[llvm] 4ccdab3 - [ConstraintElim] Use isKnownNonNegative for condition transfer. (#72879)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 21 02:09:42 PST 2023
Author: Florian Hahn
Date: 2023-11-21T10:09:35Z
New Revision: 4ccdab363618afbfd6958c4351c5a9ab3e37dcc8
URL: https://github.com/llvm/llvm-project/commit/4ccdab363618afbfd6958c4351c5a9ab3e37dcc8
DIFF: https://github.com/llvm/llvm-project/commit/4ccdab363618afbfd6958c4351c5a9ab3e37dcc8.diff
LOG: [ConstraintElim] Use isKnownNonNegative for condition transfer. (#72879)
Use isKnownNonNegative for information transfer. This can improve
results, in cases where ValueTracking can infer additional non-negative
info, e.g. for phi nodes.
This allows simplifying the check from
https://github.com/llvm/llvm-project/issues/63126 by
ConstraintElimination. It is also simplified by IndVarSimplify now; note
the changes in llvm/test/Transforms/PhaseOrdering/loop-access-checks.ll,
due to this now being simplified earlier.
Added:
Modified:
llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
llvm/test/Transforms/ConstraintElimination/transfer-signed-facts-to-unsigned-is-known-non-negative.ll
llvm/test/Transforms/ConstraintElimination/transfer-unsigned-facts-to-signed-is-known-non-negative.ll
llvm/test/Transforms/PhaseOrdering/loop-access-checks.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index 8ec2ee47db1b70c..7aadd810c1da387 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -773,6 +773,10 @@ bool ConstraintInfo::doesHold(CmpInst::Predicate Pred, Value *A,
void ConstraintInfo::transferToOtherSystem(
CmpInst::Predicate Pred, Value *A, Value *B, unsigned NumIn,
unsigned NumOut, SmallVectorImpl<StackEntry> &DFSInStack) {
+ auto IsKnownNonNegative = [this](Value *V) {
+ return doesHold(CmpInst::ICMP_SGE, V, ConstantInt::get(V->getType(), 0)) ||
+ isKnownNonNegative(V, DL, /*Depth=*/MaxAnalysisRecursionDepth - 1);
+ };
// Check if we can combine facts from the signed and unsigned systems to
// derive additional facts.
if (!A->getType()->isIntegerTy())
@@ -786,7 +790,7 @@ void ConstraintInfo::transferToOtherSystem(
case CmpInst::ICMP_ULT:
case CmpInst::ICMP_ULE:
// If B is a signed positive constant, then A >=s 0 and A <s (or <=s) B.
- if (doesHold(CmpInst::ICMP_SGE, B, ConstantInt::get(B->getType(), 0))) {
+ if (IsKnownNonNegative(B)) {
addFact(CmpInst::ICMP_SGE, A, ConstantInt::get(B->getType(), 0), NumIn,
NumOut, DFSInStack);
addFact(CmpInst::getSignedPredicate(Pred), A, B, NumIn, NumOut,
@@ -796,7 +800,7 @@ void ConstraintInfo::transferToOtherSystem(
case CmpInst::ICMP_UGE:
case CmpInst::ICMP_UGT:
// If A is a signed positive constant, then B >=s 0 and A >s (or >=s) B.
- if (doesHold(CmpInst::ICMP_SGE, A, ConstantInt::get(B->getType(), 0))) {
+ if (IsKnownNonNegative(A)) {
addFact(CmpInst::ICMP_SGE, B, ConstantInt::get(B->getType(), 0), NumIn,
NumOut, DFSInStack);
addFact(CmpInst::getSignedPredicate(Pred), A, B, NumIn, NumOut,
@@ -804,22 +808,21 @@ void ConstraintInfo::transferToOtherSystem(
}
break;
case CmpInst::ICMP_SLT:
- if (doesHold(CmpInst::ICMP_SGE, A, ConstantInt::get(B->getType(), 0)))
+ if (IsKnownNonNegative(A))
addFact(CmpInst::ICMP_ULT, A, B, NumIn, NumOut, DFSInStack);
break;
case CmpInst::ICMP_SGT: {
if (doesHold(CmpInst::ICMP_SGE, B, ConstantInt::get(B->getType(), -1)))
addFact(CmpInst::ICMP_UGE, A, ConstantInt::get(B->getType(), 0), NumIn,
NumOut, DFSInStack);
- if (doesHold(CmpInst::ICMP_SGE, B, ConstantInt::get(B->getType(), 0)))
+ if (IsKnownNonNegative(B))
addFact(CmpInst::ICMP_UGT, A, B, NumIn, NumOut, DFSInStack);
break;
}
case CmpInst::ICMP_SGE:
- if (doesHold(CmpInst::ICMP_SGE, B, ConstantInt::get(B->getType(), 0))) {
+ if (IsKnownNonNegative(B))
addFact(CmpInst::ICMP_UGE, A, B, NumIn, NumOut, DFSInStack);
- }
break;
}
}
diff --git a/llvm/test/Transforms/ConstraintElimination/transfer-signed-facts-to-unsigned-is-known-non-negative.ll b/llvm/test/Transforms/ConstraintElimination/transfer-signed-facts-to-unsigned-is-known-non-negative.ll
index e75cf70b71af0d6..efb218ff099fb02 100644
--- a/llvm/test/Transforms/ConstraintElimination/transfer-signed-facts-to-unsigned-is-known-non-negative.ll
+++ b/llvm/test/Transforms/ConstraintElimination/transfer-signed-facts-to-unsigned-is-known-non-negative.ll
@@ -12,16 +12,11 @@ define void @iv_known_non_negative_constant_trip_count() {
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[IV]], 2
; CHECK-NEXT: br i1 [[CMP]], label [[LOOP_LATCH]], label [[EXIT_1:%.*]]
; CHECK: loop.latch:
-; CHECK-NEXT: [[T_1:%.*]] = icmp ugt i8 2, [[IV]]
-; CHECK-NEXT: call void @use(i1 [[T_1]])
-; CHECK-NEXT: [[T_2:%.*]] = icmp sgt i8 2, [[IV]]
-; CHECK-NEXT: call void @use(i1 [[T_2]])
-; CHECK-NEXT: [[F_1:%.*]] = icmp ule i8 2, [[IV]]
-; CHECK-NEXT: call void @use(i1 [[F_1]])
-; CHECK-NEXT: [[F_2:%.*]] = icmp sle i8 2, [[IV]]
-; CHECK-NEXT: call void @use(i1 [[F_2]])
-; CHECK-NEXT: [[C_0:%.*]] = icmp ugt i8 [[IV]], 2
-; CHECK-NEXT: call void @use(i1 [[C_0]])
+; CHECK-NEXT: call void @use(i1 true)
+; CHECK-NEXT: call void @use(i1 true)
+; CHECK-NEXT: call void @use(i1 false)
+; CHECK-NEXT: call void @use(i1 false)
+; CHECK-NEXT: call void @use(i1 false)
; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i8 [[IV]], 1
; CHECK-NEXT: br label [[LOOP_HEADER]]
; CHECK: exit.1:
@@ -112,11 +107,9 @@ define void @iv_known_non_negative_variable_trip_count(i8 %N) {
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[IV]], [[N:%.*]]
; CHECK-NEXT: br i1 [[CMP]], label [[LOOP_LATCH]], label [[EXIT_1:%.*]]
; CHECK: loop.latch:
-; CHECK-NEXT: [[T_1:%.*]] = icmp ugt i8 [[N]], [[IV]]
-; CHECK-NEXT: call void @use(i1 [[T_1]])
; CHECK-NEXT: call void @use(i1 true)
-; CHECK-NEXT: [[F_1:%.*]] = icmp ule i8 [[N]], [[IV]]
-; CHECK-NEXT: call void @use(i1 [[F_1]])
+; CHECK-NEXT: call void @use(i1 true)
+; CHECK-NEXT: call void @use(i1 false)
; CHECK-NEXT: call void @use(i1 false)
; CHECK-NEXT: [[C_0:%.*]] = icmp ugt i8 [[IV]], 2
; CHECK-NEXT: call void @use(i1 [[C_0]])
@@ -208,16 +201,11 @@ define void @iv_known_non_negative_iv_constant_trip_count_sgt() {
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 2, [[IV]]
; CHECK-NEXT: br i1 [[CMP]], label [[LOOP_LATCH]], label [[EXIT_1:%.*]]
; CHECK: loop.latch:
-; CHECK-NEXT: [[T_1:%.*]] = icmp ugt i8 [[IV]], 2
-; CHECK-NEXT: call void @use(i1 [[T_1]])
-; CHECK-NEXT: [[T_2:%.*]] = icmp sgt i8 2, [[IV]]
-; CHECK-NEXT: call void @use(i1 [[T_2]])
-; CHECK-NEXT: [[F_1:%.*]] = icmp ule i8 2, [[IV]]
-; CHECK-NEXT: call void @use(i1 [[F_1]])
-; CHECK-NEXT: [[F_2:%.*]] = icmp sle i8 2, [[IV]]
-; CHECK-NEXT: call void @use(i1 [[F_2]])
-; CHECK-NEXT: [[C_0:%.*]] = icmp ugt i8 [[IV]], 2
-; CHECK-NEXT: call void @use(i1 [[C_0]])
+; CHECK-NEXT: call void @use(i1 false)
+; CHECK-NEXT: call void @use(i1 true)
+; CHECK-NEXT: call void @use(i1 false)
+; CHECK-NEXT: call void @use(i1 false)
+; CHECK-NEXT: call void @use(i1 false)
; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i8 [[IV]], 1
; CHECK-NEXT: br label [[LOOP_HEADER]]
; CHECK: exit.1:
@@ -258,11 +246,9 @@ define void @iv_known_non_negative_iv_variable_trip_count_sgt(i8 %N) {
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[N:%.*]], [[IV]]
; CHECK-NEXT: br i1 [[CMP]], label [[LOOP_LATCH]], label [[EXIT_1:%.*]]
; CHECK: loop.latch:
-; CHECK-NEXT: [[T_1:%.*]] = icmp ugt i8 [[IV]], [[N]]
-; CHECK-NEXT: call void @use(i1 [[T_1]])
+; CHECK-NEXT: call void @use(i1 false)
; CHECK-NEXT: call void @use(i1 true)
-; CHECK-NEXT: [[F_1:%.*]] = icmp ule i8 [[N]], [[IV]]
-; CHECK-NEXT: call void @use(i1 [[F_1]])
+; CHECK-NEXT: call void @use(i1 false)
; CHECK-NEXT: call void @use(i1 false)
; CHECK-NEXT: [[C_0:%.*]] = icmp ugt i8 [[IV]], 2
; CHECK-NEXT: call void @use(i1 [[C_0]])
@@ -355,16 +341,11 @@ define void @iv_known_non_negative_iv_constant_trip_count_sge() {
; CHECK-NEXT: [[CMP:%.*]] = icmp sge i8 2, [[IV]]
; CHECK-NEXT: br i1 [[CMP]], label [[LOOP_LATCH]], label [[EXIT_1:%.*]]
; CHECK: loop.latch:
-; CHECK-NEXT: [[T_1:%.*]] = icmp uge i8 2, [[IV]]
-; CHECK-NEXT: call void @use(i1 [[T_1]])
-; CHECK-NEXT: [[T_2:%.*]] = icmp sge i8 2, [[IV]]
-; CHECK-NEXT: call void @use(i1 [[T_2]])
-; CHECK-NEXT: [[F_1:%.*]] = icmp ult i8 2, [[IV]]
-; CHECK-NEXT: call void @use(i1 [[F_1]])
-; CHECK-NEXT: [[F_2:%.*]] = icmp slt i8 2, [[IV]]
-; CHECK-NEXT: call void @use(i1 [[F_2]])
-; CHECK-NEXT: [[C_0:%.*]] = icmp ugt i8 [[IV]], 2
-; CHECK-NEXT: call void @use(i1 [[C_0]])
+; CHECK-NEXT: call void @use(i1 true)
+; CHECK-NEXT: call void @use(i1 true)
+; CHECK-NEXT: call void @use(i1 false)
+; CHECK-NEXT: call void @use(i1 false)
+; CHECK-NEXT: call void @use(i1 false)
; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i8 [[IV]], 1
; CHECK-NEXT: br label [[LOOP_HEADER]]
; CHECK: exit.1:
@@ -405,11 +386,9 @@ define void @iv_known_non_negative_iv_variable_trip_count_sge(i8 %N) {
; CHECK-NEXT: [[CMP:%.*]] = icmp sge i8 [[N:%.*]], [[IV]]
; CHECK-NEXT: br i1 [[CMP]], label [[LOOP_LATCH]], label [[EXIT_1:%.*]]
; CHECK: loop.latch:
-; CHECK-NEXT: [[T_1:%.*]] = icmp uge i8 [[N]], [[IV]]
-; CHECK-NEXT: call void @use(i1 [[T_1]])
; CHECK-NEXT: call void @use(i1 true)
-; CHECK-NEXT: [[F_1:%.*]] = icmp ult i8 [[N]], [[IV]]
-; CHECK-NEXT: call void @use(i1 [[F_1]])
+; CHECK-NEXT: call void @use(i1 true)
+; CHECK-NEXT: call void @use(i1 false)
; CHECK-NEXT: call void @use(i1 false)
; CHECK-NEXT: [[C_0:%.*]] = icmp ugt i8 [[IV]], 2
; CHECK-NEXT: call void @use(i1 [[C_0]])
diff --git a/llvm/test/Transforms/ConstraintElimination/transfer-unsigned-facts-to-signed-is-known-non-negative.ll b/llvm/test/Transforms/ConstraintElimination/transfer-unsigned-facts-to-signed-is-known-non-negative.ll
index a25d532774fb91c..b127361ff024450 100644
--- a/llvm/test/Transforms/ConstraintElimination/transfer-unsigned-facts-to-signed-is-known-non-negative.ll
+++ b/llvm/test/Transforms/ConstraintElimination/transfer-unsigned-facts-to-signed-is-known-non-negative.ll
@@ -55,11 +55,9 @@ define void @iv_known_non_negative_iv_variable_trip_count_uge(i8 %N) {
; CHECK-NEXT: br i1 [[CMP]], label [[LOOP_LATCH]], label [[EXIT_1:%.*]]
; CHECK: loop.latch:
; CHECK-NEXT: call void @use(i1 true)
-; CHECK-NEXT: [[T_2:%.*]] = icmp sge i8 [[IV]], [[N]]
-; CHECK-NEXT: call void @use(i1 [[T_2]])
+; CHECK-NEXT: call void @use(i1 true)
+; CHECK-NEXT: call void @use(i1 false)
; CHECK-NEXT: call void @use(i1 false)
-; CHECK-NEXT: [[F_2:%.*]] = icmp slt i8 [[IV]], [[N]]
-; CHECK-NEXT: call void @use(i1 [[F_2]])
; CHECK-NEXT: [[C_0:%.*]] = icmp ugt i8 [[IV]], 2
; CHECK-NEXT: call void @use(i1 [[C_0]])
; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i8 [[IV]], 1
@@ -147,11 +145,9 @@ define void @iv_known_non_negative_iv_variable_trip_count_ugt(i8 %N) {
; CHECK-NEXT: br i1 [[CMP]], label [[LOOP_LATCH]], label [[EXIT_1:%.*]]
; CHECK: loop.latch:
; CHECK-NEXT: call void @use(i1 true)
-; CHECK-NEXT: [[T_2:%.*]] = icmp sgt i8 [[IV]], [[N]]
-; CHECK-NEXT: call void @use(i1 [[T_2]])
+; CHECK-NEXT: call void @use(i1 true)
+; CHECK-NEXT: call void @use(i1 false)
; CHECK-NEXT: call void @use(i1 false)
-; CHECK-NEXT: [[F_2:%.*]] = icmp slt i8 [[IV]], [[N]]
-; CHECK-NEXT: call void @use(i1 [[F_2]])
; CHECK-NEXT: [[C_0:%.*]] = icmp ugt i8 [[IV]], 2
; CHECK-NEXT: call void @use(i1 [[C_0]])
; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i8 [[IV]], 1
diff --git a/llvm/test/Transforms/PhaseOrdering/loop-access-checks.ll b/llvm/test/Transforms/PhaseOrdering/loop-access-checks.ll
index e1eac5f80485494..dbe29a071092e40 100644
--- a/llvm/test/Transforms/PhaseOrdering/loop-access-checks.ll
+++ b/llvm/test/Transforms/PhaseOrdering/loop-access-checks.ll
@@ -280,18 +280,18 @@ define void @loop_with_signed_induction(ptr noundef nonnull align 8 dereferencea
; CHECK-NEXT: [[SUB_PTR_SUB_I_I:%.*]] = sub i64 [[SUB_PTR_LHS_CAST_I_I]], [[SUB_PTR_RHS_CAST_I_I]]
; CHECK-NEXT: [[SUB_PTR_DIV_I_I:%.*]] = ashr exact i64 [[SUB_PTR_SUB_I_I]], 3
; CHECK-NEXT: [[CMP9:%.*]] = icmp sgt i64 [[SUB_PTR_DIV_I_I]], 0
-; CHECK-NEXT: br i1 [[CMP9]], label [[OPERATOR_ACC_EXIT:%.*]], label [[FOR_COND_CLEANUP:%.*]]
+; CHECK-NEXT: br i1 [[CMP9]], label [[FOR_BODY:%.*]], label [[FOR_COND_CLEANUP:%.*]]
; CHECK: for.cond.cleanup:
; CHECK-NEXT: ret void
-; CHECK: operator_acc.exit:
-; CHECK-NEXT: [[I_010:%.*]] = phi i64 [ [[INC:%.*]], [[OPERATOR_ACC_EXIT]] ], [ 0, [[ENTRY:%.*]] ]
+; CHECK: for.body:
+; CHECK-NEXT: [[I_010:%.*]] = phi i64 [ [[INC:%.*]], [[FOR_BODY]] ], [ 0, [[ENTRY:%.*]] ]
; CHECK-NEXT: [[ADD_PTR_I:%.*]] = getelementptr inbounds double, ptr [[TMP1]], i64 [[I_010]]
; CHECK-NEXT: [[TMP2:%.*]] = load double, ptr [[ADD_PTR_I]], align 8, !tbaa [[TBAA6:![0-9]+]]
; CHECK-NEXT: [[ADD:%.*]] = fadd double [[TMP2]], 1.000000e+00
; CHECK-NEXT: store double [[ADD]], ptr [[ADD_PTR_I]], align 8, !tbaa [[TBAA6]]
; CHECK-NEXT: [[INC]] = add nuw nsw i64 [[I_010]], 1
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i64 [[INC]], [[SUB_PTR_DIV_I_I]]
-; CHECK-NEXT: br i1 [[CMP]], label [[OPERATOR_ACC_EXIT]], label [[FOR_COND_CLEANUP]]
+; CHECK-NEXT: br i1 [[CMP]], label [[FOR_BODY]], label [[FOR_COND_CLEANUP]]
;
entry:
%vec.addr = alloca ptr, align 8
More information about the llvm-commits
mailing list