Skip to content

[IndVarSimplify] Drop samesign flags after narrowing compares #116263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 15, 2024

Conversation

dtcxzyw
Copy link
Member

@dtcxzyw dtcxzyw commented Nov 14, 2024

Samesign flag cannot be preserved after narrowing the compare since the position of the sign bit is changed.

Closes #116249.

@llvmbot
Copy link
Member

llvmbot commented Nov 14, 2024

@llvm/pr-subscribers-llvm-transforms

Author: Yingwei Zheng (dtcxzyw)

Changes

Samesign flag cannot be preserved after narrowing the compare since the position of the sign bit is changed.

Closes #116249.


Full diff: https://github.com/llvm/llvm-project/pull/116263.diff

2 Files Affected:

  • (modified) llvm/lib/Transforms/Scalar/IndVarSimplify.cpp (+2)
  • (modified) llvm/test/Transforms/IndVarSimplify/finite-exit-comparisons.ll (+40)
diff --git a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
index 2b2d516a707934..8a3e0bc3eb9712 100644
--- a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
+++ b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
@@ -1530,6 +1530,8 @@ bool IndVarSimplify::canonicalizeExitCondition(Loop *L) {
           L->getLoopPreheader()->getTerminator()->getIterator());
       ICmp->setOperand(Swapped ? 1 : 0, LHSOp);
       ICmp->setOperand(Swapped ? 0 : 1, NewRHS);
+      // Samesign flag cannot be preserved after narrowing the compare.
+      ICmp->setSameSign(false);
       if (LHS->use_empty())
         DeadInsts.push_back(LHS);
     };
diff --git a/llvm/test/Transforms/IndVarSimplify/finite-exit-comparisons.ll b/llvm/test/Transforms/IndVarSimplify/finite-exit-comparisons.ll
index 038129e9a7cf21..3c6b12dac21191 100644
--- a/llvm/test/Transforms/IndVarSimplify/finite-exit-comparisons.ll
+++ b/llvm/test/Transforms/IndVarSimplify/finite-exit-comparisons.ll
@@ -1052,3 +1052,43 @@ for.end:                                          ; preds = %for.body, %entry
   ret void
 }
 
+define i8 @test_drop_icmp_samesign(i1 %cond, i32 range(i32 0, 32) %x) {
+; CHECK-LABEL: @test_drop_icmp_samesign(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    br i1 [[COND:%.*]], label [[FOR_BODY_PREHEADER:%.*]], label [[ELSE:%.*]]
+; CHECK:       for.body.preheader:
+; CHECK-NEXT:    [[TMP0:%.*]] = trunc i32 [[X:%.*]] to i8
+; CHECK-NEXT:    br label [[FOR_BODY:%.*]]
+; CHECK:       else:
+; CHECK-NEXT:    [[CALL1:%.*]] = call i8 @callee()
+; CHECK-NEXT:    br label [[EXIT:%.*]]
+; CHECK:       for.body:
+; CHECK-NEXT:    [[CALL2:%.*]] = call i8 @callee()
+; CHECK-NEXT:    [[COND2:%.*]] = icmp ugt i8 [[TMP0]], [[CALL2]]
+; CHECK-NEXT:    br i1 [[COND2]], label [[FOR_BODY]], label [[EXIT_LOOPEXIT:%.*]]
+; CHECK:       exit.loopexit:
+; CHECK-NEXT:    [[CALL2_LCSSA:%.*]] = phi i8 [ [[CALL2]], [[FOR_BODY]] ]
+; CHECK-NEXT:    br label [[EXIT]]
+; CHECK:       exit:
+; CHECK-NEXT:    [[RES:%.*]] = phi i8 [ [[CALL1]], [[ELSE]] ], [ [[CALL2_LCSSA]], [[EXIT_LOOPEXIT]] ]
+; CHECK-NEXT:    ret i8 [[RES]]
+;
+entry:
+  br i1 %cond, label %for.body, label %else
+
+else:
+  %call1 = call i8 @callee()
+  br label %exit
+
+for.body:
+  %call2 = call i8 @callee()
+  %ext = zext i8 %call2 to i32
+  %cond2 = icmp samesign ugt i32 %x, %ext
+  br i1 %cond2, label %for.body, label %exit
+
+exit:
+  %res = phi i8 [ %call1, %else ], [ %call2, %for.body ]
+  ret i8 %res
+}
+
+declare i8 @callee()

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dtcxzyw dtcxzyw merged commit 40a647f into llvm:main Nov 15, 2024
10 checks passed
@dtcxzyw dtcxzyw deleted the fix-116249 branch November 15, 2024 01:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[IndVarSimplify] samesign flag should be dropped after narrowing
3 participants