blob: ce23651b96828fcc833a55af462dde4aac4bb570 [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.
Florian Hahn75e87c32018-05-30 21:00:18 +000061 TreePatternNodePtr 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 Lattner053a28a2010-03-01 07:17:40 +0000104 bool EmitMatcherCode(unsigned Variant);
Chris Lattner7ed81692010-02-18 06:47:49 +0000105 void EmitResultCode();
Jim Grosbach65586fe2010-12-21 16:16:00 +0000106
Chris Lattner2c3f6492010-02-25 02:04:40 +0000107 Matcher *GetMatcher() const { return TheMatcher; }
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000108 private:
Chris Lattner2c3f6492010-02-25 02:04:40 +0000109 void AddMatcher(Matcher *NewNode);
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000110 void InferPossibleTypes(unsigned ForceMode);
Jim Grosbach65586fe2010-12-21 16:16:00 +0000111
Chris Lattner7ed81692010-02-18 06:47:49 +0000112 // Matcher Generation.
Florian Hahn6b1db822018-06-14 20:32:58 +0000113 void EmitMatchCode(const TreePatternNode *N, TreePatternNode *NodeNoTypes,
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000114 unsigned ForceMode);
Florian Hahn6b1db822018-06-14 20:32:58 +0000115 void EmitLeafMatchCode(const TreePatternNode *N);
116 void EmitOperatorMatchCode(const TreePatternNode *N,
117 TreePatternNode *NodeNoTypes,
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000118 unsigned ForceMode);
Jim Grosbach65586fe2010-12-21 16:16:00 +0000119
Tim Northoverc807a172014-05-20 11:52:46 +0000120 /// If this is the first time a node with unique identifier Name has been
121 /// seen, record it. Otherwise, emit a check to make sure this is the same
122 /// node. Returns true if this is the first encounter.
Benjamin Kramerc321e532016-06-08 19:09:22 +0000123 bool recordUniqueNode(const std::string &Name);
Tim Northoverc807a172014-05-20 11:52:46 +0000124
Chris Lattner7ed81692010-02-18 06:47:49 +0000125 // Result Code Generation.
Chris Lattner132df652010-02-21 03:22:59 +0000126 unsigned getNamedArgumentSlot(StringRef Name) {
127 unsigned VarMapEntry = VariableMap[Name];
128 assert(VarMapEntry != 0 &&
129 "Variable referenced but not defined and not caught earlier!");
130 return VarMapEntry-1;
131 }
132
Florian Hahn6b1db822018-06-14 20:32:58 +0000133 void EmitResultOperand(const TreePatternNode *N,
Chris Lattner132df652010-02-21 03:22:59 +0000134 SmallVectorImpl<unsigned> &ResultOps);
Florian Hahn6b1db822018-06-14 20:32:58 +0000135 void EmitResultOfNamedOperand(const TreePatternNode *N,
Chris Lattner132df652010-02-21 03:22:59 +0000136 SmallVectorImpl<unsigned> &ResultOps);
Florian Hahn6b1db822018-06-14 20:32:58 +0000137 void EmitResultLeafAsOperand(const TreePatternNode *N,
Chris Lattner132df652010-02-21 03:22:59 +0000138 SmallVectorImpl<unsigned> &ResultOps);
Florian Hahn6b1db822018-06-14 20:32:58 +0000139 void EmitResultInstructionAsOperand(const TreePatternNode *N,
Chris Lattner132df652010-02-21 03:22:59 +0000140 SmallVectorImpl<unsigned> &ResultOps);
Florian Hahn6b1db822018-06-14 20:32:58 +0000141 void EmitResultSDNodeXFormAsOperand(const TreePatternNode *N,
Chris Lattner132df652010-02-21 03:22:59 +0000142 SmallVectorImpl<unsigned> &ResultOps);
143 };
Jim Grosbach65586fe2010-12-21 16:16:00 +0000144
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000145} // end anon namespace.
146
147MatcherGen::MatcherGen(const PatternToMatch &pattern,
148 const CodeGenDAGPatterns &cgp)
Chris Lattnerdc61962e2010-02-19 00:33:13 +0000149: Pattern(pattern), CGP(cgp), NextRecordedOperandNo(0),
Craig Topper24064772014-04-15 07:20:03 +0000150 TheMatcher(nullptr), CurPredicate(nullptr) {
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000151 // We need to produce the matcher tree for the patterns source pattern. To do
152 // this we need to match the structure as well as the types. To do the type
153 // matching, we want to figure out the fewest number of type checks we need to
154 // emit. For example, if there is only one integer type supported by a
155 // target, there should be no type comparisons at all for integer patterns!
156 //
157 // To figure out the fewest number of type checks needed, clone the pattern,
158 // remove the types, then perform type inference on the pattern as a whole.
159 // If there are unresolved types, emit an explicit check for those types,
160 // apply the type to the tree, then rerun type inference. Iterate until all
161 // types are resolved.
162 //
163 PatWithNoTypes = Pattern.getSrcPattern()->clone();
164 PatWithNoTypes->RemoveAllTypes();
Jim Grosbach65586fe2010-12-21 16:16:00 +0000165
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000166 // If there are types that are manifestly known, infer them.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000167 InferPossibleTypes(Pattern.ForceMode);
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000168}
169
170/// InferPossibleTypes - As we emit the pattern, we end up generating type
171/// checks and applying them to the 'PatWithNoTypes' tree. As we do this, we
172/// want to propagate implied types as far throughout the tree as possible so
173/// that we avoid doing redundant type checks. This does the type propagation.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000174void MatcherGen::InferPossibleTypes(unsigned ForceMode) {
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000175 // TP - Get *SOME* tree pattern, we don't care which. It is only used for
176 // diagnostics, which we know are impossible at this point.
177 TreePattern &TP = *CGP.pf_begin()->second;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000178 TP.getInfer().CodeGen = true;
179 TP.getInfer().ForceMode = ForceMode;
Jim Grosbach65586fe2010-12-21 16:16:00 +0000180
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000181 bool MadeChange = true;
182 while (MadeChange)
183 MadeChange = PatWithNoTypes->ApplyTypeConstraints(TP,
184 true/*Ignore reg constraints*/);
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000185}
186
187
Jim Grosbach65586fe2010-12-21 16:16:00 +0000188/// AddMatcher - Add a matcher node to the current graph we're building.
Chris Lattner2c3f6492010-02-25 02:04:40 +0000189void MatcherGen::AddMatcher(Matcher *NewNode) {
Craig Topper24064772014-04-15 07:20:03 +0000190 if (CurPredicate)
Chris Lattner186ad802010-02-18 02:53:41 +0000191 CurPredicate->setNext(NewNode);
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000192 else
Chris Lattner2c3f6492010-02-25 02:04:40 +0000193 TheMatcher = NewNode;
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000194 CurPredicate = NewNode;
195}
196
197
Chris Lattner7ed81692010-02-18 06:47:49 +0000198//===----------------------------------------------------------------------===//
199// Pattern Match Generation
200//===----------------------------------------------------------------------===//
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000201
202/// EmitLeafMatchCode - Generate matching code for leaf nodes.
Florian Hahn6b1db822018-06-14 20:32:58 +0000203void MatcherGen::EmitLeafMatchCode(const TreePatternNode *N) {
204 assert(N->isLeaf() && "Not a leaf?");
Jim Grosbach65586fe2010-12-21 16:16:00 +0000205
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000206 // Direct match against an integer constant.
Florian Hahn6b1db822018-06-14 20:32:58 +0000207 if (IntInit *II = dyn_cast<IntInit>(N->getLeafValue())) {
Chris Lattnera44697c2010-03-01 07:27:07 +0000208 // If this is the root of the dag we're matching, we emit a redundant opcode
209 // check to ensure that this gets folded into the normal top-level
210 // OpcodeSwitch.
Florian Hahn6b1db822018-06-14 20:32:58 +0000211 if (N == Pattern.getSrcPattern()) {
Chris Lattnera44697c2010-03-01 07:27:07 +0000212 const SDNodeInfo &NI = CGP.getSDNodeInfo(CGP.getSDNodeNamed("imm"));
213 AddMatcher(new CheckOpcodeMatcher(NI));
214 }
215
Chris Lattner2c3f6492010-02-25 02:04:40 +0000216 return AddMatcher(new CheckIntegerMatcher(II->getValue()));
Chris Lattnera44697c2010-03-01 07:27:07 +0000217 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000218
Jakob Stoklund Olesen99ffcc82013-03-24 19:37:00 +0000219 // An UnsetInit represents a named node without any constraints.
Florian Hahn6b1db822018-06-14 20:32:58 +0000220 if (isa<UnsetInit>(N->getLeafValue())) {
221 assert(N->hasName() && "Unnamed ? leaf");
Jakob Stoklund Olesen99ffcc82013-03-24 19:37:00 +0000222 return;
223 }
224
Florian Hahn6b1db822018-06-14 20:32:58 +0000225 DefInit *DI = dyn_cast<DefInit>(N->getLeafValue());
Craig Topper24064772014-04-15 07:20:03 +0000226 if (!DI) {
Florian Hahn6b1db822018-06-14 20:32:58 +0000227 errs() << "Unknown leaf kind: " << *N << "\n";
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000228 abort();
229 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000230
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000231 Record *LeafRec = DI->getDef();
Jakob Stoklund Olesend906b902013-03-23 20:35:01 +0000232
233 // A ValueType leaf node can represent a register when named, or itself when
234 // unnamed.
235 if (LeafRec->isSubClassOf("ValueType")) {
236 // A named ValueType leaf always matches: (add i32:$a, i32:$b).
Florian Hahn6b1db822018-06-14 20:32:58 +0000237 if (N->hasName())
Jakob Stoklund Olesend906b902013-03-23 20:35:01 +0000238 return;
239 // An unnamed ValueType as in (sext_inreg GPR:$foo, i8).
240 return AddMatcher(new CheckValueTypeMatcher(LeafRec->getName()));
241 }
242
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000243 if (// Handle register references. Nothing to do here, they always match.
Jim Grosbach65586fe2010-12-21 16:16:00 +0000244 LeafRec->isSubClassOf("RegisterClass") ||
Owen Andersona84be6c2011-06-27 21:06:21 +0000245 LeafRec->isSubClassOf("RegisterOperand") ||
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000246 LeafRec->isSubClassOf("PointerLikeRegClass") ||
Jakob Stoklund Olesen1c696462010-05-24 14:48:12 +0000247 LeafRec->isSubClassOf("SubRegIndex") ||
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000248 // Place holder for SRCVALUE nodes. Nothing to do here.
249 LeafRec->getName() == "srcvalue")
250 return;
Chris Lattner132df652010-02-21 03:22:59 +0000251
252 // If we have a physreg reference like (mul gpr:$src, EAX) then we need to
Jim Grosbach65586fe2010-12-21 16:16:00 +0000253 // record the register
Chris Lattner132df652010-02-21 03:22:59 +0000254 if (LeafRec->isSubClassOf("Register")) {
Matthias Braun4a86d452016-12-04 05:48:16 +0000255 AddMatcher(new RecordMatcher("physreg input "+LeafRec->getName().str(),
Chris Lattner4634d9b2010-03-01 02:24:17 +0000256 NextRecordedOperandNo));
Chris Lattner132df652010-02-21 03:22:59 +0000257 PhysRegInputs.push_back(std::make_pair(LeafRec, NextRecordedOperandNo++));
258 return;
259 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000260
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000261 if (LeafRec->isSubClassOf("CondCode"))
Chris Lattner2c3f6492010-02-25 02:04:40 +0000262 return AddMatcher(new CheckCondCodeMatcher(LeafRec->getName()));
Jim Grosbach65586fe2010-12-21 16:16:00 +0000263
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000264 if (LeafRec->isSubClassOf("ComplexPattern")) {
Chris Lattner7b390142010-02-17 00:11:30 +0000265 // We can't model ComplexPattern uses that don't have their name taken yet.
266 // The OPC_CheckComplexPattern operation implicitly records the results.
Florian Hahn6b1db822018-06-14 20:32:58 +0000267 if (N->getName().empty()) {
James Y Knighte452e272015-05-11 22:17:13 +0000268 std::string S;
269 raw_string_ostream OS(S);
Florian Hahn6b1db822018-06-14 20:32:58 +0000270 OS << "We expect complex pattern uses to have names: " << *N;
James Y Knighte452e272015-05-11 22:17:13 +0000271 PrintFatalError(OS.str());
Chris Lattnere2347aa2010-02-16 23:16:25 +0000272 }
Chris Lattnera5a29852010-02-22 22:18:05 +0000273
Chris Lattner0acbb712010-03-04 01:23:08 +0000274 // Remember this ComplexPattern so that we can emit it after all the other
275 // structural matches are done.
Florian Hahn6b1db822018-06-14 20:32:58 +0000276 unsigned InputOperand = VariableMap[N->getName()] - 1;
277 MatchedComplexPatterns.push_back(std::make_pair(N, InputOperand));
Chris Lattner8ad29342010-02-17 06:08:25 +0000278 return;
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000279 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000280
Florian Hahn6b1db822018-06-14 20:32:58 +0000281 errs() << "Unknown leaf kind: " << *N << "\n";
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000282 abort();
283}
284
Florian Hahn6b1db822018-06-14 20:32:58 +0000285void MatcherGen::EmitOperatorMatchCode(const TreePatternNode *N,
286 TreePatternNode *NodeNoTypes,
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000287 unsigned ForceMode) {
Florian Hahn6b1db822018-06-14 20:32:58 +0000288 assert(!N->isLeaf() && "Not an operator?");
Tim Northoverc807a172014-05-20 11:52:46 +0000289
Florian Hahn6b1db822018-06-14 20:32:58 +0000290 if (N->getOperator()->isSubClassOf("ComplexPattern")) {
Tim Northoverc807a172014-05-20 11:52:46 +0000291 // The "name" of a non-leaf complex pattern (MY_PAT $op1, $op2) is
292 // "MY_PAT:op1:op2". We should already have validated that the uses are
293 // consistent.
Florian Hahn6b1db822018-06-14 20:32:58 +0000294 std::string PatternName = N->getOperator()->getName();
295 for (unsigned i = 0; i < N->getNumChildren(); ++i) {
Tim Northoverc807a172014-05-20 11:52:46 +0000296 PatternName += ":";
Florian Hahn6b1db822018-06-14 20:32:58 +0000297 PatternName += N->getChild(i)->getName();
Tim Northoverc807a172014-05-20 11:52:46 +0000298 }
299
300 if (recordUniqueNode(PatternName)) {
Florian Hahn6b1db822018-06-14 20:32:58 +0000301 auto NodeAndOpNum = std::make_pair(N, NextRecordedOperandNo - 1);
Tim Northoverc807a172014-05-20 11:52:46 +0000302 MatchedComplexPatterns.push_back(NodeAndOpNum);
303 }
304
305 return;
306 }
307
Florian Hahn6b1db822018-06-14 20:32:58 +0000308 const SDNodeInfo &CInfo = CGP.getSDNodeInfo(N->getOperator());
Jim Grosbach65586fe2010-12-21 16:16:00 +0000309
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000310 // If this is an 'and R, 1234' where the operation is AND/OR and the RHS is
Lama Saba880e8782017-07-30 20:12:17 +0000311 // a constant without a predicate fn that has more than one bit set, handle
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000312 // this as a special case. This is usually for targets that have special
313 // handling of certain large constants (e.g. alpha with it's 8/16/32-bit
314 // handling stuff). Using these instructions is often far more efficient
315 // than materializing the constant. Unfortunately, both the instcombiner
316 // and the dag combiner can often infer that bits are dead, and thus drop
317 // them from the mask in the dag. For example, it might turn 'AND X, 255'
318 // into 'AND X, 254' if it knows the low bit is set. Emit code that checks
319 // to handle this.
Florian Hahn6b1db822018-06-14 20:32:58 +0000320 if ((N->getOperator()->getName() == "and" ||
321 N->getOperator()->getName() == "or") &&
322 N->getChild(1)->isLeaf() && N->getChild(1)->getPredicateFns().empty() &&
323 N->getPredicateFns().empty()) {
324 if (IntInit *II = dyn_cast<IntInit>(N->getChild(1)->getLeafValue())) {
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000325 if (!isPowerOf2_32(II->getValue())) { // Don't bother with single bits.
Chris Lattner90b1b9d2010-03-01 02:15:34 +0000326 // If this is at the root of the pattern, we emit a redundant
327 // CheckOpcode so that the following checks get factored properly under
328 // a single opcode check.
Florian Hahn6b1db822018-06-14 20:32:58 +0000329 if (N == Pattern.getSrcPattern())
Chris Lattner90b1b9d2010-03-01 02:15:34 +0000330 AddMatcher(new CheckOpcodeMatcher(CInfo));
331
332 // Emit the CheckAndImm/CheckOrImm node.
Florian Hahn6b1db822018-06-14 20:32:58 +0000333 if (N->getOperator()->getName() == "and")
Chris Lattner2c3f6492010-02-25 02:04:40 +0000334 AddMatcher(new CheckAndImmMatcher(II->getValue()));
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000335 else
Chris Lattner2c3f6492010-02-25 02:04:40 +0000336 AddMatcher(new CheckOrImmMatcher(II->getValue()));
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000337
338 // Match the LHS of the AND as appropriate.
Chris Lattner2c3f6492010-02-25 02:04:40 +0000339 AddMatcher(new MoveChildMatcher(0));
Florian Hahn6b1db822018-06-14 20:32:58 +0000340 EmitMatchCode(N->getChild(0), NodeNoTypes->getChild(0), ForceMode);
Chris Lattner2c3f6492010-02-25 02:04:40 +0000341 AddMatcher(new MoveParentMatcher());
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000342 return;
343 }
344 }
345 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000346
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000347 // Check that the current opcode lines up.
Chris Lattner278606b2010-02-27 21:48:43 +0000348 AddMatcher(new CheckOpcodeMatcher(CInfo));
Jim Grosbach65586fe2010-12-21 16:16:00 +0000349
Chris Lattner132df652010-02-21 03:22:59 +0000350 // If this node has memory references (i.e. is a load or store), tell the
351 // interpreter to capture them in the memref array.
Florian Hahn6b1db822018-06-14 20:32:58 +0000352 if (N->NodeHasProperty(SDNPMemOperand, CGP))
Chris Lattner2c3f6492010-02-25 02:04:40 +0000353 AddMatcher(new RecordMemRefMatcher());
Jim Grosbach65586fe2010-12-21 16:16:00 +0000354
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000355 // If this node has a chain, then the chain is operand #0 is the SDNode, and
356 // the child numbers of the node are all offset by one.
357 unsigned OpNo = 0;
Florian Hahn6b1db822018-06-14 20:32:58 +0000358 if (N->NodeHasProperty(SDNPHasChain, CGP)) {
Chris Lattner132df652010-02-21 03:22:59 +0000359 // Record the node and remember it in our chained nodes list.
Florian Hahn6b1db822018-06-14 20:32:58 +0000360 AddMatcher(new RecordMatcher("'" + N->getOperator()->getName().str() +
Chris Lattner4634d9b2010-03-01 02:24:17 +0000361 "' chained node",
362 NextRecordedOperandNo));
Chris Lattner469cfb92010-02-17 02:16:19 +0000363 // Remember all of the input chains our pattern will match.
Chris Lattner132df652010-02-21 03:22:59 +0000364 MatchedChainNodes.push_back(NextRecordedOperandNo++);
Jim Grosbach65586fe2010-12-21 16:16:00 +0000365
Chris Lattner022e5112010-02-17 01:34:15 +0000366 // Don't look at the input chain when matching the tree pattern to the
367 // SDNode.
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000368 OpNo = 1;
369
Chris Lattner6c132332010-02-16 19:19:58 +0000370 // If this node is not the root and the subtree underneath it produces a
371 // chain, then the result of matching the node is also produce a chain.
372 // Beyond that, this means that we're also folding (at least) the root node
373 // into the node that produce the chain (for example, matching
374 // "(add reg, (load ptr))" as a add_with_memory on X86). This is
375 // problematic, if the 'reg' node also uses the load (say, its chain).
376 // Graphically:
377 //
378 // [LD]
379 // ^ ^
380 // | \ DAG's like cheese.
381 // / |
382 // / [YY]
383 // | ^
384 // [XX]--/
385 //
386 // It would be invalid to fold XX and LD. In this case, folding the two
387 // nodes together would induce a cycle in the DAG, making it a 'cyclic DAG'
388 // To prevent this, we emit a dynamic check for legality before allowing
389 // this to be folded.
390 //
391 const TreePatternNode *Root = Pattern.getSrcPattern();
Florian Hahn6b1db822018-06-14 20:32:58 +0000392 if (N != Root) { // Not the root of the pattern.
Chris Lattneraa7d3e02010-02-16 06:10:58 +0000393 // If there is a node between the root and this node, then we definitely
394 // need to emit the check.
Florian Hahn6b1db822018-06-14 20:32:58 +0000395 bool NeedCheck = !Root->hasChild(N);
Jim Grosbach65586fe2010-12-21 16:16:00 +0000396
Chris Lattneraa7d3e02010-02-16 06:10:58 +0000397 // If it *is* an immediate child of the root, we can still need a check if
398 // the root SDNode has multiple inputs. For us, this means that it is an
399 // intrinsic, has multiple operands, or has other inputs like chain or
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000400 // glue).
Chris Lattneraa7d3e02010-02-16 06:10:58 +0000401 if (!NeedCheck) {
402 const SDNodeInfo &PInfo = CGP.getSDNodeInfo(Root->getOperator());
403 NeedCheck =
404 Root->getOperator() == CGP.get_intrinsic_void_sdnode() ||
405 Root->getOperator() == CGP.get_intrinsic_w_chain_sdnode() ||
406 Root->getOperator() == CGP.get_intrinsic_wo_chain_sdnode() ||
407 PInfo.getNumOperands() > 1 ||
408 PInfo.hasProperty(SDNPHasChain) ||
Chris Lattner2a0a3b42010-12-23 18:28:41 +0000409 PInfo.hasProperty(SDNPInGlue) ||
410 PInfo.hasProperty(SDNPOptInGlue);
Chris Lattneraa7d3e02010-02-16 06:10:58 +0000411 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000412
Chris Lattneraa7d3e02010-02-16 06:10:58 +0000413 if (NeedCheck)
Chris Lattner2c3f6492010-02-25 02:04:40 +0000414 AddMatcher(new CheckFoldableChainNodeMatcher());
Chris Lattneraa7d3e02010-02-16 06:10:58 +0000415 }
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000416 }
Chris Lattner29364372010-02-24 05:33:42 +0000417
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000418 // If this node has an output glue and isn't the root, remember it.
Florian Hahn6b1db822018-06-14 20:32:58 +0000419 if (N->NodeHasProperty(SDNPOutGlue, CGP) &&
420 N != Pattern.getSrcPattern()) {
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000421 // TODO: This redundantly records nodes with both glues and chains.
Jim Grosbach65586fe2010-12-21 16:16:00 +0000422
Chris Lattner29364372010-02-24 05:33:42 +0000423 // Record the node and remember it in our chained nodes list.
Florian Hahn6b1db822018-06-14 20:32:58 +0000424 AddMatcher(new RecordMatcher("'" + N->getOperator()->getName().str() +
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000425 "' glue output node",
Chris Lattner4634d9b2010-03-01 02:24:17 +0000426 NextRecordedOperandNo));
Chris Lattner29364372010-02-24 05:33:42 +0000427 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000428
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000429 // If this node is known to have an input glue or if it *might* have an input
430 // glue, capture it as the glue input of the pattern.
Florian Hahn6b1db822018-06-14 20:32:58 +0000431 if (N->NodeHasProperty(SDNPOptInGlue, CGP) ||
432 N->NodeHasProperty(SDNPInGlue, CGP))
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000433 AddMatcher(new CaptureGlueInputMatcher());
Jim Grosbach65586fe2010-12-21 16:16:00 +0000434
Florian Hahn6b1db822018-06-14 20:32:58 +0000435 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) {
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000436 // Get the code suitable for matching this child. Move to the child, check
437 // it then move back to the parent.
Chris Lattner2c3f6492010-02-25 02:04:40 +0000438 AddMatcher(new MoveChildMatcher(OpNo));
Florian Hahn6b1db822018-06-14 20:32:58 +0000439 EmitMatchCode(N->getChild(i), NodeNoTypes->getChild(i), ForceMode);
Chris Lattner2c3f6492010-02-25 02:04:40 +0000440 AddMatcher(new MoveParentMatcher());
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000441 }
442}
443
Benjamin Kramerc321e532016-06-08 19:09:22 +0000444bool MatcherGen::recordUniqueNode(const std::string &Name) {
Tim Northoverc807a172014-05-20 11:52:46 +0000445 unsigned &VarMapEntry = VariableMap[Name];
446 if (VarMapEntry == 0) {
447 // If it is a named node, we must emit a 'Record' opcode.
448 AddMatcher(new RecordMatcher("$" + Name, NextRecordedOperandNo));
449 VarMapEntry = ++NextRecordedOperandNo;
450 return true;
451 }
452
453 // If we get here, this is a second reference to a specific name. Since
454 // we already have checked that the first reference is valid, we don't
455 // have to recursively match it, just check that it's the same as the
456 // previously named thing.
457 AddMatcher(new CheckSameMatcher(VarMapEntry-1));
458 return false;
459}
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000460
Florian Hahn6b1db822018-06-14 20:32:58 +0000461void MatcherGen::EmitMatchCode(const TreePatternNode *N,
462 TreePatternNode *NodeNoTypes,
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000463 unsigned ForceMode) {
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000464 // If N and NodeNoTypes don't agree on a type, then this is a case where we
Craig Topper336e1f12014-01-25 17:34:23 +0000465 // need to do a type check. Emit the check, apply the type to NodeNoTypes and
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000466 // reinfer any correlated types.
Chris Lattner6c2d1782010-03-24 00:41:19 +0000467 SmallVector<unsigned, 2> ResultsToTypeCheck;
Jim Grosbach65586fe2010-12-21 16:16:00 +0000468
Florian Hahn6b1db822018-06-14 20:32:58 +0000469 for (unsigned i = 0, e = NodeNoTypes->getNumTypes(); i != e; ++i) {
470 if (NodeNoTypes->getExtType(i) == N->getExtType(i)) continue;
471 NodeNoTypes->setType(i, N->getExtType(i));
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000472 InferPossibleTypes(ForceMode);
Chris Lattner6c2d1782010-03-24 00:41:19 +0000473 ResultsToTypeCheck.push_back(i);
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000474 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000475
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000476 // If this node has a name associated with it, capture it in VariableMap. If
477 // we already saw this in the pattern, emit code to verify dagness.
Florian Hahn6b1db822018-06-14 20:32:58 +0000478 if (!N->getName().empty())
479 if (!recordUniqueNode(N->getName()))
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000480 return;
Jim Grosbach65586fe2010-12-21 16:16:00 +0000481
Florian Hahn6b1db822018-06-14 20:32:58 +0000482 if (N->isLeaf())
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000483 EmitLeafMatchCode(N);
484 else
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000485 EmitOperatorMatchCode(N, NodeNoTypes, ForceMode);
Jim Grosbach65586fe2010-12-21 16:16:00 +0000486
Chris Lattnera1603892010-03-07 07:20:49 +0000487 // If there are node predicates for this node, generate their checks.
Florian Hahn6b1db822018-06-14 20:32:58 +0000488 for (unsigned i = 0, e = N->getPredicateFns().size(); i != e; ++i)
489 AddMatcher(new CheckPredicateMatcher(N->getPredicateFns()[i]));
Jim Grosbach65586fe2010-12-21 16:16:00 +0000490
Chris Lattner6c2d1782010-03-24 00:41:19 +0000491 for (unsigned i = 0, e = ResultsToTypeCheck.size(); i != e; ++i)
Florian Hahn6b1db822018-06-14 20:32:58 +0000492 AddMatcher(new CheckTypeMatcher(N->getSimpleType(ResultsToTypeCheck[i]),
Chris Lattner6c2d1782010-03-24 00:41:19 +0000493 ResultsToTypeCheck[i]));
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000494}
495
Chris Lattner053a28a2010-03-01 07:17:40 +0000496/// EmitMatcherCode - Generate the code that matches the predicate of this
497/// pattern for the specified Variant. If the variant is invalid this returns
498/// true and does not generate code, if it is valid, it returns false.
499bool MatcherGen::EmitMatcherCode(unsigned Variant) {
500 // If the root of the pattern is a ComplexPattern and if it is specified to
501 // match some number of root opcodes, these are considered to be our variants.
502 // Depending on which variant we're generating code for, emit the root opcode
503 // check.
504 if (const ComplexPattern *CP =
505 Pattern.getSrcPattern()->getComplexPatternInfo(CGP)) {
Chris Lattner053a28a2010-03-01 07:17:40 +0000506 const std::vector<Record*> &OpNodes = CP->getRootNodes();
Chris Lattner1e634e32010-03-01 22:29:19 +0000507 assert(!OpNodes.empty() &&"Complex Pattern must specify what it can match");
508 if (Variant >= OpNodes.size()) return true;
Jim Grosbach65586fe2010-12-21 16:16:00 +0000509
Chris Lattner1e634e32010-03-01 22:29:19 +0000510 AddMatcher(new CheckOpcodeMatcher(CGP.getSDNodeInfo(OpNodes[Variant])));
Chris Lattner053a28a2010-03-01 07:17:40 +0000511 } else {
512 if (Variant != 0) return true;
513 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000514
Chris Lattner0e2cedb2010-03-04 01:25:36 +0000515 // Emit the matcher for the pattern structure and types.
Florian Hahn6b1db822018-06-14 20:32:58 +0000516 EmitMatchCode(Pattern.getSrcPattern(), PatWithNoTypes.get(),
Florian Hahn75e87c32018-05-30 21:00:18 +0000517 Pattern.ForceMode);
Jim Grosbach65586fe2010-12-21 16:16:00 +0000518
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000519 // If the pattern has a predicate on it (e.g. only enabled when a subtarget
520 // feature is around, do the check).
521 if (!Pattern.getPredicateCheck().empty())
Chris Lattner053a28a2010-03-01 07:17:40 +0000522 AddMatcher(new CheckPatternPredicateMatcher(Pattern.getPredicateCheck()));
Jim Grosbach65586fe2010-12-21 16:16:00 +0000523
Chris Lattner0acbb712010-03-04 01:23:08 +0000524 // Now that we've completed the structural type match, emit any ComplexPattern
525 // checks (e.g. addrmode matches). We emit this after the structural match
526 // because they are generally more expensive to evaluate and more difficult to
527 // factor.
Chris Lattner0acbb712010-03-04 01:23:08 +0000528 for (unsigned i = 0, e = MatchedComplexPatterns.size(); i != e; ++i) {
Florian Hahn75e87c32018-05-30 21:00:18 +0000529 auto N = MatchedComplexPatterns[i].first;
Jim Grosbach65586fe2010-12-21 16:16:00 +0000530
Chris Lattner0acbb712010-03-04 01:23:08 +0000531 // Remember where the results of this match get stuck.
Tim Northoverc807a172014-05-20 11:52:46 +0000532 if (N->isLeaf()) {
533 NamedComplexPatternOperands[N->getName()] = NextRecordedOperandNo + 1;
534 } else {
535 unsigned CurOp = NextRecordedOperandNo;
536 for (unsigned i = 0; i < N->getNumChildren(); ++i) {
Florian Hahn6b1db822018-06-14 20:32:58 +0000537 NamedComplexPatternOperands[N->getChild(i)->getName()] = CurOp + 1;
538 CurOp += N->getChild(i)->getNumMIResults(CGP);
Tim Northoverc807a172014-05-20 11:52:46 +0000539 }
540 }
Chris Lattner0acbb712010-03-04 01:23:08 +0000541
542 // Get the slot we recorded the value in from the name on the node.
Tim Northoverc807a172014-05-20 11:52:46 +0000543 unsigned RecNodeEntry = MatchedComplexPatterns[i].second;
Jim Grosbach65586fe2010-12-21 16:16:00 +0000544
Tim Northoverc807a172014-05-20 11:52:46 +0000545 const ComplexPattern &CP = *N->getComplexPatternInfo(CGP);
Jim Grosbach65586fe2010-12-21 16:16:00 +0000546
Chris Lattner0acbb712010-03-04 01:23:08 +0000547 // Emit a CheckComplexPat operation, which does the match (aborting if it
548 // fails) and pushes the matched operands onto the recorded nodes list.
549 AddMatcher(new CheckComplexPatMatcher(CP, RecNodeEntry,
550 N->getName(), NextRecordedOperandNo));
Jim Grosbach65586fe2010-12-21 16:16:00 +0000551
Chris Lattner0acbb712010-03-04 01:23:08 +0000552 // Record the right number of operands.
553 NextRecordedOperandNo += CP.getNumOperands();
554 if (CP.hasProperty(SDNPHasChain)) {
555 // If the complex pattern has a chain, then we need to keep track of the
556 // fact that we just recorded a chain input. The chain input will be
557 // matched as the last operand of the predicate if it was successful.
558 ++NextRecordedOperandNo; // Chained node operand.
Jim Grosbach65586fe2010-12-21 16:16:00 +0000559
Chris Lattner0acbb712010-03-04 01:23:08 +0000560 // It is the last operand recorded.
561 assert(NextRecordedOperandNo > 1 &&
562 "Should have recorded input/result chains at least!");
563 MatchedChainNodes.push_back(NextRecordedOperandNo-1);
564 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000565
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000566 // TODO: Complex patterns can't have output glues, if they did, we'd want
Chris Lattner0acbb712010-03-04 01:23:08 +0000567 // to record them.
568 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000569
Chris Lattner053a28a2010-03-01 07:17:40 +0000570 return false;
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000571}
572
573
Chris Lattner7ed81692010-02-18 06:47:49 +0000574//===----------------------------------------------------------------------===//
575// Node Result Generation
576//===----------------------------------------------------------------------===//
577
Florian Hahn6b1db822018-06-14 20:32:58 +0000578void MatcherGen::EmitResultOfNamedOperand(const TreePatternNode *N,
Chris Lattner132df652010-02-21 03:22:59 +0000579 SmallVectorImpl<unsigned> &ResultOps){
Florian Hahn6b1db822018-06-14 20:32:58 +0000580 assert(!N->getName().empty() && "Operand not named!");
Jim Grosbach65586fe2010-12-21 16:16:00 +0000581
Florian Hahn6b1db822018-06-14 20:32:58 +0000582 if (unsigned SlotNo = NamedComplexPatternOperands[N->getName()]) {
Tim Northoverc807a172014-05-20 11:52:46 +0000583 // Complex operands have already been completely selected, just find the
584 // right slot ant add the arguments directly.
Florian Hahn6b1db822018-06-14 20:32:58 +0000585 for (unsigned i = 0; i < N->getNumMIResults(CGP); ++i)
Tim Northoverc807a172014-05-20 11:52:46 +0000586 ResultOps.push_back(SlotNo - 1 + i);
Jim Grosbach65586fe2010-12-21 16:16:00 +0000587
Chris Lattner132df652010-02-21 03:22:59 +0000588 return;
589 }
590
Florian Hahn6b1db822018-06-14 20:32:58 +0000591 unsigned SlotNo = getNamedArgumentSlot(N->getName());
Chris Lattner0acbb712010-03-04 01:23:08 +0000592
Chris Lattner132df652010-02-21 03:22:59 +0000593 // If this is an 'imm' or 'fpimm' node, make sure to convert it to the target
594 // version of the immediate so that it doesn't get selected due to some other
595 // node use.
Florian Hahn6b1db822018-06-14 20:32:58 +0000596 if (!N->isLeaf()) {
597 StringRef OperatorName = N->getOperator()->getName();
Chris Lattner132df652010-02-21 03:22:59 +0000598 if (OperatorName == "imm" || OperatorName == "fpimm") {
Chris Lattner2c3f6492010-02-25 02:04:40 +0000599 AddMatcher(new EmitConvertToTargetMatcher(SlotNo));
Chris Lattner132df652010-02-21 03:22:59 +0000600 ResultOps.push_back(NextRecordedOperandNo++);
601 return;
602 }
603 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000604
Florian Hahn6b1db822018-06-14 20:32:58 +0000605 for (unsigned i = 0; i < N->getNumMIResults(CGP); ++i)
Tim Northoverc807a172014-05-20 11:52:46 +0000606 ResultOps.push_back(SlotNo + i);
Chris Lattner132df652010-02-21 03:22:59 +0000607}
608
Florian Hahn6b1db822018-06-14 20:32:58 +0000609void MatcherGen::EmitResultLeafAsOperand(const TreePatternNode *N,
Chris Lattner132df652010-02-21 03:22:59 +0000610 SmallVectorImpl<unsigned> &ResultOps) {
Florian Hahn6b1db822018-06-14 20:32:58 +0000611 assert(N->isLeaf() && "Must be a leaf");
Jim Grosbach65586fe2010-12-21 16:16:00 +0000612
Florian Hahn6b1db822018-06-14 20:32:58 +0000613 if (IntInit *II = dyn_cast<IntInit>(N->getLeafValue())) {
614 AddMatcher(new EmitIntegerMatcher(II->getValue(), N->getSimpleType(0)));
Chris Lattner132df652010-02-21 03:22:59 +0000615 ResultOps.push_back(NextRecordedOperandNo++);
Chris Lattner42a7ba72010-02-18 22:03:03 +0000616 return;
617 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000618
Chris Lattner42a7ba72010-02-18 22:03:03 +0000619 // If this is an explicit register reference, handle it.
Florian Hahn6b1db822018-06-14 20:32:58 +0000620 if (DefInit *DI = dyn_cast<DefInit>(N->getLeafValue())) {
Owen Andersona84be6c2011-06-27 21:06:21 +0000621 Record *Def = DI->getDef();
622 if (Def->isSubClassOf("Register")) {
Jakob Stoklund Olesen8e188be2011-06-18 04:26:06 +0000623 const CodeGenRegister *Reg =
Owen Andersona84be6c2011-06-27 21:06:21 +0000624 CGP.getTargetInfo().getRegBank().getReg(Def);
Florian Hahn6b1db822018-06-14 20:32:58 +0000625 AddMatcher(new EmitRegisterMatcher(Reg, N->getSimpleType(0)));
Chris Lattner132df652010-02-21 03:22:59 +0000626 ResultOps.push_back(NextRecordedOperandNo++);
Chris Lattner42a7ba72010-02-18 22:03:03 +0000627 return;
628 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000629
Owen Andersona84be6c2011-06-27 21:06:21 +0000630 if (Def->getName() == "zero_reg") {
Florian Hahn6b1db822018-06-14 20:32:58 +0000631 AddMatcher(new EmitRegisterMatcher(nullptr, N->getSimpleType(0)));
Chris Lattner132df652010-02-21 03:22:59 +0000632 ResultOps.push_back(NextRecordedOperandNo++);
Chris Lattner42a7ba72010-02-18 22:03:03 +0000633 return;
634 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000635
Chris Lattner132df652010-02-21 03:22:59 +0000636 // Handle a reference to a register class. This is used
637 // in COPY_TO_SUBREG instructions.
Owen Andersona84be6c2011-06-27 21:06:21 +0000638 if (Def->isSubClassOf("RegisterOperand"))
639 Def = Def->getValueAsDef("RegClass");
640 if (Def->isSubClassOf("RegisterClass")) {
641 std::string Value = getQualifiedName(Def) + "RegClassID";
Chris Lattner2c3f6492010-02-25 02:04:40 +0000642 AddMatcher(new EmitStringIntegerMatcher(Value, MVT::i32));
Chris Lattner132df652010-02-21 03:22:59 +0000643 ResultOps.push_back(NextRecordedOperandNo++);
644 return;
Chris Lattner42a7ba72010-02-18 22:03:03 +0000645 }
Jakob Stoklund Olesen1c696462010-05-24 14:48:12 +0000646
647 // Handle a subregister index. This is used for INSERT_SUBREG etc.
Owen Andersona84be6c2011-06-27 21:06:21 +0000648 if (Def->isSubClassOf("SubRegIndex")) {
649 std::string Value = getQualifiedName(Def);
Jakob Stoklund Olesen1c696462010-05-24 14:48:12 +0000650 AddMatcher(new EmitStringIntegerMatcher(Value, MVT::i32));
651 ResultOps.push_back(NextRecordedOperandNo++);
652 return;
653 }
Chris Lattner42a7ba72010-02-18 22:03:03 +0000654 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000655
Chris Lattner7ed81692010-02-18 06:47:49 +0000656 errs() << "unhandled leaf node: \n";
Florian Hahn6b1db822018-06-14 20:32:58 +0000657 N->dump();
Chris Lattner7ed81692010-02-18 06:47:49 +0000658}
659
Cameron Zwarich8b76e012011-05-19 21:13:30 +0000660static bool
Florian Hahn6b1db822018-06-14 20:32:58 +0000661mayInstNodeLoadOrStore(const TreePatternNode *N,
Cameron Zwarich8b76e012011-05-19 21:13:30 +0000662 const CodeGenDAGPatterns &CGP) {
Florian Hahn6b1db822018-06-14 20:32:58 +0000663 Record *Op = N->getOperator();
Cameron Zwarich8b76e012011-05-19 21:13:30 +0000664 const CodeGenTarget &CGT = CGP.getTargetInfo();
665 CodeGenInstruction &II = CGT.getInstruction(Op);
666 return II.mayLoad || II.mayStore;
667}
668
669static unsigned
Florian Hahn6b1db822018-06-14 20:32:58 +0000670numNodesThatMayLoadOrStore(const TreePatternNode *N,
Cameron Zwarich8b76e012011-05-19 21:13:30 +0000671 const CodeGenDAGPatterns &CGP) {
Florian Hahn6b1db822018-06-14 20:32:58 +0000672 if (N->isLeaf())
Cameron Zwarich8b76e012011-05-19 21:13:30 +0000673 return 0;
674
Florian Hahn6b1db822018-06-14 20:32:58 +0000675 Record *OpRec = N->getOperator();
Cameron Zwarich8b76e012011-05-19 21:13:30 +0000676 if (!OpRec->isSubClassOf("Instruction"))
677 return 0;
678
679 unsigned Count = 0;
680 if (mayInstNodeLoadOrStore(N, CGP))
681 ++Count;
682
Florian Hahn6b1db822018-06-14 20:32:58 +0000683 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
684 Count += numNodesThatMayLoadOrStore(N->getChild(i), CGP);
Cameron Zwarich8b76e012011-05-19 21:13:30 +0000685
686 return Count;
687}
688
Chris Lattner132df652010-02-21 03:22:59 +0000689void MatcherGen::
Florian Hahn6b1db822018-06-14 20:32:58 +0000690EmitResultInstructionAsOperand(const TreePatternNode *N,
Chris Lattner132df652010-02-21 03:22:59 +0000691 SmallVectorImpl<unsigned> &OutputOps) {
Florian Hahn6b1db822018-06-14 20:32:58 +0000692 Record *Op = N->getOperator();
Chris Lattner7ed81692010-02-18 06:47:49 +0000693 const CodeGenTarget &CGT = CGP.getTargetInfo();
Chris Lattner9aec14b2010-03-19 00:07:20 +0000694 CodeGenInstruction &II = CGT.getInstruction(Op);
Chris Lattner7ed81692010-02-18 06:47:49 +0000695 const DAGInstruction &Inst = CGP.getInstruction(Op);
Jim Grosbach65586fe2010-12-21 16:16:00 +0000696
Florian Hahn6b1db822018-06-14 20:32:58 +0000697 bool isRoot = N == Pattern.getDstPattern();
Chris Lattner132df652010-02-21 03:22:59 +0000698
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000699 // TreeHasOutGlue - True if this tree has glue.
700 bool TreeHasInGlue = false, TreeHasOutGlue = false;
Chris Lattner132df652010-02-21 03:22:59 +0000701 if (isRoot) {
702 const TreePatternNode *SrcPat = Pattern.getSrcPattern();
Chris Lattner2a0a3b42010-12-23 18:28:41 +0000703 TreeHasInGlue = SrcPat->TreeHasProperty(SDNPOptInGlue, CGP) ||
704 SrcPat->TreeHasProperty(SDNPInGlue, CGP);
Jim Grosbach65586fe2010-12-21 16:16:00 +0000705
Chris Lattner132df652010-02-21 03:22:59 +0000706 // FIXME2: this is checking the entire pattern, not just the node in
707 // question, doing this just for the root seems like a total hack.
Chris Lattner2a0a3b42010-12-23 18:28:41 +0000708 TreeHasOutGlue = SrcPat->TreeHasProperty(SDNPOutGlue, CGP);
Chris Lattner132df652010-02-21 03:22:59 +0000709 }
710
711 // NumResults - This is the number of results produced by the instruction in
712 // the "outs" list.
Jim Grosbach65586fe2010-12-21 16:16:00 +0000713 unsigned NumResults = Inst.getNumResults();
Chris Lattner7ed81692010-02-18 06:47:49 +0000714
Matt Arsenaulteb492162014-11-02 23:46:51 +0000715 // Number of operands we know the output instruction must have. If it is
716 // variadic, we could have more operands.
717 unsigned NumFixedOperands = II.Operands.size();
Jim Grosbach65586fe2010-12-21 16:16:00 +0000718
Matt Arsenaulteb492162014-11-02 23:46:51 +0000719 SmallVector<unsigned, 8> InstOps;
720
721 // Loop over all of the fixed operands of the instruction pattern, emitting
722 // code to fill them all in. The node 'N' usually has number children equal to
723 // the number of input operands of the instruction. However, in cases where
724 // there are predicate operands for an instruction, we need to fill in the
725 // 'execute always' values. Match up the node operands to the instruction
726 // operands to do this.
727 unsigned ChildNo = 0;
728 for (unsigned InstOpNo = NumResults, e = NumFixedOperands;
729 InstOpNo != e; ++InstOpNo) {
Chris Lattner7ed81692010-02-18 06:47:49 +0000730 // Determine what to emit for this operand.
Chris Lattnerd8adec72010-11-01 04:03:32 +0000731 Record *OperandNode = II.Operands[InstOpNo].Rec;
Tom Stellardb7246a72012-09-06 14:15:52 +0000732 if (OperandNode->isSubClassOf("OperandWithDefaultOps") &&
Chris Lattner7ed81692010-02-18 06:47:49 +0000733 !CGP.getDefaultOperand(OperandNode).DefaultOps.empty()) {
734 // This is a predicate or optional def operand; emit the
735 // 'default ops' operands.
Eric Christophere264e092010-08-10 23:46:20 +0000736 const DAGDefaultOperand &DefaultOp
Jim Grosbach65586fe2010-12-21 16:16:00 +0000737 = CGP.getDefaultOperand(OperandNode);
Chris Lattner7ed81692010-02-18 06:47:49 +0000738 for (unsigned i = 0, e = DefaultOp.DefaultOps.size(); i != e; ++i)
Florian Hahn6b1db822018-06-14 20:32:58 +0000739 EmitResultOperand(DefaultOp.DefaultOps[i].get(), InstOps);
Chris Lattner7ed81692010-02-18 06:47:49 +0000740 continue;
741 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000742
Chris Lattner7ed81692010-02-18 06:47:49 +0000743 // Otherwise this is a normal operand or a predicate operand without
744 // 'execute always'; emit it.
Jim Grosbach65586fe2010-12-21 16:16:00 +0000745
Ulrich Weigande618abd2013-03-19 19:51:09 +0000746 // For operands with multiple sub-operands we may need to emit
747 // multiple child patterns to cover them all. However, ComplexPattern
748 // children may themselves emit multiple MI operands.
749 unsigned NumSubOps = 1;
750 if (OperandNode->isSubClassOf("Operand")) {
751 DagInit *MIOpInfo = OperandNode->getValueAsDag("MIOperandInfo");
752 if (unsigned NumArgs = MIOpInfo->getNumArgs())
753 NumSubOps = NumArgs;
754 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000755
Ulrich Weigande618abd2013-03-19 19:51:09 +0000756 unsigned FinalNumOps = InstOps.size() + NumSubOps;
757 while (InstOps.size() < FinalNumOps) {
Florian Hahn6b1db822018-06-14 20:32:58 +0000758 const TreePatternNode *Child = N->getChild(ChildNo);
Ulrich Weigande618abd2013-03-19 19:51:09 +0000759 unsigned BeforeAddingNumOps = InstOps.size();
760 EmitResultOperand(Child, InstOps);
761 assert(InstOps.size() > BeforeAddingNumOps && "Didn't add any operands");
762
763 // If the operand is an instruction and it produced multiple results, just
764 // take the first one.
Florian Hahn6b1db822018-06-14 20:32:58 +0000765 if (!Child->isLeaf() && Child->getOperator()->isSubClassOf("Instruction"))
Ulrich Weigande618abd2013-03-19 19:51:09 +0000766 InstOps.resize(BeforeAddingNumOps+1);
767
768 ++ChildNo;
769 }
Chris Lattner7ed81692010-02-18 06:47:49 +0000770 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000771
Matt Arsenaulteb492162014-11-02 23:46:51 +0000772 // If this is a variadic output instruction (i.e. REG_SEQUENCE), we can't
773 // expand suboperands, use default operands, or other features determined from
774 // the CodeGenInstruction after the fixed operands, which were handled
775 // above. Emit the remaining instructions implicitly added by the use for
776 // variable_ops.
777 if (II.Operands.isVariadic) {
Florian Hahn6b1db822018-06-14 20:32:58 +0000778 for (unsigned I = ChildNo, E = N->getNumChildren(); I < E; ++I)
779 EmitResultOperand(N->getChild(I), InstOps);
Matt Arsenaulteb492162014-11-02 23:46:51 +0000780 }
781
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000782 // If this node has input glue or explicitly specified input physregs, we
783 // need to add chained and glued copyfromreg nodes and materialize the glue
Chris Lattner132df652010-02-21 03:22:59 +0000784 // input.
785 if (isRoot && !PhysRegInputs.empty()) {
786 // Emit all of the CopyToReg nodes for the input physical registers. These
787 // occur in patterns like (mul:i8 AL:i8, GR8:i8:$src).
788 for (unsigned i = 0, e = PhysRegInputs.size(); i != e; ++i)
Chris Lattner2c3f6492010-02-25 02:04:40 +0000789 AddMatcher(new EmitCopyToRegMatcher(PhysRegInputs[i].second,
Chris Lattner63a627c2010-03-18 23:57:40 +0000790 PhysRegInputs[i].first));
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000791 // Even if the node has no other glue inputs, the resultant node must be
792 // glued to the CopyFromReg nodes we just generated.
793 TreeHasInGlue = true;
Chris Lattner132df652010-02-21 03:22:59 +0000794 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000795
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000796 // Result order: node results, chain, glue
Jim Grosbach65586fe2010-12-21 16:16:00 +0000797
Chris Lattner132df652010-02-21 03:22:59 +0000798 // Determine the result types.
799 SmallVector<MVT::SimpleValueType, 4> ResultVTs;
Florian Hahn6b1db822018-06-14 20:32:58 +0000800 for (unsigned i = 0, e = N->getNumTypes(); i != e; ++i)
801 ResultVTs.push_back(N->getSimpleType(i));
Jim Grosbach65586fe2010-12-21 16:16:00 +0000802
Chris Lattner132df652010-02-21 03:22:59 +0000803 // If this is the root instruction of a pattern that has physical registers in
804 // its result pattern, add output VTs for them. For example, X86 has:
805 // (set AL, (mul ...))
806 // This also handles implicit results like:
807 // (implicit EFLAGS)
Chris Lattner725d3f62010-03-27 20:45:15 +0000808 if (isRoot && !Pattern.getDstRegs().empty()) {
Chris Lattner63a627c2010-03-18 23:57:40 +0000809 // If the root came from an implicit def in the instruction handling stuff,
810 // don't re-add it.
Craig Topper24064772014-04-15 07:20:03 +0000811 Record *HandledReg = nullptr;
Chris Lattner7bc5d9b2010-03-27 20:09:24 +0000812 if (II.HasOneImplicitDefWithKnownVT(CGT) != MVT::Other)
Chris Lattner63a627c2010-03-18 23:57:40 +0000813 HandledReg = II.ImplicitDefs[0];
Jim Grosbach65586fe2010-12-21 16:16:00 +0000814
Simon Pilgrim48bed532017-06-19 13:24:12 +0000815 for (Record *Reg : Pattern.getDstRegs()) {
Chris Lattner63a627c2010-03-18 23:57:40 +0000816 if (!Reg->isSubClassOf("Register") || Reg == HandledReg) continue;
817 ResultVTs.push_back(getRegisterValueType(Reg, CGT));
818 }
Chris Lattner132df652010-02-21 03:22:59 +0000819 }
Chris Lattner132df652010-02-21 03:22:59 +0000820
Chris Lattnerbb34b4e2010-03-19 05:34:15 +0000821 // If this is the root of the pattern and the pattern we're matching includes
822 // a node that is variadic, mark the generated node as variadic so that it
823 // gets the excess operands from the input DAG.
Chris Lattner132df652010-02-21 03:22:59 +0000824 int NumFixedArityOperands = -1;
Chris Lattnerbb34b4e2010-03-19 05:34:15 +0000825 if (isRoot &&
Matt Arsenaulteb492162014-11-02 23:46:51 +0000826 Pattern.getSrcPattern()->NodeHasProperty(SDNPVariadic, CGP))
Chris Lattner132df652010-02-21 03:22:59 +0000827 NumFixedArityOperands = Pattern.getSrcPattern()->getNumChildren();
Jim Grosbach65586fe2010-12-21 16:16:00 +0000828
Cameron Zwarich8b76e012011-05-19 21:13:30 +0000829 // If this is the root node and multiple matched nodes in the input pattern
830 // have MemRefs in them, have the interpreter collect them and plop them onto
831 // this node. If there is just one node with MemRefs, leave them on that node
832 // even if it is not the root.
Chris Lattner132df652010-02-21 03:22:59 +0000833 //
Cameron Zwarich8b76e012011-05-19 21:13:30 +0000834 // FIXME3: This is actively incorrect for result patterns with multiple
835 // memory-referencing instructions.
836 bool PatternHasMemOperands =
837 Pattern.getSrcPattern()->TreeHasProperty(SDNPMemOperand, CGP);
838
839 bool NodeHasMemRefs = false;
840 if (PatternHasMemOperands) {
841 unsigned NumNodesThatLoadOrStore =
Florian Hahn6b1db822018-06-14 20:32:58 +0000842 numNodesThatMayLoadOrStore(Pattern.getDstPattern(), CGP);
Cameron Zwarich8b76e012011-05-19 21:13:30 +0000843 bool NodeIsUniqueLoadOrStore = mayInstNodeLoadOrStore(N, CGP) &&
844 NumNodesThatLoadOrStore == 1;
845 NodeHasMemRefs =
846 NodeIsUniqueLoadOrStore || (isRoot && (mayInstNodeLoadOrStore(N, CGP) ||
847 NumNodesThatLoadOrStore != 1));
848 }
Chris Lattner132df652010-02-21 03:22:59 +0000849
Ulrich Weigandc48aefb2018-07-13 13:18:00 +0000850 // Determine whether we need to attach a chain to this node.
851 bool NodeHasChain = false;
852 if (Pattern.getSrcPattern()->TreeHasProperty(SDNPHasChain, CGP)) {
853 // For some instructions, we were able to infer from the pattern whether
854 // they should have a chain. Otherwise, attach the chain to the root.
855 //
856 // FIXME2: This is extremely dubious for several reasons, not the least of
857 // which it gives special status to instructions with patterns that Pat<>
858 // nodes can't duplicate.
859 if (II.hasChain_Inferred)
860 NodeHasChain = II.hasChain;
861 else
862 NodeHasChain = isRoot;
863 // Instructions which load and store from memory should have a chain,
864 // regardless of whether they happen to have a pattern saying so.
865 if (II.hasCtrlDep || II.mayLoad || II.mayStore || II.canFoldAsLoad ||
866 II.hasSideEffects)
867 NodeHasChain = true;
868 }
869
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000870 assert((!ResultVTs.empty() || TreeHasOutGlue || NodeHasChain) &&
Chris Lattnerd44966f2010-03-27 19:15:02 +0000871 "Node has no result");
Jim Grosbach65586fe2010-12-21 16:16:00 +0000872
Craig Topper86a9aee2017-07-07 06:22:35 +0000873 AddMatcher(new EmitNodeMatcher(II.Namespace.str()+"::"+II.TheDef->getName().str(),
Craig Toppera2c60192014-01-21 07:20:05 +0000874 ResultVTs, InstOps,
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000875 NodeHasChain, TreeHasInGlue, TreeHasOutGlue,
Chris Lattnerabb1c792010-02-28 02:41:25 +0000876 NodeHasMemRefs, NumFixedArityOperands,
877 NextRecordedOperandNo));
Jim Grosbach65586fe2010-12-21 16:16:00 +0000878
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000879 // The non-chain and non-glue results of the newly emitted node get recorded.
Chris Lattner9fd97c32010-02-21 23:54:05 +0000880 for (unsigned i = 0, e = ResultVTs.size(); i != e; ++i) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000881 if (ResultVTs[i] == MVT::Other || ResultVTs[i] == MVT::Glue) break;
Chris Lattner79eaeb42010-02-21 06:03:07 +0000882 OutputOps.push_back(NextRecordedOperandNo++);
Chris Lattner9fd97c32010-02-21 23:54:05 +0000883 }
Chris Lattner132df652010-02-21 03:22:59 +0000884}
885
886void MatcherGen::
Florian Hahn6b1db822018-06-14 20:32:58 +0000887EmitResultSDNodeXFormAsOperand(const TreePatternNode *N,
Chris Lattner132df652010-02-21 03:22:59 +0000888 SmallVectorImpl<unsigned> &ResultOps) {
Florian Hahn6b1db822018-06-14 20:32:58 +0000889 assert(N->getOperator()->isSubClassOf("SDNodeXForm") && "Not SDNodeXForm?");
Chris Lattner132df652010-02-21 03:22:59 +0000890
891 // Emit the operand.
892 SmallVector<unsigned, 8> InputOps;
Jim Grosbach65586fe2010-12-21 16:16:00 +0000893
Chris Lattner132df652010-02-21 03:22:59 +0000894 // FIXME2: Could easily generalize this to support multiple inputs and outputs
895 // to the SDNodeXForm. For now we just support one input and one output like
896 // the old instruction selector.
Florian Hahn6b1db822018-06-14 20:32:58 +0000897 assert(N->getNumChildren() == 1);
898 EmitResultOperand(N->getChild(0), InputOps);
Chris Lattner132df652010-02-21 03:22:59 +0000899
900 // The input currently must have produced exactly one result.
901 assert(InputOps.size() == 1 && "Unexpected input to SDNodeXForm");
902
Florian Hahn6b1db822018-06-14 20:32:58 +0000903 AddMatcher(new EmitNodeXFormMatcher(InputOps[0], N->getOperator()));
Chris Lattner132df652010-02-21 03:22:59 +0000904 ResultOps.push_back(NextRecordedOperandNo++);
Chris Lattner7ed81692010-02-18 06:47:49 +0000905}
906
Florian Hahn6b1db822018-06-14 20:32:58 +0000907void MatcherGen::EmitResultOperand(const TreePatternNode *N,
Chris Lattner132df652010-02-21 03:22:59 +0000908 SmallVectorImpl<unsigned> &ResultOps) {
Chris Lattner7ed81692010-02-18 06:47:49 +0000909 // This is something selected from the pattern we matched.
Florian Hahn6b1db822018-06-14 20:32:58 +0000910 if (!N->getName().empty())
Chris Lattner132df652010-02-21 03:22:59 +0000911 return EmitResultOfNamedOperand(N, ResultOps);
Chris Lattner7ed81692010-02-18 06:47:49 +0000912
Florian Hahn6b1db822018-06-14 20:32:58 +0000913 if (N->isLeaf())
Chris Lattner7ed81692010-02-18 06:47:49 +0000914 return EmitResultLeafAsOperand(N, ResultOps);
915
Florian Hahn6b1db822018-06-14 20:32:58 +0000916 Record *OpRec = N->getOperator();
Chris Lattner7ed81692010-02-18 06:47:49 +0000917 if (OpRec->isSubClassOf("Instruction"))
918 return EmitResultInstructionAsOperand(N, ResultOps);
919 if (OpRec->isSubClassOf("SDNodeXForm"))
Chris Lattner132df652010-02-21 03:22:59 +0000920 return EmitResultSDNodeXFormAsOperand(N, ResultOps);
Florian Hahn6b1db822018-06-14 20:32:58 +0000921 errs() << "Unknown result node to emit code for: " << *N << '\n';
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000922 PrintFatalError("Unknown node in result pattern!");
Chris Lattner7ed81692010-02-18 06:47:49 +0000923}
924
925void MatcherGen::EmitResultCode() {
Chris Lattner28182722010-03-01 22:46:42 +0000926 // Patterns that match nodes with (potentially multiple) chain inputs have to
927 // merge them together into a token factor. This informs the generated code
928 // what all the chained nodes are.
929 if (!MatchedChainNodes.empty())
Craig Toppera2c60192014-01-21 07:20:05 +0000930 AddMatcher(new EmitMergeInputChainsMatcher(MatchedChainNodes));
Jim Grosbach65586fe2010-12-21 16:16:00 +0000931
Chris Lattner9fd97c32010-02-21 23:54:05 +0000932 // Codegen the root of the result pattern, capturing the resulting values.
Chris Lattner132df652010-02-21 03:22:59 +0000933 SmallVector<unsigned, 8> Ops;
Florian Hahn6b1db822018-06-14 20:32:58 +0000934 EmitResultOperand(Pattern.getDstPattern(), Ops);
Chris Lattner132df652010-02-21 03:22:59 +0000935
Chris Lattner9fd97c32010-02-21 23:54:05 +0000936 // At this point, we have however many values the result pattern produces.
937 // However, the input pattern might not need all of these. If there are
Chris Lattner725d3f62010-03-27 20:45:15 +0000938 // excess values at the end (such as implicit defs of condition codes etc)
Chris Lattnerf7f9e8c2010-12-23 17:03:20 +0000939 // just lop them off. This doesn't need to worry about glue or chains, just
Chris Lattner725d3f62010-03-27 20:45:15 +0000940 // explicit results.
Chris Lattner9fd97c32010-02-21 23:54:05 +0000941 //
Chris Lattner725d3f62010-03-27 20:45:15 +0000942 unsigned NumSrcResults = Pattern.getSrcPattern()->getNumTypes();
Jim Grosbach65586fe2010-12-21 16:16:00 +0000943
Chris Lattner725d3f62010-03-27 20:45:15 +0000944 // If the pattern also has (implicit) results, count them as well.
945 if (!Pattern.getDstRegs().empty()) {
946 // If the root came from an implicit def in the instruction handling stuff,
947 // don't re-add it.
Craig Topper24064772014-04-15 07:20:03 +0000948 Record *HandledReg = nullptr;
Chris Lattner725d3f62010-03-27 20:45:15 +0000949 const TreePatternNode *DstPat = Pattern.getDstPattern();
950 if (!DstPat->isLeaf() &&DstPat->getOperator()->isSubClassOf("Instruction")){
951 const CodeGenTarget &CGT = CGP.getTargetInfo();
952 CodeGenInstruction &II = CGT.getInstruction(DstPat->getOperator());
953
954 if (II.HasOneImplicitDefWithKnownVT(CGT) != MVT::Other)
955 HandledReg = II.ImplicitDefs[0];
956 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000957
Simon Pilgrim48bed532017-06-19 13:24:12 +0000958 for (Record *Reg : Pattern.getDstRegs()) {
Chris Lattner725d3f62010-03-27 20:45:15 +0000959 if (!Reg->isSubClassOf("Register") || Reg == HandledReg) continue;
960 ++NumSrcResults;
961 }
Jim Grosbach65586fe2010-12-21 16:16:00 +0000962 }
963
Chris Lattner9fd97c32010-02-21 23:54:05 +0000964 assert(Ops.size() >= NumSrcResults && "Didn't provide enough results");
965 Ops.resize(NumSrcResults);
Chris Lattner29364372010-02-24 05:33:42 +0000966
Craig Toppera2c60192014-01-21 07:20:05 +0000967 AddMatcher(new CompleteMatchMatcher(Ops, Pattern));
Chris Lattner7ed81692010-02-18 06:47:49 +0000968}
969
970
Chris Lattner053a28a2010-03-01 07:17:40 +0000971/// ConvertPatternToMatcher - Create the matcher for the specified pattern with
972/// the specified variant. If the variant number is invalid, this returns null.
Chris Lattner2c3f6492010-02-25 02:04:40 +0000973Matcher *llvm::ConvertPatternToMatcher(const PatternToMatch &Pattern,
Chris Lattner053a28a2010-03-01 07:17:40 +0000974 unsigned Variant,
Chris Lattner102a8a02010-02-28 20:49:53 +0000975 const CodeGenDAGPatterns &CGP) {
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000976 MatcherGen Gen(Pattern, CGP);
977
978 // Generate the code for the matcher.
Chris Lattner053a28a2010-03-01 07:17:40 +0000979 if (Gen.EmitMatcherCode(Variant))
Craig Topper24064772014-04-15 07:20:03 +0000980 return nullptr;
Jim Grosbach65586fe2010-12-21 16:16:00 +0000981
Chris Lattner132df652010-02-21 03:22:59 +0000982 // FIXME2: Kill extra MoveParent commands at the end of the matcher sequence.
983 // FIXME2: Split result code out to another table, and make the matcher end
984 // with an "Emit <index>" command. This allows result generation stuff to be
985 // shared and factored?
Jim Grosbach65586fe2010-12-21 16:16:00 +0000986
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000987 // If the match succeeds, then we generate Pattern.
Chris Lattner7ed81692010-02-18 06:47:49 +0000988 Gen.EmitResultCode();
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000989
990 // Unconditional match.
Chris Lattner7ed81692010-02-18 06:47:49 +0000991 return Gen.GetMatcher();
Chris Lattnerb02cdaa2010-02-15 08:04:42 +0000992}