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