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/SymbolTable.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, 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...
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::setParent(BasicBlock *P) {
54 if (!P) LeakDetector::addGarbageObject(this);
56 if (P) LeakDetector::removeGarbageObject(this);
62 void Instruction::removeFromParent() {
63 getParent()->getInstList().remove(this);
66 void Instruction::eraseFromParent() {
67 getParent()->getInstList().erase(this);
70 /// moveBefore - Unlink this instruction from its current basic block and
71 /// insert it into the basic block that MovePos lives in, right before
73 void Instruction::moveBefore(Instruction *MovePos) {
74 MovePos->getParent()->getInstList().splice(MovePos,getParent()->getInstList(),
79 const char *Instruction::getOpcodeName(unsigned OpCode) {
82 case Ret: return "ret";
84 case Switch: return "switch";
85 case Invoke: return "invoke";
86 case Unwind: return "unwind";
87 case Unreachable: return "unreachable";
89 // Standard binary operators...
90 case Add: return "add";
91 case Sub: return "sub";
92 case Mul: return "mul";
93 case UDiv: return "udiv";
94 case SDiv: return "sdiv";
95 case FDiv: return "fdiv";
96 case URem: return "urem";
97 case SRem: return "srem";
98 case FRem: return "frem";
100 // Logical operators...
101 case And: return "and";
102 case Or : return "or";
103 case Xor: return "xor";
105 // Memory instructions...
106 case Malloc: return "malloc";
107 case Free: return "free";
108 case Alloca: return "alloca";
109 case Load: return "load";
110 case Store: return "store";
111 case GetElementPtr: return "getelementptr";
113 // Convert instructions...
114 case Trunc: return "trunc";
115 case ZExt: return "zext";
116 case SExt: return "sext";
117 case FPTrunc: return "fptrunc";
118 case FPExt: return "fpext";
119 case FPToUI: return "fptoui";
120 case FPToSI: return "fptosi";
121 case UIToFP: return "uitofp";
122 case SIToFP: return "sitofp";
123 case IntToPtr: return "inttoptr";
124 case PtrToInt: return "ptrtoint";
125 case BitCast: return "bitcast";
127 // Other instructions...
128 case ICmp: return "icmp";
129 case FCmp: return "fcmp";
130 case PHI: return "phi";
131 case Select: return "select";
132 case Call: return "call";
133 case Shl: return "shl";
134 case LShr: return "lshr";
135 case AShr: return "ashr";
136 case VAArg: return "va_arg";
137 case ExtractElement: return "extractelement";
138 case InsertElement: return "insertelement";
139 case ShuffleVector: return "shufflevector";
141 default: return "<Invalid operator> ";
147 /// isIdenticalTo - Return true if the specified instruction is exactly
148 /// identical to the current one. This means that all operands match and any
149 /// extra information (e.g. load is volatile) agree.
150 bool Instruction::isIdenticalTo(Instruction *I) const {
151 if (getOpcode() != I->getOpcode() ||
152 getNumOperands() != I->getNumOperands() ||
153 getType() != I->getType())
156 // We have two instructions of identical opcode and #operands. Check to see
157 // if all operands are the same.
158 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
159 if (getOperand(i) != I->getOperand(i))
162 // Check special state that is a part of some instructions.
163 if (const LoadInst *LI = dyn_cast<LoadInst>(this))
164 return LI->isVolatile() == cast<LoadInst>(I)->isVolatile();
165 if (const StoreInst *SI = dyn_cast<StoreInst>(this))
166 return SI->isVolatile() == cast<StoreInst>(I)->isVolatile();
167 if (const CmpInst *CI = dyn_cast<CmpInst>(this))
168 return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
169 if (const CallInst *CI = dyn_cast<CallInst>(this))
170 return CI->isTailCall() == cast<CallInst>(I)->isTailCall();
175 bool Instruction::isSameOperationAs(Instruction *I) const {
176 if (getOpcode() != I->getOpcode() || getType() != I->getType() ||
177 getNumOperands() != I->getNumOperands())
180 // We have two instructions of identical opcode and #operands. Check to see
181 // if all operands are the same type
182 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
183 if (getOperand(i)->getType() != I->getOperand(i)->getType())
186 // Check special state that is a part of some instructions.
187 if (const LoadInst *LI = dyn_cast<LoadInst>(this))
188 return LI->isVolatile() == cast<LoadInst>(I)->isVolatile();
189 if (const StoreInst *SI = dyn_cast<StoreInst>(this))
190 return SI->isVolatile() == cast<StoreInst>(I)->isVolatile();
191 if (const CmpInst *CI = dyn_cast<CmpInst>(this))
192 return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
193 if (const CallInst *CI = dyn_cast<CallInst>(this))
194 return CI->isTailCall() == cast<CallInst>(I)->isTailCall();
200 /// isAssociative - Return true if the instruction is associative:
202 /// Associative operators satisfy: x op (y op z) === (x op y) op z)
204 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
205 /// applied to floating point types.
207 bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
208 if (Opcode == And || Opcode == Or || Opcode == Xor)
211 // Add/Mul reassociate unless they are FP or FP vectors.
212 if (Opcode == Add || Opcode == Mul)
213 return !Ty->isFPOrFPVector();
217 /// isCommutative - Return true if the instruction is commutative:
219 /// Commutative operators satisfy: (x op y) === (y op x)
221 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
222 /// applied to any type.
224 bool Instruction::isCommutative(unsigned op) {
237 /// isTrappingInstruction - Return true if the instruction may trap.
239 bool Instruction::isTrapping(unsigned op) {