aboutsummaryrefslogtreecommitdiff
path: root/backend/lib/CodeGen/PrologueEpilogueInsertion.cpp
blob: 99f3b20a33c32242179c464075a94190a1158c1e (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
// SPDX-License-Identifier: GPL-3.0-or-later

#include "rcc/CodeGen/PrologueEpilogueInsertion.h"

#include "rcc/CodeGen/FrameIndexElimination.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/MachineMemOperand.h"
#include "rcc/CodeGen/MachineOperand.h"
#include "rcc/CodeGen/MachineVerifier.h"
#include "rcc/CodeGen/RegisterScavenger.h"
#include "rcc/CodeGen/TargetFrameLowering.h"

#include <string>
#include <utility>
#include <vector>

namespace rcc {
namespace {} // namespace

const MachinePassContract &PrologueEpilogueInsertionPass::contract() const noexcept {
  static const MachinePassContract Contract = [] {
    MachinePassContract Value{
        .Name = "prologepilog",
        .Requires = {MachineFunctionProperty::NoPHIs, MachineFunctionProperty::NoVRegs},
        .Sets = {MachineFunctionProperty::FrameFinalized, MachineFunctionProperty::NoFrameIndices},
        .Clears = {},
        .RequiredAnalyses = {},
        .VerificationStage = PipelineStage::FrameFinalized,
    };
    Value.ThreadSafety = PassThreadSafety::ThreadSafe;
    Value.DeclaredChanges = {MachinePassChange::Instructions, MachinePassChange::Operands,
                             MachinePassChange::Registers, MachinePassChange::Frame};
    return Value;
  }();
  return Contract;
}

Expected<MachinePassResult> PrologueEpilogueInsertionPass::run(MachineFunction &Function,
                                                               const TargetDescription &Target,
                                                               MachineFunctionAnalysisManager &) {
  if (Info == nullptr)
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "PrologueEpilogueInsertion requires target frame lowering");
  const MachineFunctionProperties Missing = contract().Requires.missingFrom(Function.properties());
  if (!Missing.empty()) {
    return makeUnexpected(ErrorCode::VerificationError,
                          "PrologueEpilogueInsertion requires properties: " + Missing.toString());
  }

  auto Trial = cloneMachineFunction(
      Function, Target,
      MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
  if (!Trial)
    return std::unexpected(withErrorContext(Trial.error(), "frame transaction setup failed: "));
  auto TrialRun = runImplementation(*Trial->ClonedFunction, Target);
  if (!TrialRun)
    return std::unexpected(
        withErrorContext(TrialRun.error(), "frame transaction rejected input: "));
  auto Transition = Trial->ClonedFunction->properties().applyTransition(
      contract().Requires, contract().Sets, contract().Clears);
  if (!Transition) {
    return std::unexpected(
        withErrorContext(Transition.error(), "frame transaction property update failed: "));
  }
  auto Verified =
      MachineVerifier::verify(*Trial->ClonedFunction, Target,
                              MachineVerifierOptions{.Mode = MachineVerifierMode::Generic,
                                                     .Stage = contract().VerificationStage,
                                                     .VerifyProperties = true});
  if (!Verified) {
    return std::unexpected(
        withErrorContext(Verified.error(), "frame transaction postcondition failed: "));
  }

  auto Result = runImplementation(Function, Target);
  if (!Result) {
    return std::unexpected(withErrorContext(
        Result.error(), "frame lowering diverged after successful transaction preflight: "));
  }
  return Result;
}

Expected<MachinePassResult>
PrologueEpilogueInsertionPass::runImplementation(MachineFunction &Function,
                                                 const TargetDescription &Target) const {
  auto Prepared = Info->prepareFrame(Function, Target);
  if (!Prepared)
    return std::unexpected(Prepared.error());

  RegisterScavenger Scavenger(Function, Target);
  std::vector<MachineInstr *> CallFramePseudos;
  std::vector<std::pair<MachineBasicBlock *, MachineInstr *>> Epilogues;
  for (MachineBasicBlock *Block = Function.firstBlock(); Block != nullptr; Block = Block->next()) {
    for (MachineInstr *Instruction = Block->firstInstruction(); Instruction != nullptr;
         Instruction = Instruction->next()) {
      if (Info->isCallFramePseudo(*Instruction))
        CallFramePseudos.push_back(Instruction);
      if (Info->isEpilogueTerminator(*Instruction, Target))
        Epilogues.emplace_back(Block, Instruction);
    }
  }
  for (MachineInstr *Pseudo : CallFramePseudos) {
    auto Eliminated = Info->eliminateCallFramePseudo(*Pseudo, Function, Target, Scavenger);
    if (!Eliminated)
      return std::unexpected(Eliminated.error());
  }

  auto CalleeSaved = Info->insertCalleeSavedSpillsAndRestores(Function, Target);
  if (!CalleeSaved)
    return std::unexpected(CalleeSaved.error());

  auto PreFinalized = Info->processFunctionBeforeFrameFinalized(Function, Target);
  if (!PreFinalized)
    return std::unexpected(PreFinalized.error());

  auto Finalized = Info->finalizeFrame(Function, Target);
  if (!Finalized)
    return std::unexpected(Finalized.error());
  if (!Function.frameInfo().isFrozen()) {
    return makeUnexpected(ErrorCode::InternalError,
                          "target frame lowering did not freeze the frame layout");
  }

  auto Prologue = Info->emitPrologue(Function, Target, Scavenger);
  if (!Prologue)
    return std::unexpected(Prologue.error());
  for (const auto &[Block, Terminator] : Epilogues) {
    auto Epilogue = Info->emitEpilogue(*Block, *Terminator, Function, Target, Scavenger);
    if (!Epilogue)
      return std::unexpected(Epilogue.error());
  }

  auto PreFrameIndices = Info->processFunctionBeforeFrameIndexElimination(Function, Target);
  if (!PreFrameIndices)
    return std::unexpected(PreFrameIndices.error());

  auto FrameIndices = eliminateFrameIndices(Function, Target, *Info, Scavenger);
  if (!FrameIndices)
    return std::unexpected(FrameIndices.error());

  if (Function.properties().has(MachineFunctionProperty::TracksLiveness)) {
    auto Rebuilt = rebuildMachineLiveness(Function, Target);
    if (!Rebuilt)
      return std::unexpected(Rebuilt.error());
  }

  MachinePassResult Result;
  Result.Changed = true;
  Result.Changes = {MachinePassChange::Instructions, MachinePassChange::Operands,
                    MachinePassChange::Registers, MachinePassChange::Frame};
  Result.Preserved = PreservedAnalyses::none();
  return Result;
}

} // namespace rcc