blob: a7773c47168ee849f32895f93aef15940ae464be [file] [log] [blame]
Chris Lattner44d2c352003-10-13 03:32:08 +00001//===-- Instruction.cpp - Implement the Instruction class -----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chandler Carruthef860a22013-01-02 09:10:48 +000010// This file implements the Instruction class for the IR library.
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth9fb823b2013-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 Sands38ef3a82007-12-03 20:06:50 +000020#include "llvm/Support/CallSite.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000021#include "llvm/Support/LeakDetector.h"
Chris Lattner0e03ab62003-11-20 17:45:12 +000022using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000023
Chris Lattner229907c2011-07-18 04:54:35 +000024Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,
Chris Lattner2195fc42007-02-24 00:55:48 +000025 Instruction *InsertBefore)
Chris Lattnerc0f5ce32010-04-01 05:23:13 +000026 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) {
Chris Lattner184b2982002-09-08 18:59:35 +000027 // Make sure that we get added to a basicblock
28 LeakDetector::addGarbageObject(this);
Chris Lattner3c787442002-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 Lattner2f7c9632001-06-06 20:29:01 +000036}
37
Chris Lattner229907c2011-07-18 04:54:35 +000038Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,
Chris Lattner2195fc42007-02-24 00:55:48 +000039 BasicBlock *InsertAtEnd)
Chris Lattnerc0f5ce32010-04-01 05:23:13 +000040 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) {
Chris Lattner5d1bc2c2005-01-29 00:35:33 +000041 // Make sure that we get added to a basicblock
42 LeakDetector::addGarbageObject(this);
Alkis Evlogimenos9f0fdf72004-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 Lattner0f048162007-02-13 07:54:42 +000047}
48
49
Chris Lattner1c12a882006-06-21 16:53:47 +000050// Out of line virtual method, so the vtable, etc has a home.
Gordon Henriksen14a55692007-12-10 02:14:30 +000051Instruction::~Instruction() {
Devang Patel4e6f2e42009-09-24 16:19:11 +000052 assert(Parent == 0 && "Instruction still linked in the program!");
Dan Gohman48a995f2010-07-20 22:25:04 +000053 if (hasMetadataHashEntry())
54 clearMetadataHashEntries();
Chris Lattner1c12a882006-06-21 16:53:47 +000055}
56
57
Chris Lattner9ed7aef2002-09-06 21:33:15 +000058void Instruction::setParent(BasicBlock *P) {
Chris Lattnerd8a232b2004-02-04 01:06:38 +000059 if (getParent()) {
60 if (!P) LeakDetector::addGarbageObject(this);
61 } else {
62 if (P) LeakDetector::removeGarbageObject(this);
63 }
Chris Lattner184b2982002-09-08 18:59:35 +000064
Chris Lattner9ed7aef2002-09-06 21:33:15 +000065 Parent = P;
66}
67
Chris Lattner02a71e72004-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. Adve8aee7962002-07-14 23:09:40 +000075
Owen Anderson2505e0c2008-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 Lattner6e66d0a2009-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 Lattner24a0a432005-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 Ilseman149209e2012-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 Lattner24a0a432005-08-08 05:21:50 +0000179
Michael Ilseman05d3bf77a2012-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. Adve8aee7962002-07-14 23:09:40 +0000186const char *Instruction::getOpcodeName(unsigned OpCode) {
187 switch (OpCode) {
188 // Terminators
Chris Lattnerb193ff82002-08-14 18:18:02 +0000189 case Ret: return "ret";
190 case Br: return "br";
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000191 case Switch: return "switch";
Chris Lattnerd04cb6d2009-10-28 00:19:10 +0000192 case IndirectBr: return "indirectbr";
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000193 case Invoke: return "invoke";
Bill Wendlingf891bf82011-07-31 06:30:59 +0000194 case Resume: return "resume";
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000195 case Unreachable: return "unreachable";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000196
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000197 // Standard binary operators...
198 case Add: return "add";
Dan Gohmana5b96452009-06-04 22:49:04 +0000199 case FAdd: return "fadd";
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000200 case Sub: return "sub";
Dan Gohmana5b96452009-06-04 22:49:04 +0000201 case FSub: return "fsub";
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000202 case Mul: return "mul";
Dan Gohmana5b96452009-06-04 22:49:04 +0000203 case FMul: return "fmul";
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000204 case UDiv: return "udiv";
205 case SDiv: return "sdiv";
206 case FDiv: return "fdiv";
Reid Spencer7eb55b32006-11-02 01:53:59 +0000207 case URem: return "urem";
208 case SRem: return "srem";
209 case FRem: return "frem";
Vikram S. Adve8aee7962002-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. Adve8aee7962002-07-14 23:09:40 +0000216 // Memory instructions...
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000217 case Alloca: return "alloca";
218 case Load: return "load";
219 case Store: return "store";
Eli Friedmanc9a551e2011-07-28 21:48:00 +0000220 case AtomicCmpXchg: return "cmpxchg";
221 case AtomicRMW: return "atomicrmw";
Eli Friedmanfee02c62011-07-25 23:16:38 +0000222 case Fence: return "fence";
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000223 case GetElementPtr: return "getelementptr";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000224
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000225 // Convert instructions...
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +0000226 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 case AddrSpaceCast: return "addrspacecast";
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000239
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000240 // Other instructions...
Reid Spencer45e52392006-12-03 06:27:29 +0000241 case ICmp: return "icmp";
242 case FCmp: return "fcmp";
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000243 case PHI: return "phi";
244 case Select: return "select";
245 case Call: return "call";
246 case Shl: return "shl";
247 case LShr: return "lshr";
248 case AShr: return "ashr";
249 case VAArg: return "va_arg";
Robert Bocchino23004482006-01-10 19:05:34 +0000250 case ExtractElement: return "extractelement";
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000251 case InsertElement: return "insertelement";
252 case ShuffleVector: return "shufflevector";
Matthijs Kooijmanbf8d6ce2008-05-30 10:31:54 +0000253 case ExtractValue: return "extractvalue";
254 case InsertValue: return "insertvalue";
Bill Wendlingfae14752011-08-12 20:24:12 +0000255 case LandingPad: return "landingpad";
Chris Lattnerf70da102003-05-08 02:44:12 +0000256
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000257 default: return "<Invalid operator> ";
258 }
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000259}
Chris Lattnercab6c332002-10-31 04:14:01 +0000260
Chris Lattner0b6ebd8d2004-11-30 02:51:53 +0000261/// isIdenticalTo - Return true if the specified instruction is exactly
262/// identical to the current one. This means that all operands match and any
263/// extra information (e.g. load is volatile) agree.
Chris Lattner378b0412008-11-27 08:39:18 +0000264bool Instruction::isIdenticalTo(const Instruction *I) const {
Dan Gohman23d2c762009-08-25 22:24:20 +0000265 return isIdenticalToWhenDefined(I) &&
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000266 SubclassOptionalData == I->SubclassOptionalData;
267}
268
Dan Gohman23d2c762009-08-25 22:24:20 +0000269/// isIdenticalToWhenDefined - This is like isIdenticalTo, except that it
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000270/// ignores the SubclassOptionalData flags, which specify conditions
271/// under which the instruction's result is undefined.
272bool Instruction::isIdenticalToWhenDefined(const Instruction *I) const {
Chris Lattner0b6ebd8d2004-11-30 02:51:53 +0000273 if (getOpcode() != I->getOpcode() ||
274 getNumOperands() != I->getNumOperands() ||
275 getType() != I->getType())
276 return false;
277
278 // We have two instructions of identical opcode and #operands. Check to see
279 // if all operands are the same.
280 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
281 if (getOperand(i) != I->getOperand(i))
282 return false;
283
284 // Check special state that is a part of some instructions.
285 if (const LoadInst *LI = dyn_cast<LoadInst>(this))
Dan Gohmanfc27e252008-10-16 01:24:45 +0000286 return LI->isVolatile() == cast<LoadInst>(I)->isVolatile() &&
Eli Friedmanb9d5a632011-08-15 21:00:18 +0000287 LI->getAlignment() == cast<LoadInst>(I)->getAlignment() &&
288 LI->getOrdering() == cast<LoadInst>(I)->getOrdering() &&
289 LI->getSynchScope() == cast<LoadInst>(I)->getSynchScope();
Chris Lattner0b6ebd8d2004-11-30 02:51:53 +0000290 if (const StoreInst *SI = dyn_cast<StoreInst>(this))
Dan Gohmanfc27e252008-10-16 01:24:45 +0000291 return SI->isVolatile() == cast<StoreInst>(I)->isVolatile() &&
Eli Friedmanb9d5a632011-08-15 21:00:18 +0000292 SI->getAlignment() == cast<StoreInst>(I)->getAlignment() &&
293 SI->getOrdering() == cast<StoreInst>(I)->getOrdering() &&
294 SI->getSynchScope() == cast<StoreInst>(I)->getSynchScope();
Reid Spencer266e42b2006-12-23 06:05:41 +0000295 if (const CmpInst *CI = dyn_cast<CmpInst>(this))
296 return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
Chris Lattner06038452005-05-06 05:51:46 +0000297 if (const CallInst *CI = dyn_cast<CallInst>(this))
Dan Gohmanfc27e252008-10-16 01:24:45 +0000298 return CI->isTailCall() == cast<CallInst>(I)->isTailCall() &&
299 CI->getCallingConv() == cast<CallInst>(I)->getCallingConv() &&
Nick Lewycky91543442011-01-26 09:23:19 +0000300 CI->getAttributes() == cast<CallInst>(I)->getAttributes();
Dan Gohmanfc27e252008-10-16 01:24:45 +0000301 if (const InvokeInst *CI = dyn_cast<InvokeInst>(this))
Nick Lewyckyc5a68d22008-10-27 07:28:44 +0000302 return CI->getCallingConv() == cast<InvokeInst>(I)->getCallingConv() &&
Nick Lewycky91543442011-01-26 09:23:19 +0000303 CI->getAttributes() == cast<InvokeInst>(I)->getAttributes();
Jay Foad57aa6362011-07-13 10:26:04 +0000304 if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(this))
305 return IVI->getIndices() == cast<InsertValueInst>(I)->getIndices();
306 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(this))
307 return EVI->getIndices() == cast<ExtractValueInst>(I)->getIndices();
Eli Friedman89b694b2011-07-27 01:08:30 +0000308 if (const FenceInst *FI = dyn_cast<FenceInst>(this))
309 return FI->getOrdering() == cast<FenceInst>(FI)->getOrdering() &&
310 FI->getSynchScope() == cast<FenceInst>(FI)->getSynchScope();
Eli Friedmanadec5872011-07-29 03:05:32 +0000311 if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(this))
312 return CXI->isVolatile() == cast<AtomicCmpXchgInst>(I)->isVolatile() &&
313 CXI->getOrdering() == cast<AtomicCmpXchgInst>(I)->getOrdering() &&
314 CXI->getSynchScope() == cast<AtomicCmpXchgInst>(I)->getSynchScope();
315 if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(this))
316 return RMWI->getOperation() == cast<AtomicRMWInst>(I)->getOperation() &&
317 RMWI->isVolatile() == cast<AtomicRMWInst>(I)->isVolatile() &&
318 RMWI->getOrdering() == cast<AtomicRMWInst>(I)->getOrdering() &&
319 RMWI->getSynchScope() == cast<AtomicRMWInst>(I)->getSynchScope();
Joel Jones3d90a9a2012-05-10 15:59:41 +0000320 if (const PHINode *thisPHI = dyn_cast<PHINode>(this)) {
321 const PHINode *otherPHI = cast<PHINode>(I);
322 for (unsigned i = 0, e = thisPHI->getNumOperands(); i != e; ++i) {
323 if (thisPHI->getIncomingBlock(i) != otherPHI->getIncomingBlock(i))
324 return false;
325 }
326 return true;
327 }
Chris Lattner0b6ebd8d2004-11-30 02:51:53 +0000328 return true;
329}
330
Reid Spencer266e42b2006-12-23 06:05:41 +0000331// isSameOperationAs
Dan Gohman17fb0d22009-06-12 19:03:05 +0000332// This should be kept in sync with isEquivalentOperation in
333// lib/Transforms/IPO/MergeFunctions.cpp.
Hal Finkel74e52252012-06-28 05:42:26 +0000334bool Instruction::isSameOperationAs(const Instruction *I,
335 unsigned flags) const {
336 bool IgnoreAlignment = flags & CompareIgnoringAlignment;
337 bool UseScalarTypes = flags & CompareUsingScalarTypes;
338
Dan Gohman17fb0d22009-06-12 19:03:05 +0000339 if (getOpcode() != I->getOpcode() ||
340 getNumOperands() != I->getNumOperands() ||
Hal Finkel74e52252012-06-28 05:42:26 +0000341 (UseScalarTypes ?
342 getType()->getScalarType() != I->getType()->getScalarType() :
343 getType() != I->getType()))
Reid Spencer266e42b2006-12-23 06:05:41 +0000344 return false;
345
346 // We have two instructions of identical opcode and #operands. Check to see
347 // if all operands are the same type
348 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Hal Finkel74e52252012-06-28 05:42:26 +0000349 if (UseScalarTypes ?
350 getOperand(i)->getType()->getScalarType() !=
351 I->getOperand(i)->getType()->getScalarType() :
352 getOperand(i)->getType() != I->getOperand(i)->getType())
Reid Spencer266e42b2006-12-23 06:05:41 +0000353 return false;
354
355 // Check special state that is a part of some instructions.
356 if (const LoadInst *LI = dyn_cast<LoadInst>(this))
Dan Gohmanfc27e252008-10-16 01:24:45 +0000357 return LI->isVolatile() == cast<LoadInst>(I)->isVolatile() &&
Hal Finkel74e52252012-06-28 05:42:26 +0000358 (LI->getAlignment() == cast<LoadInst>(I)->getAlignment() ||
359 IgnoreAlignment) &&
Eli Friedmanb9d5a632011-08-15 21:00:18 +0000360 LI->getOrdering() == cast<LoadInst>(I)->getOrdering() &&
361 LI->getSynchScope() == cast<LoadInst>(I)->getSynchScope();
Reid Spencer266e42b2006-12-23 06:05:41 +0000362 if (const StoreInst *SI = dyn_cast<StoreInst>(this))
Dan Gohmanfc27e252008-10-16 01:24:45 +0000363 return SI->isVolatile() == cast<StoreInst>(I)->isVolatile() &&
Hal Finkel74e52252012-06-28 05:42:26 +0000364 (SI->getAlignment() == cast<StoreInst>(I)->getAlignment() ||
365 IgnoreAlignment) &&
Eli Friedmanb9d5a632011-08-15 21:00:18 +0000366 SI->getOrdering() == cast<StoreInst>(I)->getOrdering() &&
367 SI->getSynchScope() == cast<StoreInst>(I)->getSynchScope();
Reid Spencer266e42b2006-12-23 06:05:41 +0000368 if (const CmpInst *CI = dyn_cast<CmpInst>(this))
369 return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
370 if (const CallInst *CI = dyn_cast<CallInst>(this))
Dan Gohmanfc27e252008-10-16 01:24:45 +0000371 return CI->isTailCall() == cast<CallInst>(I)->isTailCall() &&
372 CI->getCallingConv() == cast<CallInst>(I)->getCallingConv() &&
Nick Lewycky91543442011-01-26 09:23:19 +0000373 CI->getAttributes() == cast<CallInst>(I)->getAttributes();
Dan Gohmanfc27e252008-10-16 01:24:45 +0000374 if (const InvokeInst *CI = dyn_cast<InvokeInst>(this))
Nick Lewyckyc5a68d22008-10-27 07:28:44 +0000375 return CI->getCallingConv() == cast<InvokeInst>(I)->getCallingConv() &&
Nick Lewycky91543442011-01-26 09:23:19 +0000376 CI->getAttributes() ==
377 cast<InvokeInst>(I)->getAttributes();
Jay Foad57aa6362011-07-13 10:26:04 +0000378 if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(this))
379 return IVI->getIndices() == cast<InsertValueInst>(I)->getIndices();
380 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(this))
381 return EVI->getIndices() == cast<ExtractValueInst>(I)->getIndices();
Eli Friedman89b694b2011-07-27 01:08:30 +0000382 if (const FenceInst *FI = dyn_cast<FenceInst>(this))
Eli Friedmanadec5872011-07-29 03:05:32 +0000383 return FI->getOrdering() == cast<FenceInst>(I)->getOrdering() &&
384 FI->getSynchScope() == cast<FenceInst>(I)->getSynchScope();
385 if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(this))
386 return CXI->isVolatile() == cast<AtomicCmpXchgInst>(I)->isVolatile() &&
387 CXI->getOrdering() == cast<AtomicCmpXchgInst>(I)->getOrdering() &&
388 CXI->getSynchScope() == cast<AtomicCmpXchgInst>(I)->getSynchScope();
389 if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(this))
390 return RMWI->getOperation() == cast<AtomicRMWInst>(I)->getOperation() &&
391 RMWI->isVolatile() == cast<AtomicRMWInst>(I)->isVolatile() &&
392 RMWI->getOrdering() == cast<AtomicRMWInst>(I)->getOrdering() &&
393 RMWI->getSynchScope() == cast<AtomicRMWInst>(I)->getSynchScope();
Reid Spencer266e42b2006-12-23 06:05:41 +0000394
395 return true;
396}
397
Chris Lattner8ec6e8a2008-04-20 22:11:30 +0000398/// isUsedOutsideOfBlock - Return true if there are any uses of I outside of the
399/// specified block. Note that PHI nodes are considered to evaluate their
400/// operands in the corresponding predecessor block.
401bool Instruction::isUsedOutsideOfBlock(const BasicBlock *BB) const {
Gabor Greifc78d7202010-03-25 23:06:16 +0000402 for (const_use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Chris Lattner8ec6e8a2008-04-20 22:11:30 +0000403 // PHI nodes uses values in the corresponding predecessor block. For other
404 // instructions, just check to see whether the parent of the use matches up.
Gabor Greif2a464d72010-07-12 10:36:48 +0000405 const User *U = *UI;
406 const PHINode *PN = dyn_cast<PHINode>(U);
Chris Lattner8ec6e8a2008-04-20 22:11:30 +0000407 if (PN == 0) {
Gabor Greif2a464d72010-07-12 10:36:48 +0000408 if (cast<Instruction>(U)->getParent() != BB)
Chris Lattner8ec6e8a2008-04-20 22:11:30 +0000409 return true;
410 continue;
411 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000412
Gabor Greifeb61fcf2009-01-23 19:40:15 +0000413 if (PN->getIncomingBlock(UI) != BB)
Chris Lattner8ec6e8a2008-04-20 22:11:30 +0000414 return true;
415 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000416 return false;
Chris Lattner8ec6e8a2008-04-20 22:11:30 +0000417}
418
Chris Lattner84627112008-05-08 17:16:51 +0000419/// mayReadFromMemory - Return true if this instruction may read memory.
420///
421bool Instruction::mayReadFromMemory() const {
422 switch (getOpcode()) {
423 default: return false;
Chris Lattner84627112008-05-08 17:16:51 +0000424 case Instruction::VAArg:
Chris Lattner954907a2008-05-08 21:58:49 +0000425 case Instruction::Load:
Eli Friedman89b694b2011-07-27 01:08:30 +0000426 case Instruction::Fence: // FIXME: refine definition of mayReadFromMemory
Eli Friedmanadec5872011-07-29 03:05:32 +0000427 case Instruction::AtomicCmpXchg:
428 case Instruction::AtomicRMW:
Chris Lattner84627112008-05-08 17:16:51 +0000429 return true;
430 case Instruction::Call:
431 return !cast<CallInst>(this)->doesNotAccessMemory();
432 case Instruction::Invoke:
433 return !cast<InvokeInst>(this)->doesNotAccessMemory();
Chris Lattner954907a2008-05-08 21:58:49 +0000434 case Instruction::Store:
Eli Friedmanb9d5a632011-08-15 21:00:18 +0000435 return !cast<StoreInst>(this)->isUnordered();
Chris Lattner84627112008-05-08 17:16:51 +0000436 }
437}
Chris Lattner8ec6e8a2008-04-20 22:11:30 +0000438
Chris Lattnerc992e182007-02-15 23:15:00 +0000439/// mayWriteToMemory - Return true if this instruction may modify memory.
440///
441bool Instruction::mayWriteToMemory() const {
442 switch (getOpcode()) {
443 default: return false;
Eli Friedman89b694b2011-07-27 01:08:30 +0000444 case Instruction::Fence: // FIXME: refine definition of mayWriteToMemory
Duncan Sands38ef3a82007-12-03 20:06:50 +0000445 case Instruction::Store:
Chris Lattnerc992e182007-02-15 23:15:00 +0000446 case Instruction::VAArg:
Eli Friedmanadec5872011-07-29 03:05:32 +0000447 case Instruction::AtomicCmpXchg:
448 case Instruction::AtomicRMW:
Chris Lattnerc992e182007-02-15 23:15:00 +0000449 return true;
450 case Instruction::Call:
Duncan Sands38ef3a82007-12-03 20:06:50 +0000451 return !cast<CallInst>(this)->onlyReadsMemory();
Chris Lattner84627112008-05-08 17:16:51 +0000452 case Instruction::Invoke:
453 return !cast<InvokeInst>(this)->onlyReadsMemory();
Chris Lattnerc992e182007-02-15 23:15:00 +0000454 case Instruction::Load:
Eli Friedmanb9d5a632011-08-15 21:00:18 +0000455 return !cast<LoadInst>(this)->isUnordered();
Chris Lattnerc992e182007-02-15 23:15:00 +0000456 }
457}
Chris Lattnercab6c332002-10-31 04:14:01 +0000458
Duncan Sands1efabaa2009-05-06 06:49:50 +0000459bool Instruction::mayThrow() const {
460 if (const CallInst *CI = dyn_cast<CallInst>(this))
461 return !CI->doesNotThrow();
Bill Wendlingd7c6c912011-08-16 21:15:50 +0000462 return isa<ResumeInst>(this);
Duncan Sands1efabaa2009-05-06 06:49:50 +0000463}
464
Nadav Rotem01863472013-02-19 20:02:09 +0000465bool Instruction::mayReturn() const {
466 if (const CallInst *CI = dyn_cast<CallInst>(this))
467 return !CI->doesNotReturn();
468 return true;
469}
470
Chris Lattnercab6c332002-10-31 04:14:01 +0000471/// isAssociative - Return true if the instruction is associative:
472///
Dan Gohmana5b96452009-06-04 22:49:04 +0000473/// Associative operators satisfy: x op (y op z) === (x op y) op z
Chris Lattnercab6c332002-10-31 04:14:01 +0000474///
Dan Gohmana5b96452009-06-04 22:49:04 +0000475/// In LLVM, the Add, Mul, And, Or, and Xor operators are associative.
Chris Lattnercab6c332002-10-31 04:14:01 +0000476///
Duncan Sands70db5e72010-12-20 13:10:23 +0000477bool Instruction::isAssociative(unsigned Opcode) {
Dan Gohmana5b96452009-06-04 22:49:04 +0000478 return Opcode == And || Opcode == Or || Opcode == Xor ||
479 Opcode == Add || Opcode == Mul;
Chris Lattnercab6c332002-10-31 04:14:01 +0000480}
481
Shuxin Yang01ab5d72012-11-29 01:47:31 +0000482bool Instruction::isAssociative() const {
483 unsigned Opcode = getOpcode();
484 if (isAssociative(Opcode))
485 return true;
486
487 switch (Opcode) {
488 case FMul:
489 case FAdd:
490 return cast<FPMathOperator>(this)->hasUnsafeAlgebra();
491 default:
492 return false;
493 }
494}
495
Chris Lattnercab6c332002-10-31 04:14:01 +0000496/// isCommutative - Return true if the instruction is commutative:
497///
Misha Brukmanfa100532003-10-10 17:54:14 +0000498/// Commutative operators satisfy: (x op y) === (y op x)
Chris Lattnercab6c332002-10-31 04:14:01 +0000499///
500/// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
501/// applied to any type.
502///
503bool Instruction::isCommutative(unsigned op) {
504 switch (op) {
505 case Add:
Dan Gohmana5b96452009-06-04 22:49:04 +0000506 case FAdd:
Chris Lattnercab6c332002-10-31 04:14:01 +0000507 case Mul:
Dan Gohmana5b96452009-06-04 22:49:04 +0000508 case FMul:
Misha Brukmanb1c93172005-04-21 23:48:37 +0000509 case And:
Chris Lattnercab6c332002-10-31 04:14:01 +0000510 case Or:
511 case Xor:
Chris Lattnercab6c332002-10-31 04:14:01 +0000512 return true;
513 default:
514 return false;
515 }
516}
Tanya Lattnera93c7ae2003-07-31 04:05:50 +0000517
Duncan Sandsd7aeefe2012-06-12 14:33:56 +0000518/// isIdempotent - Return true if the instruction is idempotent:
519///
520/// Idempotent operators satisfy: x op x === x
521///
522/// In LLVM, the And and Or operators are idempotent.
523///
524bool Instruction::isIdempotent(unsigned Opcode) {
525 return Opcode == And || Opcode == Or;
526}
527
528/// isNilpotent - Return true if the instruction is nilpotent:
529///
530/// Nilpotent operators satisfy: x op x === Id,
531///
532/// where Id is the identity for the operator, i.e. a constant such that
533/// x op Id === x and Id op x === x for all x.
534///
535/// In LLVM, the Xor operator is nilpotent.
536///
537bool Instruction::isNilpotent(unsigned Opcode) {
538 return Opcode == Xor;
539}
540
Devang Patel11cf3f42009-10-27 22:16:29 +0000541Instruction *Instruction::clone() const {
542 Instruction *New = clone_impl();
543 New->SubclassOptionalData = SubclassOptionalData;
Chris Lattner68017802009-12-29 07:44:16 +0000544 if (!hasMetadata())
545 return New;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000546
Chris Lattner68017802009-12-29 07:44:16 +0000547 // Otherwise, enumerate and copy over metadata from the old instruction to the
548 // new one.
549 SmallVector<std::pair<unsigned, MDNode*>, 4> TheMDs;
Chris Lattner8f294912011-07-14 18:57:51 +0000550 getAllMetadataOtherThanDebugLoc(TheMDs);
Chris Lattner68017802009-12-29 07:44:16 +0000551 for (unsigned i = 0, e = TheMDs.size(); i != e; ++i)
552 New->setMetadata(TheMDs[i].first, TheMDs[i].second);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000553
Chris Lattner8f294912011-07-14 18:57:51 +0000554 New->setDebugLoc(getDebugLoc());
Devang Patel11cf3f42009-10-27 22:16:29 +0000555 return New;
556}