1 //===- DAGISelMatcherOpt.cpp - Optimize a DAG Matcher ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the DAG Matcher optimizer.
12 //===----------------------------------------------------------------------===//
14 #include "DAGISelMatcher.h"
18 static void FormRecordChildNodes(OwningPtr<MatcherNode> &Matcher) {
19 // If we reached the end of the chain, we're done.
20 MatcherNode *N = Matcher.get();
23 // If we have a push node, walk down both edges.
24 if (PushMatcherNode *Push = dyn_cast<PushMatcherNode>(N))
25 FormRecordChildNodes(Push->getFailurePtr());
27 // If we found a movechild node, check to see if our pattern matches.
28 if (MoveChildMatcherNode *MC = dyn_cast<MoveChildMatcherNode>(N)) {
29 if (RecordMatcherNode *RM = dyn_cast<RecordMatcherNode>(MC->getNext()))
30 if (MoveParentMatcherNode *MP =
31 dyn_cast<MoveParentMatcherNode>(RM->getNext())) {
33 = new RecordChildMatcherNode(MC->getChildNo(), RM->getWhatFor());
34 New->setNext(MP->takeNext());
36 return FormRecordChildNodes(Matcher);
40 FormRecordChildNodes(N->getNextPtr());
44 MatcherNode *llvm::OptimizeMatcher(MatcherNode *Matcher) {
45 OwningPtr<MatcherNode> MatcherPtr(Matcher);
46 FormRecordChildNodes(MatcherPtr);
47 return MatcherPtr.take();