enhance RecordNode and RecordChild comments to indicate what
[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/DenseMap.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include <vector>
21 using namespace llvm;
22
23 /// ContractNodes - Turn multiple matcher node patterns like 'MoveChild+Record'
24 /// into single compound nodes like RecordChild.
25 static void ContractNodes(OwningPtr<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 == 0) return;
30   
31   // If we have a scope node, walk down all of the children.
32   if (ScopeMatcher *Scope = dyn_cast<ScopeMatcher>(N)) {
33     for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) {
34       OwningPtr<Matcher> Child(Scope->takeChild(i));
35       ContractNodes(Child, CGP);
36       Scope->resetChild(i, Child.take());
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)) {
44     Matcher *New = 0;
45     if (RecordMatcher *RM = dyn_cast<RecordMatcher>(MC->getNext()))
46       New = new RecordChildMatcher(MC->getChildNo(), RM->getWhatFor(),
47                                    RM->getResultNo());
48     
49     if (CheckTypeMatcher *CT= dyn_cast<CheckTypeMatcher>(MC->getNext()))
50       New = new CheckChildTypeMatcher(MC->getChildNo(), CT->getType());
51     
52     if (New) {
53       // Insert the new node.
54       New->setNext(MatcherPtr.take());
55       MatcherPtr.reset(New);
56       // Remove the old one.
57       MC->setNext(MC->getNext()->takeNext());
58       return ContractNodes(MatcherPtr, CGP);
59     }
60   }
61   
62   // Zap movechild -> moveparent.
63   if (MoveChildMatcher *MC = dyn_cast<MoveChildMatcher>(N))
64     if (MoveParentMatcher *MP = 
65           dyn_cast<MoveParentMatcher>(MC->getNext())) {
66       MatcherPtr.reset(MP->takeNext());
67       return ContractNodes(MatcherPtr, CGP);
68     }
69
70   // FIXME: Handle OPC_MarkFlagResults.
71
72   // Turn EmitNode->CompleteMatch into MorphNodeTo if we can.
73   if (EmitNodeMatcher *EN = dyn_cast<EmitNodeMatcher>(N))
74     if (CompleteMatchMatcher *CM =
75           dyn_cast<CompleteMatchMatcher>(EN->getNext())) {
76       // We can only use MorphNodeTo if the result values match up.
77       unsigned RootResultFirst = EN->getFirstResultSlot();
78       bool ResultsMatch = true;
79       for (unsigned i = 0, e = CM->getNumResults(); i != e; ++i)
80         if (CM->getResult(i) != RootResultFirst+i)
81           ResultsMatch = false;
82       
83       // If the selected node defines a subset of the flag/chain results, we
84       // can't use MorphNodeTo.  For example, we can't use MorphNodeTo if the
85       // matched pattern has a chain but the root node doesn't.
86       const PatternToMatch &Pattern = CM->getPattern();
87       
88       if (!EN->hasChain() &&
89           Pattern.getSrcPattern()->NodeHasProperty(SDNPHasChain, CGP))
90         ResultsMatch = false;
91
92       // If the matched node has a flag and the output root doesn't, we can't
93       // use MorphNodeTo.
94       //
95       // NOTE: Strictly speaking, we don't have to check for the flag here
96       // because the code in the pattern generator doesn't handle it right.  We
97       // do it anyway for thoroughness.
98       if (!EN->hasOutFlag() &&
99           Pattern.getSrcPattern()->NodeHasProperty(SDNPOutFlag, CGP))
100         ResultsMatch = false;
101       
102       
103       // If the root result node defines more results than the source root node
104       // *and* has a chain or flag input, then we can't match it because it
105       // would end up replacing the extra result with the chain/flag.
106 #if 0
107       if ((EN->hasFlag() || EN->hasChain()) &&
108           EN->getNumNonChainFlagVTs() > ... need to get no results reliably ...)
109         ResultMatch = false;
110 #endif
111           
112       if (ResultsMatch) {
113         const SmallVectorImpl<MVT::SimpleValueType> &VTs = EN->getVTList();
114         const SmallVectorImpl<unsigned> &Operands = EN->getOperandList();
115         MatcherPtr.reset(new MorphNodeToMatcher(EN->getOpcodeName(),
116                                                 VTs.data(), VTs.size(),
117                                                 Operands.data(),Operands.size(),
118                                                 EN->hasChain(), EN->hasInFlag(),
119                                                 EN->hasOutFlag(),
120                                                 EN->hasMemRefs(),
121                                                 EN->getNumFixedArityOperands(),
122                                                 Pattern));
123         return;
124       }
125
126       // FIXME2: Kill off all the SelectionDAG::MorphNodeTo and getMachineNode
127       // variants.
128     }
129   
130   ContractNodes(N->getNextPtr(), CGP);
131   
132   
133   // If we have a CheckType/CheckChildType/Record node followed by a
134   // CheckOpcode, invert the two nodes.  We prefer to do structural checks
135   // before type checks, as this opens opportunities for factoring on targets
136   // like X86 where many operations are valid on multiple types.
137   if ((isa<CheckTypeMatcher>(N) || isa<CheckChildTypeMatcher>(N) ||
138        isa<RecordMatcher>(N)) &&
139       isa<CheckOpcodeMatcher>(N->getNext())) {
140     // Unlink the two nodes from the list.
141     Matcher *CheckType = MatcherPtr.take();
142     Matcher *CheckOpcode = CheckType->takeNext();
143     Matcher *Tail = CheckOpcode->takeNext();
144     
145     // Relink them.
146     MatcherPtr.reset(CheckOpcode);
147     CheckOpcode->setNext(CheckType);
148     CheckType->setNext(Tail);
149     return ContractNodes(MatcherPtr, CGP);
150   }
151 }
152
153 /// SinkPatternPredicates - Pattern predicates can be checked at any level of
154 /// the matching tree.  The generator dumps them at the top level of the pattern
155 /// though, which prevents factoring from being able to see past them.  This
156 /// optimization sinks them as far down into the pattern as possible.
157 ///
158 /// Conceptually, we'd like to sink these predicates all the way to the last
159 /// matcher predicate in the series.  However, it turns out that some
160 /// ComplexPatterns have side effects on the graph, so we really don't want to
161 /// run a the complex pattern if the pattern predicate will fail.  For this
162 /// reason, we refuse to sink the pattern predicate past a ComplexPattern.
163 ///
164 static void SinkPatternPredicates(OwningPtr<Matcher> &MatcherPtr) {
165   // Recursively scan for a PatternPredicate.
166   // If we reached the end of the chain, we're done.
167   Matcher *N = MatcherPtr.get();
168   if (N == 0) return;
169   
170   // Walk down all members of a scope node.
171   if (ScopeMatcher *Scope = dyn_cast<ScopeMatcher>(N)) {
172     for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) {
173       OwningPtr<Matcher> Child(Scope->takeChild(i));
174       SinkPatternPredicates(Child);
175       Scope->resetChild(i, Child.take());
176     }
177     return;
178   }
179   
180   // If this node isn't a CheckPatternPredicateMatcher we keep scanning until
181   // we find one.
182   CheckPatternPredicateMatcher *CPPM =dyn_cast<CheckPatternPredicateMatcher>(N);
183   if (CPPM == 0)
184     return SinkPatternPredicates(N->getNextPtr());
185   
186   // Ok, we found one, lets try to sink it. Check if we can sink it past the
187   // next node in the chain.  If not, we won't be able to change anything and
188   // might as well bail.
189   if (!CPPM->getNext()->isSafeToReorderWithPatternPredicate())
190     return;
191   
192   // Okay, we know we can sink it past at least one node.  Unlink it from the
193   // chain and scan for the new insertion point.
194   MatcherPtr.take();  // Don't delete CPPM.
195   MatcherPtr.reset(CPPM->takeNext());
196   
197   N = MatcherPtr.get();
198   while (N->getNext()->isSafeToReorderWithPatternPredicate())
199     N = N->getNext();
200   
201   // At this point, we want to insert CPPM after N.
202   CPPM->setNext(N->takeNext());
203   N->setNext(CPPM);
204 }
205
206 /// FactorNodes - Turn matches like this:
207 ///   Scope
208 ///     OPC_CheckType i32
209 ///       ABC
210 ///     OPC_CheckType i32
211 ///       XYZ
212 /// into:
213 ///   OPC_CheckType i32
214 ///     Scope
215 ///       ABC
216 ///       XYZ
217 ///
218 static void FactorNodes(OwningPtr<Matcher> &MatcherPtr) {
219   // If we reached the end of the chain, we're done.
220   Matcher *N = MatcherPtr.get();
221   if (N == 0) return;
222   
223   // If this is not a push node, just scan for one.
224   ScopeMatcher *Scope = dyn_cast<ScopeMatcher>(N);
225   if (Scope == 0)
226     return FactorNodes(N->getNextPtr());
227   
228   // Okay, pull together the children of the scope node into a vector so we can
229   // inspect it more easily.  While we're at it, bucket them up by the hash
230   // code of their first predicate.
231   SmallVector<Matcher*, 32> OptionsToMatch;
232   
233   for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) {
234     // Factor the subexpression.
235     OwningPtr<Matcher> Child(Scope->takeChild(i));
236     FactorNodes(Child);
237     
238     if (Matcher *N = Child.take())
239       OptionsToMatch.push_back(N);
240   }
241   
242   SmallVector<Matcher*, 32> NewOptionsToMatch;
243
244   // Loop over options to match, merging neighboring patterns with identical
245   // starting nodes into a shared matcher.
246   for (unsigned OptionIdx = 0, e = OptionsToMatch.size(); OptionIdx != e;) {
247     // Find the set of matchers that start with this node.
248     Matcher *Optn = OptionsToMatch[OptionIdx++];
249
250     if (OptionIdx == e) {
251       NewOptionsToMatch.push_back(Optn);
252       continue;
253     }
254     
255     // See if the next option starts with the same matcher.  If the two
256     // neighbors *do* start with the same matcher, we can factor the matcher out
257     // of at least these two patterns.  See what the maximal set we can merge
258     // together is.
259     SmallVector<Matcher*, 8> EqualMatchers;
260     EqualMatchers.push_back(Optn);
261     
262     // Factor all of the known-equal matchers after this one into the same
263     // group.
264     while (OptionIdx != e && OptionsToMatch[OptionIdx]->isEqual(Optn))
265       EqualMatchers.push_back(OptionsToMatch[OptionIdx++]);
266
267     // If we found a non-equal matcher, see if it is contradictory with the
268     // current node.  If so, we know that the ordering relation between the
269     // current sets of nodes and this node don't matter.  Look past it to see if
270     // we can merge anything else into this matching group.
271     unsigned Scan = OptionIdx;
272     while (1) {
273       while (Scan != e && Optn->isContradictory(OptionsToMatch[Scan]))
274         ++Scan;
275       
276       // Ok, we found something that isn't known to be contradictory.  If it is
277       // equal, we can merge it into the set of nodes to factor, if not, we have
278       // to cease factoring.
279       if (Scan == e || !Optn->isEqual(OptionsToMatch[Scan])) break;
280
281       // If is equal after all, add the option to EqualMatchers and remove it
282       // from OptionsToMatch.
283       EqualMatchers.push_back(OptionsToMatch[Scan]);
284       OptionsToMatch.erase(OptionsToMatch.begin()+Scan);
285       --e;
286     }
287       
288     if (Scan != e &&
289         // Don't print it's obvious nothing extra could be merged anyway.
290         Scan+1 != e) {
291       DEBUG(errs() << "Couldn't merge this:\n";
292             Optn->print(errs(), 4);
293             errs() << "into this:\n";
294             OptionsToMatch[Scan]->print(errs(), 4);
295             if (Scan+1 != e)
296               OptionsToMatch[Scan+1]->printOne(errs());
297             if (Scan+2 < e)
298               OptionsToMatch[Scan+2]->printOne(errs());
299             errs() << "\n");
300     }
301     
302     // If we only found one option starting with this matcher, no factoring is
303     // possible.
304     if (EqualMatchers.size() == 1) {
305       NewOptionsToMatch.push_back(EqualMatchers[0]);
306       continue;
307     }
308     
309     // Factor these checks by pulling the first node off each entry and
310     // discarding it.  Take the first one off the first entry to reuse.
311     Matcher *Shared = Optn;
312     Optn = Optn->takeNext();
313     EqualMatchers[0] = Optn;
314
315     // Remove and delete the first node from the other matchers we're factoring.
316     for (unsigned i = 1, e = EqualMatchers.size(); i != e; ++i) {
317       Matcher *Tmp = EqualMatchers[i]->takeNext();
318       delete EqualMatchers[i];
319       EqualMatchers[i] = Tmp;
320     }
321     
322     Shared->setNext(new ScopeMatcher(&EqualMatchers[0], EqualMatchers.size()));
323
324     // Recursively factor the newly created node.
325     FactorNodes(Shared->getNextPtr());
326     
327     NewOptionsToMatch.push_back(Shared);
328   }
329
330   // Reassemble a new Scope node.
331   assert(!NewOptionsToMatch.empty() && "where'd all our children go?");
332   if (NewOptionsToMatch.empty())
333     MatcherPtr.reset(0);
334   if (NewOptionsToMatch.size() == 1)
335     MatcherPtr.reset(NewOptionsToMatch[0]);
336   else {
337     Scope->setNumChildren(NewOptionsToMatch.size());
338     for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i)
339       Scope->resetChild(i, NewOptionsToMatch[i]);
340   }
341 }
342
343 Matcher *llvm::OptimizeMatcher(Matcher *TheMatcher,
344                                const CodeGenDAGPatterns &CGP) {
345   OwningPtr<Matcher> MatcherPtr(TheMatcher);
346   ContractNodes(MatcherPtr, CGP);
347   SinkPatternPredicates(MatcherPtr);
348   FactorNodes(MatcherPtr);
349   return MatcherPtr.take();
350 }