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> &MatcherPtr) {
18 // If we reached the end of the chain, we're done.
19 MatcherNode *N = MatcherPtr.get();
22 // If we have a scope node, walk down both edges.
23 if (ScopeMatcherNode *Push = dyn_cast<ScopeMatcherNode>(N))
24 ContractNodes(Push->getCheckPtr());
26 // If we found a movechild node with a node that comes in a 'foochild' form,
28 if (MoveChildMatcherNode *MC = dyn_cast<MoveChildMatcherNode>(N)) {
30 if (RecordMatcherNode *RM = dyn_cast<RecordMatcherNode>(MC->getNext()))
31 New = new RecordChildMatcherNode(MC->getChildNo(), RM->getWhatFor());
33 if (CheckTypeMatcherNode *CT= dyn_cast<CheckTypeMatcherNode>(MC->getNext()))
34 New = new CheckChildTypeMatcherNode(MC->getChildNo(), CT->getType());
37 // Insert the new node.
38 New->setNext(MatcherPtr.take());
39 MatcherPtr.reset(New);
40 // Remove the old one.
41 MC->setNext(MC->getNext()->takeNext());
42 return ContractNodes(MatcherPtr);
46 if (MoveChildMatcherNode *MC = dyn_cast<MoveChildMatcherNode>(N))
47 if (MoveParentMatcherNode *MP =
48 dyn_cast<MoveParentMatcherNode>(MC->getNext())) {
49 MatcherPtr.reset(MP->takeNext());
50 return ContractNodes(MatcherPtr);
53 ContractNodes(N->getNextPtr());
56 MatcherNode *llvm::OptimizeMatcher(MatcherNode *Matcher) {
57 OwningPtr<MatcherNode> MatcherPtr(Matcher);
58 ContractNodes(MatcherPtr);
59 return MatcherPtr.take();