From 935e35d2b9f889566207b76a7026b63a1619742c Mon Sep 17 00:00:00 2001 From: Shuxin Yang Date: Wed, 9 Jan 2013 00:13:41 +0000 Subject: [PATCH] Consider expression "0.0 - X" as the negation of X if - this expression is explicitly marked no-signed-zero, or - no-signed-zero of this expression can be derived from some context. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@171922 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/IR/Constant.h | 3 +++ include/llvm/IR/InstrTypes.h | 2 +- lib/IR/Constants.cpp | 9 +++++++++ lib/IR/Instructions.cpp | 9 ++++++--- lib/Transforms/InstCombine/InstCombine.h | 2 +- .../InstCombine/InstructionCombining.cpp | 4 ++-- test/Transforms/InstCombine/fast-math.ll | 15 +++++++++++++-- 7 files changed, 35 insertions(+), 9 deletions(-) diff --git a/include/llvm/IR/Constant.h b/include/llvm/IR/Constant.h index 36a4538e86c..5a3ba284473 100644 --- a/include/llvm/IR/Constant.h +++ b/include/llvm/IR/Constant.h @@ -61,6 +61,9 @@ public: /// by getZeroValueForNegation. bool isNegativeZeroValue() const; + /// Return true if the value is negative zero or null value. + bool isZeroValue() const; + /// canTrap - Return true if evaluation of this constant could trap. This is /// true for things like constant expressions that could divide by zero. bool canTrap() const; diff --git a/include/llvm/IR/InstrTypes.h b/include/llvm/IR/InstrTypes.h index 66bf8dde979..7ee5cdb3223 100644 --- a/include/llvm/IR/InstrTypes.h +++ b/include/llvm/IR/InstrTypes.h @@ -309,7 +309,7 @@ public: /// NEG, FNeg, or NOT instruction. /// static bool isNeg(const Value *V); - static bool isFNeg(const Value *V); + static bool isFNeg(const Value *V, bool IgnoreZeroSign=false); static bool isNot(const Value *V); /// getNegArgument, getNotArgument - Helper functions to extract the diff --git a/lib/IR/Constants.cpp b/lib/IR/Constants.cpp index 4b585999554..812692f3fae 100644 --- a/lib/IR/Constants.cpp +++ b/lib/IR/Constants.cpp @@ -51,6 +51,15 @@ bool Constant::isNegativeZeroValue() const { return isNullValue(); } +bool Constant::isZeroValue() const { + // Floating point values have an explicit -0.0 value. + if (const ConstantFP *CFP = dyn_cast(this)) + return CFP->isZero(); + + // Otherwise, just use +0.0. + return isNullValue(); +} + bool Constant::isNullValue() const { // 0 is null. if (const ConstantInt *CI = dyn_cast(this)) diff --git a/lib/IR/Instructions.cpp b/lib/IR/Instructions.cpp index 1b5d004978c..f2e9813bc63 100644 --- a/lib/IR/Instructions.cpp +++ b/lib/IR/Instructions.cpp @@ -1926,11 +1926,14 @@ bool BinaryOperator::isNeg(const Value *V) { return false; } -bool BinaryOperator::isFNeg(const Value *V) { +bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) { if (const BinaryOperator *Bop = dyn_cast(V)) if (Bop->getOpcode() == Instruction::FSub) - if (Constant* C = dyn_cast(Bop->getOperand(0))) - return C->isNegativeZeroValue(); + if (Constant* C = dyn_cast(Bop->getOperand(0))) { + if (!IgnoreZeroSign) + IgnoreZeroSign = cast(V)->hasNoSignedZeros(); + return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue(); + } return false; } diff --git a/lib/Transforms/InstCombine/InstCombine.h b/lib/Transforms/InstCombine/InstCombine.h index 959daa258d8..a36b1e6b9e5 100644 --- a/lib/Transforms/InstCombine/InstCombine.h +++ b/lib/Transforms/InstCombine/InstCombine.h @@ -211,7 +211,7 @@ public: private: bool ShouldChangeType(Type *From, Type *To) const; Value *dyn_castNegVal(Value *V) const; - Value *dyn_castFNegVal(Value *V) const; + Value *dyn_castFNegVal(Value *V, bool NoSignedZero=false) const; Type *FindElementAtOffset(Type *Ty, int64_t Offset, SmallVectorImpl &NewIndices); Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI); diff --git a/lib/Transforms/InstCombine/InstructionCombining.cpp b/lib/Transforms/InstCombine/InstructionCombining.cpp index 6f24cdd738c..dc7fe5cf6b5 100644 --- a/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -516,8 +516,8 @@ Value *InstCombiner::dyn_castNegVal(Value *V) const { // instruction if the LHS is a constant negative zero (which is the 'negate' // form). // -Value *InstCombiner::dyn_castFNegVal(Value *V) const { - if (BinaryOperator::isFNeg(V)) +Value *InstCombiner::dyn_castFNegVal(Value *V, bool IgnoreZeroSign) const { + if (BinaryOperator::isFNeg(V, IgnoreZeroSign)) return BinaryOperator::getFNegArgument(V); // Constants can be considered to be negated values if they can be folded. diff --git a/test/Transforms/InstCombine/fast-math.ll b/test/Transforms/InstCombine/fast-math.ll index 5d40d71f875..df0455a2032 100644 --- a/test/Transforms/InstCombine/fast-math.ll +++ b/test/Transforms/InstCombine/fast-math.ll @@ -243,5 +243,16 @@ define float @fmul5(float %f1, float %f2) { ; CHECK: fdiv fast float %f1, 0x47E8000000000000 } - - +; ========================================================================= +; +; Testing-cases about negation +; +; ========================================================================= +define float @fneg1(float %f1, float %f2) { + %sub = fsub float -0.000000e+00, %f1 + %sub1 = fsub nsz float 0.000000e+00, %f2 + %mul = fmul float %sub, %sub1 + ret float %mul +; CHECK: @fneg1 +; CHECK: fmul float %f1, %f2 +} -- 2.34.1