[llvm] [LoopUnroll][AArch64] Add separate upper-bound limit for loops with early exits (PR #205102)
Igor Kirillov via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 22 06:18:47 PDT 2026
https://github.com/igogo-x86 created https://github.com/llvm/llvm-project/pull/205102
UnrollingPreferences::MaxUpperBound does not distinguish loops with early exits from loops that run exactly 0 or N times (N <= MaxUpperBound).
Bounded (upper-bound) unrolling keeps all but the last loop test, so for loops that may exit on any iteration it turns a single in-loop branch into N branches. When such loops usually exit early, unrolling replaces a single well-predicted branch with several rarely-taken ones, adding branch-predictor and code-size pressure for little benefit.
Introduce UnrollingPreferences::MaxUpperBoundWithEarlyExits and the -unroll-max-upperbound-with-early-exits option to bound such loops separately. It defaults to MaxUpperBound, so behavior is unchanged unless a target or the user lowers it. Loops known to run either the max count or zero times (MaxOrZero) keep using MaxUpperBound.
Set the AArch64 limit to 5 to avoid bloating small early-exit loops such as loops that count the number of bytes in a variable-length integer encoding.
>From d607f9654fae4244d8a05a0f077470d88b88e0d7 Mon Sep 17 00:00:00 2001
From: Igor Kirillov <igor.kirillov at arm.com>
Date: Fri, 19 Jun 2026 11:31:27 +0000
Subject: [PATCH] [LoopUnroll][AArch64] Add separate upper-bound limit for
loops with early exits
UnrollingPreferences::MaxUpperBound does not distinguish loops with early
exits from loops that run exactly 0 or N times (N <= MaxUpperBound).
Bounded (upper-bound) unrolling keeps all but the last loop test, so for
loops that may exit on any iteration it turns a single in-loop branch into
N branches. When such loops usually exit early, unrolling replaces a single
well-predicted branch with several rarely-taken ones, adding
branch-predictor and code-size pressure for little benefit.
Introduce UnrollingPreferences::MaxUpperBoundWithEarlyExits and the
-unroll-max-upperbound-with-early-exits option to bound such loops
separately. It defaults to MaxUpperBound, so behavior is unchanged unless a
target or the user lowers it. Loops known to run either the max count or
zero times (MaxOrZero) keep using MaxUpperBound.
Set the AArch64 limit to 5 to avoid bloating small early-exit loops such as
loops that count the number of bytes in a variable-length integer encoding.
---
.../llvm/Analysis/TargetTransformInfo.h | 3 +
.../AArch64/AArch64TargetTransformInfo.cpp | 1 +
llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp | 16 +-
.../unroll-max-upperbound-with-early-exits.ll | 142 ++++++++++++++++++
.../unroll-max-upperbound-with-early-exits.ll | 137 +++++++++++++++++
5 files changed, 297 insertions(+), 2 deletions(-)
create mode 100644 llvm/test/Transforms/LoopUnroll/AArch64/unroll-max-upperbound-with-early-exits.ll
create mode 100644 llvm/test/Transforms/LoopUnroll/unroll-max-upperbound-with-early-exits.ll
diff --git a/llvm/include/llvm/Analysis/TargetTransformInfo.h b/llvm/include/llvm/Analysis/TargetTransformInfo.h
index aaf5ed6de93b6..3e04d2c5abc92 100644
--- a/llvm/include/llvm/Analysis/TargetTransformInfo.h
+++ b/llvm/include/llvm/Analysis/TargetTransformInfo.h
@@ -681,6 +681,9 @@ class TargetTransformInfo {
/// to be overrided by a target gives more flexiblity on certain cases.
/// By default, MaxUpperBound uses UnrollMaxUpperBound which value is 8.
unsigned MaxUpperBound;
+ /// Set the maximum upper bound of trip count for loops that may exit before
+ /// reaching the bound. This defaults to MaxUpperBound.
+ unsigned MaxUpperBoundWithEarlyExits;
/// Set the maximum unrolling factor for full unrolling. Like MaxCount, but
/// applies even if full unrolling is selected. This allows a target to fall
/// back to Partial unrolling if full unrolling is above FullUnrollMaxCount.
diff --git a/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp b/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp
index f8d2006cbd7de..ac367c312ed09 100644
--- a/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp
+++ b/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp
@@ -5574,6 +5574,7 @@ void AArch64TTIImpl::getUnrollingPreferences(
BaseT::getUnrollingPreferences(L, SE, UP, ORE);
UP.UpperBound = true;
+ UP.MaxUpperBoundWithEarlyExits = 5;
// For inner loop, it is more likely to be a hot one, and the runtime check
// can be promoted out from LICM pass, so the overhead is less, let's try
diff --git a/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp b/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
index 7034e595a4435..7ea7cffb3423f 100644
--- a/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
@@ -141,6 +141,11 @@ static cl::opt<unsigned> UnrollMaxUpperBound(
cl::desc(
"The max of trip count upper bound that is considered in unrolling"));
+static cl::opt<unsigned> UnrollMaxUpperBoundWithEarlyExits(
+ "unroll-max-upperbound-with-early-exits", cl::Hidden,
+ cl::desc("The max of trip count upper bound that is considered in "
+ "unrolling loops that may exit before reaching the bound"));
+
static cl::opt<unsigned> PragmaUnrollThreshold(
"pragma-unroll-threshold", cl::init(16 * 1024), cl::Hidden,
cl::desc("Unrolled size limit for loops with unroll metadata "
@@ -207,6 +212,7 @@ TargetTransformInfo::UnrollingPreferences llvm::gatherUnrollingPreferences(
UP.DefaultUnrollRuntimeCount = 8;
UP.MaxCount = std::numeric_limits<unsigned>::max();
UP.MaxUpperBound = UnrollMaxUpperBound;
+ UP.MaxUpperBoundWithEarlyExits = UP.MaxUpperBound;
UP.FullUnrollMaxCount = std::numeric_limits<unsigned>::max();
UP.BEInsns = 2;
UP.Partial = false;
@@ -247,8 +253,12 @@ TargetTransformInfo::UnrollingPreferences llvm::gatherUnrollingPreferences(
UP.MaxPercentThresholdBoost = UnrollMaxPercentThresholdBoost;
if (UnrollMaxCount.getNumOccurrences() > 0)
UP.MaxCount = UnrollMaxCount;
- if (UnrollMaxUpperBound.getNumOccurrences() > 0)
+ if (UnrollMaxUpperBound.getNumOccurrences() > 0) {
UP.MaxUpperBound = UnrollMaxUpperBound;
+ UP.MaxUpperBoundWithEarlyExits = UP.MaxUpperBound;
+ }
+ if (UnrollMaxUpperBoundWithEarlyExits.getNumOccurrences() > 0)
+ UP.MaxUpperBoundWithEarlyExits = UnrollMaxUpperBoundWithEarlyExits;
if (UnrollFullMaxCount.getNumOccurrences() > 0)
UP.FullUnrollMaxCount = UnrollFullMaxCount;
if (UnrollAllowPartial.getNumOccurrences() > 0)
@@ -1130,8 +1140,10 @@ void llvm::computeUnrollCount(Loop *L, const TargetTransformInfo &TTI,
// cost of exact full unrolling. As such, if we have an exact count and
// found it unprofitable, we'll never chose to bounded unroll.
LLVM_DEBUG(dbgs().indent(1) << "Trying upper-bound unroll...\n");
+ unsigned UpperBoundLimit =
+ MaxOrZero ? UP.MaxUpperBound : UP.MaxUpperBoundWithEarlyExits;
if (!TripCount && MaxTripCount && (UP.UpperBound || MaxOrZero) &&
- MaxTripCount <= UP.MaxUpperBound) {
+ MaxTripCount <= UpperBoundLimit) {
if (auto UnrollFactor = shouldFullUnroll(L, TTI, DT, SE, EphValues,
MaxTripCount, UCE, UP)) {
UP.Count = *UnrollFactor;
diff --git a/llvm/test/Transforms/LoopUnroll/AArch64/unroll-max-upperbound-with-early-exits.ll b/llvm/test/Transforms/LoopUnroll/AArch64/unroll-max-upperbound-with-early-exits.ll
new file mode 100644
index 0000000000000..9a034f01d76d3
--- /dev/null
+++ b/llvm/test/Transforms/LoopUnroll/AArch64/unroll-max-upperbound-with-early-exits.ll
@@ -0,0 +1,142 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -S -passes=loop-unroll -mtriple=aarch64 < %s | FileCheck %s --check-prefixes=DEFAULT
+; RUN: opt -S -passes=loop-unroll -mtriple=aarch64 -unroll-max-upperbound-with-early-exits=8 < %s | FileCheck %s --check-prefixes=WIDE
+
+; AArch64 uses a lower upper-bound limit for loops that can exit early. A typical
+; case is a varint-length helper:
+;
+; int varint_len(uint32_t serial_type) {
+; uint64_t v = serial_type;
+; int i;
+; for (i = 1; (v >>= 7) != 0; i++) {}
+; return i;
+; }
+;
+; This loop runs at most 6 times, over the AArch64 limit of 5, so it is not
+; unrolled by default. Raising the bound to 8 lets it unroll.
+define i32 @varint_i32(i32 %serial_type) {
+; DEFAULT-LABEL: @varint_i32(
+; DEFAULT-NEXT: entry:
+; DEFAULT-NEXT: [[V0:%.*]] = zext i32 [[SERIAL_TYPE:%.*]] to i64
+; DEFAULT-NEXT: br label [[LOOP:%.*]]
+; DEFAULT: loop:
+; DEFAULT-NEXT: [[I:%.*]] = phi i32 [ 1, [[ENTRY:%.*]] ], [ [[I_NEXT:%.*]], [[LOOP]] ]
+; DEFAULT-NEXT: [[V:%.*]] = phi i64 [ [[V0]], [[ENTRY]] ], [ [[V_SHR:%.*]], [[LOOP]] ]
+; DEFAULT-NEXT: [[V_SHR]] = lshr i64 [[V]], 7
+; DEFAULT-NEXT: [[I_NEXT]] = add i32 [[I]], 1
+; DEFAULT-NEXT: [[CMP:%.*]] = icmp ne i64 [[V_SHR]], 0
+; DEFAULT-NEXT: br i1 [[CMP]], label [[LOOP]], label [[EXIT:%.*]]
+; DEFAULT: exit:
+; DEFAULT-NEXT: [[I_LCSSA:%.*]] = phi i32 [ [[I]], [[LOOP]] ]
+; DEFAULT-NEXT: ret i32 [[I_LCSSA]]
+;
+; WIDE-LABEL: @varint_i32(
+; WIDE-NEXT: entry:
+; WIDE-NEXT: [[V0:%.*]] = zext i32 [[SERIAL_TYPE:%.*]] to i64
+; WIDE-NEXT: br label [[LOOP:%.*]]
+; WIDE: loop:
+; WIDE-NEXT: [[V_SHR:%.*]] = lshr i64 [[V0]], 7
+; WIDE-NEXT: [[CMP:%.*]] = icmp ne i64 [[V_SHR]], 0
+; WIDE-NEXT: br i1 [[CMP]], label [[LOOP_1:%.*]], label [[EXIT:%.*]]
+; WIDE: loop.1:
+; WIDE-NEXT: [[V_SHR_1:%.*]] = lshr i64 [[V_SHR]], 7
+; WIDE-NEXT: [[CMP_1:%.*]] = icmp ne i64 [[V_SHR_1]], 0
+; WIDE-NEXT: br i1 [[CMP_1]], label [[LOOP_2:%.*]], label [[EXIT]]
+; WIDE: loop.2:
+; WIDE-NEXT: [[V_SHR_2:%.*]] = lshr i64 [[V_SHR_1]], 7
+; WIDE-NEXT: [[CMP_2:%.*]] = icmp ne i64 [[V_SHR_2]], 0
+; WIDE-NEXT: br i1 [[CMP_2]], label [[LOOP_3:%.*]], label [[EXIT]]
+; WIDE: loop.3:
+; WIDE-NEXT: [[V_SHR_3:%.*]] = lshr i64 [[V_SHR_2]], 7
+; WIDE-NEXT: [[CMP_3:%.*]] = icmp ne i64 [[V_SHR_3]], 0
+; WIDE-NEXT: br i1 [[CMP_3]], label [[LOOP_4:%.*]], label [[EXIT]]
+; WIDE: loop.4:
+; WIDE-NEXT: [[V_SHR_4:%.*]] = lshr i64 [[V_SHR_3]], 7
+; WIDE-NEXT: [[CMP_4:%.*]] = icmp ne i64 [[V_SHR_4]], 0
+; WIDE-NEXT: br i1 [[CMP_4]], label [[LOOP_5:%.*]], label [[EXIT]]
+; WIDE: loop.5:
+; WIDE-NEXT: br label [[EXIT]]
+; WIDE: exit:
+; WIDE-NEXT: [[I_LCSSA:%.*]] = phi i32 [ 1, [[LOOP]] ], [ 2, [[LOOP_1]] ], [ 3, [[LOOP_2]] ], [ 4, [[LOOP_3]] ], [ 5, [[LOOP_4]] ], [ 6, [[LOOP_5]] ]
+; WIDE-NEXT: ret i32 [[I_LCSSA]]
+;
+entry:
+ %v0 = zext i32 %serial_type to i64
+ br label %loop
+
+loop:
+ %i = phi i32 [ 1, %entry ], [ %i.next, %loop ]
+ %v = phi i64 [ %v0, %entry ], [ %v.shr, %loop ]
+ %v.shr = lshr i64 %v, 7
+ %i.next = add i32 %i, 1
+ %cmp = icmp ne i64 %v.shr, 0
+ br i1 %cmp, label %loop, label %exit
+
+exit:
+ %i.lcssa = phi i32 [ %i, %loop ]
+ ret i32 %i.lcssa
+}
+
+; The 16-bit version runs at most 4 times, under the limit of 5, so it is still
+; unrolled by default.
+define i32 @varint_i16(i16 %serial_type) {
+; DEFAULT-LABEL: @varint_i16(
+; DEFAULT-NEXT: entry:
+; DEFAULT-NEXT: [[V0:%.*]] = zext i16 [[SERIAL_TYPE:%.*]] to i64
+; DEFAULT-NEXT: br label [[LOOP:%.*]]
+; DEFAULT: loop:
+; DEFAULT-NEXT: [[V_SHR:%.*]] = lshr i64 [[V0]], 7
+; DEFAULT-NEXT: [[CMP:%.*]] = icmp ne i64 [[V_SHR]], 0
+; DEFAULT-NEXT: br i1 [[CMP]], label [[LOOP_1:%.*]], label [[EXIT:%.*]]
+; DEFAULT: loop.1:
+; DEFAULT-NEXT: [[V_SHR_1:%.*]] = lshr i64 [[V_SHR]], 7
+; DEFAULT-NEXT: [[CMP_1:%.*]] = icmp ne i64 [[V_SHR_1]], 0
+; DEFAULT-NEXT: br i1 [[CMP_1]], label [[LOOP_2:%.*]], label [[EXIT]]
+; DEFAULT: loop.2:
+; DEFAULT-NEXT: [[V_SHR_2:%.*]] = lshr i64 [[V_SHR_1]], 7
+; DEFAULT-NEXT: [[CMP_2:%.*]] = icmp ne i64 [[V_SHR_2]], 0
+; DEFAULT-NEXT: br i1 [[CMP_2]], label [[LOOP_3:%.*]], label [[EXIT]]
+; DEFAULT: loop.3:
+; DEFAULT-NEXT: br label [[EXIT]]
+; DEFAULT: exit:
+; DEFAULT-NEXT: [[I_LCSSA:%.*]] = phi i32 [ 1, [[LOOP]] ], [ 2, [[LOOP_1]] ], [ 3, [[LOOP_2]] ], [ 4, [[LOOP_3]] ]
+; DEFAULT-NEXT: ret i32 [[I_LCSSA]]
+;
+; WIDE-LABEL: @varint_i16(
+; WIDE-NEXT: entry:
+; WIDE-NEXT: [[V0:%.*]] = zext i16 [[SERIAL_TYPE:%.*]] to i64
+; WIDE-NEXT: br label [[LOOP:%.*]]
+; WIDE: loop:
+; WIDE-NEXT: [[V_SHR:%.*]] = lshr i64 [[V0]], 7
+; WIDE-NEXT: [[CMP:%.*]] = icmp ne i64 [[V_SHR]], 0
+; WIDE-NEXT: br i1 [[CMP]], label [[LOOP_1:%.*]], label [[EXIT:%.*]]
+; WIDE: loop.1:
+; WIDE-NEXT: [[V_SHR_1:%.*]] = lshr i64 [[V_SHR]], 7
+; WIDE-NEXT: [[CMP_1:%.*]] = icmp ne i64 [[V_SHR_1]], 0
+; WIDE-NEXT: br i1 [[CMP_1]], label [[LOOP_2:%.*]], label [[EXIT]]
+; WIDE: loop.2:
+; WIDE-NEXT: [[V_SHR_2:%.*]] = lshr i64 [[V_SHR_1]], 7
+; WIDE-NEXT: [[CMP_2:%.*]] = icmp ne i64 [[V_SHR_2]], 0
+; WIDE-NEXT: br i1 [[CMP_2]], label [[LOOP_3:%.*]], label [[EXIT]]
+; WIDE: loop.3:
+; WIDE-NEXT: br label [[EXIT]]
+; WIDE: exit:
+; WIDE-NEXT: [[I_LCSSA:%.*]] = phi i32 [ 1, [[LOOP]] ], [ 2, [[LOOP_1]] ], [ 3, [[LOOP_2]] ], [ 4, [[LOOP_3]] ]
+; WIDE-NEXT: ret i32 [[I_LCSSA]]
+;
+entry:
+ %v0 = zext i16 %serial_type to i64
+ br label %loop
+
+loop:
+ %i = phi i32 [ 1, %entry ], [ %i.next, %loop ]
+ %v = phi i64 [ %v0, %entry ], [ %v.shr, %loop ]
+ %v.shr = lshr i64 %v, 7
+ %i.next = add i32 %i, 1
+ %cmp = icmp ne i64 %v.shr, 0
+ br i1 %cmp, label %loop, label %exit
+
+exit:
+ %i.lcssa = phi i32 [ %i, %loop ]
+ ret i32 %i.lcssa
+}
diff --git a/llvm/test/Transforms/LoopUnroll/unroll-max-upperbound-with-early-exits.ll b/llvm/test/Transforms/LoopUnroll/unroll-max-upperbound-with-early-exits.ll
new file mode 100644
index 0000000000000..29ff1345cfd07
--- /dev/null
+++ b/llvm/test/Transforms/LoopUnroll/unroll-max-upperbound-with-early-exits.ll
@@ -0,0 +1,137 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -S -passes='loop-unroll<upperbound>' < %s | FileCheck %s --check-prefixes=DEFAULT
+; RUN: opt -S -passes='loop-unroll<upperbound>' -unroll-max-upperbound-with-early-exits=5 < %s | FileCheck %s --check-prefixes=LIMIT
+
+; -unroll-max-upperbound-with-early-exits only limits loops that can exit early.
+; Loops that run their max count or zero times still use -unroll-max-upperbound.
+
+; This loop can exit on any iteration and runs at most 6 times. The bound of 5
+; is below that, so it is not unrolled.
+define i32 @early_exit(i32 %serial_type) {
+; DEFAULT-LABEL: @early_exit(
+; DEFAULT-NEXT: entry:
+; DEFAULT-NEXT: [[V0:%.*]] = zext i32 [[SERIAL_TYPE:%.*]] to i64
+; DEFAULT-NEXT: br label [[LOOP:%.*]]
+; DEFAULT: loop:
+; DEFAULT-NEXT: [[V_SHR:%.*]] = lshr i64 [[V0]], 7
+; DEFAULT-NEXT: [[CMP:%.*]] = icmp ne i64 [[V_SHR]], 0
+; DEFAULT-NEXT: br i1 [[CMP]], label [[LOOP_1:%.*]], label [[EXIT:%.*]]
+; DEFAULT: loop.1:
+; DEFAULT-NEXT: [[V_SHR_1:%.*]] = lshr i64 [[V_SHR]], 7
+; DEFAULT-NEXT: [[CMP_1:%.*]] = icmp ne i64 [[V_SHR_1]], 0
+; DEFAULT-NEXT: br i1 [[CMP_1]], label [[LOOP_2:%.*]], label [[EXIT]]
+; DEFAULT: loop.2:
+; DEFAULT-NEXT: [[V_SHR_2:%.*]] = lshr i64 [[V_SHR_1]], 7
+; DEFAULT-NEXT: [[CMP_2:%.*]] = icmp ne i64 [[V_SHR_2]], 0
+; DEFAULT-NEXT: br i1 [[CMP_2]], label [[LOOP_3:%.*]], label [[EXIT]]
+; DEFAULT: loop.3:
+; DEFAULT-NEXT: [[V_SHR_3:%.*]] = lshr i64 [[V_SHR_2]], 7
+; DEFAULT-NEXT: [[CMP_3:%.*]] = icmp ne i64 [[V_SHR_3]], 0
+; DEFAULT-NEXT: br i1 [[CMP_3]], label [[LOOP_4:%.*]], label [[EXIT]]
+; DEFAULT: loop.4:
+; DEFAULT-NEXT: [[V_SHR_4:%.*]] = lshr i64 [[V_SHR_3]], 7
+; DEFAULT-NEXT: [[CMP_4:%.*]] = icmp ne i64 [[V_SHR_4]], 0
+; DEFAULT-NEXT: br i1 [[CMP_4]], label [[LOOP_5:%.*]], label [[EXIT]]
+; DEFAULT: loop.5:
+; DEFAULT-NEXT: br label [[EXIT]]
+; DEFAULT: exit:
+; DEFAULT-NEXT: [[I_LCSSA:%.*]] = phi i32 [ 1, [[LOOP]] ], [ 2, [[LOOP_1]] ], [ 3, [[LOOP_2]] ], [ 4, [[LOOP_3]] ], [ 5, [[LOOP_4]] ], [ 6, [[LOOP_5]] ]
+; DEFAULT-NEXT: ret i32 [[I_LCSSA]]
+;
+; LIMIT-LABEL: @early_exit(
+; LIMIT-NEXT: entry:
+; LIMIT-NEXT: [[V0:%.*]] = zext i32 [[SERIAL_TYPE:%.*]] to i64
+; LIMIT-NEXT: br label [[LOOP:%.*]]
+; LIMIT: loop:
+; LIMIT-NEXT: [[I:%.*]] = phi i32 [ 1, [[ENTRY:%.*]] ], [ [[I_NEXT:%.*]], [[LOOP]] ]
+; LIMIT-NEXT: [[V:%.*]] = phi i64 [ [[V0]], [[ENTRY]] ], [ [[V_SHR:%.*]], [[LOOP]] ]
+; LIMIT-NEXT: [[V_SHR]] = lshr i64 [[V]], 7
+; LIMIT-NEXT: [[I_NEXT]] = add i32 [[I]], 1
+; LIMIT-NEXT: [[CMP:%.*]] = icmp ne i64 [[V_SHR]], 0
+; LIMIT-NEXT: br i1 [[CMP]], label [[LOOP]], label [[EXIT:%.*]]
+; LIMIT: exit:
+; LIMIT-NEXT: [[I_LCSSA:%.*]] = phi i32 [ [[I]], [[LOOP]] ]
+; LIMIT-NEXT: ret i32 [[I_LCSSA]]
+;
+entry:
+ %v0 = zext i32 %serial_type to i64
+ br label %loop
+
+loop:
+ %i = phi i32 [ 1, %entry ], [ %i.next, %loop ]
+ %v = phi i64 [ %v0, %entry ], [ %v.shr, %loop ]
+ %v.shr = lshr i64 %v, 7
+ %i.next = add i32 %i, 1
+ %cmp = icmp ne i64 %v.shr, 0
+ br i1 %cmp, label %loop, label %exit
+
+exit:
+ %i.lcssa = phi i32 [ %i, %loop ]
+ ret i32 %i.lcssa
+}
+
+; This loop runs at most 6 times, but always its max count or zero times, so the
+; option does not affect it. It is fully unrolled in both runs.
+define i32 @max_or_zero(i32 %n, ptr %p) {
+; DEFAULT-LABEL: @max_or_zero(
+; DEFAULT-NEXT: entry:
+; DEFAULT-NEXT: [[END:%.*]] = add i32 [[N:%.*]], 6
+; DEFAULT-NEXT: br label [[LOOP:%.*]]
+; DEFAULT: loop:
+; DEFAULT-NEXT: store i32 [[N]], ptr [[P:%.*]], align 4
+; DEFAULT-NEXT: [[I_NEXT:%.*]] = add i32 [[N]], 1
+; DEFAULT-NEXT: [[C:%.*]] = icmp ult i32 [[I_NEXT]], [[END]]
+; DEFAULT-NEXT: br i1 [[C]], label [[LOOP_1:%.*]], label [[EXIT:%.*]]
+; DEFAULT: loop.1:
+; DEFAULT-NEXT: store i32 [[I_NEXT]], ptr [[P]], align 4
+; DEFAULT-NEXT: [[I_NEXT_1:%.*]] = add i32 [[N]], 2
+; DEFAULT-NEXT: store i32 [[I_NEXT_1]], ptr [[P]], align 4
+; DEFAULT-NEXT: [[I_NEXT_2:%.*]] = add i32 [[N]], 3
+; DEFAULT-NEXT: store i32 [[I_NEXT_2]], ptr [[P]], align 4
+; DEFAULT-NEXT: [[I_NEXT_3:%.*]] = add i32 [[N]], 4
+; DEFAULT-NEXT: store i32 [[I_NEXT_3]], ptr [[P]], align 4
+; DEFAULT-NEXT: [[I_NEXT_4:%.*]] = add i32 [[N]], 5
+; DEFAULT-NEXT: store i32 [[I_NEXT_4]], ptr [[P]], align 4
+; DEFAULT-NEXT: br label [[EXIT]]
+; DEFAULT: exit:
+; DEFAULT-NEXT: [[I_LCSSA:%.*]] = phi i32 [ [[N]], [[LOOP]] ], [ [[I_NEXT_4]], [[LOOP_1]] ]
+; DEFAULT-NEXT: ret i32 [[I_LCSSA]]
+;
+; LIMIT-LABEL: @max_or_zero(
+; LIMIT-NEXT: entry:
+; LIMIT-NEXT: [[END:%.*]] = add i32 [[N:%.*]], 6
+; LIMIT-NEXT: br label [[LOOP:%.*]]
+; LIMIT: loop:
+; LIMIT-NEXT: store i32 [[N]], ptr [[P:%.*]], align 4
+; LIMIT-NEXT: [[I_NEXT:%.*]] = add i32 [[N]], 1
+; LIMIT-NEXT: [[C:%.*]] = icmp ult i32 [[I_NEXT]], [[END]]
+; LIMIT-NEXT: br i1 [[C]], label [[LOOP_1:%.*]], label [[EXIT:%.*]]
+; LIMIT: loop.1:
+; LIMIT-NEXT: store i32 [[I_NEXT]], ptr [[P]], align 4
+; LIMIT-NEXT: [[I_NEXT_1:%.*]] = add i32 [[N]], 2
+; LIMIT-NEXT: store i32 [[I_NEXT_1]], ptr [[P]], align 4
+; LIMIT-NEXT: [[I_NEXT_2:%.*]] = add i32 [[N]], 3
+; LIMIT-NEXT: store i32 [[I_NEXT_2]], ptr [[P]], align 4
+; LIMIT-NEXT: [[I_NEXT_3:%.*]] = add i32 [[N]], 4
+; LIMIT-NEXT: store i32 [[I_NEXT_3]], ptr [[P]], align 4
+; LIMIT-NEXT: [[I_NEXT_4:%.*]] = add i32 [[N]], 5
+; LIMIT-NEXT: store i32 [[I_NEXT_4]], ptr [[P]], align 4
+; LIMIT-NEXT: br label [[EXIT]]
+; LIMIT: exit:
+; LIMIT-NEXT: [[I_LCSSA:%.*]] = phi i32 [ [[N]], [[LOOP]] ], [ [[I_NEXT_4]], [[LOOP_1]] ]
+; LIMIT-NEXT: ret i32 [[I_LCSSA]]
+;
+entry:
+ %end = add i32 %n, 6
+ br label %loop
+
+loop:
+ %i = phi i32 [ %n, %entry ], [ %i.next, %loop ]
+ store i32 %i, ptr %p
+ %i.next = add i32 %i, 1
+ %c = icmp ult i32 %i.next, %end
+ br i1 %c, label %loop, label %exit
+
+exit:
+ ret i32 %i
+}
More information about the llvm-commits
mailing list