-//===----------------------------------------------------------------------===//
-// DirectRules Subclasses
-//===----------------------------------------------------------------------===//
-//
-// Given the DirectRules class we can now implement lots of types with little
-// code. Thank goodness C++ compilers are great at stomping out layers of
-// templates... can you imagine having to do this all by hand? (/me is lazy :)
-//
-
-// ConstRules::find - Return the constant rules that take care of the specified
-// type.
-//
-Annotation *ConstRules::find(AnnotationID AID, const Annotable *TyA, void *) {
- assert(AID == ConstRules::AID && "Bad annotation for factory!");
- const Type *Ty = cast<Type>((const Value*)TyA);
-
- switch (Ty->getPrimitiveID()) {
- case Type::BoolTyID: return new BoolRules();
- case Type::PointerTyID: return new PointerRules();
- case Type::SByteTyID:
- return new DirectIntRules<ConstantSInt, signed char , &Type::SByteTy>();
- case Type::UByteTyID:
- return new DirectIntRules<ConstantUInt, unsigned char , &Type::UByteTy>();
- case Type::ShortTyID:
- return new DirectIntRules<ConstantSInt, signed short, &Type::ShortTy>();
- case Type::UShortTyID:
- return new DirectIntRules<ConstantUInt, unsigned short, &Type::UShortTy>();
- case Type::IntTyID:
- return new DirectIntRules<ConstantSInt, signed int , &Type::IntTy>();
- case Type::UIntTyID:
- return new DirectIntRules<ConstantUInt, unsigned int , &Type::UIntTy>();
- case Type::LongTyID:
- return new DirectIntRules<ConstantSInt, int64_t , &Type::LongTy>();
- case Type::ULongTyID:
- return new DirectIntRules<ConstantUInt, uint64_t , &Type::ULongTy>();
- case Type::FloatTyID:
- return new DirectFPRules<ConstantFP , float , &Type::FloatTy>();
- case Type::DoubleTyID:
- return new DirectFPRules<ConstantFP , double , &Type::DoubleTy>();
- default:
- return new EmptyRules();
+ConstRules &ConstRules::get(const Constant &V1, const Constant &V2) {
+ static EmptyRules EmptyR;
+ static BoolRules BoolR;
+ static NullPointerRules NullPointerR;
+ static DirectIntRules<ConstantSInt, signed char , &Type::SByteTy> SByteR;
+ static DirectIntRules<ConstantUInt, unsigned char , &Type::UByteTy> UByteR;
+ static DirectIntRules<ConstantSInt, signed short, &Type::ShortTy> ShortR;
+ static DirectIntRules<ConstantUInt, unsigned short, &Type::UShortTy> UShortR;
+ static DirectIntRules<ConstantSInt, signed int , &Type::IntTy> IntR;
+ static DirectIntRules<ConstantUInt, unsigned int , &Type::UIntTy> UIntR;
+ static DirectIntRules<ConstantSInt, int64_t , &Type::LongTy> LongR;
+ static DirectIntRules<ConstantUInt, uint64_t , &Type::ULongTy> ULongR;
+ static DirectFPRules <ConstantFP , float , &Type::FloatTy> FloatR;
+ static DirectFPRules <ConstantFP , double , &Type::DoubleTy> DoubleR;
+
+ if (isa<ConstantExpr>(V1) || isa<ConstantExpr>(V2) ||
+ isa<ConstantPointerRef>(V1) || isa<ConstantPointerRef>(V2))
+ return EmptyR;
+
+ // FIXME: This assert doesn't work because shifts pass both operands in to
+ // check for constant exprs. :(
+ //assert(V1.getType() == V2.getType() &&"Nonequal types to constant folder?");
+
+ switch (V1.getType()->getPrimitiveID()) {
+ default: assert(0 && "Unknown value type for constant folding!");
+ case Type::BoolTyID: return BoolR;
+ case Type::PointerTyID: return NullPointerR;
+ case Type::SByteTyID: return SByteR;
+ case Type::UByteTyID: return UByteR;
+ case Type::ShortTyID: return ShortR;
+ case Type::UShortTyID: return UShortR;
+ case Type::IntTyID: return IntR;
+ case Type::UIntTyID: return UIntR;
+ case Type::LongTyID: return LongR;
+ case Type::ULongTyID: return ULongR;
+ case Type::FloatTyID: return FloatR;
+ case Type::DoubleTyID: return DoubleR;