Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1 | //===- GlobalISelEmitter.cpp - Generate an instruction selector -----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | /// \file |
| 11 | /// This tablegen backend emits code for use by the GlobalISel instruction |
| 12 | /// selector. See include/llvm/CodeGen/TargetGlobalISel.td. |
| 13 | /// |
| 14 | /// This file analyzes the patterns recognized by the SelectionDAGISel tablegen |
| 15 | /// backend, filters out the ones that are unsupported, maps |
| 16 | /// SelectionDAG-specific constructs to their GlobalISel counterpart |
| 17 | /// (when applicable: MVT to LLT; SDNode to generic Instruction). |
| 18 | /// |
| 19 | /// Not all patterns are supported: pass the tablegen invocation |
| 20 | /// "-warn-on-skipped-patterns" to emit a warning when a pattern is skipped, |
| 21 | /// as well as why. |
| 22 | /// |
| 23 | /// The generated file defines a single method: |
| 24 | /// bool <Target>InstructionSelector::selectImpl(MachineInstr &I) const; |
| 25 | /// intended to be used in InstructionSelector::select as the first-step |
| 26 | /// selector for the patterns that don't require complex C++. |
| 27 | /// |
| 28 | /// FIXME: We'll probably want to eventually define a base |
| 29 | /// "TargetGenInstructionSelector" class. |
| 30 | /// |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
| 33 | #include "CodeGenDAGPatterns.h" |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 34 | #include "SubtargetFeatureInfo.h" |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/Optional.h" |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/SmallSet.h" |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/Statistic.h" |
| 38 | #include "llvm/CodeGen/MachineValueType.h" |
| 39 | #include "llvm/Support/CommandLine.h" |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 40 | #include "llvm/Support/Error.h" |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 41 | #include "llvm/Support/LowLevelTypeImpl.h" |
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" |
| 46 | #include <string> |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 47 | #include <numeric> |
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"); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 55 | STATISTIC(NumPatternEmitted, "Number of patterns emitted"); |
| 56 | |
Daniel Sanders | 0848b23 | 2017-03-27 13:15:13 +0000 | [diff] [blame] | 57 | cl::OptionCategory GlobalISelEmitterCat("Options for -gen-global-isel"); |
| 58 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 59 | static cl::opt<bool> WarnOnSkippedPatterns( |
| 60 | "warn-on-skipped-patterns", |
| 61 | cl::desc("Explain why a pattern was skipped for inclusion " |
| 62 | "in the GlobalISel selector"), |
Daniel Sanders | 0848b23 | 2017-03-27 13:15:13 +0000 | [diff] [blame] | 63 | cl::init(false), cl::cat(GlobalISelEmitterCat)); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 64 | |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 65 | namespace { |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 66 | //===- Helper functions ---------------------------------------------------===// |
| 67 | |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 68 | /// This class stands in for LLT wherever we want to tablegen-erate an |
| 69 | /// equivalent at compiler run-time. |
| 70 | class LLTCodeGen { |
| 71 | private: |
| 72 | LLT Ty; |
| 73 | |
| 74 | public: |
| 75 | LLTCodeGen(const LLT &Ty) : Ty(Ty) {} |
| 76 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 77 | std::string getCxxEnumValue() const { |
| 78 | std::string Str; |
| 79 | raw_string_ostream OS(Str); |
| 80 | |
| 81 | emitCxxEnumValue(OS); |
| 82 | return OS.str(); |
| 83 | } |
| 84 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 85 | void emitCxxEnumValue(raw_ostream &OS) const { |
| 86 | if (Ty.isScalar()) { |
| 87 | OS << "GILLT_s" << Ty.getSizeInBits(); |
| 88 | return; |
| 89 | } |
| 90 | if (Ty.isVector()) { |
| 91 | OS << "GILLT_v" << Ty.getNumElements() << "s" << Ty.getScalarSizeInBits(); |
| 92 | return; |
| 93 | } |
| 94 | llvm_unreachable("Unhandled LLT"); |
| 95 | } |
| 96 | |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 97 | void emitCxxConstructorCall(raw_ostream &OS) const { |
| 98 | if (Ty.isScalar()) { |
| 99 | OS << "LLT::scalar(" << Ty.getSizeInBits() << ")"; |
| 100 | return; |
| 101 | } |
| 102 | if (Ty.isVector()) { |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 103 | OS << "LLT::vector(" << Ty.getNumElements() << ", " |
| 104 | << Ty.getScalarSizeInBits() << ")"; |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 105 | return; |
| 106 | } |
| 107 | llvm_unreachable("Unhandled LLT"); |
| 108 | } |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 109 | |
| 110 | const LLT &get() const { return Ty; } |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 111 | |
| 112 | /// This ordering is used for std::unique() and std::sort(). There's no |
Daniel Sanders | eb2f5f3 | 2017-08-15 15:10:31 +0000 | [diff] [blame^] | 113 | /// particular logic behind the order. |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 114 | bool operator<(const LLTCodeGen &Other) const { |
| 115 | if (!Ty.isValid()) |
Daniel Sanders | eb2f5f3 | 2017-08-15 15:10:31 +0000 | [diff] [blame^] | 116 | return Other.Ty.isValid(); |
| 117 | if (Ty.isScalar()) { |
| 118 | if (!Other.Ty.isValid()) |
| 119 | return false; |
| 120 | if (Other.Ty.isScalar()) |
| 121 | return Ty.getSizeInBits() < Other.Ty.getSizeInBits(); |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 122 | return false; |
Daniel Sanders | eb2f5f3 | 2017-08-15 15:10:31 +0000 | [diff] [blame^] | 123 | } |
| 124 | if (Ty.isVector()) { |
| 125 | if (!Other.Ty.isValid() || Other.Ty.isScalar()) |
| 126 | return false; |
| 127 | if (Other.Ty.isVector()) { |
| 128 | if (Ty.getNumElements() < Other.Ty.getNumElements()) |
| 129 | return true; |
| 130 | if (Ty.getNumElements() > Other.Ty.getNumElements()) |
| 131 | return false; |
| 132 | return Ty.getSizeInBits() < Other.Ty.getSizeInBits(); |
| 133 | } |
| 134 | return false; |
| 135 | } |
| 136 | llvm_unreachable("Unhandled LLT"); |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 137 | } |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | class InstructionMatcher; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 141 | /// Convert an MVT to an equivalent LLT if possible, or the invalid LLT() for |
| 142 | /// 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] | 143 | static Optional<LLTCodeGen> MVTToLLT(MVT::SimpleValueType SVT) { |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 144 | MVT VT(SVT); |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 145 | if (VT.isVector() && VT.getVectorNumElements() != 1) |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 146 | return LLTCodeGen( |
| 147 | LLT::vector(VT.getVectorNumElements(), VT.getScalarSizeInBits())); |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 148 | if (VT.isInteger() || VT.isFloatingPoint()) |
| 149 | return LLTCodeGen(LLT::scalar(VT.getSizeInBits())); |
| 150 | return None; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 153 | static std::string explainPredicates(const TreePatternNode *N) { |
| 154 | std::string Explanation = ""; |
| 155 | StringRef Separator = ""; |
| 156 | for (const auto &P : N->getPredicateFns()) { |
| 157 | Explanation += |
| 158 | (Separator + P.getOrigPatFragRecord()->getRecord()->getName()).str(); |
| 159 | if (P.isAlwaysTrue()) |
| 160 | Explanation += " always-true"; |
| 161 | if (P.isImmediatePattern()) |
| 162 | Explanation += " immediate"; |
| 163 | } |
| 164 | return Explanation; |
| 165 | } |
| 166 | |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 167 | std::string explainOperator(Record *Operator) { |
| 168 | if (Operator->isSubClassOf("SDNode")) |
Craig Topper | 2b8419a | 2017-05-31 19:01:11 +0000 | [diff] [blame] | 169 | return (" (" + Operator->getValueAsString("Opcode") + ")").str(); |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 170 | |
| 171 | if (Operator->isSubClassOf("Intrinsic")) |
| 172 | return (" (Operator is an Intrinsic, " + Operator->getName() + ")").str(); |
| 173 | |
| 174 | return " (Operator not understood)"; |
| 175 | } |
| 176 | |
| 177 | /// Helper function to let the emitter report skip reason error messages. |
| 178 | static Error failedImport(const Twine &Reason) { |
| 179 | return make_error<StringError>(Reason, inconvertibleErrorCode()); |
| 180 | } |
| 181 | |
| 182 | static Error isTrivialOperatorNode(const TreePatternNode *N) { |
| 183 | std::string Explanation = ""; |
| 184 | std::string Separator = ""; |
Daniel Sanders | eb2f5f3 | 2017-08-15 15:10:31 +0000 | [diff] [blame^] | 185 | if (N->isLeaf()) { |
| 186 | if (isa<IntInit>(N->getLeafValue())) |
| 187 | return Error::success(); |
| 188 | |
| 189 | Explanation = "Is a leaf"; |
| 190 | Separator = ", "; |
| 191 | } |
| 192 | |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 193 | if (N->hasAnyPredicate()) { |
| 194 | Explanation = Separator + "Has a predicate (" + explainPredicates(N) + ")"; |
| 195 | Separator = ", "; |
| 196 | } |
| 197 | |
| 198 | if (N->getTransformFn()) { |
| 199 | Explanation += Separator + "Has a transform function"; |
| 200 | Separator = ", "; |
| 201 | } |
| 202 | |
Daniel Sanders | eb2f5f3 | 2017-08-15 15:10:31 +0000 | [diff] [blame^] | 203 | if (!N->isLeaf() && !N->hasAnyPredicate() && !N->getTransformFn()) |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 204 | return Error::success(); |
| 205 | |
| 206 | return failedImport(Explanation); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 209 | static Record *getInitValueAsRegClass(Init *V) { |
| 210 | if (DefInit *VDefInit = dyn_cast<DefInit>(V)) { |
| 211 | if (VDefInit->getDef()->isSubClassOf("RegisterOperand")) |
| 212 | return VDefInit->getDef()->getValueAsDef("RegClass"); |
| 213 | if (VDefInit->getDef()->isSubClassOf("RegisterClass")) |
| 214 | return VDefInit->getDef(); |
| 215 | } |
| 216 | return nullptr; |
| 217 | } |
| 218 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 219 | std::string |
| 220 | getNameForFeatureBitset(const std::vector<Record *> &FeatureBitset) { |
| 221 | std::string Name = "GIFBS"; |
| 222 | for (const auto &Feature : FeatureBitset) |
| 223 | Name += ("_" + Feature->getName()).str(); |
| 224 | return Name; |
| 225 | } |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 226 | |
| 227 | //===- MatchTable Helpers -------------------------------------------------===// |
| 228 | |
| 229 | class MatchTable; |
| 230 | |
| 231 | /// A record to be stored in a MatchTable. |
| 232 | /// |
| 233 | /// This class represents any and all output that may be required to emit the |
| 234 | /// MatchTable. Instances are most often configured to represent an opcode or |
| 235 | /// value that will be emitted to the table with some formatting but it can also |
| 236 | /// represent commas, comments, and other formatting instructions. |
| 237 | struct MatchTableRecord { |
| 238 | enum RecordFlagsBits { |
| 239 | MTRF_None = 0x0, |
| 240 | /// Causes EmitStr to be formatted as comment when emitted. |
| 241 | MTRF_Comment = 0x1, |
| 242 | /// Causes the record value to be followed by a comma when emitted. |
| 243 | MTRF_CommaFollows = 0x2, |
| 244 | /// Causes the record value to be followed by a line break when emitted. |
| 245 | MTRF_LineBreakFollows = 0x4, |
| 246 | /// Indicates that the record defines a label and causes an additional |
| 247 | /// comment to be emitted containing the index of the label. |
| 248 | MTRF_Label = 0x8, |
| 249 | /// Causes the record to be emitted as the index of the label specified by |
| 250 | /// LabelID along with a comment indicating where that label is. |
| 251 | MTRF_JumpTarget = 0x10, |
| 252 | /// Causes the formatter to add a level of indentation before emitting the |
| 253 | /// record. |
| 254 | MTRF_Indent = 0x20, |
| 255 | /// Causes the formatter to remove a level of indentation after emitting the |
| 256 | /// record. |
| 257 | MTRF_Outdent = 0x40, |
| 258 | }; |
| 259 | |
| 260 | /// When MTRF_Label or MTRF_JumpTarget is used, indicates a label id to |
| 261 | /// reference or define. |
| 262 | unsigned LabelID; |
| 263 | /// The string to emit. Depending on the MTRF_* flags it may be a comment, a |
| 264 | /// value, a label name. |
| 265 | std::string EmitStr; |
| 266 | |
| 267 | private: |
| 268 | /// The number of MatchTable elements described by this record. Comments are 0 |
| 269 | /// while values are typically 1. Values >1 may occur when we need to emit |
| 270 | /// values that exceed the size of a MatchTable element. |
| 271 | unsigned NumElements; |
| 272 | |
| 273 | public: |
| 274 | /// A bitfield of RecordFlagsBits flags. |
| 275 | unsigned Flags; |
| 276 | |
| 277 | MatchTableRecord(Optional<unsigned> LabelID_, StringRef EmitStr, |
| 278 | unsigned NumElements, unsigned Flags) |
| 279 | : LabelID(LabelID_.hasValue() ? LabelID_.getValue() : ~0u), |
| 280 | EmitStr(EmitStr), NumElements(NumElements), Flags(Flags) { |
| 281 | assert((!LabelID_.hasValue() || LabelID != ~0u) && |
| 282 | "This value is reserved for non-labels"); |
| 283 | } |
| 284 | |
| 285 | void emit(raw_ostream &OS, bool LineBreakNextAfterThis, |
| 286 | const MatchTable &Table) const; |
| 287 | unsigned size() const { return NumElements; } |
| 288 | }; |
| 289 | |
| 290 | /// Holds the contents of a generated MatchTable to enable formatting and the |
| 291 | /// necessary index tracking needed to support GIM_Try. |
| 292 | class MatchTable { |
| 293 | /// An unique identifier for the table. The generated table will be named |
| 294 | /// MatchTable${ID}. |
| 295 | unsigned ID; |
| 296 | /// The records that make up the table. Also includes comments describing the |
| 297 | /// values being emitted and line breaks to format it. |
| 298 | std::vector<MatchTableRecord> Contents; |
| 299 | /// The currently defined labels. |
| 300 | DenseMap<unsigned, unsigned> LabelMap; |
| 301 | /// Tracks the sum of MatchTableRecord::NumElements as the table is built. |
| 302 | unsigned CurrentSize; |
| 303 | |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 304 | /// A unique identifier for a MatchTable label. |
| 305 | static unsigned CurrentLabelID; |
| 306 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 307 | public: |
| 308 | static MatchTableRecord LineBreak; |
| 309 | static MatchTableRecord Comment(StringRef Comment) { |
| 310 | return MatchTableRecord(None, Comment, 0, MatchTableRecord::MTRF_Comment); |
| 311 | } |
| 312 | static MatchTableRecord Opcode(StringRef Opcode, int IndentAdjust = 0) { |
| 313 | unsigned ExtraFlags = 0; |
| 314 | if (IndentAdjust > 0) |
| 315 | ExtraFlags |= MatchTableRecord::MTRF_Indent; |
| 316 | if (IndentAdjust < 0) |
| 317 | ExtraFlags |= MatchTableRecord::MTRF_Outdent; |
| 318 | |
| 319 | return MatchTableRecord(None, Opcode, 1, |
| 320 | MatchTableRecord::MTRF_CommaFollows | ExtraFlags); |
| 321 | } |
| 322 | static MatchTableRecord NamedValue(StringRef NamedValue) { |
| 323 | return MatchTableRecord(None, NamedValue, 1, |
| 324 | MatchTableRecord::MTRF_CommaFollows); |
| 325 | } |
| 326 | static MatchTableRecord NamedValue(StringRef Namespace, |
| 327 | StringRef NamedValue) { |
| 328 | return MatchTableRecord(None, (Namespace + "::" + NamedValue).str(), 1, |
| 329 | MatchTableRecord::MTRF_CommaFollows); |
| 330 | } |
| 331 | static MatchTableRecord IntValue(int64_t IntValue) { |
| 332 | return MatchTableRecord(None, llvm::to_string(IntValue), 1, |
| 333 | MatchTableRecord::MTRF_CommaFollows); |
| 334 | } |
| 335 | static MatchTableRecord Label(unsigned LabelID) { |
| 336 | return MatchTableRecord(LabelID, "Label " + llvm::to_string(LabelID), 0, |
| 337 | MatchTableRecord::MTRF_Label | |
| 338 | MatchTableRecord::MTRF_Comment | |
| 339 | MatchTableRecord::MTRF_LineBreakFollows); |
| 340 | } |
| 341 | static MatchTableRecord JumpTarget(unsigned LabelID) { |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 342 | return MatchTableRecord(LabelID, "Label " + llvm::to_string(LabelID), 1, |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 343 | MatchTableRecord::MTRF_JumpTarget | |
| 344 | MatchTableRecord::MTRF_Comment | |
| 345 | MatchTableRecord::MTRF_CommaFollows); |
| 346 | } |
| 347 | |
| 348 | MatchTable(unsigned ID) : ID(ID), CurrentSize(0) {} |
| 349 | |
| 350 | void push_back(const MatchTableRecord &Value) { |
| 351 | if (Value.Flags & MatchTableRecord::MTRF_Label) |
| 352 | defineLabel(Value.LabelID); |
| 353 | Contents.push_back(Value); |
| 354 | CurrentSize += Value.size(); |
| 355 | } |
| 356 | |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 357 | unsigned allocateLabelID() const { return CurrentLabelID++; } |
| 358 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 359 | void defineLabel(unsigned LabelID) { |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 360 | LabelMap.insert(std::make_pair(LabelID, CurrentSize)); |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | unsigned getLabelIndex(unsigned LabelID) const { |
| 364 | const auto I = LabelMap.find(LabelID); |
| 365 | assert(I != LabelMap.end() && "Use of undeclared label"); |
| 366 | return I->second; |
| 367 | } |
| 368 | |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 369 | void emitUse(raw_ostream &OS) const { OS << "MatchTable" << ID; } |
| 370 | |
| 371 | void emitDeclaration(raw_ostream &OS) const { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 372 | unsigned Indentation = 4; |
Daniel Sanders | cbbbfe4 | 2017-07-27 12:47:31 +0000 | [diff] [blame] | 373 | OS << " constexpr static int64_t MatchTable" << ID << "[] = {"; |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 374 | LineBreak.emit(OS, true, *this); |
| 375 | OS << std::string(Indentation, ' '); |
| 376 | |
| 377 | for (auto I = Contents.begin(), E = Contents.end(); I != E; |
| 378 | ++I) { |
| 379 | bool LineBreakIsNext = false; |
| 380 | const auto &NextI = std::next(I); |
| 381 | |
| 382 | if (NextI != E) { |
| 383 | if (NextI->EmitStr == "" && |
| 384 | NextI->Flags == MatchTableRecord::MTRF_LineBreakFollows) |
| 385 | LineBreakIsNext = true; |
| 386 | } |
| 387 | |
| 388 | if (I->Flags & MatchTableRecord::MTRF_Indent) |
| 389 | Indentation += 2; |
| 390 | |
| 391 | I->emit(OS, LineBreakIsNext, *this); |
| 392 | if (I->Flags & MatchTableRecord::MTRF_LineBreakFollows) |
| 393 | OS << std::string(Indentation, ' '); |
| 394 | |
| 395 | if (I->Flags & MatchTableRecord::MTRF_Outdent) |
| 396 | Indentation -= 2; |
| 397 | } |
| 398 | OS << "};\n"; |
| 399 | } |
| 400 | }; |
| 401 | |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 402 | unsigned MatchTable::CurrentLabelID = 0; |
| 403 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 404 | MatchTableRecord MatchTable::LineBreak = { |
| 405 | None, "" /* Emit String */, 0 /* Elements */, |
| 406 | MatchTableRecord::MTRF_LineBreakFollows}; |
| 407 | |
| 408 | void MatchTableRecord::emit(raw_ostream &OS, bool LineBreakIsNextAfterThis, |
| 409 | const MatchTable &Table) const { |
| 410 | bool UseLineComment = |
| 411 | LineBreakIsNextAfterThis | (Flags & MTRF_LineBreakFollows); |
| 412 | if (Flags & (MTRF_JumpTarget | MTRF_CommaFollows)) |
| 413 | UseLineComment = false; |
| 414 | |
| 415 | if (Flags & MTRF_Comment) |
| 416 | OS << (UseLineComment ? "// " : "/*"); |
| 417 | |
| 418 | OS << EmitStr; |
| 419 | if (Flags & MTRF_Label) |
| 420 | OS << ": @" << Table.getLabelIndex(LabelID); |
| 421 | |
| 422 | if (Flags & MTRF_Comment && !UseLineComment) |
| 423 | OS << "*/"; |
| 424 | |
| 425 | if (Flags & MTRF_JumpTarget) { |
| 426 | if (Flags & MTRF_Comment) |
| 427 | OS << " "; |
| 428 | OS << Table.getLabelIndex(LabelID); |
| 429 | } |
| 430 | |
| 431 | if (Flags & MTRF_CommaFollows) { |
| 432 | OS << ","; |
| 433 | if (!LineBreakIsNextAfterThis && !(Flags & MTRF_LineBreakFollows)) |
| 434 | OS << " "; |
| 435 | } |
| 436 | |
| 437 | if (Flags & MTRF_LineBreakFollows) |
| 438 | OS << "\n"; |
| 439 | } |
| 440 | |
| 441 | MatchTable &operator<<(MatchTable &Table, const MatchTableRecord &Value) { |
| 442 | Table.push_back(Value); |
| 443 | return Table; |
| 444 | } |
| 445 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 446 | //===- Matchers -----------------------------------------------------------===// |
| 447 | |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 448 | class OperandMatcher; |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 449 | class MatchAction; |
| 450 | |
| 451 | /// Generates code to check that a match rule matches. |
| 452 | class RuleMatcher { |
| 453 | /// A list of matchers that all need to succeed for the current rule to match. |
| 454 | /// FIXME: This currently supports a single match position but could be |
| 455 | /// extended to support multiple positions to support div/rem fusion or |
| 456 | /// load-multiple instructions. |
| 457 | std::vector<std::unique_ptr<InstructionMatcher>> Matchers; |
| 458 | |
| 459 | /// A list of actions that need to be taken when all predicates in this rule |
| 460 | /// have succeeded. |
| 461 | std::vector<std::unique_ptr<MatchAction>> Actions; |
| 462 | |
Daniel Sanders | 078572b | 2017-08-02 11:03:36 +0000 | [diff] [blame] | 463 | typedef std::map<const InstructionMatcher *, unsigned> |
| 464 | DefinedInsnVariablesMap; |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 465 | /// A map of instruction matchers to the local variables created by |
Daniel Sanders | 9d662d2 | 2017-07-06 10:06:12 +0000 | [diff] [blame] | 466 | /// emitCaptureOpcodes(). |
Daniel Sanders | 078572b | 2017-08-02 11:03:36 +0000 | [diff] [blame] | 467 | DefinedInsnVariablesMap InsnVariableIDs; |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 468 | |
| 469 | /// ID for the next instruction variable defined with defineInsnVar() |
| 470 | unsigned NextInsnVarID; |
| 471 | |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 472 | std::vector<Record *> RequiredFeatures; |
| 473 | |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 474 | public: |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 475 | RuleMatcher() |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 476 | : Matchers(), Actions(), InsnVariableIDs(), NextInsnVarID(0) {} |
Zachary Turner | b7dbd87 | 2017-03-20 19:56:52 +0000 | [diff] [blame] | 477 | RuleMatcher(RuleMatcher &&Other) = default; |
| 478 | RuleMatcher &operator=(RuleMatcher &&Other) = default; |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 479 | |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 480 | InstructionMatcher &addInstructionMatcher(StringRef SymbolicName); |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 481 | void addRequiredFeature(Record *Feature); |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 482 | const std::vector<Record *> &getRequiredFeatures() const; |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 483 | |
| 484 | template <class Kind, class... Args> Kind &addAction(Args &&... args); |
| 485 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 486 | /// Define an instruction without emitting any code to do so. |
| 487 | /// This is used for the root of the match. |
| 488 | unsigned implicitlyDefineInsnVar(const InstructionMatcher &Matcher); |
| 489 | /// Define an instruction and emit corresponding state-machine opcodes. |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 490 | unsigned defineInsnVar(MatchTable &Table, const InstructionMatcher &Matcher, |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 491 | unsigned InsnVarID, unsigned OpIdx); |
| 492 | unsigned getInsnVarID(const InstructionMatcher &InsnMatcher) const; |
Daniel Sanders | 078572b | 2017-08-02 11:03:36 +0000 | [diff] [blame] | 493 | DefinedInsnVariablesMap::const_iterator defined_insn_vars_begin() const { |
| 494 | return InsnVariableIDs.begin(); |
| 495 | } |
| 496 | DefinedInsnVariablesMap::const_iterator defined_insn_vars_end() const { |
| 497 | return InsnVariableIDs.end(); |
| 498 | } |
| 499 | iterator_range<typename DefinedInsnVariablesMap::const_iterator> |
| 500 | defined_insn_vars() const { |
| 501 | return make_range(defined_insn_vars_begin(), defined_insn_vars_end()); |
| 502 | } |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 503 | |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 504 | const InstructionMatcher &getInstructionMatcher(StringRef SymbolicName) const; |
| 505 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 506 | void emitCaptureOpcodes(MatchTable &Table); |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 507 | |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 508 | void emit(MatchTable &Table); |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 509 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 510 | /// Compare the priority of this object and B. |
| 511 | /// |
| 512 | /// Returns true if this object is more important than B. |
| 513 | bool isHigherPriorityThan(const RuleMatcher &B) const; |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 514 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 515 | /// Report the maximum number of temporary operands needed by the rule |
| 516 | /// matcher. |
| 517 | unsigned countRendererFns() const; |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 518 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 519 | // FIXME: Remove this as soon as possible |
| 520 | InstructionMatcher &insnmatcher_front() const { return *Matchers.front(); } |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 521 | }; |
| 522 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 523 | template <class PredicateTy> class PredicateListMatcher { |
| 524 | private: |
| 525 | typedef std::vector<std::unique_ptr<PredicateTy>> PredicateVec; |
| 526 | PredicateVec Predicates; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 527 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 528 | public: |
| 529 | /// Construct a new operand predicate and add it to the matcher. |
| 530 | template <class Kind, class... Args> |
| 531 | Kind &addPredicate(Args&&... args) { |
Ahmed Bougacha | b67a3ce | 2017-01-26 22:07:37 +0000 | [diff] [blame] | 532 | Predicates.emplace_back( |
| 533 | llvm::make_unique<Kind>(std::forward<Args>(args)...)); |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 534 | return *static_cast<Kind *>(Predicates.back().get()); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 535 | } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 536 | |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 537 | typename PredicateVec::const_iterator predicates_begin() const { |
| 538 | return Predicates.begin(); |
| 539 | } |
| 540 | typename PredicateVec::const_iterator predicates_end() const { |
| 541 | return Predicates.end(); |
| 542 | } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 543 | iterator_range<typename PredicateVec::const_iterator> predicates() const { |
| 544 | return make_range(predicates_begin(), predicates_end()); |
| 545 | } |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 546 | typename PredicateVec::size_type predicates_size() const { |
| 547 | return Predicates.size(); |
| 548 | } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 549 | |
Daniel Sanders | 9d662d2 | 2017-07-06 10:06:12 +0000 | [diff] [blame] | 550 | /// Emit MatchTable opcodes that tests whether all the predicates are met. |
Ahmed Bougacha | b67a3ce | 2017-01-26 22:07:37 +0000 | [diff] [blame] | 551 | template <class... Args> |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 552 | void emitPredicateListOpcodes(MatchTable &Table, Args &&... args) const { |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 553 | if (Predicates.empty()) { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 554 | Table << MatchTable::Comment("No predicates") << MatchTable::LineBreak; |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 555 | return; |
| 556 | } |
| 557 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 558 | for (const auto &Predicate : predicates()) |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 559 | Predicate->emitPredicateOpcodes(Table, std::forward<Args>(args)...); |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 560 | } |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 561 | }; |
| 562 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 563 | /// Generates code to check a predicate of an operand. |
| 564 | /// |
| 565 | /// Typical predicates include: |
| 566 | /// * Operand is a particular register. |
| 567 | /// * Operand is assigned a particular register bank. |
| 568 | /// * Operand is an MBB. |
| 569 | class OperandPredicateMatcher { |
| 570 | public: |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 571 | /// This enum is used for RTTI and also defines the priority that is given to |
| 572 | /// the predicate when generating the matcher code. Kinds with higher priority |
| 573 | /// must be tested first. |
| 574 | /// |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 575 | /// The relative priority of OPM_LLT, OPM_RegBank, and OPM_MBB do not matter |
| 576 | /// but OPM_Int must have priority over OPM_RegBank since constant integers |
| 577 | /// are represented by a virtual register defined by a G_CONSTANT instruction. |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 578 | enum PredicateKind { |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 579 | OPM_ComplexPattern, |
Daniel Sanders | fe12c0f | 2017-07-11 08:57:29 +0000 | [diff] [blame] | 580 | OPM_IntrinsicID, |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 581 | OPM_Instruction, |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 582 | OPM_Int, |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 583 | OPM_LiteralInt, |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 584 | OPM_LLT, |
| 585 | OPM_RegBank, |
| 586 | OPM_MBB, |
| 587 | }; |
| 588 | |
| 589 | protected: |
| 590 | PredicateKind Kind; |
| 591 | |
| 592 | public: |
| 593 | OperandPredicateMatcher(PredicateKind Kind) : Kind(Kind) {} |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 594 | virtual ~OperandPredicateMatcher() {} |
| 595 | |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 596 | PredicateKind getKind() const { return Kind; } |
| 597 | |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 598 | /// Return the OperandMatcher for the specified operand or nullptr if there |
| 599 | /// isn't one by that name in this operand predicate matcher. |
| 600 | /// |
| 601 | /// InstructionOperandMatcher is the only subclass that can return non-null |
| 602 | /// for this. |
| 603 | virtual Optional<const OperandMatcher *> |
Daniel Sanders | db7ed37 | 2017-04-04 13:52:00 +0000 | [diff] [blame] | 604 | getOptionalOperand(StringRef SymbolicName) const { |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 605 | assert(!SymbolicName.empty() && "Cannot lookup unnamed operand"); |
| 606 | return None; |
| 607 | } |
| 608 | |
Daniel Sanders | 9d662d2 | 2017-07-06 10:06:12 +0000 | [diff] [blame] | 609 | /// Emit MatchTable opcodes to capture instructions into the MIs table. |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 610 | /// |
Daniel Sanders | 9d662d2 | 2017-07-06 10:06:12 +0000 | [diff] [blame] | 611 | /// Only InstructionOperandMatcher needs to do anything for this method the |
| 612 | /// rest just walk the tree. |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 613 | virtual void emitCaptureOpcodes(MatchTable &Table, RuleMatcher &Rule, |
Daniel Sanders | 9d662d2 | 2017-07-06 10:06:12 +0000 | [diff] [blame] | 614 | unsigned InsnVarID, unsigned OpIdx) const {} |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 615 | |
Daniel Sanders | 9d662d2 | 2017-07-06 10:06:12 +0000 | [diff] [blame] | 616 | /// Emit MatchTable opcodes that check the predicate for the given operand. |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 617 | virtual void emitPredicateOpcodes(MatchTable &Table, RuleMatcher &Rule, |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 618 | unsigned InsnVarID, |
| 619 | unsigned OpIdx) const = 0; |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 620 | |
| 621 | /// Compare the priority of this object and B. |
| 622 | /// |
| 623 | /// Returns true if this object is more important than B. |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 624 | virtual bool isHigherPriorityThan(const OperandPredicateMatcher &B) const; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 625 | |
| 626 | /// Report the maximum number of temporary operands needed by the predicate |
| 627 | /// matcher. |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 628 | virtual unsigned countRendererFns() const { return 0; } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 629 | }; |
| 630 | |
| 631 | /// Generates code to check that an operand is a particular LLT. |
| 632 | class LLTOperandMatcher : public OperandPredicateMatcher { |
| 633 | protected: |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 634 | LLTCodeGen Ty; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 635 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 636 | public: |
Daniel Sanders | 52b4ce7 | 2017-03-07 23:20:35 +0000 | [diff] [blame] | 637 | LLTOperandMatcher(const LLTCodeGen &Ty) |
Daniel Sanders | eb2f5f3 | 2017-08-15 15:10:31 +0000 | [diff] [blame^] | 638 | : OperandPredicateMatcher(OPM_LLT), Ty(Ty) {} |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 639 | |
| 640 | static bool classof(const OperandPredicateMatcher *P) { |
| 641 | return P->getKind() == OPM_LLT; |
| 642 | } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 643 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 644 | void emitPredicateOpcodes(MatchTable &Table, RuleMatcher &Rule, |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 645 | unsigned InsnVarID, unsigned OpIdx) const override { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 646 | Table << MatchTable::Opcode("GIM_CheckType") << MatchTable::Comment("MI") |
| 647 | << MatchTable::IntValue(InsnVarID) << MatchTable::Comment("Op") |
| 648 | << MatchTable::IntValue(OpIdx) << MatchTable::Comment("Type") |
| 649 | << MatchTable::NamedValue(Ty.getCxxEnumValue()) |
| 650 | << MatchTable::LineBreak; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 651 | } |
| 652 | }; |
| 653 | |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 654 | /// Generates code to check that an operand is a particular target constant. |
| 655 | class ComplexPatternOperandMatcher : public OperandPredicateMatcher { |
| 656 | protected: |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 657 | const OperandMatcher &Operand; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 658 | const Record &TheDef; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 659 | |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 660 | unsigned getAllocatedTemporariesBaseID() const; |
| 661 | |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 662 | public: |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 663 | ComplexPatternOperandMatcher(const OperandMatcher &Operand, |
| 664 | const Record &TheDef) |
| 665 | : OperandPredicateMatcher(OPM_ComplexPattern), Operand(Operand), |
| 666 | TheDef(TheDef) {} |
| 667 | |
| 668 | static bool classof(const OperandPredicateMatcher *P) { |
| 669 | return P->getKind() == OPM_ComplexPattern; |
| 670 | } |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 671 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 672 | void emitPredicateOpcodes(MatchTable &Table, RuleMatcher &Rule, |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 673 | unsigned InsnVarID, unsigned OpIdx) const override { |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 674 | unsigned ID = getAllocatedTemporariesBaseID(); |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 675 | Table << MatchTable::Opcode("GIM_CheckComplexPattern") |
| 676 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 677 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) |
| 678 | << MatchTable::Comment("Renderer") << MatchTable::IntValue(ID) |
| 679 | << MatchTable::NamedValue(("GICP_" + TheDef.getName()).str()) |
| 680 | << MatchTable::LineBreak; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 683 | unsigned countRendererFns() const override { |
| 684 | return 1; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 685 | } |
| 686 | }; |
| 687 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 688 | /// Generates code to check that an operand is in a particular register bank. |
| 689 | class RegisterBankOperandMatcher : public OperandPredicateMatcher { |
| 690 | protected: |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 691 | const CodeGenRegisterClass &RC; |
| 692 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 693 | public: |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 694 | RegisterBankOperandMatcher(const CodeGenRegisterClass &RC) |
| 695 | : OperandPredicateMatcher(OPM_RegBank), RC(RC) {} |
| 696 | |
| 697 | static bool classof(const OperandPredicateMatcher *P) { |
| 698 | return P->getKind() == OPM_RegBank; |
| 699 | } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 700 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 701 | void emitPredicateOpcodes(MatchTable &Table, RuleMatcher &Rule, |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 702 | unsigned InsnVarID, unsigned OpIdx) const override { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 703 | Table << MatchTable::Opcode("GIM_CheckRegBankForClass") |
| 704 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 705 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) |
| 706 | << MatchTable::Comment("RC") |
| 707 | << MatchTable::NamedValue(RC.getQualifiedName() + "RegClassID") |
| 708 | << MatchTable::LineBreak; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 709 | } |
| 710 | }; |
| 711 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 712 | /// Generates code to check that an operand is a basic block. |
| 713 | class MBBOperandMatcher : public OperandPredicateMatcher { |
| 714 | public: |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 715 | MBBOperandMatcher() : OperandPredicateMatcher(OPM_MBB) {} |
| 716 | |
| 717 | static bool classof(const OperandPredicateMatcher *P) { |
| 718 | return P->getKind() == OPM_MBB; |
| 719 | } |
| 720 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 721 | void emitPredicateOpcodes(MatchTable &Table, RuleMatcher &Rule, |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 722 | unsigned InsnVarID, unsigned OpIdx) const override { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 723 | Table << MatchTable::Opcode("GIM_CheckIsMBB") << MatchTable::Comment("MI") |
| 724 | << MatchTable::IntValue(InsnVarID) << MatchTable::Comment("Op") |
| 725 | << MatchTable::IntValue(OpIdx) << MatchTable::LineBreak; |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 726 | } |
| 727 | }; |
| 728 | |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 729 | /// Generates code to check that an operand is a G_CONSTANT with a particular |
| 730 | /// int. |
| 731 | class ConstantIntOperandMatcher : public OperandPredicateMatcher { |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 732 | protected: |
| 733 | int64_t Value; |
| 734 | |
| 735 | public: |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 736 | ConstantIntOperandMatcher(int64_t Value) |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 737 | : OperandPredicateMatcher(OPM_Int), Value(Value) {} |
| 738 | |
| 739 | static bool classof(const OperandPredicateMatcher *P) { |
| 740 | return P->getKind() == OPM_Int; |
| 741 | } |
| 742 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 743 | void emitPredicateOpcodes(MatchTable &Table, RuleMatcher &Rule, |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 744 | unsigned InsnVarID, unsigned OpIdx) const override { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 745 | Table << MatchTable::Opcode("GIM_CheckConstantInt") |
| 746 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 747 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) |
| 748 | << MatchTable::IntValue(Value) << MatchTable::LineBreak; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 749 | } |
| 750 | }; |
| 751 | |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 752 | /// Generates code to check that an operand is a raw int (where MO.isImm() or |
| 753 | /// MO.isCImm() is true). |
| 754 | class LiteralIntOperandMatcher : public OperandPredicateMatcher { |
| 755 | protected: |
| 756 | int64_t Value; |
| 757 | |
| 758 | public: |
| 759 | LiteralIntOperandMatcher(int64_t Value) |
| 760 | : OperandPredicateMatcher(OPM_LiteralInt), Value(Value) {} |
| 761 | |
| 762 | static bool classof(const OperandPredicateMatcher *P) { |
| 763 | return P->getKind() == OPM_LiteralInt; |
| 764 | } |
| 765 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 766 | void emitPredicateOpcodes(MatchTable &Table, RuleMatcher &Rule, |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 767 | unsigned InsnVarID, unsigned OpIdx) const override { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 768 | Table << MatchTable::Opcode("GIM_CheckLiteralInt") |
| 769 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 770 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) |
| 771 | << MatchTable::IntValue(Value) << MatchTable::LineBreak; |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 772 | } |
| 773 | }; |
| 774 | |
Daniel Sanders | fe12c0f | 2017-07-11 08:57:29 +0000 | [diff] [blame] | 775 | /// Generates code to check that an operand is an intrinsic ID. |
| 776 | class IntrinsicIDOperandMatcher : public OperandPredicateMatcher { |
| 777 | protected: |
| 778 | const CodeGenIntrinsic *II; |
| 779 | |
| 780 | public: |
| 781 | IntrinsicIDOperandMatcher(const CodeGenIntrinsic *II) |
| 782 | : OperandPredicateMatcher(OPM_IntrinsicID), II(II) {} |
| 783 | |
| 784 | static bool classof(const OperandPredicateMatcher *P) { |
| 785 | return P->getKind() == OPM_IntrinsicID; |
| 786 | } |
| 787 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 788 | void emitPredicateOpcodes(MatchTable &Table, RuleMatcher &Rule, |
Daniel Sanders | fe12c0f | 2017-07-11 08:57:29 +0000 | [diff] [blame] | 789 | unsigned InsnVarID, unsigned OpIdx) const override { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 790 | Table << MatchTable::Opcode("GIM_CheckIntrinsicID") |
| 791 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) |
| 792 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) |
| 793 | << MatchTable::NamedValue("Intrinsic::" + II->EnumName) |
| 794 | << MatchTable::LineBreak; |
Daniel Sanders | fe12c0f | 2017-07-11 08:57:29 +0000 | [diff] [blame] | 795 | } |
| 796 | }; |
| 797 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 798 | /// Generates code to check that a set of predicates match for a particular |
| 799 | /// operand. |
| 800 | class OperandMatcher : public PredicateListMatcher<OperandPredicateMatcher> { |
| 801 | protected: |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 802 | InstructionMatcher &Insn; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 803 | unsigned OpIdx; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 804 | std::string SymbolicName; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 805 | |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 806 | /// The index of the first temporary variable allocated to this operand. The |
| 807 | /// number of allocated temporaries can be found with |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 808 | /// countRendererFns(). |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 809 | unsigned AllocatedTemporariesBaseID; |
| 810 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 811 | public: |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 812 | OperandMatcher(InstructionMatcher &Insn, unsigned OpIdx, |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 813 | const std::string &SymbolicName, |
| 814 | unsigned AllocatedTemporariesBaseID) |
| 815 | : Insn(Insn), OpIdx(OpIdx), SymbolicName(SymbolicName), |
| 816 | AllocatedTemporariesBaseID(AllocatedTemporariesBaseID) {} |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 817 | |
| 818 | bool hasSymbolicName() const { return !SymbolicName.empty(); } |
| 819 | const StringRef getSymbolicName() const { return SymbolicName; } |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 820 | void setSymbolicName(StringRef Name) { |
| 821 | assert(SymbolicName.empty() && "Operand already has a symbolic name"); |
| 822 | SymbolicName = Name; |
| 823 | } |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 824 | unsigned getOperandIndex() const { return OpIdx; } |
| 825 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 826 | std::string getOperandExpr(unsigned InsnVarID) const { |
| 827 | return "State.MIs[" + llvm::to_string(InsnVarID) + "]->getOperand(" + |
| 828 | llvm::to_string(OpIdx) + ")"; |
Daniel Sanders | e604ef5 | 2017-02-20 15:30:43 +0000 | [diff] [blame] | 829 | } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 830 | |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 831 | Optional<const OperandMatcher *> |
| 832 | getOptionalOperand(StringRef DesiredSymbolicName) const { |
| 833 | assert(!DesiredSymbolicName.empty() && "Cannot lookup unnamed operand"); |
| 834 | if (DesiredSymbolicName == SymbolicName) |
| 835 | return this; |
| 836 | for (const auto &OP : predicates()) { |
| 837 | const auto &MaybeOperand = OP->getOptionalOperand(DesiredSymbolicName); |
| 838 | if (MaybeOperand.hasValue()) |
| 839 | return MaybeOperand.getValue(); |
| 840 | } |
| 841 | return None; |
| 842 | } |
| 843 | |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 844 | InstructionMatcher &getInstructionMatcher() const { return Insn; } |
| 845 | |
Daniel Sanders | 9d662d2 | 2017-07-06 10:06:12 +0000 | [diff] [blame] | 846 | /// Emit MatchTable opcodes to capture instructions into the MIs table. |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 847 | void emitCaptureOpcodes(MatchTable &Table, RuleMatcher &Rule, |
Daniel Sanders | 9d662d2 | 2017-07-06 10:06:12 +0000 | [diff] [blame] | 848 | unsigned InsnVarID) const { |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 849 | for (const auto &Predicate : predicates()) |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 850 | Predicate->emitCaptureOpcodes(Table, Rule, InsnVarID, OpIdx); |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 851 | } |
| 852 | |
Daniel Sanders | 9d662d2 | 2017-07-06 10:06:12 +0000 | [diff] [blame] | 853 | /// Emit MatchTable opcodes that test whether the instruction named in |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 854 | /// InsnVarID matches all the predicates and all the operands. |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 855 | void emitPredicateOpcodes(MatchTable &Table, RuleMatcher &Rule, |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 856 | unsigned InsnVarID) const { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 857 | std::string Comment; |
| 858 | raw_string_ostream CommentOS(Comment); |
| 859 | CommentOS << "MIs[" << InsnVarID << "] "; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 860 | if (SymbolicName.empty()) |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 861 | CommentOS << "Operand " << OpIdx; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 862 | else |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 863 | CommentOS << SymbolicName; |
| 864 | Table << MatchTable::Comment(CommentOS.str()) << MatchTable::LineBreak; |
| 865 | |
| 866 | emitPredicateListOpcodes(Table, Rule, InsnVarID, OpIdx); |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 867 | } |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 868 | |
| 869 | /// Compare the priority of this object and B. |
| 870 | /// |
| 871 | /// Returns true if this object is more important than B. |
| 872 | bool isHigherPriorityThan(const OperandMatcher &B) const { |
| 873 | // Operand matchers involving more predicates have higher priority. |
| 874 | if (predicates_size() > B.predicates_size()) |
| 875 | return true; |
| 876 | if (predicates_size() < B.predicates_size()) |
| 877 | return false; |
| 878 | |
| 879 | // This assumes that predicates are added in a consistent order. |
| 880 | for (const auto &Predicate : zip(predicates(), B.predicates())) { |
| 881 | if (std::get<0>(Predicate)->isHigherPriorityThan(*std::get<1>(Predicate))) |
| 882 | return true; |
| 883 | if (std::get<1>(Predicate)->isHigherPriorityThan(*std::get<0>(Predicate))) |
| 884 | return false; |
| 885 | } |
| 886 | |
| 887 | return false; |
| 888 | }; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 889 | |
| 890 | /// Report the maximum number of temporary operands needed by the operand |
| 891 | /// matcher. |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 892 | unsigned countRendererFns() const { |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 893 | return std::accumulate( |
| 894 | predicates().begin(), predicates().end(), 0, |
| 895 | [](unsigned A, |
| 896 | const std::unique_ptr<OperandPredicateMatcher> &Predicate) { |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 897 | return A + Predicate->countRendererFns(); |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 898 | }); |
| 899 | } |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 900 | |
| 901 | unsigned getAllocatedTemporariesBaseID() const { |
| 902 | return AllocatedTemporariesBaseID; |
| 903 | } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 904 | }; |
| 905 | |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 906 | unsigned ComplexPatternOperandMatcher::getAllocatedTemporariesBaseID() const { |
| 907 | return Operand.getAllocatedTemporariesBaseID(); |
| 908 | } |
| 909 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 910 | /// Generates code to check a predicate on an instruction. |
| 911 | /// |
| 912 | /// Typical predicates include: |
| 913 | /// * The opcode of the instruction is a particular value. |
| 914 | /// * The nsw/nuw flag is/isn't set. |
| 915 | class InstructionPredicateMatcher { |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 916 | protected: |
| 917 | /// This enum is used for RTTI and also defines the priority that is given to |
| 918 | /// the predicate when generating the matcher code. Kinds with higher priority |
| 919 | /// must be tested first. |
| 920 | enum PredicateKind { |
| 921 | IPM_Opcode, |
| 922 | }; |
| 923 | |
| 924 | PredicateKind Kind; |
| 925 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 926 | public: |
Daniel Sanders | 8d4d72f | 2017-02-24 14:53:35 +0000 | [diff] [blame] | 927 | InstructionPredicateMatcher(PredicateKind Kind) : Kind(Kind) {} |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 928 | virtual ~InstructionPredicateMatcher() {} |
| 929 | |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 930 | PredicateKind getKind() const { return Kind; } |
| 931 | |
Daniel Sanders | 9d662d2 | 2017-07-06 10:06:12 +0000 | [diff] [blame] | 932 | /// Emit MatchTable opcodes that test whether the instruction named in |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 933 | /// InsnVarID matches the predicate. |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 934 | virtual void emitPredicateOpcodes(MatchTable &Table, RuleMatcher &Rule, |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 935 | unsigned InsnVarID) const = 0; |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 936 | |
| 937 | /// Compare the priority of this object and B. |
| 938 | /// |
| 939 | /// Returns true if this object is more important than B. |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 940 | virtual bool |
| 941 | isHigherPriorityThan(const InstructionPredicateMatcher &B) const { |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 942 | return Kind < B.Kind; |
| 943 | }; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 944 | |
| 945 | /// Report the maximum number of temporary operands needed by the predicate |
| 946 | /// matcher. |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 947 | virtual unsigned countRendererFns() const { return 0; } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 948 | }; |
| 949 | |
| 950 | /// Generates code to check the opcode of an instruction. |
| 951 | class InstructionOpcodeMatcher : public InstructionPredicateMatcher { |
| 952 | protected: |
| 953 | const CodeGenInstruction *I; |
| 954 | |
| 955 | public: |
Daniel Sanders | 8d4d72f | 2017-02-24 14:53:35 +0000 | [diff] [blame] | 956 | InstructionOpcodeMatcher(const CodeGenInstruction *I) |
| 957 | : InstructionPredicateMatcher(IPM_Opcode), I(I) {} |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 958 | |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 959 | static bool classof(const InstructionPredicateMatcher *P) { |
| 960 | return P->getKind() == IPM_Opcode; |
| 961 | } |
| 962 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 963 | void emitPredicateOpcodes(MatchTable &Table, RuleMatcher &Rule, |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 964 | unsigned InsnVarID) const override { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 965 | Table << MatchTable::Opcode("GIM_CheckOpcode") << MatchTable::Comment("MI") |
| 966 | << MatchTable::IntValue(InsnVarID) |
| 967 | << MatchTable::NamedValue(I->Namespace, I->TheDef->getName()) |
| 968 | << MatchTable::LineBreak; |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 969 | } |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 970 | |
| 971 | /// Compare the priority of this object and B. |
| 972 | /// |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 973 | /// Returns true if this object is more important than B. |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 974 | bool |
| 975 | isHigherPriorityThan(const InstructionPredicateMatcher &B) const override { |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 976 | if (InstructionPredicateMatcher::isHigherPriorityThan(B)) |
| 977 | return true; |
| 978 | if (B.InstructionPredicateMatcher::isHigherPriorityThan(*this)) |
| 979 | return false; |
| 980 | |
| 981 | // Prioritize opcodes for cosmetic reasons in the generated source. Although |
| 982 | // this is cosmetic at the moment, we may want to drive a similar ordering |
| 983 | // using instruction frequency information to improve compile time. |
| 984 | if (const InstructionOpcodeMatcher *BO = |
| 985 | dyn_cast<InstructionOpcodeMatcher>(&B)) |
| 986 | return I->TheDef->getName() < BO->I->TheDef->getName(); |
| 987 | |
| 988 | return false; |
| 989 | }; |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 990 | |
| 991 | bool isConstantInstruction() const { |
| 992 | return I->TheDef->getName() == "G_CONSTANT"; |
| 993 | } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 994 | }; |
| 995 | |
| 996 | /// Generates code to check that a set of predicates and operands match for a |
| 997 | /// particular instruction. |
| 998 | /// |
| 999 | /// Typical predicates include: |
| 1000 | /// * Has a specific opcode. |
| 1001 | /// * Has an nsw/nuw flag or doesn't. |
| 1002 | class InstructionMatcher |
| 1003 | : public PredicateListMatcher<InstructionPredicateMatcher> { |
| 1004 | protected: |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1005 | typedef std::vector<std::unique_ptr<OperandMatcher>> OperandVec; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1006 | |
| 1007 | /// The operands to match. All rendered operands must be present even if the |
| 1008 | /// condition is always true. |
| 1009 | OperandVec Operands; |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1010 | |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 1011 | std::string SymbolicName; |
| 1012 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1013 | public: |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 1014 | InstructionMatcher(StringRef SymbolicName) : SymbolicName(SymbolicName) {} |
| 1015 | |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1016 | /// Add an operand to the matcher. |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1017 | OperandMatcher &addOperand(unsigned OpIdx, const std::string &SymbolicName, |
| 1018 | unsigned AllocatedTemporariesBaseID) { |
| 1019 | Operands.emplace_back(new OperandMatcher(*this, OpIdx, SymbolicName, |
| 1020 | AllocatedTemporariesBaseID)); |
| 1021 | return *Operands.back(); |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 1024 | OperandMatcher &getOperand(unsigned OpIdx) { |
| 1025 | auto I = std::find_if(Operands.begin(), Operands.end(), |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1026 | [&OpIdx](const std::unique_ptr<OperandMatcher> &X) { |
| 1027 | return X->getOperandIndex() == OpIdx; |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 1028 | }); |
| 1029 | if (I != Operands.end()) |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1030 | return **I; |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 1031 | llvm_unreachable("Failed to lookup operand"); |
| 1032 | } |
| 1033 | |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 1034 | Optional<const OperandMatcher *> |
| 1035 | getOptionalOperand(StringRef SymbolicName) const { |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1036 | assert(!SymbolicName.empty() && "Cannot lookup unnamed operand"); |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 1037 | for (const auto &Operand : Operands) { |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1038 | const auto &OM = Operand->getOptionalOperand(SymbolicName); |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 1039 | if (OM.hasValue()) |
| 1040 | return OM.getValue(); |
| 1041 | } |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 1042 | return None; |
| 1043 | } |
| 1044 | |
Daniel Sanders | db7ed37 | 2017-04-04 13:52:00 +0000 | [diff] [blame] | 1045 | const OperandMatcher &getOperand(StringRef SymbolicName) const { |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 1046 | Optional<const OperandMatcher *>OM = getOptionalOperand(SymbolicName); |
| 1047 | if (OM.hasValue()) |
| 1048 | return *OM.getValue(); |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1049 | llvm_unreachable("Failed to lookup operand"); |
| 1050 | } |
| 1051 | |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 1052 | StringRef getSymbolicName() const { return SymbolicName; } |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1053 | unsigned getNumOperands() const { return Operands.size(); } |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 1054 | OperandVec::iterator operands_begin() { return Operands.begin(); } |
| 1055 | OperandVec::iterator operands_end() { return Operands.end(); } |
| 1056 | iterator_range<OperandVec::iterator> operands() { |
| 1057 | return make_range(operands_begin(), operands_end()); |
| 1058 | } |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 1059 | OperandVec::const_iterator operands_begin() const { return Operands.begin(); } |
| 1060 | OperandVec::const_iterator operands_end() const { return Operands.end(); } |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1061 | iterator_range<OperandVec::const_iterator> operands() const { |
| 1062 | return make_range(operands_begin(), operands_end()); |
| 1063 | } |
| 1064 | |
Daniel Sanders | 9d662d2 | 2017-07-06 10:06:12 +0000 | [diff] [blame] | 1065 | /// Emit MatchTable opcodes to check the shape of the match and capture |
| 1066 | /// instructions into the MIs table. |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1067 | void emitCaptureOpcodes(MatchTable &Table, RuleMatcher &Rule, |
Daniel Sanders | 9d662d2 | 2017-07-06 10:06:12 +0000 | [diff] [blame] | 1068 | unsigned InsnID) { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1069 | Table << MatchTable::Opcode("GIM_CheckNumOperands") |
| 1070 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnID) |
| 1071 | << MatchTable::Comment("Expected") |
| 1072 | << MatchTable::IntValue(getNumOperands()) << MatchTable::LineBreak; |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 1073 | for (const auto &Operand : Operands) |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1074 | Operand->emitCaptureOpcodes(Table, Rule, InsnID); |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
Daniel Sanders | 9d662d2 | 2017-07-06 10:06:12 +0000 | [diff] [blame] | 1077 | /// Emit MatchTable opcodes that test whether the instruction named in |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1078 | /// InsnVarName matches all the predicates and all the operands. |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1079 | void emitPredicateOpcodes(MatchTable &Table, RuleMatcher &Rule, |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 1080 | unsigned InsnVarID) const { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1081 | emitPredicateListOpcodes(Table, Rule, InsnVarID); |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 1082 | for (const auto &Operand : Operands) |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1083 | Operand->emitPredicateOpcodes(Table, Rule, InsnVarID); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1084 | } |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1085 | |
| 1086 | /// Compare the priority of this object and B. |
| 1087 | /// |
| 1088 | /// Returns true if this object is more important than B. |
| 1089 | bool isHigherPriorityThan(const InstructionMatcher &B) const { |
| 1090 | // Instruction matchers involving more operands have higher priority. |
| 1091 | if (Operands.size() > B.Operands.size()) |
| 1092 | return true; |
| 1093 | if (Operands.size() < B.Operands.size()) |
| 1094 | return false; |
| 1095 | |
| 1096 | for (const auto &Predicate : zip(predicates(), B.predicates())) { |
| 1097 | if (std::get<0>(Predicate)->isHigherPriorityThan(*std::get<1>(Predicate))) |
| 1098 | return true; |
| 1099 | if (std::get<1>(Predicate)->isHigherPriorityThan(*std::get<0>(Predicate))) |
| 1100 | return false; |
| 1101 | } |
| 1102 | |
| 1103 | for (const auto &Operand : zip(Operands, B.Operands)) { |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1104 | if (std::get<0>(Operand)->isHigherPriorityThan(*std::get<1>(Operand))) |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1105 | return true; |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1106 | if (std::get<1>(Operand)->isHigherPriorityThan(*std::get<0>(Operand))) |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1107 | return false; |
| 1108 | } |
| 1109 | |
| 1110 | return false; |
| 1111 | }; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1112 | |
| 1113 | /// Report the maximum number of temporary operands needed by the instruction |
| 1114 | /// matcher. |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 1115 | unsigned countRendererFns() const { |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1116 | return std::accumulate(predicates().begin(), predicates().end(), 0, |
| 1117 | [](unsigned A, |
| 1118 | const std::unique_ptr<InstructionPredicateMatcher> |
| 1119 | &Predicate) { |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 1120 | return A + Predicate->countRendererFns(); |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1121 | }) + |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1122 | std::accumulate( |
| 1123 | Operands.begin(), Operands.end(), 0, |
| 1124 | [](unsigned A, const std::unique_ptr<OperandMatcher> &Operand) { |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 1125 | return A + Operand->countRendererFns(); |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1126 | }); |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1127 | } |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 1128 | |
| 1129 | bool isConstantInstruction() const { |
| 1130 | for (const auto &P : predicates()) |
| 1131 | if (const InstructionOpcodeMatcher *Opcode = |
| 1132 | dyn_cast<InstructionOpcodeMatcher>(P.get())) |
| 1133 | return Opcode->isConstantInstruction(); |
| 1134 | return false; |
| 1135 | } |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1136 | }; |
| 1137 | |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 1138 | /// Generates code to check that the operand is a register defined by an |
| 1139 | /// instruction that matches the given instruction matcher. |
| 1140 | /// |
| 1141 | /// For example, the pattern: |
| 1142 | /// (set $dst, (G_MUL (G_ADD $src1, $src2), $src3)) |
| 1143 | /// would use an InstructionOperandMatcher for operand 1 of the G_MUL to match |
| 1144 | /// the: |
| 1145 | /// (G_ADD $src1, $src2) |
| 1146 | /// subpattern. |
| 1147 | class InstructionOperandMatcher : public OperandPredicateMatcher { |
| 1148 | protected: |
| 1149 | std::unique_ptr<InstructionMatcher> InsnMatcher; |
| 1150 | |
| 1151 | public: |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 1152 | InstructionOperandMatcher(StringRef SymbolicName) |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 1153 | : OperandPredicateMatcher(OPM_Instruction), |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 1154 | InsnMatcher(new InstructionMatcher(SymbolicName)) {} |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 1155 | |
| 1156 | static bool classof(const OperandPredicateMatcher *P) { |
| 1157 | return P->getKind() == OPM_Instruction; |
| 1158 | } |
| 1159 | |
| 1160 | InstructionMatcher &getInsnMatcher() const { return *InsnMatcher; } |
| 1161 | |
| 1162 | Optional<const OperandMatcher *> |
| 1163 | getOptionalOperand(StringRef SymbolicName) const override { |
| 1164 | assert(!SymbolicName.empty() && "Cannot lookup unnamed operand"); |
| 1165 | return InsnMatcher->getOptionalOperand(SymbolicName); |
| 1166 | } |
| 1167 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1168 | void emitCaptureOpcodes(MatchTable &Table, RuleMatcher &Rule, |
Daniel Sanders | 9d662d2 | 2017-07-06 10:06:12 +0000 | [diff] [blame] | 1169 | unsigned InsnID, unsigned OpIdx) const override { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1170 | unsigned InsnVarID = Rule.defineInsnVar(Table, *InsnMatcher, InsnID, OpIdx); |
| 1171 | InsnMatcher->emitCaptureOpcodes(Table, Rule, InsnVarID); |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1174 | void emitPredicateOpcodes(MatchTable &Table, RuleMatcher &Rule, |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 1175 | unsigned InsnVarID_, |
| 1176 | unsigned OpIdx_) const override { |
| 1177 | unsigned InsnVarID = Rule.getInsnVarID(*InsnMatcher); |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1178 | InsnMatcher->emitPredicateOpcodes(Table, Rule, InsnVarID); |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 1179 | } |
| 1180 | }; |
| 1181 | |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 1182 | //===- Actions ------------------------------------------------------------===// |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1183 | class OperandRenderer { |
| 1184 | public: |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 1185 | enum RendererKind { |
| 1186 | OR_Copy, |
| 1187 | OR_CopySubReg, |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 1188 | OR_CopyConstantAsImm, |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 1189 | OR_Imm, |
| 1190 | OR_Register, |
| 1191 | OR_ComplexPattern |
| 1192 | }; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1193 | |
| 1194 | protected: |
| 1195 | RendererKind Kind; |
| 1196 | |
| 1197 | public: |
| 1198 | OperandRenderer(RendererKind Kind) : Kind(Kind) {} |
| 1199 | virtual ~OperandRenderer() {} |
| 1200 | |
| 1201 | RendererKind getKind() const { return Kind; } |
| 1202 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1203 | virtual void emitRenderOpcodes(MatchTable &Table, |
| 1204 | RuleMatcher &Rule) const = 0; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1205 | }; |
| 1206 | |
| 1207 | /// A CopyRenderer emits code to copy a single operand from an existing |
| 1208 | /// instruction to the one being built. |
| 1209 | class CopyRenderer : public OperandRenderer { |
| 1210 | protected: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 1211 | unsigned NewInsnID; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1212 | /// The matcher for the instruction that this operand is copied from. |
| 1213 | /// This provides the facility for looking up an a operand by it's name so |
| 1214 | /// that it can be used as a source for the instruction being built. |
| 1215 | const InstructionMatcher &Matched; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1216 | /// The name of the operand. |
| 1217 | const StringRef SymbolicName; |
| 1218 | |
| 1219 | public: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 1220 | CopyRenderer(unsigned NewInsnID, const InstructionMatcher &Matched, |
| 1221 | StringRef SymbolicName) |
| 1222 | : OperandRenderer(OR_Copy), NewInsnID(NewInsnID), Matched(Matched), |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 1223 | SymbolicName(SymbolicName) { |
| 1224 | assert(!SymbolicName.empty() && "Cannot copy from an unspecified source"); |
| 1225 | } |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1226 | |
| 1227 | static bool classof(const OperandRenderer *R) { |
| 1228 | return R->getKind() == OR_Copy; |
| 1229 | } |
| 1230 | |
| 1231 | const StringRef getSymbolicName() const { return SymbolicName; } |
| 1232 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1233 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 1234 | const OperandMatcher &Operand = Matched.getOperand(SymbolicName); |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 1235 | unsigned OldInsnVarID = Rule.getInsnVarID(Operand.getInstructionMatcher()); |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1236 | Table << MatchTable::Opcode("GIR_Copy") << MatchTable::Comment("NewInsnID") |
| 1237 | << MatchTable::IntValue(NewInsnID) << MatchTable::Comment("OldInsnID") |
| 1238 | << MatchTable::IntValue(OldInsnVarID) << MatchTable::Comment("OpIdx") |
| 1239 | << MatchTable::IntValue(Operand.getOperandIndex()) |
| 1240 | << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1241 | } |
| 1242 | }; |
| 1243 | |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 1244 | /// A CopyConstantAsImmRenderer emits code to render a G_CONSTANT instruction to |
| 1245 | /// an extended immediate operand. |
| 1246 | class CopyConstantAsImmRenderer : public OperandRenderer { |
| 1247 | protected: |
| 1248 | unsigned NewInsnID; |
| 1249 | /// The name of the operand. |
| 1250 | const std::string SymbolicName; |
| 1251 | bool Signed; |
| 1252 | |
| 1253 | public: |
| 1254 | CopyConstantAsImmRenderer(unsigned NewInsnID, StringRef SymbolicName) |
| 1255 | : OperandRenderer(OR_CopyConstantAsImm), NewInsnID(NewInsnID), |
| 1256 | SymbolicName(SymbolicName), Signed(true) {} |
| 1257 | |
| 1258 | static bool classof(const OperandRenderer *R) { |
| 1259 | return R->getKind() == OR_CopyConstantAsImm; |
| 1260 | } |
| 1261 | |
| 1262 | const StringRef getSymbolicName() const { return SymbolicName; } |
| 1263 | |
| 1264 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
| 1265 | const InstructionMatcher &InsnMatcher = Rule.getInstructionMatcher(SymbolicName); |
| 1266 | unsigned OldInsnVarID = Rule.getInsnVarID(InsnMatcher); |
| 1267 | Table << MatchTable::Opcode(Signed ? "GIR_CopyConstantAsSImm" |
| 1268 | : "GIR_CopyConstantAsUImm") |
| 1269 | << MatchTable::Comment("NewInsnID") << MatchTable::IntValue(NewInsnID) |
| 1270 | << MatchTable::Comment("OldInsnID") |
| 1271 | << MatchTable::IntValue(OldInsnVarID) |
| 1272 | << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak; |
| 1273 | } |
| 1274 | }; |
| 1275 | |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 1276 | /// A CopySubRegRenderer emits code to copy a single register operand from an |
| 1277 | /// existing instruction to the one being built and indicate that only a |
| 1278 | /// subregister should be copied. |
| 1279 | class CopySubRegRenderer : public OperandRenderer { |
| 1280 | protected: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 1281 | unsigned NewInsnID; |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 1282 | /// The matcher for the instruction that this operand is copied from. |
| 1283 | /// This provides the facility for looking up an a operand by it's name so |
| 1284 | /// that it can be used as a source for the instruction being built. |
| 1285 | const InstructionMatcher &Matched; |
| 1286 | /// The name of the operand. |
| 1287 | const StringRef SymbolicName; |
| 1288 | /// The subregister to extract. |
| 1289 | const CodeGenSubRegIndex *SubReg; |
| 1290 | |
| 1291 | public: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 1292 | CopySubRegRenderer(unsigned NewInsnID, const InstructionMatcher &Matched, |
| 1293 | StringRef SymbolicName, const CodeGenSubRegIndex *SubReg) |
| 1294 | : OperandRenderer(OR_CopySubReg), NewInsnID(NewInsnID), Matched(Matched), |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 1295 | SymbolicName(SymbolicName), SubReg(SubReg) {} |
| 1296 | |
| 1297 | static bool classof(const OperandRenderer *R) { |
| 1298 | return R->getKind() == OR_CopySubReg; |
| 1299 | } |
| 1300 | |
| 1301 | const StringRef getSymbolicName() const { return SymbolicName; } |
| 1302 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1303 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 1304 | const OperandMatcher &Operand = Matched.getOperand(SymbolicName); |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 1305 | unsigned OldInsnVarID = Rule.getInsnVarID(Operand.getInstructionMatcher()); |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1306 | Table << MatchTable::Opcode("GIR_CopySubReg") |
| 1307 | << MatchTable::Comment("NewInsnID") << MatchTable::IntValue(NewInsnID) |
| 1308 | << MatchTable::Comment("OldInsnID") |
| 1309 | << MatchTable::IntValue(OldInsnVarID) << MatchTable::Comment("OpIdx") |
| 1310 | << MatchTable::IntValue(Operand.getOperandIndex()) |
| 1311 | << MatchTable::Comment("SubRegIdx") |
| 1312 | << MatchTable::IntValue(SubReg->EnumValue) |
| 1313 | << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak; |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 1314 | } |
| 1315 | }; |
| 1316 | |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1317 | /// Adds a specific physical register to the instruction being built. |
| 1318 | /// This is typically useful for WZR/XZR on AArch64. |
| 1319 | class AddRegisterRenderer : public OperandRenderer { |
| 1320 | protected: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 1321 | unsigned InsnID; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1322 | const Record *RegisterDef; |
| 1323 | |
| 1324 | public: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 1325 | AddRegisterRenderer(unsigned InsnID, const Record *RegisterDef) |
| 1326 | : OperandRenderer(OR_Register), InsnID(InsnID), RegisterDef(RegisterDef) { |
| 1327 | } |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1328 | |
| 1329 | static bool classof(const OperandRenderer *R) { |
| 1330 | return R->getKind() == OR_Register; |
| 1331 | } |
| 1332 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1333 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
| 1334 | Table << MatchTable::Opcode("GIR_AddRegister") |
| 1335 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 1336 | << MatchTable::NamedValue( |
| 1337 | (RegisterDef->getValue("Namespace") |
| 1338 | ? RegisterDef->getValueAsString("Namespace") |
| 1339 | : ""), |
| 1340 | RegisterDef->getName()) |
| 1341 | << MatchTable::LineBreak; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1342 | } |
| 1343 | }; |
| 1344 | |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 1345 | /// Adds a specific immediate to the instruction being built. |
| 1346 | class ImmRenderer : public OperandRenderer { |
| 1347 | protected: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 1348 | unsigned InsnID; |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 1349 | int64_t Imm; |
| 1350 | |
| 1351 | public: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 1352 | ImmRenderer(unsigned InsnID, int64_t Imm) |
| 1353 | : OperandRenderer(OR_Imm), InsnID(InsnID), Imm(Imm) {} |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 1354 | |
| 1355 | static bool classof(const OperandRenderer *R) { |
| 1356 | return R->getKind() == OR_Imm; |
| 1357 | } |
| 1358 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1359 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
| 1360 | Table << MatchTable::Opcode("GIR_AddImm") << MatchTable::Comment("InsnID") |
| 1361 | << MatchTable::IntValue(InsnID) << MatchTable::Comment("Imm") |
| 1362 | << MatchTable::IntValue(Imm) << MatchTable::LineBreak; |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 1363 | } |
| 1364 | }; |
| 1365 | |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 1366 | /// Adds operands by calling a renderer function supplied by the ComplexPattern |
| 1367 | /// matcher function. |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1368 | class RenderComplexPatternOperand : public OperandRenderer { |
| 1369 | private: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 1370 | unsigned InsnID; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1371 | const Record &TheDef; |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 1372 | /// The name of the operand. |
| 1373 | const StringRef SymbolicName; |
| 1374 | /// The renderer number. This must be unique within a rule since it's used to |
| 1375 | /// identify a temporary variable to hold the renderer function. |
| 1376 | unsigned RendererID; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1377 | |
| 1378 | unsigned getNumOperands() const { |
| 1379 | return TheDef.getValueAsDag("Operands")->getNumArgs(); |
| 1380 | } |
| 1381 | |
| 1382 | public: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 1383 | RenderComplexPatternOperand(unsigned InsnID, const Record &TheDef, |
| 1384 | StringRef SymbolicName, unsigned RendererID) |
| 1385 | : OperandRenderer(OR_ComplexPattern), InsnID(InsnID), TheDef(TheDef), |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 1386 | SymbolicName(SymbolicName), RendererID(RendererID) {} |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1387 | |
| 1388 | static bool classof(const OperandRenderer *R) { |
| 1389 | return R->getKind() == OR_ComplexPattern; |
| 1390 | } |
| 1391 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1392 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { |
| 1393 | Table << MatchTable::Opcode("GIR_ComplexRenderer") |
| 1394 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 1395 | << MatchTable::Comment("RendererID") |
| 1396 | << MatchTable::IntValue(RendererID) << MatchTable::LineBreak; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1397 | } |
| 1398 | }; |
| 1399 | |
Ahmed Bougacha | 56ca3a9 | 2017-02-04 00:47:10 +0000 | [diff] [blame] | 1400 | /// An action taken when all Matcher predicates succeeded for a parent rule. |
| 1401 | /// |
| 1402 | /// Typical actions include: |
| 1403 | /// * Changing the opcode of an instruction. |
| 1404 | /// * Adding an operand to an instruction. |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 1405 | class MatchAction { |
| 1406 | public: |
| 1407 | virtual ~MatchAction() {} |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1408 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1409 | /// Emit the MatchTable opcodes to implement the action. |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1410 | /// |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 1411 | /// \param RecycleInsnID If given, it's an instruction to recycle. The |
| 1412 | /// requirements on the instruction vary from action to |
| 1413 | /// action. |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1414 | virtual void emitActionOpcodes(MatchTable &Table, RuleMatcher &Rule, |
| 1415 | unsigned RecycleInsnID) const = 0; |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 1416 | }; |
| 1417 | |
Ahmed Bougacha | 9aa4c10 | 2017-02-04 00:47:08 +0000 | [diff] [blame] | 1418 | /// Generates a comment describing the matched rule being acted upon. |
| 1419 | class DebugCommentAction : public MatchAction { |
| 1420 | private: |
| 1421 | const PatternToMatch &P; |
| 1422 | |
| 1423 | public: |
| 1424 | DebugCommentAction(const PatternToMatch &P) : P(P) {} |
| 1425 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1426 | void emitActionOpcodes(MatchTable &Table, RuleMatcher &Rule, |
| 1427 | unsigned RecycleInsnID) const override { |
| 1428 | Table << MatchTable::Comment(llvm::to_string(*P.getSrcPattern()) + " => " + |
| 1429 | llvm::to_string(*P.getDstPattern())) |
| 1430 | << MatchTable::LineBreak; |
Ahmed Bougacha | 9aa4c10 | 2017-02-04 00:47:08 +0000 | [diff] [blame] | 1431 | } |
| 1432 | }; |
| 1433 | |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1434 | /// Generates code to build an instruction or mutate an existing instruction |
| 1435 | /// into the desired instruction when this is possible. |
| 1436 | class BuildMIAction : public MatchAction { |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 1437 | private: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 1438 | unsigned InsnID; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1439 | const CodeGenInstruction *I; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1440 | const InstructionMatcher &Matched; |
| 1441 | std::vector<std::unique_ptr<OperandRenderer>> OperandRenderers; |
| 1442 | |
| 1443 | /// True if the instruction can be built solely by mutating the opcode. |
| 1444 | bool canMutate() const { |
Daniel Sanders | e9fdba3 | 2017-04-29 17:30:09 +0000 | [diff] [blame] | 1445 | if (OperandRenderers.size() != Matched.getNumOperands()) |
| 1446 | return false; |
| 1447 | |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1448 | for (const auto &Renderer : enumerate(OperandRenderers)) { |
Zachary Turner | 309a088 | 2017-03-13 16:24:10 +0000 | [diff] [blame] | 1449 | if (const auto *Copy = dyn_cast<CopyRenderer>(&*Renderer.value())) { |
Daniel Sanders | 3016d3c | 2017-04-22 14:31:28 +0000 | [diff] [blame] | 1450 | const OperandMatcher &OM = Matched.getOperand(Copy->getSymbolicName()); |
| 1451 | if (&Matched != &OM.getInstructionMatcher() || |
| 1452 | OM.getOperandIndex() != Renderer.index()) |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1453 | return false; |
| 1454 | } else |
| 1455 | return false; |
| 1456 | } |
| 1457 | |
| 1458 | return true; |
| 1459 | } |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1460 | |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 1461 | public: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 1462 | BuildMIAction(unsigned InsnID, const CodeGenInstruction *I, |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 1463 | const InstructionMatcher &Matched) |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 1464 | : InsnID(InsnID), I(I), Matched(Matched) {} |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 1465 | |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1466 | template <class Kind, class... Args> |
| 1467 | Kind &addRenderer(Args&&... args) { |
| 1468 | OperandRenderers.emplace_back( |
| 1469 | llvm::make_unique<Kind>(std::forward<Args>(args)...)); |
| 1470 | return *static_cast<Kind *>(OperandRenderers.back().get()); |
| 1471 | } |
| 1472 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1473 | void emitActionOpcodes(MatchTable &Table, RuleMatcher &Rule, |
| 1474 | unsigned RecycleInsnID) const override { |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1475 | if (canMutate()) { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1476 | Table << MatchTable::Opcode("GIR_MutateOpcode") |
| 1477 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 1478 | << MatchTable::Comment("RecycleInsnID") |
| 1479 | << MatchTable::IntValue(RecycleInsnID) |
| 1480 | << MatchTable::Comment("Opcode") |
| 1481 | << MatchTable::NamedValue(I->Namespace, I->TheDef->getName()) |
| 1482 | << MatchTable::LineBreak; |
Tim Northover | 4340d64 | 2017-03-20 21:58:23 +0000 | [diff] [blame] | 1483 | |
| 1484 | if (!I->ImplicitDefs.empty() || !I->ImplicitUses.empty()) { |
Tim Northover | 4340d64 | 2017-03-20 21:58:23 +0000 | [diff] [blame] | 1485 | for (auto Def : I->ImplicitDefs) { |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 1486 | auto Namespace = Def->getValue("Namespace") |
| 1487 | ? Def->getValueAsString("Namespace") |
| 1488 | : ""; |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1489 | Table << MatchTable::Opcode("GIR_AddImplicitDef") |
| 1490 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 1491 | << MatchTable::NamedValue(Namespace, Def->getName()) |
| 1492 | << MatchTable::LineBreak; |
Tim Northover | 4340d64 | 2017-03-20 21:58:23 +0000 | [diff] [blame] | 1493 | } |
| 1494 | for (auto Use : I->ImplicitUses) { |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 1495 | auto Namespace = Use->getValue("Namespace") |
| 1496 | ? Use->getValueAsString("Namespace") |
| 1497 | : ""; |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1498 | Table << MatchTable::Opcode("GIR_AddImplicitUse") |
| 1499 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 1500 | << MatchTable::NamedValue(Namespace, Use->getName()) |
| 1501 | << MatchTable::LineBreak; |
Tim Northover | 4340d64 | 2017-03-20 21:58:23 +0000 | [diff] [blame] | 1502 | } |
| 1503 | } |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1504 | return; |
| 1505 | } |
| 1506 | |
| 1507 | // TODO: Simple permutation looks like it could be almost as common as |
| 1508 | // mutation due to commutative operations. |
| 1509 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1510 | Table << MatchTable::Opcode("GIR_BuildMI") << MatchTable::Comment("InsnID") |
| 1511 | << MatchTable::IntValue(InsnID) << MatchTable::Comment("Opcode") |
| 1512 | << MatchTable::NamedValue(I->Namespace, I->TheDef->getName()) |
| 1513 | << MatchTable::LineBreak; |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 1514 | for (const auto &Renderer : OperandRenderers) |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1515 | Renderer->emitRenderOpcodes(Table, Rule); |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 1516 | |
Florian Hahn | 3bc3ec6 | 2017-08-03 14:48:22 +0000 | [diff] [blame] | 1517 | if (I->mayLoad || I->mayStore) { |
| 1518 | Table << MatchTable::Opcode("GIR_MergeMemOperands") |
| 1519 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 1520 | << MatchTable::Comment("MergeInsnID's"); |
| 1521 | // Emit the ID's for all the instructions that are matched by this rule. |
| 1522 | // TODO: Limit this to matched instructions that mayLoad/mayStore or have |
| 1523 | // some other means of having a memoperand. Also limit this to |
| 1524 | // emitted instructions that expect to have a memoperand too. For |
| 1525 | // example, (G_SEXT (G_LOAD x)) that results in separate load and |
| 1526 | // sign-extend instructions shouldn't put the memoperand on the |
| 1527 | // sign-extend since it has no effect there. |
| 1528 | std::vector<unsigned> MergeInsnIDs; |
| 1529 | for (const auto &IDMatcherPair : Rule.defined_insn_vars()) |
| 1530 | MergeInsnIDs.push_back(IDMatcherPair.second); |
| 1531 | std::sort(MergeInsnIDs.begin(), MergeInsnIDs.end()); |
| 1532 | for (const auto &MergeInsnID : MergeInsnIDs) |
| 1533 | Table << MatchTable::IntValue(MergeInsnID); |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 1534 | Table << MatchTable::NamedValue("GIU_MergeMemOperands_EndOfList") |
| 1535 | << MatchTable::LineBreak; |
Florian Hahn | 3bc3ec6 | 2017-08-03 14:48:22 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
| 1538 | Table << MatchTable::Opcode("GIR_EraseFromParent") |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1539 | << MatchTable::Comment("InsnID") |
| 1540 | << MatchTable::IntValue(RecycleInsnID) << MatchTable::LineBreak; |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 1541 | } |
| 1542 | }; |
| 1543 | |
| 1544 | /// Generates code to constrain the operands of an output instruction to the |
| 1545 | /// register classes specified by the definition of that instruction. |
| 1546 | class ConstrainOperandsToDefinitionAction : public MatchAction { |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 1547 | unsigned InsnID; |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 1548 | |
| 1549 | public: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 1550 | ConstrainOperandsToDefinitionAction(unsigned InsnID) : InsnID(InsnID) {} |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 1551 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1552 | void emitActionOpcodes(MatchTable &Table, RuleMatcher &Rule, |
| 1553 | unsigned RecycleInsnID) const override { |
| 1554 | Table << MatchTable::Opcode("GIR_ConstrainSelectedInstOperands") |
| 1555 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 1556 | << MatchTable::LineBreak; |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 1557 | } |
| 1558 | }; |
| 1559 | |
| 1560 | /// Generates code to constrain the specified operand of an output instruction |
| 1561 | /// to the specified register class. |
| 1562 | class ConstrainOperandToRegClassAction : public MatchAction { |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 1563 | unsigned InsnID; |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 1564 | unsigned OpIdx; |
| 1565 | const CodeGenRegisterClass &RC; |
| 1566 | |
| 1567 | public: |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 1568 | ConstrainOperandToRegClassAction(unsigned InsnID, unsigned OpIdx, |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 1569 | const CodeGenRegisterClass &RC) |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 1570 | : InsnID(InsnID), OpIdx(OpIdx), RC(RC) {} |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 1571 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1572 | void emitActionOpcodes(MatchTable &Table, RuleMatcher &Rule, |
| 1573 | unsigned RecycleInsnID) const override { |
| 1574 | Table << MatchTable::Opcode("GIR_ConstrainOperandRC") |
| 1575 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 1576 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) |
| 1577 | << MatchTable::Comment("RC " + RC.getName()) |
| 1578 | << MatchTable::IntValue(RC.EnumValue) << MatchTable::LineBreak; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1579 | } |
| 1580 | }; |
| 1581 | |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 1582 | InstructionMatcher &RuleMatcher::addInstructionMatcher(StringRef SymbolicName) { |
| 1583 | Matchers.emplace_back(new InstructionMatcher(SymbolicName)); |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 1584 | return *Matchers.back(); |
| 1585 | } |
Ahmed Bougacha | 56ca3a9 | 2017-02-04 00:47:10 +0000 | [diff] [blame] | 1586 | |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 1587 | void RuleMatcher::addRequiredFeature(Record *Feature) { |
| 1588 | RequiredFeatures.push_back(Feature); |
| 1589 | } |
| 1590 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 1591 | const std::vector<Record *> &RuleMatcher::getRequiredFeatures() const { |
| 1592 | return RequiredFeatures; |
| 1593 | } |
| 1594 | |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 1595 | template <class Kind, class... Args> |
| 1596 | Kind &RuleMatcher::addAction(Args &&... args) { |
| 1597 | Actions.emplace_back(llvm::make_unique<Kind>(std::forward<Args>(args)...)); |
| 1598 | return *static_cast<Kind *>(Actions.back().get()); |
| 1599 | } |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1600 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 1601 | unsigned |
| 1602 | RuleMatcher::implicitlyDefineInsnVar(const InstructionMatcher &Matcher) { |
| 1603 | unsigned NewInsnVarID = NextInsnVarID++; |
| 1604 | InsnVariableIDs[&Matcher] = NewInsnVarID; |
| 1605 | return NewInsnVarID; |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1608 | unsigned RuleMatcher::defineInsnVar(MatchTable &Table, |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 1609 | const InstructionMatcher &Matcher, |
| 1610 | unsigned InsnID, unsigned OpIdx) { |
| 1611 | unsigned NewInsnVarID = implicitlyDefineInsnVar(Matcher); |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1612 | Table << MatchTable::Opcode("GIM_RecordInsn") |
| 1613 | << MatchTable::Comment("DefineMI") << MatchTable::IntValue(NewInsnVarID) |
| 1614 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnID) |
| 1615 | << MatchTable::Comment("OpIdx") << MatchTable::IntValue(OpIdx) |
| 1616 | << MatchTable::Comment("MIs[" + llvm::to_string(NewInsnVarID) + "]") |
| 1617 | << MatchTable::LineBreak; |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 1618 | return NewInsnVarID; |
| 1619 | } |
| 1620 | |
| 1621 | unsigned RuleMatcher::getInsnVarID(const InstructionMatcher &InsnMatcher) const { |
| 1622 | const auto &I = InsnVariableIDs.find(&InsnMatcher); |
| 1623 | if (I != InsnVariableIDs.end()) |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 1624 | return I->second; |
| 1625 | llvm_unreachable("Matched Insn was not captured in a local variable"); |
| 1626 | } |
| 1627 | |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 1628 | const InstructionMatcher & |
| 1629 | RuleMatcher::getInstructionMatcher(StringRef SymbolicName) const { |
| 1630 | for (const auto &I : InsnVariableIDs) |
| 1631 | if (I.first->getSymbolicName() == SymbolicName) |
| 1632 | return *I.first; |
| 1633 | llvm_unreachable( |
| 1634 | ("Failed to lookup instruction " + SymbolicName).str().c_str()); |
| 1635 | } |
| 1636 | |
Daniel Sanders | 9d662d2 | 2017-07-06 10:06:12 +0000 | [diff] [blame] | 1637 | /// Emit MatchTable opcodes to check the shape of the match and capture |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 1638 | /// instructions into local variables. |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1639 | void RuleMatcher::emitCaptureOpcodes(MatchTable &Table) { |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 1640 | assert(Matchers.size() == 1 && "Cannot handle multi-root matchers yet"); |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 1641 | unsigned InsnVarID = implicitlyDefineInsnVar(*Matchers.front()); |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1642 | Matchers.front()->emitCaptureOpcodes(Table, *this, InsnVarID); |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 1645 | void RuleMatcher::emit(MatchTable &Table) { |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 1646 | if (Matchers.empty()) |
| 1647 | llvm_unreachable("Unexpected empty matcher!"); |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 1648 | |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 1649 | // The representation supports rules that require multiple roots such as: |
| 1650 | // %ptr(p0) = ... |
| 1651 | // %elt0(s32) = G_LOAD %ptr |
| 1652 | // %1(p0) = G_ADD %ptr, 4 |
| 1653 | // %elt1(s32) = G_LOAD p0 %1 |
| 1654 | // which could be usefully folded into: |
| 1655 | // %ptr(p0) = ... |
| 1656 | // %elt0(s32), %elt1(s32) = TGT_LOAD_PAIR %ptr |
| 1657 | // on some targets but we don't need to make use of that yet. |
| 1658 | assert(Matchers.size() == 1 && "Cannot handle multi-root matchers yet"); |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 1659 | |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 1660 | unsigned LabelID = Table.allocateLabelID(); |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1661 | Table << MatchTable::Opcode("GIM_Try", +1) |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 1662 | << MatchTable::Comment("On fail goto") << MatchTable::JumpTarget(LabelID) |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1663 | << MatchTable::LineBreak; |
| 1664 | |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 1665 | if (!RequiredFeatures.empty()) { |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1666 | Table << MatchTable::Opcode("GIM_CheckFeatures") |
| 1667 | << MatchTable::NamedValue(getNameForFeatureBitset(RequiredFeatures)) |
| 1668 | << MatchTable::LineBreak; |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 1669 | } |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 1670 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1671 | emitCaptureOpcodes(Table); |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 1672 | |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1673 | Matchers.front()->emitPredicateOpcodes(Table, *this, |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 1674 | getInsnVarID(*Matchers.front())); |
| 1675 | |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 1676 | // 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] | 1677 | if (InsnVariableIDs.size() >= 2) { |
Galina Kistanova | 1754fee | 2017-05-25 01:51:53 +0000 | [diff] [blame] | 1678 | // Invert the map to create stable ordering (by var names) |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 1679 | SmallVector<unsigned, 2> InsnIDs; |
| 1680 | for (const auto &Pair : InsnVariableIDs) { |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 1681 | // Skip the root node since it isn't moving anywhere. Everything else is |
| 1682 | // sinking to meet it. |
| 1683 | if (Pair.first == Matchers.front().get()) |
| 1684 | continue; |
| 1685 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 1686 | InsnIDs.push_back(Pair.second); |
Galina Kistanova | 1754fee | 2017-05-25 01:51:53 +0000 | [diff] [blame] | 1687 | } |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 1688 | std::sort(InsnIDs.begin(), InsnIDs.end()); |
Galina Kistanova | 1754fee | 2017-05-25 01:51:53 +0000 | [diff] [blame] | 1689 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 1690 | for (const auto &InsnID : InsnIDs) { |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 1691 | // Reject the difficult cases until we have a more accurate check. |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1692 | Table << MatchTable::Opcode("GIM_CheckIsSafeToFold") |
| 1693 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) |
| 1694 | << MatchTable::LineBreak; |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 1695 | |
| 1696 | // FIXME: Emit checks to determine it's _actually_ safe to fold and/or |
| 1697 | // account for unsafe cases. |
| 1698 | // |
| 1699 | // Example: |
| 1700 | // MI1--> %0 = ... |
| 1701 | // %1 = ... %0 |
| 1702 | // MI0--> %2 = ... %0 |
| 1703 | // It's not safe to erase MI1. We currently handle this by not |
| 1704 | // erasing %0 (even when it's dead). |
| 1705 | // |
| 1706 | // Example: |
| 1707 | // MI1--> %0 = load volatile @a |
| 1708 | // %1 = load volatile @a |
| 1709 | // MI0--> %2 = ... %0 |
| 1710 | // It's not safe to sink %0's def past %1. We currently handle |
| 1711 | // this by rejecting all loads. |
| 1712 | // |
| 1713 | // Example: |
| 1714 | // MI1--> %0 = load @a |
| 1715 | // %1 = store @a |
| 1716 | // MI0--> %2 = ... %0 |
| 1717 | // It's not safe to sink %0's def past %1. We currently handle |
| 1718 | // this by rejecting all loads. |
| 1719 | // |
| 1720 | // Example: |
| 1721 | // G_CONDBR %cond, @BB1 |
| 1722 | // BB0: |
| 1723 | // MI1--> %0 = load @a |
| 1724 | // G_BR @BB1 |
| 1725 | // BB1: |
| 1726 | // MI0--> %2 = ... %0 |
| 1727 | // It's not always safe to sink %0 across control flow. In this |
| 1728 | // case it may introduce a memory fault. We currentl handle this |
| 1729 | // by rejecting all loads. |
| 1730 | } |
| 1731 | } |
| 1732 | |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 1733 | for (const auto &MA : Actions) |
Daniel Sanders | 7aac7cc | 2017-07-20 09:25:44 +0000 | [diff] [blame] | 1734 | MA->emitActionOpcodes(Table, *this, 0); |
| 1735 | Table << MatchTable::Opcode("GIR_Done", -1) << MatchTable::LineBreak |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 1736 | << MatchTable::Label(LabelID); |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 1737 | } |
Daniel Sanders | 43c882c | 2017-02-01 10:53:10 +0000 | [diff] [blame] | 1738 | |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 1739 | bool RuleMatcher::isHigherPriorityThan(const RuleMatcher &B) const { |
| 1740 | // Rules involving more match roots have higher priority. |
| 1741 | if (Matchers.size() > B.Matchers.size()) |
| 1742 | return true; |
| 1743 | if (Matchers.size() < B.Matchers.size()) |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 1744 | return false; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1745 | |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 1746 | for (const auto &Matcher : zip(Matchers, B.Matchers)) { |
| 1747 | if (std::get<0>(Matcher)->isHigherPriorityThan(*std::get<1>(Matcher))) |
| 1748 | return true; |
| 1749 | if (std::get<1>(Matcher)->isHigherPriorityThan(*std::get<0>(Matcher))) |
| 1750 | return false; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1751 | } |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 1752 | |
| 1753 | return false; |
Simon Pilgrim | a7d1da8 | 2017-03-15 22:50:47 +0000 | [diff] [blame] | 1754 | } |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1755 | |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 1756 | unsigned RuleMatcher::countRendererFns() const { |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 1757 | return std::accumulate( |
| 1758 | Matchers.begin(), Matchers.end(), 0, |
| 1759 | [](unsigned A, const std::unique_ptr<InstructionMatcher> &Matcher) { |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 1760 | return A + Matcher->countRendererFns(); |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 1761 | }); |
| 1762 | } |
| 1763 | |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 1764 | bool OperandPredicateMatcher::isHigherPriorityThan( |
| 1765 | const OperandPredicateMatcher &B) const { |
| 1766 | // Generally speaking, an instruction is more important than an Int or a |
| 1767 | // LiteralInt because it can cover more nodes but theres an exception to |
| 1768 | // this. G_CONSTANT's are less important than either of those two because they |
| 1769 | // are more permissive. |
| 1770 | if (const InstructionOperandMatcher *AOM = |
| 1771 | dyn_cast<InstructionOperandMatcher>(this)) { |
| 1772 | if (AOM->getInsnMatcher().isConstantInstruction()) { |
| 1773 | if (B.Kind == OPM_Int) { |
| 1774 | return false; |
| 1775 | } |
| 1776 | } |
| 1777 | } |
| 1778 | if (const InstructionOperandMatcher *BOM = |
| 1779 | dyn_cast<InstructionOperandMatcher>(&B)) { |
| 1780 | if (BOM->getInsnMatcher().isConstantInstruction()) { |
| 1781 | if (Kind == OPM_Int) { |
| 1782 | return true; |
| 1783 | } |
| 1784 | } |
| 1785 | } |
| 1786 | |
| 1787 | return Kind < B.Kind; |
Daniel Sanders | 75b84fc | 2017-08-08 13:21:26 +0000 | [diff] [blame] | 1788 | } |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 1789 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1790 | //===- GlobalISelEmitter class --------------------------------------------===// |
| 1791 | |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 1792 | class GlobalISelEmitter { |
| 1793 | public: |
| 1794 | explicit GlobalISelEmitter(RecordKeeper &RK); |
| 1795 | void run(raw_ostream &OS); |
| 1796 | |
| 1797 | private: |
| 1798 | const RecordKeeper &RK; |
| 1799 | const CodeGenDAGPatterns CGP; |
| 1800 | const CodeGenTarget &Target; |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 1801 | CodeGenRegBank CGRegs; |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 1802 | |
| 1803 | /// Keep track of the equivalence between SDNodes and Instruction. |
| 1804 | /// This is defined using 'GINodeEquiv' in the target description. |
| 1805 | DenseMap<Record *, const CodeGenInstruction *> NodeEquivs; |
| 1806 | |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1807 | /// Keep track of the equivalence between ComplexPattern's and |
| 1808 | /// GIComplexOperandMatcher. Map entries are specified by subclassing |
| 1809 | /// GIComplexPatternEquiv. |
| 1810 | DenseMap<const Record *, const Record *> ComplexPatternEquivs; |
| 1811 | |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 1812 | // Map of predicates to their subtarget features. |
Daniel Sanders | e9fdba3 | 2017-04-29 17:30:09 +0000 | [diff] [blame] | 1813 | SubtargetFeatureInfoMap SubtargetFeatures; |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 1814 | |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 1815 | void gatherNodeEquivs(); |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 1816 | const CodeGenInstruction *findNodeEquiv(Record *N) const; |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 1817 | |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 1818 | Error importRulePredicates(RuleMatcher &M, ArrayRef<Init *> Predicates); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 1819 | Expected<InstructionMatcher &> |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 1820 | createAndImportSelDAGMatcher(InstructionMatcher &InsnMatcher, |
Daniel Sanders | 57938df | 2017-07-11 10:40:18 +0000 | [diff] [blame] | 1821 | const TreePatternNode *Src, |
| 1822 | unsigned &TempOpIdx) const; |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 1823 | Error importChildMatcher(InstructionMatcher &InsnMatcher, |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 1824 | const TreePatternNode *SrcChild, unsigned OpIdx, |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 1825 | unsigned &TempOpIdx) const; |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 1826 | Expected<BuildMIAction &> |
| 1827 | createAndImportInstructionRenderer(RuleMatcher &M, const TreePatternNode *Dst, |
| 1828 | const InstructionMatcher &InsnMatcher); |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 1829 | Error importExplicitUseRenderer(BuildMIAction &DstMIBuilder, |
| 1830 | TreePatternNode *DstChild, |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1831 | const InstructionMatcher &InsnMatcher) const; |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 1832 | Error importDefaultOperandRenderers(BuildMIAction &DstMIBuilder, |
| 1833 | DagInit *DefaultOps) const; |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 1834 | Error |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 1835 | importImplicitDefRenderers(BuildMIAction &DstMIBuilder, |
| 1836 | const std::vector<Record *> &ImplicitDefs) const; |
| 1837 | |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 1838 | /// Analyze pattern \p P, returning a matcher for it if possible. |
| 1839 | /// Otherwise, return an Error explaining why we don't support it. |
| 1840 | Expected<RuleMatcher> runOnPattern(const PatternToMatch &P); |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 1841 | |
| 1842 | void declareSubtargetFeature(Record *Predicate); |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 1843 | }; |
| 1844 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1845 | void GlobalISelEmitter::gatherNodeEquivs() { |
| 1846 | assert(NodeEquivs.empty()); |
| 1847 | for (Record *Equiv : RK.getAllDerivedDefinitions("GINodeEquiv")) |
| 1848 | NodeEquivs[Equiv->getValueAsDef("Node")] = |
| 1849 | &Target.getInstruction(Equiv->getValueAsDef("I")); |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1850 | |
| 1851 | assert(ComplexPatternEquivs.empty()); |
| 1852 | for (Record *Equiv : RK.getAllDerivedDefinitions("GIComplexPatternEquiv")) { |
| 1853 | Record *SelDAGEquiv = Equiv->getValueAsDef("SelDAGEquivalent"); |
| 1854 | if (!SelDAGEquiv) |
| 1855 | continue; |
| 1856 | ComplexPatternEquivs[SelDAGEquiv] = Equiv; |
| 1857 | } |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1858 | } |
| 1859 | |
Daniel Sanders | bdfebb8 | 2017-03-15 20:18:38 +0000 | [diff] [blame] | 1860 | const CodeGenInstruction *GlobalISelEmitter::findNodeEquiv(Record *N) const { |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1861 | return NodeEquivs.lookup(N); |
| 1862 | } |
| 1863 | |
| 1864 | GlobalISelEmitter::GlobalISelEmitter(RecordKeeper &RK) |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 1865 | : RK(RK), CGP(RK), Target(CGP.getTargetInfo()), CGRegs(RK) {} |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 1866 | |
| 1867 | //===- Emitter ------------------------------------------------------------===// |
| 1868 | |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 1869 | Error |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 1870 | GlobalISelEmitter::importRulePredicates(RuleMatcher &M, |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 1871 | ArrayRef<Init *> Predicates) { |
| 1872 | for (const Init *Predicate : Predicates) { |
| 1873 | const DefInit *PredicateDef = static_cast<const DefInit *>(Predicate); |
| 1874 | declareSubtargetFeature(PredicateDef->getDef()); |
| 1875 | M.addRequiredFeature(PredicateDef->getDef()); |
| 1876 | } |
| 1877 | |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 1878 | return Error::success(); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 1879 | } |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 1880 | |
Daniel Sanders | 57938df | 2017-07-11 10:40:18 +0000 | [diff] [blame] | 1881 | Expected<InstructionMatcher &> |
| 1882 | GlobalISelEmitter::createAndImportSelDAGMatcher(InstructionMatcher &InsnMatcher, |
| 1883 | const TreePatternNode *Src, |
| 1884 | unsigned &TempOpIdx) const { |
Daniel Sanders | 85ffd36 | 2017-07-06 08:12:20 +0000 | [diff] [blame] | 1885 | const CodeGenInstruction *SrcGIOrNull = nullptr; |
| 1886 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 1887 | // Start with the defined operands (i.e., the results of the root operator). |
| 1888 | if (Src->getExtTypes().size() > 1) |
| 1889 | return failedImport("Src pattern has multiple results"); |
| 1890 | |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 1891 | if (Src->isLeaf()) { |
| 1892 | Init *SrcInit = Src->getLeafValue(); |
Daniel Sanders | 3334cc0 | 2017-05-23 20:02:48 +0000 | [diff] [blame] | 1893 | if (isa<IntInit>(SrcInit)) { |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 1894 | InsnMatcher.addPredicate<InstructionOpcodeMatcher>( |
| 1895 | &Target.getInstruction(RK.getDef("G_CONSTANT"))); |
| 1896 | } else |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 1897 | return failedImport( |
| 1898 | "Unable to deduce gMIR opcode to handle Src (which is a leaf)"); |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 1899 | } else { |
Daniel Sanders | 85ffd36 | 2017-07-06 08:12:20 +0000 | [diff] [blame] | 1900 | SrcGIOrNull = findNodeEquiv(Src->getOperator()); |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 1901 | if (!SrcGIOrNull) |
| 1902 | return failedImport("Pattern operator lacks an equivalent Instruction" + |
| 1903 | explainOperator(Src->getOperator())); |
| 1904 | auto &SrcGI = *SrcGIOrNull; |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 1905 | |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 1906 | // The operators look good: match the opcode |
| 1907 | InsnMatcher.addPredicate<InstructionOpcodeMatcher>(&SrcGI); |
| 1908 | } |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 1909 | |
| 1910 | unsigned OpIdx = 0; |
| 1911 | for (const EEVT::TypeSet &Ty : Src->getExtTypes()) { |
| 1912 | auto OpTyOrNone = MVTToLLT(Ty.getConcrete()); |
| 1913 | |
| 1914 | if (!OpTyOrNone) |
| 1915 | return failedImport( |
| 1916 | "Result of Src pattern operator has an unsupported type"); |
| 1917 | |
| 1918 | // Results don't have a name unless they are the root node. The caller will |
| 1919 | // set the name if appropriate. |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1920 | OperandMatcher &OM = InsnMatcher.addOperand(OpIdx++, "", TempOpIdx); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 1921 | OM.addPredicate<LLTOperandMatcher>(*OpTyOrNone); |
| 1922 | } |
| 1923 | |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 1924 | if (Src->isLeaf()) { |
| 1925 | Init *SrcInit = Src->getLeafValue(); |
| 1926 | if (IntInit *SrcIntInit = dyn_cast<IntInit>(SrcInit)) { |
| 1927 | OperandMatcher &OM = InsnMatcher.addOperand(OpIdx++, "", TempOpIdx); |
| 1928 | OM.addPredicate<LiteralIntOperandMatcher>(SrcIntInit->getValue()); |
| 1929 | } else |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 1930 | return failedImport( |
| 1931 | "Unable to deduce gMIR opcode to handle Src (which is a leaf)"); |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 1932 | } else { |
Daniel Sanders | 85ffd36 | 2017-07-06 08:12:20 +0000 | [diff] [blame] | 1933 | assert(SrcGIOrNull && |
| 1934 | "Expected to have already found an equivalent Instruction"); |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 1935 | if (SrcGIOrNull->TheDef->getName() == "G_CONSTANT") { |
| 1936 | // imm still has an operand but we don't need to do anything with it |
| 1937 | // here since we don't support ImmLeaf predicates yet. However, we still |
| 1938 | // need to note the hidden operand to get GIM_CheckNumOperands correct. |
| 1939 | InsnMatcher.addOperand(OpIdx++, "", TempOpIdx); |
| 1940 | return InsnMatcher; |
| 1941 | } |
| 1942 | |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 1943 | // Match the used operands (i.e. the children of the operator). |
| 1944 | for (unsigned i = 0, e = Src->getNumChildren(); i != e; ++i) { |
Daniel Sanders | 85ffd36 | 2017-07-06 08:12:20 +0000 | [diff] [blame] | 1945 | TreePatternNode *SrcChild = Src->getChild(i); |
| 1946 | |
| 1947 | // For G_INTRINSIC, the operand immediately following the defs is an |
| 1948 | // intrinsic ID. |
| 1949 | if (SrcGIOrNull->TheDef->getName() == "G_INTRINSIC" && i == 0) { |
Daniel Sanders | fe12c0f | 2017-07-11 08:57:29 +0000 | [diff] [blame] | 1950 | if (const CodeGenIntrinsic *II = Src->getIntrinsicInfo(CGP)) { |
Daniel Sanders | 85ffd36 | 2017-07-06 08:12:20 +0000 | [diff] [blame] | 1951 | OperandMatcher &OM = |
| 1952 | InsnMatcher.addOperand(OpIdx++, SrcChild->getName(), TempOpIdx); |
Daniel Sanders | fe12c0f | 2017-07-11 08:57:29 +0000 | [diff] [blame] | 1953 | OM.addPredicate<IntrinsicIDOperandMatcher>(II); |
Daniel Sanders | 85ffd36 | 2017-07-06 08:12:20 +0000 | [diff] [blame] | 1954 | continue; |
| 1955 | } |
| 1956 | |
| 1957 | return failedImport("Expected IntInit containing instrinsic ID)"); |
| 1958 | } |
| 1959 | |
| 1960 | if (auto Error = |
| 1961 | importChildMatcher(InsnMatcher, SrcChild, OpIdx++, TempOpIdx)) |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 1962 | return std::move(Error); |
| 1963 | } |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 1964 | } |
| 1965 | |
| 1966 | return InsnMatcher; |
| 1967 | } |
| 1968 | |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 1969 | Error GlobalISelEmitter::importChildMatcher(InstructionMatcher &InsnMatcher, |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 1970 | const TreePatternNode *SrcChild, |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 1971 | unsigned OpIdx, |
| 1972 | unsigned &TempOpIdx) const { |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 1973 | OperandMatcher &OM = |
| 1974 | InsnMatcher.addOperand(OpIdx, SrcChild->getName(), TempOpIdx); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 1975 | |
| 1976 | if (SrcChild->hasAnyPredicate()) |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 1977 | return failedImport("Src pattern child has predicate (" + |
| 1978 | explainPredicates(SrcChild) + ")"); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 1979 | |
| 1980 | ArrayRef<EEVT::TypeSet> ChildTypes = SrcChild->getExtTypes(); |
| 1981 | if (ChildTypes.size() != 1) |
| 1982 | return failedImport("Src pattern child has multiple results"); |
| 1983 | |
| 1984 | // Check MBB's before the type check since they are not a known type. |
| 1985 | if (!SrcChild->isLeaf()) { |
| 1986 | if (SrcChild->getOperator()->isSubClassOf("SDNode")) { |
| 1987 | auto &ChildSDNI = CGP.getSDNodeInfo(SrcChild->getOperator()); |
| 1988 | if (ChildSDNI.getSDClassName() == "BasicBlockSDNode") { |
| 1989 | OM.addPredicate<MBBOperandMatcher>(); |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 1990 | return Error::success(); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 1991 | } |
| 1992 | } |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 1993 | } |
| 1994 | |
| 1995 | auto OpTyOrNone = MVTToLLT(ChildTypes.front().getConcrete()); |
| 1996 | if (!OpTyOrNone) |
Daniel Sanders | 85ffd36 | 2017-07-06 08:12:20 +0000 | [diff] [blame] | 1997 | return failedImport("Src operand has an unsupported type (" + to_string(*SrcChild) + ")"); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 1998 | OM.addPredicate<LLTOperandMatcher>(*OpTyOrNone); |
| 1999 | |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 2000 | // Check for nested instructions. |
| 2001 | if (!SrcChild->isLeaf()) { |
| 2002 | // Map the node to a gMIR instruction. |
| 2003 | InstructionOperandMatcher &InsnOperand = |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2004 | OM.addPredicate<InstructionOperandMatcher>(SrcChild->getName()); |
Daniel Sanders | 57938df | 2017-07-11 10:40:18 +0000 | [diff] [blame] | 2005 | auto InsnMatcherOrError = createAndImportSelDAGMatcher( |
| 2006 | InsnOperand.getInsnMatcher(), SrcChild, TempOpIdx); |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 2007 | if (auto Error = InsnMatcherOrError.takeError()) |
| 2008 | return Error; |
| 2009 | |
| 2010 | return Error::success(); |
| 2011 | } |
| 2012 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2013 | // Check for constant immediates. |
| 2014 | if (auto *ChildInt = dyn_cast<IntInit>(SrcChild->getLeafValue())) { |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 2015 | OM.addPredicate<ConstantIntOperandMatcher>(ChildInt->getValue()); |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 2016 | return Error::success(); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2017 | } |
| 2018 | |
| 2019 | // Check for def's like register classes or ComplexPattern's. |
| 2020 | if (auto *ChildDefInit = dyn_cast<DefInit>(SrcChild->getLeafValue())) { |
| 2021 | auto *ChildRec = ChildDefInit->getDef(); |
| 2022 | |
| 2023 | // Check for register classes. |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2024 | if (ChildRec->isSubClassOf("RegisterClass") || |
| 2025 | ChildRec->isSubClassOf("RegisterOperand")) { |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2026 | OM.addPredicate<RegisterBankOperandMatcher>( |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2027 | Target.getRegisterClass(getInitValueAsRegClass(ChildDefInit))); |
Daniel Sanders | 658541f | 2017-04-22 15:53:21 +0000 | [diff] [blame] | 2028 | return Error::success(); |
| 2029 | } |
| 2030 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2031 | // Check for ComplexPattern's. |
| 2032 | if (ChildRec->isSubClassOf("ComplexPattern")) { |
| 2033 | const auto &ComplexPattern = ComplexPatternEquivs.find(ChildRec); |
| 2034 | if (ComplexPattern == ComplexPatternEquivs.end()) |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 2035 | return failedImport("SelectionDAG ComplexPattern (" + |
| 2036 | ChildRec->getName() + ") not mapped to GlobalISel"); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2037 | |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 2038 | OM.addPredicate<ComplexPatternOperandMatcher>(OM, |
| 2039 | *ComplexPattern->second); |
| 2040 | TempOpIdx++; |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 2041 | return Error::success(); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2042 | } |
| 2043 | |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 2044 | if (ChildRec->isSubClassOf("ImmLeaf")) { |
| 2045 | return failedImport( |
| 2046 | "Src pattern child def is an unsupported tablegen class (ImmLeaf)"); |
| 2047 | } |
| 2048 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2049 | return failedImport( |
| 2050 | "Src pattern child def is an unsupported tablegen class"); |
| 2051 | } |
| 2052 | |
| 2053 | return failedImport("Src pattern child is an unsupported kind"); |
| 2054 | } |
| 2055 | |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 2056 | Error GlobalISelEmitter::importExplicitUseRenderer( |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2057 | BuildMIAction &DstMIBuilder, TreePatternNode *DstChild, |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 2058 | const InstructionMatcher &InsnMatcher) const { |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2059 | if (!DstChild->isLeaf()) { |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2060 | // We accept 'bb' here. It's an operator because BasicBlockSDNode isn't |
| 2061 | // inline, but in MI it's just another operand. |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2062 | if (DstChild->getOperator()->isSubClassOf("SDNode")) { |
| 2063 | auto &ChildSDNI = CGP.getSDNodeInfo(DstChild->getOperator()); |
| 2064 | if (ChildSDNI.getSDClassName() == "BasicBlockSDNode") { |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2065 | DstMIBuilder.addRenderer<CopyRenderer>(0, InsnMatcher, |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2066 | DstChild->getName()); |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 2067 | return Error::success(); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2068 | } |
| 2069 | } |
Daniel Sanders | 0554004 | 2017-08-08 10:44:31 +0000 | [diff] [blame] | 2070 | |
| 2071 | // Similarly, imm is an operator in TreePatternNode's view but must be |
| 2072 | // rendered as operands. |
| 2073 | // FIXME: The target should be able to choose sign-extended when appropriate |
| 2074 | // (e.g. on Mips). |
| 2075 | if (DstChild->getOperator()->getName() == "imm") { |
| 2076 | DstMIBuilder.addRenderer<CopyConstantAsImmRenderer>(0, |
| 2077 | DstChild->getName()); |
| 2078 | return Error::success(); |
| 2079 | } |
| 2080 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2081 | return failedImport("Dst pattern child isn't a leaf node or an MBB"); |
| 2082 | } |
| 2083 | |
| 2084 | // Otherwise, we're looking for a bog-standard RegisterClass operand. |
| 2085 | if (DstChild->hasAnyPredicate()) |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 2086 | return failedImport("Dst pattern child has predicate (" + |
| 2087 | explainPredicates(DstChild) + ")"); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2088 | |
| 2089 | if (auto *ChildDefInit = dyn_cast<DefInit>(DstChild->getLeafValue())) { |
| 2090 | auto *ChildRec = ChildDefInit->getDef(); |
| 2091 | |
| 2092 | ArrayRef<EEVT::TypeSet> ChildTypes = DstChild->getExtTypes(); |
| 2093 | if (ChildTypes.size() != 1) |
| 2094 | return failedImport("Dst pattern child has multiple results"); |
| 2095 | |
| 2096 | auto OpTyOrNone = MVTToLLT(ChildTypes.front().getConcrete()); |
| 2097 | if (!OpTyOrNone) |
| 2098 | return failedImport("Dst operand has an unsupported type"); |
| 2099 | |
| 2100 | if (ChildRec->isSubClassOf("Register")) { |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2101 | DstMIBuilder.addRenderer<AddRegisterRenderer>(0, ChildRec); |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 2102 | return Error::success(); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2103 | } |
| 2104 | |
Daniel Sanders | 658541f | 2017-04-22 15:53:21 +0000 | [diff] [blame] | 2105 | if (ChildRec->isSubClassOf("RegisterClass") || |
| 2106 | ChildRec->isSubClassOf("RegisterOperand")) { |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2107 | DstMIBuilder.addRenderer<CopyRenderer>(0, InsnMatcher, |
| 2108 | DstChild->getName()); |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 2109 | return Error::success(); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2110 | } |
| 2111 | |
| 2112 | if (ChildRec->isSubClassOf("ComplexPattern")) { |
| 2113 | const auto &ComplexPattern = ComplexPatternEquivs.find(ChildRec); |
| 2114 | if (ComplexPattern == ComplexPatternEquivs.end()) |
| 2115 | return failedImport( |
| 2116 | "SelectionDAG ComplexPattern not mapped to GlobalISel"); |
| 2117 | |
Daniel Sanders | 4f3eb24 | 2017-04-05 13:14:03 +0000 | [diff] [blame] | 2118 | const OperandMatcher &OM = InsnMatcher.getOperand(DstChild->getName()); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2119 | DstMIBuilder.addRenderer<RenderComplexPatternOperand>( |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2120 | 0, *ComplexPattern->second, DstChild->getName(), |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 2121 | OM.getAllocatedTemporariesBaseID()); |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 2122 | return Error::success(); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2123 | } |
| 2124 | |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 2125 | if (ChildRec->isSubClassOf("SDNodeXForm")) |
| 2126 | return failedImport("Dst pattern child def is an unsupported tablegen " |
| 2127 | "class (SDNodeXForm)"); |
| 2128 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2129 | return failedImport( |
| 2130 | "Dst pattern child def is an unsupported tablegen class"); |
| 2131 | } |
| 2132 | |
| 2133 | return failedImport("Dst pattern child is an unsupported kind"); |
| 2134 | } |
| 2135 | |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 2136 | Expected<BuildMIAction &> GlobalISelEmitter::createAndImportInstructionRenderer( |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2137 | RuleMatcher &M, const TreePatternNode *Dst, |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2138 | const InstructionMatcher &InsnMatcher) { |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2139 | Record *DstOp = Dst->getOperator(); |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 2140 | if (!DstOp->isSubClassOf("Instruction")) { |
| 2141 | if (DstOp->isSubClassOf("ValueType")) |
| 2142 | return failedImport( |
| 2143 | "Pattern operator isn't an instruction (it's a ValueType)"); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2144 | return failedImport("Pattern operator isn't an instruction"); |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 2145 | } |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2146 | CodeGenInstruction *DstI = &Target.getInstruction(DstOp); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2147 | |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2148 | unsigned DstINumUses = DstI->Operands.size() - DstI->Operands.NumDefs; |
| 2149 | unsigned ExpectedDstINumUses = Dst->getNumChildren(); |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2150 | bool IsExtractSubReg = false; |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2151 | |
| 2152 | // COPY_TO_REGCLASS is just a copy with a ConstrainOperandToRegClassAction |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2153 | // attached. Similarly for EXTRACT_SUBREG except that's a subregister copy. |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2154 | if (DstI->TheDef->getName() == "COPY_TO_REGCLASS") { |
| 2155 | DstI = &Target.getInstruction(RK.getDef("COPY")); |
| 2156 | DstINumUses--; // Ignore the class constraint. |
| 2157 | ExpectedDstINumUses--; |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2158 | } else if (DstI->TheDef->getName() == "EXTRACT_SUBREG") { |
| 2159 | DstI = &Target.getInstruction(RK.getDef("COPY")); |
| 2160 | IsExtractSubReg = true; |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2161 | } |
| 2162 | |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2163 | auto &DstMIBuilder = M.addAction<BuildMIAction>(0, DstI, InsnMatcher); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2164 | |
| 2165 | // Render the explicit defs. |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2166 | for (unsigned I = 0; I < DstI->Operands.NumDefs; ++I) { |
| 2167 | const CGIOperandList::OperandInfo &DstIOperand = DstI->Operands[I]; |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2168 | DstMIBuilder.addRenderer<CopyRenderer>(0, InsnMatcher, DstIOperand.Name); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2169 | } |
| 2170 | |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2171 | // EXTRACT_SUBREG needs to use a subregister COPY. |
| 2172 | if (IsExtractSubReg) { |
| 2173 | if (!Dst->getChild(0)->isLeaf()) |
| 2174 | return failedImport("EXTRACT_SUBREG child #1 is not a leaf"); |
| 2175 | |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 2176 | if (DefInit *SubRegInit = |
| 2177 | dyn_cast<DefInit>(Dst->getChild(1)->getLeafValue())) { |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2178 | CodeGenRegisterClass *RC = CGRegs.getRegClass( |
| 2179 | getInitValueAsRegClass(Dst->getChild(0)->getLeafValue())); |
| 2180 | CodeGenSubRegIndex *SubIdx = CGRegs.getSubRegIdx(SubRegInit->getDef()); |
| 2181 | |
| 2182 | const auto &SrcRCDstRCPair = |
| 2183 | RC->getMatchingSubClassWithSubRegs(CGRegs, SubIdx); |
| 2184 | if (SrcRCDstRCPair.hasValue()) { |
| 2185 | assert(SrcRCDstRCPair->second && "Couldn't find a matching subclass"); |
| 2186 | if (SrcRCDstRCPair->first != RC) |
| 2187 | return failedImport("EXTRACT_SUBREG requires an additional COPY"); |
| 2188 | } |
| 2189 | |
| 2190 | DstMIBuilder.addRenderer<CopySubRegRenderer>( |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2191 | 0, InsnMatcher, Dst->getChild(0)->getName(), SubIdx); |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2192 | return DstMIBuilder; |
| 2193 | } |
| 2194 | |
| 2195 | return failedImport("EXTRACT_SUBREG child #1 is not a subreg index"); |
| 2196 | } |
| 2197 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2198 | // Render the explicit uses. |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 2199 | unsigned Child = 0; |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 2200 | unsigned NumDefaultOps = 0; |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 2201 | for (unsigned I = 0; I != DstINumUses; ++I) { |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2202 | const CGIOperandList::OperandInfo &DstIOperand = |
| 2203 | DstI->Operands[DstI->Operands.NumDefs + I]; |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 2204 | |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 2205 | // If the operand has default values, introduce them now. |
| 2206 | // FIXME: Until we have a decent test case that dictates we should do |
| 2207 | // otherwise, we're going to assume that operands with default values cannot |
| 2208 | // be specified in the patterns. Therefore, adding them will not cause us to |
| 2209 | // end up with too many rendered operands. |
| 2210 | if (DstIOperand.Rec->isSubClassOf("OperandWithDefaultOps")) { |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 2211 | DagInit *DefaultOps = DstIOperand.Rec->getValueAsDag("DefaultOps"); |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 2212 | if (auto Error = importDefaultOperandRenderers(DstMIBuilder, DefaultOps)) |
| 2213 | return std::move(Error); |
| 2214 | ++NumDefaultOps; |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 2215 | continue; |
| 2216 | } |
| 2217 | |
| 2218 | if (auto Error = importExplicitUseRenderer( |
| 2219 | DstMIBuilder, Dst->getChild(Child), InsnMatcher)) |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2220 | return std::move(Error); |
Daniel Sanders | 0ed2882 | 2017-04-12 08:23:08 +0000 | [diff] [blame] | 2221 | ++Child; |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2222 | } |
| 2223 | |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2224 | if (NumDefaultOps + ExpectedDstINumUses != DstINumUses) |
Diana Picus | eb2057c | 2017-05-17 09:25:08 +0000 | [diff] [blame] | 2225 | return failedImport("Expected " + llvm::to_string(DstINumUses) + |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 2226 | " used operands but found " + |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2227 | llvm::to_string(ExpectedDstINumUses) + |
Diana Picus | eb2057c | 2017-05-17 09:25:08 +0000 | [diff] [blame] | 2228 | " explicit ones and " + llvm::to_string(NumDefaultOps) + |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 2229 | " default ones"); |
| 2230 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2231 | return DstMIBuilder; |
| 2232 | } |
| 2233 | |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 2234 | Error GlobalISelEmitter::importDefaultOperandRenderers( |
| 2235 | BuildMIAction &DstMIBuilder, DagInit *DefaultOps) const { |
Craig Topper | 481ff70 | 2017-05-29 21:49:34 +0000 | [diff] [blame] | 2236 | for (const auto *DefaultOp : DefaultOps->getArgs()) { |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 2237 | // Look through ValueType operators. |
| 2238 | if (const DagInit *DefaultDagOp = dyn_cast<DagInit>(DefaultOp)) { |
| 2239 | if (const DefInit *DefaultDagOperator = |
| 2240 | dyn_cast<DefInit>(DefaultDagOp->getOperator())) { |
| 2241 | if (DefaultDagOperator->getDef()->isSubClassOf("ValueType")) |
| 2242 | DefaultOp = DefaultDagOp->getArg(0); |
| 2243 | } |
| 2244 | } |
| 2245 | |
| 2246 | if (const DefInit *DefaultDefOp = dyn_cast<DefInit>(DefaultOp)) { |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2247 | DstMIBuilder.addRenderer<AddRegisterRenderer>(0, DefaultDefOp->getDef()); |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 2248 | continue; |
| 2249 | } |
| 2250 | |
| 2251 | if (const IntInit *DefaultIntOp = dyn_cast<IntInit>(DefaultOp)) { |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2252 | DstMIBuilder.addRenderer<ImmRenderer>(0, DefaultIntOp->getValue()); |
Diana Picus | 382602f | 2017-05-17 08:57:28 +0000 | [diff] [blame] | 2253 | continue; |
| 2254 | } |
| 2255 | |
| 2256 | return failedImport("Could not add default op"); |
| 2257 | } |
| 2258 | |
| 2259 | return Error::success(); |
| 2260 | } |
| 2261 | |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 2262 | Error GlobalISelEmitter::importImplicitDefRenderers( |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2263 | BuildMIAction &DstMIBuilder, |
| 2264 | const std::vector<Record *> &ImplicitDefs) const { |
| 2265 | if (!ImplicitDefs.empty()) |
| 2266 | return failedImport("Pattern defines a physical register"); |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 2267 | return Error::success(); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2268 | } |
| 2269 | |
| 2270 | Expected<RuleMatcher> GlobalISelEmitter::runOnPattern(const PatternToMatch &P) { |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2271 | // Keep track of the matchers and actions to emit. |
Ahmed Bougacha | 9aa4c10 | 2017-02-04 00:47:08 +0000 | [diff] [blame] | 2272 | RuleMatcher M; |
| 2273 | M.addAction<DebugCommentAction>(P); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2274 | |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 2275 | if (auto Error = importRulePredicates(M, P.getPredicates()->getValues())) |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2276 | return std::move(Error); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2277 | |
| 2278 | // Next, analyze the pattern operators. |
| 2279 | TreePatternNode *Src = P.getSrcPattern(); |
| 2280 | TreePatternNode *Dst = P.getDstPattern(); |
| 2281 | |
| 2282 | // 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] | 2283 | if (auto Err = isTrivialOperatorNode(Dst)) |
| 2284 | return failedImport("Dst pattern root isn't a trivial operator (" + |
| 2285 | toString(std::move(Err)) + ")"); |
| 2286 | if (auto Err = isTrivialOperatorNode(Src)) |
| 2287 | return failedImport("Src pattern root isn't a trivial operator (" + |
| 2288 | toString(std::move(Err)) + ")"); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2289 | |
Daniel Sanders | eb2f5f3 | 2017-08-15 15:10:31 +0000 | [diff] [blame^] | 2290 | if (Dst->isLeaf()) |
Daniel Sanders | 452c8ae | 2017-05-23 19:33:16 +0000 | [diff] [blame] | 2291 | return failedImport("Dst pattern root isn't a known leaf"); |
| 2292 | |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 2293 | // Start with the defined operands (i.e., the results of the root operator). |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2294 | Record *DstOp = Dst->getOperator(); |
| 2295 | if (!DstOp->isSubClassOf("Instruction")) |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 2296 | return failedImport("Pattern operator isn't an instruction"); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2297 | |
| 2298 | auto &DstI = Target.getInstruction(DstOp); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2299 | if (DstI.Operands.NumDefs != Src->getExtTypes().size()) |
Daniel Sanders | d0656a3 | 2017-04-13 09:45:37 +0000 | [diff] [blame] | 2300 | return failedImport("Src pattern results and dst MI defs are different (" + |
| 2301 | to_string(Src->getExtTypes().size()) + " def(s) vs " + |
| 2302 | to_string(DstI.Operands.NumDefs) + " def(s))"); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2303 | |
Daniel Sanders | eb2f5f3 | 2017-08-15 15:10:31 +0000 | [diff] [blame^] | 2304 | InstructionMatcher &InsnMatcherTemp = M.addInstructionMatcher(Src->getName()); |
| 2305 | unsigned TempOpIdx = 0; |
| 2306 | auto InsnMatcherOrError = |
| 2307 | createAndImportSelDAGMatcher(InsnMatcherTemp, Src, TempOpIdx); |
| 2308 | if (auto Error = InsnMatcherOrError.takeError()) |
| 2309 | return std::move(Error); |
| 2310 | InstructionMatcher &InsnMatcher = InsnMatcherOrError.get(); |
| 2311 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2312 | // The root of the match also has constraints on the register bank so that it |
| 2313 | // matches the result instruction. |
| 2314 | unsigned OpIdx = 0; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2315 | for (const EEVT::TypeSet &Ty : Src->getExtTypes()) { |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2316 | (void)Ty; |
| 2317 | |
Daniel Sanders | 066ebbf | 2017-02-24 15:43:30 +0000 | [diff] [blame] | 2318 | const auto &DstIOperand = DstI.Operands[OpIdx]; |
| 2319 | Record *DstIOpRec = DstIOperand.Rec; |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2320 | if (DstI.TheDef->getName() == "COPY_TO_REGCLASS") { |
| 2321 | DstIOpRec = getInitValueAsRegClass(Dst->getChild(1)->getLeafValue()); |
| 2322 | |
| 2323 | if (DstIOpRec == nullptr) |
| 2324 | return failedImport( |
| 2325 | "COPY_TO_REGCLASS operand #1 isn't a register class"); |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2326 | } else if (DstI.TheDef->getName() == "EXTRACT_SUBREG") { |
| 2327 | if (!Dst->getChild(0)->isLeaf()) |
| 2328 | return failedImport("EXTRACT_SUBREG operand #0 isn't a leaf"); |
| 2329 | |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 2330 | // We can assume that a subregister is in the same bank as it's super |
| 2331 | // register. |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2332 | DstIOpRec = getInitValueAsRegClass(Dst->getChild(0)->getLeafValue()); |
| 2333 | |
| 2334 | if (DstIOpRec == nullptr) |
| 2335 | return failedImport( |
| 2336 | "EXTRACT_SUBREG operand #0 isn't a register class"); |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2337 | } else if (DstIOpRec->isSubClassOf("RegisterOperand")) |
Daniel Sanders | 658541f | 2017-04-22 15:53:21 +0000 | [diff] [blame] | 2338 | DstIOpRec = DstIOpRec->getValueAsDef("RegClass"); |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2339 | else if (!DstIOpRec->isSubClassOf("RegisterClass")) |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 2340 | return failedImport("Dst MI def isn't a register class" + |
| 2341 | to_string(*Dst)); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2342 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2343 | OperandMatcher &OM = InsnMatcher.getOperand(OpIdx); |
| 2344 | OM.setSymbolicName(DstIOperand.Name); |
Daniel Sanders | dc662ff | 2017-01-26 11:10:14 +0000 | [diff] [blame] | 2345 | OM.addPredicate<RegisterBankOperandMatcher>( |
| 2346 | Target.getRegisterClass(DstIOpRec)); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2347 | ++OpIdx; |
| 2348 | } |
| 2349 | |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 2350 | auto DstMIBuilderOrError = |
| 2351 | createAndImportInstructionRenderer(M, Dst, InsnMatcher); |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2352 | if (auto Error = DstMIBuilderOrError.takeError()) |
| 2353 | return std::move(Error); |
| 2354 | BuildMIAction &DstMIBuilder = DstMIBuilderOrError.get(); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2355 | |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2356 | // Render the implicit defs. |
| 2357 | // These are only added to the root of the result. |
Daniel Sanders | c270c50 | 2017-03-30 09:36:33 +0000 | [diff] [blame] | 2358 | if (auto Error = importImplicitDefRenderers(DstMIBuilder, P.getDstRegs())) |
Daniel Sanders | ffc7d58 | 2017-03-29 15:37:18 +0000 | [diff] [blame] | 2359 | return std::move(Error); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2360 | |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2361 | // Constrain the registers to classes. This is normally derived from the |
| 2362 | // emitted instruction but a few instructions require special handling. |
| 2363 | if (DstI.TheDef->getName() == "COPY_TO_REGCLASS") { |
| 2364 | // COPY_TO_REGCLASS does not provide operand constraints itself but the |
| 2365 | // result is constrained to the class given by the second child. |
| 2366 | Record *DstIOpRec = |
| 2367 | getInitValueAsRegClass(Dst->getChild(1)->getLeafValue()); |
| 2368 | |
| 2369 | if (DstIOpRec == nullptr) |
| 2370 | return failedImport("COPY_TO_REGCLASS operand #1 isn't a register class"); |
| 2371 | |
| 2372 | M.addAction<ConstrainOperandToRegClassAction>( |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2373 | 0, 0, Target.getRegisterClass(DstIOpRec)); |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2374 | |
| 2375 | // We're done with this pattern! It's eligible for GISel emission; return |
| 2376 | // it. |
| 2377 | ++NumPatternImported; |
| 2378 | return std::move(M); |
| 2379 | } |
| 2380 | |
| 2381 | if (DstI.TheDef->getName() == "EXTRACT_SUBREG") { |
| 2382 | // EXTRACT_SUBREG selects into a subregister COPY but unlike most |
| 2383 | // instructions, the result register class is controlled by the |
| 2384 | // subregisters of the operand. As a result, we must constrain the result |
| 2385 | // class rather than check that it's already the right one. |
| 2386 | if (!Dst->getChild(0)->isLeaf()) |
| 2387 | return failedImport("EXTRACT_SUBREG child #1 is not a leaf"); |
| 2388 | |
Daniel Sanders | 320390b | 2017-06-28 15:16:03 +0000 | [diff] [blame] | 2389 | DefInit *SubRegInit = dyn_cast<DefInit>(Dst->getChild(1)->getLeafValue()); |
| 2390 | if (!SubRegInit) |
| 2391 | return failedImport("EXTRACT_SUBREG child #1 is not a subreg index"); |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2392 | |
Daniel Sanders | 320390b | 2017-06-28 15:16:03 +0000 | [diff] [blame] | 2393 | // Constrain the result to the same register bank as the operand. |
| 2394 | Record *DstIOpRec = |
| 2395 | getInitValueAsRegClass(Dst->getChild(0)->getLeafValue()); |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2396 | |
Daniel Sanders | 320390b | 2017-06-28 15:16:03 +0000 | [diff] [blame] | 2397 | if (DstIOpRec == nullptr) |
| 2398 | return failedImport("EXTRACT_SUBREG operand #1 isn't a register class"); |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2399 | |
Daniel Sanders | 320390b | 2017-06-28 15:16:03 +0000 | [diff] [blame] | 2400 | CodeGenSubRegIndex *SubIdx = CGRegs.getSubRegIdx(SubRegInit->getDef()); |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2401 | CodeGenRegisterClass *SrcRC = CGRegs.getRegClass(DstIOpRec); |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2402 | |
Daniel Sanders | 320390b | 2017-06-28 15:16:03 +0000 | [diff] [blame] | 2403 | // It would be nice to leave this constraint implicit but we're required |
| 2404 | // to pick a register class so constrain the result to a register class |
| 2405 | // that can hold the correct MVT. |
| 2406 | // |
| 2407 | // FIXME: This may introduce an extra copy if the chosen class doesn't |
| 2408 | // actually contain the subregisters. |
| 2409 | assert(Src->getExtTypes().size() == 1 && |
| 2410 | "Expected Src of EXTRACT_SUBREG to have one result type"); |
Daniel Sanders | cc36dbf | 2017-06-27 10:11:39 +0000 | [diff] [blame] | 2411 | |
Daniel Sanders | 320390b | 2017-06-28 15:16:03 +0000 | [diff] [blame] | 2412 | const auto &SrcRCDstRCPair = |
| 2413 | SrcRC->getMatchingSubClassWithSubRegs(CGRegs, SubIdx); |
| 2414 | assert(SrcRCDstRCPair->second && "Couldn't find a matching subclass"); |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame] | 2415 | M.addAction<ConstrainOperandToRegClassAction>(0, 0, *SrcRCDstRCPair->second); |
| 2416 | M.addAction<ConstrainOperandToRegClassAction>(0, 1, *SrcRCDstRCPair->first); |
| 2417 | |
| 2418 | // We're done with this pattern! It's eligible for GISel emission; return |
| 2419 | // it. |
| 2420 | ++NumPatternImported; |
| 2421 | return std::move(M); |
| 2422 | } |
| 2423 | |
| 2424 | M.addAction<ConstrainOperandsToDefinitionAction>(0); |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 2425 | |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 2426 | // 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] | 2427 | ++NumPatternImported; |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 2428 | return std::move(M); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2429 | } |
| 2430 | |
| 2431 | void GlobalISelEmitter::run(raw_ostream &OS) { |
| 2432 | // Track the GINodeEquiv definitions. |
| 2433 | gatherNodeEquivs(); |
| 2434 | |
| 2435 | emitSourceFileHeader(("Global Instruction Selector for the " + |
| 2436 | Target.getName() + " target").str(), OS); |
Daniel Sanders | b41ce2b | 2017-02-20 14:31:27 +0000 | [diff] [blame] | 2437 | std::vector<RuleMatcher> Rules; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2438 | // Look through the SelectionDAG patterns we found, possibly emitting some. |
| 2439 | for (const PatternToMatch &Pat : CGP.ptms()) { |
| 2440 | ++NumPatternTotal; |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 2441 | auto MatcherOrErr = runOnPattern(Pat); |
| 2442 | |
| 2443 | // The pattern analysis can fail, indicating an unsupported pattern. |
| 2444 | // Report that if we've been asked to do so. |
| 2445 | if (auto Err = MatcherOrErr.takeError()) { |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2446 | if (WarnOnSkippedPatterns) { |
| 2447 | PrintWarning(Pat.getSrcRecord()->getLoc(), |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 2448 | "Skipped pattern: " + toString(std::move(Err))); |
| 2449 | } else { |
| 2450 | consumeError(std::move(Err)); |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2451 | } |
Daniel Sanders | b41ce2b | 2017-02-20 14:31:27 +0000 | [diff] [blame] | 2452 | ++NumPatternImportsSkipped; |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 2453 | continue; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2454 | } |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 2455 | |
Daniel Sanders | b41ce2b | 2017-02-20 14:31:27 +0000 | [diff] [blame] | 2456 | Rules.push_back(std::move(MatcherOrErr.get())); |
| 2457 | } |
| 2458 | |
Daniel Sanders | eb2f5f3 | 2017-08-15 15:10:31 +0000 | [diff] [blame^] | 2459 | std::stable_sort(Rules.begin(), Rules.end(), |
| 2460 | [&](const RuleMatcher &A, const RuleMatcher &B) { |
| 2461 | if (A.isHigherPriorityThan(B)) { |
| 2462 | assert(!B.isHigherPriorityThan(A) && "Cannot be more important " |
| 2463 | "and less important at " |
| 2464 | "the same time"); |
| 2465 | return true; |
| 2466 | } |
| 2467 | return false; |
| 2468 | }); |
Daniel Sanders | 759ff41 | 2017-02-24 13:58:11 +0000 | [diff] [blame] | 2469 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 2470 | std::vector<Record *> ComplexPredicates = |
| 2471 | RK.getAllDerivedDefinitions("GIComplexOperandMatcher"); |
| 2472 | std::sort(ComplexPredicates.begin(), ComplexPredicates.end(), |
| 2473 | [](const Record *A, const Record *B) { |
| 2474 | if (A->getName() < B->getName()) |
| 2475 | return true; |
| 2476 | return false; |
| 2477 | }); |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 2478 | unsigned MaxTemporaries = 0; |
| 2479 | for (const auto &Rule : Rules) |
Daniel Sanders | 2deea18 | 2017-04-22 15:11:04 +0000 | [diff] [blame] | 2480 | MaxTemporaries = std::max(MaxTemporaries, Rule.countRendererFns()); |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 2481 | |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 2482 | OS << "#ifdef GET_GLOBALISEL_PREDICATE_BITSET\n" |
| 2483 | << "const unsigned MAX_SUBTARGET_PREDICATES = " << SubtargetFeatures.size() |
| 2484 | << ";\n" |
| 2485 | << "using PredicateBitset = " |
| 2486 | "llvm::PredicateBitsetImpl<MAX_SUBTARGET_PREDICATES>;\n" |
| 2487 | << "#endif // ifdef GET_GLOBALISEL_PREDICATE_BITSET\n\n"; |
| 2488 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 2489 | OS << "#ifdef GET_GLOBALISEL_TEMPORARIES_DECL\n" |
| 2490 | << " mutable MatcherState State;\n" |
| 2491 | << " typedef " |
| 2492 | "ComplexRendererFn(" |
| 2493 | << Target.getName() |
| 2494 | << "InstructionSelector::*ComplexMatcherMemFn)(MachineOperand &) const;\n" |
| 2495 | << "const MatcherInfoTy<PredicateBitset, ComplexMatcherMemFn> " |
| 2496 | "MatcherInfo;\n" |
| 2497 | << "#endif // ifdef GET_GLOBALISEL_TEMPORARIES_DECL\n\n"; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 2498 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 2499 | OS << "#ifdef GET_GLOBALISEL_TEMPORARIES_INIT\n" |
| 2500 | << ", State(" << MaxTemporaries << "),\n" |
| 2501 | << "MatcherInfo({TypeObjects, FeatureBitsets, {\n" |
| 2502 | << " nullptr, // GICP_Invalid\n"; |
| 2503 | for (const auto &Record : ComplexPredicates) |
| 2504 | OS << " &" << Target.getName() |
| 2505 | << "InstructionSelector::" << Record->getValueAsString("MatcherFn") |
| 2506 | << ", // " << Record->getName() << "\n"; |
| 2507 | OS << "}})\n" |
| 2508 | << "#endif // ifdef GET_GLOBALISEL_TEMPORARIES_INIT\n\n"; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 2509 | |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 2510 | OS << "#ifdef GET_GLOBALISEL_IMPL\n"; |
| 2511 | SubtargetFeatureInfo::emitSubtargetFeatureBitEnumeration(SubtargetFeatures, |
| 2512 | OS); |
Daniel Sanders | e9fdba3 | 2017-04-29 17:30:09 +0000 | [diff] [blame] | 2513 | |
| 2514 | // Separate subtarget features by how often they must be recomputed. |
| 2515 | SubtargetFeatureInfoMap ModuleFeatures; |
| 2516 | std::copy_if(SubtargetFeatures.begin(), SubtargetFeatures.end(), |
| 2517 | std::inserter(ModuleFeatures, ModuleFeatures.end()), |
| 2518 | [](const SubtargetFeatureInfoMap::value_type &X) { |
| 2519 | return !X.second.mustRecomputePerFunction(); |
| 2520 | }); |
| 2521 | SubtargetFeatureInfoMap FunctionFeatures; |
| 2522 | std::copy_if(SubtargetFeatures.begin(), SubtargetFeatures.end(), |
| 2523 | std::inserter(FunctionFeatures, FunctionFeatures.end()), |
| 2524 | [](const SubtargetFeatureInfoMap::value_type &X) { |
| 2525 | return X.second.mustRecomputePerFunction(); |
| 2526 | }); |
| 2527 | |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 2528 | SubtargetFeatureInfo::emitComputeAvailableFeatures( |
Daniel Sanders | e9fdba3 | 2017-04-29 17:30:09 +0000 | [diff] [blame] | 2529 | Target.getName(), "InstructionSelector", "computeAvailableModuleFeatures", |
| 2530 | ModuleFeatures, OS); |
| 2531 | SubtargetFeatureInfo::emitComputeAvailableFeatures( |
| 2532 | Target.getName(), "InstructionSelector", |
| 2533 | "computeAvailableFunctionFeatures", FunctionFeatures, OS, |
| 2534 | "const MachineFunction *MF"); |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 2535 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 2536 | // Emit a table containing the LLT objects needed by the matcher and an enum |
| 2537 | // for the matcher to reference them with. |
Daniel Sanders | eb2f5f3 | 2017-08-15 15:10:31 +0000 | [diff] [blame^] | 2538 | std::vector<LLTCodeGen> TypeObjects = { |
| 2539 | LLT::scalar(8), LLT::scalar(16), LLT::scalar(32), |
| 2540 | LLT::scalar(64), LLT::scalar(80), LLT::vector(8, 1), |
| 2541 | LLT::vector(16, 1), LLT::vector(32, 1), LLT::vector(64, 1), |
| 2542 | LLT::vector(8, 8), LLT::vector(16, 8), LLT::vector(32, 8), |
| 2543 | LLT::vector(64, 8), LLT::vector(4, 16), LLT::vector(8, 16), |
| 2544 | LLT::vector(16, 16), LLT::vector(32, 16), LLT::vector(2, 32), |
| 2545 | LLT::vector(4, 32), LLT::vector(8, 32), LLT::vector(16, 32), |
| 2546 | LLT::vector(2, 64), LLT::vector(4, 64), LLT::vector(8, 64), |
| 2547 | }; |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 2548 | std::sort(TypeObjects.begin(), TypeObjects.end()); |
| 2549 | OS << "enum {\n"; |
| 2550 | for (const auto &TypeObject : TypeObjects) { |
| 2551 | OS << " "; |
| 2552 | TypeObject.emitCxxEnumValue(OS); |
| 2553 | OS << ",\n"; |
| 2554 | } |
| 2555 | OS << "};\n" |
| 2556 | << "const static LLT TypeObjects[] = {\n"; |
| 2557 | for (const auto &TypeObject : TypeObjects) { |
| 2558 | OS << " "; |
| 2559 | TypeObject.emitCxxConstructorCall(OS); |
| 2560 | OS << ",\n"; |
| 2561 | } |
| 2562 | OS << "};\n\n"; |
| 2563 | |
| 2564 | // Emit a table containing the PredicateBitsets objects needed by the matcher |
| 2565 | // and an enum for the matcher to reference them with. |
| 2566 | std::vector<std::vector<Record *>> FeatureBitsets; |
| 2567 | for (auto &Rule : Rules) |
| 2568 | FeatureBitsets.push_back(Rule.getRequiredFeatures()); |
| 2569 | std::sort( |
| 2570 | FeatureBitsets.begin(), FeatureBitsets.end(), |
| 2571 | [&](const std::vector<Record *> &A, const std::vector<Record *> &B) { |
| 2572 | if (A.size() < B.size()) |
| 2573 | return true; |
| 2574 | if (A.size() > B.size()) |
| 2575 | return false; |
| 2576 | for (const auto &Pair : zip(A, B)) { |
| 2577 | if (std::get<0>(Pair)->getName() < std::get<1>(Pair)->getName()) |
| 2578 | return true; |
| 2579 | if (std::get<0>(Pair)->getName() > std::get<1>(Pair)->getName()) |
| 2580 | return false; |
| 2581 | } |
| 2582 | return false; |
| 2583 | }); |
| 2584 | FeatureBitsets.erase( |
| 2585 | std::unique(FeatureBitsets.begin(), FeatureBitsets.end()), |
| 2586 | FeatureBitsets.end()); |
| 2587 | OS << "enum {\n" |
| 2588 | << " GIFBS_Invalid,\n"; |
| 2589 | for (const auto &FeatureBitset : FeatureBitsets) { |
| 2590 | if (FeatureBitset.empty()) |
| 2591 | continue; |
| 2592 | OS << " " << getNameForFeatureBitset(FeatureBitset) << ",\n"; |
| 2593 | } |
| 2594 | OS << "};\n" |
| 2595 | << "const static PredicateBitset FeatureBitsets[] {\n" |
| 2596 | << " {}, // GIFBS_Invalid\n"; |
| 2597 | for (const auto &FeatureBitset : FeatureBitsets) { |
| 2598 | if (FeatureBitset.empty()) |
| 2599 | continue; |
| 2600 | OS << " {"; |
| 2601 | for (const auto &Feature : FeatureBitset) { |
| 2602 | const auto &I = SubtargetFeatures.find(Feature); |
| 2603 | assert(I != SubtargetFeatures.end() && "Didn't import predicate?"); |
| 2604 | OS << I->second.getEnumBitName() << ", "; |
| 2605 | } |
| 2606 | OS << "},\n"; |
| 2607 | } |
| 2608 | OS << "};\n\n"; |
| 2609 | |
| 2610 | // Emit complex predicate table and an enum to reference them with. |
| 2611 | OS << "enum {\n" |
| 2612 | << " GICP_Invalid,\n"; |
| 2613 | for (const auto &Record : ComplexPredicates) |
| 2614 | OS << " GICP_" << Record->getName() << ",\n"; |
| 2615 | OS << "};\n" |
| 2616 | << "// See constructor for table contents\n\n"; |
| 2617 | |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 2618 | OS << "bool " << Target.getName() |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 2619 | << "InstructionSelector::selectImpl(MachineInstr &I) const {\n" |
| 2620 | << " MachineFunction &MF = *I.getParent()->getParent();\n" |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 2621 | << " MachineRegisterInfo &MRI = MF.getRegInfo();\n" |
Daniel Sanders | 3229198 | 2017-06-28 13:50:04 +0000 | [diff] [blame] | 2622 | << " // FIXME: This should be computed on a per-function basis rather " |
| 2623 | "than per-insn.\n" |
| 2624 | << " AvailableFunctionFeatures = computeAvailableFunctionFeatures(&STI, " |
| 2625 | "&MF);\n" |
Daniel Sanders | a6cfce6 | 2017-07-05 14:50:18 +0000 | [diff] [blame] | 2626 | << " const PredicateBitset AvailableFeatures = getAvailableFeatures();\n" |
| 2627 | << " NewMIVector OutMIs;\n" |
| 2628 | << " State.MIs.clear();\n" |
| 2629 | << " State.MIs.push_back(&I);\n\n"; |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 2630 | |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 2631 | MatchTable Table(0); |
Daniel Sanders | b96f40d | 2017-03-20 15:20:42 +0000 | [diff] [blame] | 2632 | for (auto &Rule : Rules) { |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 2633 | Rule.emit(Table); |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 2634 | ++NumPatternEmitted; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2635 | } |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 2636 | Table << MatchTable::Opcode("GIM_Reject") << MatchTable::LineBreak; |
| 2637 | Table.emitDeclaration(OS); |
| 2638 | OS << " if (executeMatchTable(*this, OutMIs, State, MatcherInfo, "; |
| 2639 | Table.emitUse(OS); |
| 2640 | OS << ", TII, MRI, TRI, RBI, AvailableFeatures)) {\n" |
| 2641 | << " return true;\n" |
| 2642 | << " }\n\n"; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2643 | |
Daniel Sanders | 8a4bae9 | 2017-03-14 21:32:08 +0000 | [diff] [blame] | 2644 | OS << " return false;\n" |
| 2645 | << "}\n" |
| 2646 | << "#endif // ifdef GET_GLOBALISEL_IMPL\n"; |
Daniel Sanders | e9fdba3 | 2017-04-29 17:30:09 +0000 | [diff] [blame] | 2647 | |
| 2648 | OS << "#ifdef GET_GLOBALISEL_PREDICATES_DECL\n" |
| 2649 | << "PredicateBitset AvailableModuleFeatures;\n" |
| 2650 | << "mutable PredicateBitset AvailableFunctionFeatures;\n" |
| 2651 | << "PredicateBitset getAvailableFeatures() const {\n" |
| 2652 | << " return AvailableModuleFeatures | AvailableFunctionFeatures;\n" |
| 2653 | << "}\n" |
| 2654 | << "PredicateBitset\n" |
| 2655 | << "computeAvailableModuleFeatures(const " << Target.getName() |
| 2656 | << "Subtarget *Subtarget) const;\n" |
| 2657 | << "PredicateBitset\n" |
| 2658 | << "computeAvailableFunctionFeatures(const " << Target.getName() |
| 2659 | << "Subtarget *Subtarget,\n" |
| 2660 | << " const MachineFunction *MF) const;\n" |
| 2661 | << "#endif // ifdef GET_GLOBALISEL_PREDICATES_DECL\n"; |
| 2662 | |
| 2663 | OS << "#ifdef GET_GLOBALISEL_PREDICATES_INIT\n" |
| 2664 | << "AvailableModuleFeatures(computeAvailableModuleFeatures(&STI)),\n" |
| 2665 | << "AvailableFunctionFeatures()\n" |
| 2666 | << "#endif // ifdef GET_GLOBALISEL_PREDICATES_INIT\n"; |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2667 | } |
| 2668 | |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 2669 | void GlobalISelEmitter::declareSubtargetFeature(Record *Predicate) { |
| 2670 | if (SubtargetFeatures.count(Predicate) == 0) |
| 2671 | SubtargetFeatures.emplace( |
| 2672 | Predicate, SubtargetFeatureInfo(Predicate, SubtargetFeatures.size())); |
| 2673 | } |
| 2674 | |
Ahmed Bougacha | 982c5eb | 2017-02-10 04:00:17 +0000 | [diff] [blame] | 2675 | } // end anonymous namespace |
| 2676 | |
Ahmed Bougacha | 36f7035 | 2016-12-21 23:26:20 +0000 | [diff] [blame] | 2677 | //===----------------------------------------------------------------------===// |
| 2678 | |
| 2679 | namespace llvm { |
| 2680 | void EmitGlobalISel(RecordKeeper &RK, raw_ostream &OS) { |
| 2681 | GlobalISelEmitter(RK).run(OS); |
| 2682 | } |
| 2683 | } // End llvm namespace |