Bug Summary

File:utils/TableGen/DAGISelMatcherOpt.cpp
Warning:line 59, column 35
Potential leak of memory pointed to by 'New'

Annotated Source Code

1//===- 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"
15#include "CodeGenDAGPatterns.h"
16#include "llvm/ADT/StringSet.h"
17#include "llvm/Support/Debug.h"
18#include "llvm/Support/raw_ostream.h"
19using namespace llvm;
20
21#define DEBUG_TYPE"isel-opt" "isel-opt"
22
23/// ContractNodes - Turn multiple matcher node patterns like 'MoveChild+Record'
24/// into single compound nodes like RecordChild.
25static void ContractNodes(std::unique_ptr<Matcher> &MatcherPtr,
26 const CodeGenDAGPatterns &CGP) {
27 // If we reached the end of the chain, we're done.
28 Matcher *N = MatcherPtr.get();
29 if (!N) return;
2
Assuming 'N' is non-null
3
Taking false branch
10
Taking false branch
17
Taking false branch
23
Taking false branch
30
Taking false branch
30
31 // If we have a scope node, walk down all of the children.
32 if (ScopeMatcher *Scope = dyn_cast<ScopeMatcher>(N)) {
4
Taking false branch
11
Taking false branch
18
Taking false branch
24
Taking false branch
31
Assuming 'Scope' is null
32
Taking false branch
33 for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) {
34 std::unique_ptr<Matcher> Child(Scope->takeChild(i));
35 ContractNodes(Child, CGP);
36 Scope->resetChild(i, Child.release());
37 }
38 return;
39 }
40
41 // If we found a movechild node with a node that comes in a 'foochild' form,
42 // transform it.
43 if (MoveChildMatcher *MC = dyn_cast<MoveChildMatcher>(N)) {
5
Taking false branch
12
Taking false branch
19
Taking false branch
25
Taking false branch
33
Assuming 'MC' is non-null
34
Taking true branch
44 Matcher *New = nullptr;
45 if (RecordMatcher *RM = dyn_cast<RecordMatcher>(MC->getNext()))
35
Assuming 'RM' is null
36
Taking false branch
46 if (MC->getChildNo() < 8) // Only have RecordChild0...7
47 New = new RecordChildMatcher(MC->getChildNo(), RM->getWhatFor(),
48 RM->getResultNo());
49
50 if (CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(MC->getNext()))
37
Assuming 'CT' is non-null
38
Taking true branch
51 if (MC->getChildNo() < 8 && // Only have CheckChildType0...7
39
Assuming the condition is true
41
Taking true branch
52 CT->getResNo() == 0) // CheckChildType checks res #0
40
Assuming the condition is true
53 New = new CheckChildTypeMatcher(MC->getChildNo(), CT->getType());
42
Memory is allocated
54
55 if (CheckSameMatcher *CS = dyn_cast<CheckSameMatcher>(MC->getNext()))
43
Assuming 'CS' is non-null
44
Taking true branch
56 if (MC->getChildNo() < 4) // Only have CheckChildSame0...3
45
Assuming the condition is true
46
Taking true branch
57 New = new CheckChildSameMatcher(MC->getChildNo(), CS->getMatchNumber());
58
59 if (CheckIntegerMatcher *CS = dyn_cast<CheckIntegerMatcher>(MC->getNext()))
47
Potential leak of memory pointed to by 'New'
60 if (MC->getChildNo() < 5) // Only have CheckChildInteger0...4
61 New = new CheckChildIntegerMatcher(MC->getChildNo(), CS->getValue());
62
63 if (New) {
64 // Insert the new node.
65 New->setNext(MatcherPtr.release());
66 MatcherPtr.reset(New);
67 // Remove the old one.
68 MC->setNext(MC->getNext()->takeNext());
69 return ContractNodes(MatcherPtr, CGP);
70 }
71 }
72
73 // Zap movechild -> moveparent.
74 if (MoveChildMatcher *MC = dyn_cast<MoveChildMatcher>(N))
6
Taking false branch
13
Taking false branch
20
Taking false branch
26
Taking false branch
75 if (MoveParentMatcher *MP =
76 dyn_cast<MoveParentMatcher>(MC->getNext())) {
77 MatcherPtr.reset(MP->takeNext());
78 return ContractNodes(MatcherPtr, CGP);
79 }
80
81 // Turn EmitNode->CompleteMatch into MorphNodeTo if we can.
82 if (EmitNodeMatcher *EN = dyn_cast<EmitNodeMatcher>(N))
7
Taking false branch
14
Taking false branch
21
Taking false branch
27
Taking false branch
83 if (CompleteMatchMatcher *CM =
84 dyn_cast<CompleteMatchMatcher>(EN->getNext())) {
85 // We can only use MorphNodeTo if the result values match up.
86 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
92 // If the selected node defines a subset of the glue/chain results, we
93 // can't use MorphNodeTo. For example, we can't use MorphNodeTo if the
94 // 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
101 // If the matched node has glue and the output root doesn't, we can't
102 // use MorphNodeTo.
103 //
104 // NOTE: Strictly speaking, we don't have to check for glue here
105 // because the code in the pattern generator doesn't handle it right. We
106 // do it anyway for thoroughness.
107 if (!EN->hasOutFlag() &&
108 Pattern.getSrcPattern()->NodeHasProperty(SDNPOutGlue, CGP))
109 ResultsMatch = false;
110
111
112 // If the root result node defines more results than the source root node
113 // *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.
115#if 0
116 if ((EN->hasGlue() || EN->hasChain()) &&
117 EN->getNumNonChainGlueVTs() > ... need to get no results reliably ...)
118 ResultMatch = false;
119#endif
120
121 if (ResultsMatch) {
122 const SmallVectorImpl<MVT::SimpleValueType> &VTs = EN->getVTList();
123 const SmallVectorImpl<unsigned> &Operands = EN->getOperandList();
124 MatcherPtr.reset(new MorphNodeToMatcher(EN->getOpcodeName(),
125 VTs, Operands,
126 EN->hasChain(), EN->hasInFlag(),
127 EN->hasOutFlag(),
128 EN->hasMemRefs(),
129 EN->getNumFixedArityOperands(),
130 Pattern));
131 return;
132 }
133
134 // FIXME2: Kill off all the SelectionDAG::SelectNodeTo and getMachineNode
135 // variants.
136 }
137
138 ContractNodes(N->getNextPtr(), CGP);
22
Calling 'ContractNodes'
139
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) ||
8
Taking true branch
15
Taking true branch
28
Taking true branch
146 isa<RecordMatcher>(N)) &&
147 isa<CheckOpcodeMatcher>(N->getNext())) {
148 // Unlink the two nodes from the list.
149 Matcher *CheckType = MatcherPtr.release();
150 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);
9
Calling 'ContractNodes'
16
Calling 'ContractNodes'
29
Calling 'ContractNodes'
158 }
159}
160
161/// 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) {
165 for (; M; M = M->getNext())
166 if (M->getKind() == Kind)
167 return M;
168 return nullptr;
169}
170
171
172/// 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///
184static 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;
199
200 // Okay, pull together the children of the scope node into a vector so we can
201 // inspect it more easily.
202 SmallVector<Matcher*, 32> OptionsToMatch;
203
204 for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) {
205 // Factor the subexpression.
206 std::unique_ptr<Matcher> Child(Scope->takeChild(i));
207 FactorNodes(Child);
208
209 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 }
218 }
219
220 SmallVector<Matcher*, 32> NewOptionsToMatch;
221
222 // Loop over options to match, merging neighboring patterns with identical
223 // starting nodes into a shared matcher.
224 for (unsigned OptionIdx = 0, e = OptionsToMatch.size(); OptionIdx != e;) {
225 // Find the set of matchers that start with this node.
226 Matcher *Optn = OptionsToMatch[OptionIdx++];
227
228 if (OptionIdx == e) {
229 NewOptionsToMatch.push_back(Optn);
230 continue;
231 }
232
233 // 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.
237 SmallVector<Matcher*, 8> EqualMatchers;
238 EqualMatchers.push_back(Optn);
239
240 // 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) {
251 // If we ran out of stuff to scan, we're done.
252 if (Scan == e) break;
253
254 Matcher *ScanMatcher = OptionsToMatch[Scan];
255
256 // If we found an entry that matches out matcher, merge it into the set to
257 // handle.
258 if (Optn->isEqual(ScanMatcher)) {
259 // If is equal after all, add the option to EqualMatchers and remove it
260 // from OptionsToMatch.
261 EqualMatchers.push_back(ScanMatcher);
262 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.
269 if (Optn->isContradictory(ScanMatcher)) {
270 ++Scan;
271 continue;
272 }
273
274 // 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());
279 if (M2 && M2 != ScanMatcher &&
280 M2->canMoveBefore(ScanMatcher) &&
281 (M2->isEqual(Optn) || M2->isContradictory(Optn))) {
282 Matcher *MatcherWithoutM2 = ScanMatcher->unlinkNode(M2);
283 M2->setNext(MatcherWithoutM2);
284 OptionsToMatch[Scan] = M2;
285 continue;
286 }
287 }
288
289 // Otherwise, we don't know how to handle this entry, we have to bail.
290 break;
291 }
292
293 if (Scan != e &&
294 // Don't print it's obvious nothing extra could be merged anyway.
295 Scan+1 != e) {
296 DEBUG(errs() << "Couldn't merge this:\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("isel-opt")) { errs() << "Couldn't merge this:\n"; Optn
->print(errs(), 4); errs() << "into this:\n"; OptionsToMatch
[Scan]->print(errs(), 4); if (Scan+1 != e) OptionsToMatch[
Scan+1]->printOne(errs()); if (Scan+2 < e) OptionsToMatch
[Scan+2]->printOne(errs()); errs() << "\n"; } } while
(false)
297 Optn->print(errs(), 4);do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("isel-opt")) { errs() << "Couldn't merge this:\n"; Optn
->print(errs(), 4); errs() << "into this:\n"; OptionsToMatch
[Scan]->print(errs(), 4); if (Scan+1 != e) OptionsToMatch[
Scan+1]->printOne(errs()); if (Scan+2 < e) OptionsToMatch
[Scan+2]->printOne(errs()); errs() << "\n"; } } while
(false)
298 errs() << "into this:\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("isel-opt")) { errs() << "Couldn't merge this:\n"; Optn
->print(errs(), 4); errs() << "into this:\n"; OptionsToMatch
[Scan]->print(errs(), 4); if (Scan+1 != e) OptionsToMatch[
Scan+1]->printOne(errs()); if (Scan+2 < e) OptionsToMatch
[Scan+2]->printOne(errs()); errs() << "\n"; } } while
(false)
299 OptionsToMatch[Scan]->print(errs(), 4);do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("isel-opt")) { errs() << "Couldn't merge this:\n"; Optn
->print(errs(), 4); errs() << "into this:\n"; OptionsToMatch
[Scan]->print(errs(), 4); if (Scan+1 != e) OptionsToMatch[
Scan+1]->printOne(errs()); if (Scan+2 < e) OptionsToMatch
[Scan+2]->printOne(errs()); errs() << "\n"; } } while
(false)
300 if (Scan+1 != e)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("isel-opt")) { errs() << "Couldn't merge this:\n"; Optn
->print(errs(), 4); errs() << "into this:\n"; OptionsToMatch
[Scan]->print(errs(), 4); if (Scan+1 != e) OptionsToMatch[
Scan+1]->printOne(errs()); if (Scan+2 < e) OptionsToMatch
[Scan+2]->printOne(errs()); errs() << "\n"; } } while
(false)
301 OptionsToMatch[Scan+1]->printOne(errs());do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("isel-opt")) { errs() << "Couldn't merge this:\n"; Optn
->print(errs(), 4); errs() << "into this:\n"; OptionsToMatch
[Scan]->print(errs(), 4); if (Scan+1 != e) OptionsToMatch[
Scan+1]->printOne(errs()); if (Scan+2 < e) OptionsToMatch
[Scan+2]->printOne(errs()); errs() << "\n"; } } while
(false)
302 if (Scan+2 < e)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("isel-opt")) { errs() << "Couldn't merge this:\n"; Optn
->print(errs(), 4); errs() << "into this:\n"; OptionsToMatch
[Scan]->print(errs(), 4); if (Scan+1 != e) OptionsToMatch[
Scan+1]->printOne(errs()); if (Scan+2 < e) OptionsToMatch
[Scan+2]->printOne(errs()); errs() << "\n"; } } while
(false)
303 OptionsToMatch[Scan+2]->printOne(errs());do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("isel-opt")) { errs() << "Couldn't merge this:\n"; Optn
->print(errs(), 4); errs() << "into this:\n"; OptionsToMatch
[Scan]->print(errs(), 4); if (Scan+1 != e) OptionsToMatch[
Scan+1]->printOne(errs()); if (Scan+2 < e) OptionsToMatch
[Scan+2]->printOne(errs()); errs() << "\n"; } } while
(false)
304 errs() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("isel-opt")) { errs() << "Couldn't merge this:\n"; Optn
->print(errs(), 4); errs() << "into this:\n"; OptionsToMatch
[Scan]->print(errs(), 4); if (Scan+1 != e) OptionsToMatch[
Scan+1]->printOne(errs()); if (Scan+2 < e) OptionsToMatch
[Scan+2]->printOne(errs()); errs() << "\n"; } } while
(false)
;
305 }
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 }
313
314 // Factor these checks by pulling the first node off each entry and
315 // 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
320 // 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 }
326
327 Shared->setNext(new ScopeMatcher(EqualMatchers));
328
329 // Recursively factor the newly created node.
330 FactorNodes(Shared->getNextPtr());
331
332 NewOptionsToMatch.push_back(Shared);
333 }
334
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
342 if (NewOptionsToMatch.empty()) {
343 MatcherPtr.reset();
344 return;
345 }
346
347 // 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.
352 bool AllOpcodeChecks = true, AllTypeChecks = true;
353 for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i) {
354 // Check to see if this breaks a series of CheckOpcodeMatchers.
355 if (AllOpcodeChecks &&
356 !isa<CheckOpcodeMatcher>(NewOptionsToMatch[i])) {
357#if 0
358 if (i > 3) {
359 errs() << "FAILING OPC #" << i << "\n";
360 NewOptionsToMatch[i]->dump();
361 }
362#endif
363 AllOpcodeChecks = false;
364 }
365
366 // Check to see if this breaks a series of CheckTypeMatcher's.
367 if (AllTypeChecks) {
368 CheckTypeMatcher *CTM =
369 cast_or_null<CheckTypeMatcher>(FindNodeWithKind(NewOptionsToMatch[i],
370 Matcher::CheckType));
371 if (!CTM ||
372 // iPTR checks could alias any other case without us knowing, don't
373 // bother with them.
374 CTM->getType() == MVT::iPTR ||
375 // SwitchType only works for result #0.
376 CTM->getResNo() != 0 ||
377 // If the CheckType isn't at the start of the list, see if we can move
378 // it there.
379 !CTM->canMoveBefore(NewOptionsToMatch[i])) {
380#if 0
381 if (i > 3 && AllTypeChecks) {
382 errs() << "FAILING TYPE #" << i << "\n";
383 NewOptionsToMatch[i]->dump();
384 }
385#endif
386 AllTypeChecks = false;
387 }
388 }
389 }
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) {
396 CheckOpcodeMatcher *COM = cast<CheckOpcodeMatcher>(NewOptionsToMatch[i]);
397 assert(Opcodes.insert(COM->getOpcode().getEnumName()).second &&(static_cast <bool> (Opcodes.insert(COM->getOpcode()
.getEnumName()).second && "Duplicate opcodes not factored?"
) ? void (0) : __assert_fail ("Opcodes.insert(COM->getOpcode().getEnumName()).second && \"Duplicate opcodes not factored?\""
, "/build/llvm-toolchain-snapshot-6.0~svn318601/utils/TableGen/DAGISelMatcherOpt.cpp"
, 398, __extension__ __PRETTY_FUNCTION__))
398 "Duplicate opcodes not factored?")(static_cast <bool> (Opcodes.insert(COM->getOpcode()
.getEnumName()).second && "Duplicate opcodes not factored?"
) ? void (0) : __assert_fail ("Opcodes.insert(COM->getOpcode().getEnumName()).second && \"Duplicate opcodes not factored?\""
, "/build/llvm-toolchain-snapshot-6.0~svn318601/utils/TableGen/DAGISelMatcherOpt.cpp"
, 398, __extension__ __PRETTY_FUNCTION__))
;
399 Cases.push_back(std::make_pair(&COM->getOpcode(), COM->takeNext()));
400 delete COM;
401 }
402
403 MatcherPtr.reset(new SwitchOpcodeMatcher(Cases));
404 return;
405 }
406
407 // If all the options are CheckType's, we can form the SwitchType, woot.
408 if (AllTypeChecks) {
409 DenseMap<unsigned, unsigned> TypeEntry;
410 SmallVector<std::pair<MVT::SimpleValueType, Matcher*>, 8> Cases;
411 for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i) {
412 CheckTypeMatcher *CTM =
413 cast_or_null<CheckTypeMatcher>(FindNodeWithKind(NewOptionsToMatch[i],
414 Matcher::CheckType));
415 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 };
430 Cases[Entry-1].second = new ScopeMatcher(Entries);
431 continue;
432 }
433
434 Entry = Cases.size()+1;
435 Cases.push_back(std::make_pair(CTMTy, MatcherWithoutCTM));
436 }
437
438 // 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")(static_cast <bool> (M.second && "null matcher"
) ? void (0) : __assert_fail ("M.second && \"null matcher\""
, "/build/llvm-toolchain-snapshot-6.0~svn318601/utils/TableGen/DAGISelMatcherOpt.cpp"
, 444, __extension__ __PRETTY_FUNCTION__))
;
445 }
446 }
447
448 if (Cases.size() != 1) {
449 MatcherPtr.reset(new SwitchTypeMatcher(Cases));
450 } else {
451 // If we factored and ended up with one case, create it now.
452 MatcherPtr.reset(new CheckTypeMatcher(Cases[0].first, 0));
453 MatcherPtr->setNext(Cases[0].second);
454 }
455 return;
456 }
457
458
459 // 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]);
463}
464
465void
466llvm::OptimizeMatcher(std::unique_ptr<Matcher> &MatcherPtr,
467 const CodeGenDAGPatterns &CGP) {
468 ContractNodes(MatcherPtr, CGP);
1
Calling 'ContractNodes'
469 FactorNodes(MatcherPtr);
470}