LLVM API Documentation

TargetFrameLowering.h
Go to the documentation of this file.
00001 //===-- llvm/Target/TargetFrameLowering.h ---------------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // Interface to describe the layout of a stack frame on the target machine.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_TARGET_TARGETFRAMELOWERING_H
00015 #define LLVM_TARGET_TARGETFRAMELOWERING_H
00016 
00017 #include "llvm/CodeGen/MachineBasicBlock.h"
00018 #include <utility>
00019 #include <vector>
00020 
00021 namespace llvm {
00022   class CalleeSavedInfo;
00023   class MachineFunction;
00024   class RegScavenger;
00025 
00026 /// Information about stack frame layout on the target.  It holds the direction
00027 /// of stack growth, the known stack alignment on entry to each function, and
00028 /// the offset to the locals area.
00029 ///
00030 /// The offset to the local area is the offset from the stack pointer on
00031 /// function entry to the first location where function data (local variables,
00032 /// spill locations) can be stored.
00033 class TargetFrameLowering {
00034 public:
00035   enum StackDirection {
00036     StackGrowsUp,        // Adding to the stack increases the stack address
00037     StackGrowsDown       // Adding to the stack decreases the stack address
00038   };
00039 
00040   // Maps a callee saved register to a stack slot with a fixed offset.
00041   struct SpillSlot {
00042     unsigned Reg;
00043     int Offset; // Offset relative to stack pointer on function entry.
00044   };
00045 private:
00046   StackDirection StackDir;
00047   unsigned StackAlignment;
00048   unsigned TransientStackAlignment;
00049   int LocalAreaOffset;
00050   bool StackRealignable;
00051 public:
00052   TargetFrameLowering(StackDirection D, unsigned StackAl, int LAO,
00053                       unsigned TransAl = 1, bool StackReal = true)
00054     : StackDir(D), StackAlignment(StackAl), TransientStackAlignment(TransAl),
00055       LocalAreaOffset(LAO), StackRealignable(StackReal) {}
00056 
00057   virtual ~TargetFrameLowering();
00058 
00059   // These methods return information that describes the abstract stack layout
00060   // of the target machine.
00061 
00062   /// getStackGrowthDirection - Return the direction the stack grows
00063   ///
00064   StackDirection getStackGrowthDirection() const { return StackDir; }
00065 
00066   /// getStackAlignment - This method returns the number of bytes to which the
00067   /// stack pointer must be aligned on entry to a function.  Typically, this
00068   /// is the largest alignment for any data object in the target.
00069   ///
00070   unsigned getStackAlignment() const { return StackAlignment; }
00071 
00072   /// getTransientStackAlignment - This method returns the number of bytes to
00073   /// which the stack pointer must be aligned at all times, even between
00074   /// calls.
00075   ///
00076   unsigned getTransientStackAlignment() const {
00077     return TransientStackAlignment;
00078   }
00079 
00080   /// isStackRealignable - This method returns whether the stack can be
00081   /// realigned.
00082   bool isStackRealignable() const {
00083     return StackRealignable;
00084   }
00085 
00086   /// getOffsetOfLocalArea - This method returns the offset of the local area
00087   /// from the stack pointer on entrance to a function.
00088   ///
00089   int getOffsetOfLocalArea() const { return LocalAreaOffset; }
00090 
00091   /// isFPCloseToIncomingSP - Return true if the frame pointer is close to
00092   /// the incoming stack pointer, false if it is close to the post-prologue
00093   /// stack pointer.
00094   virtual bool isFPCloseToIncomingSP() const { return true; }
00095 
00096   /// assignCalleeSavedSpillSlots - Allows target to override spill slot
00097   /// assignment logic.  If implemented, assignCalleeSavedSpillSlots() should
00098   /// assign frame slots to all CSI entries and return true.  If this method
00099   /// returns false, spill slots will be assigned using generic implementation.
00100   /// assignCalleeSavedSpillSlots() may add, delete or rearrange elements of
00101   /// CSI.
00102   virtual bool
00103   assignCalleeSavedSpillSlots(MachineFunction &MF,
00104                               const TargetRegisterInfo *TRI,
00105                               std::vector<CalleeSavedInfo> &CSI) const {
00106     return false;
00107   }
00108 
00109   /// getCalleeSavedSpillSlots - This method returns a pointer to an array of
00110   /// pairs, that contains an entry for each callee saved register that must be
00111   /// spilled to a particular stack location if it is spilled.
00112   ///
00113   /// Each entry in this array contains a <register,offset> pair, indicating the
00114   /// fixed offset from the incoming stack pointer that each register should be
00115   /// spilled at. If a register is not listed here, the code generator is
00116   /// allowed to spill it anywhere it chooses.
00117   ///
00118   virtual const SpillSlot *
00119   getCalleeSavedSpillSlots(unsigned &NumEntries) const {
00120     NumEntries = 0;
00121     return nullptr;
00122   }
00123 
00124   /// targetHandlesStackFrameRounding - Returns true if the target is
00125   /// responsible for rounding up the stack frame (probably at emitPrologue
00126   /// time).
00127   virtual bool targetHandlesStackFrameRounding() const {
00128     return false;
00129   }
00130 
00131   /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
00132   /// the function.
00133   virtual void emitPrologue(MachineFunction &MF) const = 0;
00134   virtual void emitEpilogue(MachineFunction &MF,
00135                             MachineBasicBlock &MBB) const = 0;
00136 
00137   /// Adjust the prologue to have the function use segmented stacks. This works
00138   /// by adding a check even before the "normal" function prologue.
00139   virtual void adjustForSegmentedStacks(MachineFunction &MF) const { }
00140 
00141   /// Adjust the prologue to add Erlang Run-Time System (ERTS) specific code in
00142   /// the assembly prologue to explicitly handle the stack.
00143   virtual void adjustForHiPEPrologue(MachineFunction &MF) const { }
00144 
00145   /// spillCalleeSavedRegisters - Issues instruction(s) to spill all callee
00146   /// saved registers and returns true if it isn't possible / profitable to do
00147   /// so by issuing a series of store instructions via
00148   /// storeRegToStackSlot(). Returns false otherwise.
00149   virtual bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
00150                                          MachineBasicBlock::iterator MI,
00151                                         const std::vector<CalleeSavedInfo> &CSI,
00152                                          const TargetRegisterInfo *TRI) const {
00153     return false;
00154   }
00155 
00156   /// restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee
00157   /// saved registers and returns true if it isn't possible / profitable to do
00158   /// so by issuing a series of load instructions via loadRegToStackSlot().
00159   /// Returns false otherwise.
00160   virtual bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
00161                                            MachineBasicBlock::iterator MI,
00162                                         const std::vector<CalleeSavedInfo> &CSI,
00163                                         const TargetRegisterInfo *TRI) const {
00164     return false;
00165   }
00166 
00167   /// hasFP - Return true if the specified function should have a dedicated
00168   /// frame pointer register. For most targets this is true only if the function
00169   /// has variable sized allocas or if frame pointer elimination is disabled.
00170   virtual bool hasFP(const MachineFunction &MF) const = 0;
00171 
00172   /// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
00173   /// not required, we reserve argument space for call sites in the function
00174   /// immediately on entry to the current function. This eliminates the need for
00175   /// add/sub sp brackets around call sites. Returns true if the call frame is
00176   /// included as part of the stack frame.
00177   virtual bool hasReservedCallFrame(const MachineFunction &MF) const {
00178     return !hasFP(MF);
00179   }
00180 
00181   /// canSimplifyCallFramePseudos - When possible, it's best to simplify the
00182   /// call frame pseudo ops before doing frame index elimination. This is
00183   /// possible only when frame index references between the pseudos won't
00184   /// need adjusting for the call frame adjustments. Normally, that's true
00185   /// if the function has a reserved call frame or a frame pointer. Some
00186   /// targets (Thumb2, for example) may have more complicated criteria,
00187   /// however, and can override this behavior.
00188   virtual bool canSimplifyCallFramePseudos(const MachineFunction &MF) const {
00189     return hasReservedCallFrame(MF) || hasFP(MF);
00190   }
00191 
00192   /// getFrameIndexOffset - Returns the displacement from the frame register to
00193   /// the stack frame of the specified index.
00194   virtual int getFrameIndexOffset(const MachineFunction &MF, int FI) const;
00195 
00196   /// getFrameIndexReference - This method should return the base register
00197   /// and offset used to reference a frame index location. The offset is
00198   /// returned directly, and the base register is returned via FrameReg.
00199   virtual int getFrameIndexReference(const MachineFunction &MF, int FI,
00200                                      unsigned &FrameReg) const;
00201 
00202   /// processFunctionBeforeCalleeSavedScan - This method is called immediately
00203   /// before PrologEpilogInserter scans the physical registers used to determine
00204   /// what callee saved registers should be spilled. This method is optional.
00205   virtual void processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
00206                                              RegScavenger *RS = nullptr) const {
00207 
00208   }
00209 
00210   /// processFunctionBeforeFrameFinalized - This method is called immediately
00211   /// before the specified function's frame layout (MF.getFrameInfo()) is
00212   /// finalized.  Once the frame is finalized, MO_FrameIndex operands are
00213   /// replaced with direct constants.  This method is optional.
00214   ///
00215   virtual void processFunctionBeforeFrameFinalized(MachineFunction &MF,
00216                                              RegScavenger *RS = nullptr) const {
00217   }
00218 
00219   /// eliminateCallFramePseudoInstr - This method is called during prolog/epilog
00220   /// code insertion to eliminate call frame setup and destroy pseudo
00221   /// instructions (but only if the Target is using them).  It is responsible
00222   /// for eliminating these instructions, replacing them with concrete
00223   /// instructions.  This method need only be implemented if using call frame
00224   /// setup/destroy pseudo instructions.
00225   ///
00226   virtual void
00227   eliminateCallFramePseudoInstr(MachineFunction &MF,
00228                                 MachineBasicBlock &MBB,
00229                                 MachineBasicBlock::iterator MI) const {
00230     llvm_unreachable("Call Frame Pseudo Instructions do not exist on this "
00231                      "target!");
00232   }
00233 };
00234 
00235 } // End llvm namespace
00236 
00237 #endif