xref: /freebsd/contrib/llvm-project/llvm/lib/IR/BasicBlock.cpp (revision 911f0260390e18cf85f3dbf2c719b593efdc1e3c)
1 //===-- BasicBlock.cpp - Implement BasicBlock related methods -------------===//
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 implements the BasicBlock class for the IR library.
10 //
11 //===----------------------------------------------------------------------===//
12 
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"
23 
24 using namespace llvm;
25 
26 #define DEBUG_TYPE "ir"
27 STATISTIC(NumInstrRenumberings, "Number of renumberings across all blocks");
28 
29 ValueSymbolTable *BasicBlock::getValueSymbolTable() {
30   if (Function *F = getParent())
31     return F->getValueSymbolTable();
32   return nullptr;
33 }
34 
35 LLVMContext &BasicBlock::getContext() const {
36   return getType()->getContext();
37 }
38 
39 template <> void llvm::invalidateParentIListOrdering(BasicBlock *BB) {
40   BB->invalidateOrders();
41 }
42 
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 
47 BasicBlock::BasicBlock(LLVMContext &C, const Twine &Name, Function *NewParent,
48                        BasicBlock *InsertBefore)
49   : Value(Type::getLabelTy(C), Value::BasicBlockVal), Parent(nullptr) {
50 
51   if (NewParent)
52     insertInto(NewParent, InsertBefore);
53   else
54     assert(!InsertBefore &&
55            "Cannot insert block before another block with no function!");
56 
57   setName(Name);
58 }
59 
60 void BasicBlock::insertInto(Function *NewParent, BasicBlock *InsertBefore) {
61   assert(NewParent && "Expected a parent");
62   assert(!Parent && "Already has a parent");
63 
64   if (InsertBefore)
65     NewParent->insert(InsertBefore->getIterator(), this);
66   else
67     NewParent->insert(NewParent->end(), this);
68 }
69 
70 BasicBlock::~BasicBlock() {
71   validateInstrOrdering();
72 
73   // If the address of the block is taken and it is being deleted (e.g. because
74   // it is dead), this means that there is either a dangling constant expr
75   // hanging off the block, or an undefined use of the block (source code
76   // expecting the address of a label to keep the block alive even though there
77   // is no indirect branch).  Handle these cases by zapping the BlockAddress
78   // nodes.  There are no other possible uses at this point.
79   if (hasAddressTaken()) {
80     assert(!use_empty() && "There should be at least one blockaddress!");
81     Constant *Replacement =
82       ConstantInt::get(llvm::Type::getInt32Ty(getContext()), 1);
83     while (!use_empty()) {
84       BlockAddress *BA = cast<BlockAddress>(user_back());
85       BA->replaceAllUsesWith(ConstantExpr::getIntToPtr(Replacement,
86                                                        BA->getType()));
87       BA->destroyConstant();
88     }
89   }
90 
91   assert(getParent() == nullptr && "BasicBlock still linked into the program!");
92   dropAllReferences();
93   InstList.clear();
94 }
95 
96 void BasicBlock::setParent(Function *parent) {
97   // Set Parent=parent, updating instruction symtab entries as appropriate.
98   InstList.setSymTabObject(&Parent, parent);
99 }
100 
101 iterator_range<filter_iterator<BasicBlock::const_iterator,
102                                std::function<bool(const Instruction &)>>>
103 BasicBlock::instructionsWithoutDebug(bool SkipPseudoOp) const {
104   std::function<bool(const Instruction &)> Fn = [=](const Instruction &I) {
105     return !isa<DbgInfoIntrinsic>(I) &&
106            !(SkipPseudoOp && isa<PseudoProbeInst>(I));
107   };
108   return make_filter_range(*this, Fn);
109 }
110 
111 iterator_range<
112     filter_iterator<BasicBlock::iterator, std::function<bool(Instruction &)>>>
113 BasicBlock::instructionsWithoutDebug(bool SkipPseudoOp) {
114   std::function<bool(Instruction &)> Fn = [=](Instruction &I) {
115     return !isa<DbgInfoIntrinsic>(I) &&
116            !(SkipPseudoOp && isa<PseudoProbeInst>(I));
117   };
118   return make_filter_range(*this, Fn);
119 }
120 
121 filter_iterator<BasicBlock::const_iterator,
122                 std::function<bool(const Instruction &)>>::difference_type
123 BasicBlock::sizeWithoutDebug() const {
124   return std::distance(instructionsWithoutDebug().begin(),
125                        instructionsWithoutDebug().end());
126 }
127 
128 void BasicBlock::removeFromParent() {
129   getParent()->getBasicBlockList().remove(getIterator());
130 }
131 
132 iplist<BasicBlock>::iterator BasicBlock::eraseFromParent() {
133   return getParent()->getBasicBlockList().erase(getIterator());
134 }
135 
136 void BasicBlock::moveBefore(BasicBlock *MovePos) {
137   MovePos->getParent()->splice(MovePos->getIterator(), getParent(),
138                                getIterator());
139 }
140 
141 void BasicBlock::moveAfter(BasicBlock *MovePos) {
142   MovePos->getParent()->splice(++MovePos->getIterator(), getParent(),
143                                getIterator());
144 }
145 
146 const Module *BasicBlock::getModule() const {
147   return getParent()->getParent();
148 }
149 
150 const CallInst *BasicBlock::getTerminatingMustTailCall() const {
151   if (InstList.empty())
152     return nullptr;
153   const ReturnInst *RI = dyn_cast<ReturnInst>(&InstList.back());
154   if (!RI || RI == &InstList.front())
155     return nullptr;
156 
157   const Instruction *Prev = RI->getPrevNode();
158   if (!Prev)
159     return nullptr;
160 
161   if (Value *RV = RI->getReturnValue()) {
162     if (RV != Prev)
163       return nullptr;
164 
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)
170         return nullptr;
171     }
172   }
173 
174   if (auto *CI = dyn_cast<CallInst>(Prev)) {
175     if (CI->isMustTailCall())
176       return CI;
177   }
178   return nullptr;
179 }
180 
181 const CallInst *BasicBlock::getTerminatingDeoptimizeCall() const {
182   if (InstList.empty())
183     return nullptr;
184   auto *RI = dyn_cast<ReturnInst>(&InstList.back());
185   if (!RI || RI == &InstList.front())
186     return nullptr;
187 
188   if (auto *CI = dyn_cast_or_null<CallInst>(RI->getPrevNode()))
189     if (Function *F = CI->getCalledFunction())
190       if (F->getIntrinsicID() == Intrinsic::experimental_deoptimize)
191         return CI;
192 
193   return nullptr;
194 }
195 
196 const CallInst *BasicBlock::getPostdominatingDeoptimizeCall() const {
197   const BasicBlock* BB = this;
198   SmallPtrSet<const BasicBlock *, 8> Visited;
199   Visited.insert(BB);
200   while (auto *Succ = BB->getUniqueSuccessor()) {
201     if (!Visited.insert(Succ).second)
202       return nullptr;
203     BB = Succ;
204   }
205   return BB->getTerminatingDeoptimizeCall();
206 }
207 
208 const Instruction* BasicBlock::getFirstNonPHI() const {
209   for (const Instruction &I : *this)
210     if (!isa<PHINode>(I))
211       return &I;
212   return nullptr;
213 }
214 
215 const Instruction *BasicBlock::getFirstNonPHIOrDbg(bool SkipPseudoOp) const {
216   for (const Instruction &I : *this) {
217     if (isa<PHINode>(I) || isa<DbgInfoIntrinsic>(I))
218       continue;
219 
220     if (SkipPseudoOp && isa<PseudoProbeInst>(I))
221       continue;
222 
223     return &I;
224   }
225   return nullptr;
226 }
227 
228 const Instruction *
229 BasicBlock::getFirstNonPHIOrDbgOrLifetime(bool SkipPseudoOp) const {
230   for (const Instruction &I : *this) {
231     if (isa<PHINode>(I) || isa<DbgInfoIntrinsic>(I))
232       continue;
233 
234     if (I.isLifetimeStartOrEnd())
235       continue;
236 
237     if (SkipPseudoOp && isa<PseudoProbeInst>(I))
238       continue;
239 
240     return &I;
241   }
242   return nullptr;
243 }
244 
245 BasicBlock::const_iterator BasicBlock::getFirstInsertionPt() const {
246   const Instruction *FirstNonPHI = getFirstNonPHI();
247   if (!FirstNonPHI)
248     return end();
249 
250   const_iterator InsertPt = FirstNonPHI->getIterator();
251   if (InsertPt->isEHPad()) ++InsertPt;
252   return InsertPt;
253 }
254 
255 BasicBlock::const_iterator BasicBlock::getFirstNonPHIOrDbgOrAlloca() const {
256   const Instruction *FirstNonPHI = getFirstNonPHI();
257   if (!FirstNonPHI)
258     return end();
259 
260   const_iterator InsertPt = FirstNonPHI->getIterator();
261   if (InsertPt->isEHPad())
262     ++InsertPt;
263 
264   if (isEntryBlock()) {
265     const_iterator End = end();
266     while (InsertPt != End &&
267            (isa<AllocaInst>(*InsertPt) || isa<DbgInfoIntrinsic>(*InsertPt) ||
268             isa<PseudoProbeInst>(*InsertPt))) {
269       if (const AllocaInst *AI = dyn_cast<AllocaInst>(&*InsertPt)) {
270         if (!AI->isStaticAlloca())
271           break;
272       }
273       ++InsertPt;
274     }
275   }
276   return InsertPt;
277 }
278 
279 void BasicBlock::dropAllReferences() {
280   for (Instruction &I : *this)
281     I.dropAllReferences();
282 }
283 
284 const BasicBlock *BasicBlock::getSinglePredecessor() const {
285   const_pred_iterator PI = pred_begin(this), E = pred_end(this);
286   if (PI == E) return nullptr;         // No preds.
287   const BasicBlock *ThePred = *PI;
288   ++PI;
289   return (PI == E) ? ThePred : nullptr /*multiple preds*/;
290 }
291 
292 const BasicBlock *BasicBlock::getUniquePredecessor() const {
293   const_pred_iterator PI = pred_begin(this), E = pred_end(this);
294   if (PI == E) return nullptr; // No preds.
295   const BasicBlock *PredBB = *PI;
296   ++PI;
297   for (;PI != E; ++PI) {
298     if (*PI != PredBB)
299       return nullptr;
300     // The same predecessor appears multiple times in the predecessor list.
301     // This is OK.
302   }
303   return PredBB;
304 }
305 
306 bool BasicBlock::hasNPredecessors(unsigned N) const {
307   return hasNItems(pred_begin(this), pred_end(this), N);
308 }
309 
310 bool BasicBlock::hasNPredecessorsOrMore(unsigned N) const {
311   return hasNItemsOrMore(pred_begin(this), pred_end(this), N);
312 }
313 
314 const BasicBlock *BasicBlock::getSingleSuccessor() const {
315   const_succ_iterator SI = succ_begin(this), E = succ_end(this);
316   if (SI == E) return nullptr; // no successors
317   const BasicBlock *TheSucc = *SI;
318   ++SI;
319   return (SI == E) ? TheSucc : nullptr /* multiple successors */;
320 }
321 
322 const BasicBlock *BasicBlock::getUniqueSuccessor() const {
323   const_succ_iterator SI = succ_begin(this), E = succ_end(this);
324   if (SI == E) return nullptr; // No successors
325   const BasicBlock *SuccBB = *SI;
326   ++SI;
327   for (;SI != E; ++SI) {
328     if (*SI != SuccBB)
329       return nullptr;
330     // The same successor appears multiple times in the successor list.
331     // This is OK.
332   }
333   return SuccBB;
334 }
335 
336 iterator_range<BasicBlock::phi_iterator> BasicBlock::phis() {
337   PHINode *P = empty() ? nullptr : dyn_cast<PHINode>(&*begin());
338   return make_range<phi_iterator>(P, nullptr);
339 }
340 
341 void BasicBlock::removePredecessor(BasicBlock *Pred,
342                                    bool KeepOneInputPHIs) {
343   // Use hasNUsesOrMore to bound the cost of this assertion for complex CFGs.
344   assert((hasNUsesOrMore(16) || llvm::is_contained(predecessors(this), Pred)) &&
345          "Pred is not a predecessor!");
346 
347   // Return early if there are no PHI nodes to update.
348   if (empty() || !isa<PHINode>(begin()))
349     return;
350 
351   unsigned NumPreds = cast<PHINode>(front()).getNumIncomingValues();
352   for (PHINode &Phi : make_early_inc_range(phis())) {
353     Phi.removeIncomingValue(Pred, !KeepOneInputPHIs);
354     if (KeepOneInputPHIs)
355       continue;
356 
357     // If we have a single predecessor, removeIncomingValue may have erased the
358     // PHI node itself.
359     if (NumPreds == 1)
360       continue;
361 
362     // Try to replace the PHI node with a constant value.
363     if (Value *PhiConstant = Phi.hasConstantValue()) {
364       Phi.replaceAllUsesWith(PhiConstant);
365       Phi.eraseFromParent();
366     }
367   }
368 }
369 
370 bool BasicBlock::canSplitPredecessors() const {
371   const Instruction *FirstNonPHI = getFirstNonPHI();
372   if (isa<LandingPadInst>(FirstNonPHI))
373     return true;
374   // This is perhaps a little conservative because constructs like
375   // CleanupBlockInst are pretty easy to split.  However, SplitBlockPredecessors
376   // cannot handle such things just yet.
377   if (FirstNonPHI->isEHPad())
378     return false;
379   return true;
380 }
381 
382 bool BasicBlock::isLegalToHoistInto() const {
383   auto *Term = getTerminator();
384   // No terminator means the block is under construction.
385   if (!Term)
386     return true;
387 
388   // If the block has no successors, there can be no instructions to hoist.
389   assert(Term->getNumSuccessors() > 0);
390 
391   // Instructions should not be hoisted across exception handling boundaries.
392   return !Term->isExceptionalTerminator();
393 }
394 
395 bool BasicBlock::isEntryBlock() const {
396   const Function *F = getParent();
397   assert(F && "Block must have a parent function to use this API");
398   return this == &F->getEntryBlock();
399 }
400 
401 BasicBlock *BasicBlock::splitBasicBlock(iterator I, const Twine &BBName,
402                                         bool Before) {
403   if (Before)
404     return splitBasicBlockBefore(I, BBName);
405 
406   assert(getTerminator() && "Can't use splitBasicBlock on degenerate BB!");
407   assert(I != InstList.end() &&
408          "Trying to get me to create degenerate basic block!");
409 
410   BasicBlock *New = BasicBlock::Create(getContext(), BBName, getParent(),
411                                        this->getNextNode());
412 
413   // Save DebugLoc of split point before invalidating iterator.
414   DebugLoc Loc = I->getDebugLoc();
415   // Move all of the specified instructions from the original basic block into
416   // the new basic block.
417   New->splice(New->end(), this, I, end());
418 
419   // Add a branch instruction to the newly formed basic block.
420   BranchInst *BI = BranchInst::Create(New, this);
421   BI->setDebugLoc(Loc);
422 
423   // Now we must loop through all of the successors of the New block (which
424   // _were_ the successors of the 'this' block), and update any PHI nodes in
425   // successors.  If there were PHI nodes in the successors, then they need to
426   // know that incoming branches will be from New, not from Old (this).
427   //
428   New->replaceSuccessorsPhiUsesWith(this, New);
429   return New;
430 }
431 
432 BasicBlock *BasicBlock::splitBasicBlockBefore(iterator I, const Twine &BBName) {
433   assert(getTerminator() &&
434          "Can't use splitBasicBlockBefore on degenerate BB!");
435   assert(I != InstList.end() &&
436          "Trying to get me to create degenerate basic block!");
437 
438   assert((!isa<PHINode>(*I) || getSinglePredecessor()) &&
439          "cannot split on multi incoming phis");
440 
441   BasicBlock *New = BasicBlock::Create(getContext(), BBName, getParent(), this);
442   // Save DebugLoc of split point before invalidating iterator.
443   DebugLoc Loc = I->getDebugLoc();
444   // Move all of the specified instructions from the original basic block into
445   // the new basic block.
446   New->splice(New->end(), this, begin(), I);
447 
448   // Loop through all of the predecessors of the 'this' block (which will be the
449   // predecessors of the New block), replace the specified successor 'this'
450   // block to point at the New block and update any PHI nodes in 'this' block.
451   // If there were PHI nodes in 'this' block, the PHI nodes are updated
452   // to reflect that the incoming branches will be from the New block and not
453   // from predecessors of the 'this' block.
454   // Save predecessors to separate vector before modifying them.
455   SmallVector<BasicBlock *, 4> Predecessors;
456   for (BasicBlock *Pred : predecessors(this))
457     Predecessors.push_back(Pred);
458   for (BasicBlock *Pred : Predecessors) {
459     Instruction *TI = Pred->getTerminator();
460     TI->replaceSuccessorWith(this, New);
461     this->replacePhiUsesWith(Pred, New);
462   }
463   // Add a branch instruction from  "New" to "this" Block.
464   BranchInst *BI = BranchInst::Create(this, New);
465   BI->setDebugLoc(Loc);
466 
467   return New;
468 }
469 
470 void BasicBlock::splice(BasicBlock::iterator ToIt, BasicBlock *FromBB,
471                         BasicBlock::iterator FromBeginIt,
472                         BasicBlock::iterator FromEndIt) {
473 #ifdef EXPENSIVE_CHECKS
474   // Check that FromBeginIt is befor FromEndIt.
475   auto FromBBEnd = FromBB->end();
476   for (auto It = FromBeginIt; It != FromEndIt; ++It)
477     assert(It != FromBBEnd && "FromBeginIt not before FromEndIt!");
478 #endif // EXPENSIVE_CHECKS
479   getInstList().splice(ToIt, FromBB->getInstList(), FromBeginIt, FromEndIt);
480 }
481 
482 BasicBlock::iterator BasicBlock::erase(BasicBlock::iterator FromIt,
483                                        BasicBlock::iterator ToIt) {
484   return InstList.erase(FromIt, ToIt);
485 }
486 
487 void BasicBlock::replacePhiUsesWith(BasicBlock *Old, BasicBlock *New) {
488   // N.B. This might not be a complete BasicBlock, so don't assume
489   // that it ends with a non-phi instruction.
490   for (Instruction &I : *this) {
491     PHINode *PN = dyn_cast<PHINode>(&I);
492     if (!PN)
493       break;
494     PN->replaceIncomingBlockWith(Old, New);
495   }
496 }
497 
498 void BasicBlock::replaceSuccessorsPhiUsesWith(BasicBlock *Old,
499                                               BasicBlock *New) {
500   Instruction *TI = getTerminator();
501   if (!TI)
502     // Cope with being called on a BasicBlock that doesn't have a terminator
503     // yet. Clang's CodeGenFunction::EmitReturnBlock() likes to do this.
504     return;
505   for (BasicBlock *Succ : successors(TI))
506     Succ->replacePhiUsesWith(Old, New);
507 }
508 
509 void BasicBlock::replaceSuccessorsPhiUsesWith(BasicBlock *New) {
510   this->replaceSuccessorsPhiUsesWith(this, New);
511 }
512 
513 bool BasicBlock::isLandingPad() const {
514   return isa<LandingPadInst>(getFirstNonPHI());
515 }
516 
517 const LandingPadInst *BasicBlock::getLandingPadInst() const {
518   return dyn_cast<LandingPadInst>(getFirstNonPHI());
519 }
520 
521 std::optional<uint64_t> BasicBlock::getIrrLoopHeaderWeight() const {
522   const Instruction *TI = getTerminator();
523   if (MDNode *MDIrrLoopHeader =
524       TI->getMetadata(LLVMContext::MD_irr_loop)) {
525     MDString *MDName = cast<MDString>(MDIrrLoopHeader->getOperand(0));
526     if (MDName->getString().equals("loop_header_weight")) {
527       auto *CI = mdconst::extract<ConstantInt>(MDIrrLoopHeader->getOperand(1));
528       return std::optional<uint64_t>(CI->getValue().getZExtValue());
529     }
530   }
531   return std::nullopt;
532 }
533 
534 BasicBlock::iterator llvm::skipDebugIntrinsics(BasicBlock::iterator It) {
535   while (isa<DbgInfoIntrinsic>(It))
536     ++It;
537   return It;
538 }
539 
540 void BasicBlock::renumberInstructions() {
541   unsigned Order = 0;
542   for (Instruction &I : *this)
543     I.Order = Order++;
544 
545   // Set the bit to indicate that the instruction order valid and cached.
546   BasicBlockBits Bits = getBasicBlockBits();
547   Bits.InstrOrderValid = true;
548   setBasicBlockBits(Bits);
549 
550   NumInstrRenumberings++;
551 }
552 
553 #ifndef NDEBUG
554 /// In asserts builds, this checks the numbering. In non-asserts builds, it
555 /// is defined as a no-op inline function in BasicBlock.h.
556 void BasicBlock::validateInstrOrdering() const {
557   if (!isInstrOrderValid())
558     return;
559   const Instruction *Prev = nullptr;
560   for (const Instruction &I : *this) {
561     assert((!Prev || Prev->comesBefore(&I)) &&
562            "cached instruction ordering is incorrect");
563     Prev = &I;
564   }
565 }
566 #endif
567