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"
20 AnnotationID ConstRules::AID(AnnotationManager::getID("opt::ConstRules",
23 // ConstantFoldInstruction - Attempt to constant fold the specified instruction.
24 // If successful, the constant result is returned, if not, null is returned.
26 Constant *ConstantFoldInstruction(Instruction *I) {
27 if (PHINode *PN = dyn_cast<PHINode>(I)) {
28 if (PN->getNumIncomingValues() == 0)
29 return Constant::getNullValue(PN->getType());
31 Constant *Result = dyn_cast<Constant>(PN->getIncomingValue(0));
32 if (Result == 0) return 0;
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...
39 // If we reach here, all incoming values are the same constant.
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
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
56 if (isa<BinaryOperator>(I))
57 return ConstantExpr::get(I->getOpcode(), Op0, Op1);
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)))
73 return 0; // Non-constant operand
74 return ConstantExpr::getGetElementPtr(Op0, IdxList);
81 static unsigned getSize(const Type *Ty) {
82 unsigned S = Ty->getPrimitiveSize();
83 return S ? S : 8; // Treat pointers at 8 bytes
86 Constant *ConstantFoldCastInstruction(const Constant *V, const Type *DestTy) {
87 if (V->getType() == DestTy) return (Constant*)V;
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)
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);
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()) {
115 return ConstantExpr::getCast(CE->getOperand(0), DestTy);
118 return ConstRules::get(*V, *V)->castTo(V, DestTy);
121 Constant *ConstantFoldBinaryInstruction(unsigned Opcode, const Constant *V1,
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 *ConstantFoldShiftInstruction(unsigned Opcode, const Constant *V1,
144 const Constant *V2) {
146 case Instruction::Shl: return *V1 << *V2;
147 case Instruction::Shr: return *V1 >> *V2;
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);
158 // TODO If C is null and all idx's are null, return null of the right type.
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.
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)));
173 // Add the last index of the source with the first index of the new GEP.
175 ConstantExpr::get(Instruction::Add, IdxList[0],
176 CE->getOperand(CE->getNumOperands()-1));
178 NewIndices.push_back(Combined);
179 NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end());
180 return ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices);
184 // Implement folding of:
185 // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
187 // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
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);
204 //===----------------------------------------------------------------------===//
205 // TemplateRules Class
206 //===----------------------------------------------------------------------===//
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...
213 // This class also provides subclasses with typesafe implementations of methods
214 // so that don't have to do type casting.
216 template<class ArgType, class SubClassName>
217 class TemplateRules : public ConstRules {
219 //===--------------------------------------------------------------------===//
220 // Redirecting functions that cast to the appropriate types
221 //===--------------------------------------------------------------------===//
223 virtual Constant *add(const Constant *V1, const Constant *V2) const {
224 return SubClassName::Add((const ArgType *)V1, (const ArgType *)V2);
226 virtual Constant *sub(const Constant *V1, const Constant *V2) const {
227 return SubClassName::Sub((const ArgType *)V1, (const ArgType *)V2);
229 virtual Constant *mul(const Constant *V1, const Constant *V2) const {
230 return SubClassName::Mul((const ArgType *)V1, (const ArgType *)V2);
232 virtual Constant *div(const Constant *V1, const Constant *V2) const {
233 return SubClassName::Div((const ArgType *)V1, (const ArgType *)V2);
235 virtual Constant *rem(const Constant *V1, const Constant *V2) const {
236 return SubClassName::Rem((const ArgType *)V1, (const ArgType *)V2);
238 virtual Constant *op_and(const Constant *V1, const Constant *V2) const {
239 return SubClassName::And((const ArgType *)V1, (const ArgType *)V2);
241 virtual Constant *op_or(const Constant *V1, const Constant *V2) const {
242 return SubClassName::Or((const ArgType *)V1, (const ArgType *)V2);
244 virtual Constant *op_xor(const Constant *V1, const Constant *V2) const {
245 return SubClassName::Xor((const ArgType *)V1, (const ArgType *)V2);
247 virtual Constant *shl(const Constant *V1, const Constant *V2) const {
248 return SubClassName::Shl((const ArgType *)V1, (const ArgType *)V2);
250 virtual Constant *shr(const Constant *V1, const Constant *V2) const {
251 return SubClassName::Shr((const ArgType *)V1, (const ArgType *)V2);
254 virtual ConstantBool *lessthan(const Constant *V1,
255 const Constant *V2) const {
256 return SubClassName::LessThan((const ArgType *)V1, (const ArgType *)V2);
259 // Casting operators. ick
260 virtual ConstantBool *castToBool(const Constant *V) const {
261 return SubClassName::CastToBool((const ArgType*)V);
263 virtual ConstantSInt *castToSByte(const Constant *V) const {
264 return SubClassName::CastToSByte((const ArgType*)V);
266 virtual ConstantUInt *castToUByte(const Constant *V) const {
267 return SubClassName::CastToUByte((const ArgType*)V);
269 virtual ConstantSInt *castToShort(const Constant *V) const {
270 return SubClassName::CastToShort((const ArgType*)V);
272 virtual ConstantUInt *castToUShort(const Constant *V) const {
273 return SubClassName::CastToUShort((const ArgType*)V);
275 virtual ConstantSInt *castToInt(const Constant *V) const {
276 return SubClassName::CastToInt((const ArgType*)V);
278 virtual ConstantUInt *castToUInt(const Constant *V) const {
279 return SubClassName::CastToUInt((const ArgType*)V);
281 virtual ConstantSInt *castToLong(const Constant *V) const {
282 return SubClassName::CastToLong((const ArgType*)V);
284 virtual ConstantUInt *castToULong(const Constant *V) const {
285 return SubClassName::CastToULong((const ArgType*)V);
287 virtual ConstantFP *castToFloat(const Constant *V) const {
288 return SubClassName::CastToFloat((const ArgType*)V);
290 virtual ConstantFP *castToDouble(const Constant *V) const {
291 return SubClassName::CastToDouble((const ArgType*)V);
293 virtual Constant *castToPointer(const Constant *V,
294 const PointerType *Ty) const {
295 return SubClassName::CastToPointer((const ArgType*)V, Ty);
298 //===--------------------------------------------------------------------===//
299 // Default "noop" implementations
300 //===--------------------------------------------------------------------===//
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) {
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;}
334 //===----------------------------------------------------------------------===//
336 //===----------------------------------------------------------------------===//
338 // EmptyRules provides a concrete base class of ConstRules that does nothing
340 struct EmptyRules : public TemplateRules<Constant, EmptyRules> {
345 //===----------------------------------------------------------------------===//
347 //===----------------------------------------------------------------------===//
349 // BoolRules provides a concrete base class of ConstRules for the 'bool' type.
351 struct BoolRules : public TemplateRules<ConstantBool, BoolRules> {
353 static ConstantBool *LessThan(const ConstantBool *V1, const ConstantBool *V2){
354 return ConstantBool::get(V1->getValue() < V2->getValue());
357 static Constant *And(const ConstantBool *V1, const ConstantBool *V2) {
358 return ConstantBool::get(V1->getValue() & V2->getValue());
361 static Constant *Or(const ConstantBool *V1, const ConstantBool *V2) {
362 return ConstantBool::get(V1->getValue() | V2->getValue());
365 static Constant *Xor(const ConstantBool *V1, const ConstantBool *V2) {
366 return ConstantBool::get(V1->getValue() ^ V2->getValue());
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()); \
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)
390 //===----------------------------------------------------------------------===//
391 // PointerRules Class
392 //===----------------------------------------------------------------------===//
394 // PointerRules provides a concrete base class of ConstRules for pointer types
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
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
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
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
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
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
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
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
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
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
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
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
453 //===----------------------------------------------------------------------===//
455 //===----------------------------------------------------------------------===//
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.
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);
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);
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);
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);
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);
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
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()); \
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)
518 //===----------------------------------------------------------------------===//
519 // DirectIntRules Class
520 //===----------------------------------------------------------------------===//
522 // DirectIntRules provides implementations of functions that are valid on
523 // integer types, but not all types in general.
525 template <class ConstantClass, class BuiltinType, Type **Ty>
526 struct DirectIntRules
527 : public DirectRules<ConstantClass, BuiltinType, Ty,
528 DirectIntRules<ConstantClass, BuiltinType, Ty> > {
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())
535 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
536 return ConstantClass::get(*Ty, R);
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())
545 BuiltinType R = (BuiltinType)V1->getValue() % (BuiltinType)V2->getValue();
546 return ConstantClass::get(*Ty, R);
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);
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);
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);
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);
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);
574 //===----------------------------------------------------------------------===//
575 // DirectFPRules Class
576 //===----------------------------------------------------------------------===//
578 // DirectFPRules provides implementations of functions that are valid on
579 // floating point types, but not all types in general.
581 template <class ConstantClass, class BuiltinType, Type **Ty>
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);
593 //===----------------------------------------------------------------------===//
594 // DirectRules Subclasses
595 //===----------------------------------------------------------------------===//
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 :)
602 // ConstRules::find - Return the constant rules that take care of the specified
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);
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>();
621 return new DirectIntRules<ConstantSInt, signed int , &Type::IntTy>();
623 return new DirectIntRules<ConstantUInt, unsigned int , &Type::UIntTy>();
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>();
633 return new EmptyRules();
637 ConstRules *ConstRules::getConstantExprRules() {
638 static EmptyRules CERules;