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