LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

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/MachineBasicBlock.h"
00022 #include "llvm/Support/Annotation.h"
00023 
00024 namespace llvm {
00025 
00026 class Function;
00027 class TargetMachine;
00028 class SSARegMap;
00029 class MachineFrameInfo;
00030 class MachineConstantPool;
00031 
00032 // ilist_traits
00033 template <>
00034 struct ilist_traits<MachineBasicBlock> {
00035   // this is only set by the MachineFunction owning the ilist
00036   friend class MachineFunction;
00037   MachineFunction* Parent;
00038   
00039 public:
00040   ilist_traits<MachineBasicBlock>() : Parent(0) { }
00041   
00042   static MachineBasicBlock* getPrev(MachineBasicBlock* N) { return N->Prev; }
00043   static MachineBasicBlock* getNext(MachineBasicBlock* N) { return N->Next; }
00044   
00045   static const MachineBasicBlock*
00046   getPrev(const MachineBasicBlock* N) { return N->Prev; }
00047   
00048   static const MachineBasicBlock*
00049   getNext(const MachineBasicBlock* N) { return N->Next; }
00050   
00051   static void setPrev(MachineBasicBlock* N, MachineBasicBlock* prev) {
00052     N->Prev = prev;
00053   }
00054   static void setNext(MachineBasicBlock* N, MachineBasicBlock* next) {
00055     N->Next = next;
00056   }
00057 
00058   static MachineBasicBlock* createNode();
00059   void addNodeToList(MachineBasicBlock* N);
00060   void removeNodeFromList(MachineBasicBlock* N);
00061   void transferNodesFromList(iplist<MachineBasicBlock,
00062                                     ilist_traits<MachineBasicBlock> > &toList,
00063            ilist_iterator<MachineBasicBlock> first,
00064            ilist_iterator<MachineBasicBlock> last);
00065 };
00066 
00067 /// MachineFunctionInfo - This class can be derived from and used by targets to
00068 /// hold private target-specific information for each MachineFunction.  Objects
00069 /// of type are accessed/created with MF::getInfo and destroyed when the
00070 /// MachineFunction is destroyed.
00071 struct MachineFunctionInfo {
00072   virtual ~MachineFunctionInfo() {};
00073 };
00074 
00075 class MachineFunction : private Annotation {
00076   const Function *Fn;
00077   const TargetMachine &Target;
00078 
00079   // List of machine basic blocks in function
00080   ilist<MachineBasicBlock> BasicBlocks;
00081 
00082   // Keeping track of mapping from SSA values to registers
00083   SSARegMap *SSARegMapping;
00084 
00085   // Used to keep track of target-specific per-machine function information for
00086   // the target implementation.
00087   MachineFunctionInfo *MFInfo;
00088 
00089   // Keep track of objects allocated on the stack.
00090   MachineFrameInfo *FrameInfo;
00091 
00092   // Keep track of constants which are spilled to memory
00093   MachineConstantPool *ConstantPool;
00094 
00095   // Function-level unique numbering for MachineBasicBlocks.  When a
00096   // MachineBasicBlock is inserted into a MachineFunction is it automatically
00097   // numbered and this vector keeps track of the mapping from ID's to MBB's.
00098   std::vector<MachineBasicBlock*> MBBNumbering;
00099 
00100 public:
00101   MachineFunction(const Function *Fn, const TargetMachine &TM);
00102   ~MachineFunction();
00103 
00104   /// getFunction - Return the LLVM function that this machine code represents
00105   ///
00106   const Function *getFunction() const { return Fn; }
00107 
00108   /// getTarget - Return the target machine this machine code is compiled with
00109   ///
00110   const TargetMachine &getTarget() const { return Target; }
00111 
00112   /// SSARegMap Interface... Keep track of information about each SSA virtual
00113   /// register, such as which register class it belongs to.
00114   ///
00115   SSARegMap *getSSARegMap() const { return SSARegMapping; }
00116   void clearSSARegMap();
00117 
00118   /// getFrameInfo - Return the frame info object for the current function.
00119   /// This object contains information about objects allocated on the stack
00120   /// frame of the current function in an abstract way.
00121   ///
00122   MachineFrameInfo *getFrameInfo() const { return FrameInfo; }
00123 
00124   /// getConstantPool - Return the constant pool object for the current
00125   /// function.
00126   ///
00127   MachineConstantPool *getConstantPool() const { return ConstantPool; }
00128 
00129   /// MachineFunctionInfo - Keep track of various per-function pieces of
00130   /// information for the sparc backend.
00131   ///
00132   template<typename Ty>
00133   Ty *getInfo() {
00134     if (!MFInfo) MFInfo = new Ty(*this);
00135 
00136     assert((void*)dynamic_cast<Ty*>(MFInfo) == (void*)MFInfo &&
00137            "Invalid concrete type or multiple inheritence for getInfo");
00138     return static_cast<Ty*>(MFInfo);
00139   }
00140 
00141   /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
00142   /// are inserted into the machine function.  The block number for a machine
00143   /// basic block can be found by using the MBB::getBlockNumber method, this
00144   /// method provides the inverse mapping.
00145   ///
00146   MachineBasicBlock *getBlockNumbered(unsigned N) {
00147     assert(N < MBBNumbering.size() && "Illegal block number");
00148     assert(MBBNumbering[N] && "Block was removed from the machine function!");
00149     return MBBNumbering[N];
00150   }
00151 
00152   /// getLastBlock - Returns the MachineBasicBlock with the greatest number
00153   MachineBasicBlock *getLastBlock() {
00154     return MBBNumbering.back();
00155   }
00156   const MachineBasicBlock *getLastBlock() const {
00157     return MBBNumbering.back();
00158   }
00159 
00160   /// print - Print out the MachineFunction in a format suitable for debugging
00161   /// to the specified stream.
00162   ///
00163   void print(std::ostream &OS) const;
00164 
00165   /// viewCFG - This function is meant for use from the debugger.  You can just
00166   /// say 'call F->viewCFG()' and a ghostview window should pop up from the
00167   /// program, displaying the CFG of the current function with the code for each
00168   /// basic block inside.  This depends on there being a 'dot' and 'gv' program
00169   /// in your path.
00170   ///
00171   void viewCFG() const;
00172   
00173   /// viewCFGOnly - This function is meant for use from the debugger.  It works
00174   /// just like viewCFG, but it does not include the contents of basic blocks
00175   /// into the nodes, just the label.  If you are only interested in the CFG
00176   /// this can make the graph smaller.
00177   ///
00178   void viewCFGOnly() const;
00179 
00180   /// dump - Print the current MachineFunction to cerr, useful for debugger use.
00181   ///
00182   void dump() const;
00183 
00184   /// construct - Allocate and initialize a MachineFunction for a given Function
00185   /// and Target
00186   ///
00187   static MachineFunction& construct(const Function *F, const TargetMachine &TM);
00188 
00189   /// destruct - Destroy the MachineFunction corresponding to a given Function
00190   ///
00191   static void destruct(const Function *F);
00192 
00193   /// get - Return a handle to a MachineFunction corresponding to the given
00194   /// Function.  This should not be called before "construct()" for a given
00195   /// Function.
00196   ///
00197   static MachineFunction& get(const Function *F);
00198 
00199   // Provide accessors for the MachineBasicBlock list...
00200   typedef ilist<MachineBasicBlock> BasicBlockListType;
00201   typedef BasicBlockListType::iterator iterator;
00202   typedef BasicBlockListType::const_iterator const_iterator;
00203   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
00204   typedef std::reverse_iterator<iterator>             reverse_iterator;
00205 
00206   // Provide accessors for basic blocks...
00207   const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
00208         BasicBlockListType &getBasicBlockList()       { return BasicBlocks; }
00209  
00210   //===--------------------------------------------------------------------===//
00211   // BasicBlock iterator forwarding functions
00212   //
00213   iterator                 begin()       { return BasicBlocks.begin(); }
00214   const_iterator           begin() const { return BasicBlocks.begin(); }
00215   iterator                 end  ()       { return BasicBlocks.end();   }
00216   const_iterator           end  () const { return BasicBlocks.end();   }
00217 
00218   reverse_iterator        rbegin()       { return BasicBlocks.rbegin(); }
00219   const_reverse_iterator  rbegin() const { return BasicBlocks.rbegin(); }
00220   reverse_iterator        rend  ()       { return BasicBlocks.rend();   }
00221   const_reverse_iterator  rend  () const { return BasicBlocks.rend();   }
00222 
00223   unsigned                  size() const { return BasicBlocks.size(); }
00224   bool                     empty() const { return BasicBlocks.empty(); }
00225   const MachineBasicBlock &front() const { return BasicBlocks.front(); }
00226         MachineBasicBlock &front()       { return BasicBlocks.front(); }
00227   const MachineBasicBlock & back() const { return BasicBlocks.back(); }
00228         MachineBasicBlock & back()       { return BasicBlocks.back(); }
00229 
00230   //===--------------------------------------------------------------------===//
00231   // Internal functions used to automatically number MachineBasicBlocks
00232   //
00233 
00234   /// getNextMBBNumber - Returns the next unique number to be assigned
00235   /// to a MachineBasicBlock in this MachineFunction.
00236   ///
00237   unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
00238     MBBNumbering.push_back(MBB);
00239     return MBBNumbering.size()-1;
00240   }
00241 
00242   /// removeFromMBBNumbering - Remove the specific machine basic block from our
00243   /// tracker, this is only really to be used by the MachineBasicBlock
00244   /// implementation.
00245   void removeFromMBBNumbering(unsigned N) {
00246     assert(N < MBBNumbering.size() && "Illegal basic block #");
00247     MBBNumbering[N] = 0;
00248   }
00249 };
00250 
00251 //===--------------------------------------------------------------------===//
00252 // GraphTraits specializations for function basic block graphs (CFGs)
00253 //===--------------------------------------------------------------------===//
00254 
00255 // Provide specializations of GraphTraits to be able to treat a
00256 // machine function as a graph of machine basic blocks... these are
00257 // the same as the machine basic block iterators, except that the root
00258 // node is implicitly the first node of the function.
00259 //
00260 template <> struct GraphTraits<MachineFunction*> :
00261   public GraphTraits<MachineBasicBlock*> {
00262   static NodeType *getEntryNode(MachineFunction *F) {
00263     return &F->front();
00264   }
00265 
00266   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
00267   typedef MachineFunction::iterator nodes_iterator;
00268   static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); }
00269   static nodes_iterator nodes_end  (MachineFunction *F) { return F->end(); }
00270 };
00271 template <> struct GraphTraits<const MachineFunction*> :
00272   public GraphTraits<const MachineBasicBlock*> {
00273   static NodeType *getEntryNode(const MachineFunction *F) {
00274     return &F->front();
00275   }
00276 
00277   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
00278   typedef MachineFunction::const_iterator nodes_iterator;
00279   static nodes_iterator nodes_begin(const MachineFunction *F) { return F->begin(); }
00280   static nodes_iterator nodes_end  (const MachineFunction *F) { return F->end(); }
00281 };
00282 
00283 
00284 // Provide specializations of GraphTraits to be able to treat a function as a 
00285 // graph of basic blocks... and to walk it in inverse order.  Inverse order for
00286 // a function is considered to be when traversing the predecessor edges of a BB
00287 // instead of the successor edges.
00288 //
00289 template <> struct GraphTraits<Inverse<MachineFunction*> > :
00290   public GraphTraits<Inverse<MachineBasicBlock*> > {
00291   static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
00292     return &G.Graph->front();
00293   }
00294 };
00295 template <> struct GraphTraits<Inverse<const MachineFunction*> > :
00296   public GraphTraits<Inverse<const MachineBasicBlock*> > {
00297   static NodeType *getEntryNode(Inverse<const MachineFunction *> G) {
00298     return &G.Graph->front();
00299   }
00300 };
00301 
00302 } // End llvm namespace
00303 
00304 #endif