1 //===- DAGISelMatcher.cpp - Representation of DAG pattern matcher ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "DAGISelMatcher.h"
11 #include "CodeGenDAGPatterns.h"
12 #include "CodeGenTarget.h"
13 #include "llvm/Support/raw_ostream.h"
14 #include "llvm/TableGen/Record.h"
17 void Matcher::anchor() { }
19 void Matcher::dump() const {
23 void Matcher::print(raw_ostream &OS, unsigned indent) const {
24 printImpl(OS, indent);
26 return Next->print(OS, indent);
29 void Matcher::printOne(raw_ostream &OS) const {
33 /// unlinkNode - Unlink the specified node from this chain. If Other == this,
34 /// we unlink the next pointer and return it. Otherwise we unlink Other from
35 /// the list and return this.
36 Matcher *Matcher::unlinkNode(Matcher *Other) {
40 // Scan until we find the predecessor of Other.
42 for (; Cur && Cur->getNext() != Other; Cur = Cur->getNext())
45 if (!Cur) return nullptr;
47 Cur->setNext(Other->takeNext());
51 /// canMoveBefore - Return true if this matcher is the same as Other, or if
52 /// we can move this matcher past all of the nodes in-between Other and this
53 /// node. Other must be equal to or before this.
54 bool Matcher::canMoveBefore(const Matcher *Other) const {
55 for (;; Other = Other->getNext()) {
56 assert(Other && "Other didn't come before 'this'?");
57 if (this == Other) return true;
59 // We have to be able to move this node across the Other node.
60 if (!canMoveBeforeNode(Other))
65 /// canMoveBeforeNode - Return true if it is safe to move the current matcher
66 /// across the specified one.
67 bool Matcher::canMoveBeforeNode(const Matcher *Other) const {
68 // We can move simple predicates before record nodes.
69 if (isSimplePredicateNode())
70 return Other->isSimplePredicateOrRecordNode();
72 // We can move record nodes across simple predicates.
73 if (isSimplePredicateOrRecordNode())
74 return isSimplePredicateNode();
76 // We can't move record nodes across each other etc.
81 ScopeMatcher::~ScopeMatcher() {
82 for (Matcher *C : Children)
86 SwitchOpcodeMatcher::~SwitchOpcodeMatcher() {
91 SwitchTypeMatcher::~SwitchTypeMatcher() {
96 CheckPredicateMatcher::CheckPredicateMatcher(
97 const TreePredicateFn &pred, const SmallVectorImpl<unsigned> &Ops)
98 : Matcher(CheckPredicate), Pred(pred.getOrigPatFragRecord()),
99 Operands(Ops.begin(), Ops.end()) {}
101 TreePredicateFn CheckPredicateMatcher::getPredicate() const {
102 return TreePredicateFn(Pred);
105 unsigned CheckPredicateMatcher::getNumOperands() const {
106 return Operands.size();
109 unsigned CheckPredicateMatcher::getOperandNo(unsigned i) const {
110 assert(i < Operands.size());
115 // printImpl methods.
117 void ScopeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
118 OS.indent(indent) << "Scope\n";
119 for (const Matcher *C : Children) {
121 OS.indent(indent+1) << "NULL POINTER\n";
123 C->print(OS, indent+2);
127 void RecordMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
128 OS.indent(indent) << "Record\n";
131 void RecordChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
132 OS.indent(indent) << "RecordChild: " << ChildNo << '\n';
135 void RecordMemRefMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
136 OS.indent(indent) << "RecordMemRef\n";
139 void CaptureGlueInputMatcher::printImpl(raw_ostream &OS, unsigned indent) const{
140 OS.indent(indent) << "CaptureGlueInput\n";
143 void MoveChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
144 OS.indent(indent) << "MoveChild " << ChildNo << '\n';
147 void MoveParentMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
148 OS.indent(indent) << "MoveParent\n";
151 void CheckSameMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
152 OS.indent(indent) << "CheckSame " << MatchNumber << '\n';
155 void CheckChildSameMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
156 OS.indent(indent) << "CheckChild" << ChildNo << "Same\n";
159 void CheckPatternPredicateMatcher::
160 printImpl(raw_ostream &OS, unsigned indent) const {
161 OS.indent(indent) << "CheckPatternPredicate " << Predicate << '\n';
164 void CheckPredicateMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
165 OS.indent(indent) << "CheckPredicate " << getPredicate().getFnName() << '\n';
168 void CheckOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
169 OS.indent(indent) << "CheckOpcode " << Opcode.getEnumName() << '\n';
172 void SwitchOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
173 OS.indent(indent) << "SwitchOpcode: {\n";
174 for (const auto &C : Cases) {
175 OS.indent(indent) << "case " << C.first->getEnumName() << ":\n";
176 C.second->print(OS, indent+2);
178 OS.indent(indent) << "}\n";
182 void CheckTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
183 OS.indent(indent) << "CheckType " << getEnumName(Type) << ", ResNo="
187 void SwitchTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
188 OS.indent(indent) << "SwitchType: {\n";
189 for (const auto &C : Cases) {
190 OS.indent(indent) << "case " << getEnumName(C.first) << ":\n";
191 C.second->print(OS, indent+2);
193 OS.indent(indent) << "}\n";
196 void CheckChildTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
197 OS.indent(indent) << "CheckChildType " << ChildNo << " "
198 << getEnumName(Type) << '\n';
202 void CheckIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
203 OS.indent(indent) << "CheckInteger " << Value << '\n';
206 void CheckChildIntegerMatcher::printImpl(raw_ostream &OS,
207 unsigned indent) const {
208 OS.indent(indent) << "CheckChildInteger " << ChildNo << " " << Value << '\n';
211 void CheckCondCodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
212 OS.indent(indent) << "CheckCondCode ISD::" << CondCodeName << '\n';
215 void CheckValueTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
216 OS.indent(indent) << "CheckValueType MVT::" << TypeName << '\n';
219 void CheckComplexPatMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
220 OS.indent(indent) << "CheckComplexPat " << Pattern.getSelectFunc() << '\n';
223 void CheckAndImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
224 OS.indent(indent) << "CheckAndImm " << Value << '\n';
227 void CheckOrImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
228 OS.indent(indent) << "CheckOrImm " << Value << '\n';
231 void CheckFoldableChainNodeMatcher::printImpl(raw_ostream &OS,
232 unsigned indent) const {
233 OS.indent(indent) << "CheckFoldableChainNode\n";
236 void EmitIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
237 OS.indent(indent) << "EmitInteger " << Val << " VT=" << getEnumName(VT)
241 void EmitStringIntegerMatcher::
242 printImpl(raw_ostream &OS, unsigned indent) const {
243 OS.indent(indent) << "EmitStringInteger " << Val << " VT=" << getEnumName(VT)
247 void EmitRegisterMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
248 OS.indent(indent) << "EmitRegister ";
250 OS << Reg->getName();
253 OS << " VT=" << getEnumName(VT) << '\n';
256 void EmitConvertToTargetMatcher::
257 printImpl(raw_ostream &OS, unsigned indent) const {
258 OS.indent(indent) << "EmitConvertToTarget " << Slot << '\n';
261 void EmitMergeInputChainsMatcher::
262 printImpl(raw_ostream &OS, unsigned indent) const {
263 OS.indent(indent) << "EmitMergeInputChains <todo: args>\n";
266 void EmitCopyToRegMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
267 OS.indent(indent) << "EmitCopyToReg <todo: args>\n";
270 void EmitNodeXFormMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
271 OS.indent(indent) << "EmitNodeXForm " << NodeXForm->getName()
272 << " Slot=" << Slot << '\n';
276 void EmitNodeMatcherCommon::printImpl(raw_ostream &OS, unsigned indent) const {
278 OS << (isa<MorphNodeToMatcher>(this) ? "MorphNodeTo: " : "EmitNode: ")
279 << OpcodeName << ": <todo flags> ";
281 for (unsigned i = 0, e = VTs.size(); i != e; ++i)
282 OS << ' ' << getEnumName(VTs[i]);
284 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
285 OS << Operands[i] << ' ';
289 void CompleteMatchMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
290 OS.indent(indent) << "CompleteMatch <todo args>\n";
291 OS.indent(indent) << "Src = " << *Pattern.getSrcPattern() << "\n";
292 OS.indent(indent) << "Dst = " << *Pattern.getDstPattern() << "\n";
295 bool CheckOpcodeMatcher::isEqualImpl(const Matcher *M) const {
296 // Note: pointer equality isn't enough here, we have to check the enum names
297 // to ensure that the nodes are for the same opcode.
298 return cast<CheckOpcodeMatcher>(M)->Opcode.getEnumName() ==
299 Opcode.getEnumName();
302 bool EmitNodeMatcherCommon::isEqualImpl(const Matcher *m) const {
303 const EmitNodeMatcherCommon *M = cast<EmitNodeMatcherCommon>(m);
304 return M->OpcodeName == OpcodeName && M->VTs == VTs &&
305 M->Operands == Operands && M->HasChain == HasChain &&
306 M->HasInGlue == HasInGlue && M->HasOutGlue == HasOutGlue &&
307 M->HasMemRefs == HasMemRefs &&
308 M->NumFixedArityOperands == NumFixedArityOperands;
311 void EmitNodeMatcher::anchor() { }
313 void MorphNodeToMatcher::anchor() { }
315 // isContradictoryImpl Implementations.
317 static bool TypesAreContradictory(MVT::SimpleValueType T1,
318 MVT::SimpleValueType T2) {
319 // If the two types are the same, then they are the same, so they don't
321 if (T1 == T2) return false;
323 // If either type is about iPtr, then they don't conflict unless the other
324 // one is not a scalar integer type.
326 return !MVT(T2).isInteger() || MVT(T2).isVector();
329 return !MVT(T1).isInteger() || MVT(T1).isVector();
331 // Otherwise, they are two different non-iPTR types, they conflict.
335 bool CheckOpcodeMatcher::isContradictoryImpl(const Matcher *M) const {
336 if (const CheckOpcodeMatcher *COM = dyn_cast<CheckOpcodeMatcher>(M)) {
337 // One node can't have two different opcodes!
338 // Note: pointer equality isn't enough here, we have to check the enum names
339 // to ensure that the nodes are for the same opcode.
340 return COM->getOpcode().getEnumName() != getOpcode().getEnumName();
343 // If the node has a known type, and if the type we're checking for is
344 // different, then we know they contradict. For example, a check for
345 // ISD::STORE will never be true at the same time a check for Type i32 is.
346 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M)) {
347 // If checking for a result the opcode doesn't have, it can't match.
348 if (CT->getResNo() >= getOpcode().getNumResults())
351 MVT::SimpleValueType NodeType = getOpcode().getKnownType(CT->getResNo());
352 if (NodeType != MVT::Other)
353 return TypesAreContradictory(NodeType, CT->getType());
359 bool CheckTypeMatcher::isContradictoryImpl(const Matcher *M) const {
360 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M))
361 return TypesAreContradictory(getType(), CT->getType());
365 bool CheckChildTypeMatcher::isContradictoryImpl(const Matcher *M) const {
366 if (const CheckChildTypeMatcher *CC = dyn_cast<CheckChildTypeMatcher>(M)) {
367 // If the two checks are about different nodes, we don't know if they
369 if (CC->getChildNo() != getChildNo())
372 return TypesAreContradictory(getType(), CC->getType());
377 bool CheckIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
378 if (const CheckIntegerMatcher *CIM = dyn_cast<CheckIntegerMatcher>(M))
379 return CIM->getValue() != getValue();
383 bool CheckChildIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
384 if (const CheckChildIntegerMatcher *CCIM = dyn_cast<CheckChildIntegerMatcher>(M)) {
385 // If the two checks are about different nodes, we don't know if they
387 if (CCIM->getChildNo() != getChildNo())
390 return CCIM->getValue() != getValue();
395 bool CheckValueTypeMatcher::isContradictoryImpl(const Matcher *M) const {
396 if (const CheckValueTypeMatcher *CVT = dyn_cast<CheckValueTypeMatcher>(M))
397 return CVT->getTypeName() != getTypeName();