[llvm] 6f2e943 - InstSimplify: Handle folding fcmp with literal nans without a context instruction

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Sat Sep 2 07:22:14 PDT 2023


Author: Matt Arsenault
Date: 2023-09-02T10:22:09-04:00
New Revision: 6f2e943de67aed26c356ebbc03c5afb9925557bd

URL: https://github.com/llvm/llvm-project/commit/6f2e943de67aed26c356ebbc03c5afb9925557bd
DIFF: https://github.com/llvm/llvm-project/commit/6f2e943de67aed26c356ebbc03c5afb9925557bd.diff

LOG: InstSimplify: Handle folding fcmp with literal nans without a context instruction

Fixes reported assert after ddb3f12c428bc4bd5a98913d74dfd7f2402bdfd8

Added: 
    llvm/test/Transforms/JumpThreading/simplify-fcmp-inst-no-context-literal-nan.ll

Modified: 
    llvm/lib/Analysis/InstructionSimplify.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 47a4991e4ef1cc..d0cc56ebc2be31 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -4125,15 +4125,17 @@ static Value *simplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
 
   // Handle fcmp with constant RHS.
   if (C) {
+    // TODO: If we always required a context function, we wouldn't need to
+    // special case nans.
+    if (C->isNaN())
+      return ConstantInt::get(RetTy, CmpInst::isUnordered(Pred));
+
     // TODO: Need version fcmpToClassTest which returns implied class when the
     // compare isn't a complete class test. e.g. > 1.0 implies fcPositive, but
     // isn't implementable as a class call.
     if (C->isNegative() && !C->isNegZero()) {
       FPClassTest Interested = KnownFPClass::OrderedLessThanZeroMask;
 
-      // FIXME: This assert won't always hold if we depend on the context
-      // instruction above
-      assert(!C->isNaN() && "Unexpected NaN constant!");
       // TODO: We can catch more cases by using a range check rather than
       //       relying on CannotBeOrderedLessThanZero.
       switch (Pred) {

diff  --git a/llvm/test/Transforms/JumpThreading/simplify-fcmp-inst-no-context-literal-nan.ll b/llvm/test/Transforms/JumpThreading/simplify-fcmp-inst-no-context-literal-nan.ll
new file mode 100644
index 00000000000000..22b1cf0babb654
--- /dev/null
+++ b/llvm/test/Transforms/JumpThreading/simplify-fcmp-inst-no-context-literal-nan.ll
@@ -0,0 +1,101 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 3
+; RUN: opt -S -passes=jump-threading < %s | FileCheck %s
+
+; Test for assert that fired after
+; ddb3f12c428bc4bd5a98913d74dfd7f2402bdfd8. simplifyFCmpInst is called
+; with a literal negative nan without a context instruction.
+
+
+; simplifyFCmpInst gets called without a context instruction and
+; expects to simplify an ordered compare with a negative nan.
+define i1 @simplify_fcmp_negative_nan_oeq(i1 %cond, float %arg) {
+; CHECK-LABEL: define i1 @simplify_fcmp_negative_nan_oeq(
+; CHECK-SAME: i1 [[COND:%.*]], float [[ARG:%.*]]) {
+; CHECK-NEXT:  bb:
+; CHECK-NEXT:    br i1 [[COND]], label [[BB2:%.*]], label [[BB5:%.*]]
+; CHECK:       bb2:
+; CHECK-NEXT:    [[I3:%.*]] = fcmp oeq float 0.000000e+00, [[ARG]]
+; CHECK-NEXT:    br i1 [[I3]], label [[BB5]], label [[BB5]]
+; CHECK:       bb5:
+; CHECK-NEXT:    [[I33:%.*]] = phi i1 [ [[I3]], [[BB2]] ], [ [[I3]], [[BB2]] ], [ false, [[BB:%.*]] ]
+; CHECK-NEXT:    ret i1 [[I33]]
+;
+bb:
+  br i1 %cond, label %bb1, label %bb2
+
+bb1:
+  br label %bb2
+
+bb2:                                              ; preds = %bb1, %bb
+  %i = phi float [ 0xFFFFFFFFE0000000, %bb ], [ 0.000000e+00, %bb1 ]
+  %i3 = fcmp oeq float %i, %arg
+  br i1 %i3, label %bb4, label %bb5
+
+bb4:                                              ; preds = %bb2
+  br label %bb5
+
+bb5:                                              ; preds = %bb4, %bb2
+  ret i1 %i3
+}
+
+; simplifyFCmpInst gets called without a context instruction and
+; expects to simplify an unordered compare with a negative nan.
+define i1 @simplify_fcmp_negative_nan_ueq(i1 %cond, float %arg) {
+; CHECK-LABEL: define i1 @simplify_fcmp_negative_nan_ueq(
+; CHECK-SAME: i1 [[COND:%.*]], float [[ARG:%.*]]) {
+; CHECK-NEXT:  bb:
+; CHECK-NEXT:    br i1 [[COND]], label [[BB2:%.*]], label [[BB5:%.*]]
+; CHECK:       bb2:
+; CHECK-NEXT:    [[I3:%.*]] = fcmp ueq float 0.000000e+00, [[ARG]]
+; CHECK-NEXT:    br i1 [[I3]], label [[BB5]], label [[BB5]]
+; CHECK:       bb5:
+; CHECK-NEXT:    [[I33:%.*]] = phi i1 [ [[I3]], [[BB2]] ], [ [[I3]], [[BB2]] ], [ true, [[BB:%.*]] ]
+; CHECK-NEXT:    ret i1 [[I33]]
+;
+bb:
+  br i1 %cond, label %bb1, label %bb2
+
+bb1:
+  br label %bb2
+
+bb2:                                              ; preds = %bb1, %bb
+  %i = phi float [ 0xFFFFFFFFE0000000, %bb ], [ 0.000000e+00, %bb1 ]
+  %i3 = fcmp ueq float %i, %arg
+  br i1 %i3, label %bb4, label %bb5
+
+bb4:                                              ; preds = %bb2
+  br label %bb5
+
+bb5:                                              ; preds = %bb4, %bb2
+  ret i1 %i3
+}
+
+define i1 @simplify_fcmp_positive_nan_oeq(i1 %cond, float %arg) {
+; CHECK-LABEL: define i1 @simplify_fcmp_positive_nan_oeq(
+; CHECK-SAME: i1 [[COND:%.*]], float [[ARG:%.*]]) {
+; CHECK-NEXT:  bb:
+; CHECK-NEXT:    br i1 [[COND]], label [[BB2:%.*]], label [[BB5:%.*]]
+; CHECK:       bb2:
+; CHECK-NEXT:    [[I3:%.*]] = fcmp oeq float 0.000000e+00, [[ARG]]
+; CHECK-NEXT:    br i1 [[I3]], label [[BB5]], label [[BB5]]
+; CHECK:       bb5:
+; CHECK-NEXT:    [[I33:%.*]] = phi i1 [ [[I3]], [[BB2]] ], [ [[I3]], [[BB2]] ], [ false, [[BB:%.*]] ]
+; CHECK-NEXT:    ret i1 [[I33]]
+;
+bb:
+  br i1 %cond, label %bb1, label %bb2
+
+bb1:
+  br label %bb2
+
+bb2:                                              ; preds = %bb1, %bb
+  %i = phi float [ 0x7FFFFFFFE0000000, %bb ], [ 0.000000e+00, %bb1 ]
+  %i3 = fcmp oeq float %i, %arg
+  br i1 %i3, label %bb4, label %bb5
+
+bb4:                                              ; preds = %bb2
+  br label %bb5
+
+bb5:                                              ; preds = %bb4, %bb2
+  ret i1 %i3
+}


        


More information about the llvm-commits mailing list