LLVM  mainline
MachineBasicBlock.h
Go to the documentation of this file.
00001 //===-- llvm/CodeGen/MachineBasicBlock.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 the sequence of machine instructions for a basic block.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_CODEGEN_MACHINEBASICBLOCK_H
00015 #define LLVM_CODEGEN_MACHINEBASICBLOCK_H
00016 
00017 #include "llvm/ADT/GraphTraits.h"
00018 #include "llvm/CodeGen/MachineInstr.h"
00019 #include "llvm/Support/DataTypes.h"
00020 #include <functional>
00021 
00022 namespace llvm {
00023 
00024 class Pass;
00025 class BasicBlock;
00026 class MachineFunction;
00027 class MCSymbol;
00028 class SlotIndexes;
00029 class StringRef;
00030 class raw_ostream;
00031 class MachineBranchProbabilityInfo;
00032 
00033 template <>
00034 struct ilist_traits<MachineInstr> : public ilist_default_traits<MachineInstr> {
00035 private:
00036   mutable ilist_half_node<MachineInstr> Sentinel;
00037 
00038   // this is only set by the MachineBasicBlock owning the LiveList
00039   friend class MachineBasicBlock;
00040   MachineBasicBlock* Parent;
00041 
00042 public:
00043   MachineInstr *createSentinel() const {
00044     return static_cast<MachineInstr*>(&Sentinel);
00045   }
00046   void destroySentinel(MachineInstr *) const {}
00047 
00048   MachineInstr *provideInitialHead() const { return createSentinel(); }
00049   MachineInstr *ensureHead(MachineInstr*) const { return createSentinel(); }
00050   static void noteHead(MachineInstr*, MachineInstr*) {}
00051 
00052   void addNodeToList(MachineInstr* N);
00053   void removeNodeFromList(MachineInstr* N);
00054   void transferNodesFromList(ilist_traits &SrcTraits,
00055                              ilist_iterator<MachineInstr> first,
00056                              ilist_iterator<MachineInstr> last);
00057   void deleteNode(MachineInstr *N);
00058 private:
00059   void createNode(const MachineInstr &);
00060 };
00061 
00062 class MachineBasicBlock : public ilist_node<MachineBasicBlock> {
00063   typedef ilist<MachineInstr> Instructions;
00064   Instructions Insts;
00065   const BasicBlock *BB;
00066   int Number;
00067   MachineFunction *xParent;
00068 
00069   /// Predecessors/Successors - Keep track of the predecessor / successor
00070   /// basicblocks.
00071   std::vector<MachineBasicBlock *> Predecessors;
00072   std::vector<MachineBasicBlock *> Successors;
00073 
00074   /// Weights - Keep track of the weights to the successors. This vector
00075   /// has the same order as Successors, or it is empty if we don't use it
00076   /// (disable optimization).
00077   std::vector<uint32_t> Weights;
00078   typedef std::vector<uint32_t>::iterator weight_iterator;
00079   typedef std::vector<uint32_t>::const_iterator const_weight_iterator;
00080 
00081   /// LiveIns - Keep track of the physical registers that are livein of
00082   /// the basicblock.
00083   std::vector<unsigned> LiveIns;
00084 
00085   /// Alignment - Alignment of the basic block. Zero if the basic block does
00086   /// not need to be aligned.
00087   /// The alignment is specified as log2(bytes).
00088   unsigned Alignment;
00089 
00090   /// IsLandingPad - Indicate that this basic block is entered via an
00091   /// exception handler.
00092   bool IsLandingPad;
00093 
00094   /// AddressTaken - Indicate that this basic block is potentially the
00095   /// target of an indirect branch.
00096   bool AddressTaken;
00097 
00098   /// \brief since getSymbol is a relatively heavy-weight operation, the symbol
00099   /// is only computed once and is cached.
00100   mutable MCSymbol *CachedMCSymbol;
00101 
00102   // Intrusive list support
00103   MachineBasicBlock() {}
00104 
00105   explicit MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb);
00106 
00107   ~MachineBasicBlock();
00108 
00109   // MachineBasicBlocks are allocated and owned by MachineFunction.
00110   friend class MachineFunction;
00111 
00112 public:
00113   /// getBasicBlock - Return the LLVM basic block that this instance
00114   /// corresponded to originally. Note that this may be NULL if this instance
00115   /// does not correspond directly to an LLVM basic block.
00116   ///
00117   const BasicBlock *getBasicBlock() const { return BB; }
00118 
00119   /// getName - Return the name of the corresponding LLVM basic block, or
00120   /// "(null)".
00121   StringRef getName() const;
00122 
00123   /// getFullName - Return a formatted string to identify this block and its
00124   /// parent function.
00125   std::string getFullName() const;
00126 
00127   /// hasAddressTaken - Test whether this block is potentially the target
00128   /// of an indirect branch.
00129   bool hasAddressTaken() const { return AddressTaken; }
00130 
00131   /// setHasAddressTaken - Set this block to reflect that it potentially
00132   /// is the target of an indirect branch.
00133   void setHasAddressTaken() { AddressTaken = true; }
00134 
00135   /// getParent - Return the MachineFunction containing this basic block.
00136   ///
00137   const MachineFunction *getParent() const { return xParent; }
00138   MachineFunction *getParent() { return xParent; }
00139 
00140 
00141   /// bundle_iterator - MachineBasicBlock iterator that automatically skips over
00142   /// MIs that are inside bundles (i.e. walk top level MIs only).
00143   template<typename Ty, typename IterTy>
00144   class bundle_iterator
00145     : public std::iterator<std::bidirectional_iterator_tag, Ty, ptrdiff_t> {
00146     IterTy MII;
00147 
00148   public:
00149     bundle_iterator(IterTy mii) : MII(mii) {}
00150 
00151     bundle_iterator(Ty &mi) : MII(mi) {
00152       assert(!mi.isBundledWithPred() &&
00153              "It's not legal to initialize bundle_iterator with a bundled MI");
00154     }
00155     bundle_iterator(Ty *mi) : MII(mi) {
00156       assert((!mi || !mi->isBundledWithPred()) &&
00157              "It's not legal to initialize bundle_iterator with a bundled MI");
00158     }
00159     // Template allows conversion from const to nonconst.
00160     template<class OtherTy, class OtherIterTy>
00161     bundle_iterator(const bundle_iterator<OtherTy, OtherIterTy> &I)
00162       : MII(I.getInstrIterator()) {}
00163     bundle_iterator() : MII(nullptr) {}
00164 
00165     Ty &operator*() const { return *MII; }
00166     Ty *operator->() const { return &operator*(); }
00167 
00168     operator Ty*() const { return MII; }
00169 
00170     bool operator==(const bundle_iterator &x) const {
00171       return MII == x.MII;
00172     }
00173     bool operator!=(const bundle_iterator &x) const {
00174       return !operator==(x);
00175     }
00176 
00177     // Increment and decrement operators...
00178     bundle_iterator &operator--() {      // predecrement - Back up
00179       do --MII;
00180       while (MII->isBundledWithPred());
00181       return *this;
00182     }
00183     bundle_iterator &operator++() {      // preincrement - Advance
00184       while (MII->isBundledWithSucc())
00185         ++MII;
00186       ++MII;
00187       return *this;
00188     }
00189     bundle_iterator operator--(int) {    // postdecrement operators...
00190       bundle_iterator tmp = *this;
00191       --*this;
00192       return tmp;
00193     }
00194     bundle_iterator operator++(int) {    // postincrement operators...
00195       bundle_iterator tmp = *this;
00196       ++*this;
00197       return tmp;
00198     }
00199 
00200     IterTy getInstrIterator() const {
00201       return MII;
00202     }
00203   };
00204 
00205   typedef Instructions::iterator                                 instr_iterator;
00206   typedef Instructions::const_iterator                     const_instr_iterator;
00207   typedef std::reverse_iterator<instr_iterator>          reverse_instr_iterator;
00208   typedef
00209   std::reverse_iterator<const_instr_iterator>      const_reverse_instr_iterator;
00210 
00211   typedef
00212   bundle_iterator<MachineInstr,instr_iterator>                         iterator;
00213   typedef
00214   bundle_iterator<const MachineInstr,const_instr_iterator>       const_iterator;
00215   typedef std::reverse_iterator<const_iterator>          const_reverse_iterator;
00216   typedef std::reverse_iterator<iterator>                      reverse_iterator;
00217 
00218 
00219   unsigned size() const { return (unsigned)Insts.size(); }
00220   bool empty() const { return Insts.empty(); }
00221 
00222   MachineInstr       &instr_front()       { return Insts.front(); }
00223   MachineInstr       &instr_back()        { return Insts.back();  }
00224   const MachineInstr &instr_front() const { return Insts.front(); }
00225   const MachineInstr &instr_back()  const { return Insts.back();  }
00226 
00227   MachineInstr       &front()             { return Insts.front(); }
00228   MachineInstr       &back()              { return *--end();      }
00229   const MachineInstr &front()       const { return Insts.front(); }
00230   const MachineInstr &back()        const { return *--end();      }
00231 
00232   instr_iterator                instr_begin()       { return Insts.begin();  }
00233   const_instr_iterator          instr_begin() const { return Insts.begin();  }
00234   instr_iterator                  instr_end()       { return Insts.end();    }
00235   const_instr_iterator            instr_end() const { return Insts.end();    }
00236   reverse_instr_iterator       instr_rbegin()       { return Insts.rbegin(); }
00237   const_reverse_instr_iterator instr_rbegin() const { return Insts.rbegin(); }
00238   reverse_instr_iterator       instr_rend  ()       { return Insts.rend();   }
00239   const_reverse_instr_iterator instr_rend  () const { return Insts.rend();   }
00240 
00241   iterator                begin()       { return instr_begin();  }
00242   const_iterator          begin() const { return instr_begin();  }
00243   iterator                end  ()       { return instr_end();    }
00244   const_iterator          end  () const { return instr_end();    }
00245   reverse_iterator       rbegin()       { return instr_rbegin(); }
00246   const_reverse_iterator rbegin() const { return instr_rbegin(); }
00247   reverse_iterator       rend  ()       { return instr_rend();   }
00248   const_reverse_iterator rend  () const { return instr_rend();   }
00249 
00250   inline iterator_range<iterator> terminators() {
00251     return iterator_range<iterator>(getFirstTerminator(), end());
00252   }
00253   inline iterator_range<const_iterator> terminators() const {
00254     return iterator_range<const_iterator>(getFirstTerminator(), end());
00255   }
00256 
00257   // Machine-CFG iterators
00258   typedef std::vector<MachineBasicBlock *>::iterator       pred_iterator;
00259   typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
00260   typedef std::vector<MachineBasicBlock *>::iterator       succ_iterator;
00261   typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
00262   typedef std::vector<MachineBasicBlock *>::reverse_iterator
00263                                                          pred_reverse_iterator;
00264   typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
00265                                                    const_pred_reverse_iterator;
00266   typedef std::vector<MachineBasicBlock *>::reverse_iterator
00267                                                          succ_reverse_iterator;
00268   typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
00269                                                    const_succ_reverse_iterator;
00270   pred_iterator        pred_begin()       { return Predecessors.begin(); }
00271   const_pred_iterator  pred_begin() const { return Predecessors.begin(); }
00272   pred_iterator        pred_end()         { return Predecessors.end();   }
00273   const_pred_iterator  pred_end()   const { return Predecessors.end();   }
00274   pred_reverse_iterator        pred_rbegin()
00275                                           { return Predecessors.rbegin();}
00276   const_pred_reverse_iterator  pred_rbegin() const
00277                                           { return Predecessors.rbegin();}
00278   pred_reverse_iterator        pred_rend()
00279                                           { return Predecessors.rend();  }
00280   const_pred_reverse_iterator  pred_rend()   const
00281                                           { return Predecessors.rend();  }
00282   unsigned             pred_size()  const {
00283     return (unsigned)Predecessors.size();
00284   }
00285   bool                 pred_empty() const { return Predecessors.empty(); }
00286   succ_iterator        succ_begin()       { return Successors.begin();   }
00287   const_succ_iterator  succ_begin() const { return Successors.begin();   }
00288   succ_iterator        succ_end()         { return Successors.end();     }
00289   const_succ_iterator  succ_end()   const { return Successors.end();     }
00290   succ_reverse_iterator        succ_rbegin()
00291                                           { return Successors.rbegin();  }
00292   const_succ_reverse_iterator  succ_rbegin() const
00293                                           { return Successors.rbegin();  }
00294   succ_reverse_iterator        succ_rend()
00295                                           { return Successors.rend();    }
00296   const_succ_reverse_iterator  succ_rend()   const
00297                                           { return Successors.rend();    }
00298   unsigned             succ_size()  const {
00299     return (unsigned)Successors.size();
00300   }
00301   bool                 succ_empty() const { return Successors.empty();   }
00302 
00303   inline iterator_range<pred_iterator> predecessors() {
00304     return iterator_range<pred_iterator>(pred_begin(), pred_end());
00305   }
00306   inline iterator_range<const_pred_iterator> predecessors() const {
00307     return iterator_range<const_pred_iterator>(pred_begin(), pred_end());
00308   }
00309   inline iterator_range<succ_iterator> successors() {
00310     return iterator_range<succ_iterator>(succ_begin(), succ_end());
00311   }
00312   inline iterator_range<const_succ_iterator> successors() const {
00313     return iterator_range<const_succ_iterator>(succ_begin(), succ_end());
00314   }
00315 
00316   // LiveIn management methods.
00317 
00318   /// Adds the specified register as a live in. Note that it is an error to add
00319   /// the same register to the same set more than once unless the intention is
00320   /// to call sortUniqueLiveIns after all registers are added.
00321   void addLiveIn(unsigned Reg) { LiveIns.push_back(Reg); }
00322 
00323   /// Sorts and uniques the LiveIns vector. It can be significantly faster to do
00324   /// this than repeatedly calling isLiveIn before calling addLiveIn for every
00325   /// LiveIn insertion.
00326   void sortUniqueLiveIns() {
00327     std::sort(LiveIns.begin(), LiveIns.end());
00328     LiveIns.erase(std::unique(LiveIns.begin(), LiveIns.end()), LiveIns.end());
00329   }
00330 
00331   /// Add PhysReg as live in to this block, and ensure that there is a copy of
00332   /// PhysReg to a virtual register of class RC. Return the virtual register
00333   /// that is a copy of the live in PhysReg.
00334   unsigned addLiveIn(unsigned PhysReg, const TargetRegisterClass *RC);
00335 
00336   /// removeLiveIn - Remove the specified register from the live in set.
00337   ///
00338   void removeLiveIn(unsigned Reg);
00339 
00340   /// isLiveIn - Return true if the specified register is in the live in set.
00341   ///
00342   bool isLiveIn(unsigned Reg) const;
00343 
00344   // Iteration support for live in sets.  These sets are kept in sorted
00345   // order by their register number.
00346   typedef std::vector<unsigned>::const_iterator livein_iterator;
00347   livein_iterator livein_begin() const { return LiveIns.begin(); }
00348   livein_iterator livein_end()   const { return LiveIns.end(); }
00349   bool            livein_empty() const { return LiveIns.empty(); }
00350 
00351   /// getAlignment - Return alignment of the basic block.
00352   /// The alignment is specified as log2(bytes).
00353   ///
00354   unsigned getAlignment() const { return Alignment; }
00355 
00356   /// setAlignment - Set alignment of the basic block.
00357   /// The alignment is specified as log2(bytes).
00358   ///
00359   void setAlignment(unsigned Align) { Alignment = Align; }
00360 
00361   /// isLandingPad - Returns true if the block is a landing pad. That is
00362   /// this basic block is entered via an exception handler.
00363   bool isLandingPad() const { return IsLandingPad; }
00364 
00365   /// setIsLandingPad - Indicates the block is a landing pad.  That is
00366   /// this basic block is entered via an exception handler.
00367   void setIsLandingPad(bool V = true) { IsLandingPad = V; }
00368 
00369   /// getLandingPadSuccessor - If this block has a successor that is a landing
00370   /// pad, return it. Otherwise return NULL.
00371   const MachineBasicBlock *getLandingPadSuccessor() const;
00372 
00373   // Code Layout methods.
00374 
00375   /// moveBefore/moveAfter - move 'this' block before or after the specified
00376   /// block.  This only moves the block, it does not modify the CFG or adjust
00377   /// potential fall-throughs at the end of the block.
00378   void moveBefore(MachineBasicBlock *NewAfter);
00379   void moveAfter(MachineBasicBlock *NewBefore);
00380 
00381   /// updateTerminator - Update the terminator instructions in block to account
00382   /// for changes to the layout. If the block previously used a fallthrough,
00383   /// it may now need a branch, and if it previously used branching it may now
00384   /// be able to use a fallthrough.
00385   void updateTerminator();
00386 
00387   // Machine-CFG mutators
00388 
00389   /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
00390   /// The Predecessors list of succ is automatically updated. WEIGHT
00391   /// parameter is stored in Weights list and it may be used by
00392   /// MachineBranchProbabilityInfo analysis to calculate branch probability.
00393   ///
00394   /// Note that duplicate Machine CFG edges are not allowed.
00395   ///
00396   void addSuccessor(MachineBasicBlock *succ, uint32_t weight = 0);
00397 
00398   /// Set successor weight of a given iterator.
00399   void setSuccWeight(succ_iterator I, uint32_t weight);
00400 
00401   /// removeSuccessor - Remove successor from the successors list of this
00402   /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
00403   ///
00404   void removeSuccessor(MachineBasicBlock *succ);
00405 
00406   /// removeSuccessor - Remove specified successor from the successors list of
00407   /// this MachineBasicBlock. The Predecessors list of succ is automatically
00408   /// updated.  Return the iterator to the element after the one removed.
00409   ///
00410   succ_iterator removeSuccessor(succ_iterator I);
00411 
00412   /// replaceSuccessor - Replace successor OLD with NEW and update weight info.
00413   ///
00414   void replaceSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New);
00415 
00416 
00417   /// transferSuccessors - Transfers all the successors from MBB to this
00418   /// machine basic block (i.e., copies all the successors fromMBB and
00419   /// remove all the successors from fromMBB).
00420   void transferSuccessors(MachineBasicBlock *fromMBB);
00421 
00422   /// transferSuccessorsAndUpdatePHIs - Transfers all the successors, as
00423   /// in transferSuccessors, and update PHI operands in the successor blocks
00424   /// which refer to fromMBB to refer to this.
00425   void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *fromMBB);
00426 
00427   /// isPredecessor - Return true if the specified MBB is a predecessor of this
00428   /// block.
00429   bool isPredecessor(const MachineBasicBlock *MBB) const;
00430 
00431   /// isSuccessor - Return true if the specified MBB is a successor of this
00432   /// block.
00433   bool isSuccessor(const MachineBasicBlock *MBB) const;
00434 
00435   /// isLayoutSuccessor - Return true if the specified MBB will be emitted
00436   /// immediately after this block, such that if this block exits by
00437   /// falling through, control will transfer to the specified MBB. Note
00438   /// that MBB need not be a successor at all, for example if this block
00439   /// ends with an unconditional branch to some other block.
00440   bool isLayoutSuccessor(const MachineBasicBlock *MBB) const;
00441 
00442   /// canFallThrough - Return true if the block can implicitly transfer
00443   /// control to the block after it by falling off the end of it.  This should
00444   /// return false if it can reach the block after it, but it uses an explicit
00445   /// branch to do so (e.g., a table jump).  True is a conservative answer.
00446   bool canFallThrough();
00447 
00448   /// Returns a pointer to the first instruction in this block that is not a
00449   /// PHINode instruction. When adding instructions to the beginning of the
00450   /// basic block, they should be added before the returned value, not before
00451   /// the first instruction, which might be PHI.
00452   /// Returns end() is there's no non-PHI instruction.
00453   iterator getFirstNonPHI();
00454 
00455   /// SkipPHIsAndLabels - Return the first instruction in MBB after I that is
00456   /// not a PHI or a label. This is the correct point to insert copies at the
00457   /// beginning of a basic block.
00458   iterator SkipPHIsAndLabels(iterator I);
00459 
00460   /// getFirstTerminator - returns an iterator to the first terminator
00461   /// instruction of this basic block. If a terminator does not exist,
00462   /// it returns end()
00463   iterator getFirstTerminator();
00464   const_iterator getFirstTerminator() const;
00465 
00466   /// getFirstInstrTerminator - Same getFirstTerminator but it ignores bundles
00467   /// and return an instr_iterator instead.
00468   instr_iterator getFirstInstrTerminator();
00469 
00470   /// getLastNonDebugInstr - returns an iterator to the last non-debug
00471   /// instruction in the basic block, or end()
00472   iterator getLastNonDebugInstr();
00473   const_iterator getLastNonDebugInstr() const;
00474 
00475   /// SplitCriticalEdge - Split the critical edge from this block to the
00476   /// given successor block, and return the newly created block, or null
00477   /// if splitting is not possible.
00478   ///
00479   /// This function updates LiveVariables, MachineDominatorTree, and
00480   /// MachineLoopInfo, as applicable.
00481   MachineBasicBlock *SplitCriticalEdge(MachineBasicBlock *Succ, Pass *P);
00482 
00483   void pop_front() { Insts.pop_front(); }
00484   void pop_back() { Insts.pop_back(); }
00485   void push_back(MachineInstr *MI) { Insts.push_back(MI); }
00486 
00487   /// Insert MI into the instruction list before I, possibly inside a bundle.
00488   ///
00489   /// If the insertion point is inside a bundle, MI will be added to the bundle,
00490   /// otherwise MI will not be added to any bundle. That means this function
00491   /// alone can't be used to prepend or append instructions to bundles. See
00492   /// MIBundleBuilder::insert() for a more reliable way of doing that.
00493   instr_iterator insert(instr_iterator I, MachineInstr *M);
00494 
00495   /// Insert a range of instructions into the instruction list before I.
00496   template<typename IT>
00497   void insert(iterator I, IT S, IT E) {
00498     assert((I == end() || I->getParent() == this) &&
00499            "iterator points outside of basic block");
00500     Insts.insert(I.getInstrIterator(), S, E);
00501   }
00502 
00503   /// Insert MI into the instruction list before I.
00504   iterator insert(iterator I, MachineInstr *MI) {
00505     assert((I == end() || I->getParent() == this) &&
00506            "iterator points outside of basic block");
00507     assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() &&
00508            "Cannot insert instruction with bundle flags");
00509     return Insts.insert(I.getInstrIterator(), MI);
00510   }
00511 
00512   /// Insert MI into the instruction list after I.
00513   iterator insertAfter(iterator I, MachineInstr *MI) {
00514     assert((I == end() || I->getParent() == this) &&
00515            "iterator points outside of basic block");
00516     assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() &&
00517            "Cannot insert instruction with bundle flags");
00518     return Insts.insertAfter(I.getInstrIterator(), MI);
00519   }
00520 
00521   /// Remove an instruction from the instruction list and delete it.
00522   ///
00523   /// If the instruction is part of a bundle, the other instructions in the
00524   /// bundle will still be bundled after removing the single instruction.
00525   instr_iterator erase(instr_iterator I);
00526 
00527   /// Remove an instruction from the instruction list and delete it.
00528   ///
00529   /// If the instruction is part of a bundle, the other instructions in the
00530   /// bundle will still be bundled after removing the single instruction.
00531   instr_iterator erase_instr(MachineInstr *I) {
00532     return erase(instr_iterator(I));
00533   }
00534 
00535   /// Remove a range of instructions from the instruction list and delete them.
00536   iterator erase(iterator I, iterator E) {
00537     return Insts.erase(I.getInstrIterator(), E.getInstrIterator());
00538   }
00539 
00540   /// Remove an instruction or bundle from the instruction list and delete it.
00541   ///
00542   /// If I points to a bundle of instructions, they are all erased.
00543   iterator erase(iterator I) {
00544     return erase(I, std::next(I));
00545   }
00546 
00547   /// Remove an instruction from the instruction list and delete it.
00548   ///
00549   /// If I is the head of a bundle of instructions, the whole bundle will be
00550   /// erased.
00551   iterator erase(MachineInstr *I) {
00552     return erase(iterator(I));
00553   }
00554 
00555   /// Remove the unbundled instruction from the instruction list without
00556   /// deleting it.
00557   ///
00558   /// This function can not be used to remove bundled instructions, use
00559   /// remove_instr to remove individual instructions from a bundle.
00560   MachineInstr *remove(MachineInstr *I) {
00561     assert(!I->isBundled() && "Cannot remove bundled instructions");
00562     return Insts.remove(I);
00563   }
00564 
00565   /// Remove the possibly bundled instruction from the instruction list
00566   /// without deleting it.
00567   ///
00568   /// If the instruction is part of a bundle, the other instructions in the
00569   /// bundle will still be bundled after removing the single instruction.
00570   MachineInstr *remove_instr(MachineInstr *I);
00571 
00572   void clear() {
00573     Insts.clear();
00574   }
00575 
00576   /// Take an instruction from MBB 'Other' at the position From, and insert it
00577   /// into this MBB right before 'Where'.
00578   ///
00579   /// If From points to a bundle of instructions, the whole bundle is moved.
00580   void splice(iterator Where, MachineBasicBlock *Other, iterator From) {
00581     // The range splice() doesn't allow noop moves, but this one does.
00582     if (Where != From)
00583       splice(Where, Other, From, std::next(From));
00584   }
00585 
00586   /// Take a block of instructions from MBB 'Other' in the range [From, To),
00587   /// and insert them into this MBB right before 'Where'.
00588   ///
00589   /// The instruction at 'Where' must not be included in the range of
00590   /// instructions to move.
00591   void splice(iterator Where, MachineBasicBlock *Other,
00592               iterator From, iterator To) {
00593     Insts.splice(Where.getInstrIterator(), Other->Insts,
00594                  From.getInstrIterator(), To.getInstrIterator());
00595   }
00596 
00597   /// removeFromParent - This method unlinks 'this' from the containing
00598   /// function, and returns it, but does not delete it.
00599   MachineBasicBlock *removeFromParent();
00600 
00601   /// eraseFromParent - This method unlinks 'this' from the containing
00602   /// function and deletes it.
00603   void eraseFromParent();
00604 
00605   /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
00606   /// 'Old', change the code and CFG so that it branches to 'New' instead.
00607   void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
00608 
00609   /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in
00610   /// the CFG to be inserted.  If we have proven that MBB can only branch to
00611   /// DestA and DestB, remove any other MBB successors from the CFG. DestA and
00612   /// DestB can be null. Besides DestA and DestB, retain other edges leading
00613   /// to LandingPads (currently there can be only one; we don't check or require
00614   /// that here). Note it is possible that DestA and/or DestB are LandingPads.
00615   bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
00616                             MachineBasicBlock *DestB,
00617                             bool isCond);
00618 
00619   /// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping
00620   /// any DBG_VALUE instructions.  Return UnknownLoc if there is none.
00621   DebugLoc findDebugLoc(instr_iterator MBBI);
00622   DebugLoc findDebugLoc(iterator MBBI) {
00623     return findDebugLoc(MBBI.getInstrIterator());
00624   }
00625 
00626   /// Possible outcome of a register liveness query to computeRegisterLiveness()
00627   enum LivenessQueryResult {
00628     LQR_Live,            ///< Register is known to be live.
00629     LQR_OverlappingLive, ///< Register itself is not live, but some overlapping
00630                          ///< register is.
00631     LQR_Dead,            ///< Register is known to be dead.
00632     LQR_Unknown          ///< Register liveness not decidable from local
00633                          ///< neighborhood.
00634   };
00635 
00636   /// computeRegisterLiveness - Return whether (physical) register \c Reg
00637   /// has been <def>ined and not <kill>ed as of just before \c MI.
00638   ///
00639   /// Search is localised to a neighborhood of
00640   /// \c Neighborhood instructions before (searching for defs or kills) and
00641   /// Neighborhood instructions after (searching just for defs) MI.
00642   ///
00643   /// \c Reg must be a physical register.
00644   LivenessQueryResult computeRegisterLiveness(const TargetRegisterInfo *TRI,
00645                                               unsigned Reg, MachineInstr *MI,
00646                                               unsigned Neighborhood=10);
00647 
00648   // Debugging methods.
00649   void dump() const;
00650   void print(raw_ostream &OS, SlotIndexes* = nullptr) const;
00651 
00652   // Printing method used by LoopInfo.
00653   void printAsOperand(raw_ostream &OS, bool PrintType = true) const;
00654 
00655   /// getNumber - MachineBasicBlocks are uniquely numbered at the function
00656   /// level, unless they're not in a MachineFunction yet, in which case this
00657   /// will return -1.
00658   ///
00659   int getNumber() const { return Number; }
00660   void setNumber(int N) { Number = N; }
00661 
00662   /// getSymbol - Return the MCSymbol for this basic block.
00663   ///
00664   MCSymbol *getSymbol() const;
00665 
00666 
00667 private:
00668   /// getWeightIterator - Return weight iterator corresponding to the I
00669   /// successor iterator.
00670   weight_iterator getWeightIterator(succ_iterator I);
00671   const_weight_iterator getWeightIterator(const_succ_iterator I) const;
00672 
00673   friend class MachineBranchProbabilityInfo;
00674 
00675   /// getSuccWeight - Return weight of the edge from this block to MBB. This
00676   /// method should NOT be called directly, but by using getEdgeWeight method
00677   /// from MachineBranchProbabilityInfo class.
00678   uint32_t getSuccWeight(const_succ_iterator Succ) const;
00679 
00680 
00681   // Methods used to maintain doubly linked list of blocks...
00682   friend struct ilist_traits<MachineBasicBlock>;
00683 
00684   // Machine-CFG mutators
00685 
00686   /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
00687   /// Don't do this unless you know what you're doing, because it doesn't
00688   /// update pred's successors list. Use pred->addSuccessor instead.
00689   ///
00690   void addPredecessor(MachineBasicBlock *pred);
00691 
00692   /// removePredecessor - Remove pred as a predecessor of this
00693   /// MachineBasicBlock. Don't do this unless you know what you're
00694   /// doing, because it doesn't update pred's successors list. Use
00695   /// pred->removeSuccessor instead.
00696   ///
00697   void removePredecessor(MachineBasicBlock *pred);
00698 };
00699 
00700 raw_ostream& operator<<(raw_ostream &OS, const MachineBasicBlock &MBB);
00701 
00702 // This is useful when building IndexedMaps keyed on basic block pointers.
00703 struct MBB2NumberFunctor :
00704   public std::unary_function<const MachineBasicBlock*, unsigned> {
00705   unsigned operator()(const MachineBasicBlock *MBB) const {
00706     return MBB->getNumber();
00707   }
00708 };
00709 
00710 //===--------------------------------------------------------------------===//
00711 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
00712 //===--------------------------------------------------------------------===//
00713 
00714 // Provide specializations of GraphTraits to be able to treat a
00715 // MachineFunction as a graph of MachineBasicBlocks...
00716 //
00717 
00718 template <> struct GraphTraits<MachineBasicBlock *> {
00719   typedef MachineBasicBlock NodeType;
00720   typedef MachineBasicBlock::succ_iterator ChildIteratorType;
00721 
00722   static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
00723   static inline ChildIteratorType child_begin(NodeType *N) {
00724     return N->succ_begin();
00725   }
00726   static inline ChildIteratorType child_end(NodeType *N) {
00727     return N->succ_end();
00728   }
00729 };
00730 
00731 template <> struct GraphTraits<const MachineBasicBlock *> {
00732   typedef const MachineBasicBlock NodeType;
00733   typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
00734 
00735   static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
00736   static inline ChildIteratorType child_begin(NodeType *N) {
00737     return N->succ_begin();
00738   }
00739   static inline ChildIteratorType child_end(NodeType *N) {
00740     return N->succ_end();
00741   }
00742 };
00743 
00744 // Provide specializations of GraphTraits to be able to treat a
00745 // MachineFunction as a graph of MachineBasicBlocks... and to walk it
00746 // in inverse order.  Inverse order for a function is considered
00747 // to be when traversing the predecessor edges of a MBB
00748 // instead of the successor edges.
00749 //
00750 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
00751   typedef MachineBasicBlock NodeType;
00752   typedef MachineBasicBlock::pred_iterator ChildIteratorType;
00753   static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
00754     return G.Graph;
00755   }
00756   static inline ChildIteratorType child_begin(NodeType *N) {
00757     return N->pred_begin();
00758   }
00759   static inline ChildIteratorType child_end(NodeType *N) {
00760     return N->pred_end();
00761   }
00762 };
00763 
00764 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
00765   typedef const MachineBasicBlock NodeType;
00766   typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
00767   static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
00768     return G.Graph;
00769   }
00770   static inline ChildIteratorType child_begin(NodeType *N) {
00771     return N->pred_begin();
00772   }
00773   static inline ChildIteratorType child_end(NodeType *N) {
00774     return N->pred_end();
00775   }
00776 };
00777 
00778 
00779 
00780 /// MachineInstrSpan provides an interface to get an iteration range
00781 /// containing the instruction it was initialized with, along with all
00782 /// those instructions inserted prior to or following that instruction
00783 /// at some point after the MachineInstrSpan is constructed.
00784 class MachineInstrSpan {
00785   MachineBasicBlock &MBB;
00786   MachineBasicBlock::iterator I, B, E;
00787 public:
00788   MachineInstrSpan(MachineBasicBlock::iterator I)
00789     : MBB(*I->getParent()),
00790       I(I),
00791       B(I == MBB.begin() ? MBB.end() : std::prev(I)),
00792       E(std::next(I)) {}
00793 
00794   MachineBasicBlock::iterator begin() {
00795     return B == MBB.end() ? MBB.begin() : std::next(B);
00796   }
00797   MachineBasicBlock::iterator end() { return E; }
00798   bool empty() { return begin() == end(); }
00799 
00800   MachineBasicBlock::iterator getInitial() { return I; }
00801 };
00802 
00803 } // End llvm namespace
00804 
00805 #endif