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, Name), 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);
34 Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps,
35 const std::string &Name, BasicBlock *InsertAtEnd)
36 : User(ty, Value::InstructionVal + it, Ops, NumOps, Name), Parent(0) {
37 // Make sure that we get added to a basicblock
38 LeakDetector::addGarbageObject(this);
40 // append this instruction into the basic block
41 assert(InsertAtEnd && "Basic block to append to may not be NULL!");
42 InsertAtEnd->getInstList().push_back(this);
45 // Out of line virtual method, so the vtable, etc has a home.
46 Instruction::~Instruction() {
47 assert(Parent == 0 && "Instruction still linked in the program!");
51 void Instruction::setParent(BasicBlock *P) {
53 if (!P) LeakDetector::addGarbageObject(this);
55 if (P) LeakDetector::removeGarbageObject(this);
61 void Instruction::removeFromParent() {
62 getParent()->getInstList().remove(this);
65 void Instruction::eraseFromParent() {
66 getParent()->getInstList().erase(this);
69 /// moveBefore - Unlink this instruction from its current basic block and
70 /// insert it into the basic block that MovePos lives in, right before
72 void Instruction::moveBefore(Instruction *MovePos) {
73 MovePos->getParent()->getInstList().splice(MovePos,getParent()->getInstList(),
78 const char *Instruction::getOpcodeName(unsigned OpCode) {
81 case Ret: return "ret";
83 case Switch: return "switch";
84 case Invoke: return "invoke";
85 case Unwind: return "unwind";
86 case Unreachable: return "unreachable";
88 // Standard binary operators...
89 case Add: return "add";
90 case Sub: return "sub";
91 case Mul: return "mul";
92 case UDiv: return "udiv";
93 case SDiv: return "sdiv";
94 case FDiv: return "fdiv";
95 case URem: return "urem";
96 case SRem: return "srem";
97 case FRem: return "frem";
99 // Logical operators...
100 case And: return "and";
101 case Or : return "or";
102 case Xor: return "xor";
104 // Memory instructions...
105 case Malloc: return "malloc";
106 case Free: return "free";
107 case Alloca: return "alloca";
108 case Load: return "load";
109 case Store: return "store";
110 case GetElementPtr: return "getelementptr";
112 // Convert instructions...
113 case Trunc: return "trunc";
114 case ZExt: return "zext";
115 case SExt: return "sext";
116 case FPTrunc: return "fptrunc";
117 case FPExt: return "fpext";
118 case FPToUI: return "fptoui";
119 case FPToSI: return "fptosi";
120 case UIToFP: return "uitofp";
121 case SIToFP: return "sitofp";
122 case IntToPtr: return "inttoptr";
123 case PtrToInt: return "ptrtoint";
124 case BitCast: return "bitcast";
126 // Other instructions...
127 case ICmp: return "icmp";
128 case FCmp: return "fcmp";
129 case PHI: return "phi";
130 case Select: return "select";
131 case Call: return "call";
132 case Shl: return "shl";
133 case LShr: return "lshr";
134 case AShr: return "ashr";
135 case VAArg: return "va_arg";
136 case ExtractElement: return "extractelement";
137 case InsertElement: return "insertelement";
138 case ShuffleVector: return "shufflevector";
140 default: return "<Invalid operator> ";
146 /// isIdenticalTo - Return true if the specified instruction is exactly
147 /// identical to the current one. This means that all operands match and any
148 /// extra information (e.g. load is volatile) agree.
149 bool Instruction::isIdenticalTo(Instruction *I) const {
150 if (getOpcode() != I->getOpcode() ||
151 getNumOperands() != I->getNumOperands() ||
152 getType() != I->getType())
155 // We have two instructions of identical opcode and #operands. Check to see
156 // if all operands are the same.
157 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
158 if (getOperand(i) != I->getOperand(i))
161 // Check special state that is a part of some instructions.
162 if (const LoadInst *LI = dyn_cast<LoadInst>(this))
163 return LI->isVolatile() == cast<LoadInst>(I)->isVolatile();
164 if (const StoreInst *SI = dyn_cast<StoreInst>(this))
165 return SI->isVolatile() == cast<StoreInst>(I)->isVolatile();
166 if (const CmpInst *CI = dyn_cast<CmpInst>(this))
167 return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
168 if (const CallInst *CI = dyn_cast<CallInst>(this))
169 return CI->isTailCall() == cast<CallInst>(I)->isTailCall();
174 bool Instruction::isSameOperationAs(Instruction *I) const {
175 if (getOpcode() != I->getOpcode() || getType() != I->getType() ||
176 getNumOperands() != I->getNumOperands())
179 // We have two instructions of identical opcode and #operands. Check to see
180 // if all operands are the same type
181 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
182 if (getOperand(i)->getType() != I->getOperand(i)->getType())
185 // Check special state that is a part of some instructions.
186 if (const LoadInst *LI = dyn_cast<LoadInst>(this))
187 return LI->isVolatile() == cast<LoadInst>(I)->isVolatile();
188 if (const StoreInst *SI = dyn_cast<StoreInst>(this))
189 return SI->isVolatile() == cast<StoreInst>(I)->isVolatile();
190 if (const CmpInst *CI = dyn_cast<CmpInst>(this))
191 return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
192 if (const CallInst *CI = dyn_cast<CallInst>(this))
193 return CI->isTailCall() == cast<CallInst>(I)->isTailCall();
199 /// isAssociative - Return true if the instruction is associative:
201 /// Associative operators satisfy: x op (y op z) === (x op y) op z)
203 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
204 /// applied to floating point types.
206 bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
207 if (Opcode == And || Opcode == Or || Opcode == Xor)
210 // Add/Mul reassociate unless they are FP or FP vectors.
211 if (Opcode == Add || Opcode == Mul)
212 return !Ty->isFPOrFPVector();
216 /// isCommutative - Return true if the instruction is commutative:
218 /// Commutative operators satisfy: (x op y) === (y op x)
220 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
221 /// applied to any type.
223 bool Instruction::isCommutative(unsigned op) {
236 /// isTrappingInstruction - Return true if the instruction may trap.
238 bool Instruction::isTrapping(unsigned op) {