1 //===- DAGISelMatcher.cpp - Representation of DAG pattern matcher ---------===//
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
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"
16 void Matcher::anchor() { }
18 void Matcher::dump() const {
22 void Matcher::print(raw_ostream
&OS
, unsigned indent
) const {
23 printImpl(OS
, indent
);
25 return Next
->print(OS
, indent
);
28 void Matcher::printOne(raw_ostream
&OS
) const {
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
) {
39 // Scan until we find the predecessor of Other.
41 for (; Cur
&& Cur
->getNext() != Other
; Cur
= Cur
->getNext())
44 if (!Cur
) return nullptr;
46 Cur
->setNext(Other
->takeNext());
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
))
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.
80 ScopeMatcher::~ScopeMatcher() {
81 for (Matcher
*C
: Children
)
85 SwitchOpcodeMatcher::~SwitchOpcodeMatcher() {
90 SwitchTypeMatcher::~SwitchTypeMatcher() {
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());
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
) {
120 OS
.indent(indent
+1) << "NULL POINTER\n";
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="
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
)
240 void EmitStringIntegerMatcher::
241 printImpl(raw_ostream
&OS
, unsigned indent
) const {
242 OS
.indent(indent
) << "EmitStringInteger " << Val
<< " VT=" << getEnumName(VT
)
246 void EmitRegisterMatcher::printImpl(raw_ostream
&OS
, unsigned indent
) const {
247 OS
.indent(indent
) << "EmitRegister ";
249 OS
<< Reg
->getName();
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 {
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
]);
283 for (unsigned i
= 0, e
= Operands
.size(); i
!= e
; ++i
)
284 OS
<< Operands
[i
] << ' ';
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
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.
325 return !MVT(T2
).isInteger() || MVT(T2
).isVector();
328 return !MVT(T1
).isInteger() || MVT(T1
).isVector();
330 // Otherwise, they are two different non-iPTR types, they conflict.
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())
350 MVT::SimpleValueType NodeType
= getOpcode().getKnownType(CT
->getResNo());
351 if (NodeType
!= MVT::Other
)
352 return TypesAreContradictory(NodeType
, CT
->getType());
358 bool CheckTypeMatcher::isContradictoryImpl(const Matcher
*M
) const {
359 if (const CheckTypeMatcher
*CT
= dyn_cast
<CheckTypeMatcher
>(M
))
360 return TypesAreContradictory(getType(), CT
->getType());
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
368 if (CC
->getChildNo() != getChildNo())
371 return TypesAreContradictory(getType(), CC
->getType());
376 bool CheckIntegerMatcher::isContradictoryImpl(const Matcher
*M
) const {
377 if (const CheckIntegerMatcher
*CIM
= dyn_cast
<CheckIntegerMatcher
>(M
))
378 return CIM
->getValue() != getValue();
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
386 if (CCIM
->getChildNo() != getChildNo())
389 return CCIM
->getValue() != getValue();
394 bool CheckValueTypeMatcher::isContradictoryImpl(const Matcher
*M
) const {
395 if (const CheckValueTypeMatcher
*CVT
= dyn_cast
<CheckValueTypeMatcher
>(M
))
396 return CVT
->getTypeName() != getTypeName();