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

#include "rcc/CodeGen/PrologueEpilogueInsertion.h"

#include "rcc/CodeGen/FrameIndexElimination.h"
#include "rcc/CodeGen/MachineBasicBlock.h"
#include "rcc/CodeGen/MachineFunction.h"
#include "rcc/CodeGen/MachineInstr.h"
#include "rcc/CodeGen/MachineMemOperand.h"
#include "rcc/CodeGen/MachineOperand.h"
#include "rcc/CodeGen/RegisterScavenger.h"
#include "rcc/CodeGen/TargetFrameLowering.h"

#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");
  return runImplementation(Function, Target, Options);
}

Expected<MachinePassResult>
PrologueEpilogueInsertionPass::runImplementation(MachineFunction &Function,
                                                 const TargetDescription &Target,
                                                 FrameLoweringOptions RunOptions) const {
  auto Prepared = Info->prepareFrame(Function, Target, RunOptions);
  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());

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

} // namespace rcc