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