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