From 0dc396909e01c5fb27e2a5a192131a5d79c3f0e9 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Mon, 17 Nov 2003 19:05:17 +0000 Subject: [PATCH] Constant folding shalt not be built on annotations git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@10052 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/ConstantHandling.h | 47 ++++++-------- lib/VMCore/ConstantFold.cpp | 111 ++++++++++++++------------------ lib/VMCore/ConstantFold.h | 47 ++++++-------- lib/VMCore/ConstantFolding.h | 47 ++++++-------- 4 files changed, 103 insertions(+), 149 deletions(-) diff --git a/include/llvm/ConstantHandling.h b/include/llvm/ConstantHandling.h index b392ad11737..d733ac80962 100644 --- a/include/llvm/ConstantHandling.h +++ b/include/llvm/ConstantHandling.h @@ -64,11 +64,8 @@ inline ConstantBool *operator!=(const Constant &V1, const Constant &V2) { // Implement all other operators indirectly through TypeRules system //===----------------------------------------------------------------------===// -class ConstRules : public Annotation { -protected: - inline ConstRules() : Annotation(AID) {} // Can only be subclassed... -public: - static AnnotationID AID; // AnnotationID for this class +struct ConstRules { + ConstRules() {} // Binary Operators... virtual Constant *add(const Constant *V1, const Constant *V2) const = 0; @@ -119,19 +116,11 @@ public: } } - // ConstRules::get - A type will cache its own type rules if one is needed... - // we just want to make sure to hit the cache instead of doing it indirectly, - // if possible... + // ConstRules::get - Return an instance of ConstRules for the specified + // constant operands. // - static inline ConstRules *get(const Constant &V1, const Constant &V2) { - if (isa(V1) || isa(V2)) - return getConstantExprRules(); - return static_cast(V1.getType()->getOrCreateAnnotation(AID)); - } + static ConstRules &get(const Constant &V1, const Constant &V2); private: - static ConstRules *getConstantExprRules(); - static Annotation *find(AnnotationID AID, const Annotable *Ty, void *); - ConstRules(const ConstRules &); // Do not implement ConstRules &operator=(const ConstRules &); // Do not implement }; @@ -139,71 +128,71 @@ private: // Unary operators... inline Constant *operator~(const Constant &V) { assert(V.getType()->isIntegral() && "Cannot invert non-integral constant!"); - return ConstRules::get(V, V)->op_xor(&V, + return ConstRules::get(V, V).op_xor(&V, ConstantInt::getAllOnesValue(V.getType())); } inline Constant *operator-(const Constant &V) { - return ConstRules::get(V, V)->sub(Constant::getNullValue(V.getType()), &V); + return ConstRules::get(V, V).sub(Constant::getNullValue(V.getType()), &V); } // Standard binary operators... inline Constant *operator+(const Constant &V1, const Constant &V2) { assert(V1.getType() == V2.getType() && "Constant types must be identical!"); - return ConstRules::get(V1, V2)->add(&V1, &V2); + return ConstRules::get(V1, V2).add(&V1, &V2); } inline Constant *operator-(const Constant &V1, const Constant &V2) { assert(V1.getType() == V2.getType() && "Constant types must be identical!"); - return ConstRules::get(V1, V2)->sub(&V1, &V2); + return ConstRules::get(V1, V2).sub(&V1, &V2); } inline Constant *operator*(const Constant &V1, const Constant &V2) { assert(V1.getType() == V2.getType() && "Constant types must be identical!"); - return ConstRules::get(V1, V2)->mul(&V1, &V2); + return ConstRules::get(V1, V2).mul(&V1, &V2); } inline Constant *operator/(const Constant &V1, const Constant &V2) { assert(V1.getType() == V2.getType() && "Constant types must be identical!"); - return ConstRules::get(V1, V2)->div(&V1, &V2); + return ConstRules::get(V1, V2).div(&V1, &V2); } inline Constant *operator%(const Constant &V1, const Constant &V2) { assert(V1.getType() == V2.getType() && "Constant types must be identical!"); - return ConstRules::get(V1, V2)->rem(&V1, &V2); + return ConstRules::get(V1, V2).rem(&V1, &V2); } // Logical Operators... inline Constant *operator&(const Constant &V1, const Constant &V2) { assert(V1.getType() == V2.getType() && "Constant types must be identical!"); - return ConstRules::get(V1, V2)->op_and(&V1, &V2); + return ConstRules::get(V1, V2).op_and(&V1, &V2); } inline Constant *operator|(const Constant &V1, const Constant &V2) { assert(V1.getType() == V2.getType() && "Constant types must be identical!"); - return ConstRules::get(V1, V2)->op_or(&V1, &V2); + return ConstRules::get(V1, V2).op_or(&V1, &V2); } inline Constant *operator^(const Constant &V1, const Constant &V2) { assert(V1.getType() == V2.getType() && "Constant types must be identical!"); - return ConstRules::get(V1, V2)->op_xor(&V1, &V2); + return ConstRules::get(V1, V2).op_xor(&V1, &V2); } // Shift Instructions... inline Constant *operator<<(const Constant &V1, const Constant &V2) { assert(V1.getType()->isInteger() && V2.getType() == Type::UByteTy); - return ConstRules::get(V1, V2)->shl(&V1, &V2); + return ConstRules::get(V1, V2).shl(&V1, &V2); } inline Constant *operator>>(const Constant &V1, const Constant &V2) { assert(V1.getType()->isInteger() && V2.getType() == Type::UByteTy); - return ConstRules::get(V1, V2)->shr(&V1, &V2); + return ConstRules::get(V1, V2).shr(&V1, &V2); } inline ConstantBool *operator<(const Constant &V1, const Constant &V2) { assert(V1.getType() == V2.getType() && "Constant types must be identical!"); - return ConstRules::get(V1, V2)->lessthan(&V1, &V2); + return ConstRules::get(V1, V2).lessthan(&V1, &V2); } diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp index 7fb7a05e55d..aca7df25893 100644 --- a/lib/VMCore/ConstantFold.cpp +++ b/lib/VMCore/ConstantFold.cpp @@ -16,16 +16,12 @@ #include "llvm/InstrTypes.h" #include "llvm/DerivedTypes.h" #include - -namespace llvm { - -AnnotationID ConstRules::AID(AnnotationManager::getID("opt::ConstRules", - &ConstRules::find)); +using namespace llvm; // ConstantFoldInstruction - Attempt to constant fold the specified instruction. // If successful, the constant result is returned, if not, null is returned. // -Constant *ConstantFoldInstruction(Instruction *I) { +Constant *llvm::ConstantFoldInstruction(Instruction *I) { if (PHINode *PN = dyn_cast(I)) { if (PN->getNumIncomingValues() == 0) return Constant::getNullValue(PN->getType()); @@ -85,7 +81,8 @@ static unsigned getSize(const Type *Ty) { return S ? S : 8; // Treat pointers at 8 bytes } -Constant *ConstantFoldCastInstruction(const Constant *V, const Type *DestTy) { +Constant *llvm::ConstantFoldCastInstruction(const Constant *V, + const Type *DestTy) { if (V->getType() == DestTy) return (Constant*)V; if (const ConstantExpr *CE = dyn_cast(V)) @@ -117,11 +114,12 @@ Constant *ConstantFoldCastInstruction(const Constant *V, const Type *DestTy) { return ConstantExpr::getCast(CE->getOperand(0), DestTy); } - return ConstRules::get(*V, *V)->castTo(V, DestTy); + return ConstRules::get(*V, *V).castTo(V, DestTy); } -Constant *ConstantFoldBinaryInstruction(unsigned Opcode, const Constant *V1, - const Constant *V2) { +Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, + const Constant *V1, + const Constant *V2) { switch (Opcode) { case Instruction::Add: return *V1 + *V2; case Instruction::Sub: return *V1 - *V2; @@ -142,8 +140,9 @@ Constant *ConstantFoldBinaryInstruction(unsigned Opcode, const Constant *V1, return 0; } -Constant *ConstantFoldShiftInstruction(unsigned Opcode, const Constant *V1, - const Constant *V2) { +Constant *llvm::ConstantFoldShiftInstruction(unsigned Opcode, + const Constant *V1, + const Constant *V2) { switch (Opcode) { case Instruction::Shl: return *V1 << *V2; case Instruction::Shr: return *V1 >> *V2; @@ -151,8 +150,8 @@ Constant *ConstantFoldShiftInstruction(unsigned Opcode, const Constant *V1, } } -Constant *ConstantFoldGetElementPtr(const Constant *C, - const std::vector &IdxList) { +Constant *llvm::ConstantFoldGetElementPtr(const Constant *C, + const std::vector &IdxList) { if (IdxList.size() == 0 || (IdxList.size() == 1 && IdxList[0]->isNullValue())) return const_cast(C); @@ -592,53 +591,41 @@ struct DirectFPRules } }; -//===----------------------------------------------------------------------===// -// 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((const Value*)TyA); - - switch (Ty->getPrimitiveID()) { - case Type::BoolTyID: return new BoolRules(); - case Type::PointerTyID: return new PointerRules(); - case Type::SByteTyID: - return new DirectIntRules(); - case Type::UByteTyID: - return new DirectIntRules(); - case Type::ShortTyID: - return new DirectIntRules(); - case Type::UShortTyID: - return new DirectIntRules(); - case Type::IntTyID: - return new DirectIntRules(); - case Type::UIntTyID: - return new DirectIntRules(); - case Type::LongTyID: - return new DirectIntRules(); - case Type::ULongTyID: - return new DirectIntRules(); - case Type::FloatTyID: - return new DirectFPRules(); - case Type::DoubleTyID: - return new DirectFPRules(); - default: - return new EmptyRules(); +ConstRules &ConstRules::get(const Constant &V1, const Constant &V2) { + static EmptyRules EmptyR; + static BoolRules BoolR; + static PointerRules PointerR; + 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)) + 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 PointerR; + 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; } } - -ConstRules *ConstRules::getConstantExprRules() { - static EmptyRules CERules; - return &CERules; -} - -} // End llvm namespace diff --git a/lib/VMCore/ConstantFold.h b/lib/VMCore/ConstantFold.h index b392ad11737..d733ac80962 100644 --- a/lib/VMCore/ConstantFold.h +++ b/lib/VMCore/ConstantFold.h @@ -64,11 +64,8 @@ inline ConstantBool *operator!=(const Constant &V1, const Constant &V2) { // Implement all other operators indirectly through TypeRules system //===----------------------------------------------------------------------===// -class ConstRules : public Annotation { -protected: - inline ConstRules() : Annotation(AID) {} // Can only be subclassed... -public: - static AnnotationID AID; // AnnotationID for this class +struct ConstRules { + ConstRules() {} // Binary Operators... virtual Constant *add(const Constant *V1, const Constant *V2) const = 0; @@ -119,19 +116,11 @@ public: } } - // ConstRules::get - A type will cache its own type rules if one is needed... - // we just want to make sure to hit the cache instead of doing it indirectly, - // if possible... + // ConstRules::get - Return an instance of ConstRules for the specified + // constant operands. // - static inline ConstRules *get(const Constant &V1, const Constant &V2) { - if (isa(V1) || isa(V2)) - return getConstantExprRules(); - return static_cast(V1.getType()->getOrCreateAnnotation(AID)); - } + static ConstRules &get(const Constant &V1, const Constant &V2); private: - static ConstRules *getConstantExprRules(); - static Annotation *find(AnnotationID AID, const Annotable *Ty, void *); - ConstRules(const ConstRules &); // Do not implement ConstRules &operator=(const ConstRules &); // Do not implement }; @@ -139,71 +128,71 @@ private: // Unary operators... inline Constant *operator~(const Constant &V) { assert(V.getType()->isIntegral() && "Cannot invert non-integral constant!"); - return ConstRules::get(V, V)->op_xor(&V, + return ConstRules::get(V, V).op_xor(&V, ConstantInt::getAllOnesValue(V.getType())); } inline Constant *operator-(const Constant &V) { - return ConstRules::get(V, V)->sub(Constant::getNullValue(V.getType()), &V); + return ConstRules::get(V, V).sub(Constant::getNullValue(V.getType()), &V); } // Standard binary operators... inline Constant *operator+(const Constant &V1, const Constant &V2) { assert(V1.getType() == V2.getType() && "Constant types must be identical!"); - return ConstRules::get(V1, V2)->add(&V1, &V2); + return ConstRules::get(V1, V2).add(&V1, &V2); } inline Constant *operator-(const Constant &V1, const Constant &V2) { assert(V1.getType() == V2.getType() && "Constant types must be identical!"); - return ConstRules::get(V1, V2)->sub(&V1, &V2); + return ConstRules::get(V1, V2).sub(&V1, &V2); } inline Constant *operator*(const Constant &V1, const Constant &V2) { assert(V1.getType() == V2.getType() && "Constant types must be identical!"); - return ConstRules::get(V1, V2)->mul(&V1, &V2); + return ConstRules::get(V1, V2).mul(&V1, &V2); } inline Constant *operator/(const Constant &V1, const Constant &V2) { assert(V1.getType() == V2.getType() && "Constant types must be identical!"); - return ConstRules::get(V1, V2)->div(&V1, &V2); + return ConstRules::get(V1, V2).div(&V1, &V2); } inline Constant *operator%(const Constant &V1, const Constant &V2) { assert(V1.getType() == V2.getType() && "Constant types must be identical!"); - return ConstRules::get(V1, V2)->rem(&V1, &V2); + return ConstRules::get(V1, V2).rem(&V1, &V2); } // Logical Operators... inline Constant *operator&(const Constant &V1, const Constant &V2) { assert(V1.getType() == V2.getType() && "Constant types must be identical!"); - return ConstRules::get(V1, V2)->op_and(&V1, &V2); + return ConstRules::get(V1, V2).op_and(&V1, &V2); } inline Constant *operator|(const Constant &V1, const Constant &V2) { assert(V1.getType() == V2.getType() && "Constant types must be identical!"); - return ConstRules::get(V1, V2)->op_or(&V1, &V2); + return ConstRules::get(V1, V2).op_or(&V1, &V2); } inline Constant *operator^(const Constant &V1, const Constant &V2) { assert(V1.getType() == V2.getType() && "Constant types must be identical!"); - return ConstRules::get(V1, V2)->op_xor(&V1, &V2); + return ConstRules::get(V1, V2).op_xor(&V1, &V2); } // Shift Instructions... inline Constant *operator<<(const Constant &V1, const Constant &V2) { assert(V1.getType()->isInteger() && V2.getType() == Type::UByteTy); - return ConstRules::get(V1, V2)->shl(&V1, &V2); + return ConstRules::get(V1, V2).shl(&V1, &V2); } inline Constant *operator>>(const Constant &V1, const Constant &V2) { assert(V1.getType()->isInteger() && V2.getType() == Type::UByteTy); - return ConstRules::get(V1, V2)->shr(&V1, &V2); + return ConstRules::get(V1, V2).shr(&V1, &V2); } inline ConstantBool *operator<(const Constant &V1, const Constant &V2) { assert(V1.getType() == V2.getType() && "Constant types must be identical!"); - return ConstRules::get(V1, V2)->lessthan(&V1, &V2); + return ConstRules::get(V1, V2).lessthan(&V1, &V2); } diff --git a/lib/VMCore/ConstantFolding.h b/lib/VMCore/ConstantFolding.h index b392ad11737..d733ac80962 100644 --- a/lib/VMCore/ConstantFolding.h +++ b/lib/VMCore/ConstantFolding.h @@ -64,11 +64,8 @@ inline ConstantBool *operator!=(const Constant &V1, const Constant &V2) { // Implement all other operators indirectly through TypeRules system //===----------------------------------------------------------------------===// -class ConstRules : public Annotation { -protected: - inline ConstRules() : Annotation(AID) {} // Can only be subclassed... -public: - static AnnotationID AID; // AnnotationID for this class +struct ConstRules { + ConstRules() {} // Binary Operators... virtual Constant *add(const Constant *V1, const Constant *V2) const = 0; @@ -119,19 +116,11 @@ public: } } - // ConstRules::get - A type will cache its own type rules if one is needed... - // we just want to make sure to hit the cache instead of doing it indirectly, - // if possible... + // ConstRules::get - Return an instance of ConstRules for the specified + // constant operands. // - static inline ConstRules *get(const Constant &V1, const Constant &V2) { - if (isa(V1) || isa(V2)) - return getConstantExprRules(); - return static_cast(V1.getType()->getOrCreateAnnotation(AID)); - } + static ConstRules &get(const Constant &V1, const Constant &V2); private: - static ConstRules *getConstantExprRules(); - static Annotation *find(AnnotationID AID, const Annotable *Ty, void *); - ConstRules(const ConstRules &); // Do not implement ConstRules &operator=(const ConstRules &); // Do not implement }; @@ -139,71 +128,71 @@ private: // Unary operators... inline Constant *operator~(const Constant &V) { assert(V.getType()->isIntegral() && "Cannot invert non-integral constant!"); - return ConstRules::get(V, V)->op_xor(&V, + return ConstRules::get(V, V).op_xor(&V, ConstantInt::getAllOnesValue(V.getType())); } inline Constant *operator-(const Constant &V) { - return ConstRules::get(V, V)->sub(Constant::getNullValue(V.getType()), &V); + return ConstRules::get(V, V).sub(Constant::getNullValue(V.getType()), &V); } // Standard binary operators... inline Constant *operator+(const Constant &V1, const Constant &V2) { assert(V1.getType() == V2.getType() && "Constant types must be identical!"); - return ConstRules::get(V1, V2)->add(&V1, &V2); + return ConstRules::get(V1, V2).add(&V1, &V2); } inline Constant *operator-(const Constant &V1, const Constant &V2) { assert(V1.getType() == V2.getType() && "Constant types must be identical!"); - return ConstRules::get(V1, V2)->sub(&V1, &V2); + return ConstRules::get(V1, V2).sub(&V1, &V2); } inline Constant *operator*(const Constant &V1, const Constant &V2) { assert(V1.getType() == V2.getType() && "Constant types must be identical!"); - return ConstRules::get(V1, V2)->mul(&V1, &V2); + return ConstRules::get(V1, V2).mul(&V1, &V2); } inline Constant *operator/(const Constant &V1, const Constant &V2) { assert(V1.getType() == V2.getType() && "Constant types must be identical!"); - return ConstRules::get(V1, V2)->div(&V1, &V2); + return ConstRules::get(V1, V2).div(&V1, &V2); } inline Constant *operator%(const Constant &V1, const Constant &V2) { assert(V1.getType() == V2.getType() && "Constant types must be identical!"); - return ConstRules::get(V1, V2)->rem(&V1, &V2); + return ConstRules::get(V1, V2).rem(&V1, &V2); } // Logical Operators... inline Constant *operator&(const Constant &V1, const Constant &V2) { assert(V1.getType() == V2.getType() && "Constant types must be identical!"); - return ConstRules::get(V1, V2)->op_and(&V1, &V2); + return ConstRules::get(V1, V2).op_and(&V1, &V2); } inline Constant *operator|(const Constant &V1, const Constant &V2) { assert(V1.getType() == V2.getType() && "Constant types must be identical!"); - return ConstRules::get(V1, V2)->op_or(&V1, &V2); + return ConstRules::get(V1, V2).op_or(&V1, &V2); } inline Constant *operator^(const Constant &V1, const Constant &V2) { assert(V1.getType() == V2.getType() && "Constant types must be identical!"); - return ConstRules::get(V1, V2)->op_xor(&V1, &V2); + return ConstRules::get(V1, V2).op_xor(&V1, &V2); } // Shift Instructions... inline Constant *operator<<(const Constant &V1, const Constant &V2) { assert(V1.getType()->isInteger() && V2.getType() == Type::UByteTy); - return ConstRules::get(V1, V2)->shl(&V1, &V2); + return ConstRules::get(V1, V2).shl(&V1, &V2); } inline Constant *operator>>(const Constant &V1, const Constant &V2) { assert(V1.getType()->isInteger() && V2.getType() == Type::UByteTy); - return ConstRules::get(V1, V2)->shr(&V1, &V2); + return ConstRules::get(V1, V2).shr(&V1, &V2); } inline ConstantBool *operator<(const Constant &V1, const Constant &V2) { assert(V1.getType() == V2.getType() && "Constant types must be identical!"); - return ConstRules::get(V1, V2)->lessthan(&V1, &V2); + return ConstRules::get(V1, V2).lessthan(&V1, &V2); } -- 2.34.1