fe4ba507c176388a1cbeb030e90f8314150ab529
[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/Instructions.h"
15 #include "llvm/Function.h"
16 #include "llvm/SymbolTable.h"
17 #include "llvm/Type.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 void Instruction::setOpcode(unsigned opc) {
47   setValueType(Value::InstructionVal + opc);
48 }
49
50 void Instruction::setParent(BasicBlock *P) {
51   if (getParent()) {
52     if (!P) LeakDetector::addGarbageObject(this);
53   } else {
54     if (P) LeakDetector::removeGarbageObject(this);
55   }
56
57   Parent = P;
58 }
59
60 void Instruction::removeFromParent() {
61   getParent()->getInstList().remove(this);
62 }
63
64 void Instruction::eraseFromParent() {
65   getParent()->getInstList().erase(this);
66 }
67
68 /// moveBefore - Unlink this instruction from its current basic block and
69 /// insert it into the basic block that MovePos lives in, right before
70 /// MovePos.
71 void Instruction::moveBefore(Instruction *MovePos) {
72   MovePos->getParent()->getInstList().splice(MovePos,getParent()->getInstList(),
73                                              this);
74 }
75
76
77 const char *Instruction::getOpcodeName(unsigned OpCode) {
78   switch (OpCode) {
79   // Terminators
80   case Ret:    return "ret";
81   case Br:     return "br";
82   case Switch: return "switch";
83   case Invoke: return "invoke";
84   case Unwind: return "unwind";
85   case Unreachable: return "unreachable";
86
87   // Standard binary operators...
88   case Add: return "add";
89   case Sub: return "sub";
90   case Mul: return "mul";
91   case Div: return "div";
92   case Rem: return "rem";
93
94   // Logical operators...
95   case And: return "and";
96   case Or : return "or";
97   case Xor: return "xor";
98
99   // SetCC operators...
100   case SetLE:  return "setle";
101   case SetGE:  return "setge";
102   case SetLT:  return "setlt";
103   case SetGT:  return "setgt";
104   case SetEQ:  return "seteq";
105   case SetNE:  return "setne";
106
107   // Memory instructions...
108   case Malloc:        return "malloc";
109   case Free:          return "free";
110   case Alloca:        return "alloca";
111   case Load:          return "load";
112   case Store:         return "store";
113   case GetElementPtr: return "getelementptr";
114
115   // Other instructions...
116   case PHI:     return "phi";
117   case Cast:    return "cast";
118   case Select:  return "select";
119   case Call:    return "call";
120   case Shl:     return "shl";
121   case Shr:     return "shr";
122   case VAArg:   return "va_arg";
123   case ExtractElement: return "extractelement";
124   case InsertElement: return "insertelement";
125   case ShuffleVector: return "shufflevector";
126
127   default: return "<Invalid operator> ";
128   }
129
130   return 0;
131 }
132
133 /// isIdenticalTo - Return true if the specified instruction is exactly
134 /// identical to the current one.  This means that all operands match and any
135 /// extra information (e.g. load is volatile) agree.
136 bool Instruction::isIdenticalTo(Instruction *I) const {
137   if (getOpcode() != I->getOpcode() ||
138       getNumOperands() != I->getNumOperands() ||
139       getType() != I->getType())
140     return false;
141
142   // We have two instructions of identical opcode and #operands.  Check to see
143   // if all operands are the same.
144   for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
145     if (getOperand(i) != I->getOperand(i))
146       return false;
147
148   // Check special state that is a part of some instructions.
149   if (const LoadInst *LI = dyn_cast<LoadInst>(this))
150     return LI->isVolatile() == cast<LoadInst>(I)->isVolatile();
151   if (const StoreInst *SI = dyn_cast<StoreInst>(this))
152     return SI->isVolatile() == cast<StoreInst>(I)->isVolatile();
153   if (const CallInst *CI = dyn_cast<CallInst>(this))
154     return CI->isTailCall() == cast<CallInst>(I)->isTailCall();
155   return true;
156 }
157
158
159 /// isAssociative - Return true if the instruction is associative:
160 ///
161 ///   Associative operators satisfy:  x op (y op z) === (x op y) op z)
162 ///
163 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
164 /// applied to floating point types.
165 ///
166 bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
167   if (Opcode == Add || Opcode == Mul ||
168       Opcode == And || Opcode == Or || Opcode == Xor) {
169     // Floating point operations do not associate!
170     return !Ty->isFloatingPoint();
171   }
172   return 0;
173 }
174
175 /// isCommutative - Return true if the instruction is commutative:
176 ///
177 ///   Commutative operators satisfy: (x op y) === (y op x)
178 ///
179 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
180 /// applied to any type.
181 ///
182 bool Instruction::isCommutative(unsigned op) {
183   switch (op) {
184   case Add:
185   case Mul:
186   case And:
187   case Or:
188   case Xor:
189   case SetEQ:
190   case SetNE:
191     return true;
192   default:
193     return false;
194   }
195 }
196
197 /// isRelational - Return true if the instruction is a Set* instruction:
198 ///
199 bool Instruction::isRelational(unsigned op) {
200   switch (op) {
201   case SetEQ:
202   case SetNE:
203   case SetLT:
204   case SetGT:
205   case SetLE:
206   case SetGE:
207     return true;
208   }
209   return false;
210 }
211
212
213
214 /// isTrappingInstruction - Return true if the instruction may trap.
215 ///
216 bool Instruction::isTrapping(unsigned op) {
217   switch(op) {
218   case Div:
219   case Rem:
220   case Load:
221   case Store:
222   case Call:
223   case Invoke:
224     return true;
225   default:
226     return false;
227   }
228 }