Skip to content

Commit

Permalink
[LVI] Switch from BFS to DFS exploration order
Browse files Browse the repository at this point in the history
This patch changes the order in which LVI explores previously unexplored paths.

Previously, the code used an BFS strategy where each unexplored input was added to the search queue before any of them were explored. This has the effect of causing all inputs to be explored before returning to re-evaluate the merge point (non-local or phi node). This has the unfortunate property of doing redundant work if one of the inputs to the merge is found to be overdefined (i.e. unanalysable). If any input is overdefined, the result of the merge will be too; regardless of the values of other inputs.

The new code uses a DFS strategy where we re-evaluate the merge after evaluating each input. If we discover an overdefined input, we immediately return without exploring other inputs.

We have reports of large (4-10x) improvements of compile time with this patch and some reports of more precise analysis results as well.  See the review discussion for details.  The original motivating case was pr10584.

Differential Revision: https://reviews.llvm.org/D28190



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@294264 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
preames committed Feb 7, 2017
1 parent bcca9da commit 85df517
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions lib/Analysis/LazyValueInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -839,13 +839,19 @@ bool LazyValueInfoImpl::solveBlockValueNonLocal(LVILatticeVal &BBLV,
}

// Loop over all of our predecessors, merging what we know from them into
// result.
bool EdgesMissing = false;
// result. If we encounter an unexplored predecessor, we eagerly explore it
// in a depth first manner. In practice, this has the effect of discovering
// paths we can't analyze eagerly without spending compile times analyzing
// other paths. This heuristic benefits from the fact that predecessors are
// frequently arranged such that dominating ones come first and we quickly
// find a path to function entry. TODO: We should consider explicitly
// canonicalizing to make this true rather than relying on this happy
// accident.
for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
LVILatticeVal EdgeResult;
EdgesMissing |= !getEdgeValue(Val, *PI, BB, EdgeResult);
if (EdgesMissing)
continue;
if (!getEdgeValue(Val, *PI, BB, EdgeResult))
// Explore that input, then return here
return false;

Result.mergeIn(EdgeResult, DL);

Expand All @@ -866,8 +872,6 @@ bool LazyValueInfoImpl::solveBlockValueNonLocal(LVILatticeVal &BBLV,
return true;
}
}
if (EdgesMissing)
return false;

// Return the merged value, which is more precise than 'overdefined'.
assert(!Result.isOverdefined());
Expand All @@ -880,18 +884,18 @@ bool LazyValueInfoImpl::solveBlockValuePHINode(LVILatticeVal &BBLV,
LVILatticeVal Result; // Start Undefined.

// Loop over all of our predecessors, merging what we know from them into
// result.
bool EdgesMissing = false;
// result. See the comment about the chosen traversal order in
// solveBlockValueNonLocal; the same reasoning applies here.
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
BasicBlock *PhiBB = PN->getIncomingBlock(i);
Value *PhiVal = PN->getIncomingValue(i);
LVILatticeVal EdgeResult;
// Note that we can provide PN as the context value to getEdgeValue, even
// though the results will be cached, because PN is the value being used as
// the cache key in the caller.
EdgesMissing |= !getEdgeValue(PhiVal, PhiBB, BB, EdgeResult, PN);
if (EdgesMissing)
continue;
if (!getEdgeValue(PhiVal, PhiBB, BB, EdgeResult, PN))
// Explore that input, then return here
return false;

Result.mergeIn(EdgeResult, DL);

Expand All @@ -905,8 +909,6 @@ bool LazyValueInfoImpl::solveBlockValuePHINode(LVILatticeVal &BBLV,
return true;
}
}
if (EdgesMissing)
return false;

// Return the merged value, which is more precise than 'overdefined'.
assert(!Result.isOverdefined() && "Possible PHI in entry block?");
Expand Down

0 comments on commit 85df517

Please sign in to comment.