rename fooMatcherNode to fooMatcher.
[oota-llvm.git] / utils / TableGen / DAGISelMatcherOpt.cpp
1 //===- DAGISelMatcherOpt.cpp - Optimize a DAG Matcher ---------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the DAG Matcher optimizer.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "DAGISelMatcher.h"
15 using namespace llvm;
16
17 static void ContractNodes(OwningPtr<Matcher> &MatcherPtr) {
18   // If we reached the end of the chain, we're done.
19   Matcher *N = MatcherPtr.get();
20   if (N == 0) return;
21   
22   // If we have a scope node, walk down both edges.
23   if (ScopeMatcher *Push = dyn_cast<ScopeMatcher>(N))
24     ContractNodes(Push->getCheckPtr());
25   
26   // If we found a movechild node with a node that comes in a 'foochild' form,
27   // transform it.
28   if (MoveChildMatcher *MC = dyn_cast<MoveChildMatcher>(N)) {
29     Matcher *New = 0;
30     if (RecordMatcher *RM = dyn_cast<RecordMatcher>(MC->getNext()))
31       New = new RecordChildMatcher(MC->getChildNo(), RM->getWhatFor());
32     
33     if (CheckTypeMatcher *CT= dyn_cast<CheckTypeMatcher>(MC->getNext()))
34       New = new CheckChildTypeMatcher(MC->getChildNo(), CT->getType());
35     
36     if (New) {
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);
43     }
44   }
45   
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);
51     }
52   
53   ContractNodes(N->getNextPtr());
54 }
55
56 static void FactorNodes(OwningPtr<Matcher> &MatcherPtr) {
57   // If we reached the end of the chain, we're done.
58   Matcher *N = MatcherPtr.get();
59   if (N == 0) return;
60   
61   // If this is not a push node, just scan for one.
62   if (!isa<ScopeMatcher>(N))
63     return FactorNodes(N->getNextPtr());
64   
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;
68   
69   Matcher *CurNode = N;
70   for (; ScopeMatcher *PMN = dyn_cast<ScopeMatcher>(CurNode);
71        CurNode = PMN->getNext())
72     OptionsToMatch.push_back(PMN->getCheck());
73   OptionsToMatch.push_back(CurNode);
74   
75   
76 }
77
78 Matcher *llvm::OptimizeMatcher(Matcher *TheMatcher) {
79   OwningPtr<Matcher> MatcherPtr(TheMatcher);
80   ContractNodes(MatcherPtr);
81   FactorNodes(MatcherPtr);
82   return MatcherPtr.take();
83 }