Silence -Wunused-variable in release builds.
[llvm/stm8.git] / utils / TableGen / DAGISelMatcher.cpp
blobb12e1015c33b629befa10776b681c622c5fa5d9f
1 //===- 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 //===----------------------------------------------------------------------===//
10 #include "DAGISelMatcher.h"
11 #include "CodeGenDAGPatterns.h"
12 #include "CodeGenTarget.h"
13 #include "Record.h"
14 #include "llvm/Support/raw_ostream.h"
15 #include "llvm/ADT/StringExtras.h"
16 using namespace llvm;
18 void Matcher::dump() const {
19 print(errs(), 0);
22 void Matcher::print(raw_ostream &OS, unsigned indent) const {
23 printImpl(OS, indent);
24 if (Next)
25 return Next->print(OS, indent);
28 void Matcher::printOne(raw_ostream &OS) const {
29 printImpl(OS, 0);
32 /// unlinkNode - Unlink the specified node from this chain. If Other == this,
33 /// we unlink the next pointer and return it. Otherwise we unlink Other from
34 /// the list and return this.
35 Matcher *Matcher::unlinkNode(Matcher *Other) {
36 if (this == Other)
37 return takeNext();
39 // Scan until we find the predecessor of Other.
40 Matcher *Cur = this;
41 for (; Cur && Cur->getNext() != Other; Cur = Cur->getNext())
42 /*empty*/;
44 if (Cur == 0) return 0;
45 Cur->takeNext();
46 Cur->setNext(Other->takeNext());
47 return this;
50 /// canMoveBefore - Return true if this matcher is the same as Other, or if
51 /// we can move this matcher past all of the nodes in-between Other and this
52 /// node. Other must be equal to or before this.
53 bool Matcher::canMoveBefore(const Matcher *Other) const {
54 for (;; Other = Other->getNext()) {
55 assert(Other && "Other didn't come before 'this'?");
56 if (this == Other) return true;
58 // We have to be able to move this node across the Other node.
59 if (!canMoveBeforeNode(Other))
60 return false;
64 /// canMoveBefore - Return true if it is safe to move the current matcher
65 /// across the specified one.
66 bool Matcher::canMoveBeforeNode(const Matcher *Other) const {
67 // We can move simple predicates before record nodes.
68 if (isSimplePredicateNode())
69 return Other->isSimplePredicateOrRecordNode();
71 // We can move record nodes across simple predicates.
72 if (isSimplePredicateOrRecordNode())
73 return isSimplePredicateNode();
75 // We can't move record nodes across each other etc.
76 return false;
80 ScopeMatcher::~ScopeMatcher() {
81 for (unsigned i = 0, e = Children.size(); i != e; ++i)
82 delete Children[i];
86 CheckPredicateMatcher::CheckPredicateMatcher(const TreePredicateFn &pred)
87 : Matcher(CheckPredicate), Pred(pred.getOrigPatFragRecord()) {}
89 TreePredicateFn CheckPredicateMatcher::getPredicate() const {
90 return TreePredicateFn(Pred);
95 // printImpl methods.
97 void ScopeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
98 OS.indent(indent) << "Scope\n";
99 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) {
100 if (getChild(i) == 0)
101 OS.indent(indent+1) << "NULL POINTER\n";
102 else
103 getChild(i)->print(OS, indent+2);
107 void RecordMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
108 OS.indent(indent) << "Record\n";
111 void RecordChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
112 OS.indent(indent) << "RecordChild: " << ChildNo << '\n';
115 void RecordMemRefMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
116 OS.indent(indent) << "RecordMemRef\n";
119 void CaptureGlueInputMatcher::printImpl(raw_ostream &OS, unsigned indent) const{
120 OS.indent(indent) << "CaptureGlueInput\n";
123 void MoveChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
124 OS.indent(indent) << "MoveChild " << ChildNo << '\n';
127 void MoveParentMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
128 OS.indent(indent) << "MoveParent\n";
131 void CheckSameMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
132 OS.indent(indent) << "CheckSame " << MatchNumber << '\n';
135 void CheckPatternPredicateMatcher::
136 printImpl(raw_ostream &OS, unsigned indent) const {
137 OS.indent(indent) << "CheckPatternPredicate " << Predicate << '\n';
140 void CheckPredicateMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
141 OS.indent(indent) << "CheckPredicate " << getPredicate().getFnName() << '\n';
144 void CheckOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
145 OS.indent(indent) << "CheckOpcode " << Opcode.getEnumName() << '\n';
148 void SwitchOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
149 OS.indent(indent) << "SwitchOpcode: {\n";
150 for (unsigned i = 0, e = Cases.size(); i != e; ++i) {
151 OS.indent(indent) << "case " << Cases[i].first->getEnumName() << ":\n";
152 Cases[i].second->print(OS, indent+2);
154 OS.indent(indent) << "}\n";
158 void CheckTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
159 OS.indent(indent) << "CheckType " << getEnumName(Type) << ", ResNo="
160 << ResNo << '\n';
163 void SwitchTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
164 OS.indent(indent) << "SwitchType: {\n";
165 for (unsigned i = 0, e = Cases.size(); i != e; ++i) {
166 OS.indent(indent) << "case " << getEnumName(Cases[i].first) << ":\n";
167 Cases[i].second->print(OS, indent+2);
169 OS.indent(indent) << "}\n";
172 void CheckChildTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
173 OS.indent(indent) << "CheckChildType " << ChildNo << " "
174 << getEnumName(Type) << '\n';
178 void CheckIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
179 OS.indent(indent) << "CheckInteger " << Value << '\n';
182 void CheckCondCodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
183 OS.indent(indent) << "CheckCondCode ISD::" << CondCodeName << '\n';
186 void CheckValueTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
187 OS.indent(indent) << "CheckValueType MVT::" << TypeName << '\n';
190 void CheckComplexPatMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
191 OS.indent(indent) << "CheckComplexPat " << Pattern.getSelectFunc() << '\n';
194 void CheckAndImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
195 OS.indent(indent) << "CheckAndImm " << Value << '\n';
198 void CheckOrImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
199 OS.indent(indent) << "CheckOrImm " << Value << '\n';
202 void CheckFoldableChainNodeMatcher::printImpl(raw_ostream &OS,
203 unsigned indent) const {
204 OS.indent(indent) << "CheckFoldableChainNode\n";
207 void EmitIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
208 OS.indent(indent) << "EmitInteger " << Val << " VT=" << VT << '\n';
211 void EmitStringIntegerMatcher::
212 printImpl(raw_ostream &OS, unsigned indent) const {
213 OS.indent(indent) << "EmitStringInteger " << Val << " VT=" << VT << '\n';
216 void EmitRegisterMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
217 OS.indent(indent) << "EmitRegister ";
218 if (Reg)
219 OS << Reg->getName();
220 else
221 OS << "zero_reg";
222 OS << " VT=" << VT << '\n';
225 void EmitConvertToTargetMatcher::
226 printImpl(raw_ostream &OS, unsigned indent) const {
227 OS.indent(indent) << "EmitConvertToTarget " << Slot << '\n';
230 void EmitMergeInputChainsMatcher::
231 printImpl(raw_ostream &OS, unsigned indent) const {
232 OS.indent(indent) << "EmitMergeInputChains <todo: args>\n";
235 void EmitCopyToRegMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
236 OS.indent(indent) << "EmitCopyToReg <todo: args>\n";
239 void EmitNodeXFormMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
240 OS.indent(indent) << "EmitNodeXForm " << NodeXForm->getName()
241 << " Slot=" << Slot << '\n';
245 void EmitNodeMatcherCommon::printImpl(raw_ostream &OS, unsigned indent) const {
246 OS.indent(indent);
247 OS << (isa<MorphNodeToMatcher>(this) ? "MorphNodeTo: " : "EmitNode: ")
248 << OpcodeName << ": <todo flags> ";
250 for (unsigned i = 0, e = VTs.size(); i != e; ++i)
251 OS << ' ' << getEnumName(VTs[i]);
252 OS << '(';
253 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
254 OS << Operands[i] << ' ';
255 OS << ")\n";
258 void MarkGlueResultsMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
259 OS.indent(indent) << "MarkGlueResults <todo: args>\n";
262 void CompleteMatchMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
263 OS.indent(indent) << "CompleteMatch <todo args>\n";
264 OS.indent(indent) << "Src = " << *Pattern.getSrcPattern() << "\n";
265 OS.indent(indent) << "Dst = " << *Pattern.getDstPattern() << "\n";
268 // getHashImpl Implementation.
270 unsigned CheckPatternPredicateMatcher::getHashImpl() const {
271 return HashString(Predicate);
274 unsigned CheckPredicateMatcher::getHashImpl() const {
275 return HashString(getPredicate().getFnName());
278 unsigned CheckOpcodeMatcher::getHashImpl() const {
279 return HashString(Opcode.getEnumName());
282 unsigned CheckCondCodeMatcher::getHashImpl() const {
283 return HashString(CondCodeName);
286 unsigned CheckValueTypeMatcher::getHashImpl() const {
287 return HashString(TypeName);
290 unsigned EmitStringIntegerMatcher::getHashImpl() const {
291 return HashString(Val) ^ VT;
294 template<typename It>
295 static unsigned HashUnsigneds(It I, It E) {
296 unsigned Result = 0;
297 for (; I != E; ++I)
298 Result = (Result<<3) ^ *I;
299 return Result;
302 unsigned EmitMergeInputChainsMatcher::getHashImpl() const {
303 return HashUnsigneds(ChainNodes.begin(), ChainNodes.end());
306 bool CheckOpcodeMatcher::isEqualImpl(const Matcher *M) const {
307 // Note: pointer equality isn't enough here, we have to check the enum names
308 // to ensure that the nodes are for the same opcode.
309 return cast<CheckOpcodeMatcher>(M)->Opcode.getEnumName() ==
310 Opcode.getEnumName();
313 bool EmitNodeMatcherCommon::isEqualImpl(const Matcher *m) const {
314 const EmitNodeMatcherCommon *M = cast<EmitNodeMatcherCommon>(m);
315 return M->OpcodeName == OpcodeName && M->VTs == VTs &&
316 M->Operands == Operands && M->HasChain == HasChain &&
317 M->HasInGlue == HasInGlue && M->HasOutGlue == HasOutGlue &&
318 M->HasMemRefs == HasMemRefs &&
319 M->NumFixedArityOperands == NumFixedArityOperands;
322 unsigned EmitNodeMatcherCommon::getHashImpl() const {
323 return (HashString(OpcodeName) << 4) | Operands.size();
327 unsigned MarkGlueResultsMatcher::getHashImpl() const {
328 return HashUnsigneds(GlueResultNodes.begin(), GlueResultNodes.end());
331 unsigned CompleteMatchMatcher::getHashImpl() const {
332 return HashUnsigneds(Results.begin(), Results.end()) ^
333 ((unsigned)(intptr_t)&Pattern << 8);
336 // isContradictoryImpl Implementations.
338 static bool TypesAreContradictory(MVT::SimpleValueType T1,
339 MVT::SimpleValueType T2) {
340 // If the two types are the same, then they are the same, so they don't
341 // contradict.
342 if (T1 == T2) return false;
344 // If either type is about iPtr, then they don't conflict unless the other
345 // one is not a scalar integer type.
346 if (T1 == MVT::iPTR)
347 return !MVT(T2).isInteger() || MVT(T2).isVector();
349 if (T2 == MVT::iPTR)
350 return !MVT(T1).isInteger() || MVT(T1).isVector();
352 // Otherwise, they are two different non-iPTR types, they conflict.
353 return true;
356 bool CheckOpcodeMatcher::isContradictoryImpl(const Matcher *M) const {
357 if (const CheckOpcodeMatcher *COM = dyn_cast<CheckOpcodeMatcher>(M)) {
358 // One node can't have two different opcodes!
359 // Note: pointer equality isn't enough here, we have to check the enum names
360 // to ensure that the nodes are for the same opcode.
361 return COM->getOpcode().getEnumName() != getOpcode().getEnumName();
364 // If the node has a known type, and if the type we're checking for is
365 // different, then we know they contradict. For example, a check for
366 // ISD::STORE will never be true at the same time a check for Type i32 is.
367 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M)) {
368 // If checking for a result the opcode doesn't have, it can't match.
369 if (CT->getResNo() >= getOpcode().getNumResults())
370 return true;
372 MVT::SimpleValueType NodeType = getOpcode().getKnownType(CT->getResNo());
373 if (NodeType != MVT::Other)
374 return TypesAreContradictory(NodeType, CT->getType());
377 return false;
380 bool CheckTypeMatcher::isContradictoryImpl(const Matcher *M) const {
381 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M))
382 return TypesAreContradictory(getType(), CT->getType());
383 return false;
386 bool CheckChildTypeMatcher::isContradictoryImpl(const Matcher *M) const {
387 if (const CheckChildTypeMatcher *CC = dyn_cast<CheckChildTypeMatcher>(M)) {
388 // If the two checks are about different nodes, we don't know if they
389 // conflict!
390 if (CC->getChildNo() != getChildNo())
391 return false;
393 return TypesAreContradictory(getType(), CC->getType());
395 return false;
398 bool CheckIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
399 if (const CheckIntegerMatcher *CIM = dyn_cast<CheckIntegerMatcher>(M))
400 return CIM->getValue() != getValue();
401 return false;
404 bool CheckValueTypeMatcher::isContradictoryImpl(const Matcher *M) const {
405 if (const CheckValueTypeMatcher *CVT = dyn_cast<CheckValueTypeMatcher>(M))
406 return CVT->getTypeName() != getTypeName();
407 return false;