1 //===- ConstantHandling.cpp - Implement ConstantHandling.h ----------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the various intrinsic operations, on constant values.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ConstantHandling.h"
15 #include "llvm/iPHINode.h"
16 #include "llvm/InstrTypes.h"
17 #include "llvm/DerivedTypes.h"
21 // ConstantFoldInstruction - Attempt to constant fold the specified instruction.
22 // If successful, the constant result is returned, if not, null is returned.
24 Constant *llvm::ConstantFoldInstruction(Instruction *I) {
25 if (PHINode *PN = dyn_cast<PHINode>(I)) {
26 if (PN->getNumIncomingValues() == 0)
27 return Constant::getNullValue(PN->getType());
29 Constant *Result = dyn_cast<Constant>(PN->getIncomingValue(0));
30 if (Result == 0) return 0;
32 // Handle PHI nodes specially here...
33 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i)
34 if (PN->getIncomingValue(i) != Result)
35 return 0; // Not all the same incoming constants...
37 // If we reach here, all incoming values are the same constant.
44 if (I->getNumOperands() != 0) { // Get first operand if it's a constant...
45 Op0 = dyn_cast<Constant>(I->getOperand(0));
46 if (Op0 == 0) return 0; // Not a constant?, can't fold
48 if (I->getNumOperands() != 1) { // Get second operand if it's a constant...
49 Op1 = dyn_cast<Constant>(I->getOperand(1));
50 if (Op1 == 0) return 0; // Not a constant?, can't fold
54 if (isa<BinaryOperator>(I))
55 return ConstantExpr::get(I->getOpcode(), Op0, Op1);
57 switch (I->getOpcode()) {
58 case Instruction::Cast:
59 return ConstantExpr::getCast(Op0, I->getType());
60 case Instruction::Shl:
61 case Instruction::Shr:
62 return ConstantExpr::getShift(I->getOpcode(), Op0, Op1);
63 case Instruction::GetElementPtr: {
64 std::vector<Constant*> IdxList;
65 IdxList.reserve(I->getNumOperands()-1);
66 if (Op1) IdxList.push_back(Op1);
67 for (unsigned i = 2, e = I->getNumOperands(); i != e; ++i)
68 if (Constant *C = dyn_cast<Constant>(I->getOperand(i)))
71 return 0; // Non-constant operand
72 return ConstantExpr::getGetElementPtr(Op0, IdxList);
79 static unsigned getSize(const Type *Ty) {
80 unsigned S = Ty->getPrimitiveSize();
81 return S ? S : 8; // Treat pointers at 8 bytes
84 Constant *llvm::ConstantFoldCastInstruction(const Constant *V,
86 if (V->getType() == DestTy) return (Constant*)V;
88 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
89 if (CE->getOpcode() == Instruction::Cast) {
90 Constant *Op = const_cast<Constant*>(CE->getOperand(0));
91 // Try to not produce a cast of a cast, which is almost always redundant.
92 if (!Op->getType()->isFloatingPoint() &&
93 !CE->getType()->isFloatingPoint() &&
94 !DestTy->getType()->isFloatingPoint()) {
95 unsigned S1 = getSize(Op->getType()), S2 = getSize(CE->getType());
96 unsigned S3 = getSize(DestTy);
97 if (Op->getType() == DestTy && S3 >= S2)
99 if (S1 >= S2 && S2 >= S3)
100 return ConstantExpr::getCast(Op, DestTy);
101 if (S1 <= S2 && S2 >= S3 && S1 <= S3)
102 return ConstantExpr::getCast(Op, DestTy);
104 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
105 // If all of the indexes in the GEP are null values, there is no pointer
106 // adjustment going on. We might as well cast the source pointer.
107 bool isAllNull = true;
108 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
109 if (!CE->getOperand(i)->isNullValue()) {
114 return ConstantExpr::getCast(CE->getOperand(0), DestTy);
117 return ConstRules::get(*V, *V).castTo(V, DestTy);
120 Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
122 const Constant *V2) {
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;
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;
143 Constant *llvm::ConstantFoldShiftInstruction(unsigned Opcode,
145 const Constant *V2) {
147 case Instruction::Shl: return *V1 << *V2;
148 case Instruction::Shr: return *V1 >> *V2;
153 Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
154 const std::vector<Constant*> &IdxList) {
155 if (IdxList.size() == 0 ||
156 (IdxList.size() == 1 && IdxList[0]->isNullValue()))
157 return const_cast<Constant*>(C);
159 // TODO If C is null and all idx's are null, return null of the right type.
162 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
163 // Combine Indices - If the source pointer to this getelementptr instruction
164 // is a getelementptr instruction, combine the indices of the two
165 // getelementptr instructions into a single instruction.
167 if (CE->getOpcode() == Instruction::GetElementPtr) {
168 if (CE->getOperand(CE->getNumOperands()-1)->getType() == Type::LongTy) {
169 std::vector<Constant*> NewIndices;
170 NewIndices.reserve(IdxList.size() + CE->getNumOperands());
171 for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
172 NewIndices.push_back(cast<Constant>(CE->getOperand(i)));
174 // Add the last index of the source with the first index of the new GEP.
176 ConstantExpr::get(Instruction::Add, IdxList[0],
177 CE->getOperand(CE->getNumOperands()-1));
179 NewIndices.push_back(Combined);
180 NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end());
181 return ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices);
185 // Implement folding of:
186 // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
188 // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
190 if (CE->getOpcode() == Instruction::Cast && IdxList.size() > 1 &&
191 IdxList[0]->isNullValue())
192 if (const PointerType *SPT =
193 dyn_cast<PointerType>(CE->getOperand(0)->getType()))
194 if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
195 if (const ArrayType *CAT =
196 dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
197 if (CAT->getElementType() == SAT->getElementType())
198 return ConstantExpr::getGetElementPtr(
199 (Constant*)CE->getOperand(0), IdxList);
205 //===----------------------------------------------------------------------===//
206 // TemplateRules Class
207 //===----------------------------------------------------------------------===//
209 // TemplateRules - Implement a subclass of ConstRules that provides all
210 // operations as noops. All other rules classes inherit from this class so
211 // that if functionality is needed in the future, it can simply be added here
212 // and to ConstRules without changing anything else...
214 // This class also provides subclasses with typesafe implementations of methods
215 // so that don't have to do type casting.
217 template<class ArgType, class SubClassName>
218 class TemplateRules : public ConstRules {
220 //===--------------------------------------------------------------------===//
221 // Redirecting functions that cast to the appropriate types
222 //===--------------------------------------------------------------------===//
224 virtual Constant *add(const Constant *V1, const Constant *V2) const {
225 return SubClassName::Add((const ArgType *)V1, (const ArgType *)V2);
227 virtual Constant *sub(const Constant *V1, const Constant *V2) const {
228 return SubClassName::Sub((const ArgType *)V1, (const ArgType *)V2);
230 virtual Constant *mul(const Constant *V1, const Constant *V2) const {
231 return SubClassName::Mul((const ArgType *)V1, (const ArgType *)V2);
233 virtual Constant *div(const Constant *V1, const Constant *V2) const {
234 return SubClassName::Div((const ArgType *)V1, (const ArgType *)V2);
236 virtual Constant *rem(const Constant *V1, const Constant *V2) const {
237 return SubClassName::Rem((const ArgType *)V1, (const ArgType *)V2);
239 virtual Constant *op_and(const Constant *V1, const Constant *V2) const {
240 return SubClassName::And((const ArgType *)V1, (const ArgType *)V2);
242 virtual Constant *op_or(const Constant *V1, const Constant *V2) const {
243 return SubClassName::Or((const ArgType *)V1, (const ArgType *)V2);
245 virtual Constant *op_xor(const Constant *V1, const Constant *V2) const {
246 return SubClassName::Xor((const ArgType *)V1, (const ArgType *)V2);
248 virtual Constant *shl(const Constant *V1, const Constant *V2) const {
249 return SubClassName::Shl((const ArgType *)V1, (const ArgType *)V2);
251 virtual Constant *shr(const Constant *V1, const Constant *V2) const {
252 return SubClassName::Shr((const ArgType *)V1, (const ArgType *)V2);
255 virtual ConstantBool *lessthan(const Constant *V1,
256 const Constant *V2) const {
257 return SubClassName::LessThan((const ArgType *)V1, (const ArgType *)V2);
260 // Casting operators. ick
261 virtual ConstantBool *castToBool(const Constant *V) const {
262 return SubClassName::CastToBool((const ArgType*)V);
264 virtual ConstantSInt *castToSByte(const Constant *V) const {
265 return SubClassName::CastToSByte((const ArgType*)V);
267 virtual ConstantUInt *castToUByte(const Constant *V) const {
268 return SubClassName::CastToUByte((const ArgType*)V);
270 virtual ConstantSInt *castToShort(const Constant *V) const {
271 return SubClassName::CastToShort((const ArgType*)V);
273 virtual ConstantUInt *castToUShort(const Constant *V) const {
274 return SubClassName::CastToUShort((const ArgType*)V);
276 virtual ConstantSInt *castToInt(const Constant *V) const {
277 return SubClassName::CastToInt((const ArgType*)V);
279 virtual ConstantUInt *castToUInt(const Constant *V) const {
280 return SubClassName::CastToUInt((const ArgType*)V);
282 virtual ConstantSInt *castToLong(const Constant *V) const {
283 return SubClassName::CastToLong((const ArgType*)V);
285 virtual ConstantUInt *castToULong(const Constant *V) const {
286 return SubClassName::CastToULong((const ArgType*)V);
288 virtual ConstantFP *castToFloat(const Constant *V) const {
289 return SubClassName::CastToFloat((const ArgType*)V);
291 virtual ConstantFP *castToDouble(const Constant *V) const {
292 return SubClassName::CastToDouble((const ArgType*)V);
294 virtual Constant *castToPointer(const Constant *V,
295 const PointerType *Ty) const {
296 return SubClassName::CastToPointer((const ArgType*)V, Ty);
299 //===--------------------------------------------------------------------===//
300 // Default "noop" implementations
301 //===--------------------------------------------------------------------===//
303 static Constant *Add(const ArgType *V1, const ArgType *V2) { return 0; }
304 static Constant *Sub(const ArgType *V1, const ArgType *V2) { return 0; }
305 static Constant *Mul(const ArgType *V1, const ArgType *V2) { return 0; }
306 static Constant *Div(const ArgType *V1, const ArgType *V2) { return 0; }
307 static Constant *Rem(const ArgType *V1, const ArgType *V2) { return 0; }
308 static Constant *And(const ArgType *V1, const ArgType *V2) { return 0; }
309 static Constant *Or (const ArgType *V1, const ArgType *V2) { return 0; }
310 static Constant *Xor(const ArgType *V1, const ArgType *V2) { return 0; }
311 static Constant *Shl(const ArgType *V1, const ArgType *V2) { return 0; }
312 static Constant *Shr(const ArgType *V1, const ArgType *V2) { return 0; }
313 static ConstantBool *LessThan(const ArgType *V1, const ArgType *V2) {
317 // Casting operators. ick
318 static ConstantBool *CastToBool (const Constant *V) { return 0; }
319 static ConstantSInt *CastToSByte (const Constant *V) { return 0; }
320 static ConstantUInt *CastToUByte (const Constant *V) { return 0; }
321 static ConstantSInt *CastToShort (const Constant *V) { return 0; }
322 static ConstantUInt *CastToUShort(const Constant *V) { return 0; }
323 static ConstantSInt *CastToInt (const Constant *V) { return 0; }
324 static ConstantUInt *CastToUInt (const Constant *V) { return 0; }
325 static ConstantSInt *CastToLong (const Constant *V) { return 0; }
326 static ConstantUInt *CastToULong (const Constant *V) { return 0; }
327 static ConstantFP *CastToFloat (const Constant *V) { return 0; }
328 static ConstantFP *CastToDouble(const Constant *V) { return 0; }
329 static Constant *CastToPointer(const Constant *,
330 const PointerType *) {return 0;}
335 //===----------------------------------------------------------------------===//
337 //===----------------------------------------------------------------------===//
339 // EmptyRules provides a concrete base class of ConstRules that does nothing
341 struct EmptyRules : public TemplateRules<Constant, EmptyRules> {
346 //===----------------------------------------------------------------------===//
348 //===----------------------------------------------------------------------===//
350 // BoolRules provides a concrete base class of ConstRules for the 'bool' type.
352 struct BoolRules : public TemplateRules<ConstantBool, BoolRules> {
354 static ConstantBool *LessThan(const ConstantBool *V1, const ConstantBool *V2){
355 return ConstantBool::get(V1->getValue() < V2->getValue());
358 static Constant *And(const ConstantBool *V1, const ConstantBool *V2) {
359 return ConstantBool::get(V1->getValue() & V2->getValue());
362 static Constant *Or(const ConstantBool *V1, const ConstantBool *V2) {
363 return ConstantBool::get(V1->getValue() | V2->getValue());
366 static Constant *Xor(const ConstantBool *V1, const ConstantBool *V2) {
367 return ConstantBool::get(V1->getValue() ^ V2->getValue());
370 // Casting operators. ick
371 #define DEF_CAST(TYPE, CLASS, CTYPE) \
372 static CLASS *CastTo##TYPE (const ConstantBool *V) { \
373 return CLASS::get(Type::TYPE##Ty, (CTYPE)(bool)V->getValue()); \
376 DEF_CAST(Bool , ConstantBool, bool)
377 DEF_CAST(SByte , ConstantSInt, signed char)
378 DEF_CAST(UByte , ConstantUInt, unsigned char)
379 DEF_CAST(Short , ConstantSInt, signed short)
380 DEF_CAST(UShort, ConstantUInt, unsigned short)
381 DEF_CAST(Int , ConstantSInt, signed int)
382 DEF_CAST(UInt , ConstantUInt, unsigned int)
383 DEF_CAST(Long , ConstantSInt, int64_t)
384 DEF_CAST(ULong , ConstantUInt, uint64_t)
385 DEF_CAST(Float , ConstantFP , float)
386 DEF_CAST(Double, ConstantFP , double)
391 //===----------------------------------------------------------------------===//
392 // PointerRules Class
393 //===----------------------------------------------------------------------===//
395 // PointerRules provides a concrete base class of ConstRules for pointer types
397 struct PointerRules : public TemplateRules<ConstantPointer, PointerRules> {
398 static ConstantBool *CastToBool (const Constant *V) {
399 if (V->isNullValue()) return ConstantBool::False;
400 return 0; // Can't const prop other types of pointers
402 static ConstantSInt *CastToSByte (const Constant *V) {
403 if (V->isNullValue()) return ConstantSInt::get(Type::SByteTy, 0);
404 return 0; // Can't const prop other types of pointers
406 static ConstantUInt *CastToUByte (const Constant *V) {
407 if (V->isNullValue()) return ConstantUInt::get(Type::UByteTy, 0);
408 return 0; // Can't const prop other types of pointers
410 static ConstantSInt *CastToShort (const Constant *V) {
411 if (V->isNullValue()) return ConstantSInt::get(Type::ShortTy, 0);
412 return 0; // Can't const prop other types of pointers
414 static ConstantUInt *CastToUShort(const Constant *V) {
415 if (V->isNullValue()) return ConstantUInt::get(Type::UShortTy, 0);
416 return 0; // Can't const prop other types of pointers
418 static ConstantSInt *CastToInt (const Constant *V) {
419 if (V->isNullValue()) return ConstantSInt::get(Type::IntTy, 0);
420 return 0; // Can't const prop other types of pointers
422 static ConstantUInt *CastToUInt (const Constant *V) {
423 if (V->isNullValue()) return ConstantUInt::get(Type::UIntTy, 0);
424 return 0; // Can't const prop other types of pointers
426 static ConstantSInt *CastToLong (const Constant *V) {
427 if (V->isNullValue()) return ConstantSInt::get(Type::LongTy, 0);
428 return 0; // Can't const prop other types of pointers
430 static ConstantUInt *CastToULong (const Constant *V) {
431 if (V->isNullValue()) return ConstantUInt::get(Type::ULongTy, 0);
432 return 0; // Can't const prop other types of pointers
434 static ConstantFP *CastToFloat (const Constant *V) {
435 if (V->isNullValue()) return ConstantFP::get(Type::FloatTy, 0);
436 return 0; // Can't const prop other types of pointers
438 static ConstantFP *CastToDouble(const Constant *V) {
439 if (V->isNullValue()) return ConstantFP::get(Type::DoubleTy, 0);
440 return 0; // Can't const prop other types of pointers
443 static Constant *CastToPointer(const ConstantPointer *V,
444 const PointerType *PTy) {
445 if (V->getType() == PTy)
446 return const_cast<ConstantPointer*>(V); // Allow cast %PTy %ptr to %PTy
447 if (V->isNullValue())
448 return ConstantPointerNull::get(PTy);
449 return 0; // Can't const prop other types of pointers
454 //===----------------------------------------------------------------------===//
456 //===----------------------------------------------------------------------===//
458 // DirectRules provides a concrete base classes of ConstRules for a variety of
459 // different types. This allows the C++ compiler to automatically generate our
460 // constant handling operations in a typesafe and accurate manner.
462 template<class ConstantClass, class BuiltinType, Type **Ty, class SuperClass>
463 struct DirectRules : public TemplateRules<ConstantClass, SuperClass> {
464 static Constant *Add(const ConstantClass *V1, const ConstantClass *V2) {
465 BuiltinType R = (BuiltinType)V1->getValue() + (BuiltinType)V2->getValue();
466 return ConstantClass::get(*Ty, R);
469 static Constant *Sub(const ConstantClass *V1, const ConstantClass *V2) {
470 BuiltinType R = (BuiltinType)V1->getValue() - (BuiltinType)V2->getValue();
471 return ConstantClass::get(*Ty, R);
474 static Constant *Mul(const ConstantClass *V1, const ConstantClass *V2) {
475 BuiltinType R = (BuiltinType)V1->getValue() * (BuiltinType)V2->getValue();
476 return ConstantClass::get(*Ty, R);
479 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
480 if (V2->isNullValue()) return 0;
481 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
482 return ConstantClass::get(*Ty, R);
485 static ConstantBool *LessThan(const ConstantClass *V1,
486 const ConstantClass *V2) {
487 bool R = (BuiltinType)V1->getValue() < (BuiltinType)V2->getValue();
488 return ConstantBool::get(R);
491 static Constant *CastToPointer(const ConstantClass *V,
492 const PointerType *PTy) {
493 if (V->isNullValue()) // Is it a FP or Integral null value?
494 return ConstantPointerNull::get(PTy);
495 return 0; // Can't const prop other types of pointers
498 // Casting operators. ick
499 #define DEF_CAST(TYPE, CLASS, CTYPE) \
500 static CLASS *CastTo##TYPE (const ConstantClass *V) { \
501 return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getValue()); \
504 DEF_CAST(Bool , ConstantBool, bool)
505 DEF_CAST(SByte , ConstantSInt, signed char)
506 DEF_CAST(UByte , ConstantUInt, unsigned char)
507 DEF_CAST(Short , ConstantSInt, signed short)
508 DEF_CAST(UShort, ConstantUInt, unsigned short)
509 DEF_CAST(Int , ConstantSInt, signed int)
510 DEF_CAST(UInt , ConstantUInt, unsigned int)
511 DEF_CAST(Long , ConstantSInt, int64_t)
512 DEF_CAST(ULong , ConstantUInt, uint64_t)
513 DEF_CAST(Float , ConstantFP , float)
514 DEF_CAST(Double, ConstantFP , double)
519 //===----------------------------------------------------------------------===//
520 // DirectIntRules Class
521 //===----------------------------------------------------------------------===//
523 // DirectIntRules provides implementations of functions that are valid on
524 // integer types, but not all types in general.
526 template <class ConstantClass, class BuiltinType, Type **Ty>
527 struct DirectIntRules
528 : public DirectRules<ConstantClass, BuiltinType, Ty,
529 DirectIntRules<ConstantClass, BuiltinType, Ty> > {
531 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
532 if (V2->isNullValue()) return 0;
533 if (V2->isAllOnesValue() && // MIN_INT / -1
534 (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
536 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
537 return ConstantClass::get(*Ty, R);
540 static Constant *Rem(const ConstantClass *V1,
541 const ConstantClass *V2) {
542 if (V2->isNullValue()) return 0; // X / 0
543 if (V2->isAllOnesValue() && // MIN_INT / -1
544 (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
546 BuiltinType R = (BuiltinType)V1->getValue() % (BuiltinType)V2->getValue();
547 return ConstantClass::get(*Ty, R);
550 static Constant *And(const ConstantClass *V1, const ConstantClass *V2) {
551 BuiltinType R = (BuiltinType)V1->getValue() & (BuiltinType)V2->getValue();
552 return ConstantClass::get(*Ty, R);
554 static Constant *Or(const ConstantClass *V1, const ConstantClass *V2) {
555 BuiltinType R = (BuiltinType)V1->getValue() | (BuiltinType)V2->getValue();
556 return ConstantClass::get(*Ty, R);
558 static Constant *Xor(const ConstantClass *V1, const ConstantClass *V2) {
559 BuiltinType R = (BuiltinType)V1->getValue() ^ (BuiltinType)V2->getValue();
560 return ConstantClass::get(*Ty, R);
563 static Constant *Shl(const ConstantClass *V1, const ConstantClass *V2) {
564 BuiltinType R = (BuiltinType)V1->getValue() << (BuiltinType)V2->getValue();
565 return ConstantClass::get(*Ty, R);
568 static Constant *Shr(const ConstantClass *V1, const ConstantClass *V2) {
569 BuiltinType R = (BuiltinType)V1->getValue() >> (BuiltinType)V2->getValue();
570 return ConstantClass::get(*Ty, R);
575 //===----------------------------------------------------------------------===//
576 // DirectFPRules Class
577 //===----------------------------------------------------------------------===//
579 // DirectFPRules provides implementations of functions that are valid on
580 // floating point types, but not all types in general.
582 template <class ConstantClass, class BuiltinType, Type **Ty>
584 : public DirectRules<ConstantClass, BuiltinType, Ty,
585 DirectFPRules<ConstantClass, BuiltinType, Ty> > {
586 static Constant *Rem(const ConstantClass *V1, const ConstantClass *V2) {
587 if (V2->isNullValue()) return 0;
588 BuiltinType Result = std::fmod((BuiltinType)V1->getValue(),
589 (BuiltinType)V2->getValue());
590 return ConstantClass::get(*Ty, Result);
594 ConstRules &ConstRules::get(const Constant &V1, const Constant &V2) {
595 static EmptyRules EmptyR;
596 static BoolRules BoolR;
597 static PointerRules PointerR;
598 static DirectIntRules<ConstantSInt, signed char , &Type::SByteTy> SByteR;
599 static DirectIntRules<ConstantUInt, unsigned char , &Type::UByteTy> UByteR;
600 static DirectIntRules<ConstantSInt, signed short, &Type::ShortTy> ShortR;
601 static DirectIntRules<ConstantUInt, unsigned short, &Type::UShortTy> UShortR;
602 static DirectIntRules<ConstantSInt, signed int , &Type::IntTy> IntR;
603 static DirectIntRules<ConstantUInt, unsigned int , &Type::UIntTy> UIntR;
604 static DirectIntRules<ConstantSInt, int64_t , &Type::LongTy> LongR;
605 static DirectIntRules<ConstantUInt, uint64_t , &Type::ULongTy> ULongR;
606 static DirectFPRules <ConstantFP , float , &Type::FloatTy> FloatR;
607 static DirectFPRules <ConstantFP , double , &Type::DoubleTy> DoubleR;
609 if (isa<ConstantExpr>(V1) || isa<ConstantExpr>(V2))
612 // FIXME: This assert doesn't work because shifts pass both operands in to
613 // check for constant exprs. :(
614 //assert(V1.getType() == V2.getType() &&"Nonequal types to constant folder?");
616 switch (V1.getType()->getPrimitiveID()) {
617 default: assert(0 && "Unknown value type for constant folding!");
618 case Type::BoolTyID: return BoolR;
619 case Type::PointerTyID: return PointerR;
620 case Type::SByteTyID: return SByteR;
621 case Type::UByteTyID: return UByteR;
622 case Type::ShortTyID: return ShortR;
623 case Type::UShortTyID: return UShortR;
624 case Type::IntTyID: return IntR;
625 case Type::UIntTyID: return UIntR;
626 case Type::LongTyID: return LongR;
627 case Type::ULongTyID: return ULongR;
628 case Type::FloatTyID: return FloatR;
629 case Type::DoubleTyID: return DoubleR;