blob: 8fde8ce5bc0fcfd81154a43c90959d3fb0abde85 [file] [log] [blame]
Chris Lattnere7327432010-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 Lattnerc577b812010-02-27 07:49:13 +000014#define DEBUG_TYPE "isel-opt"
Chris Lattnere7327432010-02-24 07:06:50 +000015#include "DAGISelMatcher.h"
Chris Lattner102a8a02010-02-28 20:49:53 +000016#include "CodeGenDAGPatterns.h"
Chris Lattner3e1ffd02010-03-03 06:28:15 +000017#include "llvm/ADT/DenseSet.h"
Chris Lattnerf4d17752010-03-01 06:59:22 +000018#include "llvm/ADT/StringSet.h"
Chris Lattnerc577b812010-02-27 07:49:13 +000019#include "llvm/Support/Debug.h"
20#include "llvm/Support/raw_ostream.h"
Chris Lattnere7327432010-02-24 07:06:50 +000021using namespace llvm;
22
Chris Lattnerd9e1e832010-02-27 06:22:57 +000023/// ContractNodes - Turn multiple matcher node patterns like 'MoveChild+Record'
24/// into single compound nodes like RecordChild.
Chris Lattner102a8a02010-02-28 20:49:53 +000025static void ContractNodes(OwningPtr<Matcher> &MatcherPtr,
26 const CodeGenDAGPatterns &CGP) {
Chris Lattnerab417562010-02-24 07:31:45 +000027 // If we reached the end of the chain, we're done.
Chris Lattner2c3f6492010-02-25 02:04:40 +000028 Matcher *N = MatcherPtr.get();
Chris Lattnerab417562010-02-24 07:31:45 +000029 if (N == 0) return;
30
Chris Lattnerf7fc2d82010-02-25 19:00:39 +000031 // If we have a scope node, walk down all of the children.
32 if (ScopeMatcher *Scope = dyn_cast<ScopeMatcher>(N)) {
33 for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) {
34 OwningPtr<Matcher> Child(Scope->takeChild(i));
Chris Lattner102a8a02010-02-28 20:49:53 +000035 ContractNodes(Child, CGP);
Chris Lattnerf7fc2d82010-02-25 19:00:39 +000036 Scope->resetChild(i, Child.take());
37 }
38 return;
39 }
Chris Lattnerab417562010-02-24 07:31:45 +000040
Chris Lattner6b792322010-02-24 19:52:48 +000041 // If we found a movechild node with a node that comes in a 'foochild' form,
42 // transform it.
Chris Lattner2c3f6492010-02-25 02:04:40 +000043 if (MoveChildMatcher *MC = dyn_cast<MoveChildMatcher>(N)) {
44 Matcher *New = 0;
45 if (RecordMatcher *RM = dyn_cast<RecordMatcher>(MC->getNext()))
Chris Lattnerf57437a2010-03-16 00:35:11 +000046 if (MC->getChildNo() < 8) // Only have RecordChild0...7
47 New = new RecordChildMatcher(MC->getChildNo(), RM->getWhatFor(),
48 RM->getResultNo());
Chris Lattner0c95baa2010-02-24 20:15:25 +000049
Chris Lattner6c2d1782010-03-24 00:41:19 +000050 if (CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(MC->getNext()))
51 if (MC->getChildNo() < 8 && // Only have CheckChildType0...7
52 CT->getResNo() == 0) // CheckChildType checks res #0
Chris Lattnerf57437a2010-03-16 00:35:11 +000053 New = new CheckChildTypeMatcher(MC->getChildNo(), CT->getType());
Craig Toppera1bbc322013-10-05 05:38:16 +000054
55 if (CheckSameMatcher *CS = dyn_cast<CheckSameMatcher>(MC->getNext()))
56 if (MC->getChildNo() < 4) // Only have CheckChildSame0...3
57 New = new CheckChildSameMatcher(MC->getChildNo(), CS->getMatchNumber());
58
Chris Lattner0c95baa2010-02-24 20:15:25 +000059 if (New) {
60 // Insert the new node.
Chris Lattnerac55f9d2010-02-25 01:56:48 +000061 New->setNext(MatcherPtr.take());
62 MatcherPtr.reset(New);
Chris Lattner0c95baa2010-02-24 20:15:25 +000063 // Remove the old one.
64 MC->setNext(MC->getNext()->takeNext());
Chris Lattner102a8a02010-02-28 20:49:53 +000065 return ContractNodes(MatcherPtr, CGP);
Chris Lattner6b792322010-02-24 19:52:48 +000066 }
Chris Lattnerab417562010-02-24 07:31:45 +000067 }
Chris Lattner6b792322010-02-24 19:52:48 +000068
Chris Lattnerc3f80e02010-02-28 02:31:26 +000069 // Zap movechild -> moveparent.
Chris Lattner2c3f6492010-02-25 02:04:40 +000070 if (MoveChildMatcher *MC = dyn_cast<MoveChildMatcher>(N))
71 if (MoveParentMatcher *MP =
72 dyn_cast<MoveParentMatcher>(MC->getNext())) {
Chris Lattnerac55f9d2010-02-25 01:56:48 +000073 MatcherPtr.reset(MP->takeNext());
Chris Lattner102a8a02010-02-28 20:49:53 +000074 return ContractNodes(MatcherPtr, CGP);
Chris Lattner6b792322010-02-24 19:52:48 +000075 }
Chris Lattner560169d2010-02-28 23:00:47 +000076
Chris Lattnerdb5b73a2010-03-01 02:33:14 +000077 // Turn EmitNode->MarkFlagResults->CompleteMatch into
78 // MarkFlagResults->EmitNode->CompleteMatch when we can to encourage
79 // MorphNodeTo formation. This is safe because MarkFlagResults never refers
80 // to the root of the pattern.
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +000081 if (isa<EmitNodeMatcher>(N) && isa<MarkGlueResultsMatcher>(N->getNext()) &&
Chris Lattnerdb5b73a2010-03-01 02:33:14 +000082 isa<CompleteMatchMatcher>(N->getNext()->getNext())) {
83 // Unlink the two nodes from the list.
84 Matcher *EmitNode = MatcherPtr.take();
85 Matcher *MFR = EmitNode->takeNext();
86 Matcher *Tail = MFR->takeNext();
87
88 // Relink them.
89 MatcherPtr.reset(MFR);
90 MFR->setNext(EmitNode);
91 EmitNode->setNext(Tail);
92 return ContractNodes(MatcherPtr, CGP);
93 }
Chris Lattner560169d2010-02-28 23:00:47 +000094
Chris Lattner9d67dca2010-02-28 20:55:18 +000095 // Turn EmitNode->CompleteMatch into MorphNodeTo if we can.
Chris Lattnerc3f80e02010-02-28 02:31:26 +000096 if (EmitNodeMatcher *EN = dyn_cast<EmitNodeMatcher>(N))
Chris Lattnerabb1c792010-02-28 02:41:25 +000097 if (CompleteMatchMatcher *CM =
98 dyn_cast<CompleteMatchMatcher>(EN->getNext())) {
Chris Lattner9d67dca2010-02-28 20:55:18 +000099 // We can only use MorphNodeTo if the result values match up.
Chris Lattner102a8a02010-02-28 20:49:53 +0000100 unsigned RootResultFirst = EN->getFirstResultSlot();
101 bool ResultsMatch = true;
102 for (unsigned i = 0, e = CM->getNumResults(); i != e; ++i)
103 if (CM->getResult(i) != RootResultFirst+i)
104 ResultsMatch = false;
105
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000106 // If the selected node defines a subset of the glue/chain results, we
Chris Lattner9d67dca2010-02-28 20:55:18 +0000107 // can't use MorphNodeTo. For example, we can't use MorphNodeTo if the
Chris Lattner102a8a02010-02-28 20:49:53 +0000108 // matched pattern has a chain but the root node doesn't.
109 const PatternToMatch &Pattern = CM->getPattern();
110
111 if (!EN->hasChain() &&
112 Pattern.getSrcPattern()->NodeHasProperty(SDNPHasChain, CGP))
113 ResultsMatch = false;
114
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000115 // If the matched node has glue and the output root doesn't, we can't
Chris Lattner9d67dca2010-02-28 20:55:18 +0000116 // use MorphNodeTo.
Chris Lattner102a8a02010-02-28 20:49:53 +0000117 //
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000118 // NOTE: Strictly speaking, we don't have to check for glue here
Chris Lattner102a8a02010-02-28 20:49:53 +0000119 // because the code in the pattern generator doesn't handle it right. We
120 // do it anyway for thoroughness.
Chris Lattnera8382642010-02-28 21:53:42 +0000121 if (!EN->hasOutFlag() &&
Chris Lattner2a0a3b42010-12-23 18:28:41 +0000122 Pattern.getSrcPattern()->NodeHasProperty(SDNPOutGlue, CGP))
Chris Lattner102a8a02010-02-28 20:49:53 +0000123 ResultsMatch = false;
124
125
126 // If the root result node defines more results than the source root node
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000127 // *and* has a chain or glue input, then we can't match it because it
128 // would end up replacing the extra result with the chain/glue.
Chris Lattner102a8a02010-02-28 20:49:53 +0000129#if 0
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000130 if ((EN->hasGlue() || EN->hasChain()) &&
131 EN->getNumNonChainGlueVTs() > ... need to get no results reliably ...)
Chris Lattner102a8a02010-02-28 20:49:53 +0000132 ResultMatch = false;
133#endif
134
135 if (ResultsMatch) {
136 const SmallVectorImpl<MVT::SimpleValueType> &VTs = EN->getVTList();
137 const SmallVectorImpl<unsigned> &Operands = EN->getOperandList();
Chris Lattner9d67dca2010-02-28 20:55:18 +0000138 MatcherPtr.reset(new MorphNodeToMatcher(EN->getOpcodeName(),
Craig Toppera2c60192014-01-21 07:20:05 +0000139 VTs, Operands,
Chris Lattnera8382642010-02-28 21:53:42 +0000140 EN->hasChain(), EN->hasInFlag(),
141 EN->hasOutFlag(),
Chris Lattner9d67dca2010-02-28 20:55:18 +0000142 EN->hasMemRefs(),
143 EN->getNumFixedArityOperands(),
144 Pattern));
Chris Lattner102a8a02010-02-28 20:49:53 +0000145 return;
146 }
147
Chris Lattner1e634e32010-03-01 22:29:19 +0000148 // FIXME2: Kill off all the SelectionDAG::SelectNodeTo and getMachineNode
Chris Lattner9d67dca2010-02-28 20:55:18 +0000149 // variants.
Chris Lattnerc3f80e02010-02-28 02:31:26 +0000150 }
151
Chris Lattner102a8a02010-02-28 20:49:53 +0000152 ContractNodes(N->getNextPtr(), CGP);
Chris Lattner90b1b9d2010-03-01 02:15:34 +0000153
154
155 // If we have a CheckType/CheckChildType/Record node followed by a
156 // CheckOpcode, invert the two nodes. We prefer to do structural checks
157 // before type checks, as this opens opportunities for factoring on targets
158 // like X86 where many operations are valid on multiple types.
159 if ((isa<CheckTypeMatcher>(N) || isa<CheckChildTypeMatcher>(N) ||
160 isa<RecordMatcher>(N)) &&
Chris Lattner053a28a2010-03-01 07:17:40 +0000161 isa<CheckOpcodeMatcher>(N->getNext())) {
Chris Lattner90b1b9d2010-03-01 02:15:34 +0000162 // Unlink the two nodes from the list.
163 Matcher *CheckType = MatcherPtr.take();
164 Matcher *CheckOpcode = CheckType->takeNext();
165 Matcher *Tail = CheckOpcode->takeNext();
166
167 // Relink them.
168 MatcherPtr.reset(CheckOpcode);
169 CheckOpcode->setNext(CheckType);
170 CheckType->setNext(Tail);
171 return ContractNodes(MatcherPtr, CGP);
172 }
Chris Lattnerab417562010-02-24 07:31:45 +0000173}
174
Chris Lattnerd9e1e832010-02-27 06:22:57 +0000175/// SinkPatternPredicates - Pattern predicates can be checked at any level of
176/// the matching tree. The generator dumps them at the top level of the pattern
177/// though, which prevents factoring from being able to see past them. This
178/// optimization sinks them as far down into the pattern as possible.
179///
180/// Conceptually, we'd like to sink these predicates all the way to the last
181/// matcher predicate in the series. However, it turns out that some
182/// ComplexPatterns have side effects on the graph, so we really don't want to
183/// run a the complex pattern if the pattern predicate will fail. For this
184/// reason, we refuse to sink the pattern predicate past a ComplexPattern.
185///
186static void SinkPatternPredicates(OwningPtr<Matcher> &MatcherPtr) {
187 // Recursively scan for a PatternPredicate.
188 // If we reached the end of the chain, we're done.
189 Matcher *N = MatcherPtr.get();
190 if (N == 0) return;
191
192 // Walk down all members of a scope node.
193 if (ScopeMatcher *Scope = dyn_cast<ScopeMatcher>(N)) {
194 for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) {
195 OwningPtr<Matcher> Child(Scope->takeChild(i));
196 SinkPatternPredicates(Child);
197 Scope->resetChild(i, Child.take());
198 }
199 return;
200 }
201
202 // If this node isn't a CheckPatternPredicateMatcher we keep scanning until
203 // we find one.
204 CheckPatternPredicateMatcher *CPPM =dyn_cast<CheckPatternPredicateMatcher>(N);
205 if (CPPM == 0)
206 return SinkPatternPredicates(N->getNextPtr());
207
208 // Ok, we found one, lets try to sink it. Check if we can sink it past the
209 // next node in the chain. If not, we won't be able to change anything and
210 // might as well bail.
211 if (!CPPM->getNext()->isSafeToReorderWithPatternPredicate())
212 return;
213
214 // Okay, we know we can sink it past at least one node. Unlink it from the
215 // chain and scan for the new insertion point.
216 MatcherPtr.take(); // Don't delete CPPM.
217 MatcherPtr.reset(CPPM->takeNext());
218
219 N = MatcherPtr.get();
220 while (N->getNext()->isSafeToReorderWithPatternPredicate())
221 N = N->getNext();
222
223 // At this point, we want to insert CPPM after N.
224 CPPM->setNext(N->takeNext());
225 N->setNext(CPPM);
226}
227
Chris Lattnera1603892010-03-07 07:20:49 +0000228/// FindNodeWithKind - Scan a series of matchers looking for a matcher with a
229/// specified kind. Return null if we didn't find one otherwise return the
230/// matcher.
231static Matcher *FindNodeWithKind(Matcher *M, Matcher::KindTy Kind) {
Chris Lattnerb9071a22010-03-07 07:01:28 +0000232 for (; M; M = M->getNext())
Chris Lattnera1603892010-03-07 07:20:49 +0000233 if (M->getKind() == Kind)
234 return M;
Chris Lattnerb9071a22010-03-07 07:01:28 +0000235 return 0;
236}
237
238
Chris Lattnerd9e1e832010-02-27 06:22:57 +0000239/// FactorNodes - Turn matches like this:
240/// Scope
241/// OPC_CheckType i32
242/// ABC
243/// OPC_CheckType i32
244/// XYZ
245/// into:
246/// OPC_CheckType i32
247/// Scope
248/// ABC
249/// XYZ
250///
Chris Lattner2c3f6492010-02-25 02:04:40 +0000251static void FactorNodes(OwningPtr<Matcher> &MatcherPtr) {
Chris Lattnerc36ab922010-02-25 01:57:41 +0000252 // If we reached the end of the chain, we're done.
Chris Lattner2c3f6492010-02-25 02:04:40 +0000253 Matcher *N = MatcherPtr.get();
Chris Lattnerc36ab922010-02-25 01:57:41 +0000254 if (N == 0) return;
255
256 // If this is not a push node, just scan for one.
Chris Lattnerf7fc2d82010-02-25 19:00:39 +0000257 ScopeMatcher *Scope = dyn_cast<ScopeMatcher>(N);
258 if (Scope == 0)
Chris Lattnerc36ab922010-02-25 01:57:41 +0000259 return FactorNodes(N->getNextPtr());
260
Chris Lattnerf7fc2d82010-02-25 19:00:39 +0000261 // Okay, pull together the children of the scope node into a vector so we can
Chris Lattner62702da02010-02-25 07:45:24 +0000262 // inspect it more easily. While we're at it, bucket them up by the hash
263 // code of their first predicate.
Chris Lattner2c3f6492010-02-25 02:04:40 +0000264 SmallVector<Matcher*, 32> OptionsToMatch;
Chris Lattnerc36ab922010-02-25 01:57:41 +0000265
Chris Lattnerf7fc2d82010-02-25 19:00:39 +0000266 for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) {
Chris Lattner62702da02010-02-25 07:45:24 +0000267 // Factor the subexpression.
Chris Lattnerf7fc2d82010-02-25 19:00:39 +0000268 OwningPtr<Matcher> Child(Scope->takeChild(i));
269 FactorNodes(Child);
270
Chris Lattner4f9a6712010-02-26 08:08:41 +0000271 if (Matcher *N = Child.take())
Chris Lattnerf7fc2d82010-02-25 19:00:39 +0000272 OptionsToMatch.push_back(N);
Chris Lattner62702da02010-02-25 07:45:24 +0000273 }
Chris Lattnerc36ab922010-02-25 01:57:41 +0000274
Chris Lattner62702da02010-02-25 07:45:24 +0000275 SmallVector<Matcher*, 32> NewOptionsToMatch;
Chris Lattnerf4d17752010-03-01 06:59:22 +0000276
Chris Lattner4f9a6712010-02-26 08:08:41 +0000277 // Loop over options to match, merging neighboring patterns with identical
278 // starting nodes into a shared matcher.
Chris Lattnerc577b812010-02-27 07:49:13 +0000279 for (unsigned OptionIdx = 0, e = OptionsToMatch.size(); OptionIdx != e;) {
Chris Lattner62702da02010-02-25 07:45:24 +0000280 // Find the set of matchers that start with this node.
Chris Lattnerc577b812010-02-27 07:49:13 +0000281 Matcher *Optn = OptionsToMatch[OptionIdx++];
282
283 if (OptionIdx == e) {
Chris Lattner62702da02010-02-25 07:45:24 +0000284 NewOptionsToMatch.push_back(Optn);
285 continue;
286 }
287
Chris Lattnerc577b812010-02-27 07:49:13 +0000288 // See if the next option starts with the same matcher. If the two
289 // neighbors *do* start with the same matcher, we can factor the matcher out
290 // of at least these two patterns. See what the maximal set we can merge
291 // together is.
Chris Lattner4f9a6712010-02-26 08:08:41 +0000292 SmallVector<Matcher*, 8> EqualMatchers;
293 EqualMatchers.push_back(Optn);
Chris Lattner4f9a6712010-02-26 08:08:41 +0000294
Chris Lattnerc577b812010-02-27 07:49:13 +0000295 // Factor all of the known-equal matchers after this one into the same
296 // group.
297 while (OptionIdx != e && OptionsToMatch[OptionIdx]->isEqual(Optn))
298 EqualMatchers.push_back(OptionsToMatch[OptionIdx++]);
299
300 // If we found a non-equal matcher, see if it is contradictory with the
301 // current node. If so, we know that the ordering relation between the
302 // current sets of nodes and this node don't matter. Look past it to see if
303 // we can merge anything else into this matching group.
304 unsigned Scan = OptionIdx;
305 while (1) {
Chris Lattnerb9071a22010-03-07 07:01:28 +0000306 // If we ran out of stuff to scan, we're done.
307 if (Scan == e) break;
Chris Lattnerc577b812010-02-27 07:49:13 +0000308
Chris Lattnera1603892010-03-07 07:20:49 +0000309 Matcher *ScanMatcher = OptionsToMatch[Scan];
310
Chris Lattnerb9071a22010-03-07 07:01:28 +0000311 // If we found an entry that matches out matcher, merge it into the set to
312 // handle.
Chris Lattnera1603892010-03-07 07:20:49 +0000313 if (Optn->isEqual(ScanMatcher)) {
Chris Lattnerb9071a22010-03-07 07:01:28 +0000314 // If is equal after all, add the option to EqualMatchers and remove it
315 // from OptionsToMatch.
Chris Lattnera1603892010-03-07 07:20:49 +0000316 EqualMatchers.push_back(ScanMatcher);
Chris Lattnerb9071a22010-03-07 07:01:28 +0000317 OptionsToMatch.erase(OptionsToMatch.begin()+Scan);
318 --e;
319 continue;
320 }
321
322 // If the option we're checking for contradicts the start of the list,
323 // skip over it.
Chris Lattnera1603892010-03-07 07:20:49 +0000324 if (Optn->isContradictory(ScanMatcher)) {
Chris Lattnerb9071a22010-03-07 07:01:28 +0000325 ++Scan;
326 continue;
327 }
Chris Lattnerc577b812010-02-27 07:49:13 +0000328
Chris Lattnera1603892010-03-07 07:20:49 +0000329 // If we're scanning for a simple node, see if it occurs later in the
330 // sequence. If so, and if we can move it up, it might be contradictory
331 // or the same as what we're looking for. If so, reorder it.
332 if (Optn->isSimplePredicateOrRecordNode()) {
333 Matcher *M2 = FindNodeWithKind(ScanMatcher, Optn->getKind());
334 if (M2 != 0 && M2 != ScanMatcher &&
335 M2->canMoveBefore(ScanMatcher) &&
336 (M2->isEqual(Optn) || M2->isContradictory(Optn))) {
337 Matcher *MatcherWithoutM2 = ScanMatcher->unlinkNode(M2);
338 M2->setNext(MatcherWithoutM2);
339 OptionsToMatch[Scan] = M2;
Chris Lattnerb9071a22010-03-07 07:01:28 +0000340 continue;
341 }
342 }
343
344 // Otherwise, we don't know how to handle this entry, we have to bail.
345 break;
Chris Lattnerc577b812010-02-27 07:49:13 +0000346 }
347
Chris Lattner278606b2010-02-27 21:48:43 +0000348 if (Scan != e &&
349 // Don't print it's obvious nothing extra could be merged anyway.
350 Scan+1 != e) {
Chris Lattnerc95d58d2010-03-07 07:21:24 +0000351 DEBUG(errs() << "Couldn't merge this:\n";
Chris Lattner2586c862010-02-27 08:11:15 +0000352 Optn->print(errs(), 4);
353 errs() << "into this:\n";
354 OptionsToMatch[Scan]->print(errs(), 4);
Chris Lattner21a7bf32010-02-27 08:13:23 +0000355 if (Scan+1 != e)
Chris Lattner2586c862010-02-27 08:11:15 +0000356 OptionsToMatch[Scan+1]->printOne(errs());
Chris Lattner21a7bf32010-02-27 08:13:23 +0000357 if (Scan+2 < e)
Chris Lattner2586c862010-02-27 08:11:15 +0000358 OptionsToMatch[Scan+2]->printOne(errs());
Chris Lattnerc95d58d2010-03-07 07:21:24 +0000359 errs() << "\n");
Chris Lattnerc577b812010-02-27 07:49:13 +0000360 }
361
362 // If we only found one option starting with this matcher, no factoring is
363 // possible.
364 if (EqualMatchers.size() == 1) {
365 NewOptionsToMatch.push_back(EqualMatchers[0]);
366 continue;
367 }
Chris Lattner4f9a6712010-02-26 08:08:41 +0000368
Chris Lattner62702da02010-02-25 07:45:24 +0000369 // Factor these checks by pulling the first node off each entry and
Chris Lattnerbe5b6342010-02-26 07:36:37 +0000370 // discarding it. Take the first one off the first entry to reuse.
371 Matcher *Shared = Optn;
372 Optn = Optn->takeNext();
373 EqualMatchers[0] = Optn;
374
Chris Lattner4f9a6712010-02-26 08:08:41 +0000375 // Remove and delete the first node from the other matchers we're factoring.
376 for (unsigned i = 1, e = EqualMatchers.size(); i != e; ++i) {
377 Matcher *Tmp = EqualMatchers[i]->takeNext();
378 delete EqualMatchers[i];
379 EqualMatchers[i] = Tmp;
380 }
Chris Lattner62702da02010-02-25 07:45:24 +0000381
Craig Toppera2c60192014-01-21 07:20:05 +0000382 Shared->setNext(new ScopeMatcher(EqualMatchers));
Chris Lattnerbe5b6342010-02-26 07:36:37 +0000383
384 // Recursively factor the newly created node.
385 FactorNodes(Shared->getNextPtr());
386
387 NewOptionsToMatch.push_back(Shared);
Chris Lattner62702da02010-02-25 07:45:24 +0000388 }
Chris Lattnerf4d17752010-03-01 06:59:22 +0000389
390 // If we're down to a single pattern to match, then we don't need this scope
391 // anymore.
392 if (NewOptionsToMatch.size() == 1) {
393 MatcherPtr.reset(NewOptionsToMatch[0]);
394 return;
395 }
396
Chris Lattner664ac982010-03-01 22:04:33 +0000397 if (NewOptionsToMatch.empty()) {
398 MatcherPtr.reset(0);
399 return;
400 }
401
Chris Lattnerf4d17752010-03-01 06:59:22 +0000402 // If our factoring failed (didn't achieve anything) see if we can simplify in
403 // other ways.
404
405 // Check to see if all of the leading entries are now opcode checks. If so,
406 // we can convert this Scope to be a OpcodeSwitch instead.
Chris Lattner3e1ffd02010-03-03 06:28:15 +0000407 bool AllOpcodeChecks = true, AllTypeChecks = true;
Chris Lattnerf4d17752010-03-01 06:59:22 +0000408 for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i) {
Chris Lattnerb9071a22010-03-07 07:01:28 +0000409 // Check to see if this breaks a series of CheckOpcodeMatchers.
410 if (AllOpcodeChecks &&
411 !isa<CheckOpcodeMatcher>(NewOptionsToMatch[i])) {
Chris Lattnerf4d17752010-03-01 06:59:22 +0000412#if 0
Chris Lattnerb9071a22010-03-07 07:01:28 +0000413 if (i > 3) {
Chris Lattner3e1ffd02010-03-03 06:28:15 +0000414 errs() << "FAILING OPC #" << i << "\n";
415 NewOptionsToMatch[i]->dump();
416 }
Chris Lattnerf4d17752010-03-01 06:59:22 +0000417#endif
Chris Lattner3e1ffd02010-03-03 06:28:15 +0000418 AllOpcodeChecks = false;
419 }
420
Chris Lattnerb9071a22010-03-07 07:01:28 +0000421 // Check to see if this breaks a series of CheckTypeMatcher's.
422 if (AllTypeChecks) {
Chris Lattnera1603892010-03-07 07:20:49 +0000423 CheckTypeMatcher *CTM =
424 cast_or_null<CheckTypeMatcher>(FindNodeWithKind(NewOptionsToMatch[i],
425 Matcher::CheckType));
Chris Lattner6c2d1782010-03-24 00:41:19 +0000426 if (CTM == 0 ||
Chris Lattnerb9071a22010-03-07 07:01:28 +0000427 // iPTR checks could alias any other case without us knowing, don't
428 // bother with them.
429 CTM->getType() == MVT::iPTR ||
Chris Lattner6c2d1782010-03-24 00:41:19 +0000430 // SwitchType only works for result #0.
431 CTM->getResNo() != 0 ||
Chris Lattnerb9071a22010-03-07 07:01:28 +0000432 // If the CheckType isn't at the start of the list, see if we can move
433 // it there.
434 !CTM->canMoveBefore(NewOptionsToMatch[i])) {
Chris Lattner3e1ffd02010-03-03 06:28:15 +0000435#if 0
Chris Lattnerb9071a22010-03-07 07:01:28 +0000436 if (i > 3 && AllTypeChecks) {
437 errs() << "FAILING TYPE #" << i << "\n";
438 NewOptionsToMatch[i]->dump();
439 }
Chris Lattner3e1ffd02010-03-03 06:28:15 +0000440#endif
Chris Lattnerb9071a22010-03-07 07:01:28 +0000441 AllTypeChecks = false;
442 }
Chris Lattner3e1ffd02010-03-03 06:28:15 +0000443 }
Chris Lattnerf4d17752010-03-01 06:59:22 +0000444 }
445
446 // If all the options are CheckOpcode's, we can form the SwitchOpcode, woot.
447 if (AllOpcodeChecks) {
448 StringSet<> Opcodes;
449 SmallVector<std::pair<const SDNodeInfo*, Matcher*>, 8> Cases;
450 for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i) {
Chris Lattner3e1ffd02010-03-03 06:28:15 +0000451 CheckOpcodeMatcher *COM = cast<CheckOpcodeMatcher>(NewOptionsToMatch[i]);
Chris Lattnerf4d17752010-03-01 06:59:22 +0000452 assert(Opcodes.insert(COM->getOpcode().getEnumName()) &&
453 "Duplicate opcodes not factored?");
454 Cases.push_back(std::make_pair(&COM->getOpcode(), COM->getNext()));
455 }
456
Craig Toppera2c60192014-01-21 07:20:05 +0000457 MatcherPtr.reset(new SwitchOpcodeMatcher(Cases));
Chris Lattnerf4d17752010-03-01 06:59:22 +0000458 return;
459 }
460
Chris Lattner3e1ffd02010-03-03 06:28:15 +0000461 // If all the options are CheckType's, we can form the SwitchType, woot.
462 if (AllTypeChecks) {
Chris Lattnerb9071a22010-03-07 07:01:28 +0000463 DenseMap<unsigned, unsigned> TypeEntry;
Chris Lattner3e1ffd02010-03-03 06:28:15 +0000464 SmallVector<std::pair<MVT::SimpleValueType, Matcher*>, 8> Cases;
465 for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i) {
Chris Lattnera1603892010-03-07 07:20:49 +0000466 CheckTypeMatcher *CTM =
467 cast_or_null<CheckTypeMatcher>(FindNodeWithKind(NewOptionsToMatch[i],
468 Matcher::CheckType));
Chris Lattnerb9071a22010-03-07 07:01:28 +0000469 Matcher *MatcherWithoutCTM = NewOptionsToMatch[i]->unlinkNode(CTM);
470 MVT::SimpleValueType CTMTy = CTM->getType();
471 delete CTM;
472
473 unsigned &Entry = TypeEntry[CTMTy];
474 if (Entry != 0) {
475 // If we have unfactored duplicate types, then we should factor them.
476 Matcher *PrevMatcher = Cases[Entry-1].second;
477 if (ScopeMatcher *SM = dyn_cast<ScopeMatcher>(PrevMatcher)) {
478 SM->setNumChildren(SM->getNumChildren()+1);
479 SM->resetChild(SM->getNumChildren()-1, MatcherWithoutCTM);
480 continue;
481 }
482
483 Matcher *Entries[2] = { PrevMatcher, MatcherWithoutCTM };
Craig Toppera2c60192014-01-21 07:20:05 +0000484 Cases[Entry-1].second = new ScopeMatcher(Entries);
Chris Lattnerb9071a22010-03-07 07:01:28 +0000485 continue;
486 }
487
488 Entry = Cases.size()+1;
489 Cases.push_back(std::make_pair(CTMTy, MatcherWithoutCTM));
Chris Lattner3e1ffd02010-03-03 06:28:15 +0000490 }
491
Chris Lattnerb9071a22010-03-07 07:01:28 +0000492 if (Cases.size() != 1) {
Craig Toppera2c60192014-01-21 07:20:05 +0000493 MatcherPtr.reset(new SwitchTypeMatcher(Cases));
Chris Lattnerb9071a22010-03-07 07:01:28 +0000494 } else {
495 // If we factored and ended up with one case, create it now.
Chris Lattner6c2d1782010-03-24 00:41:19 +0000496 MatcherPtr.reset(new CheckTypeMatcher(Cases[0].first, 0));
Chris Lattnerb9071a22010-03-07 07:01:28 +0000497 MatcherPtr->setNext(Cases[0].second);
498 }
Chris Lattner3e1ffd02010-03-03 06:28:15 +0000499 return;
500 }
501
Chris Lattner62702da02010-02-25 07:45:24 +0000502
Chris Lattner00f2e4b2010-03-01 22:19:47 +0000503 // Reassemble the Scope node with the adjusted children.
504 Scope->setNumChildren(NewOptionsToMatch.size());
505 for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i)
506 Scope->resetChild(i, NewOptionsToMatch[i]);
Chris Lattnerc36ab922010-02-25 01:57:41 +0000507}
508
Chris Lattner102a8a02010-02-28 20:49:53 +0000509Matcher *llvm::OptimizeMatcher(Matcher *TheMatcher,
510 const CodeGenDAGPatterns &CGP) {
Chris Lattner2c3f6492010-02-25 02:04:40 +0000511 OwningPtr<Matcher> MatcherPtr(TheMatcher);
Chris Lattner102a8a02010-02-28 20:49:53 +0000512 ContractNodes(MatcherPtr, CGP);
Chris Lattnerd9e1e832010-02-27 06:22:57 +0000513 SinkPatternPredicates(MatcherPtr);
Chris Lattnerc36ab922010-02-25 01:57:41 +0000514 FactorNodes(MatcherPtr);
Chris Lattnerab417562010-02-24 07:31:45 +0000515 return MatcherPtr.take();
Chris Lattnere7327432010-02-24 07:06:50 +0000516}