blob: e3f8b8f8242d196905021021b0c17f8e291a9681 [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.
Ahmed Charles56440fd2014-03-06 05:51:42 +000025static void ContractNodes(std::unique_ptr<Matcher> &MatcherPtr,
Chris Lattner102a8a02010-02-28 20:49:53 +000026 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();
Craig Topper24064772014-04-15 07:20:03 +000029 if (!N) return;
Chris Lattnerab417562010-02-24 07:31:45 +000030
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) {
Ahmed Charles56440fd2014-03-06 05:51:42 +000034 std::unique_ptr<Matcher> Child(Scope->takeChild(i));
Chris Lattner102a8a02010-02-28 20:49:53 +000035 ContractNodes(Child, CGP);
Ahmed Charles96c9d952014-03-05 10:19:29 +000036 Scope->resetChild(i, Child.release());
Chris Lattnerf7fc2d82010-02-25 19:00:39 +000037 }
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)) {
Craig Topper24064772014-04-15 07:20:03 +000044 Matcher *New = nullptr;
Chris Lattner2c3f6492010-02-25 02:04:40 +000045 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());
Craig Topper7ca1d182014-02-05 05:44:28 +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
Craig Topper7ca1d182014-02-05 05:44:28 +000059 if (CheckIntegerMatcher *CS = dyn_cast<CheckIntegerMatcher>(MC->getNext()))
60 if (MC->getChildNo() < 5) // Only have CheckChildInteger0...4
61 New = new CheckChildIntegerMatcher(MC->getChildNo(), CS->getValue());
62
Chris Lattner0c95baa2010-02-24 20:15:25 +000063 if (New) {
64 // Insert the new node.
Ahmed Charles96c9d952014-03-05 10:19:29 +000065 New->setNext(MatcherPtr.release());
Chris Lattnerac55f9d2010-02-25 01:56:48 +000066 MatcherPtr.reset(New);
Chris Lattner0c95baa2010-02-24 20:15:25 +000067 // Remove the old one.
68 MC->setNext(MC->getNext()->takeNext());
Chris Lattner102a8a02010-02-28 20:49:53 +000069 return ContractNodes(MatcherPtr, CGP);
Chris Lattner6b792322010-02-24 19:52:48 +000070 }
Chris Lattnerab417562010-02-24 07:31:45 +000071 }
Chris Lattner6b792322010-02-24 19:52:48 +000072
Chris Lattnerc3f80e02010-02-28 02:31:26 +000073 // Zap movechild -> moveparent.
Chris Lattner2c3f6492010-02-25 02:04:40 +000074 if (MoveChildMatcher *MC = dyn_cast<MoveChildMatcher>(N))
75 if (MoveParentMatcher *MP =
76 dyn_cast<MoveParentMatcher>(MC->getNext())) {
Chris Lattnerac55f9d2010-02-25 01:56:48 +000077 MatcherPtr.reset(MP->takeNext());
Chris Lattner102a8a02010-02-28 20:49:53 +000078 return ContractNodes(MatcherPtr, CGP);
Chris Lattner6b792322010-02-24 19:52:48 +000079 }
Chris Lattner560169d2010-02-28 23:00:47 +000080
Chris Lattnerdb5b73a2010-03-01 02:33:14 +000081 // Turn EmitNode->MarkFlagResults->CompleteMatch into
82 // MarkFlagResults->EmitNode->CompleteMatch when we can to encourage
83 // MorphNodeTo formation. This is safe because MarkFlagResults never refers
84 // to the root of the pattern.
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +000085 if (isa<EmitNodeMatcher>(N) && isa<MarkGlueResultsMatcher>(N->getNext()) &&
Chris Lattnerdb5b73a2010-03-01 02:33:14 +000086 isa<CompleteMatchMatcher>(N->getNext()->getNext())) {
87 // Unlink the two nodes from the list.
Ahmed Charles96c9d952014-03-05 10:19:29 +000088 Matcher *EmitNode = MatcherPtr.release();
Chris Lattnerdb5b73a2010-03-01 02:33:14 +000089 Matcher *MFR = EmitNode->takeNext();
90 Matcher *Tail = MFR->takeNext();
91
92 // Relink them.
93 MatcherPtr.reset(MFR);
94 MFR->setNext(EmitNode);
95 EmitNode->setNext(Tail);
96 return ContractNodes(MatcherPtr, CGP);
97 }
Chris Lattner560169d2010-02-28 23:00:47 +000098
Chris Lattner9d67dca2010-02-28 20:55:18 +000099 // Turn EmitNode->CompleteMatch into MorphNodeTo if we can.
Chris Lattnerc3f80e02010-02-28 02:31:26 +0000100 if (EmitNodeMatcher *EN = dyn_cast<EmitNodeMatcher>(N))
Chris Lattnerabb1c792010-02-28 02:41:25 +0000101 if (CompleteMatchMatcher *CM =
102 dyn_cast<CompleteMatchMatcher>(EN->getNext())) {
Chris Lattner9d67dca2010-02-28 20:55:18 +0000103 // We can only use MorphNodeTo if the result values match up.
Chris Lattner102a8a02010-02-28 20:49:53 +0000104 unsigned RootResultFirst = EN->getFirstResultSlot();
105 bool ResultsMatch = true;
106 for (unsigned i = 0, e = CM->getNumResults(); i != e; ++i)
107 if (CM->getResult(i) != RootResultFirst+i)
108 ResultsMatch = false;
109
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000110 // If the selected node defines a subset of the glue/chain results, we
Chris Lattner9d67dca2010-02-28 20:55:18 +0000111 // can't use MorphNodeTo. For example, we can't use MorphNodeTo if the
Chris Lattner102a8a02010-02-28 20:49:53 +0000112 // matched pattern has a chain but the root node doesn't.
113 const PatternToMatch &Pattern = CM->getPattern();
114
115 if (!EN->hasChain() &&
116 Pattern.getSrcPattern()->NodeHasProperty(SDNPHasChain, CGP))
117 ResultsMatch = false;
118
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000119 // If the matched node has glue and the output root doesn't, we can't
Chris Lattner9d67dca2010-02-28 20:55:18 +0000120 // use MorphNodeTo.
Chris Lattner102a8a02010-02-28 20:49:53 +0000121 //
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000122 // NOTE: Strictly speaking, we don't have to check for glue here
Chris Lattner102a8a02010-02-28 20:49:53 +0000123 // because the code in the pattern generator doesn't handle it right. We
124 // do it anyway for thoroughness.
Chris Lattnera8382642010-02-28 21:53:42 +0000125 if (!EN->hasOutFlag() &&
Chris Lattner2a0a3b42010-12-23 18:28:41 +0000126 Pattern.getSrcPattern()->NodeHasProperty(SDNPOutGlue, CGP))
Chris Lattner102a8a02010-02-28 20:49:53 +0000127 ResultsMatch = false;
128
129
130 // If the root result node defines more results than the source root node
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000131 // *and* has a chain or glue input, then we can't match it because it
132 // would end up replacing the extra result with the chain/glue.
Chris Lattner102a8a02010-02-28 20:49:53 +0000133#if 0
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000134 if ((EN->hasGlue() || EN->hasChain()) &&
135 EN->getNumNonChainGlueVTs() > ... need to get no results reliably ...)
Chris Lattner102a8a02010-02-28 20:49:53 +0000136 ResultMatch = false;
137#endif
138
139 if (ResultsMatch) {
140 const SmallVectorImpl<MVT::SimpleValueType> &VTs = EN->getVTList();
141 const SmallVectorImpl<unsigned> &Operands = EN->getOperandList();
Chris Lattner9d67dca2010-02-28 20:55:18 +0000142 MatcherPtr.reset(new MorphNodeToMatcher(EN->getOpcodeName(),
Craig Toppera2c60192014-01-21 07:20:05 +0000143 VTs, Operands,
Chris Lattnera8382642010-02-28 21:53:42 +0000144 EN->hasChain(), EN->hasInFlag(),
145 EN->hasOutFlag(),
Chris Lattner9d67dca2010-02-28 20:55:18 +0000146 EN->hasMemRefs(),
147 EN->getNumFixedArityOperands(),
148 Pattern));
Chris Lattner102a8a02010-02-28 20:49:53 +0000149 return;
150 }
151
Chris Lattner1e634e32010-03-01 22:29:19 +0000152 // FIXME2: Kill off all the SelectionDAG::SelectNodeTo and getMachineNode
Chris Lattner9d67dca2010-02-28 20:55:18 +0000153 // variants.
Chris Lattnerc3f80e02010-02-28 02:31:26 +0000154 }
155
Chris Lattner102a8a02010-02-28 20:49:53 +0000156 ContractNodes(N->getNextPtr(), CGP);
Chris Lattner90b1b9d2010-03-01 02:15:34 +0000157
158
159 // If we have a CheckType/CheckChildType/Record node followed by a
160 // CheckOpcode, invert the two nodes. We prefer to do structural checks
161 // before type checks, as this opens opportunities for factoring on targets
162 // like X86 where many operations are valid on multiple types.
163 if ((isa<CheckTypeMatcher>(N) || isa<CheckChildTypeMatcher>(N) ||
164 isa<RecordMatcher>(N)) &&
Chris Lattner053a28a2010-03-01 07:17:40 +0000165 isa<CheckOpcodeMatcher>(N->getNext())) {
Chris Lattner90b1b9d2010-03-01 02:15:34 +0000166 // Unlink the two nodes from the list.
Ahmed Charles96c9d952014-03-05 10:19:29 +0000167 Matcher *CheckType = MatcherPtr.release();
Chris Lattner90b1b9d2010-03-01 02:15:34 +0000168 Matcher *CheckOpcode = CheckType->takeNext();
169 Matcher *Tail = CheckOpcode->takeNext();
170
171 // Relink them.
172 MatcherPtr.reset(CheckOpcode);
173 CheckOpcode->setNext(CheckType);
174 CheckType->setNext(Tail);
175 return ContractNodes(MatcherPtr, CGP);
176 }
Chris Lattnerab417562010-02-24 07:31:45 +0000177}
178
Chris Lattnerd9e1e832010-02-27 06:22:57 +0000179/// SinkPatternPredicates - Pattern predicates can be checked at any level of
180/// the matching tree. The generator dumps them at the top level of the pattern
181/// though, which prevents factoring from being able to see past them. This
182/// optimization sinks them as far down into the pattern as possible.
183///
184/// Conceptually, we'd like to sink these predicates all the way to the last
185/// matcher predicate in the series. However, it turns out that some
186/// ComplexPatterns have side effects on the graph, so we really don't want to
187/// run a the complex pattern if the pattern predicate will fail. For this
188/// reason, we refuse to sink the pattern predicate past a ComplexPattern.
189///
Ahmed Charles56440fd2014-03-06 05:51:42 +0000190static void SinkPatternPredicates(std::unique_ptr<Matcher> &MatcherPtr) {
Chris Lattnerd9e1e832010-02-27 06:22:57 +0000191 // Recursively scan for a PatternPredicate.
192 // If we reached the end of the chain, we're done.
193 Matcher *N = MatcherPtr.get();
Craig Topper24064772014-04-15 07:20:03 +0000194 if (!N) return;
Chris Lattnerd9e1e832010-02-27 06:22:57 +0000195
196 // Walk down all members of a scope node.
197 if (ScopeMatcher *Scope = dyn_cast<ScopeMatcher>(N)) {
198 for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000199 std::unique_ptr<Matcher> Child(Scope->takeChild(i));
Chris Lattnerd9e1e832010-02-27 06:22:57 +0000200 SinkPatternPredicates(Child);
Ahmed Charles96c9d952014-03-05 10:19:29 +0000201 Scope->resetChild(i, Child.release());
Chris Lattnerd9e1e832010-02-27 06:22:57 +0000202 }
203 return;
204 }
205
206 // If this node isn't a CheckPatternPredicateMatcher we keep scanning until
207 // we find one.
208 CheckPatternPredicateMatcher *CPPM =dyn_cast<CheckPatternPredicateMatcher>(N);
Craig Topper24064772014-04-15 07:20:03 +0000209 if (!CPPM)
Chris Lattnerd9e1e832010-02-27 06:22:57 +0000210 return SinkPatternPredicates(N->getNextPtr());
211
212 // Ok, we found one, lets try to sink it. Check if we can sink it past the
213 // next node in the chain. If not, we won't be able to change anything and
214 // might as well bail.
215 if (!CPPM->getNext()->isSafeToReorderWithPatternPredicate())
216 return;
217
218 // Okay, we know we can sink it past at least one node. Unlink it from the
219 // chain and scan for the new insertion point.
Ahmed Charles96c9d952014-03-05 10:19:29 +0000220 MatcherPtr.release(); // Don't delete CPPM.
Chris Lattnerd9e1e832010-02-27 06:22:57 +0000221 MatcherPtr.reset(CPPM->takeNext());
222
223 N = MatcherPtr.get();
224 while (N->getNext()->isSafeToReorderWithPatternPredicate())
225 N = N->getNext();
226
227 // At this point, we want to insert CPPM after N.
228 CPPM->setNext(N->takeNext());
229 N->setNext(CPPM);
230}
231
Chris Lattnera1603892010-03-07 07:20:49 +0000232/// FindNodeWithKind - Scan a series of matchers looking for a matcher with a
233/// specified kind. Return null if we didn't find one otherwise return the
234/// matcher.
235static Matcher *FindNodeWithKind(Matcher *M, Matcher::KindTy Kind) {
Chris Lattnerb9071a22010-03-07 07:01:28 +0000236 for (; M; M = M->getNext())
Chris Lattnera1603892010-03-07 07:20:49 +0000237 if (M->getKind() == Kind)
238 return M;
Craig Topper24064772014-04-15 07:20:03 +0000239 return nullptr;
Chris Lattnerb9071a22010-03-07 07:01:28 +0000240}
241
242
Chris Lattnerd9e1e832010-02-27 06:22:57 +0000243/// FactorNodes - Turn matches like this:
244/// Scope
245/// OPC_CheckType i32
246/// ABC
247/// OPC_CheckType i32
248/// XYZ
249/// into:
250/// OPC_CheckType i32
251/// Scope
252/// ABC
253/// XYZ
254///
Ahmed Charles56440fd2014-03-06 05:51:42 +0000255static void FactorNodes(std::unique_ptr<Matcher> &MatcherPtr) {
Chris Lattnerc36ab922010-02-25 01:57:41 +0000256 // If we reached the end of the chain, we're done.
Chris Lattner2c3f6492010-02-25 02:04:40 +0000257 Matcher *N = MatcherPtr.get();
Craig Topper24064772014-04-15 07:20:03 +0000258 if (!N) return;
Chris Lattnerc36ab922010-02-25 01:57:41 +0000259
260 // If this is not a push node, just scan for one.
Chris Lattnerf7fc2d82010-02-25 19:00:39 +0000261 ScopeMatcher *Scope = dyn_cast<ScopeMatcher>(N);
Craig Topper24064772014-04-15 07:20:03 +0000262 if (!Scope)
Chris Lattnerc36ab922010-02-25 01:57:41 +0000263 return FactorNodes(N->getNextPtr());
264
Chris Lattnerf7fc2d82010-02-25 19:00:39 +0000265 // Okay, pull together the children of the scope node into a vector so we can
Chris Lattner62702da02010-02-25 07:45:24 +0000266 // inspect it more easily. While we're at it, bucket them up by the hash
267 // code of their first predicate.
Chris Lattner2c3f6492010-02-25 02:04:40 +0000268 SmallVector<Matcher*, 32> OptionsToMatch;
Chris Lattnerc36ab922010-02-25 01:57:41 +0000269
Chris Lattnerf7fc2d82010-02-25 19:00:39 +0000270 for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) {
Chris Lattner62702da02010-02-25 07:45:24 +0000271 // Factor the subexpression.
Ahmed Charles56440fd2014-03-06 05:51:42 +0000272 std::unique_ptr<Matcher> Child(Scope->takeChild(i));
Chris Lattnerf7fc2d82010-02-25 19:00:39 +0000273 FactorNodes(Child);
274
Ahmed Charles96c9d952014-03-05 10:19:29 +0000275 if (Matcher *N = Child.release())
Chris Lattnerf7fc2d82010-02-25 19:00:39 +0000276 OptionsToMatch.push_back(N);
Chris Lattner62702da02010-02-25 07:45:24 +0000277 }
Chris Lattnerc36ab922010-02-25 01:57:41 +0000278
Chris Lattner62702da02010-02-25 07:45:24 +0000279 SmallVector<Matcher*, 32> NewOptionsToMatch;
Chris Lattnerf4d17752010-03-01 06:59:22 +0000280
Chris Lattner4f9a6712010-02-26 08:08:41 +0000281 // Loop over options to match, merging neighboring patterns with identical
282 // starting nodes into a shared matcher.
Chris Lattnerc577b812010-02-27 07:49:13 +0000283 for (unsigned OptionIdx = 0, e = OptionsToMatch.size(); OptionIdx != e;) {
Chris Lattner62702da02010-02-25 07:45:24 +0000284 // Find the set of matchers that start with this node.
Chris Lattnerc577b812010-02-27 07:49:13 +0000285 Matcher *Optn = OptionsToMatch[OptionIdx++];
286
287 if (OptionIdx == e) {
Chris Lattner62702da02010-02-25 07:45:24 +0000288 NewOptionsToMatch.push_back(Optn);
289 continue;
290 }
291
Chris Lattnerc577b812010-02-27 07:49:13 +0000292 // See if the next option starts with the same matcher. If the two
293 // neighbors *do* start with the same matcher, we can factor the matcher out
294 // of at least these two patterns. See what the maximal set we can merge
295 // together is.
Chris Lattner4f9a6712010-02-26 08:08:41 +0000296 SmallVector<Matcher*, 8> EqualMatchers;
297 EqualMatchers.push_back(Optn);
Chris Lattner4f9a6712010-02-26 08:08:41 +0000298
Chris Lattnerc577b812010-02-27 07:49:13 +0000299 // Factor all of the known-equal matchers after this one into the same
300 // group.
301 while (OptionIdx != e && OptionsToMatch[OptionIdx]->isEqual(Optn))
302 EqualMatchers.push_back(OptionsToMatch[OptionIdx++]);
303
304 // If we found a non-equal matcher, see if it is contradictory with the
305 // current node. If so, we know that the ordering relation between the
306 // current sets of nodes and this node don't matter. Look past it to see if
307 // we can merge anything else into this matching group.
308 unsigned Scan = OptionIdx;
309 while (1) {
Chris Lattnerb9071a22010-03-07 07:01:28 +0000310 // If we ran out of stuff to scan, we're done.
311 if (Scan == e) break;
Chris Lattnerc577b812010-02-27 07:49:13 +0000312
Chris Lattnera1603892010-03-07 07:20:49 +0000313 Matcher *ScanMatcher = OptionsToMatch[Scan];
314
Chris Lattnerb9071a22010-03-07 07:01:28 +0000315 // If we found an entry that matches out matcher, merge it into the set to
316 // handle.
Chris Lattnera1603892010-03-07 07:20:49 +0000317 if (Optn->isEqual(ScanMatcher)) {
Chris Lattnerb9071a22010-03-07 07:01:28 +0000318 // If is equal after all, add the option to EqualMatchers and remove it
319 // from OptionsToMatch.
Chris Lattnera1603892010-03-07 07:20:49 +0000320 EqualMatchers.push_back(ScanMatcher);
Chris Lattnerb9071a22010-03-07 07:01:28 +0000321 OptionsToMatch.erase(OptionsToMatch.begin()+Scan);
322 --e;
323 continue;
324 }
325
326 // If the option we're checking for contradicts the start of the list,
327 // skip over it.
Chris Lattnera1603892010-03-07 07:20:49 +0000328 if (Optn->isContradictory(ScanMatcher)) {
Chris Lattnerb9071a22010-03-07 07:01:28 +0000329 ++Scan;
330 continue;
331 }
Chris Lattnerc577b812010-02-27 07:49:13 +0000332
Chris Lattnera1603892010-03-07 07:20:49 +0000333 // If we're scanning for a simple node, see if it occurs later in the
334 // sequence. If so, and if we can move it up, it might be contradictory
335 // or the same as what we're looking for. If so, reorder it.
336 if (Optn->isSimplePredicateOrRecordNode()) {
337 Matcher *M2 = FindNodeWithKind(ScanMatcher, Optn->getKind());
Craig Topper24064772014-04-15 07:20:03 +0000338 if (M2 && M2 != ScanMatcher &&
Chris Lattnera1603892010-03-07 07:20:49 +0000339 M2->canMoveBefore(ScanMatcher) &&
340 (M2->isEqual(Optn) || M2->isContradictory(Optn))) {
341 Matcher *MatcherWithoutM2 = ScanMatcher->unlinkNode(M2);
342 M2->setNext(MatcherWithoutM2);
343 OptionsToMatch[Scan] = M2;
Chris Lattnerb9071a22010-03-07 07:01:28 +0000344 continue;
345 }
346 }
347
348 // Otherwise, we don't know how to handle this entry, we have to bail.
349 break;
Chris Lattnerc577b812010-02-27 07:49:13 +0000350 }
351
Chris Lattner278606b2010-02-27 21:48:43 +0000352 if (Scan != e &&
353 // Don't print it's obvious nothing extra could be merged anyway.
354 Scan+1 != e) {
Chris Lattnerc95d58d2010-03-07 07:21:24 +0000355 DEBUG(errs() << "Couldn't merge this:\n";
Chris Lattner2586c862010-02-27 08:11:15 +0000356 Optn->print(errs(), 4);
357 errs() << "into this:\n";
358 OptionsToMatch[Scan]->print(errs(), 4);
Chris Lattner21a7bf32010-02-27 08:13:23 +0000359 if (Scan+1 != e)
Chris Lattner2586c862010-02-27 08:11:15 +0000360 OptionsToMatch[Scan+1]->printOne(errs());
Chris Lattner21a7bf32010-02-27 08:13:23 +0000361 if (Scan+2 < e)
Chris Lattner2586c862010-02-27 08:11:15 +0000362 OptionsToMatch[Scan+2]->printOne(errs());
Chris Lattnerc95d58d2010-03-07 07:21:24 +0000363 errs() << "\n");
Chris Lattnerc577b812010-02-27 07:49:13 +0000364 }
365
366 // If we only found one option starting with this matcher, no factoring is
367 // possible.
368 if (EqualMatchers.size() == 1) {
369 NewOptionsToMatch.push_back(EqualMatchers[0]);
370 continue;
371 }
Chris Lattner4f9a6712010-02-26 08:08:41 +0000372
Chris Lattner62702da02010-02-25 07:45:24 +0000373 // Factor these checks by pulling the first node off each entry and
Chris Lattnerbe5b6342010-02-26 07:36:37 +0000374 // discarding it. Take the first one off the first entry to reuse.
375 Matcher *Shared = Optn;
376 Optn = Optn->takeNext();
377 EqualMatchers[0] = Optn;
378
Chris Lattner4f9a6712010-02-26 08:08:41 +0000379 // Remove and delete the first node from the other matchers we're factoring.
380 for (unsigned i = 1, e = EqualMatchers.size(); i != e; ++i) {
381 Matcher *Tmp = EqualMatchers[i]->takeNext();
382 delete EqualMatchers[i];
383 EqualMatchers[i] = Tmp;
384 }
Chris Lattner62702da02010-02-25 07:45:24 +0000385
Craig Toppera2c60192014-01-21 07:20:05 +0000386 Shared->setNext(new ScopeMatcher(EqualMatchers));
Chris Lattnerbe5b6342010-02-26 07:36:37 +0000387
388 // Recursively factor the newly created node.
389 FactorNodes(Shared->getNextPtr());
390
391 NewOptionsToMatch.push_back(Shared);
Chris Lattner62702da02010-02-25 07:45:24 +0000392 }
Chris Lattnerf4d17752010-03-01 06:59:22 +0000393
394 // If we're down to a single pattern to match, then we don't need this scope
395 // anymore.
396 if (NewOptionsToMatch.size() == 1) {
397 MatcherPtr.reset(NewOptionsToMatch[0]);
398 return;
399 }
400
Chris Lattner664ac982010-03-01 22:04:33 +0000401 if (NewOptionsToMatch.empty()) {
Craig Topper24064772014-04-15 07:20:03 +0000402 MatcherPtr.reset(nullptr);
Chris Lattner664ac982010-03-01 22:04:33 +0000403 return;
404 }
405
Chris Lattnerf4d17752010-03-01 06:59:22 +0000406 // If our factoring failed (didn't achieve anything) see if we can simplify in
407 // other ways.
408
409 // Check to see if all of the leading entries are now opcode checks. If so,
410 // we can convert this Scope to be a OpcodeSwitch instead.
Chris Lattner3e1ffd02010-03-03 06:28:15 +0000411 bool AllOpcodeChecks = true, AllTypeChecks = true;
Chris Lattnerf4d17752010-03-01 06:59:22 +0000412 for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i) {
Chris Lattnerb9071a22010-03-07 07:01:28 +0000413 // Check to see if this breaks a series of CheckOpcodeMatchers.
414 if (AllOpcodeChecks &&
415 !isa<CheckOpcodeMatcher>(NewOptionsToMatch[i])) {
Chris Lattnerf4d17752010-03-01 06:59:22 +0000416#if 0
Chris Lattnerb9071a22010-03-07 07:01:28 +0000417 if (i > 3) {
Chris Lattner3e1ffd02010-03-03 06:28:15 +0000418 errs() << "FAILING OPC #" << i << "\n";
419 NewOptionsToMatch[i]->dump();
420 }
Chris Lattnerf4d17752010-03-01 06:59:22 +0000421#endif
Chris Lattner3e1ffd02010-03-03 06:28:15 +0000422 AllOpcodeChecks = false;
423 }
424
Chris Lattnerb9071a22010-03-07 07:01:28 +0000425 // Check to see if this breaks a series of CheckTypeMatcher's.
426 if (AllTypeChecks) {
Chris Lattnera1603892010-03-07 07:20:49 +0000427 CheckTypeMatcher *CTM =
428 cast_or_null<CheckTypeMatcher>(FindNodeWithKind(NewOptionsToMatch[i],
429 Matcher::CheckType));
Craig Topper24064772014-04-15 07:20:03 +0000430 if (!CTM ||
Chris Lattnerb9071a22010-03-07 07:01:28 +0000431 // iPTR checks could alias any other case without us knowing, don't
432 // bother with them.
433 CTM->getType() == MVT::iPTR ||
Chris Lattner6c2d1782010-03-24 00:41:19 +0000434 // SwitchType only works for result #0.
435 CTM->getResNo() != 0 ||
Chris Lattnerb9071a22010-03-07 07:01:28 +0000436 // If the CheckType isn't at the start of the list, see if we can move
437 // it there.
438 !CTM->canMoveBefore(NewOptionsToMatch[i])) {
Chris Lattner3e1ffd02010-03-03 06:28:15 +0000439#if 0
Chris Lattnerb9071a22010-03-07 07:01:28 +0000440 if (i > 3 && AllTypeChecks) {
441 errs() << "FAILING TYPE #" << i << "\n";
442 NewOptionsToMatch[i]->dump();
443 }
Chris Lattner3e1ffd02010-03-03 06:28:15 +0000444#endif
Chris Lattnerb9071a22010-03-07 07:01:28 +0000445 AllTypeChecks = false;
446 }
Chris Lattner3e1ffd02010-03-03 06:28:15 +0000447 }
Chris Lattnerf4d17752010-03-01 06:59:22 +0000448 }
449
450 // If all the options are CheckOpcode's, we can form the SwitchOpcode, woot.
451 if (AllOpcodeChecks) {
452 StringSet<> Opcodes;
453 SmallVector<std::pair<const SDNodeInfo*, Matcher*>, 8> Cases;
454 for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i) {
Chris Lattner3e1ffd02010-03-03 06:28:15 +0000455 CheckOpcodeMatcher *COM = cast<CheckOpcodeMatcher>(NewOptionsToMatch[i]);
Chris Lattnerf4d17752010-03-01 06:59:22 +0000456 assert(Opcodes.insert(COM->getOpcode().getEnumName()) &&
457 "Duplicate opcodes not factored?");
458 Cases.push_back(std::make_pair(&COM->getOpcode(), COM->getNext()));
459 }
460
Craig Toppera2c60192014-01-21 07:20:05 +0000461 MatcherPtr.reset(new SwitchOpcodeMatcher(Cases));
Chris Lattnerf4d17752010-03-01 06:59:22 +0000462 return;
463 }
464
Chris Lattner3e1ffd02010-03-03 06:28:15 +0000465 // If all the options are CheckType's, we can form the SwitchType, woot.
466 if (AllTypeChecks) {
Chris Lattnerb9071a22010-03-07 07:01:28 +0000467 DenseMap<unsigned, unsigned> TypeEntry;
Chris Lattner3e1ffd02010-03-03 06:28:15 +0000468 SmallVector<std::pair<MVT::SimpleValueType, Matcher*>, 8> Cases;
469 for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i) {
Chris Lattnera1603892010-03-07 07:20:49 +0000470 CheckTypeMatcher *CTM =
471 cast_or_null<CheckTypeMatcher>(FindNodeWithKind(NewOptionsToMatch[i],
472 Matcher::CheckType));
Chris Lattnerb9071a22010-03-07 07:01:28 +0000473 Matcher *MatcherWithoutCTM = NewOptionsToMatch[i]->unlinkNode(CTM);
474 MVT::SimpleValueType CTMTy = CTM->getType();
475 delete CTM;
476
477 unsigned &Entry = TypeEntry[CTMTy];
478 if (Entry != 0) {
479 // If we have unfactored duplicate types, then we should factor them.
480 Matcher *PrevMatcher = Cases[Entry-1].second;
481 if (ScopeMatcher *SM = dyn_cast<ScopeMatcher>(PrevMatcher)) {
482 SM->setNumChildren(SM->getNumChildren()+1);
483 SM->resetChild(SM->getNumChildren()-1, MatcherWithoutCTM);
484 continue;
485 }
486
487 Matcher *Entries[2] = { PrevMatcher, MatcherWithoutCTM };
Craig Toppera2c60192014-01-21 07:20:05 +0000488 Cases[Entry-1].second = new ScopeMatcher(Entries);
Chris Lattnerb9071a22010-03-07 07:01:28 +0000489 continue;
490 }
491
492 Entry = Cases.size()+1;
493 Cases.push_back(std::make_pair(CTMTy, MatcherWithoutCTM));
Chris Lattner3e1ffd02010-03-03 06:28:15 +0000494 }
495
Chris Lattnerb9071a22010-03-07 07:01:28 +0000496 if (Cases.size() != 1) {
Craig Toppera2c60192014-01-21 07:20:05 +0000497 MatcherPtr.reset(new SwitchTypeMatcher(Cases));
Chris Lattnerb9071a22010-03-07 07:01:28 +0000498 } else {
499 // If we factored and ended up with one case, create it now.
Chris Lattner6c2d1782010-03-24 00:41:19 +0000500 MatcherPtr.reset(new CheckTypeMatcher(Cases[0].first, 0));
Chris Lattnerb9071a22010-03-07 07:01:28 +0000501 MatcherPtr->setNext(Cases[0].second);
502 }
Chris Lattner3e1ffd02010-03-03 06:28:15 +0000503 return;
504 }
505
Chris Lattner62702da02010-02-25 07:45:24 +0000506
Chris Lattner00f2e4b2010-03-01 22:19:47 +0000507 // Reassemble the Scope node with the adjusted children.
508 Scope->setNumChildren(NewOptionsToMatch.size());
509 for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i)
510 Scope->resetChild(i, NewOptionsToMatch[i]);
Chris Lattnerc36ab922010-02-25 01:57:41 +0000511}
512
Chris Lattner102a8a02010-02-28 20:49:53 +0000513Matcher *llvm::OptimizeMatcher(Matcher *TheMatcher,
514 const CodeGenDAGPatterns &CGP) {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000515 std::unique_ptr<Matcher> MatcherPtr(TheMatcher);
Chris Lattner102a8a02010-02-28 20:49:53 +0000516 ContractNodes(MatcherPtr, CGP);
Chris Lattnerd9e1e832010-02-27 06:22:57 +0000517 SinkPatternPredicates(MatcherPtr);
Chris Lattnerc36ab922010-02-25 01:57:41 +0000518 FactorNodes(MatcherPtr);
Ahmed Charles96c9d952014-03-05 10:19:29 +0000519 return MatcherPtr.release();
Chris Lattnere7327432010-02-24 07:06:50 +0000520}