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