26#define DEBUG_TYPE "ir"
27STATISTIC(NumInstrRenumberings,
"Number of renumberings across all blocks");
31 return F->getValueSymbolTable();
52 insertInto(NewParent, InsertBefore);
55 "Cannot insert block before another block with no function!");
61 assert(NewParent &&
"Expected a parent");
62 assert(!Parent &&
"Already has a parent");
67 NewParent->
insert(NewParent->
end(),
this);
91 assert(
getParent() ==
nullptr &&
"BasicBlock still linked into the program!");
96void BasicBlock::setParent(
Function *parent) {
98 InstList.setSymTabObject(&Parent, parent);
105 return !isa<DbgInfoIntrinsic>(
I) &&
106 !(SkipPseudoOp && isa<PseudoProbeInst>(
I));
115 return !isa<DbgInfoIntrinsic>(
I) &&
116 !(SkipPseudoOp && isa<PseudoProbeInst>(
I));
150 if (InstList.empty())
152 const ReturnInst *RI = dyn_cast<ReturnInst>(&InstList.back());
153 if (!RI || RI == &InstList.front())
165 if (
auto *BI = dyn_cast<BitCastInst>(Prev)) {
168 if (!Prev || RV != Prev)
173 if (
auto *CI = dyn_cast<CallInst>(Prev)) {
174 if (CI->isMustTailCall())
181 if (InstList.empty())
183 auto *RI = dyn_cast<ReturnInst>(&InstList.back());
184 if (!RI || RI == &InstList.front())
187 if (
auto *CI = dyn_cast_or_null<CallInst>(RI->getPrevNode()))
188 if (
Function *
F = CI->getCalledFunction())
189 if (
F->getIntrinsicID() == Intrinsic::experimental_deoptimize)
200 if (!Visited.
insert(Succ).second)
208 if (InstList.empty())
211 if (isa<LoadInst>(
I) || isa<StoreInst>(
I) || isa<CallBase>(
I))
218 if (!isa<PHINode>(
I))
229 if (isa<PHINode>(
I) || isa<DbgInfoIntrinsic>(
I))
232 if (SkipPseudoOp && isa<PseudoProbeInst>(
I))
243 if (isa<PHINode>(
I) || isa<DbgInfoIntrinsic>(
I))
246 if (
I.isLifetimeStartOrEnd())
249 if (SkipPseudoOp && isa<PseudoProbeInst>(
I))
263 if (InsertPt->isEHPad()) ++InsertPt;
273 if (InsertPt->isEHPad())
278 while (InsertPt !=
End &&
279 (isa<AllocaInst>(*InsertPt) || isa<DbgInfoIntrinsic>(*InsertPt) ||
280 isa<PseudoProbeInst>(*InsertPt))) {
281 if (
const AllocaInst *AI = dyn_cast<AllocaInst>(&*InsertPt)) {
282 if (!AI->isStaticAlloca())
293 I.dropAllReferences();
298 if (PI ==
E)
return nullptr;
301 return (PI ==
E) ? ThePred :
nullptr ;
306 if (PI ==
E)
return nullptr;
309 for (;PI !=
E; ++PI) {
328 if (SI ==
E)
return nullptr;
331 return (SI ==
E) ? TheSucc :
nullptr ;
336 if (SI ==
E)
return nullptr;
339 for (;SI !=
E; ++SI) {
350 return make_range<phi_iterator>(
P,
nullptr);
354 bool KeepOneInputPHIs) {
357 "Pred is not a predecessor!");
363 unsigned NumPreds = cast<PHINode>(
front()).getNumIncomingValues();
365 Phi.removeIncomingValue(Pred, !KeepOneInputPHIs);
366 if (KeepOneInputPHIs)
375 if (
Value *PhiConstant = Phi.hasConstantValue()) {
376 Phi.replaceAllUsesWith(PhiConstant);
377 Phi.eraseFromParent();
384 if (isa<LandingPadInst>(FirstNonPHI))
401 assert(Term->getNumSuccessors() > 0);
405 return !Term->isSpecialTerminator();
410 assert(
F &&
"Block must have a parent function to use this API");
411 return this == &
F->getEntryBlock();
421 "Trying to get me to create degenerate basic block!");
430 New->splice(New->end(),
this,
I,
end());
441 New->replaceSuccessorsPhiUsesWith(
this, New);
447 "Can't use splitBasicBlockBefore on degenerate BB!");
449 "Trying to get me to create degenerate basic block!");
452 "cannot split on multi incoming phis");
459 New->splice(New->end(),
this,
begin(),
I);
486#ifdef EXPENSIVE_CHECKS
488 auto FromBBEnd = FromBB->
end();
489 for (
auto It = FromBeginIt; It != FromEndIt; ++It)
490 assert(It != FromBBEnd &&
"FromBeginIt not before FromEndIt!");
492 getInstList().
splice(ToIt, FromBB->getInstList(), FromBeginIt, FromEndIt);
497 return InstList.
erase(FromIt, ToIt);
504 PHINode *PN = dyn_cast<PHINode>(&
I);
519 Succ->replacePhiUsesWith(Old, New);
536 if (
MDNode *MDIrrLoopHeader =
538 MDString *MDName = cast<MDString>(MDIrrLoopHeader->getOperand(0));
540 auto *CI = mdconst::extract<ConstantInt>(MDIrrLoopHeader->getOperand(1));
541 return std::optional<uint64_t>(CI->getValue().getZExtValue());
548 while (isa<DbgInfoIntrinsic>(It))
559 BasicBlockBits Bits = getBasicBlockBits();
560 Bits.InstrOrderValid =
true;
561 setBasicBlockBits(Bits);
563 NumInstrRenumberings++;
575 "cached instruction ordering is incorrect");
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
an instruction to allocate memory on the stack
LLVM Basic Block Representation.
BasicBlock::iterator erase(BasicBlock::iterator FromIt, BasicBlock::iterator ToIt)
Erases a range of instructions from FromIt to (not including) ToIt.
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...
iterator begin()
Instruction iterator methods.
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
const LandingPadInst * getLandingPadInst() const
Return the landingpad instruction associated with the landing pad.
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
void renumberInstructions()
Renumber instructions and mark the ordering as valid.
iterator_range< filter_iterator< BasicBlock::const_iterator, std::function< bool(const Instruction &)> > > instructionsWithoutDebug(bool SkipPseudoOp=true) const
Return a const iterator range over the instructions in the block, skipping any debug instructions.
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,...
InstListType::const_iterator getFirstNonPHIIt() const
Iterator returning form of getFirstNonPHI.
void invalidateOrders()
Mark instruction ordering invalid. Done on every instruction insert.
friend void Instruction::removeFromParent()
InstListType::const_iterator const_iterator
const Instruction * getFirstNonPHI() const
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
const Instruction & front() const
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
bool isEntryBlock() const
Return true if this is the entry block of the containing function.
ValueSymbolTable * getValueSymbolTable()
Returns a pointer to the symbol table if one exists.
void moveAfter(BasicBlock *MovePos)
Unlink this basic block from its current function and insert it right after MovePos in the function M...
bool hasNPredecessors(unsigned N) const
Return true if this block has exactly N predecessors.
BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="", bool Before=false)
Split the basic block into two basic blocks at the specified instruction.
const BasicBlock * getUniqueSuccessor() const
Return the successor of this block if it has a unique successor.
friend iplist< Instruction >::iterator Instruction::eraseFromParent()
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
std::optional< uint64_t > getIrrLoopHeaderWeight() const
const CallInst * getTerminatingDeoptimizeCall() const
Returns the call instruction calling @llvm.experimental.deoptimize prior to the terminating return in...
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.
const Instruction * getFirstNonPHIOrDbgOrLifetime(bool SkipPseudoOp=true) const
Returns a pointer to the first instruction in this block that is not a PHINode, a debug intrinsic,...
const BasicBlock * getUniquePredecessor() const
Return the predecessor of this block if it has a unique predecessor block.
const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
const Function * getParent() const
Return the enclosing method, or null if none.
void validateInstrOrdering() const
Asserts that instruction order numbers are marked invalid, or that they are in ascending order.
const Instruction * getFirstNonPHIOrDbg(bool SkipPseudoOp=true) const
Returns a pointer to the first instruction in this block that is not a PHINode or a debug intrinsic,...
filter_iterator< BasicBlock::const_iterator, std::function< bool(constInstruction &)> >::difference_type sizeWithoutDebug() const
Return the size of the basic block ignoring debug instructions.
InstListType::iterator iterator
Instruction iterators...
LLVMContext & getContext() const
Get the context in which this basic block lives.
const_iterator getFirstNonPHIOrDbgOrAlloca() const
Returns an iterator to the first instruction in this block that is not a PHINode, a debug intrinsic,...
void dropAllReferences()
Cause all subinstructions to "let go" of all the references that said subinstructions are maintaining...
void moveBefore(BasicBlock *MovePos)
Unlink this basic block from its current function and insert it into the function that MovePos lives ...
bool isLandingPad() const
Return true if this basic block is a landing pad.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
bool canSplitPredecessors() const
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)
bool isLegalToHoistInto() const
Return true if it is legal to hoist instructions into this block.
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.
const CallInst * getPostdominatingDeoptimizeCall() const
Returns the call instruction calling @llvm.experimental.deoptimize that is present either in current ...
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.
const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr if the function does no...
void removePredecessor(BasicBlock *Pred, bool KeepOneInputPHIs=false)
Update PHI nodes in this BasicBlock before removal of predecessor Pred.
The address of a basic block.
Conditional or Unconditional Branch instruction.
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
This class represents a function call, abstracting a target machine's calling convention.
static Constant * getIntToPtr(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static Constant * get(Type *Ty, uint64_t V, bool IsSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
This is an important base class in LLVM.
void destroyConstant()
Called if some element of this constant is no longer valid.
void splice(Function::iterator ToIt, Function *FromF)
Transfer all blocks from FromF to this function at ToIt.
Function::iterator insert(Function::iterator Position, BasicBlock *BB)
Insert BB in the basic block list at Position.
Module * getParent()
Get the module that this global value is contained inside of...
bool isEHPad() const
Return true if the instruction is a variety of EH-block.
void replaceSuccessorWith(BasicBlock *OldBB, BasicBlock *NewBB)
Replace specified successor OldBB to point at the provided block.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
bool comesBefore(const Instruction *Other) const
Given an instruction Other in the same basic block as this instruction, return true if this instructi...
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
This is an important class for using LLVM in a threaded context.
The landingpad instruction holds all of the information necessary to generate correct exception handl...
StringRef getString() const
A Module instance is used to store all the information related to an LLVM module.
void replaceIncomingBlockWith(const BasicBlock *Old, BasicBlock *New)
Replace every incoming basic block Old to basic block New.
Return a value (possibly void), from a function.
Value * getReturnValue() const
Convenience accessor. Returns null if there is no return value.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
The instances of the Type class are immutable: once they are created, they are never changed.
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
static IntegerType * getInt32Ty(LLVMContext &C)
Value * getOperand(unsigned i) const
This class provides a symbol table of name/value pairs.
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
bool hasNUsesOrMore(unsigned N) const
Return true if this value has N uses or more.
Specialization of filter_iterator_base for forward iteration only.
Iterator for intrusive lists based on ilist_node.
self_iterator getIterator()
BasicBlock * getNextNode()
Get the next node, or nullptr for the list tail.
void splice(iterator where, iplist_impl &L2)
iterator erase(iterator where)
pointer remove(iterator &IT)
A range adaptor for a pair of iterators.
@ C
The default llvm calling convention, compatible with C.
This is an optimization pass for GlobalISel generic memory operations.
Interval::succ_iterator succ_end(Interval *I)
auto successors(const MachineBasicBlock *BB)
Interval::succ_iterator succ_begin(Interval *I)
succ_begin/succ_end - define methods so that Intervals may be used just like BasicBlocks can with the...
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
bool hasNItemsOrMore(IterTy &&Begin, IterTy &&End, unsigned N, Pred &&ShouldBeCounted=[](const decltype(*std::declval< IterTy >()) &) { return true;}, std::enable_if_t< !std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< std::remove_reference_t< decltype(Begin)> >::iterator_category >::value, void > *=nullptr)
Return true if the sequence [Begin, End) has N or more items.
Interval::pred_iterator pred_end(Interval *I)
BasicBlock::iterator skipDebugIntrinsics(BasicBlock::iterator It)
Advance It while it points to a debug instruction and return the result.
bool hasNItems(IterTy &&Begin, IterTy &&End, unsigned N, Pred &&ShouldBeCounted=[](const decltype(*std::declval< IterTy >()) &) { return true;}, std::enable_if_t< !std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< std::remove_reference_t< decltype(Begin)> >::iterator_category >::value, void > *=nullptr)
Return true if the sequence [Begin, End) has exactly N items.
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
iterator_range< filter_iterator< detail::IterOfRange< RangeT >, PredicateT > > make_filter_range(RangeT &&Range, PredicateT Pred)
Convenience function that takes a range of elements and a predicate, and return a new filter_iterator...
void invalidateParentIListOrdering(ParentClass *Parent)
Notify basic blocks when an instruction is inserted.
auto predecessors(const MachineBasicBlock *BB)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.