1 //===-- llvm/Target/TargetFrameLowering.h ---------------------------*- 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 // Interface to describe the layout of a stack frame on the target machine. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_TARGET_TARGETFRAMELOWERING_H 15 #define LLVM_TARGET_TARGETFRAMELOWERING_H 16 17 #include "llvm/CodeGen/MachineBasicBlock.h" 18 #include <utility> 19 #include <vector> 20 21 namespace llvm { 22 class CalleeSavedInfo; 23 class MachineFunction; 24 class RegScavenger; 25 26 /// Information about stack frame layout on the target. It holds the direction 27 /// of stack growth, the known stack alignment on entry to each function, and 28 /// the offset to the locals area. 29 /// 30 /// The offset to the local area is the offset from the stack pointer on 31 /// function entry to the first location where function data (local variables, 32 /// spill locations) can be stored. 33 class TargetFrameLowering { 34 public: 35 enum StackDirection { 36 StackGrowsUp, // Adding to the stack increases the stack address 37 StackGrowsDown // Adding to the stack decreases the stack address 38 }; 39 40 // Maps a callee saved register to a stack slot with a fixed offset. 41 struct SpillSlot { 42 unsigned Reg; 43 int Offset; // Offset relative to stack pointer on function entry. 44 }; 45 private: 46 StackDirection StackDir; 47 unsigned StackAlignment; 48 unsigned TransientStackAlignment; 49 int LocalAreaOffset; 50 bool StackRealignable; 51 public: 52 TargetFrameLowering(StackDirection D, unsigned StackAl, int LAO, 53 unsigned TransAl = 1, bool StackReal = true) StackDir(D)54 : StackDir(D), StackAlignment(StackAl), TransientStackAlignment(TransAl), 55 LocalAreaOffset(LAO), StackRealignable(StackReal) {} 56 57 virtual ~TargetFrameLowering(); 58 59 // These methods return information that describes the abstract stack layout 60 // of the target machine. 61 62 /// getStackGrowthDirection - Return the direction the stack grows 63 /// getStackGrowthDirection()64 StackDirection getStackGrowthDirection() const { return StackDir; } 65 66 /// getStackAlignment - This method returns the number of bytes to which the 67 /// stack pointer must be aligned on entry to a function. Typically, this 68 /// is the largest alignment for any data object in the target. 69 /// getStackAlignment()70 unsigned getStackAlignment() const { return StackAlignment; } 71 72 /// getTransientStackAlignment - This method returns the number of bytes to 73 /// which the stack pointer must be aligned at all times, even between 74 /// calls. 75 /// getTransientStackAlignment()76 unsigned getTransientStackAlignment() const { 77 return TransientStackAlignment; 78 } 79 80 /// isStackRealignable - This method returns whether the stack can be 81 /// realigned. isStackRealignable()82 bool isStackRealignable() const { 83 return StackRealignable; 84 } 85 86 /// getOffsetOfLocalArea - This method returns the offset of the local area 87 /// from the stack pointer on entrance to a function. 88 /// getOffsetOfLocalArea()89 int getOffsetOfLocalArea() const { return LocalAreaOffset; } 90 91 /// isFPCloseToIncomingSP - Return true if the frame pointer is close to 92 /// the incoming stack pointer, false if it is close to the post-prologue 93 /// stack pointer. isFPCloseToIncomingSP()94 virtual bool isFPCloseToIncomingSP() const { return true; } 95 96 /// getCalleeSavedSpillSlots - This method returns a pointer to an array of 97 /// pairs, that contains an entry for each callee saved register that must be 98 /// spilled to a particular stack location if it is spilled. 99 /// 100 /// Each entry in this array contains a <register,offset> pair, indicating the 101 /// fixed offset from the incoming stack pointer that each register should be 102 /// spilled at. If a register is not listed here, the code generator is 103 /// allowed to spill it anywhere it chooses. 104 /// 105 virtual const SpillSlot * getCalleeSavedSpillSlots(unsigned & NumEntries)106 getCalleeSavedSpillSlots(unsigned &NumEntries) const { 107 NumEntries = 0; 108 return 0; 109 } 110 111 /// targetHandlesStackFrameRounding - Returns true if the target is 112 /// responsible for rounding up the stack frame (probably at emitPrologue 113 /// time). targetHandlesStackFrameRounding()114 virtual bool targetHandlesStackFrameRounding() const { 115 return false; 116 } 117 118 /// emitProlog/emitEpilog - These methods insert prolog and epilog code into 119 /// the function. 120 virtual void emitPrologue(MachineFunction &MF) const = 0; 121 virtual void emitEpilogue(MachineFunction &MF, 122 MachineBasicBlock &MBB) const = 0; 123 124 /// Adjust the prologue to have the function use segmented stacks. This works 125 /// by adding a check even before the "normal" function prologue. adjustForSegmentedStacks(MachineFunction & MF)126 virtual void adjustForSegmentedStacks(MachineFunction &MF) const { } 127 128 /// Adjust the prologue to add Erlang Run-Time System (ERTS) specific code in 129 /// the assembly prologue to explicitly handle the stack. adjustForHiPEPrologue(MachineFunction & MF)130 virtual void adjustForHiPEPrologue(MachineFunction &MF) const { } 131 132 /// spillCalleeSavedRegisters - Issues instruction(s) to spill all callee 133 /// saved registers and returns true if it isn't possible / profitable to do 134 /// so by issuing a series of store instructions via 135 /// storeRegToStackSlot(). Returns false otherwise. spillCalleeSavedRegisters(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,const std::vector<CalleeSavedInfo> & CSI,const TargetRegisterInfo * TRI)136 virtual bool spillCalleeSavedRegisters(MachineBasicBlock &MBB, 137 MachineBasicBlock::iterator MI, 138 const std::vector<CalleeSavedInfo> &CSI, 139 const TargetRegisterInfo *TRI) const { 140 return false; 141 } 142 143 /// restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee 144 /// saved registers and returns true if it isn't possible / profitable to do 145 /// so by issuing a series of load instructions via loadRegToStackSlot(). 146 /// Returns false otherwise. restoreCalleeSavedRegisters(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,const std::vector<CalleeSavedInfo> & CSI,const TargetRegisterInfo * TRI)147 virtual bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB, 148 MachineBasicBlock::iterator MI, 149 const std::vector<CalleeSavedInfo> &CSI, 150 const TargetRegisterInfo *TRI) const { 151 return false; 152 } 153 154 /// hasFP - Return true if the specified function should have a dedicated 155 /// frame pointer register. For most targets this is true only if the function 156 /// has variable sized allocas or if frame pointer elimination is disabled. 157 virtual bool hasFP(const MachineFunction &MF) const = 0; 158 159 /// hasReservedCallFrame - Under normal circumstances, when a frame pointer is 160 /// not required, we reserve argument space for call sites in the function 161 /// immediately on entry to the current function. This eliminates the need for 162 /// add/sub sp brackets around call sites. Returns true if the call frame is 163 /// included as part of the stack frame. hasReservedCallFrame(const MachineFunction & MF)164 virtual bool hasReservedCallFrame(const MachineFunction &MF) const { 165 return !hasFP(MF); 166 } 167 168 /// canSimplifyCallFramePseudos - When possible, it's best to simplify the 169 /// call frame pseudo ops before doing frame index elimination. This is 170 /// possible only when frame index references between the pseudos won't 171 /// need adjusting for the call frame adjustments. Normally, that's true 172 /// if the function has a reserved call frame or a frame pointer. Some 173 /// targets (Thumb2, for example) may have more complicated criteria, 174 /// however, and can override this behavior. canSimplifyCallFramePseudos(const MachineFunction & MF)175 virtual bool canSimplifyCallFramePseudos(const MachineFunction &MF) const { 176 return hasReservedCallFrame(MF) || hasFP(MF); 177 } 178 179 /// getFrameIndexOffset - Returns the displacement from the frame register to 180 /// the stack frame of the specified index. 181 virtual int getFrameIndexOffset(const MachineFunction &MF, int FI) const; 182 183 /// getFrameIndexReference - This method should return the base register 184 /// and offset used to reference a frame index location. The offset is 185 /// returned directly, and the base register is returned via FrameReg. 186 virtual int getFrameIndexReference(const MachineFunction &MF, int FI, 187 unsigned &FrameReg) const; 188 189 /// processFunctionBeforeCalleeSavedScan - This method is called immediately 190 /// before PrologEpilogInserter scans the physical registers used to determine 191 /// what callee saved registers should be spilled. This method is optional. 192 virtual void processFunctionBeforeCalleeSavedScan(MachineFunction &MF, 193 RegScavenger *RS = NULL) const { 194 195 } 196 197 /// processFunctionBeforeFrameFinalized - This method is called immediately 198 /// before the specified function's frame layout (MF.getFrameInfo()) is 199 /// finalized. Once the frame is finalized, MO_FrameIndex operands are 200 /// replaced with direct constants. This method is optional. 201 /// 202 virtual void processFunctionBeforeFrameFinalized(MachineFunction &MF, 203 RegScavenger *RS = NULL) const { 204 } 205 206 /// eliminateCallFramePseudoInstr - This method is called during prolog/epilog 207 /// code insertion to eliminate call frame setup and destroy pseudo 208 /// instructions (but only if the Target is using them). It is responsible 209 /// for eliminating these instructions, replacing them with concrete 210 /// instructions. This method need only be implemented if using call frame 211 /// setup/destroy pseudo instructions. 212 /// 213 virtual void eliminateCallFramePseudoInstr(MachineFunction & MF,MachineBasicBlock & MBB,MachineBasicBlock::iterator MI)214 eliminateCallFramePseudoInstr(MachineFunction &MF, 215 MachineBasicBlock &MBB, 216 MachineBasicBlock::iterator MI) const { 217 llvm_unreachable("Call Frame Pseudo Instructions do not exist on this " 218 "target!"); 219 } 220 }; 221 222 } // End llvm namespace 223 224 #endif 225