Index: include/llvm/Transforms/Scalar/LoopUnrollPass.h =================================================================== --- include/llvm/Transforms/Scalar/LoopUnrollPass.h +++ include/llvm/Transforms/Scalar/LoopUnrollPass.h @@ -16,12 +16,29 @@ namespace llvm { -struct LoopUnrollPass : public PassInfoMixin { - Optional ProvidedCount; - Optional ProvidedThreshold; - Optional ProvidedAllowPartial; - Optional ProvidedRuntime; - Optional ProvidedUpperBound; +class LoopUnrollPass : public PassInfoMixin { + const bool AllowPartialUnrolling; + + explicit LoopUnrollPass(bool AllowPartialUnrolling) + : AllowPartialUnrolling(AllowPartialUnrolling) {} + +public: + /// Create an instance of the loop unroll pass that will support both full + /// and partial unrolling. + /// + /// This uses the target information (or flags) to control the thresholds for + /// different unrolling stategies but supports all of them. + static LoopUnrollPass create() { + return LoopUnrollPass(/*AllowPartialUnrolling*/ true); + } + + /// Create an instance of the loop unroll pass that only does full loop + /// unrolling. + /// + /// This will disable any runtime or partial unrolling. + static LoopUnrollPass createFull() { + return LoopUnrollPass(/*AllowPartialUnrolling*/ false); + } PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U); Index: lib/Passes/PassRegistry.def =================================================================== --- lib/Passes/PassRegistry.def +++ lib/Passes/PassRegistry.def @@ -224,7 +224,8 @@ LOOP_PASS("simplify-cfg", LoopSimplifyCFGPass()) LOOP_PASS("strength-reduce", LoopStrengthReducePass()) LOOP_PASS("indvars", IndVarSimplifyPass()) -LOOP_PASS("unroll", LoopUnrollPass()) +LOOP_PASS("unroll", LoopUnrollPass::create()) +LOOP_PASS("unroll-full", LoopUnrollPass::createFull()) LOOP_PASS("print-access-info", LoopAccessInfoPrinterPass(dbgs())) LOOP_PASS("print", IVUsersPrinterPass(dbgs())) #undef LOOP_PASS Index: lib/Transforms/Scalar/LoopUnrollPass.cpp =================================================================== --- lib/Transforms/Scalar/LoopUnrollPass.cpp +++ lib/Transforms/Scalar/LoopUnrollPass.cpp @@ -1146,10 +1146,17 @@ else OldLoops.insert(AR.LI.begin(), AR.LI.end()); + // The API here is quite complex to call, but there are only two interesting + // states we support: partial and full (or "simple") unrolling. However, to + // enable these things we actually pass "None" in for the optional to avoid + // providing an explicit choice. + Optional AllowPartialParam, RuntimeParam, UpperBoundParam; + if (!AllowPartialUnrolling) + AllowPartialParam = RuntimeParam = UpperBoundParam = false; bool Changed = tryToUnrollLoop(&L, AR.DT, &AR.LI, &AR.SE, AR.TTI, AR.AC, *ORE, - /*PreserveLCSSA*/ true, ProvidedCount, - ProvidedThreshold, ProvidedAllowPartial, - ProvidedRuntime, ProvidedUpperBound); + /*PreserveLCSSA*/ true, /*Count*/ None, + /*Threshold*/ None, AllowPartialParam, + RuntimeParam, UpperBoundParam); if (!Changed) return PreservedAnalyses::all(); Index: test/Transforms/LoopUnroll/full-unroll-bad-cost.ll =================================================================== --- test/Transforms/LoopUnroll/full-unroll-bad-cost.ll +++ test/Transforms/LoopUnroll/full-unroll-bad-cost.ll @@ -1,5 +1,5 @@ ; RUN: opt -S -loop-unroll < %s | FileCheck %s -; RUN: opt < %s -passes='require,loop(unroll)' -S | FileCheck %s +; RUN: opt < %s -passes='require,loop(unroll-full)' -S | FileCheck %s ; LLVM should not try to fully unroll this loop. Index: test/Transforms/LoopUnroll/full-unroll-crashers.ll =================================================================== --- test/Transforms/LoopUnroll/full-unroll-crashers.ll +++ test/Transforms/LoopUnroll/full-unroll-crashers.ll @@ -1,6 +1,6 @@ ; Check that we don't crash on corner cases. ; RUN: opt < %s -S -loop-unroll -unroll-max-iteration-count-to-analyze=1000 -unroll-threshold=1 -unroll-max-percent-threshold-boost=200 -o /dev/null -; RUN: opt < %s -S -passes='require,loop(unroll)' -unroll-max-iteration-count-to-analyze=1000 -unroll-threshold=1 -unroll-max-percent-threshold-boost=200 -o /dev/null +; RUN: opt < %s -S -passes='require,loop(unroll-full)' -unroll-max-iteration-count-to-analyze=1000 -unroll-threshold=1 -unroll-max-percent-threshold-boost=200 -o /dev/null target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" @known_constant = internal unnamed_addr constant [10 x i32] [i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1], align 16 Index: test/Transforms/LoopUnroll/full-unroll-heuristics-2.ll =================================================================== --- test/Transforms/LoopUnroll/full-unroll-heuristics-2.ll +++ test/Transforms/LoopUnroll/full-unroll-heuristics-2.ll @@ -1,5 +1,5 @@ ; RUN: opt < %s -S -loop-unroll -unroll-max-iteration-count-to-analyze=1000 -unroll-threshold=10 -unroll-max-percent-threshold-boost=200 | FileCheck %s -; RUN: opt < %s -S -passes='require,loop(unroll)' -unroll-max-iteration-count-to-analyze=1000 -unroll-threshold=10 -unroll-max-percent-threshold-boost=200 | FileCheck %s +; RUN: opt < %s -S -passes='require,loop(unroll-full)' -unroll-max-iteration-count-to-analyze=1000 -unroll-threshold=10 -unroll-max-percent-threshold-boost=200 | FileCheck %s target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" @unknown_global = internal unnamed_addr global [9 x i32] [i32 0, i32 -1, i32 0, i32 -1, i32 5, i32 -1, i32 0, i32 -1, i32 0], align 16 Index: test/Transforms/LoopUnroll/full-unroll-heuristics-cmp.ll =================================================================== --- test/Transforms/LoopUnroll/full-unroll-heuristics-cmp.ll +++ test/Transforms/LoopUnroll/full-unroll-heuristics-cmp.ll @@ -1,5 +1,5 @@ ; RUN: opt < %s -S -loop-unroll -unroll-max-iteration-count-to-analyze=100 -unroll-threshold=10 -unroll-max-percent-threshold-boost=200 | FileCheck %s -; RUN: opt < %s -S -passes='require,loop(unroll)' -unroll-max-iteration-count-to-analyze=100 -unroll-threshold=10 -unroll-max-percent-threshold-boost=200 | FileCheck %s +; RUN: opt < %s -S -passes='require,loop(unroll-full)' -unroll-max-iteration-count-to-analyze=100 -unroll-threshold=10 -unroll-max-percent-threshold-boost=200 | FileCheck %s target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" @known_constant = internal unnamed_addr constant [10 x i32] [i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1], align 16 Index: test/Transforms/LoopUnroll/full-unroll-heuristics-dce.ll =================================================================== --- test/Transforms/LoopUnroll/full-unroll-heuristics-dce.ll +++ test/Transforms/LoopUnroll/full-unroll-heuristics-dce.ll @@ -1,5 +1,5 @@ ; RUN: opt < %s -S -loop-unroll -unroll-max-iteration-count-to-analyze=100 -unroll-threshold=12 -unroll-max-percent-threshold-boost=400 | FileCheck %s -; RUN: opt < %s -S -passes='require,loop(unroll)' -unroll-max-iteration-count-to-analyze=100 -unroll-threshold=12 -unroll-max-percent-threshold-boost=400 | FileCheck %s +; RUN: opt < %s -S -passes='require,loop(unroll-full)' -unroll-max-iteration-count-to-analyze=100 -unroll-threshold=12 -unroll-max-percent-threshold-boost=400 | FileCheck %s target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" @known_constant = internal unnamed_addr constant [10 x i32] [i32 0, i32 0, i32 0, i32 0, i32 1, i32 0, i32 0, i32 0, i32 0, i32 0], align 16 Index: test/Transforms/LoopUnroll/full-unroll-heuristics-geps.ll =================================================================== --- test/Transforms/LoopUnroll/full-unroll-heuristics-geps.ll +++ test/Transforms/LoopUnroll/full-unroll-heuristics-geps.ll @@ -1,5 +1,5 @@ ; RUN: opt < %s -S -loop-unroll -unroll-max-iteration-count-to-analyze=100 -unroll-threshold=10 -unroll-max-percent-threshold-boost=200 | FileCheck %s -; RUN: opt < %s -S -passes='require,loop(unroll)' -unroll-max-iteration-count-to-analyze=100 -unroll-threshold=10 -unroll-max-percent-threshold-boost=200 | FileCheck %s +; RUN: opt < %s -S -passes='require,loop(unroll-full)' -unroll-max-iteration-count-to-analyze=100 -unroll-threshold=10 -unroll-max-percent-threshold-boost=200 | FileCheck %s target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" ; When examining gep-instructions we shouldn't consider them simplified if the Index: test/Transforms/LoopUnroll/full-unroll-heuristics-phi-prop.ll =================================================================== --- test/Transforms/LoopUnroll/full-unroll-heuristics-phi-prop.ll +++ test/Transforms/LoopUnroll/full-unroll-heuristics-phi-prop.ll @@ -1,5 +1,5 @@ ; RUN: opt < %s -S -loop-unroll -unroll-max-iteration-count-to-analyze=100 -unroll-threshold=10 -unroll-max-percent-threshold-boost=200 | FileCheck %s -; RUN: opt < %s -S -passes='require,loop(unroll)' -unroll-max-iteration-count-to-analyze=100 -unroll-threshold=10 -unroll-max-percent-threshold-boost=200 | FileCheck %s +; RUN: opt < %s -S -passes='require,loop(unroll-full)' -unroll-max-iteration-count-to-analyze=100 -unroll-threshold=10 -unroll-max-percent-threshold-boost=200 | FileCheck %s target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" define i64 @propagate_loop_phis() { Index: test/Transforms/LoopUnroll/full-unroll-heuristics.ll =================================================================== --- test/Transforms/LoopUnroll/full-unroll-heuristics.ll +++ test/Transforms/LoopUnroll/full-unroll-heuristics.ll @@ -21,6 +21,15 @@ ; RUN: opt < %s -S -loop-unroll -unroll-max-iteration-count-to-analyze=1000 -unroll-threshold=20 -unroll-max-percent-threshold-boost=200 | FileCheck %s -check-prefix=TEST2 ; RUN: opt < %s -S -loop-unroll -unroll-max-iteration-count-to-analyze=1000 -unroll-threshold=20 -unroll-max-percent-threshold-boost=100 | FileCheck %s -check-prefix=TEST3 +; RUN: opt < %s -S -passes='require,loop(unroll-full)' -unroll-max-iteration-count-to-analyze=1000 -unroll-threshold=10 -unroll-max-percent-threshold-boost=100 | FileCheck %s -check-prefix=TEST1 +; RUN: opt < %s -S -passes='require,loop(unroll-full)' -unroll-max-iteration-count-to-analyze=1000 -unroll-threshold=20 -unroll-max-percent-threshold-boost=200 | FileCheck %s -check-prefix=TEST2 +; RUN: opt < %s -S -passes='require,loop(unroll-full)' -unroll-max-iteration-count-to-analyze=1000 -unroll-threshold=20 -unroll-max-percent-threshold-boost=100 | FileCheck %s -check-prefix=TEST3 + +; Check that these work when the unroller has partial unrolling enabled too. +; RUN: opt < %s -S -passes='require,loop(unroll)' -unroll-max-iteration-count-to-analyze=1000 -unroll-threshold=10 -unroll-max-percent-threshold-boost=100 | FileCheck %s -check-prefix=TEST1 +; RUN: opt < %s -S -passes='require,loop(unroll)' -unroll-max-iteration-count-to-analyze=1000 -unroll-threshold=20 -unroll-max-percent-threshold-boost=200 | FileCheck %s -check-prefix=TEST2 +; RUN: opt < %s -S -passes='require,loop(unroll)' -unroll-max-iteration-count-to-analyze=1000 -unroll-threshold=20 -unroll-max-percent-threshold-boost=100 | FileCheck %s -check-prefix=TEST3 + ; If the absolute threshold is too low, we should not unroll: ; TEST1: %array_const_idx = getelementptr inbounds [9 x i32], [9 x i32]* @known_constant, i64 0, i64 %iv @@ -32,6 +41,7 @@ ; And check that we don't crash when we're not allowed to do any analysis. ; RUN: opt < %s -loop-unroll -unroll-max-iteration-count-to-analyze=0 -disable-output +; RUN: opt < %s -passes='require,loop(unroll-full)' -unroll-max-iteration-count-to-analyze=0 -disable-output target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" @known_constant = internal unnamed_addr constant [9 x i32] [i32 0, i32 -1, i32 0, i32 -1, i32 5, i32 -1, i32 0, i32 -1, i32 0], align 16 Index: test/Transforms/LoopUnroll/full-unroll-keep-first-exit.ll =================================================================== --- test/Transforms/LoopUnroll/full-unroll-keep-first-exit.ll +++ test/Transforms/LoopUnroll/full-unroll-keep-first-exit.ll @@ -1,5 +1,5 @@ ; RUN: opt -S -loop-unroll < %s | FileCheck %s -; RUN: opt -S -passes='require,loop(unroll)' < %s | FileCheck %s +; RUN: opt -S -passes='require,loop(unroll-full)' < %s | FileCheck %s ; Unroll twice, with first loop exit kept ; CHECK-LABEL: @s32_max1 Index: test/Transforms/LoopUnroll/partial-unroll-const-bounds.ll =================================================================== --- test/Transforms/LoopUnroll/partial-unroll-const-bounds.ll +++ test/Transforms/LoopUnroll/partial-unroll-const-bounds.ll @@ -1,4 +1,8 @@ ; RUN: opt < %s -S -unroll-partial-threshold=20 -unroll-threshold=20 -loop-unroll -unroll-allow-partial -unroll-runtime -unroll-allow-remainder -unroll-max-percent-threshold-boost=100 | FileCheck %s +; RUN: opt < %s -S -passes='require,loop(unroll)' -unroll-partial-threshold=20 -unroll-threshold=20 -unroll-allow-partial -unroll-runtime -unroll-allow-remainder -unroll-max-percent-threshold-boost=100 | FileCheck %s +; +; Also check that the simple unroller doesn't allow the partial unrolling. +; RUN: opt < %s -S -passes='require,loop(unroll-full)' -unroll-partial-threshold=20 -unroll-threshold=20 -unroll-allow-partial -unroll-runtime -unroll-allow-remainder -unroll-max-percent-threshold-boost=100 | FileCheck %s --check-prefix=CHECK-NO-UNROLL ; The Loop TripCount is 9. However unroll factors 3 or 9 exceed given threshold. ; The test checks that we choose a smaller, power-of-two, unroll count and do not give up on unrolling. @@ -8,6 +12,10 @@ ; CHECK: for.body.1: ; CHECK: store +; CHECK-NO-UNROLL: for.body: +; CHECK-NO-UNROLL: store +; CHECK-NO-UNROLL-NOT: store + define void @foo(i32* nocapture %a, i32* nocapture readonly %b) nounwind uwtable { entry: br label %for.body