blob: 0bb656826fbdf9f69fcda5ac3a6dc3d55103f42b [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
14#include "DAGISelMatcher.h"
Chris Lattner102a8a02010-02-28 20:49:53 +000015#include "CodeGenDAGPatterns.h"
Chris Lattnerf4d17752010-03-01 06:59:22 +000016#include "llvm/ADT/StringSet.h"
Chris Lattnerc577b812010-02-27 07:49:13 +000017#include "llvm/Support/Debug.h"
18#include "llvm/Support/raw_ostream.h"
Chris Lattnere7327432010-02-24 07:06:50 +000019using namespace llvm;
20
Chandler Carruth97acce22014-04-22 03:06:00 +000021#define DEBUG_TYPE "isel-opt"
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 Lattner9d67dca2010-02-28 20:55:18 +000081 // Turn EmitNode->CompleteMatch into MorphNodeTo if we can.
Chris Lattnerc3f80e02010-02-28 02:31:26 +000082 if (EmitNodeMatcher *EN = dyn_cast<EmitNodeMatcher>(N))
Chris Lattnerabb1c792010-02-28 02:41:25 +000083 if (CompleteMatchMatcher *CM =
84 dyn_cast<CompleteMatchMatcher>(EN->getNext())) {
Chris Lattner9d67dca2010-02-28 20:55:18 +000085 // We can only use MorphNodeTo if the result values match up.
Chris Lattner102a8a02010-02-28 20:49:53 +000086 unsigned RootResultFirst = EN->getFirstResultSlot();
87 bool ResultsMatch = true;
88 for (unsigned i = 0, e = CM->getNumResults(); i != e; ++i)
89 if (CM->getResult(i) != RootResultFirst+i)
90 ResultsMatch = false;
91
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +000092 // If the selected node defines a subset of the glue/chain results, we
Chris Lattner9d67dca2010-02-28 20:55:18 +000093 // can't use MorphNodeTo. For example, we can't use MorphNodeTo if the
Chris Lattner102a8a02010-02-28 20:49:53 +000094 // matched pattern has a chain but the root node doesn't.
95 const PatternToMatch &Pattern = CM->getPattern();
96
97 if (!EN->hasChain() &&
98 Pattern.getSrcPattern()->NodeHasProperty(SDNPHasChain, CGP))
99 ResultsMatch = false;
100
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000101 // If the matched node has glue and the output root doesn't, we can't
Chris Lattner9d67dca2010-02-28 20:55:18 +0000102 // use MorphNodeTo.
Chris Lattner102a8a02010-02-28 20:49:53 +0000103 //
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000104 // NOTE: Strictly speaking, we don't have to check for glue here
Chris Lattner102a8a02010-02-28 20:49:53 +0000105 // because the code in the pattern generator doesn't handle it right. We
106 // do it anyway for thoroughness.
Chris Lattnera8382642010-02-28 21:53:42 +0000107 if (!EN->hasOutFlag() &&
Chris Lattner2a0a3b42010-12-23 18:28:41 +0000108 Pattern.getSrcPattern()->NodeHasProperty(SDNPOutGlue, CGP))
Chris Lattner102a8a02010-02-28 20:49:53 +0000109 ResultsMatch = false;
110
111
112 // If the root result node defines more results than the source root node
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000113 // *and* has a chain or glue input, then we can't match it because it
114 // would end up replacing the extra result with the chain/glue.
Chris Lattner102a8a02010-02-28 20:49:53 +0000115#if 0
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000116 if ((EN->hasGlue() || EN->hasChain()) &&
117 EN->getNumNonChainGlueVTs() > ... need to get no results reliably ...)
Chris Lattner102a8a02010-02-28 20:49:53 +0000118 ResultMatch = false;
119#endif
120
121 if (ResultsMatch) {
122 const SmallVectorImpl<MVT::SimpleValueType> &VTs = EN->getVTList();
123 const SmallVectorImpl<unsigned> &Operands = EN->getOperandList();
Chris Lattner9d67dca2010-02-28 20:55:18 +0000124 MatcherPtr.reset(new MorphNodeToMatcher(EN->getOpcodeName(),
Craig Toppera2c60192014-01-21 07:20:05 +0000125 VTs, Operands,
Chris Lattnera8382642010-02-28 21:53:42 +0000126 EN->hasChain(), EN->hasInFlag(),
127 EN->hasOutFlag(),
Chris Lattner9d67dca2010-02-28 20:55:18 +0000128 EN->hasMemRefs(),
129 EN->getNumFixedArityOperands(),
130 Pattern));
Chris Lattner102a8a02010-02-28 20:49:53 +0000131 return;
132 }
133
Chris Lattner1e634e32010-03-01 22:29:19 +0000134 // FIXME2: Kill off all the SelectionDAG::SelectNodeTo and getMachineNode
Chris Lattner9d67dca2010-02-28 20:55:18 +0000135 // variants.
Chris Lattnerc3f80e02010-02-28 02:31:26 +0000136 }
137
Chris Lattner102a8a02010-02-28 20:49:53 +0000138 ContractNodes(N->getNextPtr(), CGP);
Chris Lattner90b1b9d2010-03-01 02:15:34 +0000139
140
141 // If we have a CheckType/CheckChildType/Record node followed by a
142 // CheckOpcode, invert the two nodes. We prefer to do structural checks
143 // before type checks, as this opens opportunities for factoring on targets
144 // like X86 where many operations are valid on multiple types.
145 if ((isa<CheckTypeMatcher>(N) || isa<CheckChildTypeMatcher>(N) ||
146 isa<RecordMatcher>(N)) &&
Chris Lattner053a28a2010-03-01 07:17:40 +0000147 isa<CheckOpcodeMatcher>(N->getNext())) {
Chris Lattner90b1b9d2010-03-01 02:15:34 +0000148 // Unlink the two nodes from the list.
Ahmed Charles96c9d952014-03-05 10:19:29 +0000149 Matcher *CheckType = MatcherPtr.release();
Chris Lattner90b1b9d2010-03-01 02:15:34 +0000150 Matcher *CheckOpcode = CheckType->takeNext();
151 Matcher *Tail = CheckOpcode->takeNext();
152
153 // Relink them.
154 MatcherPtr.reset(CheckOpcode);
155 CheckOpcode->setNext(CheckType);
156 CheckType->setNext(Tail);
157 return ContractNodes(MatcherPtr, CGP);
158 }
Chris Lattnerab417562010-02-24 07:31:45 +0000159}
160
Chris Lattnera1603892010-03-07 07:20:49 +0000161/// FindNodeWithKind - Scan a series of matchers looking for a matcher with a
162/// specified kind. Return null if we didn't find one otherwise return the
163/// matcher.
164static Matcher *FindNodeWithKind(Matcher *M, Matcher::KindTy Kind) {
Chris Lattnerb9071a22010-03-07 07:01:28 +0000165 for (; M; M = M->getNext())
Chris Lattnera1603892010-03-07 07:20:49 +0000166 if (M->getKind() == Kind)
167 return M;
Craig Topper24064772014-04-15 07:20:03 +0000168 return nullptr;
Chris Lattnerb9071a22010-03-07 07:01:28 +0000169}
170
171
Chris Lattnerd9e1e832010-02-27 06:22:57 +0000172/// FactorNodes - Turn matches like this:
173/// Scope
174/// OPC_CheckType i32
175/// ABC
176/// OPC_CheckType i32
177/// XYZ
178/// into:
179/// OPC_CheckType i32
180/// Scope
181/// ABC
182/// XYZ
183///
Jon Chesterfield1b4eed42017-02-06 19:41:44 +0000184static void FactorNodes(std::unique_ptr<Matcher> &InputMatcherPtr) {
185 // Look for a push node. Iterates instead of recurses to reduce stack usage.
186 ScopeMatcher *Scope = nullptr;
187 std::unique_ptr<Matcher> *RebindableMatcherPtr = &InputMatcherPtr;
188 while (!Scope) {
189 // If we reached the end of the chain, we're done.
190 Matcher *N = RebindableMatcherPtr->get();
191 if (!N) return;
192
193 // If this is not a push node, just scan for one.
194 Scope = dyn_cast<ScopeMatcher>(N);
195 if (!Scope)
196 RebindableMatcherPtr = &(N->getNextPtr());
197 }
198 std::unique_ptr<Matcher> &MatcherPtr = *RebindableMatcherPtr;
Chris Lattnerc36ab922010-02-25 01:57:41 +0000199
Chris Lattnerf7fc2d82010-02-25 19:00:39 +0000200 // Okay, pull together the children of the scope node into a vector so we can
Craig Topperd34bf352016-05-05 06:19:25 +0000201 // inspect it more easily.
Chris Lattner2c3f6492010-02-25 02:04:40 +0000202 SmallVector<Matcher*, 32> OptionsToMatch;
Chris Lattnerc36ab922010-02-25 01:57:41 +0000203
Chris Lattnerf7fc2d82010-02-25 19:00:39 +0000204 for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) {
Chris Lattner62702da02010-02-25 07:45:24 +0000205 // Factor the subexpression.
Ahmed Charles56440fd2014-03-06 05:51:42 +0000206 std::unique_ptr<Matcher> Child(Scope->takeChild(i));
Chris Lattnerf7fc2d82010-02-25 19:00:39 +0000207 FactorNodes(Child);
208
Craig Topper3dc06672016-11-22 07:00:06 +0000209 if (Child) {
210 // If the child is a ScopeMatcher we can just merge its contents.
211 if (auto *SM = dyn_cast<ScopeMatcher>(Child.get())) {
212 for (unsigned j = 0, e = SM->getNumChildren(); j != e; ++j)
213 OptionsToMatch.push_back(SM->takeChild(j));
214 } else {
215 OptionsToMatch.push_back(Child.release());
216 }
217 }
Chris Lattner62702da02010-02-25 07:45:24 +0000218 }
Chris Lattnerc36ab922010-02-25 01:57:41 +0000219
Chris Lattner62702da02010-02-25 07:45:24 +0000220 SmallVector<Matcher*, 32> NewOptionsToMatch;
Chris Lattnerf4d17752010-03-01 06:59:22 +0000221
Chris Lattner4f9a6712010-02-26 08:08:41 +0000222 // Loop over options to match, merging neighboring patterns with identical
223 // starting nodes into a shared matcher.
Chris Lattnerc577b812010-02-27 07:49:13 +0000224 for (unsigned OptionIdx = 0, e = OptionsToMatch.size(); OptionIdx != e;) {
Chris Lattner62702da02010-02-25 07:45:24 +0000225 // Find the set of matchers that start with this node.
Chris Lattnerc577b812010-02-27 07:49:13 +0000226 Matcher *Optn = OptionsToMatch[OptionIdx++];
227
228 if (OptionIdx == e) {
Chris Lattner62702da02010-02-25 07:45:24 +0000229 NewOptionsToMatch.push_back(Optn);
230 continue;
231 }
232
Chris Lattnerc577b812010-02-27 07:49:13 +0000233 // See if the next option starts with the same matcher. If the two
234 // neighbors *do* start with the same matcher, we can factor the matcher out
235 // of at least these two patterns. See what the maximal set we can merge
236 // together is.
Chris Lattner4f9a6712010-02-26 08:08:41 +0000237 SmallVector<Matcher*, 8> EqualMatchers;
238 EqualMatchers.push_back(Optn);
Chris Lattner4f9a6712010-02-26 08:08:41 +0000239
Chris Lattnerc577b812010-02-27 07:49:13 +0000240 // Factor all of the known-equal matchers after this one into the same
241 // group.
242 while (OptionIdx != e && OptionsToMatch[OptionIdx]->isEqual(Optn))
243 EqualMatchers.push_back(OptionsToMatch[OptionIdx++]);
244
245 // If we found a non-equal matcher, see if it is contradictory with the
246 // current node. If so, we know that the ordering relation between the
247 // current sets of nodes and this node don't matter. Look past it to see if
248 // we can merge anything else into this matching group.
249 unsigned Scan = OptionIdx;
250 while (1) {
Chris Lattnerb9071a22010-03-07 07:01:28 +0000251 // If we ran out of stuff to scan, we're done.
252 if (Scan == e) break;
Chris Lattnerc577b812010-02-27 07:49:13 +0000253
Chris Lattnera1603892010-03-07 07:20:49 +0000254 Matcher *ScanMatcher = OptionsToMatch[Scan];
255
Chris Lattnerb9071a22010-03-07 07:01:28 +0000256 // If we found an entry that matches out matcher, merge it into the set to
257 // handle.
Chris Lattnera1603892010-03-07 07:20:49 +0000258 if (Optn->isEqual(ScanMatcher)) {
Chris Lattnerb9071a22010-03-07 07:01:28 +0000259 // If is equal after all, add the option to EqualMatchers and remove it
260 // from OptionsToMatch.
Chris Lattnera1603892010-03-07 07:20:49 +0000261 EqualMatchers.push_back(ScanMatcher);
Chris Lattnerb9071a22010-03-07 07:01:28 +0000262 OptionsToMatch.erase(OptionsToMatch.begin()+Scan);
263 --e;
264 continue;
265 }
266
267 // If the option we're checking for contradicts the start of the list,
268 // skip over it.
Chris Lattnera1603892010-03-07 07:20:49 +0000269 if (Optn->isContradictory(ScanMatcher)) {
Chris Lattnerb9071a22010-03-07 07:01:28 +0000270 ++Scan;
271 continue;
272 }
Chris Lattnerc577b812010-02-27 07:49:13 +0000273
Chris Lattnera1603892010-03-07 07:20:49 +0000274 // If we're scanning for a simple node, see if it occurs later in the
275 // sequence. If so, and if we can move it up, it might be contradictory
276 // or the same as what we're looking for. If so, reorder it.
277 if (Optn->isSimplePredicateOrRecordNode()) {
278 Matcher *M2 = FindNodeWithKind(ScanMatcher, Optn->getKind());
Craig Topper24064772014-04-15 07:20:03 +0000279 if (M2 && M2 != ScanMatcher &&
Chris Lattnera1603892010-03-07 07:20:49 +0000280 M2->canMoveBefore(ScanMatcher) &&
281 (M2->isEqual(Optn) || M2->isContradictory(Optn))) {
282 Matcher *MatcherWithoutM2 = ScanMatcher->unlinkNode(M2);
283 M2->setNext(MatcherWithoutM2);
284 OptionsToMatch[Scan] = M2;
Chris Lattnerb9071a22010-03-07 07:01:28 +0000285 continue;
286 }
287 }
288
289 // Otherwise, we don't know how to handle this entry, we have to bail.
290 break;
Chris Lattnerc577b812010-02-27 07:49:13 +0000291 }
292
Chris Lattner278606b2010-02-27 21:48:43 +0000293 if (Scan != e &&
294 // Don't print it's obvious nothing extra could be merged anyway.
295 Scan+1 != e) {
Chris Lattnerc95d58d2010-03-07 07:21:24 +0000296 DEBUG(errs() << "Couldn't merge this:\n";
Chris Lattner2586c862010-02-27 08:11:15 +0000297 Optn->print(errs(), 4);
298 errs() << "into this:\n";
299 OptionsToMatch[Scan]->print(errs(), 4);
Chris Lattner21a7bf32010-02-27 08:13:23 +0000300 if (Scan+1 != e)
Chris Lattner2586c862010-02-27 08:11:15 +0000301 OptionsToMatch[Scan+1]->printOne(errs());
Chris Lattner21a7bf32010-02-27 08:13:23 +0000302 if (Scan+2 < e)
Chris Lattner2586c862010-02-27 08:11:15 +0000303 OptionsToMatch[Scan+2]->printOne(errs());
Chris Lattnerc95d58d2010-03-07 07:21:24 +0000304 errs() << "\n");
Chris Lattnerc577b812010-02-27 07:49:13 +0000305 }
306
307 // If we only found one option starting with this matcher, no factoring is
308 // possible.
309 if (EqualMatchers.size() == 1) {
310 NewOptionsToMatch.push_back(EqualMatchers[0]);
311 continue;
312 }
Chris Lattner4f9a6712010-02-26 08:08:41 +0000313
Chris Lattner62702da02010-02-25 07:45:24 +0000314 // Factor these checks by pulling the first node off each entry and
Chris Lattnerbe5b6342010-02-26 07:36:37 +0000315 // discarding it. Take the first one off the first entry to reuse.
316 Matcher *Shared = Optn;
317 Optn = Optn->takeNext();
318 EqualMatchers[0] = Optn;
319
Chris Lattner4f9a6712010-02-26 08:08:41 +0000320 // Remove and delete the first node from the other matchers we're factoring.
321 for (unsigned i = 1, e = EqualMatchers.size(); i != e; ++i) {
322 Matcher *Tmp = EqualMatchers[i]->takeNext();
323 delete EqualMatchers[i];
324 EqualMatchers[i] = Tmp;
325 }
Chris Lattner62702da02010-02-25 07:45:24 +0000326
Craig Toppera2c60192014-01-21 07:20:05 +0000327 Shared->setNext(new ScopeMatcher(EqualMatchers));
Chris Lattnerbe5b6342010-02-26 07:36:37 +0000328
329 // Recursively factor the newly created node.
330 FactorNodes(Shared->getNextPtr());
331
332 NewOptionsToMatch.push_back(Shared);
Chris Lattner62702da02010-02-25 07:45:24 +0000333 }
Chris Lattnerf4d17752010-03-01 06:59:22 +0000334
335 // If we're down to a single pattern to match, then we don't need this scope
336 // anymore.
337 if (NewOptionsToMatch.size() == 1) {
338 MatcherPtr.reset(NewOptionsToMatch[0]);
339 return;
340 }
341
Chris Lattner664ac982010-03-01 22:04:33 +0000342 if (NewOptionsToMatch.empty()) {
David Blaikieb61064e2014-07-19 01:05:11 +0000343 MatcherPtr.reset();
Chris Lattner664ac982010-03-01 22:04:33 +0000344 return;
345 }
346
Chris Lattnerf4d17752010-03-01 06:59:22 +0000347 // If our factoring failed (didn't achieve anything) see if we can simplify in
348 // other ways.
349
350 // Check to see if all of the leading entries are now opcode checks. If so,
351 // we can convert this Scope to be a OpcodeSwitch instead.
Chris Lattner3e1ffd02010-03-03 06:28:15 +0000352 bool AllOpcodeChecks = true, AllTypeChecks = true;
Chris Lattnerf4d17752010-03-01 06:59:22 +0000353 for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i) {
Chris Lattnerb9071a22010-03-07 07:01:28 +0000354 // Check to see if this breaks a series of CheckOpcodeMatchers.
355 if (AllOpcodeChecks &&
356 !isa<CheckOpcodeMatcher>(NewOptionsToMatch[i])) {
Chris Lattnerf4d17752010-03-01 06:59:22 +0000357#if 0
Chris Lattnerb9071a22010-03-07 07:01:28 +0000358 if (i > 3) {
Chris Lattner3e1ffd02010-03-03 06:28:15 +0000359 errs() << "FAILING OPC #" << i << "\n";
360 NewOptionsToMatch[i]->dump();
361 }
Chris Lattnerf4d17752010-03-01 06:59:22 +0000362#endif
Chris Lattner3e1ffd02010-03-03 06:28:15 +0000363 AllOpcodeChecks = false;
364 }
365
Chris Lattnerb9071a22010-03-07 07:01:28 +0000366 // Check to see if this breaks a series of CheckTypeMatcher's.
367 if (AllTypeChecks) {
Chris Lattnera1603892010-03-07 07:20:49 +0000368 CheckTypeMatcher *CTM =
369 cast_or_null<CheckTypeMatcher>(FindNodeWithKind(NewOptionsToMatch[i],
370 Matcher::CheckType));
Craig Topper24064772014-04-15 07:20:03 +0000371 if (!CTM ||
Chris Lattnerb9071a22010-03-07 07:01:28 +0000372 // iPTR checks could alias any other case without us knowing, don't
373 // bother with them.
374 CTM->getType() == MVT::iPTR ||
Chris Lattner6c2d1782010-03-24 00:41:19 +0000375 // SwitchType only works for result #0.
376 CTM->getResNo() != 0 ||
Chris Lattnerb9071a22010-03-07 07:01:28 +0000377 // If the CheckType isn't at the start of the list, see if we can move
378 // it there.
379 !CTM->canMoveBefore(NewOptionsToMatch[i])) {
Chris Lattner3e1ffd02010-03-03 06:28:15 +0000380#if 0
Chris Lattnerb9071a22010-03-07 07:01:28 +0000381 if (i > 3 && AllTypeChecks) {
382 errs() << "FAILING TYPE #" << i << "\n";
383 NewOptionsToMatch[i]->dump();
384 }
Chris Lattner3e1ffd02010-03-03 06:28:15 +0000385#endif
Chris Lattnerb9071a22010-03-07 07:01:28 +0000386 AllTypeChecks = false;
387 }
Chris Lattner3e1ffd02010-03-03 06:28:15 +0000388 }
Chris Lattnerf4d17752010-03-01 06:59:22 +0000389 }
390
391 // If all the options are CheckOpcode's, we can form the SwitchOpcode, woot.
392 if (AllOpcodeChecks) {
393 StringSet<> Opcodes;
394 SmallVector<std::pair<const SDNodeInfo*, Matcher*>, 8> Cases;
395 for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i) {
Chris Lattner3e1ffd02010-03-03 06:28:15 +0000396 CheckOpcodeMatcher *COM = cast<CheckOpcodeMatcher>(NewOptionsToMatch[i]);
David Blaikie03569752014-11-19 02:56:00 +0000397 assert(Opcodes.insert(COM->getOpcode().getEnumName()).second &&
Chris Lattnerf4d17752010-03-01 06:59:22 +0000398 "Duplicate opcodes not factored?");
Craig Topperc6b36692016-05-06 06:56:14 +0000399 Cases.push_back(std::make_pair(&COM->getOpcode(), COM->takeNext()));
400 delete COM;
Chris Lattnerf4d17752010-03-01 06:59:22 +0000401 }
402
Craig Toppera2c60192014-01-21 07:20:05 +0000403 MatcherPtr.reset(new SwitchOpcodeMatcher(Cases));
Chris Lattnerf4d17752010-03-01 06:59:22 +0000404 return;
405 }
406
Chris Lattner3e1ffd02010-03-03 06:28:15 +0000407 // If all the options are CheckType's, we can form the SwitchType, woot.
408 if (AllTypeChecks) {
Chris Lattnerb9071a22010-03-07 07:01:28 +0000409 DenseMap<unsigned, unsigned> TypeEntry;
Chris Lattner3e1ffd02010-03-03 06:28:15 +0000410 SmallVector<std::pair<MVT::SimpleValueType, Matcher*>, 8> Cases;
411 for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i) {
Chris Lattnera1603892010-03-07 07:20:49 +0000412 CheckTypeMatcher *CTM =
413 cast_or_null<CheckTypeMatcher>(FindNodeWithKind(NewOptionsToMatch[i],
414 Matcher::CheckType));
Chris Lattnerb9071a22010-03-07 07:01:28 +0000415 Matcher *MatcherWithoutCTM = NewOptionsToMatch[i]->unlinkNode(CTM);
416 MVT::SimpleValueType CTMTy = CTM->getType();
417 delete CTM;
418
419 unsigned &Entry = TypeEntry[CTMTy];
420 if (Entry != 0) {
421 // If we have unfactored duplicate types, then we should factor them.
422 Matcher *PrevMatcher = Cases[Entry-1].second;
423 if (ScopeMatcher *SM = dyn_cast<ScopeMatcher>(PrevMatcher)) {
424 SM->setNumChildren(SM->getNumChildren()+1);
425 SM->resetChild(SM->getNumChildren()-1, MatcherWithoutCTM);
426 continue;
427 }
428
429 Matcher *Entries[2] = { PrevMatcher, MatcherWithoutCTM };
Craig Topper5e87d522016-11-21 04:07:58 +0000430 Cases[Entry-1].second = new ScopeMatcher(Entries);
Chris Lattnerb9071a22010-03-07 07:01:28 +0000431 continue;
432 }
433
434 Entry = Cases.size()+1;
435 Cases.push_back(std::make_pair(CTMTy, MatcherWithoutCTM));
Chris Lattner3e1ffd02010-03-03 06:28:15 +0000436 }
437
Craig Topper5e87d522016-11-21 04:07:58 +0000438 // Make sure we recursively factor any scopes we may have created.
439 for (auto &M : Cases) {
440 if (ScopeMatcher *SM = dyn_cast<ScopeMatcher>(M.second)) {
441 std::unique_ptr<Matcher> Scope(SM);
442 FactorNodes(Scope);
443 M.second = Scope.release();
444 assert(M.second && "null matcher");
445 }
446 }
447
Chris Lattnerb9071a22010-03-07 07:01:28 +0000448 if (Cases.size() != 1) {
Craig Toppera2c60192014-01-21 07:20:05 +0000449 MatcherPtr.reset(new SwitchTypeMatcher(Cases));
Chris Lattnerb9071a22010-03-07 07:01:28 +0000450 } else {
451 // If we factored and ended up with one case, create it now.
Chris Lattner6c2d1782010-03-24 00:41:19 +0000452 MatcherPtr.reset(new CheckTypeMatcher(Cases[0].first, 0));
Chris Lattnerb9071a22010-03-07 07:01:28 +0000453 MatcherPtr->setNext(Cases[0].second);
454 }
Chris Lattner3e1ffd02010-03-03 06:28:15 +0000455 return;
456 }
457
Chris Lattner62702da02010-02-25 07:45:24 +0000458
Chris Lattner00f2e4b2010-03-01 22:19:47 +0000459 // Reassemble the Scope node with the adjusted children.
460 Scope->setNumChildren(NewOptionsToMatch.size());
461 for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i)
462 Scope->resetChild(i, NewOptionsToMatch[i]);
Chris Lattnerc36ab922010-02-25 01:57:41 +0000463}
464
Craig Topper574f6d42014-12-15 00:40:07 +0000465void
466llvm::OptimizeMatcher(std::unique_ptr<Matcher> &MatcherPtr,
467 const CodeGenDAGPatterns &CGP) {
Chris Lattner102a8a02010-02-28 20:49:53 +0000468 ContractNodes(MatcherPtr, CGP);
Chris Lattnerc36ab922010-02-25 01:57:41 +0000469 FactorNodes(MatcherPtr);
Chris Lattnere7327432010-02-24 07:06:50 +0000470}