blob: 2c8b8b23b18e37fe8ab22439812e70ce14385945 [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 Lattner5d1bc2c2005-01-29 00:35:33 +000023Instruction::Instruction(const 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 Lattner5d1bc2c2005-01-29 00:35:33 +000037Instruction::Instruction(const 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";
130 case GetElementPtr: return "getelementptr";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000131
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000132 // Convert instructions...
133 case Trunc: return "trunc";
134 case ZExt: return "zext";
135 case SExt: return "sext";
136 case FPTrunc: return "fptrunc";
137 case FPExt: return "fpext";
138 case FPToUI: return "fptoui";
139 case FPToSI: return "fptosi";
140 case UIToFP: return "uitofp";
141 case SIToFP: return "sitofp";
142 case IntToPtr: return "inttoptr";
143 case PtrToInt: return "ptrtoint";
144 case BitCast: return "bitcast";
145
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000146 // Other instructions...
Reid Spencer45e52392006-12-03 06:27:29 +0000147 case ICmp: return "icmp";
148 case FCmp: return "fcmp";
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000149 case PHI: return "phi";
150 case Select: return "select";
151 case Call: return "call";
152 case Shl: return "shl";
153 case LShr: return "lshr";
154 case AShr: return "ashr";
155 case VAArg: return "va_arg";
Robert Bocchino23004482006-01-10 19:05:34 +0000156 case ExtractElement: return "extractelement";
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000157 case InsertElement: return "insertelement";
158 case ShuffleVector: return "shufflevector";
Matthijs Kooijmanbf8d6ce2008-05-30 10:31:54 +0000159 case ExtractValue: return "extractvalue";
160 case InsertValue: return "insertvalue";
Chris Lattnerf70da102003-05-08 02:44:12 +0000161
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000162 default: return "<Invalid operator> ";
163 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000164
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000165 return 0;
166}
Chris Lattnercab6c332002-10-31 04:14:01 +0000167
Chris Lattner0b6ebd8d2004-11-30 02:51:53 +0000168/// isIdenticalTo - Return true if the specified instruction is exactly
169/// identical to the current one. This means that all operands match and any
170/// extra information (e.g. load is volatile) agree.
Chris Lattner378b0412008-11-27 08:39:18 +0000171bool Instruction::isIdenticalTo(const Instruction *I) const {
Dan Gohman23d2c762009-08-25 22:24:20 +0000172 return isIdenticalToWhenDefined(I) &&
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000173 SubclassOptionalData == I->SubclassOptionalData;
174}
175
Dan Gohman23d2c762009-08-25 22:24:20 +0000176/// isIdenticalToWhenDefined - This is like isIdenticalTo, except that it
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000177/// ignores the SubclassOptionalData flags, which specify conditions
178/// under which the instruction's result is undefined.
179bool Instruction::isIdenticalToWhenDefined(const Instruction *I) const {
Chris Lattner0b6ebd8d2004-11-30 02:51:53 +0000180 if (getOpcode() != I->getOpcode() ||
181 getNumOperands() != I->getNumOperands() ||
182 getType() != I->getType())
183 return false;
184
185 // We have two instructions of identical opcode and #operands. Check to see
186 // if all operands are the same.
187 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
188 if (getOperand(i) != I->getOperand(i))
189 return false;
190
191 // Check special state that is a part of some instructions.
192 if (const LoadInst *LI = dyn_cast<LoadInst>(this))
Dan Gohmanfc27e252008-10-16 01:24:45 +0000193 return LI->isVolatile() == cast<LoadInst>(I)->isVolatile() &&
194 LI->getAlignment() == cast<LoadInst>(I)->getAlignment();
Chris Lattner0b6ebd8d2004-11-30 02:51:53 +0000195 if (const StoreInst *SI = dyn_cast<StoreInst>(this))
Dan Gohmanfc27e252008-10-16 01:24:45 +0000196 return SI->isVolatile() == cast<StoreInst>(I)->isVolatile() &&
197 SI->getAlignment() == cast<StoreInst>(I)->getAlignment();
Reid Spencer266e42b2006-12-23 06:05:41 +0000198 if (const CmpInst *CI = dyn_cast<CmpInst>(this))
199 return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
Chris Lattner06038452005-05-06 05:51:46 +0000200 if (const CallInst *CI = dyn_cast<CallInst>(this))
Dan Gohmanfc27e252008-10-16 01:24:45 +0000201 return CI->isTailCall() == cast<CallInst>(I)->isTailCall() &&
202 CI->getCallingConv() == cast<CallInst>(I)->getCallingConv() &&
Nick Lewycky91543442011-01-26 09:23:19 +0000203 CI->getAttributes() == cast<CallInst>(I)->getAttributes();
Dan Gohmanfc27e252008-10-16 01:24:45 +0000204 if (const InvokeInst *CI = dyn_cast<InvokeInst>(this))
Nick Lewyckyc5a68d22008-10-27 07:28:44 +0000205 return CI->getCallingConv() == cast<InvokeInst>(I)->getCallingConv() &&
Nick Lewycky91543442011-01-26 09:23:19 +0000206 CI->getAttributes() == cast<InvokeInst>(I)->getAttributes();
Dan Gohmanfc27e252008-10-16 01:24:45 +0000207 if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(this)) {
208 if (IVI->getNumIndices() != cast<InsertValueInst>(I)->getNumIndices())
209 return false;
210 for (unsigned i = 0, e = IVI->getNumIndices(); i != e; ++i)
211 if (IVI->idx_begin()[i] != cast<InsertValueInst>(I)->idx_begin()[i])
212 return false;
213 return true;
214 }
215 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(this)) {
216 if (EVI->getNumIndices() != cast<ExtractValueInst>(I)->getNumIndices())
217 return false;
218 for (unsigned i = 0, e = EVI->getNumIndices(); i != e; ++i)
219 if (EVI->idx_begin()[i] != cast<ExtractValueInst>(I)->idx_begin()[i])
220 return false;
221 return true;
222 }
223
Chris Lattner0b6ebd8d2004-11-30 02:51:53 +0000224 return true;
225}
226
Reid Spencer266e42b2006-12-23 06:05:41 +0000227// isSameOperationAs
Dan Gohman17fb0d22009-06-12 19:03:05 +0000228// This should be kept in sync with isEquivalentOperation in
229// lib/Transforms/IPO/MergeFunctions.cpp.
Chris Lattner378b0412008-11-27 08:39:18 +0000230bool Instruction::isSameOperationAs(const Instruction *I) const {
Dan Gohman17fb0d22009-06-12 19:03:05 +0000231 if (getOpcode() != I->getOpcode() ||
232 getNumOperands() != I->getNumOperands() ||
233 getType() != I->getType())
Reid Spencer266e42b2006-12-23 06:05:41 +0000234 return false;
235
236 // We have two instructions of identical opcode and #operands. Check to see
237 // if all operands are the same type
238 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
239 if (getOperand(i)->getType() != I->getOperand(i)->getType())
240 return false;
241
242 // Check special state that is a part of some instructions.
243 if (const LoadInst *LI = dyn_cast<LoadInst>(this))
Dan Gohmanfc27e252008-10-16 01:24:45 +0000244 return LI->isVolatile() == cast<LoadInst>(I)->isVolatile() &&
245 LI->getAlignment() == cast<LoadInst>(I)->getAlignment();
Reid Spencer266e42b2006-12-23 06:05:41 +0000246 if (const StoreInst *SI = dyn_cast<StoreInst>(this))
Dan Gohmanfc27e252008-10-16 01:24:45 +0000247 return SI->isVolatile() == cast<StoreInst>(I)->isVolatile() &&
248 SI->getAlignment() == cast<StoreInst>(I)->getAlignment();
Reid Spencer266e42b2006-12-23 06:05:41 +0000249 if (const CmpInst *CI = dyn_cast<CmpInst>(this))
250 return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
251 if (const CallInst *CI = dyn_cast<CallInst>(this))
Dan Gohmanfc27e252008-10-16 01:24:45 +0000252 return CI->isTailCall() == cast<CallInst>(I)->isTailCall() &&
253 CI->getCallingConv() == cast<CallInst>(I)->getCallingConv() &&
Nick Lewycky91543442011-01-26 09:23:19 +0000254 CI->getAttributes() == cast<CallInst>(I)->getAttributes();
Dan Gohmanfc27e252008-10-16 01:24:45 +0000255 if (const InvokeInst *CI = dyn_cast<InvokeInst>(this))
Nick Lewyckyc5a68d22008-10-27 07:28:44 +0000256 return CI->getCallingConv() == cast<InvokeInst>(I)->getCallingConv() &&
Nick Lewycky91543442011-01-26 09:23:19 +0000257 CI->getAttributes() ==
258 cast<InvokeInst>(I)->getAttributes();
Dan Gohmanfc27e252008-10-16 01:24:45 +0000259 if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(this)) {
260 if (IVI->getNumIndices() != cast<InsertValueInst>(I)->getNumIndices())
261 return false;
262 for (unsigned i = 0, e = IVI->getNumIndices(); i != e; ++i)
263 if (IVI->idx_begin()[i] != cast<InsertValueInst>(I)->idx_begin()[i])
264 return false;
265 return true;
266 }
267 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(this)) {
268 if (EVI->getNumIndices() != cast<ExtractValueInst>(I)->getNumIndices())
269 return false;
270 for (unsigned i = 0, e = EVI->getNumIndices(); i != e; ++i)
271 if (EVI->idx_begin()[i] != cast<ExtractValueInst>(I)->idx_begin()[i])
272 return false;
273 return true;
274 }
Reid Spencer266e42b2006-12-23 06:05:41 +0000275
276 return true;
277}
278
Chris Lattner8ec6e8a2008-04-20 22:11:30 +0000279/// isUsedOutsideOfBlock - Return true if there are any uses of I outside of the
280/// specified block. Note that PHI nodes are considered to evaluate their
281/// operands in the corresponding predecessor block.
282bool Instruction::isUsedOutsideOfBlock(const BasicBlock *BB) const {
Gabor Greifc78d7202010-03-25 23:06:16 +0000283 for (const_use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Chris Lattner8ec6e8a2008-04-20 22:11:30 +0000284 // PHI nodes uses values in the corresponding predecessor block. For other
285 // instructions, just check to see whether the parent of the use matches up.
Gabor Greif2a464d72010-07-12 10:36:48 +0000286 const User *U = *UI;
287 const PHINode *PN = dyn_cast<PHINode>(U);
Chris Lattner8ec6e8a2008-04-20 22:11:30 +0000288 if (PN == 0) {
Gabor Greif2a464d72010-07-12 10:36:48 +0000289 if (cast<Instruction>(U)->getParent() != BB)
Chris Lattner8ec6e8a2008-04-20 22:11:30 +0000290 return true;
291 continue;
292 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000293
Gabor Greifeb61fcf2009-01-23 19:40:15 +0000294 if (PN->getIncomingBlock(UI) != BB)
Chris Lattner8ec6e8a2008-04-20 22:11:30 +0000295 return true;
296 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000297 return false;
Chris Lattner8ec6e8a2008-04-20 22:11:30 +0000298}
299
Chris Lattner84627112008-05-08 17:16:51 +0000300/// mayReadFromMemory - Return true if this instruction may read memory.
301///
302bool Instruction::mayReadFromMemory() const {
303 switch (getOpcode()) {
304 default: return false;
Chris Lattner84627112008-05-08 17:16:51 +0000305 case Instruction::VAArg:
Chris Lattner954907a2008-05-08 21:58:49 +0000306 case Instruction::Load:
Chris Lattner84627112008-05-08 17:16:51 +0000307 return true;
308 case Instruction::Call:
309 return !cast<CallInst>(this)->doesNotAccessMemory();
310 case Instruction::Invoke:
311 return !cast<InvokeInst>(this)->doesNotAccessMemory();
Chris Lattner954907a2008-05-08 21:58:49 +0000312 case Instruction::Store:
313 return cast<StoreInst>(this)->isVolatile();
Chris Lattner84627112008-05-08 17:16:51 +0000314 }
315}
Chris Lattner8ec6e8a2008-04-20 22:11:30 +0000316
Chris Lattnerc992e182007-02-15 23:15:00 +0000317/// mayWriteToMemory - Return true if this instruction may modify memory.
318///
319bool Instruction::mayWriteToMemory() const {
320 switch (getOpcode()) {
321 default: return false;
Duncan Sands38ef3a82007-12-03 20:06:50 +0000322 case Instruction::Store:
Chris Lattnerc992e182007-02-15 23:15:00 +0000323 case Instruction::VAArg:
324 return true;
325 case Instruction::Call:
Duncan Sands38ef3a82007-12-03 20:06:50 +0000326 return !cast<CallInst>(this)->onlyReadsMemory();
Chris Lattner84627112008-05-08 17:16:51 +0000327 case Instruction::Invoke:
328 return !cast<InvokeInst>(this)->onlyReadsMemory();
Chris Lattnerc992e182007-02-15 23:15:00 +0000329 case Instruction::Load:
330 return cast<LoadInst>(this)->isVolatile();
331 }
332}
Chris Lattnercab6c332002-10-31 04:14:01 +0000333
Duncan Sands1efabaa2009-05-06 06:49:50 +0000334/// mayThrow - Return true if this instruction may throw an exception.
335///
336bool Instruction::mayThrow() const {
337 if (const CallInst *CI = dyn_cast<CallInst>(this))
338 return !CI->doesNotThrow();
339 return false;
340}
341
Chris Lattnercab6c332002-10-31 04:14:01 +0000342/// isAssociative - Return true if the instruction is associative:
343///
Dan Gohmana5b96452009-06-04 22:49:04 +0000344/// Associative operators satisfy: x op (y op z) === (x op y) op z
Chris Lattnercab6c332002-10-31 04:14:01 +0000345///
Dan Gohmana5b96452009-06-04 22:49:04 +0000346/// In LLVM, the Add, Mul, And, Or, and Xor operators are associative.
Chris Lattnercab6c332002-10-31 04:14:01 +0000347///
Duncan Sands70db5e72010-12-20 13:10:23 +0000348bool Instruction::isAssociative(unsigned Opcode) {
Dan Gohmana5b96452009-06-04 22:49:04 +0000349 return Opcode == And || Opcode == Or || Opcode == Xor ||
350 Opcode == Add || Opcode == Mul;
Chris Lattnercab6c332002-10-31 04:14:01 +0000351}
352
353/// isCommutative - Return true if the instruction is commutative:
354///
Misha Brukmanfa100532003-10-10 17:54:14 +0000355/// Commutative operators satisfy: (x op y) === (y op x)
Chris Lattnercab6c332002-10-31 04:14:01 +0000356///
357/// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
358/// applied to any type.
359///
360bool Instruction::isCommutative(unsigned op) {
361 switch (op) {
362 case Add:
Dan Gohmana5b96452009-06-04 22:49:04 +0000363 case FAdd:
Chris Lattnercab6c332002-10-31 04:14:01 +0000364 case Mul:
Dan Gohmana5b96452009-06-04 22:49:04 +0000365 case FMul:
Misha Brukmanb1c93172005-04-21 23:48:37 +0000366 case And:
Chris Lattnercab6c332002-10-31 04:14:01 +0000367 case Or:
368 case Xor:
Chris Lattnercab6c332002-10-31 04:14:01 +0000369 return true;
370 default:
371 return false;
372 }
373}
Tanya Lattnera93c7ae2003-07-31 04:05:50 +0000374
Eli Friedmanb8f6a4f2009-07-17 04:28:42 +0000375bool Instruction::isSafeToSpeculativelyExecute() const {
376 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
377 if (Constant *C = dyn_cast<Constant>(getOperand(i)))
378 if (C->canTrap())
379 return false;
380
381 switch (getOpcode()) {
Tanya Lattnera93c7ae2003-07-31 04:05:50 +0000382 default:
Eli Friedmanb8f6a4f2009-07-17 04:28:42 +0000383 return true;
384 case UDiv:
385 case URem: {
386 // x / y is undefined if y == 0, but calcuations like x / 3 are safe.
387 ConstantInt *Op = dyn_cast<ConstantInt>(getOperand(1));
388 return Op && !Op->isNullValue();
389 }
390 case SDiv:
391 case SRem: {
392 // x / y is undefined if y == 0, and might be undefined if y == -1,
393 // but calcuations like x / 3 are safe.
394 ConstantInt *Op = dyn_cast<ConstantInt>(getOperand(1));
395 return Op && !Op->isNullValue() && !Op->isAllOnesValue();
396 }
397 case Load: {
Dan Gohmana826a882010-11-11 21:23:25 +0000398 const LoadInst *LI = cast<LoadInst>(this);
399 if (LI->isVolatile())
Eli Friedmanb8f6a4f2009-07-17 04:28:42 +0000400 return false;
Dan Gohmana826a882010-11-11 21:23:25 +0000401 return LI->getPointerOperand()->isDereferenceablePointer();
Tanya Lattnera93c7ae2003-07-31 04:05:50 +0000402 }
Eli Friedmanb8f6a4f2009-07-17 04:28:42 +0000403 case Call:
404 return false; // The called function could have undefined behavior or
405 // side-effects.
406 // FIXME: We should special-case some intrinsics (bswap,
407 // overflow-checking arithmetic, etc.)
408 case VAArg:
409 case Alloca:
Eli Friedmanb8f6a4f2009-07-17 04:28:42 +0000410 case Invoke:
411 case PHI:
412 case Store:
Eli Friedmanb8f6a4f2009-07-17 04:28:42 +0000413 case Ret:
414 case Br:
Dan Gohmanee8d80d2010-07-02 00:35:34 +0000415 case IndirectBr:
Eli Friedmanb8f6a4f2009-07-17 04:28:42 +0000416 case Switch:
417 case Unwind:
418 case Unreachable:
419 return false; // Misc instructions which have effects
420 }
Tanya Lattnera93c7ae2003-07-31 04:05:50 +0000421}
Devang Patel11cf3f42009-10-27 22:16:29 +0000422
423Instruction *Instruction::clone() const {
424 Instruction *New = clone_impl();
425 New->SubclassOptionalData = SubclassOptionalData;
Chris Lattner68017802009-12-29 07:44:16 +0000426 if (!hasMetadata())
427 return New;
428
429 // Otherwise, enumerate and copy over metadata from the old instruction to the
430 // new one.
431 SmallVector<std::pair<unsigned, MDNode*>, 4> TheMDs;
432 getAllMetadata(TheMDs);
433 for (unsigned i = 0, e = TheMDs.size(); i != e; ++i)
434 New->setMetadata(TheMDs[i].first, TheMDs[i].second);
Devang Patel11cf3f42009-10-27 22:16:29 +0000435 return New;
436}