blob: 4208144cb5b5c6df0b174218cb505bdc02eb3058 [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//
10// This file implements the Instruction class for the VMCore library.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner508b19a2009-12-29 07:44:16 +000014#include "llvm/Instruction.h"
Reid Spencer45fb3f32006-11-20 01:22:35 +000015#include "llvm/Type.h"
Chris Lattner38f14552004-11-30 02:51:53 +000016#include "llvm/Instructions.h"
Eli Friedman0b79a772009-07-17 04:28:42 +000017#include "llvm/Constants.h"
Victor Hernandez88d98392009-09-18 19:20:02 +000018#include "llvm/Module.h"
Michael Ilseman125fc7f2012-11-27 00:41:22 +000019#include "llvm/Operator.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
Vikram S. Advec1056452002-07-14 23:09:40 +0000180const char *Instruction::getOpcodeName(unsigned OpCode) {
181 switch (OpCode) {
182 // Terminators
Chris Lattner0513e9f2002-08-14 18:18:02 +0000183 case Ret: return "ret";
184 case Br: return "br";
Vikram S. Advec1056452002-07-14 23:09:40 +0000185 case Switch: return "switch";
Chris Lattnerab21db72009-10-28 00:19:10 +0000186 case IndirectBr: return "indirectbr";
Vikram S. Advec1056452002-07-14 23:09:40 +0000187 case Invoke: return "invoke";
Bill Wendlingdccc03b2011-07-31 06:30:59 +0000188 case Resume: return "resume";
Chris Lattnerb976e662004-10-16 18:08:06 +0000189 case Unreachable: return "unreachable";
Misha Brukmanfd939082005-04-21 23:48:37 +0000190
Vikram S. Advec1056452002-07-14 23:09:40 +0000191 // Standard binary operators...
192 case Add: return "add";
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000193 case FAdd: return "fadd";
Vikram S. Advec1056452002-07-14 23:09:40 +0000194 case Sub: return "sub";
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000195 case FSub: return "fsub";
Vikram S. Advec1056452002-07-14 23:09:40 +0000196 case Mul: return "mul";
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000197 case FMul: return "fmul";
Reid Spencer1628cec2006-10-26 06:15:43 +0000198 case UDiv: return "udiv";
199 case SDiv: return "sdiv";
200 case FDiv: return "fdiv";
Reid Spencer0a783f72006-11-02 01:53:59 +0000201 case URem: return "urem";
202 case SRem: return "srem";
203 case FRem: return "frem";
Vikram S. Advec1056452002-07-14 23:09:40 +0000204
205 // Logical operators...
206 case And: return "and";
207 case Or : return "or";
208 case Xor: return "xor";
209
Vikram S. Advec1056452002-07-14 23:09:40 +0000210 // Memory instructions...
Vikram S. Advec1056452002-07-14 23:09:40 +0000211 case Alloca: return "alloca";
212 case Load: return "load";
213 case Store: return "store";
Eli Friedmanff030482011-07-28 21:48:00 +0000214 case AtomicCmpXchg: return "cmpxchg";
215 case AtomicRMW: return "atomicrmw";
Eli Friedman47f35132011-07-25 23:16:38 +0000216 case Fence: return "fence";
Vikram S. Advec1056452002-07-14 23:09:40 +0000217 case GetElementPtr: return "getelementptr";
Misha Brukmanfd939082005-04-21 23:48:37 +0000218
Reid Spencer3da59db2006-11-27 01:05:10 +0000219 // Convert instructions...
220 case Trunc: return "trunc";
221 case ZExt: return "zext";
222 case SExt: return "sext";
223 case FPTrunc: return "fptrunc";
224 case FPExt: return "fpext";
225 case FPToUI: return "fptoui";
226 case FPToSI: return "fptosi";
227 case UIToFP: return "uitofp";
228 case SIToFP: return "sitofp";
229 case IntToPtr: return "inttoptr";
230 case PtrToInt: return "ptrtoint";
231 case BitCast: return "bitcast";
232
Vikram S. Advec1056452002-07-14 23:09:40 +0000233 // Other instructions...
Reid Spencer74f16422006-12-03 06:27:29 +0000234 case ICmp: return "icmp";
235 case FCmp: return "fcmp";
Reid Spencer3da59db2006-11-27 01:05:10 +0000236 case PHI: return "phi";
237 case Select: return "select";
238 case Call: return "call";
239 case Shl: return "shl";
240 case LShr: return "lshr";
241 case AShr: return "ashr";
242 case VAArg: return "va_arg";
Robert Bocchinob52ee7f2006-01-10 19:05:34 +0000243 case ExtractElement: return "extractelement";
Reid Spencer3da59db2006-11-27 01:05:10 +0000244 case InsertElement: return "insertelement";
245 case ShuffleVector: return "shufflevector";
Matthijs Kooijman74b5e072008-05-30 10:31:54 +0000246 case ExtractValue: return "extractvalue";
247 case InsertValue: return "insertvalue";
Bill Wendlinge6e88262011-08-12 20:24:12 +0000248 case LandingPad: return "landingpad";
Chris Lattner8f77dae2003-05-08 02:44:12 +0000249
Vikram S. Advec1056452002-07-14 23:09:40 +0000250 default: return "<Invalid operator> ";
251 }
Vikram S. Advec1056452002-07-14 23:09:40 +0000252}
Chris Lattnerf2da7242002-10-31 04:14:01 +0000253
Chris Lattner38f14552004-11-30 02:51:53 +0000254/// isIdenticalTo - Return true if the specified instruction is exactly
255/// identical to the current one. This means that all operands match and any
256/// extra information (e.g. load is volatile) agree.
Chris Lattnere3a08842008-11-27 08:39:18 +0000257bool Instruction::isIdenticalTo(const Instruction *I) const {
Dan Gohman75b0eda2009-08-25 22:24:20 +0000258 return isIdenticalToWhenDefined(I) &&
Dan Gohman58cfa3b2009-08-25 22:11:20 +0000259 SubclassOptionalData == I->SubclassOptionalData;
260}
261
Dan Gohman75b0eda2009-08-25 22:24:20 +0000262/// isIdenticalToWhenDefined - This is like isIdenticalTo, except that it
Dan Gohman58cfa3b2009-08-25 22:11:20 +0000263/// ignores the SubclassOptionalData flags, which specify conditions
264/// under which the instruction's result is undefined.
265bool Instruction::isIdenticalToWhenDefined(const Instruction *I) const {
Chris Lattner38f14552004-11-30 02:51:53 +0000266 if (getOpcode() != I->getOpcode() ||
267 getNumOperands() != I->getNumOperands() ||
268 getType() != I->getType())
269 return false;
270
271 // We have two instructions of identical opcode and #operands. Check to see
272 // if all operands are the same.
273 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
274 if (getOperand(i) != I->getOperand(i))
275 return false;
276
277 // Check special state that is a part of some instructions.
278 if (const LoadInst *LI = dyn_cast<LoadInst>(this))
Dan Gohman9a8af452008-10-16 01:24:45 +0000279 return LI->isVolatile() == cast<LoadInst>(I)->isVolatile() &&
Eli Friedmane5e77122011-08-15 21:00:18 +0000280 LI->getAlignment() == cast<LoadInst>(I)->getAlignment() &&
281 LI->getOrdering() == cast<LoadInst>(I)->getOrdering() &&
282 LI->getSynchScope() == cast<LoadInst>(I)->getSynchScope();
Chris Lattner38f14552004-11-30 02:51:53 +0000283 if (const StoreInst *SI = dyn_cast<StoreInst>(this))
Dan Gohman9a8af452008-10-16 01:24:45 +0000284 return SI->isVolatile() == cast<StoreInst>(I)->isVolatile() &&
Eli Friedmane5e77122011-08-15 21:00:18 +0000285 SI->getAlignment() == cast<StoreInst>(I)->getAlignment() &&
286 SI->getOrdering() == cast<StoreInst>(I)->getOrdering() &&
287 SI->getSynchScope() == cast<StoreInst>(I)->getSynchScope();
Reid Spencere4d87aa2006-12-23 06:05:41 +0000288 if (const CmpInst *CI = dyn_cast<CmpInst>(this))
289 return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
Chris Lattnerddb6db42005-05-06 05:51:46 +0000290 if (const CallInst *CI = dyn_cast<CallInst>(this))
Dan Gohman9a8af452008-10-16 01:24:45 +0000291 return CI->isTailCall() == cast<CallInst>(I)->isTailCall() &&
292 CI->getCallingConv() == cast<CallInst>(I)->getCallingConv() &&
Nick Lewyckyf6c63c22011-01-26 09:23:19 +0000293 CI->getAttributes() == cast<CallInst>(I)->getAttributes();
Dan Gohman9a8af452008-10-16 01:24:45 +0000294 if (const InvokeInst *CI = dyn_cast<InvokeInst>(this))
Nick Lewycky41fe88b2008-10-27 07:28:44 +0000295 return CI->getCallingConv() == cast<InvokeInst>(I)->getCallingConv() &&
Nick Lewyckyf6c63c22011-01-26 09:23:19 +0000296 CI->getAttributes() == cast<InvokeInst>(I)->getAttributes();
Jay Foadfc6d3a42011-07-13 10:26:04 +0000297 if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(this))
298 return IVI->getIndices() == cast<InsertValueInst>(I)->getIndices();
299 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(this))
300 return EVI->getIndices() == cast<ExtractValueInst>(I)->getIndices();
Eli Friedman8a552bb2011-07-27 01:08:30 +0000301 if (const FenceInst *FI = dyn_cast<FenceInst>(this))
302 return FI->getOrdering() == cast<FenceInst>(FI)->getOrdering() &&
303 FI->getSynchScope() == cast<FenceInst>(FI)->getSynchScope();
Eli Friedman55ba8162011-07-29 03:05:32 +0000304 if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(this))
305 return CXI->isVolatile() == cast<AtomicCmpXchgInst>(I)->isVolatile() &&
306 CXI->getOrdering() == cast<AtomicCmpXchgInst>(I)->getOrdering() &&
307 CXI->getSynchScope() == cast<AtomicCmpXchgInst>(I)->getSynchScope();
308 if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(this))
309 return RMWI->getOperation() == cast<AtomicRMWInst>(I)->getOperation() &&
310 RMWI->isVolatile() == cast<AtomicRMWInst>(I)->isVolatile() &&
311 RMWI->getOrdering() == cast<AtomicRMWInst>(I)->getOrdering() &&
312 RMWI->getSynchScope() == cast<AtomicRMWInst>(I)->getSynchScope();
Joel Jones9df72a92012-05-10 15:59:41 +0000313 if (const PHINode *thisPHI = dyn_cast<PHINode>(this)) {
314 const PHINode *otherPHI = cast<PHINode>(I);
315 for (unsigned i = 0, e = thisPHI->getNumOperands(); i != e; ++i) {
316 if (thisPHI->getIncomingBlock(i) != otherPHI->getIncomingBlock(i))
317 return false;
318 }
319 return true;
320 }
Chris Lattner38f14552004-11-30 02:51:53 +0000321 return true;
322}
323
Reid Spencere4d87aa2006-12-23 06:05:41 +0000324// isSameOperationAs
Dan Gohman194ae782009-06-12 19:03:05 +0000325// This should be kept in sync with isEquivalentOperation in
326// lib/Transforms/IPO/MergeFunctions.cpp.
Hal Finkelec4e85e2012-06-28 05:42:26 +0000327bool Instruction::isSameOperationAs(const Instruction *I,
328 unsigned flags) const {
329 bool IgnoreAlignment = flags & CompareIgnoringAlignment;
330 bool UseScalarTypes = flags & CompareUsingScalarTypes;
331
Dan Gohman194ae782009-06-12 19:03:05 +0000332 if (getOpcode() != I->getOpcode() ||
333 getNumOperands() != I->getNumOperands() ||
Hal Finkelec4e85e2012-06-28 05:42:26 +0000334 (UseScalarTypes ?
335 getType()->getScalarType() != I->getType()->getScalarType() :
336 getType() != I->getType()))
Reid Spencere4d87aa2006-12-23 06:05:41 +0000337 return false;
338
339 // We have two instructions of identical opcode and #operands. Check to see
340 // if all operands are the same type
341 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Hal Finkelec4e85e2012-06-28 05:42:26 +0000342 if (UseScalarTypes ?
343 getOperand(i)->getType()->getScalarType() !=
344 I->getOperand(i)->getType()->getScalarType() :
345 getOperand(i)->getType() != I->getOperand(i)->getType())
Reid Spencere4d87aa2006-12-23 06:05:41 +0000346 return false;
347
348 // Check special state that is a part of some instructions.
349 if (const LoadInst *LI = dyn_cast<LoadInst>(this))
Dan Gohman9a8af452008-10-16 01:24:45 +0000350 return LI->isVolatile() == cast<LoadInst>(I)->isVolatile() &&
Hal Finkelec4e85e2012-06-28 05:42:26 +0000351 (LI->getAlignment() == cast<LoadInst>(I)->getAlignment() ||
352 IgnoreAlignment) &&
Eli Friedmane5e77122011-08-15 21:00:18 +0000353 LI->getOrdering() == cast<LoadInst>(I)->getOrdering() &&
354 LI->getSynchScope() == cast<LoadInst>(I)->getSynchScope();
Reid Spencere4d87aa2006-12-23 06:05:41 +0000355 if (const StoreInst *SI = dyn_cast<StoreInst>(this))
Dan Gohman9a8af452008-10-16 01:24:45 +0000356 return SI->isVolatile() == cast<StoreInst>(I)->isVolatile() &&
Hal Finkelec4e85e2012-06-28 05:42:26 +0000357 (SI->getAlignment() == cast<StoreInst>(I)->getAlignment() ||
358 IgnoreAlignment) &&
Eli Friedmane5e77122011-08-15 21:00:18 +0000359 SI->getOrdering() == cast<StoreInst>(I)->getOrdering() &&
360 SI->getSynchScope() == cast<StoreInst>(I)->getSynchScope();
Reid Spencere4d87aa2006-12-23 06:05:41 +0000361 if (const CmpInst *CI = dyn_cast<CmpInst>(this))
362 return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
363 if (const CallInst *CI = dyn_cast<CallInst>(this))
Dan Gohman9a8af452008-10-16 01:24:45 +0000364 return CI->isTailCall() == cast<CallInst>(I)->isTailCall() &&
365 CI->getCallingConv() == cast<CallInst>(I)->getCallingConv() &&
Nick Lewyckyf6c63c22011-01-26 09:23:19 +0000366 CI->getAttributes() == cast<CallInst>(I)->getAttributes();
Dan Gohman9a8af452008-10-16 01:24:45 +0000367 if (const InvokeInst *CI = dyn_cast<InvokeInst>(this))
Nick Lewycky41fe88b2008-10-27 07:28:44 +0000368 return CI->getCallingConv() == cast<InvokeInst>(I)->getCallingConv() &&
Nick Lewyckyf6c63c22011-01-26 09:23:19 +0000369 CI->getAttributes() ==
370 cast<InvokeInst>(I)->getAttributes();
Jay Foadfc6d3a42011-07-13 10:26:04 +0000371 if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(this))
372 return IVI->getIndices() == cast<InsertValueInst>(I)->getIndices();
373 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(this))
374 return EVI->getIndices() == cast<ExtractValueInst>(I)->getIndices();
Eli Friedman8a552bb2011-07-27 01:08:30 +0000375 if (const FenceInst *FI = dyn_cast<FenceInst>(this))
Eli Friedman55ba8162011-07-29 03:05:32 +0000376 return FI->getOrdering() == cast<FenceInst>(I)->getOrdering() &&
377 FI->getSynchScope() == cast<FenceInst>(I)->getSynchScope();
378 if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(this))
379 return CXI->isVolatile() == cast<AtomicCmpXchgInst>(I)->isVolatile() &&
380 CXI->getOrdering() == cast<AtomicCmpXchgInst>(I)->getOrdering() &&
381 CXI->getSynchScope() == cast<AtomicCmpXchgInst>(I)->getSynchScope();
382 if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(this))
383 return RMWI->getOperation() == cast<AtomicRMWInst>(I)->getOperation() &&
384 RMWI->isVolatile() == cast<AtomicRMWInst>(I)->isVolatile() &&
385 RMWI->getOrdering() == cast<AtomicRMWInst>(I)->getOrdering() &&
386 RMWI->getSynchScope() == cast<AtomicRMWInst>(I)->getSynchScope();
Reid Spencere4d87aa2006-12-23 06:05:41 +0000387
388 return true;
389}
390
Chris Lattner7ae40e72008-04-20 22:11:30 +0000391/// isUsedOutsideOfBlock - Return true if there are any uses of I outside of the
392/// specified block. Note that PHI nodes are considered to evaluate their
393/// operands in the corresponding predecessor block.
394bool Instruction::isUsedOutsideOfBlock(const BasicBlock *BB) const {
Gabor Greif60ad7812010-03-25 23:06:16 +0000395 for (const_use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Chris Lattner7ae40e72008-04-20 22:11:30 +0000396 // PHI nodes uses values in the corresponding predecessor block. For other
397 // instructions, just check to see whether the parent of the use matches up.
Gabor Greifa8b9df72010-07-12 10:36:48 +0000398 const User *U = *UI;
399 const PHINode *PN = dyn_cast<PHINode>(U);
Chris Lattner7ae40e72008-04-20 22:11:30 +0000400 if (PN == 0) {
Gabor Greifa8b9df72010-07-12 10:36:48 +0000401 if (cast<Instruction>(U)->getParent() != BB)
Chris Lattner7ae40e72008-04-20 22:11:30 +0000402 return true;
403 continue;
404 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000405
Gabor Greifa36791d2009-01-23 19:40:15 +0000406 if (PN->getIncomingBlock(UI) != BB)
Chris Lattner7ae40e72008-04-20 22:11:30 +0000407 return true;
408 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000409 return false;
Chris Lattner7ae40e72008-04-20 22:11:30 +0000410}
411
Chris Lattnerd96288a2008-05-08 17:16:51 +0000412/// mayReadFromMemory - Return true if this instruction may read memory.
413///
414bool Instruction::mayReadFromMemory() const {
415 switch (getOpcode()) {
416 default: return false;
Chris Lattnerd96288a2008-05-08 17:16:51 +0000417 case Instruction::VAArg:
Chris Lattner748118d2008-05-08 21:58:49 +0000418 case Instruction::Load:
Eli Friedman8a552bb2011-07-27 01:08:30 +0000419 case Instruction::Fence: // FIXME: refine definition of mayReadFromMemory
Eli Friedman55ba8162011-07-29 03:05:32 +0000420 case Instruction::AtomicCmpXchg:
421 case Instruction::AtomicRMW:
Chris Lattnerd96288a2008-05-08 17:16:51 +0000422 return true;
423 case Instruction::Call:
424 return !cast<CallInst>(this)->doesNotAccessMemory();
425 case Instruction::Invoke:
426 return !cast<InvokeInst>(this)->doesNotAccessMemory();
Chris Lattner748118d2008-05-08 21:58:49 +0000427 case Instruction::Store:
Eli Friedmane5e77122011-08-15 21:00:18 +0000428 return !cast<StoreInst>(this)->isUnordered();
Chris Lattnerd96288a2008-05-08 17:16:51 +0000429 }
430}
Chris Lattner7ae40e72008-04-20 22:11:30 +0000431
Chris Lattnerbb5493d2007-02-15 23:15:00 +0000432/// mayWriteToMemory - Return true if this instruction may modify memory.
433///
434bool Instruction::mayWriteToMemory() const {
435 switch (getOpcode()) {
436 default: return false;
Eli Friedman8a552bb2011-07-27 01:08:30 +0000437 case Instruction::Fence: // FIXME: refine definition of mayWriteToMemory
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000438 case Instruction::Store:
Chris Lattnerbb5493d2007-02-15 23:15:00 +0000439 case Instruction::VAArg:
Eli Friedman55ba8162011-07-29 03:05:32 +0000440 case Instruction::AtomicCmpXchg:
441 case Instruction::AtomicRMW:
Chris Lattnerbb5493d2007-02-15 23:15:00 +0000442 return true;
443 case Instruction::Call:
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000444 return !cast<CallInst>(this)->onlyReadsMemory();
Chris Lattnerd96288a2008-05-08 17:16:51 +0000445 case Instruction::Invoke:
446 return !cast<InvokeInst>(this)->onlyReadsMemory();
Chris Lattnerbb5493d2007-02-15 23:15:00 +0000447 case Instruction::Load:
Eli Friedmane5e77122011-08-15 21:00:18 +0000448 return !cast<LoadInst>(this)->isUnordered();
Chris Lattnerbb5493d2007-02-15 23:15:00 +0000449 }
450}
Chris Lattnerf2da7242002-10-31 04:14:01 +0000451
Duncan Sands7af1c782009-05-06 06:49:50 +0000452/// mayThrow - Return true if this instruction may throw an exception.
453///
454bool Instruction::mayThrow() const {
455 if (const CallInst *CI = dyn_cast<CallInst>(this))
456 return !CI->doesNotThrow();
Bill Wendling55fdb4e2011-08-16 21:15:50 +0000457 return isa<ResumeInst>(this);
Duncan Sands7af1c782009-05-06 06:49:50 +0000458}
459
Chris Lattnerf2da7242002-10-31 04:14:01 +0000460/// isAssociative - Return true if the instruction is associative:
461///
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000462/// Associative operators satisfy: x op (y op z) === (x op y) op z
Chris Lattnerf2da7242002-10-31 04:14:01 +0000463///
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000464/// In LLVM, the Add, Mul, And, Or, and Xor operators are associative.
Chris Lattnerf2da7242002-10-31 04:14:01 +0000465///
Duncan Sands0d7ce5f2010-12-20 13:10:23 +0000466bool Instruction::isAssociative(unsigned Opcode) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000467 return Opcode == And || Opcode == Or || Opcode == Xor ||
468 Opcode == Add || Opcode == Mul;
Chris Lattnerf2da7242002-10-31 04:14:01 +0000469}
470
471/// isCommutative - Return true if the instruction is commutative:
472///
Misha Brukman6b634522003-10-10 17:54:14 +0000473/// Commutative operators satisfy: (x op y) === (y op x)
Chris Lattnerf2da7242002-10-31 04:14:01 +0000474///
475/// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
476/// applied to any type.
477///
478bool Instruction::isCommutative(unsigned op) {
479 switch (op) {
480 case Add:
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000481 case FAdd:
Chris Lattnerf2da7242002-10-31 04:14:01 +0000482 case Mul:
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000483 case FMul:
Misha Brukmanfd939082005-04-21 23:48:37 +0000484 case And:
Chris Lattnerf2da7242002-10-31 04:14:01 +0000485 case Or:
486 case Xor:
Chris Lattnerf2da7242002-10-31 04:14:01 +0000487 return true;
488 default:
489 return false;
490 }
491}
Tanya Lattner741bb002003-07-31 04:05:50 +0000492
Duncan Sandsc038a782012-06-12 14:33:56 +0000493/// isIdempotent - Return true if the instruction is idempotent:
494///
495/// Idempotent operators satisfy: x op x === x
496///
497/// In LLVM, the And and Or operators are idempotent.
498///
499bool Instruction::isIdempotent(unsigned Opcode) {
500 return Opcode == And || Opcode == Or;
501}
502
503/// isNilpotent - Return true if the instruction is nilpotent:
504///
505/// Nilpotent operators satisfy: x op x === Id,
506///
507/// where Id is the identity for the operator, i.e. a constant such that
508/// x op Id === x and Id op x === x for all x.
509///
510/// In LLVM, the Xor operator is nilpotent.
511///
512bool Instruction::isNilpotent(unsigned Opcode) {
513 return Opcode == Xor;
514}
515
Devang Patel50b6e332009-10-27 22:16:29 +0000516Instruction *Instruction::clone() const {
517 Instruction *New = clone_impl();
518 New->SubclassOptionalData = SubclassOptionalData;
Chris Lattner508b19a2009-12-29 07:44:16 +0000519 if (!hasMetadata())
520 return New;
Michael Ilseman407a6162012-11-15 22:34:00 +0000521
Chris Lattner508b19a2009-12-29 07:44:16 +0000522 // Otherwise, enumerate and copy over metadata from the old instruction to the
523 // new one.
524 SmallVector<std::pair<unsigned, MDNode*>, 4> TheMDs;
Chris Lattner4baa5102011-07-14 18:57:51 +0000525 getAllMetadataOtherThanDebugLoc(TheMDs);
Chris Lattner508b19a2009-12-29 07:44:16 +0000526 for (unsigned i = 0, e = TheMDs.size(); i != e; ++i)
527 New->setMetadata(TheMDs[i].first, TheMDs[i].second);
Michael Ilseman407a6162012-11-15 22:34:00 +0000528
Chris Lattner4baa5102011-07-14 18:57:51 +0000529 New->setDebugLoc(getDebugLoc());
Devang Patel50b6e332009-10-27 22:16:29 +0000530 return New;
531}