blob: 39a895510ae27b4c2810127a54fca6c2d0c499e7 [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"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000016#include "llvm/Function.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000017#include "llvm/Support/LeakDetector.h"
Chris Lattner4b74c832003-11-20 17:45:12 +000018using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000019
Chris Lattner96d83f62005-01-29 00:35:33 +000020Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps,
21 const std::string &Name, Instruction *InsertBefore)
Chris Lattnerdec628e2007-02-12 05:18:08 +000022 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) {
Chris Lattnerd1e693f2002-09-08 18:59:35 +000023 // Make sure that we get added to a basicblock
24 LeakDetector::addGarbageObject(this);
Chris Lattner2aa83112002-09-10 15:45:53 +000025
26 // If requested, insert this instruction into a basic block...
27 if (InsertBefore) {
28 assert(InsertBefore->getParent() &&
29 "Instruction to insert before is not in a basic block!");
30 InsertBefore->getParent()->getInstList().insert(InsertBefore, this);
31 }
Chris Lattnerdec628e2007-02-12 05:18:08 +000032 setName(Name);
Chris Lattner00950542001-06-06 20:29:01 +000033}
34
Chris Lattner96d83f62005-01-29 00:35:33 +000035Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps,
36 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattnerdec628e2007-02-12 05:18:08 +000037 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) {
Chris Lattner96d83f62005-01-29 00:35:33 +000038 // Make sure that we get added to a basicblock
39 LeakDetector::addGarbageObject(this);
Alkis Evlogimenose5828f12004-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 Lattnerdec628e2007-02-12 05:18:08 +000044 setName(Name);
Alkis Evlogimenose5828f12004-05-26 21:41:09 +000045}
46
Chris Lattnerf00042a2007-02-13 07:54:42 +000047Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps,
48 const char *Name, Instruction *InsertBefore)
49 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) {
50 // Make sure that we get added to a basicblock
51 LeakDetector::addGarbageObject(this);
52
53 // If requested, insert this instruction into a basic block...
54 if (InsertBefore) {
55 assert(InsertBefore->getParent() &&
56 "Instruction to insert before is not in a basic block!");
57 InsertBefore->getParent()->getInstList().insert(InsertBefore, this);
58 }
59 if (Name && *Name) setName(Name);
60}
61
62Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps,
63 const char *Name, BasicBlock *InsertAtEnd)
64 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) {
65 // Make sure that we get added to a basicblock
66 LeakDetector::addGarbageObject(this);
67
68 // append this instruction into the basic block
69 assert(InsertAtEnd && "Basic block to append to may not be NULL!");
70 InsertAtEnd->getInstList().push_back(this);
71 if (Name && *Name) setName(Name);
72}
73
74
Chris Lattner70aa33e2006-06-21 16:53:47 +000075// Out of line virtual method, so the vtable, etc has a home.
76Instruction::~Instruction() {
77 assert(Parent == 0 && "Instruction still linked in the program!");
78}
79
80
Chris Lattnerbded1322002-09-06 21:33:15 +000081void Instruction::setParent(BasicBlock *P) {
Chris Lattner786993c2004-02-04 01:06:38 +000082 if (getParent()) {
83 if (!P) LeakDetector::addGarbageObject(this);
84 } else {
85 if (P) LeakDetector::removeGarbageObject(this);
86 }
Chris Lattnerd1e693f2002-09-08 18:59:35 +000087
Chris Lattnerbded1322002-09-06 21:33:15 +000088 Parent = P;
89}
90
Chris Lattner4b833802004-10-11 22:21:39 +000091void Instruction::removeFromParent() {
92 getParent()->getInstList().remove(this);
93}
94
95void Instruction::eraseFromParent() {
96 getParent()->getInstList().erase(this);
97}
Vikram S. Advec1056452002-07-14 23:09:40 +000098
Chris Lattner0fe34d82005-08-08 05:21:50 +000099/// moveBefore - Unlink this instruction from its current basic block and
100/// insert it into the basic block that MovePos lives in, right before
101/// MovePos.
102void Instruction::moveBefore(Instruction *MovePos) {
103 MovePos->getParent()->getInstList().splice(MovePos,getParent()->getInstList(),
104 this);
105}
106
107
Vikram S. Advec1056452002-07-14 23:09:40 +0000108const char *Instruction::getOpcodeName(unsigned OpCode) {
109 switch (OpCode) {
110 // Terminators
Chris Lattner0513e9f2002-08-14 18:18:02 +0000111 case Ret: return "ret";
112 case Br: return "br";
Vikram S. Advec1056452002-07-14 23:09:40 +0000113 case Switch: return "switch";
114 case Invoke: return "invoke";
Chris Lattnera6ce8982003-09-08 18:54:36 +0000115 case Unwind: return "unwind";
Chris Lattnerb976e662004-10-16 18:08:06 +0000116 case Unreachable: return "unreachable";
Misha Brukmanfd939082005-04-21 23:48:37 +0000117
Vikram S. Advec1056452002-07-14 23:09:40 +0000118 // Standard binary operators...
119 case Add: return "add";
120 case Sub: return "sub";
121 case Mul: return "mul";
Reid Spencer1628cec2006-10-26 06:15:43 +0000122 case UDiv: return "udiv";
123 case SDiv: return "sdiv";
124 case FDiv: return "fdiv";
Reid Spencer0a783f72006-11-02 01:53:59 +0000125 case URem: return "urem";
126 case SRem: return "srem";
127 case FRem: return "frem";
Vikram S. Advec1056452002-07-14 23:09:40 +0000128
129 // Logical operators...
130 case And: return "and";
131 case Or : return "or";
132 case Xor: return "xor";
133
Vikram S. Advec1056452002-07-14 23:09:40 +0000134 // Memory instructions...
135 case Malloc: return "malloc";
136 case Free: return "free";
137 case Alloca: return "alloca";
138 case Load: return "load";
139 case Store: return "store";
140 case GetElementPtr: return "getelementptr";
Misha Brukmanfd939082005-04-21 23:48:37 +0000141
Reid Spencer3da59db2006-11-27 01:05:10 +0000142 // Convert instructions...
143 case Trunc: return "trunc";
144 case ZExt: return "zext";
145 case SExt: return "sext";
146 case FPTrunc: return "fptrunc";
147 case FPExt: return "fpext";
148 case FPToUI: return "fptoui";
149 case FPToSI: return "fptosi";
150 case UIToFP: return "uitofp";
151 case SIToFP: return "sitofp";
152 case IntToPtr: return "inttoptr";
153 case PtrToInt: return "ptrtoint";
154 case BitCast: return "bitcast";
155
Vikram S. Advec1056452002-07-14 23:09:40 +0000156 // Other instructions...
Reid Spencer74f16422006-12-03 06:27:29 +0000157 case ICmp: return "icmp";
158 case FCmp: return "fcmp";
Reid Spencer3da59db2006-11-27 01:05:10 +0000159 case PHI: return "phi";
160 case Select: return "select";
161 case Call: return "call";
162 case Shl: return "shl";
163 case LShr: return "lshr";
164 case AShr: return "ashr";
165 case VAArg: return "va_arg";
Robert Bocchinob52ee7f2006-01-10 19:05:34 +0000166 case ExtractElement: return "extractelement";
Reid Spencer3da59db2006-11-27 01:05:10 +0000167 case InsertElement: return "insertelement";
168 case ShuffleVector: return "shufflevector";
Chris Lattner8f77dae2003-05-08 02:44:12 +0000169
Vikram S. Advec1056452002-07-14 23:09:40 +0000170 default: return "<Invalid operator> ";
171 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000172
Vikram S. Advec1056452002-07-14 23:09:40 +0000173 return 0;
174}
Chris Lattnerf2da7242002-10-31 04:14:01 +0000175
Chris Lattner38f14552004-11-30 02:51:53 +0000176/// isIdenticalTo - Return true if the specified instruction is exactly
177/// identical to the current one. This means that all operands match and any
178/// extra information (e.g. load is volatile) agree.
179bool Instruction::isIdenticalTo(Instruction *I) const {
180 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))
193 return LI->isVolatile() == cast<LoadInst>(I)->isVolatile();
194 if (const StoreInst *SI = dyn_cast<StoreInst>(this))
195 return SI->isVolatile() == cast<StoreInst>(I)->isVolatile();
Reid Spencere4d87aa2006-12-23 06:05:41 +0000196 if (const CmpInst *CI = dyn_cast<CmpInst>(this))
197 return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
Chris Lattnerddb6db42005-05-06 05:51:46 +0000198 if (const CallInst *CI = dyn_cast<CallInst>(this))
199 return CI->isTailCall() == cast<CallInst>(I)->isTailCall();
Chris Lattner38f14552004-11-30 02:51:53 +0000200 return true;
201}
202
Reid Spencere4d87aa2006-12-23 06:05:41 +0000203// isSameOperationAs
204bool Instruction::isSameOperationAs(Instruction *I) const {
205 if (getOpcode() != I->getOpcode() || getType() != I->getType() ||
206 getNumOperands() != I->getNumOperands())
207 return false;
208
209 // We have two instructions of identical opcode and #operands. Check to see
210 // if all operands are the same type
211 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
212 if (getOperand(i)->getType() != I->getOperand(i)->getType())
213 return false;
214
215 // Check special state that is a part of some instructions.
216 if (const LoadInst *LI = dyn_cast<LoadInst>(this))
217 return LI->isVolatile() == cast<LoadInst>(I)->isVolatile();
218 if (const StoreInst *SI = dyn_cast<StoreInst>(this))
219 return SI->isVolatile() == cast<StoreInst>(I)->isVolatile();
220 if (const CmpInst *CI = dyn_cast<CmpInst>(this))
221 return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
222 if (const CallInst *CI = dyn_cast<CallInst>(this))
223 return CI->isTailCall() == cast<CallInst>(I)->isTailCall();
224
225 return true;
226}
227
Chris Lattnerf2da7242002-10-31 04:14:01 +0000228
229/// isAssociative - Return true if the instruction is associative:
230///
231/// Associative operators satisfy: x op (y op z) === (x op y) op z)
232///
233/// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
234/// applied to floating point types.
235///
236bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
Chris Lattner4d3839d2006-10-26 18:27:26 +0000237 if (Opcode == And || Opcode == Or || Opcode == Xor)
238 return true;
239
240 // Add/Mul reassociate unless they are FP or FP vectors.
241 if (Opcode == Add || Opcode == Mul)
242 return !Ty->isFPOrFPVector();
Chris Lattnerf2da7242002-10-31 04:14:01 +0000243 return 0;
244}
245
246/// isCommutative - Return true if the instruction is commutative:
247///
Misha Brukman6b634522003-10-10 17:54:14 +0000248/// Commutative operators satisfy: (x op y) === (y op x)
Chris Lattnerf2da7242002-10-31 04:14:01 +0000249///
250/// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
251/// applied to any type.
252///
253bool Instruction::isCommutative(unsigned op) {
254 switch (op) {
255 case Add:
256 case Mul:
Misha Brukmanfd939082005-04-21 23:48:37 +0000257 case And:
Chris Lattnerf2da7242002-10-31 04:14:01 +0000258 case Or:
259 case Xor:
Chris Lattnerf2da7242002-10-31 04:14:01 +0000260 return true;
261 default:
262 return false;
263 }
264}
Tanya Lattner741bb002003-07-31 04:05:50 +0000265
Tanya Lattner741bb002003-07-31 04:05:50 +0000266/// isTrappingInstruction - Return true if the instruction may trap.
267///
Tanya Lattnerec127bb2003-07-31 05:06:09 +0000268bool Instruction::isTrapping(unsigned op) {
Tanya Lattner741bb002003-07-31 04:05:50 +0000269 switch(op) {
Reid Spencer1628cec2006-10-26 06:15:43 +0000270 case UDiv:
271 case SDiv:
272 case FDiv:
Reid Spencer0a783f72006-11-02 01:53:59 +0000273 case URem:
274 case SRem:
275 case FRem:
Tanya Lattner741bb002003-07-31 04:05:50 +0000276 case Load:
277 case Store:
278 case Call:
279 case Invoke:
280 return true;
281 default:
282 return false;
283 }
284}