[llvm-shlib] Fix the version naming style of libLLVM for Windows (#85710)
[llvm-project.git] / llvm / utils / TableGen / DAGISelMatcher.cpp
blob1a5c728fafd9ca141ea381fbb0da23651bb4d0c7
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 "CodeGenInstruction.h"
12 #include "CodeGenRegisters.h"
13 #include "CodeGenTarget.h"
14 #include "llvm/Support/raw_ostream.h"
15 #include "llvm/TableGen/Record.h"
16 using namespace llvm;
18 void Matcher::anchor() { }
20 void Matcher::dump() const {
21 print(errs(), 0);
24 void Matcher::print(raw_ostream &OS, unsigned indent) const {
25 printImpl(OS, indent);
26 if (Next)
27 return Next->print(OS, indent);
30 void Matcher::printOne(raw_ostream &OS) const {
31 printImpl(OS, 0);
34 /// unlinkNode - Unlink the specified node from this chain. If Other == this,
35 /// we unlink the next pointer and return it. Otherwise we unlink Other from
36 /// the list and return this.
37 Matcher *Matcher::unlinkNode(Matcher *Other) {
38 if (this == Other)
39 return takeNext();
41 // Scan until we find the predecessor of Other.
42 Matcher *Cur = this;
43 for (; Cur && Cur->getNext() != Other; Cur = Cur->getNext())
44 /*empty*/;
46 if (!Cur) return nullptr;
47 Cur->takeNext();
48 Cur->setNext(Other->takeNext());
49 return this;
52 /// canMoveBefore - Return true if this matcher is the same as Other, or if
53 /// we can move this matcher past all of the nodes in-between Other and this
54 /// node. Other must be equal to or before this.
55 bool Matcher::canMoveBefore(const Matcher *Other) const {
56 for (;; Other = Other->getNext()) {
57 assert(Other && "Other didn't come before 'this'?");
58 if (this == Other) return true;
60 // We have to be able to move this node across the Other node.
61 if (!canMoveBeforeNode(Other))
62 return false;
66 /// canMoveBeforeNode - Return true if it is safe to move the current matcher
67 /// across the specified one.
68 bool Matcher::canMoveBeforeNode(const Matcher *Other) const {
69 // We can move simple predicates before record nodes.
70 if (isSimplePredicateNode())
71 return Other->isSimplePredicateOrRecordNode();
73 // We can move record nodes across simple predicates.
74 if (isSimplePredicateOrRecordNode())
75 return isSimplePredicateNode();
77 // We can't move record nodes across each other etc.
78 return false;
82 ScopeMatcher::~ScopeMatcher() {
83 for (Matcher *C : Children)
84 delete C;
87 SwitchOpcodeMatcher::~SwitchOpcodeMatcher() {
88 for (auto &C : Cases)
89 delete C.second;
92 SwitchTypeMatcher::~SwitchTypeMatcher() {
93 for (auto &C : Cases)
94 delete C.second;
97 CheckPredicateMatcher::CheckPredicateMatcher(
98 const TreePredicateFn &pred, const SmallVectorImpl<unsigned> &Ops)
99 : Matcher(CheckPredicate), Pred(pred.getOrigPatFragRecord()),
100 Operands(Ops.begin(), Ops.end()) {}
102 TreePredicateFn CheckPredicateMatcher::getPredicate() const {
103 return TreePredicateFn(Pred);
106 unsigned CheckPredicateMatcher::getNumOperands() const {
107 return Operands.size();
110 unsigned CheckPredicateMatcher::getOperandNo(unsigned i) const {
111 assert(i < Operands.size());
112 return Operands[i];
116 // printImpl methods.
118 void ScopeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
119 OS.indent(indent) << "Scope\n";
120 for (const Matcher *C : Children) {
121 if (!C)
122 OS.indent(indent+1) << "NULL POINTER\n";
123 else
124 C->print(OS, indent+2);
128 void RecordMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
129 OS.indent(indent) << "Record\n";
132 void RecordChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
133 OS.indent(indent) << "RecordChild: " << ChildNo << '\n';
136 void RecordMemRefMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
137 OS.indent(indent) << "RecordMemRef\n";
140 void CaptureGlueInputMatcher::printImpl(raw_ostream &OS, unsigned indent) const{
141 OS.indent(indent) << "CaptureGlueInput\n";
144 void MoveChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
145 OS.indent(indent) << "MoveChild " << ChildNo << '\n';
148 void MoveSiblingMatcher::printImpl(raw_ostream &OS, unsigned Indent) const {
149 OS.indent(Indent) << "MoveSibling " << SiblingNo << '\n';
152 void MoveParentMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
153 OS.indent(indent) << "MoveParent\n";
156 void CheckSameMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
157 OS.indent(indent) << "CheckSame " << MatchNumber << '\n';
160 void CheckChildSameMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
161 OS.indent(indent) << "CheckChild" << ChildNo << "Same\n";
164 void CheckPatternPredicateMatcher::
165 printImpl(raw_ostream &OS, unsigned indent) const {
166 OS.indent(indent) << "CheckPatternPredicate " << Predicate << '\n';
169 void CheckPredicateMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
170 OS.indent(indent) << "CheckPredicate " << getPredicate().getFnName() << '\n';
173 void CheckOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
174 OS.indent(indent) << "CheckOpcode " << Opcode.getEnumName() << '\n';
177 void SwitchOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
178 OS.indent(indent) << "SwitchOpcode: {\n";
179 for (const auto &C : Cases) {
180 OS.indent(indent) << "case " << C.first->getEnumName() << ":\n";
181 C.second->print(OS, indent+2);
183 OS.indent(indent) << "}\n";
187 void CheckTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
188 OS.indent(indent) << "CheckType " << getEnumName(Type) << ", ResNo="
189 << ResNo << '\n';
192 void SwitchTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
193 OS.indent(indent) << "SwitchType: {\n";
194 for (const auto &C : Cases) {
195 OS.indent(indent) << "case " << getEnumName(C.first) << ":\n";
196 C.second->print(OS, indent+2);
198 OS.indent(indent) << "}\n";
201 void CheckChildTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
202 OS.indent(indent) << "CheckChildType " << ChildNo << " "
203 << getEnumName(Type) << '\n';
207 void CheckIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
208 OS.indent(indent) << "CheckInteger " << Value << '\n';
211 void CheckChildIntegerMatcher::printImpl(raw_ostream &OS,
212 unsigned indent) const {
213 OS.indent(indent) << "CheckChildInteger " << ChildNo << " " << Value << '\n';
216 void CheckCondCodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
217 OS.indent(indent) << "CheckCondCode ISD::" << CondCodeName << '\n';
220 void CheckChild2CondCodeMatcher::printImpl(raw_ostream &OS,
221 unsigned indent) const {
222 OS.indent(indent) << "CheckChild2CondCode ISD::" << CondCodeName << '\n';
225 void CheckValueTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
226 OS.indent(indent) << "CheckValueType MVT::" << TypeName << '\n';
229 void CheckComplexPatMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
230 OS.indent(indent) << "CheckComplexPat " << Pattern.getSelectFunc() << '\n';
233 void CheckAndImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
234 OS.indent(indent) << "CheckAndImm " << Value << '\n';
237 void CheckOrImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
238 OS.indent(indent) << "CheckOrImm " << Value << '\n';
241 void CheckFoldableChainNodeMatcher::printImpl(raw_ostream &OS,
242 unsigned indent) const {
243 OS.indent(indent) << "CheckFoldableChainNode\n";
246 void CheckImmAllOnesVMatcher::printImpl(raw_ostream &OS,
247 unsigned indent) const {
248 OS.indent(indent) << "CheckAllOnesV\n";
251 void CheckImmAllZerosVMatcher::printImpl(raw_ostream &OS,
252 unsigned indent) const {
253 OS.indent(indent) << "CheckAllZerosV\n";
256 void EmitIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
257 OS.indent(indent) << "EmitInteger " << Val << " VT=" << getEnumName(VT)
258 << '\n';
261 void EmitStringIntegerMatcher::
262 printImpl(raw_ostream &OS, unsigned indent) const {
263 OS.indent(indent) << "EmitStringInteger " << Val << " VT=" << getEnumName(VT)
264 << '\n';
267 void EmitRegisterMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
268 OS.indent(indent) << "EmitRegister ";
269 if (Reg)
270 OS << Reg->getName();
271 else
272 OS << "zero_reg";
273 OS << " VT=" << getEnumName(VT) << '\n';
276 void EmitConvertToTargetMatcher::
277 printImpl(raw_ostream &OS, unsigned indent) const {
278 OS.indent(indent) << "EmitConvertToTarget " << Slot << '\n';
281 void EmitMergeInputChainsMatcher::
282 printImpl(raw_ostream &OS, unsigned indent) const {
283 OS.indent(indent) << "EmitMergeInputChains <todo: args>\n";
286 void EmitCopyToRegMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
287 OS.indent(indent) << "EmitCopyToReg <todo: args>\n";
290 void EmitNodeXFormMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
291 OS.indent(indent) << "EmitNodeXForm " << NodeXForm->getName()
292 << " Slot=" << Slot << '\n';
296 void EmitNodeMatcherCommon::printImpl(raw_ostream &OS, unsigned indent) const {
297 OS.indent(indent);
298 OS << (isa<MorphNodeToMatcher>(this) ? "MorphNodeTo: " : "EmitNode: ")
299 << CGI.Namespace << "::" << CGI.TheDef->getName() << ": <todo flags> ";
301 for (unsigned i = 0, e = VTs.size(); i != e; ++i)
302 OS << ' ' << getEnumName(VTs[i]);
303 OS << '(';
304 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
305 OS << Operands[i] << ' ';
306 OS << ")\n";
309 void CompleteMatchMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
310 OS.indent(indent) << "CompleteMatch <todo args>\n";
311 OS.indent(indent) << "Src = " << *Pattern.getSrcPattern() << "\n";
312 OS.indent(indent) << "Dst = " << *Pattern.getDstPattern() << "\n";
315 bool CheckOpcodeMatcher::isEqualImpl(const Matcher *M) const {
316 // Note: pointer equality isn't enough here, we have to check the enum names
317 // to ensure that the nodes are for the same opcode.
318 return cast<CheckOpcodeMatcher>(M)->Opcode.getEnumName() ==
319 Opcode.getEnumName();
322 bool EmitNodeMatcherCommon::isEqualImpl(const Matcher *m) const {
323 const EmitNodeMatcherCommon *M = cast<EmitNodeMatcherCommon>(m);
324 return &M->CGI == &CGI && M->VTs == VTs && M->Operands == Operands &&
325 M->HasChain == HasChain && M->HasInGlue == HasInGlue &&
326 M->HasOutGlue == HasOutGlue && M->HasMemRefs == HasMemRefs &&
327 M->NumFixedArityOperands == NumFixedArityOperands;
330 void EmitNodeMatcher::anchor() { }
332 void MorphNodeToMatcher::anchor() { }
334 // isContradictoryImpl Implementations.
336 static bool TypesAreContradictory(MVT::SimpleValueType T1,
337 MVT::SimpleValueType T2) {
338 // If the two types are the same, then they are the same, so they don't
339 // contradict.
340 if (T1 == T2) return false;
342 // If either type is about iPtr, then they don't conflict unless the other
343 // one is not a scalar integer type.
344 if (T1 == MVT::iPTR)
345 return !MVT(T2).isInteger() || MVT(T2).isVector();
347 if (T2 == MVT::iPTR)
348 return !MVT(T1).isInteger() || MVT(T1).isVector();
350 // Otherwise, they are two different non-iPTR types, they conflict.
351 return true;
354 bool CheckOpcodeMatcher::isContradictoryImpl(const Matcher *M) const {
355 if (const CheckOpcodeMatcher *COM = dyn_cast<CheckOpcodeMatcher>(M)) {
356 // One node can't have two different opcodes!
357 // Note: pointer equality isn't enough here, we have to check the enum names
358 // to ensure that the nodes are for the same opcode.
359 return COM->getOpcode().getEnumName() != getOpcode().getEnumName();
362 // If the node has a known type, and if the type we're checking for is
363 // different, then we know they contradict. For example, a check for
364 // ISD::STORE will never be true at the same time a check for Type i32 is.
365 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M)) {
366 // If checking for a result the opcode doesn't have, it can't match.
367 if (CT->getResNo() >= getOpcode().getNumResults())
368 return true;
370 MVT::SimpleValueType NodeType = getOpcode().getKnownType(CT->getResNo());
371 if (NodeType != MVT::Other)
372 return TypesAreContradictory(NodeType, CT->getType());
375 return false;
378 bool CheckTypeMatcher::isContradictoryImpl(const Matcher *M) const {
379 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M))
380 return TypesAreContradictory(getType(), CT->getType());
381 return false;
384 bool CheckChildTypeMatcher::isContradictoryImpl(const Matcher *M) const {
385 if (const CheckChildTypeMatcher *CC = dyn_cast<CheckChildTypeMatcher>(M)) {
386 // If the two checks are about different nodes, we don't know if they
387 // conflict!
388 if (CC->getChildNo() != getChildNo())
389 return false;
391 return TypesAreContradictory(getType(), CC->getType());
393 return false;
396 bool CheckIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
397 if (const CheckIntegerMatcher *CIM = dyn_cast<CheckIntegerMatcher>(M))
398 return CIM->getValue() != getValue();
399 return false;
402 bool CheckChildIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
403 if (const CheckChildIntegerMatcher *CCIM = dyn_cast<CheckChildIntegerMatcher>(M)) {
404 // If the two checks are about different nodes, we don't know if they
405 // conflict!
406 if (CCIM->getChildNo() != getChildNo())
407 return false;
409 return CCIM->getValue() != getValue();
411 return false;
414 bool CheckValueTypeMatcher::isContradictoryImpl(const Matcher *M) const {
415 if (const CheckValueTypeMatcher *CVT = dyn_cast<CheckValueTypeMatcher>(M))
416 return CVT->getTypeName() != getTypeName();
417 return false;
420 bool CheckImmAllOnesVMatcher::isContradictoryImpl(const Matcher *M) const {
421 // AllZeros is contradictory.
422 return isa<CheckImmAllZerosVMatcher>(M);
425 bool CheckImmAllZerosVMatcher::isContradictoryImpl(const Matcher *M) const {
426 // AllOnes is contradictory.
427 return isa<CheckImmAllOnesVMatcher>(M);
430 bool CheckCondCodeMatcher::isContradictoryImpl(const Matcher *M) const {
431 if (const auto *CCCM = dyn_cast<CheckCondCodeMatcher>(M))
432 return CCCM->getCondCodeName() != getCondCodeName();
433 return false;
436 bool CheckChild2CondCodeMatcher::isContradictoryImpl(const Matcher *M) const {
437 if (const auto *CCCCM = dyn_cast<CheckChild2CondCodeMatcher>(M))
438 return CCCCM->getCondCodeName() != getCondCodeName();
439 return false;