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