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