teach tblgen to be more aggressive when factoring CheckType nodes.
[oota-llvm.git] / utils / TableGen / DAGISelMatcherOpt.cpp
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 #define DEBUG_TYPE "isel-opt"
15 #include "DAGISelMatcher.h"
16 #include "CodeGenDAGPatterns.h"
17 #include "llvm/ADT/DenseSet.h"
18 #include "llvm/ADT/StringSet.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include <vector>
22 using namespace llvm;
23
24 /// ContractNodes - Turn multiple matcher node patterns like 'MoveChild+Record'
25 /// into single compound nodes like RecordChild.
26 static void ContractNodes(OwningPtr<Matcher> &MatcherPtr,
27                           const CodeGenDAGPatterns &CGP) {
28   // If we reached the end of the chain, we're done.
29   Matcher *N = MatcherPtr.get();
30   if (N == 0) return;
31   
32   // If we have a scope node, walk down all of the children.
33   if (ScopeMatcher *Scope = dyn_cast<ScopeMatcher>(N)) {
34     for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) {
35       OwningPtr<Matcher> Child(Scope->takeChild(i));
36       ContractNodes(Child, CGP);
37       Scope->resetChild(i, Child.take());
38     }
39     return;
40   }
41   
42   // If we found a movechild node with a node that comes in a 'foochild' form,
43   // transform it.
44   if (MoveChildMatcher *MC = dyn_cast<MoveChildMatcher>(N)) {
45     Matcher *New = 0;
46     if (RecordMatcher *RM = dyn_cast<RecordMatcher>(MC->getNext()))
47       New = new RecordChildMatcher(MC->getChildNo(), RM->getWhatFor(),
48                                    RM->getResultNo());
49     
50     if (CheckTypeMatcher *CT= dyn_cast<CheckTypeMatcher>(MC->getNext()))
51       New = new CheckChildTypeMatcher(MC->getChildNo(), CT->getType());
52     
53     if (New) {
54       // Insert the new node.
55       New->setNext(MatcherPtr.take());
56       MatcherPtr.reset(New);
57       // Remove the old one.
58       MC->setNext(MC->getNext()->takeNext());
59       return ContractNodes(MatcherPtr, CGP);
60     }
61   }
62   
63   // Zap movechild -> moveparent.
64   if (MoveChildMatcher *MC = dyn_cast<MoveChildMatcher>(N))
65     if (MoveParentMatcher *MP = 
66           dyn_cast<MoveParentMatcher>(MC->getNext())) {
67       MatcherPtr.reset(MP->takeNext());
68       return ContractNodes(MatcherPtr, CGP);
69     }
70
71   // Turn EmitNode->MarkFlagResults->CompleteMatch into
72   // MarkFlagResults->EmitNode->CompleteMatch when we can to encourage
73   // MorphNodeTo formation.  This is safe because MarkFlagResults never refers
74   // to the root of the pattern.
75   if (isa<EmitNodeMatcher>(N) && isa<MarkFlagResultsMatcher>(N->getNext()) &&
76       isa<CompleteMatchMatcher>(N->getNext()->getNext())) {
77     // Unlink the two nodes from the list.
78     Matcher *EmitNode = MatcherPtr.take();
79     Matcher *MFR = EmitNode->takeNext();
80     Matcher *Tail = MFR->takeNext();
81         
82     // Relink them.
83     MatcherPtr.reset(MFR);
84     MFR->setNext(EmitNode);
85     EmitNode->setNext(Tail);
86     return ContractNodes(MatcherPtr, CGP);
87   }
88
89   // Turn EmitNode->CompleteMatch into MorphNodeTo if we can.
90   if (EmitNodeMatcher *EN = dyn_cast<EmitNodeMatcher>(N))
91     if (CompleteMatchMatcher *CM =
92           dyn_cast<CompleteMatchMatcher>(EN->getNext())) {
93       // We can only use MorphNodeTo if the result values match up.
94       unsigned RootResultFirst = EN->getFirstResultSlot();
95       bool ResultsMatch = true;
96       for (unsigned i = 0, e = CM->getNumResults(); i != e; ++i)
97         if (CM->getResult(i) != RootResultFirst+i)
98           ResultsMatch = false;
99       
100       // If the selected node defines a subset of the flag/chain results, we
101       // can't use MorphNodeTo.  For example, we can't use MorphNodeTo if the
102       // matched pattern has a chain but the root node doesn't.
103       const PatternToMatch &Pattern = CM->getPattern();
104       
105       if (!EN->hasChain() &&
106           Pattern.getSrcPattern()->NodeHasProperty(SDNPHasChain, CGP))
107         ResultsMatch = false;
108
109       // If the matched node has a flag and the output root doesn't, we can't
110       // use MorphNodeTo.
111       //
112       // NOTE: Strictly speaking, we don't have to check for the flag here
113       // because the code in the pattern generator doesn't handle it right.  We
114       // do it anyway for thoroughness.
115       if (!EN->hasOutFlag() &&
116           Pattern.getSrcPattern()->NodeHasProperty(SDNPOutFlag, CGP))
117         ResultsMatch = false;
118       
119       
120       // If the root result node defines more results than the source root node
121       // *and* has a chain or flag input, then we can't match it because it
122       // would end up replacing the extra result with the chain/flag.
123 #if 0
124       if ((EN->hasFlag() || EN->hasChain()) &&
125           EN->getNumNonChainFlagVTs() > ... need to get no results reliably ...)
126         ResultMatch = false;
127 #endif
128           
129       if (ResultsMatch) {
130         const SmallVectorImpl<MVT::SimpleValueType> &VTs = EN->getVTList();
131         const SmallVectorImpl<unsigned> &Operands = EN->getOperandList();
132         MatcherPtr.reset(new MorphNodeToMatcher(EN->getOpcodeName(),
133                                                 VTs.data(), VTs.size(),
134                                                 Operands.data(),Operands.size(),
135                                                 EN->hasChain(), EN->hasInFlag(),
136                                                 EN->hasOutFlag(),
137                                                 EN->hasMemRefs(),
138                                                 EN->getNumFixedArityOperands(),
139                                                 Pattern));
140         return;
141       }
142
143       // FIXME2: Kill off all the SelectionDAG::SelectNodeTo and getMachineNode
144       // variants.
145     }
146   
147   ContractNodes(N->getNextPtr(), CGP);
148   
149   
150   // If we have a CheckType/CheckChildType/Record node followed by a
151   // CheckOpcode, invert the two nodes.  We prefer to do structural checks
152   // before type checks, as this opens opportunities for factoring on targets
153   // like X86 where many operations are valid on multiple types.
154   if ((isa<CheckTypeMatcher>(N) || isa<CheckChildTypeMatcher>(N) ||
155        isa<RecordMatcher>(N)) &&
156       isa<CheckOpcodeMatcher>(N->getNext())) {
157     // Unlink the two nodes from the list.
158     Matcher *CheckType = MatcherPtr.take();
159     Matcher *CheckOpcode = CheckType->takeNext();
160     Matcher *Tail = CheckOpcode->takeNext();
161     
162     // Relink them.
163     MatcherPtr.reset(CheckOpcode);
164     CheckOpcode->setNext(CheckType);
165     CheckType->setNext(Tail);
166     return ContractNodes(MatcherPtr, CGP);
167   }
168 }
169
170 /// SinkPatternPredicates - Pattern predicates can be checked at any level of
171 /// the matching tree.  The generator dumps them at the top level of the pattern
172 /// though, which prevents factoring from being able to see past them.  This
173 /// optimization sinks them as far down into the pattern as possible.
174 ///
175 /// Conceptually, we'd like to sink these predicates all the way to the last
176 /// matcher predicate in the series.  However, it turns out that some
177 /// ComplexPatterns have side effects on the graph, so we really don't want to
178 /// run a the complex pattern if the pattern predicate will fail.  For this
179 /// reason, we refuse to sink the pattern predicate past a ComplexPattern.
180 ///
181 static void SinkPatternPredicates(OwningPtr<Matcher> &MatcherPtr) {
182   // Recursively scan for a PatternPredicate.
183   // If we reached the end of the chain, we're done.
184   Matcher *N = MatcherPtr.get();
185   if (N == 0) return;
186   
187   // Walk down all members of a scope node.
188   if (ScopeMatcher *Scope = dyn_cast<ScopeMatcher>(N)) {
189     for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) {
190       OwningPtr<Matcher> Child(Scope->takeChild(i));
191       SinkPatternPredicates(Child);
192       Scope->resetChild(i, Child.take());
193     }
194     return;
195   }
196   
197   // If this node isn't a CheckPatternPredicateMatcher we keep scanning until
198   // we find one.
199   CheckPatternPredicateMatcher *CPPM =dyn_cast<CheckPatternPredicateMatcher>(N);
200   if (CPPM == 0)
201     return SinkPatternPredicates(N->getNextPtr());
202   
203   // Ok, we found one, lets try to sink it. Check if we can sink it past the
204   // next node in the chain.  If not, we won't be able to change anything and
205   // might as well bail.
206   if (!CPPM->getNext()->isSafeToReorderWithPatternPredicate())
207     return;
208   
209   // Okay, we know we can sink it past at least one node.  Unlink it from the
210   // chain and scan for the new insertion point.
211   MatcherPtr.take();  // Don't delete CPPM.
212   MatcherPtr.reset(CPPM->takeNext());
213   
214   N = MatcherPtr.get();
215   while (N->getNext()->isSafeToReorderWithPatternPredicate())
216     N = N->getNext();
217   
218   // At this point, we want to insert CPPM after N.
219   CPPM->setNext(N->takeNext());
220   N->setNext(CPPM);
221 }
222
223 /// FindCheckType - Scan a series of matchers looking for a CheckType that can
224 /// be pulled up to the start of the matcher.  Return null if we didn't find one
225 /// otherwise return the matcher.
226 static CheckTypeMatcher *FindCheckType(Matcher *M) {
227   for (; M; M = M->getNext())
228     if (CheckTypeMatcher *CTM = dyn_cast<CheckTypeMatcher>(M))
229       return CTM;
230   return 0;
231 }
232
233
234 /// FactorNodes - Turn matches like this:
235 ///   Scope
236 ///     OPC_CheckType i32
237 ///       ABC
238 ///     OPC_CheckType i32
239 ///       XYZ
240 /// into:
241 ///   OPC_CheckType i32
242 ///     Scope
243 ///       ABC
244 ///       XYZ
245 ///
246 static void FactorNodes(OwningPtr<Matcher> &MatcherPtr) {
247   // If we reached the end of the chain, we're done.
248   Matcher *N = MatcherPtr.get();
249   if (N == 0) return;
250   
251   // If this is not a push node, just scan for one.
252   ScopeMatcher *Scope = dyn_cast<ScopeMatcher>(N);
253   if (Scope == 0)
254     return FactorNodes(N->getNextPtr());
255   
256   // Okay, pull together the children of the scope node into a vector so we can
257   // inspect it more easily.  While we're at it, bucket them up by the hash
258   // code of their first predicate.
259   SmallVector<Matcher*, 32> OptionsToMatch;
260   
261   for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) {
262     // Factor the subexpression.
263     OwningPtr<Matcher> Child(Scope->takeChild(i));
264     FactorNodes(Child);
265     
266     if (Matcher *N = Child.take())
267       OptionsToMatch.push_back(N);
268   }
269   
270   SmallVector<Matcher*, 32> NewOptionsToMatch;
271   
272   // Loop over options to match, merging neighboring patterns with identical
273   // starting nodes into a shared matcher.
274   for (unsigned OptionIdx = 0, e = OptionsToMatch.size(); OptionIdx != e;) {
275     // Find the set of matchers that start with this node.
276     Matcher *Optn = OptionsToMatch[OptionIdx++];
277
278     if (OptionIdx == e) {
279       NewOptionsToMatch.push_back(Optn);
280       continue;
281     }
282     
283     // See if the next option starts with the same matcher.  If the two
284     // neighbors *do* start with the same matcher, we can factor the matcher out
285     // of at least these two patterns.  See what the maximal set we can merge
286     // together is.
287     SmallVector<Matcher*, 8> EqualMatchers;
288     EqualMatchers.push_back(Optn);
289     
290     // Factor all of the known-equal matchers after this one into the same
291     // group.
292     while (OptionIdx != e && OptionsToMatch[OptionIdx]->isEqual(Optn))
293       EqualMatchers.push_back(OptionsToMatch[OptionIdx++]);
294
295     // If we found a non-equal matcher, see if it is contradictory with the
296     // current node.  If so, we know that the ordering relation between the
297     // current sets of nodes and this node don't matter.  Look past it to see if
298     // we can merge anything else into this matching group.
299     unsigned Scan = OptionIdx;
300     while (1) {
301       // If we ran out of stuff to scan, we're done.
302       if (Scan == e) break;
303       
304       // If we found an entry that matches out matcher, merge it into the set to
305       // handle.
306       if (Optn->isEqual(OptionsToMatch[Scan])) {
307         // If is equal after all, add the option to EqualMatchers and remove it
308         // from OptionsToMatch.
309         EqualMatchers.push_back(OptionsToMatch[Scan]);
310         OptionsToMatch.erase(OptionsToMatch.begin()+Scan);
311         --e;
312         continue;
313       }
314       
315       // If the option we're checking for contradicts the start of the list,
316       // skip over it.
317       if (Optn->isContradictory(OptionsToMatch[Scan])) {
318         ++Scan;
319         continue;
320       }
321
322       // If we're scannig for a type comparison and the type comparison got
323       // moved late, see if we can pull it up.
324       if (isa<CheckTypeMatcher>(Optn)) {
325         CheckTypeMatcher *CTM = FindCheckType(OptionsToMatch[Scan]);
326         if (CTM != 0 && CTM != OptionsToMatch[Scan] &&
327             CTM->canMoveBefore(OptionsToMatch[Scan])) {
328           Matcher *MatcherWithoutCTM = OptionsToMatch[Scan]->unlinkNode(CTM);
329           CTM->setNext(MatcherWithoutCTM);
330           OptionsToMatch[Scan] = CTM;
331           continue;
332         }
333       }
334       
335       // Otherwise, we don't know how to handle this entry, we have to bail.
336       break;
337     }
338       
339     if (Scan != e &&
340         // Don't print it's obvious nothing extra could be merged anyway.
341         Scan+1 != e) {
342       DEBUG(errs() << "Couldn't merge this:\n";
343             Optn->print(errs(), 4);
344             errs() << "into this:\n";
345             OptionsToMatch[Scan]->print(errs(), 4);
346             if (Scan+1 != e)
347               OptionsToMatch[Scan+1]->printOne(errs());
348             if (Scan+2 < e)
349               OptionsToMatch[Scan+2]->printOne(errs());
350             errs() << "\n");
351     }
352     
353     // If we only found one option starting with this matcher, no factoring is
354     // possible.
355     if (EqualMatchers.size() == 1) {
356       NewOptionsToMatch.push_back(EqualMatchers[0]);
357       continue;
358     }
359     
360     // Factor these checks by pulling the first node off each entry and
361     // discarding it.  Take the first one off the first entry to reuse.
362     Matcher *Shared = Optn;
363     Optn = Optn->takeNext();
364     EqualMatchers[0] = Optn;
365
366     // Remove and delete the first node from the other matchers we're factoring.
367     for (unsigned i = 1, e = EqualMatchers.size(); i != e; ++i) {
368       Matcher *Tmp = EqualMatchers[i]->takeNext();
369       delete EqualMatchers[i];
370       EqualMatchers[i] = Tmp;
371     }
372     
373     Shared->setNext(new ScopeMatcher(&EqualMatchers[0], EqualMatchers.size()));
374
375     // Recursively factor the newly created node.
376     FactorNodes(Shared->getNextPtr());
377     
378     NewOptionsToMatch.push_back(Shared);
379   }
380   
381   // If we're down to a single pattern to match, then we don't need this scope
382   // anymore.
383   if (NewOptionsToMatch.size() == 1) {
384     MatcherPtr.reset(NewOptionsToMatch[0]);
385     return;
386   }
387   
388   if (NewOptionsToMatch.empty()) {
389     MatcherPtr.reset(0);
390     return;
391   }
392   
393   // If our factoring failed (didn't achieve anything) see if we can simplify in
394   // other ways.
395   
396   // Check to see if all of the leading entries are now opcode checks.  If so,
397   // we can convert this Scope to be a OpcodeSwitch instead.
398   bool AllOpcodeChecks = true, AllTypeChecks = true;
399   for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i) {
400     // Check to see if this breaks a series of CheckOpcodeMatchers.
401     if (AllOpcodeChecks &&
402         !isa<CheckOpcodeMatcher>(NewOptionsToMatch[i])) {
403 #if 0
404       if (i > 3) {
405         errs() << "FAILING OPC #" << i << "\n";
406         NewOptionsToMatch[i]->dump();
407       }
408 #endif
409       AllOpcodeChecks = false;
410     }
411
412     // Check to see if this breaks a series of CheckTypeMatcher's.
413     if (AllTypeChecks) {
414       CheckTypeMatcher *CTM = FindCheckType(NewOptionsToMatch[i]);
415       if (CTM == 0 || 
416           // iPTR checks could alias any other case without us knowing, don't
417           // bother with them.
418           CTM->getType() == MVT::iPTR ||
419           // If the CheckType isn't at the start of the list, see if we can move
420           // it there.
421           !CTM->canMoveBefore(NewOptionsToMatch[i])) {
422 #if 0
423         if (i > 3 && AllTypeChecks) {
424           errs() << "FAILING TYPE #" << i << "\n";
425           NewOptionsToMatch[i]->dump();
426         }
427 #endif
428         AllTypeChecks = false;
429       }
430     }
431   }
432   
433   // If all the options are CheckOpcode's, we can form the SwitchOpcode, woot.
434   if (AllOpcodeChecks) {
435     StringSet<> Opcodes;
436     SmallVector<std::pair<const SDNodeInfo*, Matcher*>, 8> Cases;
437     for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i) {
438       CheckOpcodeMatcher *COM = cast<CheckOpcodeMatcher>(NewOptionsToMatch[i]);
439       assert(Opcodes.insert(COM->getOpcode().getEnumName()) &&
440              "Duplicate opcodes not factored?");
441       Cases.push_back(std::make_pair(&COM->getOpcode(), COM->getNext()));
442     }
443     
444     MatcherPtr.reset(new SwitchOpcodeMatcher(&Cases[0], Cases.size()));
445     return;
446   }
447   
448   // If all the options are CheckType's, we can form the SwitchType, woot.
449   if (AllTypeChecks) {
450     DenseMap<unsigned, unsigned> TypeEntry;
451     SmallVector<std::pair<MVT::SimpleValueType, Matcher*>, 8> Cases;
452     for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i) {
453       CheckTypeMatcher *CTM = FindCheckType(NewOptionsToMatch[i]);
454       Matcher *MatcherWithoutCTM = NewOptionsToMatch[i]->unlinkNode(CTM);
455       MVT::SimpleValueType CTMTy = CTM->getType();
456       delete CTM;
457       
458       unsigned &Entry = TypeEntry[CTMTy];
459       if (Entry != 0) {
460         // If we have unfactored duplicate types, then we should factor them.
461         Matcher *PrevMatcher = Cases[Entry-1].second;
462         if (ScopeMatcher *SM = dyn_cast<ScopeMatcher>(PrevMatcher)) {
463           SM->setNumChildren(SM->getNumChildren()+1);
464           SM->resetChild(SM->getNumChildren()-1, MatcherWithoutCTM);
465           continue;
466         }
467         
468         Matcher *Entries[2] = { PrevMatcher, MatcherWithoutCTM };
469         Cases[Entry-1].second = new ScopeMatcher(Entries, 2);
470         continue;
471       }
472       
473       Entry = Cases.size()+1;
474       Cases.push_back(std::make_pair(CTMTy, MatcherWithoutCTM));
475     }
476     
477     if (Cases.size() != 1) {
478       MatcherPtr.reset(new SwitchTypeMatcher(&Cases[0], Cases.size()));
479     } else {
480       // If we factored and ended up with one case, create it now.
481       MatcherPtr.reset(new CheckTypeMatcher(Cases[0].first));
482       MatcherPtr->setNext(Cases[0].second);
483     }
484     return;
485   }
486   
487
488   // Reassemble the Scope node with the adjusted children.
489   Scope->setNumChildren(NewOptionsToMatch.size());
490   for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i)
491     Scope->resetChild(i, NewOptionsToMatch[i]);
492 }
493
494 Matcher *llvm::OptimizeMatcher(Matcher *TheMatcher,
495                                const CodeGenDAGPatterns &CGP) {
496   OwningPtr<Matcher> MatcherPtr(TheMatcher);
497   ContractNodes(MatcherPtr, CGP);
498   SinkPatternPredicates(MatcherPtr);
499   FactorNodes(MatcherPtr);
500   return MatcherPtr.take();
501 }