1 //===-- Instruction.cpp - Implement the Instruction class --------*- C++ -*--=//
3 // This file implements the Instruction class for the VMCore library.
5 //===----------------------------------------------------------------------===//
7 #include "llvm/Function.h"
8 #include "llvm/SymbolTable.h"
10 #include "Support/LeakDetector.h"
12 Instruction::Instruction(const Type *ty, unsigned it, const std::string &Name,
13 Instruction *InsertBefore)
14 : User(ty, Value::InstructionVal, Name) {
18 // Make sure that we get added to a basicblock
19 LeakDetector::addGarbageObject(this);
21 // If requested, insert this instruction into a basic block...
23 assert(InsertBefore->getParent() &&
24 "Instruction to insert before is not in a basic block!");
25 InsertBefore->getParent()->getInstList().insert(InsertBefore, this);
29 void Instruction::setParent(BasicBlock *P) {
31 LeakDetector::addGarbageObject(this);
36 LeakDetector::removeGarbageObject(this);
39 // Specialize setName to take care of symbol table majik
40 void Instruction::setName(const std::string &name, SymbolTable *ST) {
41 BasicBlock *P = 0; Function *PP = 0;
42 assert((ST == 0 || !getParent() || !getParent()->getParent() ||
43 ST == &getParent()->getParent()->getSymbolTable()) &&
44 "Invalid symtab argument!");
45 if ((P = getParent()) && (PP = P->getParent()) && hasName())
46 PP->getSymbolTable().remove(this);
48 if (PP && hasName()) PP->getSymbolTable().insert(this);
52 const char *Instruction::getOpcodeName(unsigned OpCode) {
55 case Ret: return "ret";
57 case Switch: return "switch";
58 case Invoke: return "invoke";
59 case Unwind: return "unwind";
61 // Standard binary operators...
62 case Add: return "add";
63 case Sub: return "sub";
64 case Mul: return "mul";
65 case Div: return "div";
66 case Rem: return "rem";
68 // Logical operators...
69 case And: return "and";
70 case Or : return "or";
71 case Xor: return "xor";
74 case SetLE: return "setle";
75 case SetGE: return "setge";
76 case SetLT: return "setlt";
77 case SetGT: return "setgt";
78 case SetEQ: return "seteq";
79 case SetNE: return "setne";
81 // Memory instructions...
82 case Malloc: return "malloc";
83 case Free: return "free";
84 case Alloca: return "alloca";
85 case Load: return "load";
86 case Store: return "store";
87 case GetElementPtr: return "getelementptr";
89 // Other instructions...
90 case PHINode: return "phi";
91 case Cast: return "cast";
92 case Call: return "call";
93 case Shl: return "shl";
94 case Shr: return "shr";
95 case VarArg: return "va_arg";
97 default: return "<Invalid operator> ";
104 /// isAssociative - Return true if the instruction is associative:
106 /// Associative operators satisfy: x op (y op z) === (x op y) op z)
108 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
109 /// applied to floating point types.
111 bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
112 if (Opcode == Add || Opcode == Mul ||
113 Opcode == And || Opcode == Or || Opcode == Xor) {
114 // Floating point operations do not associate!
115 return !Ty->isFloatingPoint();
120 /// isCommutative - Return true if the instruction is commutative:
122 /// Commutative operators satistify: (x op y) === (y op x)
124 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
125 /// applied to any type.
127 bool Instruction::isCommutative(unsigned op) {
143 /// isTrappingInstruction - Return true if the instruction may trap.
145 bool Instruction::isTrapping(unsigned op) {