Merging the linear scan register allocator in trunk. It currently passes most tests...
[oota-llvm.git] / include / llvm / ConstantHandling.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 all other operators indirectly through TypeRules system
52 //===----------------------------------------------------------------------===//
53
54 struct ConstRules {
55   ConstRules() {}
56
57   // Binary Operators...
58   virtual Constant *add(const Constant *V1, const Constant *V2) const = 0;
59   virtual Constant *sub(const Constant *V1, const Constant *V2) const = 0;
60   virtual Constant *mul(const Constant *V1, const Constant *V2) const = 0;
61   virtual Constant *div(const Constant *V1, const Constant *V2) const = 0;
62   virtual Constant *rem(const Constant *V1, const Constant *V2) const = 0;
63   virtual Constant *op_and(const Constant *V1, const Constant *V2) const = 0;
64   virtual Constant *op_or (const Constant *V1, const Constant *V2) const = 0;
65   virtual Constant *op_xor(const Constant *V1, const Constant *V2) const = 0;
66   virtual Constant *shl(const Constant *V1, const Constant *V2) const = 0;
67   virtual Constant *shr(const Constant *V1, const Constant *V2) const = 0;
68
69   virtual ConstantBool *lessthan(const Constant *V1, 
70                                  const Constant *V2) const = 0;
71   virtual ConstantBool *equalto(const Constant *V1, 
72                                 const Constant *V2) const = 0;
73
74   // Casting operators.  ick
75   virtual ConstantBool *castToBool  (const Constant *V) const = 0;
76   virtual ConstantSInt *castToSByte (const Constant *V) const = 0;
77   virtual ConstantUInt *castToUByte (const Constant *V) const = 0;
78   virtual ConstantSInt *castToShort (const Constant *V) const = 0;
79   virtual ConstantUInt *castToUShort(const Constant *V) const = 0;
80   virtual ConstantSInt *castToInt   (const Constant *V) const = 0;
81   virtual ConstantUInt *castToUInt  (const Constant *V) const = 0;
82   virtual ConstantSInt *castToLong  (const Constant *V) const = 0;
83   virtual ConstantUInt *castToULong (const Constant *V) const = 0;
84   virtual ConstantFP   *castToFloat (const Constant *V) const = 0;
85   virtual ConstantFP   *castToDouble(const Constant *V) const = 0;
86   virtual Constant     *castToPointer(const Constant *V,
87                                       const PointerType *Ty) const = 0;
88
89   inline Constant *castTo(const Constant *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     case Type::PointerTyID:
103       return castToPointer(V, reinterpret_cast<const PointerType*>(Ty));
104     default: return 0;
105     }
106   }
107
108   // ConstRules::get - Return an instance of ConstRules for the specified
109   // constant operands.
110   //
111   static ConstRules &get(const Constant &V1, const Constant &V2);
112 private:
113   ConstRules(const ConstRules &);             // Do not implement
114   ConstRules &operator=(const ConstRules &);  // Do not implement
115 };
116
117 // Unary operators...
118 inline Constant *operator~(const Constant &V) {
119   assert(V.getType()->isIntegral() && "Cannot invert non-integral constant!");
120   return ConstRules::get(V, V).op_xor(&V,
121                                     ConstantInt::getAllOnesValue(V.getType()));
122 }
123
124 inline Constant *operator-(const Constant &V) {
125   return ConstRules::get(V, V).sub(Constant::getNullValue(V.getType()), &V);
126 }
127
128 // Standard binary operators...
129 inline Constant *operator+(const Constant &V1, const Constant &V2) {
130   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
131   return ConstRules::get(V1, V2).add(&V1, &V2);
132 }
133
134 inline Constant *operator-(const Constant &V1, const Constant &V2) {
135   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
136   return ConstRules::get(V1, V2).sub(&V1, &V2);
137 }
138
139 inline Constant *operator*(const Constant &V1, const Constant &V2) {
140   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
141   return ConstRules::get(V1, V2).mul(&V1, &V2);
142 }
143
144 inline Constant *operator/(const Constant &V1, const Constant &V2) {
145   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
146   return ConstRules::get(V1, V2).div(&V1, &V2);
147 }
148
149 inline Constant *operator%(const Constant &V1, const Constant &V2) {
150   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
151   return ConstRules::get(V1, V2).rem(&V1, &V2);
152 }
153
154 // Logical Operators...
155 inline Constant *operator&(const Constant &V1, const Constant &V2) {
156   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
157   return ConstRules::get(V1, V2).op_and(&V1, &V2);
158 }
159
160 inline Constant *operator|(const Constant &V1, const Constant &V2) {
161   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
162   return ConstRules::get(V1, V2).op_or(&V1, &V2);
163 }
164
165 inline Constant *operator^(const Constant &V1, const Constant &V2) {
166   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
167   return ConstRules::get(V1, V2).op_xor(&V1, &V2);
168 }
169
170 // Shift Instructions...
171 inline Constant *operator<<(const Constant &V1, const Constant &V2) {
172   assert(V1.getType()->isInteger() && V2.getType() == Type::UByteTy);
173   return ConstRules::get(V1, V2).shl(&V1, &V2);
174 }
175
176 inline Constant *operator>>(const Constant &V1, const Constant &V2) {
177   assert(V1.getType()->isInteger() && V2.getType() == Type::UByteTy);
178   return ConstRules::get(V1, V2).shr(&V1, &V2);
179 }
180
181 inline ConstantBool *operator<(const Constant &V1, 
182                                const Constant &V2) {
183   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
184   return ConstRules::get(V1, V2).lessthan(&V1, &V2);
185 }
186
187 inline ConstantBool *operator==(const Constant &V1, const Constant &V2) {
188   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
189   return ConstRules::get(V1, V2).equalto(&V1, &V2);
190 }
191
192 //===----------------------------------------------------------------------===//
193 //  Implement 'derived' operators based on what we already have...
194 //===----------------------------------------------------------------------===//
195
196 inline ConstantBool *operator!=(const Constant &V1, const Constant &V2) {
197   if (ConstantBool *V = (V1 == V2))
198     return V->inverted();                // !(V1 == V2)
199   return 0;
200 }
201
202 inline ConstantBool *operator>(const Constant &V1, 
203                                const Constant &V2) {
204   return V2 < V1;
205 }
206
207 inline ConstantBool *operator>=(const Constant &V1, 
208                                 const Constant &V2) {
209   if (ConstantBool *V = (V1 < V2))
210     return V->inverted();                // !(V1 < V2)
211   return 0;
212 }
213
214 inline ConstantBool *operator<=(const Constant &V1, 
215                                 const Constant &V2) {
216   if (ConstantBool *V = (V1 > V2))
217     return V->inverted();                // !(V1 > V2)
218   return 0;
219 }
220
221
222 //===----------------------------------------------------------------------===//
223 //  Implement higher level instruction folding type instructions
224 //===----------------------------------------------------------------------===//
225
226 // ConstantFoldInstruction - Attempt to constant fold the specified instruction.
227 // If successful, the constant result is returned, if not, null is returned.
228 //
229 Constant *ConstantFoldInstruction(Instruction *I);
230
231 // Constant fold various types of instruction...
232 Constant *ConstantFoldCastInstruction(const Constant *V, const Type *DestTy);
233 Constant *ConstantFoldBinaryInstruction(unsigned Opcode, const Constant *V1,
234                                         const Constant *V2);
235 Constant *ConstantFoldShiftInstruction(unsigned Opcode, const Constant *V1,
236                                        const Constant *V2);
237 Constant *ConstantFoldGetElementPtr(const Constant *C,
238                                     const std::vector<Constant*> &IdxList);
239
240 } // End llvm namespace
241
242 #endif