From 4bbee17ecb00bcdf43a43650a64172a4d9eb130c Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Mon, 7 Mar 2022 13:33:50 +0000 Subject: [PATCH] [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 --- llvm/lib/Transforms/Scalar/ConstraintElimination.cpp | 9 +++++---- llvm/test/Transforms/ConstraintElimination/add-nuw.ll | 10 +++++----- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp index c094b5c..eb92c11a 100644 --- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp +++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp @@ -153,9 +153,9 @@ decompose(Value *V, SmallVector &Preconditions, } if (auto *CI = dyn_cast(V)) { - if (CI->isNegative() || CI->uge(MaxConstraintValue)) + if (CI->uge(MaxConstraintValue)) return {}; - return {{CI->getSExtValue(), nullptr}}; + return {{CI->getZExtValue(), nullptr}}; } auto *GEP = dyn_cast(V); if (GEP && GEP->getNumOperands() == 2 && GEP->isInBounds()) { @@ -208,8 +208,9 @@ decompose(Value *V, SmallVector &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 5276c07..bf79a52 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: -- 2.7.4