Be a bit more efficient when processing the active and inactive
[oota-llvm.git] / lib / VMCore / Instruction.cpp
1 //===-- Instruction.cpp - Implement the Instruction class -----------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the Instruction class for the VMCore library.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Function.h"
15 #include "llvm/SymbolTable.h"
16 #include "llvm/Type.h"
17 #include "Support/LeakDetector.h"
18 using namespace llvm;
19
20 void Instruction::init()
21 {
22   // Make sure that we get added to a basicblock
23   LeakDetector::addGarbageObject(this);
24 }
25
26 Instruction::Instruction(const Type *ty, unsigned it, const std::string &Name,
27                          Instruction *InsertBefore)
28   : User(ty, Value::InstructionVal + it, Name), Parent(0) {
29   init();
30
31   // If requested, insert this instruction into a basic block...
32   if (InsertBefore) {
33     assert(InsertBefore->getParent() &&
34            "Instruction to insert before is not in a basic block!");
35     InsertBefore->getParent()->getInstList().insert(InsertBefore, this);
36   }
37 }
38
39 Instruction::Instruction(const Type *ty, unsigned it, const std::string &Name,
40                          BasicBlock *InsertAtEnd)
41   : User(ty, Value::InstructionVal + it, Name), Parent(0) {
42   init();
43
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);
47 }
48
49 void Instruction::setOpcode(unsigned opc) {
50   setValueType(Value::InstructionVal + opc);
51 }
52
53 void Instruction::setParent(BasicBlock *P) {
54   if (getParent()) {
55     if (!P) LeakDetector::addGarbageObject(this);
56   } else {
57     if (P) LeakDetector::removeGarbageObject(this);
58   }
59
60   Parent = P;
61 }
62
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);
71   Value::setName(name);
72   if (PP && hasName()) PP->getSymbolTable().insert(this);
73 }
74
75
76 const char *Instruction::getOpcodeName(unsigned OpCode) {
77   switch (OpCode) {
78   // Terminators
79   case Ret:    return "ret";
80   case Br:     return "br";
81   case Switch: return "switch";
82   case Invoke: return "invoke";
83   case Unwind: return "unwind";
84     
85   // Standard binary operators...
86   case Add: return "add";
87   case Sub: return "sub";
88   case Mul: return "mul";
89   case Div: return "div";
90   case Rem: return "rem";
91
92   // Logical operators...
93   case And: return "and";
94   case Or : return "or";
95   case Xor: return "xor";
96
97   // SetCC operators...
98   case SetLE:  return "setle";
99   case SetGE:  return "setge";
100   case SetLT:  return "setlt";
101   case SetGT:  return "setgt";
102   case SetEQ:  return "seteq";
103   case SetNE:  return "setne";
104     
105   // Memory instructions...
106   case Malloc:        return "malloc";
107   case Free:          return "free";
108   case Alloca:        return "alloca";
109   case Load:          return "load";
110   case Store:         return "store";
111   case GetElementPtr: return "getelementptr";
112     
113   // Other instructions...
114   case PHI:     return "phi";
115   case Cast:    return "cast";
116   case Select:  return "select";
117   case Call:    return "call";
118   case Shl:     return "shl";
119   case Shr:     return "shr";
120   case VANext:  return "vanext";
121   case VAArg:   return "vaarg";
122
123   default: return "<Invalid operator> ";
124   }
125   
126   return 0;
127 }
128
129
130 /// isAssociative - Return true if the instruction is associative:
131 ///
132 ///   Associative operators satisfy:  x op (y op z) === (x op y) op z)
133 ///
134 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
135 /// applied to floating point types.
136 ///
137 bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
138   if (Opcode == Add || Opcode == Mul ||
139       Opcode == And || Opcode == Or || Opcode == Xor) {
140     // Floating point operations do not associate!
141     return !Ty->isFloatingPoint();
142   }
143   return 0;
144 }
145
146 /// isCommutative - Return true if the instruction is commutative:
147 ///
148 ///   Commutative operators satisfy: (x op y) === (y op x)
149 ///
150 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
151 /// applied to any type.
152 ///
153 bool Instruction::isCommutative(unsigned op) {
154   switch (op) {
155   case Add:
156   case Mul:
157   case And: 
158   case Or:
159   case Xor:
160   case SetEQ:
161   case SetNE:
162     return true;
163   default:
164     return false;
165   }
166 }
167
168 /// isRelational - Return true if the instruction is a Set* instruction:
169 ///
170 bool Instruction::isRelational(unsigned op) {
171   switch (op) {
172   case SetEQ:
173   case SetNE:
174   case SetLT:
175   case SetGT:
176   case SetLE:
177   case SetGE:
178     return true;
179   }
180   return false;
181 }
182
183
184
185 /// isTrappingInstruction - Return true if the instruction may trap.
186 ///
187 bool Instruction::isTrapping(unsigned op) {
188   switch(op) {
189   case Div:
190   case Rem:
191   case Load:
192   case Store:
193   case Call:
194   case Invoke:
195     return true;
196   default:
197     return false;
198   }
199 }