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 SwitchOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
92 OS.indent(indent) << "SwitchOpcode: {\n";
93 for (unsigned i = 0, e = Cases.size(); i != e; ++i) {
94 OS.indent(indent) << "case " << Cases[i].first->getEnumName() << ":\n";
95 Cases[i].second->print(OS, indent+2);
97 OS.indent(indent) << "}\n";
101 void CheckMultiOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const{
102 OS.indent(indent) << "CheckMultiOpcode <todo args>\n";
105 void CheckTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
106 OS.indent(indent) << "CheckType " << getEnumName(Type) << '\n';
109 void CheckChildTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
110 OS.indent(indent) << "CheckChildType " << ChildNo << " "
111 << getEnumName(Type) << '\n';
115 void CheckIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
116 OS.indent(indent) << "CheckInteger " << Value << '\n';
119 void CheckCondCodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
120 OS.indent(indent) << "CheckCondCode ISD::" << CondCodeName << '\n';
123 void CheckValueTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
124 OS.indent(indent) << "CheckValueType MVT::" << TypeName << '\n';
127 void CheckComplexPatMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
128 OS.indent(indent) << "CheckComplexPat " << Pattern.getSelectFunc() << '\n';
131 void CheckAndImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
132 OS.indent(indent) << "CheckAndImm " << Value << '\n';
135 void CheckOrImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
136 OS.indent(indent) << "CheckOrImm " << Value << '\n';
139 void CheckFoldableChainNodeMatcher::printImpl(raw_ostream &OS,
140 unsigned indent) const {
141 OS.indent(indent) << "CheckFoldableChainNode\n";
144 void CheckChainCompatibleMatcher::printImpl(raw_ostream &OS,
145 unsigned indent) const {
146 OS.indent(indent) << "CheckChainCompatible " << PreviousOp << "\n";
149 void EmitIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
150 OS.indent(indent) << "EmitInteger " << Val << " VT=" << VT << '\n';
153 void EmitStringIntegerMatcher::
154 printImpl(raw_ostream &OS, unsigned indent) const {
155 OS.indent(indent) << "EmitStringInteger " << Val << " VT=" << VT << '\n';
158 void EmitRegisterMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
159 OS.indent(indent) << "EmitRegister ";
161 OS << Reg->getName();
164 OS << " VT=" << VT << '\n';
167 void EmitConvertToTargetMatcher::
168 printImpl(raw_ostream &OS, unsigned indent) const {
169 OS.indent(indent) << "EmitConvertToTarget " << Slot << '\n';
172 void EmitMergeInputChainsMatcher::
173 printImpl(raw_ostream &OS, unsigned indent) const {
174 OS.indent(indent) << "EmitMergeInputChains <todo: args>\n";
177 void EmitCopyToRegMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
178 OS.indent(indent) << "EmitCopyToReg <todo: args>\n";
181 void EmitNodeXFormMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
182 OS.indent(indent) << "EmitNodeXForm " << NodeXForm->getName()
183 << " Slot=" << Slot << '\n';
187 void EmitNodeMatcherCommon::printImpl(raw_ostream &OS, unsigned indent) const {
189 OS << (isa<MorphNodeToMatcher>(this) ? "MorphNodeTo: " : "EmitNode: ")
190 << OpcodeName << ": <todo flags> ";
192 for (unsigned i = 0, e = VTs.size(); i != e; ++i)
193 OS << ' ' << getEnumName(VTs[i]);
195 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
196 OS << Operands[i] << ' ';
200 void MarkFlagResultsMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
201 OS.indent(indent) << "MarkFlagResults <todo: args>\n";
204 void CompleteMatchMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
205 OS.indent(indent) << "CompleteMatch <todo args>\n";
206 OS.indent(indent) << "Src = " << *Pattern.getSrcPattern() << "\n";
207 OS.indent(indent) << "Dst = " << *Pattern.getDstPattern() << "\n";
210 // getHashImpl Implementation.
212 unsigned CheckPatternPredicateMatcher::getHashImpl() const {
213 return HashString(Predicate);
216 unsigned CheckPredicateMatcher::getHashImpl() const {
217 return HashString(PredName);
220 unsigned CheckOpcodeMatcher::getHashImpl() const {
221 return HashString(Opcode.getEnumName());
224 unsigned CheckMultiOpcodeMatcher::getHashImpl() const {
226 for (unsigned i = 0, e = Opcodes.size(); i != e; ++i)
227 Result |= HashString(Opcodes[i]->getEnumName());
231 unsigned CheckCondCodeMatcher::getHashImpl() const {
232 return HashString(CondCodeName);
235 unsigned CheckValueTypeMatcher::getHashImpl() const {
236 return HashString(TypeName);
239 unsigned EmitStringIntegerMatcher::getHashImpl() const {
240 return HashString(Val) ^ VT;
243 template<typename It>
244 static unsigned HashUnsigneds(It I, It E) {
247 Result = (Result<<3) ^ *I;
251 unsigned EmitMergeInputChainsMatcher::getHashImpl() const {
252 return HashUnsigneds(ChainNodes.begin(), ChainNodes.end());
255 bool CheckOpcodeMatcher::isEqualImpl(const Matcher *M) const {
256 // Note: pointer equality isn't enough here, we have to check the enum names
257 // to ensure that the nodes are for the same opcode.
258 return cast<CheckOpcodeMatcher>(M)->Opcode.getEnumName() ==
259 Opcode.getEnumName();
263 bool EmitNodeMatcherCommon::isEqualImpl(const Matcher *m) const {
264 const EmitNodeMatcherCommon *M = cast<EmitNodeMatcherCommon>(m);
265 return M->OpcodeName == OpcodeName && M->VTs == VTs &&
266 M->Operands == Operands && M->HasChain == HasChain &&
267 M->HasInFlag == HasInFlag && M->HasOutFlag == HasOutFlag &&
268 M->HasMemRefs == HasMemRefs &&
269 M->NumFixedArityOperands == NumFixedArityOperands;
272 unsigned EmitNodeMatcherCommon::getHashImpl() const {
273 return (HashString(OpcodeName) << 4) | Operands.size();
277 unsigned MarkFlagResultsMatcher::getHashImpl() const {
278 return HashUnsigneds(FlagResultNodes.begin(), FlagResultNodes.end());
281 unsigned CompleteMatchMatcher::getHashImpl() const {
282 return HashUnsigneds(Results.begin(), Results.end()) ^
283 ((unsigned)(intptr_t)&Pattern << 8);
286 // isContradictoryImpl Implementations.
288 static bool TypesAreContradictory(MVT::SimpleValueType T1,
289 MVT::SimpleValueType T2) {
290 // If the two types are the same, then they are the same, so they don't
292 if (T1 == T2) return false;
294 // If either type is about iPtr, then they don't conflict unless the other
295 // one is not a scalar integer type.
297 return !MVT(T2).isInteger() || MVT(T2).isVector();
300 return !MVT(T1).isInteger() || MVT(T1).isVector();
302 // Otherwise, they are two different non-iPTR types, they conflict.
306 bool CheckOpcodeMatcher::isContradictoryImpl(const Matcher *M) const {
307 if (const CheckOpcodeMatcher *COM = dyn_cast<CheckOpcodeMatcher>(M)) {
308 // One node can't have two different opcodes!
309 // Note: pointer equality isn't enough here, we have to check the enum names
310 // to ensure that the nodes are for the same opcode.
311 return COM->getOpcode().getEnumName() != getOpcode().getEnumName();
314 // TODO: CheckMultiOpcodeMatcher?
316 // If the node has a known type, and if the type we're checking for is
317 // different, then we know they contradict. For example, a check for
318 // ISD::STORE will never be true at the same time a check for Type i32 is.
319 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M)) {
320 // FIXME: What result is this referring to?
322 if (getOpcode().getNumResults() == 0)
323 NodeType = MVT::isVoid;
325 NodeType = getOpcode().getKnownType();
326 if (NodeType != EEVT::isUnknown)
327 return TypesAreContradictory((MVT::SimpleValueType)NodeType,
334 bool CheckTypeMatcher::isContradictoryImpl(const Matcher *M) const {
335 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M))
336 return TypesAreContradictory(getType(), CT->getType());
340 bool CheckChildTypeMatcher::isContradictoryImpl(const Matcher *M) const {
341 if (const CheckChildTypeMatcher *CC = dyn_cast<CheckChildTypeMatcher>(M)) {
342 // If the two checks are about different nodes, we don't know if they
344 if (CC->getChildNo() != getChildNo())
347 return TypesAreContradictory(getType(), CC->getType());
352 bool CheckIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
353 if (const CheckIntegerMatcher *CIM = dyn_cast<CheckIntegerMatcher>(M))
354 return CIM->getValue() != getValue();