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