XCore target: Lower FRAME_TO_ARGS_OFFSET
[oota-llvm.git] / lib / Target / XCore / XCoreFrameToArgsOffsetElim.cpp
1 //===-- XCoreFrameToArgsOffsetElim.cpp ----------------------------*- C++ -*-=//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Replace Pseudo FRAME_TO_ARGS_OFFSET with the appropriate real offset.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "XCore.h"
15 #include "XCoreInstrInfo.h"
16 #include "llvm/CodeGen/MachineFrameInfo.h"
17 #include "llvm/CodeGen/MachineFunctionPass.h"
18 #include "llvm/CodeGen/MachineInstrBuilder.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/Target/TargetMachine.h"
22 using namespace llvm;
23
24 namespace {
25   struct XCoreFTAOElim : public MachineFunctionPass {
26     static char ID;
27     XCoreFTAOElim() : MachineFunctionPass(ID) {}
28
29     virtual bool runOnMachineFunction(MachineFunction &Fn);
30
31     virtual const char *getPassName() const {
32       return "XCore FRAME_TO_ARGS_OFFSET Elimination";
33     }
34   };
35   char XCoreFTAOElim::ID = 0;
36 }
37
38 /// createXCoreFrameToArgsOffsetEliminationPass - returns an instance of the
39 /// Frame to args offset elimination pass
40 FunctionPass *llvm::createXCoreFrameToArgsOffsetEliminationPass() {
41   return new XCoreFTAOElim();
42 }
43
44 static inline bool isImmU6(unsigned val) {
45   return val < (1 << 6);
46 }
47
48 static inline bool isImmU16(unsigned val) {
49   return val < (1 << 16);
50 }
51
52 bool XCoreFTAOElim::runOnMachineFunction(MachineFunction &MF) {
53   const XCoreInstrInfo &TII =
54           *static_cast<const XCoreInstrInfo*>(MF.getTarget().getInstrInfo());
55   unsigned StackSize = MF.getFrameInfo()->getStackSize();
56   for (MachineFunction::iterator MFI = MF.begin(), E = MF.end(); MFI != E;
57        ++MFI) {
58     MachineBasicBlock &MBB = *MFI;
59     for (MachineBasicBlock::iterator MBBI = MBB.begin(), EE = MBB.end();
60          MBBI != EE; ++MBBI) {
61       if (MBBI->getOpcode() == XCore::FRAME_TO_ARGS_OFFSET) {
62         MachineInstr *OldInst = MBBI;
63         unsigned Reg = OldInst->getOperand(0).getReg();
64         MBBI = TII.loadImmediate(MBB, MBBI, Reg, StackSize);
65         OldInst->eraseFromParent();
66       }
67     }
68   }
69   return true;
70 }