53
53
#include " llvm/ADT/STLExtras.h"
54
54
#include " llvm/ADT/SmallPtrSet.h"
55
55
#include " llvm/ADT/Statistic.h"
56
+ #include " llvm/Analysis/BlockFrequencyInfo.h"
56
57
#include " llvm/Analysis/DomTreeUpdater.h"
57
58
#include " llvm/Analysis/GlobalsModRef.h"
58
59
#include " llvm/Analysis/InstructionSimplify.h"
75
76
#include " llvm/IR/Module.h"
76
77
#include " llvm/InitializePasses.h"
77
78
#include " llvm/Pass.h"
79
+ #include " llvm/Support/CommandLine.h"
78
80
#include " llvm/Support/Debug.h"
79
81
#include " llvm/Support/raw_ostream.h"
80
82
#include " llvm/Transforms/Scalar.h"
@@ -87,6 +89,11 @@ STATISTIC(NumEliminated, "Number of tail calls removed");
87
89
STATISTIC (NumRetDuped, " Number of return duplicated" );
88
90
STATISTIC (NumAccumAdded, " Number of accumulators introduced" );
89
91
92
+ static cl::opt<bool > ForceDisableBFI (
93
+ " tre-disable-entrycount-recompute" , cl::init(false ), cl::Hidden,
94
+ cl::desc(" Force disabling recomputing of function entry count, on "
95
+ " successful tail recursion elimination." ));
96
+
90
97
// / Scan the specified function for alloca instructions.
91
98
// / If it contains any dynamic allocas, returns false.
92
99
static bool canTRE (Function &F) {
@@ -409,6 +416,8 @@ class TailRecursionEliminator {
409
416
AliasAnalysis *AA;
410
417
OptimizationRemarkEmitter *ORE;
411
418
DomTreeUpdater &DTU;
419
+ const uint64_t OrigEntryBBFreq;
420
+ DenseMap<const BasicBlock *, uint64_t > OriginalBBFreqs;
412
421
413
422
// The below are shared state we want to have available when eliminating any
414
423
// calls in the function. There values should be populated by
@@ -438,8 +447,23 @@ class TailRecursionEliminator {
438
447
439
448
TailRecursionEliminator (Function &F, const TargetTransformInfo *TTI,
440
449
AliasAnalysis *AA, OptimizationRemarkEmitter *ORE,
441
- DomTreeUpdater &DTU)
442
- : F(F), TTI(TTI), AA(AA), ORE(ORE), DTU(DTU) {}
450
+ DomTreeUpdater &DTU, BlockFrequencyInfo *BFI)
451
+ : F(F), TTI(TTI), AA(AA), ORE(ORE), DTU(DTU),
452
+ OrigEntryBBFreq (
453
+ BFI ? BFI->getBlockFreq (&F.getEntryBlock()).getFrequency() : 0U) {
454
+ if (BFI) {
455
+ auto EC = F.getEntryCount ();
456
+ (void )EC;
457
+ assert (
458
+ (EC.has_value () && EC->getCount () != 0 && OrigEntryBBFreq) &&
459
+ " If the function has an entry count, its entry basic block should "
460
+ " have a non-zero frequency. Pass a nullptr BFI if the function has "
461
+ " no entry count" );
462
+
463
+ for (const auto &BB : F)
464
+ OriginalBBFreqs.insert ({&BB, BFI->getBlockFreq (&BB).getFrequency ()});
465
+ }
466
+ }
443
467
444
468
CallInst *findTRECandidate (BasicBlock *BB);
445
469
@@ -460,7 +484,7 @@ class TailRecursionEliminator {
460
484
public:
461
485
static bool eliminate (Function &F, const TargetTransformInfo *TTI,
462
486
AliasAnalysis *AA, OptimizationRemarkEmitter *ORE,
463
- DomTreeUpdater &DTU);
487
+ DomTreeUpdater &DTU, BlockFrequencyInfo *BFI );
464
488
};
465
489
} // namespace
466
490
@@ -746,6 +770,17 @@ bool TailRecursionEliminator::eliminateCall(CallInst *CI) {
746
770
CI->eraseFromParent (); // Remove call.
747
771
DTU.applyUpdates ({{DominatorTree::Insert, BB, HeaderBB}});
748
772
++NumEliminated;
773
+ if (OrigEntryBBFreq) {
774
+ assert (F.getEntryCount ().has_value ());
775
+ auto It = OriginalBBFreqs.find (BB);
776
+ assert (It != OriginalBBFreqs.end ());
777
+ auto RelativeBBFreq =
778
+ static_cast <double >(It->second ) / static_cast <double >(OrigEntryBBFreq);
779
+ auto OldEntryCount = F.getEntryCount ()->getCount ();
780
+ auto ToSubtract = static_cast <uint64_t >(RelativeBBFreq * OldEntryCount);
781
+ assert (OldEntryCount > ToSubtract);
782
+ F.setEntryCount (OldEntryCount - ToSubtract, F.getEntryCount ()->getType ());
783
+ }
749
784
return true ;
750
785
}
751
786
@@ -872,7 +907,8 @@ bool TailRecursionEliminator::eliminate(Function &F,
872
907
const TargetTransformInfo *TTI,
873
908
AliasAnalysis *AA,
874
909
OptimizationRemarkEmitter *ORE,
875
- DomTreeUpdater &DTU) {
910
+ DomTreeUpdater &DTU,
911
+ BlockFrequencyInfo *BFI) {
876
912
if (F.getFnAttribute (" disable-tail-calls" ).getValueAsBool ())
877
913
return false ;
878
914
@@ -888,7 +924,7 @@ bool TailRecursionEliminator::eliminate(Function &F,
888
924
return MadeChange;
889
925
890
926
// Change any tail recursive calls to loops.
891
- TailRecursionEliminator TRE (F, TTI, AA, ORE, DTU);
927
+ TailRecursionEliminator TRE (F, TTI, AA, ORE, DTU, BFI );
892
928
893
929
for (BasicBlock &BB : F)
894
930
MadeChange |= TRE.processBlock (BB);
@@ -930,7 +966,8 @@ struct TailCallElim : public FunctionPass {
930
966
return TailRecursionEliminator::eliminate (
931
967
F, &getAnalysis<TargetTransformInfoWrapperPass>().getTTI (F),
932
968
&getAnalysis<AAResultsWrapperPass>().getAAResults (),
933
- &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE (), DTU);
969
+ &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE (), DTU,
970
+ nullptr );
934
971
}
935
972
};
936
973
}
@@ -953,14 +990,22 @@ PreservedAnalyses TailCallElimPass::run(Function &F,
953
990
954
991
TargetTransformInfo &TTI = AM.getResult <TargetIRAnalysis>(F);
955
992
AliasAnalysis &AA = AM.getResult <AAManager>(F);
993
+ // This must come first. It needs the 2 analyses, meaning, if it came after
994
+ // the lines asking for the cached result, should they be nullptr (which, in
995
+ // the case of the PDT, is likely), updates to the trees would be missed.
996
+ auto *BFI = (!ForceDisableBFI && UpdateFunctionEntryCount &&
997
+ F.getEntryCount ().has_value () && F.getEntryCount ()->getCount ())
998
+ ? &AM.getResult <BlockFrequencyAnalysis>(F)
999
+ : nullptr ;
956
1000
auto &ORE = AM.getResult <OptimizationRemarkEmitterAnalysis>(F);
957
1001
auto *DT = AM.getCachedResult <DominatorTreeAnalysis>(F);
958
1002
auto *PDT = AM.getCachedResult <PostDominatorTreeAnalysis>(F);
959
1003
// There is no noticable performance difference here between Lazy and Eager
960
1004
// UpdateStrategy based on some test results. It is feasible to switch the
961
1005
// UpdateStrategy to Lazy if we find it profitable later.
962
1006
DomTreeUpdater DTU (DT, PDT, DomTreeUpdater::UpdateStrategy::Eager);
963
- bool Changed = TailRecursionEliminator::eliminate (F, &TTI, &AA, &ORE, DTU);
1007
+ bool Changed =
1008
+ TailRecursionEliminator::eliminate (F, &TTI, &AA, &ORE, DTU, BFI);
964
1009
965
1010
if (!Changed)
966
1011
return PreservedAnalyses::all ();
0 commit comments