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<Matcher> &MatcherPtr) {
18 // If we reached the end of the chain, we're done.
19 Matcher *N = MatcherPtr.get();
22 // If we have a scope node, walk down both edges.
23 if (ScopeMatcher *Push = dyn_cast<ScopeMatcher>(N))
24 ContractNodes(Push->getCheckPtr());
26 // If we found a movechild node with a node that comes in a 'foochild' form,
28 if (MoveChildMatcher *MC = dyn_cast<MoveChildMatcher>(N)) {
30 if (RecordMatcher *RM = dyn_cast<RecordMatcher>(MC->getNext()))
31 New = new RecordChildMatcher(MC->getChildNo(), RM->getWhatFor());
33 if (CheckTypeMatcher *CT= dyn_cast<CheckTypeMatcher>(MC->getNext()))
34 New = new CheckChildTypeMatcher(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 (MoveChildMatcher *MC = dyn_cast<MoveChildMatcher>(N))
47 if (MoveParentMatcher *MP =
48 dyn_cast<MoveParentMatcher>(MC->getNext())) {
49 MatcherPtr.reset(MP->takeNext());
50 return ContractNodes(MatcherPtr);
53 ContractNodes(N->getNextPtr());
56 static void FactorNodes(OwningPtr<Matcher> &MatcherPtr) {
57 // If we reached the end of the chain, we're done.
58 Matcher *N = MatcherPtr.get();
61 // If this is not a push node, just scan for one.
62 if (!isa<ScopeMatcher>(N))
63 return FactorNodes(N->getNextPtr());
65 // Okay, pull together the series of linear push nodes into a vector so we can
66 // inspect it more easily.
67 SmallVector<Matcher*, 32> OptionsToMatch;
70 for (; ScopeMatcher *PMN = dyn_cast<ScopeMatcher>(CurNode);
71 CurNode = PMN->getNext())
72 OptionsToMatch.push_back(PMN->getCheck());
73 OptionsToMatch.push_back(CurNode);
78 Matcher *llvm::OptimizeMatcher(Matcher *TheMatcher) {
79 OwningPtr<Matcher> MatcherPtr(TheMatcher);
80 ContractNodes(MatcherPtr);
81 FactorNodes(MatcherPtr);
82 return MatcherPtr.take();