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