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