MachineBasicBlock.h revision 704b07613eb16b7ffb8c8cbc94482bf7425ccd77
1//===-- llvm/CodeGen/MachineBasicBlock.h ------------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// Collect the sequence of machine instructions for a basic block. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CODEGEN_MACHINEBASICBLOCK_H 15#define LLVM_CODEGEN_MACHINEBASICBLOCK_H 16 17#include "llvm/CodeGen/MachineInstr.h" 18#include "llvm/ADT/GraphTraits.h" 19 20namespace llvm { 21 22class Pass; 23class BasicBlock; 24class MachineFunction; 25class MCSymbol; 26class StringRef; 27class raw_ostream; 28 29template <> 30struct ilist_traits<MachineInstr> : public ilist_default_traits<MachineInstr> { 31private: 32 mutable ilist_half_node<MachineInstr> Sentinel; 33 34 // this is only set by the MachineBasicBlock owning the LiveList 35 friend class MachineBasicBlock; 36 MachineBasicBlock* Parent; 37 38public: 39 MachineInstr *createSentinel() const { 40 return static_cast<MachineInstr*>(&Sentinel); 41 } 42 void destroySentinel(MachineInstr *) const {} 43 44 MachineInstr *provideInitialHead() const { return createSentinel(); } 45 MachineInstr *ensureHead(MachineInstr*) const { return createSentinel(); } 46 static void noteHead(MachineInstr*, MachineInstr*) {} 47 48 void addNodeToList(MachineInstr* N); 49 void removeNodeFromList(MachineInstr* N); 50 void transferNodesFromList(ilist_traits &SrcTraits, 51 ilist_iterator<MachineInstr> first, 52 ilist_iterator<MachineInstr> last); 53 void deleteNode(MachineInstr *N); 54private: 55 void createNode(const MachineInstr &); 56}; 57 58class MachineBasicBlock : public ilist_node<MachineBasicBlock> { 59 typedef ilist<MachineInstr> Instructions; 60 Instructions Insts; 61 const BasicBlock *BB; 62 int Number; 63 MachineFunction *xParent; 64 65 /// Predecessors/Successors - Keep track of the predecessor / successor 66 /// basicblocks. 67 std::vector<MachineBasicBlock *> Predecessors; 68 std::vector<MachineBasicBlock *> Successors; 69 70 /// LiveIns - Keep track of the physical registers that are livein of 71 /// the basicblock. 72 std::vector<unsigned> LiveIns; 73 74 /// Alignment - Alignment of the basic block. Zero if the basic block does 75 /// not need to be aligned. 76 unsigned Alignment; 77 78 /// IsLandingPad - Indicate that this basic block is entered via an 79 /// exception handler. 80 bool IsLandingPad; 81 82 /// AddressTaken - Indicate that this basic block is potentially the 83 /// target of an indirect branch. 84 bool AddressTaken; 85 86 // Intrusive list support 87 MachineBasicBlock() {} 88 89 explicit MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb); 90 91 ~MachineBasicBlock(); 92 93 // MachineBasicBlocks are allocated and owned by MachineFunction. 94 friend class MachineFunction; 95 96public: 97 /// getBasicBlock - Return the LLVM basic block that this instance 98 /// corresponded to originally. Note that this may be NULL if this instance 99 /// does not correspond directly to an LLVM basic block. 100 /// 101 const BasicBlock *getBasicBlock() const { return BB; } 102 103 /// getName - Return the name of the corresponding LLVM basic block, or 104 /// "(null)". 105 StringRef getName() const; 106 107 /// hasAddressTaken - Test whether this block is potentially the target 108 /// of an indirect branch. 109 bool hasAddressTaken() const { return AddressTaken; } 110 111 /// setHasAddressTaken - Set this block to reflect that it potentially 112 /// is the target of an indirect branch. 113 void setHasAddressTaken() { AddressTaken = true; } 114 115 /// getParent - Return the MachineFunction containing this basic block. 116 /// 117 const MachineFunction *getParent() const { return xParent; } 118 MachineFunction *getParent() { return xParent; } 119 120 typedef Instructions::iterator iterator; 121 typedef Instructions::const_iterator const_iterator; 122 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 123 typedef std::reverse_iterator<iterator> reverse_iterator; 124 125 unsigned size() const { return (unsigned)Insts.size(); } 126 bool empty() const { return Insts.empty(); } 127 128 MachineInstr& front() { return Insts.front(); } 129 MachineInstr& back() { return Insts.back(); } 130 const MachineInstr& front() const { return Insts.front(); } 131 const MachineInstr& back() const { return Insts.back(); } 132 133 iterator begin() { return Insts.begin(); } 134 const_iterator begin() const { return Insts.begin(); } 135 iterator end() { return Insts.end(); } 136 const_iterator end() const { return Insts.end(); } 137 reverse_iterator rbegin() { return Insts.rbegin(); } 138 const_reverse_iterator rbegin() const { return Insts.rbegin(); } 139 reverse_iterator rend () { return Insts.rend(); } 140 const_reverse_iterator rend () const { return Insts.rend(); } 141 142 // Machine-CFG iterators 143 typedef std::vector<MachineBasicBlock *>::iterator pred_iterator; 144 typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator; 145 typedef std::vector<MachineBasicBlock *>::iterator succ_iterator; 146 typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator; 147 typedef std::vector<MachineBasicBlock *>::reverse_iterator 148 pred_reverse_iterator; 149 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator 150 const_pred_reverse_iterator; 151 typedef std::vector<MachineBasicBlock *>::reverse_iterator 152 succ_reverse_iterator; 153 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator 154 const_succ_reverse_iterator; 155 156 pred_iterator pred_begin() { return Predecessors.begin(); } 157 const_pred_iterator pred_begin() const { return Predecessors.begin(); } 158 pred_iterator pred_end() { return Predecessors.end(); } 159 const_pred_iterator pred_end() const { return Predecessors.end(); } 160 pred_reverse_iterator pred_rbegin() 161 { return Predecessors.rbegin();} 162 const_pred_reverse_iterator pred_rbegin() const 163 { return Predecessors.rbegin();} 164 pred_reverse_iterator pred_rend() 165 { return Predecessors.rend(); } 166 const_pred_reverse_iterator pred_rend() const 167 { return Predecessors.rend(); } 168 unsigned pred_size() const { 169 return (unsigned)Predecessors.size(); 170 } 171 bool pred_empty() const { return Predecessors.empty(); } 172 succ_iterator succ_begin() { return Successors.begin(); } 173 const_succ_iterator succ_begin() const { return Successors.begin(); } 174 succ_iterator succ_end() { return Successors.end(); } 175 const_succ_iterator succ_end() const { return Successors.end(); } 176 succ_reverse_iterator succ_rbegin() 177 { return Successors.rbegin(); } 178 const_succ_reverse_iterator succ_rbegin() const 179 { return Successors.rbegin(); } 180 succ_reverse_iterator succ_rend() 181 { return Successors.rend(); } 182 const_succ_reverse_iterator succ_rend() const 183 { return Successors.rend(); } 184 unsigned succ_size() const { 185 return (unsigned)Successors.size(); 186 } 187 bool succ_empty() const { return Successors.empty(); } 188 189 // LiveIn management methods. 190 191 /// addLiveIn - Add the specified register as a live in. Note that it 192 /// is an error to add the same register to the same set more than once. 193 void addLiveIn(unsigned Reg) { LiveIns.push_back(Reg); } 194 195 /// removeLiveIn - Remove the specified register from the live in set. 196 /// 197 void removeLiveIn(unsigned Reg); 198 199 /// isLiveIn - Return true if the specified register is in the live in set. 200 /// 201 bool isLiveIn(unsigned Reg) const; 202 203 // Iteration support for live in sets. These sets are kept in sorted 204 // order by their register number. 205 typedef std::vector<unsigned>::const_iterator livein_iterator; 206 livein_iterator livein_begin() const { return LiveIns.begin(); } 207 livein_iterator livein_end() const { return LiveIns.end(); } 208 bool livein_empty() const { return LiveIns.empty(); } 209 210 /// getAlignment - Return alignment of the basic block. 211 /// 212 unsigned getAlignment() const { return Alignment; } 213 214 /// setAlignment - Set alignment of the basic block. 215 /// 216 void setAlignment(unsigned Align) { Alignment = Align; } 217 218 /// isLandingPad - Returns true if the block is a landing pad. That is 219 /// this basic block is entered via an exception handler. 220 bool isLandingPad() const { return IsLandingPad; } 221 222 /// setIsLandingPad - Indicates the block is a landing pad. That is 223 /// this basic block is entered via an exception handler. 224 void setIsLandingPad() { IsLandingPad = true; } 225 226 // Code Layout methods. 227 228 /// moveBefore/moveAfter - move 'this' block before or after the specified 229 /// block. This only moves the block, it does not modify the CFG or adjust 230 /// potential fall-throughs at the end of the block. 231 void moveBefore(MachineBasicBlock *NewAfter); 232 void moveAfter(MachineBasicBlock *NewBefore); 233 234 /// updateTerminator - Update the terminator instructions in block to account 235 /// for changes to the layout. If the block previously used a fallthrough, 236 /// it may now need a branch, and if it previously used branching it may now 237 /// be able to use a fallthrough. 238 void updateTerminator(); 239 240 // Machine-CFG mutators 241 242 /// addSuccessor - Add succ as a successor of this MachineBasicBlock. 243 /// The Predecessors list of succ is automatically updated. 244 /// 245 void addSuccessor(MachineBasicBlock *succ); 246 247 /// removeSuccessor - Remove successor from the successors list of this 248 /// MachineBasicBlock. The Predecessors list of succ is automatically updated. 249 /// 250 void removeSuccessor(MachineBasicBlock *succ); 251 252 /// removeSuccessor - Remove specified successor from the successors list of 253 /// this MachineBasicBlock. The Predecessors list of succ is automatically 254 /// updated. Return the iterator to the element after the one removed. 255 /// 256 succ_iterator removeSuccessor(succ_iterator I); 257 258 /// transferSuccessors - Transfers all the successors from MBB to this 259 /// machine basic block (i.e., copies all the successors fromMBB and 260 /// remove all the successors from fromMBB). 261 void transferSuccessors(MachineBasicBlock *fromMBB); 262 263 /// transferSuccessorsAndUpdatePHIs - Transfers all the successors, as 264 /// in transferSuccessors, and update PHI operands in the successor blocks 265 /// which refer to fromMBB to refer to this. 266 void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *fromMBB); 267 268 /// isSuccessor - Return true if the specified MBB is a successor of this 269 /// block. 270 bool isSuccessor(const MachineBasicBlock *MBB) const; 271 272 /// isLayoutSuccessor - Return true if the specified MBB will be emitted 273 /// immediately after this block, such that if this block exits by 274 /// falling through, control will transfer to the specified MBB. Note 275 /// that MBB need not be a successor at all, for example if this block 276 /// ends with an unconditional branch to some other block. 277 bool isLayoutSuccessor(const MachineBasicBlock *MBB) const; 278 279 /// canFallThrough - Return true if the block can implicitly transfer 280 /// control to the block after it by falling off the end of it. This should 281 /// return false if it can reach the block after it, but it uses an explicit 282 /// branch to do so (e.g., a table jump). True is a conservative answer. 283 bool canFallThrough(); 284 285 /// Returns a pointer to the first instructon in this block that is not a 286 /// PHINode instruction. When adding instruction to the beginning of the 287 /// basic block, they should be added before the returned value, not before 288 /// the first instruction, which might be PHI. 289 /// Returns end() is there's no non-PHI instruction. 290 iterator getFirstNonPHI(); 291 292 /// getFirstTerminator - returns an iterator to the first terminator 293 /// instruction of this basic block. If a terminator does not exist, 294 /// it returns end() 295 iterator getFirstTerminator(); 296 297 /// SplitCriticalEdge - Split the critical edge from this block to the 298 /// given successor block, and return the newly created block, or null 299 /// if splitting is not possible. 300 /// 301 /// This function updates LiveVariables, MachineDominatorTree, and 302 /// MachineLoopInfo, as applicable. 303 MachineBasicBlock *SplitCriticalEdge(MachineBasicBlock *Succ, Pass *P); 304 305 void pop_front() { Insts.pop_front(); } 306 void pop_back() { Insts.pop_back(); } 307 void push_back(MachineInstr *MI) { Insts.push_back(MI); } 308 template<typename IT> 309 void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); } 310 iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); } 311 iterator insertAfter(iterator I, MachineInstr *M) { 312 return Insts.insertAfter(I, M); 313 } 314 315 // erase - Remove the specified element or range from the instruction list. 316 // These functions delete any instructions removed. 317 // 318 iterator erase(iterator I) { return Insts.erase(I); } 319 iterator erase(iterator I, iterator E) { return Insts.erase(I, E); } 320 MachineInstr *remove(MachineInstr *I) { return Insts.remove(I); } 321 void clear() { Insts.clear(); } 322 323 /// splice - Take an instruction from MBB 'Other' at the position From, 324 /// and insert it into this MBB right before 'where'. 325 void splice(iterator where, MachineBasicBlock *Other, iterator From) { 326 Insts.splice(where, Other->Insts, From); 327 } 328 329 /// splice - Take a block of instructions from MBB 'Other' in the range [From, 330 /// To), and insert them into this MBB right before 'where'. 331 void splice(iterator where, MachineBasicBlock *Other, iterator From, 332 iterator To) { 333 Insts.splice(where, Other->Insts, From, To); 334 } 335 336 /// removeFromParent - This method unlinks 'this' from the containing 337 /// function, and returns it, but does not delete it. 338 MachineBasicBlock *removeFromParent(); 339 340 /// eraseFromParent - This method unlinks 'this' from the containing 341 /// function and deletes it. 342 void eraseFromParent(); 343 344 /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to 345 /// 'Old', change the code and CFG so that it branches to 'New' instead. 346 void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New); 347 348 /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in 349 /// the CFG to be inserted. If we have proven that MBB can only branch to 350 /// DestA and DestB, remove any other MBB successors from the CFG. DestA and 351 /// DestB can be null. Besides DestA and DestB, retain other edges leading 352 /// to LandingPads (currently there can be only one; we don't check or require 353 /// that here). Note it is possible that DestA and/or DestB are LandingPads. 354 bool CorrectExtraCFGEdges(MachineBasicBlock *DestA, 355 MachineBasicBlock *DestB, 356 bool isCond); 357 358 /// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping 359 /// any DBG_VALUE instructions. Return UnknownLoc if there is none. 360 DebugLoc findDebugLoc(MachineBasicBlock::iterator &MBBI); 361 362 // Debugging methods. 363 void dump() const; 364 void print(raw_ostream &OS) const; 365 366 /// getNumber - MachineBasicBlocks are uniquely numbered at the function 367 /// level, unless they're not in a MachineFunction yet, in which case this 368 /// will return -1. 369 /// 370 int getNumber() const { return Number; } 371 void setNumber(int N) { Number = N; } 372 373 /// getSymbol - Return the MCSymbol for this basic block. 374 /// 375 MCSymbol *getSymbol() const; 376 377private: // Methods used to maintain doubly linked list of blocks... 378 friend struct ilist_traits<MachineBasicBlock>; 379 380 // Machine-CFG mutators 381 382 /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock. 383 /// Don't do this unless you know what you're doing, because it doesn't 384 /// update pred's successors list. Use pred->addSuccessor instead. 385 /// 386 void addPredecessor(MachineBasicBlock *pred); 387 388 /// removePredecessor - Remove pred as a predecessor of this 389 /// MachineBasicBlock. Don't do this unless you know what you're 390 /// doing, because it doesn't update pred's successors list. Use 391 /// pred->removeSuccessor instead. 392 /// 393 void removePredecessor(MachineBasicBlock *pred); 394}; 395 396raw_ostream& operator<<(raw_ostream &OS, const MachineBasicBlock &MBB); 397 398void WriteAsOperand(raw_ostream &, const MachineBasicBlock*, bool t); 399 400//===--------------------------------------------------------------------===// 401// GraphTraits specializations for machine basic block graphs (machine-CFGs) 402//===--------------------------------------------------------------------===// 403 404// Provide specializations of GraphTraits to be able to treat a 405// MachineFunction as a graph of MachineBasicBlocks... 406// 407 408template <> struct GraphTraits<MachineBasicBlock *> { 409 typedef MachineBasicBlock NodeType; 410 typedef MachineBasicBlock::succ_iterator ChildIteratorType; 411 412 static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; } 413 static inline ChildIteratorType child_begin(NodeType *N) { 414 return N->succ_begin(); 415 } 416 static inline ChildIteratorType child_end(NodeType *N) { 417 return N->succ_end(); 418 } 419}; 420 421template <> struct GraphTraits<const MachineBasicBlock *> { 422 typedef const MachineBasicBlock NodeType; 423 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType; 424 425 static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; } 426 static inline ChildIteratorType child_begin(NodeType *N) { 427 return N->succ_begin(); 428 } 429 static inline ChildIteratorType child_end(NodeType *N) { 430 return N->succ_end(); 431 } 432}; 433 434// Provide specializations of GraphTraits to be able to treat a 435// MachineFunction as a graph of MachineBasicBlocks... and to walk it 436// in inverse order. Inverse order for a function is considered 437// to be when traversing the predecessor edges of a MBB 438// instead of the successor edges. 439// 440template <> struct GraphTraits<Inverse<MachineBasicBlock*> > { 441 typedef MachineBasicBlock NodeType; 442 typedef MachineBasicBlock::pred_iterator ChildIteratorType; 443 static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) { 444 return G.Graph; 445 } 446 static inline ChildIteratorType child_begin(NodeType *N) { 447 return N->pred_begin(); 448 } 449 static inline ChildIteratorType child_end(NodeType *N) { 450 return N->pred_end(); 451 } 452}; 453 454template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > { 455 typedef const MachineBasicBlock NodeType; 456 typedef MachineBasicBlock::const_pred_iterator ChildIteratorType; 457 static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) { 458 return G.Graph; 459 } 460 static inline ChildIteratorType child_begin(NodeType *N) { 461 return N->pred_begin(); 462 } 463 static inline ChildIteratorType child_end(NodeType *N) { 464 return N->pred_end(); 465 } 466}; 467 468} // End llvm namespace 469 470#endif 471