| 1 | //===-- XCoreFrameToArgsOffsetElim.cpp ----------------------------*- C++ -*-=// |
|---|---|
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Replace Pseudo FRAME_TO_ARGS_OFFSET with the appropriate real offset. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "XCore.h" |
| 14 | #include "XCoreInstrInfo.h" |
| 15 | #include "XCoreSubtarget.h" |
| 16 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 17 | #include "llvm/CodeGen/MachineFunctionAnalysisManager.h" |
| 18 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 19 | #include "llvm/CodeGen/MachinePassManager.h" |
| 20 | #include "llvm/IR/Analysis.h" |
| 21 | #include "llvm/Pass.h" |
| 22 | #include "llvm/Target/TargetMachine.h" |
| 23 | using namespace llvm; |
| 24 | |
| 25 | namespace { |
| 26 | struct XCoreFrameToArgsOffsetEliminationLegacy : public MachineFunctionPass { |
| 27 | static char ID; |
| 28 | XCoreFrameToArgsOffsetEliminationLegacy() : MachineFunctionPass(ID) {} |
| 29 | |
| 30 | bool runOnMachineFunction(MachineFunction &Fn) override; |
| 31 | MachineFunctionProperties getRequiredProperties() const override { |
| 32 | return MachineFunctionProperties().setNoVRegs(); |
| 33 | } |
| 34 | |
| 35 | StringRef getPassName() const override { |
| 36 | return "XCore FRAME_TO_ARGS_OFFSET Elimination"; |
| 37 | } |
| 38 | |
| 39 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 40 | AU.setPreservesCFG(); |
| 41 | MachineFunctionPass::getAnalysisUsage(AU); |
| 42 | } |
| 43 | }; |
| 44 | char XCoreFrameToArgsOffsetEliminationLegacy::ID = 0; |
| 45 | } |
| 46 | |
| 47 | /// createXCoreFrameToArgsOffsetEliminationPass - returns an instance of the |
| 48 | /// Frame to args offset elimination pass |
| 49 | FunctionPass *llvm::createXCoreFrameToArgsOffsetEliminationLegacyPass() { |
| 50 | return new XCoreFrameToArgsOffsetEliminationLegacy(); |
| 51 | } |
| 52 | |
| 53 | static bool runFrameToArgOffsetElimination(MachineFunction &MF) { |
| 54 | const XCoreInstrInfo &TII = |
| 55 | *static_cast<const XCoreInstrInfo *>(MF.getSubtarget().getInstrInfo()); |
| 56 | unsigned StackSize = MF.getFrameInfo().getStackSize(); |
| 57 | for (MachineBasicBlock &MBB : MF) { |
| 58 | for (MachineBasicBlock::iterator MBBI = MBB.begin(), EE = MBB.end(); |
| 59 | MBBI != EE; ++MBBI) { |
| 60 | if (MBBI->getOpcode() == XCore::FRAME_TO_ARGS_OFFSET) { |
| 61 | MachineInstr &OldInst = *MBBI; |
| 62 | Register Reg = OldInst.getOperand(i: 0).getReg(); |
| 63 | MBBI = TII.loadImmediate(MBB, MI: MBBI, Reg, Value: StackSize); |
| 64 | OldInst.eraseFromParent(); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | bool XCoreFrameToArgsOffsetEliminationLegacy::runOnMachineFunction( |
| 72 | MachineFunction &MF) { |
| 73 | return runFrameToArgOffsetElimination(MF); |
| 74 | } |
| 75 | |
| 76 | PreservedAnalyses XCoreFrameToArgsOffsetEliminationPass::run( |
| 77 | MachineFunction &MF, MachineFunctionAnalysisManager &MFAM) { |
| 78 | return runFrameToArgOffsetElimination(MF) |
| 79 | ? getMachineFunctionPassPreservedAnalyses() |
| 80 | .preserveSet<CFGAnalyses>() |
| 81 | : PreservedAnalyses::all(); |
| 82 | } |
| 83 |