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