TargetFrameLowering.h revision 69261a644298bff1497d46c8cd38d688670f307b
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 21namespace 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. 33class TargetFrameLowering { 34public: 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 }; 45private: 46 StackDirection StackDir; 47 unsigned StackAlignment; 48 unsigned TransientStackAlignment; 49 int LocalAreaOffset; 50 bool StackRealignable; 51public: 52 TargetFrameLowering(StackDirection D, unsigned StackAl, int LAO, 53 unsigned TransAl = 1, bool StackReal = true) 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 /// 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 /// 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 /// 76 unsigned getTransientStackAlignment() const { 77 return TransientStackAlignment; 78 } 79 80 /// isStackRealignable - This method returns whether the stack can be 81 /// realigned. 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 /// 89 int getOffsetOfLocalArea() const { return LocalAreaOffset; } 90 91 /// getCalleeSavedSpillSlots - This method returns a pointer to an array of 92 /// pairs, that contains an entry for each callee saved register that must be 93 /// spilled to a particular stack location if it is spilled. 94 /// 95 /// Each entry in this array contains a <register,offset> pair, indicating the 96 /// fixed offset from the incoming stack pointer that each register should be 97 /// spilled at. If a register is not listed here, the code generator is 98 /// allowed to spill it anywhere it chooses. 99 /// 100 virtual const SpillSlot * 101 getCalleeSavedSpillSlots(unsigned &NumEntries) const { 102 NumEntries = 0; 103 return 0; 104 } 105 106 /// targetHandlesStackFrameRounding - Returns true if the target is 107 /// responsible for rounding up the stack frame (probably at emitPrologue 108 /// time). 109 virtual bool targetHandlesStackFrameRounding() const { 110 return false; 111 } 112 113 /// emitProlog/emitEpilog - These methods insert prolog and epilog code into 114 /// the function. 115 virtual void emitPrologue(MachineFunction &MF) const = 0; 116 virtual void emitEpilogue(MachineFunction &MF, 117 MachineBasicBlock &MBB) const = 0; 118 119 /// Adjust the prologue to have the function use segmented stacks. This works 120 /// by adding a check even before the "normal" function prologue. 121 virtual void adjustForSegmentedStacks(MachineFunction &MF) const { } 122 123 /// spillCalleeSavedRegisters - Issues instruction(s) to spill all callee 124 /// saved registers and returns true if it isn't possible / profitable to do 125 /// so by issuing a series of store instructions via 126 /// storeRegToStackSlot(). Returns false otherwise. 127 virtual bool spillCalleeSavedRegisters(MachineBasicBlock &MBB, 128 MachineBasicBlock::iterator MI, 129 const std::vector<CalleeSavedInfo> &CSI, 130 const TargetRegisterInfo *TRI) const { 131 return false; 132 } 133 134 /// restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee 135 /// saved registers and returns true if it isn't possible / profitable to do 136 /// so by issuing a series of load instructions via loadRegToStackSlot(). 137 /// Returns false otherwise. 138 virtual bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB, 139 MachineBasicBlock::iterator MI, 140 const std::vector<CalleeSavedInfo> &CSI, 141 const TargetRegisterInfo *TRI) const { 142 return false; 143 } 144 145 /// hasFP - Return true if the specified function should have a dedicated 146 /// frame pointer register. For most targets this is true only if the function 147 /// has variable sized allocas or if frame pointer elimination is disabled. 148 virtual bool hasFP(const MachineFunction &MF) const = 0; 149 150 /// hasReservedCallFrame - Under normal circumstances, when a frame pointer is 151 /// not required, we reserve argument space for call sites in the function 152 /// immediately on entry to the current function. This eliminates the need for 153 /// add/sub sp brackets around call sites. Returns true if the call frame is 154 /// included as part of the stack frame. 155 virtual bool hasReservedCallFrame(const MachineFunction &MF) const { 156 return !hasFP(MF); 157 } 158 159 /// canSimplifyCallFramePseudos - When possible, it's best to simplify the 160 /// call frame pseudo ops before doing frame index elimination. This is 161 /// possible only when frame index references between the pseudos won't 162 /// need adjusting for the call frame adjustments. Normally, that's true 163 /// if the function has a reserved call frame or a frame pointer. Some 164 /// targets (Thumb2, for example) may have more complicated criteria, 165 /// however, and can override this behavior. 166 virtual bool canSimplifyCallFramePseudos(const MachineFunction &MF) const { 167 return hasReservedCallFrame(MF) || hasFP(MF); 168 } 169 170 /// getFrameIndexOffset - Returns the displacement from the frame register to 171 /// the stack frame of the specified index. 172 virtual int getFrameIndexOffset(const MachineFunction &MF, int FI) const; 173 174 /// getFrameIndexReference - This method should return the base register 175 /// and offset used to reference a frame index location. The offset is 176 /// returned directly, and the base register is returned via FrameReg. 177 virtual int getFrameIndexReference(const MachineFunction &MF, int FI, 178 unsigned &FrameReg) const; 179 180 /// processFunctionBeforeCalleeSavedScan - This method is called immediately 181 /// before PrologEpilogInserter scans the physical registers used to determine 182 /// what callee saved registers should be spilled. This method is optional. 183 virtual void processFunctionBeforeCalleeSavedScan(MachineFunction &MF, 184 RegScavenger *RS = NULL) const { 185 186 } 187 188 /// processFunctionBeforeFrameFinalized - This method is called immediately 189 /// before the specified function's frame layout (MF.getFrameInfo()) is 190 /// finalized. Once the frame is finalized, MO_FrameIndex operands are 191 /// replaced with direct constants. This method is optional. 192 /// 193 virtual void processFunctionBeforeFrameFinalized(MachineFunction &MF) const { 194 } 195}; 196 197} // End llvm namespace 198 199#endif 200