From 589afe8d33b8c5e75f388b515ca295dd57c1cb27 Mon Sep 17 00:00:00 2001 From: Ramkumar Ramachandra Date: Sat, 11 Jan 2025 09:14:27 +0000 Subject: [PATCH 1/2] VT: teach a special-case optz about samesign There is a narrow special-case in isImpliedCondICmps that can benefit from being taught about samesign. Since it costs us nothing to implement it, teach it about samesign, for completeness. This patch marks the completion of the effort to teach ValueTracking about samesign. --- llvm/lib/Analysis/ValueTracking.cpp | 6 ++++-- .../ValueTracking/implied-condition-samesign.ll | 10 ++-------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp index 92338d33b27a4..2491493c993cb 100644 --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -9495,7 +9495,8 @@ isImpliedCondICmps(const ICmpInst *LHS, CmpPredicate RPred, const Value *R0, // must be positive if X >= Y and no overflow". // Take SGT as an example: L0:x > L1:y and C >= 0 // ==> R0:(x -nsw y) < R1:(-C) is false - if ((LPred == ICmpInst::ICMP_SGT || LPred == ICmpInst::ICMP_SGE) && + if ((ICmpInst::isSigned(LPred) || LPred.hasSameSign()) && + (ICmpInst::isGE(LPred) || ICmpInst::isGT(LPred)) && match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) { if (match(R1, m_NonPositive()) && isImpliedCondMatchingOperands(LPred, RPred) == false) @@ -9504,7 +9505,8 @@ isImpliedCondICmps(const ICmpInst *LHS, CmpPredicate RPred, const Value *R0, // Take SLT as an example: L0:x < L1:y and C <= 0 // ==> R0:(x -nsw y) < R1:(-C) is true - if ((LPred == ICmpInst::ICMP_SLT || LPred == ICmpInst::ICMP_SLE) && + if ((ICmpInst::isSigned(LPred) || LPred.hasSameSign()) && + (ICmpInst::isLE(LPred) || ICmpInst::isLT(LPred)) && match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) { if (match(R1, m_NonNegative()) && isImpliedCondMatchingOperands(LPred, RPred) == true) diff --git a/llvm/test/Analysis/ValueTracking/implied-condition-samesign.ll b/llvm/test/Analysis/ValueTracking/implied-condition-samesign.ll index 546ff2d77d86e..35cfadaa2965a 100644 --- a/llvm/test/Analysis/ValueTracking/implied-condition-samesign.ll +++ b/llvm/test/Analysis/ValueTracking/implied-condition-samesign.ll @@ -207,10 +207,7 @@ define i32 @gt_sub_nsw(i32 %x, i32 %y) { ; CHECK: [[TAKEN]]: ; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[X]], [[Y]] ; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[SUB]], 1 -; CHECK-NEXT: [[NEG:%.*]] = xor i32 [[SUB]], -1 -; CHECK-NEXT: [[ABSCOND:%.*]] = icmp samesign ult i32 [[SUB]], -1 -; CHECK-NEXT: [[ABS:%.*]] = select i1 [[ABSCOND]], i32 [[NEG]], i32 [[ADD]] -; CHECK-NEXT: ret i32 [[ABS]] +; CHECK-NEXT: ret i32 [[ADD]] ; CHECK: [[END]]: ; CHECK-NEXT: ret i32 0 ; @@ -239,10 +236,7 @@ define i32 @ge_sub_nsw(i32 %x, i32 %y) { ; CHECK: [[TAKEN]]: ; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[X]], [[Y]] ; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[SUB]], 1 -; CHECK-NEXT: [[NEG:%.*]] = xor i32 [[SUB]], -1 -; CHECK-NEXT: [[ABSCOND:%.*]] = icmp samesign ult i32 [[SUB]], -1 -; CHECK-NEXT: [[ABS:%.*]] = select i1 [[ABSCOND]], i32 [[NEG]], i32 [[ADD]] -; CHECK-NEXT: ret i32 [[ABS]] +; CHECK-NEXT: ret i32 [[ADD]] ; CHECK: [[END]]: ; CHECK-NEXT: ret i32 0 ; From 98efe553d481055bc619725d46cb094e4b4510e2 Mon Sep 17 00:00:00 2001 From: Ramkumar Ramachandra Date: Sat, 11 Jan 2025 14:40:15 +0000 Subject: [PATCH 2/2] VT: use CmpPredicate::getMatching; address review --- llvm/lib/Analysis/ValueTracking.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp index 2491493c993cb..d89b88ce5e390 100644 --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -9495,8 +9495,8 @@ isImpliedCondICmps(const ICmpInst *LHS, CmpPredicate RPred, const Value *R0, // must be positive if X >= Y and no overflow". // Take SGT as an example: L0:x > L1:y and C >= 0 // ==> R0:(x -nsw y) < R1:(-C) is false - if ((ICmpInst::isSigned(LPred) || LPred.hasSameSign()) && - (ICmpInst::isGE(LPred) || ICmpInst::isGT(LPred)) && + if ((CmpPredicate::getMatching(LPred, ICmpInst::ICMP_SGT) || + CmpPredicate::getMatching(LPred, ICmpInst::ICMP_SGE)) && match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) { if (match(R1, m_NonPositive()) && isImpliedCondMatchingOperands(LPred, RPred) == false) @@ -9505,8 +9505,8 @@ isImpliedCondICmps(const ICmpInst *LHS, CmpPredicate RPred, const Value *R0, // Take SLT as an example: L0:x < L1:y and C <= 0 // ==> R0:(x -nsw y) < R1:(-C) is true - if ((ICmpInst::isSigned(LPred) || LPred.hasSameSign()) && - (ICmpInst::isLE(LPred) || ICmpInst::isLT(LPred)) && + if ((CmpPredicate::getMatching(LPred, ICmpInst::ICMP_SLT) || + CmpPredicate::getMatching(LPred, ICmpInst::ICMP_SLE)) && match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) { if (match(R1, m_NonNegative()) && isImpliedCondMatchingOperands(LPred, RPred) == true)