Reverting back to original 1.8 version so I can manually merge in patch.
[llvm-complete.git] / lib / VMCore / Instruction.cpp
blobab4aaac745077cec6a0e9c8686a460061e109165
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 // 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!");
52 void Instruction::setOpcode(unsigned opc) {
53 setValueType(Value::InstructionVal + opc);
56 void Instruction::setParent(BasicBlock *P) {
57 if (getParent()) {
58 if (!P) LeakDetector::addGarbageObject(this);
59 } else {
60 if (P) LeakDetector::removeGarbageObject(this);
63 Parent = P;
66 void Instruction::removeFromParent() {
67 getParent()->getInstList().remove(this);
70 void Instruction::eraseFromParent() {
71 getParent()->getInstList().erase(this);
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);
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";
93 // Standard binary operators...
94 case Add: return "add";
95 case Sub: return "sub";
96 case Mul: return "mul";
97 case Div: return "div";
98 case Rem: return "rem";
100 // Logical operators...
101 case And: return "and";
102 case Or : return "or";
103 case Xor: return "xor";
105 // SetCC operators...
106 case SetLE: return "setle";
107 case SetGE: return "setge";
108 case SetLT: return "setlt";
109 case SetGT: return "setgt";
110 case SetEQ: return "seteq";
111 case SetNE: return "setne";
113 // Memory instructions...
114 case Malloc: return "malloc";
115 case Free: return "free";
116 case Alloca: return "alloca";
117 case Load: return "load";
118 case Store: return "store";
119 case GetElementPtr: return "getelementptr";
121 // Other instructions...
122 case PHI: return "phi";
123 case Cast: return "cast";
124 case Select: return "select";
125 case Call: return "call";
126 case Shl: return "shl";
127 case Shr: return "shr";
128 case VAArg: return "va_arg";
129 case ExtractElement: return "extractelement";
130 case InsertElement: return "insertelement";
131 case ShuffleVector: return "shufflevector";
133 default: return "<Invalid operator> ";
136 return 0;
139 /// isIdenticalTo - Return true if the specified instruction is exactly
140 /// identical to the current one. This means that all operands match and any
141 /// extra information (e.g. load is volatile) agree.
142 bool Instruction::isIdenticalTo(Instruction *I) const {
143 if (getOpcode() != I->getOpcode() ||
144 getNumOperands() != I->getNumOperands() ||
145 getType() != I->getType())
146 return false;
148 // We have two instructions of identical opcode and #operands. Check to see
149 // if all operands are the same.
150 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
151 if (getOperand(i) != I->getOperand(i))
152 return false;
154 // Check special state that is a part of some instructions.
155 if (const LoadInst *LI = dyn_cast<LoadInst>(this))
156 return LI->isVolatile() == cast<LoadInst>(I)->isVolatile();
157 if (const StoreInst *SI = dyn_cast<StoreInst>(this))
158 return SI->isVolatile() == cast<StoreInst>(I)->isVolatile();
159 if (const CallInst *CI = dyn_cast<CallInst>(this))
160 return CI->isTailCall() == cast<CallInst>(I)->isTailCall();
161 return true;
165 /// isAssociative - Return true if the instruction is associative:
167 /// Associative operators satisfy: x op (y op z) === (x op y) op z)
169 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
170 /// applied to floating point types.
172 bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
173 if (Opcode == Add || Opcode == Mul ||
174 Opcode == And || Opcode == Or || Opcode == Xor) {
175 // Floating point operations do not associate!
176 return !Ty->isFloatingPoint();
178 return 0;
181 /// isCommutative - Return true if the instruction is commutative:
183 /// Commutative operators satisfy: (x op y) === (y op x)
185 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
186 /// applied to any type.
188 bool Instruction::isCommutative(unsigned op) {
189 switch (op) {
190 case Add:
191 case Mul:
192 case And:
193 case Or:
194 case Xor:
195 case SetEQ:
196 case SetNE:
197 return true;
198 default:
199 return false;
203 /// isRelational - Return true if the instruction is a Set* instruction:
205 bool Instruction::isRelational(unsigned op) {
206 switch (op) {
207 case SetEQ:
208 case SetNE:
209 case SetLT:
210 case SetGT:
211 case SetLE:
212 case SetGE:
213 return true;
215 return false;
220 /// isTrappingInstruction - Return true if the instruction may trap.
222 bool Instruction::isTrapping(unsigned op) {
223 switch(op) {
224 case Div:
225 case Rem:
226 case Load:
227 case Store:
228 case Call:
229 case Invoke:
230 return true;
231 default:
232 return false;