blob: 067a4bb639f869c781b1f5f49ba0f5fa71501c63 [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";
Matthijs Kooijmanbf8d6ce2008-05-30 10:31:54 +0000149 case ExtractValue: return "extractvalue";
150 case InsertValue: return "insertvalue";
Chris Lattnerf70da102003-05-08 02:44:12 +0000151
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000152 default: return "<Invalid operator> ";
153 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000154
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000155 return 0;
156}
Chris Lattnercab6c332002-10-31 04:14:01 +0000157
Chris Lattner0b6ebd8d2004-11-30 02:51:53 +0000158/// isIdenticalTo - Return true if the specified instruction is exactly
159/// identical to the current one. This means that all operands match and any
160/// extra information (e.g. load is volatile) agree.
161bool Instruction::isIdenticalTo(Instruction *I) const {
162 if (getOpcode() != I->getOpcode() ||
163 getNumOperands() != I->getNumOperands() ||
164 getType() != I->getType())
165 return false;
166
167 // We have two instructions of identical opcode and #operands. Check to see
168 // if all operands are the same.
169 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
170 if (getOperand(i) != I->getOperand(i))
171 return false;
172
173 // Check special state that is a part of some instructions.
174 if (const LoadInst *LI = dyn_cast<LoadInst>(this))
175 return LI->isVolatile() == cast<LoadInst>(I)->isVolatile();
176 if (const StoreInst *SI = dyn_cast<StoreInst>(this))
177 return SI->isVolatile() == cast<StoreInst>(I)->isVolatile();
Reid Spencer266e42b2006-12-23 06:05:41 +0000178 if (const CmpInst *CI = dyn_cast<CmpInst>(this))
179 return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
Chris Lattner06038452005-05-06 05:51:46 +0000180 if (const CallInst *CI = dyn_cast<CallInst>(this))
181 return CI->isTailCall() == cast<CallInst>(I)->isTailCall();
Chris Lattner0b6ebd8d2004-11-30 02:51:53 +0000182 return true;
183}
184
Reid Spencer266e42b2006-12-23 06:05:41 +0000185// isSameOperationAs
186bool Instruction::isSameOperationAs(Instruction *I) const {
187 if (getOpcode() != I->getOpcode() || getType() != I->getType() ||
188 getNumOperands() != I->getNumOperands())
189 return false;
190
191 // We have two instructions of identical opcode and #operands. Check to see
192 // if all operands are the same type
193 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
194 if (getOperand(i)->getType() != I->getOperand(i)->getType())
195 return false;
196
197 // Check special state that is a part of some instructions.
198 if (const LoadInst *LI = dyn_cast<LoadInst>(this))
199 return LI->isVolatile() == cast<LoadInst>(I)->isVolatile();
200 if (const StoreInst *SI = dyn_cast<StoreInst>(this))
201 return SI->isVolatile() == cast<StoreInst>(I)->isVolatile();
202 if (const CmpInst *CI = dyn_cast<CmpInst>(this))
203 return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
204 if (const CallInst *CI = dyn_cast<CallInst>(this))
205 return CI->isTailCall() == cast<CallInst>(I)->isTailCall();
206
207 return true;
208}
209
Chris Lattner8ec6e8a2008-04-20 22:11:30 +0000210/// isUsedOutsideOfBlock - Return true if there are any uses of I outside of the
211/// specified block. Note that PHI nodes are considered to evaluate their
212/// operands in the corresponding predecessor block.
213bool Instruction::isUsedOutsideOfBlock(const BasicBlock *BB) const {
214 for (use_const_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
215 // PHI nodes uses values in the corresponding predecessor block. For other
216 // instructions, just check to see whether the parent of the use matches up.
217 const PHINode *PN = dyn_cast<PHINode>(*UI);
218 if (PN == 0) {
219 if (cast<Instruction>(*UI)->getParent() != BB)
220 return true;
221 continue;
222 }
223
224 unsigned UseOperand = UI.getOperandNo();
225 if (PN->getIncomingBlock(UseOperand/2) != BB)
226 return true;
227 }
228 return false;
229}
230
Chris Lattner84627112008-05-08 17:16:51 +0000231/// mayReadFromMemory - Return true if this instruction may read memory.
232///
233bool Instruction::mayReadFromMemory() const {
234 switch (getOpcode()) {
235 default: return false;
236 case Instruction::Free:
Chris Lattner84627112008-05-08 17:16:51 +0000237 case Instruction::VAArg:
Chris Lattner954907a2008-05-08 21:58:49 +0000238 case Instruction::Load:
Chris Lattner84627112008-05-08 17:16:51 +0000239 return true;
240 case Instruction::Call:
241 return !cast<CallInst>(this)->doesNotAccessMemory();
242 case Instruction::Invoke:
243 return !cast<InvokeInst>(this)->doesNotAccessMemory();
Chris Lattner954907a2008-05-08 21:58:49 +0000244 case Instruction::Store:
245 return cast<StoreInst>(this)->isVolatile();
Chris Lattner84627112008-05-08 17:16:51 +0000246 }
247}
Chris Lattner8ec6e8a2008-04-20 22:11:30 +0000248
Chris Lattnerc992e182007-02-15 23:15:00 +0000249/// mayWriteToMemory - Return true if this instruction may modify memory.
250///
251bool Instruction::mayWriteToMemory() const {
252 switch (getOpcode()) {
253 default: return false;
254 case Instruction::Free:
Duncan Sands38ef3a82007-12-03 20:06:50 +0000255 case Instruction::Store:
Chris Lattnerc992e182007-02-15 23:15:00 +0000256 case Instruction::VAArg:
257 return true;
258 case Instruction::Call:
Duncan Sands38ef3a82007-12-03 20:06:50 +0000259 return !cast<CallInst>(this)->onlyReadsMemory();
Chris Lattner84627112008-05-08 17:16:51 +0000260 case Instruction::Invoke:
261 return !cast<InvokeInst>(this)->onlyReadsMemory();
Chris Lattnerc992e182007-02-15 23:15:00 +0000262 case Instruction::Load:
263 return cast<LoadInst>(this)->isVolatile();
264 }
265}
Chris Lattnercab6c332002-10-31 04:14:01 +0000266
267/// isAssociative - Return true if the instruction is associative:
268///
269/// Associative operators satisfy: x op (y op z) === (x op y) op z)
270///
271/// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
272/// applied to floating point types.
273///
274bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
Chris Lattner91386802006-10-26 18:27:26 +0000275 if (Opcode == And || Opcode == Or || Opcode == Xor)
276 return true;
277
278 // Add/Mul reassociate unless they are FP or FP vectors.
279 if (Opcode == Add || Opcode == Mul)
280 return !Ty->isFPOrFPVector();
Chris Lattnercab6c332002-10-31 04:14:01 +0000281 return 0;
282}
283
284/// isCommutative - Return true if the instruction is commutative:
285///
Misha Brukmanfa100532003-10-10 17:54:14 +0000286/// Commutative operators satisfy: (x op y) === (y op x)
Chris Lattnercab6c332002-10-31 04:14:01 +0000287///
288/// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
289/// applied to any type.
290///
291bool Instruction::isCommutative(unsigned op) {
292 switch (op) {
293 case Add:
294 case Mul:
Misha Brukmanb1c93172005-04-21 23:48:37 +0000295 case And:
Chris Lattnercab6c332002-10-31 04:14:01 +0000296 case Or:
297 case Xor:
Chris Lattnercab6c332002-10-31 04:14:01 +0000298 return true;
299 default:
300 return false;
301 }
302}
Tanya Lattnera93c7ae2003-07-31 04:05:50 +0000303
Dan Gohman78471422008-10-15 22:56:21 +0000304/// isTrapping - Return true if the instruction may trap.
Tanya Lattnera93c7ae2003-07-31 04:05:50 +0000305///
Tanya Lattnerf8c563f2003-07-31 05:06:09 +0000306bool Instruction::isTrapping(unsigned op) {
Tanya Lattnera93c7ae2003-07-31 04:05:50 +0000307 switch(op) {
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000308 case UDiv:
309 case SDiv:
310 case FDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +0000311 case URem:
312 case SRem:
313 case FRem:
Tanya Lattnera93c7ae2003-07-31 04:05:50 +0000314 case Load:
315 case Store:
316 case Call:
317 case Invoke:
Dan Gohman19bce7f2008-04-14 15:07:08 +0000318 case VAArg:
Tanya Lattnera93c7ae2003-07-31 04:05:50 +0000319 return true;
320 default:
321 return false;
322 }
323}