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"
14 #include "llvm/Support/raw_ostream.h"
15 #include "llvm/ADT/StringExtras.h"
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
== 0) return 0;
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 /// canMoveBefore - 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 (unsigned i
= 0, e
= Children
.size(); i
!= e
; ++i
)
86 CheckPredicateMatcher::CheckPredicateMatcher(const TreePredicateFn
&pred
)
87 : Matcher(CheckPredicate
), Pred(pred
.getOrigPatFragRecord()) {}
89 TreePredicateFn
CheckPredicateMatcher::getPredicate() const {
90 return TreePredicateFn(Pred
);
97 void ScopeMatcher::printImpl(raw_ostream
&OS
, unsigned indent
) const {
98 OS
.indent(indent
) << "Scope\n";
99 for (unsigned i
= 0, e
= getNumChildren(); i
!= e
; ++i
) {
100 if (getChild(i
) == 0)
101 OS
.indent(indent
+1) << "NULL POINTER\n";
103 getChild(i
)->print(OS
, indent
+2);
107 void RecordMatcher::printImpl(raw_ostream
&OS
, unsigned indent
) const {
108 OS
.indent(indent
) << "Record\n";
111 void RecordChildMatcher::printImpl(raw_ostream
&OS
, unsigned indent
) const {
112 OS
.indent(indent
) << "RecordChild: " << ChildNo
<< '\n';
115 void RecordMemRefMatcher::printImpl(raw_ostream
&OS
, unsigned indent
) const {
116 OS
.indent(indent
) << "RecordMemRef\n";
119 void CaptureGlueInputMatcher::printImpl(raw_ostream
&OS
, unsigned indent
) const{
120 OS
.indent(indent
) << "CaptureGlueInput\n";
123 void MoveChildMatcher::printImpl(raw_ostream
&OS
, unsigned indent
) const {
124 OS
.indent(indent
) << "MoveChild " << ChildNo
<< '\n';
127 void MoveParentMatcher::printImpl(raw_ostream
&OS
, unsigned indent
) const {
128 OS
.indent(indent
) << "MoveParent\n";
131 void CheckSameMatcher::printImpl(raw_ostream
&OS
, unsigned indent
) const {
132 OS
.indent(indent
) << "CheckSame " << MatchNumber
<< '\n';
135 void CheckPatternPredicateMatcher::
136 printImpl(raw_ostream
&OS
, unsigned indent
) const {
137 OS
.indent(indent
) << "CheckPatternPredicate " << Predicate
<< '\n';
140 void CheckPredicateMatcher::printImpl(raw_ostream
&OS
, unsigned indent
) const {
141 OS
.indent(indent
) << "CheckPredicate " << getPredicate().getFnName() << '\n';
144 void CheckOpcodeMatcher::printImpl(raw_ostream
&OS
, unsigned indent
) const {
145 OS
.indent(indent
) << "CheckOpcode " << Opcode
.getEnumName() << '\n';
148 void SwitchOpcodeMatcher::printImpl(raw_ostream
&OS
, unsigned indent
) const {
149 OS
.indent(indent
) << "SwitchOpcode: {\n";
150 for (unsigned i
= 0, e
= Cases
.size(); i
!= e
; ++i
) {
151 OS
.indent(indent
) << "case " << Cases
[i
].first
->getEnumName() << ":\n";
152 Cases
[i
].second
->print(OS
, indent
+2);
154 OS
.indent(indent
) << "}\n";
158 void CheckTypeMatcher::printImpl(raw_ostream
&OS
, unsigned indent
) const {
159 OS
.indent(indent
) << "CheckType " << getEnumName(Type
) << ", ResNo="
163 void SwitchTypeMatcher::printImpl(raw_ostream
&OS
, unsigned indent
) const {
164 OS
.indent(indent
) << "SwitchType: {\n";
165 for (unsigned i
= 0, e
= Cases
.size(); i
!= e
; ++i
) {
166 OS
.indent(indent
) << "case " << getEnumName(Cases
[i
].first
) << ":\n";
167 Cases
[i
].second
->print(OS
, indent
+2);
169 OS
.indent(indent
) << "}\n";
172 void CheckChildTypeMatcher::printImpl(raw_ostream
&OS
, unsigned indent
) const {
173 OS
.indent(indent
) << "CheckChildType " << ChildNo
<< " "
174 << getEnumName(Type
) << '\n';
178 void CheckIntegerMatcher::printImpl(raw_ostream
&OS
, unsigned indent
) const {
179 OS
.indent(indent
) << "CheckInteger " << Value
<< '\n';
182 void CheckCondCodeMatcher::printImpl(raw_ostream
&OS
, unsigned indent
) const {
183 OS
.indent(indent
) << "CheckCondCode ISD::" << CondCodeName
<< '\n';
186 void CheckValueTypeMatcher::printImpl(raw_ostream
&OS
, unsigned indent
) const {
187 OS
.indent(indent
) << "CheckValueType MVT::" << TypeName
<< '\n';
190 void CheckComplexPatMatcher::printImpl(raw_ostream
&OS
, unsigned indent
) const {
191 OS
.indent(indent
) << "CheckComplexPat " << Pattern
.getSelectFunc() << '\n';
194 void CheckAndImmMatcher::printImpl(raw_ostream
&OS
, unsigned indent
) const {
195 OS
.indent(indent
) << "CheckAndImm " << Value
<< '\n';
198 void CheckOrImmMatcher::printImpl(raw_ostream
&OS
, unsigned indent
) const {
199 OS
.indent(indent
) << "CheckOrImm " << Value
<< '\n';
202 void CheckFoldableChainNodeMatcher::printImpl(raw_ostream
&OS
,
203 unsigned indent
) const {
204 OS
.indent(indent
) << "CheckFoldableChainNode\n";
207 void EmitIntegerMatcher::printImpl(raw_ostream
&OS
, unsigned indent
) const {
208 OS
.indent(indent
) << "EmitInteger " << Val
<< " VT=" << VT
<< '\n';
211 void EmitStringIntegerMatcher::
212 printImpl(raw_ostream
&OS
, unsigned indent
) const {
213 OS
.indent(indent
) << "EmitStringInteger " << Val
<< " VT=" << VT
<< '\n';
216 void EmitRegisterMatcher::printImpl(raw_ostream
&OS
, unsigned indent
) const {
217 OS
.indent(indent
) << "EmitRegister ";
219 OS
<< Reg
->getName();
222 OS
<< " VT=" << VT
<< '\n';
225 void EmitConvertToTargetMatcher::
226 printImpl(raw_ostream
&OS
, unsigned indent
) const {
227 OS
.indent(indent
) << "EmitConvertToTarget " << Slot
<< '\n';
230 void EmitMergeInputChainsMatcher::
231 printImpl(raw_ostream
&OS
, unsigned indent
) const {
232 OS
.indent(indent
) << "EmitMergeInputChains <todo: args>\n";
235 void EmitCopyToRegMatcher::printImpl(raw_ostream
&OS
, unsigned indent
) const {
236 OS
.indent(indent
) << "EmitCopyToReg <todo: args>\n";
239 void EmitNodeXFormMatcher::printImpl(raw_ostream
&OS
, unsigned indent
) const {
240 OS
.indent(indent
) << "EmitNodeXForm " << NodeXForm
->getName()
241 << " Slot=" << Slot
<< '\n';
245 void EmitNodeMatcherCommon::printImpl(raw_ostream
&OS
, unsigned indent
) const {
247 OS
<< (isa
<MorphNodeToMatcher
>(this) ? "MorphNodeTo: " : "EmitNode: ")
248 << OpcodeName
<< ": <todo flags> ";
250 for (unsigned i
= 0, e
= VTs
.size(); i
!= e
; ++i
)
251 OS
<< ' ' << getEnumName(VTs
[i
]);
253 for (unsigned i
= 0, e
= Operands
.size(); i
!= e
; ++i
)
254 OS
<< Operands
[i
] << ' ';
258 void MarkGlueResultsMatcher::printImpl(raw_ostream
&OS
, unsigned indent
) const {
259 OS
.indent(indent
) << "MarkGlueResults <todo: args>\n";
262 void CompleteMatchMatcher::printImpl(raw_ostream
&OS
, unsigned indent
) const {
263 OS
.indent(indent
) << "CompleteMatch <todo args>\n";
264 OS
.indent(indent
) << "Src = " << *Pattern
.getSrcPattern() << "\n";
265 OS
.indent(indent
) << "Dst = " << *Pattern
.getDstPattern() << "\n";
268 // getHashImpl Implementation.
270 unsigned CheckPatternPredicateMatcher::getHashImpl() const {
271 return HashString(Predicate
);
274 unsigned CheckPredicateMatcher::getHashImpl() const {
275 return HashString(getPredicate().getFnName());
278 unsigned CheckOpcodeMatcher::getHashImpl() const {
279 return HashString(Opcode
.getEnumName());
282 unsigned CheckCondCodeMatcher::getHashImpl() const {
283 return HashString(CondCodeName
);
286 unsigned CheckValueTypeMatcher::getHashImpl() const {
287 return HashString(TypeName
);
290 unsigned EmitStringIntegerMatcher::getHashImpl() const {
291 return HashString(Val
) ^ VT
;
294 template<typename It
>
295 static unsigned HashUnsigneds(It I
, It E
) {
298 Result
= (Result
<<3) ^ *I
;
302 unsigned EmitMergeInputChainsMatcher::getHashImpl() const {
303 return HashUnsigneds(ChainNodes
.begin(), ChainNodes
.end());
306 bool CheckOpcodeMatcher::isEqualImpl(const Matcher
*M
) const {
307 // Note: pointer equality isn't enough here, we have to check the enum names
308 // to ensure that the nodes are for the same opcode.
309 return cast
<CheckOpcodeMatcher
>(M
)->Opcode
.getEnumName() ==
310 Opcode
.getEnumName();
313 bool EmitNodeMatcherCommon::isEqualImpl(const Matcher
*m
) const {
314 const EmitNodeMatcherCommon
*M
= cast
<EmitNodeMatcherCommon
>(m
);
315 return M
->OpcodeName
== OpcodeName
&& M
->VTs
== VTs
&&
316 M
->Operands
== Operands
&& M
->HasChain
== HasChain
&&
317 M
->HasInGlue
== HasInGlue
&& M
->HasOutGlue
== HasOutGlue
&&
318 M
->HasMemRefs
== HasMemRefs
&&
319 M
->NumFixedArityOperands
== NumFixedArityOperands
;
322 unsigned EmitNodeMatcherCommon::getHashImpl() const {
323 return (HashString(OpcodeName
) << 4) | Operands
.size();
327 unsigned MarkGlueResultsMatcher::getHashImpl() const {
328 return HashUnsigneds(GlueResultNodes
.begin(), GlueResultNodes
.end());
331 unsigned CompleteMatchMatcher::getHashImpl() const {
332 return HashUnsigneds(Results
.begin(), Results
.end()) ^
333 ((unsigned)(intptr_t)&Pattern
<< 8);
336 // isContradictoryImpl Implementations.
338 static bool TypesAreContradictory(MVT::SimpleValueType T1
,
339 MVT::SimpleValueType T2
) {
340 // If the two types are the same, then they are the same, so they don't
342 if (T1
== T2
) return false;
344 // If either type is about iPtr, then they don't conflict unless the other
345 // one is not a scalar integer type.
347 return !MVT(T2
).isInteger() || MVT(T2
).isVector();
350 return !MVT(T1
).isInteger() || MVT(T1
).isVector();
352 // Otherwise, they are two different non-iPTR types, they conflict.
356 bool CheckOpcodeMatcher::isContradictoryImpl(const Matcher
*M
) const {
357 if (const CheckOpcodeMatcher
*COM
= dyn_cast
<CheckOpcodeMatcher
>(M
)) {
358 // One node can't have two different opcodes!
359 // Note: pointer equality isn't enough here, we have to check the enum names
360 // to ensure that the nodes are for the same opcode.
361 return COM
->getOpcode().getEnumName() != getOpcode().getEnumName();
364 // If the node has a known type, and if the type we're checking for is
365 // different, then we know they contradict. For example, a check for
366 // ISD::STORE will never be true at the same time a check for Type i32 is.
367 if (const CheckTypeMatcher
*CT
= dyn_cast
<CheckTypeMatcher
>(M
)) {
368 // If checking for a result the opcode doesn't have, it can't match.
369 if (CT
->getResNo() >= getOpcode().getNumResults())
372 MVT::SimpleValueType NodeType
= getOpcode().getKnownType(CT
->getResNo());
373 if (NodeType
!= MVT::Other
)
374 return TypesAreContradictory(NodeType
, CT
->getType());
380 bool CheckTypeMatcher::isContradictoryImpl(const Matcher
*M
) const {
381 if (const CheckTypeMatcher
*CT
= dyn_cast
<CheckTypeMatcher
>(M
))
382 return TypesAreContradictory(getType(), CT
->getType());
386 bool CheckChildTypeMatcher::isContradictoryImpl(const Matcher
*M
) const {
387 if (const CheckChildTypeMatcher
*CC
= dyn_cast
<CheckChildTypeMatcher
>(M
)) {
388 // If the two checks are about different nodes, we don't know if they
390 if (CC
->getChildNo() != getChildNo())
393 return TypesAreContradictory(getType(), CC
->getType());
398 bool CheckIntegerMatcher::isContradictoryImpl(const Matcher
*M
) const {
399 if (const CheckIntegerMatcher
*CIM
= dyn_cast
<CheckIntegerMatcher
>(M
))
400 return CIM
->getValue() != getValue();
404 bool CheckValueTypeMatcher::isContradictoryImpl(const Matcher
*M
) const {
405 if (const CheckValueTypeMatcher
*CVT
= dyn_cast
<CheckValueTypeMatcher
>(M
))
406 return CVT
->getTypeName() != getTypeName();