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/IR/CFG.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/Instructions.h"
19 #include "llvm/IR/IntrinsicInst.h"
20 #include "llvm/IR/LLVMContext.h"
21 #include "llvm/IR/Type.h"
26 ValueSymbolTable
*BasicBlock::getValueSymbolTable() {
27 if (Function
*F
= getParent())
28 return F
->getValueSymbolTable();
32 LLVMContext
&BasicBlock::getContext() const {
33 return getType()->getContext();
36 // Explicit instantiation of SymbolTableListTraits since some of the methods
37 // are not in the public header file...
38 template class llvm::SymbolTableListTraits
<Instruction
>;
40 BasicBlock::BasicBlock(LLVMContext
&C
, const Twine
&Name
, Function
*NewParent
,
41 BasicBlock
*InsertBefore
)
42 : Value(Type::getLabelTy(C
), Value::BasicBlockVal
), Parent(nullptr) {
45 insertInto(NewParent
, InsertBefore
);
47 assert(!InsertBefore
&&
48 "Cannot insert block before another block with no function!");
53 void BasicBlock::insertInto(Function
*NewParent
, BasicBlock
*InsertBefore
) {
54 assert(NewParent
&& "Expected a parent");
55 assert(!Parent
&& "Already has a parent");
58 NewParent
->getBasicBlockList().insert(InsertBefore
->getIterator(), this);
60 NewParent
->getBasicBlockList().push_back(this);
63 BasicBlock::~BasicBlock() {
64 // If the address of the block is taken and it is being deleted (e.g. because
65 // it is dead), this means that there is either a dangling constant expr
66 // hanging off the block, or an undefined use of the block (source code
67 // expecting the address of a label to keep the block alive even though there
68 // is no indirect branch). Handle these cases by zapping the BlockAddress
69 // nodes. There are no other possible uses at this point.
70 if (hasAddressTaken()) {
71 assert(!use_empty() && "There should be at least one blockaddress!");
72 Constant
*Replacement
=
73 ConstantInt::get(llvm::Type::getInt32Ty(getContext()), 1);
74 while (!use_empty()) {
75 BlockAddress
*BA
= cast
<BlockAddress
>(user_back());
76 BA
->replaceAllUsesWith(ConstantExpr::getIntToPtr(Replacement
,
78 BA
->destroyConstant();
82 assert(getParent() == nullptr && "BasicBlock still linked into the program!");
87 void BasicBlock::setParent(Function
*parent
) {
88 // Set Parent=parent, updating instruction symtab entries as appropriate.
89 InstList
.setSymTabObject(&Parent
, parent
);
92 iterator_range
<filter_iterator
<BasicBlock::const_iterator
,
93 std::function
<bool(const Instruction
&)>>>
94 BasicBlock::instructionsWithoutDebug() const {
95 std::function
<bool(const Instruction
&)> Fn
= [](const Instruction
&I
) {
96 return !isa
<DbgInfoIntrinsic
>(I
);
98 return make_filter_range(*this, Fn
);
101 iterator_range
<filter_iterator
<BasicBlock::iterator
,
102 std::function
<bool(Instruction
&)>>>
103 BasicBlock::instructionsWithoutDebug() {
104 std::function
<bool(Instruction
&)> Fn
= [](Instruction
&I
) {
105 return !isa
<DbgInfoIntrinsic
>(I
);
107 return make_filter_range(*this, Fn
);
110 filter_iterator
<BasicBlock::const_iterator
,
111 std::function
<bool(const Instruction
&)>>::difference_type
112 BasicBlock::sizeWithoutDebug() const {
113 return std::distance(instructionsWithoutDebug().begin(),
114 instructionsWithoutDebug().end());
117 void BasicBlock::removeFromParent() {
118 getParent()->getBasicBlockList().remove(getIterator());
121 iplist
<BasicBlock
>::iterator
BasicBlock::eraseFromParent() {
122 return getParent()->getBasicBlockList().erase(getIterator());
125 /// Unlink this basic block from its current function and
126 /// insert it into the function that MovePos lives in, right before MovePos.
127 void BasicBlock::moveBefore(BasicBlock
*MovePos
) {
128 MovePos
->getParent()->getBasicBlockList().splice(
129 MovePos
->getIterator(), getParent()->getBasicBlockList(), getIterator());
132 /// Unlink this basic block from its current function and
133 /// insert it into the function that MovePos lives in, right after MovePos.
134 void BasicBlock::moveAfter(BasicBlock
*MovePos
) {
135 MovePos
->getParent()->getBasicBlockList().splice(
136 ++MovePos
->getIterator(), getParent()->getBasicBlockList(),
140 const Module
*BasicBlock::getModule() const {
141 return getParent()->getParent();
144 const Instruction
*BasicBlock::getTerminator() const {
145 if (InstList
.empty() || !InstList
.back().isTerminator())
147 return &InstList
.back();
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 Instruction
* BasicBlock::getFirstNonPHI() const {
197 for (const Instruction
&I
: *this)
198 if (!isa
<PHINode
>(I
))
203 const Instruction
* BasicBlock::getFirstNonPHIOrDbg() const {
204 for (const Instruction
&I
: *this)
205 if (!isa
<PHINode
>(I
) && !isa
<DbgInfoIntrinsic
>(I
))
210 const Instruction
* BasicBlock::getFirstNonPHIOrDbgOrLifetime() const {
211 for (const Instruction
&I
: *this) {
212 if (isa
<PHINode
>(I
) || isa
<DbgInfoIntrinsic
>(I
))
215 if (I
.isLifetimeStartOrEnd())
223 BasicBlock::const_iterator
BasicBlock::getFirstInsertionPt() const {
224 const Instruction
*FirstNonPHI
= getFirstNonPHI();
228 const_iterator InsertPt
= FirstNonPHI
->getIterator();
229 if (InsertPt
->isEHPad()) ++InsertPt
;
233 void BasicBlock::dropAllReferences() {
234 for (Instruction
&I
: *this)
235 I
.dropAllReferences();
238 /// If this basic block has a single predecessor block,
239 /// return the block, otherwise return a null pointer.
240 const BasicBlock
*BasicBlock::getSinglePredecessor() const {
241 const_pred_iterator PI
= pred_begin(this), E
= pred_end(this);
242 if (PI
== E
) return nullptr; // No preds.
243 const BasicBlock
*ThePred
= *PI
;
245 return (PI
== E
) ? ThePred
: nullptr /*multiple preds*/;
248 /// If this basic block has a unique predecessor block,
249 /// return the block, otherwise return a null pointer.
250 /// Note that unique predecessor doesn't mean single edge, there can be
251 /// multiple edges from the unique predecessor to this block (for example
252 /// a switch statement with multiple cases having the same destination).
253 const BasicBlock
*BasicBlock::getUniquePredecessor() const {
254 const_pred_iterator PI
= pred_begin(this), E
= pred_end(this);
255 if (PI
== E
) return nullptr; // No preds.
256 const BasicBlock
*PredBB
= *PI
;
258 for (;PI
!= E
; ++PI
) {
261 // The same predecessor appears multiple times in the predecessor list.
267 bool BasicBlock::hasNPredecessors(unsigned N
) const {
268 return hasNItems(pred_begin(this), pred_end(this), N
);
271 bool BasicBlock::hasNPredecessorsOrMore(unsigned N
) const {
272 return hasNItemsOrMore(pred_begin(this), pred_end(this), N
);
275 const BasicBlock
*BasicBlock::getSingleSuccessor() const {
276 succ_const_iterator SI
= succ_begin(this), E
= succ_end(this);
277 if (SI
== E
) return nullptr; // no successors
278 const BasicBlock
*TheSucc
= *SI
;
280 return (SI
== E
) ? TheSucc
: nullptr /* multiple successors */;
283 const BasicBlock
*BasicBlock::getUniqueSuccessor() const {
284 succ_const_iterator SI
= succ_begin(this), E
= succ_end(this);
285 if (SI
== E
) return nullptr; // No successors
286 const BasicBlock
*SuccBB
= *SI
;
288 for (;SI
!= E
; ++SI
) {
291 // The same successor appears multiple times in the successor list.
297 iterator_range
<BasicBlock::phi_iterator
> BasicBlock::phis() {
298 PHINode
*P
= empty() ? nullptr : dyn_cast
<PHINode
>(&*begin());
299 return make_range
<phi_iterator
>(P
, nullptr);
302 /// This method is used to notify a BasicBlock that the
303 /// specified Predecessor of the block is no longer able to reach it. This is
304 /// actually not used to update the Predecessor list, but is actually used to
305 /// update the PHI nodes that reside in the block. Note that this should be
306 /// called while the predecessor still refers to this block.
308 void BasicBlock::removePredecessor(BasicBlock
*Pred
,
309 bool KeepOneInputPHIs
) {
310 assert((hasNUsesOrMore(16)||// Reduce cost of this assertion for complex CFGs.
311 find(pred_begin(this), pred_end(this), Pred
) != pred_end(this)) &&
312 "removePredecessor: BB is not a predecessor!");
314 if (InstList
.empty()) return;
315 PHINode
*APN
= dyn_cast
<PHINode
>(&front());
316 if (!APN
) return; // Quick exit.
318 // If there are exactly two predecessors, then we want to nuke the PHI nodes
319 // altogether. However, we cannot do this, if this in this case:
322 // %x = phi [X, Loop]
323 // %x2 = add %x, 1 ;; This would become %x2 = add %x2, 1
324 // br Loop ;; %x2 does not dominate all uses
326 // This is because the PHI node input is actually taken from the predecessor
327 // basic block. The only case this can happen is with a self loop, so we
328 // check for this case explicitly now.
330 unsigned max_idx
= APN
->getNumIncomingValues();
331 assert(max_idx
!= 0 && "PHI Node in block with 0 predecessors!?!?!");
333 BasicBlock
*Other
= APN
->getIncomingBlock(APN
->getIncomingBlock(0) == Pred
);
335 // Disable PHI elimination!
336 if (this == Other
) max_idx
= 3;
339 // <= Two predecessors BEFORE I remove one?
340 if (max_idx
<= 2 && !KeepOneInputPHIs
) {
341 // Yup, loop through and nuke the PHI nodes
342 while (PHINode
*PN
= dyn_cast
<PHINode
>(&front())) {
343 // Remove the predecessor first.
344 PN
->removeIncomingValue(Pred
, !KeepOneInputPHIs
);
346 // If the PHI _HAD_ two uses, replace PHI node with its now *single* value
348 if (PN
->getIncomingValue(0) != PN
)
349 PN
->replaceAllUsesWith(PN
->getIncomingValue(0));
351 // We are left with an infinite loop with no entries: kill the PHI.
352 PN
->replaceAllUsesWith(UndefValue::get(PN
->getType()));
353 getInstList().pop_front(); // Remove the PHI node
356 // If the PHI node already only had one entry, it got deleted by
357 // removeIncomingValue.
360 // Okay, now we know that we need to remove predecessor #pred_idx from all
361 // PHI nodes. Iterate over each PHI node fixing them up
363 for (iterator II
= begin(); (PN
= dyn_cast
<PHINode
>(II
)); ) {
365 PN
->removeIncomingValue(Pred
, false);
366 // If all incoming values to the Phi are the same, we can replace the Phi
368 Value
* PNV
= nullptr;
369 if (!KeepOneInputPHIs
&& (PNV
= PN
->hasConstantValue()))
371 PN
->replaceAllUsesWith(PNV
);
372 PN
->eraseFromParent();
378 bool BasicBlock::canSplitPredecessors() const {
379 const Instruction
*FirstNonPHI
= getFirstNonPHI();
380 if (isa
<LandingPadInst
>(FirstNonPHI
))
382 // This is perhaps a little conservative because constructs like
383 // CleanupBlockInst are pretty easy to split. However, SplitBlockPredecessors
384 // cannot handle such things just yet.
385 if (FirstNonPHI
->isEHPad())
390 bool BasicBlock::isLegalToHoistInto() const {
391 auto *Term
= getTerminator();
392 // No terminator means the block is under construction.
396 // If the block has no successors, there can be no instructions to hoist.
397 assert(Term
->getNumSuccessors() > 0);
399 // Instructions should not be hoisted across exception handling boundaries.
400 return !Term
->isExceptionalTerminator();
403 /// This splits a basic block into two at the specified
404 /// instruction. Note that all instructions BEFORE the specified iterator stay
405 /// as part of the original basic block, an unconditional branch is added to
406 /// the new BB, and the rest of the instructions in the BB are moved to the new
407 /// BB, including the old terminator. This invalidates the iterator.
409 /// Note that this only works on well formed basic blocks (must have a
410 /// terminator), and 'I' must not be the end of instruction list (which would
411 /// cause a degenerate basic block to be formed, having a terminator inside of
412 /// the basic block).
414 BasicBlock
*BasicBlock::splitBasicBlock(iterator I
, const Twine
&BBName
) {
415 assert(getTerminator() && "Can't use splitBasicBlock on degenerate BB!");
416 assert(I
!= InstList
.end() &&
417 "Trying to get me to create degenerate basic block!");
419 BasicBlock
*New
= BasicBlock::Create(getContext(), BBName
, getParent(),
420 this->getNextNode());
422 // Save DebugLoc of split point before invalidating iterator.
423 DebugLoc Loc
= I
->getDebugLoc();
424 // Move all of the specified instructions from the original basic block into
425 // the new basic block.
426 New
->getInstList().splice(New
->end(), this->getInstList(), I
, end());
428 // Add a branch instruction to the newly formed basic block.
429 BranchInst
*BI
= BranchInst::Create(New
, this);
430 BI
->setDebugLoc(Loc
);
432 // Now we must loop through all of the successors of the New block (which
433 // _were_ the successors of the 'this' block), and update any PHI nodes in
434 // successors. If there were PHI nodes in the successors, then they need to
435 // know that incoming branches will be from New, not from Old (this).
437 New
->replaceSuccessorsPhiUsesWith(this, New
);
441 void BasicBlock::replacePhiUsesWith(BasicBlock
*Old
, BasicBlock
*New
) {
442 // N.B. This might not be a complete BasicBlock, so don't assume
443 // that it ends with a non-phi instruction.
444 for (iterator II
= begin(), IE
= end(); II
!= IE
; ++II
) {
445 PHINode
*PN
= dyn_cast
<PHINode
>(II
);
448 PN
->replaceIncomingBlockWith(Old
, New
);
452 void BasicBlock::replaceSuccessorsPhiUsesWith(BasicBlock
*Old
,
454 Instruction
*TI
= getTerminator();
456 // Cope with being called on a BasicBlock that doesn't have a terminator
457 // yet. Clang's CodeGenFunction::EmitReturnBlock() likes to do this.
459 llvm::for_each(successors(TI
), [Old
, New
](BasicBlock
*Succ
) {
460 Succ
->replacePhiUsesWith(Old
, New
);
464 void BasicBlock::replaceSuccessorsPhiUsesWith(BasicBlock
*New
) {
465 this->replaceSuccessorsPhiUsesWith(this, New
);
468 /// Return true if this basic block is a landing pad. I.e., it's
469 /// the destination of the 'unwind' edge of an invoke instruction.
470 bool BasicBlock::isLandingPad() const {
471 return isa
<LandingPadInst
>(getFirstNonPHI());
474 /// Return the landingpad instruction associated with the landing pad.
475 const LandingPadInst
*BasicBlock::getLandingPadInst() const {
476 return dyn_cast
<LandingPadInst
>(getFirstNonPHI());
479 Optional
<uint64_t> BasicBlock::getIrrLoopHeaderWeight() const {
480 const Instruction
*TI
= getTerminator();
481 if (MDNode
*MDIrrLoopHeader
=
482 TI
->getMetadata(LLVMContext::MD_irr_loop
)) {
483 MDString
*MDName
= cast
<MDString
>(MDIrrLoopHeader
->getOperand(0));
484 if (MDName
->getString().equals("loop_header_weight")) {
485 auto *CI
= mdconst::extract
<ConstantInt
>(MDIrrLoopHeader
->getOperand(1));
486 return Optional
<uint64_t>(CI
->getValue().getZExtValue());
489 return Optional
<uint64_t>();
492 BasicBlock::iterator
llvm::skipDebugIntrinsics(BasicBlock::iterator It
) {
493 while (isa
<DbgInfoIntrinsic
>(It
))