[ConstraintElimination] Use ZExtValue for unsigned decomposition.
authorFlorian Hahn <flo@fhahn.com>
Mon, 7 Mar 2022 13:33:50 +0000 (13:33 +0000)
committerFlorian Hahn <flo@fhahn.com>
Mon, 7 Mar 2022 13:34:01 +0000 (13:34 +0000)
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

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

index c094b5c..eb92c11 100644 (file)
@@ -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,
index 5276c07..bf79a52 100644 (file)
@@ -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: