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/Instructions.h"
15 #include "llvm/Function.h"
16 #include "llvm/SymbolTable.h"
17 #include "llvm/Type.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 void Instruction::setOpcode(unsigned opc) {
47 setValueType(Value::InstructionVal + opc);
50 void Instruction::setParent(BasicBlock *P) {
52 if (!P) LeakDetector::addGarbageObject(this);
54 if (P) LeakDetector::removeGarbageObject(this);
60 // Specialize setName to take care of symbol table majik
61 void Instruction::setName(const std::string &name, SymbolTable *ST) {
62 BasicBlock *P = 0; Function *PP = 0;
63 assert((ST == 0 || !getParent() || !getParent()->getParent() ||
64 ST == &getParent()->getParent()->getSymbolTable()) &&
65 "Invalid symtab argument!");
66 if ((P = getParent()) && (PP = P->getParent()) && hasName())
67 PP->getSymbolTable().remove(this);
69 if (PP && hasName()) PP->getSymbolTable().insert(this);
72 void Instruction::removeFromParent() {
73 getParent()->getInstList().remove(this);
76 void Instruction::eraseFromParent() {
77 getParent()->getInstList().erase(this);
80 const char *Instruction::getOpcodeName(unsigned OpCode) {
83 case Ret: return "ret";
85 case Switch: return "switch";
86 case Invoke: return "invoke";
87 case Unwind: return "unwind";
88 case Unreachable: return "unreachable";
90 // Standard binary operators...
91 case Add: return "add";
92 case Sub: return "sub";
93 case Mul: return "mul";
94 case Div: return "div";
95 case Rem: return "rem";
97 // Logical operators...
98 case And: return "and";
99 case Or : return "or";
100 case Xor: return "xor";
102 // SetCC operators...
103 case SetLE: return "setle";
104 case SetGE: return "setge";
105 case SetLT: return "setlt";
106 case SetGT: return "setgt";
107 case SetEQ: return "seteq";
108 case SetNE: return "setne";
110 // Memory instructions...
111 case Malloc: return "malloc";
112 case Free: return "free";
113 case Alloca: return "alloca";
114 case Load: return "load";
115 case Store: return "store";
116 case GetElementPtr: return "getelementptr";
118 // Other instructions...
119 case PHI: return "phi";
120 case Cast: return "cast";
121 case Select: return "select";
122 case Call: return "call";
123 case Shl: return "shl";
124 case Shr: return "shr";
125 case VANext: return "vanext";
126 case VAArg: return "vaarg";
128 default: return "<Invalid operator> ";
134 /// isIdenticalTo - Return true if the specified instruction is exactly
135 /// identical to the current one. This means that all operands match and any
136 /// extra information (e.g. load is volatile) agree.
137 bool Instruction::isIdenticalTo(Instruction *I) const {
138 if (getOpcode() != I->getOpcode() ||
139 getNumOperands() != I->getNumOperands() ||
140 getType() != I->getType())
143 // We have two instructions of identical opcode and #operands. Check to see
144 // if all operands are the same.
145 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
146 if (getOperand(i) != I->getOperand(i))
149 // Check special state that is a part of some instructions.
150 if (const LoadInst *LI = dyn_cast<LoadInst>(this))
151 return LI->isVolatile() == cast<LoadInst>(I)->isVolatile();
152 if (const StoreInst *SI = dyn_cast<StoreInst>(this))
153 return SI->isVolatile() == cast<StoreInst>(I)->isVolatile();
154 if (const VANextInst *VAN = dyn_cast<VANextInst>(this))
155 return VAN->getArgType() == cast<VANextInst>(I)->getArgType();
160 /// isAssociative - Return true if the instruction is associative:
162 /// Associative operators satisfy: x op (y op z) === (x op y) op z)
164 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
165 /// applied to floating point types.
167 bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
168 if (Opcode == Add || Opcode == Mul ||
169 Opcode == And || Opcode == Or || Opcode == Xor) {
170 // Floating point operations do not associate!
171 return !Ty->isFloatingPoint();
176 /// isCommutative - Return true if the instruction is commutative:
178 /// Commutative operators satisfy: (x op y) === (y op x)
180 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
181 /// applied to any type.
183 bool Instruction::isCommutative(unsigned op) {
198 /// isRelational - Return true if the instruction is a Set* instruction:
200 bool Instruction::isRelational(unsigned op) {
215 /// isTrappingInstruction - Return true if the instruction may trap.
217 bool Instruction::isTrapping(unsigned op) {