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