[llvm] [ConstraintElim] Check the other signedness when hasSameSign (PR #128168)

via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 21 04:23:38 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Marina Taylor (citymarina)

<details>
<summary>Changes</summary>



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


2 Files Affected:

- (modified) llvm/lib/Transforms/Scalar/ConstraintElimination.cpp (+8-1) 
- (modified) llvm/test/Transforms/ConstraintElimination/transfer-samesign-facts.ll (+20) 


``````````diff
diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index 6dd26910f6846..9a1a3f3dfeabc 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -1427,7 +1427,7 @@ static std::optional<bool> checkCondition(CmpInst::Predicate Pred, Value *A,
 }
 
 static bool checkAndReplaceCondition(
-    CmpInst *Cmp, ConstraintInfo &Info, unsigned NumIn, unsigned NumOut,
+    ICmpInst *Cmp, ConstraintInfo &Info, unsigned NumIn, unsigned NumOut,
     Instruction *ContextInst, Module *ReproducerModule,
     ArrayRef<ReproducerEntry> ReproducerCondStack, DominatorTree &DT,
     SmallVectorImpl<Instruction *> &ToRemove) {
@@ -1460,6 +1460,13 @@ static bool checkAndReplaceCondition(
           checkCondition(Cmp->getPredicate(), Cmp->getOperand(0),
                          Cmp->getOperand(1), Cmp, Info))
     return ReplaceCmpWithConstant(Cmp, *ImpliedCondition);
+
+  if (Cmp->hasSameSign())
+    if (auto ImpliedCondition = checkCondition(
+            ICmpInst::getFlippedSignednessPredicate(Cmp->getPredicate()),
+            Cmp->getOperand(0), Cmp->getOperand(1), Cmp, Info))
+      return ReplaceCmpWithConstant(Cmp, *ImpliedCondition);
+
   return false;
 }
 
diff --git a/llvm/test/Transforms/ConstraintElimination/transfer-samesign-facts.ll b/llvm/test/Transforms/ConstraintElimination/transfer-samesign-facts.ll
index 1b155e050de29..09cbbf1c661c7 100644
--- a/llvm/test/Transforms/ConstraintElimination/transfer-samesign-facts.ll
+++ b/llvm/test/Transforms/ConstraintElimination/transfer-samesign-facts.ll
@@ -303,3 +303,23 @@ define i1 @ugt_assumed_positive_values(i8 %a, i8 %b) {
 
   ret i1 %result
 }
+
+define i1 @samesign_flipped_signedness(i8 %a)  {
+; CHECK-LABEL: @samesign_flipped_signedness(
+; CHECK-NEXT:    [[TMP1:%.*]] = icmp ugt i8 [[A:%.*]], 0
+; CHECK-NEXT:    br i1 [[TMP1]], label [[GREATER:%.*]], label [[EXIT:%.*]]
+; CHECK:       greater:
+; CHECK-NEXT:    ret i1 true
+; CHECK:       exit:
+; CHECK-NEXT:    ret i1 false
+;
+  %1 = icmp ugt i8 %a, 0
+  br i1 %1, label %greater, label %exit
+
+greater:
+  %2 = icmp samesign sgt i8 %a, 0
+  ret i1 %2
+
+exit:
+  ret i1 false
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/128168


More information about the llvm-commits mailing list