Index: include/llvm/Analysis/TargetTransformInfo.h =================================================================== --- include/llvm/Analysis/TargetTransformInfo.h +++ include/llvm/Analysis/TargetTransformInfo.h @@ -260,6 +260,10 @@ // (set to UINT_MAX to disable). This does not apply in cases where the // loop is being fully unrolled. unsigned MaxCount; + /// 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. + unsigned FullUnrollMaxCount; /// Allow partial unrolling (unrolling of loops to expand the size of the /// loop body, not only to eliminate small constant-trip-count loops). bool Partial; Index: lib/Transforms/Scalar/LoopUnrollPass.cpp =================================================================== --- lib/Transforms/Scalar/LoopUnrollPass.cpp +++ lib/Transforms/Scalar/LoopUnrollPass.cpp @@ -65,6 +65,15 @@ cl::desc("Use this unroll count for all loops including those with " "unroll_count pragma values, for testing purposes")); +static cl::opt +UnrollMaxCount("unroll-max-count", cl::Hidden, + cl::desc("Set the max unroll count for partial and runtime unrolling, for" + "testing purposes")); + +static cl::opt +UnrollFullMaxCount("unroll-full-max-count", cl::Hidden, + cl::desc("Set the max unroll count for full unrolling, for testing purposes")); + static cl::opt UnrollAllowPartial("unroll-allow-partial", cl::Hidden, cl::desc("Allows loops to be partially unrolled until " @@ -107,6 +116,7 @@ UP.PartialOptSizeThreshold = UP.OptSizeThreshold; UP.Count = 0; UP.MaxCount = UINT_MAX; + UP.FullUnrollMaxCount = UINT_MAX; UP.Partial = false; UP.Runtime = false; UP.AllowExpensiveTripCount = false; @@ -138,6 +148,10 @@ UP.DynamicCostSavingsDiscount = UnrollDynamicCostSavingsDiscount; if (UnrollCount.getNumOccurrences() > 0) UP.Count = UnrollCount; + if (UnrollMaxCount.getNumOccurrences() > 0) + UP.MaxCount = UnrollMaxCount; + if (UnrollFullMaxCount.getNumOccurrences() > 0) + UP.FullUnrollMaxCount = UnrollFullMaxCount; if (UnrollAllowPartial.getNumOccurrences() > 0) UP.Partial = UnrollAllowPartial; if (UnrollRuntime.getNumOccurrences() > 0) @@ -566,6 +580,7 @@ Count = TripCount == 0 ? DefaultUnrollRuntimeCount : TripCount; if (TripCount && Count > TripCount) Count = TripCount; + Count = std::min(Count, UP.FullUnrollMaxCount); unsigned NumInlineCandidates; bool NotDuplicatable; @@ -633,10 +648,12 @@ << "-unroll-allow-partial not given\n"); return false; } - if (UP.PartialThreshold != NoThreshold && - UnrolledSize > UP.PartialThreshold) { + if (UP.PartialThreshold != NoThreshold) { // Reduce unroll count to be modulo of TripCount for partial unrolling. - Count = (std::max(UP.PartialThreshold, 3u) - 2) / (LoopSize - 2); + if (UnrolledSize > UP.PartialThreshold) + Count = (std::max(UP.PartialThreshold, 3u) - 2) / (LoopSize - 2); + if (Count > UP.MaxCount) + Count = UP.MaxCount; while (Count != 0 && TripCount % Count != 0) Count--; } Index: test/Transforms/LoopUnroll/partial-unroll-maxcount.ll =================================================================== --- test/Transforms/LoopUnroll/partial-unroll-maxcount.ll +++ test/Transforms/LoopUnroll/partial-unroll-maxcount.ll @@ -0,0 +1,22 @@ +; RUN: opt < %s -S -loop-unroll -unroll-allow-partial -unroll-full-max-count=6 | FileCheck %s + +; Check that we properly round down to an unroll-by-3 instead of emitting a partial unroll by +; 6. +define void @unroll_opt_for_size() nounwind optsize { +entry: + br label %loop + +loop: + %iv = phi i32 [ 0, %entry ], [ %inc, %loop ] + %inc = add i32 %iv, 1 + %exitcnd = icmp uge i32 %inc, 9 + br i1 %exitcnd, label %exit, label %loop + +exit: + ret void +} + +; CHECK: add +; CHECK-NEXT: add +; CHECK-NEXT: add +; CHECK-NEXT: icmp