[llvm] [cmake] Add possibility to use ChooseMSVCCRT.cmake when include LLVM library
[llvm-core.git] / utils / TableGen / DAGISelMatcher.cpp
blobbebd205ad58ff631823ee9700ba56c0e56998000
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 CheckChild2CondCodeMatcher::printImpl(raw_ostream &OS,
215 unsigned indent) const {
216 OS.indent(indent) << "CheckChild2CondCode ISD::" << CondCodeName << '\n';
219 void CheckValueTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
220 OS.indent(indent) << "CheckValueType MVT::" << TypeName << '\n';
223 void CheckComplexPatMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
224 OS.indent(indent) << "CheckComplexPat " << Pattern.getSelectFunc() << '\n';
227 void CheckAndImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
228 OS.indent(indent) << "CheckAndImm " << Value << '\n';
231 void CheckOrImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
232 OS.indent(indent) << "CheckOrImm " << Value << '\n';
235 void CheckFoldableChainNodeMatcher::printImpl(raw_ostream &OS,
236 unsigned indent) const {
237 OS.indent(indent) << "CheckFoldableChainNode\n";
240 void CheckImmAllOnesVMatcher::printImpl(raw_ostream &OS,
241 unsigned indent) const {
242 OS.indent(indent) << "CheckAllOnesV\n";
245 void CheckImmAllZerosVMatcher::printImpl(raw_ostream &OS,
246 unsigned indent) const {
247 OS.indent(indent) << "CheckAllZerosV\n";
250 void EmitIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
251 OS.indent(indent) << "EmitInteger " << Val << " VT=" << getEnumName(VT)
252 << '\n';
255 void EmitStringIntegerMatcher::
256 printImpl(raw_ostream &OS, unsigned indent) const {
257 OS.indent(indent) << "EmitStringInteger " << Val << " VT=" << getEnumName(VT)
258 << '\n';
261 void EmitRegisterMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
262 OS.indent(indent) << "EmitRegister ";
263 if (Reg)
264 OS << Reg->getName();
265 else
266 OS << "zero_reg";
267 OS << " VT=" << getEnumName(VT) << '\n';
270 void EmitConvertToTargetMatcher::
271 printImpl(raw_ostream &OS, unsigned indent) const {
272 OS.indent(indent) << "EmitConvertToTarget " << Slot << '\n';
275 void EmitMergeInputChainsMatcher::
276 printImpl(raw_ostream &OS, unsigned indent) const {
277 OS.indent(indent) << "EmitMergeInputChains <todo: args>\n";
280 void EmitCopyToRegMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
281 OS.indent(indent) << "EmitCopyToReg <todo: args>\n";
284 void EmitNodeXFormMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
285 OS.indent(indent) << "EmitNodeXForm " << NodeXForm->getName()
286 << " Slot=" << Slot << '\n';
290 void EmitNodeMatcherCommon::printImpl(raw_ostream &OS, unsigned indent) const {
291 OS.indent(indent);
292 OS << (isa<MorphNodeToMatcher>(this) ? "MorphNodeTo: " : "EmitNode: ")
293 << OpcodeName << ": <todo flags> ";
295 for (unsigned i = 0, e = VTs.size(); i != e; ++i)
296 OS << ' ' << getEnumName(VTs[i]);
297 OS << '(';
298 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
299 OS << Operands[i] << ' ';
300 OS << ")\n";
303 void CompleteMatchMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
304 OS.indent(indent) << "CompleteMatch <todo args>\n";
305 OS.indent(indent) << "Src = " << *Pattern.getSrcPattern() << "\n";
306 OS.indent(indent) << "Dst = " << *Pattern.getDstPattern() << "\n";
309 bool CheckOpcodeMatcher::isEqualImpl(const Matcher *M) const {
310 // Note: pointer equality isn't enough here, we have to check the enum names
311 // to ensure that the nodes are for the same opcode.
312 return cast<CheckOpcodeMatcher>(M)->Opcode.getEnumName() ==
313 Opcode.getEnumName();
316 bool EmitNodeMatcherCommon::isEqualImpl(const Matcher *m) const {
317 const EmitNodeMatcherCommon *M = cast<EmitNodeMatcherCommon>(m);
318 return M->OpcodeName == OpcodeName && M->VTs == VTs &&
319 M->Operands == Operands && M->HasChain == HasChain &&
320 M->HasInGlue == HasInGlue && M->HasOutGlue == HasOutGlue &&
321 M->HasMemRefs == HasMemRefs &&
322 M->NumFixedArityOperands == NumFixedArityOperands;
325 void EmitNodeMatcher::anchor() { }
327 void MorphNodeToMatcher::anchor() { }
329 // isContradictoryImpl Implementations.
331 static bool TypesAreContradictory(MVT::SimpleValueType T1,
332 MVT::SimpleValueType T2) {
333 // If the two types are the same, then they are the same, so they don't
334 // contradict.
335 if (T1 == T2) return false;
337 // If either type is about iPtr, then they don't conflict unless the other
338 // one is not a scalar integer type.
339 if (T1 == MVT::iPTR)
340 return !MVT(T2).isInteger() || MVT(T2).isVector();
342 if (T2 == MVT::iPTR)
343 return !MVT(T1).isInteger() || MVT(T1).isVector();
345 // Otherwise, they are two different non-iPTR types, they conflict.
346 return true;
349 bool CheckOpcodeMatcher::isContradictoryImpl(const Matcher *M) const {
350 if (const CheckOpcodeMatcher *COM = dyn_cast<CheckOpcodeMatcher>(M)) {
351 // One node can't have two different opcodes!
352 // Note: pointer equality isn't enough here, we have to check the enum names
353 // to ensure that the nodes are for the same opcode.
354 return COM->getOpcode().getEnumName() != getOpcode().getEnumName();
357 // If the node has a known type, and if the type we're checking for is
358 // different, then we know they contradict. For example, a check for
359 // ISD::STORE will never be true at the same time a check for Type i32 is.
360 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M)) {
361 // If checking for a result the opcode doesn't have, it can't match.
362 if (CT->getResNo() >= getOpcode().getNumResults())
363 return true;
365 MVT::SimpleValueType NodeType = getOpcode().getKnownType(CT->getResNo());
366 if (NodeType != MVT::Other)
367 return TypesAreContradictory(NodeType, CT->getType());
370 return false;
373 bool CheckTypeMatcher::isContradictoryImpl(const Matcher *M) const {
374 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M))
375 return TypesAreContradictory(getType(), CT->getType());
376 return false;
379 bool CheckChildTypeMatcher::isContradictoryImpl(const Matcher *M) const {
380 if (const CheckChildTypeMatcher *CC = dyn_cast<CheckChildTypeMatcher>(M)) {
381 // If the two checks are about different nodes, we don't know if they
382 // conflict!
383 if (CC->getChildNo() != getChildNo())
384 return false;
386 return TypesAreContradictory(getType(), CC->getType());
388 return false;
391 bool CheckIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
392 if (const CheckIntegerMatcher *CIM = dyn_cast<CheckIntegerMatcher>(M))
393 return CIM->getValue() != getValue();
394 return false;
397 bool CheckChildIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
398 if (const CheckChildIntegerMatcher *CCIM = dyn_cast<CheckChildIntegerMatcher>(M)) {
399 // If the two checks are about different nodes, we don't know if they
400 // conflict!
401 if (CCIM->getChildNo() != getChildNo())
402 return false;
404 return CCIM->getValue() != getValue();
406 return false;
409 bool CheckValueTypeMatcher::isContradictoryImpl(const Matcher *M) const {
410 if (const CheckValueTypeMatcher *CVT = dyn_cast<CheckValueTypeMatcher>(M))
411 return CVT->getTypeName() != getTypeName();
412 return false;
415 bool CheckImmAllOnesVMatcher::isContradictoryImpl(const Matcher *M) const {
416 // AllZeros is contradictory.
417 return isa<CheckImmAllZerosVMatcher>(M);
420 bool CheckImmAllZerosVMatcher::isContradictoryImpl(const Matcher *M) const {
421 // AllOnes is contradictory.
422 return isa<CheckImmAllOnesVMatcher>(M);