blob: a19b9e4b95c7bcd142b8adf542d0705b0af73fa3 [file] [log] [blame]
Chris Lattnerb02cdaa2010-02-15 08:04:42 +00001//===- DAGISelMatcherGen.cpp - Matcher generator --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "DAGISelMatcher.h"
11#include "CodeGenDAGPatterns.h"
Jim Grosbachf17b0032011-03-11 02:19:02 +000012#include "CodeGenRegisters.h"
Chris Lattner469cfb92010-02-17 02:16:19 +000013#include "llvm/ADT/SmallVector.h"
Chris Lattnerb02cdaa2010-02-15 08:04:42 +000014#include "llvm/ADT/StringMap.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000015#include "llvm/TableGen/Error.h"
16#include "llvm/TableGen/Record.h"
Chris Lattner132df652010-02-21 03:22:59 +000017#include <utility>
Chris Lattnerb02cdaa2010-02-15 08:04:42 +000018using namespace llvm;
19
Chris Lattner16cd8cd2010-02-19 00:27:40 +000020
Chris Lattner132df652010-02-21 03:22:59 +000021/// getRegisterValueType - Look up and return the ValueType of the specified
22/// register. If the register is a member of multiple register classes which
23/// have different associated types, return MVT::Other.
24static MVT::SimpleValueType getRegisterValueType(Record *R,
25 const CodeGenTarget &T) {
26 bool FoundRC = false;
27 MVT::SimpleValueType VT = MVT::Other;
Jakob Stoklund Olesend7bc5c22011-06-15 04:50:36 +000028 const CodeGenRegister *Reg = T.getRegBank().getReg(R);
Jim Grosbach65586fe2010-12-21 16:16:00 +000029
David Blaikiedacea4b2014-12-03 19:58:45 +000030 for (const auto &RC : T.getRegBank().getRegClasses()) {
31 if (!RC.contains(Reg))
Chris Lattner132df652010-02-21 03:22:59 +000032 continue;
Jim Grosbach65586fe2010-12-21 16:16:00 +000033
Chris Lattner132df652010-02-21 03:22:59 +000034 if (!FoundRC) {
35 FoundRC = true;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +000036 ValueTypeByHwMode VVT = RC.getValueTypeNum(0);
37 if (VVT.isSimple())
38 VT = VVT.getSimple().SimpleTy;
Chris Lattner132df652010-02-21 03:22:59 +000039 continue;
Chris Lattner16cd8cd2010-02-19 00:27:40 +000040 }
Chris Lattner3d869722010-03-01 22:49:06 +000041
42 // If this occurs in multiple register classes, they all have to agree.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +000043#ifndef NDEBUG
44 ValueTypeByHwMode T = RC.getValueTypeNum(0);
45 assert((!T.isSimple() || T.getSimple().SimpleTy == VT) &&
46 "ValueType mismatch between register classes for this register");
47#endif
Chris Lattner132df652010-02-21 03:22:59 +000048 }
49 return VT;
50}
51
52
53namespace {
Chris Lattnerb02cdaa2010-02-15 08:04:42 +000054 class MatcherGen {
55 const PatternToMatch &Pattern;
56 const CodeGenDAGPatterns &CGP;
Jim Grosbach65586fe2010-12-21 16:16:00 +000057
Chris Lattnerb02cdaa2010-02-15 08:04:42 +000058 /// PatWithNoTypes - This is a clone of Pattern.getSrcPattern() that starts
59 /// out with all of the types removed. This allows us to insert type checks
60 /// as we scan the tree.
61 TreePatternNode *PatWithNoTypes;
Jim Grosbach65586fe2010-12-21 16:16:00 +000062
Chris Lattnerb02cdaa2010-02-15 08:04:42 +000063 /// VariableMap - A map from variable names ('$dst') to the recorded operand
64 /// number that they were captured as. These are biased by 1 to make
65 /// insertion easier.
66 StringMap<unsigned> VariableMap;
Jim Grosbach65586fe2010-12-21 16:16:00 +000067
Tim Northoverc807a172014-05-20 11:52:46 +000068 /// This maintains the recorded operand number that OPC_CheckComplexPattern
69 /// drops each sub-operand into. We don't want to insert these into
70 /// VariableMap because that leads to identity checking if they are
71 /// encountered multiple times. Biased by 1 like VariableMap for
72 /// consistency.
73 StringMap<unsigned> NamedComplexPatternOperands;
74
Chris Lattner16cd8cd2010-02-19 00:27:40 +000075 /// NextRecordedOperandNo - As we emit opcodes to record matched values in
76 /// the RecordedNodes array, this keeps track of which slot will be next to
77 /// record into.
Chris Lattnerb02cdaa2010-02-15 08:04:42 +000078 unsigned NextRecordedOperandNo;
Jim Grosbach65586fe2010-12-21 16:16:00 +000079
Chris Lattner132df652010-02-21 03:22:59 +000080 /// MatchedChainNodes - This maintains the position in the recorded nodes
81 /// array of all of the recorded input nodes that have chains.
82 SmallVector<unsigned, 2> MatchedChainNodes;
Chris Lattner29364372010-02-24 05:33:42 +000083
Chris Lattner0acbb712010-03-04 01:23:08 +000084 /// MatchedComplexPatterns - This maintains a list of all of the
Tim Northoverc807a172014-05-20 11:52:46 +000085 /// ComplexPatterns that we need to check. The second element of each pair
86 /// is the recorded operand number of the input node.
Chris Lattner0acbb712010-03-04 01:23:08 +000087 SmallVector<std::pair<const TreePatternNode*,
88 unsigned>, 2> MatchedComplexPatterns;
Jim Grosbach65586fe2010-12-21 16:16:00 +000089
Chris Lattner132df652010-02-21 03:22:59 +000090 /// PhysRegInputs - List list has an entry for each explicitly specified
91 /// physreg input to the pattern. The first elt is the Register node, the
92 /// second is the recorded slot number the input pattern match saved it in.
93 SmallVector<std::pair<Record*, unsigned>, 2> PhysRegInputs;
Jim Grosbach65586fe2010-12-21 16:16:00 +000094
Chris Lattner469cfb92010-02-17 02:16:19 +000095 /// Matcher - This is the top level of the generated matcher, the result.
Chris Lattner2c3f6492010-02-25 02:04:40 +000096 Matcher *TheMatcher;
Jim Grosbach65586fe2010-12-21 16:16:00 +000097
Chris Lattner469cfb92010-02-17 02:16:19 +000098 /// CurPredicate - As we emit matcher nodes, this points to the latest check
Chris Lattner186ad802010-02-18 02:53:41 +000099 /// which should have future checks stuck into its Next position.
Chris Lattner2c3f6492010-02-25 02:04:40 +0000100 Matcher *CurPredicate;
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000101 public:
102 MatcherGen(const PatternToMatch &pattern, const CodeGenDAGPatterns &cgp);
Jim Grosbach65586fe2010-12-21 16:16:00 +0000103
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000104 ~MatcherGen() {
105 delete PatWithNoTypes;
106 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000107
Chris Lattner053a28a2010-03-01 07:17:40 +0000108 bool EmitMatcherCode(unsigned Variant);
Chris Lattner7ed81692010-02-18 06:47:49 +0000109 void EmitResultCode();
Jim Grosbach65586fe2010-12-21 16:16:00 +0000110
Chris Lattner2c3f6492010-02-25 02:04:40 +0000111 Matcher *GetMatcher() const { return TheMatcher; }
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000112 private:
Chris Lattner2c3f6492010-02-25 02:04:40 +0000113 void AddMatcher(Matcher *NewNode);
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000114 void InferPossibleTypes(unsigned ForceMode);
Jim Grosbach65586fe2010-12-21 16:16:00 +0000115
Chris Lattner7ed81692010-02-18 06:47:49 +0000116 // Matcher Generation.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000117 void EmitMatchCode(const TreePatternNode *N, TreePatternNode *NodeNoTypes,
118 unsigned ForceMode);
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000119 void EmitLeafMatchCode(const TreePatternNode *N);
120 void EmitOperatorMatchCode(const TreePatternNode *N,
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000121 TreePatternNode *NodeNoTypes,
122 unsigned ForceMode);
Jim Grosbach65586fe2010-12-21 16:16:00 +0000123
Tim Northoverc807a172014-05-20 11:52:46 +0000124 /// If this is the first time a node with unique identifier Name has been
125 /// seen, record it. Otherwise, emit a check to make sure this is the same
126 /// node. Returns true if this is the first encounter.
Benjamin Kramerc321e532016-06-08 19:09:22 +0000127 bool recordUniqueNode(const std::string &Name);
Tim Northoverc807a172014-05-20 11:52:46 +0000128
Chris Lattner7ed81692010-02-18 06:47:49 +0000129 // Result Code Generation.
Chris Lattner132df652010-02-21 03:22:59 +0000130 unsigned getNamedArgumentSlot(StringRef Name) {
131 unsigned VarMapEntry = VariableMap[Name];
132 assert(VarMapEntry != 0 &&
133 "Variable referenced but not defined and not caught earlier!");
134 return VarMapEntry-1;
135 }
136
137 /// GetInstPatternNode - Get the pattern for an instruction.
138 const TreePatternNode *GetInstPatternNode(const DAGInstruction &Ins,
139 const TreePatternNode *N);
Jim Grosbach65586fe2010-12-21 16:16:00 +0000140
Chris Lattner7ed81692010-02-18 06:47:49 +0000141 void EmitResultOperand(const TreePatternNode *N,
Chris Lattner132df652010-02-21 03:22:59 +0000142 SmallVectorImpl<unsigned> &ResultOps);
143 void EmitResultOfNamedOperand(const TreePatternNode *N,
144 SmallVectorImpl<unsigned> &ResultOps);
Chris Lattner7ed81692010-02-18 06:47:49 +0000145 void EmitResultLeafAsOperand(const TreePatternNode *N,
Chris Lattner132df652010-02-21 03:22:59 +0000146 SmallVectorImpl<unsigned> &ResultOps);
Chris Lattner7ed81692010-02-18 06:47:49 +0000147 void EmitResultInstructionAsOperand(const TreePatternNode *N,
Chris Lattner132df652010-02-21 03:22:59 +0000148 SmallVectorImpl<unsigned> &ResultOps);
149 void EmitResultSDNodeXFormAsOperand(const TreePatternNode *N,
150 SmallVectorImpl<unsigned> &ResultOps);
151 };
Jim Grosbach65586fe2010-12-21 16:16:00 +0000152
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000153} // end anon namespace.
154
155MatcherGen::MatcherGen(const PatternToMatch &pattern,
156 const CodeGenDAGPatterns &cgp)
Chris Lattnerdc61962e2010-02-19 00:33:13 +0000157: Pattern(pattern), CGP(cgp), NextRecordedOperandNo(0),
Craig Topper24064772014-04-15 07:20:03 +0000158 TheMatcher(nullptr), CurPredicate(nullptr) {
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000159 // We need to produce the matcher tree for the patterns source pattern. To do
160 // this we need to match the structure as well as the types. To do the type
161 // matching, we want to figure out the fewest number of type checks we need to
162 // emit. For example, if there is only one integer type supported by a
163 // target, there should be no type comparisons at all for integer patterns!
164 //
165 // To figure out the fewest number of type checks needed, clone the pattern,
166 // remove the types, then perform type inference on the pattern as a whole.
167 // If there are unresolved types, emit an explicit check for those types,
168 // apply the type to the tree, then rerun type inference. Iterate until all
169 // types are resolved.
170 //
171 PatWithNoTypes = Pattern.getSrcPattern()->clone();
172 PatWithNoTypes->RemoveAllTypes();
Jim Grosbach65586fe2010-12-21 16:16:00 +0000173
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000174 // If there are types that are manifestly known, infer them.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000175 InferPossibleTypes(Pattern.ForceMode);
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000176}
177
178/// InferPossibleTypes - As we emit the pattern, we end up generating type
179/// checks and applying them to the 'PatWithNoTypes' tree. As we do this, we
180/// want to propagate implied types as far throughout the tree as possible so
181/// that we avoid doing redundant type checks. This does the type propagation.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000182void MatcherGen::InferPossibleTypes(unsigned ForceMode) {
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000183 // TP - Get *SOME* tree pattern, we don't care which. It is only used for
184 // diagnostics, which we know are impossible at this point.
185 TreePattern &TP = *CGP.pf_begin()->second;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000186 TP.getInfer().CodeGen = true;
187 TP.getInfer().ForceMode = ForceMode;
Jim Grosbach65586fe2010-12-21 16:16:00 +0000188
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000189 bool MadeChange = true;
190 while (MadeChange)
191 MadeChange = PatWithNoTypes->ApplyTypeConstraints(TP,
192 true/*Ignore reg constraints*/);
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000193}
194
195
Jim Grosbach65586fe2010-12-21 16:16:00 +0000196/// AddMatcher - Add a matcher node to the current graph we're building.
Chris Lattner2c3f6492010-02-25 02:04:40 +0000197void MatcherGen::AddMatcher(Matcher *NewNode) {
Craig Topper24064772014-04-15 07:20:03 +0000198 if (CurPredicate)
Chris Lattner186ad802010-02-18 02:53:41 +0000199 CurPredicate->setNext(NewNode);
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000200 else
Chris Lattner2c3f6492010-02-25 02:04:40 +0000201 TheMatcher = NewNode;
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000202 CurPredicate = NewNode;
203}
204
205
Chris Lattner7ed81692010-02-18 06:47:49 +0000206//===----------------------------------------------------------------------===//
207// Pattern Match Generation
208//===----------------------------------------------------------------------===//
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000209
210/// EmitLeafMatchCode - Generate matching code for leaf nodes.
211void MatcherGen::EmitLeafMatchCode(const TreePatternNode *N) {
212 assert(N->isLeaf() && "Not a leaf?");
Jim Grosbach65586fe2010-12-21 16:16:00 +0000213
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000214 // Direct match against an integer constant.
Sean Silvafb509ed2012-10-10 20:24:43 +0000215 if (IntInit *II = dyn_cast<IntInit>(N->getLeafValue())) {
Chris Lattnera44697c2010-03-01 07:27:07 +0000216 // If this is the root of the dag we're matching, we emit a redundant opcode
217 // check to ensure that this gets folded into the normal top-level
218 // OpcodeSwitch.
219 if (N == Pattern.getSrcPattern()) {
220 const SDNodeInfo &NI = CGP.getSDNodeInfo(CGP.getSDNodeNamed("imm"));
221 AddMatcher(new CheckOpcodeMatcher(NI));
222 }
223
Chris Lattner2c3f6492010-02-25 02:04:40 +0000224 return AddMatcher(new CheckIntegerMatcher(II->getValue()));
Chris Lattnera44697c2010-03-01 07:27:07 +0000225 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000226
Jakob Stoklund Olesen99ffcc82013-03-24 19:37:00 +0000227 // An UnsetInit represents a named node without any constraints.
Craig Topper1bf3d1f2015-04-22 02:09:45 +0000228 if (isa<UnsetInit>(N->getLeafValue())) {
Jakob Stoklund Olesen99ffcc82013-03-24 19:37:00 +0000229 assert(N->hasName() && "Unnamed ? leaf");
230 return;
231 }
232
Sean Silvafb509ed2012-10-10 20:24:43 +0000233 DefInit *DI = dyn_cast<DefInit>(N->getLeafValue());
Craig Topper24064772014-04-15 07:20:03 +0000234 if (!DI) {
Chris Lattner9e4e3352012-03-26 19:11:51 +0000235 errs() << "Unknown leaf kind: " << *N << "\n";
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000236 abort();
237 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000238
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000239 Record *LeafRec = DI->getDef();
Jakob Stoklund Olesend906b902013-03-23 20:35:01 +0000240
241 // A ValueType leaf node can represent a register when named, or itself when
242 // unnamed.
243 if (LeafRec->isSubClassOf("ValueType")) {
244 // A named ValueType leaf always matches: (add i32:$a, i32:$b).
245 if (N->hasName())
246 return;
247 // An unnamed ValueType as in (sext_inreg GPR:$foo, i8).
248 return AddMatcher(new CheckValueTypeMatcher(LeafRec->getName()));
249 }
250
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000251 if (// Handle register references. Nothing to do here, they always match.
Jim Grosbach65586fe2010-12-21 16:16:00 +0000252 LeafRec->isSubClassOf("RegisterClass") ||
Owen Andersona84be6c2011-06-27 21:06:21 +0000253 LeafRec->isSubClassOf("RegisterOperand") ||
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000254 LeafRec->isSubClassOf("PointerLikeRegClass") ||
Jakob Stoklund Olesen1c696462010-05-24 14:48:12 +0000255 LeafRec->isSubClassOf("SubRegIndex") ||
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000256 // Place holder for SRCVALUE nodes. Nothing to do here.
257 LeafRec->getName() == "srcvalue")
258 return;
Chris Lattner132df652010-02-21 03:22:59 +0000259
260 // If we have a physreg reference like (mul gpr:$src, EAX) then we need to
Jim Grosbach65586fe2010-12-21 16:16:00 +0000261 // record the register
Chris Lattner132df652010-02-21 03:22:59 +0000262 if (LeafRec->isSubClassOf("Register")) {
Matthias Braun4a86d452016-12-04 05:48:16 +0000263 AddMatcher(new RecordMatcher("physreg input "+LeafRec->getName().str(),
Chris Lattner4634d9b2010-03-01 02:24:17 +0000264 NextRecordedOperandNo));
Chris Lattner132df652010-02-21 03:22:59 +0000265 PhysRegInputs.push_back(std::make_pair(LeafRec, NextRecordedOperandNo++));
266 return;
267 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000268
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000269 if (LeafRec->isSubClassOf("CondCode"))
Chris Lattner2c3f6492010-02-25 02:04:40 +0000270 return AddMatcher(new CheckCondCodeMatcher(LeafRec->getName()));
Jim Grosbach65586fe2010-12-21 16:16:00 +0000271
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000272 if (LeafRec->isSubClassOf("ComplexPattern")) {
Chris Lattner7b390142010-02-17 00:11:30 +0000273 // We can't model ComplexPattern uses that don't have their name taken yet.
274 // The OPC_CheckComplexPattern operation implicitly records the results.
275 if (N->getName().empty()) {
James Y Knighte452e272015-05-11 22:17:13 +0000276 std::string S;
277 raw_string_ostream OS(S);
278 OS << "We expect complex pattern uses to have names: " << *N;
279 PrintFatalError(OS.str());
Chris Lattnere2347aa2010-02-16 23:16:25 +0000280 }
Chris Lattnera5a29852010-02-22 22:18:05 +0000281
Chris Lattner0acbb712010-03-04 01:23:08 +0000282 // Remember this ComplexPattern so that we can emit it after all the other
283 // structural matches are done.
Tim Northoverc807a172014-05-20 11:52:46 +0000284 unsigned InputOperand = VariableMap[N->getName()] - 1;
285 MatchedComplexPatterns.push_back(std::make_pair(N, InputOperand));
Chris Lattner8ad29342010-02-17 06:08:25 +0000286 return;
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000287 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000288
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000289 errs() << "Unknown leaf kind: " << *N << "\n";
290 abort();
291}
292
293void MatcherGen::EmitOperatorMatchCode(const TreePatternNode *N,
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000294 TreePatternNode *NodeNoTypes,
295 unsigned ForceMode) {
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000296 assert(!N->isLeaf() && "Not an operator?");
Tim Northoverc807a172014-05-20 11:52:46 +0000297
298 if (N->getOperator()->isSubClassOf("ComplexPattern")) {
299 // The "name" of a non-leaf complex pattern (MY_PAT $op1, $op2) is
300 // "MY_PAT:op1:op2". We should already have validated that the uses are
301 // consistent.
302 std::string PatternName = N->getOperator()->getName();
303 for (unsigned i = 0; i < N->getNumChildren(); ++i) {
304 PatternName += ":";
305 PatternName += N->getChild(i)->getName();
306 }
307
308 if (recordUniqueNode(PatternName)) {
309 auto NodeAndOpNum = std::make_pair(N, NextRecordedOperandNo - 1);
310 MatchedComplexPatterns.push_back(NodeAndOpNum);
311 }
312
313 return;
314 }
315
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000316 const SDNodeInfo &CInfo = CGP.getSDNodeInfo(N->getOperator());
Jim Grosbach65586fe2010-12-21 16:16:00 +0000317
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000318 // If this is an 'and R, 1234' where the operation is AND/OR and the RHS is
Lama Saba880e8782017-07-30 20:12:17 +0000319 // a constant without a predicate fn that has more than one bit set, handle
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000320 // this as a special case. This is usually for targets that have special
321 // handling of certain large constants (e.g. alpha with it's 8/16/32-bit
322 // handling stuff). Using these instructions is often far more efficient
323 // than materializing the constant. Unfortunately, both the instcombiner
324 // and the dag combiner can often infer that bits are dead, and thus drop
325 // them from the mask in the dag. For example, it might turn 'AND X, 255'
326 // into 'AND X, 254' if it knows the low bit is set. Emit code that checks
327 // to handle this.
Jim Grosbach65586fe2010-12-21 16:16:00 +0000328 if ((N->getOperator()->getName() == "and" ||
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000329 N->getOperator()->getName() == "or") &&
Chris Lattner132df652010-02-21 03:22:59 +0000330 N->getChild(1)->isLeaf() && N->getChild(1)->getPredicateFns().empty() &&
331 N->getPredicateFns().empty()) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000332 if (IntInit *II = dyn_cast<IntInit>(N->getChild(1)->getLeafValue())) {
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000333 if (!isPowerOf2_32(II->getValue())) { // Don't bother with single bits.
Chris Lattner90b1b9d2010-03-01 02:15:34 +0000334 // If this is at the root of the pattern, we emit a redundant
335 // CheckOpcode so that the following checks get factored properly under
336 // a single opcode check.
337 if (N == Pattern.getSrcPattern())
338 AddMatcher(new CheckOpcodeMatcher(CInfo));
339
340 // Emit the CheckAndImm/CheckOrImm node.
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000341 if (N->getOperator()->getName() == "and")
Chris Lattner2c3f6492010-02-25 02:04:40 +0000342 AddMatcher(new CheckAndImmMatcher(II->getValue()));
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000343 else
Chris Lattner2c3f6492010-02-25 02:04:40 +0000344 AddMatcher(new CheckOrImmMatcher(II->getValue()));
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000345
346 // Match the LHS of the AND as appropriate.
Chris Lattner2c3f6492010-02-25 02:04:40 +0000347 AddMatcher(new MoveChildMatcher(0));
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000348 EmitMatchCode(N->getChild(0), NodeNoTypes->getChild(0), ForceMode);
Chris Lattner2c3f6492010-02-25 02:04:40 +0000349 AddMatcher(new MoveParentMatcher());
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000350 return;
351 }
352 }
353 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000354
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000355 // Check that the current opcode lines up.
Chris Lattner278606b2010-02-27 21:48:43 +0000356 AddMatcher(new CheckOpcodeMatcher(CInfo));
Jim Grosbach65586fe2010-12-21 16:16:00 +0000357
Chris Lattner132df652010-02-21 03:22:59 +0000358 // If this node has memory references (i.e. is a load or store), tell the
359 // interpreter to capture them in the memref array.
360 if (N->NodeHasProperty(SDNPMemOperand, CGP))
Chris Lattner2c3f6492010-02-25 02:04:40 +0000361 AddMatcher(new RecordMemRefMatcher());
Jim Grosbach65586fe2010-12-21 16:16:00 +0000362
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000363 // If this node has a chain, then the chain is operand #0 is the SDNode, and
364 // the child numbers of the node are all offset by one.
365 unsigned OpNo = 0;
Chris Lattner6c132332010-02-16 19:19:58 +0000366 if (N->NodeHasProperty(SDNPHasChain, CGP)) {
Chris Lattner132df652010-02-21 03:22:59 +0000367 // Record the node and remember it in our chained nodes list.
Matthias Braun4a86d452016-12-04 05:48:16 +0000368 AddMatcher(new RecordMatcher("'" + N->getOperator()->getName().str() +
Chris Lattner4634d9b2010-03-01 02:24:17 +0000369 "' chained node",
370 NextRecordedOperandNo));
Chris Lattner469cfb92010-02-17 02:16:19 +0000371 // Remember all of the input chains our pattern will match.
Chris Lattner132df652010-02-21 03:22:59 +0000372 MatchedChainNodes.push_back(NextRecordedOperandNo++);
Jim Grosbach65586fe2010-12-21 16:16:00 +0000373
Chris Lattner022e5112010-02-17 01:34:15 +0000374 // Don't look at the input chain when matching the tree pattern to the
375 // SDNode.
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000376 OpNo = 1;
377
Chris Lattner6c132332010-02-16 19:19:58 +0000378 // If this node is not the root and the subtree underneath it produces a
379 // chain, then the result of matching the node is also produce a chain.
380 // Beyond that, this means that we're also folding (at least) the root node
381 // into the node that produce the chain (for example, matching
382 // "(add reg, (load ptr))" as a add_with_memory on X86). This is
383 // problematic, if the 'reg' node also uses the load (say, its chain).
384 // Graphically:
385 //
386 // [LD]
387 // ^ ^
388 // | \ DAG's like cheese.
389 // / |
390 // / [YY]
391 // | ^
392 // [XX]--/
393 //
394 // It would be invalid to fold XX and LD. In this case, folding the two
395 // nodes together would induce a cycle in the DAG, making it a 'cyclic DAG'
396 // To prevent this, we emit a dynamic check for legality before allowing
397 // this to be folded.
398 //
399 const TreePatternNode *Root = Pattern.getSrcPattern();
400 if (N != Root) { // Not the root of the pattern.
Chris Lattneraa7d3e02010-02-16 06:10:58 +0000401 // If there is a node between the root and this node, then we definitely
402 // need to emit the check.
403 bool NeedCheck = !Root->hasChild(N);
Jim Grosbach65586fe2010-12-21 16:16:00 +0000404
Chris Lattneraa7d3e02010-02-16 06:10:58 +0000405 // If it *is* an immediate child of the root, we can still need a check if
406 // the root SDNode has multiple inputs. For us, this means that it is an
407 // intrinsic, has multiple operands, or has other inputs like chain or
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000408 // glue).
Chris Lattneraa7d3e02010-02-16 06:10:58 +0000409 if (!NeedCheck) {
410 const SDNodeInfo &PInfo = CGP.getSDNodeInfo(Root->getOperator());
411 NeedCheck =
412 Root->getOperator() == CGP.get_intrinsic_void_sdnode() ||
413 Root->getOperator() == CGP.get_intrinsic_w_chain_sdnode() ||
414 Root->getOperator() == CGP.get_intrinsic_wo_chain_sdnode() ||
415 PInfo.getNumOperands() > 1 ||
416 PInfo.hasProperty(SDNPHasChain) ||
Chris Lattner2a0a3b42010-12-23 18:28:41 +0000417 PInfo.hasProperty(SDNPInGlue) ||
418 PInfo.hasProperty(SDNPOptInGlue);
Chris Lattneraa7d3e02010-02-16 06:10:58 +0000419 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000420
Chris Lattneraa7d3e02010-02-16 06:10:58 +0000421 if (NeedCheck)
Chris Lattner2c3f6492010-02-25 02:04:40 +0000422 AddMatcher(new CheckFoldableChainNodeMatcher());
Chris Lattneraa7d3e02010-02-16 06:10:58 +0000423 }
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000424 }
Chris Lattner29364372010-02-24 05:33:42 +0000425
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000426 // If this node has an output glue and isn't the root, remember it.
Chris Lattner2a0a3b42010-12-23 18:28:41 +0000427 if (N->NodeHasProperty(SDNPOutGlue, CGP) &&
Chris Lattner29364372010-02-24 05:33:42 +0000428 N != Pattern.getSrcPattern()) {
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000429 // TODO: This redundantly records nodes with both glues and chains.
Jim Grosbach65586fe2010-12-21 16:16:00 +0000430
Chris Lattner29364372010-02-24 05:33:42 +0000431 // Record the node and remember it in our chained nodes list.
Matthias Braun4a86d452016-12-04 05:48:16 +0000432 AddMatcher(new RecordMatcher("'" + N->getOperator()->getName().str() +
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000433 "' glue output node",
Chris Lattner4634d9b2010-03-01 02:24:17 +0000434 NextRecordedOperandNo));
Chris Lattner29364372010-02-24 05:33:42 +0000435 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000436
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000437 // If this node is known to have an input glue or if it *might* have an input
438 // glue, capture it as the glue input of the pattern.
Chris Lattner2a0a3b42010-12-23 18:28:41 +0000439 if (N->NodeHasProperty(SDNPOptInGlue, CGP) ||
440 N->NodeHasProperty(SDNPInGlue, CGP))
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000441 AddMatcher(new CaptureGlueInputMatcher());
Jim Grosbach65586fe2010-12-21 16:16:00 +0000442
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000443 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) {
444 // Get the code suitable for matching this child. Move to the child, check
445 // it then move back to the parent.
Chris Lattner2c3f6492010-02-25 02:04:40 +0000446 AddMatcher(new MoveChildMatcher(OpNo));
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000447 EmitMatchCode(N->getChild(i), NodeNoTypes->getChild(i), ForceMode);
Chris Lattner2c3f6492010-02-25 02:04:40 +0000448 AddMatcher(new MoveParentMatcher());
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000449 }
450}
451
Benjamin Kramerc321e532016-06-08 19:09:22 +0000452bool MatcherGen::recordUniqueNode(const std::string &Name) {
Tim Northoverc807a172014-05-20 11:52:46 +0000453 unsigned &VarMapEntry = VariableMap[Name];
454 if (VarMapEntry == 0) {
455 // If it is a named node, we must emit a 'Record' opcode.
456 AddMatcher(new RecordMatcher("$" + Name, NextRecordedOperandNo));
457 VarMapEntry = ++NextRecordedOperandNo;
458 return true;
459 }
460
461 // If we get here, this is a second reference to a specific name. Since
462 // we already have checked that the first reference is valid, we don't
463 // have to recursively match it, just check that it's the same as the
464 // previously named thing.
465 AddMatcher(new CheckSameMatcher(VarMapEntry-1));
466 return false;
467}
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000468
469void MatcherGen::EmitMatchCode(const TreePatternNode *N,
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000470 TreePatternNode *NodeNoTypes,
471 unsigned ForceMode) {
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000472 // If N and NodeNoTypes don't agree on a type, then this is a case where we
Craig Topper336e1f12014-01-25 17:34:23 +0000473 // need to do a type check. Emit the check, apply the type to NodeNoTypes and
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000474 // reinfer any correlated types.
Chris Lattner6c2d1782010-03-24 00:41:19 +0000475 SmallVector<unsigned, 2> ResultsToTypeCheck;
Jim Grosbach65586fe2010-12-21 16:16:00 +0000476
Chris Lattner6c2d1782010-03-24 00:41:19 +0000477 for (unsigned i = 0, e = NodeNoTypes->getNumTypes(); i != e; ++i) {
478 if (NodeNoTypes->getExtType(i) == N->getExtType(i)) continue;
479 NodeNoTypes->setType(i, N->getExtType(i));
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000480 InferPossibleTypes(ForceMode);
Chris Lattner6c2d1782010-03-24 00:41:19 +0000481 ResultsToTypeCheck.push_back(i);
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000482 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000483
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000484 // If this node has a name associated with it, capture it in VariableMap. If
485 // we already saw this in the pattern, emit code to verify dagness.
Tim Northoverc807a172014-05-20 11:52:46 +0000486 if (!N->getName().empty())
487 if (!recordUniqueNode(N->getName()))
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000488 return;
Jim Grosbach65586fe2010-12-21 16:16:00 +0000489
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000490 if (N->isLeaf())
491 EmitLeafMatchCode(N);
492 else
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000493 EmitOperatorMatchCode(N, NodeNoTypes, ForceMode);
Jim Grosbach65586fe2010-12-21 16:16:00 +0000494
Chris Lattnera1603892010-03-07 07:20:49 +0000495 // If there are node predicates for this node, generate their checks.
496 for (unsigned i = 0, e = N->getPredicateFns().size(); i != e; ++i)
497 AddMatcher(new CheckPredicateMatcher(N->getPredicateFns()[i]));
Jim Grosbach65586fe2010-12-21 16:16:00 +0000498
Chris Lattner6c2d1782010-03-24 00:41:19 +0000499 for (unsigned i = 0, e = ResultsToTypeCheck.size(); i != e; ++i)
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000500 AddMatcher(new CheckTypeMatcher(N->getSimpleType(ResultsToTypeCheck[i]),
Chris Lattner6c2d1782010-03-24 00:41:19 +0000501 ResultsToTypeCheck[i]));
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000502}
503
Chris Lattner053a28a2010-03-01 07:17:40 +0000504/// EmitMatcherCode - Generate the code that matches the predicate of this
505/// pattern for the specified Variant. If the variant is invalid this returns
506/// true and does not generate code, if it is valid, it returns false.
507bool MatcherGen::EmitMatcherCode(unsigned Variant) {
508 // If the root of the pattern is a ComplexPattern and if it is specified to
509 // match some number of root opcodes, these are considered to be our variants.
510 // Depending on which variant we're generating code for, emit the root opcode
511 // check.
512 if (const ComplexPattern *CP =
513 Pattern.getSrcPattern()->getComplexPatternInfo(CGP)) {
Chris Lattner053a28a2010-03-01 07:17:40 +0000514 const std::vector<Record*> &OpNodes = CP->getRootNodes();
Chris Lattner1e634e32010-03-01 22:29:19 +0000515 assert(!OpNodes.empty() &&"Complex Pattern must specify what it can match");
516 if (Variant >= OpNodes.size()) return true;
Jim Grosbach65586fe2010-12-21 16:16:00 +0000517
Chris Lattner1e634e32010-03-01 22:29:19 +0000518 AddMatcher(new CheckOpcodeMatcher(CGP.getSDNodeInfo(OpNodes[Variant])));
Chris Lattner053a28a2010-03-01 07:17:40 +0000519 } else {
520 if (Variant != 0) return true;
521 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000522
Chris Lattner0e2cedb2010-03-04 01:25:36 +0000523 // Emit the matcher for the pattern structure and types.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000524 EmitMatchCode(Pattern.getSrcPattern(), PatWithNoTypes, Pattern.ForceMode);
Jim Grosbach65586fe2010-12-21 16:16:00 +0000525
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000526 // If the pattern has a predicate on it (e.g. only enabled when a subtarget
527 // feature is around, do the check).
528 if (!Pattern.getPredicateCheck().empty())
Chris Lattner053a28a2010-03-01 07:17:40 +0000529 AddMatcher(new CheckPatternPredicateMatcher(Pattern.getPredicateCheck()));
Jim Grosbach65586fe2010-12-21 16:16:00 +0000530
Chris Lattner0acbb712010-03-04 01:23:08 +0000531 // Now that we've completed the structural type match, emit any ComplexPattern
532 // checks (e.g. addrmode matches). We emit this after the structural match
533 // because they are generally more expensive to evaluate and more difficult to
534 // factor.
Chris Lattner0acbb712010-03-04 01:23:08 +0000535 for (unsigned i = 0, e = MatchedComplexPatterns.size(); i != e; ++i) {
536 const TreePatternNode *N = MatchedComplexPatterns[i].first;
Jim Grosbach65586fe2010-12-21 16:16:00 +0000537
Chris Lattner0acbb712010-03-04 01:23:08 +0000538 // Remember where the results of this match get stuck.
Tim Northoverc807a172014-05-20 11:52:46 +0000539 if (N->isLeaf()) {
540 NamedComplexPatternOperands[N->getName()] = NextRecordedOperandNo + 1;
541 } else {
542 unsigned CurOp = NextRecordedOperandNo;
543 for (unsigned i = 0; i < N->getNumChildren(); ++i) {
544 NamedComplexPatternOperands[N->getChild(i)->getName()] = CurOp + 1;
545 CurOp += N->getChild(i)->getNumMIResults(CGP);
546 }
547 }
Chris Lattner0acbb712010-03-04 01:23:08 +0000548
549 // Get the slot we recorded the value in from the name on the node.
Tim Northoverc807a172014-05-20 11:52:46 +0000550 unsigned RecNodeEntry = MatchedComplexPatterns[i].second;
Jim Grosbach65586fe2010-12-21 16:16:00 +0000551
Tim Northoverc807a172014-05-20 11:52:46 +0000552 const ComplexPattern &CP = *N->getComplexPatternInfo(CGP);
Jim Grosbach65586fe2010-12-21 16:16:00 +0000553
Chris Lattner0acbb712010-03-04 01:23:08 +0000554 // Emit a CheckComplexPat operation, which does the match (aborting if it
555 // fails) and pushes the matched operands onto the recorded nodes list.
556 AddMatcher(new CheckComplexPatMatcher(CP, RecNodeEntry,
557 N->getName(), NextRecordedOperandNo));
Jim Grosbach65586fe2010-12-21 16:16:00 +0000558
Chris Lattner0acbb712010-03-04 01:23:08 +0000559 // Record the right number of operands.
560 NextRecordedOperandNo += CP.getNumOperands();
561 if (CP.hasProperty(SDNPHasChain)) {
562 // If the complex pattern has a chain, then we need to keep track of the
563 // fact that we just recorded a chain input. The chain input will be
564 // matched as the last operand of the predicate if it was successful.
565 ++NextRecordedOperandNo; // Chained node operand.
Jim Grosbach65586fe2010-12-21 16:16:00 +0000566
Chris Lattner0acbb712010-03-04 01:23:08 +0000567 // It is the last operand recorded.
568 assert(NextRecordedOperandNo > 1 &&
569 "Should have recorded input/result chains at least!");
570 MatchedChainNodes.push_back(NextRecordedOperandNo-1);
571 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000572
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000573 // TODO: Complex patterns can't have output glues, if they did, we'd want
Chris Lattner0acbb712010-03-04 01:23:08 +0000574 // to record them.
575 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000576
Chris Lattner053a28a2010-03-01 07:17:40 +0000577 return false;
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000578}
579
580
Chris Lattner7ed81692010-02-18 06:47:49 +0000581//===----------------------------------------------------------------------===//
582// Node Result Generation
583//===----------------------------------------------------------------------===//
584
Chris Lattner132df652010-02-21 03:22:59 +0000585void MatcherGen::EmitResultOfNamedOperand(const TreePatternNode *N,
586 SmallVectorImpl<unsigned> &ResultOps){
587 assert(!N->getName().empty() && "Operand not named!");
Jim Grosbach65586fe2010-12-21 16:16:00 +0000588
Tim Northoverc807a172014-05-20 11:52:46 +0000589 if (unsigned SlotNo = NamedComplexPatternOperands[N->getName()]) {
590 // Complex operands have already been completely selected, just find the
591 // right slot ant add the arguments directly.
592 for (unsigned i = 0; i < N->getNumMIResults(CGP); ++i)
593 ResultOps.push_back(SlotNo - 1 + i);
Jim Grosbach65586fe2010-12-21 16:16:00 +0000594
Chris Lattner132df652010-02-21 03:22:59 +0000595 return;
596 }
597
Chris Lattner0acbb712010-03-04 01:23:08 +0000598 unsigned SlotNo = getNamedArgumentSlot(N->getName());
599
Chris Lattner132df652010-02-21 03:22:59 +0000600 // If this is an 'imm' or 'fpimm' node, make sure to convert it to the target
601 // version of the immediate so that it doesn't get selected due to some other
602 // node use.
603 if (!N->isLeaf()) {
604 StringRef OperatorName = N->getOperator()->getName();
605 if (OperatorName == "imm" || OperatorName == "fpimm") {
Chris Lattner2c3f6492010-02-25 02:04:40 +0000606 AddMatcher(new EmitConvertToTargetMatcher(SlotNo));
Chris Lattner132df652010-02-21 03:22:59 +0000607 ResultOps.push_back(NextRecordedOperandNo++);
608 return;
609 }
610 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000611
Tim Northoverc807a172014-05-20 11:52:46 +0000612 for (unsigned i = 0; i < N->getNumMIResults(CGP); ++i)
613 ResultOps.push_back(SlotNo + i);
Chris Lattner132df652010-02-21 03:22:59 +0000614}
615
Chris Lattner7ed81692010-02-18 06:47:49 +0000616void MatcherGen::EmitResultLeafAsOperand(const TreePatternNode *N,
Chris Lattner132df652010-02-21 03:22:59 +0000617 SmallVectorImpl<unsigned> &ResultOps) {
Chris Lattner42a7ba72010-02-18 22:03:03 +0000618 assert(N->isLeaf() && "Must be a leaf");
Jim Grosbach65586fe2010-12-21 16:16:00 +0000619
Sean Silvafb509ed2012-10-10 20:24:43 +0000620 if (IntInit *II = dyn_cast<IntInit>(N->getLeafValue())) {
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000621 AddMatcher(new EmitIntegerMatcher(II->getValue(), N->getSimpleType(0)));
Chris Lattner132df652010-02-21 03:22:59 +0000622 ResultOps.push_back(NextRecordedOperandNo++);
Chris Lattner42a7ba72010-02-18 22:03:03 +0000623 return;
624 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000625
Chris Lattner42a7ba72010-02-18 22:03:03 +0000626 // If this is an explicit register reference, handle it.
Sean Silvafb509ed2012-10-10 20:24:43 +0000627 if (DefInit *DI = dyn_cast<DefInit>(N->getLeafValue())) {
Owen Andersona84be6c2011-06-27 21:06:21 +0000628 Record *Def = DI->getDef();
629 if (Def->isSubClassOf("Register")) {
Jakob Stoklund Olesen8e188be2011-06-18 04:26:06 +0000630 const CodeGenRegister *Reg =
Owen Andersona84be6c2011-06-27 21:06:21 +0000631 CGP.getTargetInfo().getRegBank().getReg(Def);
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000632 AddMatcher(new EmitRegisterMatcher(Reg, N->getSimpleType(0)));
Chris Lattner132df652010-02-21 03:22:59 +0000633 ResultOps.push_back(NextRecordedOperandNo++);
Chris Lattner42a7ba72010-02-18 22:03:03 +0000634 return;
635 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000636
Owen Andersona84be6c2011-06-27 21:06:21 +0000637 if (Def->getName() == "zero_reg") {
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000638 AddMatcher(new EmitRegisterMatcher(nullptr, N->getSimpleType(0)));
Chris Lattner132df652010-02-21 03:22:59 +0000639 ResultOps.push_back(NextRecordedOperandNo++);
Chris Lattner42a7ba72010-02-18 22:03:03 +0000640 return;
641 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000642
Chris Lattner132df652010-02-21 03:22:59 +0000643 // Handle a reference to a register class. This is used
644 // in COPY_TO_SUBREG instructions.
Owen Andersona84be6c2011-06-27 21:06:21 +0000645 if (Def->isSubClassOf("RegisterOperand"))
646 Def = Def->getValueAsDef("RegClass");
647 if (Def->isSubClassOf("RegisterClass")) {
648 std::string Value = getQualifiedName(Def) + "RegClassID";
Chris Lattner2c3f6492010-02-25 02:04:40 +0000649 AddMatcher(new EmitStringIntegerMatcher(Value, MVT::i32));
Chris Lattner132df652010-02-21 03:22:59 +0000650 ResultOps.push_back(NextRecordedOperandNo++);
651 return;
Chris Lattner42a7ba72010-02-18 22:03:03 +0000652 }
Jakob Stoklund Olesen1c696462010-05-24 14:48:12 +0000653
654 // Handle a subregister index. This is used for INSERT_SUBREG etc.
Owen Andersona84be6c2011-06-27 21:06:21 +0000655 if (Def->isSubClassOf("SubRegIndex")) {
656 std::string Value = getQualifiedName(Def);
Jakob Stoklund Olesen1c696462010-05-24 14:48:12 +0000657 AddMatcher(new EmitStringIntegerMatcher(Value, MVT::i32));
658 ResultOps.push_back(NextRecordedOperandNo++);
659 return;
660 }
Chris Lattner42a7ba72010-02-18 22:03:03 +0000661 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000662
Chris Lattner7ed81692010-02-18 06:47:49 +0000663 errs() << "unhandled leaf node: \n";
664 N->dump();
665}
666
Chris Lattner132df652010-02-21 03:22:59 +0000667/// GetInstPatternNode - Get the pattern for an instruction.
Jim Grosbach65586fe2010-12-21 16:16:00 +0000668///
Chris Lattner132df652010-02-21 03:22:59 +0000669const TreePatternNode *MatcherGen::
670GetInstPatternNode(const DAGInstruction &Inst, const TreePatternNode *N) {
671 const TreePattern *InstPat = Inst.getPattern();
Jim Grosbach65586fe2010-12-21 16:16:00 +0000672
Chris Lattner132df652010-02-21 03:22:59 +0000673 // FIXME2?: Assume actual pattern comes before "implicit".
674 TreePatternNode *InstPatNode;
675 if (InstPat)
676 InstPatNode = InstPat->getTree(0);
677 else if (/*isRoot*/ N == Pattern.getDstPattern())
678 InstPatNode = Pattern.getSrcPattern();
679 else
Craig Topper24064772014-04-15 07:20:03 +0000680 return nullptr;
Jim Grosbach65586fe2010-12-21 16:16:00 +0000681
Chris Lattner132df652010-02-21 03:22:59 +0000682 if (InstPatNode && !InstPatNode->isLeaf() &&
683 InstPatNode->getOperator()->getName() == "set")
684 InstPatNode = InstPatNode->getChild(InstPatNode->getNumChildren()-1);
Jim Grosbach65586fe2010-12-21 16:16:00 +0000685
Chris Lattner132df652010-02-21 03:22:59 +0000686 return InstPatNode;
687}
688
Cameron Zwarich8b76e012011-05-19 21:13:30 +0000689static bool
690mayInstNodeLoadOrStore(const TreePatternNode *N,
691 const CodeGenDAGPatterns &CGP) {
692 Record *Op = N->getOperator();
693 const CodeGenTarget &CGT = CGP.getTargetInfo();
694 CodeGenInstruction &II = CGT.getInstruction(Op);
695 return II.mayLoad || II.mayStore;
696}
697
698static unsigned
699numNodesThatMayLoadOrStore(const TreePatternNode *N,
700 const CodeGenDAGPatterns &CGP) {
701 if (N->isLeaf())
702 return 0;
703
704 Record *OpRec = N->getOperator();
705 if (!OpRec->isSubClassOf("Instruction"))
706 return 0;
707
708 unsigned Count = 0;
709 if (mayInstNodeLoadOrStore(N, CGP))
710 ++Count;
711
712 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
713 Count += numNodesThatMayLoadOrStore(N->getChild(i), CGP);
714
715 return Count;
716}
717
Chris Lattner132df652010-02-21 03:22:59 +0000718void MatcherGen::
719EmitResultInstructionAsOperand(const TreePatternNode *N,
720 SmallVectorImpl<unsigned> &OutputOps) {
Chris Lattner7ed81692010-02-18 06:47:49 +0000721 Record *Op = N->getOperator();
722 const CodeGenTarget &CGT = CGP.getTargetInfo();
Chris Lattner9aec14b2010-03-19 00:07:20 +0000723 CodeGenInstruction &II = CGT.getInstruction(Op);
Chris Lattner7ed81692010-02-18 06:47:49 +0000724 const DAGInstruction &Inst = CGP.getInstruction(Op);
Jim Grosbach65586fe2010-12-21 16:16:00 +0000725
Matt Arsenault3c0714a2014-11-02 23:46:47 +0000726 // If we can, get the pattern for the instruction we're generating. We derive
Chris Lattner132df652010-02-21 03:22:59 +0000727 // a variety of information from this pattern, such as whether it has a chain.
728 //
729 // FIXME2: This is extremely dubious for several reasons, not the least of
730 // which it gives special status to instructions with patterns that Pat<>
731 // nodes can't duplicate.
732 const TreePatternNode *InstPatNode = GetInstPatternNode(Inst, N);
733
Jim Grosbach65586fe2010-12-21 16:16:00 +0000734 // NodeHasChain - Whether the instruction node we're creating takes chains.
Chris Lattner132df652010-02-21 03:22:59 +0000735 bool NodeHasChain = InstPatNode &&
736 InstPatNode->TreeHasProperty(SDNPHasChain, CGP);
Jim Grosbach65586fe2010-12-21 16:16:00 +0000737
Tim Northovere5629962012-06-26 18:46:28 +0000738 // Instructions which load and store from memory should have a chain,
739 // regardless of whether they happen to have an internal pattern saying so.
740 if (Pattern.getSrcPattern()->TreeHasProperty(SDNPHasChain, CGP)
741 && (II.hasCtrlDep || II.mayLoad || II.mayStore || II.canFoldAsLoad ||
742 II.hasSideEffects))
743 NodeHasChain = true;
744
Chris Lattner132df652010-02-21 03:22:59 +0000745 bool isRoot = N == Pattern.getDstPattern();
746
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000747 // TreeHasOutGlue - True if this tree has glue.
748 bool TreeHasInGlue = false, TreeHasOutGlue = false;
Chris Lattner132df652010-02-21 03:22:59 +0000749 if (isRoot) {
750 const TreePatternNode *SrcPat = Pattern.getSrcPattern();
Chris Lattner2a0a3b42010-12-23 18:28:41 +0000751 TreeHasInGlue = SrcPat->TreeHasProperty(SDNPOptInGlue, CGP) ||
752 SrcPat->TreeHasProperty(SDNPInGlue, CGP);
Jim Grosbach65586fe2010-12-21 16:16:00 +0000753
Chris Lattner132df652010-02-21 03:22:59 +0000754 // FIXME2: this is checking the entire pattern, not just the node in
755 // question, doing this just for the root seems like a total hack.
Chris Lattner2a0a3b42010-12-23 18:28:41 +0000756 TreeHasOutGlue = SrcPat->TreeHasProperty(SDNPOutGlue, CGP);
Chris Lattner132df652010-02-21 03:22:59 +0000757 }
758
759 // NumResults - This is the number of results produced by the instruction in
760 // the "outs" list.
Jim Grosbach65586fe2010-12-21 16:16:00 +0000761 unsigned NumResults = Inst.getNumResults();
Chris Lattner7ed81692010-02-18 06:47:49 +0000762
Matt Arsenaulteb492162014-11-02 23:46:51 +0000763 // Number of operands we know the output instruction must have. If it is
764 // variadic, we could have more operands.
765 unsigned NumFixedOperands = II.Operands.size();
Jim Grosbach65586fe2010-12-21 16:16:00 +0000766
Matt Arsenaulteb492162014-11-02 23:46:51 +0000767 SmallVector<unsigned, 8> InstOps;
768
769 // Loop over all of the fixed operands of the instruction pattern, emitting
770 // code to fill them all in. The node 'N' usually has number children equal to
771 // the number of input operands of the instruction. However, in cases where
772 // there are predicate operands for an instruction, we need to fill in the
773 // 'execute always' values. Match up the node operands to the instruction
774 // operands to do this.
775 unsigned ChildNo = 0;
776 for (unsigned InstOpNo = NumResults, e = NumFixedOperands;
777 InstOpNo != e; ++InstOpNo) {
Chris Lattner7ed81692010-02-18 06:47:49 +0000778 // Determine what to emit for this operand.
Chris Lattnerd8adec72010-11-01 04:03:32 +0000779 Record *OperandNode = II.Operands[InstOpNo].Rec;
Tom Stellardb7246a72012-09-06 14:15:52 +0000780 if (OperandNode->isSubClassOf("OperandWithDefaultOps") &&
Chris Lattner7ed81692010-02-18 06:47:49 +0000781 !CGP.getDefaultOperand(OperandNode).DefaultOps.empty()) {
782 // This is a predicate or optional def operand; emit the
783 // 'default ops' operands.
Eric Christophere264e092010-08-10 23:46:20 +0000784 const DAGDefaultOperand &DefaultOp
Jim Grosbach65586fe2010-12-21 16:16:00 +0000785 = CGP.getDefaultOperand(OperandNode);
Chris Lattner7ed81692010-02-18 06:47:49 +0000786 for (unsigned i = 0, e = DefaultOp.DefaultOps.size(); i != e; ++i)
Chris Lattner132df652010-02-21 03:22:59 +0000787 EmitResultOperand(DefaultOp.DefaultOps[i], InstOps);
Chris Lattner7ed81692010-02-18 06:47:49 +0000788 continue;
789 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000790
Chris Lattner7ed81692010-02-18 06:47:49 +0000791 // Otherwise this is a normal operand or a predicate operand without
792 // 'execute always'; emit it.
Jim Grosbach65586fe2010-12-21 16:16:00 +0000793
Ulrich Weigande618abd2013-03-19 19:51:09 +0000794 // For operands with multiple sub-operands we may need to emit
795 // multiple child patterns to cover them all. However, ComplexPattern
796 // children may themselves emit multiple MI operands.
797 unsigned NumSubOps = 1;
798 if (OperandNode->isSubClassOf("Operand")) {
799 DagInit *MIOpInfo = OperandNode->getValueAsDag("MIOperandInfo");
800 if (unsigned NumArgs = MIOpInfo->getNumArgs())
801 NumSubOps = NumArgs;
802 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000803
Ulrich Weigande618abd2013-03-19 19:51:09 +0000804 unsigned FinalNumOps = InstOps.size() + NumSubOps;
805 while (InstOps.size() < FinalNumOps) {
806 const TreePatternNode *Child = N->getChild(ChildNo);
807 unsigned BeforeAddingNumOps = InstOps.size();
808 EmitResultOperand(Child, InstOps);
809 assert(InstOps.size() > BeforeAddingNumOps && "Didn't add any operands");
810
811 // If the operand is an instruction and it produced multiple results, just
812 // take the first one.
813 if (!Child->isLeaf() && Child->getOperator()->isSubClassOf("Instruction"))
814 InstOps.resize(BeforeAddingNumOps+1);
815
816 ++ChildNo;
817 }
Chris Lattner7ed81692010-02-18 06:47:49 +0000818 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000819
Matt Arsenaulteb492162014-11-02 23:46:51 +0000820 // If this is a variadic output instruction (i.e. REG_SEQUENCE), we can't
821 // expand suboperands, use default operands, or other features determined from
822 // the CodeGenInstruction after the fixed operands, which were handled
823 // above. Emit the remaining instructions implicitly added by the use for
824 // variable_ops.
825 if (II.Operands.isVariadic) {
826 for (unsigned I = ChildNo, E = N->getNumChildren(); I < E; ++I)
827 EmitResultOperand(N->getChild(I), InstOps);
828 }
829
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000830 // If this node has input glue or explicitly specified input physregs, we
831 // need to add chained and glued copyfromreg nodes and materialize the glue
Chris Lattner132df652010-02-21 03:22:59 +0000832 // input.
833 if (isRoot && !PhysRegInputs.empty()) {
834 // Emit all of the CopyToReg nodes for the input physical registers. These
835 // occur in patterns like (mul:i8 AL:i8, GR8:i8:$src).
836 for (unsigned i = 0, e = PhysRegInputs.size(); i != e; ++i)
Chris Lattner2c3f6492010-02-25 02:04:40 +0000837 AddMatcher(new EmitCopyToRegMatcher(PhysRegInputs[i].second,
Chris Lattner63a627c2010-03-18 23:57:40 +0000838 PhysRegInputs[i].first));
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000839 // Even if the node has no other glue inputs, the resultant node must be
840 // glued to the CopyFromReg nodes we just generated.
841 TreeHasInGlue = true;
Chris Lattner132df652010-02-21 03:22:59 +0000842 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000843
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000844 // Result order: node results, chain, glue
Jim Grosbach65586fe2010-12-21 16:16:00 +0000845
Chris Lattner132df652010-02-21 03:22:59 +0000846 // Determine the result types.
847 SmallVector<MVT::SimpleValueType, 4> ResultVTs;
Chris Lattnerd44966f2010-03-27 19:15:02 +0000848 for (unsigned i = 0, e = N->getNumTypes(); i != e; ++i)
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000849 ResultVTs.push_back(N->getSimpleType(i));
Jim Grosbach65586fe2010-12-21 16:16:00 +0000850
Chris Lattner132df652010-02-21 03:22:59 +0000851 // If this is the root instruction of a pattern that has physical registers in
852 // its result pattern, add output VTs for them. For example, X86 has:
853 // (set AL, (mul ...))
854 // This also handles implicit results like:
855 // (implicit EFLAGS)
Chris Lattner725d3f62010-03-27 20:45:15 +0000856 if (isRoot && !Pattern.getDstRegs().empty()) {
Chris Lattner63a627c2010-03-18 23:57:40 +0000857 // If the root came from an implicit def in the instruction handling stuff,
858 // don't re-add it.
Craig Topper24064772014-04-15 07:20:03 +0000859 Record *HandledReg = nullptr;
Chris Lattner7bc5d9b2010-03-27 20:09:24 +0000860 if (II.HasOneImplicitDefWithKnownVT(CGT) != MVT::Other)
Chris Lattner63a627c2010-03-18 23:57:40 +0000861 HandledReg = II.ImplicitDefs[0];
Jim Grosbach65586fe2010-12-21 16:16:00 +0000862
Simon Pilgrim48bed532017-06-19 13:24:12 +0000863 for (Record *Reg : Pattern.getDstRegs()) {
Chris Lattner63a627c2010-03-18 23:57:40 +0000864 if (!Reg->isSubClassOf("Register") || Reg == HandledReg) continue;
865 ResultVTs.push_back(getRegisterValueType(Reg, CGT));
866 }
Chris Lattner132df652010-02-21 03:22:59 +0000867 }
Chris Lattner132df652010-02-21 03:22:59 +0000868
Chris Lattnerbb34b4e2010-03-19 05:34:15 +0000869 // If this is the root of the pattern and the pattern we're matching includes
870 // a node that is variadic, mark the generated node as variadic so that it
871 // gets the excess operands from the input DAG.
Chris Lattner132df652010-02-21 03:22:59 +0000872 int NumFixedArityOperands = -1;
Chris Lattnerbb34b4e2010-03-19 05:34:15 +0000873 if (isRoot &&
Matt Arsenaulteb492162014-11-02 23:46:51 +0000874 Pattern.getSrcPattern()->NodeHasProperty(SDNPVariadic, CGP))
Chris Lattner132df652010-02-21 03:22:59 +0000875 NumFixedArityOperands = Pattern.getSrcPattern()->getNumChildren();
Jim Grosbach65586fe2010-12-21 16:16:00 +0000876
Cameron Zwarich8b76e012011-05-19 21:13:30 +0000877 // If this is the root node and multiple matched nodes in the input pattern
878 // have MemRefs in them, have the interpreter collect them and plop them onto
879 // this node. If there is just one node with MemRefs, leave them on that node
880 // even if it is not the root.
Chris Lattner132df652010-02-21 03:22:59 +0000881 //
Cameron Zwarich8b76e012011-05-19 21:13:30 +0000882 // FIXME3: This is actively incorrect for result patterns with multiple
883 // memory-referencing instructions.
884 bool PatternHasMemOperands =
885 Pattern.getSrcPattern()->TreeHasProperty(SDNPMemOperand, CGP);
886
887 bool NodeHasMemRefs = false;
888 if (PatternHasMemOperands) {
889 unsigned NumNodesThatLoadOrStore =
890 numNodesThatMayLoadOrStore(Pattern.getDstPattern(), CGP);
891 bool NodeIsUniqueLoadOrStore = mayInstNodeLoadOrStore(N, CGP) &&
892 NumNodesThatLoadOrStore == 1;
893 NodeHasMemRefs =
894 NodeIsUniqueLoadOrStore || (isRoot && (mayInstNodeLoadOrStore(N, CGP) ||
895 NumNodesThatLoadOrStore != 1));
896 }
Chris Lattner132df652010-02-21 03:22:59 +0000897
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000898 assert((!ResultVTs.empty() || TreeHasOutGlue || NodeHasChain) &&
Chris Lattnerd44966f2010-03-27 19:15:02 +0000899 "Node has no result");
Jim Grosbach65586fe2010-12-21 16:16:00 +0000900
Craig Topper86a9aee2017-07-07 06:22:35 +0000901 AddMatcher(new EmitNodeMatcher(II.Namespace.str()+"::"+II.TheDef->getName().str(),
Craig Toppera2c60192014-01-21 07:20:05 +0000902 ResultVTs, InstOps,
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000903 NodeHasChain, TreeHasInGlue, TreeHasOutGlue,
Chris Lattnerabb1c792010-02-28 02:41:25 +0000904 NodeHasMemRefs, NumFixedArityOperands,
905 NextRecordedOperandNo));
Jim Grosbach65586fe2010-12-21 16:16:00 +0000906
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000907 // The non-chain and non-glue results of the newly emitted node get recorded.
Chris Lattner9fd97c32010-02-21 23:54:05 +0000908 for (unsigned i = 0, e = ResultVTs.size(); i != e; ++i) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000909 if (ResultVTs[i] == MVT::Other || ResultVTs[i] == MVT::Glue) break;
Chris Lattner79eaeb42010-02-21 06:03:07 +0000910 OutputOps.push_back(NextRecordedOperandNo++);
Chris Lattner9fd97c32010-02-21 23:54:05 +0000911 }
Chris Lattner132df652010-02-21 03:22:59 +0000912}
913
914void MatcherGen::
915EmitResultSDNodeXFormAsOperand(const TreePatternNode *N,
916 SmallVectorImpl<unsigned> &ResultOps) {
917 assert(N->getOperator()->isSubClassOf("SDNodeXForm") && "Not SDNodeXForm?");
918
919 // Emit the operand.
920 SmallVector<unsigned, 8> InputOps;
Jim Grosbach65586fe2010-12-21 16:16:00 +0000921
Chris Lattner132df652010-02-21 03:22:59 +0000922 // FIXME2: Could easily generalize this to support multiple inputs and outputs
923 // to the SDNodeXForm. For now we just support one input and one output like
924 // the old instruction selector.
925 assert(N->getNumChildren() == 1);
926 EmitResultOperand(N->getChild(0), InputOps);
927
928 // The input currently must have produced exactly one result.
929 assert(InputOps.size() == 1 && "Unexpected input to SDNodeXForm");
930
Chris Lattner2c3f6492010-02-25 02:04:40 +0000931 AddMatcher(new EmitNodeXFormMatcher(InputOps[0], N->getOperator()));
Chris Lattner132df652010-02-21 03:22:59 +0000932 ResultOps.push_back(NextRecordedOperandNo++);
Chris Lattner7ed81692010-02-18 06:47:49 +0000933}
934
935void MatcherGen::EmitResultOperand(const TreePatternNode *N,
Chris Lattner132df652010-02-21 03:22:59 +0000936 SmallVectorImpl<unsigned> &ResultOps) {
Chris Lattner7ed81692010-02-18 06:47:49 +0000937 // This is something selected from the pattern we matched.
Chris Lattner132df652010-02-21 03:22:59 +0000938 if (!N->getName().empty())
939 return EmitResultOfNamedOperand(N, ResultOps);
Chris Lattner7ed81692010-02-18 06:47:49 +0000940
941 if (N->isLeaf())
942 return EmitResultLeafAsOperand(N, ResultOps);
943
944 Record *OpRec = N->getOperator();
945 if (OpRec->isSubClassOf("Instruction"))
946 return EmitResultInstructionAsOperand(N, ResultOps);
947 if (OpRec->isSubClassOf("SDNodeXForm"))
Chris Lattner132df652010-02-21 03:22:59 +0000948 return EmitResultSDNodeXFormAsOperand(N, ResultOps);
Chris Lattner7ed81692010-02-18 06:47:49 +0000949 errs() << "Unknown result node to emit code for: " << *N << '\n';
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000950 PrintFatalError("Unknown node in result pattern!");
Chris Lattner7ed81692010-02-18 06:47:49 +0000951}
952
953void MatcherGen::EmitResultCode() {
Chris Lattner28182722010-03-01 22:46:42 +0000954 // Patterns that match nodes with (potentially multiple) chain inputs have to
955 // merge them together into a token factor. This informs the generated code
956 // what all the chained nodes are.
957 if (!MatchedChainNodes.empty())
Craig Toppera2c60192014-01-21 07:20:05 +0000958 AddMatcher(new EmitMergeInputChainsMatcher(MatchedChainNodes));
Jim Grosbach65586fe2010-12-21 16:16:00 +0000959
Chris Lattner9fd97c32010-02-21 23:54:05 +0000960 // Codegen the root of the result pattern, capturing the resulting values.
Chris Lattner132df652010-02-21 03:22:59 +0000961 SmallVector<unsigned, 8> Ops;
Chris Lattner7ed81692010-02-18 06:47:49 +0000962 EmitResultOperand(Pattern.getDstPattern(), Ops);
Chris Lattner132df652010-02-21 03:22:59 +0000963
Chris Lattner9fd97c32010-02-21 23:54:05 +0000964 // At this point, we have however many values the result pattern produces.
965 // However, the input pattern might not need all of these. If there are
Chris Lattner725d3f62010-03-27 20:45:15 +0000966 // excess values at the end (such as implicit defs of condition codes etc)
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000967 // just lop them off. This doesn't need to worry about glue or chains, just
Chris Lattner725d3f62010-03-27 20:45:15 +0000968 // explicit results.
Chris Lattner9fd97c32010-02-21 23:54:05 +0000969 //
Chris Lattner725d3f62010-03-27 20:45:15 +0000970 unsigned NumSrcResults = Pattern.getSrcPattern()->getNumTypes();
Jim Grosbach65586fe2010-12-21 16:16:00 +0000971
Chris Lattner725d3f62010-03-27 20:45:15 +0000972 // If the pattern also has (implicit) results, count them as well.
973 if (!Pattern.getDstRegs().empty()) {
974 // If the root came from an implicit def in the instruction handling stuff,
975 // don't re-add it.
Craig Topper24064772014-04-15 07:20:03 +0000976 Record *HandledReg = nullptr;
Chris Lattner725d3f62010-03-27 20:45:15 +0000977 const TreePatternNode *DstPat = Pattern.getDstPattern();
978 if (!DstPat->isLeaf() &&DstPat->getOperator()->isSubClassOf("Instruction")){
979 const CodeGenTarget &CGT = CGP.getTargetInfo();
980 CodeGenInstruction &II = CGT.getInstruction(DstPat->getOperator());
981
982 if (II.HasOneImplicitDefWithKnownVT(CGT) != MVT::Other)
983 HandledReg = II.ImplicitDefs[0];
984 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000985
Simon Pilgrim48bed532017-06-19 13:24:12 +0000986 for (Record *Reg : Pattern.getDstRegs()) {
Chris Lattner725d3f62010-03-27 20:45:15 +0000987 if (!Reg->isSubClassOf("Register") || Reg == HandledReg) continue;
988 ++NumSrcResults;
989 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000990 }
991
Chris Lattner9fd97c32010-02-21 23:54:05 +0000992 assert(Ops.size() >= NumSrcResults && "Didn't provide enough results");
993 Ops.resize(NumSrcResults);
Chris Lattner29364372010-02-24 05:33:42 +0000994
Craig Toppera2c60192014-01-21 07:20:05 +0000995 AddMatcher(new CompleteMatchMatcher(Ops, Pattern));
Chris Lattner7ed81692010-02-18 06:47:49 +0000996}
997
998
Chris Lattner053a28a2010-03-01 07:17:40 +0000999/// ConvertPatternToMatcher - Create the matcher for the specified pattern with
1000/// the specified variant. If the variant number is invalid, this returns null.
Chris Lattner2c3f6492010-02-25 02:04:40 +00001001Matcher *llvm::ConvertPatternToMatcher(const PatternToMatch &Pattern,
Chris Lattner053a28a2010-03-01 07:17:40 +00001002 unsigned Variant,
Chris Lattner102a8a02010-02-28 20:49:53 +00001003 const CodeGenDAGPatterns &CGP) {
Chris Lattnerb02cdaa2010-02-15 08:04:42 +00001004 MatcherGen Gen(Pattern, CGP);
1005
1006 // Generate the code for the matcher.
Chris Lattner053a28a2010-03-01 07:17:40 +00001007 if (Gen.EmitMatcherCode(Variant))
Craig Topper24064772014-04-15 07:20:03 +00001008 return nullptr;
Jim Grosbach65586fe2010-12-21 16:16:00 +00001009
Chris Lattner132df652010-02-21 03:22:59 +00001010 // FIXME2: Kill extra MoveParent commands at the end of the matcher sequence.
1011 // FIXME2: Split result code out to another table, and make the matcher end
1012 // with an "Emit <index>" command. This allows result generation stuff to be
1013 // shared and factored?
Jim Grosbach65586fe2010-12-21 16:16:00 +00001014
Chris Lattnerb02cdaa2010-02-15 08:04:42 +00001015 // If the match succeeds, then we generate Pattern.
Chris Lattner7ed81692010-02-18 06:47:49 +00001016 Gen.EmitResultCode();
Chris Lattnerb02cdaa2010-02-15 08:04:42 +00001017
1018 // Unconditional match.
Chris Lattner7ed81692010-02-18 06:47:49 +00001019 return Gen.GetMatcher();
Chris Lattnerb02cdaa2010-02-15 08:04:42 +00001020}