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/Function.h"
15 #include "llvm/SymbolTable.h"
16 #include "llvm/Type.h"
17 #include "Support/LeakDetector.h"
19 Instruction::Instruction(const Type *ty, unsigned it, const std::string &Name,
20 Instruction *InsertBefore)
21 : User(ty, Value::InstructionVal, Name) {
25 // Make sure that we get added to a basicblock
26 LeakDetector::addGarbageObject(this);
28 // If requested, insert this instruction into a basic block...
30 assert(InsertBefore->getParent() &&
31 "Instruction to insert before is not in a basic block!");
32 InsertBefore->getParent()->getInstList().insert(InsertBefore, this);
36 void Instruction::setParent(BasicBlock *P) {
38 LeakDetector::addGarbageObject(this);
43 LeakDetector::removeGarbageObject(this);
46 // Specialize setName to take care of symbol table majik
47 void Instruction::setName(const std::string &name, SymbolTable *ST) {
48 BasicBlock *P = 0; Function *PP = 0;
49 assert((ST == 0 || !getParent() || !getParent()->getParent() ||
50 ST == &getParent()->getParent()->getSymbolTable()) &&
51 "Invalid symtab argument!");
52 if ((P = getParent()) && (PP = P->getParent()) && hasName())
53 PP->getSymbolTable().remove(this);
55 if (PP && hasName()) PP->getSymbolTable().insert(this);
59 const char *Instruction::getOpcodeName(unsigned OpCode) {
62 case Ret: return "ret";
64 case Switch: return "switch";
65 case Invoke: return "invoke";
66 case Unwind: return "unwind";
68 // Standard binary operators...
69 case Add: return "add";
70 case Sub: return "sub";
71 case Mul: return "mul";
72 case Div: return "div";
73 case Rem: return "rem";
75 // Logical operators...
76 case And: return "and";
77 case Or : return "or";
78 case Xor: return "xor";
81 case SetLE: return "setle";
82 case SetGE: return "setge";
83 case SetLT: return "setlt";
84 case SetGT: return "setgt";
85 case SetEQ: return "seteq";
86 case SetNE: return "setne";
88 // Memory instructions...
89 case Malloc: return "malloc";
90 case Free: return "free";
91 case Alloca: return "alloca";
92 case Load: return "load";
93 case Store: return "store";
94 case GetElementPtr: return "getelementptr";
96 // Other instructions...
97 case PHI: return "phi";
98 case Cast: return "cast";
99 case Call: return "call";
100 case Shl: return "shl";
101 case Shr: return "shr";
102 case VANext: return "vanext";
103 case VAArg: return "vaarg";
105 default: return "<Invalid operator> ";
112 /// isAssociative - Return true if the instruction is associative:
114 /// Associative operators satisfy: x op (y op z) === (x op y) op z)
116 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
117 /// applied to floating point types.
119 bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
120 if (Opcode == Add || Opcode == Mul ||
121 Opcode == And || Opcode == Or || Opcode == Xor) {
122 // Floating point operations do not associate!
123 return !Ty->isFloatingPoint();
128 /// isCommutative - Return true if the instruction is commutative:
130 /// Commutative operators satisfy: (x op y) === (y op x)
132 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
133 /// applied to any type.
135 bool Instruction::isCommutative(unsigned op) {
151 /// isTrappingInstruction - Return true if the instruction may trap.
153 bool Instruction::isTrapping(unsigned op) {