remove warning
[oota-llvm.git] / lib / VMCore / Instruction.cpp
1 //===-- Instruction.cpp - Implement the Instruction class -----------------===//
2 //
3 //                     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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the Instruction class for the VMCore library.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Type.h"
15 #include "llvm/Instructions.h"
16 #include "llvm/IntrinsicInst.h"
17 #include "llvm/Function.h"
18 #include "llvm/Support/LeakDetector.h"
19 using namespace llvm;
20
21 Instruction::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), Parent(0) {
24   // Make sure that we get added to a basicblock
25   LeakDetector::addGarbageObject(this);
26
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   }
33   setName(Name);
34 }
35
36 Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps,
37                          const std::string &Name, BasicBlock *InsertAtEnd)
38   : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) {
39   // Make sure that we get added to a basicblock
40   LeakDetector::addGarbageObject(this);
41
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);
45   setName(Name);
46 }
47
48 Instruction::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
63 Instruction::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
76 // Out of line virtual method, so the vtable, etc has a home.
77 Instruction::~Instruction() {
78   assert(Parent == 0 && "Instruction still linked in the program!");
79 }
80
81
82 void Instruction::setParent(BasicBlock *P) {
83   if (getParent()) {
84     if (!P) LeakDetector::addGarbageObject(this);
85   } else {
86     if (P) LeakDetector::removeGarbageObject(this);
87   }
88
89   Parent = P;
90 }
91
92 void Instruction::removeFromParent() {
93   getParent()->getInstList().remove(this);
94 }
95
96 void Instruction::eraseFromParent() {
97   getParent()->getInstList().erase(this);
98 }
99
100 /// 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.
103 void Instruction::moveBefore(Instruction *MovePos) {
104   MovePos->getParent()->getInstList().splice(MovePos,getParent()->getInstList(),
105                                              this);
106 }
107
108
109 const char *Instruction::getOpcodeName(unsigned OpCode) {
110   switch (OpCode) {
111   // Terminators
112   case Ret:    return "ret";
113   case Br:     return "br";
114   case Switch: return "switch";
115   case Invoke: return "invoke";
116   case Unwind: return "unwind";
117   case Unreachable: return "unreachable";
118
119   // Standard binary operators...
120   case Add: return "add";
121   case Sub: return "sub";
122   case Mul: return "mul";
123   case UDiv: return "udiv";
124   case SDiv: return "sdiv";
125   case FDiv: return "fdiv";
126   case URem: return "urem";
127   case SRem: return "srem";
128   case FRem: return "frem";
129
130   // Logical operators...
131   case And: return "and";
132   case Or : return "or";
133   case Xor: return "xor";
134
135   // 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";
142
143   // 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
157   // Other instructions...
158   case ICmp:           return "icmp";
159   case FCmp:           return "fcmp";
160   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";
167   case ExtractElement: return "extractelement";
168   case InsertElement:  return "insertelement";
169   case ShuffleVector:  return "shufflevector";
170
171   default: return "<Invalid operator> ";
172   }
173
174   return 0;
175 }
176
177 /// 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.
180 bool 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();
197   if (const CmpInst *CI = dyn_cast<CmpInst>(this))
198     return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
199   if (const CallInst *CI = dyn_cast<CallInst>(this))
200     return CI->isTailCall() == cast<CallInst>(I)->isTailCall();
201   return true;
202 }
203
204 // isSameOperationAs
205 bool 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
229 /// mayWriteToMemory - Return true if this instruction may modify memory.
230 ///
231 bool 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:
240     //if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(this)) {
241       // If the intrinsic doesn't write memory, it is safe.
242       // FIXME: this is obviously supposed to determine which  intrinsics 
243       // don't write to memory, but hasn't been implemented yet.
244     //}
245     return true;
246   case Instruction::Load:
247     return cast<LoadInst>(this)->isVolatile();
248   }
249 }
250
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 ///
258 bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
259   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();
265   return 0;
266 }
267
268 /// isCommutative - Return true if the instruction is commutative:
269 ///
270 ///   Commutative operators satisfy: (x op y) === (y op x)
271 ///
272 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
273 /// applied to any type.
274 ///
275 bool Instruction::isCommutative(unsigned op) {
276   switch (op) {
277   case Add:
278   case Mul:
279   case And:
280   case Or:
281   case Xor:
282     return true;
283   default:
284     return false;
285   }
286 }
287
288 /// isTrappingInstruction - Return true if the instruction may trap.
289 ///
290 bool Instruction::isTrapping(unsigned op) {
291   switch(op) {
292   case UDiv:
293   case SDiv:
294   case FDiv:
295   case URem:
296   case SRem:
297   case FRem:
298   case Load:
299   case Store:
300   case Call:
301   case Invoke:
302     return true;
303   default:
304     return false;
305   }
306 }