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

#include "rcc/CodeGen/FrameIndexElimination.h"

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

#include <cstddef>

namespace rcc {

Expected<bool> eliminateFrameIndices(MachineFunction &Function, const TargetDescription &Target,
                                     const TargetFrameLowering &FrameLowering,
                                     RegisterScavenger &Scavenger) {
  bool Changed = false;
  for (MachineBasicBlock *Block = Function.firstBlock(); Block != nullptr; Block = Block->next()) {
    for (MachineInstr *Instruction = Block->firstInstruction(); Instruction != nullptr;) {
      MachineInstr *Next = Instruction->next();
      for (std::size_t Index = 0; Index < Instruction->operandCount(); ++Index) {
        if (Instruction->operandAt(Index)->kind() != MachineOperand::Kind::FrameIndex)
          continue;
        auto Eliminated =
            FrameLowering.eliminateFrameIndex(*Instruction, Index, Function, Target, Scavenger);
        if (!Eliminated)
          return std::unexpected(Eliminated.error());
        Changed = true;
      }

      Instruction = Next;
    }
  }
  return Changed;
}

} // namespace rcc