blob: e8738d2ba72bd755ab7427f8987834bac0781558 [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
Reid Spencerd9436b62006-11-20 01:22:35 +000014#include "llvm/Type.h"
Chris Lattner0b6ebd8d2004-11-30 02:51:53 +000015#include "llvm/Instructions.h"
Chris Lattner62b7fd12002-04-07 20:49:59 +000016#include "llvm/Function.h"
Duncan Sands38ef3a82007-12-03 20:06:50 +000017#include "llvm/Support/CallSite.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000018#include "llvm/Support/LeakDetector.h"
Chris Lattner0e03ab62003-11-20 17:45:12 +000019using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000020
Chris Lattner5d1bc2c2005-01-29 00:35:33 +000021Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps,
Chris Lattner2195fc42007-02-24 00:55:48 +000022 Instruction *InsertBefore)
Chris Lattner32ab6432007-02-12 05:18:08 +000023 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) {
Chris Lattner184b2982002-09-08 18:59:35 +000024 // Make sure that we get added to a basicblock
25 LeakDetector::addGarbageObject(this);
Chris Lattner3c787442002-09-10 15:45:53 +000026
27 // If requested, insert this instruction into a basic block...
28 if (InsertBefore) {
29 assert(InsertBefore->getParent() &&
30 "Instruction to insert before is not in a basic block!");
31 InsertBefore->getParent()->getInstList().insert(InsertBefore, this);
32 }
Chris Lattner2f7c9632001-06-06 20:29:01 +000033}
34
Chris Lattner5d1bc2c2005-01-29 00:35:33 +000035Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps,
Chris Lattner2195fc42007-02-24 00:55:48 +000036 BasicBlock *InsertAtEnd)
Chris Lattner32ab6432007-02-12 05:18:08 +000037 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) {
Chris Lattner5d1bc2c2005-01-29 00:35:33 +000038 // Make sure that we get added to a basicblock
39 LeakDetector::addGarbageObject(this);
Alkis Evlogimenos9f0fdf72004-05-26 21:41:09 +000040
41 // append this instruction into the basic block
42 assert(InsertAtEnd && "Basic block to append to may not be NULL!");
43 InsertAtEnd->getInstList().push_back(this);
Chris Lattner0f048162007-02-13 07:54:42 +000044}
45
46
Chris Lattner1c12a882006-06-21 16:53:47 +000047// Out of line virtual method, so the vtable, etc has a home.
Gordon Henriksen14a55692007-12-10 02:14:30 +000048Instruction::~Instruction() {
49 assert(Parent == 0 && "Instruction still linked in the program!");
Chris Lattner1c12a882006-06-21 16:53:47 +000050}
51
52
Chris Lattner9ed7aef2002-09-06 21:33:15 +000053void Instruction::setParent(BasicBlock *P) {
Chris Lattnerd8a232b2004-02-04 01:06:38 +000054 if (getParent()) {
55 if (!P) LeakDetector::addGarbageObject(this);
56 } else {
57 if (P) LeakDetector::removeGarbageObject(this);
58 }
Chris Lattner184b2982002-09-08 18:59:35 +000059
Chris Lattner9ed7aef2002-09-06 21:33:15 +000060 Parent = P;
61}
62
Chris Lattner02a71e72004-10-11 22:21:39 +000063void Instruction::removeFromParent() {
64 getParent()->getInstList().remove(this);
65}
66
67void Instruction::eraseFromParent() {
68 getParent()->getInstList().erase(this);
69}
Vikram S. Adve8aee7962002-07-14 23:09:40 +000070
Owen Anderson2505e0c2008-06-17 18:29:27 +000071/// insertBefore - Insert an unlinked instructions into a basic block
72/// immediately before the specified instruction.
73void Instruction::insertBefore(Instruction *InsertPos) {
74 InsertPos->getParent()->getInstList().insert(InsertPos, this);
75}
76
Chris Lattner24a0a432005-08-08 05:21:50 +000077/// moveBefore - Unlink this instruction from its current basic block and
78/// insert it into the basic block that MovePos lives in, right before
79/// MovePos.
80void Instruction::moveBefore(Instruction *MovePos) {
81 MovePos->getParent()->getInstList().splice(MovePos,getParent()->getInstList(),
82 this);
83}
84
85
Vikram S. Adve8aee7962002-07-14 23:09:40 +000086const char *Instruction::getOpcodeName(unsigned OpCode) {
87 switch (OpCode) {
88 // Terminators
Chris Lattnerb193ff82002-08-14 18:18:02 +000089 case Ret: return "ret";
90 case Br: return "br";
Vikram S. Adve8aee7962002-07-14 23:09:40 +000091 case Switch: return "switch";
92 case Invoke: return "invoke";
Chris Lattner66d5f572003-09-08 18:54:36 +000093 case Unwind: return "unwind";
Chris Lattner5e0b9f22004-10-16 18:08:06 +000094 case Unreachable: return "unreachable";
Misha Brukmanb1c93172005-04-21 23:48:37 +000095
Vikram S. Adve8aee7962002-07-14 23:09:40 +000096 // Standard binary operators...
97 case Add: return "add";
98 case Sub: return "sub";
99 case Mul: return "mul";
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000100 case UDiv: return "udiv";
101 case SDiv: return "sdiv";
102 case FDiv: return "fdiv";
Reid Spencer7eb55b32006-11-02 01:53:59 +0000103 case URem: return "urem";
104 case SRem: return "srem";
105 case FRem: return "frem";
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000106
107 // Logical operators...
108 case And: return "and";
109 case Or : return "or";
110 case Xor: return "xor";
111
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000112 // Memory instructions...
113 case Malloc: return "malloc";
114 case Free: return "free";
115 case Alloca: return "alloca";
116 case Load: return "load";
117 case Store: return "store";
118 case GetElementPtr: return "getelementptr";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000119
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000120 // Convert instructions...
121 case Trunc: return "trunc";
122 case ZExt: return "zext";
123 case SExt: return "sext";
124 case FPTrunc: return "fptrunc";
125 case FPExt: return "fpext";
126 case FPToUI: return "fptoui";
127 case FPToSI: return "fptosi";
128 case UIToFP: return "uitofp";
129 case SIToFP: return "sitofp";
130 case IntToPtr: return "inttoptr";
131 case PtrToInt: return "ptrtoint";
132 case BitCast: return "bitcast";
133
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000134 // Other instructions...
Reid Spencer45e52392006-12-03 06:27:29 +0000135 case ICmp: return "icmp";
136 case FCmp: return "fcmp";
Nate Begemand2195702008-05-12 19:01:56 +0000137 case VICmp: return "vicmp";
138 case VFCmp: return "vfcmp";
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000139 case PHI: return "phi";
140 case Select: return "select";
141 case Call: return "call";
142 case Shl: return "shl";
143 case LShr: return "lshr";
144 case AShr: return "ashr";
145 case VAArg: return "va_arg";
Robert Bocchino23004482006-01-10 19:05:34 +0000146 case ExtractElement: return "extractelement";
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000147 case InsertElement: return "insertelement";
148 case ShuffleVector: return "shufflevector";
Devang Patel35519d52008-02-21 23:02:20 +0000149 case GetResult: return "getresult";
Matthijs Kooijmanbf8d6ce2008-05-30 10:31:54 +0000150 case ExtractValue: return "extractvalue";
151 case InsertValue: return "insertvalue";
Chris Lattnerf70da102003-05-08 02:44:12 +0000152
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000153 default: return "<Invalid operator> ";
154 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000155
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000156 return 0;
157}
Chris Lattnercab6c332002-10-31 04:14:01 +0000158
Chris Lattner0b6ebd8d2004-11-30 02:51:53 +0000159/// isIdenticalTo - Return true if the specified instruction is exactly
160/// identical to the current one. This means that all operands match and any
161/// extra information (e.g. load is volatile) agree.
162bool Instruction::isIdenticalTo(Instruction *I) const {
163 if (getOpcode() != I->getOpcode() ||
164 getNumOperands() != I->getNumOperands() ||
165 getType() != I->getType())
166 return false;
167
168 // We have two instructions of identical opcode and #operands. Check to see
169 // if all operands are the same.
170 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
171 if (getOperand(i) != I->getOperand(i))
172 return false;
173
174 // Check special state that is a part of some instructions.
175 if (const LoadInst *LI = dyn_cast<LoadInst>(this))
176 return LI->isVolatile() == cast<LoadInst>(I)->isVolatile();
177 if (const StoreInst *SI = dyn_cast<StoreInst>(this))
178 return SI->isVolatile() == cast<StoreInst>(I)->isVolatile();
Reid Spencer266e42b2006-12-23 06:05:41 +0000179 if (const CmpInst *CI = dyn_cast<CmpInst>(this))
180 return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
Chris Lattner06038452005-05-06 05:51:46 +0000181 if (const CallInst *CI = dyn_cast<CallInst>(this))
182 return CI->isTailCall() == cast<CallInst>(I)->isTailCall();
Chris Lattner0b6ebd8d2004-11-30 02:51:53 +0000183 return true;
184}
185
Reid Spencer266e42b2006-12-23 06:05:41 +0000186// isSameOperationAs
187bool Instruction::isSameOperationAs(Instruction *I) const {
188 if (getOpcode() != I->getOpcode() || getType() != I->getType() ||
189 getNumOperands() != I->getNumOperands())
190 return false;
191
192 // We have two instructions of identical opcode and #operands. Check to see
193 // if all operands are the same type
194 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
195 if (getOperand(i)->getType() != I->getOperand(i)->getType())
196 return false;
197
198 // Check special state that is a part of some instructions.
199 if (const LoadInst *LI = dyn_cast<LoadInst>(this))
200 return LI->isVolatile() == cast<LoadInst>(I)->isVolatile();
201 if (const StoreInst *SI = dyn_cast<StoreInst>(this))
202 return SI->isVolatile() == cast<StoreInst>(I)->isVolatile();
203 if (const CmpInst *CI = dyn_cast<CmpInst>(this))
204 return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
205 if (const CallInst *CI = dyn_cast<CallInst>(this))
206 return CI->isTailCall() == cast<CallInst>(I)->isTailCall();
207
208 return true;
209}
210
Chris Lattner8ec6e8a2008-04-20 22:11:30 +0000211/// isUsedOutsideOfBlock - Return true if there are any uses of I outside of the
212/// specified block. Note that PHI nodes are considered to evaluate their
213/// operands in the corresponding predecessor block.
214bool Instruction::isUsedOutsideOfBlock(const BasicBlock *BB) const {
215 for (use_const_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
216 // PHI nodes uses values in the corresponding predecessor block. For other
217 // instructions, just check to see whether the parent of the use matches up.
218 const PHINode *PN = dyn_cast<PHINode>(*UI);
219 if (PN == 0) {
220 if (cast<Instruction>(*UI)->getParent() != BB)
221 return true;
222 continue;
223 }
224
225 unsigned UseOperand = UI.getOperandNo();
226 if (PN->getIncomingBlock(UseOperand/2) != BB)
227 return true;
228 }
229 return false;
230}
231
Chris Lattner84627112008-05-08 17:16:51 +0000232/// mayReadFromMemory - Return true if this instruction may read memory.
233///
234bool Instruction::mayReadFromMemory() const {
235 switch (getOpcode()) {
236 default: return false;
237 case Instruction::Free:
Chris Lattner84627112008-05-08 17:16:51 +0000238 case Instruction::VAArg:
Chris Lattner954907a2008-05-08 21:58:49 +0000239 case Instruction::Load:
Chris Lattner84627112008-05-08 17:16:51 +0000240 return true;
241 case Instruction::Call:
242 return !cast<CallInst>(this)->doesNotAccessMemory();
243 case Instruction::Invoke:
244 return !cast<InvokeInst>(this)->doesNotAccessMemory();
Chris Lattner954907a2008-05-08 21:58:49 +0000245 case Instruction::Store:
246 return cast<StoreInst>(this)->isVolatile();
Chris Lattner84627112008-05-08 17:16:51 +0000247 }
248}
Chris Lattner8ec6e8a2008-04-20 22:11:30 +0000249
Chris Lattnerc992e182007-02-15 23:15:00 +0000250/// mayWriteToMemory - Return true if this instruction may modify memory.
251///
252bool Instruction::mayWriteToMemory() const {
253 switch (getOpcode()) {
254 default: return false;
255 case Instruction::Free:
Duncan Sands38ef3a82007-12-03 20:06:50 +0000256 case Instruction::Store:
Chris Lattnerc992e182007-02-15 23:15:00 +0000257 case Instruction::VAArg:
258 return true;
259 case Instruction::Call:
Duncan Sands38ef3a82007-12-03 20:06:50 +0000260 return !cast<CallInst>(this)->onlyReadsMemory();
Chris Lattner84627112008-05-08 17:16:51 +0000261 case Instruction::Invoke:
262 return !cast<InvokeInst>(this)->onlyReadsMemory();
Chris Lattnerc992e182007-02-15 23:15:00 +0000263 case Instruction::Load:
264 return cast<LoadInst>(this)->isVolatile();
265 }
266}
Chris Lattnercab6c332002-10-31 04:14:01 +0000267
268/// isAssociative - Return true if the instruction is associative:
269///
270/// Associative operators satisfy: x op (y op z) === (x op y) op z)
271///
272/// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
273/// applied to floating point types.
274///
275bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
Chris Lattner91386802006-10-26 18:27:26 +0000276 if (Opcode == And || Opcode == Or || Opcode == Xor)
277 return true;
278
279 // Add/Mul reassociate unless they are FP or FP vectors.
280 if (Opcode == Add || Opcode == Mul)
281 return !Ty->isFPOrFPVector();
Chris Lattnercab6c332002-10-31 04:14:01 +0000282 return 0;
283}
284
285/// isCommutative - Return true if the instruction is commutative:
286///
Misha Brukmanfa100532003-10-10 17:54:14 +0000287/// Commutative operators satisfy: (x op y) === (y op x)
Chris Lattnercab6c332002-10-31 04:14:01 +0000288///
289/// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
290/// applied to any type.
291///
292bool Instruction::isCommutative(unsigned op) {
293 switch (op) {
294 case Add:
295 case Mul:
Misha Brukmanb1c93172005-04-21 23:48:37 +0000296 case And:
Chris Lattnercab6c332002-10-31 04:14:01 +0000297 case Or:
298 case Xor:
Chris Lattnercab6c332002-10-31 04:14:01 +0000299 return true;
300 default:
301 return false;
302 }
303}
Tanya Lattnera93c7ae2003-07-31 04:05:50 +0000304
Tanya Lattnera93c7ae2003-07-31 04:05:50 +0000305/// isTrappingInstruction - Return true if the instruction may trap.
306///
Tanya Lattnerf8c563f2003-07-31 05:06:09 +0000307bool Instruction::isTrapping(unsigned op) {
Tanya Lattnera93c7ae2003-07-31 04:05:50 +0000308 switch(op) {
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000309 case UDiv:
310 case SDiv:
311 case FDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +0000312 case URem:
313 case SRem:
314 case FRem:
Tanya Lattnera93c7ae2003-07-31 04:05:50 +0000315 case Load:
316 case Store:
317 case Call:
318 case Invoke:
Dan Gohman19bce7f2008-04-14 15:07:08 +0000319 case VAArg:
Tanya Lattnera93c7ae2003-07-31 04:05:50 +0000320 return true;
321 default:
322 return false;
323 }
324}