Implement Assembler/2003-08-21-ConstantExprCast-Fold.llx
[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     
60   // Standard binary operators...
61   case Add: return "add";
62   case Sub: return "sub";
63   case Mul: return "mul";
64   case Div: return "div";
65   case Rem: return "rem";
66
67   // Logical operators...
68   case And: return "and";
69   case Or : return "or";
70   case Xor: return "xor";
71
72   // SetCC operators...
73   case SetLE:  return "setle";
74   case SetGE:  return "setge";
75   case SetLT:  return "setlt";
76   case SetGT:  return "setgt";
77   case SetEQ:  return "seteq";
78   case SetNE:  return "setne";
79     
80   // Memory instructions...
81   case Malloc:        return "malloc";
82   case Free:          return "free";
83   case Alloca:        return "alloca";
84   case Load:          return "load";
85   case Store:         return "store";
86   case GetElementPtr: return "getelementptr";
87     
88   // Other instructions...
89   case PHINode: return "phi";
90   case Cast:    return "cast";
91   case Call:    return "call";
92   case Shl:     return "shl";
93   case Shr:     return "shr";
94   case VarArg:  return "va_arg";
95
96   default: return "<Invalid operator> ";
97   }
98   
99   return 0;
100 }
101
102
103 /// isAssociative - Return true if the instruction is associative:
104 ///
105 ///   Associative operators satisfy:  x op (y op z) === (x op y) op z)
106 ///
107 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
108 /// applied to floating point types.
109 ///
110 bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
111   if (Opcode == Add || Opcode == Mul ||
112       Opcode == And || Opcode == Or || Opcode == Xor) {
113     // Floating point operations do not associate!
114     return !Ty->isFloatingPoint();
115   }
116   return 0;
117 }
118
119 /// isCommutative - Return true if the instruction is commutative:
120 ///
121 ///   Commutative operators satistify: (x op y) === (y op x)
122 ///
123 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
124 /// applied to any type.
125 ///
126 bool Instruction::isCommutative(unsigned op) {
127   switch (op) {
128   case Add:
129   case Mul:
130   case And: 
131   case Or:
132   case Xor:
133   case SetEQ:
134   case SetNE:
135     return true;
136   default:
137     return false;
138   }
139 }
140
141
142 /// isTrappingInstruction - Return true if the instruction may trap.
143 ///
144 bool Instruction::isTrapping(unsigned op) {
145   switch(op) {
146   case Div:
147   case Rem:
148   case Load:
149   case Store:
150   case Call:
151   case Invoke:
152     return true;
153   default:
154     return false;
155   }
156 }