LLVM  3.7.0
MSP430FrameLowering.cpp
Go to the documentation of this file.
1 //===-- MSP430FrameLowering.cpp - MSP430 Frame Information ----------------===//
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 // This file contains the MSP430 implementation of TargetFrameLowering class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "MSP430FrameLowering.h"
15 #include "MSP430InstrInfo.h"
17 #include "MSP430Subtarget.h"
23 #include "llvm/IR/DataLayout.h"
24 #include "llvm/IR/Function.h"
27 
28 using namespace llvm;
29 
31  const MachineFrameInfo *MFI = MF.getFrameInfo();
32 
33  return (MF.getTarget().Options.DisableFramePointerElim(MF) ||
35  MFI->isFrameAddressTaken());
36 }
37 
39  return !MF.getFrameInfo()->hasVarSizedObjects();
40 }
41 
43  MachineBasicBlock &MBB) const {
44  assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
45  MachineFrameInfo *MFI = MF.getFrameInfo();
47  const MSP430InstrInfo &TII =
48  *static_cast<const MSP430InstrInfo *>(MF.getSubtarget().getInstrInfo());
49 
51  DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
52 
53  // Get the number of bytes to allocate from the FrameInfo.
54  uint64_t StackSize = MFI->getStackSize();
55 
56  uint64_t NumBytes = 0;
57  if (hasFP(MF)) {
58  // Calculate required stack adjustment
59  uint64_t FrameSize = StackSize - 2;
60  NumBytes = FrameSize - MSP430FI->getCalleeSavedFrameSize();
61 
62  // Get the offset of the stack slot for the EBP register... which is
63  // guaranteed to be the last slot by processFunctionBeforeFrameFinalized.
64  // Update the frame offset adjustment.
65  MFI->setOffsetAdjustment(-NumBytes);
66 
67  // Save FP into the appropriate stack slot...
68  BuildMI(MBB, MBBI, DL, TII.get(MSP430::PUSH16r))
69  .addReg(MSP430::FP, RegState::Kill);
70 
71  // Update FP with the new base value...
72  BuildMI(MBB, MBBI, DL, TII.get(MSP430::MOV16rr), MSP430::FP)
73  .addReg(MSP430::SP);
74 
75  // Mark the FramePtr as live-in in every block except the entry.
76  for (MachineFunction::iterator I = std::next(MF.begin()), E = MF.end();
77  I != E; ++I)
78  I->addLiveIn(MSP430::FP);
79 
80  } else
81  NumBytes = StackSize - MSP430FI->getCalleeSavedFrameSize();
82 
83  // Skip the callee-saved push instructions.
84  while (MBBI != MBB.end() && (MBBI->getOpcode() == MSP430::PUSH16r))
85  ++MBBI;
86 
87  if (MBBI != MBB.end())
88  DL = MBBI->getDebugLoc();
89 
90  if (NumBytes) { // adjust stack pointer: SP -= numbytes
91  // If there is an SUB16ri of SP immediately before this instruction, merge
92  // the two.
93  //NumBytes -= mergeSPUpdates(MBB, MBBI, true);
94  // If there is an ADD16ri or SUB16ri of SP immediately after this
95  // instruction, merge the two instructions.
96  // mergeSPUpdatesDown(MBB, MBBI, &NumBytes);
97 
98  if (NumBytes) {
99  MachineInstr *MI =
100  BuildMI(MBB, MBBI, DL, TII.get(MSP430::SUB16ri), MSP430::SP)
101  .addReg(MSP430::SP).addImm(NumBytes);
102  // The SRW implicit def is dead.
103  MI->getOperand(3).setIsDead();
104  }
105  }
106 }
107 
109  MachineBasicBlock &MBB) const {
110  const MachineFrameInfo *MFI = MF.getFrameInfo();
112  const MSP430InstrInfo &TII =
113  *static_cast<const MSP430InstrInfo *>(MF.getSubtarget().getInstrInfo());
114 
116  unsigned RetOpcode = MBBI->getOpcode();
117  DebugLoc DL = MBBI->getDebugLoc();
118 
119  switch (RetOpcode) {
120  case MSP430::RET:
121  case MSP430::RETI: break; // These are ok
122  default:
123  llvm_unreachable("Can only insert epilog into returning blocks");
124  }
125 
126  // Get the number of bytes to allocate from the FrameInfo
127  uint64_t StackSize = MFI->getStackSize();
128  unsigned CSSize = MSP430FI->getCalleeSavedFrameSize();
129  uint64_t NumBytes = 0;
130 
131  if (hasFP(MF)) {
132  // Calculate required stack adjustment
133  uint64_t FrameSize = StackSize - 2;
134  NumBytes = FrameSize - CSSize;
135 
136  // pop FP.
137  BuildMI(MBB, MBBI, DL, TII.get(MSP430::POP16r), MSP430::FP);
138  } else
139  NumBytes = StackSize - CSSize;
140 
141  // Skip the callee-saved pop instructions.
142  while (MBBI != MBB.begin()) {
143  MachineBasicBlock::iterator PI = std::prev(MBBI);
144  unsigned Opc = PI->getOpcode();
145  if (Opc != MSP430::POP16r && !PI->isTerminator())
146  break;
147  --MBBI;
148  }
149 
150  DL = MBBI->getDebugLoc();
151 
152  // If there is an ADD16ri or SUB16ri of SP immediately before this
153  // instruction, merge the two instructions.
154  //if (NumBytes || MFI->hasVarSizedObjects())
155  // mergeSPUpdatesUp(MBB, MBBI, StackPtr, &NumBytes);
156 
157  if (MFI->hasVarSizedObjects()) {
158  BuildMI(MBB, MBBI, DL,
159  TII.get(MSP430::MOV16rr), MSP430::SP).addReg(MSP430::FP);
160  if (CSSize) {
161  MachineInstr *MI =
162  BuildMI(MBB, MBBI, DL,
163  TII.get(MSP430::SUB16ri), MSP430::SP)
164  .addReg(MSP430::SP).addImm(CSSize);
165  // The SRW implicit def is dead.
166  MI->getOperand(3).setIsDead();
167  }
168  } else {
169  // adjust stack pointer back: SP += numbytes
170  if (NumBytes) {
171  MachineInstr *MI =
172  BuildMI(MBB, MBBI, DL, TII.get(MSP430::ADD16ri), MSP430::SP)
173  .addReg(MSP430::SP).addImm(NumBytes);
174  // The SRW implicit def is dead.
175  MI->getOperand(3).setIsDead();
176  }
177  }
178 }
179 
180 // FIXME: Can we eleminate these in favour of generic code?
181 bool
184  const std::vector<CalleeSavedInfo> &CSI,
185  const TargetRegisterInfo *TRI) const {
186  if (CSI.empty())
187  return false;
188 
189  DebugLoc DL;
190  if (MI != MBB.end()) DL = MI->getDebugLoc();
191 
192  MachineFunction &MF = *MBB.getParent();
193  const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
195  MFI->setCalleeSavedFrameSize(CSI.size() * 2);
196 
197  for (unsigned i = CSI.size(); i != 0; --i) {
198  unsigned Reg = CSI[i-1].getReg();
199  // Add the callee-saved register as live-in. It's killed at the spill.
200  MBB.addLiveIn(Reg);
201  BuildMI(MBB, MI, DL, TII.get(MSP430::PUSH16r))
202  .addReg(Reg, RegState::Kill);
203  }
204  return true;
205 }
206 
207 bool
210  const std::vector<CalleeSavedInfo> &CSI,
211  const TargetRegisterInfo *TRI) const {
212  if (CSI.empty())
213  return false;
214 
215  DebugLoc DL;
216  if (MI != MBB.end()) DL = MI->getDebugLoc();
217 
218  MachineFunction &MF = *MBB.getParent();
219  const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
220 
221  for (unsigned i = 0, e = CSI.size(); i != e; ++i)
222  BuildMI(MBB, MI, DL, TII.get(MSP430::POP16r), CSI[i].getReg());
223 
224  return true;
225 }
226 
230  const MSP430InstrInfo &TII =
231  *static_cast<const MSP430InstrInfo *>(MF.getSubtarget().getInstrInfo());
232  unsigned StackAlign = getStackAlignment();
233 
234  if (!hasReservedCallFrame(MF)) {
235  // If the stack pointer can be changed after prologue, turn the
236  // adjcallstackup instruction into a 'sub SP, <amt>' and the
237  // adjcallstackdown instruction into 'add SP, <amt>'
238  // TODO: consider using push / pop instead of sub + store / add
239  MachineInstr *Old = I;
240  uint64_t Amount = Old->getOperand(0).getImm();
241  if (Amount != 0) {
242  // We need to keep the stack aligned properly. To do this, we round the
243  // amount of space needed for the outgoing arguments up to the next
244  // alignment boundary.
245  Amount = (Amount+StackAlign-1)/StackAlign*StackAlign;
246 
247  MachineInstr *New = nullptr;
248  if (Old->getOpcode() == TII.getCallFrameSetupOpcode()) {
249  New = BuildMI(MF, Old->getDebugLoc(),
250  TII.get(MSP430::SUB16ri), MSP430::SP)
251  .addReg(MSP430::SP).addImm(Amount);
252  } else {
253  assert(Old->getOpcode() == TII.getCallFrameDestroyOpcode());
254  // factor out the amount the callee already popped.
255  uint64_t CalleeAmt = Old->getOperand(1).getImm();
256  Amount -= CalleeAmt;
257  if (Amount)
258  New = BuildMI(MF, Old->getDebugLoc(),
259  TII.get(MSP430::ADD16ri), MSP430::SP)
260  .addReg(MSP430::SP).addImm(Amount);
261  }
262 
263  if (New) {
264  // The SRW implicit def is dead.
265  New->getOperand(3).setIsDead();
266 
267  // Replace the pseudo instruction with a new instruction...
268  MBB.insert(I, New);
269  }
270  }
271  } else if (I->getOpcode() == TII.getCallFrameDestroyOpcode()) {
272  // If we are performing frame pointer elimination and if the callee pops
273  // something off the stack pointer, add it back.
274  if (uint64_t CalleeAmt = I->getOperand(1).getImm()) {
275  MachineInstr *Old = I;
276  MachineInstr *New =
277  BuildMI(MF, Old->getDebugLoc(), TII.get(MSP430::SUB16ri),
278  MSP430::SP).addReg(MSP430::SP).addImm(CalleeAmt);
279  // The SRW implicit def is dead.
280  New->getOperand(3).setIsDead();
281 
282  MBB.insert(I, New);
283  }
284  }
285 
286  MBB.erase(I);
287 }
288 
289 void
291  RegScavenger *) const {
292  // Create a frame entry for the FP register that must be saved.
293  if (hasFP(MF)) {
294  int FrameIdx = MF.getFrameInfo()->CreateFixedObject(2, -4, true);
295  (void)FrameIdx;
296  assert(FrameIdx == MF.getFrameInfo()->getObjectIndexBegin() &&
297  "Slot for FP register must be last in order to be found!");
298  }
299 }
unsigned getStackAlignment() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
const MachineFunction * getParent() const
getParent - Return the MachineFunction containing this basic block.
MSP430MachineFunctionInfo - This class is derived from MachineFunction and contains private MSP430 ta...
void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override
void addLiveIn(unsigned Reg)
Adds the specified register as a live in.
A debug info location.
Definition: DebugLoc.h:34
void setIsDead(bool Val=true)
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
DILocation * get() const
Get the underlying DILocation.
Definition: DebugLoc.cpp:22
const HexagonInstrInfo * TII
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
bool DisableFramePointerElim(const MachineFunction &MF) const
DisableFramePointerElim - This returns true if frame pointer elimination optimization should be disab...
Reg
All possible values of the reg field in the ModR/M byte.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted...
bool isFrameAddressTaken() const
This method may be called any time after instruction selection is complete to determine if there is a...
const MachineInstrBuilder & addImm(int64_t Val) const
addImm - Add a new immediate operand.
const MachineBasicBlock & front() const
int getObjectIndexBegin() const
Return the minimum frame object index.
iterator getLastNonDebugInstr()
getLastNonDebugInstr - returns an iterator to the last non-debug instruction in the basic block...
int64_t getImm() const
instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:267
bool hasFP(const MachineFunction &MF) const override
hasFP - Return true if the specified function should have a dedicated frame pointer register...
TargetInstrInfo - Interface to description of machine instruction set.
bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, const std::vector< CalleeSavedInfo > &CSI, const TargetRegisterInfo *TRI) const override
restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee saved registers and returns...
bundle_iterator< MachineInstr, instr_iterator > iterator
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:273
bool spillCalleeSavedRegisters(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, const std::vector< CalleeSavedInfo > &CSI, const TargetRegisterInfo *TRI) const override
spillCalleeSavedRegisters - Issues instruction(s) to spill all callee saved registers and returns tru...
void processFunctionBeforeFrameFinalized(MachineFunction &MF, RegScavenger *RS=nullptr) const override
processFunctionBeforeFrameFinalized - This method is called immediately before the specified function...
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
int CreateFixedObject(uint64_t Size, int64_t SPOffset, bool Immutable, bool isAliased=false)
Create a new object at a fixed location on the stack.
MachineInstrBuilder BuildMI(MachineFunction &MF, DebugLoc DL, const MCInstrDesc &MCID)
BuildMI - Builder interface.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
bool hasReservedCallFrame(const MachineFunction &MF) const override
hasReservedCallFrame - Under normal circumstances, when a frame pointer is not required, we reserve argument space for call sites in the function immediately on entry to the current function.
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode...
Definition: MCInstrInfo.h:45
void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override
emitProlog/emitEpilog - These methods insert prolog and epilog code into the function.
MachineFrameInfo * getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Definition: MachineInstr.h:238
Representation of each machine instruction.
Definition: MachineInstr.h:51
#define I(x, y, z)
Definition: MD5.cpp:54
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
void eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator I) const override
eliminateCallFramePseudoInstr - This method is called during prolog/epilog code insertion to eliminat...
virtual const TargetInstrInfo * getInstrInfo() const
BasicBlockListType::iterator iterator