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" |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Error.h" |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 39 | #include "llvm/Support/LowLevelTypeImpl.h" |
Pavel Labath | 52a82e2 | 2017-02-21 09:19:41 +0000 | [diff] [blame] | 40 | #include "llvm/Support/ScopedPrinter.h" |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 41 | #include "llvm/TableGen/Error.h" |
| 42 | #include "llvm/TableGen/Record.h" |
| 43 | #include "llvm/TableGen/TableGenBackend.h" |
| 44 | #include <string> |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 45 | #include <numeric> |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 46 | using namespace llvm; |
| 47 | |
| 48 | #define DEBUG_TYPE "gisel-emitter" |
| 49 | |
| 50 | STATISTIC(NumPatternTotal, "Total number of patterns"); |
Daniel Sanders | b41ce2b | 2017-02-20 14:31:27 +0000 | [diff] [blame] | 51 | STATISTIC(NumPatternImported, "Number of patterns imported from SelectionDAG"); |
| 52 | STATISTIC(NumPatternImportsSkipped, "Number of SelectionDAG imports skipped"); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 53 | STATISTIC(NumPatternEmitted, "Number of patterns emitted"); |
| 54 | |
Daniel Sanders | 0848b23 | 2017-03-27 13:15:13 +0000 | [diff] [blame] | 55 | cl::OptionCategory GlobalISelEmitterCat("Options for -gen-global-isel"); |
| 56 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 57 | static cl::opt<bool> WarnOnSkippedPatterns( |
| 58 | "warn-on-skipped-patterns", |
| 59 | cl::desc("Explain why a pattern was skipped for inclusion " |
| 60 | "in the GlobalISel selector"), |
Daniel Sanders | 0848b23 | 2017-03-27 13:15:13 +0000 | [diff] [blame] | 61 | cl::init(false), cl::cat(GlobalISelEmitterCat)); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 62 | |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 63 | namespace { |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 64 | //===- Helper functions ---------------------------------------------------===// |
| 65 | |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 66 | /// This class stands in for LLT wherever we want to tablegen-erate an |
| 67 | /// equivalent at compiler run-time. |
| 68 | class LLTCodeGen { |
| 69 | private: |
| 70 | LLT Ty; |
| 71 | |
| 72 | public: |
| 73 | LLTCodeGen(const LLT &Ty) : Ty(Ty) {} |
| 74 | |
| 75 | void emitCxxConstructorCall(raw_ostream &OS) const { |
| 76 | if (Ty.isScalar()) { |
| 77 | OS << "LLT::scalar(" << Ty.getSizeInBits() << ")"; |
| 78 | return; |
| 79 | } |
| 80 | if (Ty.isVector()) { |
| 81 | OS << "LLT::vector(" << Ty.getNumElements() << ", " << Ty.getSizeInBits() |
| 82 | << ")"; |
| 83 | return; |
| 84 | } |
| 85 | llvm_unreachable("Unhandled LLT"); |
| 86 | } |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 87 | |
| 88 | const LLT &get() const { return Ty; } |
| 89 | }; |
| 90 | |
| 91 | class InstructionMatcher; |
| 92 | class OperandPlaceholder { |
| 93 | private: |
| 94 | enum PlaceholderKind { |
| 95 | OP_MatchReference, |
| 96 | OP_Temporary, |
| 97 | } Kind; |
| 98 | |
| 99 | struct MatchReferenceData { |
| 100 | InstructionMatcher *InsnMatcher; |
| 101 | StringRef InsnVarName; |
| 102 | StringRef SymbolicName; |
| 103 | }; |
| 104 | |
| 105 | struct TemporaryData { |
| 106 | unsigned OpIdx; |
| 107 | }; |
| 108 | |
| 109 | union { |
| 110 | struct MatchReferenceData MatchReference; |
| 111 | struct TemporaryData Temporary; |
| 112 | }; |
| 113 | |
| 114 | OperandPlaceholder(PlaceholderKind Kind) : Kind(Kind) {} |
| 115 | |
| 116 | public: |
| 117 | ~OperandPlaceholder() {} |
| 118 | |
| 119 | static OperandPlaceholder |
| 120 | CreateMatchReference(InstructionMatcher *InsnMatcher, |
| 121 | const StringRef InsnVarName, const StringRef SymbolicName) { |
| 122 | OperandPlaceholder Result(OP_MatchReference); |
| 123 | Result.MatchReference.InsnMatcher = InsnMatcher; |
| 124 | Result.MatchReference.InsnVarName = InsnVarName; |
| 125 | Result.MatchReference.SymbolicName = SymbolicName; |
| 126 | return Result; |
| 127 | } |
| 128 | |
| 129 | static OperandPlaceholder CreateTemporary(unsigned OpIdx) { |
| 130 | OperandPlaceholder Result(OP_Temporary); |
| 131 | Result.Temporary.OpIdx = OpIdx; |
| 132 | return Result; |
| 133 | } |
| 134 | |
| 135 | void emitCxxValueExpr(raw_ostream &OS) const; |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 136 | }; |
| 137 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 138 | /// Convert an MVT to an equivalent LLT if possible, or the invalid LLT() for |
| 139 | /// MVTs that don't map cleanly to an LLT (e.g., iPTR, *any, ...). |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 140 | static Optional<LLTCodeGen> MVTToLLT(MVT::SimpleValueType SVT) { |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 141 | MVT VT(SVT); |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 142 | if (VT.isVector() && VT.getVectorNumElements() != 1) |
| 143 | return LLTCodeGen(LLT::vector(VT.getVectorNumElements(), VT.getScalarSizeInBits())); |
| 144 | if (VT.isInteger() || VT.isFloatingPoint()) |
| 145 | return LLTCodeGen(LLT::scalar(VT.getSizeInBits())); |
| 146 | return None; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | static bool isTrivialOperatorNode(const TreePatternNode *N) { |
| 150 | return !N->isLeaf() && !N->hasAnyPredicate() && !N->getTransformFn(); |
| 151 | } |
| 152 | |
| 153 | //===- Matchers -----------------------------------------------------------===// |
| 154 | |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 155 | class MatchAction; |
| 156 | |
| 157 | /// Generates code to check that a match rule matches. |
| 158 | class RuleMatcher { |
| 159 | /// A list of matchers that all need to succeed for the current rule to match. |
| 160 | /// FIXME: This currently supports a single match position but could be |
| 161 | /// extended to support multiple positions to support div/rem fusion or |
| 162 | /// load-multiple instructions. |
| 163 | std::vector<std::unique_ptr<InstructionMatcher>> Matchers; |
| 164 | |
| 165 | /// A list of actions that need to be taken when all predicates in this rule |
| 166 | /// have succeeded. |
| 167 | std::vector<std::unique_ptr<MatchAction>> Actions; |
| 168 | |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 169 | /// A map of instruction matchers to the local variables created by |
| 170 | /// emitCxxCaptureStmts(). |
| 171 | std::map<const InstructionMatcher *, std::string> InsnVariableNames; |
| 172 | |
| 173 | /// ID for the next instruction variable defined with defineInsnVar() |
| 174 | unsigned NextInsnVarID; |
| 175 | |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 176 | public: |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 177 | RuleMatcher() |
| 178 | : Matchers(), Actions(), InsnVariableNames(), NextInsnVarID(0) {} |
Zachary Turner | b7dbd87 | 2017-03-20 19:56:52 +0000 | [diff] [blame] | 179 | RuleMatcher(RuleMatcher &&Other) = default; |
| 180 | RuleMatcher &operator=(RuleMatcher &&Other) = default; |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 181 | |
| 182 | InstructionMatcher &addInstructionMatcher(); |
| 183 | |
| 184 | template <class Kind, class... Args> Kind &addAction(Args &&... args); |
| 185 | |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 186 | std::string defineInsnVar(raw_ostream &OS, const InstructionMatcher &Matcher, |
| 187 | StringRef Value); |
| 188 | StringRef getInsnVarName(const InstructionMatcher &InsnMatcher) const; |
| 189 | |
| 190 | void emitCxxCaptureStmts(raw_ostream &OS, StringRef Expr); |
| 191 | |
| 192 | void emit(raw_ostream &OS); |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 193 | |
| 194 | /// Compare the priority of this object and B. |
| 195 | /// |
| 196 | /// Returns true if this object is more important than B. |
| 197 | bool isHigherPriorityThan(const RuleMatcher &B) const; |
| 198 | |
| 199 | /// Report the maximum number of temporary operands needed by the rule |
| 200 | /// matcher. |
| 201 | unsigned countTemporaryOperands() const; |
| 202 | }; |
| 203 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 204 | template <class PredicateTy> class PredicateListMatcher { |
| 205 | private: |
| 206 | typedef std::vector<std::unique_ptr<PredicateTy>> PredicateVec; |
| 207 | PredicateVec Predicates; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 208 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 209 | public: |
| 210 | /// Construct a new operand predicate and add it to the matcher. |
| 211 | template <class Kind, class... Args> |
| 212 | Kind &addPredicate(Args&&... args) { |
Ahmed Bougacha | b67a3ce | 2017-01-26 22:07:37 +0000 | [diff] [blame] | 213 | Predicates.emplace_back( |
| 214 | llvm::make_unique<Kind>(std::forward<Args>(args)...)); |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 215 | return *static_cast<Kind *>(Predicates.back().get()); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 216 | } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 217 | |
| 218 | typename PredicateVec::const_iterator predicates_begin() const { return Predicates.begin(); } |
| 219 | typename PredicateVec::const_iterator predicates_end() const { return Predicates.end(); } |
| 220 | iterator_range<typename PredicateVec::const_iterator> predicates() const { |
| 221 | return make_range(predicates_begin(), predicates_end()); |
| 222 | } |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 223 | typename PredicateVec::size_type predicates_size() const { return Predicates.size(); } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 224 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 225 | /// Emit a C++ expression that tests whether all the predicates are met. |
Ahmed Bougacha | b67a3ce | 2017-01-26 22:07:37 +0000 | [diff] [blame] | 226 | template <class... Args> |
Daniel Sanders | f8c804f | 2017-01-28 11:10:42 +0000 | [diff] [blame] | 227 | void emitCxxPredicateListExpr(raw_ostream &OS, Args &&... args) const { |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 228 | if (Predicates.empty()) { |
| 229 | OS << "true"; |
| 230 | return; |
| 231 | } |
| 232 | |
| 233 | StringRef Separator = ""; |
| 234 | for (const auto &Predicate : predicates()) { |
| 235 | OS << Separator << "("; |
Ahmed Bougacha | b67a3ce | 2017-01-26 22:07:37 +0000 | [diff] [blame] | 236 | Predicate->emitCxxPredicateExpr(OS, std::forward<Args>(args)...); |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 237 | OS << ")"; |
Ahmed Bougacha | 905af9f | 2017-02-04 00:47:02 +0000 | [diff] [blame] | 238 | Separator = " &&\n"; |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 239 | } |
| 240 | } |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 241 | }; |
| 242 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 243 | /// Generates code to check a predicate of an operand. |
| 244 | /// |
| 245 | /// Typical predicates include: |
| 246 | /// * Operand is a particular register. |
| 247 | /// * Operand is assigned a particular register bank. |
| 248 | /// * Operand is an MBB. |
| 249 | class OperandPredicateMatcher { |
| 250 | public: |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 251 | /// This enum is used for RTTI and also defines the priority that is given to |
| 252 | /// the predicate when generating the matcher code. Kinds with higher priority |
| 253 | /// must be tested first. |
| 254 | /// |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 255 | /// The relative priority of OPM_LLT, OPM_RegBank, and OPM_MBB do not matter |
| 256 | /// but OPM_Int must have priority over OPM_RegBank since constant integers |
| 257 | /// are represented by a virtual register defined by a G_CONSTANT instruction. |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 258 | enum PredicateKind { |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 259 | OPM_ComplexPattern, |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 260 | OPM_Int, |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 261 | OPM_LLT, |
| 262 | OPM_RegBank, |
| 263 | OPM_MBB, |
| 264 | }; |
| 265 | |
| 266 | protected: |
| 267 | PredicateKind Kind; |
| 268 | |
| 269 | public: |
| 270 | OperandPredicateMatcher(PredicateKind Kind) : Kind(Kind) {} |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 271 | virtual ~OperandPredicateMatcher() {} |
| 272 | |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 273 | PredicateKind getKind() const { return Kind; } |
| 274 | |
Daniel Sanders | e604ef5 | 2017-02-20 15:30:43 +0000 | [diff] [blame] | 275 | /// Emit a C++ expression that checks the predicate for the given operand. |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 276 | virtual void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule, |
Daniel Sanders | e604ef5 | 2017-02-20 15:30:43 +0000 | [diff] [blame] | 277 | StringRef OperandExpr) const = 0; |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 278 | |
| 279 | /// Compare the priority of this object and B. |
| 280 | /// |
| 281 | /// Returns true if this object is more important than B. |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 282 | virtual bool isHigherPriorityThan(const OperandPredicateMatcher &B) const { |
| 283 | return Kind < B.Kind; |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 284 | }; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 285 | |
| 286 | /// Report the maximum number of temporary operands needed by the predicate |
| 287 | /// matcher. |
| 288 | virtual unsigned countTemporaryOperands() const { return 0; } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 289 | }; |
| 290 | |
| 291 | /// Generates code to check that an operand is a particular LLT. |
| 292 | class LLTOperandMatcher : public OperandPredicateMatcher { |
| 293 | protected: |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 294 | LLTCodeGen Ty; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 295 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 296 | public: |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 297 | LLTOperandMatcher(const LLTCodeGen &Ty) |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 298 | : OperandPredicateMatcher(OPM_LLT), Ty(Ty) {} |
| 299 | |
| 300 | static bool classof(const OperandPredicateMatcher *P) { |
| 301 | return P->getKind() == OPM_LLT; |
| 302 | } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 303 | |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 304 | void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule, |
Daniel Sanders | e604ef5 | 2017-02-20 15:30:43 +0000 | [diff] [blame] | 305 | StringRef OperandExpr) const override { |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 306 | OS << "MRI.getType(" << OperandExpr << ".getReg()) == ("; |
| 307 | Ty.emitCxxConstructorCall(OS); |
| 308 | OS << ")"; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 309 | } |
| 310 | }; |
| 311 | |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 312 | /// Generates code to check that an operand is a particular target constant. |
| 313 | class ComplexPatternOperandMatcher : public OperandPredicateMatcher { |
| 314 | protected: |
| 315 | const Record &TheDef; |
| 316 | /// The index of the first temporary operand to allocate to this |
| 317 | /// ComplexPattern. |
| 318 | unsigned BaseTemporaryID; |
| 319 | |
| 320 | unsigned getNumOperands() const { |
| 321 | return TheDef.getValueAsDag("Operands")->getNumArgs(); |
| 322 | } |
| 323 | |
| 324 | public: |
| 325 | ComplexPatternOperandMatcher(const Record &TheDef, unsigned BaseTemporaryID) |
| 326 | : OperandPredicateMatcher(OPM_ComplexPattern), TheDef(TheDef), |
| 327 | BaseTemporaryID(BaseTemporaryID) {} |
| 328 | |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 329 | void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule, |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 330 | StringRef OperandExpr) const override { |
| 331 | OS << TheDef.getValueAsString("MatcherFn") << "(" << OperandExpr; |
| 332 | for (unsigned I = 0; I < getNumOperands(); ++I) { |
| 333 | OS << ", "; |
| 334 | OperandPlaceholder::CreateTemporary(BaseTemporaryID + I) |
| 335 | .emitCxxValueExpr(OS); |
| 336 | } |
| 337 | OS << ")"; |
| 338 | } |
| 339 | |
| 340 | unsigned countTemporaryOperands() const override { |
| 341 | return getNumOperands(); |
| 342 | } |
| 343 | }; |
| 344 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 345 | /// Generates code to check that an operand is in a particular register bank. |
| 346 | class RegisterBankOperandMatcher : public OperandPredicateMatcher { |
| 347 | protected: |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 348 | const CodeGenRegisterClass &RC; |
| 349 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 350 | public: |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 351 | RegisterBankOperandMatcher(const CodeGenRegisterClass &RC) |
| 352 | : OperandPredicateMatcher(OPM_RegBank), RC(RC) {} |
| 353 | |
| 354 | static bool classof(const OperandPredicateMatcher *P) { |
| 355 | return P->getKind() == OPM_RegBank; |
| 356 | } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 357 | |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 358 | void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule, |
Daniel Sanders | e604ef5 | 2017-02-20 15:30:43 +0000 | [diff] [blame] | 359 | StringRef OperandExpr) const override { |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 360 | OS << "(&RBI.getRegBankFromRegClass(" << RC.getQualifiedName() |
Daniel Sanders | e604ef5 | 2017-02-20 15:30:43 +0000 | [diff] [blame] | 361 | << "RegClass) == RBI.getRegBank(" << OperandExpr |
| 362 | << ".getReg(), MRI, TRI))"; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 363 | } |
| 364 | }; |
| 365 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 366 | /// Generates code to check that an operand is a basic block. |
| 367 | class MBBOperandMatcher : public OperandPredicateMatcher { |
| 368 | public: |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 369 | MBBOperandMatcher() : OperandPredicateMatcher(OPM_MBB) {} |
| 370 | |
| 371 | static bool classof(const OperandPredicateMatcher *P) { |
| 372 | return P->getKind() == OPM_MBB; |
| 373 | } |
| 374 | |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 375 | void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule, |
Daniel Sanders | e604ef5 | 2017-02-20 15:30:43 +0000 | [diff] [blame] | 376 | StringRef OperandExpr) const override { |
| 377 | OS << OperandExpr << ".isMBB()"; |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 378 | } |
| 379 | }; |
| 380 | |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 381 | /// Generates code to check that an operand is a particular int. |
| 382 | class IntOperandMatcher : public OperandPredicateMatcher { |
| 383 | protected: |
| 384 | int64_t Value; |
| 385 | |
| 386 | public: |
| 387 | IntOperandMatcher(int64_t Value) |
| 388 | : OperandPredicateMatcher(OPM_Int), Value(Value) {} |
| 389 | |
| 390 | static bool classof(const OperandPredicateMatcher *P) { |
| 391 | return P->getKind() == OPM_Int; |
| 392 | } |
| 393 | |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 394 | void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule, |
Simon Pilgrim | d030291 | 2017-02-24 17:20:27 +0000 | [diff] [blame] | 395 | StringRef OperandExpr) const override { |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 396 | OS << "isOperandImmEqual(" << OperandExpr << ", " << Value << ", MRI)"; |
| 397 | } |
| 398 | }; |
| 399 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 400 | /// Generates code to check that a set of predicates match for a particular |
| 401 | /// operand. |
| 402 | class OperandMatcher : public PredicateListMatcher<OperandPredicateMatcher> { |
| 403 | protected: |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 404 | InstructionMatcher &Insn; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 405 | unsigned OpIdx; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 406 | std::string SymbolicName; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 407 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 408 | public: |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 409 | OperandMatcher(InstructionMatcher &Insn, unsigned OpIdx, |
| 410 | const std::string &SymbolicName) |
| 411 | : Insn(Insn), OpIdx(OpIdx), SymbolicName(SymbolicName) {} |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 412 | |
| 413 | bool hasSymbolicName() const { return !SymbolicName.empty(); } |
| 414 | const StringRef getSymbolicName() const { return SymbolicName; } |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame^] | 415 | void setSymbolicName(StringRef Name) { |
| 416 | assert(SymbolicName.empty() && "Operand already has a symbolic name"); |
| 417 | SymbolicName = Name; |
| 418 | } |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 419 | unsigned getOperandIndex() const { return OpIdx; } |
| 420 | |
| 421 | std::string getOperandExpr(const StringRef InsnVarName) const { |
Pavel Labath | 52a82e2 | 2017-02-21 09:19:41 +0000 | [diff] [blame] | 422 | return (InsnVarName + ".getOperand(" + llvm::to_string(OpIdx) + ")").str(); |
Daniel Sanders | e604ef5 | 2017-02-20 15:30:43 +0000 | [diff] [blame] | 423 | } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 424 | |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 425 | InstructionMatcher &getInstructionMatcher() const { return Insn; } |
| 426 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 427 | /// Emit a C++ expression that tests whether the instruction named in |
| 428 | /// InsnVarName matches all the predicate and all the operands. |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 429 | void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule, |
| 430 | const StringRef InsnVarName) const { |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 431 | OS << "(/* "; |
| 432 | if (SymbolicName.empty()) |
| 433 | OS << "Operand " << OpIdx; |
| 434 | else |
| 435 | OS << SymbolicName; |
| 436 | OS << " */ "; |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 437 | emitCxxPredicateListExpr(OS, Rule, getOperandExpr(InsnVarName)); |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 438 | OS << ")"; |
| 439 | } |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 440 | |
| 441 | /// Compare the priority of this object and B. |
| 442 | /// |
| 443 | /// Returns true if this object is more important than B. |
| 444 | bool isHigherPriorityThan(const OperandMatcher &B) const { |
| 445 | // Operand matchers involving more predicates have higher priority. |
| 446 | if (predicates_size() > B.predicates_size()) |
| 447 | return true; |
| 448 | if (predicates_size() < B.predicates_size()) |
| 449 | return false; |
| 450 | |
| 451 | // This assumes that predicates are added in a consistent order. |
| 452 | for (const auto &Predicate : zip(predicates(), B.predicates())) { |
| 453 | if (std::get<0>(Predicate)->isHigherPriorityThan(*std::get<1>(Predicate))) |
| 454 | return true; |
| 455 | if (std::get<1>(Predicate)->isHigherPriorityThan(*std::get<0>(Predicate))) |
| 456 | return false; |
| 457 | } |
| 458 | |
| 459 | return false; |
| 460 | }; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 461 | |
| 462 | /// Report the maximum number of temporary operands needed by the operand |
| 463 | /// matcher. |
| 464 | unsigned countTemporaryOperands() const { |
| 465 | return std::accumulate( |
| 466 | predicates().begin(), predicates().end(), 0, |
| 467 | [](unsigned A, |
| 468 | const std::unique_ptr<OperandPredicateMatcher> &Predicate) { |
| 469 | return A + Predicate->countTemporaryOperands(); |
| 470 | }); |
| 471 | } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 472 | }; |
| 473 | |
| 474 | /// Generates code to check a predicate on an instruction. |
| 475 | /// |
| 476 | /// Typical predicates include: |
| 477 | /// * The opcode of the instruction is a particular value. |
| 478 | /// * The nsw/nuw flag is/isn't set. |
| 479 | class InstructionPredicateMatcher { |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 480 | protected: |
| 481 | /// This enum is used for RTTI and also defines the priority that is given to |
| 482 | /// the predicate when generating the matcher code. Kinds with higher priority |
| 483 | /// must be tested first. |
| 484 | enum PredicateKind { |
| 485 | IPM_Opcode, |
| 486 | }; |
| 487 | |
| 488 | PredicateKind Kind; |
| 489 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 490 | public: |
Daniel Sanders | 8d4d72f | 2017-02-24 14:53:35 +0000 | [diff] [blame] | 491 | InstructionPredicateMatcher(PredicateKind Kind) : Kind(Kind) {} |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 492 | virtual ~InstructionPredicateMatcher() {} |
| 493 | |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 494 | PredicateKind getKind() const { return Kind; } |
| 495 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 496 | /// Emit a C++ expression that tests whether the instruction named in |
| 497 | /// InsnVarName matches the predicate. |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 498 | virtual void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule, |
Ahmed Bougacha | 6a1ac5a | 2017-02-09 02:50:01 +0000 | [diff] [blame] | 499 | StringRef InsnVarName) const = 0; |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 500 | |
| 501 | /// Compare the priority of this object and B. |
| 502 | /// |
| 503 | /// Returns true if this object is more important than B. |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 504 | virtual bool isHigherPriorityThan(const InstructionPredicateMatcher &B) const { |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 505 | return Kind < B.Kind; |
| 506 | }; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 507 | |
| 508 | /// Report the maximum number of temporary operands needed by the predicate |
| 509 | /// matcher. |
| 510 | virtual unsigned countTemporaryOperands() const { return 0; } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 511 | }; |
| 512 | |
| 513 | /// Generates code to check the opcode of an instruction. |
| 514 | class InstructionOpcodeMatcher : public InstructionPredicateMatcher { |
| 515 | protected: |
| 516 | const CodeGenInstruction *I; |
| 517 | |
| 518 | public: |
Daniel Sanders | 8d4d72f | 2017-02-24 14:53:35 +0000 | [diff] [blame] | 519 | InstructionOpcodeMatcher(const CodeGenInstruction *I) |
| 520 | : InstructionPredicateMatcher(IPM_Opcode), I(I) {} |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 521 | |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 522 | static bool classof(const InstructionPredicateMatcher *P) { |
| 523 | return P->getKind() == IPM_Opcode; |
| 524 | } |
| 525 | |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 526 | void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule, |
Ahmed Bougacha | 6a1ac5a | 2017-02-09 02:50:01 +0000 | [diff] [blame] | 527 | StringRef InsnVarName) const override { |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 528 | OS << InsnVarName << ".getOpcode() == " << I->Namespace |
| 529 | << "::" << I->TheDef->getName(); |
| 530 | } |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 531 | |
| 532 | /// Compare the priority of this object and B. |
| 533 | /// |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 534 | /// Returns true if this object is more important than B. |
| 535 | bool isHigherPriorityThan(const InstructionPredicateMatcher &B) const override { |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 536 | if (InstructionPredicateMatcher::isHigherPriorityThan(B)) |
| 537 | return true; |
| 538 | if (B.InstructionPredicateMatcher::isHigherPriorityThan(*this)) |
| 539 | return false; |
| 540 | |
| 541 | // Prioritize opcodes for cosmetic reasons in the generated source. Although |
| 542 | // this is cosmetic at the moment, we may want to drive a similar ordering |
| 543 | // using instruction frequency information to improve compile time. |
| 544 | if (const InstructionOpcodeMatcher *BO = |
| 545 | dyn_cast<InstructionOpcodeMatcher>(&B)) |
| 546 | return I->TheDef->getName() < BO->I->TheDef->getName(); |
| 547 | |
| 548 | return false; |
| 549 | }; |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 550 | }; |
| 551 | |
| 552 | /// Generates code to check that a set of predicates and operands match for a |
| 553 | /// particular instruction. |
| 554 | /// |
| 555 | /// Typical predicates include: |
| 556 | /// * Has a specific opcode. |
| 557 | /// * Has an nsw/nuw flag or doesn't. |
| 558 | class InstructionMatcher |
| 559 | : public PredicateListMatcher<InstructionPredicateMatcher> { |
| 560 | protected: |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 561 | typedef std::vector<OperandMatcher> OperandVec; |
| 562 | |
| 563 | /// The operands to match. All rendered operands must be present even if the |
| 564 | /// condition is always true. |
| 565 | OperandVec Operands; |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 566 | |
| 567 | public: |
| 568 | /// Add an operand to the matcher. |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 569 | OperandMatcher &addOperand(unsigned OpIdx, const std::string &SymbolicName) { |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 570 | Operands.emplace_back(*this, OpIdx, SymbolicName); |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 571 | return Operands.back(); |
| 572 | } |
| 573 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame^] | 574 | OperandMatcher &getOperand(unsigned OpIdx) { |
| 575 | auto I = std::find_if(Operands.begin(), Operands.end(), |
| 576 | [&OpIdx](const OperandMatcher &X) { |
| 577 | return X.getOperandIndex() == OpIdx; |
| 578 | }); |
| 579 | if (I != Operands.end()) |
| 580 | return *I; |
| 581 | llvm_unreachable("Failed to lookup operand"); |
| 582 | } |
| 583 | |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 584 | Optional<const OperandMatcher *> getOptionalOperand(StringRef SymbolicName) const { |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 585 | assert(!SymbolicName.empty() && "Cannot lookup unnamed operand"); |
| 586 | const auto &I = std::find_if(Operands.begin(), Operands.end(), |
| 587 | [&SymbolicName](const OperandMatcher &X) { |
| 588 | return X.getSymbolicName() == SymbolicName; |
| 589 | }); |
| 590 | if (I != Operands.end()) |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 591 | return &*I; |
| 592 | return None; |
| 593 | } |
| 594 | |
| 595 | const OperandMatcher &getOperand(const StringRef SymbolicName) const { |
| 596 | Optional<const OperandMatcher *>OM = getOptionalOperand(SymbolicName); |
| 597 | if (OM.hasValue()) |
| 598 | return *OM.getValue(); |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 599 | llvm_unreachable("Failed to lookup operand"); |
| 600 | } |
| 601 | |
| 602 | unsigned getNumOperands() const { return Operands.size(); } |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame^] | 603 | OperandVec::const_iterator operands_begin() const { return Operands.begin(); } |
| 604 | OperandVec::const_iterator operands_end() const { return Operands.end(); } |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 605 | iterator_range<OperandVec::const_iterator> operands() const { |
| 606 | return make_range(operands_begin(), operands_end()); |
| 607 | } |
| 608 | |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 609 | /// Emit C++ statements to check the shape of the match and capture |
| 610 | /// instructions into local variables. |
| 611 | /// |
| 612 | /// TODO: When nested instruction matching is implemented, this function will |
| 613 | /// descend into the operands and capture variables. |
| 614 | void emitCxxCaptureStmts(raw_ostream &OS, RuleMatcher &Rule, StringRef Expr) { |
| 615 | OS << "if (" << Expr << ".getNumOperands() < " << getNumOperands() << ")\n" |
| 616 | << " return false;\n"; |
| 617 | } |
| 618 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 619 | /// Emit a C++ expression that tests whether the instruction named in |
| 620 | /// InsnVarName matches all the predicates and all the operands. |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 621 | void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule, |
| 622 | StringRef InsnVarName) const { |
| 623 | emitCxxPredicateListExpr(OS, Rule, InsnVarName); |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 624 | for (const auto &Operand : Operands) { |
Ahmed Bougacha | 905af9f | 2017-02-04 00:47:02 +0000 | [diff] [blame] | 625 | OS << " &&\n("; |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 626 | Operand.emitCxxPredicateExpr(OS, Rule, InsnVarName); |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 627 | OS << ")"; |
| 628 | } |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 629 | } |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 630 | |
| 631 | /// Compare the priority of this object and B. |
| 632 | /// |
| 633 | /// Returns true if this object is more important than B. |
| 634 | bool isHigherPriorityThan(const InstructionMatcher &B) const { |
| 635 | // Instruction matchers involving more operands have higher priority. |
| 636 | if (Operands.size() > B.Operands.size()) |
| 637 | return true; |
| 638 | if (Operands.size() < B.Operands.size()) |
| 639 | return false; |
| 640 | |
| 641 | for (const auto &Predicate : zip(predicates(), B.predicates())) { |
| 642 | if (std::get<0>(Predicate)->isHigherPriorityThan(*std::get<1>(Predicate))) |
| 643 | return true; |
| 644 | if (std::get<1>(Predicate)->isHigherPriorityThan(*std::get<0>(Predicate))) |
| 645 | return false; |
| 646 | } |
| 647 | |
| 648 | for (const auto &Operand : zip(Operands, B.Operands)) { |
| 649 | if (std::get<0>(Operand).isHigherPriorityThan(std::get<1>(Operand))) |
| 650 | return true; |
| 651 | if (std::get<1>(Operand).isHigherPriorityThan(std::get<0>(Operand))) |
| 652 | return false; |
| 653 | } |
| 654 | |
| 655 | return false; |
| 656 | }; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 657 | |
| 658 | /// Report the maximum number of temporary operands needed by the instruction |
| 659 | /// matcher. |
| 660 | unsigned countTemporaryOperands() const { |
| 661 | return std::accumulate(predicates().begin(), predicates().end(), 0, |
| 662 | [](unsigned A, |
| 663 | const std::unique_ptr<InstructionPredicateMatcher> |
| 664 | &Predicate) { |
| 665 | return A + Predicate->countTemporaryOperands(); |
| 666 | }) + |
| 667 | std::accumulate(Operands.begin(), Operands.end(), 0, |
| 668 | [](unsigned A, const OperandMatcher &Operand) { |
| 669 | return A + Operand.countTemporaryOperands(); |
| 670 | }); |
| 671 | } |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 672 | }; |
| 673 | |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 674 | //===- Actions ------------------------------------------------------------===// |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 675 | void OperandPlaceholder::emitCxxValueExpr(raw_ostream &OS) const { |
| 676 | switch (Kind) { |
| 677 | case OP_MatchReference: |
| 678 | OS << MatchReference.InsnMatcher->getOperand(MatchReference.SymbolicName) |
| 679 | .getOperandExpr(MatchReference.InsnVarName); |
| 680 | break; |
| 681 | case OP_Temporary: |
| 682 | OS << "TempOp" << Temporary.OpIdx; |
| 683 | break; |
| 684 | } |
| 685 | } |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 686 | |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 687 | class OperandRenderer { |
| 688 | public: |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 689 | enum RendererKind { OR_Copy, OR_Register, OR_ComplexPattern }; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 690 | |
| 691 | protected: |
| 692 | RendererKind Kind; |
| 693 | |
| 694 | public: |
| 695 | OperandRenderer(RendererKind Kind) : Kind(Kind) {} |
| 696 | virtual ~OperandRenderer() {} |
| 697 | |
| 698 | RendererKind getKind() const { return Kind; } |
| 699 | |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 700 | virtual void emitCxxRenderStmts(raw_ostream &OS, RuleMatcher &Rule) const = 0; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 701 | }; |
| 702 | |
| 703 | /// A CopyRenderer emits code to copy a single operand from an existing |
| 704 | /// instruction to the one being built. |
| 705 | class CopyRenderer : public OperandRenderer { |
| 706 | protected: |
| 707 | /// The matcher for the instruction that this operand is copied from. |
| 708 | /// This provides the facility for looking up an a operand by it's name so |
| 709 | /// that it can be used as a source for the instruction being built. |
| 710 | const InstructionMatcher &Matched; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 711 | /// The name of the operand. |
| 712 | const StringRef SymbolicName; |
| 713 | |
| 714 | public: |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 715 | CopyRenderer(const InstructionMatcher &Matched, StringRef SymbolicName) |
| 716 | : OperandRenderer(OR_Copy), Matched(Matched), SymbolicName(SymbolicName) { |
| 717 | } |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 718 | |
| 719 | static bool classof(const OperandRenderer *R) { |
| 720 | return R->getKind() == OR_Copy; |
| 721 | } |
| 722 | |
| 723 | const StringRef getSymbolicName() const { return SymbolicName; } |
| 724 | |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 725 | void emitCxxRenderStmts(raw_ostream &OS, RuleMatcher &Rule) const override { |
| 726 | const OperandMatcher &Operand = Matched.getOperand(SymbolicName); |
| 727 | StringRef InsnVarName = |
| 728 | Rule.getInsnVarName(Operand.getInstructionMatcher()); |
| 729 | std::string OperandExpr = Operand.getOperandExpr(InsnVarName); |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 730 | OS << " MIB.add(" << OperandExpr << "/*" << SymbolicName << "*/);\n"; |
| 731 | } |
| 732 | }; |
| 733 | |
| 734 | /// Adds a specific physical register to the instruction being built. |
| 735 | /// This is typically useful for WZR/XZR on AArch64. |
| 736 | class AddRegisterRenderer : public OperandRenderer { |
| 737 | protected: |
| 738 | const Record *RegisterDef; |
| 739 | |
| 740 | public: |
| 741 | AddRegisterRenderer(const Record *RegisterDef) |
| 742 | : OperandRenderer(OR_Register), RegisterDef(RegisterDef) {} |
| 743 | |
| 744 | static bool classof(const OperandRenderer *R) { |
| 745 | return R->getKind() == OR_Register; |
| 746 | } |
| 747 | |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 748 | void emitCxxRenderStmts(raw_ostream &OS, RuleMatcher &Rule) const override { |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 749 | OS << " MIB.addReg(" << RegisterDef->getValueAsString("Namespace") |
| 750 | << "::" << RegisterDef->getName() << ");\n"; |
| 751 | } |
| 752 | }; |
| 753 | |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 754 | class RenderComplexPatternOperand : public OperandRenderer { |
| 755 | private: |
| 756 | const Record &TheDef; |
| 757 | std::vector<OperandPlaceholder> Sources; |
| 758 | |
| 759 | unsigned getNumOperands() const { |
| 760 | return TheDef.getValueAsDag("Operands")->getNumArgs(); |
| 761 | } |
| 762 | |
| 763 | public: |
| 764 | RenderComplexPatternOperand(const Record &TheDef, |
| 765 | const ArrayRef<OperandPlaceholder> Sources) |
| 766 | : OperandRenderer(OR_ComplexPattern), TheDef(TheDef), Sources(Sources) {} |
| 767 | |
| 768 | static bool classof(const OperandRenderer *R) { |
| 769 | return R->getKind() == OR_ComplexPattern; |
| 770 | } |
| 771 | |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 772 | void emitCxxRenderStmts(raw_ostream &OS, RuleMatcher &Rule) const override { |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 773 | assert(Sources.size() == getNumOperands() && "Inconsistent number of operands"); |
| 774 | for (const auto &Source : Sources) { |
| 775 | OS << "MIB.add("; |
| 776 | Source.emitCxxValueExpr(OS); |
| 777 | OS << ");\n"; |
| 778 | } |
| 779 | } |
| 780 | }; |
| 781 | |
Ahmed Bougacha | 56ca3a9 | 2017-02-04 00:47:10 +0000 | [diff] [blame] | 782 | /// An action taken when all Matcher predicates succeeded for a parent rule. |
| 783 | /// |
| 784 | /// Typical actions include: |
| 785 | /// * Changing the opcode of an instruction. |
| 786 | /// * Adding an operand to an instruction. |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 787 | class MatchAction { |
| 788 | public: |
| 789 | virtual ~MatchAction() {} |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 790 | |
| 791 | /// Emit the C++ statements to implement the action. |
| 792 | /// |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 793 | /// \param RecycleVarName If given, it's an instruction to recycle. The |
| 794 | /// requirements on the instruction vary from action to |
| 795 | /// action. |
| 796 | virtual void emitCxxActionStmts(raw_ostream &OS, RuleMatcher &Rule, |
| 797 | StringRef RecycleVarName) const = 0; |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 798 | }; |
| 799 | |
Ahmed Bougacha | 9aa4c10 | 2017-02-04 00:47:08 +0000 | [diff] [blame] | 800 | /// Generates a comment describing the matched rule being acted upon. |
| 801 | class DebugCommentAction : public MatchAction { |
| 802 | private: |
| 803 | const PatternToMatch &P; |
| 804 | |
| 805 | public: |
| 806 | DebugCommentAction(const PatternToMatch &P) : P(P) {} |
| 807 | |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 808 | void emitCxxActionStmts(raw_ostream &OS, RuleMatcher &Rule, |
| 809 | StringRef RecycleVarName) const override { |
| 810 | OS << "// " << *P.getSrcPattern() << " => " << *P.getDstPattern() << "\n"; |
Ahmed Bougacha | 9aa4c10 | 2017-02-04 00:47:08 +0000 | [diff] [blame] | 811 | } |
| 812 | }; |
| 813 | |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 814 | /// Generates code to build an instruction or mutate an existing instruction |
| 815 | /// into the desired instruction when this is possible. |
| 816 | class BuildMIAction : public MatchAction { |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 817 | private: |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 818 | const CodeGenInstruction *I; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 819 | const InstructionMatcher &Matched; |
| 820 | std::vector<std::unique_ptr<OperandRenderer>> OperandRenderers; |
| 821 | |
| 822 | /// True if the instruction can be built solely by mutating the opcode. |
| 823 | bool canMutate() const { |
| 824 | for (const auto &Renderer : enumerate(OperandRenderers)) { |
Zachary Turner | 309a088 | 2017-03-13 16:24:10 +0000 | [diff] [blame] | 825 | if (const auto *Copy = dyn_cast<CopyRenderer>(&*Renderer.value())) { |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 826 | if (Matched.getOperand(Copy->getSymbolicName()).getOperandIndex() != |
Zachary Turner | 309a088 | 2017-03-13 16:24:10 +0000 | [diff] [blame] | 827 | Renderer.index()) |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 828 | return false; |
| 829 | } else |
| 830 | return false; |
| 831 | } |
| 832 | |
| 833 | return true; |
| 834 | } |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 835 | |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 836 | public: |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 837 | BuildMIAction(const CodeGenInstruction *I, const InstructionMatcher &Matched) |
| 838 | : I(I), Matched(Matched) {} |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 839 | |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 840 | template <class Kind, class... Args> |
| 841 | Kind &addRenderer(Args&&... args) { |
| 842 | OperandRenderers.emplace_back( |
| 843 | llvm::make_unique<Kind>(std::forward<Args>(args)...)); |
| 844 | return *static_cast<Kind *>(OperandRenderers.back().get()); |
| 845 | } |
| 846 | |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 847 | void emitCxxActionStmts(raw_ostream &OS, RuleMatcher &Rule, |
| 848 | StringRef RecycleVarName) const override { |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 849 | if (canMutate()) { |
Tim Northover | 4340d64 | 2017-03-20 21:58:23 +0000 | [diff] [blame] | 850 | OS << " " << RecycleVarName << ".setDesc(TII.get(" << I->Namespace |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 851 | << "::" << I->TheDef->getName() << "));\n"; |
Tim Northover | 4340d64 | 2017-03-20 21:58:23 +0000 | [diff] [blame] | 852 | |
| 853 | if (!I->ImplicitDefs.empty() || !I->ImplicitUses.empty()) { |
| 854 | OS << " auto MIB = MachineInstrBuilder(MF, &" << RecycleVarName |
| 855 | << ");\n"; |
| 856 | |
| 857 | for (auto Def : I->ImplicitDefs) { |
| 858 | auto Namespace = Def->getValueAsString("Namespace"); |
| 859 | OS << " MIB.addDef(" << Namespace << "::" << Def->getName() |
| 860 | << ", RegState::Implicit);\n"; |
| 861 | } |
| 862 | for (auto Use : I->ImplicitUses) { |
| 863 | auto Namespace = Use->getValueAsString("Namespace"); |
| 864 | OS << " MIB.addUse(" << Namespace << "::" << Use->getName() |
| 865 | << ", RegState::Implicit);\n"; |
| 866 | } |
| 867 | } |
| 868 | |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 869 | OS << " MachineInstr &NewI = " << RecycleVarName << ";\n"; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 870 | return; |
| 871 | } |
| 872 | |
| 873 | // TODO: Simple permutation looks like it could be almost as common as |
| 874 | // mutation due to commutative operations. |
| 875 | |
| 876 | OS << "MachineInstrBuilder MIB = BuildMI(*I.getParent(), I, " |
| 877 | "I.getDebugLoc(), TII.get(" |
| 878 | << I->Namespace << "::" << I->TheDef->getName() << "));\n"; |
| 879 | for (const auto &Renderer : OperandRenderers) |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 880 | Renderer->emitCxxRenderStmts(OS, Rule); |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 881 | OS << " MIB.setMemRefs(I.memoperands_begin(), I.memoperands_end());\n"; |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 882 | OS << " " << RecycleVarName << ".eraseFromParent();\n"; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 883 | OS << " MachineInstr &NewI = *MIB;\n"; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 884 | } |
| 885 | }; |
| 886 | |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 887 | InstructionMatcher &RuleMatcher::addInstructionMatcher() { |
| 888 | Matchers.emplace_back(new InstructionMatcher()); |
| 889 | return *Matchers.back(); |
| 890 | } |
Ahmed Bougacha | 56ca3a9 | 2017-02-04 00:47:10 +0000 | [diff] [blame] | 891 | |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 892 | template <class Kind, class... Args> |
| 893 | Kind &RuleMatcher::addAction(Args &&... args) { |
| 894 | Actions.emplace_back(llvm::make_unique<Kind>(std::forward<Args>(args)...)); |
| 895 | return *static_cast<Kind *>(Actions.back().get()); |
| 896 | } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 897 | |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 898 | std::string RuleMatcher::defineInsnVar(raw_ostream &OS, |
| 899 | const InstructionMatcher &Matcher, |
| 900 | StringRef Value) { |
| 901 | std::string InsnVarName = "MI" + llvm::to_string(NextInsnVarID++); |
| 902 | OS << "MachineInstr &" << InsnVarName << " = " << Value << ";\n"; |
| 903 | InsnVariableNames[&Matcher] = InsnVarName; |
| 904 | return InsnVarName; |
| 905 | } |
| 906 | |
| 907 | StringRef RuleMatcher::getInsnVarName(const InstructionMatcher &InsnMatcher) const { |
| 908 | const auto &I = InsnVariableNames.find(&InsnMatcher); |
| 909 | if (I != InsnVariableNames.end()) |
| 910 | return I->second; |
| 911 | llvm_unreachable("Matched Insn was not captured in a local variable"); |
| 912 | } |
| 913 | |
| 914 | /// Emit C++ statements to check the shape of the match and capture |
| 915 | /// instructions into local variables. |
| 916 | void RuleMatcher::emitCxxCaptureStmts(raw_ostream &OS, StringRef Expr) { |
| 917 | assert(Matchers.size() == 1 && "Cannot handle multi-root matchers yet"); |
| 918 | std::string InsnVarName = defineInsnVar(OS, *Matchers.front(), Expr); |
| 919 | Matchers.front()->emitCxxCaptureStmts(OS, *this, InsnVarName); |
| 920 | } |
| 921 | |
| 922 | void RuleMatcher::emit(raw_ostream &OS) { |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 923 | if (Matchers.empty()) |
| 924 | llvm_unreachable("Unexpected empty matcher!"); |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 925 | |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 926 | // The representation supports rules that require multiple roots such as: |
| 927 | // %ptr(p0) = ... |
| 928 | // %elt0(s32) = G_LOAD %ptr |
| 929 | // %1(p0) = G_ADD %ptr, 4 |
| 930 | // %elt1(s32) = G_LOAD p0 %1 |
| 931 | // which could be usefully folded into: |
| 932 | // %ptr(p0) = ... |
| 933 | // %elt0(s32), %elt1(s32) = TGT_LOAD_PAIR %ptr |
| 934 | // on some targets but we don't need to make use of that yet. |
| 935 | assert(Matchers.size() == 1 && "Cannot handle multi-root matchers yet"); |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 936 | OS << "if ([&]() {\n"; |
| 937 | |
| 938 | emitCxxCaptureStmts(OS, "I"); |
| 939 | |
| 940 | OS << " if ("; |
| 941 | Matchers.front()->emitCxxPredicateExpr(OS, *this, |
| 942 | getInsnVarName(*Matchers.front())); |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 943 | OS << ") {\n"; |
| 944 | |
| 945 | for (const auto &MA : Actions) { |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 946 | MA->emitCxxActionStmts(OS, *this, "I"); |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 947 | } |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 948 | |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 949 | OS << " constrainSelectedInstRegOperands(NewI, TII, TRI, RBI);\n"; |
| 950 | OS << " return true;\n"; |
| 951 | OS << " }\n"; |
| 952 | OS << " return false;\n"; |
| 953 | OS << " }()) { return true; }\n\n"; |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 954 | } |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 955 | |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 956 | bool RuleMatcher::isHigherPriorityThan(const RuleMatcher &B) const { |
| 957 | // Rules involving more match roots have higher priority. |
| 958 | if (Matchers.size() > B.Matchers.size()) |
| 959 | return true; |
| 960 | if (Matchers.size() < B.Matchers.size()) |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 961 | return false; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 962 | |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 963 | for (const auto &Matcher : zip(Matchers, B.Matchers)) { |
| 964 | if (std::get<0>(Matcher)->isHigherPriorityThan(*std::get<1>(Matcher))) |
| 965 | return true; |
| 966 | if (std::get<1>(Matcher)->isHigherPriorityThan(*std::get<0>(Matcher))) |
| 967 | return false; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 968 | } |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 969 | |
| 970 | return false; |
Simon Pilgrim | a7d1da8 | 2017-03-15 22:50:47 +0000 | [diff] [blame] | 971 | } |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 972 | |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 973 | unsigned RuleMatcher::countTemporaryOperands() const { |
| 974 | return std::accumulate( |
| 975 | Matchers.begin(), Matchers.end(), 0, |
| 976 | [](unsigned A, const std::unique_ptr<InstructionMatcher> &Matcher) { |
| 977 | return A + Matcher->countTemporaryOperands(); |
| 978 | }); |
| 979 | } |
| 980 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 981 | //===- GlobalISelEmitter class --------------------------------------------===// |
| 982 | |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 983 | class GlobalISelEmitter { |
| 984 | public: |
| 985 | explicit GlobalISelEmitter(RecordKeeper &RK); |
| 986 | void run(raw_ostream &OS); |
| 987 | |
| 988 | private: |
| 989 | const RecordKeeper &RK; |
| 990 | const CodeGenDAGPatterns CGP; |
| 991 | const CodeGenTarget &Target; |
| 992 | |
| 993 | /// Keep track of the equivalence between SDNodes and Instruction. |
| 994 | /// This is defined using 'GINodeEquiv' in the target description. |
| 995 | DenseMap<Record *, const CodeGenInstruction *> NodeEquivs; |
| 996 | |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 997 | /// Keep track of the equivalence between ComplexPattern's and |
| 998 | /// GIComplexOperandMatcher. Map entries are specified by subclassing |
| 999 | /// GIComplexPatternEquiv. |
| 1000 | DenseMap<const Record *, const Record *> ComplexPatternEquivs; |
| 1001 | |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 1002 | void gatherNodeEquivs(); |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 1003 | const CodeGenInstruction *findNodeEquiv(Record *N) const; |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 1004 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame^] | 1005 | Expected<bool> importRulePredicates(RuleMatcher &M, |
| 1006 | ArrayRef<Init *> Predicates) const; |
| 1007 | Expected<InstructionMatcher &> |
| 1008 | importSelDAGMatcher(InstructionMatcher &InsnMatcher, |
| 1009 | const TreePatternNode *Src) const; |
| 1010 | Expected<bool> importChildMatcher(InstructionMatcher &InsnMatcher, |
| 1011 | TreePatternNode *SrcChild, unsigned OpIdx, |
| 1012 | unsigned &TempOpIdx) const; |
| 1013 | Expected<BuildMIAction &> |
| 1014 | importInstructionRenderer(RuleMatcher &M, const TreePatternNode *Dst, |
| 1015 | const InstructionMatcher &InsnMatcher) const; |
| 1016 | Expected<bool> importExplicitUseRenderer( |
| 1017 | BuildMIAction &DstMIBuilder, TreePatternNode *DstChild, |
| 1018 | const InstructionMatcher &InsnMatcher, unsigned &TempOpIdx) const; |
| 1019 | Expected<bool> |
| 1020 | importImplicitDefRenderers(BuildMIAction &DstMIBuilder, |
| 1021 | const std::vector<Record *> &ImplicitDefs) const; |
| 1022 | |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 1023 | /// Analyze pattern \p P, returning a matcher for it if possible. |
| 1024 | /// Otherwise, return an Error explaining why we don't support it. |
| 1025 | Expected<RuleMatcher> runOnPattern(const PatternToMatch &P); |
| 1026 | }; |
| 1027 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1028 | void GlobalISelEmitter::gatherNodeEquivs() { |
| 1029 | assert(NodeEquivs.empty()); |
| 1030 | for (Record *Equiv : RK.getAllDerivedDefinitions("GINodeEquiv")) |
| 1031 | NodeEquivs[Equiv->getValueAsDef("Node")] = |
| 1032 | &Target.getInstruction(Equiv->getValueAsDef("I")); |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1033 | |
| 1034 | assert(ComplexPatternEquivs.empty()); |
| 1035 | for (Record *Equiv : RK.getAllDerivedDefinitions("GIComplexPatternEquiv")) { |
| 1036 | Record *SelDAGEquiv = Equiv->getValueAsDef("SelDAGEquivalent"); |
| 1037 | if (!SelDAGEquiv) |
| 1038 | continue; |
| 1039 | ComplexPatternEquivs[SelDAGEquiv] = Equiv; |
| 1040 | } |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 1043 | const CodeGenInstruction *GlobalISelEmitter::findNodeEquiv(Record *N) const { |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1044 | return NodeEquivs.lookup(N); |
| 1045 | } |
| 1046 | |
| 1047 | GlobalISelEmitter::GlobalISelEmitter(RecordKeeper &RK) |
| 1048 | : RK(RK), CGP(RK), Target(CGP.getTargetInfo()) {} |
| 1049 | |
| 1050 | //===- Emitter ------------------------------------------------------------===// |
| 1051 | |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 1052 | /// Helper function to let the emitter report skip reason error messages. |
| 1053 | static Error failedImport(const Twine &Reason) { |
| 1054 | return make_error<StringError>(Reason, inconvertibleErrorCode()); |
| 1055 | } |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1056 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame^] | 1057 | Expected<bool> |
| 1058 | GlobalISelEmitter::importRulePredicates(RuleMatcher &M, |
| 1059 | ArrayRef<Init *> Predicates) const { |
| 1060 | if (!Predicates.empty()) |
| 1061 | return failedImport("Pattern has a predicate"); |
| 1062 | return true; |
| 1063 | } |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1064 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame^] | 1065 | Expected<InstructionMatcher &> |
| 1066 | GlobalISelEmitter::importSelDAGMatcher(InstructionMatcher &InsnMatcher, |
| 1067 | const TreePatternNode *Src) const { |
| 1068 | // Start with the defined operands (i.e., the results of the root operator). |
| 1069 | if (Src->getExtTypes().size() > 1) |
| 1070 | return failedImport("Src pattern has multiple results"); |
| 1071 | |
| 1072 | auto SrcGIOrNull = findNodeEquiv(Src->getOperator()); |
| 1073 | if (!SrcGIOrNull) |
| 1074 | return failedImport("Pattern operator lacks an equivalent Instruction"); |
| 1075 | auto &SrcGI = *SrcGIOrNull; |
| 1076 | |
| 1077 | // The operators look good: match the opcode and mutate it to the new one. |
| 1078 | InsnMatcher.addPredicate<InstructionOpcodeMatcher>(&SrcGI); |
| 1079 | |
| 1080 | unsigned OpIdx = 0; |
| 1081 | for (const EEVT::TypeSet &Ty : Src->getExtTypes()) { |
| 1082 | auto OpTyOrNone = MVTToLLT(Ty.getConcrete()); |
| 1083 | |
| 1084 | if (!OpTyOrNone) |
| 1085 | return failedImport( |
| 1086 | "Result of Src pattern operator has an unsupported type"); |
| 1087 | |
| 1088 | // Results don't have a name unless they are the root node. The caller will |
| 1089 | // set the name if appropriate. |
| 1090 | OperandMatcher &OM = InsnMatcher.addOperand(OpIdx++, ""); |
| 1091 | OM.addPredicate<LLTOperandMatcher>(*OpTyOrNone); |
| 1092 | } |
| 1093 | |
| 1094 | unsigned TempOpIdx = 0; |
| 1095 | // Match the used operands (i.e. the children of the operator). |
| 1096 | for (unsigned i = 0, e = Src->getNumChildren(); i != e; ++i) { |
| 1097 | if (auto Error = importChildMatcher(InsnMatcher, Src->getChild(i), OpIdx++, |
| 1098 | TempOpIdx) |
| 1099 | .takeError()) |
| 1100 | return std::move(Error); |
| 1101 | } |
| 1102 | |
| 1103 | return InsnMatcher; |
| 1104 | } |
| 1105 | |
| 1106 | Expected<bool> |
| 1107 | GlobalISelEmitter::importChildMatcher(InstructionMatcher &InsnMatcher, |
| 1108 | TreePatternNode *SrcChild, unsigned OpIdx, |
| 1109 | unsigned &TempOpIdx) const { |
| 1110 | OperandMatcher &OM = InsnMatcher.addOperand(OpIdx, SrcChild->getName()); |
| 1111 | |
| 1112 | if (SrcChild->hasAnyPredicate()) |
| 1113 | return failedImport("Src pattern child has predicate"); |
| 1114 | |
| 1115 | ArrayRef<EEVT::TypeSet> ChildTypes = SrcChild->getExtTypes(); |
| 1116 | if (ChildTypes.size() != 1) |
| 1117 | return failedImport("Src pattern child has multiple results"); |
| 1118 | |
| 1119 | // Check MBB's before the type check since they are not a known type. |
| 1120 | if (!SrcChild->isLeaf()) { |
| 1121 | if (SrcChild->getOperator()->isSubClassOf("SDNode")) { |
| 1122 | auto &ChildSDNI = CGP.getSDNodeInfo(SrcChild->getOperator()); |
| 1123 | if (ChildSDNI.getSDClassName() == "BasicBlockSDNode") { |
| 1124 | OM.addPredicate<MBBOperandMatcher>(); |
| 1125 | return true; |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | return failedImport("Src child operand is an unsupported type"); |
| 1130 | } |
| 1131 | |
| 1132 | auto OpTyOrNone = MVTToLLT(ChildTypes.front().getConcrete()); |
| 1133 | if (!OpTyOrNone) |
| 1134 | return failedImport("Src operand has an unsupported type"); |
| 1135 | OM.addPredicate<LLTOperandMatcher>(*OpTyOrNone); |
| 1136 | |
| 1137 | // Check for constant immediates. |
| 1138 | if (auto *ChildInt = dyn_cast<IntInit>(SrcChild->getLeafValue())) { |
| 1139 | OM.addPredicate<IntOperandMatcher>(ChildInt->getValue()); |
| 1140 | return true; |
| 1141 | } |
| 1142 | |
| 1143 | // Check for def's like register classes or ComplexPattern's. |
| 1144 | if (auto *ChildDefInit = dyn_cast<DefInit>(SrcChild->getLeafValue())) { |
| 1145 | auto *ChildRec = ChildDefInit->getDef(); |
| 1146 | |
| 1147 | // Check for register classes. |
| 1148 | if (ChildRec->isSubClassOf("RegisterClass")) { |
| 1149 | OM.addPredicate<RegisterBankOperandMatcher>( |
| 1150 | Target.getRegisterClass(ChildRec)); |
| 1151 | return true; |
| 1152 | } |
| 1153 | |
| 1154 | // Check for ComplexPattern's. |
| 1155 | if (ChildRec->isSubClassOf("ComplexPattern")) { |
| 1156 | const auto &ComplexPattern = ComplexPatternEquivs.find(ChildRec); |
| 1157 | if (ComplexPattern == ComplexPatternEquivs.end()) |
| 1158 | return failedImport( |
| 1159 | "SelectionDAG ComplexPattern not mapped to GlobalISel"); |
| 1160 | |
| 1161 | const auto &Predicate = OM.addPredicate<ComplexPatternOperandMatcher>( |
| 1162 | *ComplexPattern->second, TempOpIdx); |
| 1163 | TempOpIdx += Predicate.countTemporaryOperands(); |
| 1164 | return true; |
| 1165 | } |
| 1166 | |
| 1167 | return failedImport( |
| 1168 | "Src pattern child def is an unsupported tablegen class"); |
| 1169 | } |
| 1170 | |
| 1171 | return failedImport("Src pattern child is an unsupported kind"); |
| 1172 | } |
| 1173 | |
| 1174 | Expected<bool> GlobalISelEmitter::importExplicitUseRenderer( |
| 1175 | BuildMIAction &DstMIBuilder, TreePatternNode *DstChild, |
| 1176 | const InstructionMatcher &InsnMatcher, unsigned &TempOpIdx) const { |
| 1177 | // The only non-leaf child we accept is 'bb': it's an operator because |
| 1178 | // BasicBlockSDNode isn't inline, but in MI it's just another operand. |
| 1179 | if (!DstChild->isLeaf()) { |
| 1180 | if (DstChild->getOperator()->isSubClassOf("SDNode")) { |
| 1181 | auto &ChildSDNI = CGP.getSDNodeInfo(DstChild->getOperator()); |
| 1182 | if (ChildSDNI.getSDClassName() == "BasicBlockSDNode") { |
| 1183 | DstMIBuilder.addRenderer<CopyRenderer>(InsnMatcher, |
| 1184 | DstChild->getName()); |
| 1185 | return true; |
| 1186 | } |
| 1187 | } |
| 1188 | return failedImport("Dst pattern child isn't a leaf node or an MBB"); |
| 1189 | } |
| 1190 | |
| 1191 | // Otherwise, we're looking for a bog-standard RegisterClass operand. |
| 1192 | if (DstChild->hasAnyPredicate()) |
| 1193 | return failedImport("Dst pattern child has predicate"); |
| 1194 | |
| 1195 | if (auto *ChildDefInit = dyn_cast<DefInit>(DstChild->getLeafValue())) { |
| 1196 | auto *ChildRec = ChildDefInit->getDef(); |
| 1197 | |
| 1198 | ArrayRef<EEVT::TypeSet> ChildTypes = DstChild->getExtTypes(); |
| 1199 | if (ChildTypes.size() != 1) |
| 1200 | return failedImport("Dst pattern child has multiple results"); |
| 1201 | |
| 1202 | auto OpTyOrNone = MVTToLLT(ChildTypes.front().getConcrete()); |
| 1203 | if (!OpTyOrNone) |
| 1204 | return failedImport("Dst operand has an unsupported type"); |
| 1205 | |
| 1206 | if (ChildRec->isSubClassOf("Register")) { |
| 1207 | DstMIBuilder.addRenderer<AddRegisterRenderer>(ChildRec); |
| 1208 | return true; |
| 1209 | } |
| 1210 | |
| 1211 | if (ChildRec->isSubClassOf("RegisterClass")) { |
| 1212 | DstMIBuilder.addRenderer<CopyRenderer>(InsnMatcher, DstChild->getName()); |
| 1213 | return true; |
| 1214 | } |
| 1215 | |
| 1216 | if (ChildRec->isSubClassOf("ComplexPattern")) { |
| 1217 | const auto &ComplexPattern = ComplexPatternEquivs.find(ChildRec); |
| 1218 | if (ComplexPattern == ComplexPatternEquivs.end()) |
| 1219 | return failedImport( |
| 1220 | "SelectionDAG ComplexPattern not mapped to GlobalISel"); |
| 1221 | |
| 1222 | SmallVector<OperandPlaceholder, 2> RenderedOperands; |
| 1223 | for (unsigned I = 0; |
| 1224 | I < |
| 1225 | InsnMatcher.getOperand(DstChild->getName()).countTemporaryOperands(); |
| 1226 | ++I) { |
| 1227 | RenderedOperands.push_back(OperandPlaceholder::CreateTemporary(I)); |
| 1228 | TempOpIdx++; |
| 1229 | } |
| 1230 | DstMIBuilder.addRenderer<RenderComplexPatternOperand>( |
| 1231 | *ComplexPattern->second, RenderedOperands); |
| 1232 | return true; |
| 1233 | } |
| 1234 | |
| 1235 | return failedImport( |
| 1236 | "Dst pattern child def is an unsupported tablegen class"); |
| 1237 | } |
| 1238 | |
| 1239 | return failedImport("Dst pattern child is an unsupported kind"); |
| 1240 | } |
| 1241 | |
| 1242 | Expected<BuildMIAction &> GlobalISelEmitter::importInstructionRenderer( |
| 1243 | RuleMatcher &M, const TreePatternNode *Dst, |
| 1244 | const InstructionMatcher &InsnMatcher) const { |
| 1245 | Record *DstOp = Dst->getOperator(); |
| 1246 | if (!DstOp->isSubClassOf("Instruction")) |
| 1247 | return failedImport("Pattern operator isn't an instruction"); |
| 1248 | auto &DstI = Target.getInstruction(DstOp); |
| 1249 | |
| 1250 | auto &DstMIBuilder = M.addAction<BuildMIAction>(&DstI, InsnMatcher); |
| 1251 | |
| 1252 | // Render the explicit defs. |
| 1253 | for (unsigned I = 0; I < DstI.Operands.NumDefs; ++I) { |
| 1254 | const auto &DstIOperand = DstI.Operands[I]; |
| 1255 | DstMIBuilder.addRenderer<CopyRenderer>(InsnMatcher, DstIOperand.Name); |
| 1256 | } |
| 1257 | |
| 1258 | // Render the explicit uses. |
| 1259 | unsigned TempOpIdx = 0; |
| 1260 | for (unsigned i = 0, e = Dst->getNumChildren(); i != e; ++i) { |
| 1261 | if (auto Error = importExplicitUseRenderer(DstMIBuilder, Dst->getChild(i), |
| 1262 | InsnMatcher, TempOpIdx) |
| 1263 | .takeError()) |
| 1264 | return std::move(Error); |
| 1265 | } |
| 1266 | |
| 1267 | return DstMIBuilder; |
| 1268 | } |
| 1269 | |
| 1270 | Expected<bool> GlobalISelEmitter::importImplicitDefRenderers( |
| 1271 | BuildMIAction &DstMIBuilder, |
| 1272 | const std::vector<Record *> &ImplicitDefs) const { |
| 1273 | if (!ImplicitDefs.empty()) |
| 1274 | return failedImport("Pattern defines a physical register"); |
| 1275 | return true; |
| 1276 | } |
| 1277 | |
| 1278 | Expected<RuleMatcher> GlobalISelEmitter::runOnPattern(const PatternToMatch &P) { |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1279 | // Keep track of the matchers and actions to emit. |
Ahmed Bougacha | 9aa4c10 | 2017-02-04 00:47:08 +0000 | [diff] [blame] | 1280 | RuleMatcher M; |
| 1281 | M.addAction<DebugCommentAction>(P); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1282 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame^] | 1283 | if (auto Error = |
| 1284 | importRulePredicates(M, P.getPredicates()->getValues()).takeError()) |
| 1285 | return std::move(Error); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1286 | |
| 1287 | // Next, analyze the pattern operators. |
| 1288 | TreePatternNode *Src = P.getSrcPattern(); |
| 1289 | TreePatternNode *Dst = P.getDstPattern(); |
| 1290 | |
| 1291 | // If the root of either pattern isn't a simple operator, ignore it. |
| 1292 | if (!isTrivialOperatorNode(Dst)) |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 1293 | return failedImport("Dst pattern root isn't a trivial operator"); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1294 | if (!isTrivialOperatorNode(Src)) |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 1295 | return failedImport("Src pattern root isn't a trivial operator"); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1296 | |
| 1297 | Record *DstOp = Dst->getOperator(); |
| 1298 | if (!DstOp->isSubClassOf("Instruction")) |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 1299 | return failedImport("Pattern operator isn't an instruction"); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1300 | |
| 1301 | auto &DstI = Target.getInstruction(DstOp); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1302 | if (DstI.Operands.NumDefs != Src->getExtTypes().size()) |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 1303 | return failedImport("Src pattern results and dst MI defs are different"); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1304 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame^] | 1305 | InstructionMatcher &InsnMatcherTemp = M.addInstructionMatcher(); |
| 1306 | auto InsnMatcherOrError = importSelDAGMatcher(InsnMatcherTemp, Src); |
| 1307 | if (auto Error = InsnMatcherOrError.takeError()) |
| 1308 | return std::move(Error); |
| 1309 | InstructionMatcher &InsnMatcher = InsnMatcherOrError.get(); |
| 1310 | |
| 1311 | // The root of the match also has constraints on the register bank so that it |
| 1312 | // matches the result instruction. |
| 1313 | unsigned OpIdx = 0; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1314 | for (const EEVT::TypeSet &Ty : Src->getExtTypes()) { |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame^] | 1315 | (void)Ty; |
| 1316 | |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1317 | const auto &DstIOperand = DstI.Operands[OpIdx]; |
| 1318 | Record *DstIOpRec = DstIOperand.Rec; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1319 | if (!DstIOpRec->isSubClassOf("RegisterClass")) |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 1320 | return failedImport("Dst MI def isn't a register class"); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1321 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame^] | 1322 | OperandMatcher &OM = InsnMatcher.getOperand(OpIdx); |
| 1323 | OM.setSymbolicName(DstIOperand.Name); |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1324 | OM.addPredicate<RegisterBankOperandMatcher>( |
| 1325 | Target.getRegisterClass(DstIOpRec)); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1326 | ++OpIdx; |
| 1327 | } |
| 1328 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame^] | 1329 | auto DstMIBuilderOrError = importInstructionRenderer(M, Dst, InsnMatcher); |
| 1330 | if (auto Error = DstMIBuilderOrError.takeError()) |
| 1331 | return std::move(Error); |
| 1332 | BuildMIAction &DstMIBuilder = DstMIBuilderOrError.get(); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1333 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame^] | 1334 | // Render the implicit defs. |
| 1335 | // These are only added to the root of the result. |
| 1336 | if (auto Error = |
| 1337 | importImplicitDefRenderers(DstMIBuilder, P.getDstRegs()).takeError()) |
| 1338 | return std::move(Error); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1339 | |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 1340 | // We're done with this pattern! It's eligible for GISel emission; return it. |
Daniel Sanders | b41ce2b | 2017-02-20 14:31:27 +0000 | [diff] [blame] | 1341 | ++NumPatternImported; |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 1342 | return std::move(M); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
| 1345 | void GlobalISelEmitter::run(raw_ostream &OS) { |
| 1346 | // Track the GINodeEquiv definitions. |
| 1347 | gatherNodeEquivs(); |
| 1348 | |
| 1349 | emitSourceFileHeader(("Global Instruction Selector for the " + |
| 1350 | Target.getName() + " target").str(), OS); |
Daniel Sanders | b41ce2b | 2017-02-20 14:31:27 +0000 | [diff] [blame] | 1351 | std::vector<RuleMatcher> Rules; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1352 | // Look through the SelectionDAG patterns we found, possibly emitting some. |
| 1353 | for (const PatternToMatch &Pat : CGP.ptms()) { |
| 1354 | ++NumPatternTotal; |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 1355 | auto MatcherOrErr = runOnPattern(Pat); |
| 1356 | |
| 1357 | // The pattern analysis can fail, indicating an unsupported pattern. |
| 1358 | // Report that if we've been asked to do so. |
| 1359 | if (auto Err = MatcherOrErr.takeError()) { |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1360 | if (WarnOnSkippedPatterns) { |
| 1361 | PrintWarning(Pat.getSrcRecord()->getLoc(), |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 1362 | "Skipped pattern: " + toString(std::move(Err))); |
| 1363 | } else { |
| 1364 | consumeError(std::move(Err)); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1365 | } |
Daniel Sanders | b41ce2b | 2017-02-20 14:31:27 +0000 | [diff] [blame] | 1366 | ++NumPatternImportsSkipped; |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 1367 | continue; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1368 | } |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 1369 | |
Daniel Sanders | b41ce2b | 2017-02-20 14:31:27 +0000 | [diff] [blame] | 1370 | Rules.push_back(std::move(MatcherOrErr.get())); |
| 1371 | } |
| 1372 | |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1373 | std::stable_sort(Rules.begin(), Rules.end(), |
| 1374 | [&](const RuleMatcher &A, const RuleMatcher &B) { |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1375 | if (A.isHigherPriorityThan(B)) { |
| 1376 | assert(!B.isHigherPriorityThan(A) && "Cannot be more important " |
| 1377 | "and less important at " |
| 1378 | "the same time"); |
| 1379 | return true; |
| 1380 | } |
| 1381 | return false; |
| 1382 | }); |
| 1383 | |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1384 | unsigned MaxTemporaries = 0; |
| 1385 | for (const auto &Rule : Rules) |
| 1386 | MaxTemporaries = std::max(MaxTemporaries, Rule.countTemporaryOperands()); |
| 1387 | |
| 1388 | OS << "#ifdef GET_GLOBALISEL_TEMPORARIES_DECL\n"; |
| 1389 | for (unsigned I = 0; I < MaxTemporaries; ++I) |
| 1390 | OS << " mutable MachineOperand TempOp" << I << ";\n"; |
| 1391 | OS << "#endif // ifdef GET_GLOBALISEL_TEMPORARIES_DECL\n\n"; |
| 1392 | |
| 1393 | OS << "#ifdef GET_GLOBALISEL_TEMPORARIES_INIT\n"; |
| 1394 | for (unsigned I = 0; I < MaxTemporaries; ++I) |
| 1395 | OS << ", TempOp" << I << "(MachineOperand::CreatePlaceholder())\n"; |
| 1396 | OS << "#endif // ifdef GET_GLOBALISEL_TEMPORARIES_INIT\n\n"; |
| 1397 | |
| 1398 | OS << "#ifdef GET_GLOBALISEL_IMPL\n" |
| 1399 | << "bool " << Target.getName() |
| 1400 | << "InstructionSelector::selectImpl(MachineInstr &I) const {\n" |
| 1401 | << " MachineFunction &MF = *I.getParent()->getParent();\n" |
| 1402 | << " const MachineRegisterInfo &MRI = MF.getRegInfo();\n"; |
| 1403 | |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 1404 | for (auto &Rule : Rules) { |
Daniel Sanders | b41ce2b | 2017-02-20 14:31:27 +0000 | [diff] [blame] | 1405 | Rule.emit(OS); |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 1406 | ++NumPatternEmitted; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1409 | OS << " return false;\n" |
| 1410 | << "}\n" |
| 1411 | << "#endif // ifdef GET_GLOBALISEL_IMPL\n"; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 1414 | } // end anonymous namespace |
| 1415 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1416 | //===----------------------------------------------------------------------===// |
| 1417 | |
| 1418 | namespace llvm { |
| 1419 | void EmitGlobalISel(RecordKeeper &RK, raw_ostream &OS) { |
| 1420 | GlobalISelEmitter(RK).run(OS); |
| 1421 | } |
| 1422 | } // End llvm namespace |