[llvm] f213128 - [ConstraintElimination] Further de-compose operands of add operations.

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 14 04:00:59 PDT 2022


Author: Florian Hahn
Date: 2022-09-14T12:00:32+01:00
New Revision: f213128b292da85f68eeebbb68cba1541e1c39e2

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

LOG: [ConstraintElimination] Further de-compose operands of add operations.

This simply extends the existing logic to look through adds and combine
the components as done in other places already.

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index 3b97754c05dde..e939aab83912e 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -262,17 +262,27 @@ decompose(Value *V, SmallVector<PreconditionTy, 4> &Preconditions,
   Value *Op1;
   ConstantInt *CI;
   if (match(V, m_NUWAdd(m_Value(Op0), m_ConstantInt(CI))) &&
-      !CI->uge(MaxConstraintValue))
-    return {{int(CI->getZExtValue()), nullptr}, {1, Op0}};
+      !CI->uge(MaxConstraintValue)) {
+    auto Res = decompose(Op0, Preconditions, IsSigned);
+    Res[0].Coefficient += int(CI->getZExtValue());
+    return Res;
+  }
   if (match(V, m_Add(m_Value(Op0), m_ConstantInt(CI))) && CI->isNegative() &&
       CanUseSExt(CI)) {
     Preconditions.emplace_back(
         CmpInst::ICMP_UGE, Op0,
         ConstantInt::get(Op0->getType(), CI->getSExtValue() * -1));
-    return {{CI->getSExtValue(), nullptr}, {1, Op0}};
+    auto Res = decompose(Op0, Preconditions, IsSigned);
+    Res[0].Coefficient += int(CI->getSExtValue());
+    return Res;
+  }
+  if (match(V, m_NUWAdd(m_Value(Op0), m_Value(Op1)))) {
+    auto Res = decompose(Op0, Preconditions, IsSigned);
+    auto D1 = decompose(Op1, Preconditions, IsSigned);
+    Res[0].Coefficient += D1[0].Coefficient;
+    append_range(Res, drop_begin(D1));
+    return Res;
   }
-  if (match(V, m_NUWAdd(m_Value(Op0), m_Value(Op1))))
-    return {{0, nullptr}, {1, Op0}, {1, Op1}};
 
   if (match(V, m_NUWSub(m_Value(Op0), m_ConstantInt(CI))) && CanUseSExt(CI))
     return {{-1 * CI->getSExtValue(), nullptr}, {1, Op0}};

diff  --git a/llvm/test/Transforms/ConstraintElimination/add-nuw.ll b/llvm/test/Transforms/ConstraintElimination/add-nuw.ll
index c823d09cf1577..e8ac51bd9c6b5 100644
--- a/llvm/test/Transforms/ConstraintElimination/add-nuw.ll
+++ b/llvm/test/Transforms/ConstraintElimination/add-nuw.ll
@@ -344,7 +344,7 @@ define i1 @test_n_must_ule_1_due_to_nuw(i8 %n, i8 %i) {
 ; CHECK-NEXT:    br i1 [[C_1]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
 ; CHECK:       if.then:
 ; CHECK-NEXT:    [[T:%.*]] = icmp ule i8 [[N]], 1
-; CHECK-NEXT:    ret i1 [[T]]
+; CHECK-NEXT:    ret i1 true
 ; CHECK:       if.end:
 ; CHECK-NEXT:    [[F:%.*]] = icmp ule i8 [[N]], 1
 ; CHECK-NEXT:    ret i1 [[F]]
@@ -403,7 +403,7 @@ define i1 @test_n_must_ule_2_due_to_nuw(i8 %n, i8 %i) {
 ; CHECK-NEXT:    br i1 [[C_1]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
 ; CHECK:       if.then:
 ; CHECK-NEXT:    [[T:%.*]] = icmp ule i8 [[N]], 2
-; CHECK-NEXT:    ret i1 [[T]]
+; CHECK-NEXT:    ret i1 true
 ; CHECK:       if.end:
 ; CHECK-NEXT:    [[F:%.*]] = icmp ule i8 [[N]], 2
 ; CHECK-NEXT:    ret i1 [[F]]
@@ -590,7 +590,7 @@ define i1 @test_chained_adds_nuw_1(i8 %a, i8 %b) {
 ; CHECK-NEXT:    [[ADD_2:%.*]] = add nuw i8 [[ADD_1]], 2
 ; CHECK-NEXT:    [[T_1:%.*]] = icmp uge i8 [[ADD_2]], 13
 ; CHECK-NEXT:    [[C_1:%.*]] = icmp uge i8 [[ADD_2]], 14
-; CHECK-NEXT:    [[RES_1:%.*]] = xor i1 [[T_1]], [[C_1]]
+; CHECK-NEXT:    [[RES_1:%.*]] = xor i1 true, [[C_1]]
 ; CHECK-NEXT:    ret i1 [[RES_1]]
 ;
 entry:
@@ -618,7 +618,7 @@ define i1 @test_chained_adds_nuw_2(i8 %a, i8 %b) {
 ; CHECK-NEXT:    [[ADD_3:%.*]] = add nuw i8 [[ADD_2]], [[A]]
 ; CHECK-NEXT:    [[T_1:%.*]] = icmp uge i8 [[ADD_3]], 18
 ; CHECK-NEXT:    [[C_1:%.*]] = icmp uge i8 [[ADD_3]], 19
-; CHECK-NEXT:    [[RES_1:%.*]] = xor i1 [[T_1]], [[C_1]]
+; CHECK-NEXT:    [[RES_1:%.*]] = xor i1 true, [[C_1]]
 ; CHECK-NEXT:    ret i1 [[RES_1]]
 ;
 entry:
@@ -647,7 +647,7 @@ define i1 @test_chained_adds_nuw_3(i8 %a, i8 %b) {
 ; CHECK-NEXT:    [[ADD_3:%.*]] = add nuw i8 [[ADD_2]], [[A]]
 ; CHECK-NEXT:    [[T_1:%.*]] = icmp uge i8 [[ADD_3]], 18
 ; CHECK-NEXT:    [[C_1:%.*]] = icmp uge i8 [[ADD_3]], 19
-; CHECK-NEXT:    [[RES_1:%.*]] = xor i1 [[T_1]], [[C_1]]
+; CHECK-NEXT:    [[RES_1:%.*]] = xor i1 true, [[C_1]]
 ; CHECK-NEXT:    ret i1 [[RES_1]]
 ;
 entry:
@@ -676,7 +676,7 @@ define i1 @test_chained_adds_nuw_4(i8 %a, i8 %b) {
 ; CHECK-NEXT:    [[ADD_3:%.*]] = add nuw i8 [[ADD_2]], 10
 ; CHECK-NEXT:    [[T_1:%.*]] = icmp uge i8 [[ADD_3]], 23
 ; CHECK-NEXT:    [[C_1:%.*]] = icmp uge i8 [[ADD_3]], 24
-; CHECK-NEXT:    [[RES_1:%.*]] = xor i1 [[T_1]], [[C_1]]
+; CHECK-NEXT:    [[RES_1:%.*]] = xor i1 true, [[C_1]]
 ; CHECK-NEXT:    ret i1 [[RES_1]]
 ;
 entry:

diff  --git a/llvm/test/Transforms/ConstraintElimination/wrapping-math.ll b/llvm/test/Transforms/ConstraintElimination/wrapping-math.ll
index 91708e1ddeee8..55312348adbfa 100644
--- a/llvm/test/Transforms/ConstraintElimination/wrapping-math.ll
+++ b/llvm/test/Transforms/ConstraintElimination/wrapping-math.ll
@@ -281,9 +281,9 @@ define i1 @wrapping_add_known_1_add_nuw(i8 %a) {
 ; CHECK-NEXT:    [[ADD:%.*]] = add nuw i8 [[SUB_1]], 10
 ; CHECK-NEXT:    [[T_1:%.*]] = icmp uge i8 [[ADD]], 10
 ; CHECK-NEXT:    [[T_2:%.*]] = icmp ule i8 [[ADD]], 10
-; CHECK-NEXT:    [[RES_1:%.*]] = xor i1 [[T_1]], [[T_2]]
+; CHECK-NEXT:    [[RES_1:%.*]] = xor i1 true, true
 ; CHECK-NEXT:    [[F_1:%.*]] = icmp ult i8 [[ADD]], 10
-; CHECK-NEXT:    [[RES_2:%.*]] = xor i1 [[RES_1]], [[F_1]]
+; CHECK-NEXT:    [[RES_2:%.*]] = xor i1 [[RES_1]], false
 ; CHECK-NEXT:    ret i1 [[RES_2]]
 ;
 entry:
@@ -308,9 +308,9 @@ define i1 @add_nuw_wrapping_add_known_1(i8 %a) {
 ; CHECK-NEXT:    [[SUB_1:%.*]] = add i8 [[ADD]], -1
 ; CHECK-NEXT:    [[T_1:%.*]] = icmp uge i8 [[SUB_1]], 10
 ; CHECK-NEXT:    [[T_2:%.*]] = icmp ule i8 [[SUB_1]], 10
-; CHECK-NEXT:    [[RES_1:%.*]] = xor i1 [[T_1]], [[T_2]]
+; CHECK-NEXT:    [[RES_1:%.*]] = xor i1 true, true
 ; CHECK-NEXT:    [[F_1:%.*]] = icmp ult i8 [[SUB_1]], 10
-; CHECK-NEXT:    [[RES_2:%.*]] = xor i1 [[RES_1]], [[F_1]]
+; CHECK-NEXT:    [[RES_2:%.*]] = xor i1 [[RES_1]], false
 ; CHECK-NEXT:    ret i1 [[RES_2]]
 ;
 entry:


        


More information about the llvm-commits mailing list