xref: /aosp_15_r20/external/llvm/lib/IR/BasicBlock.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- BasicBlock.cpp - Implement BasicBlock related methods -------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements the BasicBlock class for the IR library.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker 
14*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/BasicBlock.h"
15*9880d681SAndroid Build Coastguard Worker #include "SymbolTableListTraitsImpl.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/STLExtras.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/CFG.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Constants.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instructions.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/IntrinsicInst.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/LLVMContext.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Type.h"
23*9880d681SAndroid Build Coastguard Worker #include <algorithm>
24*9880d681SAndroid Build Coastguard Worker 
25*9880d681SAndroid Build Coastguard Worker using namespace llvm;
26*9880d681SAndroid Build Coastguard Worker 
getValueSymbolTable()27*9880d681SAndroid Build Coastguard Worker ValueSymbolTable *BasicBlock::getValueSymbolTable() {
28*9880d681SAndroid Build Coastguard Worker   if (Function *F = getParent())
29*9880d681SAndroid Build Coastguard Worker     return &F->getValueSymbolTable();
30*9880d681SAndroid Build Coastguard Worker   return nullptr;
31*9880d681SAndroid Build Coastguard Worker }
32*9880d681SAndroid Build Coastguard Worker 
getContext() const33*9880d681SAndroid Build Coastguard Worker LLVMContext &BasicBlock::getContext() const {
34*9880d681SAndroid Build Coastguard Worker   return getType()->getContext();
35*9880d681SAndroid Build Coastguard Worker }
36*9880d681SAndroid Build Coastguard Worker 
37*9880d681SAndroid Build Coastguard Worker // Explicit instantiation of SymbolTableListTraits since some of the methods
38*9880d681SAndroid Build Coastguard Worker // are not in the public header file...
39*9880d681SAndroid Build Coastguard Worker template class llvm::SymbolTableListTraits<Instruction>;
40*9880d681SAndroid Build Coastguard Worker 
BasicBlock(LLVMContext & C,const Twine & Name,Function * NewParent,BasicBlock * InsertBefore)41*9880d681SAndroid Build Coastguard Worker BasicBlock::BasicBlock(LLVMContext &C, const Twine &Name, Function *NewParent,
42*9880d681SAndroid Build Coastguard Worker                        BasicBlock *InsertBefore)
43*9880d681SAndroid Build Coastguard Worker   : Value(Type::getLabelTy(C), Value::BasicBlockVal), Parent(nullptr) {
44*9880d681SAndroid Build Coastguard Worker 
45*9880d681SAndroid Build Coastguard Worker   if (NewParent)
46*9880d681SAndroid Build Coastguard Worker     insertInto(NewParent, InsertBefore);
47*9880d681SAndroid Build Coastguard Worker   else
48*9880d681SAndroid Build Coastguard Worker     assert(!InsertBefore &&
49*9880d681SAndroid Build Coastguard Worker            "Cannot insert block before another block with no function!");
50*9880d681SAndroid Build Coastguard Worker 
51*9880d681SAndroid Build Coastguard Worker   setName(Name);
52*9880d681SAndroid Build Coastguard Worker }
53*9880d681SAndroid Build Coastguard Worker 
insertInto(Function * NewParent,BasicBlock * InsertBefore)54*9880d681SAndroid Build Coastguard Worker void BasicBlock::insertInto(Function *NewParent, BasicBlock *InsertBefore) {
55*9880d681SAndroid Build Coastguard Worker   assert(NewParent && "Expected a parent");
56*9880d681SAndroid Build Coastguard Worker   assert(!Parent && "Already has a parent");
57*9880d681SAndroid Build Coastguard Worker 
58*9880d681SAndroid Build Coastguard Worker   if (InsertBefore)
59*9880d681SAndroid Build Coastguard Worker     NewParent->getBasicBlockList().insert(InsertBefore->getIterator(), this);
60*9880d681SAndroid Build Coastguard Worker   else
61*9880d681SAndroid Build Coastguard Worker     NewParent->getBasicBlockList().push_back(this);
62*9880d681SAndroid Build Coastguard Worker }
63*9880d681SAndroid Build Coastguard Worker 
~BasicBlock()64*9880d681SAndroid Build Coastguard Worker BasicBlock::~BasicBlock() {
65*9880d681SAndroid Build Coastguard Worker   // If the address of the block is taken and it is being deleted (e.g. because
66*9880d681SAndroid Build Coastguard Worker   // it is dead), this means that there is either a dangling constant expr
67*9880d681SAndroid Build Coastguard Worker   // hanging off the block, or an undefined use of the block (source code
68*9880d681SAndroid Build Coastguard Worker   // expecting the address of a label to keep the block alive even though there
69*9880d681SAndroid Build Coastguard Worker   // is no indirect branch).  Handle these cases by zapping the BlockAddress
70*9880d681SAndroid Build Coastguard Worker   // nodes.  There are no other possible uses at this point.
71*9880d681SAndroid Build Coastguard Worker   if (hasAddressTaken()) {
72*9880d681SAndroid Build Coastguard Worker     assert(!use_empty() && "There should be at least one blockaddress!");
73*9880d681SAndroid Build Coastguard Worker     Constant *Replacement =
74*9880d681SAndroid Build Coastguard Worker       ConstantInt::get(llvm::Type::getInt32Ty(getContext()), 1);
75*9880d681SAndroid Build Coastguard Worker     while (!use_empty()) {
76*9880d681SAndroid Build Coastguard Worker       BlockAddress *BA = cast<BlockAddress>(user_back());
77*9880d681SAndroid Build Coastguard Worker       BA->replaceAllUsesWith(ConstantExpr::getIntToPtr(Replacement,
78*9880d681SAndroid Build Coastguard Worker                                                        BA->getType()));
79*9880d681SAndroid Build Coastguard Worker       BA->destroyConstant();
80*9880d681SAndroid Build Coastguard Worker     }
81*9880d681SAndroid Build Coastguard Worker   }
82*9880d681SAndroid Build Coastguard Worker 
83*9880d681SAndroid Build Coastguard Worker   assert(getParent() == nullptr && "BasicBlock still linked into the program!");
84*9880d681SAndroid Build Coastguard Worker   dropAllReferences();
85*9880d681SAndroid Build Coastguard Worker   InstList.clear();
86*9880d681SAndroid Build Coastguard Worker }
87*9880d681SAndroid Build Coastguard Worker 
setParent(Function * parent)88*9880d681SAndroid Build Coastguard Worker void BasicBlock::setParent(Function *parent) {
89*9880d681SAndroid Build Coastguard Worker   // Set Parent=parent, updating instruction symtab entries as appropriate.
90*9880d681SAndroid Build Coastguard Worker   InstList.setSymTabObject(&Parent, parent);
91*9880d681SAndroid Build Coastguard Worker }
92*9880d681SAndroid Build Coastguard Worker 
removeFromParent()93*9880d681SAndroid Build Coastguard Worker void BasicBlock::removeFromParent() {
94*9880d681SAndroid Build Coastguard Worker   getParent()->getBasicBlockList().remove(getIterator());
95*9880d681SAndroid Build Coastguard Worker }
96*9880d681SAndroid Build Coastguard Worker 
eraseFromParent()97*9880d681SAndroid Build Coastguard Worker iplist<BasicBlock>::iterator BasicBlock::eraseFromParent() {
98*9880d681SAndroid Build Coastguard Worker   return getParent()->getBasicBlockList().erase(getIterator());
99*9880d681SAndroid Build Coastguard Worker }
100*9880d681SAndroid Build Coastguard Worker 
101*9880d681SAndroid Build Coastguard Worker /// Unlink this basic block from its current function and
102*9880d681SAndroid Build Coastguard Worker /// insert it into the function that MovePos lives in, right before MovePos.
moveBefore(BasicBlock * MovePos)103*9880d681SAndroid Build Coastguard Worker void BasicBlock::moveBefore(BasicBlock *MovePos) {
104*9880d681SAndroid Build Coastguard Worker   MovePos->getParent()->getBasicBlockList().splice(
105*9880d681SAndroid Build Coastguard Worker       MovePos->getIterator(), getParent()->getBasicBlockList(), getIterator());
106*9880d681SAndroid Build Coastguard Worker }
107*9880d681SAndroid Build Coastguard Worker 
108*9880d681SAndroid Build Coastguard Worker /// Unlink this basic block from its current function and
109*9880d681SAndroid Build Coastguard Worker /// insert it into the function that MovePos lives in, right after MovePos.
moveAfter(BasicBlock * MovePos)110*9880d681SAndroid Build Coastguard Worker void BasicBlock::moveAfter(BasicBlock *MovePos) {
111*9880d681SAndroid Build Coastguard Worker   MovePos->getParent()->getBasicBlockList().splice(
112*9880d681SAndroid Build Coastguard Worker       ++MovePos->getIterator(), getParent()->getBasicBlockList(),
113*9880d681SAndroid Build Coastguard Worker       getIterator());
114*9880d681SAndroid Build Coastguard Worker }
115*9880d681SAndroid Build Coastguard Worker 
getModule() const116*9880d681SAndroid Build Coastguard Worker const Module *BasicBlock::getModule() const {
117*9880d681SAndroid Build Coastguard Worker   return getParent()->getParent();
118*9880d681SAndroid Build Coastguard Worker }
119*9880d681SAndroid Build Coastguard Worker 
getModule()120*9880d681SAndroid Build Coastguard Worker Module *BasicBlock::getModule() {
121*9880d681SAndroid Build Coastguard Worker   return getParent()->getParent();
122*9880d681SAndroid Build Coastguard Worker }
123*9880d681SAndroid Build Coastguard Worker 
getTerminator()124*9880d681SAndroid Build Coastguard Worker TerminatorInst *BasicBlock::getTerminator() {
125*9880d681SAndroid Build Coastguard Worker   if (InstList.empty()) return nullptr;
126*9880d681SAndroid Build Coastguard Worker   return dyn_cast<TerminatorInst>(&InstList.back());
127*9880d681SAndroid Build Coastguard Worker }
128*9880d681SAndroid Build Coastguard Worker 
getTerminator() const129*9880d681SAndroid Build Coastguard Worker const TerminatorInst *BasicBlock::getTerminator() const {
130*9880d681SAndroid Build Coastguard Worker   if (InstList.empty()) return nullptr;
131*9880d681SAndroid Build Coastguard Worker   return dyn_cast<TerminatorInst>(&InstList.back());
132*9880d681SAndroid Build Coastguard Worker }
133*9880d681SAndroid Build Coastguard Worker 
getTerminatingMustTailCall()134*9880d681SAndroid Build Coastguard Worker CallInst *BasicBlock::getTerminatingMustTailCall() {
135*9880d681SAndroid Build Coastguard Worker   if (InstList.empty())
136*9880d681SAndroid Build Coastguard Worker     return nullptr;
137*9880d681SAndroid Build Coastguard Worker   ReturnInst *RI = dyn_cast<ReturnInst>(&InstList.back());
138*9880d681SAndroid Build Coastguard Worker   if (!RI || RI == &InstList.front())
139*9880d681SAndroid Build Coastguard Worker     return nullptr;
140*9880d681SAndroid Build Coastguard Worker 
141*9880d681SAndroid Build Coastguard Worker   Instruction *Prev = RI->getPrevNode();
142*9880d681SAndroid Build Coastguard Worker   if (!Prev)
143*9880d681SAndroid Build Coastguard Worker     return nullptr;
144*9880d681SAndroid Build Coastguard Worker 
145*9880d681SAndroid Build Coastguard Worker   if (Value *RV = RI->getReturnValue()) {
146*9880d681SAndroid Build Coastguard Worker     if (RV != Prev)
147*9880d681SAndroid Build Coastguard Worker       return nullptr;
148*9880d681SAndroid Build Coastguard Worker 
149*9880d681SAndroid Build Coastguard Worker     // Look through the optional bitcast.
150*9880d681SAndroid Build Coastguard Worker     if (auto *BI = dyn_cast<BitCastInst>(Prev)) {
151*9880d681SAndroid Build Coastguard Worker       RV = BI->getOperand(0);
152*9880d681SAndroid Build Coastguard Worker       Prev = BI->getPrevNode();
153*9880d681SAndroid Build Coastguard Worker       if (!Prev || RV != Prev)
154*9880d681SAndroid Build Coastguard Worker         return nullptr;
155*9880d681SAndroid Build Coastguard Worker     }
156*9880d681SAndroid Build Coastguard Worker   }
157*9880d681SAndroid Build Coastguard Worker 
158*9880d681SAndroid Build Coastguard Worker   if (auto *CI = dyn_cast<CallInst>(Prev)) {
159*9880d681SAndroid Build Coastguard Worker     if (CI->isMustTailCall())
160*9880d681SAndroid Build Coastguard Worker       return CI;
161*9880d681SAndroid Build Coastguard Worker   }
162*9880d681SAndroid Build Coastguard Worker   return nullptr;
163*9880d681SAndroid Build Coastguard Worker }
164*9880d681SAndroid Build Coastguard Worker 
getTerminatingDeoptimizeCall()165*9880d681SAndroid Build Coastguard Worker CallInst *BasicBlock::getTerminatingDeoptimizeCall() {
166*9880d681SAndroid Build Coastguard Worker   if (InstList.empty())
167*9880d681SAndroid Build Coastguard Worker     return nullptr;
168*9880d681SAndroid Build Coastguard Worker   auto *RI = dyn_cast<ReturnInst>(&InstList.back());
169*9880d681SAndroid Build Coastguard Worker   if (!RI || RI == &InstList.front())
170*9880d681SAndroid Build Coastguard Worker     return nullptr;
171*9880d681SAndroid Build Coastguard Worker 
172*9880d681SAndroid Build Coastguard Worker   if (auto *CI = dyn_cast_or_null<CallInst>(RI->getPrevNode()))
173*9880d681SAndroid Build Coastguard Worker     if (Function *F = CI->getCalledFunction())
174*9880d681SAndroid Build Coastguard Worker       if (F->getIntrinsicID() == Intrinsic::experimental_deoptimize)
175*9880d681SAndroid Build Coastguard Worker         return CI;
176*9880d681SAndroid Build Coastguard Worker 
177*9880d681SAndroid Build Coastguard Worker   return nullptr;
178*9880d681SAndroid Build Coastguard Worker }
179*9880d681SAndroid Build Coastguard Worker 
getFirstNonPHI()180*9880d681SAndroid Build Coastguard Worker Instruction* BasicBlock::getFirstNonPHI() {
181*9880d681SAndroid Build Coastguard Worker   for (Instruction &I : *this)
182*9880d681SAndroid Build Coastguard Worker     if (!isa<PHINode>(I))
183*9880d681SAndroid Build Coastguard Worker       return &I;
184*9880d681SAndroid Build Coastguard Worker   return nullptr;
185*9880d681SAndroid Build Coastguard Worker }
186*9880d681SAndroid Build Coastguard Worker 
getFirstNonPHIOrDbg()187*9880d681SAndroid Build Coastguard Worker Instruction* BasicBlock::getFirstNonPHIOrDbg() {
188*9880d681SAndroid Build Coastguard Worker   for (Instruction &I : *this)
189*9880d681SAndroid Build Coastguard Worker     if (!isa<PHINode>(I) && !isa<DbgInfoIntrinsic>(I))
190*9880d681SAndroid Build Coastguard Worker       return &I;
191*9880d681SAndroid Build Coastguard Worker   return nullptr;
192*9880d681SAndroid Build Coastguard Worker }
193*9880d681SAndroid Build Coastguard Worker 
getFirstNonPHIOrDbgOrLifetime()194*9880d681SAndroid Build Coastguard Worker Instruction* BasicBlock::getFirstNonPHIOrDbgOrLifetime() {
195*9880d681SAndroid Build Coastguard Worker   for (Instruction &I : *this) {
196*9880d681SAndroid Build Coastguard Worker     if (isa<PHINode>(I) || isa<DbgInfoIntrinsic>(I))
197*9880d681SAndroid Build Coastguard Worker       continue;
198*9880d681SAndroid Build Coastguard Worker 
199*9880d681SAndroid Build Coastguard Worker     if (auto *II = dyn_cast<IntrinsicInst>(&I))
200*9880d681SAndroid Build Coastguard Worker       if (II->getIntrinsicID() == Intrinsic::lifetime_start ||
201*9880d681SAndroid Build Coastguard Worker           II->getIntrinsicID() == Intrinsic::lifetime_end)
202*9880d681SAndroid Build Coastguard Worker         continue;
203*9880d681SAndroid Build Coastguard Worker 
204*9880d681SAndroid Build Coastguard Worker     return &I;
205*9880d681SAndroid Build Coastguard Worker   }
206*9880d681SAndroid Build Coastguard Worker   return nullptr;
207*9880d681SAndroid Build Coastguard Worker }
208*9880d681SAndroid Build Coastguard Worker 
getFirstInsertionPt()209*9880d681SAndroid Build Coastguard Worker BasicBlock::iterator BasicBlock::getFirstInsertionPt() {
210*9880d681SAndroid Build Coastguard Worker   Instruction *FirstNonPHI = getFirstNonPHI();
211*9880d681SAndroid Build Coastguard Worker   if (!FirstNonPHI)
212*9880d681SAndroid Build Coastguard Worker     return end();
213*9880d681SAndroid Build Coastguard Worker 
214*9880d681SAndroid Build Coastguard Worker   iterator InsertPt = FirstNonPHI->getIterator();
215*9880d681SAndroid Build Coastguard Worker   if (InsertPt->isEHPad()) ++InsertPt;
216*9880d681SAndroid Build Coastguard Worker   return InsertPt;
217*9880d681SAndroid Build Coastguard Worker }
218*9880d681SAndroid Build Coastguard Worker 
dropAllReferences()219*9880d681SAndroid Build Coastguard Worker void BasicBlock::dropAllReferences() {
220*9880d681SAndroid Build Coastguard Worker   for (Instruction &I : *this)
221*9880d681SAndroid Build Coastguard Worker     I.dropAllReferences();
222*9880d681SAndroid Build Coastguard Worker }
223*9880d681SAndroid Build Coastguard Worker 
224*9880d681SAndroid Build Coastguard Worker /// If this basic block has a single predecessor block,
225*9880d681SAndroid Build Coastguard Worker /// return the block, otherwise return a null pointer.
getSinglePredecessor()226*9880d681SAndroid Build Coastguard Worker BasicBlock *BasicBlock::getSinglePredecessor() {
227*9880d681SAndroid Build Coastguard Worker   pred_iterator PI = pred_begin(this), E = pred_end(this);
228*9880d681SAndroid Build Coastguard Worker   if (PI == E) return nullptr;         // No preds.
229*9880d681SAndroid Build Coastguard Worker   BasicBlock *ThePred = *PI;
230*9880d681SAndroid Build Coastguard Worker   ++PI;
231*9880d681SAndroid Build Coastguard Worker   return (PI == E) ? ThePred : nullptr /*multiple preds*/;
232*9880d681SAndroid Build Coastguard Worker }
233*9880d681SAndroid Build Coastguard Worker 
234*9880d681SAndroid Build Coastguard Worker /// If this basic block has a unique predecessor block,
235*9880d681SAndroid Build Coastguard Worker /// return the block, otherwise return a null pointer.
236*9880d681SAndroid Build Coastguard Worker /// Note that unique predecessor doesn't mean single edge, there can be
237*9880d681SAndroid Build Coastguard Worker /// multiple edges from the unique predecessor to this block (for example
238*9880d681SAndroid Build Coastguard Worker /// a switch statement with multiple cases having the same destination).
getUniquePredecessor()239*9880d681SAndroid Build Coastguard Worker BasicBlock *BasicBlock::getUniquePredecessor() {
240*9880d681SAndroid Build Coastguard Worker   pred_iterator PI = pred_begin(this), E = pred_end(this);
241*9880d681SAndroid Build Coastguard Worker   if (PI == E) return nullptr; // No preds.
242*9880d681SAndroid Build Coastguard Worker   BasicBlock *PredBB = *PI;
243*9880d681SAndroid Build Coastguard Worker   ++PI;
244*9880d681SAndroid Build Coastguard Worker   for (;PI != E; ++PI) {
245*9880d681SAndroid Build Coastguard Worker     if (*PI != PredBB)
246*9880d681SAndroid Build Coastguard Worker       return nullptr;
247*9880d681SAndroid Build Coastguard Worker     // The same predecessor appears multiple times in the predecessor list.
248*9880d681SAndroid Build Coastguard Worker     // This is OK.
249*9880d681SAndroid Build Coastguard Worker   }
250*9880d681SAndroid Build Coastguard Worker   return PredBB;
251*9880d681SAndroid Build Coastguard Worker }
252*9880d681SAndroid Build Coastguard Worker 
getSingleSuccessor()253*9880d681SAndroid Build Coastguard Worker BasicBlock *BasicBlock::getSingleSuccessor() {
254*9880d681SAndroid Build Coastguard Worker   succ_iterator SI = succ_begin(this), E = succ_end(this);
255*9880d681SAndroid Build Coastguard Worker   if (SI == E) return nullptr; // no successors
256*9880d681SAndroid Build Coastguard Worker   BasicBlock *TheSucc = *SI;
257*9880d681SAndroid Build Coastguard Worker   ++SI;
258*9880d681SAndroid Build Coastguard Worker   return (SI == E) ? TheSucc : nullptr /* multiple successors */;
259*9880d681SAndroid Build Coastguard Worker }
260*9880d681SAndroid Build Coastguard Worker 
getUniqueSuccessor()261*9880d681SAndroid Build Coastguard Worker BasicBlock *BasicBlock::getUniqueSuccessor() {
262*9880d681SAndroid Build Coastguard Worker   succ_iterator SI = succ_begin(this), E = succ_end(this);
263*9880d681SAndroid Build Coastguard Worker   if (SI == E) return nullptr; // No successors
264*9880d681SAndroid Build Coastguard Worker   BasicBlock *SuccBB = *SI;
265*9880d681SAndroid Build Coastguard Worker   ++SI;
266*9880d681SAndroid Build Coastguard Worker   for (;SI != E; ++SI) {
267*9880d681SAndroid Build Coastguard Worker     if (*SI != SuccBB)
268*9880d681SAndroid Build Coastguard Worker       return nullptr;
269*9880d681SAndroid Build Coastguard Worker     // The same successor appears multiple times in the successor list.
270*9880d681SAndroid Build Coastguard Worker     // This is OK.
271*9880d681SAndroid Build Coastguard Worker   }
272*9880d681SAndroid Build Coastguard Worker   return SuccBB;
273*9880d681SAndroid Build Coastguard Worker }
274*9880d681SAndroid Build Coastguard Worker 
275*9880d681SAndroid Build Coastguard Worker /// This method is used to notify a BasicBlock that the
276*9880d681SAndroid Build Coastguard Worker /// specified Predecessor of the block is no longer able to reach it.  This is
277*9880d681SAndroid Build Coastguard Worker /// actually not used to update the Predecessor list, but is actually used to
278*9880d681SAndroid Build Coastguard Worker /// update the PHI nodes that reside in the block.  Note that this should be
279*9880d681SAndroid Build Coastguard Worker /// called while the predecessor still refers to this block.
280*9880d681SAndroid Build Coastguard Worker ///
removePredecessor(BasicBlock * Pred,bool DontDeleteUselessPHIs)281*9880d681SAndroid Build Coastguard Worker void BasicBlock::removePredecessor(BasicBlock *Pred,
282*9880d681SAndroid Build Coastguard Worker                                    bool DontDeleteUselessPHIs) {
283*9880d681SAndroid Build Coastguard Worker   assert((hasNUsesOrMore(16)||// Reduce cost of this assertion for complex CFGs.
284*9880d681SAndroid Build Coastguard Worker           find(pred_begin(this), pred_end(this), Pred) != pred_end(this)) &&
285*9880d681SAndroid Build Coastguard Worker          "removePredecessor: BB is not a predecessor!");
286*9880d681SAndroid Build Coastguard Worker 
287*9880d681SAndroid Build Coastguard Worker   if (InstList.empty()) return;
288*9880d681SAndroid Build Coastguard Worker   PHINode *APN = dyn_cast<PHINode>(&front());
289*9880d681SAndroid Build Coastguard Worker   if (!APN) return;   // Quick exit.
290*9880d681SAndroid Build Coastguard Worker 
291*9880d681SAndroid Build Coastguard Worker   // If there are exactly two predecessors, then we want to nuke the PHI nodes
292*9880d681SAndroid Build Coastguard Worker   // altogether.  However, we cannot do this, if this in this case:
293*9880d681SAndroid Build Coastguard Worker   //
294*9880d681SAndroid Build Coastguard Worker   //  Loop:
295*9880d681SAndroid Build Coastguard Worker   //    %x = phi [X, Loop]
296*9880d681SAndroid Build Coastguard Worker   //    %x2 = add %x, 1         ;; This would become %x2 = add %x2, 1
297*9880d681SAndroid Build Coastguard Worker   //    br Loop                 ;; %x2 does not dominate all uses
298*9880d681SAndroid Build Coastguard Worker   //
299*9880d681SAndroid Build Coastguard Worker   // This is because the PHI node input is actually taken from the predecessor
300*9880d681SAndroid Build Coastguard Worker   // basic block.  The only case this can happen is with a self loop, so we
301*9880d681SAndroid Build Coastguard Worker   // check for this case explicitly now.
302*9880d681SAndroid Build Coastguard Worker   //
303*9880d681SAndroid Build Coastguard Worker   unsigned max_idx = APN->getNumIncomingValues();
304*9880d681SAndroid Build Coastguard Worker   assert(max_idx != 0 && "PHI Node in block with 0 predecessors!?!?!");
305*9880d681SAndroid Build Coastguard Worker   if (max_idx == 2) {
306*9880d681SAndroid Build Coastguard Worker     BasicBlock *Other = APN->getIncomingBlock(APN->getIncomingBlock(0) == Pred);
307*9880d681SAndroid Build Coastguard Worker 
308*9880d681SAndroid Build Coastguard Worker     // Disable PHI elimination!
309*9880d681SAndroid Build Coastguard Worker     if (this == Other) max_idx = 3;
310*9880d681SAndroid Build Coastguard Worker   }
311*9880d681SAndroid Build Coastguard Worker 
312*9880d681SAndroid Build Coastguard Worker   // <= Two predecessors BEFORE I remove one?
313*9880d681SAndroid Build Coastguard Worker   if (max_idx <= 2 && !DontDeleteUselessPHIs) {
314*9880d681SAndroid Build Coastguard Worker     // Yup, loop through and nuke the PHI nodes
315*9880d681SAndroid Build Coastguard Worker     while (PHINode *PN = dyn_cast<PHINode>(&front())) {
316*9880d681SAndroid Build Coastguard Worker       // Remove the predecessor first.
317*9880d681SAndroid Build Coastguard Worker       PN->removeIncomingValue(Pred, !DontDeleteUselessPHIs);
318*9880d681SAndroid Build Coastguard Worker 
319*9880d681SAndroid Build Coastguard Worker       // If the PHI _HAD_ two uses, replace PHI node with its now *single* value
320*9880d681SAndroid Build Coastguard Worker       if (max_idx == 2) {
321*9880d681SAndroid Build Coastguard Worker         if (PN->getIncomingValue(0) != PN)
322*9880d681SAndroid Build Coastguard Worker           PN->replaceAllUsesWith(PN->getIncomingValue(0));
323*9880d681SAndroid Build Coastguard Worker         else
324*9880d681SAndroid Build Coastguard Worker           // We are left with an infinite loop with no entries: kill the PHI.
325*9880d681SAndroid Build Coastguard Worker           PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
326*9880d681SAndroid Build Coastguard Worker         getInstList().pop_front();    // Remove the PHI node
327*9880d681SAndroid Build Coastguard Worker       }
328*9880d681SAndroid Build Coastguard Worker 
329*9880d681SAndroid Build Coastguard Worker       // If the PHI node already only had one entry, it got deleted by
330*9880d681SAndroid Build Coastguard Worker       // removeIncomingValue.
331*9880d681SAndroid Build Coastguard Worker     }
332*9880d681SAndroid Build Coastguard Worker   } else {
333*9880d681SAndroid Build Coastguard Worker     // Okay, now we know that we need to remove predecessor #pred_idx from all
334*9880d681SAndroid Build Coastguard Worker     // PHI nodes.  Iterate over each PHI node fixing them up
335*9880d681SAndroid Build Coastguard Worker     PHINode *PN;
336*9880d681SAndroid Build Coastguard Worker     for (iterator II = begin(); (PN = dyn_cast<PHINode>(II)); ) {
337*9880d681SAndroid Build Coastguard Worker       ++II;
338*9880d681SAndroid Build Coastguard Worker       PN->removeIncomingValue(Pred, false);
339*9880d681SAndroid Build Coastguard Worker       // If all incoming values to the Phi are the same, we can replace the Phi
340*9880d681SAndroid Build Coastguard Worker       // with that value.
341*9880d681SAndroid Build Coastguard Worker       Value* PNV = nullptr;
342*9880d681SAndroid Build Coastguard Worker       if (!DontDeleteUselessPHIs && (PNV = PN->hasConstantValue()))
343*9880d681SAndroid Build Coastguard Worker         if (PNV != PN) {
344*9880d681SAndroid Build Coastguard Worker           PN->replaceAllUsesWith(PNV);
345*9880d681SAndroid Build Coastguard Worker           PN->eraseFromParent();
346*9880d681SAndroid Build Coastguard Worker         }
347*9880d681SAndroid Build Coastguard Worker     }
348*9880d681SAndroid Build Coastguard Worker   }
349*9880d681SAndroid Build Coastguard Worker }
350*9880d681SAndroid Build Coastguard Worker 
canSplitPredecessors() const351*9880d681SAndroid Build Coastguard Worker bool BasicBlock::canSplitPredecessors() const {
352*9880d681SAndroid Build Coastguard Worker   const Instruction *FirstNonPHI = getFirstNonPHI();
353*9880d681SAndroid Build Coastguard Worker   if (isa<LandingPadInst>(FirstNonPHI))
354*9880d681SAndroid Build Coastguard Worker     return true;
355*9880d681SAndroid Build Coastguard Worker   // This is perhaps a little conservative because constructs like
356*9880d681SAndroid Build Coastguard Worker   // CleanupBlockInst are pretty easy to split.  However, SplitBlockPredecessors
357*9880d681SAndroid Build Coastguard Worker   // cannot handle such things just yet.
358*9880d681SAndroid Build Coastguard Worker   if (FirstNonPHI->isEHPad())
359*9880d681SAndroid Build Coastguard Worker     return false;
360*9880d681SAndroid Build Coastguard Worker   return true;
361*9880d681SAndroid Build Coastguard Worker }
362*9880d681SAndroid Build Coastguard Worker 
363*9880d681SAndroid Build Coastguard Worker /// This splits a basic block into two at the specified
364*9880d681SAndroid Build Coastguard Worker /// instruction.  Note that all instructions BEFORE the specified iterator stay
365*9880d681SAndroid Build Coastguard Worker /// as part of the original basic block, an unconditional branch is added to
366*9880d681SAndroid Build Coastguard Worker /// the new BB, and the rest of the instructions in the BB are moved to the new
367*9880d681SAndroid Build Coastguard Worker /// BB, including the old terminator.  This invalidates the iterator.
368*9880d681SAndroid Build Coastguard Worker ///
369*9880d681SAndroid Build Coastguard Worker /// Note that this only works on well formed basic blocks (must have a
370*9880d681SAndroid Build Coastguard Worker /// terminator), and 'I' must not be the end of instruction list (which would
371*9880d681SAndroid Build Coastguard Worker /// cause a degenerate basic block to be formed, having a terminator inside of
372*9880d681SAndroid Build Coastguard Worker /// the basic block).
373*9880d681SAndroid Build Coastguard Worker ///
splitBasicBlock(iterator I,const Twine & BBName)374*9880d681SAndroid Build Coastguard Worker BasicBlock *BasicBlock::splitBasicBlock(iterator I, const Twine &BBName) {
375*9880d681SAndroid Build Coastguard Worker   assert(getTerminator() && "Can't use splitBasicBlock on degenerate BB!");
376*9880d681SAndroid Build Coastguard Worker   assert(I != InstList.end() &&
377*9880d681SAndroid Build Coastguard Worker          "Trying to get me to create degenerate basic block!");
378*9880d681SAndroid Build Coastguard Worker 
379*9880d681SAndroid Build Coastguard Worker   BasicBlock *New = BasicBlock::Create(getContext(), BBName, getParent(),
380*9880d681SAndroid Build Coastguard Worker                                        this->getNextNode());
381*9880d681SAndroid Build Coastguard Worker 
382*9880d681SAndroid Build Coastguard Worker   // Save DebugLoc of split point before invalidating iterator.
383*9880d681SAndroid Build Coastguard Worker   DebugLoc Loc = I->getDebugLoc();
384*9880d681SAndroid Build Coastguard Worker   // Move all of the specified instructions from the original basic block into
385*9880d681SAndroid Build Coastguard Worker   // the new basic block.
386*9880d681SAndroid Build Coastguard Worker   New->getInstList().splice(New->end(), this->getInstList(), I, end());
387*9880d681SAndroid Build Coastguard Worker 
388*9880d681SAndroid Build Coastguard Worker   // Add a branch instruction to the newly formed basic block.
389*9880d681SAndroid Build Coastguard Worker   BranchInst *BI = BranchInst::Create(New, this);
390*9880d681SAndroid Build Coastguard Worker   BI->setDebugLoc(Loc);
391*9880d681SAndroid Build Coastguard Worker 
392*9880d681SAndroid Build Coastguard Worker   // Now we must loop through all of the successors of the New block (which
393*9880d681SAndroid Build Coastguard Worker   // _were_ the successors of the 'this' block), and update any PHI nodes in
394*9880d681SAndroid Build Coastguard Worker   // successors.  If there were PHI nodes in the successors, then they need to
395*9880d681SAndroid Build Coastguard Worker   // know that incoming branches will be from New, not from Old.
396*9880d681SAndroid Build Coastguard Worker   //
397*9880d681SAndroid Build Coastguard Worker   for (succ_iterator I = succ_begin(New), E = succ_end(New); I != E; ++I) {
398*9880d681SAndroid Build Coastguard Worker     // Loop over any phi nodes in the basic block, updating the BB field of
399*9880d681SAndroid Build Coastguard Worker     // incoming values...
400*9880d681SAndroid Build Coastguard Worker     BasicBlock *Successor = *I;
401*9880d681SAndroid Build Coastguard Worker     PHINode *PN;
402*9880d681SAndroid Build Coastguard Worker     for (BasicBlock::iterator II = Successor->begin();
403*9880d681SAndroid Build Coastguard Worker          (PN = dyn_cast<PHINode>(II)); ++II) {
404*9880d681SAndroid Build Coastguard Worker       int IDX = PN->getBasicBlockIndex(this);
405*9880d681SAndroid Build Coastguard Worker       while (IDX != -1) {
406*9880d681SAndroid Build Coastguard Worker         PN->setIncomingBlock((unsigned)IDX, New);
407*9880d681SAndroid Build Coastguard Worker         IDX = PN->getBasicBlockIndex(this);
408*9880d681SAndroid Build Coastguard Worker       }
409*9880d681SAndroid Build Coastguard Worker     }
410*9880d681SAndroid Build Coastguard Worker   }
411*9880d681SAndroid Build Coastguard Worker   return New;
412*9880d681SAndroid Build Coastguard Worker }
413*9880d681SAndroid Build Coastguard Worker 
replaceSuccessorsPhiUsesWith(BasicBlock * New)414*9880d681SAndroid Build Coastguard Worker void BasicBlock::replaceSuccessorsPhiUsesWith(BasicBlock *New) {
415*9880d681SAndroid Build Coastguard Worker   TerminatorInst *TI = getTerminator();
416*9880d681SAndroid Build Coastguard Worker   if (!TI)
417*9880d681SAndroid Build Coastguard Worker     // Cope with being called on a BasicBlock that doesn't have a terminator
418*9880d681SAndroid Build Coastguard Worker     // yet. Clang's CodeGenFunction::EmitReturnBlock() likes to do this.
419*9880d681SAndroid Build Coastguard Worker     return;
420*9880d681SAndroid Build Coastguard Worker   for (BasicBlock *Succ : TI->successors()) {
421*9880d681SAndroid Build Coastguard Worker     // N.B. Succ might not be a complete BasicBlock, so don't assume
422*9880d681SAndroid Build Coastguard Worker     // that it ends with a non-phi instruction.
423*9880d681SAndroid Build Coastguard Worker     for (iterator II = Succ->begin(), IE = Succ->end(); II != IE; ++II) {
424*9880d681SAndroid Build Coastguard Worker       PHINode *PN = dyn_cast<PHINode>(II);
425*9880d681SAndroid Build Coastguard Worker       if (!PN)
426*9880d681SAndroid Build Coastguard Worker         break;
427*9880d681SAndroid Build Coastguard Worker       int i;
428*9880d681SAndroid Build Coastguard Worker       while ((i = PN->getBasicBlockIndex(this)) >= 0)
429*9880d681SAndroid Build Coastguard Worker         PN->setIncomingBlock(i, New);
430*9880d681SAndroid Build Coastguard Worker     }
431*9880d681SAndroid Build Coastguard Worker   }
432*9880d681SAndroid Build Coastguard Worker }
433*9880d681SAndroid Build Coastguard Worker 
434*9880d681SAndroid Build Coastguard Worker /// Return true if this basic block is a landing pad. I.e., it's
435*9880d681SAndroid Build Coastguard Worker /// the destination of the 'unwind' edge of an invoke instruction.
isLandingPad() const436*9880d681SAndroid Build Coastguard Worker bool BasicBlock::isLandingPad() const {
437*9880d681SAndroid Build Coastguard Worker   return isa<LandingPadInst>(getFirstNonPHI());
438*9880d681SAndroid Build Coastguard Worker }
439*9880d681SAndroid Build Coastguard Worker 
440*9880d681SAndroid Build Coastguard Worker /// Return the landingpad instruction associated with the landing pad.
getLandingPadInst()441*9880d681SAndroid Build Coastguard Worker LandingPadInst *BasicBlock::getLandingPadInst() {
442*9880d681SAndroid Build Coastguard Worker   return dyn_cast<LandingPadInst>(getFirstNonPHI());
443*9880d681SAndroid Build Coastguard Worker }
getLandingPadInst() const444*9880d681SAndroid Build Coastguard Worker const LandingPadInst *BasicBlock::getLandingPadInst() const {
445*9880d681SAndroid Build Coastguard Worker   return dyn_cast<LandingPadInst>(getFirstNonPHI());
446*9880d681SAndroid Build Coastguard Worker }
447