[PATCH] D148230: ValueTracking: fadd +0 cannot return -0

Matt Arsenault via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 13 07:08:28 PDT 2023


arsenm created this revision.
arsenm added reviewers: jcranmer-intel, kpn, foad, sepavloff, andrew.w.kaylor.
Herald added subscribers: StephenFan, okura, kuter, hiraditya.
Herald added a project: All.
arsenm requested review of this revision.
Herald added a subscriber: wdng.
Herald added a reviewer: jdoerfert.
Herald added a reviewer: sstefan1.
Herald added a project: LLVM.

https://reviews.llvm.org/D148230

Files:
  llvm/lib/Analysis/ValueTracking.cpp
  llvm/test/Transforms/Attributor/nofpclass.ll


Index: llvm/test/Transforms/Attributor/nofpclass.ll
===================================================================
--- llvm/test/Transforms/Attributor/nofpclass.ll
+++ llvm/test/Transforms/Attributor/nofpclass.ll
@@ -1095,3 +1095,69 @@
   %cvt = sitofp i32 %arg to float
   ret float %cvt
 }
+
+define float @fadd_p0(float %arg0) {
+; CHECK: Function Attrs: nofree norecurse nosync nounwind willreturn memory(none)
+; CHECK-LABEL: define nofpclass(nzero) float @fadd_p0
+; CHECK-SAME: (float [[ARG0:%.*]]) #[[ATTR2]] {
+; CHECK-NEXT:    [[ADD:%.*]] = fadd float [[ARG0]], 0.000000e+00
+; CHECK-NEXT:    ret float [[ADD]]
+;
+  %add = fadd float %arg0, 0.0
+  ret float %add
+}
+
+define float @fadd_n0(float %arg0) {
+; CHECK: Function Attrs: nofree norecurse nosync nounwind willreturn memory(none)
+; CHECK-LABEL: define float @fadd_n0
+; CHECK-SAME: (float [[ARG0:%.*]]) #[[ATTR2]] {
+; CHECK-NEXT:    [[ADD:%.*]] = fadd float [[ARG0]], -0.000000e+00
+; CHECK-NEXT:    ret float [[ADD]]
+;
+  %add = fadd float %arg0, -0.0
+  ret float %add
+}
+
+define float @fsub_p0(float %arg0) {
+; CHECK: Function Attrs: nofree norecurse nosync nounwind willreturn memory(none)
+; CHECK-LABEL: define float @fsub_p0
+; CHECK-SAME: (float [[ARG0:%.*]]) #[[ATTR2]] {
+; CHECK-NEXT:    [[SUB:%.*]] = fsub float [[ARG0]], 0.000000e+00
+; CHECK-NEXT:    ret float [[SUB]]
+;
+  %sub = fsub float %arg0, 0.0
+  ret float %sub
+}
+
+define float @fsub_n0(float %arg0) {
+; CHECK: Function Attrs: nofree norecurse nosync nounwind willreturn memory(none)
+; CHECK-LABEL: define float @fsub_n0
+; CHECK-SAME: (float [[ARG0:%.*]]) #[[ATTR2]] {
+; CHECK-NEXT:    [[SUB:%.*]] = fsub float [[ARG0]], -0.000000e+00
+; CHECK-NEXT:    ret float [[SUB]]
+;
+  %sub = fsub float %arg0, -0.0
+  ret float %sub
+}
+
+define float @fsub_p0_commute(float %arg0) {
+; CHECK: Function Attrs: nofree norecurse nosync nounwind willreturn memory(none)
+; CHECK-LABEL: define float @fsub_p0_commute
+; CHECK-SAME: (float [[ARG0:%.*]]) #[[ATTR2]] {
+; CHECK-NEXT:    [[SUB:%.*]] = fsub float 0.000000e+00, [[ARG0]]
+; CHECK-NEXT:    ret float [[SUB]]
+;
+  %sub = fsub float 0.0, %arg0
+  ret float %sub
+}
+
+define float @fsub_n0_commute(float %arg0) {
+; CHECK: Function Attrs: nofree norecurse nosync nounwind willreturn memory(none)
+; CHECK-LABEL: define float @fsub_n0_commute
+; CHECK-SAME: (float [[ARG0:%.*]]) #[[ATTR2]] {
+; CHECK-NEXT:    [[SUB:%.*]] = fsub float -0.000000e+00, [[ARG0]]
+; CHECK-NEXT:    ret float [[SUB]]
+;
+  %sub = fsub float -0.0, %arg0
+  ret float %sub
+}
Index: llvm/lib/Analysis/ValueTracking.cpp
===================================================================
--- llvm/lib/Analysis/ValueTracking.cpp
+++ llvm/lib/Analysis/ValueTracking.cpp
@@ -4363,6 +4363,11 @@
     KnownFPClass KnownLHS, KnownRHS;
     computeKnownFPClass(Op->getOperand(1), DemandedElts, fcNan | fcInf,
                         KnownRHS, Depth + 1, Q, TLI);
+
+    // (fadd x, 0.0) is guaranteed to return +0.0, not -0.0.
+    if (KnownRHS.KnownFPClasses == fcPosZero && Op->getOpcode() == Instruction::FAdd)
+      Known.knownNot(fcNegZero);
+
     if (KnownRHS.isKnownNeverNaN()) {
       // RHS is canonically cheaper to compute. Skip inspecting the LHS if
       // there's no point.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D148230.513222.patch
Type: text/x-patch
Size: 3270 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230413/b72f52c3/attachment.bin>


More information about the llvm-commits mailing list