aboutsummaryrefslogtreecommitdiff
path: root/backend/lib/CodeGen/LivePhysRegs.cpp
blob: 9bbc06912c1d69700a08198137fffdb5071e6eb8 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// SPDX-License-Identifier: GPL-3.0-or-later

#include "rcc/CodeGen/LivePhysRegs.h"

#include "rcc/CodeGen/MachineBasicBlock.h"
#include "rcc/CodeGen/MachineFunction.h"
#include "rcc/CodeGen/MachineInstr.h"
#include "rcc/CodeGen/MachineOperand.h"
#include "rcc/CodeGen/RegisterMask.h"
#include "rcc/CodeGen/TargetDescription.h"
#include "rcc/CodeGen/TargetRegisterInfo.h"

#include <algorithm>
#include <cstddef>
#include <ranges>
#include <vector>

namespace rcc {

namespace {
struct OperandFlagUpdate final {
  MachineOperand *Operand = nullptr;
  RegisterOperandFlags Flags;
};

[[nodiscard]] bool intersects(const LiveRegUnits &Live,
                              const std::vector<PhysicalUnitAccess> &Units) noexcept {
  return std::ranges::any_of(Units, [&Live](const PhysicalUnitAccess &Unit) {
    return Live.intersects(Unit.Unit, Unit.Lanes);
  });
}

void addUnits(LiveRegUnits &Live, const std::vector<PhysicalUnitAccess> &Units) {
  for (const PhysicalUnitAccess &Unit : Units)
    Live.add(Unit.Unit, Unit.Lanes);
}

void removeUnits(LiveRegUnits &Live, const std::vector<PhysicalUnitAccess> &Units) {
  for (const PhysicalUnitAccess &Unit : Units)
    Live.remove(Unit.Unit, Unit.Lanes);
}

[[nodiscard]] OperandFlagUpdate &getFlagUpdate(std::vector<OperandFlagUpdate> &Updates,
                                               MachineOperand &Operand) {
  const auto Position = std::ranges::find_if(
      Updates, [&Operand](const OperandFlagUpdate &Update) { return Update.Operand == &Operand; });
  if (Position != Updates.end())
    return *Position;
  Updates.push_back({&Operand, Operand.registerFlags()});
  return Updates.back();
}

[[nodiscard]] Expected<std::vector<OperandFlagUpdate>>
buildBlockLivenessRepairPlan(MachineBasicBlock &Block, const TargetDescription &Target,
                             const MachineBlockLiveness &Boundary) {
  LiveRegUnits Live = Boundary.PhysicalLiveOut;
  std::vector<OperandFlagUpdate> Updates;
  for (MachineInstr *Instruction = Block.lastInstruction(); Instruction != nullptr;
       Instruction = Instruction->previous()) {
    for (const MachineOperand &Operand : Instruction->operands()) {
      const auto *Mask = Operand.getIf<LiveOutRegisterMaskOperand>();
      if (Mask == nullptr)
        continue;
      if (!Mask->Mask) {
        return makeUnexpected(ErrorCode::VerificationError,
                              "post-RA liveness repair encountered a null live-out mask");
      }
      for (const PhysicalRegisterDesc &Register : Target.registers().physicalRegisters()) {
        if (Mask->Mask->preserves(Register.Reg))
          addPhysicalRegister(Live, Register);
      }
    }

    for (std::size_t Index = 0; Index < Instruction->operandCount(); ++Index) {
      MachineOperand *Operand = Instruction->operandAt(Index);
      if (Operand == nullptr || !Operand->isRegister())
        continue;
      OperandFlagUpdate &Update = getFlagUpdate(Updates, *Operand);
      Update.Flags.clear(RegisterOperandFlag::Dead);
      if (!Operand->isDef())
        continue;
      const Register *Reg = Operand->getIf<Register>();
      if (Reg == nullptr || Reg->isNoRegister())
        continue;
      auto Units = physicalOperandUnits(*Operand, Target);
      if (!Units)
        return std::unexpected(Units.error());
      if (!intersects(Live, *Units))
        Update.Flags.set(RegisterOperandFlag::Dead);
    }

    for (const MachineOperand &Operand : Instruction->operands()) {
      if (Operand.isRegister() && Operand.isDef()) {
        const Register *Reg = Operand.getIf<Register>();
        if (Reg == nullptr || Reg->isNoRegister())
          continue;
        auto Units = physicalOperandUnits(Operand, Target);
        if (!Units)
          return std::unexpected(Units.error());
        removeUnits(Live, *Units);
        continue;
      }
      const auto *Mask = Operand.getIf<RegisterMaskOperand>();
      if (Mask == nullptr)
        continue;
      if (!Mask->Mask) {
        return makeUnexpected(ErrorCode::VerificationError,
                              "post-RA liveness repair encountered a null register mask");
      }
      LiveRegUnits Clobbers;
      for (const PhysicalRegisterDesc &Register : Target.registers().physicalRegisters()) {
        if (Mask->Mask->clobbers(Register.Reg))
          addPhysicalRegister(Clobbers, Register);
      }
      Live.subtract(Clobbers);
    }

    for (std::size_t Index = 0; Index < Instruction->operandCount(); ++Index) {
      MachineOperand *Operand = Instruction->operandAt(Index);
      if (Operand == nullptr || !Operand->isRegister())
        continue;
      OperandFlagUpdate &Update = getFlagUpdate(Updates, *Operand);
      Update.Flags.clear(RegisterOperandFlag::Kill);
      if (!Operand->isUse() || Operand->isDebugUse() ||
          Operand->registerFlags().has(RegisterOperandFlag::Undef)) {
        continue;
      }
      const Register *Reg = Operand->getIf<Register>();
      if (Reg == nullptr || Reg->isNoRegister())
        continue;
      if (isReservedPhysicalRegister(*Reg, Block.function(), Target))
        continue;
      auto Units = physicalOperandUnits(*Operand, Target);
      if (!Units)
        return std::unexpected(Units.error());
      if (!intersects(Live, *Units))
        Update.Flags.set(RegisterOperandFlag::Kill);
      addUnits(Live, *Units);
    }
  }
  if (Live != Boundary.PhysicalLiveIn) {
    return makeUnexpected(ErrorCode::InternalError,
                          "post-RA local liveness repair changed the block boundary");
  }
  return Updates;
}
} // namespace

Expected<bool> repairPostRAMachineBlockLiveness(std::span<MachineBasicBlock *const> Blocks,
                                                const TargetDescription &Target,
                                                const LiveVariablesAnalysis::Result &Liveness) {
  std::vector<OperandFlagUpdate> Updates;
  for (MachineBasicBlock *Block : Blocks) {
    const MachineBlockLiveness *Boundary = Liveness.block(*Block);
    if (Boundary == nullptr) {
      return makeUnexpected(ErrorCode::InternalError,
                            "post-RA scheduler lost a block liveness boundary");
    }
    auto BlockUpdates = buildBlockLivenessRepairPlan(*Block, Target, *Boundary);
    if (!BlockUpdates)
      return std::unexpected(BlockUpdates.error());
    Updates.insert(Updates.end(), BlockUpdates->begin(), BlockUpdates->end());
  }
  bool Changed = false;
  for (const OperandFlagUpdate &Update : Updates) {
    Changed |= Update.Operand->registerFlags() != Update.Flags;
    auto Applied = Update.Operand->setRegisterFlags(Update.Flags);
    if (!Applied)
      return std::unexpected(Applied.error());
  }
  return Changed;
}

} // namespace rcc