Skip to content

Commit 31db7af

Browse files
authored
[TableGen] Split DAGISelMatcherOpt FactorNodes into 2 functions. NFC (#125330)
The loop at the top of FactorNodes creates additional variables to deal with needing to use a pointer to a unique_ptr instead of a reference. Encapsulate this to its own function for better scoping. This also allows us to directly skip this loop when we already know we have a ScopeMatcher.
1 parent f17b9dd commit 31db7af

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

llvm/utils/TableGen/DAGISelMatcherOpt.cpp

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,9 @@ static Matcher *FindNodeWithKind(Matcher *M, Matcher::KindTy Kind) {
191191
return nullptr;
192192
}
193193

194-
/// FactorNodes - Turn matches like this:
194+
static void FactorNodes(std::unique_ptr<Matcher> &InputMatcherPtr);
195+
196+
/// Turn matches like this:
195197
/// Scope
196198
/// OPC_CheckType i32
197199
/// ABC
@@ -203,22 +205,8 @@ static Matcher *FindNodeWithKind(Matcher *M, Matcher::KindTy Kind) {
203205
/// ABC
204206
/// XYZ
205207
///
206-
static void FactorNodes(std::unique_ptr<Matcher> &InputMatcherPtr) {
207-
// Look for a push node. Iterates instead of recurses to reduce stack usage.
208-
ScopeMatcher *Scope = nullptr;
209-
std::unique_ptr<Matcher> *RebindableMatcherPtr = &InputMatcherPtr;
210-
while (!Scope) {
211-
// If we reached the end of the chain, we're done.
212-
Matcher *N = RebindableMatcherPtr->get();
213-
if (!N)
214-
return;
215-
216-
// If this is not a push node, just scan for one.
217-
Scope = dyn_cast<ScopeMatcher>(N);
218-
if (!Scope)
219-
RebindableMatcherPtr = &(N->getNextPtr());
220-
}
221-
std::unique_ptr<Matcher> &MatcherPtr = *RebindableMatcherPtr;
208+
static void FactorScope(std::unique_ptr<Matcher> &MatcherPtr) {
209+
ScopeMatcher *Scope = cast<ScopeMatcher>(MatcherPtr.get());
222210

223211
// Okay, pull together the children of the scope node into a vector so we can
224212
// inspect it more easily.
@@ -353,7 +341,7 @@ static void FactorNodes(std::unique_ptr<Matcher> &InputMatcherPtr) {
353341
Shared->setNext(new ScopeMatcher(std::move(EqualMatchers)));
354342

355343
// Recursively factor the newly created node.
356-
FactorNodes(Shared->getNextPtr());
344+
FactorScope(Shared->getNextPtr());
357345
}
358346

359347
// Put the new Matcher where we started in OptionsToMatch.
@@ -469,7 +457,7 @@ static void FactorNodes(std::unique_ptr<Matcher> &InputMatcherPtr) {
469457
for (auto &M : Cases) {
470458
if (ScopeMatcher *SM = dyn_cast<ScopeMatcher>(M.second)) {
471459
std::unique_ptr<Matcher> Scope(SM);
472-
FactorNodes(Scope);
460+
FactorScope(Scope);
473461
M.second = Scope.release();
474462
assert(M.second && "null matcher");
475463
}
@@ -491,6 +479,20 @@ static void FactorNodes(std::unique_ptr<Matcher> &InputMatcherPtr) {
491479
Scope->resetChild(i, OptionsToMatch[i]);
492480
}
493481

482+
/// Search a ScopeMatcher to factor with FactorScope.
483+
static void FactorNodes(std::unique_ptr<Matcher> &InputMatcherPtr) {
484+
// Look for a scope matcher. Iterates instead of recurses to reduce stack
485+
// usage.
486+
std::unique_ptr<Matcher> *MatcherPtr = &InputMatcherPtr;
487+
do {
488+
if (isa<ScopeMatcher>(*MatcherPtr))
489+
return FactorScope(*MatcherPtr);
490+
491+
// If this is not a scope matcher, go to the next node.
492+
MatcherPtr = &(MatcherPtr->get()->getNextPtr());
493+
} while (MatcherPtr->get());
494+
}
495+
494496
void llvm::OptimizeMatcher(std::unique_ptr<Matcher> &MatcherPtr,
495497
const CodeGenDAGPatterns &CGP) {
496498
ContractNodes(MatcherPtr, CGP);

0 commit comments

Comments
 (0)