[llvm] 4bbee17 - [ConstraintElimination] Use ZExtValue for unsigned decomposition.

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 7 05:34:18 PST 2022


Author: Florian Hahn
Date: 2022-03-07T13:34:01Z
New Revision: 4bbee17ecb00bcdf43a43650a64172a4d9eb130c

URL: https://github.com/llvm/llvm-project/commit/4bbee17ecb00bcdf43a43650a64172a4d9eb130c
DIFF: https://github.com/llvm/llvm-project/commit/4bbee17ecb00bcdf43a43650a64172a4d9eb130c.diff

LOG: [ConstraintElimination] Use ZExtValue for unsigned decomposition.

When decomposing constraints for unsigned conditions, we can use
negative values by zero-extending them, as long as they are less than
the maximum constraint value.

Fixes https://github.com/llvm/llvm-project/issues/54224

Added: 
    

Modified: 
    llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
    llvm/test/Transforms/ConstraintElimination/add-nuw.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index c094b5c660471..eb92c11af2071 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -153,9 +153,9 @@ decompose(Value *V, SmallVector<PreconditionTy, 4> &Preconditions,
   }
 
   if (auto *CI = dyn_cast<ConstantInt>(V)) {
-    if (CI->isNegative() || CI->uge(MaxConstraintValue))
+    if (CI->uge(MaxConstraintValue))
       return {};
-    return {{CI->getSExtValue(), nullptr}};
+    return {{CI->getZExtValue(), nullptr}};
   }
   auto *GEP = dyn_cast<GetElementPtrInst>(V);
   if (GEP && GEP->getNumOperands() == 2 && GEP->isInBounds()) {
@@ -208,8 +208,9 @@ decompose(Value *V, SmallVector<PreconditionTy, 4> &Preconditions,
 
   Value *Op1;
   ConstantInt *CI;
-  if (match(V, m_NUWAdd(m_Value(Op0), m_ConstantInt(CI))))
-    return {{CI->getSExtValue(), nullptr}, {1, Op0}};
+  if (match(V, m_NUWAdd(m_Value(Op0), m_ConstantInt(CI))) &&
+      !CI->uge(MaxConstraintValue))
+    return {{CI->getZExtValue(), nullptr}, {1, Op0}};
   if (match(V, m_Add(m_Value(Op0), m_ConstantInt(CI))) && CI->isNegative()) {
     Preconditions.emplace_back(
         CmpInst::ICMP_UGE, Op0,

diff  --git a/llvm/test/Transforms/ConstraintElimination/add-nuw.ll b/llvm/test/Transforms/ConstraintElimination/add-nuw.ll
index 5276c07bd94cb..bf79a5290d717 100644
--- a/llvm/test/Transforms/ConstraintElimination/add-nuw.ll
+++ b/llvm/test/Transforms/ConstraintElimination/add-nuw.ll
@@ -463,10 +463,10 @@ define i1 @add_nuw_neg_pr54224_i16(i16 %a) {
 ; CHECK-NEXT:    br i1 [[C_1]], label [[EXIT_1:%.*]], label [[EXIT_2:%.*]]
 ; CHECK:       exit.1:
 ; CHECK-NEXT:    [[C_2:%.*]] = icmp ugt i16 [[A]], 0
-; CHECK-NEXT:    ret i1 [[C_2]]
+; CHECK-NEXT:    ret i1 false
 ; CHECK:       exit.2:
 ; CHECK-NEXT:    [[C_3:%.*]] = icmp ugt i16 [[A]], 0
-; CHECK-NEXT:    ret i1 true
+; CHECK-NEXT:    ret i1 [[C_3]]
 ;
 entry:
   %neg2 = add nuw i16 %a, -305
@@ -493,7 +493,7 @@ define i1 @add_nuw_neg_pr54224_i64(i64 %a) {
 ; CHECK-NEXT:    ret i1 [[C_2]]
 ; CHECK:       exit.2:
 ; CHECK-NEXT:    [[C_3:%.*]] = icmp ugt i64 [[A]], 0
-; CHECK-NEXT:    ret i1 true
+; CHECK-NEXT:    ret i1 [[C_3]]
 ;
 entry:
   %neg2 = add nuw i64 %a, -305
@@ -518,12 +518,12 @@ define i1 @add_nuw_neg2_i8(i8 %a) {
 ; CHECK:       exit.1:
 ; CHECK-NEXT:    [[T_1:%.*]] = icmp ult i8 [[A]], 2
 ; CHECK-NEXT:    [[C_2:%.*]] = icmp ult i8 [[A]], 1
-; CHECK-NEXT:    [[RES_1:%.*]] = xor i1 [[T_1]], [[C_2]]
+; CHECK-NEXT:    [[RES_1:%.*]] = xor i1 true, [[C_2]]
 ; CHECK-NEXT:    ret i1 [[RES_1]]
 ; CHECK:       exit.2:
 ; CHECK-NEXT:    [[C_3:%.*]] = icmp ult i8 [[A]], 3
 ; CHECK-NEXT:    [[F_1:%.*]] = icmp ult i8 [[A]], 2
-; CHECK-NEXT:    [[RES_2:%.*]] = xor i1 [[C_3]], [[F_1]]
+; CHECK-NEXT:    [[RES_2:%.*]] = xor i1 [[C_3]], false
 ; CHECK-NEXT:    ret i1 [[RES_2]]
 ;
 entry:


        


More information about the llvm-commits mailing list