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     return const_cast<MachineBasicBlock *>(this)->getFirstTerminator();
00466   }
00467 
00468   /// getFirstInstrTerminator - Same getFirstTerminator but it ignores bundles
00469   /// and return an instr_iterator instead.
00470   instr_iterator getFirstInstrTerminator();
00471 
00472   /// getFirstNonDebugInstr - returns an iterator to the first non-debug
00473   /// instruction in the basic block, or end()
00474   iterator getFirstNonDebugInstr();
00475   const_iterator getFirstNonDebugInstr() const {
00476     return const_cast<MachineBasicBlock *>(this)->getFirstNonDebugInstr();
00477   }
00478 
00479   /// getLastNonDebugInstr - returns an iterator to the last non-debug
00480   /// instruction in the basic block, or end()
00481   iterator getLastNonDebugInstr();
00482   const_iterator getLastNonDebugInstr() const {
00483     return const_cast<MachineBasicBlock *>(this)->getLastNonDebugInstr();
00484   }
00485 
00486   /// SplitCriticalEdge - Split the critical edge from this block to the
00487   /// given successor block, and return the newly created block, or null
00488   /// if splitting is not possible.
00489   ///
00490   /// This function updates LiveVariables, MachineDominatorTree, and
00491   /// MachineLoopInfo, as applicable.
00492   MachineBasicBlock *SplitCriticalEdge(MachineBasicBlock *Succ, Pass *P);
00493 
00494   void pop_front() { Insts.pop_front(); }
00495   void pop_back() { Insts.pop_back(); }
00496   void push_back(MachineInstr *MI) { Insts.push_back(MI); }
00497 
00498   /// Insert MI into the instruction list before I, possibly inside a bundle.
00499   ///
00500   /// If the insertion point is inside a bundle, MI will be added to the bundle,
00501   /// otherwise MI will not be added to any bundle. That means this function
00502   /// alone can't be used to prepend or append instructions to bundles. See
00503   /// MIBundleBuilder::insert() for a more reliable way of doing that.
00504   instr_iterator insert(instr_iterator I, MachineInstr *M);
00505 
00506   /// Insert a range of instructions into the instruction list before I.
00507   template<typename IT>
00508   void insert(iterator I, IT S, IT E) {
00509     assert((I == end() || I->getParent() == this) &&
00510            "iterator points outside of basic block");
00511     Insts.insert(I.getInstrIterator(), S, E);
00512   }
00513 
00514   /// Insert MI into the instruction list before I.
00515   iterator insert(iterator I, MachineInstr *MI) {
00516     assert((I == end() || I->getParent() == this) &&
00517            "iterator points outside of basic block");
00518     assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() &&
00519            "Cannot insert instruction with bundle flags");
00520     return Insts.insert(I.getInstrIterator(), MI);
00521   }
00522 
00523   /// Insert MI into the instruction list after I.
00524   iterator insertAfter(iterator I, MachineInstr *MI) {
00525     assert((I == end() || I->getParent() == this) &&
00526            "iterator points outside of basic block");
00527     assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() &&
00528            "Cannot insert instruction with bundle flags");
00529     return Insts.insertAfter(I.getInstrIterator(), MI);
00530   }
00531 
00532   /// Remove an instruction from the instruction list and delete it.
00533   ///
00534   /// If the instruction is part of a bundle, the other instructions in the
00535   /// bundle will still be bundled after removing the single instruction.
00536   instr_iterator erase(instr_iterator I);
00537 
00538   /// Remove an instruction from the instruction list and delete it.
00539   ///
00540   /// If the instruction is part of a bundle, the other instructions in the
00541   /// bundle will still be bundled after removing the single instruction.
00542   instr_iterator erase_instr(MachineInstr *I) {
00543     return erase(instr_iterator(I));
00544   }
00545 
00546   /// Remove a range of instructions from the instruction list and delete them.
00547   iterator erase(iterator I, iterator E) {
00548     return Insts.erase(I.getInstrIterator(), E.getInstrIterator());
00549   }
00550 
00551   /// Remove an instruction or bundle from the instruction list and delete it.
00552   ///
00553   /// If I points to a bundle of instructions, they are all erased.
00554   iterator erase(iterator I) {
00555     return erase(I, std::next(I));
00556   }
00557 
00558   /// Remove an instruction from the instruction list and delete it.
00559   ///
00560   /// If I is the head of a bundle of instructions, the whole bundle will be
00561   /// erased.
00562   iterator erase(MachineInstr *I) {
00563     return erase(iterator(I));
00564   }
00565 
00566   /// Remove the unbundled instruction from the instruction list without
00567   /// deleting it.
00568   ///
00569   /// This function can not be used to remove bundled instructions, use
00570   /// remove_instr to remove individual instructions from a bundle.
00571   MachineInstr *remove(MachineInstr *I) {
00572     assert(!I->isBundled() && "Cannot remove bundled instructions");
00573     return Insts.remove(I);
00574   }
00575 
00576   /// Remove the possibly bundled instruction from the instruction list
00577   /// without deleting it.
00578   ///
00579   /// If the instruction is part of a bundle, the other instructions in the
00580   /// bundle will still be bundled after removing the single instruction.
00581   MachineInstr *remove_instr(MachineInstr *I);
00582 
00583   void clear() {
00584     Insts.clear();
00585   }
00586 
00587   /// Take an instruction from MBB 'Other' at the position From, and insert it
00588   /// into this MBB right before 'Where'.
00589   ///
00590   /// If From points to a bundle of instructions, the whole bundle is moved.
00591   void splice(iterator Where, MachineBasicBlock *Other, iterator From) {
00592     // The range splice() doesn't allow noop moves, but this one does.
00593     if (Where != From)
00594       splice(Where, Other, From, std::next(From));
00595   }
00596 
00597   /// Take a block of instructions from MBB 'Other' in the range [From, To),
00598   /// and insert them into this MBB right before 'Where'.
00599   ///
00600   /// The instruction at 'Where' must not be included in the range of
00601   /// instructions to move.
00602   void splice(iterator Where, MachineBasicBlock *Other,
00603               iterator From, iterator To) {
00604     Insts.splice(Where.getInstrIterator(), Other->Insts,
00605                  From.getInstrIterator(), To.getInstrIterator());
00606   }
00607 
00608   /// removeFromParent - This method unlinks 'this' from the containing
00609   /// function, and returns it, but does not delete it.
00610   MachineBasicBlock *removeFromParent();
00611 
00612   /// eraseFromParent - This method unlinks 'this' from the containing
00613   /// function and deletes it.
00614   void eraseFromParent();
00615 
00616   /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
00617   /// 'Old', change the code and CFG so that it branches to 'New' instead.
00618   void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
00619 
00620   /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in
00621   /// the CFG to be inserted.  If we have proven that MBB can only branch to
00622   /// DestA and DestB, remove any other MBB successors from the CFG. DestA and
00623   /// DestB can be null. Besides DestA and DestB, retain other edges leading
00624   /// to LandingPads (currently there can be only one; we don't check or require
00625   /// that here). Note it is possible that DestA and/or DestB are LandingPads.
00626   bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
00627                             MachineBasicBlock *DestB,
00628                             bool isCond);
00629 
00630   /// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping
00631   /// any DBG_VALUE instructions.  Return UnknownLoc if there is none.
00632   DebugLoc findDebugLoc(instr_iterator MBBI);
00633   DebugLoc findDebugLoc(iterator MBBI) {
00634     return findDebugLoc(MBBI.getInstrIterator());
00635   }
00636 
00637   /// Possible outcome of a register liveness query to computeRegisterLiveness()
00638   enum LivenessQueryResult {
00639     LQR_Live,            ///< Register is known to be live.
00640     LQR_OverlappingLive, ///< Register itself is not live, but some overlapping
00641                          ///< register is.
00642     LQR_Dead,            ///< Register is known to be dead.
00643     LQR_Unknown          ///< Register liveness not decidable from local
00644                          ///< neighborhood.
00645   };
00646 
00647   /// Return whether (physical) register \p Reg has been <def>ined and not
00648   /// <kill>ed as of just before \p Before.
00649   ///
00650   /// Search is localised to a neighborhood of \p Neighborhood instructions
00651   /// before (searching for defs or kills) and \p Neighborhood instructions
00652   /// after (searching just for defs) \p Before.
00653   ///
00654   /// \p Reg must be a physical register.
00655   LivenessQueryResult computeRegisterLiveness(const TargetRegisterInfo *TRI,
00656                                               unsigned Reg,
00657                                               const_iterator Before,
00658                                               unsigned Neighborhood=10) const;
00659 
00660   // Debugging methods.
00661   void dump() const;
00662   void print(raw_ostream &OS, SlotIndexes* = nullptr) const;
00663 
00664   // Printing method used by LoopInfo.
00665   void printAsOperand(raw_ostream &OS, bool PrintType = true) const;
00666 
00667   /// getNumber - MachineBasicBlocks are uniquely numbered at the function
00668   /// level, unless they're not in a MachineFunction yet, in which case this
00669   /// will return -1.
00670   ///
00671   int getNumber() const { return Number; }
00672   void setNumber(int N) { Number = N; }
00673 
00674   /// getSymbol - Return the MCSymbol for this basic block.
00675   ///
00676   MCSymbol *getSymbol() const;
00677 
00678 
00679 private:
00680   /// getWeightIterator - Return weight iterator corresponding to the I
00681   /// successor iterator.
00682   weight_iterator getWeightIterator(succ_iterator I);
00683   const_weight_iterator getWeightIterator(const_succ_iterator I) const;
00684 
00685   friend class MachineBranchProbabilityInfo;
00686 
00687   /// getSuccWeight - Return weight of the edge from this block to MBB. This
00688   /// method should NOT be called directly, but by using getEdgeWeight method
00689   /// from MachineBranchProbabilityInfo class.
00690   uint32_t getSuccWeight(const_succ_iterator Succ) const;
00691 
00692 
00693   // Methods used to maintain doubly linked list of blocks...
00694   friend struct ilist_traits<MachineBasicBlock>;
00695 
00696   // Machine-CFG mutators
00697 
00698   /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
00699   /// Don't do this unless you know what you're doing, because it doesn't
00700   /// update pred's successors list. Use pred->addSuccessor instead.
00701   ///
00702   void addPredecessor(MachineBasicBlock *pred);
00703 
00704   /// removePredecessor - Remove pred as a predecessor of this
00705   /// MachineBasicBlock. Don't do this unless you know what you're
00706   /// doing, because it doesn't update pred's successors list. Use
00707   /// pred->removeSuccessor instead.
00708   ///
00709   void removePredecessor(MachineBasicBlock *pred);
00710 };
00711 
00712 raw_ostream& operator<<(raw_ostream &OS, const MachineBasicBlock &MBB);
00713 
00714 // This is useful when building IndexedMaps keyed on basic block pointers.
00715 struct MBB2NumberFunctor :
00716   public std::unary_function<const MachineBasicBlock*, unsigned> {
00717   unsigned operator()(const MachineBasicBlock *MBB) const {
00718     return MBB->getNumber();
00719   }
00720 };
00721 
00722 //===--------------------------------------------------------------------===//
00723 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
00724 //===--------------------------------------------------------------------===//
00725 
00726 // Provide specializations of GraphTraits to be able to treat a
00727 // MachineFunction as a graph of MachineBasicBlocks...
00728 //
00729 
00730 template <> struct GraphTraits<MachineBasicBlock *> {
00731   typedef MachineBasicBlock NodeType;
00732   typedef MachineBasicBlock::succ_iterator ChildIteratorType;
00733 
00734   static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
00735   static inline ChildIteratorType child_begin(NodeType *N) {
00736     return N->succ_begin();
00737   }
00738   static inline ChildIteratorType child_end(NodeType *N) {
00739     return N->succ_end();
00740   }
00741 };
00742 
00743 template <> struct GraphTraits<const MachineBasicBlock *> {
00744   typedef const MachineBasicBlock NodeType;
00745   typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
00746 
00747   static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
00748   static inline ChildIteratorType child_begin(NodeType *N) {
00749     return N->succ_begin();
00750   }
00751   static inline ChildIteratorType child_end(NodeType *N) {
00752     return N->succ_end();
00753   }
00754 };
00755 
00756 // Provide specializations of GraphTraits to be able to treat a
00757 // MachineFunction as a graph of MachineBasicBlocks... and to walk it
00758 // in inverse order.  Inverse order for a function is considered
00759 // to be when traversing the predecessor edges of a MBB
00760 // instead of the successor edges.
00761 //
00762 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
00763   typedef MachineBasicBlock NodeType;
00764   typedef MachineBasicBlock::pred_iterator ChildIteratorType;
00765   static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
00766     return G.Graph;
00767   }
00768   static inline ChildIteratorType child_begin(NodeType *N) {
00769     return N->pred_begin();
00770   }
00771   static inline ChildIteratorType child_end(NodeType *N) {
00772     return N->pred_end();
00773   }
00774 };
00775 
00776 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
00777   typedef const MachineBasicBlock NodeType;
00778   typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
00779   static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
00780     return G.Graph;
00781   }
00782   static inline ChildIteratorType child_begin(NodeType *N) {
00783     return N->pred_begin();
00784   }
00785   static inline ChildIteratorType child_end(NodeType *N) {
00786     return N->pred_end();
00787   }
00788 };
00789 
00790 
00791 
00792 /// MachineInstrSpan provides an interface to get an iteration range
00793 /// containing the instruction it was initialized with, along with all
00794 /// those instructions inserted prior to or following that instruction
00795 /// at some point after the MachineInstrSpan is constructed.
00796 class MachineInstrSpan {
00797   MachineBasicBlock &MBB;
00798   MachineBasicBlock::iterator I, B, E;
00799 public:
00800   MachineInstrSpan(MachineBasicBlock::iterator I)
00801     : MBB(*I->getParent()),
00802       I(I),
00803       B(I == MBB.begin() ? MBB.end() : std::prev(I)),
00804       E(std::next(I)) {}
00805 
00806   MachineBasicBlock::iterator begin() {
00807     return B == MBB.end() ? MBB.begin() : std::next(B);
00808   }
00809   MachineBasicBlock::iterator end() { return E; }
00810   bool empty() { return begin() == end(); }
00811 
00812   MachineBasicBlock::iterator getInitial() { return I; }
00813 };
00814 
00815 } // End llvm namespace
00816 
00817 #endif