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";
60 // Standard binary operators...
61 case Add: return "add";
62 case Sub: return "sub";
63 case Mul: return "mul";
64 case Div: return "div";
65 case Rem: return "rem";
67 // Logical operators...
68 case And: return "and";
69 case Or : return "or";
70 case Xor: return "xor";
73 case SetLE: return "setle";
74 case SetGE: return "setge";
75 case SetLT: return "setlt";
76 case SetGT: return "setgt";
77 case SetEQ: return "seteq";
78 case SetNE: return "setne";
80 // Memory instructions...
81 case Malloc: return "malloc";
82 case Free: return "free";
83 case Alloca: return "alloca";
84 case Load: return "load";
85 case Store: return "store";
86 case GetElementPtr: return "getelementptr";
88 // Other instructions...
89 case PHINode: return "phi";
90 case Cast: return "cast";
91 case Call: return "call";
92 case Shl: return "shl";
93 case Shr: return "shr";
94 case VarArg: return "va_arg";
96 default: return "<Invalid operator> ";
103 /// isAssociative - Return true if the instruction is associative:
105 /// Associative operators satisfy: x op (y op z) === (x op y) op z)
107 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
108 /// applied to floating point types.
110 bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
111 if (Opcode == Add || Opcode == Mul ||
112 Opcode == And || Opcode == Or || Opcode == Xor) {
113 // Floating point operations do not associate!
114 return !Ty->isFloatingPoint();
119 /// isCommutative - Return true if the instruction is commutative:
121 /// Commutative operators satistify: (x op y) === (y op x)
123 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
124 /// applied to any type.
126 bool Instruction::isCommutative(unsigned op) {
142 /// isTrappingInstruction - Return true if the instruction may trap.
144 bool Instruction::isTrapping(unsigned op) {