blob: 2891269979122de4eb091a1eb4fb8a85d6a5a073 [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//
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 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"
Chris Lattner2f7c9632001-06-06 20:29:01 +000017#include "llvm/SymbolTable.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,
22 const std::string &Name, Instruction *InsertBefore)
23 : User(ty, Value::InstructionVal + it, Ops, NumOps, Name), 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,
36 const std::string &Name, BasicBlock *InsertAtEnd)
37 : User(ty, Value::InstructionVal + it, Ops, NumOps, Name), Parent(0) {
38 // 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);
44}
45
Chris Lattner1c12a882006-06-21 16:53:47 +000046// Out of line virtual method, so the vtable, etc has a home.
47Instruction::~Instruction() {
48 assert(Parent == 0 && "Instruction still linked in the program!");
49}
50
51
Chris Lattnerdd7a7072004-06-27 18:38:48 +000052void Instruction::setOpcode(unsigned opc) {
53 setValueType(Value::InstructionVal + opc);
54}
55
Chris Lattner9ed7aef2002-09-06 21:33:15 +000056void Instruction::setParent(BasicBlock *P) {
Chris Lattnerd8a232b2004-02-04 01:06:38 +000057 if (getParent()) {
58 if (!P) LeakDetector::addGarbageObject(this);
59 } else {
60 if (P) LeakDetector::removeGarbageObject(this);
61 }
Chris Lattner184b2982002-09-08 18:59:35 +000062
Chris Lattner9ed7aef2002-09-06 21:33:15 +000063 Parent = P;
64}
65
Chris Lattner02a71e72004-10-11 22:21:39 +000066void Instruction::removeFromParent() {
67 getParent()->getInstList().remove(this);
68}
69
70void Instruction::eraseFromParent() {
71 getParent()->getInstList().erase(this);
72}
Vikram S. Adve8aee7962002-07-14 23:09:40 +000073
Chris Lattner24a0a432005-08-08 05:21:50 +000074/// moveBefore - Unlink this instruction from its current basic block and
75/// insert it into the basic block that MovePos lives in, right before
76/// MovePos.
77void Instruction::moveBefore(Instruction *MovePos) {
78 MovePos->getParent()->getInstList().splice(MovePos,getParent()->getInstList(),
79 this);
80}
81
82
Vikram S. Adve8aee7962002-07-14 23:09:40 +000083const char *Instruction::getOpcodeName(unsigned OpCode) {
84 switch (OpCode) {
85 // Terminators
Chris Lattnerb193ff82002-08-14 18:18:02 +000086 case Ret: return "ret";
87 case Br: return "br";
Vikram S. Adve8aee7962002-07-14 23:09:40 +000088 case Switch: return "switch";
89 case Invoke: return "invoke";
Chris Lattner66d5f572003-09-08 18:54:36 +000090 case Unwind: return "unwind";
Chris Lattner5e0b9f22004-10-16 18:08:06 +000091 case Unreachable: return "unreachable";
Misha Brukmanb1c93172005-04-21 23:48:37 +000092
Vikram S. Adve8aee7962002-07-14 23:09:40 +000093 // Standard binary operators...
94 case Add: return "add";
95 case Sub: return "sub";
96 case Mul: return "mul";
Reid Spencer7e80b0b2006-10-26 06:15:43 +000097 case UDiv: return "udiv";
98 case SDiv: return "sdiv";
99 case FDiv: return "fdiv";
Reid Spencer7eb55b32006-11-02 01:53:59 +0000100 case URem: return "urem";
101 case SRem: return "srem";
102 case FRem: return "frem";
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000103
104 // Logical operators...
105 case And: return "and";
106 case Or : return "or";
107 case Xor: return "xor";
108
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000109 // Memory instructions...
110 case Malloc: return "malloc";
111 case Free: return "free";
112 case Alloca: return "alloca";
113 case Load: return "load";
114 case Store: return "store";
115 case GetElementPtr: return "getelementptr";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000116
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000117 // Convert instructions...
118 case Trunc: return "trunc";
119 case ZExt: return "zext";
120 case SExt: return "sext";
121 case FPTrunc: return "fptrunc";
122 case FPExt: return "fpext";
123 case FPToUI: return "fptoui";
124 case FPToSI: return "fptosi";
125 case UIToFP: return "uitofp";
126 case SIToFP: return "sitofp";
127 case IntToPtr: return "inttoptr";
128 case PtrToInt: return "ptrtoint";
129 case BitCast: return "bitcast";
130
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000131 // Other instructions...
Reid Spencer45e52392006-12-03 06:27:29 +0000132 case ICmp: return "icmp";
133 case FCmp: return "fcmp";
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000134 case PHI: return "phi";
135 case Select: return "select";
136 case Call: return "call";
137 case Shl: return "shl";
138 case LShr: return "lshr";
139 case AShr: return "ashr";
140 case VAArg: return "va_arg";
Robert Bocchino23004482006-01-10 19:05:34 +0000141 case ExtractElement: return "extractelement";
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000142 case InsertElement: return "insertelement";
143 case ShuffleVector: return "shufflevector";
Chris Lattnerf70da102003-05-08 02:44:12 +0000144
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000145 default: return "<Invalid operator> ";
146 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000147
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000148 return 0;
149}
Chris Lattnercab6c332002-10-31 04:14:01 +0000150
Chris Lattner0b6ebd8d2004-11-30 02:51:53 +0000151/// isIdenticalTo - Return true if the specified instruction is exactly
152/// identical to the current one. This means that all operands match and any
153/// extra information (e.g. load is volatile) agree.
154bool Instruction::isIdenticalTo(Instruction *I) const {
155 if (getOpcode() != I->getOpcode() ||
156 getNumOperands() != I->getNumOperands() ||
157 getType() != I->getType())
158 return false;
159
160 // We have two instructions of identical opcode and #operands. Check to see
161 // if all operands are the same.
162 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
163 if (getOperand(i) != I->getOperand(i))
164 return false;
165
166 // Check special state that is a part of some instructions.
167 if (const LoadInst *LI = dyn_cast<LoadInst>(this))
168 return LI->isVolatile() == cast<LoadInst>(I)->isVolatile();
169 if (const StoreInst *SI = dyn_cast<StoreInst>(this))
170 return SI->isVolatile() == cast<StoreInst>(I)->isVolatile();
Reid Spencer266e42b2006-12-23 06:05:41 +0000171 if (const CmpInst *CI = dyn_cast<CmpInst>(this))
172 return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
Chris Lattner06038452005-05-06 05:51:46 +0000173 if (const CallInst *CI = dyn_cast<CallInst>(this))
174 return CI->isTailCall() == cast<CallInst>(I)->isTailCall();
Chris Lattner0b6ebd8d2004-11-30 02:51:53 +0000175 return true;
176}
177
Reid Spencer266e42b2006-12-23 06:05:41 +0000178// isSameOperationAs
179bool Instruction::isSameOperationAs(Instruction *I) const {
180 if (getOpcode() != I->getOpcode() || getType() != I->getType() ||
181 getNumOperands() != I->getNumOperands())
182 return false;
183
184 // We have two instructions of identical opcode and #operands. Check to see
185 // if all operands are the same type
186 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
187 if (getOperand(i)->getType() != I->getOperand(i)->getType())
188 return false;
189
190 // Check special state that is a part of some instructions.
191 if (const LoadInst *LI = dyn_cast<LoadInst>(this))
192 return LI->isVolatile() == cast<LoadInst>(I)->isVolatile();
193 if (const StoreInst *SI = dyn_cast<StoreInst>(this))
194 return SI->isVolatile() == cast<StoreInst>(I)->isVolatile();
195 if (const CmpInst *CI = dyn_cast<CmpInst>(this))
196 return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
197 if (const CallInst *CI = dyn_cast<CallInst>(this))
198 return CI->isTailCall() == cast<CallInst>(I)->isTailCall();
199
200 return true;
201}
202
Chris Lattnercab6c332002-10-31 04:14:01 +0000203
204/// isAssociative - Return true if the instruction is associative:
205///
206/// Associative operators satisfy: x op (y op z) === (x op y) op z)
207///
208/// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
209/// applied to floating point types.
210///
211bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
Chris Lattner91386802006-10-26 18:27:26 +0000212 if (Opcode == And || Opcode == Or || Opcode == Xor)
213 return true;
214
215 // Add/Mul reassociate unless they are FP or FP vectors.
216 if (Opcode == Add || Opcode == Mul)
217 return !Ty->isFPOrFPVector();
Chris Lattnercab6c332002-10-31 04:14:01 +0000218 return 0;
219}
220
221/// isCommutative - Return true if the instruction is commutative:
222///
Misha Brukmanfa100532003-10-10 17:54:14 +0000223/// Commutative operators satisfy: (x op y) === (y op x)
Chris Lattnercab6c332002-10-31 04:14:01 +0000224///
225/// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
226/// applied to any type.
227///
228bool Instruction::isCommutative(unsigned op) {
229 switch (op) {
230 case Add:
231 case Mul:
Misha Brukmanb1c93172005-04-21 23:48:37 +0000232 case And:
Chris Lattnercab6c332002-10-31 04:14:01 +0000233 case Or:
234 case Xor:
Chris Lattnercab6c332002-10-31 04:14:01 +0000235 return true;
236 default:
237 return false;
238 }
239}
Tanya Lattnera93c7ae2003-07-31 04:05:50 +0000240
Tanya Lattnera93c7ae2003-07-31 04:05:50 +0000241/// isTrappingInstruction - Return true if the instruction may trap.
242///
Tanya Lattnerf8c563f2003-07-31 05:06:09 +0000243bool Instruction::isTrapping(unsigned op) {
Tanya Lattnera93c7ae2003-07-31 04:05:50 +0000244 switch(op) {
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000245 case UDiv:
246 case SDiv:
247 case FDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +0000248 case URem:
249 case SRem:
250 case FRem:
Tanya Lattnera93c7ae2003-07-31 04:05:50 +0000251 case Load:
252 case Store:
253 case Call:
254 case Invoke:
255 return true;
256 default:
257 return false;
258 }
259}