Home | History | Annotate | Download | only in CodeGen
      1 //===-- llvm/CodeGen/MachineBasicBlock.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 the sequence of machine instructions for a basic block.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_CODEGEN_MACHINEBASICBLOCK_H
     15 #define LLVM_CODEGEN_MACHINEBASICBLOCK_H
     16 
     17 #include "llvm/ADT/GraphTraits.h"
     18 #include "llvm/CodeGen/MachineInstr.h"
     19 #include "llvm/Support/DataTypes.h"
     20 #include <functional>
     21 
     22 namespace llvm {
     23 
     24 class Pass;
     25 class BasicBlock;
     26 class MachineFunction;
     27 class MCSymbol;
     28 class SlotIndexes;
     29 class StringRef;
     30 class raw_ostream;
     31 class MachineBranchProbabilityInfo;
     32 
     33 template <>
     34 struct ilist_traits<MachineInstr> : public ilist_default_traits<MachineInstr> {
     35 private:
     36   mutable ilist_half_node<MachineInstr> Sentinel;
     37 
     38   // this is only set by the MachineBasicBlock owning the LiveList
     39   friend class MachineBasicBlock;
     40   MachineBasicBlock* Parent;
     41 
     42 public:
     43   MachineInstr *createSentinel() const {
     44     return static_cast<MachineInstr*>(&Sentinel);
     45   }
     46   void destroySentinel(MachineInstr *) const {}
     47 
     48   MachineInstr *provideInitialHead() const { return createSentinel(); }
     49   MachineInstr *ensureHead(MachineInstr*) const { return createSentinel(); }
     50   static void noteHead(MachineInstr*, MachineInstr*) {}
     51 
     52   void addNodeToList(MachineInstr* N);
     53   void removeNodeFromList(MachineInstr* N);
     54   void transferNodesFromList(ilist_traits &SrcTraits,
     55                              ilist_iterator<MachineInstr> first,
     56                              ilist_iterator<MachineInstr> last);
     57   void deleteNode(MachineInstr *N);
     58 private:
     59   void createNode(const MachineInstr &);
     60 };
     61 
     62 class MachineBasicBlock : public ilist_node<MachineBasicBlock> {
     63   typedef ilist<MachineInstr> Instructions;
     64   Instructions Insts;
     65   const BasicBlock *BB;
     66   int Number;
     67   MachineFunction *xParent;
     68 
     69   /// Predecessors/Successors - Keep track of the predecessor / successor
     70   /// basicblocks.
     71   std::vector<MachineBasicBlock *> Predecessors;
     72   std::vector<MachineBasicBlock *> Successors;
     73 
     74 
     75   /// Weights - Keep track of the weights to the successors. This vector
     76   /// has the same order as Successors, or it is empty if we don't use it
     77   /// (disable optimization).
     78   std::vector<uint32_t> Weights;
     79   typedef std::vector<uint32_t>::iterator weight_iterator;
     80   typedef std::vector<uint32_t>::const_iterator const_weight_iterator;
     81 
     82   /// LiveIns - Keep track of the physical registers that are livein of
     83   /// the basicblock.
     84   std::vector<unsigned> LiveIns;
     85 
     86   /// Alignment - Alignment of the basic block. Zero if the basic block does
     87   /// not need to be aligned.
     88   /// The alignment is specified as log2(bytes).
     89   unsigned Alignment;
     90 
     91   /// IsLandingPad - Indicate that this basic block is entered via an
     92   /// exception handler.
     93   bool IsLandingPad;
     94 
     95   /// AddressTaken - Indicate that this basic block is potentially the
     96   /// target of an indirect branch.
     97   bool AddressTaken;
     98 
     99   // Intrusive list support
    100   MachineBasicBlock() {}
    101 
    102   explicit MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb);
    103 
    104   ~MachineBasicBlock();
    105 
    106   // MachineBasicBlocks are allocated and owned by MachineFunction.
    107   friend class MachineFunction;
    108 
    109 public:
    110   /// getBasicBlock - Return the LLVM basic block that this instance
    111   /// corresponded to originally. Note that this may be NULL if this instance
    112   /// does not correspond directly to an LLVM basic block.
    113   ///
    114   const BasicBlock *getBasicBlock() const { return BB; }
    115 
    116   /// getName - Return the name of the corresponding LLVM basic block, or
    117   /// "(null)".
    118   StringRef getName() const;
    119 
    120   /// getFullName - Return a formatted string to identify this block and its
    121   /// parent function.
    122   std::string getFullName() const;
    123 
    124   /// hasAddressTaken - Test whether this block is potentially the target
    125   /// of an indirect branch.
    126   bool hasAddressTaken() const { return AddressTaken; }
    127 
    128   /// setHasAddressTaken - Set this block to reflect that it potentially
    129   /// is the target of an indirect branch.
    130   void setHasAddressTaken() { AddressTaken = true; }
    131 
    132   /// getParent - Return the MachineFunction containing this basic block.
    133   ///
    134   const MachineFunction *getParent() const { return xParent; }
    135   MachineFunction *getParent() { return xParent; }
    136 
    137 
    138   /// bundle_iterator - MachineBasicBlock iterator that automatically skips over
    139   /// MIs that are inside bundles (i.e. walk top level MIs only).
    140   template<typename Ty, typename IterTy>
    141   class bundle_iterator
    142     : public std::iterator<std::bidirectional_iterator_tag, Ty, ptrdiff_t> {
    143     IterTy MII;
    144 
    145   public:
    146     bundle_iterator(IterTy mii) : MII(mii) {}
    147 
    148     bundle_iterator(Ty &mi) : MII(mi) {
    149       assert(!mi.isBundledWithPred() &&
    150              "It's not legal to initialize bundle_iterator with a bundled MI");
    151     }
    152     bundle_iterator(Ty *mi) : MII(mi) {
    153       assert((!mi || !mi->isBundledWithPred()) &&
    154              "It's not legal to initialize bundle_iterator with a bundled MI");
    155     }
    156     // Template allows conversion from const to nonconst.
    157     template<class OtherTy, class OtherIterTy>
    158     bundle_iterator(const bundle_iterator<OtherTy, OtherIterTy> &I)
    159       : MII(I.getInstrIterator()) {}
    160     bundle_iterator() : MII(0) {}
    161 
    162     Ty &operator*() const { return *MII; }
    163     Ty *operator->() const { return &operator*(); }
    164 
    165     operator Ty*() const { return MII; }
    166 
    167     bool operator==(const bundle_iterator &x) const {
    168       return MII == x.MII;
    169     }
    170     bool operator!=(const bundle_iterator &x) const {
    171       return !operator==(x);
    172     }
    173 
    174     // Increment and decrement operators...
    175     bundle_iterator &operator--() {      // predecrement - Back up
    176       do --MII;
    177       while (MII->isBundledWithPred());
    178       return *this;
    179     }
    180     bundle_iterator &operator++() {      // preincrement - Advance
    181       while (MII->isBundledWithSucc())
    182         ++MII;
    183       ++MII;
    184       return *this;
    185     }
    186     bundle_iterator operator--(int) {    // postdecrement operators...
    187       bundle_iterator tmp = *this;
    188       --*this;
    189       return tmp;
    190     }
    191     bundle_iterator operator++(int) {    // postincrement operators...
    192       bundle_iterator tmp = *this;
    193       ++*this;
    194       return tmp;
    195     }
    196 
    197     IterTy getInstrIterator() const {
    198       return MII;
    199     }
    200   };
    201 
    202   typedef Instructions::iterator                                 instr_iterator;
    203   typedef Instructions::const_iterator                     const_instr_iterator;
    204   typedef std::reverse_iterator<instr_iterator>          reverse_instr_iterator;
    205   typedef
    206   std::reverse_iterator<const_instr_iterator>      const_reverse_instr_iterator;
    207 
    208   typedef
    209   bundle_iterator<MachineInstr,instr_iterator>                         iterator;
    210   typedef
    211   bundle_iterator<const MachineInstr,const_instr_iterator>       const_iterator;
    212   typedef std::reverse_iterator<const_iterator>          const_reverse_iterator;
    213   typedef std::reverse_iterator<iterator>                      reverse_iterator;
    214 
    215 
    216   unsigned size() const { return (unsigned)Insts.size(); }
    217   bool empty() const { return Insts.empty(); }
    218 
    219   MachineInstr& front() { return Insts.front(); }
    220   MachineInstr& back()  { return Insts.back(); }
    221   const MachineInstr& front() const { return Insts.front(); }
    222   const MachineInstr& back()  const { return Insts.back(); }
    223 
    224   instr_iterator                instr_begin()       { return Insts.begin();  }
    225   const_instr_iterator          instr_begin() const { return Insts.begin();  }
    226   instr_iterator                  instr_end()       { return Insts.end();    }
    227   const_instr_iterator            instr_end() const { return Insts.end();    }
    228   reverse_instr_iterator       instr_rbegin()       { return Insts.rbegin(); }
    229   const_reverse_instr_iterator instr_rbegin() const { return Insts.rbegin(); }
    230   reverse_instr_iterator       instr_rend  ()       { return Insts.rend();   }
    231   const_reverse_instr_iterator instr_rend  () const { return Insts.rend();   }
    232 
    233   iterator                begin()       { return instr_begin();  }
    234   const_iterator          begin() const { return instr_begin();  }
    235   iterator                end  ()       { return instr_end();    }
    236   const_iterator          end  () const { return instr_end();    }
    237   reverse_iterator       rbegin()       { return instr_rbegin(); }
    238   const_reverse_iterator rbegin() const { return instr_rbegin(); }
    239   reverse_iterator       rend  ()       { return instr_rend();   }
    240   const_reverse_iterator rend  () const { return instr_rend();   }
    241 
    242 
    243   // Machine-CFG iterators
    244   typedef std::vector<MachineBasicBlock *>::iterator       pred_iterator;
    245   typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
    246   typedef std::vector<MachineBasicBlock *>::iterator       succ_iterator;
    247   typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
    248   typedef std::vector<MachineBasicBlock *>::reverse_iterator
    249                                                          pred_reverse_iterator;
    250   typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
    251                                                    const_pred_reverse_iterator;
    252   typedef std::vector<MachineBasicBlock *>::reverse_iterator
    253                                                          succ_reverse_iterator;
    254   typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
    255                                                    const_succ_reverse_iterator;
    256 
    257   pred_iterator        pred_begin()       { return Predecessors.begin(); }
    258   const_pred_iterator  pred_begin() const { return Predecessors.begin(); }
    259   pred_iterator        pred_end()         { return Predecessors.end();   }
    260   const_pred_iterator  pred_end()   const { return Predecessors.end();   }
    261   pred_reverse_iterator        pred_rbegin()
    262                                           { return Predecessors.rbegin();}
    263   const_pred_reverse_iterator  pred_rbegin() const
    264                                           { return Predecessors.rbegin();}
    265   pred_reverse_iterator        pred_rend()
    266                                           { return Predecessors.rend();  }
    267   const_pred_reverse_iterator  pred_rend()   const
    268                                           { return Predecessors.rend();  }
    269   unsigned             pred_size()  const {
    270     return (unsigned)Predecessors.size();
    271   }
    272   bool                 pred_empty() const { return Predecessors.empty(); }
    273   succ_iterator        succ_begin()       { return Successors.begin();   }
    274   const_succ_iterator  succ_begin() const { return Successors.begin();   }
    275   succ_iterator        succ_end()         { return Successors.end();     }
    276   const_succ_iterator  succ_end()   const { return Successors.end();     }
    277   succ_reverse_iterator        succ_rbegin()
    278                                           { return Successors.rbegin();  }
    279   const_succ_reverse_iterator  succ_rbegin() const
    280                                           { return Successors.rbegin();  }
    281   succ_reverse_iterator        succ_rend()
    282                                           { return Successors.rend();    }
    283   const_succ_reverse_iterator  succ_rend()   const
    284                                           { return Successors.rend();    }
    285   unsigned             succ_size()  const {
    286     return (unsigned)Successors.size();
    287   }
    288   bool                 succ_empty() const { return Successors.empty();   }
    289 
    290   // LiveIn management methods.
    291 
    292   /// addLiveIn - Add the specified register as a live in.  Note that it
    293   /// is an error to add the same register to the same set more than once.
    294   void addLiveIn(unsigned Reg)  { LiveIns.push_back(Reg); }
    295 
    296   /// removeLiveIn - Remove the specified register from the live in set.
    297   ///
    298   void removeLiveIn(unsigned Reg);
    299 
    300   /// isLiveIn - Return true if the specified register is in the live in set.
    301   ///
    302   bool isLiveIn(unsigned Reg) const;
    303 
    304   // Iteration support for live in sets.  These sets are kept in sorted
    305   // order by their register number.
    306   typedef std::vector<unsigned>::const_iterator livein_iterator;
    307   livein_iterator livein_begin() const { return LiveIns.begin(); }
    308   livein_iterator livein_end()   const { return LiveIns.end(); }
    309   bool            livein_empty() const { return LiveIns.empty(); }
    310 
    311   /// getAlignment - Return alignment of the basic block.
    312   /// The alignment is specified as log2(bytes).
    313   ///
    314   unsigned getAlignment() const { return Alignment; }
    315 
    316   /// setAlignment - Set alignment of the basic block.
    317   /// The alignment is specified as log2(bytes).
    318   ///
    319   void setAlignment(unsigned Align) { Alignment = Align; }
    320 
    321   /// isLandingPad - Returns true if the block is a landing pad. That is
    322   /// this basic block is entered via an exception handler.
    323   bool isLandingPad() const { return IsLandingPad; }
    324 
    325   /// setIsLandingPad - Indicates the block is a landing pad.  That is
    326   /// this basic block is entered via an exception handler.
    327   void setIsLandingPad(bool V = true) { IsLandingPad = V; }
    328 
    329   /// getLandingPadSuccessor - If this block has a successor that is a landing
    330   /// pad, return it. Otherwise return NULL.
    331   const MachineBasicBlock *getLandingPadSuccessor() const;
    332 
    333   // Code Layout methods.
    334 
    335   /// moveBefore/moveAfter - move 'this' block before or after the specified
    336   /// block.  This only moves the block, it does not modify the CFG or adjust
    337   /// potential fall-throughs at the end of the block.
    338   void moveBefore(MachineBasicBlock *NewAfter);
    339   void moveAfter(MachineBasicBlock *NewBefore);
    340 
    341   /// updateTerminator - Update the terminator instructions in block to account
    342   /// for changes to the layout. If the block previously used a fallthrough,
    343   /// it may now need a branch, and if it previously used branching it may now
    344   /// be able to use a fallthrough.
    345   void updateTerminator();
    346 
    347   // Machine-CFG mutators
    348 
    349   /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
    350   /// The Predecessors list of succ is automatically updated. WEIGHT
    351   /// parameter is stored in Weights list and it may be used by
    352   /// MachineBranchProbabilityInfo analysis to calculate branch probability.
    353   ///
    354   /// Note that duplicate Machine CFG edges are not allowed.
    355   ///
    356   void addSuccessor(MachineBasicBlock *succ, uint32_t weight = 0);
    357 
    358   /// removeSuccessor - Remove successor from the successors list of this
    359   /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
    360   ///
    361   void removeSuccessor(MachineBasicBlock *succ);
    362 
    363   /// removeSuccessor - Remove specified successor from the successors list of
    364   /// this MachineBasicBlock. The Predecessors list of succ is automatically
    365   /// updated.  Return the iterator to the element after the one removed.
    366   ///
    367   succ_iterator removeSuccessor(succ_iterator I);
    368 
    369   /// replaceSuccessor - Replace successor OLD with NEW and update weight info.
    370   ///
    371   void replaceSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New);
    372 
    373 
    374   /// transferSuccessors - Transfers all the successors from MBB to this
    375   /// machine basic block (i.e., copies all the successors fromMBB and
    376   /// remove all the successors from fromMBB).
    377   void transferSuccessors(MachineBasicBlock *fromMBB);
    378 
    379   /// transferSuccessorsAndUpdatePHIs - Transfers all the successors, as
    380   /// in transferSuccessors, and update PHI operands in the successor blocks
    381   /// which refer to fromMBB to refer to this.
    382   void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *fromMBB);
    383 
    384   /// isPredecessor - Return true if the specified MBB is a predecessor of this
    385   /// block.
    386   bool isPredecessor(const MachineBasicBlock *MBB) const;
    387 
    388   /// isSuccessor - Return true if the specified MBB is a successor of this
    389   /// block.
    390   bool isSuccessor(const MachineBasicBlock *MBB) const;
    391 
    392   /// isLayoutSuccessor - Return true if the specified MBB will be emitted
    393   /// immediately after this block, such that if this block exits by
    394   /// falling through, control will transfer to the specified MBB. Note
    395   /// that MBB need not be a successor at all, for example if this block
    396   /// ends with an unconditional branch to some other block.
    397   bool isLayoutSuccessor(const MachineBasicBlock *MBB) const;
    398 
    399   /// canFallThrough - Return true if the block can implicitly transfer
    400   /// control to the block after it by falling off the end of it.  This should
    401   /// return false if it can reach the block after it, but it uses an explicit
    402   /// branch to do so (e.g., a table jump).  True is a conservative answer.
    403   bool canFallThrough();
    404 
    405   /// Returns a pointer to the first instructon in this block that is not a
    406   /// PHINode instruction. When adding instruction to the beginning of the
    407   /// basic block, they should be added before the returned value, not before
    408   /// the first instruction, which might be PHI.
    409   /// Returns end() is there's no non-PHI instruction.
    410   iterator getFirstNonPHI();
    411 
    412   /// SkipPHIsAndLabels - Return the first instruction in MBB after I that is
    413   /// not a PHI or a label. This is the correct point to insert copies at the
    414   /// beginning of a basic block.
    415   iterator SkipPHIsAndLabels(iterator I);
    416 
    417   /// getFirstTerminator - returns an iterator to the first terminator
    418   /// instruction of this basic block. If a terminator does not exist,
    419   /// it returns end()
    420   iterator getFirstTerminator();
    421   const_iterator getFirstTerminator() const;
    422 
    423   /// getFirstInstrTerminator - Same getFirstTerminator but it ignores bundles
    424   /// and return an instr_iterator instead.
    425   instr_iterator getFirstInstrTerminator();
    426 
    427   /// getLastNonDebugInstr - returns an iterator to the last non-debug
    428   /// instruction in the basic block, or end()
    429   iterator getLastNonDebugInstr();
    430   const_iterator getLastNonDebugInstr() const;
    431 
    432   /// SplitCriticalEdge - Split the critical edge from this block to the
    433   /// given successor block, and return the newly created block, or null
    434   /// if splitting is not possible.
    435   ///
    436   /// This function updates LiveVariables, MachineDominatorTree, and
    437   /// MachineLoopInfo, as applicable.
    438   MachineBasicBlock *SplitCriticalEdge(MachineBasicBlock *Succ, Pass *P);
    439 
    440   void pop_front() { Insts.pop_front(); }
    441   void pop_back() { Insts.pop_back(); }
    442   void push_back(MachineInstr *MI) { Insts.push_back(MI); }
    443 
    444   /// Insert MI into the instruction list before I, possibly inside a bundle.
    445   ///
    446   /// If the insertion point is inside a bundle, MI will be added to the bundle,
    447   /// otherwise MI will not be added to any bundle. That means this function
    448   /// alone can't be used to prepend or append instructions to bundles. See
    449   /// MIBundleBuilder::insert() for a more reliable way of doing that.
    450   instr_iterator insert(instr_iterator I, MachineInstr *M);
    451 
    452   /// Insert a range of instructions into the instruction list before I.
    453   template<typename IT>
    454   void insert(iterator I, IT S, IT E) {
    455     Insts.insert(I.getInstrIterator(), S, E);
    456   }
    457 
    458   /// Insert MI into the instruction list before I.
    459   iterator insert(iterator I, MachineInstr *MI) {
    460     assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() &&
    461            "Cannot insert instruction with bundle flags");
    462     return Insts.insert(I.getInstrIterator(), MI);
    463   }
    464 
    465   /// Insert MI into the instruction list after I.
    466   iterator insertAfter(iterator I, MachineInstr *MI) {
    467     assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() &&
    468            "Cannot insert instruction with bundle flags");
    469     return Insts.insertAfter(I.getInstrIterator(), MI);
    470   }
    471 
    472   /// Remove an instruction from the instruction list and delete it.
    473   ///
    474   /// If the instruction is part of a bundle, the other instructions in the
    475   /// bundle will still be bundled after removing the single instruction.
    476   instr_iterator erase(instr_iterator I);
    477 
    478   /// Remove an instruction from the instruction list and delete it.
    479   ///
    480   /// If the instruction is part of a bundle, the other instructions in the
    481   /// bundle will still be bundled after removing the single instruction.
    482   instr_iterator erase_instr(MachineInstr *I) {
    483     return erase(instr_iterator(I));
    484   }
    485 
    486   /// Remove a range of instructions from the instruction list and delete them.
    487   iterator erase(iterator I, iterator E) {
    488     return Insts.erase(I.getInstrIterator(), E.getInstrIterator());
    489   }
    490 
    491   /// Remove an instruction or bundle from the instruction list and delete it.
    492   ///
    493   /// If I points to a bundle of instructions, they are all erased.
    494   iterator erase(iterator I) {
    495     return erase(I, llvm::next(I));
    496   }
    497 
    498   /// Remove an instruction from the instruction list and delete it.
    499   ///
    500   /// If I is the head of a bundle of instructions, the whole bundle will be
    501   /// erased.
    502   iterator erase(MachineInstr *I) {
    503     return erase(iterator(I));
    504   }
    505 
    506   /// Remove the unbundled instruction from the instruction list without
    507   /// deleting it.
    508   ///
    509   /// This function can not be used to remove bundled instructions, use
    510   /// remove_instr to remove individual instructions from a bundle.
    511   MachineInstr *remove(MachineInstr *I) {
    512     assert(!I->isBundled() && "Cannot remove bundled instructions");
    513     return Insts.remove(I);
    514   }
    515 
    516   /// Remove the possibly bundled instruction from the instruction list
    517   /// without deleting it.
    518   ///
    519   /// If the instruction is part of a bundle, the other instructions in the
    520   /// bundle will still be bundled after removing the single instruction.
    521   MachineInstr *remove_instr(MachineInstr *I);
    522 
    523   void clear() {
    524     Insts.clear();
    525   }
    526 
    527   /// Take an instruction from MBB 'Other' at the position From, and insert it
    528   /// into this MBB right before 'Where'.
    529   ///
    530   /// If From points to a bundle of instructions, the whole bundle is moved.
    531   void splice(iterator Where, MachineBasicBlock *Other, iterator From) {
    532     // The range splice() doesn't allow noop moves, but this one does.
    533     if (Where != From)
    534       splice(Where, Other, From, llvm::next(From));
    535   }
    536 
    537   /// Take a block of instructions from MBB 'Other' in the range [From, To),
    538   /// and insert them into this MBB right before 'Where'.
    539   ///
    540   /// The instruction at 'Where' must not be included in the range of
    541   /// instructions to move.
    542   void splice(iterator Where, MachineBasicBlock *Other,
    543               iterator From, iterator To) {
    544     Insts.splice(Where.getInstrIterator(), Other->Insts,
    545                  From.getInstrIterator(), To.getInstrIterator());
    546   }
    547 
    548   /// removeFromParent - This method unlinks 'this' from the containing
    549   /// function, and returns it, but does not delete it.
    550   MachineBasicBlock *removeFromParent();
    551 
    552   /// eraseFromParent - This method unlinks 'this' from the containing
    553   /// function and deletes it.
    554   void eraseFromParent();
    555 
    556   /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
    557   /// 'Old', change the code and CFG so that it branches to 'New' instead.
    558   void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
    559 
    560   /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in
    561   /// the CFG to be inserted.  If we have proven that MBB can only branch to
    562   /// DestA and DestB, remove any other MBB successors from the CFG. DestA and
    563   /// DestB can be null. Besides DestA and DestB, retain other edges leading
    564   /// to LandingPads (currently there can be only one; we don't check or require
    565   /// that here). Note it is possible that DestA and/or DestB are LandingPads.
    566   bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
    567                             MachineBasicBlock *DestB,
    568                             bool isCond);
    569 
    570   /// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping
    571   /// any DBG_VALUE instructions.  Return UnknownLoc if there is none.
    572   DebugLoc findDebugLoc(instr_iterator MBBI);
    573   DebugLoc findDebugLoc(iterator MBBI) {
    574     return findDebugLoc(MBBI.getInstrIterator());
    575   }
    576 
    577   /// Possible outcome of a register liveness query to computeRegisterLiveness()
    578   enum LivenessQueryResult {
    579     LQR_Live,            ///< Register is known to be live.
    580     LQR_OverlappingLive, ///< Register itself is not live, but some overlapping
    581                          ///< register is.
    582     LQR_Dead,            ///< Register is known to be dead.
    583     LQR_Unknown          ///< Register liveness not decidable from local
    584                          ///< neighborhood.
    585   };
    586 
    587   /// computeRegisterLiveness - Return whether (physical) register \c Reg
    588   /// has been <def>ined and not <kill>ed as of just before \c MI.
    589   ///
    590   /// Search is localised to a neighborhood of
    591   /// \c Neighborhood instructions before (searching for defs or kills) and
    592   /// Neighborhood instructions after (searching just for defs) MI.
    593   ///
    594   /// \c Reg must be a physical register.
    595   LivenessQueryResult computeRegisterLiveness(const TargetRegisterInfo *TRI,
    596                                               unsigned Reg, MachineInstr *MI,
    597                                               unsigned Neighborhood=10);
    598 
    599   // Debugging methods.
    600   void dump() const;
    601   void print(raw_ostream &OS, SlotIndexes* = 0) const;
    602 
    603   /// getNumber - MachineBasicBlocks are uniquely numbered at the function
    604   /// level, unless they're not in a MachineFunction yet, in which case this
    605   /// will return -1.
    606   ///
    607   int getNumber() const { return Number; }
    608   void setNumber(int N) { Number = N; }
    609 
    610   /// getSymbol - Return the MCSymbol for this basic block.
    611   ///
    612   MCSymbol *getSymbol() const;
    613 
    614 
    615 private:
    616   /// getWeightIterator - Return weight iterator corresponding to the I
    617   /// successor iterator.
    618   weight_iterator getWeightIterator(succ_iterator I);
    619   const_weight_iterator getWeightIterator(const_succ_iterator I) const;
    620 
    621   friend class MachineBranchProbabilityInfo;
    622 
    623   /// getSuccWeight - Return weight of the edge from this block to MBB. This
    624   /// method should NOT be called directly, but by using getEdgeWeight method
    625   /// from MachineBranchProbabilityInfo class.
    626   uint32_t getSuccWeight(const_succ_iterator Succ) const;
    627 
    628 
    629   // Methods used to maintain doubly linked list of blocks...
    630   friend struct ilist_traits<MachineBasicBlock>;
    631 
    632   // Machine-CFG mutators
    633 
    634   /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
    635   /// Don't do this unless you know what you're doing, because it doesn't
    636   /// update pred's successors list. Use pred->addSuccessor instead.
    637   ///
    638   void addPredecessor(MachineBasicBlock *pred);
    639 
    640   /// removePredecessor - Remove pred as a predecessor of this
    641   /// MachineBasicBlock. Don't do this unless you know what you're
    642   /// doing, because it doesn't update pred's successors list. Use
    643   /// pred->removeSuccessor instead.
    644   ///
    645   void removePredecessor(MachineBasicBlock *pred);
    646 };
    647 
    648 raw_ostream& operator<<(raw_ostream &OS, const MachineBasicBlock &MBB);
    649 
    650 void WriteAsOperand(raw_ostream &, const MachineBasicBlock*, bool t);
    651 
    652 // This is useful when building IndexedMaps keyed on basic block pointers.
    653 struct MBB2NumberFunctor :
    654   public std::unary_function<const MachineBasicBlock*, unsigned> {
    655   unsigned operator()(const MachineBasicBlock *MBB) const {
    656     return MBB->getNumber();
    657   }
    658 };
    659 
    660 //===--------------------------------------------------------------------===//
    661 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
    662 //===--------------------------------------------------------------------===//
    663 
    664 // Provide specializations of GraphTraits to be able to treat a
    665 // MachineFunction as a graph of MachineBasicBlocks...
    666 //
    667 
    668 template <> struct GraphTraits<MachineBasicBlock *> {
    669   typedef MachineBasicBlock NodeType;
    670   typedef MachineBasicBlock::succ_iterator ChildIteratorType;
    671 
    672   static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
    673   static inline ChildIteratorType child_begin(NodeType *N) {
    674     return N->succ_begin();
    675   }
    676   static inline ChildIteratorType child_end(NodeType *N) {
    677     return N->succ_end();
    678   }
    679 };
    680 
    681 template <> struct GraphTraits<const MachineBasicBlock *> {
    682   typedef const MachineBasicBlock NodeType;
    683   typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
    684 
    685   static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
    686   static inline ChildIteratorType child_begin(NodeType *N) {
    687     return N->succ_begin();
    688   }
    689   static inline ChildIteratorType child_end(NodeType *N) {
    690     return N->succ_end();
    691   }
    692 };
    693 
    694 // Provide specializations of GraphTraits to be able to treat a
    695 // MachineFunction as a graph of MachineBasicBlocks... and to walk it
    696 // in inverse order.  Inverse order for a function is considered
    697 // to be when traversing the predecessor edges of a MBB
    698 // instead of the successor edges.
    699 //
    700 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
    701   typedef MachineBasicBlock NodeType;
    702   typedef MachineBasicBlock::pred_iterator ChildIteratorType;
    703   static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
    704     return G.Graph;
    705   }
    706   static inline ChildIteratorType child_begin(NodeType *N) {
    707     return N->pred_begin();
    708   }
    709   static inline ChildIteratorType child_end(NodeType *N) {
    710     return N->pred_end();
    711   }
    712 };
    713 
    714 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
    715   typedef const MachineBasicBlock NodeType;
    716   typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
    717   static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
    718     return G.Graph;
    719   }
    720   static inline ChildIteratorType child_begin(NodeType *N) {
    721     return N->pred_begin();
    722   }
    723   static inline ChildIteratorType child_end(NodeType *N) {
    724     return N->pred_end();
    725   }
    726 };
    727 
    728 } // End llvm namespace
    729 
    730 #endif
    731