7589bb9a35c6ee36d1f3b8f60a0234ede8bb0eb0
[oota-llvm.git] / lib / VMCore / ConstantFolding.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 <assert.h>
37
38 #include "llvm/Constants.h"
39 #include "llvm/Type.h"
40 class PointerType;
41
42 //===----------------------------------------------------------------------===//
43 //  Implement == and != directly...
44 //===----------------------------------------------------------------------===//
45
46 inline ConstantBool *operator==(const Constant &V1, 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, const Constant &V2) {
52   return ConstantBool::get(&V1 != &V2);
53 }
54
55 //===----------------------------------------------------------------------===//
56 //  Implement all other operators indirectly through TypeRules system
57 //===----------------------------------------------------------------------===//
58
59 class ConstRules : public Annotation {
60 protected:
61   inline ConstRules() : Annotation(AID) {}  // Can only be subclassed...
62 public:
63   static AnnotationID AID;    // AnnotationID for this class
64
65   // Binary Operators...
66   virtual Constant *add(const Constant *V1, const Constant *V2) const = 0;
67   virtual Constant *sub(const Constant *V1, const Constant *V2) const = 0;
68   virtual Constant *mul(const Constant *V1, const Constant *V2) const = 0;
69   virtual Constant *div(const Constant *V1, const Constant *V2) const = 0;
70   virtual Constant *rem(const Constant *V1, const Constant *V2) const = 0;
71   virtual Constant *op_and(const Constant *V1, const Constant *V2) const = 0;
72   virtual Constant *op_or (const Constant *V1, const Constant *V2) const = 0;
73   virtual Constant *op_xor(const Constant *V1, const Constant *V2) const = 0;
74   virtual Constant *shl(const Constant *V1, const Constant *V2) const = 0;
75   virtual Constant *shr(const Constant *V1, const Constant *V2) const = 0;
76
77   virtual ConstantBool *lessthan(const Constant *V1, 
78                                  const Constant *V2) const = 0;
79
80   // Casting operators.  ick
81   virtual ConstantBool *castToBool  (const Constant *V) const = 0;
82   virtual ConstantSInt *castToSByte (const Constant *V) const = 0;
83   virtual ConstantUInt *castToUByte (const Constant *V) const = 0;
84   virtual ConstantSInt *castToShort (const Constant *V) const = 0;
85   virtual ConstantUInt *castToUShort(const Constant *V) const = 0;
86   virtual ConstantSInt *castToInt   (const Constant *V) const = 0;
87   virtual ConstantUInt *castToUInt  (const Constant *V) const = 0;
88   virtual ConstantSInt *castToLong  (const Constant *V) const = 0;
89   virtual ConstantUInt *castToULong (const Constant *V) const = 0;
90   virtual ConstantFP   *castToFloat (const Constant *V) const = 0;
91   virtual ConstantFP   *castToDouble(const Constant *V) const = 0;
92   virtual Constant     *castToPointer(const Constant *V,
93                                       const PointerType *Ty) const = 0;
94
95   inline Constant *castTo(const Constant *V, const Type *Ty) const {
96     switch (Ty->getPrimitiveID()) {
97     case Type::BoolTyID:   return castToBool(V);
98     case Type::UByteTyID:  return castToUByte(V);
99     case Type::SByteTyID:  return castToSByte(V);
100     case Type::UShortTyID: return castToUShort(V);
101     case Type::ShortTyID:  return castToShort(V);
102     case Type::UIntTyID:   return castToUInt(V);
103     case Type::IntTyID:    return castToInt(V);
104     case Type::ULongTyID:  return castToULong(V);
105     case Type::LongTyID:   return castToLong(V);
106     case Type::FloatTyID:  return castToFloat(V);
107     case Type::DoubleTyID: return castToDouble(V);
108     case Type::PointerTyID:return castToPointer(V, (PointerType*)Ty);
109     default: return 0;
110     }
111   }
112
113   // ConstRules::get - A type will cache its own type rules if one is needed...
114   // we just want to make sure to hit the cache instead of doing it indirectly,
115   //  if possible...
116   //
117   static inline ConstRules *get(const Constant &V1, const Constant &V2) {
118     if (isa<ConstantExpr>(V1) || isa<ConstantExpr>(V2))
119       return getConstantExprRules();
120     return (ConstRules*)V1.getType()->getOrCreateAnnotation(AID);
121   }
122 private:
123   static ConstRules *getConstantExprRules();
124   static Annotation *find(AnnotationID AID, const Annotable *Ty, void *);
125
126   ConstRules(const ConstRules &);             // Do not implement
127   ConstRules &operator=(const ConstRules &);  // Do not implement
128 };
129
130
131 // Standard binary operators...
132 inline Constant *operator+(const Constant &V1, const Constant &V2) {
133   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
134   return ConstRules::get(V1, V2)->add(&V1, &V2);
135 }
136
137 inline Constant *operator-(const Constant &V1, const Constant &V2) {
138   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
139   return ConstRules::get(V1, V2)->sub(&V1, &V2);
140 }
141
142 inline Constant *operator*(const Constant &V1, const Constant &V2) {
143   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
144   return ConstRules::get(V1, V2)->mul(&V1, &V2);
145 }
146
147 inline Constant *operator/(const Constant &V1, const Constant &V2) {
148   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
149   return ConstRules::get(V1, V2)->div(&V1, &V2);
150 }
151
152 inline Constant *operator%(const Constant &V1, const Constant &V2) {
153   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
154   return ConstRules::get(V1, V2)->rem(&V1, &V2);
155 }
156
157 // Logical Operators...
158 inline Constant *operator&(const Constant &V1, const Constant &V2) {
159   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
160   return ConstRules::get(V1, V2)->op_and(&V1, &V2);
161 }
162
163 inline Constant *operator|(const Constant &V1, const Constant &V2) {
164   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
165   return ConstRules::get(V1, V2)->op_or(&V1, &V2);
166 }
167
168 inline Constant *operator^(const Constant &V1, const Constant &V2) {
169   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
170   return ConstRules::get(V1, V2)->op_xor(&V1, &V2);
171 }
172
173 // Shift Instructions...
174 inline Constant *operator<<(const Constant &V1, const Constant &V2) {
175   assert(V1.getType()->isInteger() && V2.getType() == Type::UByteTy);
176   return ConstRules::get(V1, V2)->shl(&V1, &V2);
177 }
178
179 inline Constant *operator>>(const Constant &V1, const Constant &V2) {
180   assert(V1.getType()->isInteger() && V2.getType() == Type::UByteTy);
181   return ConstRules::get(V1, V2)->shr(&V1, &V2);
182 }
183
184 inline ConstantBool *operator<(const Constant &V1, 
185                                const Constant &V2) {
186   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
187   return ConstRules::get(V1, V2)->lessthan(&V1, &V2);
188 }
189
190
191 //===----------------------------------------------------------------------===//
192 //  Implement 'derived' operators based on what we already have...
193 //===----------------------------------------------------------------------===//
194
195 inline ConstantBool *operator>(const Constant &V1, 
196                                const Constant &V2) {
197   return V2 < V1;
198 }
199
200 inline ConstantBool *operator>=(const Constant &V1, 
201                                 const Constant &V2) {
202   if (ConstantBool *V = (V1 < V2))
203     return V->inverted();                // !(V1 < V2)
204   return 0;
205 }
206
207 inline ConstantBool *operator<=(const Constant &V1, 
208                                 const Constant &V2) {
209   if (ConstantBool *V = (V1 > V2))
210     return V->inverted();                // !(V1 > V2)
211   return 0;
212 }
213
214
215 //===----------------------------------------------------------------------===//
216 //  Implement higher level instruction folding type instructions
217 //===----------------------------------------------------------------------===//
218
219 // ConstantFoldInstruction - Attempt to constant fold the specified instruction.
220 // If successful, the constant result is returned, if not, null is returned.
221 //
222 Constant *ConstantFoldInstruction(Instruction *I);
223
224 // Constant fold various types of instruction...
225 Constant *ConstantFoldCastInstruction(const Constant *V, const Type *DestTy);
226 Constant *ConstantFoldBinaryInstruction(unsigned Opcode, const Constant *V1,
227                                         const Constant *V2);
228 Constant *ConstantFoldShiftInstruction(unsigned Opcode, const Constant *V1,
229                                        const Constant *V2);
230 Constant *ConstantFoldGetElementPtr(const Constant *C,
231                                     const std::vector<Constant*> &IdxList);
232 #endif