[llvm] [LoopFlatten] Use loop versioning when overflow can't be disproven (PR #78576)
John Brawn via llvm-commits
llvm-commits at lists.llvm.org
Thu Jan 25 05:39:15 PST 2024
================
@@ -926,18 +936,53 @@ static bool FlattenLoopPair(FlattenInfo &FI, DominatorTree *DT, LoopInfo *LI,
// variable might overflow. In this case, we need to version the loop, and
// select the original version at runtime if the iteration space is too
// large.
- // TODO: We currently don't version the loop.
OverflowResult OR = checkOverflow(FI, DT, AC);
if (OR == OverflowResult::AlwaysOverflowsHigh ||
OR == OverflowResult::AlwaysOverflowsLow) {
LLVM_DEBUG(dbgs() << "Multiply would always overflow, so not profitable\n");
return false;
} else if (OR == OverflowResult::MayOverflow) {
- LLVM_DEBUG(dbgs() << "Multiply might overflow, not flattening\n");
- return false;
+ Module *M = FI.OuterLoop->getHeader()->getParent()->getParent();
+ const DataLayout &DL = M->getDataLayout();
+ if (!VersionLoops) {
+ LLVM_DEBUG(dbgs() << "Multiply might overflow, not flattening\n");
+ return false;
+ } else if (!DL.isLegalInteger(
+ FI.OuterTripCount->getType()->getScalarSizeInBits())) {
+ // If the trip count type isn't legal then it won't be possible to check
+ // for overflow using only a single multiply instruction, so don't
+ // flatten.
+ LLVM_DEBUG(
+ dbgs() << "Can't check overflow efficiently, not flattening\n");
+ return false;
+ }
+ LLVM_DEBUG(dbgs() << "Multiply might overflow, versioning loop\n");
+
+ // Version the loop. The overflow check isn't a runtime pointer check, so we
----------------
john-brawn-arm wrote:
I've expanded on the comment and also added an assert checking that the branch condition is false as expected.
https://github.com/llvm/llvm-project/pull/78576
More information about the llvm-commits
mailing list