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 ScopeMatcher::~ScopeMatcher() {
33 for (unsigned i = 0, e = Children.size(); i != e; ++i)
40 void ScopeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
41 OS.indent(indent) << "Scope\n";
42 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) {
44 OS.indent(indent+1) << "NULL POINTER\n";
46 getChild(i)->print(OS, indent+2);
50 void RecordMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
51 OS.indent(indent) << "Record\n";
54 void RecordChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
55 OS.indent(indent) << "RecordChild: " << ChildNo << '\n';
58 void RecordMemRefMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
59 OS.indent(indent) << "RecordMemRef\n";
62 void CaptureFlagInputMatcher::printImpl(raw_ostream &OS, unsigned indent) const{
63 OS.indent(indent) << "CaptureFlagInput\n";
66 void MoveChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
67 OS.indent(indent) << "MoveChild " << ChildNo << '\n';
70 void MoveParentMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
71 OS.indent(indent) << "MoveParent\n";
74 void CheckSameMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
75 OS.indent(indent) << "CheckSame " << MatchNumber << '\n';
78 void CheckPatternPredicateMatcher::
79 printImpl(raw_ostream &OS, unsigned indent) const {
80 OS.indent(indent) << "CheckPatternPredicate " << Predicate << '\n';
83 void CheckPredicateMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
84 OS.indent(indent) << "CheckPredicate " << PredName << '\n';
87 void CheckOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
88 OS.indent(indent) << "CheckOpcode " << Opcode.getEnumName() << '\n';
91 void CheckMultiOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const{
92 OS.indent(indent) << "CheckMultiOpcode <todo args>\n";
95 void CheckTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
96 OS.indent(indent) << "CheckType " << getEnumName(Type) << '\n';
99 void CheckChildTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
100 OS.indent(indent) << "CheckChildType " << ChildNo << " "
101 << getEnumName(Type) << '\n';
105 void CheckIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
106 OS.indent(indent) << "CheckInteger " << Value << '\n';
109 void CheckCondCodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
110 OS.indent(indent) << "CheckCondCode ISD::" << CondCodeName << '\n';
113 void CheckValueTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
114 OS.indent(indent) << "CheckValueType MVT::" << TypeName << '\n';
117 void CheckComplexPatMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
118 OS.indent(indent) << "CheckComplexPat " << Pattern.getSelectFunc() << '\n';
121 void CheckAndImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
122 OS.indent(indent) << "CheckAndImm " << Value << '\n';
125 void CheckOrImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
126 OS.indent(indent) << "CheckOrImm " << Value << '\n';
129 void CheckFoldableChainNodeMatcher::printImpl(raw_ostream &OS,
130 unsigned indent) const {
131 OS.indent(indent) << "CheckFoldableChainNode\n";
134 void CheckChainCompatibleMatcher::printImpl(raw_ostream &OS,
135 unsigned indent) const {
136 OS.indent(indent) << "CheckChainCompatible " << PreviousOp << "\n";
139 void EmitIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
140 OS.indent(indent) << "EmitInteger " << Val << " VT=" << VT << '\n';
143 void EmitStringIntegerMatcher::
144 printImpl(raw_ostream &OS, unsigned indent) const {
145 OS.indent(indent) << "EmitStringInteger " << Val << " VT=" << VT << '\n';
148 void EmitRegisterMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
149 OS.indent(indent) << "EmitRegister ";
151 OS << Reg->getName();
154 OS << " VT=" << VT << '\n';
157 void EmitConvertToTargetMatcher::
158 printImpl(raw_ostream &OS, unsigned indent) const {
159 OS.indent(indent) << "EmitConvertToTarget " << Slot << '\n';
162 void EmitMergeInputChainsMatcher::
163 printImpl(raw_ostream &OS, unsigned indent) const {
164 OS.indent(indent) << "EmitMergeInputChains <todo: args>\n";
167 void EmitCopyToRegMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
168 OS.indent(indent) << "EmitCopyToReg <todo: args>\n";
171 void EmitNodeXFormMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
172 OS.indent(indent) << "EmitNodeXForm " << NodeXForm->getName()
173 << " Slot=" << Slot << '\n';
177 void EmitNodeMatcherCommon::printImpl(raw_ostream &OS, unsigned indent) const {
179 OS << (isa<SelectNodeToMatcher>(this) ? "SelectNodeTo: " : "EmitNode: ")
180 << OpcodeName << ": <todo flags> ";
182 for (unsigned i = 0, e = VTs.size(); i != e; ++i)
183 OS << ' ' << getEnumName(VTs[i]);
185 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
186 OS << Operands[i] << ' ';
190 void MarkFlagResultsMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
191 OS.indent(indent) << "MarkFlagResults <todo: args>\n";
194 void CompleteMatchMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
195 OS.indent(indent) << "CompleteMatch <todo args>\n";
196 OS.indent(indent) << "Src = " << *Pattern.getSrcPattern() << "\n";
197 OS.indent(indent) << "Dst = " << *Pattern.getDstPattern() << "\n";
200 // getHashImpl Implementation.
202 unsigned CheckPatternPredicateMatcher::getHashImpl() const {
203 return HashString(Predicate);
206 unsigned CheckPredicateMatcher::getHashImpl() const {
207 return HashString(PredName);
210 unsigned CheckOpcodeMatcher::getHashImpl() const {
211 return HashString(Opcode.getEnumName());
214 unsigned CheckMultiOpcodeMatcher::getHashImpl() const {
216 for (unsigned i = 0, e = Opcodes.size(); i != e; ++i)
217 Result |= HashString(Opcodes[i]->getEnumName());
221 unsigned CheckCondCodeMatcher::getHashImpl() const {
222 return HashString(CondCodeName);
225 unsigned CheckValueTypeMatcher::getHashImpl() const {
226 return HashString(TypeName);
229 unsigned EmitStringIntegerMatcher::getHashImpl() const {
230 return HashString(Val) ^ VT;
233 template<typename It>
234 static unsigned HashUnsigneds(It I, It E) {
237 Result = (Result<<3) ^ *I;
241 unsigned EmitMergeInputChainsMatcher::getHashImpl() const {
242 return HashUnsigneds(ChainNodes.begin(), ChainNodes.end());
245 bool EmitNodeMatcherCommon::isEqualImpl(const Matcher *m) const {
246 const EmitNodeMatcherCommon *M = cast<EmitNodeMatcherCommon>(m);
247 return M->OpcodeName == OpcodeName && M->VTs == VTs &&
248 M->Operands == Operands && M->HasChain == HasChain &&
249 M->HasFlag == HasFlag && M->HasMemRefs == HasMemRefs &&
250 M->NumFixedArityOperands == NumFixedArityOperands;
253 unsigned EmitNodeMatcherCommon::getHashImpl() const {
254 return (HashString(OpcodeName) << 4) | Operands.size();
258 unsigned MarkFlagResultsMatcher::getHashImpl() const {
259 return HashUnsigneds(FlagResultNodes.begin(), FlagResultNodes.end());
262 unsigned CompleteMatchMatcher::getHashImpl() const {
263 return HashUnsigneds(Results.begin(), Results.end()) ^
264 ((unsigned)(intptr_t)&Pattern << 8);
267 // isContradictoryImpl Implementations.
269 static bool TypesAreContradictory(MVT::SimpleValueType T1,
270 MVT::SimpleValueType T2) {
271 // If the two types are the same, then they are the same, so they don't
273 if (T1 == T2) return false;
275 // If either type is about iPtr, then they don't conflict unless the other
276 // one is not a scalar integer type.
278 return !MVT(T2).isInteger() || MVT(T2).isVector();
281 return !MVT(T1).isInteger() || MVT(T1).isVector();
283 // Otherwise, they are two different non-iPTR types, they conflict.
287 bool CheckOpcodeMatcher::isContradictoryImpl(const Matcher *M) const {
288 if (const CheckOpcodeMatcher *COM = dyn_cast<CheckOpcodeMatcher>(M)) {
289 // One node can't have two different opcodes!
290 return &COM->getOpcode() != &getOpcode();
293 // TODO: CheckMultiOpcodeMatcher?
295 // If the node has a known type, and if the type we're checking for is
296 // different, then we know they contradict. For example, a check for
297 // ISD::STORE will never be true at the same time a check for Type i32 is.
298 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M)) {
299 // FIXME: What result is this referring to?
301 if (getOpcode().getNumResults() == 0)
302 NodeType = MVT::isVoid;
304 NodeType = getOpcode().getKnownType();
305 if (NodeType != EEVT::isUnknown)
306 return TypesAreContradictory((MVT::SimpleValueType)NodeType,
313 bool CheckTypeMatcher::isContradictoryImpl(const Matcher *M) const {
314 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M))
315 return TypesAreContradictory(getType(), CT->getType());
319 bool CheckChildTypeMatcher::isContradictoryImpl(const Matcher *M) const {
320 if (const CheckChildTypeMatcher *CC = dyn_cast<CheckChildTypeMatcher>(M)) {
321 // If the two checks are about different nodes, we don't know if they
323 if (CC->getChildNo() != getChildNo())
326 return TypesAreContradictory(getType(), CC->getType());
331 bool CheckIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
332 if (const CheckIntegerMatcher *CIM = dyn_cast<CheckIntegerMatcher>(M))
333 return CIM->getValue() != getValue();