aboutsummaryrefslogtreecommitdiff
path: root/backend/lib/Target/RISCV/GISel/RISCVRegisterBankInfo.cpp
blob: 7c60219d380e3a60e27524685cd144c757a2866a (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// SPDX-License-Identifier: GPL-3.0-or-later

#include "RISCVRegisterBankInfo.h"

#include "rcc/CodeGen/GenericOpcodes.h"
#include "rcc/CodeGen/MachineFunction.h"
#include "rcc/CodeGen/MachineInstr.h"
#include "rcc/CodeGen/MachineRegisterInfo.h"
#include "rcc/CodeGen/TargetDescription.h"
#include "rcc/Target/RISCV/RISCVRegisters.h"

#include <cstdint>
#include <optional>
#include <ranges>
#include <string_view>
#include <unordered_set>
#include <utility>
#include <vector>

namespace rcc::riscv {
namespace {

[[nodiscard]] bool isRVFloatType(LLT Type) noexcept {
  return Type.isFloat() && (Type.scalarBitWidth() == 32 || Type.scalarBitWidth() == 64);
}

[[nodiscard]] bool isScalarGPRType(LLT Type) noexcept {
  return (Type.isPointer() && Type.addressSpace() == 0 && Type.scalarBitWidth() == 64) ||
         ((Type.isInteger() || Type.isAnyScalar()) && Type.scalarBitWidth() >= 1 &&
          Type.scalarBitWidth() <= 64);
}

[[nodiscard]] constexpr RegisterBankID gprBank() noexcept {
  return RegisterBankID::fromValue(static_cast<std::uint32_t>(RegisterBank::GPR));
}

[[nodiscard]] constexpr RegisterBankID fprBank() noexcept {
  return RegisterBankID::fromValue(static_cast<std::uint32_t>(RegisterBank::FPR));
}

[[nodiscard]] std::optional<std::size_t> operandIndex(const MachineInstr &Instruction,
                                                      const MachineOperand &Operand) noexcept {
  for (std::size_t Index = 0; Index < Instruction.operandCount(); ++Index) {
    if (Instruction.operandAt(Index) == &Operand)
      return Index;
  }
  return std::nullopt;
}

[[nodiscard]] std::optional<RegisterBankID>
physicalRegisterBank(Register Reg, const TargetDescription &Target) noexcept {
  const std::optional<PhysicalRegister> Physical = Reg.physicalRegister();
  if (!Physical)
    return std::nullopt;
  for (const RegisterBankDesc &Bank : Target.registers().registerBanks()) {
    if (Target.registers().bankContainsRegister(Bank.ID, *Physical))
      return Bank.ID;
  }
  return std::nullopt;
}

[[nodiscard]] bool isFloatingGenericOpcode(GenericOpcode OpcodeValue) noexcept {
  switch (OpcodeValue) {
  case GenericOpcode::G_FRAME_INDEX:
  case GenericOpcode::G_FREEZE:
  case GenericOpcode::G_FENCE:
  case GenericOpcode::G_FSHL:
  case GenericOpcode::G_FSHR:
    return false;
  default:
    return getGenericOpcodeName(OpcodeValue).starts_with("G_F");
  }
}

[[nodiscard]] std::optional<RegisterBankID>
requiredBankForOperand(const MachineInstr &Instruction, const MachineOperand &Operand,
                       const TargetDescription &Target) {
  const std::optional<std::size_t> Index = operandIndex(Instruction, Operand);
  if (!Index)
    return std::nullopt;

  if (const InstructionDescriptor *Descriptor = Target.instructionDescriptor(Instruction.opcode());
      Descriptor != nullptr && *Index < Descriptor->Operands.size() &&
      Descriptor->Operands[*Index].RegBank) {
    return Descriptor->Operands[*Index].RegBank;
  }

  if (Instruction.opcode() == opcode(GenericOpcode::COPY)) {
    if (!Operand.isDef())
      return std::nullopt;
    for (const MachineOperand &Other : Instruction.operands()) {
      if (&Other == &Operand)
        continue;
      const Register *OtherReg = Other.getIf<Register>();
      if (OtherReg != nullptr && Other.isUse() && OtherReg->isPhysical())
        return physicalRegisterBank(*OtherReg, Target);
    }
    return std::nullopt;
  }

  if (!isGenericOpcode(Instruction.opcode()))
    return std::nullopt;
  const GenericOpcode Generic = static_cast<GenericOpcode>(Instruction.opcode().value());
  switch (Generic) {
  case GenericOpcode::G_FPEXTLOAD:
  case GenericOpcode::G_FPTRUNCSTORE:
    return *Index == 0 ? fprBank() : gprBank();
  case GenericOpcode::G_FPTOSI:
  case GenericOpcode::G_FPTOUI:
  case GenericOpcode::G_FPTOSI_SAT:
  case GenericOpcode::G_FPTOUI_SAT:
  case GenericOpcode::G_LROUND:
  case GenericOpcode::G_LLROUND:
    return *Index == 0 ? gprBank() : fprBank();
  case GenericOpcode::G_SITOFP:
  case GenericOpcode::G_UITOFP:
    return *Index == 0 ? fprBank() : gprBank();
  case GenericOpcode::G_FCMP:
  case GenericOpcode::G_IS_FPCLASS:
    return *Index == 0 ? gprBank() : fprBank();
  case GenericOpcode::G_FPOWI:
  case GenericOpcode::G_FLDEXP:
    return *Index < 2 ? fprBank() : gprBank();
  case GenericOpcode::G_FFREXP:
    return *Index == 1 ? gprBank() : fprBank();
  case GenericOpcode::G_LOAD:
  case GenericOpcode::G_SEXTLOAD:
  case GenericOpcode::G_ZEXTLOAD:
    return *Index == 1 ? std::optional<RegisterBankID>(gprBank()) : std::nullopt;
  case GenericOpcode::G_STORE:
    return *Index == 1 ? std::optional<RegisterBankID>(gprBank()) : std::nullopt;
  case GenericOpcode::G_SELECT:
    return *Index == 1 ? std::optional<RegisterBankID>(gprBank()) : std::nullopt;
  case GenericOpcode::G_IMPLICIT_DEF:
  case GenericOpcode::G_PHI:
    return std::nullopt;
  default:
    return isFloatingGenericOpcode(Generic) ? fprBank() : gprBank();
  }
}

void appendConnectedRegisters(const MachineInstr &Instruction, const MachineOperand &Operand,
                              std::vector<Register> &Pending) {
  const std::optional<std::size_t> Index = operandIndex(Instruction, Operand);
  if (!Index)
    return;
  const bool IsPhi = Instruction.opcode() == opcode(GenericOpcode::G_PHI);
  if (!IsPhi)
    return;
  for (std::size_t OtherIndex = 0; OtherIndex < Instruction.operandCount(); ++OtherIndex) {
    if (OtherIndex == *Index)
      continue;
    const MachineOperand *Other = Instruction.operandAt(OtherIndex);
    const Register *OtherReg = Other == nullptr ? nullptr : Other->getIf<Register>();
    if (OtherReg != nullptr && OtherReg->isVirtual())
      Pending.push_back(*OtherReg);
  }
}

[[nodiscard]] Expected<std::optional<RegisterBankID>>
requiredBankForRegister(const MachineFunction &Function, Register Reg,
                        const TargetDescription &Target) {
  std::vector<Register> Pending{Reg};
  std::unordered_set<Register> Visited;
  std::optional<RegisterBankID> Required;
  while (!Pending.empty()) {
    const Register Current = Pending.back();
    Pending.pop_back();
    if (!Visited.insert(Current).second)
      continue;
    for (const MachineOperand &Operand : Function.registerInfo().regOperands(Current)) {
      const MachineInstr *Instruction = Operand.parent();
      if (Instruction == nullptr)
        continue;
      const std::optional<RegisterBankID> OperandBank =
          requiredBankForOperand(*Instruction, Operand, Target);
      if (OperandBank) {
        if (Required && *Required != *OperandBank) {
          return makeUnexpected(ErrorCode::VerificationError,
                                "RISC-V virtual register has conflicting register-bank uses");
        }
        Required = *OperandBank;
      }
      appendConnectedRegisters(*Instruction, Operand, Pending);
    }
  }
  return Required;
}

[[nodiscard]] bool hasFloatingCopyUse(const MachineFunction &Function, Register Reg,
                                      const TargetDescription &Target) {
  for (const MachineOperand &Operand : Function.registerInfo().regOperands(Reg)) {
    const MachineInstr *Instruction = Operand.parent();
    if (!Operand.isUse() || Instruction == nullptr ||
        Instruction->opcode() != opcode(GenericOpcode::COPY))
      continue;
    for (const MachineOperand &Other : Instruction->operands()) {
      const Register *OtherReg = Other.getIf<Register>();
      if (!Other.isDef() || OtherReg == nullptr || !OtherReg->isPhysical())
        continue;
      if (physicalRegisterBank(*OtherReg, Target) == fprBank())
        return true;
    }
  }
  return false;
}

[[nodiscard]] Expected<RegisterClassID> requiredRegisterClass(const MachineFunction &Function,
                                                              Register Reg,
                                                              const TargetDescription &Target) {
  if (!Reg.isVirtual())
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "RISC-V register-class selection requires a virtual register");
  auto Info = Function.registerInfo().virtualRegisterInfo(Reg);
  if (!Info)
    return std::unexpected(Info.error());
  if (!Info->get().Type) {
    return makeUnexpected(ErrorCode::VerificationError,
                          "RISC-V selection requires a type for every virtual register");
  }
  const VirtualRegisterInfo &RegisterInfo = Info->get();
  const LLT Type = RegisterInfo.Type.value_or(LLT{});
  RegisterClassID RequiredClass;
  if ((RegisterInfo.Bank && *RegisterInfo.Bank == fprBank()) || Type.isFloat()) {
    if (Type.scalarBitWidth() != 32 && Type.scalarBitWidth() != 64) {
      return makeUnexpected(ErrorCode::Unsupported,
                            "RISC-V RV64GC has no floating register class for type '" +
                                Type.toString() + "'");
    }
    RequiredClass =
        Type.scalarBitWidth() == 32
            ? RegisterClassID::fromValue(static_cast<std::uint32_t>(RegisterClass::FPR32))
            : RegisterClassID::fromValue(static_cast<std::uint32_t>(RegisterClass::FPR64));
  } else if (isScalarGPRType(Type)) {
    RequiredClass = RegisterClassID::fromValue(static_cast<std::uint32_t>(RegisterClass::GPR));
  } else {
    return makeUnexpected(ErrorCode::Unsupported,
                          "RISC-V RV64GC has no register class for type '" + Type.toString() + "'");
  }

  if (Target.registers().registerClass(RequiredClass) == nullptr) {
    return makeUnexpected(ErrorCode::InternalError,
                          "RISC-V target description is missing a required register class");
  }
  if (RegisterInfo.Bank && !Target.registers().bankCoversClass(*RegisterInfo.Bank, RequiredClass)) {
    return makeUnexpected(ErrorCode::VerificationError,
                          "RISC-V virtual register bank cannot cover its required class");
  }
  if (RegisterInfo.RegClass &&
      !Target.registers().isSubclass(*RegisterInfo.RegClass, RequiredClass)) {
    return makeUnexpected(ErrorCode::VerificationError,
                          "RISC-V virtual register has a conflicting register class");
  }
  return RequiredClass;
}

} // namespace

Expected<RegisterBankSelection>
RISCVRegisterBankInfo::selectBank(const MachineFunction &Function, Register Reg,
                                  const VirtualRegisterInfo &Info, const MachineInstr *,
                                  const TargetDescription &Target) const {
  if (!Info.Type) {
    return makeUnexpected(ErrorCode::VerificationError,
                          "RISC-V register-bank selection requires a virtual-register type");
  }

  auto Required = requiredBankForRegister(Function, Reg, Target);
  if (!Required)
    return std::unexpected(Required.error());
  const bool IsConstrained = Required->has_value() || Info.Bank.has_value();
  const RegisterBankID Selected =
      Required->value_or(isRVFloatType(*Info.Type) ? fprBank() : gprBank());
  if (Info.Bank && *Info.Bank != Selected) {
    return makeUnexpected(ErrorCode::VerificationError,
                          "RISC-V virtual register has a conflicting register bank");
  }
  return RegisterBankSelection{.Bank = Selected, .IsConstrained = IsConstrained};
}

Expected<RegisterBankInstructionMapping>
RISCVRegisterBankInfo::getInstructionMapping(const MachineInstr &Instruction,
                                             const MachineFunction &Function,
                                             const TargetDescription &Target) const {
  if (Instruction.opcode() != opcode(GenericOpcode::G_SELECT))
    return RegisterBankInstructionMapping{};
  if (Instruction.operandCount() != 4) {
    return makeUnexpected(ErrorCode::VerificationError,
                          "RISC-V G_SELECT register-bank mapping requires four operands");
  }

  RegisterBankInstructionMapping Mapping(Instruction.operandCount());
  Mapping[1] = gprBank();
  unsigned FloatingConstraints = 0;

  const Register *Result = Instruction.operandAt(0)->getIf<Register>();
  if (Result == nullptr || !Result->isVirtual()) {
    return makeUnexpected(ErrorCode::VerificationError,
                          "RISC-V G_SELECT result must be a virtual register");
  }
  auto ResultRequirement = requiredBankForRegister(Function, *Result, Target);
  if (!ResultRequirement)
    return std::unexpected(ResultRequirement.error());
  if (*ResultRequirement == fprBank() || hasFloatingCopyUse(Function, *Result, Target))
    ++FloatingConstraints;

  for (const std::size_t Index : {std::size_t{2}, std::size_t{3}}) {
    const Register *Source = Instruction.operandAt(Index)->getIf<Register>();
    if (Source == nullptr || !Source->isVirtual()) {
      return makeUnexpected(ErrorCode::VerificationError,
                            "RISC-V G_SELECT sources must be virtual registers");
    }
    auto SourceInfo = Function.registerInfo().virtualRegisterInfo(*Source);
    if (!SourceInfo)
      return std::unexpected(SourceInfo.error());
    if (SourceInfo->get().Bank == fprBank())
      ++FloatingConstraints;
  }

  const RegisterBankID ValueBank = FloatingConstraints >= 2 ? fprBank() : gprBank();
  Mapping[0] = ValueBank;
  Mapping[2] = ValueBank;
  Mapping[3] = ValueBank;
  return Mapping;
}

Expected<void> RISCVRegisterBankInfo::constrainSelectedInstruction(
    MachineInstr &Instruction, MachineFunction &Function, const TargetDescription &Target) const {
  std::vector<std::pair<Register, RegisterClassID>> PlannedClasses;
  for (const MachineOperand &Operand : Instruction.operands()) {
    const Register *Reg = Operand.getIf<Register>();
    if (Reg == nullptr || !Reg->isVirtual())
      continue;
    auto Required = requiredRegisterClass(Function, *Reg, Target);
    if (!Required)
      return std::unexpected(Required.error());
    if (std::ranges::find_if(PlannedClasses, [Reg](const auto &Entry) {
          return Entry.first == *Reg;
        }) == PlannedClasses.end())
      PlannedClasses.emplace_back(*Reg, *Required);
  }
  for (const auto &[Reg, Required] : PlannedClasses) {
    auto Info = Function.registerInfo().virtualRegisterInfo(Reg);
    if (!Info)
      return std::unexpected(Info.error());
    if (Info->get().RegClass)
      continue;
    auto SetClass = Function.registerInfo().setRegisterClass(Reg, Required);
    if (!SetClass)
      return std::unexpected(SetClass.error());
  }
  return {};
}

Expected<void> RISCVRegisterBankInfo::constrainRegister(MachineFunction &Function, Register Reg,
                                                        const TargetDescription &Target) const {
  auto Info = Function.registerInfo().virtualRegisterInfo(Reg);
  if (!Info)
    return std::unexpected(Info.error());
  auto Bank = selectBank(Function, Reg, Info->get(), nullptr, Target);
  if (!Bank)
    return std::unexpected(Bank.error());
  auto RequiredClass = requiredRegisterClass(Function, Reg, Target);
  if (!RequiredClass)
    return std::unexpected(RequiredClass.error());
  if (!Info->get().Bank) {
    auto SetBank = Function.registerInfo().setRegisterBank(Reg, Bank->Bank);
    if (!SetBank)
      return std::unexpected(SetBank.error());
  }
  if (!Info->get().RegClass) {
    auto SetClass = Function.registerInfo().setRegisterClass(Reg, *RequiredClass);
    if (!SetClass)
      return std::unexpected(SetClass.error());
  }
  return {};
}

Expected<void> RISCVRegisterBankInfo::validateInstruction(const MachineInstr &Instruction,
                                                          const MachineFunction &Function,
                                                          const TargetDescription &Target) const {
  for (const MachineOperand &Operand : Instruction.operands()) {
    const Register *Reg = Operand.getIf<Register>();
    if (Reg == nullptr || !Reg->isVirtual())
      continue;
    auto Required = requiredRegisterClass(Function, *Reg, Target);
    if (!Required)
      return std::unexpected(Required.error());
  }
  return {};
}

} // namespace rcc::riscv