1
- // ===- UnrollLoopPeel .cpp - Loop peeling utilities ------------------------===//
1
+ // ===- LoopPeel .cpp ------------------------------- ------------------------===//
2
2
//
3
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
4
// See https://llvm.org/LICENSE.txt for license information.
5
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
6
//
7
7
// ===----------------------------------------------------------------------===//
8
8
//
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.
13
10
// ===----------------------------------------------------------------------===//
14
11
12
+ #include " llvm/Transforms/Utils/LoopPeel.h"
15
13
#include " llvm/ADT/DenseMap.h"
16
14
#include " llvm/ADT/Optional.h"
17
15
#include " llvm/ADT/SmallVector.h"
49
47
using namespace llvm ;
50
48
using namespace llvm ::PatternMatch;
51
49
52
- #define DEBUG_TYPE " loop-unroll "
50
+ #define DEBUG_TYPE " loop-peel "
53
51
54
52
STATISTIC (NumPeeled, " Number of loops peeled" );
55
53
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
+
56
68
static cl::opt<unsigned > UnrollPeelMaxCount (
57
69
" unroll-peel-max-count" , cl::init(7 ), cl::Hidden,
58
70
cl::desc(" Max average trip count which will cause loop peeling." ));
@@ -278,9 +290,9 @@ static unsigned countToEliminateCompares(Loop &L, unsigned MaxPeelCount,
278
290
279
291
// Return the number of iterations we want to peel off.
280
292
void llvm::computePeelCount (Loop *L, unsigned LoopSize,
281
- TargetTransformInfo::UnrollingPreferences &UP,
282
293
TargetTransformInfo::PeelingPreferences &PP,
283
- unsigned &TripCount, ScalarEvolution &SE) {
294
+ unsigned &TripCount, ScalarEvolution &SE,
295
+ unsigned Threshold) {
284
296
assert (LoopSize > 0 && " Zero loop size is not allowed!" );
285
297
// Save the PP.PeelCount value set by the target in
286
298
// TTI.getPeelingPreferences or by the flag -unroll-peel-count.
@@ -322,7 +334,7 @@ void llvm::computePeelCount(Loop *L, unsigned LoopSize,
322
334
// maximum number of iterations among these values, thus turning all those
323
335
// Phis into invariants.
324
336
// 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 ) {
326
338
// Store the pre-calculated values here.
327
339
SmallDenseMap<PHINode *, unsigned > IterationsToInvariance;
328
340
// Now go through all Phis to calculate their the number of iterations they
@@ -342,7 +354,7 @@ void llvm::computePeelCount(Loop *L, unsigned LoopSize,
342
354
343
355
// Pay respect to limitations implied by loop size and the max peel count.
344
356
unsigned MaxPeelCount = UnrollPeelMaxCount;
345
- MaxPeelCount = std::min (MaxPeelCount, UP. Threshold / LoopSize - 1 );
357
+ MaxPeelCount = std::min (MaxPeelCount, Threshold / LoopSize - 1 );
346
358
347
359
DesiredPeelCount = std::max (DesiredPeelCount,
348
360
countToEliminateCompares (*L, MaxPeelCount, SE));
@@ -385,7 +397,7 @@ void llvm::computePeelCount(Loop *L, unsigned LoopSize,
385
397
386
398
if (*PeelCount) {
387
399
if ((*PeelCount + AlreadyPeeled <= UnrollPeelMaxCount) &&
388
- (LoopSize * (*PeelCount + 1 ) <= UP. Threshold )) {
400
+ (LoopSize * (*PeelCount + 1 ) <= Threshold)) {
389
401
LLVM_DEBUG (dbgs () << " Peeling first " << *PeelCount
390
402
<< " iterations.\n " );
391
403
PP.PeelCount = *PeelCount;
@@ -396,7 +408,7 @@ void llvm::computePeelCount(Loop *L, unsigned LoopSize,
396
408
LLVM_DEBUG (dbgs () << " Max peel count: " << UnrollPeelMaxCount << " \n " );
397
409
LLVM_DEBUG (dbgs () << " Peel cost: " << LoopSize * (*PeelCount + 1 )
398
410
<< " \n " );
399
- LLVM_DEBUG (dbgs () << " Max peel cost: " << UP. Threshold << " \n " );
411
+ LLVM_DEBUG (dbgs () << " Max peel cost: " << Threshold << " \n " );
400
412
}
401
413
}
402
414
}
@@ -491,7 +503,7 @@ static void fixupBranchWeights(BasicBlock *Header, BranchInst *LatchBR,
491
503
// / instructions in the last peeled-off iteration.
492
504
static void cloneLoopBlocks (
493
505
Loop *L, unsigned IterNumber, BasicBlock *InsertTop, BasicBlock *InsertBot,
494
- SmallVectorImpl<std::pair<BasicBlock *, BasicBlock *> > &ExitEdges,
506
+ SmallVectorImpl<std::pair<BasicBlock *, BasicBlock *>> &ExitEdges,
495
507
SmallVectorImpl<BasicBlock *> &NewBlocks, LoopBlocksDFS &LoopBlocks,
496
508
ValueToValueMapTy &VMap, ValueToValueMapTy &LVMap, DominatorTree *DT,
497
509
LoopInfo *LI) {
@@ -599,6 +611,40 @@ static void cloneLoopBlocks(
599
611
LVMap[KV.first ] = KV.second ;
600
612
}
601
613
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
+
602
648
// / Peel off the first \p PeelCount iterations of loop \p L.
603
649
// /
604
650
// / Note that this does not peel them off as a single straight-line block.
@@ -609,8 +655,8 @@ static void cloneLoopBlocks(
609
655
// / for the bulk of dynamic execution, can be further simplified by scalar
610
656
// / optimizations.
611
657
bool 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) {
614
660
assert (PeelCount > 0 && " Attempt to peel out zero iterations?" );
615
661
assert (canPeel (L) && " Attempt to peel a loop which is not peelable?" );
616
662
0 commit comments