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   succ_const_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   succ_const_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.  However, we cannot do this, if this in this case:
338   //
339   //  Loop:
340   //    %x = phi [X, Loop]
341   //    %x2 = add %x, 1         ;; This would become %x2 = add %x2, 1
342   //    br Loop                 ;; %x2 does not dominate all uses
343   //
344   // This is because the PHI node input is actually taken from the predecessor
345   // basic block.  The only case this can happen is with a self loop, so we
346   // check for this case explicitly now.
347   //
348   unsigned max_idx = APN->getNumIncomingValues();
349   assert(max_idx != 0 && "PHI Node in block with 0 predecessors!?!?!");
350   if (max_idx == 2) {
351     BasicBlock *Other = APN->getIncomingBlock(APN->getIncomingBlock(0) == Pred);
352 
353     // Disable PHI elimination!
354     if (this == Other) max_idx = 3;
355   }
356 
357   // <= Two predecessors BEFORE I remove one?
358   if (max_idx <= 2 && !KeepOneInputPHIs) {
359     // Yup, loop through and nuke the PHI nodes
360     while (PHINode *PN = dyn_cast<PHINode>(&front())) {
361       // Remove the predecessor first.
362       PN->removeIncomingValue(Pred, !KeepOneInputPHIs);
363 
364       // If the PHI _HAD_ two uses, replace PHI node with its now *single* value
365       if (max_idx == 2) {
366         if (PN->getIncomingValue(0) != PN)
367           PN->replaceAllUsesWith(PN->getIncomingValue(0));
368         else
369           // We are left with an infinite loop with no entries: kill the PHI.
370           PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
371         getInstList().pop_front();    // Remove the PHI node
372       }
373 
374       // If the PHI node already only had one entry, it got deleted by
375       // removeIncomingValue.
376     }
377   } else {
378     // Okay, now we know that we need to remove predecessor #pred_idx from all
379     // PHI nodes.  Iterate over each PHI node fixing them up
380     PHINode *PN;
381     for (iterator II = begin(); (PN = dyn_cast<PHINode>(II)); ) {
382       ++II;
383       PN->removeIncomingValue(Pred, false);
384       // If all incoming values to the Phi are the same, we can replace the Phi
385       // with that value.
386       Value* PNV = nullptr;
387       if (!KeepOneInputPHIs && (PNV = PN->hasConstantValue()))
388         if (PNV != PN) {
389           PN->replaceAllUsesWith(PNV);
390           PN->eraseFromParent();
391         }
392     }
393   }
394 }
395 
396 bool BasicBlock::canSplitPredecessors() const {
397   const Instruction *FirstNonPHI = getFirstNonPHI();
398   if (isa<LandingPadInst>(FirstNonPHI))
399     return true;
400   // This is perhaps a little conservative because constructs like
401   // CleanupBlockInst are pretty easy to split.  However, SplitBlockPredecessors
402   // cannot handle such things just yet.
403   if (FirstNonPHI->isEHPad())
404     return false;
405   return true;
406 }
407 
408 bool BasicBlock::isLegalToHoistInto() const {
409   auto *Term = getTerminator();
410   // No terminator means the block is under construction.
411   if (!Term)
412     return true;
413 
414   // If the block has no successors, there can be no instructions to hoist.
415   assert(Term->getNumSuccessors() > 0);
416 
417   // Instructions should not be hoisted across exception handling boundaries.
418   return !Term->isExceptionalTerminator();
419 }
420 
421 /// This splits a basic block into two at the specified
422 /// instruction.  Note that all instructions BEFORE the specified iterator stay
423 /// as part of the original basic block, an unconditional branch is added to
424 /// the new BB, and the rest of the instructions in the BB are moved to the new
425 /// BB, including the old terminator.  This invalidates the iterator.
426 ///
427 /// Note that this only works on well formed basic blocks (must have a
428 /// terminator), and 'I' must not be the end of instruction list (which would
429 /// cause a degenerate basic block to be formed, having a terminator inside of
430 /// the basic block).
431 ///
432 BasicBlock *BasicBlock::splitBasicBlock(iterator I, const Twine &BBName) {
433   assert(getTerminator() && "Can't use splitBasicBlock on degenerate BB!");
434   assert(I != InstList.end() &&
435          "Trying to get me to create degenerate basic block!");
436 
437   BasicBlock *New = BasicBlock::Create(getContext(), BBName, getParent(),
438                                        this->getNextNode());
439 
440   // Save DebugLoc of split point before invalidating iterator.
441   DebugLoc Loc = I->getDebugLoc();
442   // Move all of the specified instructions from the original basic block into
443   // the new basic block.
444   New->getInstList().splice(New->end(), this->getInstList(), I, end());
445 
446   // Add a branch instruction to the newly formed basic block.
447   BranchInst *BI = BranchInst::Create(New, this);
448   BI->setDebugLoc(Loc);
449 
450   // Now we must loop through all of the successors of the New block (which
451   // _were_ the successors of the 'this' block), and update any PHI nodes in
452   // successors.  If there were PHI nodes in the successors, then they need to
453   // know that incoming branches will be from New, not from Old (this).
454   //
455   New->replaceSuccessorsPhiUsesWith(this, New);
456   return New;
457 }
458 
459 void BasicBlock::replacePhiUsesWith(BasicBlock *Old, BasicBlock *New) {
460   // N.B. This might not be a complete BasicBlock, so don't assume
461   // that it ends with a non-phi instruction.
462   for (iterator II = begin(), IE = end(); II != IE; ++II) {
463     PHINode *PN = dyn_cast<PHINode>(II);
464     if (!PN)
465       break;
466     PN->replaceIncomingBlockWith(Old, New);
467   }
468 }
469 
470 void BasicBlock::replaceSuccessorsPhiUsesWith(BasicBlock *Old,
471                                               BasicBlock *New) {
472   Instruction *TI = getTerminator();
473   if (!TI)
474     // Cope with being called on a BasicBlock that doesn't have a terminator
475     // yet. Clang's CodeGenFunction::EmitReturnBlock() likes to do this.
476     return;
477   llvm::for_each(successors(TI), [Old, New](BasicBlock *Succ) {
478     Succ->replacePhiUsesWith(Old, New);
479   });
480 }
481 
482 void BasicBlock::replaceSuccessorsPhiUsesWith(BasicBlock *New) {
483   this->replaceSuccessorsPhiUsesWith(this, New);
484 }
485 
486 /// Return true if this basic block is a landing pad. I.e., it's
487 /// the destination of the 'unwind' edge of an invoke instruction.
488 bool BasicBlock::isLandingPad() const {
489   return isa<LandingPadInst>(getFirstNonPHI());
490 }
491 
492 /// Return the landingpad instruction associated with the landing pad.
493 const LandingPadInst *BasicBlock::getLandingPadInst() const {
494   return dyn_cast<LandingPadInst>(getFirstNonPHI());
495 }
496 
497 Optional<uint64_t> BasicBlock::getIrrLoopHeaderWeight() const {
498   const Instruction *TI = getTerminator();
499   if (MDNode *MDIrrLoopHeader =
500       TI->getMetadata(LLVMContext::MD_irr_loop)) {
501     MDString *MDName = cast<MDString>(MDIrrLoopHeader->getOperand(0));
502     if (MDName->getString().equals("loop_header_weight")) {
503       auto *CI = mdconst::extract<ConstantInt>(MDIrrLoopHeader->getOperand(1));
504       return Optional<uint64_t>(CI->getValue().getZExtValue());
505     }
506   }
507   return Optional<uint64_t>();
508 }
509 
510 BasicBlock::iterator llvm::skipDebugIntrinsics(BasicBlock::iterator It) {
511   while (isa<DbgInfoIntrinsic>(It))
512     ++It;
513   return It;
514 }
515 
516 void BasicBlock::renumberInstructions() {
517   unsigned Order = 0;
518   for (Instruction &I : *this)
519     I.Order = Order++;
520 
521   // Set the bit to indicate that the instruction order valid and cached.
522   BasicBlockBits Bits = getBasicBlockBits();
523   Bits.InstrOrderValid = true;
524   setBasicBlockBits(Bits);
525 }
526 
527 #ifndef NDEBUG
528 /// In asserts builds, this checks the numbering. In non-asserts builds, it
529 /// is defined as a no-op inline function in BasicBlock.h.
530 void BasicBlock::validateInstrOrdering() const {
531   if (!isInstrOrderValid())
532     return;
533   const Instruction *Prev = nullptr;
534   for (const Instruction &I : *this) {
535     assert((!Prev || Prev->comesBefore(&I)) &&
536            "cached instruction ordering is incorrect");
537     Prev = &I;
538   }
539 }
540 #endif
541