1 //===- DAGISelMatcherEmitter.cpp - Matcher Emitter ------------------------===//
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 // This file contains code to generate C++ code a matcher.
12 //===----------------------------------------------------------------------===//
14 #include "DAGISelMatcher.h"
15 #include "CodeGenDAGPatterns.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/Support/Casting.h"
18 #include "llvm/Support/FormattedStream.h"
27 static unsigned EmitMatcherAndChildren(const MatcherNode *N,
28 formatted_raw_ostream &FOS,
31 /// ClassifyInt - Classify an integer by size, return '1','2','4','8' if this
32 /// fits in 1, 2, 4, or 8 sign extended bytes.
33 static char ClassifyInt(int64_t Val) {
34 if (Val == int8_t(Val)) return '1';
35 if (Val == int16_t(Val)) return '2';
36 if (Val == int32_t(Val)) return '4';
40 /// EmitInt - Emit the specified integer, returning the number of bytes emitted.
41 static unsigned EmitInt(int64_t Val, formatted_raw_ostream &OS) {
42 unsigned BytesEmitted = 1;
43 OS << (int)(unsigned char)Val << ", ";
44 if (Val == int8_t(Val)) {
49 OS << (int)(unsigned char)(Val >> 8) << ", ";
52 if (Val != int16_t(Val)) {
53 OS << (int)(unsigned char)(Val >> 16) << ','
54 << (int)(unsigned char)(Val >> 24) << ',';
57 if (Val != int32_t(Val)) {
58 OS << (int)(unsigned char)(Val >> 32) << ','
59 << (int)(unsigned char)(Val >> 40) << ','
60 << (int)(unsigned char)(Val >> 48) << ','
61 << (int)(unsigned char)(Val >> 56) << ',';
66 OS.PadToColumn(CommentIndent) << "// " << Val << '\n';
70 /// EmitMatcherOpcodes - Emit bytes for the specified matcher and return
71 /// the number of bytes emitted.
72 static unsigned EmitMatcher(const MatcherNode *N, formatted_raw_ostream &OS,
74 OS.PadToColumn(Indent*2);
76 switch (N->getKind()) {
77 case MatcherNode::Push: assert(0 && "Should be handled by caller");
78 case MatcherNode::EmitNode:
79 OS << "OPC_Emit, /*XXX*/";
80 OS.PadToColumn(CommentIndent) << "// Src: "
81 << *cast<EmitNodeMatcherNode>(N)->getPattern().getSrcPattern() << '\n';
82 OS.PadToColumn(CommentIndent) << "// Dst: "
83 << *cast<EmitNodeMatcherNode>(N)->getPattern().getDstPattern() << '\n';
85 case MatcherNode::Record:
86 OS << "OPC_Record,\n";
88 case MatcherNode::MoveChild:
89 OS << "OPC_MoveChild, "
90 << cast<MoveChildMatcherNode>(N)->getChildNo() << ",\n";
93 case MatcherNode::MoveParent:
94 OS << "OPC_MoveParent,\n";
97 case MatcherNode::CheckSame:
98 OS << "OPC_CheckSame, "
99 << cast<CheckSameMatcherNode>(N)->getMatchNumber() << ",\n";
102 case MatcherNode::CheckPatternPredicate:
103 OS << "OPC_CheckPatternPredicate, /*XXX*/0,";
104 OS.PadToColumn(CommentIndent) << "// "
105 << cast<CheckPatternPredicateMatcherNode>(N)->getPredicate() << '\n';
108 case MatcherNode::CheckPredicate:
109 OS << "OPC_CheckPredicate, /*XXX*/0,";
110 OS.PadToColumn(CommentIndent) << "// "
111 << cast<CheckPredicateMatcherNode>(N)->getPredicateName() << '\n';
114 case MatcherNode::CheckOpcode:
115 OS << "OPC_CheckOpcode, "
116 << cast<CheckOpcodeMatcherNode>(N)->getOpcodeName() << ",\n";
119 case MatcherNode::CheckType:
120 OS << "OPC_CheckType, "
121 << getEnumName(cast<CheckTypeMatcherNode>(N)->getType()) << ",\n";
124 case MatcherNode::CheckInteger: {
125 int64_t Val = cast<CheckIntegerMatcherNode>(N)->getValue();
126 OS << "OPC_CheckInteger" << ClassifyInt(Val) << ", ";
127 return EmitInt(Val, OS)+1;
129 case MatcherNode::CheckCondCode:
130 OS << "OPC_CheckCondCode, ISD::"
131 << cast<CheckCondCodeMatcherNode>(N)->getCondCodeName() << ",\n";
134 case MatcherNode::CheckValueType:
135 OS << "OPC_CheckValueType, MVT::"
136 << cast<CheckValueTypeMatcherNode>(N)->getTypeName() << ",\n";
139 case MatcherNode::CheckComplexPat:
140 OS << "OPC_CheckComplexPat, 0/*XXX*/,\n";
143 case MatcherNode::CheckAndImm: {
144 int64_t Val = cast<CheckAndImmMatcherNode>(N)->getValue();
145 OS << "OPC_CheckAndImm" << ClassifyInt(Val) << ", ";
146 return EmitInt(Val, OS)+1;
149 case MatcherNode::CheckOrImm: {
150 int64_t Val = cast<CheckOrImmMatcherNode>(N)->getValue();
151 OS << "OPC_CheckOrImm" << ClassifyInt(Val) << ", ";
152 return EmitInt(Val, OS)+1;
154 case MatcherNode::CheckProfitableToFold:
155 OS << "OPC_IsProfitableToFold,\n";
157 case MatcherNode::CheckLegalToFold:
158 OS << "OPC_IsLegalToFold,\n";
161 assert(0 && "Unreachable");
165 /// EmitMatcherAndChildren - Emit the bytes for the specified matcher subtree.
166 static unsigned EmitMatcherAndChildren(const MatcherNode *N,
167 formatted_raw_ostream &OS,
171 // Push is a special case since it is binary.
172 if (const PushMatcherNode *PMN = dyn_cast<PushMatcherNode>(N)) {
173 // We need to encode the child and the offset of the failure code before
174 // emitting either of them. Handle this by buffering the output into a
175 // string while we get the size.
176 SmallString<128> TmpBuf;
179 raw_svector_ostream OS(TmpBuf);
180 formatted_raw_ostream FOS(OS);
182 EmitMatcherAndChildren(cast<PushMatcherNode>(N)->getChild(), FOS,
186 if (ChildSize > 255) {
188 "Tblgen internal error: can't handle predicate this complex yet\n";
192 OS.PadToColumn(Indent*2);
193 OS << "OPC_Push, " << ChildSize << ",\n";
196 Size += 2 + ChildSize;
198 N = PMN->getFailure();
202 Size += EmitMatcher(N, OS, Indent);
204 // If there are children of this node, iterate to them, otherwise we're
206 if (const MatcherNodeWithChild *MNWC = dyn_cast<MatcherNodeWithChild>(N))
207 N = MNWC->getChild();
213 void llvm::EmitMatcherTable(const MatcherNode *Matcher, raw_ostream &O) {
214 formatted_raw_ostream OS(O);
216 OS << "// The main instruction selector code.\n";
217 OS << "SDNode *SelectCode2(SDNode *N) {\n";
219 OS << " static const unsigned char MatcherTable[] = {\n";
220 unsigned TotalSize = EmitMatcherAndChildren(Matcher, OS, 2);
221 OS << " 0\n }; // Total Array size is " << (TotalSize+1) << " bytes\n\n";
222 OS << " return SelectCodeCommon(N, MatcherTable, sizeof(MatcherTable));\n}\n";