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 is distributed under the University of Illinois Open Source
00006 // 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/ADT/ilist.h"
00022 #include "llvm/CodeGen/MachineBasicBlock.h"
00023 #include "llvm/Support/Allocator.h"
00024 #include "llvm/Support/ArrayRecycler.h"
00025 #include "llvm/Support/DebugLoc.h"
00026 #include "llvm/Support/Recycler.h"
00027 
00028 namespace llvm {
00029 
00030 class Value;
00031 class Function;
00032 class GCModuleInfo;
00033 class MachineRegisterInfo;
00034 class MachineFrameInfo;
00035 class MachineConstantPool;
00036 class MachineJumpTableInfo;
00037 class MachineModuleInfo;
00038 class MCContext;
00039 class Pass;
00040 class TargetMachine;
00041 class TargetRegisterClass;
00042 struct MachinePointerInfo;
00043 
00044 template <>
00045 struct ilist_traits<MachineBasicBlock>
00046     : public ilist_default_traits<MachineBasicBlock> {
00047   mutable ilist_half_node<MachineBasicBlock> Sentinel;
00048 public:
00049   MachineBasicBlock *createSentinel() const {
00050     return static_cast<MachineBasicBlock*>(&Sentinel);
00051   }
00052   void destroySentinel(MachineBasicBlock *) const {}
00053 
00054   MachineBasicBlock *provideInitialHead() const { return createSentinel(); }
00055   MachineBasicBlock *ensureHead(MachineBasicBlock*) const {
00056     return createSentinel();
00057   }
00058   static void noteHead(MachineBasicBlock*, MachineBasicBlock*) {}
00059 
00060   void addNodeToList(MachineBasicBlock* MBB);
00061   void removeNodeFromList(MachineBasicBlock* MBB);
00062   void deleteNode(MachineBasicBlock *MBB);
00063 private:
00064   void createNode(const MachineBasicBlock &);
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 {
00076   const Function *Fn;
00077   const TargetMachine &Target;
00078   MCContext &Ctx;
00079   MachineModuleInfo &MMI;
00080   GCModuleInfo *GMI;
00081   
00082   // RegInfo - Information about each register in use in the function.
00083   MachineRegisterInfo *RegInfo;
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   // Keep track of jump tables for switch instructions
00096   MachineJumpTableInfo *JumpTableInfo;
00097 
00098   // Function-level unique numbering for MachineBasicBlocks.  When a
00099   // MachineBasicBlock is inserted into a MachineFunction is it automatically
00100   // numbered and this vector keeps track of the mapping from ID's to MBB's.
00101   std::vector<MachineBasicBlock*> MBBNumbering;
00102 
00103   // Pool-allocate MachineFunction-lifetime and IR objects.
00104   BumpPtrAllocator Allocator;
00105 
00106   // Allocation management for instructions in function.
00107   Recycler<MachineInstr> InstructionRecycler;
00108 
00109   // Allocation management for operand arrays on instructions.
00110   ArrayRecycler<MachineOperand> OperandRecycler;
00111 
00112   // Allocation management for basic blocks in function.
00113   Recycler<MachineBasicBlock> BasicBlockRecycler;
00114 
00115   // List of machine basic blocks in function
00116   typedef ilist<MachineBasicBlock> BasicBlockListType;
00117   BasicBlockListType BasicBlocks;
00118 
00119   /// FunctionNumber - This provides a unique ID for each function emitted in
00120   /// this translation unit.
00121   ///
00122   unsigned FunctionNumber;
00123   
00124   /// Alignment - The alignment of the function.
00125   unsigned Alignment;
00126 
00127   /// ExposesReturnsTwice - True if the function calls setjmp or related
00128   /// functions with attribute "returns twice", but doesn't have
00129   /// the attribute itself.
00130   /// This is used to limit optimizations which cannot reason
00131   /// about the control flow of such functions.
00132   bool ExposesReturnsTwice;
00133 
00134   /// True if the function includes MS-style inline assembly.
00135   bool HasMSInlineAsm;
00136 
00137   MachineFunction(const MachineFunction &) LLVM_DELETED_FUNCTION;
00138   void operator=(const MachineFunction&) LLVM_DELETED_FUNCTION;
00139 public:
00140   MachineFunction(const Function *Fn, const TargetMachine &TM,
00141                   unsigned FunctionNum, MachineModuleInfo &MMI,
00142                   GCModuleInfo* GMI);
00143   ~MachineFunction();
00144 
00145   MachineModuleInfo &getMMI() const { return MMI; }
00146   GCModuleInfo *getGMI() const { return GMI; }
00147   MCContext &getContext() const { return Ctx; }
00148 
00149   /// getFunction - Return the LLVM function that this machine code represents
00150   ///
00151   const Function *getFunction() const { return Fn; }
00152 
00153   /// getName - Return the name of the corresponding LLVM function.
00154   ///
00155   StringRef getName() const;
00156 
00157   /// getFunctionNumber - Return a unique ID for the current function.
00158   ///
00159   unsigned getFunctionNumber() const { return FunctionNumber; }
00160 
00161   /// getTarget - Return the target machine this machine code is compiled with
00162   ///
00163   const TargetMachine &getTarget() const { return Target; }
00164 
00165   /// getRegInfo - Return information about the registers currently in use.
00166   ///
00167   MachineRegisterInfo &getRegInfo() { return *RegInfo; }
00168   const MachineRegisterInfo &getRegInfo() const { return *RegInfo; }
00169 
00170   /// getFrameInfo - Return the frame info object for the current function.
00171   /// This object contains information about objects allocated on the stack
00172   /// frame of the current function in an abstract way.
00173   ///
00174   MachineFrameInfo *getFrameInfo() { return FrameInfo; }
00175   const MachineFrameInfo *getFrameInfo() const { return FrameInfo; }
00176 
00177   /// getJumpTableInfo - Return the jump table info object for the current 
00178   /// function.  This object contains information about jump tables in the
00179   /// current function.  If the current function has no jump tables, this will
00180   /// return null.
00181   const MachineJumpTableInfo *getJumpTableInfo() const { return JumpTableInfo; }
00182   MachineJumpTableInfo *getJumpTableInfo() { return JumpTableInfo; }
00183 
00184   /// getOrCreateJumpTableInfo - Get the JumpTableInfo for this function, if it
00185   /// does already exist, allocate one.
00186   MachineJumpTableInfo *getOrCreateJumpTableInfo(unsigned JTEntryKind);
00187 
00188   
00189   /// getConstantPool - Return the constant pool object for the current
00190   /// function.
00191   ///
00192   MachineConstantPool *getConstantPool() { return ConstantPool; }
00193   const MachineConstantPool *getConstantPool() const { return ConstantPool; }
00194 
00195   /// getAlignment - Return the alignment (log2, not bytes) of the function.
00196   ///
00197   unsigned getAlignment() const { return Alignment; }
00198 
00199   /// setAlignment - Set the alignment (log2, not bytes) of the function.
00200   ///
00201   void setAlignment(unsigned A) { Alignment = A; }
00202 
00203   /// ensureAlignment - Make sure the function is at least 1 << A bytes aligned.
00204   void ensureAlignment(unsigned A) {
00205     if (Alignment < A) Alignment = A;
00206   }
00207 
00208   /// exposesReturnsTwice - Returns true if the function calls setjmp or
00209   /// any other similar functions with attribute "returns twice" without
00210   /// having the attribute itself.
00211   bool exposesReturnsTwice() const {
00212     return ExposesReturnsTwice;
00213   }
00214 
00215   /// setCallsSetJmp - Set a flag that indicates if there's a call to
00216   /// a "returns twice" function.
00217   void setExposesReturnsTwice(bool B) {
00218     ExposesReturnsTwice = B;
00219   }
00220 
00221   /// Returns true if the function contains any MS-style inline assembly.
00222   bool hasMSInlineAsm() const {
00223     return HasMSInlineAsm;
00224   }
00225 
00226   /// Set a flag that indicates that the function contains MS-style inline
00227   /// assembly.
00228   void setHasMSInlineAsm(bool B) {
00229     HasMSInlineAsm = B;
00230   }
00231   
00232   /// getInfo - Keep track of various per-function pieces of information for
00233   /// backends that would like to do so.
00234   ///
00235   template<typename Ty>
00236   Ty *getInfo() {
00237     if (!MFInfo) {
00238         // This should be just `new (Allocator.Allocate<Ty>()) Ty(*this)', but
00239         // that apparently breaks GCC 3.3.
00240         Ty *Loc = static_cast<Ty*>(Allocator.Allocate(sizeof(Ty),
00241                                                       AlignOf<Ty>::Alignment));
00242         MFInfo = new (Loc) Ty(*this);
00243     }
00244     return static_cast<Ty*>(MFInfo);
00245   }
00246 
00247   template<typename Ty>
00248   const Ty *getInfo() const {
00249      return const_cast<MachineFunction*>(this)->getInfo<Ty>();
00250   }
00251 
00252   /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
00253   /// are inserted into the machine function.  The block number for a machine
00254   /// basic block can be found by using the MBB::getBlockNumber method, this
00255   /// method provides the inverse mapping.
00256   ///
00257   MachineBasicBlock *getBlockNumbered(unsigned N) const {
00258     assert(N < MBBNumbering.size() && "Illegal block number");
00259     assert(MBBNumbering[N] && "Block was removed from the machine function!");
00260     return MBBNumbering[N];
00261   }
00262 
00263   /// getNumBlockIDs - Return the number of MBB ID's allocated.
00264   ///
00265   unsigned getNumBlockIDs() const { return (unsigned)MBBNumbering.size(); }
00266   
00267   /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
00268   /// recomputes them.  This guarantees that the MBB numbers are sequential,
00269   /// dense, and match the ordering of the blocks within the function.  If a
00270   /// specific MachineBasicBlock is specified, only that block and those after
00271   /// it are renumbered.
00272   void RenumberBlocks(MachineBasicBlock *MBBFrom = 0);
00273   
00274   /// print - Print out the MachineFunction in a format suitable for debugging
00275   /// to the specified stream.
00276   ///
00277   void print(raw_ostream &OS, SlotIndexes* = 0) const;
00278 
00279   /// viewCFG - This function is meant for use from the debugger.  You can just
00280   /// say 'call F->viewCFG()' and a ghostview window should pop up from the
00281   /// program, displaying the CFG of the current function with the code for each
00282   /// basic block inside.  This depends on there being a 'dot' and 'gv' program
00283   /// in your path.
00284   ///
00285   void viewCFG() const;
00286 
00287   /// viewCFGOnly - This function is meant for use from the debugger.  It works
00288   /// just like viewCFG, but it does not include the contents of basic blocks
00289   /// into the nodes, just the label.  If you are only interested in the CFG
00290   /// this can make the graph smaller.
00291   ///
00292   void viewCFGOnly() const;
00293 
00294   /// dump - Print the current MachineFunction to cerr, useful for debugger use.
00295   ///
00296   void dump() const;
00297 
00298   /// verify - Run the current MachineFunction through the machine code
00299   /// verifier, useful for debugger use.
00300   void verify(Pass *p = NULL, const char *Banner = NULL) const;
00301 
00302   // Provide accessors for the MachineBasicBlock list...
00303   typedef BasicBlockListType::iterator iterator;
00304   typedef BasicBlockListType::const_iterator const_iterator;
00305   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
00306   typedef std::reverse_iterator<iterator>             reverse_iterator;
00307 
00308   /// addLiveIn - Add the specified physical register as a live-in value and
00309   /// create a corresponding virtual register for it.
00310   unsigned addLiveIn(unsigned PReg, const TargetRegisterClass *RC);
00311 
00312   //===--------------------------------------------------------------------===//
00313   // BasicBlock accessor functions.
00314   //
00315   iterator                 begin()       { return BasicBlocks.begin(); }
00316   const_iterator           begin() const { return BasicBlocks.begin(); }
00317   iterator                 end  ()       { return BasicBlocks.end();   }
00318   const_iterator           end  () const { return BasicBlocks.end();   }
00319 
00320   reverse_iterator        rbegin()       { return BasicBlocks.rbegin(); }
00321   const_reverse_iterator  rbegin() const { return BasicBlocks.rbegin(); }
00322   reverse_iterator        rend  ()       { return BasicBlocks.rend();   }
00323   const_reverse_iterator  rend  () const { return BasicBlocks.rend();   }
00324 
00325   unsigned                  size() const { return (unsigned)BasicBlocks.size();}
00326   bool                     empty() const { return BasicBlocks.empty(); }
00327   const MachineBasicBlock &front() const { return BasicBlocks.front(); }
00328         MachineBasicBlock &front()       { return BasicBlocks.front(); }
00329   const MachineBasicBlock & back() const { return BasicBlocks.back(); }
00330         MachineBasicBlock & back()       { return BasicBlocks.back(); }
00331 
00332   void push_back (MachineBasicBlock *MBB) { BasicBlocks.push_back (MBB); }
00333   void push_front(MachineBasicBlock *MBB) { BasicBlocks.push_front(MBB); }
00334   void insert(iterator MBBI, MachineBasicBlock *MBB) {
00335     BasicBlocks.insert(MBBI, MBB);
00336   }
00337   void splice(iterator InsertPt, iterator MBBI) {
00338     BasicBlocks.splice(InsertPt, BasicBlocks, MBBI);
00339   }
00340   void splice(iterator InsertPt, iterator MBBI, iterator MBBE) {
00341     BasicBlocks.splice(InsertPt, BasicBlocks, MBBI, MBBE);
00342   }
00343 
00344   void remove(iterator MBBI) {
00345     BasicBlocks.remove(MBBI);
00346   }
00347   void erase(iterator MBBI) {
00348     BasicBlocks.erase(MBBI);
00349   }
00350 
00351   //===--------------------------------------------------------------------===//
00352   // Internal functions used to automatically number MachineBasicBlocks
00353   //
00354 
00355   /// \brief Adds the MBB to the internal numbering. Returns the unique number
00356   /// assigned to the MBB.
00357   ///
00358   unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
00359     MBBNumbering.push_back(MBB);
00360     return (unsigned)MBBNumbering.size()-1;
00361   }
00362 
00363   /// removeFromMBBNumbering - Remove the specific machine basic block from our
00364   /// tracker, this is only really to be used by the MachineBasicBlock
00365   /// implementation.
00366   void removeFromMBBNumbering(unsigned N) {
00367     assert(N < MBBNumbering.size() && "Illegal basic block #");
00368     MBBNumbering[N] = 0;
00369   }
00370 
00371   /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
00372   /// of `new MachineInstr'.
00373   ///
00374   MachineInstr *CreateMachineInstr(const MCInstrDesc &MCID,
00375                                    DebugLoc DL,
00376                                    bool NoImp = false);
00377 
00378   /// CloneMachineInstr - Create a new MachineInstr which is a copy of the
00379   /// 'Orig' instruction, identical in all ways except the instruction
00380   /// has no parent, prev, or next.
00381   ///
00382   /// See also TargetInstrInfo::duplicate() for target-specific fixes to cloned
00383   /// instructions.
00384   MachineInstr *CloneMachineInstr(const MachineInstr *Orig);
00385 
00386   /// DeleteMachineInstr - Delete the given MachineInstr.
00387   ///
00388   void DeleteMachineInstr(MachineInstr *MI);
00389 
00390   /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
00391   /// instead of `new MachineBasicBlock'.
00392   ///
00393   MachineBasicBlock *CreateMachineBasicBlock(const BasicBlock *bb = 0);
00394 
00395   /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
00396   ///
00397   void DeleteMachineBasicBlock(MachineBasicBlock *MBB);
00398 
00399   /// getMachineMemOperand - Allocate a new MachineMemOperand.
00400   /// MachineMemOperands are owned by the MachineFunction and need not be
00401   /// explicitly deallocated.
00402   MachineMemOperand *getMachineMemOperand(MachinePointerInfo PtrInfo,
00403                                           unsigned f, uint64_t s,
00404                                           unsigned base_alignment,
00405                                           const MDNode *TBAAInfo = 0,
00406                                           const MDNode *Ranges = 0);
00407   
00408   /// getMachineMemOperand - Allocate a new MachineMemOperand by copying
00409   /// an existing one, adjusting by an offset and using the given size.
00410   /// MachineMemOperands are owned by the MachineFunction and need not be
00411   /// explicitly deallocated.
00412   MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
00413                                           int64_t Offset, uint64_t Size);
00414 
00415   typedef ArrayRecycler<MachineOperand>::Capacity OperandCapacity;
00416 
00417   /// Allocate an array of MachineOperands. This is only intended for use by
00418   /// internal MachineInstr functions.
00419   MachineOperand *allocateOperandArray(OperandCapacity Cap) {
00420     return OperandRecycler.allocate(Cap, Allocator);
00421   }
00422 
00423   /// Dellocate an array of MachineOperands and recycle the memory. This is
00424   /// only intended for use by internal MachineInstr functions.
00425   /// Cap must be the same capacity that was used to allocate the array.
00426   void deallocateOperandArray(OperandCapacity Cap, MachineOperand *Array) {
00427     OperandRecycler.deallocate(Cap, Array);
00428   }
00429 
00430   /// allocateMemRefsArray - Allocate an array to hold MachineMemOperand
00431   /// pointers.  This array is owned by the MachineFunction.
00432   MachineInstr::mmo_iterator allocateMemRefsArray(unsigned long Num);
00433 
00434   /// extractLoadMemRefs - Allocate an array and populate it with just the
00435   /// load information from the given MachineMemOperand sequence.
00436   std::pair<MachineInstr::mmo_iterator,
00437             MachineInstr::mmo_iterator>
00438     extractLoadMemRefs(MachineInstr::mmo_iterator Begin,
00439                        MachineInstr::mmo_iterator End);
00440 
00441   /// extractStoreMemRefs - Allocate an array and populate it with just the
00442   /// store information from the given MachineMemOperand sequence.
00443   std::pair<MachineInstr::mmo_iterator,
00444             MachineInstr::mmo_iterator>
00445     extractStoreMemRefs(MachineInstr::mmo_iterator Begin,
00446                         MachineInstr::mmo_iterator End);
00447 
00448   //===--------------------------------------------------------------------===//
00449   // Label Manipulation.
00450   //
00451   
00452   /// getJTISymbol - Return the MCSymbol for the specified non-empty jump table.
00453   /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
00454   /// normal 'L' label is returned.
00455   MCSymbol *getJTISymbol(unsigned JTI, MCContext &Ctx, 
00456                          bool isLinkerPrivate = false) const;
00457   
00458   /// getPICBaseSymbol - Return a function-local symbol to represent the PIC
00459   /// base.
00460   MCSymbol *getPICBaseSymbol() const;
00461 };
00462 
00463 //===--------------------------------------------------------------------===//
00464 // GraphTraits specializations for function basic block graphs (CFGs)
00465 //===--------------------------------------------------------------------===//
00466 
00467 // Provide specializations of GraphTraits to be able to treat a
00468 // machine function as a graph of machine basic blocks... these are
00469 // the same as the machine basic block iterators, except that the root
00470 // node is implicitly the first node of the function.
00471 //
00472 template <> struct GraphTraits<MachineFunction*> :
00473   public GraphTraits<MachineBasicBlock*> {
00474   static NodeType *getEntryNode(MachineFunction *F) {
00475     return &F->front();
00476   }
00477 
00478   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
00479   typedef MachineFunction::iterator nodes_iterator;
00480   static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); }
00481   static nodes_iterator nodes_end  (MachineFunction *F) { return F->end(); }
00482   static unsigned       size       (MachineFunction *F) { return F->size(); }
00483 };
00484 template <> struct GraphTraits<const MachineFunction*> :
00485   public GraphTraits<const MachineBasicBlock*> {
00486   static NodeType *getEntryNode(const MachineFunction *F) {
00487     return &F->front();
00488   }
00489 
00490   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
00491   typedef MachineFunction::const_iterator nodes_iterator;
00492   static nodes_iterator nodes_begin(const MachineFunction *F) {
00493     return F->begin();
00494   }
00495   static nodes_iterator nodes_end  (const MachineFunction *F) {
00496     return F->end();
00497   }
00498   static unsigned       size       (const MachineFunction *F)  {
00499     return F->size();
00500   }
00501 };
00502 
00503 
00504 // Provide specializations of GraphTraits to be able to treat a function as a
00505 // graph of basic blocks... and to walk it in inverse order.  Inverse order for
00506 // a function is considered to be when traversing the predecessor edges of a BB
00507 // instead of the successor edges.
00508 //
00509 template <> struct GraphTraits<Inverse<MachineFunction*> > :
00510   public GraphTraits<Inverse<MachineBasicBlock*> > {
00511   static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
00512     return &G.Graph->front();
00513   }
00514 };
00515 template <> struct GraphTraits<Inverse<const MachineFunction*> > :
00516   public GraphTraits<Inverse<const MachineBasicBlock*> > {
00517   static NodeType *getEntryNode(Inverse<const MachineFunction *> G) {
00518     return &G.Graph->front();
00519   }
00520 };
00521 
00522 } // End llvm namespace
00523 
00524 #endif