xref: /aosp_15_r20/external/llvm/utils/TableGen/DAGISelMatcherGen.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===- DAGISelMatcherGen.cpp - Matcher generator --------------------------===//
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 #include "DAGISelMatcher.h"
11*9880d681SAndroid Build Coastguard Worker #include "CodeGenDAGPatterns.h"
12*9880d681SAndroid Build Coastguard Worker #include "CodeGenRegisters.h"
13*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallVector.h"
14*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/StringMap.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm/TableGen/Error.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/TableGen/Record.h"
17*9880d681SAndroid Build Coastguard Worker #include <utility>
18*9880d681SAndroid Build Coastguard Worker using namespace llvm;
19*9880d681SAndroid Build Coastguard Worker 
20*9880d681SAndroid Build Coastguard Worker 
21*9880d681SAndroid Build Coastguard Worker /// getRegisterValueType - Look up and return the ValueType of the specified
22*9880d681SAndroid Build Coastguard Worker /// register. If the register is a member of multiple register classes which
23*9880d681SAndroid Build Coastguard Worker /// have different associated types, return MVT::Other.
getRegisterValueType(Record * R,const CodeGenTarget & T)24*9880d681SAndroid Build Coastguard Worker static MVT::SimpleValueType getRegisterValueType(Record *R,
25*9880d681SAndroid Build Coastguard Worker                                                  const CodeGenTarget &T) {
26*9880d681SAndroid Build Coastguard Worker   bool FoundRC = false;
27*9880d681SAndroid Build Coastguard Worker   MVT::SimpleValueType VT = MVT::Other;
28*9880d681SAndroid Build Coastguard Worker   const CodeGenRegister *Reg = T.getRegBank().getReg(R);
29*9880d681SAndroid Build Coastguard Worker 
30*9880d681SAndroid Build Coastguard Worker   for (const auto &RC : T.getRegBank().getRegClasses()) {
31*9880d681SAndroid Build Coastguard Worker     if (!RC.contains(Reg))
32*9880d681SAndroid Build Coastguard Worker       continue;
33*9880d681SAndroid Build Coastguard Worker 
34*9880d681SAndroid Build Coastguard Worker     if (!FoundRC) {
35*9880d681SAndroid Build Coastguard Worker       FoundRC = true;
36*9880d681SAndroid Build Coastguard Worker       VT = RC.getValueTypeNum(0);
37*9880d681SAndroid Build Coastguard Worker       continue;
38*9880d681SAndroid Build Coastguard Worker     }
39*9880d681SAndroid Build Coastguard Worker 
40*9880d681SAndroid Build Coastguard Worker     // If this occurs in multiple register classes, they all have to agree.
41*9880d681SAndroid Build Coastguard Worker     assert(VT == RC.getValueTypeNum(0));
42*9880d681SAndroid Build Coastguard Worker   }
43*9880d681SAndroid Build Coastguard Worker   return VT;
44*9880d681SAndroid Build Coastguard Worker }
45*9880d681SAndroid Build Coastguard Worker 
46*9880d681SAndroid Build Coastguard Worker 
47*9880d681SAndroid Build Coastguard Worker namespace {
48*9880d681SAndroid Build Coastguard Worker   class MatcherGen {
49*9880d681SAndroid Build Coastguard Worker     const PatternToMatch &Pattern;
50*9880d681SAndroid Build Coastguard Worker     const CodeGenDAGPatterns &CGP;
51*9880d681SAndroid Build Coastguard Worker 
52*9880d681SAndroid Build Coastguard Worker     /// PatWithNoTypes - This is a clone of Pattern.getSrcPattern() that starts
53*9880d681SAndroid Build Coastguard Worker     /// out with all of the types removed.  This allows us to insert type checks
54*9880d681SAndroid Build Coastguard Worker     /// as we scan the tree.
55*9880d681SAndroid Build Coastguard Worker     TreePatternNode *PatWithNoTypes;
56*9880d681SAndroid Build Coastguard Worker 
57*9880d681SAndroid Build Coastguard Worker     /// VariableMap - A map from variable names ('$dst') to the recorded operand
58*9880d681SAndroid Build Coastguard Worker     /// number that they were captured as.  These are biased by 1 to make
59*9880d681SAndroid Build Coastguard Worker     /// insertion easier.
60*9880d681SAndroid Build Coastguard Worker     StringMap<unsigned> VariableMap;
61*9880d681SAndroid Build Coastguard Worker 
62*9880d681SAndroid Build Coastguard Worker     /// This maintains the recorded operand number that OPC_CheckComplexPattern
63*9880d681SAndroid Build Coastguard Worker     /// drops each sub-operand into. We don't want to insert these into
64*9880d681SAndroid Build Coastguard Worker     /// VariableMap because that leads to identity checking if they are
65*9880d681SAndroid Build Coastguard Worker     /// encountered multiple times. Biased by 1 like VariableMap for
66*9880d681SAndroid Build Coastguard Worker     /// consistency.
67*9880d681SAndroid Build Coastguard Worker     StringMap<unsigned> NamedComplexPatternOperands;
68*9880d681SAndroid Build Coastguard Worker 
69*9880d681SAndroid Build Coastguard Worker     /// NextRecordedOperandNo - As we emit opcodes to record matched values in
70*9880d681SAndroid Build Coastguard Worker     /// the RecordedNodes array, this keeps track of which slot will be next to
71*9880d681SAndroid Build Coastguard Worker     /// record into.
72*9880d681SAndroid Build Coastguard Worker     unsigned NextRecordedOperandNo;
73*9880d681SAndroid Build Coastguard Worker 
74*9880d681SAndroid Build Coastguard Worker     /// MatchedChainNodes - This maintains the position in the recorded nodes
75*9880d681SAndroid Build Coastguard Worker     /// array of all of the recorded input nodes that have chains.
76*9880d681SAndroid Build Coastguard Worker     SmallVector<unsigned, 2> MatchedChainNodes;
77*9880d681SAndroid Build Coastguard Worker 
78*9880d681SAndroid Build Coastguard Worker     /// MatchedComplexPatterns - This maintains a list of all of the
79*9880d681SAndroid Build Coastguard Worker     /// ComplexPatterns that we need to check. The second element of each pair
80*9880d681SAndroid Build Coastguard Worker     /// is the recorded operand number of the input node.
81*9880d681SAndroid Build Coastguard Worker     SmallVector<std::pair<const TreePatternNode*,
82*9880d681SAndroid Build Coastguard Worker                           unsigned>, 2> MatchedComplexPatterns;
83*9880d681SAndroid Build Coastguard Worker 
84*9880d681SAndroid Build Coastguard Worker     /// PhysRegInputs - List list has an entry for each explicitly specified
85*9880d681SAndroid Build Coastguard Worker     /// physreg input to the pattern.  The first elt is the Register node, the
86*9880d681SAndroid Build Coastguard Worker     /// second is the recorded slot number the input pattern match saved it in.
87*9880d681SAndroid Build Coastguard Worker     SmallVector<std::pair<Record*, unsigned>, 2> PhysRegInputs;
88*9880d681SAndroid Build Coastguard Worker 
89*9880d681SAndroid Build Coastguard Worker     /// Matcher - This is the top level of the generated matcher, the result.
90*9880d681SAndroid Build Coastguard Worker     Matcher *TheMatcher;
91*9880d681SAndroid Build Coastguard Worker 
92*9880d681SAndroid Build Coastguard Worker     /// CurPredicate - As we emit matcher nodes, this points to the latest check
93*9880d681SAndroid Build Coastguard Worker     /// which should have future checks stuck into its Next position.
94*9880d681SAndroid Build Coastguard Worker     Matcher *CurPredicate;
95*9880d681SAndroid Build Coastguard Worker   public:
96*9880d681SAndroid Build Coastguard Worker     MatcherGen(const PatternToMatch &pattern, const CodeGenDAGPatterns &cgp);
97*9880d681SAndroid Build Coastguard Worker 
~MatcherGen()98*9880d681SAndroid Build Coastguard Worker     ~MatcherGen() {
99*9880d681SAndroid Build Coastguard Worker       delete PatWithNoTypes;
100*9880d681SAndroid Build Coastguard Worker     }
101*9880d681SAndroid Build Coastguard Worker 
102*9880d681SAndroid Build Coastguard Worker     bool EmitMatcherCode(unsigned Variant);
103*9880d681SAndroid Build Coastguard Worker     void EmitResultCode();
104*9880d681SAndroid Build Coastguard Worker 
GetMatcher() const105*9880d681SAndroid Build Coastguard Worker     Matcher *GetMatcher() const { return TheMatcher; }
106*9880d681SAndroid Build Coastguard Worker   private:
107*9880d681SAndroid Build Coastguard Worker     void AddMatcher(Matcher *NewNode);
108*9880d681SAndroid Build Coastguard Worker     void InferPossibleTypes();
109*9880d681SAndroid Build Coastguard Worker 
110*9880d681SAndroid Build Coastguard Worker     // Matcher Generation.
111*9880d681SAndroid Build Coastguard Worker     void EmitMatchCode(const TreePatternNode *N, TreePatternNode *NodeNoTypes);
112*9880d681SAndroid Build Coastguard Worker     void EmitLeafMatchCode(const TreePatternNode *N);
113*9880d681SAndroid Build Coastguard Worker     void EmitOperatorMatchCode(const TreePatternNode *N,
114*9880d681SAndroid Build Coastguard Worker                                TreePatternNode *NodeNoTypes);
115*9880d681SAndroid Build Coastguard Worker 
116*9880d681SAndroid Build Coastguard Worker     /// If this is the first time a node with unique identifier Name has been
117*9880d681SAndroid Build Coastguard Worker     /// seen, record it. Otherwise, emit a check to make sure this is the same
118*9880d681SAndroid Build Coastguard Worker     /// node. Returns true if this is the first encounter.
119*9880d681SAndroid Build Coastguard Worker     bool recordUniqueNode(const std::string &Name);
120*9880d681SAndroid Build Coastguard Worker 
121*9880d681SAndroid Build Coastguard Worker     // Result Code Generation.
getNamedArgumentSlot(StringRef Name)122*9880d681SAndroid Build Coastguard Worker     unsigned getNamedArgumentSlot(StringRef Name) {
123*9880d681SAndroid Build Coastguard Worker       unsigned VarMapEntry = VariableMap[Name];
124*9880d681SAndroid Build Coastguard Worker       assert(VarMapEntry != 0 &&
125*9880d681SAndroid Build Coastguard Worker              "Variable referenced but not defined and not caught earlier!");
126*9880d681SAndroid Build Coastguard Worker       return VarMapEntry-1;
127*9880d681SAndroid Build Coastguard Worker     }
128*9880d681SAndroid Build Coastguard Worker 
129*9880d681SAndroid Build Coastguard Worker     /// GetInstPatternNode - Get the pattern for an instruction.
130*9880d681SAndroid Build Coastguard Worker     const TreePatternNode *GetInstPatternNode(const DAGInstruction &Ins,
131*9880d681SAndroid Build Coastguard Worker                                               const TreePatternNode *N);
132*9880d681SAndroid Build Coastguard Worker 
133*9880d681SAndroid Build Coastguard Worker     void EmitResultOperand(const TreePatternNode *N,
134*9880d681SAndroid Build Coastguard Worker                            SmallVectorImpl<unsigned> &ResultOps);
135*9880d681SAndroid Build Coastguard Worker     void EmitResultOfNamedOperand(const TreePatternNode *N,
136*9880d681SAndroid Build Coastguard Worker                                   SmallVectorImpl<unsigned> &ResultOps);
137*9880d681SAndroid Build Coastguard Worker     void EmitResultLeafAsOperand(const TreePatternNode *N,
138*9880d681SAndroid Build Coastguard Worker                                  SmallVectorImpl<unsigned> &ResultOps);
139*9880d681SAndroid Build Coastguard Worker     void EmitResultInstructionAsOperand(const TreePatternNode *N,
140*9880d681SAndroid Build Coastguard Worker                                         SmallVectorImpl<unsigned> &ResultOps);
141*9880d681SAndroid Build Coastguard Worker     void EmitResultSDNodeXFormAsOperand(const TreePatternNode *N,
142*9880d681SAndroid Build Coastguard Worker                                         SmallVectorImpl<unsigned> &ResultOps);
143*9880d681SAndroid Build Coastguard Worker     };
144*9880d681SAndroid Build Coastguard Worker 
145*9880d681SAndroid Build Coastguard Worker } // end anon namespace.
146*9880d681SAndroid Build Coastguard Worker 
MatcherGen(const PatternToMatch & pattern,const CodeGenDAGPatterns & cgp)147*9880d681SAndroid Build Coastguard Worker MatcherGen::MatcherGen(const PatternToMatch &pattern,
148*9880d681SAndroid Build Coastguard Worker                        const CodeGenDAGPatterns &cgp)
149*9880d681SAndroid Build Coastguard Worker : Pattern(pattern), CGP(cgp), NextRecordedOperandNo(0),
150*9880d681SAndroid Build Coastguard Worker   TheMatcher(nullptr), CurPredicate(nullptr) {
151*9880d681SAndroid Build Coastguard Worker   // We need to produce the matcher tree for the patterns source pattern.  To do
152*9880d681SAndroid Build Coastguard Worker   // this we need to match the structure as well as the types.  To do the type
153*9880d681SAndroid Build Coastguard Worker   // matching, we want to figure out the fewest number of type checks we need to
154*9880d681SAndroid Build Coastguard Worker   // emit.  For example, if there is only one integer type supported by a
155*9880d681SAndroid Build Coastguard Worker   // target, there should be no type comparisons at all for integer patterns!
156*9880d681SAndroid Build Coastguard Worker   //
157*9880d681SAndroid Build Coastguard Worker   // To figure out the fewest number of type checks needed, clone the pattern,
158*9880d681SAndroid Build Coastguard Worker   // remove the types, then perform type inference on the pattern as a whole.
159*9880d681SAndroid Build Coastguard Worker   // If there are unresolved types, emit an explicit check for those types,
160*9880d681SAndroid Build Coastguard Worker   // apply the type to the tree, then rerun type inference.  Iterate until all
161*9880d681SAndroid Build Coastguard Worker   // types are resolved.
162*9880d681SAndroid Build Coastguard Worker   //
163*9880d681SAndroid Build Coastguard Worker   PatWithNoTypes = Pattern.getSrcPattern()->clone();
164*9880d681SAndroid Build Coastguard Worker   PatWithNoTypes->RemoveAllTypes();
165*9880d681SAndroid Build Coastguard Worker 
166*9880d681SAndroid Build Coastguard Worker   // If there are types that are manifestly known, infer them.
167*9880d681SAndroid Build Coastguard Worker   InferPossibleTypes();
168*9880d681SAndroid Build Coastguard Worker }
169*9880d681SAndroid Build Coastguard Worker 
170*9880d681SAndroid Build Coastguard Worker /// InferPossibleTypes - As we emit the pattern, we end up generating type
171*9880d681SAndroid Build Coastguard Worker /// checks and applying them to the 'PatWithNoTypes' tree.  As we do this, we
172*9880d681SAndroid Build Coastguard Worker /// want to propagate implied types as far throughout the tree as possible so
173*9880d681SAndroid Build Coastguard Worker /// that we avoid doing redundant type checks.  This does the type propagation.
InferPossibleTypes()174*9880d681SAndroid Build Coastguard Worker void MatcherGen::InferPossibleTypes() {
175*9880d681SAndroid Build Coastguard Worker   // TP - Get *SOME* tree pattern, we don't care which.  It is only used for
176*9880d681SAndroid Build Coastguard Worker   // diagnostics, which we know are impossible at this point.
177*9880d681SAndroid Build Coastguard Worker   TreePattern &TP = *CGP.pf_begin()->second;
178*9880d681SAndroid Build Coastguard Worker 
179*9880d681SAndroid Build Coastguard Worker   bool MadeChange = true;
180*9880d681SAndroid Build Coastguard Worker   while (MadeChange)
181*9880d681SAndroid Build Coastguard Worker     MadeChange = PatWithNoTypes->ApplyTypeConstraints(TP,
182*9880d681SAndroid Build Coastguard Worker                                               true/*Ignore reg constraints*/);
183*9880d681SAndroid Build Coastguard Worker }
184*9880d681SAndroid Build Coastguard Worker 
185*9880d681SAndroid Build Coastguard Worker 
186*9880d681SAndroid Build Coastguard Worker /// AddMatcher - Add a matcher node to the current graph we're building.
AddMatcher(Matcher * NewNode)187*9880d681SAndroid Build Coastguard Worker void MatcherGen::AddMatcher(Matcher *NewNode) {
188*9880d681SAndroid Build Coastguard Worker   if (CurPredicate)
189*9880d681SAndroid Build Coastguard Worker     CurPredicate->setNext(NewNode);
190*9880d681SAndroid Build Coastguard Worker   else
191*9880d681SAndroid Build Coastguard Worker     TheMatcher = NewNode;
192*9880d681SAndroid Build Coastguard Worker   CurPredicate = NewNode;
193*9880d681SAndroid Build Coastguard Worker }
194*9880d681SAndroid Build Coastguard Worker 
195*9880d681SAndroid Build Coastguard Worker 
196*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
197*9880d681SAndroid Build Coastguard Worker // Pattern Match Generation
198*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
199*9880d681SAndroid Build Coastguard Worker 
200*9880d681SAndroid Build Coastguard Worker /// EmitLeafMatchCode - Generate matching code for leaf nodes.
EmitLeafMatchCode(const TreePatternNode * N)201*9880d681SAndroid Build Coastguard Worker void MatcherGen::EmitLeafMatchCode(const TreePatternNode *N) {
202*9880d681SAndroid Build Coastguard Worker   assert(N->isLeaf() && "Not a leaf?");
203*9880d681SAndroid Build Coastguard Worker 
204*9880d681SAndroid Build Coastguard Worker   // Direct match against an integer constant.
205*9880d681SAndroid Build Coastguard Worker   if (IntInit *II = dyn_cast<IntInit>(N->getLeafValue())) {
206*9880d681SAndroid Build Coastguard Worker     // If this is the root of the dag we're matching, we emit a redundant opcode
207*9880d681SAndroid Build Coastguard Worker     // check to ensure that this gets folded into the normal top-level
208*9880d681SAndroid Build Coastguard Worker     // OpcodeSwitch.
209*9880d681SAndroid Build Coastguard Worker     if (N == Pattern.getSrcPattern()) {
210*9880d681SAndroid Build Coastguard Worker       const SDNodeInfo &NI = CGP.getSDNodeInfo(CGP.getSDNodeNamed("imm"));
211*9880d681SAndroid Build Coastguard Worker       AddMatcher(new CheckOpcodeMatcher(NI));
212*9880d681SAndroid Build Coastguard Worker     }
213*9880d681SAndroid Build Coastguard Worker 
214*9880d681SAndroid Build Coastguard Worker     return AddMatcher(new CheckIntegerMatcher(II->getValue()));
215*9880d681SAndroid Build Coastguard Worker   }
216*9880d681SAndroid Build Coastguard Worker 
217*9880d681SAndroid Build Coastguard Worker   // An UnsetInit represents a named node without any constraints.
218*9880d681SAndroid Build Coastguard Worker   if (isa<UnsetInit>(N->getLeafValue())) {
219*9880d681SAndroid Build Coastguard Worker     assert(N->hasName() && "Unnamed ? leaf");
220*9880d681SAndroid Build Coastguard Worker     return;
221*9880d681SAndroid Build Coastguard Worker   }
222*9880d681SAndroid Build Coastguard Worker 
223*9880d681SAndroid Build Coastguard Worker   DefInit *DI = dyn_cast<DefInit>(N->getLeafValue());
224*9880d681SAndroid Build Coastguard Worker   if (!DI) {
225*9880d681SAndroid Build Coastguard Worker     errs() << "Unknown leaf kind: " << *N << "\n";
226*9880d681SAndroid Build Coastguard Worker     abort();
227*9880d681SAndroid Build Coastguard Worker   }
228*9880d681SAndroid Build Coastguard Worker 
229*9880d681SAndroid Build Coastguard Worker   Record *LeafRec = DI->getDef();
230*9880d681SAndroid Build Coastguard Worker 
231*9880d681SAndroid Build Coastguard Worker   // A ValueType leaf node can represent a register when named, or itself when
232*9880d681SAndroid Build Coastguard Worker   // unnamed.
233*9880d681SAndroid Build Coastguard Worker   if (LeafRec->isSubClassOf("ValueType")) {
234*9880d681SAndroid Build Coastguard Worker     // A named ValueType leaf always matches: (add i32:$a, i32:$b).
235*9880d681SAndroid Build Coastguard Worker     if (N->hasName())
236*9880d681SAndroid Build Coastguard Worker       return;
237*9880d681SAndroid Build Coastguard Worker     // An unnamed ValueType as in (sext_inreg GPR:$foo, i8).
238*9880d681SAndroid Build Coastguard Worker     return AddMatcher(new CheckValueTypeMatcher(LeafRec->getName()));
239*9880d681SAndroid Build Coastguard Worker   }
240*9880d681SAndroid Build Coastguard Worker 
241*9880d681SAndroid Build Coastguard Worker   if (// Handle register references.  Nothing to do here, they always match.
242*9880d681SAndroid Build Coastguard Worker       LeafRec->isSubClassOf("RegisterClass") ||
243*9880d681SAndroid Build Coastguard Worker       LeafRec->isSubClassOf("RegisterOperand") ||
244*9880d681SAndroid Build Coastguard Worker       LeafRec->isSubClassOf("PointerLikeRegClass") ||
245*9880d681SAndroid Build Coastguard Worker       LeafRec->isSubClassOf("SubRegIndex") ||
246*9880d681SAndroid Build Coastguard Worker       // Place holder for SRCVALUE nodes. Nothing to do here.
247*9880d681SAndroid Build Coastguard Worker       LeafRec->getName() == "srcvalue")
248*9880d681SAndroid Build Coastguard Worker     return;
249*9880d681SAndroid Build Coastguard Worker 
250*9880d681SAndroid Build Coastguard Worker   // If we have a physreg reference like (mul gpr:$src, EAX) then we need to
251*9880d681SAndroid Build Coastguard Worker   // record the register
252*9880d681SAndroid Build Coastguard Worker   if (LeafRec->isSubClassOf("Register")) {
253*9880d681SAndroid Build Coastguard Worker     AddMatcher(new RecordMatcher("physreg input "+LeafRec->getName(),
254*9880d681SAndroid Build Coastguard Worker                                  NextRecordedOperandNo));
255*9880d681SAndroid Build Coastguard Worker     PhysRegInputs.push_back(std::make_pair(LeafRec, NextRecordedOperandNo++));
256*9880d681SAndroid Build Coastguard Worker     return;
257*9880d681SAndroid Build Coastguard Worker   }
258*9880d681SAndroid Build Coastguard Worker 
259*9880d681SAndroid Build Coastguard Worker   if (LeafRec->isSubClassOf("CondCode"))
260*9880d681SAndroid Build Coastguard Worker     return AddMatcher(new CheckCondCodeMatcher(LeafRec->getName()));
261*9880d681SAndroid Build Coastguard Worker 
262*9880d681SAndroid Build Coastguard Worker   if (LeafRec->isSubClassOf("ComplexPattern")) {
263*9880d681SAndroid Build Coastguard Worker     // We can't model ComplexPattern uses that don't have their name taken yet.
264*9880d681SAndroid Build Coastguard Worker     // The OPC_CheckComplexPattern operation implicitly records the results.
265*9880d681SAndroid Build Coastguard Worker     if (N->getName().empty()) {
266*9880d681SAndroid Build Coastguard Worker       std::string S;
267*9880d681SAndroid Build Coastguard Worker       raw_string_ostream OS(S);
268*9880d681SAndroid Build Coastguard Worker       OS << "We expect complex pattern uses to have names: " << *N;
269*9880d681SAndroid Build Coastguard Worker       PrintFatalError(OS.str());
270*9880d681SAndroid Build Coastguard Worker     }
271*9880d681SAndroid Build Coastguard Worker 
272*9880d681SAndroid Build Coastguard Worker     // Remember this ComplexPattern so that we can emit it after all the other
273*9880d681SAndroid Build Coastguard Worker     // structural matches are done.
274*9880d681SAndroid Build Coastguard Worker     unsigned InputOperand = VariableMap[N->getName()] - 1;
275*9880d681SAndroid Build Coastguard Worker     MatchedComplexPatterns.push_back(std::make_pair(N, InputOperand));
276*9880d681SAndroid Build Coastguard Worker     return;
277*9880d681SAndroid Build Coastguard Worker   }
278*9880d681SAndroid Build Coastguard Worker 
279*9880d681SAndroid Build Coastguard Worker   errs() << "Unknown leaf kind: " << *N << "\n";
280*9880d681SAndroid Build Coastguard Worker   abort();
281*9880d681SAndroid Build Coastguard Worker }
282*9880d681SAndroid Build Coastguard Worker 
EmitOperatorMatchCode(const TreePatternNode * N,TreePatternNode * NodeNoTypes)283*9880d681SAndroid Build Coastguard Worker void MatcherGen::EmitOperatorMatchCode(const TreePatternNode *N,
284*9880d681SAndroid Build Coastguard Worker                                        TreePatternNode *NodeNoTypes) {
285*9880d681SAndroid Build Coastguard Worker   assert(!N->isLeaf() && "Not an operator?");
286*9880d681SAndroid Build Coastguard Worker 
287*9880d681SAndroid Build Coastguard Worker   if (N->getOperator()->isSubClassOf("ComplexPattern")) {
288*9880d681SAndroid Build Coastguard Worker     // The "name" of a non-leaf complex pattern (MY_PAT $op1, $op2) is
289*9880d681SAndroid Build Coastguard Worker     // "MY_PAT:op1:op2". We should already have validated that the uses are
290*9880d681SAndroid Build Coastguard Worker     // consistent.
291*9880d681SAndroid Build Coastguard Worker     std::string PatternName = N->getOperator()->getName();
292*9880d681SAndroid Build Coastguard Worker     for (unsigned i = 0; i < N->getNumChildren(); ++i) {
293*9880d681SAndroid Build Coastguard Worker       PatternName += ":";
294*9880d681SAndroid Build Coastguard Worker       PatternName += N->getChild(i)->getName();
295*9880d681SAndroid Build Coastguard Worker     }
296*9880d681SAndroid Build Coastguard Worker 
297*9880d681SAndroid Build Coastguard Worker     if (recordUniqueNode(PatternName)) {
298*9880d681SAndroid Build Coastguard Worker       auto NodeAndOpNum = std::make_pair(N, NextRecordedOperandNo - 1);
299*9880d681SAndroid Build Coastguard Worker       MatchedComplexPatterns.push_back(NodeAndOpNum);
300*9880d681SAndroid Build Coastguard Worker     }
301*9880d681SAndroid Build Coastguard Worker 
302*9880d681SAndroid Build Coastguard Worker     return;
303*9880d681SAndroid Build Coastguard Worker   }
304*9880d681SAndroid Build Coastguard Worker 
305*9880d681SAndroid Build Coastguard Worker   const SDNodeInfo &CInfo = CGP.getSDNodeInfo(N->getOperator());
306*9880d681SAndroid Build Coastguard Worker 
307*9880d681SAndroid Build Coastguard Worker   // If this is an 'and R, 1234' where the operation is AND/OR and the RHS is
308*9880d681SAndroid Build Coastguard Worker   // a constant without a predicate fn that has more that one bit set, handle
309*9880d681SAndroid Build Coastguard Worker   // this as a special case.  This is usually for targets that have special
310*9880d681SAndroid Build Coastguard Worker   // handling of certain large constants (e.g. alpha with it's 8/16/32-bit
311*9880d681SAndroid Build Coastguard Worker   // handling stuff).  Using these instructions is often far more efficient
312*9880d681SAndroid Build Coastguard Worker   // than materializing the constant.  Unfortunately, both the instcombiner
313*9880d681SAndroid Build Coastguard Worker   // and the dag combiner can often infer that bits are dead, and thus drop
314*9880d681SAndroid Build Coastguard Worker   // them from the mask in the dag.  For example, it might turn 'AND X, 255'
315*9880d681SAndroid Build Coastguard Worker   // into 'AND X, 254' if it knows the low bit is set.  Emit code that checks
316*9880d681SAndroid Build Coastguard Worker   // to handle this.
317*9880d681SAndroid Build Coastguard Worker   if ((N->getOperator()->getName() == "and" ||
318*9880d681SAndroid Build Coastguard Worker        N->getOperator()->getName() == "or") &&
319*9880d681SAndroid Build Coastguard Worker       N->getChild(1)->isLeaf() && N->getChild(1)->getPredicateFns().empty() &&
320*9880d681SAndroid Build Coastguard Worker       N->getPredicateFns().empty()) {
321*9880d681SAndroid Build Coastguard Worker     if (IntInit *II = dyn_cast<IntInit>(N->getChild(1)->getLeafValue())) {
322*9880d681SAndroid Build Coastguard Worker       if (!isPowerOf2_32(II->getValue())) {  // Don't bother with single bits.
323*9880d681SAndroid Build Coastguard Worker         // If this is at the root of the pattern, we emit a redundant
324*9880d681SAndroid Build Coastguard Worker         // CheckOpcode so that the following checks get factored properly under
325*9880d681SAndroid Build Coastguard Worker         // a single opcode check.
326*9880d681SAndroid Build Coastguard Worker         if (N == Pattern.getSrcPattern())
327*9880d681SAndroid Build Coastguard Worker           AddMatcher(new CheckOpcodeMatcher(CInfo));
328*9880d681SAndroid Build Coastguard Worker 
329*9880d681SAndroid Build Coastguard Worker         // Emit the CheckAndImm/CheckOrImm node.
330*9880d681SAndroid Build Coastguard Worker         if (N->getOperator()->getName() == "and")
331*9880d681SAndroid Build Coastguard Worker           AddMatcher(new CheckAndImmMatcher(II->getValue()));
332*9880d681SAndroid Build Coastguard Worker         else
333*9880d681SAndroid Build Coastguard Worker           AddMatcher(new CheckOrImmMatcher(II->getValue()));
334*9880d681SAndroid Build Coastguard Worker 
335*9880d681SAndroid Build Coastguard Worker         // Match the LHS of the AND as appropriate.
336*9880d681SAndroid Build Coastguard Worker         AddMatcher(new MoveChildMatcher(0));
337*9880d681SAndroid Build Coastguard Worker         EmitMatchCode(N->getChild(0), NodeNoTypes->getChild(0));
338*9880d681SAndroid Build Coastguard Worker         AddMatcher(new MoveParentMatcher());
339*9880d681SAndroid Build Coastguard Worker         return;
340*9880d681SAndroid Build Coastguard Worker       }
341*9880d681SAndroid Build Coastguard Worker     }
342*9880d681SAndroid Build Coastguard Worker   }
343*9880d681SAndroid Build Coastguard Worker 
344*9880d681SAndroid Build Coastguard Worker   // Check that the current opcode lines up.
345*9880d681SAndroid Build Coastguard Worker   AddMatcher(new CheckOpcodeMatcher(CInfo));
346*9880d681SAndroid Build Coastguard Worker 
347*9880d681SAndroid Build Coastguard Worker   // If this node has memory references (i.e. is a load or store), tell the
348*9880d681SAndroid Build Coastguard Worker   // interpreter to capture them in the memref array.
349*9880d681SAndroid Build Coastguard Worker   if (N->NodeHasProperty(SDNPMemOperand, CGP))
350*9880d681SAndroid Build Coastguard Worker     AddMatcher(new RecordMemRefMatcher());
351*9880d681SAndroid Build Coastguard Worker 
352*9880d681SAndroid Build Coastguard Worker   // If this node has a chain, then the chain is operand #0 is the SDNode, and
353*9880d681SAndroid Build Coastguard Worker   // the child numbers of the node are all offset by one.
354*9880d681SAndroid Build Coastguard Worker   unsigned OpNo = 0;
355*9880d681SAndroid Build Coastguard Worker   if (N->NodeHasProperty(SDNPHasChain, CGP)) {
356*9880d681SAndroid Build Coastguard Worker     // Record the node and remember it in our chained nodes list.
357*9880d681SAndroid Build Coastguard Worker     AddMatcher(new RecordMatcher("'" + N->getOperator()->getName() +
358*9880d681SAndroid Build Coastguard Worker                                          "' chained node",
359*9880d681SAndroid Build Coastguard Worker                                  NextRecordedOperandNo));
360*9880d681SAndroid Build Coastguard Worker     // Remember all of the input chains our pattern will match.
361*9880d681SAndroid Build Coastguard Worker     MatchedChainNodes.push_back(NextRecordedOperandNo++);
362*9880d681SAndroid Build Coastguard Worker 
363*9880d681SAndroid Build Coastguard Worker     // Don't look at the input chain when matching the tree pattern to the
364*9880d681SAndroid Build Coastguard Worker     // SDNode.
365*9880d681SAndroid Build Coastguard Worker     OpNo = 1;
366*9880d681SAndroid Build Coastguard Worker 
367*9880d681SAndroid Build Coastguard Worker     // If this node is not the root and the subtree underneath it produces a
368*9880d681SAndroid Build Coastguard Worker     // chain, then the result of matching the node is also produce a chain.
369*9880d681SAndroid Build Coastguard Worker     // Beyond that, this means that we're also folding (at least) the root node
370*9880d681SAndroid Build Coastguard Worker     // into the node that produce the chain (for example, matching
371*9880d681SAndroid Build Coastguard Worker     // "(add reg, (load ptr))" as a add_with_memory on X86).  This is
372*9880d681SAndroid Build Coastguard Worker     // problematic, if the 'reg' node also uses the load (say, its chain).
373*9880d681SAndroid Build Coastguard Worker     // Graphically:
374*9880d681SAndroid Build Coastguard Worker     //
375*9880d681SAndroid Build Coastguard Worker     //         [LD]
376*9880d681SAndroid Build Coastguard Worker     //         ^  ^
377*9880d681SAndroid Build Coastguard Worker     //         |  \                              DAG's like cheese.
378*9880d681SAndroid Build Coastguard Worker     //        /    |
379*9880d681SAndroid Build Coastguard Worker     //       /    [YY]
380*9880d681SAndroid Build Coastguard Worker     //       |     ^
381*9880d681SAndroid Build Coastguard Worker     //      [XX]--/
382*9880d681SAndroid Build Coastguard Worker     //
383*9880d681SAndroid Build Coastguard Worker     // It would be invalid to fold XX and LD.  In this case, folding the two
384*9880d681SAndroid Build Coastguard Worker     // nodes together would induce a cycle in the DAG, making it a 'cyclic DAG'
385*9880d681SAndroid Build Coastguard Worker     // To prevent this, we emit a dynamic check for legality before allowing
386*9880d681SAndroid Build Coastguard Worker     // this to be folded.
387*9880d681SAndroid Build Coastguard Worker     //
388*9880d681SAndroid Build Coastguard Worker     const TreePatternNode *Root = Pattern.getSrcPattern();
389*9880d681SAndroid Build Coastguard Worker     if (N != Root) {                             // Not the root of the pattern.
390*9880d681SAndroid Build Coastguard Worker       // If there is a node between the root and this node, then we definitely
391*9880d681SAndroid Build Coastguard Worker       // need to emit the check.
392*9880d681SAndroid Build Coastguard Worker       bool NeedCheck = !Root->hasChild(N);
393*9880d681SAndroid Build Coastguard Worker 
394*9880d681SAndroid Build Coastguard Worker       // If it *is* an immediate child of the root, we can still need a check if
395*9880d681SAndroid Build Coastguard Worker       // the root SDNode has multiple inputs.  For us, this means that it is an
396*9880d681SAndroid Build Coastguard Worker       // intrinsic, has multiple operands, or has other inputs like chain or
397*9880d681SAndroid Build Coastguard Worker       // glue).
398*9880d681SAndroid Build Coastguard Worker       if (!NeedCheck) {
399*9880d681SAndroid Build Coastguard Worker         const SDNodeInfo &PInfo = CGP.getSDNodeInfo(Root->getOperator());
400*9880d681SAndroid Build Coastguard Worker         NeedCheck =
401*9880d681SAndroid Build Coastguard Worker           Root->getOperator() == CGP.get_intrinsic_void_sdnode() ||
402*9880d681SAndroid Build Coastguard Worker           Root->getOperator() == CGP.get_intrinsic_w_chain_sdnode() ||
403*9880d681SAndroid Build Coastguard Worker           Root->getOperator() == CGP.get_intrinsic_wo_chain_sdnode() ||
404*9880d681SAndroid Build Coastguard Worker           PInfo.getNumOperands() > 1 ||
405*9880d681SAndroid Build Coastguard Worker           PInfo.hasProperty(SDNPHasChain) ||
406*9880d681SAndroid Build Coastguard Worker           PInfo.hasProperty(SDNPInGlue) ||
407*9880d681SAndroid Build Coastguard Worker           PInfo.hasProperty(SDNPOptInGlue);
408*9880d681SAndroid Build Coastguard Worker       }
409*9880d681SAndroid Build Coastguard Worker 
410*9880d681SAndroid Build Coastguard Worker       if (NeedCheck)
411*9880d681SAndroid Build Coastguard Worker         AddMatcher(new CheckFoldableChainNodeMatcher());
412*9880d681SAndroid Build Coastguard Worker     }
413*9880d681SAndroid Build Coastguard Worker   }
414*9880d681SAndroid Build Coastguard Worker 
415*9880d681SAndroid Build Coastguard Worker   // If this node has an output glue and isn't the root, remember it.
416*9880d681SAndroid Build Coastguard Worker   if (N->NodeHasProperty(SDNPOutGlue, CGP) &&
417*9880d681SAndroid Build Coastguard Worker       N != Pattern.getSrcPattern()) {
418*9880d681SAndroid Build Coastguard Worker     // TODO: This redundantly records nodes with both glues and chains.
419*9880d681SAndroid Build Coastguard Worker 
420*9880d681SAndroid Build Coastguard Worker     // Record the node and remember it in our chained nodes list.
421*9880d681SAndroid Build Coastguard Worker     AddMatcher(new RecordMatcher("'" + N->getOperator()->getName() +
422*9880d681SAndroid Build Coastguard Worker                                          "' glue output node",
423*9880d681SAndroid Build Coastguard Worker                                  NextRecordedOperandNo));
424*9880d681SAndroid Build Coastguard Worker   }
425*9880d681SAndroid Build Coastguard Worker 
426*9880d681SAndroid Build Coastguard Worker   // If this node is known to have an input glue or if it *might* have an input
427*9880d681SAndroid Build Coastguard Worker   // glue, capture it as the glue input of the pattern.
428*9880d681SAndroid Build Coastguard Worker   if (N->NodeHasProperty(SDNPOptInGlue, CGP) ||
429*9880d681SAndroid Build Coastguard Worker       N->NodeHasProperty(SDNPInGlue, CGP))
430*9880d681SAndroid Build Coastguard Worker     AddMatcher(new CaptureGlueInputMatcher());
431*9880d681SAndroid Build Coastguard Worker 
432*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) {
433*9880d681SAndroid Build Coastguard Worker     // Get the code suitable for matching this child.  Move to the child, check
434*9880d681SAndroid Build Coastguard Worker     // it then move back to the parent.
435*9880d681SAndroid Build Coastguard Worker     AddMatcher(new MoveChildMatcher(OpNo));
436*9880d681SAndroid Build Coastguard Worker     EmitMatchCode(N->getChild(i), NodeNoTypes->getChild(i));
437*9880d681SAndroid Build Coastguard Worker     AddMatcher(new MoveParentMatcher());
438*9880d681SAndroid Build Coastguard Worker   }
439*9880d681SAndroid Build Coastguard Worker }
440*9880d681SAndroid Build Coastguard Worker 
recordUniqueNode(const std::string & Name)441*9880d681SAndroid Build Coastguard Worker bool MatcherGen::recordUniqueNode(const std::string &Name) {
442*9880d681SAndroid Build Coastguard Worker   unsigned &VarMapEntry = VariableMap[Name];
443*9880d681SAndroid Build Coastguard Worker   if (VarMapEntry == 0) {
444*9880d681SAndroid Build Coastguard Worker     // If it is a named node, we must emit a 'Record' opcode.
445*9880d681SAndroid Build Coastguard Worker     AddMatcher(new RecordMatcher("$" + Name, NextRecordedOperandNo));
446*9880d681SAndroid Build Coastguard Worker     VarMapEntry = ++NextRecordedOperandNo;
447*9880d681SAndroid Build Coastguard Worker     return true;
448*9880d681SAndroid Build Coastguard Worker   }
449*9880d681SAndroid Build Coastguard Worker 
450*9880d681SAndroid Build Coastguard Worker   // If we get here, this is a second reference to a specific name.  Since
451*9880d681SAndroid Build Coastguard Worker   // we already have checked that the first reference is valid, we don't
452*9880d681SAndroid Build Coastguard Worker   // have to recursively match it, just check that it's the same as the
453*9880d681SAndroid Build Coastguard Worker   // previously named thing.
454*9880d681SAndroid Build Coastguard Worker   AddMatcher(new CheckSameMatcher(VarMapEntry-1));
455*9880d681SAndroid Build Coastguard Worker   return false;
456*9880d681SAndroid Build Coastguard Worker }
457*9880d681SAndroid Build Coastguard Worker 
EmitMatchCode(const TreePatternNode * N,TreePatternNode * NodeNoTypes)458*9880d681SAndroid Build Coastguard Worker void MatcherGen::EmitMatchCode(const TreePatternNode *N,
459*9880d681SAndroid Build Coastguard Worker                                TreePatternNode *NodeNoTypes) {
460*9880d681SAndroid Build Coastguard Worker   // If N and NodeNoTypes don't agree on a type, then this is a case where we
461*9880d681SAndroid Build Coastguard Worker   // need to do a type check.  Emit the check, apply the type to NodeNoTypes and
462*9880d681SAndroid Build Coastguard Worker   // reinfer any correlated types.
463*9880d681SAndroid Build Coastguard Worker   SmallVector<unsigned, 2> ResultsToTypeCheck;
464*9880d681SAndroid Build Coastguard Worker 
465*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = NodeNoTypes->getNumTypes(); i != e; ++i) {
466*9880d681SAndroid Build Coastguard Worker     if (NodeNoTypes->getExtType(i) == N->getExtType(i)) continue;
467*9880d681SAndroid Build Coastguard Worker     NodeNoTypes->setType(i, N->getExtType(i));
468*9880d681SAndroid Build Coastguard Worker     InferPossibleTypes();
469*9880d681SAndroid Build Coastguard Worker     ResultsToTypeCheck.push_back(i);
470*9880d681SAndroid Build Coastguard Worker   }
471*9880d681SAndroid Build Coastguard Worker 
472*9880d681SAndroid Build Coastguard Worker   // If this node has a name associated with it, capture it in VariableMap. If
473*9880d681SAndroid Build Coastguard Worker   // we already saw this in the pattern, emit code to verify dagness.
474*9880d681SAndroid Build Coastguard Worker   if (!N->getName().empty())
475*9880d681SAndroid Build Coastguard Worker     if (!recordUniqueNode(N->getName()))
476*9880d681SAndroid Build Coastguard Worker       return;
477*9880d681SAndroid Build Coastguard Worker 
478*9880d681SAndroid Build Coastguard Worker   if (N->isLeaf())
479*9880d681SAndroid Build Coastguard Worker     EmitLeafMatchCode(N);
480*9880d681SAndroid Build Coastguard Worker   else
481*9880d681SAndroid Build Coastguard Worker     EmitOperatorMatchCode(N, NodeNoTypes);
482*9880d681SAndroid Build Coastguard Worker 
483*9880d681SAndroid Build Coastguard Worker   // If there are node predicates for this node, generate their checks.
484*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = N->getPredicateFns().size(); i != e; ++i)
485*9880d681SAndroid Build Coastguard Worker     AddMatcher(new CheckPredicateMatcher(N->getPredicateFns()[i]));
486*9880d681SAndroid Build Coastguard Worker 
487*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = ResultsToTypeCheck.size(); i != e; ++i)
488*9880d681SAndroid Build Coastguard Worker     AddMatcher(new CheckTypeMatcher(N->getType(ResultsToTypeCheck[i]),
489*9880d681SAndroid Build Coastguard Worker                                     ResultsToTypeCheck[i]));
490*9880d681SAndroid Build Coastguard Worker }
491*9880d681SAndroid Build Coastguard Worker 
492*9880d681SAndroid Build Coastguard Worker /// EmitMatcherCode - Generate the code that matches the predicate of this
493*9880d681SAndroid Build Coastguard Worker /// pattern for the specified Variant.  If the variant is invalid this returns
494*9880d681SAndroid Build Coastguard Worker /// true and does not generate code, if it is valid, it returns false.
EmitMatcherCode(unsigned Variant)495*9880d681SAndroid Build Coastguard Worker bool MatcherGen::EmitMatcherCode(unsigned Variant) {
496*9880d681SAndroid Build Coastguard Worker   // If the root of the pattern is a ComplexPattern and if it is specified to
497*9880d681SAndroid Build Coastguard Worker   // match some number of root opcodes, these are considered to be our variants.
498*9880d681SAndroid Build Coastguard Worker   // Depending on which variant we're generating code for, emit the root opcode
499*9880d681SAndroid Build Coastguard Worker   // check.
500*9880d681SAndroid Build Coastguard Worker   if (const ComplexPattern *CP =
501*9880d681SAndroid Build Coastguard Worker                    Pattern.getSrcPattern()->getComplexPatternInfo(CGP)) {
502*9880d681SAndroid Build Coastguard Worker     const std::vector<Record*> &OpNodes = CP->getRootNodes();
503*9880d681SAndroid Build Coastguard Worker     assert(!OpNodes.empty() &&"Complex Pattern must specify what it can match");
504*9880d681SAndroid Build Coastguard Worker     if (Variant >= OpNodes.size()) return true;
505*9880d681SAndroid Build Coastguard Worker 
506*9880d681SAndroid Build Coastguard Worker     AddMatcher(new CheckOpcodeMatcher(CGP.getSDNodeInfo(OpNodes[Variant])));
507*9880d681SAndroid Build Coastguard Worker   } else {
508*9880d681SAndroid Build Coastguard Worker     if (Variant != 0) return true;
509*9880d681SAndroid Build Coastguard Worker   }
510*9880d681SAndroid Build Coastguard Worker 
511*9880d681SAndroid Build Coastguard Worker   // Emit the matcher for the pattern structure and types.
512*9880d681SAndroid Build Coastguard Worker   EmitMatchCode(Pattern.getSrcPattern(), PatWithNoTypes);
513*9880d681SAndroid Build Coastguard Worker 
514*9880d681SAndroid Build Coastguard Worker   // If the pattern has a predicate on it (e.g. only enabled when a subtarget
515*9880d681SAndroid Build Coastguard Worker   // feature is around, do the check).
516*9880d681SAndroid Build Coastguard Worker   if (!Pattern.getPredicateCheck().empty())
517*9880d681SAndroid Build Coastguard Worker     AddMatcher(new CheckPatternPredicateMatcher(Pattern.getPredicateCheck()));
518*9880d681SAndroid Build Coastguard Worker 
519*9880d681SAndroid Build Coastguard Worker   // Now that we've completed the structural type match, emit any ComplexPattern
520*9880d681SAndroid Build Coastguard Worker   // checks (e.g. addrmode matches).  We emit this after the structural match
521*9880d681SAndroid Build Coastguard Worker   // because they are generally more expensive to evaluate and more difficult to
522*9880d681SAndroid Build Coastguard Worker   // factor.
523*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = MatchedComplexPatterns.size(); i != e; ++i) {
524*9880d681SAndroid Build Coastguard Worker     const TreePatternNode *N = MatchedComplexPatterns[i].first;
525*9880d681SAndroid Build Coastguard Worker 
526*9880d681SAndroid Build Coastguard Worker     // Remember where the results of this match get stuck.
527*9880d681SAndroid Build Coastguard Worker     if (N->isLeaf()) {
528*9880d681SAndroid Build Coastguard Worker       NamedComplexPatternOperands[N->getName()] = NextRecordedOperandNo + 1;
529*9880d681SAndroid Build Coastguard Worker     } else {
530*9880d681SAndroid Build Coastguard Worker       unsigned CurOp = NextRecordedOperandNo;
531*9880d681SAndroid Build Coastguard Worker       for (unsigned i = 0; i < N->getNumChildren(); ++i) {
532*9880d681SAndroid Build Coastguard Worker         NamedComplexPatternOperands[N->getChild(i)->getName()] = CurOp + 1;
533*9880d681SAndroid Build Coastguard Worker         CurOp += N->getChild(i)->getNumMIResults(CGP);
534*9880d681SAndroid Build Coastguard Worker       }
535*9880d681SAndroid Build Coastguard Worker     }
536*9880d681SAndroid Build Coastguard Worker 
537*9880d681SAndroid Build Coastguard Worker     // Get the slot we recorded the value in from the name on the node.
538*9880d681SAndroid Build Coastguard Worker     unsigned RecNodeEntry = MatchedComplexPatterns[i].second;
539*9880d681SAndroid Build Coastguard Worker 
540*9880d681SAndroid Build Coastguard Worker     const ComplexPattern &CP = *N->getComplexPatternInfo(CGP);
541*9880d681SAndroid Build Coastguard Worker 
542*9880d681SAndroid Build Coastguard Worker     // Emit a CheckComplexPat operation, which does the match (aborting if it
543*9880d681SAndroid Build Coastguard Worker     // fails) and pushes the matched operands onto the recorded nodes list.
544*9880d681SAndroid Build Coastguard Worker     AddMatcher(new CheckComplexPatMatcher(CP, RecNodeEntry,
545*9880d681SAndroid Build Coastguard Worker                                           N->getName(), NextRecordedOperandNo));
546*9880d681SAndroid Build Coastguard Worker 
547*9880d681SAndroid Build Coastguard Worker     // Record the right number of operands.
548*9880d681SAndroid Build Coastguard Worker     NextRecordedOperandNo += CP.getNumOperands();
549*9880d681SAndroid Build Coastguard Worker     if (CP.hasProperty(SDNPHasChain)) {
550*9880d681SAndroid Build Coastguard Worker       // If the complex pattern has a chain, then we need to keep track of the
551*9880d681SAndroid Build Coastguard Worker       // fact that we just recorded a chain input.  The chain input will be
552*9880d681SAndroid Build Coastguard Worker       // matched as the last operand of the predicate if it was successful.
553*9880d681SAndroid Build Coastguard Worker       ++NextRecordedOperandNo; // Chained node operand.
554*9880d681SAndroid Build Coastguard Worker 
555*9880d681SAndroid Build Coastguard Worker       // It is the last operand recorded.
556*9880d681SAndroid Build Coastguard Worker       assert(NextRecordedOperandNo > 1 &&
557*9880d681SAndroid Build Coastguard Worker              "Should have recorded input/result chains at least!");
558*9880d681SAndroid Build Coastguard Worker       MatchedChainNodes.push_back(NextRecordedOperandNo-1);
559*9880d681SAndroid Build Coastguard Worker     }
560*9880d681SAndroid Build Coastguard Worker 
561*9880d681SAndroid Build Coastguard Worker     // TODO: Complex patterns can't have output glues, if they did, we'd want
562*9880d681SAndroid Build Coastguard Worker     // to record them.
563*9880d681SAndroid Build Coastguard Worker   }
564*9880d681SAndroid Build Coastguard Worker 
565*9880d681SAndroid Build Coastguard Worker   return false;
566*9880d681SAndroid Build Coastguard Worker }
567*9880d681SAndroid Build Coastguard Worker 
568*9880d681SAndroid Build Coastguard Worker 
569*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
570*9880d681SAndroid Build Coastguard Worker // Node Result Generation
571*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
572*9880d681SAndroid Build Coastguard Worker 
EmitResultOfNamedOperand(const TreePatternNode * N,SmallVectorImpl<unsigned> & ResultOps)573*9880d681SAndroid Build Coastguard Worker void MatcherGen::EmitResultOfNamedOperand(const TreePatternNode *N,
574*9880d681SAndroid Build Coastguard Worker                                           SmallVectorImpl<unsigned> &ResultOps){
575*9880d681SAndroid Build Coastguard Worker   assert(!N->getName().empty() && "Operand not named!");
576*9880d681SAndroid Build Coastguard Worker 
577*9880d681SAndroid Build Coastguard Worker   if (unsigned SlotNo = NamedComplexPatternOperands[N->getName()]) {
578*9880d681SAndroid Build Coastguard Worker     // Complex operands have already been completely selected, just find the
579*9880d681SAndroid Build Coastguard Worker     // right slot ant add the arguments directly.
580*9880d681SAndroid Build Coastguard Worker     for (unsigned i = 0; i < N->getNumMIResults(CGP); ++i)
581*9880d681SAndroid Build Coastguard Worker       ResultOps.push_back(SlotNo - 1 + i);
582*9880d681SAndroid Build Coastguard Worker 
583*9880d681SAndroid Build Coastguard Worker     return;
584*9880d681SAndroid Build Coastguard Worker   }
585*9880d681SAndroid Build Coastguard Worker 
586*9880d681SAndroid Build Coastguard Worker   unsigned SlotNo = getNamedArgumentSlot(N->getName());
587*9880d681SAndroid Build Coastguard Worker 
588*9880d681SAndroid Build Coastguard Worker   // If this is an 'imm' or 'fpimm' node, make sure to convert it to the target
589*9880d681SAndroid Build Coastguard Worker   // version of the immediate so that it doesn't get selected due to some other
590*9880d681SAndroid Build Coastguard Worker   // node use.
591*9880d681SAndroid Build Coastguard Worker   if (!N->isLeaf()) {
592*9880d681SAndroid Build Coastguard Worker     StringRef OperatorName = N->getOperator()->getName();
593*9880d681SAndroid Build Coastguard Worker     if (OperatorName == "imm" || OperatorName == "fpimm") {
594*9880d681SAndroid Build Coastguard Worker       AddMatcher(new EmitConvertToTargetMatcher(SlotNo));
595*9880d681SAndroid Build Coastguard Worker       ResultOps.push_back(NextRecordedOperandNo++);
596*9880d681SAndroid Build Coastguard Worker       return;
597*9880d681SAndroid Build Coastguard Worker     }
598*9880d681SAndroid Build Coastguard Worker   }
599*9880d681SAndroid Build Coastguard Worker 
600*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0; i < N->getNumMIResults(CGP); ++i)
601*9880d681SAndroid Build Coastguard Worker     ResultOps.push_back(SlotNo + i);
602*9880d681SAndroid Build Coastguard Worker }
603*9880d681SAndroid Build Coastguard Worker 
EmitResultLeafAsOperand(const TreePatternNode * N,SmallVectorImpl<unsigned> & ResultOps)604*9880d681SAndroid Build Coastguard Worker void MatcherGen::EmitResultLeafAsOperand(const TreePatternNode *N,
605*9880d681SAndroid Build Coastguard Worker                                          SmallVectorImpl<unsigned> &ResultOps) {
606*9880d681SAndroid Build Coastguard Worker   assert(N->isLeaf() && "Must be a leaf");
607*9880d681SAndroid Build Coastguard Worker 
608*9880d681SAndroid Build Coastguard Worker   if (IntInit *II = dyn_cast<IntInit>(N->getLeafValue())) {
609*9880d681SAndroid Build Coastguard Worker     AddMatcher(new EmitIntegerMatcher(II->getValue(), N->getType(0)));
610*9880d681SAndroid Build Coastguard Worker     ResultOps.push_back(NextRecordedOperandNo++);
611*9880d681SAndroid Build Coastguard Worker     return;
612*9880d681SAndroid Build Coastguard Worker   }
613*9880d681SAndroid Build Coastguard Worker 
614*9880d681SAndroid Build Coastguard Worker   // If this is an explicit register reference, handle it.
615*9880d681SAndroid Build Coastguard Worker   if (DefInit *DI = dyn_cast<DefInit>(N->getLeafValue())) {
616*9880d681SAndroid Build Coastguard Worker     Record *Def = DI->getDef();
617*9880d681SAndroid Build Coastguard Worker     if (Def->isSubClassOf("Register")) {
618*9880d681SAndroid Build Coastguard Worker       const CodeGenRegister *Reg =
619*9880d681SAndroid Build Coastguard Worker         CGP.getTargetInfo().getRegBank().getReg(Def);
620*9880d681SAndroid Build Coastguard Worker       AddMatcher(new EmitRegisterMatcher(Reg, N->getType(0)));
621*9880d681SAndroid Build Coastguard Worker       ResultOps.push_back(NextRecordedOperandNo++);
622*9880d681SAndroid Build Coastguard Worker       return;
623*9880d681SAndroid Build Coastguard Worker     }
624*9880d681SAndroid Build Coastguard Worker 
625*9880d681SAndroid Build Coastguard Worker     if (Def->getName() == "zero_reg") {
626*9880d681SAndroid Build Coastguard Worker       AddMatcher(new EmitRegisterMatcher(nullptr, N->getType(0)));
627*9880d681SAndroid Build Coastguard Worker       ResultOps.push_back(NextRecordedOperandNo++);
628*9880d681SAndroid Build Coastguard Worker       return;
629*9880d681SAndroid Build Coastguard Worker     }
630*9880d681SAndroid Build Coastguard Worker 
631*9880d681SAndroid Build Coastguard Worker     // Handle a reference to a register class. This is used
632*9880d681SAndroid Build Coastguard Worker     // in COPY_TO_SUBREG instructions.
633*9880d681SAndroid Build Coastguard Worker     if (Def->isSubClassOf("RegisterOperand"))
634*9880d681SAndroid Build Coastguard Worker       Def = Def->getValueAsDef("RegClass");
635*9880d681SAndroid Build Coastguard Worker     if (Def->isSubClassOf("RegisterClass")) {
636*9880d681SAndroid Build Coastguard Worker       std::string Value = getQualifiedName(Def) + "RegClassID";
637*9880d681SAndroid Build Coastguard Worker       AddMatcher(new EmitStringIntegerMatcher(Value, MVT::i32));
638*9880d681SAndroid Build Coastguard Worker       ResultOps.push_back(NextRecordedOperandNo++);
639*9880d681SAndroid Build Coastguard Worker       return;
640*9880d681SAndroid Build Coastguard Worker     }
641*9880d681SAndroid Build Coastguard Worker 
642*9880d681SAndroid Build Coastguard Worker     // Handle a subregister index. This is used for INSERT_SUBREG etc.
643*9880d681SAndroid Build Coastguard Worker     if (Def->isSubClassOf("SubRegIndex")) {
644*9880d681SAndroid Build Coastguard Worker       std::string Value = getQualifiedName(Def);
645*9880d681SAndroid Build Coastguard Worker       AddMatcher(new EmitStringIntegerMatcher(Value, MVT::i32));
646*9880d681SAndroid Build Coastguard Worker       ResultOps.push_back(NextRecordedOperandNo++);
647*9880d681SAndroid Build Coastguard Worker       return;
648*9880d681SAndroid Build Coastguard Worker     }
649*9880d681SAndroid Build Coastguard Worker   }
650*9880d681SAndroid Build Coastguard Worker 
651*9880d681SAndroid Build Coastguard Worker   errs() << "unhandled leaf node: \n";
652*9880d681SAndroid Build Coastguard Worker   N->dump();
653*9880d681SAndroid Build Coastguard Worker }
654*9880d681SAndroid Build Coastguard Worker 
655*9880d681SAndroid Build Coastguard Worker /// GetInstPatternNode - Get the pattern for an instruction.
656*9880d681SAndroid Build Coastguard Worker ///
657*9880d681SAndroid Build Coastguard Worker const TreePatternNode *MatcherGen::
GetInstPatternNode(const DAGInstruction & Inst,const TreePatternNode * N)658*9880d681SAndroid Build Coastguard Worker GetInstPatternNode(const DAGInstruction &Inst, const TreePatternNode *N) {
659*9880d681SAndroid Build Coastguard Worker   const TreePattern *InstPat = Inst.getPattern();
660*9880d681SAndroid Build Coastguard Worker 
661*9880d681SAndroid Build Coastguard Worker   // FIXME2?: Assume actual pattern comes before "implicit".
662*9880d681SAndroid Build Coastguard Worker   TreePatternNode *InstPatNode;
663*9880d681SAndroid Build Coastguard Worker   if (InstPat)
664*9880d681SAndroid Build Coastguard Worker     InstPatNode = InstPat->getTree(0);
665*9880d681SAndroid Build Coastguard Worker   else if (/*isRoot*/ N == Pattern.getDstPattern())
666*9880d681SAndroid Build Coastguard Worker     InstPatNode = Pattern.getSrcPattern();
667*9880d681SAndroid Build Coastguard Worker   else
668*9880d681SAndroid Build Coastguard Worker     return nullptr;
669*9880d681SAndroid Build Coastguard Worker 
670*9880d681SAndroid Build Coastguard Worker   if (InstPatNode && !InstPatNode->isLeaf() &&
671*9880d681SAndroid Build Coastguard Worker       InstPatNode->getOperator()->getName() == "set")
672*9880d681SAndroid Build Coastguard Worker     InstPatNode = InstPatNode->getChild(InstPatNode->getNumChildren()-1);
673*9880d681SAndroid Build Coastguard Worker 
674*9880d681SAndroid Build Coastguard Worker   return InstPatNode;
675*9880d681SAndroid Build Coastguard Worker }
676*9880d681SAndroid Build Coastguard Worker 
677*9880d681SAndroid Build Coastguard Worker static bool
mayInstNodeLoadOrStore(const TreePatternNode * N,const CodeGenDAGPatterns & CGP)678*9880d681SAndroid Build Coastguard Worker mayInstNodeLoadOrStore(const TreePatternNode *N,
679*9880d681SAndroid Build Coastguard Worker                        const CodeGenDAGPatterns &CGP) {
680*9880d681SAndroid Build Coastguard Worker   Record *Op = N->getOperator();
681*9880d681SAndroid Build Coastguard Worker   const CodeGenTarget &CGT = CGP.getTargetInfo();
682*9880d681SAndroid Build Coastguard Worker   CodeGenInstruction &II = CGT.getInstruction(Op);
683*9880d681SAndroid Build Coastguard Worker   return II.mayLoad || II.mayStore;
684*9880d681SAndroid Build Coastguard Worker }
685*9880d681SAndroid Build Coastguard Worker 
686*9880d681SAndroid Build Coastguard Worker static unsigned
numNodesThatMayLoadOrStore(const TreePatternNode * N,const CodeGenDAGPatterns & CGP)687*9880d681SAndroid Build Coastguard Worker numNodesThatMayLoadOrStore(const TreePatternNode *N,
688*9880d681SAndroid Build Coastguard Worker                            const CodeGenDAGPatterns &CGP) {
689*9880d681SAndroid Build Coastguard Worker   if (N->isLeaf())
690*9880d681SAndroid Build Coastguard Worker     return 0;
691*9880d681SAndroid Build Coastguard Worker 
692*9880d681SAndroid Build Coastguard Worker   Record *OpRec = N->getOperator();
693*9880d681SAndroid Build Coastguard Worker   if (!OpRec->isSubClassOf("Instruction"))
694*9880d681SAndroid Build Coastguard Worker     return 0;
695*9880d681SAndroid Build Coastguard Worker 
696*9880d681SAndroid Build Coastguard Worker   unsigned Count = 0;
697*9880d681SAndroid Build Coastguard Worker   if (mayInstNodeLoadOrStore(N, CGP))
698*9880d681SAndroid Build Coastguard Worker     ++Count;
699*9880d681SAndroid Build Coastguard Worker 
700*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
701*9880d681SAndroid Build Coastguard Worker     Count += numNodesThatMayLoadOrStore(N->getChild(i), CGP);
702*9880d681SAndroid Build Coastguard Worker 
703*9880d681SAndroid Build Coastguard Worker   return Count;
704*9880d681SAndroid Build Coastguard Worker }
705*9880d681SAndroid Build Coastguard Worker 
706*9880d681SAndroid Build Coastguard Worker void MatcherGen::
EmitResultInstructionAsOperand(const TreePatternNode * N,SmallVectorImpl<unsigned> & OutputOps)707*9880d681SAndroid Build Coastguard Worker EmitResultInstructionAsOperand(const TreePatternNode *N,
708*9880d681SAndroid Build Coastguard Worker                                SmallVectorImpl<unsigned> &OutputOps) {
709*9880d681SAndroid Build Coastguard Worker   Record *Op = N->getOperator();
710*9880d681SAndroid Build Coastguard Worker   const CodeGenTarget &CGT = CGP.getTargetInfo();
711*9880d681SAndroid Build Coastguard Worker   CodeGenInstruction &II = CGT.getInstruction(Op);
712*9880d681SAndroid Build Coastguard Worker   const DAGInstruction &Inst = CGP.getInstruction(Op);
713*9880d681SAndroid Build Coastguard Worker 
714*9880d681SAndroid Build Coastguard Worker   // If we can, get the pattern for the instruction we're generating. We derive
715*9880d681SAndroid Build Coastguard Worker   // a variety of information from this pattern, such as whether it has a chain.
716*9880d681SAndroid Build Coastguard Worker   //
717*9880d681SAndroid Build Coastguard Worker   // FIXME2: This is extremely dubious for several reasons, not the least of
718*9880d681SAndroid Build Coastguard Worker   // which it gives special status to instructions with patterns that Pat<>
719*9880d681SAndroid Build Coastguard Worker   // nodes can't duplicate.
720*9880d681SAndroid Build Coastguard Worker   const TreePatternNode *InstPatNode = GetInstPatternNode(Inst, N);
721*9880d681SAndroid Build Coastguard Worker 
722*9880d681SAndroid Build Coastguard Worker   // NodeHasChain - Whether the instruction node we're creating takes chains.
723*9880d681SAndroid Build Coastguard Worker   bool NodeHasChain = InstPatNode &&
724*9880d681SAndroid Build Coastguard Worker                       InstPatNode->TreeHasProperty(SDNPHasChain, CGP);
725*9880d681SAndroid Build Coastguard Worker 
726*9880d681SAndroid Build Coastguard Worker   // Instructions which load and store from memory should have a chain,
727*9880d681SAndroid Build Coastguard Worker   // regardless of whether they happen to have an internal pattern saying so.
728*9880d681SAndroid Build Coastguard Worker   if (Pattern.getSrcPattern()->TreeHasProperty(SDNPHasChain, CGP)
729*9880d681SAndroid Build Coastguard Worker       && (II.hasCtrlDep || II.mayLoad || II.mayStore || II.canFoldAsLoad ||
730*9880d681SAndroid Build Coastguard Worker           II.hasSideEffects))
731*9880d681SAndroid Build Coastguard Worker       NodeHasChain = true;
732*9880d681SAndroid Build Coastguard Worker 
733*9880d681SAndroid Build Coastguard Worker   bool isRoot = N == Pattern.getDstPattern();
734*9880d681SAndroid Build Coastguard Worker 
735*9880d681SAndroid Build Coastguard Worker   // TreeHasOutGlue - True if this tree has glue.
736*9880d681SAndroid Build Coastguard Worker   bool TreeHasInGlue = false, TreeHasOutGlue = false;
737*9880d681SAndroid Build Coastguard Worker   if (isRoot) {
738*9880d681SAndroid Build Coastguard Worker     const TreePatternNode *SrcPat = Pattern.getSrcPattern();
739*9880d681SAndroid Build Coastguard Worker     TreeHasInGlue = SrcPat->TreeHasProperty(SDNPOptInGlue, CGP) ||
740*9880d681SAndroid Build Coastguard Worker                     SrcPat->TreeHasProperty(SDNPInGlue, CGP);
741*9880d681SAndroid Build Coastguard Worker 
742*9880d681SAndroid Build Coastguard Worker     // FIXME2: this is checking the entire pattern, not just the node in
743*9880d681SAndroid Build Coastguard Worker     // question, doing this just for the root seems like a total hack.
744*9880d681SAndroid Build Coastguard Worker     TreeHasOutGlue = SrcPat->TreeHasProperty(SDNPOutGlue, CGP);
745*9880d681SAndroid Build Coastguard Worker   }
746*9880d681SAndroid Build Coastguard Worker 
747*9880d681SAndroid Build Coastguard Worker   // NumResults - This is the number of results produced by the instruction in
748*9880d681SAndroid Build Coastguard Worker   // the "outs" list.
749*9880d681SAndroid Build Coastguard Worker   unsigned NumResults = Inst.getNumResults();
750*9880d681SAndroid Build Coastguard Worker 
751*9880d681SAndroid Build Coastguard Worker   // Number of operands we know the output instruction must have. If it is
752*9880d681SAndroid Build Coastguard Worker   // variadic, we could have more operands.
753*9880d681SAndroid Build Coastguard Worker   unsigned NumFixedOperands = II.Operands.size();
754*9880d681SAndroid Build Coastguard Worker 
755*9880d681SAndroid Build Coastguard Worker   SmallVector<unsigned, 8> InstOps;
756*9880d681SAndroid Build Coastguard Worker 
757*9880d681SAndroid Build Coastguard Worker   // Loop over all of the fixed operands of the instruction pattern, emitting
758*9880d681SAndroid Build Coastguard Worker   // code to fill them all in. The node 'N' usually has number children equal to
759*9880d681SAndroid Build Coastguard Worker   // the number of input operands of the instruction.  However, in cases where
760*9880d681SAndroid Build Coastguard Worker   // there are predicate operands for an instruction, we need to fill in the
761*9880d681SAndroid Build Coastguard Worker   // 'execute always' values. Match up the node operands to the instruction
762*9880d681SAndroid Build Coastguard Worker   // operands to do this.
763*9880d681SAndroid Build Coastguard Worker   unsigned ChildNo = 0;
764*9880d681SAndroid Build Coastguard Worker   for (unsigned InstOpNo = NumResults, e = NumFixedOperands;
765*9880d681SAndroid Build Coastguard Worker        InstOpNo != e; ++InstOpNo) {
766*9880d681SAndroid Build Coastguard Worker     // Determine what to emit for this operand.
767*9880d681SAndroid Build Coastguard Worker     Record *OperandNode = II.Operands[InstOpNo].Rec;
768*9880d681SAndroid Build Coastguard Worker     if (OperandNode->isSubClassOf("OperandWithDefaultOps") &&
769*9880d681SAndroid Build Coastguard Worker         !CGP.getDefaultOperand(OperandNode).DefaultOps.empty()) {
770*9880d681SAndroid Build Coastguard Worker       // This is a predicate or optional def operand; emit the
771*9880d681SAndroid Build Coastguard Worker       // 'default ops' operands.
772*9880d681SAndroid Build Coastguard Worker       const DAGDefaultOperand &DefaultOp
773*9880d681SAndroid Build Coastguard Worker         = CGP.getDefaultOperand(OperandNode);
774*9880d681SAndroid Build Coastguard Worker       for (unsigned i = 0, e = DefaultOp.DefaultOps.size(); i != e; ++i)
775*9880d681SAndroid Build Coastguard Worker         EmitResultOperand(DefaultOp.DefaultOps[i], InstOps);
776*9880d681SAndroid Build Coastguard Worker       continue;
777*9880d681SAndroid Build Coastguard Worker     }
778*9880d681SAndroid Build Coastguard Worker 
779*9880d681SAndroid Build Coastguard Worker     // Otherwise this is a normal operand or a predicate operand without
780*9880d681SAndroid Build Coastguard Worker     // 'execute always'; emit it.
781*9880d681SAndroid Build Coastguard Worker 
782*9880d681SAndroid Build Coastguard Worker     // For operands with multiple sub-operands we may need to emit
783*9880d681SAndroid Build Coastguard Worker     // multiple child patterns to cover them all.  However, ComplexPattern
784*9880d681SAndroid Build Coastguard Worker     // children may themselves emit multiple MI operands.
785*9880d681SAndroid Build Coastguard Worker     unsigned NumSubOps = 1;
786*9880d681SAndroid Build Coastguard Worker     if (OperandNode->isSubClassOf("Operand")) {
787*9880d681SAndroid Build Coastguard Worker       DagInit *MIOpInfo = OperandNode->getValueAsDag("MIOperandInfo");
788*9880d681SAndroid Build Coastguard Worker       if (unsigned NumArgs = MIOpInfo->getNumArgs())
789*9880d681SAndroid Build Coastguard Worker         NumSubOps = NumArgs;
790*9880d681SAndroid Build Coastguard Worker     }
791*9880d681SAndroid Build Coastguard Worker 
792*9880d681SAndroid Build Coastguard Worker     unsigned FinalNumOps = InstOps.size() + NumSubOps;
793*9880d681SAndroid Build Coastguard Worker     while (InstOps.size() < FinalNumOps) {
794*9880d681SAndroid Build Coastguard Worker       const TreePatternNode *Child = N->getChild(ChildNo);
795*9880d681SAndroid Build Coastguard Worker       unsigned BeforeAddingNumOps = InstOps.size();
796*9880d681SAndroid Build Coastguard Worker       EmitResultOperand(Child, InstOps);
797*9880d681SAndroid Build Coastguard Worker       assert(InstOps.size() > BeforeAddingNumOps && "Didn't add any operands");
798*9880d681SAndroid Build Coastguard Worker 
799*9880d681SAndroid Build Coastguard Worker       // If the operand is an instruction and it produced multiple results, just
800*9880d681SAndroid Build Coastguard Worker       // take the first one.
801*9880d681SAndroid Build Coastguard Worker       if (!Child->isLeaf() && Child->getOperator()->isSubClassOf("Instruction"))
802*9880d681SAndroid Build Coastguard Worker         InstOps.resize(BeforeAddingNumOps+1);
803*9880d681SAndroid Build Coastguard Worker 
804*9880d681SAndroid Build Coastguard Worker       ++ChildNo;
805*9880d681SAndroid Build Coastguard Worker     }
806*9880d681SAndroid Build Coastguard Worker   }
807*9880d681SAndroid Build Coastguard Worker 
808*9880d681SAndroid Build Coastguard Worker   // If this is a variadic output instruction (i.e. REG_SEQUENCE), we can't
809*9880d681SAndroid Build Coastguard Worker   // expand suboperands, use default operands, or other features determined from
810*9880d681SAndroid Build Coastguard Worker   // the CodeGenInstruction after the fixed operands, which were handled
811*9880d681SAndroid Build Coastguard Worker   // above. Emit the remaining instructions implicitly added by the use for
812*9880d681SAndroid Build Coastguard Worker   // variable_ops.
813*9880d681SAndroid Build Coastguard Worker   if (II.Operands.isVariadic) {
814*9880d681SAndroid Build Coastguard Worker     for (unsigned I = ChildNo, E = N->getNumChildren(); I < E; ++I)
815*9880d681SAndroid Build Coastguard Worker       EmitResultOperand(N->getChild(I), InstOps);
816*9880d681SAndroid Build Coastguard Worker   }
817*9880d681SAndroid Build Coastguard Worker 
818*9880d681SAndroid Build Coastguard Worker   // If this node has input glue or explicitly specified input physregs, we
819*9880d681SAndroid Build Coastguard Worker   // need to add chained and glued copyfromreg nodes and materialize the glue
820*9880d681SAndroid Build Coastguard Worker   // input.
821*9880d681SAndroid Build Coastguard Worker   if (isRoot && !PhysRegInputs.empty()) {
822*9880d681SAndroid Build Coastguard Worker     // Emit all of the CopyToReg nodes for the input physical registers.  These
823*9880d681SAndroid Build Coastguard Worker     // occur in patterns like (mul:i8 AL:i8, GR8:i8:$src).
824*9880d681SAndroid Build Coastguard Worker     for (unsigned i = 0, e = PhysRegInputs.size(); i != e; ++i)
825*9880d681SAndroid Build Coastguard Worker       AddMatcher(new EmitCopyToRegMatcher(PhysRegInputs[i].second,
826*9880d681SAndroid Build Coastguard Worker                                           PhysRegInputs[i].first));
827*9880d681SAndroid Build Coastguard Worker     // Even if the node has no other glue inputs, the resultant node must be
828*9880d681SAndroid Build Coastguard Worker     // glued to the CopyFromReg nodes we just generated.
829*9880d681SAndroid Build Coastguard Worker     TreeHasInGlue = true;
830*9880d681SAndroid Build Coastguard Worker   }
831*9880d681SAndroid Build Coastguard Worker 
832*9880d681SAndroid Build Coastguard Worker   // Result order: node results, chain, glue
833*9880d681SAndroid Build Coastguard Worker 
834*9880d681SAndroid Build Coastguard Worker   // Determine the result types.
835*9880d681SAndroid Build Coastguard Worker   SmallVector<MVT::SimpleValueType, 4> ResultVTs;
836*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = N->getNumTypes(); i != e; ++i)
837*9880d681SAndroid Build Coastguard Worker     ResultVTs.push_back(N->getType(i));
838*9880d681SAndroid Build Coastguard Worker 
839*9880d681SAndroid Build Coastguard Worker   // If this is the root instruction of a pattern that has physical registers in
840*9880d681SAndroid Build Coastguard Worker   // its result pattern, add output VTs for them.  For example, X86 has:
841*9880d681SAndroid Build Coastguard Worker   //   (set AL, (mul ...))
842*9880d681SAndroid Build Coastguard Worker   // This also handles implicit results like:
843*9880d681SAndroid Build Coastguard Worker   //   (implicit EFLAGS)
844*9880d681SAndroid Build Coastguard Worker   if (isRoot && !Pattern.getDstRegs().empty()) {
845*9880d681SAndroid Build Coastguard Worker     // If the root came from an implicit def in the instruction handling stuff,
846*9880d681SAndroid Build Coastguard Worker     // don't re-add it.
847*9880d681SAndroid Build Coastguard Worker     Record *HandledReg = nullptr;
848*9880d681SAndroid Build Coastguard Worker     if (II.HasOneImplicitDefWithKnownVT(CGT) != MVT::Other)
849*9880d681SAndroid Build Coastguard Worker       HandledReg = II.ImplicitDefs[0];
850*9880d681SAndroid Build Coastguard Worker 
851*9880d681SAndroid Build Coastguard Worker     for (unsigned i = 0; i != Pattern.getDstRegs().size(); ++i) {
852*9880d681SAndroid Build Coastguard Worker       Record *Reg = Pattern.getDstRegs()[i];
853*9880d681SAndroid Build Coastguard Worker       if (!Reg->isSubClassOf("Register") || Reg == HandledReg) continue;
854*9880d681SAndroid Build Coastguard Worker       ResultVTs.push_back(getRegisterValueType(Reg, CGT));
855*9880d681SAndroid Build Coastguard Worker     }
856*9880d681SAndroid Build Coastguard Worker   }
857*9880d681SAndroid Build Coastguard Worker 
858*9880d681SAndroid Build Coastguard Worker   // If this is the root of the pattern and the pattern we're matching includes
859*9880d681SAndroid Build Coastguard Worker   // a node that is variadic, mark the generated node as variadic so that it
860*9880d681SAndroid Build Coastguard Worker   // gets the excess operands from the input DAG.
861*9880d681SAndroid Build Coastguard Worker   int NumFixedArityOperands = -1;
862*9880d681SAndroid Build Coastguard Worker   if (isRoot &&
863*9880d681SAndroid Build Coastguard Worker       Pattern.getSrcPattern()->NodeHasProperty(SDNPVariadic, CGP))
864*9880d681SAndroid Build Coastguard Worker     NumFixedArityOperands = Pattern.getSrcPattern()->getNumChildren();
865*9880d681SAndroid Build Coastguard Worker 
866*9880d681SAndroid Build Coastguard Worker   // If this is the root node and multiple matched nodes in the input pattern
867*9880d681SAndroid Build Coastguard Worker   // have MemRefs in them, have the interpreter collect them and plop them onto
868*9880d681SAndroid Build Coastguard Worker   // this node. If there is just one node with MemRefs, leave them on that node
869*9880d681SAndroid Build Coastguard Worker   // even if it is not the root.
870*9880d681SAndroid Build Coastguard Worker   //
871*9880d681SAndroid Build Coastguard Worker   // FIXME3: This is actively incorrect for result patterns with multiple
872*9880d681SAndroid Build Coastguard Worker   // memory-referencing instructions.
873*9880d681SAndroid Build Coastguard Worker   bool PatternHasMemOperands =
874*9880d681SAndroid Build Coastguard Worker     Pattern.getSrcPattern()->TreeHasProperty(SDNPMemOperand, CGP);
875*9880d681SAndroid Build Coastguard Worker 
876*9880d681SAndroid Build Coastguard Worker   bool NodeHasMemRefs = false;
877*9880d681SAndroid Build Coastguard Worker   if (PatternHasMemOperands) {
878*9880d681SAndroid Build Coastguard Worker     unsigned NumNodesThatLoadOrStore =
879*9880d681SAndroid Build Coastguard Worker       numNodesThatMayLoadOrStore(Pattern.getDstPattern(), CGP);
880*9880d681SAndroid Build Coastguard Worker     bool NodeIsUniqueLoadOrStore = mayInstNodeLoadOrStore(N, CGP) &&
881*9880d681SAndroid Build Coastguard Worker                                    NumNodesThatLoadOrStore == 1;
882*9880d681SAndroid Build Coastguard Worker     NodeHasMemRefs =
883*9880d681SAndroid Build Coastguard Worker       NodeIsUniqueLoadOrStore || (isRoot && (mayInstNodeLoadOrStore(N, CGP) ||
884*9880d681SAndroid Build Coastguard Worker                                              NumNodesThatLoadOrStore != 1));
885*9880d681SAndroid Build Coastguard Worker   }
886*9880d681SAndroid Build Coastguard Worker 
887*9880d681SAndroid Build Coastguard Worker   assert((!ResultVTs.empty() || TreeHasOutGlue || NodeHasChain) &&
888*9880d681SAndroid Build Coastguard Worker          "Node has no result");
889*9880d681SAndroid Build Coastguard Worker 
890*9880d681SAndroid Build Coastguard Worker   AddMatcher(new EmitNodeMatcher(II.Namespace+"::"+II.TheDef->getName(),
891*9880d681SAndroid Build Coastguard Worker                                  ResultVTs, InstOps,
892*9880d681SAndroid Build Coastguard Worker                                  NodeHasChain, TreeHasInGlue, TreeHasOutGlue,
893*9880d681SAndroid Build Coastguard Worker                                  NodeHasMemRefs, NumFixedArityOperands,
894*9880d681SAndroid Build Coastguard Worker                                  NextRecordedOperandNo));
895*9880d681SAndroid Build Coastguard Worker 
896*9880d681SAndroid Build Coastguard Worker   // The non-chain and non-glue results of the newly emitted node get recorded.
897*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = ResultVTs.size(); i != e; ++i) {
898*9880d681SAndroid Build Coastguard Worker     if (ResultVTs[i] == MVT::Other || ResultVTs[i] == MVT::Glue) break;
899*9880d681SAndroid Build Coastguard Worker     OutputOps.push_back(NextRecordedOperandNo++);
900*9880d681SAndroid Build Coastguard Worker   }
901*9880d681SAndroid Build Coastguard Worker }
902*9880d681SAndroid Build Coastguard Worker 
903*9880d681SAndroid Build Coastguard Worker void MatcherGen::
EmitResultSDNodeXFormAsOperand(const TreePatternNode * N,SmallVectorImpl<unsigned> & ResultOps)904*9880d681SAndroid Build Coastguard Worker EmitResultSDNodeXFormAsOperand(const TreePatternNode *N,
905*9880d681SAndroid Build Coastguard Worker                                SmallVectorImpl<unsigned> &ResultOps) {
906*9880d681SAndroid Build Coastguard Worker   assert(N->getOperator()->isSubClassOf("SDNodeXForm") && "Not SDNodeXForm?");
907*9880d681SAndroid Build Coastguard Worker 
908*9880d681SAndroid Build Coastguard Worker   // Emit the operand.
909*9880d681SAndroid Build Coastguard Worker   SmallVector<unsigned, 8> InputOps;
910*9880d681SAndroid Build Coastguard Worker 
911*9880d681SAndroid Build Coastguard Worker   // FIXME2: Could easily generalize this to support multiple inputs and outputs
912*9880d681SAndroid Build Coastguard Worker   // to the SDNodeXForm.  For now we just support one input and one output like
913*9880d681SAndroid Build Coastguard Worker   // the old instruction selector.
914*9880d681SAndroid Build Coastguard Worker   assert(N->getNumChildren() == 1);
915*9880d681SAndroid Build Coastguard Worker   EmitResultOperand(N->getChild(0), InputOps);
916*9880d681SAndroid Build Coastguard Worker 
917*9880d681SAndroid Build Coastguard Worker   // The input currently must have produced exactly one result.
918*9880d681SAndroid Build Coastguard Worker   assert(InputOps.size() == 1 && "Unexpected input to SDNodeXForm");
919*9880d681SAndroid Build Coastguard Worker 
920*9880d681SAndroid Build Coastguard Worker   AddMatcher(new EmitNodeXFormMatcher(InputOps[0], N->getOperator()));
921*9880d681SAndroid Build Coastguard Worker   ResultOps.push_back(NextRecordedOperandNo++);
922*9880d681SAndroid Build Coastguard Worker }
923*9880d681SAndroid Build Coastguard Worker 
EmitResultOperand(const TreePatternNode * N,SmallVectorImpl<unsigned> & ResultOps)924*9880d681SAndroid Build Coastguard Worker void MatcherGen::EmitResultOperand(const TreePatternNode *N,
925*9880d681SAndroid Build Coastguard Worker                                    SmallVectorImpl<unsigned> &ResultOps) {
926*9880d681SAndroid Build Coastguard Worker   // This is something selected from the pattern we matched.
927*9880d681SAndroid Build Coastguard Worker   if (!N->getName().empty())
928*9880d681SAndroid Build Coastguard Worker     return EmitResultOfNamedOperand(N, ResultOps);
929*9880d681SAndroid Build Coastguard Worker 
930*9880d681SAndroid Build Coastguard Worker   if (N->isLeaf())
931*9880d681SAndroid Build Coastguard Worker     return EmitResultLeafAsOperand(N, ResultOps);
932*9880d681SAndroid Build Coastguard Worker 
933*9880d681SAndroid Build Coastguard Worker   Record *OpRec = N->getOperator();
934*9880d681SAndroid Build Coastguard Worker   if (OpRec->isSubClassOf("Instruction"))
935*9880d681SAndroid Build Coastguard Worker     return EmitResultInstructionAsOperand(N, ResultOps);
936*9880d681SAndroid Build Coastguard Worker   if (OpRec->isSubClassOf("SDNodeXForm"))
937*9880d681SAndroid Build Coastguard Worker     return EmitResultSDNodeXFormAsOperand(N, ResultOps);
938*9880d681SAndroid Build Coastguard Worker   errs() << "Unknown result node to emit code for: " << *N << '\n';
939*9880d681SAndroid Build Coastguard Worker   PrintFatalError("Unknown node in result pattern!");
940*9880d681SAndroid Build Coastguard Worker }
941*9880d681SAndroid Build Coastguard Worker 
EmitResultCode()942*9880d681SAndroid Build Coastguard Worker void MatcherGen::EmitResultCode() {
943*9880d681SAndroid Build Coastguard Worker   // Patterns that match nodes with (potentially multiple) chain inputs have to
944*9880d681SAndroid Build Coastguard Worker   // merge them together into a token factor.  This informs the generated code
945*9880d681SAndroid Build Coastguard Worker   // what all the chained nodes are.
946*9880d681SAndroid Build Coastguard Worker   if (!MatchedChainNodes.empty())
947*9880d681SAndroid Build Coastguard Worker     AddMatcher(new EmitMergeInputChainsMatcher(MatchedChainNodes));
948*9880d681SAndroid Build Coastguard Worker 
949*9880d681SAndroid Build Coastguard Worker   // Codegen the root of the result pattern, capturing the resulting values.
950*9880d681SAndroid Build Coastguard Worker   SmallVector<unsigned, 8> Ops;
951*9880d681SAndroid Build Coastguard Worker   EmitResultOperand(Pattern.getDstPattern(), Ops);
952*9880d681SAndroid Build Coastguard Worker 
953*9880d681SAndroid Build Coastguard Worker   // At this point, we have however many values the result pattern produces.
954*9880d681SAndroid Build Coastguard Worker   // However, the input pattern might not need all of these.  If there are
955*9880d681SAndroid Build Coastguard Worker   // excess values at the end (such as implicit defs of condition codes etc)
956*9880d681SAndroid Build Coastguard Worker   // just lop them off.  This doesn't need to worry about glue or chains, just
957*9880d681SAndroid Build Coastguard Worker   // explicit results.
958*9880d681SAndroid Build Coastguard Worker   //
959*9880d681SAndroid Build Coastguard Worker   unsigned NumSrcResults = Pattern.getSrcPattern()->getNumTypes();
960*9880d681SAndroid Build Coastguard Worker 
961*9880d681SAndroid Build Coastguard Worker   // If the pattern also has (implicit) results, count them as well.
962*9880d681SAndroid Build Coastguard Worker   if (!Pattern.getDstRegs().empty()) {
963*9880d681SAndroid Build Coastguard Worker     // If the root came from an implicit def in the instruction handling stuff,
964*9880d681SAndroid Build Coastguard Worker     // don't re-add it.
965*9880d681SAndroid Build Coastguard Worker     Record *HandledReg = nullptr;
966*9880d681SAndroid Build Coastguard Worker     const TreePatternNode *DstPat = Pattern.getDstPattern();
967*9880d681SAndroid Build Coastguard Worker     if (!DstPat->isLeaf() &&DstPat->getOperator()->isSubClassOf("Instruction")){
968*9880d681SAndroid Build Coastguard Worker       const CodeGenTarget &CGT = CGP.getTargetInfo();
969*9880d681SAndroid Build Coastguard Worker       CodeGenInstruction &II = CGT.getInstruction(DstPat->getOperator());
970*9880d681SAndroid Build Coastguard Worker 
971*9880d681SAndroid Build Coastguard Worker       if (II.HasOneImplicitDefWithKnownVT(CGT) != MVT::Other)
972*9880d681SAndroid Build Coastguard Worker         HandledReg = II.ImplicitDefs[0];
973*9880d681SAndroid Build Coastguard Worker     }
974*9880d681SAndroid Build Coastguard Worker 
975*9880d681SAndroid Build Coastguard Worker     for (unsigned i = 0; i != Pattern.getDstRegs().size(); ++i) {
976*9880d681SAndroid Build Coastguard Worker       Record *Reg = Pattern.getDstRegs()[i];
977*9880d681SAndroid Build Coastguard Worker       if (!Reg->isSubClassOf("Register") || Reg == HandledReg) continue;
978*9880d681SAndroid Build Coastguard Worker       ++NumSrcResults;
979*9880d681SAndroid Build Coastguard Worker     }
980*9880d681SAndroid Build Coastguard Worker   }
981*9880d681SAndroid Build Coastguard Worker 
982*9880d681SAndroid Build Coastguard Worker   assert(Ops.size() >= NumSrcResults && "Didn't provide enough results");
983*9880d681SAndroid Build Coastguard Worker   Ops.resize(NumSrcResults);
984*9880d681SAndroid Build Coastguard Worker 
985*9880d681SAndroid Build Coastguard Worker   AddMatcher(new CompleteMatchMatcher(Ops, Pattern));
986*9880d681SAndroid Build Coastguard Worker }
987*9880d681SAndroid Build Coastguard Worker 
988*9880d681SAndroid Build Coastguard Worker 
989*9880d681SAndroid Build Coastguard Worker /// ConvertPatternToMatcher - Create the matcher for the specified pattern with
990*9880d681SAndroid Build Coastguard Worker /// the specified variant.  If the variant number is invalid, this returns null.
ConvertPatternToMatcher(const PatternToMatch & Pattern,unsigned Variant,const CodeGenDAGPatterns & CGP)991*9880d681SAndroid Build Coastguard Worker Matcher *llvm::ConvertPatternToMatcher(const PatternToMatch &Pattern,
992*9880d681SAndroid Build Coastguard Worker                                        unsigned Variant,
993*9880d681SAndroid Build Coastguard Worker                                        const CodeGenDAGPatterns &CGP) {
994*9880d681SAndroid Build Coastguard Worker   MatcherGen Gen(Pattern, CGP);
995*9880d681SAndroid Build Coastguard Worker 
996*9880d681SAndroid Build Coastguard Worker   // Generate the code for the matcher.
997*9880d681SAndroid Build Coastguard Worker   if (Gen.EmitMatcherCode(Variant))
998*9880d681SAndroid Build Coastguard Worker     return nullptr;
999*9880d681SAndroid Build Coastguard Worker 
1000*9880d681SAndroid Build Coastguard Worker   // FIXME2: Kill extra MoveParent commands at the end of the matcher sequence.
1001*9880d681SAndroid Build Coastguard Worker   // FIXME2: Split result code out to another table, and make the matcher end
1002*9880d681SAndroid Build Coastguard Worker   // with an "Emit <index>" command.  This allows result generation stuff to be
1003*9880d681SAndroid Build Coastguard Worker   // shared and factored?
1004*9880d681SAndroid Build Coastguard Worker 
1005*9880d681SAndroid Build Coastguard Worker   // If the match succeeds, then we generate Pattern.
1006*9880d681SAndroid Build Coastguard Worker   Gen.EmitResultCode();
1007*9880d681SAndroid Build Coastguard Worker 
1008*9880d681SAndroid Build Coastguard Worker   // Unconditional match.
1009*9880d681SAndroid Build Coastguard Worker   return Gen.GetMatcher();
1010*9880d681SAndroid Build Coastguard Worker }
1011