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