Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1 | //===- GlobalISelEmitter.cpp - Generate an instruction selector -----------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | /// \file |
| 10 | /// This tablegen backend emits code for use by the GlobalISel instruction |
| 11 | /// selector. See include/llvm/CodeGen/TargetGlobalISel.td. |
| 12 | /// |
| 13 | /// This file analyzes the patterns recognized by the SelectionDAGISel tablegen |
| 14 | /// backend, filters out the ones that are unsupported, maps |
| 15 | /// SelectionDAG-specific constructs to their GlobalISel counterpart |
| 16 | /// (when applicable: MVT to LLT; SDNode to generic Instruction). |
| 17 | /// |
| 18 | /// Not all patterns are supported: pass the tablegen invocation |
| 19 | /// "-warn-on-skipped-patterns" to emit a warning when a pattern is skipped, |
| 20 | /// as well as why. |
| 21 | /// |
| 22 | /// The generated file defines a single method: |
| 23 | /// bool <Target>InstructionSelector::selectImpl(MachineInstr &I) const; |
| 24 | /// intended to be used in InstructionSelector::select as the first-step |
| 25 | /// selector for the patterns that don't require complex C++. |
| 26 | /// |
| 27 | /// FIXME: We'll probably want to eventually define a base |
| 28 | /// "TargetGenInstructionSelector" class. |
| 29 | /// |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | |
| 32 | #include "CodeGenDAGPatterns.h" |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 33 | #include "SubtargetFeatureInfo.h" |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/Optional.h" |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/SmallSet.h" |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/Statistic.h" |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 37 | #include "llvm/Support/CodeGenCoverage.h" |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 38 | #include "llvm/Support/CommandLine.h" |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Error.h" |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 40 | #include "llvm/Support/LowLevelTypeImpl.h" |
David Blaikie | 13e77db | 2018-03-23 23:58:25 +0000 | [diff] [blame] | 41 | #include "llvm/Support/MachineValueType.h" |
Pavel Labath | 52a82e2 | 2017-02-21 09:19:41 +0000 | [diff] [blame] | 42 | #include "llvm/Support/ScopedPrinter.h" |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 43 | #include "llvm/TableGen/Error.h" |
| 44 | #include "llvm/TableGen/Record.h" |
| 45 | #include "llvm/TableGen/TableGenBackend.h" |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 46 | #include <numeric> |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 47 | #include <string> |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 48 | using namespace llvm; |
| 49 | |
| 50 | #define DEBUG_TYPE "gisel-emitter" |
| 51 | |
| 52 | STATISTIC(NumPatternTotal, "Total number of patterns"); |
Daniel Sanders | b41ce2b | 2017-02-20 14:31:27 +0000 | [diff] [blame] | 53 | STATISTIC(NumPatternImported, "Number of patterns imported from SelectionDAG"); |
| 54 | STATISTIC(NumPatternImportsSkipped, "Number of SelectionDAG imports skipped"); |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 55 | STATISTIC(NumPatternsTested, "Number of patterns executed according to coverage information"); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 56 | STATISTIC(NumPatternEmitted, "Number of patterns emitted"); |
| 57 | |
Daniel Sanders | 0848b23 | 2017-03-27 13:15:13 +0000 | [diff] [blame] | 58 | cl::OptionCategory GlobalISelEmitterCat("Options for -gen-global-isel"); |
| 59 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 60 | static cl::opt<bool> WarnOnSkippedPatterns( |
| 61 | "warn-on-skipped-patterns", |
| 62 | cl::desc("Explain why a pattern was skipped for inclusion " |
| 63 | "in the GlobalISel selector"), |
Daniel Sanders | 0848b23 | 2017-03-27 13:15:13 +0000 | [diff] [blame] | 64 | cl::init(false), cl::cat(GlobalISelEmitterCat)); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 65 | |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 66 | static cl::opt<bool> GenerateCoverage( |
| 67 | "instrument-gisel-coverage", |
| 68 | cl::desc("Generate coverage instrumentation for GlobalISel"), |
| 69 | cl::init(false), cl::cat(GlobalISelEmitterCat)); |
| 70 | |
| 71 | static cl::opt<std::string> UseCoverageFile( |
| 72 | "gisel-coverage-file", cl::init(""), |
| 73 | cl::desc("Specify file to retrieve coverage information from"), |
| 74 | cl::cat(GlobalISelEmitterCat)); |
| 75 | |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 76 | static cl::opt<bool> OptimizeMatchTable( |
| 77 | "optimize-match-table", |
| 78 | cl::desc("Generate an optimized version of the match table"), |
| 79 | cl::init(true), cl::cat(GlobalISelEmitterCat)); |
| 80 | |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 81 | namespace { |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 82 | //===- Helper functions ---------------------------------------------------===// |
| 83 | |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 84 | /// Get the name of the enum value used to number the predicate function. |
| 85 | std::string getEnumNameForPredicate(const TreePredicateFn &Predicate) { |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 86 | if (Predicate.hasGISelPredicateCode()) |
| 87 | return "GIPFP_MI_" + Predicate.getFnName(); |
Simon Pilgrim | 6ecae9f | 2017-10-14 21:27:53 +0000 | [diff] [blame] | 88 | return "GIPFP_" + Predicate.getImmTypeIdentifier().str() + "_" + |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 89 | Predicate.getFnName(); |
| 90 | } |
| 91 | |
| 92 | /// Get the opcode used to check this predicate. |
| 93 | std::string getMatchOpcodeForPredicate(const TreePredicateFn &Predicate) { |
Simon Pilgrim | 6ecae9f | 2017-10-14 21:27:53 +0000 | [diff] [blame] | 94 | return "GIM_Check" + Predicate.getImmTypeIdentifier().str() + "ImmPredicate"; |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 97 | /// This class stands in for LLT wherever we want to tablegen-erate an |
| 98 | /// equivalent at compiler run-time. |
| 99 | class LLTCodeGen { |
| 100 | private: |
| 101 | LLT Ty; |
| 102 | |
| 103 | public: |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 104 | LLTCodeGen() = default; |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 105 | LLTCodeGen(const LLT &Ty) : Ty(Ty) {} |
| 106 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 107 | std::string getCxxEnumValue() const { |
| 108 | std::string Str; |
| 109 | raw_string_ostream OS(Str); |
| 110 | |
| 111 | emitCxxEnumValue(OS); |
| 112 | return OS.str(); |
| 113 | } |
| 114 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 115 | void emitCxxEnumValue(raw_ostream &OS) const { |
| 116 | if (Ty.isScalar()) { |
| 117 | OS << "GILLT_s" << Ty.getSizeInBits(); |
| 118 | return; |
| 119 | } |
| 120 | if (Ty.isVector()) { |
| 121 | OS << "GILLT_v" << Ty.getNumElements() << "s" << Ty.getScalarSizeInBits(); |
| 122 | return; |
| 123 | } |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 124 | if (Ty.isPointer()) { |
| 125 | OS << "GILLT_p" << Ty.getAddressSpace(); |
| 126 | if (Ty.getSizeInBits() > 0) |
| 127 | OS << "s" << Ty.getSizeInBits(); |
| 128 | return; |
| 129 | } |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 130 | llvm_unreachable("Unhandled LLT"); |
| 131 | } |
| 132 | |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 133 | void emitCxxConstructorCall(raw_ostream &OS) const { |
| 134 | if (Ty.isScalar()) { |
| 135 | OS << "LLT::scalar(" << Ty.getSizeInBits() << ")"; |
| 136 | return; |
| 137 | } |
| 138 | if (Ty.isVector()) { |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 139 | OS << "LLT::vector(" << Ty.getNumElements() << ", " |
| 140 | << Ty.getScalarSizeInBits() << ")"; |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 141 | return; |
| 142 | } |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 143 | if (Ty.isPointer() && Ty.getSizeInBits() > 0) { |
| 144 | OS << "LLT::pointer(" << Ty.getAddressSpace() << ", " |
| 145 | << Ty.getSizeInBits() << ")"; |
| 146 | return; |
| 147 | } |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 148 | llvm_unreachable("Unhandled LLT"); |
| 149 | } |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 150 | |
| 151 | const LLT &get() const { return Ty; } |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 152 | |
Mandeep Singh Grang | 1b0e2f2 | 2018-04-06 20:18:05 +0000 | [diff] [blame] | 153 | /// This ordering is used for std::unique() and llvm::sort(). There's no |
Daniel Sanders | 032e7f2 | 2017-08-17 13:18:35 +0000 | [diff] [blame] | 154 | /// particular logic behind the order but either A < B or B < A must be |
| 155 | /// true if A != B. |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 156 | bool operator<(const LLTCodeGen &Other) const { |
Daniel Sanders | 032e7f2 | 2017-08-17 13:18:35 +0000 | [diff] [blame] | 157 | if (Ty.isValid() != Other.Ty.isValid()) |
| 158 | return Ty.isValid() < Other.Ty.isValid(); |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 159 | if (!Ty.isValid()) |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 160 | return false; |
Daniel Sanders | 032e7f2 | 2017-08-17 13:18:35 +0000 | [diff] [blame] | 161 | |
| 162 | if (Ty.isVector() != Other.Ty.isVector()) |
| 163 | return Ty.isVector() < Other.Ty.isVector(); |
| 164 | if (Ty.isScalar() != Other.Ty.isScalar()) |
| 165 | return Ty.isScalar() < Other.Ty.isScalar(); |
| 166 | if (Ty.isPointer() != Other.Ty.isPointer()) |
| 167 | return Ty.isPointer() < Other.Ty.isPointer(); |
| 168 | |
| 169 | if (Ty.isPointer() && Ty.getAddressSpace() != Other.Ty.getAddressSpace()) |
| 170 | return Ty.getAddressSpace() < Other.Ty.getAddressSpace(); |
| 171 | |
| 172 | if (Ty.isVector() && Ty.getNumElements() != Other.Ty.getNumElements()) |
| 173 | return Ty.getNumElements() < Other.Ty.getNumElements(); |
| 174 | |
| 175 | return Ty.getSizeInBits() < Other.Ty.getSizeInBits(); |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 176 | } |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 177 | |
| 178 | bool operator==(const LLTCodeGen &B) const { return Ty == B.Ty; } |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 179 | }; |
| 180 | |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 181 | // Track all types that are used so we can emit the corresponding enum. |
| 182 | std::set<LLTCodeGen> KnownTypes; |
| 183 | |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 184 | class InstructionMatcher; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 185 | /// Convert an MVT to an equivalent LLT if possible, or the invalid LLT() for |
| 186 | /// 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] | 187 | static Optional<LLTCodeGen> MVTToLLT(MVT::SimpleValueType SVT) { |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 188 | MVT VT(SVT); |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 189 | |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 190 | if (VT.isVector() && VT.getVectorNumElements() != 1) |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 191 | return LLTCodeGen( |
| 192 | LLT::vector(VT.getVectorNumElements(), VT.getScalarSizeInBits())); |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 193 | |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 194 | if (VT.isInteger() || VT.isFloatingPoint()) |
| 195 | return LLTCodeGen(LLT::scalar(VT.getSizeInBits())); |
| 196 | return None; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 199 | static std::string explainPredicates(const TreePatternNode *N) { |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 200 | std::string Explanation = ""; |
| 201 | StringRef Separator = ""; |
Nicolai Haehnle | 445b0b6 | 2018-11-30 14:15:13 +0000 | [diff] [blame] | 202 | for (const TreePredicateCall &Call : N->getPredicateCalls()) { |
| 203 | const TreePredicateFn &P = Call.Fn; |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 204 | Explanation += |
| 205 | (Separator + P.getOrigPatFragRecord()->getRecord()->getName()).str(); |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 206 | Separator = ", "; |
| 207 | |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 208 | if (P.isAlwaysTrue()) |
| 209 | Explanation += " always-true"; |
| 210 | if (P.isImmediatePattern()) |
| 211 | Explanation += " immediate"; |
Daniel Sanders | 3f267bf | 2017-10-15 02:06:44 +0000 | [diff] [blame] | 212 | |
| 213 | if (P.isUnindexed()) |
| 214 | Explanation += " unindexed"; |
| 215 | |
| 216 | if (P.isNonExtLoad()) |
| 217 | Explanation += " non-extload"; |
| 218 | if (P.isAnyExtLoad()) |
| 219 | Explanation += " extload"; |
| 220 | if (P.isSignExtLoad()) |
| 221 | Explanation += " sextload"; |
| 222 | if (P.isZeroExtLoad()) |
| 223 | Explanation += " zextload"; |
| 224 | |
| 225 | if (P.isNonTruncStore()) |
| 226 | Explanation += " non-truncstore"; |
| 227 | if (P.isTruncStore()) |
| 228 | Explanation += " truncstore"; |
| 229 | |
| 230 | if (Record *VT = P.getMemoryVT()) |
| 231 | Explanation += (" MemVT=" + VT->getName()).str(); |
| 232 | if (Record *VT = P.getScalarMemoryVT()) |
| 233 | Explanation += (" ScalarVT(MemVT)=" + VT->getName()).str(); |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 234 | |
Matt Arsenault | d00d857 | 2019-07-15 20:59:42 +0000 | [diff] [blame] | 235 | if (ListInit *AddrSpaces = P.getAddressSpaces()) { |
| 236 | raw_string_ostream OS(Explanation); |
| 237 | OS << " AddressSpaces=["; |
| 238 | |
| 239 | StringRef AddrSpaceSeparator; |
| 240 | for (Init *Val : AddrSpaces->getValues()) { |
| 241 | IntInit *IntVal = dyn_cast<IntInit>(Val); |
| 242 | if (!IntVal) |
| 243 | continue; |
| 244 | |
| 245 | OS << AddrSpaceSeparator << IntVal->getValue(); |
| 246 | AddrSpaceSeparator = ", "; |
| 247 | } |
| 248 | |
| 249 | OS << ']'; |
| 250 | } |
| 251 | |
Matt Arsenault | 52c2624 | 2019-07-31 00:14:43 +0000 | [diff] [blame] | 252 | int64_t MinAlign = P.getMinAlignment(); |
| 253 | if (MinAlign > 0) |
| 254 | Explanation += " MinAlign=" + utostr(MinAlign); |
| 255 | |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 256 | if (P.isAtomicOrderingMonotonic()) |
| 257 | Explanation += " monotonic"; |
| 258 | if (P.isAtomicOrderingAcquire()) |
| 259 | Explanation += " acquire"; |
| 260 | if (P.isAtomicOrderingRelease()) |
| 261 | Explanation += " release"; |
| 262 | if (P.isAtomicOrderingAcquireRelease()) |
| 263 | Explanation += " acq_rel"; |
| 264 | if (P.isAtomicOrderingSequentiallyConsistent()) |
| 265 | Explanation += " seq_cst"; |
Daniel Sanders | 0c43b3a | 2017-11-30 21:05:59 +0000 | [diff] [blame] | 266 | if (P.isAtomicOrderingAcquireOrStronger()) |
| 267 | Explanation += " >=acquire"; |
| 268 | if (P.isAtomicOrderingWeakerThanAcquire()) |
| 269 | Explanation += " <acquire"; |
| 270 | if (P.isAtomicOrderingReleaseOrStronger()) |
| 271 | Explanation += " >=release"; |
| 272 | if (P.isAtomicOrderingWeakerThanRelease()) |
| 273 | Explanation += " <release"; |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 274 | } |
| 275 | return Explanation; |
| 276 | } |
| 277 | |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 278 | std::string explainOperator(Record *Operator) { |
| 279 | if (Operator->isSubClassOf("SDNode")) |
Craig Topper | 2b8419a | 2017-05-31 19:01:11 +0000 | [diff] [blame] | 280 | return (" (" + Operator->getValueAsString("Opcode") + ")").str(); |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 281 | |
| 282 | if (Operator->isSubClassOf("Intrinsic")) |
| 283 | return (" (Operator is an Intrinsic, " + Operator->getName() + ")").str(); |
| 284 | |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 285 | if (Operator->isSubClassOf("ComplexPattern")) |
| 286 | return (" (Operator is an unmapped ComplexPattern, " + Operator->getName() + |
| 287 | ")") |
| 288 | .str(); |
| 289 | |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 290 | if (Operator->isSubClassOf("SDNodeXForm")) |
| 291 | return (" (Operator is an unmapped SDNodeXForm, " + Operator->getName() + |
| 292 | ")") |
| 293 | .str(); |
| 294 | |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 295 | return (" (Operator " + Operator->getName() + " not understood)").str(); |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /// Helper function to let the emitter report skip reason error messages. |
| 299 | static Error failedImport(const Twine &Reason) { |
| 300 | return make_error<StringError>(Reason, inconvertibleErrorCode()); |
| 301 | } |
| 302 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 303 | static Error isTrivialOperatorNode(const TreePatternNode *N) { |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 304 | std::string Explanation = ""; |
| 305 | std::string Separator = ""; |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 306 | |
| 307 | bool HasUnsupportedPredicate = false; |
Nicolai Haehnle | 445b0b6 | 2018-11-30 14:15:13 +0000 | [diff] [blame] | 308 | for (const TreePredicateCall &Call : N->getPredicateCalls()) { |
| 309 | const TreePredicateFn &Predicate = Call.Fn; |
| 310 | |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 311 | if (Predicate.isAlwaysTrue()) |
| 312 | continue; |
| 313 | |
| 314 | if (Predicate.isImmediatePattern()) |
| 315 | continue; |
| 316 | |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 317 | if (Predicate.isNonExtLoad() || Predicate.isAnyExtLoad() || |
| 318 | Predicate.isSignExtLoad() || Predicate.isZeroExtLoad()) |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 319 | continue; |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 320 | |
Matt Arsenault | 0277249 | 2019-07-15 21:15:20 +0000 | [diff] [blame] | 321 | if (Predicate.isNonTruncStore() || Predicate.isTruncStore()) |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 322 | continue; |
| 323 | |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 324 | if (Predicate.isLoad() && Predicate.getMemoryVT()) |
| 325 | continue; |
| 326 | |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 327 | if (Predicate.isLoad() || Predicate.isStore()) { |
| 328 | if (Predicate.isUnindexed()) |
| 329 | continue; |
| 330 | } |
| 331 | |
Matt Arsenault | d00d857 | 2019-07-15 20:59:42 +0000 | [diff] [blame] | 332 | if (Predicate.isLoad() || Predicate.isStore() || Predicate.isAtomic()) { |
| 333 | const ListInit *AddrSpaces = Predicate.getAddressSpaces(); |
| 334 | if (AddrSpaces && !AddrSpaces->empty()) |
| 335 | continue; |
Matt Arsenault | 52c2624 | 2019-07-31 00:14:43 +0000 | [diff] [blame] | 336 | |
| 337 | if (Predicate.getMinAlignment() > 0) |
| 338 | continue; |
Matt Arsenault | d00d857 | 2019-07-15 20:59:42 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 341 | if (Predicate.isAtomic() && Predicate.getMemoryVT()) |
| 342 | continue; |
| 343 | |
| 344 | if (Predicate.isAtomic() && |
| 345 | (Predicate.isAtomicOrderingMonotonic() || |
| 346 | Predicate.isAtomicOrderingAcquire() || |
| 347 | Predicate.isAtomicOrderingRelease() || |
| 348 | Predicate.isAtomicOrderingAcquireRelease() || |
Daniel Sanders | 0c43b3a | 2017-11-30 21:05:59 +0000 | [diff] [blame] | 349 | Predicate.isAtomicOrderingSequentiallyConsistent() || |
| 350 | Predicate.isAtomicOrderingAcquireOrStronger() || |
| 351 | Predicate.isAtomicOrderingWeakerThanAcquire() || |
| 352 | Predicate.isAtomicOrderingReleaseOrStronger() || |
| 353 | Predicate.isAtomicOrderingWeakerThanRelease())) |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 354 | continue; |
| 355 | |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 356 | if (Predicate.hasGISelPredicateCode()) |
| 357 | continue; |
| 358 | |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 359 | HasUnsupportedPredicate = true; |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 360 | Explanation = Separator + "Has a predicate (" + explainPredicates(N) + ")"; |
| 361 | Separator = ", "; |
Daniel Sanders | 3f267bf | 2017-10-15 02:06:44 +0000 | [diff] [blame] | 362 | Explanation += (Separator + "first-failing:" + |
| 363 | Predicate.getOrigPatFragRecord()->getRecord()->getName()) |
| 364 | .str(); |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 365 | break; |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 368 | if (!HasUnsupportedPredicate) |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 369 | return Error::success(); |
| 370 | |
| 371 | return failedImport(Explanation); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 374 | static Record *getInitValueAsRegClass(Init *V) { |
| 375 | if (DefInit *VDefInit = dyn_cast<DefInit>(V)) { |
| 376 | if (VDefInit->getDef()->isSubClassOf("RegisterOperand")) |
| 377 | return VDefInit->getDef()->getValueAsDef("RegClass"); |
| 378 | if (VDefInit->getDef()->isSubClassOf("RegisterClass")) |
| 379 | return VDefInit->getDef(); |
| 380 | } |
| 381 | return nullptr; |
| 382 | } |
| 383 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 384 | std::string |
| 385 | getNameForFeatureBitset(const std::vector<Record *> &FeatureBitset) { |
| 386 | std::string Name = "GIFBS"; |
| 387 | for (const auto &Feature : FeatureBitset) |
| 388 | Name += ("_" + Feature->getName()).str(); |
| 389 | return Name; |
| 390 | } |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 391 | |
| 392 | //===- MatchTable Helpers -------------------------------------------------===// |
| 393 | |
| 394 | class MatchTable; |
| 395 | |
| 396 | /// A record to be stored in a MatchTable. |
| 397 | /// |
| 398 | /// This class represents any and all output that may be required to emit the |
| 399 | /// MatchTable. Instances are most often configured to represent an opcode or |
| 400 | /// value that will be emitted to the table with some formatting but it can also |
| 401 | /// represent commas, comments, and other formatting instructions. |
| 402 | struct MatchTableRecord { |
| 403 | enum RecordFlagsBits { |
| 404 | MTRF_None = 0x0, |
| 405 | /// Causes EmitStr to be formatted as comment when emitted. |
| 406 | MTRF_Comment = 0x1, |
| 407 | /// Causes the record value to be followed by a comma when emitted. |
| 408 | MTRF_CommaFollows = 0x2, |
| 409 | /// Causes the record value to be followed by a line break when emitted. |
| 410 | MTRF_LineBreakFollows = 0x4, |
| 411 | /// Indicates that the record defines a label and causes an additional |
| 412 | /// comment to be emitted containing the index of the label. |
| 413 | MTRF_Label = 0x8, |
| 414 | /// Causes the record to be emitted as the index of the label specified by |
| 415 | /// LabelID along with a comment indicating where that label is. |
| 416 | MTRF_JumpTarget = 0x10, |
| 417 | /// Causes the formatter to add a level of indentation before emitting the |
| 418 | /// record. |
| 419 | MTRF_Indent = 0x20, |
| 420 | /// Causes the formatter to remove a level of indentation after emitting the |
| 421 | /// record. |
| 422 | MTRF_Outdent = 0x40, |
| 423 | }; |
| 424 | |
| 425 | /// When MTRF_Label or MTRF_JumpTarget is used, indicates a label id to |
| 426 | /// reference or define. |
| 427 | unsigned LabelID; |
| 428 | /// The string to emit. Depending on the MTRF_* flags it may be a comment, a |
| 429 | /// value, a label name. |
| 430 | std::string EmitStr; |
| 431 | |
| 432 | private: |
| 433 | /// The number of MatchTable elements described by this record. Comments are 0 |
| 434 | /// while values are typically 1. Values >1 may occur when we need to emit |
| 435 | /// values that exceed the size of a MatchTable element. |
| 436 | unsigned NumElements; |
| 437 | |
| 438 | public: |
| 439 | /// A bitfield of RecordFlagsBits flags. |
| 440 | unsigned Flags; |
| 441 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 442 | /// The actual run-time value, if known |
| 443 | int64_t RawValue; |
| 444 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 445 | MatchTableRecord(Optional<unsigned> LabelID_, StringRef EmitStr, |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 446 | unsigned NumElements, unsigned Flags, |
| 447 | int64_t RawValue = std::numeric_limits<int64_t>::min()) |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 448 | : LabelID(LabelID_.hasValue() ? LabelID_.getValue() : ~0u), |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 449 | EmitStr(EmitStr), NumElements(NumElements), Flags(Flags), |
| 450 | RawValue(RawValue) { |
| 451 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 452 | assert((!LabelID_.hasValue() || LabelID != ~0u) && |
| 453 | "This value is reserved for non-labels"); |
| 454 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 455 | MatchTableRecord(const MatchTableRecord &Other) = default; |
| 456 | MatchTableRecord(MatchTableRecord &&Other) = default; |
| 457 | |
| 458 | /// Useful if a Match Table Record gets optimized out |
| 459 | void turnIntoComment() { |
| 460 | Flags |= MTRF_Comment; |
| 461 | Flags &= ~MTRF_CommaFollows; |
| 462 | NumElements = 0; |
| 463 | } |
| 464 | |
| 465 | /// For Jump Table generation purposes |
| 466 | bool operator<(const MatchTableRecord &Other) const { |
| 467 | return RawValue < Other.RawValue; |
| 468 | } |
| 469 | int64_t getRawValue() const { return RawValue; } |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 470 | |
| 471 | void emit(raw_ostream &OS, bool LineBreakNextAfterThis, |
| 472 | const MatchTable &Table) const; |
| 473 | unsigned size() const { return NumElements; } |
| 474 | }; |
| 475 | |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 476 | class Matcher; |
| 477 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 478 | /// Holds the contents of a generated MatchTable to enable formatting and the |
| 479 | /// necessary index tracking needed to support GIM_Try. |
| 480 | class MatchTable { |
| 481 | /// An unique identifier for the table. The generated table will be named |
| 482 | /// MatchTable${ID}. |
| 483 | unsigned ID; |
| 484 | /// The records that make up the table. Also includes comments describing the |
| 485 | /// values being emitted and line breaks to format it. |
| 486 | std::vector<MatchTableRecord> Contents; |
| 487 | /// The currently defined labels. |
| 488 | DenseMap<unsigned, unsigned> LabelMap; |
| 489 | /// Tracks the sum of MatchTableRecord::NumElements as the table is built. |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 490 | unsigned CurrentSize = 0; |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 491 | /// A unique identifier for a MatchTable label. |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 492 | unsigned CurrentLabelID = 0; |
Roman Tereshin | beb3931 | 2018-05-02 20:15:11 +0000 | [diff] [blame] | 493 | /// Determines if the table should be instrumented for rule coverage tracking. |
| 494 | bool IsWithCoverage; |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 495 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 496 | public: |
| 497 | static MatchTableRecord LineBreak; |
| 498 | static MatchTableRecord Comment(StringRef Comment) { |
| 499 | return MatchTableRecord(None, Comment, 0, MatchTableRecord::MTRF_Comment); |
| 500 | } |
| 501 | static MatchTableRecord Opcode(StringRef Opcode, int IndentAdjust = 0) { |
| 502 | unsigned ExtraFlags = 0; |
| 503 | if (IndentAdjust > 0) |
| 504 | ExtraFlags |= MatchTableRecord::MTRF_Indent; |
| 505 | if (IndentAdjust < 0) |
| 506 | ExtraFlags |= MatchTableRecord::MTRF_Outdent; |
| 507 | |
| 508 | return MatchTableRecord(None, Opcode, 1, |
| 509 | MatchTableRecord::MTRF_CommaFollows | ExtraFlags); |
| 510 | } |
| 511 | static MatchTableRecord NamedValue(StringRef NamedValue) { |
| 512 | return MatchTableRecord(None, NamedValue, 1, |
| 513 | MatchTableRecord::MTRF_CommaFollows); |
| 514 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 515 | static MatchTableRecord NamedValue(StringRef NamedValue, int64_t RawValue) { |
| 516 | return MatchTableRecord(None, NamedValue, 1, |
| 517 | MatchTableRecord::MTRF_CommaFollows, RawValue); |
| 518 | } |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 519 | static MatchTableRecord NamedValue(StringRef Namespace, |
| 520 | StringRef NamedValue) { |
| 521 | return MatchTableRecord(None, (Namespace + "::" + NamedValue).str(), 1, |
| 522 | MatchTableRecord::MTRF_CommaFollows); |
| 523 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 524 | static MatchTableRecord NamedValue(StringRef Namespace, StringRef NamedValue, |
| 525 | int64_t RawValue) { |
| 526 | return MatchTableRecord(None, (Namespace + "::" + NamedValue).str(), 1, |
| 527 | MatchTableRecord::MTRF_CommaFollows, RawValue); |
| 528 | } |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 529 | static MatchTableRecord IntValue(int64_t IntValue) { |
| 530 | return MatchTableRecord(None, llvm::to_string(IntValue), 1, |
| 531 | MatchTableRecord::MTRF_CommaFollows); |
| 532 | } |
| 533 | static MatchTableRecord Label(unsigned LabelID) { |
| 534 | return MatchTableRecord(LabelID, "Label " + llvm::to_string(LabelID), 0, |
| 535 | MatchTableRecord::MTRF_Label | |
| 536 | MatchTableRecord::MTRF_Comment | |
| 537 | MatchTableRecord::MTRF_LineBreakFollows); |
| 538 | } |
| 539 | static MatchTableRecord JumpTarget(unsigned LabelID) { |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 540 | return MatchTableRecord(LabelID, "Label " + llvm::to_string(LabelID), 1, |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 541 | MatchTableRecord::MTRF_JumpTarget | |
| 542 | MatchTableRecord::MTRF_Comment | |
| 543 | MatchTableRecord::MTRF_CommaFollows); |
| 544 | } |
| 545 | |
Roman Tereshin | beb3931 | 2018-05-02 20:15:11 +0000 | [diff] [blame] | 546 | static MatchTable buildTable(ArrayRef<Matcher *> Rules, bool WithCoverage); |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 547 | |
Roman Tereshin | beb3931 | 2018-05-02 20:15:11 +0000 | [diff] [blame] | 548 | MatchTable(bool WithCoverage, unsigned ID = 0) |
| 549 | : ID(ID), IsWithCoverage(WithCoverage) {} |
| 550 | |
| 551 | bool isWithCoverage() const { return IsWithCoverage; } |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 552 | |
| 553 | void push_back(const MatchTableRecord &Value) { |
| 554 | if (Value.Flags & MatchTableRecord::MTRF_Label) |
| 555 | defineLabel(Value.LabelID); |
| 556 | Contents.push_back(Value); |
| 557 | CurrentSize += Value.size(); |
| 558 | } |
| 559 | |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 560 | unsigned allocateLabelID() { return CurrentLabelID++; } |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 561 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 562 | void defineLabel(unsigned LabelID) { |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 563 | LabelMap.insert(std::make_pair(LabelID, CurrentSize)); |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | unsigned getLabelIndex(unsigned LabelID) const { |
| 567 | const auto I = LabelMap.find(LabelID); |
| 568 | assert(I != LabelMap.end() && "Use of undeclared label"); |
| 569 | return I->second; |
| 570 | } |
| 571 | |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 572 | void emitUse(raw_ostream &OS) const { OS << "MatchTable" << ID; } |
| 573 | |
| 574 | void emitDeclaration(raw_ostream &OS) const { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 575 | unsigned Indentation = 4; |
Daniel Sanders | cbbbfe4 | 2017-07-27 12:47:31 +0000 | [diff] [blame] | 576 | OS << " constexpr static int64_t MatchTable" << ID << "[] = {"; |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 577 | LineBreak.emit(OS, true, *this); |
| 578 | OS << std::string(Indentation, ' '); |
| 579 | |
| 580 | for (auto I = Contents.begin(), E = Contents.end(); I != E; |
| 581 | ++I) { |
| 582 | bool LineBreakIsNext = false; |
| 583 | const auto &NextI = std::next(I); |
| 584 | |
| 585 | if (NextI != E) { |
| 586 | if (NextI->EmitStr == "" && |
| 587 | NextI->Flags == MatchTableRecord::MTRF_LineBreakFollows) |
| 588 | LineBreakIsNext = true; |
| 589 | } |
| 590 | |
| 591 | if (I->Flags & MatchTableRecord::MTRF_Indent) |
| 592 | Indentation += 2; |
| 593 | |
| 594 | I->emit(OS, LineBreakIsNext, *this); |
| 595 | if (I->Flags & MatchTableRecord::MTRF_LineBreakFollows) |
| 596 | OS << std::string(Indentation, ' '); |
| 597 | |
| 598 | if (I->Flags & MatchTableRecord::MTRF_Outdent) |
| 599 | Indentation -= 2; |
| 600 | } |
| 601 | OS << "};\n"; |
| 602 | } |
| 603 | }; |
| 604 | |
| 605 | MatchTableRecord MatchTable::LineBreak = { |
| 606 | None, "" /* Emit String */, 0 /* Elements */, |
| 607 | MatchTableRecord::MTRF_LineBreakFollows}; |
| 608 | |
| 609 | void MatchTableRecord::emit(raw_ostream &OS, bool LineBreakIsNextAfterThis, |
| 610 | const MatchTable &Table) const { |
| 611 | bool UseLineComment = |
Simon Pilgrim | 43fe9af | 2019-11-02 21:01:45 +0000 | [diff] [blame] | 612 | LineBreakIsNextAfterThis || (Flags & MTRF_LineBreakFollows); |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 613 | if (Flags & (MTRF_JumpTarget | MTRF_CommaFollows)) |
| 614 | UseLineComment = false; |
| 615 | |
| 616 | if (Flags & MTRF_Comment) |
| 617 | OS << (UseLineComment ? "// " : "/*"); |
| 618 | |
| 619 | OS << EmitStr; |
| 620 | if (Flags & MTRF_Label) |
| 621 | OS << ": @" << Table.getLabelIndex(LabelID); |
| 622 | |
Simon Pilgrim | 43fe9af | 2019-11-02 21:01:45 +0000 | [diff] [blame] | 623 | if ((Flags & MTRF_Comment) && !UseLineComment) |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 624 | OS << "*/"; |
| 625 | |
| 626 | if (Flags & MTRF_JumpTarget) { |
| 627 | if (Flags & MTRF_Comment) |
| 628 | OS << " "; |
| 629 | OS << Table.getLabelIndex(LabelID); |
| 630 | } |
| 631 | |
| 632 | if (Flags & MTRF_CommaFollows) { |
| 633 | OS << ","; |
| 634 | if (!LineBreakIsNextAfterThis && !(Flags & MTRF_LineBreakFollows)) |
| 635 | OS << " "; |
| 636 | } |
| 637 | |
| 638 | if (Flags & MTRF_LineBreakFollows) |
| 639 | OS << "\n"; |
| 640 | } |
| 641 | |
| 642 | MatchTable &operator<<(MatchTable &Table, const MatchTableRecord &Value) { |
| 643 | Table.push_back(Value); |
| 644 | return Table; |
| 645 | } |
| 646 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 647 | //===- Matchers -----------------------------------------------------------===// |
| 648 | |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 649 | class OperandMatcher; |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 650 | class MatchAction; |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 651 | class PredicateMatcher; |
| 652 | class RuleMatcher; |
| 653 | |
| 654 | class Matcher { |
| 655 | public: |
| 656 | virtual ~Matcher() = default; |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 657 | virtual void optimize() {} |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 658 | virtual void emit(MatchTable &Table) = 0; |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 659 | |
| 660 | virtual bool hasFirstCondition() const = 0; |
| 661 | virtual const PredicateMatcher &getFirstCondition() const = 0; |
| 662 | virtual std::unique_ptr<PredicateMatcher> popFirstCondition() = 0; |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 663 | }; |
| 664 | |
Roman Tereshin | beb3931 | 2018-05-02 20:15:11 +0000 | [diff] [blame] | 665 | MatchTable MatchTable::buildTable(ArrayRef<Matcher *> Rules, |
| 666 | bool WithCoverage) { |
| 667 | MatchTable Table(WithCoverage); |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 668 | for (Matcher *Rule : Rules) |
| 669 | Rule->emit(Table); |
| 670 | |
| 671 | return Table << MatchTable::Opcode("GIM_Reject") << MatchTable::LineBreak; |
| 672 | } |
| 673 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 674 | class GroupMatcher final : public Matcher { |
| 675 | /// Conditions that form a common prefix of all the matchers contained. |
| 676 | SmallVector<std::unique_ptr<PredicateMatcher>, 1> Conditions; |
| 677 | |
| 678 | /// All the nested matchers, sharing a common prefix. |
| 679 | std::vector<Matcher *> Matchers; |
| 680 | |
| 681 | /// An owning collection for any auxiliary matchers created while optimizing |
| 682 | /// nested matchers contained. |
| 683 | std::vector<std::unique_ptr<Matcher>> MatcherStorage; |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 684 | |
| 685 | public: |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 686 | /// Add a matcher to the collection of nested matchers if it meets the |
| 687 | /// requirements, and return true. If it doesn't, do nothing and return false. |
| 688 | /// |
| 689 | /// Expected to preserve its argument, so it could be moved out later on. |
| 690 | bool addMatcher(Matcher &Candidate); |
| 691 | |
| 692 | /// Mark the matcher as fully-built and ensure any invariants expected by both |
| 693 | /// optimize() and emit(...) methods. Generally, both sequences of calls |
| 694 | /// are expected to lead to a sensible result: |
| 695 | /// |
| 696 | /// addMatcher(...)*; finalize(); optimize(); emit(...); and |
| 697 | /// addMatcher(...)*; finalize(); emit(...); |
| 698 | /// |
| 699 | /// or generally |
| 700 | /// |
| 701 | /// addMatcher(...)*; finalize(); { optimize()*; emit(...); }* |
| 702 | /// |
| 703 | /// Multiple calls to optimize() are expected to be handled gracefully, though |
| 704 | /// optimize() is not expected to be idempotent. Multiple calls to finalize() |
| 705 | /// aren't generally supported. emit(...) is expected to be non-mutating and |
| 706 | /// producing the exact same results upon repeated calls. |
| 707 | /// |
| 708 | /// addMatcher() calls after the finalize() call are not supported. |
| 709 | /// |
| 710 | /// finalize() and optimize() are both allowed to mutate the contained |
| 711 | /// matchers, so moving them out after finalize() is not supported. |
| 712 | void finalize(); |
Roman Tereshin | fedae33 | 2018-05-23 02:04:19 +0000 | [diff] [blame] | 713 | void optimize() override; |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 714 | void emit(MatchTable &Table) override; |
Quentin Colombet | 34688b9 | 2017-12-18 21:25:53 +0000 | [diff] [blame] | 715 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 716 | /// Could be used to move out the matchers added previously, unless finalize() |
| 717 | /// has been already called. If any of the matchers are moved out, the group |
| 718 | /// becomes safe to destroy, but not safe to re-use for anything else. |
| 719 | iterator_range<std::vector<Matcher *>::iterator> matchers() { |
| 720 | return make_range(Matchers.begin(), Matchers.end()); |
Quentin Colombet | 34688b9 | 2017-12-18 21:25:53 +0000 | [diff] [blame] | 721 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 722 | size_t size() const { return Matchers.size(); } |
| 723 | bool empty() const { return Matchers.empty(); } |
| 724 | |
| 725 | std::unique_ptr<PredicateMatcher> popFirstCondition() override { |
| 726 | assert(!Conditions.empty() && |
| 727 | "Trying to pop a condition from a condition-less group"); |
| 728 | std::unique_ptr<PredicateMatcher> P = std::move(Conditions.front()); |
| 729 | Conditions.erase(Conditions.begin()); |
| 730 | return P; |
| 731 | } |
| 732 | const PredicateMatcher &getFirstCondition() const override { |
| 733 | assert(!Conditions.empty() && |
| 734 | "Trying to get a condition from a condition-less group"); |
| 735 | return *Conditions.front(); |
| 736 | } |
| 737 | bool hasFirstCondition() const override { return !Conditions.empty(); } |
| 738 | |
| 739 | private: |
| 740 | /// See if a candidate matcher could be added to this group solely by |
| 741 | /// analyzing its first condition. |
| 742 | bool candidateConditionMatches(const PredicateMatcher &Predicate) const; |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 743 | }; |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 744 | |
Roman Tereshin | 0ee082f | 2018-05-22 19:37:59 +0000 | [diff] [blame] | 745 | class SwitchMatcher : public Matcher { |
| 746 | /// All the nested matchers, representing distinct switch-cases. The first |
| 747 | /// conditions (as Matcher::getFirstCondition() reports) of all the nested |
| 748 | /// matchers must share the same type and path to a value they check, in other |
| 749 | /// words, be isIdenticalDownToValue, but have different values they check |
| 750 | /// against. |
| 751 | std::vector<Matcher *> Matchers; |
| 752 | |
| 753 | /// The representative condition, with a type and a path (InsnVarID and OpIdx |
| 754 | /// in most cases) shared by all the matchers contained. |
| 755 | std::unique_ptr<PredicateMatcher> Condition = nullptr; |
| 756 | |
| 757 | /// Temporary set used to check that the case values don't repeat within the |
| 758 | /// same switch. |
| 759 | std::set<MatchTableRecord> Values; |
| 760 | |
| 761 | /// An owning collection for any auxiliary matchers created while optimizing |
| 762 | /// nested matchers contained. |
| 763 | std::vector<std::unique_ptr<Matcher>> MatcherStorage; |
| 764 | |
| 765 | public: |
| 766 | bool addMatcher(Matcher &Candidate); |
| 767 | |
| 768 | void finalize(); |
| 769 | void emit(MatchTable &Table) override; |
| 770 | |
| 771 | iterator_range<std::vector<Matcher *>::iterator> matchers() { |
| 772 | return make_range(Matchers.begin(), Matchers.end()); |
| 773 | } |
| 774 | size_t size() const { return Matchers.size(); } |
| 775 | bool empty() const { return Matchers.empty(); } |
| 776 | |
| 777 | std::unique_ptr<PredicateMatcher> popFirstCondition() override { |
| 778 | // SwitchMatcher doesn't have a common first condition for its cases, as all |
| 779 | // the cases only share a kind of a value (a type and a path to it) they |
| 780 | // match, but deliberately differ in the actual value they match. |
| 781 | llvm_unreachable("Trying to pop a condition from a condition-less group"); |
| 782 | } |
| 783 | const PredicateMatcher &getFirstCondition() const override { |
| 784 | llvm_unreachable("Trying to pop a condition from a condition-less group"); |
| 785 | } |
| 786 | bool hasFirstCondition() const override { return false; } |
| 787 | |
| 788 | private: |
| 789 | /// See if the predicate type has a Switch-implementation for it. |
| 790 | static bool isSupportedPredicateType(const PredicateMatcher &Predicate); |
| 791 | |
| 792 | bool candidateConditionMatches(const PredicateMatcher &Predicate) const; |
| 793 | |
| 794 | /// emit()-helper |
| 795 | static void emitPredicateSpecificOpcodes(const PredicateMatcher &P, |
| 796 | MatchTable &Table); |
| 797 | }; |
| 798 | |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 799 | /// Generates code to check that a match rule matches. |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 800 | class RuleMatcher : public Matcher { |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 801 | public: |
Daniel Sanders | 0846452 | 2018-01-29 21:09:12 +0000 | [diff] [blame] | 802 | using ActionList = std::list<std::unique_ptr<MatchAction>>; |
| 803 | using action_iterator = ActionList::iterator; |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 804 | |
| 805 | protected: |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 806 | /// A list of matchers that all need to succeed for the current rule to match. |
| 807 | /// FIXME: This currently supports a single match position but could be |
| 808 | /// extended to support multiple positions to support div/rem fusion or |
| 809 | /// load-multiple instructions. |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 810 | using MatchersTy = std::vector<std::unique_ptr<InstructionMatcher>> ; |
| 811 | MatchersTy Matchers; |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 812 | |
| 813 | /// A list of actions that need to be taken when all predicates in this rule |
| 814 | /// have succeeded. |
Daniel Sanders | 0846452 | 2018-01-29 21:09:12 +0000 | [diff] [blame] | 815 | ActionList Actions; |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 816 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 817 | using DefinedInsnVariablesMap = std::map<InstructionMatcher *, unsigned>; |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 818 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 819 | /// A map of instruction matchers to the local variables |
Daniel Sanders | 078572b | 2017-08-02 11:03:36 +0000 | [diff] [blame] | 820 | DefinedInsnVariablesMap InsnVariableIDs; |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 821 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 822 | using MutatableInsnSet = SmallPtrSet<InstructionMatcher *, 4>; |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 823 | |
| 824 | // The set of instruction matchers that have not yet been claimed for mutation |
| 825 | // by a BuildMI. |
| 826 | MutatableInsnSet MutatableInsns; |
| 827 | |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 828 | /// A map of named operands defined by the matchers that may be referenced by |
| 829 | /// the renderers. |
| 830 | StringMap<OperandMatcher *> DefinedOperands; |
| 831 | |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 832 | /// A map of anonymous physical register operands defined by the matchers that |
| 833 | /// may be referenced by the renderers. |
| 834 | DenseMap<Record *, OperandMatcher *> PhysRegOperands; |
| 835 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 836 | /// ID for the next instruction variable defined with implicitlyDefineInsnVar() |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 837 | unsigned NextInsnVarID; |
| 838 | |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 839 | /// ID for the next output instruction allocated with allocateOutputInsnID() |
| 840 | unsigned NextOutputInsnID; |
| 841 | |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 842 | /// ID for the next temporary register ID allocated with allocateTempRegID() |
| 843 | unsigned NextTempRegID; |
| 844 | |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 845 | std::vector<Record *> RequiredFeatures; |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 846 | std::vector<std::unique_ptr<PredicateMatcher>> EpilogueMatchers; |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 847 | |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 848 | ArrayRef<SMLoc> SrcLoc; |
| 849 | |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 850 | typedef std::tuple<Record *, unsigned, unsigned> |
| 851 | DefinedComplexPatternSubOperand; |
| 852 | typedef StringMap<DefinedComplexPatternSubOperand> |
| 853 | DefinedComplexPatternSubOperandMap; |
| 854 | /// A map of Symbolic Names to ComplexPattern sub-operands. |
| 855 | DefinedComplexPatternSubOperandMap ComplexSubOperands; |
| 856 | |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 857 | uint64_t RuleID; |
| 858 | static uint64_t NextRuleID; |
| 859 | |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 860 | public: |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 861 | RuleMatcher(ArrayRef<SMLoc> SrcLoc) |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 862 | : Matchers(), Actions(), InsnVariableIDs(), MutatableInsns(), |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 863 | DefinedOperands(), NextInsnVarID(0), NextOutputInsnID(0), |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 864 | NextTempRegID(0), SrcLoc(SrcLoc), ComplexSubOperands(), |
| 865 | RuleID(NextRuleID++) {} |
Zachary Turner | b7dbd87 | 2017-03-20 19:56:52 +0000 | [diff] [blame] | 866 | RuleMatcher(RuleMatcher &&Other) = default; |
| 867 | RuleMatcher &operator=(RuleMatcher &&Other) = default; |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 868 | |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 869 | uint64_t getRuleID() const { return RuleID; } |
| 870 | |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 871 | InstructionMatcher &addInstructionMatcher(StringRef SymbolicName); |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 872 | void addRequiredFeature(Record *Feature); |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 873 | const std::vector<Record *> &getRequiredFeatures() const; |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 874 | |
| 875 | template <class Kind, class... Args> Kind &addAction(Args &&... args); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 876 | template <class Kind, class... Args> |
| 877 | action_iterator insertAction(action_iterator InsertPt, Args &&... args); |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 878 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 879 | /// Define an instruction without emitting any code to do so. |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 880 | unsigned implicitlyDefineInsnVar(InstructionMatcher &Matcher); |
| 881 | |
| 882 | unsigned getInsnVarID(InstructionMatcher &InsnMatcher) const; |
Daniel Sanders | 078572b | 2017-08-02 11:03:36 +0000 | [diff] [blame] | 883 | DefinedInsnVariablesMap::const_iterator defined_insn_vars_begin() const { |
| 884 | return InsnVariableIDs.begin(); |
| 885 | } |
| 886 | DefinedInsnVariablesMap::const_iterator defined_insn_vars_end() const { |
| 887 | return InsnVariableIDs.end(); |
| 888 | } |
| 889 | iterator_range<typename DefinedInsnVariablesMap::const_iterator> |
| 890 | defined_insn_vars() const { |
| 891 | return make_range(defined_insn_vars_begin(), defined_insn_vars_end()); |
| 892 | } |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 893 | |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 894 | MutatableInsnSet::const_iterator mutatable_insns_begin() const { |
| 895 | return MutatableInsns.begin(); |
| 896 | } |
| 897 | MutatableInsnSet::const_iterator mutatable_insns_end() const { |
| 898 | return MutatableInsns.end(); |
| 899 | } |
| 900 | iterator_range<typename MutatableInsnSet::const_iterator> |
| 901 | mutatable_insns() const { |
| 902 | return make_range(mutatable_insns_begin(), mutatable_insns_end()); |
| 903 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 904 | void reserveInsnMatcherForMutation(InstructionMatcher *InsnMatcher) { |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 905 | bool R = MutatableInsns.erase(InsnMatcher); |
| 906 | assert(R && "Reserving a mutatable insn that isn't available"); |
| 907 | (void)R; |
| 908 | } |
| 909 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 910 | action_iterator actions_begin() { return Actions.begin(); } |
| 911 | action_iterator actions_end() { return Actions.end(); } |
| 912 | iterator_range<action_iterator> actions() { |
| 913 | return make_range(actions_begin(), actions_end()); |
| 914 | } |
| 915 | |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 916 | void defineOperand(StringRef SymbolicName, OperandMatcher &OM); |
| 917 | |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 918 | void definePhysRegOperand(Record *Reg, OperandMatcher &OM); |
| 919 | |
Jessica Paquette | 1ed1dd6 | 2019-02-09 00:29:13 +0000 | [diff] [blame] | 920 | Error defineComplexSubOperand(StringRef SymbolicName, Record *ComplexPattern, |
| 921 | unsigned RendererID, unsigned SubOperandID) { |
| 922 | if (ComplexSubOperands.count(SymbolicName)) |
| 923 | return failedImport( |
| 924 | "Complex suboperand referenced more than once (Operand: " + |
| 925 | SymbolicName + ")"); |
| 926 | |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 927 | ComplexSubOperands[SymbolicName] = |
| 928 | std::make_tuple(ComplexPattern, RendererID, SubOperandID); |
Jessica Paquette | 1ed1dd6 | 2019-02-09 00:29:13 +0000 | [diff] [blame] | 929 | |
| 930 | return Error::success(); |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 931 | } |
Jessica Paquette | 1ed1dd6 | 2019-02-09 00:29:13 +0000 | [diff] [blame] | 932 | |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 933 | Optional<DefinedComplexPatternSubOperand> |
| 934 | getComplexSubOperand(StringRef SymbolicName) const { |
| 935 | const auto &I = ComplexSubOperands.find(SymbolicName); |
| 936 | if (I == ComplexSubOperands.end()) |
| 937 | return None; |
| 938 | return I->second; |
| 939 | } |
| 940 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 941 | InstructionMatcher &getInstructionMatcher(StringRef SymbolicName) const; |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 942 | const OperandMatcher &getOperandMatcher(StringRef Name) const; |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 943 | const OperandMatcher &getPhysRegOperandMatcher(Record *) const; |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 944 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 945 | void optimize() override; |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 946 | void emit(MatchTable &Table) override; |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 947 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 948 | /// Compare the priority of this object and B. |
| 949 | /// |
| 950 | /// Returns true if this object is more important than B. |
| 951 | bool isHigherPriorityThan(const RuleMatcher &B) const; |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 952 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 953 | /// Report the maximum number of temporary operands needed by the rule |
| 954 | /// matcher. |
| 955 | unsigned countRendererFns() const; |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 956 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 957 | std::unique_ptr<PredicateMatcher> popFirstCondition() override; |
| 958 | const PredicateMatcher &getFirstCondition() const override; |
Roman Tereshin | 9a9fa49 | 2018-05-23 21:30:16 +0000 | [diff] [blame] | 959 | LLTCodeGen getFirstConditionAsRootType(); |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 960 | bool hasFirstCondition() const override; |
| 961 | unsigned getNumOperands() const; |
Roman Tereshin | 19da667 | 2018-05-22 04:31:50 +0000 | [diff] [blame] | 962 | StringRef getOpcode() const; |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 963 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 964 | // FIXME: Remove this as soon as possible |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 965 | InstructionMatcher &insnmatchers_front() const { return *Matchers.front(); } |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 966 | |
| 967 | unsigned allocateOutputInsnID() { return NextOutputInsnID++; } |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 968 | unsigned allocateTempRegID() { return NextTempRegID++; } |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 969 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 970 | iterator_range<MatchersTy::iterator> insnmatchers() { |
| 971 | return make_range(Matchers.begin(), Matchers.end()); |
| 972 | } |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 973 | bool insnmatchers_empty() const { return Matchers.empty(); } |
| 974 | void insnmatchers_pop_front() { Matchers.erase(Matchers.begin()); } |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 975 | }; |
| 976 | |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 977 | uint64_t RuleMatcher::NextRuleID = 0; |
| 978 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 979 | using action_iterator = RuleMatcher::action_iterator; |
| 980 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 981 | template <class PredicateTy> class PredicateListMatcher { |
| 982 | private: |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 983 | /// Template instantiations should specialize this to return a string to use |
| 984 | /// for the comment emitted when there are no predicates. |
| 985 | std::string getNoPredicateComment() const; |
| 986 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 987 | protected: |
| 988 | using PredicatesTy = std::deque<std::unique_ptr<PredicateTy>>; |
| 989 | PredicatesTy Predicates; |
Roman Tereshin | f0dc9fa | 2018-05-21 22:04:39 +0000 | [diff] [blame] | 990 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 991 | /// Track if the list of predicates was manipulated by one of the optimization |
| 992 | /// methods. |
| 993 | bool Optimized = false; |
| 994 | |
| 995 | public: |
| 996 | /// Construct a new predicate and add it to the matcher. |
| 997 | template <class Kind, class... Args> |
| 998 | Optional<Kind *> addPredicate(Args &&... args); |
| 999 | |
| 1000 | typename PredicatesTy::iterator predicates_begin() { |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 1001 | return Predicates.begin(); |
| 1002 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1003 | typename PredicatesTy::iterator predicates_end() { |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 1004 | return Predicates.end(); |
| 1005 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1006 | iterator_range<typename PredicatesTy::iterator> predicates() { |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1007 | return make_range(predicates_begin(), predicates_end()); |
| 1008 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1009 | typename PredicatesTy::size_type predicates_size() const { |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 1010 | return Predicates.size(); |
| 1011 | } |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 1012 | bool predicates_empty() const { return Predicates.empty(); } |
| 1013 | |
| 1014 | std::unique_ptr<PredicateTy> predicates_pop_front() { |
| 1015 | std::unique_ptr<PredicateTy> Front = std::move(Predicates.front()); |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1016 | Predicates.pop_front(); |
| 1017 | Optimized = true; |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 1018 | return Front; |
| 1019 | } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1020 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1021 | void prependPredicate(std::unique_ptr<PredicateTy> &&Predicate) { |
| 1022 | Predicates.push_front(std::move(Predicate)); |
| 1023 | } |
| 1024 | |
| 1025 | void eraseNullPredicates() { |
| 1026 | const auto NewEnd = |
| 1027 | std::stable_partition(Predicates.begin(), Predicates.end(), |
| 1028 | std::logical_not<std::unique_ptr<PredicateTy>>()); |
| 1029 | if (NewEnd != Predicates.begin()) { |
| 1030 | Predicates.erase(Predicates.begin(), NewEnd); |
| 1031 | Optimized = true; |
| 1032 | } |
| 1033 | } |
| 1034 | |
Daniel Sanders | 9d662d2 | 2017-07-06 10:06:12 +0000 | [diff] [blame] | 1035 | /// Emit MatchTable opcodes that tests whether all the predicates are met. |
Ahmed Bougacha | b67a3ce | 2017-01-26 22:07:37 +0000 | [diff] [blame] | 1036 | template <class... Args> |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1037 | void emitPredicateListOpcodes(MatchTable &Table, Args &&... args) { |
| 1038 | if (Predicates.empty() && !Optimized) { |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 1039 | Table << MatchTable::Comment(getNoPredicateComment()) |
| 1040 | << MatchTable::LineBreak; |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1041 | return; |
| 1042 | } |
| 1043 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1044 | for (const auto &Predicate : predicates()) |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1045 | Predicate->emitPredicateOpcodes(Table, std::forward<Args>(args)...); |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1046 | } |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1047 | }; |
| 1048 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1049 | class PredicateMatcher { |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1050 | public: |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1051 | /// This enum is used for RTTI and also defines the priority that is given to |
| 1052 | /// the predicate when generating the matcher code. Kinds with higher priority |
| 1053 | /// must be tested first. |
| 1054 | /// |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1055 | /// The relative priority of OPM_LLT, OPM_RegBank, and OPM_MBB do not matter |
| 1056 | /// but OPM_Int must have priority over OPM_RegBank since constant integers |
| 1057 | /// are represented by a virtual register defined by a G_CONSTANT instruction. |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1058 | /// |
| 1059 | /// Note: The relative priority between IPM_ and OPM_ does not matter, they |
| 1060 | /// are currently not compared between each other. |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1061 | enum PredicateKind { |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1062 | IPM_Opcode, |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1063 | IPM_NumOperands, |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1064 | IPM_ImmPredicate, |
Matt Arsenault | 3ecab8e | 2019-09-19 16:26:14 +0000 | [diff] [blame] | 1065 | IPM_Imm, |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1066 | IPM_AtomicOrderingMMO, |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 1067 | IPM_MemoryLLTSize, |
| 1068 | IPM_MemoryVsLLTSize, |
Matt Arsenault | d00d857 | 2019-07-15 20:59:42 +0000 | [diff] [blame] | 1069 | IPM_MemoryAddressSpace, |
Matt Arsenault | 52c2624 | 2019-07-31 00:14:43 +0000 | [diff] [blame] | 1070 | IPM_MemoryAlignment, |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 1071 | IPM_GenericPredicate, |
Daniel Sanders | 1e4569f | 2017-10-20 20:55:29 +0000 | [diff] [blame] | 1072 | OPM_SameOperand, |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1073 | OPM_ComplexPattern, |
Daniel Sanders | fe12c0f | 2017-07-11 08:57:29 +0000 | [diff] [blame] | 1074 | OPM_IntrinsicID, |
Matt Arsenault | 8ec5c10 | 2019-08-29 01:13:41 +0000 | [diff] [blame] | 1075 | OPM_CmpPredicate, |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 1076 | OPM_Instruction, |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1077 | OPM_Int, |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 1078 | OPM_LiteralInt, |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1079 | OPM_LLT, |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 1080 | OPM_PointerToAny, |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1081 | OPM_RegBank, |
| 1082 | OPM_MBB, |
| 1083 | }; |
| 1084 | |
| 1085 | protected: |
| 1086 | PredicateKind Kind; |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1087 | unsigned InsnVarID; |
| 1088 | unsigned OpIdx; |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1089 | |
| 1090 | public: |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1091 | PredicateMatcher(PredicateKind Kind, unsigned InsnVarID, unsigned OpIdx = ~0) |
| 1092 | : Kind(Kind), InsnVarID(InsnVarID), OpIdx(OpIdx) {} |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1093 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1094 | unsigned getInsnVarID() const { return InsnVarID; } |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1095 | unsigned getOpIdx() const { return OpIdx; } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1096 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1097 | virtual ~PredicateMatcher() = default; |
| 1098 | /// Emit MatchTable opcodes that check the predicate for the given operand. |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1099 | virtual void emitPredicateOpcodes(MatchTable &Table, |
| 1100 | RuleMatcher &Rule) const = 0; |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1101 | |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1102 | PredicateKind getKind() const { return Kind; } |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 1103 | |
| 1104 | virtual bool isIdentical(const PredicateMatcher &B) const { |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 1105 | return B.getKind() == getKind() && InsnVarID == B.InsnVarID && |
| 1106 | OpIdx == B.OpIdx; |
| 1107 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1108 | |
| 1109 | virtual bool isIdenticalDownToValue(const PredicateMatcher &B) const { |
| 1110 | return hasValue() && PredicateMatcher::isIdentical(B); |
| 1111 | } |
| 1112 | |
| 1113 | virtual MatchTableRecord getValue() const { |
| 1114 | assert(hasValue() && "Can not get a value of a value-less predicate!"); |
| 1115 | llvm_unreachable("Not implemented yet"); |
| 1116 | } |
| 1117 | virtual bool hasValue() const { return false; } |
| 1118 | |
| 1119 | /// Report the maximum number of temporary operands needed by the predicate |
| 1120 | /// matcher. |
| 1121 | virtual unsigned countRendererFns() const { return 0; } |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1122 | }; |
| 1123 | |
| 1124 | /// Generates code to check a predicate of an operand. |
| 1125 | /// |
| 1126 | /// Typical predicates include: |
| 1127 | /// * Operand is a particular register. |
| 1128 | /// * Operand is assigned a particular register bank. |
| 1129 | /// * Operand is an MBB. |
| 1130 | class OperandPredicateMatcher : public PredicateMatcher { |
| 1131 | public: |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1132 | OperandPredicateMatcher(PredicateKind Kind, unsigned InsnVarID, |
| 1133 | unsigned OpIdx) |
| 1134 | : PredicateMatcher(Kind, InsnVarID, OpIdx) {} |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1135 | virtual ~OperandPredicateMatcher() {} |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1136 | |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1137 | /// Compare the priority of this object and B. |
| 1138 | /// |
| 1139 | /// Returns true if this object is more important than B. |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 1140 | virtual bool isHigherPriorityThan(const OperandPredicateMatcher &B) const; |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1141 | }; |
| 1142 | |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 1143 | template <> |
| 1144 | std::string |
| 1145 | PredicateListMatcher<OperandPredicateMatcher>::getNoPredicateComment() const { |
| 1146 | return "No operand predicates"; |
| 1147 | } |
| 1148 | |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 1149 | /// Generates code to check that a register operand is defined by the same exact |
| 1150 | /// one as another. |
| 1151 | class SameOperandMatcher : public OperandPredicateMatcher { |
Daniel Sanders | 1e4569f | 2017-10-20 20:55:29 +0000 | [diff] [blame] | 1152 | std::string MatchingName; |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 1153 | |
| 1154 | public: |
Quentin Colombet | eba10cb | 2017-12-18 22:12:13 +0000 | [diff] [blame] | 1155 | SameOperandMatcher(unsigned InsnVarID, unsigned OpIdx, StringRef MatchingName) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1156 | : OperandPredicateMatcher(OPM_SameOperand, InsnVarID, OpIdx), |
| 1157 | MatchingName(MatchingName) {} |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 1158 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1159 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | 1e4569f | 2017-10-20 20:55:29 +0000 | [diff] [blame] | 1160 | return P->getKind() == OPM_SameOperand; |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1163 | void emitPredicateOpcodes(MatchTable &Table, |
| 1164 | RuleMatcher &Rule) const override; |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1165 | |
| 1166 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1167 | return OperandPredicateMatcher::isIdentical(B) && |
| 1168 | MatchingName == cast<SameOperandMatcher>(&B)->MatchingName; |
| 1169 | } |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 1170 | }; |
| 1171 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1172 | /// Generates code to check that an operand is a particular LLT. |
| 1173 | class LLTOperandMatcher : public OperandPredicateMatcher { |
| 1174 | protected: |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 1175 | LLTCodeGen Ty; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1176 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1177 | public: |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1178 | static std::map<LLTCodeGen, unsigned> TypeIDValues; |
| 1179 | |
| 1180 | static void initTypeIDValuesMap() { |
| 1181 | TypeIDValues.clear(); |
| 1182 | |
| 1183 | unsigned ID = 0; |
Mark de Wever | e8d448e | 2019-12-22 18:58:32 +0100 | [diff] [blame] | 1184 | for (const LLTCodeGen &LLTy : KnownTypes) |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1185 | TypeIDValues[LLTy] = ID++; |
| 1186 | } |
| 1187 | |
Quentin Colombet | eba10cb | 2017-12-18 22:12:13 +0000 | [diff] [blame] | 1188 | LLTOperandMatcher(unsigned InsnVarID, unsigned OpIdx, const LLTCodeGen &Ty) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1189 | : OperandPredicateMatcher(OPM_LLT, InsnVarID, OpIdx), Ty(Ty) { |
Daniel Sanders | 032e7f2 | 2017-08-17 13:18:35 +0000 | [diff] [blame] | 1190 | KnownTypes.insert(Ty); |
| 1191 | } |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1192 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1193 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1194 | return P->getKind() == OPM_LLT; |
| 1195 | } |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 1196 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1197 | return OperandPredicateMatcher::isIdentical(B) && |
| 1198 | Ty == cast<LLTOperandMatcher>(&B)->Ty; |
| 1199 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1200 | MatchTableRecord getValue() const override { |
| 1201 | const auto VI = TypeIDValues.find(Ty); |
| 1202 | if (VI == TypeIDValues.end()) |
| 1203 | return MatchTable::NamedValue(getTy().getCxxEnumValue()); |
| 1204 | return MatchTable::NamedValue(getTy().getCxxEnumValue(), VI->second); |
| 1205 | } |
| 1206 | bool hasValue() const override { |
| 1207 | if (TypeIDValues.size() != KnownTypes.size()) |
| 1208 | initTypeIDValuesMap(); |
| 1209 | return TypeIDValues.count(Ty); |
| 1210 | } |
| 1211 | |
| 1212 | LLTCodeGen getTy() const { return Ty; } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1213 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1214 | void emitPredicateOpcodes(MatchTable &Table, |
| 1215 | RuleMatcher &Rule) const override { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1216 | Table << MatchTable::Opcode("GIM_CheckType") << MatchTable::Comment("MI") |
| 1217 | << MatchTable::IntValue(InsnVarID) << MatchTable::Comment("Op") |
| 1218 | << MatchTable::IntValue(OpIdx) << MatchTable::Comment("Type") |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1219 | << getValue() << MatchTable::LineBreak; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1220 | } |
| 1221 | }; |
| 1222 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1223 | std::map<LLTCodeGen, unsigned> LLTOperandMatcher::TypeIDValues; |
| 1224 | |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 1225 | /// Generates code to check that an operand is a pointer to any address space. |
| 1226 | /// |
| 1227 | /// In SelectionDAG, the types did not describe pointers or address spaces. As a |
| 1228 | /// result, iN is used to describe a pointer of N bits to any address space and |
| 1229 | /// PatFrag predicates are typically used to constrain the address space. There's |
| 1230 | /// no reliable means to derive the missing type information from the pattern so |
| 1231 | /// imported rules must test the components of a pointer separately. |
| 1232 | /// |
Daniel Sanders | ea8711b | 2017-10-16 03:36:29 +0000 | [diff] [blame] | 1233 | /// If SizeInBits is zero, then the pointer size will be obtained from the |
| 1234 | /// subtarget. |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 1235 | class PointerToAnyOperandMatcher : public OperandPredicateMatcher { |
| 1236 | protected: |
| 1237 | unsigned SizeInBits; |
| 1238 | |
| 1239 | public: |
Quentin Colombet | eba10cb | 2017-12-18 22:12:13 +0000 | [diff] [blame] | 1240 | PointerToAnyOperandMatcher(unsigned InsnVarID, unsigned OpIdx, |
| 1241 | unsigned SizeInBits) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1242 | : OperandPredicateMatcher(OPM_PointerToAny, InsnVarID, OpIdx), |
| 1243 | SizeInBits(SizeInBits) {} |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 1244 | |
| 1245 | static bool classof(const OperandPredicateMatcher *P) { |
| 1246 | return P->getKind() == OPM_PointerToAny; |
| 1247 | } |
| 1248 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1249 | void emitPredicateOpcodes(MatchTable &Table, |
| 1250 | RuleMatcher &Rule) const override { |
| 1251 | Table << MatchTable::Opcode("GIM_CheckPointerToAny") |
| 1252 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1253 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) |
| 1254 | << MatchTable::Comment("SizeInBits") |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 1255 | << MatchTable::IntValue(SizeInBits) << MatchTable::LineBreak; |
| 1256 | } |
| 1257 | }; |
| 1258 | |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1259 | /// Generates code to check that an operand is a particular target constant. |
| 1260 | class ComplexPatternOperandMatcher : public OperandPredicateMatcher { |
| 1261 | protected: |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1262 | const OperandMatcher &Operand; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1263 | const Record &TheDef; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1264 | |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1265 | unsigned getAllocatedTemporariesBaseID() const; |
| 1266 | |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1267 | public: |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 1268 | bool isIdentical(const PredicateMatcher &B) const override { return false; } |
| 1269 | |
Quentin Colombet | eba10cb | 2017-12-18 22:12:13 +0000 | [diff] [blame] | 1270 | ComplexPatternOperandMatcher(unsigned InsnVarID, unsigned OpIdx, |
| 1271 | const OperandMatcher &Operand, |
| 1272 | const Record &TheDef) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1273 | : OperandPredicateMatcher(OPM_ComplexPattern, InsnVarID, OpIdx), |
| 1274 | Operand(Operand), TheDef(TheDef) {} |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1275 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1276 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1277 | return P->getKind() == OPM_ComplexPattern; |
| 1278 | } |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1279 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1280 | void emitPredicateOpcodes(MatchTable &Table, |
| 1281 | RuleMatcher &Rule) const override { |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 1282 | unsigned ID = getAllocatedTemporariesBaseID(); |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1283 | Table << MatchTable::Opcode("GIM_CheckComplexPattern") |
| 1284 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1285 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) |
| 1286 | << MatchTable::Comment("Renderer") << MatchTable::IntValue(ID) |
| 1287 | << MatchTable::NamedValue(("GICP_" + TheDef.getName()).str()) |
| 1288 | << MatchTable::LineBreak; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 1291 | unsigned countRendererFns() const override { |
| 1292 | return 1; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1293 | } |
| 1294 | }; |
| 1295 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1296 | /// Generates code to check that an operand is in a particular register bank. |
| 1297 | class RegisterBankOperandMatcher : public OperandPredicateMatcher { |
| 1298 | protected: |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1299 | const CodeGenRegisterClass &RC; |
| 1300 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1301 | public: |
Quentin Colombet | eba10cb | 2017-12-18 22:12:13 +0000 | [diff] [blame] | 1302 | RegisterBankOperandMatcher(unsigned InsnVarID, unsigned OpIdx, |
| 1303 | const CodeGenRegisterClass &RC) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1304 | : OperandPredicateMatcher(OPM_RegBank, InsnVarID, OpIdx), RC(RC) {} |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1305 | |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 1306 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1307 | return OperandPredicateMatcher::isIdentical(B) && |
| 1308 | RC.getDef() == cast<RegisterBankOperandMatcher>(&B)->RC.getDef(); |
| 1309 | } |
| 1310 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1311 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1312 | return P->getKind() == OPM_RegBank; |
| 1313 | } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1314 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1315 | void emitPredicateOpcodes(MatchTable &Table, |
| 1316 | RuleMatcher &Rule) const override { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1317 | Table << MatchTable::Opcode("GIM_CheckRegBankForClass") |
| 1318 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1319 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) |
| 1320 | << MatchTable::Comment("RC") |
| 1321 | << MatchTable::NamedValue(RC.getQualifiedName() + "RegClassID") |
| 1322 | << MatchTable::LineBreak; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1323 | } |
| 1324 | }; |
| 1325 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1326 | /// Generates code to check that an operand is a basic block. |
| 1327 | class MBBOperandMatcher : public OperandPredicateMatcher { |
| 1328 | public: |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1329 | MBBOperandMatcher(unsigned InsnVarID, unsigned OpIdx) |
| 1330 | : OperandPredicateMatcher(OPM_MBB, InsnVarID, OpIdx) {} |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1331 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1332 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1333 | return P->getKind() == OPM_MBB; |
| 1334 | } |
| 1335 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1336 | void emitPredicateOpcodes(MatchTable &Table, |
| 1337 | RuleMatcher &Rule) const override { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1338 | Table << MatchTable::Opcode("GIM_CheckIsMBB") << MatchTable::Comment("MI") |
| 1339 | << MatchTable::IntValue(InsnVarID) << MatchTable::Comment("Op") |
| 1340 | << MatchTable::IntValue(OpIdx) << MatchTable::LineBreak; |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1341 | } |
| 1342 | }; |
| 1343 | |
Matt Arsenault | 3ecab8e | 2019-09-19 16:26:14 +0000 | [diff] [blame] | 1344 | class ImmOperandMatcher : public OperandPredicateMatcher { |
| 1345 | public: |
| 1346 | ImmOperandMatcher(unsigned InsnVarID, unsigned OpIdx) |
| 1347 | : OperandPredicateMatcher(IPM_Imm, InsnVarID, OpIdx) {} |
| 1348 | |
| 1349 | static bool classof(const PredicateMatcher *P) { |
| 1350 | return P->getKind() == IPM_Imm; |
| 1351 | } |
| 1352 | |
| 1353 | void emitPredicateOpcodes(MatchTable &Table, |
| 1354 | RuleMatcher &Rule) const override { |
| 1355 | Table << MatchTable::Opcode("GIM_CheckIsImm") << MatchTable::Comment("MI") |
| 1356 | << MatchTable::IntValue(InsnVarID) << MatchTable::Comment("Op") |
| 1357 | << MatchTable::IntValue(OpIdx) << MatchTable::LineBreak; |
| 1358 | } |
| 1359 | }; |
| 1360 | |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 1361 | /// Generates code to check that an operand is a G_CONSTANT with a particular |
| 1362 | /// int. |
| 1363 | class ConstantIntOperandMatcher : public OperandPredicateMatcher { |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1364 | protected: |
| 1365 | int64_t Value; |
| 1366 | |
| 1367 | public: |
Quentin Colombet | eba10cb | 2017-12-18 22:12:13 +0000 | [diff] [blame] | 1368 | ConstantIntOperandMatcher(unsigned InsnVarID, unsigned OpIdx, int64_t Value) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1369 | : OperandPredicateMatcher(OPM_Int, InsnVarID, OpIdx), Value(Value) {} |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1370 | |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 1371 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1372 | return OperandPredicateMatcher::isIdentical(B) && |
| 1373 | Value == cast<ConstantIntOperandMatcher>(&B)->Value; |
| 1374 | } |
| 1375 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1376 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1377 | return P->getKind() == OPM_Int; |
| 1378 | } |
| 1379 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1380 | void emitPredicateOpcodes(MatchTable &Table, |
| 1381 | RuleMatcher &Rule) const override { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1382 | Table << MatchTable::Opcode("GIM_CheckConstantInt") |
| 1383 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1384 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) |
| 1385 | << MatchTable::IntValue(Value) << MatchTable::LineBreak; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1386 | } |
| 1387 | }; |
| 1388 | |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 1389 | /// Generates code to check that an operand is a raw int (where MO.isImm() or |
| 1390 | /// MO.isCImm() is true). |
| 1391 | class LiteralIntOperandMatcher : public OperandPredicateMatcher { |
| 1392 | protected: |
| 1393 | int64_t Value; |
| 1394 | |
| 1395 | public: |
Quentin Colombet | eba10cb | 2017-12-18 22:12:13 +0000 | [diff] [blame] | 1396 | LiteralIntOperandMatcher(unsigned InsnVarID, unsigned OpIdx, int64_t Value) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1397 | : OperandPredicateMatcher(OPM_LiteralInt, InsnVarID, OpIdx), |
| 1398 | Value(Value) {} |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 1399 | |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 1400 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1401 | return OperandPredicateMatcher::isIdentical(B) && |
| 1402 | Value == cast<LiteralIntOperandMatcher>(&B)->Value; |
| 1403 | } |
| 1404 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1405 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 1406 | return P->getKind() == OPM_LiteralInt; |
| 1407 | } |
| 1408 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1409 | void emitPredicateOpcodes(MatchTable &Table, |
| 1410 | RuleMatcher &Rule) const override { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1411 | Table << MatchTable::Opcode("GIM_CheckLiteralInt") |
| 1412 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1413 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) |
| 1414 | << MatchTable::IntValue(Value) << MatchTable::LineBreak; |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 1415 | } |
| 1416 | }; |
| 1417 | |
Matt Arsenault | 8ec5c10 | 2019-08-29 01:13:41 +0000 | [diff] [blame] | 1418 | /// Generates code to check that an operand is an CmpInst predicate |
| 1419 | class CmpPredicateOperandMatcher : public OperandPredicateMatcher { |
| 1420 | protected: |
| 1421 | std::string PredName; |
| 1422 | |
| 1423 | public: |
| 1424 | CmpPredicateOperandMatcher(unsigned InsnVarID, unsigned OpIdx, |
| 1425 | std::string P) |
| 1426 | : OperandPredicateMatcher(OPM_CmpPredicate, InsnVarID, OpIdx), PredName(P) {} |
| 1427 | |
| 1428 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1429 | return OperandPredicateMatcher::isIdentical(B) && |
| 1430 | PredName == cast<CmpPredicateOperandMatcher>(&B)->PredName; |
| 1431 | } |
| 1432 | |
| 1433 | static bool classof(const PredicateMatcher *P) { |
| 1434 | return P->getKind() == OPM_CmpPredicate; |
| 1435 | } |
| 1436 | |
| 1437 | void emitPredicateOpcodes(MatchTable &Table, |
| 1438 | RuleMatcher &Rule) const override { |
| 1439 | Table << MatchTable::Opcode("GIM_CheckCmpPredicate") |
| 1440 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1441 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) |
| 1442 | << MatchTable::Comment("Predicate") |
| 1443 | << MatchTable::NamedValue("CmpInst", PredName) |
| 1444 | << MatchTable::LineBreak; |
| 1445 | } |
| 1446 | }; |
| 1447 | |
Daniel Sanders | fe12c0f | 2017-07-11 08:57:29 +0000 | [diff] [blame] | 1448 | /// Generates code to check that an operand is an intrinsic ID. |
| 1449 | class IntrinsicIDOperandMatcher : public OperandPredicateMatcher { |
| 1450 | protected: |
| 1451 | const CodeGenIntrinsic *II; |
| 1452 | |
| 1453 | public: |
Quentin Colombet | eba10cb | 2017-12-18 22:12:13 +0000 | [diff] [blame] | 1454 | IntrinsicIDOperandMatcher(unsigned InsnVarID, unsigned OpIdx, |
| 1455 | const CodeGenIntrinsic *II) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1456 | : OperandPredicateMatcher(OPM_IntrinsicID, InsnVarID, OpIdx), II(II) {} |
Daniel Sanders | fe12c0f | 2017-07-11 08:57:29 +0000 | [diff] [blame] | 1457 | |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 1458 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1459 | return OperandPredicateMatcher::isIdentical(B) && |
| 1460 | II == cast<IntrinsicIDOperandMatcher>(&B)->II; |
| 1461 | } |
| 1462 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1463 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | fe12c0f | 2017-07-11 08:57:29 +0000 | [diff] [blame] | 1464 | return P->getKind() == OPM_IntrinsicID; |
| 1465 | } |
| 1466 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1467 | void emitPredicateOpcodes(MatchTable &Table, |
| 1468 | RuleMatcher &Rule) const override { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1469 | Table << MatchTable::Opcode("GIM_CheckIntrinsicID") |
| 1470 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1471 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) |
| 1472 | << MatchTable::NamedValue("Intrinsic::" + II->EnumName) |
| 1473 | << MatchTable::LineBreak; |
Daniel Sanders | fe12c0f | 2017-07-11 08:57:29 +0000 | [diff] [blame] | 1474 | } |
| 1475 | }; |
| 1476 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1477 | /// Generates code to check that a set of predicates match for a particular |
| 1478 | /// operand. |
| 1479 | class OperandMatcher : public PredicateListMatcher<OperandPredicateMatcher> { |
| 1480 | protected: |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 1481 | InstructionMatcher &Insn; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1482 | unsigned OpIdx; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1483 | std::string SymbolicName; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1484 | |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1485 | /// The index of the first temporary variable allocated to this operand. The |
| 1486 | /// number of allocated temporaries can be found with |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 1487 | /// countRendererFns(). |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1488 | unsigned AllocatedTemporariesBaseID; |
| 1489 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1490 | public: |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 1491 | OperandMatcher(InstructionMatcher &Insn, unsigned OpIdx, |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1492 | const std::string &SymbolicName, |
| 1493 | unsigned AllocatedTemporariesBaseID) |
| 1494 | : Insn(Insn), OpIdx(OpIdx), SymbolicName(SymbolicName), |
| 1495 | AllocatedTemporariesBaseID(AllocatedTemporariesBaseID) {} |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1496 | |
| 1497 | bool hasSymbolicName() const { return !SymbolicName.empty(); } |
| 1498 | const StringRef getSymbolicName() const { return SymbolicName; } |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 1499 | void setSymbolicName(StringRef Name) { |
| 1500 | assert(SymbolicName.empty() && "Operand already has a symbolic name"); |
| 1501 | SymbolicName = Name; |
| 1502 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1503 | |
| 1504 | /// Construct a new operand predicate and add it to the matcher. |
| 1505 | template <class Kind, class... Args> |
| 1506 | Optional<Kind *> addPredicate(Args &&... args) { |
| 1507 | if (isSameAsAnotherOperand()) |
| 1508 | return None; |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 1509 | Predicates.emplace_back(std::make_unique<Kind>( |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1510 | getInsnVarID(), getOpIdx(), std::forward<Args>(args)...)); |
| 1511 | return static_cast<Kind *>(Predicates.back().get()); |
| 1512 | } |
| 1513 | |
| 1514 | unsigned getOpIdx() const { return OpIdx; } |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1515 | unsigned getInsnVarID() const; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1516 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 1517 | std::string getOperandExpr(unsigned InsnVarID) const { |
| 1518 | return "State.MIs[" + llvm::to_string(InsnVarID) + "]->getOperand(" + |
| 1519 | llvm::to_string(OpIdx) + ")"; |
Daniel Sanders | e604ef5 | 2017-02-20 15:30:43 +0000 | [diff] [blame] | 1520 | } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1521 | |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 1522 | InstructionMatcher &getInstructionMatcher() const { return Insn; } |
| 1523 | |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 1524 | Error addTypeCheckPredicate(const TypeSetByHwMode &VTy, |
Reid Kleckner | cfdd4a2 | 2017-10-16 20:31:16 +0000 | [diff] [blame] | 1525 | bool OperandIsAPointer); |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 1526 | |
Daniel Sanders | 9d662d2 | 2017-07-06 10:06:12 +0000 | [diff] [blame] | 1527 | /// Emit MatchTable opcodes that test whether the instruction named in |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 1528 | /// InsnVarID matches all the predicates and all the operands. |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1529 | void emitPredicateOpcodes(MatchTable &Table, RuleMatcher &Rule) { |
| 1530 | if (!Optimized) { |
| 1531 | std::string Comment; |
| 1532 | raw_string_ostream CommentOS(Comment); |
| 1533 | CommentOS << "MIs[" << getInsnVarID() << "] "; |
| 1534 | if (SymbolicName.empty()) |
| 1535 | CommentOS << "Operand " << OpIdx; |
| 1536 | else |
| 1537 | CommentOS << SymbolicName; |
| 1538 | Table << MatchTable::Comment(CommentOS.str()) << MatchTable::LineBreak; |
| 1539 | } |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1540 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1541 | emitPredicateListOpcodes(Table, Rule); |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1542 | } |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1543 | |
| 1544 | /// Compare the priority of this object and B. |
| 1545 | /// |
| 1546 | /// Returns true if this object is more important than B. |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1547 | bool isHigherPriorityThan(OperandMatcher &B) { |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1548 | // Operand matchers involving more predicates have higher priority. |
| 1549 | if (predicates_size() > B.predicates_size()) |
| 1550 | return true; |
| 1551 | if (predicates_size() < B.predicates_size()) |
| 1552 | return false; |
| 1553 | |
| 1554 | // This assumes that predicates are added in a consistent order. |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1555 | for (auto &&Predicate : zip(predicates(), B.predicates())) { |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1556 | if (std::get<0>(Predicate)->isHigherPriorityThan(*std::get<1>(Predicate))) |
| 1557 | return true; |
| 1558 | if (std::get<1>(Predicate)->isHigherPriorityThan(*std::get<0>(Predicate))) |
| 1559 | return false; |
| 1560 | } |
| 1561 | |
| 1562 | return false; |
| 1563 | }; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1564 | |
| 1565 | /// Report the maximum number of temporary operands needed by the operand |
| 1566 | /// matcher. |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1567 | unsigned countRendererFns() { |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1568 | return std::accumulate( |
| 1569 | predicates().begin(), predicates().end(), 0, |
| 1570 | [](unsigned A, |
| 1571 | const std::unique_ptr<OperandPredicateMatcher> &Predicate) { |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 1572 | return A + Predicate->countRendererFns(); |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1573 | }); |
| 1574 | } |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1575 | |
| 1576 | unsigned getAllocatedTemporariesBaseID() const { |
| 1577 | return AllocatedTemporariesBaseID; |
| 1578 | } |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 1579 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1580 | bool isSameAsAnotherOperand() { |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 1581 | for (const auto &Predicate : predicates()) |
| 1582 | if (isa<SameOperandMatcher>(Predicate)) |
| 1583 | return true; |
| 1584 | return false; |
| 1585 | } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1586 | }; |
| 1587 | |
Reid Kleckner | cfdd4a2 | 2017-10-16 20:31:16 +0000 | [diff] [blame] | 1588 | Error OperandMatcher::addTypeCheckPredicate(const TypeSetByHwMode &VTy, |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1589 | bool OperandIsAPointer) { |
Reid Kleckner | cfdd4a2 | 2017-10-16 20:31:16 +0000 | [diff] [blame] | 1590 | if (!VTy.isMachineValueType()) |
| 1591 | return failedImport("unsupported typeset"); |
| 1592 | |
| 1593 | if (VTy.getMachineValueType() == MVT::iPTR && OperandIsAPointer) { |
| 1594 | addPredicate<PointerToAnyOperandMatcher>(0); |
| 1595 | return Error::success(); |
| 1596 | } |
| 1597 | |
| 1598 | auto OpTyOrNone = MVTToLLT(VTy.getMachineValueType().SimpleTy); |
| 1599 | if (!OpTyOrNone) |
| 1600 | return failedImport("unsupported type"); |
| 1601 | |
| 1602 | if (OperandIsAPointer) |
| 1603 | addPredicate<PointerToAnyOperandMatcher>(OpTyOrNone->get().getSizeInBits()); |
Tom Stellard | 9ad714f | 2019-02-20 19:43:47 +0000 | [diff] [blame] | 1604 | else if (VTy.isPointer()) |
| 1605 | addPredicate<LLTOperandMatcher>(LLT::pointer(VTy.getPtrAddrSpace(), |
| 1606 | OpTyOrNone->get().getSizeInBits())); |
Reid Kleckner | cfdd4a2 | 2017-10-16 20:31:16 +0000 | [diff] [blame] | 1607 | else |
| 1608 | addPredicate<LLTOperandMatcher>(*OpTyOrNone); |
| 1609 | return Error::success(); |
| 1610 | } |
| 1611 | |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1612 | unsigned ComplexPatternOperandMatcher::getAllocatedTemporariesBaseID() const { |
| 1613 | return Operand.getAllocatedTemporariesBaseID(); |
| 1614 | } |
| 1615 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1616 | /// Generates code to check a predicate on an instruction. |
| 1617 | /// |
| 1618 | /// Typical predicates include: |
| 1619 | /// * The opcode of the instruction is a particular value. |
| 1620 | /// * The nsw/nuw flag is/isn't set. |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1621 | class InstructionPredicateMatcher : public PredicateMatcher { |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1622 | public: |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1623 | InstructionPredicateMatcher(PredicateKind Kind, unsigned InsnVarID) |
| 1624 | : PredicateMatcher(Kind, InsnVarID) {} |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1625 | virtual ~InstructionPredicateMatcher() {} |
| 1626 | |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1627 | /// Compare the priority of this object and B. |
| 1628 | /// |
| 1629 | /// Returns true if this object is more important than B. |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 1630 | virtual bool |
| 1631 | isHigherPriorityThan(const InstructionPredicateMatcher &B) const { |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1632 | return Kind < B.Kind; |
| 1633 | }; |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1634 | }; |
| 1635 | |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 1636 | template <> |
| 1637 | std::string |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1638 | PredicateListMatcher<PredicateMatcher>::getNoPredicateComment() const { |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 1639 | return "No instruction predicates"; |
| 1640 | } |
| 1641 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1642 | /// Generates code to check the opcode of an instruction. |
| 1643 | class InstructionOpcodeMatcher : public InstructionPredicateMatcher { |
| 1644 | protected: |
| 1645 | const CodeGenInstruction *I; |
| 1646 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1647 | static DenseMap<const CodeGenInstruction *, unsigned> OpcodeValues; |
| 1648 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1649 | public: |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1650 | static void initOpcodeValuesMap(const CodeGenTarget &Target) { |
| 1651 | OpcodeValues.clear(); |
| 1652 | |
| 1653 | unsigned OpcodeValue = 0; |
| 1654 | for (const CodeGenInstruction *I : Target.getInstructionsByEnumValue()) |
| 1655 | OpcodeValues[I] = OpcodeValue++; |
| 1656 | } |
| 1657 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1658 | InstructionOpcodeMatcher(unsigned InsnVarID, const CodeGenInstruction *I) |
| 1659 | : InstructionPredicateMatcher(IPM_Opcode, InsnVarID), I(I) {} |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1660 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1661 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1662 | return P->getKind() == IPM_Opcode; |
| 1663 | } |
| 1664 | |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 1665 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1666 | return InstructionPredicateMatcher::isIdentical(B) && |
| 1667 | I == cast<InstructionOpcodeMatcher>(&B)->I; |
| 1668 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1669 | MatchTableRecord getValue() const override { |
| 1670 | const auto VI = OpcodeValues.find(I); |
| 1671 | if (VI != OpcodeValues.end()) |
| 1672 | return MatchTable::NamedValue(I->Namespace, I->TheDef->getName(), |
| 1673 | VI->second); |
| 1674 | return MatchTable::NamedValue(I->Namespace, I->TheDef->getName()); |
| 1675 | } |
| 1676 | bool hasValue() const override { return OpcodeValues.count(I); } |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 1677 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1678 | void emitPredicateOpcodes(MatchTable &Table, |
| 1679 | RuleMatcher &Rule) const override { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1680 | Table << MatchTable::Opcode("GIM_CheckOpcode") << MatchTable::Comment("MI") |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1681 | << MatchTable::IntValue(InsnVarID) << getValue() |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1682 | << MatchTable::LineBreak; |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1683 | } |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1684 | |
| 1685 | /// Compare the priority of this object and B. |
| 1686 | /// |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1687 | /// Returns true if this object is more important than B. |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 1688 | bool |
| 1689 | isHigherPriorityThan(const InstructionPredicateMatcher &B) const override { |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1690 | if (InstructionPredicateMatcher::isHigherPriorityThan(B)) |
| 1691 | return true; |
| 1692 | if (B.InstructionPredicateMatcher::isHigherPriorityThan(*this)) |
| 1693 | return false; |
| 1694 | |
| 1695 | // Prioritize opcodes for cosmetic reasons in the generated source. Although |
| 1696 | // this is cosmetic at the moment, we may want to drive a similar ordering |
| 1697 | // using instruction frequency information to improve compile time. |
| 1698 | if (const InstructionOpcodeMatcher *BO = |
| 1699 | dyn_cast<InstructionOpcodeMatcher>(&B)) |
| 1700 | return I->TheDef->getName() < BO->I->TheDef->getName(); |
| 1701 | |
| 1702 | return false; |
| 1703 | }; |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 1704 | |
| 1705 | bool isConstantInstruction() const { |
| 1706 | return I->TheDef->getName() == "G_CONSTANT"; |
| 1707 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1708 | |
Roman Tereshin | 19da667 | 2018-05-22 04:31:50 +0000 | [diff] [blame] | 1709 | StringRef getOpcode() const { return I->TheDef->getName(); } |
Roman Tereshin | 6082a06 | 2019-10-30 20:58:46 -0700 | [diff] [blame] | 1710 | bool isVariadicNumOperands() const { return I->Operands.isVariadic; } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1711 | |
| 1712 | StringRef getOperandType(unsigned OpIdx) const { |
| 1713 | return I->Operands[OpIdx].OperandType; |
| 1714 | } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1715 | }; |
| 1716 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1717 | DenseMap<const CodeGenInstruction *, unsigned> |
| 1718 | InstructionOpcodeMatcher::OpcodeValues; |
| 1719 | |
Roman Tereshin | 19da667 | 2018-05-22 04:31:50 +0000 | [diff] [blame] | 1720 | class InstructionNumOperandsMatcher final : public InstructionPredicateMatcher { |
| 1721 | unsigned NumOperands = 0; |
| 1722 | |
| 1723 | public: |
| 1724 | InstructionNumOperandsMatcher(unsigned InsnVarID, unsigned NumOperands) |
| 1725 | : InstructionPredicateMatcher(IPM_NumOperands, InsnVarID), |
| 1726 | NumOperands(NumOperands) {} |
| 1727 | |
| 1728 | static bool classof(const PredicateMatcher *P) { |
| 1729 | return P->getKind() == IPM_NumOperands; |
| 1730 | } |
| 1731 | |
| 1732 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1733 | return InstructionPredicateMatcher::isIdentical(B) && |
| 1734 | NumOperands == cast<InstructionNumOperandsMatcher>(&B)->NumOperands; |
| 1735 | } |
| 1736 | |
| 1737 | void emitPredicateOpcodes(MatchTable &Table, |
| 1738 | RuleMatcher &Rule) const override { |
| 1739 | Table << MatchTable::Opcode("GIM_CheckNumOperands") |
| 1740 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1741 | << MatchTable::Comment("Expected") |
| 1742 | << MatchTable::IntValue(NumOperands) << MatchTable::LineBreak; |
| 1743 | } |
| 1744 | }; |
| 1745 | |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 1746 | /// Generates code to check that this instruction is a constant whose value |
| 1747 | /// meets an immediate predicate. |
| 1748 | /// |
| 1749 | /// Immediates are slightly odd since they are typically used like an operand |
| 1750 | /// but are represented as an operator internally. We typically write simm8:$src |
| 1751 | /// in a tablegen pattern, but this is just syntactic sugar for |
| 1752 | /// (imm:i32)<<P:Predicate_simm8>>:$imm which more directly describes the nodes |
| 1753 | /// that will be matched and the predicate (which is attached to the imm |
| 1754 | /// operator) that will be tested. In SelectionDAG this describes a |
| 1755 | /// ConstantSDNode whose internal value will be tested using the simm8 predicate. |
| 1756 | /// |
| 1757 | /// The corresponding GlobalISel representation is %1 = G_CONSTANT iN Value. In |
| 1758 | /// this representation, the immediate could be tested with an |
| 1759 | /// InstructionMatcher, InstructionOpcodeMatcher, OperandMatcher, and a |
| 1760 | /// OperandPredicateMatcher-subclass to check the Value meets the predicate but |
| 1761 | /// there are two implementation issues with producing that matcher |
| 1762 | /// configuration from the SelectionDAG pattern: |
| 1763 | /// * ImmLeaf is a PatFrag whose root is an InstructionMatcher. This means that |
| 1764 | /// were we to sink the immediate predicate to the operand we would have to |
| 1765 | /// have two partial implementations of PatFrag support, one for immediates |
| 1766 | /// and one for non-immediates. |
| 1767 | /// * At the point we handle the predicate, the OperandMatcher hasn't been |
| 1768 | /// created yet. If we were to sink the predicate to the OperandMatcher we |
| 1769 | /// would also have to complicate (or duplicate) the code that descends and |
| 1770 | /// creates matchers for the subtree. |
| 1771 | /// Overall, it's simpler to handle it in the place it was found. |
| 1772 | class InstructionImmPredicateMatcher : public InstructionPredicateMatcher { |
| 1773 | protected: |
| 1774 | TreePredicateFn Predicate; |
| 1775 | |
| 1776 | public: |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1777 | InstructionImmPredicateMatcher(unsigned InsnVarID, |
| 1778 | const TreePredicateFn &Predicate) |
| 1779 | : InstructionPredicateMatcher(IPM_ImmPredicate, InsnVarID), |
| 1780 | Predicate(Predicate) {} |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 1781 | |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 1782 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1783 | return InstructionPredicateMatcher::isIdentical(B) && |
| 1784 | Predicate.getOrigPatFragRecord() == |
| 1785 | cast<InstructionImmPredicateMatcher>(&B) |
| 1786 | ->Predicate.getOrigPatFragRecord(); |
| 1787 | } |
| 1788 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1789 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 1790 | return P->getKind() == IPM_ImmPredicate; |
| 1791 | } |
| 1792 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1793 | void emitPredicateOpcodes(MatchTable &Table, |
| 1794 | RuleMatcher &Rule) const override { |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 1795 | Table << MatchTable::Opcode(getMatchOpcodeForPredicate(Predicate)) |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 1796 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1797 | << MatchTable::Comment("Predicate") |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 1798 | << MatchTable::NamedValue(getEnumNameForPredicate(Predicate)) |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 1799 | << MatchTable::LineBreak; |
| 1800 | } |
| 1801 | }; |
| 1802 | |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 1803 | /// Generates code to check that a memory instruction has a atomic ordering |
| 1804 | /// MachineMemoryOperand. |
| 1805 | class AtomicOrderingMMOPredicateMatcher : public InstructionPredicateMatcher { |
Daniel Sanders | 0c43b3a | 2017-11-30 21:05:59 +0000 | [diff] [blame] | 1806 | public: |
| 1807 | enum AOComparator { |
| 1808 | AO_Exactly, |
| 1809 | AO_OrStronger, |
| 1810 | AO_WeakerThan, |
| 1811 | }; |
| 1812 | |
| 1813 | protected: |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 1814 | StringRef Order; |
Daniel Sanders | 0c43b3a | 2017-11-30 21:05:59 +0000 | [diff] [blame] | 1815 | AOComparator Comparator; |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 1816 | |
Daniel Sanders | 39690bd | 2017-10-15 02:41:12 +0000 | [diff] [blame] | 1817 | public: |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1818 | AtomicOrderingMMOPredicateMatcher(unsigned InsnVarID, StringRef Order, |
Daniel Sanders | 0c43b3a | 2017-11-30 21:05:59 +0000 | [diff] [blame] | 1819 | AOComparator Comparator = AO_Exactly) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1820 | : InstructionPredicateMatcher(IPM_AtomicOrderingMMO, InsnVarID), |
| 1821 | Order(Order), Comparator(Comparator) {} |
Daniel Sanders | 39690bd | 2017-10-15 02:41:12 +0000 | [diff] [blame] | 1822 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1823 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 1824 | return P->getKind() == IPM_AtomicOrderingMMO; |
Daniel Sanders | 39690bd | 2017-10-15 02:41:12 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1827 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1828 | if (!InstructionPredicateMatcher::isIdentical(B)) |
| 1829 | return false; |
| 1830 | const auto &R = *cast<AtomicOrderingMMOPredicateMatcher>(&B); |
| 1831 | return Order == R.Order && Comparator == R.Comparator; |
| 1832 | } |
| 1833 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1834 | void emitPredicateOpcodes(MatchTable &Table, |
| 1835 | RuleMatcher &Rule) const override { |
Daniel Sanders | 0c43b3a | 2017-11-30 21:05:59 +0000 | [diff] [blame] | 1836 | StringRef Opcode = "GIM_CheckAtomicOrdering"; |
| 1837 | |
| 1838 | if (Comparator == AO_OrStronger) |
| 1839 | Opcode = "GIM_CheckAtomicOrderingOrStrongerThan"; |
| 1840 | if (Comparator == AO_WeakerThan) |
| 1841 | Opcode = "GIM_CheckAtomicOrderingWeakerThan"; |
| 1842 | |
| 1843 | Table << MatchTable::Opcode(Opcode) << MatchTable::Comment("MI") |
| 1844 | << MatchTable::IntValue(InsnVarID) << MatchTable::Comment("Order") |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 1845 | << MatchTable::NamedValue(("(int64_t)AtomicOrdering::" + Order).str()) |
Daniel Sanders | 39690bd | 2017-10-15 02:41:12 +0000 | [diff] [blame] | 1846 | << MatchTable::LineBreak; |
| 1847 | } |
| 1848 | }; |
| 1849 | |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 1850 | /// Generates code to check that the size of an MMO is exactly N bytes. |
| 1851 | class MemorySizePredicateMatcher : public InstructionPredicateMatcher { |
| 1852 | protected: |
| 1853 | unsigned MMOIdx; |
| 1854 | uint64_t Size; |
| 1855 | |
| 1856 | public: |
| 1857 | MemorySizePredicateMatcher(unsigned InsnVarID, unsigned MMOIdx, unsigned Size) |
| 1858 | : InstructionPredicateMatcher(IPM_MemoryLLTSize, InsnVarID), |
| 1859 | MMOIdx(MMOIdx), Size(Size) {} |
| 1860 | |
| 1861 | static bool classof(const PredicateMatcher *P) { |
| 1862 | return P->getKind() == IPM_MemoryLLTSize; |
| 1863 | } |
| 1864 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1865 | return InstructionPredicateMatcher::isIdentical(B) && |
| 1866 | MMOIdx == cast<MemorySizePredicateMatcher>(&B)->MMOIdx && |
| 1867 | Size == cast<MemorySizePredicateMatcher>(&B)->Size; |
| 1868 | } |
| 1869 | |
| 1870 | void emitPredicateOpcodes(MatchTable &Table, |
| 1871 | RuleMatcher &Rule) const override { |
| 1872 | Table << MatchTable::Opcode("GIM_CheckMemorySizeEqualTo") |
| 1873 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1874 | << MatchTable::Comment("MMO") << MatchTable::IntValue(MMOIdx) |
| 1875 | << MatchTable::Comment("Size") << MatchTable::IntValue(Size) |
| 1876 | << MatchTable::LineBreak; |
| 1877 | } |
| 1878 | }; |
| 1879 | |
Matt Arsenault | d00d857 | 2019-07-15 20:59:42 +0000 | [diff] [blame] | 1880 | class MemoryAddressSpacePredicateMatcher : public InstructionPredicateMatcher { |
| 1881 | protected: |
| 1882 | unsigned MMOIdx; |
| 1883 | SmallVector<unsigned, 4> AddrSpaces; |
| 1884 | |
| 1885 | public: |
| 1886 | MemoryAddressSpacePredicateMatcher(unsigned InsnVarID, unsigned MMOIdx, |
| 1887 | ArrayRef<unsigned> AddrSpaces) |
| 1888 | : InstructionPredicateMatcher(IPM_MemoryAddressSpace, InsnVarID), |
| 1889 | MMOIdx(MMOIdx), AddrSpaces(AddrSpaces.begin(), AddrSpaces.end()) {} |
| 1890 | |
| 1891 | static bool classof(const PredicateMatcher *P) { |
| 1892 | return P->getKind() == IPM_MemoryAddressSpace; |
| 1893 | } |
| 1894 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1895 | if (!InstructionPredicateMatcher::isIdentical(B)) |
| 1896 | return false; |
| 1897 | auto *Other = cast<MemoryAddressSpacePredicateMatcher>(&B); |
| 1898 | return MMOIdx == Other->MMOIdx && AddrSpaces == Other->AddrSpaces; |
| 1899 | } |
| 1900 | |
| 1901 | void emitPredicateOpcodes(MatchTable &Table, |
| 1902 | RuleMatcher &Rule) const override { |
| 1903 | Table << MatchTable::Opcode("GIM_CheckMemoryAddressSpace") |
| 1904 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1905 | << MatchTable::Comment("MMO") << MatchTable::IntValue(MMOIdx) |
| 1906 | // Encode number of address spaces to expect. |
| 1907 | << MatchTable::Comment("NumAddrSpace") |
| 1908 | << MatchTable::IntValue(AddrSpaces.size()); |
| 1909 | for (unsigned AS : AddrSpaces) |
| 1910 | Table << MatchTable::Comment("AddrSpace") << MatchTable::IntValue(AS); |
| 1911 | |
| 1912 | Table << MatchTable::LineBreak; |
| 1913 | } |
| 1914 | }; |
| 1915 | |
Matt Arsenault | 52c2624 | 2019-07-31 00:14:43 +0000 | [diff] [blame] | 1916 | class MemoryAlignmentPredicateMatcher : public InstructionPredicateMatcher { |
| 1917 | protected: |
| 1918 | unsigned MMOIdx; |
| 1919 | int MinAlign; |
| 1920 | |
| 1921 | public: |
| 1922 | MemoryAlignmentPredicateMatcher(unsigned InsnVarID, unsigned MMOIdx, |
| 1923 | int MinAlign) |
| 1924 | : InstructionPredicateMatcher(IPM_MemoryAlignment, InsnVarID), |
| 1925 | MMOIdx(MMOIdx), MinAlign(MinAlign) { |
| 1926 | assert(MinAlign > 0); |
| 1927 | } |
| 1928 | |
| 1929 | static bool classof(const PredicateMatcher *P) { |
| 1930 | return P->getKind() == IPM_MemoryAlignment; |
| 1931 | } |
| 1932 | |
| 1933 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1934 | if (!InstructionPredicateMatcher::isIdentical(B)) |
| 1935 | return false; |
| 1936 | auto *Other = cast<MemoryAlignmentPredicateMatcher>(&B); |
| 1937 | return MMOIdx == Other->MMOIdx && MinAlign == Other->MinAlign; |
| 1938 | } |
| 1939 | |
| 1940 | void emitPredicateOpcodes(MatchTable &Table, |
| 1941 | RuleMatcher &Rule) const override { |
| 1942 | Table << MatchTable::Opcode("GIM_CheckMemoryAlignment") |
| 1943 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1944 | << MatchTable::Comment("MMO") << MatchTable::IntValue(MMOIdx) |
| 1945 | << MatchTable::Comment("MinAlign") << MatchTable::IntValue(MinAlign) |
| 1946 | << MatchTable::LineBreak; |
| 1947 | } |
| 1948 | }; |
| 1949 | |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 1950 | /// Generates code to check that the size of an MMO is less-than, equal-to, or |
| 1951 | /// greater than a given LLT. |
| 1952 | class MemoryVsLLTSizePredicateMatcher : public InstructionPredicateMatcher { |
| 1953 | public: |
| 1954 | enum RelationKind { |
| 1955 | GreaterThan, |
| 1956 | EqualTo, |
| 1957 | LessThan, |
| 1958 | }; |
| 1959 | |
| 1960 | protected: |
| 1961 | unsigned MMOIdx; |
| 1962 | RelationKind Relation; |
| 1963 | unsigned OpIdx; |
| 1964 | |
| 1965 | public: |
| 1966 | MemoryVsLLTSizePredicateMatcher(unsigned InsnVarID, unsigned MMOIdx, |
| 1967 | enum RelationKind Relation, |
| 1968 | unsigned OpIdx) |
| 1969 | : InstructionPredicateMatcher(IPM_MemoryVsLLTSize, InsnVarID), |
| 1970 | MMOIdx(MMOIdx), Relation(Relation), OpIdx(OpIdx) {} |
| 1971 | |
| 1972 | static bool classof(const PredicateMatcher *P) { |
| 1973 | return P->getKind() == IPM_MemoryVsLLTSize; |
| 1974 | } |
| 1975 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1976 | return InstructionPredicateMatcher::isIdentical(B) && |
| 1977 | MMOIdx == cast<MemoryVsLLTSizePredicateMatcher>(&B)->MMOIdx && |
| 1978 | Relation == cast<MemoryVsLLTSizePredicateMatcher>(&B)->Relation && |
| 1979 | OpIdx == cast<MemoryVsLLTSizePredicateMatcher>(&B)->OpIdx; |
| 1980 | } |
| 1981 | |
| 1982 | void emitPredicateOpcodes(MatchTable &Table, |
| 1983 | RuleMatcher &Rule) const override { |
| 1984 | Table << MatchTable::Opcode(Relation == EqualTo |
| 1985 | ? "GIM_CheckMemorySizeEqualToLLT" |
| 1986 | : Relation == GreaterThan |
| 1987 | ? "GIM_CheckMemorySizeGreaterThanLLT" |
| 1988 | : "GIM_CheckMemorySizeLessThanLLT") |
| 1989 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1990 | << MatchTable::Comment("MMO") << MatchTable::IntValue(MMOIdx) |
| 1991 | << MatchTable::Comment("OpIdx") << MatchTable::IntValue(OpIdx) |
| 1992 | << MatchTable::LineBreak; |
| 1993 | } |
| 1994 | }; |
| 1995 | |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 1996 | /// Generates code to check an arbitrary C++ instruction predicate. |
| 1997 | class GenericInstructionPredicateMatcher : public InstructionPredicateMatcher { |
| 1998 | protected: |
| 1999 | TreePredicateFn Predicate; |
| 2000 | |
| 2001 | public: |
| 2002 | GenericInstructionPredicateMatcher(unsigned InsnVarID, |
| 2003 | TreePredicateFn Predicate) |
| 2004 | : InstructionPredicateMatcher(IPM_GenericPredicate, InsnVarID), |
| 2005 | Predicate(Predicate) {} |
| 2006 | |
| 2007 | static bool classof(const InstructionPredicateMatcher *P) { |
| 2008 | return P->getKind() == IPM_GenericPredicate; |
| 2009 | } |
Daniel Sanders | 06f4ff1 | 2018-09-25 17:59:02 +0000 | [diff] [blame] | 2010 | bool isIdentical(const PredicateMatcher &B) const override { |
| 2011 | return InstructionPredicateMatcher::isIdentical(B) && |
| 2012 | Predicate == |
| 2013 | static_cast<const GenericInstructionPredicateMatcher &>(B) |
| 2014 | .Predicate; |
| 2015 | } |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 2016 | void emitPredicateOpcodes(MatchTable &Table, |
| 2017 | RuleMatcher &Rule) const override { |
| 2018 | Table << MatchTable::Opcode("GIM_CheckCxxInsnPredicate") |
| 2019 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 2020 | << MatchTable::Comment("FnId") |
| 2021 | << MatchTable::NamedValue(getEnumNameForPredicate(Predicate)) |
| 2022 | << MatchTable::LineBreak; |
| 2023 | } |
| 2024 | }; |
| 2025 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 2026 | /// Generates code to check that a set of predicates and operands match for a |
| 2027 | /// particular instruction. |
| 2028 | /// |
| 2029 | /// Typical predicates include: |
| 2030 | /// * Has a specific opcode. |
| 2031 | /// * Has an nsw/nuw flag or doesn't. |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2032 | class InstructionMatcher final : public PredicateListMatcher<PredicateMatcher> { |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 2033 | protected: |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 2034 | typedef std::vector<std::unique_ptr<OperandMatcher>> OperandVec; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2035 | |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 2036 | RuleMatcher &Rule; |
| 2037 | |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2038 | /// The operands to match. All rendered operands must be present even if the |
| 2039 | /// condition is always true. |
| 2040 | OperandVec Operands; |
Roman Tereshin | 19da667 | 2018-05-22 04:31:50 +0000 | [diff] [blame] | 2041 | bool NumOperandsCheck = true; |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 2042 | |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2043 | std::string SymbolicName; |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 2044 | unsigned InsnVarID; |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2045 | |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 2046 | /// PhysRegInputs - List list has an entry for each explicitly specified |
| 2047 | /// physreg input to the pattern. The first elt is the Register node, the |
| 2048 | /// second is the recorded slot number the input pattern match saved it in. |
| 2049 | SmallVector<std::pair<Record *, unsigned>, 2> PhysRegInputs; |
| 2050 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 2051 | public: |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 2052 | InstructionMatcher(RuleMatcher &Rule, StringRef SymbolicName) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 2053 | : Rule(Rule), SymbolicName(SymbolicName) { |
| 2054 | // We create a new instruction matcher. |
| 2055 | // Get a new ID for that instruction. |
| 2056 | InsnVarID = Rule.implicitlyDefineInsnVar(*this); |
| 2057 | } |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 2058 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2059 | /// Construct a new instruction predicate and add it to the matcher. |
| 2060 | template <class Kind, class... Args> |
| 2061 | Optional<Kind *> addPredicate(Args &&... args) { |
| 2062 | Predicates.emplace_back( |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 2063 | std::make_unique<Kind>(getInsnVarID(), std::forward<Args>(args)...)); |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2064 | return static_cast<Kind *>(Predicates.back().get()); |
| 2065 | } |
| 2066 | |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 2067 | RuleMatcher &getRuleMatcher() const { return Rule; } |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2068 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2069 | unsigned getInsnVarID() const { return InsnVarID; } |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 2070 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 2071 | /// Add an operand to the matcher. |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 2072 | OperandMatcher &addOperand(unsigned OpIdx, const std::string &SymbolicName, |
| 2073 | unsigned AllocatedTemporariesBaseID) { |
| 2074 | Operands.emplace_back(new OperandMatcher(*this, OpIdx, SymbolicName, |
| 2075 | AllocatedTemporariesBaseID)); |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 2076 | if (!SymbolicName.empty()) |
| 2077 | Rule.defineOperand(SymbolicName, *Operands.back()); |
| 2078 | |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 2079 | return *Operands.back(); |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 2080 | } |
| 2081 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2082 | OperandMatcher &getOperand(unsigned OpIdx) { |
| 2083 | auto I = std::find_if(Operands.begin(), Operands.end(), |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 2084 | [&OpIdx](const std::unique_ptr<OperandMatcher> &X) { |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2085 | return X->getOpIdx() == OpIdx; |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2086 | }); |
| 2087 | if (I != Operands.end()) |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 2088 | return **I; |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2089 | llvm_unreachable("Failed to lookup operand"); |
| 2090 | } |
| 2091 | |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 2092 | OperandMatcher &addPhysRegInput(Record *Reg, unsigned OpIdx, |
| 2093 | unsigned TempOpIdx) { |
| 2094 | assert(SymbolicName.empty()); |
| 2095 | OperandMatcher *OM = new OperandMatcher(*this, OpIdx, "", TempOpIdx); |
| 2096 | Operands.emplace_back(OM); |
| 2097 | Rule.definePhysRegOperand(Reg, *OM); |
| 2098 | PhysRegInputs.emplace_back(Reg, OpIdx); |
| 2099 | return *OM; |
| 2100 | } |
| 2101 | |
| 2102 | ArrayRef<std::pair<Record *, unsigned>> getPhysRegInputs() const { |
| 2103 | return PhysRegInputs; |
| 2104 | } |
| 2105 | |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2106 | StringRef getSymbolicName() const { return SymbolicName; } |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2107 | unsigned getNumOperands() const { return Operands.size(); } |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 2108 | OperandVec::iterator operands_begin() { return Operands.begin(); } |
| 2109 | OperandVec::iterator operands_end() { return Operands.end(); } |
| 2110 | iterator_range<OperandVec::iterator> operands() { |
| 2111 | return make_range(operands_begin(), operands_end()); |
| 2112 | } |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2113 | OperandVec::const_iterator operands_begin() const { return Operands.begin(); } |
| 2114 | OperandVec::const_iterator operands_end() const { return Operands.end(); } |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2115 | iterator_range<OperandVec::const_iterator> operands() const { |
| 2116 | return make_range(operands_begin(), operands_end()); |
| 2117 | } |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 2118 | bool operands_empty() const { return Operands.empty(); } |
| 2119 | |
| 2120 | void pop_front() { Operands.erase(Operands.begin()); } |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2121 | |
Roman Tereshin | 19da667 | 2018-05-22 04:31:50 +0000 | [diff] [blame] | 2122 | void optimize(); |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2123 | |
| 2124 | /// Emit MatchTable opcodes that test whether the instruction named in |
| 2125 | /// InsnVarName matches all the predicates and all the operands. |
| 2126 | void emitPredicateOpcodes(MatchTable &Table, RuleMatcher &Rule) { |
Roman Tereshin | 19da667 | 2018-05-22 04:31:50 +0000 | [diff] [blame] | 2127 | if (NumOperandsCheck) |
| 2128 | InstructionNumOperandsMatcher(InsnVarID, getNumOperands()) |
| 2129 | .emitPredicateOpcodes(Table, Rule); |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 2130 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 2131 | emitPredicateListOpcodes(Table, Rule); |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2132 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 2133 | for (const auto &Operand : Operands) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 2134 | Operand->emitPredicateOpcodes(Table, Rule); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2135 | } |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 2136 | |
| 2137 | /// Compare the priority of this object and B. |
| 2138 | /// |
| 2139 | /// Returns true if this object is more important than B. |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2140 | bool isHigherPriorityThan(InstructionMatcher &B) { |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 2141 | // Instruction matchers involving more operands have higher priority. |
| 2142 | if (Operands.size() > B.Operands.size()) |
| 2143 | return true; |
| 2144 | if (Operands.size() < B.Operands.size()) |
| 2145 | return false; |
| 2146 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2147 | for (auto &&P : zip(predicates(), B.predicates())) { |
| 2148 | auto L = static_cast<InstructionPredicateMatcher *>(std::get<0>(P).get()); |
| 2149 | auto R = static_cast<InstructionPredicateMatcher *>(std::get<1>(P).get()); |
| 2150 | if (L->isHigherPriorityThan(*R)) |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 2151 | return true; |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2152 | if (R->isHigherPriorityThan(*L)) |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 2153 | return false; |
| 2154 | } |
| 2155 | |
Mark de Wever | e8d448e | 2019-12-22 18:58:32 +0100 | [diff] [blame] | 2156 | for (auto Operand : zip(Operands, B.Operands)) { |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 2157 | if (std::get<0>(Operand)->isHigherPriorityThan(*std::get<1>(Operand))) |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 2158 | return true; |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 2159 | if (std::get<1>(Operand)->isHigherPriorityThan(*std::get<0>(Operand))) |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 2160 | return false; |
| 2161 | } |
| 2162 | |
| 2163 | return false; |
| 2164 | }; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 2165 | |
| 2166 | /// Report the maximum number of temporary operands needed by the instruction |
| 2167 | /// matcher. |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2168 | unsigned countRendererFns() { |
| 2169 | return std::accumulate( |
| 2170 | predicates().begin(), predicates().end(), 0, |
| 2171 | [](unsigned A, |
| 2172 | const std::unique_ptr<PredicateMatcher> &Predicate) { |
| 2173 | return A + Predicate->countRendererFns(); |
| 2174 | }) + |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 2175 | std::accumulate( |
| 2176 | Operands.begin(), Operands.end(), 0, |
| 2177 | [](unsigned A, const std::unique_ptr<OperandMatcher> &Operand) { |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 2178 | return A + Operand->countRendererFns(); |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 2179 | }); |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 2180 | } |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2181 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2182 | InstructionOpcodeMatcher &getOpcodeMatcher() { |
| 2183 | for (auto &P : predicates()) |
| 2184 | if (auto *OpMatcher = dyn_cast<InstructionOpcodeMatcher>(P.get())) |
| 2185 | return *OpMatcher; |
| 2186 | llvm_unreachable("Didn't find an opcode matcher"); |
| 2187 | } |
| 2188 | |
| 2189 | bool isConstantInstruction() { |
| 2190 | return getOpcodeMatcher().isConstantInstruction(); |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2191 | } |
Roman Tereshin | 19da667 | 2018-05-22 04:31:50 +0000 | [diff] [blame] | 2192 | |
| 2193 | StringRef getOpcode() { return getOpcodeMatcher().getOpcode(); } |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2194 | }; |
| 2195 | |
Roman Tereshin | 19da667 | 2018-05-22 04:31:50 +0000 | [diff] [blame] | 2196 | StringRef RuleMatcher::getOpcode() const { |
| 2197 | return Matchers.front()->getOpcode(); |
| 2198 | } |
| 2199 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2200 | unsigned RuleMatcher::getNumOperands() const { |
| 2201 | return Matchers.front()->getNumOperands(); |
| 2202 | } |
| 2203 | |
Roman Tereshin | 9a9fa49 | 2018-05-23 21:30:16 +0000 | [diff] [blame] | 2204 | LLTCodeGen RuleMatcher::getFirstConditionAsRootType() { |
| 2205 | InstructionMatcher &InsnMatcher = *Matchers.front(); |
| 2206 | if (!InsnMatcher.predicates_empty()) |
| 2207 | if (const auto *TM = |
| 2208 | dyn_cast<LLTOperandMatcher>(&**InsnMatcher.predicates_begin())) |
| 2209 | if (TM->getInsnVarID() == 0 && TM->getOpIdx() == 0) |
| 2210 | return TM->getTy(); |
| 2211 | return {}; |
| 2212 | } |
| 2213 | |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 2214 | /// Generates code to check that the operand is a register defined by an |
| 2215 | /// instruction that matches the given instruction matcher. |
| 2216 | /// |
| 2217 | /// For example, the pattern: |
| 2218 | /// (set $dst, (G_MUL (G_ADD $src1, $src2), $src3)) |
| 2219 | /// would use an InstructionOperandMatcher for operand 1 of the G_MUL to match |
| 2220 | /// the: |
| 2221 | /// (G_ADD $src1, $src2) |
| 2222 | /// subpattern. |
| 2223 | class InstructionOperandMatcher : public OperandPredicateMatcher { |
| 2224 | protected: |
| 2225 | std::unique_ptr<InstructionMatcher> InsnMatcher; |
| 2226 | |
| 2227 | public: |
Quentin Colombet | eba10cb | 2017-12-18 22:12:13 +0000 | [diff] [blame] | 2228 | InstructionOperandMatcher(unsigned InsnVarID, unsigned OpIdx, |
| 2229 | RuleMatcher &Rule, StringRef SymbolicName) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 2230 | : OperandPredicateMatcher(OPM_Instruction, InsnVarID, OpIdx), |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 2231 | InsnMatcher(new InstructionMatcher(Rule, SymbolicName)) {} |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 2232 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 2233 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 2234 | return P->getKind() == OPM_Instruction; |
| 2235 | } |
| 2236 | |
| 2237 | InstructionMatcher &getInsnMatcher() const { return *InsnMatcher; } |
| 2238 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2239 | void emitCaptureOpcodes(MatchTable &Table, RuleMatcher &Rule) const { |
| 2240 | const unsigned NewInsnVarID = InsnMatcher->getInsnVarID(); |
| 2241 | Table << MatchTable::Opcode("GIM_RecordInsn") |
| 2242 | << MatchTable::Comment("DefineMI") |
| 2243 | << MatchTable::IntValue(NewInsnVarID) << MatchTable::Comment("MI") |
| 2244 | << MatchTable::IntValue(getInsnVarID()) |
| 2245 | << MatchTable::Comment("OpIdx") << MatchTable::IntValue(getOpIdx()) |
| 2246 | << MatchTable::Comment("MIs[" + llvm::to_string(NewInsnVarID) + "]") |
| 2247 | << MatchTable::LineBreak; |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 2248 | } |
| 2249 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 2250 | void emitPredicateOpcodes(MatchTable &Table, |
| 2251 | RuleMatcher &Rule) const override { |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2252 | emitCaptureOpcodes(Table, Rule); |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 2253 | InsnMatcher->emitPredicateOpcodes(Table, Rule); |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 2254 | } |
Daniel Sanders | 12e6e70 | 2018-01-17 20:34:29 +0000 | [diff] [blame] | 2255 | |
| 2256 | bool isHigherPriorityThan(const OperandPredicateMatcher &B) const override { |
| 2257 | if (OperandPredicateMatcher::isHigherPriorityThan(B)) |
| 2258 | return true; |
| 2259 | if (B.OperandPredicateMatcher::isHigherPriorityThan(*this)) |
| 2260 | return false; |
| 2261 | |
| 2262 | if (const InstructionOperandMatcher *BP = |
| 2263 | dyn_cast<InstructionOperandMatcher>(&B)) |
| 2264 | if (InsnMatcher->isHigherPriorityThan(*BP->InsnMatcher)) |
| 2265 | return true; |
| 2266 | return false; |
| 2267 | } |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 2268 | }; |
| 2269 | |
Roman Tereshin | 19da667 | 2018-05-22 04:31:50 +0000 | [diff] [blame] | 2270 | void InstructionMatcher::optimize() { |
| 2271 | SmallVector<std::unique_ptr<PredicateMatcher>, 8> Stash; |
| 2272 | const auto &OpcMatcher = getOpcodeMatcher(); |
| 2273 | |
| 2274 | Stash.push_back(predicates_pop_front()); |
| 2275 | if (Stash.back().get() == &OpcMatcher) { |
Roman Tereshin | 6082a06 | 2019-10-30 20:58:46 -0700 | [diff] [blame] | 2276 | if (NumOperandsCheck && OpcMatcher.isVariadicNumOperands()) |
Roman Tereshin | 19da667 | 2018-05-22 04:31:50 +0000 | [diff] [blame] | 2277 | Stash.emplace_back( |
| 2278 | new InstructionNumOperandsMatcher(InsnVarID, getNumOperands())); |
| 2279 | NumOperandsCheck = false; |
Roman Tereshin | fedae33 | 2018-05-23 02:04:19 +0000 | [diff] [blame] | 2280 | |
| 2281 | for (auto &OM : Operands) |
| 2282 | for (auto &OP : OM->predicates()) |
| 2283 | if (isa<IntrinsicIDOperandMatcher>(OP)) { |
| 2284 | Stash.push_back(std::move(OP)); |
| 2285 | OM->eraseNullPredicates(); |
| 2286 | break; |
| 2287 | } |
Roman Tereshin | 19da667 | 2018-05-22 04:31:50 +0000 | [diff] [blame] | 2288 | } |
| 2289 | |
| 2290 | if (InsnVarID > 0) { |
| 2291 | assert(!Operands.empty() && "Nested instruction is expected to def a vreg"); |
| 2292 | for (auto &OP : Operands[0]->predicates()) |
| 2293 | OP.reset(); |
| 2294 | Operands[0]->eraseNullPredicates(); |
| 2295 | } |
Roman Tereshin | b1ba127 | 2018-05-23 19:16:59 +0000 | [diff] [blame] | 2296 | for (auto &OM : Operands) { |
| 2297 | for (auto &OP : OM->predicates()) |
| 2298 | if (isa<LLTOperandMatcher>(OP)) |
| 2299 | Stash.push_back(std::move(OP)); |
| 2300 | OM->eraseNullPredicates(); |
| 2301 | } |
Roman Tereshin | 19da667 | 2018-05-22 04:31:50 +0000 | [diff] [blame] | 2302 | while (!Stash.empty()) |
| 2303 | prependPredicate(Stash.pop_back_val()); |
| 2304 | } |
| 2305 | |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 2306 | //===- Actions ------------------------------------------------------------===// |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2307 | class OperandRenderer { |
| 2308 | public: |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2309 | enum RendererKind { |
| 2310 | OR_Copy, |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 2311 | OR_CopyOrAddZeroReg, |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2312 | OR_CopySubReg, |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 2313 | OR_CopyPhysReg, |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2314 | OR_CopyConstantAsImm, |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 2315 | OR_CopyFConstantAsFPImm, |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2316 | OR_Imm, |
Matt Arsenault | 4a23ae5 | 2019-09-10 17:57:33 +0000 | [diff] [blame] | 2317 | OR_SubRegIndex, |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2318 | OR_Register, |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 2319 | OR_TempRegister, |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 2320 | OR_ComplexPattern, |
| 2321 | OR_Custom |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2322 | }; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2323 | |
| 2324 | protected: |
| 2325 | RendererKind Kind; |
| 2326 | |
| 2327 | public: |
| 2328 | OperandRenderer(RendererKind Kind) : Kind(Kind) {} |
| 2329 | virtual ~OperandRenderer() {} |
| 2330 | |
| 2331 | RendererKind getKind() const { return Kind; } |
| 2332 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2333 | virtual void emitRenderOpcodes(MatchTable &Table, |
| 2334 | RuleMatcher &Rule) const = 0; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2335 | }; |
| 2336 | |
| 2337 | /// A CopyRenderer emits code to copy a single operand from an existing |
| 2338 | /// instruction to the one being built. |
| 2339 | class CopyRenderer : public OperandRenderer { |
| 2340 | protected: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2341 | unsigned NewInsnID; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2342 | /// The name of the operand. |
| 2343 | const StringRef SymbolicName; |
| 2344 | |
| 2345 | public: |
Daniel Sanders | bd83ad4 | 2017-10-24 01:48:34 +0000 | [diff] [blame] | 2346 | CopyRenderer(unsigned NewInsnID, StringRef SymbolicName) |
| 2347 | : OperandRenderer(OR_Copy), NewInsnID(NewInsnID), |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2348 | SymbolicName(SymbolicName) { |
| 2349 | assert(!SymbolicName.empty() && "Cannot copy from an unspecified source"); |
| 2350 | } |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2351 | |
| 2352 | static bool classof(const OperandRenderer *R) { |
| 2353 | return R->getKind() == OR_Copy; |
| 2354 | } |
| 2355 | |
| 2356 | const StringRef getSymbolicName() const { return SymbolicName; } |
| 2357 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2358 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 2359 | const OperandMatcher &Operand = Rule.getOperandMatcher(SymbolicName); |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2360 | unsigned OldInsnVarID = Rule.getInsnVarID(Operand.getInstructionMatcher()); |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2361 | Table << MatchTable::Opcode("GIR_Copy") << MatchTable::Comment("NewInsnID") |
| 2362 | << MatchTable::IntValue(NewInsnID) << MatchTable::Comment("OldInsnID") |
| 2363 | << MatchTable::IntValue(OldInsnVarID) << MatchTable::Comment("OpIdx") |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2364 | << MatchTable::IntValue(Operand.getOpIdx()) |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2365 | << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2366 | } |
| 2367 | }; |
| 2368 | |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 2369 | /// A CopyRenderer emits code to copy a virtual register to a specific physical |
| 2370 | /// register. |
| 2371 | class CopyPhysRegRenderer : public OperandRenderer { |
| 2372 | protected: |
| 2373 | unsigned NewInsnID; |
| 2374 | Record *PhysReg; |
| 2375 | |
| 2376 | public: |
| 2377 | CopyPhysRegRenderer(unsigned NewInsnID, Record *Reg) |
| 2378 | : OperandRenderer(OR_CopyPhysReg), NewInsnID(NewInsnID), |
| 2379 | PhysReg(Reg) { |
| 2380 | assert(PhysReg); |
| 2381 | } |
| 2382 | |
| 2383 | static bool classof(const OperandRenderer *R) { |
| 2384 | return R->getKind() == OR_CopyPhysReg; |
| 2385 | } |
| 2386 | |
| 2387 | Record *getPhysReg() const { return PhysReg; } |
| 2388 | |
| 2389 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
| 2390 | const OperandMatcher &Operand = Rule.getPhysRegOperandMatcher(PhysReg); |
| 2391 | unsigned OldInsnVarID = Rule.getInsnVarID(Operand.getInstructionMatcher()); |
| 2392 | Table << MatchTable::Opcode("GIR_Copy") << MatchTable::Comment("NewInsnID") |
| 2393 | << MatchTable::IntValue(NewInsnID) << MatchTable::Comment("OldInsnID") |
| 2394 | << MatchTable::IntValue(OldInsnVarID) << MatchTable::Comment("OpIdx") |
| 2395 | << MatchTable::IntValue(Operand.getOpIdx()) |
| 2396 | << MatchTable::Comment(PhysReg->getName()) |
| 2397 | << MatchTable::LineBreak; |
| 2398 | } |
| 2399 | }; |
| 2400 | |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 2401 | /// A CopyOrAddZeroRegRenderer emits code to copy a single operand from an |
| 2402 | /// existing instruction to the one being built. If the operand turns out to be |
| 2403 | /// a 'G_CONSTANT 0' then it replaces the operand with a zero register. |
| 2404 | class CopyOrAddZeroRegRenderer : public OperandRenderer { |
| 2405 | protected: |
| 2406 | unsigned NewInsnID; |
| 2407 | /// The name of the operand. |
| 2408 | const StringRef SymbolicName; |
| 2409 | const Record *ZeroRegisterDef; |
| 2410 | |
| 2411 | public: |
| 2412 | CopyOrAddZeroRegRenderer(unsigned NewInsnID, |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 2413 | StringRef SymbolicName, Record *ZeroRegisterDef) |
| 2414 | : OperandRenderer(OR_CopyOrAddZeroReg), NewInsnID(NewInsnID), |
| 2415 | SymbolicName(SymbolicName), ZeroRegisterDef(ZeroRegisterDef) { |
| 2416 | assert(!SymbolicName.empty() && "Cannot copy from an unspecified source"); |
| 2417 | } |
| 2418 | |
| 2419 | static bool classof(const OperandRenderer *R) { |
| 2420 | return R->getKind() == OR_CopyOrAddZeroReg; |
| 2421 | } |
| 2422 | |
| 2423 | const StringRef getSymbolicName() const { return SymbolicName; } |
| 2424 | |
| 2425 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
| 2426 | const OperandMatcher &Operand = Rule.getOperandMatcher(SymbolicName); |
| 2427 | unsigned OldInsnVarID = Rule.getInsnVarID(Operand.getInstructionMatcher()); |
| 2428 | Table << MatchTable::Opcode("GIR_CopyOrAddZeroReg") |
| 2429 | << MatchTable::Comment("NewInsnID") << MatchTable::IntValue(NewInsnID) |
| 2430 | << MatchTable::Comment("OldInsnID") |
| 2431 | << MatchTable::IntValue(OldInsnVarID) << MatchTable::Comment("OpIdx") |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2432 | << MatchTable::IntValue(Operand.getOpIdx()) |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 2433 | << MatchTable::NamedValue( |
| 2434 | (ZeroRegisterDef->getValue("Namespace") |
| 2435 | ? ZeroRegisterDef->getValueAsString("Namespace") |
| 2436 | : ""), |
| 2437 | ZeroRegisterDef->getName()) |
| 2438 | << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak; |
| 2439 | } |
| 2440 | }; |
| 2441 | |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2442 | /// A CopyConstantAsImmRenderer emits code to render a G_CONSTANT instruction to |
| 2443 | /// an extended immediate operand. |
| 2444 | class CopyConstantAsImmRenderer : public OperandRenderer { |
| 2445 | protected: |
| 2446 | unsigned NewInsnID; |
| 2447 | /// The name of the operand. |
| 2448 | const std::string SymbolicName; |
| 2449 | bool Signed; |
| 2450 | |
| 2451 | public: |
| 2452 | CopyConstantAsImmRenderer(unsigned NewInsnID, StringRef SymbolicName) |
| 2453 | : OperandRenderer(OR_CopyConstantAsImm), NewInsnID(NewInsnID), |
| 2454 | SymbolicName(SymbolicName), Signed(true) {} |
| 2455 | |
| 2456 | static bool classof(const OperandRenderer *R) { |
| 2457 | return R->getKind() == OR_CopyConstantAsImm; |
| 2458 | } |
| 2459 | |
| 2460 | const StringRef getSymbolicName() const { return SymbolicName; } |
| 2461 | |
| 2462 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2463 | InstructionMatcher &InsnMatcher = Rule.getInstructionMatcher(SymbolicName); |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2464 | unsigned OldInsnVarID = Rule.getInsnVarID(InsnMatcher); |
| 2465 | Table << MatchTable::Opcode(Signed ? "GIR_CopyConstantAsSImm" |
| 2466 | : "GIR_CopyConstantAsUImm") |
| 2467 | << MatchTable::Comment("NewInsnID") << MatchTable::IntValue(NewInsnID) |
| 2468 | << MatchTable::Comment("OldInsnID") |
| 2469 | << MatchTable::IntValue(OldInsnVarID) |
| 2470 | << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak; |
| 2471 | } |
| 2472 | }; |
| 2473 | |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 2474 | /// A CopyFConstantAsFPImmRenderer emits code to render a G_FCONSTANT |
| 2475 | /// instruction to an extended immediate operand. |
| 2476 | class CopyFConstantAsFPImmRenderer : public OperandRenderer { |
| 2477 | protected: |
| 2478 | unsigned NewInsnID; |
| 2479 | /// The name of the operand. |
| 2480 | const std::string SymbolicName; |
| 2481 | |
| 2482 | public: |
| 2483 | CopyFConstantAsFPImmRenderer(unsigned NewInsnID, StringRef SymbolicName) |
| 2484 | : OperandRenderer(OR_CopyFConstantAsFPImm), NewInsnID(NewInsnID), |
| 2485 | SymbolicName(SymbolicName) {} |
| 2486 | |
| 2487 | static bool classof(const OperandRenderer *R) { |
| 2488 | return R->getKind() == OR_CopyFConstantAsFPImm; |
| 2489 | } |
| 2490 | |
| 2491 | const StringRef getSymbolicName() const { return SymbolicName; } |
| 2492 | |
| 2493 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2494 | InstructionMatcher &InsnMatcher = Rule.getInstructionMatcher(SymbolicName); |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 2495 | unsigned OldInsnVarID = Rule.getInsnVarID(InsnMatcher); |
| 2496 | Table << MatchTable::Opcode("GIR_CopyFConstantAsFPImm") |
| 2497 | << MatchTable::Comment("NewInsnID") << MatchTable::IntValue(NewInsnID) |
| 2498 | << MatchTable::Comment("OldInsnID") |
| 2499 | << MatchTable::IntValue(OldInsnVarID) |
| 2500 | << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak; |
| 2501 | } |
| 2502 | }; |
| 2503 | |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2504 | /// A CopySubRegRenderer emits code to copy a single register operand from an |
| 2505 | /// existing instruction to the one being built and indicate that only a |
| 2506 | /// subregister should be copied. |
| 2507 | class CopySubRegRenderer : public OperandRenderer { |
| 2508 | protected: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2509 | unsigned NewInsnID; |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2510 | /// The name of the operand. |
| 2511 | const StringRef SymbolicName; |
| 2512 | /// The subregister to extract. |
| 2513 | const CodeGenSubRegIndex *SubReg; |
| 2514 | |
| 2515 | public: |
Daniel Sanders | bd83ad4 | 2017-10-24 01:48:34 +0000 | [diff] [blame] | 2516 | CopySubRegRenderer(unsigned NewInsnID, StringRef SymbolicName, |
| 2517 | const CodeGenSubRegIndex *SubReg) |
| 2518 | : OperandRenderer(OR_CopySubReg), NewInsnID(NewInsnID), |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2519 | SymbolicName(SymbolicName), SubReg(SubReg) {} |
| 2520 | |
| 2521 | static bool classof(const OperandRenderer *R) { |
| 2522 | return R->getKind() == OR_CopySubReg; |
| 2523 | } |
| 2524 | |
| 2525 | const StringRef getSymbolicName() const { return SymbolicName; } |
| 2526 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2527 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 2528 | const OperandMatcher &Operand = Rule.getOperandMatcher(SymbolicName); |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2529 | unsigned OldInsnVarID = Rule.getInsnVarID(Operand.getInstructionMatcher()); |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2530 | Table << MatchTable::Opcode("GIR_CopySubReg") |
| 2531 | << MatchTable::Comment("NewInsnID") << MatchTable::IntValue(NewInsnID) |
| 2532 | << MatchTable::Comment("OldInsnID") |
| 2533 | << MatchTable::IntValue(OldInsnVarID) << MatchTable::Comment("OpIdx") |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2534 | << MatchTable::IntValue(Operand.getOpIdx()) |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2535 | << MatchTable::Comment("SubRegIdx") |
| 2536 | << MatchTable::IntValue(SubReg->EnumValue) |
| 2537 | << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak; |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2538 | } |
| 2539 | }; |
| 2540 | |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2541 | /// Adds a specific physical register to the instruction being built. |
| 2542 | /// This is typically useful for WZR/XZR on AArch64. |
| 2543 | class AddRegisterRenderer : public OperandRenderer { |
| 2544 | protected: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2545 | unsigned InsnID; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2546 | const Record *RegisterDef; |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 2547 | bool IsDef; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2548 | |
| 2549 | public: |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 2550 | AddRegisterRenderer(unsigned InsnID, const Record *RegisterDef, |
| 2551 | bool IsDef = false) |
| 2552 | : OperandRenderer(OR_Register), InsnID(InsnID), RegisterDef(RegisterDef), |
| 2553 | IsDef(IsDef) {} |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2554 | |
| 2555 | static bool classof(const OperandRenderer *R) { |
| 2556 | return R->getKind() == OR_Register; |
| 2557 | } |
| 2558 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2559 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
| 2560 | Table << MatchTable::Opcode("GIR_AddRegister") |
| 2561 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 2562 | << MatchTable::NamedValue( |
| 2563 | (RegisterDef->getValue("Namespace") |
| 2564 | ? RegisterDef->getValueAsString("Namespace") |
| 2565 | : ""), |
| 2566 | RegisterDef->getName()) |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 2567 | << MatchTable::Comment("AddRegisterRegFlags"); |
| 2568 | |
| 2569 | // TODO: This is encoded as a 64-bit element, but only 16 or 32-bits are |
| 2570 | // really needed for a physical register reference. We can pack the |
| 2571 | // register and flags in a single field. |
| 2572 | if (IsDef) |
| 2573 | Table << MatchTable::NamedValue("RegState::Define"); |
| 2574 | else |
| 2575 | Table << MatchTable::IntValue(0); |
| 2576 | Table << MatchTable::LineBreak; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2577 | } |
| 2578 | }; |
| 2579 | |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 2580 | /// Adds a specific temporary virtual register to the instruction being built. |
| 2581 | /// This is used to chain instructions together when emitting multiple |
| 2582 | /// instructions. |
| 2583 | class TempRegRenderer : public OperandRenderer { |
| 2584 | protected: |
| 2585 | unsigned InsnID; |
| 2586 | unsigned TempRegID; |
| 2587 | bool IsDef; |
| 2588 | |
| 2589 | public: |
| 2590 | TempRegRenderer(unsigned InsnID, unsigned TempRegID, bool IsDef = false) |
| 2591 | : OperandRenderer(OR_Register), InsnID(InsnID), TempRegID(TempRegID), |
| 2592 | IsDef(IsDef) {} |
| 2593 | |
| 2594 | static bool classof(const OperandRenderer *R) { |
| 2595 | return R->getKind() == OR_TempRegister; |
| 2596 | } |
| 2597 | |
| 2598 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
| 2599 | Table << MatchTable::Opcode("GIR_AddTempRegister") |
| 2600 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 2601 | << MatchTable::Comment("TempRegID") << MatchTable::IntValue(TempRegID) |
| 2602 | << MatchTable::Comment("TempRegFlags"); |
| 2603 | if (IsDef) |
| 2604 | Table << MatchTable::NamedValue("RegState::Define"); |
| 2605 | else |
| 2606 | Table << MatchTable::IntValue(0); |
| 2607 | Table << MatchTable::LineBreak; |
| 2608 | } |
| 2609 | }; |
| 2610 | |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 2611 | /// Adds a specific immediate to the instruction being built. |
| 2612 | class ImmRenderer : public OperandRenderer { |
| 2613 | protected: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2614 | unsigned InsnID; |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 2615 | int64_t Imm; |
| 2616 | |
| 2617 | public: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2618 | ImmRenderer(unsigned InsnID, int64_t Imm) |
| 2619 | : OperandRenderer(OR_Imm), InsnID(InsnID), Imm(Imm) {} |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 2620 | |
| 2621 | static bool classof(const OperandRenderer *R) { |
| 2622 | return R->getKind() == OR_Imm; |
| 2623 | } |
| 2624 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2625 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
| 2626 | Table << MatchTable::Opcode("GIR_AddImm") << MatchTable::Comment("InsnID") |
| 2627 | << MatchTable::IntValue(InsnID) << MatchTable::Comment("Imm") |
| 2628 | << MatchTable::IntValue(Imm) << MatchTable::LineBreak; |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 2629 | } |
| 2630 | }; |
| 2631 | |
Matt Arsenault | 4a23ae5 | 2019-09-10 17:57:33 +0000 | [diff] [blame] | 2632 | /// Adds an enum value for a subreg index to the instruction being built. |
| 2633 | class SubRegIndexRenderer : public OperandRenderer { |
| 2634 | protected: |
| 2635 | unsigned InsnID; |
| 2636 | const CodeGenSubRegIndex *SubRegIdx; |
| 2637 | |
| 2638 | public: |
| 2639 | SubRegIndexRenderer(unsigned InsnID, const CodeGenSubRegIndex *SRI) |
| 2640 | : OperandRenderer(OR_SubRegIndex), InsnID(InsnID), SubRegIdx(SRI) {} |
| 2641 | |
| 2642 | static bool classof(const OperandRenderer *R) { |
| 2643 | return R->getKind() == OR_SubRegIndex; |
| 2644 | } |
| 2645 | |
| 2646 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
| 2647 | Table << MatchTable::Opcode("GIR_AddImm") << MatchTable::Comment("InsnID") |
| 2648 | << MatchTable::IntValue(InsnID) << MatchTable::Comment("SubRegIndex") |
| 2649 | << MatchTable::IntValue(SubRegIdx->EnumValue) |
| 2650 | << MatchTable::LineBreak; |
| 2651 | } |
| 2652 | }; |
| 2653 | |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 2654 | /// Adds operands by calling a renderer function supplied by the ComplexPattern |
| 2655 | /// matcher function. |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 2656 | class RenderComplexPatternOperand : public OperandRenderer { |
| 2657 | private: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2658 | unsigned InsnID; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 2659 | const Record &TheDef; |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 2660 | /// The name of the operand. |
| 2661 | const StringRef SymbolicName; |
| 2662 | /// The renderer number. This must be unique within a rule since it's used to |
| 2663 | /// identify a temporary variable to hold the renderer function. |
| 2664 | unsigned RendererID; |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 2665 | /// When provided, this is the suboperand of the ComplexPattern operand to |
| 2666 | /// render. Otherwise all the suboperands will be rendered. |
| 2667 | Optional<unsigned> SubOperand; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 2668 | |
| 2669 | unsigned getNumOperands() const { |
| 2670 | return TheDef.getValueAsDag("Operands")->getNumArgs(); |
| 2671 | } |
| 2672 | |
| 2673 | public: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2674 | RenderComplexPatternOperand(unsigned InsnID, const Record &TheDef, |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 2675 | StringRef SymbolicName, unsigned RendererID, |
| 2676 | Optional<unsigned> SubOperand = None) |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2677 | : OperandRenderer(OR_ComplexPattern), InsnID(InsnID), TheDef(TheDef), |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 2678 | SymbolicName(SymbolicName), RendererID(RendererID), |
| 2679 | SubOperand(SubOperand) {} |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 2680 | |
| 2681 | static bool classof(const OperandRenderer *R) { |
| 2682 | return R->getKind() == OR_ComplexPattern; |
| 2683 | } |
| 2684 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2685 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 2686 | Table << MatchTable::Opcode(SubOperand.hasValue() ? "GIR_ComplexSubOperandRenderer" |
| 2687 | : "GIR_ComplexRenderer") |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2688 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 2689 | << MatchTable::Comment("RendererID") |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 2690 | << MatchTable::IntValue(RendererID); |
| 2691 | if (SubOperand.hasValue()) |
| 2692 | Table << MatchTable::Comment("SubOperand") |
| 2693 | << MatchTable::IntValue(SubOperand.getValue()); |
| 2694 | Table << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 2695 | } |
| 2696 | }; |
| 2697 | |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 2698 | class CustomRenderer : public OperandRenderer { |
| 2699 | protected: |
| 2700 | unsigned InsnID; |
| 2701 | const Record &Renderer; |
| 2702 | /// The name of the operand. |
| 2703 | const std::string SymbolicName; |
| 2704 | |
| 2705 | public: |
| 2706 | CustomRenderer(unsigned InsnID, const Record &Renderer, |
| 2707 | StringRef SymbolicName) |
| 2708 | : OperandRenderer(OR_Custom), InsnID(InsnID), Renderer(Renderer), |
| 2709 | SymbolicName(SymbolicName) {} |
| 2710 | |
| 2711 | static bool classof(const OperandRenderer *R) { |
| 2712 | return R->getKind() == OR_Custom; |
| 2713 | } |
| 2714 | |
| 2715 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2716 | InstructionMatcher &InsnMatcher = Rule.getInstructionMatcher(SymbolicName); |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 2717 | unsigned OldInsnVarID = Rule.getInsnVarID(InsnMatcher); |
| 2718 | Table << MatchTable::Opcode("GIR_CustomRenderer") |
| 2719 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 2720 | << MatchTable::Comment("OldInsnID") |
| 2721 | << MatchTable::IntValue(OldInsnVarID) |
| 2722 | << MatchTable::Comment("Renderer") |
| 2723 | << MatchTable::NamedValue( |
| 2724 | "GICR_" + Renderer.getValueAsString("RendererFn").str()) |
| 2725 | << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak; |
| 2726 | } |
| 2727 | }; |
| 2728 | |
Ahmed Bougacha | 56ca3a9 | 2017-02-04 00:47:10 +0000 | [diff] [blame] | 2729 | /// An action taken when all Matcher predicates succeeded for a parent rule. |
| 2730 | /// |
| 2731 | /// Typical actions include: |
| 2732 | /// * Changing the opcode of an instruction. |
| 2733 | /// * Adding an operand to an instruction. |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 2734 | class MatchAction { |
| 2735 | public: |
| 2736 | virtual ~MatchAction() {} |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2737 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2738 | /// Emit the MatchTable opcodes to implement the action. |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2739 | virtual void emitActionOpcodes(MatchTable &Table, |
| 2740 | RuleMatcher &Rule) const = 0; |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 2741 | }; |
| 2742 | |
Ahmed Bougacha | 9aa4c10 | 2017-02-04 00:47:08 +0000 | [diff] [blame] | 2743 | /// Generates a comment describing the matched rule being acted upon. |
| 2744 | class DebugCommentAction : public MatchAction { |
| 2745 | private: |
Daniel Sanders | 6ea17ed | 2017-10-31 18:07:03 +0000 | [diff] [blame] | 2746 | std::string S; |
Ahmed Bougacha | 9aa4c10 | 2017-02-04 00:47:08 +0000 | [diff] [blame] | 2747 | |
| 2748 | public: |
Daniel Sanders | 6ea17ed | 2017-10-31 18:07:03 +0000 | [diff] [blame] | 2749 | DebugCommentAction(StringRef S) : S(S) {} |
Ahmed Bougacha | 9aa4c10 | 2017-02-04 00:47:08 +0000 | [diff] [blame] | 2750 | |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2751 | void emitActionOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
Daniel Sanders | 6ea17ed | 2017-10-31 18:07:03 +0000 | [diff] [blame] | 2752 | Table << MatchTable::Comment(S) << MatchTable::LineBreak; |
Ahmed Bougacha | 9aa4c10 | 2017-02-04 00:47:08 +0000 | [diff] [blame] | 2753 | } |
| 2754 | }; |
| 2755 | |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2756 | /// Generates code to build an instruction or mutate an existing instruction |
| 2757 | /// into the desired instruction when this is possible. |
| 2758 | class BuildMIAction : public MatchAction { |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 2759 | private: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2760 | unsigned InsnID; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2761 | const CodeGenInstruction *I; |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2762 | InstructionMatcher *Matched; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2763 | std::vector<std::unique_ptr<OperandRenderer>> OperandRenderers; |
| 2764 | |
| 2765 | /// True if the instruction can be built solely by mutating the opcode. |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2766 | bool canMutate(RuleMatcher &Rule, const InstructionMatcher *Insn) const { |
| 2767 | if (!Insn) |
Daniel Sanders | ab1d119 | 2017-10-24 18:11:54 +0000 | [diff] [blame] | 2768 | return false; |
| 2769 | |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2770 | if (OperandRenderers.size() != Insn->getNumOperands()) |
Daniel Sanders | e9fdba3 | 2017-04-29 17:30:09 +0000 | [diff] [blame] | 2771 | return false; |
| 2772 | |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2773 | for (const auto &Renderer : enumerate(OperandRenderers)) { |
Zachary Turner | 309a088 | 2017-03-13 16:24:10 +0000 | [diff] [blame] | 2774 | if (const auto *Copy = dyn_cast<CopyRenderer>(&*Renderer.value())) { |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 2775 | const OperandMatcher &OM = Rule.getOperandMatcher(Copy->getSymbolicName()); |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2776 | if (Insn != &OM.getInstructionMatcher() || |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2777 | OM.getOpIdx() != Renderer.index()) |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2778 | return false; |
| 2779 | } else |
| 2780 | return false; |
| 2781 | } |
| 2782 | |
| 2783 | return true; |
| 2784 | } |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2785 | |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 2786 | public: |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2787 | BuildMIAction(unsigned InsnID, const CodeGenInstruction *I) |
| 2788 | : InsnID(InsnID), I(I), Matched(nullptr) {} |
| 2789 | |
Daniel Sanders | 0846452 | 2018-01-29 21:09:12 +0000 | [diff] [blame] | 2790 | unsigned getInsnID() const { return InsnID; } |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 2791 | const CodeGenInstruction *getCGI() const { return I; } |
| 2792 | |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2793 | void chooseInsnToMutate(RuleMatcher &Rule) { |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2794 | for (auto *MutateCandidate : Rule.mutatable_insns()) { |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2795 | if (canMutate(Rule, MutateCandidate)) { |
| 2796 | // Take the first one we're offered that we're able to mutate. |
| 2797 | Rule.reserveInsnMatcherForMutation(MutateCandidate); |
| 2798 | Matched = MutateCandidate; |
| 2799 | return; |
| 2800 | } |
| 2801 | } |
| 2802 | } |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 2803 | |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2804 | template <class Kind, class... Args> |
| 2805 | Kind &addRenderer(Args&&... args) { |
| 2806 | OperandRenderers.emplace_back( |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 2807 | std::make_unique<Kind>(InsnID, std::forward<Args>(args)...)); |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2808 | return *static_cast<Kind *>(OperandRenderers.back().get()); |
| 2809 | } |
| 2810 | |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2811 | void emitActionOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
| 2812 | if (Matched) { |
| 2813 | assert(canMutate(Rule, Matched) && |
| 2814 | "Arranged to mutate an insn that isn't mutatable"); |
| 2815 | |
| 2816 | unsigned RecycleInsnID = Rule.getInsnVarID(*Matched); |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2817 | Table << MatchTable::Opcode("GIR_MutateOpcode") |
| 2818 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 2819 | << MatchTable::Comment("RecycleInsnID") |
| 2820 | << MatchTable::IntValue(RecycleInsnID) |
| 2821 | << MatchTable::Comment("Opcode") |
| 2822 | << MatchTable::NamedValue(I->Namespace, I->TheDef->getName()) |
| 2823 | << MatchTable::LineBreak; |
Tim Northover | 4340d64 | 2017-03-20 21:58:23 +0000 | [diff] [blame] | 2824 | |
| 2825 | if (!I->ImplicitDefs.empty() || !I->ImplicitUses.empty()) { |
Tim Northover | 4340d64 | 2017-03-20 21:58:23 +0000 | [diff] [blame] | 2826 | for (auto Def : I->ImplicitDefs) { |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 2827 | auto Namespace = Def->getValue("Namespace") |
| 2828 | ? Def->getValueAsString("Namespace") |
| 2829 | : ""; |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2830 | Table << MatchTable::Opcode("GIR_AddImplicitDef") |
| 2831 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 2832 | << MatchTable::NamedValue(Namespace, Def->getName()) |
| 2833 | << MatchTable::LineBreak; |
Tim Northover | 4340d64 | 2017-03-20 21:58:23 +0000 | [diff] [blame] | 2834 | } |
| 2835 | for (auto Use : I->ImplicitUses) { |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 2836 | auto Namespace = Use->getValue("Namespace") |
| 2837 | ? Use->getValueAsString("Namespace") |
| 2838 | : ""; |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2839 | Table << MatchTable::Opcode("GIR_AddImplicitUse") |
| 2840 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 2841 | << MatchTable::NamedValue(Namespace, Use->getName()) |
| 2842 | << MatchTable::LineBreak; |
Tim Northover | 4340d64 | 2017-03-20 21:58:23 +0000 | [diff] [blame] | 2843 | } |
| 2844 | } |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2845 | return; |
| 2846 | } |
| 2847 | |
| 2848 | // TODO: Simple permutation looks like it could be almost as common as |
| 2849 | // mutation due to commutative operations. |
| 2850 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2851 | Table << MatchTable::Opcode("GIR_BuildMI") << MatchTable::Comment("InsnID") |
| 2852 | << MatchTable::IntValue(InsnID) << MatchTable::Comment("Opcode") |
| 2853 | << MatchTable::NamedValue(I->Namespace, I->TheDef->getName()) |
| 2854 | << MatchTable::LineBreak; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2855 | for (const auto &Renderer : OperandRenderers) |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2856 | Renderer->emitRenderOpcodes(Table, Rule); |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2857 | |
Florian Hahn | 3bc3ec6 | 2017-08-03 14:48:22 +0000 | [diff] [blame] | 2858 | if (I->mayLoad || I->mayStore) { |
| 2859 | Table << MatchTable::Opcode("GIR_MergeMemOperands") |
| 2860 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 2861 | << MatchTable::Comment("MergeInsnID's"); |
| 2862 | // Emit the ID's for all the instructions that are matched by this rule. |
| 2863 | // TODO: Limit this to matched instructions that mayLoad/mayStore or have |
| 2864 | // some other means of having a memoperand. Also limit this to |
| 2865 | // emitted instructions that expect to have a memoperand too. For |
| 2866 | // example, (G_SEXT (G_LOAD x)) that results in separate load and |
| 2867 | // sign-extend instructions shouldn't put the memoperand on the |
| 2868 | // sign-extend since it has no effect there. |
| 2869 | std::vector<unsigned> MergeInsnIDs; |
| 2870 | for (const auto &IDMatcherPair : Rule.defined_insn_vars()) |
| 2871 | MergeInsnIDs.push_back(IDMatcherPair.second); |
Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 2872 | llvm::sort(MergeInsnIDs); |
Florian Hahn | 3bc3ec6 | 2017-08-03 14:48:22 +0000 | [diff] [blame] | 2873 | for (const auto &MergeInsnID : MergeInsnIDs) |
| 2874 | Table << MatchTable::IntValue(MergeInsnID); |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2875 | Table << MatchTable::NamedValue("GIU_MergeMemOperands_EndOfList") |
| 2876 | << MatchTable::LineBreak; |
Florian Hahn | 3bc3ec6 | 2017-08-03 14:48:22 +0000 | [diff] [blame] | 2877 | } |
| 2878 | |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 2879 | // FIXME: This is a hack but it's sufficient for ISel. We'll need to do |
| 2880 | // better for combines. Particularly when there are multiple match |
| 2881 | // roots. |
| 2882 | if (InsnID == 0) |
| 2883 | Table << MatchTable::Opcode("GIR_EraseFromParent") |
| 2884 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 2885 | << MatchTable::LineBreak; |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2886 | } |
| 2887 | }; |
| 2888 | |
| 2889 | /// Generates code to constrain the operands of an output instruction to the |
| 2890 | /// register classes specified by the definition of that instruction. |
| 2891 | class ConstrainOperandsToDefinitionAction : public MatchAction { |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2892 | unsigned InsnID; |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2893 | |
| 2894 | public: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2895 | ConstrainOperandsToDefinitionAction(unsigned InsnID) : InsnID(InsnID) {} |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2896 | |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2897 | void emitActionOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2898 | Table << MatchTable::Opcode("GIR_ConstrainSelectedInstOperands") |
| 2899 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 2900 | << MatchTable::LineBreak; |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2901 | } |
| 2902 | }; |
| 2903 | |
| 2904 | /// Generates code to constrain the specified operand of an output instruction |
| 2905 | /// to the specified register class. |
| 2906 | class ConstrainOperandToRegClassAction : public MatchAction { |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2907 | unsigned InsnID; |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2908 | unsigned OpIdx; |
| 2909 | const CodeGenRegisterClass &RC; |
| 2910 | |
| 2911 | public: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2912 | ConstrainOperandToRegClassAction(unsigned InsnID, unsigned OpIdx, |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2913 | const CodeGenRegisterClass &RC) |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2914 | : InsnID(InsnID), OpIdx(OpIdx), RC(RC) {} |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2915 | |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2916 | void emitActionOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2917 | Table << MatchTable::Opcode("GIR_ConstrainOperandRC") |
| 2918 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 2919 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) |
| 2920 | << MatchTable::Comment("RC " + RC.getName()) |
| 2921 | << MatchTable::IntValue(RC.EnumValue) << MatchTable::LineBreak; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2922 | } |
| 2923 | }; |
| 2924 | |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 2925 | /// Generates code to create a temporary register which can be used to chain |
| 2926 | /// instructions together. |
| 2927 | class MakeTempRegisterAction : public MatchAction { |
| 2928 | private: |
| 2929 | LLTCodeGen Ty; |
| 2930 | unsigned TempRegID; |
| 2931 | |
| 2932 | public: |
| 2933 | MakeTempRegisterAction(const LLTCodeGen &Ty, unsigned TempRegID) |
Matt Arsenault | 4a23ae5 | 2019-09-10 17:57:33 +0000 | [diff] [blame] | 2934 | : Ty(Ty), TempRegID(TempRegID) { |
| 2935 | KnownTypes.insert(Ty); |
| 2936 | } |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 2937 | |
| 2938 | void emitActionOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
| 2939 | Table << MatchTable::Opcode("GIR_MakeTempReg") |
| 2940 | << MatchTable::Comment("TempRegID") << MatchTable::IntValue(TempRegID) |
| 2941 | << MatchTable::Comment("TypeID") |
| 2942 | << MatchTable::NamedValue(Ty.getCxxEnumValue()) |
| 2943 | << MatchTable::LineBreak; |
| 2944 | } |
| 2945 | }; |
| 2946 | |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2947 | InstructionMatcher &RuleMatcher::addInstructionMatcher(StringRef SymbolicName) { |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 2948 | Matchers.emplace_back(new InstructionMatcher(*this, SymbolicName)); |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2949 | MutatableInsns.insert(Matchers.back().get()); |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 2950 | return *Matchers.back(); |
| 2951 | } |
Ahmed Bougacha | 56ca3a9 | 2017-02-04 00:47:10 +0000 | [diff] [blame] | 2952 | |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 2953 | void RuleMatcher::addRequiredFeature(Record *Feature) { |
| 2954 | RequiredFeatures.push_back(Feature); |
| 2955 | } |
| 2956 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 2957 | const std::vector<Record *> &RuleMatcher::getRequiredFeatures() const { |
| 2958 | return RequiredFeatures; |
| 2959 | } |
| 2960 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 2961 | // Emplaces an action of the specified Kind at the end of the action list. |
| 2962 | // |
| 2963 | // Returns a reference to the newly created action. |
| 2964 | // |
| 2965 | // Like std::vector::emplace_back(), may invalidate all iterators if the new |
| 2966 | // size exceeds the capacity. Otherwise, only invalidates the past-the-end |
| 2967 | // iterator. |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 2968 | template <class Kind, class... Args> |
| 2969 | Kind &RuleMatcher::addAction(Args &&... args) { |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 2970 | Actions.emplace_back(std::make_unique<Kind>(std::forward<Args>(args)...)); |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 2971 | return *static_cast<Kind *>(Actions.back().get()); |
| 2972 | } |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 2973 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 2974 | // Emplaces an action of the specified Kind before the given insertion point. |
| 2975 | // |
| 2976 | // Returns an iterator pointing at the newly created instruction. |
| 2977 | // |
| 2978 | // Like std::vector::insert(), may invalidate all iterators if the new size |
| 2979 | // exceeds the capacity. Otherwise, only invalidates the iterators from the |
| 2980 | // insertion point onwards. |
| 2981 | template <class Kind, class... Args> |
| 2982 | action_iterator RuleMatcher::insertAction(action_iterator InsertPt, |
| 2983 | Args &&... args) { |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 2984 | return Actions.emplace(InsertPt, |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 2985 | std::make_unique<Kind>(std::forward<Args>(args)...)); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 2986 | } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 2987 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2988 | unsigned RuleMatcher::implicitlyDefineInsnVar(InstructionMatcher &Matcher) { |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 2989 | unsigned NewInsnVarID = NextInsnVarID++; |
| 2990 | InsnVariableIDs[&Matcher] = NewInsnVarID; |
| 2991 | return NewInsnVarID; |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 2992 | } |
| 2993 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2994 | unsigned RuleMatcher::getInsnVarID(InstructionMatcher &InsnMatcher) const { |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 2995 | const auto &I = InsnVariableIDs.find(&InsnMatcher); |
| 2996 | if (I != InsnVariableIDs.end()) |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 2997 | return I->second; |
| 2998 | llvm_unreachable("Matched Insn was not captured in a local variable"); |
| 2999 | } |
| 3000 | |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 3001 | void RuleMatcher::defineOperand(StringRef SymbolicName, OperandMatcher &OM) { |
| 3002 | if (DefinedOperands.find(SymbolicName) == DefinedOperands.end()) { |
| 3003 | DefinedOperands[SymbolicName] = &OM; |
| 3004 | return; |
| 3005 | } |
| 3006 | |
| 3007 | // If the operand is already defined, then we must ensure both references in |
| 3008 | // the matcher have the exact same node. |
| 3009 | OM.addPredicate<SameOperandMatcher>(OM.getSymbolicName()); |
| 3010 | } |
| 3011 | |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 3012 | void RuleMatcher::definePhysRegOperand(Record *Reg, OperandMatcher &OM) { |
| 3013 | if (PhysRegOperands.find(Reg) == PhysRegOperands.end()) { |
| 3014 | PhysRegOperands[Reg] = &OM; |
| 3015 | return; |
| 3016 | } |
| 3017 | } |
| 3018 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 3019 | InstructionMatcher & |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 3020 | RuleMatcher::getInstructionMatcher(StringRef SymbolicName) const { |
| 3021 | for (const auto &I : InsnVariableIDs) |
| 3022 | if (I.first->getSymbolicName() == SymbolicName) |
| 3023 | return *I.first; |
| 3024 | llvm_unreachable( |
| 3025 | ("Failed to lookup instruction " + SymbolicName).str().c_str()); |
| 3026 | } |
| 3027 | |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 3028 | const OperandMatcher & |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 3029 | RuleMatcher::getPhysRegOperandMatcher(Record *Reg) const { |
| 3030 | const auto &I = PhysRegOperands.find(Reg); |
| 3031 | |
| 3032 | if (I == PhysRegOperands.end()) { |
| 3033 | PrintFatalError(SrcLoc, "Register " + Reg->getName() + |
| 3034 | " was not declared in matcher"); |
| 3035 | } |
| 3036 | |
| 3037 | return *I->second; |
| 3038 | } |
| 3039 | |
| 3040 | const OperandMatcher & |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 3041 | RuleMatcher::getOperandMatcher(StringRef Name) const { |
| 3042 | const auto &I = DefinedOperands.find(Name); |
| 3043 | |
| 3044 | if (I == DefinedOperands.end()) |
| 3045 | PrintFatalError(SrcLoc, "Operand " + Name + " was not declared in matcher"); |
| 3046 | |
| 3047 | return *I->second; |
| 3048 | } |
| 3049 | |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 3050 | void RuleMatcher::emit(MatchTable &Table) { |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 3051 | if (Matchers.empty()) |
| 3052 | llvm_unreachable("Unexpected empty matcher!"); |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 3053 | |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 3054 | // The representation supports rules that require multiple roots such as: |
| 3055 | // %ptr(p0) = ... |
| 3056 | // %elt0(s32) = G_LOAD %ptr |
| 3057 | // %1(p0) = G_ADD %ptr, 4 |
| 3058 | // %elt1(s32) = G_LOAD p0 %1 |
| 3059 | // which could be usefully folded into: |
| 3060 | // %ptr(p0) = ... |
| 3061 | // %elt0(s32), %elt1(s32) = TGT_LOAD_PAIR %ptr |
| 3062 | // on some targets but we don't need to make use of that yet. |
| 3063 | assert(Matchers.size() == 1 && "Cannot handle multi-root matchers yet"); |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 3064 | |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 3065 | unsigned LabelID = Table.allocateLabelID(); |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 3066 | Table << MatchTable::Opcode("GIM_Try", +1) |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 3067 | << MatchTable::Comment("On fail goto") |
| 3068 | << MatchTable::JumpTarget(LabelID) |
| 3069 | << MatchTable::Comment(("Rule ID " + Twine(RuleID) + " //").str()) |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 3070 | << MatchTable::LineBreak; |
| 3071 | |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 3072 | if (!RequiredFeatures.empty()) { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 3073 | Table << MatchTable::Opcode("GIM_CheckFeatures") |
| 3074 | << MatchTable::NamedValue(getNameForFeatureBitset(RequiredFeatures)) |
| 3075 | << MatchTable::LineBreak; |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 3076 | } |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 3077 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 3078 | Matchers.front()->emitPredicateOpcodes(Table, *this); |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 3079 | |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 3080 | // We must also check if it's safe to fold the matched instructions. |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 3081 | if (InsnVariableIDs.size() >= 2) { |
Galina Kistanova | 1754fee | 2017-05-25 01:51:53 +0000 | [diff] [blame] | 3082 | // Invert the map to create stable ordering (by var names) |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 3083 | SmallVector<unsigned, 2> InsnIDs; |
| 3084 | for (const auto &Pair : InsnVariableIDs) { |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 3085 | // Skip the root node since it isn't moving anywhere. Everything else is |
| 3086 | // sinking to meet it. |
| 3087 | if (Pair.first == Matchers.front().get()) |
| 3088 | continue; |
| 3089 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 3090 | InsnIDs.push_back(Pair.second); |
Galina Kistanova | 1754fee | 2017-05-25 01:51:53 +0000 | [diff] [blame] | 3091 | } |
Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 3092 | llvm::sort(InsnIDs); |
Galina Kistanova | 1754fee | 2017-05-25 01:51:53 +0000 | [diff] [blame] | 3093 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 3094 | for (const auto &InsnID : InsnIDs) { |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 3095 | // Reject the difficult cases until we have a more accurate check. |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 3096 | Table << MatchTable::Opcode("GIM_CheckIsSafeToFold") |
| 3097 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 3098 | << MatchTable::LineBreak; |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 3099 | |
| 3100 | // FIXME: Emit checks to determine it's _actually_ safe to fold and/or |
| 3101 | // account for unsafe cases. |
| 3102 | // |
| 3103 | // Example: |
| 3104 | // MI1--> %0 = ... |
| 3105 | // %1 = ... %0 |
| 3106 | // MI0--> %2 = ... %0 |
| 3107 | // It's not safe to erase MI1. We currently handle this by not |
| 3108 | // erasing %0 (even when it's dead). |
| 3109 | // |
| 3110 | // Example: |
| 3111 | // MI1--> %0 = load volatile @a |
| 3112 | // %1 = load volatile @a |
| 3113 | // MI0--> %2 = ... %0 |
| 3114 | // It's not safe to sink %0's def past %1. We currently handle |
| 3115 | // this by rejecting all loads. |
| 3116 | // |
| 3117 | // Example: |
| 3118 | // MI1--> %0 = load @a |
| 3119 | // %1 = store @a |
| 3120 | // MI0--> %2 = ... %0 |
| 3121 | // It's not safe to sink %0's def past %1. We currently handle |
| 3122 | // this by rejecting all loads. |
| 3123 | // |
| 3124 | // Example: |
| 3125 | // G_CONDBR %cond, @BB1 |
| 3126 | // BB0: |
| 3127 | // MI1--> %0 = load @a |
| 3128 | // G_BR @BB1 |
| 3129 | // BB1: |
| 3130 | // MI0--> %2 = ... %0 |
| 3131 | // It's not always safe to sink %0 across control flow. In this |
| 3132 | // case it may introduce a memory fault. We currentl handle this |
| 3133 | // by rejecting all loads. |
| 3134 | } |
| 3135 | } |
| 3136 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 3137 | for (const auto &PM : EpilogueMatchers) |
| 3138 | PM->emitPredicateOpcodes(Table, *this); |
| 3139 | |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 3140 | for (const auto &MA : Actions) |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 3141 | MA->emitActionOpcodes(Table, *this); |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 3142 | |
Roman Tereshin | beb3931 | 2018-05-02 20:15:11 +0000 | [diff] [blame] | 3143 | if (Table.isWithCoverage()) |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 3144 | Table << MatchTable::Opcode("GIR_Coverage") << MatchTable::IntValue(RuleID) |
| 3145 | << MatchTable::LineBreak; |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 3146 | else |
| 3147 | Table << MatchTable::Comment(("GIR_Coverage, " + Twine(RuleID) + ",").str()) |
| 3148 | << MatchTable::LineBreak; |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 3149 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 3150 | Table << MatchTable::Opcode("GIR_Done", -1) << MatchTable::LineBreak |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 3151 | << MatchTable::Label(LabelID); |
Volkan Keles | 4f3fa79 | 2018-01-25 00:18:52 +0000 | [diff] [blame] | 3152 | ++NumPatternEmitted; |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 3153 | } |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 3154 | |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 3155 | bool RuleMatcher::isHigherPriorityThan(const RuleMatcher &B) const { |
| 3156 | // Rules involving more match roots have higher priority. |
| 3157 | if (Matchers.size() > B.Matchers.size()) |
| 3158 | return true; |
| 3159 | if (Matchers.size() < B.Matchers.size()) |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 3160 | return false; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 3161 | |
Mark de Wever | e8d448e | 2019-12-22 18:58:32 +0100 | [diff] [blame] | 3162 | for (auto Matcher : zip(Matchers, B.Matchers)) { |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 3163 | if (std::get<0>(Matcher)->isHigherPriorityThan(*std::get<1>(Matcher))) |
| 3164 | return true; |
| 3165 | if (std::get<1>(Matcher)->isHigherPriorityThan(*std::get<0>(Matcher))) |
| 3166 | return false; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 3167 | } |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 3168 | |
| 3169 | return false; |
Simon Pilgrim | a7d1da8 | 2017-03-15 22:50:47 +0000 | [diff] [blame] | 3170 | } |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 3171 | |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 3172 | unsigned RuleMatcher::countRendererFns() const { |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 3173 | return std::accumulate( |
| 3174 | Matchers.begin(), Matchers.end(), 0, |
| 3175 | [](unsigned A, const std::unique_ptr<InstructionMatcher> &Matcher) { |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 3176 | return A + Matcher->countRendererFns(); |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 3177 | }); |
| 3178 | } |
| 3179 | |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 3180 | bool OperandPredicateMatcher::isHigherPriorityThan( |
| 3181 | const OperandPredicateMatcher &B) const { |
| 3182 | // Generally speaking, an instruction is more important than an Int or a |
| 3183 | // LiteralInt because it can cover more nodes but theres an exception to |
| 3184 | // this. G_CONSTANT's are less important than either of those two because they |
| 3185 | // are more permissive. |
Daniel Sanders | edd0784 | 2017-08-17 09:26:14 +0000 | [diff] [blame] | 3186 | |
| 3187 | const InstructionOperandMatcher *AOM = |
| 3188 | dyn_cast<InstructionOperandMatcher>(this); |
| 3189 | const InstructionOperandMatcher *BOM = |
| 3190 | dyn_cast<InstructionOperandMatcher>(&B); |
| 3191 | bool AIsConstantInsn = AOM && AOM->getInsnMatcher().isConstantInstruction(); |
| 3192 | bool BIsConstantInsn = BOM && BOM->getInsnMatcher().isConstantInstruction(); |
| 3193 | |
| 3194 | if (AOM && BOM) { |
| 3195 | // The relative priorities between a G_CONSTANT and any other instruction |
| 3196 | // don't actually matter but this code is needed to ensure a strict weak |
| 3197 | // ordering. This is particularly important on Windows where the rules will |
| 3198 | // be incorrectly sorted without it. |
| 3199 | if (AIsConstantInsn != BIsConstantInsn) |
| 3200 | return AIsConstantInsn < BIsConstantInsn; |
| 3201 | return false; |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 3202 | } |
Daniel Sanders | edd0784 | 2017-08-17 09:26:14 +0000 | [diff] [blame] | 3203 | |
| 3204 | if (AOM && AIsConstantInsn && (B.Kind == OPM_Int || B.Kind == OPM_LiteralInt)) |
| 3205 | return false; |
| 3206 | if (BOM && BIsConstantInsn && (Kind == OPM_Int || Kind == OPM_LiteralInt)) |
| 3207 | return true; |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 3208 | |
| 3209 | return Kind < B.Kind; |
Daniel Sanders | 75b84fc | 2017-08-08 13:21:26 +0000 | [diff] [blame] | 3210 | } |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 3211 | |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 3212 | void SameOperandMatcher::emitPredicateOpcodes(MatchTable &Table, |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 3213 | RuleMatcher &Rule) const { |
Daniel Sanders | 1e4569f | 2017-10-20 20:55:29 +0000 | [diff] [blame] | 3214 | const OperandMatcher &OtherOM = Rule.getOperandMatcher(MatchingName); |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 3215 | unsigned OtherInsnVarID = Rule.getInsnVarID(OtherOM.getInstructionMatcher()); |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 3216 | assert(OtherInsnVarID == OtherOM.getInstructionMatcher().getInsnVarID()); |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 3217 | |
| 3218 | Table << MatchTable::Opcode("GIM_CheckIsSameOperand") |
| 3219 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 3220 | << MatchTable::Comment("OpIdx") << MatchTable::IntValue(OpIdx) |
| 3221 | << MatchTable::Comment("OtherMI") |
| 3222 | << MatchTable::IntValue(OtherInsnVarID) |
| 3223 | << MatchTable::Comment("OtherOpIdx") |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 3224 | << MatchTable::IntValue(OtherOM.getOpIdx()) |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 3225 | << MatchTable::LineBreak; |
| 3226 | } |
| 3227 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 3228 | //===- GlobalISelEmitter class --------------------------------------------===// |
| 3229 | |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 3230 | class GlobalISelEmitter { |
| 3231 | public: |
| 3232 | explicit GlobalISelEmitter(RecordKeeper &RK); |
| 3233 | void run(raw_ostream &OS); |
| 3234 | |
| 3235 | private: |
| 3236 | const RecordKeeper &RK; |
| 3237 | const CodeGenDAGPatterns CGP; |
| 3238 | const CodeGenTarget &Target; |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 3239 | CodeGenRegBank CGRegs; |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 3240 | |
Daniel Sanders | 39690bd | 2017-10-15 02:41:12 +0000 | [diff] [blame] | 3241 | /// Keep track of the equivalence between SDNodes and Instruction by mapping |
Daniel Sanders | 3c1c4c0 | 2017-12-05 05:52:07 +0000 | [diff] [blame] | 3242 | /// SDNodes to the GINodeEquiv mapping. We need to map to the GINodeEquiv to |
| 3243 | /// check for attributes on the relation such as CheckMMOIsNonAtomic. |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 3244 | /// This is defined using 'GINodeEquiv' in the target description. |
Daniel Sanders | 39690bd | 2017-10-15 02:41:12 +0000 | [diff] [blame] | 3245 | DenseMap<Record *, Record *> NodeEquivs; |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 3246 | |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 3247 | /// Keep track of the equivalence between ComplexPattern's and |
| 3248 | /// GIComplexOperandMatcher. Map entries are specified by subclassing |
| 3249 | /// GIComplexPatternEquiv. |
| 3250 | DenseMap<const Record *, const Record *> ComplexPatternEquivs; |
| 3251 | |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 3252 | /// Keep track of the equivalence between SDNodeXForm's and |
| 3253 | /// GICustomOperandRenderer. Map entries are specified by subclassing |
| 3254 | /// GISDNodeXFormEquiv. |
| 3255 | DenseMap<const Record *, const Record *> SDNodeXFormEquivs; |
| 3256 | |
Aditya Nandakumar | b63e763 | 2018-02-16 22:37:15 +0000 | [diff] [blame] | 3257 | /// Keep track of Scores of PatternsToMatch similar to how the DAG does. |
| 3258 | /// This adds compatibility for RuleMatchers to use this for ordering rules. |
| 3259 | DenseMap<uint64_t, int> RuleMatcherScores; |
| 3260 | |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 3261 | // Map of predicates to their subtarget features. |
Daniel Sanders | e9fdba3 | 2017-04-29 17:30:09 +0000 | [diff] [blame] | 3262 | SubtargetFeatureInfoMap SubtargetFeatures; |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 3263 | |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 3264 | // Rule coverage information. |
| 3265 | Optional<CodeGenCoverage> RuleCoverage; |
| 3266 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 3267 | void gatherOpcodeValues(); |
| 3268 | void gatherTypeIDValues(); |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 3269 | void gatherNodeEquivs(); |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 3270 | |
Daniel Sanders | 39690bd | 2017-10-15 02:41:12 +0000 | [diff] [blame] | 3271 | Record *findNodeEquiv(Record *N) const; |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 3272 | const CodeGenInstruction *getEquivNode(Record &Equiv, |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3273 | const TreePatternNode *N) const; |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 3274 | |
Krzysztof Parzyszek | 779d98e | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 3275 | Error importRulePredicates(RuleMatcher &M, ArrayRef<Predicate> Predicates); |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 3276 | Expected<InstructionMatcher &> |
| 3277 | createAndImportSelDAGMatcher(RuleMatcher &Rule, |
| 3278 | InstructionMatcher &InsnMatcher, |
| 3279 | const TreePatternNode *Src, unsigned &TempOpIdx); |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 3280 | Error importComplexPatternOperandMatcher(OperandMatcher &OM, Record *R, |
| 3281 | unsigned &TempOpIdx) const; |
| 3282 | Error importChildMatcher(RuleMatcher &Rule, InstructionMatcher &InsnMatcher, |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3283 | const TreePatternNode *SrcChild, |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 3284 | bool OperandIsAPointer, unsigned OpIdx, |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 3285 | unsigned &TempOpIdx); |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 3286 | |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 3287 | Expected<BuildMIAction &> createAndImportInstructionRenderer( |
| 3288 | RuleMatcher &M, InstructionMatcher &InsnMatcher, |
| 3289 | const TreePatternNode *Src, const TreePatternNode *Dst); |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 3290 | Expected<action_iterator> createAndImportSubInstructionRenderer( |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3291 | action_iterator InsertPt, RuleMatcher &M, const TreePatternNode *Dst, |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 3292 | unsigned TempReg); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3293 | Expected<action_iterator> |
| 3294 | createInstructionRenderer(action_iterator InsertPt, RuleMatcher &M, |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3295 | const TreePatternNode *Dst); |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 3296 | void importExplicitDefRenderers(BuildMIAction &DstMIBuilder); |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 3297 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3298 | Expected<action_iterator> |
| 3299 | importExplicitUseRenderers(action_iterator InsertPt, RuleMatcher &M, |
| 3300 | BuildMIAction &DstMIBuilder, |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3301 | const llvm::TreePatternNode *Dst); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3302 | Expected<action_iterator> |
| 3303 | importExplicitUseRenderer(action_iterator InsertPt, RuleMatcher &Rule, |
| 3304 | BuildMIAction &DstMIBuilder, |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3305 | TreePatternNode *DstChild); |
Sjoerd Meijer | de23484 | 2019-05-30 07:30:37 +0000 | [diff] [blame] | 3306 | Error importDefaultOperandRenderers(action_iterator InsertPt, RuleMatcher &M, |
| 3307 | BuildMIAction &DstMIBuilder, |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 3308 | DagInit *DefaultOps) const; |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 3309 | Error |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3310 | importImplicitDefRenderers(BuildMIAction &DstMIBuilder, |
| 3311 | const std::vector<Record *> &ImplicitDefs) const; |
| 3312 | |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 3313 | void emitCxxPredicateFns(raw_ostream &OS, StringRef CodeFieldName, |
| 3314 | StringRef TypeIdentifier, StringRef ArgType, |
| 3315 | StringRef ArgName, StringRef AdditionalDeclarations, |
| 3316 | std::function<bool(const Record *R)> Filter); |
| 3317 | void emitImmPredicateFns(raw_ostream &OS, StringRef TypeIdentifier, |
| 3318 | StringRef ArgType, |
| 3319 | std::function<bool(const Record *R)> Filter); |
| 3320 | void emitMIPredicateFns(raw_ostream &OS); |
Daniel Sanders | 649c585 | 2017-10-13 20:42:18 +0000 | [diff] [blame] | 3321 | |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 3322 | /// Analyze pattern \p P, returning a matcher for it if possible. |
| 3323 | /// Otherwise, return an Error explaining why we don't support it. |
| 3324 | Expected<RuleMatcher> runOnPattern(const PatternToMatch &P); |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 3325 | |
| 3326 | void declareSubtargetFeature(Record *Predicate); |
Daniel Sanders | 7e52367 | 2017-11-11 03:23:44 +0000 | [diff] [blame] | 3327 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 3328 | MatchTable buildMatchTable(MutableArrayRef<RuleMatcher> Rules, bool Optimize, |
| 3329 | bool WithCoverage); |
| 3330 | |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 3331 | /// Infer a CodeGenRegisterClass for the type of \p SuperRegNode. The returned |
| 3332 | /// CodeGenRegisterClass will support the CodeGenRegisterClass of |
| 3333 | /// \p SubRegNode, and the subregister index defined by \p SubRegIdxNode. |
| 3334 | /// If no register class is found, return None. |
| 3335 | Optional<const CodeGenRegisterClass *> |
Jessica Paquette | 7080ffa | 2019-08-28 20:12:31 +0000 | [diff] [blame] | 3336 | inferSuperRegisterClassForNode(const TypeSetByHwMode &Ty, |
| 3337 | TreePatternNode *SuperRegNode, |
| 3338 | TreePatternNode *SubRegIdxNode); |
Matt Arsenault | 9ceb6ed | 2019-09-06 00:05:58 +0000 | [diff] [blame] | 3339 | Optional<CodeGenSubRegIndex *> |
| 3340 | inferSubRegIndexForNode(TreePatternNode *SubRegIdxNode); |
Jessica Paquette | 7080ffa | 2019-08-28 20:12:31 +0000 | [diff] [blame] | 3341 | |
| 3342 | /// Infer a CodeGenRegisterClass which suppoorts \p Ty and \p SubRegIdxNode. |
| 3343 | /// Return None if no such class exists. |
| 3344 | Optional<const CodeGenRegisterClass *> |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 3345 | inferSuperRegisterClass(const TypeSetByHwMode &Ty, |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 3346 | TreePatternNode *SubRegIdxNode); |
| 3347 | |
| 3348 | /// Return the CodeGenRegisterClass associated with \p Leaf if it has one. |
| 3349 | Optional<const CodeGenRegisterClass *> |
| 3350 | getRegClassFromLeaf(TreePatternNode *Leaf); |
| 3351 | |
| 3352 | /// Return a CodeGenRegisterClass for \p N if one can be found. Return None |
| 3353 | /// otherwise. |
| 3354 | Optional<const CodeGenRegisterClass *> |
| 3355 | inferRegClassFromPattern(TreePatternNode *N); |
| 3356 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 3357 | public: |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 3358 | /// Takes a sequence of \p Rules and group them based on the predicates |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 3359 | /// they share. \p MatcherStorage is used as a memory container |
Hiroshi Inoue | 501931b | 2018-01-24 05:04:35 +0000 | [diff] [blame] | 3360 | /// for the group that are created as part of this process. |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 3361 | /// |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 3362 | /// What this optimization does looks like if GroupT = GroupMatcher: |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 3363 | /// Output without optimization: |
| 3364 | /// \verbatim |
| 3365 | /// # R1 |
| 3366 | /// # predicate A |
| 3367 | /// # predicate B |
| 3368 | /// ... |
| 3369 | /// # R2 |
| 3370 | /// # predicate A // <-- effectively this is going to be checked twice. |
| 3371 | /// // Once in R1 and once in R2. |
| 3372 | /// # predicate C |
| 3373 | /// \endverbatim |
| 3374 | /// Output with optimization: |
| 3375 | /// \verbatim |
| 3376 | /// # Group1_2 |
| 3377 | /// # predicate A // <-- Check is now shared. |
| 3378 | /// # R1 |
| 3379 | /// # predicate B |
| 3380 | /// # R2 |
| 3381 | /// # predicate C |
| 3382 | /// \endverbatim |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 3383 | template <class GroupT> |
| 3384 | static std::vector<Matcher *> optimizeRules( |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 3385 | ArrayRef<Matcher *> Rules, |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 3386 | std::vector<std::unique_ptr<Matcher>> &MatcherStorage); |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 3387 | }; |
| 3388 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 3389 | void GlobalISelEmitter::gatherOpcodeValues() { |
| 3390 | InstructionOpcodeMatcher::initOpcodeValuesMap(Target); |
| 3391 | } |
| 3392 | |
| 3393 | void GlobalISelEmitter::gatherTypeIDValues() { |
| 3394 | LLTOperandMatcher::initTypeIDValuesMap(); |
| 3395 | } |
| 3396 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 3397 | void GlobalISelEmitter::gatherNodeEquivs() { |
| 3398 | assert(NodeEquivs.empty()); |
| 3399 | for (Record *Equiv : RK.getAllDerivedDefinitions("GINodeEquiv")) |
Daniel Sanders | 39690bd | 2017-10-15 02:41:12 +0000 | [diff] [blame] | 3400 | NodeEquivs[Equiv->getValueAsDef("Node")] = Equiv; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 3401 | |
| 3402 | assert(ComplexPatternEquivs.empty()); |
| 3403 | for (Record *Equiv : RK.getAllDerivedDefinitions("GIComplexPatternEquiv")) { |
| 3404 | Record *SelDAGEquiv = Equiv->getValueAsDef("SelDAGEquivalent"); |
| 3405 | if (!SelDAGEquiv) |
| 3406 | continue; |
| 3407 | ComplexPatternEquivs[SelDAGEquiv] = Equiv; |
| 3408 | } |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 3409 | |
| 3410 | assert(SDNodeXFormEquivs.empty()); |
| 3411 | for (Record *Equiv : RK.getAllDerivedDefinitions("GISDNodeXFormEquiv")) { |
| 3412 | Record *SelDAGEquiv = Equiv->getValueAsDef("SelDAGEquivalent"); |
| 3413 | if (!SelDAGEquiv) |
| 3414 | continue; |
| 3415 | SDNodeXFormEquivs[SelDAGEquiv] = Equiv; |
| 3416 | } |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 3417 | } |
| 3418 | |
Daniel Sanders | 39690bd | 2017-10-15 02:41:12 +0000 | [diff] [blame] | 3419 | Record *GlobalISelEmitter::findNodeEquiv(Record *N) const { |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 3420 | return NodeEquivs.lookup(N); |
| 3421 | } |
| 3422 | |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 3423 | const CodeGenInstruction * |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3424 | GlobalISelEmitter::getEquivNode(Record &Equiv, const TreePatternNode *N) const { |
Matt Arsenault | 8ec5c10 | 2019-08-29 01:13:41 +0000 | [diff] [blame] | 3425 | if (N->getNumChildren() >= 1) { |
| 3426 | // setcc operation maps to two different G_* instructions based on the type. |
| 3427 | if (!Equiv.isValueUnset("IfFloatingPoint") && |
| 3428 | MVT(N->getChild(0)->getSimpleType(0)).isFloatingPoint()) |
| 3429 | return &Target.getInstruction(Equiv.getValueAsDef("IfFloatingPoint")); |
| 3430 | } |
| 3431 | |
Nicolai Haehnle | 445b0b6 | 2018-11-30 14:15:13 +0000 | [diff] [blame] | 3432 | for (const TreePredicateCall &Call : N->getPredicateCalls()) { |
| 3433 | const TreePredicateFn &Predicate = Call.Fn; |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 3434 | if (!Equiv.isValueUnset("IfSignExtend") && Predicate.isLoad() && |
| 3435 | Predicate.isSignExtLoad()) |
| 3436 | return &Target.getInstruction(Equiv.getValueAsDef("IfSignExtend")); |
| 3437 | if (!Equiv.isValueUnset("IfZeroExtend") && Predicate.isLoad() && |
| 3438 | Predicate.isZeroExtLoad()) |
| 3439 | return &Target.getInstruction(Equiv.getValueAsDef("IfZeroExtend")); |
| 3440 | } |
Matt Arsenault | 8ec5c10 | 2019-08-29 01:13:41 +0000 | [diff] [blame] | 3441 | |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 3442 | return &Target.getInstruction(Equiv.getValueAsDef("I")); |
| 3443 | } |
| 3444 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 3445 | GlobalISelEmitter::GlobalISelEmitter(RecordKeeper &RK) |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 3446 | : RK(RK), CGP(RK), Target(CGP.getTargetInfo()), |
| 3447 | CGRegs(RK, Target.getHwModes()) {} |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 3448 | |
| 3449 | //===- Emitter ------------------------------------------------------------===// |
| 3450 | |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 3451 | Error |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3452 | GlobalISelEmitter::importRulePredicates(RuleMatcher &M, |
Krzysztof Parzyszek | 779d98e | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 3453 | ArrayRef<Predicate> Predicates) { |
| 3454 | for (const Predicate &P : Predicates) { |
Matt Arsenault | 57ef94f | 2019-07-30 15:56:43 +0000 | [diff] [blame] | 3455 | if (!P.Def || P.getCondString().empty()) |
Krzysztof Parzyszek | 779d98e | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 3456 | continue; |
| 3457 | declareSubtargetFeature(P.Def); |
| 3458 | M.addRequiredFeature(P.Def); |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 3459 | } |
| 3460 | |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 3461 | return Error::success(); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3462 | } |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 3463 | |
Daniel Sanders | 3c1c4c0 | 2017-12-05 05:52:07 +0000 | [diff] [blame] | 3464 | Expected<InstructionMatcher &> GlobalISelEmitter::createAndImportSelDAGMatcher( |
| 3465 | RuleMatcher &Rule, InstructionMatcher &InsnMatcher, |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 3466 | const TreePatternNode *Src, unsigned &TempOpIdx) { |
Daniel Sanders | 3c1c4c0 | 2017-12-05 05:52:07 +0000 | [diff] [blame] | 3467 | Record *SrcGIEquivOrNull = nullptr; |
| 3468 | const CodeGenInstruction *SrcGIOrNull = nullptr; |
| 3469 | |
| 3470 | // Start with the defined operands (i.e., the results of the root operator). |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3471 | if (Src->getExtTypes().size() > 1) |
Daniel Sanders | 3c1c4c0 | 2017-12-05 05:52:07 +0000 | [diff] [blame] | 3472 | return failedImport("Src pattern has multiple results"); |
| 3473 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3474 | if (Src->isLeaf()) { |
| 3475 | Init *SrcInit = Src->getLeafValue(); |
Daniel Sanders | 3c1c4c0 | 2017-12-05 05:52:07 +0000 | [diff] [blame] | 3476 | if (isa<IntInit>(SrcInit)) { |
| 3477 | InsnMatcher.addPredicate<InstructionOpcodeMatcher>( |
| 3478 | &Target.getInstruction(RK.getDef("G_CONSTANT"))); |
| 3479 | } else |
| 3480 | return failedImport( |
| 3481 | "Unable to deduce gMIR opcode to handle Src (which is a leaf)"); |
| 3482 | } else { |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3483 | SrcGIEquivOrNull = findNodeEquiv(Src->getOperator()); |
Daniel Sanders | 3c1c4c0 | 2017-12-05 05:52:07 +0000 | [diff] [blame] | 3484 | if (!SrcGIEquivOrNull) |
| 3485 | return failedImport("Pattern operator lacks an equivalent Instruction" + |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3486 | explainOperator(Src->getOperator())); |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 3487 | SrcGIOrNull = getEquivNode(*SrcGIEquivOrNull, Src); |
Daniel Sanders | 3c1c4c0 | 2017-12-05 05:52:07 +0000 | [diff] [blame] | 3488 | |
| 3489 | // The operators look good: match the opcode |
| 3490 | InsnMatcher.addPredicate<InstructionOpcodeMatcher>(SrcGIOrNull); |
| 3491 | } |
| 3492 | |
| 3493 | unsigned OpIdx = 0; |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3494 | for (const TypeSetByHwMode &VTy : Src->getExtTypes()) { |
Daniel Sanders | 3c1c4c0 | 2017-12-05 05:52:07 +0000 | [diff] [blame] | 3495 | // Results don't have a name unless they are the root node. The caller will |
| 3496 | // set the name if appropriate. |
| 3497 | OperandMatcher &OM = InsnMatcher.addOperand(OpIdx++, "", TempOpIdx); |
| 3498 | if (auto Error = OM.addTypeCheckPredicate(VTy, false /* OperandIsAPointer */)) |
| 3499 | return failedImport(toString(std::move(Error)) + |
| 3500 | " for result of Src pattern operator"); |
| 3501 | } |
| 3502 | |
Nicolai Haehnle | 445b0b6 | 2018-11-30 14:15:13 +0000 | [diff] [blame] | 3503 | for (const TreePredicateCall &Call : Src->getPredicateCalls()) { |
| 3504 | const TreePredicateFn &Predicate = Call.Fn; |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 3505 | if (Predicate.isAlwaysTrue()) |
| 3506 | continue; |
| 3507 | |
| 3508 | if (Predicate.isImmediatePattern()) { |
| 3509 | InsnMatcher.addPredicate<InstructionImmPredicateMatcher>(Predicate); |
| 3510 | continue; |
| 3511 | } |
| 3512 | |
Matt Arsenault | d00d857 | 2019-07-15 20:59:42 +0000 | [diff] [blame] | 3513 | // An address space check is needed in all contexts if there is one. |
| 3514 | if (Predicate.isLoad() || Predicate.isStore() || Predicate.isAtomic()) { |
| 3515 | if (const ListInit *AddrSpaces = Predicate.getAddressSpaces()) { |
| 3516 | SmallVector<unsigned, 4> ParsedAddrSpaces; |
| 3517 | |
| 3518 | for (Init *Val : AddrSpaces->getValues()) { |
| 3519 | IntInit *IntVal = dyn_cast<IntInit>(Val); |
| 3520 | if (!IntVal) |
| 3521 | return failedImport("Address space is not an integer"); |
| 3522 | ParsedAddrSpaces.push_back(IntVal->getValue()); |
| 3523 | } |
| 3524 | |
| 3525 | if (!ParsedAddrSpaces.empty()) { |
| 3526 | InsnMatcher.addPredicate<MemoryAddressSpacePredicateMatcher>( |
| 3527 | 0, ParsedAddrSpaces); |
| 3528 | } |
| 3529 | } |
Matt Arsenault | 52c2624 | 2019-07-31 00:14:43 +0000 | [diff] [blame] | 3530 | |
| 3531 | int64_t MinAlign = Predicate.getMinAlignment(); |
| 3532 | if (MinAlign > 0) |
| 3533 | InsnMatcher.addPredicate<MemoryAlignmentPredicateMatcher>(0, MinAlign); |
Matt Arsenault | d00d857 | 2019-07-15 20:59:42 +0000 | [diff] [blame] | 3534 | } |
| 3535 | |
| 3536 | // G_LOAD is used for both non-extending and any-extending loads. |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 3537 | if (Predicate.isLoad() && Predicate.isNonExtLoad()) { |
| 3538 | InsnMatcher.addPredicate<MemoryVsLLTSizePredicateMatcher>( |
| 3539 | 0, MemoryVsLLTSizePredicateMatcher::EqualTo, 0); |
| 3540 | continue; |
| 3541 | } |
| 3542 | if (Predicate.isLoad() && Predicate.isAnyExtLoad()) { |
| 3543 | InsnMatcher.addPredicate<MemoryVsLLTSizePredicateMatcher>( |
| 3544 | 0, MemoryVsLLTSizePredicateMatcher::LessThan, 0); |
| 3545 | continue; |
| 3546 | } |
| 3547 | |
Amara Emerson | 52e6d52 | 2019-08-02 23:33:13 +0000 | [diff] [blame] | 3548 | if (Predicate.isStore()) { |
| 3549 | if (Predicate.isTruncStore()) { |
| 3550 | // FIXME: If MemoryVT is set, we end up with 2 checks for the MMO size. |
| 3551 | InsnMatcher.addPredicate<MemoryVsLLTSizePredicateMatcher>( |
| 3552 | 0, MemoryVsLLTSizePredicateMatcher::LessThan, 0); |
| 3553 | continue; |
| 3554 | } |
| 3555 | if (Predicate.isNonTruncStore()) { |
| 3556 | // We need to check the sizes match here otherwise we could incorrectly |
| 3557 | // match truncating stores with non-truncating ones. |
| 3558 | InsnMatcher.addPredicate<MemoryVsLLTSizePredicateMatcher>( |
| 3559 | 0, MemoryVsLLTSizePredicateMatcher::EqualTo, 0); |
| 3560 | } |
Matt Arsenault | 0277249 | 2019-07-15 21:15:20 +0000 | [diff] [blame] | 3561 | } |
| 3562 | |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 3563 | // No check required. We already did it by swapping the opcode. |
| 3564 | if (!SrcGIEquivOrNull->isValueUnset("IfSignExtend") && |
| 3565 | Predicate.isSignExtLoad()) |
| 3566 | continue; |
| 3567 | |
| 3568 | // No check required. We already did it by swapping the opcode. |
| 3569 | if (!SrcGIEquivOrNull->isValueUnset("IfZeroExtend") && |
| 3570 | Predicate.isZeroExtLoad()) |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 3571 | continue; |
| 3572 | |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 3573 | // No check required. G_STORE by itself is a non-extending store. |
| 3574 | if (Predicate.isNonTruncStore()) |
| 3575 | continue; |
| 3576 | |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 3577 | if (Predicate.isLoad() || Predicate.isStore() || Predicate.isAtomic()) { |
| 3578 | if (Predicate.getMemoryVT() != nullptr) { |
| 3579 | Optional<LLTCodeGen> MemTyOrNone = |
| 3580 | MVTToLLT(getValueType(Predicate.getMemoryVT())); |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 3581 | |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 3582 | if (!MemTyOrNone) |
| 3583 | return failedImport("MemVT could not be converted to LLT"); |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 3584 | |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 3585 | // MMO's work in bytes so we must take care of unusual types like i1 |
| 3586 | // don't round down. |
| 3587 | unsigned MemSizeInBits = |
| 3588 | llvm::alignTo(MemTyOrNone->get().getSizeInBits(), 8); |
| 3589 | |
| 3590 | InsnMatcher.addPredicate<MemorySizePredicateMatcher>( |
| 3591 | 0, MemSizeInBits / 8); |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 3592 | continue; |
| 3593 | } |
| 3594 | } |
| 3595 | |
| 3596 | if (Predicate.isLoad() || Predicate.isStore()) { |
| 3597 | // No check required. A G_LOAD/G_STORE is an unindexed load. |
| 3598 | if (Predicate.isUnindexed()) |
| 3599 | continue; |
| 3600 | } |
| 3601 | |
| 3602 | if (Predicate.isAtomic()) { |
| 3603 | if (Predicate.isAtomicOrderingMonotonic()) { |
| 3604 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>( |
| 3605 | "Monotonic"); |
| 3606 | continue; |
| 3607 | } |
| 3608 | if (Predicate.isAtomicOrderingAcquire()) { |
| 3609 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>("Acquire"); |
| 3610 | continue; |
| 3611 | } |
| 3612 | if (Predicate.isAtomicOrderingRelease()) { |
| 3613 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>("Release"); |
| 3614 | continue; |
| 3615 | } |
| 3616 | if (Predicate.isAtomicOrderingAcquireRelease()) { |
| 3617 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>( |
| 3618 | "AcquireRelease"); |
| 3619 | continue; |
| 3620 | } |
| 3621 | if (Predicate.isAtomicOrderingSequentiallyConsistent()) { |
| 3622 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>( |
| 3623 | "SequentiallyConsistent"); |
| 3624 | continue; |
| 3625 | } |
Daniel Sanders | 0c43b3a | 2017-11-30 21:05:59 +0000 | [diff] [blame] | 3626 | |
| 3627 | if (Predicate.isAtomicOrderingAcquireOrStronger()) { |
| 3628 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>( |
| 3629 | "Acquire", AtomicOrderingMMOPredicateMatcher::AO_OrStronger); |
| 3630 | continue; |
| 3631 | } |
| 3632 | if (Predicate.isAtomicOrderingWeakerThanAcquire()) { |
| 3633 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>( |
| 3634 | "Acquire", AtomicOrderingMMOPredicateMatcher::AO_WeakerThan); |
| 3635 | continue; |
| 3636 | } |
| 3637 | |
| 3638 | if (Predicate.isAtomicOrderingReleaseOrStronger()) { |
| 3639 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>( |
| 3640 | "Release", AtomicOrderingMMOPredicateMatcher::AO_OrStronger); |
| 3641 | continue; |
| 3642 | } |
| 3643 | if (Predicate.isAtomicOrderingWeakerThanRelease()) { |
| 3644 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>( |
| 3645 | "Release", AtomicOrderingMMOPredicateMatcher::AO_WeakerThan); |
| 3646 | continue; |
| 3647 | } |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 3648 | } |
| 3649 | |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 3650 | if (Predicate.hasGISelPredicateCode()) { |
| 3651 | InsnMatcher.addPredicate<GenericInstructionPredicateMatcher>(Predicate); |
| 3652 | continue; |
| 3653 | } |
| 3654 | |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 3655 | return failedImport("Src pattern child has predicate (" + |
| 3656 | explainPredicates(Src) + ")"); |
| 3657 | } |
Daniel Sanders | 3c1c4c0 | 2017-12-05 05:52:07 +0000 | [diff] [blame] | 3658 | if (SrcGIEquivOrNull && SrcGIEquivOrNull->getValueAsBit("CheckMMOIsNonAtomic")) |
| 3659 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>("NotAtomic"); |
Matt Arsenault | 63e6d8d | 2019-09-09 16:18:07 +0000 | [diff] [blame] | 3660 | else if (SrcGIEquivOrNull && SrcGIEquivOrNull->getValueAsBit("CheckMMOIsAtomic")) { |
| 3661 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>( |
| 3662 | "Unordered", AtomicOrderingMMOPredicateMatcher::AO_OrStronger); |
| 3663 | } |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 3664 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3665 | if (Src->isLeaf()) { |
| 3666 | Init *SrcInit = Src->getLeafValue(); |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 3667 | if (IntInit *SrcIntInit = dyn_cast<IntInit>(SrcInit)) { |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 3668 | OperandMatcher &OM = |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3669 | InsnMatcher.addOperand(OpIdx++, Src->getName(), TempOpIdx); |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 3670 | OM.addPredicate<LiteralIntOperandMatcher>(SrcIntInit->getValue()); |
| 3671 | } else |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 3672 | return failedImport( |
| 3673 | "Unable to deduce gMIR opcode to handle Src (which is a leaf)"); |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 3674 | } else { |
Daniel Sanders | 85ffd36 | 2017-07-06 08:12:20 +0000 | [diff] [blame] | 3675 | assert(SrcGIOrNull && |
| 3676 | "Expected to have already found an equivalent Instruction"); |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 3677 | if (SrcGIOrNull->TheDef->getName() == "G_CONSTANT" || |
| 3678 | SrcGIOrNull->TheDef->getName() == "G_FCONSTANT") { |
| 3679 | // imm/fpimm still have operands but we don't need to do anything with it |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 3680 | // here since we don't support ImmLeaf predicates yet. However, we still |
| 3681 | // need to note the hidden operand to get GIM_CheckNumOperands correct. |
| 3682 | InsnMatcher.addOperand(OpIdx++, "", TempOpIdx); |
| 3683 | return InsnMatcher; |
| 3684 | } |
| 3685 | |
Matt Arsenault | 8ec5c10 | 2019-08-29 01:13:41 +0000 | [diff] [blame] | 3686 | // Special case because the operand order is changed from setcc. The |
| 3687 | // predicate operand needs to be swapped from the last operand to the first |
| 3688 | // source. |
| 3689 | |
| 3690 | unsigned NumChildren = Src->getNumChildren(); |
| 3691 | bool IsFCmp = SrcGIOrNull->TheDef->getName() == "G_FCMP"; |
| 3692 | |
| 3693 | if (IsFCmp || SrcGIOrNull->TheDef->getName() == "G_ICMP") { |
| 3694 | TreePatternNode *SrcChild = Src->getChild(NumChildren - 1); |
| 3695 | if (SrcChild->isLeaf()) { |
| 3696 | DefInit *DI = dyn_cast<DefInit>(SrcChild->getLeafValue()); |
| 3697 | Record *CCDef = DI ? DI->getDef() : nullptr; |
| 3698 | if (!CCDef || !CCDef->isSubClassOf("CondCode")) |
| 3699 | return failedImport("Unable to handle CondCode"); |
| 3700 | |
| 3701 | OperandMatcher &OM = |
| 3702 | InsnMatcher.addOperand(OpIdx++, SrcChild->getName(), TempOpIdx); |
| 3703 | StringRef PredType = IsFCmp ? CCDef->getValueAsString("FCmpPredicate") : |
| 3704 | CCDef->getValueAsString("ICmpPredicate"); |
| 3705 | |
| 3706 | if (!PredType.empty()) { |
| 3707 | OM.addPredicate<CmpPredicateOperandMatcher>(PredType); |
| 3708 | // Process the other 2 operands normally. |
| 3709 | --NumChildren; |
| 3710 | } |
| 3711 | } |
| 3712 | } |
| 3713 | |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 3714 | // Match the used operands (i.e. the children of the operator). |
Jessica Paquette | 5c8a29f | 2019-08-20 22:04:10 +0000 | [diff] [blame] | 3715 | bool IsIntrinsic = |
| 3716 | SrcGIOrNull->TheDef->getName() == "G_INTRINSIC" || |
| 3717 | SrcGIOrNull->TheDef->getName() == "G_INTRINSIC_W_SIDE_EFFECTS"; |
| 3718 | const CodeGenIntrinsic *II = Src->getIntrinsicInfo(CGP); |
| 3719 | if (IsIntrinsic && !II) |
| 3720 | return failedImport("Expected IntInit containing intrinsic ID)"); |
| 3721 | |
Matt Arsenault | 8ec5c10 | 2019-08-29 01:13:41 +0000 | [diff] [blame] | 3722 | for (unsigned i = 0; i != NumChildren; ++i) { |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3723 | TreePatternNode *SrcChild = Src->getChild(i); |
Daniel Sanders | 85ffd36 | 2017-07-06 08:12:20 +0000 | [diff] [blame] | 3724 | |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 3725 | // SelectionDAG allows pointers to be represented with iN since it doesn't |
| 3726 | // distinguish between pointers and integers but they are different types in GlobalISel. |
| 3727 | // Coerce integers to pointers to address space 0 if the context indicates a pointer. |
Daniel Sanders | c54aa9c | 2017-11-18 00:16:44 +0000 | [diff] [blame] | 3728 | bool OperandIsAPointer = SrcGIOrNull->isOperandAPointer(i); |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 3729 | |
Jessica Paquette | 5c8a29f | 2019-08-20 22:04:10 +0000 | [diff] [blame] | 3730 | if (IsIntrinsic) { |
| 3731 | // For G_INTRINSIC/G_INTRINSIC_W_SIDE_EFFECTS, the operand immediately |
| 3732 | // following the defs is an intrinsic ID. |
| 3733 | if (i == 0) { |
Daniel Sanders | 85ffd36 | 2017-07-06 08:12:20 +0000 | [diff] [blame] | 3734 | OperandMatcher &OM = |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3735 | InsnMatcher.addOperand(OpIdx++, SrcChild->getName(), TempOpIdx); |
Daniel Sanders | fe12c0f | 2017-07-11 08:57:29 +0000 | [diff] [blame] | 3736 | OM.addPredicate<IntrinsicIDOperandMatcher>(II); |
Daniel Sanders | 85ffd36 | 2017-07-06 08:12:20 +0000 | [diff] [blame] | 3737 | continue; |
| 3738 | } |
| 3739 | |
Jessica Paquette | 5c8a29f | 2019-08-20 22:04:10 +0000 | [diff] [blame] | 3740 | // We have to check intrinsics for llvm_anyptr_ty parameters. |
| 3741 | // |
| 3742 | // Note that we have to look at the i-1th parameter, because we don't |
| 3743 | // have the intrinsic ID in the intrinsic's parameter list. |
| 3744 | OperandIsAPointer |= II->isParamAPointer(i - 1); |
Daniel Sanders | 85ffd36 | 2017-07-06 08:12:20 +0000 | [diff] [blame] | 3745 | } |
| 3746 | |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 3747 | if (auto Error = |
| 3748 | importChildMatcher(Rule, InsnMatcher, SrcChild, OperandIsAPointer, |
| 3749 | OpIdx++, TempOpIdx)) |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 3750 | return std::move(Error); |
| 3751 | } |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3752 | } |
| 3753 | |
| 3754 | return InsnMatcher; |
| 3755 | } |
| 3756 | |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 3757 | Error GlobalISelEmitter::importComplexPatternOperandMatcher( |
| 3758 | OperandMatcher &OM, Record *R, unsigned &TempOpIdx) const { |
| 3759 | const auto &ComplexPattern = ComplexPatternEquivs.find(R); |
| 3760 | if (ComplexPattern == ComplexPatternEquivs.end()) |
| 3761 | return failedImport("SelectionDAG ComplexPattern (" + R->getName() + |
| 3762 | ") not mapped to GlobalISel"); |
| 3763 | |
| 3764 | OM.addPredicate<ComplexPatternOperandMatcher>(OM, *ComplexPattern->second); |
| 3765 | TempOpIdx++; |
| 3766 | return Error::success(); |
| 3767 | } |
| 3768 | |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 3769 | // Get the name to use for a pattern operand. For an anonymous physical register |
| 3770 | // input, this should use the register name. |
| 3771 | static StringRef getSrcChildName(const TreePatternNode *SrcChild, |
| 3772 | Record *&PhysReg) { |
| 3773 | StringRef SrcChildName = SrcChild->getName(); |
| 3774 | if (SrcChildName.empty() && SrcChild->isLeaf()) { |
| 3775 | if (auto *ChildDefInit = dyn_cast<DefInit>(SrcChild->getLeafValue())) { |
| 3776 | auto *ChildRec = ChildDefInit->getDef(); |
| 3777 | if (ChildRec->isSubClassOf("Register")) { |
| 3778 | SrcChildName = ChildRec->getName(); |
| 3779 | PhysReg = ChildRec; |
| 3780 | } |
| 3781 | } |
| 3782 | } |
| 3783 | |
| 3784 | return SrcChildName; |
| 3785 | } |
| 3786 | |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 3787 | Error GlobalISelEmitter::importChildMatcher(RuleMatcher &Rule, |
| 3788 | InstructionMatcher &InsnMatcher, |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3789 | const TreePatternNode *SrcChild, |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 3790 | bool OperandIsAPointer, |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 3791 | unsigned OpIdx, |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 3792 | unsigned &TempOpIdx) { |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 3793 | |
| 3794 | Record *PhysReg = nullptr; |
| 3795 | StringRef SrcChildName = getSrcChildName(SrcChild, PhysReg); |
| 3796 | |
| 3797 | OperandMatcher &OM = PhysReg ? |
| 3798 | InsnMatcher.addPhysRegInput(PhysReg, OpIdx, TempOpIdx) : |
| 3799 | InsnMatcher.addOperand(OpIdx, SrcChildName, TempOpIdx); |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 3800 | if (OM.isSameAsAnotherOperand()) |
| 3801 | return Error::success(); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3802 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3803 | ArrayRef<TypeSetByHwMode> ChildTypes = SrcChild->getExtTypes(); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3804 | if (ChildTypes.size() != 1) |
| 3805 | return failedImport("Src pattern child has multiple results"); |
| 3806 | |
| 3807 | // Check MBB's before the type check since they are not a known type. |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3808 | if (!SrcChild->isLeaf()) { |
| 3809 | if (SrcChild->getOperator()->isSubClassOf("SDNode")) { |
| 3810 | auto &ChildSDNI = CGP.getSDNodeInfo(SrcChild->getOperator()); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3811 | if (ChildSDNI.getSDClassName() == "BasicBlockSDNode") { |
| 3812 | OM.addPredicate<MBBOperandMatcher>(); |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 3813 | return Error::success(); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3814 | } |
Matt Arsenault | 3ecab8e | 2019-09-19 16:26:14 +0000 | [diff] [blame] | 3815 | if (SrcChild->getOperator()->getName() == "timm") { |
| 3816 | OM.addPredicate<ImmOperandMatcher>(); |
| 3817 | return Error::success(); |
| 3818 | } |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3819 | } |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3820 | } |
| 3821 | |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 3822 | if (auto Error = |
| 3823 | OM.addTypeCheckPredicate(ChildTypes.front(), OperandIsAPointer)) |
| 3824 | return failedImport(toString(std::move(Error)) + " for Src operand (" + |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3825 | to_string(*SrcChild) + ")"); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3826 | |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 3827 | // Check for nested instructions. |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3828 | if (!SrcChild->isLeaf()) { |
| 3829 | if (SrcChild->getOperator()->isSubClassOf("ComplexPattern")) { |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 3830 | // When a ComplexPattern is used as an operator, it should do the same |
| 3831 | // thing as when used as a leaf. However, the children of the operator |
| 3832 | // name the sub-operands that make up the complex operand and we must |
| 3833 | // prepare to reference them in the renderer too. |
| 3834 | unsigned RendererID = TempOpIdx; |
| 3835 | if (auto Error = importComplexPatternOperandMatcher( |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3836 | OM, SrcChild->getOperator(), TempOpIdx)) |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 3837 | return Error; |
| 3838 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3839 | for (unsigned i = 0, e = SrcChild->getNumChildren(); i != e; ++i) { |
| 3840 | auto *SubOperand = SrcChild->getChild(i); |
Jessica Paquette | 1ed1dd6 | 2019-02-09 00:29:13 +0000 | [diff] [blame] | 3841 | if (!SubOperand->getName().empty()) { |
| 3842 | if (auto Error = Rule.defineComplexSubOperand(SubOperand->getName(), |
| 3843 | SrcChild->getOperator(), |
| 3844 | RendererID, i)) |
| 3845 | return Error; |
| 3846 | } |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 3847 | } |
| 3848 | |
| 3849 | return Error::success(); |
| 3850 | } |
| 3851 | |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 3852 | auto MaybeInsnOperand = OM.addPredicate<InstructionOperandMatcher>( |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3853 | InsnMatcher.getRuleMatcher(), SrcChild->getName()); |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 3854 | if (!MaybeInsnOperand.hasValue()) { |
| 3855 | // This isn't strictly true. If the user were to provide exactly the same |
| 3856 | // matchers as the original operand then we could allow it. However, it's |
| 3857 | // simpler to not permit the redundant specification. |
| 3858 | return failedImport("Nested instruction cannot be the same as another operand"); |
| 3859 | } |
| 3860 | |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 3861 | // Map the node to a gMIR instruction. |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 3862 | InstructionOperandMatcher &InsnOperand = **MaybeInsnOperand; |
Daniel Sanders | 57938df | 2017-07-11 10:40:18 +0000 | [diff] [blame] | 3863 | auto InsnMatcherOrError = createAndImportSelDAGMatcher( |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 3864 | Rule, InsnOperand.getInsnMatcher(), SrcChild, TempOpIdx); |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 3865 | if (auto Error = InsnMatcherOrError.takeError()) |
| 3866 | return Error; |
| 3867 | |
| 3868 | return Error::success(); |
| 3869 | } |
| 3870 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3871 | if (SrcChild->hasAnyPredicate()) |
Diana Picus | d1b6181 | 2017-11-03 10:30:19 +0000 | [diff] [blame] | 3872 | return failedImport("Src pattern child has unsupported predicate"); |
| 3873 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3874 | // Check for constant immediates. |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3875 | if (auto *ChildInt = dyn_cast<IntInit>(SrcChild->getLeafValue())) { |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 3876 | OM.addPredicate<ConstantIntOperandMatcher>(ChildInt->getValue()); |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 3877 | return Error::success(); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3878 | } |
| 3879 | |
| 3880 | // Check for def's like register classes or ComplexPattern's. |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3881 | if (auto *ChildDefInit = dyn_cast<DefInit>(SrcChild->getLeafValue())) { |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3882 | auto *ChildRec = ChildDefInit->getDef(); |
| 3883 | |
| 3884 | // Check for register classes. |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 3885 | if (ChildRec->isSubClassOf("RegisterClass") || |
| 3886 | ChildRec->isSubClassOf("RegisterOperand")) { |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3887 | OM.addPredicate<RegisterBankOperandMatcher>( |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 3888 | Target.getRegisterClass(getInitValueAsRegClass(ChildDefInit))); |
Daniel Sanders | 658541f | 2017-04-22 15:53:21 +0000 | [diff] [blame] | 3889 | return Error::success(); |
| 3890 | } |
| 3891 | |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 3892 | if (ChildRec->isSubClassOf("Register")) { |
| 3893 | // This just be emitted as a copy to the specific register. |
| 3894 | ValueTypeByHwMode VT = ChildTypes.front().getValueTypeByHwMode(); |
| 3895 | const CodeGenRegisterClass *RC |
| 3896 | = CGRegs.getMinimalPhysRegClass(ChildRec, &VT); |
| 3897 | if (!RC) { |
| 3898 | return failedImport( |
| 3899 | "Could not determine physical register class of pattern source"); |
| 3900 | } |
| 3901 | |
| 3902 | OM.addPredicate<RegisterBankOperandMatcher>(*RC); |
| 3903 | return Error::success(); |
| 3904 | } |
| 3905 | |
Daniel Sanders | 4d4e765 | 2017-10-09 18:14:53 +0000 | [diff] [blame] | 3906 | // Check for ValueType. |
| 3907 | if (ChildRec->isSubClassOf("ValueType")) { |
| 3908 | // We already added a type check as standard practice so this doesn't need |
| 3909 | // to do anything. |
| 3910 | return Error::success(); |
| 3911 | } |
| 3912 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3913 | // Check for ComplexPattern's. |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 3914 | if (ChildRec->isSubClassOf("ComplexPattern")) |
| 3915 | return importComplexPatternOperandMatcher(OM, ChildRec, TempOpIdx); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3916 | |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 3917 | if (ChildRec->isSubClassOf("ImmLeaf")) { |
| 3918 | return failedImport( |
| 3919 | "Src pattern child def is an unsupported tablegen class (ImmLeaf)"); |
| 3920 | } |
| 3921 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3922 | return failedImport( |
| 3923 | "Src pattern child def is an unsupported tablegen class"); |
| 3924 | } |
| 3925 | |
| 3926 | return failedImport("Src pattern child is an unsupported kind"); |
| 3927 | } |
| 3928 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3929 | Expected<action_iterator> GlobalISelEmitter::importExplicitUseRenderer( |
| 3930 | action_iterator InsertPt, RuleMatcher &Rule, BuildMIAction &DstMIBuilder, |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3931 | TreePatternNode *DstChild) { |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 3932 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3933 | const auto &SubOperand = Rule.getComplexSubOperand(DstChild->getName()); |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 3934 | if (SubOperand.hasValue()) { |
| 3935 | DstMIBuilder.addRenderer<RenderComplexPatternOperand>( |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3936 | *std::get<0>(*SubOperand), DstChild->getName(), |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 3937 | std::get<1>(*SubOperand), std::get<2>(*SubOperand)); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3938 | return InsertPt; |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 3939 | } |
| 3940 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3941 | if (!DstChild->isLeaf()) { |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 3942 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3943 | if (DstChild->getOperator()->isSubClassOf("SDNodeXForm")) { |
| 3944 | auto Child = DstChild->getChild(0); |
| 3945 | auto I = SDNodeXFormEquivs.find(DstChild->getOperator()); |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 3946 | if (I != SDNodeXFormEquivs.end()) { |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3947 | DstMIBuilder.addRenderer<CustomRenderer>(*I->second, Child->getName()); |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 3948 | return InsertPt; |
| 3949 | } |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3950 | return failedImport("SDNodeXForm " + Child->getName() + |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 3951 | " has no custom renderer"); |
| 3952 | } |
| 3953 | |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 3954 | // We accept 'bb' here. It's an operator because BasicBlockSDNode isn't |
| 3955 | // inline, but in MI it's just another operand. |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3956 | if (DstChild->getOperator()->isSubClassOf("SDNode")) { |
| 3957 | auto &ChildSDNI = CGP.getSDNodeInfo(DstChild->getOperator()); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3958 | if (ChildSDNI.getSDClassName() == "BasicBlockSDNode") { |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3959 | DstMIBuilder.addRenderer<CopyRenderer>(DstChild->getName()); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3960 | return InsertPt; |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3961 | } |
| 3962 | } |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 3963 | |
| 3964 | // Similarly, imm is an operator in TreePatternNode's view but must be |
| 3965 | // rendered as operands. |
| 3966 | // FIXME: The target should be able to choose sign-extended when appropriate |
| 3967 | // (e.g. on Mips). |
Matt Arsenault | 3ecab8e | 2019-09-19 16:26:14 +0000 | [diff] [blame] | 3968 | if (DstChild->getOperator()->getName() == "timm") { |
| 3969 | DstMIBuilder.addRenderer<CopyRenderer>(DstChild->getName()); |
| 3970 | return InsertPt; |
| 3971 | } else if (DstChild->getOperator()->getName() == "imm") { |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3972 | DstMIBuilder.addRenderer<CopyConstantAsImmRenderer>(DstChild->getName()); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3973 | return InsertPt; |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3974 | } else if (DstChild->getOperator()->getName() == "fpimm") { |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 3975 | DstMIBuilder.addRenderer<CopyFConstantAsFPImmRenderer>( |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3976 | DstChild->getName()); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3977 | return InsertPt; |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 3978 | } |
| 3979 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3980 | if (DstChild->getOperator()->isSubClassOf("Instruction")) { |
| 3981 | ArrayRef<TypeSetByHwMode> ChildTypes = DstChild->getExtTypes(); |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 3982 | if (ChildTypes.size() != 1) |
| 3983 | return failedImport("Dst pattern child has multiple results"); |
| 3984 | |
| 3985 | Optional<LLTCodeGen> OpTyOrNone = None; |
| 3986 | if (ChildTypes.front().isMachineValueType()) |
| 3987 | OpTyOrNone = |
| 3988 | MVTToLLT(ChildTypes.front().getMachineValueType().SimpleTy); |
| 3989 | if (!OpTyOrNone) |
| 3990 | return failedImport("Dst operand has an unsupported type"); |
| 3991 | |
| 3992 | unsigned TempRegID = Rule.allocateTempRegID(); |
| 3993 | InsertPt = Rule.insertAction<MakeTempRegisterAction>( |
| 3994 | InsertPt, OpTyOrNone.getValue(), TempRegID); |
| 3995 | DstMIBuilder.addRenderer<TempRegRenderer>(TempRegID); |
| 3996 | |
| 3997 | auto InsertPtOrError = createAndImportSubInstructionRenderer( |
| 3998 | ++InsertPt, Rule, DstChild, TempRegID); |
| 3999 | if (auto Error = InsertPtOrError.takeError()) |
| 4000 | return std::move(Error); |
| 4001 | return InsertPtOrError.get(); |
| 4002 | } |
| 4003 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4004 | return failedImport("Dst pattern child isn't a leaf node or an MBB" + llvm::to_string(*DstChild)); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4005 | } |
| 4006 | |
Daniel Sanders | f499b2b | 2017-11-30 18:48:35 +0000 | [diff] [blame] | 4007 | // It could be a specific immediate in which case we should just check for |
| 4008 | // that immediate. |
| 4009 | if (const IntInit *ChildIntInit = |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4010 | dyn_cast<IntInit>(DstChild->getLeafValue())) { |
Daniel Sanders | f499b2b | 2017-11-30 18:48:35 +0000 | [diff] [blame] | 4011 | DstMIBuilder.addRenderer<ImmRenderer>(ChildIntInit->getValue()); |
| 4012 | return InsertPt; |
| 4013 | } |
| 4014 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4015 | // Otherwise, we're looking for a bog-standard RegisterClass operand. |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4016 | if (auto *ChildDefInit = dyn_cast<DefInit>(DstChild->getLeafValue())) { |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4017 | auto *ChildRec = ChildDefInit->getDef(); |
| 4018 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4019 | ArrayRef<TypeSetByHwMode> ChildTypes = DstChild->getExtTypes(); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4020 | if (ChildTypes.size() != 1) |
| 4021 | return failedImport("Dst pattern child has multiple results"); |
| 4022 | |
Krzysztof Parzyszek | 779d98e | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 4023 | Optional<LLTCodeGen> OpTyOrNone = None; |
| 4024 | if (ChildTypes.front().isMachineValueType()) |
| 4025 | OpTyOrNone = MVTToLLT(ChildTypes.front().getMachineValueType().SimpleTy); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4026 | if (!OpTyOrNone) |
| 4027 | return failedImport("Dst operand has an unsupported type"); |
| 4028 | |
| 4029 | if (ChildRec->isSubClassOf("Register")) { |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 4030 | DstMIBuilder.addRenderer<AddRegisterRenderer>(ChildRec); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 4031 | return InsertPt; |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4032 | } |
| 4033 | |
Daniel Sanders | 658541f | 2017-04-22 15:53:21 +0000 | [diff] [blame] | 4034 | if (ChildRec->isSubClassOf("RegisterClass") || |
Daniel Sanders | 4d4e765 | 2017-10-09 18:14:53 +0000 | [diff] [blame] | 4035 | ChildRec->isSubClassOf("RegisterOperand") || |
| 4036 | ChildRec->isSubClassOf("ValueType")) { |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 4037 | if (ChildRec->isSubClassOf("RegisterOperand") && |
| 4038 | !ChildRec->isValueUnset("GIZeroRegister")) { |
| 4039 | DstMIBuilder.addRenderer<CopyOrAddZeroRegRenderer>( |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4040 | DstChild->getName(), ChildRec->getValueAsDef("GIZeroRegister")); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 4041 | return InsertPt; |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 4042 | } |
| 4043 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4044 | DstMIBuilder.addRenderer<CopyRenderer>(DstChild->getName()); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 4045 | return InsertPt; |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4046 | } |
| 4047 | |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 4048 | if (ChildRec->isSubClassOf("SubRegIndex")) { |
| 4049 | CodeGenSubRegIndex *SubIdx = CGRegs.getSubRegIdx(ChildRec); |
| 4050 | DstMIBuilder.addRenderer<ImmRenderer>(SubIdx->EnumValue); |
| 4051 | return InsertPt; |
| 4052 | } |
| 4053 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4054 | if (ChildRec->isSubClassOf("ComplexPattern")) { |
| 4055 | const auto &ComplexPattern = ComplexPatternEquivs.find(ChildRec); |
| 4056 | if (ComplexPattern == ComplexPatternEquivs.end()) |
| 4057 | return failedImport( |
| 4058 | "SelectionDAG ComplexPattern not mapped to GlobalISel"); |
| 4059 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4060 | const OperandMatcher &OM = Rule.getOperandMatcher(DstChild->getName()); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4061 | DstMIBuilder.addRenderer<RenderComplexPatternOperand>( |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4062 | *ComplexPattern->second, DstChild->getName(), |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 4063 | OM.getAllocatedTemporariesBaseID()); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 4064 | return InsertPt; |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4065 | } |
| 4066 | |
| 4067 | return failedImport( |
| 4068 | "Dst pattern child def is an unsupported tablegen class"); |
| 4069 | } |
| 4070 | |
| 4071 | return failedImport("Dst pattern child is an unsupported kind"); |
| 4072 | } |
| 4073 | |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 4074 | Expected<BuildMIAction &> GlobalISelEmitter::createAndImportInstructionRenderer( |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 4075 | RuleMatcher &M, InstructionMatcher &InsnMatcher, const TreePatternNode *Src, |
| 4076 | const TreePatternNode *Dst) { |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 4077 | auto InsertPtOrError = createInstructionRenderer(M.actions_end(), M, Dst); |
| 4078 | if (auto Error = InsertPtOrError.takeError()) |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 4079 | return std::move(Error); |
| 4080 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 4081 | action_iterator InsertPt = InsertPtOrError.get(); |
| 4082 | BuildMIAction &DstMIBuilder = *static_cast<BuildMIAction *>(InsertPt->get()); |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 4083 | |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 4084 | for (auto PhysInput : InsnMatcher.getPhysRegInputs()) { |
| 4085 | InsertPt = M.insertAction<BuildMIAction>( |
| 4086 | InsertPt, M.allocateOutputInsnID(), |
| 4087 | &Target.getInstruction(RK.getDef("COPY"))); |
| 4088 | BuildMIAction &CopyToPhysRegMIBuilder = |
| 4089 | *static_cast<BuildMIAction *>(InsertPt->get()); |
| 4090 | CopyToPhysRegMIBuilder.addRenderer<AddRegisterRenderer>(PhysInput.first, |
| 4091 | true); |
| 4092 | CopyToPhysRegMIBuilder.addRenderer<CopyPhysRegRenderer>(PhysInput.first); |
| 4093 | } |
| 4094 | |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 4095 | importExplicitDefRenderers(DstMIBuilder); |
| 4096 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 4097 | if (auto Error = importExplicitUseRenderers(InsertPt, M, DstMIBuilder, Dst) |
| 4098 | .takeError()) |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 4099 | return std::move(Error); |
| 4100 | |
| 4101 | return DstMIBuilder; |
| 4102 | } |
| 4103 | |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 4104 | Expected<action_iterator> |
| 4105 | GlobalISelEmitter::createAndImportSubInstructionRenderer( |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4106 | const action_iterator InsertPt, RuleMatcher &M, const TreePatternNode *Dst, |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 4107 | unsigned TempRegID) { |
| 4108 | auto InsertPtOrError = createInstructionRenderer(InsertPt, M, Dst); |
| 4109 | |
| 4110 | // TODO: Assert there's exactly one result. |
| 4111 | |
| 4112 | if (auto Error = InsertPtOrError.takeError()) |
| 4113 | return std::move(Error); |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 4114 | |
| 4115 | BuildMIAction &DstMIBuilder = |
| 4116 | *static_cast<BuildMIAction *>(InsertPtOrError.get()->get()); |
| 4117 | |
| 4118 | // Assign the result to TempReg. |
| 4119 | DstMIBuilder.addRenderer<TempRegRenderer>(TempRegID, true); |
| 4120 | |
Daniel Sanders | 0846452 | 2018-01-29 21:09:12 +0000 | [diff] [blame] | 4121 | InsertPtOrError = |
| 4122 | importExplicitUseRenderers(InsertPtOrError.get(), M, DstMIBuilder, Dst); |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 4123 | if (auto Error = InsertPtOrError.takeError()) |
| 4124 | return std::move(Error); |
| 4125 | |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 4126 | // We need to make sure that when we import an INSERT_SUBREG as a |
| 4127 | // subinstruction that it ends up being constrained to the correct super |
| 4128 | // register and subregister classes. |
Matt Arsenault | 38fb344 | 2019-09-04 16:19:34 +0000 | [diff] [blame] | 4129 | auto OpName = Target.getInstruction(Dst->getOperator()).TheDef->getName(); |
| 4130 | if (OpName == "INSERT_SUBREG") { |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 4131 | auto SubClass = inferRegClassFromPattern(Dst->getChild(1)); |
| 4132 | if (!SubClass) |
| 4133 | return failedImport( |
| 4134 | "Cannot infer register class from INSERT_SUBREG operand #1"); |
Jessica Paquette | 7080ffa | 2019-08-28 20:12:31 +0000 | [diff] [blame] | 4135 | Optional<const CodeGenRegisterClass *> SuperClass = |
| 4136 | inferSuperRegisterClassForNode(Dst->getExtType(0), Dst->getChild(0), |
| 4137 | Dst->getChild(2)); |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 4138 | if (!SuperClass) |
| 4139 | return failedImport( |
| 4140 | "Cannot infer register class for INSERT_SUBREG operand #0"); |
| 4141 | // The destination and the super register source of an INSERT_SUBREG must |
| 4142 | // be the same register class. |
| 4143 | M.insertAction<ConstrainOperandToRegClassAction>( |
| 4144 | InsertPt, DstMIBuilder.getInsnID(), 0, **SuperClass); |
| 4145 | M.insertAction<ConstrainOperandToRegClassAction>( |
| 4146 | InsertPt, DstMIBuilder.getInsnID(), 1, **SuperClass); |
| 4147 | M.insertAction<ConstrainOperandToRegClassAction>( |
| 4148 | InsertPt, DstMIBuilder.getInsnID(), 2, **SubClass); |
| 4149 | return InsertPtOrError.get(); |
| 4150 | } |
| 4151 | |
Matt Arsenault | 9ceb6ed | 2019-09-06 00:05:58 +0000 | [diff] [blame] | 4152 | if (OpName == "EXTRACT_SUBREG") { |
| 4153 | // EXTRACT_SUBREG selects into a subregister COPY but unlike most |
| 4154 | // instructions, the result register class is controlled by the |
| 4155 | // subregisters of the operand. As a result, we must constrain the result |
| 4156 | // class rather than check that it's already the right one. |
| 4157 | auto SuperClass = inferRegClassFromPattern(Dst->getChild(0)); |
| 4158 | if (!SuperClass) |
| 4159 | return failedImport( |
| 4160 | "Cannot infer register class from EXTRACT_SUBREG operand #0"); |
| 4161 | |
| 4162 | auto SubIdx = inferSubRegIndexForNode(Dst->getChild(1)); |
| 4163 | if (!SubIdx) |
| 4164 | return failedImport("EXTRACT_SUBREG child #1 is not a subreg index"); |
| 4165 | |
| 4166 | const auto &SrcRCDstRCPair = |
| 4167 | (*SuperClass)->getMatchingSubClassWithSubRegs(CGRegs, *SubIdx); |
| 4168 | assert(SrcRCDstRCPair->second && "Couldn't find a matching subclass"); |
| 4169 | M.insertAction<ConstrainOperandToRegClassAction>( |
| 4170 | InsertPt, DstMIBuilder.getInsnID(), 0, *SrcRCDstRCPair->second); |
| 4171 | M.insertAction<ConstrainOperandToRegClassAction>( |
| 4172 | InsertPt, DstMIBuilder.getInsnID(), 1, *SrcRCDstRCPair->first); |
| 4173 | |
| 4174 | // We're done with this pattern! It's eligible for GISel emission; return |
| 4175 | // it. |
| 4176 | return InsertPtOrError.get(); |
| 4177 | } |
| 4178 | |
Jessica Paquette | 7080ffa | 2019-08-28 20:12:31 +0000 | [diff] [blame] | 4179 | // Similar to INSERT_SUBREG, we also have to handle SUBREG_TO_REG as a |
| 4180 | // subinstruction. |
Matt Arsenault | 38fb344 | 2019-09-04 16:19:34 +0000 | [diff] [blame] | 4181 | if (OpName == "SUBREG_TO_REG") { |
| 4182 | auto SubClass = inferRegClassFromPattern(Dst->getChild(1)); |
| 4183 | if (!SubClass) |
| 4184 | return failedImport( |
Jessica Paquette | 7080ffa | 2019-08-28 20:12:31 +0000 | [diff] [blame] | 4185 | "Cannot infer register class from SUBREG_TO_REG child #1"); |
Matt Arsenault | 38fb344 | 2019-09-04 16:19:34 +0000 | [diff] [blame] | 4186 | auto SuperClass = inferSuperRegisterClass(Dst->getExtType(0), |
| 4187 | Dst->getChild(2)); |
| 4188 | if (!SuperClass) |
| 4189 | return failedImport( |
Jessica Paquette | 7080ffa | 2019-08-28 20:12:31 +0000 | [diff] [blame] | 4190 | "Cannot infer register class for SUBREG_TO_REG operand #0"); |
Matt Arsenault | 38fb344 | 2019-09-04 16:19:34 +0000 | [diff] [blame] | 4191 | M.insertAction<ConstrainOperandToRegClassAction>( |
Jessica Paquette | 7080ffa | 2019-08-28 20:12:31 +0000 | [diff] [blame] | 4192 | InsertPt, DstMIBuilder.getInsnID(), 0, **SuperClass); |
Matt Arsenault | 38fb344 | 2019-09-04 16:19:34 +0000 | [diff] [blame] | 4193 | M.insertAction<ConstrainOperandToRegClassAction>( |
Jessica Paquette | 7080ffa | 2019-08-28 20:12:31 +0000 | [diff] [blame] | 4194 | InsertPt, DstMIBuilder.getInsnID(), 2, **SubClass); |
Matt Arsenault | 38fb344 | 2019-09-04 16:19:34 +0000 | [diff] [blame] | 4195 | return InsertPtOrError.get(); |
| 4196 | } |
Jessica Paquette | 7080ffa | 2019-08-28 20:12:31 +0000 | [diff] [blame] | 4197 | |
Daniel Sanders | 0846452 | 2018-01-29 21:09:12 +0000 | [diff] [blame] | 4198 | M.insertAction<ConstrainOperandsToDefinitionAction>(InsertPt, |
| 4199 | DstMIBuilder.getInsnID()); |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 4200 | return InsertPtOrError.get(); |
| 4201 | } |
| 4202 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 4203 | Expected<action_iterator> GlobalISelEmitter::createInstructionRenderer( |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4204 | action_iterator InsertPt, RuleMatcher &M, const TreePatternNode *Dst) { |
| 4205 | Record *DstOp = Dst->getOperator(); |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 4206 | if (!DstOp->isSubClassOf("Instruction")) { |
| 4207 | if (DstOp->isSubClassOf("ValueType")) |
| 4208 | return failedImport( |
| 4209 | "Pattern operator isn't an instruction (it's a ValueType)"); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4210 | return failedImport("Pattern operator isn't an instruction"); |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 4211 | } |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 4212 | CodeGenInstruction *DstI = &Target.getInstruction(DstOp); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4213 | |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 4214 | // COPY_TO_REGCLASS is just a copy with a ConstrainOperandToRegClassAction |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 4215 | // attached. Similarly for EXTRACT_SUBREG except that's a subregister copy. |
Matt Arsenault | 4a23ae5 | 2019-09-10 17:57:33 +0000 | [diff] [blame] | 4216 | StringRef Name = DstI->TheDef->getName(); |
| 4217 | if (Name == "COPY_TO_REGCLASS" || Name == "EXTRACT_SUBREG") |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 4218 | DstI = &Target.getInstruction(RK.getDef("COPY")); |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 4219 | |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 4220 | return M.insertAction<BuildMIAction>(InsertPt, M.allocateOutputInsnID(), |
| 4221 | DstI); |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 4222 | } |
| 4223 | |
| 4224 | void GlobalISelEmitter::importExplicitDefRenderers( |
| 4225 | BuildMIAction &DstMIBuilder) { |
| 4226 | const CodeGenInstruction *DstI = DstMIBuilder.getCGI(); |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 4227 | for (unsigned I = 0; I < DstI->Operands.NumDefs; ++I) { |
| 4228 | const CGIOperandList::OperandInfo &DstIOperand = DstI->Operands[I]; |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 4229 | DstMIBuilder.addRenderer<CopyRenderer>(DstIOperand.Name); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4230 | } |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 4231 | } |
| 4232 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 4233 | Expected<action_iterator> GlobalISelEmitter::importExplicitUseRenderers( |
| 4234 | action_iterator InsertPt, RuleMatcher &M, BuildMIAction &DstMIBuilder, |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4235 | const llvm::TreePatternNode *Dst) { |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 4236 | const CodeGenInstruction *DstI = DstMIBuilder.getCGI(); |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4237 | CodeGenInstruction *OrigDstI = &Target.getInstruction(Dst->getOperator()); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4238 | |
Matt Arsenault | 4a23ae5 | 2019-09-10 17:57:33 +0000 | [diff] [blame] | 4239 | StringRef Name = OrigDstI->TheDef->getName(); |
| 4240 | unsigned ExpectedDstINumUses = Dst->getNumChildren(); |
| 4241 | |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 4242 | // EXTRACT_SUBREG needs to use a subregister COPY. |
Matt Arsenault | 4a23ae5 | 2019-09-10 17:57:33 +0000 | [diff] [blame] | 4243 | if (Name == "EXTRACT_SUBREG") { |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4244 | if (!Dst->getChild(0)->isLeaf()) |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 4245 | return failedImport("EXTRACT_SUBREG child #1 is not a leaf"); |
| 4246 | |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 4247 | if (DefInit *SubRegInit = |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4248 | dyn_cast<DefInit>(Dst->getChild(1)->getLeafValue())) { |
| 4249 | Record *RCDef = getInitValueAsRegClass(Dst->getChild(0)->getLeafValue()); |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 4250 | if (!RCDef) |
| 4251 | return failedImport("EXTRACT_SUBREG child #0 could not " |
| 4252 | "be coerced to a register class"); |
| 4253 | |
| 4254 | CodeGenRegisterClass *RC = CGRegs.getRegClass(RCDef); |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 4255 | CodeGenSubRegIndex *SubIdx = CGRegs.getSubRegIdx(SubRegInit->getDef()); |
| 4256 | |
| 4257 | const auto &SrcRCDstRCPair = |
| 4258 | RC->getMatchingSubClassWithSubRegs(CGRegs, SubIdx); |
| 4259 | if (SrcRCDstRCPair.hasValue()) { |
| 4260 | assert(SrcRCDstRCPair->second && "Couldn't find a matching subclass"); |
| 4261 | if (SrcRCDstRCPair->first != RC) |
| 4262 | return failedImport("EXTRACT_SUBREG requires an additional COPY"); |
| 4263 | } |
| 4264 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4265 | DstMIBuilder.addRenderer<CopySubRegRenderer>(Dst->getChild(0)->getName(), |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 4266 | SubIdx); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 4267 | return InsertPt; |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 4268 | } |
| 4269 | |
| 4270 | return failedImport("EXTRACT_SUBREG child #1 is not a subreg index"); |
| 4271 | } |
| 4272 | |
Matt Arsenault | 4a23ae5 | 2019-09-10 17:57:33 +0000 | [diff] [blame] | 4273 | if (Name == "REG_SEQUENCE") { |
| 4274 | if (!Dst->getChild(0)->isLeaf()) |
| 4275 | return failedImport("REG_SEQUENCE child #0 is not a leaf"); |
| 4276 | |
| 4277 | Record *RCDef = getInitValueAsRegClass(Dst->getChild(0)->getLeafValue()); |
| 4278 | if (!RCDef) |
| 4279 | return failedImport("REG_SEQUENCE child #0 could not " |
| 4280 | "be coerced to a register class"); |
| 4281 | |
| 4282 | if ((ExpectedDstINumUses - 1) % 2 != 0) |
| 4283 | return failedImport("Malformed REG_SEQUENCE"); |
| 4284 | |
| 4285 | for (unsigned I = 1; I != ExpectedDstINumUses; I += 2) { |
| 4286 | TreePatternNode *ValChild = Dst->getChild(I); |
| 4287 | TreePatternNode *SubRegChild = Dst->getChild(I + 1); |
| 4288 | |
| 4289 | if (DefInit *SubRegInit = |
| 4290 | dyn_cast<DefInit>(SubRegChild->getLeafValue())) { |
| 4291 | CodeGenSubRegIndex *SubIdx = CGRegs.getSubRegIdx(SubRegInit->getDef()); |
| 4292 | |
| 4293 | auto InsertPtOrError = |
| 4294 | importExplicitUseRenderer(InsertPt, M, DstMIBuilder, ValChild); |
| 4295 | if (auto Error = InsertPtOrError.takeError()) |
| 4296 | return std::move(Error); |
| 4297 | InsertPt = InsertPtOrError.get(); |
| 4298 | DstMIBuilder.addRenderer<SubRegIndexRenderer>(SubIdx); |
| 4299 | } |
| 4300 | } |
| 4301 | |
| 4302 | return InsertPt; |
| 4303 | } |
| 4304 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4305 | // Render the explicit uses. |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 4306 | unsigned DstINumUses = OrigDstI->Operands.size() - OrigDstI->Operands.NumDefs; |
Matt Arsenault | 4a23ae5 | 2019-09-10 17:57:33 +0000 | [diff] [blame] | 4307 | if (Name == "COPY_TO_REGCLASS") { |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 4308 | DstINumUses--; // Ignore the class constraint. |
| 4309 | ExpectedDstINumUses--; |
| 4310 | } |
| 4311 | |
Matt Arsenault | 26f714f | 2019-10-21 21:39:42 -0700 | [diff] [blame] | 4312 | // NumResults - This is the number of results produced by the instruction in |
| 4313 | // the "outs" list. |
| 4314 | unsigned NumResults = OrigDstI->Operands.NumDefs; |
| 4315 | |
| 4316 | // Number of operands we know the output instruction must have. If it is |
| 4317 | // variadic, we could have more operands. |
| 4318 | unsigned NumFixedOperands = DstI->Operands.size(); |
| 4319 | |
| 4320 | // Loop over all of the fixed operands of the instruction pattern, emitting |
| 4321 | // code to fill them all in. The node 'N' usually has number children equal to |
| 4322 | // the number of input operands of the instruction. However, in cases where |
| 4323 | // there are predicate operands for an instruction, we need to fill in the |
| 4324 | // 'execute always' values. Match up the node operands to the instruction |
| 4325 | // operands to do this. |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 4326 | unsigned Child = 0; |
Matt Arsenault | 26f714f | 2019-10-21 21:39:42 -0700 | [diff] [blame] | 4327 | |
| 4328 | // Similarly to the code in TreePatternNode::ApplyTypeConstraints, count the |
| 4329 | // number of operands at the end of the list which have default values. |
| 4330 | // Those can come from the pattern if it provides enough arguments, or be |
| 4331 | // filled in with the default if the pattern hasn't provided them. But any |
| 4332 | // operand with a default value _before_ the last mandatory one will be |
| 4333 | // filled in with their defaults unconditionally. |
| 4334 | unsigned NonOverridableOperands = NumFixedOperands; |
| 4335 | while (NonOverridableOperands > NumResults && |
| 4336 | CGP.operandHasDefault(DstI->Operands[NonOverridableOperands - 1].Rec)) |
| 4337 | --NonOverridableOperands; |
| 4338 | |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 4339 | unsigned NumDefaultOps = 0; |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 4340 | for (unsigned I = 0; I != DstINumUses; ++I) { |
Matt Arsenault | 26f714f | 2019-10-21 21:39:42 -0700 | [diff] [blame] | 4341 | unsigned InstOpNo = DstI->Operands.NumDefs + I; |
| 4342 | |
| 4343 | // Determine what to emit for this operand. |
| 4344 | Record *OperandNode = DstI->Operands[InstOpNo].Rec; |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 4345 | |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 4346 | // If the operand has default values, introduce them now. |
Matt Arsenault | 26f714f | 2019-10-21 21:39:42 -0700 | [diff] [blame] | 4347 | if (CGP.operandHasDefault(OperandNode) && |
| 4348 | (InstOpNo < NonOverridableOperands || Child >= Dst->getNumChildren())) { |
| 4349 | // This is a predicate or optional def operand which the pattern has not |
| 4350 | // overridden, or which we aren't letting it override; emit the 'default |
| 4351 | // ops' operands. |
| 4352 | |
| 4353 | const CGIOperandList::OperandInfo &DstIOperand = DstI->Operands[InstOpNo]; |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 4354 | DagInit *DefaultOps = DstIOperand.Rec->getValueAsDag("DefaultOps"); |
Sjoerd Meijer | de23484 | 2019-05-30 07:30:37 +0000 | [diff] [blame] | 4355 | if (auto Error = importDefaultOperandRenderers( |
| 4356 | InsertPt, M, DstMIBuilder, DefaultOps)) |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 4357 | return std::move(Error); |
| 4358 | ++NumDefaultOps; |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 4359 | continue; |
| 4360 | } |
| 4361 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 4362 | auto InsertPtOrError = importExplicitUseRenderer(InsertPt, M, DstMIBuilder, |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4363 | Dst->getChild(Child)); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 4364 | if (auto Error = InsertPtOrError.takeError()) |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4365 | return std::move(Error); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 4366 | InsertPt = InsertPtOrError.get(); |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 4367 | ++Child; |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4368 | } |
| 4369 | |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 4370 | if (NumDefaultOps + ExpectedDstINumUses != DstINumUses) |
Diana Picus | eb2057c | 2017-05-17 09:25:08 +0000 | [diff] [blame] | 4371 | return failedImport("Expected " + llvm::to_string(DstINumUses) + |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 4372 | " used operands but found " + |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 4373 | llvm::to_string(ExpectedDstINumUses) + |
Diana Picus | eb2057c | 2017-05-17 09:25:08 +0000 | [diff] [blame] | 4374 | " explicit ones and " + llvm::to_string(NumDefaultOps) + |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 4375 | " default ones"); |
| 4376 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 4377 | return InsertPt; |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4378 | } |
| 4379 | |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 4380 | Error GlobalISelEmitter::importDefaultOperandRenderers( |
Sjoerd Meijer | de23484 | 2019-05-30 07:30:37 +0000 | [diff] [blame] | 4381 | action_iterator InsertPt, RuleMatcher &M, BuildMIAction &DstMIBuilder, |
| 4382 | DagInit *DefaultOps) const { |
Craig Topper | 481ff70 | 2017-05-29 21:49:34 +0000 | [diff] [blame] | 4383 | for (const auto *DefaultOp : DefaultOps->getArgs()) { |
Sjoerd Meijer | de23484 | 2019-05-30 07:30:37 +0000 | [diff] [blame] | 4384 | Optional<LLTCodeGen> OpTyOrNone = None; |
| 4385 | |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 4386 | // Look through ValueType operators. |
| 4387 | if (const DagInit *DefaultDagOp = dyn_cast<DagInit>(DefaultOp)) { |
| 4388 | if (const DefInit *DefaultDagOperator = |
| 4389 | dyn_cast<DefInit>(DefaultDagOp->getOperator())) { |
Sjoerd Meijer | 3cac8d2 | 2019-05-31 08:39:34 +0000 | [diff] [blame] | 4390 | if (DefaultDagOperator->getDef()->isSubClassOf("ValueType")) { |
Sjoerd Meijer | de23484 | 2019-05-30 07:30:37 +0000 | [diff] [blame] | 4391 | OpTyOrNone = MVTToLLT(getValueType( |
| 4392 | DefaultDagOperator->getDef())); |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 4393 | DefaultOp = DefaultDagOp->getArg(0); |
Sjoerd Meijer | 3cac8d2 | 2019-05-31 08:39:34 +0000 | [diff] [blame] | 4394 | } |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 4395 | } |
| 4396 | } |
| 4397 | |
| 4398 | if (const DefInit *DefaultDefOp = dyn_cast<DefInit>(DefaultOp)) { |
Sjoerd Meijer | de23484 | 2019-05-30 07:30:37 +0000 | [diff] [blame] | 4399 | auto Def = DefaultDefOp->getDef(); |
| 4400 | if (Def->getName() == "undef_tied_input") { |
| 4401 | unsigned TempRegID = M.allocateTempRegID(); |
| 4402 | M.insertAction<MakeTempRegisterAction>( |
| 4403 | InsertPt, OpTyOrNone.getValue(), TempRegID); |
| 4404 | InsertPt = M.insertAction<BuildMIAction>( |
| 4405 | InsertPt, M.allocateOutputInsnID(), |
| 4406 | &Target.getInstruction(RK.getDef("IMPLICIT_DEF"))); |
| 4407 | BuildMIAction &IDMIBuilder = *static_cast<BuildMIAction *>( |
| 4408 | InsertPt->get()); |
| 4409 | IDMIBuilder.addRenderer<TempRegRenderer>(TempRegID); |
| 4410 | DstMIBuilder.addRenderer<TempRegRenderer>(TempRegID); |
| 4411 | } else { |
| 4412 | DstMIBuilder.addRenderer<AddRegisterRenderer>(Def); |
| 4413 | } |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 4414 | continue; |
| 4415 | } |
| 4416 | |
| 4417 | if (const IntInit *DefaultIntOp = dyn_cast<IntInit>(DefaultOp)) { |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 4418 | DstMIBuilder.addRenderer<ImmRenderer>(DefaultIntOp->getValue()); |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 4419 | continue; |
| 4420 | } |
| 4421 | |
| 4422 | return failedImport("Could not add default op"); |
| 4423 | } |
| 4424 | |
| 4425 | return Error::success(); |
| 4426 | } |
| 4427 | |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 4428 | Error GlobalISelEmitter::importImplicitDefRenderers( |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4429 | BuildMIAction &DstMIBuilder, |
| 4430 | const std::vector<Record *> &ImplicitDefs) const { |
| 4431 | if (!ImplicitDefs.empty()) |
| 4432 | return failedImport("Pattern defines a physical register"); |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 4433 | return Error::success(); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4434 | } |
| 4435 | |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 4436 | Optional<const CodeGenRegisterClass *> |
| 4437 | GlobalISelEmitter::getRegClassFromLeaf(TreePatternNode *Leaf) { |
| 4438 | assert(Leaf && "Expected node?"); |
| 4439 | assert(Leaf->isLeaf() && "Expected leaf?"); |
| 4440 | Record *RCRec = getInitValueAsRegClass(Leaf->getLeafValue()); |
| 4441 | if (!RCRec) |
| 4442 | return None; |
| 4443 | CodeGenRegisterClass *RC = CGRegs.getRegClass(RCRec); |
| 4444 | if (!RC) |
| 4445 | return None; |
| 4446 | return RC; |
| 4447 | } |
| 4448 | |
| 4449 | Optional<const CodeGenRegisterClass *> |
| 4450 | GlobalISelEmitter::inferRegClassFromPattern(TreePatternNode *N) { |
| 4451 | if (!N) |
| 4452 | return None; |
| 4453 | |
| 4454 | if (N->isLeaf()) |
| 4455 | return getRegClassFromLeaf(N); |
| 4456 | |
| 4457 | // We don't have a leaf node, so we have to try and infer something. Check |
| 4458 | // that we have an instruction that we an infer something from. |
| 4459 | |
| 4460 | // Only handle things that produce a single type. |
| 4461 | if (N->getNumTypes() != 1) |
| 4462 | return None; |
| 4463 | Record *OpRec = N->getOperator(); |
| 4464 | |
| 4465 | // We only want instructions. |
| 4466 | if (!OpRec->isSubClassOf("Instruction")) |
| 4467 | return None; |
| 4468 | |
| 4469 | // Don't want to try and infer things when there could potentially be more |
| 4470 | // than one candidate register class. |
| 4471 | auto &Inst = Target.getInstruction(OpRec); |
| 4472 | if (Inst.Operands.NumDefs > 1) |
| 4473 | return None; |
| 4474 | |
| 4475 | // Handle any special-case instructions which we can safely infer register |
| 4476 | // classes from. |
| 4477 | StringRef InstName = Inst.TheDef->getName(); |
Matt Arsenault | 38fb344 | 2019-09-04 16:19:34 +0000 | [diff] [blame] | 4478 | bool IsRegSequence = InstName == "REG_SEQUENCE"; |
| 4479 | if (IsRegSequence || InstName == "COPY_TO_REGCLASS") { |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 4480 | // If we have a COPY_TO_REGCLASS, then we need to handle it specially. It |
| 4481 | // has the desired register class as the first child. |
Matt Arsenault | 38fb344 | 2019-09-04 16:19:34 +0000 | [diff] [blame] | 4482 | TreePatternNode *RCChild = N->getChild(IsRegSequence ? 0 : 1); |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 4483 | if (!RCChild->isLeaf()) |
| 4484 | return None; |
| 4485 | return getRegClassFromLeaf(RCChild); |
| 4486 | } |
| 4487 | |
| 4488 | // Handle destination record types that we can safely infer a register class |
| 4489 | // from. |
| 4490 | const auto &DstIOperand = Inst.Operands[0]; |
| 4491 | Record *DstIOpRec = DstIOperand.Rec; |
| 4492 | if (DstIOpRec->isSubClassOf("RegisterOperand")) { |
| 4493 | DstIOpRec = DstIOpRec->getValueAsDef("RegClass"); |
| 4494 | const CodeGenRegisterClass &RC = Target.getRegisterClass(DstIOpRec); |
| 4495 | return &RC; |
| 4496 | } |
| 4497 | |
| 4498 | if (DstIOpRec->isSubClassOf("RegisterClass")) { |
| 4499 | const CodeGenRegisterClass &RC = Target.getRegisterClass(DstIOpRec); |
| 4500 | return &RC; |
| 4501 | } |
| 4502 | |
| 4503 | return None; |
| 4504 | } |
| 4505 | |
| 4506 | Optional<const CodeGenRegisterClass *> |
| 4507 | GlobalISelEmitter::inferSuperRegisterClass(const TypeSetByHwMode &Ty, |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 4508 | TreePatternNode *SubRegIdxNode) { |
Jessica Paquette | 7080ffa | 2019-08-28 20:12:31 +0000 | [diff] [blame] | 4509 | assert(SubRegIdxNode && "Expected subregister index node!"); |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 4510 | // We need a ValueTypeByHwMode for getSuperRegForSubReg. |
| 4511 | if (!Ty.isValueTypeByHwMode(false)) |
| 4512 | return None; |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 4513 | if (!SubRegIdxNode->isLeaf()) |
| 4514 | return None; |
| 4515 | DefInit *SubRegInit = dyn_cast<DefInit>(SubRegIdxNode->getLeafValue()); |
| 4516 | if (!SubRegInit) |
| 4517 | return None; |
| 4518 | CodeGenSubRegIndex *SubIdx = CGRegs.getSubRegIdx(SubRegInit->getDef()); |
| 4519 | |
| 4520 | // Use the information we found above to find a minimal register class which |
| 4521 | // supports the subregister and type we want. |
| 4522 | auto RC = |
| 4523 | Target.getSuperRegForSubReg(Ty.getValueTypeByHwMode(), CGRegs, SubIdx); |
| 4524 | if (!RC) |
| 4525 | return None; |
| 4526 | return *RC; |
| 4527 | } |
| 4528 | |
Jessica Paquette | 7080ffa | 2019-08-28 20:12:31 +0000 | [diff] [blame] | 4529 | Optional<const CodeGenRegisterClass *> |
| 4530 | GlobalISelEmitter::inferSuperRegisterClassForNode( |
| 4531 | const TypeSetByHwMode &Ty, TreePatternNode *SuperRegNode, |
| 4532 | TreePatternNode *SubRegIdxNode) { |
| 4533 | assert(SuperRegNode && "Expected super register node!"); |
| 4534 | // Check if we already have a defined register class for the super register |
| 4535 | // node. If we do, then we should preserve that rather than inferring anything |
| 4536 | // from the subregister index node. We can assume that whoever wrote the |
| 4537 | // pattern in the first place made sure that the super register and |
| 4538 | // subregister are compatible. |
| 4539 | if (Optional<const CodeGenRegisterClass *> SuperRegisterClass = |
| 4540 | inferRegClassFromPattern(SuperRegNode)) |
| 4541 | return *SuperRegisterClass; |
| 4542 | return inferSuperRegisterClass(Ty, SubRegIdxNode); |
| 4543 | } |
| 4544 | |
Matt Arsenault | 9ceb6ed | 2019-09-06 00:05:58 +0000 | [diff] [blame] | 4545 | Optional<CodeGenSubRegIndex *> |
| 4546 | GlobalISelEmitter::inferSubRegIndexForNode(TreePatternNode *SubRegIdxNode) { |
| 4547 | if (!SubRegIdxNode->isLeaf()) |
| 4548 | return None; |
| 4549 | |
| 4550 | DefInit *SubRegInit = dyn_cast<DefInit>(SubRegIdxNode->getLeafValue()); |
| 4551 | if (!SubRegInit) |
| 4552 | return None; |
| 4553 | return CGRegs.getSubRegIdx(SubRegInit->getDef()); |
| 4554 | } |
| 4555 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4556 | Expected<RuleMatcher> GlobalISelEmitter::runOnPattern(const PatternToMatch &P) { |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4557 | // Keep track of the matchers and actions to emit. |
Aditya Nandakumar | b63e763 | 2018-02-16 22:37:15 +0000 | [diff] [blame] | 4558 | int Score = P.getPatternComplexity(CGP); |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 4559 | RuleMatcher M(P.getSrcRecord()->getLoc()); |
Aditya Nandakumar | b63e763 | 2018-02-16 22:37:15 +0000 | [diff] [blame] | 4560 | RuleMatcherScores[M.getRuleID()] = Score; |
Daniel Sanders | 6ea17ed | 2017-10-31 18:07:03 +0000 | [diff] [blame] | 4561 | M.addAction<DebugCommentAction>(llvm::to_string(*P.getSrcPattern()) + |
| 4562 | " => " + |
| 4563 | llvm::to_string(*P.getDstPattern())); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4564 | |
Krzysztof Parzyszek | 779d98e | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 4565 | if (auto Error = importRulePredicates(M, P.getPredicates())) |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4566 | return std::move(Error); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4567 | |
| 4568 | // Next, analyze the pattern operators. |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4569 | TreePatternNode *Src = P.getSrcPattern(); |
| 4570 | TreePatternNode *Dst = P.getDstPattern(); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4571 | |
| 4572 | // If the root of either pattern isn't a simple operator, ignore it. |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 4573 | if (auto Err = isTrivialOperatorNode(Dst)) |
| 4574 | return failedImport("Dst pattern root isn't a trivial operator (" + |
| 4575 | toString(std::move(Err)) + ")"); |
| 4576 | if (auto Err = isTrivialOperatorNode(Src)) |
| 4577 | return failedImport("Src pattern root isn't a trivial operator (" + |
| 4578 | toString(std::move(Err)) + ")"); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4579 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 4580 | // The different predicates and matchers created during |
| 4581 | // addInstructionMatcher use the RuleMatcher M to set up their |
| 4582 | // instruction ID (InsnVarID) that are going to be used when |
| 4583 | // M is going to be emitted. |
| 4584 | // However, the code doing the emission still relies on the IDs |
| 4585 | // returned during that process by the RuleMatcher when issuing |
| 4586 | // the recordInsn opcodes. |
| 4587 | // Because of that: |
| 4588 | // 1. The order in which we created the predicates |
| 4589 | // and such must be the same as the order in which we emit them, |
| 4590 | // and |
| 4591 | // 2. We need to reset the generation of the IDs in M somewhere between |
| 4592 | // addInstructionMatcher and emit |
| 4593 | // |
| 4594 | // FIXME: Long term, we don't want to have to rely on this implicit |
| 4595 | // naming being the same. One possible solution would be to have |
| 4596 | // explicit operator for operation capture and reference those. |
| 4597 | // The plus side is that it would expose opportunities to share |
| 4598 | // the capture accross rules. The downside is that it would |
| 4599 | // introduce a dependency between predicates (captures must happen |
| 4600 | // before their first use.) |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4601 | InstructionMatcher &InsnMatcherTemp = M.addInstructionMatcher(Src->getName()); |
Daniel Sanders | edd0784 | 2017-08-17 09:26:14 +0000 | [diff] [blame] | 4602 | unsigned TempOpIdx = 0; |
| 4603 | auto InsnMatcherOrError = |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 4604 | createAndImportSelDAGMatcher(M, InsnMatcherTemp, Src, TempOpIdx); |
Daniel Sanders | edd0784 | 2017-08-17 09:26:14 +0000 | [diff] [blame] | 4605 | if (auto Error = InsnMatcherOrError.takeError()) |
| 4606 | return std::move(Error); |
| 4607 | InstructionMatcher &InsnMatcher = InsnMatcherOrError.get(); |
| 4608 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4609 | if (Dst->isLeaf()) { |
| 4610 | Record *RCDef = getInitValueAsRegClass(Dst->getLeafValue()); |
Daniel Sanders | edd0784 | 2017-08-17 09:26:14 +0000 | [diff] [blame] | 4611 | |
| 4612 | const CodeGenRegisterClass &RC = Target.getRegisterClass(RCDef); |
| 4613 | if (RCDef) { |
| 4614 | // We need to replace the def and all its uses with the specified |
| 4615 | // operand. However, we must also insert COPY's wherever needed. |
| 4616 | // For now, emit a copy and let the register allocator clean up. |
| 4617 | auto &DstI = Target.getInstruction(RK.getDef("COPY")); |
| 4618 | const auto &DstIOperand = DstI.Operands[0]; |
| 4619 | |
| 4620 | OperandMatcher &OM0 = InsnMatcher.getOperand(0); |
| 4621 | OM0.setSymbolicName(DstIOperand.Name); |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 4622 | M.defineOperand(OM0.getSymbolicName(), OM0); |
Daniel Sanders | edd0784 | 2017-08-17 09:26:14 +0000 | [diff] [blame] | 4623 | OM0.addPredicate<RegisterBankOperandMatcher>(RC); |
| 4624 | |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 4625 | auto &DstMIBuilder = |
| 4626 | M.addAction<BuildMIAction>(M.allocateOutputInsnID(), &DstI); |
| 4627 | DstMIBuilder.addRenderer<CopyRenderer>(DstIOperand.Name); |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4628 | DstMIBuilder.addRenderer<CopyRenderer>(Dst->getName()); |
Daniel Sanders | edd0784 | 2017-08-17 09:26:14 +0000 | [diff] [blame] | 4629 | M.addAction<ConstrainOperandToRegClassAction>(0, 0, RC); |
| 4630 | |
| 4631 | // We're done with this pattern! It's eligible for GISel emission; return |
| 4632 | // it. |
| 4633 | ++NumPatternImported; |
| 4634 | return std::move(M); |
| 4635 | } |
| 4636 | |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 4637 | return failedImport("Dst pattern root isn't a known leaf"); |
Daniel Sanders | edd0784 | 2017-08-17 09:26:14 +0000 | [diff] [blame] | 4638 | } |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 4639 | |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 4640 | // Start with the defined operands (i.e., the results of the root operator). |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4641 | Record *DstOp = Dst->getOperator(); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4642 | if (!DstOp->isSubClassOf("Instruction")) |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 4643 | return failedImport("Pattern operator isn't an instruction"); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4644 | |
| 4645 | auto &DstI = Target.getInstruction(DstOp); |
Matt Arsenault | 38fb344 | 2019-09-04 16:19:34 +0000 | [diff] [blame] | 4646 | StringRef DstIName = DstI.TheDef->getName(); |
| 4647 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4648 | if (DstI.Operands.NumDefs != Src->getExtTypes().size()) |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 4649 | return failedImport("Src pattern results and dst MI defs are different (" + |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4650 | to_string(Src->getExtTypes().size()) + " def(s) vs " + |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 4651 | to_string(DstI.Operands.NumDefs) + " def(s))"); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4652 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4653 | // The root of the match also has constraints on the register bank so that it |
| 4654 | // matches the result instruction. |
| 4655 | unsigned OpIdx = 0; |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4656 | for (const TypeSetByHwMode &VTy : Src->getExtTypes()) { |
Krzysztof Parzyszek | 779d98e | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 4657 | (void)VTy; |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4658 | |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 4659 | const auto &DstIOperand = DstI.Operands[OpIdx]; |
| 4660 | Record *DstIOpRec = DstIOperand.Rec; |
Matt Arsenault | 38fb344 | 2019-09-04 16:19:34 +0000 | [diff] [blame] | 4661 | if (DstIName == "COPY_TO_REGCLASS") { |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4662 | DstIOpRec = getInitValueAsRegClass(Dst->getChild(1)->getLeafValue()); |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 4663 | |
| 4664 | if (DstIOpRec == nullptr) |
| 4665 | return failedImport( |
| 4666 | "COPY_TO_REGCLASS operand #1 isn't a register class"); |
Matt Arsenault | 38fb344 | 2019-09-04 16:19:34 +0000 | [diff] [blame] | 4667 | } else if (DstIName == "REG_SEQUENCE") { |
| 4668 | DstIOpRec = getInitValueAsRegClass(Dst->getChild(0)->getLeafValue()); |
| 4669 | if (DstIOpRec == nullptr) |
| 4670 | return failedImport("REG_SEQUENCE operand #0 isn't a register class"); |
| 4671 | } else if (DstIName == "EXTRACT_SUBREG") { |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4672 | if (!Dst->getChild(0)->isLeaf()) |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 4673 | return failedImport("EXTRACT_SUBREG operand #0 isn't a leaf"); |
| 4674 | |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 4675 | // We can assume that a subregister is in the same bank as it's super |
| 4676 | // register. |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4677 | DstIOpRec = getInitValueAsRegClass(Dst->getChild(0)->getLeafValue()); |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 4678 | |
| 4679 | if (DstIOpRec == nullptr) |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 4680 | return failedImport("EXTRACT_SUBREG operand #0 isn't a register class"); |
Matt Arsenault | 38fb344 | 2019-09-04 16:19:34 +0000 | [diff] [blame] | 4681 | } else if (DstIName == "INSERT_SUBREG") { |
Jessica Paquette | 7080ffa | 2019-08-28 20:12:31 +0000 | [diff] [blame] | 4682 | auto MaybeSuperClass = inferSuperRegisterClassForNode( |
| 4683 | VTy, Dst->getChild(0), Dst->getChild(2)); |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 4684 | if (!MaybeSuperClass) |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 4685 | return failedImport( |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 4686 | "Cannot infer register class for INSERT_SUBREG operand #0"); |
| 4687 | // Move to the next pattern here, because the register class we found |
| 4688 | // doesn't necessarily have a record associated with it. So, we can't |
| 4689 | // set DstIOpRec using this. |
| 4690 | OperandMatcher &OM = InsnMatcher.getOperand(OpIdx); |
| 4691 | OM.setSymbolicName(DstIOperand.Name); |
| 4692 | M.defineOperand(OM.getSymbolicName(), OM); |
| 4693 | OM.addPredicate<RegisterBankOperandMatcher>(**MaybeSuperClass); |
| 4694 | ++OpIdx; |
| 4695 | continue; |
Matt Arsenault | 38fb344 | 2019-09-04 16:19:34 +0000 | [diff] [blame] | 4696 | } else if (DstIName == "SUBREG_TO_REG") { |
Jessica Paquette | 7080ffa | 2019-08-28 20:12:31 +0000 | [diff] [blame] | 4697 | auto MaybeRegClass = inferSuperRegisterClass(VTy, Dst->getChild(2)); |
| 4698 | if (!MaybeRegClass) |
| 4699 | return failedImport( |
| 4700 | "Cannot infer register class for SUBREG_TO_REG operand #0"); |
| 4701 | OperandMatcher &OM = InsnMatcher.getOperand(OpIdx); |
| 4702 | OM.setSymbolicName(DstIOperand.Name); |
| 4703 | M.defineOperand(OM.getSymbolicName(), OM); |
| 4704 | OM.addPredicate<RegisterBankOperandMatcher>(**MaybeRegClass); |
| 4705 | ++OpIdx; |
| 4706 | continue; |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 4707 | } else if (DstIOpRec->isSubClassOf("RegisterOperand")) |
Daniel Sanders | 658541f | 2017-04-22 15:53:21 +0000 | [diff] [blame] | 4708 | DstIOpRec = DstIOpRec->getValueAsDef("RegClass"); |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 4709 | else if (!DstIOpRec->isSubClassOf("RegisterClass")) |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4710 | return failedImport("Dst MI def isn't a register class" + |
| 4711 | to_string(*Dst)); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4712 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4713 | OperandMatcher &OM = InsnMatcher.getOperand(OpIdx); |
| 4714 | OM.setSymbolicName(DstIOperand.Name); |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 4715 | M.defineOperand(OM.getSymbolicName(), OM); |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 4716 | OM.addPredicate<RegisterBankOperandMatcher>( |
| 4717 | Target.getRegisterClass(DstIOpRec)); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4718 | ++OpIdx; |
| 4719 | } |
| 4720 | |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 4721 | auto DstMIBuilderOrError = |
| 4722 | createAndImportInstructionRenderer(M, InsnMatcher, Src, Dst); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4723 | if (auto Error = DstMIBuilderOrError.takeError()) |
| 4724 | return std::move(Error); |
| 4725 | BuildMIAction &DstMIBuilder = DstMIBuilderOrError.get(); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4726 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4727 | // Render the implicit defs. |
| 4728 | // These are only added to the root of the result. |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 4729 | if (auto Error = importImplicitDefRenderers(DstMIBuilder, P.getDstRegs())) |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4730 | return std::move(Error); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4731 | |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 4732 | DstMIBuilder.chooseInsnToMutate(M); |
| 4733 | |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 4734 | // Constrain the registers to classes. This is normally derived from the |
| 4735 | // emitted instruction but a few instructions require special handling. |
Matt Arsenault | 38fb344 | 2019-09-04 16:19:34 +0000 | [diff] [blame] | 4736 | if (DstIName == "COPY_TO_REGCLASS") { |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 4737 | // COPY_TO_REGCLASS does not provide operand constraints itself but the |
| 4738 | // result is constrained to the class given by the second child. |
| 4739 | Record *DstIOpRec = |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4740 | getInitValueAsRegClass(Dst->getChild(1)->getLeafValue()); |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 4741 | |
| 4742 | if (DstIOpRec == nullptr) |
| 4743 | return failedImport("COPY_TO_REGCLASS operand #1 isn't a register class"); |
| 4744 | |
| 4745 | M.addAction<ConstrainOperandToRegClassAction>( |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 4746 | 0, 0, Target.getRegisterClass(DstIOpRec)); |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 4747 | |
| 4748 | // We're done with this pattern! It's eligible for GISel emission; return |
| 4749 | // it. |
| 4750 | ++NumPatternImported; |
| 4751 | return std::move(M); |
| 4752 | } |
| 4753 | |
Matt Arsenault | 38fb344 | 2019-09-04 16:19:34 +0000 | [diff] [blame] | 4754 | if (DstIName == "EXTRACT_SUBREG") { |
Matt Arsenault | 9ceb6ed | 2019-09-06 00:05:58 +0000 | [diff] [blame] | 4755 | auto SuperClass = inferRegClassFromPattern(Dst->getChild(0)); |
| 4756 | if (!SuperClass) |
| 4757 | return failedImport( |
| 4758 | "Cannot infer register class from EXTRACT_SUBREG operand #0"); |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 4759 | |
Matt Arsenault | 9ceb6ed | 2019-09-06 00:05:58 +0000 | [diff] [blame] | 4760 | auto SubIdx = inferSubRegIndexForNode(Dst->getChild(1)); |
| 4761 | if (!SubIdx) |
Daniel Sanders | 320390b | 2017-06-28 15:16:03 +0000 | [diff] [blame] | 4762 | return failedImport("EXTRACT_SUBREG child #1 is not a subreg index"); |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 4763 | |
Daniel Sanders | 320390b | 2017-06-28 15:16:03 +0000 | [diff] [blame] | 4764 | // It would be nice to leave this constraint implicit but we're required |
| 4765 | // to pick a register class so constrain the result to a register class |
| 4766 | // that can hold the correct MVT. |
| 4767 | // |
| 4768 | // FIXME: This may introduce an extra copy if the chosen class doesn't |
| 4769 | // actually contain the subregisters. |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4770 | assert(Src->getExtTypes().size() == 1 && |
Daniel Sanders | 320390b | 2017-06-28 15:16:03 +0000 | [diff] [blame] | 4771 | "Expected Src of EXTRACT_SUBREG to have one result type"); |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 4772 | |
Daniel Sanders | 320390b | 2017-06-28 15:16:03 +0000 | [diff] [blame] | 4773 | const auto &SrcRCDstRCPair = |
Matt Arsenault | 9ceb6ed | 2019-09-06 00:05:58 +0000 | [diff] [blame] | 4774 | (*SuperClass)->getMatchingSubClassWithSubRegs(CGRegs, *SubIdx); |
Daniel Sanders | 320390b | 2017-06-28 15:16:03 +0000 | [diff] [blame] | 4775 | assert(SrcRCDstRCPair->second && "Couldn't find a matching subclass"); |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 4776 | M.addAction<ConstrainOperandToRegClassAction>(0, 0, *SrcRCDstRCPair->second); |
| 4777 | M.addAction<ConstrainOperandToRegClassAction>(0, 1, *SrcRCDstRCPair->first); |
| 4778 | |
| 4779 | // We're done with this pattern! It's eligible for GISel emission; return |
| 4780 | // it. |
| 4781 | ++NumPatternImported; |
| 4782 | return std::move(M); |
| 4783 | } |
| 4784 | |
Matt Arsenault | 38fb344 | 2019-09-04 16:19:34 +0000 | [diff] [blame] | 4785 | if (DstIName == "INSERT_SUBREG") { |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 4786 | assert(Src->getExtTypes().size() == 1 && |
| 4787 | "Expected Src of INSERT_SUBREG to have one result type"); |
| 4788 | // We need to constrain the destination, a super regsister source, and a |
| 4789 | // subregister source. |
| 4790 | auto SubClass = inferRegClassFromPattern(Dst->getChild(1)); |
| 4791 | if (!SubClass) |
| 4792 | return failedImport( |
| 4793 | "Cannot infer register class from INSERT_SUBREG operand #1"); |
Jessica Paquette | 7080ffa | 2019-08-28 20:12:31 +0000 | [diff] [blame] | 4794 | auto SuperClass = inferSuperRegisterClassForNode( |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 4795 | Src->getExtType(0), Dst->getChild(0), Dst->getChild(2)); |
| 4796 | if (!SuperClass) |
| 4797 | return failedImport( |
| 4798 | "Cannot infer register class for INSERT_SUBREG operand #0"); |
| 4799 | M.addAction<ConstrainOperandToRegClassAction>(0, 0, **SuperClass); |
| 4800 | M.addAction<ConstrainOperandToRegClassAction>(0, 1, **SuperClass); |
| 4801 | M.addAction<ConstrainOperandToRegClassAction>(0, 2, **SubClass); |
| 4802 | ++NumPatternImported; |
| 4803 | return std::move(M); |
| 4804 | } |
| 4805 | |
Matt Arsenault | 38fb344 | 2019-09-04 16:19:34 +0000 | [diff] [blame] | 4806 | if (DstIName == "SUBREG_TO_REG") { |
Jessica Paquette | 7080ffa | 2019-08-28 20:12:31 +0000 | [diff] [blame] | 4807 | // We need to constrain the destination and subregister source. |
| 4808 | assert(Src->getExtTypes().size() == 1 && |
| 4809 | "Expected Src of SUBREG_TO_REG to have one result type"); |
| 4810 | |
| 4811 | // Attempt to infer the subregister source from the first child. If it has |
| 4812 | // an explicitly given register class, we'll use that. Otherwise, we will |
| 4813 | // fail. |
| 4814 | auto SubClass = inferRegClassFromPattern(Dst->getChild(1)); |
| 4815 | if (!SubClass) |
| 4816 | return failedImport( |
| 4817 | "Cannot infer register class from SUBREG_TO_REG child #1"); |
| 4818 | // We don't have a child to look at that might have a super register node. |
| 4819 | auto SuperClass = |
| 4820 | inferSuperRegisterClass(Src->getExtType(0), Dst->getChild(2)); |
| 4821 | if (!SuperClass) |
| 4822 | return failedImport( |
| 4823 | "Cannot infer register class for SUBREG_TO_REG operand #0"); |
| 4824 | M.addAction<ConstrainOperandToRegClassAction>(0, 0, **SuperClass); |
| 4825 | M.addAction<ConstrainOperandToRegClassAction>(0, 2, **SubClass); |
| 4826 | ++NumPatternImported; |
| 4827 | return std::move(M); |
| 4828 | } |
| 4829 | |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 4830 | M.addAction<ConstrainOperandsToDefinitionAction>(0); |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 4831 | |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 4832 | // 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] | 4833 | ++NumPatternImported; |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 4834 | return std::move(M); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4835 | } |
| 4836 | |
Daniel Sanders | 649c585 | 2017-10-13 20:42:18 +0000 | [diff] [blame] | 4837 | // Emit imm predicate table and an enum to reference them with. |
| 4838 | // The 'Predicate_' part of the name is redundant but eliminating it is more |
| 4839 | // trouble than it's worth. |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 4840 | void GlobalISelEmitter::emitCxxPredicateFns( |
| 4841 | raw_ostream &OS, StringRef CodeFieldName, StringRef TypeIdentifier, |
| 4842 | StringRef ArgType, StringRef ArgName, StringRef AdditionalDeclarations, |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 4843 | std::function<bool(const Record *R)> Filter) { |
Daniel Sanders | 649c585 | 2017-10-13 20:42:18 +0000 | [diff] [blame] | 4844 | std::vector<const Record *> MatchedRecords; |
| 4845 | const auto &Defs = RK.getAllDerivedDefinitions("PatFrag"); |
| 4846 | std::copy_if(Defs.begin(), Defs.end(), std::back_inserter(MatchedRecords), |
| 4847 | [&](Record *Record) { |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 4848 | return !Record->getValueAsString(CodeFieldName).empty() && |
Daniel Sanders | 649c585 | 2017-10-13 20:42:18 +0000 | [diff] [blame] | 4849 | Filter(Record); |
| 4850 | }); |
| 4851 | |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 4852 | if (!MatchedRecords.empty()) { |
| 4853 | OS << "// PatFrag predicates.\n" |
| 4854 | << "enum {\n"; |
Daniel Sanders | 2fed4ff | 2017-10-13 21:51:20 +0000 | [diff] [blame] | 4855 | std::string EnumeratorSeparator = |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 4856 | (" = GIPFP_" + TypeIdentifier + "_Invalid + 1,\n").str(); |
| 4857 | for (const auto *Record : MatchedRecords) { |
| 4858 | OS << " GIPFP_" << TypeIdentifier << "_Predicate_" << Record->getName() |
| 4859 | << EnumeratorSeparator; |
| 4860 | EnumeratorSeparator = ",\n"; |
| 4861 | } |
| 4862 | OS << "};\n"; |
Daniel Sanders | 649c585 | 2017-10-13 20:42:18 +0000 | [diff] [blame] | 4863 | } |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 4864 | |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 4865 | OS << "bool " << Target.getName() << "InstructionSelector::test" << ArgName |
| 4866 | << "Predicate_" << TypeIdentifier << "(unsigned PredicateID, " << ArgType << " " |
| 4867 | << ArgName << ") const {\n" |
| 4868 | << AdditionalDeclarations; |
| 4869 | if (!AdditionalDeclarations.empty()) |
| 4870 | OS << "\n"; |
Aaron Ballman | 82e17f5 | 2017-12-20 20:09:30 +0000 | [diff] [blame] | 4871 | if (!MatchedRecords.empty()) |
| 4872 | OS << " switch (PredicateID) {\n"; |
Daniel Sanders | 32de8bb | 2017-12-20 14:41:51 +0000 | [diff] [blame] | 4873 | for (const auto *Record : MatchedRecords) { |
| 4874 | OS << " case GIPFP_" << TypeIdentifier << "_Predicate_" |
| 4875 | << Record->getName() << ": {\n" |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 4876 | << " " << Record->getValueAsString(CodeFieldName) << "\n" |
| 4877 | << " llvm_unreachable(\"" << CodeFieldName |
| 4878 | << " should have returned\");\n" |
Daniel Sanders | 32de8bb | 2017-12-20 14:41:51 +0000 | [diff] [blame] | 4879 | << " return false;\n" |
| 4880 | << " }\n"; |
| 4881 | } |
Aaron Ballman | 82e17f5 | 2017-12-20 20:09:30 +0000 | [diff] [blame] | 4882 | if (!MatchedRecords.empty()) |
| 4883 | OS << " }\n"; |
| 4884 | OS << " llvm_unreachable(\"Unknown predicate\");\n" |
Daniel Sanders | 32de8bb | 2017-12-20 14:41:51 +0000 | [diff] [blame] | 4885 | << " return false;\n" |
| 4886 | << "}\n"; |
Daniel Sanders | 649c585 | 2017-10-13 20:42:18 +0000 | [diff] [blame] | 4887 | } |
| 4888 | |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 4889 | void GlobalISelEmitter::emitImmPredicateFns( |
| 4890 | raw_ostream &OS, StringRef TypeIdentifier, StringRef ArgType, |
| 4891 | std::function<bool(const Record *R)> Filter) { |
| 4892 | return emitCxxPredicateFns(OS, "ImmediateCode", TypeIdentifier, ArgType, |
| 4893 | "Imm", "", Filter); |
| 4894 | } |
| 4895 | |
| 4896 | void GlobalISelEmitter::emitMIPredicateFns(raw_ostream &OS) { |
| 4897 | return emitCxxPredicateFns( |
| 4898 | OS, "GISelPredicateCode", "MI", "const MachineInstr &", "MI", |
| 4899 | " const MachineFunction &MF = *MI.getParent()->getParent();\n" |
Andrei Elovikov | 36cbbff | 2018-06-26 07:05:08 +0000 | [diff] [blame] | 4900 | " const MachineRegisterInfo &MRI = MF.getRegInfo();\n" |
| 4901 | " (void)MRI;", |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 4902 | [](const Record *R) { return true; }); |
| 4903 | } |
| 4904 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4905 | template <class GroupT> |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 4906 | std::vector<Matcher *> GlobalISelEmitter::optimizeRules( |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 4907 | ArrayRef<Matcher *> Rules, |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4908 | std::vector<std::unique_ptr<Matcher>> &MatcherStorage) { |
| 4909 | |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 4910 | std::vector<Matcher *> OptRules; |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 4911 | std::unique_ptr<GroupT> CurrentGroup = std::make_unique<GroupT>(); |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4912 | assert(CurrentGroup->empty() && "Newly created group isn't empty!"); |
| 4913 | unsigned NumGroups = 0; |
| 4914 | |
| 4915 | auto ProcessCurrentGroup = [&]() { |
| 4916 | if (CurrentGroup->empty()) |
| 4917 | // An empty group is good to be reused: |
| 4918 | return; |
| 4919 | |
| 4920 | // If the group isn't large enough to provide any benefit, move all the |
| 4921 | // added rules out of it and make sure to re-create the group to properly |
| 4922 | // re-initialize it: |
| 4923 | if (CurrentGroup->size() < 2) |
| 4924 | for (Matcher *M : CurrentGroup->matchers()) |
| 4925 | OptRules.push_back(M); |
| 4926 | else { |
| 4927 | CurrentGroup->finalize(); |
Roman Tereshin | 8bdf7be | 2018-05-21 22:21:24 +0000 | [diff] [blame] | 4928 | OptRules.push_back(CurrentGroup.get()); |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4929 | MatcherStorage.emplace_back(std::move(CurrentGroup)); |
| 4930 | ++NumGroups; |
Roman Tereshin | 8bdf7be | 2018-05-21 22:21:24 +0000 | [diff] [blame] | 4931 | } |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 4932 | CurrentGroup = std::make_unique<GroupT>(); |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4933 | }; |
| 4934 | for (Matcher *Rule : Rules) { |
| 4935 | // Greedily add as many matchers as possible to the current group: |
| 4936 | if (CurrentGroup->addMatcher(*Rule)) |
| 4937 | continue; |
| 4938 | |
| 4939 | ProcessCurrentGroup(); |
| 4940 | assert(CurrentGroup->empty() && "A group wasn't properly re-initialized"); |
| 4941 | |
| 4942 | // Try to add the pending matcher to a newly created empty group: |
| 4943 | if (!CurrentGroup->addMatcher(*Rule)) |
| 4944 | // If we couldn't add the matcher to an empty group, that group type |
| 4945 | // doesn't support that kind of matchers at all, so just skip it: |
| 4946 | OptRules.push_back(Rule); |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 4947 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4948 | ProcessCurrentGroup(); |
| 4949 | |
Nicola Zaghen | 03d0b91 | 2018-05-23 15:09:29 +0000 | [diff] [blame] | 4950 | LLVM_DEBUG(dbgs() << "NumGroups: " << NumGroups << "\n"); |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4951 | assert(CurrentGroup->empty() && "The last group wasn't properly processed"); |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 4952 | return OptRules; |
| 4953 | } |
| 4954 | |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 4955 | MatchTable |
| 4956 | GlobalISelEmitter::buildMatchTable(MutableArrayRef<RuleMatcher> Rules, |
Roman Tereshin | beb3931 | 2018-05-02 20:15:11 +0000 | [diff] [blame] | 4957 | bool Optimize, bool WithCoverage) { |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 4958 | std::vector<Matcher *> InputRules; |
| 4959 | for (Matcher &Rule : Rules) |
| 4960 | InputRules.push_back(&Rule); |
| 4961 | |
| 4962 | if (!Optimize) |
Roman Tereshin | beb3931 | 2018-05-02 20:15:11 +0000 | [diff] [blame] | 4963 | return MatchTable::buildTable(InputRules, WithCoverage); |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 4964 | |
Roman Tereshin | 7701360 | 2018-05-22 16:54:27 +0000 | [diff] [blame] | 4965 | unsigned CurrentOrdering = 0; |
| 4966 | StringMap<unsigned> OpcodeOrder; |
| 4967 | for (RuleMatcher &Rule : Rules) { |
| 4968 | const StringRef Opcode = Rule.getOpcode(); |
| 4969 | assert(!Opcode.empty() && "Didn't expect an undefined opcode"); |
| 4970 | if (OpcodeOrder.count(Opcode) == 0) |
| 4971 | OpcodeOrder[Opcode] = CurrentOrdering++; |
| 4972 | } |
| 4973 | |
| 4974 | std::stable_sort(InputRules.begin(), InputRules.end(), |
| 4975 | [&OpcodeOrder](const Matcher *A, const Matcher *B) { |
| 4976 | auto *L = static_cast<const RuleMatcher *>(A); |
| 4977 | auto *R = static_cast<const RuleMatcher *>(B); |
| 4978 | return std::make_tuple(OpcodeOrder[L->getOpcode()], |
| 4979 | L->getNumOperands()) < |
| 4980 | std::make_tuple(OpcodeOrder[R->getOpcode()], |
| 4981 | R->getNumOperands()); |
| 4982 | }); |
| 4983 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4984 | for (Matcher *Rule : InputRules) |
| 4985 | Rule->optimize(); |
| 4986 | |
| 4987 | std::vector<std::unique_ptr<Matcher>> MatcherStorage; |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 4988 | std::vector<Matcher *> OptRules = |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4989 | optimizeRules<GroupMatcher>(InputRules, MatcherStorage); |
| 4990 | |
| 4991 | for (Matcher *Rule : OptRules) |
| 4992 | Rule->optimize(); |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 4993 | |
Roman Tereshin | 0ee082f | 2018-05-22 19:37:59 +0000 | [diff] [blame] | 4994 | OptRules = optimizeRules<SwitchMatcher>(OptRules, MatcherStorage); |
| 4995 | |
Roman Tereshin | beb3931 | 2018-05-02 20:15:11 +0000 | [diff] [blame] | 4996 | return MatchTable::buildTable(OptRules, WithCoverage); |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 4997 | } |
| 4998 | |
Roman Tereshin | fedae33 | 2018-05-23 02:04:19 +0000 | [diff] [blame] | 4999 | void GroupMatcher::optimize() { |
Roman Tereshin | 9a9fa49 | 2018-05-23 21:30:16 +0000 | [diff] [blame] | 5000 | // Make sure we only sort by a specific predicate within a range of rules that |
| 5001 | // all have that predicate checked against a specific value (not a wildcard): |
| 5002 | auto F = Matchers.begin(); |
| 5003 | auto T = F; |
| 5004 | auto E = Matchers.end(); |
| 5005 | while (T != E) { |
| 5006 | while (T != E) { |
| 5007 | auto *R = static_cast<RuleMatcher *>(*T); |
| 5008 | if (!R->getFirstConditionAsRootType().get().isValid()) |
| 5009 | break; |
| 5010 | ++T; |
| 5011 | } |
| 5012 | std::stable_sort(F, T, [](Matcher *A, Matcher *B) { |
| 5013 | auto *L = static_cast<RuleMatcher *>(A); |
| 5014 | auto *R = static_cast<RuleMatcher *>(B); |
| 5015 | return L->getFirstConditionAsRootType() < |
| 5016 | R->getFirstConditionAsRootType(); |
| 5017 | }); |
| 5018 | if (T != E) |
| 5019 | F = ++T; |
| 5020 | } |
Roman Tereshin | fedae33 | 2018-05-23 02:04:19 +0000 | [diff] [blame] | 5021 | GlobalISelEmitter::optimizeRules<GroupMatcher>(Matchers, MatcherStorage) |
| 5022 | .swap(Matchers); |
Roman Tereshin | a4c410d | 2018-05-24 00:24:15 +0000 | [diff] [blame] | 5023 | GlobalISelEmitter::optimizeRules<SwitchMatcher>(Matchers, MatcherStorage) |
| 5024 | .swap(Matchers); |
Roman Tereshin | fedae33 | 2018-05-23 02:04:19 +0000 | [diff] [blame] | 5025 | } |
| 5026 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 5027 | void GlobalISelEmitter::run(raw_ostream &OS) { |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 5028 | if (!UseCoverageFile.empty()) { |
| 5029 | RuleCoverage = CodeGenCoverage(); |
| 5030 | auto RuleCoverageBufOrErr = MemoryBuffer::getFile(UseCoverageFile); |
| 5031 | if (!RuleCoverageBufOrErr) { |
| 5032 | PrintWarning(SMLoc(), "Missing rule coverage data"); |
| 5033 | RuleCoverage = None; |
| 5034 | } else { |
| 5035 | if (!RuleCoverage->parse(*RuleCoverageBufOrErr.get(), Target.getName())) { |
| 5036 | PrintWarning(SMLoc(), "Ignoring invalid or missing rule coverage data"); |
| 5037 | RuleCoverage = None; |
| 5038 | } |
| 5039 | } |
| 5040 | } |
| 5041 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 5042 | // Track the run-time opcode values |
| 5043 | gatherOpcodeValues(); |
| 5044 | // Track the run-time LLT ID values |
| 5045 | gatherTypeIDValues(); |
| 5046 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 5047 | // Track the GINodeEquiv definitions. |
| 5048 | gatherNodeEquivs(); |
| 5049 | |
| 5050 | emitSourceFileHeader(("Global Instruction Selector for the " + |
| 5051 | Target.getName() + " target").str(), OS); |
Daniel Sanders | b41ce2b | 2017-02-20 14:31:27 +0000 | [diff] [blame] | 5052 | std::vector<RuleMatcher> Rules; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 5053 | // Look through the SelectionDAG patterns we found, possibly emitting some. |
| 5054 | for (const PatternToMatch &Pat : CGP.ptms()) { |
| 5055 | ++NumPatternTotal; |
Daniel Sanders | 7e52367 | 2017-11-11 03:23:44 +0000 | [diff] [blame] | 5056 | |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 5057 | auto MatcherOrErr = runOnPattern(Pat); |
| 5058 | |
| 5059 | // The pattern analysis can fail, indicating an unsupported pattern. |
| 5060 | // Report that if we've been asked to do so. |
| 5061 | if (auto Err = MatcherOrErr.takeError()) { |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 5062 | if (WarnOnSkippedPatterns) { |
| 5063 | PrintWarning(Pat.getSrcRecord()->getLoc(), |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 5064 | "Skipped pattern: " + toString(std::move(Err))); |
| 5065 | } else { |
| 5066 | consumeError(std::move(Err)); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 5067 | } |
Daniel Sanders | b41ce2b | 2017-02-20 14:31:27 +0000 | [diff] [blame] | 5068 | ++NumPatternImportsSkipped; |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 5069 | continue; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 5070 | } |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 5071 | |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 5072 | if (RuleCoverage) { |
| 5073 | if (RuleCoverage->isCovered(MatcherOrErr->getRuleID())) |
| 5074 | ++NumPatternsTested; |
| 5075 | else |
| 5076 | PrintWarning(Pat.getSrcRecord()->getLoc(), |
| 5077 | "Pattern is not covered by a test"); |
| 5078 | } |
Daniel Sanders | b41ce2b | 2017-02-20 14:31:27 +0000 | [diff] [blame] | 5079 | Rules.push_back(std::move(MatcherOrErr.get())); |
| 5080 | } |
| 5081 | |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 5082 | // Comparison function to order records by name. |
| 5083 | auto orderByName = [](const Record *A, const Record *B) { |
| 5084 | return A->getName() < B->getName(); |
| 5085 | }; |
| 5086 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 5087 | std::vector<Record *> ComplexPredicates = |
| 5088 | RK.getAllDerivedDefinitions("GIComplexOperandMatcher"); |
Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 5089 | llvm::sort(ComplexPredicates, orderByName); |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 5090 | |
| 5091 | std::vector<Record *> CustomRendererFns = |
| 5092 | RK.getAllDerivedDefinitions("GICustomOperandRenderer"); |
Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 5093 | llvm::sort(CustomRendererFns, orderByName); |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 5094 | |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 5095 | unsigned MaxTemporaries = 0; |
| 5096 | for (const auto &Rule : Rules) |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 5097 | MaxTemporaries = std::max(MaxTemporaries, Rule.countRendererFns()); |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 5098 | |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 5099 | OS << "#ifdef GET_GLOBALISEL_PREDICATE_BITSET\n" |
| 5100 | << "const unsigned MAX_SUBTARGET_PREDICATES = " << SubtargetFeatures.size() |
| 5101 | << ";\n" |
| 5102 | << "using PredicateBitset = " |
| 5103 | "llvm::PredicateBitsetImpl<MAX_SUBTARGET_PREDICATES>;\n" |
| 5104 | << "#endif // ifdef GET_GLOBALISEL_PREDICATE_BITSET\n\n"; |
| 5105 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 5106 | OS << "#ifdef GET_GLOBALISEL_TEMPORARIES_DECL\n" |
| 5107 | << " mutable MatcherState State;\n" |
| 5108 | << " typedef " |
Daniel Sanders | 1e4569f | 2017-10-20 20:55:29 +0000 | [diff] [blame] | 5109 | "ComplexRendererFns(" |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 5110 | << Target.getName() |
| 5111 | << "InstructionSelector::*ComplexMatcherMemFn)(MachineOperand &) const;\n" |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 5112 | |
| 5113 | << " typedef void(" << Target.getName() |
| 5114 | << "InstructionSelector::*CustomRendererFn)(MachineInstrBuilder &, const " |
| 5115 | "MachineInstr&) " |
| 5116 | "const;\n" |
| 5117 | << " const ISelInfoTy<PredicateBitset, ComplexMatcherMemFn, " |
| 5118 | "CustomRendererFn> " |
| 5119 | "ISelInfo;\n"; |
| 5120 | OS << " static " << Target.getName() |
Daniel Sanders | ea8711b | 2017-10-16 03:36:29 +0000 | [diff] [blame] | 5121 | << "InstructionSelector::ComplexMatcherMemFn ComplexPredicateFns[];\n" |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 5122 | << " static " << Target.getName() |
| 5123 | << "InstructionSelector::CustomRendererFn CustomRenderers[];\n" |
Roman Tereshin | 2df4c22 | 2018-05-02 20:07:15 +0000 | [diff] [blame] | 5124 | << " bool testImmPredicate_I64(unsigned PredicateID, int64_t Imm) const " |
Daniel Sanders | 32de8bb | 2017-12-20 14:41:51 +0000 | [diff] [blame] | 5125 | "override;\n" |
Roman Tereshin | 2df4c22 | 2018-05-02 20:07:15 +0000 | [diff] [blame] | 5126 | << " bool testImmPredicate_APInt(unsigned PredicateID, const APInt &Imm) " |
Daniel Sanders | 32de8bb | 2017-12-20 14:41:51 +0000 | [diff] [blame] | 5127 | "const override;\n" |
Roman Tereshin | 2df4c22 | 2018-05-02 20:07:15 +0000 | [diff] [blame] | 5128 | << " bool testImmPredicate_APFloat(unsigned PredicateID, const APFloat " |
Daniel Sanders | 32de8bb | 2017-12-20 14:41:51 +0000 | [diff] [blame] | 5129 | "&Imm) const override;\n" |
Roman Tereshin | 2df4c22 | 2018-05-02 20:07:15 +0000 | [diff] [blame] | 5130 | << " const int64_t *getMatchTable() const override;\n" |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 5131 | << " bool testMIPredicate_MI(unsigned PredicateID, const MachineInstr &MI) " |
| 5132 | "const override;\n" |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 5133 | << "#endif // ifdef GET_GLOBALISEL_TEMPORARIES_DECL\n\n"; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 5134 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 5135 | OS << "#ifdef GET_GLOBALISEL_TEMPORARIES_INIT\n" |
| 5136 | << ", State(" << MaxTemporaries << "),\n" |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 5137 | << "ISelInfo(TypeObjects, NumTypeObjects, FeatureBitsets" |
| 5138 | << ", ComplexPredicateFns, CustomRenderers)\n" |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 5139 | << "#endif // ifdef GET_GLOBALISEL_TEMPORARIES_INIT\n\n"; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 5140 | |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 5141 | OS << "#ifdef GET_GLOBALISEL_IMPL\n"; |
| 5142 | SubtargetFeatureInfo::emitSubtargetFeatureBitEnumeration(SubtargetFeatures, |
| 5143 | OS); |
Daniel Sanders | e9fdba3 | 2017-04-29 17:30:09 +0000 | [diff] [blame] | 5144 | |
| 5145 | // Separate subtarget features by how often they must be recomputed. |
| 5146 | SubtargetFeatureInfoMap ModuleFeatures; |
| 5147 | std::copy_if(SubtargetFeatures.begin(), SubtargetFeatures.end(), |
| 5148 | std::inserter(ModuleFeatures, ModuleFeatures.end()), |
| 5149 | [](const SubtargetFeatureInfoMap::value_type &X) { |
| 5150 | return !X.second.mustRecomputePerFunction(); |
| 5151 | }); |
| 5152 | SubtargetFeatureInfoMap FunctionFeatures; |
| 5153 | std::copy_if(SubtargetFeatures.begin(), SubtargetFeatures.end(), |
| 5154 | std::inserter(FunctionFeatures, FunctionFeatures.end()), |
| 5155 | [](const SubtargetFeatureInfoMap::value_type &X) { |
| 5156 | return X.second.mustRecomputePerFunction(); |
| 5157 | }); |
| 5158 | |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 5159 | SubtargetFeatureInfo::emitComputeAvailableFeatures( |
Matt Arsenault | f937b43 | 2020-01-08 19:49:30 -0500 | [diff] [blame^] | 5160 | Target.getName(), "InstructionSelector", "computeAvailableModuleFeatures", |
Daniel Sanders | e9fdba3 | 2017-04-29 17:30:09 +0000 | [diff] [blame] | 5161 | ModuleFeatures, OS); |
Hiroshi Yamauchi | 52e3774 | 2019-11-11 10:59:36 -0800 | [diff] [blame] | 5162 | |
Matt Arsenault | f937b43 | 2020-01-08 19:49:30 -0500 | [diff] [blame^] | 5163 | |
| 5164 | OS << "void " << Target.getName() << "InstructionSelector" |
| 5165 | "::setupGeneratedPerFunctionState(MachineFunction &MF) {\n" |
| 5166 | " AvailableFunctionFeatures = computeAvailableFunctionFeatures(" |
| 5167 | "(const " << Target.getName() << "Subtarget*)&MF.getSubtarget(), &MF);\n" |
| 5168 | "}\n"; |
| 5169 | |
Hiroshi Yamauchi | 52e3774 | 2019-11-11 10:59:36 -0800 | [diff] [blame] | 5170 | if (Target.getName() == "X86" || Target.getName() == "AArch64") { |
| 5171 | // TODO: Implement PGSO. |
| 5172 | OS << "static bool shouldOptForSize(const MachineFunction *MF) {\n"; |
| 5173 | OS << " return MF->getFunction().hasOptSize();\n"; |
| 5174 | OS << "}\n\n"; |
| 5175 | } |
| 5176 | |
Daniel Sanders | e9fdba3 | 2017-04-29 17:30:09 +0000 | [diff] [blame] | 5177 | SubtargetFeatureInfo::emitComputeAvailableFeatures( |
| 5178 | Target.getName(), "InstructionSelector", |
| 5179 | "computeAvailableFunctionFeatures", FunctionFeatures, OS, |
| 5180 | "const MachineFunction *MF"); |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 5181 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 5182 | // Emit a table containing the LLT objects needed by the matcher and an enum |
| 5183 | // for the matcher to reference them with. |
Daniel Sanders | 032e7f2 | 2017-08-17 13:18:35 +0000 | [diff] [blame] | 5184 | std::vector<LLTCodeGen> TypeObjects; |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 5185 | for (const auto &Ty : KnownTypes) |
Daniel Sanders | 032e7f2 | 2017-08-17 13:18:35 +0000 | [diff] [blame] | 5186 | TypeObjects.push_back(Ty); |
Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 5187 | llvm::sort(TypeObjects); |
Daniel Sanders | 4998070 | 2017-08-23 10:09:25 +0000 | [diff] [blame] | 5188 | OS << "// LLT Objects.\n" |
| 5189 | << "enum {\n"; |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 5190 | for (const auto &TypeObject : TypeObjects) { |
| 5191 | OS << " "; |
| 5192 | TypeObject.emitCxxEnumValue(OS); |
| 5193 | OS << ",\n"; |
| 5194 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 5195 | OS << "};\n"; |
| 5196 | OS << "const static size_t NumTypeObjects = " << TypeObjects.size() << ";\n" |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 5197 | << "const static LLT TypeObjects[] = {\n"; |
| 5198 | for (const auto &TypeObject : TypeObjects) { |
| 5199 | OS << " "; |
| 5200 | TypeObject.emitCxxConstructorCall(OS); |
| 5201 | OS << ",\n"; |
| 5202 | } |
| 5203 | OS << "};\n\n"; |
| 5204 | |
| 5205 | // Emit a table containing the PredicateBitsets objects needed by the matcher |
| 5206 | // and an enum for the matcher to reference them with. |
| 5207 | std::vector<std::vector<Record *>> FeatureBitsets; |
| 5208 | for (auto &Rule : Rules) |
| 5209 | FeatureBitsets.push_back(Rule.getRequiredFeatures()); |
Fangrui Song | 3507c6e | 2018-09-30 22:31:29 +0000 | [diff] [blame] | 5210 | llvm::sort(FeatureBitsets, [&](const std::vector<Record *> &A, |
| 5211 | const std::vector<Record *> &B) { |
| 5212 | if (A.size() < B.size()) |
| 5213 | return true; |
| 5214 | if (A.size() > B.size()) |
| 5215 | return false; |
Mark de Wever | e8d448e | 2019-12-22 18:58:32 +0100 | [diff] [blame] | 5216 | for (auto Pair : zip(A, B)) { |
Fangrui Song | 3507c6e | 2018-09-30 22:31:29 +0000 | [diff] [blame] | 5217 | if (std::get<0>(Pair)->getName() < std::get<1>(Pair)->getName()) |
| 5218 | return true; |
| 5219 | if (std::get<0>(Pair)->getName() > std::get<1>(Pair)->getName()) |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 5220 | return false; |
Fangrui Song | 3507c6e | 2018-09-30 22:31:29 +0000 | [diff] [blame] | 5221 | } |
| 5222 | return false; |
| 5223 | }); |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 5224 | FeatureBitsets.erase( |
| 5225 | std::unique(FeatureBitsets.begin(), FeatureBitsets.end()), |
| 5226 | FeatureBitsets.end()); |
Daniel Sanders | 4998070 | 2017-08-23 10:09:25 +0000 | [diff] [blame] | 5227 | OS << "// Feature bitsets.\n" |
| 5228 | << "enum {\n" |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 5229 | << " GIFBS_Invalid,\n"; |
| 5230 | for (const auto &FeatureBitset : FeatureBitsets) { |
| 5231 | if (FeatureBitset.empty()) |
| 5232 | continue; |
| 5233 | OS << " " << getNameForFeatureBitset(FeatureBitset) << ",\n"; |
| 5234 | } |
| 5235 | OS << "};\n" |
| 5236 | << "const static PredicateBitset FeatureBitsets[] {\n" |
| 5237 | << " {}, // GIFBS_Invalid\n"; |
| 5238 | for (const auto &FeatureBitset : FeatureBitsets) { |
| 5239 | if (FeatureBitset.empty()) |
| 5240 | continue; |
| 5241 | OS << " {"; |
| 5242 | for (const auto &Feature : FeatureBitset) { |
| 5243 | const auto &I = SubtargetFeatures.find(Feature); |
| 5244 | assert(I != SubtargetFeatures.end() && "Didn't import predicate?"); |
| 5245 | OS << I->second.getEnumBitName() << ", "; |
| 5246 | } |
| 5247 | OS << "},\n"; |
| 5248 | } |
| 5249 | OS << "};\n\n"; |
| 5250 | |
| 5251 | // Emit complex predicate table and an enum to reference them with. |
Daniel Sanders | 4998070 | 2017-08-23 10:09:25 +0000 | [diff] [blame] | 5252 | OS << "// ComplexPattern predicates.\n" |
| 5253 | << "enum {\n" |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 5254 | << " GICP_Invalid,\n"; |
| 5255 | for (const auto &Record : ComplexPredicates) |
| 5256 | OS << " GICP_" << Record->getName() << ",\n"; |
| 5257 | OS << "};\n" |
| 5258 | << "// See constructor for table contents\n\n"; |
| 5259 | |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 5260 | emitImmPredicateFns(OS, "I64", "int64_t", [](const Record *R) { |
Daniel Sanders | 649c585 | 2017-10-13 20:42:18 +0000 | [diff] [blame] | 5261 | bool Unset; |
| 5262 | return !R->getValueAsBitOrUnset("IsAPFloat", Unset) && |
| 5263 | !R->getValueAsBit("IsAPInt"); |
| 5264 | }); |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 5265 | emitImmPredicateFns(OS, "APFloat", "const APFloat &", [](const Record *R) { |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 5266 | bool Unset; |
| 5267 | return R->getValueAsBitOrUnset("IsAPFloat", Unset); |
| 5268 | }); |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 5269 | emitImmPredicateFns(OS, "APInt", "const APInt &", [](const Record *R) { |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 5270 | return R->getValueAsBit("IsAPInt"); |
| 5271 | }); |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 5272 | emitMIPredicateFns(OS); |
Daniel Sanders | ea8711b | 2017-10-16 03:36:29 +0000 | [diff] [blame] | 5273 | OS << "\n"; |
| 5274 | |
| 5275 | OS << Target.getName() << "InstructionSelector::ComplexMatcherMemFn\n" |
| 5276 | << Target.getName() << "InstructionSelector::ComplexPredicateFns[] = {\n" |
| 5277 | << " nullptr, // GICP_Invalid\n"; |
| 5278 | for (const auto &Record : ComplexPredicates) |
| 5279 | OS << " &" << Target.getName() |
| 5280 | << "InstructionSelector::" << Record->getValueAsString("MatcherFn") |
| 5281 | << ", // " << Record->getName() << "\n"; |
| 5282 | OS << "};\n\n"; |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 5283 | |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 5284 | OS << "// Custom renderers.\n" |
| 5285 | << "enum {\n" |
| 5286 | << " GICR_Invalid,\n"; |
| 5287 | for (const auto &Record : CustomRendererFns) |
| 5288 | OS << " GICR_" << Record->getValueAsString("RendererFn") << ", \n"; |
| 5289 | OS << "};\n"; |
| 5290 | |
| 5291 | OS << Target.getName() << "InstructionSelector::CustomRendererFn\n" |
| 5292 | << Target.getName() << "InstructionSelector::CustomRenderers[] = {\n" |
Matt Arsenault | 0274ed9 | 2020-01-08 18:57:44 -0500 | [diff] [blame] | 5293 | << " nullptr, // GICR_Invalid\n"; |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 5294 | for (const auto &Record : CustomRendererFns) |
| 5295 | OS << " &" << Target.getName() |
| 5296 | << "InstructionSelector::" << Record->getValueAsString("RendererFn") |
| 5297 | << ", // " << Record->getName() << "\n"; |
| 5298 | OS << "};\n\n"; |
| 5299 | |
Fangrui Song | efd94c5 | 2019-04-23 14:51:27 +0000 | [diff] [blame] | 5300 | llvm::stable_sort(Rules, [&](const RuleMatcher &A, const RuleMatcher &B) { |
Aditya Nandakumar | b63e763 | 2018-02-16 22:37:15 +0000 | [diff] [blame] | 5301 | int ScoreA = RuleMatcherScores[A.getRuleID()]; |
| 5302 | int ScoreB = RuleMatcherScores[B.getRuleID()]; |
| 5303 | if (ScoreA > ScoreB) |
| 5304 | return true; |
| 5305 | if (ScoreB > ScoreA) |
| 5306 | return false; |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 5307 | if (A.isHigherPriorityThan(B)) { |
| 5308 | assert(!B.isHigherPriorityThan(A) && "Cannot be more important " |
| 5309 | "and less important at " |
| 5310 | "the same time"); |
| 5311 | return true; |
| 5312 | } |
| 5313 | return false; |
| 5314 | }); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 5315 | |
Roman Tereshin | 2df4c22 | 2018-05-02 20:07:15 +0000 | [diff] [blame] | 5316 | OS << "bool " << Target.getName() |
| 5317 | << "InstructionSelector::selectImpl(MachineInstr &I, CodeGenCoverage " |
| 5318 | "&CoverageInfo) const {\n" |
| 5319 | << " MachineFunction &MF = *I.getParent()->getParent();\n" |
| 5320 | << " MachineRegisterInfo &MRI = MF.getRegInfo();\n" |
Roman Tereshin | 2df4c22 | 2018-05-02 20:07:15 +0000 | [diff] [blame] | 5321 | << " const PredicateBitset AvailableFeatures = getAvailableFeatures();\n" |
| 5322 | << " NewMIVector OutMIs;\n" |
| 5323 | << " State.MIs.clear();\n" |
| 5324 | << " State.MIs.push_back(&I);\n\n" |
| 5325 | << " if (executeMatchTable(*this, OutMIs, State, ISelInfo" |
| 5326 | << ", getMatchTable(), TII, MRI, TRI, RBI, AvailableFeatures" |
| 5327 | << ", CoverageInfo)) {\n" |
| 5328 | << " return true;\n" |
| 5329 | << " }\n\n" |
| 5330 | << " return false;\n" |
| 5331 | << "}\n\n"; |
| 5332 | |
Roman Tereshin | beb3931 | 2018-05-02 20:15:11 +0000 | [diff] [blame] | 5333 | const MatchTable Table = |
| 5334 | buildMatchTable(Rules, OptimizeMatchTable, GenerateCoverage); |
Roman Tereshin | 2df4c22 | 2018-05-02 20:07:15 +0000 | [diff] [blame] | 5335 | OS << "const int64_t *" << Target.getName() |
| 5336 | << "InstructionSelector::getMatchTable() const {\n"; |
| 5337 | Table.emitDeclaration(OS); |
| 5338 | OS << " return "; |
| 5339 | Table.emitUse(OS); |
| 5340 | OS << ";\n}\n"; |
| 5341 | OS << "#endif // ifdef GET_GLOBALISEL_IMPL\n"; |
Daniel Sanders | e9fdba3 | 2017-04-29 17:30:09 +0000 | [diff] [blame] | 5342 | |
| 5343 | OS << "#ifdef GET_GLOBALISEL_PREDICATES_DECL\n" |
| 5344 | << "PredicateBitset AvailableModuleFeatures;\n" |
| 5345 | << "mutable PredicateBitset AvailableFunctionFeatures;\n" |
| 5346 | << "PredicateBitset getAvailableFeatures() const {\n" |
| 5347 | << " return AvailableModuleFeatures | AvailableFunctionFeatures;\n" |
| 5348 | << "}\n" |
| 5349 | << "PredicateBitset\n" |
| 5350 | << "computeAvailableModuleFeatures(const " << Target.getName() |
| 5351 | << "Subtarget *Subtarget) const;\n" |
| 5352 | << "PredicateBitset\n" |
| 5353 | << "computeAvailableFunctionFeatures(const " << Target.getName() |
| 5354 | << "Subtarget *Subtarget,\n" |
| 5355 | << " const MachineFunction *MF) const;\n" |
Matt Arsenault | f937b43 | 2020-01-08 19:49:30 -0500 | [diff] [blame^] | 5356 | << "void setupGeneratedPerFunctionState(MachineFunction &MF) override;\n" |
Daniel Sanders | e9fdba3 | 2017-04-29 17:30:09 +0000 | [diff] [blame] | 5357 | << "#endif // ifdef GET_GLOBALISEL_PREDICATES_DECL\n"; |
| 5358 | |
| 5359 | OS << "#ifdef GET_GLOBALISEL_PREDICATES_INIT\n" |
| 5360 | << "AvailableModuleFeatures(computeAvailableModuleFeatures(&STI)),\n" |
| 5361 | << "AvailableFunctionFeatures()\n" |
| 5362 | << "#endif // ifdef GET_GLOBALISEL_PREDICATES_INIT\n"; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 5363 | } |
| 5364 | |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 5365 | void GlobalISelEmitter::declareSubtargetFeature(Record *Predicate) { |
| 5366 | if (SubtargetFeatures.count(Predicate) == 0) |
| 5367 | SubtargetFeatures.emplace( |
| 5368 | Predicate, SubtargetFeatureInfo(Predicate, SubtargetFeatures.size())); |
| 5369 | } |
| 5370 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 5371 | void RuleMatcher::optimize() { |
| 5372 | for (auto &Item : InsnVariableIDs) { |
| 5373 | InstructionMatcher &InsnMatcher = *Item.first; |
| 5374 | for (auto &OM : InsnMatcher.operands()) { |
Roman Tereshin | 5f5e550 | 2018-05-23 23:58:10 +0000 | [diff] [blame] | 5375 | // Complex Patterns are usually expensive and they relatively rarely fail |
| 5376 | // on their own: more often we end up throwing away all the work done by a |
| 5377 | // matching part of a complex pattern because some other part of the |
| 5378 | // enclosing pattern didn't match. All of this makes it beneficial to |
| 5379 | // delay complex patterns until the very end of the rule matching, |
| 5380 | // especially for targets having lots of complex patterns. |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 5381 | for (auto &OP : OM->predicates()) |
Roman Tereshin | 5f5e550 | 2018-05-23 23:58:10 +0000 | [diff] [blame] | 5382 | if (isa<ComplexPatternOperandMatcher>(OP)) |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 5383 | EpilogueMatchers.emplace_back(std::move(OP)); |
| 5384 | OM->eraseNullPredicates(); |
| 5385 | } |
| 5386 | InsnMatcher.optimize(); |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 5387 | } |
Fangrui Song | 3507c6e | 2018-09-30 22:31:29 +0000 | [diff] [blame] | 5388 | llvm::sort(EpilogueMatchers, [](const std::unique_ptr<PredicateMatcher> &L, |
| 5389 | const std::unique_ptr<PredicateMatcher> &R) { |
| 5390 | return std::make_tuple(L->getKind(), L->getInsnVarID(), L->getOpIdx()) < |
| 5391 | std::make_tuple(R->getKind(), R->getInsnVarID(), R->getOpIdx()); |
| 5392 | }); |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 5393 | } |
| 5394 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 5395 | bool RuleMatcher::hasFirstCondition() const { |
| 5396 | if (insnmatchers_empty()) |
| 5397 | return false; |
| 5398 | InstructionMatcher &Matcher = insnmatchers_front(); |
| 5399 | if (!Matcher.predicates_empty()) |
| 5400 | return true; |
| 5401 | for (auto &OM : Matcher.operands()) |
| 5402 | for (auto &OP : OM->predicates()) |
| 5403 | if (!isa<InstructionOperandMatcher>(OP)) |
| 5404 | return true; |
| 5405 | return false; |
| 5406 | } |
| 5407 | |
| 5408 | const PredicateMatcher &RuleMatcher::getFirstCondition() const { |
| 5409 | assert(!insnmatchers_empty() && |
| 5410 | "Trying to get a condition from an empty RuleMatcher"); |
| 5411 | |
| 5412 | InstructionMatcher &Matcher = insnmatchers_front(); |
| 5413 | if (!Matcher.predicates_empty()) |
| 5414 | return **Matcher.predicates_begin(); |
| 5415 | // If there is no more predicate on the instruction itself, look at its |
| 5416 | // operands. |
| 5417 | for (auto &OM : Matcher.operands()) |
| 5418 | for (auto &OP : OM->predicates()) |
| 5419 | if (!isa<InstructionOperandMatcher>(OP)) |
| 5420 | return *OP; |
| 5421 | |
| 5422 | llvm_unreachable("Trying to get a condition from an InstructionMatcher with " |
| 5423 | "no conditions"); |
| 5424 | } |
| 5425 | |
| 5426 | std::unique_ptr<PredicateMatcher> RuleMatcher::popFirstCondition() { |
| 5427 | assert(!insnmatchers_empty() && |
| 5428 | "Trying to pop a condition from an empty RuleMatcher"); |
| 5429 | |
| 5430 | InstructionMatcher &Matcher = insnmatchers_front(); |
| 5431 | if (!Matcher.predicates_empty()) |
| 5432 | return Matcher.predicates_pop_front(); |
| 5433 | // If there is no more predicate on the instruction itself, look at its |
| 5434 | // operands. |
| 5435 | for (auto &OM : Matcher.operands()) |
| 5436 | for (auto &OP : OM->predicates()) |
| 5437 | if (!isa<InstructionOperandMatcher>(OP)) { |
| 5438 | std::unique_ptr<PredicateMatcher> Result = std::move(OP); |
| 5439 | OM->eraseNullPredicates(); |
| 5440 | return Result; |
| 5441 | } |
| 5442 | |
| 5443 | llvm_unreachable("Trying to pop a condition from an InstructionMatcher with " |
| 5444 | "no conditions"); |
| 5445 | } |
| 5446 | |
| 5447 | bool GroupMatcher::candidateConditionMatches( |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 5448 | const PredicateMatcher &Predicate) const { |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 5449 | |
| 5450 | if (empty()) { |
| 5451 | // Sharing predicates for nested instructions is not supported yet as we |
| 5452 | // currently don't hoist the GIM_RecordInsn's properly, therefore we can |
| 5453 | // only work on the original root instruction (InsnVarID == 0): |
| 5454 | if (Predicate.getInsnVarID() != 0) |
| 5455 | return false; |
| 5456 | // ... otherwise an empty group can handle any predicate with no specific |
| 5457 | // requirements: |
| 5458 | return true; |
| 5459 | } |
| 5460 | |
| 5461 | const Matcher &Representative = **Matchers.begin(); |
| 5462 | const auto &RepresentativeCondition = Representative.getFirstCondition(); |
| 5463 | // ... if not empty, the group can only accomodate matchers with the exact |
| 5464 | // same first condition: |
| 5465 | return Predicate.isIdentical(RepresentativeCondition); |
| 5466 | } |
| 5467 | |
| 5468 | bool GroupMatcher::addMatcher(Matcher &Candidate) { |
| 5469 | if (!Candidate.hasFirstCondition()) |
| 5470 | return false; |
| 5471 | |
| 5472 | const PredicateMatcher &Predicate = Candidate.getFirstCondition(); |
| 5473 | if (!candidateConditionMatches(Predicate)) |
| 5474 | return false; |
| 5475 | |
| 5476 | Matchers.push_back(&Candidate); |
| 5477 | return true; |
| 5478 | } |
| 5479 | |
| 5480 | void GroupMatcher::finalize() { |
| 5481 | assert(Conditions.empty() && "Already finalized?"); |
| 5482 | if (empty()) |
| 5483 | return; |
| 5484 | |
| 5485 | Matcher &FirstRule = **Matchers.begin(); |
Roman Tereshin | 152fc16 | 2018-05-23 22:50:53 +0000 | [diff] [blame] | 5486 | for (;;) { |
| 5487 | // All the checks are expected to succeed during the first iteration: |
| 5488 | for (const auto &Rule : Matchers) |
| 5489 | if (!Rule->hasFirstCondition()) |
| 5490 | return; |
| 5491 | const auto &FirstCondition = FirstRule.getFirstCondition(); |
| 5492 | for (unsigned I = 1, E = Matchers.size(); I < E; ++I) |
| 5493 | if (!Matchers[I]->getFirstCondition().isIdentical(FirstCondition)) |
| 5494 | return; |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 5495 | |
Roman Tereshin | 152fc16 | 2018-05-23 22:50:53 +0000 | [diff] [blame] | 5496 | Conditions.push_back(FirstRule.popFirstCondition()); |
| 5497 | for (unsigned I = 1, E = Matchers.size(); I < E; ++I) |
| 5498 | Matchers[I]->popFirstCondition(); |
| 5499 | } |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 5500 | } |
| 5501 | |
| 5502 | void GroupMatcher::emit(MatchTable &Table) { |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 5503 | unsigned LabelID = ~0U; |
| 5504 | if (!Conditions.empty()) { |
| 5505 | LabelID = Table.allocateLabelID(); |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 5506 | Table << MatchTable::Opcode("GIM_Try", +1) |
| 5507 | << MatchTable::Comment("On fail goto") |
| 5508 | << MatchTable::JumpTarget(LabelID) << MatchTable::LineBreak; |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 5509 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 5510 | for (auto &Condition : Conditions) |
| 5511 | Condition->emitPredicateOpcodes( |
| 5512 | Table, *static_cast<RuleMatcher *>(*Matchers.begin())); |
| 5513 | |
| 5514 | for (const auto &M : Matchers) |
| 5515 | M->emit(Table); |
| 5516 | |
| 5517 | // Exit the group |
| 5518 | if (!Conditions.empty()) |
| 5519 | Table << MatchTable::Opcode("GIM_Reject", -1) << MatchTable::LineBreak |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 5520 | << MatchTable::Label(LabelID); |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 5521 | } |
| 5522 | |
Roman Tereshin | 0ee082f | 2018-05-22 19:37:59 +0000 | [diff] [blame] | 5523 | bool SwitchMatcher::isSupportedPredicateType(const PredicateMatcher &P) { |
Roman Tereshin | a4c410d | 2018-05-24 00:24:15 +0000 | [diff] [blame] | 5524 | return isa<InstructionOpcodeMatcher>(P) || isa<LLTOperandMatcher>(P); |
Roman Tereshin | 0ee082f | 2018-05-22 19:37:59 +0000 | [diff] [blame] | 5525 | } |
| 5526 | |
| 5527 | bool SwitchMatcher::candidateConditionMatches( |
| 5528 | const PredicateMatcher &Predicate) const { |
| 5529 | |
| 5530 | if (empty()) { |
| 5531 | // Sharing predicates for nested instructions is not supported yet as we |
| 5532 | // currently don't hoist the GIM_RecordInsn's properly, therefore we can |
| 5533 | // only work on the original root instruction (InsnVarID == 0): |
| 5534 | if (Predicate.getInsnVarID() != 0) |
| 5535 | return false; |
| 5536 | // ... while an attempt to add even a root matcher to an empty SwitchMatcher |
| 5537 | // could fail as not all the types of conditions are supported: |
| 5538 | if (!isSupportedPredicateType(Predicate)) |
| 5539 | return false; |
| 5540 | // ... or the condition might not have a proper implementation of |
| 5541 | // getValue() / isIdenticalDownToValue() yet: |
| 5542 | if (!Predicate.hasValue()) |
| 5543 | return false; |
| 5544 | // ... otherwise an empty Switch can accomodate the condition with no |
| 5545 | // further requirements: |
| 5546 | return true; |
| 5547 | } |
| 5548 | |
| 5549 | const Matcher &CaseRepresentative = **Matchers.begin(); |
| 5550 | const auto &RepresentativeCondition = CaseRepresentative.getFirstCondition(); |
| 5551 | // Switch-cases must share the same kind of condition and path to the value it |
| 5552 | // checks: |
| 5553 | if (!Predicate.isIdenticalDownToValue(RepresentativeCondition)) |
| 5554 | return false; |
| 5555 | |
| 5556 | const auto Value = Predicate.getValue(); |
| 5557 | // ... but be unique with respect to the actual value they check: |
| 5558 | return Values.count(Value) == 0; |
| 5559 | } |
| 5560 | |
| 5561 | bool SwitchMatcher::addMatcher(Matcher &Candidate) { |
| 5562 | if (!Candidate.hasFirstCondition()) |
| 5563 | return false; |
| 5564 | |
| 5565 | const PredicateMatcher &Predicate = Candidate.getFirstCondition(); |
| 5566 | if (!candidateConditionMatches(Predicate)) |
| 5567 | return false; |
| 5568 | const auto Value = Predicate.getValue(); |
| 5569 | Values.insert(Value); |
| 5570 | |
| 5571 | Matchers.push_back(&Candidate); |
| 5572 | return true; |
| 5573 | } |
| 5574 | |
| 5575 | void SwitchMatcher::finalize() { |
| 5576 | assert(Condition == nullptr && "Already finalized"); |
| 5577 | assert(Values.size() == Matchers.size() && "Broken SwitchMatcher"); |
| 5578 | if (empty()) |
| 5579 | return; |
| 5580 | |
| 5581 | std::stable_sort(Matchers.begin(), Matchers.end(), |
| 5582 | [](const Matcher *L, const Matcher *R) { |
| 5583 | return L->getFirstCondition().getValue() < |
| 5584 | R->getFirstCondition().getValue(); |
| 5585 | }); |
| 5586 | Condition = Matchers[0]->popFirstCondition(); |
| 5587 | for (unsigned I = 1, E = Values.size(); I < E; ++I) |
| 5588 | Matchers[I]->popFirstCondition(); |
| 5589 | } |
| 5590 | |
| 5591 | void SwitchMatcher::emitPredicateSpecificOpcodes(const PredicateMatcher &P, |
| 5592 | MatchTable &Table) { |
| 5593 | assert(isSupportedPredicateType(P) && "Predicate type is not supported"); |
| 5594 | |
| 5595 | if (const auto *Condition = dyn_cast<InstructionOpcodeMatcher>(&P)) { |
| 5596 | Table << MatchTable::Opcode("GIM_SwitchOpcode") << MatchTable::Comment("MI") |
| 5597 | << MatchTable::IntValue(Condition->getInsnVarID()); |
| 5598 | return; |
| 5599 | } |
Roman Tereshin | a4c410d | 2018-05-24 00:24:15 +0000 | [diff] [blame] | 5600 | if (const auto *Condition = dyn_cast<LLTOperandMatcher>(&P)) { |
| 5601 | Table << MatchTable::Opcode("GIM_SwitchType") << MatchTable::Comment("MI") |
| 5602 | << MatchTable::IntValue(Condition->getInsnVarID()) |
| 5603 | << MatchTable::Comment("Op") |
| 5604 | << MatchTable::IntValue(Condition->getOpIdx()); |
| 5605 | return; |
| 5606 | } |
Roman Tereshin | 0ee082f | 2018-05-22 19:37:59 +0000 | [diff] [blame] | 5607 | |
| 5608 | llvm_unreachable("emitPredicateSpecificOpcodes is broken: can not handle a " |
| 5609 | "predicate type that is claimed to be supported"); |
| 5610 | } |
| 5611 | |
| 5612 | void SwitchMatcher::emit(MatchTable &Table) { |
| 5613 | assert(Values.size() == Matchers.size() && "Broken SwitchMatcher"); |
| 5614 | if (empty()) |
| 5615 | return; |
| 5616 | assert(Condition != nullptr && |
| 5617 | "Broken SwitchMatcher, hasn't been finalized?"); |
| 5618 | |
| 5619 | std::vector<unsigned> LabelIDs(Values.size()); |
| 5620 | std::generate(LabelIDs.begin(), LabelIDs.end(), |
| 5621 | [&Table]() { return Table.allocateLabelID(); }); |
| 5622 | const unsigned Default = Table.allocateLabelID(); |
| 5623 | |
| 5624 | const int64_t LowerBound = Values.begin()->getRawValue(); |
| 5625 | const int64_t UpperBound = Values.rbegin()->getRawValue() + 1; |
| 5626 | |
| 5627 | emitPredicateSpecificOpcodes(*Condition, Table); |
| 5628 | |
| 5629 | Table << MatchTable::Comment("[") << MatchTable::IntValue(LowerBound) |
| 5630 | << MatchTable::IntValue(UpperBound) << MatchTable::Comment(")") |
| 5631 | << MatchTable::Comment("default:") << MatchTable::JumpTarget(Default); |
| 5632 | |
| 5633 | int64_t J = LowerBound; |
| 5634 | auto VI = Values.begin(); |
| 5635 | for (unsigned I = 0, E = Values.size(); I < E; ++I) { |
| 5636 | auto V = *VI++; |
| 5637 | while (J++ < V.getRawValue()) |
| 5638 | Table << MatchTable::IntValue(0); |
| 5639 | V.turnIntoComment(); |
| 5640 | Table << MatchTable::LineBreak << V << MatchTable::JumpTarget(LabelIDs[I]); |
| 5641 | } |
| 5642 | Table << MatchTable::LineBreak; |
| 5643 | |
| 5644 | for (unsigned I = 0, E = Values.size(); I < E; ++I) { |
| 5645 | Table << MatchTable::Label(LabelIDs[I]); |
| 5646 | Matchers[I]->emit(Table); |
| 5647 | Table << MatchTable::Opcode("GIM_Reject") << MatchTable::LineBreak; |
| 5648 | } |
| 5649 | Table << MatchTable::Label(Default); |
| 5650 | } |
| 5651 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 5652 | unsigned OperandMatcher::getInsnVarID() const { return Insn.getInsnVarID(); } |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 5653 | |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 5654 | } // end anonymous namespace |
| 5655 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 5656 | //===----------------------------------------------------------------------===// |
| 5657 | |
| 5658 | namespace llvm { |
| 5659 | void EmitGlobalISel(RecordKeeper &RK, raw_ostream &OS) { |
| 5660 | GlobalISelEmitter(RK).run(OS); |
| 5661 | } |
| 5662 | } // End llvm namespace |