1 //===-- Instruction.cpp - Implement the Instruction class -----------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Instruction class for the VMCore library.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Type.h"
15 #include "llvm/Instructions.h"
16 #include "llvm/IntrinsicInst.h"
17 #include "llvm/Function.h"
18 #include "llvm/Support/LeakDetector.h"
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), 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...
29 assert(InsertBefore->getParent() &&
30 "Instruction to insert before is not in a basic block!");
31 InsertBefore->getParent()->getInstList().insert(InsertBefore, this);
36 Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps,
37 const std::string &Name, BasicBlock *InsertAtEnd)
38 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) {
39 // Make sure that we get added to a basicblock
40 LeakDetector::addGarbageObject(this);
42 // append this instruction into the basic block
43 assert(InsertAtEnd && "Basic block to append to may not be NULL!");
44 InsertAtEnd->getInstList().push_back(this);
48 Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps,
49 const char *Name, Instruction *InsertBefore)
50 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) {
51 // Make sure that we get added to a basicblock
52 LeakDetector::addGarbageObject(this);
54 // If requested, insert this instruction into a basic block...
56 assert(InsertBefore->getParent() &&
57 "Instruction to insert before is not in a basic block!");
58 InsertBefore->getParent()->getInstList().insert(InsertBefore, this);
60 if (Name && *Name) setName(Name);
63 Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps,
64 const char *Name, BasicBlock *InsertAtEnd)
65 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) {
66 // Make sure that we get added to a basicblock
67 LeakDetector::addGarbageObject(this);
69 // append this instruction into the basic block
70 assert(InsertAtEnd && "Basic block to append to may not be NULL!");
71 InsertAtEnd->getInstList().push_back(this);
72 if (Name && *Name) setName(Name);
76 // Out of line virtual method, so the vtable, etc has a home.
77 Instruction::~Instruction() {
78 assert(Parent == 0 && "Instruction still linked in the program!");
82 void Instruction::setParent(BasicBlock *P) {
84 if (!P) LeakDetector::addGarbageObject(this);
86 if (P) LeakDetector::removeGarbageObject(this);
92 void Instruction::removeFromParent() {
93 getParent()->getInstList().remove(this);
96 void Instruction::eraseFromParent() {
97 getParent()->getInstList().erase(this);
100 /// moveBefore - Unlink this instruction from its current basic block and
101 /// insert it into the basic block that MovePos lives in, right before
103 void Instruction::moveBefore(Instruction *MovePos) {
104 MovePos->getParent()->getInstList().splice(MovePos,getParent()->getInstList(),
109 const char *Instruction::getOpcodeName(unsigned OpCode) {
112 case Ret: return "ret";
113 case Br: return "br";
114 case Switch: return "switch";
115 case Invoke: return "invoke";
116 case Unwind: return "unwind";
117 case Unreachable: return "unreachable";
119 // Standard binary operators...
120 case Add: return "add";
121 case Sub: return "sub";
122 case Mul: return "mul";
123 case UDiv: return "udiv";
124 case SDiv: return "sdiv";
125 case FDiv: return "fdiv";
126 case URem: return "urem";
127 case SRem: return "srem";
128 case FRem: return "frem";
130 // Logical operators...
131 case And: return "and";
132 case Or : return "or";
133 case Xor: return "xor";
135 // Memory instructions...
136 case Malloc: return "malloc";
137 case Free: return "free";
138 case Alloca: return "alloca";
139 case Load: return "load";
140 case Store: return "store";
141 case GetElementPtr: return "getelementptr";
143 // Convert instructions...
144 case Trunc: return "trunc";
145 case ZExt: return "zext";
146 case SExt: return "sext";
147 case FPTrunc: return "fptrunc";
148 case FPExt: return "fpext";
149 case FPToUI: return "fptoui";
150 case FPToSI: return "fptosi";
151 case UIToFP: return "uitofp";
152 case SIToFP: return "sitofp";
153 case IntToPtr: return "inttoptr";
154 case PtrToInt: return "ptrtoint";
155 case BitCast: return "bitcast";
157 // Other instructions...
158 case ICmp: return "icmp";
159 case FCmp: return "fcmp";
160 case PHI: return "phi";
161 case Select: return "select";
162 case Call: return "call";
163 case Shl: return "shl";
164 case LShr: return "lshr";
165 case AShr: return "ashr";
166 case VAArg: return "va_arg";
167 case ExtractElement: return "extractelement";
168 case InsertElement: return "insertelement";
169 case ShuffleVector: return "shufflevector";
171 default: return "<Invalid operator> ";
177 /// isIdenticalTo - Return true if the specified instruction is exactly
178 /// identical to the current one. This means that all operands match and any
179 /// extra information (e.g. load is volatile) agree.
180 bool Instruction::isIdenticalTo(Instruction *I) const {
181 if (getOpcode() != I->getOpcode() ||
182 getNumOperands() != I->getNumOperands() ||
183 getType() != I->getType())
186 // We have two instructions of identical opcode and #operands. Check to see
187 // if all operands are the same.
188 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
189 if (getOperand(i) != I->getOperand(i))
192 // Check special state that is a part of some instructions.
193 if (const LoadInst *LI = dyn_cast<LoadInst>(this))
194 return LI->isVolatile() == cast<LoadInst>(I)->isVolatile();
195 if (const StoreInst *SI = dyn_cast<StoreInst>(this))
196 return SI->isVolatile() == cast<StoreInst>(I)->isVolatile();
197 if (const CmpInst *CI = dyn_cast<CmpInst>(this))
198 return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
199 if (const CallInst *CI = dyn_cast<CallInst>(this))
200 return CI->isTailCall() == cast<CallInst>(I)->isTailCall();
205 bool Instruction::isSameOperationAs(Instruction *I) const {
206 if (getOpcode() != I->getOpcode() || getType() != I->getType() ||
207 getNumOperands() != I->getNumOperands())
210 // We have two instructions of identical opcode and #operands. Check to see
211 // if all operands are the same type
212 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
213 if (getOperand(i)->getType() != I->getOperand(i)->getType())
216 // Check special state that is a part of some instructions.
217 if (const LoadInst *LI = dyn_cast<LoadInst>(this))
218 return LI->isVolatile() == cast<LoadInst>(I)->isVolatile();
219 if (const StoreInst *SI = dyn_cast<StoreInst>(this))
220 return SI->isVolatile() == cast<StoreInst>(I)->isVolatile();
221 if (const CmpInst *CI = dyn_cast<CmpInst>(this))
222 return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
223 if (const CallInst *CI = dyn_cast<CallInst>(this))
224 return CI->isTailCall() == cast<CallInst>(I)->isTailCall();
229 /// mayWriteToMemory - Return true if this instruction may modify memory.
231 bool Instruction::mayWriteToMemory() const {
232 switch (getOpcode()) {
233 default: return false;
234 case Instruction::Free:
235 case Instruction::Store:
236 case Instruction::Invoke:
237 case Instruction::VAArg:
239 case Instruction::Call:
240 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(this)) {
241 // If the intrinsic doesn't write memory, it is safe.
244 case Instruction::Load:
245 return cast<LoadInst>(this)->isVolatile();
249 /// isAssociative - Return true if the instruction is associative:
251 /// Associative operators satisfy: x op (y op z) === (x op y) op z)
253 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
254 /// applied to floating point types.
256 bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
257 if (Opcode == And || Opcode == Or || Opcode == Xor)
260 // Add/Mul reassociate unless they are FP or FP vectors.
261 if (Opcode == Add || Opcode == Mul)
262 return !Ty->isFPOrFPVector();
266 /// isCommutative - Return true if the instruction is commutative:
268 /// Commutative operators satisfy: (x op y) === (y op x)
270 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
271 /// applied to any type.
273 bool Instruction::isCommutative(unsigned op) {
286 /// isTrappingInstruction - Return true if the instruction may trap.
288 bool Instruction::isTrapping(unsigned op) {