From 9d31d1c214e0f010fbb12ecb1780ac27dab6e435 Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Sat, 8 Oct 2022 11:03:44 +0100 Subject: [PATCH] [ConstraintElimination] Use logic from 3771310eed for queries only. The logic added in 3771310eed was placed sub-optimally. Applying the transform in ::getConstraint meant that it would also impact conditions that are added to the system by the signed <-> unsigned transfer logic. This meant we failed to add some signed facts to the signed system. To make sure we still add as many useful facts to the signed/unsigned systems, move the logic to the point where we query the system. --- .../Scalar/ConstraintElimination.cpp | 31 ++++++++++--------- .../signed-query-unsigned-system.ll | 2 +- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp index 5feab3b56d7f..2402802cfadc 100644 --- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp +++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp @@ -111,11 +111,7 @@ class ConstraintInfo { ConstraintSystem UnsignedCS; ConstraintSystem SignedCS; - const DataLayout &DL; - public: - ConstraintInfo(const DataLayout &DL) : DL(DL) {} - DenseMap &getValue2Index(bool Signed) { return Signed ? SignedValue2Index : UnsignedValue2Index; } @@ -331,14 +327,6 @@ ConstraintInfo::getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1, Pred != CmpInst::ICMP_SLE && Pred != CmpInst::ICMP_SLT) return {}; - // If both operands are known to be non-negative, change signed predicates to - // unsigned ones. This increases the reasoning effectiveness in combination - // with the signed <-> unsigned transfer logic. - if (CmpInst::isSigned(Pred) && - isKnownNonNegative(Op0, DL, /*Depth=*/MaxAnalysisRecursionDepth - 1) && - isKnownNonNegative(Op1, DL, /*Depth=*/MaxAnalysisRecursionDepth - 1)) - Pred = CmpInst::getUnsignedPredicate(Pred); - SmallVector Preconditions; bool IsSigned = CmpInst::isSigned(Pred); auto &Value2Index = getValue2Index(IsSigned); @@ -754,7 +742,7 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT) { bool Changed = false; DT.updateDFSNumbers(); - ConstraintInfo Info(F.getParent()->getDataLayout()); + ConstraintInfo Info; State S(DT); // First, collect conditions implied by branches and blocks with their @@ -837,7 +825,22 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT) { LLVM_DEBUG(dbgs() << "Checking " << *Cmp << "\n"); SmallVector NewVariables; - auto R = Info.getConstraint(Cmp, NewVariables); + CmpInst::Predicate Pred = Cmp->getPredicate(); + Value *A = Cmp->getOperand(0); + Value *B = Cmp->getOperand(1); + const DataLayout &DL = Cmp->getModule()->getDataLayout(); + + // If both operands are known to be non-negative, change signed + // predicates to unsigned ones. This increases the reasoning + // effectiveness in combination with the signed <-> unsigned transfer + // logic. + if (CmpInst::isSigned(Pred) && + isKnownNonNegative(A, DL, + /*Depth=*/MaxAnalysisRecursionDepth - 1) && + isKnownNonNegative(B, DL, /*Depth=*/MaxAnalysisRecursionDepth - 1)) + Pred = CmpInst::getUnsignedPredicate(Pred); + + auto R = Info.getConstraint(Pred, A, B, NewVariables); if (R.IsEq || R.empty() || !NewVariables.empty() || !R.isValid(Info)) continue; diff --git a/llvm/test/Transforms/ConstraintElimination/signed-query-unsigned-system.ll b/llvm/test/Transforms/ConstraintElimination/signed-query-unsigned-system.ll index a7fa432ad20e..a78f17554158 100644 --- a/llvm/test/Transforms/ConstraintElimination/signed-query-unsigned-system.ll +++ b/llvm/test/Transforms/ConstraintElimination/signed-query-unsigned-system.ll @@ -149,7 +149,7 @@ define i1 @sge_neg_1_sge_0_known(i8 %a) { ; CHECK-NEXT: [[A_NE_0:%.*]] = icmp sge i16 [[EXT]], 0 ; CHECK-NEXT: call void @llvm.assume(i1 [[A_NE_0]]) ; CHECK-NEXT: [[T:%.*]] = icmp sge i16 [[EXT]], -1 -; CHECK-NEXT: ret i1 [[T]] +; CHECK-NEXT: ret i1 true ; %ext = zext i8 %a to i16 %a.ne.0 = icmp sge i16 %ext, 0