The noreturn GCC extension is now supported.
[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/Instructions.h"
15 #include "llvm/Function.h"
16 #include "llvm/SymbolTable.h"
17 #include "llvm/Type.h"
18 #include "llvm/Support/LeakDetector.h"
19 using namespace llvm;
20
21 void Instruction::init() {
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 void Instruction::removeFromParent() {
76   getParent()->getInstList().remove(this);
77 }
78
79 void Instruction::eraseFromParent() {
80   getParent()->getInstList().erase(this);
81 }
82
83 const char *Instruction::getOpcodeName(unsigned OpCode) {
84   switch (OpCode) {
85   // Terminators
86   case Ret:    return "ret";
87   case Br:     return "br";
88   case Switch: return "switch";
89   case Invoke: return "invoke";
90   case Unwind: return "unwind";
91   case Unreachable: return "unreachable";
92     
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";
99
100   // Logical operators...
101   case And: return "and";
102   case Or : return "or";
103   case Xor: return "xor";
104
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";
112     
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";
120     
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";
130
131   default: return "<Invalid operator> ";
132   }
133   
134   return 0;
135 }
136
137 /// isIdenticalTo - Return true if the specified instruction is exactly
138 /// identical to the current one.  This means that all operands match and any
139 /// extra information (e.g. load is volatile) agree.
140 bool Instruction::isIdenticalTo(Instruction *I) const {
141   if (getOpcode() != I->getOpcode() ||
142       getNumOperands() != I->getNumOperands() ||
143       getType() != I->getType())
144     return false;
145
146   // We have two instructions of identical opcode and #operands.  Check to see
147   // if all operands are the same.
148   for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
149     if (getOperand(i) != I->getOperand(i))
150       return false;
151
152   // Check special state that is a part of some instructions.
153   if (const LoadInst *LI = dyn_cast<LoadInst>(this))
154     return LI->isVolatile() == cast<LoadInst>(I)->isVolatile();
155   if (const StoreInst *SI = dyn_cast<StoreInst>(this))
156     return SI->isVolatile() == cast<StoreInst>(I)->isVolatile();
157   if (const VANextInst *VAN = dyn_cast<VANextInst>(this))
158     return VAN->getArgType() == cast<VANextInst>(I)->getArgType();
159   return true;
160 }
161
162
163 /// isAssociative - Return true if the instruction is associative:
164 ///
165 ///   Associative operators satisfy:  x op (y op z) === (x op y) op z)
166 ///
167 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
168 /// applied to floating point types.
169 ///
170 bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
171   if (Opcode == Add || Opcode == Mul ||
172       Opcode == And || Opcode == Or || Opcode == Xor) {
173     // Floating point operations do not associate!
174     return !Ty->isFloatingPoint();
175   }
176   return 0;
177 }
178
179 /// isCommutative - Return true if the instruction is commutative:
180 ///
181 ///   Commutative operators satisfy: (x op y) === (y op x)
182 ///
183 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
184 /// applied to any type.
185 ///
186 bool Instruction::isCommutative(unsigned op) {
187   switch (op) {
188   case Add:
189   case Mul:
190   case And: 
191   case Or:
192   case Xor:
193   case SetEQ:
194   case SetNE:
195     return true;
196   default:
197     return false;
198   }
199 }
200
201 /// isRelational - Return true if the instruction is a Set* instruction:
202 ///
203 bool Instruction::isRelational(unsigned op) {
204   switch (op) {
205   case SetEQ:
206   case SetNE:
207   case SetLT:
208   case SetGT:
209   case SetLE:
210   case SetGE:
211     return true;
212   }
213   return false;
214 }
215
216
217
218 /// isTrappingInstruction - Return true if the instruction may trap.
219 ///
220 bool Instruction::isTrapping(unsigned op) {
221   switch(op) {
222   case Div:
223   case Rem:
224   case Load:
225   case Store:
226   case Call:
227   case Invoke:
228     return true;
229   default:
230     return false;
231   }
232 }