Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1 | //===- GlobalISelEmitter.cpp - Generate an instruction selector -----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | /// \file |
| 11 | /// This tablegen backend emits code for use by the GlobalISel instruction |
| 12 | /// selector. See include/llvm/CodeGen/TargetGlobalISel.td. |
| 13 | /// |
| 14 | /// This file analyzes the patterns recognized by the SelectionDAGISel tablegen |
| 15 | /// backend, filters out the ones that are unsupported, maps |
| 16 | /// SelectionDAG-specific constructs to their GlobalISel counterpart |
| 17 | /// (when applicable: MVT to LLT; SDNode to generic Instruction). |
| 18 | /// |
| 19 | /// Not all patterns are supported: pass the tablegen invocation |
| 20 | /// "-warn-on-skipped-patterns" to emit a warning when a pattern is skipped, |
| 21 | /// as well as why. |
| 22 | /// |
| 23 | /// The generated file defines a single method: |
| 24 | /// bool <Target>InstructionSelector::selectImpl(MachineInstr &I) const; |
| 25 | /// intended to be used in InstructionSelector::select as the first-step |
| 26 | /// selector for the patterns that don't require complex C++. |
| 27 | /// |
| 28 | /// FIXME: We'll probably want to eventually define a base |
| 29 | /// "TargetGenInstructionSelector" class. |
| 30 | /// |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
| 33 | #include "CodeGenDAGPatterns.h" |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 34 | #include "SubtargetFeatureInfo.h" |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/Optional.h" |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/SmallSet.h" |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/Statistic.h" |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 38 | #include "llvm/Support/CodeGenCoverage.h" |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 39 | #include "llvm/Support/CommandLine.h" |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 40 | #include "llvm/Support/Error.h" |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 41 | #include "llvm/Support/LowLevelTypeImpl.h" |
David Blaikie | 13e77db | 2018-03-23 23:58:25 +0000 | [diff] [blame] | 42 | #include "llvm/Support/MachineValueType.h" |
Pavel Labath | 52a82e2 | 2017-02-21 09:19:41 +0000 | [diff] [blame] | 43 | #include "llvm/Support/ScopedPrinter.h" |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 44 | #include "llvm/TableGen/Error.h" |
| 45 | #include "llvm/TableGen/Record.h" |
| 46 | #include "llvm/TableGen/TableGenBackend.h" |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 47 | #include <numeric> |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 48 | #include <string> |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 49 | using namespace llvm; |
| 50 | |
| 51 | #define DEBUG_TYPE "gisel-emitter" |
| 52 | |
| 53 | STATISTIC(NumPatternTotal, "Total number of patterns"); |
Daniel Sanders | b41ce2b | 2017-02-20 14:31:27 +0000 | [diff] [blame] | 54 | STATISTIC(NumPatternImported, "Number of patterns imported from SelectionDAG"); |
| 55 | STATISTIC(NumPatternImportsSkipped, "Number of SelectionDAG imports skipped"); |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 56 | STATISTIC(NumPatternsTested, "Number of patterns executed according to coverage information"); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 57 | STATISTIC(NumPatternEmitted, "Number of patterns emitted"); |
| 58 | |
Daniel Sanders | 0848b23 | 2017-03-27 13:15:13 +0000 | [diff] [blame] | 59 | cl::OptionCategory GlobalISelEmitterCat("Options for -gen-global-isel"); |
| 60 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 61 | static cl::opt<bool> WarnOnSkippedPatterns( |
| 62 | "warn-on-skipped-patterns", |
| 63 | cl::desc("Explain why a pattern was skipped for inclusion " |
| 64 | "in the GlobalISel selector"), |
Daniel Sanders | 0848b23 | 2017-03-27 13:15:13 +0000 | [diff] [blame] | 65 | cl::init(false), cl::cat(GlobalISelEmitterCat)); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 66 | |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 67 | static cl::opt<bool> GenerateCoverage( |
| 68 | "instrument-gisel-coverage", |
| 69 | cl::desc("Generate coverage instrumentation for GlobalISel"), |
| 70 | cl::init(false), cl::cat(GlobalISelEmitterCat)); |
| 71 | |
| 72 | static cl::opt<std::string> UseCoverageFile( |
| 73 | "gisel-coverage-file", cl::init(""), |
| 74 | cl::desc("Specify file to retrieve coverage information from"), |
| 75 | cl::cat(GlobalISelEmitterCat)); |
| 76 | |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 77 | static cl::opt<bool> OptimizeMatchTable( |
| 78 | "optimize-match-table", |
| 79 | cl::desc("Generate an optimized version of the match table"), |
| 80 | cl::init(true), cl::cat(GlobalISelEmitterCat)); |
| 81 | |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 82 | namespace { |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 83 | //===- Helper functions ---------------------------------------------------===// |
| 84 | |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 85 | /// Get the name of the enum value used to number the predicate function. |
| 86 | std::string getEnumNameForPredicate(const TreePredicateFn &Predicate) { |
Simon Pilgrim | 6ecae9f | 2017-10-14 21:27:53 +0000 | [diff] [blame] | 87 | return "GIPFP_" + Predicate.getImmTypeIdentifier().str() + "_" + |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 88 | Predicate.getFnName(); |
| 89 | } |
| 90 | |
| 91 | /// Get the opcode used to check this predicate. |
| 92 | std::string getMatchOpcodeForPredicate(const TreePredicateFn &Predicate) { |
Simon Pilgrim | 6ecae9f | 2017-10-14 21:27:53 +0000 | [diff] [blame] | 93 | return "GIM_Check" + Predicate.getImmTypeIdentifier().str() + "ImmPredicate"; |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 96 | /// This class stands in for LLT wherever we want to tablegen-erate an |
| 97 | /// equivalent at compiler run-time. |
| 98 | class LLTCodeGen { |
| 99 | private: |
| 100 | LLT Ty; |
| 101 | |
| 102 | public: |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 103 | LLTCodeGen() = default; |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 104 | LLTCodeGen(const LLT &Ty) : Ty(Ty) {} |
| 105 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 106 | std::string getCxxEnumValue() const { |
| 107 | std::string Str; |
| 108 | raw_string_ostream OS(Str); |
| 109 | |
| 110 | emitCxxEnumValue(OS); |
| 111 | return OS.str(); |
| 112 | } |
| 113 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 114 | void emitCxxEnumValue(raw_ostream &OS) const { |
| 115 | if (Ty.isScalar()) { |
| 116 | OS << "GILLT_s" << Ty.getSizeInBits(); |
| 117 | return; |
| 118 | } |
| 119 | if (Ty.isVector()) { |
| 120 | OS << "GILLT_v" << Ty.getNumElements() << "s" << Ty.getScalarSizeInBits(); |
| 121 | return; |
| 122 | } |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 123 | if (Ty.isPointer()) { |
| 124 | OS << "GILLT_p" << Ty.getAddressSpace(); |
| 125 | if (Ty.getSizeInBits() > 0) |
| 126 | OS << "s" << Ty.getSizeInBits(); |
| 127 | return; |
| 128 | } |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 129 | llvm_unreachable("Unhandled LLT"); |
| 130 | } |
| 131 | |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 132 | void emitCxxConstructorCall(raw_ostream &OS) const { |
| 133 | if (Ty.isScalar()) { |
| 134 | OS << "LLT::scalar(" << Ty.getSizeInBits() << ")"; |
| 135 | return; |
| 136 | } |
| 137 | if (Ty.isVector()) { |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 138 | OS << "LLT::vector(" << Ty.getNumElements() << ", " |
| 139 | << Ty.getScalarSizeInBits() << ")"; |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 140 | return; |
| 141 | } |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 142 | if (Ty.isPointer() && Ty.getSizeInBits() > 0) { |
| 143 | OS << "LLT::pointer(" << Ty.getAddressSpace() << ", " |
| 144 | << Ty.getSizeInBits() << ")"; |
| 145 | return; |
| 146 | } |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 147 | llvm_unreachable("Unhandled LLT"); |
| 148 | } |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 149 | |
| 150 | const LLT &get() const { return Ty; } |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 151 | |
Mandeep Singh Grang | 1b0e2f2 | 2018-04-06 20:18:05 +0000 | [diff] [blame] | 152 | /// 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] | 153 | /// particular logic behind the order but either A < B or B < A must be |
| 154 | /// true if A != B. |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 155 | bool operator<(const LLTCodeGen &Other) const { |
Daniel Sanders | 032e7f2 | 2017-08-17 13:18:35 +0000 | [diff] [blame] | 156 | if (Ty.isValid() != Other.Ty.isValid()) |
| 157 | return Ty.isValid() < Other.Ty.isValid(); |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 158 | if (!Ty.isValid()) |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 159 | return false; |
Daniel Sanders | 032e7f2 | 2017-08-17 13:18:35 +0000 | [diff] [blame] | 160 | |
| 161 | if (Ty.isVector() != Other.Ty.isVector()) |
| 162 | return Ty.isVector() < Other.Ty.isVector(); |
| 163 | if (Ty.isScalar() != Other.Ty.isScalar()) |
| 164 | return Ty.isScalar() < Other.Ty.isScalar(); |
| 165 | if (Ty.isPointer() != Other.Ty.isPointer()) |
| 166 | return Ty.isPointer() < Other.Ty.isPointer(); |
| 167 | |
| 168 | if (Ty.isPointer() && Ty.getAddressSpace() != Other.Ty.getAddressSpace()) |
| 169 | return Ty.getAddressSpace() < Other.Ty.getAddressSpace(); |
| 170 | |
| 171 | if (Ty.isVector() && Ty.getNumElements() != Other.Ty.getNumElements()) |
| 172 | return Ty.getNumElements() < Other.Ty.getNumElements(); |
| 173 | |
| 174 | return Ty.getSizeInBits() < Other.Ty.getSizeInBits(); |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 175 | } |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 176 | |
| 177 | bool operator==(const LLTCodeGen &B) const { return Ty == B.Ty; } |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 178 | }; |
| 179 | |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 180 | // Track all types that are used so we can emit the corresponding enum. |
| 181 | std::set<LLTCodeGen> KnownTypes; |
| 182 | |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 183 | class InstructionMatcher; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 184 | /// Convert an MVT to an equivalent LLT if possible, or the invalid LLT() for |
| 185 | /// 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] | 186 | static Optional<LLTCodeGen> MVTToLLT(MVT::SimpleValueType SVT) { |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 187 | MVT VT(SVT); |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 188 | |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 189 | if (VT.isVector() && VT.getVectorNumElements() != 1) |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 190 | return LLTCodeGen( |
| 191 | LLT::vector(VT.getVectorNumElements(), VT.getScalarSizeInBits())); |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 192 | |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 193 | if (VT.isInteger() || VT.isFloatingPoint()) |
| 194 | return LLTCodeGen(LLT::scalar(VT.getSizeInBits())); |
| 195 | return None; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 198 | static std::string explainPredicates(const TreePatternNode *N) { |
| 199 | std::string Explanation = ""; |
| 200 | StringRef Separator = ""; |
| 201 | for (const auto &P : N->getPredicateFns()) { |
| 202 | Explanation += |
| 203 | (Separator + P.getOrigPatFragRecord()->getRecord()->getName()).str(); |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 204 | Separator = ", "; |
| 205 | |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 206 | if (P.isAlwaysTrue()) |
| 207 | Explanation += " always-true"; |
| 208 | if (P.isImmediatePattern()) |
| 209 | Explanation += " immediate"; |
Daniel Sanders | 3f267bf | 2017-10-15 02:06:44 +0000 | [diff] [blame] | 210 | |
| 211 | if (P.isUnindexed()) |
| 212 | Explanation += " unindexed"; |
| 213 | |
| 214 | if (P.isNonExtLoad()) |
| 215 | Explanation += " non-extload"; |
| 216 | if (P.isAnyExtLoad()) |
| 217 | Explanation += " extload"; |
| 218 | if (P.isSignExtLoad()) |
| 219 | Explanation += " sextload"; |
| 220 | if (P.isZeroExtLoad()) |
| 221 | Explanation += " zextload"; |
| 222 | |
| 223 | if (P.isNonTruncStore()) |
| 224 | Explanation += " non-truncstore"; |
| 225 | if (P.isTruncStore()) |
| 226 | Explanation += " truncstore"; |
| 227 | |
| 228 | if (Record *VT = P.getMemoryVT()) |
| 229 | Explanation += (" MemVT=" + VT->getName()).str(); |
| 230 | if (Record *VT = P.getScalarMemoryVT()) |
| 231 | Explanation += (" ScalarVT(MemVT)=" + VT->getName()).str(); |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 232 | |
| 233 | if (P.isAtomicOrderingMonotonic()) |
| 234 | Explanation += " monotonic"; |
| 235 | if (P.isAtomicOrderingAcquire()) |
| 236 | Explanation += " acquire"; |
| 237 | if (P.isAtomicOrderingRelease()) |
| 238 | Explanation += " release"; |
| 239 | if (P.isAtomicOrderingAcquireRelease()) |
| 240 | Explanation += " acq_rel"; |
| 241 | if (P.isAtomicOrderingSequentiallyConsistent()) |
| 242 | Explanation += " seq_cst"; |
Daniel Sanders | 0c43b3a | 2017-11-30 21:05:59 +0000 | [diff] [blame] | 243 | if (P.isAtomicOrderingAcquireOrStronger()) |
| 244 | Explanation += " >=acquire"; |
| 245 | if (P.isAtomicOrderingWeakerThanAcquire()) |
| 246 | Explanation += " <acquire"; |
| 247 | if (P.isAtomicOrderingReleaseOrStronger()) |
| 248 | Explanation += " >=release"; |
| 249 | if (P.isAtomicOrderingWeakerThanRelease()) |
| 250 | Explanation += " <release"; |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 251 | } |
| 252 | return Explanation; |
| 253 | } |
| 254 | |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 255 | std::string explainOperator(Record *Operator) { |
| 256 | if (Operator->isSubClassOf("SDNode")) |
Craig Topper | 2b8419a | 2017-05-31 19:01:11 +0000 | [diff] [blame] | 257 | return (" (" + Operator->getValueAsString("Opcode") + ")").str(); |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 258 | |
| 259 | if (Operator->isSubClassOf("Intrinsic")) |
| 260 | return (" (Operator is an Intrinsic, " + Operator->getName() + ")").str(); |
| 261 | |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 262 | if (Operator->isSubClassOf("ComplexPattern")) |
| 263 | return (" (Operator is an unmapped ComplexPattern, " + Operator->getName() + |
| 264 | ")") |
| 265 | .str(); |
| 266 | |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 267 | if (Operator->isSubClassOf("SDNodeXForm")) |
| 268 | return (" (Operator is an unmapped SDNodeXForm, " + Operator->getName() + |
| 269 | ")") |
| 270 | .str(); |
| 271 | |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 272 | return (" (Operator " + Operator->getName() + " not understood)").str(); |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /// Helper function to let the emitter report skip reason error messages. |
| 276 | static Error failedImport(const Twine &Reason) { |
| 277 | return make_error<StringError>(Reason, inconvertibleErrorCode()); |
| 278 | } |
| 279 | |
| 280 | static Error isTrivialOperatorNode(const TreePatternNode *N) { |
| 281 | std::string Explanation = ""; |
| 282 | std::string Separator = ""; |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 283 | |
| 284 | bool HasUnsupportedPredicate = false; |
| 285 | for (const auto &Predicate : N->getPredicateFns()) { |
| 286 | if (Predicate.isAlwaysTrue()) |
| 287 | continue; |
| 288 | |
| 289 | if (Predicate.isImmediatePattern()) |
| 290 | continue; |
| 291 | |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 292 | if (Predicate.isNonExtLoad() || Predicate.isAnyExtLoad() || |
| 293 | Predicate.isSignExtLoad() || Predicate.isZeroExtLoad()) |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 294 | continue; |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 295 | |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 296 | if (Predicate.isNonTruncStore()) |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 297 | continue; |
| 298 | |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 299 | if (Predicate.isLoad() && Predicate.getMemoryVT()) |
| 300 | continue; |
| 301 | |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 302 | if (Predicate.isLoad() || Predicate.isStore()) { |
| 303 | if (Predicate.isUnindexed()) |
| 304 | continue; |
| 305 | } |
| 306 | |
| 307 | if (Predicate.isAtomic() && Predicate.getMemoryVT()) |
| 308 | continue; |
| 309 | |
| 310 | if (Predicate.isAtomic() && |
| 311 | (Predicate.isAtomicOrderingMonotonic() || |
| 312 | Predicate.isAtomicOrderingAcquire() || |
| 313 | Predicate.isAtomicOrderingRelease() || |
| 314 | Predicate.isAtomicOrderingAcquireRelease() || |
Daniel Sanders | 0c43b3a | 2017-11-30 21:05:59 +0000 | [diff] [blame] | 315 | Predicate.isAtomicOrderingSequentiallyConsistent() || |
| 316 | Predicate.isAtomicOrderingAcquireOrStronger() || |
| 317 | Predicate.isAtomicOrderingWeakerThanAcquire() || |
| 318 | Predicate.isAtomicOrderingReleaseOrStronger() || |
| 319 | Predicate.isAtomicOrderingWeakerThanRelease())) |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 320 | continue; |
| 321 | |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 322 | HasUnsupportedPredicate = true; |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 323 | Explanation = Separator + "Has a predicate (" + explainPredicates(N) + ")"; |
| 324 | Separator = ", "; |
Daniel Sanders | 3f267bf | 2017-10-15 02:06:44 +0000 | [diff] [blame] | 325 | Explanation += (Separator + "first-failing:" + |
| 326 | Predicate.getOrigPatFragRecord()->getRecord()->getName()) |
| 327 | .str(); |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 328 | break; |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 331 | if (!HasUnsupportedPredicate) |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 332 | return Error::success(); |
| 333 | |
| 334 | return failedImport(Explanation); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 337 | static Record *getInitValueAsRegClass(Init *V) { |
| 338 | if (DefInit *VDefInit = dyn_cast<DefInit>(V)) { |
| 339 | if (VDefInit->getDef()->isSubClassOf("RegisterOperand")) |
| 340 | return VDefInit->getDef()->getValueAsDef("RegClass"); |
| 341 | if (VDefInit->getDef()->isSubClassOf("RegisterClass")) |
| 342 | return VDefInit->getDef(); |
| 343 | } |
| 344 | return nullptr; |
| 345 | } |
| 346 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 347 | std::string |
| 348 | getNameForFeatureBitset(const std::vector<Record *> &FeatureBitset) { |
| 349 | std::string Name = "GIFBS"; |
| 350 | for (const auto &Feature : FeatureBitset) |
| 351 | Name += ("_" + Feature->getName()).str(); |
| 352 | return Name; |
| 353 | } |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 354 | |
| 355 | //===- MatchTable Helpers -------------------------------------------------===// |
| 356 | |
| 357 | class MatchTable; |
| 358 | |
| 359 | /// A record to be stored in a MatchTable. |
| 360 | /// |
| 361 | /// This class represents any and all output that may be required to emit the |
| 362 | /// MatchTable. Instances are most often configured to represent an opcode or |
| 363 | /// value that will be emitted to the table with some formatting but it can also |
| 364 | /// represent commas, comments, and other formatting instructions. |
| 365 | struct MatchTableRecord { |
| 366 | enum RecordFlagsBits { |
| 367 | MTRF_None = 0x0, |
| 368 | /// Causes EmitStr to be formatted as comment when emitted. |
| 369 | MTRF_Comment = 0x1, |
| 370 | /// Causes the record value to be followed by a comma when emitted. |
| 371 | MTRF_CommaFollows = 0x2, |
| 372 | /// Causes the record value to be followed by a line break when emitted. |
| 373 | MTRF_LineBreakFollows = 0x4, |
| 374 | /// Indicates that the record defines a label and causes an additional |
| 375 | /// comment to be emitted containing the index of the label. |
| 376 | MTRF_Label = 0x8, |
| 377 | /// Causes the record to be emitted as the index of the label specified by |
| 378 | /// LabelID along with a comment indicating where that label is. |
| 379 | MTRF_JumpTarget = 0x10, |
| 380 | /// Causes the formatter to add a level of indentation before emitting the |
| 381 | /// record. |
| 382 | MTRF_Indent = 0x20, |
| 383 | /// Causes the formatter to remove a level of indentation after emitting the |
| 384 | /// record. |
| 385 | MTRF_Outdent = 0x40, |
| 386 | }; |
| 387 | |
| 388 | /// When MTRF_Label or MTRF_JumpTarget is used, indicates a label id to |
| 389 | /// reference or define. |
| 390 | unsigned LabelID; |
| 391 | /// The string to emit. Depending on the MTRF_* flags it may be a comment, a |
| 392 | /// value, a label name. |
| 393 | std::string EmitStr; |
| 394 | |
| 395 | private: |
| 396 | /// The number of MatchTable elements described by this record. Comments are 0 |
| 397 | /// while values are typically 1. Values >1 may occur when we need to emit |
| 398 | /// values that exceed the size of a MatchTable element. |
| 399 | unsigned NumElements; |
| 400 | |
| 401 | public: |
| 402 | /// A bitfield of RecordFlagsBits flags. |
| 403 | unsigned Flags; |
| 404 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 405 | /// The actual run-time value, if known |
| 406 | int64_t RawValue; |
| 407 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 408 | MatchTableRecord(Optional<unsigned> LabelID_, StringRef EmitStr, |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 409 | unsigned NumElements, unsigned Flags, |
| 410 | int64_t RawValue = std::numeric_limits<int64_t>::min()) |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 411 | : LabelID(LabelID_.hasValue() ? LabelID_.getValue() : ~0u), |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 412 | EmitStr(EmitStr), NumElements(NumElements), Flags(Flags), |
| 413 | RawValue(RawValue) { |
| 414 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 415 | assert((!LabelID_.hasValue() || LabelID != ~0u) && |
| 416 | "This value is reserved for non-labels"); |
| 417 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 418 | MatchTableRecord(const MatchTableRecord &Other) = default; |
| 419 | MatchTableRecord(MatchTableRecord &&Other) = default; |
| 420 | |
| 421 | /// Useful if a Match Table Record gets optimized out |
| 422 | void turnIntoComment() { |
| 423 | Flags |= MTRF_Comment; |
| 424 | Flags &= ~MTRF_CommaFollows; |
| 425 | NumElements = 0; |
| 426 | } |
| 427 | |
| 428 | /// For Jump Table generation purposes |
| 429 | bool operator<(const MatchTableRecord &Other) const { |
| 430 | return RawValue < Other.RawValue; |
| 431 | } |
| 432 | int64_t getRawValue() const { return RawValue; } |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 433 | |
| 434 | void emit(raw_ostream &OS, bool LineBreakNextAfterThis, |
| 435 | const MatchTable &Table) const; |
| 436 | unsigned size() const { return NumElements; } |
| 437 | }; |
| 438 | |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 439 | class Matcher; |
| 440 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 441 | /// Holds the contents of a generated MatchTable to enable formatting and the |
| 442 | /// necessary index tracking needed to support GIM_Try. |
| 443 | class MatchTable { |
| 444 | /// An unique identifier for the table. The generated table will be named |
| 445 | /// MatchTable${ID}. |
| 446 | unsigned ID; |
| 447 | /// The records that make up the table. Also includes comments describing the |
| 448 | /// values being emitted and line breaks to format it. |
| 449 | std::vector<MatchTableRecord> Contents; |
| 450 | /// The currently defined labels. |
| 451 | DenseMap<unsigned, unsigned> LabelMap; |
| 452 | /// Tracks the sum of MatchTableRecord::NumElements as the table is built. |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 453 | unsigned CurrentSize = 0; |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 454 | /// A unique identifier for a MatchTable label. |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 455 | unsigned CurrentLabelID = 0; |
Roman Tereshin | beb3931 | 2018-05-02 20:15:11 +0000 | [diff] [blame] | 456 | /// Determines if the table should be instrumented for rule coverage tracking. |
| 457 | bool IsWithCoverage; |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 458 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 459 | public: |
| 460 | static MatchTableRecord LineBreak; |
| 461 | static MatchTableRecord Comment(StringRef Comment) { |
| 462 | return MatchTableRecord(None, Comment, 0, MatchTableRecord::MTRF_Comment); |
| 463 | } |
| 464 | static MatchTableRecord Opcode(StringRef Opcode, int IndentAdjust = 0) { |
| 465 | unsigned ExtraFlags = 0; |
| 466 | if (IndentAdjust > 0) |
| 467 | ExtraFlags |= MatchTableRecord::MTRF_Indent; |
| 468 | if (IndentAdjust < 0) |
| 469 | ExtraFlags |= MatchTableRecord::MTRF_Outdent; |
| 470 | |
| 471 | return MatchTableRecord(None, Opcode, 1, |
| 472 | MatchTableRecord::MTRF_CommaFollows | ExtraFlags); |
| 473 | } |
| 474 | static MatchTableRecord NamedValue(StringRef NamedValue) { |
| 475 | return MatchTableRecord(None, NamedValue, 1, |
| 476 | MatchTableRecord::MTRF_CommaFollows); |
| 477 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 478 | static MatchTableRecord NamedValue(StringRef NamedValue, int64_t RawValue) { |
| 479 | return MatchTableRecord(None, NamedValue, 1, |
| 480 | MatchTableRecord::MTRF_CommaFollows, RawValue); |
| 481 | } |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 482 | static MatchTableRecord NamedValue(StringRef Namespace, |
| 483 | StringRef NamedValue) { |
| 484 | return MatchTableRecord(None, (Namespace + "::" + NamedValue).str(), 1, |
| 485 | MatchTableRecord::MTRF_CommaFollows); |
| 486 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 487 | static MatchTableRecord NamedValue(StringRef Namespace, StringRef NamedValue, |
| 488 | int64_t RawValue) { |
| 489 | return MatchTableRecord(None, (Namespace + "::" + NamedValue).str(), 1, |
| 490 | MatchTableRecord::MTRF_CommaFollows, RawValue); |
| 491 | } |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 492 | static MatchTableRecord IntValue(int64_t IntValue) { |
| 493 | return MatchTableRecord(None, llvm::to_string(IntValue), 1, |
| 494 | MatchTableRecord::MTRF_CommaFollows); |
| 495 | } |
| 496 | static MatchTableRecord Label(unsigned LabelID) { |
| 497 | return MatchTableRecord(LabelID, "Label " + llvm::to_string(LabelID), 0, |
| 498 | MatchTableRecord::MTRF_Label | |
| 499 | MatchTableRecord::MTRF_Comment | |
| 500 | MatchTableRecord::MTRF_LineBreakFollows); |
| 501 | } |
| 502 | static MatchTableRecord JumpTarget(unsigned LabelID) { |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 503 | return MatchTableRecord(LabelID, "Label " + llvm::to_string(LabelID), 1, |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 504 | MatchTableRecord::MTRF_JumpTarget | |
| 505 | MatchTableRecord::MTRF_Comment | |
| 506 | MatchTableRecord::MTRF_CommaFollows); |
| 507 | } |
| 508 | |
Roman Tereshin | beb3931 | 2018-05-02 20:15:11 +0000 | [diff] [blame] | 509 | static MatchTable buildTable(ArrayRef<Matcher *> Rules, bool WithCoverage); |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 510 | |
Roman Tereshin | beb3931 | 2018-05-02 20:15:11 +0000 | [diff] [blame] | 511 | MatchTable(bool WithCoverage, unsigned ID = 0) |
| 512 | : ID(ID), IsWithCoverage(WithCoverage) {} |
| 513 | |
| 514 | bool isWithCoverage() const { return IsWithCoverage; } |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 515 | |
| 516 | void push_back(const MatchTableRecord &Value) { |
| 517 | if (Value.Flags & MatchTableRecord::MTRF_Label) |
| 518 | defineLabel(Value.LabelID); |
| 519 | Contents.push_back(Value); |
| 520 | CurrentSize += Value.size(); |
| 521 | } |
| 522 | |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 523 | unsigned allocateLabelID() { return CurrentLabelID++; } |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 524 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 525 | void defineLabel(unsigned LabelID) { |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 526 | LabelMap.insert(std::make_pair(LabelID, CurrentSize)); |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | unsigned getLabelIndex(unsigned LabelID) const { |
| 530 | const auto I = LabelMap.find(LabelID); |
| 531 | assert(I != LabelMap.end() && "Use of undeclared label"); |
| 532 | return I->second; |
| 533 | } |
| 534 | |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 535 | void emitUse(raw_ostream &OS) const { OS << "MatchTable" << ID; } |
| 536 | |
| 537 | void emitDeclaration(raw_ostream &OS) const { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 538 | unsigned Indentation = 4; |
Daniel Sanders | cbbbfe4 | 2017-07-27 12:47:31 +0000 | [diff] [blame] | 539 | OS << " constexpr static int64_t MatchTable" << ID << "[] = {"; |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 540 | LineBreak.emit(OS, true, *this); |
| 541 | OS << std::string(Indentation, ' '); |
| 542 | |
| 543 | for (auto I = Contents.begin(), E = Contents.end(); I != E; |
| 544 | ++I) { |
| 545 | bool LineBreakIsNext = false; |
| 546 | const auto &NextI = std::next(I); |
| 547 | |
| 548 | if (NextI != E) { |
| 549 | if (NextI->EmitStr == "" && |
| 550 | NextI->Flags == MatchTableRecord::MTRF_LineBreakFollows) |
| 551 | LineBreakIsNext = true; |
| 552 | } |
| 553 | |
| 554 | if (I->Flags & MatchTableRecord::MTRF_Indent) |
| 555 | Indentation += 2; |
| 556 | |
| 557 | I->emit(OS, LineBreakIsNext, *this); |
| 558 | if (I->Flags & MatchTableRecord::MTRF_LineBreakFollows) |
| 559 | OS << std::string(Indentation, ' '); |
| 560 | |
| 561 | if (I->Flags & MatchTableRecord::MTRF_Outdent) |
| 562 | Indentation -= 2; |
| 563 | } |
| 564 | OS << "};\n"; |
| 565 | } |
| 566 | }; |
| 567 | |
| 568 | MatchTableRecord MatchTable::LineBreak = { |
| 569 | None, "" /* Emit String */, 0 /* Elements */, |
| 570 | MatchTableRecord::MTRF_LineBreakFollows}; |
| 571 | |
| 572 | void MatchTableRecord::emit(raw_ostream &OS, bool LineBreakIsNextAfterThis, |
| 573 | const MatchTable &Table) const { |
| 574 | bool UseLineComment = |
| 575 | LineBreakIsNextAfterThis | (Flags & MTRF_LineBreakFollows); |
| 576 | if (Flags & (MTRF_JumpTarget | MTRF_CommaFollows)) |
| 577 | UseLineComment = false; |
| 578 | |
| 579 | if (Flags & MTRF_Comment) |
| 580 | OS << (UseLineComment ? "// " : "/*"); |
| 581 | |
| 582 | OS << EmitStr; |
| 583 | if (Flags & MTRF_Label) |
| 584 | OS << ": @" << Table.getLabelIndex(LabelID); |
| 585 | |
| 586 | if (Flags & MTRF_Comment && !UseLineComment) |
| 587 | OS << "*/"; |
| 588 | |
| 589 | if (Flags & MTRF_JumpTarget) { |
| 590 | if (Flags & MTRF_Comment) |
| 591 | OS << " "; |
| 592 | OS << Table.getLabelIndex(LabelID); |
| 593 | } |
| 594 | |
| 595 | if (Flags & MTRF_CommaFollows) { |
| 596 | OS << ","; |
| 597 | if (!LineBreakIsNextAfterThis && !(Flags & MTRF_LineBreakFollows)) |
| 598 | OS << " "; |
| 599 | } |
| 600 | |
| 601 | if (Flags & MTRF_LineBreakFollows) |
| 602 | OS << "\n"; |
| 603 | } |
| 604 | |
| 605 | MatchTable &operator<<(MatchTable &Table, const MatchTableRecord &Value) { |
| 606 | Table.push_back(Value); |
| 607 | return Table; |
| 608 | } |
| 609 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 610 | //===- Matchers -----------------------------------------------------------===// |
| 611 | |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 612 | class OperandMatcher; |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 613 | class MatchAction; |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 614 | class PredicateMatcher; |
| 615 | class RuleMatcher; |
| 616 | |
| 617 | class Matcher { |
| 618 | public: |
| 619 | virtual ~Matcher() = default; |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 620 | virtual void optimize() {} |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 621 | virtual void emit(MatchTable &Table) = 0; |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 622 | |
| 623 | virtual bool hasFirstCondition() const = 0; |
| 624 | virtual const PredicateMatcher &getFirstCondition() const = 0; |
| 625 | virtual std::unique_ptr<PredicateMatcher> popFirstCondition() = 0; |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 626 | }; |
| 627 | |
Roman Tereshin | beb3931 | 2018-05-02 20:15:11 +0000 | [diff] [blame] | 628 | MatchTable MatchTable::buildTable(ArrayRef<Matcher *> Rules, |
| 629 | bool WithCoverage) { |
| 630 | MatchTable Table(WithCoverage); |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 631 | for (Matcher *Rule : Rules) |
| 632 | Rule->emit(Table); |
| 633 | |
| 634 | return Table << MatchTable::Opcode("GIM_Reject") << MatchTable::LineBreak; |
| 635 | } |
| 636 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 637 | class GroupMatcher final : public Matcher { |
| 638 | /// Conditions that form a common prefix of all the matchers contained. |
| 639 | SmallVector<std::unique_ptr<PredicateMatcher>, 1> Conditions; |
| 640 | |
| 641 | /// All the nested matchers, sharing a common prefix. |
| 642 | std::vector<Matcher *> Matchers; |
| 643 | |
| 644 | /// An owning collection for any auxiliary matchers created while optimizing |
| 645 | /// nested matchers contained. |
| 646 | std::vector<std::unique_ptr<Matcher>> MatcherStorage; |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 647 | |
| 648 | public: |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 649 | /// Add a matcher to the collection of nested matchers if it meets the |
| 650 | /// requirements, and return true. If it doesn't, do nothing and return false. |
| 651 | /// |
| 652 | /// Expected to preserve its argument, so it could be moved out later on. |
| 653 | bool addMatcher(Matcher &Candidate); |
| 654 | |
| 655 | /// Mark the matcher as fully-built and ensure any invariants expected by both |
| 656 | /// optimize() and emit(...) methods. Generally, both sequences of calls |
| 657 | /// are expected to lead to a sensible result: |
| 658 | /// |
| 659 | /// addMatcher(...)*; finalize(); optimize(); emit(...); and |
| 660 | /// addMatcher(...)*; finalize(); emit(...); |
| 661 | /// |
| 662 | /// or generally |
| 663 | /// |
| 664 | /// addMatcher(...)*; finalize(); { optimize()*; emit(...); }* |
| 665 | /// |
| 666 | /// Multiple calls to optimize() are expected to be handled gracefully, though |
| 667 | /// optimize() is not expected to be idempotent. Multiple calls to finalize() |
| 668 | /// aren't generally supported. emit(...) is expected to be non-mutating and |
| 669 | /// producing the exact same results upon repeated calls. |
| 670 | /// |
| 671 | /// addMatcher() calls after the finalize() call are not supported. |
| 672 | /// |
| 673 | /// finalize() and optimize() are both allowed to mutate the contained |
| 674 | /// matchers, so moving them out after finalize() is not supported. |
| 675 | void finalize(); |
Roman Tereshin | fedae33 | 2018-05-23 02:04:19 +0000 | [diff] [blame] | 676 | void optimize() override; |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 677 | void emit(MatchTable &Table) override; |
Quentin Colombet | 34688b9 | 2017-12-18 21:25:53 +0000 | [diff] [blame] | 678 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 679 | /// Could be used to move out the matchers added previously, unless finalize() |
| 680 | /// has been already called. If any of the matchers are moved out, the group |
| 681 | /// becomes safe to destroy, but not safe to re-use for anything else. |
| 682 | iterator_range<std::vector<Matcher *>::iterator> matchers() { |
| 683 | return make_range(Matchers.begin(), Matchers.end()); |
Quentin Colombet | 34688b9 | 2017-12-18 21:25:53 +0000 | [diff] [blame] | 684 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 685 | size_t size() const { return Matchers.size(); } |
| 686 | bool empty() const { return Matchers.empty(); } |
| 687 | |
| 688 | std::unique_ptr<PredicateMatcher> popFirstCondition() override { |
| 689 | assert(!Conditions.empty() && |
| 690 | "Trying to pop a condition from a condition-less group"); |
| 691 | std::unique_ptr<PredicateMatcher> P = std::move(Conditions.front()); |
| 692 | Conditions.erase(Conditions.begin()); |
| 693 | return P; |
| 694 | } |
| 695 | const PredicateMatcher &getFirstCondition() const override { |
| 696 | assert(!Conditions.empty() && |
| 697 | "Trying to get a condition from a condition-less group"); |
| 698 | return *Conditions.front(); |
| 699 | } |
| 700 | bool hasFirstCondition() const override { return !Conditions.empty(); } |
| 701 | |
| 702 | private: |
| 703 | /// See if a candidate matcher could be added to this group solely by |
| 704 | /// analyzing its first condition. |
| 705 | bool candidateConditionMatches(const PredicateMatcher &Predicate) const; |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 706 | }; |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 707 | |
Roman Tereshin | 0ee082f | 2018-05-22 19:37:59 +0000 | [diff] [blame] | 708 | class SwitchMatcher : public Matcher { |
| 709 | /// All the nested matchers, representing distinct switch-cases. The first |
| 710 | /// conditions (as Matcher::getFirstCondition() reports) of all the nested |
| 711 | /// matchers must share the same type and path to a value they check, in other |
| 712 | /// words, be isIdenticalDownToValue, but have different values they check |
| 713 | /// against. |
| 714 | std::vector<Matcher *> Matchers; |
| 715 | |
| 716 | /// The representative condition, with a type and a path (InsnVarID and OpIdx |
| 717 | /// in most cases) shared by all the matchers contained. |
| 718 | std::unique_ptr<PredicateMatcher> Condition = nullptr; |
| 719 | |
| 720 | /// Temporary set used to check that the case values don't repeat within the |
| 721 | /// same switch. |
| 722 | std::set<MatchTableRecord> Values; |
| 723 | |
| 724 | /// An owning collection for any auxiliary matchers created while optimizing |
| 725 | /// nested matchers contained. |
| 726 | std::vector<std::unique_ptr<Matcher>> MatcherStorage; |
| 727 | |
| 728 | public: |
| 729 | bool addMatcher(Matcher &Candidate); |
| 730 | |
| 731 | void finalize(); |
| 732 | void emit(MatchTable &Table) override; |
| 733 | |
| 734 | iterator_range<std::vector<Matcher *>::iterator> matchers() { |
| 735 | return make_range(Matchers.begin(), Matchers.end()); |
| 736 | } |
| 737 | size_t size() const { return Matchers.size(); } |
| 738 | bool empty() const { return Matchers.empty(); } |
| 739 | |
| 740 | std::unique_ptr<PredicateMatcher> popFirstCondition() override { |
| 741 | // SwitchMatcher doesn't have a common first condition for its cases, as all |
| 742 | // the cases only share a kind of a value (a type and a path to it) they |
| 743 | // match, but deliberately differ in the actual value they match. |
| 744 | llvm_unreachable("Trying to pop a condition from a condition-less group"); |
| 745 | } |
| 746 | const PredicateMatcher &getFirstCondition() const override { |
| 747 | llvm_unreachable("Trying to pop a condition from a condition-less group"); |
| 748 | } |
| 749 | bool hasFirstCondition() const override { return false; } |
| 750 | |
| 751 | private: |
| 752 | /// See if the predicate type has a Switch-implementation for it. |
| 753 | static bool isSupportedPredicateType(const PredicateMatcher &Predicate); |
| 754 | |
| 755 | bool candidateConditionMatches(const PredicateMatcher &Predicate) const; |
| 756 | |
| 757 | /// emit()-helper |
| 758 | static void emitPredicateSpecificOpcodes(const PredicateMatcher &P, |
| 759 | MatchTable &Table); |
| 760 | }; |
| 761 | |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 762 | /// Generates code to check that a match rule matches. |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 763 | class RuleMatcher : public Matcher { |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 764 | public: |
Daniel Sanders | 0846452 | 2018-01-29 21:09:12 +0000 | [diff] [blame] | 765 | using ActionList = std::list<std::unique_ptr<MatchAction>>; |
| 766 | using action_iterator = ActionList::iterator; |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 767 | |
| 768 | protected: |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 769 | /// A list of matchers that all need to succeed for the current rule to match. |
| 770 | /// FIXME: This currently supports a single match position but could be |
| 771 | /// extended to support multiple positions to support div/rem fusion or |
| 772 | /// load-multiple instructions. |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 773 | using MatchersTy = std::vector<std::unique_ptr<InstructionMatcher>> ; |
| 774 | MatchersTy Matchers; |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 775 | |
| 776 | /// A list of actions that need to be taken when all predicates in this rule |
| 777 | /// have succeeded. |
Daniel Sanders | 0846452 | 2018-01-29 21:09:12 +0000 | [diff] [blame] | 778 | ActionList Actions; |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 779 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 780 | using DefinedInsnVariablesMap = std::map<InstructionMatcher *, unsigned>; |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 781 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 782 | /// A map of instruction matchers to the local variables |
Daniel Sanders | 078572b | 2017-08-02 11:03:36 +0000 | [diff] [blame] | 783 | DefinedInsnVariablesMap InsnVariableIDs; |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 784 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 785 | using MutatableInsnSet = SmallPtrSet<InstructionMatcher *, 4>; |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 786 | |
| 787 | // The set of instruction matchers that have not yet been claimed for mutation |
| 788 | // by a BuildMI. |
| 789 | MutatableInsnSet MutatableInsns; |
| 790 | |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 791 | /// A map of named operands defined by the matchers that may be referenced by |
| 792 | /// the renderers. |
| 793 | StringMap<OperandMatcher *> DefinedOperands; |
| 794 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 795 | /// ID for the next instruction variable defined with implicitlyDefineInsnVar() |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 796 | unsigned NextInsnVarID; |
| 797 | |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 798 | /// ID for the next output instruction allocated with allocateOutputInsnID() |
| 799 | unsigned NextOutputInsnID; |
| 800 | |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 801 | /// ID for the next temporary register ID allocated with allocateTempRegID() |
| 802 | unsigned NextTempRegID; |
| 803 | |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 804 | std::vector<Record *> RequiredFeatures; |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 805 | std::vector<std::unique_ptr<PredicateMatcher>> EpilogueMatchers; |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 806 | |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 807 | ArrayRef<SMLoc> SrcLoc; |
| 808 | |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 809 | typedef std::tuple<Record *, unsigned, unsigned> |
| 810 | DefinedComplexPatternSubOperand; |
| 811 | typedef StringMap<DefinedComplexPatternSubOperand> |
| 812 | DefinedComplexPatternSubOperandMap; |
| 813 | /// A map of Symbolic Names to ComplexPattern sub-operands. |
| 814 | DefinedComplexPatternSubOperandMap ComplexSubOperands; |
| 815 | |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 816 | uint64_t RuleID; |
| 817 | static uint64_t NextRuleID; |
| 818 | |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 819 | public: |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 820 | RuleMatcher(ArrayRef<SMLoc> SrcLoc) |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 821 | : Matchers(), Actions(), InsnVariableIDs(), MutatableInsns(), |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 822 | DefinedOperands(), NextInsnVarID(0), NextOutputInsnID(0), |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 823 | NextTempRegID(0), SrcLoc(SrcLoc), ComplexSubOperands(), |
| 824 | RuleID(NextRuleID++) {} |
Zachary Turner | b7dbd87 | 2017-03-20 19:56:52 +0000 | [diff] [blame] | 825 | RuleMatcher(RuleMatcher &&Other) = default; |
| 826 | RuleMatcher &operator=(RuleMatcher &&Other) = default; |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 827 | |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 828 | uint64_t getRuleID() const { return RuleID; } |
| 829 | |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 830 | InstructionMatcher &addInstructionMatcher(StringRef SymbolicName); |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 831 | void addRequiredFeature(Record *Feature); |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 832 | const std::vector<Record *> &getRequiredFeatures() const; |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 833 | |
| 834 | template <class Kind, class... Args> Kind &addAction(Args &&... args); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 835 | template <class Kind, class... Args> |
| 836 | action_iterator insertAction(action_iterator InsertPt, Args &&... args); |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 837 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 838 | /// Define an instruction without emitting any code to do so. |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 839 | unsigned implicitlyDefineInsnVar(InstructionMatcher &Matcher); |
| 840 | |
| 841 | unsigned getInsnVarID(InstructionMatcher &InsnMatcher) const; |
Daniel Sanders | 078572b | 2017-08-02 11:03:36 +0000 | [diff] [blame] | 842 | DefinedInsnVariablesMap::const_iterator defined_insn_vars_begin() const { |
| 843 | return InsnVariableIDs.begin(); |
| 844 | } |
| 845 | DefinedInsnVariablesMap::const_iterator defined_insn_vars_end() const { |
| 846 | return InsnVariableIDs.end(); |
| 847 | } |
| 848 | iterator_range<typename DefinedInsnVariablesMap::const_iterator> |
| 849 | defined_insn_vars() const { |
| 850 | return make_range(defined_insn_vars_begin(), defined_insn_vars_end()); |
| 851 | } |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 852 | |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 853 | MutatableInsnSet::const_iterator mutatable_insns_begin() const { |
| 854 | return MutatableInsns.begin(); |
| 855 | } |
| 856 | MutatableInsnSet::const_iterator mutatable_insns_end() const { |
| 857 | return MutatableInsns.end(); |
| 858 | } |
| 859 | iterator_range<typename MutatableInsnSet::const_iterator> |
| 860 | mutatable_insns() const { |
| 861 | return make_range(mutatable_insns_begin(), mutatable_insns_end()); |
| 862 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 863 | void reserveInsnMatcherForMutation(InstructionMatcher *InsnMatcher) { |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 864 | bool R = MutatableInsns.erase(InsnMatcher); |
| 865 | assert(R && "Reserving a mutatable insn that isn't available"); |
| 866 | (void)R; |
| 867 | } |
| 868 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 869 | action_iterator actions_begin() { return Actions.begin(); } |
| 870 | action_iterator actions_end() { return Actions.end(); } |
| 871 | iterator_range<action_iterator> actions() { |
| 872 | return make_range(actions_begin(), actions_end()); |
| 873 | } |
| 874 | |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 875 | void defineOperand(StringRef SymbolicName, OperandMatcher &OM); |
| 876 | |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 877 | void defineComplexSubOperand(StringRef SymbolicName, Record *ComplexPattern, |
| 878 | unsigned RendererID, unsigned SubOperandID) { |
| 879 | assert(ComplexSubOperands.count(SymbolicName) == 0 && "Already defined"); |
| 880 | ComplexSubOperands[SymbolicName] = |
| 881 | std::make_tuple(ComplexPattern, RendererID, SubOperandID); |
| 882 | } |
| 883 | Optional<DefinedComplexPatternSubOperand> |
| 884 | getComplexSubOperand(StringRef SymbolicName) const { |
| 885 | const auto &I = ComplexSubOperands.find(SymbolicName); |
| 886 | if (I == ComplexSubOperands.end()) |
| 887 | return None; |
| 888 | return I->second; |
| 889 | } |
| 890 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 891 | InstructionMatcher &getInstructionMatcher(StringRef SymbolicName) const; |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 892 | const OperandMatcher &getOperandMatcher(StringRef Name) const; |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 893 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 894 | void optimize() override; |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 895 | void emit(MatchTable &Table) override; |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 896 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 897 | /// Compare the priority of this object and B. |
| 898 | /// |
| 899 | /// Returns true if this object is more important than B. |
| 900 | bool isHigherPriorityThan(const RuleMatcher &B) const; |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 901 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 902 | /// Report the maximum number of temporary operands needed by the rule |
| 903 | /// matcher. |
| 904 | unsigned countRendererFns() const; |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 905 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 906 | std::unique_ptr<PredicateMatcher> popFirstCondition() override; |
| 907 | const PredicateMatcher &getFirstCondition() const override; |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 908 | bool hasFirstCondition() const override; |
| 909 | unsigned getNumOperands() const; |
Roman Tereshin | 19da667 | 2018-05-22 04:31:50 +0000 | [diff] [blame] | 910 | StringRef getOpcode() const; |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 911 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 912 | // FIXME: Remove this as soon as possible |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 913 | InstructionMatcher &insnmatchers_front() const { return *Matchers.front(); } |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 914 | |
| 915 | unsigned allocateOutputInsnID() { return NextOutputInsnID++; } |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 916 | unsigned allocateTempRegID() { return NextTempRegID++; } |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 917 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 918 | iterator_range<MatchersTy::iterator> insnmatchers() { |
| 919 | return make_range(Matchers.begin(), Matchers.end()); |
| 920 | } |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 921 | bool insnmatchers_empty() const { return Matchers.empty(); } |
| 922 | void insnmatchers_pop_front() { Matchers.erase(Matchers.begin()); } |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 923 | }; |
| 924 | |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 925 | uint64_t RuleMatcher::NextRuleID = 0; |
| 926 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 927 | using action_iterator = RuleMatcher::action_iterator; |
| 928 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 929 | template <class PredicateTy> class PredicateListMatcher { |
| 930 | private: |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 931 | /// Template instantiations should specialize this to return a string to use |
| 932 | /// for the comment emitted when there are no predicates. |
| 933 | std::string getNoPredicateComment() const; |
| 934 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 935 | protected: |
| 936 | using PredicatesTy = std::deque<std::unique_ptr<PredicateTy>>; |
| 937 | PredicatesTy Predicates; |
Roman Tereshin | f0dc9fa | 2018-05-21 22:04:39 +0000 | [diff] [blame] | 938 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 939 | /// Track if the list of predicates was manipulated by one of the optimization |
| 940 | /// methods. |
| 941 | bool Optimized = false; |
| 942 | |
| 943 | public: |
| 944 | /// Construct a new predicate and add it to the matcher. |
| 945 | template <class Kind, class... Args> |
| 946 | Optional<Kind *> addPredicate(Args &&... args); |
| 947 | |
| 948 | typename PredicatesTy::iterator predicates_begin() { |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 949 | return Predicates.begin(); |
| 950 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 951 | typename PredicatesTy::iterator predicates_end() { |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 952 | return Predicates.end(); |
| 953 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 954 | iterator_range<typename PredicatesTy::iterator> predicates() { |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 955 | return make_range(predicates_begin(), predicates_end()); |
| 956 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 957 | typename PredicatesTy::size_type predicates_size() const { |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 958 | return Predicates.size(); |
| 959 | } |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 960 | bool predicates_empty() const { return Predicates.empty(); } |
| 961 | |
| 962 | std::unique_ptr<PredicateTy> predicates_pop_front() { |
| 963 | std::unique_ptr<PredicateTy> Front = std::move(Predicates.front()); |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 964 | Predicates.pop_front(); |
| 965 | Optimized = true; |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 966 | return Front; |
| 967 | } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 968 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 969 | void prependPredicate(std::unique_ptr<PredicateTy> &&Predicate) { |
| 970 | Predicates.push_front(std::move(Predicate)); |
| 971 | } |
| 972 | |
| 973 | void eraseNullPredicates() { |
| 974 | const auto NewEnd = |
| 975 | std::stable_partition(Predicates.begin(), Predicates.end(), |
| 976 | std::logical_not<std::unique_ptr<PredicateTy>>()); |
| 977 | if (NewEnd != Predicates.begin()) { |
| 978 | Predicates.erase(Predicates.begin(), NewEnd); |
| 979 | Optimized = true; |
| 980 | } |
| 981 | } |
| 982 | |
Daniel Sanders | 9d662d2 | 2017-07-06 10:06:12 +0000 | [diff] [blame] | 983 | /// Emit MatchTable opcodes that tests whether all the predicates are met. |
Ahmed Bougacha | b67a3ce | 2017-01-26 22:07:37 +0000 | [diff] [blame] | 984 | template <class... Args> |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 985 | void emitPredicateListOpcodes(MatchTable &Table, Args &&... args) { |
| 986 | if (Predicates.empty() && !Optimized) { |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 987 | Table << MatchTable::Comment(getNoPredicateComment()) |
| 988 | << MatchTable::LineBreak; |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 989 | return; |
| 990 | } |
| 991 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 992 | for (const auto &Predicate : predicates()) |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 993 | Predicate->emitPredicateOpcodes(Table, std::forward<Args>(args)...); |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 994 | } |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 995 | }; |
| 996 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 997 | class PredicateMatcher { |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 998 | public: |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 999 | /// This enum is used for RTTI and also defines the priority that is given to |
| 1000 | /// the predicate when generating the matcher code. Kinds with higher priority |
| 1001 | /// must be tested first. |
| 1002 | /// |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1003 | /// The relative priority of OPM_LLT, OPM_RegBank, and OPM_MBB do not matter |
| 1004 | /// but OPM_Int must have priority over OPM_RegBank since constant integers |
| 1005 | /// are represented by a virtual register defined by a G_CONSTANT instruction. |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1006 | /// |
| 1007 | /// Note: The relative priority between IPM_ and OPM_ does not matter, they |
| 1008 | /// are currently not compared between each other. |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1009 | enum PredicateKind { |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1010 | IPM_Opcode, |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1011 | IPM_NumOperands, |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1012 | IPM_ImmPredicate, |
| 1013 | IPM_AtomicOrderingMMO, |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 1014 | IPM_MemoryLLTSize, |
| 1015 | IPM_MemoryVsLLTSize, |
Daniel Sanders | 1e4569f | 2017-10-20 20:55:29 +0000 | [diff] [blame] | 1016 | OPM_SameOperand, |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1017 | OPM_ComplexPattern, |
Daniel Sanders | fe12c0f | 2017-07-11 08:57:29 +0000 | [diff] [blame] | 1018 | OPM_IntrinsicID, |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 1019 | OPM_Instruction, |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1020 | OPM_Int, |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 1021 | OPM_LiteralInt, |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1022 | OPM_LLT, |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 1023 | OPM_PointerToAny, |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1024 | OPM_RegBank, |
| 1025 | OPM_MBB, |
| 1026 | }; |
| 1027 | |
| 1028 | protected: |
| 1029 | PredicateKind Kind; |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1030 | unsigned InsnVarID; |
| 1031 | unsigned OpIdx; |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1032 | |
| 1033 | public: |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1034 | PredicateMatcher(PredicateKind Kind, unsigned InsnVarID, unsigned OpIdx = ~0) |
| 1035 | : Kind(Kind), InsnVarID(InsnVarID), OpIdx(OpIdx) {} |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1036 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1037 | unsigned getInsnVarID() const { return InsnVarID; } |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1038 | unsigned getOpIdx() const { return OpIdx; } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1039 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1040 | virtual ~PredicateMatcher() = default; |
| 1041 | /// Emit MatchTable opcodes that check the predicate for the given operand. |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1042 | virtual void emitPredicateOpcodes(MatchTable &Table, |
| 1043 | RuleMatcher &Rule) const = 0; |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1044 | |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1045 | PredicateKind getKind() const { return Kind; } |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 1046 | |
| 1047 | virtual bool isIdentical(const PredicateMatcher &B) const { |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 1048 | return B.getKind() == getKind() && InsnVarID == B.InsnVarID && |
| 1049 | OpIdx == B.OpIdx; |
| 1050 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1051 | |
| 1052 | virtual bool isIdenticalDownToValue(const PredicateMatcher &B) const { |
| 1053 | return hasValue() && PredicateMatcher::isIdentical(B); |
| 1054 | } |
| 1055 | |
| 1056 | virtual MatchTableRecord getValue() const { |
| 1057 | assert(hasValue() && "Can not get a value of a value-less predicate!"); |
| 1058 | llvm_unreachable("Not implemented yet"); |
| 1059 | } |
| 1060 | virtual bool hasValue() const { return false; } |
| 1061 | |
| 1062 | /// Report the maximum number of temporary operands needed by the predicate |
| 1063 | /// matcher. |
| 1064 | virtual unsigned countRendererFns() const { return 0; } |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1065 | }; |
| 1066 | |
| 1067 | /// Generates code to check a predicate of an operand. |
| 1068 | /// |
| 1069 | /// Typical predicates include: |
| 1070 | /// * Operand is a particular register. |
| 1071 | /// * Operand is assigned a particular register bank. |
| 1072 | /// * Operand is an MBB. |
| 1073 | class OperandPredicateMatcher : public PredicateMatcher { |
| 1074 | public: |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1075 | OperandPredicateMatcher(PredicateKind Kind, unsigned InsnVarID, |
| 1076 | unsigned OpIdx) |
| 1077 | : PredicateMatcher(Kind, InsnVarID, OpIdx) {} |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1078 | virtual ~OperandPredicateMatcher() {} |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1079 | |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1080 | /// Compare the priority of this object and B. |
| 1081 | /// |
| 1082 | /// Returns true if this object is more important than B. |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 1083 | virtual bool isHigherPriorityThan(const OperandPredicateMatcher &B) const; |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1084 | }; |
| 1085 | |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 1086 | template <> |
| 1087 | std::string |
| 1088 | PredicateListMatcher<OperandPredicateMatcher>::getNoPredicateComment() const { |
| 1089 | return "No operand predicates"; |
| 1090 | } |
| 1091 | |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 1092 | /// Generates code to check that a register operand is defined by the same exact |
| 1093 | /// one as another. |
| 1094 | class SameOperandMatcher : public OperandPredicateMatcher { |
Daniel Sanders | 1e4569f | 2017-10-20 20:55:29 +0000 | [diff] [blame] | 1095 | std::string MatchingName; |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 1096 | |
| 1097 | public: |
Quentin Colombet | eba10cb | 2017-12-18 22:12:13 +0000 | [diff] [blame] | 1098 | SameOperandMatcher(unsigned InsnVarID, unsigned OpIdx, StringRef MatchingName) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1099 | : OperandPredicateMatcher(OPM_SameOperand, InsnVarID, OpIdx), |
| 1100 | MatchingName(MatchingName) {} |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 1101 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1102 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | 1e4569f | 2017-10-20 20:55:29 +0000 | [diff] [blame] | 1103 | return P->getKind() == OPM_SameOperand; |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1106 | void emitPredicateOpcodes(MatchTable &Table, |
| 1107 | RuleMatcher &Rule) const override; |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1108 | |
| 1109 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1110 | return OperandPredicateMatcher::isIdentical(B) && |
| 1111 | MatchingName == cast<SameOperandMatcher>(&B)->MatchingName; |
| 1112 | } |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 1113 | }; |
| 1114 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1115 | /// Generates code to check that an operand is a particular LLT. |
| 1116 | class LLTOperandMatcher : public OperandPredicateMatcher { |
| 1117 | protected: |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 1118 | LLTCodeGen Ty; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1119 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1120 | public: |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1121 | static std::map<LLTCodeGen, unsigned> TypeIDValues; |
| 1122 | |
| 1123 | static void initTypeIDValuesMap() { |
| 1124 | TypeIDValues.clear(); |
| 1125 | |
| 1126 | unsigned ID = 0; |
| 1127 | for (const LLTCodeGen LLTy : KnownTypes) |
| 1128 | TypeIDValues[LLTy] = ID++; |
| 1129 | } |
| 1130 | |
Quentin Colombet | eba10cb | 2017-12-18 22:12:13 +0000 | [diff] [blame] | 1131 | LLTOperandMatcher(unsigned InsnVarID, unsigned OpIdx, const LLTCodeGen &Ty) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1132 | : OperandPredicateMatcher(OPM_LLT, InsnVarID, OpIdx), Ty(Ty) { |
Daniel Sanders | 032e7f2 | 2017-08-17 13:18:35 +0000 | [diff] [blame] | 1133 | KnownTypes.insert(Ty); |
| 1134 | } |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1135 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1136 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1137 | return P->getKind() == OPM_LLT; |
| 1138 | } |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 1139 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1140 | return OperandPredicateMatcher::isIdentical(B) && |
| 1141 | Ty == cast<LLTOperandMatcher>(&B)->Ty; |
| 1142 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1143 | MatchTableRecord getValue() const override { |
| 1144 | const auto VI = TypeIDValues.find(Ty); |
| 1145 | if (VI == TypeIDValues.end()) |
| 1146 | return MatchTable::NamedValue(getTy().getCxxEnumValue()); |
| 1147 | return MatchTable::NamedValue(getTy().getCxxEnumValue(), VI->second); |
| 1148 | } |
| 1149 | bool hasValue() const override { |
| 1150 | if (TypeIDValues.size() != KnownTypes.size()) |
| 1151 | initTypeIDValuesMap(); |
| 1152 | return TypeIDValues.count(Ty); |
| 1153 | } |
| 1154 | |
| 1155 | LLTCodeGen getTy() const { return Ty; } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1156 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1157 | void emitPredicateOpcodes(MatchTable &Table, |
| 1158 | RuleMatcher &Rule) const override { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1159 | Table << MatchTable::Opcode("GIM_CheckType") << MatchTable::Comment("MI") |
| 1160 | << MatchTable::IntValue(InsnVarID) << MatchTable::Comment("Op") |
| 1161 | << MatchTable::IntValue(OpIdx) << MatchTable::Comment("Type") |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1162 | << getValue() << MatchTable::LineBreak; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1163 | } |
| 1164 | }; |
| 1165 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1166 | std::map<LLTCodeGen, unsigned> LLTOperandMatcher::TypeIDValues; |
| 1167 | |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 1168 | /// Generates code to check that an operand is a pointer to any address space. |
| 1169 | /// |
| 1170 | /// In SelectionDAG, the types did not describe pointers or address spaces. As a |
| 1171 | /// result, iN is used to describe a pointer of N bits to any address space and |
| 1172 | /// PatFrag predicates are typically used to constrain the address space. There's |
| 1173 | /// no reliable means to derive the missing type information from the pattern so |
| 1174 | /// imported rules must test the components of a pointer separately. |
| 1175 | /// |
Daniel Sanders | ea8711b | 2017-10-16 03:36:29 +0000 | [diff] [blame] | 1176 | /// If SizeInBits is zero, then the pointer size will be obtained from the |
| 1177 | /// subtarget. |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 1178 | class PointerToAnyOperandMatcher : public OperandPredicateMatcher { |
| 1179 | protected: |
| 1180 | unsigned SizeInBits; |
| 1181 | |
| 1182 | public: |
Quentin Colombet | eba10cb | 2017-12-18 22:12:13 +0000 | [diff] [blame] | 1183 | PointerToAnyOperandMatcher(unsigned InsnVarID, unsigned OpIdx, |
| 1184 | unsigned SizeInBits) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1185 | : OperandPredicateMatcher(OPM_PointerToAny, InsnVarID, OpIdx), |
| 1186 | SizeInBits(SizeInBits) {} |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 1187 | |
| 1188 | static bool classof(const OperandPredicateMatcher *P) { |
| 1189 | return P->getKind() == OPM_PointerToAny; |
| 1190 | } |
| 1191 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1192 | void emitPredicateOpcodes(MatchTable &Table, |
| 1193 | RuleMatcher &Rule) const override { |
| 1194 | Table << MatchTable::Opcode("GIM_CheckPointerToAny") |
| 1195 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1196 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) |
| 1197 | << MatchTable::Comment("SizeInBits") |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 1198 | << MatchTable::IntValue(SizeInBits) << MatchTable::LineBreak; |
| 1199 | } |
| 1200 | }; |
| 1201 | |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1202 | /// Generates code to check that an operand is a particular target constant. |
| 1203 | class ComplexPatternOperandMatcher : public OperandPredicateMatcher { |
| 1204 | protected: |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1205 | const OperandMatcher &Operand; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1206 | const Record &TheDef; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1207 | |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1208 | unsigned getAllocatedTemporariesBaseID() const; |
| 1209 | |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1210 | public: |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 1211 | bool isIdentical(const PredicateMatcher &B) const override { return false; } |
| 1212 | |
Quentin Colombet | eba10cb | 2017-12-18 22:12:13 +0000 | [diff] [blame] | 1213 | ComplexPatternOperandMatcher(unsigned InsnVarID, unsigned OpIdx, |
| 1214 | const OperandMatcher &Operand, |
| 1215 | const Record &TheDef) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1216 | : OperandPredicateMatcher(OPM_ComplexPattern, InsnVarID, OpIdx), |
| 1217 | Operand(Operand), TheDef(TheDef) {} |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1218 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1219 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1220 | return P->getKind() == OPM_ComplexPattern; |
| 1221 | } |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1222 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1223 | void emitPredicateOpcodes(MatchTable &Table, |
| 1224 | RuleMatcher &Rule) const override { |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 1225 | unsigned ID = getAllocatedTemporariesBaseID(); |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1226 | Table << MatchTable::Opcode("GIM_CheckComplexPattern") |
| 1227 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1228 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) |
| 1229 | << MatchTable::Comment("Renderer") << MatchTable::IntValue(ID) |
| 1230 | << MatchTable::NamedValue(("GICP_" + TheDef.getName()).str()) |
| 1231 | << MatchTable::LineBreak; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 1234 | unsigned countRendererFns() const override { |
| 1235 | return 1; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1236 | } |
| 1237 | }; |
| 1238 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1239 | /// Generates code to check that an operand is in a particular register bank. |
| 1240 | class RegisterBankOperandMatcher : public OperandPredicateMatcher { |
| 1241 | protected: |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1242 | const CodeGenRegisterClass &RC; |
| 1243 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1244 | public: |
Quentin Colombet | eba10cb | 2017-12-18 22:12:13 +0000 | [diff] [blame] | 1245 | RegisterBankOperandMatcher(unsigned InsnVarID, unsigned OpIdx, |
| 1246 | const CodeGenRegisterClass &RC) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1247 | : OperandPredicateMatcher(OPM_RegBank, InsnVarID, OpIdx), RC(RC) {} |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1248 | |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 1249 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1250 | return OperandPredicateMatcher::isIdentical(B) && |
| 1251 | RC.getDef() == cast<RegisterBankOperandMatcher>(&B)->RC.getDef(); |
| 1252 | } |
| 1253 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1254 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1255 | return P->getKind() == OPM_RegBank; |
| 1256 | } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1257 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1258 | void emitPredicateOpcodes(MatchTable &Table, |
| 1259 | RuleMatcher &Rule) const override { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1260 | Table << MatchTable::Opcode("GIM_CheckRegBankForClass") |
| 1261 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1262 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) |
| 1263 | << MatchTable::Comment("RC") |
| 1264 | << MatchTable::NamedValue(RC.getQualifiedName() + "RegClassID") |
| 1265 | << MatchTable::LineBreak; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1266 | } |
| 1267 | }; |
| 1268 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1269 | /// Generates code to check that an operand is a basic block. |
| 1270 | class MBBOperandMatcher : public OperandPredicateMatcher { |
| 1271 | public: |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1272 | MBBOperandMatcher(unsigned InsnVarID, unsigned OpIdx) |
| 1273 | : OperandPredicateMatcher(OPM_MBB, InsnVarID, OpIdx) {} |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1274 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1275 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1276 | return P->getKind() == OPM_MBB; |
| 1277 | } |
| 1278 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1279 | void emitPredicateOpcodes(MatchTable &Table, |
| 1280 | RuleMatcher &Rule) const override { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1281 | Table << MatchTable::Opcode("GIM_CheckIsMBB") << MatchTable::Comment("MI") |
| 1282 | << MatchTable::IntValue(InsnVarID) << MatchTable::Comment("Op") |
| 1283 | << MatchTable::IntValue(OpIdx) << MatchTable::LineBreak; |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1284 | } |
| 1285 | }; |
| 1286 | |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 1287 | /// Generates code to check that an operand is a G_CONSTANT with a particular |
| 1288 | /// int. |
| 1289 | class ConstantIntOperandMatcher : public OperandPredicateMatcher { |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1290 | protected: |
| 1291 | int64_t Value; |
| 1292 | |
| 1293 | public: |
Quentin Colombet | eba10cb | 2017-12-18 22:12:13 +0000 | [diff] [blame] | 1294 | ConstantIntOperandMatcher(unsigned InsnVarID, unsigned OpIdx, int64_t Value) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1295 | : OperandPredicateMatcher(OPM_Int, InsnVarID, OpIdx), Value(Value) {} |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1296 | |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 1297 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1298 | return OperandPredicateMatcher::isIdentical(B) && |
| 1299 | Value == cast<ConstantIntOperandMatcher>(&B)->Value; |
| 1300 | } |
| 1301 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1302 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1303 | return P->getKind() == OPM_Int; |
| 1304 | } |
| 1305 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1306 | void emitPredicateOpcodes(MatchTable &Table, |
| 1307 | RuleMatcher &Rule) const override { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1308 | Table << MatchTable::Opcode("GIM_CheckConstantInt") |
| 1309 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1310 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) |
| 1311 | << MatchTable::IntValue(Value) << MatchTable::LineBreak; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1312 | } |
| 1313 | }; |
| 1314 | |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 1315 | /// Generates code to check that an operand is a raw int (where MO.isImm() or |
| 1316 | /// MO.isCImm() is true). |
| 1317 | class LiteralIntOperandMatcher : public OperandPredicateMatcher { |
| 1318 | protected: |
| 1319 | int64_t Value; |
| 1320 | |
| 1321 | public: |
Quentin Colombet | eba10cb | 2017-12-18 22:12:13 +0000 | [diff] [blame] | 1322 | LiteralIntOperandMatcher(unsigned InsnVarID, unsigned OpIdx, int64_t Value) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1323 | : OperandPredicateMatcher(OPM_LiteralInt, InsnVarID, OpIdx), |
| 1324 | Value(Value) {} |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 1325 | |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 1326 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1327 | return OperandPredicateMatcher::isIdentical(B) && |
| 1328 | Value == cast<LiteralIntOperandMatcher>(&B)->Value; |
| 1329 | } |
| 1330 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1331 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 1332 | return P->getKind() == OPM_LiteralInt; |
| 1333 | } |
| 1334 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1335 | void emitPredicateOpcodes(MatchTable &Table, |
| 1336 | RuleMatcher &Rule) const override { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1337 | Table << MatchTable::Opcode("GIM_CheckLiteralInt") |
| 1338 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1339 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) |
| 1340 | << MatchTable::IntValue(Value) << MatchTable::LineBreak; |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 1341 | } |
| 1342 | }; |
| 1343 | |
Daniel Sanders | fe12c0f | 2017-07-11 08:57:29 +0000 | [diff] [blame] | 1344 | /// Generates code to check that an operand is an intrinsic ID. |
| 1345 | class IntrinsicIDOperandMatcher : public OperandPredicateMatcher { |
| 1346 | protected: |
| 1347 | const CodeGenIntrinsic *II; |
| 1348 | |
| 1349 | public: |
Quentin Colombet | eba10cb | 2017-12-18 22:12:13 +0000 | [diff] [blame] | 1350 | IntrinsicIDOperandMatcher(unsigned InsnVarID, unsigned OpIdx, |
| 1351 | const CodeGenIntrinsic *II) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1352 | : OperandPredicateMatcher(OPM_IntrinsicID, InsnVarID, OpIdx), II(II) {} |
Daniel Sanders | fe12c0f | 2017-07-11 08:57:29 +0000 | [diff] [blame] | 1353 | |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 1354 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1355 | return OperandPredicateMatcher::isIdentical(B) && |
| 1356 | II == cast<IntrinsicIDOperandMatcher>(&B)->II; |
| 1357 | } |
| 1358 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1359 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | fe12c0f | 2017-07-11 08:57:29 +0000 | [diff] [blame] | 1360 | return P->getKind() == OPM_IntrinsicID; |
| 1361 | } |
| 1362 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1363 | void emitPredicateOpcodes(MatchTable &Table, |
| 1364 | RuleMatcher &Rule) const override { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1365 | Table << MatchTable::Opcode("GIM_CheckIntrinsicID") |
| 1366 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1367 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) |
| 1368 | << MatchTable::NamedValue("Intrinsic::" + II->EnumName) |
| 1369 | << MatchTable::LineBreak; |
Daniel Sanders | fe12c0f | 2017-07-11 08:57:29 +0000 | [diff] [blame] | 1370 | } |
| 1371 | }; |
| 1372 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1373 | /// Generates code to check that a set of predicates match for a particular |
| 1374 | /// operand. |
| 1375 | class OperandMatcher : public PredicateListMatcher<OperandPredicateMatcher> { |
| 1376 | protected: |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 1377 | InstructionMatcher &Insn; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1378 | unsigned OpIdx; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1379 | std::string SymbolicName; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1380 | |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1381 | /// The index of the first temporary variable allocated to this operand. The |
| 1382 | /// number of allocated temporaries can be found with |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 1383 | /// countRendererFns(). |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1384 | unsigned AllocatedTemporariesBaseID; |
| 1385 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1386 | public: |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 1387 | OperandMatcher(InstructionMatcher &Insn, unsigned OpIdx, |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1388 | const std::string &SymbolicName, |
| 1389 | unsigned AllocatedTemporariesBaseID) |
| 1390 | : Insn(Insn), OpIdx(OpIdx), SymbolicName(SymbolicName), |
| 1391 | AllocatedTemporariesBaseID(AllocatedTemporariesBaseID) {} |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1392 | |
| 1393 | bool hasSymbolicName() const { return !SymbolicName.empty(); } |
| 1394 | const StringRef getSymbolicName() const { return SymbolicName; } |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 1395 | void setSymbolicName(StringRef Name) { |
| 1396 | assert(SymbolicName.empty() && "Operand already has a symbolic name"); |
| 1397 | SymbolicName = Name; |
| 1398 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1399 | |
| 1400 | /// Construct a new operand predicate and add it to the matcher. |
| 1401 | template <class Kind, class... Args> |
| 1402 | Optional<Kind *> addPredicate(Args &&... args) { |
| 1403 | if (isSameAsAnotherOperand()) |
| 1404 | return None; |
| 1405 | Predicates.emplace_back(llvm::make_unique<Kind>( |
| 1406 | getInsnVarID(), getOpIdx(), std::forward<Args>(args)...)); |
| 1407 | return static_cast<Kind *>(Predicates.back().get()); |
| 1408 | } |
| 1409 | |
| 1410 | unsigned getOpIdx() const { return OpIdx; } |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1411 | unsigned getInsnVarID() const; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1412 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 1413 | std::string getOperandExpr(unsigned InsnVarID) const { |
| 1414 | return "State.MIs[" + llvm::to_string(InsnVarID) + "]->getOperand(" + |
| 1415 | llvm::to_string(OpIdx) + ")"; |
Daniel Sanders | e604ef5 | 2017-02-20 15:30:43 +0000 | [diff] [blame] | 1416 | } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1417 | |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 1418 | InstructionMatcher &getInstructionMatcher() const { return Insn; } |
| 1419 | |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 1420 | Error addTypeCheckPredicate(const TypeSetByHwMode &VTy, |
Reid Kleckner | cfdd4a2 | 2017-10-16 20:31:16 +0000 | [diff] [blame] | 1421 | bool OperandIsAPointer); |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 1422 | |
Daniel Sanders | 9d662d2 | 2017-07-06 10:06:12 +0000 | [diff] [blame] | 1423 | /// Emit MatchTable opcodes that test whether the instruction named in |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 1424 | /// InsnVarID matches all the predicates and all the operands. |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1425 | void emitPredicateOpcodes(MatchTable &Table, RuleMatcher &Rule) { |
| 1426 | if (!Optimized) { |
| 1427 | std::string Comment; |
| 1428 | raw_string_ostream CommentOS(Comment); |
| 1429 | CommentOS << "MIs[" << getInsnVarID() << "] "; |
| 1430 | if (SymbolicName.empty()) |
| 1431 | CommentOS << "Operand " << OpIdx; |
| 1432 | else |
| 1433 | CommentOS << SymbolicName; |
| 1434 | Table << MatchTable::Comment(CommentOS.str()) << MatchTable::LineBreak; |
| 1435 | } |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1436 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1437 | emitPredicateListOpcodes(Table, Rule); |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1438 | } |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1439 | |
| 1440 | /// Compare the priority of this object and B. |
| 1441 | /// |
| 1442 | /// Returns true if this object is more important than B. |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1443 | bool isHigherPriorityThan(OperandMatcher &B) { |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1444 | // Operand matchers involving more predicates have higher priority. |
| 1445 | if (predicates_size() > B.predicates_size()) |
| 1446 | return true; |
| 1447 | if (predicates_size() < B.predicates_size()) |
| 1448 | return false; |
| 1449 | |
| 1450 | // This assumes that predicates are added in a consistent order. |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1451 | for (auto &&Predicate : zip(predicates(), B.predicates())) { |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1452 | if (std::get<0>(Predicate)->isHigherPriorityThan(*std::get<1>(Predicate))) |
| 1453 | return true; |
| 1454 | if (std::get<1>(Predicate)->isHigherPriorityThan(*std::get<0>(Predicate))) |
| 1455 | return false; |
| 1456 | } |
| 1457 | |
| 1458 | return false; |
| 1459 | }; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1460 | |
| 1461 | /// Report the maximum number of temporary operands needed by the operand |
| 1462 | /// matcher. |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1463 | unsigned countRendererFns() { |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1464 | return std::accumulate( |
| 1465 | predicates().begin(), predicates().end(), 0, |
| 1466 | [](unsigned A, |
| 1467 | const std::unique_ptr<OperandPredicateMatcher> &Predicate) { |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 1468 | return A + Predicate->countRendererFns(); |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1469 | }); |
| 1470 | } |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1471 | |
| 1472 | unsigned getAllocatedTemporariesBaseID() const { |
| 1473 | return AllocatedTemporariesBaseID; |
| 1474 | } |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 1475 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1476 | bool isSameAsAnotherOperand() { |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 1477 | for (const auto &Predicate : predicates()) |
| 1478 | if (isa<SameOperandMatcher>(Predicate)) |
| 1479 | return true; |
| 1480 | return false; |
| 1481 | } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1482 | }; |
| 1483 | |
Reid Kleckner | cfdd4a2 | 2017-10-16 20:31:16 +0000 | [diff] [blame] | 1484 | Error OperandMatcher::addTypeCheckPredicate(const TypeSetByHwMode &VTy, |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1485 | bool OperandIsAPointer) { |
Reid Kleckner | cfdd4a2 | 2017-10-16 20:31:16 +0000 | [diff] [blame] | 1486 | if (!VTy.isMachineValueType()) |
| 1487 | return failedImport("unsupported typeset"); |
| 1488 | |
| 1489 | if (VTy.getMachineValueType() == MVT::iPTR && OperandIsAPointer) { |
| 1490 | addPredicate<PointerToAnyOperandMatcher>(0); |
| 1491 | return Error::success(); |
| 1492 | } |
| 1493 | |
| 1494 | auto OpTyOrNone = MVTToLLT(VTy.getMachineValueType().SimpleTy); |
| 1495 | if (!OpTyOrNone) |
| 1496 | return failedImport("unsupported type"); |
| 1497 | |
| 1498 | if (OperandIsAPointer) |
| 1499 | addPredicate<PointerToAnyOperandMatcher>(OpTyOrNone->get().getSizeInBits()); |
| 1500 | else |
| 1501 | addPredicate<LLTOperandMatcher>(*OpTyOrNone); |
| 1502 | return Error::success(); |
| 1503 | } |
| 1504 | |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1505 | unsigned ComplexPatternOperandMatcher::getAllocatedTemporariesBaseID() const { |
| 1506 | return Operand.getAllocatedTemporariesBaseID(); |
| 1507 | } |
| 1508 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1509 | /// Generates code to check a predicate on an instruction. |
| 1510 | /// |
| 1511 | /// Typical predicates include: |
| 1512 | /// * The opcode of the instruction is a particular value. |
| 1513 | /// * The nsw/nuw flag is/isn't set. |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1514 | class InstructionPredicateMatcher : public PredicateMatcher { |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1515 | public: |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1516 | InstructionPredicateMatcher(PredicateKind Kind, unsigned InsnVarID) |
| 1517 | : PredicateMatcher(Kind, InsnVarID) {} |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1518 | virtual ~InstructionPredicateMatcher() {} |
| 1519 | |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1520 | /// Compare the priority of this object and B. |
| 1521 | /// |
| 1522 | /// Returns true if this object is more important than B. |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 1523 | virtual bool |
| 1524 | isHigherPriorityThan(const InstructionPredicateMatcher &B) const { |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1525 | return Kind < B.Kind; |
| 1526 | }; |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1527 | }; |
| 1528 | |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 1529 | template <> |
| 1530 | std::string |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1531 | PredicateListMatcher<PredicateMatcher>::getNoPredicateComment() const { |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 1532 | return "No instruction predicates"; |
| 1533 | } |
| 1534 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1535 | /// Generates code to check the opcode of an instruction. |
| 1536 | class InstructionOpcodeMatcher : public InstructionPredicateMatcher { |
| 1537 | protected: |
| 1538 | const CodeGenInstruction *I; |
| 1539 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1540 | static DenseMap<const CodeGenInstruction *, unsigned> OpcodeValues; |
| 1541 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1542 | public: |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1543 | static void initOpcodeValuesMap(const CodeGenTarget &Target) { |
| 1544 | OpcodeValues.clear(); |
| 1545 | |
| 1546 | unsigned OpcodeValue = 0; |
| 1547 | for (const CodeGenInstruction *I : Target.getInstructionsByEnumValue()) |
| 1548 | OpcodeValues[I] = OpcodeValue++; |
| 1549 | } |
| 1550 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1551 | InstructionOpcodeMatcher(unsigned InsnVarID, const CodeGenInstruction *I) |
| 1552 | : InstructionPredicateMatcher(IPM_Opcode, InsnVarID), I(I) {} |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1553 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1554 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1555 | return P->getKind() == IPM_Opcode; |
| 1556 | } |
| 1557 | |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 1558 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1559 | return InstructionPredicateMatcher::isIdentical(B) && |
| 1560 | I == cast<InstructionOpcodeMatcher>(&B)->I; |
| 1561 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1562 | MatchTableRecord getValue() const override { |
| 1563 | const auto VI = OpcodeValues.find(I); |
| 1564 | if (VI != OpcodeValues.end()) |
| 1565 | return MatchTable::NamedValue(I->Namespace, I->TheDef->getName(), |
| 1566 | VI->second); |
| 1567 | return MatchTable::NamedValue(I->Namespace, I->TheDef->getName()); |
| 1568 | } |
| 1569 | bool hasValue() const override { return OpcodeValues.count(I); } |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 1570 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1571 | void emitPredicateOpcodes(MatchTable &Table, |
| 1572 | RuleMatcher &Rule) const override { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1573 | Table << MatchTable::Opcode("GIM_CheckOpcode") << MatchTable::Comment("MI") |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1574 | << MatchTable::IntValue(InsnVarID) << getValue() |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1575 | << MatchTable::LineBreak; |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1576 | } |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1577 | |
| 1578 | /// Compare the priority of this object and B. |
| 1579 | /// |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1580 | /// Returns true if this object is more important than B. |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 1581 | bool |
| 1582 | isHigherPriorityThan(const InstructionPredicateMatcher &B) const override { |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1583 | if (InstructionPredicateMatcher::isHigherPriorityThan(B)) |
| 1584 | return true; |
| 1585 | if (B.InstructionPredicateMatcher::isHigherPriorityThan(*this)) |
| 1586 | return false; |
| 1587 | |
| 1588 | // Prioritize opcodes for cosmetic reasons in the generated source. Although |
| 1589 | // this is cosmetic at the moment, we may want to drive a similar ordering |
| 1590 | // using instruction frequency information to improve compile time. |
| 1591 | if (const InstructionOpcodeMatcher *BO = |
| 1592 | dyn_cast<InstructionOpcodeMatcher>(&B)) |
| 1593 | return I->TheDef->getName() < BO->I->TheDef->getName(); |
| 1594 | |
| 1595 | return false; |
| 1596 | }; |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 1597 | |
| 1598 | bool isConstantInstruction() const { |
| 1599 | return I->TheDef->getName() == "G_CONSTANT"; |
| 1600 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1601 | |
Roman Tereshin | 19da667 | 2018-05-22 04:31:50 +0000 | [diff] [blame] | 1602 | StringRef getOpcode() const { return I->TheDef->getName(); } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1603 | unsigned getNumOperands() const { return I->Operands.size(); } |
| 1604 | |
| 1605 | StringRef getOperandType(unsigned OpIdx) const { |
| 1606 | return I->Operands[OpIdx].OperandType; |
| 1607 | } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1608 | }; |
| 1609 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1610 | DenseMap<const CodeGenInstruction *, unsigned> |
| 1611 | InstructionOpcodeMatcher::OpcodeValues; |
| 1612 | |
Roman Tereshin | 19da667 | 2018-05-22 04:31:50 +0000 | [diff] [blame] | 1613 | class InstructionNumOperandsMatcher final : public InstructionPredicateMatcher { |
| 1614 | unsigned NumOperands = 0; |
| 1615 | |
| 1616 | public: |
| 1617 | InstructionNumOperandsMatcher(unsigned InsnVarID, unsigned NumOperands) |
| 1618 | : InstructionPredicateMatcher(IPM_NumOperands, InsnVarID), |
| 1619 | NumOperands(NumOperands) {} |
| 1620 | |
| 1621 | static bool classof(const PredicateMatcher *P) { |
| 1622 | return P->getKind() == IPM_NumOperands; |
| 1623 | } |
| 1624 | |
| 1625 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1626 | return InstructionPredicateMatcher::isIdentical(B) && |
| 1627 | NumOperands == cast<InstructionNumOperandsMatcher>(&B)->NumOperands; |
| 1628 | } |
| 1629 | |
| 1630 | void emitPredicateOpcodes(MatchTable &Table, |
| 1631 | RuleMatcher &Rule) const override { |
| 1632 | Table << MatchTable::Opcode("GIM_CheckNumOperands") |
| 1633 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1634 | << MatchTable::Comment("Expected") |
| 1635 | << MatchTable::IntValue(NumOperands) << MatchTable::LineBreak; |
| 1636 | } |
| 1637 | }; |
| 1638 | |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 1639 | /// Generates code to check that this instruction is a constant whose value |
| 1640 | /// meets an immediate predicate. |
| 1641 | /// |
| 1642 | /// Immediates are slightly odd since they are typically used like an operand |
| 1643 | /// but are represented as an operator internally. We typically write simm8:$src |
| 1644 | /// in a tablegen pattern, but this is just syntactic sugar for |
| 1645 | /// (imm:i32)<<P:Predicate_simm8>>:$imm which more directly describes the nodes |
| 1646 | /// that will be matched and the predicate (which is attached to the imm |
| 1647 | /// operator) that will be tested. In SelectionDAG this describes a |
| 1648 | /// ConstantSDNode whose internal value will be tested using the simm8 predicate. |
| 1649 | /// |
| 1650 | /// The corresponding GlobalISel representation is %1 = G_CONSTANT iN Value. In |
| 1651 | /// this representation, the immediate could be tested with an |
| 1652 | /// InstructionMatcher, InstructionOpcodeMatcher, OperandMatcher, and a |
| 1653 | /// OperandPredicateMatcher-subclass to check the Value meets the predicate but |
| 1654 | /// there are two implementation issues with producing that matcher |
| 1655 | /// configuration from the SelectionDAG pattern: |
| 1656 | /// * ImmLeaf is a PatFrag whose root is an InstructionMatcher. This means that |
| 1657 | /// were we to sink the immediate predicate to the operand we would have to |
| 1658 | /// have two partial implementations of PatFrag support, one for immediates |
| 1659 | /// and one for non-immediates. |
| 1660 | /// * At the point we handle the predicate, the OperandMatcher hasn't been |
| 1661 | /// created yet. If we were to sink the predicate to the OperandMatcher we |
| 1662 | /// would also have to complicate (or duplicate) the code that descends and |
| 1663 | /// creates matchers for the subtree. |
| 1664 | /// Overall, it's simpler to handle it in the place it was found. |
| 1665 | class InstructionImmPredicateMatcher : public InstructionPredicateMatcher { |
| 1666 | protected: |
| 1667 | TreePredicateFn Predicate; |
| 1668 | |
| 1669 | public: |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1670 | InstructionImmPredicateMatcher(unsigned InsnVarID, |
| 1671 | const TreePredicateFn &Predicate) |
| 1672 | : InstructionPredicateMatcher(IPM_ImmPredicate, InsnVarID), |
| 1673 | Predicate(Predicate) {} |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 1674 | |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 1675 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1676 | return InstructionPredicateMatcher::isIdentical(B) && |
| 1677 | Predicate.getOrigPatFragRecord() == |
| 1678 | cast<InstructionImmPredicateMatcher>(&B) |
| 1679 | ->Predicate.getOrigPatFragRecord(); |
| 1680 | } |
| 1681 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1682 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 1683 | return P->getKind() == IPM_ImmPredicate; |
| 1684 | } |
| 1685 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1686 | void emitPredicateOpcodes(MatchTable &Table, |
| 1687 | RuleMatcher &Rule) const override { |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 1688 | Table << MatchTable::Opcode(getMatchOpcodeForPredicate(Predicate)) |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 1689 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1690 | << MatchTable::Comment("Predicate") |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 1691 | << MatchTable::NamedValue(getEnumNameForPredicate(Predicate)) |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 1692 | << MatchTable::LineBreak; |
| 1693 | } |
| 1694 | }; |
| 1695 | |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 1696 | /// Generates code to check that a memory instruction has a atomic ordering |
| 1697 | /// MachineMemoryOperand. |
| 1698 | class AtomicOrderingMMOPredicateMatcher : public InstructionPredicateMatcher { |
Daniel Sanders | 0c43b3a | 2017-11-30 21:05:59 +0000 | [diff] [blame] | 1699 | public: |
| 1700 | enum AOComparator { |
| 1701 | AO_Exactly, |
| 1702 | AO_OrStronger, |
| 1703 | AO_WeakerThan, |
| 1704 | }; |
| 1705 | |
| 1706 | protected: |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 1707 | StringRef Order; |
Daniel Sanders | 0c43b3a | 2017-11-30 21:05:59 +0000 | [diff] [blame] | 1708 | AOComparator Comparator; |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 1709 | |
Daniel Sanders | 39690bd | 2017-10-15 02:41:12 +0000 | [diff] [blame] | 1710 | public: |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1711 | AtomicOrderingMMOPredicateMatcher(unsigned InsnVarID, StringRef Order, |
Daniel Sanders | 0c43b3a | 2017-11-30 21:05:59 +0000 | [diff] [blame] | 1712 | AOComparator Comparator = AO_Exactly) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1713 | : InstructionPredicateMatcher(IPM_AtomicOrderingMMO, InsnVarID), |
| 1714 | Order(Order), Comparator(Comparator) {} |
Daniel Sanders | 39690bd | 2017-10-15 02:41:12 +0000 | [diff] [blame] | 1715 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1716 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 1717 | return P->getKind() == IPM_AtomicOrderingMMO; |
Daniel Sanders | 39690bd | 2017-10-15 02:41:12 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1720 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1721 | if (!InstructionPredicateMatcher::isIdentical(B)) |
| 1722 | return false; |
| 1723 | const auto &R = *cast<AtomicOrderingMMOPredicateMatcher>(&B); |
| 1724 | return Order == R.Order && Comparator == R.Comparator; |
| 1725 | } |
| 1726 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1727 | void emitPredicateOpcodes(MatchTable &Table, |
| 1728 | RuleMatcher &Rule) const override { |
Daniel Sanders | 0c43b3a | 2017-11-30 21:05:59 +0000 | [diff] [blame] | 1729 | StringRef Opcode = "GIM_CheckAtomicOrdering"; |
| 1730 | |
| 1731 | if (Comparator == AO_OrStronger) |
| 1732 | Opcode = "GIM_CheckAtomicOrderingOrStrongerThan"; |
| 1733 | if (Comparator == AO_WeakerThan) |
| 1734 | Opcode = "GIM_CheckAtomicOrderingWeakerThan"; |
| 1735 | |
| 1736 | Table << MatchTable::Opcode(Opcode) << MatchTable::Comment("MI") |
| 1737 | << MatchTable::IntValue(InsnVarID) << MatchTable::Comment("Order") |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 1738 | << MatchTable::NamedValue(("(int64_t)AtomicOrdering::" + Order).str()) |
Daniel Sanders | 39690bd | 2017-10-15 02:41:12 +0000 | [diff] [blame] | 1739 | << MatchTable::LineBreak; |
| 1740 | } |
| 1741 | }; |
| 1742 | |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 1743 | /// Generates code to check that the size of an MMO is exactly N bytes. |
| 1744 | class MemorySizePredicateMatcher : public InstructionPredicateMatcher { |
| 1745 | protected: |
| 1746 | unsigned MMOIdx; |
| 1747 | uint64_t Size; |
| 1748 | |
| 1749 | public: |
| 1750 | MemorySizePredicateMatcher(unsigned InsnVarID, unsigned MMOIdx, unsigned Size) |
| 1751 | : InstructionPredicateMatcher(IPM_MemoryLLTSize, InsnVarID), |
| 1752 | MMOIdx(MMOIdx), Size(Size) {} |
| 1753 | |
| 1754 | static bool classof(const PredicateMatcher *P) { |
| 1755 | return P->getKind() == IPM_MemoryLLTSize; |
| 1756 | } |
| 1757 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1758 | return InstructionPredicateMatcher::isIdentical(B) && |
| 1759 | MMOIdx == cast<MemorySizePredicateMatcher>(&B)->MMOIdx && |
| 1760 | Size == cast<MemorySizePredicateMatcher>(&B)->Size; |
| 1761 | } |
| 1762 | |
| 1763 | void emitPredicateOpcodes(MatchTable &Table, |
| 1764 | RuleMatcher &Rule) const override { |
| 1765 | Table << MatchTable::Opcode("GIM_CheckMemorySizeEqualTo") |
| 1766 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1767 | << MatchTable::Comment("MMO") << MatchTable::IntValue(MMOIdx) |
| 1768 | << MatchTable::Comment("Size") << MatchTable::IntValue(Size) |
| 1769 | << MatchTable::LineBreak; |
| 1770 | } |
| 1771 | }; |
| 1772 | |
| 1773 | /// Generates code to check that the size of an MMO is less-than, equal-to, or |
| 1774 | /// greater than a given LLT. |
| 1775 | class MemoryVsLLTSizePredicateMatcher : public InstructionPredicateMatcher { |
| 1776 | public: |
| 1777 | enum RelationKind { |
| 1778 | GreaterThan, |
| 1779 | EqualTo, |
| 1780 | LessThan, |
| 1781 | }; |
| 1782 | |
| 1783 | protected: |
| 1784 | unsigned MMOIdx; |
| 1785 | RelationKind Relation; |
| 1786 | unsigned OpIdx; |
| 1787 | |
| 1788 | public: |
| 1789 | MemoryVsLLTSizePredicateMatcher(unsigned InsnVarID, unsigned MMOIdx, |
| 1790 | enum RelationKind Relation, |
| 1791 | unsigned OpIdx) |
| 1792 | : InstructionPredicateMatcher(IPM_MemoryVsLLTSize, InsnVarID), |
| 1793 | MMOIdx(MMOIdx), Relation(Relation), OpIdx(OpIdx) {} |
| 1794 | |
| 1795 | static bool classof(const PredicateMatcher *P) { |
| 1796 | return P->getKind() == IPM_MemoryVsLLTSize; |
| 1797 | } |
| 1798 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1799 | return InstructionPredicateMatcher::isIdentical(B) && |
| 1800 | MMOIdx == cast<MemoryVsLLTSizePredicateMatcher>(&B)->MMOIdx && |
| 1801 | Relation == cast<MemoryVsLLTSizePredicateMatcher>(&B)->Relation && |
| 1802 | OpIdx == cast<MemoryVsLLTSizePredicateMatcher>(&B)->OpIdx; |
| 1803 | } |
| 1804 | |
| 1805 | void emitPredicateOpcodes(MatchTable &Table, |
| 1806 | RuleMatcher &Rule) const override { |
| 1807 | Table << MatchTable::Opcode(Relation == EqualTo |
| 1808 | ? "GIM_CheckMemorySizeEqualToLLT" |
| 1809 | : Relation == GreaterThan |
| 1810 | ? "GIM_CheckMemorySizeGreaterThanLLT" |
| 1811 | : "GIM_CheckMemorySizeLessThanLLT") |
| 1812 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1813 | << MatchTable::Comment("MMO") << MatchTable::IntValue(MMOIdx) |
| 1814 | << MatchTable::Comment("OpIdx") << MatchTable::IntValue(OpIdx) |
| 1815 | << MatchTable::LineBreak; |
| 1816 | } |
| 1817 | }; |
| 1818 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1819 | /// Generates code to check that a set of predicates and operands match for a |
| 1820 | /// particular instruction. |
| 1821 | /// |
| 1822 | /// Typical predicates include: |
| 1823 | /// * Has a specific opcode. |
| 1824 | /// * Has an nsw/nuw flag or doesn't. |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1825 | class InstructionMatcher final : public PredicateListMatcher<PredicateMatcher> { |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1826 | protected: |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1827 | typedef std::vector<std::unique_ptr<OperandMatcher>> OperandVec; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1828 | |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 1829 | RuleMatcher &Rule; |
| 1830 | |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1831 | /// The operands to match. All rendered operands must be present even if the |
| 1832 | /// condition is always true. |
| 1833 | OperandVec Operands; |
Roman Tereshin | 19da667 | 2018-05-22 04:31:50 +0000 | [diff] [blame] | 1834 | bool NumOperandsCheck = true; |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1835 | |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 1836 | std::string SymbolicName; |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1837 | unsigned InsnVarID; |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 1838 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1839 | public: |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 1840 | InstructionMatcher(RuleMatcher &Rule, StringRef SymbolicName) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1841 | : Rule(Rule), SymbolicName(SymbolicName) { |
| 1842 | // We create a new instruction matcher. |
| 1843 | // Get a new ID for that instruction. |
| 1844 | InsnVarID = Rule.implicitlyDefineInsnVar(*this); |
| 1845 | } |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 1846 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1847 | /// Construct a new instruction predicate and add it to the matcher. |
| 1848 | template <class Kind, class... Args> |
| 1849 | Optional<Kind *> addPredicate(Args &&... args) { |
| 1850 | Predicates.emplace_back( |
| 1851 | llvm::make_unique<Kind>(getInsnVarID(), std::forward<Args>(args)...)); |
| 1852 | return static_cast<Kind *>(Predicates.back().get()); |
| 1853 | } |
| 1854 | |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 1855 | RuleMatcher &getRuleMatcher() const { return Rule; } |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 1856 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1857 | unsigned getInsnVarID() const { return InsnVarID; } |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1858 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1859 | /// Add an operand to the matcher. |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1860 | OperandMatcher &addOperand(unsigned OpIdx, const std::string &SymbolicName, |
| 1861 | unsigned AllocatedTemporariesBaseID) { |
| 1862 | Operands.emplace_back(new OperandMatcher(*this, OpIdx, SymbolicName, |
| 1863 | AllocatedTemporariesBaseID)); |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 1864 | if (!SymbolicName.empty()) |
| 1865 | Rule.defineOperand(SymbolicName, *Operands.back()); |
| 1866 | |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1867 | return *Operands.back(); |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1868 | } |
| 1869 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 1870 | OperandMatcher &getOperand(unsigned OpIdx) { |
| 1871 | auto I = std::find_if(Operands.begin(), Operands.end(), |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1872 | [&OpIdx](const std::unique_ptr<OperandMatcher> &X) { |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1873 | return X->getOpIdx() == OpIdx; |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 1874 | }); |
| 1875 | if (I != Operands.end()) |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1876 | return **I; |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 1877 | llvm_unreachable("Failed to lookup operand"); |
| 1878 | } |
| 1879 | |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 1880 | StringRef getSymbolicName() const { return SymbolicName; } |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1881 | unsigned getNumOperands() const { return Operands.size(); } |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 1882 | OperandVec::iterator operands_begin() { return Operands.begin(); } |
| 1883 | OperandVec::iterator operands_end() { return Operands.end(); } |
| 1884 | iterator_range<OperandVec::iterator> operands() { |
| 1885 | return make_range(operands_begin(), operands_end()); |
| 1886 | } |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 1887 | OperandVec::const_iterator operands_begin() const { return Operands.begin(); } |
| 1888 | OperandVec::const_iterator operands_end() const { return Operands.end(); } |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1889 | iterator_range<OperandVec::const_iterator> operands() const { |
| 1890 | return make_range(operands_begin(), operands_end()); |
| 1891 | } |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 1892 | bool operands_empty() const { return Operands.empty(); } |
| 1893 | |
| 1894 | void pop_front() { Operands.erase(Operands.begin()); } |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1895 | |
Roman Tereshin | 19da667 | 2018-05-22 04:31:50 +0000 | [diff] [blame] | 1896 | void optimize(); |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1897 | |
| 1898 | /// Emit MatchTable opcodes that test whether the instruction named in |
| 1899 | /// InsnVarName matches all the predicates and all the operands. |
| 1900 | void emitPredicateOpcodes(MatchTable &Table, RuleMatcher &Rule) { |
Roman Tereshin | 19da667 | 2018-05-22 04:31:50 +0000 | [diff] [blame] | 1901 | if (NumOperandsCheck) |
| 1902 | InstructionNumOperandsMatcher(InsnVarID, getNumOperands()) |
| 1903 | .emitPredicateOpcodes(Table, Rule); |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 1904 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1905 | emitPredicateListOpcodes(Table, Rule); |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1906 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 1907 | for (const auto &Operand : Operands) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1908 | Operand->emitPredicateOpcodes(Table, Rule); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1909 | } |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1910 | |
| 1911 | /// Compare the priority of this object and B. |
| 1912 | /// |
| 1913 | /// Returns true if this object is more important than B. |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1914 | bool isHigherPriorityThan(InstructionMatcher &B) { |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1915 | // Instruction matchers involving more operands have higher priority. |
| 1916 | if (Operands.size() > B.Operands.size()) |
| 1917 | return true; |
| 1918 | if (Operands.size() < B.Operands.size()) |
| 1919 | return false; |
| 1920 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1921 | for (auto &&P : zip(predicates(), B.predicates())) { |
| 1922 | auto L = static_cast<InstructionPredicateMatcher *>(std::get<0>(P).get()); |
| 1923 | auto R = static_cast<InstructionPredicateMatcher *>(std::get<1>(P).get()); |
| 1924 | if (L->isHigherPriorityThan(*R)) |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1925 | return true; |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1926 | if (R->isHigherPriorityThan(*L)) |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1927 | return false; |
| 1928 | } |
| 1929 | |
| 1930 | for (const auto &Operand : zip(Operands, B.Operands)) { |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1931 | if (std::get<0>(Operand)->isHigherPriorityThan(*std::get<1>(Operand))) |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1932 | return true; |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1933 | if (std::get<1>(Operand)->isHigherPriorityThan(*std::get<0>(Operand))) |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1934 | return false; |
| 1935 | } |
| 1936 | |
| 1937 | return false; |
| 1938 | }; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1939 | |
| 1940 | /// Report the maximum number of temporary operands needed by the instruction |
| 1941 | /// matcher. |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1942 | unsigned countRendererFns() { |
| 1943 | return std::accumulate( |
| 1944 | predicates().begin(), predicates().end(), 0, |
| 1945 | [](unsigned A, |
| 1946 | const std::unique_ptr<PredicateMatcher> &Predicate) { |
| 1947 | return A + Predicate->countRendererFns(); |
| 1948 | }) + |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1949 | std::accumulate( |
| 1950 | Operands.begin(), Operands.end(), 0, |
| 1951 | [](unsigned A, const std::unique_ptr<OperandMatcher> &Operand) { |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 1952 | return A + Operand->countRendererFns(); |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1953 | }); |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1954 | } |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 1955 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1956 | InstructionOpcodeMatcher &getOpcodeMatcher() { |
| 1957 | for (auto &P : predicates()) |
| 1958 | if (auto *OpMatcher = dyn_cast<InstructionOpcodeMatcher>(P.get())) |
| 1959 | return *OpMatcher; |
| 1960 | llvm_unreachable("Didn't find an opcode matcher"); |
| 1961 | } |
| 1962 | |
| 1963 | bool isConstantInstruction() { |
| 1964 | return getOpcodeMatcher().isConstantInstruction(); |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 1965 | } |
Roman Tereshin | 19da667 | 2018-05-22 04:31:50 +0000 | [diff] [blame] | 1966 | |
| 1967 | StringRef getOpcode() { return getOpcodeMatcher().getOpcode(); } |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1968 | }; |
| 1969 | |
Roman Tereshin | 19da667 | 2018-05-22 04:31:50 +0000 | [diff] [blame] | 1970 | StringRef RuleMatcher::getOpcode() const { |
| 1971 | return Matchers.front()->getOpcode(); |
| 1972 | } |
| 1973 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1974 | unsigned RuleMatcher::getNumOperands() const { |
| 1975 | return Matchers.front()->getNumOperands(); |
| 1976 | } |
| 1977 | |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 1978 | /// Generates code to check that the operand is a register defined by an |
| 1979 | /// instruction that matches the given instruction matcher. |
| 1980 | /// |
| 1981 | /// For example, the pattern: |
| 1982 | /// (set $dst, (G_MUL (G_ADD $src1, $src2), $src3)) |
| 1983 | /// would use an InstructionOperandMatcher for operand 1 of the G_MUL to match |
| 1984 | /// the: |
| 1985 | /// (G_ADD $src1, $src2) |
| 1986 | /// subpattern. |
| 1987 | class InstructionOperandMatcher : public OperandPredicateMatcher { |
| 1988 | protected: |
| 1989 | std::unique_ptr<InstructionMatcher> InsnMatcher; |
| 1990 | |
| 1991 | public: |
Quentin Colombet | eba10cb | 2017-12-18 22:12:13 +0000 | [diff] [blame] | 1992 | InstructionOperandMatcher(unsigned InsnVarID, unsigned OpIdx, |
| 1993 | RuleMatcher &Rule, StringRef SymbolicName) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1994 | : OperandPredicateMatcher(OPM_Instruction, InsnVarID, OpIdx), |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 1995 | InsnMatcher(new InstructionMatcher(Rule, SymbolicName)) {} |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 1996 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1997 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 1998 | return P->getKind() == OPM_Instruction; |
| 1999 | } |
| 2000 | |
| 2001 | InstructionMatcher &getInsnMatcher() const { return *InsnMatcher; } |
| 2002 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2003 | void emitCaptureOpcodes(MatchTable &Table, RuleMatcher &Rule) const { |
| 2004 | const unsigned NewInsnVarID = InsnMatcher->getInsnVarID(); |
| 2005 | Table << MatchTable::Opcode("GIM_RecordInsn") |
| 2006 | << MatchTable::Comment("DefineMI") |
| 2007 | << MatchTable::IntValue(NewInsnVarID) << MatchTable::Comment("MI") |
| 2008 | << MatchTable::IntValue(getInsnVarID()) |
| 2009 | << MatchTable::Comment("OpIdx") << MatchTable::IntValue(getOpIdx()) |
| 2010 | << MatchTable::Comment("MIs[" + llvm::to_string(NewInsnVarID) + "]") |
| 2011 | << MatchTable::LineBreak; |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 2012 | } |
| 2013 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 2014 | void emitPredicateOpcodes(MatchTable &Table, |
| 2015 | RuleMatcher &Rule) const override { |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2016 | emitCaptureOpcodes(Table, Rule); |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 2017 | InsnMatcher->emitPredicateOpcodes(Table, Rule); |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 2018 | } |
Daniel Sanders | 12e6e70 | 2018-01-17 20:34:29 +0000 | [diff] [blame] | 2019 | |
| 2020 | bool isHigherPriorityThan(const OperandPredicateMatcher &B) const override { |
| 2021 | if (OperandPredicateMatcher::isHigherPriorityThan(B)) |
| 2022 | return true; |
| 2023 | if (B.OperandPredicateMatcher::isHigherPriorityThan(*this)) |
| 2024 | return false; |
| 2025 | |
| 2026 | if (const InstructionOperandMatcher *BP = |
| 2027 | dyn_cast<InstructionOperandMatcher>(&B)) |
| 2028 | if (InsnMatcher->isHigherPriorityThan(*BP->InsnMatcher)) |
| 2029 | return true; |
| 2030 | return false; |
| 2031 | } |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 2032 | }; |
| 2033 | |
Roman Tereshin | 19da667 | 2018-05-22 04:31:50 +0000 | [diff] [blame] | 2034 | void InstructionMatcher::optimize() { |
| 2035 | SmallVector<std::unique_ptr<PredicateMatcher>, 8> Stash; |
| 2036 | const auto &OpcMatcher = getOpcodeMatcher(); |
| 2037 | |
| 2038 | Stash.push_back(predicates_pop_front()); |
| 2039 | if (Stash.back().get() == &OpcMatcher) { |
| 2040 | if (NumOperandsCheck && OpcMatcher.getNumOperands() < getNumOperands()) |
| 2041 | Stash.emplace_back( |
| 2042 | new InstructionNumOperandsMatcher(InsnVarID, getNumOperands())); |
| 2043 | NumOperandsCheck = false; |
Roman Tereshin | fedae33 | 2018-05-23 02:04:19 +0000 | [diff] [blame] | 2044 | |
| 2045 | for (auto &OM : Operands) |
| 2046 | for (auto &OP : OM->predicates()) |
| 2047 | if (isa<IntrinsicIDOperandMatcher>(OP)) { |
| 2048 | Stash.push_back(std::move(OP)); |
| 2049 | OM->eraseNullPredicates(); |
| 2050 | break; |
| 2051 | } |
Roman Tereshin | 19da667 | 2018-05-22 04:31:50 +0000 | [diff] [blame] | 2052 | } |
| 2053 | |
| 2054 | if (InsnVarID > 0) { |
| 2055 | assert(!Operands.empty() && "Nested instruction is expected to def a vreg"); |
| 2056 | for (auto &OP : Operands[0]->predicates()) |
| 2057 | OP.reset(); |
| 2058 | Operands[0]->eraseNullPredicates(); |
| 2059 | } |
| 2060 | while (!Stash.empty()) |
| 2061 | prependPredicate(Stash.pop_back_val()); |
| 2062 | } |
| 2063 | |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 2064 | //===- Actions ------------------------------------------------------------===// |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2065 | class OperandRenderer { |
| 2066 | public: |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2067 | enum RendererKind { |
| 2068 | OR_Copy, |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 2069 | OR_CopyOrAddZeroReg, |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2070 | OR_CopySubReg, |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2071 | OR_CopyConstantAsImm, |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 2072 | OR_CopyFConstantAsFPImm, |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2073 | OR_Imm, |
| 2074 | OR_Register, |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 2075 | OR_TempRegister, |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 2076 | OR_ComplexPattern, |
| 2077 | OR_Custom |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2078 | }; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2079 | |
| 2080 | protected: |
| 2081 | RendererKind Kind; |
| 2082 | |
| 2083 | public: |
| 2084 | OperandRenderer(RendererKind Kind) : Kind(Kind) {} |
| 2085 | virtual ~OperandRenderer() {} |
| 2086 | |
| 2087 | RendererKind getKind() const { return Kind; } |
| 2088 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2089 | virtual void emitRenderOpcodes(MatchTable &Table, |
| 2090 | RuleMatcher &Rule) const = 0; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2091 | }; |
| 2092 | |
| 2093 | /// A CopyRenderer emits code to copy a single operand from an existing |
| 2094 | /// instruction to the one being built. |
| 2095 | class CopyRenderer : public OperandRenderer { |
| 2096 | protected: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2097 | unsigned NewInsnID; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2098 | /// The name of the operand. |
| 2099 | const StringRef SymbolicName; |
| 2100 | |
| 2101 | public: |
Daniel Sanders | bd83ad4 | 2017-10-24 01:48:34 +0000 | [diff] [blame] | 2102 | CopyRenderer(unsigned NewInsnID, StringRef SymbolicName) |
| 2103 | : OperandRenderer(OR_Copy), NewInsnID(NewInsnID), |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2104 | SymbolicName(SymbolicName) { |
| 2105 | assert(!SymbolicName.empty() && "Cannot copy from an unspecified source"); |
| 2106 | } |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2107 | |
| 2108 | static bool classof(const OperandRenderer *R) { |
| 2109 | return R->getKind() == OR_Copy; |
| 2110 | } |
| 2111 | |
| 2112 | const StringRef getSymbolicName() const { return SymbolicName; } |
| 2113 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2114 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 2115 | const OperandMatcher &Operand = Rule.getOperandMatcher(SymbolicName); |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2116 | unsigned OldInsnVarID = Rule.getInsnVarID(Operand.getInstructionMatcher()); |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2117 | Table << MatchTable::Opcode("GIR_Copy") << MatchTable::Comment("NewInsnID") |
| 2118 | << MatchTable::IntValue(NewInsnID) << MatchTable::Comment("OldInsnID") |
| 2119 | << MatchTable::IntValue(OldInsnVarID) << MatchTable::Comment("OpIdx") |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2120 | << MatchTable::IntValue(Operand.getOpIdx()) |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2121 | << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2122 | } |
| 2123 | }; |
| 2124 | |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 2125 | /// A CopyOrAddZeroRegRenderer emits code to copy a single operand from an |
| 2126 | /// existing instruction to the one being built. If the operand turns out to be |
| 2127 | /// a 'G_CONSTANT 0' then it replaces the operand with a zero register. |
| 2128 | class CopyOrAddZeroRegRenderer : public OperandRenderer { |
| 2129 | protected: |
| 2130 | unsigned NewInsnID; |
| 2131 | /// The name of the operand. |
| 2132 | const StringRef SymbolicName; |
| 2133 | const Record *ZeroRegisterDef; |
| 2134 | |
| 2135 | public: |
| 2136 | CopyOrAddZeroRegRenderer(unsigned NewInsnID, |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 2137 | StringRef SymbolicName, Record *ZeroRegisterDef) |
| 2138 | : OperandRenderer(OR_CopyOrAddZeroReg), NewInsnID(NewInsnID), |
| 2139 | SymbolicName(SymbolicName), ZeroRegisterDef(ZeroRegisterDef) { |
| 2140 | assert(!SymbolicName.empty() && "Cannot copy from an unspecified source"); |
| 2141 | } |
| 2142 | |
| 2143 | static bool classof(const OperandRenderer *R) { |
| 2144 | return R->getKind() == OR_CopyOrAddZeroReg; |
| 2145 | } |
| 2146 | |
| 2147 | const StringRef getSymbolicName() const { return SymbolicName; } |
| 2148 | |
| 2149 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
| 2150 | const OperandMatcher &Operand = Rule.getOperandMatcher(SymbolicName); |
| 2151 | unsigned OldInsnVarID = Rule.getInsnVarID(Operand.getInstructionMatcher()); |
| 2152 | Table << MatchTable::Opcode("GIR_CopyOrAddZeroReg") |
| 2153 | << MatchTable::Comment("NewInsnID") << MatchTable::IntValue(NewInsnID) |
| 2154 | << MatchTable::Comment("OldInsnID") |
| 2155 | << MatchTable::IntValue(OldInsnVarID) << MatchTable::Comment("OpIdx") |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2156 | << MatchTable::IntValue(Operand.getOpIdx()) |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 2157 | << MatchTable::NamedValue( |
| 2158 | (ZeroRegisterDef->getValue("Namespace") |
| 2159 | ? ZeroRegisterDef->getValueAsString("Namespace") |
| 2160 | : ""), |
| 2161 | ZeroRegisterDef->getName()) |
| 2162 | << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak; |
| 2163 | } |
| 2164 | }; |
| 2165 | |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2166 | /// A CopyConstantAsImmRenderer emits code to render a G_CONSTANT instruction to |
| 2167 | /// an extended immediate operand. |
| 2168 | class CopyConstantAsImmRenderer : public OperandRenderer { |
| 2169 | protected: |
| 2170 | unsigned NewInsnID; |
| 2171 | /// The name of the operand. |
| 2172 | const std::string SymbolicName; |
| 2173 | bool Signed; |
| 2174 | |
| 2175 | public: |
| 2176 | CopyConstantAsImmRenderer(unsigned NewInsnID, StringRef SymbolicName) |
| 2177 | : OperandRenderer(OR_CopyConstantAsImm), NewInsnID(NewInsnID), |
| 2178 | SymbolicName(SymbolicName), Signed(true) {} |
| 2179 | |
| 2180 | static bool classof(const OperandRenderer *R) { |
| 2181 | return R->getKind() == OR_CopyConstantAsImm; |
| 2182 | } |
| 2183 | |
| 2184 | const StringRef getSymbolicName() const { return SymbolicName; } |
| 2185 | |
| 2186 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2187 | InstructionMatcher &InsnMatcher = Rule.getInstructionMatcher(SymbolicName); |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2188 | unsigned OldInsnVarID = Rule.getInsnVarID(InsnMatcher); |
| 2189 | Table << MatchTable::Opcode(Signed ? "GIR_CopyConstantAsSImm" |
| 2190 | : "GIR_CopyConstantAsUImm") |
| 2191 | << MatchTable::Comment("NewInsnID") << MatchTable::IntValue(NewInsnID) |
| 2192 | << MatchTable::Comment("OldInsnID") |
| 2193 | << MatchTable::IntValue(OldInsnVarID) |
| 2194 | << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak; |
| 2195 | } |
| 2196 | }; |
| 2197 | |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 2198 | /// A CopyFConstantAsFPImmRenderer emits code to render a G_FCONSTANT |
| 2199 | /// instruction to an extended immediate operand. |
| 2200 | class CopyFConstantAsFPImmRenderer : public OperandRenderer { |
| 2201 | protected: |
| 2202 | unsigned NewInsnID; |
| 2203 | /// The name of the operand. |
| 2204 | const std::string SymbolicName; |
| 2205 | |
| 2206 | public: |
| 2207 | CopyFConstantAsFPImmRenderer(unsigned NewInsnID, StringRef SymbolicName) |
| 2208 | : OperandRenderer(OR_CopyFConstantAsFPImm), NewInsnID(NewInsnID), |
| 2209 | SymbolicName(SymbolicName) {} |
| 2210 | |
| 2211 | static bool classof(const OperandRenderer *R) { |
| 2212 | return R->getKind() == OR_CopyFConstantAsFPImm; |
| 2213 | } |
| 2214 | |
| 2215 | const StringRef getSymbolicName() const { return SymbolicName; } |
| 2216 | |
| 2217 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2218 | InstructionMatcher &InsnMatcher = Rule.getInstructionMatcher(SymbolicName); |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 2219 | unsigned OldInsnVarID = Rule.getInsnVarID(InsnMatcher); |
| 2220 | Table << MatchTable::Opcode("GIR_CopyFConstantAsFPImm") |
| 2221 | << MatchTable::Comment("NewInsnID") << MatchTable::IntValue(NewInsnID) |
| 2222 | << MatchTable::Comment("OldInsnID") |
| 2223 | << MatchTable::IntValue(OldInsnVarID) |
| 2224 | << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak; |
| 2225 | } |
| 2226 | }; |
| 2227 | |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2228 | /// A CopySubRegRenderer emits code to copy a single register operand from an |
| 2229 | /// existing instruction to the one being built and indicate that only a |
| 2230 | /// subregister should be copied. |
| 2231 | class CopySubRegRenderer : public OperandRenderer { |
| 2232 | protected: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2233 | unsigned NewInsnID; |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2234 | /// The name of the operand. |
| 2235 | const StringRef SymbolicName; |
| 2236 | /// The subregister to extract. |
| 2237 | const CodeGenSubRegIndex *SubReg; |
| 2238 | |
| 2239 | public: |
Daniel Sanders | bd83ad4 | 2017-10-24 01:48:34 +0000 | [diff] [blame] | 2240 | CopySubRegRenderer(unsigned NewInsnID, StringRef SymbolicName, |
| 2241 | const CodeGenSubRegIndex *SubReg) |
| 2242 | : OperandRenderer(OR_CopySubReg), NewInsnID(NewInsnID), |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2243 | SymbolicName(SymbolicName), SubReg(SubReg) {} |
| 2244 | |
| 2245 | static bool classof(const OperandRenderer *R) { |
| 2246 | return R->getKind() == OR_CopySubReg; |
| 2247 | } |
| 2248 | |
| 2249 | const StringRef getSymbolicName() const { return SymbolicName; } |
| 2250 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2251 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 2252 | const OperandMatcher &Operand = Rule.getOperandMatcher(SymbolicName); |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2253 | unsigned OldInsnVarID = Rule.getInsnVarID(Operand.getInstructionMatcher()); |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2254 | Table << MatchTable::Opcode("GIR_CopySubReg") |
| 2255 | << MatchTable::Comment("NewInsnID") << MatchTable::IntValue(NewInsnID) |
| 2256 | << MatchTable::Comment("OldInsnID") |
| 2257 | << MatchTable::IntValue(OldInsnVarID) << MatchTable::Comment("OpIdx") |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2258 | << MatchTable::IntValue(Operand.getOpIdx()) |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2259 | << MatchTable::Comment("SubRegIdx") |
| 2260 | << MatchTable::IntValue(SubReg->EnumValue) |
| 2261 | << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak; |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2262 | } |
| 2263 | }; |
| 2264 | |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2265 | /// Adds a specific physical register to the instruction being built. |
| 2266 | /// This is typically useful for WZR/XZR on AArch64. |
| 2267 | class AddRegisterRenderer : public OperandRenderer { |
| 2268 | protected: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2269 | unsigned InsnID; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2270 | const Record *RegisterDef; |
| 2271 | |
| 2272 | public: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2273 | AddRegisterRenderer(unsigned InsnID, const Record *RegisterDef) |
| 2274 | : OperandRenderer(OR_Register), InsnID(InsnID), RegisterDef(RegisterDef) { |
| 2275 | } |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2276 | |
| 2277 | static bool classof(const OperandRenderer *R) { |
| 2278 | return R->getKind() == OR_Register; |
| 2279 | } |
| 2280 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2281 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
| 2282 | Table << MatchTable::Opcode("GIR_AddRegister") |
| 2283 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 2284 | << MatchTable::NamedValue( |
| 2285 | (RegisterDef->getValue("Namespace") |
| 2286 | ? RegisterDef->getValueAsString("Namespace") |
| 2287 | : ""), |
| 2288 | RegisterDef->getName()) |
| 2289 | << MatchTable::LineBreak; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2290 | } |
| 2291 | }; |
| 2292 | |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 2293 | /// Adds a specific temporary virtual register to the instruction being built. |
| 2294 | /// This is used to chain instructions together when emitting multiple |
| 2295 | /// instructions. |
| 2296 | class TempRegRenderer : public OperandRenderer { |
| 2297 | protected: |
| 2298 | unsigned InsnID; |
| 2299 | unsigned TempRegID; |
| 2300 | bool IsDef; |
| 2301 | |
| 2302 | public: |
| 2303 | TempRegRenderer(unsigned InsnID, unsigned TempRegID, bool IsDef = false) |
| 2304 | : OperandRenderer(OR_Register), InsnID(InsnID), TempRegID(TempRegID), |
| 2305 | IsDef(IsDef) {} |
| 2306 | |
| 2307 | static bool classof(const OperandRenderer *R) { |
| 2308 | return R->getKind() == OR_TempRegister; |
| 2309 | } |
| 2310 | |
| 2311 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
| 2312 | Table << MatchTable::Opcode("GIR_AddTempRegister") |
| 2313 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 2314 | << MatchTable::Comment("TempRegID") << MatchTable::IntValue(TempRegID) |
| 2315 | << MatchTable::Comment("TempRegFlags"); |
| 2316 | if (IsDef) |
| 2317 | Table << MatchTable::NamedValue("RegState::Define"); |
| 2318 | else |
| 2319 | Table << MatchTable::IntValue(0); |
| 2320 | Table << MatchTable::LineBreak; |
| 2321 | } |
| 2322 | }; |
| 2323 | |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 2324 | /// Adds a specific immediate to the instruction being built. |
| 2325 | class ImmRenderer : public OperandRenderer { |
| 2326 | protected: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2327 | unsigned InsnID; |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 2328 | int64_t Imm; |
| 2329 | |
| 2330 | public: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2331 | ImmRenderer(unsigned InsnID, int64_t Imm) |
| 2332 | : OperandRenderer(OR_Imm), InsnID(InsnID), Imm(Imm) {} |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 2333 | |
| 2334 | static bool classof(const OperandRenderer *R) { |
| 2335 | return R->getKind() == OR_Imm; |
| 2336 | } |
| 2337 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2338 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
| 2339 | Table << MatchTable::Opcode("GIR_AddImm") << MatchTable::Comment("InsnID") |
| 2340 | << MatchTable::IntValue(InsnID) << MatchTable::Comment("Imm") |
| 2341 | << MatchTable::IntValue(Imm) << MatchTable::LineBreak; |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 2342 | } |
| 2343 | }; |
| 2344 | |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 2345 | /// Adds operands by calling a renderer function supplied by the ComplexPattern |
| 2346 | /// matcher function. |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 2347 | class RenderComplexPatternOperand : public OperandRenderer { |
| 2348 | private: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2349 | unsigned InsnID; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 2350 | const Record &TheDef; |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 2351 | /// The name of the operand. |
| 2352 | const StringRef SymbolicName; |
| 2353 | /// The renderer number. This must be unique within a rule since it's used to |
| 2354 | /// identify a temporary variable to hold the renderer function. |
| 2355 | unsigned RendererID; |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 2356 | /// When provided, this is the suboperand of the ComplexPattern operand to |
| 2357 | /// render. Otherwise all the suboperands will be rendered. |
| 2358 | Optional<unsigned> SubOperand; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 2359 | |
| 2360 | unsigned getNumOperands() const { |
| 2361 | return TheDef.getValueAsDag("Operands")->getNumArgs(); |
| 2362 | } |
| 2363 | |
| 2364 | public: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2365 | RenderComplexPatternOperand(unsigned InsnID, const Record &TheDef, |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 2366 | StringRef SymbolicName, unsigned RendererID, |
| 2367 | Optional<unsigned> SubOperand = None) |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2368 | : OperandRenderer(OR_ComplexPattern), InsnID(InsnID), TheDef(TheDef), |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 2369 | SymbolicName(SymbolicName), RendererID(RendererID), |
| 2370 | SubOperand(SubOperand) {} |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 2371 | |
| 2372 | static bool classof(const OperandRenderer *R) { |
| 2373 | return R->getKind() == OR_ComplexPattern; |
| 2374 | } |
| 2375 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2376 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 2377 | Table << MatchTable::Opcode(SubOperand.hasValue() ? "GIR_ComplexSubOperandRenderer" |
| 2378 | : "GIR_ComplexRenderer") |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2379 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 2380 | << MatchTable::Comment("RendererID") |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 2381 | << MatchTable::IntValue(RendererID); |
| 2382 | if (SubOperand.hasValue()) |
| 2383 | Table << MatchTable::Comment("SubOperand") |
| 2384 | << MatchTable::IntValue(SubOperand.getValue()); |
| 2385 | Table << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 2386 | } |
| 2387 | }; |
| 2388 | |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 2389 | class CustomRenderer : public OperandRenderer { |
| 2390 | protected: |
| 2391 | unsigned InsnID; |
| 2392 | const Record &Renderer; |
| 2393 | /// The name of the operand. |
| 2394 | const std::string SymbolicName; |
| 2395 | |
| 2396 | public: |
| 2397 | CustomRenderer(unsigned InsnID, const Record &Renderer, |
| 2398 | StringRef SymbolicName) |
| 2399 | : OperandRenderer(OR_Custom), InsnID(InsnID), Renderer(Renderer), |
| 2400 | SymbolicName(SymbolicName) {} |
| 2401 | |
| 2402 | static bool classof(const OperandRenderer *R) { |
| 2403 | return R->getKind() == OR_Custom; |
| 2404 | } |
| 2405 | |
| 2406 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2407 | InstructionMatcher &InsnMatcher = Rule.getInstructionMatcher(SymbolicName); |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 2408 | unsigned OldInsnVarID = Rule.getInsnVarID(InsnMatcher); |
| 2409 | Table << MatchTable::Opcode("GIR_CustomRenderer") |
| 2410 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 2411 | << MatchTable::Comment("OldInsnID") |
| 2412 | << MatchTable::IntValue(OldInsnVarID) |
| 2413 | << MatchTable::Comment("Renderer") |
| 2414 | << MatchTable::NamedValue( |
| 2415 | "GICR_" + Renderer.getValueAsString("RendererFn").str()) |
| 2416 | << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak; |
| 2417 | } |
| 2418 | }; |
| 2419 | |
Ahmed Bougacha | 56ca3a9 | 2017-02-04 00:47:10 +0000 | [diff] [blame] | 2420 | /// An action taken when all Matcher predicates succeeded for a parent rule. |
| 2421 | /// |
| 2422 | /// Typical actions include: |
| 2423 | /// * Changing the opcode of an instruction. |
| 2424 | /// * Adding an operand to an instruction. |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 2425 | class MatchAction { |
| 2426 | public: |
| 2427 | virtual ~MatchAction() {} |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2428 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2429 | /// Emit the MatchTable opcodes to implement the action. |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2430 | virtual void emitActionOpcodes(MatchTable &Table, |
| 2431 | RuleMatcher &Rule) const = 0; |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 2432 | }; |
| 2433 | |
Ahmed Bougacha | 9aa4c10 | 2017-02-04 00:47:08 +0000 | [diff] [blame] | 2434 | /// Generates a comment describing the matched rule being acted upon. |
| 2435 | class DebugCommentAction : public MatchAction { |
| 2436 | private: |
Daniel Sanders | 6ea17ed | 2017-10-31 18:07:03 +0000 | [diff] [blame] | 2437 | std::string S; |
Ahmed Bougacha | 9aa4c10 | 2017-02-04 00:47:08 +0000 | [diff] [blame] | 2438 | |
| 2439 | public: |
Daniel Sanders | 6ea17ed | 2017-10-31 18:07:03 +0000 | [diff] [blame] | 2440 | DebugCommentAction(StringRef S) : S(S) {} |
Ahmed Bougacha | 9aa4c10 | 2017-02-04 00:47:08 +0000 | [diff] [blame] | 2441 | |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2442 | void emitActionOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
Daniel Sanders | 6ea17ed | 2017-10-31 18:07:03 +0000 | [diff] [blame] | 2443 | Table << MatchTable::Comment(S) << MatchTable::LineBreak; |
Ahmed Bougacha | 9aa4c10 | 2017-02-04 00:47:08 +0000 | [diff] [blame] | 2444 | } |
| 2445 | }; |
| 2446 | |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2447 | /// Generates code to build an instruction or mutate an existing instruction |
| 2448 | /// into the desired instruction when this is possible. |
| 2449 | class BuildMIAction : public MatchAction { |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 2450 | private: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2451 | unsigned InsnID; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2452 | const CodeGenInstruction *I; |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2453 | InstructionMatcher *Matched; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2454 | std::vector<std::unique_ptr<OperandRenderer>> OperandRenderers; |
| 2455 | |
| 2456 | /// True if the instruction can be built solely by mutating the opcode. |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2457 | bool canMutate(RuleMatcher &Rule, const InstructionMatcher *Insn) const { |
| 2458 | if (!Insn) |
Daniel Sanders | ab1d119 | 2017-10-24 18:11:54 +0000 | [diff] [blame] | 2459 | return false; |
| 2460 | |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2461 | if (OperandRenderers.size() != Insn->getNumOperands()) |
Daniel Sanders | e9fdba3 | 2017-04-29 17:30:09 +0000 | [diff] [blame] | 2462 | return false; |
| 2463 | |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2464 | for (const auto &Renderer : enumerate(OperandRenderers)) { |
Zachary Turner | 309a088 | 2017-03-13 16:24:10 +0000 | [diff] [blame] | 2465 | if (const auto *Copy = dyn_cast<CopyRenderer>(&*Renderer.value())) { |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 2466 | const OperandMatcher &OM = Rule.getOperandMatcher(Copy->getSymbolicName()); |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2467 | if (Insn != &OM.getInstructionMatcher() || |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2468 | OM.getOpIdx() != Renderer.index()) |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2469 | return false; |
| 2470 | } else |
| 2471 | return false; |
| 2472 | } |
| 2473 | |
| 2474 | return true; |
| 2475 | } |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2476 | |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 2477 | public: |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2478 | BuildMIAction(unsigned InsnID, const CodeGenInstruction *I) |
| 2479 | : InsnID(InsnID), I(I), Matched(nullptr) {} |
| 2480 | |
Daniel Sanders | 0846452 | 2018-01-29 21:09:12 +0000 | [diff] [blame] | 2481 | unsigned getInsnID() const { return InsnID; } |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 2482 | const CodeGenInstruction *getCGI() const { return I; } |
| 2483 | |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2484 | void chooseInsnToMutate(RuleMatcher &Rule) { |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2485 | for (auto *MutateCandidate : Rule.mutatable_insns()) { |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2486 | if (canMutate(Rule, MutateCandidate)) { |
| 2487 | // Take the first one we're offered that we're able to mutate. |
| 2488 | Rule.reserveInsnMatcherForMutation(MutateCandidate); |
| 2489 | Matched = MutateCandidate; |
| 2490 | return; |
| 2491 | } |
| 2492 | } |
| 2493 | } |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 2494 | |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2495 | template <class Kind, class... Args> |
| 2496 | Kind &addRenderer(Args&&... args) { |
| 2497 | OperandRenderers.emplace_back( |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 2498 | llvm::make_unique<Kind>(InsnID, std::forward<Args>(args)...)); |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2499 | return *static_cast<Kind *>(OperandRenderers.back().get()); |
| 2500 | } |
| 2501 | |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2502 | void emitActionOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
| 2503 | if (Matched) { |
| 2504 | assert(canMutate(Rule, Matched) && |
| 2505 | "Arranged to mutate an insn that isn't mutatable"); |
| 2506 | |
| 2507 | unsigned RecycleInsnID = Rule.getInsnVarID(*Matched); |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2508 | Table << MatchTable::Opcode("GIR_MutateOpcode") |
| 2509 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 2510 | << MatchTable::Comment("RecycleInsnID") |
| 2511 | << MatchTable::IntValue(RecycleInsnID) |
| 2512 | << MatchTable::Comment("Opcode") |
| 2513 | << MatchTable::NamedValue(I->Namespace, I->TheDef->getName()) |
| 2514 | << MatchTable::LineBreak; |
Tim Northover | 4340d64 | 2017-03-20 21:58:23 +0000 | [diff] [blame] | 2515 | |
| 2516 | if (!I->ImplicitDefs.empty() || !I->ImplicitUses.empty()) { |
Tim Northover | 4340d64 | 2017-03-20 21:58:23 +0000 | [diff] [blame] | 2517 | for (auto Def : I->ImplicitDefs) { |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 2518 | auto Namespace = Def->getValue("Namespace") |
| 2519 | ? Def->getValueAsString("Namespace") |
| 2520 | : ""; |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2521 | Table << MatchTable::Opcode("GIR_AddImplicitDef") |
| 2522 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 2523 | << MatchTable::NamedValue(Namespace, Def->getName()) |
| 2524 | << MatchTable::LineBreak; |
Tim Northover | 4340d64 | 2017-03-20 21:58:23 +0000 | [diff] [blame] | 2525 | } |
| 2526 | for (auto Use : I->ImplicitUses) { |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 2527 | auto Namespace = Use->getValue("Namespace") |
| 2528 | ? Use->getValueAsString("Namespace") |
| 2529 | : ""; |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2530 | Table << MatchTable::Opcode("GIR_AddImplicitUse") |
| 2531 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 2532 | << MatchTable::NamedValue(Namespace, Use->getName()) |
| 2533 | << MatchTable::LineBreak; |
Tim Northover | 4340d64 | 2017-03-20 21:58:23 +0000 | [diff] [blame] | 2534 | } |
| 2535 | } |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2536 | return; |
| 2537 | } |
| 2538 | |
| 2539 | // TODO: Simple permutation looks like it could be almost as common as |
| 2540 | // mutation due to commutative operations. |
| 2541 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2542 | Table << MatchTable::Opcode("GIR_BuildMI") << MatchTable::Comment("InsnID") |
| 2543 | << MatchTable::IntValue(InsnID) << MatchTable::Comment("Opcode") |
| 2544 | << MatchTable::NamedValue(I->Namespace, I->TheDef->getName()) |
| 2545 | << MatchTable::LineBreak; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2546 | for (const auto &Renderer : OperandRenderers) |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2547 | Renderer->emitRenderOpcodes(Table, Rule); |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2548 | |
Florian Hahn | 3bc3ec6 | 2017-08-03 14:48:22 +0000 | [diff] [blame] | 2549 | if (I->mayLoad || I->mayStore) { |
| 2550 | Table << MatchTable::Opcode("GIR_MergeMemOperands") |
| 2551 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 2552 | << MatchTable::Comment("MergeInsnID's"); |
| 2553 | // Emit the ID's for all the instructions that are matched by this rule. |
| 2554 | // TODO: Limit this to matched instructions that mayLoad/mayStore or have |
| 2555 | // some other means of having a memoperand. Also limit this to |
| 2556 | // emitted instructions that expect to have a memoperand too. For |
| 2557 | // example, (G_SEXT (G_LOAD x)) that results in separate load and |
| 2558 | // sign-extend instructions shouldn't put the memoperand on the |
| 2559 | // sign-extend since it has no effect there. |
| 2560 | std::vector<unsigned> MergeInsnIDs; |
| 2561 | for (const auto &IDMatcherPair : Rule.defined_insn_vars()) |
| 2562 | MergeInsnIDs.push_back(IDMatcherPair.second); |
Mandeep Singh Grang | 1b0e2f2 | 2018-04-06 20:18:05 +0000 | [diff] [blame] | 2563 | llvm::sort(MergeInsnIDs.begin(), MergeInsnIDs.end()); |
Florian Hahn | 3bc3ec6 | 2017-08-03 14:48:22 +0000 | [diff] [blame] | 2564 | for (const auto &MergeInsnID : MergeInsnIDs) |
| 2565 | Table << MatchTable::IntValue(MergeInsnID); |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2566 | Table << MatchTable::NamedValue("GIU_MergeMemOperands_EndOfList") |
| 2567 | << MatchTable::LineBreak; |
Florian Hahn | 3bc3ec6 | 2017-08-03 14:48:22 +0000 | [diff] [blame] | 2568 | } |
| 2569 | |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 2570 | // FIXME: This is a hack but it's sufficient for ISel. We'll need to do |
| 2571 | // better for combines. Particularly when there are multiple match |
| 2572 | // roots. |
| 2573 | if (InsnID == 0) |
| 2574 | Table << MatchTable::Opcode("GIR_EraseFromParent") |
| 2575 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 2576 | << MatchTable::LineBreak; |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2577 | } |
| 2578 | }; |
| 2579 | |
| 2580 | /// Generates code to constrain the operands of an output instruction to the |
| 2581 | /// register classes specified by the definition of that instruction. |
| 2582 | class ConstrainOperandsToDefinitionAction : public MatchAction { |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2583 | unsigned InsnID; |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2584 | |
| 2585 | public: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2586 | ConstrainOperandsToDefinitionAction(unsigned InsnID) : InsnID(InsnID) {} |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2587 | |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2588 | void emitActionOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2589 | Table << MatchTable::Opcode("GIR_ConstrainSelectedInstOperands") |
| 2590 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 2591 | << MatchTable::LineBreak; |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2592 | } |
| 2593 | }; |
| 2594 | |
| 2595 | /// Generates code to constrain the specified operand of an output instruction |
| 2596 | /// to the specified register class. |
| 2597 | class ConstrainOperandToRegClassAction : public MatchAction { |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2598 | unsigned InsnID; |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2599 | unsigned OpIdx; |
| 2600 | const CodeGenRegisterClass &RC; |
| 2601 | |
| 2602 | public: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2603 | ConstrainOperandToRegClassAction(unsigned InsnID, unsigned OpIdx, |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2604 | const CodeGenRegisterClass &RC) |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2605 | : InsnID(InsnID), OpIdx(OpIdx), RC(RC) {} |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2606 | |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2607 | void emitActionOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2608 | Table << MatchTable::Opcode("GIR_ConstrainOperandRC") |
| 2609 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 2610 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) |
| 2611 | << MatchTable::Comment("RC " + RC.getName()) |
| 2612 | << MatchTable::IntValue(RC.EnumValue) << MatchTable::LineBreak; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2613 | } |
| 2614 | }; |
| 2615 | |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 2616 | /// Generates code to create a temporary register which can be used to chain |
| 2617 | /// instructions together. |
| 2618 | class MakeTempRegisterAction : public MatchAction { |
| 2619 | private: |
| 2620 | LLTCodeGen Ty; |
| 2621 | unsigned TempRegID; |
| 2622 | |
| 2623 | public: |
| 2624 | MakeTempRegisterAction(const LLTCodeGen &Ty, unsigned TempRegID) |
| 2625 | : Ty(Ty), TempRegID(TempRegID) {} |
| 2626 | |
| 2627 | void emitActionOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
| 2628 | Table << MatchTable::Opcode("GIR_MakeTempReg") |
| 2629 | << MatchTable::Comment("TempRegID") << MatchTable::IntValue(TempRegID) |
| 2630 | << MatchTable::Comment("TypeID") |
| 2631 | << MatchTable::NamedValue(Ty.getCxxEnumValue()) |
| 2632 | << MatchTable::LineBreak; |
| 2633 | } |
| 2634 | }; |
| 2635 | |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2636 | InstructionMatcher &RuleMatcher::addInstructionMatcher(StringRef SymbolicName) { |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 2637 | Matchers.emplace_back(new InstructionMatcher(*this, SymbolicName)); |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2638 | MutatableInsns.insert(Matchers.back().get()); |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 2639 | return *Matchers.back(); |
| 2640 | } |
Ahmed Bougacha | 56ca3a9 | 2017-02-04 00:47:10 +0000 | [diff] [blame] | 2641 | |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 2642 | void RuleMatcher::addRequiredFeature(Record *Feature) { |
| 2643 | RequiredFeatures.push_back(Feature); |
| 2644 | } |
| 2645 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 2646 | const std::vector<Record *> &RuleMatcher::getRequiredFeatures() const { |
| 2647 | return RequiredFeatures; |
| 2648 | } |
| 2649 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 2650 | // Emplaces an action of the specified Kind at the end of the action list. |
| 2651 | // |
| 2652 | // Returns a reference to the newly created action. |
| 2653 | // |
| 2654 | // Like std::vector::emplace_back(), may invalidate all iterators if the new |
| 2655 | // size exceeds the capacity. Otherwise, only invalidates the past-the-end |
| 2656 | // iterator. |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 2657 | template <class Kind, class... Args> |
| 2658 | Kind &RuleMatcher::addAction(Args &&... args) { |
| 2659 | Actions.emplace_back(llvm::make_unique<Kind>(std::forward<Args>(args)...)); |
| 2660 | return *static_cast<Kind *>(Actions.back().get()); |
| 2661 | } |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 2662 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 2663 | // Emplaces an action of the specified Kind before the given insertion point. |
| 2664 | // |
| 2665 | // Returns an iterator pointing at the newly created instruction. |
| 2666 | // |
| 2667 | // Like std::vector::insert(), may invalidate all iterators if the new size |
| 2668 | // exceeds the capacity. Otherwise, only invalidates the iterators from the |
| 2669 | // insertion point onwards. |
| 2670 | template <class Kind, class... Args> |
| 2671 | action_iterator RuleMatcher::insertAction(action_iterator InsertPt, |
| 2672 | Args &&... args) { |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 2673 | return Actions.emplace(InsertPt, |
| 2674 | llvm::make_unique<Kind>(std::forward<Args>(args)...)); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 2675 | } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 2676 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2677 | unsigned RuleMatcher::implicitlyDefineInsnVar(InstructionMatcher &Matcher) { |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 2678 | unsigned NewInsnVarID = NextInsnVarID++; |
| 2679 | InsnVariableIDs[&Matcher] = NewInsnVarID; |
| 2680 | return NewInsnVarID; |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 2681 | } |
| 2682 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2683 | unsigned RuleMatcher::getInsnVarID(InstructionMatcher &InsnMatcher) const { |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 2684 | const auto &I = InsnVariableIDs.find(&InsnMatcher); |
| 2685 | if (I != InsnVariableIDs.end()) |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 2686 | return I->second; |
| 2687 | llvm_unreachable("Matched Insn was not captured in a local variable"); |
| 2688 | } |
| 2689 | |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 2690 | void RuleMatcher::defineOperand(StringRef SymbolicName, OperandMatcher &OM) { |
| 2691 | if (DefinedOperands.find(SymbolicName) == DefinedOperands.end()) { |
| 2692 | DefinedOperands[SymbolicName] = &OM; |
| 2693 | return; |
| 2694 | } |
| 2695 | |
| 2696 | // If the operand is already defined, then we must ensure both references in |
| 2697 | // the matcher have the exact same node. |
| 2698 | OM.addPredicate<SameOperandMatcher>(OM.getSymbolicName()); |
| 2699 | } |
| 2700 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2701 | InstructionMatcher & |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2702 | RuleMatcher::getInstructionMatcher(StringRef SymbolicName) const { |
| 2703 | for (const auto &I : InsnVariableIDs) |
| 2704 | if (I.first->getSymbolicName() == SymbolicName) |
| 2705 | return *I.first; |
| 2706 | llvm_unreachable( |
| 2707 | ("Failed to lookup instruction " + SymbolicName).str().c_str()); |
| 2708 | } |
| 2709 | |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 2710 | const OperandMatcher & |
| 2711 | RuleMatcher::getOperandMatcher(StringRef Name) const { |
| 2712 | const auto &I = DefinedOperands.find(Name); |
| 2713 | |
| 2714 | if (I == DefinedOperands.end()) |
| 2715 | PrintFatalError(SrcLoc, "Operand " + Name + " was not declared in matcher"); |
| 2716 | |
| 2717 | return *I->second; |
| 2718 | } |
| 2719 | |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 2720 | void RuleMatcher::emit(MatchTable &Table) { |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 2721 | if (Matchers.empty()) |
| 2722 | llvm_unreachable("Unexpected empty matcher!"); |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 2723 | |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 2724 | // The representation supports rules that require multiple roots such as: |
| 2725 | // %ptr(p0) = ... |
| 2726 | // %elt0(s32) = G_LOAD %ptr |
| 2727 | // %1(p0) = G_ADD %ptr, 4 |
| 2728 | // %elt1(s32) = G_LOAD p0 %1 |
| 2729 | // which could be usefully folded into: |
| 2730 | // %ptr(p0) = ... |
| 2731 | // %elt0(s32), %elt1(s32) = TGT_LOAD_PAIR %ptr |
| 2732 | // on some targets but we don't need to make use of that yet. |
| 2733 | assert(Matchers.size() == 1 && "Cannot handle multi-root matchers yet"); |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 2734 | |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 2735 | unsigned LabelID = Table.allocateLabelID(); |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2736 | Table << MatchTable::Opcode("GIM_Try", +1) |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2737 | << MatchTable::Comment("On fail goto") |
| 2738 | << MatchTable::JumpTarget(LabelID) |
| 2739 | << MatchTable::Comment(("Rule ID " + Twine(RuleID) + " //").str()) |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2740 | << MatchTable::LineBreak; |
| 2741 | |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 2742 | if (!RequiredFeatures.empty()) { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2743 | Table << MatchTable::Opcode("GIM_CheckFeatures") |
| 2744 | << MatchTable::NamedValue(getNameForFeatureBitset(RequiredFeatures)) |
| 2745 | << MatchTable::LineBreak; |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 2746 | } |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 2747 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 2748 | Matchers.front()->emitPredicateOpcodes(Table, *this); |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 2749 | |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 2750 | // 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] | 2751 | if (InsnVariableIDs.size() >= 2) { |
Galina Kistanova | 1754fee | 2017-05-25 01:51:53 +0000 | [diff] [blame] | 2752 | // Invert the map to create stable ordering (by var names) |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 2753 | SmallVector<unsigned, 2> InsnIDs; |
| 2754 | for (const auto &Pair : InsnVariableIDs) { |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 2755 | // Skip the root node since it isn't moving anywhere. Everything else is |
| 2756 | // sinking to meet it. |
| 2757 | if (Pair.first == Matchers.front().get()) |
| 2758 | continue; |
| 2759 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 2760 | InsnIDs.push_back(Pair.second); |
Galina Kistanova | 1754fee | 2017-05-25 01:51:53 +0000 | [diff] [blame] | 2761 | } |
Mandeep Singh Grang | 1b0e2f2 | 2018-04-06 20:18:05 +0000 | [diff] [blame] | 2762 | llvm::sort(InsnIDs.begin(), InsnIDs.end()); |
Galina Kistanova | 1754fee | 2017-05-25 01:51:53 +0000 | [diff] [blame] | 2763 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 2764 | for (const auto &InsnID : InsnIDs) { |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 2765 | // Reject the difficult cases until we have a more accurate check. |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2766 | Table << MatchTable::Opcode("GIM_CheckIsSafeToFold") |
| 2767 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 2768 | << MatchTable::LineBreak; |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 2769 | |
| 2770 | // FIXME: Emit checks to determine it's _actually_ safe to fold and/or |
| 2771 | // account for unsafe cases. |
| 2772 | // |
| 2773 | // Example: |
| 2774 | // MI1--> %0 = ... |
| 2775 | // %1 = ... %0 |
| 2776 | // MI0--> %2 = ... %0 |
| 2777 | // It's not safe to erase MI1. We currently handle this by not |
| 2778 | // erasing %0 (even when it's dead). |
| 2779 | // |
| 2780 | // Example: |
| 2781 | // MI1--> %0 = load volatile @a |
| 2782 | // %1 = load volatile @a |
| 2783 | // MI0--> %2 = ... %0 |
| 2784 | // It's not safe to sink %0's def past %1. We currently handle |
| 2785 | // this by rejecting all loads. |
| 2786 | // |
| 2787 | // Example: |
| 2788 | // MI1--> %0 = load @a |
| 2789 | // %1 = store @a |
| 2790 | // MI0--> %2 = ... %0 |
| 2791 | // It's not safe to sink %0's def past %1. We currently handle |
| 2792 | // this by rejecting all loads. |
| 2793 | // |
| 2794 | // Example: |
| 2795 | // G_CONDBR %cond, @BB1 |
| 2796 | // BB0: |
| 2797 | // MI1--> %0 = load @a |
| 2798 | // G_BR @BB1 |
| 2799 | // BB1: |
| 2800 | // MI0--> %2 = ... %0 |
| 2801 | // It's not always safe to sink %0 across control flow. In this |
| 2802 | // case it may introduce a memory fault. We currentl handle this |
| 2803 | // by rejecting all loads. |
| 2804 | } |
| 2805 | } |
| 2806 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2807 | for (const auto &PM : EpilogueMatchers) |
| 2808 | PM->emitPredicateOpcodes(Table, *this); |
| 2809 | |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2810 | for (const auto &MA : Actions) |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2811 | MA->emitActionOpcodes(Table, *this); |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 2812 | |
Roman Tereshin | beb3931 | 2018-05-02 20:15:11 +0000 | [diff] [blame] | 2813 | if (Table.isWithCoverage()) |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 2814 | Table << MatchTable::Opcode("GIR_Coverage") << MatchTable::IntValue(RuleID) |
| 2815 | << MatchTable::LineBreak; |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2816 | else |
| 2817 | Table << MatchTable::Comment(("GIR_Coverage, " + Twine(RuleID) + ",").str()) |
| 2818 | << MatchTable::LineBreak; |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 2819 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2820 | Table << MatchTable::Opcode("GIR_Done", -1) << MatchTable::LineBreak |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 2821 | << MatchTable::Label(LabelID); |
Volkan Keles | 4f3fa79 | 2018-01-25 00:18:52 +0000 | [diff] [blame] | 2822 | ++NumPatternEmitted; |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 2823 | } |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 2824 | |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 2825 | bool RuleMatcher::isHigherPriorityThan(const RuleMatcher &B) const { |
| 2826 | // Rules involving more match roots have higher priority. |
| 2827 | if (Matchers.size() > B.Matchers.size()) |
| 2828 | return true; |
| 2829 | if (Matchers.size() < B.Matchers.size()) |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 2830 | return false; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 2831 | |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 2832 | for (const auto &Matcher : zip(Matchers, B.Matchers)) { |
| 2833 | if (std::get<0>(Matcher)->isHigherPriorityThan(*std::get<1>(Matcher))) |
| 2834 | return true; |
| 2835 | if (std::get<1>(Matcher)->isHigherPriorityThan(*std::get<0>(Matcher))) |
| 2836 | return false; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 2837 | } |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 2838 | |
| 2839 | return false; |
Simon Pilgrim | a7d1da8 | 2017-03-15 22:50:47 +0000 | [diff] [blame] | 2840 | } |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2841 | |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 2842 | unsigned RuleMatcher::countRendererFns() const { |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 2843 | return std::accumulate( |
| 2844 | Matchers.begin(), Matchers.end(), 0, |
| 2845 | [](unsigned A, const std::unique_ptr<InstructionMatcher> &Matcher) { |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 2846 | return A + Matcher->countRendererFns(); |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 2847 | }); |
| 2848 | } |
| 2849 | |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2850 | bool OperandPredicateMatcher::isHigherPriorityThan( |
| 2851 | const OperandPredicateMatcher &B) const { |
| 2852 | // Generally speaking, an instruction is more important than an Int or a |
| 2853 | // LiteralInt because it can cover more nodes but theres an exception to |
| 2854 | // this. G_CONSTANT's are less important than either of those two because they |
| 2855 | // are more permissive. |
Daniel Sanders | edd0784 | 2017-08-17 09:26:14 +0000 | [diff] [blame] | 2856 | |
| 2857 | const InstructionOperandMatcher *AOM = |
| 2858 | dyn_cast<InstructionOperandMatcher>(this); |
| 2859 | const InstructionOperandMatcher *BOM = |
| 2860 | dyn_cast<InstructionOperandMatcher>(&B); |
| 2861 | bool AIsConstantInsn = AOM && AOM->getInsnMatcher().isConstantInstruction(); |
| 2862 | bool BIsConstantInsn = BOM && BOM->getInsnMatcher().isConstantInstruction(); |
| 2863 | |
| 2864 | if (AOM && BOM) { |
| 2865 | // The relative priorities between a G_CONSTANT and any other instruction |
| 2866 | // don't actually matter but this code is needed to ensure a strict weak |
| 2867 | // ordering. This is particularly important on Windows where the rules will |
| 2868 | // be incorrectly sorted without it. |
| 2869 | if (AIsConstantInsn != BIsConstantInsn) |
| 2870 | return AIsConstantInsn < BIsConstantInsn; |
| 2871 | return false; |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2872 | } |
Daniel Sanders | edd0784 | 2017-08-17 09:26:14 +0000 | [diff] [blame] | 2873 | |
| 2874 | if (AOM && AIsConstantInsn && (B.Kind == OPM_Int || B.Kind == OPM_LiteralInt)) |
| 2875 | return false; |
| 2876 | if (BOM && BIsConstantInsn && (Kind == OPM_Int || Kind == OPM_LiteralInt)) |
| 2877 | return true; |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2878 | |
| 2879 | return Kind < B.Kind; |
Daniel Sanders | 75b84fc | 2017-08-08 13:21:26 +0000 | [diff] [blame] | 2880 | } |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2881 | |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 2882 | void SameOperandMatcher::emitPredicateOpcodes(MatchTable &Table, |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 2883 | RuleMatcher &Rule) const { |
Daniel Sanders | 1e4569f | 2017-10-20 20:55:29 +0000 | [diff] [blame] | 2884 | const OperandMatcher &OtherOM = Rule.getOperandMatcher(MatchingName); |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 2885 | unsigned OtherInsnVarID = Rule.getInsnVarID(OtherOM.getInstructionMatcher()); |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2886 | assert(OtherInsnVarID == OtherOM.getInstructionMatcher().getInsnVarID()); |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 2887 | |
| 2888 | Table << MatchTable::Opcode("GIM_CheckIsSameOperand") |
| 2889 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 2890 | << MatchTable::Comment("OpIdx") << MatchTable::IntValue(OpIdx) |
| 2891 | << MatchTable::Comment("OtherMI") |
| 2892 | << MatchTable::IntValue(OtherInsnVarID) |
| 2893 | << MatchTable::Comment("OtherOpIdx") |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2894 | << MatchTable::IntValue(OtherOM.getOpIdx()) |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 2895 | << MatchTable::LineBreak; |
| 2896 | } |
| 2897 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2898 | //===- GlobalISelEmitter class --------------------------------------------===// |
| 2899 | |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 2900 | class GlobalISelEmitter { |
| 2901 | public: |
| 2902 | explicit GlobalISelEmitter(RecordKeeper &RK); |
| 2903 | void run(raw_ostream &OS); |
| 2904 | |
| 2905 | private: |
| 2906 | const RecordKeeper &RK; |
| 2907 | const CodeGenDAGPatterns CGP; |
| 2908 | const CodeGenTarget &Target; |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2909 | CodeGenRegBank CGRegs; |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 2910 | |
Daniel Sanders | 39690bd | 2017-10-15 02:41:12 +0000 | [diff] [blame] | 2911 | /// Keep track of the equivalence between SDNodes and Instruction by mapping |
Daniel Sanders | 3c1c4c0 | 2017-12-05 05:52:07 +0000 | [diff] [blame] | 2912 | /// SDNodes to the GINodeEquiv mapping. We need to map to the GINodeEquiv to |
| 2913 | /// check for attributes on the relation such as CheckMMOIsNonAtomic. |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 2914 | /// This is defined using 'GINodeEquiv' in the target description. |
Daniel Sanders | 39690bd | 2017-10-15 02:41:12 +0000 | [diff] [blame] | 2915 | DenseMap<Record *, Record *> NodeEquivs; |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 2916 | |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 2917 | /// Keep track of the equivalence between ComplexPattern's and |
| 2918 | /// GIComplexOperandMatcher. Map entries are specified by subclassing |
| 2919 | /// GIComplexPatternEquiv. |
| 2920 | DenseMap<const Record *, const Record *> ComplexPatternEquivs; |
| 2921 | |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 2922 | /// Keep track of the equivalence between SDNodeXForm's and |
| 2923 | /// GICustomOperandRenderer. Map entries are specified by subclassing |
| 2924 | /// GISDNodeXFormEquiv. |
| 2925 | DenseMap<const Record *, const Record *> SDNodeXFormEquivs; |
| 2926 | |
Aditya Nandakumar | b63e763 | 2018-02-16 22:37:15 +0000 | [diff] [blame] | 2927 | /// Keep track of Scores of PatternsToMatch similar to how the DAG does. |
| 2928 | /// This adds compatibility for RuleMatchers to use this for ordering rules. |
| 2929 | DenseMap<uint64_t, int> RuleMatcherScores; |
| 2930 | |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 2931 | // Map of predicates to their subtarget features. |
Daniel Sanders | e9fdba3 | 2017-04-29 17:30:09 +0000 | [diff] [blame] | 2932 | SubtargetFeatureInfoMap SubtargetFeatures; |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 2933 | |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 2934 | // Rule coverage information. |
| 2935 | Optional<CodeGenCoverage> RuleCoverage; |
| 2936 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2937 | void gatherOpcodeValues(); |
| 2938 | void gatherTypeIDValues(); |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 2939 | void gatherNodeEquivs(); |
Daniel Sanders | 39690bd | 2017-10-15 02:41:12 +0000 | [diff] [blame] | 2940 | Record *findNodeEquiv(Record *N) const; |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 2941 | const CodeGenInstruction *getEquivNode(Record &Equiv, |
| 2942 | const TreePatternNode *N) const; |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 2943 | |
Krzysztof Parzyszek | 779d98e | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 2944 | Error importRulePredicates(RuleMatcher &M, ArrayRef<Predicate> Predicates); |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 2945 | Expected<InstructionMatcher &> createAndImportSelDAGMatcher( |
| 2946 | RuleMatcher &Rule, InstructionMatcher &InsnMatcher, |
| 2947 | const TreePatternNode *Src, unsigned &TempOpIdx) const; |
| 2948 | Error importComplexPatternOperandMatcher(OperandMatcher &OM, Record *R, |
| 2949 | unsigned &TempOpIdx) const; |
| 2950 | Error importChildMatcher(RuleMatcher &Rule, InstructionMatcher &InsnMatcher, |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 2951 | const TreePatternNode *SrcChild, |
| 2952 | bool OperandIsAPointer, unsigned OpIdx, |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 2953 | unsigned &TempOpIdx) const; |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 2954 | |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2955 | Expected<BuildMIAction &> |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2956 | createAndImportInstructionRenderer(RuleMatcher &M, |
| 2957 | const TreePatternNode *Dst); |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 2958 | Expected<action_iterator> createAndImportSubInstructionRenderer( |
| 2959 | action_iterator InsertPt, RuleMatcher &M, const TreePatternNode *Dst, |
| 2960 | unsigned TempReg); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 2961 | Expected<action_iterator> |
| 2962 | createInstructionRenderer(action_iterator InsertPt, RuleMatcher &M, |
| 2963 | const TreePatternNode *Dst); |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 2964 | void importExplicitDefRenderers(BuildMIAction &DstMIBuilder); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 2965 | Expected<action_iterator> |
| 2966 | importExplicitUseRenderers(action_iterator InsertPt, RuleMatcher &M, |
| 2967 | BuildMIAction &DstMIBuilder, |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 2968 | const llvm::TreePatternNode *Dst); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 2969 | Expected<action_iterator> |
| 2970 | importExplicitUseRenderer(action_iterator InsertPt, RuleMatcher &Rule, |
| 2971 | BuildMIAction &DstMIBuilder, |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 2972 | TreePatternNode *DstChild); |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 2973 | Error importDefaultOperandRenderers(BuildMIAction &DstMIBuilder, |
| 2974 | DagInit *DefaultOps) const; |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 2975 | Error |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2976 | importImplicitDefRenderers(BuildMIAction &DstMIBuilder, |
| 2977 | const std::vector<Record *> &ImplicitDefs) const; |
| 2978 | |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 2979 | void emitImmPredicates(raw_ostream &OS, StringRef TypeIdentifier, |
| 2980 | StringRef Type, |
Daniel Sanders | 649c585 | 2017-10-13 20:42:18 +0000 | [diff] [blame] | 2981 | std::function<bool(const Record *R)> Filter); |
| 2982 | |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 2983 | /// Analyze pattern \p P, returning a matcher for it if possible. |
| 2984 | /// Otherwise, return an Error explaining why we don't support it. |
| 2985 | Expected<RuleMatcher> runOnPattern(const PatternToMatch &P); |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 2986 | |
| 2987 | void declareSubtargetFeature(Record *Predicate); |
Daniel Sanders | 7e52367 | 2017-11-11 03:23:44 +0000 | [diff] [blame] | 2988 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2989 | MatchTable buildMatchTable(MutableArrayRef<RuleMatcher> Rules, bool Optimize, |
| 2990 | bool WithCoverage); |
| 2991 | |
| 2992 | public: |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 2993 | /// 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] | 2994 | /// they share. \p MatcherStorage is used as a memory container |
Hiroshi Inoue | 501931b | 2018-01-24 05:04:35 +0000 | [diff] [blame] | 2995 | /// for the group that are created as part of this process. |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 2996 | /// |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2997 | /// What this optimization does looks like if GroupT = GroupMatcher: |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 2998 | /// Output without optimization: |
| 2999 | /// \verbatim |
| 3000 | /// # R1 |
| 3001 | /// # predicate A |
| 3002 | /// # predicate B |
| 3003 | /// ... |
| 3004 | /// # R2 |
| 3005 | /// # predicate A // <-- effectively this is going to be checked twice. |
| 3006 | /// // Once in R1 and once in R2. |
| 3007 | /// # predicate C |
| 3008 | /// \endverbatim |
| 3009 | /// Output with optimization: |
| 3010 | /// \verbatim |
| 3011 | /// # Group1_2 |
| 3012 | /// # predicate A // <-- Check is now shared. |
| 3013 | /// # R1 |
| 3014 | /// # predicate B |
| 3015 | /// # R2 |
| 3016 | /// # predicate C |
| 3017 | /// \endverbatim |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 3018 | template <class GroupT> |
| 3019 | static std::vector<Matcher *> optimizeRules( |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 3020 | ArrayRef<Matcher *> Rules, |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 3021 | std::vector<std::unique_ptr<Matcher>> &MatcherStorage); |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 3022 | }; |
| 3023 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 3024 | void GlobalISelEmitter::gatherOpcodeValues() { |
| 3025 | InstructionOpcodeMatcher::initOpcodeValuesMap(Target); |
| 3026 | } |
| 3027 | |
| 3028 | void GlobalISelEmitter::gatherTypeIDValues() { |
| 3029 | LLTOperandMatcher::initTypeIDValuesMap(); |
| 3030 | } |
| 3031 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 3032 | void GlobalISelEmitter::gatherNodeEquivs() { |
| 3033 | assert(NodeEquivs.empty()); |
| 3034 | for (Record *Equiv : RK.getAllDerivedDefinitions("GINodeEquiv")) |
Daniel Sanders | 39690bd | 2017-10-15 02:41:12 +0000 | [diff] [blame] | 3035 | NodeEquivs[Equiv->getValueAsDef("Node")] = Equiv; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 3036 | |
| 3037 | assert(ComplexPatternEquivs.empty()); |
| 3038 | for (Record *Equiv : RK.getAllDerivedDefinitions("GIComplexPatternEquiv")) { |
| 3039 | Record *SelDAGEquiv = Equiv->getValueAsDef("SelDAGEquivalent"); |
| 3040 | if (!SelDAGEquiv) |
| 3041 | continue; |
| 3042 | ComplexPatternEquivs[SelDAGEquiv] = Equiv; |
| 3043 | } |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 3044 | |
| 3045 | assert(SDNodeXFormEquivs.empty()); |
| 3046 | for (Record *Equiv : RK.getAllDerivedDefinitions("GISDNodeXFormEquiv")) { |
| 3047 | Record *SelDAGEquiv = Equiv->getValueAsDef("SelDAGEquivalent"); |
| 3048 | if (!SelDAGEquiv) |
| 3049 | continue; |
| 3050 | SDNodeXFormEquivs[SelDAGEquiv] = Equiv; |
| 3051 | } |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 3052 | } |
| 3053 | |
Daniel Sanders | 39690bd | 2017-10-15 02:41:12 +0000 | [diff] [blame] | 3054 | Record *GlobalISelEmitter::findNodeEquiv(Record *N) const { |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 3055 | return NodeEquivs.lookup(N); |
| 3056 | } |
| 3057 | |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 3058 | const CodeGenInstruction * |
| 3059 | GlobalISelEmitter::getEquivNode(Record &Equiv, const TreePatternNode *N) const { |
| 3060 | for (const auto &Predicate : N->getPredicateFns()) { |
| 3061 | if (!Equiv.isValueUnset("IfSignExtend") && Predicate.isLoad() && |
| 3062 | Predicate.isSignExtLoad()) |
| 3063 | return &Target.getInstruction(Equiv.getValueAsDef("IfSignExtend")); |
| 3064 | if (!Equiv.isValueUnset("IfZeroExtend") && Predicate.isLoad() && |
| 3065 | Predicate.isZeroExtLoad()) |
| 3066 | return &Target.getInstruction(Equiv.getValueAsDef("IfZeroExtend")); |
| 3067 | } |
| 3068 | return &Target.getInstruction(Equiv.getValueAsDef("I")); |
| 3069 | } |
| 3070 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 3071 | GlobalISelEmitter::GlobalISelEmitter(RecordKeeper &RK) |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 3072 | : RK(RK), CGP(RK), Target(CGP.getTargetInfo()), |
| 3073 | CGRegs(RK, Target.getHwModes()) {} |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 3074 | |
| 3075 | //===- Emitter ------------------------------------------------------------===// |
| 3076 | |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 3077 | Error |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3078 | GlobalISelEmitter::importRulePredicates(RuleMatcher &M, |
Krzysztof Parzyszek | 779d98e | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 3079 | ArrayRef<Predicate> Predicates) { |
| 3080 | for (const Predicate &P : Predicates) { |
| 3081 | if (!P.Def) |
| 3082 | continue; |
| 3083 | declareSubtargetFeature(P.Def); |
| 3084 | M.addRequiredFeature(P.Def); |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 3085 | } |
| 3086 | |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 3087 | return Error::success(); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3088 | } |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 3089 | |
Daniel Sanders | 3c1c4c0 | 2017-12-05 05:52:07 +0000 | [diff] [blame] | 3090 | Expected<InstructionMatcher &> GlobalISelEmitter::createAndImportSelDAGMatcher( |
| 3091 | RuleMatcher &Rule, InstructionMatcher &InsnMatcher, |
| 3092 | const TreePatternNode *Src, unsigned &TempOpIdx) const { |
| 3093 | Record *SrcGIEquivOrNull = nullptr; |
| 3094 | const CodeGenInstruction *SrcGIOrNull = nullptr; |
| 3095 | |
| 3096 | // Start with the defined operands (i.e., the results of the root operator). |
| 3097 | if (Src->getExtTypes().size() > 1) |
| 3098 | return failedImport("Src pattern has multiple results"); |
| 3099 | |
| 3100 | if (Src->isLeaf()) { |
| 3101 | Init *SrcInit = Src->getLeafValue(); |
| 3102 | if (isa<IntInit>(SrcInit)) { |
| 3103 | InsnMatcher.addPredicate<InstructionOpcodeMatcher>( |
| 3104 | &Target.getInstruction(RK.getDef("G_CONSTANT"))); |
| 3105 | } else |
| 3106 | return failedImport( |
| 3107 | "Unable to deduce gMIR opcode to handle Src (which is a leaf)"); |
| 3108 | } else { |
| 3109 | SrcGIEquivOrNull = findNodeEquiv(Src->getOperator()); |
| 3110 | if (!SrcGIEquivOrNull) |
| 3111 | return failedImport("Pattern operator lacks an equivalent Instruction" + |
| 3112 | explainOperator(Src->getOperator())); |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 3113 | SrcGIOrNull = getEquivNode(*SrcGIEquivOrNull, Src); |
Daniel Sanders | 3c1c4c0 | 2017-12-05 05:52:07 +0000 | [diff] [blame] | 3114 | |
| 3115 | // The operators look good: match the opcode |
| 3116 | InsnMatcher.addPredicate<InstructionOpcodeMatcher>(SrcGIOrNull); |
| 3117 | } |
| 3118 | |
| 3119 | unsigned OpIdx = 0; |
| 3120 | for (const TypeSetByHwMode &VTy : Src->getExtTypes()) { |
| 3121 | // Results don't have a name unless they are the root node. The caller will |
| 3122 | // set the name if appropriate. |
| 3123 | OperandMatcher &OM = InsnMatcher.addOperand(OpIdx++, "", TempOpIdx); |
| 3124 | if (auto Error = OM.addTypeCheckPredicate(VTy, false /* OperandIsAPointer */)) |
| 3125 | return failedImport(toString(std::move(Error)) + |
| 3126 | " for result of Src pattern operator"); |
| 3127 | } |
| 3128 | |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 3129 | for (const auto &Predicate : Src->getPredicateFns()) { |
| 3130 | if (Predicate.isAlwaysTrue()) |
| 3131 | continue; |
| 3132 | |
| 3133 | if (Predicate.isImmediatePattern()) { |
| 3134 | InsnMatcher.addPredicate<InstructionImmPredicateMatcher>(Predicate); |
| 3135 | continue; |
| 3136 | } |
| 3137 | |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 3138 | // G_LOAD is used for both non-extending and any-extending loads. |
| 3139 | if (Predicate.isLoad() && Predicate.isNonExtLoad()) { |
| 3140 | InsnMatcher.addPredicate<MemoryVsLLTSizePredicateMatcher>( |
| 3141 | 0, MemoryVsLLTSizePredicateMatcher::EqualTo, 0); |
| 3142 | continue; |
| 3143 | } |
| 3144 | if (Predicate.isLoad() && Predicate.isAnyExtLoad()) { |
| 3145 | InsnMatcher.addPredicate<MemoryVsLLTSizePredicateMatcher>( |
| 3146 | 0, MemoryVsLLTSizePredicateMatcher::LessThan, 0); |
| 3147 | continue; |
| 3148 | } |
| 3149 | |
| 3150 | // No check required. We already did it by swapping the opcode. |
| 3151 | if (!SrcGIEquivOrNull->isValueUnset("IfSignExtend") && |
| 3152 | Predicate.isSignExtLoad()) |
| 3153 | continue; |
| 3154 | |
| 3155 | // No check required. We already did it by swapping the opcode. |
| 3156 | if (!SrcGIEquivOrNull->isValueUnset("IfZeroExtend") && |
| 3157 | Predicate.isZeroExtLoad()) |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 3158 | continue; |
| 3159 | |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 3160 | // No check required. G_STORE by itself is a non-extending store. |
| 3161 | if (Predicate.isNonTruncStore()) |
| 3162 | continue; |
| 3163 | |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 3164 | if (Predicate.isLoad() || Predicate.isStore() || Predicate.isAtomic()) { |
| 3165 | if (Predicate.getMemoryVT() != nullptr) { |
| 3166 | Optional<LLTCodeGen> MemTyOrNone = |
| 3167 | MVTToLLT(getValueType(Predicate.getMemoryVT())); |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 3168 | |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 3169 | if (!MemTyOrNone) |
| 3170 | return failedImport("MemVT could not be converted to LLT"); |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 3171 | |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 3172 | // MMO's work in bytes so we must take care of unusual types like i1 |
| 3173 | // don't round down. |
| 3174 | unsigned MemSizeInBits = |
| 3175 | llvm::alignTo(MemTyOrNone->get().getSizeInBits(), 8); |
| 3176 | |
| 3177 | InsnMatcher.addPredicate<MemorySizePredicateMatcher>( |
| 3178 | 0, MemSizeInBits / 8); |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 3179 | continue; |
| 3180 | } |
| 3181 | } |
| 3182 | |
| 3183 | if (Predicate.isLoad() || Predicate.isStore()) { |
| 3184 | // No check required. A G_LOAD/G_STORE is an unindexed load. |
| 3185 | if (Predicate.isUnindexed()) |
| 3186 | continue; |
| 3187 | } |
| 3188 | |
| 3189 | if (Predicate.isAtomic()) { |
| 3190 | if (Predicate.isAtomicOrderingMonotonic()) { |
| 3191 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>( |
| 3192 | "Monotonic"); |
| 3193 | continue; |
| 3194 | } |
| 3195 | if (Predicate.isAtomicOrderingAcquire()) { |
| 3196 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>("Acquire"); |
| 3197 | continue; |
| 3198 | } |
| 3199 | if (Predicate.isAtomicOrderingRelease()) { |
| 3200 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>("Release"); |
| 3201 | continue; |
| 3202 | } |
| 3203 | if (Predicate.isAtomicOrderingAcquireRelease()) { |
| 3204 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>( |
| 3205 | "AcquireRelease"); |
| 3206 | continue; |
| 3207 | } |
| 3208 | if (Predicate.isAtomicOrderingSequentiallyConsistent()) { |
| 3209 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>( |
| 3210 | "SequentiallyConsistent"); |
| 3211 | continue; |
| 3212 | } |
Daniel Sanders | 0c43b3a | 2017-11-30 21:05:59 +0000 | [diff] [blame] | 3213 | |
| 3214 | if (Predicate.isAtomicOrderingAcquireOrStronger()) { |
| 3215 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>( |
| 3216 | "Acquire", AtomicOrderingMMOPredicateMatcher::AO_OrStronger); |
| 3217 | continue; |
| 3218 | } |
| 3219 | if (Predicate.isAtomicOrderingWeakerThanAcquire()) { |
| 3220 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>( |
| 3221 | "Acquire", AtomicOrderingMMOPredicateMatcher::AO_WeakerThan); |
| 3222 | continue; |
| 3223 | } |
| 3224 | |
| 3225 | if (Predicate.isAtomicOrderingReleaseOrStronger()) { |
| 3226 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>( |
| 3227 | "Release", AtomicOrderingMMOPredicateMatcher::AO_OrStronger); |
| 3228 | continue; |
| 3229 | } |
| 3230 | if (Predicate.isAtomicOrderingWeakerThanRelease()) { |
| 3231 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>( |
| 3232 | "Release", AtomicOrderingMMOPredicateMatcher::AO_WeakerThan); |
| 3233 | continue; |
| 3234 | } |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 3235 | } |
| 3236 | |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 3237 | return failedImport("Src pattern child has predicate (" + |
| 3238 | explainPredicates(Src) + ")"); |
| 3239 | } |
Daniel Sanders | 3c1c4c0 | 2017-12-05 05:52:07 +0000 | [diff] [blame] | 3240 | if (SrcGIEquivOrNull && SrcGIEquivOrNull->getValueAsBit("CheckMMOIsNonAtomic")) |
| 3241 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>("NotAtomic"); |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 3242 | |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 3243 | if (Src->isLeaf()) { |
| 3244 | Init *SrcInit = Src->getLeafValue(); |
| 3245 | if (IntInit *SrcIntInit = dyn_cast<IntInit>(SrcInit)) { |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 3246 | OperandMatcher &OM = |
| 3247 | InsnMatcher.addOperand(OpIdx++, Src->getName(), TempOpIdx); |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 3248 | OM.addPredicate<LiteralIntOperandMatcher>(SrcIntInit->getValue()); |
| 3249 | } else |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 3250 | return failedImport( |
| 3251 | "Unable to deduce gMIR opcode to handle Src (which is a leaf)"); |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 3252 | } else { |
Daniel Sanders | 85ffd36 | 2017-07-06 08:12:20 +0000 | [diff] [blame] | 3253 | assert(SrcGIOrNull && |
| 3254 | "Expected to have already found an equivalent Instruction"); |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 3255 | if (SrcGIOrNull->TheDef->getName() == "G_CONSTANT" || |
| 3256 | SrcGIOrNull->TheDef->getName() == "G_FCONSTANT") { |
| 3257 | // 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] | 3258 | // here since we don't support ImmLeaf predicates yet. However, we still |
| 3259 | // need to note the hidden operand to get GIM_CheckNumOperands correct. |
| 3260 | InsnMatcher.addOperand(OpIdx++, "", TempOpIdx); |
| 3261 | return InsnMatcher; |
| 3262 | } |
| 3263 | |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 3264 | // Match the used operands (i.e. the children of the operator). |
| 3265 | for (unsigned i = 0, e = Src->getNumChildren(); i != e; ++i) { |
Daniel Sanders | 85ffd36 | 2017-07-06 08:12:20 +0000 | [diff] [blame] | 3266 | TreePatternNode *SrcChild = Src->getChild(i); |
| 3267 | |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 3268 | // SelectionDAG allows pointers to be represented with iN since it doesn't |
| 3269 | // distinguish between pointers and integers but they are different types in GlobalISel. |
| 3270 | // 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] | 3271 | bool OperandIsAPointer = SrcGIOrNull->isOperandAPointer(i); |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 3272 | |
Daniel Sanders | 28887fe | 2017-09-19 12:56:36 +0000 | [diff] [blame] | 3273 | // For G_INTRINSIC/G_INTRINSIC_W_SIDE_EFFECTS, the operand immediately |
| 3274 | // following the defs is an intrinsic ID. |
| 3275 | if ((SrcGIOrNull->TheDef->getName() == "G_INTRINSIC" || |
| 3276 | SrcGIOrNull->TheDef->getName() == "G_INTRINSIC_W_SIDE_EFFECTS") && |
| 3277 | i == 0) { |
Daniel Sanders | fe12c0f | 2017-07-11 08:57:29 +0000 | [diff] [blame] | 3278 | if (const CodeGenIntrinsic *II = Src->getIntrinsicInfo(CGP)) { |
Daniel Sanders | 85ffd36 | 2017-07-06 08:12:20 +0000 | [diff] [blame] | 3279 | OperandMatcher &OM = |
| 3280 | InsnMatcher.addOperand(OpIdx++, SrcChild->getName(), TempOpIdx); |
Daniel Sanders | fe12c0f | 2017-07-11 08:57:29 +0000 | [diff] [blame] | 3281 | OM.addPredicate<IntrinsicIDOperandMatcher>(II); |
Daniel Sanders | 85ffd36 | 2017-07-06 08:12:20 +0000 | [diff] [blame] | 3282 | continue; |
| 3283 | } |
| 3284 | |
| 3285 | return failedImport("Expected IntInit containing instrinsic ID)"); |
| 3286 | } |
| 3287 | |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 3288 | if (auto Error = |
| 3289 | importChildMatcher(Rule, InsnMatcher, SrcChild, OperandIsAPointer, |
| 3290 | OpIdx++, TempOpIdx)) |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 3291 | return std::move(Error); |
| 3292 | } |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3293 | } |
| 3294 | |
| 3295 | return InsnMatcher; |
| 3296 | } |
| 3297 | |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 3298 | Error GlobalISelEmitter::importComplexPatternOperandMatcher( |
| 3299 | OperandMatcher &OM, Record *R, unsigned &TempOpIdx) const { |
| 3300 | const auto &ComplexPattern = ComplexPatternEquivs.find(R); |
| 3301 | if (ComplexPattern == ComplexPatternEquivs.end()) |
| 3302 | return failedImport("SelectionDAG ComplexPattern (" + R->getName() + |
| 3303 | ") not mapped to GlobalISel"); |
| 3304 | |
| 3305 | OM.addPredicate<ComplexPatternOperandMatcher>(OM, *ComplexPattern->second); |
| 3306 | TempOpIdx++; |
| 3307 | return Error::success(); |
| 3308 | } |
| 3309 | |
| 3310 | Error GlobalISelEmitter::importChildMatcher(RuleMatcher &Rule, |
| 3311 | InstructionMatcher &InsnMatcher, |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 3312 | const TreePatternNode *SrcChild, |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 3313 | bool OperandIsAPointer, |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 3314 | unsigned OpIdx, |
| 3315 | unsigned &TempOpIdx) const { |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 3316 | OperandMatcher &OM = |
| 3317 | InsnMatcher.addOperand(OpIdx, SrcChild->getName(), TempOpIdx); |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 3318 | if (OM.isSameAsAnotherOperand()) |
| 3319 | return Error::success(); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3320 | |
Krzysztof Parzyszek | 779d98e | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 3321 | ArrayRef<TypeSetByHwMode> ChildTypes = SrcChild->getExtTypes(); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3322 | if (ChildTypes.size() != 1) |
| 3323 | return failedImport("Src pattern child has multiple results"); |
| 3324 | |
| 3325 | // Check MBB's before the type check since they are not a known type. |
| 3326 | if (!SrcChild->isLeaf()) { |
| 3327 | if (SrcChild->getOperator()->isSubClassOf("SDNode")) { |
| 3328 | auto &ChildSDNI = CGP.getSDNodeInfo(SrcChild->getOperator()); |
| 3329 | if (ChildSDNI.getSDClassName() == "BasicBlockSDNode") { |
| 3330 | OM.addPredicate<MBBOperandMatcher>(); |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 3331 | return Error::success(); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3332 | } |
| 3333 | } |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3334 | } |
| 3335 | |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 3336 | if (auto Error = |
| 3337 | OM.addTypeCheckPredicate(ChildTypes.front(), OperandIsAPointer)) |
| 3338 | return failedImport(toString(std::move(Error)) + " for Src operand (" + |
| 3339 | to_string(*SrcChild) + ")"); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3340 | |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 3341 | // Check for nested instructions. |
| 3342 | if (!SrcChild->isLeaf()) { |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 3343 | if (SrcChild->getOperator()->isSubClassOf("ComplexPattern")) { |
| 3344 | // When a ComplexPattern is used as an operator, it should do the same |
| 3345 | // thing as when used as a leaf. However, the children of the operator |
| 3346 | // name the sub-operands that make up the complex operand and we must |
| 3347 | // prepare to reference them in the renderer too. |
| 3348 | unsigned RendererID = TempOpIdx; |
| 3349 | if (auto Error = importComplexPatternOperandMatcher( |
| 3350 | OM, SrcChild->getOperator(), TempOpIdx)) |
| 3351 | return Error; |
| 3352 | |
| 3353 | for (unsigned i = 0, e = SrcChild->getNumChildren(); i != e; ++i) { |
| 3354 | auto *SubOperand = SrcChild->getChild(i); |
| 3355 | if (!SubOperand->getName().empty()) |
| 3356 | Rule.defineComplexSubOperand(SubOperand->getName(), |
| 3357 | SrcChild->getOperator(), RendererID, i); |
| 3358 | } |
| 3359 | |
| 3360 | return Error::success(); |
| 3361 | } |
| 3362 | |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 3363 | auto MaybeInsnOperand = OM.addPredicate<InstructionOperandMatcher>( |
| 3364 | InsnMatcher.getRuleMatcher(), SrcChild->getName()); |
| 3365 | if (!MaybeInsnOperand.hasValue()) { |
| 3366 | // This isn't strictly true. If the user were to provide exactly the same |
| 3367 | // matchers as the original operand then we could allow it. However, it's |
| 3368 | // simpler to not permit the redundant specification. |
| 3369 | return failedImport("Nested instruction cannot be the same as another operand"); |
| 3370 | } |
| 3371 | |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 3372 | // Map the node to a gMIR instruction. |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 3373 | InstructionOperandMatcher &InsnOperand = **MaybeInsnOperand; |
Daniel Sanders | 57938df | 2017-07-11 10:40:18 +0000 | [diff] [blame] | 3374 | auto InsnMatcherOrError = createAndImportSelDAGMatcher( |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 3375 | Rule, InsnOperand.getInsnMatcher(), SrcChild, TempOpIdx); |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 3376 | if (auto Error = InsnMatcherOrError.takeError()) |
| 3377 | return Error; |
| 3378 | |
| 3379 | return Error::success(); |
| 3380 | } |
| 3381 | |
Diana Picus | d1b6181 | 2017-11-03 10:30:19 +0000 | [diff] [blame] | 3382 | if (SrcChild->hasAnyPredicate()) |
| 3383 | return failedImport("Src pattern child has unsupported predicate"); |
| 3384 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3385 | // Check for constant immediates. |
| 3386 | if (auto *ChildInt = dyn_cast<IntInit>(SrcChild->getLeafValue())) { |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 3387 | OM.addPredicate<ConstantIntOperandMatcher>(ChildInt->getValue()); |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 3388 | return Error::success(); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3389 | } |
| 3390 | |
| 3391 | // Check for def's like register classes or ComplexPattern's. |
| 3392 | if (auto *ChildDefInit = dyn_cast<DefInit>(SrcChild->getLeafValue())) { |
| 3393 | auto *ChildRec = ChildDefInit->getDef(); |
| 3394 | |
| 3395 | // Check for register classes. |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 3396 | if (ChildRec->isSubClassOf("RegisterClass") || |
| 3397 | ChildRec->isSubClassOf("RegisterOperand")) { |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3398 | OM.addPredicate<RegisterBankOperandMatcher>( |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 3399 | Target.getRegisterClass(getInitValueAsRegClass(ChildDefInit))); |
Daniel Sanders | 658541f | 2017-04-22 15:53:21 +0000 | [diff] [blame] | 3400 | return Error::success(); |
| 3401 | } |
| 3402 | |
Daniel Sanders | 4d4e765 | 2017-10-09 18:14:53 +0000 | [diff] [blame] | 3403 | // Check for ValueType. |
| 3404 | if (ChildRec->isSubClassOf("ValueType")) { |
| 3405 | // We already added a type check as standard practice so this doesn't need |
| 3406 | // to do anything. |
| 3407 | return Error::success(); |
| 3408 | } |
| 3409 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3410 | // Check for ComplexPattern's. |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 3411 | if (ChildRec->isSubClassOf("ComplexPattern")) |
| 3412 | return importComplexPatternOperandMatcher(OM, ChildRec, TempOpIdx); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3413 | |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 3414 | if (ChildRec->isSubClassOf("ImmLeaf")) { |
| 3415 | return failedImport( |
| 3416 | "Src pattern child def is an unsupported tablegen class (ImmLeaf)"); |
| 3417 | } |
| 3418 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3419 | return failedImport( |
| 3420 | "Src pattern child def is an unsupported tablegen class"); |
| 3421 | } |
| 3422 | |
| 3423 | return failedImport("Src pattern child is an unsupported kind"); |
| 3424 | } |
| 3425 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3426 | Expected<action_iterator> GlobalISelEmitter::importExplicitUseRenderer( |
| 3427 | action_iterator InsertPt, RuleMatcher &Rule, BuildMIAction &DstMIBuilder, |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 3428 | TreePatternNode *DstChild) { |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 3429 | |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 3430 | const auto &SubOperand = Rule.getComplexSubOperand(DstChild->getName()); |
| 3431 | if (SubOperand.hasValue()) { |
| 3432 | DstMIBuilder.addRenderer<RenderComplexPatternOperand>( |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 3433 | *std::get<0>(*SubOperand), DstChild->getName(), |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 3434 | std::get<1>(*SubOperand), std::get<2>(*SubOperand)); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3435 | return InsertPt; |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 3436 | } |
| 3437 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3438 | if (!DstChild->isLeaf()) { |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 3439 | |
| 3440 | if (DstChild->getOperator()->isSubClassOf("SDNodeXForm")) { |
| 3441 | auto Child = DstChild->getChild(0); |
| 3442 | auto I = SDNodeXFormEquivs.find(DstChild->getOperator()); |
| 3443 | if (I != SDNodeXFormEquivs.end()) { |
| 3444 | DstMIBuilder.addRenderer<CustomRenderer>(*I->second, Child->getName()); |
| 3445 | return InsertPt; |
| 3446 | } |
| 3447 | return failedImport("SDNodeXForm " + Child->getName() + |
| 3448 | " has no custom renderer"); |
| 3449 | } |
| 3450 | |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 3451 | // We accept 'bb' here. It's an operator because BasicBlockSDNode isn't |
| 3452 | // inline, but in MI it's just another operand. |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3453 | if (DstChild->getOperator()->isSubClassOf("SDNode")) { |
| 3454 | auto &ChildSDNI = CGP.getSDNodeInfo(DstChild->getOperator()); |
| 3455 | if (ChildSDNI.getSDClassName() == "BasicBlockSDNode") { |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 3456 | DstMIBuilder.addRenderer<CopyRenderer>(DstChild->getName()); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3457 | return InsertPt; |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3458 | } |
| 3459 | } |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 3460 | |
| 3461 | // Similarly, imm is an operator in TreePatternNode's view but must be |
| 3462 | // rendered as operands. |
| 3463 | // FIXME: The target should be able to choose sign-extended when appropriate |
| 3464 | // (e.g. on Mips). |
| 3465 | if (DstChild->getOperator()->getName() == "imm") { |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 3466 | DstMIBuilder.addRenderer<CopyConstantAsImmRenderer>(DstChild->getName()); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3467 | return InsertPt; |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 3468 | } else if (DstChild->getOperator()->getName() == "fpimm") { |
| 3469 | DstMIBuilder.addRenderer<CopyFConstantAsFPImmRenderer>( |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 3470 | DstChild->getName()); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3471 | return InsertPt; |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 3472 | } |
| 3473 | |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 3474 | if (DstChild->getOperator()->isSubClassOf("Instruction")) { |
| 3475 | ArrayRef<TypeSetByHwMode> ChildTypes = DstChild->getExtTypes(); |
| 3476 | if (ChildTypes.size() != 1) |
| 3477 | return failedImport("Dst pattern child has multiple results"); |
| 3478 | |
| 3479 | Optional<LLTCodeGen> OpTyOrNone = None; |
| 3480 | if (ChildTypes.front().isMachineValueType()) |
| 3481 | OpTyOrNone = |
| 3482 | MVTToLLT(ChildTypes.front().getMachineValueType().SimpleTy); |
| 3483 | if (!OpTyOrNone) |
| 3484 | return failedImport("Dst operand has an unsupported type"); |
| 3485 | |
| 3486 | unsigned TempRegID = Rule.allocateTempRegID(); |
| 3487 | InsertPt = Rule.insertAction<MakeTempRegisterAction>( |
| 3488 | InsertPt, OpTyOrNone.getValue(), TempRegID); |
| 3489 | DstMIBuilder.addRenderer<TempRegRenderer>(TempRegID); |
| 3490 | |
| 3491 | auto InsertPtOrError = createAndImportSubInstructionRenderer( |
| 3492 | ++InsertPt, Rule, DstChild, TempRegID); |
| 3493 | if (auto Error = InsertPtOrError.takeError()) |
| 3494 | return std::move(Error); |
| 3495 | return InsertPtOrError.get(); |
| 3496 | } |
| 3497 | |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 3498 | 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] | 3499 | } |
| 3500 | |
Daniel Sanders | f499b2b | 2017-11-30 18:48:35 +0000 | [diff] [blame] | 3501 | // It could be a specific immediate in which case we should just check for |
| 3502 | // that immediate. |
| 3503 | if (const IntInit *ChildIntInit = |
| 3504 | dyn_cast<IntInit>(DstChild->getLeafValue())) { |
| 3505 | DstMIBuilder.addRenderer<ImmRenderer>(ChildIntInit->getValue()); |
| 3506 | return InsertPt; |
| 3507 | } |
| 3508 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3509 | // Otherwise, we're looking for a bog-standard RegisterClass operand. |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3510 | if (auto *ChildDefInit = dyn_cast<DefInit>(DstChild->getLeafValue())) { |
| 3511 | auto *ChildRec = ChildDefInit->getDef(); |
| 3512 | |
Krzysztof Parzyszek | 779d98e | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 3513 | ArrayRef<TypeSetByHwMode> ChildTypes = DstChild->getExtTypes(); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3514 | if (ChildTypes.size() != 1) |
| 3515 | return failedImport("Dst pattern child has multiple results"); |
| 3516 | |
Krzysztof Parzyszek | 779d98e | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 3517 | Optional<LLTCodeGen> OpTyOrNone = None; |
| 3518 | if (ChildTypes.front().isMachineValueType()) |
| 3519 | OpTyOrNone = MVTToLLT(ChildTypes.front().getMachineValueType().SimpleTy); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3520 | if (!OpTyOrNone) |
| 3521 | return failedImport("Dst operand has an unsupported type"); |
| 3522 | |
| 3523 | if (ChildRec->isSubClassOf("Register")) { |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 3524 | DstMIBuilder.addRenderer<AddRegisterRenderer>(ChildRec); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3525 | return InsertPt; |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3526 | } |
| 3527 | |
Daniel Sanders | 658541f | 2017-04-22 15:53:21 +0000 | [diff] [blame] | 3528 | if (ChildRec->isSubClassOf("RegisterClass") || |
Daniel Sanders | 4d4e765 | 2017-10-09 18:14:53 +0000 | [diff] [blame] | 3529 | ChildRec->isSubClassOf("RegisterOperand") || |
| 3530 | ChildRec->isSubClassOf("ValueType")) { |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 3531 | if (ChildRec->isSubClassOf("RegisterOperand") && |
| 3532 | !ChildRec->isValueUnset("GIZeroRegister")) { |
| 3533 | DstMIBuilder.addRenderer<CopyOrAddZeroRegRenderer>( |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 3534 | DstChild->getName(), ChildRec->getValueAsDef("GIZeroRegister")); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3535 | return InsertPt; |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 3536 | } |
| 3537 | |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 3538 | DstMIBuilder.addRenderer<CopyRenderer>(DstChild->getName()); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3539 | return InsertPt; |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3540 | } |
| 3541 | |
| 3542 | if (ChildRec->isSubClassOf("ComplexPattern")) { |
| 3543 | const auto &ComplexPattern = ComplexPatternEquivs.find(ChildRec); |
| 3544 | if (ComplexPattern == ComplexPatternEquivs.end()) |
| 3545 | return failedImport( |
| 3546 | "SelectionDAG ComplexPattern not mapped to GlobalISel"); |
| 3547 | |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 3548 | const OperandMatcher &OM = Rule.getOperandMatcher(DstChild->getName()); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3549 | DstMIBuilder.addRenderer<RenderComplexPatternOperand>( |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 3550 | *ComplexPattern->second, DstChild->getName(), |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 3551 | OM.getAllocatedTemporariesBaseID()); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3552 | return InsertPt; |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3553 | } |
| 3554 | |
| 3555 | return failedImport( |
| 3556 | "Dst pattern child def is an unsupported tablegen class"); |
| 3557 | } |
| 3558 | |
| 3559 | return failedImport("Dst pattern child is an unsupported kind"); |
| 3560 | } |
| 3561 | |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 3562 | Expected<BuildMIAction &> GlobalISelEmitter::createAndImportInstructionRenderer( |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 3563 | RuleMatcher &M, const TreePatternNode *Dst) { |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3564 | auto InsertPtOrError = createInstructionRenderer(M.actions_end(), M, Dst); |
| 3565 | if (auto Error = InsertPtOrError.takeError()) |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 3566 | return std::move(Error); |
| 3567 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3568 | action_iterator InsertPt = InsertPtOrError.get(); |
| 3569 | BuildMIAction &DstMIBuilder = *static_cast<BuildMIAction *>(InsertPt->get()); |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 3570 | |
| 3571 | importExplicitDefRenderers(DstMIBuilder); |
| 3572 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3573 | if (auto Error = importExplicitUseRenderers(InsertPt, M, DstMIBuilder, Dst) |
| 3574 | .takeError()) |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 3575 | return std::move(Error); |
| 3576 | |
| 3577 | return DstMIBuilder; |
| 3578 | } |
| 3579 | |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 3580 | Expected<action_iterator> |
| 3581 | GlobalISelEmitter::createAndImportSubInstructionRenderer( |
Daniel Sanders | 0846452 | 2018-01-29 21:09:12 +0000 | [diff] [blame] | 3582 | const action_iterator InsertPt, RuleMatcher &M, const TreePatternNode *Dst, |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 3583 | unsigned TempRegID) { |
| 3584 | auto InsertPtOrError = createInstructionRenderer(InsertPt, M, Dst); |
| 3585 | |
| 3586 | // TODO: Assert there's exactly one result. |
| 3587 | |
| 3588 | if (auto Error = InsertPtOrError.takeError()) |
| 3589 | return std::move(Error); |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 3590 | |
| 3591 | BuildMIAction &DstMIBuilder = |
| 3592 | *static_cast<BuildMIAction *>(InsertPtOrError.get()->get()); |
| 3593 | |
| 3594 | // Assign the result to TempReg. |
| 3595 | DstMIBuilder.addRenderer<TempRegRenderer>(TempRegID, true); |
| 3596 | |
Daniel Sanders | 0846452 | 2018-01-29 21:09:12 +0000 | [diff] [blame] | 3597 | InsertPtOrError = |
| 3598 | importExplicitUseRenderers(InsertPtOrError.get(), M, DstMIBuilder, Dst); |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 3599 | if (auto Error = InsertPtOrError.takeError()) |
| 3600 | return std::move(Error); |
| 3601 | |
Daniel Sanders | 0846452 | 2018-01-29 21:09:12 +0000 | [diff] [blame] | 3602 | M.insertAction<ConstrainOperandsToDefinitionAction>(InsertPt, |
| 3603 | DstMIBuilder.getInsnID()); |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 3604 | return InsertPtOrError.get(); |
| 3605 | } |
| 3606 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3607 | Expected<action_iterator> GlobalISelEmitter::createInstructionRenderer( |
| 3608 | action_iterator InsertPt, RuleMatcher &M, const TreePatternNode *Dst) { |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3609 | Record *DstOp = Dst->getOperator(); |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 3610 | if (!DstOp->isSubClassOf("Instruction")) { |
| 3611 | if (DstOp->isSubClassOf("ValueType")) |
| 3612 | return failedImport( |
| 3613 | "Pattern operator isn't an instruction (it's a ValueType)"); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3614 | return failedImport("Pattern operator isn't an instruction"); |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 3615 | } |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 3616 | CodeGenInstruction *DstI = &Target.getInstruction(DstOp); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3617 | |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 3618 | // COPY_TO_REGCLASS is just a copy with a ConstrainOperandToRegClassAction |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 3619 | // attached. Similarly for EXTRACT_SUBREG except that's a subregister copy. |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 3620 | if (DstI->TheDef->getName() == "COPY_TO_REGCLASS") |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 3621 | DstI = &Target.getInstruction(RK.getDef("COPY")); |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 3622 | else if (DstI->TheDef->getName() == "EXTRACT_SUBREG") |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 3623 | DstI = &Target.getInstruction(RK.getDef("COPY")); |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 3624 | else if (DstI->TheDef->getName() == "REG_SEQUENCE") |
| 3625 | return failedImport("Unable to emit REG_SEQUENCE"); |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 3626 | |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 3627 | return M.insertAction<BuildMIAction>(InsertPt, M.allocateOutputInsnID(), |
| 3628 | DstI); |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 3629 | } |
| 3630 | |
| 3631 | void GlobalISelEmitter::importExplicitDefRenderers( |
| 3632 | BuildMIAction &DstMIBuilder) { |
| 3633 | const CodeGenInstruction *DstI = DstMIBuilder.getCGI(); |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 3634 | for (unsigned I = 0; I < DstI->Operands.NumDefs; ++I) { |
| 3635 | const CGIOperandList::OperandInfo &DstIOperand = DstI->Operands[I]; |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 3636 | DstMIBuilder.addRenderer<CopyRenderer>(DstIOperand.Name); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3637 | } |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 3638 | } |
| 3639 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3640 | Expected<action_iterator> GlobalISelEmitter::importExplicitUseRenderers( |
| 3641 | action_iterator InsertPt, RuleMatcher &M, BuildMIAction &DstMIBuilder, |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 3642 | const llvm::TreePatternNode *Dst) { |
| 3643 | const CodeGenInstruction *DstI = DstMIBuilder.getCGI(); |
| 3644 | CodeGenInstruction *OrigDstI = &Target.getInstruction(Dst->getOperator()); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3645 | |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 3646 | // EXTRACT_SUBREG needs to use a subregister COPY. |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 3647 | if (OrigDstI->TheDef->getName() == "EXTRACT_SUBREG") { |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 3648 | if (!Dst->getChild(0)->isLeaf()) |
| 3649 | return failedImport("EXTRACT_SUBREG child #1 is not a leaf"); |
| 3650 | |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 3651 | if (DefInit *SubRegInit = |
| 3652 | dyn_cast<DefInit>(Dst->getChild(1)->getLeafValue())) { |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 3653 | Record *RCDef = getInitValueAsRegClass(Dst->getChild(0)->getLeafValue()); |
| 3654 | if (!RCDef) |
| 3655 | return failedImport("EXTRACT_SUBREG child #0 could not " |
| 3656 | "be coerced to a register class"); |
| 3657 | |
| 3658 | CodeGenRegisterClass *RC = CGRegs.getRegClass(RCDef); |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 3659 | CodeGenSubRegIndex *SubIdx = CGRegs.getSubRegIdx(SubRegInit->getDef()); |
| 3660 | |
| 3661 | const auto &SrcRCDstRCPair = |
| 3662 | RC->getMatchingSubClassWithSubRegs(CGRegs, SubIdx); |
| 3663 | if (SrcRCDstRCPair.hasValue()) { |
| 3664 | assert(SrcRCDstRCPair->second && "Couldn't find a matching subclass"); |
| 3665 | if (SrcRCDstRCPair->first != RC) |
| 3666 | return failedImport("EXTRACT_SUBREG requires an additional COPY"); |
| 3667 | } |
| 3668 | |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 3669 | DstMIBuilder.addRenderer<CopySubRegRenderer>(Dst->getChild(0)->getName(), |
| 3670 | SubIdx); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3671 | return InsertPt; |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 3672 | } |
| 3673 | |
| 3674 | return failedImport("EXTRACT_SUBREG child #1 is not a subreg index"); |
| 3675 | } |
| 3676 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3677 | // Render the explicit uses. |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 3678 | unsigned DstINumUses = OrigDstI->Operands.size() - OrigDstI->Operands.NumDefs; |
| 3679 | unsigned ExpectedDstINumUses = Dst->getNumChildren(); |
| 3680 | if (OrigDstI->TheDef->getName() == "COPY_TO_REGCLASS") { |
| 3681 | DstINumUses--; // Ignore the class constraint. |
| 3682 | ExpectedDstINumUses--; |
| 3683 | } |
| 3684 | |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 3685 | unsigned Child = 0; |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 3686 | unsigned NumDefaultOps = 0; |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 3687 | for (unsigned I = 0; I != DstINumUses; ++I) { |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 3688 | const CGIOperandList::OperandInfo &DstIOperand = |
| 3689 | DstI->Operands[DstI->Operands.NumDefs + I]; |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 3690 | |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 3691 | // If the operand has default values, introduce them now. |
| 3692 | // FIXME: Until we have a decent test case that dictates we should do |
| 3693 | // otherwise, we're going to assume that operands with default values cannot |
| 3694 | // be specified in the patterns. Therefore, adding them will not cause us to |
| 3695 | // end up with too many rendered operands. |
| 3696 | if (DstIOperand.Rec->isSubClassOf("OperandWithDefaultOps")) { |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 3697 | DagInit *DefaultOps = DstIOperand.Rec->getValueAsDag("DefaultOps"); |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 3698 | if (auto Error = importDefaultOperandRenderers(DstMIBuilder, DefaultOps)) |
| 3699 | return std::move(Error); |
| 3700 | ++NumDefaultOps; |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 3701 | continue; |
| 3702 | } |
| 3703 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3704 | auto InsertPtOrError = importExplicitUseRenderer(InsertPt, M, DstMIBuilder, |
| 3705 | Dst->getChild(Child)); |
| 3706 | if (auto Error = InsertPtOrError.takeError()) |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3707 | return std::move(Error); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3708 | InsertPt = InsertPtOrError.get(); |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 3709 | ++Child; |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3710 | } |
| 3711 | |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 3712 | if (NumDefaultOps + ExpectedDstINumUses != DstINumUses) |
Diana Picus | eb2057c | 2017-05-17 09:25:08 +0000 | [diff] [blame] | 3713 | return failedImport("Expected " + llvm::to_string(DstINumUses) + |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 3714 | " used operands but found " + |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 3715 | llvm::to_string(ExpectedDstINumUses) + |
Diana Picus | eb2057c | 2017-05-17 09:25:08 +0000 | [diff] [blame] | 3716 | " explicit ones and " + llvm::to_string(NumDefaultOps) + |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 3717 | " default ones"); |
| 3718 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3719 | return InsertPt; |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3720 | } |
| 3721 | |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 3722 | Error GlobalISelEmitter::importDefaultOperandRenderers( |
| 3723 | BuildMIAction &DstMIBuilder, DagInit *DefaultOps) const { |
Craig Topper | 481ff70 | 2017-05-29 21:49:34 +0000 | [diff] [blame] | 3724 | for (const auto *DefaultOp : DefaultOps->getArgs()) { |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 3725 | // Look through ValueType operators. |
| 3726 | if (const DagInit *DefaultDagOp = dyn_cast<DagInit>(DefaultOp)) { |
| 3727 | if (const DefInit *DefaultDagOperator = |
| 3728 | dyn_cast<DefInit>(DefaultDagOp->getOperator())) { |
| 3729 | if (DefaultDagOperator->getDef()->isSubClassOf("ValueType")) |
| 3730 | DefaultOp = DefaultDagOp->getArg(0); |
| 3731 | } |
| 3732 | } |
| 3733 | |
| 3734 | if (const DefInit *DefaultDefOp = dyn_cast<DefInit>(DefaultOp)) { |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 3735 | DstMIBuilder.addRenderer<AddRegisterRenderer>(DefaultDefOp->getDef()); |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 3736 | continue; |
| 3737 | } |
| 3738 | |
| 3739 | if (const IntInit *DefaultIntOp = dyn_cast<IntInit>(DefaultOp)) { |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 3740 | DstMIBuilder.addRenderer<ImmRenderer>(DefaultIntOp->getValue()); |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 3741 | continue; |
| 3742 | } |
| 3743 | |
| 3744 | return failedImport("Could not add default op"); |
| 3745 | } |
| 3746 | |
| 3747 | return Error::success(); |
| 3748 | } |
| 3749 | |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 3750 | Error GlobalISelEmitter::importImplicitDefRenderers( |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3751 | BuildMIAction &DstMIBuilder, |
| 3752 | const std::vector<Record *> &ImplicitDefs) const { |
| 3753 | if (!ImplicitDefs.empty()) |
| 3754 | return failedImport("Pattern defines a physical register"); |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 3755 | return Error::success(); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3756 | } |
| 3757 | |
| 3758 | Expected<RuleMatcher> GlobalISelEmitter::runOnPattern(const PatternToMatch &P) { |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 3759 | // Keep track of the matchers and actions to emit. |
Aditya Nandakumar | b63e763 | 2018-02-16 22:37:15 +0000 | [diff] [blame] | 3760 | int Score = P.getPatternComplexity(CGP); |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 3761 | RuleMatcher M(P.getSrcRecord()->getLoc()); |
Aditya Nandakumar | b63e763 | 2018-02-16 22:37:15 +0000 | [diff] [blame] | 3762 | RuleMatcherScores[M.getRuleID()] = Score; |
Daniel Sanders | 6ea17ed | 2017-10-31 18:07:03 +0000 | [diff] [blame] | 3763 | M.addAction<DebugCommentAction>(llvm::to_string(*P.getSrcPattern()) + |
| 3764 | " => " + |
| 3765 | llvm::to_string(*P.getDstPattern())); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 3766 | |
Krzysztof Parzyszek | 779d98e | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 3767 | if (auto Error = importRulePredicates(M, P.getPredicates())) |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3768 | return std::move(Error); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 3769 | |
| 3770 | // Next, analyze the pattern operators. |
| 3771 | TreePatternNode *Src = P.getSrcPattern(); |
| 3772 | TreePatternNode *Dst = P.getDstPattern(); |
| 3773 | |
| 3774 | // 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] | 3775 | if (auto Err = isTrivialOperatorNode(Dst)) |
| 3776 | return failedImport("Dst pattern root isn't a trivial operator (" + |
| 3777 | toString(std::move(Err)) + ")"); |
| 3778 | if (auto Err = isTrivialOperatorNode(Src)) |
| 3779 | return failedImport("Src pattern root isn't a trivial operator (" + |
| 3780 | toString(std::move(Err)) + ")"); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 3781 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 3782 | // The different predicates and matchers created during |
| 3783 | // addInstructionMatcher use the RuleMatcher M to set up their |
| 3784 | // instruction ID (InsnVarID) that are going to be used when |
| 3785 | // M is going to be emitted. |
| 3786 | // However, the code doing the emission still relies on the IDs |
| 3787 | // returned during that process by the RuleMatcher when issuing |
| 3788 | // the recordInsn opcodes. |
| 3789 | // Because of that: |
| 3790 | // 1. The order in which we created the predicates |
| 3791 | // and such must be the same as the order in which we emit them, |
| 3792 | // and |
| 3793 | // 2. We need to reset the generation of the IDs in M somewhere between |
| 3794 | // addInstructionMatcher and emit |
| 3795 | // |
| 3796 | // FIXME: Long term, we don't want to have to rely on this implicit |
| 3797 | // naming being the same. One possible solution would be to have |
| 3798 | // explicit operator for operation capture and reference those. |
| 3799 | // The plus side is that it would expose opportunities to share |
| 3800 | // the capture accross rules. The downside is that it would |
| 3801 | // introduce a dependency between predicates (captures must happen |
| 3802 | // before their first use.) |
Daniel Sanders | edd0784 | 2017-08-17 09:26:14 +0000 | [diff] [blame] | 3803 | InstructionMatcher &InsnMatcherTemp = M.addInstructionMatcher(Src->getName()); |
| 3804 | unsigned TempOpIdx = 0; |
| 3805 | auto InsnMatcherOrError = |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 3806 | createAndImportSelDAGMatcher(M, InsnMatcherTemp, Src, TempOpIdx); |
Daniel Sanders | edd0784 | 2017-08-17 09:26:14 +0000 | [diff] [blame] | 3807 | if (auto Error = InsnMatcherOrError.takeError()) |
| 3808 | return std::move(Error); |
| 3809 | InstructionMatcher &InsnMatcher = InsnMatcherOrError.get(); |
| 3810 | |
| 3811 | if (Dst->isLeaf()) { |
| 3812 | Record *RCDef = getInitValueAsRegClass(Dst->getLeafValue()); |
| 3813 | |
| 3814 | const CodeGenRegisterClass &RC = Target.getRegisterClass(RCDef); |
| 3815 | if (RCDef) { |
| 3816 | // We need to replace the def and all its uses with the specified |
| 3817 | // operand. However, we must also insert COPY's wherever needed. |
| 3818 | // For now, emit a copy and let the register allocator clean up. |
| 3819 | auto &DstI = Target.getInstruction(RK.getDef("COPY")); |
| 3820 | const auto &DstIOperand = DstI.Operands[0]; |
| 3821 | |
| 3822 | OperandMatcher &OM0 = InsnMatcher.getOperand(0); |
| 3823 | OM0.setSymbolicName(DstIOperand.Name); |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 3824 | M.defineOperand(OM0.getSymbolicName(), OM0); |
Daniel Sanders | edd0784 | 2017-08-17 09:26:14 +0000 | [diff] [blame] | 3825 | OM0.addPredicate<RegisterBankOperandMatcher>(RC); |
| 3826 | |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 3827 | auto &DstMIBuilder = |
| 3828 | M.addAction<BuildMIAction>(M.allocateOutputInsnID(), &DstI); |
| 3829 | DstMIBuilder.addRenderer<CopyRenderer>(DstIOperand.Name); |
| 3830 | DstMIBuilder.addRenderer<CopyRenderer>(Dst->getName()); |
Daniel Sanders | edd0784 | 2017-08-17 09:26:14 +0000 | [diff] [blame] | 3831 | M.addAction<ConstrainOperandToRegClassAction>(0, 0, RC); |
| 3832 | |
| 3833 | // We're done with this pattern! It's eligible for GISel emission; return |
| 3834 | // it. |
| 3835 | ++NumPatternImported; |
| 3836 | return std::move(M); |
| 3837 | } |
| 3838 | |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 3839 | return failedImport("Dst pattern root isn't a known leaf"); |
Daniel Sanders | edd0784 | 2017-08-17 09:26:14 +0000 | [diff] [blame] | 3840 | } |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 3841 | |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 3842 | // Start with the defined operands (i.e., the results of the root operator). |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 3843 | Record *DstOp = Dst->getOperator(); |
| 3844 | if (!DstOp->isSubClassOf("Instruction")) |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 3845 | return failedImport("Pattern operator isn't an instruction"); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 3846 | |
| 3847 | auto &DstI = Target.getInstruction(DstOp); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 3848 | if (DstI.Operands.NumDefs != Src->getExtTypes().size()) |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 3849 | return failedImport("Src pattern results and dst MI defs are different (" + |
| 3850 | to_string(Src->getExtTypes().size()) + " def(s) vs " + |
| 3851 | to_string(DstI.Operands.NumDefs) + " def(s))"); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 3852 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3853 | // The root of the match also has constraints on the register bank so that it |
| 3854 | // matches the result instruction. |
| 3855 | unsigned OpIdx = 0; |
Krzysztof Parzyszek | 779d98e | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 3856 | for (const TypeSetByHwMode &VTy : Src->getExtTypes()) { |
| 3857 | (void)VTy; |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3858 | |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 3859 | const auto &DstIOperand = DstI.Operands[OpIdx]; |
| 3860 | Record *DstIOpRec = DstIOperand.Rec; |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 3861 | if (DstI.TheDef->getName() == "COPY_TO_REGCLASS") { |
| 3862 | DstIOpRec = getInitValueAsRegClass(Dst->getChild(1)->getLeafValue()); |
| 3863 | |
| 3864 | if (DstIOpRec == nullptr) |
| 3865 | return failedImport( |
| 3866 | "COPY_TO_REGCLASS operand #1 isn't a register class"); |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 3867 | } else if (DstI.TheDef->getName() == "EXTRACT_SUBREG") { |
| 3868 | if (!Dst->getChild(0)->isLeaf()) |
| 3869 | return failedImport("EXTRACT_SUBREG operand #0 isn't a leaf"); |
| 3870 | |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 3871 | // We can assume that a subregister is in the same bank as it's super |
| 3872 | // register. |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 3873 | DstIOpRec = getInitValueAsRegClass(Dst->getChild(0)->getLeafValue()); |
| 3874 | |
| 3875 | if (DstIOpRec == nullptr) |
| 3876 | return failedImport( |
| 3877 | "EXTRACT_SUBREG operand #0 isn't a register class"); |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 3878 | } else if (DstIOpRec->isSubClassOf("RegisterOperand")) |
Daniel Sanders | 658541f | 2017-04-22 15:53:21 +0000 | [diff] [blame] | 3879 | DstIOpRec = DstIOpRec->getValueAsDef("RegClass"); |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 3880 | else if (!DstIOpRec->isSubClassOf("RegisterClass")) |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 3881 | return failedImport("Dst MI def isn't a register class" + |
| 3882 | to_string(*Dst)); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 3883 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3884 | OperandMatcher &OM = InsnMatcher.getOperand(OpIdx); |
| 3885 | OM.setSymbolicName(DstIOperand.Name); |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 3886 | M.defineOperand(OM.getSymbolicName(), OM); |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 3887 | OM.addPredicate<RegisterBankOperandMatcher>( |
| 3888 | Target.getRegisterClass(DstIOpRec)); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 3889 | ++OpIdx; |
| 3890 | } |
| 3891 | |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 3892 | auto DstMIBuilderOrError = createAndImportInstructionRenderer(M, Dst); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3893 | if (auto Error = DstMIBuilderOrError.takeError()) |
| 3894 | return std::move(Error); |
| 3895 | BuildMIAction &DstMIBuilder = DstMIBuilderOrError.get(); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 3896 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3897 | // Render the implicit defs. |
| 3898 | // These are only added to the root of the result. |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 3899 | if (auto Error = importImplicitDefRenderers(DstMIBuilder, P.getDstRegs())) |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3900 | return std::move(Error); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 3901 | |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 3902 | DstMIBuilder.chooseInsnToMutate(M); |
| 3903 | |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 3904 | // Constrain the registers to classes. This is normally derived from the |
| 3905 | // emitted instruction but a few instructions require special handling. |
| 3906 | if (DstI.TheDef->getName() == "COPY_TO_REGCLASS") { |
| 3907 | // COPY_TO_REGCLASS does not provide operand constraints itself but the |
| 3908 | // result is constrained to the class given by the second child. |
| 3909 | Record *DstIOpRec = |
| 3910 | getInitValueAsRegClass(Dst->getChild(1)->getLeafValue()); |
| 3911 | |
| 3912 | if (DstIOpRec == nullptr) |
| 3913 | return failedImport("COPY_TO_REGCLASS operand #1 isn't a register class"); |
| 3914 | |
| 3915 | M.addAction<ConstrainOperandToRegClassAction>( |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 3916 | 0, 0, Target.getRegisterClass(DstIOpRec)); |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 3917 | |
| 3918 | // We're done with this pattern! It's eligible for GISel emission; return |
| 3919 | // it. |
| 3920 | ++NumPatternImported; |
| 3921 | return std::move(M); |
| 3922 | } |
| 3923 | |
| 3924 | if (DstI.TheDef->getName() == "EXTRACT_SUBREG") { |
| 3925 | // EXTRACT_SUBREG selects into a subregister COPY but unlike most |
| 3926 | // instructions, the result register class is controlled by the |
| 3927 | // subregisters of the operand. As a result, we must constrain the result |
| 3928 | // class rather than check that it's already the right one. |
| 3929 | if (!Dst->getChild(0)->isLeaf()) |
| 3930 | return failedImport("EXTRACT_SUBREG child #1 is not a leaf"); |
| 3931 | |
Daniel Sanders | 320390b | 2017-06-28 15:16:03 +0000 | [diff] [blame] | 3932 | DefInit *SubRegInit = dyn_cast<DefInit>(Dst->getChild(1)->getLeafValue()); |
| 3933 | if (!SubRegInit) |
| 3934 | return failedImport("EXTRACT_SUBREG child #1 is not a subreg index"); |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 3935 | |
Daniel Sanders | 320390b | 2017-06-28 15:16:03 +0000 | [diff] [blame] | 3936 | // Constrain the result to the same register bank as the operand. |
| 3937 | Record *DstIOpRec = |
| 3938 | getInitValueAsRegClass(Dst->getChild(0)->getLeafValue()); |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 3939 | |
Daniel Sanders | 320390b | 2017-06-28 15:16:03 +0000 | [diff] [blame] | 3940 | if (DstIOpRec == nullptr) |
| 3941 | return failedImport("EXTRACT_SUBREG operand #1 isn't a register class"); |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 3942 | |
Daniel Sanders | 320390b | 2017-06-28 15:16:03 +0000 | [diff] [blame] | 3943 | CodeGenSubRegIndex *SubIdx = CGRegs.getSubRegIdx(SubRegInit->getDef()); |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 3944 | CodeGenRegisterClass *SrcRC = CGRegs.getRegClass(DstIOpRec); |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 3945 | |
Daniel Sanders | 320390b | 2017-06-28 15:16:03 +0000 | [diff] [blame] | 3946 | // It would be nice to leave this constraint implicit but we're required |
| 3947 | // to pick a register class so constrain the result to a register class |
| 3948 | // that can hold the correct MVT. |
| 3949 | // |
| 3950 | // FIXME: This may introduce an extra copy if the chosen class doesn't |
| 3951 | // actually contain the subregisters. |
| 3952 | assert(Src->getExtTypes().size() == 1 && |
| 3953 | "Expected Src of EXTRACT_SUBREG to have one result type"); |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 3954 | |
Daniel Sanders | 320390b | 2017-06-28 15:16:03 +0000 | [diff] [blame] | 3955 | const auto &SrcRCDstRCPair = |
| 3956 | SrcRC->getMatchingSubClassWithSubRegs(CGRegs, SubIdx); |
| 3957 | assert(SrcRCDstRCPair->second && "Couldn't find a matching subclass"); |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 3958 | M.addAction<ConstrainOperandToRegClassAction>(0, 0, *SrcRCDstRCPair->second); |
| 3959 | M.addAction<ConstrainOperandToRegClassAction>(0, 1, *SrcRCDstRCPair->first); |
| 3960 | |
| 3961 | // We're done with this pattern! It's eligible for GISel emission; return |
| 3962 | // it. |
| 3963 | ++NumPatternImported; |
| 3964 | return std::move(M); |
| 3965 | } |
| 3966 | |
| 3967 | M.addAction<ConstrainOperandsToDefinitionAction>(0); |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 3968 | |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 3969 | // 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] | 3970 | ++NumPatternImported; |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 3971 | return std::move(M); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 3972 | } |
| 3973 | |
Daniel Sanders | 649c585 | 2017-10-13 20:42:18 +0000 | [diff] [blame] | 3974 | // Emit imm predicate table and an enum to reference them with. |
| 3975 | // The 'Predicate_' part of the name is redundant but eliminating it is more |
| 3976 | // trouble than it's worth. |
| 3977 | void GlobalISelEmitter::emitImmPredicates( |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 3978 | raw_ostream &OS, StringRef TypeIdentifier, StringRef Type, |
| 3979 | std::function<bool(const Record *R)> Filter) { |
Daniel Sanders | 649c585 | 2017-10-13 20:42:18 +0000 | [diff] [blame] | 3980 | std::vector<const Record *> MatchedRecords; |
| 3981 | const auto &Defs = RK.getAllDerivedDefinitions("PatFrag"); |
| 3982 | std::copy_if(Defs.begin(), Defs.end(), std::back_inserter(MatchedRecords), |
| 3983 | [&](Record *Record) { |
| 3984 | return !Record->getValueAsString("ImmediateCode").empty() && |
| 3985 | Filter(Record); |
| 3986 | }); |
| 3987 | |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 3988 | if (!MatchedRecords.empty()) { |
| 3989 | OS << "// PatFrag predicates.\n" |
| 3990 | << "enum {\n"; |
Daniel Sanders | 2fed4ff | 2017-10-13 21:51:20 +0000 | [diff] [blame] | 3991 | std::string EnumeratorSeparator = |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 3992 | (" = GIPFP_" + TypeIdentifier + "_Invalid + 1,\n").str(); |
| 3993 | for (const auto *Record : MatchedRecords) { |
| 3994 | OS << " GIPFP_" << TypeIdentifier << "_Predicate_" << Record->getName() |
| 3995 | << EnumeratorSeparator; |
| 3996 | EnumeratorSeparator = ",\n"; |
| 3997 | } |
| 3998 | OS << "};\n"; |
Daniel Sanders | 649c585 | 2017-10-13 20:42:18 +0000 | [diff] [blame] | 3999 | } |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 4000 | |
Daniel Sanders | 32de8bb | 2017-12-20 14:41:51 +0000 | [diff] [blame] | 4001 | OS << "bool " << Target.getName() << "InstructionSelector::testImmPredicate_" |
Aaron Ballman | 82e17f5 | 2017-12-20 20:09:30 +0000 | [diff] [blame] | 4002 | << TypeIdentifier << "(unsigned PredicateID, " << Type |
| 4003 | << " Imm) const {\n"; |
| 4004 | if (!MatchedRecords.empty()) |
| 4005 | OS << " switch (PredicateID) {\n"; |
Daniel Sanders | 32de8bb | 2017-12-20 14:41:51 +0000 | [diff] [blame] | 4006 | for (const auto *Record : MatchedRecords) { |
| 4007 | OS << " case GIPFP_" << TypeIdentifier << "_Predicate_" |
| 4008 | << Record->getName() << ": {\n" |
| 4009 | << " " << Record->getValueAsString("ImmediateCode") << "\n" |
| 4010 | << " llvm_unreachable(\"ImmediateCode should have returned\");\n" |
| 4011 | << " return false;\n" |
| 4012 | << " }\n"; |
| 4013 | } |
Aaron Ballman | 82e17f5 | 2017-12-20 20:09:30 +0000 | [diff] [blame] | 4014 | if (!MatchedRecords.empty()) |
| 4015 | OS << " }\n"; |
| 4016 | OS << " llvm_unreachable(\"Unknown predicate\");\n" |
Daniel Sanders | 32de8bb | 2017-12-20 14:41:51 +0000 | [diff] [blame] | 4017 | << " return false;\n" |
| 4018 | << "}\n"; |
Daniel Sanders | 649c585 | 2017-10-13 20:42:18 +0000 | [diff] [blame] | 4019 | } |
| 4020 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4021 | template <class GroupT> |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 4022 | std::vector<Matcher *> GlobalISelEmitter::optimizeRules( |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 4023 | ArrayRef<Matcher *> Rules, |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4024 | std::vector<std::unique_ptr<Matcher>> &MatcherStorage) { |
| 4025 | |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 4026 | std::vector<Matcher *> OptRules; |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4027 | std::unique_ptr<GroupT> CurrentGroup = make_unique<GroupT>(); |
| 4028 | assert(CurrentGroup->empty() && "Newly created group isn't empty!"); |
| 4029 | unsigned NumGroups = 0; |
| 4030 | |
| 4031 | auto ProcessCurrentGroup = [&]() { |
| 4032 | if (CurrentGroup->empty()) |
| 4033 | // An empty group is good to be reused: |
| 4034 | return; |
| 4035 | |
| 4036 | // If the group isn't large enough to provide any benefit, move all the |
| 4037 | // added rules out of it and make sure to re-create the group to properly |
| 4038 | // re-initialize it: |
| 4039 | if (CurrentGroup->size() < 2) |
| 4040 | for (Matcher *M : CurrentGroup->matchers()) |
| 4041 | OptRules.push_back(M); |
| 4042 | else { |
| 4043 | CurrentGroup->finalize(); |
Roman Tereshin | 8bdf7be | 2018-05-21 22:21:24 +0000 | [diff] [blame] | 4044 | OptRules.push_back(CurrentGroup.get()); |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4045 | MatcherStorage.emplace_back(std::move(CurrentGroup)); |
| 4046 | ++NumGroups; |
Roman Tereshin | 8bdf7be | 2018-05-21 22:21:24 +0000 | [diff] [blame] | 4047 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4048 | CurrentGroup = make_unique<GroupT>(); |
| 4049 | }; |
| 4050 | for (Matcher *Rule : Rules) { |
| 4051 | // Greedily add as many matchers as possible to the current group: |
| 4052 | if (CurrentGroup->addMatcher(*Rule)) |
| 4053 | continue; |
| 4054 | |
| 4055 | ProcessCurrentGroup(); |
| 4056 | assert(CurrentGroup->empty() && "A group wasn't properly re-initialized"); |
| 4057 | |
| 4058 | // Try to add the pending matcher to a newly created empty group: |
| 4059 | if (!CurrentGroup->addMatcher(*Rule)) |
| 4060 | // If we couldn't add the matcher to an empty group, that group type |
| 4061 | // doesn't support that kind of matchers at all, so just skip it: |
| 4062 | OptRules.push_back(Rule); |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 4063 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4064 | ProcessCurrentGroup(); |
| 4065 | |
Nicola Zaghen | 03d0b91 | 2018-05-23 15:09:29 +0000 | [diff] [blame^] | 4066 | LLVM_DEBUG(dbgs() << "NumGroups: " << NumGroups << "\n"); |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4067 | assert(CurrentGroup->empty() && "The last group wasn't properly processed"); |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 4068 | return OptRules; |
| 4069 | } |
| 4070 | |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 4071 | MatchTable |
| 4072 | GlobalISelEmitter::buildMatchTable(MutableArrayRef<RuleMatcher> Rules, |
Roman Tereshin | beb3931 | 2018-05-02 20:15:11 +0000 | [diff] [blame] | 4073 | bool Optimize, bool WithCoverage) { |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 4074 | std::vector<Matcher *> InputRules; |
| 4075 | for (Matcher &Rule : Rules) |
| 4076 | InputRules.push_back(&Rule); |
| 4077 | |
| 4078 | if (!Optimize) |
Roman Tereshin | beb3931 | 2018-05-02 20:15:11 +0000 | [diff] [blame] | 4079 | return MatchTable::buildTable(InputRules, WithCoverage); |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 4080 | |
Roman Tereshin | 7701360 | 2018-05-22 16:54:27 +0000 | [diff] [blame] | 4081 | unsigned CurrentOrdering = 0; |
| 4082 | StringMap<unsigned> OpcodeOrder; |
| 4083 | for (RuleMatcher &Rule : Rules) { |
| 4084 | const StringRef Opcode = Rule.getOpcode(); |
| 4085 | assert(!Opcode.empty() && "Didn't expect an undefined opcode"); |
| 4086 | if (OpcodeOrder.count(Opcode) == 0) |
| 4087 | OpcodeOrder[Opcode] = CurrentOrdering++; |
| 4088 | } |
| 4089 | |
| 4090 | std::stable_sort(InputRules.begin(), InputRules.end(), |
| 4091 | [&OpcodeOrder](const Matcher *A, const Matcher *B) { |
| 4092 | auto *L = static_cast<const RuleMatcher *>(A); |
| 4093 | auto *R = static_cast<const RuleMatcher *>(B); |
| 4094 | return std::make_tuple(OpcodeOrder[L->getOpcode()], |
| 4095 | L->getNumOperands()) < |
| 4096 | std::make_tuple(OpcodeOrder[R->getOpcode()], |
| 4097 | R->getNumOperands()); |
| 4098 | }); |
| 4099 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4100 | for (Matcher *Rule : InputRules) |
| 4101 | Rule->optimize(); |
| 4102 | |
| 4103 | std::vector<std::unique_ptr<Matcher>> MatcherStorage; |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 4104 | std::vector<Matcher *> OptRules = |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4105 | optimizeRules<GroupMatcher>(InputRules, MatcherStorage); |
| 4106 | |
| 4107 | for (Matcher *Rule : OptRules) |
| 4108 | Rule->optimize(); |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 4109 | |
Roman Tereshin | 0ee082f | 2018-05-22 19:37:59 +0000 | [diff] [blame] | 4110 | OptRules = optimizeRules<SwitchMatcher>(OptRules, MatcherStorage); |
| 4111 | |
Roman Tereshin | beb3931 | 2018-05-02 20:15:11 +0000 | [diff] [blame] | 4112 | return MatchTable::buildTable(OptRules, WithCoverage); |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 4113 | } |
| 4114 | |
Roman Tereshin | fedae33 | 2018-05-23 02:04:19 +0000 | [diff] [blame] | 4115 | void GroupMatcher::optimize() { |
| 4116 | GlobalISelEmitter::optimizeRules<GroupMatcher>(Matchers, MatcherStorage) |
| 4117 | .swap(Matchers); |
| 4118 | } |
| 4119 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4120 | void GlobalISelEmitter::run(raw_ostream &OS) { |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 4121 | if (!UseCoverageFile.empty()) { |
| 4122 | RuleCoverage = CodeGenCoverage(); |
| 4123 | auto RuleCoverageBufOrErr = MemoryBuffer::getFile(UseCoverageFile); |
| 4124 | if (!RuleCoverageBufOrErr) { |
| 4125 | PrintWarning(SMLoc(), "Missing rule coverage data"); |
| 4126 | RuleCoverage = None; |
| 4127 | } else { |
| 4128 | if (!RuleCoverage->parse(*RuleCoverageBufOrErr.get(), Target.getName())) { |
| 4129 | PrintWarning(SMLoc(), "Ignoring invalid or missing rule coverage data"); |
| 4130 | RuleCoverage = None; |
| 4131 | } |
| 4132 | } |
| 4133 | } |
| 4134 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4135 | // Track the run-time opcode values |
| 4136 | gatherOpcodeValues(); |
| 4137 | // Track the run-time LLT ID values |
| 4138 | gatherTypeIDValues(); |
| 4139 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4140 | // Track the GINodeEquiv definitions. |
| 4141 | gatherNodeEquivs(); |
| 4142 | |
| 4143 | emitSourceFileHeader(("Global Instruction Selector for the " + |
| 4144 | Target.getName() + " target").str(), OS); |
Daniel Sanders | b41ce2b | 2017-02-20 14:31:27 +0000 | [diff] [blame] | 4145 | std::vector<RuleMatcher> Rules; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4146 | // Look through the SelectionDAG patterns we found, possibly emitting some. |
| 4147 | for (const PatternToMatch &Pat : CGP.ptms()) { |
| 4148 | ++NumPatternTotal; |
Daniel Sanders | 7e52367 | 2017-11-11 03:23:44 +0000 | [diff] [blame] | 4149 | |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 4150 | auto MatcherOrErr = runOnPattern(Pat); |
| 4151 | |
| 4152 | // The pattern analysis can fail, indicating an unsupported pattern. |
| 4153 | // Report that if we've been asked to do so. |
| 4154 | if (auto Err = MatcherOrErr.takeError()) { |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4155 | if (WarnOnSkippedPatterns) { |
| 4156 | PrintWarning(Pat.getSrcRecord()->getLoc(), |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 4157 | "Skipped pattern: " + toString(std::move(Err))); |
| 4158 | } else { |
| 4159 | consumeError(std::move(Err)); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4160 | } |
Daniel Sanders | b41ce2b | 2017-02-20 14:31:27 +0000 | [diff] [blame] | 4161 | ++NumPatternImportsSkipped; |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 4162 | continue; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4163 | } |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 4164 | |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 4165 | if (RuleCoverage) { |
| 4166 | if (RuleCoverage->isCovered(MatcherOrErr->getRuleID())) |
| 4167 | ++NumPatternsTested; |
| 4168 | else |
| 4169 | PrintWarning(Pat.getSrcRecord()->getLoc(), |
| 4170 | "Pattern is not covered by a test"); |
| 4171 | } |
Daniel Sanders | b41ce2b | 2017-02-20 14:31:27 +0000 | [diff] [blame] | 4172 | Rules.push_back(std::move(MatcherOrErr.get())); |
| 4173 | } |
| 4174 | |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 4175 | // Comparison function to order records by name. |
| 4176 | auto orderByName = [](const Record *A, const Record *B) { |
| 4177 | return A->getName() < B->getName(); |
| 4178 | }; |
| 4179 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 4180 | std::vector<Record *> ComplexPredicates = |
| 4181 | RK.getAllDerivedDefinitions("GIComplexOperandMatcher"); |
Mandeep Singh Grang | 1b0e2f2 | 2018-04-06 20:18:05 +0000 | [diff] [blame] | 4182 | llvm::sort(ComplexPredicates.begin(), ComplexPredicates.end(), orderByName); |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 4183 | |
| 4184 | std::vector<Record *> CustomRendererFns = |
| 4185 | RK.getAllDerivedDefinitions("GICustomOperandRenderer"); |
Mandeep Singh Grang | 1b0e2f2 | 2018-04-06 20:18:05 +0000 | [diff] [blame] | 4186 | llvm::sort(CustomRendererFns.begin(), CustomRendererFns.end(), orderByName); |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 4187 | |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 4188 | unsigned MaxTemporaries = 0; |
| 4189 | for (const auto &Rule : Rules) |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 4190 | MaxTemporaries = std::max(MaxTemporaries, Rule.countRendererFns()); |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 4191 | |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 4192 | OS << "#ifdef GET_GLOBALISEL_PREDICATE_BITSET\n" |
| 4193 | << "const unsigned MAX_SUBTARGET_PREDICATES = " << SubtargetFeatures.size() |
| 4194 | << ";\n" |
| 4195 | << "using PredicateBitset = " |
| 4196 | "llvm::PredicateBitsetImpl<MAX_SUBTARGET_PREDICATES>;\n" |
| 4197 | << "#endif // ifdef GET_GLOBALISEL_PREDICATE_BITSET\n\n"; |
| 4198 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 4199 | OS << "#ifdef GET_GLOBALISEL_TEMPORARIES_DECL\n" |
| 4200 | << " mutable MatcherState State;\n" |
| 4201 | << " typedef " |
Daniel Sanders | 1e4569f | 2017-10-20 20:55:29 +0000 | [diff] [blame] | 4202 | "ComplexRendererFns(" |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 4203 | << Target.getName() |
| 4204 | << "InstructionSelector::*ComplexMatcherMemFn)(MachineOperand &) const;\n" |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 4205 | |
| 4206 | << " typedef void(" << Target.getName() |
| 4207 | << "InstructionSelector::*CustomRendererFn)(MachineInstrBuilder &, const " |
| 4208 | "MachineInstr&) " |
| 4209 | "const;\n" |
| 4210 | << " const ISelInfoTy<PredicateBitset, ComplexMatcherMemFn, " |
| 4211 | "CustomRendererFn> " |
| 4212 | "ISelInfo;\n"; |
| 4213 | OS << " static " << Target.getName() |
Daniel Sanders | ea8711b | 2017-10-16 03:36:29 +0000 | [diff] [blame] | 4214 | << "InstructionSelector::ComplexMatcherMemFn ComplexPredicateFns[];\n" |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 4215 | << " static " << Target.getName() |
| 4216 | << "InstructionSelector::CustomRendererFn CustomRenderers[];\n" |
Roman Tereshin | 2df4c22 | 2018-05-02 20:07:15 +0000 | [diff] [blame] | 4217 | << " bool testImmPredicate_I64(unsigned PredicateID, int64_t Imm) const " |
Daniel Sanders | 32de8bb | 2017-12-20 14:41:51 +0000 | [diff] [blame] | 4218 | "override;\n" |
Roman Tereshin | 2df4c22 | 2018-05-02 20:07:15 +0000 | [diff] [blame] | 4219 | << " bool testImmPredicate_APInt(unsigned PredicateID, const APInt &Imm) " |
Daniel Sanders | 32de8bb | 2017-12-20 14:41:51 +0000 | [diff] [blame] | 4220 | "const override;\n" |
Roman Tereshin | 2df4c22 | 2018-05-02 20:07:15 +0000 | [diff] [blame] | 4221 | << " bool testImmPredicate_APFloat(unsigned PredicateID, const APFloat " |
Daniel Sanders | 32de8bb | 2017-12-20 14:41:51 +0000 | [diff] [blame] | 4222 | "&Imm) const override;\n" |
Roman Tereshin | 2df4c22 | 2018-05-02 20:07:15 +0000 | [diff] [blame] | 4223 | << " const int64_t *getMatchTable() const override;\n" |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 4224 | << "#endif // ifdef GET_GLOBALISEL_TEMPORARIES_DECL\n\n"; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 4225 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 4226 | OS << "#ifdef GET_GLOBALISEL_TEMPORARIES_INIT\n" |
| 4227 | << ", State(" << MaxTemporaries << "),\n" |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4228 | << "ISelInfo(TypeObjects, NumTypeObjects, FeatureBitsets" |
| 4229 | << ", ComplexPredicateFns, CustomRenderers)\n" |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 4230 | << "#endif // ifdef GET_GLOBALISEL_TEMPORARIES_INIT\n\n"; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 4231 | |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 4232 | OS << "#ifdef GET_GLOBALISEL_IMPL\n"; |
| 4233 | SubtargetFeatureInfo::emitSubtargetFeatureBitEnumeration(SubtargetFeatures, |
| 4234 | OS); |
Daniel Sanders | e9fdba3 | 2017-04-29 17:30:09 +0000 | [diff] [blame] | 4235 | |
| 4236 | // Separate subtarget features by how often they must be recomputed. |
| 4237 | SubtargetFeatureInfoMap ModuleFeatures; |
| 4238 | std::copy_if(SubtargetFeatures.begin(), SubtargetFeatures.end(), |
| 4239 | std::inserter(ModuleFeatures, ModuleFeatures.end()), |
| 4240 | [](const SubtargetFeatureInfoMap::value_type &X) { |
| 4241 | return !X.second.mustRecomputePerFunction(); |
| 4242 | }); |
| 4243 | SubtargetFeatureInfoMap FunctionFeatures; |
| 4244 | std::copy_if(SubtargetFeatures.begin(), SubtargetFeatures.end(), |
| 4245 | std::inserter(FunctionFeatures, FunctionFeatures.end()), |
| 4246 | [](const SubtargetFeatureInfoMap::value_type &X) { |
| 4247 | return X.second.mustRecomputePerFunction(); |
| 4248 | }); |
| 4249 | |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 4250 | SubtargetFeatureInfo::emitComputeAvailableFeatures( |
Daniel Sanders | e9fdba3 | 2017-04-29 17:30:09 +0000 | [diff] [blame] | 4251 | Target.getName(), "InstructionSelector", "computeAvailableModuleFeatures", |
| 4252 | ModuleFeatures, OS); |
| 4253 | SubtargetFeatureInfo::emitComputeAvailableFeatures( |
| 4254 | Target.getName(), "InstructionSelector", |
| 4255 | "computeAvailableFunctionFeatures", FunctionFeatures, OS, |
| 4256 | "const MachineFunction *MF"); |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 4257 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 4258 | // Emit a table containing the LLT objects needed by the matcher and an enum |
| 4259 | // for the matcher to reference them with. |
Daniel Sanders | 032e7f2 | 2017-08-17 13:18:35 +0000 | [diff] [blame] | 4260 | std::vector<LLTCodeGen> TypeObjects; |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 4261 | for (const auto &Ty : KnownTypes) |
Daniel Sanders | 032e7f2 | 2017-08-17 13:18:35 +0000 | [diff] [blame] | 4262 | TypeObjects.push_back(Ty); |
Mandeep Singh Grang | 1b0e2f2 | 2018-04-06 20:18:05 +0000 | [diff] [blame] | 4263 | llvm::sort(TypeObjects.begin(), TypeObjects.end()); |
Daniel Sanders | 4998070 | 2017-08-23 10:09:25 +0000 | [diff] [blame] | 4264 | OS << "// LLT Objects.\n" |
| 4265 | << "enum {\n"; |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 4266 | for (const auto &TypeObject : TypeObjects) { |
| 4267 | OS << " "; |
| 4268 | TypeObject.emitCxxEnumValue(OS); |
| 4269 | OS << ",\n"; |
| 4270 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4271 | OS << "};\n"; |
| 4272 | OS << "const static size_t NumTypeObjects = " << TypeObjects.size() << ";\n" |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 4273 | << "const static LLT TypeObjects[] = {\n"; |
| 4274 | for (const auto &TypeObject : TypeObjects) { |
| 4275 | OS << " "; |
| 4276 | TypeObject.emitCxxConstructorCall(OS); |
| 4277 | OS << ",\n"; |
| 4278 | } |
| 4279 | OS << "};\n\n"; |
| 4280 | |
| 4281 | // Emit a table containing the PredicateBitsets objects needed by the matcher |
| 4282 | // and an enum for the matcher to reference them with. |
| 4283 | std::vector<std::vector<Record *>> FeatureBitsets; |
| 4284 | for (auto &Rule : Rules) |
| 4285 | FeatureBitsets.push_back(Rule.getRequiredFeatures()); |
Mandeep Singh Grang | 1b0e2f2 | 2018-04-06 20:18:05 +0000 | [diff] [blame] | 4286 | llvm::sort( |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 4287 | FeatureBitsets.begin(), FeatureBitsets.end(), |
| 4288 | [&](const std::vector<Record *> &A, const std::vector<Record *> &B) { |
| 4289 | if (A.size() < B.size()) |
| 4290 | return true; |
| 4291 | if (A.size() > B.size()) |
| 4292 | return false; |
| 4293 | for (const auto &Pair : zip(A, B)) { |
| 4294 | if (std::get<0>(Pair)->getName() < std::get<1>(Pair)->getName()) |
| 4295 | return true; |
| 4296 | if (std::get<0>(Pair)->getName() > std::get<1>(Pair)->getName()) |
| 4297 | return false; |
| 4298 | } |
| 4299 | return false; |
| 4300 | }); |
| 4301 | FeatureBitsets.erase( |
| 4302 | std::unique(FeatureBitsets.begin(), FeatureBitsets.end()), |
| 4303 | FeatureBitsets.end()); |
Daniel Sanders | 4998070 | 2017-08-23 10:09:25 +0000 | [diff] [blame] | 4304 | OS << "// Feature bitsets.\n" |
| 4305 | << "enum {\n" |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 4306 | << " GIFBS_Invalid,\n"; |
| 4307 | for (const auto &FeatureBitset : FeatureBitsets) { |
| 4308 | if (FeatureBitset.empty()) |
| 4309 | continue; |
| 4310 | OS << " " << getNameForFeatureBitset(FeatureBitset) << ",\n"; |
| 4311 | } |
| 4312 | OS << "};\n" |
| 4313 | << "const static PredicateBitset FeatureBitsets[] {\n" |
| 4314 | << " {}, // GIFBS_Invalid\n"; |
| 4315 | for (const auto &FeatureBitset : FeatureBitsets) { |
| 4316 | if (FeatureBitset.empty()) |
| 4317 | continue; |
| 4318 | OS << " {"; |
| 4319 | for (const auto &Feature : FeatureBitset) { |
| 4320 | const auto &I = SubtargetFeatures.find(Feature); |
| 4321 | assert(I != SubtargetFeatures.end() && "Didn't import predicate?"); |
| 4322 | OS << I->second.getEnumBitName() << ", "; |
| 4323 | } |
| 4324 | OS << "},\n"; |
| 4325 | } |
| 4326 | OS << "};\n\n"; |
| 4327 | |
| 4328 | // Emit complex predicate table and an enum to reference them with. |
Daniel Sanders | 4998070 | 2017-08-23 10:09:25 +0000 | [diff] [blame] | 4329 | OS << "// ComplexPattern predicates.\n" |
| 4330 | << "enum {\n" |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 4331 | << " GICP_Invalid,\n"; |
| 4332 | for (const auto &Record : ComplexPredicates) |
| 4333 | OS << " GICP_" << Record->getName() << ",\n"; |
| 4334 | OS << "};\n" |
| 4335 | << "// See constructor for table contents\n\n"; |
| 4336 | |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 4337 | emitImmPredicates(OS, "I64", "int64_t", [](const Record *R) { |
Daniel Sanders | 649c585 | 2017-10-13 20:42:18 +0000 | [diff] [blame] | 4338 | bool Unset; |
| 4339 | return !R->getValueAsBitOrUnset("IsAPFloat", Unset) && |
| 4340 | !R->getValueAsBit("IsAPInt"); |
| 4341 | }); |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 4342 | emitImmPredicates(OS, "APFloat", "const APFloat &", [](const Record *R) { |
| 4343 | bool Unset; |
| 4344 | return R->getValueAsBitOrUnset("IsAPFloat", Unset); |
| 4345 | }); |
| 4346 | emitImmPredicates(OS, "APInt", "const APInt &", [](const Record *R) { |
| 4347 | return R->getValueAsBit("IsAPInt"); |
| 4348 | }); |
Daniel Sanders | ea8711b | 2017-10-16 03:36:29 +0000 | [diff] [blame] | 4349 | OS << "\n"; |
| 4350 | |
| 4351 | OS << Target.getName() << "InstructionSelector::ComplexMatcherMemFn\n" |
| 4352 | << Target.getName() << "InstructionSelector::ComplexPredicateFns[] = {\n" |
| 4353 | << " nullptr, // GICP_Invalid\n"; |
| 4354 | for (const auto &Record : ComplexPredicates) |
| 4355 | OS << " &" << Target.getName() |
| 4356 | << "InstructionSelector::" << Record->getValueAsString("MatcherFn") |
| 4357 | << ", // " << Record->getName() << "\n"; |
| 4358 | OS << "};\n\n"; |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 4359 | |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 4360 | OS << "// Custom renderers.\n" |
| 4361 | << "enum {\n" |
| 4362 | << " GICR_Invalid,\n"; |
| 4363 | for (const auto &Record : CustomRendererFns) |
| 4364 | OS << " GICR_" << Record->getValueAsString("RendererFn") << ", \n"; |
| 4365 | OS << "};\n"; |
| 4366 | |
| 4367 | OS << Target.getName() << "InstructionSelector::CustomRendererFn\n" |
| 4368 | << Target.getName() << "InstructionSelector::CustomRenderers[] = {\n" |
| 4369 | << " nullptr, // GICP_Invalid\n"; |
| 4370 | for (const auto &Record : CustomRendererFns) |
| 4371 | OS << " &" << Target.getName() |
| 4372 | << "InstructionSelector::" << Record->getValueAsString("RendererFn") |
| 4373 | << ", // " << Record->getName() << "\n"; |
| 4374 | OS << "};\n\n"; |
| 4375 | |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 4376 | std::stable_sort(Rules.begin(), Rules.end(), [&](const RuleMatcher &A, |
| 4377 | const RuleMatcher &B) { |
Aditya Nandakumar | b63e763 | 2018-02-16 22:37:15 +0000 | [diff] [blame] | 4378 | int ScoreA = RuleMatcherScores[A.getRuleID()]; |
| 4379 | int ScoreB = RuleMatcherScores[B.getRuleID()]; |
| 4380 | if (ScoreA > ScoreB) |
| 4381 | return true; |
| 4382 | if (ScoreB > ScoreA) |
| 4383 | return false; |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 4384 | if (A.isHigherPriorityThan(B)) { |
| 4385 | assert(!B.isHigherPriorityThan(A) && "Cannot be more important " |
| 4386 | "and less important at " |
| 4387 | "the same time"); |
| 4388 | return true; |
| 4389 | } |
| 4390 | return false; |
| 4391 | }); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4392 | |
Roman Tereshin | 2df4c22 | 2018-05-02 20:07:15 +0000 | [diff] [blame] | 4393 | OS << "bool " << Target.getName() |
| 4394 | << "InstructionSelector::selectImpl(MachineInstr &I, CodeGenCoverage " |
| 4395 | "&CoverageInfo) const {\n" |
| 4396 | << " MachineFunction &MF = *I.getParent()->getParent();\n" |
| 4397 | << " MachineRegisterInfo &MRI = MF.getRegInfo();\n" |
| 4398 | << " // FIXME: This should be computed on a per-function basis rather " |
| 4399 | "than per-insn.\n" |
| 4400 | << " AvailableFunctionFeatures = computeAvailableFunctionFeatures(&STI, " |
| 4401 | "&MF);\n" |
| 4402 | << " const PredicateBitset AvailableFeatures = getAvailableFeatures();\n" |
| 4403 | << " NewMIVector OutMIs;\n" |
| 4404 | << " State.MIs.clear();\n" |
| 4405 | << " State.MIs.push_back(&I);\n\n" |
| 4406 | << " if (executeMatchTable(*this, OutMIs, State, ISelInfo" |
| 4407 | << ", getMatchTable(), TII, MRI, TRI, RBI, AvailableFeatures" |
| 4408 | << ", CoverageInfo)) {\n" |
| 4409 | << " return true;\n" |
| 4410 | << " }\n\n" |
| 4411 | << " return false;\n" |
| 4412 | << "}\n\n"; |
| 4413 | |
Roman Tereshin | beb3931 | 2018-05-02 20:15:11 +0000 | [diff] [blame] | 4414 | const MatchTable Table = |
| 4415 | buildMatchTable(Rules, OptimizeMatchTable, GenerateCoverage); |
Roman Tereshin | 2df4c22 | 2018-05-02 20:07:15 +0000 | [diff] [blame] | 4416 | OS << "const int64_t *" << Target.getName() |
| 4417 | << "InstructionSelector::getMatchTable() const {\n"; |
| 4418 | Table.emitDeclaration(OS); |
| 4419 | OS << " return "; |
| 4420 | Table.emitUse(OS); |
| 4421 | OS << ";\n}\n"; |
| 4422 | OS << "#endif // ifdef GET_GLOBALISEL_IMPL\n"; |
Daniel Sanders | e9fdba3 | 2017-04-29 17:30:09 +0000 | [diff] [blame] | 4423 | |
| 4424 | OS << "#ifdef GET_GLOBALISEL_PREDICATES_DECL\n" |
| 4425 | << "PredicateBitset AvailableModuleFeatures;\n" |
| 4426 | << "mutable PredicateBitset AvailableFunctionFeatures;\n" |
| 4427 | << "PredicateBitset getAvailableFeatures() const {\n" |
| 4428 | << " return AvailableModuleFeatures | AvailableFunctionFeatures;\n" |
| 4429 | << "}\n" |
| 4430 | << "PredicateBitset\n" |
| 4431 | << "computeAvailableModuleFeatures(const " << Target.getName() |
| 4432 | << "Subtarget *Subtarget) const;\n" |
| 4433 | << "PredicateBitset\n" |
| 4434 | << "computeAvailableFunctionFeatures(const " << Target.getName() |
| 4435 | << "Subtarget *Subtarget,\n" |
| 4436 | << " const MachineFunction *MF) const;\n" |
| 4437 | << "#endif // ifdef GET_GLOBALISEL_PREDICATES_DECL\n"; |
| 4438 | |
| 4439 | OS << "#ifdef GET_GLOBALISEL_PREDICATES_INIT\n" |
| 4440 | << "AvailableModuleFeatures(computeAvailableModuleFeatures(&STI)),\n" |
| 4441 | << "AvailableFunctionFeatures()\n" |
| 4442 | << "#endif // ifdef GET_GLOBALISEL_PREDICATES_INIT\n"; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4443 | } |
| 4444 | |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 4445 | void GlobalISelEmitter::declareSubtargetFeature(Record *Predicate) { |
| 4446 | if (SubtargetFeatures.count(Predicate) == 0) |
| 4447 | SubtargetFeatures.emplace( |
| 4448 | Predicate, SubtargetFeatureInfo(Predicate, SubtargetFeatures.size())); |
| 4449 | } |
| 4450 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4451 | void RuleMatcher::optimize() { |
| 4452 | for (auto &Item : InsnVariableIDs) { |
| 4453 | InstructionMatcher &InsnMatcher = *Item.first; |
| 4454 | for (auto &OM : InsnMatcher.operands()) { |
| 4455 | // Register Banks checks rarely fail, but often crash as targets usually |
| 4456 | // provide only partially defined RegisterBankInfo::getRegBankFromRegClass |
| 4457 | // method. Often the problem is hidden as non-optimized MatchTable checks |
| 4458 | // banks rather late, most notably after checking target / function / |
| 4459 | // module features and a few opcodes. That makes these checks a) |
| 4460 | // beneficial to delay until the very end (we don't want to perform a lot |
| 4461 | // of checks that all pass and then fail at the very end) b) not safe to |
| 4462 | // have as early checks. |
| 4463 | for (auto &OP : OM->predicates()) |
| 4464 | if (isa<RegisterBankOperandMatcher>(OP) || |
| 4465 | isa<ComplexPatternOperandMatcher>(OP)) |
| 4466 | EpilogueMatchers.emplace_back(std::move(OP)); |
| 4467 | OM->eraseNullPredicates(); |
| 4468 | } |
| 4469 | InsnMatcher.optimize(); |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 4470 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4471 | llvm::sort( |
| 4472 | EpilogueMatchers.begin(), EpilogueMatchers.end(), |
| 4473 | [](const std::unique_ptr<PredicateMatcher> &L, |
| 4474 | const std::unique_ptr<PredicateMatcher> &R) { |
| 4475 | return std::make_tuple(L->getKind(), L->getInsnVarID(), L->getOpIdx()) < |
| 4476 | std::make_tuple(R->getKind(), R->getInsnVarID(), R->getOpIdx()); |
| 4477 | }); |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 4478 | } |
| 4479 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4480 | bool RuleMatcher::hasFirstCondition() const { |
| 4481 | if (insnmatchers_empty()) |
| 4482 | return false; |
| 4483 | InstructionMatcher &Matcher = insnmatchers_front(); |
| 4484 | if (!Matcher.predicates_empty()) |
| 4485 | return true; |
| 4486 | for (auto &OM : Matcher.operands()) |
| 4487 | for (auto &OP : OM->predicates()) |
| 4488 | if (!isa<InstructionOperandMatcher>(OP)) |
| 4489 | return true; |
| 4490 | return false; |
| 4491 | } |
| 4492 | |
| 4493 | const PredicateMatcher &RuleMatcher::getFirstCondition() const { |
| 4494 | assert(!insnmatchers_empty() && |
| 4495 | "Trying to get a condition from an empty RuleMatcher"); |
| 4496 | |
| 4497 | InstructionMatcher &Matcher = insnmatchers_front(); |
| 4498 | if (!Matcher.predicates_empty()) |
| 4499 | return **Matcher.predicates_begin(); |
| 4500 | // If there is no more predicate on the instruction itself, look at its |
| 4501 | // operands. |
| 4502 | for (auto &OM : Matcher.operands()) |
| 4503 | for (auto &OP : OM->predicates()) |
| 4504 | if (!isa<InstructionOperandMatcher>(OP)) |
| 4505 | return *OP; |
| 4506 | |
| 4507 | llvm_unreachable("Trying to get a condition from an InstructionMatcher with " |
| 4508 | "no conditions"); |
| 4509 | } |
| 4510 | |
| 4511 | std::unique_ptr<PredicateMatcher> RuleMatcher::popFirstCondition() { |
| 4512 | assert(!insnmatchers_empty() && |
| 4513 | "Trying to pop a condition from an empty RuleMatcher"); |
| 4514 | |
| 4515 | InstructionMatcher &Matcher = insnmatchers_front(); |
| 4516 | if (!Matcher.predicates_empty()) |
| 4517 | return Matcher.predicates_pop_front(); |
| 4518 | // If there is no more predicate on the instruction itself, look at its |
| 4519 | // operands. |
| 4520 | for (auto &OM : Matcher.operands()) |
| 4521 | for (auto &OP : OM->predicates()) |
| 4522 | if (!isa<InstructionOperandMatcher>(OP)) { |
| 4523 | std::unique_ptr<PredicateMatcher> Result = std::move(OP); |
| 4524 | OM->eraseNullPredicates(); |
| 4525 | return Result; |
| 4526 | } |
| 4527 | |
| 4528 | llvm_unreachable("Trying to pop a condition from an InstructionMatcher with " |
| 4529 | "no conditions"); |
| 4530 | } |
| 4531 | |
| 4532 | bool GroupMatcher::candidateConditionMatches( |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 4533 | const PredicateMatcher &Predicate) const { |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4534 | |
| 4535 | if (empty()) { |
| 4536 | // Sharing predicates for nested instructions is not supported yet as we |
| 4537 | // currently don't hoist the GIM_RecordInsn's properly, therefore we can |
| 4538 | // only work on the original root instruction (InsnVarID == 0): |
| 4539 | if (Predicate.getInsnVarID() != 0) |
| 4540 | return false; |
| 4541 | // ... otherwise an empty group can handle any predicate with no specific |
| 4542 | // requirements: |
| 4543 | return true; |
| 4544 | } |
| 4545 | |
| 4546 | const Matcher &Representative = **Matchers.begin(); |
| 4547 | const auto &RepresentativeCondition = Representative.getFirstCondition(); |
| 4548 | // ... if not empty, the group can only accomodate matchers with the exact |
| 4549 | // same first condition: |
| 4550 | return Predicate.isIdentical(RepresentativeCondition); |
| 4551 | } |
| 4552 | |
| 4553 | bool GroupMatcher::addMatcher(Matcher &Candidate) { |
| 4554 | if (!Candidate.hasFirstCondition()) |
| 4555 | return false; |
| 4556 | |
| 4557 | const PredicateMatcher &Predicate = Candidate.getFirstCondition(); |
| 4558 | if (!candidateConditionMatches(Predicate)) |
| 4559 | return false; |
| 4560 | |
| 4561 | Matchers.push_back(&Candidate); |
| 4562 | return true; |
| 4563 | } |
| 4564 | |
| 4565 | void GroupMatcher::finalize() { |
| 4566 | assert(Conditions.empty() && "Already finalized?"); |
| 4567 | if (empty()) |
| 4568 | return; |
| 4569 | |
| 4570 | Matcher &FirstRule = **Matchers.begin(); |
| 4571 | |
| 4572 | Conditions.push_back(FirstRule.popFirstCondition()); |
| 4573 | for (unsigned I = 1, E = Matchers.size(); I < E; ++I) |
| 4574 | Matchers[I]->popFirstCondition(); |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 4575 | } |
| 4576 | |
| 4577 | void GroupMatcher::emit(MatchTable &Table) { |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4578 | unsigned LabelID = ~0U; |
| 4579 | if (!Conditions.empty()) { |
| 4580 | LabelID = Table.allocateLabelID(); |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 4581 | Table << MatchTable::Opcode("GIM_Try", +1) |
| 4582 | << MatchTable::Comment("On fail goto") |
| 4583 | << MatchTable::JumpTarget(LabelID) << MatchTable::LineBreak; |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 4584 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4585 | for (auto &Condition : Conditions) |
| 4586 | Condition->emitPredicateOpcodes( |
| 4587 | Table, *static_cast<RuleMatcher *>(*Matchers.begin())); |
| 4588 | |
| 4589 | for (const auto &M : Matchers) |
| 4590 | M->emit(Table); |
| 4591 | |
| 4592 | // Exit the group |
| 4593 | if (!Conditions.empty()) |
| 4594 | Table << MatchTable::Opcode("GIM_Reject", -1) << MatchTable::LineBreak |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 4595 | << MatchTable::Label(LabelID); |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 4596 | } |
| 4597 | |
Roman Tereshin | 0ee082f | 2018-05-22 19:37:59 +0000 | [diff] [blame] | 4598 | bool SwitchMatcher::isSupportedPredicateType(const PredicateMatcher &P) { |
| 4599 | return isa<InstructionOpcodeMatcher>(P); |
| 4600 | } |
| 4601 | |
| 4602 | bool SwitchMatcher::candidateConditionMatches( |
| 4603 | const PredicateMatcher &Predicate) const { |
| 4604 | |
| 4605 | if (empty()) { |
| 4606 | // Sharing predicates for nested instructions is not supported yet as we |
| 4607 | // currently don't hoist the GIM_RecordInsn's properly, therefore we can |
| 4608 | // only work on the original root instruction (InsnVarID == 0): |
| 4609 | if (Predicate.getInsnVarID() != 0) |
| 4610 | return false; |
| 4611 | // ... while an attempt to add even a root matcher to an empty SwitchMatcher |
| 4612 | // could fail as not all the types of conditions are supported: |
| 4613 | if (!isSupportedPredicateType(Predicate)) |
| 4614 | return false; |
| 4615 | // ... or the condition might not have a proper implementation of |
| 4616 | // getValue() / isIdenticalDownToValue() yet: |
| 4617 | if (!Predicate.hasValue()) |
| 4618 | return false; |
| 4619 | // ... otherwise an empty Switch can accomodate the condition with no |
| 4620 | // further requirements: |
| 4621 | return true; |
| 4622 | } |
| 4623 | |
| 4624 | const Matcher &CaseRepresentative = **Matchers.begin(); |
| 4625 | const auto &RepresentativeCondition = CaseRepresentative.getFirstCondition(); |
| 4626 | // Switch-cases must share the same kind of condition and path to the value it |
| 4627 | // checks: |
| 4628 | if (!Predicate.isIdenticalDownToValue(RepresentativeCondition)) |
| 4629 | return false; |
| 4630 | |
| 4631 | const auto Value = Predicate.getValue(); |
| 4632 | // ... but be unique with respect to the actual value they check: |
| 4633 | return Values.count(Value) == 0; |
| 4634 | } |
| 4635 | |
| 4636 | bool SwitchMatcher::addMatcher(Matcher &Candidate) { |
| 4637 | if (!Candidate.hasFirstCondition()) |
| 4638 | return false; |
| 4639 | |
| 4640 | const PredicateMatcher &Predicate = Candidate.getFirstCondition(); |
| 4641 | if (!candidateConditionMatches(Predicate)) |
| 4642 | return false; |
| 4643 | const auto Value = Predicate.getValue(); |
| 4644 | Values.insert(Value); |
| 4645 | |
| 4646 | Matchers.push_back(&Candidate); |
| 4647 | return true; |
| 4648 | } |
| 4649 | |
| 4650 | void SwitchMatcher::finalize() { |
| 4651 | assert(Condition == nullptr && "Already finalized"); |
| 4652 | assert(Values.size() == Matchers.size() && "Broken SwitchMatcher"); |
| 4653 | if (empty()) |
| 4654 | return; |
| 4655 | |
| 4656 | std::stable_sort(Matchers.begin(), Matchers.end(), |
| 4657 | [](const Matcher *L, const Matcher *R) { |
| 4658 | return L->getFirstCondition().getValue() < |
| 4659 | R->getFirstCondition().getValue(); |
| 4660 | }); |
| 4661 | Condition = Matchers[0]->popFirstCondition(); |
| 4662 | for (unsigned I = 1, E = Values.size(); I < E; ++I) |
| 4663 | Matchers[I]->popFirstCondition(); |
| 4664 | } |
| 4665 | |
| 4666 | void SwitchMatcher::emitPredicateSpecificOpcodes(const PredicateMatcher &P, |
| 4667 | MatchTable &Table) { |
| 4668 | assert(isSupportedPredicateType(P) && "Predicate type is not supported"); |
| 4669 | |
| 4670 | if (const auto *Condition = dyn_cast<InstructionOpcodeMatcher>(&P)) { |
| 4671 | Table << MatchTable::Opcode("GIM_SwitchOpcode") << MatchTable::Comment("MI") |
| 4672 | << MatchTable::IntValue(Condition->getInsnVarID()); |
| 4673 | return; |
| 4674 | } |
| 4675 | |
| 4676 | llvm_unreachable("emitPredicateSpecificOpcodes is broken: can not handle a " |
| 4677 | "predicate type that is claimed to be supported"); |
| 4678 | } |
| 4679 | |
| 4680 | void SwitchMatcher::emit(MatchTable &Table) { |
| 4681 | assert(Values.size() == Matchers.size() && "Broken SwitchMatcher"); |
| 4682 | if (empty()) |
| 4683 | return; |
| 4684 | assert(Condition != nullptr && |
| 4685 | "Broken SwitchMatcher, hasn't been finalized?"); |
| 4686 | |
| 4687 | std::vector<unsigned> LabelIDs(Values.size()); |
| 4688 | std::generate(LabelIDs.begin(), LabelIDs.end(), |
| 4689 | [&Table]() { return Table.allocateLabelID(); }); |
| 4690 | const unsigned Default = Table.allocateLabelID(); |
| 4691 | |
| 4692 | const int64_t LowerBound = Values.begin()->getRawValue(); |
| 4693 | const int64_t UpperBound = Values.rbegin()->getRawValue() + 1; |
| 4694 | |
| 4695 | emitPredicateSpecificOpcodes(*Condition, Table); |
| 4696 | |
| 4697 | Table << MatchTable::Comment("[") << MatchTable::IntValue(LowerBound) |
| 4698 | << MatchTable::IntValue(UpperBound) << MatchTable::Comment(")") |
| 4699 | << MatchTable::Comment("default:") << MatchTable::JumpTarget(Default); |
| 4700 | |
| 4701 | int64_t J = LowerBound; |
| 4702 | auto VI = Values.begin(); |
| 4703 | for (unsigned I = 0, E = Values.size(); I < E; ++I) { |
| 4704 | auto V = *VI++; |
| 4705 | while (J++ < V.getRawValue()) |
| 4706 | Table << MatchTable::IntValue(0); |
| 4707 | V.turnIntoComment(); |
| 4708 | Table << MatchTable::LineBreak << V << MatchTable::JumpTarget(LabelIDs[I]); |
| 4709 | } |
| 4710 | Table << MatchTable::LineBreak; |
| 4711 | |
| 4712 | for (unsigned I = 0, E = Values.size(); I < E; ++I) { |
| 4713 | Table << MatchTable::Label(LabelIDs[I]); |
| 4714 | Matchers[I]->emit(Table); |
| 4715 | Table << MatchTable::Opcode("GIM_Reject") << MatchTable::LineBreak; |
| 4716 | } |
| 4717 | Table << MatchTable::Label(Default); |
| 4718 | } |
| 4719 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4720 | unsigned OperandMatcher::getInsnVarID() const { return Insn.getInsnVarID(); } |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 4721 | |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 4722 | } // end anonymous namespace |
| 4723 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4724 | //===----------------------------------------------------------------------===// |
| 4725 | |
| 4726 | namespace llvm { |
| 4727 | void EmitGlobalISel(RecordKeeper &RK, raw_ostream &OS) { |
| 4728 | GlobalISelEmitter(RK).run(OS); |
| 4729 | } |
| 4730 | } // End llvm namespace |