Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 1 | //===- DAGISelMatcherEmitter.cpp - Matcher Emitter ------------------------===// |
| 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 | // This file contains code to generate C++ code a matcher. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "DAGISelMatcher.h" |
| 15 | #include "CodeGenDAGPatterns.h" |
| 16 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | 050a03d | 2010-02-16 07:21:10 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringMap.h" |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 18 | #include "llvm/Support/FormattedStream.h" |
| 19 | using namespace llvm; |
| 20 | |
| 21 | namespace { |
| 22 | enum { |
| 23 | CommentIndent = 25 |
| 24 | }; |
| 25 | } |
| 26 | |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 27 | /// ClassifyInt - Classify an integer by size, return '1','2','4','8' if this |
| 28 | /// fits in 1, 2, 4, or 8 sign extended bytes. |
| 29 | static char ClassifyInt(int64_t Val) { |
| 30 | if (Val == int8_t(Val)) return '1'; |
| 31 | if (Val == int16_t(Val)) return '2'; |
| 32 | if (Val == int32_t(Val)) return '4'; |
| 33 | return '8'; |
| 34 | } |
| 35 | |
| 36 | /// EmitInt - Emit the specified integer, returning the number of bytes emitted. |
| 37 | static unsigned EmitInt(int64_t Val, formatted_raw_ostream &OS) { |
| 38 | unsigned BytesEmitted = 1; |
| 39 | OS << (int)(unsigned char)Val << ", "; |
| 40 | if (Val == int8_t(Val)) { |
| 41 | OS << "\n"; |
| 42 | return BytesEmitted; |
| 43 | } |
| 44 | |
| 45 | OS << (int)(unsigned char)(Val >> 8) << ", "; |
| 46 | ++BytesEmitted; |
| 47 | |
| 48 | if (Val != int16_t(Val)) { |
| 49 | OS << (int)(unsigned char)(Val >> 16) << ',' |
| 50 | << (int)(unsigned char)(Val >> 24) << ','; |
| 51 | BytesEmitted += 2; |
| 52 | |
| 53 | if (Val != int32_t(Val)) { |
| 54 | OS << (int)(unsigned char)(Val >> 32) << ',' |
| 55 | << (int)(unsigned char)(Val >> 40) << ',' |
| 56 | << (int)(unsigned char)(Val >> 48) << ',' |
| 57 | << (int)(unsigned char)(Val >> 56) << ','; |
| 58 | BytesEmitted += 4; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | OS.PadToColumn(CommentIndent) << "// " << Val << '\n'; |
| 63 | return BytesEmitted; |
| 64 | } |
| 65 | |
Chris Lattner | e02ea54 | 2010-02-16 06:52:01 +0000 | [diff] [blame] | 66 | namespace { |
| 67 | class MatcherTableEmitter { |
| 68 | formatted_raw_ostream &OS; |
Chris Lattner | 050a03d | 2010-02-16 07:21:10 +0000 | [diff] [blame] | 69 | |
| 70 | StringMap<unsigned> NodePredicateMap, PatternPredicateMap; |
| 71 | std::vector<std::string> NodePredicates, PatternPredicates; |
| 72 | |
Chris Lattner | e02ea54 | 2010-02-16 06:52:01 +0000 | [diff] [blame] | 73 | public: |
| 74 | MatcherTableEmitter(formatted_raw_ostream &os) : OS(os) {} |
| 75 | |
| 76 | unsigned EmitMatcherAndChildren(const MatcherNode *N, unsigned Indent); |
Chris Lattner | 050a03d | 2010-02-16 07:21:10 +0000 | [diff] [blame] | 77 | |
| 78 | void EmitPredicateFunctions(); |
Chris Lattner | e02ea54 | 2010-02-16 06:52:01 +0000 | [diff] [blame] | 79 | private: |
| 80 | unsigned EmitMatcher(const MatcherNode *N, unsigned Indent); |
Chris Lattner | 050a03d | 2010-02-16 07:21:10 +0000 | [diff] [blame] | 81 | |
| 82 | unsigned getNodePredicate(StringRef PredName) { |
| 83 | unsigned &Entry = NodePredicateMap[PredName]; |
| 84 | if (Entry == 0) { |
| 85 | NodePredicates.push_back(PredName.str()); |
| 86 | Entry = NodePredicates.size(); |
| 87 | } |
| 88 | return Entry-1; |
| 89 | } |
| 90 | unsigned getPatternPredicate(StringRef PredName) { |
| 91 | unsigned &Entry = PatternPredicateMap[PredName]; |
| 92 | if (Entry == 0) { |
| 93 | PatternPredicates.push_back(PredName.str()); |
| 94 | Entry = PatternPredicates.size(); |
| 95 | } |
| 96 | return Entry-1; |
| 97 | } |
Chris Lattner | e02ea54 | 2010-02-16 06:52:01 +0000 | [diff] [blame] | 98 | }; |
| 99 | } // end anonymous namespace. |
| 100 | |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 101 | /// EmitMatcherOpcodes - Emit bytes for the specified matcher and return |
| 102 | /// the number of bytes emitted. |
Chris Lattner | e02ea54 | 2010-02-16 06:52:01 +0000 | [diff] [blame] | 103 | unsigned MatcherTableEmitter:: |
| 104 | EmitMatcher(const MatcherNode *N, unsigned Indent) { |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 105 | OS.PadToColumn(Indent*2); |
| 106 | |
| 107 | switch (N->getKind()) { |
| 108 | case MatcherNode::Push: assert(0 && "Should be handled by caller"); |
| 109 | case MatcherNode::EmitNode: |
| 110 | OS << "OPC_Emit, /*XXX*/"; |
| 111 | OS.PadToColumn(CommentIndent) << "// Src: " |
| 112 | << *cast<EmitNodeMatcherNode>(N)->getPattern().getSrcPattern() << '\n'; |
| 113 | OS.PadToColumn(CommentIndent) << "// Dst: " |
| 114 | << *cast<EmitNodeMatcherNode>(N)->getPattern().getDstPattern() << '\n'; |
| 115 | return 1; |
| 116 | case MatcherNode::Record: |
| 117 | OS << "OPC_Record,\n"; |
| 118 | return 1; |
| 119 | case MatcherNode::MoveChild: |
| 120 | OS << "OPC_MoveChild, " |
| 121 | << cast<MoveChildMatcherNode>(N)->getChildNo() << ",\n"; |
| 122 | return 2; |
| 123 | |
| 124 | case MatcherNode::MoveParent: |
| 125 | OS << "OPC_MoveParent,\n"; |
| 126 | return 1; |
| 127 | |
| 128 | case MatcherNode::CheckSame: |
| 129 | OS << "OPC_CheckSame, " |
| 130 | << cast<CheckSameMatcherNode>(N)->getMatchNumber() << ",\n"; |
| 131 | return 2; |
| 132 | |
Chris Lattner | 050a03d | 2010-02-16 07:21:10 +0000 | [diff] [blame] | 133 | case MatcherNode::CheckPatternPredicate: { |
| 134 | StringRef Pred = cast<CheckPatternPredicateMatcherNode>(N)->getPredicate(); |
| 135 | OS << "OPC_CheckPatternPredicate, " << getPatternPredicate(Pred) << ','; |
| 136 | OS.PadToColumn(CommentIndent) << "// " << Pred << '\n'; |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 137 | return 2; |
Chris Lattner | 050a03d | 2010-02-16 07:21:10 +0000 | [diff] [blame] | 138 | } |
| 139 | case MatcherNode::CheckPredicate: { |
| 140 | StringRef Pred = cast<CheckPredicateMatcherNode>(N)->getPredicateName(); |
| 141 | OS << "OPC_CheckPredicate, " << getNodePredicate(Pred) << ','; |
| 142 | OS.PadToColumn(CommentIndent) << "// " << Pred << '\n'; |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 143 | return 2; |
Chris Lattner | 050a03d | 2010-02-16 07:21:10 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 146 | case MatcherNode::CheckOpcode: |
| 147 | OS << "OPC_CheckOpcode, " |
| 148 | << cast<CheckOpcodeMatcherNode>(N)->getOpcodeName() << ",\n"; |
| 149 | return 2; |
| 150 | |
| 151 | case MatcherNode::CheckType: |
| 152 | OS << "OPC_CheckType, " |
| 153 | << getEnumName(cast<CheckTypeMatcherNode>(N)->getType()) << ",\n"; |
| 154 | return 2; |
| 155 | |
| 156 | case MatcherNode::CheckInteger: { |
| 157 | int64_t Val = cast<CheckIntegerMatcherNode>(N)->getValue(); |
| 158 | OS << "OPC_CheckInteger" << ClassifyInt(Val) << ", "; |
| 159 | return EmitInt(Val, OS)+1; |
| 160 | } |
| 161 | case MatcherNode::CheckCondCode: |
| 162 | OS << "OPC_CheckCondCode, ISD::" |
| 163 | << cast<CheckCondCodeMatcherNode>(N)->getCondCodeName() << ",\n"; |
| 164 | return 2; |
| 165 | |
| 166 | case MatcherNode::CheckValueType: |
| 167 | OS << "OPC_CheckValueType, MVT::" |
| 168 | << cast<CheckValueTypeMatcherNode>(N)->getTypeName() << ",\n"; |
| 169 | return 2; |
| 170 | |
| 171 | case MatcherNode::CheckComplexPat: |
| 172 | OS << "OPC_CheckComplexPat, 0/*XXX*/,\n"; |
| 173 | return 2; |
| 174 | |
| 175 | case MatcherNode::CheckAndImm: { |
| 176 | int64_t Val = cast<CheckAndImmMatcherNode>(N)->getValue(); |
| 177 | OS << "OPC_CheckAndImm" << ClassifyInt(Val) << ", "; |
| 178 | return EmitInt(Val, OS)+1; |
| 179 | } |
| 180 | |
| 181 | case MatcherNode::CheckOrImm: { |
| 182 | int64_t Val = cast<CheckOrImmMatcherNode>(N)->getValue(); |
| 183 | OS << "OPC_CheckOrImm" << ClassifyInt(Val) << ", "; |
| 184 | return EmitInt(Val, OS)+1; |
| 185 | } |
Chris Lattner | 21390d7 | 2010-02-16 19:15:55 +0000 | [diff] [blame^] | 186 | case MatcherNode::CheckFoldableChainNode: |
| 187 | OS << "OPC_CheckFoldableChainNode,\n"; |
Chris Lattner | e39650a | 2010-02-16 06:10:58 +0000 | [diff] [blame] | 188 | return 1; |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 189 | } |
| 190 | assert(0 && "Unreachable"); |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | /// EmitMatcherAndChildren - Emit the bytes for the specified matcher subtree. |
Chris Lattner | e02ea54 | 2010-02-16 06:52:01 +0000 | [diff] [blame] | 195 | unsigned MatcherTableEmitter:: |
| 196 | EmitMatcherAndChildren(const MatcherNode *N, unsigned Indent) { |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 197 | unsigned Size = 0; |
| 198 | while (1) { |
| 199 | // Push is a special case since it is binary. |
| 200 | if (const PushMatcherNode *PMN = dyn_cast<PushMatcherNode>(N)) { |
| 201 | // We need to encode the child and the offset of the failure code before |
| 202 | // emitting either of them. Handle this by buffering the output into a |
| 203 | // string while we get the size. |
| 204 | SmallString<128> TmpBuf; |
| 205 | unsigned ChildSize; |
| 206 | { |
| 207 | raw_svector_ostream OS(TmpBuf); |
| 208 | formatted_raw_ostream FOS(OS); |
| 209 | ChildSize = |
Chris Lattner | e02ea54 | 2010-02-16 06:52:01 +0000 | [diff] [blame] | 210 | EmitMatcherAndChildren(cast<PushMatcherNode>(N)->getChild(),Indent+1); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | if (ChildSize > 255) { |
| 214 | errs() << |
| 215 | "Tblgen internal error: can't handle predicate this complex yet\n"; |
| 216 | exit(1); |
| 217 | } |
| 218 | |
| 219 | OS.PadToColumn(Indent*2); |
| 220 | OS << "OPC_Push, " << ChildSize << ",\n"; |
| 221 | OS << TmpBuf.str(); |
| 222 | |
| 223 | Size += 2 + ChildSize; |
| 224 | |
| 225 | N = PMN->getFailure(); |
| 226 | continue; |
| 227 | } |
| 228 | |
Chris Lattner | e02ea54 | 2010-02-16 06:52:01 +0000 | [diff] [blame] | 229 | Size += EmitMatcher(N, Indent); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 230 | |
| 231 | // If there are children of this node, iterate to them, otherwise we're |
| 232 | // done. |
| 233 | if (const MatcherNodeWithChild *MNWC = dyn_cast<MatcherNodeWithChild>(N)) |
| 234 | N = MNWC->getChild(); |
| 235 | else |
| 236 | return Size; |
| 237 | } |
| 238 | } |
| 239 | |
Chris Lattner | 050a03d | 2010-02-16 07:21:10 +0000 | [diff] [blame] | 240 | void MatcherTableEmitter::EmitPredicateFunctions() { |
| 241 | OS << "bool CheckPatternPredicate(unsigned PredNo) const {\n"; |
| 242 | OS << " switch (PredNo) {\n"; |
| 243 | OS << " default: assert(0 && \"Invalid predicate in table?\");\n"; |
| 244 | for (unsigned i = 0, e = PatternPredicates.size(); i != e; ++i) |
| 245 | OS << " case " << i << ": return " << PatternPredicates[i] << ";\n"; |
| 246 | OS << " }\n"; |
| 247 | OS << "}\n\n"; |
| 248 | |
| 249 | OS << "bool CheckNodePredicate(SDNode *N, unsigned PredNo) const {\n"; |
| 250 | OS << " switch (PredNo) {\n"; |
| 251 | OS << " default: assert(0 && \"Invalid predicate in table?\");\n"; |
| 252 | for (unsigned i = 0, e = NodePredicates.size(); i != e; ++i) |
| 253 | OS << " case " << i << ": return " << NodePredicates[i] << "(N);\n"; |
| 254 | OS << " }\n"; |
| 255 | OS << "}\n\n"; |
| 256 | } |
| 257 | |
| 258 | |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 259 | void llvm::EmitMatcherTable(const MatcherNode *Matcher, raw_ostream &O) { |
| 260 | formatted_raw_ostream OS(O); |
| 261 | |
| 262 | OS << "// The main instruction selector code.\n"; |
| 263 | OS << "SDNode *SelectCode2(SDNode *N) {\n"; |
| 264 | |
Chris Lattner | e02ea54 | 2010-02-16 06:52:01 +0000 | [diff] [blame] | 265 | MatcherTableEmitter MatcherEmitter(OS); |
| 266 | |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 267 | OS << " static const unsigned char MatcherTable[] = {\n"; |
Chris Lattner | e02ea54 | 2010-02-16 06:52:01 +0000 | [diff] [blame] | 268 | unsigned TotalSize = MatcherEmitter.EmitMatcherAndChildren(Matcher, 2); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 269 | OS << " 0\n }; // Total Array size is " << (TotalSize+1) << " bytes\n\n"; |
Chris Lattner | e02ea54 | 2010-02-16 06:52:01 +0000 | [diff] [blame] | 270 | OS << " return SelectCodeCommon(N, MatcherTable,sizeof(MatcherTable));\n}\n"; |
Chris Lattner | 050a03d | 2010-02-16 07:21:10 +0000 | [diff] [blame] | 271 | OS << "\n"; |
| 272 | |
| 273 | // Next up, emit the function for node and pattern predicates: |
| 274 | MatcherEmitter.EmitPredicateFunctions(); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 275 | } |