1 //===- llvm/BasicBlock.h - Represent a basic block in the VM ----*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file contains the declaration of the BasicBlock class. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_IR_BASICBLOCK_H 14 #define LLVM_IR_BASICBLOCK_H 15 16 #include "llvm-c/Types.h" 17 #include "llvm/ADT/Twine.h" 18 #include "llvm/ADT/ilist.h" 19 #include "llvm/ADT/ilist_node.h" 20 #include "llvm/ADT/iterator.h" 21 #include "llvm/ADT/iterator_range.h" 22 #include "llvm/IR/Instruction.h" 23 #include "llvm/IR/SymbolTableListTraits.h" 24 #include "llvm/IR/Value.h" 25 #include "llvm/Support/CBindingWrapping.h" 26 #include "llvm/Support/Casting.h" 27 #include "llvm/Support/Compiler.h" 28 #include <cassert> 29 #include <cstddef> 30 #include <iterator> 31 32 namespace llvm { 33 34 class AssemblyAnnotationWriter; 35 class CallInst; 36 class Function; 37 class LandingPadInst; 38 class LLVMContext; 39 class Module; 40 class PHINode; 41 class ValueSymbolTable; 42 43 /// LLVM Basic Block Representation 44 /// 45 /// This represents a single basic block in LLVM. A basic block is simply a 46 /// container of instructions that execute sequentially. Basic blocks are Values 47 /// because they are referenced by instructions such as branches and switch 48 /// tables. The type of a BasicBlock is "Type::LabelTy" because the basic block 49 /// represents a label to which a branch can jump. 50 /// 51 /// A well formed basic block is formed of a list of non-terminating 52 /// instructions followed by a single terminator instruction. Terminator 53 /// instructions may not occur in the middle of basic blocks, and must terminate 54 /// the blocks. The BasicBlock class allows malformed basic blocks to occur 55 /// because it may be useful in the intermediate stage of constructing or 56 /// modifying a program. However, the verifier will ensure that basic blocks are 57 /// "well formed". 58 class BasicBlock final : public Value, // Basic blocks are data objects also 59 public ilist_node_with_parent<BasicBlock, Function> { 60 public: 61 using InstListType = SymbolTableList<Instruction>; 62 63 private: 64 friend class BlockAddress; 65 friend class SymbolTableListTraits<BasicBlock>; 66 67 InstListType InstList; 68 Function *Parent; 69 70 void setParent(Function *parent); 71 72 /// Constructor. 73 /// 74 /// If the function parameter is specified, the basic block is automatically 75 /// inserted at either the end of the function (if InsertBefore is null), or 76 /// before the specified basic block. 77 explicit BasicBlock(LLVMContext &C, const Twine &Name = "", 78 Function *Parent = nullptr, 79 BasicBlock *InsertBefore = nullptr); 80 81 public: 82 BasicBlock(const BasicBlock &) = delete; 83 BasicBlock &operator=(const BasicBlock &) = delete; 84 ~BasicBlock(); 85 86 /// Get the context in which this basic block lives. 87 LLVMContext &getContext() const; 88 89 /// Instruction iterators... 90 using iterator = InstListType::iterator; 91 using const_iterator = InstListType::const_iterator; 92 using reverse_iterator = InstListType::reverse_iterator; 93 using const_reverse_iterator = InstListType::const_reverse_iterator; 94 95 /// Creates a new BasicBlock. 96 /// 97 /// If the Parent parameter is specified, the basic block is automatically 98 /// inserted at either the end of the function (if InsertBefore is 0), or 99 /// before the specified basic block. 100 static BasicBlock *Create(LLVMContext &Context, const Twine &Name = "", 101 Function *Parent = nullptr, 102 BasicBlock *InsertBefore = nullptr) { 103 return new BasicBlock(Context, Name, Parent, InsertBefore); 104 } 105 106 /// Return the enclosing method, or null if none. 107 const Function *getParent() const { return Parent; } 108 Function *getParent() { return Parent; } 109 110 /// Return the module owning the function this basic block belongs to, or 111 /// nullptr if the function does not have a module. 112 /// 113 /// Note: this is undefined behavior if the block does not have a parent. 114 const Module *getModule() const; 115 Module *getModule() { 116 return const_cast<Module *>( 117 static_cast<const BasicBlock *>(this)->getModule()); 118 } 119 120 /// Returns the terminator instruction if the block is well formed or null 121 /// if the block is not well formed. 122 const Instruction *getTerminator() const LLVM_READONLY; 123 Instruction *getTerminator() { 124 return const_cast<Instruction *>( 125 static_cast<const BasicBlock *>(this)->getTerminator()); 126 } 127 128 /// Returns the call instruction calling \@llvm.experimental.deoptimize 129 /// prior to the terminating return instruction of this basic block, if such 130 /// a call is present. Otherwise, returns null. 131 const CallInst *getTerminatingDeoptimizeCall() const; 132 CallInst *getTerminatingDeoptimizeCall() { 133 return const_cast<CallInst *>( 134 static_cast<const BasicBlock *>(this)->getTerminatingDeoptimizeCall()); 135 } 136 137 /// Returns the call instruction calling \@llvm.experimental.deoptimize 138 /// that is present either in current basic block or in block that is a unique 139 /// successor to current block, if such call is present. Otherwise, returns null. 140 const CallInst *getPostdominatingDeoptimizeCall() const; 141 CallInst *getPostdominatingDeoptimizeCall() { 142 return const_cast<CallInst *>( 143 static_cast<const BasicBlock *>(this)->getPostdominatingDeoptimizeCall()); 144 } 145 146 /// Returns the call instruction marked 'musttail' prior to the terminating 147 /// return instruction of this basic block, if such a call is present. 148 /// Otherwise, returns null. 149 const CallInst *getTerminatingMustTailCall() const; 150 CallInst *getTerminatingMustTailCall() { 151 return const_cast<CallInst *>( 152 static_cast<const BasicBlock *>(this)->getTerminatingMustTailCall()); 153 } 154 155 /// Returns a pointer to the first instruction in this block that is not a 156 /// PHINode instruction. 157 /// 158 /// When adding instructions to the beginning of the basic block, they should 159 /// be added before the returned value, not before the first instruction, 160 /// which might be PHI. Returns 0 is there's no non-PHI instruction. 161 const Instruction* getFirstNonPHI() const; 162 Instruction* getFirstNonPHI() { 163 return const_cast<Instruction *>( 164 static_cast<const BasicBlock *>(this)->getFirstNonPHI()); 165 } 166 167 /// Returns a pointer to the first instruction in this block that is not a 168 /// PHINode or a debug intrinsic. 169 const Instruction* getFirstNonPHIOrDbg() const; 170 Instruction* getFirstNonPHIOrDbg() { 171 return const_cast<Instruction *>( 172 static_cast<const BasicBlock *>(this)->getFirstNonPHIOrDbg()); 173 } 174 175 /// Returns a pointer to the first instruction in this block that is not a 176 /// PHINode, a debug intrinsic, or a lifetime intrinsic. 177 const Instruction* getFirstNonPHIOrDbgOrLifetime() const; 178 Instruction* getFirstNonPHIOrDbgOrLifetime() { 179 return const_cast<Instruction *>( 180 static_cast<const BasicBlock *>(this)->getFirstNonPHIOrDbgOrLifetime()); 181 } 182 183 /// Returns an iterator to the first instruction in this block that is 184 /// suitable for inserting a non-PHI instruction. 185 /// 186 /// In particular, it skips all PHIs and LandingPad instructions. 187 const_iterator getFirstInsertionPt() const; 188 iterator getFirstInsertionPt() { 189 return static_cast<const BasicBlock *>(this) 190 ->getFirstInsertionPt().getNonConst(); 191 } 192 193 /// Return a const iterator range over the instructions in the block, skipping 194 /// any debug instructions. 195 iterator_range<filter_iterator<BasicBlock::const_iterator, 196 std::function<bool(const Instruction &)>>> 197 instructionsWithoutDebug() const; 198 199 /// Return an iterator range over the instructions in the block, skipping any 200 /// debug instructions. 201 iterator_range<filter_iterator<BasicBlock::iterator, 202 std::function<bool(Instruction &)>>> 203 instructionsWithoutDebug(); 204 205 /// Return the size of the basic block ignoring debug instructions 206 filter_iterator<BasicBlock::const_iterator, 207 std::function<bool(const Instruction &)>>::difference_type 208 sizeWithoutDebug() const; 209 210 /// Unlink 'this' from the containing function, but do not delete it. 211 void removeFromParent(); 212 213 /// Unlink 'this' from the containing function and delete it. 214 /// 215 // \returns an iterator pointing to the element after the erased one. 216 SymbolTableList<BasicBlock>::iterator eraseFromParent(); 217 218 /// Unlink this basic block from its current function and insert it into 219 /// the function that \p MovePos lives in, right before \p MovePos. 220 void moveBefore(BasicBlock *MovePos); 221 222 /// Unlink this basic block from its current function and insert it 223 /// right after \p MovePos in the function \p MovePos lives in. 224 void moveAfter(BasicBlock *MovePos); 225 226 /// Insert unlinked basic block into a function. 227 /// 228 /// Inserts an unlinked basic block into \c Parent. If \c InsertBefore is 229 /// provided, inserts before that basic block, otherwise inserts at the end. 230 /// 231 /// \pre \a getParent() is \c nullptr. 232 void insertInto(Function *Parent, BasicBlock *InsertBefore = nullptr); 233 234 /// Return the predecessor of this block if it has a single predecessor 235 /// block. Otherwise return a null pointer. 236 const BasicBlock *getSinglePredecessor() const; 237 BasicBlock *getSinglePredecessor() { 238 return const_cast<BasicBlock *>( 239 static_cast<const BasicBlock *>(this)->getSinglePredecessor()); 240 } 241 242 /// Return the predecessor of this block if it has a unique predecessor 243 /// block. Otherwise return a null pointer. 244 /// 245 /// Note that unique predecessor doesn't mean single edge, there can be 246 /// multiple edges from the unique predecessor to this block (for example a 247 /// switch statement with multiple cases having the same destination). 248 const BasicBlock *getUniquePredecessor() const; 249 BasicBlock *getUniquePredecessor() { 250 return const_cast<BasicBlock *>( 251 static_cast<const BasicBlock *>(this)->getUniquePredecessor()); 252 } 253 254 /// Return true if this block has exactly N predecessors. 255 bool hasNPredecessors(unsigned N) const; 256 257 /// Return true if this block has N predecessors or more. 258 bool hasNPredecessorsOrMore(unsigned N) const; 259 260 /// Return the successor of this block if it has a single successor. 261 /// Otherwise return a null pointer. 262 /// 263 /// This method is analogous to getSinglePredecessor above. 264 const BasicBlock *getSingleSuccessor() const; 265 BasicBlock *getSingleSuccessor() { 266 return const_cast<BasicBlock *>( 267 static_cast<const BasicBlock *>(this)->getSingleSuccessor()); 268 } 269 270 /// Return the successor of this block if it has a unique successor. 271 /// Otherwise return a null pointer. 272 /// 273 /// This method is analogous to getUniquePredecessor above. 274 const BasicBlock *getUniqueSuccessor() const; 275 BasicBlock *getUniqueSuccessor() { 276 return const_cast<BasicBlock *>( 277 static_cast<const BasicBlock *>(this)->getUniqueSuccessor()); 278 } 279 280 /// Print the basic block to an output stream with an optional 281 /// AssemblyAnnotationWriter. 282 void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW = nullptr, 283 bool ShouldPreserveUseListOrder = false, 284 bool IsForDebug = false) const; 285 286 //===--------------------------------------------------------------------===// 287 /// Instruction iterator methods 288 /// 289 inline iterator begin() { return InstList.begin(); } 290 inline const_iterator begin() const { return InstList.begin(); } 291 inline iterator end () { return InstList.end(); } 292 inline const_iterator end () const { return InstList.end(); } 293 294 inline reverse_iterator rbegin() { return InstList.rbegin(); } 295 inline const_reverse_iterator rbegin() const { return InstList.rbegin(); } 296 inline reverse_iterator rend () { return InstList.rend(); } 297 inline const_reverse_iterator rend () const { return InstList.rend(); } 298 299 inline size_t size() const { return InstList.size(); } 300 inline bool empty() const { return InstList.empty(); } 301 inline const Instruction &front() const { return InstList.front(); } 302 inline Instruction &front() { return InstList.front(); } 303 inline const Instruction &back() const { return InstList.back(); } 304 inline Instruction &back() { return InstList.back(); } 305 306 /// Iterator to walk just the phi nodes in the basic block. 307 template <typename PHINodeT = PHINode, typename BBIteratorT = iterator> 308 class phi_iterator_impl 309 : public iterator_facade_base<phi_iterator_impl<PHINodeT, BBIteratorT>, 310 std::forward_iterator_tag, PHINodeT> { 311 friend BasicBlock; 312 313 PHINodeT *PN; 314 315 phi_iterator_impl(PHINodeT *PN) : PN(PN) {} 316 317 public: 318 // Allow default construction to build variables, but this doesn't build 319 // a useful iterator. 320 phi_iterator_impl() = default; 321 322 // Allow conversion between instantiations where valid. 323 template <typename PHINodeU, typename BBIteratorU> 324 phi_iterator_impl(const phi_iterator_impl<PHINodeU, BBIteratorU> &Arg) 325 : PN(Arg.PN) {} 326 327 bool operator==(const phi_iterator_impl &Arg) const { return PN == Arg.PN; } 328 329 PHINodeT &operator*() const { return *PN; } 330 331 using phi_iterator_impl::iterator_facade_base::operator++; 332 phi_iterator_impl &operator++() { 333 assert(PN && "Cannot increment the end iterator!"); 334 PN = dyn_cast<PHINodeT>(std::next(BBIteratorT(PN))); 335 return *this; 336 } 337 }; 338 using phi_iterator = phi_iterator_impl<>; 339 using const_phi_iterator = 340 phi_iterator_impl<const PHINode, BasicBlock::const_iterator>; 341 342 /// Returns a range that iterates over the phis in the basic block. 343 /// 344 /// Note that this cannot be used with basic blocks that have no terminator. 345 iterator_range<const_phi_iterator> phis() const { 346 return const_cast<BasicBlock *>(this)->phis(); 347 } 348 iterator_range<phi_iterator> phis(); 349 350 /// Return the underlying instruction list container. 351 /// 352 /// Currently you need to access the underlying instruction list container 353 /// directly if you want to modify it. 354 const InstListType &getInstList() const { return InstList; } 355 InstListType &getInstList() { return InstList; } 356 357 /// Returns a pointer to a member of the instruction list. 358 static InstListType BasicBlock::*getSublistAccess(Instruction*) { 359 return &BasicBlock::InstList; 360 } 361 362 /// Returns a pointer to the symbol table if one exists. 363 ValueSymbolTable *getValueSymbolTable(); 364 365 /// Methods for support type inquiry through isa, cast, and dyn_cast. 366 static bool classof(const Value *V) { 367 return V->getValueID() == Value::BasicBlockVal; 368 } 369 370 /// Cause all subinstructions to "let go" of all the references that said 371 /// subinstructions are maintaining. 372 /// 373 /// This allows one to 'delete' a whole class at a time, even though there may 374 /// be circular references... first all references are dropped, and all use 375 /// counts go to zero. Then everything is delete'd for real. Note that no 376 /// operations are valid on an object that has "dropped all references", 377 /// except operator delete. 378 void dropAllReferences(); 379 380 /// Update PHI nodes in this BasicBlock before removal of predecessor \p Pred. 381 /// Note that this function does not actually remove the predecessor. 382 /// 383 /// If \p KeepOneInputPHIs is true then don't remove PHIs that are left with 384 /// zero or one incoming values, and don't simplify PHIs with all incoming 385 /// values the same. 386 void removePredecessor(BasicBlock *Pred, bool KeepOneInputPHIs = false); 387 388 bool canSplitPredecessors() const; 389 390 /// Split the basic block into two basic blocks at the specified instruction. 391 /// 392 /// Note that all instructions BEFORE the specified iterator stay as part of 393 /// the original basic block, an unconditional branch is added to the original 394 /// BB, and the rest of the instructions in the BB are moved to the new BB, 395 /// including the old terminator. The newly formed BasicBlock is returned. 396 /// This function invalidates the specified iterator. 397 /// 398 /// Note that this only works on well formed basic blocks (must have a 399 /// terminator), and 'I' must not be the end of instruction list (which would 400 /// cause a degenerate basic block to be formed, having a terminator inside of 401 /// the basic block). 402 /// 403 /// Also note that this doesn't preserve any passes. To split blocks while 404 /// keeping loop information consistent, use the SplitBlock utility function. 405 BasicBlock *splitBasicBlock(iterator I, const Twine &BBName = ""); 406 BasicBlock *splitBasicBlock(Instruction *I, const Twine &BBName = "") { 407 return splitBasicBlock(I->getIterator(), BBName); 408 } 409 410 /// Returns true if there are any uses of this basic block other than 411 /// direct branches, switches, etc. to it. 412 bool hasAddressTaken() const { 413 return getBasicBlockBits().BlockAddressRefCount != 0; 414 } 415 416 /// Update all phi nodes in this basic block to refer to basic block \p New 417 /// instead of basic block \p Old. 418 void replacePhiUsesWith(BasicBlock *Old, BasicBlock *New); 419 420 /// Update all phi nodes in this basic block's successors to refer to basic 421 /// block \p New instead of basic block \p Old. 422 void replaceSuccessorsPhiUsesWith(BasicBlock *Old, BasicBlock *New); 423 424 /// Update all phi nodes in this basic block's successors to refer to basic 425 /// block \p New instead of to it. 426 void replaceSuccessorsPhiUsesWith(BasicBlock *New); 427 428 /// Return true if this basic block is an exception handling block. 429 bool isEHPad() const { return getFirstNonPHI()->isEHPad(); } 430 431 /// Return true if this basic block is a landing pad. 432 /// 433 /// Being a ``landing pad'' means that the basic block is the destination of 434 /// the 'unwind' edge of an invoke instruction. 435 bool isLandingPad() const; 436 437 /// Return the landingpad instruction associated with the landing pad. 438 const LandingPadInst *getLandingPadInst() const; 439 LandingPadInst *getLandingPadInst() { 440 return const_cast<LandingPadInst *>( 441 static_cast<const BasicBlock *>(this)->getLandingPadInst()); 442 } 443 444 /// Return true if it is legal to hoist instructions into this block. 445 bool isLegalToHoistInto() const; 446 447 Optional<uint64_t> getIrrLoopHeaderWeight() const; 448 449 /// Returns true if the Order field of child Instructions is valid. 450 bool isInstrOrderValid() const { 451 return getBasicBlockBits().InstrOrderValid; 452 } 453 454 /// Mark instruction ordering invalid. Done on every instruction insert. 455 void invalidateOrders() { 456 validateInstrOrdering(); 457 BasicBlockBits Bits = getBasicBlockBits(); 458 Bits.InstrOrderValid = false; 459 setBasicBlockBits(Bits); 460 } 461 462 /// Renumber instructions and mark the ordering as valid. 463 void renumberInstructions(); 464 465 /// Asserts that instruction order numbers are marked invalid, or that they 466 /// are in ascending order. This is constant time if the ordering is invalid, 467 /// and linear in the number of instructions if the ordering is valid. Callers 468 /// should be careful not to call this in ways that make common operations 469 /// O(n^2). For example, it takes O(n) time to assign order numbers to 470 /// instructions, so the order should be validated no more than once after 471 /// each ordering to ensure that transforms have the same algorithmic 472 /// complexity when asserts are enabled as when they are disabled. 473 void validateInstrOrdering() const; 474 475 private: 476 #if defined(_AIX) && (!defined(__GNUC__) || defined(__ibmxl__)) 477 // Except for GCC; by default, AIX compilers store bit-fields in 4-byte words 478 // and give the `pack` pragma push semantics. 479 #define BEGIN_TWO_BYTE_PACK() _Pragma("pack(2)") 480 #define END_TWO_BYTE_PACK() _Pragma("pack(pop)") 481 #else 482 #define BEGIN_TWO_BYTE_PACK() 483 #define END_TWO_BYTE_PACK() 484 #endif 485 486 BEGIN_TWO_BYTE_PACK() 487 /// Bitfield to help interpret the bits in Value::SubclassData. 488 struct BasicBlockBits { 489 unsigned short BlockAddressRefCount : 15; 490 unsigned short InstrOrderValid : 1; 491 }; 492 END_TWO_BYTE_PACK() 493 494 #undef BEGIN_TWO_BYTE_PACK 495 #undef END_TWO_BYTE_PACK 496 497 /// Safely reinterpret the subclass data bits to a more useful form. 498 BasicBlockBits getBasicBlockBits() const { 499 static_assert(sizeof(BasicBlockBits) == sizeof(unsigned short), 500 "too many bits for Value::SubclassData"); 501 unsigned short ValueData = getSubclassDataFromValue(); 502 BasicBlockBits AsBits; 503 memcpy(&AsBits, &ValueData, sizeof(AsBits)); 504 return AsBits; 505 } 506 507 /// Reinterpret our subclass bits and store them back into Value. 508 void setBasicBlockBits(BasicBlockBits AsBits) { 509 unsigned short D; 510 memcpy(&D, &AsBits, sizeof(D)); 511 Value::setValueSubclassData(D); 512 } 513 514 /// Increment the internal refcount of the number of BlockAddresses 515 /// referencing this BasicBlock by \p Amt. 516 /// 517 /// This is almost always 0, sometimes one possibly, but almost never 2, and 518 /// inconceivably 3 or more. 519 void AdjustBlockAddressRefCount(int Amt) { 520 BasicBlockBits Bits = getBasicBlockBits(); 521 Bits.BlockAddressRefCount += Amt; 522 setBasicBlockBits(Bits); 523 assert(Bits.BlockAddressRefCount < 255 && "Refcount wrap-around"); 524 } 525 526 /// Shadow Value::setValueSubclassData with a private forwarding method so 527 /// that any future subclasses cannot accidentally use it. 528 void setValueSubclassData(unsigned short D) { 529 Value::setValueSubclassData(D); 530 } 531 }; 532 533 // Create wrappers for C Binding types (see CBindingWrapping.h). 534 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(BasicBlock, LLVMBasicBlockRef) 535 536 /// Advance \p It while it points to a debug instruction and return the result. 537 /// This assumes that \p It is not at the end of a block. 538 BasicBlock::iterator skipDebugIntrinsics(BasicBlock::iterator It); 539 540 #ifdef NDEBUG 541 /// In release builds, this is a no-op. For !NDEBUG builds, the checks are 542 /// implemented in the .cpp file to avoid circular header deps. 543 inline void BasicBlock::validateInstrOrdering() const {} 544 #endif 545 546 } // end namespace llvm 547 548 #endif // LLVM_IR_BASICBLOCK_H 549