Removed IIIi specific changes. This should be fixed to add floating point deps for...
[llvm-complete.git] / lib / VMCore / Instruction.cpp
blobe1dead99f23fc7260f91608057688b2867601096
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.
12 //===----------------------------------------------------------------------===//
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;
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);
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);
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);
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);
46 void Instruction::setOpcode(unsigned opc) {
47 setValueType(Value::InstructionVal + opc);
50 void Instruction::setParent(BasicBlock *P) {
51 if (getParent()) {
52 if (!P) LeakDetector::addGarbageObject(this);
53 } else {
54 if (P) LeakDetector::removeGarbageObject(this);
57 Parent = P;
60 void Instruction::removeFromParent() {
61 getParent()->getInstList().remove(this);
64 void Instruction::eraseFromParent() {
65 getParent()->getInstList().erase(this);
68 const char *Instruction::getOpcodeName(unsigned OpCode) {
69 switch (OpCode) {
70 // Terminators
71 case Ret: return "ret";
72 case Br: return "br";
73 case Switch: return "switch";
74 case Invoke: return "invoke";
75 case Unwind: return "unwind";
76 case Unreachable: return "unreachable";
78 // Standard binary operators...
79 case Add: return "add";
80 case Sub: return "sub";
81 case Mul: return "mul";
82 case Div: return "div";
83 case Rem: return "rem";
85 // Logical operators...
86 case And: return "and";
87 case Or : return "or";
88 case Xor: return "xor";
90 // SetCC operators...
91 case SetLE: return "setle";
92 case SetGE: return "setge";
93 case SetLT: return "setlt";
94 case SetGT: return "setgt";
95 case SetEQ: return "seteq";
96 case SetNE: return "setne";
98 // Memory instructions...
99 case Malloc: return "malloc";
100 case Free: return "free";
101 case Alloca: return "alloca";
102 case Load: return "load";
103 case Store: return "store";
104 case GetElementPtr: return "getelementptr";
106 // Other instructions...
107 case PHI: return "phi";
108 case Cast: return "cast";
109 case Select: return "select";
110 case Call: return "call";
111 case Shl: return "shl";
112 case Shr: return "shr";
113 case VANext: return "vanext";
114 case VAArg: return "vaarg";
116 default: return "<Invalid operator> ";
119 return 0;
122 /// isIdenticalTo - Return true if the specified instruction is exactly
123 /// identical to the current one. This means that all operands match and any
124 /// extra information (e.g. load is volatile) agree.
125 bool Instruction::isIdenticalTo(Instruction *I) const {
126 if (getOpcode() != I->getOpcode() ||
127 getNumOperands() != I->getNumOperands() ||
128 getType() != I->getType())
129 return false;
131 // We have two instructions of identical opcode and #operands. Check to see
132 // if all operands are the same.
133 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
134 if (getOperand(i) != I->getOperand(i))
135 return false;
137 // Check special state that is a part of some instructions.
138 if (const LoadInst *LI = dyn_cast<LoadInst>(this))
139 return LI->isVolatile() == cast<LoadInst>(I)->isVolatile();
140 if (const StoreInst *SI = dyn_cast<StoreInst>(this))
141 return SI->isVolatile() == cast<StoreInst>(I)->isVolatile();
142 if (const VANextInst *VAN = dyn_cast<VANextInst>(this))
143 return VAN->getArgType() == cast<VANextInst>(I)->getArgType();
144 if (const CallInst *CI = dyn_cast<CallInst>(this))
145 return CI->isTailCall() == cast<CallInst>(I)->isTailCall();
146 return true;
150 /// isAssociative - Return true if the instruction is associative:
152 /// Associative operators satisfy: x op (y op z) === (x op y) op z)
154 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
155 /// applied to floating point types.
157 bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
158 if (Opcode == Add || Opcode == Mul ||
159 Opcode == And || Opcode == Or || Opcode == Xor) {
160 // Floating point operations do not associate!
161 return !Ty->isFloatingPoint();
163 return 0;
166 /// isCommutative - Return true if the instruction is commutative:
168 /// Commutative operators satisfy: (x op y) === (y op x)
170 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
171 /// applied to any type.
173 bool Instruction::isCommutative(unsigned op) {
174 switch (op) {
175 case Add:
176 case Mul:
177 case And:
178 case Or:
179 case Xor:
180 case SetEQ:
181 case SetNE:
182 return true;
183 default:
184 return false;
188 /// isRelational - Return true if the instruction is a Set* instruction:
190 bool Instruction::isRelational(unsigned op) {
191 switch (op) {
192 case SetEQ:
193 case SetNE:
194 case SetLT:
195 case SetGT:
196 case SetLE:
197 case SetGE:
198 return true;
200 return false;
205 /// isTrappingInstruction - Return true if the instruction may trap.
207 bool Instruction::isTrapping(unsigned op) {
208 switch(op) {
209 case Div:
210 case Rem:
211 case Load:
212 case Store:
213 case Call:
214 case Invoke:
215 return true;
216 default:
217 return false;