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