From 8799552d28163b539bfcdedb065082002f2ad6c6 Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Wed, 18 Nov 2020 11:25:39 +0000 Subject: [PATCH] [ConstraintElimination] Decompose add nuw/sub nuw. Make use of the more flexible constraint handling added in a8a79c90699a7ae9dee07daf7281cbbd592bf6ea to decompose add nuw/sub nuw. --- .../Scalar/ConstraintElimination.cpp | 14 +++++++ .../ConstraintElimination/add-nuw.ll | 40 +++++++++---------- .../ConstraintElimination/sub-nuw.ll | 26 ++++++------ 3 files changed, 47 insertions(+), 33 deletions(-) diff --git a/lib/Transforms/Scalar/ConstraintElimination.cpp b/lib/Transforms/Scalar/ConstraintElimination.cpp index 2ae913ce8f0..251faa0b182 100644 --- a/lib/Transforms/Scalar/ConstraintElimination.cpp +++ b/lib/Transforms/Scalar/ConstraintElimination.cpp @@ -57,6 +57,20 @@ static SmallVector, 4> decompose(Value *V) { nullptr}, {1, GEP->getPointerOperand()}}; } + + Value *Op0; + 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_Value(Op1)))) + return {{0, nullptr}, {1, Op0}, {1, Op1}}; + + if (match(V, m_NUWSub(m_Value(Op0), m_ConstantInt(CI)))) + return {{-1 * CI->getSExtValue(), nullptr}, {1, Op0}}; + if (match(V, m_NUWSub(m_Value(Op0), m_Value(Op1)))) + return {{0, nullptr}, {1, Op0}, {1, Op1}}; + return {{0, nullptr}, {1, V}}; } diff --git a/test/Transforms/ConstraintElimination/add-nuw.ll b/test/Transforms/ConstraintElimination/add-nuw.ll index 2e2b3213a7a..17925166748 100644 --- a/test/Transforms/ConstraintElimination/add-nuw.ll +++ b/test/Transforms/ConstraintElimination/add-nuw.ll @@ -11,16 +11,16 @@ define void @test.not.uge.ult(i8 %start, i8 %low, i8 %high) { ; CHECK-NEXT: ret void ; CHECK: if.end: ; CHECK-NEXT: [[T_0:%.*]] = icmp ult i8 [[START]], [[HIGH]] -; CHECK-NEXT: call void @use(i1 [[T_0]]) +; CHECK-NEXT: call void @use(i1 true) ; CHECK-NEXT: [[START_1:%.*]] = add nuw i8 [[START]], 1 ; CHECK-NEXT: [[T_1:%.*]] = icmp ult i8 [[START_1]], [[HIGH]] -; CHECK-NEXT: call void @use(i1 [[T_1]]) +; CHECK-NEXT: call void @use(i1 true) ; CHECK-NEXT: [[START_2:%.*]] = add nuw i8 [[START]], 2 ; CHECK-NEXT: [[T_2:%.*]] = icmp ult i8 [[START_2]], [[HIGH]] -; CHECK-NEXT: call void @use(i1 [[T_2]]) +; CHECK-NEXT: call void @use(i1 true) ; CHECK-NEXT: [[START_3:%.*]] = add nuw i8 [[START]], 3 ; CHECK-NEXT: [[T_3:%.*]] = icmp ult i8 [[START_3]], [[HIGH]] -; CHECK-NEXT: call void @use(i1 [[T_3]]) +; CHECK-NEXT: call void @use(i1 true) ; CHECK-NEXT: [[START_4:%.*]] = add nuw i8 [[START]], 4 ; CHECK-NEXT: [[C_4:%.*]] = icmp ult i8 [[START_4]], [[HIGH]] ; CHECK-NEXT: call void @use(i1 [[C_4]]) @@ -62,19 +62,19 @@ define void @test.not.uge.ule(i8 %start, i8 %low, i8 %high) { ; CHECK-NEXT: ret void ; CHECK: if.end: ; CHECK-NEXT: [[T_0:%.*]] = icmp ule i8 [[START]], [[HIGH]] -; CHECK-NEXT: call void @use(i1 [[T_0]]) +; CHECK-NEXT: call void @use(i1 true) ; CHECK-NEXT: [[START_1:%.*]] = add nuw i8 [[START]], 1 ; CHECK-NEXT: [[T_1:%.*]] = icmp ule i8 [[START_1]], [[HIGH]] -; CHECK-NEXT: call void @use(i1 [[T_1]]) +; CHECK-NEXT: call void @use(i1 true) ; CHECK-NEXT: [[START_2:%.*]] = add nuw i8 [[START]], 2 ; CHECK-NEXT: [[T_2:%.*]] = icmp ule i8 [[START_2]], [[HIGH]] -; CHECK-NEXT: call void @use(i1 [[T_2]]) +; CHECK-NEXT: call void @use(i1 true) ; CHECK-NEXT: [[START_3:%.*]] = add nuw i8 [[START]], 3 ; CHECK-NEXT: [[T_3:%.*]] = icmp ule i8 [[START_3]], [[HIGH]] -; CHECK-NEXT: call void @use(i1 [[T_3]]) +; CHECK-NEXT: call void @use(i1 true) ; CHECK-NEXT: [[START_4:%.*]] = add nuw i8 [[START]], 4 ; CHECK-NEXT: [[T_4:%.*]] = icmp ule i8 [[START_4]], [[HIGH]] -; CHECK-NEXT: call void @use(i1 [[T_4]]) +; CHECK-NEXT: call void @use(i1 true) ; CHECK-NEXT: [[START_5:%.*]] = add nuw i8 [[START]], 5 ; CHECK-NEXT: [[C_5:%.*]] = icmp ule i8 [[START_5]], [[HIGH]] ; CHECK-NEXT: call void @use(i1 [[C_5]]) @@ -121,19 +121,19 @@ define void @test.not.uge.ugt(i8 %start, i8 %low, i8 %high) { ; CHECK-NEXT: ret void ; CHECK: if.end: ; CHECK-NEXT: [[F_0:%.*]] = icmp ugt i8 [[START]], [[HIGH]] -; CHECK-NEXT: call void @use(i1 [[F_0]]) +; CHECK-NEXT: call void @use(i1 false) ; CHECK-NEXT: [[START_1:%.*]] = add nuw i8 [[START]], 1 ; CHECK-NEXT: [[F_1:%.*]] = icmp ugt i8 [[START_1]], [[HIGH]] -; CHECK-NEXT: call void @use(i1 [[F_1]]) +; CHECK-NEXT: call void @use(i1 false) ; CHECK-NEXT: [[START_2:%.*]] = add nuw i8 [[START]], 2 ; CHECK-NEXT: [[F_2:%.*]] = icmp ugt i8 [[START_2]], [[HIGH]] -; CHECK-NEXT: call void @use(i1 [[F_2]]) +; CHECK-NEXT: call void @use(i1 false) ; CHECK-NEXT: [[START_3:%.*]] = add nuw i8 [[START]], 3 ; CHECK-NEXT: [[F_3:%.*]] = icmp ugt i8 [[START_3]], [[HIGH]] -; CHECK-NEXT: call void @use(i1 [[F_3]]) +; CHECK-NEXT: call void @use(i1 false) ; CHECK-NEXT: [[START_4:%.*]] = add nuw i8 [[START]], 4 ; CHECK-NEXT: [[F_4:%.*]] = icmp ugt i8 [[START_4]], [[HIGH]] -; CHECK-NEXT: call void @use(i1 [[F_4]]) +; CHECK-NEXT: call void @use(i1 false) ; CHECK-NEXT: [[START_5:%.*]] = add nuw i8 [[START]], 5 ; CHECK-NEXT: [[C_5:%.*]] = icmp ugt i8 [[START_5]], [[HIGH]] ; CHECK-NEXT: call void @use(i1 [[C_5]]) @@ -184,16 +184,16 @@ define void @test.not.uge.uge(i8 %start, i8 %low, i8 %high) { ; CHECK-NEXT: ret void ; CHECK: if.end: ; CHECK-NEXT: [[F_0:%.*]] = icmp ugt i8 [[START]], [[HIGH]] -; CHECK-NEXT: call void @use(i1 [[F_0]]) +; CHECK-NEXT: call void @use(i1 false) ; CHECK-NEXT: [[START_1:%.*]] = add nuw i8 [[START]], 1 ; CHECK-NEXT: [[F_1:%.*]] = icmp uge i8 [[START_1]], [[HIGH]] -; CHECK-NEXT: call void @use(i1 [[F_1]]) +; CHECK-NEXT: call void @use(i1 false) ; CHECK-NEXT: [[START_2:%.*]] = add nuw i8 [[START]], 2 ; CHECK-NEXT: [[F_2:%.*]] = icmp uge i8 [[START_2]], [[HIGH]] -; CHECK-NEXT: call void @use(i1 [[F_2]]) +; CHECK-NEXT: call void @use(i1 false) ; CHECK-NEXT: [[START_3:%.*]] = add nuw i8 [[START]], 3 ; CHECK-NEXT: [[F_3:%.*]] = icmp uge i8 [[START_3]], [[HIGH]] -; CHECK-NEXT: call void @use(i1 [[F_3]]) +; CHECK-NEXT: call void @use(i1 false) ; CHECK-NEXT: [[START_4:%.*]] = add nuw i8 [[START]], 4 ; CHECK-NEXT: [[C_4:%.*]] = icmp uge i8 [[START_4]], [[HIGH]] ; CHECK-NEXT: call void @use(i1 [[C_4]]) @@ -252,10 +252,10 @@ define void @test.decompose.nonconst(i8 %a, i8 %b, i8 %c, i8 %d) { ; CHECK: if.then.2: ; CHECK-NEXT: [[ADD_0:%.*]] = add nuw i8 [[A]], [[B]] ; CHECK-NEXT: [[T_0:%.*]] = icmp uge i8 [[ADD_0]], [[C]] -; CHECK-NEXT: call void @use(i1 [[T_0]]) +; CHECK-NEXT: call void @use(i1 true) ; CHECK-NEXT: [[ADD_1:%.*]] = add nuw i8 [[A]], [[A]] ; CHECK-NEXT: [[T_1:%.*]] = icmp uge i8 [[ADD_0]], [[C]] -; CHECK-NEXT: call void @use(i1 [[T_1]]) +; CHECK-NEXT: call void @use(i1 true) ; CHECK-NEXT: [[ADD_2:%.*]] = add nuw i8 [[A]], [[D:%.*]] ; CHECK-NEXT: [[C_4:%.*]] = icmp uge i8 [[ADD_2]], [[C]] ; CHECK-NEXT: call void @use(i1 [[C_4]]) diff --git a/test/Transforms/ConstraintElimination/sub-nuw.ll b/test/Transforms/ConstraintElimination/sub-nuw.ll index f440bf7bbff..7912c3c9355 100644 --- a/test/Transforms/ConstraintElimination/sub-nuw.ll +++ b/test/Transforms/ConstraintElimination/sub-nuw.ll @@ -20,10 +20,10 @@ define void @test.not.uge.ult(i8 %start, i8 %low, i8 %high) { ; CHECK-NEXT: call void @use(i1 [[T_2]]) ; CHECK-NEXT: [[START_3:%.*]] = sub nuw i8 [[START]], 3 ; CHECK-NEXT: [[T_3:%.*]] = icmp ult i8 [[START_3]], [[HIGH]] -; CHECK-NEXT: call void @use(i1 [[T_3]]) +; CHECK-NEXT: call void @use(i1 true) ; CHECK-NEXT: [[START_4:%.*]] = sub nuw i8 [[START]], 4 ; CHECK-NEXT: [[C_4:%.*]] = icmp ult i8 [[START_4]], [[HIGH]] -; CHECK-NEXT: call void @use(i1 [[C_4]]) +; CHECK-NEXT: call void @use(i1 true) ; CHECK-NEXT: ret void ; entry: @@ -68,16 +68,16 @@ define void @test.not.uge.ule(i8 %start, i8 %low, i8 %high) { ; CHECK-NEXT: call void @use(i1 [[T_1]]) ; CHECK-NEXT: [[START_2:%.*]] = sub nuw i8 [[START]], 2 ; CHECK-NEXT: [[T_2:%.*]] = icmp ule i8 [[START_2]], [[HIGH]] -; CHECK-NEXT: call void @use(i1 [[T_2]]) +; CHECK-NEXT: call void @use(i1 true) ; CHECK-NEXT: [[START_3:%.*]] = sub nuw i8 [[START]], 3 ; CHECK-NEXT: [[T_3:%.*]] = icmp ule i8 [[START_3]], [[HIGH]] -; CHECK-NEXT: call void @use(i1 [[T_3]]) +; CHECK-NEXT: call void @use(i1 true) ; CHECK-NEXT: [[START_4:%.*]] = sub nuw i8 [[START]], 4 ; CHECK-NEXT: [[T_4:%.*]] = icmp ule i8 [[START_4]], [[HIGH]] -; CHECK-NEXT: call void @use(i1 [[T_4]]) +; CHECK-NEXT: call void @use(i1 true) ; CHECK-NEXT: [[START_5:%.*]] = sub nuw i8 [[START]], 5 ; CHECK-NEXT: [[C_5:%.*]] = icmp ule i8 [[START_5]], [[HIGH]] -; CHECK-NEXT: call void @use(i1 [[C_5]]) +; CHECK-NEXT: call void @use(i1 true) ; CHECK-NEXT: ret void ; entry: @@ -127,16 +127,16 @@ define void @test.not.uge.ugt(i8 %start, i8 %low, i8 %high) { ; CHECK-NEXT: call void @use(i1 [[F_1]]) ; CHECK-NEXT: [[START_2:%.*]] = sub nuw i8 [[START]], 2 ; CHECK-NEXT: [[F_2:%.*]] = icmp ugt i8 [[START_2]], [[HIGH]] -; CHECK-NEXT: call void @use(i1 [[F_2]]) +; CHECK-NEXT: call void @use(i1 false) ; CHECK-NEXT: [[START_3:%.*]] = sub nuw i8 [[START]], 3 ; CHECK-NEXT: [[F_3:%.*]] = icmp ugt i8 [[START_3]], [[HIGH]] -; CHECK-NEXT: call void @use(i1 [[F_3]]) +; CHECK-NEXT: call void @use(i1 false) ; CHECK-NEXT: [[START_4:%.*]] = sub nuw i8 [[START]], 4 ; CHECK-NEXT: [[F_4:%.*]] = icmp ugt i8 [[START_4]], [[HIGH]] -; CHECK-NEXT: call void @use(i1 [[F_4]]) +; CHECK-NEXT: call void @use(i1 false) ; CHECK-NEXT: [[START_5:%.*]] = sub nuw i8 [[START]], 5 ; CHECK-NEXT: [[C_5:%.*]] = icmp ugt i8 [[START_5]], [[HIGH]] -; CHECK-NEXT: call void @use(i1 [[C_5]]) +; CHECK-NEXT: call void @use(i1 false) ; CHECK-NEXT: ret void ; entry: @@ -193,13 +193,13 @@ define void @test.not.uge.uge(i8 %start, i8 %low, i8 %high) { ; CHECK-NEXT: call void @use(i1 [[F_2]]) ; CHECK-NEXT: [[START_3:%.*]] = sub nuw i8 [[START]], 3 ; CHECK-NEXT: [[F_3:%.*]] = icmp uge i8 [[START_3]], [[HIGH]] -; CHECK-NEXT: call void @use(i1 [[F_3]]) +; CHECK-NEXT: call void @use(i1 false) ; CHECK-NEXT: [[START_4:%.*]] = sub nuw i8 [[START]], 4 ; CHECK-NEXT: [[C_4:%.*]] = icmp uge i8 [[START_4]], [[HIGH]] -; CHECK-NEXT: call void @use(i1 [[C_4]]) +; CHECK-NEXT: call void @use(i1 false) ; CHECK-NEXT: [[START_5:%.*]] = sub nuw i8 [[START]], 5 ; CHECK-NEXT: [[C_5:%.*]] = icmp uge i8 [[START_5]], [[HIGH]] -; CHECK-NEXT: call void @use(i1 [[C_5]]) +; CHECK-NEXT: call void @use(i1 false) ; CHECK-NEXT: ret void ; entry: