[PATCH] D104741: [SCEV] Support single-cond range check idiom in applyLoopGuards.
Florian Hahn via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Jun 22 13:17:36 PDT 2021
fhahn created this revision.
fhahn added reviewers: reames, efriedma, mkazantsev.
Herald added a subscriber: hiraditya.
fhahn requested review of this revision.
Herald added a project: LLVM.
This patch extends applyLoopGuards to detect a single-cond range check
idiom that InstCombine generates.
It extends applyLoopGuards to detect (LHS Predicate RHS) which
represents a compare for a range check with a single condition,
as InstCombine may create. A check of the form (-1 + X) u< C
where C in [1, unsigned max) can be de-composed into 2 separate checks:
- X u< (1 + C)
- X u> 0.
In practice, this enables us to correctly compute a tight trip count
bounds for code as in the function below. InstCombine will fold the
minimum iteration check created by LoopRotate with the user check (< 8).
void unsigned_check(short *pred, unsigned width) {
if (width < 8) {
for (int x = 0; x < width; x++)
pred[x] = pred[x] * pred[x];
}
}
As a consequence, LLVM creates dead vector loops for the code above,
e.g. see https://godbolt.org/z/cb8eTcqET
Note that I think it is a bit unfortunate that we still need to detect
such patterns explicitly, but I am not sure if there's a better
alternative at the moment.
Attempt to model using Alive2:
https://alive2.llvm.org/ce/z/_VbcYZ
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D104741
Files:
llvm/lib/Analysis/ScalarEvolution.cpp
llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info.ll
Index: llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info.ll
===================================================================
--- llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info.ll
+++ llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info.ll
@@ -1260,14 +1260,14 @@
; CHECK-NEXT: %N.off = add i32 %N, -1
; CHECK-NEXT: --> (-1 + %N) U: full-set S: full-set
; CHECK-NEXT: %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
-; CHECK-NEXT: --> {0,+,1}<nuw><nsw><%loop> U: [0,-2147483648) S: [0,-2147483648) Exits: (-1 + %N) LoopDispositions: { %loop: Computable }
+; CHECK-NEXT: --> {0,+,1}<nuw><nsw><%loop> U: [0,8) S: [0,8) Exits: (-1 + %N) LoopDispositions: { %loop: Computable }
; CHECK-NEXT: %gep = getelementptr inbounds i16, i16* %pred, i32 %iv
; CHECK-NEXT: --> {%pred,+,2}<nuw><%loop> U: full-set S: full-set Exits: ((2 * (zext i32 (-1 + %N) to i64))<nuw><nsw> + %pred) LoopDispositions: { %loop: Computable }
; CHECK-NEXT: %iv.next = add nuw nsw i32 %iv, 1
-; CHECK-NEXT: --> {1,+,1}<nuw><nsw><%loop> U: [1,-2147483648) S: [1,-2147483648) Exits: %N LoopDispositions: { %loop: Computable }
+; CHECK-NEXT: --> {1,+,1}<nuw><nsw><%loop> U: [1,9) S: [1,9) Exits: %N LoopDispositions: { %loop: Computable }
; CHECK-NEXT: Determining loop execution counts for: @optimized_range_check_unsigned
; CHECK-NEXT: Loop %loop: backedge-taken count is (-1 + %N)
-; CHECK-NEXT: Loop %loop: max backedge-taken count is -1
+; CHECK-NEXT: Loop %loop: max backedge-taken count is 7
; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is (-1 + %N)
; CHECK-NEXT: Predicates:
; CHECK: Loop %loop: Trip multiple is 1
Index: llvm/lib/Analysis/ScalarEvolution.cpp
===================================================================
--- llvm/lib/Analysis/ScalarEvolution.cpp
+++ llvm/lib/Analysis/ScalarEvolution.cpp
@@ -13652,11 +13652,35 @@
}
}
- if (!isa<SCEVUnknown>(LHS)) {
+ if (!isa<SCEVUnknown>(LHS) && isa<SCEVUnknown>(RHS)) {
std::swap(LHS, RHS);
Predicate = CmpInst::getSwappedPredicate(Predicate);
}
+ // Check if (LHS Predicate RHS) represent a compare for a range check with a
+ // single condition, as InstCombine may create. A check of the form
+ // (-1 + X) u< C where C in [1, unsigned max) can be de-composed into 2
+ // separate checks:
+ // * X u< (1 + C)
+ // * X u> 0.
+ if (auto *AddExpr = dyn_cast<SCEVAddExpr>(LHS)) {
+ auto *C1 = dyn_cast<SCEVConstant>(AddExpr->getOperand(0));
+ auto *C2 = dyn_cast<SCEVConstant>(RHS);
+ if (AddExpr->getNumOperands() == 2 && C1 &&
+ C1->getValue()->isMinusOne() && Predicate == CmpInst::ICMP_ULT &&
+ C2 && !C2->getAPInt().isMaxValue() && !C2->getAPInt().isNullValue()) {
+ auto *LHSUnknown = dyn_cast<SCEVUnknown>(AddExpr->getOperand(1));
+ if (LHSUnknown) {
+ auto I = RewriteMap.find(LHSUnknown->getValue());
+ const SCEV *RewrittenLHS = I != RewriteMap.end() ? I->second : LHS;
+ RewriteMap[LHSUnknown->getValue()] =
+ getUMaxExpr(getOne(RHS->getType()),
+ getUMinExpr(RewrittenLHS,
+ getAddExpr(RHS, getOne(RHS->getType()))));
+ return;
+ }
+ }
+ }
// For now, limit to conditions that provide information about unknown
// expressions. RHS also cannot contain add recurrences.
auto *LHSUnknown = dyn_cast<SCEVUnknown>(LHS);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D104741.353763.patch
Type: text/x-patch
Size: 3544 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210622/844e64ea/attachment.bin>
More information about the llvm-commits
mailing list