Skip to content

Commit 9c8dac0

Browse files
committed
Use function_ref, return const reference
1 parent 7a0bf19 commit 9c8dac0

File tree

3 files changed

+46
-42
lines changed

3 files changed

+46
-42
lines changed

llvm/include/llvm/Analysis/ScalarEvolution.h

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,10 +1206,11 @@ class ScalarEvolution {
12061206
///
12071207
/// If \p AllowPredicates is set, this call will try to use a minimal set of
12081208
/// SCEV predicates in order to return an exact answer.
1209-
ExitLimit computeExitLimitFromCond(const Loop *L, Value *ExitCond,
1210-
std::function<LoopGuards()> GetLoopGuards,
1211-
bool ExitIfTrue, bool ControlsOnlyExit,
1212-
bool AllowPredicates = false);
1209+
ExitLimit
1210+
computeExitLimitFromCond(const Loop *L, Value *ExitCond,
1211+
function_ref<const LoopGuards &()> GetLoopGuards,
1212+
bool ExitIfTrue, bool ControlsOnlyExit,
1213+
bool AllowPredicates = false);
12131214

12141215
/// A predicate is said to be monotonically increasing if may go from being
12151216
/// false to being true as the loop iterates, but never the other way
@@ -1841,7 +1842,7 @@ class ScalarEvolution {
18411842
/// this call will try to use a minimal set of SCEV predicates in order to
18421843
/// return an exact answer.
18431844
ExitLimit computeExitLimit(const Loop *L, BasicBlock *ExitingBlock,
1844-
std::function<LoopGuards()> GetLoopGuards,
1845+
function_ref<const LoopGuards &()> GetLoopGuards,
18451846
bool IsOnlyExit, bool AllowPredicates = false);
18461847

18471848
// Helper functions for computeExitLimitFromCond to avoid exponential time
@@ -1876,43 +1877,44 @@ class ScalarEvolution {
18761877

18771878
ExitLimit computeExitLimitFromCondCached(
18781879
ExitLimitCacheTy &Cache, const Loop *L, Value *ExitCond,
1879-
std::function<LoopGuards()> GetLoopGuards, bool ExitIfTrue,
1880+
function_ref<const LoopGuards &()> GetLoopGuards, bool ExitIfTrue,
18801881
bool ControlsOnlyExit, bool AllowPredicates);
18811882
ExitLimit computeExitLimitFromCondImpl(
18821883
ExitLimitCacheTy &Cache, const Loop *L, Value *ExitCond,
1883-
std::function<LoopGuards()> GetLoopGuards, bool ExitIfTrue,
1884+
function_ref<const LoopGuards &()> GetLoopGuards, bool ExitIfTrue,
18841885
bool ControlsOnlyExit, bool AllowPredicates);
18851886
std::optional<ScalarEvolution::ExitLimit> computeExitLimitFromCondFromBinOp(
18861887
ExitLimitCacheTy &Cache, const Loop *L, Value *ExitCond,
1887-
std::function<LoopGuards()> GetLoopGuards, bool ExitIfTrue,
1888+
function_ref<const LoopGuards &()> GetLoopGuards, bool ExitIfTrue,
18881889
bool ControlsOnlyExit, bool AllowPredicates);
18891890

18901891
/// Compute the number of times the backedge of the specified loop will
18911892
/// execute if its exit condition were a conditional branch of the ICmpInst
18921893
/// ExitCond and ExitIfTrue. If AllowPredicates is set, this call will try
18931894
/// to use a minimal set of SCEV predicates in order to return an exact
18941895
/// answer.
1895-
ExitLimit computeExitLimitFromICmp(const Loop *L, ICmpInst *ExitCond,
1896-
std::function<LoopGuards()> GetLoopGuards,
1897-
bool ExitIfTrue, bool IsSubExpr,
1898-
bool AllowPredicates = false);
1896+
ExitLimit
1897+
computeExitLimitFromICmp(const Loop *L, ICmpInst *ExitCond,
1898+
function_ref<const LoopGuards &()> GetLoopGuards,
1899+
bool ExitIfTrue, bool IsSubExpr,
1900+
bool AllowPredicates = false);
18991901

19001902
/// Variant of previous which takes the components representing an ICmp
19011903
/// as opposed to the ICmpInst itself. Note that the prior version can
19021904
/// return more precise results in some cases and is preferred when caller
19031905
/// has a materialized ICmp.
1904-
ExitLimit computeExitLimitFromICmp(const Loop *L, ICmpInst::Predicate Pred,
1905-
const SCEV *LHS, const SCEV *RHS,
1906-
std::function<LoopGuards()> GetLoopGuards,
1907-
bool IsSubExpr,
1908-
bool AllowPredicates = false);
1906+
ExitLimit
1907+
computeExitLimitFromICmp(const Loop *L, ICmpInst::Predicate Pred,
1908+
const SCEV *LHS, const SCEV *RHS,
1909+
function_ref<const LoopGuards &()> GetLoopGuards,
1910+
bool IsSubExpr, bool AllowPredicates = false);
19091911

19101912
/// Compute the number of times the backedge of the specified loop will
19111913
/// execute if its exit condition were a switch with a single exiting case
19121914
/// to ExitingBB.
19131915
ExitLimit computeExitLimitFromSingleExitSwitch(
19141916
const Loop *L, SwitchInst *Switch, BasicBlock *ExitingBB,
1915-
std::function<LoopGuards()> GetLoopGuards, bool IsSubExpr);
1917+
function_ref<const LoopGuards &()> GetLoopGuards, bool IsSubExpr);
19161918

19171919
/// Compute the exit limit of a loop that is controlled by a
19181920
/// "(IV >> 1) != 0" type comparison. We cannot compute the exact trip
@@ -1937,7 +1939,7 @@ class ScalarEvolution {
19371939
/// If AllowPredicates is set, this call will try to use a minimal set of
19381940
/// SCEV predicates in order to return an exact answer.
19391941
ExitLimit howFarToZero(const SCEV *V, const Loop *L,
1940-
std::function<LoopGuards()> GetLoopGuards,
1942+
function_ref<const LoopGuards &()> GetLoopGuards,
19411943
bool IsSubExpr, bool AllowPredicates = false);
19421944

19431945
/// Return the number of times an exit condition checking the specified

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8806,11 +8806,11 @@ ScalarEvolution::computeBackedgeTakenCount(const Loop *L,
88068806
const SCEV *MayExitMaxBECount = nullptr;
88078807
bool MustExitMaxOrZero = false;
88088808
bool IsOnlyExit = ExitingBlocks.size() == 1;
8809-
std::optional<LoopGuards> LoopGuards;
8810-
auto GetLoopGuards = [&LoopGuards, &L, this]() {
8811-
if (!LoopGuards)
8812-
LoopGuards.emplace(LoopGuards::collect(L, *this));
8813-
return *LoopGuards;
8809+
std::optional<LoopGuards> CachedLoopGuards;
8810+
auto GetLoopGuards = [&CachedLoopGuards, &L, this]() -> const LoopGuards & {
8811+
if (!CachedLoopGuards)
8812+
CachedLoopGuards.emplace(LoopGuards::collect(L, *this));
8813+
return *CachedLoopGuards;
88148814
};
88158815

88168816
// Compute the ExitLimit for each loop exit. Use this to populate ExitCounts
@@ -8901,10 +8901,10 @@ ScalarEvolution::computeBackedgeTakenCount(const Loop *L,
89018901
MaxBECount, MaxOrZero);
89028902
}
89038903

8904-
ScalarEvolution::ExitLimit
8905-
ScalarEvolution::computeExitLimit(const Loop *L, BasicBlock *ExitingBlock,
8906-
std::function<LoopGuards()> GetLoopGuards,
8907-
bool IsOnlyExit, bool AllowPredicates) {
8904+
ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimit(
8905+
const Loop *L, BasicBlock *ExitingBlock,
8906+
function_ref<const LoopGuards &()> GetLoopGuards, bool IsOnlyExit,
8907+
bool AllowPredicates) {
89088908
assert(L->contains(ExitingBlock) && "Exit count for non-loop block?");
89098909
// If our exiting block does not dominate the latch, then its connection with
89108910
// loop's exit limit may be far from trivial.
@@ -8942,8 +8942,9 @@ ScalarEvolution::computeExitLimit(const Loop *L, BasicBlock *ExitingBlock,
89428942
}
89438943

89448944
ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromCond(
8945-
const Loop *L, Value *ExitCond, std::function<LoopGuards()> GetLoopGuards,
8946-
bool ExitIfTrue, bool ControlsOnlyExit, bool AllowPredicates) {
8945+
const Loop *L, Value *ExitCond,
8946+
function_ref<const LoopGuards &()> GetLoopGuards, bool ExitIfTrue,
8947+
bool ControlsOnlyExit, bool AllowPredicates) {
89478948
ScalarEvolution::ExitLimitCacheTy Cache(L, ExitIfTrue, AllowPredicates);
89488949
return computeExitLimitFromCondCached(Cache, L, ExitCond, GetLoopGuards,
89498950
ExitIfTrue, ControlsOnlyExit,
@@ -8984,7 +8985,7 @@ void ScalarEvolution::ExitLimitCache::insert(const Loop *L, Value *ExitCond,
89848985

89858986
ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromCondCached(
89868987
ExitLimitCacheTy &Cache, const Loop *L, Value *ExitCond,
8987-
std::function<LoopGuards()> GetLoopGuards, bool ExitIfTrue,
8988+
function_ref<const LoopGuards &()> GetLoopGuards, bool ExitIfTrue,
89888989
bool ControlsOnlyExit, bool AllowPredicates) {
89898990

89908991
if (auto MaybeEL = Cache.find(L, ExitCond, ExitIfTrue, ControlsOnlyExit,
@@ -9000,7 +9001,7 @@ ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromCondCached(
90009001

90019002
ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromCondImpl(
90029003
ExitLimitCacheTy &Cache, const Loop *L, Value *ExitCond,
9003-
std::function<LoopGuards()> GetLoopGuards, bool ExitIfTrue,
9004+
function_ref<const LoopGuards &()> GetLoopGuards, bool ExitIfTrue,
90049005
bool ControlsOnlyExit, bool AllowPredicates) {
90059006
// Handle BinOp conditions (And, Or).
90069007
if (auto LimitFromBinOp = computeExitLimitFromCondFromBinOp(
@@ -9066,7 +9067,7 @@ ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromCondImpl(
90669067
std::optional<ScalarEvolution::ExitLimit>
90679068
ScalarEvolution::computeExitLimitFromCondFromBinOp(
90689069
ExitLimitCacheTy &Cache, const Loop *L, Value *ExitCond,
9069-
std::function<LoopGuards()> GetLoopGuards, bool ExitIfTrue,
9070+
function_ref<const LoopGuards &()> GetLoopGuards, bool ExitIfTrue,
90709071
bool ControlsOnlyExit, bool AllowPredicates) {
90719072
// Check if the controlling expression for this loop is an And or Or.
90729073
Value *Op0, *Op1;
@@ -9147,7 +9148,7 @@ ScalarEvolution::computeExitLimitFromCondFromBinOp(
91479148

91489149
ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromICmp(
91499150
const Loop *L, ICmpInst *ExitCond,
9150-
std::function<LoopGuards()> GetLoopGuards, bool ExitIfTrue,
9151+
function_ref<const LoopGuards &()> GetLoopGuards, bool ExitIfTrue,
91519152
bool ControlsOnlyExit, bool AllowPredicates) {
91529153
// If the condition was exit on true, convert the condition to exit on false
91539154
ICmpInst::Predicate Pred;
@@ -9176,7 +9177,7 @@ ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromICmp(
91769177
}
91779178
ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromICmp(
91789179
const Loop *L, ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS,
9179-
std::function<LoopGuards()> GetLoopGuards, bool ControlsOnlyExit,
9180+
function_ref<const LoopGuards &()> GetLoopGuards, bool ControlsOnlyExit,
91809181
bool AllowPredicates) {
91819182

91829183
// Try to evaluate any dependencies out of the loop.
@@ -9350,7 +9351,7 @@ ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromICmp(
93509351
ScalarEvolution::ExitLimit
93519352
ScalarEvolution::computeExitLimitFromSingleExitSwitch(
93529353
const Loop *L, SwitchInst *Switch, BasicBlock *ExitingBlock,
9353-
std::function<LoopGuards()> GetLoopGuards, bool ControlsOnlyExit) {
9354+
function_ref<const LoopGuards &()> GetLoopGuards, bool ControlsOnlyExit) {
93549355
assert(!L->contains(ExitingBlock) && "Not an exiting block!");
93559356

93569357
// Give up if the exit is the default dest of a switch.
@@ -10504,7 +10505,7 @@ SolveQuadraticAddRecRange(const SCEVAddRecExpr *AddRec,
1050410505

1050510506
ScalarEvolution::ExitLimit
1050610507
ScalarEvolution::howFarToZero(const SCEV *V, const Loop *L,
10507-
std::function<LoopGuards()> GetLoopGuards,
10508+
function_ref<const LoopGuards &()> GetLoopGuards,
1050810509
bool ControlsOnlyExit, bool AllowPredicates) {
1050910510

1051010511
// This is only used for loops with a "x != y" exit test. The exit condition

llvm/lib/Transforms/Scalar/IndVarSimplify.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,11 +1335,12 @@ static bool optimizeLoopExitWithUnknownExitCount(
13351335
Visited.insert(OldCond);
13361336
Worklist.push_back(OldCond);
13371337

1338-
std::optional<ScalarEvolution::LoopGuards> LoopGuards;
1339-
auto GetLoopGuards = [&LoopGuards, &L, &SE]() {
1340-
if (!LoopGuards)
1341-
LoopGuards.emplace(ScalarEvolution::LoopGuards::collect(L, *SE));
1342-
return *LoopGuards;
1338+
std::optional<ScalarEvolution::LoopGuards> CachedLoopGuards;
1339+
auto GetLoopGuards = [&CachedLoopGuards, &L,
1340+
&SE]() -> const ScalarEvolution::LoopGuards & {
1341+
if (!CachedLoopGuards)
1342+
CachedLoopGuards.emplace(ScalarEvolution::LoopGuards::collect(L, *SE));
1343+
return *CachedLoopGuards;
13431344
};
13441345

13451346
auto GoThrough = [&](Value *V) {

0 commit comments

Comments
 (0)