If changing a parent, don't add then remove the object from the leak detector
[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 Instruction::Instruction(const Type *ty, unsigned it, const std::string &Name,
21                          Instruction *InsertBefore)
22   : User(ty, Value::InstructionVal, Name) {
23   Parent = 0;
24   iType = it;
25
26   // Make sure that we get added to a basicblock
27   LeakDetector::addGarbageObject(this);
28
29   // If requested, insert this instruction into a basic block...
30   if (InsertBefore) {
31     assert(InsertBefore->getParent() &&
32            "Instruction to insert before is not in a basic block!");
33     InsertBefore->getParent()->getInstList().insert(InsertBefore, this);
34   }
35 }
36
37 void Instruction::setParent(BasicBlock *P) {
38   if (getParent()) {
39     if (!P) LeakDetector::addGarbageObject(this);
40   } else {
41     if (P) LeakDetector::removeGarbageObject(this);
42   }
43
44   Parent = P;
45 }
46
47 // Specialize setName to take care of symbol table majik
48 void Instruction::setName(const std::string &name, SymbolTable *ST) {
49   BasicBlock *P = 0; Function *PP = 0;
50   assert((ST == 0 || !getParent() || !getParent()->getParent() || 
51           ST == &getParent()->getParent()->getSymbolTable()) &&
52          "Invalid symtab argument!");
53   if ((P = getParent()) && (PP = P->getParent()) && hasName())
54     PP->getSymbolTable().remove(this);
55   Value::setName(name);
56   if (PP && hasName()) PP->getSymbolTable().insert(this);
57 }
58
59
60 const char *Instruction::getOpcodeName(unsigned OpCode) {
61   switch (OpCode) {
62   // Terminators
63   case Ret:    return "ret";
64   case Br:     return "br";
65   case Switch: return "switch";
66   case Invoke: return "invoke";
67   case Unwind: return "unwind";
68     
69   // Standard binary operators...
70   case Add: return "add";
71   case Sub: return "sub";
72   case Mul: return "mul";
73   case Div: return "div";
74   case Rem: return "rem";
75
76   // Logical operators...
77   case And: return "and";
78   case Or : return "or";
79   case Xor: return "xor";
80
81   // SetCC operators...
82   case SetLE:  return "setle";
83   case SetGE:  return "setge";
84   case SetLT:  return "setlt";
85   case SetGT:  return "setgt";
86   case SetEQ:  return "seteq";
87   case SetNE:  return "setne";
88     
89   // Memory instructions...
90   case Malloc:        return "malloc";
91   case Free:          return "free";
92   case Alloca:        return "alloca";
93   case Load:          return "load";
94   case Store:         return "store";
95   case GetElementPtr: return "getelementptr";
96     
97   // Other instructions...
98   case PHI:     return "phi";
99   case Cast:    return "cast";
100   case Call:    return "call";
101   case Shl:     return "shl";
102   case Shr:     return "shr";
103   case VANext:  return "vanext";
104   case VAArg:   return "vaarg";
105
106   default: return "<Invalid operator> ";
107   }
108   
109   return 0;
110 }
111
112
113 /// isAssociative - Return true if the instruction is associative:
114 ///
115 ///   Associative operators satisfy:  x op (y op z) === (x op y) op z)
116 ///
117 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
118 /// applied to floating point types.
119 ///
120 bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
121   if (Opcode == Add || Opcode == Mul ||
122       Opcode == And || Opcode == Or || Opcode == Xor) {
123     // Floating point operations do not associate!
124     return !Ty->isFloatingPoint();
125   }
126   return 0;
127 }
128
129 /// isCommutative - Return true if the instruction is commutative:
130 ///
131 ///   Commutative operators satisfy: (x op y) === (y op x)
132 ///
133 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
134 /// applied to any type.
135 ///
136 bool Instruction::isCommutative(unsigned op) {
137   switch (op) {
138   case Add:
139   case Mul:
140   case And: 
141   case Or:
142   case Xor:
143   case SetEQ:
144   case SetNE:
145     return true;
146   default:
147     return false;
148   }
149 }
150
151 /// isRelational - Return true if the instruction is a Set* instruction:
152 ///
153 bool Instruction::isRelational(unsigned op) {
154   switch (op) {
155   case SetEQ:
156   case SetNE:
157   case SetLT:
158   case SetGT:
159   case SetLE:
160   case SetGE:
161     return true;
162   }
163   return false;
164 }
165
166
167
168 /// isTrappingInstruction - Return true if the instruction may trap.
169 ///
170 bool Instruction::isTrapping(unsigned op) {
171   switch(op) {
172   case Div:
173   case Rem:
174   case Load:
175   case Store:
176   case Call:
177   case Invoke:
178     return true;
179   default:
180     return false;
181   }
182 }