Add support for casting operators
[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 return pointers to newly 'new'd objects.  You MUST
16 //          make sure to free them if you don't want them hanging around. Also,
17 //          note that these may return a null object if I don't know how to 
18 //          perform those operations on the specified constant types.
19 //
20 //===----------------------------------------------------------------------===//
21 //
22 // Implementation notes:
23 //   This library is implemented this way for a reason: In most cases, we do
24 //   not want to have to link the constant mucking code into an executable.
25 //   We do, however want to tie some of this into the main type system, as an
26 //   optional component.  By using a mutable cache member in the Type class, we
27 //   get exactly the kind of behavior we want.
28 //
29 // In the end, we get performance almost exactly the same as having a virtual
30 // function dispatch, but we don't have to put our virtual functions into the
31 // "Type" class, and we can implement functionality with templates. Good deal.
32 //
33 //===----------------------------------------------------------------------===//
34
35 #ifndef LLVM_OPT_CONSTANTHANDLING_H
36 #define LLVM_OPT_CONSTANTHANDLING_H
37
38 #include "llvm/ConstPoolVals.h"
39 #include "llvm/Instruction.h"
40 #include "llvm/Type.h"
41
42 namespace opt {
43
44 //===----------------------------------------------------------------------===//
45 //  Implement == directly...
46 //===----------------------------------------------------------------------===//
47
48 inline ConstPoolBool *operator==(const ConstPoolVal &V1, 
49                                  const ConstPoolVal &V2) {
50   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
51   return new ConstPoolBool(V1.equals(&V2));
52 }
53
54 //===----------------------------------------------------------------------===//
55 //  Implement all other operators indirectly through TypeRules system
56 //===----------------------------------------------------------------------===//
57
58 class ConstRules {
59 protected:
60   inline ConstRules() {}  // Can only be subclassed...
61 public:
62   // Unary Operators...
63   virtual ConstPoolVal *not(const ConstPoolVal *V) const = 0;
64
65   // Binary Operators...
66   virtual ConstPoolVal *add(const ConstPoolVal *V1, 
67                             const ConstPoolVal *V2) const = 0;
68   virtual ConstPoolVal *sub(const ConstPoolVal *V1, 
69                             const ConstPoolVal *V2) const = 0;
70   virtual ConstPoolVal *mul(const ConstPoolVal *V1, 
71                             const ConstPoolVal *V2) const = 0;
72
73   virtual ConstPoolBool *lessthan(const ConstPoolVal *V1, 
74                                   const ConstPoolVal *V2) const = 0;
75
76   // Casting operators.  ick
77   virtual ConstPoolBool *castToBool  (const ConstPoolVal *V) const = 0;
78   virtual ConstPoolSInt *castToSByte (const ConstPoolVal *V) const = 0;
79   virtual ConstPoolUInt *castToUByte (const ConstPoolVal *V) const = 0;
80   virtual ConstPoolSInt *castToShort (const ConstPoolVal *V) const = 0;
81   virtual ConstPoolUInt *castToUShort(const ConstPoolVal *V) const = 0;
82   virtual ConstPoolSInt *castToInt   (const ConstPoolVal *V) const = 0;
83   virtual ConstPoolUInt *castToUInt  (const ConstPoolVal *V) const = 0;
84   virtual ConstPoolSInt *castToLong  (const ConstPoolVal *V) const = 0;
85   virtual ConstPoolUInt *castToULong (const ConstPoolVal *V) const = 0;
86   virtual ConstPoolFP   *castToFloat (const ConstPoolVal *V) const = 0;
87   virtual ConstPoolFP   *castToDouble(const ConstPoolVal *V) const = 0;
88
89   inline ConstPoolVal *castTo(const ConstPoolVal *V, const Type *Ty) const {
90     switch (Ty->getPrimitiveID()) {
91     case Type::BoolTyID:   return castToBool(V);
92     case Type::UByteTyID:  return castToUByte(V);
93     case Type::SByteTyID:  return castToSByte(V);
94     case Type::UShortTyID: return castToUShort(V);
95     case Type::ShortTyID:  return castToShort(V);
96     case Type::UIntTyID:   return castToUInt(V);
97     case Type::IntTyID:    return castToInt(V);
98     case Type::ULongTyID:  return castToULong(V);
99     case Type::LongTyID:   return castToLong(V);
100     case Type::FloatTyID:  return castToFloat(V);
101     case Type::DoubleTyID: return castToDouble(V);
102     default: return 0;
103     }
104   }
105
106   // ConstRules::get - A type will cache its own type rules if one is needed...
107   // we just want to make sure to hit the cache instead of doing it indirectly,
108   //  if possible...
109   //
110   static inline const ConstRules *get(const ConstPoolVal &V) {
111     const ConstRules *Result = V.getType()->getConstRules();
112     return Result ? Result : find(V.getType());
113   }
114 private :
115   static const ConstRules *find(const Type *Ty);
116
117   ConstRules(const ConstRules &);             // Do not implement
118   ConstRules &operator=(const ConstRules &);  // Do not implement
119 };
120
121
122 inline ConstPoolVal *operator!(const ConstPoolVal &V) {
123   return ConstRules::get(V)->not(&V);
124 }
125
126
127
128 inline ConstPoolVal *operator+(const ConstPoolVal &V1, const ConstPoolVal &V2) {
129   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
130   return ConstRules::get(V1)->add(&V1, &V2);
131 }
132
133 inline ConstPoolVal *operator-(const ConstPoolVal &V1, const ConstPoolVal &V2) {
134   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
135   return ConstRules::get(V1)->sub(&V1, &V2);
136 }
137
138 inline ConstPoolVal *operator*(const ConstPoolVal &V1, const ConstPoolVal &V2) {
139   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
140   return ConstRules::get(V1)->mul(&V1, &V2);
141 }
142
143 inline ConstPoolBool *operator<(const ConstPoolVal &V1, 
144                                 const ConstPoolVal &V2) {
145   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
146   return ConstRules::get(V1)->lessthan(&V1, &V2);
147 }
148
149
150 //===----------------------------------------------------------------------===//
151 //  Implement 'derived' operators based on what we already have...
152 //===----------------------------------------------------------------------===//
153
154 inline ConstPoolBool *operator>(const ConstPoolVal &V1, 
155                                 const ConstPoolVal &V2) {
156   return V2 < V1;
157 }
158
159 inline ConstPoolBool *operator!=(const ConstPoolVal &V1, 
160                                  const ConstPoolVal &V2) {
161   ConstPoolBool *Result = V1 == V2;
162   Result->setValue(!Result->getValue());     // Invert value
163   return Result;     // !(V1 == V2)
164 }
165
166 inline ConstPoolBool *operator>=(const ConstPoolVal &V1, 
167                                  const ConstPoolVal &V2) {
168   ConstPoolBool *Result = V1 < V2;
169   Result->setValue(!Result->getValue());     // Invert value
170   return Result;      // !(V1 < V2)
171 }
172
173 inline ConstPoolBool *operator<=(const ConstPoolVal &V1, 
174                                  const ConstPoolVal &V2) {
175   ConstPoolBool *Result = V1 > V2;
176   Result->setValue(!Result->getValue());     // Invert value
177   return Result;      // !(V1 > V2)
178 }
179
180
181 //===----------------------------------------------------------------------===//
182 //  Implement higher level instruction folding type instructions
183 //===----------------------------------------------------------------------===//
184
185 inline ConstPoolVal *ConstantFoldUnaryInstruction(unsigned Opcode, 
186                                                   ConstPoolVal *V) {
187   switch (Opcode) {
188   case Instruction::Not:  return !*V;
189   }
190   return 0;
191 }
192
193 inline ConstPoolVal *ConstantFoldBinaryInstruction(unsigned Opcode,
194                                                    ConstPoolVal *V1, 
195                                                    ConstPoolVal *V2) {
196   switch (Opcode) {
197   case Instruction::Add:     return *V1 + *V2;
198   case Instruction::Sub:     return *V1 - *V2;
199
200   case Instruction::SetEQ:   return *V1 == *V2;
201   case Instruction::SetNE:   return *V1 != *V2;
202   case Instruction::SetLE:   return *V1 <= *V2;
203   case Instruction::SetGE:   return *V1 >= *V2;
204   case Instruction::SetLT:   return *V1 <  *V2;
205   case Instruction::SetGT:   return *V1 >  *V2;
206   }
207   return 0;
208 }
209
210 } // end namespace opt
211 #endif