1*9880d681SAndroid Build Coastguard Worker //===-- Instruction.cpp - Implement the Instruction class -----------------===//
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 Instruction 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/Instruction.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/CallSite.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Constants.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instructions.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Module.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Operator.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Type.h"
21*9880d681SAndroid Build Coastguard Worker using namespace llvm;
22*9880d681SAndroid Build Coastguard Worker
Instruction(Type * ty,unsigned it,Use * Ops,unsigned NumOps,Instruction * InsertBefore)23*9880d681SAndroid Build Coastguard Worker Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,
24*9880d681SAndroid Build Coastguard Worker Instruction *InsertBefore)
25*9880d681SAndroid Build Coastguard Worker : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) {
26*9880d681SAndroid Build Coastguard Worker
27*9880d681SAndroid Build Coastguard Worker // If requested, insert this instruction into a basic block...
28*9880d681SAndroid Build Coastguard Worker if (InsertBefore) {
29*9880d681SAndroid Build Coastguard Worker BasicBlock *BB = InsertBefore->getParent();
30*9880d681SAndroid Build Coastguard Worker assert(BB && "Instruction to insert before is not in a basic block!");
31*9880d681SAndroid Build Coastguard Worker BB->getInstList().insert(InsertBefore->getIterator(), this);
32*9880d681SAndroid Build Coastguard Worker }
33*9880d681SAndroid Build Coastguard Worker }
34*9880d681SAndroid Build Coastguard Worker
Instruction(Type * ty,unsigned it,Use * Ops,unsigned NumOps,BasicBlock * InsertAtEnd)35*9880d681SAndroid Build Coastguard Worker Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,
36*9880d681SAndroid Build Coastguard Worker BasicBlock *InsertAtEnd)
37*9880d681SAndroid Build Coastguard Worker : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) {
38*9880d681SAndroid Build Coastguard Worker
39*9880d681SAndroid Build Coastguard Worker // append this instruction into the basic block
40*9880d681SAndroid Build Coastguard Worker assert(InsertAtEnd && "Basic block to append to may not be NULL!");
41*9880d681SAndroid Build Coastguard Worker InsertAtEnd->getInstList().push_back(this);
42*9880d681SAndroid Build Coastguard Worker }
43*9880d681SAndroid Build Coastguard Worker
44*9880d681SAndroid Build Coastguard Worker
45*9880d681SAndroid Build Coastguard Worker // Out of line virtual method, so the vtable, etc has a home.
~Instruction()46*9880d681SAndroid Build Coastguard Worker Instruction::~Instruction() {
47*9880d681SAndroid Build Coastguard Worker assert(!Parent && "Instruction still linked in the program!");
48*9880d681SAndroid Build Coastguard Worker if (hasMetadataHashEntry())
49*9880d681SAndroid Build Coastguard Worker clearMetadataHashEntries();
50*9880d681SAndroid Build Coastguard Worker }
51*9880d681SAndroid Build Coastguard Worker
52*9880d681SAndroid Build Coastguard Worker
setParent(BasicBlock * P)53*9880d681SAndroid Build Coastguard Worker void Instruction::setParent(BasicBlock *P) {
54*9880d681SAndroid Build Coastguard Worker Parent = P;
55*9880d681SAndroid Build Coastguard Worker }
56*9880d681SAndroid Build Coastguard Worker
getModule() const57*9880d681SAndroid Build Coastguard Worker const Module *Instruction::getModule() const {
58*9880d681SAndroid Build Coastguard Worker return getParent()->getModule();
59*9880d681SAndroid Build Coastguard Worker }
60*9880d681SAndroid Build Coastguard Worker
getModule()61*9880d681SAndroid Build Coastguard Worker Module *Instruction::getModule() {
62*9880d681SAndroid Build Coastguard Worker return getParent()->getModule();
63*9880d681SAndroid Build Coastguard Worker }
64*9880d681SAndroid Build Coastguard Worker
getFunction()65*9880d681SAndroid Build Coastguard Worker Function *Instruction::getFunction() { return getParent()->getParent(); }
66*9880d681SAndroid Build Coastguard Worker
getFunction() const67*9880d681SAndroid Build Coastguard Worker const Function *Instruction::getFunction() const {
68*9880d681SAndroid Build Coastguard Worker return getParent()->getParent();
69*9880d681SAndroid Build Coastguard Worker }
70*9880d681SAndroid Build Coastguard Worker
removeFromParent()71*9880d681SAndroid Build Coastguard Worker void Instruction::removeFromParent() {
72*9880d681SAndroid Build Coastguard Worker getParent()->getInstList().remove(getIterator());
73*9880d681SAndroid Build Coastguard Worker }
74*9880d681SAndroid Build Coastguard Worker
eraseFromParent()75*9880d681SAndroid Build Coastguard Worker iplist<Instruction>::iterator Instruction::eraseFromParent() {
76*9880d681SAndroid Build Coastguard Worker return getParent()->getInstList().erase(getIterator());
77*9880d681SAndroid Build Coastguard Worker }
78*9880d681SAndroid Build Coastguard Worker
79*9880d681SAndroid Build Coastguard Worker /// Insert an unlinked instruction into a basic block immediately before the
80*9880d681SAndroid Build Coastguard Worker /// specified instruction.
insertBefore(Instruction * InsertPos)81*9880d681SAndroid Build Coastguard Worker void Instruction::insertBefore(Instruction *InsertPos) {
82*9880d681SAndroid Build Coastguard Worker InsertPos->getParent()->getInstList().insert(InsertPos->getIterator(), this);
83*9880d681SAndroid Build Coastguard Worker }
84*9880d681SAndroid Build Coastguard Worker
85*9880d681SAndroid Build Coastguard Worker /// Insert an unlinked instruction into a basic block immediately after the
86*9880d681SAndroid Build Coastguard Worker /// specified instruction.
insertAfter(Instruction * InsertPos)87*9880d681SAndroid Build Coastguard Worker void Instruction::insertAfter(Instruction *InsertPos) {
88*9880d681SAndroid Build Coastguard Worker InsertPos->getParent()->getInstList().insertAfter(InsertPos->getIterator(),
89*9880d681SAndroid Build Coastguard Worker this);
90*9880d681SAndroid Build Coastguard Worker }
91*9880d681SAndroid Build Coastguard Worker
92*9880d681SAndroid Build Coastguard Worker /// Unlink this instruction from its current basic block and insert it into the
93*9880d681SAndroid Build Coastguard Worker /// basic block that MovePos lives in, right before MovePos.
moveBefore(Instruction * MovePos)94*9880d681SAndroid Build Coastguard Worker void Instruction::moveBefore(Instruction *MovePos) {
95*9880d681SAndroid Build Coastguard Worker MovePos->getParent()->getInstList().splice(
96*9880d681SAndroid Build Coastguard Worker MovePos->getIterator(), getParent()->getInstList(), getIterator());
97*9880d681SAndroid Build Coastguard Worker }
98*9880d681SAndroid Build Coastguard Worker
setHasNoUnsignedWrap(bool b)99*9880d681SAndroid Build Coastguard Worker void Instruction::setHasNoUnsignedWrap(bool b) {
100*9880d681SAndroid Build Coastguard Worker cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
101*9880d681SAndroid Build Coastguard Worker }
102*9880d681SAndroid Build Coastguard Worker
setHasNoSignedWrap(bool b)103*9880d681SAndroid Build Coastguard Worker void Instruction::setHasNoSignedWrap(bool b) {
104*9880d681SAndroid Build Coastguard Worker cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
105*9880d681SAndroid Build Coastguard Worker }
106*9880d681SAndroid Build Coastguard Worker
setIsExact(bool b)107*9880d681SAndroid Build Coastguard Worker void Instruction::setIsExact(bool b) {
108*9880d681SAndroid Build Coastguard Worker cast<PossiblyExactOperator>(this)->setIsExact(b);
109*9880d681SAndroid Build Coastguard Worker }
110*9880d681SAndroid Build Coastguard Worker
hasNoUnsignedWrap() const111*9880d681SAndroid Build Coastguard Worker bool Instruction::hasNoUnsignedWrap() const {
112*9880d681SAndroid Build Coastguard Worker return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
113*9880d681SAndroid Build Coastguard Worker }
114*9880d681SAndroid Build Coastguard Worker
hasNoSignedWrap() const115*9880d681SAndroid Build Coastguard Worker bool Instruction::hasNoSignedWrap() const {
116*9880d681SAndroid Build Coastguard Worker return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
117*9880d681SAndroid Build Coastguard Worker }
118*9880d681SAndroid Build Coastguard Worker
isExact() const119*9880d681SAndroid Build Coastguard Worker bool Instruction::isExact() const {
120*9880d681SAndroid Build Coastguard Worker return cast<PossiblyExactOperator>(this)->isExact();
121*9880d681SAndroid Build Coastguard Worker }
122*9880d681SAndroid Build Coastguard Worker
123*9880d681SAndroid Build Coastguard Worker /// Set or clear the unsafe-algebra flag on this instruction, which must be an
124*9880d681SAndroid Build Coastguard Worker /// operator which supports this flag. See LangRef.html for the meaning of this
125*9880d681SAndroid Build Coastguard Worker /// flag.
setHasUnsafeAlgebra(bool B)126*9880d681SAndroid Build Coastguard Worker void Instruction::setHasUnsafeAlgebra(bool B) {
127*9880d681SAndroid Build Coastguard Worker assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
128*9880d681SAndroid Build Coastguard Worker cast<FPMathOperator>(this)->setHasUnsafeAlgebra(B);
129*9880d681SAndroid Build Coastguard Worker }
130*9880d681SAndroid Build Coastguard Worker
131*9880d681SAndroid Build Coastguard Worker /// Set or clear the NoNaNs flag on this instruction, which must be an operator
132*9880d681SAndroid Build Coastguard Worker /// which supports this flag. See LangRef.html for the meaning of this flag.
setHasNoNaNs(bool B)133*9880d681SAndroid Build Coastguard Worker void Instruction::setHasNoNaNs(bool B) {
134*9880d681SAndroid Build Coastguard Worker assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
135*9880d681SAndroid Build Coastguard Worker cast<FPMathOperator>(this)->setHasNoNaNs(B);
136*9880d681SAndroid Build Coastguard Worker }
137*9880d681SAndroid Build Coastguard Worker
138*9880d681SAndroid Build Coastguard Worker /// Set or clear the no-infs flag on this instruction, which must be an operator
139*9880d681SAndroid Build Coastguard Worker /// which supports this flag. See LangRef.html for the meaning of this flag.
setHasNoInfs(bool B)140*9880d681SAndroid Build Coastguard Worker void Instruction::setHasNoInfs(bool B) {
141*9880d681SAndroid Build Coastguard Worker assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
142*9880d681SAndroid Build Coastguard Worker cast<FPMathOperator>(this)->setHasNoInfs(B);
143*9880d681SAndroid Build Coastguard Worker }
144*9880d681SAndroid Build Coastguard Worker
145*9880d681SAndroid Build Coastguard Worker /// Set or clear the no-signed-zeros flag on this instruction, which must be an
146*9880d681SAndroid Build Coastguard Worker /// operator which supports this flag. See LangRef.html for the meaning of this
147*9880d681SAndroid Build Coastguard Worker /// flag.
setHasNoSignedZeros(bool B)148*9880d681SAndroid Build Coastguard Worker void Instruction::setHasNoSignedZeros(bool B) {
149*9880d681SAndroid Build Coastguard Worker assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
150*9880d681SAndroid Build Coastguard Worker cast<FPMathOperator>(this)->setHasNoSignedZeros(B);
151*9880d681SAndroid Build Coastguard Worker }
152*9880d681SAndroid Build Coastguard Worker
153*9880d681SAndroid Build Coastguard Worker /// Set or clear the allow-reciprocal flag on this instruction, which must be an
154*9880d681SAndroid Build Coastguard Worker /// operator which supports this flag. See LangRef.html for the meaning of this
155*9880d681SAndroid Build Coastguard Worker /// flag.
setHasAllowReciprocal(bool B)156*9880d681SAndroid Build Coastguard Worker void Instruction::setHasAllowReciprocal(bool B) {
157*9880d681SAndroid Build Coastguard Worker assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
158*9880d681SAndroid Build Coastguard Worker cast<FPMathOperator>(this)->setHasAllowReciprocal(B);
159*9880d681SAndroid Build Coastguard Worker }
160*9880d681SAndroid Build Coastguard Worker
161*9880d681SAndroid Build Coastguard Worker /// Convenience function for setting all the fast-math flags on this
162*9880d681SAndroid Build Coastguard Worker /// instruction, which must be an operator which supports these flags. See
163*9880d681SAndroid Build Coastguard Worker /// LangRef.html for the meaning of these flats.
setFastMathFlags(FastMathFlags FMF)164*9880d681SAndroid Build Coastguard Worker void Instruction::setFastMathFlags(FastMathFlags FMF) {
165*9880d681SAndroid Build Coastguard Worker assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
166*9880d681SAndroid Build Coastguard Worker cast<FPMathOperator>(this)->setFastMathFlags(FMF);
167*9880d681SAndroid Build Coastguard Worker }
168*9880d681SAndroid Build Coastguard Worker
copyFastMathFlags(FastMathFlags FMF)169*9880d681SAndroid Build Coastguard Worker void Instruction::copyFastMathFlags(FastMathFlags FMF) {
170*9880d681SAndroid Build Coastguard Worker assert(isa<FPMathOperator>(this) && "copying fast-math flag on invalid op");
171*9880d681SAndroid Build Coastguard Worker cast<FPMathOperator>(this)->copyFastMathFlags(FMF);
172*9880d681SAndroid Build Coastguard Worker }
173*9880d681SAndroid Build Coastguard Worker
174*9880d681SAndroid Build Coastguard Worker /// Determine whether the unsafe-algebra flag is set.
hasUnsafeAlgebra() const175*9880d681SAndroid Build Coastguard Worker bool Instruction::hasUnsafeAlgebra() const {
176*9880d681SAndroid Build Coastguard Worker assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
177*9880d681SAndroid Build Coastguard Worker return cast<FPMathOperator>(this)->hasUnsafeAlgebra();
178*9880d681SAndroid Build Coastguard Worker }
179*9880d681SAndroid Build Coastguard Worker
180*9880d681SAndroid Build Coastguard Worker /// Determine whether the no-NaNs flag is set.
hasNoNaNs() const181*9880d681SAndroid Build Coastguard Worker bool Instruction::hasNoNaNs() const {
182*9880d681SAndroid Build Coastguard Worker assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
183*9880d681SAndroid Build Coastguard Worker return cast<FPMathOperator>(this)->hasNoNaNs();
184*9880d681SAndroid Build Coastguard Worker }
185*9880d681SAndroid Build Coastguard Worker
186*9880d681SAndroid Build Coastguard Worker /// Determine whether the no-infs flag is set.
hasNoInfs() const187*9880d681SAndroid Build Coastguard Worker bool Instruction::hasNoInfs() const {
188*9880d681SAndroid Build Coastguard Worker assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
189*9880d681SAndroid Build Coastguard Worker return cast<FPMathOperator>(this)->hasNoInfs();
190*9880d681SAndroid Build Coastguard Worker }
191*9880d681SAndroid Build Coastguard Worker
192*9880d681SAndroid Build Coastguard Worker /// Determine whether the no-signed-zeros flag is set.
hasNoSignedZeros() const193*9880d681SAndroid Build Coastguard Worker bool Instruction::hasNoSignedZeros() const {
194*9880d681SAndroid Build Coastguard Worker assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
195*9880d681SAndroid Build Coastguard Worker return cast<FPMathOperator>(this)->hasNoSignedZeros();
196*9880d681SAndroid Build Coastguard Worker }
197*9880d681SAndroid Build Coastguard Worker
198*9880d681SAndroid Build Coastguard Worker /// Determine whether the allow-reciprocal flag is set.
hasAllowReciprocal() const199*9880d681SAndroid Build Coastguard Worker bool Instruction::hasAllowReciprocal() const {
200*9880d681SAndroid Build Coastguard Worker assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
201*9880d681SAndroid Build Coastguard Worker return cast<FPMathOperator>(this)->hasAllowReciprocal();
202*9880d681SAndroid Build Coastguard Worker }
203*9880d681SAndroid Build Coastguard Worker
204*9880d681SAndroid Build Coastguard Worker /// Convenience function for getting all the fast-math flags, which must be an
205*9880d681SAndroid Build Coastguard Worker /// operator which supports these flags. See LangRef.html for the meaning of
206*9880d681SAndroid Build Coastguard Worker /// these flags.
getFastMathFlags() const207*9880d681SAndroid Build Coastguard Worker FastMathFlags Instruction::getFastMathFlags() const {
208*9880d681SAndroid Build Coastguard Worker assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
209*9880d681SAndroid Build Coastguard Worker return cast<FPMathOperator>(this)->getFastMathFlags();
210*9880d681SAndroid Build Coastguard Worker }
211*9880d681SAndroid Build Coastguard Worker
212*9880d681SAndroid Build Coastguard Worker /// Copy I's fast-math flags
copyFastMathFlags(const Instruction * I)213*9880d681SAndroid Build Coastguard Worker void Instruction::copyFastMathFlags(const Instruction *I) {
214*9880d681SAndroid Build Coastguard Worker copyFastMathFlags(I->getFastMathFlags());
215*9880d681SAndroid Build Coastguard Worker }
216*9880d681SAndroid Build Coastguard Worker
copyIRFlags(const Value * V)217*9880d681SAndroid Build Coastguard Worker void Instruction::copyIRFlags(const Value *V) {
218*9880d681SAndroid Build Coastguard Worker // Copy the wrapping flags.
219*9880d681SAndroid Build Coastguard Worker if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
220*9880d681SAndroid Build Coastguard Worker if (isa<OverflowingBinaryOperator>(this)) {
221*9880d681SAndroid Build Coastguard Worker setHasNoSignedWrap(OB->hasNoSignedWrap());
222*9880d681SAndroid Build Coastguard Worker setHasNoUnsignedWrap(OB->hasNoUnsignedWrap());
223*9880d681SAndroid Build Coastguard Worker }
224*9880d681SAndroid Build Coastguard Worker }
225*9880d681SAndroid Build Coastguard Worker
226*9880d681SAndroid Build Coastguard Worker // Copy the exact flag.
227*9880d681SAndroid Build Coastguard Worker if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
228*9880d681SAndroid Build Coastguard Worker if (isa<PossiblyExactOperator>(this))
229*9880d681SAndroid Build Coastguard Worker setIsExact(PE->isExact());
230*9880d681SAndroid Build Coastguard Worker
231*9880d681SAndroid Build Coastguard Worker // Copy the fast-math flags.
232*9880d681SAndroid Build Coastguard Worker if (auto *FP = dyn_cast<FPMathOperator>(V))
233*9880d681SAndroid Build Coastguard Worker if (isa<FPMathOperator>(this))
234*9880d681SAndroid Build Coastguard Worker copyFastMathFlags(FP->getFastMathFlags());
235*9880d681SAndroid Build Coastguard Worker }
236*9880d681SAndroid Build Coastguard Worker
andIRFlags(const Value * V)237*9880d681SAndroid Build Coastguard Worker void Instruction::andIRFlags(const Value *V) {
238*9880d681SAndroid Build Coastguard Worker if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
239*9880d681SAndroid Build Coastguard Worker if (isa<OverflowingBinaryOperator>(this)) {
240*9880d681SAndroid Build Coastguard Worker setHasNoSignedWrap(hasNoSignedWrap() & OB->hasNoSignedWrap());
241*9880d681SAndroid Build Coastguard Worker setHasNoUnsignedWrap(hasNoUnsignedWrap() & OB->hasNoUnsignedWrap());
242*9880d681SAndroid Build Coastguard Worker }
243*9880d681SAndroid Build Coastguard Worker }
244*9880d681SAndroid Build Coastguard Worker
245*9880d681SAndroid Build Coastguard Worker if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
246*9880d681SAndroid Build Coastguard Worker if (isa<PossiblyExactOperator>(this))
247*9880d681SAndroid Build Coastguard Worker setIsExact(isExact() & PE->isExact());
248*9880d681SAndroid Build Coastguard Worker
249*9880d681SAndroid Build Coastguard Worker if (auto *FP = dyn_cast<FPMathOperator>(V)) {
250*9880d681SAndroid Build Coastguard Worker if (isa<FPMathOperator>(this)) {
251*9880d681SAndroid Build Coastguard Worker FastMathFlags FM = getFastMathFlags();
252*9880d681SAndroid Build Coastguard Worker FM &= FP->getFastMathFlags();
253*9880d681SAndroid Build Coastguard Worker copyFastMathFlags(FM);
254*9880d681SAndroid Build Coastguard Worker }
255*9880d681SAndroid Build Coastguard Worker }
256*9880d681SAndroid Build Coastguard Worker }
257*9880d681SAndroid Build Coastguard Worker
getOpcodeName(unsigned OpCode)258*9880d681SAndroid Build Coastguard Worker const char *Instruction::getOpcodeName(unsigned OpCode) {
259*9880d681SAndroid Build Coastguard Worker switch (OpCode) {
260*9880d681SAndroid Build Coastguard Worker // Terminators
261*9880d681SAndroid Build Coastguard Worker case Ret: return "ret";
262*9880d681SAndroid Build Coastguard Worker case Br: return "br";
263*9880d681SAndroid Build Coastguard Worker case Switch: return "switch";
264*9880d681SAndroid Build Coastguard Worker case IndirectBr: return "indirectbr";
265*9880d681SAndroid Build Coastguard Worker case Invoke: return "invoke";
266*9880d681SAndroid Build Coastguard Worker case Resume: return "resume";
267*9880d681SAndroid Build Coastguard Worker case Unreachable: return "unreachable";
268*9880d681SAndroid Build Coastguard Worker case CleanupRet: return "cleanupret";
269*9880d681SAndroid Build Coastguard Worker case CatchRet: return "catchret";
270*9880d681SAndroid Build Coastguard Worker case CatchPad: return "catchpad";
271*9880d681SAndroid Build Coastguard Worker case CatchSwitch: return "catchswitch";
272*9880d681SAndroid Build Coastguard Worker
273*9880d681SAndroid Build Coastguard Worker // Standard binary operators...
274*9880d681SAndroid Build Coastguard Worker case Add: return "add";
275*9880d681SAndroid Build Coastguard Worker case FAdd: return "fadd";
276*9880d681SAndroid Build Coastguard Worker case Sub: return "sub";
277*9880d681SAndroid Build Coastguard Worker case FSub: return "fsub";
278*9880d681SAndroid Build Coastguard Worker case Mul: return "mul";
279*9880d681SAndroid Build Coastguard Worker case FMul: return "fmul";
280*9880d681SAndroid Build Coastguard Worker case UDiv: return "udiv";
281*9880d681SAndroid Build Coastguard Worker case SDiv: return "sdiv";
282*9880d681SAndroid Build Coastguard Worker case FDiv: return "fdiv";
283*9880d681SAndroid Build Coastguard Worker case URem: return "urem";
284*9880d681SAndroid Build Coastguard Worker case SRem: return "srem";
285*9880d681SAndroid Build Coastguard Worker case FRem: return "frem";
286*9880d681SAndroid Build Coastguard Worker
287*9880d681SAndroid Build Coastguard Worker // Logical operators...
288*9880d681SAndroid Build Coastguard Worker case And: return "and";
289*9880d681SAndroid Build Coastguard Worker case Or : return "or";
290*9880d681SAndroid Build Coastguard Worker case Xor: return "xor";
291*9880d681SAndroid Build Coastguard Worker
292*9880d681SAndroid Build Coastguard Worker // Memory instructions...
293*9880d681SAndroid Build Coastguard Worker case Alloca: return "alloca";
294*9880d681SAndroid Build Coastguard Worker case Load: return "load";
295*9880d681SAndroid Build Coastguard Worker case Store: return "store";
296*9880d681SAndroid Build Coastguard Worker case AtomicCmpXchg: return "cmpxchg";
297*9880d681SAndroid Build Coastguard Worker case AtomicRMW: return "atomicrmw";
298*9880d681SAndroid Build Coastguard Worker case Fence: return "fence";
299*9880d681SAndroid Build Coastguard Worker case GetElementPtr: return "getelementptr";
300*9880d681SAndroid Build Coastguard Worker
301*9880d681SAndroid Build Coastguard Worker // Convert instructions...
302*9880d681SAndroid Build Coastguard Worker case Trunc: return "trunc";
303*9880d681SAndroid Build Coastguard Worker case ZExt: return "zext";
304*9880d681SAndroid Build Coastguard Worker case SExt: return "sext";
305*9880d681SAndroid Build Coastguard Worker case FPTrunc: return "fptrunc";
306*9880d681SAndroid Build Coastguard Worker case FPExt: return "fpext";
307*9880d681SAndroid Build Coastguard Worker case FPToUI: return "fptoui";
308*9880d681SAndroid Build Coastguard Worker case FPToSI: return "fptosi";
309*9880d681SAndroid Build Coastguard Worker case UIToFP: return "uitofp";
310*9880d681SAndroid Build Coastguard Worker case SIToFP: return "sitofp";
311*9880d681SAndroid Build Coastguard Worker case IntToPtr: return "inttoptr";
312*9880d681SAndroid Build Coastguard Worker case PtrToInt: return "ptrtoint";
313*9880d681SAndroid Build Coastguard Worker case BitCast: return "bitcast";
314*9880d681SAndroid Build Coastguard Worker case AddrSpaceCast: return "addrspacecast";
315*9880d681SAndroid Build Coastguard Worker
316*9880d681SAndroid Build Coastguard Worker // Other instructions...
317*9880d681SAndroid Build Coastguard Worker case ICmp: return "icmp";
318*9880d681SAndroid Build Coastguard Worker case FCmp: return "fcmp";
319*9880d681SAndroid Build Coastguard Worker case PHI: return "phi";
320*9880d681SAndroid Build Coastguard Worker case Select: return "select";
321*9880d681SAndroid Build Coastguard Worker case Call: return "call";
322*9880d681SAndroid Build Coastguard Worker case Shl: return "shl";
323*9880d681SAndroid Build Coastguard Worker case LShr: return "lshr";
324*9880d681SAndroid Build Coastguard Worker case AShr: return "ashr";
325*9880d681SAndroid Build Coastguard Worker case VAArg: return "va_arg";
326*9880d681SAndroid Build Coastguard Worker case ExtractElement: return "extractelement";
327*9880d681SAndroid Build Coastguard Worker case InsertElement: return "insertelement";
328*9880d681SAndroid Build Coastguard Worker case ShuffleVector: return "shufflevector";
329*9880d681SAndroid Build Coastguard Worker case ExtractValue: return "extractvalue";
330*9880d681SAndroid Build Coastguard Worker case InsertValue: return "insertvalue";
331*9880d681SAndroid Build Coastguard Worker case LandingPad: return "landingpad";
332*9880d681SAndroid Build Coastguard Worker case CleanupPad: return "cleanuppad";
333*9880d681SAndroid Build Coastguard Worker
334*9880d681SAndroid Build Coastguard Worker default: return "<Invalid operator> ";
335*9880d681SAndroid Build Coastguard Worker }
336*9880d681SAndroid Build Coastguard Worker }
337*9880d681SAndroid Build Coastguard Worker
338*9880d681SAndroid Build Coastguard Worker /// Return true if both instructions have the same special state This must be
339*9880d681SAndroid Build Coastguard Worker /// kept in sync with FunctionComparator::cmpOperations in
340*9880d681SAndroid Build Coastguard Worker /// lib/Transforms/IPO/MergeFunctions.cpp.
haveSameSpecialState(const Instruction * I1,const Instruction * I2,bool IgnoreAlignment=false)341*9880d681SAndroid Build Coastguard Worker static bool haveSameSpecialState(const Instruction *I1, const Instruction *I2,
342*9880d681SAndroid Build Coastguard Worker bool IgnoreAlignment = false) {
343*9880d681SAndroid Build Coastguard Worker assert(I1->getOpcode() == I2->getOpcode() &&
344*9880d681SAndroid Build Coastguard Worker "Can not compare special state of different instructions");
345*9880d681SAndroid Build Coastguard Worker
346*9880d681SAndroid Build Coastguard Worker if (const AllocaInst *AI = dyn_cast<AllocaInst>(I1))
347*9880d681SAndroid Build Coastguard Worker return AI->getAllocatedType() == cast<AllocaInst>(I2)->getAllocatedType() &&
348*9880d681SAndroid Build Coastguard Worker (AI->getAlignment() == cast<AllocaInst>(I2)->getAlignment() ||
349*9880d681SAndroid Build Coastguard Worker IgnoreAlignment);
350*9880d681SAndroid Build Coastguard Worker if (const LoadInst *LI = dyn_cast<LoadInst>(I1))
351*9880d681SAndroid Build Coastguard Worker return LI->isVolatile() == cast<LoadInst>(I2)->isVolatile() &&
352*9880d681SAndroid Build Coastguard Worker (LI->getAlignment() == cast<LoadInst>(I2)->getAlignment() ||
353*9880d681SAndroid Build Coastguard Worker IgnoreAlignment) &&
354*9880d681SAndroid Build Coastguard Worker LI->getOrdering() == cast<LoadInst>(I2)->getOrdering() &&
355*9880d681SAndroid Build Coastguard Worker LI->getSynchScope() == cast<LoadInst>(I2)->getSynchScope();
356*9880d681SAndroid Build Coastguard Worker if (const StoreInst *SI = dyn_cast<StoreInst>(I1))
357*9880d681SAndroid Build Coastguard Worker return SI->isVolatile() == cast<StoreInst>(I2)->isVolatile() &&
358*9880d681SAndroid Build Coastguard Worker (SI->getAlignment() == cast<StoreInst>(I2)->getAlignment() ||
359*9880d681SAndroid Build Coastguard Worker IgnoreAlignment) &&
360*9880d681SAndroid Build Coastguard Worker SI->getOrdering() == cast<StoreInst>(I2)->getOrdering() &&
361*9880d681SAndroid Build Coastguard Worker SI->getSynchScope() == cast<StoreInst>(I2)->getSynchScope();
362*9880d681SAndroid Build Coastguard Worker if (const CmpInst *CI = dyn_cast<CmpInst>(I1))
363*9880d681SAndroid Build Coastguard Worker return CI->getPredicate() == cast<CmpInst>(I2)->getPredicate();
364*9880d681SAndroid Build Coastguard Worker if (const CallInst *CI = dyn_cast<CallInst>(I1))
365*9880d681SAndroid Build Coastguard Worker return CI->isTailCall() == cast<CallInst>(I2)->isTailCall() &&
366*9880d681SAndroid Build Coastguard Worker CI->getCallingConv() == cast<CallInst>(I2)->getCallingConv() &&
367*9880d681SAndroid Build Coastguard Worker CI->getAttributes() == cast<CallInst>(I2)->getAttributes() &&
368*9880d681SAndroid Build Coastguard Worker CI->hasIdenticalOperandBundleSchema(*cast<CallInst>(I2));
369*9880d681SAndroid Build Coastguard Worker if (const InvokeInst *CI = dyn_cast<InvokeInst>(I1))
370*9880d681SAndroid Build Coastguard Worker return CI->getCallingConv() == cast<InvokeInst>(I2)->getCallingConv() &&
371*9880d681SAndroid Build Coastguard Worker CI->getAttributes() == cast<InvokeInst>(I2)->getAttributes() &&
372*9880d681SAndroid Build Coastguard Worker CI->hasIdenticalOperandBundleSchema(*cast<InvokeInst>(I2));
373*9880d681SAndroid Build Coastguard Worker if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(I1))
374*9880d681SAndroid Build Coastguard Worker return IVI->getIndices() == cast<InsertValueInst>(I2)->getIndices();
375*9880d681SAndroid Build Coastguard Worker if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I1))
376*9880d681SAndroid Build Coastguard Worker return EVI->getIndices() == cast<ExtractValueInst>(I2)->getIndices();
377*9880d681SAndroid Build Coastguard Worker if (const FenceInst *FI = dyn_cast<FenceInst>(I1))
378*9880d681SAndroid Build Coastguard Worker return FI->getOrdering() == cast<FenceInst>(I2)->getOrdering() &&
379*9880d681SAndroid Build Coastguard Worker FI->getSynchScope() == cast<FenceInst>(I2)->getSynchScope();
380*9880d681SAndroid Build Coastguard Worker if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(I1))
381*9880d681SAndroid Build Coastguard Worker return CXI->isVolatile() == cast<AtomicCmpXchgInst>(I2)->isVolatile() &&
382*9880d681SAndroid Build Coastguard Worker CXI->isWeak() == cast<AtomicCmpXchgInst>(I2)->isWeak() &&
383*9880d681SAndroid Build Coastguard Worker CXI->getSuccessOrdering() ==
384*9880d681SAndroid Build Coastguard Worker cast<AtomicCmpXchgInst>(I2)->getSuccessOrdering() &&
385*9880d681SAndroid Build Coastguard Worker CXI->getFailureOrdering() ==
386*9880d681SAndroid Build Coastguard Worker cast<AtomicCmpXchgInst>(I2)->getFailureOrdering() &&
387*9880d681SAndroid Build Coastguard Worker CXI->getSynchScope() == cast<AtomicCmpXchgInst>(I2)->getSynchScope();
388*9880d681SAndroid Build Coastguard Worker if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(I1))
389*9880d681SAndroid Build Coastguard Worker return RMWI->getOperation() == cast<AtomicRMWInst>(I2)->getOperation() &&
390*9880d681SAndroid Build Coastguard Worker RMWI->isVolatile() == cast<AtomicRMWInst>(I2)->isVolatile() &&
391*9880d681SAndroid Build Coastguard Worker RMWI->getOrdering() == cast<AtomicRMWInst>(I2)->getOrdering() &&
392*9880d681SAndroid Build Coastguard Worker RMWI->getSynchScope() == cast<AtomicRMWInst>(I2)->getSynchScope();
393*9880d681SAndroid Build Coastguard Worker
394*9880d681SAndroid Build Coastguard Worker return true;
395*9880d681SAndroid Build Coastguard Worker }
396*9880d681SAndroid Build Coastguard Worker
397*9880d681SAndroid Build Coastguard Worker /// isIdenticalTo - Return true if the specified instruction is exactly
398*9880d681SAndroid Build Coastguard Worker /// identical to the current one. This means that all operands match and any
399*9880d681SAndroid Build Coastguard Worker /// extra information (e.g. load is volatile) agree.
isIdenticalTo(const Instruction * I) const400*9880d681SAndroid Build Coastguard Worker bool Instruction::isIdenticalTo(const Instruction *I) const {
401*9880d681SAndroid Build Coastguard Worker return isIdenticalToWhenDefined(I) &&
402*9880d681SAndroid Build Coastguard Worker SubclassOptionalData == I->SubclassOptionalData;
403*9880d681SAndroid Build Coastguard Worker }
404*9880d681SAndroid Build Coastguard Worker
405*9880d681SAndroid Build Coastguard Worker /// isIdenticalToWhenDefined - This is like isIdenticalTo, except that it
406*9880d681SAndroid Build Coastguard Worker /// ignores the SubclassOptionalData flags, which specify conditions
407*9880d681SAndroid Build Coastguard Worker /// under which the instruction's result is undefined.
isIdenticalToWhenDefined(const Instruction * I) const408*9880d681SAndroid Build Coastguard Worker bool Instruction::isIdenticalToWhenDefined(const Instruction *I) const {
409*9880d681SAndroid Build Coastguard Worker if (getOpcode() != I->getOpcode() ||
410*9880d681SAndroid Build Coastguard Worker getNumOperands() != I->getNumOperands() ||
411*9880d681SAndroid Build Coastguard Worker getType() != I->getType())
412*9880d681SAndroid Build Coastguard Worker return false;
413*9880d681SAndroid Build Coastguard Worker
414*9880d681SAndroid Build Coastguard Worker // If both instructions have no operands, they are identical.
415*9880d681SAndroid Build Coastguard Worker if (getNumOperands() == 0 && I->getNumOperands() == 0)
416*9880d681SAndroid Build Coastguard Worker return haveSameSpecialState(this, I);
417*9880d681SAndroid Build Coastguard Worker
418*9880d681SAndroid Build Coastguard Worker // We have two instructions of identical opcode and #operands. Check to see
419*9880d681SAndroid Build Coastguard Worker // if all operands are the same.
420*9880d681SAndroid Build Coastguard Worker if (!std::equal(op_begin(), op_end(), I->op_begin()))
421*9880d681SAndroid Build Coastguard Worker return false;
422*9880d681SAndroid Build Coastguard Worker
423*9880d681SAndroid Build Coastguard Worker if (const PHINode *thisPHI = dyn_cast<PHINode>(this)) {
424*9880d681SAndroid Build Coastguard Worker const PHINode *otherPHI = cast<PHINode>(I);
425*9880d681SAndroid Build Coastguard Worker return std::equal(thisPHI->block_begin(), thisPHI->block_end(),
426*9880d681SAndroid Build Coastguard Worker otherPHI->block_begin());
427*9880d681SAndroid Build Coastguard Worker }
428*9880d681SAndroid Build Coastguard Worker
429*9880d681SAndroid Build Coastguard Worker return haveSameSpecialState(this, I);
430*9880d681SAndroid Build Coastguard Worker }
431*9880d681SAndroid Build Coastguard Worker
432*9880d681SAndroid Build Coastguard Worker // Keep this in sync with FunctionComparator::cmpOperations in
433*9880d681SAndroid Build Coastguard Worker // lib/Transforms/IPO/MergeFunctions.cpp.
isSameOperationAs(const Instruction * I,unsigned flags) const434*9880d681SAndroid Build Coastguard Worker bool Instruction::isSameOperationAs(const Instruction *I,
435*9880d681SAndroid Build Coastguard Worker unsigned flags) const {
436*9880d681SAndroid Build Coastguard Worker bool IgnoreAlignment = flags & CompareIgnoringAlignment;
437*9880d681SAndroid Build Coastguard Worker bool UseScalarTypes = flags & CompareUsingScalarTypes;
438*9880d681SAndroid Build Coastguard Worker
439*9880d681SAndroid Build Coastguard Worker if (getOpcode() != I->getOpcode() ||
440*9880d681SAndroid Build Coastguard Worker getNumOperands() != I->getNumOperands() ||
441*9880d681SAndroid Build Coastguard Worker (UseScalarTypes ?
442*9880d681SAndroid Build Coastguard Worker getType()->getScalarType() != I->getType()->getScalarType() :
443*9880d681SAndroid Build Coastguard Worker getType() != I->getType()))
444*9880d681SAndroid Build Coastguard Worker return false;
445*9880d681SAndroid Build Coastguard Worker
446*9880d681SAndroid Build Coastguard Worker // We have two instructions of identical opcode and #operands. Check to see
447*9880d681SAndroid Build Coastguard Worker // if all operands are the same type
448*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
449*9880d681SAndroid Build Coastguard Worker if (UseScalarTypes ?
450*9880d681SAndroid Build Coastguard Worker getOperand(i)->getType()->getScalarType() !=
451*9880d681SAndroid Build Coastguard Worker I->getOperand(i)->getType()->getScalarType() :
452*9880d681SAndroid Build Coastguard Worker getOperand(i)->getType() != I->getOperand(i)->getType())
453*9880d681SAndroid Build Coastguard Worker return false;
454*9880d681SAndroid Build Coastguard Worker
455*9880d681SAndroid Build Coastguard Worker return haveSameSpecialState(this, I, IgnoreAlignment);
456*9880d681SAndroid Build Coastguard Worker }
457*9880d681SAndroid Build Coastguard Worker
458*9880d681SAndroid Build Coastguard Worker /// isUsedOutsideOfBlock - Return true if there are any uses of I outside of the
459*9880d681SAndroid Build Coastguard Worker /// specified block. Note that PHI nodes are considered to evaluate their
460*9880d681SAndroid Build Coastguard Worker /// operands in the corresponding predecessor block.
isUsedOutsideOfBlock(const BasicBlock * BB) const461*9880d681SAndroid Build Coastguard Worker bool Instruction::isUsedOutsideOfBlock(const BasicBlock *BB) const {
462*9880d681SAndroid Build Coastguard Worker for (const Use &U : uses()) {
463*9880d681SAndroid Build Coastguard Worker // PHI nodes uses values in the corresponding predecessor block. For other
464*9880d681SAndroid Build Coastguard Worker // instructions, just check to see whether the parent of the use matches up.
465*9880d681SAndroid Build Coastguard Worker const Instruction *I = cast<Instruction>(U.getUser());
466*9880d681SAndroid Build Coastguard Worker const PHINode *PN = dyn_cast<PHINode>(I);
467*9880d681SAndroid Build Coastguard Worker if (!PN) {
468*9880d681SAndroid Build Coastguard Worker if (I->getParent() != BB)
469*9880d681SAndroid Build Coastguard Worker return true;
470*9880d681SAndroid Build Coastguard Worker continue;
471*9880d681SAndroid Build Coastguard Worker }
472*9880d681SAndroid Build Coastguard Worker
473*9880d681SAndroid Build Coastguard Worker if (PN->getIncomingBlock(U) != BB)
474*9880d681SAndroid Build Coastguard Worker return true;
475*9880d681SAndroid Build Coastguard Worker }
476*9880d681SAndroid Build Coastguard Worker return false;
477*9880d681SAndroid Build Coastguard Worker }
478*9880d681SAndroid Build Coastguard Worker
479*9880d681SAndroid Build Coastguard Worker /// mayReadFromMemory - Return true if this instruction may read memory.
480*9880d681SAndroid Build Coastguard Worker ///
mayReadFromMemory() const481*9880d681SAndroid Build Coastguard Worker bool Instruction::mayReadFromMemory() const {
482*9880d681SAndroid Build Coastguard Worker switch (getOpcode()) {
483*9880d681SAndroid Build Coastguard Worker default: return false;
484*9880d681SAndroid Build Coastguard Worker case Instruction::VAArg:
485*9880d681SAndroid Build Coastguard Worker case Instruction::Load:
486*9880d681SAndroid Build Coastguard Worker case Instruction::Fence: // FIXME: refine definition of mayReadFromMemory
487*9880d681SAndroid Build Coastguard Worker case Instruction::AtomicCmpXchg:
488*9880d681SAndroid Build Coastguard Worker case Instruction::AtomicRMW:
489*9880d681SAndroid Build Coastguard Worker case Instruction::CatchPad:
490*9880d681SAndroid Build Coastguard Worker case Instruction::CatchRet:
491*9880d681SAndroid Build Coastguard Worker return true;
492*9880d681SAndroid Build Coastguard Worker case Instruction::Call:
493*9880d681SAndroid Build Coastguard Worker return !cast<CallInst>(this)->doesNotAccessMemory();
494*9880d681SAndroid Build Coastguard Worker case Instruction::Invoke:
495*9880d681SAndroid Build Coastguard Worker return !cast<InvokeInst>(this)->doesNotAccessMemory();
496*9880d681SAndroid Build Coastguard Worker case Instruction::Store:
497*9880d681SAndroid Build Coastguard Worker return !cast<StoreInst>(this)->isUnordered();
498*9880d681SAndroid Build Coastguard Worker }
499*9880d681SAndroid Build Coastguard Worker }
500*9880d681SAndroid Build Coastguard Worker
501*9880d681SAndroid Build Coastguard Worker /// mayWriteToMemory - Return true if this instruction may modify memory.
502*9880d681SAndroid Build Coastguard Worker ///
mayWriteToMemory() const503*9880d681SAndroid Build Coastguard Worker bool Instruction::mayWriteToMemory() const {
504*9880d681SAndroid Build Coastguard Worker switch (getOpcode()) {
505*9880d681SAndroid Build Coastguard Worker default: return false;
506*9880d681SAndroid Build Coastguard Worker case Instruction::Fence: // FIXME: refine definition of mayWriteToMemory
507*9880d681SAndroid Build Coastguard Worker case Instruction::Store:
508*9880d681SAndroid Build Coastguard Worker case Instruction::VAArg:
509*9880d681SAndroid Build Coastguard Worker case Instruction::AtomicCmpXchg:
510*9880d681SAndroid Build Coastguard Worker case Instruction::AtomicRMW:
511*9880d681SAndroid Build Coastguard Worker case Instruction::CatchPad:
512*9880d681SAndroid Build Coastguard Worker case Instruction::CatchRet:
513*9880d681SAndroid Build Coastguard Worker return true;
514*9880d681SAndroid Build Coastguard Worker case Instruction::Call:
515*9880d681SAndroid Build Coastguard Worker return !cast<CallInst>(this)->onlyReadsMemory();
516*9880d681SAndroid Build Coastguard Worker case Instruction::Invoke:
517*9880d681SAndroid Build Coastguard Worker return !cast<InvokeInst>(this)->onlyReadsMemory();
518*9880d681SAndroid Build Coastguard Worker case Instruction::Load:
519*9880d681SAndroid Build Coastguard Worker return !cast<LoadInst>(this)->isUnordered();
520*9880d681SAndroid Build Coastguard Worker }
521*9880d681SAndroid Build Coastguard Worker }
522*9880d681SAndroid Build Coastguard Worker
isAtomic() const523*9880d681SAndroid Build Coastguard Worker bool Instruction::isAtomic() const {
524*9880d681SAndroid Build Coastguard Worker switch (getOpcode()) {
525*9880d681SAndroid Build Coastguard Worker default:
526*9880d681SAndroid Build Coastguard Worker return false;
527*9880d681SAndroid Build Coastguard Worker case Instruction::AtomicCmpXchg:
528*9880d681SAndroid Build Coastguard Worker case Instruction::AtomicRMW:
529*9880d681SAndroid Build Coastguard Worker case Instruction::Fence:
530*9880d681SAndroid Build Coastguard Worker return true;
531*9880d681SAndroid Build Coastguard Worker case Instruction::Load:
532*9880d681SAndroid Build Coastguard Worker return cast<LoadInst>(this)->getOrdering() != AtomicOrdering::NotAtomic;
533*9880d681SAndroid Build Coastguard Worker case Instruction::Store:
534*9880d681SAndroid Build Coastguard Worker return cast<StoreInst>(this)->getOrdering() != AtomicOrdering::NotAtomic;
535*9880d681SAndroid Build Coastguard Worker }
536*9880d681SAndroid Build Coastguard Worker }
537*9880d681SAndroid Build Coastguard Worker
mayThrow() const538*9880d681SAndroid Build Coastguard Worker bool Instruction::mayThrow() const {
539*9880d681SAndroid Build Coastguard Worker if (const CallInst *CI = dyn_cast<CallInst>(this))
540*9880d681SAndroid Build Coastguard Worker return !CI->doesNotThrow();
541*9880d681SAndroid Build Coastguard Worker if (const auto *CRI = dyn_cast<CleanupReturnInst>(this))
542*9880d681SAndroid Build Coastguard Worker return CRI->unwindsToCaller();
543*9880d681SAndroid Build Coastguard Worker if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(this))
544*9880d681SAndroid Build Coastguard Worker return CatchSwitch->unwindsToCaller();
545*9880d681SAndroid Build Coastguard Worker return isa<ResumeInst>(this);
546*9880d681SAndroid Build Coastguard Worker }
547*9880d681SAndroid Build Coastguard Worker
548*9880d681SAndroid Build Coastguard Worker /// isAssociative - Return true if the instruction is associative:
549*9880d681SAndroid Build Coastguard Worker ///
550*9880d681SAndroid Build Coastguard Worker /// Associative operators satisfy: x op (y op z) === (x op y) op z
551*9880d681SAndroid Build Coastguard Worker ///
552*9880d681SAndroid Build Coastguard Worker /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative.
553*9880d681SAndroid Build Coastguard Worker ///
isAssociative(unsigned Opcode)554*9880d681SAndroid Build Coastguard Worker bool Instruction::isAssociative(unsigned Opcode) {
555*9880d681SAndroid Build Coastguard Worker return Opcode == And || Opcode == Or || Opcode == Xor ||
556*9880d681SAndroid Build Coastguard Worker Opcode == Add || Opcode == Mul;
557*9880d681SAndroid Build Coastguard Worker }
558*9880d681SAndroid Build Coastguard Worker
isAssociative() const559*9880d681SAndroid Build Coastguard Worker bool Instruction::isAssociative() const {
560*9880d681SAndroid Build Coastguard Worker unsigned Opcode = getOpcode();
561*9880d681SAndroid Build Coastguard Worker if (isAssociative(Opcode))
562*9880d681SAndroid Build Coastguard Worker return true;
563*9880d681SAndroid Build Coastguard Worker
564*9880d681SAndroid Build Coastguard Worker switch (Opcode) {
565*9880d681SAndroid Build Coastguard Worker case FMul:
566*9880d681SAndroid Build Coastguard Worker case FAdd:
567*9880d681SAndroid Build Coastguard Worker return cast<FPMathOperator>(this)->hasUnsafeAlgebra();
568*9880d681SAndroid Build Coastguard Worker default:
569*9880d681SAndroid Build Coastguard Worker return false;
570*9880d681SAndroid Build Coastguard Worker }
571*9880d681SAndroid Build Coastguard Worker }
572*9880d681SAndroid Build Coastguard Worker
573*9880d681SAndroid Build Coastguard Worker /// isCommutative - Return true if the instruction is commutative:
574*9880d681SAndroid Build Coastguard Worker ///
575*9880d681SAndroid Build Coastguard Worker /// Commutative operators satisfy: (x op y) === (y op x)
576*9880d681SAndroid Build Coastguard Worker ///
577*9880d681SAndroid Build Coastguard Worker /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
578*9880d681SAndroid Build Coastguard Worker /// applied to any type.
579*9880d681SAndroid Build Coastguard Worker ///
isCommutative(unsigned op)580*9880d681SAndroid Build Coastguard Worker bool Instruction::isCommutative(unsigned op) {
581*9880d681SAndroid Build Coastguard Worker switch (op) {
582*9880d681SAndroid Build Coastguard Worker case Add:
583*9880d681SAndroid Build Coastguard Worker case FAdd:
584*9880d681SAndroid Build Coastguard Worker case Mul:
585*9880d681SAndroid Build Coastguard Worker case FMul:
586*9880d681SAndroid Build Coastguard Worker case And:
587*9880d681SAndroid Build Coastguard Worker case Or:
588*9880d681SAndroid Build Coastguard Worker case Xor:
589*9880d681SAndroid Build Coastguard Worker return true;
590*9880d681SAndroid Build Coastguard Worker default:
591*9880d681SAndroid Build Coastguard Worker return false;
592*9880d681SAndroid Build Coastguard Worker }
593*9880d681SAndroid Build Coastguard Worker }
594*9880d681SAndroid Build Coastguard Worker
595*9880d681SAndroid Build Coastguard Worker /// isIdempotent - Return true if the instruction is idempotent:
596*9880d681SAndroid Build Coastguard Worker ///
597*9880d681SAndroid Build Coastguard Worker /// Idempotent operators satisfy: x op x === x
598*9880d681SAndroid Build Coastguard Worker ///
599*9880d681SAndroid Build Coastguard Worker /// In LLVM, the And and Or operators are idempotent.
600*9880d681SAndroid Build Coastguard Worker ///
isIdempotent(unsigned Opcode)601*9880d681SAndroid Build Coastguard Worker bool Instruction::isIdempotent(unsigned Opcode) {
602*9880d681SAndroid Build Coastguard Worker return Opcode == And || Opcode == Or;
603*9880d681SAndroid Build Coastguard Worker }
604*9880d681SAndroid Build Coastguard Worker
605*9880d681SAndroid Build Coastguard Worker /// isNilpotent - Return true if the instruction is nilpotent:
606*9880d681SAndroid Build Coastguard Worker ///
607*9880d681SAndroid Build Coastguard Worker /// Nilpotent operators satisfy: x op x === Id,
608*9880d681SAndroid Build Coastguard Worker ///
609*9880d681SAndroid Build Coastguard Worker /// where Id is the identity for the operator, i.e. a constant such that
610*9880d681SAndroid Build Coastguard Worker /// x op Id === x and Id op x === x for all x.
611*9880d681SAndroid Build Coastguard Worker ///
612*9880d681SAndroid Build Coastguard Worker /// In LLVM, the Xor operator is nilpotent.
613*9880d681SAndroid Build Coastguard Worker ///
isNilpotent(unsigned Opcode)614*9880d681SAndroid Build Coastguard Worker bool Instruction::isNilpotent(unsigned Opcode) {
615*9880d681SAndroid Build Coastguard Worker return Opcode == Xor;
616*9880d681SAndroid Build Coastguard Worker }
617*9880d681SAndroid Build Coastguard Worker
cloneImpl() const618*9880d681SAndroid Build Coastguard Worker Instruction *Instruction::cloneImpl() const {
619*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Subclass of Instruction failed to implement cloneImpl");
620*9880d681SAndroid Build Coastguard Worker }
621*9880d681SAndroid Build Coastguard Worker
clone() const622*9880d681SAndroid Build Coastguard Worker Instruction *Instruction::clone() const {
623*9880d681SAndroid Build Coastguard Worker Instruction *New = nullptr;
624*9880d681SAndroid Build Coastguard Worker switch (getOpcode()) {
625*9880d681SAndroid Build Coastguard Worker default:
626*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Unhandled Opcode.");
627*9880d681SAndroid Build Coastguard Worker #define HANDLE_INST(num, opc, clas) \
628*9880d681SAndroid Build Coastguard Worker case Instruction::opc: \
629*9880d681SAndroid Build Coastguard Worker New = cast<clas>(this)->cloneImpl(); \
630*9880d681SAndroid Build Coastguard Worker break;
631*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instruction.def"
632*9880d681SAndroid Build Coastguard Worker #undef HANDLE_INST
633*9880d681SAndroid Build Coastguard Worker }
634*9880d681SAndroid Build Coastguard Worker
635*9880d681SAndroid Build Coastguard Worker New->SubclassOptionalData = SubclassOptionalData;
636*9880d681SAndroid Build Coastguard Worker if (!hasMetadata())
637*9880d681SAndroid Build Coastguard Worker return New;
638*9880d681SAndroid Build Coastguard Worker
639*9880d681SAndroid Build Coastguard Worker // Otherwise, enumerate and copy over metadata from the old instruction to the
640*9880d681SAndroid Build Coastguard Worker // new one.
641*9880d681SAndroid Build Coastguard Worker SmallVector<std::pair<unsigned, MDNode *>, 4> TheMDs;
642*9880d681SAndroid Build Coastguard Worker getAllMetadataOtherThanDebugLoc(TheMDs);
643*9880d681SAndroid Build Coastguard Worker for (const auto &MD : TheMDs)
644*9880d681SAndroid Build Coastguard Worker New->setMetadata(MD.first, MD.second);
645*9880d681SAndroid Build Coastguard Worker
646*9880d681SAndroid Build Coastguard Worker New->setDebugLoc(getDebugLoc());
647*9880d681SAndroid Build Coastguard Worker return New;
648*9880d681SAndroid Build Coastguard Worker }
649