aboutsummaryrefslogtreecommitdiff
path: root/backend/include/rcc/CodeGen/LiveRegMatrix.h
blob: fd1bac4372b978e8aa08287868cd7879ec61f537 (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_LIVEREGMATRIX_H
#define RCC_CODEGEN_LIVEREGMATRIX_H

#include "rcc/CodeGen/LiveIntervals.h"
#include "rcc/CodeGen/TargetRegisterInfo.h"

#include <cstdint>
#include <optional>
#include <span>
#include <unordered_map>
#include <utility>
#include <vector>

namespace rcc {

class MachineFunction;
class TargetDescription;

enum class RegisterInterferenceKind : std::uint8_t {
  Free,
  VirtualRegister,
  FixedRegister,
  RegisterMask,
  RegisterUnit,
};

struct RegisterInterference final {
  RegisterInterferenceKind Kind = RegisterInterferenceKind::Free;
  std::optional<Register> VirtualReg;
  std::optional<PhysicalRegister> FixedReg;
  std::optional<RegisterUnitID> Unit;
  std::optional<MachineInstrStableID> Instruction;
};

class LiveRegMatrix final {
public:
  LiveRegMatrix(const MachineFunction &Function, const TargetDescription &Target,
                const LiveIntervalsAnalysis::Result &Intervals) noexcept
      : Function(&Function), Target(&Target), Intervals(&Intervals) {}

  [[nodiscard]] Expected<void> assign(Register VirtualReg, PhysicalRegister PhysicalReg);
  [[nodiscard]] Expected<void> unassign(Register VirtualReg);
  [[nodiscard]] std::optional<PhysicalRegister> assignment(Register VirtualReg) const noexcept;
  [[nodiscard]] RegisterInterference checkInterference(Register VirtualReg,
                                                       PhysicalRegister PhysicalReg) const noexcept;
  [[nodiscard]] std::vector<RegisterInterference>
  collectInterferences(Register VirtualReg, PhysicalRegister PhysicalReg) const;

  [[nodiscard]] std::span<const std::pair<Register, PhysicalRegister>>
  assignments() const noexcept {
    return OrderedAssignments;
  }

private:
  [[nodiscard]] bool intervalsOverlapOnCandidates(const LiveInterval &Left,
                                                  PhysicalRegister LeftReg,
                                                  const LiveInterval &Right,
                                                  PhysicalRegister RightReg) const noexcept;
  [[nodiscard]] bool intervalOverlapsUnit(const LiveInterval &Interval, RegisterUnitLane Unit,
                                          const LiveRange &UnitRange) const noexcept;

  const MachineFunction *Function;
  const TargetDescription *Target;
  const LiveIntervalsAnalysis::Result *Intervals;
  std::unordered_map<Register, PhysicalRegister> Assignments;
  std::vector<std::pair<Register, PhysicalRegister>> OrderedAssignments;
};

} // namespace rcc

#endif // RCC_CODEGEN_LIVEREGMATRIX_H