aboutsummaryrefslogtreecommitdiff
path: root/backend/lib/CodeGen/MachinePseudoExpansion.cpp
blob: 8388079e6efeaddd392f287c2e5f94c7f33ac6a6 (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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
// SPDX-License-Identifier: GPL-3.0-or-later

#include "rcc/CodeGen/MachinePseudoExpansion.h"

#include "rcc/CodeGen/InstructionDescriptor.h"
#include "rcc/CodeGen/LiveVariables.h"
#include "rcc/CodeGen/MachineBasicBlock.h"
#include "rcc/CodeGen/MachineFrameInfo.h"
#include "rcc/CodeGen/MachineFunction.h"
#include "rcc/CodeGen/MachineFunctionCloner.h"
#include "rcc/CodeGen/MachineInstr.h"
#include "rcc/CodeGen/MachineOperand.h"
#include "rcc/CodeGen/MachineRegisterInfo.h"
#include "rcc/CodeGen/TargetDescription.h"
#include "rcc/CodeGen/TargetInstrInfo.h"

#include <cstddef>
#include <cstdint>
#include <limits>
#include <optional>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

namespace rcc {
namespace {

using CFGEdge = std::tuple<std::uint32_t, std::uint32_t, std::uint32_t>;

struct ResourceSnapshot final {
  std::size_t BlockCount = 0;
  std::size_t VirtualRegisterCount = 0;
  std::size_t FrameObjectCount = 0;
  bool FrameFrozen = false;
  std::vector<CFGEdge> Edges;
};

using BlockSizeSnapshot = std::vector<std::pair<MachineBlockStableID, std::uint64_t>>;

[[nodiscard]] Expected<std::uint64_t> blockSize(const MachineBasicBlock &Block,
                                                const MachineFunction &Function,
                                                const TargetDescription &Target,
                                                const TargetInstrInfo &BranchInfo) {
  std::uint64_t Size = 0;
  for (const MachineInstr *Instruction = Block.firstInstruction(); Instruction != nullptr;
       Instruction = Instruction->next()) {
    auto InstructionSize = BranchInfo.instructionSize(*Instruction, Function, Target);
    if (!InstructionSize)
      return std::unexpected(InstructionSize.error());
    if (Size > std::numeric_limits<std::uint64_t>::max() - *InstructionSize) {
      return makeUnexpected(ErrorCode::PseudoExpansionFailed, "target-late pseudo size overflowed");
    }
    Size += *InstructionSize;
  }
  return Size;
}

[[nodiscard]] Expected<BlockSizeSnapshot> snapshotBlockSizes(const MachineFunction &Function,
                                                             const TargetDescription &Target,
                                                             const TargetInstrInfo &BranchInfo) {
  BlockSizeSnapshot Result;
  for (const MachineBasicBlock *Block = Function.firstBlock(); Block != nullptr;
       Block = Block->next()) {
    auto Size = blockSize(*Block, Function, Target, BranchInfo);
    if (!Size)
      return std::unexpected(Size.error());
    Result.emplace_back(Block->stableID(), *Size);
  }
  return Result;
}

[[nodiscard]] ResourceSnapshot snapshotResources(const MachineFunction &Function) {
  ResourceSnapshot Result;
  Result.BlockCount = Function.blockCount();
  Result.VirtualRegisterCount = Function.registerInfo().virtualRegisters().size();
  Result.FrameObjectCount = Function.frameInfo().objects().size();
  Result.FrameFrozen = Function.frameInfo().isFrozen();
  for (const MachineBasicBlock *Block = Function.firstBlock(); Block != nullptr;
       Block = Block->next()) {
    for (const MachineSuccessor &Successor : Block->successors()) {
      Result.Edges.emplace_back(Block->stableID().value(), Successor.Block->stableID().value(),
                                Successor.Probability.rawValue());
    }
  }
  return Result;
}

[[nodiscard]] Expected<void> verifyResourceContract(const ResourceSnapshot &Before,
                                                    const MachineFunction &Function,
                                                    MachinePseudoExpansionPhase Phase) {
  const ResourceSnapshot After = snapshotResources(Function);
  if (Before.BlockCount != After.BlockCount || Before.Edges != After.Edges) {
    return makeUnexpected(ErrorCode::PseudoExpansionFailed,
                          "pseudo expansion changed CFG resources outside its owner contract");
  }
  if (Phase != MachinePseudoExpansionPhase::PreRA &&
      Before.VirtualRegisterCount != After.VirtualRegisterCount) {
    return makeUnexpected(ErrorCode::PseudoExpansionFailed,
                          "post-selection pseudo expansion created a virtual register");
  }
  if (Phase != MachinePseudoExpansionPhase::PrePEI &&
      (Before.FrameObjectCount != After.FrameObjectCount ||
       Before.FrameFrozen != After.FrameFrozen)) {
    return makeUnexpected(ErrorCode::PseudoExpansionFailed,
                          "post-PEI pseudo expansion changed frozen frame resources");
  }
  return {};
}

[[nodiscard]] PseudoOwner ownerFor(MachinePseudoExpansionPhase Phase) noexcept {
  switch (Phase) {
  case MachinePseudoExpansionPhase::PreRA:
    return PseudoOwner::PreRAExpansion;
  case MachinePseudoExpansionPhase::PrePEI:
    return PseudoOwner::PrePEIExpansion;
  case MachinePseudoExpansionPhase::PostRA:
    return PseudoOwner::PostRAExpansion;
  case MachinePseudoExpansionPhase::TargetLate:
    return PseudoOwner::TargetLateExpansion;
  }
  return PseudoOwner::None;
}

[[nodiscard]] const MachinePassContract &preparationContract() {
  static const MachinePassContract Contract = [] {
    MachinePassContract Value{
        .Name = "prepare-post-ra-pseudos",
        .Requires = {MachineFunctionProperty::Selected},
        .Sets = {},
        .Clears = {},
        .RequiredAnalyses = {},
        .VerificationStage = PipelineStage::Selected,
    };
    Value.ThreadSafety = PassThreadSafety::ThreadSafe;
    Value.DeclaredChanges = {MachinePassChange::Operands};
    return Value;
  }();
  return Contract;
}

[[nodiscard]] const MachinePassContract &phaseContract(MachinePseudoExpansionPhase Phase) {
  static const MachinePassContract PreRA = [] {
    MachinePassContract Value{
        .Name = "pre-ra-target-pseudo-expansion",
        .Requires = {MachineFunctionProperty::Selected, MachineFunctionProperty::NoPHIs,
                     MachineFunctionProperty::TiedOpsRewritten},
        .Sets = {},
        .Clears = {},
        .RequiredAnalyses = {},
        .VerificationStage = PipelineStage::TiedOpsRewritten,
    };
    Value.ThreadSafety = PassThreadSafety::ThreadSafe;
    Value.DeclaredChanges = {MachinePassChange::Instructions, MachinePassChange::Operands,
                             MachinePassChange::Registers};
    return Value;
  }();
  static const MachinePassContract PrePEI = [] {
    MachinePassContract Value{
        .Name = "pre-pei-target-pseudo-expansion",
        .Requires = {MachineFunctionProperty::Selected, MachineFunctionProperty::NoPHIs,
                     MachineFunctionProperty::NoVRegs, MachineFunctionProperty::TracksLiveness},
        .Sets = {},
        .Clears = {},
        .RequiredAnalyses = {},
        .VerificationStage = PipelineStage::RegisterAllocated,
    };
    Value.ThreadSafety = PassThreadSafety::ThreadSafe;
    Value.DeclaredChanges = {MachinePassChange::Instructions, MachinePassChange::Operands,
                             MachinePassChange::Frame};
    return Value;
  }();
  static const MachinePassContract PostRA = [] {
    MachinePassContract Value{
        .Name = "postrapseudos",
        .Requires = {MachineFunctionProperty::Selected, MachineFunctionProperty::NoPHIs,
                     MachineFunctionProperty::NoVRegs, MachineFunctionProperty::TracksLiveness,
                     MachineFunctionProperty::FrameFinalized,
                     MachineFunctionProperty::NoFrameIndices},
        .Sets = {},
        .Clears = {},
        .RequiredAnalyses = {},
        .VerificationStage = PipelineStage::PostRAFinalized,
    };
    Value.ThreadSafety = PassThreadSafety::ThreadSafe;
    Value.DeclaredChanges = {MachinePassChange::Instructions, MachinePassChange::Operands};
    return Value;
  }();
  static const MachinePassContract TargetLate = [] {
    MachinePassContract Value{
        .Name = "target-late-pseudo-expansion",
        .Requires = {MachineFunctionProperty::Selected, MachineFunctionProperty::NoPHIs,
                     MachineFunctionProperty::NoVRegs, MachineFunctionProperty::TracksLiveness,
                     MachineFunctionProperty::FrameFinalized,
                     MachineFunctionProperty::NoFrameIndices},
        .Sets = {},
        .Clears = {},
        .RequiredAnalyses = {},
        .VerificationStage = PipelineStage::PostRAFinalized,
    };
    Value.ThreadSafety = PassThreadSafety::ThreadSafe;
    Value.DeclaredChanges = {MachinePassChange::Instructions, MachinePassChange::Operands};
    return Value;
  }();
  switch (Phase) {
  case MachinePseudoExpansionPhase::PreRA:
    return PreRA;
  case MachinePseudoExpansionPhase::PrePEI:
    return PrePEI;
  case MachinePseudoExpansionPhase::PostRA:
    return PostRA;
  case MachinePseudoExpansionPhase::TargetLate:
    return TargetLate;
  }
  return TargetLate;
}

[[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]] Expected<MachinePassResult>
prepareImplementation(MachineFunction &Function, const TargetDescription &Target,
                      const TargetPseudoExpansionInfo &Info, MachineActionReplay &Actions) {
  bool Changed = false;
  for (MachineInstr *Instruction : layoutInstructions(Function)) {
    const InstructionDescriptor *Descriptor = Target.instructionDescriptor(Instruction->opcode());
    if (Descriptor == nullptr)
      return makeUnexpected(ErrorCode::VerificationError,
                            "pseudo preparation encountered an unknown opcode");
    if (Descriptor->Owner != PseudoOwner::PostRAExpansion)
      continue;
    std::string Rule = "prepare-pseudo.";
    Rule.append(Descriptor->Name);
    auto Accepted = Actions.request(Rule, Function, Instruction->stableID());
    if (!Accepted)
      return std::unexpected(Accepted.error());
    if (!*Accepted) {
      return makeUnexpected(ErrorCode::PseudoExpansionFailed,
                            "required pseudo preparation action was rejected");
    }
    auto Prepared = Info.preparePostRAPseudo(*Instruction, Function, Target);
    if (!Prepared)
      return std::unexpected(Prepared.error());
    Changed |= *Prepared;
  }
  MachinePassResult Result;
  Result.Changed = Changed;
  if (Changed) {
    Result.Changes = {MachinePassChange::Operands};
    Result.Preserved = PreservedAnalyses::none();
  } else {
    Result.Preserved = PreservedAnalyses::all();
  }
  return Result;
}

[[nodiscard]] Expected<MachinePassResult>
expandImplementation(MachineFunction &Function, const TargetDescription &Target,
                     const TargetPseudoExpansionInfo &Info, MachinePseudoExpansionPhase Phase,
                     const TargetInstrInfo *BranchInfo, MachineActionReplay &Actions) {
  const ResourceSnapshot Before = snapshotResources(Function);
  std::optional<BlockSizeSnapshot> SizesBefore;
  if (Phase == MachinePseudoExpansionPhase::TargetLate) {
    if (BranchInfo == nullptr) {
      return makeUnexpected(ErrorCode::InvalidArgument,
                            "target-late pseudo expansion requires branch sizing");
    }
    auto Snapshot = snapshotBlockSizes(Function, Target, *BranchInfo);
    if (!Snapshot)
      return std::unexpected(Snapshot.error());
    SizesBefore = std::move(*Snapshot);
  }
  std::vector<MachineInstr *> Worklist;
  Worklist = layoutInstructions(Function);
  bool Changed = false;
  for (MachineInstr *Instruction : Worklist) {
    if (Instruction->parent() == nullptr)
      continue;
    const InstructionDescriptor *Descriptor = Target.instructionDescriptor(Instruction->opcode());
    if (Descriptor == nullptr)
      return makeUnexpected(ErrorCode::VerificationError,
                            "pseudo expansion encountered an unknown opcode");
    if (!Descriptor->Flags.has(InstructionFlag::Pseudo) || Descriptor->Owner != ownerFor(Phase)) {
      continue;
    }
    std::string Rule = "expand-pseudo.";
    Rule.append(Descriptor->Name);
    auto Accepted = Actions.request(Rule, Function, Instruction->stableID());
    if (!Accepted)
      return std::unexpected(Accepted.error());
    if (!*Accepted) {
      return makeUnexpected(ErrorCode::PseudoExpansionFailed,
                            "required pseudo expansion action was rejected");
    }
    MachineBasicBlock *ExpansionBlock = Instruction->parent();
    std::optional<std::uint64_t> ExpansionSizeBefore;
    if (Phase == MachinePseudoExpansionPhase::TargetLate) {
      auto Size = blockSize(*ExpansionBlock, Function, Target, *BranchInfo);
      if (!Size)
        return std::unexpected(Size.error());
      ExpansionSizeBefore = *Size;
    }
    auto Expanded = Info.expandPseudo(*Instruction, Phase, Function, Target);
    if (!Expanded)
      return std::unexpected(Expanded.error());
    if (!*Expanded) {
      return makeUnexpected(ErrorCode::PseudoExpansionFailed,
                            "target has no expansion for an owned executable pseudo");
    }
    if (ExpansionSizeBefore.has_value()) {
      auto SizeAfter = blockSize(*ExpansionBlock, Function, Target, *BranchInfo);
      if (!SizeAfter)
        return std::unexpected(SizeAfter.error());
      if (*SizeAfter > *ExpansionSizeBefore) {
        return makeUnexpected(ErrorCode::PseudoExpansionFailed,
                              "one target-late pseudo expansion increased its encoded block size");
      }
    }
    Changed = true;
  }

  auto Resources = verifyResourceContract(Before, Function, Phase);
  if (!Resources)
    return std::unexpected(Resources.error());
  if (SizesBefore.has_value()) {
    auto SizesAfter = snapshotBlockSizes(Function, Target, *BranchInfo);
    if (!SizesAfter)
      return std::unexpected(SizesAfter.error());
    if (SizesAfter->size() != SizesBefore->size()) {
      return makeUnexpected(ErrorCode::PseudoExpansionFailed,
                            "target-late pseudo expansion changed block layout");
    }
    for (std::size_t Index = 0; Index < SizesAfter->size(); ++Index) {
      if ((*SizesAfter)[Index].first != (*SizesBefore)[Index].first ||
          (*SizesAfter)[Index].second > (*SizesBefore)[Index].second) {
        return makeUnexpected(ErrorCode::PseudoExpansionFailed,
                              "target-late pseudo expansion increased an encoded block size");
      }
    }
  }
  for (MachineInstr *Instruction : layoutInstructions(Function)) {
    const InstructionDescriptor *Descriptor = Target.instructionDescriptor(Instruction->opcode());
    if (Descriptor != nullptr && Descriptor->Flags.has(InstructionFlag::Pseudo) &&
        Descriptor->Owner == ownerFor(Phase)) {
      return makeUnexpected(ErrorCode::PseudoExpansionFailed,
                            "pseudo expansion left an instruction owned by the completed phase");
    }
  }
  if (Changed && Function.properties().has(MachineFunctionProperty::TracksLiveness)) {
    auto Rebuilt = rebuildMachineLiveness(Function, Target);
    if (!Rebuilt)
      return std::unexpected(Rebuilt.error());
  }
  MachinePassResult Result;
  Result.Changed = Changed;
  if (Changed) {
    Result.Changes = {MachinePassChange::Instructions, MachinePassChange::Operands};
    Result.Preserved = PreservedAnalyses::none();
  } else {
    Result.Preserved = PreservedAnalyses::all();
  }
  return Result;
}

[[nodiscard]] Expected<MachinePassResult>
runPreparationTransaction(MachineFunction &Function, const TargetDescription &Target,
                          const TargetPseudoExpansionInfo &Info,
                          MachineFunctionAnalysisManager &Analyses) {
  auto Trial = cloneMachineFunction(
      Function, Target,
      MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
  if (!Trial)
    return std::unexpected(withErrorContext(Trial.error(), "pseudo preparation setup failed: "));
  MachineActionReplay Actions(Analyses, Trial->InstructionStableIDs);
  auto TrialResult = prepareImplementation(*Trial->ClonedFunction, Target, Info, Actions);
  if (!TrialResult)
    return std::unexpected(TrialResult.error());
  Actions.beginReplay();
  auto Result = prepareImplementation(Function, Target, Info, Actions);
  if (!Result) {
    return std::unexpected(withErrorContext(
        Result.error(), "pseudo preparation diverged after successful preflight: "));
  }
  auto Replayed = Actions.finishReplay();
  if (!Replayed)
    return std::unexpected(Replayed.error());
  return Result;
}

[[nodiscard]] Expected<MachinePassResult>
runExpansionTransaction(MachineFunction &Function, const TargetDescription &Target,
                        const TargetPseudoExpansionInfo &Info, MachinePseudoExpansionPhase Phase,
                        const TargetInstrInfo *BranchInfo,
                        MachineFunctionAnalysisManager &Analyses) {
  auto Trial = cloneMachineFunction(
      Function, Target,
      MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
  if (!Trial)
    return std::unexpected(withErrorContext(Trial.error(), "pseudo expansion setup failed: "));
  MachineActionReplay Actions(Analyses, Trial->InstructionStableIDs);
  auto TrialResult =
      expandImplementation(*Trial->ClonedFunction, Target, Info, Phase, BranchInfo, Actions);
  if (!TrialResult)
    return std::unexpected(TrialResult.error());
  Actions.beginReplay();
  auto Result = expandImplementation(Function, Target, Info, Phase, BranchInfo, Actions);
  if (!Result) {
    return std::unexpected(
        withErrorContext(Result.error(), "pseudo expansion diverged after successful preflight: "));
  }
  auto Replayed = Actions.finishReplay();
  if (!Replayed)
    return std::unexpected(Replayed.error());
  return Result;
}

} // namespace

const MachinePassContract &PreRATargetPseudoExpansionPass::contract() const noexcept {
  return phaseContract(MachinePseudoExpansionPhase::PreRA);
}

Expected<MachinePassResult>
PreRATargetPseudoExpansionPass::run(MachineFunction &Function, const TargetDescription &Target,
                                    MachineFunctionAnalysisManager &Analyses) {
  if (!Function.properties().containsAll(contract().Requires)) {
    return makeUnexpected(ErrorCode::VerificationError,
                          "pre-RA pseudo expansion requires selected SSA-exited MIR");
  }
  return runExpansionTransaction(Function, Target, *Info, MachinePseudoExpansionPhase::PreRA,
                                 nullptr, Analyses);
}

const MachinePassContract &PreparePostRAPseudosPass::contract() const noexcept {
  return preparationContract();
}

Expected<MachinePassResult>
PreparePostRAPseudosPass::run(MachineFunction &Function, const TargetDescription &Target,
                              MachineFunctionAnalysisManager &Analyses) {
  if (!Function.properties().containsAll(contract().Requires))
    return makeUnexpected(ErrorCode::VerificationError,
                          "post-RA pseudo preparation requires selected MachineIR");
  return runPreparationTransaction(Function, Target, *Info, Analyses);
}

const MachinePassContract &PrePEITargetPseudoExpansionPass::contract() const noexcept {
  return phaseContract(MachinePseudoExpansionPhase::PrePEI);
}

Expected<MachinePassResult>
PrePEITargetPseudoExpansionPass::run(MachineFunction &Function, const TargetDescription &Target,
                                     MachineFunctionAnalysisManager &Analyses) {
  if (!Function.properties().containsAll(contract().Requires))
    return makeUnexpected(ErrorCode::VerificationError,
                          "pre-PEI pseudo expansion requires allocated tracked-liveness MIR");
  return runExpansionTransaction(Function, Target, *Info, MachinePseudoExpansionPhase::PrePEI,
                                 nullptr, Analyses);
}

const MachinePassContract &ExpandPostRAPseudosPass::contract() const noexcept {
  return phaseContract(MachinePseudoExpansionPhase::PostRA);
}

Expected<MachinePassResult> ExpandPostRAPseudosPass::run(MachineFunction &Function,
                                                         const TargetDescription &Target,
                                                         MachineFunctionAnalysisManager &Analyses) {
  if (!Function.properties().containsAll(contract().Requires))
    return makeUnexpected(ErrorCode::VerificationError,
                          "post-RA pseudo expansion requires finalized frame and liveness");
  return runExpansionTransaction(Function, Target, *Info, MachinePseudoExpansionPhase::PostRA,
                                 nullptr, Analyses);
}

const MachinePassContract &TargetLatePseudoExpansionPass::contract() const noexcept {
  return phaseContract(MachinePseudoExpansionPhase::TargetLate);
}

Expected<MachinePassResult>
TargetLatePseudoExpansionPass::run(MachineFunction &Function, const TargetDescription &Target,
                                   MachineFunctionAnalysisManager &Analyses) {
  if (!Function.properties().containsAll(contract().Requires))
    return makeUnexpected(ErrorCode::VerificationError,
                          "target-late pseudo expansion requires finalized allocated MIR");
  return runExpansionTransaction(Function, Target, *Info, MachinePseudoExpansionPhase::TargetLate,
                                 BranchInfo, Analyses);
}

} // namespace rcc