Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1 | //===- GlobalISelEmitter.cpp - Generate an instruction selector -----------===// |
| 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 | /// \file |
| 11 | /// This tablegen backend emits code for use by the GlobalISel instruction |
| 12 | /// selector. See include/llvm/CodeGen/TargetGlobalISel.td. |
| 13 | /// |
| 14 | /// This file analyzes the patterns recognized by the SelectionDAGISel tablegen |
| 15 | /// backend, filters out the ones that are unsupported, maps |
| 16 | /// SelectionDAG-specific constructs to their GlobalISel counterpart |
| 17 | /// (when applicable: MVT to LLT; SDNode to generic Instruction). |
| 18 | /// |
| 19 | /// Not all patterns are supported: pass the tablegen invocation |
| 20 | /// "-warn-on-skipped-patterns" to emit a warning when a pattern is skipped, |
| 21 | /// as well as why. |
| 22 | /// |
| 23 | /// The generated file defines a single method: |
| 24 | /// bool <Target>InstructionSelector::selectImpl(MachineInstr &I) const; |
| 25 | /// intended to be used in InstructionSelector::select as the first-step |
| 26 | /// selector for the patterns that don't require complex C++. |
| 27 | /// |
| 28 | /// FIXME: We'll probably want to eventually define a base |
| 29 | /// "TargetGenInstructionSelector" class. |
| 30 | /// |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
| 33 | #include "CodeGenDAGPatterns.h" |
| 34 | #include "llvm/ADT/Optional.h" |
| 35 | #include "llvm/ADT/Statistic.h" |
| 36 | #include "llvm/CodeGen/MachineValueType.h" |
| 37 | #include "llvm/Support/CommandLine.h" |
| 38 | #include "llvm/TableGen/Error.h" |
| 39 | #include "llvm/TableGen/Record.h" |
| 40 | #include "llvm/TableGen/TableGenBackend.h" |
| 41 | #include <string> |
| 42 | using namespace llvm; |
| 43 | |
| 44 | #define DEBUG_TYPE "gisel-emitter" |
| 45 | |
| 46 | STATISTIC(NumPatternTotal, "Total number of patterns"); |
| 47 | STATISTIC(NumPatternSkipped, "Number of patterns skipped"); |
| 48 | STATISTIC(NumPatternEmitted, "Number of patterns emitted"); |
| 49 | |
| 50 | static cl::opt<bool> WarnOnSkippedPatterns( |
| 51 | "warn-on-skipped-patterns", |
| 52 | cl::desc("Explain why a pattern was skipped for inclusion " |
| 53 | "in the GlobalISel selector"), |
| 54 | cl::init(false)); |
| 55 | |
| 56 | namespace { |
| 57 | |
| 58 | class GlobalISelEmitter { |
| 59 | public: |
| 60 | explicit GlobalISelEmitter(RecordKeeper &RK); |
| 61 | void run(raw_ostream &OS); |
| 62 | |
| 63 | private: |
| 64 | const RecordKeeper &RK; |
| 65 | const CodeGenDAGPatterns CGP; |
| 66 | const CodeGenTarget &Target; |
| 67 | |
| 68 | /// Keep track of the equivalence between SDNodes and Instruction. |
| 69 | /// This is defined using 'GINodeEquiv' in the target description. |
| 70 | DenseMap<Record *, const CodeGenInstruction *> NodeEquivs; |
| 71 | |
| 72 | void gatherNodeEquivs(); |
| 73 | const CodeGenInstruction *findNodeEquiv(Record *N); |
| 74 | |
| 75 | struct SkipReason { |
| 76 | std::string Reason; |
| 77 | }; |
| 78 | |
| 79 | /// Analyze pattern \p P, possibly emitting matching code for it to \p OS. |
| 80 | /// Otherwise, return a reason why this pattern was skipped for emission. |
| 81 | Optional<SkipReason> runOnPattern(const PatternToMatch &P, |
| 82 | raw_ostream &OS); |
| 83 | }; |
| 84 | |
| 85 | } // end anonymous namespace |
| 86 | |
| 87 | //===- Helper functions ---------------------------------------------------===// |
| 88 | |
| 89 | /// Convert an MVT to an equivalent LLT if possible, or the invalid LLT() for |
| 90 | /// MVTs that don't map cleanly to an LLT (e.g., iPTR, *any, ...). |
| 91 | static Optional<std::string> MVTToLLT(MVT::SimpleValueType SVT) { |
| 92 | std::string TyStr; |
| 93 | raw_string_ostream OS(TyStr); |
| 94 | MVT VT(SVT); |
| 95 | if (VT.isVector() && VT.getVectorNumElements() != 1) { |
| 96 | OS << "LLT::vector(" << VT.getVectorNumElements() << ", " |
| 97 | << VT.getScalarSizeInBits() << ")"; |
| 98 | } else if (VT.isInteger() || VT.isFloatingPoint()) { |
| 99 | OS << "LLT::scalar(" << VT.getSizeInBits() << ")"; |
| 100 | } else { |
| 101 | return None; |
| 102 | } |
| 103 | OS.flush(); |
| 104 | return TyStr; |
| 105 | } |
| 106 | |
| 107 | static bool isTrivialOperatorNode(const TreePatternNode *N) { |
| 108 | return !N->isLeaf() && !N->hasAnyPredicate() && !N->getTransformFn(); |
| 109 | } |
| 110 | |
| 111 | //===- Matchers -----------------------------------------------------------===// |
| 112 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 113 | struct MatchAction { |
| 114 | virtual ~MatchAction() {} |
| 115 | virtual void emit(raw_ostream &OS) const = 0; |
| 116 | }; |
| 117 | |
| 118 | raw_ostream &operator<<(raw_ostream &S, const MatchAction &A) { |
| 119 | A.emit(S); |
| 120 | return S; |
| 121 | } |
| 122 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 123 | template <class PredicateTy> class PredicateListMatcher { |
| 124 | private: |
| 125 | typedef std::vector<std::unique_ptr<PredicateTy>> PredicateVec; |
| 126 | PredicateVec Predicates; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 127 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 128 | public: |
| 129 | /// Construct a new operand predicate and add it to the matcher. |
| 130 | template <class Kind, class... Args> |
| 131 | Kind &addPredicate(Args&&... args) { |
Ahmed Bougacha | b67a3ce | 2017-01-26 22:07:37 +0000 | [diff] [blame^] | 132 | Predicates.emplace_back( |
| 133 | llvm::make_unique<Kind>(std::forward<Args>(args)...)); |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 134 | return *static_cast<Kind *>(Predicates.back().get()); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 135 | } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 136 | |
| 137 | typename PredicateVec::const_iterator predicates_begin() const { return Predicates.begin(); } |
| 138 | typename PredicateVec::const_iterator predicates_end() const { return Predicates.end(); } |
| 139 | iterator_range<typename PredicateVec::const_iterator> predicates() const { |
| 140 | return make_range(predicates_begin(), predicates_end()); |
| 141 | } |
| 142 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 143 | /// Emit a C++ expression that tests whether all the predicates are met. |
Ahmed Bougacha | b67a3ce | 2017-01-26 22:07:37 +0000 | [diff] [blame^] | 144 | template <class... Args> |
| 145 | void emitCxxPredicatesExpr(raw_ostream &OS, Args &&... args) const { |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 146 | if (Predicates.empty()) { |
| 147 | OS << "true"; |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | StringRef Separator = ""; |
| 152 | for (const auto &Predicate : predicates()) { |
| 153 | OS << Separator << "("; |
Ahmed Bougacha | b67a3ce | 2017-01-26 22:07:37 +0000 | [diff] [blame^] | 154 | Predicate->emitCxxPredicateExpr(OS, std::forward<Args>(args)...); |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 155 | OS << ")"; |
| 156 | Separator = " && "; |
| 157 | } |
| 158 | } |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 159 | }; |
| 160 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 161 | /// Generates code to check a predicate of an operand. |
| 162 | /// |
| 163 | /// Typical predicates include: |
| 164 | /// * Operand is a particular register. |
| 165 | /// * Operand is assigned a particular register bank. |
| 166 | /// * Operand is an MBB. |
| 167 | class OperandPredicateMatcher { |
| 168 | public: |
| 169 | virtual ~OperandPredicateMatcher() {} |
| 170 | |
| 171 | /// Emit a C++ expression that checks the predicate for the OpIdx operand of |
| 172 | /// the instruction given in InsnVarName. |
| 173 | virtual void emitCxxPredicateExpr(raw_ostream &OS, |
| 174 | const StringRef InsnVarName, |
| 175 | unsigned OpIdx) const = 0; |
| 176 | }; |
| 177 | |
| 178 | /// Generates code to check that an operand is a particular LLT. |
| 179 | class LLTOperandMatcher : public OperandPredicateMatcher { |
| 180 | protected: |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 181 | std::string Ty; |
| 182 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 183 | public: |
| 184 | LLTOperandMatcher(std::string Ty) : Ty(Ty) {} |
| 185 | |
| 186 | void emitCxxPredicateExpr(raw_ostream &OS, const StringRef InsnVarName, |
| 187 | unsigned OpIdx) const override { |
| 188 | OS << "MRI.getType(" << InsnVarName << ".getOperand(" << OpIdx |
| 189 | << ").getReg()) == (" << Ty << ")"; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 190 | } |
| 191 | }; |
| 192 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 193 | /// Generates code to check that an operand is in a particular register bank. |
| 194 | class RegisterBankOperandMatcher : public OperandPredicateMatcher { |
| 195 | protected: |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 196 | const CodeGenRegisterClass &RC; |
| 197 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 198 | public: |
| 199 | RegisterBankOperandMatcher(const CodeGenRegisterClass &RC) : RC(RC) {} |
| 200 | |
| 201 | void emitCxxPredicateExpr(raw_ostream &OS, const StringRef InsnVarName, |
| 202 | unsigned OpIdx) const override { |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 203 | OS << "(&RBI.getRegBankFromRegClass(" << RC.getQualifiedName() |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 204 | << "RegClass) == RBI.getRegBank(" << InsnVarName << ".getOperand(" |
| 205 | << OpIdx << ").getReg(), MRI, TRI))"; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 206 | } |
| 207 | }; |
| 208 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 209 | /// Generates code to check that an operand is a basic block. |
| 210 | class MBBOperandMatcher : public OperandPredicateMatcher { |
| 211 | public: |
| 212 | void emitCxxPredicateExpr(raw_ostream &OS, const StringRef InsnVarName, |
| 213 | unsigned OpIdx) const override { |
| 214 | OS << InsnVarName << ".getOperand(" << OpIdx << ").isMBB()"; |
| 215 | } |
| 216 | }; |
| 217 | |
| 218 | /// Generates code to check that a set of predicates match for a particular |
| 219 | /// operand. |
| 220 | class OperandMatcher : public PredicateListMatcher<OperandPredicateMatcher> { |
| 221 | protected: |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 222 | unsigned OpIdx; |
| 223 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 224 | public: |
| 225 | OperandMatcher(unsigned OpIdx) : OpIdx(OpIdx) {} |
| 226 | |
| 227 | /// Emit a C++ expression that tests whether the instruction named in |
| 228 | /// InsnVarName matches all the predicate and all the operands. |
| 229 | void emitCxxPredicateExpr(raw_ostream &OS, const StringRef InsnVarName) const { |
| 230 | OS << "("; |
| 231 | emitCxxPredicatesExpr(OS, InsnVarName, OpIdx); |
| 232 | OS << ")"; |
| 233 | } |
| 234 | }; |
| 235 | |
| 236 | /// Generates code to check a predicate on an instruction. |
| 237 | /// |
| 238 | /// Typical predicates include: |
| 239 | /// * The opcode of the instruction is a particular value. |
| 240 | /// * The nsw/nuw flag is/isn't set. |
| 241 | class InstructionPredicateMatcher { |
| 242 | public: |
| 243 | virtual ~InstructionPredicateMatcher() {} |
| 244 | |
| 245 | /// Emit a C++ expression that tests whether the instruction named in |
| 246 | /// InsnVarName matches the predicate. |
| 247 | virtual void emitCxxPredicateExpr(raw_ostream &OS, |
| 248 | const StringRef InsnVarName) const = 0; |
| 249 | }; |
| 250 | |
| 251 | /// Generates code to check the opcode of an instruction. |
| 252 | class InstructionOpcodeMatcher : public InstructionPredicateMatcher { |
| 253 | protected: |
| 254 | const CodeGenInstruction *I; |
| 255 | |
| 256 | public: |
| 257 | InstructionOpcodeMatcher(const CodeGenInstruction *I) : I(I) {} |
| 258 | |
| 259 | void emitCxxPredicateExpr(raw_ostream &OS, |
| 260 | const StringRef InsnVarName) const override { |
| 261 | OS << InsnVarName << ".getOpcode() == " << I->Namespace |
| 262 | << "::" << I->TheDef->getName(); |
| 263 | } |
| 264 | }; |
| 265 | |
| 266 | /// Generates code to check that a set of predicates and operands match for a |
| 267 | /// particular instruction. |
| 268 | /// |
| 269 | /// Typical predicates include: |
| 270 | /// * Has a specific opcode. |
| 271 | /// * Has an nsw/nuw flag or doesn't. |
| 272 | class InstructionMatcher |
| 273 | : public PredicateListMatcher<InstructionPredicateMatcher> { |
| 274 | protected: |
| 275 | std::vector<OperandMatcher> Operands; |
| 276 | |
| 277 | public: |
| 278 | /// Add an operand to the matcher. |
| 279 | OperandMatcher &addOperand(unsigned OpIdx) { |
| 280 | Operands.emplace_back(OpIdx); |
| 281 | return Operands.back(); |
| 282 | } |
| 283 | |
| 284 | /// Emit a C++ expression that tests whether the instruction named in |
| 285 | /// InsnVarName matches all the predicates and all the operands. |
| 286 | void emitCxxPredicateExpr(raw_ostream &OS, const StringRef InsnVarName) const { |
| 287 | emitCxxPredicatesExpr(OS, InsnVarName); |
| 288 | for (const auto &Operand : Operands) { |
| 289 | OS << " && ("; |
| 290 | Operand.emitCxxPredicateExpr(OS, InsnVarName); |
| 291 | OS << ")"; |
| 292 | } |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 293 | } |
| 294 | }; |
| 295 | |
| 296 | struct MutateOpcode : public MatchAction { |
| 297 | MutateOpcode(const CodeGenInstruction *I) : I(I) {} |
| 298 | const CodeGenInstruction *I; |
| 299 | |
| 300 | virtual void emit(raw_ostream &OS) const { |
| 301 | OS << "I.setDesc(TII.get(" << I->Namespace << "::" << I->TheDef->getName() |
| 302 | << "));"; |
| 303 | } |
| 304 | }; |
| 305 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 306 | /// Generates code to check that a match rule matches. |
| 307 | /// |
| 308 | /// This currently supports a single match position but could be extended to |
| 309 | /// support multiple positions to support div/rem fusion or load-multiple |
| 310 | /// instructions. |
| 311 | class RuleMatcher { |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 312 | const PatternToMatch &P; |
| 313 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 314 | std::vector<std::unique_ptr<InstructionMatcher>> Matchers; |
| 315 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 316 | public: |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 317 | std::vector<std::unique_ptr<MatchAction>> Actions; |
| 318 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 319 | RuleMatcher(const PatternToMatch &P) : P(P) {} |
| 320 | |
| 321 | InstructionMatcher &addInstructionMatcher() { |
| 322 | Matchers.emplace_back(new InstructionMatcher()); |
| 323 | return *Matchers.back(); |
| 324 | } |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 325 | |
| 326 | void emit(raw_ostream &OS) { |
| 327 | if (Matchers.empty()) |
| 328 | llvm_unreachable("Unexpected empty matcher!"); |
| 329 | |
| 330 | OS << " // Src: " << *P.getSrcPattern() << "\n" |
| 331 | << " // Dst: " << *P.getDstPattern() << "\n"; |
| 332 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 333 | // The representation supports rules that require multiple roots such as: |
| 334 | // %ptr(p0) = ... |
| 335 | // %elt0(s32) = G_LOAD %ptr |
| 336 | // %1(p0) = G_ADD %ptr, 4 |
| 337 | // %elt1(s32) = G_LOAD p0 %1 |
| 338 | // which could be usefully folded into: |
| 339 | // %ptr(p0) = ... |
| 340 | // %elt0(s32), %elt1(s32) = TGT_LOAD_PAIR %ptr |
| 341 | // on some targets but we don't need to make use of that yet. |
| 342 | assert(Matchers.size() == 1 && "Cannot handle multi-root matchers yet"); |
| 343 | OS << " if ("; |
| 344 | Matchers.front()->emitCxxPredicateExpr(OS, "I"); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 345 | OS << ") {\n"; |
| 346 | |
| 347 | for (auto &MA : Actions) |
| 348 | OS << " " << *MA << "\n"; |
| 349 | |
| 350 | OS << " constrainSelectedInstRegOperands(I, TII, TRI, RBI);\n"; |
| 351 | OS << " return true;\n"; |
| 352 | OS << " }\n"; |
| 353 | } |
| 354 | }; |
| 355 | |
| 356 | //===- GlobalISelEmitter class --------------------------------------------===// |
| 357 | |
| 358 | void GlobalISelEmitter::gatherNodeEquivs() { |
| 359 | assert(NodeEquivs.empty()); |
| 360 | for (Record *Equiv : RK.getAllDerivedDefinitions("GINodeEquiv")) |
| 361 | NodeEquivs[Equiv->getValueAsDef("Node")] = |
| 362 | &Target.getInstruction(Equiv->getValueAsDef("I")); |
| 363 | } |
| 364 | |
| 365 | const CodeGenInstruction *GlobalISelEmitter::findNodeEquiv(Record *N) { |
| 366 | return NodeEquivs.lookup(N); |
| 367 | } |
| 368 | |
| 369 | GlobalISelEmitter::GlobalISelEmitter(RecordKeeper &RK) |
| 370 | : RK(RK), CGP(RK), Target(CGP.getTargetInfo()) {} |
| 371 | |
| 372 | //===- Emitter ------------------------------------------------------------===// |
| 373 | |
| 374 | Optional<GlobalISelEmitter::SkipReason> |
| 375 | GlobalISelEmitter::runOnPattern(const PatternToMatch &P, raw_ostream &OS) { |
| 376 | |
| 377 | // Keep track of the matchers and actions to emit. |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 378 | RuleMatcher M(P); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 379 | |
| 380 | // First, analyze the whole pattern. |
| 381 | // If the entire pattern has a predicate (e.g., target features), ignore it. |
| 382 | if (!P.getPredicates()->getValues().empty()) |
| 383 | return SkipReason{"Pattern has a predicate"}; |
| 384 | |
| 385 | // Physreg imp-defs require additional logic. Ignore the pattern. |
| 386 | if (!P.getDstRegs().empty()) |
| 387 | return SkipReason{"Pattern defines a physical register"}; |
| 388 | |
| 389 | // Next, analyze the pattern operators. |
| 390 | TreePatternNode *Src = P.getSrcPattern(); |
| 391 | TreePatternNode *Dst = P.getDstPattern(); |
| 392 | |
| 393 | // If the root of either pattern isn't a simple operator, ignore it. |
| 394 | if (!isTrivialOperatorNode(Dst)) |
| 395 | return SkipReason{"Dst pattern root isn't a trivial operator"}; |
| 396 | if (!isTrivialOperatorNode(Src)) |
| 397 | return SkipReason{"Src pattern root isn't a trivial operator"}; |
| 398 | |
| 399 | Record *DstOp = Dst->getOperator(); |
| 400 | if (!DstOp->isSubClassOf("Instruction")) |
| 401 | return SkipReason{"Pattern operator isn't an instruction"}; |
| 402 | |
| 403 | auto &DstI = Target.getInstruction(DstOp); |
| 404 | |
| 405 | auto SrcGIOrNull = findNodeEquiv(Src->getOperator()); |
| 406 | if (!SrcGIOrNull) |
| 407 | return SkipReason{"Pattern operator lacks an equivalent Instruction"}; |
| 408 | auto &SrcGI = *SrcGIOrNull; |
| 409 | |
| 410 | // The operators look good: match the opcode and mutate it to the new one. |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 411 | InstructionMatcher &InsnMatcher = M.addInstructionMatcher(); |
| 412 | InsnMatcher.addPredicate<InstructionOpcodeMatcher>(&SrcGI); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 413 | M.Actions.emplace_back(new MutateOpcode(&DstI)); |
| 414 | |
| 415 | // Next, analyze the children, only accepting patterns that don't require |
| 416 | // any change to operands. |
| 417 | if (Src->getNumChildren() != Dst->getNumChildren()) |
| 418 | return SkipReason{"Src/dst patterns have a different # of children"}; |
| 419 | |
| 420 | unsigned OpIdx = 0; |
| 421 | |
| 422 | // Start with the defined operands (i.e., the results of the root operator). |
| 423 | if (DstI.Operands.NumDefs != Src->getExtTypes().size()) |
| 424 | return SkipReason{"Src pattern results and dst MI defs are different"}; |
| 425 | |
| 426 | for (const EEVT::TypeSet &Ty : Src->getExtTypes()) { |
| 427 | Record *DstIOpRec = DstI.Operands[OpIdx].Rec; |
| 428 | if (!DstIOpRec->isSubClassOf("RegisterClass")) |
| 429 | return SkipReason{"Dst MI def isn't a register class"}; |
| 430 | |
| 431 | auto OpTyOrNone = MVTToLLT(Ty.getConcrete()); |
| 432 | if (!OpTyOrNone) |
| 433 | return SkipReason{"Dst operand has an unsupported type"}; |
| 434 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 435 | OperandMatcher &OM = InsnMatcher.addOperand(OpIdx); |
| 436 | OM.addPredicate<LLTOperandMatcher>(*OpTyOrNone); |
| 437 | OM.addPredicate<RegisterBankOperandMatcher>( |
| 438 | Target.getRegisterClass(DstIOpRec)); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 439 | ++OpIdx; |
| 440 | } |
| 441 | |
| 442 | // Finally match the used operands (i.e., the children of the root operator). |
| 443 | for (unsigned i = 0, e = Src->getNumChildren(); i != e; ++i) { |
| 444 | auto *SrcChild = Src->getChild(i); |
| 445 | auto *DstChild = Dst->getChild(i); |
| 446 | |
| 447 | // Patterns can reorder operands. Ignore those for now. |
| 448 | if (SrcChild->getName() != DstChild->getName()) |
| 449 | return SkipReason{"Src/dst pattern children not in same order"}; |
| 450 | |
| 451 | // The only non-leaf child we accept is 'bb': it's an operator because |
| 452 | // BasicBlockSDNode isn't inline, but in MI it's just another operand. |
| 453 | if (!SrcChild->isLeaf()) { |
| 454 | if (DstChild->isLeaf() || |
| 455 | SrcChild->getOperator() != DstChild->getOperator()) |
| 456 | return SkipReason{"Src/dst pattern child operator mismatch"}; |
| 457 | |
| 458 | if (SrcChild->getOperator()->isSubClassOf("SDNode")) { |
| 459 | auto &ChildSDNI = CGP.getSDNodeInfo(SrcChild->getOperator()); |
| 460 | if (ChildSDNI.getSDClassName() == "BasicBlockSDNode") { |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 461 | InsnMatcher.addOperand(OpIdx++).addPredicate<MBBOperandMatcher>(); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 462 | continue; |
| 463 | } |
| 464 | } |
| 465 | return SkipReason{"Src pattern child isn't a leaf node"}; |
| 466 | } |
| 467 | |
| 468 | if (SrcChild->getLeafValue() != DstChild->getLeafValue()) |
| 469 | return SkipReason{"Src/dst pattern child leaf mismatch"}; |
| 470 | |
| 471 | // Otherwise, we're looking for a bog-standard RegisterClass operand. |
| 472 | if (SrcChild->hasAnyPredicate()) |
| 473 | return SkipReason{"Src pattern child has predicate"}; |
| 474 | auto *ChildRec = cast<DefInit>(SrcChild->getLeafValue())->getDef(); |
| 475 | if (!ChildRec->isSubClassOf("RegisterClass")) |
| 476 | return SkipReason{"Src pattern child isn't a RegisterClass"}; |
| 477 | |
| 478 | ArrayRef<EEVT::TypeSet> ChildTypes = SrcChild->getExtTypes(); |
| 479 | if (ChildTypes.size() != 1) |
| 480 | return SkipReason{"Src pattern child has multiple results"}; |
| 481 | |
| 482 | auto OpTyOrNone = MVTToLLT(ChildTypes.front().getConcrete()); |
| 483 | if (!OpTyOrNone) |
| 484 | return SkipReason{"Src operand has an unsupported type"}; |
| 485 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 486 | OperandMatcher &OM = InsnMatcher.addOperand(OpIdx); |
| 487 | OM.addPredicate<LLTOperandMatcher>(*OpTyOrNone); |
| 488 | OM.addPredicate<RegisterBankOperandMatcher>( |
| 489 | Target.getRegisterClass(ChildRec)); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 490 | ++OpIdx; |
| 491 | } |
| 492 | |
| 493 | // We're done with this pattern! Emit the processed result. |
| 494 | M.emit(OS); |
| 495 | ++NumPatternEmitted; |
| 496 | return None; |
| 497 | } |
| 498 | |
| 499 | void GlobalISelEmitter::run(raw_ostream &OS) { |
| 500 | // Track the GINodeEquiv definitions. |
| 501 | gatherNodeEquivs(); |
| 502 | |
| 503 | emitSourceFileHeader(("Global Instruction Selector for the " + |
| 504 | Target.getName() + " target").str(), OS); |
| 505 | OS << "bool " << Target.getName() |
| 506 | << "InstructionSelector::selectImpl" |
| 507 | "(MachineInstr &I) const {\n const MachineRegisterInfo &MRI = " |
| 508 | "I.getParent()->getParent()->getRegInfo();\n"; |
| 509 | |
| 510 | // Look through the SelectionDAG patterns we found, possibly emitting some. |
| 511 | for (const PatternToMatch &Pat : CGP.ptms()) { |
| 512 | ++NumPatternTotal; |
| 513 | if (auto SkipReason = runOnPattern(Pat, OS)) { |
| 514 | if (WarnOnSkippedPatterns) { |
| 515 | PrintWarning(Pat.getSrcRecord()->getLoc(), |
| 516 | "Skipped pattern: " + SkipReason->Reason); |
| 517 | } |
| 518 | ++NumPatternSkipped; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | OS << " return false;\n}\n"; |
| 523 | } |
| 524 | |
| 525 | //===----------------------------------------------------------------------===// |
| 526 | |
| 527 | namespace llvm { |
| 528 | void EmitGlobalISel(RecordKeeper &RK, raw_ostream &OS) { |
| 529 | GlobalISelEmitter(RK).run(OS); |
| 530 | } |
| 531 | } // End llvm namespace |