diff options
Diffstat (limited to 'llvm/lib/VMCore')
-rw-r--r-- | llvm/lib/VMCore/ConstantFold.cpp | 16 | ||||
-rw-r--r-- | llvm/lib/VMCore/Constants.cpp | 81 | ||||
-rw-r--r-- | llvm/lib/VMCore/Core.cpp | 4 | ||||
-rw-r--r-- | llvm/lib/VMCore/Instructions.cpp | 8 | ||||
-rw-r--r-- | llvm/lib/VMCore/LLVMContext.cpp | 74 | ||||
-rw-r--r-- | llvm/lib/VMCore/LLVMContextImpl.cpp | 35 | ||||
-rw-r--r-- | llvm/lib/VMCore/LLVMContextImpl.h | 3 |
7 files changed, 104 insertions, 117 deletions
diff --git a/llvm/lib/VMCore/ConstantFold.cpp b/llvm/lib/VMCore/ConstantFold.cpp index a26bee8f950..6546dc70d80 100644 --- a/llvm/lib/VMCore/ConstantFold.cpp +++ b/llvm/lib/VMCore/ConstantFold.cpp @@ -161,7 +161,7 @@ static Constant *FoldBitCast(LLVMContext &Context, return V; if (DestTy->isFloatingPoint()) - return Context.getConstantFP(APFloat(CI->getValue(), + return ConstantFP::get(Context, APFloat(CI->getValue(), DestTy != Type::PPC_FP128Ty)); // Otherwise, can't fold this (vector?) @@ -245,7 +245,7 @@ Constant *llvm::ConstantFoldCastInstruction(LLVMContext &Context, DestTy == Type::FP128Ty ? APFloat::IEEEquad : APFloat::Bogus, APFloat::rmNearestTiesToEven, &ignored); - return Context.getConstantFP(Val); + return ConstantFP::get(Context, Val); } return 0; // Can't fold. case Instruction::FPToUI: @@ -279,7 +279,7 @@ Constant *llvm::ConstantFoldCastInstruction(LLVMContext &Context, (void)apf.convertFromAPInt(api, opc==Instruction::SIToFP, APFloat::rmNearestTiesToEven); - return Context.getConstantFP(apf); + return ConstantFP::get(Context, apf); } return 0; case Instruction::ZExt: @@ -795,19 +795,19 @@ Constant *llvm::ConstantFoldBinaryInstruction(LLVMContext &Context, break; case Instruction::FAdd: (void)C3V.add(C2V, APFloat::rmNearestTiesToEven); - return Context.getConstantFP(C3V); + return ConstantFP::get(Context, C3V); case Instruction::FSub: (void)C3V.subtract(C2V, APFloat::rmNearestTiesToEven); - return Context.getConstantFP(C3V); + return ConstantFP::get(Context, C3V); case Instruction::FMul: (void)C3V.multiply(C2V, APFloat::rmNearestTiesToEven); - return Context.getConstantFP(C3V); + return ConstantFP::get(Context, C3V); case Instruction::FDiv: (void)C3V.divide(C2V, APFloat::rmNearestTiesToEven); - return Context.getConstantFP(C3V); + return ConstantFP::get(Context, C3V); case Instruction::FRem: (void)C3V.mod(C2V, APFloat::rmNearestTiesToEven); - return Context.getConstantFP(C3V); + return ConstantFP::get(Context, C3V); } } } else if (const VectorType *VTy = dyn_cast<VectorType>(C1->getType())) { diff --git a/llvm/lib/VMCore/Constants.cpp b/llvm/lib/VMCore/Constants.cpp index 96f729a7e66..873a0f1dc27 100644 --- a/llvm/lib/VMCore/Constants.cpp +++ b/llvm/lib/VMCore/Constants.cpp @@ -258,6 +258,87 @@ static const fltSemantics *TypeToFloatSemantics(const Type *Ty) { } #endif +/// get() - This returns a constant fp for the specified value in the +/// specified type. This should only be used for simple constant values like +/// 2.0/1.0 etc, that are known-valid both as double and as the target format. +Constant* ConstantFP::get(const Type* Ty, double V) { + LLVMContext &Context = Ty->getContext(); + + APFloat FV(V); + bool ignored; + FV.convert(*TypeToFloatSemantics(Ty->getScalarType()), + APFloat::rmNearestTiesToEven, &ignored); + Constant *C = get(Context, FV); + + // For vectors, broadcast the value. + if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) + return Context.getConstantVector( + std::vector<Constant *>(VTy->getNumElements(), C)); + + return C; +} + +ConstantFP* ConstantFP::getNegativeZero(const Type* Ty) { + LLVMContext &Context = Ty->getContext(); + APFloat apf = cast <ConstantFP>(Context.getNullValue(Ty))->getValueAPF(); + apf.changeSign(); + return get(Context, apf); +} + + +Constant* ConstantFP::getZeroValueForNegation(const Type* Ty) { + LLVMContext &Context = Ty->getContext(); + if (const VectorType *PTy = dyn_cast<VectorType>(Ty)) + if (PTy->getElementType()->isFloatingPoint()) { + std::vector<Constant*> zeros(PTy->getNumElements(), + getNegativeZero(PTy->getElementType())); + return Context.getConstantVector(PTy, zeros); + } + + if (Ty->isFloatingPoint()) + return getNegativeZero(Ty); + + return Context.getNullValue(Ty); +} + + +// ConstantFP accessors. +ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) { + DenseMapAPFloatKeyInfo::KeyTy Key(V); + + LLVMContextImpl* pImpl = Context.pImpl; + + pImpl->ConstantsLock.reader_acquire(); + ConstantFP *&Slot = pImpl->FPConstants[Key]; + pImpl->ConstantsLock.reader_release(); + + if (!Slot) { + sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock); + ConstantFP *&NewSlot = pImpl->FPConstants[Key]; + if (!NewSlot) { + const Type *Ty; + if (&V.getSemantics() == &APFloat::IEEEsingle) + Ty = Type::FloatTy; + else if (&V.getSemantics() == &APFloat::IEEEdouble) + Ty = Type::DoubleTy; + else if (&V.getSemantics() == &APFloat::x87DoubleExtended) + Ty = Type::X86_FP80Ty; + else if (&V.getSemantics() == &APFloat::IEEEquad) + Ty = Type::FP128Ty; + else { + assert(&V.getSemantics() == &APFloat::PPCDoubleDouble && + "Unknown FP format"); + Ty = Type::PPC_FP128Ty; + } + NewSlot = new ConstantFP(Ty, V); + } + + return NewSlot; + } + + return Slot; +} + ConstantFP::ConstantFP(const Type *Ty, const APFloat& V) : Constant(Ty, ConstantFPVal, 0, 0), Val(V) { assert(&V.getSemantics() == TypeToFloatSemantics(Ty) && diff --git a/llvm/lib/VMCore/Core.cpp b/llvm/lib/VMCore/Core.cpp index 15d9f7a5253..90f08c8e482 100644 --- a/llvm/lib/VMCore/Core.cpp +++ b/llvm/lib/VMCore/Core.cpp @@ -388,11 +388,11 @@ LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) { bool ignored; APN.convert(SemanticsForType(unwrap(RealTy)), APFloat::rmNearestTiesToEven, &ignored); - return wrap(getGlobalContext().getConstantFP(APN)); + return wrap(ConstantFP::get(getGlobalContext(), APN)); } LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) { - return wrap(getGlobalContext().getConstantFP( + return wrap(ConstantFP::get(getGlobalContext(), APFloat(SemanticsForType(unwrap(RealTy)), Text))); } diff --git a/llvm/lib/VMCore/Instructions.cpp b/llvm/lib/VMCore/Instructions.cpp index 20de3fecf86..a90bb4afcbd 100644 --- a/llvm/lib/VMCore/Instructions.cpp +++ b/llvm/lib/VMCore/Instructions.cpp @@ -1577,7 +1577,7 @@ BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, BinaryOperator *BinaryOperator::CreateNeg(LLVMContext &Context, Value *Op, const Twine &Name, Instruction *InsertBefore) { - Value *zero = Context.getZeroValueForNegation(Op->getType()); + Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); return new BinaryOperator(Instruction::Sub, zero, Op, Op->getType(), Name, InsertBefore); @@ -1586,7 +1586,7 @@ BinaryOperator *BinaryOperator::CreateNeg(LLVMContext &Context, BinaryOperator *BinaryOperator::CreateNeg(LLVMContext &Context, Value *Op, const Twine &Name, BasicBlock *InsertAtEnd) { - Value *zero = Context.getZeroValueForNegation(Op->getType()); + Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); return new BinaryOperator(Instruction::Sub, zero, Op, Op->getType(), Name, InsertAtEnd); @@ -1595,7 +1595,7 @@ BinaryOperator *BinaryOperator::CreateNeg(LLVMContext &Context, BinaryOperator *BinaryOperator::CreateFNeg(LLVMContext &Context, Value *Op, const Twine &Name, Instruction *InsertBefore) { - Value *zero = Context.getZeroValueForNegation(Op->getType()); + Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); return new BinaryOperator(Instruction::FSub, zero, Op, Op->getType(), Name, InsertBefore); @@ -1604,7 +1604,7 @@ BinaryOperator *BinaryOperator::CreateFNeg(LLVMContext &Context, BinaryOperator *BinaryOperator::CreateFNeg(LLVMContext &Context, Value *Op, const Twine &Name, BasicBlock *InsertAtEnd) { - Value *zero = Context.getZeroValueForNegation(Op->getType()); + Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); return new BinaryOperator(Instruction::FSub, zero, Op, Op->getType(), Name, InsertAtEnd); diff --git a/llvm/lib/VMCore/LLVMContext.cpp b/llvm/lib/VMCore/LLVMContext.cpp index 3ee029d4c31..d7f4594e3ab 100644 --- a/llvm/lib/VMCore/LLVMContext.cpp +++ b/llvm/lib/VMCore/LLVMContext.cpp @@ -41,15 +41,16 @@ Constant* LLVMContext::getNullValue(const Type* Ty) { case Type::IntegerTyID: return ConstantInt::get(Ty, 0); case Type::FloatTyID: - return getConstantFP(APFloat(APInt(32, 0))); + return ConstantFP::get(Ty->getContext(), APFloat(APInt(32, 0))); case Type::DoubleTyID: - return getConstantFP(APFloat(APInt(64, 0))); + return ConstantFP::get(Ty->getContext(), APFloat(APInt(64, 0))); case Type::X86_FP80TyID: - return getConstantFP(APFloat(APInt(80, 2, zero))); + return ConstantFP::get(Ty->getContext(), APFloat(APInt(80, 2, zero))); case Type::FP128TyID: - return getConstantFP(APFloat(APInt(128, 2, zero), true)); + return ConstantFP::get(Ty->getContext(), + APFloat(APInt(128, 2, zero), true)); case Type::PPC_FP128TyID: - return getConstantFP(APFloat(APInt(128, 2, zero))); + return ConstantFP::get(Ty->getContext(), APFloat(APInt(128, 2, zero))); case Type::PointerTyID: return getConstantPointerNull(cast<PointerType>(Ty)); case Type::StructTyID: @@ -276,7 +277,7 @@ Constant* LLVMContext::getConstantExprNeg(Constant* C) { assert(C->getType()->isIntOrIntVector() && "Cannot NEG a nonintegral value!"); return getConstantExpr(Instruction::Sub, - getZeroValueForNegation(C->getType()), + ConstantFP::getZeroValueForNegation(C->getType()), C); } @@ -284,7 +285,7 @@ Constant* LLVMContext::getConstantExprFNeg(Constant* C) { assert(C->getType()->isFPOrFPVector() && "Cannot FNEG a non-floating-point value!"); return getConstantExpr(Instruction::FSub, - getZeroValueForNegation(C->getType()), + ConstantFP::getZeroValueForNegation(C->getType()), C); } @@ -424,65 +425,6 @@ Constant* LLVMContext::getConstantExprSizeOf(const Type* Ty) { return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int64Ty); } -Constant* LLVMContext::getZeroValueForNegation(const Type* Ty) { - if (const VectorType *PTy = dyn_cast<VectorType>(Ty)) - if (PTy->getElementType()->isFloatingPoint()) { - std::vector<Constant*> zeros(PTy->getNumElements(), - getConstantFPNegativeZero(PTy->getElementType())); - return getConstantVector(PTy, zeros); - } - - if (Ty->isFloatingPoint()) - return getConstantFPNegativeZero(Ty); - - return getNullValue(Ty); -} - - -// ConstantFP accessors. -ConstantFP* LLVMContext::getConstantFP(const APFloat& V) { - return pImpl->getConstantFP(V); -} - -static const fltSemantics *TypeToFloatSemantics(const Type *Ty) { - if (Ty == Type::FloatTy) - return &APFloat::IEEEsingle; - if (Ty == Type::DoubleTy) - return &APFloat::IEEEdouble; - if (Ty == Type::X86_FP80Ty) - return &APFloat::x87DoubleExtended; - else if (Ty == Type::FP128Ty) - return &APFloat::IEEEquad; - - assert(Ty == Type::PPC_FP128Ty && "Unknown FP format"); - return &APFloat::PPCDoubleDouble; -} - -/// get() - This returns a constant fp for the specified value in the -/// specified type. This should only be used for simple constant values like -/// 2.0/1.0 etc, that are known-valid both as double and as the target format. -Constant* LLVMContext::getConstantFP(const Type* Ty, double V) { - APFloat FV(V); - bool ignored; - FV.convert(*TypeToFloatSemantics(Ty->getScalarType()), - APFloat::rmNearestTiesToEven, &ignored); - Constant *C = getConstantFP(FV); - - // For vectors, broadcast the value. - if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) - return - getConstantVector(std::vector<Constant *>(VTy->getNumElements(), C)); - - return C; -} - -ConstantFP* LLVMContext::getConstantFPNegativeZero(const Type* Ty) { - APFloat apf = cast <ConstantFP>(getNullValue(Ty))->getValueAPF(); - apf.changeSign(); - return getConstantFP(apf); -} - - // ConstantVector accessors. Constant* LLVMContext::getConstantVector(const VectorType* T, const std::vector<Constant*>& V) { diff --git a/llvm/lib/VMCore/LLVMContextImpl.cpp b/llvm/lib/VMCore/LLVMContextImpl.cpp index edd76fc031b..5315e23f8e3 100644 --- a/llvm/lib/VMCore/LLVMContextImpl.cpp +++ b/llvm/lib/VMCore/LLVMContextImpl.cpp @@ -49,41 +49,6 @@ static std::vector<Constant*> getValType(ConstantVector *CP) { LLVMContextImpl::LLVMContextImpl(LLVMContext &C) : Context(C), TheTrueVal(0), TheFalseVal(0) { } - -ConstantFP *LLVMContextImpl::getConstantFP(const APFloat &V) { - DenseMapAPFloatKeyInfo::KeyTy Key(V); - - ConstantsLock.reader_acquire(); - ConstantFP *&Slot = FPConstants[Key]; - ConstantsLock.reader_release(); - - if (!Slot) { - sys::SmartScopedWriter<true> Writer(ConstantsLock); - ConstantFP *&NewSlot = FPConstants[Key]; - if (!NewSlot) { - const Type *Ty; - if (&V.getSemantics() == &APFloat::IEEEsingle) - Ty = Type::FloatTy; - else if (&V.getSemantics() == &APFloat::IEEEdouble) - Ty = Type::DoubleTy; - else if (&V.getSemantics() == &APFloat::x87DoubleExtended) - Ty = Type::X86_FP80Ty; - else if (&V.getSemantics() == &APFloat::IEEEquad) - Ty = Type::FP128Ty; - else { - assert(&V.getSemantics() == &APFloat::PPCDoubleDouble && - "Unknown FP format"); - Ty = Type::PPC_FP128Ty; - } - NewSlot = new ConstantFP(Ty, V); - } - - return NewSlot; - } - - return Slot; -} - MDString *LLVMContextImpl::getMDString(const char *StrBegin, unsigned StrLength) { sys::SmartScopedWriter<true> Writer(ConstantsLock); diff --git a/llvm/lib/VMCore/LLVMContextImpl.h b/llvm/lib/VMCore/LLVMContextImpl.h index 96ac0f80fca..a5ff8d5c54e 100644 --- a/llvm/lib/VMCore/LLVMContextImpl.h +++ b/llvm/lib/VMCore/LLVMContextImpl.h @@ -457,11 +457,10 @@ class LLVMContextImpl { LLVMContextImpl(const LLVMContextImpl&); friend class ConstantInt; + friend class ConstantFP; public: LLVMContextImpl(LLVMContext &C); - ConstantFP *getConstantFP(const APFloat &V); - MDString *getMDString(const char *StrBegin, unsigned StrLength); MDNode *getMDNode(Value*const* Vals, unsigned NumVals); |