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