Move llvm::ConstantFoldInstruction from here to Transforms/Utils
[oota-llvm.git] / lib / VMCore / ConstantFold.cpp
1 //===- ConstantHandling.cpp - Implement ConstantHandling.h ----------------===//
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 implements the various intrinsic operations, on constant values.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/ConstantHandling.h"
15 #include "llvm/iPHINode.h"
16 #include "llvm/InstrTypes.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Support/GetElementPtrTypeIterator.h"
19 #include <cmath>
20 using namespace llvm;
21
22 static unsigned getSize(const Type *Ty) {
23   unsigned S = Ty->getPrimitiveSize();
24   return S ? S : 8;  // Treat pointers at 8 bytes
25 }
26
27 Constant *llvm::ConstantFoldCastInstruction(const Constant *V,
28                                             const Type *DestTy) {
29   if (V->getType() == DestTy) return (Constant*)V;
30
31   if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
32     if (CE->getOpcode() == Instruction::Cast) {
33       Constant *Op = const_cast<Constant*>(CE->getOperand(0));
34       // Try to not produce a cast of a cast, which is almost always redundant.
35       if (!Op->getType()->isFloatingPoint() &&
36           !CE->getType()->isFloatingPoint() &&
37           !DestTy->getType()->isFloatingPoint()) {
38         unsigned S1 = getSize(Op->getType()), S2 = getSize(CE->getType());
39         unsigned S3 = getSize(DestTy);
40         if (Op->getType() == DestTy && S3 >= S2)
41           return Op;
42         if (S1 >= S2 && S2 >= S3)
43           return ConstantExpr::getCast(Op, DestTy);
44         if (S1 <= S2 && S2 >= S3 && S1 <= S3)
45           return ConstantExpr::getCast(Op, DestTy);
46       }
47     } else if (CE->getOpcode() == Instruction::GetElementPtr) {
48       // If all of the indexes in the GEP are null values, there is no pointer
49       // adjustment going on.  We might as well cast the source pointer.
50       bool isAllNull = true;
51       for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
52         if (!CE->getOperand(i)->isNullValue()) {
53           isAllNull = false;
54           break;
55         }
56       if (isAllNull)
57         return ConstantExpr::getCast(CE->getOperand(0), DestTy);
58     }
59
60   return ConstRules::get(*V, *V).castTo(V, DestTy);
61 }
62
63 Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
64                                               const Constant *V1,
65                                               const Constant *V2) {
66   switch (Opcode) {
67   case Instruction::Add:     return *V1 + *V2;
68   case Instruction::Sub:     return *V1 - *V2;
69   case Instruction::Mul:     return *V1 * *V2;
70   case Instruction::Div:     return *V1 / *V2;
71   case Instruction::Rem:     return *V1 % *V2;
72   case Instruction::And:     return *V1 & *V2;
73   case Instruction::Or:      return *V1 | *V2;
74   case Instruction::Xor:     return *V1 ^ *V2;
75
76   case Instruction::SetEQ:   return *V1 == *V2;
77   case Instruction::SetNE:   return *V1 != *V2;
78   case Instruction::SetLE:   return *V1 <= *V2;
79   case Instruction::SetGE:   return *V1 >= *V2;
80   case Instruction::SetLT:   return *V1 <  *V2;
81   case Instruction::SetGT:   return *V1 >  *V2;
82   }
83   return 0;
84 }
85
86 Constant *llvm::ConstantFoldShiftInstruction(unsigned Opcode,
87                                              const Constant *V1, 
88                                              const Constant *V2) {
89   switch (Opcode) {
90   case Instruction::Shl:     return *V1 << *V2;
91   case Instruction::Shr:     return *V1 >> *V2;
92   default:                   return 0;
93   }
94 }
95
96 Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
97                                         const std::vector<Constant*> &IdxList) {
98   if (IdxList.size() == 0 ||
99       (IdxList.size() == 1 && IdxList[0]->isNullValue()))
100     return const_cast<Constant*>(C);
101
102   // TODO If C is null and all idx's are null, return null of the right type.
103
104
105   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) {
106     // Combine Indices - If the source pointer to this getelementptr instruction
107     // is a getelementptr instruction, combine the indices of the two
108     // getelementptr instructions into a single instruction.
109     //
110     if (CE->getOpcode() == Instruction::GetElementPtr) {
111       const Type *LastTy = 0;
112       for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
113            I != E; ++I)
114         LastTy = *I;
115
116       if ((LastTy && isa<ArrayType>(LastTy)) || IdxList[0]->isNullValue()) {
117         std::vector<Constant*> NewIndices;
118         NewIndices.reserve(IdxList.size() + CE->getNumOperands());
119         for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
120           NewIndices.push_back(cast<Constant>(CE->getOperand(i)));
121
122         // Add the last index of the source with the first index of the new GEP.
123         // Make sure to handle the case when they are actually different types.
124         Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
125         if (!IdxList[0]->isNullValue())   // Otherwise it must be an array
126           Combined = 
127             ConstantExpr::get(Instruction::Add,
128                               ConstantExpr::getCast(IdxList[0], Type::LongTy),
129                               ConstantExpr::getCast(Combined, Type::LongTy));
130         
131         NewIndices.push_back(Combined);
132         NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end());
133         return ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices);
134       }
135     }
136
137     // Implement folding of:
138     //    int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
139     //                        long 0, long 0)
140     // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
141     //
142     if (CE->getOpcode() == Instruction::Cast && IdxList.size() > 1 &&
143         IdxList[0]->isNullValue())
144       if (const PointerType *SPT = 
145           dyn_cast<PointerType>(CE->getOperand(0)->getType()))
146         if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
147           if (const ArrayType *CAT =
148               dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
149             if (CAT->getElementType() == SAT->getElementType())
150               return ConstantExpr::getGetElementPtr(
151                       (Constant*)CE->getOperand(0), IdxList);
152   }
153   return 0;
154 }
155
156
157 //===----------------------------------------------------------------------===//
158 //                             TemplateRules Class
159 //===----------------------------------------------------------------------===//
160 //
161 // TemplateRules - Implement a subclass of ConstRules that provides all 
162 // operations as noops.  All other rules classes inherit from this class so 
163 // that if functionality is needed in the future, it can simply be added here 
164 // and to ConstRules without changing anything else...
165 // 
166 // This class also provides subclasses with typesafe implementations of methods
167 // so that don't have to do type casting.
168 //
169 template<class ArgType, class SubClassName>
170 class TemplateRules : public ConstRules {
171
172   //===--------------------------------------------------------------------===//
173   // Redirecting functions that cast to the appropriate types
174   //===--------------------------------------------------------------------===//
175
176   virtual Constant *add(const Constant *V1, const Constant *V2) const { 
177     return SubClassName::Add((const ArgType *)V1, (const ArgType *)V2);  
178   }
179   virtual Constant *sub(const Constant *V1, const Constant *V2) const { 
180     return SubClassName::Sub((const ArgType *)V1, (const ArgType *)V2);  
181   }
182   virtual Constant *mul(const Constant *V1, const Constant *V2) const { 
183     return SubClassName::Mul((const ArgType *)V1, (const ArgType *)V2);  
184   }
185   virtual Constant *div(const Constant *V1, const Constant *V2) const { 
186     return SubClassName::Div((const ArgType *)V1, (const ArgType *)V2);  
187   }
188   virtual Constant *rem(const Constant *V1, const Constant *V2) const { 
189     return SubClassName::Rem((const ArgType *)V1, (const ArgType *)V2);  
190   }
191   virtual Constant *op_and(const Constant *V1, const Constant *V2) const { 
192     return SubClassName::And((const ArgType *)V1, (const ArgType *)V2);  
193   }
194   virtual Constant *op_or(const Constant *V1, const Constant *V2) const { 
195     return SubClassName::Or((const ArgType *)V1, (const ArgType *)V2);  
196   }
197   virtual Constant *op_xor(const Constant *V1, const Constant *V2) const { 
198     return SubClassName::Xor((const ArgType *)V1, (const ArgType *)V2);  
199   }
200   virtual Constant *shl(const Constant *V1, const Constant *V2) const { 
201     return SubClassName::Shl((const ArgType *)V1, (const ArgType *)V2);  
202   }
203   virtual Constant *shr(const Constant *V1, const Constant *V2) const { 
204     return SubClassName::Shr((const ArgType *)V1, (const ArgType *)V2);  
205   }
206
207   virtual ConstantBool *lessthan(const Constant *V1, 
208                                  const Constant *V2) const { 
209     return SubClassName::LessThan((const ArgType *)V1, (const ArgType *)V2);
210   }
211   virtual ConstantBool *equalto(const Constant *V1, 
212                                 const Constant *V2) const { 
213     return SubClassName::EqualTo((const ArgType *)V1, (const ArgType *)V2);
214   }
215
216   // Casting operators.  ick
217   virtual ConstantBool *castToBool(const Constant *V) const {
218     return SubClassName::CastToBool((const ArgType*)V);
219   }
220   virtual ConstantSInt *castToSByte(const Constant *V) const {
221     return SubClassName::CastToSByte((const ArgType*)V);
222   }
223   virtual ConstantUInt *castToUByte(const Constant *V) const {
224     return SubClassName::CastToUByte((const ArgType*)V);
225   }
226   virtual ConstantSInt *castToShort(const Constant *V) const {
227     return SubClassName::CastToShort((const ArgType*)V);
228   }
229   virtual ConstantUInt *castToUShort(const Constant *V) const {
230     return SubClassName::CastToUShort((const ArgType*)V);
231   }
232   virtual ConstantSInt *castToInt(const Constant *V) const {
233     return SubClassName::CastToInt((const ArgType*)V);
234   }
235   virtual ConstantUInt *castToUInt(const Constant *V) const {
236     return SubClassName::CastToUInt((const ArgType*)V);
237   }
238   virtual ConstantSInt *castToLong(const Constant *V) const {
239     return SubClassName::CastToLong((const ArgType*)V);
240   }
241   virtual ConstantUInt *castToULong(const Constant *V) const {
242     return SubClassName::CastToULong((const ArgType*)V);
243   }
244   virtual ConstantFP   *castToFloat(const Constant *V) const {
245     return SubClassName::CastToFloat((const ArgType*)V);
246   }
247   virtual ConstantFP   *castToDouble(const Constant *V) const {
248     return SubClassName::CastToDouble((const ArgType*)V);
249   }
250   virtual Constant *castToPointer(const Constant *V, 
251                                   const PointerType *Ty) const {
252     return SubClassName::CastToPointer((const ArgType*)V, Ty);
253   }
254
255   //===--------------------------------------------------------------------===//
256   // Default "noop" implementations
257   //===--------------------------------------------------------------------===//
258
259   static Constant *Add(const ArgType *V1, const ArgType *V2) { return 0; }
260   static Constant *Sub(const ArgType *V1, const ArgType *V2) { return 0; }
261   static Constant *Mul(const ArgType *V1, const ArgType *V2) { return 0; }
262   static Constant *Div(const ArgType *V1, const ArgType *V2) { return 0; }
263   static Constant *Rem(const ArgType *V1, const ArgType *V2) { return 0; }
264   static Constant *And(const ArgType *V1, const ArgType *V2) { return 0; }
265   static Constant *Or (const ArgType *V1, const ArgType *V2) { return 0; }
266   static Constant *Xor(const ArgType *V1, const ArgType *V2) { return 0; }
267   static Constant *Shl(const ArgType *V1, const ArgType *V2) { return 0; }
268   static Constant *Shr(const ArgType *V1, const ArgType *V2) { return 0; }
269   static ConstantBool *LessThan(const ArgType *V1, const ArgType *V2) {
270     return 0;
271   }
272   static ConstantBool *EqualTo(const ArgType *V1, const ArgType *V2) {
273     return 0;
274   }
275
276   // Casting operators.  ick
277   static ConstantBool *CastToBool  (const Constant *V) { return 0; }
278   static ConstantSInt *CastToSByte (const Constant *V) { return 0; }
279   static ConstantUInt *CastToUByte (const Constant *V) { return 0; }
280   static ConstantSInt *CastToShort (const Constant *V) { return 0; }
281   static ConstantUInt *CastToUShort(const Constant *V) { return 0; }
282   static ConstantSInt *CastToInt   (const Constant *V) { return 0; }
283   static ConstantUInt *CastToUInt  (const Constant *V) { return 0; }
284   static ConstantSInt *CastToLong  (const Constant *V) { return 0; }
285   static ConstantUInt *CastToULong (const Constant *V) { return 0; }
286   static ConstantFP   *CastToFloat (const Constant *V) { return 0; }
287   static ConstantFP   *CastToDouble(const Constant *V) { return 0; }
288   static Constant     *CastToPointer(const Constant *,
289                                      const PointerType *) {return 0;}
290 };
291
292
293
294 //===----------------------------------------------------------------------===//
295 //                             EmptyRules Class
296 //===----------------------------------------------------------------------===//
297 //
298 // EmptyRules provides a concrete base class of ConstRules that does nothing
299 //
300 struct EmptyRules : public TemplateRules<Constant, EmptyRules> {
301   static ConstantBool *EqualTo(const Constant *V1, const Constant *V2) {
302     if (V1 == V2) return ConstantBool::True;
303     return 0;
304   }
305 };
306
307
308
309 //===----------------------------------------------------------------------===//
310 //                              BoolRules Class
311 //===----------------------------------------------------------------------===//
312 //
313 // BoolRules provides a concrete base class of ConstRules for the 'bool' type.
314 //
315 struct BoolRules : public TemplateRules<ConstantBool, BoolRules> {
316
317   static ConstantBool *LessThan(const ConstantBool *V1, const ConstantBool *V2){
318     return ConstantBool::get(V1->getValue() < V2->getValue());
319   }
320
321   static ConstantBool *EqualTo(const Constant *V1, const Constant *V2) {
322     return ConstantBool::get(V1 == V2);
323   }
324
325   static Constant *And(const ConstantBool *V1, const ConstantBool *V2) {
326     return ConstantBool::get(V1->getValue() & V2->getValue());
327   }
328
329   static Constant *Or(const ConstantBool *V1, const ConstantBool *V2) {
330     return ConstantBool::get(V1->getValue() | V2->getValue());
331   }
332
333   static Constant *Xor(const ConstantBool *V1, const ConstantBool *V2) {
334     return ConstantBool::get(V1->getValue() ^ V2->getValue());
335   }
336
337   // Casting operators.  ick
338 #define DEF_CAST(TYPE, CLASS, CTYPE) \
339   static CLASS *CastTo##TYPE  (const ConstantBool *V) {    \
340     return CLASS::get(Type::TYPE##Ty, (CTYPE)(bool)V->getValue()); \
341   }
342
343   DEF_CAST(Bool  , ConstantBool, bool)
344   DEF_CAST(SByte , ConstantSInt, signed char)
345   DEF_CAST(UByte , ConstantUInt, unsigned char)
346   DEF_CAST(Short , ConstantSInt, signed short)
347   DEF_CAST(UShort, ConstantUInt, unsigned short)
348   DEF_CAST(Int   , ConstantSInt, signed int)
349   DEF_CAST(UInt  , ConstantUInt, unsigned int)
350   DEF_CAST(Long  , ConstantSInt, int64_t)
351   DEF_CAST(ULong , ConstantUInt, uint64_t)
352   DEF_CAST(Float , ConstantFP  , float)
353   DEF_CAST(Double, ConstantFP  , double)
354 #undef DEF_CAST
355 };
356
357
358 //===----------------------------------------------------------------------===//
359 //                            NullPointerRules Class
360 //===----------------------------------------------------------------------===//
361 //
362 // NullPointerRules provides a concrete base class of ConstRules for null
363 // pointers.
364 //
365 struct NullPointerRules : public TemplateRules<ConstantPointerNull,
366                                                NullPointerRules> {
367   static ConstantBool *EqualTo(const Constant *V1, const Constant *V2) {
368     return ConstantBool::True;  // Null pointers are always equal
369   }
370   static ConstantBool *CastToBool  (const Constant *V) {
371     return ConstantBool::False;
372   }
373   static ConstantSInt *CastToSByte (const Constant *V) {
374     return ConstantSInt::get(Type::SByteTy, 0);
375   }
376   static ConstantUInt *CastToUByte (const Constant *V) {
377     return ConstantUInt::get(Type::UByteTy, 0);
378   }
379   static ConstantSInt *CastToShort (const Constant *V) {
380     return ConstantSInt::get(Type::ShortTy, 0);
381   }
382   static ConstantUInt *CastToUShort(const Constant *V) {
383     return ConstantUInt::get(Type::UShortTy, 0);
384   }
385   static ConstantSInt *CastToInt   (const Constant *V) {
386     return ConstantSInt::get(Type::IntTy, 0);
387   }
388   static ConstantUInt *CastToUInt  (const Constant *V) {
389     return ConstantUInt::get(Type::UIntTy, 0);
390   }
391   static ConstantSInt *CastToLong  (const Constant *V) {
392     return ConstantSInt::get(Type::LongTy, 0);
393   }
394   static ConstantUInt *CastToULong (const Constant *V) {
395     return ConstantUInt::get(Type::ULongTy, 0);
396   }
397   static ConstantFP   *CastToFloat (const Constant *V) {
398     return ConstantFP::get(Type::FloatTy, 0);
399   }
400   static ConstantFP   *CastToDouble(const Constant *V) {
401     return ConstantFP::get(Type::DoubleTy, 0);
402   }
403
404   static Constant *CastToPointer(const ConstantPointerNull *V,
405                                  const PointerType *PTy) {
406     return ConstantPointerNull::get(PTy);
407   }
408 };
409
410
411 //===----------------------------------------------------------------------===//
412 //                             DirectRules Class
413 //===----------------------------------------------------------------------===//
414 //
415 // DirectRules provides a concrete base classes of ConstRules for a variety of
416 // different types.  This allows the C++ compiler to automatically generate our
417 // constant handling operations in a typesafe and accurate manner.
418 //
419 template<class ConstantClass, class BuiltinType, Type **Ty, class SuperClass>
420 struct DirectRules : public TemplateRules<ConstantClass, SuperClass> {
421   static Constant *Add(const ConstantClass *V1, const ConstantClass *V2) {
422     BuiltinType R = (BuiltinType)V1->getValue() + (BuiltinType)V2->getValue();
423     return ConstantClass::get(*Ty, R);
424   }
425
426   static Constant *Sub(const ConstantClass *V1, const ConstantClass *V2) {
427     BuiltinType R = (BuiltinType)V1->getValue() - (BuiltinType)V2->getValue();
428     return ConstantClass::get(*Ty, R);
429   }
430
431   static Constant *Mul(const ConstantClass *V1, const ConstantClass *V2) {
432     BuiltinType R = (BuiltinType)V1->getValue() * (BuiltinType)V2->getValue();
433     return ConstantClass::get(*Ty, R);
434   }
435
436   static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
437     if (V2->isNullValue()) return 0;
438     BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
439     return ConstantClass::get(*Ty, R);
440   }
441
442   static ConstantBool *LessThan(const ConstantClass *V1,
443                                 const ConstantClass *V2) {
444     bool R = (BuiltinType)V1->getValue() < (BuiltinType)V2->getValue();
445     return ConstantBool::get(R);
446   } 
447
448   static ConstantBool *EqualTo(const ConstantClass *V1,
449                                const ConstantClass *V2) {
450     bool R = (BuiltinType)V1->getValue() == (BuiltinType)V2->getValue();
451     return ConstantBool::get(R);
452   }
453
454   static Constant *CastToPointer(const ConstantClass *V,
455                                  const PointerType *PTy) {
456     if (V->isNullValue())    // Is it a FP or Integral null value?
457       return ConstantPointerNull::get(PTy);
458     return 0;  // Can't const prop other types of pointers
459   }
460
461   // Casting operators.  ick
462 #define DEF_CAST(TYPE, CLASS, CTYPE) \
463   static CLASS *CastTo##TYPE  (const ConstantClass *V) {    \
464     return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getValue()); \
465   }
466
467   DEF_CAST(Bool  , ConstantBool, bool)
468   DEF_CAST(SByte , ConstantSInt, signed char)
469   DEF_CAST(UByte , ConstantUInt, unsigned char)
470   DEF_CAST(Short , ConstantSInt, signed short)
471   DEF_CAST(UShort, ConstantUInt, unsigned short)
472   DEF_CAST(Int   , ConstantSInt, signed int)
473   DEF_CAST(UInt  , ConstantUInt, unsigned int)
474   DEF_CAST(Long  , ConstantSInt, int64_t)
475   DEF_CAST(ULong , ConstantUInt, uint64_t)
476   DEF_CAST(Float , ConstantFP  , float)
477   DEF_CAST(Double, ConstantFP  , double)
478 #undef DEF_CAST
479 };
480
481
482 //===----------------------------------------------------------------------===//
483 //                           DirectIntRules Class
484 //===----------------------------------------------------------------------===//
485 //
486 // DirectIntRules provides implementations of functions that are valid on
487 // integer types, but not all types in general.
488 //
489 template <class ConstantClass, class BuiltinType, Type **Ty>
490 struct DirectIntRules
491   : public DirectRules<ConstantClass, BuiltinType, Ty,
492                        DirectIntRules<ConstantClass, BuiltinType, Ty> > {
493
494   static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
495     if (V2->isNullValue()) return 0;
496     if (V2->isAllOnesValue() &&              // MIN_INT / -1
497         (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
498       return 0;
499     BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
500     return ConstantClass::get(*Ty, R);
501   }
502
503   static Constant *Rem(const ConstantClass *V1,
504                        const ConstantClass *V2) {
505     if (V2->isNullValue()) return 0;         // X / 0
506     if (V2->isAllOnesValue() &&              // MIN_INT / -1
507         (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
508       return 0;
509     BuiltinType R = (BuiltinType)V1->getValue() % (BuiltinType)V2->getValue();
510     return ConstantClass::get(*Ty, R);
511   }
512
513   static Constant *And(const ConstantClass *V1, const ConstantClass *V2) {
514     BuiltinType R = (BuiltinType)V1->getValue() & (BuiltinType)V2->getValue();
515     return ConstantClass::get(*Ty, R);
516   }
517   static Constant *Or(const ConstantClass *V1, const ConstantClass *V2) {
518     BuiltinType R = (BuiltinType)V1->getValue() | (BuiltinType)V2->getValue();
519     return ConstantClass::get(*Ty, R);
520   }
521   static Constant *Xor(const ConstantClass *V1, const ConstantClass *V2) {
522     BuiltinType R = (BuiltinType)V1->getValue() ^ (BuiltinType)V2->getValue();
523     return ConstantClass::get(*Ty, R);
524   }
525
526   static Constant *Shl(const ConstantClass *V1, const ConstantClass *V2) {
527     BuiltinType R = (BuiltinType)V1->getValue() << (BuiltinType)V2->getValue();
528     return ConstantClass::get(*Ty, R);
529   }
530
531   static Constant *Shr(const ConstantClass *V1, const ConstantClass *V2) {
532     BuiltinType R = (BuiltinType)V1->getValue() >> (BuiltinType)V2->getValue();
533     return ConstantClass::get(*Ty, R);
534   }
535 };
536
537
538 //===----------------------------------------------------------------------===//
539 //                           DirectFPRules Class
540 //===----------------------------------------------------------------------===//
541 //
542 // DirectFPRules provides implementations of functions that are valid on
543 // floating point types, but not all types in general.
544 //
545 template <class ConstantClass, class BuiltinType, Type **Ty>
546 struct DirectFPRules
547   : public DirectRules<ConstantClass, BuiltinType, Ty,
548                        DirectFPRules<ConstantClass, BuiltinType, Ty> > {
549   static Constant *Rem(const ConstantClass *V1, const ConstantClass *V2) {
550     if (V2->isNullValue()) return 0;
551     BuiltinType Result = std::fmod((BuiltinType)V1->getValue(),
552                                    (BuiltinType)V2->getValue());
553     return ConstantClass::get(*Ty, Result);
554   }
555 };
556
557 ConstRules &ConstRules::get(const Constant &V1, const Constant &V2) {
558   static EmptyRules       EmptyR;
559   static BoolRules        BoolR;
560   static NullPointerRules NullPointerR;
561   static DirectIntRules<ConstantSInt,   signed char , &Type::SByteTy>  SByteR;
562   static DirectIntRules<ConstantUInt, unsigned char , &Type::UByteTy>  UByteR;
563   static DirectIntRules<ConstantSInt,   signed short, &Type::ShortTy>  ShortR;
564   static DirectIntRules<ConstantUInt, unsigned short, &Type::UShortTy> UShortR;
565   static DirectIntRules<ConstantSInt,   signed int  , &Type::IntTy>    IntR;
566   static DirectIntRules<ConstantUInt, unsigned int  , &Type::UIntTy>   UIntR;
567   static DirectIntRules<ConstantSInt,  int64_t      , &Type::LongTy>   LongR;
568   static DirectIntRules<ConstantUInt, uint64_t      , &Type::ULongTy>  ULongR;
569   static DirectFPRules <ConstantFP  , float         , &Type::FloatTy>  FloatR;
570   static DirectFPRules <ConstantFP  , double        , &Type::DoubleTy> DoubleR;
571
572   if (isa<ConstantExpr>(V1) || isa<ConstantExpr>(V2) ||
573       isa<ConstantPointerRef>(V1) || isa<ConstantPointerRef>(V2))
574     return EmptyR;
575
576   // FIXME: This assert doesn't work because shifts pass both operands in to
577   // check for constant exprs.  :(
578   //assert(V1.getType() == V2.getType() &&"Nonequal types to constant folder?");
579
580   switch (V1.getType()->getPrimitiveID()) {
581   default: assert(0 && "Unknown value type for constant folding!");
582   case Type::BoolTyID:    return BoolR;
583   case Type::PointerTyID: return NullPointerR;
584   case Type::SByteTyID:   return SByteR;
585   case Type::UByteTyID:   return UByteR;
586   case Type::ShortTyID:   return ShortR;
587   case Type::UShortTyID:  return UShortR;
588   case Type::IntTyID:     return IntR;
589   case Type::UIntTyID:    return UIntR;
590   case Type::LongTyID:    return LongR;
591   case Type::ULongTyID:   return ULongR;
592   case Type::FloatTyID:   return FloatR;
593   case Type::DoubleTyID:  return DoubleR;
594   }
595 }