blob: 2a740fcc9589ac88f78623b0417062c5ec8f1b90 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- Instruction.cpp - Implement the Instruction class -----------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements the Instruction class for the VMCore library.
11//
12//===----------------------------------------------------------------------===//
13
Reid Spencer45fb3f32006-11-20 01:22:35 +000014#include "llvm/Type.h"
Chris Lattner38f14552004-11-30 02:51:53 +000015#include "llvm/Instructions.h"
Andrew Lenharthfadb3f72007-02-16 02:25:55 +000016#include "llvm/IntrinsicInst.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000017#include "llvm/Function.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000018#include "llvm/Support/LeakDetector.h"
Chris Lattner4b74c832003-11-20 17:45:12 +000019using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000020
Chris Lattner96d83f62005-01-29 00:35:33 +000021Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps,
22 const std::string &Name, Instruction *InsertBefore)
Chris Lattnerdec628e2007-02-12 05:18:08 +000023 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) {
Chris Lattnerd1e693f2002-09-08 18:59:35 +000024 // Make sure that we get added to a basicblock
25 LeakDetector::addGarbageObject(this);
Chris Lattner2aa83112002-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 Lattnerdec628e2007-02-12 05:18:08 +000033 setName(Name);
Chris Lattner00950542001-06-06 20:29:01 +000034}
35
Chris Lattner96d83f62005-01-29 00:35:33 +000036Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps,
37 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattnerdec628e2007-02-12 05:18:08 +000038 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) {
Chris Lattner96d83f62005-01-29 00:35:33 +000039 // Make sure that we get added to a basicblock
40 LeakDetector::addGarbageObject(this);
Alkis Evlogimenose5828f12004-05-26 21:41:09 +000041
42 // append this instruction into the basic block
43 assert(InsertAtEnd && "Basic block to append to may not be NULL!");
44 InsertAtEnd->getInstList().push_back(this);
Chris Lattnerdec628e2007-02-12 05:18:08 +000045 setName(Name);
Alkis Evlogimenose5828f12004-05-26 21:41:09 +000046}
47
Chris Lattnerf00042a2007-02-13 07:54:42 +000048Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps,
49 const char *Name, Instruction *InsertBefore)
50 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) {
51 // Make sure that we get added to a basicblock
52 LeakDetector::addGarbageObject(this);
53
54 // If requested, insert this instruction into a basic block...
55 if (InsertBefore) {
56 assert(InsertBefore->getParent() &&
57 "Instruction to insert before is not in a basic block!");
58 InsertBefore->getParent()->getInstList().insert(InsertBefore, this);
59 }
60 if (Name && *Name) setName(Name);
61}
62
63Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps,
64 const char *Name, BasicBlock *InsertAtEnd)
65 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) {
66 // Make sure that we get added to a basicblock
67 LeakDetector::addGarbageObject(this);
68
69 // append this instruction into the basic block
70 assert(InsertAtEnd && "Basic block to append to may not be NULL!");
71 InsertAtEnd->getInstList().push_back(this);
72 if (Name && *Name) setName(Name);
73}
74
75
Chris Lattner70aa33e2006-06-21 16:53:47 +000076// Out of line virtual method, so the vtable, etc has a home.
77Instruction::~Instruction() {
78 assert(Parent == 0 && "Instruction still linked in the program!");
79}
80
81
Chris Lattnerbded1322002-09-06 21:33:15 +000082void Instruction::setParent(BasicBlock *P) {
Chris Lattner786993c2004-02-04 01:06:38 +000083 if (getParent()) {
84 if (!P) LeakDetector::addGarbageObject(this);
85 } else {
86 if (P) LeakDetector::removeGarbageObject(this);
87 }
Chris Lattnerd1e693f2002-09-08 18:59:35 +000088
Chris Lattnerbded1322002-09-06 21:33:15 +000089 Parent = P;
90}
91
Chris Lattner4b833802004-10-11 22:21:39 +000092void Instruction::removeFromParent() {
93 getParent()->getInstList().remove(this);
94}
95
96void Instruction::eraseFromParent() {
97 getParent()->getInstList().erase(this);
98}
Vikram S. Advec1056452002-07-14 23:09:40 +000099
Chris Lattner0fe34d82005-08-08 05:21:50 +0000100/// moveBefore - Unlink this instruction from its current basic block and
101/// insert it into the basic block that MovePos lives in, right before
102/// MovePos.
103void Instruction::moveBefore(Instruction *MovePos) {
104 MovePos->getParent()->getInstList().splice(MovePos,getParent()->getInstList(),
105 this);
106}
107
108
Vikram S. Advec1056452002-07-14 23:09:40 +0000109const char *Instruction::getOpcodeName(unsigned OpCode) {
110 switch (OpCode) {
111 // Terminators
Chris Lattner0513e9f2002-08-14 18:18:02 +0000112 case Ret: return "ret";
113 case Br: return "br";
Vikram S. Advec1056452002-07-14 23:09:40 +0000114 case Switch: return "switch";
115 case Invoke: return "invoke";
Chris Lattnera6ce8982003-09-08 18:54:36 +0000116 case Unwind: return "unwind";
Chris Lattnerb976e662004-10-16 18:08:06 +0000117 case Unreachable: return "unreachable";
Misha Brukmanfd939082005-04-21 23:48:37 +0000118
Vikram S. Advec1056452002-07-14 23:09:40 +0000119 // Standard binary operators...
120 case Add: return "add";
121 case Sub: return "sub";
122 case Mul: return "mul";
Reid Spencer1628cec2006-10-26 06:15:43 +0000123 case UDiv: return "udiv";
124 case SDiv: return "sdiv";
125 case FDiv: return "fdiv";
Reid Spencer0a783f72006-11-02 01:53:59 +0000126 case URem: return "urem";
127 case SRem: return "srem";
128 case FRem: return "frem";
Vikram S. Advec1056452002-07-14 23:09:40 +0000129
130 // Logical operators...
131 case And: return "and";
132 case Or : return "or";
133 case Xor: return "xor";
134
Vikram S. Advec1056452002-07-14 23:09:40 +0000135 // Memory instructions...
136 case Malloc: return "malloc";
137 case Free: return "free";
138 case Alloca: return "alloca";
139 case Load: return "load";
140 case Store: return "store";
141 case GetElementPtr: return "getelementptr";
Misha Brukmanfd939082005-04-21 23:48:37 +0000142
Reid Spencer3da59db2006-11-27 01:05:10 +0000143 // Convert instructions...
144 case Trunc: return "trunc";
145 case ZExt: return "zext";
146 case SExt: return "sext";
147 case FPTrunc: return "fptrunc";
148 case FPExt: return "fpext";
149 case FPToUI: return "fptoui";
150 case FPToSI: return "fptosi";
151 case UIToFP: return "uitofp";
152 case SIToFP: return "sitofp";
153 case IntToPtr: return "inttoptr";
154 case PtrToInt: return "ptrtoint";
155 case BitCast: return "bitcast";
156
Vikram S. Advec1056452002-07-14 23:09:40 +0000157 // Other instructions...
Reid Spencer74f16422006-12-03 06:27:29 +0000158 case ICmp: return "icmp";
159 case FCmp: return "fcmp";
Reid Spencer3da59db2006-11-27 01:05:10 +0000160 case PHI: return "phi";
161 case Select: return "select";
162 case Call: return "call";
163 case Shl: return "shl";
164 case LShr: return "lshr";
165 case AShr: return "ashr";
166 case VAArg: return "va_arg";
Robert Bocchinob52ee7f2006-01-10 19:05:34 +0000167 case ExtractElement: return "extractelement";
Reid Spencer3da59db2006-11-27 01:05:10 +0000168 case InsertElement: return "insertelement";
169 case ShuffleVector: return "shufflevector";
Chris Lattner8f77dae2003-05-08 02:44:12 +0000170
Vikram S. Advec1056452002-07-14 23:09:40 +0000171 default: return "<Invalid operator> ";
172 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000173
Vikram S. Advec1056452002-07-14 23:09:40 +0000174 return 0;
175}
Chris Lattnerf2da7242002-10-31 04:14:01 +0000176
Chris Lattner38f14552004-11-30 02:51:53 +0000177/// isIdenticalTo - Return true if the specified instruction is exactly
178/// identical to the current one. This means that all operands match and any
179/// extra information (e.g. load is volatile) agree.
180bool Instruction::isIdenticalTo(Instruction *I) const {
181 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))
194 return LI->isVolatile() == cast<LoadInst>(I)->isVolatile();
195 if (const StoreInst *SI = dyn_cast<StoreInst>(this))
196 return SI->isVolatile() == cast<StoreInst>(I)->isVolatile();
Reid Spencere4d87aa2006-12-23 06:05:41 +0000197 if (const CmpInst *CI = dyn_cast<CmpInst>(this))
198 return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
Chris Lattnerddb6db42005-05-06 05:51:46 +0000199 if (const CallInst *CI = dyn_cast<CallInst>(this))
200 return CI->isTailCall() == cast<CallInst>(I)->isTailCall();
Chris Lattner38f14552004-11-30 02:51:53 +0000201 return true;
202}
203
Reid Spencere4d87aa2006-12-23 06:05:41 +0000204// isSameOperationAs
205bool Instruction::isSameOperationAs(Instruction *I) const {
206 if (getOpcode() != I->getOpcode() || getType() != I->getType() ||
207 getNumOperands() != I->getNumOperands())
208 return false;
209
210 // We have two instructions of identical opcode and #operands. Check to see
211 // if all operands are the same type
212 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
213 if (getOperand(i)->getType() != I->getOperand(i)->getType())
214 return false;
215
216 // Check special state that is a part of some instructions.
217 if (const LoadInst *LI = dyn_cast<LoadInst>(this))
218 return LI->isVolatile() == cast<LoadInst>(I)->isVolatile();
219 if (const StoreInst *SI = dyn_cast<StoreInst>(this))
220 return SI->isVolatile() == cast<StoreInst>(I)->isVolatile();
221 if (const CmpInst *CI = dyn_cast<CmpInst>(this))
222 return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
223 if (const CallInst *CI = dyn_cast<CallInst>(this))
224 return CI->isTailCall() == cast<CallInst>(I)->isTailCall();
225
226 return true;
227}
228
Chris Lattnerbb5493d2007-02-15 23:15:00 +0000229/// mayWriteToMemory - Return true if this instruction may modify memory.
230///
231bool Instruction::mayWriteToMemory() const {
232 switch (getOpcode()) {
233 default: return false;
234 case Instruction::Free:
235 case Instruction::Store:
236 case Instruction::Invoke:
237 case Instruction::VAArg:
238 return true;
239 case Instruction::Call:
Chris Lattnerda304d02007-02-19 19:46:17 +0000240 //if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(this)) {
Chris Lattnerbb5493d2007-02-15 23:15:00 +0000241 // If the intrinsic doesn't write memory, it is safe.
Reid Spencerc552ff22007-02-19 19:00:29 +0000242 // FIXME: this is obviously supposed to determine which intrinsics
243 // don't write to memory, but hasn't been implemented yet.
Chris Lattnerda304d02007-02-19 19:46:17 +0000244 //}
Chris Lattnerbb5493d2007-02-15 23:15:00 +0000245 return true;
246 case Instruction::Load:
247 return cast<LoadInst>(this)->isVolatile();
248 }
249}
Chris Lattnerf2da7242002-10-31 04:14:01 +0000250
251/// isAssociative - Return true if the instruction is associative:
252///
253/// Associative operators satisfy: x op (y op z) === (x op y) op z)
254///
255/// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
256/// applied to floating point types.
257///
258bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
Chris Lattner4d3839d2006-10-26 18:27:26 +0000259 if (Opcode == And || Opcode == Or || Opcode == Xor)
260 return true;
261
262 // Add/Mul reassociate unless they are FP or FP vectors.
263 if (Opcode == Add || Opcode == Mul)
264 return !Ty->isFPOrFPVector();
Chris Lattnerf2da7242002-10-31 04:14:01 +0000265 return 0;
266}
267
268/// isCommutative - Return true if the instruction is commutative:
269///
Misha Brukman6b634522003-10-10 17:54:14 +0000270/// Commutative operators satisfy: (x op y) === (y op x)
Chris Lattnerf2da7242002-10-31 04:14:01 +0000271///
272/// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
273/// applied to any type.
274///
275bool Instruction::isCommutative(unsigned op) {
276 switch (op) {
277 case Add:
278 case Mul:
Misha Brukmanfd939082005-04-21 23:48:37 +0000279 case And:
Chris Lattnerf2da7242002-10-31 04:14:01 +0000280 case Or:
281 case Xor:
Chris Lattnerf2da7242002-10-31 04:14:01 +0000282 return true;
283 default:
284 return false;
285 }
286}
Tanya Lattner741bb002003-07-31 04:05:50 +0000287
Tanya Lattner741bb002003-07-31 04:05:50 +0000288/// isTrappingInstruction - Return true if the instruction may trap.
289///
Tanya Lattnerec127bb2003-07-31 05:06:09 +0000290bool Instruction::isTrapping(unsigned op) {
Tanya Lattner741bb002003-07-31 04:05:50 +0000291 switch(op) {
Reid Spencer1628cec2006-10-26 06:15:43 +0000292 case UDiv:
293 case SDiv:
294 case FDiv:
Reid Spencer0a783f72006-11-02 01:53:59 +0000295 case URem:
296 case SRem:
297 case FRem:
Tanya Lattner741bb002003-07-31 04:05:50 +0000298 case Load:
299 case Store:
300 case Call:
301 case Invoke:
302 return true;
303 default:
304 return false;
305 }
306}