Implement constant propogation of shift instructions
[oota-llvm.git] / lib / VMCore / ConstantFold.h
1 //===-- ConstantHandling.h - Stuff for manipulating constants ----*- C++ -*--=//
2 //
3 // This file contains the declarations of some cool operators that allow you
4 // to do natural things with constant pool values.
5 //
6 // Unfortunately we can't overload operators on pointer types (like this:)
7 //
8 //      inline bool operator==(const Constant *V1, const Constant *V2)
9 //
10 // so we must make due with references, even though it leads to some butt ugly
11 // looking code downstream.  *sigh*  (ex:  Constant *Result = *V1 + *v2; )
12 //
13 //===----------------------------------------------------------------------===//
14 //
15 // WARNING: These operators may return a null object if I don't know how to 
16 //          perform the specified operation on the specified constant types.
17 //
18 //===----------------------------------------------------------------------===//
19 //
20 // Implementation notes:
21 //   This library is implemented this way for a reason: In most cases, we do
22 //   not want to have to link the constant mucking code into an executable.
23 //   We do, however want to tie some of this into the main type system, as an
24 //   optional component.  By using a mutable cache member in the Type class, we
25 //   get exactly the kind of behavior we want.
26 //
27 // In the end, we get performance almost exactly the same as having a virtual
28 // function dispatch, but we don't have to put our virtual functions into the
29 // "Type" class, and we can implement functionality with templates. Good deal.
30 //
31 //===----------------------------------------------------------------------===//
32
33 #ifndef LLVM_CONSTANTHANDLING_H
34 #define LLVM_CONSTANTHANDLING_H
35
36 #include "llvm/Constants.h"
37 #include "llvm/Instruction.h"
38 #include "llvm/Type.h"
39 class PointerType;
40
41 //===----------------------------------------------------------------------===//
42 //  Implement == and != directly...
43 //===----------------------------------------------------------------------===//
44
45 inline ConstantBool *operator==(const Constant &V1, 
46                                 const Constant &V2) {
47   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
48   return ConstantBool::get(&V1 == &V2);
49 }
50
51 inline ConstantBool *operator!=(const Constant &V1, 
52                                 const Constant &V2) {
53   return ConstantBool::get(&V1 != &V2);
54 }
55
56 //===----------------------------------------------------------------------===//
57 //  Implement all other operators indirectly through TypeRules system
58 //===----------------------------------------------------------------------===//
59
60 class ConstRules : public Annotation {
61 protected:
62   inline ConstRules() : Annotation(AID) {}  // Can only be subclassed...
63 public:
64   static AnnotationID AID;    // AnnotationID for this class
65
66   // Unary Operators...
67   virtual Constant *op_not(const Constant *V) const = 0;
68
69   // Binary Operators...
70   virtual Constant *add(const Constant *V1, const Constant *V2) const = 0;
71   virtual Constant *sub(const Constant *V1, const Constant *V2) const = 0;
72   virtual Constant *mul(const Constant *V1, const Constant *V2) const = 0;
73   virtual Constant *div(const Constant *V1, const Constant *V2) const = 0;
74   virtual Constant *rem(const Constant *V1, const Constant *V2) const = 0;
75   virtual Constant *shl(const Constant *V1, const Constant *V2) const = 0;
76   virtual Constant *shr(const Constant *V1, const Constant *V2) const = 0;
77
78   virtual ConstantBool *lessthan(const Constant *V1, 
79                                  const Constant *V2) const = 0;
80
81   // Casting operators.  ick
82   virtual ConstantBool *castToBool  (const Constant *V) const = 0;
83   virtual ConstantSInt *castToSByte (const Constant *V) const = 0;
84   virtual ConstantUInt *castToUByte (const Constant *V) const = 0;
85   virtual ConstantSInt *castToShort (const Constant *V) const = 0;
86   virtual ConstantUInt *castToUShort(const Constant *V) const = 0;
87   virtual ConstantSInt *castToInt   (const Constant *V) const = 0;
88   virtual ConstantUInt *castToUInt  (const Constant *V) const = 0;
89   virtual ConstantSInt *castToLong  (const Constant *V) const = 0;
90   virtual ConstantUInt *castToULong (const Constant *V) const = 0;
91   virtual ConstantFP   *castToFloat (const Constant *V) const = 0;
92   virtual ConstantFP   *castToDouble(const Constant *V) const = 0;
93   virtual ConstantPointer *castToPointer(const Constant *V,
94                                          const PointerType *Ty) const = 0;
95
96   inline Constant *castTo(const Constant *V, const Type *Ty) const {
97     switch (Ty->getPrimitiveID()) {
98     case Type::BoolTyID:   return castToBool(V);
99     case Type::UByteTyID:  return castToUByte(V);
100     case Type::SByteTyID:  return castToSByte(V);
101     case Type::UShortTyID: return castToUShort(V);
102     case Type::ShortTyID:  return castToShort(V);
103     case Type::UIntTyID:   return castToUInt(V);
104     case Type::IntTyID:    return castToInt(V);
105     case Type::ULongTyID:  return castToULong(V);
106     case Type::LongTyID:   return castToLong(V);
107     case Type::FloatTyID:  return castToFloat(V);
108     case Type::DoubleTyID: return castToDouble(V);
109     case Type::PointerTyID:return castToPointer(V, (PointerType*)Ty);
110     default: return 0;
111     }
112   }
113
114   // ConstRules::get - A type will cache its own type rules if one is needed...
115   // we just want to make sure to hit the cache instead of doing it indirectly,
116   //  if possible...
117   //
118   static inline ConstRules *get(const Constant &V) {
119     return (ConstRules*)V.getType()->getOrCreateAnnotation(AID);
120   }
121 private :
122   static Annotation *find(AnnotationID AID, const Annotable *Ty, void *);
123
124   ConstRules(const ConstRules &);             // Do not implement
125   ConstRules &operator=(const ConstRules &);  // Do not implement
126 };
127
128
129 inline Constant *operator~(const Constant &V) {
130   return ConstRules::get(V)->op_not(&V);
131 }
132
133
134
135 inline Constant *operator+(const Constant &V1, const Constant &V2) {
136   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
137   return ConstRules::get(V1)->add(&V1, &V2);
138 }
139
140 inline Constant *operator-(const Constant &V1, const Constant &V2) {
141   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
142   return ConstRules::get(V1)->sub(&V1, &V2);
143 }
144
145 inline Constant *operator*(const Constant &V1, const Constant &V2) {
146   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
147   return ConstRules::get(V1)->mul(&V1, &V2);
148 }
149
150 inline Constant *operator/(const Constant &V1, const Constant &V2) {
151   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
152   return ConstRules::get(V1)->div(&V1, &V2);
153 }
154
155 inline Constant *operator%(const Constant &V1, const Constant &V2) {
156   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
157   return ConstRules::get(V1)->rem(&V1, &V2);
158 }
159
160 inline Constant *operator<<(const Constant &V1, const Constant &V2) {
161   assert(V1.getType()->isIntegral() && V2.getType() == Type::UByteTy);
162   return ConstRules::get(V1)->shl(&V1, &V2);
163 }
164
165 inline Constant *operator>>(const Constant &V1, const Constant &V2) {
166   assert(V1.getType()->isIntegral() && V2.getType() == Type::UByteTy);
167   return ConstRules::get(V1)->shr(&V1, &V2);
168 }
169
170 inline ConstantBool *operator<(const Constant &V1, 
171                                const Constant &V2) {
172   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
173   return ConstRules::get(V1)->lessthan(&V1, &V2);
174 }
175
176
177 //===----------------------------------------------------------------------===//
178 //  Implement 'derived' operators based on what we already have...
179 //===----------------------------------------------------------------------===//
180
181 inline ConstantBool *operator>(const Constant &V1, 
182                                const Constant &V2) {
183   return V2 < V1;
184 }
185
186 inline ConstantBool *operator>=(const Constant &V1, 
187                                 const Constant &V2) {
188   return (V1 < V2)->inverted();      // !(V1 < V2)
189 }
190
191 inline ConstantBool *operator<=(const Constant &V1, 
192                                 const Constant &V2) {
193   return (V1 > V2)->inverted();      // !(V1 > V2)
194 }
195
196
197 //===----------------------------------------------------------------------===//
198 //  Implement higher level instruction folding type instructions
199 //===----------------------------------------------------------------------===//
200
201 inline Constant *ConstantFoldCastInstruction(const Constant *V,
202                                              const Type *DestTy) {
203   return ConstRules::get(*V)->castTo(V, DestTy);
204 }
205
206 inline Constant *ConstantFoldUnaryInstruction(unsigned Opcode, 
207                                               const Constant *V) {
208   switch (Opcode) {
209   case Instruction::Not:  return ~*V;
210   }
211   return 0;
212 }
213
214 inline Constant *ConstantFoldBinaryInstruction(unsigned Opcode,
215                                                const Constant *V1, 
216                                                const Constant *V2) {
217   switch (Opcode) {
218   case Instruction::Add:     return *V1 + *V2;
219   case Instruction::Sub:     return *V1 - *V2;
220   case Instruction::Mul:     return *V1 * *V2;
221   case Instruction::Div:     return *V1 / *V2;
222   case Instruction::Rem:     return *V1 % *V2;
223
224   case Instruction::SetEQ:   return *V1 == *V2;
225   case Instruction::SetNE:   return *V1 != *V2;
226   case Instruction::SetLE:   return *V1 <= *V2;
227   case Instruction::SetGE:   return *V1 >= *V2;
228   case Instruction::SetLT:   return *V1 <  *V2;
229   case Instruction::SetGT:   return *V1 >  *V2;
230   }
231   return 0;
232 }
233
234 inline Constant *ConstantFoldShiftInstruction(unsigned Opcode,
235                                               const Constant *V1, 
236                                               const Constant *V2) {
237   switch (Opcode) {
238   case Instruction::Shl:     return *V1 << *V2;
239   case Instruction::Shr:     return *V1 >> *V2;
240   default:                   return 0;
241   }
242 }
243
244 #endif