aboutsummaryrefslogtreecommitdiff
path: root/backend/lib/CodeGen/GlobalISel/InstructionSelect.cpp
blob: 344e63ed243e979665453c17798d8768e271e6e1 (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
// SPDX-License-Identifier: GPL-3.0-or-later

#include "rcc/CodeGen/GlobalISel/InstructionSelect.h"

#include "rcc/CodeGen/LiveVariables.h"
#include "rcc/CodeGen/MachineBasicBlock.h"
#include "rcc/CodeGen/MachineFunction.h"
#include "rcc/CodeGen/MachineFunctionCloner.h"
#include "rcc/CodeGen/MachineInstr.h"
#include "rcc/CodeGen/MachineRegisterInfo.h"
#include "rcc/CodeGen/TargetDescription.h"

#include <algorithm>
#include <cstdint>
#include <optional>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>

namespace rcc {
namespace {

[[nodiscard]] const MachinePassContract &instructionSelectContract() {
  static const MachinePassContract Contract{
      .Name = "instruction-select",
      .Requires = {MachineFunctionProperty::Legalized, MachineFunctionProperty::RegBankSelected},
      .Sets = {MachineFunctionProperty::Selected},
      .Clears = {},
      .RequiredAnalyses = {},
      .VerificationStage = PipelineStage::Selected,
  };
  return Contract;
}

struct OpcodeState final {
  MachineInstrStableID Instruction;
  Opcode OpcodeValue;
  std::uint64_t OperandFingerprint = 0;

  friend bool operator==(const OpcodeState &Left, const OpcodeState &Right) noexcept = default;
};

struct OpcodeStateHash final {
  [[nodiscard]] std::size_t operator()(const OpcodeState &State) const noexcept {
    return std::hash<std::uint64_t>{}(State.Instruction.value()) ^
           (std::hash<std::uint32_t>{}(State.OpcodeValue.value()) << 1U) ^
           (std::hash<std::uint64_t>{}(State.OperandFingerprint) << 2U);
  }
};

void combineFingerprint(std::uint64_t &State, std::uint64_t Value) noexcept {
  State ^= Value + 0x9e3779b97f4a7c15ULL + (State << 6U) + (State >> 2U);
}

[[nodiscard]] std::uint64_t operandFingerprint(const MachineInstr &Instruction) noexcept {
  std::uint64_t Result = 0xcbf29ce484222325ULL;
  combineFingerprint(Result, Instruction.flags().rawValue());
  combineFingerprint(Result, Instruction.operandCount());
  for (const MachineOperand &Operand : Instruction.operands()) {
    combineFingerprint(Result, static_cast<std::uint64_t>(Operand.kind()));
    combineFingerprint(Result, Operand.targetFlags());
    combineFingerprint(Result, Operand.registerFlags().rawValue());
    combineFingerprint(Result, Operand.subReg().value());
    if (const Register *Reg = Operand.getIf<Register>())
      combineFingerprint(Result, Reg->opaqueValue());
    else if (const std::int64_t *Immediate = Operand.getIf<std::int64_t>())
      combineFingerprint(Result, static_cast<std::uint64_t>(*Immediate));
    else if (const auto *Integer = Operand.getIf<IntegerImmediateOperand>())
      combineFingerprint(Result, Integer->Value.bitWidth());
    else if (const auto *Floating = Operand.getIf<FloatingImmediateOperand>())
      combineFingerprint(Result, static_cast<std::uint64_t>(Floating->Bits.semantics()));
    else if (const auto *Block = Operand.getIf<BasicBlockOperand>())
      combineFingerprint(Result, Block->Block == nullptr ? 0 : Block->Block->stableID().value());
  }
  return Result;
}

[[nodiscard]] OpcodeState stateOf(const MachineInstr &Instruction) noexcept {
  return {Instruction.stableID(), Instruction.opcode(), operandFingerprint(Instruction)};
}

[[nodiscard]] std::vector<MachineInstr *> layoutInstructions(MachineFunction &Function) {
  std::vector<MachineInstr *> Result;
  for (MachineBasicBlock *Block = Function.firstBlock(); Block != nullptr; Block = Block->next())
    for (MachineInstr *Instruction = Block->firstInstruction(); Instruction != nullptr;
         Instruction = Instruction->next())
      Result.push_back(Instruction);
  return Result;
}

[[nodiscard]] std::vector<MachineBasicBlock *> stableCFGPostOrder(MachineFunction &Function) {
  std::vector<MachineBasicBlock *> Roots;
  for (MachineBasicBlock *Block = Function.firstBlock(); Block != nullptr; Block = Block->next())
    Roots.push_back(Block);
  std::ranges::sort(Roots, {},
                    [](const MachineBasicBlock *Block) { return Block->stableID().value(); });
  if (MachineBasicBlock *Entry = Function.firstBlock(); Entry != nullptr) {
    const auto Position = std::ranges::find(Roots, Entry);
    if (Position != Roots.end())
      std::rotate(Roots.begin(), Position, std::next(Position));
  }

  struct TraversalFrame final {
    MachineBasicBlock *Block = nullptr;
    std::vector<MachineBasicBlock *> Successors;
    std::size_t NextSuccessor = 0;
  };
  const auto MakeFrame = [](MachineBasicBlock &Block) {
    TraversalFrame Frame;
    Frame.Block = &Block;
    for (const MachineSuccessor &Successor : Block.successors()) {
      if (Successor.Block != nullptr)
        Frame.Successors.push_back(Successor.Block);
    }
    std::ranges::sort(Frame.Successors, {}, [](const MachineBasicBlock *Successor) {
      return Successor->stableID().value();
    });
    return Frame;
  };

  std::vector<MachineBasicBlock *> Result;
  std::unordered_set<MachineBasicBlock *> Visited;
  std::vector<TraversalFrame> Stack;
  for (MachineBasicBlock *Root : Roots) {
    if (!Visited.insert(Root).second)
      continue;
    Stack.push_back(MakeFrame(*Root));
    while (!Stack.empty()) {
      TraversalFrame &Frame = Stack.back();
      if (Frame.NextSuccessor < Frame.Successors.size()) {
        MachineBasicBlock *Successor = Frame.Successors[Frame.NextSuccessor++];
        if (Visited.insert(Successor).second)
          Stack.push_back(MakeFrame(*Successor));
        continue;
      }
      Result.push_back(Frame.Block);
      Stack.pop_back();
    }
  }
  return Result;
}

[[nodiscard]] std::vector<MachineInstr *> bottomUpInstructions(MachineFunction &Function) {
  std::vector<MachineInstr *> Result;
  for (MachineBasicBlock *Block : stableCFGPostOrder(Function)) {
    for (MachineInstr *Instruction = Block->lastInstruction(); Instruction != nullptr;
         Instruction = Instruction->previous())
      Result.push_back(Instruction);
  }
  return Result;
}

[[nodiscard]] Expected<void> requireProperties(const MachineFunction &Function,
                                               const MachinePassContract &Contract) {
  const MachineFunctionProperties Missing = Contract.Requires.missingFrom(Function.properties());
  if (!Missing.empty()) {
    return makeUnexpected(ErrorCode::VerificationError,
                          "pass '" + Contract.Name +
                              "' requires properties: " + Missing.toString());
  }
  return {};
}

[[nodiscard]] Error passFailure(std::string Message,
                                ErrorCode Code = ErrorCode::VerificationError) {
  return Error(Code, std::move(Message));
}

[[nodiscard]] Error prefixedError(const Error &Source, std::string Prefix) {
  Prefix.append(Source.message());
  return Error(Source.code(), std::move(Prefix));
}

[[nodiscard]] Expected<void> repairTrackedLiveness(MachineFunction &Function,
                                                   const TargetDescription &Target,
                                                   MachinePassResult &Result) {
  if (!Result.Changed || !Function.properties().has(MachineFunctionProperty::TracksLiveness)) {
    return {};
  }
  auto Rebuilt = rebuildMachineLiveness(Function, Target);
  if (!Rebuilt)
    return std::unexpected(Rebuilt.error());
  if (*Rebuilt)
    Result.Changes.add(MachinePassChange::Operands);
  return {};
}

} // namespace

const MachinePassContract &InstructionSelect::contract() const noexcept {
  return instructionSelectContract();
}

Expected<MachinePassResult> InstructionSelect::run(MachineFunction &Function,
                                                   const TargetDescription &Target,
                                                   MachineFunctionAnalysisManager &Analyses) {
  if (Info == nullptr)
    return std::unexpected(passFailure("instruction select has no target behavior interface",
                                       ErrorCode::InvalidArgument));
  auto Required = requireProperties(Function, contract());
  if (!Required)
    return std::unexpected(Required.error());

  auto Trial = cloneMachineFunction(
      Function, Target,
      MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
  if (!Trial) {
    return std::unexpected(
        prefixedError(Trial.error(), "instruction-selection transaction setup failed: "));
  }
  MachineActionReplay Actions(Analyses, Trial->InstructionStableIDs);
  auto TrialResult = runImplementation(*Trial->ClonedFunction, Target, Actions);
  if (!TrialResult)
    return std::unexpected(TrialResult.error());
  Actions.beginReplay();
  auto Result = runImplementation(Function, Target, Actions);
  if (!Result) {
    return std::unexpected(
        prefixedError(Result.error(),
                      "instruction selection diverged after a successful transaction preflight: "));
  }
  auto Replayed = Actions.finishReplay();
  if (!Replayed)
    return std::unexpected(Replayed.error());
  return Result;
}

Expected<MachinePassResult> InstructionSelect::runImplementation(MachineFunction &Function,
                                                                 const TargetDescription &Target,
                                                                 MachineActionReplay &Actions) {

  MachinePassResult Result;
  std::vector<MachineInstr *> Worklist;
  std::unordered_set<MachineInstrStableID> DiscoveredInstructions;
  std::unordered_set<OpcodeState, OpcodeStateHash> SeenStates;
  for (MachineInstr *Instruction : bottomUpInstructions(Function)) {
    const InstructionDescriptor *Descriptor = Target.instructionDescriptor(Instruction->opcode());
    if (Descriptor == nullptr)
      return std::unexpected(
          passFailure("instruction selector encountered an unknown instruction descriptor"));
    auto Validation = Target.validateInstruction(*Instruction, Function.registerInfo());
    if (!Validation)
      return std::unexpected(
          prefixedError(Validation.error(), "instruction selector input is invalid: "));
    if (!Descriptor->Flags.has(InstructionFlag::Generic))
      continue;
    auto Match = Info->canSelect(*Instruction, Function, Target);
    if (!Match)
      return std::unexpected(Match.error());
    Worklist.push_back(Instruction);
    DiscoveredInstructions.insert(Instruction->stableID());
  }

  for (std::size_t Index = 0; Index < Worklist.size(); ++Index) {
    MachineInstr *Instruction = Worklist[Index];
    if (Instruction->parent() == nullptr)
      return std::unexpected(passFailure("instruction selector lost its input instruction"));
    const InstructionDescriptor *CurrentDescriptor =
        Target.instructionDescriptor(Instruction->opcode());
    if (CurrentDescriptor == nullptr)
      return std::unexpected(
          passFailure("instruction selector encountered an unknown instruction descriptor"));
    if (!CurrentDescriptor->Flags.has(InstructionFlag::Generic))
      continue;
    const OpcodeState Before = stateOf(*Instruction);
    if (!SeenStates.insert(Before).second) {
      return std::unexpected(passFailure("instruction selector entered a repeated state for '" +
                                             CurrentDescriptor->Name + "'",
                                         ErrorCode::InternalError));
    }
    auto Match = Info->canSelect(*Instruction, Function, Target);
    if (!Match)
      return std::unexpected(Match.error());
    std::string Rule = "select.";
    Rule.append(CurrentDescriptor->Name);
    auto Accepted = Actions.request(Rule, Function, Instruction->stableID());
    if (!Accepted)
      return std::unexpected(Accepted.error());
    if (!*Accepted) {
      return std::unexpected(passFailure("required selection action was rejected for '" +
                                             CurrentDescriptor->Name + "'",
                                         ErrorCode::VerificationError));
    }
    auto Selection = Info->select(*Instruction, Function, Target);
    if (!Selection)
      return std::unexpected(Selection.error());
    const InstructionDescriptor *Selected = Target.instructionDescriptor(Instruction->opcode());
    if (Selected == nullptr)
      return std::unexpected(
          passFailure("instruction selector produced an unknown instruction descriptor"));
    auto Constraint = Info->constrainSelectedInstruction(*Instruction, Function, Target);
    if (!Constraint) {
      return std::unexpected(prefixedError(
          Constraint.error(), "instruction selector could not constrain selected registers: "));
    }
    auto Validation = Target.validateInstruction(*Instruction, Function.registerInfo());
    if (!Validation)
      return std::unexpected(
          prefixedError(Validation.error(), "instruction selector produced invalid MachineIR: "));
    const OpcodeState After = stateOf(*Instruction);
    if (After == Before)
      return std::unexpected(
          passFailure("instruction selector reported success without observable progress",
                      ErrorCode::InternalError));
    Result.Changed = true;
    Result.Changes.add(MachinePassChange::Instructions);
    if (Selected->Flags.has(InstructionFlag::Generic))
      Worklist.push_back(Instruction);
    for (MachineInstr *Candidate : bottomUpInstructions(Function)) {
      const InstructionDescriptor *CandidateDescriptor =
          Target.instructionDescriptor(Candidate->opcode());
      if (CandidateDescriptor == nullptr)
        return std::unexpected(
            passFailure("instruction selector produced an unknown instruction descriptor"));
      if (!CandidateDescriptor->Flags.has(InstructionFlag::Generic) ||
          !DiscoveredInstructions.insert(Candidate->stableID()).second) {
        continue;
      }
      auto CandidateMatch = Info->canSelect(*Candidate, Function, Target);
      if (!CandidateMatch)
        return std::unexpected(CandidateMatch.error());
      Worklist.push_back(Candidate);
    }
  }
  for (MachineInstr *Instruction : layoutInstructions(Function)) {
    const InstructionDescriptor *Descriptor = Target.instructionDescriptor(Instruction->opcode());
    if (Descriptor == nullptr)
      return std::unexpected(
          passFailure("instruction selector produced an unknown instruction descriptor"));
    auto Validation = Target.validateInstruction(*Instruction, Function.registerInfo());
    if (!Validation)
      return std::unexpected(
          prefixedError(Validation.error(), "instruction selector produced invalid MachineIR: "));
    if (Descriptor->Flags.has(InstructionFlag::Generic))
      return std::unexpected(
          passFailure("instruction selector left a generic opcode in the function"));
  }
  auto RegisterValidation = Target.validateRegisterInfo(Function.registerInfo());
  if (!RegisterValidation)
    return std::unexpected(prefixedError(RegisterValidation.error(),
                                         "instruction selection produced invalid registers: "));
  auto Liveness = repairTrackedLiveness(Function, Target, Result);
  if (!Liveness) {
    return std::unexpected(
        prefixedError(Liveness.error(), "instruction-selection liveness repair failed: "));
  }
  Result.Preserved = Result.Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
  return Result;
}
} // namespace rcc