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