LLVM  14.0.0git
MSP430FrameLowering.cpp
Go to the documentation of this file.
1 //===-- MSP430FrameLowering.cpp - MSP430 Frame Information ----------------===//
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 // This file contains the MSP430 implementation of TargetFrameLowering class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "MSP430FrameLowering.h"
14 #include "MSP430InstrInfo.h"
16 #include "MSP430Subtarget.h"
22 #include "llvm/IR/DataLayout.h"
23 #include "llvm/IR/Function.h"
25 
26 using namespace llvm;
27 
29  const MachineFrameInfo &MFI = MF.getFrameInfo();
30 
31  return (MF.getTarget().Options.DisableFramePointerElim(MF) ||
33  MFI.isFrameAddressTaken());
34 }
35 
37  return !MF.getFrameInfo().hasVarSizedObjects();
38 }
39 
41  MachineBasicBlock &MBB) const {
42  assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
43  MachineFrameInfo &MFI = MF.getFrameInfo();
45  const MSP430InstrInfo &TII =
46  *static_cast<const MSP430InstrInfo *>(MF.getSubtarget().getInstrInfo());
47 
49  DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
50 
51  // Get the number of bytes to allocate from the FrameInfo.
52  uint64_t StackSize = MFI.getStackSize();
53 
54  uint64_t NumBytes = 0;
55  if (hasFP(MF)) {
56  // Calculate required stack adjustment
57  uint64_t FrameSize = StackSize - 2;
58  NumBytes = FrameSize - MSP430FI->getCalleeSavedFrameSize();
59 
60  // Get the offset of the stack slot for the EBP register... which is
61  // guaranteed to be the last slot by processFunctionBeforeFrameFinalized.
62  // Update the frame offset adjustment.
63  MFI.setOffsetAdjustment(-NumBytes);
64 
65  // Save FP into the appropriate stack slot...
66  BuildMI(MBB, MBBI, DL, TII.get(MSP430::PUSH16r))
68 
69  // Update FP with the new base value...
70  BuildMI(MBB, MBBI, DL, TII.get(MSP430::MOV16rr), MSP430::R4)
71  .addReg(MSP430::SP);
72 
73  // Mark the FramePtr as live-in in every block except the entry.
74  for (MachineBasicBlock &MBBJ : llvm::drop_begin(MF))
75  MBBJ.addLiveIn(MSP430::R4);
76 
77  } else
78  NumBytes = StackSize - MSP430FI->getCalleeSavedFrameSize();
79 
80  // Skip the callee-saved push instructions.
81  while (MBBI != MBB.end() && (MBBI->getOpcode() == MSP430::PUSH16r))
82  ++MBBI;
83 
84  if (MBBI != MBB.end())
85  DL = MBBI->getDebugLoc();
86 
87  if (NumBytes) { // adjust stack pointer: SP -= numbytes
88  // If there is an SUB16ri of SP immediately before this instruction, merge
89  // the two.
90  //NumBytes -= mergeSPUpdates(MBB, MBBI, true);
91  // If there is an ADD16ri or SUB16ri of SP immediately after this
92  // instruction, merge the two instructions.
93  // mergeSPUpdatesDown(MBB, MBBI, &NumBytes);
94 
95  if (NumBytes) {
96  MachineInstr *MI =
97  BuildMI(MBB, MBBI, DL, TII.get(MSP430::SUB16ri), MSP430::SP)
98  .addReg(MSP430::SP).addImm(NumBytes);
99  // The SRW implicit def is dead.
100  MI->getOperand(3).setIsDead();
101  }
102  }
103 }
104 
106  MachineBasicBlock &MBB) const {
107  const MachineFrameInfo &MFI = MF.getFrameInfo();
109  const MSP430InstrInfo &TII =
110  *static_cast<const MSP430InstrInfo *>(MF.getSubtarget().getInstrInfo());
111 
113  unsigned RetOpcode = MBBI->getOpcode();
114  DebugLoc DL = MBBI->getDebugLoc();
115 
116  switch (RetOpcode) {
117  case MSP430::RET:
118  case MSP430::RETI: break; // These are ok
119  default:
120  llvm_unreachable("Can only insert epilog into returning blocks");
121  }
122 
123  // Get the number of bytes to allocate from the FrameInfo
124  uint64_t StackSize = MFI.getStackSize();
125  unsigned CSSize = MSP430FI->getCalleeSavedFrameSize();
126  uint64_t NumBytes = 0;
127 
128  if (hasFP(MF)) {
129  // Calculate required stack adjustment
130  uint64_t FrameSize = StackSize - 2;
131  NumBytes = FrameSize - CSSize;
132 
133  // pop FP.
134  BuildMI(MBB, MBBI, DL, TII.get(MSP430::POP16r), MSP430::R4);
135  } else
136  NumBytes = StackSize - CSSize;
137 
138  // Skip the callee-saved pop instructions.
139  while (MBBI != MBB.begin()) {
140  MachineBasicBlock::iterator PI = std::prev(MBBI);
141  unsigned Opc = PI->getOpcode();
142  if (Opc != MSP430::POP16r && !PI->isTerminator())
143  break;
144  --MBBI;
145  }
146 
147  DL = MBBI->getDebugLoc();
148 
149  // If there is an ADD16ri or SUB16ri of SP immediately before this
150  // instruction, merge the two instructions.
151  //if (NumBytes || MFI.hasVarSizedObjects())
152  // mergeSPUpdatesUp(MBB, MBBI, StackPtr, &NumBytes);
153 
154  if (MFI.hasVarSizedObjects()) {
155  BuildMI(MBB, MBBI, DL,
156  TII.get(MSP430::MOV16rr), MSP430::SP).addReg(MSP430::R4);
157  if (CSSize) {
158  MachineInstr *MI =
159  BuildMI(MBB, MBBI, DL,
160  TII.get(MSP430::SUB16ri), MSP430::SP)
161  .addReg(MSP430::SP).addImm(CSSize);
162  // The SRW implicit def is dead.
163  MI->getOperand(3).setIsDead();
164  }
165  } else {
166  // adjust stack pointer back: SP += numbytes
167  if (NumBytes) {
168  MachineInstr *MI =
169  BuildMI(MBB, MBBI, DL, TII.get(MSP430::ADD16ri), MSP430::SP)
170  .addReg(MSP430::SP).addImm(NumBytes);
171  // The SRW implicit def is dead.
172  MI->getOperand(3).setIsDead();
173  }
174  }
175 }
176 
177 // FIXME: Can we eleminate these in favour of generic code?
181  if (CSI.empty())
182  return false;
183 
184  DebugLoc DL;
185  if (MI != MBB.end()) DL = MI->getDebugLoc();
186 
187  MachineFunction &MF = *MBB.getParent();
188  const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
190  MFI->setCalleeSavedFrameSize(CSI.size() * 2);
191 
192  for (unsigned i = CSI.size(); i != 0; --i) {
193  unsigned Reg = CSI[i-1].getReg();
194  // Add the callee-saved register as live-in. It's killed at the spill.
195  MBB.addLiveIn(Reg);
196  BuildMI(MBB, MI, DL, TII.get(MSP430::PUSH16r))
198  }
199  return true;
200 }
201 
205  if (CSI.empty())
206  return false;
207 
208  DebugLoc DL;
209  if (MI != MBB.end()) DL = MI->getDebugLoc();
210 
211  MachineFunction &MF = *MBB.getParent();
212  const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
213 
214  for (unsigned i = 0, e = CSI.size(); i != e; ++i)
215  BuildMI(MBB, MI, DL, TII.get(MSP430::POP16r), CSI[i].getReg());
216 
217  return true;
218 }
219 
223  const MSP430InstrInfo &TII =
224  *static_cast<const MSP430InstrInfo *>(MF.getSubtarget().getInstrInfo());
225  if (!hasReservedCallFrame(MF)) {
226  // If the stack pointer can be changed after prologue, turn the
227  // adjcallstackup instruction into a 'sub SP, <amt>' and the
228  // adjcallstackdown instruction into 'add SP, <amt>'
229  // TODO: consider using push / pop instead of sub + store / add
230  MachineInstr &Old = *I;
231  uint64_t Amount = TII.getFrameSize(Old);
232  if (Amount != 0) {
233  // We need to keep the stack aligned properly. To do this, we round the
234  // amount of space needed for the outgoing arguments up to the next
235  // alignment boundary.
236  Amount = alignTo(Amount, getStackAlign());
237 
238  MachineInstr *New = nullptr;
239  if (Old.getOpcode() == TII.getCallFrameSetupOpcode()) {
240  New =
241  BuildMI(MF, Old.getDebugLoc(), TII.get(MSP430::SUB16ri), MSP430::SP)
242  .addReg(MSP430::SP)
243  .addImm(Amount);
244  } else {
245  assert(Old.getOpcode() == TII.getCallFrameDestroyOpcode());
246  // factor out the amount the callee already popped.
247  Amount -= TII.getFramePoppedByCallee(Old);
248  if (Amount)
249  New = BuildMI(MF, Old.getDebugLoc(), TII.get(MSP430::ADD16ri),
250  MSP430::SP)
251  .addReg(MSP430::SP)
252  .addImm(Amount);
253  }
254 
255  if (New) {
256  // The SRW implicit def is dead.
257  New->getOperand(3).setIsDead();
258 
259  // Replace the pseudo instruction with a new instruction...
260  MBB.insert(I, New);
261  }
262  }
263  } else if (I->getOpcode() == TII.getCallFrameDestroyOpcode()) {
264  // If we are performing frame pointer elimination and if the callee pops
265  // something off the stack pointer, add it back.
266  if (uint64_t CalleeAmt = TII.getFramePoppedByCallee(*I)) {
267  MachineInstr &Old = *I;
268  MachineInstr *New =
269  BuildMI(MF, Old.getDebugLoc(), TII.get(MSP430::SUB16ri), MSP430::SP)
270  .addReg(MSP430::SP)
271  .addImm(CalleeAmt);
272  // The SRW implicit def is dead.
273  New->getOperand(3).setIsDead();
274 
275  MBB.insert(I, New);
276  }
277  }
278 
279  return MBB.erase(I);
280 }
281 
282 void
284  RegScavenger *) const {
285  // Create a frame entry for the FP register that must be saved.
286  if (hasFP(MF)) {
287  int FrameIdx = MF.getFrameInfo().CreateFixedObject(2, -4, true);
288  (void)FrameIdx;
289  assert(FrameIdx == MF.getFrameInfo().getObjectIndexBegin() &&
290  "Slot for FP register must be last in order to be found!");
291  }
292 }
i
i
Definition: README.txt:29
llvm::alignTo
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:148
llvm::MachineFrameInfo::hasVarSizedObjects
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
Definition: MachineFrameInfo.h:353
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:103
llvm::MachineInstrBuilder::addImm
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
Definition: MachineInstrBuilder.h:131
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
Reg
unsigned Reg
Definition: MachineSink.cpp:1566
llvm::drop_begin
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition: STLExtras.h:266
llvm::MSP430FrameLowering::emitPrologue
void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override
emitProlog/emitEpilog - These methods insert prolog and epilog code into the function.
Definition: MSP430FrameLowering.cpp:40
llvm::TargetSubtargetInfo::getInstrInfo
virtual const TargetInstrInfo * getInstrInfo() const
Definition: TargetSubtargetInfo.h:92
R4
#define R4(n)
llvm::TargetRegisterInfo
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
Definition: TargetRegisterInfo.h:233
MSP430Subtarget.h
MSP430InstrInfo.h
TRI
unsigned const TargetRegisterInfo * TRI
Definition: MachineSink.cpp:1567
llvm::ArrayRef::empty
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:160
MachineRegisterInfo.h
llvm::MachineBasicBlock::erase
instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
Definition: MachineBasicBlock.cpp:1299
llvm::MSP430MachineFunctionInfo
MSP430MachineFunctionInfo - This class is derived from MachineFunction and contains private MSP430 ta...
Definition: MSP430MachineFunctionInfo.h:22
llvm::MachineFunction::front
const MachineBasicBlock & front() const
Definition: MachineFunction.h:820
llvm::TargetInstrInfo
TargetInstrInfo - Interface to description of machine instruction set.
Definition: TargetInstrInfo.h:97
llvm::MutableArrayRef
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:307
llvm::MachineFunction::getInfo
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
Definition: MachineFunction.h:724
llvm::TargetFrameLowering::getStackAlign
Align getStackAlign() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
Definition: TargetFrameLowering.h:100
TII
const HexagonInstrInfo * TII
Definition: HexagonCopyToCombine.cpp:129
llvm::MSP430FrameLowering::eliminateCallFramePseudoInstr
MachineBasicBlock::iterator eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator I) const override
This method is called during prolog/epilog code insertion to eliminate call frame setup and destroy p...
Definition: MSP430FrameLowering.cpp:220
llvm::MachineFrameInfo::getStackSize
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects.
Definition: MachineFrameInfo.h:553
llvm::MachineFrameInfo::getObjectIndexBegin
int getObjectIndexBegin() const
Return the minimum frame object index.
Definition: MachineFrameInfo.h:390
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:95
llvm::TargetOptions::DisableFramePointerElim
bool DisableFramePointerElim(const MachineFunction &MF) const
DisableFramePointerElim - This returns true if frame pointer elimination optimization should be disab...
Definition: TargetOptionsImpl.cpp:24
llvm::MachineFunction::getSubtarget
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Definition: MachineFunction.h:626
llvm::MachineInstr::getDebugLoc
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Definition: MachineInstr.h:418
MSP430FrameLowering.h
llvm::MSP430InstrInfo
Definition: MSP430InstrInfo.h:26
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:64
uint64_t
llvm::MSP430FrameLowering::emitEpilogue
void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override
Definition: MSP430FrameLowering.cpp:105
llvm::numbers::e
constexpr double e
Definition: MathExtras.h:57
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::RegScavenger
Definition: RegisterScavenging.h:34
llvm::MachineBasicBlock::getLastNonDebugInstr
iterator getLastNonDebugInstr(bool SkipPseudoOp=true)
Returns an iterator to the last non-debug instruction in the basic block, or end().
Definition: MachineBasicBlock.cpp:267
llvm::TargetMachine::Options
TargetOptions Options
Definition: TargetMachine.h:120
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MachineFrameInfo::CreateFixedObject
int CreateFixedObject(uint64_t Size, int64_t SPOffset, bool IsImmutable, bool isAliased=false)
Create a new object at a fixed location on the stack.
Definition: MachineFrameInfo.cpp:83
llvm::MachineFunction::getFrameInfo
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Definition: MachineFunction.h:642
llvm::MachineBasicBlock::getParent
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
Definition: MachineBasicBlock.h:225
llvm::MSP430MachineFunctionInfo::setCalleeSavedFrameSize
void setCalleeSavedFrameSize(unsigned bytes)
Definition: MSP430MachineFunctionInfo.h:47
llvm::MSP430FrameLowering::restoreCalleeSavedRegisters
bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, MutableArrayRef< CalleeSavedInfo > CSI, const TargetRegisterInfo *TRI) const override
restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee saved registers and returns...
Definition: MSP430FrameLowering.cpp:202
MachineModuleInfo.h
llvm::MachineInstrBuilder::addReg
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Definition: MachineInstrBuilder.h:97
llvm::MSP430MachineFunctionInfo::getCalleeSavedFrameSize
unsigned getCalleeSavedFrameSize() const
Definition: MSP430MachineFunctionInfo.h:46
llvm::MachineFunction
Definition: MachineFunction.h:230
TargetOptions.h
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
DataLayout.h
MBBI
MachineBasicBlock MachineBasicBlock::iterator MBBI
Definition: AArch64SLSHardening.cpp:75
llvm::MachineInstr::getOpcode
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:489
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::MachineBasicBlock::addLiveIn
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
Definition: MachineBasicBlock.h:367
llvm::MachineFrameInfo::isFrameAddressTaken
bool isFrameAddressTaken() const
This method may be called any time after instruction selection is complete to determine if there is a...
Definition: MachineFrameInfo.h:368
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::MSP430FrameLowering::hasFP
bool hasFP(const MachineFunction &MF) const override
hasFP - Return true if the specified function should have a dedicated frame pointer register.
Definition: MSP430FrameLowering.cpp:28
llvm::MachineFunction::getTarget
const LLVMTargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
Definition: MachineFunction.h:622
llvm::MachineBasicBlock::insert
instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
Definition: MachineBasicBlock.cpp:1312
MachineFrameInfo.h
llvm::ARCISD::RET
@ RET
Definition: ARCISelLowering.h:52
Function.h
llvm::MSP430FrameLowering::spillCalleeSavedRegisters
bool spillCalleeSavedRegisters(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, ArrayRef< CalleeSavedInfo > CSI, const TargetRegisterInfo *TRI) const override
spillCalleeSavedRegisters - Issues instruction(s) to spill all callee saved registers and returns tru...
Definition: MSP430FrameLowering.cpp:178
MSP430MachineFunctionInfo.h
llvm::MachineFrameInfo
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
Definition: MachineFrameInfo.h:107
llvm::MachineBasicBlock::begin
iterator begin()
Definition: MachineBasicBlock.h:268
MachineInstrBuilder.h
llvm::BuildMI
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
Definition: MachineInstrBuilder.h:328
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
llvm::RegState::Kill
@ Kill
The last use of a register.
Definition: MachineInstrBuilder.h:48
llvm::MachineFrameInfo::setOffsetAdjustment
void setOffsetAdjustment(int Adj)
Set the correction for frame offsets.
Definition: MachineFrameInfo.h:565
llvm::MSP430FrameLowering::hasReservedCallFrame
bool hasReservedCallFrame(const MachineFunction &MF) const override
hasReservedCallFrame - Under normal circumstances, when a frame pointer is not required,...
Definition: MSP430FrameLowering.cpp:36
llvm::DebugLoc
A debug info location.
Definition: DebugLoc.h:33
MachineFunction.h
llvm::MachineInstrBundleIterator< MachineInstr >
llvm::MSP430FrameLowering::processFunctionBeforeFrameFinalized
void processFunctionBeforeFrameFinalized(MachineFunction &MF, RegScavenger *RS=nullptr) const override
processFunctionBeforeFrameFinalized - This method is called immediately before the specified function...
Definition: MSP430FrameLowering.cpp:283
llvm::MachineBasicBlock::end
iterator end()
Definition: MachineBasicBlock.h:270