@@ -191,7 +191,9 @@ static Matcher *FindNodeWithKind(Matcher *M, Matcher::KindTy Kind) {
191
191
return nullptr ;
192
192
}
193
193
194
- // / FactorNodes - Turn matches like this:
194
+ static void FactorNodes (std::unique_ptr<Matcher> &InputMatcherPtr);
195
+
196
+ // / Turn matches like this:
195
197
// / Scope
196
198
// / OPC_CheckType i32
197
199
// / ABC
@@ -203,22 +205,8 @@ static Matcher *FindNodeWithKind(Matcher *M, Matcher::KindTy Kind) {
203
205
// / ABC
204
206
// / XYZ
205
207
// /
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 ());
222
210
223
211
// Okay, pull together the children of the scope node into a vector so we can
224
212
// inspect it more easily.
@@ -353,7 +341,7 @@ static void FactorNodes(std::unique_ptr<Matcher> &InputMatcherPtr) {
353
341
Shared->setNext (new ScopeMatcher (std::move (EqualMatchers)));
354
342
355
343
// Recursively factor the newly created node.
356
- FactorNodes (Shared->getNextPtr ());
344
+ FactorScope (Shared->getNextPtr ());
357
345
}
358
346
359
347
// Put the new Matcher where we started in OptionsToMatch.
@@ -469,7 +457,7 @@ static void FactorNodes(std::unique_ptr<Matcher> &InputMatcherPtr) {
469
457
for (auto &M : Cases) {
470
458
if (ScopeMatcher *SM = dyn_cast<ScopeMatcher>(M.second )) {
471
459
std::unique_ptr<Matcher> Scope (SM);
472
- FactorNodes (Scope);
460
+ FactorScope (Scope);
473
461
M.second = Scope.release ();
474
462
assert (M.second && " null matcher" );
475
463
}
@@ -491,6 +479,20 @@ static void FactorNodes(std::unique_ptr<Matcher> &InputMatcherPtr) {
491
479
Scope->resetChild (i, OptionsToMatch[i]);
492
480
}
493
481
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
+
494
496
void llvm::OptimizeMatcher (std::unique_ptr<Matcher> &MatcherPtr,
495
497
const CodeGenDAGPatterns &CGP) {
496
498
ContractNodes (MatcherPtr, CGP);
0 commit comments