0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
1 //===-- Instruction.cpp - Implement the Instruction class -----------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
3 // The LLVM Compiler Infrastructure
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
4 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
5 // This file is distributed under the University of Illinois Open Source
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
6 // License. See LICENSE.TXT for details.
|
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 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
10 // This file implements the Instruction class for the IR library.
|
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
|
121
|
14 #include "llvm/IR/Instruction.h"
|
120
|
15 #include "llvm/ADT/DenseSet.h"
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
16 #include "llvm/IR/Constants.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
17 #include "llvm/IR/Instructions.h"
|
121
|
18 #include "llvm/IR/MDBuilder.h"
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
19 #include "llvm/IR/Operator.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
20 #include "llvm/IR/Type.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
21 using namespace llvm;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
22
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
23 Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
24 Instruction *InsertBefore)
|
77
|
25 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
26
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
27 // If requested, insert this instruction into a basic block...
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
28 if (InsertBefore) {
|
95
|
29 BasicBlock *BB = InsertBefore->getParent();
|
|
30 assert(BB && "Instruction to insert before is not in a basic block!");
|
|
31 BB->getInstList().insert(InsertBefore->getIterator(), this);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
32 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
33 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
34
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
35 Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
36 BasicBlock *InsertAtEnd)
|
77
|
37 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
38
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
39 // append this instruction into the basic block
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
40 assert(InsertAtEnd && "Basic block to append to may not be NULL!");
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
41 InsertAtEnd->getInstList().push_back(this);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
42 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
43
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
44 Instruction::~Instruction() {
|
77
|
45 assert(!Parent && "Instruction still linked in the program!");
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
46 if (hasMetadataHashEntry())
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
47 clearMetadataHashEntries();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
48 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
49
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
50
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
51 void Instruction::setParent(BasicBlock *P) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
52 Parent = P;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
53 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
54
|
95
|
55 const Module *Instruction::getModule() const {
|
|
56 return getParent()->getModule();
|
|
57 }
|
|
58
|
100
|
59 const Function *Instruction::getFunction() const {
|
|
60 return getParent()->getParent();
|
|
61 }
|
95
|
62
|
|
63 void Instruction::removeFromParent() {
|
|
64 getParent()->getInstList().remove(getIterator());
|
|
65 }
|
|
66
|
|
67 iplist<Instruction>::iterator Instruction::eraseFromParent() {
|
|
68 return getParent()->getInstList().erase(getIterator());
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
69 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
70
|
100
|
71 /// Insert an unlinked instruction into a basic block immediately before the
|
|
72 /// specified instruction.
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
73 void Instruction::insertBefore(Instruction *InsertPos) {
|
95
|
74 InsertPos->getParent()->getInstList().insert(InsertPos->getIterator(), this);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
75 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
76
|
100
|
77 /// Insert an unlinked instruction into a basic block immediately after the
|
|
78 /// specified instruction.
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
79 void Instruction::insertAfter(Instruction *InsertPos) {
|
95
|
80 InsertPos->getParent()->getInstList().insertAfter(InsertPos->getIterator(),
|
|
81 this);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
82 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
83
|
100
|
84 /// Unlink this instruction from its current basic block and insert it into the
|
|
85 /// basic block that MovePos lives in, right before MovePos.
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
86 void Instruction::moveBefore(Instruction *MovePos) {
|
120
|
87 moveBefore(*MovePos->getParent(), MovePos->getIterator());
|
|
88 }
|
|
89
|
121
|
90 void Instruction::moveAfter(Instruction *MovePos) {
|
|
91 moveBefore(*MovePos->getParent(), ++MovePos->getIterator());
|
|
92 }
|
|
93
|
120
|
94 void Instruction::moveBefore(BasicBlock &BB,
|
|
95 SymbolTableList<Instruction>::iterator I) {
|
|
96 assert(I == BB.end() || I->getParent() == &BB);
|
|
97 BB.getInstList().splice(I, getParent()->getInstList(), getIterator());
|
|
98 }
|
|
99
|
|
100 void Instruction::setHasNoUnsignedWrap(bool b) {
|
|
101 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
|
|
102 }
|
|
103
|
|
104 void Instruction::setHasNoSignedWrap(bool b) {
|
|
105 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
106 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
107
|
120
|
108 void Instruction::setIsExact(bool b) {
|
|
109 cast<PossiblyExactOperator>(this)->setIsExact(b);
|
|
110 }
|
|
111
|
|
112 bool Instruction::hasNoUnsignedWrap() const {
|
|
113 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
|
|
114 }
|
|
115
|
|
116 bool Instruction::hasNoSignedWrap() const {
|
|
117 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
|
|
118 }
|
|
119
|
121
|
120 void Instruction::dropPoisonGeneratingFlags() {
|
|
121 switch (getOpcode()) {
|
|
122 case Instruction::Add:
|
|
123 case Instruction::Sub:
|
|
124 case Instruction::Mul:
|
|
125 case Instruction::Shl:
|
|
126 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(false);
|
|
127 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(false);
|
|
128 break;
|
|
129
|
|
130 case Instruction::UDiv:
|
|
131 case Instruction::SDiv:
|
|
132 case Instruction::AShr:
|
|
133 case Instruction::LShr:
|
|
134 cast<PossiblyExactOperator>(this)->setIsExact(false);
|
|
135 break;
|
|
136
|
|
137 case Instruction::GetElementPtr:
|
|
138 cast<GetElementPtrInst>(this)->setIsInBounds(false);
|
|
139 break;
|
|
140 }
|
|
141 }
|
|
142
|
120
|
143 bool Instruction::isExact() const {
|
|
144 return cast<PossiblyExactOperator>(this)->isExact();
|
|
145 }
|
|
146
|
134
|
147 void Instruction::setFast(bool B) {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
148 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
|
134
|
149 cast<FPMathOperator>(this)->setFast(B);
|
|
150 }
|
|
151
|
|
152 void Instruction::setHasAllowReassoc(bool B) {
|
|
153 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
|
|
154 cast<FPMathOperator>(this)->setHasAllowReassoc(B);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
155 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
156
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
157 void Instruction::setHasNoNaNs(bool B) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
158 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
159 cast<FPMathOperator>(this)->setHasNoNaNs(B);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
160 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
161
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
162 void Instruction::setHasNoInfs(bool B) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
163 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
164 cast<FPMathOperator>(this)->setHasNoInfs(B);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
165 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
166
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
167 void Instruction::setHasNoSignedZeros(bool B) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
168 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
169 cast<FPMathOperator>(this)->setHasNoSignedZeros(B);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
170 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
171
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
172 void Instruction::setHasAllowReciprocal(bool B) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
173 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
174 cast<FPMathOperator>(this)->setHasAllowReciprocal(B);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
175 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
176
|
134
|
177 void Instruction::setHasApproxFunc(bool B) {
|
|
178 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
|
|
179 cast<FPMathOperator>(this)->setHasApproxFunc(B);
|
|
180 }
|
|
181
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
182 void Instruction::setFastMathFlags(FastMathFlags FMF) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
183 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
184 cast<FPMathOperator>(this)->setFastMathFlags(FMF);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
185 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
186
|
77
|
187 void Instruction::copyFastMathFlags(FastMathFlags FMF) {
|
|
188 assert(isa<FPMathOperator>(this) && "copying fast-math flag on invalid op");
|
|
189 cast<FPMathOperator>(this)->copyFastMathFlags(FMF);
|
|
190 }
|
|
191
|
134
|
192 bool Instruction::isFast() const {
|
77
|
193 assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
|
134
|
194 return cast<FPMathOperator>(this)->isFast();
|
|
195 }
|
|
196
|
|
197 bool Instruction::hasAllowReassoc() const {
|
|
198 assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
|
|
199 return cast<FPMathOperator>(this)->hasAllowReassoc();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
200 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
201
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
202 bool Instruction::hasNoNaNs() const {
|
77
|
203 assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
204 return cast<FPMathOperator>(this)->hasNoNaNs();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
205 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
206
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
207 bool Instruction::hasNoInfs() const {
|
77
|
208 assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
209 return cast<FPMathOperator>(this)->hasNoInfs();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
210 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
211
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
212 bool Instruction::hasNoSignedZeros() const {
|
77
|
213 assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
214 return cast<FPMathOperator>(this)->hasNoSignedZeros();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
215 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
216
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
217 bool Instruction::hasAllowReciprocal() const {
|
77
|
218 assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
219 return cast<FPMathOperator>(this)->hasAllowReciprocal();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
220 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
221
|
121
|
222 bool Instruction::hasAllowContract() const {
|
|
223 assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
|
|
224 return cast<FPMathOperator>(this)->hasAllowContract();
|
|
225 }
|
|
226
|
134
|
227 bool Instruction::hasApproxFunc() const {
|
|
228 assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
|
|
229 return cast<FPMathOperator>(this)->hasApproxFunc();
|
|
230 }
|
|
231
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
232 FastMathFlags Instruction::getFastMathFlags() const {
|
77
|
233 assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
234 return cast<FPMathOperator>(this)->getFastMathFlags();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
235 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
236
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
237 void Instruction::copyFastMathFlags(const Instruction *I) {
|
77
|
238 copyFastMathFlags(I->getFastMathFlags());
|
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
|
121
|
241 void Instruction::copyIRFlags(const Value *V, bool IncludeWrapFlags) {
|
120
|
242 // Copy the wrapping flags.
|
121
|
243 if (IncludeWrapFlags && isa<OverflowingBinaryOperator>(this)) {
|
|
244 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
|
120
|
245 setHasNoSignedWrap(OB->hasNoSignedWrap());
|
|
246 setHasNoUnsignedWrap(OB->hasNoUnsignedWrap());
|
|
247 }
|
|
248 }
|
|
249
|
|
250 // Copy the exact flag.
|
|
251 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
|
|
252 if (isa<PossiblyExactOperator>(this))
|
|
253 setIsExact(PE->isExact());
|
|
254
|
|
255 // Copy the fast-math flags.
|
|
256 if (auto *FP = dyn_cast<FPMathOperator>(V))
|
|
257 if (isa<FPMathOperator>(this))
|
|
258 copyFastMathFlags(FP->getFastMathFlags());
|
|
259
|
|
260 if (auto *SrcGEP = dyn_cast<GetElementPtrInst>(V))
|
|
261 if (auto *DestGEP = dyn_cast<GetElementPtrInst>(this))
|
|
262 DestGEP->setIsInBounds(SrcGEP->isInBounds() | DestGEP->isInBounds());
|
|
263 }
|
|
264
|
|
265 void Instruction::andIRFlags(const Value *V) {
|
|
266 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
|
|
267 if (isa<OverflowingBinaryOperator>(this)) {
|
|
268 setHasNoSignedWrap(hasNoSignedWrap() & OB->hasNoSignedWrap());
|
|
269 setHasNoUnsignedWrap(hasNoUnsignedWrap() & OB->hasNoUnsignedWrap());
|
|
270 }
|
|
271 }
|
|
272
|
|
273 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
|
|
274 if (isa<PossiblyExactOperator>(this))
|
|
275 setIsExact(isExact() & PE->isExact());
|
|
276
|
|
277 if (auto *FP = dyn_cast<FPMathOperator>(V)) {
|
|
278 if (isa<FPMathOperator>(this)) {
|
|
279 FastMathFlags FM = getFastMathFlags();
|
|
280 FM &= FP->getFastMathFlags();
|
|
281 copyFastMathFlags(FM);
|
|
282 }
|
|
283 }
|
|
284
|
|
285 if (auto *SrcGEP = dyn_cast<GetElementPtrInst>(V))
|
|
286 if (auto *DestGEP = dyn_cast<GetElementPtrInst>(this))
|
|
287 DestGEP->setIsInBounds(SrcGEP->isInBounds() & DestGEP->isInBounds());
|
|
288 }
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
289
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
290 const char *Instruction::getOpcodeName(unsigned OpCode) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
291 switch (OpCode) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
292 // Terminators
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
293 case Ret: return "ret";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
294 case Br: return "br";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
295 case Switch: return "switch";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
296 case IndirectBr: return "indirectbr";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
297 case Invoke: return "invoke";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
298 case Resume: return "resume";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
299 case Unreachable: return "unreachable";
|
95
|
300 case CleanupRet: return "cleanupret";
|
|
301 case CatchRet: return "catchret";
|
|
302 case CatchPad: return "catchpad";
|
100
|
303 case CatchSwitch: return "catchswitch";
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
304
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
305 // Standard binary operators...
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
306 case Add: return "add";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
307 case FAdd: return "fadd";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
308 case Sub: return "sub";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
309 case FSub: return "fsub";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
310 case Mul: return "mul";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
311 case FMul: return "fmul";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
312 case UDiv: return "udiv";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
313 case SDiv: return "sdiv";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
314 case FDiv: return "fdiv";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
315 case URem: return "urem";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
316 case SRem: return "srem";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
317 case FRem: return "frem";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
318
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
319 // Logical operators...
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
320 case And: return "and";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
321 case Or : return "or";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
322 case Xor: return "xor";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
323
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
324 // Memory instructions...
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
325 case Alloca: return "alloca";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
326 case Load: return "load";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
327 case Store: return "store";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
328 case AtomicCmpXchg: return "cmpxchg";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
329 case AtomicRMW: return "atomicrmw";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
330 case Fence: return "fence";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
331 case GetElementPtr: return "getelementptr";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
332
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
333 // Convert instructions...
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
334 case Trunc: return "trunc";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
335 case ZExt: return "zext";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
336 case SExt: return "sext";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
337 case FPTrunc: return "fptrunc";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
338 case FPExt: return "fpext";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
339 case FPToUI: return "fptoui";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
340 case FPToSI: return "fptosi";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
341 case UIToFP: return "uitofp";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
342 case SIToFP: return "sitofp";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
343 case IntToPtr: return "inttoptr";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
344 case PtrToInt: return "ptrtoint";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
345 case BitCast: return "bitcast";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
346 case AddrSpaceCast: return "addrspacecast";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
347
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
348 // Other instructions...
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
349 case ICmp: return "icmp";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
350 case FCmp: return "fcmp";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
351 case PHI: return "phi";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
352 case Select: return "select";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
353 case Call: return "call";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
354 case Shl: return "shl";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
355 case LShr: return "lshr";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
356 case AShr: return "ashr";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
357 case VAArg: return "va_arg";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
358 case ExtractElement: return "extractelement";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
359 case InsertElement: return "insertelement";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
360 case ShuffleVector: return "shufflevector";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
361 case ExtractValue: return "extractvalue";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
362 case InsertValue: return "insertvalue";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
363 case LandingPad: return "landingpad";
|
95
|
364 case CleanupPad: return "cleanuppad";
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
365
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
366 default: return "<Invalid operator> ";
|
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
|
120
|
370 /// Return true if both instructions have the same special state. This must be
|
|
371 /// kept in sync with FunctionComparator::cmpOperations in
|
|
372 /// lib/Transforms/IPO/MergeFunctions.cpp.
|
77
|
373 static bool haveSameSpecialState(const Instruction *I1, const Instruction *I2,
|
|
374 bool IgnoreAlignment = false) {
|
|
375 assert(I1->getOpcode() == I2->getOpcode() &&
|
|
376 "Can not compare special state of different instructions");
|
|
377
|
120
|
378 if (const AllocaInst *AI = dyn_cast<AllocaInst>(I1))
|
|
379 return AI->getAllocatedType() == cast<AllocaInst>(I2)->getAllocatedType() &&
|
|
380 (AI->getAlignment() == cast<AllocaInst>(I2)->getAlignment() ||
|
|
381 IgnoreAlignment);
|
77
|
382 if (const LoadInst *LI = dyn_cast<LoadInst>(I1))
|
|
383 return LI->isVolatile() == cast<LoadInst>(I2)->isVolatile() &&
|
|
384 (LI->getAlignment() == cast<LoadInst>(I2)->getAlignment() ||
|
|
385 IgnoreAlignment) &&
|
|
386 LI->getOrdering() == cast<LoadInst>(I2)->getOrdering() &&
|
121
|
387 LI->getSyncScopeID() == cast<LoadInst>(I2)->getSyncScopeID();
|
77
|
388 if (const StoreInst *SI = dyn_cast<StoreInst>(I1))
|
|
389 return SI->isVolatile() == cast<StoreInst>(I2)->isVolatile() &&
|
|
390 (SI->getAlignment() == cast<StoreInst>(I2)->getAlignment() ||
|
|
391 IgnoreAlignment) &&
|
|
392 SI->getOrdering() == cast<StoreInst>(I2)->getOrdering() &&
|
121
|
393 SI->getSyncScopeID() == cast<StoreInst>(I2)->getSyncScopeID();
|
77
|
394 if (const CmpInst *CI = dyn_cast<CmpInst>(I1))
|
|
395 return CI->getPredicate() == cast<CmpInst>(I2)->getPredicate();
|
|
396 if (const CallInst *CI = dyn_cast<CallInst>(I1))
|
|
397 return CI->isTailCall() == cast<CallInst>(I2)->isTailCall() &&
|
|
398 CI->getCallingConv() == cast<CallInst>(I2)->getCallingConv() &&
|
100
|
399 CI->getAttributes() == cast<CallInst>(I2)->getAttributes() &&
|
|
400 CI->hasIdenticalOperandBundleSchema(*cast<CallInst>(I2));
|
77
|
401 if (const InvokeInst *CI = dyn_cast<InvokeInst>(I1))
|
|
402 return CI->getCallingConv() == cast<InvokeInst>(I2)->getCallingConv() &&
|
100
|
403 CI->getAttributes() == cast<InvokeInst>(I2)->getAttributes() &&
|
|
404 CI->hasIdenticalOperandBundleSchema(*cast<InvokeInst>(I2));
|
77
|
405 if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(I1))
|
|
406 return IVI->getIndices() == cast<InsertValueInst>(I2)->getIndices();
|
|
407 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I1))
|
|
408 return EVI->getIndices() == cast<ExtractValueInst>(I2)->getIndices();
|
|
409 if (const FenceInst *FI = dyn_cast<FenceInst>(I1))
|
|
410 return FI->getOrdering() == cast<FenceInst>(I2)->getOrdering() &&
|
121
|
411 FI->getSyncScopeID() == cast<FenceInst>(I2)->getSyncScopeID();
|
77
|
412 if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(I1))
|
|
413 return CXI->isVolatile() == cast<AtomicCmpXchgInst>(I2)->isVolatile() &&
|
|
414 CXI->isWeak() == cast<AtomicCmpXchgInst>(I2)->isWeak() &&
|
|
415 CXI->getSuccessOrdering() ==
|
|
416 cast<AtomicCmpXchgInst>(I2)->getSuccessOrdering() &&
|
|
417 CXI->getFailureOrdering() ==
|
|
418 cast<AtomicCmpXchgInst>(I2)->getFailureOrdering() &&
|
121
|
419 CXI->getSyncScopeID() ==
|
|
420 cast<AtomicCmpXchgInst>(I2)->getSyncScopeID();
|
77
|
421 if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(I1))
|
|
422 return RMWI->getOperation() == cast<AtomicRMWInst>(I2)->getOperation() &&
|
|
423 RMWI->isVolatile() == cast<AtomicRMWInst>(I2)->isVolatile() &&
|
|
424 RMWI->getOrdering() == cast<AtomicRMWInst>(I2)->getOrdering() &&
|
121
|
425 RMWI->getSyncScopeID() == cast<AtomicRMWInst>(I2)->getSyncScopeID();
|
77
|
426
|
|
427 return true;
|
|
428 }
|
|
429
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
430 bool Instruction::isIdenticalTo(const Instruction *I) const {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
431 return isIdenticalToWhenDefined(I) &&
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
432 SubclassOptionalData == I->SubclassOptionalData;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
433 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
434
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
435 bool Instruction::isIdenticalToWhenDefined(const Instruction *I) const {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
436 if (getOpcode() != I->getOpcode() ||
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
437 getNumOperands() != I->getNumOperands() ||
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
438 getType() != I->getType())
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
439 return false;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
440
|
77
|
441 // If both instructions have no operands, they are identical.
|
|
442 if (getNumOperands() == 0 && I->getNumOperands() == 0)
|
|
443 return haveSameSpecialState(this, I);
|
|
444
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
445 // We have two instructions of identical opcode and #operands. Check to see
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
446 // if all operands are the same.
|
77
|
447 if (!std::equal(op_begin(), op_end(), I->op_begin()))
|
|
448 return false;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
449
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
450 if (const PHINode *thisPHI = dyn_cast<PHINode>(this)) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
451 const PHINode *otherPHI = cast<PHINode>(I);
|
77
|
452 return std::equal(thisPHI->block_begin(), thisPHI->block_end(),
|
|
453 otherPHI->block_begin());
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
454 }
|
77
|
455
|
|
456 return haveSameSpecialState(this, I);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
457 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
458
|
120
|
459 // Keep this in sync with FunctionComparator::cmpOperations in
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
460 // lib/Transforms/IPO/MergeFunctions.cpp.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
461 bool Instruction::isSameOperationAs(const Instruction *I,
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
462 unsigned flags) const {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
463 bool IgnoreAlignment = flags & CompareIgnoringAlignment;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
464 bool UseScalarTypes = flags & CompareUsingScalarTypes;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
465
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
466 if (getOpcode() != I->getOpcode() ||
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
467 getNumOperands() != I->getNumOperands() ||
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
468 (UseScalarTypes ?
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
469 getType()->getScalarType() != I->getType()->getScalarType() :
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
470 getType() != I->getType()))
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
471 return false;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
472
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
473 // We have two instructions of identical opcode and #operands. Check to see
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
474 // if all operands are the same type
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
475 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
476 if (UseScalarTypes ?
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
477 getOperand(i)->getType()->getScalarType() !=
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
478 I->getOperand(i)->getType()->getScalarType() :
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
479 getOperand(i)->getType() != I->getOperand(i)->getType())
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
480 return false;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
481
|
77
|
482 return haveSameSpecialState(this, I, IgnoreAlignment);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
483 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
484
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
485 bool Instruction::isUsedOutsideOfBlock(const BasicBlock *BB) const {
|
77
|
486 for (const Use &U : uses()) {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
487 // PHI nodes uses values in the corresponding predecessor block. For other
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
488 // instructions, just check to see whether the parent of the use matches up.
|
77
|
489 const Instruction *I = cast<Instruction>(U.getUser());
|
|
490 const PHINode *PN = dyn_cast<PHINode>(I);
|
|
491 if (!PN) {
|
|
492 if (I->getParent() != BB)
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
493 return true;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
494 continue;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
495 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
496
|
77
|
497 if (PN->getIncomingBlock(U) != BB)
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
498 return true;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
499 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
500 return false;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
501 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
502
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
503 bool Instruction::mayReadFromMemory() const {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
504 switch (getOpcode()) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
505 default: return false;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
506 case Instruction::VAArg:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
507 case Instruction::Load:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
508 case Instruction::Fence: // FIXME: refine definition of mayReadFromMemory
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
509 case Instruction::AtomicCmpXchg:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
510 case Instruction::AtomicRMW:
|
95
|
511 case Instruction::CatchPad:
|
|
512 case Instruction::CatchRet:
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
513 return true;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
514 case Instruction::Call:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
515 return !cast<CallInst>(this)->doesNotAccessMemory();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
516 case Instruction::Invoke:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
517 return !cast<InvokeInst>(this)->doesNotAccessMemory();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
518 case Instruction::Store:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
519 return !cast<StoreInst>(this)->isUnordered();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
520 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
521 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
522
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
523 bool Instruction::mayWriteToMemory() const {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
524 switch (getOpcode()) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
525 default: return false;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
526 case Instruction::Fence: // FIXME: refine definition of mayWriteToMemory
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
527 case Instruction::Store:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
528 case Instruction::VAArg:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
529 case Instruction::AtomicCmpXchg:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
530 case Instruction::AtomicRMW:
|
95
|
531 case Instruction::CatchPad:
|
|
532 case Instruction::CatchRet:
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
533 return true;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
534 case Instruction::Call:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
535 return !cast<CallInst>(this)->onlyReadsMemory();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
536 case Instruction::Invoke:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
537 return !cast<InvokeInst>(this)->onlyReadsMemory();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
538 case Instruction::Load:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
539 return !cast<LoadInst>(this)->isUnordered();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
540 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
541 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
542
|
77
|
543 bool Instruction::isAtomic() const {
|
|
544 switch (getOpcode()) {
|
|
545 default:
|
|
546 return false;
|
|
547 case Instruction::AtomicCmpXchg:
|
|
548 case Instruction::AtomicRMW:
|
|
549 case Instruction::Fence:
|
|
550 return true;
|
|
551 case Instruction::Load:
|
120
|
552 return cast<LoadInst>(this)->getOrdering() != AtomicOrdering::NotAtomic;
|
77
|
553 case Instruction::Store:
|
120
|
554 return cast<StoreInst>(this)->getOrdering() != AtomicOrdering::NotAtomic;
|
77
|
555 }
|
|
556 }
|
|
557
|
121
|
558 bool Instruction::hasAtomicLoad() const {
|
|
559 assert(isAtomic());
|
|
560 switch (getOpcode()) {
|
|
561 default:
|
|
562 return false;
|
|
563 case Instruction::AtomicCmpXchg:
|
|
564 case Instruction::AtomicRMW:
|
|
565 case Instruction::Load:
|
|
566 return true;
|
|
567 }
|
|
568 }
|
|
569
|
|
570 bool Instruction::hasAtomicStore() const {
|
|
571 assert(isAtomic());
|
|
572 switch (getOpcode()) {
|
|
573 default:
|
|
574 return false;
|
|
575 case Instruction::AtomicCmpXchg:
|
|
576 case Instruction::AtomicRMW:
|
|
577 case Instruction::Store:
|
|
578 return true;
|
|
579 }
|
|
580 }
|
|
581
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
582 bool Instruction::mayThrow() const {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
583 if (const CallInst *CI = dyn_cast<CallInst>(this))
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
584 return !CI->doesNotThrow();
|
95
|
585 if (const auto *CRI = dyn_cast<CleanupReturnInst>(this))
|
|
586 return CRI->unwindsToCaller();
|
100
|
587 if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(this))
|
|
588 return CatchSwitch->unwindsToCaller();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
589 return isa<ResumeInst>(this);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
590 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
591
|
134
|
592 bool Instruction::isSafeToRemove() const {
|
|
593 return (!isa<CallInst>(this) || !this->mayHaveSideEffects()) &&
|
|
594 !isa<TerminatorInst>(this);
|
|
595 }
|
|
596
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
597 bool Instruction::isAssociative() const {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
598 unsigned Opcode = getOpcode();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
599 if (isAssociative(Opcode))
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
600 return true;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
601
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
602 switch (Opcode) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
603 case FMul:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
604 case FAdd:
|
134
|
605 return cast<FPMathOperator>(this)->isFast();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
606 default:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
607 return false;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
608 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
609 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
610
|
95
|
611 Instruction *Instruction::cloneImpl() const {
|
|
612 llvm_unreachable("Subclass of Instruction failed to implement cloneImpl");
|
|
613 }
|
|
614
|
120
|
615 void Instruction::swapProfMetadata() {
|
|
616 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
|
|
617 if (!ProfileData || ProfileData->getNumOperands() != 3 ||
|
|
618 !isa<MDString>(ProfileData->getOperand(0)))
|
|
619 return;
|
|
620
|
|
621 MDString *MDName = cast<MDString>(ProfileData->getOperand(0));
|
|
622 if (MDName->getString() != "branch_weights")
|
|
623 return;
|
|
624
|
|
625 // The first operand is the name. Fetch them backwards and build a new one.
|
|
626 Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2),
|
|
627 ProfileData->getOperand(1)};
|
|
628 setMetadata(LLVMContext::MD_prof,
|
|
629 MDNode::get(ProfileData->getContext(), Ops));
|
|
630 }
|
|
631
|
|
632 void Instruction::copyMetadata(const Instruction &SrcInst,
|
|
633 ArrayRef<unsigned> WL) {
|
|
634 if (!SrcInst.hasMetadata())
|
|
635 return;
|
|
636
|
|
637 DenseSet<unsigned> WLS;
|
|
638 for (unsigned M : WL)
|
|
639 WLS.insert(M);
|
|
640
|
|
641 // Otherwise, enumerate and copy over metadata from the old instruction to the
|
|
642 // new one.
|
|
643 SmallVector<std::pair<unsigned, MDNode *>, 4> TheMDs;
|
|
644 SrcInst.getAllMetadataOtherThanDebugLoc(TheMDs);
|
|
645 for (const auto &MD : TheMDs) {
|
|
646 if (WL.empty() || WLS.count(MD.first))
|
|
647 setMetadata(MD.first, MD.second);
|
|
648 }
|
|
649 if (WL.empty() || WLS.count(LLVMContext::MD_dbg))
|
|
650 setDebugLoc(SrcInst.getDebugLoc());
|
|
651 }
|
|
652
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
653 Instruction *Instruction::clone() const {
|
95
|
654 Instruction *New = nullptr;
|
|
655 switch (getOpcode()) {
|
|
656 default:
|
|
657 llvm_unreachable("Unhandled Opcode.");
|
|
658 #define HANDLE_INST(num, opc, clas) \
|
|
659 case Instruction::opc: \
|
|
660 New = cast<clas>(this)->cloneImpl(); \
|
|
661 break;
|
|
662 #include "llvm/IR/Instruction.def"
|
|
663 #undef HANDLE_INST
|
|
664 }
|
|
665
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
666 New->SubclassOptionalData = SubclassOptionalData;
|
120
|
667 New->copyMetadata(*this);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
668 return New;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
669 }
|
121
|
670
|
|
671 void Instruction::updateProfWeight(uint64_t S, uint64_t T) {
|
|
672 auto *ProfileData = getMetadata(LLVMContext::MD_prof);
|
|
673 if (ProfileData == nullptr)
|
|
674 return;
|
|
675
|
|
676 auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0));
|
|
677 if (!ProfDataName || (!ProfDataName->getString().equals("branch_weights") &&
|
|
678 !ProfDataName->getString().equals("VP")))
|
|
679 return;
|
|
680
|
|
681 MDBuilder MDB(getContext());
|
|
682 SmallVector<Metadata *, 3> Vals;
|
|
683 Vals.push_back(ProfileData->getOperand(0));
|
|
684 APInt APS(128, S), APT(128, T);
|
|
685 if (ProfDataName->getString().equals("branch_weights"))
|
|
686 for (unsigned i = 1; i < ProfileData->getNumOperands(); i++) {
|
|
687 // Using APInt::div may be expensive, but most cases should fit 64 bits.
|
|
688 APInt Val(128,
|
|
689 mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(i))
|
|
690 ->getValue()
|
|
691 .getZExtValue());
|
|
692 Val *= APS;
|
|
693 Vals.push_back(MDB.createConstant(
|
|
694 ConstantInt::get(Type::getInt64Ty(getContext()),
|
|
695 Val.udiv(APT).getLimitedValue())));
|
|
696 }
|
|
697 else if (ProfDataName->getString().equals("VP"))
|
|
698 for (unsigned i = 1; i < ProfileData->getNumOperands(); i += 2) {
|
|
699 // The first value is the key of the value profile, which will not change.
|
|
700 Vals.push_back(ProfileData->getOperand(i));
|
|
701 // Using APInt::div may be expensive, but most cases should fit 64 bits.
|
|
702 APInt Val(128,
|
|
703 mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(i + 1))
|
|
704 ->getValue()
|
|
705 .getZExtValue());
|
|
706 Val *= APS;
|
|
707 Vals.push_back(MDB.createConstant(
|
|
708 ConstantInt::get(Type::getInt64Ty(getContext()),
|
|
709 Val.udiv(APT).getLimitedValue())));
|
|
710 }
|
|
711 setMetadata(LLVMContext::MD_prof, MDNode::get(getContext(), Vals));
|
|
712 }
|
|
713
|
|
714 void Instruction::setProfWeight(uint64_t W) {
|
|
715 assert((isa<CallInst>(this) || isa<InvokeInst>(this)) &&
|
|
716 "Can only set weights for call and invoke instrucitons");
|
|
717 SmallVector<uint32_t, 1> Weights;
|
|
718 Weights.push_back(W);
|
|
719 MDBuilder MDB(getContext());
|
|
720 setMetadata(LLVMContext::MD_prof, MDB.createBranchWeights(Weights));
|
|
721 }
|