LLVM  4.0.0
MipsFrameLowering.cpp
Go to the documentation of this file.
1 //===-- MipsFrameLowering.cpp - Mips 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 Mips implementation of TargetFrameLowering class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "MipsFrameLowering.h"
16 #include "MipsInstrInfo.h"
17 #include "MipsMachineFunction.h"
18 #include "MipsTargetMachine.h"
24 #include "llvm/IR/DataLayout.h"
25 #include "llvm/IR/Function.h"
27 
28 using namespace llvm;
29 
30 
31 //===----------------------------------------------------------------------===//
32 //
33 // Stack Frame Processing methods
34 // +----------------------------+
35 //
36 // The stack is allocated decrementing the stack pointer on
37 // the first instruction of a function prologue. Once decremented,
38 // all stack references are done thought a positive offset
39 // from the stack/frame pointer, so the stack is considering
40 // to grow up! Otherwise terrible hacks would have to be made
41 // to get this stack ABI compliant :)
42 //
43 // The stack frame required by the ABI (after call):
44 // Offset
45 //
46 // 0 ----------
47 // 4 Args to pass
48 // . saved $GP (used in PIC)
49 // . Alloca allocations
50 // . Local Area
51 // . CPU "Callee Saved" Registers
52 // . saved FP
53 // . saved RA
54 // . FPU "Callee Saved" Registers
55 // StackSize -----------
56 //
57 // Offset - offset from sp after stack allocation on function prologue
58 //
59 // The sp is the stack pointer subtracted/added from the stack size
60 // at the Prologue/Epilogue
61 //
62 // References to the previous stack (to obtain arguments) are done
63 // with offsets that exceeds the stack size: (stacksize+(4*(num_arg-1))
64 //
65 // Examples:
66 // - reference to the actual stack frame
67 // for any local area var there is smt like : FI >= 0, StackOffset: 4
68 // sw REGX, 4(SP)
69 //
70 // - reference to previous stack frame
71 // suppose there's a load to the 5th arguments : FI < 0, StackOffset: 16.
72 // The emitted instruction will be something like:
73 // lw REGX, 16+StackSize(SP)
74 //
75 // Since the total stack size is unknown on LowerFormalArguments, all
76 // stack references (ObjectOffset) created to reference the function
77 // arguments, are negative numbers. This way, on eliminateFrameIndex it's
78 // possible to detect those references and the offsets are adjusted to
79 // their real location.
80 //
81 //===----------------------------------------------------------------------===//
82 
84  if (ST.inMips16Mode())
86 
88 }
89 
90 // hasFP - Return true if the specified function should have a dedicated frame
91 // pointer register. This is true if the function has variable sized allocas,
92 // if it needs dynamic stack realignment, if frame pointer elimination is
93 // disabled, or if the frame address is taken.
95  const MachineFrameInfo &MFI = MF.getFrameInfo();
96  const TargetRegisterInfo *TRI = STI.getRegisterInfo();
97 
98  return MF.getTarget().Options.DisableFramePointerElim(MF) ||
99  MFI.hasVarSizedObjects() || MFI.isFrameAddressTaken() ||
100  TRI->needsStackRealignment(MF);
101 }
102 
104  const MachineFrameInfo &MFI = MF.getFrameInfo();
105  const TargetRegisterInfo *TRI = STI.getRegisterInfo();
106 
107  return MFI.hasVarSizedObjects() && TRI->needsStackRealignment(MF);
108 }
109 
111  const MachineFrameInfo &MFI = MF.getFrameInfo();
112  const TargetRegisterInfo &TRI = *STI.getRegisterInfo();
113 
114  int64_t Offset = 0;
115 
116  // Iterate over fixed sized objects.
117  for (int I = MFI.getObjectIndexBegin(); I != 0; ++I)
118  Offset = std::max(Offset, -MFI.getObjectOffset(I));
119 
120  // Conservatively assume all callee-saved registers will be saved.
121  for (const MCPhysReg *R = TRI.getCalleeSavedRegs(&MF); *R; ++R) {
122  unsigned Size = TRI.getMinimalPhysRegClass(*R)->getSize();
123  Offset = alignTo(Offset + Size, Size);
124  }
125 
126  unsigned MaxAlign = MFI.getMaxAlignment();
127 
128  // Check that MaxAlign is not zero if there is a stack object that is not a
129  // callee-saved spill.
130  assert(!MFI.getObjectIndexEnd() || MaxAlign);
131 
132  // Iterate over other objects.
133  for (unsigned I = 0, E = MFI.getObjectIndexEnd(); I != E; ++I)
134  Offset = alignTo(Offset + MFI.getObjectSize(I), MaxAlign);
135 
136  // Call frame.
137  if (MFI.adjustsStack() && hasReservedCallFrame(MF))
138  Offset = alignTo(Offset + MFI.getMaxCallFrameSize(),
139  std::max(MaxAlign, getStackAlignment()));
140 
141  return alignTo(Offset, getStackAlignment());
142 }
143 
144 // Eliminate ADJCALLSTACKDOWN, ADJCALLSTACKUP pseudo instructions
148  unsigned SP = STI.getABI().IsN64() ? Mips::SP_64 : Mips::SP;
149 
150  if (!hasReservedCallFrame(MF)) {
151  int64_t Amount = I->getOperand(0).getImm();
152  if (I->getOpcode() == Mips::ADJCALLSTACKDOWN)
153  Amount = -Amount;
154 
155  STI.getInstrInfo()->adjustStackPtr(SP, Amount, MBB, I);
156  }
157 
158  return MBB.erase(I);
159 }
unsigned getStackAlignment() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
const MipsFrameLowering * createMipsSEFrameLowering(const MipsSubtarget &ST)
const MipsABIInfo & getABI() const
const MipsFrameLowering * createMips16FrameLowering(const MipsSubtarget &ST)
Create MipsFrameLowering objects.
const MipsSubtarget & STI
const MipsInstrInfo * getInstrInfo() const override
bool adjustsStack() const
Return true if this function adjusts the stack – e.g., when calling another function.
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
Definition: MathExtras.h:664
static const MipsFrameLowering * create(const MipsSubtarget &ST)
unsigned getMaxAlignment() const
Return the alignment in bytes that this function must be aligned to, which is greater than the defaul...
unsigned getSize() const
Return the size of the register in bytes, which is also the size of a stack slot allocated to hold a ...
bool DisableFramePointerElim(const MachineFunction &MF) const
DisableFramePointerElim - This returns true if frame pointer elimination optimization should be disab...
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...
bool hasFP(const MachineFunction &MF) const override
hasFP - Return true if the specified function should have a dedicated frame pointer register...
int getObjectIndexBegin() const
Return the minimum frame object index.
MachineBasicBlock * MBB
bool hasBP(const MachineFunction &MF) const
virtual const MCPhysReg * getCalleeSavedRegs(const MachineFunction *MF) const =0
Return a null-terminated list of all of the callee-saved registers on this target.
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
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...
uint32_t Offset
bool IsN64() const
Definition: MipsABIInfo.h:44
bool inMips16Mode() const
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual bool hasReservedCallFrame(const MachineFunction &MF) const
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.
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
const MipsRegisterInfo * getRegisterInfo() const override
unsigned getMaxCallFrameSize() const
Return the maximum size of a call frame that must be allocated for an outgoing function call...
const TargetRegisterClass * getMinimalPhysRegClass(unsigned Reg, MVT VT=MVT::Other) const
Returns the Register Class of a physical register of the given type, picking the most sub register cl...
#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 ...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
uint64_t estimateStackSize(const MachineFunction &MF) const
bool needsStackRealignment(const MachineFunction &MF) const
True if storage within the function requires the stack pointer to be aligned more than the normal cal...
int getObjectIndexEnd() const
Return one past the maximum frame object index.
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
virtual void adjustStackPtr(unsigned SP, int64_t Amount, MachineBasicBlock &MBB, MachineBasicBlock::iterator I) const =0