blob: cd8f737e28444069efde470b46674f1233d4eac2 [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//
10// This file implements the Instruction class for the VMCore library.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner68017802009-12-29 07:44:16 +000014#include "llvm/Instruction.h"
Reid Spencerd9436b62006-11-20 01:22:35 +000015#include "llvm/Type.h"
Chris Lattner0b6ebd8d2004-11-30 02:51:53 +000016#include "llvm/Instructions.h"
Eli Friedmanb8f6a4f2009-07-17 04:28:42 +000017#include "llvm/Constants.h"
Victor Hernandez788eaab2009-09-18 19:20:02 +000018#include "llvm/Module.h"
Duncan Sands38ef3a82007-12-03 20:06:50 +000019#include "llvm/Support/CallSite.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000020#include "llvm/Support/LeakDetector.h"
Chris Lattner0e03ab62003-11-20 17:45:12 +000021using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000022
Chris Lattner229907c2011-07-18 04:54:35 +000023Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,
Chris Lattner2195fc42007-02-24 00:55:48 +000024 Instruction *InsertBefore)
Chris Lattnerc0f5ce32010-04-01 05:23:13 +000025 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) {
Chris Lattner184b2982002-09-08 18:59:35 +000026 // Make sure that we get added to a basicblock
27 LeakDetector::addGarbageObject(this);
Chris Lattner3c787442002-09-10 15:45:53 +000028
29 // If requested, insert this instruction into a basic block...
30 if (InsertBefore) {
31 assert(InsertBefore->getParent() &&
32 "Instruction to insert before is not in a basic block!");
33 InsertBefore->getParent()->getInstList().insert(InsertBefore, this);
34 }
Chris Lattner2f7c9632001-06-06 20:29:01 +000035}
36
Chris Lattner229907c2011-07-18 04:54:35 +000037Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,
Chris Lattner2195fc42007-02-24 00:55:48 +000038 BasicBlock *InsertAtEnd)
Chris Lattnerc0f5ce32010-04-01 05:23:13 +000039 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) {
Chris Lattner5d1bc2c2005-01-29 00:35:33 +000040 // Make sure that we get added to a basicblock
41 LeakDetector::addGarbageObject(this);
Alkis Evlogimenos9f0fdf72004-05-26 21:41:09 +000042
43 // append this instruction into the basic block
44 assert(InsertAtEnd && "Basic block to append to may not be NULL!");
45 InsertAtEnd->getInstList().push_back(this);
Chris Lattner0f048162007-02-13 07:54:42 +000046}
47
48
Chris Lattner1c12a882006-06-21 16:53:47 +000049// Out of line virtual method, so the vtable, etc has a home.
Gordon Henriksen14a55692007-12-10 02:14:30 +000050Instruction::~Instruction() {
Devang Patel4e6f2e42009-09-24 16:19:11 +000051 assert(Parent == 0 && "Instruction still linked in the program!");
Dan Gohman48a995f2010-07-20 22:25:04 +000052 if (hasMetadataHashEntry())
53 clearMetadataHashEntries();
Chris Lattner1c12a882006-06-21 16:53:47 +000054}
55
56
Chris Lattner9ed7aef2002-09-06 21:33:15 +000057void Instruction::setParent(BasicBlock *P) {
Chris Lattnerd8a232b2004-02-04 01:06:38 +000058 if (getParent()) {
59 if (!P) LeakDetector::addGarbageObject(this);
60 } else {
61 if (P) LeakDetector::removeGarbageObject(this);
62 }
Chris Lattner184b2982002-09-08 18:59:35 +000063
Chris Lattner9ed7aef2002-09-06 21:33:15 +000064 Parent = P;
65}
66
Chris Lattner02a71e72004-10-11 22:21:39 +000067void Instruction::removeFromParent() {
68 getParent()->getInstList().remove(this);
69}
70
71void Instruction::eraseFromParent() {
72 getParent()->getInstList().erase(this);
73}
Vikram S. Adve8aee7962002-07-14 23:09:40 +000074
Owen Anderson2505e0c2008-06-17 18:29:27 +000075/// insertBefore - Insert an unlinked instructions into a basic block
76/// immediately before the specified instruction.
77void Instruction::insertBefore(Instruction *InsertPos) {
78 InsertPos->getParent()->getInstList().insert(InsertPos, this);
79}
80
Chris Lattner6e66d0a2009-01-13 07:43:51 +000081/// insertAfter - Insert an unlinked instructions into a basic block
82/// immediately after the specified instruction.
83void Instruction::insertAfter(Instruction *InsertPos) {
84 InsertPos->getParent()->getInstList().insertAfter(InsertPos, this);
85}
86
Chris Lattner24a0a432005-08-08 05:21:50 +000087/// moveBefore - Unlink this instruction from its current basic block and
88/// insert it into the basic block that MovePos lives in, right before
89/// MovePos.
90void Instruction::moveBefore(Instruction *MovePos) {
91 MovePos->getParent()->getInstList().splice(MovePos,getParent()->getInstList(),
92 this);
93}
94
95
Vikram S. Adve8aee7962002-07-14 23:09:40 +000096const char *Instruction::getOpcodeName(unsigned OpCode) {
97 switch (OpCode) {
98 // Terminators
Chris Lattnerb193ff82002-08-14 18:18:02 +000099 case Ret: return "ret";
100 case Br: return "br";
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000101 case Switch: return "switch";
Chris Lattnerd04cb6d2009-10-28 00:19:10 +0000102 case IndirectBr: return "indirectbr";
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000103 case Invoke: return "invoke";
Chris Lattner66d5f572003-09-08 18:54:36 +0000104 case Unwind: return "unwind";
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000105 case Unreachable: return "unreachable";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000106
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000107 // Standard binary operators...
108 case Add: return "add";
Dan Gohmana5b96452009-06-04 22:49:04 +0000109 case FAdd: return "fadd";
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000110 case Sub: return "sub";
Dan Gohmana5b96452009-06-04 22:49:04 +0000111 case FSub: return "fsub";
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000112 case Mul: return "mul";
Dan Gohmana5b96452009-06-04 22:49:04 +0000113 case FMul: return "fmul";
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000114 case UDiv: return "udiv";
115 case SDiv: return "sdiv";
116 case FDiv: return "fdiv";
Reid Spencer7eb55b32006-11-02 01:53:59 +0000117 case URem: return "urem";
118 case SRem: return "srem";
119 case FRem: return "frem";
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000120
121 // Logical operators...
122 case And: return "and";
123 case Or : return "or";
124 case Xor: return "xor";
125
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000126 // Memory instructions...
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000127 case Alloca: return "alloca";
128 case Load: return "load";
129 case Store: return "store";
Eli Friedmanfee02c62011-07-25 23:16:38 +0000130 case Fence: return "fence";
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000131 case GetElementPtr: return "getelementptr";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000132
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000133 // Convert instructions...
134 case Trunc: return "trunc";
135 case ZExt: return "zext";
136 case SExt: return "sext";
137 case FPTrunc: return "fptrunc";
138 case FPExt: return "fpext";
139 case FPToUI: return "fptoui";
140 case FPToSI: return "fptosi";
141 case UIToFP: return "uitofp";
142 case SIToFP: return "sitofp";
143 case IntToPtr: return "inttoptr";
144 case PtrToInt: return "ptrtoint";
145 case BitCast: return "bitcast";
146
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000147 // Other instructions...
Reid Spencer45e52392006-12-03 06:27:29 +0000148 case ICmp: return "icmp";
149 case FCmp: return "fcmp";
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000150 case PHI: return "phi";
151 case Select: return "select";
152 case Call: return "call";
153 case Shl: return "shl";
154 case LShr: return "lshr";
155 case AShr: return "ashr";
156 case VAArg: return "va_arg";
Robert Bocchino23004482006-01-10 19:05:34 +0000157 case ExtractElement: return "extractelement";
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000158 case InsertElement: return "insertelement";
159 case ShuffleVector: return "shufflevector";
Matthijs Kooijmanbf8d6ce2008-05-30 10:31:54 +0000160 case ExtractValue: return "extractvalue";
161 case InsertValue: return "insertvalue";
Chris Lattnerf70da102003-05-08 02:44:12 +0000162
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000163 default: return "<Invalid operator> ";
164 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000165
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000166 return 0;
167}
Chris Lattnercab6c332002-10-31 04:14:01 +0000168
Chris Lattner0b6ebd8d2004-11-30 02:51:53 +0000169/// isIdenticalTo - Return true if the specified instruction is exactly
170/// identical to the current one. This means that all operands match and any
171/// extra information (e.g. load is volatile) agree.
Chris Lattner378b0412008-11-27 08:39:18 +0000172bool Instruction::isIdenticalTo(const Instruction *I) const {
Dan Gohman23d2c762009-08-25 22:24:20 +0000173 return isIdenticalToWhenDefined(I) &&
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000174 SubclassOptionalData == I->SubclassOptionalData;
175}
176
Dan Gohman23d2c762009-08-25 22:24:20 +0000177/// isIdenticalToWhenDefined - This is like isIdenticalTo, except that it
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000178/// ignores the SubclassOptionalData flags, which specify conditions
179/// under which the instruction's result is undefined.
180bool Instruction::isIdenticalToWhenDefined(const Instruction *I) const {
Chris Lattner0b6ebd8d2004-11-30 02:51:53 +0000181 if (getOpcode() != I->getOpcode() ||
182 getNumOperands() != I->getNumOperands() ||
183 getType() != I->getType())
184 return false;
185
186 // We have two instructions of identical opcode and #operands. Check to see
187 // if all operands are the same.
188 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
189 if (getOperand(i) != I->getOperand(i))
190 return false;
191
192 // Check special state that is a part of some instructions.
193 if (const LoadInst *LI = dyn_cast<LoadInst>(this))
Dan Gohmanfc27e252008-10-16 01:24:45 +0000194 return LI->isVolatile() == cast<LoadInst>(I)->isVolatile() &&
195 LI->getAlignment() == cast<LoadInst>(I)->getAlignment();
Chris Lattner0b6ebd8d2004-11-30 02:51:53 +0000196 if (const StoreInst *SI = dyn_cast<StoreInst>(this))
Dan Gohmanfc27e252008-10-16 01:24:45 +0000197 return SI->isVolatile() == cast<StoreInst>(I)->isVolatile() &&
198 SI->getAlignment() == cast<StoreInst>(I)->getAlignment();
Reid Spencer266e42b2006-12-23 06:05:41 +0000199 if (const CmpInst *CI = dyn_cast<CmpInst>(this))
200 return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
Chris Lattner06038452005-05-06 05:51:46 +0000201 if (const CallInst *CI = dyn_cast<CallInst>(this))
Dan Gohmanfc27e252008-10-16 01:24:45 +0000202 return CI->isTailCall() == cast<CallInst>(I)->isTailCall() &&
203 CI->getCallingConv() == cast<CallInst>(I)->getCallingConv() &&
Nick Lewycky91543442011-01-26 09:23:19 +0000204 CI->getAttributes() == cast<CallInst>(I)->getAttributes();
Dan Gohmanfc27e252008-10-16 01:24:45 +0000205 if (const InvokeInst *CI = dyn_cast<InvokeInst>(this))
Nick Lewyckyc5a68d22008-10-27 07:28:44 +0000206 return CI->getCallingConv() == cast<InvokeInst>(I)->getCallingConv() &&
Nick Lewycky91543442011-01-26 09:23:19 +0000207 CI->getAttributes() == cast<InvokeInst>(I)->getAttributes();
Jay Foad57aa6362011-07-13 10:26:04 +0000208 if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(this))
209 return IVI->getIndices() == cast<InsertValueInst>(I)->getIndices();
210 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(this))
211 return EVI->getIndices() == cast<ExtractValueInst>(I)->getIndices();
Eli Friedman89b694b2011-07-27 01:08:30 +0000212 if (const FenceInst *FI = dyn_cast<FenceInst>(this))
213 return FI->getOrdering() == cast<FenceInst>(FI)->getOrdering() &&
214 FI->getSynchScope() == cast<FenceInst>(FI)->getSynchScope();
Dan Gohmanfc27e252008-10-16 01:24:45 +0000215
Chris Lattner0b6ebd8d2004-11-30 02:51:53 +0000216 return true;
217}
218
Reid Spencer266e42b2006-12-23 06:05:41 +0000219// isSameOperationAs
Dan Gohman17fb0d22009-06-12 19:03:05 +0000220// This should be kept in sync with isEquivalentOperation in
221// lib/Transforms/IPO/MergeFunctions.cpp.
Chris Lattner378b0412008-11-27 08:39:18 +0000222bool Instruction::isSameOperationAs(const Instruction *I) const {
Dan Gohman17fb0d22009-06-12 19:03:05 +0000223 if (getOpcode() != I->getOpcode() ||
224 getNumOperands() != I->getNumOperands() ||
225 getType() != I->getType())
Reid Spencer266e42b2006-12-23 06:05:41 +0000226 return false;
227
228 // We have two instructions of identical opcode and #operands. Check to see
229 // if all operands are the same type
230 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
231 if (getOperand(i)->getType() != I->getOperand(i)->getType())
232 return false;
233
234 // Check special state that is a part of some instructions.
235 if (const LoadInst *LI = dyn_cast<LoadInst>(this))
Dan Gohmanfc27e252008-10-16 01:24:45 +0000236 return LI->isVolatile() == cast<LoadInst>(I)->isVolatile() &&
237 LI->getAlignment() == cast<LoadInst>(I)->getAlignment();
Reid Spencer266e42b2006-12-23 06:05:41 +0000238 if (const StoreInst *SI = dyn_cast<StoreInst>(this))
Dan Gohmanfc27e252008-10-16 01:24:45 +0000239 return SI->isVolatile() == cast<StoreInst>(I)->isVolatile() &&
240 SI->getAlignment() == cast<StoreInst>(I)->getAlignment();
Reid Spencer266e42b2006-12-23 06:05:41 +0000241 if (const CmpInst *CI = dyn_cast<CmpInst>(this))
242 return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
243 if (const CallInst *CI = dyn_cast<CallInst>(this))
Dan Gohmanfc27e252008-10-16 01:24:45 +0000244 return CI->isTailCall() == cast<CallInst>(I)->isTailCall() &&
245 CI->getCallingConv() == cast<CallInst>(I)->getCallingConv() &&
Nick Lewycky91543442011-01-26 09:23:19 +0000246 CI->getAttributes() == cast<CallInst>(I)->getAttributes();
Dan Gohmanfc27e252008-10-16 01:24:45 +0000247 if (const InvokeInst *CI = dyn_cast<InvokeInst>(this))
Nick Lewyckyc5a68d22008-10-27 07:28:44 +0000248 return CI->getCallingConv() == cast<InvokeInst>(I)->getCallingConv() &&
Nick Lewycky91543442011-01-26 09:23:19 +0000249 CI->getAttributes() ==
250 cast<InvokeInst>(I)->getAttributes();
Jay Foad57aa6362011-07-13 10:26:04 +0000251 if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(this))
252 return IVI->getIndices() == cast<InsertValueInst>(I)->getIndices();
253 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(this))
254 return EVI->getIndices() == cast<ExtractValueInst>(I)->getIndices();
Eli Friedman89b694b2011-07-27 01:08:30 +0000255 if (const FenceInst *FI = dyn_cast<FenceInst>(this))
256 return FI->getOrdering() == cast<FenceInst>(FI)->getOrdering() &&
257 FI->getSynchScope() == cast<FenceInst>(FI)->getSynchScope();
Reid Spencer266e42b2006-12-23 06:05:41 +0000258
259 return true;
260}
261
Chris Lattner8ec6e8a2008-04-20 22:11:30 +0000262/// isUsedOutsideOfBlock - Return true if there are any uses of I outside of the
263/// specified block. Note that PHI nodes are considered to evaluate their
264/// operands in the corresponding predecessor block.
265bool Instruction::isUsedOutsideOfBlock(const BasicBlock *BB) const {
Gabor Greifc78d7202010-03-25 23:06:16 +0000266 for (const_use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Chris Lattner8ec6e8a2008-04-20 22:11:30 +0000267 // PHI nodes uses values in the corresponding predecessor block. For other
268 // instructions, just check to see whether the parent of the use matches up.
Gabor Greif2a464d72010-07-12 10:36:48 +0000269 const User *U = *UI;
270 const PHINode *PN = dyn_cast<PHINode>(U);
Chris Lattner8ec6e8a2008-04-20 22:11:30 +0000271 if (PN == 0) {
Gabor Greif2a464d72010-07-12 10:36:48 +0000272 if (cast<Instruction>(U)->getParent() != BB)
Chris Lattner8ec6e8a2008-04-20 22:11:30 +0000273 return true;
274 continue;
275 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000276
Gabor Greifeb61fcf2009-01-23 19:40:15 +0000277 if (PN->getIncomingBlock(UI) != BB)
Chris Lattner8ec6e8a2008-04-20 22:11:30 +0000278 return true;
279 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000280 return false;
Chris Lattner8ec6e8a2008-04-20 22:11:30 +0000281}
282
Chris Lattner84627112008-05-08 17:16:51 +0000283/// mayReadFromMemory - Return true if this instruction may read memory.
284///
285bool Instruction::mayReadFromMemory() const {
286 switch (getOpcode()) {
287 default: return false;
Chris Lattner84627112008-05-08 17:16:51 +0000288 case Instruction::VAArg:
Chris Lattner954907a2008-05-08 21:58:49 +0000289 case Instruction::Load:
Eli Friedman89b694b2011-07-27 01:08:30 +0000290 case Instruction::Fence: // FIXME: refine definition of mayReadFromMemory
Chris Lattner84627112008-05-08 17:16:51 +0000291 return true;
292 case Instruction::Call:
293 return !cast<CallInst>(this)->doesNotAccessMemory();
294 case Instruction::Invoke:
295 return !cast<InvokeInst>(this)->doesNotAccessMemory();
Chris Lattner954907a2008-05-08 21:58:49 +0000296 case Instruction::Store:
297 return cast<StoreInst>(this)->isVolatile();
Chris Lattner84627112008-05-08 17:16:51 +0000298 }
299}
Chris Lattner8ec6e8a2008-04-20 22:11:30 +0000300
Chris Lattnerc992e182007-02-15 23:15:00 +0000301/// mayWriteToMemory - Return true if this instruction may modify memory.
302///
303bool Instruction::mayWriteToMemory() const {
304 switch (getOpcode()) {
305 default: return false;
Eli Friedman89b694b2011-07-27 01:08:30 +0000306 case Instruction::Fence: // FIXME: refine definition of mayWriteToMemory
Duncan Sands38ef3a82007-12-03 20:06:50 +0000307 case Instruction::Store:
Chris Lattnerc992e182007-02-15 23:15:00 +0000308 case Instruction::VAArg:
309 return true;
310 case Instruction::Call:
Duncan Sands38ef3a82007-12-03 20:06:50 +0000311 return !cast<CallInst>(this)->onlyReadsMemory();
Chris Lattner84627112008-05-08 17:16:51 +0000312 case Instruction::Invoke:
313 return !cast<InvokeInst>(this)->onlyReadsMemory();
Chris Lattnerc992e182007-02-15 23:15:00 +0000314 case Instruction::Load:
315 return cast<LoadInst>(this)->isVolatile();
316 }
317}
Chris Lattnercab6c332002-10-31 04:14:01 +0000318
Duncan Sands1efabaa2009-05-06 06:49:50 +0000319/// mayThrow - Return true if this instruction may throw an exception.
320///
321bool Instruction::mayThrow() const {
322 if (const CallInst *CI = dyn_cast<CallInst>(this))
323 return !CI->doesNotThrow();
324 return false;
325}
326
Chris Lattnercab6c332002-10-31 04:14:01 +0000327/// isAssociative - Return true if the instruction is associative:
328///
Dan Gohmana5b96452009-06-04 22:49:04 +0000329/// Associative operators satisfy: x op (y op z) === (x op y) op z
Chris Lattnercab6c332002-10-31 04:14:01 +0000330///
Dan Gohmana5b96452009-06-04 22:49:04 +0000331/// In LLVM, the Add, Mul, And, Or, and Xor operators are associative.
Chris Lattnercab6c332002-10-31 04:14:01 +0000332///
Duncan Sands70db5e72010-12-20 13:10:23 +0000333bool Instruction::isAssociative(unsigned Opcode) {
Dan Gohmana5b96452009-06-04 22:49:04 +0000334 return Opcode == And || Opcode == Or || Opcode == Xor ||
335 Opcode == Add || Opcode == Mul;
Chris Lattnercab6c332002-10-31 04:14:01 +0000336}
337
338/// isCommutative - Return true if the instruction is commutative:
339///
Misha Brukmanfa100532003-10-10 17:54:14 +0000340/// Commutative operators satisfy: (x op y) === (y op x)
Chris Lattnercab6c332002-10-31 04:14:01 +0000341///
342/// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
343/// applied to any type.
344///
345bool Instruction::isCommutative(unsigned op) {
346 switch (op) {
347 case Add:
Dan Gohmana5b96452009-06-04 22:49:04 +0000348 case FAdd:
Chris Lattnercab6c332002-10-31 04:14:01 +0000349 case Mul:
Dan Gohmana5b96452009-06-04 22:49:04 +0000350 case FMul:
Misha Brukmanb1c93172005-04-21 23:48:37 +0000351 case And:
Chris Lattnercab6c332002-10-31 04:14:01 +0000352 case Or:
353 case Xor:
Chris Lattnercab6c332002-10-31 04:14:01 +0000354 return true;
355 default:
356 return false;
357 }
358}
Tanya Lattnera93c7ae2003-07-31 04:05:50 +0000359
Eli Friedmanb8f6a4f2009-07-17 04:28:42 +0000360bool Instruction::isSafeToSpeculativelyExecute() const {
361 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
362 if (Constant *C = dyn_cast<Constant>(getOperand(i)))
363 if (C->canTrap())
364 return false;
365
366 switch (getOpcode()) {
Tanya Lattnera93c7ae2003-07-31 04:05:50 +0000367 default:
Eli Friedmanb8f6a4f2009-07-17 04:28:42 +0000368 return true;
369 case UDiv:
370 case URem: {
371 // x / y is undefined if y == 0, but calcuations like x / 3 are safe.
372 ConstantInt *Op = dyn_cast<ConstantInt>(getOperand(1));
373 return Op && !Op->isNullValue();
374 }
375 case SDiv:
376 case SRem: {
377 // x / y is undefined if y == 0, and might be undefined if y == -1,
378 // but calcuations like x / 3 are safe.
379 ConstantInt *Op = dyn_cast<ConstantInt>(getOperand(1));
380 return Op && !Op->isNullValue() && !Op->isAllOnesValue();
381 }
382 case Load: {
Dan Gohmana826a882010-11-11 21:23:25 +0000383 const LoadInst *LI = cast<LoadInst>(this);
384 if (LI->isVolatile())
Eli Friedmanb8f6a4f2009-07-17 04:28:42 +0000385 return false;
Dan Gohmana826a882010-11-11 21:23:25 +0000386 return LI->getPointerOperand()->isDereferenceablePointer();
Tanya Lattnera93c7ae2003-07-31 04:05:50 +0000387 }
Eli Friedmanb8f6a4f2009-07-17 04:28:42 +0000388 case Call:
389 return false; // The called function could have undefined behavior or
390 // side-effects.
391 // FIXME: We should special-case some intrinsics (bswap,
392 // overflow-checking arithmetic, etc.)
393 case VAArg:
394 case Alloca:
Eli Friedmanb8f6a4f2009-07-17 04:28:42 +0000395 case Invoke:
396 case PHI:
397 case Store:
Eli Friedmanb8f6a4f2009-07-17 04:28:42 +0000398 case Ret:
399 case Br:
Dan Gohmanee8d80d2010-07-02 00:35:34 +0000400 case IndirectBr:
Eli Friedmanb8f6a4f2009-07-17 04:28:42 +0000401 case Switch:
402 case Unwind:
403 case Unreachable:
Eli Friedman89b694b2011-07-27 01:08:30 +0000404 case Fence:
Eli Friedmanb8f6a4f2009-07-17 04:28:42 +0000405 return false; // Misc instructions which have effects
406 }
Tanya Lattnera93c7ae2003-07-31 04:05:50 +0000407}
Devang Patel11cf3f42009-10-27 22:16:29 +0000408
409Instruction *Instruction::clone() const {
410 Instruction *New = clone_impl();
411 New->SubclassOptionalData = SubclassOptionalData;
Chris Lattner68017802009-12-29 07:44:16 +0000412 if (!hasMetadata())
413 return New;
414
415 // Otherwise, enumerate and copy over metadata from the old instruction to the
416 // new one.
417 SmallVector<std::pair<unsigned, MDNode*>, 4> TheMDs;
Chris Lattner8f294912011-07-14 18:57:51 +0000418 getAllMetadataOtherThanDebugLoc(TheMDs);
Chris Lattner68017802009-12-29 07:44:16 +0000419 for (unsigned i = 0, e = TheMDs.size(); i != e; ++i)
420 New->setMetadata(TheMDs[i].first, TheMDs[i].second);
Chris Lattner8f294912011-07-14 18:57:51 +0000421
422 New->setDebugLoc(getDebugLoc());
Devang Patel11cf3f42009-10-27 22:16:29 +0000423 return New;
424}