[llvm-exegesis] [NFC] Fixing typo.
[llvm-complete.git] / utils / TableGen / DAGISelMatcher.cpp
blob121618de916553451884d55b7f6d6b320cd6cabe
1 //===- DAGISelMatcher.cpp - Representation of DAG pattern matcher ---------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "DAGISelMatcher.h"
10 #include "CodeGenDAGPatterns.h"
11 #include "CodeGenTarget.h"
12 #include "llvm/Support/raw_ostream.h"
13 #include "llvm/TableGen/Record.h"
14 using namespace llvm;
16 void Matcher::anchor() { }
18 void Matcher::dump() const {
19 print(errs(), 0);
22 void Matcher::print(raw_ostream &OS, unsigned indent) const {
23 printImpl(OS, indent);
24 if (Next)
25 return Next->print(OS, indent);
28 void Matcher::printOne(raw_ostream &OS) const {
29 printImpl(OS, 0);
32 /// unlinkNode - Unlink the specified node from this chain. If Other == this,
33 /// we unlink the next pointer and return it. Otherwise we unlink Other from
34 /// the list and return this.
35 Matcher *Matcher::unlinkNode(Matcher *Other) {
36 if (this == Other)
37 return takeNext();
39 // Scan until we find the predecessor of Other.
40 Matcher *Cur = this;
41 for (; Cur && Cur->getNext() != Other; Cur = Cur->getNext())
42 /*empty*/;
44 if (!Cur) return nullptr;
45 Cur->takeNext();
46 Cur->setNext(Other->takeNext());
47 return this;
50 /// canMoveBefore - Return true if this matcher is the same as Other, or if
51 /// we can move this matcher past all of the nodes in-between Other and this
52 /// node. Other must be equal to or before this.
53 bool Matcher::canMoveBefore(const Matcher *Other) const {
54 for (;; Other = Other->getNext()) {
55 assert(Other && "Other didn't come before 'this'?");
56 if (this == Other) return true;
58 // We have to be able to move this node across the Other node.
59 if (!canMoveBeforeNode(Other))
60 return false;
64 /// canMoveBeforeNode - Return true if it is safe to move the current matcher
65 /// across the specified one.
66 bool Matcher::canMoveBeforeNode(const Matcher *Other) const {
67 // We can move simple predicates before record nodes.
68 if (isSimplePredicateNode())
69 return Other->isSimplePredicateOrRecordNode();
71 // We can move record nodes across simple predicates.
72 if (isSimplePredicateOrRecordNode())
73 return isSimplePredicateNode();
75 // We can't move record nodes across each other etc.
76 return false;
80 ScopeMatcher::~ScopeMatcher() {
81 for (Matcher *C : Children)
82 delete C;
85 SwitchOpcodeMatcher::~SwitchOpcodeMatcher() {
86 for (auto &C : Cases)
87 delete C.second;
90 SwitchTypeMatcher::~SwitchTypeMatcher() {
91 for (auto &C : Cases)
92 delete C.second;
95 CheckPredicateMatcher::CheckPredicateMatcher(
96 const TreePredicateFn &pred, const SmallVectorImpl<unsigned> &Ops)
97 : Matcher(CheckPredicate), Pred(pred.getOrigPatFragRecord()),
98 Operands(Ops.begin(), Ops.end()) {}
100 TreePredicateFn CheckPredicateMatcher::getPredicate() const {
101 return TreePredicateFn(Pred);
104 unsigned CheckPredicateMatcher::getNumOperands() const {
105 return Operands.size();
108 unsigned CheckPredicateMatcher::getOperandNo(unsigned i) const {
109 assert(i < Operands.size());
110 return Operands[i];
114 // printImpl methods.
116 void ScopeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
117 OS.indent(indent) << "Scope\n";
118 for (const Matcher *C : Children) {
119 if (!C)
120 OS.indent(indent+1) << "NULL POINTER\n";
121 else
122 C->print(OS, indent+2);
126 void RecordMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
127 OS.indent(indent) << "Record\n";
130 void RecordChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
131 OS.indent(indent) << "RecordChild: " << ChildNo << '\n';
134 void RecordMemRefMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
135 OS.indent(indent) << "RecordMemRef\n";
138 void CaptureGlueInputMatcher::printImpl(raw_ostream &OS, unsigned indent) const{
139 OS.indent(indent) << "CaptureGlueInput\n";
142 void MoveChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
143 OS.indent(indent) << "MoveChild " << ChildNo << '\n';
146 void MoveParentMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
147 OS.indent(indent) << "MoveParent\n";
150 void CheckSameMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
151 OS.indent(indent) << "CheckSame " << MatchNumber << '\n';
154 void CheckChildSameMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
155 OS.indent(indent) << "CheckChild" << ChildNo << "Same\n";
158 void CheckPatternPredicateMatcher::
159 printImpl(raw_ostream &OS, unsigned indent) const {
160 OS.indent(indent) << "CheckPatternPredicate " << Predicate << '\n';
163 void CheckPredicateMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
164 OS.indent(indent) << "CheckPredicate " << getPredicate().getFnName() << '\n';
167 void CheckOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
168 OS.indent(indent) << "CheckOpcode " << Opcode.getEnumName() << '\n';
171 void SwitchOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
172 OS.indent(indent) << "SwitchOpcode: {\n";
173 for (const auto &C : Cases) {
174 OS.indent(indent) << "case " << C.first->getEnumName() << ":\n";
175 C.second->print(OS, indent+2);
177 OS.indent(indent) << "}\n";
181 void CheckTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
182 OS.indent(indent) << "CheckType " << getEnumName(Type) << ", ResNo="
183 << ResNo << '\n';
186 void SwitchTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
187 OS.indent(indent) << "SwitchType: {\n";
188 for (const auto &C : Cases) {
189 OS.indent(indent) << "case " << getEnumName(C.first) << ":\n";
190 C.second->print(OS, indent+2);
192 OS.indent(indent) << "}\n";
195 void CheckChildTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
196 OS.indent(indent) << "CheckChildType " << ChildNo << " "
197 << getEnumName(Type) << '\n';
201 void CheckIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
202 OS.indent(indent) << "CheckInteger " << Value << '\n';
205 void CheckChildIntegerMatcher::printImpl(raw_ostream &OS,
206 unsigned indent) const {
207 OS.indent(indent) << "CheckChildInteger " << ChildNo << " " << Value << '\n';
210 void CheckCondCodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
211 OS.indent(indent) << "CheckCondCode ISD::" << CondCodeName << '\n';
214 void CheckValueTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
215 OS.indent(indent) << "CheckValueType MVT::" << TypeName << '\n';
218 void CheckComplexPatMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
219 OS.indent(indent) << "CheckComplexPat " << Pattern.getSelectFunc() << '\n';
222 void CheckAndImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
223 OS.indent(indent) << "CheckAndImm " << Value << '\n';
226 void CheckOrImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
227 OS.indent(indent) << "CheckOrImm " << Value << '\n';
230 void CheckFoldableChainNodeMatcher::printImpl(raw_ostream &OS,
231 unsigned indent) const {
232 OS.indent(indent) << "CheckFoldableChainNode\n";
235 void EmitIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
236 OS.indent(indent) << "EmitInteger " << Val << " VT=" << getEnumName(VT)
237 << '\n';
240 void EmitStringIntegerMatcher::
241 printImpl(raw_ostream &OS, unsigned indent) const {
242 OS.indent(indent) << "EmitStringInteger " << Val << " VT=" << getEnumName(VT)
243 << '\n';
246 void EmitRegisterMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
247 OS.indent(indent) << "EmitRegister ";
248 if (Reg)
249 OS << Reg->getName();
250 else
251 OS << "zero_reg";
252 OS << " VT=" << getEnumName(VT) << '\n';
255 void EmitConvertToTargetMatcher::
256 printImpl(raw_ostream &OS, unsigned indent) const {
257 OS.indent(indent) << "EmitConvertToTarget " << Slot << '\n';
260 void EmitMergeInputChainsMatcher::
261 printImpl(raw_ostream &OS, unsigned indent) const {
262 OS.indent(indent) << "EmitMergeInputChains <todo: args>\n";
265 void EmitCopyToRegMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
266 OS.indent(indent) << "EmitCopyToReg <todo: args>\n";
269 void EmitNodeXFormMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
270 OS.indent(indent) << "EmitNodeXForm " << NodeXForm->getName()
271 << " Slot=" << Slot << '\n';
275 void EmitNodeMatcherCommon::printImpl(raw_ostream &OS, unsigned indent) const {
276 OS.indent(indent);
277 OS << (isa<MorphNodeToMatcher>(this) ? "MorphNodeTo: " : "EmitNode: ")
278 << OpcodeName << ": <todo flags> ";
280 for (unsigned i = 0, e = VTs.size(); i != e; ++i)
281 OS << ' ' << getEnumName(VTs[i]);
282 OS << '(';
283 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
284 OS << Operands[i] << ' ';
285 OS << ")\n";
288 void CompleteMatchMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
289 OS.indent(indent) << "CompleteMatch <todo args>\n";
290 OS.indent(indent) << "Src = " << *Pattern.getSrcPattern() << "\n";
291 OS.indent(indent) << "Dst = " << *Pattern.getDstPattern() << "\n";
294 bool CheckOpcodeMatcher::isEqualImpl(const Matcher *M) const {
295 // Note: pointer equality isn't enough here, we have to check the enum names
296 // to ensure that the nodes are for the same opcode.
297 return cast<CheckOpcodeMatcher>(M)->Opcode.getEnumName() ==
298 Opcode.getEnumName();
301 bool EmitNodeMatcherCommon::isEqualImpl(const Matcher *m) const {
302 const EmitNodeMatcherCommon *M = cast<EmitNodeMatcherCommon>(m);
303 return M->OpcodeName == OpcodeName && M->VTs == VTs &&
304 M->Operands == Operands && M->HasChain == HasChain &&
305 M->HasInGlue == HasInGlue && M->HasOutGlue == HasOutGlue &&
306 M->HasMemRefs == HasMemRefs &&
307 M->NumFixedArityOperands == NumFixedArityOperands;
310 void EmitNodeMatcher::anchor() { }
312 void MorphNodeToMatcher::anchor() { }
314 // isContradictoryImpl Implementations.
316 static bool TypesAreContradictory(MVT::SimpleValueType T1,
317 MVT::SimpleValueType T2) {
318 // If the two types are the same, then they are the same, so they don't
319 // contradict.
320 if (T1 == T2) return false;
322 // If either type is about iPtr, then they don't conflict unless the other
323 // one is not a scalar integer type.
324 if (T1 == MVT::iPTR)
325 return !MVT(T2).isInteger() || MVT(T2).isVector();
327 if (T2 == MVT::iPTR)
328 return !MVT(T1).isInteger() || MVT(T1).isVector();
330 // Otherwise, they are two different non-iPTR types, they conflict.
331 return true;
334 bool CheckOpcodeMatcher::isContradictoryImpl(const Matcher *M) const {
335 if (const CheckOpcodeMatcher *COM = dyn_cast<CheckOpcodeMatcher>(M)) {
336 // One node can't have two different opcodes!
337 // Note: pointer equality isn't enough here, we have to check the enum names
338 // to ensure that the nodes are for the same opcode.
339 return COM->getOpcode().getEnumName() != getOpcode().getEnumName();
342 // If the node has a known type, and if the type we're checking for is
343 // different, then we know they contradict. For example, a check for
344 // ISD::STORE will never be true at the same time a check for Type i32 is.
345 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M)) {
346 // If checking for a result the opcode doesn't have, it can't match.
347 if (CT->getResNo() >= getOpcode().getNumResults())
348 return true;
350 MVT::SimpleValueType NodeType = getOpcode().getKnownType(CT->getResNo());
351 if (NodeType != MVT::Other)
352 return TypesAreContradictory(NodeType, CT->getType());
355 return false;
358 bool CheckTypeMatcher::isContradictoryImpl(const Matcher *M) const {
359 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M))
360 return TypesAreContradictory(getType(), CT->getType());
361 return false;
364 bool CheckChildTypeMatcher::isContradictoryImpl(const Matcher *M) const {
365 if (const CheckChildTypeMatcher *CC = dyn_cast<CheckChildTypeMatcher>(M)) {
366 // If the two checks are about different nodes, we don't know if they
367 // conflict!
368 if (CC->getChildNo() != getChildNo())
369 return false;
371 return TypesAreContradictory(getType(), CC->getType());
373 return false;
376 bool CheckIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
377 if (const CheckIntegerMatcher *CIM = dyn_cast<CheckIntegerMatcher>(M))
378 return CIM->getValue() != getValue();
379 return false;
382 bool CheckChildIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
383 if (const CheckChildIntegerMatcher *CCIM = dyn_cast<CheckChildIntegerMatcher>(M)) {
384 // If the two checks are about different nodes, we don't know if they
385 // conflict!
386 if (CCIM->getChildNo() != getChildNo())
387 return false;
389 return CCIM->getValue() != getValue();
391 return false;
394 bool CheckValueTypeMatcher::isContradictoryImpl(const Matcher *M) const {
395 if (const CheckValueTypeMatcher *CVT = dyn_cast<CheckValueTypeMatcher>(M))
396 return CVT->getTypeName() != getTypeName();
397 return false;