blob: 5aaa51f97cf66593ff614d019f26d2d0ddd75cb8 [file] [log] [blame]
Chris Lattner36e646c2010-02-24 07:06:50 +00001//===- 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"
Chris Lattnercd632fb2010-02-25 07:45:24 +000015#include "llvm/ADT/DenseMap.h"
16#include <vector>
Chris Lattner36e646c2010-02-24 07:06:50 +000017using namespace llvm;
18
Chris Lattner9a515172010-02-25 02:04:40 +000019static void ContractNodes(OwningPtr<Matcher> &MatcherPtr) {
Chris Lattner3ee1bc42010-02-24 07:31:45 +000020 // If we reached the end of the chain, we're done.
Chris Lattner9a515172010-02-25 02:04:40 +000021 Matcher *N = MatcherPtr.get();
Chris Lattner3ee1bc42010-02-24 07:31:45 +000022 if (N == 0) return;
23
Chris Lattner42363662010-02-25 19:00:39 +000024 // If we have a scope node, walk down all of the children.
25 if (ScopeMatcher *Scope = dyn_cast<ScopeMatcher>(N)) {
26 for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) {
27 OwningPtr<Matcher> Child(Scope->takeChild(i));
28 ContractNodes(Child);
29 Scope->resetChild(i, Child.take());
30 }
31 return;
32 }
Chris Lattner3ee1bc42010-02-24 07:31:45 +000033
Chris Lattner9feb1e32010-02-24 19:52:48 +000034 // If we found a movechild node with a node that comes in a 'foochild' form,
35 // transform it.
Chris Lattner9a515172010-02-25 02:04:40 +000036 if (MoveChildMatcher *MC = dyn_cast<MoveChildMatcher>(N)) {
37 Matcher *New = 0;
38 if (RecordMatcher *RM = dyn_cast<RecordMatcher>(MC->getNext()))
39 New = new RecordChildMatcher(MC->getChildNo(), RM->getWhatFor());
Chris Lattner3c664b32010-02-24 20:15:25 +000040
Chris Lattner9a515172010-02-25 02:04:40 +000041 if (CheckTypeMatcher *CT= dyn_cast<CheckTypeMatcher>(MC->getNext()))
42 New = new CheckChildTypeMatcher(MC->getChildNo(), CT->getType());
Chris Lattner3c664b32010-02-24 20:15:25 +000043
44 if (New) {
45 // Insert the new node.
Chris Lattnerac10e4f2010-02-25 01:56:48 +000046 New->setNext(MatcherPtr.take());
47 MatcherPtr.reset(New);
Chris Lattner3c664b32010-02-24 20:15:25 +000048 // Remove the old one.
49 MC->setNext(MC->getNext()->takeNext());
Chris Lattnerac10e4f2010-02-25 01:56:48 +000050 return ContractNodes(MatcherPtr);
Chris Lattner9feb1e32010-02-24 19:52:48 +000051 }
Chris Lattner3ee1bc42010-02-24 07:31:45 +000052 }
Chris Lattner9feb1e32010-02-24 19:52:48 +000053
Chris Lattner9a515172010-02-25 02:04:40 +000054 if (MoveChildMatcher *MC = dyn_cast<MoveChildMatcher>(N))
55 if (MoveParentMatcher *MP =
56 dyn_cast<MoveParentMatcher>(MC->getNext())) {
Chris Lattnerac10e4f2010-02-25 01:56:48 +000057 MatcherPtr.reset(MP->takeNext());
58 return ContractNodes(MatcherPtr);
Chris Lattner9feb1e32010-02-24 19:52:48 +000059 }
60
61 ContractNodes(N->getNextPtr());
Chris Lattner3ee1bc42010-02-24 07:31:45 +000062}
63
Chris Lattner9a515172010-02-25 02:04:40 +000064static void FactorNodes(OwningPtr<Matcher> &MatcherPtr) {
Chris Lattner0acf2ba2010-02-25 01:57:41 +000065 // If we reached the end of the chain, we're done.
Chris Lattner9a515172010-02-25 02:04:40 +000066 Matcher *N = MatcherPtr.get();
Chris Lattner0acf2ba2010-02-25 01:57:41 +000067 if (N == 0) return;
68
69 // If this is not a push node, just scan for one.
Chris Lattner42363662010-02-25 19:00:39 +000070 ScopeMatcher *Scope = dyn_cast<ScopeMatcher>(N);
71 if (Scope == 0)
Chris Lattner0acf2ba2010-02-25 01:57:41 +000072 return FactorNodes(N->getNextPtr());
73
Chris Lattner42363662010-02-25 19:00:39 +000074 // Okay, pull together the children of the scope node into a vector so we can
Chris Lattnercd632fb2010-02-25 07:45:24 +000075 // inspect it more easily. While we're at it, bucket them up by the hash
76 // code of their first predicate.
Chris Lattner9a515172010-02-25 02:04:40 +000077 SmallVector<Matcher*, 32> OptionsToMatch;
Chris Lattner0acf2ba2010-02-25 01:57:41 +000078
Chris Lattner42363662010-02-25 19:00:39 +000079 for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) {
Chris Lattnercd632fb2010-02-25 07:45:24 +000080 // Factor the subexpression.
Chris Lattner42363662010-02-25 19:00:39 +000081 OwningPtr<Matcher> Child(Scope->takeChild(i));
82 FactorNodes(Child);
83
Chris Lattner92a22462010-02-26 08:08:41 +000084 if (Matcher *N = Child.take())
Chris Lattner42363662010-02-25 19:00:39 +000085 OptionsToMatch.push_back(N);
Chris Lattnercd632fb2010-02-25 07:45:24 +000086 }
Chris Lattner0acf2ba2010-02-25 01:57:41 +000087
Chris Lattnercd632fb2010-02-25 07:45:24 +000088 SmallVector<Matcher*, 32> NewOptionsToMatch;
89
Chris Lattner92a22462010-02-26 08:08:41 +000090 // Loop over options to match, merging neighboring patterns with identical
91 // starting nodes into a shared matcher.
92 for (unsigned i = 0, e = OptionsToMatch.size(); i != e;) {
Chris Lattnercd632fb2010-02-25 07:45:24 +000093 // Find the set of matchers that start with this node.
Chris Lattner92a22462010-02-26 08:08:41 +000094 Matcher *Optn = OptionsToMatch[i++];
95
96 // See if the next option starts with the same matcher, if not, no sharing.
97 if (i == e || !OptionsToMatch[i]->isEqual(Optn)) {
98 // TODO: Skip over mutually exclusive patterns.
Chris Lattnercd632fb2010-02-25 07:45:24 +000099 NewOptionsToMatch.push_back(Optn);
100 continue;
101 }
102
Chris Lattner92a22462010-02-26 08:08:41 +0000103 // If the two neighbors *do* start with the same matcher, we can factor the
104 // matcher out of at least these two patterns. See what the maximal set we
105 // can merge together is.
106 SmallVector<Matcher*, 8> EqualMatchers;
107 EqualMatchers.push_back(Optn);
108 EqualMatchers.push_back(OptionsToMatch[i++]);
109
110 while (i != e && OptionsToMatch[i]->isEqual(Optn))
111 EqualMatchers.push_back(OptionsToMatch[i++]);
112
Chris Lattnercd632fb2010-02-25 07:45:24 +0000113 // Factor these checks by pulling the first node off each entry and
Chris Lattnerb0e68102010-02-26 07:36:37 +0000114 // discarding it. Take the first one off the first entry to reuse.
115 Matcher *Shared = Optn;
116 Optn = Optn->takeNext();
117 EqualMatchers[0] = Optn;
118
Chris Lattner92a22462010-02-26 08:08:41 +0000119 // Remove and delete the first node from the other matchers we're factoring.
120 for (unsigned i = 1, e = EqualMatchers.size(); i != e; ++i) {
121 Matcher *Tmp = EqualMatchers[i]->takeNext();
122 delete EqualMatchers[i];
123 EqualMatchers[i] = Tmp;
124 }
Chris Lattnercd632fb2010-02-25 07:45:24 +0000125
Chris Lattnerb0e68102010-02-26 07:36:37 +0000126 Shared->setNext(new ScopeMatcher(&EqualMatchers[0], EqualMatchers.size()));
127
128 // Recursively factor the newly created node.
129 FactorNodes(Shared->getNextPtr());
130
131 NewOptionsToMatch.push_back(Shared);
Chris Lattnercd632fb2010-02-25 07:45:24 +0000132 }
133
134 // Reassemble a new Scope node.
Chris Lattnerb0e68102010-02-26 07:36:37 +0000135 assert(!NewOptionsToMatch.empty() && "where'd all our children go?");
136 if (NewOptionsToMatch.size() == 1)
137 MatcherPtr.reset(NewOptionsToMatch[0]);
138 else {
139 Scope->setNumChildren(NewOptionsToMatch.size());
140 for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i)
141 Scope->resetChild(i, NewOptionsToMatch[i]);
142 }
Chris Lattner0acf2ba2010-02-25 01:57:41 +0000143}
144
Chris Lattner9a515172010-02-25 02:04:40 +0000145Matcher *llvm::OptimizeMatcher(Matcher *TheMatcher) {
146 OwningPtr<Matcher> MatcherPtr(TheMatcher);
Chris Lattner9feb1e32010-02-24 19:52:48 +0000147 ContractNodes(MatcherPtr);
Chris Lattner0acf2ba2010-02-25 01:57:41 +0000148 FactorNodes(MatcherPtr);
Chris Lattner3ee1bc42010-02-24 07:31:45 +0000149 return MatcherPtr.take();
Chris Lattner36e646c2010-02-24 07:06:50 +0000150}