Home | History | Annotate | Download | only in CodeGen
      1 //===-- llvm/CodeGen/MachineFunction.h --------------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // Collect native machine code for a function.  This class contains a list of
     11 // MachineBasicBlock instances that make up the current compiled function.
     12 //
     13 // This class also contains pointers to various classes which hold
     14 // target-specific information about the generated code.
     15 //
     16 //===----------------------------------------------------------------------===//
     17 
     18 #ifndef LLVM_CODEGEN_MACHINEFUNCTION_H
     19 #define LLVM_CODEGEN_MACHINEFUNCTION_H
     20 
     21 #include "llvm/CodeGen/MachineBasicBlock.h"
     22 #include "llvm/ADT/ilist.h"
     23 #include "llvm/Support/DebugLoc.h"
     24 #include "llvm/Support/Allocator.h"
     25 #include "llvm/Support/Recycler.h"
     26 
     27 namespace llvm {
     28 
     29 class Value;
     30 class Function;
     31 class GCModuleInfo;
     32 class MachineRegisterInfo;
     33 class MachineFrameInfo;
     34 class MachineConstantPool;
     35 class MachineJumpTableInfo;
     36 class MachineModuleInfo;
     37 class MCContext;
     38 class Pass;
     39 class TargetMachine;
     40 class TargetRegisterClass;
     41 struct MachinePointerInfo;
     42 
     43 template <>
     44 struct ilist_traits<MachineBasicBlock>
     45     : public ilist_default_traits<MachineBasicBlock> {
     46   mutable ilist_half_node<MachineBasicBlock> Sentinel;
     47 public:
     48   MachineBasicBlock *createSentinel() const {
     49     return static_cast<MachineBasicBlock*>(&Sentinel);
     50   }
     51   void destroySentinel(MachineBasicBlock *) const {}
     52 
     53   MachineBasicBlock *provideInitialHead() const { return createSentinel(); }
     54   MachineBasicBlock *ensureHead(MachineBasicBlock*) const {
     55     return createSentinel();
     56   }
     57   static void noteHead(MachineBasicBlock*, MachineBasicBlock*) {}
     58 
     59   void addNodeToList(MachineBasicBlock* MBB);
     60   void removeNodeFromList(MachineBasicBlock* MBB);
     61   void deleteNode(MachineBasicBlock *MBB);
     62 private:
     63   void createNode(const MachineBasicBlock &);
     64 };
     65 
     66 /// MachineFunctionInfo - This class can be derived from and used by targets to
     67 /// hold private target-specific information for each MachineFunction.  Objects
     68 /// of type are accessed/created with MF::getInfo and destroyed when the
     69 /// MachineFunction is destroyed.
     70 struct MachineFunctionInfo {
     71   virtual ~MachineFunctionInfo();
     72 };
     73 
     74 class MachineFunction {
     75   const Function *Fn;
     76   const TargetMachine &Target;
     77   MCContext &Ctx;
     78   MachineModuleInfo &MMI;
     79   GCModuleInfo *GMI;
     80 
     81   // RegInfo - Information about each register in use in the function.
     82   MachineRegisterInfo *RegInfo;
     83 
     84   // Used to keep track of target-specific per-machine function information for
     85   // the target implementation.
     86   MachineFunctionInfo *MFInfo;
     87 
     88   // Keep track of objects allocated on the stack.
     89   MachineFrameInfo *FrameInfo;
     90 
     91   // Keep track of constants which are spilled to memory
     92   MachineConstantPool *ConstantPool;
     93 
     94   // Keep track of jump tables for switch instructions
     95   MachineJumpTableInfo *JumpTableInfo;
     96 
     97   // Function-level unique numbering for MachineBasicBlocks.  When a
     98   // MachineBasicBlock is inserted into a MachineFunction is it automatically
     99   // numbered and this vector keeps track of the mapping from ID's to MBB's.
    100   std::vector<MachineBasicBlock*> MBBNumbering;
    101 
    102   // Pool-allocate MachineFunction-lifetime and IR objects.
    103   BumpPtrAllocator Allocator;
    104 
    105   // Allocation management for instructions in function.
    106   Recycler<MachineInstr> InstructionRecycler;
    107 
    108   // Allocation management for basic blocks in function.
    109   Recycler<MachineBasicBlock> BasicBlockRecycler;
    110 
    111   // List of machine basic blocks in function
    112   typedef ilist<MachineBasicBlock> BasicBlockListType;
    113   BasicBlockListType BasicBlocks;
    114 
    115   /// FunctionNumber - This provides a unique ID for each function emitted in
    116   /// this translation unit.
    117   ///
    118   unsigned FunctionNumber;
    119 
    120   /// Alignment - The alignment of the function.
    121   unsigned Alignment;
    122 
    123   /// CallsSetJmp - True if the function calls setjmp or sigsetjmp. This is used
    124   /// to limit optimizations which cannot reason about the control flow of
    125   /// setjmp.
    126   bool CallsSetJmp;
    127 
    128   MachineFunction(const MachineFunction &); // DO NOT IMPLEMENT
    129   void operator=(const MachineFunction&);   // DO NOT IMPLEMENT
    130 public:
    131   MachineFunction(const Function *Fn, const TargetMachine &TM,
    132                   unsigned FunctionNum, MachineModuleInfo &MMI,
    133                   GCModuleInfo* GMI);
    134   ~MachineFunction();
    135 
    136   MachineModuleInfo &getMMI() const { return MMI; }
    137   GCModuleInfo *getGMI() const { return GMI; }
    138   MCContext &getContext() const { return Ctx; }
    139 
    140   /// getFunction - Return the LLVM function that this machine code represents
    141   ///
    142   const Function *getFunction() const { return Fn; }
    143 
    144   /// getFunctionNumber - Return a unique ID for the current function.
    145   ///
    146   unsigned getFunctionNumber() const { return FunctionNumber; }
    147 
    148   /// getTarget - Return the target machine this machine code is compiled with
    149   ///
    150   const TargetMachine &getTarget() const { return Target; }
    151 
    152   /// getRegInfo - Return information about the registers currently in use.
    153   ///
    154   MachineRegisterInfo &getRegInfo() { return *RegInfo; }
    155   const MachineRegisterInfo &getRegInfo() const { return *RegInfo; }
    156 
    157   /// getFrameInfo - Return the frame info object for the current function.
    158   /// This object contains information about objects allocated on the stack
    159   /// frame of the current function in an abstract way.
    160   ///
    161   MachineFrameInfo *getFrameInfo() { return FrameInfo; }
    162   const MachineFrameInfo *getFrameInfo() const { return FrameInfo; }
    163 
    164   /// getJumpTableInfo - Return the jump table info object for the current
    165   /// function.  This object contains information about jump tables in the
    166   /// current function.  If the current function has no jump tables, this will
    167   /// return null.
    168   const MachineJumpTableInfo *getJumpTableInfo() const { return JumpTableInfo; }
    169   MachineJumpTableInfo *getJumpTableInfo() { return JumpTableInfo; }
    170 
    171   /// getOrCreateJumpTableInfo - Get the JumpTableInfo for this function, if it
    172   /// does already exist, allocate one.
    173   MachineJumpTableInfo *getOrCreateJumpTableInfo(unsigned JTEntryKind);
    174 
    175 
    176   /// getConstantPool - Return the constant pool object for the current
    177   /// function.
    178   ///
    179   MachineConstantPool *getConstantPool() { return ConstantPool; }
    180   const MachineConstantPool *getConstantPool() const { return ConstantPool; }
    181 
    182   /// getAlignment - Return the alignment (log2, not bytes) of the function.
    183   ///
    184   unsigned getAlignment() const { return Alignment; }
    185 
    186   /// setAlignment - Set the alignment (log2, not bytes) of the function.
    187   ///
    188   void setAlignment(unsigned A) { Alignment = A; }
    189 
    190   /// EnsureAlignment - Make sure the function is at least 'A' bits aligned.
    191   void EnsureAlignment(unsigned A) {
    192     if (Alignment < A) Alignment = A;
    193   }
    194 
    195   /// callsSetJmp - Returns true if the function calls setjmp or sigsetjmp.
    196   bool callsSetJmp() const {
    197     return CallsSetJmp;
    198   }
    199 
    200   /// setCallsSetJmp - Set a flag that indicates if there's a call to setjmp or
    201   /// sigsetjmp.
    202   void setCallsSetJmp(bool B) {
    203     CallsSetJmp = B;
    204   }
    205 
    206   /// getInfo - Keep track of various per-function pieces of information for
    207   /// backends that would like to do so.
    208   ///
    209   template<typename Ty>
    210   Ty *getInfo() {
    211     if (!MFInfo) {
    212         // This should be just `new (Allocator.Allocate<Ty>()) Ty(*this)', but
    213         // that apparently breaks GCC 3.3.
    214         Ty *Loc = static_cast<Ty*>(Allocator.Allocate(sizeof(Ty),
    215                                                       AlignOf<Ty>::Alignment));
    216         MFInfo = new (Loc) Ty(*this);
    217     }
    218     return static_cast<Ty*>(MFInfo);
    219   }
    220 
    221   template<typename Ty>
    222   const Ty *getInfo() const {
    223      return const_cast<MachineFunction*>(this)->getInfo<Ty>();
    224   }
    225 
    226   /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
    227   /// are inserted into the machine function.  The block number for a machine
    228   /// basic block can be found by using the MBB::getBlockNumber method, this
    229   /// method provides the inverse mapping.
    230   ///
    231   MachineBasicBlock *getBlockNumbered(unsigned N) const {
    232     assert(N < MBBNumbering.size() && "Illegal block number");
    233     assert(MBBNumbering[N] && "Block was removed from the machine function!");
    234     return MBBNumbering[N];
    235   }
    236 
    237   /// getNumBlockIDs - Return the number of MBB ID's allocated.
    238   ///
    239   unsigned getNumBlockIDs() const { return (unsigned)MBBNumbering.size(); }
    240 
    241   /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
    242   /// recomputes them.  This guarantees that the MBB numbers are sequential,
    243   /// dense, and match the ordering of the blocks within the function.  If a
    244   /// specific MachineBasicBlock is specified, only that block and those after
    245   /// it are renumbered.
    246   void RenumberBlocks(MachineBasicBlock *MBBFrom = 0);
    247 
    248   /// print - Print out the MachineFunction in a format suitable for debugging
    249   /// to the specified stream.
    250   ///
    251   void print(raw_ostream &OS, SlotIndexes* = 0) const;
    252 
    253   /// viewCFG - This function is meant for use from the debugger.  You can just
    254   /// say 'call F->viewCFG()' and a ghostview window should pop up from the
    255   /// program, displaying the CFG of the current function with the code for each
    256   /// basic block inside.  This depends on there being a 'dot' and 'gv' program
    257   /// in your path.
    258   ///
    259   void viewCFG() const;
    260 
    261   /// viewCFGOnly - This function is meant for use from the debugger.  It works
    262   /// just like viewCFG, but it does not include the contents of basic blocks
    263   /// into the nodes, just the label.  If you are only interested in the CFG
    264   /// this can make the graph smaller.
    265   ///
    266   void viewCFGOnly() const;
    267 
    268   /// dump - Print the current MachineFunction to cerr, useful for debugger use.
    269   ///
    270   void dump() const;
    271 
    272   /// verify - Run the current MachineFunction through the machine code
    273   /// verifier, useful for debugger use.
    274   void verify(Pass *p = NULL, const char *Banner = NULL) const;
    275 
    276   // Provide accessors for the MachineBasicBlock list...
    277   typedef BasicBlockListType::iterator iterator;
    278   typedef BasicBlockListType::const_iterator const_iterator;
    279   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
    280   typedef std::reverse_iterator<iterator>             reverse_iterator;
    281 
    282   /// addLiveIn - Add the specified physical register as a live-in value and
    283   /// create a corresponding virtual register for it.
    284   unsigned addLiveIn(unsigned PReg, const TargetRegisterClass *RC);
    285 
    286   //===--------------------------------------------------------------------===//
    287   // BasicBlock accessor functions.
    288   //
    289   iterator                 begin()       { return BasicBlocks.begin(); }
    290   const_iterator           begin() const { return BasicBlocks.begin(); }
    291   iterator                 end  ()       { return BasicBlocks.end();   }
    292   const_iterator           end  () const { return BasicBlocks.end();   }
    293 
    294   reverse_iterator        rbegin()       { return BasicBlocks.rbegin(); }
    295   const_reverse_iterator  rbegin() const { return BasicBlocks.rbegin(); }
    296   reverse_iterator        rend  ()       { return BasicBlocks.rend();   }
    297   const_reverse_iterator  rend  () const { return BasicBlocks.rend();   }
    298 
    299   unsigned                  size() const { return (unsigned)BasicBlocks.size();}
    300   bool                     empty() const { return BasicBlocks.empty(); }
    301   const MachineBasicBlock &front() const { return BasicBlocks.front(); }
    302         MachineBasicBlock &front()       { return BasicBlocks.front(); }
    303   const MachineBasicBlock & back() const { return BasicBlocks.back(); }
    304         MachineBasicBlock & back()       { return BasicBlocks.back(); }
    305 
    306   void push_back (MachineBasicBlock *MBB) { BasicBlocks.push_back (MBB); }
    307   void push_front(MachineBasicBlock *MBB) { BasicBlocks.push_front(MBB); }
    308   void insert(iterator MBBI, MachineBasicBlock *MBB) {
    309     BasicBlocks.insert(MBBI, MBB);
    310   }
    311   void splice(iterator InsertPt, iterator MBBI) {
    312     BasicBlocks.splice(InsertPt, BasicBlocks, MBBI);
    313   }
    314   void splice(iterator InsertPt, iterator MBBI, iterator MBBE) {
    315     BasicBlocks.splice(InsertPt, BasicBlocks, MBBI, MBBE);
    316   }
    317 
    318   void remove(iterator MBBI) {
    319     BasicBlocks.remove(MBBI);
    320   }
    321   void erase(iterator MBBI) {
    322     BasicBlocks.erase(MBBI);
    323   }
    324 
    325   //===--------------------------------------------------------------------===//
    326   // Internal functions used to automatically number MachineBasicBlocks
    327   //
    328 
    329   /// getNextMBBNumber - Returns the next unique number to be assigned
    330   /// to a MachineBasicBlock in this MachineFunction.
    331   ///
    332   unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
    333     MBBNumbering.push_back(MBB);
    334     return (unsigned)MBBNumbering.size()-1;
    335   }
    336 
    337   /// removeFromMBBNumbering - Remove the specific machine basic block from our
    338   /// tracker, this is only really to be used by the MachineBasicBlock
    339   /// implementation.
    340   void removeFromMBBNumbering(unsigned N) {
    341     assert(N < MBBNumbering.size() && "Illegal basic block #");
    342     MBBNumbering[N] = 0;
    343   }
    344 
    345   /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
    346   /// of `new MachineInstr'.
    347   ///
    348   MachineInstr *CreateMachineInstr(const MCInstrDesc &MCID,
    349                                    DebugLoc DL,
    350                                    bool NoImp = false);
    351 
    352   /// CloneMachineInstr - Create a new MachineInstr which is a copy of the
    353   /// 'Orig' instruction, identical in all ways except the instruction
    354   /// has no parent, prev, or next.
    355   ///
    356   /// See also TargetInstrInfo::duplicate() for target-specific fixes to cloned
    357   /// instructions.
    358   MachineInstr *CloneMachineInstr(const MachineInstr *Orig);
    359 
    360   /// DeleteMachineInstr - Delete the given MachineInstr.
    361   ///
    362   void DeleteMachineInstr(MachineInstr *MI);
    363 
    364   /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
    365   /// instead of `new MachineBasicBlock'.
    366   ///
    367   MachineBasicBlock *CreateMachineBasicBlock(const BasicBlock *bb = 0);
    368 
    369   /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
    370   ///
    371   void DeleteMachineBasicBlock(MachineBasicBlock *MBB);
    372 
    373   /// getMachineMemOperand - Allocate a new MachineMemOperand.
    374   /// MachineMemOperands are owned by the MachineFunction and need not be
    375   /// explicitly deallocated.
    376   MachineMemOperand *getMachineMemOperand(MachinePointerInfo PtrInfo,
    377                                           unsigned f, uint64_t s,
    378                                           unsigned base_alignment,
    379                                           const MDNode *TBAAInfo = 0);
    380 
    381   /// getMachineMemOperand - Allocate a new MachineMemOperand by copying
    382   /// an existing one, adjusting by an offset and using the given size.
    383   /// MachineMemOperands are owned by the MachineFunction and need not be
    384   /// explicitly deallocated.
    385   MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
    386                                           int64_t Offset, uint64_t Size);
    387 
    388   /// allocateMemRefsArray - Allocate an array to hold MachineMemOperand
    389   /// pointers.  This array is owned by the MachineFunction.
    390   MachineInstr::mmo_iterator allocateMemRefsArray(unsigned long Num);
    391 
    392   /// extractLoadMemRefs - Allocate an array and populate it with just the
    393   /// load information from the given MachineMemOperand sequence.
    394   std::pair<MachineInstr::mmo_iterator,
    395             MachineInstr::mmo_iterator>
    396     extractLoadMemRefs(MachineInstr::mmo_iterator Begin,
    397                        MachineInstr::mmo_iterator End);
    398 
    399   /// extractStoreMemRefs - Allocate an array and populate it with just the
    400   /// store information from the given MachineMemOperand sequence.
    401   std::pair<MachineInstr::mmo_iterator,
    402             MachineInstr::mmo_iterator>
    403     extractStoreMemRefs(MachineInstr::mmo_iterator Begin,
    404                         MachineInstr::mmo_iterator End);
    405 
    406   //===--------------------------------------------------------------------===//
    407   // Label Manipulation.
    408   //
    409 
    410   /// getJTISymbol - Return the MCSymbol for the specified non-empty jump table.
    411   /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
    412   /// normal 'L' label is returned.
    413   MCSymbol *getJTISymbol(unsigned JTI, MCContext &Ctx,
    414                          bool isLinkerPrivate = false) const;
    415 
    416   /// getPICBaseSymbol - Return a function-local symbol to represent the PIC
    417   /// base.
    418   MCSymbol *getPICBaseSymbol() const;
    419 };
    420 
    421 //===--------------------------------------------------------------------===//
    422 // GraphTraits specializations for function basic block graphs (CFGs)
    423 //===--------------------------------------------------------------------===//
    424 
    425 // Provide specializations of GraphTraits to be able to treat a
    426 // machine function as a graph of machine basic blocks... these are
    427 // the same as the machine basic block iterators, except that the root
    428 // node is implicitly the first node of the function.
    429 //
    430 template <> struct GraphTraits<MachineFunction*> :
    431   public GraphTraits<MachineBasicBlock*> {
    432   static NodeType *getEntryNode(MachineFunction *F) {
    433     return &F->front();
    434   }
    435 
    436   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
    437   typedef MachineFunction::iterator nodes_iterator;
    438   static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); }
    439   static nodes_iterator nodes_end  (MachineFunction *F) { return F->end(); }
    440 };
    441 template <> struct GraphTraits<const MachineFunction*> :
    442   public GraphTraits<const MachineBasicBlock*> {
    443   static NodeType *getEntryNode(const MachineFunction *F) {
    444     return &F->front();
    445   }
    446 
    447   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
    448   typedef MachineFunction::const_iterator nodes_iterator;
    449   static nodes_iterator nodes_begin(const MachineFunction *F) {
    450     return F->begin();
    451   }
    452   static nodes_iterator nodes_end  (const MachineFunction *F) {
    453     return F->end();
    454   }
    455 };
    456 
    457 
    458 // Provide specializations of GraphTraits to be able to treat a function as a
    459 // graph of basic blocks... and to walk it in inverse order.  Inverse order for
    460 // a function is considered to be when traversing the predecessor edges of a BB
    461 // instead of the successor edges.
    462 //
    463 template <> struct GraphTraits<Inverse<MachineFunction*> > :
    464   public GraphTraits<Inverse<MachineBasicBlock*> > {
    465   static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
    466     return &G.Graph->front();
    467   }
    468 };
    469 template <> struct GraphTraits<Inverse<const MachineFunction*> > :
    470   public GraphTraits<Inverse<const MachineBasicBlock*> > {
    471   static NodeType *getEntryNode(Inverse<const MachineFunction *> G) {
    472     return &G.Graph->front();
    473   }
    474 };
    475 
    476 } // End llvm namespace
    477 
    478 #endif
    479