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