xref: /freebsd/contrib/llvm-project/llvm/include/llvm/IR/BasicBlock.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===- llvm/BasicBlock.h - Represent a basic block in the VM ----*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file contains the declaration of the BasicBlock class.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #ifndef LLVM_IR_BASICBLOCK_H
140b57cec5SDimitry Andric #define LLVM_IR_BASICBLOCK_H
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric #include "llvm-c/Types.h"
17*0fca6ea1SDimitry Andric #include "llvm/ADT/DenseMap.h"
180b57cec5SDimitry Andric #include "llvm/ADT/Twine.h"
190b57cec5SDimitry Andric #include "llvm/ADT/ilist.h"
200b57cec5SDimitry Andric #include "llvm/ADT/ilist_node.h"
210b57cec5SDimitry Andric #include "llvm/ADT/iterator.h"
220b57cec5SDimitry Andric #include "llvm/ADT/iterator_range.h"
235f757f3fSDimitry Andric #include "llvm/IR/DebugProgramInstruction.h"
240b57cec5SDimitry Andric #include "llvm/IR/Instruction.h"
250b57cec5SDimitry Andric #include "llvm/IR/SymbolTableListTraits.h"
260b57cec5SDimitry Andric #include "llvm/IR/Value.h"
270b57cec5SDimitry Andric #include <cassert>
280b57cec5SDimitry Andric #include <cstddef>
290b57cec5SDimitry Andric #include <iterator>
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric namespace llvm {
320b57cec5SDimitry Andric 
335ffd83dbSDimitry Andric class AssemblyAnnotationWriter;
340b57cec5SDimitry Andric class CallInst;
35*0fca6ea1SDimitry Andric class DataLayout;
360b57cec5SDimitry Andric class Function;
370b57cec5SDimitry Andric class LandingPadInst;
380b57cec5SDimitry Andric class LLVMContext;
390b57cec5SDimitry Andric class Module;
400b57cec5SDimitry Andric class PHINode;
410b57cec5SDimitry Andric class ValueSymbolTable;
42*0fca6ea1SDimitry Andric class DbgVariableRecord;
43*0fca6ea1SDimitry Andric class DbgMarker;
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric /// LLVM Basic Block Representation
460b57cec5SDimitry Andric ///
470b57cec5SDimitry Andric /// This represents a single basic block in LLVM. A basic block is simply a
480b57cec5SDimitry Andric /// container of instructions that execute sequentially. Basic blocks are Values
490b57cec5SDimitry Andric /// because they are referenced by instructions such as branches and switch
500b57cec5SDimitry Andric /// tables. The type of a BasicBlock is "Type::LabelTy" because the basic block
510b57cec5SDimitry Andric /// represents a label to which a branch can jump.
520b57cec5SDimitry Andric ///
530b57cec5SDimitry Andric /// A well formed basic block is formed of a list of non-terminating
540b57cec5SDimitry Andric /// instructions followed by a single terminator instruction. Terminator
550b57cec5SDimitry Andric /// instructions may not occur in the middle of basic blocks, and must terminate
560b57cec5SDimitry Andric /// the blocks. The BasicBlock class allows malformed basic blocks to occur
570b57cec5SDimitry Andric /// because it may be useful in the intermediate stage of constructing or
580b57cec5SDimitry Andric /// modifying a program. However, the verifier will ensure that basic blocks are
590b57cec5SDimitry Andric /// "well formed".
600b57cec5SDimitry Andric class BasicBlock final : public Value, // Basic blocks are data objects also
610b57cec5SDimitry Andric                          public ilist_node_with_parent<BasicBlock, Function> {
620b57cec5SDimitry Andric public:
63*0fca6ea1SDimitry Andric   using InstListType = SymbolTableList<Instruction, ilist_iterator_bits<true>,
64*0fca6ea1SDimitry Andric                                        ilist_parent<BasicBlock>>;
655f757f3fSDimitry Andric   /// Flag recording whether or not this block stores debug-info in the form
665f757f3fSDimitry Andric   /// of intrinsic instructions (false) or non-instruction records (true).
675f757f3fSDimitry Andric   bool IsNewDbgInfoFormat;
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric private:
700b57cec5SDimitry Andric   friend class BlockAddress;
710b57cec5SDimitry Andric   friend class SymbolTableListTraits<BasicBlock>;
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric   InstListType InstList;
740b57cec5SDimitry Andric   Function *Parent;
750b57cec5SDimitry Andric 
765f757f3fSDimitry Andric public:
77*0fca6ea1SDimitry Andric   /// Attach a DbgMarker to the given instruction. Enables the storage of any
785f757f3fSDimitry Andric   /// debug-info at this position in the program.
79*0fca6ea1SDimitry Andric   DbgMarker *createMarker(Instruction *I);
80*0fca6ea1SDimitry Andric   DbgMarker *createMarker(InstListType::iterator It);
815f757f3fSDimitry Andric 
825f757f3fSDimitry Andric   /// Convert variable location debugging information stored in dbg.value
83*0fca6ea1SDimitry Andric   /// intrinsics into DbgMarkers / DbgRecords. Deletes all dbg.values in
845f757f3fSDimitry Andric   /// the process and sets IsNewDbgInfoFormat = true. Only takes effect if
855f757f3fSDimitry Andric   /// the UseNewDbgInfoFormat LLVM command line option is given.
865f757f3fSDimitry Andric   void convertToNewDbgValues();
875f757f3fSDimitry Andric 
88*0fca6ea1SDimitry Andric   /// Convert variable location debugging information stored in DbgMarkers and
89*0fca6ea1SDimitry Andric   /// DbgRecords into the dbg.value intrinsic representation. Sets
905f757f3fSDimitry Andric   /// IsNewDbgInfoFormat = false.
915f757f3fSDimitry Andric   void convertFromNewDbgValues();
925f757f3fSDimitry Andric 
935f757f3fSDimitry Andric   /// Ensure the block is in "old" dbg.value format (\p NewFlag == false) or
945f757f3fSDimitry Andric   /// in the new format (\p NewFlag == true), converting to the desired format
955f757f3fSDimitry Andric   /// if necessary.
965f757f3fSDimitry Andric   void setIsNewDbgInfoFormat(bool NewFlag);
97*0fca6ea1SDimitry Andric   void setNewDbgInfoFormatFlag(bool NewFlag);
985f757f3fSDimitry Andric 
99*0fca6ea1SDimitry Andric   /// Record that the collection of DbgRecords in \p M "trails" after the last
1005f757f3fSDimitry Andric   /// instruction of this block. These are equivalent to dbg.value intrinsics
1015f757f3fSDimitry Andric   /// that exist at the end of a basic block with no terminator (a transient
1025f757f3fSDimitry Andric   /// state that occurs regularly).
103*0fca6ea1SDimitry Andric   void setTrailingDbgRecords(DbgMarker *M);
1045f757f3fSDimitry Andric 
105*0fca6ea1SDimitry Andric   /// Fetch the collection of DbgRecords that "trail" after the last instruction
106*0fca6ea1SDimitry Andric   /// of this block, see \ref setTrailingDbgRecords. If there are none, returns
1075f757f3fSDimitry Andric   /// nullptr.
108*0fca6ea1SDimitry Andric   DbgMarker *getTrailingDbgRecords();
1095f757f3fSDimitry Andric 
110*0fca6ea1SDimitry Andric   /// Delete any trailing DbgRecords at the end of this block, see
111*0fca6ea1SDimitry Andric   /// \ref setTrailingDbgRecords.
112*0fca6ea1SDimitry Andric   void deleteTrailingDbgRecords();
1135f757f3fSDimitry Andric 
1145f757f3fSDimitry Andric   void dumpDbgValues() const;
1155f757f3fSDimitry Andric 
116*0fca6ea1SDimitry Andric   /// Return the DbgMarker for the position given by \p It, so that DbgRecords
117*0fca6ea1SDimitry Andric   /// can be inserted there. This will either be nullptr if not present, a
118*0fca6ea1SDimitry Andric   /// DbgMarker, or TrailingDbgRecords if It is end().
119*0fca6ea1SDimitry Andric   DbgMarker *getMarker(InstListType::iterator It);
1205f757f3fSDimitry Andric 
121*0fca6ea1SDimitry Andric   /// Return the DbgMarker for the position that comes after \p I. \see
122*0fca6ea1SDimitry Andric   /// BasicBlock::getMarker, this can be nullptr, a DbgMarker, or
123*0fca6ea1SDimitry Andric   /// TrailingDbgRecords if there is no next instruction.
124*0fca6ea1SDimitry Andric   DbgMarker *getNextMarker(Instruction *I);
1255f757f3fSDimitry Andric 
126*0fca6ea1SDimitry Andric   /// Insert a DbgRecord into a block at the position given by \p I.
127*0fca6ea1SDimitry Andric   void insertDbgRecordAfter(DbgRecord *DR, Instruction *I);
1285f757f3fSDimitry Andric 
129*0fca6ea1SDimitry Andric   /// Insert a DbgRecord into a block at the position given by \p Here.
130*0fca6ea1SDimitry Andric   void insertDbgRecordBefore(DbgRecord *DR, InstListType::iterator Here);
1315f757f3fSDimitry Andric 
132*0fca6ea1SDimitry Andric   /// Eject any debug-info trailing at the end of a block. DbgRecords can
1335f757f3fSDimitry Andric   /// transiently be located "off the end" of a block if the blocks terminator
1345f757f3fSDimitry Andric   /// is temporarily removed. Once a terminator is re-inserted this method will
135*0fca6ea1SDimitry Andric   /// move such DbgRecords back to the right place (ahead of the terminator).
136*0fca6ea1SDimitry Andric   void flushTerminatorDbgRecords();
1375f757f3fSDimitry Andric 
1385f757f3fSDimitry Andric   /// In rare circumstances instructions can be speculatively removed from
1395f757f3fSDimitry Andric   /// blocks, and then be re-inserted back into that position later. When this
1405f757f3fSDimitry Andric   /// happens in RemoveDIs debug-info mode, some special patching-up needs to
1415f757f3fSDimitry Andric   /// occur: inserting into the middle of a sequence of dbg.value intrinsics
142*0fca6ea1SDimitry Andric   /// does not have an equivalent with DbgRecords.
143*0fca6ea1SDimitry Andric   void reinsertInstInDbgRecords(Instruction *I,
144*0fca6ea1SDimitry Andric                                 std::optional<DbgRecord::self_iterator> Pos);
1455f757f3fSDimitry Andric 
1465f757f3fSDimitry Andric private:
1470b57cec5SDimitry Andric   void setParent(Function *parent);
1480b57cec5SDimitry Andric 
1490b57cec5SDimitry Andric   /// Constructor.
1500b57cec5SDimitry Andric   ///
1510b57cec5SDimitry Andric   /// If the function parameter is specified, the basic block is automatically
1520b57cec5SDimitry Andric   /// inserted at either the end of the function (if InsertBefore is null), or
1530b57cec5SDimitry Andric   /// before the specified basic block.
1540b57cec5SDimitry Andric   explicit BasicBlock(LLVMContext &C, const Twine &Name = "",
1550b57cec5SDimitry Andric                       Function *Parent = nullptr,
1560b57cec5SDimitry Andric                       BasicBlock *InsertBefore = nullptr);
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric public:
1590b57cec5SDimitry Andric   BasicBlock(const BasicBlock &) = delete;
1600b57cec5SDimitry Andric   BasicBlock &operator=(const BasicBlock &) = delete;
1610b57cec5SDimitry Andric   ~BasicBlock();
1620b57cec5SDimitry Andric 
1630b57cec5SDimitry Andric   /// Get the context in which this basic block lives.
1640b57cec5SDimitry Andric   LLVMContext &getContext() const;
1650b57cec5SDimitry Andric 
1660b57cec5SDimitry Andric   /// Instruction iterators...
1670b57cec5SDimitry Andric   using iterator = InstListType::iterator;
1680b57cec5SDimitry Andric   using const_iterator = InstListType::const_iterator;
1690b57cec5SDimitry Andric   using reverse_iterator = InstListType::reverse_iterator;
1700b57cec5SDimitry Andric   using const_reverse_iterator = InstListType::const_reverse_iterator;
1710b57cec5SDimitry Andric 
172bdd1243dSDimitry Andric   // These functions and classes need access to the instruction list.
173bdd1243dSDimitry Andric   friend void Instruction::removeFromParent();
1745f757f3fSDimitry Andric   friend BasicBlock::iterator Instruction::eraseFromParent();
175bdd1243dSDimitry Andric   friend BasicBlock::iterator Instruction::insertInto(BasicBlock *BB,
176bdd1243dSDimitry Andric                                                       BasicBlock::iterator It);
177*0fca6ea1SDimitry Andric   friend class llvm::SymbolTableListTraits<
178*0fca6ea1SDimitry Andric       llvm::Instruction, ilist_iterator_bits<true>, ilist_parent<BasicBlock>>;
1795f757f3fSDimitry Andric   friend class llvm::ilist_node_with_parent<llvm::Instruction, llvm::BasicBlock,
180*0fca6ea1SDimitry Andric                                             ilist_iterator_bits<true>,
181*0fca6ea1SDimitry Andric                                             ilist_parent<BasicBlock>>;
1825f757f3fSDimitry Andric 
1835f757f3fSDimitry Andric   // Friendly methods that need to access us for the maintenence of
1845f757f3fSDimitry Andric   // debug-info attachments.
1855f757f3fSDimitry Andric   friend void Instruction::insertBefore(BasicBlock::iterator InsertPos);
1865f757f3fSDimitry Andric   friend void Instruction::insertAfter(Instruction *InsertPos);
1875f757f3fSDimitry Andric   friend void Instruction::insertBefore(BasicBlock &BB,
1885f757f3fSDimitry Andric                                         InstListType::iterator InsertPos);
1895f757f3fSDimitry Andric   friend void Instruction::moveBeforeImpl(BasicBlock &BB,
1905f757f3fSDimitry Andric                                           InstListType::iterator I,
1915f757f3fSDimitry Andric                                           bool Preserve);
192*0fca6ea1SDimitry Andric   friend iterator_range<DbgRecord::self_iterator>
193*0fca6ea1SDimitry Andric   Instruction::cloneDebugInfoFrom(
194*0fca6ea1SDimitry Andric       const Instruction *From, std::optional<DbgRecord::self_iterator> FromHere,
1955f757f3fSDimitry Andric       bool InsertAtHead);
196bdd1243dSDimitry Andric 
1970b57cec5SDimitry Andric   /// Creates a new BasicBlock.
1980b57cec5SDimitry Andric   ///
1990b57cec5SDimitry Andric   /// If the Parent parameter is specified, the basic block is automatically
2000b57cec5SDimitry Andric   /// inserted at either the end of the function (if InsertBefore is 0), or
2010b57cec5SDimitry Andric   /// before the specified basic block.
2020b57cec5SDimitry Andric   static BasicBlock *Create(LLVMContext &Context, const Twine &Name = "",
2030b57cec5SDimitry Andric                             Function *Parent = nullptr,
2040b57cec5SDimitry Andric                             BasicBlock *InsertBefore = nullptr) {
2050b57cec5SDimitry Andric     return new BasicBlock(Context, Name, Parent, InsertBefore);
2060b57cec5SDimitry Andric   }
2070b57cec5SDimitry Andric 
2080b57cec5SDimitry Andric   /// Return the enclosing method, or null if none.
getParent()2090b57cec5SDimitry Andric   const Function *getParent() const { return Parent; }
getParent()2100b57cec5SDimitry Andric         Function *getParent()       { return Parent; }
2110b57cec5SDimitry Andric 
2120b57cec5SDimitry Andric   /// Return the module owning the function this basic block belongs to, or
2130b57cec5SDimitry Andric   /// nullptr if the function does not have a module.
2140b57cec5SDimitry Andric   ///
2150b57cec5SDimitry Andric   /// Note: this is undefined behavior if the block does not have a parent.
2160b57cec5SDimitry Andric   const Module *getModule() const;
getModule()2170b57cec5SDimitry Andric   Module *getModule() {
2180b57cec5SDimitry Andric     return const_cast<Module *>(
2190b57cec5SDimitry Andric                             static_cast<const BasicBlock *>(this)->getModule());
2200b57cec5SDimitry Andric   }
2210b57cec5SDimitry Andric 
222*0fca6ea1SDimitry Andric   /// Get the data layout of the module this basic block belongs to.
223*0fca6ea1SDimitry Andric   ///
224*0fca6ea1SDimitry Andric   /// Requires the basic block to have a parent module.
225*0fca6ea1SDimitry Andric   const DataLayout &getDataLayout() const;
226*0fca6ea1SDimitry Andric 
2270b57cec5SDimitry Andric   /// Returns the terminator instruction if the block is well formed or null
2280b57cec5SDimitry Andric   /// if the block is not well formed.
getTerminator()22981ad6265SDimitry Andric   const Instruction *getTerminator() const LLVM_READONLY {
23081ad6265SDimitry Andric     if (InstList.empty() || !InstList.back().isTerminator())
23181ad6265SDimitry Andric       return nullptr;
23281ad6265SDimitry Andric     return &InstList.back();
23381ad6265SDimitry Andric   }
getTerminator()2340b57cec5SDimitry Andric   Instruction *getTerminator() {
2350b57cec5SDimitry Andric     return const_cast<Instruction *>(
2360b57cec5SDimitry Andric         static_cast<const BasicBlock *>(this)->getTerminator());
2370b57cec5SDimitry Andric   }
2380b57cec5SDimitry Andric 
2390b57cec5SDimitry Andric   /// Returns the call instruction calling \@llvm.experimental.deoptimize
2400b57cec5SDimitry Andric   /// prior to the terminating return instruction of this basic block, if such
2410b57cec5SDimitry Andric   /// a call is present.  Otherwise, returns null.
2420b57cec5SDimitry Andric   const CallInst *getTerminatingDeoptimizeCall() const;
getTerminatingDeoptimizeCall()2430b57cec5SDimitry Andric   CallInst *getTerminatingDeoptimizeCall() {
2440b57cec5SDimitry Andric     return const_cast<CallInst *>(
2450b57cec5SDimitry Andric          static_cast<const BasicBlock *>(this)->getTerminatingDeoptimizeCall());
2460b57cec5SDimitry Andric   }
2470b57cec5SDimitry Andric 
2485ffd83dbSDimitry Andric   /// Returns the call instruction calling \@llvm.experimental.deoptimize
2495ffd83dbSDimitry Andric   /// that is present either in current basic block or in block that is a unique
2505ffd83dbSDimitry Andric   /// successor to current block, if such call is present. Otherwise, returns null.
2515ffd83dbSDimitry Andric   const CallInst *getPostdominatingDeoptimizeCall() const;
getPostdominatingDeoptimizeCall()2525ffd83dbSDimitry Andric   CallInst *getPostdominatingDeoptimizeCall() {
2535ffd83dbSDimitry Andric     return const_cast<CallInst *>(
2545ffd83dbSDimitry Andric          static_cast<const BasicBlock *>(this)->getPostdominatingDeoptimizeCall());
2555ffd83dbSDimitry Andric   }
2565ffd83dbSDimitry Andric 
2570b57cec5SDimitry Andric   /// Returns the call instruction marked 'musttail' prior to the terminating
2580b57cec5SDimitry Andric   /// return instruction of this basic block, if such a call is present.
2590b57cec5SDimitry Andric   /// Otherwise, returns null.
2600b57cec5SDimitry Andric   const CallInst *getTerminatingMustTailCall() const;
getTerminatingMustTailCall()2610b57cec5SDimitry Andric   CallInst *getTerminatingMustTailCall() {
2620b57cec5SDimitry Andric     return const_cast<CallInst *>(
2630b57cec5SDimitry Andric            static_cast<const BasicBlock *>(this)->getTerminatingMustTailCall());
2640b57cec5SDimitry Andric   }
2650b57cec5SDimitry Andric 
2660b57cec5SDimitry Andric   /// Returns a pointer to the first instruction in this block that is not a
2670b57cec5SDimitry Andric   /// PHINode instruction.
2680b57cec5SDimitry Andric   ///
2690b57cec5SDimitry Andric   /// When adding instructions to the beginning of the basic block, they should
2700b57cec5SDimitry Andric   /// be added before the returned value, not before the first instruction,
2710b57cec5SDimitry Andric   /// which might be PHI. Returns 0 is there's no non-PHI instruction.
2720b57cec5SDimitry Andric   const Instruction* getFirstNonPHI() const;
getFirstNonPHI()2730b57cec5SDimitry Andric   Instruction* getFirstNonPHI() {
2740b57cec5SDimitry Andric     return const_cast<Instruction *>(
2750b57cec5SDimitry Andric                        static_cast<const BasicBlock *>(this)->getFirstNonPHI());
2760b57cec5SDimitry Andric   }
2770b57cec5SDimitry Andric 
2785f757f3fSDimitry Andric   /// Iterator returning form of getFirstNonPHI. Installed as a placeholder for
2795f757f3fSDimitry Andric   /// the RemoveDIs project that will eventually remove debug intrinsics.
2805f757f3fSDimitry Andric   InstListType::const_iterator getFirstNonPHIIt() const;
getFirstNonPHIIt()2815f757f3fSDimitry Andric   InstListType::iterator getFirstNonPHIIt() {
2825f757f3fSDimitry Andric     BasicBlock::iterator It =
2835f757f3fSDimitry Andric         static_cast<const BasicBlock *>(this)->getFirstNonPHIIt().getNonConst();
2845f757f3fSDimitry Andric     It.setHeadBit(true);
2855f757f3fSDimitry Andric     return It;
2865f757f3fSDimitry Andric   }
2875f757f3fSDimitry Andric 
2880b57cec5SDimitry Andric   /// Returns a pointer to the first instruction in this block that is not a
289e8d8bef9SDimitry Andric   /// PHINode or a debug intrinsic, or any pseudo operation if \c SkipPseudoOp
290e8d8bef9SDimitry Andric   /// is true.
291349cc55cSDimitry Andric   const Instruction *getFirstNonPHIOrDbg(bool SkipPseudoOp = true) const;
292349cc55cSDimitry Andric   Instruction *getFirstNonPHIOrDbg(bool SkipPseudoOp = true) {
2930b57cec5SDimitry Andric     return const_cast<Instruction *>(
294e8d8bef9SDimitry Andric         static_cast<const BasicBlock *>(this)->getFirstNonPHIOrDbg(
295e8d8bef9SDimitry Andric             SkipPseudoOp));
2960b57cec5SDimitry Andric   }
2970b57cec5SDimitry Andric 
2980b57cec5SDimitry Andric   /// Returns a pointer to the first instruction in this block that is not a
299e8d8bef9SDimitry Andric   /// PHINode, a debug intrinsic, or a lifetime intrinsic, or any pseudo
300e8d8bef9SDimitry Andric   /// operation if \c SkipPseudoOp is true.
301e8d8bef9SDimitry Andric   const Instruction *
302349cc55cSDimitry Andric   getFirstNonPHIOrDbgOrLifetime(bool SkipPseudoOp = true) const;
303349cc55cSDimitry Andric   Instruction *getFirstNonPHIOrDbgOrLifetime(bool SkipPseudoOp = true) {
3040b57cec5SDimitry Andric     return const_cast<Instruction *>(
305e8d8bef9SDimitry Andric         static_cast<const BasicBlock *>(this)->getFirstNonPHIOrDbgOrLifetime(
306e8d8bef9SDimitry Andric             SkipPseudoOp));
3070b57cec5SDimitry Andric   }
3080b57cec5SDimitry Andric 
3090b57cec5SDimitry Andric   /// Returns an iterator to the first instruction in this block that is
3100b57cec5SDimitry Andric   /// suitable for inserting a non-PHI instruction.
3110b57cec5SDimitry Andric   ///
3120b57cec5SDimitry Andric   /// In particular, it skips all PHIs and LandingPad instructions.
3130b57cec5SDimitry Andric   const_iterator getFirstInsertionPt() const;
getFirstInsertionPt()3140b57cec5SDimitry Andric   iterator getFirstInsertionPt() {
3150b57cec5SDimitry Andric     return static_cast<const BasicBlock *>(this)
3160b57cec5SDimitry Andric                                           ->getFirstInsertionPt().getNonConst();
3170b57cec5SDimitry Andric   }
3180b57cec5SDimitry Andric 
319bdd1243dSDimitry Andric   /// Returns an iterator to the first instruction in this block that is
320bdd1243dSDimitry Andric   /// not a PHINode, a debug intrinsic, a static alloca or any pseudo operation.
321bdd1243dSDimitry Andric   const_iterator getFirstNonPHIOrDbgOrAlloca() const;
getFirstNonPHIOrDbgOrAlloca()322bdd1243dSDimitry Andric   iterator getFirstNonPHIOrDbgOrAlloca() {
323bdd1243dSDimitry Andric     return static_cast<const BasicBlock *>(this)
324bdd1243dSDimitry Andric         ->getFirstNonPHIOrDbgOrAlloca()
325bdd1243dSDimitry Andric         .getNonConst();
326bdd1243dSDimitry Andric   }
327bdd1243dSDimitry Andric 
32806c3fb27SDimitry Andric   /// Returns the first potential AsynchEH faulty instruction
32906c3fb27SDimitry Andric   /// currently it checks for loads/stores (which may dereference a null
33006c3fb27SDimitry Andric   /// pointer) and calls/invokes (which may propagate exceptions)
33106c3fb27SDimitry Andric   const Instruction* getFirstMayFaultInst() const;
getFirstMayFaultInst()33206c3fb27SDimitry Andric   Instruction* getFirstMayFaultInst() {
33306c3fb27SDimitry Andric       return const_cast<Instruction*>(
33406c3fb27SDimitry Andric           static_cast<const BasicBlock*>(this)->getFirstMayFaultInst());
33506c3fb27SDimitry Andric   }
33606c3fb27SDimitry Andric 
3370b57cec5SDimitry Andric   /// Return a const iterator range over the instructions in the block, skipping
338e8d8bef9SDimitry Andric   /// any debug instructions. Skip any pseudo operations as well if \c
339e8d8bef9SDimitry Andric   /// SkipPseudoOp is true.
3400b57cec5SDimitry Andric   iterator_range<filter_iterator<BasicBlock::const_iterator,
3410b57cec5SDimitry Andric                                  std::function<bool(const Instruction &)>>>
342349cc55cSDimitry Andric   instructionsWithoutDebug(bool SkipPseudoOp = true) const;
3430b57cec5SDimitry Andric 
3440b57cec5SDimitry Andric   /// Return an iterator range over the instructions in the block, skipping any
345e8d8bef9SDimitry Andric   /// debug instructions. Skip and any pseudo operations as well if \c
346e8d8bef9SDimitry Andric   /// SkipPseudoOp is true.
347e8d8bef9SDimitry Andric   iterator_range<
348e8d8bef9SDimitry Andric       filter_iterator<BasicBlock::iterator, std::function<bool(Instruction &)>>>
349349cc55cSDimitry Andric   instructionsWithoutDebug(bool SkipPseudoOp = true);
3500b57cec5SDimitry Andric 
3518bcb0991SDimitry Andric   /// Return the size of the basic block ignoring debug instructions
3528bcb0991SDimitry Andric   filter_iterator<BasicBlock::const_iterator,
3538bcb0991SDimitry Andric                   std::function<bool(const Instruction &)>>::difference_type
3548bcb0991SDimitry Andric   sizeWithoutDebug() const;
3558bcb0991SDimitry Andric 
3560b57cec5SDimitry Andric   /// Unlink 'this' from the containing function, but do not delete it.
3570b57cec5SDimitry Andric   void removeFromParent();
3580b57cec5SDimitry Andric 
3590b57cec5SDimitry Andric   /// Unlink 'this' from the containing function and delete it.
3600b57cec5SDimitry Andric   ///
3610b57cec5SDimitry Andric   // \returns an iterator pointing to the element after the erased one.
3620b57cec5SDimitry Andric   SymbolTableList<BasicBlock>::iterator eraseFromParent();
3630b57cec5SDimitry Andric 
3640b57cec5SDimitry Andric   /// Unlink this basic block from its current function and insert it into
3650b57cec5SDimitry Andric   /// the function that \p MovePos lives in, right before \p MovePos.
moveBefore(BasicBlock * MovePos)36606c3fb27SDimitry Andric   inline void moveBefore(BasicBlock *MovePos) {
36706c3fb27SDimitry Andric     moveBefore(MovePos->getIterator());
36806c3fb27SDimitry Andric   }
36906c3fb27SDimitry Andric   void moveBefore(SymbolTableList<BasicBlock>::iterator MovePos);
3700b57cec5SDimitry Andric 
3710b57cec5SDimitry Andric   /// Unlink this basic block from its current function and insert it
3720b57cec5SDimitry Andric   /// right after \p MovePos in the function \p MovePos lives in.
3730b57cec5SDimitry Andric   void moveAfter(BasicBlock *MovePos);
3740b57cec5SDimitry Andric 
3750b57cec5SDimitry Andric   /// Insert unlinked basic block into a function.
3760b57cec5SDimitry Andric   ///
3770b57cec5SDimitry Andric   /// Inserts an unlinked basic block into \c Parent.  If \c InsertBefore is
3780b57cec5SDimitry Andric   /// provided, inserts before that basic block, otherwise inserts at the end.
3790b57cec5SDimitry Andric   ///
3800b57cec5SDimitry Andric   /// \pre \a getParent() is \c nullptr.
3810b57cec5SDimitry Andric   void insertInto(Function *Parent, BasicBlock *InsertBefore = nullptr);
3820b57cec5SDimitry Andric 
3830b57cec5SDimitry Andric   /// Return the predecessor of this block if it has a single predecessor
3840b57cec5SDimitry Andric   /// block. Otherwise return a null pointer.
3850b57cec5SDimitry Andric   const BasicBlock *getSinglePredecessor() const;
getSinglePredecessor()3860b57cec5SDimitry Andric   BasicBlock *getSinglePredecessor() {
3870b57cec5SDimitry Andric     return const_cast<BasicBlock *>(
3880b57cec5SDimitry Andric                  static_cast<const BasicBlock *>(this)->getSinglePredecessor());
3890b57cec5SDimitry Andric   }
3900b57cec5SDimitry Andric 
3910b57cec5SDimitry Andric   /// Return the predecessor of this block if it has a unique predecessor
3920b57cec5SDimitry Andric   /// block. Otherwise return a null pointer.
3930b57cec5SDimitry Andric   ///
3940b57cec5SDimitry Andric   /// Note that unique predecessor doesn't mean single edge, there can be
3950b57cec5SDimitry Andric   /// multiple edges from the unique predecessor to this block (for example a
3960b57cec5SDimitry Andric   /// switch statement with multiple cases having the same destination).
3970b57cec5SDimitry Andric   const BasicBlock *getUniquePredecessor() const;
getUniquePredecessor()3980b57cec5SDimitry Andric   BasicBlock *getUniquePredecessor() {
3990b57cec5SDimitry Andric     return const_cast<BasicBlock *>(
4000b57cec5SDimitry Andric                  static_cast<const BasicBlock *>(this)->getUniquePredecessor());
4010b57cec5SDimitry Andric   }
4020b57cec5SDimitry Andric 
4030b57cec5SDimitry Andric   /// Return true if this block has exactly N predecessors.
4040b57cec5SDimitry Andric   bool hasNPredecessors(unsigned N) const;
4050b57cec5SDimitry Andric 
4060b57cec5SDimitry Andric   /// Return true if this block has N predecessors or more.
4070b57cec5SDimitry Andric   bool hasNPredecessorsOrMore(unsigned N) const;
4080b57cec5SDimitry Andric 
4090b57cec5SDimitry Andric   /// Return the successor of this block if it has a single successor.
4100b57cec5SDimitry Andric   /// Otherwise return a null pointer.
4110b57cec5SDimitry Andric   ///
4120b57cec5SDimitry Andric   /// This method is analogous to getSinglePredecessor above.
4130b57cec5SDimitry Andric   const BasicBlock *getSingleSuccessor() const;
getSingleSuccessor()4140b57cec5SDimitry Andric   BasicBlock *getSingleSuccessor() {
4150b57cec5SDimitry Andric     return const_cast<BasicBlock *>(
4160b57cec5SDimitry Andric                    static_cast<const BasicBlock *>(this)->getSingleSuccessor());
4170b57cec5SDimitry Andric   }
4180b57cec5SDimitry Andric 
4190b57cec5SDimitry Andric   /// Return the successor of this block if it has a unique successor.
4200b57cec5SDimitry Andric   /// Otherwise return a null pointer.
4210b57cec5SDimitry Andric   ///
4220b57cec5SDimitry Andric   /// This method is analogous to getUniquePredecessor above.
4230b57cec5SDimitry Andric   const BasicBlock *getUniqueSuccessor() const;
getUniqueSuccessor()4240b57cec5SDimitry Andric   BasicBlock *getUniqueSuccessor() {
4250b57cec5SDimitry Andric     return const_cast<BasicBlock *>(
4260b57cec5SDimitry Andric                    static_cast<const BasicBlock *>(this)->getUniqueSuccessor());
4270b57cec5SDimitry Andric   }
4280b57cec5SDimitry Andric 
4295ffd83dbSDimitry Andric   /// Print the basic block to an output stream with an optional
4305ffd83dbSDimitry Andric   /// AssemblyAnnotationWriter.
4315ffd83dbSDimitry Andric   void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW = nullptr,
4325ffd83dbSDimitry Andric              bool ShouldPreserveUseListOrder = false,
4335ffd83dbSDimitry Andric              bool IsForDebug = false) const;
4345ffd83dbSDimitry Andric 
4350b57cec5SDimitry Andric   //===--------------------------------------------------------------------===//
4360b57cec5SDimitry Andric   /// Instruction iterator methods
4370b57cec5SDimitry Andric   ///
begin()4385f757f3fSDimitry Andric   inline iterator begin() {
4395f757f3fSDimitry Andric     iterator It = InstList.begin();
4405f757f3fSDimitry Andric     // Set the head-inclusive bit to indicate that this iterator includes
4415f757f3fSDimitry Andric     // any debug-info at the start of the block. This is a no-op unless the
4425f757f3fSDimitry Andric     // appropriate CMake flag is set.
4435f757f3fSDimitry Andric     It.setHeadBit(true);
4445f757f3fSDimitry Andric     return It;
4455f757f3fSDimitry Andric   }
begin()4465f757f3fSDimitry Andric   inline const_iterator begin() const {
4475f757f3fSDimitry Andric     const_iterator It = InstList.begin();
4485f757f3fSDimitry Andric     It.setHeadBit(true);
4495f757f3fSDimitry Andric     return It;
4505f757f3fSDimitry Andric   }
end()4510b57cec5SDimitry Andric   inline iterator                end  ()       { return InstList.end();   }
end()4520b57cec5SDimitry Andric   inline const_iterator          end  () const { return InstList.end();   }
4530b57cec5SDimitry Andric 
rbegin()4540b57cec5SDimitry Andric   inline reverse_iterator        rbegin()       { return InstList.rbegin(); }
rbegin()4550b57cec5SDimitry Andric   inline const_reverse_iterator  rbegin() const { return InstList.rbegin(); }
rend()4560b57cec5SDimitry Andric   inline reverse_iterator        rend  ()       { return InstList.rend();   }
rend()4570b57cec5SDimitry Andric   inline const_reverse_iterator  rend  () const { return InstList.rend();   }
4580b57cec5SDimitry Andric 
size()4590b57cec5SDimitry Andric   inline size_t                   size() const { return InstList.size();  }
empty()4600b57cec5SDimitry Andric   inline bool                    empty() const { return InstList.empty(); }
front()4610b57cec5SDimitry Andric   inline const Instruction      &front() const { return InstList.front(); }
front()4620b57cec5SDimitry Andric   inline       Instruction      &front()       { return InstList.front(); }
back()4630b57cec5SDimitry Andric   inline const Instruction       &back() const { return InstList.back();  }
back()4640b57cec5SDimitry Andric   inline       Instruction       &back()       { return InstList.back();  }
4650b57cec5SDimitry Andric 
4660b57cec5SDimitry Andric   /// Iterator to walk just the phi nodes in the basic block.
4670b57cec5SDimitry Andric   template <typename PHINodeT = PHINode, typename BBIteratorT = iterator>
4680b57cec5SDimitry Andric   class phi_iterator_impl
4690b57cec5SDimitry Andric       : public iterator_facade_base<phi_iterator_impl<PHINodeT, BBIteratorT>,
4700b57cec5SDimitry Andric                                     std::forward_iterator_tag, PHINodeT> {
4710b57cec5SDimitry Andric     friend BasicBlock;
4720b57cec5SDimitry Andric 
4730b57cec5SDimitry Andric     PHINodeT *PN;
4740b57cec5SDimitry Andric 
phi_iterator_impl(PHINodeT * PN)4750b57cec5SDimitry Andric     phi_iterator_impl(PHINodeT *PN) : PN(PN) {}
4760b57cec5SDimitry Andric 
4770b57cec5SDimitry Andric   public:
4780b57cec5SDimitry Andric     // Allow default construction to build variables, but this doesn't build
4790b57cec5SDimitry Andric     // a useful iterator.
4800b57cec5SDimitry Andric     phi_iterator_impl() = default;
4810b57cec5SDimitry Andric 
4820b57cec5SDimitry Andric     // Allow conversion between instantiations where valid.
483e8d8bef9SDimitry Andric     template <typename PHINodeU, typename BBIteratorU,
484e8d8bef9SDimitry Andric               typename = std::enable_if_t<
485e8d8bef9SDimitry Andric                   std::is_convertible<PHINodeU *, PHINodeT *>::value>>
phi_iterator_impl(const phi_iterator_impl<PHINodeU,BBIteratorU> & Arg)4860b57cec5SDimitry Andric     phi_iterator_impl(const phi_iterator_impl<PHINodeU, BBIteratorU> &Arg)
4870b57cec5SDimitry Andric         : PN(Arg.PN) {}
4880b57cec5SDimitry Andric 
4890b57cec5SDimitry Andric     bool operator==(const phi_iterator_impl &Arg) const { return PN == Arg.PN; }
4900b57cec5SDimitry Andric 
4910b57cec5SDimitry Andric     PHINodeT &operator*() const { return *PN; }
4920b57cec5SDimitry Andric 
4930b57cec5SDimitry Andric     using phi_iterator_impl::iterator_facade_base::operator++;
4940b57cec5SDimitry Andric     phi_iterator_impl &operator++() {
4950b57cec5SDimitry Andric       assert(PN && "Cannot increment the end iterator!");
4960b57cec5SDimitry Andric       PN = dyn_cast<PHINodeT>(std::next(BBIteratorT(PN)));
4970b57cec5SDimitry Andric       return *this;
4980b57cec5SDimitry Andric     }
4990b57cec5SDimitry Andric   };
5000b57cec5SDimitry Andric   using phi_iterator = phi_iterator_impl<>;
5010b57cec5SDimitry Andric   using const_phi_iterator =
5020b57cec5SDimitry Andric       phi_iterator_impl<const PHINode, BasicBlock::const_iterator>;
5030b57cec5SDimitry Andric 
5040b57cec5SDimitry Andric   /// Returns a range that iterates over the phis in the basic block.
5050b57cec5SDimitry Andric   ///
5060b57cec5SDimitry Andric   /// Note that this cannot be used with basic blocks that have no terminator.
phis()5070b57cec5SDimitry Andric   iterator_range<const_phi_iterator> phis() const {
5080b57cec5SDimitry Andric     return const_cast<BasicBlock *>(this)->phis();
5090b57cec5SDimitry Andric   }
5100b57cec5SDimitry Andric   iterator_range<phi_iterator> phis();
5110b57cec5SDimitry Andric 
512bdd1243dSDimitry Andric private:
5130b57cec5SDimitry Andric   /// Return the underlying instruction list container.
514bdd1243dSDimitry Andric   /// This is deliberately private because we have implemented an adequate set
515bdd1243dSDimitry Andric   /// of functions to modify the list, including BasicBlock::splice(),
516bdd1243dSDimitry Andric   /// BasicBlock::erase(), Instruction::insertInto() etc.
getInstList()5170b57cec5SDimitry Andric   const InstListType &getInstList() const { return InstList; }
getInstList()5180b57cec5SDimitry Andric   InstListType &getInstList() { return InstList; }
5190b57cec5SDimitry Andric 
5200b57cec5SDimitry Andric   /// Returns a pointer to a member of the instruction list.
521bdd1243dSDimitry Andric   /// This is private on purpose, just like `getInstList()`.
getSublistAccess(Instruction *)5220b57cec5SDimitry Andric   static InstListType BasicBlock::*getSublistAccess(Instruction *) {
5230b57cec5SDimitry Andric     return &BasicBlock::InstList;
5240b57cec5SDimitry Andric   }
5250b57cec5SDimitry Andric 
5265f757f3fSDimitry Andric   /// Dedicated function for splicing debug-info: when we have an empty
5275f757f3fSDimitry Andric   /// splice (i.e. zero instructions), the caller may still intend any
5285f757f3fSDimitry Andric   /// debug-info in between the two "positions" to be spliced.
5295f757f3fSDimitry Andric   void spliceDebugInfoEmptyBlock(BasicBlock::iterator ToIt, BasicBlock *FromBB,
5305f757f3fSDimitry Andric                                  BasicBlock::iterator FromBeginIt,
5315f757f3fSDimitry Andric                                  BasicBlock::iterator FromEndIt);
5325f757f3fSDimitry Andric 
5335f757f3fSDimitry Andric   /// Perform any debug-info specific maintenence for the given splice
534*0fca6ea1SDimitry Andric   /// activity. In the DbgRecord debug-info representation, debug-info is not
5355f757f3fSDimitry Andric   /// in instructions, and so it does not automatically move from one block
5365f757f3fSDimitry Andric   /// to another.
5375f757f3fSDimitry Andric   void spliceDebugInfo(BasicBlock::iterator ToIt, BasicBlock *FromBB,
5385f757f3fSDimitry Andric                        BasicBlock::iterator FromBeginIt,
5395f757f3fSDimitry Andric                        BasicBlock::iterator FromEndIt);
5405f757f3fSDimitry Andric   void spliceDebugInfoImpl(BasicBlock::iterator ToIt, BasicBlock *FromBB,
5415f757f3fSDimitry Andric                            BasicBlock::iterator FromBeginIt,
5425f757f3fSDimitry Andric                            BasicBlock::iterator FromEndIt);
5435f757f3fSDimitry Andric 
544bdd1243dSDimitry Andric public:
5450b57cec5SDimitry Andric   /// Returns a pointer to the symbol table if one exists.
5460b57cec5SDimitry Andric   ValueSymbolTable *getValueSymbolTable();
5470b57cec5SDimitry Andric 
5480b57cec5SDimitry Andric   /// Methods for support type inquiry through isa, cast, and dyn_cast.
classof(const Value * V)5490b57cec5SDimitry Andric   static bool classof(const Value *V) {
5500b57cec5SDimitry Andric     return V->getValueID() == Value::BasicBlockVal;
5510b57cec5SDimitry Andric   }
5520b57cec5SDimitry Andric 
5530b57cec5SDimitry Andric   /// Cause all subinstructions to "let go" of all the references that said
5540b57cec5SDimitry Andric   /// subinstructions are maintaining.
5550b57cec5SDimitry Andric   ///
5560b57cec5SDimitry Andric   /// This allows one to 'delete' a whole class at a time, even though there may
5570b57cec5SDimitry Andric   /// be circular references... first all references are dropped, and all use
5580b57cec5SDimitry Andric   /// counts go to zero.  Then everything is delete'd for real.  Note that no
5590b57cec5SDimitry Andric   /// operations are valid on an object that has "dropped all references",
5600b57cec5SDimitry Andric   /// except operator delete.
5610b57cec5SDimitry Andric   void dropAllReferences();
5620b57cec5SDimitry Andric 
5635ffd83dbSDimitry Andric   /// Update PHI nodes in this BasicBlock before removal of predecessor \p Pred.
5645ffd83dbSDimitry Andric   /// Note that this function does not actually remove the predecessor.
5650b57cec5SDimitry Andric   ///
5665ffd83dbSDimitry Andric   /// If \p KeepOneInputPHIs is true then don't remove PHIs that are left with
5675ffd83dbSDimitry Andric   /// zero or one incoming values, and don't simplify PHIs with all incoming
5685ffd83dbSDimitry Andric   /// values the same.
5690b57cec5SDimitry Andric   void removePredecessor(BasicBlock *Pred, bool KeepOneInputPHIs = false);
5700b57cec5SDimitry Andric 
5710b57cec5SDimitry Andric   bool canSplitPredecessors() const;
5720b57cec5SDimitry Andric 
5730b57cec5SDimitry Andric   /// Split the basic block into two basic blocks at the specified instruction.
5740b57cec5SDimitry Andric   ///
575e8d8bef9SDimitry Andric   /// If \p Before is true, splitBasicBlockBefore handles the
576e8d8bef9SDimitry Andric   /// block splitting. Otherwise, execution proceeds as described below.
577e8d8bef9SDimitry Andric   ///
578e8d8bef9SDimitry Andric   /// Note that all instructions BEFORE the specified iterator
579e8d8bef9SDimitry Andric   /// stay as part of the original basic block, an unconditional branch is added
580e8d8bef9SDimitry Andric   /// to the original BB, and the rest of the instructions in the BB are moved
581e8d8bef9SDimitry Andric   /// to the new BB, including the old terminator.  The newly formed basic block
582e8d8bef9SDimitry Andric   /// is returned. This function invalidates the specified iterator.
5830b57cec5SDimitry Andric   ///
5840b57cec5SDimitry Andric   /// Note that this only works on well formed basic blocks (must have a
585e8d8bef9SDimitry Andric   /// terminator), and \p 'I' must not be the end of instruction list (which
586e8d8bef9SDimitry Andric   /// would cause a degenerate basic block to be formed, having a terminator
587e8d8bef9SDimitry Andric   /// inside of the basic block).
5880b57cec5SDimitry Andric   ///
5890b57cec5SDimitry Andric   /// Also note that this doesn't preserve any passes. To split blocks while
5900b57cec5SDimitry Andric   /// keeping loop information consistent, use the SplitBlock utility function.
591e8d8bef9SDimitry Andric   BasicBlock *splitBasicBlock(iterator I, const Twine &BBName = "",
592e8d8bef9SDimitry Andric                               bool Before = false);
593e8d8bef9SDimitry Andric   BasicBlock *splitBasicBlock(Instruction *I, const Twine &BBName = "",
594e8d8bef9SDimitry Andric                               bool Before = false) {
595e8d8bef9SDimitry Andric     return splitBasicBlock(I->getIterator(), BBName, Before);
596e8d8bef9SDimitry Andric   }
597e8d8bef9SDimitry Andric 
598e8d8bef9SDimitry Andric   /// Split the basic block into two basic blocks at the specified instruction
599e8d8bef9SDimitry Andric   /// and insert the new basic blocks as the predecessor of the current block.
600e8d8bef9SDimitry Andric   ///
601e8d8bef9SDimitry Andric   /// This function ensures all instructions AFTER and including the specified
602e8d8bef9SDimitry Andric   /// iterator \p I are part of the original basic block. All Instructions
603e8d8bef9SDimitry Andric   /// BEFORE the iterator \p I are moved to the new BB and an unconditional
604e8d8bef9SDimitry Andric   /// branch is added to the new BB. The new basic block is returned.
605e8d8bef9SDimitry Andric   ///
606e8d8bef9SDimitry Andric   /// Note that this only works on well formed basic blocks (must have a
607e8d8bef9SDimitry Andric   /// terminator), and \p 'I' must not be the end of instruction list (which
608e8d8bef9SDimitry Andric   /// would cause a degenerate basic block to be formed, having a terminator
609e8d8bef9SDimitry Andric   /// inside of the basic block).  \p 'I' cannot be a iterator for a PHINode
610e8d8bef9SDimitry Andric   /// with multiple incoming blocks.
611e8d8bef9SDimitry Andric   ///
612e8d8bef9SDimitry Andric   /// Also note that this doesn't preserve any passes. To split blocks while
613e8d8bef9SDimitry Andric   /// keeping loop information consistent, use the SplitBlockBefore utility
614e8d8bef9SDimitry Andric   /// function.
615e8d8bef9SDimitry Andric   BasicBlock *splitBasicBlockBefore(iterator I, const Twine &BBName = "");
616e8d8bef9SDimitry Andric   BasicBlock *splitBasicBlockBefore(Instruction *I, const Twine &BBName = "") {
617e8d8bef9SDimitry Andric     return splitBasicBlockBefore(I->getIterator(), BBName);
6180b57cec5SDimitry Andric   }
6190b57cec5SDimitry Andric 
620bdd1243dSDimitry Andric   /// Transfer all instructions from \p FromBB to this basic block at \p ToIt.
splice(BasicBlock::iterator ToIt,BasicBlock * FromBB)621bdd1243dSDimitry Andric   void splice(BasicBlock::iterator ToIt, BasicBlock *FromBB) {
622bdd1243dSDimitry Andric     splice(ToIt, FromBB, FromBB->begin(), FromBB->end());
623bdd1243dSDimitry Andric   }
624bdd1243dSDimitry Andric 
625bdd1243dSDimitry Andric   /// Transfer one instruction from \p FromBB at \p FromIt to this basic block
626bdd1243dSDimitry Andric   /// at \p ToIt.
splice(BasicBlock::iterator ToIt,BasicBlock * FromBB,BasicBlock::iterator FromIt)627bdd1243dSDimitry Andric   void splice(BasicBlock::iterator ToIt, BasicBlock *FromBB,
628bdd1243dSDimitry Andric               BasicBlock::iterator FromIt) {
629bdd1243dSDimitry Andric     auto FromItNext = std::next(FromIt);
630bdd1243dSDimitry Andric     // Single-element splice is a noop if destination == source.
631bdd1243dSDimitry Andric     if (ToIt == FromIt || ToIt == FromItNext)
632bdd1243dSDimitry Andric       return;
633bdd1243dSDimitry Andric     splice(ToIt, FromBB, FromIt, FromItNext);
634bdd1243dSDimitry Andric   }
635bdd1243dSDimitry Andric 
636bdd1243dSDimitry Andric   /// Transfer a range of instructions that belong to \p FromBB from \p
637bdd1243dSDimitry Andric   /// FromBeginIt to \p FromEndIt, to this basic block at \p ToIt.
638bdd1243dSDimitry Andric   void splice(BasicBlock::iterator ToIt, BasicBlock *FromBB,
639bdd1243dSDimitry Andric               BasicBlock::iterator FromBeginIt,
640bdd1243dSDimitry Andric               BasicBlock::iterator FromEndIt);
641bdd1243dSDimitry Andric 
642bdd1243dSDimitry Andric   /// Erases a range of instructions from \p FromIt to (not including) \p ToIt.
643bdd1243dSDimitry Andric   /// \Returns \p ToIt.
644bdd1243dSDimitry Andric   BasicBlock::iterator erase(BasicBlock::iterator FromIt, BasicBlock::iterator ToIt);
645bdd1243dSDimitry Andric 
6460b57cec5SDimitry Andric   /// Returns true if there are any uses of this basic block other than
6470b57cec5SDimitry Andric   /// direct branches, switches, etc. to it.
hasAddressTaken()6485ffd83dbSDimitry Andric   bool hasAddressTaken() const {
6495ffd83dbSDimitry Andric     return getBasicBlockBits().BlockAddressRefCount != 0;
6505ffd83dbSDimitry Andric   }
6510b57cec5SDimitry Andric 
6520b57cec5SDimitry Andric   /// Update all phi nodes in this basic block to refer to basic block \p New
6530b57cec5SDimitry Andric   /// instead of basic block \p Old.
6540b57cec5SDimitry Andric   void replacePhiUsesWith(BasicBlock *Old, BasicBlock *New);
6550b57cec5SDimitry Andric 
6560b57cec5SDimitry Andric   /// Update all phi nodes in this basic block's successors to refer to basic
6570b57cec5SDimitry Andric   /// block \p New instead of basic block \p Old.
6580b57cec5SDimitry Andric   void replaceSuccessorsPhiUsesWith(BasicBlock *Old, BasicBlock *New);
6590b57cec5SDimitry Andric 
6600b57cec5SDimitry Andric   /// Update all phi nodes in this basic block's successors to refer to basic
6610b57cec5SDimitry Andric   /// block \p New instead of to it.
6620b57cec5SDimitry Andric   void replaceSuccessorsPhiUsesWith(BasicBlock *New);
6630b57cec5SDimitry Andric 
6640b57cec5SDimitry Andric   /// Return true if this basic block is an exception handling block.
isEHPad()6650b57cec5SDimitry Andric   bool isEHPad() const { return getFirstNonPHI()->isEHPad(); }
6660b57cec5SDimitry Andric 
6670b57cec5SDimitry Andric   /// Return true if this basic block is a landing pad.
6680b57cec5SDimitry Andric   ///
6690b57cec5SDimitry Andric   /// Being a ``landing pad'' means that the basic block is the destination of
6700b57cec5SDimitry Andric   /// the 'unwind' edge of an invoke instruction.
6710b57cec5SDimitry Andric   bool isLandingPad() const;
6720b57cec5SDimitry Andric 
6730b57cec5SDimitry Andric   /// Return the landingpad instruction associated with the landing pad.
6740b57cec5SDimitry Andric   const LandingPadInst *getLandingPadInst() const;
getLandingPadInst()6750b57cec5SDimitry Andric   LandingPadInst *getLandingPadInst() {
6760b57cec5SDimitry Andric     return const_cast<LandingPadInst *>(
6770b57cec5SDimitry Andric                     static_cast<const BasicBlock *>(this)->getLandingPadInst());
6780b57cec5SDimitry Andric   }
6790b57cec5SDimitry Andric 
6800b57cec5SDimitry Andric   /// Return true if it is legal to hoist instructions into this block.
6810b57cec5SDimitry Andric   bool isLegalToHoistInto() const;
6820b57cec5SDimitry Andric 
683fe6060f1SDimitry Andric   /// Return true if this is the entry block of the containing function.
684fe6060f1SDimitry Andric   /// This method can only be used on blocks that have a parent function.
685fe6060f1SDimitry Andric   bool isEntryBlock() const;
686fe6060f1SDimitry Andric 
687bdd1243dSDimitry Andric   std::optional<uint64_t> getIrrLoopHeaderWeight() const;
6880b57cec5SDimitry Andric 
6895ffd83dbSDimitry Andric   /// Returns true if the Order field of child Instructions is valid.
isInstrOrderValid()6905ffd83dbSDimitry Andric   bool isInstrOrderValid() const {
6915ffd83dbSDimitry Andric     return getBasicBlockBits().InstrOrderValid;
6925ffd83dbSDimitry Andric   }
6935ffd83dbSDimitry Andric 
6945ffd83dbSDimitry Andric   /// Mark instruction ordering invalid. Done on every instruction insert.
invalidateOrders()6955ffd83dbSDimitry Andric   void invalidateOrders() {
6965ffd83dbSDimitry Andric     validateInstrOrdering();
6975ffd83dbSDimitry Andric     BasicBlockBits Bits = getBasicBlockBits();
6985ffd83dbSDimitry Andric     Bits.InstrOrderValid = false;
6995ffd83dbSDimitry Andric     setBasicBlockBits(Bits);
7005ffd83dbSDimitry Andric   }
7015ffd83dbSDimitry Andric 
7025ffd83dbSDimitry Andric   /// Renumber instructions and mark the ordering as valid.
7035ffd83dbSDimitry Andric   void renumberInstructions();
7045ffd83dbSDimitry Andric 
7055ffd83dbSDimitry Andric   /// Asserts that instruction order numbers are marked invalid, or that they
7065ffd83dbSDimitry Andric   /// are in ascending order. This is constant time if the ordering is invalid,
7075ffd83dbSDimitry Andric   /// and linear in the number of instructions if the ordering is valid. Callers
7085ffd83dbSDimitry Andric   /// should be careful not to call this in ways that make common operations
7095ffd83dbSDimitry Andric   /// O(n^2). For example, it takes O(n) time to assign order numbers to
7105ffd83dbSDimitry Andric   /// instructions, so the order should be validated no more than once after
7115ffd83dbSDimitry Andric   /// each ordering to ensure that transforms have the same algorithmic
7125ffd83dbSDimitry Andric   /// complexity when asserts are enabled as when they are disabled.
7135ffd83dbSDimitry Andric   void validateInstrOrdering() const;
7145ffd83dbSDimitry Andric 
7150b57cec5SDimitry Andric private:
716fe6060f1SDimitry Andric #if defined(_AIX) && (!defined(__GNUC__) || defined(__clang__))
7175ffd83dbSDimitry Andric // Except for GCC; by default, AIX compilers store bit-fields in 4-byte words
7185ffd83dbSDimitry Andric // and give the `pack` pragma push semantics.
7195ffd83dbSDimitry Andric #define BEGIN_TWO_BYTE_PACK() _Pragma("pack(2)")
7205ffd83dbSDimitry Andric #define END_TWO_BYTE_PACK() _Pragma("pack(pop)")
7215ffd83dbSDimitry Andric #else
7225ffd83dbSDimitry Andric #define BEGIN_TWO_BYTE_PACK()
7235ffd83dbSDimitry Andric #define END_TWO_BYTE_PACK()
7245ffd83dbSDimitry Andric #endif
7255ffd83dbSDimitry Andric 
7265ffd83dbSDimitry Andric   BEGIN_TWO_BYTE_PACK()
7275ffd83dbSDimitry Andric   /// Bitfield to help interpret the bits in Value::SubclassData.
7285ffd83dbSDimitry Andric   struct BasicBlockBits {
7295ffd83dbSDimitry Andric     unsigned short BlockAddressRefCount : 15;
7305ffd83dbSDimitry Andric     unsigned short InstrOrderValid : 1;
7315ffd83dbSDimitry Andric   };
END_TWO_BYTE_PACK()7325ffd83dbSDimitry Andric   END_TWO_BYTE_PACK()
7335ffd83dbSDimitry Andric 
7345ffd83dbSDimitry Andric #undef BEGIN_TWO_BYTE_PACK
7355ffd83dbSDimitry Andric #undef END_TWO_BYTE_PACK
7365ffd83dbSDimitry Andric 
7375ffd83dbSDimitry Andric   /// Safely reinterpret the subclass data bits to a more useful form.
7385ffd83dbSDimitry Andric   BasicBlockBits getBasicBlockBits() const {
7395ffd83dbSDimitry Andric     static_assert(sizeof(BasicBlockBits) == sizeof(unsigned short),
7405ffd83dbSDimitry Andric                   "too many bits for Value::SubclassData");
7415ffd83dbSDimitry Andric     unsigned short ValueData = getSubclassDataFromValue();
7425ffd83dbSDimitry Andric     BasicBlockBits AsBits;
7435ffd83dbSDimitry Andric     memcpy(&AsBits, &ValueData, sizeof(AsBits));
7445ffd83dbSDimitry Andric     return AsBits;
7455ffd83dbSDimitry Andric   }
7465ffd83dbSDimitry Andric 
7475ffd83dbSDimitry Andric   /// Reinterpret our subclass bits and store them back into Value.
setBasicBlockBits(BasicBlockBits AsBits)7485ffd83dbSDimitry Andric   void setBasicBlockBits(BasicBlockBits AsBits) {
7495ffd83dbSDimitry Andric     unsigned short D;
7505ffd83dbSDimitry Andric     memcpy(&D, &AsBits, sizeof(D));
7515ffd83dbSDimitry Andric     Value::setValueSubclassData(D);
7525ffd83dbSDimitry Andric   }
7535ffd83dbSDimitry Andric 
7540b57cec5SDimitry Andric   /// Increment the internal refcount of the number of BlockAddresses
7550b57cec5SDimitry Andric   /// referencing this BasicBlock by \p Amt.
7560b57cec5SDimitry Andric   ///
7570b57cec5SDimitry Andric   /// This is almost always 0, sometimes one possibly, but almost never 2, and
7580b57cec5SDimitry Andric   /// inconceivably 3 or more.
AdjustBlockAddressRefCount(int Amt)7590b57cec5SDimitry Andric   void AdjustBlockAddressRefCount(int Amt) {
7605ffd83dbSDimitry Andric     BasicBlockBits Bits = getBasicBlockBits();
7615ffd83dbSDimitry Andric     Bits.BlockAddressRefCount += Amt;
7625ffd83dbSDimitry Andric     setBasicBlockBits(Bits);
7635ffd83dbSDimitry Andric     assert(Bits.BlockAddressRefCount < 255 && "Refcount wrap-around");
7640b57cec5SDimitry Andric   }
7650b57cec5SDimitry Andric 
7660b57cec5SDimitry Andric   /// Shadow Value::setValueSubclassData with a private forwarding method so
7670b57cec5SDimitry Andric   /// that any future subclasses cannot accidentally use it.
setValueSubclassData(unsigned short D)7680b57cec5SDimitry Andric   void setValueSubclassData(unsigned short D) {
7690b57cec5SDimitry Andric     Value::setValueSubclassData(D);
7700b57cec5SDimitry Andric   }
7710b57cec5SDimitry Andric };
7720b57cec5SDimitry Andric 
7730b57cec5SDimitry Andric // Create wrappers for C Binding types (see CBindingWrapping.h).
7740b57cec5SDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(BasicBlock, LLVMBasicBlockRef)
7750b57cec5SDimitry Andric 
7760b57cec5SDimitry Andric /// Advance \p It while it points to a debug instruction and return the result.
7770b57cec5SDimitry Andric /// This assumes that \p It is not at the end of a block.
7780b57cec5SDimitry Andric BasicBlock::iterator skipDebugIntrinsics(BasicBlock::iterator It);
7790b57cec5SDimitry Andric 
7805ffd83dbSDimitry Andric #ifdef NDEBUG
7815ffd83dbSDimitry Andric /// In release builds, this is a no-op. For !NDEBUG builds, the checks are
7825ffd83dbSDimitry Andric /// implemented in the .cpp file to avoid circular header deps.
validateInstrOrdering()7835ffd83dbSDimitry Andric inline void BasicBlock::validateInstrOrdering() const {}
7845ffd83dbSDimitry Andric #endif
7855ffd83dbSDimitry Andric 
786*0fca6ea1SDimitry Andric // Specialize DenseMapInfo for iterators, so that ththey can be installed into
787*0fca6ea1SDimitry Andric // maps and sets. The iterator is made up of its node pointer, and the
788*0fca6ea1SDimitry Andric // debug-info "head" bit.
789*0fca6ea1SDimitry Andric template <> struct DenseMapInfo<BasicBlock::iterator> {
790*0fca6ea1SDimitry Andric   static inline BasicBlock::iterator getEmptyKey() {
791*0fca6ea1SDimitry Andric     return BasicBlock::iterator(nullptr);
792*0fca6ea1SDimitry Andric   }
793*0fca6ea1SDimitry Andric 
794*0fca6ea1SDimitry Andric   static inline BasicBlock::iterator getTombstoneKey() {
795*0fca6ea1SDimitry Andric     BasicBlock::iterator It(nullptr);
796*0fca6ea1SDimitry Andric     It.setHeadBit(true);
797*0fca6ea1SDimitry Andric     return It;
798*0fca6ea1SDimitry Andric   }
799*0fca6ea1SDimitry Andric 
800*0fca6ea1SDimitry Andric   static unsigned getHashValue(const BasicBlock::iterator &It) {
801*0fca6ea1SDimitry Andric     return DenseMapInfo<void *>::getHashValue(
802*0fca6ea1SDimitry Andric                reinterpret_cast<void *>(It.getNodePtr())) ^
803*0fca6ea1SDimitry Andric            (unsigned)It.getHeadBit();
804*0fca6ea1SDimitry Andric   }
805*0fca6ea1SDimitry Andric 
806*0fca6ea1SDimitry Andric   static bool isEqual(const BasicBlock::iterator &LHS,
807*0fca6ea1SDimitry Andric                       const BasicBlock::iterator &RHS) {
808*0fca6ea1SDimitry Andric     return LHS == RHS && LHS.getHeadBit() == RHS.getHeadBit();
809*0fca6ea1SDimitry Andric   }
810*0fca6ea1SDimitry Andric };
811*0fca6ea1SDimitry Andric 
8120b57cec5SDimitry Andric } // end namespace llvm
8130b57cec5SDimitry Andric 
8140b57cec5SDimitry Andric #endif // LLVM_IR_BASICBLOCK_H
815