blob: ade058ef5f9295f7dc64fd7df0a7de7e688f6bf5 [file] [log] [blame]
Chris Lattnerda272d12010-02-15 08:04:42 +00001//===- DAGISelMatcher.cpp - Representation of DAG pattern matcher ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "DAGISelMatcher.h"
11#include "CodeGenDAGPatterns.h"
12#include "CodeGenTarget.h"
Chris Lattner845c0422010-02-18 22:03:03 +000013#include "Record.h"
Chris Lattnerda272d12010-02-15 08:04:42 +000014#include "llvm/Support/raw_ostream.h"
Chris Lattner58aa8342010-02-25 06:49:58 +000015#include "llvm/ADT/StringExtras.h"
Chris Lattnerda272d12010-02-15 08:04:42 +000016using namespace llvm;
17
Chris Lattnerb21ba712010-02-25 02:04:40 +000018void Matcher::dump() const {
Chris Lattnera5028a62010-02-25 06:53:39 +000019 print(errs(), 0);
Chris Lattnerda272d12010-02-15 08:04:42 +000020}
21
Chris Lattnera5028a62010-02-25 06:53:39 +000022void Matcher::print(raw_ostream &OS, unsigned indent) const {
23 printImpl(OS, indent);
Chris Lattnerbd8227f2010-02-18 02:53:41 +000024 if (Next)
25 return Next->print(OS, indent);
Chris Lattnerda272d12010-02-15 08:04:42 +000026}
27
Chris Lattner82781b92010-02-27 07:49:13 +000028void Matcher::printOne(raw_ostream &OS) const {
29 printImpl(OS, 0);
30}
31
Chris Lattnerd6c84722010-02-25 19:00:39 +000032ScopeMatcher::~ScopeMatcher() {
33 for (unsigned i = 0, e = Children.size(); i != e; ++i)
34 delete Children[i];
35}
36
37
38// printImpl methods.
39
Chris Lattnera5028a62010-02-25 06:53:39 +000040void ScopeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner60df53e2010-02-25 01:56:48 +000041 OS.indent(indent) << "Scope\n";
Chris Lattnerd6c84722010-02-25 19:00:39 +000042 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
43 getChild(i)->print(OS, indent+2);
Chris Lattnerda272d12010-02-15 08:04:42 +000044}
45
Chris Lattnera5028a62010-02-25 06:53:39 +000046void RecordMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +000047 OS.indent(indent) << "Record\n";
Chris Lattnerda272d12010-02-15 08:04:42 +000048}
49
Chris Lattnera5028a62010-02-25 06:53:39 +000050void RecordChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner19b5a752010-02-24 07:31:45 +000051 OS.indent(indent) << "RecordChild: " << ChildNo << '\n';
Chris Lattner19b5a752010-02-24 07:31:45 +000052}
53
Chris Lattnera5028a62010-02-25 06:53:39 +000054void RecordMemRefMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +000055 OS.indent(indent) << "RecordMemRef\n";
Chris Lattner8e946be2010-02-21 03:22:59 +000056}
57
Chris Lattnera5028a62010-02-25 06:53:39 +000058void CaptureFlagInputMatcher::printImpl(raw_ostream &OS, unsigned indent) const{
Chris Lattner8e946be2010-02-21 03:22:59 +000059 OS.indent(indent) << "CaptureFlagInput\n";
Chris Lattner8e946be2010-02-21 03:22:59 +000060}
61
Chris Lattnera5028a62010-02-25 06:53:39 +000062void MoveChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +000063 OS.indent(indent) << "MoveChild " << ChildNo << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +000064}
65
Chris Lattnera5028a62010-02-25 06:53:39 +000066void MoveParentMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +000067 OS.indent(indent) << "MoveParent\n";
Chris Lattnerda272d12010-02-15 08:04:42 +000068}
69
Chris Lattnera5028a62010-02-25 06:53:39 +000070void CheckSameMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +000071 OS.indent(indent) << "CheckSame " << MatchNumber << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +000072}
73
Chris Lattnerb21ba712010-02-25 02:04:40 +000074void CheckPatternPredicateMatcher::
Chris Lattnera5028a62010-02-25 06:53:39 +000075printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +000076 OS.indent(indent) << "CheckPatternPredicate " << Predicate << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +000077}
78
Chris Lattnera5028a62010-02-25 06:53:39 +000079void CheckPredicateMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +000080 OS.indent(indent) << "CheckPredicate " << PredName << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +000081}
82
Chris Lattnera5028a62010-02-25 06:53:39 +000083void CheckOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnera230f962010-02-27 21:48:43 +000084 OS.indent(indent) << "CheckOpcode " << Opcode.getEnumName() << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +000085}
86
Chris Lattnera5028a62010-02-25 06:53:39 +000087void CheckMultiOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const{
Chris Lattner12a667c2010-02-22 22:30:37 +000088 OS.indent(indent) << "CheckMultiOpcode <todo args>\n";
Chris Lattner12a667c2010-02-22 22:30:37 +000089}
90
Chris Lattnera5028a62010-02-25 06:53:39 +000091void CheckTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +000092 OS.indent(indent) << "CheckType " << getEnumName(Type) << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +000093}
94
Chris Lattnera5028a62010-02-25 06:53:39 +000095void CheckChildTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner23cfda72010-02-24 20:15:25 +000096 OS.indent(indent) << "CheckChildType " << ChildNo << " "
97 << getEnumName(Type) << '\n';
Chris Lattner23cfda72010-02-24 20:15:25 +000098}
99
100
Chris Lattnera5028a62010-02-25 06:53:39 +0000101void CheckIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +0000102 OS.indent(indent) << "CheckInteger " << Value << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000103}
104
Chris Lattnera5028a62010-02-25 06:53:39 +0000105void CheckCondCodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +0000106 OS.indent(indent) << "CheckCondCode ISD::" << CondCodeName << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000107}
108
Chris Lattnera5028a62010-02-25 06:53:39 +0000109void CheckValueTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +0000110 OS.indent(indent) << "CheckValueType MVT::" << TypeName << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000111}
112
Chris Lattnera5028a62010-02-25 06:53:39 +0000113void CheckComplexPatMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +0000114 OS.indent(indent) << "CheckComplexPat " << Pattern.getSelectFunc() << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000115}
116
Chris Lattnera5028a62010-02-25 06:53:39 +0000117void CheckAndImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +0000118 OS.indent(indent) << "CheckAndImm " << Value << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000119}
120
Chris Lattnera5028a62010-02-25 06:53:39 +0000121void CheckOrImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +0000122 OS.indent(indent) << "CheckOrImm " << Value << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000123}
124
Chris Lattnera5028a62010-02-25 06:53:39 +0000125void CheckFoldableChainNodeMatcher::printImpl(raw_ostream &OS,
Chris Lattner21390d72010-02-16 19:15:55 +0000126 unsigned indent) const {
127 OS.indent(indent) << "CheckFoldableChainNode\n";
Chris Lattnere39650a2010-02-16 06:10:58 +0000128}
Chris Lattner9a747f12010-02-17 06:23:39 +0000129
Chris Lattnera5028a62010-02-25 06:53:39 +0000130void CheckChainCompatibleMatcher::printImpl(raw_ostream &OS,
Chris Lattner9a747f12010-02-17 06:23:39 +0000131 unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000132 OS.indent(indent) << "CheckChainCompatible " << PreviousOp << "\n";
Chris Lattner9a747f12010-02-17 06:23:39 +0000133}
Chris Lattner845c0422010-02-18 22:03:03 +0000134
Chris Lattnera5028a62010-02-25 06:53:39 +0000135void EmitIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000136 OS.indent(indent) << "EmitInteger " << Val << " VT=" << VT << '\n';
Chris Lattner8e946be2010-02-21 03:22:59 +0000137}
138
Chris Lattnerb21ba712010-02-25 02:04:40 +0000139void EmitStringIntegerMatcher::
Chris Lattnera5028a62010-02-25 06:53:39 +0000140printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000141 OS.indent(indent) << "EmitStringInteger " << Val << " VT=" << VT << '\n';
Chris Lattner845c0422010-02-18 22:03:03 +0000142}
143
Chris Lattnera5028a62010-02-25 06:53:39 +0000144void EmitRegisterMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000145 OS.indent(indent) << "EmitRegister ";
Chris Lattner845c0422010-02-18 22:03:03 +0000146 if (Reg)
147 OS << Reg->getName();
148 else
149 OS << "zero_reg";
150 OS << " VT=" << VT << '\n';
Chris Lattner845c0422010-02-18 22:03:03 +0000151}
152
Chris Lattnerb21ba712010-02-25 02:04:40 +0000153void EmitConvertToTargetMatcher::
Chris Lattnera5028a62010-02-25 06:53:39 +0000154printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000155 OS.indent(indent) << "EmitConvertToTarget " << Slot << '\n';
Chris Lattner8e946be2010-02-21 03:22:59 +0000156}
157
Chris Lattnerb21ba712010-02-25 02:04:40 +0000158void EmitMergeInputChainsMatcher::
Chris Lattnera5028a62010-02-25 06:53:39 +0000159printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000160 OS.indent(indent) << "EmitMergeInputChains <todo: args>\n";
Chris Lattner8e946be2010-02-21 03:22:59 +0000161}
162
Chris Lattnera5028a62010-02-25 06:53:39 +0000163void EmitCopyToRegMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000164 OS.indent(indent) << "EmitCopyToReg <todo: args>\n";
Chris Lattner8e946be2010-02-21 03:22:59 +0000165}
166
Chris Lattnera5028a62010-02-25 06:53:39 +0000167void EmitNodeXFormMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000168 OS.indent(indent) << "EmitNodeXForm " << NodeXForm->getName()
169 << " Slot=" << Slot << '\n';
Chris Lattner8e946be2010-02-21 03:22:59 +0000170}
171
172
Chris Lattnera5028a62010-02-25 06:53:39 +0000173void EmitNodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000174 OS.indent(indent) << "EmitNode: " << OpcodeName << ": <todo flags> ";
175
176 for (unsigned i = 0, e = VTs.size(); i != e; ++i)
177 OS << ' ' << getEnumName(VTs[i]);
178 OS << '(';
179 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
180 OS << Operands[i] << ' ';
181 OS << ")\n";
Chris Lattner8e946be2010-02-21 03:22:59 +0000182}
183
Chris Lattnera5028a62010-02-25 06:53:39 +0000184void MarkFlagResultsMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner02f73582010-02-24 05:33:42 +0000185 OS.indent(indent) << "MarkFlagResults <todo: args>\n";
Chris Lattner02f73582010-02-24 05:33:42 +0000186}
187
Chris Lattnera5028a62010-02-25 06:53:39 +0000188void CompleteMatchMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner77f2e272010-02-21 06:03:07 +0000189 OS.indent(indent) << "CompleteMatch <todo args>\n";
Chris Lattner8e946be2010-02-21 03:22:59 +0000190 OS.indent(indent) << "Src = " << *Pattern.getSrcPattern() << "\n";
191 OS.indent(indent) << "Dst = " << *Pattern.getDstPattern() << "\n";
Chris Lattner8e946be2010-02-21 03:22:59 +0000192}
193
Chris Lattner58aa8342010-02-25 06:49:58 +0000194// getHashImpl Implementation.
195
196unsigned CheckPatternPredicateMatcher::getHashImpl() const {
197 return HashString(Predicate);
198}
199
200unsigned CheckPredicateMatcher::getHashImpl() const {
201 return HashString(PredName);
202}
203
204unsigned CheckOpcodeMatcher::getHashImpl() const {
Chris Lattnera230f962010-02-27 21:48:43 +0000205 return HashString(Opcode.getEnumName());
Chris Lattner58aa8342010-02-25 06:49:58 +0000206}
207
208unsigned CheckMultiOpcodeMatcher::getHashImpl() const {
209 unsigned Result = 0;
Chris Lattnera230f962010-02-27 21:48:43 +0000210 for (unsigned i = 0, e = Opcodes.size(); i != e; ++i)
211 Result |= HashString(Opcodes[i]->getEnumName());
Chris Lattner58aa8342010-02-25 06:49:58 +0000212 return Result;
213}
214
215unsigned CheckCondCodeMatcher::getHashImpl() const {
216 return HashString(CondCodeName);
217}
218
219unsigned CheckValueTypeMatcher::getHashImpl() const {
220 return HashString(TypeName);
221}
222
223unsigned EmitStringIntegerMatcher::getHashImpl() const {
224 return HashString(Val) ^ VT;
225}
226
227template<typename It>
228static unsigned HashUnsigneds(It I, It E) {
229 unsigned Result = 0;
230 for (; I != E; ++I)
231 Result = (Result<<3) ^ *I;
232 return Result;
233}
234
235unsigned EmitMergeInputChainsMatcher::getHashImpl() const {
236 return HashUnsigneds(ChainNodes.begin(), ChainNodes.end());
237}
238
239bool 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;
245}
246
247unsigned EmitNodeMatcher::getHashImpl() const {
248 return (HashString(OpcodeName) << 4) | Operands.size();
249}
250
251
252unsigned MarkFlagResultsMatcher::getHashImpl() const {
253 return HashUnsigneds(FlagResultNodes.begin(), FlagResultNodes.end());
254}
255
256unsigned CompleteMatchMatcher::getHashImpl() const {
257 return HashUnsigneds(Results.begin(), Results.end()) ^
258 ((unsigned)(intptr_t)&Pattern << 8);
259}
Chris Lattner82781b92010-02-27 07:49:13 +0000260
261// isContradictoryImpl Implementations.
262
263bool CheckOpcodeMatcher::isContradictoryImpl(const Matcher *M) const {
264 if (const CheckOpcodeMatcher *COM = dyn_cast<CheckOpcodeMatcher>(M)) {
265 // One node can't have two different opcodes!
Chris Lattnera230f962010-02-27 21:48:43 +0000266 return &COM->getOpcode() != &getOpcode();
Chris Lattner82781b92010-02-27 07:49:13 +0000267 }
268
269 // TODO: CheckMultiOpcodeMatcher?
Chris Lattner24789622010-02-27 08:11:15 +0000270
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))
Chris Lattner255584a2010-02-27 08:19:47 +0000274 // FIXME: This sucks, get void nodes from type constraints.
Chris Lattnera230f962010-02-27 21:48:43 +0000275 return (getOpcode().getEnumName() == "ISD::STORE" ||
276 getOpcode().getEnumName() == "ISD::INTRINSIC_VOID") &&
Chris Lattner255584a2010-02-27 08:19:47 +0000277 CT->getType() != MVT::isVoid;
Chris Lattner24789622010-02-27 08:11:15 +0000278
Chris Lattner82781b92010-02-27 07:49:13 +0000279 return false;
280}
281
282static 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
285 // contradict.
286 if (T1 == T2) return false;
287
288 // If either type is about iPtr, then they don't conflict unless the other
289 // one is not a scalar integer type.
290 if (T1 == MVT::iPTR)
291 return !MVT(T2).isInteger() || MVT(T2).isVector();
292
293 if (T2 == MVT::iPTR)
294 return !MVT(T1).isInteger() || MVT(T1).isVector();
295
296 // Otherwise, they are two different non-iPTR types, they conflict.
297 return true;
298}
299
300bool CheckTypeMatcher::isContradictoryImpl(const Matcher *M) const {
301 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M))
302 return TypesAreContradictory(getType(), CT->getType());
303 return false;
304}
305
306bool 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
309 // conflict!
310 if (CC->getChildNo() != getChildNo())
311 return false;
312
313 return TypesAreContradictory(getType(), CC->getType());
314 }
315 return false;
316}
317
Chris Lattner24789622010-02-27 08:11:15 +0000318bool CheckIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
319 if (const CheckIntegerMatcher *CIM = dyn_cast<CheckIntegerMatcher>(M))
320 return CIM->getValue() != getValue();
321 return false;
322}