LLVM 24.0.0git
BasicBlock.h
Go to the documentation of this file.
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/DenseMap.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"
24#include "llvm/IR/Instruction.h"
26#include "llvm/IR/Value.h"
28#include <cassert>
29#include <cstddef>
30#include <iterator>
31
32namespace llvm {
33
35class CallInst;
36class DataLayout;
37class Function;
38class LandingPadInst;
39class LLVMContext;
40class Module;
41class PHINode;
43class DbgMarker;
44
45/// LLVM Basic Block Representation
46///
47/// This represents a single basic block in LLVM. A basic block is simply a
48/// container of instructions that execute sequentially. Basic blocks are Values
49/// because they are referenced by instructions such as branches and switch
50/// tables. The type of a BasicBlock is "Type::LabelTy" because the basic block
51/// represents a label to which a branch can jump.
52///
53/// A well formed basic block is formed of a list of non-terminating
54/// instructions followed by a single terminator instruction. Terminator
55/// instructions may not occur in the middle of basic blocks, and must terminate
56/// the blocks. The BasicBlock class allows malformed basic blocks to occur
57/// because it may be useful in the intermediate stage of constructing or
58/// modifying a program. However, the verifier will ensure that basic blocks are
59/// "well formed".
60class BasicBlock final : public Value, // Basic blocks are data objects also
61 public ilist_node_with_parent<BasicBlock, Function> {
62public:
65
66private:
67 // Allow Function to renumber blocks.
68 friend class Function;
69 /// Per-function unique number.
70 unsigned Number = ~0u;
71
72 friend class BlockAddress;
73 friend class SymbolTableListTraits<BasicBlock>;
74
75 InstListType InstList;
76 Function *Parent;
77
78public:
79 /// Attach a DbgMarker to the given instruction. Enables the storage of any
80 /// debug-info at this position in the program.
83
84 /// Convert variable location debugging information stored in dbg.value
85 /// intrinsics into DbgMarkers / DbgRecords. Deletes all dbg.values in
86 /// the process and sets IsNewDbgInfoFormat = true.
88
89 /// Convert variable location debugging information stored in DbgMarkers and
90 /// DbgRecords into the dbg.value intrinsic representation. Sets
91 /// IsNewDbgInfoFormat = false.
93
94 unsigned getNumber() const {
95 assert(getParent() && "only basic blocks in functions have valid numbers");
96 return Number;
97 }
98
99 /// Record that the collection of DbgRecords in \p M "trails" after the last
100 /// instruction of this block. These are equivalent to dbg.value intrinsics
101 /// that exist at the end of a basic block with no terminator (a transient
102 /// state that occurs regularly).
104
105 /// Fetch the collection of DbgRecords that "trail" after the last instruction
106 /// of this block, see \ref setTrailingDbgRecords. If there are none, returns
107 /// nullptr.
109
110 /// Delete any trailing DbgRecords at the end of this block, see
111 /// \ref setTrailingDbgRecords.
113
114 LLVM_ABI void dumpDbgValues() const;
115
116 /// Return the DbgMarker for the position given by \p It, so that DbgRecords
117 /// can be inserted there. This will either be nullptr if not present, a
118 /// DbgMarker, or TrailingDbgRecords if It is end().
120
121 /// Return the DbgMarker for the position that comes after \p I. \see
122 /// BasicBlock::getMarker, this can be nullptr, a DbgMarker, or
123 /// TrailingDbgRecords if there is no next instruction.
125
126 /// Insert a DbgRecord into a block at the position given by \p I.
128
129 /// Insert a DbgRecord into a block at the position given by \p Here.
132
133 /// Eject any debug-info trailing at the end of a block. DbgRecords can
134 /// transiently be located "off the end" of a block if the blocks terminator
135 /// is temporarily removed. Once a terminator is re-inserted this method will
136 /// move such DbgRecords back to the right place (ahead of the terminator).
138
139 /// In rare circumstances instructions can be speculatively removed from
140 /// blocks, and then be re-inserted back into that position later. When this
141 /// happens in RemoveDIs debug-info mode, some special patching-up needs to
142 /// occur: inserting into the middle of a sequence of dbg.value intrinsics
143 /// does not have an equivalent with DbgRecords.
144 LLVM_ABI void
146 std::optional<DbgRecord::self_iterator> Pos);
147
148private:
149 void setParent(Function *parent);
150
151 /// Constructor.
152 ///
153 /// If the function parameter is specified, the basic block is automatically
154 /// inserted at either the end of the function (if InsertBefore is null), or
155 /// before the specified basic block.
156 LLVM_ABI explicit BasicBlock(LLVMContext &C, const Twine &Name = "",
157 Function *Parent = nullptr,
158 BasicBlock *InsertBefore = nullptr);
159
160public:
161 BasicBlock(const BasicBlock &) = delete;
162 BasicBlock &operator=(const BasicBlock &) = delete;
164
165 /// Get the context in which this basic block lives.
167
168 /// Instruction iterators...
173
174 // These functions and classes need access to the instruction list.
179 friend class llvm::SymbolTableListTraits<
180 llvm::Instruction, ilist_iterator_bits<true>, ilist_parent<BasicBlock>>;
181 friend class llvm::ilist_node_with_parent<llvm::Instruction, llvm::BasicBlock,
182 ilist_iterator_bits<true>,
183 ilist_parent<BasicBlock>>;
184
185 // Friendly methods that need to access us for the maintenence of
186 // debug-info attachments.
187 friend void Instruction::insertBefore(BasicBlock::iterator InsertPos);
188 friend void Instruction::insertAfter(Instruction *InsertPos);
189 friend void Instruction::insertAfter(BasicBlock::iterator InsertPos);
190 friend void Instruction::insertBefore(BasicBlock &BB,
191 InstListType::iterator InsertPos);
192 friend void Instruction::moveBeforeImpl(BasicBlock &BB,
193 InstListType::iterator I,
194 bool Preserve);
195 friend iterator_range<DbgRecord::self_iterator>
197 const Instruction *From, std::optional<DbgRecord::self_iterator> FromHere,
198 bool InsertAtHead);
199
200 /// Creates a new BasicBlock.
201 ///
202 /// If the Parent parameter is specified, the basic block is automatically
203 /// inserted at either the end of the function (if InsertBefore is 0), or
204 /// before the specified basic block.
205 static BasicBlock *Create(LLVMContext &Context, const Twine &Name = "",
206 Function *Parent = nullptr,
207 BasicBlock *InsertBefore = nullptr) {
208 return new BasicBlock(Context, Name, Parent, InsertBefore);
209 }
210
211 /// Return the enclosing method, or null if none.
212 const Function *getParent() const { return Parent; }
213 Function *getParent() { return Parent; }
214
215 /// Return the module owning the function this basic block belongs to, or
216 /// nullptr if the function does not have a module.
217 ///
218 /// Note: this is undefined behavior if the block does not have a parent.
219 LLVM_ABI const Module *getModule() const;
221 return const_cast<Module *>(
222 static_cast<const BasicBlock *>(this)->getModule());
223 }
224
225 /// Get the data layout of the module this basic block belongs to.
226 ///
227 /// Requires the basic block to have a parent module.
228 LLVM_ABI const DataLayout &getDataLayout() const;
229
230 /// Returns whether the block has a terminator.
232 return !InstList.empty() && InstList.back().isTerminator();
233 }
234
235 /// Returns the terminator instruction; assumes that the block is well-formed.
237 assert(hasTerminator() && "cannot get terminator of non-well-formed block");
238 return &InstList.back();
239 }
241 return const_cast<Instruction *>(
242 static_cast<const BasicBlock *>(this)->getTerminator());
243 }
244
245 /// Returns the terminator instruction if the block is well formed or
246 /// null if the block is not well formed.
248 return hasTerminator() ? getTerminator() : nullptr;
249 }
251 return hasTerminator() ? getTerminator() : nullptr;
252 }
253
254 /// Returns the call instruction calling \@llvm.experimental.deoptimize
255 /// prior to the terminating return instruction of this basic block, if such
256 /// a call is present. Otherwise, returns null.
259 return const_cast<CallInst *>(
260 static_cast<const BasicBlock *>(this)->getTerminatingDeoptimizeCall());
261 }
262
263 /// Returns the call instruction calling \@llvm.experimental.deoptimize
264 /// that is present either in current basic block or in block that is a unique
265 /// successor to current block, if such call is present. Otherwise, returns
266 /// null.
269 return const_cast<CallInst *>(static_cast<const BasicBlock *>(this)
270 ->getPostdominatingDeoptimizeCall());
271 }
272
273 /// Returns the call instruction marked 'musttail' prior to the terminating
274 /// return instruction of this basic block, if such a call is present.
275 /// Otherwise, returns null.
278 return const_cast<CallInst *>(
279 static_cast<const BasicBlock *>(this)->getTerminatingMustTailCall());
280 }
281
282 /// Returns an iterator to the first instruction in this block that is not a
283 /// PHINode instruction.
284 ///
285 /// When adding instructions to the beginning of the basic block, they should
286 /// be added before the returned value, not before the first instruction,
287 /// which might be PHI. Returns end() if there's no non-PHI instruction.
288 ///
289 /// Avoid unwrapping the iterator to an Instruction* before inserting here,
290 /// as important debug-info is preserved in the iterator.
294 static_cast<const BasicBlock *>(this)->getFirstNonPHIIt().getNonConst();
295 It.setHeadBit(true);
296 return It;
297 }
298
299 /// Returns an iterator to the first instruction in this block that is not a
300 /// PHINode or a pseudo operation.
303 return static_cast<const BasicBlock *>(this)
305 .getNonConst();
306 }
307
308 /// Returns an iterator to the first instruction in this block that is not a
309 /// PHINode, a lifetime intrinsic, or a pseudo operation.
312 return static_cast<const BasicBlock *>(this)
314 .getNonConst();
315 }
316
317 /// Returns an iterator to the first instruction in this block that is
318 /// suitable for inserting a non-PHI instruction.
319 ///
320 /// In particular, it skips all PHIs and LandingPad instructions.
323 return static_cast<const BasicBlock *>(this)
325 .getNonConst();
326 }
327
328 /// Returns true if there is a valid insertion point for non-PHI instructions
329 /// in this block. Returns false for blocks that can only contain PHI nodes,
330 /// such as blocks with a catchswitch terminator.
331 ///
332 /// This is an O(1) check, unlike getFirstInsertionPt() which must scan
333 /// through all PHI nodes.
334 bool hasInsertionPt() const {
335 return getTerminator()->getOpcode() != Instruction::CatchSwitch;
336 }
337
338 /// Returns an iterator to the first instruction in this block that is
339 /// not a PHINode, a static alloca, or any pseudo operation.
342 return static_cast<const BasicBlock *>(this)
344 .getNonConst();
345 }
346
347 /// Returns the first potential AsynchEH faulty instruction
348 /// currently it checks for loads/stores (which may dereference a null
349 /// pointer) and calls/invokes (which may propagate exceptions)
352 return const_cast<Instruction *>(
353 static_cast<const BasicBlock *>(this)->getFirstMayFaultInst());
354 }
355
356 /// Unlink 'this' from the containing function, but do not delete it.
358
359 /// Unlink 'this' from the containing function and delete it.
360 ///
361 // \returns an iterator pointing to the element after the erased one.
363
364 /// Unlink this basic block from its current function and insert it into
365 /// the function that \p MovePos lives in, right before \p MovePos.
366 inline void moveBefore(BasicBlock *MovePos) {
367 moveBefore(MovePos->getIterator());
368 }
370
371 /// Unlink this basic block from its current function and insert it
372 /// right after \p MovePos in the function \p MovePos lives in.
373 LLVM_ABI void moveAfter(BasicBlock *MovePos);
374
375 /// Insert unlinked basic block into a function.
376 ///
377 /// Inserts an unlinked basic block into \c Parent. If \c InsertBefore is
378 /// provided, inserts before that basic block, otherwise inserts at the end.
379 ///
380 /// \pre \a getParent() is \c nullptr.
381 LLVM_ABI void insertInto(Function *Parent,
382 BasicBlock *InsertBefore = nullptr);
383
384 /// Return the predecessor of this block if it has a single predecessor
385 /// block. Otherwise return a null pointer.
387 BasicBlock *getSinglePredecessor() {
388 return const_cast<BasicBlock *>(
389 static_cast<const BasicBlock *>(this)->getSinglePredecessor());
390 }
391
392 /// Return the predecessor of this block if it has a unique predecessor
393 /// block. Otherwise return a null pointer.
394 ///
395 /// Note that unique predecessor doesn't mean single edge, there can be
396 /// multiple edges from the unique predecessor to this block (for example a
397 /// switch statement with multiple cases having the same destination).
399 BasicBlock *getUniquePredecessor() {
400 return const_cast<BasicBlock *>(
401 static_cast<const BasicBlock *>(this)->getUniquePredecessor());
402 }
403
404 /// Return true if this block has exactly N predecessors.
405 LLVM_ABI bool hasNPredecessors(unsigned N) const;
406
407 /// Return true if this block has N predecessors or more.
408 LLVM_ABI bool hasNPredecessorsOrMore(unsigned N) const;
409
410 /// Return the successor of this block if it has a single successor.
411 /// Otherwise return a null pointer.
412 ///
413 /// This method is analogous to getSinglePredecessor above.
415 BasicBlock *getSingleSuccessor() {
416 return const_cast<BasicBlock *>(
417 static_cast<const BasicBlock *>(this)->getSingleSuccessor());
418 }
419
420 /// Return the successor of this block if it has a unique successor.
421 /// Otherwise return a null pointer.
422 ///
423 /// This method is analogous to getUniquePredecessor above.
425 BasicBlock *getUniqueSuccessor() {
426 return const_cast<BasicBlock *>(
427 static_cast<const BasicBlock *>(this)->getUniqueSuccessor());
428 }
429
430 /// Print the basic block to an output stream with an optional
431 /// AssemblyAnnotationWriter.
432 LLVM_ABI void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW = nullptr,
433 bool ShouldPreserveUseListOrder = false,
434 bool IsForDebug = false) const;
435
436 //===--------------------------------------------------------------------===//
437 /// Instruction iterator methods
438 ///
439 inline iterator begin() {
440 iterator It = InstList.begin();
441 // Set the head-inclusive bit to indicate that this iterator includes
442 // any debug-info at the start of the block. This is a no-op unless the
443 // appropriate CMake flag is set.
444 It.setHeadBit(true);
445 return It;
446 }
447 inline const_iterator begin() const {
448 const_iterator It = InstList.begin();
449 It.setHeadBit(true);
450 return It;
451 }
452 inline iterator end() { return InstList.end(); }
453 inline const_iterator end() const { return InstList.end(); }
454
455 inline reverse_iterator rbegin() { return InstList.rbegin(); }
456 inline const_reverse_iterator rbegin() const { return InstList.rbegin(); }
457 inline reverse_iterator rend() { return InstList.rend(); }
458 inline const_reverse_iterator rend() const { return InstList.rend(); }
459
460 inline size_t size() const { return InstList.size(); }
461 inline bool empty() const { return InstList.empty(); }
462 inline const Instruction &front() const { return InstList.front(); }
463 inline Instruction &front() { return InstList.front(); }
464 inline const Instruction &back() const { return InstList.back(); }
465 inline Instruction &back() { return InstList.back(); }
466
467 /// Iterator to walk just the phi nodes in the basic block.
468 template <typename PHINodeT = PHINode, typename BBIteratorT = iterator>
469 class phi_iterator_impl
470 : public iterator_facade_base<phi_iterator_impl<PHINodeT, BBIteratorT>,
471 std::forward_iterator_tag, PHINodeT> {
472 friend BasicBlock;
473
474 PHINodeT *PN;
475
476 phi_iterator_impl(PHINodeT *PN) : PN(PN) {}
477
478 public:
479 // Allow default construction to build variables, but this doesn't build
480 // a useful iterator.
481 phi_iterator_impl() = default;
482
483 // Allow conversion between instantiations where valid.
484 template <typename PHINodeU, typename BBIteratorU,
485 typename = std::enable_if_t<
486 std::is_convertible<PHINodeU *, PHINodeT *>::value>>
487 phi_iterator_impl(const phi_iterator_impl<PHINodeU, BBIteratorU> &Arg)
488 : PN(Arg.PN) {}
489
490 bool operator==(const phi_iterator_impl &Arg) const { return PN == Arg.PN; }
491
492 PHINodeT &operator*() const { return *PN; }
493
494 using phi_iterator_impl::iterator_facade_base::operator++;
495 phi_iterator_impl &operator++() {
496 assert(PN && "Cannot increment the end iterator!");
497 PN = dyn_cast<PHINodeT>(std::next(BBIteratorT(PN)));
498 return *this;
499 }
500 };
504
505 /// Returns a range that iterates over the phis in the basic block.
506 ///
507 /// Note that this cannot be used with basic blocks that have no terminator.
509 return const_cast<BasicBlock *>(this)->phis();
510 }
512
513private:
514 /// Return the underlying instruction list container.
515 /// This is deliberately private because we have implemented an adequate set
516 /// of functions to modify the list, including BasicBlock::splice(),
517 /// BasicBlock::erase(), Instruction::insertInto() etc.
518 const InstListType &getInstList() const { return InstList; }
519 InstListType &getInstList() { return InstList; }
520
521 /// Returns a pointer to a member of the instruction list.
522 /// This is private on purpose, just like `getInstList()`.
523 static InstListType BasicBlock::*getSublistAccess(Instruction *) {
524 return &BasicBlock::InstList;
525 }
526
527 /// Dedicated function for splicing debug-info: when we have an empty
528 /// splice (i.e. zero instructions), the caller may still intend any
529 /// debug-info in between the two "positions" to be spliced.
530 void spliceDebugInfoEmptyBlock(BasicBlock::iterator ToIt, BasicBlock *FromBB,
531 BasicBlock::iterator FromBeginIt,
532 BasicBlock::iterator FromEndIt);
533
534 /// Perform any debug-info specific maintenence for the given splice
535 /// activity. In the DbgRecord debug-info representation, debug-info is not
536 /// in instructions, and so it does not automatically move from one block
537 /// to another.
538 void spliceDebugInfo(BasicBlock::iterator ToIt, BasicBlock *FromBB,
539 BasicBlock::iterator FromBeginIt,
540 BasicBlock::iterator FromEndIt);
541 void spliceDebugInfoImpl(BasicBlock::iterator ToIt, BasicBlock *FromBB,
542 BasicBlock::iterator FromBeginIt,
543 BasicBlock::iterator FromEndIt);
544
545 enum {
546 HasAddressTaken = 1 << 0,
547 InstrOrderValid = 1 << 1,
548 };
549
550 void setHasAddressTaken(bool B) {
551 if (B)
552 SubclassOptionalData |= HasAddressTaken;
553 else
554 SubclassOptionalData &= ~HasAddressTaken;
555 }
556
557 /// Shadow Value::setValueSubclassData with a private forwarding method so
558 /// that any future subclasses cannot accidentally use it.
559 void setValueSubclassData(unsigned short D) {
561 }
562
563public:
564 /// Returns a pointer to the symbol table if one exists.
565 LLVM_ABI ValueSymbolTable *getValueSymbolTable();
566
567 /// Methods for support type inquiry through isa, cast, and dyn_cast.
568 static bool classof(const Value *V) {
569 return V->getValueID() == Value::BasicBlockVal;
570 }
571
572 /// Cause all subinstructions to "let go" of all the references that said
573 /// subinstructions are maintaining.
574 ///
575 /// This allows one to 'delete' a whole class at a time, even though there may
576 /// be circular references... first all references are dropped, and all use
577 /// counts go to zero. Then everything is delete'd for real. Note that no
578 /// operations are valid on an object that has "dropped all references",
579 /// except operator delete.
581
582 /// Update PHI nodes in this BasicBlock before removal of predecessor \p Pred.
583 /// Note that this function does not actually remove the predecessor.
584 ///
585 /// If \p KeepOneInputPHIs is true then don't remove PHIs that are left with
586 /// zero or one incoming values, and don't simplify PHIs with all incoming
587 /// values the same.
589 bool KeepOneInputPHIs = false);
590
591 LLVM_ABI bool canSplitPredecessors() const;
592
593 /// Split the basic block into two basic blocks at the specified instruction.
594 ///
595 /// Note that all instructions BEFORE the specified iterator
596 /// stay as part of the original basic block, an unconditional branch is added
597 /// to the original BB, and the rest of the instructions in the BB are moved
598 /// to the new BB, including the old terminator. The newly formed basic block
599 /// is returned. This function invalidates the specified iterator.
600 ///
601 /// Note that this only works on well formed basic blocks (must have a
602 /// terminator), and \p 'I' must not be the end of instruction list (which
603 /// would cause a degenerate basic block to be formed, having a terminator
604 /// inside of the basic block).
605 ///
606 /// Also note that this doesn't preserve any passes. To split blocks while
607 /// keeping loop information consistent, use the SplitBlock utility function.
608 LLVM_ABI BasicBlock *splitBasicBlock(iterator I, const Twine &BBName = "");
609 BasicBlock *splitBasicBlock(Instruction *I, const Twine &BBName = "") {
610 return splitBasicBlock(I->getIterator(), BBName);
611 }
612
613 /// Split the basic block into two basic blocks at the specified instruction
614 /// and insert the new basic blocks as the predecessor of the current block.
615 ///
616 /// This function ensures all instructions AFTER and including the specified
617 /// iterator \p I are part of the original basic block. All Instructions
618 /// BEFORE the iterator \p I are moved to the new BB and an unconditional
619 /// branch is added to the new BB. The new basic block is returned.
620 ///
621 /// Note that this only works on well formed basic blocks (must have a
622 /// terminator), and \p 'I' must not be the end of instruction list (which
623 /// would cause a degenerate basic block to be formed, having a terminator
624 /// inside of the basic block). \p 'I' cannot be a iterator for a PHINode
625 /// with multiple incoming blocks.
626 ///
627 /// Also note that this doesn't preserve any passes. To split blocks while
628 /// keeping loop information consistent, use the SplitBlockBefore utility
629 /// function.
631 const Twine &BBName = "");
632 BasicBlock *splitBasicBlockBefore(Instruction *I, const Twine &BBName = "") {
633 return splitBasicBlockBefore(I->getIterator(), BBName);
634 }
635
636 /// Transfer all instructions from \p FromBB to this basic block at \p ToIt.
637 void splice(BasicBlock::iterator ToIt, BasicBlock *FromBB) {
638 splice(ToIt, FromBB, FromBB->begin(), FromBB->end());
639 }
640
641 /// Transfer one instruction from \p FromBB at \p FromIt to this basic block
642 /// at \p ToIt.
643 void splice(BasicBlock::iterator ToIt, BasicBlock *FromBB,
644 BasicBlock::iterator FromIt) {
645 auto FromItNext = std::next(FromIt);
646 // Single-element splice is a noop if destination == source.
647 if (ToIt == FromIt || ToIt == FromItNext)
648 return;
649 splice(ToIt, FromBB, FromIt, FromItNext);
650 }
651
652 /// Transfer a range of instructions that belong to \p FromBB from \p
653 /// FromBeginIt to \p FromEndIt, to this basic block at \p ToIt.
655 BasicBlock::iterator FromBeginIt,
656 BasicBlock::iterator FromEndIt);
657
658 /// Erases a range of instructions from \p FromIt to (not including) \p ToIt.
659 /// \Returns \p ToIt.
662
663 /// Returns true if there are any uses of this basic block other than
664 /// direct branches, switches, etc. to it.
665 bool hasAddressTaken() const {
666 return SubclassOptionalData & HasAddressTaken;
667 }
668
669 /// Update all phi nodes in this basic block to refer to basic block \p New
670 /// instead of basic block \p Old.
672
673 /// Update all phi nodes in this basic block's successors to refer to basic
674 /// block \p New instead of basic block \p Old.
676
677 /// Update all phi nodes in this basic block's successors to refer to basic
678 /// block \p New instead of to it.
680
681 /// Return true if this basic block is an exception handling block.
682 bool isEHPad() const { return getFirstNonPHIIt()->isEHPad(); }
683
684 /// Return true if this basic block is a landing pad.
685 ///
686 /// Being a ``landing pad'' means that the basic block is the destination of
687 /// the 'unwind' edge of an invoke instruction.
688 LLVM_ABI bool isLandingPad() const;
689
690 /// Return the landingpad instruction associated with the landing pad.
693 return const_cast<LandingPadInst *>(
694 static_cast<const BasicBlock *>(this)->getLandingPadInst());
695 }
696
697 /// Return true if it is legal to hoist instructions into this block.
698 LLVM_ABI bool isLegalToHoistInto() const;
699
700 /// Return true if this is the entry block of the containing function.
701 /// This method can only be used on blocks that have a parent function.
702 LLVM_ABI bool isEntryBlock() const;
703
704 LLVM_ABI std::optional<uint64_t> getIrrLoopHeaderWeight() const;
705
706 /// Returns true if the Order field of child Instructions is valid.
707 bool isInstrOrderValid() const {
708 return SubclassOptionalData & InstrOrderValid;
709 }
710
711 /// Mark instruction ordering invalid. Done on every instruction insert.
714 SubclassOptionalData &= ~InstrOrderValid;
715 }
716
717 /// Renumber instructions and mark the ordering as valid.
719
720 /// Asserts that instruction order numbers are marked invalid, or that they
721 /// are in ascending order. This is constant time if the ordering is invalid,
722 /// and linear in the number of instructions if the ordering is valid. Callers
723 /// should be careful not to call this in ways that make common operations
724 /// O(n^2). For example, it takes O(n) time to assign order numbers to
725 /// instructions, so the order should be validated no more than once after
726 /// each ordering to ensure that transforms have the same algorithmic
727 /// complexity when asserts are enabled as when they are disabled.
728 LLVM_ABI void validateInstrOrdering() const;
729};
730
731// Create wrappers for C Binding types (see CBindingWrapping.h).
733
734#if LLVM_ENABLE_ABI_BREAKING_CHECKS == 0
735/// In release builds, this is a no-op. For assert builds, the checks are
736/// implemented in the .cpp file to avoid circular header deps.
738#endif
739
740// Specialize DenseMapInfo for iterators, so that ththey can be installed into
741// maps and sets. The iterator is made up of its node pointer, and the
742// debug-info "head" bit.
743template <> struct DenseMapInfo<BasicBlock::iterator> {
744 static unsigned getHashValue(const BasicBlock::iterator &It) {
746 reinterpret_cast<void *>(It.getNodePtr())) ^
747 (unsigned)It.getHeadBit();
748 }
749
750 static bool isEqual(const BasicBlock::iterator &LHS,
751 const BasicBlock::iterator &RHS) {
752 return LHS == RHS && LHS.getHeadBit() == RHS.getHeadBit();
753 }
754};
755
756} // end namespace llvm
757
758#endif // LLVM_IR_BASICBLOCK_H
SDValue Pred
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref)
#define LLVM_ABI
Definition Compiler.h:211
#define LLVM_READONLY
Definition Compiler.h:334
This file defines the DenseMap class.
#define I(x, y, z)
Definition MD5.cpp:57
Value * RHS
Value * LHS
Iterator to walk just the phi nodes in the basic block.
Definition BasicBlock.h:471
bool operator==(const phi_iterator_impl &Arg) const
Definition BasicBlock.h:490
phi_iterator_impl(const phi_iterator_impl< PHINodeU, BBIteratorU > &Arg)
Definition BasicBlock.h:487
phi_iterator_impl & operator++()
Definition BasicBlock.h:495
LLVM Basic Block Representation.
Definition BasicBlock.h:61
LLVM_ABI BasicBlock::iterator erase(BasicBlock::iterator FromIt, BasicBlock::iterator ToIt)
Erases a range of instructions from FromIt to (not including) ToIt.
bool hasInsertionPt() const
Returns true if there is a valid insertion point for non-PHI instructions in this block.
Definition BasicBlock.h:334
phi_iterator_impl<> phi_iterator
Definition BasicBlock.h:501
LLVM_ABI void replaceSuccessorsPhiUsesWith(BasicBlock *Old, BasicBlock *New)
Update all phi nodes in this basic block's successors to refer to basic block New instead of basic bl...
BasicBlock(const BasicBlock &)=delete
iterator end()
Definition BasicBlock.h:452
unsigned getNumber() const
Definition BasicBlock.h:94
Instruction * getFirstMayFaultInst()
Definition BasicBlock.h:351
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:439
LLVM_ABI void deleteTrailingDbgRecords()
Delete any trailing DbgRecords at the end of this block, see setTrailingDbgRecords.
InstListType::iterator getFirstNonPHIOrLifetime()
Definition BasicBlock.h:311
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
Definition BasicBlock.h:508
LLVM_ABI const LandingPadInst * getLandingPadInst() const
Return the landingpad instruction associated with the landing pad.
LLVM_ABI void setTrailingDbgRecords(DbgMarker *M)
Record that the collection of DbgRecords in M "trails" after the last instruction of this block.
LLVM_ABI const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
LLVM_ABI BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="")
Split the basic block into two basic blocks at the specified instruction.
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:212
reverse_iterator rbegin()
Definition BasicBlock.h:455
bool hasTerminator() const LLVM_READONLY
Returns whether the block has a terminator.
Definition BasicBlock.h:231
static bool classof(const Value *V)
Methods for support type inquiry through isa, cast, and dyn_cast.
Definition BasicBlock.h:568
LLVM_ABI void renumberInstructions()
Renumber instructions and mark the ordering as valid.
InstListType::iterator getFirstNonPHIIt()
Definition BasicBlock.h:292
bool empty() const
Definition BasicBlock.h:461
LLVM_ABI DbgMarker * createMarker(Instruction *I)
Attach a DbgMarker to the given instruction.
const Instruction & back() const
Definition BasicBlock.h:464
LLVM_ABI BasicBlock * splitBasicBlockBefore(iterator I, const Twine &BBName="")
Split the basic block into two basic blocks at the specified instruction and insert the new basic blo...
bool hasAddressTaken() const
Returns true if there are any uses of this basic block other than direct branches,...
Definition BasicBlock.h:665
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
LLVM_ABI void insertDbgRecordBefore(DbgRecord *DR, InstListType::iterator Here)
Insert a DbgRecord into a block at the position given by Here.
BasicBlock * splitBasicBlockBefore(Instruction *I, const Twine &BBName="")
Definition BasicBlock.h:632
void invalidateOrders()
Mark instruction ordering invalid. Done on every instruction insert.
Definition BasicBlock.h:712
friend void Instruction::removeFromParent()
LLVM_ABI void convertToNewDbgValues()
Convert variable location debugging information stored in dbg.value intrinsics into DbgMarkers / DbgR...
LLVM_ABI void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW=nullptr, bool ShouldPreserveUseListOrder=false, bool IsForDebug=false) const
Print the basic block to an output stream with an optional AssemblyAnnotationWriter.
InstListType::const_iterator const_iterator
Definition BasicBlock.h:170
InstListType::iterator getFirstNonPHIOrPseudoProbe()
Definition BasicBlock.h:302
BasicBlock * getUniqueSuccessor()
Definition BasicBlock.h:425
Module * getModule()
Definition BasicBlock.h:220
Instruction & front()
Definition BasicBlock.h:463
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition BasicBlock.h:205
BasicBlock * getSingleSuccessor()
Definition BasicBlock.h:415
friend BasicBlock::iterator Instruction::eraseFromParent()
LLVM_ABI bool isEntryBlock() const
Return true if this is the entry block of the containing function.
LLVM_ABI ValueSymbolTable * getValueSymbolTable()
Returns a pointer to the symbol table if one exists.
BasicBlock * getUniquePredecessor()
Definition BasicBlock.h:399
LLVM_ABI void moveAfter(BasicBlock *MovePos)
Unlink this basic block from its current function and insert it right after MovePos in the function M...
LLVM_ABI bool hasNPredecessors(unsigned N) const
Return true if this block has exactly N predecessors.
BasicBlock * getSinglePredecessor()
Definition BasicBlock.h:387
LLVM_ABI bool convertFromNewDbgValues()
Convert variable location debugging information stored in DbgMarkers and DbgRecords into the dbg....
LLVM_ABI const BasicBlock * getUniqueSuccessor() const
Return the successor of this block if it has a unique successor.
LLVM_ABI const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
LLVM_ABI InstListType::const_iterator getFirstNonPHIOrLifetime() const
Returns an iterator to the first instruction in this block that is not a PHINode, a lifetime intrinsi...
LLVM_ABI std::optional< uint64_t > getIrrLoopHeaderWeight() const
const Instruction & front() const
Definition BasicBlock.h:462
LLVM_ABI void dumpDbgValues() const
LLVM_ABI const CallInst * getTerminatingDeoptimizeCall() const
Returns the call instruction calling @llvm.experimental.deoptimize prior to the terminating return in...
iterator getFirstNonPHIOrAlloca()
Definition BasicBlock.h:341
Instruction & back()
Definition BasicBlock.h:465
InstListType::reverse_iterator reverse_iterator
Definition BasicBlock.h:171
LLVM_ABI void replacePhiUsesWith(BasicBlock *Old, BasicBlock *New)
Update all phi nodes in this basic block to refer to basic block New instead of basic block Old.
LLVM_ABI const BasicBlock * getUniquePredecessor() const
Return the predecessor of this block if it has a unique predecessor block.
const Instruction * getTerminatorOrNull() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition BasicBlock.h:247
LLVM_ABI const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
SymbolTableList< Instruction, ilist_iterator_bits< true >, ilist_parent< BasicBlock > > InstListType
Definition BasicBlock.h:63
LLVM_ABI void flushTerminatorDbgRecords()
Eject any debug-info trailing at the end of a block.
void splice(BasicBlock::iterator ToIt, BasicBlock *FromBB, BasicBlock::iterator FromIt)
Transfer one instruction from FromBB at FromIt to this basic block at ToIt.
Definition BasicBlock.h:643
LLVM_ABI InstListType::const_iterator getFirstNonPHIOrPseudoProbe() const
Returns an iterator to the first instruction in this block that is not a PHINode or a pseudo operatio...
Function * getParent()
Definition BasicBlock.h:213
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this basic block belongs to.
const_reverse_iterator rend() const
Definition BasicBlock.h:458
reverse_iterator rend()
Definition BasicBlock.h:457
LLVM_ABI void insertDbgRecordAfter(DbgRecord *DR, Instruction *I)
Insert a DbgRecord into a block at the position given by I.
LLVM_ABI DbgMarker * getMarker(InstListType::iterator It)
Return the DbgMarker for the position given by It, so that DbgRecords can be inserted there.
LLVM_ABI ~BasicBlock()
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:169
LLVM_ABI LLVMContext & getContext() const
Get the context in which this basic block lives.
LLVM_ABI void validateInstrOrdering() const
Asserts that instruction order numbers are marked invalid, or that they are in ascending order.
Definition BasicBlock.h:737
CallInst * getPostdominatingDeoptimizeCall()
Definition BasicBlock.h:268
LLVM_ABI const_iterator getFirstNonPHIOrAlloca() const
Returns an iterator to the first instruction in this block that is not a PHINode, a static alloca,...
const_iterator begin() const
Definition BasicBlock.h:447
friend class BlockAddress
Definition BasicBlock.h:72
Instruction * getTerminator()
Definition BasicBlock.h:240
BasicBlock & operator=(const BasicBlock &)=delete
CallInst * getTerminatingDeoptimizeCall()
Definition BasicBlock.h:258
friend class Function
Definition BasicBlock.h:68
LLVM_ABI void dropAllReferences()
Cause all subinstructions to "let go" of all the references that said subinstructions are maintaining...
size_t size() const
Definition BasicBlock.h:460
LLVM_ABI void reinsertInstInDbgRecords(Instruction *I, std::optional< DbgRecord::self_iterator > Pos)
In rare circumstances instructions can be speculatively removed from blocks, and then be re-inserted ...
void moveBefore(BasicBlock *MovePos)
Unlink this basic block from its current function and insert it into the function that MovePos lives ...
Definition BasicBlock.h:366
LLVM_ABI bool isLandingPad() const
Return true if this basic block is a landing pad.
BasicBlock * splitBasicBlock(Instruction *I, const Twine &BBName="")
Definition BasicBlock.h:609
InstListType::const_reverse_iterator const_reverse_iterator
Definition BasicBlock.h:172
bool isEHPad() const
Return true if this basic block is an exception handling block.
Definition BasicBlock.h:682
LLVM_ABI DbgMarker * getTrailingDbgRecords()
Fetch the collection of DbgRecords that "trail" after the last instruction of this block,...
CallInst * getTerminatingMustTailCall()
Definition BasicBlock.h:277
phi_iterator_impl< const PHINode, BasicBlock::const_iterator > const_phi_iterator
Definition BasicBlock.h:502
LLVM_ABI bool canSplitPredecessors() const
const_iterator end() const
Definition BasicBlock.h:453
LLVM_ABI const CallInst * getTerminatingMustTailCall() const
Returns the call instruction marked 'musttail' prior to the terminating return instruction of this ba...
friend BasicBlock::iterator Instruction::insertInto(BasicBlock *BB, BasicBlock::iterator It)
LLVM_ABI bool isLegalToHoistInto() const
Return true if it is legal to hoist instructions into this block.
LLVM_ABI bool hasNPredecessorsOrMore(unsigned N) const
Return true if this block has N predecessors or more.
bool isInstrOrderValid() const
Returns true if the Order field of child Instructions is valid.
Definition BasicBlock.h:707
LLVM_ABI const CallInst * getPostdominatingDeoptimizeCall() const
Returns the call instruction calling @llvm.experimental.deoptimize that is present either in current ...
Instruction * getTerminatorOrNull()
Definition BasicBlock.h:250
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
Definition BasicBlock.h:236
LLVM_ABI DbgMarker * getNextMarker(Instruction *I)
Return the DbgMarker for the position that comes after I.
LLVM_ABI const Instruction * getFirstMayFaultInst() const
Returns the first potential AsynchEH faulty instruction currently it checks for loads/stores (which m...
void splice(BasicBlock::iterator ToIt, BasicBlock *FromBB)
Transfer all instructions from FromBB to this basic block at ToIt.
Definition BasicBlock.h:637
const_reverse_iterator rbegin() const
Definition BasicBlock.h:456
LandingPadInst * getLandingPadInst()
Definition BasicBlock.h:692
LLVM_ABI const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr if the function does no...
iterator getFirstInsertionPt()
Definition BasicBlock.h:322
LLVM_ABI void removePredecessor(BasicBlock *Pred, bool KeepOneInputPHIs=false)
Update PHI nodes in this BasicBlock before removal of predecessor Pred.
This class represents a function call, abstracting a target machine's calling convention.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
Per-instruction record of debug-info.
Base class for non-instruction debug metadata records that have positions within IR.
LLVM_ABI void removeFromParent()
This method unlinks 'this' from the containing basic block, but does not delete it.
LLVM_ABI iterator_range< simple_ilist< DbgRecord >::iterator > cloneDebugInfoFrom(const Instruction *From, std::optional< simple_ilist< DbgRecord >::iterator > FromHere=std::nullopt, bool InsertAtHead=false)
Clone any debug-info attached to From onto this instruction.
LLVM_ABI void insertBefore(InstListType::iterator InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified position.
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
LLVM_ABI void insertAfter(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately after the specified instruction.
LLVM_ABI InstListType::iterator insertInto(BasicBlock *ParentBB, InstListType::iterator It)
Inserts an unlinked instruction into ParentBB at position It and returns the iterator of the inserted...
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:76
The landingpad instruction holds all of the information necessary to generate correct exception handl...
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
List that automatically updates parent links and symbol tables.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
This class provides a symbol table of name/value pairs.
LLVM_ABI Value(Type *Ty, unsigned scid)
Definition Value.cpp:49
unsigned char SubclassOptionalData
Hold arbitary subclass data.
Definition Value.h:85
void setValueSubclassData(unsigned short D)
Definition Value.h:861
self_iterator getIterator()
Definition ilist_node.h:123
An ilist node that can access its parent list.
Definition ilist_node.h:316
typename base_list_type::const_reverse_iterator const_reverse_iterator
Definition ilist.h:124
typename base_list_type::reverse_iterator reverse_iterator
Definition ilist.h:123
typename base_list_type::iterator iterator
Definition ilist.h:121
typename base_list_type::const_iterator const_iterator
Definition ilist.h:122
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition iterator.h:80
A range adaptor for a pair of iterators.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
struct LLVMOpaqueBasicBlock * LLVMBasicBlockRef
Represents a basic block of instructions in LLVM IR.
Definition Types.h:82
This file defines classes to implement an intrusive doubly linked list class (i.e.
This file defines the ilist_node class template, which is a convenient base class for creating classe...
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
#define N
static unsigned getHashValue(const BasicBlock::iterator &It)
Definition BasicBlock.h:744
static bool isEqual(const BasicBlock::iterator &LHS, const BasicBlock::iterator &RHS)
Definition BasicBlock.h:750
An information struct used to provide DenseMap with the various necessary components for a given valu...
Option to add extra bits to the ilist_iterator.
Option to add a pointer to this list's owner in every node.