1- // ===- UnrollLoopPeel .cpp - Loop peeling utilities ------------------------===//
1+ // ===- LoopPeel .cpp ------------------------------- ------------------------===//
22//
33// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44// See https://llvm.org/LICENSE.txt for license information.
55// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66//
77// ===----------------------------------------------------------------------===//
88//
9- // This file implements some loop unrolling utilities for peeling loops
10- // with dynamically inferred (from PGO) trip counts. See LoopUnroll.cpp for
11- // unrolling loops with compile-time constant trip counts.
12- //
9+ // Loop Peeling Utilities.
1310// ===----------------------------------------------------------------------===//
1411
12+ #include " llvm/Transforms/Utils/LoopPeel.h"
1513#include " llvm/ADT/DenseMap.h"
1614#include " llvm/ADT/Optional.h"
1715#include " llvm/ADT/SmallVector.h"
4947using namespace llvm ;
5048using namespace llvm ::PatternMatch;
5149
52- #define DEBUG_TYPE " loop-unroll "
50+ #define DEBUG_TYPE " loop-peel "
5351
5452STATISTIC (NumPeeled, " Number of loops peeled" );
5553
54+ static cl::opt<unsigned > UnrollPeelCount (
55+ " unroll-peel-count" , cl::Hidden,
56+ cl::desc (" Set the unroll peeling count, for testing purposes" ));
57+
58+ static cl::opt<bool >
59+ UnrollAllowPeeling (" unroll-allow-peeling" , cl::init(true ), cl::Hidden,
60+ cl::desc(" Allows loops to be peeled when the dynamic "
61+ " trip count is known to be low." ));
62+
63+ static cl::opt<bool >
64+ UnrollAllowLoopNestsPeeling (" unroll-allow-loop-nests-peeling" ,
65+ cl::init (false ), cl::Hidden,
66+ cl::desc(" Allows loop nests to be peeled." ));
67+
5668static cl::opt<unsigned > UnrollPeelMaxCount (
5769 " unroll-peel-max-count" , cl::init(7 ), cl::Hidden,
5870 cl::desc(" Max average trip count which will cause loop peeling." ));
@@ -278,9 +290,9 @@ static unsigned countToEliminateCompares(Loop &L, unsigned MaxPeelCount,
278290
279291// Return the number of iterations we want to peel off.
280292void llvm::computePeelCount (Loop *L, unsigned LoopSize,
281- TargetTransformInfo::UnrollingPreferences &UP ,
282293 TargetTransformInfo::PeelingPreferences &PP ,
283- unsigned &TripCount, ScalarEvolution &SE ) {
294+ unsigned &TripCount, ScalarEvolution &SE ,
295+ unsigned Threshold) {
284296 assert (LoopSize > 0 && " Zero loop size is not allowed!" );
285297 // Save the PP.PeelCount value set by the target in
286298 // TTI.getPeelingPreferences or by the flag -unroll-peel-count.
@@ -322,7 +334,7 @@ void llvm::computePeelCount(Loop *L, unsigned LoopSize,
322334 // maximum number of iterations among these values, thus turning all those
323335 // Phis into invariants.
324336 // First, check that we can peel at least one iteration.
325- if (2 * LoopSize <= UP . Threshold && UnrollPeelMaxCount > 0 ) {
337+ if (2 * LoopSize <= Threshold && UnrollPeelMaxCount > 0 ) {
326338 // Store the pre-calculated values here.
327339 SmallDenseMap<PHINode *, unsigned > IterationsToInvariance;
328340 // Now go through all Phis to calculate their the number of iterations they
@@ -342,7 +354,7 @@ void llvm::computePeelCount(Loop *L, unsigned LoopSize,
342354
343355 // Pay respect to limitations implied by loop size and the max peel count.
344356 unsigned MaxPeelCount = UnrollPeelMaxCount;
345- MaxPeelCount = std::min (MaxPeelCount, UP . Threshold / LoopSize - 1 );
357+ MaxPeelCount = std::min (MaxPeelCount, Threshold / LoopSize - 1 );
346358
347359 DesiredPeelCount = std::max (DesiredPeelCount,
348360 countToEliminateCompares (*L, MaxPeelCount, SE ));
@@ -385,7 +397,7 @@ void llvm::computePeelCount(Loop *L, unsigned LoopSize,
385397
386398 if (*PeelCount) {
387399 if ((*PeelCount + AlreadyPeeled <= UnrollPeelMaxCount) &&
388- (LoopSize * (*PeelCount + 1 ) <= UP . Threshold )) {
400+ (LoopSize * (*PeelCount + 1 ) <= Threshold)) {
389401 LLVM_DEBUG (dbgs () << " Peeling first " << *PeelCount
390402 << " iterations.\n " );
391403 PP .PeelCount = *PeelCount;
@@ -396,7 +408,7 @@ void llvm::computePeelCount(Loop *L, unsigned LoopSize,
396408 LLVM_DEBUG (dbgs () << " Max peel count: " << UnrollPeelMaxCount << " \n " );
397409 LLVM_DEBUG (dbgs () << " Peel cost: " << LoopSize * (*PeelCount + 1 )
398410 << " \n " );
399- LLVM_DEBUG (dbgs () << " Max peel cost: " << UP . Threshold << " \n " );
411+ LLVM_DEBUG (dbgs () << " Max peel cost: " << Threshold << " \n " );
400412 }
401413 }
402414}
@@ -491,7 +503,7 @@ static void fixupBranchWeights(BasicBlock *Header, BranchInst *LatchBR,
491503// / instructions in the last peeled-off iteration.
492504static void cloneLoopBlocks (
493505 Loop *L, unsigned IterNumber, BasicBlock *InsertTop, BasicBlock *InsertBot,
494- SmallVectorImpl<std::pair<BasicBlock *, BasicBlock *> > &ExitEdges,
506+ SmallVectorImpl<std::pair<BasicBlock *, BasicBlock *>> &ExitEdges,
495507 SmallVectorImpl<BasicBlock *> &NewBlocks, LoopBlocksDFS &LoopBlocks,
496508 ValueToValueMapTy &VMap, ValueToValueMapTy &LVMap, DominatorTree *DT ,
497509 LoopInfo *LI ) {
@@ -599,6 +611,40 @@ static void cloneLoopBlocks(
599611 LVMap[KV .first ] = KV .second ;
600612}
601613
614+ TargetTransformInfo::PeelingPreferences llvm::gatherPeelingPreferences (
615+ Loop *L, ScalarEvolution &SE , const TargetTransformInfo &TTI ,
616+ Optional<bool > UserAllowPeeling,
617+ Optional<bool > UserAllowProfileBasedPeeling, bool UnrollingSpecficValues) {
618+ TargetTransformInfo::PeelingPreferences PP ;
619+
620+ // Set the default values.
621+ PP .PeelCount = 0 ;
622+ PP .AllowPeeling = true ;
623+ PP .AllowLoopNestsPeeling = false ;
624+ PP .PeelProfiledIterations = true ;
625+
626+ // Get the target specifc values.
627+ TTI .getPeelingPreferences (L, SE , PP );
628+
629+ // User specified values using cl::opt.
630+ if (UnrollingSpecficValues) {
631+ if (UnrollPeelCount.getNumOccurrences () > 0 )
632+ PP .PeelCount = UnrollPeelCount;
633+ if (UnrollAllowPeeling.getNumOccurrences () > 0 )
634+ PP .AllowPeeling = UnrollAllowPeeling;
635+ if (UnrollAllowLoopNestsPeeling.getNumOccurrences () > 0 )
636+ PP .AllowLoopNestsPeeling = UnrollAllowLoopNestsPeeling;
637+ }
638+
639+ // User specifed values provided by argument.
640+ if (UserAllowPeeling.hasValue ())
641+ PP .AllowPeeling = *UserAllowPeeling;
642+ if (UserAllowProfileBasedPeeling.hasValue ())
643+ PP .PeelProfiledIterations = *UserAllowProfileBasedPeeling;
644+
645+ return PP ;
646+ }
647+
602648// / Peel off the first \p PeelCount iterations of loop \p L.
603649// /
604650// / Note that this does not peel them off as a single straight-line block.
@@ -609,8 +655,8 @@ static void cloneLoopBlocks(
609655// / for the bulk of dynamic execution, can be further simplified by scalar
610656// / optimizations.
611657bool llvm::peelLoop (Loop *L, unsigned PeelCount, LoopInfo *LI ,
612- ScalarEvolution *SE , DominatorTree *DT ,
613- AssumptionCache * AC , bool PreserveLCSSA) {
658+ ScalarEvolution *SE , DominatorTree *DT , AssumptionCache * AC ,
659+ bool PreserveLCSSA) {
614660 assert (PeelCount > 0 && " Attempt to peel out zero iterations?" );
615661 assert (canPeel (L) && " Attempt to peel a loop which is not peelable?" );
616662
0 commit comments