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 "llvm/Support/LeakDetector.h"
20 void Instruction::init()
22 // Make sure that we get added to a basicblock
23 LeakDetector::addGarbageObject(this);
26 Instruction::Instruction(const Type *ty, unsigned it, const std::string &Name,
27 Instruction *InsertBefore)
28 : User(ty, Value::InstructionVal + it, Name), Parent(0) {
31 // If requested, insert this instruction into a basic block...
33 assert(InsertBefore->getParent() &&
34 "Instruction to insert before is not in a basic block!");
35 InsertBefore->getParent()->getInstList().insert(InsertBefore, this);
39 Instruction::Instruction(const Type *ty, unsigned it, const std::string &Name,
40 BasicBlock *InsertAtEnd)
41 : User(ty, Value::InstructionVal + it, Name), Parent(0) {
44 // append this instruction into the basic block
45 assert(InsertAtEnd && "Basic block to append to may not be NULL!");
46 InsertAtEnd->getInstList().push_back(this);
49 void Instruction::setOpcode(unsigned opc) {
50 setValueType(Value::InstructionVal + opc);
53 void Instruction::setParent(BasicBlock *P) {
55 if (!P) LeakDetector::addGarbageObject(this);
57 if (P) LeakDetector::removeGarbageObject(this);
63 // Specialize setName to take care of symbol table majik
64 void Instruction::setName(const std::string &name, SymbolTable *ST) {
65 BasicBlock *P = 0; Function *PP = 0;
66 assert((ST == 0 || !getParent() || !getParent()->getParent() ||
67 ST == &getParent()->getParent()->getSymbolTable()) &&
68 "Invalid symtab argument!");
69 if ((P = getParent()) && (PP = P->getParent()) && hasName())
70 PP->getSymbolTable().remove(this);
72 if (PP && hasName()) PP->getSymbolTable().insert(this);
75 void Instruction::removeFromParent() {
76 getParent()->getInstList().remove(this);
79 void Instruction::eraseFromParent() {
80 getParent()->getInstList().erase(this);
83 const char *Instruction::getOpcodeName(unsigned OpCode) {
86 case Ret: return "ret";
88 case Switch: return "switch";
89 case Invoke: return "invoke";
90 case Unwind: return "unwind";
91 case Unreachable: return "unreachable";
93 // Standard binary operators...
94 case Add: return "add";
95 case Sub: return "sub";
96 case Mul: return "mul";
97 case Div: return "div";
98 case Rem: return "rem";
100 // Logical operators...
101 case And: return "and";
102 case Or : return "or";
103 case Xor: return "xor";
105 // SetCC operators...
106 case SetLE: return "setle";
107 case SetGE: return "setge";
108 case SetLT: return "setlt";
109 case SetGT: return "setgt";
110 case SetEQ: return "seteq";
111 case SetNE: return "setne";
113 // Memory instructions...
114 case Malloc: return "malloc";
115 case Free: return "free";
116 case Alloca: return "alloca";
117 case Load: return "load";
118 case Store: return "store";
119 case GetElementPtr: return "getelementptr";
121 // Other instructions...
122 case PHI: return "phi";
123 case Cast: return "cast";
124 case Select: return "select";
125 case Call: return "call";
126 case Shl: return "shl";
127 case Shr: return "shr";
128 case VANext: return "vanext";
129 case VAArg: return "vaarg";
131 default: return "<Invalid operator> ";
138 /// isAssociative - Return true if the instruction is associative:
140 /// Associative operators satisfy: x op (y op z) === (x op y) op z)
142 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
143 /// applied to floating point types.
145 bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
146 if (Opcode == Add || Opcode == Mul ||
147 Opcode == And || Opcode == Or || Opcode == Xor) {
148 // Floating point operations do not associate!
149 return !Ty->isFloatingPoint();
154 /// isCommutative - Return true if the instruction is commutative:
156 /// Commutative operators satisfy: (x op y) === (y op x)
158 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
159 /// applied to any type.
161 bool Instruction::isCommutative(unsigned op) {
176 /// isRelational - Return true if the instruction is a Set* instruction:
178 bool Instruction::isRelational(unsigned op) {
193 /// isTrappingInstruction - Return true if the instruction may trap.
195 bool Instruction::isTrapping(unsigned op) {