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