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,21 @@ 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
+ assert (
456
+ (F.getEntryCount ()->getCount () != 0 == OrigEntryBBFreq != 0 ) &&
457
+ " If the function has an entry count, its entry basic block should "
458
+ " have a non-zero frequency. Pass a nullptr BFI if the function has "
459
+ " no entry count" );
460
+
461
+ for (const auto &BB : F)
462
+ OriginalBBFreqs.insert ({&BB, BFI->getBlockFreq (&BB).getFrequency ()});
463
+ }
464
+ }
443
465
444
466
CallInst *findTRECandidate (BasicBlock *BB);
445
467
@@ -460,7 +482,7 @@ class TailRecursionEliminator {
460
482
public:
461
483
static bool eliminate (Function &F, const TargetTransformInfo *TTI,
462
484
AliasAnalysis *AA, OptimizationRemarkEmitter *ORE,
463
- DomTreeUpdater &DTU);
485
+ DomTreeUpdater &DTU, BlockFrequencyInfo *BFI );
464
486
};
465
487
} // namespace
466
488
@@ -746,6 +768,17 @@ bool TailRecursionEliminator::eliminateCall(CallInst *CI) {
746
768
CI->eraseFromParent (); // Remove call.
747
769
DTU.applyUpdates ({{DominatorTree::Insert, BB, HeaderBB}});
748
770
++NumEliminated;
771
+ if (OrigEntryBBFreq) {
772
+ assert (F.getEntryCount ().has_value ());
773
+ auto It = OriginalBBFreqs.find (BB);
774
+ assert (It != OriginalBBFreqs.end ());
775
+ auto RelativeBBFreq =
776
+ static_cast <double >(It->second ) / static_cast <double >(OrigEntryBBFreq);
777
+ auto OldEntryCount = F.getEntryCount ()->getCount ();
778
+ auto ToSubtract = static_cast <uint64_t >(RelativeBBFreq * OldEntryCount);
779
+ assert (OldEntryCount > ToSubtract);
780
+ F.setEntryCount (OldEntryCount - ToSubtract, F.getEntryCount ()->getType ());
781
+ }
749
782
return true ;
750
783
}
751
784
@@ -872,7 +905,8 @@ bool TailRecursionEliminator::eliminate(Function &F,
872
905
const TargetTransformInfo *TTI,
873
906
AliasAnalysis *AA,
874
907
OptimizationRemarkEmitter *ORE,
875
- DomTreeUpdater &DTU) {
908
+ DomTreeUpdater &DTU,
909
+ BlockFrequencyInfo *BFI) {
876
910
if (F.getFnAttribute (" disable-tail-calls" ).getValueAsBool ())
877
911
return false ;
878
912
@@ -888,7 +922,7 @@ bool TailRecursionEliminator::eliminate(Function &F,
888
922
return MadeChange;
889
923
890
924
// Change any tail recursive calls to loops.
891
- TailRecursionEliminator TRE (F, TTI, AA, ORE, DTU);
925
+ TailRecursionEliminator TRE (F, TTI, AA, ORE, DTU, BFI );
892
926
893
927
for (BasicBlock &BB : F)
894
928
MadeChange |= TRE.processBlock (BB);
@@ -930,7 +964,8 @@ struct TailCallElim : public FunctionPass {
930
964
return TailRecursionEliminator::eliminate (
931
965
F, &getAnalysis<TargetTransformInfoWrapperPass>().getTTI (F),
932
966
&getAnalysis<AAResultsWrapperPass>().getAAResults (),
933
- &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE (), DTU);
967
+ &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE (), DTU,
968
+ nullptr );
934
969
}
935
970
};
936
971
}
@@ -953,14 +988,22 @@ PreservedAnalyses TailCallElimPass::run(Function &F,
953
988
954
989
TargetTransformInfo &TTI = AM.getResult <TargetIRAnalysis>(F);
955
990
AliasAnalysis &AA = AM.getResult <AAManager>(F);
991
+ // This must come first. It needs the 2 analyses, meaning, if it came after
992
+ // the lines asking for the cached result, should they be nullptr (which, in
993
+ // the case of the PDT, is likely), updates to the trees would be missed.
994
+ auto *BFI = (!ForceDisableBFI && UpdateFunctionEntryCount &&
995
+ F.getEntryCount ().has_value ())
996
+ ? &AM.getResult <BlockFrequencyAnalysis>(F)
997
+ : nullptr ;
956
998
auto &ORE = AM.getResult <OptimizationRemarkEmitterAnalysis>(F);
957
999
auto *DT = AM.getCachedResult <DominatorTreeAnalysis>(F);
958
1000
auto *PDT = AM.getCachedResult <PostDominatorTreeAnalysis>(F);
959
1001
// There is no noticable performance difference here between Lazy and Eager
960
1002
// UpdateStrategy based on some test results. It is feasible to switch the
961
1003
// UpdateStrategy to Lazy if we find it profitable later.
962
1004
DomTreeUpdater DTU (DT, PDT, DomTreeUpdater::UpdateStrategy::Eager);
963
- bool Changed = TailRecursionEliminator::eliminate (F, &TTI, &AA, &ORE, DTU);
1005
+ bool Changed =
1006
+ TailRecursionEliminator::eliminate (F, &TTI, &AA, &ORE, DTU, BFI);
964
1007
965
1008
if (!Changed)
966
1009
return PreservedAnalyses::all ();
0 commit comments