LLVM API Documentation

MachineFunction.h

Go to the documentation of this file.
00001 //===-- llvm/CodeGen/MachineFunction.h --------------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // Collect native machine code for a function.  This class contains a list of
00011 // MachineBasicBlock instances that make up the current compiled function.
00012 //
00013 // This class also contains pointers to various classes which hold
00014 // target-specific information about the generated code.
00015 //
00016 //===----------------------------------------------------------------------===//
00017 
00018 #ifndef LLVM_CODEGEN_MACHINEFUNCTION_H
00019 #define LLVM_CODEGEN_MACHINEFUNCTION_H
00020 
00021 #include "llvm/CodeGen/MachineDebugInfo.h"
00022 #include "llvm/CodeGen/MachineBasicBlock.h"
00023 #include "llvm/Support/Annotation.h"
00024 
00025 namespace llvm {
00026 
00027 class Function;
00028 class TargetMachine;
00029 class SSARegMap;
00030 class MachineFrameInfo;
00031 class MachineConstantPool;
00032 class MachineJumpTableInfo;
00033 
00034 // ilist_traits
00035 template <>
00036 struct ilist_traits<MachineBasicBlock> {
00037   // this is only set by the MachineFunction owning the ilist
00038   friend class MachineFunction;
00039   MachineFunction* Parent;
00040 
00041 public:
00042   ilist_traits<MachineBasicBlock>() : Parent(0) { }
00043 
00044   static MachineBasicBlock* getPrev(MachineBasicBlock* N) { return N->Prev; }
00045   static MachineBasicBlock* getNext(MachineBasicBlock* N) { return N->Next; }
00046 
00047   static const MachineBasicBlock*
00048   getPrev(const MachineBasicBlock* N) { return N->Prev; }
00049 
00050   static const MachineBasicBlock*
00051   getNext(const MachineBasicBlock* N) { return N->Next; }
00052 
00053   static void setPrev(MachineBasicBlock* N, MachineBasicBlock* prev) {
00054     N->Prev = prev;
00055   }
00056   static void setNext(MachineBasicBlock* N, MachineBasicBlock* next) {
00057     N->Next = next;
00058   }
00059 
00060   static MachineBasicBlock* createSentinel();
00061   static void destroySentinel(MachineBasicBlock *MBB) { delete MBB; }
00062   void addNodeToList(MachineBasicBlock* N);
00063   void removeNodeFromList(MachineBasicBlock* N);
00064   void transferNodesFromList(iplist<MachineBasicBlock,
00065                                     ilist_traits<MachineBasicBlock> > &toList,
00066                              ilist_iterator<MachineBasicBlock> first,
00067                              ilist_iterator<MachineBasicBlock> last);
00068 };
00069 
00070 /// MachineFunctionInfo - This class can be derived from and used by targets to
00071 /// hold private target-specific information for each MachineFunction.  Objects
00072 /// of type are accessed/created with MF::getInfo and destroyed when the
00073 /// MachineFunction is destroyed.
00074 struct MachineFunctionInfo {
00075   virtual ~MachineFunctionInfo() {};
00076 };
00077 
00078 class MachineFunction : private Annotation {
00079   const Function *Fn;
00080   const TargetMachine &Target;
00081 
00082   // List of machine basic blocks in function
00083   ilist<MachineBasicBlock> BasicBlocks;
00084 
00085   // Keeping track of mapping from SSA values to registers
00086   SSARegMap *SSARegMapping;
00087 
00088   // Used to keep track of target-specific per-machine function information for
00089   // the target implementation.
00090   MachineFunctionInfo *MFInfo;
00091 
00092   // Keep track of objects allocated on the stack.
00093   MachineFrameInfo *FrameInfo;
00094 
00095   // Keep track of constants which are spilled to memory
00096   MachineConstantPool *ConstantPool;
00097   
00098   // Keep track of jump tables for switch instructions
00099   MachineJumpTableInfo *JumpTableInfo;
00100 
00101   // Function-level unique numbering for MachineBasicBlocks.  When a
00102   // MachineBasicBlock is inserted into a MachineFunction is it automatically
00103   // numbered and this vector keeps track of the mapping from ID's to MBB's.
00104   std::vector<MachineBasicBlock*> MBBNumbering;
00105 
00106   /// UsedPhysRegs - This is a new[]'d array of bools that is computed and set
00107   /// by the register allocator, and must be kept up to date by passes that run
00108   /// after register allocation (though most don't modify this).  This is used
00109   /// so that the code generator knows which callee save registers to save and
00110   /// for other target specific uses.
00111   bool *UsedPhysRegs;
00112 
00113   /// LiveIns/LiveOuts - Keep track of the physical registers that are
00114   /// livein/liveout of the function.  Live in values are typically arguments in
00115   /// registers, live out values are typically return values in registers.
00116   /// LiveIn values are allowed to have virtual registers associated with them,
00117   /// stored in the second element.
00118   std::vector<std::pair<unsigned, unsigned> > LiveIns;
00119   std::vector<unsigned> LiveOuts;
00120   
00121 public:
00122   MachineFunction(const Function *Fn, const TargetMachine &TM);
00123   ~MachineFunction();
00124 
00125   /// getFunction - Return the LLVM function that this machine code represents
00126   ///
00127   const Function *getFunction() const { return Fn; }
00128 
00129   /// getTarget - Return the target machine this machine code is compiled with
00130   ///
00131   const TargetMachine &getTarget() const { return Target; }
00132 
00133   /// SSARegMap Interface... Keep track of information about each SSA virtual
00134   /// register, such as which register class it belongs to.
00135   ///
00136   SSARegMap *getSSARegMap() const { return SSARegMapping; }
00137   void clearSSARegMap();
00138 
00139   /// getFrameInfo - Return the frame info object for the current function.
00140   /// This object contains information about objects allocated on the stack
00141   /// frame of the current function in an abstract way.
00142   ///
00143   MachineFrameInfo *getFrameInfo() const { return FrameInfo; }
00144 
00145   /// getJumpTableInfo - Return the jump table info object for the current 
00146   /// function.  This object contains information about jump tables for switch
00147   /// instructions in the current function.
00148   ///
00149   MachineJumpTableInfo *getJumpTableInfo() const { return JumpTableInfo; }
00150   
00151   /// getConstantPool - Return the constant pool object for the current
00152   /// function.
00153   ///
00154   MachineConstantPool *getConstantPool() const { return ConstantPool; }
00155 
00156   /// MachineFunctionInfo - Keep track of various per-function pieces of
00157   /// information for backends that would like to do so.
00158   ///
00159   template<typename Ty>
00160   Ty *getInfo() {
00161     if (!MFInfo) MFInfo = new Ty(*this);
00162 
00163     assert((void*)dynamic_cast<Ty*>(MFInfo) == (void*)MFInfo &&
00164            "Invalid concrete type or multiple inheritence for getInfo");
00165     return static_cast<Ty*>(MFInfo);
00166   }
00167 
00168   /// setUsedPhysRegs - The register allocator should call this to initialized
00169   /// the UsedPhysRegs set.  This should be passed a new[]'d array with entries
00170   /// for all of the physical registers that the target supports.  Each array
00171   /// entry should be set to true iff the physical register is used within the
00172   /// function.
00173   void setUsedPhysRegs(bool *UPR) { UsedPhysRegs = UPR; }
00174 
00175   /// getUsedPhysregs - This returns the UsedPhysRegs array.  This returns null
00176   /// before register allocation.
00177   bool *getUsedPhysregs() { return UsedPhysRegs; }
00178   const bool *getUsedPhysregs() const { return UsedPhysRegs; }
00179 
00180   /// isPhysRegUsed - Return true if the specified register is used in this
00181   /// function.  This only works after register allocation.
00182   bool isPhysRegUsed(unsigned Reg) { return UsedPhysRegs[Reg]; }
00183 
00184   /// changePhyRegUsed - This method allows code that runs after register
00185   /// allocation to keep the PhysRegsUsed array up-to-date.
00186   void changePhyRegUsed(unsigned Reg, bool State) { UsedPhysRegs[Reg] = State; }
00187 
00188 
00189   // LiveIn/LiveOut management methods.
00190 
00191   /// addLiveIn/Out - Add the specified register as a live in/out.  Note that it
00192   /// is an error to add the same register to the same set more than once.
00193   void addLiveIn(unsigned Reg, unsigned vreg = 0) {
00194     LiveIns.push_back(std::make_pair(Reg, vreg));
00195   }
00196   void addLiveOut(unsigned Reg) { LiveOuts.push_back(Reg); }
00197 
00198   // Iteration support for live in/out sets.  These sets are kept in sorted
00199   // order by their register number.
00200   typedef std::vector<std::pair<unsigned,unsigned> >::const_iterator
00201   livein_iterator;
00202   typedef std::vector<unsigned>::const_iterator liveout_iterator;
00203   livein_iterator livein_begin() const { return LiveIns.begin(); }
00204   livein_iterator livein_end()   const { return LiveIns.end(); }
00205   bool            livein_empty() const { return LiveIns.empty(); }
00206   liveout_iterator liveout_begin() const { return LiveOuts.begin(); }
00207   liveout_iterator liveout_end()   const { return LiveOuts.end(); }
00208   bool             liveout_empty() const { return LiveOuts.empty(); }
00209 
00210   /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
00211   /// are inserted into the machine function.  The block number for a machine
00212   /// basic block can be found by using the MBB::getBlockNumber method, this
00213   /// method provides the inverse mapping.
00214   ///
00215   MachineBasicBlock *getBlockNumbered(unsigned N) {
00216     assert(N < MBBNumbering.size() && "Illegal block number");
00217     assert(MBBNumbering[N] && "Block was removed from the machine function!");
00218     return MBBNumbering[N];
00219   }
00220 
00221   /// getLastBlock - Returns the MachineBasicBlock with the greatest number
00222   MachineBasicBlock *getLastBlock() {
00223     return MBBNumbering.back();
00224   }
00225   const MachineBasicBlock *getLastBlock() const {
00226     return MBBNumbering.back();
00227   }
00228   
00229   /// print - Print out the MachineFunction in a format suitable for debugging
00230   /// to the specified stream.
00231   ///
00232   void print(std::ostream &OS) const;
00233 
00234   /// viewCFG - This function is meant for use from the debugger.  You can just
00235   /// say 'call F->viewCFG()' and a ghostview window should pop up from the
00236   /// program, displaying the CFG of the current function with the code for each
00237   /// basic block inside.  This depends on there being a 'dot' and 'gv' program
00238   /// in your path.
00239   ///
00240   void viewCFG() const;
00241 
00242   /// viewCFGOnly - This function is meant for use from the debugger.  It works
00243   /// just like viewCFG, but it does not include the contents of basic blocks
00244   /// into the nodes, just the label.  If you are only interested in the CFG
00245   /// this can make the graph smaller.
00246   ///
00247   void viewCFGOnly() const;
00248 
00249   /// dump - Print the current MachineFunction to cerr, useful for debugger use.
00250   ///
00251   void dump() const;
00252 
00253   /// construct - Allocate and initialize a MachineFunction for a given Function
00254   /// and Target
00255   ///
00256   static MachineFunction& construct(const Function *F, const TargetMachine &TM);
00257 
00258   /// destruct - Destroy the MachineFunction corresponding to a given Function
00259   ///
00260   static void destruct(const Function *F);
00261 
00262   /// get - Return a handle to a MachineFunction corresponding to the given
00263   /// Function.  This should not be called before "construct()" for a given
00264   /// Function.
00265   ///
00266   static MachineFunction& get(const Function *F);
00267 
00268   // Provide accessors for the MachineBasicBlock list...
00269   typedef ilist<MachineBasicBlock> BasicBlockListType;
00270   typedef BasicBlockListType::iterator iterator;
00271   typedef BasicBlockListType::const_iterator const_iterator;
00272   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
00273   typedef std::reverse_iterator<iterator>             reverse_iterator;
00274 
00275   // Provide accessors for basic blocks...
00276   const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
00277         BasicBlockListType &getBasicBlockList()       { return BasicBlocks; }
00278 
00279   //===--------------------------------------------------------------------===//
00280   // BasicBlock iterator forwarding functions
00281   //
00282   iterator                 begin()       { return BasicBlocks.begin(); }
00283   const_iterator           begin() const { return BasicBlocks.begin(); }
00284   iterator                 end  ()       { return BasicBlocks.end();   }
00285   const_iterator           end  () const { return BasicBlocks.end();   }
00286 
00287   reverse_iterator        rbegin()       { return BasicBlocks.rbegin(); }
00288   const_reverse_iterator  rbegin() const { return BasicBlocks.rbegin(); }
00289   reverse_iterator        rend  ()       { return BasicBlocks.rend();   }
00290   const_reverse_iterator  rend  () const { return BasicBlocks.rend();   }
00291 
00292   unsigned                  size() const { return BasicBlocks.size(); }
00293   bool                     empty() const { return BasicBlocks.empty(); }
00294   const MachineBasicBlock &front() const { return BasicBlocks.front(); }
00295         MachineBasicBlock &front()       { return BasicBlocks.front(); }
00296   const MachineBasicBlock & back() const { return BasicBlocks.back(); }
00297         MachineBasicBlock & back()       { return BasicBlocks.back(); }
00298 
00299   //===--------------------------------------------------------------------===//
00300   // Internal functions used to automatically number MachineBasicBlocks
00301   //
00302 
00303   /// getNextMBBNumber - Returns the next unique number to be assigned
00304   /// to a MachineBasicBlock in this MachineFunction.
00305   ///
00306   unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
00307     MBBNumbering.push_back(MBB);
00308     return MBBNumbering.size()-1;
00309   }
00310 
00311   /// removeFromMBBNumbering - Remove the specific machine basic block from our
00312   /// tracker, this is only really to be used by the MachineBasicBlock
00313   /// implementation.
00314   void removeFromMBBNumbering(unsigned N) {
00315     assert(N < MBBNumbering.size() && "Illegal basic block #");
00316     MBBNumbering[N] = 0;
00317   }
00318 };
00319 
00320 //===--------------------------------------------------------------------===//
00321 // GraphTraits specializations for function basic block graphs (CFGs)
00322 //===--------------------------------------------------------------------===//
00323 
00324 // Provide specializations of GraphTraits to be able to treat a
00325 // machine function as a graph of machine basic blocks... these are
00326 // the same as the machine basic block iterators, except that the root
00327 // node is implicitly the first node of the function.
00328 //
00329 template <> struct GraphTraits<MachineFunction*> :
00330   public GraphTraits<MachineBasicBlock*> {
00331   static NodeType *getEntryNode(MachineFunction *F) {
00332     return &F->front();
00333   }
00334 
00335   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
00336   typedef MachineFunction::iterator nodes_iterator;
00337   static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); }
00338   static nodes_iterator nodes_end  (MachineFunction *F) { return F->end(); }
00339 };
00340 template <> struct GraphTraits<const MachineFunction*> :
00341   public GraphTraits<const MachineBasicBlock*> {
00342   static NodeType *getEntryNode(const MachineFunction *F) {
00343     return &F->front();
00344   }
00345 
00346   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
00347   typedef MachineFunction::const_iterator nodes_iterator;
00348   static nodes_iterator nodes_begin(const MachineFunction *F) { return F->begin(); }
00349   static nodes_iterator nodes_end  (const MachineFunction *F) { return F->end(); }
00350 };
00351 
00352 
00353 // Provide specializations of GraphTraits to be able to treat a function as a
00354 // graph of basic blocks... and to walk it in inverse order.  Inverse order for
00355 // a function is considered to be when traversing the predecessor edges of a BB
00356 // instead of the successor edges.
00357 //
00358 template <> struct GraphTraits<Inverse<MachineFunction*> > :
00359   public GraphTraits<Inverse<MachineBasicBlock*> > {
00360   static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
00361     return &G.Graph->front();
00362   }
00363 };
00364 template <> struct GraphTraits<Inverse<const MachineFunction*> > :
00365   public GraphTraits<Inverse<const MachineBasicBlock*> > {
00366   static NodeType *getEntryNode(Inverse<const MachineFunction *> G) {
00367     return &G.Graph->front();
00368   }
00369 };
00370 
00371 } // End llvm namespace
00372 
00373 #endif