For PR411:
[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/Function.h"
17 #include "llvm/Support/LeakDetector.h"
18 using namespace llvm;
19
20 Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps,
21                          const std::string &Name, Instruction *InsertBefore)
22   : User(ty, Value::InstructionVal + it, Ops, NumOps, Name), Parent(0) {
23   // Make sure that we get added to a basicblock
24   LeakDetector::addGarbageObject(this);
25
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   }
32 }
33
34 Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps,
35                          const std::string &Name, BasicBlock *InsertAtEnd)
36   : User(ty, Value::InstructionVal + it, Ops, NumOps, Name), Parent(0) {
37   // Make sure that we get added to a basicblock
38   LeakDetector::addGarbageObject(this);
39
40   // append this instruction into the basic block
41   assert(InsertAtEnd && "Basic block to append to may not be NULL!");
42   InsertAtEnd->getInstList().push_back(this);
43 }
44
45 // Out of line virtual method, so the vtable, etc has a home.
46 Instruction::~Instruction() {
47   assert(Parent == 0 && "Instruction still linked in the program!");
48 }
49
50
51 void Instruction::setParent(BasicBlock *P) {
52   if (getParent()) {
53     if (!P) LeakDetector::addGarbageObject(this);
54   } else {
55     if (P) LeakDetector::removeGarbageObject(this);
56   }
57
58   Parent = P;
59 }
60
61 void Instruction::removeFromParent() {
62   getParent()->getInstList().remove(this);
63 }
64
65 void Instruction::eraseFromParent() {
66   getParent()->getInstList().erase(this);
67 }
68
69 /// moveBefore - Unlink this instruction from its current basic block and
70 /// insert it into the basic block that MovePos lives in, right before
71 /// MovePos.
72 void Instruction::moveBefore(Instruction *MovePos) {
73   MovePos->getParent()->getInstList().splice(MovePos,getParent()->getInstList(),
74                                              this);
75 }
76
77
78 const char *Instruction::getOpcodeName(unsigned OpCode) {
79   switch (OpCode) {
80   // Terminators
81   case Ret:    return "ret";
82   case Br:     return "br";
83   case Switch: return "switch";
84   case Invoke: return "invoke";
85   case Unwind: return "unwind";
86   case Unreachable: return "unreachable";
87
88   // Standard binary operators...
89   case Add: return "add";
90   case Sub: return "sub";
91   case Mul: return "mul";
92   case UDiv: return "udiv";
93   case SDiv: return "sdiv";
94   case FDiv: return "fdiv";
95   case URem: return "urem";
96   case SRem: return "srem";
97   case FRem: return "frem";
98
99   // Logical operators...
100   case And: return "and";
101   case Or : return "or";
102   case Xor: return "xor";
103
104   // Memory instructions...
105   case Malloc:        return "malloc";
106   case Free:          return "free";
107   case Alloca:        return "alloca";
108   case Load:          return "load";
109   case Store:         return "store";
110   case GetElementPtr: return "getelementptr";
111
112   // Convert instructions...
113   case Trunc:     return "trunc";
114   case ZExt:      return "zext";
115   case SExt:      return "sext";
116   case FPTrunc:   return "fptrunc";
117   case FPExt:     return "fpext";
118   case FPToUI:    return "fptoui";
119   case FPToSI:    return "fptosi";
120   case UIToFP:    return "uitofp";
121   case SIToFP:    return "sitofp";
122   case IntToPtr:  return "inttoptr";
123   case PtrToInt:  return "ptrtoint";
124   case BitCast:   return "bitcast";
125
126   // Other instructions...
127   case ICmp:           return "icmp";
128   case FCmp:           return "fcmp";
129   case PHI:            return "phi";
130   case Select:         return "select";
131   case Call:           return "call";
132   case Shl:            return "shl";
133   case LShr:           return "lshr";
134   case AShr:           return "ashr";
135   case VAArg:          return "va_arg";
136   case ExtractElement: return "extractelement";
137   case InsertElement:  return "insertelement";
138   case ShuffleVector:  return "shufflevector";
139
140   default: return "<Invalid operator> ";
141   }
142
143   return 0;
144 }
145
146 /// isIdenticalTo - Return true if the specified instruction is exactly
147 /// identical to the current one.  This means that all operands match and any
148 /// extra information (e.g. load is volatile) agree.
149 bool Instruction::isIdenticalTo(Instruction *I) const {
150   if (getOpcode() != I->getOpcode() ||
151       getNumOperands() != I->getNumOperands() ||
152       getType() != I->getType())
153     return false;
154
155   // We have two instructions of identical opcode and #operands.  Check to see
156   // if all operands are the same.
157   for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
158     if (getOperand(i) != I->getOperand(i))
159       return false;
160
161   // Check special state that is a part of some instructions.
162   if (const LoadInst *LI = dyn_cast<LoadInst>(this))
163     return LI->isVolatile() == cast<LoadInst>(I)->isVolatile();
164   if (const StoreInst *SI = dyn_cast<StoreInst>(this))
165     return SI->isVolatile() == cast<StoreInst>(I)->isVolatile();
166   if (const CmpInst *CI = dyn_cast<CmpInst>(this))
167     return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
168   if (const CallInst *CI = dyn_cast<CallInst>(this))
169     return CI->isTailCall() == cast<CallInst>(I)->isTailCall();
170   return true;
171 }
172
173 // isSameOperationAs
174 bool Instruction::isSameOperationAs(Instruction *I) const {
175   if (getOpcode() != I->getOpcode() || getType() != I->getType() ||
176       getNumOperands() != I->getNumOperands())
177     return false;
178
179   // We have two instructions of identical opcode and #operands.  Check to see
180   // if all operands are the same type
181   for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
182     if (getOperand(i)->getType() != I->getOperand(i)->getType())
183       return false;
184
185   // Check special state that is a part of some instructions.
186   if (const LoadInst *LI = dyn_cast<LoadInst>(this))
187     return LI->isVolatile() == cast<LoadInst>(I)->isVolatile();
188   if (const StoreInst *SI = dyn_cast<StoreInst>(this))
189     return SI->isVolatile() == cast<StoreInst>(I)->isVolatile();
190   if (const CmpInst *CI = dyn_cast<CmpInst>(this))
191     return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
192   if (const CallInst *CI = dyn_cast<CallInst>(this))
193     return CI->isTailCall() == cast<CallInst>(I)->isTailCall();
194
195   return true;
196 }
197
198
199 /// isAssociative - Return true if the instruction is associative:
200 ///
201 ///   Associative operators satisfy:  x op (y op z) === (x op y) op z)
202 ///
203 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
204 /// applied to floating point types.
205 ///
206 bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
207   if (Opcode == And || Opcode == Or || Opcode == Xor)
208     return true;
209
210   // Add/Mul reassociate unless they are FP or FP vectors.
211   if (Opcode == Add || Opcode == Mul)
212     return !Ty->isFPOrFPVector();
213   return 0;
214 }
215
216 /// isCommutative - Return true if the instruction is commutative:
217 ///
218 ///   Commutative operators satisfy: (x op y) === (y op x)
219 ///
220 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
221 /// applied to any type.
222 ///
223 bool Instruction::isCommutative(unsigned op) {
224   switch (op) {
225   case Add:
226   case Mul:
227   case And:
228   case Or:
229   case Xor:
230     return true;
231   default:
232     return false;
233   }
234 }
235
236 /// isTrappingInstruction - Return true if the instruction may trap.
237 ///
238 bool Instruction::isTrapping(unsigned op) {
239   switch(op) {
240   case UDiv:
241   case SDiv:
242   case FDiv:
243   case URem:
244   case SRem:
245   case FRem:
246   case Load:
247   case Store:
248   case Call:
249   case Invoke:
250     return true;
251   default:
252     return false;
253   }
254 }