aboutsummaryrefslogtreecommitdiff
path: root/backend/include/rcc/CodeGen/LiveIntervals.h
blob: a45c59326f2f4e3646ec021f4e347e3426b7f688 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef RCC_CODEGEN_LIVEINTERVALS_H
#define RCC_CODEGEN_LIVEINTERVALS_H

#include "rcc/CodeGen/LiveInterval.h"
#include "rcc/CodeGen/MachinePass.h"
#include "rcc/CodeGen/RegisterMask.h"

#include <cstdint>
#include <memory>
#include <optional>
#include <span>
#include <string_view>
#include <unordered_map>
#include <vector>

namespace rcc {

class MachineFunction;
class MachineInstr;
class TargetDescription;

struct RegMaskPosition final {
  SlotIndex Position;
  MachineInstrStableID Instruction;
  std::shared_ptr<const RegisterMask> Mask;
};

class LiveIntervalsAnalysis final {
public:
  class Result final {
  public:
    [[nodiscard]] const SlotIndexesAnalysis::Result &slotIndexes() const noexcept {
      return Indexes;
    }
    [[nodiscard]] SlotIndex slot(const MachineInstr &Instruction) const noexcept {
      return Indexes.instructionIndex(Instruction);
    }
    [[nodiscard]] const LiveInterval *interval(Register Reg) const noexcept;
    [[nodiscard]] LiveInterval *interval(Register Reg) noexcept;
    [[nodiscard]] const LiveRange *regUnitRange(RegisterUnitID Unit) const noexcept;
    [[nodiscard]] std::span<const RegMaskPosition> regMaskPositions() const noexcept {
      return RegMasks;
    }
    [[nodiscard]] bool interferes(Register Left, Register Right) const noexcept;
    [[nodiscard]] bool regMaskClobbers(const LiveInterval &Interval,
                                       PhysicalRegister Candidate) const noexcept;
    [[nodiscard]] Expected<void> shrinkToUses(Register Reg, std::span<const SlotIndex> Uses);

    [[nodiscard]] bool invalidate(const MachineFunction &,
                                  const PreservedAnalyses &Preserved) const noexcept;

  private:
    SlotIndexesAnalysis::Result Indexes;
    std::unordered_map<Register, LiveInterval> Intervals;
    std::unordered_map<RegisterUnitID, LiveRange> RegisterUnitRanges;
    std::vector<RegMaskPosition> RegMasks;

    friend class LiveIntervalsAnalysis;
  };

  [[nodiscard]] static constexpr std::string_view name() noexcept {
    return "LiveIntervals";
  }

  [[nodiscard]] static Expected<Result>
  run(MachineFunction &Function, const TargetDescription &Target, MachineAnalysisManager &Analyses);
};

} // namespace rcc

#endif // RCC_CODEGEN_LIVEINTERVALS_H