X-Git-Url: http://plrg.eecs.uci.edu/git/?p=oota-llvm.git;a=blobdiff_plain;f=lib%2FVMCore%2FConstantFold.cpp;h=ad9a33f845eda22fbfa5f2bcc7c878a13c19be69;hp=4ac5292190ce49a015d3351094a4293a06f54c45;hb=b83eb6447ba155342598f0fabe1f08f5baa9164a;hpb=84e1064f4584c20b839b9172c5c26c20b6ffb2fe diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp index 4ac5292190c..ad9a33f845e 100644 --- a/lib/VMCore/ConstantFold.cpp +++ b/lib/VMCore/ConstantFold.cpp @@ -23,13 +23,16 @@ #include "llvm/Instructions.h" #include "llvm/DerivedTypes.h" #include "llvm/Function.h" +#include "llvm/Support/Compiler.h" #include "llvm/Support/GetElementPtrTypeIterator.h" +#include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/MathExtras.h" #include #include using namespace llvm; namespace { - struct ConstRules { + struct VISIBILITY_HIDDEN ConstRules { ConstRules() {} virtual ~ConstRules() {} @@ -85,8 +88,9 @@ namespace { // This class also provides subclasses with typesafe implementations of methods // so that don't have to do type casting. // +namespace { template -class TemplateRules : public ConstRules { +class VISIBILITY_HIDDEN TemplateRules : public ConstRules { //===--------------------------------------------------------------------===// @@ -209,7 +213,7 @@ class TemplateRules : public ConstRules { public: virtual ~TemplateRules() {} }; - +} // end anonymous namespace //===----------------------------------------------------------------------===// @@ -218,12 +222,15 @@ public: // // EmptyRules provides a concrete base class of ConstRules that does nothing // -struct EmptyRules : public TemplateRules { +namespace { +struct VISIBILITY_HIDDEN EmptyRules + : public TemplateRules { static Constant *EqualTo(const Constant *V1, const Constant *V2) { - if (V1 == V2) return ConstantBool::True; + if (V1 == V2) return ConstantBool::getTrue(); return 0; } }; +} // end anonymous namespace @@ -233,9 +240,11 @@ struct EmptyRules : public TemplateRules { // // BoolRules provides a concrete base class of ConstRules for the 'bool' type. // -struct BoolRules : public TemplateRules { +namespace { +struct VISIBILITY_HIDDEN BoolRules + : public TemplateRules { - static Constant *LessThan(const ConstantBool *V1, const ConstantBool *V2){ + static Constant *LessThan(const ConstantBool *V1, const ConstantBool *V2) { return ConstantBool::get(V1->getValue() < V2->getValue()); } @@ -262,18 +271,19 @@ struct BoolRules : public TemplateRules { } DEF_CAST(Bool , ConstantBool, bool) - DEF_CAST(SByte , ConstantSInt, signed char) - DEF_CAST(UByte , ConstantUInt, unsigned char) - DEF_CAST(Short , ConstantSInt, signed short) - DEF_CAST(UShort, ConstantUInt, unsigned short) - DEF_CAST(Int , ConstantSInt, signed int) - DEF_CAST(UInt , ConstantUInt, unsigned int) - DEF_CAST(Long , ConstantSInt, int64_t) - DEF_CAST(ULong , ConstantUInt, uint64_t) + DEF_CAST(SByte , ConstantInt, signed char) + DEF_CAST(UByte , ConstantInt, unsigned char) + DEF_CAST(Short , ConstantInt, signed short) + DEF_CAST(UShort, ConstantInt, unsigned short) + DEF_CAST(Int , ConstantInt, signed int) + DEF_CAST(UInt , ConstantInt, unsigned int) + DEF_CAST(Long , ConstantInt, int64_t) + DEF_CAST(ULong , ConstantInt, uint64_t) DEF_CAST(Float , ConstantFP , float) DEF_CAST(Double, ConstantFP , double) #undef DEF_CAST }; +} // end anonymous namespace //===----------------------------------------------------------------------===// @@ -283,37 +293,38 @@ struct BoolRules : public TemplateRules { // NullPointerRules provides a concrete base class of ConstRules for null // pointers. // -struct NullPointerRules : public TemplateRules { +namespace { +struct VISIBILITY_HIDDEN NullPointerRules + : public TemplateRules { static Constant *EqualTo(const Constant *V1, const Constant *V2) { - return ConstantBool::True; // Null pointers are always equal + return ConstantBool::getTrue(); // Null pointers are always equal } static Constant *CastToBool(const Constant *V) { - return ConstantBool::False; + return ConstantBool::getFalse(); } static Constant *CastToSByte (const Constant *V) { - return ConstantSInt::get(Type::SByteTy, 0); + return ConstantInt::get(Type::SByteTy, 0); } static Constant *CastToUByte (const Constant *V) { - return ConstantUInt::get(Type::UByteTy, 0); + return ConstantInt::get(Type::UByteTy, 0); } static Constant *CastToShort (const Constant *V) { - return ConstantSInt::get(Type::ShortTy, 0); + return ConstantInt::get(Type::ShortTy, 0); } static Constant *CastToUShort(const Constant *V) { - return ConstantUInt::get(Type::UShortTy, 0); + return ConstantInt::get(Type::UShortTy, 0); } static Constant *CastToInt (const Constant *V) { - return ConstantSInt::get(Type::IntTy, 0); + return ConstantInt::get(Type::IntTy, 0); } static Constant *CastToUInt (const Constant *V) { - return ConstantUInt::get(Type::UIntTy, 0); + return ConstantInt::get(Type::UIntTy, 0); } static Constant *CastToLong (const Constant *V) { - return ConstantSInt::get(Type::LongTy, 0); + return ConstantInt::get(Type::LongTy, 0); } static Constant *CastToULong (const Constant *V) { - return ConstantUInt::get(Type::ULongTy, 0); + return ConstantInt::get(Type::ULongTy, 0); } static Constant *CastToFloat (const Constant *V) { return ConstantFP::get(Type::FloatTy, 0); @@ -327,6 +338,7 @@ struct NullPointerRules : public TemplateRules { static Constant *Add(const ConstantPacked *V1, const ConstantPacked *V2) { @@ -396,6 +409,7 @@ struct ConstantPackedRules return 0; } }; +} // end anonymous namespace //===----------------------------------------------------------------------===// @@ -406,52 +420,54 @@ struct ConstantPackedRules /// PackedType operands, where both operands are not ConstantPacked. The usual /// cause for this is that one operand is a ConstantAggregateZero. /// -struct GeneralPackedRules : public TemplateRules { +namespace { +struct VISIBILITY_HIDDEN GeneralPackedRules + : public TemplateRules { }; +} // end anonymous namespace //===----------------------------------------------------------------------===// -// DirectRules Class +// DirectIntRules Class //===----------------------------------------------------------------------===// // -// DirectRules provides a concrete base classes of ConstRules for a variety of -// different types. This allows the C++ compiler to automatically generate our -// constant handling operations in a typesafe and accurate manner. +// DirectIntRules provides implementations of functions that are valid on +// integer types, but not all types in general. // -template -struct DirectRules : public TemplateRules { - static Constant *Add(const ConstantClass *V1, const ConstantClass *V2) { - BuiltinType R = (BuiltinType)V1->getValue() + (BuiltinType)V2->getValue(); - return ConstantClass::get(*Ty, R); - } +namespace { +template +struct VISIBILITY_HIDDEN DirectIntRules + : public TemplateRules > { - static Constant *Sub(const ConstantClass *V1, const ConstantClass *V2) { - BuiltinType R = (BuiltinType)V1->getValue() - (BuiltinType)V2->getValue(); - return ConstantClass::get(*Ty, R); + static Constant *Add(const ConstantInt *V1, const ConstantInt *V2) { + BuiltinType R = (BuiltinType)V1->getZExtValue() + + (BuiltinType)V2->getZExtValue(); + return ConstantInt::get(*Ty, R); } - static Constant *Mul(const ConstantClass *V1, const ConstantClass *V2) { - BuiltinType R = (BuiltinType)V1->getValue() * (BuiltinType)V2->getValue(); - return ConstantClass::get(*Ty, R); + static Constant *Sub(const ConstantInt *V1, const ConstantInt *V2) { + BuiltinType R = (BuiltinType)V1->getZExtValue() - + (BuiltinType)V2->getZExtValue(); + return ConstantInt::get(*Ty, R); } - static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) { - if (V2->isNullValue()) return 0; - BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue(); - return ConstantClass::get(*Ty, R); + static Constant *Mul(const ConstantInt *V1, const ConstantInt *V2) { + BuiltinType R = (BuiltinType)V1->getZExtValue() * + (BuiltinType)V2->getZExtValue(); + return ConstantInt::get(*Ty, R); } - static Constant *LessThan(const ConstantClass *V1, const ConstantClass *V2) { - bool R = (BuiltinType)V1->getValue() < (BuiltinType)V2->getValue(); + static Constant *LessThan(const ConstantInt *V1, const ConstantInt *V2) { + bool R = (BuiltinType)V1->getZExtValue() < (BuiltinType)V2->getZExtValue(); return ConstantBool::get(R); } - static Constant *EqualTo(const ConstantClass *V1, const ConstantClass *V2) { - bool R = (BuiltinType)V1->getValue() == (BuiltinType)V2->getValue(); + static Constant *EqualTo(const ConstantInt *V1, const ConstantInt *V2) { + bool R = (BuiltinType)V1->getZExtValue() == (BuiltinType)V2->getZExtValue(); return ConstantBool::get(R); } - static Constant *CastToPointer(const ConstantClass *V, + static Constant *CastToPointer(const ConstantInt *V, const PointerType *PTy) { if (V->isNullValue()) // Is it a FP or Integral null value? return ConstantPointerNull::get(PTy); @@ -460,79 +476,73 @@ struct DirectRules : public TemplateRules { // Casting operators. ick #define DEF_CAST(TYPE, CLASS, CTYPE) \ - static Constant *CastTo##TYPE (const ConstantClass *V) { \ - return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getValue()); \ + static Constant *CastTo##TYPE (const ConstantInt *V) { \ + return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getZExtValue()); \ } DEF_CAST(Bool , ConstantBool, bool) - DEF_CAST(SByte , ConstantSInt, signed char) - DEF_CAST(UByte , ConstantUInt, unsigned char) - DEF_CAST(Short , ConstantSInt, signed short) - DEF_CAST(UShort, ConstantUInt, unsigned short) - DEF_CAST(Int , ConstantSInt, signed int) - DEF_CAST(UInt , ConstantUInt, unsigned int) - DEF_CAST(Long , ConstantSInt, int64_t) - DEF_CAST(ULong , ConstantUInt, uint64_t) - DEF_CAST(Float , ConstantFP , float) - DEF_CAST(Double, ConstantFP , double) + DEF_CAST(SByte , ConstantInt, signed char) + DEF_CAST(UByte , ConstantInt, unsigned char) + DEF_CAST(Short , ConstantInt, signed short) + DEF_CAST(UShort, ConstantInt, unsigned short) + DEF_CAST(Int , ConstantInt, signed int) + DEF_CAST(UInt , ConstantInt, unsigned int) + DEF_CAST(Long , ConstantInt, int64_t) + DEF_CAST(ULong , ConstantInt, uint64_t) + DEF_CAST(Float , ConstantFP , float) + DEF_CAST(Double, ConstantFP , double) #undef DEF_CAST -}; - -//===----------------------------------------------------------------------===// -// DirectIntRules Class -//===----------------------------------------------------------------------===// -// -// DirectIntRules provides implementations of functions that are valid on -// integer types, but not all types in general. -// -template -struct DirectIntRules - : public DirectRules > { - - static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) { + static Constant *Div(const ConstantInt *V1, const ConstantInt *V2) { if (V2->isNullValue()) return 0; if (V2->isAllOnesValue() && // MIN_INT / -1 - (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue()) + (BuiltinType)V1->getZExtValue() == -(BuiltinType)V1->getZExtValue()) return 0; - BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue(); - return ConstantClass::get(*Ty, R); + BuiltinType R = + (BuiltinType)V1->getZExtValue() / (BuiltinType)V2->getZExtValue(); + return ConstantInt::get(*Ty, R); } - static Constant *Rem(const ConstantClass *V1, - const ConstantClass *V2) { + static Constant *Rem(const ConstantInt *V1, + const ConstantInt *V2) { if (V2->isNullValue()) return 0; // X / 0 if (V2->isAllOnesValue() && // MIN_INT / -1 - (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue()) + (BuiltinType)V1->getZExtValue() == -(BuiltinType)V1->getZExtValue()) return 0; - BuiltinType R = (BuiltinType)V1->getValue() % (BuiltinType)V2->getValue(); - return ConstantClass::get(*Ty, R); + BuiltinType R = + (BuiltinType)V1->getZExtValue() % (BuiltinType)V2->getZExtValue(); + return ConstantInt::get(*Ty, R); } - static Constant *And(const ConstantClass *V1, const ConstantClass *V2) { - BuiltinType R = (BuiltinType)V1->getValue() & (BuiltinType)V2->getValue(); - return ConstantClass::get(*Ty, R); + static Constant *And(const ConstantInt *V1, const ConstantInt *V2) { + BuiltinType R = + (BuiltinType)V1->getZExtValue() & (BuiltinType)V2->getZExtValue(); + return ConstantInt::get(*Ty, R); } - static Constant *Or(const ConstantClass *V1, const ConstantClass *V2) { - BuiltinType R = (BuiltinType)V1->getValue() | (BuiltinType)V2->getValue(); - return ConstantClass::get(*Ty, R); + static Constant *Or(const ConstantInt *V1, const ConstantInt *V2) { + BuiltinType R = + (BuiltinType)V1->getZExtValue() | (BuiltinType)V2->getZExtValue(); + return ConstantInt::get(*Ty, R); } - static Constant *Xor(const ConstantClass *V1, const ConstantClass *V2) { - BuiltinType R = (BuiltinType)V1->getValue() ^ (BuiltinType)V2->getValue(); - return ConstantClass::get(*Ty, R); + static Constant *Xor(const ConstantInt *V1, const ConstantInt *V2) { + BuiltinType R = + (BuiltinType)V1->getZExtValue() ^ (BuiltinType)V2->getZExtValue(); + return ConstantInt::get(*Ty, R); } - static Constant *Shl(const ConstantClass *V1, const ConstantClass *V2) { - BuiltinType R = (BuiltinType)V1->getValue() << (BuiltinType)V2->getValue(); - return ConstantClass::get(*Ty, R); + static Constant *Shl(const ConstantInt *V1, const ConstantInt *V2) { + BuiltinType R = + (BuiltinType)V1->getZExtValue() << (BuiltinType)V2->getZExtValue(); + return ConstantInt::get(*Ty, R); } - static Constant *Shr(const ConstantClass *V1, const ConstantClass *V2) { - BuiltinType R = (BuiltinType)V1->getValue() >> (BuiltinType)V2->getValue(); - return ConstantClass::get(*Ty, R); + static Constant *Shr(const ConstantInt *V1, const ConstantInt *V2) { + BuiltinType R = + (BuiltinType)V1->getZExtValue() >> (BuiltinType)V2->getZExtValue(); + return ConstantInt::get(*Ty, R); } }; +} // end anonymous namespace //===----------------------------------------------------------------------===// @@ -542,68 +552,121 @@ struct DirectIntRules /// DirectFPRules provides implementations of functions that are valid on /// floating point types, but not all types in general. /// -template -struct DirectFPRules - : public DirectRules > { - static Constant *Rem(const ConstantClass *V1, const ConstantClass *V2) { +namespace { +template +struct VISIBILITY_HIDDEN DirectFPRules + : public TemplateRules > { + + static Constant *Add(const ConstantFP *V1, const ConstantFP *V2) { + BuiltinType R = (BuiltinType)V1->getValue() + + (BuiltinType)V2->getValue(); + return ConstantFP::get(*Ty, R); + } + + static Constant *Sub(const ConstantFP *V1, const ConstantFP *V2) { + BuiltinType R = (BuiltinType)V1->getValue() - (BuiltinType)V2->getValue(); + return ConstantFP::get(*Ty, R); + } + + static Constant *Mul(const ConstantFP *V1, const ConstantFP *V2) { + BuiltinType R = (BuiltinType)V1->getValue() * (BuiltinType)V2->getValue(); + return ConstantFP::get(*Ty, R); + } + + static Constant *LessThan(const ConstantFP *V1, const ConstantFP *V2) { + bool R = (BuiltinType)V1->getValue() < (BuiltinType)V2->getValue(); + return ConstantBool::get(R); + } + + static Constant *EqualTo(const ConstantFP *V1, const ConstantFP *V2) { + bool R = (BuiltinType)V1->getValue() == (BuiltinType)V2->getValue(); + return ConstantBool::get(R); + } + + static Constant *CastToPointer(const ConstantFP *V, + const PointerType *PTy) { + if (V->isNullValue()) // Is it a FP or Integral null value? + return ConstantPointerNull::get(PTy); + return 0; // Can't const prop other types of pointers + } + + // Casting operators. ick +#define DEF_CAST(TYPE, CLASS, CTYPE) \ + static Constant *CastTo##TYPE (const ConstantFP *V) { \ + return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getValue()); \ + } + + DEF_CAST(Bool , ConstantBool, bool) + DEF_CAST(SByte , ConstantInt, signed char) + DEF_CAST(UByte , ConstantInt, unsigned char) + DEF_CAST(Short , ConstantInt, signed short) + DEF_CAST(UShort, ConstantInt, unsigned short) + DEF_CAST(Int , ConstantInt, signed int) + DEF_CAST(UInt , ConstantInt, unsigned int) + DEF_CAST(Long , ConstantInt, int64_t) + DEF_CAST(ULong , ConstantInt, uint64_t) + DEF_CAST(Float , ConstantFP , float) + DEF_CAST(Double, ConstantFP , double) +#undef DEF_CAST + + static Constant *Rem(const ConstantFP *V1, const ConstantFP *V2) { if (V2->isNullValue()) return 0; BuiltinType Result = std::fmod((BuiltinType)V1->getValue(), (BuiltinType)V2->getValue()); - return ConstantClass::get(*Ty, Result); + return ConstantFP::get(*Ty, Result); } - static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) { + static Constant *Div(const ConstantFP *V1, const ConstantFP *V2) { BuiltinType inf = std::numeric_limits::infinity(); - if (V2->isExactlyValue(0.0)) return ConstantClass::get(*Ty, inf); - if (V2->isExactlyValue(-0.0)) return ConstantClass::get(*Ty, -inf); + if (V2->isExactlyValue(0.0)) return ConstantFP::get(*Ty, inf); + if (V2->isExactlyValue(-0.0)) return ConstantFP::get(*Ty, -inf); BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue(); - return ConstantClass::get(*Ty, R); + return ConstantFP::get(*Ty, R); } }; - +} // end anonymous namespace + +static ManagedStatic EmptyR; +static ManagedStatic BoolR; +static ManagedStatic NullPointerR; +static ManagedStatic ConstantPackedR; +static ManagedStatic GeneralPackedR; +static ManagedStatic > SByteR; +static ManagedStatic > UByteR; +static ManagedStatic > ShortR; +static ManagedStatic > UShortR; +static ManagedStatic > IntR; +static ManagedStatic > UIntR; +static ManagedStatic > LongR; +static ManagedStatic > ULongR; +static ManagedStatic > FloatR; +static ManagedStatic > DoubleR; /// ConstRules::get - This method returns the constant rules implementation that /// implements the semantics of the two specified constants. ConstRules &ConstRules::get(const Constant *V1, const Constant *V2) { - static EmptyRules EmptyR; - static BoolRules BoolR; - static NullPointerRules NullPointerR; - static ConstantPackedRules ConstantPackedR; - static GeneralPackedRules GeneralPackedR; - static DirectIntRules SByteR; - static DirectIntRules UByteR; - static DirectIntRules ShortR; - static DirectIntRules UShortR; - static DirectIntRules IntR; - static DirectIntRules UIntR; - static DirectIntRules LongR; - static DirectIntRules ULongR; - static DirectFPRules FloatR; - static DirectFPRules DoubleR; - if (isa(V1) || isa(V2) || isa(V1) || isa(V2) || isa(V1) || isa(V2)) - return EmptyR; + return *EmptyR; switch (V1->getType()->getTypeID()) { 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; + 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; case Type::PackedTyID: if (isa(V1) && isa(V2)) - return ConstantPackedR; - return GeneralPackedR; // Constant folding rules for ConstantAggregateZero. + return *ConstantPackedR; + return *GeneralPackedR; // Constant folding rules for ConstantAggregateZero. } } @@ -621,6 +684,81 @@ static unsigned getSize(const Type *Ty) { return S ? S : 8; // Treat pointers at 8 bytes } +/// CastConstantPacked - Convert the specified ConstantPacked node to the +/// specified packed type. At this point, we know that the elements of the +/// input packed constant are all simple integer or FP values. +static Constant *CastConstantPacked(ConstantPacked *CP, + const PackedType *DstTy) { + unsigned SrcNumElts = CP->getType()->getNumElements(); + unsigned DstNumElts = DstTy->getNumElements(); + const Type *SrcEltTy = CP->getType()->getElementType(); + const Type *DstEltTy = DstTy->getElementType(); + + // If both vectors have the same number of elements (thus, the elements + // are the same size), perform the conversion now. + if (SrcNumElts == DstNumElts) { + std::vector Result; + + // If the src and dest elements are both integers, just cast each one + // which will do the appropriate bit-convert. + if (SrcEltTy->isIntegral() && DstEltTy->isIntegral()) { + for (unsigned i = 0; i != SrcNumElts; ++i) + Result.push_back(ConstantExpr::getCast(CP->getOperand(i), + DstEltTy)); + return ConstantPacked::get(Result); + } + + if (SrcEltTy->isIntegral()) { + // Otherwise, this is an int-to-fp cast. + assert(DstEltTy->isFloatingPoint()); + if (DstEltTy->getTypeID() == Type::DoubleTyID) { + for (unsigned i = 0; i != SrcNumElts; ++i) { + double V = + BitsToDouble(cast(CP->getOperand(i))->getZExtValue()); + Result.push_back(ConstantFP::get(Type::DoubleTy, V)); + } + return ConstantPacked::get(Result); + } + assert(DstEltTy == Type::FloatTy && "Unknown fp type!"); + for (unsigned i = 0; i != SrcNumElts; ++i) { + float V = + BitsToFloat(cast(CP->getOperand(i))->getZExtValue()); + Result.push_back(ConstantFP::get(Type::FloatTy, V)); + } + return ConstantPacked::get(Result); + } + + // Otherwise, this is an fp-to-int cast. + assert(SrcEltTy->isFloatingPoint() && DstEltTy->isIntegral()); + + if (SrcEltTy->getTypeID() == Type::DoubleTyID) { + for (unsigned i = 0; i != SrcNumElts; ++i) { + uint64_t V = + DoubleToBits(cast(CP->getOperand(i))->getValue()); + Constant *C = ConstantInt::get(Type::ULongTy, V); + Result.push_back(ConstantExpr::getCast(C, DstEltTy)); + } + return ConstantPacked::get(Result); + } + + assert(SrcEltTy->getTypeID() == Type::FloatTyID); + for (unsigned i = 0; i != SrcNumElts; ++i) { + uint32_t V = FloatToBits(cast(CP->getOperand(i))->getValue()); + Constant *C = ConstantInt::get(Type::UIntTy, V); + Result.push_back(ConstantExpr::getCast(C, DstEltTy)); + } + return ConstantPacked::get(Result); + } + + // Otherwise, this is a cast that changes element count and size. Handle + // casts which shrink the elements here. + + // FIXME: We need to know endianness to do this! + + return 0; +} + + Constant *llvm::ConstantFoldCastInstruction(const Constant *V, const Type *DestTy) { if (V->getType() == DestTy) return (Constant*)V; @@ -631,7 +769,7 @@ Constant *llvm::ConstantFoldCastInstruction(const Constant *V, // FIXME: When we support 'external weak' references, we have to prevent // this transformation from happening. This code will need to be updated // to ignore external weak symbols when we support it. - return ConstantBool::True; + return ConstantBool::getTrue(); } else if (const ConstantExpr *CE = dyn_cast(V)) { if (CE->getOpcode() == Instruction::Cast) { Constant *Op = const_cast(CE->getOperand(0)); @@ -688,6 +826,38 @@ Constant *llvm::ConstantFoldCastInstruction(const Constant *V, if (ElTy == DPTy->getElementType()) return ConstantExpr::getGetElementPtr(const_cast(V),IdxList); } + + // Handle casts from one packed constant to another. We know that the src and + // dest type have the same size. + if (const PackedType *DestPTy = dyn_cast(DestTy)) { + if (const PackedType *SrcTy = dyn_cast(V->getType())) { + assert(DestPTy->getElementType()->getPrimitiveSizeInBits() * + DestPTy->getNumElements() == + SrcTy->getElementType()->getPrimitiveSizeInBits() * + SrcTy->getNumElements() && "Not cast between same sized vectors!"); + if (isa(V)) + return Constant::getNullValue(DestTy); + if (isa(V)) + return UndefValue::get(DestTy); + if (const ConstantPacked *CP = dyn_cast(V)) { + // This is a cast from a ConstantPacked of one type to a ConstantPacked + // of another type. Check to see if all elements of the input are + // simple. + bool AllSimpleConstants = true; + for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) { + if (!isa(CP->getOperand(i)) && + !isa(CP->getOperand(i))) { + AllSimpleConstants = false; + break; + } + } + + // If all of the elements are simple constants, we can fold this. + if (AllSimpleConstants) + return CastConstantPacked(const_cast(CP), DestPTy); + } + } + } ConstRules &Rules = ConstRules::get(V, V); @@ -712,17 +882,100 @@ Constant *llvm::ConstantFoldCastInstruction(const Constant *V, Constant *llvm::ConstantFoldSelectInstruction(const Constant *Cond, const Constant *V1, const Constant *V2) { - if (Cond == ConstantBool::True) - return const_cast(V1); - else if (Cond == ConstantBool::False) - return const_cast(V2); + if (const ConstantBool *CB = dyn_cast(Cond)) + return const_cast(CB->getValue() ? V1 : V2); if (isa(V1)) return const_cast(V2); if (isa(V2)) return const_cast(V1); if (isa(Cond)) return const_cast(V1); + if (V1 == V2) return const_cast(V1); + return 0; +} + +Constant *llvm::ConstantFoldExtractElementInstruction(const Constant *Val, + const Constant *Idx) { + if (isa(Val)) // ee(undef, x) -> undef + return UndefValue::get(cast(Val->getType())->getElementType()); + if (Val->isNullValue()) // ee(zero, x) -> zero + return Constant::getNullValue( + cast(Val->getType())->getElementType()); + + if (const ConstantPacked *CVal = dyn_cast(Val)) { + if (const ConstantInt *CIdx = dyn_cast(Idx)) { + return const_cast(CVal->getOperand(CIdx->getZExtValue())); + } else if (isa(Idx)) { + // ee({w,x,y,z}, undef) -> w (an arbitrary value). + return const_cast(CVal->getOperand(0)); + } + } + return 0; +} + +Constant *llvm::ConstantFoldInsertElementInstruction(const Constant *Val, + const Constant *Elt, + const Constant *Idx) { + const ConstantInt *CIdx = dyn_cast(Idx); + if (!CIdx) return 0; + uint64_t idxVal = CIdx->getZExtValue(); + if (const UndefValue *UVal = dyn_cast(Val)) { + // Insertion of scalar constant into packed undef + // Optimize away insertion of undef + if (isa(Elt)) + return const_cast(Val); + // Otherwise break the aggregate undef into multiple undefs and do + // the insertion + unsigned numOps = + cast(Val->getType())->getNumElements(); + std::vector Ops; + Ops.reserve(numOps); + for (unsigned i = 0; i < numOps; ++i) { + const Constant *Op = + (i == idxVal) ? Elt : UndefValue::get(Elt->getType()); + Ops.push_back(const_cast(Op)); + } + return ConstantPacked::get(Ops); + } + if (const ConstantAggregateZero *CVal = + dyn_cast(Val)) { + // Insertion of scalar constant into packed aggregate zero + // Optimize away insertion of zero + if (Elt->isNullValue()) + return const_cast(Val); + // Otherwise break the aggregate zero into multiple zeros and do + // the insertion + unsigned numOps = + cast(Val->getType())->getNumElements(); + std::vector Ops; + Ops.reserve(numOps); + for (unsigned i = 0; i < numOps; ++i) { + const Constant *Op = + (i == idxVal) ? Elt : Constant::getNullValue(Elt->getType()); + Ops.push_back(const_cast(Op)); + } + return ConstantPacked::get(Ops); + } + if (const ConstantPacked *CVal = dyn_cast(Val)) { + // Insertion of scalar constant into packed constant + std::vector Ops; + Ops.reserve(CVal->getNumOperands()); + for (unsigned i = 0; i < CVal->getNumOperands(); ++i) { + const Constant *Op = + (i == idxVal) ? Elt : cast(CVal->getOperand(i)); + Ops.push_back(const_cast(Op)); + } + return ConstantPacked::get(Ops); + } return 0; } +Constant *llvm::ConstantFoldShuffleVectorInstruction(const Constant *V1, + const Constant *V2, + const Constant *Mask) { + // TODO: + return 0; +} + + /// isZeroSizedType - This type is zero sized if its an array or structure of /// zero sized types. The only leaf zero sized type is an empty structure. static bool isMaybeZeroSizedType(const Type *Ty) { @@ -768,7 +1021,8 @@ static int IdxCompare(Constant *C1, Constant *C2, const Type *ElTy) { // If they are really different, now that they are the same type, then we // found a difference! - if (cast(C1)->getValue() < cast(C2)->getValue()) + if (cast(C1)->getSExtValue() < + cast(C2)->getSExtValue()) return -1; else return 1; @@ -786,27 +1040,38 @@ static int IdxCompare(Constant *C1, Constant *C2, const Type *ElTy) { /// constants (like ConstantInt) to be the simplest, followed by /// GlobalValues, followed by ConstantExpr's (the most complex). /// -static Instruction::BinaryOps evaluateRelation(const Constant *V1, - const Constant *V2) { +static Instruction::BinaryOps evaluateRelation(Constant *V1, Constant *V2) { assert(V1->getType() == V2->getType() && "Cannot compare different types of values!"); if (V1 == V2) return Instruction::SetEQ; if (!isa(V1) && !isa(V1)) { + if (!isa(V2) && !isa(V2)) { + // We distilled this down to a simple case, use the standard constant + // folder. + ConstantBool *R = dyn_cast(ConstantExpr::getSetEQ(V1, V2)); + if (R && R->getValue()) return Instruction::SetEQ; + R = dyn_cast(ConstantExpr::getSetLT(V1, V2)); + if (R && R->getValue()) return Instruction::SetLT; + R = dyn_cast(ConstantExpr::getSetGT(V1, V2)); + if (R && R->getValue()) return Instruction::SetGT; + + // If we couldn't figure it out, bail. + return Instruction::BinaryOpsEnd; + } + // If the first operand is simple, swap operands. - assert((isa(V2) || isa(V2)) && - "Simple cases should have been handled by caller!"); Instruction::BinaryOps SwappedRelation = evaluateRelation(V2, V1); if (SwappedRelation != Instruction::BinaryOpsEnd) return SetCondInst::getSwappedCondition(SwappedRelation); - } else if (const GlobalValue *CPR1 = dyn_cast(V1)){ + } else if (const GlobalValue *CPR1 = dyn_cast(V1)) { if (isa(V2)) { // Swap as necessary. - Instruction::BinaryOps SwappedRelation = evaluateRelation(V2, V1); - if (SwappedRelation != Instruction::BinaryOpsEnd) - return SetCondInst::getSwappedCondition(SwappedRelation); - else - return Instruction::BinaryOpsEnd; + Instruction::BinaryOps SwappedRelation = evaluateRelation(V2, V1); + if (SwappedRelation != Instruction::BinaryOpsEnd) + return SetCondInst::getSwappedCondition(SwappedRelation); + else + return Instruction::BinaryOpsEnd; } // Now we know that the RHS is a GlobalValue or simple constant, @@ -826,7 +1091,7 @@ static Instruction::BinaryOps evaluateRelation(const Constant *V1, } else { // Ok, the LHS is known to be a constantexpr. The RHS can be any of a // constantexpr, a CPR, or a simple constant. - const ConstantExpr *CE1 = cast(V1); + ConstantExpr *CE1 = cast(V1); Constant *CE1Op0 = CE1->getOperand(0); switch (CE1->getOpcode()) { @@ -834,9 +1099,21 @@ static Instruction::BinaryOps evaluateRelation(const Constant *V1, // If the cast is not actually changing bits, and the second operand is a // null pointer, do the comparison with the pre-casted value. if (V2->isNullValue() && - CE1->getType()->isLosslesslyConvertibleTo(CE1Op0->getType())) + (isa(CE1->getType()) || CE1->getType()->isIntegral())) return evaluateRelation(CE1Op0, Constant::getNullValue(CE1Op0->getType())); + + // If the dest type is a pointer type, and the RHS is a constantexpr cast + // from the same type as the src of the LHS, evaluate the inputs. This is + // important for things like "seteq (cast 4 to int*), (cast 5 to int*)", + // which happens a lot in compilers with tagged integers. + if (ConstantExpr *CE2 = dyn_cast(V2)) + if (isa(CE1->getType()) && + CE2->getOpcode() == Instruction::Cast && + CE1->getOperand(0)->getType() == CE2->getOperand(0)->getType() && + CE1->getOperand(0)->getType()->isIntegral()) { + return evaluateRelation(CE1->getOperand(0), CE2->getOperand(0)); + } break; case Instruction::GetElementPtr: @@ -974,10 +1251,11 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, // If we successfully folded the expression, return it now. if (C) return C; - if (SetCondInst::isRelational(Opcode)) { + if (SetCondInst::isComparison(Opcode)) { if (isa(V1) || isa(V2)) return UndefValue::get(Type::BoolTy); - switch (evaluateRelation(V1, V2)) { + switch (evaluateRelation(const_cast(V1), + const_cast(V2))) { default: assert(0 && "Unknown relational!"); case Instruction::BinaryOpsEnd: break; // Couldn't determine anything about these constants. @@ -1001,20 +1279,20 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Opcode == Instruction::SetGE); case Instruction::SetLE: // If we know that V1 <= V2, we can only partially decide this relation. - if (Opcode == Instruction::SetGT) return ConstantBool::False; - if (Opcode == Instruction::SetLT) return ConstantBool::True; + if (Opcode == Instruction::SetGT) return ConstantBool::getFalse(); + if (Opcode == Instruction::SetLT) return ConstantBool::getTrue(); break; case Instruction::SetGE: // If we know that V1 >= V2, we can only partially decide this relation. - if (Opcode == Instruction::SetLT) return ConstantBool::False; - if (Opcode == Instruction::SetGT) return ConstantBool::True; + if (Opcode == Instruction::SetLT) return ConstantBool::getFalse(); + if (Opcode == Instruction::SetGT) return ConstantBool::getTrue(); break; case Instruction::SetNE: // If we know that V1 != V2, we can only partially decide this relation. - if (Opcode == Instruction::SetEQ) return ConstantBool::False; - if (Opcode == Instruction::SetNE) return ConstantBool::True; + if (Opcode == Instruction::SetEQ) return ConstantBool::getFalse(); + if (Opcode == Instruction::SetNE) return ConstantBool::getTrue(); break; } } @@ -1077,17 +1355,17 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, case Instruction::Mul: if (V2->isNullValue()) return const_cast(V2); // X * 0 == 0 if (const ConstantInt *CI = dyn_cast(V2)) - if (CI->getRawValue() == 1) + if (CI->getZExtValue() == 1) return const_cast(V1); // X * 1 == X break; case Instruction::Div: if (const ConstantInt *CI = dyn_cast(V2)) - if (CI->getRawValue() == 1) + if (CI->getZExtValue() == 1) return const_cast(V1); // X / 1 == X break; case Instruction::Rem: if (const ConstantInt *CI = dyn_cast(V2)) - if (CI->getRawValue() == 1) + if (CI->getZExtValue() == 1) return Constant::getNullValue(CI->getType()); // X % 1 == 0 break; case Instruction::And: @@ -1101,7 +1379,7 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, // Functions are at least 4-byte aligned. If and'ing the address of a // function with a constant < 4, fold it to zero. if (const ConstantInt *CI = dyn_cast(V2)) - if (CI->getRawValue() < 4 && isa(CPR)) + if (CI->getZExtValue() < 4 && isa(CPR)) return Constant::getNullValue(CI->getType()); } break; @@ -1180,10 +1458,10 @@ Constant *llvm::ConstantFoldGetElementPtr(const Constant *C, if (IdxList.size() == 1) { const Type *ElTy = cast(C->getType())->getElementType(); - if (unsigned ElSize = ElTy->getPrimitiveSize()) { + if (uint32_t ElSize = ElTy->getPrimitiveSize()) { // gep null, C is equal to C*sizeof(nullty). If nullty is a known llvm // type, we can statically fold this. - Constant *R = ConstantUInt::get(Type::UIntTy, ElSize); + Constant *R = ConstantInt::get(Type::UIntTy, ElSize); R = ConstantExpr::getCast(R, Idx0->getType()); R = ConstantExpr::getMul(R, Idx0); return ConstantExpr::getCast(R, C->getType()); @@ -1238,7 +1516,7 @@ Constant *llvm::ConstantFoldGetElementPtr(const Constant *C, dyn_cast(CE->getOperand(0)->getType())) if (const ArrayType *SAT = dyn_cast(SPT->getElementType())) if (const ArrayType *CAT = - dyn_cast(cast(C->getType())->getElementType())) + dyn_cast(cast(C->getType())->getElementType())) if (CAT->getElementType() == SAT->getElementType()) return ConstantExpr::getGetElementPtr( (Constant*)CE->getOperand(0), IdxList);