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