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)
43 getChild(i)->print(OS, indent+2);
46 void RecordMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
47 OS.indent(indent) << "Record\n";
50 void RecordChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
51 OS.indent(indent) << "RecordChild: " << ChildNo << '\n';
54 void RecordMemRefMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
55 OS.indent(indent) << "RecordMemRef\n";
58 void CaptureFlagInputMatcher::printImpl(raw_ostream &OS, unsigned indent) const{
59 OS.indent(indent) << "CaptureFlagInput\n";
62 void MoveChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
63 OS.indent(indent) << "MoveChild " << ChildNo << '\n';
66 void MoveParentMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
67 OS.indent(indent) << "MoveParent\n";
70 void CheckSameMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
71 OS.indent(indent) << "CheckSame " << MatchNumber << '\n';
74 void CheckPatternPredicateMatcher::
75 printImpl(raw_ostream &OS, unsigned indent) const {
76 OS.indent(indent) << "CheckPatternPredicate " << Predicate << '\n';
79 void CheckPredicateMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
80 OS.indent(indent) << "CheckPredicate " << PredName << '\n';
83 void CheckOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
84 OS.indent(indent) << "CheckOpcode " << OpcodeName << '\n';
87 void CheckMultiOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const{
88 OS.indent(indent) << "CheckMultiOpcode <todo args>\n";
91 void CheckTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
92 OS.indent(indent) << "CheckType " << getEnumName(Type) << '\n';
95 void CheckChildTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
96 OS.indent(indent) << "CheckChildType " << ChildNo << " "
97 << getEnumName(Type) << '\n';
101 void CheckIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
102 OS.indent(indent) << "CheckInteger " << Value << '\n';
105 void CheckCondCodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
106 OS.indent(indent) << "CheckCondCode ISD::" << CondCodeName << '\n';
109 void CheckValueTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
110 OS.indent(indent) << "CheckValueType MVT::" << TypeName << '\n';
113 void CheckComplexPatMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
114 OS.indent(indent) << "CheckComplexPat " << Pattern.getSelectFunc() << '\n';
117 void CheckAndImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
118 OS.indent(indent) << "CheckAndImm " << Value << '\n';
121 void CheckOrImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
122 OS.indent(indent) << "CheckOrImm " << Value << '\n';
125 void CheckFoldableChainNodeMatcher::printImpl(raw_ostream &OS,
126 unsigned indent) const {
127 OS.indent(indent) << "CheckFoldableChainNode\n";
130 void CheckChainCompatibleMatcher::printImpl(raw_ostream &OS,
131 unsigned indent) const {
132 OS.indent(indent) << "CheckChainCompatible " << PreviousOp << "\n";
135 void EmitIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
136 OS.indent(indent) << "EmitInteger " << Val << " VT=" << VT << '\n';
139 void EmitStringIntegerMatcher::
140 printImpl(raw_ostream &OS, unsigned indent) const {
141 OS.indent(indent) << "EmitStringInteger " << Val << " VT=" << VT << '\n';
144 void EmitRegisterMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
145 OS.indent(indent) << "EmitRegister ";
147 OS << Reg->getName();
150 OS << " VT=" << VT << '\n';
153 void EmitConvertToTargetMatcher::
154 printImpl(raw_ostream &OS, unsigned indent) const {
155 OS.indent(indent) << "EmitConvertToTarget " << Slot << '\n';
158 void EmitMergeInputChainsMatcher::
159 printImpl(raw_ostream &OS, unsigned indent) const {
160 OS.indent(indent) << "EmitMergeInputChains <todo: args>\n";
163 void EmitCopyToRegMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
164 OS.indent(indent) << "EmitCopyToReg <todo: args>\n";
167 void EmitNodeXFormMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
168 OS.indent(indent) << "EmitNodeXForm " << NodeXForm->getName()
169 << " Slot=" << Slot << '\n';
173 void EmitNodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
174 OS.indent(indent) << "EmitNode: " << OpcodeName << ": <todo flags> ";
176 for (unsigned i = 0, e = VTs.size(); i != e; ++i)
177 OS << ' ' << getEnumName(VTs[i]);
179 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
180 OS << Operands[i] << ' ';
184 void MarkFlagResultsMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
185 OS.indent(indent) << "MarkFlagResults <todo: args>\n";
188 void CompleteMatchMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
189 OS.indent(indent) << "CompleteMatch <todo args>\n";
190 OS.indent(indent) << "Src = " << *Pattern.getSrcPattern() << "\n";
191 OS.indent(indent) << "Dst = " << *Pattern.getDstPattern() << "\n";
194 // getHashImpl Implementation.
196 unsigned CheckPatternPredicateMatcher::getHashImpl() const {
197 return HashString(Predicate);
200 unsigned CheckPredicateMatcher::getHashImpl() const {
201 return HashString(PredName);
204 unsigned CheckOpcodeMatcher::getHashImpl() const {
205 return HashString(OpcodeName);
208 unsigned CheckMultiOpcodeMatcher::getHashImpl() const {
210 for (unsigned i = 0, e = OpcodeNames.size(); i != e; ++i)
211 Result |= HashString(OpcodeNames[i]);
215 unsigned CheckCondCodeMatcher::getHashImpl() const {
216 return HashString(CondCodeName);
219 unsigned CheckValueTypeMatcher::getHashImpl() const {
220 return HashString(TypeName);
223 unsigned EmitStringIntegerMatcher::getHashImpl() const {
224 return HashString(Val) ^ VT;
227 template<typename It>
228 static unsigned HashUnsigneds(It I, It E) {
231 Result = (Result<<3) ^ *I;
235 unsigned EmitMergeInputChainsMatcher::getHashImpl() const {
236 return HashUnsigneds(ChainNodes.begin(), ChainNodes.end());
239 bool EmitNodeMatcher::isEqualImpl(const Matcher *m) const {
240 const EmitNodeMatcher *M = cast<EmitNodeMatcher>(m);
241 return M->OpcodeName == OpcodeName && M->VTs == VTs &&
242 M->Operands == Operands && M->HasChain == HasChain &&
243 M->HasFlag == HasFlag && M->HasMemRefs == HasMemRefs &&
244 M->NumFixedArityOperands == NumFixedArityOperands;
247 unsigned EmitNodeMatcher::getHashImpl() const {
248 return (HashString(OpcodeName) << 4) | Operands.size();
252 unsigned MarkFlagResultsMatcher::getHashImpl() const {
253 return HashUnsigneds(FlagResultNodes.begin(), FlagResultNodes.end());
256 unsigned CompleteMatchMatcher::getHashImpl() const {
257 return HashUnsigneds(Results.begin(), Results.end()) ^
258 ((unsigned)(intptr_t)&Pattern << 8);
261 // isContradictoryImpl Implementations.
263 bool CheckOpcodeMatcher::isContradictoryImpl(const Matcher *M) const {
264 if (const CheckOpcodeMatcher *COM = dyn_cast<CheckOpcodeMatcher>(M)) {
265 // One node can't have two different opcodes!
266 return COM->getOpcodeName() != getOpcodeName();
269 // TODO: CheckMultiOpcodeMatcher?
271 // This is a special common case we see a lot in the X86 backend, we know that
272 // ISD::STORE nodes can't have non-void type.
273 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M))
274 // FIXME: This sucks, get void nodes from type constraints.
275 return (getOpcodeName() == "ISD::STORE" ||
276 getOpcodeName() == "ISD::INTRINSIC_VOID") &&
277 CT->getType() != MVT::isVoid;
282 static bool TypesAreContradictory(MVT::SimpleValueType T1,
283 MVT::SimpleValueType T2) {
284 // If the two types are the same, then they are the same, so they don't
286 if (T1 == T2) return false;
288 // If either type is about iPtr, then they don't conflict unless the other
289 // one is not a scalar integer type.
291 return !MVT(T2).isInteger() || MVT(T2).isVector();
294 return !MVT(T1).isInteger() || MVT(T1).isVector();
296 // Otherwise, they are two different non-iPTR types, they conflict.
300 bool CheckTypeMatcher::isContradictoryImpl(const Matcher *M) const {
301 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M))
302 return TypesAreContradictory(getType(), CT->getType());
306 bool CheckChildTypeMatcher::isContradictoryImpl(const Matcher *M) const {
307 if (const CheckChildTypeMatcher *CC = dyn_cast<CheckChildTypeMatcher>(M)) {
308 // If the two checks are about different nodes, we don't know if they
310 if (CC->getChildNo() != getChildNo())
313 return TypesAreContradictory(getType(), CC->getType());
318 bool CheckIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
319 if (const CheckIntegerMatcher *CIM = dyn_cast<CheckIntegerMatcher>(M))
320 return CIM->getValue() != getValue();