blob: 2b5a0b39c3161a061378b7b03cab221b30e5e686 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- Instruction.cpp - Implement the Instruction class -----------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
Chandler Carruthc2c50cd2013-01-02 09:10:48 +000010// This file implements the Instruction class for the IR library.
Chris Lattner00950542001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000014#include "llvm/IR/Instruction.h"
15#include "llvm/IR/Constants.h"
16#include "llvm/IR/Instructions.h"
17#include "llvm/IR/Module.h"
18#include "llvm/IR/Operator.h"
19#include "llvm/IR/Type.h"
Duncan Sandsa3355ff2007-12-03 20:06:50 +000020#include "llvm/Support/CallSite.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/Support/LeakDetector.h"
Chris Lattner4b74c832003-11-20 17:45:12 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Chris Lattnerdb125cf2011-07-18 04:54:35 +000024Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,
Chris Lattner910c80a2007-02-24 00:55:48 +000025 Instruction *InsertBefore)
Chris Lattner61336ae2010-04-01 05:23:13 +000026 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) {
Chris Lattnerd1e693f2002-09-08 18:59:35 +000027 // Make sure that we get added to a basicblock
28 LeakDetector::addGarbageObject(this);
Chris Lattner2aa83112002-09-10 15:45:53 +000029
30 // If requested, insert this instruction into a basic block...
31 if (InsertBefore) {
32 assert(InsertBefore->getParent() &&
33 "Instruction to insert before is not in a basic block!");
34 InsertBefore->getParent()->getInstList().insert(InsertBefore, this);
35 }
Chris Lattner00950542001-06-06 20:29:01 +000036}
37
Chris Lattnerdb125cf2011-07-18 04:54:35 +000038Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,
Chris Lattner910c80a2007-02-24 00:55:48 +000039 BasicBlock *InsertAtEnd)
Chris Lattner61336ae2010-04-01 05:23:13 +000040 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) {
Chris Lattner96d83f62005-01-29 00:35:33 +000041 // Make sure that we get added to a basicblock
42 LeakDetector::addGarbageObject(this);
Alkis Evlogimenose5828f12004-05-26 21:41:09 +000043
44 // append this instruction into the basic block
45 assert(InsertAtEnd && "Basic block to append to may not be NULL!");
46 InsertAtEnd->getInstList().push_back(this);
Chris Lattnerf00042a2007-02-13 07:54:42 +000047}
48
49
Chris Lattner70aa33e2006-06-21 16:53:47 +000050// Out of line virtual method, so the vtable, etc has a home.
Gordon Henriksenafba8fe2007-12-10 02:14:30 +000051Instruction::~Instruction() {
Devang Patelf9e0a222009-09-24 16:19:11 +000052 assert(Parent == 0 && "Instruction still linked in the program!");
Dan Gohman4f1be4a2010-07-20 22:25:04 +000053 if (hasMetadataHashEntry())
54 clearMetadataHashEntries();
Chris Lattner70aa33e2006-06-21 16:53:47 +000055}
56
57
Chris Lattnerbded1322002-09-06 21:33:15 +000058void Instruction::setParent(BasicBlock *P) {
Chris Lattner786993c2004-02-04 01:06:38 +000059 if (getParent()) {
60 if (!P) LeakDetector::addGarbageObject(this);
61 } else {
62 if (P) LeakDetector::removeGarbageObject(this);
63 }
Chris Lattnerd1e693f2002-09-08 18:59:35 +000064
Chris Lattnerbded1322002-09-06 21:33:15 +000065 Parent = P;
66}
67
Chris Lattner4b833802004-10-11 22:21:39 +000068void Instruction::removeFromParent() {
69 getParent()->getInstList().remove(this);
70}
71
72void Instruction::eraseFromParent() {
73 getParent()->getInstList().erase(this);
74}
Vikram S. Advec1056452002-07-14 23:09:40 +000075
Owen Anderson26bb50a2008-06-17 18:29:27 +000076/// insertBefore - Insert an unlinked instructions into a basic block
77/// immediately before the specified instruction.
78void Instruction::insertBefore(Instruction *InsertPos) {
79 InsertPos->getParent()->getInstList().insert(InsertPos, this);
80}
81
Chris Lattner3ff704f2009-01-13 07:43:51 +000082/// insertAfter - Insert an unlinked instructions into a basic block
83/// immediately after the specified instruction.
84void Instruction::insertAfter(Instruction *InsertPos) {
85 InsertPos->getParent()->getInstList().insertAfter(InsertPos, this);
86}
87
Chris Lattner0fe34d82005-08-08 05:21:50 +000088/// moveBefore - Unlink this instruction from its current basic block and
89/// insert it into the basic block that MovePos lives in, right before
90/// MovePos.
91void Instruction::moveBefore(Instruction *MovePos) {
92 MovePos->getParent()->getInstList().splice(MovePos,getParent()->getInstList(),
93 this);
94}
95
Michael Ilseman125fc7f2012-11-27 00:41:22 +000096/// Set or clear the unsafe-algebra flag on this instruction, which must be an
97/// operator which supports this flag. See LangRef.html for the meaning of this
98/// flag.
99void Instruction::setHasUnsafeAlgebra(bool B) {
100 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
101 cast<FPMathOperator>(this)->setHasUnsafeAlgebra(B);
102}
103
104/// Set or clear the NoNaNs flag on this instruction, which must be an operator
105/// which supports this flag. See LangRef.html for the meaning of this flag.
106void Instruction::setHasNoNaNs(bool B) {
107 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
108 cast<FPMathOperator>(this)->setHasNoNaNs(B);
109}
110
111/// Set or clear the no-infs flag on this instruction, which must be an operator
112/// which supports this flag. See LangRef.html for the meaning of this flag.
113void Instruction::setHasNoInfs(bool B) {
114 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
115 cast<FPMathOperator>(this)->setHasNoInfs(B);
116}
117
118/// Set or clear the no-signed-zeros flag on this instruction, which must be an
119/// operator which supports this flag. See LangRef.html for the meaning of this
120/// flag.
121void Instruction::setHasNoSignedZeros(bool B) {
122 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
123 cast<FPMathOperator>(this)->setHasNoSignedZeros(B);
124}
125
126/// Set or clear the allow-reciprocal flag on this instruction, which must be an
127/// operator which supports this flag. See LangRef.html for the meaning of this
128/// flag.
129void Instruction::setHasAllowReciprocal(bool B) {
130 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
131 cast<FPMathOperator>(this)->setHasAllowReciprocal(B);
132}
133
134/// Convenience function for setting all the fast-math flags on this
135/// instruction, which must be an operator which supports these flags. See
136/// LangRef.html for the meaning of these flats.
137void Instruction::setFastMathFlags(FastMathFlags FMF) {
138 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
139 cast<FPMathOperator>(this)->setFastMathFlags(FMF);
140}
141
142/// Determine whether the unsafe-algebra flag is set.
143bool Instruction::hasUnsafeAlgebra() const {
144 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
145 return cast<FPMathOperator>(this)->hasUnsafeAlgebra();
146}
147
148/// Determine whether the no-NaNs flag is set.
149bool Instruction::hasNoNaNs() const {
150 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
151 return cast<FPMathOperator>(this)->hasNoNaNs();
152}
153
154/// Determine whether the no-infs flag is set.
155bool Instruction::hasNoInfs() const {
156 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
157 return cast<FPMathOperator>(this)->hasNoInfs();
158}
159
160/// Determine whether the no-signed-zeros flag is set.
161bool Instruction::hasNoSignedZeros() const {
162 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
163 return cast<FPMathOperator>(this)->hasNoSignedZeros();
164}
165
166/// Determine whether the allow-reciprocal flag is set.
167bool Instruction::hasAllowReciprocal() const {
168 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
169 return cast<FPMathOperator>(this)->hasAllowReciprocal();
170}
171
172/// Convenience function for getting all the fast-math flags, which must be an
173/// operator which supports these flags. See LangRef.html for the meaning of
174/// these flats.
175FastMathFlags Instruction::getFastMathFlags() const {
176 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
177 return cast<FPMathOperator>(this)->getFastMathFlags();
178}
Chris Lattner0fe34d82005-08-08 05:21:50 +0000179
Michael Ilseman4b896dd2012-11-29 21:25:12 +0000180/// Copy I's fast-math flags
181void Instruction::copyFastMathFlags(const Instruction *I) {
182 setFastMathFlags(I->getFastMathFlags());
183}
184
185
Vikram S. Advec1056452002-07-14 23:09:40 +0000186const char *Instruction::getOpcodeName(unsigned OpCode) {
187 switch (OpCode) {
188 // Terminators
Chris Lattner0513e9f2002-08-14 18:18:02 +0000189 case Ret: return "ret";
190 case Br: return "br";
Vikram S. Advec1056452002-07-14 23:09:40 +0000191 case Switch: return "switch";
Chris Lattnerab21db72009-10-28 00:19:10 +0000192 case IndirectBr: return "indirectbr";
Vikram S. Advec1056452002-07-14 23:09:40 +0000193 case Invoke: return "invoke";
Bill Wendlingdccc03b2011-07-31 06:30:59 +0000194 case Resume: return "resume";
Chris Lattnerb976e662004-10-16 18:08:06 +0000195 case Unreachable: return "unreachable";
Misha Brukmanfd939082005-04-21 23:48:37 +0000196
Vikram S. Advec1056452002-07-14 23:09:40 +0000197 // Standard binary operators...
198 case Add: return "add";
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000199 case FAdd: return "fadd";
Vikram S. Advec1056452002-07-14 23:09:40 +0000200 case Sub: return "sub";
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000201 case FSub: return "fsub";
Vikram S. Advec1056452002-07-14 23:09:40 +0000202 case Mul: return "mul";
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000203 case FMul: return "fmul";
Reid Spencer1628cec2006-10-26 06:15:43 +0000204 case UDiv: return "udiv";
205 case SDiv: return "sdiv";
206 case FDiv: return "fdiv";
Reid Spencer0a783f72006-11-02 01:53:59 +0000207 case URem: return "urem";
208 case SRem: return "srem";
209 case FRem: return "frem";
Vikram S. Advec1056452002-07-14 23:09:40 +0000210
211 // Logical operators...
212 case And: return "and";
213 case Or : return "or";
214 case Xor: return "xor";
215
Vikram S. Advec1056452002-07-14 23:09:40 +0000216 // Memory instructions...
Vikram S. Advec1056452002-07-14 23:09:40 +0000217 case Alloca: return "alloca";
218 case Load: return "load";
219 case Store: return "store";
Eli Friedmanff030482011-07-28 21:48:00 +0000220 case AtomicCmpXchg: return "cmpxchg";
221 case AtomicRMW: return "atomicrmw";
Eli Friedman47f35132011-07-25 23:16:38 +0000222 case Fence: return "fence";
Vikram S. Advec1056452002-07-14 23:09:40 +0000223 case GetElementPtr: return "getelementptr";
Misha Brukmanfd939082005-04-21 23:48:37 +0000224
Reid Spencer3da59db2006-11-27 01:05:10 +0000225 // Convert instructions...
226 case Trunc: return "trunc";
227 case ZExt: return "zext";
228 case SExt: return "sext";
229 case FPTrunc: return "fptrunc";
230 case FPExt: return "fpext";
231 case FPToUI: return "fptoui";
232 case FPToSI: return "fptosi";
233 case UIToFP: return "uitofp";
234 case SIToFP: return "sitofp";
235 case IntToPtr: return "inttoptr";
236 case PtrToInt: return "ptrtoint";
237 case BitCast: return "bitcast";
238
Vikram S. Advec1056452002-07-14 23:09:40 +0000239 // Other instructions...
Reid Spencer74f16422006-12-03 06:27:29 +0000240 case ICmp: return "icmp";
241 case FCmp: return "fcmp";
Reid Spencer3da59db2006-11-27 01:05:10 +0000242 case PHI: return "phi";
243 case Select: return "select";
244 case Call: return "call";
245 case Shl: return "shl";
246 case LShr: return "lshr";
247 case AShr: return "ashr";
248 case VAArg: return "va_arg";
Robert Bocchinob52ee7f2006-01-10 19:05:34 +0000249 case ExtractElement: return "extractelement";
Reid Spencer3da59db2006-11-27 01:05:10 +0000250 case InsertElement: return "insertelement";
251 case ShuffleVector: return "shufflevector";
Matthijs Kooijman74b5e072008-05-30 10:31:54 +0000252 case ExtractValue: return "extractvalue";
253 case InsertValue: return "insertvalue";
Bill Wendlinge6e88262011-08-12 20:24:12 +0000254 case LandingPad: return "landingpad";
Chris Lattner8f77dae2003-05-08 02:44:12 +0000255
Vikram S. Advec1056452002-07-14 23:09:40 +0000256 default: return "<Invalid operator> ";
257 }
Vikram S. Advec1056452002-07-14 23:09:40 +0000258}
Chris Lattnerf2da7242002-10-31 04:14:01 +0000259
Chris Lattner38f14552004-11-30 02:51:53 +0000260/// isIdenticalTo - Return true if the specified instruction is exactly
261/// identical to the current one. This means that all operands match and any
262/// extra information (e.g. load is volatile) agree.
Chris Lattnere3a08842008-11-27 08:39:18 +0000263bool Instruction::isIdenticalTo(const Instruction *I) const {
Dan Gohman75b0eda2009-08-25 22:24:20 +0000264 return isIdenticalToWhenDefined(I) &&
Dan Gohman58cfa3b2009-08-25 22:11:20 +0000265 SubclassOptionalData == I->SubclassOptionalData;
266}
267
Dan Gohman75b0eda2009-08-25 22:24:20 +0000268/// isIdenticalToWhenDefined - This is like isIdenticalTo, except that it
Dan Gohman58cfa3b2009-08-25 22:11:20 +0000269/// ignores the SubclassOptionalData flags, which specify conditions
270/// under which the instruction's result is undefined.
271bool Instruction::isIdenticalToWhenDefined(const Instruction *I) const {
Chris Lattner38f14552004-11-30 02:51:53 +0000272 if (getOpcode() != I->getOpcode() ||
273 getNumOperands() != I->getNumOperands() ||
274 getType() != I->getType())
275 return false;
276
277 // We have two instructions of identical opcode and #operands. Check to see
278 // if all operands are the same.
279 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
280 if (getOperand(i) != I->getOperand(i))
281 return false;
282
283 // Check special state that is a part of some instructions.
284 if (const LoadInst *LI = dyn_cast<LoadInst>(this))
Dan Gohman9a8af452008-10-16 01:24:45 +0000285 return LI->isVolatile() == cast<LoadInst>(I)->isVolatile() &&
Eli Friedmane5e77122011-08-15 21:00:18 +0000286 LI->getAlignment() == cast<LoadInst>(I)->getAlignment() &&
287 LI->getOrdering() == cast<LoadInst>(I)->getOrdering() &&
288 LI->getSynchScope() == cast<LoadInst>(I)->getSynchScope();
Chris Lattner38f14552004-11-30 02:51:53 +0000289 if (const StoreInst *SI = dyn_cast<StoreInst>(this))
Dan Gohman9a8af452008-10-16 01:24:45 +0000290 return SI->isVolatile() == cast<StoreInst>(I)->isVolatile() &&
Eli Friedmane5e77122011-08-15 21:00:18 +0000291 SI->getAlignment() == cast<StoreInst>(I)->getAlignment() &&
292 SI->getOrdering() == cast<StoreInst>(I)->getOrdering() &&
293 SI->getSynchScope() == cast<StoreInst>(I)->getSynchScope();
Reid Spencere4d87aa2006-12-23 06:05:41 +0000294 if (const CmpInst *CI = dyn_cast<CmpInst>(this))
295 return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
Chris Lattnerddb6db42005-05-06 05:51:46 +0000296 if (const CallInst *CI = dyn_cast<CallInst>(this))
Dan Gohman9a8af452008-10-16 01:24:45 +0000297 return CI->isTailCall() == cast<CallInst>(I)->isTailCall() &&
298 CI->getCallingConv() == cast<CallInst>(I)->getCallingConv() &&
Nick Lewyckyf6c63c22011-01-26 09:23:19 +0000299 CI->getAttributes() == cast<CallInst>(I)->getAttributes();
Dan Gohman9a8af452008-10-16 01:24:45 +0000300 if (const InvokeInst *CI = dyn_cast<InvokeInst>(this))
Nick Lewycky41fe88b2008-10-27 07:28:44 +0000301 return CI->getCallingConv() == cast<InvokeInst>(I)->getCallingConv() &&
Nick Lewyckyf6c63c22011-01-26 09:23:19 +0000302 CI->getAttributes() == cast<InvokeInst>(I)->getAttributes();
Jay Foadfc6d3a42011-07-13 10:26:04 +0000303 if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(this))
304 return IVI->getIndices() == cast<InsertValueInst>(I)->getIndices();
305 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(this))
306 return EVI->getIndices() == cast<ExtractValueInst>(I)->getIndices();
Eli Friedman8a552bb2011-07-27 01:08:30 +0000307 if (const FenceInst *FI = dyn_cast<FenceInst>(this))
308 return FI->getOrdering() == cast<FenceInst>(FI)->getOrdering() &&
309 FI->getSynchScope() == cast<FenceInst>(FI)->getSynchScope();
Eli Friedman55ba8162011-07-29 03:05:32 +0000310 if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(this))
311 return CXI->isVolatile() == cast<AtomicCmpXchgInst>(I)->isVolatile() &&
312 CXI->getOrdering() == cast<AtomicCmpXchgInst>(I)->getOrdering() &&
313 CXI->getSynchScope() == cast<AtomicCmpXchgInst>(I)->getSynchScope();
314 if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(this))
315 return RMWI->getOperation() == cast<AtomicRMWInst>(I)->getOperation() &&
316 RMWI->isVolatile() == cast<AtomicRMWInst>(I)->isVolatile() &&
317 RMWI->getOrdering() == cast<AtomicRMWInst>(I)->getOrdering() &&
318 RMWI->getSynchScope() == cast<AtomicRMWInst>(I)->getSynchScope();
Joel Jones9df72a92012-05-10 15:59:41 +0000319 if (const PHINode *thisPHI = dyn_cast<PHINode>(this)) {
320 const PHINode *otherPHI = cast<PHINode>(I);
321 for (unsigned i = 0, e = thisPHI->getNumOperands(); i != e; ++i) {
322 if (thisPHI->getIncomingBlock(i) != otherPHI->getIncomingBlock(i))
323 return false;
324 }
325 return true;
326 }
Chris Lattner38f14552004-11-30 02:51:53 +0000327 return true;
328}
329
Reid Spencere4d87aa2006-12-23 06:05:41 +0000330// isSameOperationAs
Dan Gohman194ae782009-06-12 19:03:05 +0000331// This should be kept in sync with isEquivalentOperation in
332// lib/Transforms/IPO/MergeFunctions.cpp.
Hal Finkelec4e85e2012-06-28 05:42:26 +0000333bool Instruction::isSameOperationAs(const Instruction *I,
334 unsigned flags) const {
335 bool IgnoreAlignment = flags & CompareIgnoringAlignment;
336 bool UseScalarTypes = flags & CompareUsingScalarTypes;
337
Dan Gohman194ae782009-06-12 19:03:05 +0000338 if (getOpcode() != I->getOpcode() ||
339 getNumOperands() != I->getNumOperands() ||
Hal Finkelec4e85e2012-06-28 05:42:26 +0000340 (UseScalarTypes ?
341 getType()->getScalarType() != I->getType()->getScalarType() :
342 getType() != I->getType()))
Reid Spencere4d87aa2006-12-23 06:05:41 +0000343 return false;
344
345 // We have two instructions of identical opcode and #operands. Check to see
346 // if all operands are the same type
347 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Hal Finkelec4e85e2012-06-28 05:42:26 +0000348 if (UseScalarTypes ?
349 getOperand(i)->getType()->getScalarType() !=
350 I->getOperand(i)->getType()->getScalarType() :
351 getOperand(i)->getType() != I->getOperand(i)->getType())
Reid Spencere4d87aa2006-12-23 06:05:41 +0000352 return false;
353
354 // Check special state that is a part of some instructions.
355 if (const LoadInst *LI = dyn_cast<LoadInst>(this))
Dan Gohman9a8af452008-10-16 01:24:45 +0000356 return LI->isVolatile() == cast<LoadInst>(I)->isVolatile() &&
Hal Finkelec4e85e2012-06-28 05:42:26 +0000357 (LI->getAlignment() == cast<LoadInst>(I)->getAlignment() ||
358 IgnoreAlignment) &&
Eli Friedmane5e77122011-08-15 21:00:18 +0000359 LI->getOrdering() == cast<LoadInst>(I)->getOrdering() &&
360 LI->getSynchScope() == cast<LoadInst>(I)->getSynchScope();
Reid Spencere4d87aa2006-12-23 06:05:41 +0000361 if (const StoreInst *SI = dyn_cast<StoreInst>(this))
Dan Gohman9a8af452008-10-16 01:24:45 +0000362 return SI->isVolatile() == cast<StoreInst>(I)->isVolatile() &&
Hal Finkelec4e85e2012-06-28 05:42:26 +0000363 (SI->getAlignment() == cast<StoreInst>(I)->getAlignment() ||
364 IgnoreAlignment) &&
Eli Friedmane5e77122011-08-15 21:00:18 +0000365 SI->getOrdering() == cast<StoreInst>(I)->getOrdering() &&
366 SI->getSynchScope() == cast<StoreInst>(I)->getSynchScope();
Reid Spencere4d87aa2006-12-23 06:05:41 +0000367 if (const CmpInst *CI = dyn_cast<CmpInst>(this))
368 return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
369 if (const CallInst *CI = dyn_cast<CallInst>(this))
Dan Gohman9a8af452008-10-16 01:24:45 +0000370 return CI->isTailCall() == cast<CallInst>(I)->isTailCall() &&
371 CI->getCallingConv() == cast<CallInst>(I)->getCallingConv() &&
Nick Lewyckyf6c63c22011-01-26 09:23:19 +0000372 CI->getAttributes() == cast<CallInst>(I)->getAttributes();
Dan Gohman9a8af452008-10-16 01:24:45 +0000373 if (const InvokeInst *CI = dyn_cast<InvokeInst>(this))
Nick Lewycky41fe88b2008-10-27 07:28:44 +0000374 return CI->getCallingConv() == cast<InvokeInst>(I)->getCallingConv() &&
Nick Lewyckyf6c63c22011-01-26 09:23:19 +0000375 CI->getAttributes() ==
376 cast<InvokeInst>(I)->getAttributes();
Jay Foadfc6d3a42011-07-13 10:26:04 +0000377 if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(this))
378 return IVI->getIndices() == cast<InsertValueInst>(I)->getIndices();
379 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(this))
380 return EVI->getIndices() == cast<ExtractValueInst>(I)->getIndices();
Eli Friedman8a552bb2011-07-27 01:08:30 +0000381 if (const FenceInst *FI = dyn_cast<FenceInst>(this))
Eli Friedman55ba8162011-07-29 03:05:32 +0000382 return FI->getOrdering() == cast<FenceInst>(I)->getOrdering() &&
383 FI->getSynchScope() == cast<FenceInst>(I)->getSynchScope();
384 if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(this))
385 return CXI->isVolatile() == cast<AtomicCmpXchgInst>(I)->isVolatile() &&
386 CXI->getOrdering() == cast<AtomicCmpXchgInst>(I)->getOrdering() &&
387 CXI->getSynchScope() == cast<AtomicCmpXchgInst>(I)->getSynchScope();
388 if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(this))
389 return RMWI->getOperation() == cast<AtomicRMWInst>(I)->getOperation() &&
390 RMWI->isVolatile() == cast<AtomicRMWInst>(I)->isVolatile() &&
391 RMWI->getOrdering() == cast<AtomicRMWInst>(I)->getOrdering() &&
392 RMWI->getSynchScope() == cast<AtomicRMWInst>(I)->getSynchScope();
Reid Spencere4d87aa2006-12-23 06:05:41 +0000393
394 return true;
395}
396
Chris Lattner7ae40e72008-04-20 22:11:30 +0000397/// isUsedOutsideOfBlock - Return true if there are any uses of I outside of the
398/// specified block. Note that PHI nodes are considered to evaluate their
399/// operands in the corresponding predecessor block.
400bool Instruction::isUsedOutsideOfBlock(const BasicBlock *BB) const {
Gabor Greif60ad7812010-03-25 23:06:16 +0000401 for (const_use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Chris Lattner7ae40e72008-04-20 22:11:30 +0000402 // PHI nodes uses values in the corresponding predecessor block. For other
403 // instructions, just check to see whether the parent of the use matches up.
Gabor Greifa8b9df72010-07-12 10:36:48 +0000404 const User *U = *UI;
405 const PHINode *PN = dyn_cast<PHINode>(U);
Chris Lattner7ae40e72008-04-20 22:11:30 +0000406 if (PN == 0) {
Gabor Greifa8b9df72010-07-12 10:36:48 +0000407 if (cast<Instruction>(U)->getParent() != BB)
Chris Lattner7ae40e72008-04-20 22:11:30 +0000408 return true;
409 continue;
410 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000411
Gabor Greifa36791d2009-01-23 19:40:15 +0000412 if (PN->getIncomingBlock(UI) != BB)
Chris Lattner7ae40e72008-04-20 22:11:30 +0000413 return true;
414 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000415 return false;
Chris Lattner7ae40e72008-04-20 22:11:30 +0000416}
417
Chris Lattnerd96288a2008-05-08 17:16:51 +0000418/// mayReadFromMemory - Return true if this instruction may read memory.
419///
420bool Instruction::mayReadFromMemory() const {
421 switch (getOpcode()) {
422 default: return false;
Chris Lattnerd96288a2008-05-08 17:16:51 +0000423 case Instruction::VAArg:
Chris Lattner748118d2008-05-08 21:58:49 +0000424 case Instruction::Load:
Eli Friedman8a552bb2011-07-27 01:08:30 +0000425 case Instruction::Fence: // FIXME: refine definition of mayReadFromMemory
Eli Friedman55ba8162011-07-29 03:05:32 +0000426 case Instruction::AtomicCmpXchg:
427 case Instruction::AtomicRMW:
Chris Lattnerd96288a2008-05-08 17:16:51 +0000428 return true;
429 case Instruction::Call:
430 return !cast<CallInst>(this)->doesNotAccessMemory();
431 case Instruction::Invoke:
432 return !cast<InvokeInst>(this)->doesNotAccessMemory();
Chris Lattner748118d2008-05-08 21:58:49 +0000433 case Instruction::Store:
Eli Friedmane5e77122011-08-15 21:00:18 +0000434 return !cast<StoreInst>(this)->isUnordered();
Chris Lattnerd96288a2008-05-08 17:16:51 +0000435 }
436}
Chris Lattner7ae40e72008-04-20 22:11:30 +0000437
Chris Lattnerbb5493d2007-02-15 23:15:00 +0000438/// mayWriteToMemory - Return true if this instruction may modify memory.
439///
440bool Instruction::mayWriteToMemory() const {
441 switch (getOpcode()) {
442 default: return false;
Eli Friedman8a552bb2011-07-27 01:08:30 +0000443 case Instruction::Fence: // FIXME: refine definition of mayWriteToMemory
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000444 case Instruction::Store:
Chris Lattnerbb5493d2007-02-15 23:15:00 +0000445 case Instruction::VAArg:
Eli Friedman55ba8162011-07-29 03:05:32 +0000446 case Instruction::AtomicCmpXchg:
447 case Instruction::AtomicRMW:
Chris Lattnerbb5493d2007-02-15 23:15:00 +0000448 return true;
449 case Instruction::Call:
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000450 return !cast<CallInst>(this)->onlyReadsMemory();
Chris Lattnerd96288a2008-05-08 17:16:51 +0000451 case Instruction::Invoke:
452 return !cast<InvokeInst>(this)->onlyReadsMemory();
Chris Lattnerbb5493d2007-02-15 23:15:00 +0000453 case Instruction::Load:
Eli Friedmane5e77122011-08-15 21:00:18 +0000454 return !cast<LoadInst>(this)->isUnordered();
Chris Lattnerbb5493d2007-02-15 23:15:00 +0000455 }
456}
Chris Lattnerf2da7242002-10-31 04:14:01 +0000457
Duncan Sands7af1c782009-05-06 06:49:50 +0000458bool Instruction::mayThrow() const {
459 if (const CallInst *CI = dyn_cast<CallInst>(this))
460 return !CI->doesNotThrow();
Bill Wendling55fdb4e2011-08-16 21:15:50 +0000461 return isa<ResumeInst>(this);
Duncan Sands7af1c782009-05-06 06:49:50 +0000462}
463
Nadav Rotem03544ec2013-02-19 20:02:09 +0000464bool Instruction::mayReturn() const {
465 if (const CallInst *CI = dyn_cast<CallInst>(this))
466 return !CI->doesNotReturn();
467 return true;
468}
469
Chris Lattnerf2da7242002-10-31 04:14:01 +0000470/// isAssociative - Return true if the instruction is associative:
471///
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000472/// Associative operators satisfy: x op (y op z) === (x op y) op z
Chris Lattnerf2da7242002-10-31 04:14:01 +0000473///
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000474/// In LLVM, the Add, Mul, And, Or, and Xor operators are associative.
Chris Lattnerf2da7242002-10-31 04:14:01 +0000475///
Duncan Sands0d7ce5f2010-12-20 13:10:23 +0000476bool Instruction::isAssociative(unsigned Opcode) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000477 return Opcode == And || Opcode == Or || Opcode == Xor ||
478 Opcode == Add || Opcode == Mul;
Chris Lattnerf2da7242002-10-31 04:14:01 +0000479}
480
Shuxin Yang9b7f6f22012-11-29 01:47:31 +0000481bool Instruction::isAssociative() const {
482 unsigned Opcode = getOpcode();
483 if (isAssociative(Opcode))
484 return true;
485
486 switch (Opcode) {
487 case FMul:
488 case FAdd:
489 return cast<FPMathOperator>(this)->hasUnsafeAlgebra();
490 default:
491 return false;
492 }
493}
494
Chris Lattnerf2da7242002-10-31 04:14:01 +0000495/// isCommutative - Return true if the instruction is commutative:
496///
Misha Brukman6b634522003-10-10 17:54:14 +0000497/// Commutative operators satisfy: (x op y) === (y op x)
Chris Lattnerf2da7242002-10-31 04:14:01 +0000498///
499/// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
500/// applied to any type.
501///
502bool Instruction::isCommutative(unsigned op) {
503 switch (op) {
504 case Add:
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000505 case FAdd:
Chris Lattnerf2da7242002-10-31 04:14:01 +0000506 case Mul:
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000507 case FMul:
Misha Brukmanfd939082005-04-21 23:48:37 +0000508 case And:
Chris Lattnerf2da7242002-10-31 04:14:01 +0000509 case Or:
510 case Xor:
Chris Lattnerf2da7242002-10-31 04:14:01 +0000511 return true;
512 default:
513 return false;
514 }
515}
Tanya Lattner741bb002003-07-31 04:05:50 +0000516
Duncan Sandsc038a782012-06-12 14:33:56 +0000517/// isIdempotent - Return true if the instruction is idempotent:
518///
519/// Idempotent operators satisfy: x op x === x
520///
521/// In LLVM, the And and Or operators are idempotent.
522///
523bool Instruction::isIdempotent(unsigned Opcode) {
524 return Opcode == And || Opcode == Or;
525}
526
527/// isNilpotent - Return true if the instruction is nilpotent:
528///
529/// Nilpotent operators satisfy: x op x === Id,
530///
531/// where Id is the identity for the operator, i.e. a constant such that
532/// x op Id === x and Id op x === x for all x.
533///
534/// In LLVM, the Xor operator is nilpotent.
535///
536bool Instruction::isNilpotent(unsigned Opcode) {
537 return Opcode == Xor;
538}
539
Devang Patel50b6e332009-10-27 22:16:29 +0000540Instruction *Instruction::clone() const {
541 Instruction *New = clone_impl();
542 New->SubclassOptionalData = SubclassOptionalData;
Chris Lattner508b19a2009-12-29 07:44:16 +0000543 if (!hasMetadata())
544 return New;
Michael Ilseman407a6162012-11-15 22:34:00 +0000545
Chris Lattner508b19a2009-12-29 07:44:16 +0000546 // Otherwise, enumerate and copy over metadata from the old instruction to the
547 // new one.
548 SmallVector<std::pair<unsigned, MDNode*>, 4> TheMDs;
Chris Lattner4baa5102011-07-14 18:57:51 +0000549 getAllMetadataOtherThanDebugLoc(TheMDs);
Chris Lattner508b19a2009-12-29 07:44:16 +0000550 for (unsigned i = 0, e = TheMDs.size(); i != e; ++i)
551 New->setMetadata(TheMDs[i].first, TheMDs[i].second);
Michael Ilseman407a6162012-11-15 22:34:00 +0000552
Chris Lattner4baa5102011-07-14 18:57:51 +0000553 New->setDebugLoc(getDebugLoc());
Devang Patel50b6e332009-10-27 22:16:29 +0000554 return New;
555}