Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame^] | 1 | //===- llvm/BasicBlock.h - Represent a basic block in the VM ----*- 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 | // This file contains the declaration of the BasicBlock class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_IR_BASICBLOCK_H |
| 15 | #define LLVM_IR_BASICBLOCK_H |
| 16 | |
| 17 | #include "llvm-c/Types.h" |
| 18 | #include "llvm/ADT/Twine.h" |
| 19 | #include "llvm/ADT/ilist.h" |
| 20 | #include "llvm/ADT/ilist_node.h" |
| 21 | #include "llvm/ADT/iterator.h" |
| 22 | #include "llvm/ADT/iterator_range.h" |
| 23 | #include "llvm/IR/Instruction.h" |
| 24 | #include "llvm/IR/SymbolTableListTraits.h" |
| 25 | #include "llvm/IR/Value.h" |
| 26 | #include "llvm/Support/CBindingWrapping.h" |
| 27 | #include "llvm/Support/Casting.h" |
| 28 | #include "llvm/Support/Compiler.h" |
| 29 | #include <cassert> |
| 30 | #include <cstddef> |
| 31 | #include <iterator> |
| 32 | |
| 33 | namespace llvm { |
| 34 | |
| 35 | class CallInst; |
| 36 | class Function; |
| 37 | class LandingPadInst; |
| 38 | class LLVMContext; |
| 39 | class Module; |
| 40 | class PHINode; |
| 41 | class TerminatorInst; |
| 42 | class ValueSymbolTable; |
| 43 | |
| 44 | /// \brief LLVM Basic Block Representation |
| 45 | /// |
| 46 | /// This represents a single basic block in LLVM. A basic block is simply a |
| 47 | /// container of instructions that execute sequentially. Basic blocks are Values |
| 48 | /// because they are referenced by instructions such as branches and switch |
| 49 | /// tables. The type of a BasicBlock is "Type::LabelTy" because the basic block |
| 50 | /// represents a label to which a branch can jump. |
| 51 | /// |
| 52 | /// A well formed basic block is formed of a list of non-terminating |
| 53 | /// instructions followed by a single TerminatorInst instruction. |
| 54 | /// TerminatorInst's may not occur in the middle of basic blocks, and must |
| 55 | /// terminate the blocks. The BasicBlock class allows malformed basic blocks to |
| 56 | /// occur because it may be useful in the intermediate stage of constructing or |
| 57 | /// modifying a program. However, the verifier will ensure that basic blocks |
| 58 | /// are "well formed". |
| 59 | class BasicBlock final : public Value, // Basic blocks are data objects also |
| 60 | public ilist_node_with_parent<BasicBlock, Function> { |
| 61 | public: |
| 62 | using InstListType = SymbolTableList<Instruction>; |
| 63 | |
| 64 | private: |
| 65 | friend class BlockAddress; |
| 66 | friend class SymbolTableListTraits<BasicBlock>; |
| 67 | |
| 68 | InstListType InstList; |
| 69 | Function *Parent; |
| 70 | |
| 71 | void setParent(Function *parent); |
| 72 | |
| 73 | /// \brief Constructor. |
| 74 | /// |
| 75 | /// If the function parameter is specified, the basic block is automatically |
| 76 | /// inserted at either the end of the function (if InsertBefore is null), or |
| 77 | /// before the specified basic block. |
| 78 | explicit BasicBlock(LLVMContext &C, const Twine &Name = "", |
| 79 | Function *Parent = nullptr, |
| 80 | BasicBlock *InsertBefore = nullptr); |
| 81 | |
| 82 | public: |
| 83 | BasicBlock(const BasicBlock &) = delete; |
| 84 | BasicBlock &operator=(const BasicBlock &) = delete; |
| 85 | ~BasicBlock(); |
| 86 | |
| 87 | /// \brief Get the context in which this basic block lives. |
| 88 | LLVMContext &getContext() const; |
| 89 | |
| 90 | /// Instruction iterators... |
| 91 | using iterator = InstListType::iterator; |
| 92 | using const_iterator = InstListType::const_iterator; |
| 93 | using reverse_iterator = InstListType::reverse_iterator; |
| 94 | using const_reverse_iterator = InstListType::const_reverse_iterator; |
| 95 | |
| 96 | /// \brief Creates a new BasicBlock. |
| 97 | /// |
| 98 | /// If the Parent parameter is specified, the basic block is automatically |
| 99 | /// inserted at either the end of the function (if InsertBefore is 0), or |
| 100 | /// before the specified basic block. |
| 101 | static BasicBlock *Create(LLVMContext &Context, const Twine &Name = "", |
| 102 | Function *Parent = nullptr, |
| 103 | BasicBlock *InsertBefore = nullptr) { |
| 104 | return new BasicBlock(Context, Name, Parent, InsertBefore); |
| 105 | } |
| 106 | |
| 107 | /// \brief Return the enclosing method, or null if none. |
| 108 | const Function *getParent() const { return Parent; } |
| 109 | Function *getParent() { return Parent; } |
| 110 | |
| 111 | /// \brief Return the module owning the function this basic block belongs to, |
| 112 | /// or nullptr it the function does not have a module. |
| 113 | /// |
| 114 | /// Note: this is undefined behavior if the block does not have a parent. |
| 115 | const Module *getModule() const; |
| 116 | Module *getModule() { |
| 117 | return const_cast<Module *>( |
| 118 | static_cast<const BasicBlock *>(this)->getModule()); |
| 119 | } |
| 120 | |
| 121 | /// \brief Returns the terminator instruction if the block is well formed or |
| 122 | /// null if the block is not well formed. |
| 123 | const TerminatorInst *getTerminator() const LLVM_READONLY; |
| 124 | TerminatorInst *getTerminator() { |
| 125 | return const_cast<TerminatorInst *>( |
| 126 | static_cast<const BasicBlock *>(this)->getTerminator()); |
| 127 | } |
| 128 | |
| 129 | /// \brief Returns the call instruction calling @llvm.experimental.deoptimize |
| 130 | /// prior to the terminating return instruction of this basic block, if such a |
| 131 | /// call is present. Otherwise, returns null. |
| 132 | const CallInst *getTerminatingDeoptimizeCall() const; |
| 133 | CallInst *getTerminatingDeoptimizeCall() { |
| 134 | return const_cast<CallInst *>( |
| 135 | static_cast<const BasicBlock *>(this)->getTerminatingDeoptimizeCall()); |
| 136 | } |
| 137 | |
| 138 | /// \brief Returns the call instruction marked 'musttail' prior to the |
| 139 | /// terminating return instruction of this basic block, if such a call is |
| 140 | /// present. Otherwise, returns null. |
| 141 | const CallInst *getTerminatingMustTailCall() const; |
| 142 | CallInst *getTerminatingMustTailCall() { |
| 143 | return const_cast<CallInst *>( |
| 144 | static_cast<const BasicBlock *>(this)->getTerminatingMustTailCall()); |
| 145 | } |
| 146 | |
| 147 | /// \brief Returns a pointer to the first instruction in this block that is |
| 148 | /// not a PHINode instruction. |
| 149 | /// |
| 150 | /// When adding instructions to the beginning of the basic block, they should |
| 151 | /// be added before the returned value, not before the first instruction, |
| 152 | /// which might be PHI. Returns 0 is there's no non-PHI instruction. |
| 153 | const Instruction* getFirstNonPHI() const; |
| 154 | Instruction* getFirstNonPHI() { |
| 155 | return const_cast<Instruction *>( |
| 156 | static_cast<const BasicBlock *>(this)->getFirstNonPHI()); |
| 157 | } |
| 158 | |
| 159 | /// \brief Returns a pointer to the first instruction in this block that is not |
| 160 | /// a PHINode or a debug intrinsic. |
| 161 | const Instruction* getFirstNonPHIOrDbg() const; |
| 162 | Instruction* getFirstNonPHIOrDbg() { |
| 163 | return const_cast<Instruction *>( |
| 164 | static_cast<const BasicBlock *>(this)->getFirstNonPHIOrDbg()); |
| 165 | } |
| 166 | |
| 167 | /// \brief Returns a pointer to the first instruction in this block that is not |
| 168 | /// a PHINode, a debug intrinsic, or a lifetime intrinsic. |
| 169 | const Instruction* getFirstNonPHIOrDbgOrLifetime() const; |
| 170 | Instruction* getFirstNonPHIOrDbgOrLifetime() { |
| 171 | return const_cast<Instruction *>( |
| 172 | static_cast<const BasicBlock *>(this)->getFirstNonPHIOrDbgOrLifetime()); |
| 173 | } |
| 174 | |
| 175 | /// \brief Returns an iterator to the first instruction in this block that is |
| 176 | /// suitable for inserting a non-PHI instruction. |
| 177 | /// |
| 178 | /// In particular, it skips all PHIs and LandingPad instructions. |
| 179 | const_iterator getFirstInsertionPt() const; |
| 180 | iterator getFirstInsertionPt() { |
| 181 | return static_cast<const BasicBlock *>(this) |
| 182 | ->getFirstInsertionPt().getNonConst(); |
| 183 | } |
| 184 | |
| 185 | /// \brief Unlink 'this' from the containing function, but do not delete it. |
| 186 | void removeFromParent(); |
| 187 | |
| 188 | /// \brief Unlink 'this' from the containing function and delete it. |
| 189 | /// |
| 190 | // \returns an iterator pointing to the element after the erased one. |
| 191 | SymbolTableList<BasicBlock>::iterator eraseFromParent(); |
| 192 | |
| 193 | /// \brief Unlink this basic block from its current function and insert it |
| 194 | /// into the function that \p MovePos lives in, right before \p MovePos. |
| 195 | void moveBefore(BasicBlock *MovePos); |
| 196 | |
| 197 | /// \brief Unlink this basic block from its current function and insert it |
| 198 | /// right after \p MovePos in the function \p MovePos lives in. |
| 199 | void moveAfter(BasicBlock *MovePos); |
| 200 | |
| 201 | /// \brief Insert unlinked basic block into a function. |
| 202 | /// |
| 203 | /// Inserts an unlinked basic block into \c Parent. If \c InsertBefore is |
| 204 | /// provided, inserts before that basic block, otherwise inserts at the end. |
| 205 | /// |
| 206 | /// \pre \a getParent() is \c nullptr. |
| 207 | void insertInto(Function *Parent, BasicBlock *InsertBefore = nullptr); |
| 208 | |
| 209 | /// \brief Return the predecessor of this block if it has a single predecessor |
| 210 | /// block. Otherwise return a null pointer. |
| 211 | const BasicBlock *getSinglePredecessor() const; |
| 212 | BasicBlock *getSinglePredecessor() { |
| 213 | return const_cast<BasicBlock *>( |
| 214 | static_cast<const BasicBlock *>(this)->getSinglePredecessor()); |
| 215 | } |
| 216 | |
| 217 | /// \brief Return the predecessor of this block if it has a unique predecessor |
| 218 | /// block. Otherwise return a null pointer. |
| 219 | /// |
| 220 | /// Note that unique predecessor doesn't mean single edge, there can be |
| 221 | /// multiple edges from the unique predecessor to this block (for example a |
| 222 | /// switch statement with multiple cases having the same destination). |
| 223 | const BasicBlock *getUniquePredecessor() const; |
| 224 | BasicBlock *getUniquePredecessor() { |
| 225 | return const_cast<BasicBlock *>( |
| 226 | static_cast<const BasicBlock *>(this)->getUniquePredecessor()); |
| 227 | } |
| 228 | |
| 229 | /// \brief Return the successor of this block if it has a single successor. |
| 230 | /// Otherwise return a null pointer. |
| 231 | /// |
| 232 | /// This method is analogous to getSinglePredecessor above. |
| 233 | const BasicBlock *getSingleSuccessor() const; |
| 234 | BasicBlock *getSingleSuccessor() { |
| 235 | return const_cast<BasicBlock *>( |
| 236 | static_cast<const BasicBlock *>(this)->getSingleSuccessor()); |
| 237 | } |
| 238 | |
| 239 | /// \brief Return the successor of this block if it has a unique successor. |
| 240 | /// Otherwise return a null pointer. |
| 241 | /// |
| 242 | /// This method is analogous to getUniquePredecessor above. |
| 243 | const BasicBlock *getUniqueSuccessor() const; |
| 244 | BasicBlock *getUniqueSuccessor() { |
| 245 | return const_cast<BasicBlock *>( |
| 246 | static_cast<const BasicBlock *>(this)->getUniqueSuccessor()); |
| 247 | } |
| 248 | |
| 249 | //===--------------------------------------------------------------------===// |
| 250 | /// Instruction iterator methods |
| 251 | /// |
| 252 | inline iterator begin() { return InstList.begin(); } |
| 253 | inline const_iterator begin() const { return InstList.begin(); } |
| 254 | inline iterator end () { return InstList.end(); } |
| 255 | inline const_iterator end () const { return InstList.end(); } |
| 256 | |
| 257 | inline reverse_iterator rbegin() { return InstList.rbegin(); } |
| 258 | inline const_reverse_iterator rbegin() const { return InstList.rbegin(); } |
| 259 | inline reverse_iterator rend () { return InstList.rend(); } |
| 260 | inline const_reverse_iterator rend () const { return InstList.rend(); } |
| 261 | |
| 262 | inline size_t size() const { return InstList.size(); } |
| 263 | inline bool empty() const { return InstList.empty(); } |
| 264 | inline const Instruction &front() const { return InstList.front(); } |
| 265 | inline Instruction &front() { return InstList.front(); } |
| 266 | inline const Instruction &back() const { return InstList.back(); } |
| 267 | inline Instruction &back() { return InstList.back(); } |
| 268 | |
| 269 | /// Iterator to walk just the phi nodes in the basic block. |
| 270 | template <typename PHINodeT = PHINode, typename BBIteratorT = iterator> |
| 271 | class phi_iterator_impl |
| 272 | : public iterator_facade_base<phi_iterator_impl<PHINodeT, BBIteratorT>, |
| 273 | std::forward_iterator_tag, PHINodeT> { |
| 274 | friend BasicBlock; |
| 275 | |
| 276 | PHINodeT *PN; |
| 277 | |
| 278 | phi_iterator_impl(PHINodeT *PN) : PN(PN) {} |
| 279 | |
| 280 | public: |
| 281 | // Allow default construction to build variables, but this doesn't build |
| 282 | // a useful iterator. |
| 283 | phi_iterator_impl() = default; |
| 284 | |
| 285 | // Allow conversion between instantiations where valid. |
| 286 | template <typename PHINodeU, typename BBIteratorU> |
| 287 | phi_iterator_impl(const phi_iterator_impl<PHINodeU, BBIteratorU> &Arg) |
| 288 | : PN(Arg.PN) {} |
| 289 | |
| 290 | bool operator==(const phi_iterator_impl &Arg) const { return PN == Arg.PN; } |
| 291 | |
| 292 | PHINodeT &operator*() const { return *PN; } |
| 293 | |
| 294 | using phi_iterator_impl::iterator_facade_base::operator++; |
| 295 | phi_iterator_impl &operator++() { |
| 296 | assert(PN && "Cannot increment the end iterator!"); |
| 297 | PN = dyn_cast<PHINodeT>(std::next(BBIteratorT(PN))); |
| 298 | return *this; |
| 299 | } |
| 300 | }; |
| 301 | using phi_iterator = phi_iterator_impl<>; |
| 302 | using const_phi_iterator = |
| 303 | phi_iterator_impl<const PHINode, BasicBlock::const_iterator>; |
| 304 | |
| 305 | /// Returns a range that iterates over the phis in the basic block. |
| 306 | /// |
| 307 | /// Note that this cannot be used with basic blocks that have no terminator. |
| 308 | iterator_range<const_phi_iterator> phis() const { |
| 309 | return const_cast<BasicBlock *>(this)->phis(); |
| 310 | } |
| 311 | iterator_range<phi_iterator> phis(); |
| 312 | |
| 313 | /// \brief Return the underlying instruction list container. |
| 314 | /// |
| 315 | /// Currently you need to access the underlying instruction list container |
| 316 | /// directly if you want to modify it. |
| 317 | const InstListType &getInstList() const { return InstList; } |
| 318 | InstListType &getInstList() { return InstList; } |
| 319 | |
| 320 | /// \brief Returns a pointer to a member of the instruction list. |
| 321 | static InstListType BasicBlock::*getSublistAccess(Instruction*) { |
| 322 | return &BasicBlock::InstList; |
| 323 | } |
| 324 | |
| 325 | /// \brief Returns a pointer to the symbol table if one exists. |
| 326 | ValueSymbolTable *getValueSymbolTable(); |
| 327 | |
| 328 | /// \brief Methods for support type inquiry through isa, cast, and dyn_cast. |
| 329 | static bool classof(const Value *V) { |
| 330 | return V->getValueID() == Value::BasicBlockVal; |
| 331 | } |
| 332 | |
| 333 | /// \brief Cause all subinstructions to "let go" of all the references that |
| 334 | /// said subinstructions are maintaining. |
| 335 | /// |
| 336 | /// This allows one to 'delete' a whole class at a time, even though there may |
| 337 | /// be circular references... first all references are dropped, and all use |
| 338 | /// counts go to zero. Then everything is delete'd for real. Note that no |
| 339 | /// operations are valid on an object that has "dropped all references", |
| 340 | /// except operator delete. |
| 341 | void dropAllReferences(); |
| 342 | |
| 343 | /// \brief Notify the BasicBlock that the predecessor \p Pred is no longer |
| 344 | /// able to reach it. |
| 345 | /// |
| 346 | /// This is actually not used to update the Predecessor list, but is actually |
| 347 | /// used to update the PHI nodes that reside in the block. Note that this |
| 348 | /// should be called while the predecessor still refers to this block. |
| 349 | void removePredecessor(BasicBlock *Pred, bool DontDeleteUselessPHIs = false); |
| 350 | |
| 351 | bool canSplitPredecessors() const; |
| 352 | |
| 353 | /// \brief Split the basic block into two basic blocks at the specified |
| 354 | /// instruction. |
| 355 | /// |
| 356 | /// Note that all instructions BEFORE the specified iterator stay as part of |
| 357 | /// the original basic block, an unconditional branch is added to the original |
| 358 | /// BB, and the rest of the instructions in the BB are moved to the new BB, |
| 359 | /// including the old terminator. The newly formed BasicBlock is returned. |
| 360 | /// This function invalidates the specified iterator. |
| 361 | /// |
| 362 | /// Note that this only works on well formed basic blocks (must have a |
| 363 | /// terminator), and 'I' must not be the end of instruction list (which would |
| 364 | /// cause a degenerate basic block to be formed, having a terminator inside of |
| 365 | /// the basic block). |
| 366 | /// |
| 367 | /// Also note that this doesn't preserve any passes. To split blocks while |
| 368 | /// keeping loop information consistent, use the SplitBlock utility function. |
| 369 | BasicBlock *splitBasicBlock(iterator I, const Twine &BBName = ""); |
| 370 | BasicBlock *splitBasicBlock(Instruction *I, const Twine &BBName = "") { |
| 371 | return splitBasicBlock(I->getIterator(), BBName); |
| 372 | } |
| 373 | |
| 374 | /// \brief Returns true if there are any uses of this basic block other than |
| 375 | /// direct branches, switches, etc. to it. |
| 376 | bool hasAddressTaken() const { return getSubclassDataFromValue() != 0; } |
| 377 | |
| 378 | /// \brief Update all phi nodes in this basic block's successors to refer to |
| 379 | /// basic block \p New instead of to it. |
| 380 | void replaceSuccessorsPhiUsesWith(BasicBlock *New); |
| 381 | |
| 382 | /// \brief Return true if this basic block is an exception handling block. |
| 383 | bool isEHPad() const { return getFirstNonPHI()->isEHPad(); } |
| 384 | |
| 385 | /// \brief Return true if this basic block is a landing pad. |
| 386 | /// |
| 387 | /// Being a ``landing pad'' means that the basic block is the destination of |
| 388 | /// the 'unwind' edge of an invoke instruction. |
| 389 | bool isLandingPad() const; |
| 390 | |
| 391 | /// \brief Return the landingpad instruction associated with the landing pad. |
| 392 | const LandingPadInst *getLandingPadInst() const; |
| 393 | LandingPadInst *getLandingPadInst() { |
| 394 | return const_cast<LandingPadInst *>( |
| 395 | static_cast<const BasicBlock *>(this)->getLandingPadInst()); |
| 396 | } |
| 397 | |
| 398 | /// \brief Return true if it is legal to hoist instructions into this block. |
| 399 | bool isLegalToHoistInto() const; |
| 400 | |
| 401 | Optional<uint64_t> getIrrLoopHeaderWeight() const; |
| 402 | |
| 403 | private: |
| 404 | /// \brief Increment the internal refcount of the number of BlockAddresses |
| 405 | /// referencing this BasicBlock by \p Amt. |
| 406 | /// |
| 407 | /// This is almost always 0, sometimes one possibly, but almost never 2, and |
| 408 | /// inconceivably 3 or more. |
| 409 | void AdjustBlockAddressRefCount(int Amt) { |
| 410 | setValueSubclassData(getSubclassDataFromValue()+Amt); |
| 411 | assert((int)(signed char)getSubclassDataFromValue() >= 0 && |
| 412 | "Refcount wrap-around"); |
| 413 | } |
| 414 | |
| 415 | /// \brief Shadow Value::setValueSubclassData with a private forwarding method |
| 416 | /// so that any future subclasses cannot accidentally use it. |
| 417 | void setValueSubclassData(unsigned short D) { |
| 418 | Value::setValueSubclassData(D); |
| 419 | } |
| 420 | }; |
| 421 | |
| 422 | // Create wrappers for C Binding types (see CBindingWrapping.h). |
| 423 | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(BasicBlock, LLVMBasicBlockRef) |
| 424 | |
| 425 | } // end namespace llvm |
| 426 | |
| 427 | #endif // LLVM_IR_BASICBLOCK_H |