Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1 | //===- GlobalISelEmitter.cpp - Generate an instruction selector -----------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | /// \file |
| 10 | /// This tablegen backend emits code for use by the GlobalISel instruction |
| 11 | /// selector. See include/llvm/CodeGen/TargetGlobalISel.td. |
| 12 | /// |
| 13 | /// This file analyzes the patterns recognized by the SelectionDAGISel tablegen |
| 14 | /// backend, filters out the ones that are unsupported, maps |
| 15 | /// SelectionDAG-specific constructs to their GlobalISel counterpart |
| 16 | /// (when applicable: MVT to LLT; SDNode to generic Instruction). |
| 17 | /// |
| 18 | /// Not all patterns are supported: pass the tablegen invocation |
| 19 | /// "-warn-on-skipped-patterns" to emit a warning when a pattern is skipped, |
| 20 | /// as well as why. |
| 21 | /// |
| 22 | /// The generated file defines a single method: |
| 23 | /// bool <Target>InstructionSelector::selectImpl(MachineInstr &I) const; |
| 24 | /// intended to be used in InstructionSelector::select as the first-step |
| 25 | /// selector for the patterns that don't require complex C++. |
| 26 | /// |
| 27 | /// FIXME: We'll probably want to eventually define a base |
| 28 | /// "TargetGenInstructionSelector" class. |
| 29 | /// |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | |
| 32 | #include "CodeGenDAGPatterns.h" |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 33 | #include "SubtargetFeatureInfo.h" |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/Optional.h" |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/SmallSet.h" |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/Statistic.h" |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 37 | #include "llvm/Support/CodeGenCoverage.h" |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 38 | #include "llvm/Support/CommandLine.h" |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Error.h" |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 40 | #include "llvm/Support/LowLevelTypeImpl.h" |
David Blaikie | 13e77db | 2018-03-23 23:58:25 +0000 | [diff] [blame] | 41 | #include "llvm/Support/MachineValueType.h" |
Pavel Labath | 52a82e2 | 2017-02-21 09:19:41 +0000 | [diff] [blame] | 42 | #include "llvm/Support/ScopedPrinter.h" |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 43 | #include "llvm/TableGen/Error.h" |
| 44 | #include "llvm/TableGen/Record.h" |
| 45 | #include "llvm/TableGen/TableGenBackend.h" |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 46 | #include <numeric> |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 47 | #include <string> |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 48 | using namespace llvm; |
| 49 | |
| 50 | #define DEBUG_TYPE "gisel-emitter" |
| 51 | |
| 52 | STATISTIC(NumPatternTotal, "Total number of patterns"); |
Daniel Sanders | b41ce2b | 2017-02-20 14:31:27 +0000 | [diff] [blame] | 53 | STATISTIC(NumPatternImported, "Number of patterns imported from SelectionDAG"); |
| 54 | STATISTIC(NumPatternImportsSkipped, "Number of SelectionDAG imports skipped"); |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 55 | STATISTIC(NumPatternsTested, "Number of patterns executed according to coverage information"); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 56 | STATISTIC(NumPatternEmitted, "Number of patterns emitted"); |
| 57 | |
Daniel Sanders | 0848b23 | 2017-03-27 13:15:13 +0000 | [diff] [blame] | 58 | cl::OptionCategory GlobalISelEmitterCat("Options for -gen-global-isel"); |
| 59 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 60 | static cl::opt<bool> WarnOnSkippedPatterns( |
| 61 | "warn-on-skipped-patterns", |
| 62 | cl::desc("Explain why a pattern was skipped for inclusion " |
| 63 | "in the GlobalISel selector"), |
Daniel Sanders | 0848b23 | 2017-03-27 13:15:13 +0000 | [diff] [blame] | 64 | cl::init(false), cl::cat(GlobalISelEmitterCat)); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 65 | |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 66 | static cl::opt<bool> GenerateCoverage( |
| 67 | "instrument-gisel-coverage", |
| 68 | cl::desc("Generate coverage instrumentation for GlobalISel"), |
| 69 | cl::init(false), cl::cat(GlobalISelEmitterCat)); |
| 70 | |
| 71 | static cl::opt<std::string> UseCoverageFile( |
| 72 | "gisel-coverage-file", cl::init(""), |
| 73 | cl::desc("Specify file to retrieve coverage information from"), |
| 74 | cl::cat(GlobalISelEmitterCat)); |
| 75 | |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 76 | static cl::opt<bool> OptimizeMatchTable( |
| 77 | "optimize-match-table", |
| 78 | cl::desc("Generate an optimized version of the match table"), |
| 79 | cl::init(true), cl::cat(GlobalISelEmitterCat)); |
| 80 | |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 81 | namespace { |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 82 | //===- Helper functions ---------------------------------------------------===// |
| 83 | |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 84 | /// Get the name of the enum value used to number the predicate function. |
| 85 | std::string getEnumNameForPredicate(const TreePredicateFn &Predicate) { |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 86 | if (Predicate.hasGISelPredicateCode()) |
| 87 | return "GIPFP_MI_" + Predicate.getFnName(); |
Simon Pilgrim | 6ecae9f | 2017-10-14 21:27:53 +0000 | [diff] [blame] | 88 | return "GIPFP_" + Predicate.getImmTypeIdentifier().str() + "_" + |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 89 | Predicate.getFnName(); |
| 90 | } |
| 91 | |
| 92 | /// Get the opcode used to check this predicate. |
| 93 | std::string getMatchOpcodeForPredicate(const TreePredicateFn &Predicate) { |
Simon Pilgrim | 6ecae9f | 2017-10-14 21:27:53 +0000 | [diff] [blame] | 94 | return "GIM_Check" + Predicate.getImmTypeIdentifier().str() + "ImmPredicate"; |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 97 | /// This class stands in for LLT wherever we want to tablegen-erate an |
| 98 | /// equivalent at compiler run-time. |
| 99 | class LLTCodeGen { |
| 100 | private: |
| 101 | LLT Ty; |
| 102 | |
| 103 | public: |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 104 | LLTCodeGen() = default; |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 105 | LLTCodeGen(const LLT &Ty) : Ty(Ty) {} |
| 106 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 107 | std::string getCxxEnumValue() const { |
| 108 | std::string Str; |
| 109 | raw_string_ostream OS(Str); |
| 110 | |
| 111 | emitCxxEnumValue(OS); |
| 112 | return OS.str(); |
| 113 | } |
| 114 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 115 | void emitCxxEnumValue(raw_ostream &OS) const { |
| 116 | if (Ty.isScalar()) { |
| 117 | OS << "GILLT_s" << Ty.getSizeInBits(); |
| 118 | return; |
| 119 | } |
| 120 | if (Ty.isVector()) { |
| 121 | OS << "GILLT_v" << Ty.getNumElements() << "s" << Ty.getScalarSizeInBits(); |
| 122 | return; |
| 123 | } |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 124 | if (Ty.isPointer()) { |
| 125 | OS << "GILLT_p" << Ty.getAddressSpace(); |
| 126 | if (Ty.getSizeInBits() > 0) |
| 127 | OS << "s" << Ty.getSizeInBits(); |
| 128 | return; |
| 129 | } |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 130 | llvm_unreachable("Unhandled LLT"); |
| 131 | } |
| 132 | |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 133 | void emitCxxConstructorCall(raw_ostream &OS) const { |
| 134 | if (Ty.isScalar()) { |
| 135 | OS << "LLT::scalar(" << Ty.getSizeInBits() << ")"; |
| 136 | return; |
| 137 | } |
| 138 | if (Ty.isVector()) { |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 139 | OS << "LLT::vector(" << Ty.getNumElements() << ", " |
| 140 | << Ty.getScalarSizeInBits() << ")"; |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 141 | return; |
| 142 | } |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 143 | if (Ty.isPointer() && Ty.getSizeInBits() > 0) { |
| 144 | OS << "LLT::pointer(" << Ty.getAddressSpace() << ", " |
| 145 | << Ty.getSizeInBits() << ")"; |
| 146 | return; |
| 147 | } |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 148 | llvm_unreachable("Unhandled LLT"); |
| 149 | } |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 150 | |
| 151 | const LLT &get() const { return Ty; } |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 152 | |
Mandeep Singh Grang | 1b0e2f2 | 2018-04-06 20:18:05 +0000 | [diff] [blame] | 153 | /// This ordering is used for std::unique() and llvm::sort(). There's no |
Daniel Sanders | 032e7f2 | 2017-08-17 13:18:35 +0000 | [diff] [blame] | 154 | /// particular logic behind the order but either A < B or B < A must be |
| 155 | /// true if A != B. |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 156 | bool operator<(const LLTCodeGen &Other) const { |
Daniel Sanders | 032e7f2 | 2017-08-17 13:18:35 +0000 | [diff] [blame] | 157 | if (Ty.isValid() != Other.Ty.isValid()) |
| 158 | return Ty.isValid() < Other.Ty.isValid(); |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 159 | if (!Ty.isValid()) |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 160 | return false; |
Daniel Sanders | 032e7f2 | 2017-08-17 13:18:35 +0000 | [diff] [blame] | 161 | |
| 162 | if (Ty.isVector() != Other.Ty.isVector()) |
| 163 | return Ty.isVector() < Other.Ty.isVector(); |
| 164 | if (Ty.isScalar() != Other.Ty.isScalar()) |
| 165 | return Ty.isScalar() < Other.Ty.isScalar(); |
| 166 | if (Ty.isPointer() != Other.Ty.isPointer()) |
| 167 | return Ty.isPointer() < Other.Ty.isPointer(); |
| 168 | |
| 169 | if (Ty.isPointer() && Ty.getAddressSpace() != Other.Ty.getAddressSpace()) |
| 170 | return Ty.getAddressSpace() < Other.Ty.getAddressSpace(); |
| 171 | |
| 172 | if (Ty.isVector() && Ty.getNumElements() != Other.Ty.getNumElements()) |
| 173 | return Ty.getNumElements() < Other.Ty.getNumElements(); |
| 174 | |
| 175 | return Ty.getSizeInBits() < Other.Ty.getSizeInBits(); |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 176 | } |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 177 | |
| 178 | bool operator==(const LLTCodeGen &B) const { return Ty == B.Ty; } |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 179 | }; |
| 180 | |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 181 | // Track all types that are used so we can emit the corresponding enum. |
| 182 | std::set<LLTCodeGen> KnownTypes; |
| 183 | |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 184 | class InstructionMatcher; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 185 | /// Convert an MVT to an equivalent LLT if possible, or the invalid LLT() for |
| 186 | /// MVTs that don't map cleanly to an LLT (e.g., iPTR, *any, ...). |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 187 | static Optional<LLTCodeGen> MVTToLLT(MVT::SimpleValueType SVT) { |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 188 | MVT VT(SVT); |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 189 | |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 190 | if (VT.isVector() && VT.getVectorNumElements() != 1) |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 191 | return LLTCodeGen( |
| 192 | LLT::vector(VT.getVectorNumElements(), VT.getScalarSizeInBits())); |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 193 | |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 194 | if (VT.isInteger() || VT.isFloatingPoint()) |
| 195 | return LLTCodeGen(LLT::scalar(VT.getSizeInBits())); |
| 196 | return None; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 199 | static std::string explainPredicates(const TreePatternNode *N) { |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 200 | std::string Explanation = ""; |
| 201 | StringRef Separator = ""; |
Nicolai Haehnle | 445b0b6 | 2018-11-30 14:15:13 +0000 | [diff] [blame] | 202 | for (const TreePredicateCall &Call : N->getPredicateCalls()) { |
| 203 | const TreePredicateFn &P = Call.Fn; |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 204 | Explanation += |
| 205 | (Separator + P.getOrigPatFragRecord()->getRecord()->getName()).str(); |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 206 | Separator = ", "; |
| 207 | |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 208 | if (P.isAlwaysTrue()) |
| 209 | Explanation += " always-true"; |
| 210 | if (P.isImmediatePattern()) |
| 211 | Explanation += " immediate"; |
Daniel Sanders | 3f267bf | 2017-10-15 02:06:44 +0000 | [diff] [blame] | 212 | |
| 213 | if (P.isUnindexed()) |
| 214 | Explanation += " unindexed"; |
| 215 | |
| 216 | if (P.isNonExtLoad()) |
| 217 | Explanation += " non-extload"; |
| 218 | if (P.isAnyExtLoad()) |
| 219 | Explanation += " extload"; |
| 220 | if (P.isSignExtLoad()) |
| 221 | Explanation += " sextload"; |
| 222 | if (P.isZeroExtLoad()) |
| 223 | Explanation += " zextload"; |
| 224 | |
| 225 | if (P.isNonTruncStore()) |
| 226 | Explanation += " non-truncstore"; |
| 227 | if (P.isTruncStore()) |
| 228 | Explanation += " truncstore"; |
| 229 | |
| 230 | if (Record *VT = P.getMemoryVT()) |
| 231 | Explanation += (" MemVT=" + VT->getName()).str(); |
| 232 | if (Record *VT = P.getScalarMemoryVT()) |
| 233 | Explanation += (" ScalarVT(MemVT)=" + VT->getName()).str(); |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 234 | |
Matt Arsenault | d00d857 | 2019-07-15 20:59:42 +0000 | [diff] [blame] | 235 | if (ListInit *AddrSpaces = P.getAddressSpaces()) { |
| 236 | raw_string_ostream OS(Explanation); |
| 237 | OS << " AddressSpaces=["; |
| 238 | |
| 239 | StringRef AddrSpaceSeparator; |
| 240 | for (Init *Val : AddrSpaces->getValues()) { |
| 241 | IntInit *IntVal = dyn_cast<IntInit>(Val); |
| 242 | if (!IntVal) |
| 243 | continue; |
| 244 | |
| 245 | OS << AddrSpaceSeparator << IntVal->getValue(); |
| 246 | AddrSpaceSeparator = ", "; |
| 247 | } |
| 248 | |
| 249 | OS << ']'; |
| 250 | } |
| 251 | |
Matt Arsenault | 52c2624 | 2019-07-31 00:14:43 +0000 | [diff] [blame] | 252 | int64_t MinAlign = P.getMinAlignment(); |
| 253 | if (MinAlign > 0) |
| 254 | Explanation += " MinAlign=" + utostr(MinAlign); |
| 255 | |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 256 | if (P.isAtomicOrderingMonotonic()) |
| 257 | Explanation += " monotonic"; |
| 258 | if (P.isAtomicOrderingAcquire()) |
| 259 | Explanation += " acquire"; |
| 260 | if (P.isAtomicOrderingRelease()) |
| 261 | Explanation += " release"; |
| 262 | if (P.isAtomicOrderingAcquireRelease()) |
| 263 | Explanation += " acq_rel"; |
| 264 | if (P.isAtomicOrderingSequentiallyConsistent()) |
| 265 | Explanation += " seq_cst"; |
Daniel Sanders | 0c43b3a | 2017-11-30 21:05:59 +0000 | [diff] [blame] | 266 | if (P.isAtomicOrderingAcquireOrStronger()) |
| 267 | Explanation += " >=acquire"; |
| 268 | if (P.isAtomicOrderingWeakerThanAcquire()) |
| 269 | Explanation += " <acquire"; |
| 270 | if (P.isAtomicOrderingReleaseOrStronger()) |
| 271 | Explanation += " >=release"; |
| 272 | if (P.isAtomicOrderingWeakerThanRelease()) |
| 273 | Explanation += " <release"; |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 274 | } |
| 275 | return Explanation; |
| 276 | } |
| 277 | |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 278 | std::string explainOperator(Record *Operator) { |
| 279 | if (Operator->isSubClassOf("SDNode")) |
Craig Topper | 2b8419a | 2017-05-31 19:01:11 +0000 | [diff] [blame] | 280 | return (" (" + Operator->getValueAsString("Opcode") + ")").str(); |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 281 | |
| 282 | if (Operator->isSubClassOf("Intrinsic")) |
| 283 | return (" (Operator is an Intrinsic, " + Operator->getName() + ")").str(); |
| 284 | |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 285 | if (Operator->isSubClassOf("ComplexPattern")) |
| 286 | return (" (Operator is an unmapped ComplexPattern, " + Operator->getName() + |
| 287 | ")") |
| 288 | .str(); |
| 289 | |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 290 | if (Operator->isSubClassOf("SDNodeXForm")) |
| 291 | return (" (Operator is an unmapped SDNodeXForm, " + Operator->getName() + |
| 292 | ")") |
| 293 | .str(); |
| 294 | |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 295 | return (" (Operator " + Operator->getName() + " not understood)").str(); |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /// Helper function to let the emitter report skip reason error messages. |
| 299 | static Error failedImport(const Twine &Reason) { |
| 300 | return make_error<StringError>(Reason, inconvertibleErrorCode()); |
| 301 | } |
| 302 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 303 | static Error isTrivialOperatorNode(const TreePatternNode *N) { |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 304 | std::string Explanation = ""; |
| 305 | std::string Separator = ""; |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 306 | |
| 307 | bool HasUnsupportedPredicate = false; |
Nicolai Haehnle | 445b0b6 | 2018-11-30 14:15:13 +0000 | [diff] [blame] | 308 | for (const TreePredicateCall &Call : N->getPredicateCalls()) { |
| 309 | const TreePredicateFn &Predicate = Call.Fn; |
| 310 | |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 311 | if (Predicate.isAlwaysTrue()) |
| 312 | continue; |
| 313 | |
| 314 | if (Predicate.isImmediatePattern()) |
| 315 | continue; |
| 316 | |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 317 | if (Predicate.isNonExtLoad() || Predicate.isAnyExtLoad() || |
| 318 | Predicate.isSignExtLoad() || Predicate.isZeroExtLoad()) |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 319 | continue; |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 320 | |
Matt Arsenault | 0277249 | 2019-07-15 21:15:20 +0000 | [diff] [blame] | 321 | if (Predicate.isNonTruncStore() || Predicate.isTruncStore()) |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 322 | continue; |
| 323 | |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 324 | if (Predicate.isLoad() && Predicate.getMemoryVT()) |
| 325 | continue; |
| 326 | |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 327 | if (Predicate.isLoad() || Predicate.isStore()) { |
| 328 | if (Predicate.isUnindexed()) |
| 329 | continue; |
| 330 | } |
| 331 | |
Matt Arsenault | d00d857 | 2019-07-15 20:59:42 +0000 | [diff] [blame] | 332 | if (Predicate.isLoad() || Predicate.isStore() || Predicate.isAtomic()) { |
| 333 | const ListInit *AddrSpaces = Predicate.getAddressSpaces(); |
| 334 | if (AddrSpaces && !AddrSpaces->empty()) |
| 335 | continue; |
Matt Arsenault | 52c2624 | 2019-07-31 00:14:43 +0000 | [diff] [blame] | 336 | |
| 337 | if (Predicate.getMinAlignment() > 0) |
| 338 | continue; |
Matt Arsenault | d00d857 | 2019-07-15 20:59:42 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 341 | if (Predicate.isAtomic() && Predicate.getMemoryVT()) |
| 342 | continue; |
| 343 | |
| 344 | if (Predicate.isAtomic() && |
| 345 | (Predicate.isAtomicOrderingMonotonic() || |
| 346 | Predicate.isAtomicOrderingAcquire() || |
| 347 | Predicate.isAtomicOrderingRelease() || |
| 348 | Predicate.isAtomicOrderingAcquireRelease() || |
Daniel Sanders | 0c43b3a | 2017-11-30 21:05:59 +0000 | [diff] [blame] | 349 | Predicate.isAtomicOrderingSequentiallyConsistent() || |
| 350 | Predicate.isAtomicOrderingAcquireOrStronger() || |
| 351 | Predicate.isAtomicOrderingWeakerThanAcquire() || |
| 352 | Predicate.isAtomicOrderingReleaseOrStronger() || |
| 353 | Predicate.isAtomicOrderingWeakerThanRelease())) |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 354 | continue; |
| 355 | |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 356 | if (Predicate.hasGISelPredicateCode()) |
| 357 | continue; |
| 358 | |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 359 | HasUnsupportedPredicate = true; |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 360 | Explanation = Separator + "Has a predicate (" + explainPredicates(N) + ")"; |
| 361 | Separator = ", "; |
Daniel Sanders | 3f267bf | 2017-10-15 02:06:44 +0000 | [diff] [blame] | 362 | Explanation += (Separator + "first-failing:" + |
| 363 | Predicate.getOrigPatFragRecord()->getRecord()->getName()) |
| 364 | .str(); |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 365 | break; |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 368 | if (!HasUnsupportedPredicate) |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 369 | return Error::success(); |
| 370 | |
| 371 | return failedImport(Explanation); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 374 | static Record *getInitValueAsRegClass(Init *V) { |
| 375 | if (DefInit *VDefInit = dyn_cast<DefInit>(V)) { |
| 376 | if (VDefInit->getDef()->isSubClassOf("RegisterOperand")) |
| 377 | return VDefInit->getDef()->getValueAsDef("RegClass"); |
| 378 | if (VDefInit->getDef()->isSubClassOf("RegisterClass")) |
| 379 | return VDefInit->getDef(); |
| 380 | } |
| 381 | return nullptr; |
| 382 | } |
| 383 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 384 | std::string |
| 385 | getNameForFeatureBitset(const std::vector<Record *> &FeatureBitset) { |
| 386 | std::string Name = "GIFBS"; |
| 387 | for (const auto &Feature : FeatureBitset) |
| 388 | Name += ("_" + Feature->getName()).str(); |
| 389 | return Name; |
| 390 | } |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 391 | |
| 392 | //===- MatchTable Helpers -------------------------------------------------===// |
| 393 | |
| 394 | class MatchTable; |
| 395 | |
| 396 | /// A record to be stored in a MatchTable. |
| 397 | /// |
| 398 | /// This class represents any and all output that may be required to emit the |
| 399 | /// MatchTable. Instances are most often configured to represent an opcode or |
| 400 | /// value that will be emitted to the table with some formatting but it can also |
| 401 | /// represent commas, comments, and other formatting instructions. |
| 402 | struct MatchTableRecord { |
| 403 | enum RecordFlagsBits { |
| 404 | MTRF_None = 0x0, |
| 405 | /// Causes EmitStr to be formatted as comment when emitted. |
| 406 | MTRF_Comment = 0x1, |
| 407 | /// Causes the record value to be followed by a comma when emitted. |
| 408 | MTRF_CommaFollows = 0x2, |
| 409 | /// Causes the record value to be followed by a line break when emitted. |
| 410 | MTRF_LineBreakFollows = 0x4, |
| 411 | /// Indicates that the record defines a label and causes an additional |
| 412 | /// comment to be emitted containing the index of the label. |
| 413 | MTRF_Label = 0x8, |
| 414 | /// Causes the record to be emitted as the index of the label specified by |
| 415 | /// LabelID along with a comment indicating where that label is. |
| 416 | MTRF_JumpTarget = 0x10, |
| 417 | /// Causes the formatter to add a level of indentation before emitting the |
| 418 | /// record. |
| 419 | MTRF_Indent = 0x20, |
| 420 | /// Causes the formatter to remove a level of indentation after emitting the |
| 421 | /// record. |
| 422 | MTRF_Outdent = 0x40, |
| 423 | }; |
| 424 | |
| 425 | /// When MTRF_Label or MTRF_JumpTarget is used, indicates a label id to |
| 426 | /// reference or define. |
| 427 | unsigned LabelID; |
| 428 | /// The string to emit. Depending on the MTRF_* flags it may be a comment, a |
| 429 | /// value, a label name. |
| 430 | std::string EmitStr; |
| 431 | |
| 432 | private: |
| 433 | /// The number of MatchTable elements described by this record. Comments are 0 |
| 434 | /// while values are typically 1. Values >1 may occur when we need to emit |
| 435 | /// values that exceed the size of a MatchTable element. |
| 436 | unsigned NumElements; |
| 437 | |
| 438 | public: |
| 439 | /// A bitfield of RecordFlagsBits flags. |
| 440 | unsigned Flags; |
| 441 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 442 | /// The actual run-time value, if known |
| 443 | int64_t RawValue; |
| 444 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 445 | MatchTableRecord(Optional<unsigned> LabelID_, StringRef EmitStr, |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 446 | unsigned NumElements, unsigned Flags, |
| 447 | int64_t RawValue = std::numeric_limits<int64_t>::min()) |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 448 | : LabelID(LabelID_.hasValue() ? LabelID_.getValue() : ~0u), |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 449 | EmitStr(EmitStr), NumElements(NumElements), Flags(Flags), |
| 450 | RawValue(RawValue) { |
| 451 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 452 | assert((!LabelID_.hasValue() || LabelID != ~0u) && |
| 453 | "This value is reserved for non-labels"); |
| 454 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 455 | MatchTableRecord(const MatchTableRecord &Other) = default; |
| 456 | MatchTableRecord(MatchTableRecord &&Other) = default; |
| 457 | |
| 458 | /// Useful if a Match Table Record gets optimized out |
| 459 | void turnIntoComment() { |
| 460 | Flags |= MTRF_Comment; |
| 461 | Flags &= ~MTRF_CommaFollows; |
| 462 | NumElements = 0; |
| 463 | } |
| 464 | |
| 465 | /// For Jump Table generation purposes |
| 466 | bool operator<(const MatchTableRecord &Other) const { |
| 467 | return RawValue < Other.RawValue; |
| 468 | } |
| 469 | int64_t getRawValue() const { return RawValue; } |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 470 | |
| 471 | void emit(raw_ostream &OS, bool LineBreakNextAfterThis, |
| 472 | const MatchTable &Table) const; |
| 473 | unsigned size() const { return NumElements; } |
| 474 | }; |
| 475 | |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 476 | class Matcher; |
| 477 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 478 | /// Holds the contents of a generated MatchTable to enable formatting and the |
| 479 | /// necessary index tracking needed to support GIM_Try. |
| 480 | class MatchTable { |
| 481 | /// An unique identifier for the table. The generated table will be named |
| 482 | /// MatchTable${ID}. |
| 483 | unsigned ID; |
| 484 | /// The records that make up the table. Also includes comments describing the |
| 485 | /// values being emitted and line breaks to format it. |
| 486 | std::vector<MatchTableRecord> Contents; |
| 487 | /// The currently defined labels. |
| 488 | DenseMap<unsigned, unsigned> LabelMap; |
| 489 | /// Tracks the sum of MatchTableRecord::NumElements as the table is built. |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 490 | unsigned CurrentSize = 0; |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 491 | /// A unique identifier for a MatchTable label. |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 492 | unsigned CurrentLabelID = 0; |
Roman Tereshin | beb3931 | 2018-05-02 20:15:11 +0000 | [diff] [blame] | 493 | /// Determines if the table should be instrumented for rule coverage tracking. |
| 494 | bool IsWithCoverage; |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 495 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 496 | public: |
| 497 | static MatchTableRecord LineBreak; |
| 498 | static MatchTableRecord Comment(StringRef Comment) { |
| 499 | return MatchTableRecord(None, Comment, 0, MatchTableRecord::MTRF_Comment); |
| 500 | } |
| 501 | static MatchTableRecord Opcode(StringRef Opcode, int IndentAdjust = 0) { |
| 502 | unsigned ExtraFlags = 0; |
| 503 | if (IndentAdjust > 0) |
| 504 | ExtraFlags |= MatchTableRecord::MTRF_Indent; |
| 505 | if (IndentAdjust < 0) |
| 506 | ExtraFlags |= MatchTableRecord::MTRF_Outdent; |
| 507 | |
| 508 | return MatchTableRecord(None, Opcode, 1, |
| 509 | MatchTableRecord::MTRF_CommaFollows | ExtraFlags); |
| 510 | } |
| 511 | static MatchTableRecord NamedValue(StringRef NamedValue) { |
| 512 | return MatchTableRecord(None, NamedValue, 1, |
| 513 | MatchTableRecord::MTRF_CommaFollows); |
| 514 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 515 | static MatchTableRecord NamedValue(StringRef NamedValue, int64_t RawValue) { |
| 516 | return MatchTableRecord(None, NamedValue, 1, |
| 517 | MatchTableRecord::MTRF_CommaFollows, RawValue); |
| 518 | } |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 519 | static MatchTableRecord NamedValue(StringRef Namespace, |
| 520 | StringRef NamedValue) { |
| 521 | return MatchTableRecord(None, (Namespace + "::" + NamedValue).str(), 1, |
| 522 | MatchTableRecord::MTRF_CommaFollows); |
| 523 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 524 | static MatchTableRecord NamedValue(StringRef Namespace, StringRef NamedValue, |
| 525 | int64_t RawValue) { |
| 526 | return MatchTableRecord(None, (Namespace + "::" + NamedValue).str(), 1, |
| 527 | MatchTableRecord::MTRF_CommaFollows, RawValue); |
| 528 | } |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 529 | static MatchTableRecord IntValue(int64_t IntValue) { |
| 530 | return MatchTableRecord(None, llvm::to_string(IntValue), 1, |
| 531 | MatchTableRecord::MTRF_CommaFollows); |
| 532 | } |
| 533 | static MatchTableRecord Label(unsigned LabelID) { |
| 534 | return MatchTableRecord(LabelID, "Label " + llvm::to_string(LabelID), 0, |
| 535 | MatchTableRecord::MTRF_Label | |
| 536 | MatchTableRecord::MTRF_Comment | |
| 537 | MatchTableRecord::MTRF_LineBreakFollows); |
| 538 | } |
| 539 | static MatchTableRecord JumpTarget(unsigned LabelID) { |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 540 | return MatchTableRecord(LabelID, "Label " + llvm::to_string(LabelID), 1, |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 541 | MatchTableRecord::MTRF_JumpTarget | |
| 542 | MatchTableRecord::MTRF_Comment | |
| 543 | MatchTableRecord::MTRF_CommaFollows); |
| 544 | } |
| 545 | |
Roman Tereshin | beb3931 | 2018-05-02 20:15:11 +0000 | [diff] [blame] | 546 | static MatchTable buildTable(ArrayRef<Matcher *> Rules, bool WithCoverage); |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 547 | |
Roman Tereshin | beb3931 | 2018-05-02 20:15:11 +0000 | [diff] [blame] | 548 | MatchTable(bool WithCoverage, unsigned ID = 0) |
| 549 | : ID(ID), IsWithCoverage(WithCoverage) {} |
| 550 | |
| 551 | bool isWithCoverage() const { return IsWithCoverage; } |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 552 | |
| 553 | void push_back(const MatchTableRecord &Value) { |
| 554 | if (Value.Flags & MatchTableRecord::MTRF_Label) |
| 555 | defineLabel(Value.LabelID); |
| 556 | Contents.push_back(Value); |
| 557 | CurrentSize += Value.size(); |
| 558 | } |
| 559 | |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 560 | unsigned allocateLabelID() { return CurrentLabelID++; } |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 561 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 562 | void defineLabel(unsigned LabelID) { |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 563 | LabelMap.insert(std::make_pair(LabelID, CurrentSize)); |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | unsigned getLabelIndex(unsigned LabelID) const { |
| 567 | const auto I = LabelMap.find(LabelID); |
| 568 | assert(I != LabelMap.end() && "Use of undeclared label"); |
| 569 | return I->second; |
| 570 | } |
| 571 | |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 572 | void emitUse(raw_ostream &OS) const { OS << "MatchTable" << ID; } |
| 573 | |
| 574 | void emitDeclaration(raw_ostream &OS) const { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 575 | unsigned Indentation = 4; |
Daniel Sanders | cbbbfe4 | 2017-07-27 12:47:31 +0000 | [diff] [blame] | 576 | OS << " constexpr static int64_t MatchTable" << ID << "[] = {"; |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 577 | LineBreak.emit(OS, true, *this); |
| 578 | OS << std::string(Indentation, ' '); |
| 579 | |
| 580 | for (auto I = Contents.begin(), E = Contents.end(); I != E; |
| 581 | ++I) { |
| 582 | bool LineBreakIsNext = false; |
| 583 | const auto &NextI = std::next(I); |
| 584 | |
| 585 | if (NextI != E) { |
| 586 | if (NextI->EmitStr == "" && |
| 587 | NextI->Flags == MatchTableRecord::MTRF_LineBreakFollows) |
| 588 | LineBreakIsNext = true; |
| 589 | } |
| 590 | |
| 591 | if (I->Flags & MatchTableRecord::MTRF_Indent) |
| 592 | Indentation += 2; |
| 593 | |
| 594 | I->emit(OS, LineBreakIsNext, *this); |
| 595 | if (I->Flags & MatchTableRecord::MTRF_LineBreakFollows) |
| 596 | OS << std::string(Indentation, ' '); |
| 597 | |
| 598 | if (I->Flags & MatchTableRecord::MTRF_Outdent) |
| 599 | Indentation -= 2; |
| 600 | } |
| 601 | OS << "};\n"; |
| 602 | } |
| 603 | }; |
| 604 | |
| 605 | MatchTableRecord MatchTable::LineBreak = { |
| 606 | None, "" /* Emit String */, 0 /* Elements */, |
| 607 | MatchTableRecord::MTRF_LineBreakFollows}; |
| 608 | |
| 609 | void MatchTableRecord::emit(raw_ostream &OS, bool LineBreakIsNextAfterThis, |
| 610 | const MatchTable &Table) const { |
| 611 | bool UseLineComment = |
| 612 | LineBreakIsNextAfterThis | (Flags & MTRF_LineBreakFollows); |
| 613 | if (Flags & (MTRF_JumpTarget | MTRF_CommaFollows)) |
| 614 | UseLineComment = false; |
| 615 | |
| 616 | if (Flags & MTRF_Comment) |
| 617 | OS << (UseLineComment ? "// " : "/*"); |
| 618 | |
| 619 | OS << EmitStr; |
| 620 | if (Flags & MTRF_Label) |
| 621 | OS << ": @" << Table.getLabelIndex(LabelID); |
| 622 | |
| 623 | if (Flags & MTRF_Comment && !UseLineComment) |
| 624 | OS << "*/"; |
| 625 | |
| 626 | if (Flags & MTRF_JumpTarget) { |
| 627 | if (Flags & MTRF_Comment) |
| 628 | OS << " "; |
| 629 | OS << Table.getLabelIndex(LabelID); |
| 630 | } |
| 631 | |
| 632 | if (Flags & MTRF_CommaFollows) { |
| 633 | OS << ","; |
| 634 | if (!LineBreakIsNextAfterThis && !(Flags & MTRF_LineBreakFollows)) |
| 635 | OS << " "; |
| 636 | } |
| 637 | |
| 638 | if (Flags & MTRF_LineBreakFollows) |
| 639 | OS << "\n"; |
| 640 | } |
| 641 | |
| 642 | MatchTable &operator<<(MatchTable &Table, const MatchTableRecord &Value) { |
| 643 | Table.push_back(Value); |
| 644 | return Table; |
| 645 | } |
| 646 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 647 | //===- Matchers -----------------------------------------------------------===// |
| 648 | |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 649 | class OperandMatcher; |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 650 | class MatchAction; |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 651 | class PredicateMatcher; |
| 652 | class RuleMatcher; |
| 653 | |
| 654 | class Matcher { |
| 655 | public: |
| 656 | virtual ~Matcher() = default; |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 657 | virtual void optimize() {} |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 658 | virtual void emit(MatchTable &Table) = 0; |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 659 | |
| 660 | virtual bool hasFirstCondition() const = 0; |
| 661 | virtual const PredicateMatcher &getFirstCondition() const = 0; |
| 662 | virtual std::unique_ptr<PredicateMatcher> popFirstCondition() = 0; |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 663 | }; |
| 664 | |
Roman Tereshin | beb3931 | 2018-05-02 20:15:11 +0000 | [diff] [blame] | 665 | MatchTable MatchTable::buildTable(ArrayRef<Matcher *> Rules, |
| 666 | bool WithCoverage) { |
| 667 | MatchTable Table(WithCoverage); |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 668 | for (Matcher *Rule : Rules) |
| 669 | Rule->emit(Table); |
| 670 | |
| 671 | return Table << MatchTable::Opcode("GIM_Reject") << MatchTable::LineBreak; |
| 672 | } |
| 673 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 674 | class GroupMatcher final : public Matcher { |
| 675 | /// Conditions that form a common prefix of all the matchers contained. |
| 676 | SmallVector<std::unique_ptr<PredicateMatcher>, 1> Conditions; |
| 677 | |
| 678 | /// All the nested matchers, sharing a common prefix. |
| 679 | std::vector<Matcher *> Matchers; |
| 680 | |
| 681 | /// An owning collection for any auxiliary matchers created while optimizing |
| 682 | /// nested matchers contained. |
| 683 | std::vector<std::unique_ptr<Matcher>> MatcherStorage; |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 684 | |
| 685 | public: |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 686 | /// Add a matcher to the collection of nested matchers if it meets the |
| 687 | /// requirements, and return true. If it doesn't, do nothing and return false. |
| 688 | /// |
| 689 | /// Expected to preserve its argument, so it could be moved out later on. |
| 690 | bool addMatcher(Matcher &Candidate); |
| 691 | |
| 692 | /// Mark the matcher as fully-built and ensure any invariants expected by both |
| 693 | /// optimize() and emit(...) methods. Generally, both sequences of calls |
| 694 | /// are expected to lead to a sensible result: |
| 695 | /// |
| 696 | /// addMatcher(...)*; finalize(); optimize(); emit(...); and |
| 697 | /// addMatcher(...)*; finalize(); emit(...); |
| 698 | /// |
| 699 | /// or generally |
| 700 | /// |
| 701 | /// addMatcher(...)*; finalize(); { optimize()*; emit(...); }* |
| 702 | /// |
| 703 | /// Multiple calls to optimize() are expected to be handled gracefully, though |
| 704 | /// optimize() is not expected to be idempotent. Multiple calls to finalize() |
| 705 | /// aren't generally supported. emit(...) is expected to be non-mutating and |
| 706 | /// producing the exact same results upon repeated calls. |
| 707 | /// |
| 708 | /// addMatcher() calls after the finalize() call are not supported. |
| 709 | /// |
| 710 | /// finalize() and optimize() are both allowed to mutate the contained |
| 711 | /// matchers, so moving them out after finalize() is not supported. |
| 712 | void finalize(); |
Roman Tereshin | fedae33 | 2018-05-23 02:04:19 +0000 | [diff] [blame] | 713 | void optimize() override; |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 714 | void emit(MatchTable &Table) override; |
Quentin Colombet | 34688b9 | 2017-12-18 21:25:53 +0000 | [diff] [blame] | 715 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 716 | /// Could be used to move out the matchers added previously, unless finalize() |
| 717 | /// has been already called. If any of the matchers are moved out, the group |
| 718 | /// becomes safe to destroy, but not safe to re-use for anything else. |
| 719 | iterator_range<std::vector<Matcher *>::iterator> matchers() { |
| 720 | return make_range(Matchers.begin(), Matchers.end()); |
Quentin Colombet | 34688b9 | 2017-12-18 21:25:53 +0000 | [diff] [blame] | 721 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 722 | size_t size() const { return Matchers.size(); } |
| 723 | bool empty() const { return Matchers.empty(); } |
| 724 | |
| 725 | std::unique_ptr<PredicateMatcher> popFirstCondition() override { |
| 726 | assert(!Conditions.empty() && |
| 727 | "Trying to pop a condition from a condition-less group"); |
| 728 | std::unique_ptr<PredicateMatcher> P = std::move(Conditions.front()); |
| 729 | Conditions.erase(Conditions.begin()); |
| 730 | return P; |
| 731 | } |
| 732 | const PredicateMatcher &getFirstCondition() const override { |
| 733 | assert(!Conditions.empty() && |
| 734 | "Trying to get a condition from a condition-less group"); |
| 735 | return *Conditions.front(); |
| 736 | } |
| 737 | bool hasFirstCondition() const override { return !Conditions.empty(); } |
| 738 | |
| 739 | private: |
| 740 | /// See if a candidate matcher could be added to this group solely by |
| 741 | /// analyzing its first condition. |
| 742 | bool candidateConditionMatches(const PredicateMatcher &Predicate) const; |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 743 | }; |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 744 | |
Roman Tereshin | 0ee082f | 2018-05-22 19:37:59 +0000 | [diff] [blame] | 745 | class SwitchMatcher : public Matcher { |
| 746 | /// All the nested matchers, representing distinct switch-cases. The first |
| 747 | /// conditions (as Matcher::getFirstCondition() reports) of all the nested |
| 748 | /// matchers must share the same type and path to a value they check, in other |
| 749 | /// words, be isIdenticalDownToValue, but have different values they check |
| 750 | /// against. |
| 751 | std::vector<Matcher *> Matchers; |
| 752 | |
| 753 | /// The representative condition, with a type and a path (InsnVarID and OpIdx |
| 754 | /// in most cases) shared by all the matchers contained. |
| 755 | std::unique_ptr<PredicateMatcher> Condition = nullptr; |
| 756 | |
| 757 | /// Temporary set used to check that the case values don't repeat within the |
| 758 | /// same switch. |
| 759 | std::set<MatchTableRecord> Values; |
| 760 | |
| 761 | /// An owning collection for any auxiliary matchers created while optimizing |
| 762 | /// nested matchers contained. |
| 763 | std::vector<std::unique_ptr<Matcher>> MatcherStorage; |
| 764 | |
| 765 | public: |
| 766 | bool addMatcher(Matcher &Candidate); |
| 767 | |
| 768 | void finalize(); |
| 769 | void emit(MatchTable &Table) override; |
| 770 | |
| 771 | iterator_range<std::vector<Matcher *>::iterator> matchers() { |
| 772 | return make_range(Matchers.begin(), Matchers.end()); |
| 773 | } |
| 774 | size_t size() const { return Matchers.size(); } |
| 775 | bool empty() const { return Matchers.empty(); } |
| 776 | |
| 777 | std::unique_ptr<PredicateMatcher> popFirstCondition() override { |
| 778 | // SwitchMatcher doesn't have a common first condition for its cases, as all |
| 779 | // the cases only share a kind of a value (a type and a path to it) they |
| 780 | // match, but deliberately differ in the actual value they match. |
| 781 | llvm_unreachable("Trying to pop a condition from a condition-less group"); |
| 782 | } |
| 783 | const PredicateMatcher &getFirstCondition() const override { |
| 784 | llvm_unreachable("Trying to pop a condition from a condition-less group"); |
| 785 | } |
| 786 | bool hasFirstCondition() const override { return false; } |
| 787 | |
| 788 | private: |
| 789 | /// See if the predicate type has a Switch-implementation for it. |
| 790 | static bool isSupportedPredicateType(const PredicateMatcher &Predicate); |
| 791 | |
| 792 | bool candidateConditionMatches(const PredicateMatcher &Predicate) const; |
| 793 | |
| 794 | /// emit()-helper |
| 795 | static void emitPredicateSpecificOpcodes(const PredicateMatcher &P, |
| 796 | MatchTable &Table); |
| 797 | }; |
| 798 | |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 799 | /// Generates code to check that a match rule matches. |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 800 | class RuleMatcher : public Matcher { |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 801 | public: |
Daniel Sanders | 0846452 | 2018-01-29 21:09:12 +0000 | [diff] [blame] | 802 | using ActionList = std::list<std::unique_ptr<MatchAction>>; |
| 803 | using action_iterator = ActionList::iterator; |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 804 | |
| 805 | protected: |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 806 | /// A list of matchers that all need to succeed for the current rule to match. |
| 807 | /// FIXME: This currently supports a single match position but could be |
| 808 | /// extended to support multiple positions to support div/rem fusion or |
| 809 | /// load-multiple instructions. |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 810 | using MatchersTy = std::vector<std::unique_ptr<InstructionMatcher>> ; |
| 811 | MatchersTy Matchers; |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 812 | |
| 813 | /// A list of actions that need to be taken when all predicates in this rule |
| 814 | /// have succeeded. |
Daniel Sanders | 0846452 | 2018-01-29 21:09:12 +0000 | [diff] [blame] | 815 | ActionList Actions; |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 816 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 817 | using DefinedInsnVariablesMap = std::map<InstructionMatcher *, unsigned>; |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 818 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 819 | /// A map of instruction matchers to the local variables |
Daniel Sanders | 078572b | 2017-08-02 11:03:36 +0000 | [diff] [blame] | 820 | DefinedInsnVariablesMap InsnVariableIDs; |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 821 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 822 | using MutatableInsnSet = SmallPtrSet<InstructionMatcher *, 4>; |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 823 | |
| 824 | // The set of instruction matchers that have not yet been claimed for mutation |
| 825 | // by a BuildMI. |
| 826 | MutatableInsnSet MutatableInsns; |
| 827 | |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 828 | /// A map of named operands defined by the matchers that may be referenced by |
| 829 | /// the renderers. |
| 830 | StringMap<OperandMatcher *> DefinedOperands; |
| 831 | |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 832 | /// A map of anonymous physical register operands defined by the matchers that |
| 833 | /// may be referenced by the renderers. |
| 834 | DenseMap<Record *, OperandMatcher *> PhysRegOperands; |
| 835 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 836 | /// ID for the next instruction variable defined with implicitlyDefineInsnVar() |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 837 | unsigned NextInsnVarID; |
| 838 | |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 839 | /// ID for the next output instruction allocated with allocateOutputInsnID() |
| 840 | unsigned NextOutputInsnID; |
| 841 | |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 842 | /// ID for the next temporary register ID allocated with allocateTempRegID() |
| 843 | unsigned NextTempRegID; |
| 844 | |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 845 | std::vector<Record *> RequiredFeatures; |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 846 | std::vector<std::unique_ptr<PredicateMatcher>> EpilogueMatchers; |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 847 | |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 848 | ArrayRef<SMLoc> SrcLoc; |
| 849 | |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 850 | typedef std::tuple<Record *, unsigned, unsigned> |
| 851 | DefinedComplexPatternSubOperand; |
| 852 | typedef StringMap<DefinedComplexPatternSubOperand> |
| 853 | DefinedComplexPatternSubOperandMap; |
| 854 | /// A map of Symbolic Names to ComplexPattern sub-operands. |
| 855 | DefinedComplexPatternSubOperandMap ComplexSubOperands; |
| 856 | |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 857 | uint64_t RuleID; |
| 858 | static uint64_t NextRuleID; |
| 859 | |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 860 | public: |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 861 | RuleMatcher(ArrayRef<SMLoc> SrcLoc) |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 862 | : Matchers(), Actions(), InsnVariableIDs(), MutatableInsns(), |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 863 | DefinedOperands(), NextInsnVarID(0), NextOutputInsnID(0), |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 864 | NextTempRegID(0), SrcLoc(SrcLoc), ComplexSubOperands(), |
| 865 | RuleID(NextRuleID++) {} |
Zachary Turner | b7dbd87 | 2017-03-20 19:56:52 +0000 | [diff] [blame] | 866 | RuleMatcher(RuleMatcher &&Other) = default; |
| 867 | RuleMatcher &operator=(RuleMatcher &&Other) = default; |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 868 | |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 869 | uint64_t getRuleID() const { return RuleID; } |
| 870 | |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 871 | InstructionMatcher &addInstructionMatcher(StringRef SymbolicName); |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 872 | void addRequiredFeature(Record *Feature); |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 873 | const std::vector<Record *> &getRequiredFeatures() const; |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 874 | |
| 875 | template <class Kind, class... Args> Kind &addAction(Args &&... args); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 876 | template <class Kind, class... Args> |
| 877 | action_iterator insertAction(action_iterator InsertPt, Args &&... args); |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 878 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 879 | /// Define an instruction without emitting any code to do so. |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 880 | unsigned implicitlyDefineInsnVar(InstructionMatcher &Matcher); |
| 881 | |
| 882 | unsigned getInsnVarID(InstructionMatcher &InsnMatcher) const; |
Daniel Sanders | 078572b | 2017-08-02 11:03:36 +0000 | [diff] [blame] | 883 | DefinedInsnVariablesMap::const_iterator defined_insn_vars_begin() const { |
| 884 | return InsnVariableIDs.begin(); |
| 885 | } |
| 886 | DefinedInsnVariablesMap::const_iterator defined_insn_vars_end() const { |
| 887 | return InsnVariableIDs.end(); |
| 888 | } |
| 889 | iterator_range<typename DefinedInsnVariablesMap::const_iterator> |
| 890 | defined_insn_vars() const { |
| 891 | return make_range(defined_insn_vars_begin(), defined_insn_vars_end()); |
| 892 | } |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 893 | |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 894 | MutatableInsnSet::const_iterator mutatable_insns_begin() const { |
| 895 | return MutatableInsns.begin(); |
| 896 | } |
| 897 | MutatableInsnSet::const_iterator mutatable_insns_end() const { |
| 898 | return MutatableInsns.end(); |
| 899 | } |
| 900 | iterator_range<typename MutatableInsnSet::const_iterator> |
| 901 | mutatable_insns() const { |
| 902 | return make_range(mutatable_insns_begin(), mutatable_insns_end()); |
| 903 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 904 | void reserveInsnMatcherForMutation(InstructionMatcher *InsnMatcher) { |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 905 | bool R = MutatableInsns.erase(InsnMatcher); |
| 906 | assert(R && "Reserving a mutatable insn that isn't available"); |
| 907 | (void)R; |
| 908 | } |
| 909 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 910 | action_iterator actions_begin() { return Actions.begin(); } |
| 911 | action_iterator actions_end() { return Actions.end(); } |
| 912 | iterator_range<action_iterator> actions() { |
| 913 | return make_range(actions_begin(), actions_end()); |
| 914 | } |
| 915 | |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 916 | void defineOperand(StringRef SymbolicName, OperandMatcher &OM); |
| 917 | |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 918 | void definePhysRegOperand(Record *Reg, OperandMatcher &OM); |
| 919 | |
Jessica Paquette | 1ed1dd6 | 2019-02-09 00:29:13 +0000 | [diff] [blame] | 920 | Error defineComplexSubOperand(StringRef SymbolicName, Record *ComplexPattern, |
| 921 | unsigned RendererID, unsigned SubOperandID) { |
| 922 | if (ComplexSubOperands.count(SymbolicName)) |
| 923 | return failedImport( |
| 924 | "Complex suboperand referenced more than once (Operand: " + |
| 925 | SymbolicName + ")"); |
| 926 | |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 927 | ComplexSubOperands[SymbolicName] = |
| 928 | std::make_tuple(ComplexPattern, RendererID, SubOperandID); |
Jessica Paquette | 1ed1dd6 | 2019-02-09 00:29:13 +0000 | [diff] [blame] | 929 | |
| 930 | return Error::success(); |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 931 | } |
Jessica Paquette | 1ed1dd6 | 2019-02-09 00:29:13 +0000 | [diff] [blame] | 932 | |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 933 | Optional<DefinedComplexPatternSubOperand> |
| 934 | getComplexSubOperand(StringRef SymbolicName) const { |
| 935 | const auto &I = ComplexSubOperands.find(SymbolicName); |
| 936 | if (I == ComplexSubOperands.end()) |
| 937 | return None; |
| 938 | return I->second; |
| 939 | } |
| 940 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 941 | InstructionMatcher &getInstructionMatcher(StringRef SymbolicName) const; |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 942 | const OperandMatcher &getOperandMatcher(StringRef Name) const; |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 943 | const OperandMatcher &getPhysRegOperandMatcher(Record *) const; |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 944 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 945 | void optimize() override; |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 946 | void emit(MatchTable &Table) override; |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 947 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 948 | /// Compare the priority of this object and B. |
| 949 | /// |
| 950 | /// Returns true if this object is more important than B. |
| 951 | bool isHigherPriorityThan(const RuleMatcher &B) const; |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 952 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 953 | /// Report the maximum number of temporary operands needed by the rule |
| 954 | /// matcher. |
| 955 | unsigned countRendererFns() const; |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 956 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 957 | std::unique_ptr<PredicateMatcher> popFirstCondition() override; |
| 958 | const PredicateMatcher &getFirstCondition() const override; |
Roman Tereshin | 9a9fa49 | 2018-05-23 21:30:16 +0000 | [diff] [blame] | 959 | LLTCodeGen getFirstConditionAsRootType(); |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 960 | bool hasFirstCondition() const override; |
| 961 | unsigned getNumOperands() const; |
Roman Tereshin | 19da667 | 2018-05-22 04:31:50 +0000 | [diff] [blame] | 962 | StringRef getOpcode() const; |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 963 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 964 | // FIXME: Remove this as soon as possible |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 965 | InstructionMatcher &insnmatchers_front() const { return *Matchers.front(); } |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 966 | |
| 967 | unsigned allocateOutputInsnID() { return NextOutputInsnID++; } |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 968 | unsigned allocateTempRegID() { return NextTempRegID++; } |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 969 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 970 | iterator_range<MatchersTy::iterator> insnmatchers() { |
| 971 | return make_range(Matchers.begin(), Matchers.end()); |
| 972 | } |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 973 | bool insnmatchers_empty() const { return Matchers.empty(); } |
| 974 | void insnmatchers_pop_front() { Matchers.erase(Matchers.begin()); } |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 975 | }; |
| 976 | |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 977 | uint64_t RuleMatcher::NextRuleID = 0; |
| 978 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 979 | using action_iterator = RuleMatcher::action_iterator; |
| 980 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 981 | template <class PredicateTy> class PredicateListMatcher { |
| 982 | private: |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 983 | /// Template instantiations should specialize this to return a string to use |
| 984 | /// for the comment emitted when there are no predicates. |
| 985 | std::string getNoPredicateComment() const; |
| 986 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 987 | protected: |
| 988 | using PredicatesTy = std::deque<std::unique_ptr<PredicateTy>>; |
| 989 | PredicatesTy Predicates; |
Roman Tereshin | f0dc9fa | 2018-05-21 22:04:39 +0000 | [diff] [blame] | 990 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 991 | /// Track if the list of predicates was manipulated by one of the optimization |
| 992 | /// methods. |
| 993 | bool Optimized = false; |
| 994 | |
| 995 | public: |
| 996 | /// Construct a new predicate and add it to the matcher. |
| 997 | template <class Kind, class... Args> |
| 998 | Optional<Kind *> addPredicate(Args &&... args); |
| 999 | |
| 1000 | typename PredicatesTy::iterator predicates_begin() { |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 1001 | return Predicates.begin(); |
| 1002 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1003 | typename PredicatesTy::iterator predicates_end() { |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 1004 | return Predicates.end(); |
| 1005 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1006 | iterator_range<typename PredicatesTy::iterator> predicates() { |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1007 | return make_range(predicates_begin(), predicates_end()); |
| 1008 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1009 | typename PredicatesTy::size_type predicates_size() const { |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 1010 | return Predicates.size(); |
| 1011 | } |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 1012 | bool predicates_empty() const { return Predicates.empty(); } |
| 1013 | |
| 1014 | std::unique_ptr<PredicateTy> predicates_pop_front() { |
| 1015 | std::unique_ptr<PredicateTy> Front = std::move(Predicates.front()); |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1016 | Predicates.pop_front(); |
| 1017 | Optimized = true; |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 1018 | return Front; |
| 1019 | } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1020 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1021 | void prependPredicate(std::unique_ptr<PredicateTy> &&Predicate) { |
| 1022 | Predicates.push_front(std::move(Predicate)); |
| 1023 | } |
| 1024 | |
| 1025 | void eraseNullPredicates() { |
| 1026 | const auto NewEnd = |
| 1027 | std::stable_partition(Predicates.begin(), Predicates.end(), |
| 1028 | std::logical_not<std::unique_ptr<PredicateTy>>()); |
| 1029 | if (NewEnd != Predicates.begin()) { |
| 1030 | Predicates.erase(Predicates.begin(), NewEnd); |
| 1031 | Optimized = true; |
| 1032 | } |
| 1033 | } |
| 1034 | |
Daniel Sanders | 9d662d2 | 2017-07-06 10:06:12 +0000 | [diff] [blame] | 1035 | /// Emit MatchTable opcodes that tests whether all the predicates are met. |
Ahmed Bougacha | b67a3ce | 2017-01-26 22:07:37 +0000 | [diff] [blame] | 1036 | template <class... Args> |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1037 | void emitPredicateListOpcodes(MatchTable &Table, Args &&... args) { |
| 1038 | if (Predicates.empty() && !Optimized) { |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 1039 | Table << MatchTable::Comment(getNoPredicateComment()) |
| 1040 | << MatchTable::LineBreak; |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1041 | return; |
| 1042 | } |
| 1043 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1044 | for (const auto &Predicate : predicates()) |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1045 | Predicate->emitPredicateOpcodes(Table, std::forward<Args>(args)...); |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1046 | } |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1047 | }; |
| 1048 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1049 | class PredicateMatcher { |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1050 | public: |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1051 | /// This enum is used for RTTI and also defines the priority that is given to |
| 1052 | /// the predicate when generating the matcher code. Kinds with higher priority |
| 1053 | /// must be tested first. |
| 1054 | /// |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1055 | /// The relative priority of OPM_LLT, OPM_RegBank, and OPM_MBB do not matter |
| 1056 | /// but OPM_Int must have priority over OPM_RegBank since constant integers |
| 1057 | /// are represented by a virtual register defined by a G_CONSTANT instruction. |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1058 | /// |
| 1059 | /// Note: The relative priority between IPM_ and OPM_ does not matter, they |
| 1060 | /// are currently not compared between each other. |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1061 | enum PredicateKind { |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1062 | IPM_Opcode, |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1063 | IPM_NumOperands, |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1064 | IPM_ImmPredicate, |
| 1065 | IPM_AtomicOrderingMMO, |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 1066 | IPM_MemoryLLTSize, |
| 1067 | IPM_MemoryVsLLTSize, |
Matt Arsenault | d00d857 | 2019-07-15 20:59:42 +0000 | [diff] [blame] | 1068 | IPM_MemoryAddressSpace, |
Matt Arsenault | 52c2624 | 2019-07-31 00:14:43 +0000 | [diff] [blame] | 1069 | IPM_MemoryAlignment, |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 1070 | IPM_GenericPredicate, |
Daniel Sanders | 1e4569f | 2017-10-20 20:55:29 +0000 | [diff] [blame] | 1071 | OPM_SameOperand, |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1072 | OPM_ComplexPattern, |
Daniel Sanders | fe12c0f | 2017-07-11 08:57:29 +0000 | [diff] [blame] | 1073 | OPM_IntrinsicID, |
Matt Arsenault | 8ec5c10 | 2019-08-29 01:13:41 +0000 | [diff] [blame] | 1074 | OPM_CmpPredicate, |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 1075 | OPM_Instruction, |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1076 | OPM_Int, |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 1077 | OPM_LiteralInt, |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1078 | OPM_LLT, |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 1079 | OPM_PointerToAny, |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1080 | OPM_RegBank, |
| 1081 | OPM_MBB, |
| 1082 | }; |
| 1083 | |
| 1084 | protected: |
| 1085 | PredicateKind Kind; |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1086 | unsigned InsnVarID; |
| 1087 | unsigned OpIdx; |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1088 | |
| 1089 | public: |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1090 | PredicateMatcher(PredicateKind Kind, unsigned InsnVarID, unsigned OpIdx = ~0) |
| 1091 | : Kind(Kind), InsnVarID(InsnVarID), OpIdx(OpIdx) {} |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1092 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1093 | unsigned getInsnVarID() const { return InsnVarID; } |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1094 | unsigned getOpIdx() const { return OpIdx; } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1095 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1096 | virtual ~PredicateMatcher() = default; |
| 1097 | /// Emit MatchTable opcodes that check the predicate for the given operand. |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1098 | virtual void emitPredicateOpcodes(MatchTable &Table, |
| 1099 | RuleMatcher &Rule) const = 0; |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1100 | |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1101 | PredicateKind getKind() const { return Kind; } |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 1102 | |
| 1103 | virtual bool isIdentical(const PredicateMatcher &B) const { |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 1104 | return B.getKind() == getKind() && InsnVarID == B.InsnVarID && |
| 1105 | OpIdx == B.OpIdx; |
| 1106 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1107 | |
| 1108 | virtual bool isIdenticalDownToValue(const PredicateMatcher &B) const { |
| 1109 | return hasValue() && PredicateMatcher::isIdentical(B); |
| 1110 | } |
| 1111 | |
| 1112 | virtual MatchTableRecord getValue() const { |
| 1113 | assert(hasValue() && "Can not get a value of a value-less predicate!"); |
| 1114 | llvm_unreachable("Not implemented yet"); |
| 1115 | } |
| 1116 | virtual bool hasValue() const { return false; } |
| 1117 | |
| 1118 | /// Report the maximum number of temporary operands needed by the predicate |
| 1119 | /// matcher. |
| 1120 | virtual unsigned countRendererFns() const { return 0; } |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1121 | }; |
| 1122 | |
| 1123 | /// Generates code to check a predicate of an operand. |
| 1124 | /// |
| 1125 | /// Typical predicates include: |
| 1126 | /// * Operand is a particular register. |
| 1127 | /// * Operand is assigned a particular register bank. |
| 1128 | /// * Operand is an MBB. |
| 1129 | class OperandPredicateMatcher : public PredicateMatcher { |
| 1130 | public: |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1131 | OperandPredicateMatcher(PredicateKind Kind, unsigned InsnVarID, |
| 1132 | unsigned OpIdx) |
| 1133 | : PredicateMatcher(Kind, InsnVarID, OpIdx) {} |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1134 | virtual ~OperandPredicateMatcher() {} |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1135 | |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1136 | /// Compare the priority of this object and B. |
| 1137 | /// |
| 1138 | /// Returns true if this object is more important than B. |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 1139 | virtual bool isHigherPriorityThan(const OperandPredicateMatcher &B) const; |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1140 | }; |
| 1141 | |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 1142 | template <> |
| 1143 | std::string |
| 1144 | PredicateListMatcher<OperandPredicateMatcher>::getNoPredicateComment() const { |
| 1145 | return "No operand predicates"; |
| 1146 | } |
| 1147 | |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 1148 | /// Generates code to check that a register operand is defined by the same exact |
| 1149 | /// one as another. |
| 1150 | class SameOperandMatcher : public OperandPredicateMatcher { |
Daniel Sanders | 1e4569f | 2017-10-20 20:55:29 +0000 | [diff] [blame] | 1151 | std::string MatchingName; |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 1152 | |
| 1153 | public: |
Quentin Colombet | eba10cb | 2017-12-18 22:12:13 +0000 | [diff] [blame] | 1154 | SameOperandMatcher(unsigned InsnVarID, unsigned OpIdx, StringRef MatchingName) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1155 | : OperandPredicateMatcher(OPM_SameOperand, InsnVarID, OpIdx), |
| 1156 | MatchingName(MatchingName) {} |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 1157 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1158 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | 1e4569f | 2017-10-20 20:55:29 +0000 | [diff] [blame] | 1159 | return P->getKind() == OPM_SameOperand; |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1162 | void emitPredicateOpcodes(MatchTable &Table, |
| 1163 | RuleMatcher &Rule) const override; |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1164 | |
| 1165 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1166 | return OperandPredicateMatcher::isIdentical(B) && |
| 1167 | MatchingName == cast<SameOperandMatcher>(&B)->MatchingName; |
| 1168 | } |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 1169 | }; |
| 1170 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1171 | /// Generates code to check that an operand is a particular LLT. |
| 1172 | class LLTOperandMatcher : public OperandPredicateMatcher { |
| 1173 | protected: |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 1174 | LLTCodeGen Ty; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1175 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1176 | public: |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1177 | static std::map<LLTCodeGen, unsigned> TypeIDValues; |
| 1178 | |
| 1179 | static void initTypeIDValuesMap() { |
| 1180 | TypeIDValues.clear(); |
| 1181 | |
| 1182 | unsigned ID = 0; |
| 1183 | for (const LLTCodeGen LLTy : KnownTypes) |
| 1184 | TypeIDValues[LLTy] = ID++; |
| 1185 | } |
| 1186 | |
Quentin Colombet | eba10cb | 2017-12-18 22:12:13 +0000 | [diff] [blame] | 1187 | LLTOperandMatcher(unsigned InsnVarID, unsigned OpIdx, const LLTCodeGen &Ty) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1188 | : OperandPredicateMatcher(OPM_LLT, InsnVarID, OpIdx), Ty(Ty) { |
Daniel Sanders | 032e7f2 | 2017-08-17 13:18:35 +0000 | [diff] [blame] | 1189 | KnownTypes.insert(Ty); |
| 1190 | } |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1191 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1192 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1193 | return P->getKind() == OPM_LLT; |
| 1194 | } |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 1195 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1196 | return OperandPredicateMatcher::isIdentical(B) && |
| 1197 | Ty == cast<LLTOperandMatcher>(&B)->Ty; |
| 1198 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1199 | MatchTableRecord getValue() const override { |
| 1200 | const auto VI = TypeIDValues.find(Ty); |
| 1201 | if (VI == TypeIDValues.end()) |
| 1202 | return MatchTable::NamedValue(getTy().getCxxEnumValue()); |
| 1203 | return MatchTable::NamedValue(getTy().getCxxEnumValue(), VI->second); |
| 1204 | } |
| 1205 | bool hasValue() const override { |
| 1206 | if (TypeIDValues.size() != KnownTypes.size()) |
| 1207 | initTypeIDValuesMap(); |
| 1208 | return TypeIDValues.count(Ty); |
| 1209 | } |
| 1210 | |
| 1211 | LLTCodeGen getTy() const { return Ty; } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1212 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1213 | void emitPredicateOpcodes(MatchTable &Table, |
| 1214 | RuleMatcher &Rule) const override { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1215 | Table << MatchTable::Opcode("GIM_CheckType") << MatchTable::Comment("MI") |
| 1216 | << MatchTable::IntValue(InsnVarID) << MatchTable::Comment("Op") |
| 1217 | << MatchTable::IntValue(OpIdx) << MatchTable::Comment("Type") |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1218 | << getValue() << MatchTable::LineBreak; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1219 | } |
| 1220 | }; |
| 1221 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1222 | std::map<LLTCodeGen, unsigned> LLTOperandMatcher::TypeIDValues; |
| 1223 | |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 1224 | /// Generates code to check that an operand is a pointer to any address space. |
| 1225 | /// |
| 1226 | /// In SelectionDAG, the types did not describe pointers or address spaces. As a |
| 1227 | /// result, iN is used to describe a pointer of N bits to any address space and |
| 1228 | /// PatFrag predicates are typically used to constrain the address space. There's |
| 1229 | /// no reliable means to derive the missing type information from the pattern so |
| 1230 | /// imported rules must test the components of a pointer separately. |
| 1231 | /// |
Daniel Sanders | ea8711b | 2017-10-16 03:36:29 +0000 | [diff] [blame] | 1232 | /// If SizeInBits is zero, then the pointer size will be obtained from the |
| 1233 | /// subtarget. |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 1234 | class PointerToAnyOperandMatcher : public OperandPredicateMatcher { |
| 1235 | protected: |
| 1236 | unsigned SizeInBits; |
| 1237 | |
| 1238 | public: |
Quentin Colombet | eba10cb | 2017-12-18 22:12:13 +0000 | [diff] [blame] | 1239 | PointerToAnyOperandMatcher(unsigned InsnVarID, unsigned OpIdx, |
| 1240 | unsigned SizeInBits) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1241 | : OperandPredicateMatcher(OPM_PointerToAny, InsnVarID, OpIdx), |
| 1242 | SizeInBits(SizeInBits) {} |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 1243 | |
| 1244 | static bool classof(const OperandPredicateMatcher *P) { |
| 1245 | return P->getKind() == OPM_PointerToAny; |
| 1246 | } |
| 1247 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1248 | void emitPredicateOpcodes(MatchTable &Table, |
| 1249 | RuleMatcher &Rule) const override { |
| 1250 | Table << MatchTable::Opcode("GIM_CheckPointerToAny") |
| 1251 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1252 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) |
| 1253 | << MatchTable::Comment("SizeInBits") |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 1254 | << MatchTable::IntValue(SizeInBits) << MatchTable::LineBreak; |
| 1255 | } |
| 1256 | }; |
| 1257 | |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1258 | /// Generates code to check that an operand is a particular target constant. |
| 1259 | class ComplexPatternOperandMatcher : public OperandPredicateMatcher { |
| 1260 | protected: |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1261 | const OperandMatcher &Operand; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1262 | const Record &TheDef; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1263 | |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1264 | unsigned getAllocatedTemporariesBaseID() const; |
| 1265 | |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1266 | public: |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 1267 | bool isIdentical(const PredicateMatcher &B) const override { return false; } |
| 1268 | |
Quentin Colombet | eba10cb | 2017-12-18 22:12:13 +0000 | [diff] [blame] | 1269 | ComplexPatternOperandMatcher(unsigned InsnVarID, unsigned OpIdx, |
| 1270 | const OperandMatcher &Operand, |
| 1271 | const Record &TheDef) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1272 | : OperandPredicateMatcher(OPM_ComplexPattern, InsnVarID, OpIdx), |
| 1273 | Operand(Operand), TheDef(TheDef) {} |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +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 | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1276 | return P->getKind() == OPM_ComplexPattern; |
| 1277 | } |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 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 | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 1281 | unsigned ID = getAllocatedTemporariesBaseID(); |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1282 | Table << MatchTable::Opcode("GIM_CheckComplexPattern") |
| 1283 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1284 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) |
| 1285 | << MatchTable::Comment("Renderer") << MatchTable::IntValue(ID) |
| 1286 | << MatchTable::NamedValue(("GICP_" + TheDef.getName()).str()) |
| 1287 | << MatchTable::LineBreak; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1288 | } |
| 1289 | |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 1290 | unsigned countRendererFns() const override { |
| 1291 | return 1; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1292 | } |
| 1293 | }; |
| 1294 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1295 | /// Generates code to check that an operand is in a particular register bank. |
| 1296 | class RegisterBankOperandMatcher : public OperandPredicateMatcher { |
| 1297 | protected: |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1298 | const CodeGenRegisterClass &RC; |
| 1299 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1300 | public: |
Quentin Colombet | eba10cb | 2017-12-18 22:12:13 +0000 | [diff] [blame] | 1301 | RegisterBankOperandMatcher(unsigned InsnVarID, unsigned OpIdx, |
| 1302 | const CodeGenRegisterClass &RC) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1303 | : OperandPredicateMatcher(OPM_RegBank, InsnVarID, OpIdx), RC(RC) {} |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1304 | |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 1305 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1306 | return OperandPredicateMatcher::isIdentical(B) && |
| 1307 | RC.getDef() == cast<RegisterBankOperandMatcher>(&B)->RC.getDef(); |
| 1308 | } |
| 1309 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1310 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1311 | return P->getKind() == OPM_RegBank; |
| 1312 | } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1313 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1314 | void emitPredicateOpcodes(MatchTable &Table, |
| 1315 | RuleMatcher &Rule) const override { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1316 | Table << MatchTable::Opcode("GIM_CheckRegBankForClass") |
| 1317 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1318 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) |
| 1319 | << MatchTable::Comment("RC") |
| 1320 | << MatchTable::NamedValue(RC.getQualifiedName() + "RegClassID") |
| 1321 | << MatchTable::LineBreak; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1322 | } |
| 1323 | }; |
| 1324 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1325 | /// Generates code to check that an operand is a basic block. |
| 1326 | class MBBOperandMatcher : public OperandPredicateMatcher { |
| 1327 | public: |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1328 | MBBOperandMatcher(unsigned InsnVarID, unsigned OpIdx) |
| 1329 | : OperandPredicateMatcher(OPM_MBB, InsnVarID, OpIdx) {} |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1330 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1331 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1332 | return P->getKind() == OPM_MBB; |
| 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_CheckIsMBB") << MatchTable::Comment("MI") |
| 1338 | << MatchTable::IntValue(InsnVarID) << MatchTable::Comment("Op") |
| 1339 | << MatchTable::IntValue(OpIdx) << MatchTable::LineBreak; |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1340 | } |
| 1341 | }; |
| 1342 | |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 1343 | /// Generates code to check that an operand is a G_CONSTANT with a particular |
| 1344 | /// int. |
| 1345 | class ConstantIntOperandMatcher : public OperandPredicateMatcher { |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1346 | protected: |
| 1347 | int64_t Value; |
| 1348 | |
| 1349 | public: |
Quentin Colombet | eba10cb | 2017-12-18 22:12:13 +0000 | [diff] [blame] | 1350 | ConstantIntOperandMatcher(unsigned InsnVarID, unsigned OpIdx, int64_t Value) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1351 | : OperandPredicateMatcher(OPM_Int, InsnVarID, OpIdx), Value(Value) {} |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1352 | |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 1353 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1354 | return OperandPredicateMatcher::isIdentical(B) && |
| 1355 | Value == cast<ConstantIntOperandMatcher>(&B)->Value; |
| 1356 | } |
| 1357 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1358 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1359 | return P->getKind() == OPM_Int; |
| 1360 | } |
| 1361 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1362 | void emitPredicateOpcodes(MatchTable &Table, |
| 1363 | RuleMatcher &Rule) const override { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1364 | Table << MatchTable::Opcode("GIM_CheckConstantInt") |
| 1365 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1366 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) |
| 1367 | << MatchTable::IntValue(Value) << MatchTable::LineBreak; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1368 | } |
| 1369 | }; |
| 1370 | |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 1371 | /// Generates code to check that an operand is a raw int (where MO.isImm() or |
| 1372 | /// MO.isCImm() is true). |
| 1373 | class LiteralIntOperandMatcher : public OperandPredicateMatcher { |
| 1374 | protected: |
| 1375 | int64_t Value; |
| 1376 | |
| 1377 | public: |
Quentin Colombet | eba10cb | 2017-12-18 22:12:13 +0000 | [diff] [blame] | 1378 | LiteralIntOperandMatcher(unsigned InsnVarID, unsigned OpIdx, int64_t Value) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1379 | : OperandPredicateMatcher(OPM_LiteralInt, InsnVarID, OpIdx), |
| 1380 | Value(Value) {} |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 1381 | |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 1382 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1383 | return OperandPredicateMatcher::isIdentical(B) && |
| 1384 | Value == cast<LiteralIntOperandMatcher>(&B)->Value; |
| 1385 | } |
| 1386 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1387 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 1388 | return P->getKind() == OPM_LiteralInt; |
| 1389 | } |
| 1390 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1391 | void emitPredicateOpcodes(MatchTable &Table, |
| 1392 | RuleMatcher &Rule) const override { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1393 | Table << MatchTable::Opcode("GIM_CheckLiteralInt") |
| 1394 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1395 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) |
| 1396 | << MatchTable::IntValue(Value) << MatchTable::LineBreak; |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 1397 | } |
| 1398 | }; |
| 1399 | |
Matt Arsenault | 8ec5c10 | 2019-08-29 01:13:41 +0000 | [diff] [blame] | 1400 | /// Generates code to check that an operand is an CmpInst predicate |
| 1401 | class CmpPredicateOperandMatcher : public OperandPredicateMatcher { |
| 1402 | protected: |
| 1403 | std::string PredName; |
| 1404 | |
| 1405 | public: |
| 1406 | CmpPredicateOperandMatcher(unsigned InsnVarID, unsigned OpIdx, |
| 1407 | std::string P) |
| 1408 | : OperandPredicateMatcher(OPM_CmpPredicate, InsnVarID, OpIdx), PredName(P) {} |
| 1409 | |
| 1410 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1411 | return OperandPredicateMatcher::isIdentical(B) && |
| 1412 | PredName == cast<CmpPredicateOperandMatcher>(&B)->PredName; |
| 1413 | } |
| 1414 | |
| 1415 | static bool classof(const PredicateMatcher *P) { |
| 1416 | return P->getKind() == OPM_CmpPredicate; |
| 1417 | } |
| 1418 | |
| 1419 | void emitPredicateOpcodes(MatchTable &Table, |
| 1420 | RuleMatcher &Rule) const override { |
| 1421 | Table << MatchTable::Opcode("GIM_CheckCmpPredicate") |
| 1422 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1423 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) |
| 1424 | << MatchTable::Comment("Predicate") |
| 1425 | << MatchTable::NamedValue("CmpInst", PredName) |
| 1426 | << MatchTable::LineBreak; |
| 1427 | } |
| 1428 | }; |
| 1429 | |
Daniel Sanders | fe12c0f | 2017-07-11 08:57:29 +0000 | [diff] [blame] | 1430 | /// Generates code to check that an operand is an intrinsic ID. |
| 1431 | class IntrinsicIDOperandMatcher : public OperandPredicateMatcher { |
| 1432 | protected: |
| 1433 | const CodeGenIntrinsic *II; |
| 1434 | |
| 1435 | public: |
Quentin Colombet | eba10cb | 2017-12-18 22:12:13 +0000 | [diff] [blame] | 1436 | IntrinsicIDOperandMatcher(unsigned InsnVarID, unsigned OpIdx, |
| 1437 | const CodeGenIntrinsic *II) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1438 | : OperandPredicateMatcher(OPM_IntrinsicID, InsnVarID, OpIdx), II(II) {} |
Daniel Sanders | fe12c0f | 2017-07-11 08:57:29 +0000 | [diff] [blame] | 1439 | |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 1440 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1441 | return OperandPredicateMatcher::isIdentical(B) && |
| 1442 | II == cast<IntrinsicIDOperandMatcher>(&B)->II; |
| 1443 | } |
| 1444 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1445 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | fe12c0f | 2017-07-11 08:57:29 +0000 | [diff] [blame] | 1446 | return P->getKind() == OPM_IntrinsicID; |
| 1447 | } |
| 1448 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1449 | void emitPredicateOpcodes(MatchTable &Table, |
| 1450 | RuleMatcher &Rule) const override { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1451 | Table << MatchTable::Opcode("GIM_CheckIntrinsicID") |
| 1452 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1453 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) |
| 1454 | << MatchTable::NamedValue("Intrinsic::" + II->EnumName) |
| 1455 | << MatchTable::LineBreak; |
Daniel Sanders | fe12c0f | 2017-07-11 08:57:29 +0000 | [diff] [blame] | 1456 | } |
| 1457 | }; |
| 1458 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1459 | /// Generates code to check that a set of predicates match for a particular |
| 1460 | /// operand. |
| 1461 | class OperandMatcher : public PredicateListMatcher<OperandPredicateMatcher> { |
| 1462 | protected: |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 1463 | InstructionMatcher &Insn; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1464 | unsigned OpIdx; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1465 | std::string SymbolicName; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1466 | |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1467 | /// The index of the first temporary variable allocated to this operand. The |
| 1468 | /// number of allocated temporaries can be found with |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 1469 | /// countRendererFns(). |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1470 | unsigned AllocatedTemporariesBaseID; |
| 1471 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1472 | public: |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 1473 | OperandMatcher(InstructionMatcher &Insn, unsigned OpIdx, |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1474 | const std::string &SymbolicName, |
| 1475 | unsigned AllocatedTemporariesBaseID) |
| 1476 | : Insn(Insn), OpIdx(OpIdx), SymbolicName(SymbolicName), |
| 1477 | AllocatedTemporariesBaseID(AllocatedTemporariesBaseID) {} |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1478 | |
| 1479 | bool hasSymbolicName() const { return !SymbolicName.empty(); } |
| 1480 | const StringRef getSymbolicName() const { return SymbolicName; } |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 1481 | void setSymbolicName(StringRef Name) { |
| 1482 | assert(SymbolicName.empty() && "Operand already has a symbolic name"); |
| 1483 | SymbolicName = Name; |
| 1484 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1485 | |
| 1486 | /// Construct a new operand predicate and add it to the matcher. |
| 1487 | template <class Kind, class... Args> |
| 1488 | Optional<Kind *> addPredicate(Args &&... args) { |
| 1489 | if (isSameAsAnotherOperand()) |
| 1490 | return None; |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 1491 | Predicates.emplace_back(std::make_unique<Kind>( |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1492 | getInsnVarID(), getOpIdx(), std::forward<Args>(args)...)); |
| 1493 | return static_cast<Kind *>(Predicates.back().get()); |
| 1494 | } |
| 1495 | |
| 1496 | unsigned getOpIdx() const { return OpIdx; } |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1497 | unsigned getInsnVarID() const; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1498 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 1499 | std::string getOperandExpr(unsigned InsnVarID) const { |
| 1500 | return "State.MIs[" + llvm::to_string(InsnVarID) + "]->getOperand(" + |
| 1501 | llvm::to_string(OpIdx) + ")"; |
Daniel Sanders | e604ef5 | 2017-02-20 15:30:43 +0000 | [diff] [blame] | 1502 | } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1503 | |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 1504 | InstructionMatcher &getInstructionMatcher() const { return Insn; } |
| 1505 | |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 1506 | Error addTypeCheckPredicate(const TypeSetByHwMode &VTy, |
Reid Kleckner | cfdd4a2 | 2017-10-16 20:31:16 +0000 | [diff] [blame] | 1507 | bool OperandIsAPointer); |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 1508 | |
Daniel Sanders | 9d662d2 | 2017-07-06 10:06:12 +0000 | [diff] [blame] | 1509 | /// Emit MatchTable opcodes that test whether the instruction named in |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 1510 | /// InsnVarID matches all the predicates and all the operands. |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1511 | void emitPredicateOpcodes(MatchTable &Table, RuleMatcher &Rule) { |
| 1512 | if (!Optimized) { |
| 1513 | std::string Comment; |
| 1514 | raw_string_ostream CommentOS(Comment); |
| 1515 | CommentOS << "MIs[" << getInsnVarID() << "] "; |
| 1516 | if (SymbolicName.empty()) |
| 1517 | CommentOS << "Operand " << OpIdx; |
| 1518 | else |
| 1519 | CommentOS << SymbolicName; |
| 1520 | Table << MatchTable::Comment(CommentOS.str()) << MatchTable::LineBreak; |
| 1521 | } |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1522 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1523 | emitPredicateListOpcodes(Table, Rule); |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1524 | } |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1525 | |
| 1526 | /// Compare the priority of this object and B. |
| 1527 | /// |
| 1528 | /// Returns true if this object is more important than B. |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1529 | bool isHigherPriorityThan(OperandMatcher &B) { |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1530 | // Operand matchers involving more predicates have higher priority. |
| 1531 | if (predicates_size() > B.predicates_size()) |
| 1532 | return true; |
| 1533 | if (predicates_size() < B.predicates_size()) |
| 1534 | return false; |
| 1535 | |
| 1536 | // This assumes that predicates are added in a consistent order. |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1537 | for (auto &&Predicate : zip(predicates(), B.predicates())) { |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1538 | if (std::get<0>(Predicate)->isHigherPriorityThan(*std::get<1>(Predicate))) |
| 1539 | return true; |
| 1540 | if (std::get<1>(Predicate)->isHigherPriorityThan(*std::get<0>(Predicate))) |
| 1541 | return false; |
| 1542 | } |
| 1543 | |
| 1544 | return false; |
| 1545 | }; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1546 | |
| 1547 | /// Report the maximum number of temporary operands needed by the operand |
| 1548 | /// matcher. |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1549 | unsigned countRendererFns() { |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1550 | return std::accumulate( |
| 1551 | predicates().begin(), predicates().end(), 0, |
| 1552 | [](unsigned A, |
| 1553 | const std::unique_ptr<OperandPredicateMatcher> &Predicate) { |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 1554 | return A + Predicate->countRendererFns(); |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1555 | }); |
| 1556 | } |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1557 | |
| 1558 | unsigned getAllocatedTemporariesBaseID() const { |
| 1559 | return AllocatedTemporariesBaseID; |
| 1560 | } |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 1561 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1562 | bool isSameAsAnotherOperand() { |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 1563 | for (const auto &Predicate : predicates()) |
| 1564 | if (isa<SameOperandMatcher>(Predicate)) |
| 1565 | return true; |
| 1566 | return false; |
| 1567 | } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1568 | }; |
| 1569 | |
Reid Kleckner | cfdd4a2 | 2017-10-16 20:31:16 +0000 | [diff] [blame] | 1570 | Error OperandMatcher::addTypeCheckPredicate(const TypeSetByHwMode &VTy, |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1571 | bool OperandIsAPointer) { |
Reid Kleckner | cfdd4a2 | 2017-10-16 20:31:16 +0000 | [diff] [blame] | 1572 | if (!VTy.isMachineValueType()) |
| 1573 | return failedImport("unsupported typeset"); |
| 1574 | |
| 1575 | if (VTy.getMachineValueType() == MVT::iPTR && OperandIsAPointer) { |
| 1576 | addPredicate<PointerToAnyOperandMatcher>(0); |
| 1577 | return Error::success(); |
| 1578 | } |
| 1579 | |
| 1580 | auto OpTyOrNone = MVTToLLT(VTy.getMachineValueType().SimpleTy); |
| 1581 | if (!OpTyOrNone) |
| 1582 | return failedImport("unsupported type"); |
| 1583 | |
| 1584 | if (OperandIsAPointer) |
| 1585 | addPredicate<PointerToAnyOperandMatcher>(OpTyOrNone->get().getSizeInBits()); |
Tom Stellard | 9ad714f | 2019-02-20 19:43:47 +0000 | [diff] [blame] | 1586 | else if (VTy.isPointer()) |
| 1587 | addPredicate<LLTOperandMatcher>(LLT::pointer(VTy.getPtrAddrSpace(), |
| 1588 | OpTyOrNone->get().getSizeInBits())); |
Reid Kleckner | cfdd4a2 | 2017-10-16 20:31:16 +0000 | [diff] [blame] | 1589 | else |
| 1590 | addPredicate<LLTOperandMatcher>(*OpTyOrNone); |
| 1591 | return Error::success(); |
| 1592 | } |
| 1593 | |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1594 | unsigned ComplexPatternOperandMatcher::getAllocatedTemporariesBaseID() const { |
| 1595 | return Operand.getAllocatedTemporariesBaseID(); |
| 1596 | } |
| 1597 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1598 | /// Generates code to check a predicate on an instruction. |
| 1599 | /// |
| 1600 | /// Typical predicates include: |
| 1601 | /// * The opcode of the instruction is a particular value. |
| 1602 | /// * The nsw/nuw flag is/isn't set. |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1603 | class InstructionPredicateMatcher : public PredicateMatcher { |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1604 | public: |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1605 | InstructionPredicateMatcher(PredicateKind Kind, unsigned InsnVarID) |
| 1606 | : PredicateMatcher(Kind, InsnVarID) {} |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1607 | virtual ~InstructionPredicateMatcher() {} |
| 1608 | |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1609 | /// Compare the priority of this object and B. |
| 1610 | /// |
| 1611 | /// Returns true if this object is more important than B. |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 1612 | virtual bool |
| 1613 | isHigherPriorityThan(const InstructionPredicateMatcher &B) const { |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1614 | return Kind < B.Kind; |
| 1615 | }; |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1616 | }; |
| 1617 | |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 1618 | template <> |
| 1619 | std::string |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1620 | PredicateListMatcher<PredicateMatcher>::getNoPredicateComment() const { |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 1621 | return "No instruction predicates"; |
| 1622 | } |
| 1623 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1624 | /// Generates code to check the opcode of an instruction. |
| 1625 | class InstructionOpcodeMatcher : public InstructionPredicateMatcher { |
| 1626 | protected: |
| 1627 | const CodeGenInstruction *I; |
| 1628 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1629 | static DenseMap<const CodeGenInstruction *, unsigned> OpcodeValues; |
| 1630 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1631 | public: |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1632 | static void initOpcodeValuesMap(const CodeGenTarget &Target) { |
| 1633 | OpcodeValues.clear(); |
| 1634 | |
| 1635 | unsigned OpcodeValue = 0; |
| 1636 | for (const CodeGenInstruction *I : Target.getInstructionsByEnumValue()) |
| 1637 | OpcodeValues[I] = OpcodeValue++; |
| 1638 | } |
| 1639 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1640 | InstructionOpcodeMatcher(unsigned InsnVarID, const CodeGenInstruction *I) |
| 1641 | : InstructionPredicateMatcher(IPM_Opcode, InsnVarID), I(I) {} |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1642 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1643 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1644 | return P->getKind() == IPM_Opcode; |
| 1645 | } |
| 1646 | |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 1647 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1648 | return InstructionPredicateMatcher::isIdentical(B) && |
| 1649 | I == cast<InstructionOpcodeMatcher>(&B)->I; |
| 1650 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1651 | MatchTableRecord getValue() const override { |
| 1652 | const auto VI = OpcodeValues.find(I); |
| 1653 | if (VI != OpcodeValues.end()) |
| 1654 | return MatchTable::NamedValue(I->Namespace, I->TheDef->getName(), |
| 1655 | VI->second); |
| 1656 | return MatchTable::NamedValue(I->Namespace, I->TheDef->getName()); |
| 1657 | } |
| 1658 | bool hasValue() const override { return OpcodeValues.count(I); } |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 1659 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1660 | void emitPredicateOpcodes(MatchTable &Table, |
| 1661 | RuleMatcher &Rule) const override { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1662 | Table << MatchTable::Opcode("GIM_CheckOpcode") << MatchTable::Comment("MI") |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1663 | << MatchTable::IntValue(InsnVarID) << getValue() |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1664 | << MatchTable::LineBreak; |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1665 | } |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1666 | |
| 1667 | /// Compare the priority of this object and B. |
| 1668 | /// |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1669 | /// Returns true if this object is more important than B. |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 1670 | bool |
| 1671 | isHigherPriorityThan(const InstructionPredicateMatcher &B) const override { |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1672 | if (InstructionPredicateMatcher::isHigherPriorityThan(B)) |
| 1673 | return true; |
| 1674 | if (B.InstructionPredicateMatcher::isHigherPriorityThan(*this)) |
| 1675 | return false; |
| 1676 | |
| 1677 | // Prioritize opcodes for cosmetic reasons in the generated source. Although |
| 1678 | // this is cosmetic at the moment, we may want to drive a similar ordering |
| 1679 | // using instruction frequency information to improve compile time. |
| 1680 | if (const InstructionOpcodeMatcher *BO = |
| 1681 | dyn_cast<InstructionOpcodeMatcher>(&B)) |
| 1682 | return I->TheDef->getName() < BO->I->TheDef->getName(); |
| 1683 | |
| 1684 | return false; |
| 1685 | }; |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 1686 | |
| 1687 | bool isConstantInstruction() const { |
| 1688 | return I->TheDef->getName() == "G_CONSTANT"; |
| 1689 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1690 | |
Roman Tereshin | 19da667 | 2018-05-22 04:31:50 +0000 | [diff] [blame] | 1691 | StringRef getOpcode() const { return I->TheDef->getName(); } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1692 | unsigned getNumOperands() const { return I->Operands.size(); } |
| 1693 | |
| 1694 | StringRef getOperandType(unsigned OpIdx) const { |
| 1695 | return I->Operands[OpIdx].OperandType; |
| 1696 | } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1697 | }; |
| 1698 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1699 | DenseMap<const CodeGenInstruction *, unsigned> |
| 1700 | InstructionOpcodeMatcher::OpcodeValues; |
| 1701 | |
Roman Tereshin | 19da667 | 2018-05-22 04:31:50 +0000 | [diff] [blame] | 1702 | class InstructionNumOperandsMatcher final : public InstructionPredicateMatcher { |
| 1703 | unsigned NumOperands = 0; |
| 1704 | |
| 1705 | public: |
| 1706 | InstructionNumOperandsMatcher(unsigned InsnVarID, unsigned NumOperands) |
| 1707 | : InstructionPredicateMatcher(IPM_NumOperands, InsnVarID), |
| 1708 | NumOperands(NumOperands) {} |
| 1709 | |
| 1710 | static bool classof(const PredicateMatcher *P) { |
| 1711 | return P->getKind() == IPM_NumOperands; |
| 1712 | } |
| 1713 | |
| 1714 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1715 | return InstructionPredicateMatcher::isIdentical(B) && |
| 1716 | NumOperands == cast<InstructionNumOperandsMatcher>(&B)->NumOperands; |
| 1717 | } |
| 1718 | |
| 1719 | void emitPredicateOpcodes(MatchTable &Table, |
| 1720 | RuleMatcher &Rule) const override { |
| 1721 | Table << MatchTable::Opcode("GIM_CheckNumOperands") |
| 1722 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1723 | << MatchTable::Comment("Expected") |
| 1724 | << MatchTable::IntValue(NumOperands) << MatchTable::LineBreak; |
| 1725 | } |
| 1726 | }; |
| 1727 | |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 1728 | /// Generates code to check that this instruction is a constant whose value |
| 1729 | /// meets an immediate predicate. |
| 1730 | /// |
| 1731 | /// Immediates are slightly odd since they are typically used like an operand |
| 1732 | /// but are represented as an operator internally. We typically write simm8:$src |
| 1733 | /// in a tablegen pattern, but this is just syntactic sugar for |
| 1734 | /// (imm:i32)<<P:Predicate_simm8>>:$imm which more directly describes the nodes |
| 1735 | /// that will be matched and the predicate (which is attached to the imm |
| 1736 | /// operator) that will be tested. In SelectionDAG this describes a |
| 1737 | /// ConstantSDNode whose internal value will be tested using the simm8 predicate. |
| 1738 | /// |
| 1739 | /// The corresponding GlobalISel representation is %1 = G_CONSTANT iN Value. In |
| 1740 | /// this representation, the immediate could be tested with an |
| 1741 | /// InstructionMatcher, InstructionOpcodeMatcher, OperandMatcher, and a |
| 1742 | /// OperandPredicateMatcher-subclass to check the Value meets the predicate but |
| 1743 | /// there are two implementation issues with producing that matcher |
| 1744 | /// configuration from the SelectionDAG pattern: |
| 1745 | /// * ImmLeaf is a PatFrag whose root is an InstructionMatcher. This means that |
| 1746 | /// were we to sink the immediate predicate to the operand we would have to |
| 1747 | /// have two partial implementations of PatFrag support, one for immediates |
| 1748 | /// and one for non-immediates. |
| 1749 | /// * At the point we handle the predicate, the OperandMatcher hasn't been |
| 1750 | /// created yet. If we were to sink the predicate to the OperandMatcher we |
| 1751 | /// would also have to complicate (or duplicate) the code that descends and |
| 1752 | /// creates matchers for the subtree. |
| 1753 | /// Overall, it's simpler to handle it in the place it was found. |
| 1754 | class InstructionImmPredicateMatcher : public InstructionPredicateMatcher { |
| 1755 | protected: |
| 1756 | TreePredicateFn Predicate; |
| 1757 | |
| 1758 | public: |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1759 | InstructionImmPredicateMatcher(unsigned InsnVarID, |
| 1760 | const TreePredicateFn &Predicate) |
| 1761 | : InstructionPredicateMatcher(IPM_ImmPredicate, InsnVarID), |
| 1762 | Predicate(Predicate) {} |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 1763 | |
Quentin Colombet | 893e0f1 | 2017-12-15 23:24:39 +0000 | [diff] [blame] | 1764 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1765 | return InstructionPredicateMatcher::isIdentical(B) && |
| 1766 | Predicate.getOrigPatFragRecord() == |
| 1767 | cast<InstructionImmPredicateMatcher>(&B) |
| 1768 | ->Predicate.getOrigPatFragRecord(); |
| 1769 | } |
| 1770 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 1771 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 1772 | return P->getKind() == IPM_ImmPredicate; |
| 1773 | } |
| 1774 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1775 | void emitPredicateOpcodes(MatchTable &Table, |
| 1776 | RuleMatcher &Rule) const override { |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 1777 | Table << MatchTable::Opcode(getMatchOpcodeForPredicate(Predicate)) |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 1778 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1779 | << MatchTable::Comment("Predicate") |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 1780 | << MatchTable::NamedValue(getEnumNameForPredicate(Predicate)) |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 1781 | << MatchTable::LineBreak; |
| 1782 | } |
| 1783 | }; |
| 1784 | |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 1785 | /// Generates code to check that a memory instruction has a atomic ordering |
| 1786 | /// MachineMemoryOperand. |
| 1787 | class AtomicOrderingMMOPredicateMatcher : public InstructionPredicateMatcher { |
Daniel Sanders | 0c43b3a | 2017-11-30 21:05:59 +0000 | [diff] [blame] | 1788 | public: |
| 1789 | enum AOComparator { |
| 1790 | AO_Exactly, |
| 1791 | AO_OrStronger, |
| 1792 | AO_WeakerThan, |
| 1793 | }; |
| 1794 | |
| 1795 | protected: |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 1796 | StringRef Order; |
Daniel Sanders | 0c43b3a | 2017-11-30 21:05:59 +0000 | [diff] [blame] | 1797 | AOComparator Comparator; |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 1798 | |
Daniel Sanders | 39690bd | 2017-10-15 02:41:12 +0000 | [diff] [blame] | 1799 | public: |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1800 | AtomicOrderingMMOPredicateMatcher(unsigned InsnVarID, StringRef Order, |
Daniel Sanders | 0c43b3a | 2017-11-30 21:05:59 +0000 | [diff] [blame] | 1801 | AOComparator Comparator = AO_Exactly) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1802 | : InstructionPredicateMatcher(IPM_AtomicOrderingMMO, InsnVarID), |
| 1803 | Order(Order), Comparator(Comparator) {} |
Daniel Sanders | 39690bd | 2017-10-15 02:41:12 +0000 | [diff] [blame] | 1804 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1805 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 1806 | return P->getKind() == IPM_AtomicOrderingMMO; |
Daniel Sanders | 39690bd | 2017-10-15 02:41:12 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 1809 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1810 | if (!InstructionPredicateMatcher::isIdentical(B)) |
| 1811 | return false; |
| 1812 | const auto &R = *cast<AtomicOrderingMMOPredicateMatcher>(&B); |
| 1813 | return Order == R.Order && Comparator == R.Comparator; |
| 1814 | } |
| 1815 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 1816 | void emitPredicateOpcodes(MatchTable &Table, |
| 1817 | RuleMatcher &Rule) const override { |
Daniel Sanders | 0c43b3a | 2017-11-30 21:05:59 +0000 | [diff] [blame] | 1818 | StringRef Opcode = "GIM_CheckAtomicOrdering"; |
| 1819 | |
| 1820 | if (Comparator == AO_OrStronger) |
| 1821 | Opcode = "GIM_CheckAtomicOrderingOrStrongerThan"; |
| 1822 | if (Comparator == AO_WeakerThan) |
| 1823 | Opcode = "GIM_CheckAtomicOrderingWeakerThan"; |
| 1824 | |
| 1825 | Table << MatchTable::Opcode(Opcode) << MatchTable::Comment("MI") |
| 1826 | << MatchTable::IntValue(InsnVarID) << MatchTable::Comment("Order") |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 1827 | << MatchTable::NamedValue(("(int64_t)AtomicOrdering::" + Order).str()) |
Daniel Sanders | 39690bd | 2017-10-15 02:41:12 +0000 | [diff] [blame] | 1828 | << MatchTable::LineBreak; |
| 1829 | } |
| 1830 | }; |
| 1831 | |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 1832 | /// Generates code to check that the size of an MMO is exactly N bytes. |
| 1833 | class MemorySizePredicateMatcher : public InstructionPredicateMatcher { |
| 1834 | protected: |
| 1835 | unsigned MMOIdx; |
| 1836 | uint64_t Size; |
| 1837 | |
| 1838 | public: |
| 1839 | MemorySizePredicateMatcher(unsigned InsnVarID, unsigned MMOIdx, unsigned Size) |
| 1840 | : InstructionPredicateMatcher(IPM_MemoryLLTSize, InsnVarID), |
| 1841 | MMOIdx(MMOIdx), Size(Size) {} |
| 1842 | |
| 1843 | static bool classof(const PredicateMatcher *P) { |
| 1844 | return P->getKind() == IPM_MemoryLLTSize; |
| 1845 | } |
| 1846 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1847 | return InstructionPredicateMatcher::isIdentical(B) && |
| 1848 | MMOIdx == cast<MemorySizePredicateMatcher>(&B)->MMOIdx && |
| 1849 | Size == cast<MemorySizePredicateMatcher>(&B)->Size; |
| 1850 | } |
| 1851 | |
| 1852 | void emitPredicateOpcodes(MatchTable &Table, |
| 1853 | RuleMatcher &Rule) const override { |
| 1854 | Table << MatchTable::Opcode("GIM_CheckMemorySizeEqualTo") |
| 1855 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1856 | << MatchTable::Comment("MMO") << MatchTable::IntValue(MMOIdx) |
| 1857 | << MatchTable::Comment("Size") << MatchTable::IntValue(Size) |
| 1858 | << MatchTable::LineBreak; |
| 1859 | } |
| 1860 | }; |
| 1861 | |
Matt Arsenault | d00d857 | 2019-07-15 20:59:42 +0000 | [diff] [blame] | 1862 | class MemoryAddressSpacePredicateMatcher : public InstructionPredicateMatcher { |
| 1863 | protected: |
| 1864 | unsigned MMOIdx; |
| 1865 | SmallVector<unsigned, 4> AddrSpaces; |
| 1866 | |
| 1867 | public: |
| 1868 | MemoryAddressSpacePredicateMatcher(unsigned InsnVarID, unsigned MMOIdx, |
| 1869 | ArrayRef<unsigned> AddrSpaces) |
| 1870 | : InstructionPredicateMatcher(IPM_MemoryAddressSpace, InsnVarID), |
| 1871 | MMOIdx(MMOIdx), AddrSpaces(AddrSpaces.begin(), AddrSpaces.end()) {} |
| 1872 | |
| 1873 | static bool classof(const PredicateMatcher *P) { |
| 1874 | return P->getKind() == IPM_MemoryAddressSpace; |
| 1875 | } |
| 1876 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1877 | if (!InstructionPredicateMatcher::isIdentical(B)) |
| 1878 | return false; |
| 1879 | auto *Other = cast<MemoryAddressSpacePredicateMatcher>(&B); |
| 1880 | return MMOIdx == Other->MMOIdx && AddrSpaces == Other->AddrSpaces; |
| 1881 | } |
| 1882 | |
| 1883 | void emitPredicateOpcodes(MatchTable &Table, |
| 1884 | RuleMatcher &Rule) const override { |
| 1885 | Table << MatchTable::Opcode("GIM_CheckMemoryAddressSpace") |
| 1886 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1887 | << MatchTable::Comment("MMO") << MatchTable::IntValue(MMOIdx) |
| 1888 | // Encode number of address spaces to expect. |
| 1889 | << MatchTable::Comment("NumAddrSpace") |
| 1890 | << MatchTable::IntValue(AddrSpaces.size()); |
| 1891 | for (unsigned AS : AddrSpaces) |
| 1892 | Table << MatchTable::Comment("AddrSpace") << MatchTable::IntValue(AS); |
| 1893 | |
| 1894 | Table << MatchTable::LineBreak; |
| 1895 | } |
| 1896 | }; |
| 1897 | |
Matt Arsenault | 52c2624 | 2019-07-31 00:14:43 +0000 | [diff] [blame] | 1898 | class MemoryAlignmentPredicateMatcher : public InstructionPredicateMatcher { |
| 1899 | protected: |
| 1900 | unsigned MMOIdx; |
| 1901 | int MinAlign; |
| 1902 | |
| 1903 | public: |
| 1904 | MemoryAlignmentPredicateMatcher(unsigned InsnVarID, unsigned MMOIdx, |
| 1905 | int MinAlign) |
| 1906 | : InstructionPredicateMatcher(IPM_MemoryAlignment, InsnVarID), |
| 1907 | MMOIdx(MMOIdx), MinAlign(MinAlign) { |
| 1908 | assert(MinAlign > 0); |
| 1909 | } |
| 1910 | |
| 1911 | static bool classof(const PredicateMatcher *P) { |
| 1912 | return P->getKind() == IPM_MemoryAlignment; |
| 1913 | } |
| 1914 | |
| 1915 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1916 | if (!InstructionPredicateMatcher::isIdentical(B)) |
| 1917 | return false; |
| 1918 | auto *Other = cast<MemoryAlignmentPredicateMatcher>(&B); |
| 1919 | return MMOIdx == Other->MMOIdx && MinAlign == Other->MinAlign; |
| 1920 | } |
| 1921 | |
| 1922 | void emitPredicateOpcodes(MatchTable &Table, |
| 1923 | RuleMatcher &Rule) const override { |
| 1924 | Table << MatchTable::Opcode("GIM_CheckMemoryAlignment") |
| 1925 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1926 | << MatchTable::Comment("MMO") << MatchTable::IntValue(MMOIdx) |
| 1927 | << MatchTable::Comment("MinAlign") << MatchTable::IntValue(MinAlign) |
| 1928 | << MatchTable::LineBreak; |
| 1929 | } |
| 1930 | }; |
| 1931 | |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 1932 | /// Generates code to check that the size of an MMO is less-than, equal-to, or |
| 1933 | /// greater than a given LLT. |
| 1934 | class MemoryVsLLTSizePredicateMatcher : public InstructionPredicateMatcher { |
| 1935 | public: |
| 1936 | enum RelationKind { |
| 1937 | GreaterThan, |
| 1938 | EqualTo, |
| 1939 | LessThan, |
| 1940 | }; |
| 1941 | |
| 1942 | protected: |
| 1943 | unsigned MMOIdx; |
| 1944 | RelationKind Relation; |
| 1945 | unsigned OpIdx; |
| 1946 | |
| 1947 | public: |
| 1948 | MemoryVsLLTSizePredicateMatcher(unsigned InsnVarID, unsigned MMOIdx, |
| 1949 | enum RelationKind Relation, |
| 1950 | unsigned OpIdx) |
| 1951 | : InstructionPredicateMatcher(IPM_MemoryVsLLTSize, InsnVarID), |
| 1952 | MMOIdx(MMOIdx), Relation(Relation), OpIdx(OpIdx) {} |
| 1953 | |
| 1954 | static bool classof(const PredicateMatcher *P) { |
| 1955 | return P->getKind() == IPM_MemoryVsLLTSize; |
| 1956 | } |
| 1957 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1958 | return InstructionPredicateMatcher::isIdentical(B) && |
| 1959 | MMOIdx == cast<MemoryVsLLTSizePredicateMatcher>(&B)->MMOIdx && |
| 1960 | Relation == cast<MemoryVsLLTSizePredicateMatcher>(&B)->Relation && |
| 1961 | OpIdx == cast<MemoryVsLLTSizePredicateMatcher>(&B)->OpIdx; |
| 1962 | } |
| 1963 | |
| 1964 | void emitPredicateOpcodes(MatchTable &Table, |
| 1965 | RuleMatcher &Rule) const override { |
| 1966 | Table << MatchTable::Opcode(Relation == EqualTo |
| 1967 | ? "GIM_CheckMemorySizeEqualToLLT" |
| 1968 | : Relation == GreaterThan |
| 1969 | ? "GIM_CheckMemorySizeGreaterThanLLT" |
| 1970 | : "GIM_CheckMemorySizeLessThanLLT") |
| 1971 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 1972 | << MatchTable::Comment("MMO") << MatchTable::IntValue(MMOIdx) |
| 1973 | << MatchTable::Comment("OpIdx") << MatchTable::IntValue(OpIdx) |
| 1974 | << MatchTable::LineBreak; |
| 1975 | } |
| 1976 | }; |
| 1977 | |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 1978 | /// Generates code to check an arbitrary C++ instruction predicate. |
| 1979 | class GenericInstructionPredicateMatcher : public InstructionPredicateMatcher { |
| 1980 | protected: |
| 1981 | TreePredicateFn Predicate; |
| 1982 | |
| 1983 | public: |
| 1984 | GenericInstructionPredicateMatcher(unsigned InsnVarID, |
| 1985 | TreePredicateFn Predicate) |
| 1986 | : InstructionPredicateMatcher(IPM_GenericPredicate, InsnVarID), |
| 1987 | Predicate(Predicate) {} |
| 1988 | |
| 1989 | static bool classof(const InstructionPredicateMatcher *P) { |
| 1990 | return P->getKind() == IPM_GenericPredicate; |
| 1991 | } |
Daniel Sanders | 06f4ff1 | 2018-09-25 17:59:02 +0000 | [diff] [blame] | 1992 | bool isIdentical(const PredicateMatcher &B) const override { |
| 1993 | return InstructionPredicateMatcher::isIdentical(B) && |
| 1994 | Predicate == |
| 1995 | static_cast<const GenericInstructionPredicateMatcher &>(B) |
| 1996 | .Predicate; |
| 1997 | } |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 1998 | void emitPredicateOpcodes(MatchTable &Table, |
| 1999 | RuleMatcher &Rule) const override { |
| 2000 | Table << MatchTable::Opcode("GIM_CheckCxxInsnPredicate") |
| 2001 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 2002 | << MatchTable::Comment("FnId") |
| 2003 | << MatchTable::NamedValue(getEnumNameForPredicate(Predicate)) |
| 2004 | << MatchTable::LineBreak; |
| 2005 | } |
| 2006 | }; |
| 2007 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 2008 | /// Generates code to check that a set of predicates and operands match for a |
| 2009 | /// particular instruction. |
| 2010 | /// |
| 2011 | /// Typical predicates include: |
| 2012 | /// * Has a specific opcode. |
| 2013 | /// * Has an nsw/nuw flag or doesn't. |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2014 | class InstructionMatcher final : public PredicateListMatcher<PredicateMatcher> { |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 2015 | protected: |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 2016 | typedef std::vector<std::unique_ptr<OperandMatcher>> OperandVec; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2017 | |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 2018 | RuleMatcher &Rule; |
| 2019 | |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2020 | /// The operands to match. All rendered operands must be present even if the |
| 2021 | /// condition is always true. |
| 2022 | OperandVec Operands; |
Roman Tereshin | 19da667 | 2018-05-22 04:31:50 +0000 | [diff] [blame] | 2023 | bool NumOperandsCheck = true; |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 2024 | |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2025 | std::string SymbolicName; |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 2026 | unsigned InsnVarID; |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2027 | |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 2028 | /// PhysRegInputs - List list has an entry for each explicitly specified |
| 2029 | /// physreg input to the pattern. The first elt is the Register node, the |
| 2030 | /// second is the recorded slot number the input pattern match saved it in. |
| 2031 | SmallVector<std::pair<Record *, unsigned>, 2> PhysRegInputs; |
| 2032 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 2033 | public: |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 2034 | InstructionMatcher(RuleMatcher &Rule, StringRef SymbolicName) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 2035 | : Rule(Rule), SymbolicName(SymbolicName) { |
| 2036 | // We create a new instruction matcher. |
| 2037 | // Get a new ID for that instruction. |
| 2038 | InsnVarID = Rule.implicitlyDefineInsnVar(*this); |
| 2039 | } |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 2040 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2041 | /// Construct a new instruction predicate and add it to the matcher. |
| 2042 | template <class Kind, class... Args> |
| 2043 | Optional<Kind *> addPredicate(Args &&... args) { |
| 2044 | Predicates.emplace_back( |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 2045 | std::make_unique<Kind>(getInsnVarID(), std::forward<Args>(args)...)); |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2046 | return static_cast<Kind *>(Predicates.back().get()); |
| 2047 | } |
| 2048 | |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 2049 | RuleMatcher &getRuleMatcher() const { return Rule; } |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2050 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2051 | unsigned getInsnVarID() const { return InsnVarID; } |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 2052 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 2053 | /// Add an operand to the matcher. |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 2054 | OperandMatcher &addOperand(unsigned OpIdx, const std::string &SymbolicName, |
| 2055 | unsigned AllocatedTemporariesBaseID) { |
| 2056 | Operands.emplace_back(new OperandMatcher(*this, OpIdx, SymbolicName, |
| 2057 | AllocatedTemporariesBaseID)); |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 2058 | if (!SymbolicName.empty()) |
| 2059 | Rule.defineOperand(SymbolicName, *Operands.back()); |
| 2060 | |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 2061 | return *Operands.back(); |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2064 | OperandMatcher &getOperand(unsigned OpIdx) { |
| 2065 | auto I = std::find_if(Operands.begin(), Operands.end(), |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 2066 | [&OpIdx](const std::unique_ptr<OperandMatcher> &X) { |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2067 | return X->getOpIdx() == OpIdx; |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2068 | }); |
| 2069 | if (I != Operands.end()) |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 2070 | return **I; |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2071 | llvm_unreachable("Failed to lookup operand"); |
| 2072 | } |
| 2073 | |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 2074 | OperandMatcher &addPhysRegInput(Record *Reg, unsigned OpIdx, |
| 2075 | unsigned TempOpIdx) { |
| 2076 | assert(SymbolicName.empty()); |
| 2077 | OperandMatcher *OM = new OperandMatcher(*this, OpIdx, "", TempOpIdx); |
| 2078 | Operands.emplace_back(OM); |
| 2079 | Rule.definePhysRegOperand(Reg, *OM); |
| 2080 | PhysRegInputs.emplace_back(Reg, OpIdx); |
| 2081 | return *OM; |
| 2082 | } |
| 2083 | |
| 2084 | ArrayRef<std::pair<Record *, unsigned>> getPhysRegInputs() const { |
| 2085 | return PhysRegInputs; |
| 2086 | } |
| 2087 | |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2088 | StringRef getSymbolicName() const { return SymbolicName; } |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2089 | unsigned getNumOperands() const { return Operands.size(); } |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 2090 | OperandVec::iterator operands_begin() { return Operands.begin(); } |
| 2091 | OperandVec::iterator operands_end() { return Operands.end(); } |
| 2092 | iterator_range<OperandVec::iterator> operands() { |
| 2093 | return make_range(operands_begin(), operands_end()); |
| 2094 | } |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2095 | OperandVec::const_iterator operands_begin() const { return Operands.begin(); } |
| 2096 | OperandVec::const_iterator operands_end() const { return Operands.end(); } |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2097 | iterator_range<OperandVec::const_iterator> operands() const { |
| 2098 | return make_range(operands_begin(), operands_end()); |
| 2099 | } |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 2100 | bool operands_empty() const { return Operands.empty(); } |
| 2101 | |
| 2102 | void pop_front() { Operands.erase(Operands.begin()); } |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2103 | |
Roman Tereshin | 19da667 | 2018-05-22 04:31:50 +0000 | [diff] [blame] | 2104 | void optimize(); |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2105 | |
| 2106 | /// Emit MatchTable opcodes that test whether the instruction named in |
| 2107 | /// InsnVarName matches all the predicates and all the operands. |
| 2108 | void emitPredicateOpcodes(MatchTable &Table, RuleMatcher &Rule) { |
Roman Tereshin | 19da667 | 2018-05-22 04:31:50 +0000 | [diff] [blame] | 2109 | if (NumOperandsCheck) |
| 2110 | InstructionNumOperandsMatcher(InsnVarID, getNumOperands()) |
| 2111 | .emitPredicateOpcodes(Table, Rule); |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 2112 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 2113 | emitPredicateListOpcodes(Table, Rule); |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2114 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 2115 | for (const auto &Operand : Operands) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 2116 | Operand->emitPredicateOpcodes(Table, Rule); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2117 | } |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 2118 | |
| 2119 | /// Compare the priority of this object and B. |
| 2120 | /// |
| 2121 | /// Returns true if this object is more important than B. |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2122 | bool isHigherPriorityThan(InstructionMatcher &B) { |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 2123 | // Instruction matchers involving more operands have higher priority. |
| 2124 | if (Operands.size() > B.Operands.size()) |
| 2125 | return true; |
| 2126 | if (Operands.size() < B.Operands.size()) |
| 2127 | return false; |
| 2128 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2129 | for (auto &&P : zip(predicates(), B.predicates())) { |
| 2130 | auto L = static_cast<InstructionPredicateMatcher *>(std::get<0>(P).get()); |
| 2131 | auto R = static_cast<InstructionPredicateMatcher *>(std::get<1>(P).get()); |
| 2132 | if (L->isHigherPriorityThan(*R)) |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 2133 | return true; |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2134 | if (R->isHigherPriorityThan(*L)) |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 2135 | return false; |
| 2136 | } |
| 2137 | |
| 2138 | for (const auto &Operand : zip(Operands, B.Operands)) { |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 2139 | if (std::get<0>(Operand)->isHigherPriorityThan(*std::get<1>(Operand))) |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 2140 | return true; |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 2141 | if (std::get<1>(Operand)->isHigherPriorityThan(*std::get<0>(Operand))) |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 2142 | return false; |
| 2143 | } |
| 2144 | |
| 2145 | return false; |
| 2146 | }; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 2147 | |
| 2148 | /// Report the maximum number of temporary operands needed by the instruction |
| 2149 | /// matcher. |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2150 | unsigned countRendererFns() { |
| 2151 | return std::accumulate( |
| 2152 | predicates().begin(), predicates().end(), 0, |
| 2153 | [](unsigned A, |
| 2154 | const std::unique_ptr<PredicateMatcher> &Predicate) { |
| 2155 | return A + Predicate->countRendererFns(); |
| 2156 | }) + |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 2157 | std::accumulate( |
| 2158 | Operands.begin(), Operands.end(), 0, |
| 2159 | [](unsigned A, const std::unique_ptr<OperandMatcher> &Operand) { |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 2160 | return A + Operand->countRendererFns(); |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 2161 | }); |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 2162 | } |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2163 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2164 | InstructionOpcodeMatcher &getOpcodeMatcher() { |
| 2165 | for (auto &P : predicates()) |
| 2166 | if (auto *OpMatcher = dyn_cast<InstructionOpcodeMatcher>(P.get())) |
| 2167 | return *OpMatcher; |
| 2168 | llvm_unreachable("Didn't find an opcode matcher"); |
| 2169 | } |
| 2170 | |
| 2171 | bool isConstantInstruction() { |
| 2172 | return getOpcodeMatcher().isConstantInstruction(); |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2173 | } |
Roman Tereshin | 19da667 | 2018-05-22 04:31:50 +0000 | [diff] [blame] | 2174 | |
| 2175 | StringRef getOpcode() { return getOpcodeMatcher().getOpcode(); } |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2176 | }; |
| 2177 | |
Roman Tereshin | 19da667 | 2018-05-22 04:31:50 +0000 | [diff] [blame] | 2178 | StringRef RuleMatcher::getOpcode() const { |
| 2179 | return Matchers.front()->getOpcode(); |
| 2180 | } |
| 2181 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2182 | unsigned RuleMatcher::getNumOperands() const { |
| 2183 | return Matchers.front()->getNumOperands(); |
| 2184 | } |
| 2185 | |
Roman Tereshin | 9a9fa49 | 2018-05-23 21:30:16 +0000 | [diff] [blame] | 2186 | LLTCodeGen RuleMatcher::getFirstConditionAsRootType() { |
| 2187 | InstructionMatcher &InsnMatcher = *Matchers.front(); |
| 2188 | if (!InsnMatcher.predicates_empty()) |
| 2189 | if (const auto *TM = |
| 2190 | dyn_cast<LLTOperandMatcher>(&**InsnMatcher.predicates_begin())) |
| 2191 | if (TM->getInsnVarID() == 0 && TM->getOpIdx() == 0) |
| 2192 | return TM->getTy(); |
| 2193 | return {}; |
| 2194 | } |
| 2195 | |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 2196 | /// Generates code to check that the operand is a register defined by an |
| 2197 | /// instruction that matches the given instruction matcher. |
| 2198 | /// |
| 2199 | /// For example, the pattern: |
| 2200 | /// (set $dst, (G_MUL (G_ADD $src1, $src2), $src3)) |
| 2201 | /// would use an InstructionOperandMatcher for operand 1 of the G_MUL to match |
| 2202 | /// the: |
| 2203 | /// (G_ADD $src1, $src2) |
| 2204 | /// subpattern. |
| 2205 | class InstructionOperandMatcher : public OperandPredicateMatcher { |
| 2206 | protected: |
| 2207 | std::unique_ptr<InstructionMatcher> InsnMatcher; |
| 2208 | |
| 2209 | public: |
Quentin Colombet | eba10cb | 2017-12-18 22:12:13 +0000 | [diff] [blame] | 2210 | InstructionOperandMatcher(unsigned InsnVarID, unsigned OpIdx, |
| 2211 | RuleMatcher &Rule, StringRef SymbolicName) |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 2212 | : OperandPredicateMatcher(OPM_Instruction, InsnVarID, OpIdx), |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 2213 | InsnMatcher(new InstructionMatcher(Rule, SymbolicName)) {} |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 2214 | |
Quentin Colombet | 063d798 | 2017-12-14 23:44:07 +0000 | [diff] [blame] | 2215 | static bool classof(const PredicateMatcher *P) { |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 2216 | return P->getKind() == OPM_Instruction; |
| 2217 | } |
| 2218 | |
| 2219 | InstructionMatcher &getInsnMatcher() const { return *InsnMatcher; } |
| 2220 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2221 | void emitCaptureOpcodes(MatchTable &Table, RuleMatcher &Rule) const { |
| 2222 | const unsigned NewInsnVarID = InsnMatcher->getInsnVarID(); |
| 2223 | Table << MatchTable::Opcode("GIM_RecordInsn") |
| 2224 | << MatchTable::Comment("DefineMI") |
| 2225 | << MatchTable::IntValue(NewInsnVarID) << MatchTable::Comment("MI") |
| 2226 | << MatchTable::IntValue(getInsnVarID()) |
| 2227 | << MatchTable::Comment("OpIdx") << MatchTable::IntValue(getOpIdx()) |
| 2228 | << MatchTable::Comment("MIs[" + llvm::to_string(NewInsnVarID) + "]") |
| 2229 | << MatchTable::LineBreak; |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 2230 | } |
| 2231 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 2232 | void emitPredicateOpcodes(MatchTable &Table, |
| 2233 | RuleMatcher &Rule) const override { |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2234 | emitCaptureOpcodes(Table, Rule); |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 2235 | InsnMatcher->emitPredicateOpcodes(Table, Rule); |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 2236 | } |
Daniel Sanders | 12e6e70 | 2018-01-17 20:34:29 +0000 | [diff] [blame] | 2237 | |
| 2238 | bool isHigherPriorityThan(const OperandPredicateMatcher &B) const override { |
| 2239 | if (OperandPredicateMatcher::isHigherPriorityThan(B)) |
| 2240 | return true; |
| 2241 | if (B.OperandPredicateMatcher::isHigherPriorityThan(*this)) |
| 2242 | return false; |
| 2243 | |
| 2244 | if (const InstructionOperandMatcher *BP = |
| 2245 | dyn_cast<InstructionOperandMatcher>(&B)) |
| 2246 | if (InsnMatcher->isHigherPriorityThan(*BP->InsnMatcher)) |
| 2247 | return true; |
| 2248 | return false; |
| 2249 | } |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 2250 | }; |
| 2251 | |
Roman Tereshin | 19da667 | 2018-05-22 04:31:50 +0000 | [diff] [blame] | 2252 | void InstructionMatcher::optimize() { |
| 2253 | SmallVector<std::unique_ptr<PredicateMatcher>, 8> Stash; |
| 2254 | const auto &OpcMatcher = getOpcodeMatcher(); |
| 2255 | |
| 2256 | Stash.push_back(predicates_pop_front()); |
| 2257 | if (Stash.back().get() == &OpcMatcher) { |
| 2258 | if (NumOperandsCheck && OpcMatcher.getNumOperands() < getNumOperands()) |
| 2259 | Stash.emplace_back( |
| 2260 | new InstructionNumOperandsMatcher(InsnVarID, getNumOperands())); |
| 2261 | NumOperandsCheck = false; |
Roman Tereshin | fedae33 | 2018-05-23 02:04:19 +0000 | [diff] [blame] | 2262 | |
| 2263 | for (auto &OM : Operands) |
| 2264 | for (auto &OP : OM->predicates()) |
| 2265 | if (isa<IntrinsicIDOperandMatcher>(OP)) { |
| 2266 | Stash.push_back(std::move(OP)); |
| 2267 | OM->eraseNullPredicates(); |
| 2268 | break; |
| 2269 | } |
Roman Tereshin | 19da667 | 2018-05-22 04:31:50 +0000 | [diff] [blame] | 2270 | } |
| 2271 | |
| 2272 | if (InsnVarID > 0) { |
| 2273 | assert(!Operands.empty() && "Nested instruction is expected to def a vreg"); |
| 2274 | for (auto &OP : Operands[0]->predicates()) |
| 2275 | OP.reset(); |
| 2276 | Operands[0]->eraseNullPredicates(); |
| 2277 | } |
Roman Tereshin | b1ba127 | 2018-05-23 19:16:59 +0000 | [diff] [blame] | 2278 | for (auto &OM : Operands) { |
| 2279 | for (auto &OP : OM->predicates()) |
| 2280 | if (isa<LLTOperandMatcher>(OP)) |
| 2281 | Stash.push_back(std::move(OP)); |
| 2282 | OM->eraseNullPredicates(); |
| 2283 | } |
Roman Tereshin | 19da667 | 2018-05-22 04:31:50 +0000 | [diff] [blame] | 2284 | while (!Stash.empty()) |
| 2285 | prependPredicate(Stash.pop_back_val()); |
| 2286 | } |
| 2287 | |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 2288 | //===- Actions ------------------------------------------------------------===// |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2289 | class OperandRenderer { |
| 2290 | public: |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2291 | enum RendererKind { |
| 2292 | OR_Copy, |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 2293 | OR_CopyOrAddZeroReg, |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2294 | OR_CopySubReg, |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 2295 | OR_CopyPhysReg, |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2296 | OR_CopyConstantAsImm, |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 2297 | OR_CopyFConstantAsFPImm, |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2298 | OR_Imm, |
| 2299 | OR_Register, |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 2300 | OR_TempRegister, |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 2301 | OR_ComplexPattern, |
| 2302 | OR_Custom |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2303 | }; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2304 | |
| 2305 | protected: |
| 2306 | RendererKind Kind; |
| 2307 | |
| 2308 | public: |
| 2309 | OperandRenderer(RendererKind Kind) : Kind(Kind) {} |
| 2310 | virtual ~OperandRenderer() {} |
| 2311 | |
| 2312 | RendererKind getKind() const { return Kind; } |
| 2313 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2314 | virtual void emitRenderOpcodes(MatchTable &Table, |
| 2315 | RuleMatcher &Rule) const = 0; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2316 | }; |
| 2317 | |
| 2318 | /// A CopyRenderer emits code to copy a single operand from an existing |
| 2319 | /// instruction to the one being built. |
| 2320 | class CopyRenderer : public OperandRenderer { |
| 2321 | protected: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2322 | unsigned NewInsnID; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2323 | /// The name of the operand. |
| 2324 | const StringRef SymbolicName; |
| 2325 | |
| 2326 | public: |
Daniel Sanders | bd83ad4 | 2017-10-24 01:48:34 +0000 | [diff] [blame] | 2327 | CopyRenderer(unsigned NewInsnID, StringRef SymbolicName) |
| 2328 | : OperandRenderer(OR_Copy), NewInsnID(NewInsnID), |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2329 | SymbolicName(SymbolicName) { |
| 2330 | assert(!SymbolicName.empty() && "Cannot copy from an unspecified source"); |
| 2331 | } |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2332 | |
| 2333 | static bool classof(const OperandRenderer *R) { |
| 2334 | return R->getKind() == OR_Copy; |
| 2335 | } |
| 2336 | |
| 2337 | const StringRef getSymbolicName() const { return SymbolicName; } |
| 2338 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2339 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 2340 | const OperandMatcher &Operand = Rule.getOperandMatcher(SymbolicName); |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2341 | unsigned OldInsnVarID = Rule.getInsnVarID(Operand.getInstructionMatcher()); |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2342 | Table << MatchTable::Opcode("GIR_Copy") << MatchTable::Comment("NewInsnID") |
| 2343 | << MatchTable::IntValue(NewInsnID) << MatchTable::Comment("OldInsnID") |
| 2344 | << MatchTable::IntValue(OldInsnVarID) << MatchTable::Comment("OpIdx") |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2345 | << MatchTable::IntValue(Operand.getOpIdx()) |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2346 | << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2347 | } |
| 2348 | }; |
| 2349 | |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 2350 | /// A CopyRenderer emits code to copy a virtual register to a specific physical |
| 2351 | /// register. |
| 2352 | class CopyPhysRegRenderer : public OperandRenderer { |
| 2353 | protected: |
| 2354 | unsigned NewInsnID; |
| 2355 | Record *PhysReg; |
| 2356 | |
| 2357 | public: |
| 2358 | CopyPhysRegRenderer(unsigned NewInsnID, Record *Reg) |
| 2359 | : OperandRenderer(OR_CopyPhysReg), NewInsnID(NewInsnID), |
| 2360 | PhysReg(Reg) { |
| 2361 | assert(PhysReg); |
| 2362 | } |
| 2363 | |
| 2364 | static bool classof(const OperandRenderer *R) { |
| 2365 | return R->getKind() == OR_CopyPhysReg; |
| 2366 | } |
| 2367 | |
| 2368 | Record *getPhysReg() const { return PhysReg; } |
| 2369 | |
| 2370 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
| 2371 | const OperandMatcher &Operand = Rule.getPhysRegOperandMatcher(PhysReg); |
| 2372 | unsigned OldInsnVarID = Rule.getInsnVarID(Operand.getInstructionMatcher()); |
| 2373 | Table << MatchTable::Opcode("GIR_Copy") << MatchTable::Comment("NewInsnID") |
| 2374 | << MatchTable::IntValue(NewInsnID) << MatchTable::Comment("OldInsnID") |
| 2375 | << MatchTable::IntValue(OldInsnVarID) << MatchTable::Comment("OpIdx") |
| 2376 | << MatchTable::IntValue(Operand.getOpIdx()) |
| 2377 | << MatchTable::Comment(PhysReg->getName()) |
| 2378 | << MatchTable::LineBreak; |
| 2379 | } |
| 2380 | }; |
| 2381 | |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 2382 | /// A CopyOrAddZeroRegRenderer emits code to copy a single operand from an |
| 2383 | /// existing instruction to the one being built. If the operand turns out to be |
| 2384 | /// a 'G_CONSTANT 0' then it replaces the operand with a zero register. |
| 2385 | class CopyOrAddZeroRegRenderer : public OperandRenderer { |
| 2386 | protected: |
| 2387 | unsigned NewInsnID; |
| 2388 | /// The name of the operand. |
| 2389 | const StringRef SymbolicName; |
| 2390 | const Record *ZeroRegisterDef; |
| 2391 | |
| 2392 | public: |
| 2393 | CopyOrAddZeroRegRenderer(unsigned NewInsnID, |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 2394 | StringRef SymbolicName, Record *ZeroRegisterDef) |
| 2395 | : OperandRenderer(OR_CopyOrAddZeroReg), NewInsnID(NewInsnID), |
| 2396 | SymbolicName(SymbolicName), ZeroRegisterDef(ZeroRegisterDef) { |
| 2397 | assert(!SymbolicName.empty() && "Cannot copy from an unspecified source"); |
| 2398 | } |
| 2399 | |
| 2400 | static bool classof(const OperandRenderer *R) { |
| 2401 | return R->getKind() == OR_CopyOrAddZeroReg; |
| 2402 | } |
| 2403 | |
| 2404 | const StringRef getSymbolicName() const { return SymbolicName; } |
| 2405 | |
| 2406 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
| 2407 | const OperandMatcher &Operand = Rule.getOperandMatcher(SymbolicName); |
| 2408 | unsigned OldInsnVarID = Rule.getInsnVarID(Operand.getInstructionMatcher()); |
| 2409 | Table << MatchTable::Opcode("GIR_CopyOrAddZeroReg") |
| 2410 | << MatchTable::Comment("NewInsnID") << MatchTable::IntValue(NewInsnID) |
| 2411 | << MatchTable::Comment("OldInsnID") |
| 2412 | << MatchTable::IntValue(OldInsnVarID) << MatchTable::Comment("OpIdx") |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2413 | << MatchTable::IntValue(Operand.getOpIdx()) |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 2414 | << MatchTable::NamedValue( |
| 2415 | (ZeroRegisterDef->getValue("Namespace") |
| 2416 | ? ZeroRegisterDef->getValueAsString("Namespace") |
| 2417 | : ""), |
| 2418 | ZeroRegisterDef->getName()) |
| 2419 | << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak; |
| 2420 | } |
| 2421 | }; |
| 2422 | |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2423 | /// A CopyConstantAsImmRenderer emits code to render a G_CONSTANT instruction to |
| 2424 | /// an extended immediate operand. |
| 2425 | class CopyConstantAsImmRenderer : public OperandRenderer { |
| 2426 | protected: |
| 2427 | unsigned NewInsnID; |
| 2428 | /// The name of the operand. |
| 2429 | const std::string SymbolicName; |
| 2430 | bool Signed; |
| 2431 | |
| 2432 | public: |
| 2433 | CopyConstantAsImmRenderer(unsigned NewInsnID, StringRef SymbolicName) |
| 2434 | : OperandRenderer(OR_CopyConstantAsImm), NewInsnID(NewInsnID), |
| 2435 | SymbolicName(SymbolicName), Signed(true) {} |
| 2436 | |
| 2437 | static bool classof(const OperandRenderer *R) { |
| 2438 | return R->getKind() == OR_CopyConstantAsImm; |
| 2439 | } |
| 2440 | |
| 2441 | const StringRef getSymbolicName() const { return SymbolicName; } |
| 2442 | |
| 2443 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2444 | InstructionMatcher &InsnMatcher = Rule.getInstructionMatcher(SymbolicName); |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2445 | unsigned OldInsnVarID = Rule.getInsnVarID(InsnMatcher); |
| 2446 | Table << MatchTable::Opcode(Signed ? "GIR_CopyConstantAsSImm" |
| 2447 | : "GIR_CopyConstantAsUImm") |
| 2448 | << MatchTable::Comment("NewInsnID") << MatchTable::IntValue(NewInsnID) |
| 2449 | << MatchTable::Comment("OldInsnID") |
| 2450 | << MatchTable::IntValue(OldInsnVarID) |
| 2451 | << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak; |
| 2452 | } |
| 2453 | }; |
| 2454 | |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 2455 | /// A CopyFConstantAsFPImmRenderer emits code to render a G_FCONSTANT |
| 2456 | /// instruction to an extended immediate operand. |
| 2457 | class CopyFConstantAsFPImmRenderer : public OperandRenderer { |
| 2458 | protected: |
| 2459 | unsigned NewInsnID; |
| 2460 | /// The name of the operand. |
| 2461 | const std::string SymbolicName; |
| 2462 | |
| 2463 | public: |
| 2464 | CopyFConstantAsFPImmRenderer(unsigned NewInsnID, StringRef SymbolicName) |
| 2465 | : OperandRenderer(OR_CopyFConstantAsFPImm), NewInsnID(NewInsnID), |
| 2466 | SymbolicName(SymbolicName) {} |
| 2467 | |
| 2468 | static bool classof(const OperandRenderer *R) { |
| 2469 | return R->getKind() == OR_CopyFConstantAsFPImm; |
| 2470 | } |
| 2471 | |
| 2472 | const StringRef getSymbolicName() const { return SymbolicName; } |
| 2473 | |
| 2474 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2475 | InstructionMatcher &InsnMatcher = Rule.getInstructionMatcher(SymbolicName); |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 2476 | unsigned OldInsnVarID = Rule.getInsnVarID(InsnMatcher); |
| 2477 | Table << MatchTable::Opcode("GIR_CopyFConstantAsFPImm") |
| 2478 | << MatchTable::Comment("NewInsnID") << MatchTable::IntValue(NewInsnID) |
| 2479 | << MatchTable::Comment("OldInsnID") |
| 2480 | << MatchTable::IntValue(OldInsnVarID) |
| 2481 | << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak; |
| 2482 | } |
| 2483 | }; |
| 2484 | |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2485 | /// A CopySubRegRenderer emits code to copy a single register operand from an |
| 2486 | /// existing instruction to the one being built and indicate that only a |
| 2487 | /// subregister should be copied. |
| 2488 | class CopySubRegRenderer : public OperandRenderer { |
| 2489 | protected: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2490 | unsigned NewInsnID; |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2491 | /// The name of the operand. |
| 2492 | const StringRef SymbolicName; |
| 2493 | /// The subregister to extract. |
| 2494 | const CodeGenSubRegIndex *SubReg; |
| 2495 | |
| 2496 | public: |
Daniel Sanders | bd83ad4 | 2017-10-24 01:48:34 +0000 | [diff] [blame] | 2497 | CopySubRegRenderer(unsigned NewInsnID, StringRef SymbolicName, |
| 2498 | const CodeGenSubRegIndex *SubReg) |
| 2499 | : OperandRenderer(OR_CopySubReg), NewInsnID(NewInsnID), |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2500 | SymbolicName(SymbolicName), SubReg(SubReg) {} |
| 2501 | |
| 2502 | static bool classof(const OperandRenderer *R) { |
| 2503 | return R->getKind() == OR_CopySubReg; |
| 2504 | } |
| 2505 | |
| 2506 | const StringRef getSymbolicName() const { return SymbolicName; } |
| 2507 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2508 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 2509 | const OperandMatcher &Operand = Rule.getOperandMatcher(SymbolicName); |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2510 | unsigned OldInsnVarID = Rule.getInsnVarID(Operand.getInstructionMatcher()); |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2511 | Table << MatchTable::Opcode("GIR_CopySubReg") |
| 2512 | << MatchTable::Comment("NewInsnID") << MatchTable::IntValue(NewInsnID) |
| 2513 | << MatchTable::Comment("OldInsnID") |
| 2514 | << MatchTable::IntValue(OldInsnVarID) << MatchTable::Comment("OpIdx") |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2515 | << MatchTable::IntValue(Operand.getOpIdx()) |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2516 | << MatchTable::Comment("SubRegIdx") |
| 2517 | << MatchTable::IntValue(SubReg->EnumValue) |
| 2518 | << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak; |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2519 | } |
| 2520 | }; |
| 2521 | |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2522 | /// Adds a specific physical register to the instruction being built. |
| 2523 | /// This is typically useful for WZR/XZR on AArch64. |
| 2524 | class AddRegisterRenderer : public OperandRenderer { |
| 2525 | protected: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2526 | unsigned InsnID; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2527 | const Record *RegisterDef; |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 2528 | bool IsDef; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2529 | |
| 2530 | public: |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 2531 | AddRegisterRenderer(unsigned InsnID, const Record *RegisterDef, |
| 2532 | bool IsDef = false) |
| 2533 | : OperandRenderer(OR_Register), InsnID(InsnID), RegisterDef(RegisterDef), |
| 2534 | IsDef(IsDef) {} |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2535 | |
| 2536 | static bool classof(const OperandRenderer *R) { |
| 2537 | return R->getKind() == OR_Register; |
| 2538 | } |
| 2539 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2540 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
| 2541 | Table << MatchTable::Opcode("GIR_AddRegister") |
| 2542 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 2543 | << MatchTable::NamedValue( |
| 2544 | (RegisterDef->getValue("Namespace") |
| 2545 | ? RegisterDef->getValueAsString("Namespace") |
| 2546 | : ""), |
| 2547 | RegisterDef->getName()) |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 2548 | << MatchTable::Comment("AddRegisterRegFlags"); |
| 2549 | |
| 2550 | // TODO: This is encoded as a 64-bit element, but only 16 or 32-bits are |
| 2551 | // really needed for a physical register reference. We can pack the |
| 2552 | // register and flags in a single field. |
| 2553 | if (IsDef) |
| 2554 | Table << MatchTable::NamedValue("RegState::Define"); |
| 2555 | else |
| 2556 | Table << MatchTable::IntValue(0); |
| 2557 | Table << MatchTable::LineBreak; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2558 | } |
| 2559 | }; |
| 2560 | |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 2561 | /// Adds a specific temporary virtual register to the instruction being built. |
| 2562 | /// This is used to chain instructions together when emitting multiple |
| 2563 | /// instructions. |
| 2564 | class TempRegRenderer : public OperandRenderer { |
| 2565 | protected: |
| 2566 | unsigned InsnID; |
| 2567 | unsigned TempRegID; |
| 2568 | bool IsDef; |
| 2569 | |
| 2570 | public: |
| 2571 | TempRegRenderer(unsigned InsnID, unsigned TempRegID, bool IsDef = false) |
| 2572 | : OperandRenderer(OR_Register), InsnID(InsnID), TempRegID(TempRegID), |
| 2573 | IsDef(IsDef) {} |
| 2574 | |
| 2575 | static bool classof(const OperandRenderer *R) { |
| 2576 | return R->getKind() == OR_TempRegister; |
| 2577 | } |
| 2578 | |
| 2579 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
| 2580 | Table << MatchTable::Opcode("GIR_AddTempRegister") |
| 2581 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 2582 | << MatchTable::Comment("TempRegID") << MatchTable::IntValue(TempRegID) |
| 2583 | << MatchTable::Comment("TempRegFlags"); |
| 2584 | if (IsDef) |
| 2585 | Table << MatchTable::NamedValue("RegState::Define"); |
| 2586 | else |
| 2587 | Table << MatchTable::IntValue(0); |
| 2588 | Table << MatchTable::LineBreak; |
| 2589 | } |
| 2590 | }; |
| 2591 | |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 2592 | /// Adds a specific immediate to the instruction being built. |
| 2593 | class ImmRenderer : public OperandRenderer { |
| 2594 | protected: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2595 | unsigned InsnID; |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 2596 | int64_t Imm; |
| 2597 | |
| 2598 | public: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2599 | ImmRenderer(unsigned InsnID, int64_t Imm) |
| 2600 | : OperandRenderer(OR_Imm), InsnID(InsnID), Imm(Imm) {} |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 2601 | |
| 2602 | static bool classof(const OperandRenderer *R) { |
| 2603 | return R->getKind() == OR_Imm; |
| 2604 | } |
| 2605 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2606 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
| 2607 | Table << MatchTable::Opcode("GIR_AddImm") << MatchTable::Comment("InsnID") |
| 2608 | << MatchTable::IntValue(InsnID) << MatchTable::Comment("Imm") |
| 2609 | << MatchTable::IntValue(Imm) << MatchTable::LineBreak; |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 2610 | } |
| 2611 | }; |
| 2612 | |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 2613 | /// Adds operands by calling a renderer function supplied by the ComplexPattern |
| 2614 | /// matcher function. |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 2615 | class RenderComplexPatternOperand : public OperandRenderer { |
| 2616 | private: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2617 | unsigned InsnID; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 2618 | const Record &TheDef; |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 2619 | /// The name of the operand. |
| 2620 | const StringRef SymbolicName; |
| 2621 | /// The renderer number. This must be unique within a rule since it's used to |
| 2622 | /// identify a temporary variable to hold the renderer function. |
| 2623 | unsigned RendererID; |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 2624 | /// When provided, this is the suboperand of the ComplexPattern operand to |
| 2625 | /// render. Otherwise all the suboperands will be rendered. |
| 2626 | Optional<unsigned> SubOperand; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 2627 | |
| 2628 | unsigned getNumOperands() const { |
| 2629 | return TheDef.getValueAsDag("Operands")->getNumArgs(); |
| 2630 | } |
| 2631 | |
| 2632 | public: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2633 | RenderComplexPatternOperand(unsigned InsnID, const Record &TheDef, |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 2634 | StringRef SymbolicName, unsigned RendererID, |
| 2635 | Optional<unsigned> SubOperand = None) |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2636 | : OperandRenderer(OR_ComplexPattern), InsnID(InsnID), TheDef(TheDef), |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 2637 | SymbolicName(SymbolicName), RendererID(RendererID), |
| 2638 | SubOperand(SubOperand) {} |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 2639 | |
| 2640 | static bool classof(const OperandRenderer *R) { |
| 2641 | return R->getKind() == OR_ComplexPattern; |
| 2642 | } |
| 2643 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2644 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 2645 | Table << MatchTable::Opcode(SubOperand.hasValue() ? "GIR_ComplexSubOperandRenderer" |
| 2646 | : "GIR_ComplexRenderer") |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2647 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 2648 | << MatchTable::Comment("RendererID") |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 2649 | << MatchTable::IntValue(RendererID); |
| 2650 | if (SubOperand.hasValue()) |
| 2651 | Table << MatchTable::Comment("SubOperand") |
| 2652 | << MatchTable::IntValue(SubOperand.getValue()); |
| 2653 | Table << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 2654 | } |
| 2655 | }; |
| 2656 | |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 2657 | class CustomRenderer : public OperandRenderer { |
| 2658 | protected: |
| 2659 | unsigned InsnID; |
| 2660 | const Record &Renderer; |
| 2661 | /// The name of the operand. |
| 2662 | const std::string SymbolicName; |
| 2663 | |
| 2664 | public: |
| 2665 | CustomRenderer(unsigned InsnID, const Record &Renderer, |
| 2666 | StringRef SymbolicName) |
| 2667 | : OperandRenderer(OR_Custom), InsnID(InsnID), Renderer(Renderer), |
| 2668 | SymbolicName(SymbolicName) {} |
| 2669 | |
| 2670 | static bool classof(const OperandRenderer *R) { |
| 2671 | return R->getKind() == OR_Custom; |
| 2672 | } |
| 2673 | |
| 2674 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2675 | InstructionMatcher &InsnMatcher = Rule.getInstructionMatcher(SymbolicName); |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 2676 | unsigned OldInsnVarID = Rule.getInsnVarID(InsnMatcher); |
| 2677 | Table << MatchTable::Opcode("GIR_CustomRenderer") |
| 2678 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 2679 | << MatchTable::Comment("OldInsnID") |
| 2680 | << MatchTable::IntValue(OldInsnVarID) |
| 2681 | << MatchTable::Comment("Renderer") |
| 2682 | << MatchTable::NamedValue( |
| 2683 | "GICR_" + Renderer.getValueAsString("RendererFn").str()) |
| 2684 | << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak; |
| 2685 | } |
| 2686 | }; |
| 2687 | |
Ahmed Bougacha | 56ca3a9 | 2017-02-04 00:47:10 +0000 | [diff] [blame] | 2688 | /// An action taken when all Matcher predicates succeeded for a parent rule. |
| 2689 | /// |
| 2690 | /// Typical actions include: |
| 2691 | /// * Changing the opcode of an instruction. |
| 2692 | /// * Adding an operand to an instruction. |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 2693 | class MatchAction { |
| 2694 | public: |
| 2695 | virtual ~MatchAction() {} |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2696 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2697 | /// Emit the MatchTable opcodes to implement the action. |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2698 | virtual void emitActionOpcodes(MatchTable &Table, |
| 2699 | RuleMatcher &Rule) const = 0; |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 2700 | }; |
| 2701 | |
Ahmed Bougacha | 9aa4c10 | 2017-02-04 00:47:08 +0000 | [diff] [blame] | 2702 | /// Generates a comment describing the matched rule being acted upon. |
| 2703 | class DebugCommentAction : public MatchAction { |
| 2704 | private: |
Daniel Sanders | 6ea17ed | 2017-10-31 18:07:03 +0000 | [diff] [blame] | 2705 | std::string S; |
Ahmed Bougacha | 9aa4c10 | 2017-02-04 00:47:08 +0000 | [diff] [blame] | 2706 | |
| 2707 | public: |
Daniel Sanders | 6ea17ed | 2017-10-31 18:07:03 +0000 | [diff] [blame] | 2708 | DebugCommentAction(StringRef S) : S(S) {} |
Ahmed Bougacha | 9aa4c10 | 2017-02-04 00:47:08 +0000 | [diff] [blame] | 2709 | |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2710 | void emitActionOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
Daniel Sanders | 6ea17ed | 2017-10-31 18:07:03 +0000 | [diff] [blame] | 2711 | Table << MatchTable::Comment(S) << MatchTable::LineBreak; |
Ahmed Bougacha | 9aa4c10 | 2017-02-04 00:47:08 +0000 | [diff] [blame] | 2712 | } |
| 2713 | }; |
| 2714 | |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2715 | /// Generates code to build an instruction or mutate an existing instruction |
| 2716 | /// into the desired instruction when this is possible. |
| 2717 | class BuildMIAction : public MatchAction { |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 2718 | private: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2719 | unsigned InsnID; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2720 | const CodeGenInstruction *I; |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2721 | InstructionMatcher *Matched; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2722 | std::vector<std::unique_ptr<OperandRenderer>> OperandRenderers; |
| 2723 | |
| 2724 | /// True if the instruction can be built solely by mutating the opcode. |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2725 | bool canMutate(RuleMatcher &Rule, const InstructionMatcher *Insn) const { |
| 2726 | if (!Insn) |
Daniel Sanders | ab1d119 | 2017-10-24 18:11:54 +0000 | [diff] [blame] | 2727 | return false; |
| 2728 | |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2729 | if (OperandRenderers.size() != Insn->getNumOperands()) |
Daniel Sanders | e9fdba3 | 2017-04-29 17:30:09 +0000 | [diff] [blame] | 2730 | return false; |
| 2731 | |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2732 | for (const auto &Renderer : enumerate(OperandRenderers)) { |
Zachary Turner | 309a088 | 2017-03-13 16:24:10 +0000 | [diff] [blame] | 2733 | if (const auto *Copy = dyn_cast<CopyRenderer>(&*Renderer.value())) { |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 2734 | const OperandMatcher &OM = Rule.getOperandMatcher(Copy->getSymbolicName()); |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2735 | if (Insn != &OM.getInstructionMatcher() || |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2736 | OM.getOpIdx() != Renderer.index()) |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2737 | return false; |
| 2738 | } else |
| 2739 | return false; |
| 2740 | } |
| 2741 | |
| 2742 | return true; |
| 2743 | } |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2744 | |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 2745 | public: |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2746 | BuildMIAction(unsigned InsnID, const CodeGenInstruction *I) |
| 2747 | : InsnID(InsnID), I(I), Matched(nullptr) {} |
| 2748 | |
Daniel Sanders | 0846452 | 2018-01-29 21:09:12 +0000 | [diff] [blame] | 2749 | unsigned getInsnID() const { return InsnID; } |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 2750 | const CodeGenInstruction *getCGI() const { return I; } |
| 2751 | |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2752 | void chooseInsnToMutate(RuleMatcher &Rule) { |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2753 | for (auto *MutateCandidate : Rule.mutatable_insns()) { |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2754 | if (canMutate(Rule, MutateCandidate)) { |
| 2755 | // Take the first one we're offered that we're able to mutate. |
| 2756 | Rule.reserveInsnMatcherForMutation(MutateCandidate); |
| 2757 | Matched = MutateCandidate; |
| 2758 | return; |
| 2759 | } |
| 2760 | } |
| 2761 | } |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 2762 | |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2763 | template <class Kind, class... Args> |
| 2764 | Kind &addRenderer(Args&&... args) { |
| 2765 | OperandRenderers.emplace_back( |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 2766 | std::make_unique<Kind>(InsnID, std::forward<Args>(args)...)); |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2767 | return *static_cast<Kind *>(OperandRenderers.back().get()); |
| 2768 | } |
| 2769 | |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2770 | void emitActionOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
| 2771 | if (Matched) { |
| 2772 | assert(canMutate(Rule, Matched) && |
| 2773 | "Arranged to mutate an insn that isn't mutatable"); |
| 2774 | |
| 2775 | unsigned RecycleInsnID = Rule.getInsnVarID(*Matched); |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2776 | Table << MatchTable::Opcode("GIR_MutateOpcode") |
| 2777 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 2778 | << MatchTable::Comment("RecycleInsnID") |
| 2779 | << MatchTable::IntValue(RecycleInsnID) |
| 2780 | << MatchTable::Comment("Opcode") |
| 2781 | << MatchTable::NamedValue(I->Namespace, I->TheDef->getName()) |
| 2782 | << MatchTable::LineBreak; |
Tim Northover | 4340d64 | 2017-03-20 21:58:23 +0000 | [diff] [blame] | 2783 | |
| 2784 | if (!I->ImplicitDefs.empty() || !I->ImplicitUses.empty()) { |
Tim Northover | 4340d64 | 2017-03-20 21:58:23 +0000 | [diff] [blame] | 2785 | for (auto Def : I->ImplicitDefs) { |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 2786 | auto Namespace = Def->getValue("Namespace") |
| 2787 | ? Def->getValueAsString("Namespace") |
| 2788 | : ""; |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2789 | Table << MatchTable::Opcode("GIR_AddImplicitDef") |
| 2790 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 2791 | << MatchTable::NamedValue(Namespace, Def->getName()) |
| 2792 | << MatchTable::LineBreak; |
Tim Northover | 4340d64 | 2017-03-20 21:58:23 +0000 | [diff] [blame] | 2793 | } |
| 2794 | for (auto Use : I->ImplicitUses) { |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 2795 | auto Namespace = Use->getValue("Namespace") |
| 2796 | ? Use->getValueAsString("Namespace") |
| 2797 | : ""; |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2798 | Table << MatchTable::Opcode("GIR_AddImplicitUse") |
| 2799 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 2800 | << MatchTable::NamedValue(Namespace, Use->getName()) |
| 2801 | << MatchTable::LineBreak; |
Tim Northover | 4340d64 | 2017-03-20 21:58:23 +0000 | [diff] [blame] | 2802 | } |
| 2803 | } |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2804 | return; |
| 2805 | } |
| 2806 | |
| 2807 | // TODO: Simple permutation looks like it could be almost as common as |
| 2808 | // mutation due to commutative operations. |
| 2809 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2810 | Table << MatchTable::Opcode("GIR_BuildMI") << MatchTable::Comment("InsnID") |
| 2811 | << MatchTable::IntValue(InsnID) << MatchTable::Comment("Opcode") |
| 2812 | << MatchTable::NamedValue(I->Namespace, I->TheDef->getName()) |
| 2813 | << MatchTable::LineBreak; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2814 | for (const auto &Renderer : OperandRenderers) |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2815 | Renderer->emitRenderOpcodes(Table, Rule); |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2816 | |
Florian Hahn | 3bc3ec6 | 2017-08-03 14:48:22 +0000 | [diff] [blame] | 2817 | if (I->mayLoad || I->mayStore) { |
| 2818 | Table << MatchTable::Opcode("GIR_MergeMemOperands") |
| 2819 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 2820 | << MatchTable::Comment("MergeInsnID's"); |
| 2821 | // Emit the ID's for all the instructions that are matched by this rule. |
| 2822 | // TODO: Limit this to matched instructions that mayLoad/mayStore or have |
| 2823 | // some other means of having a memoperand. Also limit this to |
| 2824 | // emitted instructions that expect to have a memoperand too. For |
| 2825 | // example, (G_SEXT (G_LOAD x)) that results in separate load and |
| 2826 | // sign-extend instructions shouldn't put the memoperand on the |
| 2827 | // sign-extend since it has no effect there. |
| 2828 | std::vector<unsigned> MergeInsnIDs; |
| 2829 | for (const auto &IDMatcherPair : Rule.defined_insn_vars()) |
| 2830 | MergeInsnIDs.push_back(IDMatcherPair.second); |
Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 2831 | llvm::sort(MergeInsnIDs); |
Florian Hahn | 3bc3ec6 | 2017-08-03 14:48:22 +0000 | [diff] [blame] | 2832 | for (const auto &MergeInsnID : MergeInsnIDs) |
| 2833 | Table << MatchTable::IntValue(MergeInsnID); |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2834 | Table << MatchTable::NamedValue("GIU_MergeMemOperands_EndOfList") |
| 2835 | << MatchTable::LineBreak; |
Florian Hahn | 3bc3ec6 | 2017-08-03 14:48:22 +0000 | [diff] [blame] | 2836 | } |
| 2837 | |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 2838 | // FIXME: This is a hack but it's sufficient for ISel. We'll need to do |
| 2839 | // better for combines. Particularly when there are multiple match |
| 2840 | // roots. |
| 2841 | if (InsnID == 0) |
| 2842 | Table << MatchTable::Opcode("GIR_EraseFromParent") |
| 2843 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 2844 | << MatchTable::LineBreak; |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2845 | } |
| 2846 | }; |
| 2847 | |
| 2848 | /// Generates code to constrain the operands of an output instruction to the |
| 2849 | /// register classes specified by the definition of that instruction. |
| 2850 | class ConstrainOperandsToDefinitionAction : public MatchAction { |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2851 | unsigned InsnID; |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2852 | |
| 2853 | public: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2854 | ConstrainOperandsToDefinitionAction(unsigned InsnID) : InsnID(InsnID) {} |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2855 | |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2856 | void emitActionOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2857 | Table << MatchTable::Opcode("GIR_ConstrainSelectedInstOperands") |
| 2858 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 2859 | << MatchTable::LineBreak; |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2860 | } |
| 2861 | }; |
| 2862 | |
| 2863 | /// Generates code to constrain the specified operand of an output instruction |
| 2864 | /// to the specified register class. |
| 2865 | class ConstrainOperandToRegClassAction : public MatchAction { |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2866 | unsigned InsnID; |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2867 | unsigned OpIdx; |
| 2868 | const CodeGenRegisterClass &RC; |
| 2869 | |
| 2870 | public: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2871 | ConstrainOperandToRegClassAction(unsigned InsnID, unsigned OpIdx, |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2872 | const CodeGenRegisterClass &RC) |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2873 | : InsnID(InsnID), OpIdx(OpIdx), RC(RC) {} |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2874 | |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2875 | void emitActionOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 2876 | Table << MatchTable::Opcode("GIR_ConstrainOperandRC") |
| 2877 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 2878 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) |
| 2879 | << MatchTable::Comment("RC " + RC.getName()) |
| 2880 | << MatchTable::IntValue(RC.EnumValue) << MatchTable::LineBreak; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2881 | } |
| 2882 | }; |
| 2883 | |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 2884 | /// Generates code to create a temporary register which can be used to chain |
| 2885 | /// instructions together. |
| 2886 | class MakeTempRegisterAction : public MatchAction { |
| 2887 | private: |
| 2888 | LLTCodeGen Ty; |
| 2889 | unsigned TempRegID; |
| 2890 | |
| 2891 | public: |
| 2892 | MakeTempRegisterAction(const LLTCodeGen &Ty, unsigned TempRegID) |
| 2893 | : Ty(Ty), TempRegID(TempRegID) {} |
| 2894 | |
| 2895 | void emitActionOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
| 2896 | Table << MatchTable::Opcode("GIR_MakeTempReg") |
| 2897 | << MatchTable::Comment("TempRegID") << MatchTable::IntValue(TempRegID) |
| 2898 | << MatchTable::Comment("TypeID") |
| 2899 | << MatchTable::NamedValue(Ty.getCxxEnumValue()) |
| 2900 | << MatchTable::LineBreak; |
| 2901 | } |
| 2902 | }; |
| 2903 | |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2904 | InstructionMatcher &RuleMatcher::addInstructionMatcher(StringRef SymbolicName) { |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 2905 | Matchers.emplace_back(new InstructionMatcher(*this, SymbolicName)); |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 2906 | MutatableInsns.insert(Matchers.back().get()); |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 2907 | return *Matchers.back(); |
| 2908 | } |
Ahmed Bougacha | 56ca3a9 | 2017-02-04 00:47:10 +0000 | [diff] [blame] | 2909 | |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 2910 | void RuleMatcher::addRequiredFeature(Record *Feature) { |
| 2911 | RequiredFeatures.push_back(Feature); |
| 2912 | } |
| 2913 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 2914 | const std::vector<Record *> &RuleMatcher::getRequiredFeatures() const { |
| 2915 | return RequiredFeatures; |
| 2916 | } |
| 2917 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 2918 | // Emplaces an action of the specified Kind at the end of the action list. |
| 2919 | // |
| 2920 | // Returns a reference to the newly created action. |
| 2921 | // |
| 2922 | // Like std::vector::emplace_back(), may invalidate all iterators if the new |
| 2923 | // size exceeds the capacity. Otherwise, only invalidates the past-the-end |
| 2924 | // iterator. |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 2925 | template <class Kind, class... Args> |
| 2926 | Kind &RuleMatcher::addAction(Args &&... args) { |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 2927 | Actions.emplace_back(std::make_unique<Kind>(std::forward<Args>(args)...)); |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 2928 | return *static_cast<Kind *>(Actions.back().get()); |
| 2929 | } |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 2930 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 2931 | // Emplaces an action of the specified Kind before the given insertion point. |
| 2932 | // |
| 2933 | // Returns an iterator pointing at the newly created instruction. |
| 2934 | // |
| 2935 | // Like std::vector::insert(), may invalidate all iterators if the new size |
| 2936 | // exceeds the capacity. Otherwise, only invalidates the iterators from the |
| 2937 | // insertion point onwards. |
| 2938 | template <class Kind, class... Args> |
| 2939 | action_iterator RuleMatcher::insertAction(action_iterator InsertPt, |
| 2940 | Args &&... args) { |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 2941 | return Actions.emplace(InsertPt, |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 2942 | std::make_unique<Kind>(std::forward<Args>(args)...)); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 2943 | } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 2944 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2945 | unsigned RuleMatcher::implicitlyDefineInsnVar(InstructionMatcher &Matcher) { |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 2946 | unsigned NewInsnVarID = NextInsnVarID++; |
| 2947 | InsnVariableIDs[&Matcher] = NewInsnVarID; |
| 2948 | return NewInsnVarID; |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 2949 | } |
| 2950 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2951 | unsigned RuleMatcher::getInsnVarID(InstructionMatcher &InsnMatcher) const { |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 2952 | const auto &I = InsnVariableIDs.find(&InsnMatcher); |
| 2953 | if (I != InsnVariableIDs.end()) |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 2954 | return I->second; |
| 2955 | llvm_unreachable("Matched Insn was not captured in a local variable"); |
| 2956 | } |
| 2957 | |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 2958 | void RuleMatcher::defineOperand(StringRef SymbolicName, OperandMatcher &OM) { |
| 2959 | if (DefinedOperands.find(SymbolicName) == DefinedOperands.end()) { |
| 2960 | DefinedOperands[SymbolicName] = &OM; |
| 2961 | return; |
| 2962 | } |
| 2963 | |
| 2964 | // If the operand is already defined, then we must ensure both references in |
| 2965 | // the matcher have the exact same node. |
| 2966 | OM.addPredicate<SameOperandMatcher>(OM.getSymbolicName()); |
| 2967 | } |
| 2968 | |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 2969 | void RuleMatcher::definePhysRegOperand(Record *Reg, OperandMatcher &OM) { |
| 2970 | if (PhysRegOperands.find(Reg) == PhysRegOperands.end()) { |
| 2971 | PhysRegOperands[Reg] = &OM; |
| 2972 | return; |
| 2973 | } |
| 2974 | } |
| 2975 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 2976 | InstructionMatcher & |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2977 | RuleMatcher::getInstructionMatcher(StringRef SymbolicName) const { |
| 2978 | for (const auto &I : InsnVariableIDs) |
| 2979 | if (I.first->getSymbolicName() == SymbolicName) |
| 2980 | return *I.first; |
| 2981 | llvm_unreachable( |
| 2982 | ("Failed to lookup instruction " + SymbolicName).str().c_str()); |
| 2983 | } |
| 2984 | |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 2985 | const OperandMatcher & |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 2986 | RuleMatcher::getPhysRegOperandMatcher(Record *Reg) const { |
| 2987 | const auto &I = PhysRegOperands.find(Reg); |
| 2988 | |
| 2989 | if (I == PhysRegOperands.end()) { |
| 2990 | PrintFatalError(SrcLoc, "Register " + Reg->getName() + |
| 2991 | " was not declared in matcher"); |
| 2992 | } |
| 2993 | |
| 2994 | return *I->second; |
| 2995 | } |
| 2996 | |
| 2997 | const OperandMatcher & |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 2998 | RuleMatcher::getOperandMatcher(StringRef Name) const { |
| 2999 | const auto &I = DefinedOperands.find(Name); |
| 3000 | |
| 3001 | if (I == DefinedOperands.end()) |
| 3002 | PrintFatalError(SrcLoc, "Operand " + Name + " was not declared in matcher"); |
| 3003 | |
| 3004 | return *I->second; |
| 3005 | } |
| 3006 | |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 3007 | void RuleMatcher::emit(MatchTable &Table) { |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 3008 | if (Matchers.empty()) |
| 3009 | llvm_unreachable("Unexpected empty matcher!"); |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 3010 | |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 3011 | // The representation supports rules that require multiple roots such as: |
| 3012 | // %ptr(p0) = ... |
| 3013 | // %elt0(s32) = G_LOAD %ptr |
| 3014 | // %1(p0) = G_ADD %ptr, 4 |
| 3015 | // %elt1(s32) = G_LOAD p0 %1 |
| 3016 | // which could be usefully folded into: |
| 3017 | // %ptr(p0) = ... |
| 3018 | // %elt0(s32), %elt1(s32) = TGT_LOAD_PAIR %ptr |
| 3019 | // on some targets but we don't need to make use of that yet. |
| 3020 | assert(Matchers.size() == 1 && "Cannot handle multi-root matchers yet"); |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 3021 | |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 3022 | unsigned LabelID = Table.allocateLabelID(); |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 3023 | Table << MatchTable::Opcode("GIM_Try", +1) |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 3024 | << MatchTable::Comment("On fail goto") |
| 3025 | << MatchTable::JumpTarget(LabelID) |
| 3026 | << MatchTable::Comment(("Rule ID " + Twine(RuleID) + " //").str()) |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 3027 | << MatchTable::LineBreak; |
| 3028 | |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 3029 | if (!RequiredFeatures.empty()) { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 3030 | Table << MatchTable::Opcode("GIM_CheckFeatures") |
| 3031 | << MatchTable::NamedValue(getNameForFeatureBitset(RequiredFeatures)) |
| 3032 | << MatchTable::LineBreak; |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 3033 | } |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 3034 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 3035 | Matchers.front()->emitPredicateOpcodes(Table, *this); |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 3036 | |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 3037 | // 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] | 3038 | if (InsnVariableIDs.size() >= 2) { |
Galina Kistanova | 1754fee | 2017-05-25 01:51:53 +0000 | [diff] [blame] | 3039 | // Invert the map to create stable ordering (by var names) |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 3040 | SmallVector<unsigned, 2> InsnIDs; |
| 3041 | for (const auto &Pair : InsnVariableIDs) { |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 3042 | // Skip the root node since it isn't moving anywhere. Everything else is |
| 3043 | // sinking to meet it. |
| 3044 | if (Pair.first == Matchers.front().get()) |
| 3045 | continue; |
| 3046 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 3047 | InsnIDs.push_back(Pair.second); |
Galina Kistanova | 1754fee | 2017-05-25 01:51:53 +0000 | [diff] [blame] | 3048 | } |
Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 3049 | llvm::sort(InsnIDs); |
Galina Kistanova | 1754fee | 2017-05-25 01:51:53 +0000 | [diff] [blame] | 3050 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 3051 | for (const auto &InsnID : InsnIDs) { |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 3052 | // Reject the difficult cases until we have a more accurate check. |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 3053 | Table << MatchTable::Opcode("GIM_CheckIsSafeToFold") |
| 3054 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 3055 | << MatchTable::LineBreak; |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 3056 | |
| 3057 | // FIXME: Emit checks to determine it's _actually_ safe to fold and/or |
| 3058 | // account for unsafe cases. |
| 3059 | // |
| 3060 | // Example: |
| 3061 | // MI1--> %0 = ... |
| 3062 | // %1 = ... %0 |
| 3063 | // MI0--> %2 = ... %0 |
| 3064 | // It's not safe to erase MI1. We currently handle this by not |
| 3065 | // erasing %0 (even when it's dead). |
| 3066 | // |
| 3067 | // Example: |
| 3068 | // MI1--> %0 = load volatile @a |
| 3069 | // %1 = load volatile @a |
| 3070 | // MI0--> %2 = ... %0 |
| 3071 | // It's not safe to sink %0's def past %1. We currently handle |
| 3072 | // this by rejecting all loads. |
| 3073 | // |
| 3074 | // Example: |
| 3075 | // MI1--> %0 = load @a |
| 3076 | // %1 = store @a |
| 3077 | // MI0--> %2 = ... %0 |
| 3078 | // It's not safe to sink %0's def past %1. We currently handle |
| 3079 | // this by rejecting all loads. |
| 3080 | // |
| 3081 | // Example: |
| 3082 | // G_CONDBR %cond, @BB1 |
| 3083 | // BB0: |
| 3084 | // MI1--> %0 = load @a |
| 3085 | // G_BR @BB1 |
| 3086 | // BB1: |
| 3087 | // MI0--> %2 = ... %0 |
| 3088 | // It's not always safe to sink %0 across control flow. In this |
| 3089 | // case it may introduce a memory fault. We currentl handle this |
| 3090 | // by rejecting all loads. |
| 3091 | } |
| 3092 | } |
| 3093 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 3094 | for (const auto &PM : EpilogueMatchers) |
| 3095 | PM->emitPredicateOpcodes(Table, *this); |
| 3096 | |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 3097 | for (const auto &MA : Actions) |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 3098 | MA->emitActionOpcodes(Table, *this); |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 3099 | |
Roman Tereshin | beb3931 | 2018-05-02 20:15:11 +0000 | [diff] [blame] | 3100 | if (Table.isWithCoverage()) |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 3101 | Table << MatchTable::Opcode("GIR_Coverage") << MatchTable::IntValue(RuleID) |
| 3102 | << MatchTable::LineBreak; |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 3103 | else |
| 3104 | Table << MatchTable::Comment(("GIR_Coverage, " + Twine(RuleID) + ",").str()) |
| 3105 | << MatchTable::LineBreak; |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 3106 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 3107 | Table << MatchTable::Opcode("GIR_Done", -1) << MatchTable::LineBreak |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 3108 | << MatchTable::Label(LabelID); |
Volkan Keles | 4f3fa79 | 2018-01-25 00:18:52 +0000 | [diff] [blame] | 3109 | ++NumPatternEmitted; |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 3110 | } |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 3111 | |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 3112 | bool RuleMatcher::isHigherPriorityThan(const RuleMatcher &B) const { |
| 3113 | // Rules involving more match roots have higher priority. |
| 3114 | if (Matchers.size() > B.Matchers.size()) |
| 3115 | return true; |
| 3116 | if (Matchers.size() < B.Matchers.size()) |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 3117 | return false; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 3118 | |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 3119 | for (const auto &Matcher : zip(Matchers, B.Matchers)) { |
| 3120 | if (std::get<0>(Matcher)->isHigherPriorityThan(*std::get<1>(Matcher))) |
| 3121 | return true; |
| 3122 | if (std::get<1>(Matcher)->isHigherPriorityThan(*std::get<0>(Matcher))) |
| 3123 | return false; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 3124 | } |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 3125 | |
| 3126 | return false; |
Simon Pilgrim | a7d1da8 | 2017-03-15 22:50:47 +0000 | [diff] [blame] | 3127 | } |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 3128 | |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 3129 | unsigned RuleMatcher::countRendererFns() const { |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 3130 | return std::accumulate( |
| 3131 | Matchers.begin(), Matchers.end(), 0, |
| 3132 | [](unsigned A, const std::unique_ptr<InstructionMatcher> &Matcher) { |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 3133 | return A + Matcher->countRendererFns(); |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 3134 | }); |
| 3135 | } |
| 3136 | |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 3137 | bool OperandPredicateMatcher::isHigherPriorityThan( |
| 3138 | const OperandPredicateMatcher &B) const { |
| 3139 | // Generally speaking, an instruction is more important than an Int or a |
| 3140 | // LiteralInt because it can cover more nodes but theres an exception to |
| 3141 | // this. G_CONSTANT's are less important than either of those two because they |
| 3142 | // are more permissive. |
Daniel Sanders | edd0784 | 2017-08-17 09:26:14 +0000 | [diff] [blame] | 3143 | |
| 3144 | const InstructionOperandMatcher *AOM = |
| 3145 | dyn_cast<InstructionOperandMatcher>(this); |
| 3146 | const InstructionOperandMatcher *BOM = |
| 3147 | dyn_cast<InstructionOperandMatcher>(&B); |
| 3148 | bool AIsConstantInsn = AOM && AOM->getInsnMatcher().isConstantInstruction(); |
| 3149 | bool BIsConstantInsn = BOM && BOM->getInsnMatcher().isConstantInstruction(); |
| 3150 | |
| 3151 | if (AOM && BOM) { |
| 3152 | // The relative priorities between a G_CONSTANT and any other instruction |
| 3153 | // don't actually matter but this code is needed to ensure a strict weak |
| 3154 | // ordering. This is particularly important on Windows where the rules will |
| 3155 | // be incorrectly sorted without it. |
| 3156 | if (AIsConstantInsn != BIsConstantInsn) |
| 3157 | return AIsConstantInsn < BIsConstantInsn; |
| 3158 | return false; |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 3159 | } |
Daniel Sanders | edd0784 | 2017-08-17 09:26:14 +0000 | [diff] [blame] | 3160 | |
| 3161 | if (AOM && AIsConstantInsn && (B.Kind == OPM_Int || B.Kind == OPM_LiteralInt)) |
| 3162 | return false; |
| 3163 | if (BOM && BIsConstantInsn && (Kind == OPM_Int || Kind == OPM_LiteralInt)) |
| 3164 | return true; |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 3165 | |
| 3166 | return Kind < B.Kind; |
Daniel Sanders | 75b84fc | 2017-08-08 13:21:26 +0000 | [diff] [blame] | 3167 | } |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 3168 | |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 3169 | void SameOperandMatcher::emitPredicateOpcodes(MatchTable &Table, |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 3170 | RuleMatcher &Rule) const { |
Daniel Sanders | 1e4569f | 2017-10-20 20:55:29 +0000 | [diff] [blame] | 3171 | const OperandMatcher &OtherOM = Rule.getOperandMatcher(MatchingName); |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 3172 | unsigned OtherInsnVarID = Rule.getInsnVarID(OtherOM.getInstructionMatcher()); |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 3173 | assert(OtherInsnVarID == OtherOM.getInstructionMatcher().getInsnVarID()); |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 3174 | |
| 3175 | Table << MatchTable::Opcode("GIM_CheckIsSameOperand") |
| 3176 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 3177 | << MatchTable::Comment("OpIdx") << MatchTable::IntValue(OpIdx) |
| 3178 | << MatchTable::Comment("OtherMI") |
| 3179 | << MatchTable::IntValue(OtherInsnVarID) |
| 3180 | << MatchTable::Comment("OtherOpIdx") |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 3181 | << MatchTable::IntValue(OtherOM.getOpIdx()) |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 3182 | << MatchTable::LineBreak; |
| 3183 | } |
| 3184 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 3185 | //===- GlobalISelEmitter class --------------------------------------------===// |
| 3186 | |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 3187 | class GlobalISelEmitter { |
| 3188 | public: |
| 3189 | explicit GlobalISelEmitter(RecordKeeper &RK); |
| 3190 | void run(raw_ostream &OS); |
| 3191 | |
| 3192 | private: |
| 3193 | const RecordKeeper &RK; |
| 3194 | const CodeGenDAGPatterns CGP; |
| 3195 | const CodeGenTarget &Target; |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 3196 | CodeGenRegBank CGRegs; |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 3197 | |
Daniel Sanders | 39690bd | 2017-10-15 02:41:12 +0000 | [diff] [blame] | 3198 | /// Keep track of the equivalence between SDNodes and Instruction by mapping |
Daniel Sanders | 3c1c4c0 | 2017-12-05 05:52:07 +0000 | [diff] [blame] | 3199 | /// SDNodes to the GINodeEquiv mapping. We need to map to the GINodeEquiv to |
| 3200 | /// check for attributes on the relation such as CheckMMOIsNonAtomic. |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 3201 | /// This is defined using 'GINodeEquiv' in the target description. |
Daniel Sanders | 39690bd | 2017-10-15 02:41:12 +0000 | [diff] [blame] | 3202 | DenseMap<Record *, Record *> NodeEquivs; |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 3203 | |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 3204 | /// Keep track of the equivalence between ComplexPattern's and |
| 3205 | /// GIComplexOperandMatcher. Map entries are specified by subclassing |
| 3206 | /// GIComplexPatternEquiv. |
| 3207 | DenseMap<const Record *, const Record *> ComplexPatternEquivs; |
| 3208 | |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 3209 | /// Keep track of the equivalence between SDNodeXForm's and |
| 3210 | /// GICustomOperandRenderer. Map entries are specified by subclassing |
| 3211 | /// GISDNodeXFormEquiv. |
| 3212 | DenseMap<const Record *, const Record *> SDNodeXFormEquivs; |
| 3213 | |
Aditya Nandakumar | b63e763 | 2018-02-16 22:37:15 +0000 | [diff] [blame] | 3214 | /// Keep track of Scores of PatternsToMatch similar to how the DAG does. |
| 3215 | /// This adds compatibility for RuleMatchers to use this for ordering rules. |
| 3216 | DenseMap<uint64_t, int> RuleMatcherScores; |
| 3217 | |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 3218 | // Map of predicates to their subtarget features. |
Daniel Sanders | e9fdba3 | 2017-04-29 17:30:09 +0000 | [diff] [blame] | 3219 | SubtargetFeatureInfoMap SubtargetFeatures; |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 3220 | |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 3221 | // Rule coverage information. |
| 3222 | Optional<CodeGenCoverage> RuleCoverage; |
| 3223 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 3224 | void gatherOpcodeValues(); |
| 3225 | void gatherTypeIDValues(); |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 3226 | void gatherNodeEquivs(); |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 3227 | |
Daniel Sanders | 39690bd | 2017-10-15 02:41:12 +0000 | [diff] [blame] | 3228 | Record *findNodeEquiv(Record *N) const; |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 3229 | const CodeGenInstruction *getEquivNode(Record &Equiv, |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3230 | const TreePatternNode *N) const; |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 3231 | |
Krzysztof Parzyszek | 779d98e | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 3232 | Error importRulePredicates(RuleMatcher &M, ArrayRef<Predicate> Predicates); |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 3233 | Expected<InstructionMatcher &> |
| 3234 | createAndImportSelDAGMatcher(RuleMatcher &Rule, |
| 3235 | InstructionMatcher &InsnMatcher, |
| 3236 | const TreePatternNode *Src, unsigned &TempOpIdx); |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 3237 | Error importComplexPatternOperandMatcher(OperandMatcher &OM, Record *R, |
| 3238 | unsigned &TempOpIdx) const; |
| 3239 | Error importChildMatcher(RuleMatcher &Rule, InstructionMatcher &InsnMatcher, |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3240 | const TreePatternNode *SrcChild, |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 3241 | bool OperandIsAPointer, unsigned OpIdx, |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 3242 | unsigned &TempOpIdx); |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 3243 | |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 3244 | Expected<BuildMIAction &> createAndImportInstructionRenderer( |
| 3245 | RuleMatcher &M, InstructionMatcher &InsnMatcher, |
| 3246 | const TreePatternNode *Src, const TreePatternNode *Dst); |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 3247 | Expected<action_iterator> createAndImportSubInstructionRenderer( |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3248 | action_iterator InsertPt, RuleMatcher &M, const TreePatternNode *Dst, |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 3249 | unsigned TempReg); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3250 | Expected<action_iterator> |
| 3251 | createInstructionRenderer(action_iterator InsertPt, RuleMatcher &M, |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3252 | const TreePatternNode *Dst); |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 3253 | void importExplicitDefRenderers(BuildMIAction &DstMIBuilder); |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 3254 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3255 | Expected<action_iterator> |
| 3256 | importExplicitUseRenderers(action_iterator InsertPt, RuleMatcher &M, |
| 3257 | BuildMIAction &DstMIBuilder, |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3258 | const llvm::TreePatternNode *Dst); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3259 | Expected<action_iterator> |
| 3260 | importExplicitUseRenderer(action_iterator InsertPt, RuleMatcher &Rule, |
| 3261 | BuildMIAction &DstMIBuilder, |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3262 | TreePatternNode *DstChild); |
Sjoerd Meijer | de23484 | 2019-05-30 07:30:37 +0000 | [diff] [blame] | 3263 | Error importDefaultOperandRenderers(action_iterator InsertPt, RuleMatcher &M, |
| 3264 | BuildMIAction &DstMIBuilder, |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 3265 | DagInit *DefaultOps) const; |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 3266 | Error |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3267 | importImplicitDefRenderers(BuildMIAction &DstMIBuilder, |
| 3268 | const std::vector<Record *> &ImplicitDefs) const; |
| 3269 | |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 3270 | void emitCxxPredicateFns(raw_ostream &OS, StringRef CodeFieldName, |
| 3271 | StringRef TypeIdentifier, StringRef ArgType, |
| 3272 | StringRef ArgName, StringRef AdditionalDeclarations, |
| 3273 | std::function<bool(const Record *R)> Filter); |
| 3274 | void emitImmPredicateFns(raw_ostream &OS, StringRef TypeIdentifier, |
| 3275 | StringRef ArgType, |
| 3276 | std::function<bool(const Record *R)> Filter); |
| 3277 | void emitMIPredicateFns(raw_ostream &OS); |
Daniel Sanders | 649c585 | 2017-10-13 20:42:18 +0000 | [diff] [blame] | 3278 | |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 3279 | /// Analyze pattern \p P, returning a matcher for it if possible. |
| 3280 | /// Otherwise, return an Error explaining why we don't support it. |
| 3281 | Expected<RuleMatcher> runOnPattern(const PatternToMatch &P); |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 3282 | |
| 3283 | void declareSubtargetFeature(Record *Predicate); |
Daniel Sanders | 7e52367 | 2017-11-11 03:23:44 +0000 | [diff] [blame] | 3284 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 3285 | MatchTable buildMatchTable(MutableArrayRef<RuleMatcher> Rules, bool Optimize, |
| 3286 | bool WithCoverage); |
| 3287 | |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 3288 | /// Infer a CodeGenRegisterClass for the type of \p SuperRegNode. The returned |
| 3289 | /// CodeGenRegisterClass will support the CodeGenRegisterClass of |
| 3290 | /// \p SubRegNode, and the subregister index defined by \p SubRegIdxNode. |
| 3291 | /// If no register class is found, return None. |
| 3292 | Optional<const CodeGenRegisterClass *> |
Jessica Paquette | 7080ffa | 2019-08-28 20:12:31 +0000 | [diff] [blame] | 3293 | inferSuperRegisterClassForNode(const TypeSetByHwMode &Ty, |
| 3294 | TreePatternNode *SuperRegNode, |
| 3295 | TreePatternNode *SubRegIdxNode); |
Matt Arsenault | 9ceb6ed | 2019-09-06 00:05:58 +0000 | [diff] [blame] | 3296 | Optional<CodeGenSubRegIndex *> |
| 3297 | inferSubRegIndexForNode(TreePatternNode *SubRegIdxNode); |
Jessica Paquette | 7080ffa | 2019-08-28 20:12:31 +0000 | [diff] [blame] | 3298 | |
| 3299 | /// Infer a CodeGenRegisterClass which suppoorts \p Ty and \p SubRegIdxNode. |
| 3300 | /// Return None if no such class exists. |
| 3301 | Optional<const CodeGenRegisterClass *> |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 3302 | inferSuperRegisterClass(const TypeSetByHwMode &Ty, |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 3303 | TreePatternNode *SubRegIdxNode); |
| 3304 | |
| 3305 | /// Return the CodeGenRegisterClass associated with \p Leaf if it has one. |
| 3306 | Optional<const CodeGenRegisterClass *> |
| 3307 | getRegClassFromLeaf(TreePatternNode *Leaf); |
| 3308 | |
| 3309 | /// Return a CodeGenRegisterClass for \p N if one can be found. Return None |
| 3310 | /// otherwise. |
| 3311 | Optional<const CodeGenRegisterClass *> |
| 3312 | inferRegClassFromPattern(TreePatternNode *N); |
| 3313 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 3314 | public: |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 3315 | /// 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] | 3316 | /// they share. \p MatcherStorage is used as a memory container |
Hiroshi Inoue | 501931b | 2018-01-24 05:04:35 +0000 | [diff] [blame] | 3317 | /// for the group that are created as part of this process. |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 3318 | /// |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 3319 | /// What this optimization does looks like if GroupT = GroupMatcher: |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 3320 | /// Output without optimization: |
| 3321 | /// \verbatim |
| 3322 | /// # R1 |
| 3323 | /// # predicate A |
| 3324 | /// # predicate B |
| 3325 | /// ... |
| 3326 | /// # R2 |
| 3327 | /// # predicate A // <-- effectively this is going to be checked twice. |
| 3328 | /// // Once in R1 and once in R2. |
| 3329 | /// # predicate C |
| 3330 | /// \endverbatim |
| 3331 | /// Output with optimization: |
| 3332 | /// \verbatim |
| 3333 | /// # Group1_2 |
| 3334 | /// # predicate A // <-- Check is now shared. |
| 3335 | /// # R1 |
| 3336 | /// # predicate B |
| 3337 | /// # R2 |
| 3338 | /// # predicate C |
| 3339 | /// \endverbatim |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 3340 | template <class GroupT> |
| 3341 | static std::vector<Matcher *> optimizeRules( |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 3342 | ArrayRef<Matcher *> Rules, |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 3343 | std::vector<std::unique_ptr<Matcher>> &MatcherStorage); |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 3344 | }; |
| 3345 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 3346 | void GlobalISelEmitter::gatherOpcodeValues() { |
| 3347 | InstructionOpcodeMatcher::initOpcodeValuesMap(Target); |
| 3348 | } |
| 3349 | |
| 3350 | void GlobalISelEmitter::gatherTypeIDValues() { |
| 3351 | LLTOperandMatcher::initTypeIDValuesMap(); |
| 3352 | } |
| 3353 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 3354 | void GlobalISelEmitter::gatherNodeEquivs() { |
| 3355 | assert(NodeEquivs.empty()); |
| 3356 | for (Record *Equiv : RK.getAllDerivedDefinitions("GINodeEquiv")) |
Daniel Sanders | 39690bd | 2017-10-15 02:41:12 +0000 | [diff] [blame] | 3357 | NodeEquivs[Equiv->getValueAsDef("Node")] = Equiv; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 3358 | |
| 3359 | assert(ComplexPatternEquivs.empty()); |
| 3360 | for (Record *Equiv : RK.getAllDerivedDefinitions("GIComplexPatternEquiv")) { |
| 3361 | Record *SelDAGEquiv = Equiv->getValueAsDef("SelDAGEquivalent"); |
| 3362 | if (!SelDAGEquiv) |
| 3363 | continue; |
| 3364 | ComplexPatternEquivs[SelDAGEquiv] = Equiv; |
| 3365 | } |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 3366 | |
| 3367 | assert(SDNodeXFormEquivs.empty()); |
| 3368 | for (Record *Equiv : RK.getAllDerivedDefinitions("GISDNodeXFormEquiv")) { |
| 3369 | Record *SelDAGEquiv = Equiv->getValueAsDef("SelDAGEquivalent"); |
| 3370 | if (!SelDAGEquiv) |
| 3371 | continue; |
| 3372 | SDNodeXFormEquivs[SelDAGEquiv] = Equiv; |
| 3373 | } |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 3374 | } |
| 3375 | |
Daniel Sanders | 39690bd | 2017-10-15 02:41:12 +0000 | [diff] [blame] | 3376 | Record *GlobalISelEmitter::findNodeEquiv(Record *N) const { |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 3377 | return NodeEquivs.lookup(N); |
| 3378 | } |
| 3379 | |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 3380 | const CodeGenInstruction * |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3381 | GlobalISelEmitter::getEquivNode(Record &Equiv, const TreePatternNode *N) const { |
Matt Arsenault | 8ec5c10 | 2019-08-29 01:13:41 +0000 | [diff] [blame] | 3382 | if (N->getNumChildren() >= 1) { |
| 3383 | // setcc operation maps to two different G_* instructions based on the type. |
| 3384 | if (!Equiv.isValueUnset("IfFloatingPoint") && |
| 3385 | MVT(N->getChild(0)->getSimpleType(0)).isFloatingPoint()) |
| 3386 | return &Target.getInstruction(Equiv.getValueAsDef("IfFloatingPoint")); |
| 3387 | } |
| 3388 | |
Nicolai Haehnle | 445b0b6 | 2018-11-30 14:15:13 +0000 | [diff] [blame] | 3389 | for (const TreePredicateCall &Call : N->getPredicateCalls()) { |
| 3390 | const TreePredicateFn &Predicate = Call.Fn; |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 3391 | if (!Equiv.isValueUnset("IfSignExtend") && Predicate.isLoad() && |
| 3392 | Predicate.isSignExtLoad()) |
| 3393 | return &Target.getInstruction(Equiv.getValueAsDef("IfSignExtend")); |
| 3394 | if (!Equiv.isValueUnset("IfZeroExtend") && Predicate.isLoad() && |
| 3395 | Predicate.isZeroExtLoad()) |
| 3396 | return &Target.getInstruction(Equiv.getValueAsDef("IfZeroExtend")); |
| 3397 | } |
Matt Arsenault | 8ec5c10 | 2019-08-29 01:13:41 +0000 | [diff] [blame] | 3398 | |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 3399 | return &Target.getInstruction(Equiv.getValueAsDef("I")); |
| 3400 | } |
| 3401 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 3402 | GlobalISelEmitter::GlobalISelEmitter(RecordKeeper &RK) |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 3403 | : RK(RK), CGP(RK), Target(CGP.getTargetInfo()), |
| 3404 | CGRegs(RK, Target.getHwModes()) {} |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 3405 | |
| 3406 | //===- Emitter ------------------------------------------------------------===// |
| 3407 | |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 3408 | Error |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3409 | GlobalISelEmitter::importRulePredicates(RuleMatcher &M, |
Krzysztof Parzyszek | 779d98e | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 3410 | ArrayRef<Predicate> Predicates) { |
| 3411 | for (const Predicate &P : Predicates) { |
Matt Arsenault | 57ef94f | 2019-07-30 15:56:43 +0000 | [diff] [blame] | 3412 | if (!P.Def || P.getCondString().empty()) |
Krzysztof Parzyszek | 779d98e | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 3413 | continue; |
| 3414 | declareSubtargetFeature(P.Def); |
| 3415 | M.addRequiredFeature(P.Def); |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 3416 | } |
| 3417 | |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 3418 | return Error::success(); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3419 | } |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 3420 | |
Daniel Sanders | 3c1c4c0 | 2017-12-05 05:52:07 +0000 | [diff] [blame] | 3421 | Expected<InstructionMatcher &> GlobalISelEmitter::createAndImportSelDAGMatcher( |
| 3422 | RuleMatcher &Rule, InstructionMatcher &InsnMatcher, |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 3423 | const TreePatternNode *Src, unsigned &TempOpIdx) { |
Daniel Sanders | 3c1c4c0 | 2017-12-05 05:52:07 +0000 | [diff] [blame] | 3424 | Record *SrcGIEquivOrNull = nullptr; |
| 3425 | const CodeGenInstruction *SrcGIOrNull = nullptr; |
| 3426 | |
| 3427 | // Start with the defined operands (i.e., the results of the root operator). |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3428 | if (Src->getExtTypes().size() > 1) |
Daniel Sanders | 3c1c4c0 | 2017-12-05 05:52:07 +0000 | [diff] [blame] | 3429 | return failedImport("Src pattern has multiple results"); |
| 3430 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3431 | if (Src->isLeaf()) { |
| 3432 | Init *SrcInit = Src->getLeafValue(); |
Daniel Sanders | 3c1c4c0 | 2017-12-05 05:52:07 +0000 | [diff] [blame] | 3433 | if (isa<IntInit>(SrcInit)) { |
| 3434 | InsnMatcher.addPredicate<InstructionOpcodeMatcher>( |
| 3435 | &Target.getInstruction(RK.getDef("G_CONSTANT"))); |
| 3436 | } else |
| 3437 | return failedImport( |
| 3438 | "Unable to deduce gMIR opcode to handle Src (which is a leaf)"); |
| 3439 | } else { |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3440 | SrcGIEquivOrNull = findNodeEquiv(Src->getOperator()); |
Daniel Sanders | 3c1c4c0 | 2017-12-05 05:52:07 +0000 | [diff] [blame] | 3441 | if (!SrcGIEquivOrNull) |
| 3442 | return failedImport("Pattern operator lacks an equivalent Instruction" + |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3443 | explainOperator(Src->getOperator())); |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 3444 | SrcGIOrNull = getEquivNode(*SrcGIEquivOrNull, Src); |
Daniel Sanders | 3c1c4c0 | 2017-12-05 05:52:07 +0000 | [diff] [blame] | 3445 | |
| 3446 | // The operators look good: match the opcode |
| 3447 | InsnMatcher.addPredicate<InstructionOpcodeMatcher>(SrcGIOrNull); |
| 3448 | } |
| 3449 | |
| 3450 | unsigned OpIdx = 0; |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3451 | for (const TypeSetByHwMode &VTy : Src->getExtTypes()) { |
Daniel Sanders | 3c1c4c0 | 2017-12-05 05:52:07 +0000 | [diff] [blame] | 3452 | // Results don't have a name unless they are the root node. The caller will |
| 3453 | // set the name if appropriate. |
| 3454 | OperandMatcher &OM = InsnMatcher.addOperand(OpIdx++, "", TempOpIdx); |
| 3455 | if (auto Error = OM.addTypeCheckPredicate(VTy, false /* OperandIsAPointer */)) |
| 3456 | return failedImport(toString(std::move(Error)) + |
| 3457 | " for result of Src pattern operator"); |
| 3458 | } |
| 3459 | |
Nicolai Haehnle | 445b0b6 | 2018-11-30 14:15:13 +0000 | [diff] [blame] | 3460 | for (const TreePredicateCall &Call : Src->getPredicateCalls()) { |
| 3461 | const TreePredicateFn &Predicate = Call.Fn; |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 3462 | if (Predicate.isAlwaysTrue()) |
| 3463 | continue; |
| 3464 | |
| 3465 | if (Predicate.isImmediatePattern()) { |
| 3466 | InsnMatcher.addPredicate<InstructionImmPredicateMatcher>(Predicate); |
| 3467 | continue; |
| 3468 | } |
| 3469 | |
Matt Arsenault | d00d857 | 2019-07-15 20:59:42 +0000 | [diff] [blame] | 3470 | // An address space check is needed in all contexts if there is one. |
| 3471 | if (Predicate.isLoad() || Predicate.isStore() || Predicate.isAtomic()) { |
| 3472 | if (const ListInit *AddrSpaces = Predicate.getAddressSpaces()) { |
| 3473 | SmallVector<unsigned, 4> ParsedAddrSpaces; |
| 3474 | |
| 3475 | for (Init *Val : AddrSpaces->getValues()) { |
| 3476 | IntInit *IntVal = dyn_cast<IntInit>(Val); |
| 3477 | if (!IntVal) |
| 3478 | return failedImport("Address space is not an integer"); |
| 3479 | ParsedAddrSpaces.push_back(IntVal->getValue()); |
| 3480 | } |
| 3481 | |
| 3482 | if (!ParsedAddrSpaces.empty()) { |
| 3483 | InsnMatcher.addPredicate<MemoryAddressSpacePredicateMatcher>( |
| 3484 | 0, ParsedAddrSpaces); |
| 3485 | } |
| 3486 | } |
Matt Arsenault | 52c2624 | 2019-07-31 00:14:43 +0000 | [diff] [blame] | 3487 | |
| 3488 | int64_t MinAlign = Predicate.getMinAlignment(); |
| 3489 | if (MinAlign > 0) |
| 3490 | InsnMatcher.addPredicate<MemoryAlignmentPredicateMatcher>(0, MinAlign); |
Matt Arsenault | d00d857 | 2019-07-15 20:59:42 +0000 | [diff] [blame] | 3491 | } |
| 3492 | |
| 3493 | // G_LOAD is used for both non-extending and any-extending loads. |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 3494 | if (Predicate.isLoad() && Predicate.isNonExtLoad()) { |
| 3495 | InsnMatcher.addPredicate<MemoryVsLLTSizePredicateMatcher>( |
| 3496 | 0, MemoryVsLLTSizePredicateMatcher::EqualTo, 0); |
| 3497 | continue; |
| 3498 | } |
| 3499 | if (Predicate.isLoad() && Predicate.isAnyExtLoad()) { |
| 3500 | InsnMatcher.addPredicate<MemoryVsLLTSizePredicateMatcher>( |
| 3501 | 0, MemoryVsLLTSizePredicateMatcher::LessThan, 0); |
| 3502 | continue; |
| 3503 | } |
| 3504 | |
Amara Emerson | 52e6d52 | 2019-08-02 23:33:13 +0000 | [diff] [blame] | 3505 | if (Predicate.isStore()) { |
| 3506 | if (Predicate.isTruncStore()) { |
| 3507 | // FIXME: If MemoryVT is set, we end up with 2 checks for the MMO size. |
| 3508 | InsnMatcher.addPredicate<MemoryVsLLTSizePredicateMatcher>( |
| 3509 | 0, MemoryVsLLTSizePredicateMatcher::LessThan, 0); |
| 3510 | continue; |
| 3511 | } |
| 3512 | if (Predicate.isNonTruncStore()) { |
| 3513 | // We need to check the sizes match here otherwise we could incorrectly |
| 3514 | // match truncating stores with non-truncating ones. |
| 3515 | InsnMatcher.addPredicate<MemoryVsLLTSizePredicateMatcher>( |
| 3516 | 0, MemoryVsLLTSizePredicateMatcher::EqualTo, 0); |
| 3517 | } |
Matt Arsenault | 0277249 | 2019-07-15 21:15:20 +0000 | [diff] [blame] | 3518 | } |
| 3519 | |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 3520 | // No check required. We already did it by swapping the opcode. |
| 3521 | if (!SrcGIEquivOrNull->isValueUnset("IfSignExtend") && |
| 3522 | Predicate.isSignExtLoad()) |
| 3523 | continue; |
| 3524 | |
| 3525 | // No check required. We already did it by swapping the opcode. |
| 3526 | if (!SrcGIEquivOrNull->isValueUnset("IfZeroExtend") && |
| 3527 | Predicate.isZeroExtLoad()) |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 3528 | continue; |
| 3529 | |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 3530 | // No check required. G_STORE by itself is a non-extending store. |
| 3531 | if (Predicate.isNonTruncStore()) |
| 3532 | continue; |
| 3533 | |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 3534 | if (Predicate.isLoad() || Predicate.isStore() || Predicate.isAtomic()) { |
| 3535 | if (Predicate.getMemoryVT() != nullptr) { |
| 3536 | Optional<LLTCodeGen> MemTyOrNone = |
| 3537 | MVTToLLT(getValueType(Predicate.getMemoryVT())); |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 3538 | |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 3539 | if (!MemTyOrNone) |
| 3540 | return failedImport("MemVT could not be converted to LLT"); |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 3541 | |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 3542 | // MMO's work in bytes so we must take care of unusual types like i1 |
| 3543 | // don't round down. |
| 3544 | unsigned MemSizeInBits = |
| 3545 | llvm::alignTo(MemTyOrNone->get().getSizeInBits(), 8); |
| 3546 | |
| 3547 | InsnMatcher.addPredicate<MemorySizePredicateMatcher>( |
| 3548 | 0, MemSizeInBits / 8); |
Daniel Sanders | 7666465 | 2017-11-28 22:07:05 +0000 | [diff] [blame] | 3549 | continue; |
| 3550 | } |
| 3551 | } |
| 3552 | |
| 3553 | if (Predicate.isLoad() || Predicate.isStore()) { |
| 3554 | // No check required. A G_LOAD/G_STORE is an unindexed load. |
| 3555 | if (Predicate.isUnindexed()) |
| 3556 | continue; |
| 3557 | } |
| 3558 | |
| 3559 | if (Predicate.isAtomic()) { |
| 3560 | if (Predicate.isAtomicOrderingMonotonic()) { |
| 3561 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>( |
| 3562 | "Monotonic"); |
| 3563 | continue; |
| 3564 | } |
| 3565 | if (Predicate.isAtomicOrderingAcquire()) { |
| 3566 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>("Acquire"); |
| 3567 | continue; |
| 3568 | } |
| 3569 | if (Predicate.isAtomicOrderingRelease()) { |
| 3570 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>("Release"); |
| 3571 | continue; |
| 3572 | } |
| 3573 | if (Predicate.isAtomicOrderingAcquireRelease()) { |
| 3574 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>( |
| 3575 | "AcquireRelease"); |
| 3576 | continue; |
| 3577 | } |
| 3578 | if (Predicate.isAtomicOrderingSequentiallyConsistent()) { |
| 3579 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>( |
| 3580 | "SequentiallyConsistent"); |
| 3581 | continue; |
| 3582 | } |
Daniel Sanders | 0c43b3a | 2017-11-30 21:05:59 +0000 | [diff] [blame] | 3583 | |
| 3584 | if (Predicate.isAtomicOrderingAcquireOrStronger()) { |
| 3585 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>( |
| 3586 | "Acquire", AtomicOrderingMMOPredicateMatcher::AO_OrStronger); |
| 3587 | continue; |
| 3588 | } |
| 3589 | if (Predicate.isAtomicOrderingWeakerThanAcquire()) { |
| 3590 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>( |
| 3591 | "Acquire", AtomicOrderingMMOPredicateMatcher::AO_WeakerThan); |
| 3592 | continue; |
| 3593 | } |
| 3594 | |
| 3595 | if (Predicate.isAtomicOrderingReleaseOrStronger()) { |
| 3596 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>( |
| 3597 | "Release", AtomicOrderingMMOPredicateMatcher::AO_OrStronger); |
| 3598 | continue; |
| 3599 | } |
| 3600 | if (Predicate.isAtomicOrderingWeakerThanRelease()) { |
| 3601 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>( |
| 3602 | "Release", AtomicOrderingMMOPredicateMatcher::AO_WeakerThan); |
| 3603 | continue; |
| 3604 | } |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 3605 | } |
| 3606 | |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 3607 | if (Predicate.hasGISelPredicateCode()) { |
| 3608 | InsnMatcher.addPredicate<GenericInstructionPredicateMatcher>(Predicate); |
| 3609 | continue; |
| 3610 | } |
| 3611 | |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 3612 | return failedImport("Src pattern child has predicate (" + |
| 3613 | explainPredicates(Src) + ")"); |
| 3614 | } |
Daniel Sanders | 3c1c4c0 | 2017-12-05 05:52:07 +0000 | [diff] [blame] | 3615 | if (SrcGIEquivOrNull && SrcGIEquivOrNull->getValueAsBit("CheckMMOIsNonAtomic")) |
| 3616 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>("NotAtomic"); |
Matt Arsenault | 63e6d8d | 2019-09-09 16:18:07 +0000 | [diff] [blame] | 3617 | else if (SrcGIEquivOrNull && SrcGIEquivOrNull->getValueAsBit("CheckMMOIsAtomic")) { |
| 3618 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>( |
| 3619 | "Unordered", AtomicOrderingMMOPredicateMatcher::AO_OrStronger); |
| 3620 | } |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 3621 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3622 | if (Src->isLeaf()) { |
| 3623 | Init *SrcInit = Src->getLeafValue(); |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 3624 | if (IntInit *SrcIntInit = dyn_cast<IntInit>(SrcInit)) { |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 3625 | OperandMatcher &OM = |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3626 | InsnMatcher.addOperand(OpIdx++, Src->getName(), TempOpIdx); |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 3627 | OM.addPredicate<LiteralIntOperandMatcher>(SrcIntInit->getValue()); |
| 3628 | } else |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 3629 | return failedImport( |
| 3630 | "Unable to deduce gMIR opcode to handle Src (which is a leaf)"); |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 3631 | } else { |
Daniel Sanders | 85ffd36 | 2017-07-06 08:12:20 +0000 | [diff] [blame] | 3632 | assert(SrcGIOrNull && |
| 3633 | "Expected to have already found an equivalent Instruction"); |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 3634 | if (SrcGIOrNull->TheDef->getName() == "G_CONSTANT" || |
| 3635 | SrcGIOrNull->TheDef->getName() == "G_FCONSTANT") { |
| 3636 | // 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] | 3637 | // here since we don't support ImmLeaf predicates yet. However, we still |
| 3638 | // need to note the hidden operand to get GIM_CheckNumOperands correct. |
| 3639 | InsnMatcher.addOperand(OpIdx++, "", TempOpIdx); |
| 3640 | return InsnMatcher; |
| 3641 | } |
| 3642 | |
Matt Arsenault | 8ec5c10 | 2019-08-29 01:13:41 +0000 | [diff] [blame] | 3643 | // Special case because the operand order is changed from setcc. The |
| 3644 | // predicate operand needs to be swapped from the last operand to the first |
| 3645 | // source. |
| 3646 | |
| 3647 | unsigned NumChildren = Src->getNumChildren(); |
| 3648 | bool IsFCmp = SrcGIOrNull->TheDef->getName() == "G_FCMP"; |
| 3649 | |
| 3650 | if (IsFCmp || SrcGIOrNull->TheDef->getName() == "G_ICMP") { |
| 3651 | TreePatternNode *SrcChild = Src->getChild(NumChildren - 1); |
| 3652 | if (SrcChild->isLeaf()) { |
| 3653 | DefInit *DI = dyn_cast<DefInit>(SrcChild->getLeafValue()); |
| 3654 | Record *CCDef = DI ? DI->getDef() : nullptr; |
| 3655 | if (!CCDef || !CCDef->isSubClassOf("CondCode")) |
| 3656 | return failedImport("Unable to handle CondCode"); |
| 3657 | |
| 3658 | OperandMatcher &OM = |
| 3659 | InsnMatcher.addOperand(OpIdx++, SrcChild->getName(), TempOpIdx); |
| 3660 | StringRef PredType = IsFCmp ? CCDef->getValueAsString("FCmpPredicate") : |
| 3661 | CCDef->getValueAsString("ICmpPredicate"); |
| 3662 | |
| 3663 | if (!PredType.empty()) { |
| 3664 | OM.addPredicate<CmpPredicateOperandMatcher>(PredType); |
| 3665 | // Process the other 2 operands normally. |
| 3666 | --NumChildren; |
| 3667 | } |
| 3668 | } |
| 3669 | } |
| 3670 | |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 3671 | // Match the used operands (i.e. the children of the operator). |
Jessica Paquette | 5c8a29f | 2019-08-20 22:04:10 +0000 | [diff] [blame] | 3672 | bool IsIntrinsic = |
| 3673 | SrcGIOrNull->TheDef->getName() == "G_INTRINSIC" || |
| 3674 | SrcGIOrNull->TheDef->getName() == "G_INTRINSIC_W_SIDE_EFFECTS"; |
| 3675 | const CodeGenIntrinsic *II = Src->getIntrinsicInfo(CGP); |
| 3676 | if (IsIntrinsic && !II) |
| 3677 | return failedImport("Expected IntInit containing intrinsic ID)"); |
| 3678 | |
Matt Arsenault | 8ec5c10 | 2019-08-29 01:13:41 +0000 | [diff] [blame] | 3679 | for (unsigned i = 0; i != NumChildren; ++i) { |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3680 | TreePatternNode *SrcChild = Src->getChild(i); |
Daniel Sanders | 85ffd36 | 2017-07-06 08:12:20 +0000 | [diff] [blame] | 3681 | |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 3682 | // SelectionDAG allows pointers to be represented with iN since it doesn't |
| 3683 | // distinguish between pointers and integers but they are different types in GlobalISel. |
| 3684 | // 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] | 3685 | bool OperandIsAPointer = SrcGIOrNull->isOperandAPointer(i); |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 3686 | |
Jessica Paquette | 5c8a29f | 2019-08-20 22:04:10 +0000 | [diff] [blame] | 3687 | if (IsIntrinsic) { |
| 3688 | // For G_INTRINSIC/G_INTRINSIC_W_SIDE_EFFECTS, the operand immediately |
| 3689 | // following the defs is an intrinsic ID. |
| 3690 | if (i == 0) { |
Daniel Sanders | 85ffd36 | 2017-07-06 08:12:20 +0000 | [diff] [blame] | 3691 | OperandMatcher &OM = |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3692 | InsnMatcher.addOperand(OpIdx++, SrcChild->getName(), TempOpIdx); |
Daniel Sanders | fe12c0f | 2017-07-11 08:57:29 +0000 | [diff] [blame] | 3693 | OM.addPredicate<IntrinsicIDOperandMatcher>(II); |
Daniel Sanders | 85ffd36 | 2017-07-06 08:12:20 +0000 | [diff] [blame] | 3694 | continue; |
| 3695 | } |
| 3696 | |
Jessica Paquette | 5c8a29f | 2019-08-20 22:04:10 +0000 | [diff] [blame] | 3697 | // We have to check intrinsics for llvm_anyptr_ty parameters. |
| 3698 | // |
| 3699 | // Note that we have to look at the i-1th parameter, because we don't |
| 3700 | // have the intrinsic ID in the intrinsic's parameter list. |
| 3701 | OperandIsAPointer |= II->isParamAPointer(i - 1); |
Daniel Sanders | 85ffd36 | 2017-07-06 08:12:20 +0000 | [diff] [blame] | 3702 | } |
| 3703 | |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 3704 | if (auto Error = |
| 3705 | importChildMatcher(Rule, InsnMatcher, SrcChild, OperandIsAPointer, |
| 3706 | OpIdx++, TempOpIdx)) |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 3707 | return std::move(Error); |
| 3708 | } |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3709 | } |
| 3710 | |
| 3711 | return InsnMatcher; |
| 3712 | } |
| 3713 | |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 3714 | Error GlobalISelEmitter::importComplexPatternOperandMatcher( |
| 3715 | OperandMatcher &OM, Record *R, unsigned &TempOpIdx) const { |
| 3716 | const auto &ComplexPattern = ComplexPatternEquivs.find(R); |
| 3717 | if (ComplexPattern == ComplexPatternEquivs.end()) |
| 3718 | return failedImport("SelectionDAG ComplexPattern (" + R->getName() + |
| 3719 | ") not mapped to GlobalISel"); |
| 3720 | |
| 3721 | OM.addPredicate<ComplexPatternOperandMatcher>(OM, *ComplexPattern->second); |
| 3722 | TempOpIdx++; |
| 3723 | return Error::success(); |
| 3724 | } |
| 3725 | |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 3726 | // Get the name to use for a pattern operand. For an anonymous physical register |
| 3727 | // input, this should use the register name. |
| 3728 | static StringRef getSrcChildName(const TreePatternNode *SrcChild, |
| 3729 | Record *&PhysReg) { |
| 3730 | StringRef SrcChildName = SrcChild->getName(); |
| 3731 | if (SrcChildName.empty() && SrcChild->isLeaf()) { |
| 3732 | if (auto *ChildDefInit = dyn_cast<DefInit>(SrcChild->getLeafValue())) { |
| 3733 | auto *ChildRec = ChildDefInit->getDef(); |
| 3734 | if (ChildRec->isSubClassOf("Register")) { |
| 3735 | SrcChildName = ChildRec->getName(); |
| 3736 | PhysReg = ChildRec; |
| 3737 | } |
| 3738 | } |
| 3739 | } |
| 3740 | |
| 3741 | return SrcChildName; |
| 3742 | } |
| 3743 | |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 3744 | Error GlobalISelEmitter::importChildMatcher(RuleMatcher &Rule, |
| 3745 | InstructionMatcher &InsnMatcher, |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3746 | const TreePatternNode *SrcChild, |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 3747 | bool OperandIsAPointer, |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 3748 | unsigned OpIdx, |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 3749 | unsigned &TempOpIdx) { |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 3750 | |
| 3751 | Record *PhysReg = nullptr; |
| 3752 | StringRef SrcChildName = getSrcChildName(SrcChild, PhysReg); |
| 3753 | |
| 3754 | OperandMatcher &OM = PhysReg ? |
| 3755 | InsnMatcher.addPhysRegInput(PhysReg, OpIdx, TempOpIdx) : |
| 3756 | InsnMatcher.addOperand(OpIdx, SrcChildName, TempOpIdx); |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 3757 | if (OM.isSameAsAnotherOperand()) |
| 3758 | return Error::success(); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3759 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3760 | ArrayRef<TypeSetByHwMode> ChildTypes = SrcChild->getExtTypes(); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3761 | if (ChildTypes.size() != 1) |
| 3762 | return failedImport("Src pattern child has multiple results"); |
| 3763 | |
| 3764 | // Check MBB's before the type check since they are not a known type. |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3765 | if (!SrcChild->isLeaf()) { |
| 3766 | if (SrcChild->getOperator()->isSubClassOf("SDNode")) { |
| 3767 | auto &ChildSDNI = CGP.getSDNodeInfo(SrcChild->getOperator()); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3768 | if (ChildSDNI.getSDClassName() == "BasicBlockSDNode") { |
| 3769 | OM.addPredicate<MBBOperandMatcher>(); |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 3770 | return Error::success(); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3771 | } |
| 3772 | } |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3773 | } |
| 3774 | |
Daniel Sanders | a71f454 | 2017-10-16 00:56:30 +0000 | [diff] [blame] | 3775 | if (auto Error = |
| 3776 | OM.addTypeCheckPredicate(ChildTypes.front(), OperandIsAPointer)) |
| 3777 | return failedImport(toString(std::move(Error)) + " for Src operand (" + |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3778 | to_string(*SrcChild) + ")"); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3779 | |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 3780 | // Check for nested instructions. |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3781 | if (!SrcChild->isLeaf()) { |
| 3782 | if (SrcChild->getOperator()->isSubClassOf("ComplexPattern")) { |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 3783 | // When a ComplexPattern is used as an operator, it should do the same |
| 3784 | // thing as when used as a leaf. However, the children of the operator |
| 3785 | // name the sub-operands that make up the complex operand and we must |
| 3786 | // prepare to reference them in the renderer too. |
| 3787 | unsigned RendererID = TempOpIdx; |
| 3788 | if (auto Error = importComplexPatternOperandMatcher( |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3789 | OM, SrcChild->getOperator(), TempOpIdx)) |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 3790 | return Error; |
| 3791 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3792 | for (unsigned i = 0, e = SrcChild->getNumChildren(); i != e; ++i) { |
| 3793 | auto *SubOperand = SrcChild->getChild(i); |
Jessica Paquette | 1ed1dd6 | 2019-02-09 00:29:13 +0000 | [diff] [blame] | 3794 | if (!SubOperand->getName().empty()) { |
| 3795 | if (auto Error = Rule.defineComplexSubOperand(SubOperand->getName(), |
| 3796 | SrcChild->getOperator(), |
| 3797 | RendererID, i)) |
| 3798 | return Error; |
| 3799 | } |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 3800 | } |
| 3801 | |
| 3802 | return Error::success(); |
| 3803 | } |
| 3804 | |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 3805 | auto MaybeInsnOperand = OM.addPredicate<InstructionOperandMatcher>( |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3806 | InsnMatcher.getRuleMatcher(), SrcChild->getName()); |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 3807 | if (!MaybeInsnOperand.hasValue()) { |
| 3808 | // This isn't strictly true. If the user were to provide exactly the same |
| 3809 | // matchers as the original operand then we could allow it. However, it's |
| 3810 | // simpler to not permit the redundant specification. |
| 3811 | return failedImport("Nested instruction cannot be the same as another operand"); |
| 3812 | } |
| 3813 | |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 3814 | // Map the node to a gMIR instruction. |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 3815 | InstructionOperandMatcher &InsnOperand = **MaybeInsnOperand; |
Daniel Sanders | 57938df | 2017-07-11 10:40:18 +0000 | [diff] [blame] | 3816 | auto InsnMatcherOrError = createAndImportSelDAGMatcher( |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 3817 | Rule, InsnOperand.getInsnMatcher(), SrcChild, TempOpIdx); |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 3818 | if (auto Error = InsnMatcherOrError.takeError()) |
| 3819 | return Error; |
| 3820 | |
| 3821 | return Error::success(); |
| 3822 | } |
| 3823 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3824 | if (SrcChild->hasAnyPredicate()) |
Diana Picus | d1b6181 | 2017-11-03 10:30:19 +0000 | [diff] [blame] | 3825 | return failedImport("Src pattern child has unsupported predicate"); |
| 3826 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3827 | // Check for constant immediates. |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3828 | if (auto *ChildInt = dyn_cast<IntInit>(SrcChild->getLeafValue())) { |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 3829 | OM.addPredicate<ConstantIntOperandMatcher>(ChildInt->getValue()); |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 3830 | return Error::success(); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3831 | } |
| 3832 | |
| 3833 | // Check for def's like register classes or ComplexPattern's. |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3834 | if (auto *ChildDefInit = dyn_cast<DefInit>(SrcChild->getLeafValue())) { |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3835 | auto *ChildRec = ChildDefInit->getDef(); |
| 3836 | |
| 3837 | // Check for register classes. |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 3838 | if (ChildRec->isSubClassOf("RegisterClass") || |
| 3839 | ChildRec->isSubClassOf("RegisterOperand")) { |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3840 | OM.addPredicate<RegisterBankOperandMatcher>( |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 3841 | Target.getRegisterClass(getInitValueAsRegClass(ChildDefInit))); |
Daniel Sanders | 658541f | 2017-04-22 15:53:21 +0000 | [diff] [blame] | 3842 | return Error::success(); |
| 3843 | } |
| 3844 | |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 3845 | if (ChildRec->isSubClassOf("Register")) { |
| 3846 | // This just be emitted as a copy to the specific register. |
| 3847 | ValueTypeByHwMode VT = ChildTypes.front().getValueTypeByHwMode(); |
| 3848 | const CodeGenRegisterClass *RC |
| 3849 | = CGRegs.getMinimalPhysRegClass(ChildRec, &VT); |
| 3850 | if (!RC) { |
| 3851 | return failedImport( |
| 3852 | "Could not determine physical register class of pattern source"); |
| 3853 | } |
| 3854 | |
| 3855 | OM.addPredicate<RegisterBankOperandMatcher>(*RC); |
| 3856 | return Error::success(); |
| 3857 | } |
| 3858 | |
Daniel Sanders | 4d4e765 | 2017-10-09 18:14:53 +0000 | [diff] [blame] | 3859 | // Check for ValueType. |
| 3860 | if (ChildRec->isSubClassOf("ValueType")) { |
| 3861 | // We already added a type check as standard practice so this doesn't need |
| 3862 | // to do anything. |
| 3863 | return Error::success(); |
| 3864 | } |
| 3865 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3866 | // Check for ComplexPattern's. |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 3867 | if (ChildRec->isSubClassOf("ComplexPattern")) |
| 3868 | return importComplexPatternOperandMatcher(OM, ChildRec, TempOpIdx); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3869 | |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 3870 | if (ChildRec->isSubClassOf("ImmLeaf")) { |
| 3871 | return failedImport( |
| 3872 | "Src pattern child def is an unsupported tablegen class (ImmLeaf)"); |
| 3873 | } |
| 3874 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3875 | return failedImport( |
| 3876 | "Src pattern child def is an unsupported tablegen class"); |
| 3877 | } |
| 3878 | |
| 3879 | return failedImport("Src pattern child is an unsupported kind"); |
| 3880 | } |
| 3881 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3882 | Expected<action_iterator> GlobalISelEmitter::importExplicitUseRenderer( |
| 3883 | action_iterator InsertPt, RuleMatcher &Rule, BuildMIAction &DstMIBuilder, |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3884 | TreePatternNode *DstChild) { |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 3885 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3886 | const auto &SubOperand = Rule.getComplexSubOperand(DstChild->getName()); |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 3887 | if (SubOperand.hasValue()) { |
| 3888 | DstMIBuilder.addRenderer<RenderComplexPatternOperand>( |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3889 | *std::get<0>(*SubOperand), DstChild->getName(), |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 3890 | std::get<1>(*SubOperand), std::get<2>(*SubOperand)); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3891 | return InsertPt; |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 3892 | } |
| 3893 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3894 | if (!DstChild->isLeaf()) { |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 3895 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3896 | if (DstChild->getOperator()->isSubClassOf("SDNodeXForm")) { |
| 3897 | auto Child = DstChild->getChild(0); |
| 3898 | auto I = SDNodeXFormEquivs.find(DstChild->getOperator()); |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 3899 | if (I != SDNodeXFormEquivs.end()) { |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3900 | DstMIBuilder.addRenderer<CustomRenderer>(*I->second, Child->getName()); |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 3901 | return InsertPt; |
| 3902 | } |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3903 | return failedImport("SDNodeXForm " + Child->getName() + |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 3904 | " has no custom renderer"); |
| 3905 | } |
| 3906 | |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 3907 | // We accept 'bb' here. It's an operator because BasicBlockSDNode isn't |
| 3908 | // inline, but in MI it's just another operand. |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3909 | if (DstChild->getOperator()->isSubClassOf("SDNode")) { |
| 3910 | auto &ChildSDNI = CGP.getSDNodeInfo(DstChild->getOperator()); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3911 | if (ChildSDNI.getSDClassName() == "BasicBlockSDNode") { |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3912 | DstMIBuilder.addRenderer<CopyRenderer>(DstChild->getName()); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3913 | return InsertPt; |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3914 | } |
| 3915 | } |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 3916 | |
| 3917 | // Similarly, imm is an operator in TreePatternNode's view but must be |
| 3918 | // rendered as operands. |
| 3919 | // FIXME: The target should be able to choose sign-extended when appropriate |
| 3920 | // (e.g. on Mips). |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3921 | if (DstChild->getOperator()->getName() == "imm") { |
| 3922 | DstMIBuilder.addRenderer<CopyConstantAsImmRenderer>(DstChild->getName()); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3923 | return InsertPt; |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3924 | } else if (DstChild->getOperator()->getName() == "fpimm") { |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 3925 | DstMIBuilder.addRenderer<CopyFConstantAsFPImmRenderer>( |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3926 | DstChild->getName()); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3927 | return InsertPt; |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 3928 | } |
| 3929 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3930 | if (DstChild->getOperator()->isSubClassOf("Instruction")) { |
| 3931 | ArrayRef<TypeSetByHwMode> ChildTypes = DstChild->getExtTypes(); |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 3932 | if (ChildTypes.size() != 1) |
| 3933 | return failedImport("Dst pattern child has multiple results"); |
| 3934 | |
| 3935 | Optional<LLTCodeGen> OpTyOrNone = None; |
| 3936 | if (ChildTypes.front().isMachineValueType()) |
| 3937 | OpTyOrNone = |
| 3938 | MVTToLLT(ChildTypes.front().getMachineValueType().SimpleTy); |
| 3939 | if (!OpTyOrNone) |
| 3940 | return failedImport("Dst operand has an unsupported type"); |
| 3941 | |
| 3942 | unsigned TempRegID = Rule.allocateTempRegID(); |
| 3943 | InsertPt = Rule.insertAction<MakeTempRegisterAction>( |
| 3944 | InsertPt, OpTyOrNone.getValue(), TempRegID); |
| 3945 | DstMIBuilder.addRenderer<TempRegRenderer>(TempRegID); |
| 3946 | |
| 3947 | auto InsertPtOrError = createAndImportSubInstructionRenderer( |
| 3948 | ++InsertPt, Rule, DstChild, TempRegID); |
| 3949 | if (auto Error = InsertPtOrError.takeError()) |
| 3950 | return std::move(Error); |
| 3951 | return InsertPtOrError.get(); |
| 3952 | } |
| 3953 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3954 | 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] | 3955 | } |
| 3956 | |
Daniel Sanders | f499b2b | 2017-11-30 18:48:35 +0000 | [diff] [blame] | 3957 | // It could be a specific immediate in which case we should just check for |
| 3958 | // that immediate. |
| 3959 | if (const IntInit *ChildIntInit = |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3960 | dyn_cast<IntInit>(DstChild->getLeafValue())) { |
Daniel Sanders | f499b2b | 2017-11-30 18:48:35 +0000 | [diff] [blame] | 3961 | DstMIBuilder.addRenderer<ImmRenderer>(ChildIntInit->getValue()); |
| 3962 | return InsertPt; |
| 3963 | } |
| 3964 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3965 | // Otherwise, we're looking for a bog-standard RegisterClass operand. |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3966 | if (auto *ChildDefInit = dyn_cast<DefInit>(DstChild->getLeafValue())) { |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3967 | auto *ChildRec = ChildDefInit->getDef(); |
| 3968 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3969 | ArrayRef<TypeSetByHwMode> ChildTypes = DstChild->getExtTypes(); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3970 | if (ChildTypes.size() != 1) |
| 3971 | return failedImport("Dst pattern child has multiple results"); |
| 3972 | |
Krzysztof Parzyszek | 779d98e | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 3973 | Optional<LLTCodeGen> OpTyOrNone = None; |
| 3974 | if (ChildTypes.front().isMachineValueType()) |
| 3975 | OpTyOrNone = MVTToLLT(ChildTypes.front().getMachineValueType().SimpleTy); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3976 | if (!OpTyOrNone) |
| 3977 | return failedImport("Dst operand has an unsupported type"); |
| 3978 | |
| 3979 | if (ChildRec->isSubClassOf("Register")) { |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 3980 | DstMIBuilder.addRenderer<AddRegisterRenderer>(ChildRec); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3981 | return InsertPt; |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3982 | } |
| 3983 | |
Daniel Sanders | 658541f | 2017-04-22 15:53:21 +0000 | [diff] [blame] | 3984 | if (ChildRec->isSubClassOf("RegisterClass") || |
Daniel Sanders | 4d4e765 | 2017-10-09 18:14:53 +0000 | [diff] [blame] | 3985 | ChildRec->isSubClassOf("RegisterOperand") || |
| 3986 | ChildRec->isSubClassOf("ValueType")) { |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 3987 | if (ChildRec->isSubClassOf("RegisterOperand") && |
| 3988 | !ChildRec->isValueUnset("GIZeroRegister")) { |
| 3989 | DstMIBuilder.addRenderer<CopyOrAddZeroRegRenderer>( |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3990 | DstChild->getName(), ChildRec->getValueAsDef("GIZeroRegister")); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3991 | return InsertPt; |
Daniel Sanders | d66e090 | 2017-10-23 18:19:24 +0000 | [diff] [blame] | 3992 | } |
| 3993 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 3994 | DstMIBuilder.addRenderer<CopyRenderer>(DstChild->getName()); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 3995 | return InsertPt; |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 3996 | } |
| 3997 | |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 3998 | if (ChildRec->isSubClassOf("SubRegIndex")) { |
| 3999 | CodeGenSubRegIndex *SubIdx = CGRegs.getSubRegIdx(ChildRec); |
| 4000 | DstMIBuilder.addRenderer<ImmRenderer>(SubIdx->EnumValue); |
| 4001 | return InsertPt; |
| 4002 | } |
| 4003 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4004 | if (ChildRec->isSubClassOf("ComplexPattern")) { |
| 4005 | const auto &ComplexPattern = ComplexPatternEquivs.find(ChildRec); |
| 4006 | if (ComplexPattern == ComplexPatternEquivs.end()) |
| 4007 | return failedImport( |
| 4008 | "SelectionDAG ComplexPattern not mapped to GlobalISel"); |
| 4009 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4010 | const OperandMatcher &OM = Rule.getOperandMatcher(DstChild->getName()); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4011 | DstMIBuilder.addRenderer<RenderComplexPatternOperand>( |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4012 | *ComplexPattern->second, DstChild->getName(), |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 4013 | OM.getAllocatedTemporariesBaseID()); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 4014 | return InsertPt; |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4015 | } |
| 4016 | |
| 4017 | return failedImport( |
| 4018 | "Dst pattern child def is an unsupported tablegen class"); |
| 4019 | } |
| 4020 | |
| 4021 | return failedImport("Dst pattern child is an unsupported kind"); |
| 4022 | } |
| 4023 | |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 4024 | Expected<BuildMIAction &> GlobalISelEmitter::createAndImportInstructionRenderer( |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 4025 | RuleMatcher &M, InstructionMatcher &InsnMatcher, const TreePatternNode *Src, |
| 4026 | const TreePatternNode *Dst) { |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 4027 | auto InsertPtOrError = createInstructionRenderer(M.actions_end(), M, Dst); |
| 4028 | if (auto Error = InsertPtOrError.takeError()) |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 4029 | return std::move(Error); |
| 4030 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 4031 | action_iterator InsertPt = InsertPtOrError.get(); |
| 4032 | BuildMIAction &DstMIBuilder = *static_cast<BuildMIAction *>(InsertPt->get()); |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 4033 | |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 4034 | for (auto PhysInput : InsnMatcher.getPhysRegInputs()) { |
| 4035 | InsertPt = M.insertAction<BuildMIAction>( |
| 4036 | InsertPt, M.allocateOutputInsnID(), |
| 4037 | &Target.getInstruction(RK.getDef("COPY"))); |
| 4038 | BuildMIAction &CopyToPhysRegMIBuilder = |
| 4039 | *static_cast<BuildMIAction *>(InsertPt->get()); |
| 4040 | CopyToPhysRegMIBuilder.addRenderer<AddRegisterRenderer>(PhysInput.first, |
| 4041 | true); |
| 4042 | CopyToPhysRegMIBuilder.addRenderer<CopyPhysRegRenderer>(PhysInput.first); |
| 4043 | } |
| 4044 | |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 4045 | importExplicitDefRenderers(DstMIBuilder); |
| 4046 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 4047 | if (auto Error = importExplicitUseRenderers(InsertPt, M, DstMIBuilder, Dst) |
| 4048 | .takeError()) |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 4049 | return std::move(Error); |
| 4050 | |
| 4051 | return DstMIBuilder; |
| 4052 | } |
| 4053 | |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 4054 | Expected<action_iterator> |
| 4055 | GlobalISelEmitter::createAndImportSubInstructionRenderer( |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4056 | const action_iterator InsertPt, RuleMatcher &M, const TreePatternNode *Dst, |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 4057 | unsigned TempRegID) { |
| 4058 | auto InsertPtOrError = createInstructionRenderer(InsertPt, M, Dst); |
| 4059 | |
| 4060 | // TODO: Assert there's exactly one result. |
| 4061 | |
| 4062 | if (auto Error = InsertPtOrError.takeError()) |
| 4063 | return std::move(Error); |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 4064 | |
| 4065 | BuildMIAction &DstMIBuilder = |
| 4066 | *static_cast<BuildMIAction *>(InsertPtOrError.get()->get()); |
| 4067 | |
| 4068 | // Assign the result to TempReg. |
| 4069 | DstMIBuilder.addRenderer<TempRegRenderer>(TempRegID, true); |
| 4070 | |
Daniel Sanders | 0846452 | 2018-01-29 21:09:12 +0000 | [diff] [blame] | 4071 | InsertPtOrError = |
| 4072 | importExplicitUseRenderers(InsertPtOrError.get(), M, DstMIBuilder, Dst); |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 4073 | if (auto Error = InsertPtOrError.takeError()) |
| 4074 | return std::move(Error); |
| 4075 | |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 4076 | // We need to make sure that when we import an INSERT_SUBREG as a |
| 4077 | // subinstruction that it ends up being constrained to the correct super |
| 4078 | // register and subregister classes. |
Matt Arsenault | 38fb344 | 2019-09-04 16:19:34 +0000 | [diff] [blame] | 4079 | auto OpName = Target.getInstruction(Dst->getOperator()).TheDef->getName(); |
| 4080 | if (OpName == "INSERT_SUBREG") { |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 4081 | auto SubClass = inferRegClassFromPattern(Dst->getChild(1)); |
| 4082 | if (!SubClass) |
| 4083 | return failedImport( |
| 4084 | "Cannot infer register class from INSERT_SUBREG operand #1"); |
Jessica Paquette | 7080ffa | 2019-08-28 20:12:31 +0000 | [diff] [blame] | 4085 | Optional<const CodeGenRegisterClass *> SuperClass = |
| 4086 | inferSuperRegisterClassForNode(Dst->getExtType(0), Dst->getChild(0), |
| 4087 | Dst->getChild(2)); |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 4088 | if (!SuperClass) |
| 4089 | return failedImport( |
| 4090 | "Cannot infer register class for INSERT_SUBREG operand #0"); |
| 4091 | // The destination and the super register source of an INSERT_SUBREG must |
| 4092 | // be the same register class. |
| 4093 | M.insertAction<ConstrainOperandToRegClassAction>( |
| 4094 | InsertPt, DstMIBuilder.getInsnID(), 0, **SuperClass); |
| 4095 | M.insertAction<ConstrainOperandToRegClassAction>( |
| 4096 | InsertPt, DstMIBuilder.getInsnID(), 1, **SuperClass); |
| 4097 | M.insertAction<ConstrainOperandToRegClassAction>( |
| 4098 | InsertPt, DstMIBuilder.getInsnID(), 2, **SubClass); |
| 4099 | return InsertPtOrError.get(); |
| 4100 | } |
| 4101 | |
Matt Arsenault | 9ceb6ed | 2019-09-06 00:05:58 +0000 | [diff] [blame] | 4102 | if (OpName == "EXTRACT_SUBREG") { |
| 4103 | // EXTRACT_SUBREG selects into a subregister COPY but unlike most |
| 4104 | // instructions, the result register class is controlled by the |
| 4105 | // subregisters of the operand. As a result, we must constrain the result |
| 4106 | // class rather than check that it's already the right one. |
| 4107 | auto SuperClass = inferRegClassFromPattern(Dst->getChild(0)); |
| 4108 | if (!SuperClass) |
| 4109 | return failedImport( |
| 4110 | "Cannot infer register class from EXTRACT_SUBREG operand #0"); |
| 4111 | |
| 4112 | auto SubIdx = inferSubRegIndexForNode(Dst->getChild(1)); |
| 4113 | if (!SubIdx) |
| 4114 | return failedImport("EXTRACT_SUBREG child #1 is not a subreg index"); |
| 4115 | |
| 4116 | const auto &SrcRCDstRCPair = |
| 4117 | (*SuperClass)->getMatchingSubClassWithSubRegs(CGRegs, *SubIdx); |
| 4118 | assert(SrcRCDstRCPair->second && "Couldn't find a matching subclass"); |
| 4119 | M.insertAction<ConstrainOperandToRegClassAction>( |
| 4120 | InsertPt, DstMIBuilder.getInsnID(), 0, *SrcRCDstRCPair->second); |
| 4121 | M.insertAction<ConstrainOperandToRegClassAction>( |
| 4122 | InsertPt, DstMIBuilder.getInsnID(), 1, *SrcRCDstRCPair->first); |
| 4123 | |
| 4124 | // We're done with this pattern! It's eligible for GISel emission; return |
| 4125 | // it. |
| 4126 | return InsertPtOrError.get(); |
| 4127 | } |
| 4128 | |
Jessica Paquette | 7080ffa | 2019-08-28 20:12:31 +0000 | [diff] [blame] | 4129 | // Similar to INSERT_SUBREG, we also have to handle SUBREG_TO_REG as a |
| 4130 | // subinstruction. |
Matt Arsenault | 38fb344 | 2019-09-04 16:19:34 +0000 | [diff] [blame] | 4131 | if (OpName == "SUBREG_TO_REG") { |
| 4132 | auto SubClass = inferRegClassFromPattern(Dst->getChild(1)); |
| 4133 | if (!SubClass) |
| 4134 | return failedImport( |
Jessica Paquette | 7080ffa | 2019-08-28 20:12:31 +0000 | [diff] [blame] | 4135 | "Cannot infer register class from SUBREG_TO_REG child #1"); |
Matt Arsenault | 38fb344 | 2019-09-04 16:19:34 +0000 | [diff] [blame] | 4136 | auto SuperClass = inferSuperRegisterClass(Dst->getExtType(0), |
| 4137 | Dst->getChild(2)); |
| 4138 | if (!SuperClass) |
| 4139 | return failedImport( |
Jessica Paquette | 7080ffa | 2019-08-28 20:12:31 +0000 | [diff] [blame] | 4140 | "Cannot infer register class for SUBREG_TO_REG operand #0"); |
Matt Arsenault | 38fb344 | 2019-09-04 16:19:34 +0000 | [diff] [blame] | 4141 | M.insertAction<ConstrainOperandToRegClassAction>( |
Jessica Paquette | 7080ffa | 2019-08-28 20:12:31 +0000 | [diff] [blame] | 4142 | InsertPt, DstMIBuilder.getInsnID(), 0, **SuperClass); |
Matt Arsenault | 38fb344 | 2019-09-04 16:19:34 +0000 | [diff] [blame] | 4143 | M.insertAction<ConstrainOperandToRegClassAction>( |
Jessica Paquette | 7080ffa | 2019-08-28 20:12:31 +0000 | [diff] [blame] | 4144 | InsertPt, DstMIBuilder.getInsnID(), 2, **SubClass); |
Matt Arsenault | 38fb344 | 2019-09-04 16:19:34 +0000 | [diff] [blame] | 4145 | return InsertPtOrError.get(); |
| 4146 | } |
Jessica Paquette | 7080ffa | 2019-08-28 20:12:31 +0000 | [diff] [blame] | 4147 | |
Daniel Sanders | 0846452 | 2018-01-29 21:09:12 +0000 | [diff] [blame] | 4148 | M.insertAction<ConstrainOperandsToDefinitionAction>(InsertPt, |
| 4149 | DstMIBuilder.getInsnID()); |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 4150 | return InsertPtOrError.get(); |
| 4151 | } |
| 4152 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 4153 | Expected<action_iterator> GlobalISelEmitter::createInstructionRenderer( |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4154 | action_iterator InsertPt, RuleMatcher &M, const TreePatternNode *Dst) { |
| 4155 | Record *DstOp = Dst->getOperator(); |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 4156 | if (!DstOp->isSubClassOf("Instruction")) { |
| 4157 | if (DstOp->isSubClassOf("ValueType")) |
| 4158 | return failedImport( |
| 4159 | "Pattern operator isn't an instruction (it's a ValueType)"); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4160 | return failedImport("Pattern operator isn't an instruction"); |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 4161 | } |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 4162 | CodeGenInstruction *DstI = &Target.getInstruction(DstOp); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4163 | |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 4164 | // COPY_TO_REGCLASS is just a copy with a ConstrainOperandToRegClassAction |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 4165 | // attached. Similarly for EXTRACT_SUBREG except that's a subregister copy. |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 4166 | if (DstI->TheDef->getName() == "COPY_TO_REGCLASS") |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 4167 | DstI = &Target.getInstruction(RK.getDef("COPY")); |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 4168 | else if (DstI->TheDef->getName() == "EXTRACT_SUBREG") |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 4169 | DstI = &Target.getInstruction(RK.getDef("COPY")); |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 4170 | else if (DstI->TheDef->getName() == "REG_SEQUENCE") |
| 4171 | return failedImport("Unable to emit REG_SEQUENCE"); |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 4172 | |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 4173 | return M.insertAction<BuildMIAction>(InsertPt, M.allocateOutputInsnID(), |
| 4174 | DstI); |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 4175 | } |
| 4176 | |
| 4177 | void GlobalISelEmitter::importExplicitDefRenderers( |
| 4178 | BuildMIAction &DstMIBuilder) { |
| 4179 | const CodeGenInstruction *DstI = DstMIBuilder.getCGI(); |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 4180 | for (unsigned I = 0; I < DstI->Operands.NumDefs; ++I) { |
| 4181 | const CGIOperandList::OperandInfo &DstIOperand = DstI->Operands[I]; |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 4182 | DstMIBuilder.addRenderer<CopyRenderer>(DstIOperand.Name); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4183 | } |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 4184 | } |
| 4185 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 4186 | Expected<action_iterator> GlobalISelEmitter::importExplicitUseRenderers( |
| 4187 | action_iterator InsertPt, RuleMatcher &M, BuildMIAction &DstMIBuilder, |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4188 | const llvm::TreePatternNode *Dst) { |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 4189 | const CodeGenInstruction *DstI = DstMIBuilder.getCGI(); |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4190 | CodeGenInstruction *OrigDstI = &Target.getInstruction(Dst->getOperator()); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4191 | |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 4192 | // EXTRACT_SUBREG needs to use a subregister COPY. |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 4193 | if (OrigDstI->TheDef->getName() == "EXTRACT_SUBREG") { |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4194 | if (!Dst->getChild(0)->isLeaf()) |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 4195 | return failedImport("EXTRACT_SUBREG child #1 is not a leaf"); |
| 4196 | |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 4197 | if (DefInit *SubRegInit = |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4198 | dyn_cast<DefInit>(Dst->getChild(1)->getLeafValue())) { |
| 4199 | Record *RCDef = getInitValueAsRegClass(Dst->getChild(0)->getLeafValue()); |
Daniel Sanders | 9cbe7c7 | 2017-11-01 19:57:57 +0000 | [diff] [blame] | 4200 | if (!RCDef) |
| 4201 | return failedImport("EXTRACT_SUBREG child #0 could not " |
| 4202 | "be coerced to a register class"); |
| 4203 | |
| 4204 | CodeGenRegisterClass *RC = CGRegs.getRegClass(RCDef); |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 4205 | CodeGenSubRegIndex *SubIdx = CGRegs.getSubRegIdx(SubRegInit->getDef()); |
| 4206 | |
| 4207 | const auto &SrcRCDstRCPair = |
| 4208 | RC->getMatchingSubClassWithSubRegs(CGRegs, SubIdx); |
| 4209 | if (SrcRCDstRCPair.hasValue()) { |
| 4210 | assert(SrcRCDstRCPair->second && "Couldn't find a matching subclass"); |
| 4211 | if (SrcRCDstRCPair->first != RC) |
| 4212 | return failedImport("EXTRACT_SUBREG requires an additional COPY"); |
| 4213 | } |
| 4214 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4215 | DstMIBuilder.addRenderer<CopySubRegRenderer>(Dst->getChild(0)->getName(), |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 4216 | SubIdx); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 4217 | return InsertPt; |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 4218 | } |
| 4219 | |
| 4220 | return failedImport("EXTRACT_SUBREG child #1 is not a subreg index"); |
| 4221 | } |
| 4222 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4223 | // Render the explicit uses. |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 4224 | unsigned DstINumUses = OrigDstI->Operands.size() - OrigDstI->Operands.NumDefs; |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4225 | unsigned ExpectedDstINumUses = Dst->getNumChildren(); |
Daniel Sanders | df258e3 | 2017-10-31 19:09:29 +0000 | [diff] [blame] | 4226 | if (OrigDstI->TheDef->getName() == "COPY_TO_REGCLASS") { |
| 4227 | DstINumUses--; // Ignore the class constraint. |
| 4228 | ExpectedDstINumUses--; |
| 4229 | } |
| 4230 | |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 4231 | unsigned Child = 0; |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 4232 | unsigned NumDefaultOps = 0; |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 4233 | for (unsigned I = 0; I != DstINumUses; ++I) { |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 4234 | const CGIOperandList::OperandInfo &DstIOperand = |
| 4235 | DstI->Operands[DstI->Operands.NumDefs + I]; |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 4236 | |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 4237 | // If the operand has default values, introduce them now. |
| 4238 | // FIXME: Until we have a decent test case that dictates we should do |
| 4239 | // otherwise, we're going to assume that operands with default values cannot |
| 4240 | // be specified in the patterns. Therefore, adding them will not cause us to |
| 4241 | // end up with too many rendered operands. |
| 4242 | if (DstIOperand.Rec->isSubClassOf("OperandWithDefaultOps")) { |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 4243 | DagInit *DefaultOps = DstIOperand.Rec->getValueAsDag("DefaultOps"); |
Sjoerd Meijer | de23484 | 2019-05-30 07:30:37 +0000 | [diff] [blame] | 4244 | if (auto Error = importDefaultOperandRenderers( |
| 4245 | InsertPt, M, DstMIBuilder, DefaultOps)) |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 4246 | return std::move(Error); |
| 4247 | ++NumDefaultOps; |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 4248 | continue; |
| 4249 | } |
| 4250 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 4251 | auto InsertPtOrError = importExplicitUseRenderer(InsertPt, M, DstMIBuilder, |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4252 | Dst->getChild(Child)); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 4253 | if (auto Error = InsertPtOrError.takeError()) |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4254 | return std::move(Error); |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 4255 | InsertPt = InsertPtOrError.get(); |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 4256 | ++Child; |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4257 | } |
| 4258 | |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 4259 | if (NumDefaultOps + ExpectedDstINumUses != DstINumUses) |
Diana Picus | eb2057c | 2017-05-17 09:25:08 +0000 | [diff] [blame] | 4260 | return failedImport("Expected " + llvm::to_string(DstINumUses) + |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 4261 | " used operands but found " + |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 4262 | llvm::to_string(ExpectedDstINumUses) + |
Diana Picus | eb2057c | 2017-05-17 09:25:08 +0000 | [diff] [blame] | 4263 | " explicit ones and " + llvm::to_string(NumDefaultOps) + |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 4264 | " default ones"); |
| 4265 | |
Daniel Sanders | 7438b26 | 2017-10-31 23:03:18 +0000 | [diff] [blame] | 4266 | return InsertPt; |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4267 | } |
| 4268 | |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 4269 | Error GlobalISelEmitter::importDefaultOperandRenderers( |
Sjoerd Meijer | de23484 | 2019-05-30 07:30:37 +0000 | [diff] [blame] | 4270 | action_iterator InsertPt, RuleMatcher &M, BuildMIAction &DstMIBuilder, |
| 4271 | DagInit *DefaultOps) const { |
Craig Topper | 481ff70 | 2017-05-29 21:49:34 +0000 | [diff] [blame] | 4272 | for (const auto *DefaultOp : DefaultOps->getArgs()) { |
Sjoerd Meijer | de23484 | 2019-05-30 07:30:37 +0000 | [diff] [blame] | 4273 | Optional<LLTCodeGen> OpTyOrNone = None; |
| 4274 | |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 4275 | // Look through ValueType operators. |
| 4276 | if (const DagInit *DefaultDagOp = dyn_cast<DagInit>(DefaultOp)) { |
| 4277 | if (const DefInit *DefaultDagOperator = |
| 4278 | dyn_cast<DefInit>(DefaultDagOp->getOperator())) { |
Sjoerd Meijer | 3cac8d2 | 2019-05-31 08:39:34 +0000 | [diff] [blame] | 4279 | if (DefaultDagOperator->getDef()->isSubClassOf("ValueType")) { |
Sjoerd Meijer | de23484 | 2019-05-30 07:30:37 +0000 | [diff] [blame] | 4280 | OpTyOrNone = MVTToLLT(getValueType( |
| 4281 | DefaultDagOperator->getDef())); |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 4282 | DefaultOp = DefaultDagOp->getArg(0); |
Sjoerd Meijer | 3cac8d2 | 2019-05-31 08:39:34 +0000 | [diff] [blame] | 4283 | } |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 4284 | } |
| 4285 | } |
| 4286 | |
| 4287 | if (const DefInit *DefaultDefOp = dyn_cast<DefInit>(DefaultOp)) { |
Sjoerd Meijer | de23484 | 2019-05-30 07:30:37 +0000 | [diff] [blame] | 4288 | auto Def = DefaultDefOp->getDef(); |
| 4289 | if (Def->getName() == "undef_tied_input") { |
| 4290 | unsigned TempRegID = M.allocateTempRegID(); |
| 4291 | M.insertAction<MakeTempRegisterAction>( |
| 4292 | InsertPt, OpTyOrNone.getValue(), TempRegID); |
| 4293 | InsertPt = M.insertAction<BuildMIAction>( |
| 4294 | InsertPt, M.allocateOutputInsnID(), |
| 4295 | &Target.getInstruction(RK.getDef("IMPLICIT_DEF"))); |
| 4296 | BuildMIAction &IDMIBuilder = *static_cast<BuildMIAction *>( |
| 4297 | InsertPt->get()); |
| 4298 | IDMIBuilder.addRenderer<TempRegRenderer>(TempRegID); |
| 4299 | DstMIBuilder.addRenderer<TempRegRenderer>(TempRegID); |
| 4300 | } else { |
| 4301 | DstMIBuilder.addRenderer<AddRegisterRenderer>(Def); |
| 4302 | } |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 4303 | continue; |
| 4304 | } |
| 4305 | |
| 4306 | if (const IntInit *DefaultIntOp = dyn_cast<IntInit>(DefaultOp)) { |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 4307 | DstMIBuilder.addRenderer<ImmRenderer>(DefaultIntOp->getValue()); |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 4308 | continue; |
| 4309 | } |
| 4310 | |
| 4311 | return failedImport("Could not add default op"); |
| 4312 | } |
| 4313 | |
| 4314 | return Error::success(); |
| 4315 | } |
| 4316 | |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 4317 | Error GlobalISelEmitter::importImplicitDefRenderers( |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4318 | BuildMIAction &DstMIBuilder, |
| 4319 | const std::vector<Record *> &ImplicitDefs) const { |
| 4320 | if (!ImplicitDefs.empty()) |
| 4321 | return failedImport("Pattern defines a physical register"); |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 4322 | return Error::success(); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4323 | } |
| 4324 | |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 4325 | Optional<const CodeGenRegisterClass *> |
| 4326 | GlobalISelEmitter::getRegClassFromLeaf(TreePatternNode *Leaf) { |
| 4327 | assert(Leaf && "Expected node?"); |
| 4328 | assert(Leaf->isLeaf() && "Expected leaf?"); |
| 4329 | Record *RCRec = getInitValueAsRegClass(Leaf->getLeafValue()); |
| 4330 | if (!RCRec) |
| 4331 | return None; |
| 4332 | CodeGenRegisterClass *RC = CGRegs.getRegClass(RCRec); |
| 4333 | if (!RC) |
| 4334 | return None; |
| 4335 | return RC; |
| 4336 | } |
| 4337 | |
| 4338 | Optional<const CodeGenRegisterClass *> |
| 4339 | GlobalISelEmitter::inferRegClassFromPattern(TreePatternNode *N) { |
| 4340 | if (!N) |
| 4341 | return None; |
| 4342 | |
| 4343 | if (N->isLeaf()) |
| 4344 | return getRegClassFromLeaf(N); |
| 4345 | |
| 4346 | // We don't have a leaf node, so we have to try and infer something. Check |
| 4347 | // that we have an instruction that we an infer something from. |
| 4348 | |
| 4349 | // Only handle things that produce a single type. |
| 4350 | if (N->getNumTypes() != 1) |
| 4351 | return None; |
| 4352 | Record *OpRec = N->getOperator(); |
| 4353 | |
| 4354 | // We only want instructions. |
| 4355 | if (!OpRec->isSubClassOf("Instruction")) |
| 4356 | return None; |
| 4357 | |
| 4358 | // Don't want to try and infer things when there could potentially be more |
| 4359 | // than one candidate register class. |
| 4360 | auto &Inst = Target.getInstruction(OpRec); |
| 4361 | if (Inst.Operands.NumDefs > 1) |
| 4362 | return None; |
| 4363 | |
| 4364 | // Handle any special-case instructions which we can safely infer register |
| 4365 | // classes from. |
| 4366 | StringRef InstName = Inst.TheDef->getName(); |
Matt Arsenault | 38fb344 | 2019-09-04 16:19:34 +0000 | [diff] [blame] | 4367 | bool IsRegSequence = InstName == "REG_SEQUENCE"; |
| 4368 | if (IsRegSequence || InstName == "COPY_TO_REGCLASS") { |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 4369 | // If we have a COPY_TO_REGCLASS, then we need to handle it specially. It |
| 4370 | // has the desired register class as the first child. |
Matt Arsenault | 38fb344 | 2019-09-04 16:19:34 +0000 | [diff] [blame] | 4371 | TreePatternNode *RCChild = N->getChild(IsRegSequence ? 0 : 1); |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 4372 | if (!RCChild->isLeaf()) |
| 4373 | return None; |
| 4374 | return getRegClassFromLeaf(RCChild); |
| 4375 | } |
| 4376 | |
| 4377 | // Handle destination record types that we can safely infer a register class |
| 4378 | // from. |
| 4379 | const auto &DstIOperand = Inst.Operands[0]; |
| 4380 | Record *DstIOpRec = DstIOperand.Rec; |
| 4381 | if (DstIOpRec->isSubClassOf("RegisterOperand")) { |
| 4382 | DstIOpRec = DstIOpRec->getValueAsDef("RegClass"); |
| 4383 | const CodeGenRegisterClass &RC = Target.getRegisterClass(DstIOpRec); |
| 4384 | return &RC; |
| 4385 | } |
| 4386 | |
| 4387 | if (DstIOpRec->isSubClassOf("RegisterClass")) { |
| 4388 | const CodeGenRegisterClass &RC = Target.getRegisterClass(DstIOpRec); |
| 4389 | return &RC; |
| 4390 | } |
| 4391 | |
| 4392 | return None; |
| 4393 | } |
| 4394 | |
| 4395 | Optional<const CodeGenRegisterClass *> |
| 4396 | GlobalISelEmitter::inferSuperRegisterClass(const TypeSetByHwMode &Ty, |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 4397 | TreePatternNode *SubRegIdxNode) { |
Jessica Paquette | 7080ffa | 2019-08-28 20:12:31 +0000 | [diff] [blame] | 4398 | assert(SubRegIdxNode && "Expected subregister index node!"); |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 4399 | // We need a ValueTypeByHwMode for getSuperRegForSubReg. |
| 4400 | if (!Ty.isValueTypeByHwMode(false)) |
| 4401 | return None; |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 4402 | if (!SubRegIdxNode->isLeaf()) |
| 4403 | return None; |
| 4404 | DefInit *SubRegInit = dyn_cast<DefInit>(SubRegIdxNode->getLeafValue()); |
| 4405 | if (!SubRegInit) |
| 4406 | return None; |
| 4407 | CodeGenSubRegIndex *SubIdx = CGRegs.getSubRegIdx(SubRegInit->getDef()); |
| 4408 | |
| 4409 | // Use the information we found above to find a minimal register class which |
| 4410 | // supports the subregister and type we want. |
| 4411 | auto RC = |
| 4412 | Target.getSuperRegForSubReg(Ty.getValueTypeByHwMode(), CGRegs, SubIdx); |
| 4413 | if (!RC) |
| 4414 | return None; |
| 4415 | return *RC; |
| 4416 | } |
| 4417 | |
Jessica Paquette | 7080ffa | 2019-08-28 20:12:31 +0000 | [diff] [blame] | 4418 | Optional<const CodeGenRegisterClass *> |
| 4419 | GlobalISelEmitter::inferSuperRegisterClassForNode( |
| 4420 | const TypeSetByHwMode &Ty, TreePatternNode *SuperRegNode, |
| 4421 | TreePatternNode *SubRegIdxNode) { |
| 4422 | assert(SuperRegNode && "Expected super register node!"); |
| 4423 | // Check if we already have a defined register class for the super register |
| 4424 | // node. If we do, then we should preserve that rather than inferring anything |
| 4425 | // from the subregister index node. We can assume that whoever wrote the |
| 4426 | // pattern in the first place made sure that the super register and |
| 4427 | // subregister are compatible. |
| 4428 | if (Optional<const CodeGenRegisterClass *> SuperRegisterClass = |
| 4429 | inferRegClassFromPattern(SuperRegNode)) |
| 4430 | return *SuperRegisterClass; |
| 4431 | return inferSuperRegisterClass(Ty, SubRegIdxNode); |
| 4432 | } |
| 4433 | |
Matt Arsenault | 9ceb6ed | 2019-09-06 00:05:58 +0000 | [diff] [blame] | 4434 | Optional<CodeGenSubRegIndex *> |
| 4435 | GlobalISelEmitter::inferSubRegIndexForNode(TreePatternNode *SubRegIdxNode) { |
| 4436 | if (!SubRegIdxNode->isLeaf()) |
| 4437 | return None; |
| 4438 | |
| 4439 | DefInit *SubRegInit = dyn_cast<DefInit>(SubRegIdxNode->getLeafValue()); |
| 4440 | if (!SubRegInit) |
| 4441 | return None; |
| 4442 | return CGRegs.getSubRegIdx(SubRegInit->getDef()); |
| 4443 | } |
| 4444 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4445 | Expected<RuleMatcher> GlobalISelEmitter::runOnPattern(const PatternToMatch &P) { |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4446 | // Keep track of the matchers and actions to emit. |
Aditya Nandakumar | b63e763 | 2018-02-16 22:37:15 +0000 | [diff] [blame] | 4447 | int Score = P.getPatternComplexity(CGP); |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 4448 | RuleMatcher M(P.getSrcRecord()->getLoc()); |
Aditya Nandakumar | b63e763 | 2018-02-16 22:37:15 +0000 | [diff] [blame] | 4449 | RuleMatcherScores[M.getRuleID()] = Score; |
Daniel Sanders | 6ea17ed | 2017-10-31 18:07:03 +0000 | [diff] [blame] | 4450 | M.addAction<DebugCommentAction>(llvm::to_string(*P.getSrcPattern()) + |
| 4451 | " => " + |
| 4452 | llvm::to_string(*P.getDstPattern())); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4453 | |
Krzysztof Parzyszek | 779d98e | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 4454 | if (auto Error = importRulePredicates(M, P.getPredicates())) |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4455 | return std::move(Error); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4456 | |
| 4457 | // Next, analyze the pattern operators. |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4458 | TreePatternNode *Src = P.getSrcPattern(); |
| 4459 | TreePatternNode *Dst = P.getDstPattern(); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4460 | |
| 4461 | // 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] | 4462 | if (auto Err = isTrivialOperatorNode(Dst)) |
| 4463 | return failedImport("Dst pattern root isn't a trivial operator (" + |
| 4464 | toString(std::move(Err)) + ")"); |
| 4465 | if (auto Err = isTrivialOperatorNode(Src)) |
| 4466 | return failedImport("Src pattern root isn't a trivial operator (" + |
| 4467 | toString(std::move(Err)) + ")"); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4468 | |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 4469 | // The different predicates and matchers created during |
| 4470 | // addInstructionMatcher use the RuleMatcher M to set up their |
| 4471 | // instruction ID (InsnVarID) that are going to be used when |
| 4472 | // M is going to be emitted. |
| 4473 | // However, the code doing the emission still relies on the IDs |
| 4474 | // returned during that process by the RuleMatcher when issuing |
| 4475 | // the recordInsn opcodes. |
| 4476 | // Because of that: |
| 4477 | // 1. The order in which we created the predicates |
| 4478 | // and such must be the same as the order in which we emit them, |
| 4479 | // and |
| 4480 | // 2. We need to reset the generation of the IDs in M somewhere between |
| 4481 | // addInstructionMatcher and emit |
| 4482 | // |
| 4483 | // FIXME: Long term, we don't want to have to rely on this implicit |
| 4484 | // naming being the same. One possible solution would be to have |
| 4485 | // explicit operator for operation capture and reference those. |
| 4486 | // The plus side is that it would expose opportunities to share |
| 4487 | // the capture accross rules. The downside is that it would |
| 4488 | // introduce a dependency between predicates (captures must happen |
| 4489 | // before their first use.) |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4490 | InstructionMatcher &InsnMatcherTemp = M.addInstructionMatcher(Src->getName()); |
Daniel Sanders | edd0784 | 2017-08-17 09:26:14 +0000 | [diff] [blame] | 4491 | unsigned TempOpIdx = 0; |
| 4492 | auto InsnMatcherOrError = |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 4493 | createAndImportSelDAGMatcher(M, InsnMatcherTemp, Src, TempOpIdx); |
Daniel Sanders | edd0784 | 2017-08-17 09:26:14 +0000 | [diff] [blame] | 4494 | if (auto Error = InsnMatcherOrError.takeError()) |
| 4495 | return std::move(Error); |
| 4496 | InstructionMatcher &InsnMatcher = InsnMatcherOrError.get(); |
| 4497 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4498 | if (Dst->isLeaf()) { |
| 4499 | Record *RCDef = getInitValueAsRegClass(Dst->getLeafValue()); |
Daniel Sanders | edd0784 | 2017-08-17 09:26:14 +0000 | [diff] [blame] | 4500 | |
| 4501 | const CodeGenRegisterClass &RC = Target.getRegisterClass(RCDef); |
| 4502 | if (RCDef) { |
| 4503 | // We need to replace the def and all its uses with the specified |
| 4504 | // operand. However, we must also insert COPY's wherever needed. |
| 4505 | // For now, emit a copy and let the register allocator clean up. |
| 4506 | auto &DstI = Target.getInstruction(RK.getDef("COPY")); |
| 4507 | const auto &DstIOperand = DstI.Operands[0]; |
| 4508 | |
| 4509 | OperandMatcher &OM0 = InsnMatcher.getOperand(0); |
| 4510 | OM0.setSymbolicName(DstIOperand.Name); |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 4511 | M.defineOperand(OM0.getSymbolicName(), OM0); |
Daniel Sanders | edd0784 | 2017-08-17 09:26:14 +0000 | [diff] [blame] | 4512 | OM0.addPredicate<RegisterBankOperandMatcher>(RC); |
| 4513 | |
Daniel Sanders | 198447a | 2017-11-01 00:29:47 +0000 | [diff] [blame] | 4514 | auto &DstMIBuilder = |
| 4515 | M.addAction<BuildMIAction>(M.allocateOutputInsnID(), &DstI); |
| 4516 | DstMIBuilder.addRenderer<CopyRenderer>(DstIOperand.Name); |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4517 | DstMIBuilder.addRenderer<CopyRenderer>(Dst->getName()); |
Daniel Sanders | edd0784 | 2017-08-17 09:26:14 +0000 | [diff] [blame] | 4518 | M.addAction<ConstrainOperandToRegClassAction>(0, 0, RC); |
| 4519 | |
| 4520 | // We're done with this pattern! It's eligible for GISel emission; return |
| 4521 | // it. |
| 4522 | ++NumPatternImported; |
| 4523 | return std::move(M); |
| 4524 | } |
| 4525 | |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 4526 | return failedImport("Dst pattern root isn't a known leaf"); |
Daniel Sanders | edd0784 | 2017-08-17 09:26:14 +0000 | [diff] [blame] | 4527 | } |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 4528 | |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 4529 | // Start with the defined operands (i.e., the results of the root operator). |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4530 | Record *DstOp = Dst->getOperator(); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4531 | if (!DstOp->isSubClassOf("Instruction")) |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 4532 | return failedImport("Pattern operator isn't an instruction"); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4533 | |
| 4534 | auto &DstI = Target.getInstruction(DstOp); |
Matt Arsenault | 38fb344 | 2019-09-04 16:19:34 +0000 | [diff] [blame] | 4535 | StringRef DstIName = DstI.TheDef->getName(); |
| 4536 | |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4537 | if (DstI.Operands.NumDefs != Src->getExtTypes().size()) |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 4538 | return failedImport("Src pattern results and dst MI defs are different (" + |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4539 | to_string(Src->getExtTypes().size()) + " def(s) vs " + |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 4540 | to_string(DstI.Operands.NumDefs) + " def(s))"); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4541 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4542 | // The root of the match also has constraints on the register bank so that it |
| 4543 | // matches the result instruction. |
| 4544 | unsigned OpIdx = 0; |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4545 | for (const TypeSetByHwMode &VTy : Src->getExtTypes()) { |
Krzysztof Parzyszek | 779d98e | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 4546 | (void)VTy; |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4547 | |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 4548 | const auto &DstIOperand = DstI.Operands[OpIdx]; |
| 4549 | Record *DstIOpRec = DstIOperand.Rec; |
Matt Arsenault | 38fb344 | 2019-09-04 16:19:34 +0000 | [diff] [blame] | 4550 | if (DstIName == "COPY_TO_REGCLASS") { |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4551 | DstIOpRec = getInitValueAsRegClass(Dst->getChild(1)->getLeafValue()); |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 4552 | |
| 4553 | if (DstIOpRec == nullptr) |
| 4554 | return failedImport( |
| 4555 | "COPY_TO_REGCLASS operand #1 isn't a register class"); |
Matt Arsenault | 38fb344 | 2019-09-04 16:19:34 +0000 | [diff] [blame] | 4556 | } else if (DstIName == "REG_SEQUENCE") { |
| 4557 | DstIOpRec = getInitValueAsRegClass(Dst->getChild(0)->getLeafValue()); |
| 4558 | if (DstIOpRec == nullptr) |
| 4559 | return failedImport("REG_SEQUENCE operand #0 isn't a register class"); |
| 4560 | } else if (DstIName == "EXTRACT_SUBREG") { |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4561 | if (!Dst->getChild(0)->isLeaf()) |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 4562 | return failedImport("EXTRACT_SUBREG operand #0 isn't a leaf"); |
| 4563 | |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 4564 | // We can assume that a subregister is in the same bank as it's super |
| 4565 | // register. |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4566 | DstIOpRec = getInitValueAsRegClass(Dst->getChild(0)->getLeafValue()); |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 4567 | |
| 4568 | if (DstIOpRec == nullptr) |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 4569 | return failedImport("EXTRACT_SUBREG operand #0 isn't a register class"); |
Matt Arsenault | 38fb344 | 2019-09-04 16:19:34 +0000 | [diff] [blame] | 4570 | } else if (DstIName == "INSERT_SUBREG") { |
Jessica Paquette | 7080ffa | 2019-08-28 20:12:31 +0000 | [diff] [blame] | 4571 | auto MaybeSuperClass = inferSuperRegisterClassForNode( |
| 4572 | VTy, Dst->getChild(0), Dst->getChild(2)); |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 4573 | if (!MaybeSuperClass) |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 4574 | return failedImport( |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 4575 | "Cannot infer register class for INSERT_SUBREG operand #0"); |
| 4576 | // Move to the next pattern here, because the register class we found |
| 4577 | // doesn't necessarily have a record associated with it. So, we can't |
| 4578 | // set DstIOpRec using this. |
| 4579 | OperandMatcher &OM = InsnMatcher.getOperand(OpIdx); |
| 4580 | OM.setSymbolicName(DstIOperand.Name); |
| 4581 | M.defineOperand(OM.getSymbolicName(), OM); |
| 4582 | OM.addPredicate<RegisterBankOperandMatcher>(**MaybeSuperClass); |
| 4583 | ++OpIdx; |
| 4584 | continue; |
Matt Arsenault | 38fb344 | 2019-09-04 16:19:34 +0000 | [diff] [blame] | 4585 | } else if (DstIName == "SUBREG_TO_REG") { |
Jessica Paquette | 7080ffa | 2019-08-28 20:12:31 +0000 | [diff] [blame] | 4586 | auto MaybeRegClass = inferSuperRegisterClass(VTy, Dst->getChild(2)); |
| 4587 | if (!MaybeRegClass) |
| 4588 | return failedImport( |
| 4589 | "Cannot infer register class for SUBREG_TO_REG operand #0"); |
| 4590 | OperandMatcher &OM = InsnMatcher.getOperand(OpIdx); |
| 4591 | OM.setSymbolicName(DstIOperand.Name); |
| 4592 | M.defineOperand(OM.getSymbolicName(), OM); |
| 4593 | OM.addPredicate<RegisterBankOperandMatcher>(**MaybeRegClass); |
| 4594 | ++OpIdx; |
| 4595 | continue; |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 4596 | } else if (DstIOpRec->isSubClassOf("RegisterOperand")) |
Daniel Sanders | 658541f | 2017-04-22 15:53:21 +0000 | [diff] [blame] | 4597 | DstIOpRec = DstIOpRec->getValueAsDef("RegClass"); |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 4598 | else if (!DstIOpRec->isSubClassOf("RegisterClass")) |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4599 | return failedImport("Dst MI def isn't a register class" + |
| 4600 | to_string(*Dst)); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4601 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4602 | OperandMatcher &OM = InsnMatcher.getOperand(OpIdx); |
| 4603 | OM.setSymbolicName(DstIOperand.Name); |
Daniel Sanders | bfa9e2c | 2017-10-14 00:31:58 +0000 | [diff] [blame] | 4604 | M.defineOperand(OM.getSymbolicName(), OM); |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 4605 | OM.addPredicate<RegisterBankOperandMatcher>( |
| 4606 | Target.getRegisterClass(DstIOpRec)); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4607 | ++OpIdx; |
| 4608 | } |
| 4609 | |
Matt Arsenault | 3e45c70 | 2019-09-06 20:32:37 +0000 | [diff] [blame] | 4610 | auto DstMIBuilderOrError = |
| 4611 | createAndImportInstructionRenderer(M, InsnMatcher, Src, Dst); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4612 | if (auto Error = DstMIBuilderOrError.takeError()) |
| 4613 | return std::move(Error); |
| 4614 | BuildMIAction &DstMIBuilder = DstMIBuilderOrError.get(); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4615 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4616 | // Render the implicit defs. |
| 4617 | // These are only added to the root of the result. |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 4618 | if (auto Error = importImplicitDefRenderers(DstMIBuilder, P.getDstRegs())) |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 4619 | return std::move(Error); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4620 | |
Daniel Sanders | a7b7526 | 2017-10-31 18:50:24 +0000 | [diff] [blame] | 4621 | DstMIBuilder.chooseInsnToMutate(M); |
| 4622 | |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 4623 | // Constrain the registers to classes. This is normally derived from the |
| 4624 | // emitted instruction but a few instructions require special handling. |
Matt Arsenault | 38fb344 | 2019-09-04 16:19:34 +0000 | [diff] [blame] | 4625 | if (DstIName == "COPY_TO_REGCLASS") { |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 4626 | // COPY_TO_REGCLASS does not provide operand constraints itself but the |
| 4627 | // result is constrained to the class given by the second child. |
| 4628 | Record *DstIOpRec = |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4629 | getInitValueAsRegClass(Dst->getChild(1)->getLeafValue()); |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 4630 | |
| 4631 | if (DstIOpRec == nullptr) |
| 4632 | return failedImport("COPY_TO_REGCLASS operand #1 isn't a register class"); |
| 4633 | |
| 4634 | M.addAction<ConstrainOperandToRegClassAction>( |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 4635 | 0, 0, Target.getRegisterClass(DstIOpRec)); |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 4636 | |
| 4637 | // We're done with this pattern! It's eligible for GISel emission; return |
| 4638 | // it. |
| 4639 | ++NumPatternImported; |
| 4640 | return std::move(M); |
| 4641 | } |
| 4642 | |
Matt Arsenault | 38fb344 | 2019-09-04 16:19:34 +0000 | [diff] [blame] | 4643 | if (DstIName == "EXTRACT_SUBREG") { |
Matt Arsenault | 9ceb6ed | 2019-09-06 00:05:58 +0000 | [diff] [blame] | 4644 | auto SuperClass = inferRegClassFromPattern(Dst->getChild(0)); |
| 4645 | if (!SuperClass) |
| 4646 | return failedImport( |
| 4647 | "Cannot infer register class from EXTRACT_SUBREG operand #0"); |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 4648 | |
Matt Arsenault | 9ceb6ed | 2019-09-06 00:05:58 +0000 | [diff] [blame] | 4649 | auto SubIdx = inferSubRegIndexForNode(Dst->getChild(1)); |
| 4650 | if (!SubIdx) |
Daniel Sanders | 320390b | 2017-06-28 15:16:03 +0000 | [diff] [blame] | 4651 | return failedImport("EXTRACT_SUBREG child #1 is not a subreg index"); |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 4652 | |
Daniel Sanders | 320390b | 2017-06-28 15:16:03 +0000 | [diff] [blame] | 4653 | // It would be nice to leave this constraint implicit but we're required |
| 4654 | // to pick a register class so constrain the result to a register class |
| 4655 | // that can hold the correct MVT. |
| 4656 | // |
| 4657 | // FIXME: This may introduce an extra copy if the chosen class doesn't |
| 4658 | // actually contain the subregisters. |
Florian Hahn | 6b1db82 | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 4659 | assert(Src->getExtTypes().size() == 1 && |
Daniel Sanders | 320390b | 2017-06-28 15:16:03 +0000 | [diff] [blame] | 4660 | "Expected Src of EXTRACT_SUBREG to have one result type"); |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 4661 | |
Daniel Sanders | 320390b | 2017-06-28 15:16:03 +0000 | [diff] [blame] | 4662 | const auto &SrcRCDstRCPair = |
Matt Arsenault | 9ceb6ed | 2019-09-06 00:05:58 +0000 | [diff] [blame] | 4663 | (*SuperClass)->getMatchingSubClassWithSubRegs(CGRegs, *SubIdx); |
Daniel Sanders | 320390b | 2017-06-28 15:16:03 +0000 | [diff] [blame] | 4664 | assert(SrcRCDstRCPair->second && "Couldn't find a matching subclass"); |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 4665 | M.addAction<ConstrainOperandToRegClassAction>(0, 0, *SrcRCDstRCPair->second); |
| 4666 | M.addAction<ConstrainOperandToRegClassAction>(0, 1, *SrcRCDstRCPair->first); |
| 4667 | |
| 4668 | // We're done with this pattern! It's eligible for GISel emission; return |
| 4669 | // it. |
| 4670 | ++NumPatternImported; |
| 4671 | return std::move(M); |
| 4672 | } |
| 4673 | |
Matt Arsenault | 38fb344 | 2019-09-04 16:19:34 +0000 | [diff] [blame] | 4674 | if (DstIName == "INSERT_SUBREG") { |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 4675 | assert(Src->getExtTypes().size() == 1 && |
| 4676 | "Expected Src of INSERT_SUBREG to have one result type"); |
| 4677 | // We need to constrain the destination, a super regsister source, and a |
| 4678 | // subregister source. |
| 4679 | auto SubClass = inferRegClassFromPattern(Dst->getChild(1)); |
| 4680 | if (!SubClass) |
| 4681 | return failedImport( |
| 4682 | "Cannot infer register class from INSERT_SUBREG operand #1"); |
Jessica Paquette | 7080ffa | 2019-08-28 20:12:31 +0000 | [diff] [blame] | 4683 | auto SuperClass = inferSuperRegisterClassForNode( |
Jessica Paquette | a2ea8a1 | 2019-08-27 17:47:06 +0000 | [diff] [blame] | 4684 | Src->getExtType(0), Dst->getChild(0), Dst->getChild(2)); |
| 4685 | if (!SuperClass) |
| 4686 | return failedImport( |
| 4687 | "Cannot infer register class for INSERT_SUBREG operand #0"); |
| 4688 | M.addAction<ConstrainOperandToRegClassAction>(0, 0, **SuperClass); |
| 4689 | M.addAction<ConstrainOperandToRegClassAction>(0, 1, **SuperClass); |
| 4690 | M.addAction<ConstrainOperandToRegClassAction>(0, 2, **SubClass); |
| 4691 | ++NumPatternImported; |
| 4692 | return std::move(M); |
| 4693 | } |
| 4694 | |
Matt Arsenault | 38fb344 | 2019-09-04 16:19:34 +0000 | [diff] [blame] | 4695 | if (DstIName == "SUBREG_TO_REG") { |
Jessica Paquette | 7080ffa | 2019-08-28 20:12:31 +0000 | [diff] [blame] | 4696 | // We need to constrain the destination and subregister source. |
| 4697 | assert(Src->getExtTypes().size() == 1 && |
| 4698 | "Expected Src of SUBREG_TO_REG to have one result type"); |
| 4699 | |
| 4700 | // Attempt to infer the subregister source from the first child. If it has |
| 4701 | // an explicitly given register class, we'll use that. Otherwise, we will |
| 4702 | // fail. |
| 4703 | auto SubClass = inferRegClassFromPattern(Dst->getChild(1)); |
| 4704 | if (!SubClass) |
| 4705 | return failedImport( |
| 4706 | "Cannot infer register class from SUBREG_TO_REG child #1"); |
| 4707 | // We don't have a child to look at that might have a super register node. |
| 4708 | auto SuperClass = |
| 4709 | inferSuperRegisterClass(Src->getExtType(0), Dst->getChild(2)); |
| 4710 | if (!SuperClass) |
| 4711 | return failedImport( |
| 4712 | "Cannot infer register class for SUBREG_TO_REG operand #0"); |
| 4713 | M.addAction<ConstrainOperandToRegClassAction>(0, 0, **SuperClass); |
| 4714 | M.addAction<ConstrainOperandToRegClassAction>(0, 2, **SubClass); |
| 4715 | ++NumPatternImported; |
| 4716 | return std::move(M); |
| 4717 | } |
| 4718 | |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 4719 | M.addAction<ConstrainOperandsToDefinitionAction>(0); |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 4720 | |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 4721 | // 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] | 4722 | ++NumPatternImported; |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 4723 | return std::move(M); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4724 | } |
| 4725 | |
Daniel Sanders | 649c585 | 2017-10-13 20:42:18 +0000 | [diff] [blame] | 4726 | // Emit imm predicate table and an enum to reference them with. |
| 4727 | // The 'Predicate_' part of the name is redundant but eliminating it is more |
| 4728 | // trouble than it's worth. |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 4729 | void GlobalISelEmitter::emitCxxPredicateFns( |
| 4730 | raw_ostream &OS, StringRef CodeFieldName, StringRef TypeIdentifier, |
| 4731 | StringRef ArgType, StringRef ArgName, StringRef AdditionalDeclarations, |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 4732 | std::function<bool(const Record *R)> Filter) { |
Daniel Sanders | 649c585 | 2017-10-13 20:42:18 +0000 | [diff] [blame] | 4733 | std::vector<const Record *> MatchedRecords; |
| 4734 | const auto &Defs = RK.getAllDerivedDefinitions("PatFrag"); |
| 4735 | std::copy_if(Defs.begin(), Defs.end(), std::back_inserter(MatchedRecords), |
| 4736 | [&](Record *Record) { |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 4737 | return !Record->getValueAsString(CodeFieldName).empty() && |
Daniel Sanders | 649c585 | 2017-10-13 20:42:18 +0000 | [diff] [blame] | 4738 | Filter(Record); |
| 4739 | }); |
| 4740 | |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 4741 | if (!MatchedRecords.empty()) { |
| 4742 | OS << "// PatFrag predicates.\n" |
| 4743 | << "enum {\n"; |
Daniel Sanders | 2fed4ff | 2017-10-13 21:51:20 +0000 | [diff] [blame] | 4744 | std::string EnumeratorSeparator = |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 4745 | (" = GIPFP_" + TypeIdentifier + "_Invalid + 1,\n").str(); |
| 4746 | for (const auto *Record : MatchedRecords) { |
| 4747 | OS << " GIPFP_" << TypeIdentifier << "_Predicate_" << Record->getName() |
| 4748 | << EnumeratorSeparator; |
| 4749 | EnumeratorSeparator = ",\n"; |
| 4750 | } |
| 4751 | OS << "};\n"; |
Daniel Sanders | 649c585 | 2017-10-13 20:42:18 +0000 | [diff] [blame] | 4752 | } |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 4753 | |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 4754 | OS << "bool " << Target.getName() << "InstructionSelector::test" << ArgName |
| 4755 | << "Predicate_" << TypeIdentifier << "(unsigned PredicateID, " << ArgType << " " |
| 4756 | << ArgName << ") const {\n" |
| 4757 | << AdditionalDeclarations; |
| 4758 | if (!AdditionalDeclarations.empty()) |
| 4759 | OS << "\n"; |
Aaron Ballman | 82e17f5 | 2017-12-20 20:09:30 +0000 | [diff] [blame] | 4760 | if (!MatchedRecords.empty()) |
| 4761 | OS << " switch (PredicateID) {\n"; |
Daniel Sanders | 32de8bb | 2017-12-20 14:41:51 +0000 | [diff] [blame] | 4762 | for (const auto *Record : MatchedRecords) { |
| 4763 | OS << " case GIPFP_" << TypeIdentifier << "_Predicate_" |
| 4764 | << Record->getName() << ": {\n" |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 4765 | << " " << Record->getValueAsString(CodeFieldName) << "\n" |
| 4766 | << " llvm_unreachable(\"" << CodeFieldName |
| 4767 | << " should have returned\");\n" |
Daniel Sanders | 32de8bb | 2017-12-20 14:41:51 +0000 | [diff] [blame] | 4768 | << " return false;\n" |
| 4769 | << " }\n"; |
| 4770 | } |
Aaron Ballman | 82e17f5 | 2017-12-20 20:09:30 +0000 | [diff] [blame] | 4771 | if (!MatchedRecords.empty()) |
| 4772 | OS << " }\n"; |
| 4773 | OS << " llvm_unreachable(\"Unknown predicate\");\n" |
Daniel Sanders | 32de8bb | 2017-12-20 14:41:51 +0000 | [diff] [blame] | 4774 | << " return false;\n" |
| 4775 | << "}\n"; |
Daniel Sanders | 649c585 | 2017-10-13 20:42:18 +0000 | [diff] [blame] | 4776 | } |
| 4777 | |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 4778 | void GlobalISelEmitter::emitImmPredicateFns( |
| 4779 | raw_ostream &OS, StringRef TypeIdentifier, StringRef ArgType, |
| 4780 | std::function<bool(const Record *R)> Filter) { |
| 4781 | return emitCxxPredicateFns(OS, "ImmediateCode", TypeIdentifier, ArgType, |
| 4782 | "Imm", "", Filter); |
| 4783 | } |
| 4784 | |
| 4785 | void GlobalISelEmitter::emitMIPredicateFns(raw_ostream &OS) { |
| 4786 | return emitCxxPredicateFns( |
| 4787 | OS, "GISelPredicateCode", "MI", "const MachineInstr &", "MI", |
| 4788 | " const MachineFunction &MF = *MI.getParent()->getParent();\n" |
Andrei Elovikov | 36cbbff | 2018-06-26 07:05:08 +0000 | [diff] [blame] | 4789 | " const MachineRegisterInfo &MRI = MF.getRegInfo();\n" |
| 4790 | " (void)MRI;", |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 4791 | [](const Record *R) { return true; }); |
| 4792 | } |
| 4793 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4794 | template <class GroupT> |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 4795 | std::vector<Matcher *> GlobalISelEmitter::optimizeRules( |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 4796 | ArrayRef<Matcher *> Rules, |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4797 | std::vector<std::unique_ptr<Matcher>> &MatcherStorage) { |
| 4798 | |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 4799 | std::vector<Matcher *> OptRules; |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 4800 | std::unique_ptr<GroupT> CurrentGroup = std::make_unique<GroupT>(); |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4801 | assert(CurrentGroup->empty() && "Newly created group isn't empty!"); |
| 4802 | unsigned NumGroups = 0; |
| 4803 | |
| 4804 | auto ProcessCurrentGroup = [&]() { |
| 4805 | if (CurrentGroup->empty()) |
| 4806 | // An empty group is good to be reused: |
| 4807 | return; |
| 4808 | |
| 4809 | // If the group isn't large enough to provide any benefit, move all the |
| 4810 | // added rules out of it and make sure to re-create the group to properly |
| 4811 | // re-initialize it: |
| 4812 | if (CurrentGroup->size() < 2) |
| 4813 | for (Matcher *M : CurrentGroup->matchers()) |
| 4814 | OptRules.push_back(M); |
| 4815 | else { |
| 4816 | CurrentGroup->finalize(); |
Roman Tereshin | 8bdf7be | 2018-05-21 22:21:24 +0000 | [diff] [blame] | 4817 | OptRules.push_back(CurrentGroup.get()); |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4818 | MatcherStorage.emplace_back(std::move(CurrentGroup)); |
| 4819 | ++NumGroups; |
Roman Tereshin | 8bdf7be | 2018-05-21 22:21:24 +0000 | [diff] [blame] | 4820 | } |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 4821 | CurrentGroup = std::make_unique<GroupT>(); |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4822 | }; |
| 4823 | for (Matcher *Rule : Rules) { |
| 4824 | // Greedily add as many matchers as possible to the current group: |
| 4825 | if (CurrentGroup->addMatcher(*Rule)) |
| 4826 | continue; |
| 4827 | |
| 4828 | ProcessCurrentGroup(); |
| 4829 | assert(CurrentGroup->empty() && "A group wasn't properly re-initialized"); |
| 4830 | |
| 4831 | // Try to add the pending matcher to a newly created empty group: |
| 4832 | if (!CurrentGroup->addMatcher(*Rule)) |
| 4833 | // If we couldn't add the matcher to an empty group, that group type |
| 4834 | // doesn't support that kind of matchers at all, so just skip it: |
| 4835 | OptRules.push_back(Rule); |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 4836 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4837 | ProcessCurrentGroup(); |
| 4838 | |
Nicola Zaghen | 03d0b91 | 2018-05-23 15:09:29 +0000 | [diff] [blame] | 4839 | LLVM_DEBUG(dbgs() << "NumGroups: " << NumGroups << "\n"); |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4840 | assert(CurrentGroup->empty() && "The last group wasn't properly processed"); |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 4841 | return OptRules; |
| 4842 | } |
| 4843 | |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 4844 | MatchTable |
| 4845 | GlobalISelEmitter::buildMatchTable(MutableArrayRef<RuleMatcher> Rules, |
Roman Tereshin | beb3931 | 2018-05-02 20:15:11 +0000 | [diff] [blame] | 4846 | bool Optimize, bool WithCoverage) { |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 4847 | std::vector<Matcher *> InputRules; |
| 4848 | for (Matcher &Rule : Rules) |
| 4849 | InputRules.push_back(&Rule); |
| 4850 | |
| 4851 | if (!Optimize) |
Roman Tereshin | beb3931 | 2018-05-02 20:15:11 +0000 | [diff] [blame] | 4852 | return MatchTable::buildTable(InputRules, WithCoverage); |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 4853 | |
Roman Tereshin | 7701360 | 2018-05-22 16:54:27 +0000 | [diff] [blame] | 4854 | unsigned CurrentOrdering = 0; |
| 4855 | StringMap<unsigned> OpcodeOrder; |
| 4856 | for (RuleMatcher &Rule : Rules) { |
| 4857 | const StringRef Opcode = Rule.getOpcode(); |
| 4858 | assert(!Opcode.empty() && "Didn't expect an undefined opcode"); |
| 4859 | if (OpcodeOrder.count(Opcode) == 0) |
| 4860 | OpcodeOrder[Opcode] = CurrentOrdering++; |
| 4861 | } |
| 4862 | |
| 4863 | std::stable_sort(InputRules.begin(), InputRules.end(), |
| 4864 | [&OpcodeOrder](const Matcher *A, const Matcher *B) { |
| 4865 | auto *L = static_cast<const RuleMatcher *>(A); |
| 4866 | auto *R = static_cast<const RuleMatcher *>(B); |
| 4867 | return std::make_tuple(OpcodeOrder[L->getOpcode()], |
| 4868 | L->getNumOperands()) < |
| 4869 | std::make_tuple(OpcodeOrder[R->getOpcode()], |
| 4870 | R->getNumOperands()); |
| 4871 | }); |
| 4872 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4873 | for (Matcher *Rule : InputRules) |
| 4874 | Rule->optimize(); |
| 4875 | |
| 4876 | std::vector<std::unique_ptr<Matcher>> MatcherStorage; |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 4877 | std::vector<Matcher *> OptRules = |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4878 | optimizeRules<GroupMatcher>(InputRules, MatcherStorage); |
| 4879 | |
| 4880 | for (Matcher *Rule : OptRules) |
| 4881 | Rule->optimize(); |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 4882 | |
Roman Tereshin | 0ee082f | 2018-05-22 19:37:59 +0000 | [diff] [blame] | 4883 | OptRules = optimizeRules<SwitchMatcher>(OptRules, MatcherStorage); |
| 4884 | |
Roman Tereshin | beb3931 | 2018-05-02 20:15:11 +0000 | [diff] [blame] | 4885 | return MatchTable::buildTable(OptRules, WithCoverage); |
Roman Tereshin | 2d6d376 | 2018-05-02 20:08:14 +0000 | [diff] [blame] | 4886 | } |
| 4887 | |
Roman Tereshin | fedae33 | 2018-05-23 02:04:19 +0000 | [diff] [blame] | 4888 | void GroupMatcher::optimize() { |
Roman Tereshin | 9a9fa49 | 2018-05-23 21:30:16 +0000 | [diff] [blame] | 4889 | // Make sure we only sort by a specific predicate within a range of rules that |
| 4890 | // all have that predicate checked against a specific value (not a wildcard): |
| 4891 | auto F = Matchers.begin(); |
| 4892 | auto T = F; |
| 4893 | auto E = Matchers.end(); |
| 4894 | while (T != E) { |
| 4895 | while (T != E) { |
| 4896 | auto *R = static_cast<RuleMatcher *>(*T); |
| 4897 | if (!R->getFirstConditionAsRootType().get().isValid()) |
| 4898 | break; |
| 4899 | ++T; |
| 4900 | } |
| 4901 | std::stable_sort(F, T, [](Matcher *A, Matcher *B) { |
| 4902 | auto *L = static_cast<RuleMatcher *>(A); |
| 4903 | auto *R = static_cast<RuleMatcher *>(B); |
| 4904 | return L->getFirstConditionAsRootType() < |
| 4905 | R->getFirstConditionAsRootType(); |
| 4906 | }); |
| 4907 | if (T != E) |
| 4908 | F = ++T; |
| 4909 | } |
Roman Tereshin | fedae33 | 2018-05-23 02:04:19 +0000 | [diff] [blame] | 4910 | GlobalISelEmitter::optimizeRules<GroupMatcher>(Matchers, MatcherStorage) |
| 4911 | .swap(Matchers); |
Roman Tereshin | a4c410d | 2018-05-24 00:24:15 +0000 | [diff] [blame] | 4912 | GlobalISelEmitter::optimizeRules<SwitchMatcher>(Matchers, MatcherStorage) |
| 4913 | .swap(Matchers); |
Roman Tereshin | fedae33 | 2018-05-23 02:04:19 +0000 | [diff] [blame] | 4914 | } |
| 4915 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4916 | void GlobalISelEmitter::run(raw_ostream &OS) { |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 4917 | if (!UseCoverageFile.empty()) { |
| 4918 | RuleCoverage = CodeGenCoverage(); |
| 4919 | auto RuleCoverageBufOrErr = MemoryBuffer::getFile(UseCoverageFile); |
| 4920 | if (!RuleCoverageBufOrErr) { |
| 4921 | PrintWarning(SMLoc(), "Missing rule coverage data"); |
| 4922 | RuleCoverage = None; |
| 4923 | } else { |
| 4924 | if (!RuleCoverage->parse(*RuleCoverageBufOrErr.get(), Target.getName())) { |
| 4925 | PrintWarning(SMLoc(), "Ignoring invalid or missing rule coverage data"); |
| 4926 | RuleCoverage = None; |
| 4927 | } |
| 4928 | } |
| 4929 | } |
| 4930 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 4931 | // Track the run-time opcode values |
| 4932 | gatherOpcodeValues(); |
| 4933 | // Track the run-time LLT ID values |
| 4934 | gatherTypeIDValues(); |
| 4935 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4936 | // Track the GINodeEquiv definitions. |
| 4937 | gatherNodeEquivs(); |
| 4938 | |
| 4939 | emitSourceFileHeader(("Global Instruction Selector for the " + |
| 4940 | Target.getName() + " target").str(), OS); |
Daniel Sanders | b41ce2b | 2017-02-20 14:31:27 +0000 | [diff] [blame] | 4941 | std::vector<RuleMatcher> Rules; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4942 | // Look through the SelectionDAG patterns we found, possibly emitting some. |
| 4943 | for (const PatternToMatch &Pat : CGP.ptms()) { |
| 4944 | ++NumPatternTotal; |
Daniel Sanders | 7e52367 | 2017-11-11 03:23:44 +0000 | [diff] [blame] | 4945 | |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 4946 | auto MatcherOrErr = runOnPattern(Pat); |
| 4947 | |
| 4948 | // The pattern analysis can fail, indicating an unsupported pattern. |
| 4949 | // Report that if we've been asked to do so. |
| 4950 | if (auto Err = MatcherOrErr.takeError()) { |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4951 | if (WarnOnSkippedPatterns) { |
| 4952 | PrintWarning(Pat.getSrcRecord()->getLoc(), |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 4953 | "Skipped pattern: " + toString(std::move(Err))); |
| 4954 | } else { |
| 4955 | consumeError(std::move(Err)); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4956 | } |
Daniel Sanders | b41ce2b | 2017-02-20 14:31:27 +0000 | [diff] [blame] | 4957 | ++NumPatternImportsSkipped; |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 4958 | continue; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 4959 | } |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 4960 | |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 4961 | if (RuleCoverage) { |
| 4962 | if (RuleCoverage->isCovered(MatcherOrErr->getRuleID())) |
| 4963 | ++NumPatternsTested; |
| 4964 | else |
| 4965 | PrintWarning(Pat.getSrcRecord()->getLoc(), |
| 4966 | "Pattern is not covered by a test"); |
| 4967 | } |
Daniel Sanders | b41ce2b | 2017-02-20 14:31:27 +0000 | [diff] [blame] | 4968 | Rules.push_back(std::move(MatcherOrErr.get())); |
| 4969 | } |
| 4970 | |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 4971 | // Comparison function to order records by name. |
| 4972 | auto orderByName = [](const Record *A, const Record *B) { |
| 4973 | return A->getName() < B->getName(); |
| 4974 | }; |
| 4975 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 4976 | std::vector<Record *> ComplexPredicates = |
| 4977 | RK.getAllDerivedDefinitions("GIComplexOperandMatcher"); |
Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 4978 | llvm::sort(ComplexPredicates, orderByName); |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 4979 | |
| 4980 | std::vector<Record *> CustomRendererFns = |
| 4981 | RK.getAllDerivedDefinitions("GICustomOperandRenderer"); |
Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 4982 | llvm::sort(CustomRendererFns, orderByName); |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 4983 | |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 4984 | unsigned MaxTemporaries = 0; |
| 4985 | for (const auto &Rule : Rules) |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 4986 | MaxTemporaries = std::max(MaxTemporaries, Rule.countRendererFns()); |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 4987 | |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 4988 | OS << "#ifdef GET_GLOBALISEL_PREDICATE_BITSET\n" |
| 4989 | << "const unsigned MAX_SUBTARGET_PREDICATES = " << SubtargetFeatures.size() |
| 4990 | << ";\n" |
| 4991 | << "using PredicateBitset = " |
| 4992 | "llvm::PredicateBitsetImpl<MAX_SUBTARGET_PREDICATES>;\n" |
| 4993 | << "#endif // ifdef GET_GLOBALISEL_PREDICATE_BITSET\n\n"; |
| 4994 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 4995 | OS << "#ifdef GET_GLOBALISEL_TEMPORARIES_DECL\n" |
| 4996 | << " mutable MatcherState State;\n" |
| 4997 | << " typedef " |
Daniel Sanders | 1e4569f | 2017-10-20 20:55:29 +0000 | [diff] [blame] | 4998 | "ComplexRendererFns(" |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 4999 | << Target.getName() |
| 5000 | << "InstructionSelector::*ComplexMatcherMemFn)(MachineOperand &) const;\n" |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 5001 | |
| 5002 | << " typedef void(" << Target.getName() |
| 5003 | << "InstructionSelector::*CustomRendererFn)(MachineInstrBuilder &, const " |
| 5004 | "MachineInstr&) " |
| 5005 | "const;\n" |
| 5006 | << " const ISelInfoTy<PredicateBitset, ComplexMatcherMemFn, " |
| 5007 | "CustomRendererFn> " |
| 5008 | "ISelInfo;\n"; |
| 5009 | OS << " static " << Target.getName() |
Daniel Sanders | ea8711b | 2017-10-16 03:36:29 +0000 | [diff] [blame] | 5010 | << "InstructionSelector::ComplexMatcherMemFn ComplexPredicateFns[];\n" |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 5011 | << " static " << Target.getName() |
| 5012 | << "InstructionSelector::CustomRendererFn CustomRenderers[];\n" |
Roman Tereshin | 2df4c22 | 2018-05-02 20:07:15 +0000 | [diff] [blame] | 5013 | << " bool testImmPredicate_I64(unsigned PredicateID, int64_t Imm) const " |
Daniel Sanders | 32de8bb | 2017-12-20 14:41:51 +0000 | [diff] [blame] | 5014 | "override;\n" |
Roman Tereshin | 2df4c22 | 2018-05-02 20:07:15 +0000 | [diff] [blame] | 5015 | << " bool testImmPredicate_APInt(unsigned PredicateID, const APInt &Imm) " |
Daniel Sanders | 32de8bb | 2017-12-20 14:41:51 +0000 | [diff] [blame] | 5016 | "const override;\n" |
Roman Tereshin | 2df4c22 | 2018-05-02 20:07:15 +0000 | [diff] [blame] | 5017 | << " bool testImmPredicate_APFloat(unsigned PredicateID, const APFloat " |
Daniel Sanders | 32de8bb | 2017-12-20 14:41:51 +0000 | [diff] [blame] | 5018 | "&Imm) const override;\n" |
Roman Tereshin | 2df4c22 | 2018-05-02 20:07:15 +0000 | [diff] [blame] | 5019 | << " const int64_t *getMatchTable() const override;\n" |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 5020 | << " bool testMIPredicate_MI(unsigned PredicateID, const MachineInstr &MI) " |
| 5021 | "const override;\n" |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 5022 | << "#endif // ifdef GET_GLOBALISEL_TEMPORARIES_DECL\n\n"; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 5023 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 5024 | OS << "#ifdef GET_GLOBALISEL_TEMPORARIES_INIT\n" |
| 5025 | << ", State(" << MaxTemporaries << "),\n" |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 5026 | << "ISelInfo(TypeObjects, NumTypeObjects, FeatureBitsets" |
| 5027 | << ", ComplexPredicateFns, CustomRenderers)\n" |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 5028 | << "#endif // ifdef GET_GLOBALISEL_TEMPORARIES_INIT\n\n"; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 5029 | |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 5030 | OS << "#ifdef GET_GLOBALISEL_IMPL\n"; |
| 5031 | SubtargetFeatureInfo::emitSubtargetFeatureBitEnumeration(SubtargetFeatures, |
| 5032 | OS); |
Daniel Sanders | e9fdba3 | 2017-04-29 17:30:09 +0000 | [diff] [blame] | 5033 | |
| 5034 | // Separate subtarget features by how often they must be recomputed. |
| 5035 | SubtargetFeatureInfoMap ModuleFeatures; |
| 5036 | std::copy_if(SubtargetFeatures.begin(), SubtargetFeatures.end(), |
| 5037 | std::inserter(ModuleFeatures, ModuleFeatures.end()), |
| 5038 | [](const SubtargetFeatureInfoMap::value_type &X) { |
| 5039 | return !X.second.mustRecomputePerFunction(); |
| 5040 | }); |
| 5041 | SubtargetFeatureInfoMap FunctionFeatures; |
| 5042 | std::copy_if(SubtargetFeatures.begin(), SubtargetFeatures.end(), |
| 5043 | std::inserter(FunctionFeatures, FunctionFeatures.end()), |
| 5044 | [](const SubtargetFeatureInfoMap::value_type &X) { |
| 5045 | return X.second.mustRecomputePerFunction(); |
| 5046 | }); |
| 5047 | |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 5048 | SubtargetFeatureInfo::emitComputeAvailableFeatures( |
Daniel Sanders | e9fdba3 | 2017-04-29 17:30:09 +0000 | [diff] [blame] | 5049 | Target.getName(), "InstructionSelector", "computeAvailableModuleFeatures", |
| 5050 | ModuleFeatures, OS); |
| 5051 | SubtargetFeatureInfo::emitComputeAvailableFeatures( |
| 5052 | Target.getName(), "InstructionSelector", |
| 5053 | "computeAvailableFunctionFeatures", FunctionFeatures, OS, |
| 5054 | "const MachineFunction *MF"); |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 5055 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 5056 | // Emit a table containing the LLT objects needed by the matcher and an enum |
| 5057 | // for the matcher to reference them with. |
Daniel Sanders | 032e7f2 | 2017-08-17 13:18:35 +0000 | [diff] [blame] | 5058 | std::vector<LLTCodeGen> TypeObjects; |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 5059 | for (const auto &Ty : KnownTypes) |
Daniel Sanders | 032e7f2 | 2017-08-17 13:18:35 +0000 | [diff] [blame] | 5060 | TypeObjects.push_back(Ty); |
Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 5061 | llvm::sort(TypeObjects); |
Daniel Sanders | 4998070 | 2017-08-23 10:09:25 +0000 | [diff] [blame] | 5062 | OS << "// LLT Objects.\n" |
| 5063 | << "enum {\n"; |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 5064 | for (const auto &TypeObject : TypeObjects) { |
| 5065 | OS << " "; |
| 5066 | TypeObject.emitCxxEnumValue(OS); |
| 5067 | OS << ",\n"; |
| 5068 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 5069 | OS << "};\n"; |
| 5070 | OS << "const static size_t NumTypeObjects = " << TypeObjects.size() << ";\n" |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 5071 | << "const static LLT TypeObjects[] = {\n"; |
| 5072 | for (const auto &TypeObject : TypeObjects) { |
| 5073 | OS << " "; |
| 5074 | TypeObject.emitCxxConstructorCall(OS); |
| 5075 | OS << ",\n"; |
| 5076 | } |
| 5077 | OS << "};\n\n"; |
| 5078 | |
| 5079 | // Emit a table containing the PredicateBitsets objects needed by the matcher |
| 5080 | // and an enum for the matcher to reference them with. |
| 5081 | std::vector<std::vector<Record *>> FeatureBitsets; |
| 5082 | for (auto &Rule : Rules) |
| 5083 | FeatureBitsets.push_back(Rule.getRequiredFeatures()); |
Fangrui Song | 3507c6e | 2018-09-30 22:31:29 +0000 | [diff] [blame] | 5084 | llvm::sort(FeatureBitsets, [&](const std::vector<Record *> &A, |
| 5085 | const std::vector<Record *> &B) { |
| 5086 | if (A.size() < B.size()) |
| 5087 | return true; |
| 5088 | if (A.size() > B.size()) |
| 5089 | return false; |
| 5090 | for (const auto &Pair : zip(A, B)) { |
| 5091 | if (std::get<0>(Pair)->getName() < std::get<1>(Pair)->getName()) |
| 5092 | return true; |
| 5093 | if (std::get<0>(Pair)->getName() > std::get<1>(Pair)->getName()) |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 5094 | return false; |
Fangrui Song | 3507c6e | 2018-09-30 22:31:29 +0000 | [diff] [blame] | 5095 | } |
| 5096 | return false; |
| 5097 | }); |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 5098 | FeatureBitsets.erase( |
| 5099 | std::unique(FeatureBitsets.begin(), FeatureBitsets.end()), |
| 5100 | FeatureBitsets.end()); |
Daniel Sanders | 4998070 | 2017-08-23 10:09:25 +0000 | [diff] [blame] | 5101 | OS << "// Feature bitsets.\n" |
| 5102 | << "enum {\n" |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 5103 | << " GIFBS_Invalid,\n"; |
| 5104 | for (const auto &FeatureBitset : FeatureBitsets) { |
| 5105 | if (FeatureBitset.empty()) |
| 5106 | continue; |
| 5107 | OS << " " << getNameForFeatureBitset(FeatureBitset) << ",\n"; |
| 5108 | } |
| 5109 | OS << "};\n" |
| 5110 | << "const static PredicateBitset FeatureBitsets[] {\n" |
| 5111 | << " {}, // GIFBS_Invalid\n"; |
| 5112 | for (const auto &FeatureBitset : FeatureBitsets) { |
| 5113 | if (FeatureBitset.empty()) |
| 5114 | continue; |
| 5115 | OS << " {"; |
| 5116 | for (const auto &Feature : FeatureBitset) { |
| 5117 | const auto &I = SubtargetFeatures.find(Feature); |
| 5118 | assert(I != SubtargetFeatures.end() && "Didn't import predicate?"); |
| 5119 | OS << I->second.getEnumBitName() << ", "; |
| 5120 | } |
| 5121 | OS << "},\n"; |
| 5122 | } |
| 5123 | OS << "};\n\n"; |
| 5124 | |
| 5125 | // Emit complex predicate table and an enum to reference them with. |
Daniel Sanders | 4998070 | 2017-08-23 10:09:25 +0000 | [diff] [blame] | 5126 | OS << "// ComplexPattern predicates.\n" |
| 5127 | << "enum {\n" |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 5128 | << " GICP_Invalid,\n"; |
| 5129 | for (const auto &Record : ComplexPredicates) |
| 5130 | OS << " GICP_" << Record->getName() << ",\n"; |
| 5131 | OS << "};\n" |
| 5132 | << "// See constructor for table contents\n\n"; |
| 5133 | |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 5134 | emitImmPredicateFns(OS, "I64", "int64_t", [](const Record *R) { |
Daniel Sanders | 649c585 | 2017-10-13 20:42:18 +0000 | [diff] [blame] | 5135 | bool Unset; |
| 5136 | return !R->getValueAsBitOrUnset("IsAPFloat", Unset) && |
| 5137 | !R->getValueAsBit("IsAPInt"); |
| 5138 | }); |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 5139 | emitImmPredicateFns(OS, "APFloat", "const APFloat &", [](const Record *R) { |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 5140 | bool Unset; |
| 5141 | return R->getValueAsBitOrUnset("IsAPFloat", Unset); |
| 5142 | }); |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 5143 | emitImmPredicateFns(OS, "APInt", "const APInt &", [](const Record *R) { |
Daniel Sanders | 11300ce | 2017-10-13 21:28:03 +0000 | [diff] [blame] | 5144 | return R->getValueAsBit("IsAPInt"); |
| 5145 | }); |
Daniel Sanders | 8ead129 | 2018-06-15 23:13:43 +0000 | [diff] [blame] | 5146 | emitMIPredicateFns(OS); |
Daniel Sanders | ea8711b | 2017-10-16 03:36:29 +0000 | [diff] [blame] | 5147 | OS << "\n"; |
| 5148 | |
| 5149 | OS << Target.getName() << "InstructionSelector::ComplexMatcherMemFn\n" |
| 5150 | << Target.getName() << "InstructionSelector::ComplexPredicateFns[] = {\n" |
| 5151 | << " nullptr, // GICP_Invalid\n"; |
| 5152 | for (const auto &Record : ComplexPredicates) |
| 5153 | OS << " &" << Target.getName() |
| 5154 | << "InstructionSelector::" << Record->getValueAsString("MatcherFn") |
| 5155 | << ", // " << Record->getName() << "\n"; |
| 5156 | OS << "};\n\n"; |
Daniel Sanders | 2c269f6 | 2017-08-24 09:11:20 +0000 | [diff] [blame] | 5157 | |
Volkan Keles | f7f2568 | 2018-01-16 18:44:05 +0000 | [diff] [blame] | 5158 | OS << "// Custom renderers.\n" |
| 5159 | << "enum {\n" |
| 5160 | << " GICR_Invalid,\n"; |
| 5161 | for (const auto &Record : CustomRendererFns) |
| 5162 | OS << " GICR_" << Record->getValueAsString("RendererFn") << ", \n"; |
| 5163 | OS << "};\n"; |
| 5164 | |
| 5165 | OS << Target.getName() << "InstructionSelector::CustomRendererFn\n" |
| 5166 | << Target.getName() << "InstructionSelector::CustomRenderers[] = {\n" |
| 5167 | << " nullptr, // GICP_Invalid\n"; |
| 5168 | for (const auto &Record : CustomRendererFns) |
| 5169 | OS << " &" << Target.getName() |
| 5170 | << "InstructionSelector::" << Record->getValueAsString("RendererFn") |
| 5171 | << ", // " << Record->getName() << "\n"; |
| 5172 | OS << "};\n\n"; |
| 5173 | |
Fangrui Song | efd94c5 | 2019-04-23 14:51:27 +0000 | [diff] [blame] | 5174 | llvm::stable_sort(Rules, [&](const RuleMatcher &A, const RuleMatcher &B) { |
Aditya Nandakumar | b63e763 | 2018-02-16 22:37:15 +0000 | [diff] [blame] | 5175 | int ScoreA = RuleMatcherScores[A.getRuleID()]; |
| 5176 | int ScoreB = RuleMatcherScores[B.getRuleID()]; |
| 5177 | if (ScoreA > ScoreB) |
| 5178 | return true; |
| 5179 | if (ScoreB > ScoreA) |
| 5180 | return false; |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 5181 | if (A.isHigherPriorityThan(B)) { |
| 5182 | assert(!B.isHigherPriorityThan(A) && "Cannot be more important " |
| 5183 | "and less important at " |
| 5184 | "the same time"); |
| 5185 | return true; |
| 5186 | } |
| 5187 | return false; |
| 5188 | }); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 5189 | |
Roman Tereshin | 2df4c22 | 2018-05-02 20:07:15 +0000 | [diff] [blame] | 5190 | OS << "bool " << Target.getName() |
| 5191 | << "InstructionSelector::selectImpl(MachineInstr &I, CodeGenCoverage " |
| 5192 | "&CoverageInfo) const {\n" |
| 5193 | << " MachineFunction &MF = *I.getParent()->getParent();\n" |
| 5194 | << " MachineRegisterInfo &MRI = MF.getRegInfo();\n" |
| 5195 | << " // FIXME: This should be computed on a per-function basis rather " |
| 5196 | "than per-insn.\n" |
| 5197 | << " AvailableFunctionFeatures = computeAvailableFunctionFeatures(&STI, " |
| 5198 | "&MF);\n" |
| 5199 | << " const PredicateBitset AvailableFeatures = getAvailableFeatures();\n" |
| 5200 | << " NewMIVector OutMIs;\n" |
| 5201 | << " State.MIs.clear();\n" |
| 5202 | << " State.MIs.push_back(&I);\n\n" |
| 5203 | << " if (executeMatchTable(*this, OutMIs, State, ISelInfo" |
| 5204 | << ", getMatchTable(), TII, MRI, TRI, RBI, AvailableFeatures" |
| 5205 | << ", CoverageInfo)) {\n" |
| 5206 | << " return true;\n" |
| 5207 | << " }\n\n" |
| 5208 | << " return false;\n" |
| 5209 | << "}\n\n"; |
| 5210 | |
Roman Tereshin | beb3931 | 2018-05-02 20:15:11 +0000 | [diff] [blame] | 5211 | const MatchTable Table = |
| 5212 | buildMatchTable(Rules, OptimizeMatchTable, GenerateCoverage); |
Roman Tereshin | 2df4c22 | 2018-05-02 20:07:15 +0000 | [diff] [blame] | 5213 | OS << "const int64_t *" << Target.getName() |
| 5214 | << "InstructionSelector::getMatchTable() const {\n"; |
| 5215 | Table.emitDeclaration(OS); |
| 5216 | OS << " return "; |
| 5217 | Table.emitUse(OS); |
| 5218 | OS << ";\n}\n"; |
| 5219 | OS << "#endif // ifdef GET_GLOBALISEL_IMPL\n"; |
Daniel Sanders | e9fdba3 | 2017-04-29 17:30:09 +0000 | [diff] [blame] | 5220 | |
| 5221 | OS << "#ifdef GET_GLOBALISEL_PREDICATES_DECL\n" |
| 5222 | << "PredicateBitset AvailableModuleFeatures;\n" |
| 5223 | << "mutable PredicateBitset AvailableFunctionFeatures;\n" |
| 5224 | << "PredicateBitset getAvailableFeatures() const {\n" |
| 5225 | << " return AvailableModuleFeatures | AvailableFunctionFeatures;\n" |
| 5226 | << "}\n" |
| 5227 | << "PredicateBitset\n" |
| 5228 | << "computeAvailableModuleFeatures(const " << Target.getName() |
| 5229 | << "Subtarget *Subtarget) const;\n" |
| 5230 | << "PredicateBitset\n" |
| 5231 | << "computeAvailableFunctionFeatures(const " << Target.getName() |
| 5232 | << "Subtarget *Subtarget,\n" |
| 5233 | << " const MachineFunction *MF) const;\n" |
| 5234 | << "#endif // ifdef GET_GLOBALISEL_PREDICATES_DECL\n"; |
| 5235 | |
| 5236 | OS << "#ifdef GET_GLOBALISEL_PREDICATES_INIT\n" |
| 5237 | << "AvailableModuleFeatures(computeAvailableModuleFeatures(&STI)),\n" |
| 5238 | << "AvailableFunctionFeatures()\n" |
| 5239 | << "#endif // ifdef GET_GLOBALISEL_PREDICATES_INIT\n"; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 5240 | } |
| 5241 | |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 5242 | void GlobalISelEmitter::declareSubtargetFeature(Record *Predicate) { |
| 5243 | if (SubtargetFeatures.count(Predicate) == 0) |
| 5244 | SubtargetFeatures.emplace( |
| 5245 | Predicate, SubtargetFeatureInfo(Predicate, SubtargetFeatures.size())); |
| 5246 | } |
| 5247 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 5248 | void RuleMatcher::optimize() { |
| 5249 | for (auto &Item : InsnVariableIDs) { |
| 5250 | InstructionMatcher &InsnMatcher = *Item.first; |
| 5251 | for (auto &OM : InsnMatcher.operands()) { |
Roman Tereshin | 5f5e550 | 2018-05-23 23:58:10 +0000 | [diff] [blame] | 5252 | // Complex Patterns are usually expensive and they relatively rarely fail |
| 5253 | // on their own: more often we end up throwing away all the work done by a |
| 5254 | // matching part of a complex pattern because some other part of the |
| 5255 | // enclosing pattern didn't match. All of this makes it beneficial to |
| 5256 | // delay complex patterns until the very end of the rule matching, |
| 5257 | // especially for targets having lots of complex patterns. |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 5258 | for (auto &OP : OM->predicates()) |
Roman Tereshin | 5f5e550 | 2018-05-23 23:58:10 +0000 | [diff] [blame] | 5259 | if (isa<ComplexPatternOperandMatcher>(OP)) |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 5260 | EpilogueMatchers.emplace_back(std::move(OP)); |
| 5261 | OM->eraseNullPredicates(); |
| 5262 | } |
| 5263 | InsnMatcher.optimize(); |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 5264 | } |
Fangrui Song | 3507c6e | 2018-09-30 22:31:29 +0000 | [diff] [blame] | 5265 | llvm::sort(EpilogueMatchers, [](const std::unique_ptr<PredicateMatcher> &L, |
| 5266 | const std::unique_ptr<PredicateMatcher> &R) { |
| 5267 | return std::make_tuple(L->getKind(), L->getInsnVarID(), L->getOpIdx()) < |
| 5268 | std::make_tuple(R->getKind(), R->getInsnVarID(), R->getOpIdx()); |
| 5269 | }); |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 5270 | } |
| 5271 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 5272 | bool RuleMatcher::hasFirstCondition() const { |
| 5273 | if (insnmatchers_empty()) |
| 5274 | return false; |
| 5275 | InstructionMatcher &Matcher = insnmatchers_front(); |
| 5276 | if (!Matcher.predicates_empty()) |
| 5277 | return true; |
| 5278 | for (auto &OM : Matcher.operands()) |
| 5279 | for (auto &OP : OM->predicates()) |
| 5280 | if (!isa<InstructionOperandMatcher>(OP)) |
| 5281 | return true; |
| 5282 | return false; |
| 5283 | } |
| 5284 | |
| 5285 | const PredicateMatcher &RuleMatcher::getFirstCondition() const { |
| 5286 | assert(!insnmatchers_empty() && |
| 5287 | "Trying to get a condition from an empty RuleMatcher"); |
| 5288 | |
| 5289 | InstructionMatcher &Matcher = insnmatchers_front(); |
| 5290 | if (!Matcher.predicates_empty()) |
| 5291 | return **Matcher.predicates_begin(); |
| 5292 | // If there is no more predicate on the instruction itself, look at its |
| 5293 | // operands. |
| 5294 | for (auto &OM : Matcher.operands()) |
| 5295 | for (auto &OP : OM->predicates()) |
| 5296 | if (!isa<InstructionOperandMatcher>(OP)) |
| 5297 | return *OP; |
| 5298 | |
| 5299 | llvm_unreachable("Trying to get a condition from an InstructionMatcher with " |
| 5300 | "no conditions"); |
| 5301 | } |
| 5302 | |
| 5303 | std::unique_ptr<PredicateMatcher> RuleMatcher::popFirstCondition() { |
| 5304 | assert(!insnmatchers_empty() && |
| 5305 | "Trying to pop a condition from an empty RuleMatcher"); |
| 5306 | |
| 5307 | InstructionMatcher &Matcher = insnmatchers_front(); |
| 5308 | if (!Matcher.predicates_empty()) |
| 5309 | return Matcher.predicates_pop_front(); |
| 5310 | // If there is no more predicate on the instruction itself, look at its |
| 5311 | // operands. |
| 5312 | for (auto &OM : Matcher.operands()) |
| 5313 | for (auto &OP : OM->predicates()) |
| 5314 | if (!isa<InstructionOperandMatcher>(OP)) { |
| 5315 | std::unique_ptr<PredicateMatcher> Result = std::move(OP); |
| 5316 | OM->eraseNullPredicates(); |
| 5317 | return Result; |
| 5318 | } |
| 5319 | |
| 5320 | llvm_unreachable("Trying to pop a condition from an InstructionMatcher with " |
| 5321 | "no conditions"); |
| 5322 | } |
| 5323 | |
| 5324 | bool GroupMatcher::candidateConditionMatches( |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 5325 | const PredicateMatcher &Predicate) const { |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 5326 | |
| 5327 | if (empty()) { |
| 5328 | // Sharing predicates for nested instructions is not supported yet as we |
| 5329 | // currently don't hoist the GIM_RecordInsn's properly, therefore we can |
| 5330 | // only work on the original root instruction (InsnVarID == 0): |
| 5331 | if (Predicate.getInsnVarID() != 0) |
| 5332 | return false; |
| 5333 | // ... otherwise an empty group can handle any predicate with no specific |
| 5334 | // requirements: |
| 5335 | return true; |
| 5336 | } |
| 5337 | |
| 5338 | const Matcher &Representative = **Matchers.begin(); |
| 5339 | const auto &RepresentativeCondition = Representative.getFirstCondition(); |
| 5340 | // ... if not empty, the group can only accomodate matchers with the exact |
| 5341 | // same first condition: |
| 5342 | return Predicate.isIdentical(RepresentativeCondition); |
| 5343 | } |
| 5344 | |
| 5345 | bool GroupMatcher::addMatcher(Matcher &Candidate) { |
| 5346 | if (!Candidate.hasFirstCondition()) |
| 5347 | return false; |
| 5348 | |
| 5349 | const PredicateMatcher &Predicate = Candidate.getFirstCondition(); |
| 5350 | if (!candidateConditionMatches(Predicate)) |
| 5351 | return false; |
| 5352 | |
| 5353 | Matchers.push_back(&Candidate); |
| 5354 | return true; |
| 5355 | } |
| 5356 | |
| 5357 | void GroupMatcher::finalize() { |
| 5358 | assert(Conditions.empty() && "Already finalized?"); |
| 5359 | if (empty()) |
| 5360 | return; |
| 5361 | |
| 5362 | Matcher &FirstRule = **Matchers.begin(); |
Roman Tereshin | 152fc16 | 2018-05-23 22:50:53 +0000 | [diff] [blame] | 5363 | for (;;) { |
| 5364 | // All the checks are expected to succeed during the first iteration: |
| 5365 | for (const auto &Rule : Matchers) |
| 5366 | if (!Rule->hasFirstCondition()) |
| 5367 | return; |
| 5368 | const auto &FirstCondition = FirstRule.getFirstCondition(); |
| 5369 | for (unsigned I = 1, E = Matchers.size(); I < E; ++I) |
| 5370 | if (!Matchers[I]->getFirstCondition().isIdentical(FirstCondition)) |
| 5371 | return; |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 5372 | |
Roman Tereshin | 152fc16 | 2018-05-23 22:50:53 +0000 | [diff] [blame] | 5373 | Conditions.push_back(FirstRule.popFirstCondition()); |
| 5374 | for (unsigned I = 1, E = Matchers.size(); I < E; ++I) |
| 5375 | Matchers[I]->popFirstCondition(); |
| 5376 | } |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 5377 | } |
| 5378 | |
| 5379 | void GroupMatcher::emit(MatchTable &Table) { |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 5380 | unsigned LabelID = ~0U; |
| 5381 | if (!Conditions.empty()) { |
| 5382 | LabelID = Table.allocateLabelID(); |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 5383 | Table << MatchTable::Opcode("GIM_Try", +1) |
| 5384 | << MatchTable::Comment("On fail goto") |
| 5385 | << MatchTable::JumpTarget(LabelID) << MatchTable::LineBreak; |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 5386 | } |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 5387 | for (auto &Condition : Conditions) |
| 5388 | Condition->emitPredicateOpcodes( |
| 5389 | Table, *static_cast<RuleMatcher *>(*Matchers.begin())); |
| 5390 | |
| 5391 | for (const auto &M : Matchers) |
| 5392 | M->emit(Table); |
| 5393 | |
| 5394 | // Exit the group |
| 5395 | if (!Conditions.empty()) |
| 5396 | Table << MatchTable::Opcode("GIM_Reject", -1) << MatchTable::LineBreak |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 5397 | << MatchTable::Label(LabelID); |
Quentin Colombet | ec76d9c | 2017-12-18 19:47:41 +0000 | [diff] [blame] | 5398 | } |
| 5399 | |
Roman Tereshin | 0ee082f | 2018-05-22 19:37:59 +0000 | [diff] [blame] | 5400 | bool SwitchMatcher::isSupportedPredicateType(const PredicateMatcher &P) { |
Roman Tereshin | a4c410d | 2018-05-24 00:24:15 +0000 | [diff] [blame] | 5401 | return isa<InstructionOpcodeMatcher>(P) || isa<LLTOperandMatcher>(P); |
Roman Tereshin | 0ee082f | 2018-05-22 19:37:59 +0000 | [diff] [blame] | 5402 | } |
| 5403 | |
| 5404 | bool SwitchMatcher::candidateConditionMatches( |
| 5405 | const PredicateMatcher &Predicate) const { |
| 5406 | |
| 5407 | if (empty()) { |
| 5408 | // Sharing predicates for nested instructions is not supported yet as we |
| 5409 | // currently don't hoist the GIM_RecordInsn's properly, therefore we can |
| 5410 | // only work on the original root instruction (InsnVarID == 0): |
| 5411 | if (Predicate.getInsnVarID() != 0) |
| 5412 | return false; |
| 5413 | // ... while an attempt to add even a root matcher to an empty SwitchMatcher |
| 5414 | // could fail as not all the types of conditions are supported: |
| 5415 | if (!isSupportedPredicateType(Predicate)) |
| 5416 | return false; |
| 5417 | // ... or the condition might not have a proper implementation of |
| 5418 | // getValue() / isIdenticalDownToValue() yet: |
| 5419 | if (!Predicate.hasValue()) |
| 5420 | return false; |
| 5421 | // ... otherwise an empty Switch can accomodate the condition with no |
| 5422 | // further requirements: |
| 5423 | return true; |
| 5424 | } |
| 5425 | |
| 5426 | const Matcher &CaseRepresentative = **Matchers.begin(); |
| 5427 | const auto &RepresentativeCondition = CaseRepresentative.getFirstCondition(); |
| 5428 | // Switch-cases must share the same kind of condition and path to the value it |
| 5429 | // checks: |
| 5430 | if (!Predicate.isIdenticalDownToValue(RepresentativeCondition)) |
| 5431 | return false; |
| 5432 | |
| 5433 | const auto Value = Predicate.getValue(); |
| 5434 | // ... but be unique with respect to the actual value they check: |
| 5435 | return Values.count(Value) == 0; |
| 5436 | } |
| 5437 | |
| 5438 | bool SwitchMatcher::addMatcher(Matcher &Candidate) { |
| 5439 | if (!Candidate.hasFirstCondition()) |
| 5440 | return false; |
| 5441 | |
| 5442 | const PredicateMatcher &Predicate = Candidate.getFirstCondition(); |
| 5443 | if (!candidateConditionMatches(Predicate)) |
| 5444 | return false; |
| 5445 | const auto Value = Predicate.getValue(); |
| 5446 | Values.insert(Value); |
| 5447 | |
| 5448 | Matchers.push_back(&Candidate); |
| 5449 | return true; |
| 5450 | } |
| 5451 | |
| 5452 | void SwitchMatcher::finalize() { |
| 5453 | assert(Condition == nullptr && "Already finalized"); |
| 5454 | assert(Values.size() == Matchers.size() && "Broken SwitchMatcher"); |
| 5455 | if (empty()) |
| 5456 | return; |
| 5457 | |
| 5458 | std::stable_sort(Matchers.begin(), Matchers.end(), |
| 5459 | [](const Matcher *L, const Matcher *R) { |
| 5460 | return L->getFirstCondition().getValue() < |
| 5461 | R->getFirstCondition().getValue(); |
| 5462 | }); |
| 5463 | Condition = Matchers[0]->popFirstCondition(); |
| 5464 | for (unsigned I = 1, E = Values.size(); I < E; ++I) |
| 5465 | Matchers[I]->popFirstCondition(); |
| 5466 | } |
| 5467 | |
| 5468 | void SwitchMatcher::emitPredicateSpecificOpcodes(const PredicateMatcher &P, |
| 5469 | MatchTable &Table) { |
| 5470 | assert(isSupportedPredicateType(P) && "Predicate type is not supported"); |
| 5471 | |
| 5472 | if (const auto *Condition = dyn_cast<InstructionOpcodeMatcher>(&P)) { |
| 5473 | Table << MatchTable::Opcode("GIM_SwitchOpcode") << MatchTable::Comment("MI") |
| 5474 | << MatchTable::IntValue(Condition->getInsnVarID()); |
| 5475 | return; |
| 5476 | } |
Roman Tereshin | a4c410d | 2018-05-24 00:24:15 +0000 | [diff] [blame] | 5477 | if (const auto *Condition = dyn_cast<LLTOperandMatcher>(&P)) { |
| 5478 | Table << MatchTable::Opcode("GIM_SwitchType") << MatchTable::Comment("MI") |
| 5479 | << MatchTable::IntValue(Condition->getInsnVarID()) |
| 5480 | << MatchTable::Comment("Op") |
| 5481 | << MatchTable::IntValue(Condition->getOpIdx()); |
| 5482 | return; |
| 5483 | } |
Roman Tereshin | 0ee082f | 2018-05-22 19:37:59 +0000 | [diff] [blame] | 5484 | |
| 5485 | llvm_unreachable("emitPredicateSpecificOpcodes is broken: can not handle a " |
| 5486 | "predicate type that is claimed to be supported"); |
| 5487 | } |
| 5488 | |
| 5489 | void SwitchMatcher::emit(MatchTable &Table) { |
| 5490 | assert(Values.size() == Matchers.size() && "Broken SwitchMatcher"); |
| 5491 | if (empty()) |
| 5492 | return; |
| 5493 | assert(Condition != nullptr && |
| 5494 | "Broken SwitchMatcher, hasn't been finalized?"); |
| 5495 | |
| 5496 | std::vector<unsigned> LabelIDs(Values.size()); |
| 5497 | std::generate(LabelIDs.begin(), LabelIDs.end(), |
| 5498 | [&Table]() { return Table.allocateLabelID(); }); |
| 5499 | const unsigned Default = Table.allocateLabelID(); |
| 5500 | |
| 5501 | const int64_t LowerBound = Values.begin()->getRawValue(); |
| 5502 | const int64_t UpperBound = Values.rbegin()->getRawValue() + 1; |
| 5503 | |
| 5504 | emitPredicateSpecificOpcodes(*Condition, Table); |
| 5505 | |
| 5506 | Table << MatchTable::Comment("[") << MatchTable::IntValue(LowerBound) |
| 5507 | << MatchTable::IntValue(UpperBound) << MatchTable::Comment(")") |
| 5508 | << MatchTable::Comment("default:") << MatchTable::JumpTarget(Default); |
| 5509 | |
| 5510 | int64_t J = LowerBound; |
| 5511 | auto VI = Values.begin(); |
| 5512 | for (unsigned I = 0, E = Values.size(); I < E; ++I) { |
| 5513 | auto V = *VI++; |
| 5514 | while (J++ < V.getRawValue()) |
| 5515 | Table << MatchTable::IntValue(0); |
| 5516 | V.turnIntoComment(); |
| 5517 | Table << MatchTable::LineBreak << V << MatchTable::JumpTarget(LabelIDs[I]); |
| 5518 | } |
| 5519 | Table << MatchTable::LineBreak; |
| 5520 | |
| 5521 | for (unsigned I = 0, E = Values.size(); I < E; ++I) { |
| 5522 | Table << MatchTable::Label(LabelIDs[I]); |
| 5523 | Matchers[I]->emit(Table); |
| 5524 | Table << MatchTable::Opcode("GIM_Reject") << MatchTable::LineBreak; |
| 5525 | } |
| 5526 | Table << MatchTable::Label(Default); |
| 5527 | } |
| 5528 | |
Roman Tereshin | f1aa348 | 2018-05-21 23:28:51 +0000 | [diff] [blame] | 5529 | unsigned OperandMatcher::getInsnVarID() const { return Insn.getInsnVarID(); } |
Quentin Colombet | aad20be | 2017-12-15 23:07:42 +0000 | [diff] [blame] | 5530 | |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 5531 | } // end anonymous namespace |
| 5532 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 5533 | //===----------------------------------------------------------------------===// |
| 5534 | |
| 5535 | namespace llvm { |
| 5536 | void EmitGlobalISel(RecordKeeper &RK, raw_ostream &OS) { |
| 5537 | GlobalISelEmitter(RK).run(OS); |
| 5538 | } |
| 5539 | } // End llvm namespace |