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"
17 static void ContractNodes(OwningPtr<MatcherNode> &Matcher) {
18 // If we reached the end of the chain, we're done.
19 MatcherNode *N = Matcher.get();
22 // If we have a push node, walk down both edges.
23 if (PushMatcherNode *Push = dyn_cast<PushMatcherNode>(N))
24 ContractNodes(Push->getFailurePtr());
26 // If we found a movechild node with a node that comes in a 'foochild' form,
28 if (MoveChildMatcherNode *MC = dyn_cast<MoveChildMatcherNode>(N)) {
29 if (RecordMatcherNode *RM = dyn_cast<RecordMatcherNode>(MC->getNext())) {
31 = new RecordChildMatcherNode(MC->getChildNo(), RM->getWhatFor());
32 New->setNext(Matcher.take());
34 MC->setNext(RM->takeNext());
35 return ContractNodes(Matcher);
39 if (MoveChildMatcherNode *MC = dyn_cast<MoveChildMatcherNode>(N))
40 if (MoveParentMatcherNode *MP =
41 dyn_cast<MoveParentMatcherNode>(MC->getNext())) {
42 Matcher.reset(MP->takeNext());
43 return ContractNodes(Matcher);
46 ContractNodes(N->getNextPtr());
50 MatcherNode *llvm::OptimizeMatcher(MatcherNode *Matcher) {
51 OwningPtr<MatcherNode> MatcherPtr(Matcher);
52 ContractNodes(MatcherPtr);
53 return MatcherPtr.take();