9e9e760ea1f9fba57ec1380c0feaccc9ff537695
[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 ConstPoolVal *V1, const ConstPoolVal *V2)
9 //
10 // so we must make due with references, even though it leads to some butt ugly
11 // looking code downstream.  *sigh*  (ex:  ConstPoolVal *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_OPT_CONSTANTHANDLING_H
34 #define LLVM_OPT_CONSTANTHANDLING_H
35
36 #include "llvm/ConstPoolVals.h"
37 #include "llvm/Instruction.h"
38 #include "llvm/Type.h"
39 class PointerType;
40
41 namespace opt {
42
43 //===----------------------------------------------------------------------===//
44 //  Implement == and != directly...
45 //===----------------------------------------------------------------------===//
46
47 inline ConstPoolBool *operator==(const ConstPoolVal &V1, 
48                                  const ConstPoolVal &V2) {
49   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
50   return ConstPoolBool::get(&V1 == &V2);
51 }
52
53 inline ConstPoolBool *operator!=(const ConstPoolVal &V1, 
54                                  const ConstPoolVal &V2) {
55   return ConstPoolBool::get(&V1 != &V2);
56 }
57
58 //===----------------------------------------------------------------------===//
59 //  Implement all other operators indirectly through TypeRules system
60 //===----------------------------------------------------------------------===//
61
62 class ConstRules : public Annotation {
63 protected:
64   inline ConstRules() : Annotation(AID) {}  // Can only be subclassed...
65 public:
66   static AnnotationID AID;    // AnnotationID for this class
67
68   // Unary Operators...
69   virtual ConstPoolVal *op_not(const ConstPoolVal *V) const = 0;
70
71   // Binary Operators...
72   virtual ConstPoolVal *add(const ConstPoolVal *V1, 
73                             const ConstPoolVal *V2) const = 0;
74   virtual ConstPoolVal *sub(const ConstPoolVal *V1, 
75                             const ConstPoolVal *V2) const = 0;
76   virtual ConstPoolVal *mul(const ConstPoolVal *V1, 
77                             const ConstPoolVal *V2) const = 0;
78
79   virtual ConstPoolBool *lessthan(const ConstPoolVal *V1, 
80                                   const ConstPoolVal *V2) const = 0;
81
82   // Casting operators.  ick
83   virtual ConstPoolBool *castToBool  (const ConstPoolVal *V) const = 0;
84   virtual ConstPoolSInt *castToSByte (const ConstPoolVal *V) const = 0;
85   virtual ConstPoolUInt *castToUByte (const ConstPoolVal *V) const = 0;
86   virtual ConstPoolSInt *castToShort (const ConstPoolVal *V) const = 0;
87   virtual ConstPoolUInt *castToUShort(const ConstPoolVal *V) const = 0;
88   virtual ConstPoolSInt *castToInt   (const ConstPoolVal *V) const = 0;
89   virtual ConstPoolUInt *castToUInt  (const ConstPoolVal *V) const = 0;
90   virtual ConstPoolSInt *castToLong  (const ConstPoolVal *V) const = 0;
91   virtual ConstPoolUInt *castToULong (const ConstPoolVal *V) const = 0;
92   virtual ConstPoolFP   *castToFloat (const ConstPoolVal *V) const = 0;
93   virtual ConstPoolFP   *castToDouble(const ConstPoolVal *V) const = 0;
94   virtual ConstPoolPointer *castToPointer(const ConstPoolVal *V,
95                                           const PointerType *Ty) const = 0;
96
97   inline ConstPoolVal *castTo(const ConstPoolVal *V, const Type *Ty) const {
98     switch (Ty->getPrimitiveID()) {
99     case Type::BoolTyID:   return castToBool(V);
100     case Type::UByteTyID:  return castToUByte(V);
101     case Type::SByteTyID:  return castToSByte(V);
102     case Type::UShortTyID: return castToUShort(V);
103     case Type::ShortTyID:  return castToShort(V);
104     case Type::UIntTyID:   return castToUInt(V);
105     case Type::IntTyID:    return castToInt(V);
106     case Type::ULongTyID:  return castToULong(V);
107     case Type::LongTyID:   return castToLong(V);
108     case Type::FloatTyID:  return castToFloat(V);
109     case Type::DoubleTyID: return castToDouble(V);
110     case Type::PointerTyID:return castToPointer(V, (PointerType*)Ty);
111     default: return 0;
112     }
113   }
114
115   // ConstRules::get - A type will cache its own type rules if one is needed...
116   // we just want to make sure to hit the cache instead of doing it indirectly,
117   //  if possible...
118   //
119   static inline ConstRules *get(const ConstPoolVal &V) {
120     return (ConstRules*)V.getType()->getOrCreateAnnotation(AID);
121   }
122 private :
123   static Annotation *find(AnnotationID AID, const Annotable *Ty, void *);
124
125   ConstRules(const ConstRules &);             // Do not implement
126   ConstRules &operator=(const ConstRules &);  // Do not implement
127 };
128
129
130 inline ConstPoolVal *operator!(const ConstPoolVal &V) {
131   return ConstRules::get(V)->op_not(&V);
132 }
133
134
135
136 inline ConstPoolVal *operator+(const ConstPoolVal &V1, const ConstPoolVal &V2) {
137   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
138   return ConstRules::get(V1)->add(&V1, &V2);
139 }
140
141 inline ConstPoolVal *operator-(const ConstPoolVal &V1, const ConstPoolVal &V2) {
142   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
143   return ConstRules::get(V1)->sub(&V1, &V2);
144 }
145
146 inline ConstPoolVal *operator*(const ConstPoolVal &V1, const ConstPoolVal &V2) {
147   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
148   return ConstRules::get(V1)->mul(&V1, &V2);
149 }
150
151 inline ConstPoolBool *operator<(const ConstPoolVal &V1, 
152                                 const ConstPoolVal &V2) {
153   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
154   return ConstRules::get(V1)->lessthan(&V1, &V2);
155 }
156
157
158 //===----------------------------------------------------------------------===//
159 //  Implement 'derived' operators based on what we already have...
160 //===----------------------------------------------------------------------===//
161
162 inline ConstPoolBool *operator>(const ConstPoolVal &V1, 
163                                 const ConstPoolVal &V2) {
164   return V2 < V1;
165 }
166
167 inline ConstPoolBool *operator>=(const ConstPoolVal &V1, 
168                                  const ConstPoolVal &V2) {
169   return (V1 < V2)->inverted();      // !(V1 < V2)
170 }
171
172 inline ConstPoolBool *operator<=(const ConstPoolVal &V1, 
173                                  const ConstPoolVal &V2) {
174   return (V1 > V2)->inverted();      // !(V1 > V2)
175 }
176
177
178 //===----------------------------------------------------------------------===//
179 //  Implement higher level instruction folding type instructions
180 //===----------------------------------------------------------------------===//
181
182 inline ConstPoolVal *ConstantFoldCastInstruction(ConstPoolVal *V,
183                                                  const Type *DestTy) {
184   return ConstRules::get(*V)->castTo(V, DestTy);
185 }
186
187 inline ConstPoolVal *ConstantFoldUnaryInstruction(unsigned Opcode, 
188                                                   ConstPoolVal *V) {
189   switch (Opcode) {
190   case Instruction::Not:  return !*V;
191     // TODO: Handle get element ptr instruction here in the future? GEP null?
192   }
193   return 0;
194 }
195
196 inline ConstPoolVal *ConstantFoldBinaryInstruction(unsigned Opcode,
197                                                    ConstPoolVal *V1, 
198                                                    ConstPoolVal *V2) {
199   switch (Opcode) {
200   case Instruction::Add:     return *V1 + *V2;
201   case Instruction::Sub:     return *V1 - *V2;
202
203   case Instruction::SetEQ:   return *V1 == *V2;
204   case Instruction::SetNE:   return *V1 != *V2;
205   case Instruction::SetLE:   return *V1 <= *V2;
206   case Instruction::SetGE:   return *V1 >= *V2;
207   case Instruction::SetLT:   return *V1 <  *V2;
208   case Instruction::SetGT:   return *V1 >  *V2;
209   }
210   return 0;
211 }
212
213 } // end namespace opt
214 #endif