Skip to content

Commit 91ac2de

Browse files
committed
TableGen] Split DAGISelMatcherOpt FactorNodes into 2 functions. NFC
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 c3c3262 commit 91ac2de

File tree

1 file changed

+31
-30
lines changed

1 file changed

+31
-30
lines changed

llvm/utils/TableGen/DAGISelMatcherOpt.cpp

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

194-
/// FactorNodes - Turn matches like this:
195-
/// Scope
196-
/// OPC_CheckType i32
197-
/// ABC
198-
/// OPC_CheckType i32
199-
/// XYZ
200-
/// into:
201-
/// OPC_CheckType i32
202-
/// Scope
203-
/// ABC
204-
/// XYZ
205-
///
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;
194+
static void FactorNodes(std::unique_ptr<Matcher> &InputMatcherPtr);
195+
196+
static void FactorScope(std::unique_ptr<Matcher> &MatcherPtr) {
197+
ScopeMatcher *Scope = cast<ScopeMatcher>(MatcherPtr.get());
222198

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

355331
// Recursively factor the newly created node.
356-
FactorNodes(Shared->getNextPtr());
332+
FactorScope(Shared->getNextPtr());
357333
}
358334

359335
// Put the new Matcher where we started in OptionsToMatch.
@@ -470,7 +446,7 @@ static void FactorNodes(std::unique_ptr<Matcher> &InputMatcherPtr) {
470446
for (auto &M : Cases) {
471447
if (ScopeMatcher *SM = dyn_cast<ScopeMatcher>(M.second)) {
472448
std::unique_ptr<Matcher> Scope(SM);
473-
FactorNodes(Scope);
449+
FactorScope(Scope);
474450
M.second = Scope.release();
475451
assert(M.second && "null matcher");
476452
}
@@ -492,6 +468,31 @@ static void FactorNodes(std::unique_ptr<Matcher> &InputMatcherPtr) {
492468
Scope->resetChild(i, OptionsToMatch[i]);
493469
}
494470

471+
/// FactorNodes - Turn matches like this:
472+
/// Scope
473+
/// OPC_CheckType i32
474+
/// ABC
475+
/// OPC_CheckType i32
476+
/// XYZ
477+
/// into:
478+
/// OPC_CheckType i32
479+
/// Scope
480+
/// ABC
481+
/// XYZ
482+
///
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+
495496
void llvm::OptimizeMatcher(std::unique_ptr<Matcher> &MatcherPtr,
496497
const CodeGenDAGPatterns &CGP) {
497498
ContractNodes(MatcherPtr, CGP);

0 commit comments

Comments
 (0)