LLVM API Documentation

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   /// addLiveIn - Add the specified register as a live in.  Note that it
00319   /// is an error to add the same register to the same set more than once.
00320   void addLiveIn(unsigned Reg)  { LiveIns.push_back(Reg); }
00321 
00322   /// Add PhysReg as live in to this block, and ensure that there is a copy of
00323   /// PhysReg to a virtual register of class RC. Return the virtual register
00324   /// that is a copy of the live in PhysReg.
00325   unsigned addLiveIn(unsigned PhysReg, const TargetRegisterClass *RC);
00326 
00327   /// removeLiveIn - Remove the specified register from the live in set.
00328   ///
00329   void removeLiveIn(unsigned Reg);
00330 
00331   /// isLiveIn - Return true if the specified register is in the live in set.
00332   ///
00333   bool isLiveIn(unsigned Reg) const;
00334 
00335   // Iteration support for live in sets.  These sets are kept in sorted
00336   // order by their register number.
00337   typedef std::vector<unsigned>::const_iterator livein_iterator;
00338   livein_iterator livein_begin() const { return LiveIns.begin(); }
00339   livein_iterator livein_end()   const { return LiveIns.end(); }
00340   bool            livein_empty() const { return LiveIns.empty(); }
00341 
00342   /// getAlignment - Return alignment of the basic block.
00343   /// The alignment is specified as log2(bytes).
00344   ///
00345   unsigned getAlignment() const { return Alignment; }
00346 
00347   /// setAlignment - Set alignment of the basic block.
00348   /// The alignment is specified as log2(bytes).
00349   ///
00350   void setAlignment(unsigned Align) { Alignment = Align; }
00351 
00352   /// isLandingPad - Returns true if the block is a landing pad. That is
00353   /// this basic block is entered via an exception handler.
00354   bool isLandingPad() const { return IsLandingPad; }
00355 
00356   /// setIsLandingPad - Indicates the block is a landing pad.  That is
00357   /// this basic block is entered via an exception handler.
00358   void setIsLandingPad(bool V = true) { IsLandingPad = V; }
00359 
00360   /// getLandingPadSuccessor - If this block has a successor that is a landing
00361   /// pad, return it. Otherwise return NULL.
00362   const MachineBasicBlock *getLandingPadSuccessor() const;
00363 
00364   // Code Layout methods.
00365 
00366   /// moveBefore/moveAfter - move 'this' block before or after the specified
00367   /// block.  This only moves the block, it does not modify the CFG or adjust
00368   /// potential fall-throughs at the end of the block.
00369   void moveBefore(MachineBasicBlock *NewAfter);
00370   void moveAfter(MachineBasicBlock *NewBefore);
00371 
00372   /// updateTerminator - Update the terminator instructions in block to account
00373   /// for changes to the layout. If the block previously used a fallthrough,
00374   /// it may now need a branch, and if it previously used branching it may now
00375   /// be able to use a fallthrough.
00376   void updateTerminator();
00377 
00378   // Machine-CFG mutators
00379 
00380   /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
00381   /// The Predecessors list of succ is automatically updated. WEIGHT
00382   /// parameter is stored in Weights list and it may be used by
00383   /// MachineBranchProbabilityInfo analysis to calculate branch probability.
00384   ///
00385   /// Note that duplicate Machine CFG edges are not allowed.
00386   ///
00387   void addSuccessor(MachineBasicBlock *succ, uint32_t weight = 0);
00388 
00389   /// Set successor weight of a given iterator.
00390   void setSuccWeight(succ_iterator I, uint32_t weight);
00391 
00392   /// removeSuccessor - Remove successor from the successors list of this
00393   /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
00394   ///
00395   void removeSuccessor(MachineBasicBlock *succ);
00396 
00397   /// removeSuccessor - Remove specified successor from the successors list of
00398   /// this MachineBasicBlock. The Predecessors list of succ is automatically
00399   /// updated.  Return the iterator to the element after the one removed.
00400   ///
00401   succ_iterator removeSuccessor(succ_iterator I);
00402 
00403   /// replaceSuccessor - Replace successor OLD with NEW and update weight info.
00404   ///
00405   void replaceSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New);
00406 
00407 
00408   /// transferSuccessors - Transfers all the successors from MBB to this
00409   /// machine basic block (i.e., copies all the successors fromMBB and
00410   /// remove all the successors from fromMBB).
00411   void transferSuccessors(MachineBasicBlock *fromMBB);
00412 
00413   /// transferSuccessorsAndUpdatePHIs - Transfers all the successors, as
00414   /// in transferSuccessors, and update PHI operands in the successor blocks
00415   /// which refer to fromMBB to refer to this.
00416   void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *fromMBB);
00417 
00418   /// isPredecessor - Return true if the specified MBB is a predecessor of this
00419   /// block.
00420   bool isPredecessor(const MachineBasicBlock *MBB) const;
00421 
00422   /// isSuccessor - Return true if the specified MBB is a successor of this
00423   /// block.
00424   bool isSuccessor(const MachineBasicBlock *MBB) const;
00425 
00426   /// isLayoutSuccessor - Return true if the specified MBB will be emitted
00427   /// immediately after this block, such that if this block exits by
00428   /// falling through, control will transfer to the specified MBB. Note
00429   /// that MBB need not be a successor at all, for example if this block
00430   /// ends with an unconditional branch to some other block.
00431   bool isLayoutSuccessor(const MachineBasicBlock *MBB) const;
00432 
00433   /// canFallThrough - Return true if the block can implicitly transfer
00434   /// control to the block after it by falling off the end of it.  This should
00435   /// return false if it can reach the block after it, but it uses an explicit
00436   /// branch to do so (e.g., a table jump).  True is a conservative answer.
00437   bool canFallThrough();
00438 
00439   /// Returns a pointer to the first instruction in this block that is not a
00440   /// PHINode instruction. When adding instructions to the beginning of the
00441   /// basic block, they should be added before the returned value, not before
00442   /// the first instruction, which might be PHI.
00443   /// Returns end() is there's no non-PHI instruction.
00444   iterator getFirstNonPHI();
00445 
00446   /// SkipPHIsAndLabels - Return the first instruction in MBB after I that is
00447   /// not a PHI or a label. This is the correct point to insert copies at the
00448   /// beginning of a basic block.
00449   iterator SkipPHIsAndLabels(iterator I);
00450 
00451   /// getFirstTerminator - returns an iterator to the first terminator
00452   /// instruction of this basic block. If a terminator does not exist,
00453   /// it returns end()
00454   iterator getFirstTerminator();
00455   const_iterator getFirstTerminator() const;
00456 
00457   /// getFirstInstrTerminator - Same getFirstTerminator but it ignores bundles
00458   /// and return an instr_iterator instead.
00459   instr_iterator getFirstInstrTerminator();
00460 
00461   /// getLastNonDebugInstr - returns an iterator to the last non-debug
00462   /// instruction in the basic block, or end()
00463   iterator getLastNonDebugInstr();
00464   const_iterator getLastNonDebugInstr() const;
00465 
00466   /// SplitCriticalEdge - Split the critical edge from this block to the
00467   /// given successor block, and return the newly created block, or null
00468   /// if splitting is not possible.
00469   ///
00470   /// This function updates LiveVariables, MachineDominatorTree, and
00471   /// MachineLoopInfo, as applicable.
00472   MachineBasicBlock *SplitCriticalEdge(MachineBasicBlock *Succ, Pass *P);
00473 
00474   void pop_front() { Insts.pop_front(); }
00475   void pop_back() { Insts.pop_back(); }
00476   void push_back(MachineInstr *MI) { Insts.push_back(MI); }
00477 
00478   /// Insert MI into the instruction list before I, possibly inside a bundle.
00479   ///
00480   /// If the insertion point is inside a bundle, MI will be added to the bundle,
00481   /// otherwise MI will not be added to any bundle. That means this function
00482   /// alone can't be used to prepend or append instructions to bundles. See
00483   /// MIBundleBuilder::insert() for a more reliable way of doing that.
00484   instr_iterator insert(instr_iterator I, MachineInstr *M);
00485 
00486   /// Insert a range of instructions into the instruction list before I.
00487   template<typename IT>
00488   void insert(iterator I, IT S, IT E) {
00489     assert((I == end() || I->getParent() == this) &&
00490            "iterator points outside of basic block");
00491     Insts.insert(I.getInstrIterator(), S, E);
00492   }
00493 
00494   /// Insert MI into the instruction list before I.
00495   iterator insert(iterator I, MachineInstr *MI) {
00496     assert((I == end() || I->getParent() == this) &&
00497            "iterator points outside of basic block");
00498     assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() &&
00499            "Cannot insert instruction with bundle flags");
00500     return Insts.insert(I.getInstrIterator(), MI);
00501   }
00502 
00503   /// Insert MI into the instruction list after I.
00504   iterator insertAfter(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.insertAfter(I.getInstrIterator(), MI);
00510   }
00511 
00512   /// Remove an instruction from the instruction list and delete it.
00513   ///
00514   /// If the instruction is part of a bundle, the other instructions in the
00515   /// bundle will still be bundled after removing the single instruction.
00516   instr_iterator erase(instr_iterator I);
00517 
00518   /// Remove an instruction from the instruction list and delete it.
00519   ///
00520   /// If the instruction is part of a bundle, the other instructions in the
00521   /// bundle will still be bundled after removing the single instruction.
00522   instr_iterator erase_instr(MachineInstr *I) {
00523     return erase(instr_iterator(I));
00524   }
00525 
00526   /// Remove a range of instructions from the instruction list and delete them.
00527   iterator erase(iterator I, iterator E) {
00528     return Insts.erase(I.getInstrIterator(), E.getInstrIterator());
00529   }
00530 
00531   /// Remove an instruction or bundle from the instruction list and delete it.
00532   ///
00533   /// If I points to a bundle of instructions, they are all erased.
00534   iterator erase(iterator I) {
00535     return erase(I, std::next(I));
00536   }
00537 
00538   /// Remove an instruction from the instruction list and delete it.
00539   ///
00540   /// If I is the head of a bundle of instructions, the whole bundle will be
00541   /// erased.
00542   iterator erase(MachineInstr *I) {
00543     return erase(iterator(I));
00544   }
00545 
00546   /// Remove the unbundled instruction from the instruction list without
00547   /// deleting it.
00548   ///
00549   /// This function can not be used to remove bundled instructions, use
00550   /// remove_instr to remove individual instructions from a bundle.
00551   MachineInstr *remove(MachineInstr *I) {
00552     assert(!I->isBundled() && "Cannot remove bundled instructions");
00553     return Insts.remove(I);
00554   }
00555 
00556   /// Remove the possibly bundled instruction from the instruction list
00557   /// without deleting it.
00558   ///
00559   /// If the instruction is part of a bundle, the other instructions in the
00560   /// bundle will still be bundled after removing the single instruction.
00561   MachineInstr *remove_instr(MachineInstr *I);
00562 
00563   void clear() {
00564     Insts.clear();
00565   }
00566 
00567   /// Take an instruction from MBB 'Other' at the position From, and insert it
00568   /// into this MBB right before 'Where'.
00569   ///
00570   /// If From points to a bundle of instructions, the whole bundle is moved.
00571   void splice(iterator Where, MachineBasicBlock *Other, iterator From) {
00572     // The range splice() doesn't allow noop moves, but this one does.
00573     if (Where != From)
00574       splice(Where, Other, From, std::next(From));
00575   }
00576 
00577   /// Take a block of instructions from MBB 'Other' in the range [From, To),
00578   /// and insert them into this MBB right before 'Where'.
00579   ///
00580   /// The instruction at 'Where' must not be included in the range of
00581   /// instructions to move.
00582   void splice(iterator Where, MachineBasicBlock *Other,
00583               iterator From, iterator To) {
00584     Insts.splice(Where.getInstrIterator(), Other->Insts,
00585                  From.getInstrIterator(), To.getInstrIterator());
00586   }
00587 
00588   /// removeFromParent - This method unlinks 'this' from the containing
00589   /// function, and returns it, but does not delete it.
00590   MachineBasicBlock *removeFromParent();
00591 
00592   /// eraseFromParent - This method unlinks 'this' from the containing
00593   /// function and deletes it.
00594   void eraseFromParent();
00595 
00596   /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
00597   /// 'Old', change the code and CFG so that it branches to 'New' instead.
00598   void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
00599 
00600   /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in
00601   /// the CFG to be inserted.  If we have proven that MBB can only branch to
00602   /// DestA and DestB, remove any other MBB successors from the CFG. DestA and
00603   /// DestB can be null. Besides DestA and DestB, retain other edges leading
00604   /// to LandingPads (currently there can be only one; we don't check or require
00605   /// that here). Note it is possible that DestA and/or DestB are LandingPads.
00606   bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
00607                             MachineBasicBlock *DestB,
00608                             bool isCond);
00609 
00610   /// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping
00611   /// any DBG_VALUE instructions.  Return UnknownLoc if there is none.
00612   DebugLoc findDebugLoc(instr_iterator MBBI);
00613   DebugLoc findDebugLoc(iterator MBBI) {
00614     return findDebugLoc(MBBI.getInstrIterator());
00615   }
00616 
00617   /// Possible outcome of a register liveness query to computeRegisterLiveness()
00618   enum LivenessQueryResult {
00619     LQR_Live,            ///< Register is known to be live.
00620     LQR_OverlappingLive, ///< Register itself is not live, but some overlapping
00621                          ///< register is.
00622     LQR_Dead,            ///< Register is known to be dead.
00623     LQR_Unknown          ///< Register liveness not decidable from local
00624                          ///< neighborhood.
00625   };
00626 
00627   /// computeRegisterLiveness - Return whether (physical) register \c Reg
00628   /// has been <def>ined and not <kill>ed as of just before \c MI.
00629   ///
00630   /// Search is localised to a neighborhood of
00631   /// \c Neighborhood instructions before (searching for defs or kills) and
00632   /// Neighborhood instructions after (searching just for defs) MI.
00633   ///
00634   /// \c Reg must be a physical register.
00635   LivenessQueryResult computeRegisterLiveness(const TargetRegisterInfo *TRI,
00636                                               unsigned Reg, MachineInstr *MI,
00637                                               unsigned Neighborhood=10);
00638 
00639   // Debugging methods.
00640   void dump() const;
00641   void print(raw_ostream &OS, SlotIndexes* = nullptr) const;
00642 
00643   // Printing method used by LoopInfo.
00644   void printAsOperand(raw_ostream &OS, bool PrintType = true) const;
00645 
00646   /// getNumber - MachineBasicBlocks are uniquely numbered at the function
00647   /// level, unless they're not in a MachineFunction yet, in which case this
00648   /// will return -1.
00649   ///
00650   int getNumber() const { return Number; }
00651   void setNumber(int N) { Number = N; }
00652 
00653   /// getSymbol - Return the MCSymbol for this basic block.
00654   ///
00655   MCSymbol *getSymbol() const;
00656 
00657 
00658 private:
00659   /// getWeightIterator - Return weight iterator corresponding to the I
00660   /// successor iterator.
00661   weight_iterator getWeightIterator(succ_iterator I);
00662   const_weight_iterator getWeightIterator(const_succ_iterator I) const;
00663 
00664   friend class MachineBranchProbabilityInfo;
00665 
00666   /// getSuccWeight - Return weight of the edge from this block to MBB. This
00667   /// method should NOT be called directly, but by using getEdgeWeight method
00668   /// from MachineBranchProbabilityInfo class.
00669   uint32_t getSuccWeight(const_succ_iterator Succ) const;
00670 
00671 
00672   // Methods used to maintain doubly linked list of blocks...
00673   friend struct ilist_traits<MachineBasicBlock>;
00674 
00675   // Machine-CFG mutators
00676 
00677   /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
00678   /// Don't do this unless you know what you're doing, because it doesn't
00679   /// update pred's successors list. Use pred->addSuccessor instead.
00680   ///
00681   void addPredecessor(MachineBasicBlock *pred);
00682 
00683   /// removePredecessor - Remove pred as a predecessor of this
00684   /// MachineBasicBlock. Don't do this unless you know what you're
00685   /// doing, because it doesn't update pred's successors list. Use
00686   /// pred->removeSuccessor instead.
00687   ///
00688   void removePredecessor(MachineBasicBlock *pred);
00689 };
00690 
00691 raw_ostream& operator<<(raw_ostream &OS, const MachineBasicBlock &MBB);
00692 
00693 // This is useful when building IndexedMaps keyed on basic block pointers.
00694 struct MBB2NumberFunctor :
00695   public std::unary_function<const MachineBasicBlock*, unsigned> {
00696   unsigned operator()(const MachineBasicBlock *MBB) const {
00697     return MBB->getNumber();
00698   }
00699 };
00700 
00701 //===--------------------------------------------------------------------===//
00702 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
00703 //===--------------------------------------------------------------------===//
00704 
00705 // Provide specializations of GraphTraits to be able to treat a
00706 // MachineFunction as a graph of MachineBasicBlocks...
00707 //
00708 
00709 template <> struct GraphTraits<MachineBasicBlock *> {
00710   typedef MachineBasicBlock NodeType;
00711   typedef MachineBasicBlock::succ_iterator ChildIteratorType;
00712 
00713   static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
00714   static inline ChildIteratorType child_begin(NodeType *N) {
00715     return N->succ_begin();
00716   }
00717   static inline ChildIteratorType child_end(NodeType *N) {
00718     return N->succ_end();
00719   }
00720 };
00721 
00722 template <> struct GraphTraits<const MachineBasicBlock *> {
00723   typedef const MachineBasicBlock NodeType;
00724   typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
00725 
00726   static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
00727   static inline ChildIteratorType child_begin(NodeType *N) {
00728     return N->succ_begin();
00729   }
00730   static inline ChildIteratorType child_end(NodeType *N) {
00731     return N->succ_end();
00732   }
00733 };
00734 
00735 // Provide specializations of GraphTraits to be able to treat a
00736 // MachineFunction as a graph of MachineBasicBlocks... and to walk it
00737 // in inverse order.  Inverse order for a function is considered
00738 // to be when traversing the predecessor edges of a MBB
00739 // instead of the successor edges.
00740 //
00741 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
00742   typedef MachineBasicBlock NodeType;
00743   typedef MachineBasicBlock::pred_iterator ChildIteratorType;
00744   static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
00745     return G.Graph;
00746   }
00747   static inline ChildIteratorType child_begin(NodeType *N) {
00748     return N->pred_begin();
00749   }
00750   static inline ChildIteratorType child_end(NodeType *N) {
00751     return N->pred_end();
00752   }
00753 };
00754 
00755 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
00756   typedef const MachineBasicBlock NodeType;
00757   typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
00758   static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
00759     return G.Graph;
00760   }
00761   static inline ChildIteratorType child_begin(NodeType *N) {
00762     return N->pred_begin();
00763   }
00764   static inline ChildIteratorType child_end(NodeType *N) {
00765     return N->pred_end();
00766   }
00767 };
00768 
00769 
00770 
00771 /// MachineInstrSpan provides an interface to get an iteration range
00772 /// containing the instruction it was initialized with, along with all
00773 /// those instructions inserted prior to or following that instruction
00774 /// at some point after the MachineInstrSpan is constructed.
00775 class MachineInstrSpan {
00776   MachineBasicBlock &MBB;
00777   MachineBasicBlock::iterator I, B, E;
00778 public:
00779   MachineInstrSpan(MachineBasicBlock::iterator I)
00780     : MBB(*I->getParent()),
00781       I(I),
00782       B(I == MBB.begin() ? MBB.end() : std::prev(I)),
00783       E(std::next(I)) {}
00784 
00785   MachineBasicBlock::iterator begin() {
00786     return B == MBB.end() ? MBB.begin() : std::next(B);
00787   }
00788   MachineBasicBlock::iterator end() { return E; }
00789   bool empty() { return begin() == end(); }
00790 
00791   MachineBasicBlock::iterator getInitial() { return I; }
00792 };
00793 
00794 } // End llvm namespace
00795 
00796 #endif