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 " << Opcode.getEnumName() << '\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 EmitNodeMatcherCommon::printImpl(raw_ostream &OS, unsigned indent) const {
175 OS << (isSelectNodeTo() ? "SelectNodeTo: " : "EmitNode: ")
176 << OpcodeName << ": <todo flags> ";
178 for (unsigned i = 0, e = VTs.size(); i != e; ++i)
179 OS << ' ' << getEnumName(VTs[i]);
181 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
182 OS << Operands[i] << ' ';
186 void MarkFlagResultsMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
187 OS.indent(indent) << "MarkFlagResults <todo: args>\n";
190 void CompleteMatchMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
191 OS.indent(indent) << "CompleteMatch <todo args>\n";
192 OS.indent(indent) << "Src = " << *Pattern.getSrcPattern() << "\n";
193 OS.indent(indent) << "Dst = " << *Pattern.getDstPattern() << "\n";
196 // getHashImpl Implementation.
198 unsigned CheckPatternPredicateMatcher::getHashImpl() const {
199 return HashString(Predicate);
202 unsigned CheckPredicateMatcher::getHashImpl() const {
203 return HashString(PredName);
206 unsigned CheckOpcodeMatcher::getHashImpl() const {
207 return HashString(Opcode.getEnumName());
210 unsigned CheckMultiOpcodeMatcher::getHashImpl() const {
212 for (unsigned i = 0, e = Opcodes.size(); i != e; ++i)
213 Result |= HashString(Opcodes[i]->getEnumName());
217 unsigned CheckCondCodeMatcher::getHashImpl() const {
218 return HashString(CondCodeName);
221 unsigned CheckValueTypeMatcher::getHashImpl() const {
222 return HashString(TypeName);
225 unsigned EmitStringIntegerMatcher::getHashImpl() const {
226 return HashString(Val) ^ VT;
229 template<typename It>
230 static unsigned HashUnsigneds(It I, It E) {
233 Result = (Result<<3) ^ *I;
237 unsigned EmitMergeInputChainsMatcher::getHashImpl() const {
238 return HashUnsigneds(ChainNodes.begin(), ChainNodes.end());
241 bool EmitNodeMatcherCommon::isEqualImpl(const Matcher *m) const {
242 const EmitNodeMatcherCommon *M = cast<EmitNodeMatcherCommon>(m);
243 return M->OpcodeName == OpcodeName && M->VTs == VTs &&
244 M->Operands == Operands && M->HasChain == HasChain &&
245 M->HasFlag == HasFlag && M->HasMemRefs == HasMemRefs &&
246 M->NumFixedArityOperands == NumFixedArityOperands;
249 unsigned EmitNodeMatcherCommon::getHashImpl() const {
250 return (HashString(OpcodeName) << 4) | Operands.size();
254 unsigned MarkFlagResultsMatcher::getHashImpl() const {
255 return HashUnsigneds(FlagResultNodes.begin(), FlagResultNodes.end());
258 unsigned CompleteMatchMatcher::getHashImpl() const {
259 return HashUnsigneds(Results.begin(), Results.end()) ^
260 ((unsigned)(intptr_t)&Pattern << 8);
263 // isContradictoryImpl Implementations.
265 static bool TypesAreContradictory(MVT::SimpleValueType T1,
266 MVT::SimpleValueType T2) {
267 // If the two types are the same, then they are the same, so they don't
269 if (T1 == T2) return false;
271 // If either type is about iPtr, then they don't conflict unless the other
272 // one is not a scalar integer type.
274 return !MVT(T2).isInteger() || MVT(T2).isVector();
277 return !MVT(T1).isInteger() || MVT(T1).isVector();
279 // Otherwise, they are two different non-iPTR types, they conflict.
283 bool CheckOpcodeMatcher::isContradictoryImpl(const Matcher *M) const {
284 if (const CheckOpcodeMatcher *COM = dyn_cast<CheckOpcodeMatcher>(M)) {
285 // One node can't have two different opcodes!
286 return &COM->getOpcode() != &getOpcode();
289 // TODO: CheckMultiOpcodeMatcher?
291 // If the node has a known type, and if the type we're checking for is
292 // different, then we know they contradict. For example, a check for
293 // ISD::STORE will never be true at the same time a check for Type i32 is.
294 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M)) {
295 // FIXME: What result is this referring to?
297 if (getOpcode().getNumResults() == 0)
298 NodeType = MVT::isVoid;
300 NodeType = getOpcode().getKnownType();
301 if (NodeType != EEVT::isUnknown)
302 return TypesAreContradictory((MVT::SimpleValueType)NodeType,
309 bool CheckTypeMatcher::isContradictoryImpl(const Matcher *M) const {
310 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M))
311 return TypesAreContradictory(getType(), CT->getType());
315 bool CheckChildTypeMatcher::isContradictoryImpl(const Matcher *M) const {
316 if (const CheckChildTypeMatcher *CC = dyn_cast<CheckChildTypeMatcher>(M)) {
317 // If the two checks are about different nodes, we don't know if they
319 if (CC->getChildNo() != getChildNo())
322 return TypesAreContradictory(getType(), CC->getType());
327 bool CheckIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
328 if (const CheckIntegerMatcher *CIM = dyn_cast<CheckIntegerMatcher>(M))
329 return CIM->getValue() != getValue();