[llvm] r239701 - [InstSimplify] Add self-fdiv identities for -ffinite-math-only.
Benjamin Kramer
benny.kra at gmail.com
Tue Jun 16 08:03:16 PDT 2015
On Tue, Jun 16, 2015 at 2:09 AM Sean Silva <chisophugis at gmail.com> wrote:
> On Sun, Jun 14, 2015 at 11:53 AM, Benjamin Kramer <
> benny.kra at googlemail.com> wrote:
>
>> Author: d0k
>> Date: Sun Jun 14 13:53:58 2015
>> New Revision: 239701
>>
>> URL: http://llvm.org/viewvc/llvm-project?rev=239701&view=rev
>> Log:
>> [InstSimplify] Add self-fdiv identities for -ffinite-math-only.
>>
>> When NaNs and Infs are ignored we can fold
>> X / X -> 1.0
>> -X / X -> -1.0
>> X / -X -> -1.0
>>
>> Modified:
>> llvm/trunk/lib/Analysis/InstructionSimplify.cpp
>> llvm/trunk/test/Transforms/InstSimplify/fast-math.ll
>>
>> Modified: llvm/trunk/lib/Analysis/InstructionSimplify.cpp
>> URL:
>> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/InstructionSimplify.cpp?rev=239701&r1=239700&r2=239701&view=diff
>>
>> ==============================================================================
>> --- llvm/trunk/lib/Analysis/InstructionSimplify.cpp (original)
>> +++ llvm/trunk/lib/Analysis/InstructionSimplify.cpp Sun Jun 14 13:53:58
>> 2015
>> @@ -1126,6 +1126,21 @@ static Value *SimplifyFDivInst(Value *Op
>> if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op0, m_AnyZero()))
>> return Op0;
>>
>> + if (FMF.noNaNs() && FMF.noInfs()) {
>> + // X / X -> 1.0 iff NaNs and infinities are ignored.
>>
>
> I'm not sure "iff NaNs and infinities are ignored" (in particular the
> "iff") is the best way to put it. The situation I think is more like "is
> legal when NaNs and infinities are ignored". Merely ignoring NaNs and
> infinities does not imply that X/X is guaranteed to be 1.0.
>
> Also, I don't think you need the noInfs assumption. The cases where
> X/X->1.0 will fail have X/X->NaN.
>
Removed ninf check and rephrased comment in r239821. Thanks for the review
:)
- Ben
>
>
>> + if (Op0 == Op1)
>> + return ConstantFP::get(Op0->getType(), 1.0);
>> +
>> + // -X / X -> -1.0 and
>> + // X / -X -> -1.0 iff NaNs and infinities are ignored.
>> + // We can ignore signed zeros because +-0.0/+-0.0 is NaN and ignored.
>> + if ((BinaryOperator::isFNeg(Op0, /*IgnoreZeroSign=*/true) &&
>> + BinaryOperator::getFNegArgument(Op0) == Op1) ||
>> + (BinaryOperator::isFNeg(Op1, /*IgnoreZeroSign=*/true) &&
>> + BinaryOperator::getFNegArgument(Op1) == Op0))
>> + return ConstantFP::get(Op0->getType(), -1.0);
>> + }
>> +
>> return nullptr;
>> }
>>
>>
>> Modified: llvm/trunk/test/Transforms/InstSimplify/fast-math.ll
>> URL:
>> http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/fast-math.ll?rev=239701&r1=239700&r2=239701&view=diff
>>
>> ==============================================================================
>> --- llvm/trunk/test/Transforms/InstSimplify/fast-math.ll (original)
>> +++ llvm/trunk/test/Transforms/InstSimplify/fast-math.ll Sun Jun 14
>> 13:53:58 2015
>> @@ -114,3 +114,80 @@ define double @fdiv_zero_by_x(double %X)
>> ret double %r
>> ; CHECK: ret double 0
>> }
>> +
>> +define float @fdiv_self(float %f) {
>> + %div = fdiv nnan ninf float %f, %f
>> + ret float %div
>> +; CHECK-LABEL: fdiv_self
>> +; CHECK: ret float 1.000000e+00
>> +}
>> +
>> +define float @fdiv_self_invalid1(float %f) {
>> + %div = fdiv ninf float %f, %f
>> + ret float %div
>> +; CHECK-LABEL: fdiv_self_invalid1
>> +; CHECK: %div = fdiv ninf float %f, %f
>> +; CHECK-NEXT: ret float %div
>> +}
>> +
>> +define float @fdiv_self_invalid2(float %f) {
>> + %div = fdiv nnan float %f, %f
>> + ret float %div
>> +; CHECK-LABEL: fdiv_self_invalid2
>> +; CHECK: %div = fdiv nnan float %f, %f
>> +; CHECK-NEXT: ret float %div
>> +}
>> +
>> +define float @fdiv_self_invalid3(float %f) {
>> + %div = fdiv float %f, %f
>> + ret float %div
>> +; CHECK-LABEL: fdiv_self_invalid3
>> +; CHECK: %div = fdiv float %f, %f
>> +; CHECK-NEXT: ret float %div
>> +}
>> +
>> +define float @fdiv_neg(float %f) {
>> + %neg = fsub fast float -0.000000e+00, %f
>> + %div = fdiv nnan ninf float %neg, %f
>> + ret float %div
>> +; CHECK-LABEL: fdiv_neg
>> +; CHECK: ret float -1.000000e+00
>> +}
>> +
>> +define float @fdiv_neg_invalid1(float %f) {
>> + %neg = fsub fast float -0.000000e+00, %f
>> + %div = fdiv ninf float %neg, %f
>> + ret float %div
>> +; CHECK-LABEL: fdiv_neg_invalid1
>> +; CHECK: %neg = fsub fast float -0.000000e+00, %f
>> +; CHECK-NEXT: %div = fdiv ninf float %neg, %f
>> +; CHECK-NEXT: ret float %div
>> +}
>> +
>> +define float @fdiv_neg_invalid2(float %f) {
>> + %neg = fsub fast float -0.000000e+00, %f
>> + %div = fdiv nnan float %neg, %f
>> + ret float %div
>> +; CHECK-LABEL: fdiv_neg_invalid2
>> +; CHECK: %neg = fsub fast float -0.000000e+00, %f
>> +; CHECK-NEXT: %div = fdiv nnan float %neg, %f
>> +; CHECK-NEXT: ret float %div
>> +}
>> +
>> +define float @fdiv_neg_invalid3(float %f) {
>> + %neg = fsub fast float -0.000000e+00, %f
>> + %div = fdiv float %neg, %f
>> + ret float %div
>> +; CHECK-LABEL: fdiv_neg_invalid3
>> +; CHECK: %neg = fsub fast float -0.000000e+00, %f
>> +; CHECK-NEXT: %div = fdiv float %neg, %f
>> +; CHECK-NEXT: ret float %div
>> +}
>> +
>> +define float @fdiv_neg_swapped(float %f) {
>> + %neg = fsub float 0.000000e+00, %f
>> + %div = fdiv nnan ninf float %f, %neg
>> + ret float %div
>> +; CHECK-LABEL: fdiv_neg_swapped
>> +; CHECK: ret float -1.000000e+00
>> +}
>>
>>
>> _______________________________________________
>> llvm-commits mailing list
>> llvm-commits at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150616/5ad9e909/attachment.html>
More information about the llvm-commits
mailing list