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/Function.h"
17 #include "llvm/Support/LeakDetector.h"
20 Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps,
21 const std::string &Name, Instruction *InsertBefore)
22 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) {
23 // Make sure that we get added to a basicblock
24 LeakDetector::addGarbageObject(this);
26 // If requested, insert this instruction into a basic block...
28 assert(InsertBefore->getParent() &&
29 "Instruction to insert before is not in a basic block!");
30 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), 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);
47 Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps,
48 const char *Name, Instruction *InsertBefore)
49 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) {
50 // Make sure that we get added to a basicblock
51 LeakDetector::addGarbageObject(this);
53 // If requested, insert this instruction into a basic block...
55 assert(InsertBefore->getParent() &&
56 "Instruction to insert before is not in a basic block!");
57 InsertBefore->getParent()->getInstList().insert(InsertBefore, this);
59 if (Name && *Name) setName(Name);
62 Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps,
63 const char *Name, BasicBlock *InsertAtEnd)
64 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) {
65 // Make sure that we get added to a basicblock
66 LeakDetector::addGarbageObject(this);
68 // append this instruction into the basic block
69 assert(InsertAtEnd && "Basic block to append to may not be NULL!");
70 InsertAtEnd->getInstList().push_back(this);
71 if (Name && *Name) setName(Name);
75 // Out of line virtual method, so the vtable, etc has a home.
76 Instruction::~Instruction() {
77 assert(Parent == 0 && "Instruction still linked in the program!");
81 void Instruction::setParent(BasicBlock *P) {
83 if (!P) LeakDetector::addGarbageObject(this);
85 if (P) LeakDetector::removeGarbageObject(this);
91 void Instruction::removeFromParent() {
92 getParent()->getInstList().remove(this);
95 void Instruction::eraseFromParent() {
96 getParent()->getInstList().erase(this);
99 /// moveBefore - Unlink this instruction from its current basic block and
100 /// insert it into the basic block that MovePos lives in, right before
102 void Instruction::moveBefore(Instruction *MovePos) {
103 MovePos->getParent()->getInstList().splice(MovePos,getParent()->getInstList(),
108 const char *Instruction::getOpcodeName(unsigned OpCode) {
111 case Ret: return "ret";
112 case Br: return "br";
113 case Switch: return "switch";
114 case Invoke: return "invoke";
115 case Unwind: return "unwind";
116 case Unreachable: return "unreachable";
118 // Standard binary operators...
119 case Add: return "add";
120 case Sub: return "sub";
121 case Mul: return "mul";
122 case UDiv: return "udiv";
123 case SDiv: return "sdiv";
124 case FDiv: return "fdiv";
125 case URem: return "urem";
126 case SRem: return "srem";
127 case FRem: return "frem";
129 // Logical operators...
130 case And: return "and";
131 case Or : return "or";
132 case Xor: return "xor";
134 // Memory instructions...
135 case Malloc: return "malloc";
136 case Free: return "free";
137 case Alloca: return "alloca";
138 case Load: return "load";
139 case Store: return "store";
140 case GetElementPtr: return "getelementptr";
142 // Convert instructions...
143 case Trunc: return "trunc";
144 case ZExt: return "zext";
145 case SExt: return "sext";
146 case FPTrunc: return "fptrunc";
147 case FPExt: return "fpext";
148 case FPToUI: return "fptoui";
149 case FPToSI: return "fptosi";
150 case UIToFP: return "uitofp";
151 case SIToFP: return "sitofp";
152 case IntToPtr: return "inttoptr";
153 case PtrToInt: return "ptrtoint";
154 case BitCast: return "bitcast";
156 // Other instructions...
157 case ICmp: return "icmp";
158 case FCmp: return "fcmp";
159 case PHI: return "phi";
160 case Select: return "select";
161 case Call: return "call";
162 case Shl: return "shl";
163 case LShr: return "lshr";
164 case AShr: return "ashr";
165 case VAArg: return "va_arg";
166 case ExtractElement: return "extractelement";
167 case InsertElement: return "insertelement";
168 case ShuffleVector: return "shufflevector";
170 default: return "<Invalid operator> ";
176 /// isIdenticalTo - Return true if the specified instruction is exactly
177 /// identical to the current one. This means that all operands match and any
178 /// extra information (e.g. load is volatile) agree.
179 bool Instruction::isIdenticalTo(Instruction *I) const {
180 if (getOpcode() != I->getOpcode() ||
181 getNumOperands() != I->getNumOperands() ||
182 getType() != I->getType())
185 // We have two instructions of identical opcode and #operands. Check to see
186 // if all operands are the same.
187 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
188 if (getOperand(i) != I->getOperand(i))
191 // Check special state that is a part of some instructions.
192 if (const LoadInst *LI = dyn_cast<LoadInst>(this))
193 return LI->isVolatile() == cast<LoadInst>(I)->isVolatile();
194 if (const StoreInst *SI = dyn_cast<StoreInst>(this))
195 return SI->isVolatile() == cast<StoreInst>(I)->isVolatile();
196 if (const CmpInst *CI = dyn_cast<CmpInst>(this))
197 return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
198 if (const CallInst *CI = dyn_cast<CallInst>(this))
199 return CI->isTailCall() == cast<CallInst>(I)->isTailCall();
204 bool Instruction::isSameOperationAs(Instruction *I) const {
205 if (getOpcode() != I->getOpcode() || getType() != I->getType() ||
206 getNumOperands() != I->getNumOperands())
209 // We have two instructions of identical opcode and #operands. Check to see
210 // if all operands are the same type
211 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
212 if (getOperand(i)->getType() != I->getOperand(i)->getType())
215 // Check special state that is a part of some instructions.
216 if (const LoadInst *LI = dyn_cast<LoadInst>(this))
217 return LI->isVolatile() == cast<LoadInst>(I)->isVolatile();
218 if (const StoreInst *SI = dyn_cast<StoreInst>(this))
219 return SI->isVolatile() == cast<StoreInst>(I)->isVolatile();
220 if (const CmpInst *CI = dyn_cast<CmpInst>(this))
221 return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
222 if (const CallInst *CI = dyn_cast<CallInst>(this))
223 return CI->isTailCall() == cast<CallInst>(I)->isTailCall();
228 /// mayWriteToMemory - Return true if this instruction may modify memory.
230 bool Instruction::mayWriteToMemory() const {
231 switch (getOpcode()) {
232 default: return false;
233 case Instruction::Free:
234 case Instruction::Store:
235 case Instruction::Invoke:
236 case Instruction::VAArg:
238 case Instruction::Call:
239 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(this)) {
240 // If the intrinsic doesn't write memory, it is safe.
243 case Instruction::Load:
244 return cast<LoadInst>(this)->isVolatile();
248 /// isAssociative - Return true if the instruction is associative:
250 /// Associative operators satisfy: x op (y op z) === (x op y) op z)
252 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
253 /// applied to floating point types.
255 bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
256 if (Opcode == And || Opcode == Or || Opcode == Xor)
259 // Add/Mul reassociate unless they are FP or FP vectors.
260 if (Opcode == Add || Opcode == Mul)
261 return !Ty->isFPOrFPVector();
265 /// isCommutative - Return true if the instruction is commutative:
267 /// Commutative operators satisfy: (x op y) === (y op x)
269 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
270 /// applied to any type.
272 bool Instruction::isCommutative(unsigned op) {
285 /// isTrappingInstruction - Return true if the instruction may trap.
287 bool Instruction::isTrapping(unsigned op) {