blob: 5fd11c3a2ecc33be34c0dae69646e41f57aef9d5 [file] [log] [blame]
Chris Lattner2f7c9632001-06-06 20:29:01 +00001//===- ConstantHandling.cpp - Implement ConstantHandling.h ----------------===//
2//
3// This file implements the various intrinsic operations, on constant values.
4//
5//===----------------------------------------------------------------------===//
6
Chris Lattner65b529f2002-04-08 20:18:09 +00007#include "llvm/ConstantHandling.h"
Chris Lattner0a144ad2002-05-03 21:41:07 +00008#include <cmath>
Chris Lattnerd42d4922001-06-30 04:36:40 +00009
Chris Lattner61607ee2001-09-09 21:01:20 +000010AnnotationID ConstRules::AID(AnnotationManager::getID("opt::ConstRules",
11 &ConstRules::find));
12
Chris Lattner2f7c9632001-06-06 20:29:01 +000013//===----------------------------------------------------------------------===//
14// TemplateRules Class
15//===----------------------------------------------------------------------===//
16//
17// TemplateRules - Implement a subclass of ConstRules that provides all
18// operations as noops. All other rules classes inherit from this class so
19// that if functionality is needed in the future, it can simply be added here
20// and to ConstRules without changing anything else...
21//
22// This class also provides subclasses with typesafe implementations of methods
23// so that don't have to do type casting.
24//
25template<class ArgType, class SubClassName>
26class TemplateRules : public ConstRules {
27
28 //===--------------------------------------------------------------------===//
29 // Redirecting functions that cast to the appropriate types
30 //===--------------------------------------------------------------------===//
31
Chris Lattner3462ae32001-12-03 22:26:30 +000032 virtual Constant *op_not(const Constant *V) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +000033 return SubClassName::Not((const ArgType *)V);
34 }
35
36
Chris Lattner3462ae32001-12-03 22:26:30 +000037 virtual Constant *add(const Constant *V1,
38 const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +000039 return SubClassName::Add((const ArgType *)V1, (const ArgType *)V2);
40 }
41
Chris Lattner3462ae32001-12-03 22:26:30 +000042 virtual Constant *sub(const Constant *V1,
43 const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +000044 return SubClassName::Sub((const ArgType *)V1, (const ArgType *)V2);
45 }
46
Chris Lattner3462ae32001-12-03 22:26:30 +000047 virtual Constant *mul(const Constant *V1,
48 const Constant *V2) const {
Chris Lattner4f6031f2001-07-20 19:15:36 +000049 return SubClassName::Mul((const ArgType *)V1, (const ArgType *)V2);
50 }
Chris Lattneraf259a72002-04-07 08:10:14 +000051 virtual Constant *div(const Constant *V1,
52 const Constant *V2) const {
53 return SubClassName::Div((const ArgType *)V1, (const ArgType *)V2);
54 }
Chris Lattner0a144ad2002-05-03 21:41:07 +000055 virtual Constant *rem(const Constant *V1,
56 const Constant *V2) const {
57 return SubClassName::Rem((const ArgType *)V1, (const ArgType *)V2);
58 }
Chris Lattner4f6031f2001-07-20 19:15:36 +000059
Chris Lattner3462ae32001-12-03 22:26:30 +000060 virtual ConstantBool *lessthan(const Constant *V1,
61 const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +000062 return SubClassName::LessThan((const ArgType *)V1, (const ArgType *)V2);
63 }
64
Chris Lattner55406842001-07-21 19:10:49 +000065 // Casting operators. ick
Chris Lattner3462ae32001-12-03 22:26:30 +000066 virtual ConstantBool *castToBool(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +000067 return SubClassName::CastToBool((const ArgType*)V);
68 }
Chris Lattner3462ae32001-12-03 22:26:30 +000069 virtual ConstantSInt *castToSByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +000070 return SubClassName::CastToSByte((const ArgType*)V);
71 }
Chris Lattner3462ae32001-12-03 22:26:30 +000072 virtual ConstantUInt *castToUByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +000073 return SubClassName::CastToUByte((const ArgType*)V);
74 }
Chris Lattner3462ae32001-12-03 22:26:30 +000075 virtual ConstantSInt *castToShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +000076 return SubClassName::CastToShort((const ArgType*)V);
77 }
Chris Lattner3462ae32001-12-03 22:26:30 +000078 virtual ConstantUInt *castToUShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +000079 return SubClassName::CastToUShort((const ArgType*)V);
80 }
Chris Lattner3462ae32001-12-03 22:26:30 +000081 virtual ConstantSInt *castToInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +000082 return SubClassName::CastToInt((const ArgType*)V);
83 }
Chris Lattner3462ae32001-12-03 22:26:30 +000084 virtual ConstantUInt *castToUInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +000085 return SubClassName::CastToUInt((const ArgType*)V);
86 }
Chris Lattner3462ae32001-12-03 22:26:30 +000087 virtual ConstantSInt *castToLong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +000088 return SubClassName::CastToLong((const ArgType*)V);
89 }
Chris Lattner3462ae32001-12-03 22:26:30 +000090 virtual ConstantUInt *castToULong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +000091 return SubClassName::CastToULong((const ArgType*)V);
92 }
Chris Lattner3462ae32001-12-03 22:26:30 +000093 virtual ConstantFP *castToFloat(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +000094 return SubClassName::CastToFloat((const ArgType*)V);
95 }
Chris Lattner3462ae32001-12-03 22:26:30 +000096 virtual ConstantFP *castToDouble(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +000097 return SubClassName::CastToDouble((const ArgType*)V);
98 }
Chris Lattner3462ae32001-12-03 22:26:30 +000099 virtual ConstantPointer *castToPointer(const Constant *V,
100 const PointerType *Ty) const {
Chris Lattner977f0042001-11-01 05:55:13 +0000101 return SubClassName::CastToPointer((const ArgType*)V, Ty);
102 }
Chris Lattner55406842001-07-21 19:10:49 +0000103
Chris Lattner2f7c9632001-06-06 20:29:01 +0000104 //===--------------------------------------------------------------------===//
105 // Default "noop" implementations
106 //===--------------------------------------------------------------------===//
107
Chris Lattner3462ae32001-12-03 22:26:30 +0000108 inline static Constant *Not(const ArgType *V) { return 0; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000109
Chris Lattner3462ae32001-12-03 22:26:30 +0000110 inline static Constant *Add(const ArgType *V1, const ArgType *V2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000111 return 0;
112 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000113 inline static Constant *Sub(const ArgType *V1, const ArgType *V2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000114 return 0;
115 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000116 inline static Constant *Mul(const ArgType *V1, const ArgType *V2) {
Chris Lattner4f6031f2001-07-20 19:15:36 +0000117 return 0;
118 }
Chris Lattneraf259a72002-04-07 08:10:14 +0000119 inline static Constant *Div(const ArgType *V1, const ArgType *V2) {
120 return 0;
121 }
Chris Lattner0a144ad2002-05-03 21:41:07 +0000122 inline static Constant *Rem(const ArgType *V1, const ArgType *V2) {
123 return 0;
124 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000125 inline static ConstantBool *LessThan(const ArgType *V1, const ArgType *V2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000126 return 0;
127 }
Chris Lattner55406842001-07-21 19:10:49 +0000128
129 // Casting operators. ick
Chris Lattner3462ae32001-12-03 22:26:30 +0000130 inline static ConstantBool *CastToBool (const Constant *V) { return 0; }
131 inline static ConstantSInt *CastToSByte (const Constant *V) { return 0; }
132 inline static ConstantUInt *CastToUByte (const Constant *V) { return 0; }
133 inline static ConstantSInt *CastToShort (const Constant *V) { return 0; }
134 inline static ConstantUInt *CastToUShort(const Constant *V) { return 0; }
135 inline static ConstantSInt *CastToInt (const Constant *V) { return 0; }
136 inline static ConstantUInt *CastToUInt (const Constant *V) { return 0; }
137 inline static ConstantSInt *CastToLong (const Constant *V) { return 0; }
138 inline static ConstantUInt *CastToULong (const Constant *V) { return 0; }
139 inline static ConstantFP *CastToFloat (const Constant *V) { return 0; }
140 inline static ConstantFP *CastToDouble(const Constant *V) { return 0; }
141 inline static ConstantPointer *CastToPointer(const Constant *,
142 const PointerType *) {return 0;}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000143};
144
145
146
147//===----------------------------------------------------------------------===//
148// EmptyRules Class
149//===----------------------------------------------------------------------===//
150//
151// EmptyRules provides a concrete base class of ConstRules that does nothing
152//
Chris Lattner3462ae32001-12-03 22:26:30 +0000153struct EmptyRules : public TemplateRules<Constant, EmptyRules> {
Chris Lattner61607ee2001-09-09 21:01:20 +0000154};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000155
156
157
158//===----------------------------------------------------------------------===//
159// BoolRules Class
160//===----------------------------------------------------------------------===//
161//
162// BoolRules provides a concrete base class of ConstRules for the 'bool' type.
163//
Chris Lattner3462ae32001-12-03 22:26:30 +0000164struct BoolRules : public TemplateRules<ConstantBool, BoolRules> {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000165
Chris Lattner3462ae32001-12-03 22:26:30 +0000166 inline static Constant *Not(const ConstantBool *V) {
167 return ConstantBool::get(!V->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000168 }
169
Chris Lattner3462ae32001-12-03 22:26:30 +0000170 inline static Constant *Or(const ConstantBool *V1,
171 const ConstantBool *V2) {
172 return ConstantBool::get(V1->getValue() | V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000173 }
174
Chris Lattner3462ae32001-12-03 22:26:30 +0000175 inline static Constant *And(const ConstantBool *V1,
176 const ConstantBool *V2) {
177 return ConstantBool::get(V1->getValue() & V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000178 }
Chris Lattner61607ee2001-09-09 21:01:20 +0000179};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000180
181
182//===----------------------------------------------------------------------===//
Chris Lattner977f0042001-11-01 05:55:13 +0000183// PointerRules Class
184//===----------------------------------------------------------------------===//
185//
186// PointerRules provides a concrete base class of ConstRules for pointer types
187//
Chris Lattner3462ae32001-12-03 22:26:30 +0000188struct PointerRules : public TemplateRules<ConstantPointer, PointerRules> {
189 inline static ConstantBool *CastToBool (const Constant *V) {
190 if (V->isNullValue()) return ConstantBool::False;
Chris Lattner977f0042001-11-01 05:55:13 +0000191 return 0; // Can't const prop other types of pointers
192 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000193 inline static ConstantSInt *CastToSByte (const Constant *V) {
194 if (V->isNullValue()) return ConstantSInt::get(Type::SByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000195 return 0; // Can't const prop other types of pointers
196 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000197 inline static ConstantUInt *CastToUByte (const Constant *V) {
198 if (V->isNullValue()) return ConstantUInt::get(Type::UByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000199 return 0; // Can't const prop other types of pointers
200 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000201 inline static ConstantSInt *CastToShort (const Constant *V) {
202 if (V->isNullValue()) return ConstantSInt::get(Type::ShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000203 return 0; // Can't const prop other types of pointers
204 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000205 inline static ConstantUInt *CastToUShort(const Constant *V) {
206 if (V->isNullValue()) return ConstantUInt::get(Type::UShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000207 return 0; // Can't const prop other types of pointers
208 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000209 inline static ConstantSInt *CastToInt (const Constant *V) {
210 if (V->isNullValue()) return ConstantSInt::get(Type::IntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000211 return 0; // Can't const prop other types of pointers
212 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000213 inline static ConstantUInt *CastToUInt (const Constant *V) {
214 if (V->isNullValue()) return ConstantUInt::get(Type::UIntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000215 return 0; // Can't const prop other types of pointers
216 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000217 inline static ConstantSInt *CastToLong (const Constant *V) {
218 if (V->isNullValue()) return ConstantSInt::get(Type::LongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000219 return 0; // Can't const prop other types of pointers
220 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000221 inline static ConstantUInt *CastToULong (const Constant *V) {
222 if (V->isNullValue()) return ConstantUInt::get(Type::ULongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000223 return 0; // Can't const prop other types of pointers
224 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000225 inline static ConstantFP *CastToFloat (const Constant *V) {
226 if (V->isNullValue()) return ConstantFP::get(Type::FloatTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000227 return 0; // Can't const prop other types of pointers
228 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000229 inline static ConstantFP *CastToDouble(const Constant *V) {
230 if (V->isNullValue()) return ConstantFP::get(Type::DoubleTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000231 return 0; // Can't const prop other types of pointers
232 }
233
Chris Lattner3462ae32001-12-03 22:26:30 +0000234 inline static ConstantPointer *CastToPointer(const ConstantPointer *V,
235 const PointerType *PTy) {
Chris Lattner62af86e2002-05-03 20:09:52 +0000236 if (V->getType() == PTy)
237 return const_cast<ConstantPointer*>(V); // Allow cast %PTy %ptr to %PTy
Chris Lattner977f0042001-11-01 05:55:13 +0000238 if (V->isNullValue())
Chris Lattner3462ae32001-12-03 22:26:30 +0000239 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000240 return 0; // Can't const prop other types of pointers
241 }
242};
243
244
245//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000246// DirectRules Class
247//===----------------------------------------------------------------------===//
248//
249// DirectRules provides a concrete base classes of ConstRules for a variety of
250// different types. This allows the C++ compiler to automatically generate our
251// constant handling operations in a typesafe and accurate manner.
252//
Chris Lattner0a144ad2002-05-03 21:41:07 +0000253template<class ConstantClass, class BuiltinType, Type **Ty, class SuperClass>
254struct DirectRules : public TemplateRules<ConstantClass, SuperClass> {
Chris Lattner3462ae32001-12-03 22:26:30 +0000255 inline static Constant *Add(const ConstantClass *V1,
256 const ConstantClass *V2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000257 BuiltinType Result = (BuiltinType)V1->getValue() +
258 (BuiltinType)V2->getValue();
Chris Lattner3462ae32001-12-03 22:26:30 +0000259 return ConstantClass::get(*Ty, Result);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000260 }
261
Chris Lattner3462ae32001-12-03 22:26:30 +0000262 inline static Constant *Sub(const ConstantClass *V1,
263 const ConstantClass *V2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000264 BuiltinType Result = (BuiltinType)V1->getValue() -
265 (BuiltinType)V2->getValue();
Chris Lattner3462ae32001-12-03 22:26:30 +0000266 return ConstantClass::get(*Ty, Result);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000267 }
268
Chris Lattner3462ae32001-12-03 22:26:30 +0000269 inline static Constant *Mul(const ConstantClass *V1,
270 const ConstantClass *V2) {
Chris Lattner4f6031f2001-07-20 19:15:36 +0000271 BuiltinType Result = (BuiltinType)V1->getValue() *
272 (BuiltinType)V2->getValue();
Chris Lattner3462ae32001-12-03 22:26:30 +0000273 return ConstantClass::get(*Ty, Result);
Chris Lattner4f6031f2001-07-20 19:15:36 +0000274 }
275
Chris Lattner0a144ad2002-05-03 21:41:07 +0000276 inline static Constant *Div(const ConstantClass *V1,
Chris Lattneraf259a72002-04-07 08:10:14 +0000277 const ConstantClass *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000278 if (V2->isNullValue()) return 0;
Chris Lattneraf259a72002-04-07 08:10:14 +0000279 BuiltinType Result = (BuiltinType)V1->getValue() /
280 (BuiltinType)V2->getValue();
281 return ConstantClass::get(*Ty, Result);
282 }
283
Chris Lattner3462ae32001-12-03 22:26:30 +0000284 inline static ConstantBool *LessThan(const ConstantClass *V1,
285 const ConstantClass *V2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000286 bool Result = (BuiltinType)V1->getValue() < (BuiltinType)V2->getValue();
Chris Lattner3462ae32001-12-03 22:26:30 +0000287 return ConstantBool::get(Result);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000288 }
Chris Lattner55406842001-07-21 19:10:49 +0000289
Chris Lattner3462ae32001-12-03 22:26:30 +0000290 inline static ConstantPointer *CastToPointer(const ConstantClass *V,
291 const PointerType *PTy) {
Chris Lattner977f0042001-11-01 05:55:13 +0000292 if (V->isNullValue()) // Is it a FP or Integral null value?
Chris Lattner3462ae32001-12-03 22:26:30 +0000293 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000294 return 0; // Can't const prop other types of pointers
295 }
296
Chris Lattner55406842001-07-21 19:10:49 +0000297 // Casting operators. ick
298#define DEF_CAST(TYPE, CLASS, CTYPE) \
Chris Lattner3462ae32001-12-03 22:26:30 +0000299 inline static CLASS *CastTo##TYPE (const ConstantClass *V) { \
Chris Lattnerbbb22962001-09-07 16:40:34 +0000300 return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getValue()); \
Chris Lattner55406842001-07-21 19:10:49 +0000301 }
302
Chris Lattner3462ae32001-12-03 22:26:30 +0000303 DEF_CAST(Bool , ConstantBool, bool)
304 DEF_CAST(SByte , ConstantSInt, signed char)
305 DEF_CAST(UByte , ConstantUInt, unsigned char)
306 DEF_CAST(Short , ConstantSInt, signed short)
307 DEF_CAST(UShort, ConstantUInt, unsigned short)
308 DEF_CAST(Int , ConstantSInt, signed int)
309 DEF_CAST(UInt , ConstantUInt, unsigned int)
310 DEF_CAST(Long , ConstantSInt, int64_t)
311 DEF_CAST(ULong , ConstantUInt, uint64_t)
312 DEF_CAST(Float , ConstantFP , float)
313 DEF_CAST(Double, ConstantFP , double)
Chris Lattner55406842001-07-21 19:10:49 +0000314#undef DEF_CAST
Chris Lattner2f7c9632001-06-06 20:29:01 +0000315};
316
Chris Lattner62af86e2002-05-03 20:09:52 +0000317
318//===----------------------------------------------------------------------===//
319// DirectIntRules Class
320//===----------------------------------------------------------------------===//
321//
322// DirectIntRules provides implementations of functions that are valid on
323// integer types, but not all types in general.
324//
325template <class ConstantClass, class BuiltinType, Type **Ty>
Chris Lattner0a144ad2002-05-03 21:41:07 +0000326struct DirectIntRules
327 : public DirectRules<ConstantClass, BuiltinType, Ty,
328 DirectIntRules<ConstantClass, BuiltinType, Ty> > {
Chris Lattner62af86e2002-05-03 20:09:52 +0000329 inline static Constant *Not(const ConstantClass *V) {
330 return ConstantClass::get(*Ty, ~(BuiltinType)V->getValue());;
331 }
Chris Lattner0a144ad2002-05-03 21:41:07 +0000332
333 inline static Constant *Rem(const ConstantClass *V1,
334 const ConstantClass *V2) {
335 if (V2->isNullValue()) return 0;
336 BuiltinType Result = (BuiltinType)V1->getValue() %
337 (BuiltinType)V2->getValue();
338 return ConstantClass::get(*Ty, Result);
339 }
340};
341
342
343//===----------------------------------------------------------------------===//
344// DirectFPRules Class
345//===----------------------------------------------------------------------===//
346//
347// DirectFPRules provides implementations of functions that are valid on
348// floating point types, but not all types in general.
349//
350template <class ConstantClass, class BuiltinType, Type **Ty>
351struct DirectFPRules
352 : public DirectRules<ConstantClass, BuiltinType, Ty,
353 DirectFPRules<ConstantClass, BuiltinType, Ty> > {
354 inline static Constant *Rem(const ConstantClass *V1,
355 const ConstantClass *V2) {
356 if (V2->isNullValue()) return 0;
357 BuiltinType Result = std::fmod((BuiltinType)V1->getValue(),
358 (BuiltinType)V2->getValue());
359 return ConstantClass::get(*Ty, Result);
360 }
Chris Lattner62af86e2002-05-03 20:09:52 +0000361};
362
363
Chris Lattner2f7c9632001-06-06 20:29:01 +0000364//===----------------------------------------------------------------------===//
365// DirectRules Subclasses
366//===----------------------------------------------------------------------===//
367//
368// Given the DirectRules class we can now implement lots of types with little
369// code. Thank goodness C++ compilers are great at stomping out layers of
370// templates... can you imagine having to do this all by hand? (/me is lazy :)
371//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000372
373// ConstRules::find - Return the constant rules that take care of the specified
Chris Lattner61607ee2001-09-09 21:01:20 +0000374// type.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000375//
Chris Lattner61607ee2001-09-09 21:01:20 +0000376Annotation *ConstRules::find(AnnotationID AID, const Annotable *TyA, void *) {
377 assert(AID == ConstRules::AID && "Bad annotation for factory!");
Chris Lattner8f191122001-10-01 18:26:53 +0000378 const Type *Ty = cast<Type>((const Value*)TyA);
Chris Lattner61607ee2001-09-09 21:01:20 +0000379
Chris Lattner2f7c9632001-06-06 20:29:01 +0000380 switch (Ty->getPrimitiveID()) {
Chris Lattner977f0042001-11-01 05:55:13 +0000381 case Type::BoolTyID: return new BoolRules();
382 case Type::PointerTyID: return new PointerRules();
Chris Lattner61607ee2001-09-09 21:01:20 +0000383 case Type::SByteTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000384 return new DirectIntRules<ConstantSInt, signed char , &Type::SByteTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000385 case Type::UByteTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000386 return new DirectIntRules<ConstantUInt, unsigned char , &Type::UByteTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000387 case Type::ShortTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000388 return new DirectIntRules<ConstantSInt, signed short, &Type::ShortTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000389 case Type::UShortTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000390 return new DirectIntRules<ConstantUInt, unsigned short, &Type::UShortTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000391 case Type::IntTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000392 return new DirectIntRules<ConstantSInt, signed int , &Type::IntTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000393 case Type::UIntTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000394 return new DirectIntRules<ConstantUInt, unsigned int , &Type::UIntTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000395 case Type::LongTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000396 return new DirectIntRules<ConstantSInt, int64_t , &Type::LongTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000397 case Type::ULongTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000398 return new DirectIntRules<ConstantUInt, uint64_t , &Type::ULongTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000399 case Type::FloatTyID:
Chris Lattner0a144ad2002-05-03 21:41:07 +0000400 return new DirectFPRules<ConstantFP , float , &Type::FloatTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000401 case Type::DoubleTyID:
Chris Lattner0a144ad2002-05-03 21:41:07 +0000402 return new DirectFPRules<ConstantFP , double , &Type::DoubleTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000403 default:
404 return new EmptyRules();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000405 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000406}