1 //===-- BasicBlock.cpp - Implement BasicBlock related methods -------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the BasicBlock class for the IR library.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/IR/BasicBlock.h"
14 #include "SymbolTableListTraitsImpl.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/IR/CFG.h"
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/Instructions.h"
20 #include "llvm/IR/IntrinsicInst.h"
21 #include "llvm/IR/LLVMContext.h"
22 #include "llvm/IR/Type.h"
26 #define DEBUG_TYPE "ir"
27 STATISTIC(NumInstrRenumberings
, "Number of renumberings across all blocks");
29 ValueSymbolTable
*BasicBlock::getValueSymbolTable() {
30 if (Function
*F
= getParent())
31 return F
->getValueSymbolTable();
35 LLVMContext
&BasicBlock::getContext() const {
36 return getType()->getContext();
39 template <> void llvm::invalidateParentIListOrdering(BasicBlock
*BB
) {
40 BB
->invalidateOrders();
43 // Explicit instantiation of SymbolTableListTraits since some of the methods
44 // are not in the public header file...
45 template class llvm::SymbolTableListTraits
<Instruction
,
46 ilist_iterator_bits
<true>>;
48 BasicBlock::BasicBlock(LLVMContext
&C
, const Twine
&Name
, Function
*NewParent
,
49 BasicBlock
*InsertBefore
)
50 : Value(Type::getLabelTy(C
), Value::BasicBlockVal
), Parent(nullptr) {
53 insertInto(NewParent
, InsertBefore
);
55 assert(!InsertBefore
&&
56 "Cannot insert block before another block with no function!");
61 void BasicBlock::insertInto(Function
*NewParent
, BasicBlock
*InsertBefore
) {
62 assert(NewParent
&& "Expected a parent");
63 assert(!Parent
&& "Already has a parent");
66 NewParent
->insert(InsertBefore
->getIterator(), this);
68 NewParent
->insert(NewParent
->end(), this);
71 BasicBlock::~BasicBlock() {
72 validateInstrOrdering();
74 // If the address of the block is taken and it is being deleted (e.g. because
75 // it is dead), this means that there is either a dangling constant expr
76 // hanging off the block, or an undefined use of the block (source code
77 // expecting the address of a label to keep the block alive even though there
78 // is no indirect branch). Handle these cases by zapping the BlockAddress
79 // nodes. There are no other possible uses at this point.
80 if (hasAddressTaken()) {
81 assert(!use_empty() && "There should be at least one blockaddress!");
82 Constant
*Replacement
=
83 ConstantInt::get(llvm::Type::getInt32Ty(getContext()), 1);
84 while (!use_empty()) {
85 BlockAddress
*BA
= cast
<BlockAddress
>(user_back());
86 BA
->replaceAllUsesWith(ConstantExpr::getIntToPtr(Replacement
,
88 BA
->destroyConstant();
92 assert(getParent() == nullptr && "BasicBlock still linked into the program!");
97 void BasicBlock::setParent(Function
*parent
) {
98 // Set Parent=parent, updating instruction symtab entries as appropriate.
99 InstList
.setSymTabObject(&Parent
, parent
);
102 iterator_range
<filter_iterator
<BasicBlock::const_iterator
,
103 std::function
<bool(const Instruction
&)>>>
104 BasicBlock::instructionsWithoutDebug(bool SkipPseudoOp
) const {
105 std::function
<bool(const Instruction
&)> Fn
= [=](const Instruction
&I
) {
106 return !isa
<DbgInfoIntrinsic
>(I
) &&
107 !(SkipPseudoOp
&& isa
<PseudoProbeInst
>(I
));
109 return make_filter_range(*this, Fn
);
113 filter_iterator
<BasicBlock::iterator
, std::function
<bool(Instruction
&)>>>
114 BasicBlock::instructionsWithoutDebug(bool SkipPseudoOp
) {
115 std::function
<bool(Instruction
&)> Fn
= [=](Instruction
&I
) {
116 return !isa
<DbgInfoIntrinsic
>(I
) &&
117 !(SkipPseudoOp
&& isa
<PseudoProbeInst
>(I
));
119 return make_filter_range(*this, Fn
);
122 filter_iterator
<BasicBlock::const_iterator
,
123 std::function
<bool(const Instruction
&)>>::difference_type
124 BasicBlock::sizeWithoutDebug() const {
125 return std::distance(instructionsWithoutDebug().begin(),
126 instructionsWithoutDebug().end());
129 void BasicBlock::removeFromParent() {
130 getParent()->getBasicBlockList().remove(getIterator());
133 iplist
<BasicBlock
>::iterator
BasicBlock::eraseFromParent() {
134 return getParent()->getBasicBlockList().erase(getIterator());
137 void BasicBlock::moveBefore(SymbolTableList
<BasicBlock
>::iterator MovePos
) {
138 getParent()->splice(MovePos
, getParent(), getIterator());
141 void BasicBlock::moveAfter(BasicBlock
*MovePos
) {
142 MovePos
->getParent()->splice(++MovePos
->getIterator(), getParent(),
146 const Module
*BasicBlock::getModule() const {
147 return getParent()->getParent();
150 const CallInst
*BasicBlock::getTerminatingMustTailCall() const {
151 if (InstList
.empty())
153 const ReturnInst
*RI
= dyn_cast
<ReturnInst
>(&InstList
.back());
154 if (!RI
|| RI
== &InstList
.front())
157 const Instruction
*Prev
= RI
->getPrevNode();
161 if (Value
*RV
= RI
->getReturnValue()) {
165 // Look through the optional bitcast.
166 if (auto *BI
= dyn_cast
<BitCastInst
>(Prev
)) {
167 RV
= BI
->getOperand(0);
168 Prev
= BI
->getPrevNode();
169 if (!Prev
|| RV
!= Prev
)
174 if (auto *CI
= dyn_cast
<CallInst
>(Prev
)) {
175 if (CI
->isMustTailCall())
181 const CallInst
*BasicBlock::getTerminatingDeoptimizeCall() const {
182 if (InstList
.empty())
184 auto *RI
= dyn_cast
<ReturnInst
>(&InstList
.back());
185 if (!RI
|| RI
== &InstList
.front())
188 if (auto *CI
= dyn_cast_or_null
<CallInst
>(RI
->getPrevNode()))
189 if (Function
*F
= CI
->getCalledFunction())
190 if (F
->getIntrinsicID() == Intrinsic::experimental_deoptimize
)
196 const CallInst
*BasicBlock::getPostdominatingDeoptimizeCall() const {
197 const BasicBlock
* BB
= this;
198 SmallPtrSet
<const BasicBlock
*, 8> Visited
;
200 while (auto *Succ
= BB
->getUniqueSuccessor()) {
201 if (!Visited
.insert(Succ
).second
)
205 return BB
->getTerminatingDeoptimizeCall();
208 const Instruction
*BasicBlock::getFirstMayFaultInst() const {
209 if (InstList
.empty())
211 for (const Instruction
&I
: *this)
212 if (isa
<LoadInst
>(I
) || isa
<StoreInst
>(I
) || isa
<CallBase
>(I
))
217 const Instruction
* BasicBlock::getFirstNonPHI() const {
218 for (const Instruction
&I
: *this)
219 if (!isa
<PHINode
>(I
))
224 BasicBlock::const_iterator
BasicBlock::getFirstNonPHIIt() const {
225 const Instruction
*I
= getFirstNonPHI();
226 BasicBlock::const_iterator It
= I
->getIterator();
227 // Set the head-inclusive bit to indicate that this iterator includes
228 // any debug-info at the start of the block. This is a no-op unless the
229 // appropriate CMake flag is set.
234 const Instruction
*BasicBlock::getFirstNonPHIOrDbg(bool SkipPseudoOp
) const {
235 for (const Instruction
&I
: *this) {
236 if (isa
<PHINode
>(I
) || isa
<DbgInfoIntrinsic
>(I
))
239 if (SkipPseudoOp
&& isa
<PseudoProbeInst
>(I
))
248 BasicBlock::getFirstNonPHIOrDbgOrLifetime(bool SkipPseudoOp
) const {
249 for (const Instruction
&I
: *this) {
250 if (isa
<PHINode
>(I
) || isa
<DbgInfoIntrinsic
>(I
))
253 if (I
.isLifetimeStartOrEnd())
256 if (SkipPseudoOp
&& isa
<PseudoProbeInst
>(I
))
264 BasicBlock::const_iterator
BasicBlock::getFirstInsertionPt() const {
265 const Instruction
*FirstNonPHI
= getFirstNonPHI();
269 const_iterator InsertPt
= FirstNonPHI
->getIterator();
270 if (InsertPt
->isEHPad()) ++InsertPt
;
271 // Set the head-inclusive bit to indicate that this iterator includes
272 // any debug-info at the start of the block. This is a no-op unless the
273 // appropriate CMake flag is set.
274 InsertPt
.setHeadBit(true);
278 BasicBlock::const_iterator
BasicBlock::getFirstNonPHIOrDbgOrAlloca() const {
279 const Instruction
*FirstNonPHI
= getFirstNonPHI();
283 const_iterator InsertPt
= FirstNonPHI
->getIterator();
284 if (InsertPt
->isEHPad())
287 if (isEntryBlock()) {
288 const_iterator End
= end();
289 while (InsertPt
!= End
&&
290 (isa
<AllocaInst
>(*InsertPt
) || isa
<DbgInfoIntrinsic
>(*InsertPt
) ||
291 isa
<PseudoProbeInst
>(*InsertPt
))) {
292 if (const AllocaInst
*AI
= dyn_cast
<AllocaInst
>(&*InsertPt
)) {
293 if (!AI
->isStaticAlloca())
302 void BasicBlock::dropAllReferences() {
303 for (Instruction
&I
: *this)
304 I
.dropAllReferences();
307 const BasicBlock
*BasicBlock::getSinglePredecessor() const {
308 const_pred_iterator PI
= pred_begin(this), E
= pred_end(this);
309 if (PI
== E
) return nullptr; // No preds.
310 const BasicBlock
*ThePred
= *PI
;
312 return (PI
== E
) ? ThePred
: nullptr /*multiple preds*/;
315 const BasicBlock
*BasicBlock::getUniquePredecessor() const {
316 const_pred_iterator PI
= pred_begin(this), E
= pred_end(this);
317 if (PI
== E
) return nullptr; // No preds.
318 const BasicBlock
*PredBB
= *PI
;
320 for (;PI
!= E
; ++PI
) {
323 // The same predecessor appears multiple times in the predecessor list.
329 bool BasicBlock::hasNPredecessors(unsigned N
) const {
330 return hasNItems(pred_begin(this), pred_end(this), N
);
333 bool BasicBlock::hasNPredecessorsOrMore(unsigned N
) const {
334 return hasNItemsOrMore(pred_begin(this), pred_end(this), N
);
337 const BasicBlock
*BasicBlock::getSingleSuccessor() const {
338 const_succ_iterator SI
= succ_begin(this), E
= succ_end(this);
339 if (SI
== E
) return nullptr; // no successors
340 const BasicBlock
*TheSucc
= *SI
;
342 return (SI
== E
) ? TheSucc
: nullptr /* multiple successors */;
345 const BasicBlock
*BasicBlock::getUniqueSuccessor() const {
346 const_succ_iterator SI
= succ_begin(this), E
= succ_end(this);
347 if (SI
== E
) return nullptr; // No successors
348 const BasicBlock
*SuccBB
= *SI
;
350 for (;SI
!= E
; ++SI
) {
353 // The same successor appears multiple times in the successor list.
359 iterator_range
<BasicBlock::phi_iterator
> BasicBlock::phis() {
360 PHINode
*P
= empty() ? nullptr : dyn_cast
<PHINode
>(&*begin());
361 return make_range
<phi_iterator
>(P
, nullptr);
364 void BasicBlock::removePredecessor(BasicBlock
*Pred
,
365 bool KeepOneInputPHIs
) {
366 // Use hasNUsesOrMore to bound the cost of this assertion for complex CFGs.
367 assert((hasNUsesOrMore(16) || llvm::is_contained(predecessors(this), Pred
)) &&
368 "Pred is not a predecessor!");
370 // Return early if there are no PHI nodes to update.
371 if (empty() || !isa
<PHINode
>(begin()))
374 unsigned NumPreds
= cast
<PHINode
>(front()).getNumIncomingValues();
375 for (PHINode
&Phi
: make_early_inc_range(phis())) {
376 Phi
.removeIncomingValue(Pred
, !KeepOneInputPHIs
);
377 if (KeepOneInputPHIs
)
380 // If we have a single predecessor, removeIncomingValue may have erased the
385 // Try to replace the PHI node with a constant value.
386 if (Value
*PhiConstant
= Phi
.hasConstantValue()) {
387 Phi
.replaceAllUsesWith(PhiConstant
);
388 Phi
.eraseFromParent();
393 bool BasicBlock::canSplitPredecessors() const {
394 const Instruction
*FirstNonPHI
= getFirstNonPHI();
395 if (isa
<LandingPadInst
>(FirstNonPHI
))
397 // This is perhaps a little conservative because constructs like
398 // CleanupBlockInst are pretty easy to split. However, SplitBlockPredecessors
399 // cannot handle such things just yet.
400 if (FirstNonPHI
->isEHPad())
405 bool BasicBlock::isLegalToHoistInto() const {
406 auto *Term
= getTerminator();
407 // No terminator means the block is under construction.
411 // If the block has no successors, there can be no instructions to hoist.
412 assert(Term
->getNumSuccessors() > 0);
414 // Instructions should not be hoisted across special terminators, which may
415 // have side effects or return values.
416 return !Term
->isSpecialTerminator();
419 bool BasicBlock::isEntryBlock() const {
420 const Function
*F
= getParent();
421 assert(F
&& "Block must have a parent function to use this API");
422 return this == &F
->getEntryBlock();
425 BasicBlock
*BasicBlock::splitBasicBlock(iterator I
, const Twine
&BBName
,
428 return splitBasicBlockBefore(I
, BBName
);
430 assert(getTerminator() && "Can't use splitBasicBlock on degenerate BB!");
431 assert(I
!= InstList
.end() &&
432 "Trying to get me to create degenerate basic block!");
434 BasicBlock
*New
= BasicBlock::Create(getContext(), BBName
, getParent(),
435 this->getNextNode());
437 // Save DebugLoc of split point before invalidating iterator.
438 DebugLoc Loc
= I
->getDebugLoc();
439 // Move all of the specified instructions from the original basic block into
440 // the new basic block.
441 New
->splice(New
->end(), this, I
, end());
443 // Add a branch instruction to the newly formed basic block.
444 BranchInst
*BI
= BranchInst::Create(New
, this);
445 BI
->setDebugLoc(Loc
);
447 // Now we must loop through all of the successors of the New block (which
448 // _were_ the successors of the 'this' block), and update any PHI nodes in
449 // successors. If there were PHI nodes in the successors, then they need to
450 // know that incoming branches will be from New, not from Old (this).
452 New
->replaceSuccessorsPhiUsesWith(this, New
);
456 BasicBlock
*BasicBlock::splitBasicBlockBefore(iterator I
, const Twine
&BBName
) {
457 assert(getTerminator() &&
458 "Can't use splitBasicBlockBefore on degenerate BB!");
459 assert(I
!= InstList
.end() &&
460 "Trying to get me to create degenerate basic block!");
462 assert((!isa
<PHINode
>(*I
) || getSinglePredecessor()) &&
463 "cannot split on multi incoming phis");
465 BasicBlock
*New
= BasicBlock::Create(getContext(), BBName
, getParent(), this);
466 // Save DebugLoc of split point before invalidating iterator.
467 DebugLoc Loc
= I
->getDebugLoc();
468 // Move all of the specified instructions from the original basic block into
469 // the new basic block.
470 New
->splice(New
->end(), this, begin(), I
);
472 // Loop through all of the predecessors of the 'this' block (which will be the
473 // predecessors of the New block), replace the specified successor 'this'
474 // block to point at the New block and update any PHI nodes in 'this' block.
475 // If there were PHI nodes in 'this' block, the PHI nodes are updated
476 // to reflect that the incoming branches will be from the New block and not
477 // from predecessors of the 'this' block.
478 // Save predecessors to separate vector before modifying them.
479 SmallVector
<BasicBlock
*, 4> Predecessors
;
480 for (BasicBlock
*Pred
: predecessors(this))
481 Predecessors
.push_back(Pred
);
482 for (BasicBlock
*Pred
: Predecessors
) {
483 Instruction
*TI
= Pred
->getTerminator();
484 TI
->replaceSuccessorWith(this, New
);
485 this->replacePhiUsesWith(Pred
, New
);
487 // Add a branch instruction from "New" to "this" Block.
488 BranchInst
*BI
= BranchInst::Create(this, New
);
489 BI
->setDebugLoc(Loc
);
494 void BasicBlock::splice(BasicBlock::iterator ToIt
, BasicBlock
*FromBB
,
495 BasicBlock::iterator FromBeginIt
,
496 BasicBlock::iterator FromEndIt
) {
497 #ifdef EXPENSIVE_CHECKS
498 // Check that FromBeginIt is befor FromEndIt.
499 auto FromBBEnd
= FromBB
->end();
500 for (auto It
= FromBeginIt
; It
!= FromEndIt
; ++It
)
501 assert(It
!= FromBBEnd
&& "FromBeginIt not before FromEndIt!");
502 #endif // EXPENSIVE_CHECKS
503 getInstList().splice(ToIt
, FromBB
->getInstList(), FromBeginIt
, FromEndIt
);
506 BasicBlock::iterator
BasicBlock::erase(BasicBlock::iterator FromIt
,
507 BasicBlock::iterator ToIt
) {
508 return InstList
.erase(FromIt
, ToIt
);
511 void BasicBlock::replacePhiUsesWith(BasicBlock
*Old
, BasicBlock
*New
) {
512 // N.B. This might not be a complete BasicBlock, so don't assume
513 // that it ends with a non-phi instruction.
514 for (Instruction
&I
: *this) {
515 PHINode
*PN
= dyn_cast
<PHINode
>(&I
);
518 PN
->replaceIncomingBlockWith(Old
, New
);
522 void BasicBlock::replaceSuccessorsPhiUsesWith(BasicBlock
*Old
,
524 Instruction
*TI
= getTerminator();
526 // Cope with being called on a BasicBlock that doesn't have a terminator
527 // yet. Clang's CodeGenFunction::EmitReturnBlock() likes to do this.
529 for (BasicBlock
*Succ
: successors(TI
))
530 Succ
->replacePhiUsesWith(Old
, New
);
533 void BasicBlock::replaceSuccessorsPhiUsesWith(BasicBlock
*New
) {
534 this->replaceSuccessorsPhiUsesWith(this, New
);
537 bool BasicBlock::isLandingPad() const {
538 return isa
<LandingPadInst
>(getFirstNonPHI());
541 const LandingPadInst
*BasicBlock::getLandingPadInst() const {
542 return dyn_cast
<LandingPadInst
>(getFirstNonPHI());
545 std::optional
<uint64_t> BasicBlock::getIrrLoopHeaderWeight() const {
546 const Instruction
*TI
= getTerminator();
547 if (MDNode
*MDIrrLoopHeader
=
548 TI
->getMetadata(LLVMContext::MD_irr_loop
)) {
549 MDString
*MDName
= cast
<MDString
>(MDIrrLoopHeader
->getOperand(0));
550 if (MDName
->getString().equals("loop_header_weight")) {
551 auto *CI
= mdconst::extract
<ConstantInt
>(MDIrrLoopHeader
->getOperand(1));
552 return std::optional
<uint64_t>(CI
->getValue().getZExtValue());
558 BasicBlock::iterator
llvm::skipDebugIntrinsics(BasicBlock::iterator It
) {
559 while (isa
<DbgInfoIntrinsic
>(It
))
564 void BasicBlock::renumberInstructions() {
566 for (Instruction
&I
: *this)
569 // Set the bit to indicate that the instruction order valid and cached.
570 BasicBlockBits Bits
= getBasicBlockBits();
571 Bits
.InstrOrderValid
= true;
572 setBasicBlockBits(Bits
);
574 NumInstrRenumberings
++;
578 /// In asserts builds, this checks the numbering. In non-asserts builds, it
579 /// is defined as a no-op inline function in BasicBlock.h.
580 void BasicBlock::validateInstrOrdering() const {
581 if (!isInstrOrderValid())
583 const Instruction
*Prev
= nullptr;
584 for (const Instruction
&I
: *this) {
585 assert((!Prev
|| Prev
->comesBefore(&I
)) &&
586 "cached instruction ordering is incorrect");