blob: 12c4c1b079733c3ade0ca5ed08cf95cbe68e8492 [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
Chris Lattnerddfd5ab2010-02-27 07:49:13 +000014#define DEBUG_TYPE "isel-opt"
Chris Lattner36e646c2010-02-24 07:06:50 +000015#include "DAGISelMatcher.h"
Chris Lattnercd632fb2010-02-25 07:45:24 +000016#include "llvm/ADT/DenseMap.h"
Chris Lattnerddfd5ab2010-02-27 07:49:13 +000017#include "llvm/Support/Debug.h"
18#include "llvm/Support/raw_ostream.h"
Chris Lattnercd632fb2010-02-25 07:45:24 +000019#include <vector>
Chris Lattner36e646c2010-02-24 07:06:50 +000020using namespace llvm;
21
Chris Lattnerf4950d02010-02-27 06:22:57 +000022/// ContractNodes - Turn multiple matcher node patterns like 'MoveChild+Record'
23/// into single compound nodes like RecordChild.
Chris Lattner9a515172010-02-25 02:04:40 +000024static void ContractNodes(OwningPtr<Matcher> &MatcherPtr) {
Chris Lattner3ee1bc42010-02-24 07:31:45 +000025 // If we reached the end of the chain, we're done.
Chris Lattner9a515172010-02-25 02:04:40 +000026 Matcher *N = MatcherPtr.get();
Chris Lattner3ee1bc42010-02-24 07:31:45 +000027 if (N == 0) return;
28
Chris Lattner42363662010-02-25 19:00:39 +000029 // If we have a scope node, walk down all of the children.
30 if (ScopeMatcher *Scope = dyn_cast<ScopeMatcher>(N)) {
31 for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) {
32 OwningPtr<Matcher> Child(Scope->takeChild(i));
33 ContractNodes(Child);
34 Scope->resetChild(i, Child.take());
35 }
36 return;
37 }
Chris Lattner3ee1bc42010-02-24 07:31:45 +000038
Chris Lattner9feb1e32010-02-24 19:52:48 +000039 // If we found a movechild node with a node that comes in a 'foochild' form,
40 // transform it.
Chris Lattner9a515172010-02-25 02:04:40 +000041 if (MoveChildMatcher *MC = dyn_cast<MoveChildMatcher>(N)) {
42 Matcher *New = 0;
43 if (RecordMatcher *RM = dyn_cast<RecordMatcher>(MC->getNext()))
44 New = new RecordChildMatcher(MC->getChildNo(), RM->getWhatFor());
Chris Lattner3c664b32010-02-24 20:15:25 +000045
Chris Lattner9a515172010-02-25 02:04:40 +000046 if (CheckTypeMatcher *CT= dyn_cast<CheckTypeMatcher>(MC->getNext()))
47 New = new CheckChildTypeMatcher(MC->getChildNo(), CT->getType());
Chris Lattner3c664b32010-02-24 20:15:25 +000048
49 if (New) {
50 // Insert the new node.
Chris Lattnerac10e4f2010-02-25 01:56:48 +000051 New->setNext(MatcherPtr.take());
52 MatcherPtr.reset(New);
Chris Lattner3c664b32010-02-24 20:15:25 +000053 // Remove the old one.
54 MC->setNext(MC->getNext()->takeNext());
Chris Lattnerac10e4f2010-02-25 01:56:48 +000055 return ContractNodes(MatcherPtr);
Chris Lattner9feb1e32010-02-24 19:52:48 +000056 }
Chris Lattner3ee1bc42010-02-24 07:31:45 +000057 }
Chris Lattner9feb1e32010-02-24 19:52:48 +000058
Chris Lattner7a1dab42010-02-28 02:31:26 +000059 // Zap movechild -> moveparent.
Chris Lattner9a515172010-02-25 02:04:40 +000060 if (MoveChildMatcher *MC = dyn_cast<MoveChildMatcher>(N))
61 if (MoveParentMatcher *MP =
62 dyn_cast<MoveParentMatcher>(MC->getNext())) {
Chris Lattnerac10e4f2010-02-25 01:56:48 +000063 MatcherPtr.reset(MP->takeNext());
64 return ContractNodes(MatcherPtr);
Chris Lattner9feb1e32010-02-24 19:52:48 +000065 }
66
Chris Lattner7a1dab42010-02-28 02:31:26 +000067 // Turn EmitNode->CompleteMatch into SelectNodeTo if we can.
68 if (EmitNodeMatcher *EN = dyn_cast<EmitNodeMatcher>(N))
Chris Lattner19a1fbe2010-02-28 02:41:25 +000069 if (CompleteMatchMatcher *CM =
70 dyn_cast<CompleteMatchMatcher>(EN->getNext())) {
Chris Lattner7a1dab42010-02-28 02:31:26 +000071 (void)CM;
Chris Lattner7a1dab42010-02-28 02:31:26 +000072 }
73
Chris Lattner9feb1e32010-02-24 19:52:48 +000074 ContractNodes(N->getNextPtr());
Chris Lattner3ee1bc42010-02-24 07:31:45 +000075}
76
Chris Lattnerf4950d02010-02-27 06:22:57 +000077/// SinkPatternPredicates - Pattern predicates can be checked at any level of
78/// the matching tree. The generator dumps them at the top level of the pattern
79/// though, which prevents factoring from being able to see past them. This
80/// optimization sinks them as far down into the pattern as possible.
81///
82/// Conceptually, we'd like to sink these predicates all the way to the last
83/// matcher predicate in the series. However, it turns out that some
84/// ComplexPatterns have side effects on the graph, so we really don't want to
85/// run a the complex pattern if the pattern predicate will fail. For this
86/// reason, we refuse to sink the pattern predicate past a ComplexPattern.
87///
88static void SinkPatternPredicates(OwningPtr<Matcher> &MatcherPtr) {
89 // Recursively scan for a PatternPredicate.
90 // If we reached the end of the chain, we're done.
91 Matcher *N = MatcherPtr.get();
92 if (N == 0) return;
93
94 // Walk down all members of a scope node.
95 if (ScopeMatcher *Scope = dyn_cast<ScopeMatcher>(N)) {
96 for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) {
97 OwningPtr<Matcher> Child(Scope->takeChild(i));
98 SinkPatternPredicates(Child);
99 Scope->resetChild(i, Child.take());
100 }
101 return;
102 }
103
104 // If this node isn't a CheckPatternPredicateMatcher we keep scanning until
105 // we find one.
106 CheckPatternPredicateMatcher *CPPM =dyn_cast<CheckPatternPredicateMatcher>(N);
107 if (CPPM == 0)
108 return SinkPatternPredicates(N->getNextPtr());
109
110 // Ok, we found one, lets try to sink it. Check if we can sink it past the
111 // next node in the chain. If not, we won't be able to change anything and
112 // might as well bail.
113 if (!CPPM->getNext()->isSafeToReorderWithPatternPredicate())
114 return;
115
116 // Okay, we know we can sink it past at least one node. Unlink it from the
117 // chain and scan for the new insertion point.
118 MatcherPtr.take(); // Don't delete CPPM.
119 MatcherPtr.reset(CPPM->takeNext());
120
121 N = MatcherPtr.get();
122 while (N->getNext()->isSafeToReorderWithPatternPredicate())
123 N = N->getNext();
124
125 // At this point, we want to insert CPPM after N.
126 CPPM->setNext(N->takeNext());
127 N->setNext(CPPM);
128}
129
130/// FactorNodes - Turn matches like this:
131/// Scope
132/// OPC_CheckType i32
133/// ABC
134/// OPC_CheckType i32
135/// XYZ
136/// into:
137/// OPC_CheckType i32
138/// Scope
139/// ABC
140/// XYZ
141///
Chris Lattner9a515172010-02-25 02:04:40 +0000142static void FactorNodes(OwningPtr<Matcher> &MatcherPtr) {
Chris Lattner0acf2ba2010-02-25 01:57:41 +0000143 // If we reached the end of the chain, we're done.
Chris Lattner9a515172010-02-25 02:04:40 +0000144 Matcher *N = MatcherPtr.get();
Chris Lattner0acf2ba2010-02-25 01:57:41 +0000145 if (N == 0) return;
146
147 // If this is not a push node, just scan for one.
Chris Lattner42363662010-02-25 19:00:39 +0000148 ScopeMatcher *Scope = dyn_cast<ScopeMatcher>(N);
149 if (Scope == 0)
Chris Lattner0acf2ba2010-02-25 01:57:41 +0000150 return FactorNodes(N->getNextPtr());
151
Chris Lattner42363662010-02-25 19:00:39 +0000152 // Okay, pull together the children of the scope node into a vector so we can
Chris Lattnercd632fb2010-02-25 07:45:24 +0000153 // inspect it more easily. While we're at it, bucket them up by the hash
154 // code of their first predicate.
Chris Lattner9a515172010-02-25 02:04:40 +0000155 SmallVector<Matcher*, 32> OptionsToMatch;
Chris Lattner0acf2ba2010-02-25 01:57:41 +0000156
Chris Lattner42363662010-02-25 19:00:39 +0000157 for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) {
Chris Lattnercd632fb2010-02-25 07:45:24 +0000158 // Factor the subexpression.
Chris Lattner42363662010-02-25 19:00:39 +0000159 OwningPtr<Matcher> Child(Scope->takeChild(i));
160 FactorNodes(Child);
161
Chris Lattner92a22462010-02-26 08:08:41 +0000162 if (Matcher *N = Child.take())
Chris Lattner42363662010-02-25 19:00:39 +0000163 OptionsToMatch.push_back(N);
Chris Lattnercd632fb2010-02-25 07:45:24 +0000164 }
Chris Lattner0acf2ba2010-02-25 01:57:41 +0000165
Chris Lattnercd632fb2010-02-25 07:45:24 +0000166 SmallVector<Matcher*, 32> NewOptionsToMatch;
167
Chris Lattner92a22462010-02-26 08:08:41 +0000168 // Loop over options to match, merging neighboring patterns with identical
169 // starting nodes into a shared matcher.
Chris Lattnerddfd5ab2010-02-27 07:49:13 +0000170 for (unsigned OptionIdx = 0, e = OptionsToMatch.size(); OptionIdx != e;) {
Chris Lattnercd632fb2010-02-25 07:45:24 +0000171 // Find the set of matchers that start with this node.
Chris Lattnerddfd5ab2010-02-27 07:49:13 +0000172 Matcher *Optn = OptionsToMatch[OptionIdx++];
173
174 if (OptionIdx == e) {
Chris Lattnercd632fb2010-02-25 07:45:24 +0000175 NewOptionsToMatch.push_back(Optn);
176 continue;
177 }
178
Chris Lattnerddfd5ab2010-02-27 07:49:13 +0000179 // See if the next option starts with the same matcher. If the two
180 // neighbors *do* start with the same matcher, we can factor the matcher out
181 // of at least these two patterns. See what the maximal set we can merge
182 // together is.
Chris Lattner92a22462010-02-26 08:08:41 +0000183 SmallVector<Matcher*, 8> EqualMatchers;
184 EqualMatchers.push_back(Optn);
Chris Lattner92a22462010-02-26 08:08:41 +0000185
Chris Lattnerddfd5ab2010-02-27 07:49:13 +0000186 // Factor all of the known-equal matchers after this one into the same
187 // group.
188 while (OptionIdx != e && OptionsToMatch[OptionIdx]->isEqual(Optn))
189 EqualMatchers.push_back(OptionsToMatch[OptionIdx++]);
190
191 // If we found a non-equal matcher, see if it is contradictory with the
192 // current node. If so, we know that the ordering relation between the
193 // current sets of nodes and this node don't matter. Look past it to see if
194 // we can merge anything else into this matching group.
195 unsigned Scan = OptionIdx;
196 while (1) {
197 while (Scan != e && Optn->isContradictory(OptionsToMatch[Scan]))
198 ++Scan;
199
200 // Ok, we found something that isn't known to be contradictory. If it is
201 // equal, we can merge it into the set of nodes to factor, if not, we have
202 // to cease factoring.
203 if (Scan == e || !Optn->isEqual(OptionsToMatch[Scan])) break;
204
205 // If is equal after all, add the option to EqualMatchers and remove it
206 // from OptionsToMatch.
207 EqualMatchers.push_back(OptionsToMatch[Scan]);
208 OptionsToMatch.erase(OptionsToMatch.begin()+Scan);
209 --e;
210 }
211
Chris Lattnere9f720b2010-02-27 21:48:43 +0000212 if (Scan != e &&
213 // Don't print it's obvious nothing extra could be merged anyway.
214 Scan+1 != e) {
Chris Lattner086af322010-02-27 08:11:15 +0000215 DEBUG(errs() << "Couldn't merge this:\n";
216 Optn->print(errs(), 4);
217 errs() << "into this:\n";
218 OptionsToMatch[Scan]->print(errs(), 4);
Chris Lattner53d3ec92010-02-27 08:13:23 +0000219 if (Scan+1 != e)
Chris Lattner086af322010-02-27 08:11:15 +0000220 OptionsToMatch[Scan+1]->printOne(errs());
Chris Lattner53d3ec92010-02-27 08:13:23 +0000221 if (Scan+2 < e)
Chris Lattner086af322010-02-27 08:11:15 +0000222 OptionsToMatch[Scan+2]->printOne(errs());
Chris Lattnerddfd5ab2010-02-27 07:49:13 +0000223 errs() << "\n");
224 }
225
226 // If we only found one option starting with this matcher, no factoring is
227 // possible.
228 if (EqualMatchers.size() == 1) {
229 NewOptionsToMatch.push_back(EqualMatchers[0]);
230 continue;
231 }
Chris Lattner92a22462010-02-26 08:08:41 +0000232
Chris Lattnercd632fb2010-02-25 07:45:24 +0000233 // Factor these checks by pulling the first node off each entry and
Chris Lattnerb0e68102010-02-26 07:36:37 +0000234 // discarding it. Take the first one off the first entry to reuse.
235 Matcher *Shared = Optn;
236 Optn = Optn->takeNext();
237 EqualMatchers[0] = Optn;
238
Chris Lattner92a22462010-02-26 08:08:41 +0000239 // Remove and delete the first node from the other matchers we're factoring.
240 for (unsigned i = 1, e = EqualMatchers.size(); i != e; ++i) {
241 Matcher *Tmp = EqualMatchers[i]->takeNext();
242 delete EqualMatchers[i];
243 EqualMatchers[i] = Tmp;
244 }
Chris Lattnercd632fb2010-02-25 07:45:24 +0000245
Chris Lattnerb0e68102010-02-26 07:36:37 +0000246 Shared->setNext(new ScopeMatcher(&EqualMatchers[0], EqualMatchers.size()));
247
248 // Recursively factor the newly created node.
249 FactorNodes(Shared->getNextPtr());
250
251 NewOptionsToMatch.push_back(Shared);
Chris Lattnercd632fb2010-02-25 07:45:24 +0000252 }
253
254 // Reassemble a new Scope node.
Chris Lattnerb0e68102010-02-26 07:36:37 +0000255 assert(!NewOptionsToMatch.empty() && "where'd all our children go?");
256 if (NewOptionsToMatch.size() == 1)
257 MatcherPtr.reset(NewOptionsToMatch[0]);
258 else {
259 Scope->setNumChildren(NewOptionsToMatch.size());
260 for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i)
261 Scope->resetChild(i, NewOptionsToMatch[i]);
262 }
Chris Lattner0acf2ba2010-02-25 01:57:41 +0000263}
264
Chris Lattner9a515172010-02-25 02:04:40 +0000265Matcher *llvm::OptimizeMatcher(Matcher *TheMatcher) {
266 OwningPtr<Matcher> MatcherPtr(TheMatcher);
Chris Lattner9feb1e32010-02-24 19:52:48 +0000267 ContractNodes(MatcherPtr);
Chris Lattnerf4950d02010-02-27 06:22:57 +0000268 SinkPatternPredicates(MatcherPtr);
Chris Lattner0acf2ba2010-02-25 01:57:41 +0000269 FactorNodes(MatcherPtr);
Chris Lattner3ee1bc42010-02-24 07:31:45 +0000270 return MatcherPtr.take();
Chris Lattner36e646c2010-02-24 07:06:50 +0000271}