1 | //===- Patterns.h ----------------------------------------------*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | /// \file Contains the Pattern hierarchy alongside helper classes such as |
10 | /// PatFrag, MIFlagsInfo, PatternType, etc. |
11 | /// |
12 | /// These classes are used by the GlobalISel Combiner backend to help parse, |
13 | /// process and emit MIR patterns. |
14 | // |
15 | //===----------------------------------------------------------------------===// |
16 | |
17 | #ifndef LLVM_UTILS_GLOBALISEL_PATTERNS_H |
18 | #define LLVM_UTILS_GLOBALISEL_PATTERNS_H |
19 | |
20 | #include "llvm/ADT/ArrayRef.h" |
21 | #include "llvm/ADT/SetVector.h" |
22 | #include "llvm/ADT/SmallVector.h" |
23 | #include "llvm/ADT/StringMap.h" |
24 | #include "llvm/ADT/StringRef.h" |
25 | #include "llvm/ADT/Twine.h" |
26 | #include <memory> |
27 | #include <optional> |
28 | #include <string> |
29 | |
30 | namespace llvm { |
31 | |
32 | class Record; |
33 | class SMLoc; |
34 | class StringInit; |
35 | class CodeExpansions; |
36 | class CodeGenInstruction; |
37 | struct CodeGenIntrinsic; |
38 | |
39 | namespace gi { |
40 | |
41 | class CXXPredicateCode; |
42 | class LLTCodeGen; |
43 | class LLTCodeGenOrTempType; |
44 | class RuleMatcher; |
45 | |
46 | //===- PatternType --------------------------------------------------------===// |
47 | |
48 | /// Represent the type of a Pattern Operand. |
49 | /// |
50 | /// Types have two form: |
51 | /// - LLTs, which are straightforward. |
52 | /// - Special types, e.g. GITypeOf |
53 | class PatternType { |
54 | public: |
55 | static constexpr StringLiteral SpecialTyClassName = "GISpecialType" ; |
56 | static constexpr StringLiteral TypeOfClassName = "GITypeOf" ; |
57 | |
58 | enum PTKind : uint8_t { |
59 | PT_None, |
60 | |
61 | PT_ValueType, |
62 | PT_TypeOf, |
63 | }; |
64 | |
65 | PatternType() : Kind(PT_None), Data() {} |
66 | |
67 | static std::optional<PatternType> get(ArrayRef<SMLoc> DiagLoc, |
68 | const Record *R, Twine DiagCtx); |
69 | static PatternType getTypeOf(StringRef OpName); |
70 | |
71 | bool isNone() const { return Kind == PT_None; } |
72 | bool isLLT() const { return Kind == PT_ValueType; } |
73 | bool isSpecial() const { return isTypeOf(); } |
74 | bool isTypeOf() const { return Kind == PT_TypeOf; } |
75 | |
76 | StringRef getTypeOfOpName() const; |
77 | const Record *getLLTRecord() const; |
78 | |
79 | explicit operator bool() const { return !isNone(); } |
80 | |
81 | bool operator==(const PatternType &Other) const; |
82 | bool operator!=(const PatternType &Other) const { return !operator==(Other); } |
83 | |
84 | std::string str() const; |
85 | |
86 | private: |
87 | PatternType(PTKind Kind) : Kind(Kind), Data() {} |
88 | |
89 | PTKind Kind; |
90 | union DataT { |
91 | DataT() : Str() {} |
92 | |
93 | /// PT_ValueType -> ValueType Def. |
94 | const Record *Def; |
95 | |
96 | /// PT_TypeOf -> Operand name (without the '$') |
97 | StringRef Str; |
98 | } Data; |
99 | }; |
100 | |
101 | //===- Pattern Base Class -------------------------------------------------===// |
102 | |
103 | /// Base class for all patterns that can be written in an `apply`, `match` or |
104 | /// `pattern` DAG operator. |
105 | /// |
106 | /// For example: |
107 | /// |
108 | /// (apply (G_ZEXT $x, $y), (G_ZEXT $y, $z), "return isFoo(${z})") |
109 | /// |
110 | /// Creates 3 Pattern objects: |
111 | /// - Two CodeGenInstruction Patterns |
112 | /// - A CXXPattern |
113 | class Pattern { |
114 | public: |
115 | enum { |
116 | K_AnyOpcode, |
117 | K_CXX, |
118 | |
119 | K_CodeGenInstruction, |
120 | K_PatFrag, |
121 | K_Builtin, |
122 | }; |
123 | |
124 | virtual ~Pattern() = default; |
125 | |
126 | unsigned getKind() const { return Kind; } |
127 | const char *getKindName() const; |
128 | |
129 | bool hasName() const { return !Name.empty(); } |
130 | StringRef getName() const { return Name; } |
131 | |
132 | virtual void print(raw_ostream &OS, bool PrintName = true) const = 0; |
133 | void dump() const; |
134 | |
135 | protected: |
136 | Pattern(unsigned Kind, StringRef Name) : Kind(Kind), Name(Name) { |
137 | assert(!Name.empty() && "unnamed pattern!" ); |
138 | } |
139 | |
140 | void printImpl(raw_ostream &OS, bool PrintName, |
141 | function_ref<void()> ContentPrinter) const; |
142 | |
143 | private: |
144 | unsigned Kind; |
145 | StringRef Name; |
146 | }; |
147 | |
148 | //===- AnyOpcodePattern ---------------------------------------------------===// |
149 | |
150 | /// `wip_match_opcode` patterns. |
151 | /// This matches one or more opcodes, and does not check any operands |
152 | /// whatsoever. |
153 | /// |
154 | /// TODO: Long-term, this needs to be removed. It's a hack around MIR |
155 | /// pattern matching limitations. |
156 | class AnyOpcodePattern : public Pattern { |
157 | public: |
158 | AnyOpcodePattern(StringRef Name) : Pattern(K_AnyOpcode, Name) {} |
159 | |
160 | static bool classof(const Pattern *P) { return P->getKind() == K_AnyOpcode; } |
161 | |
162 | void addOpcode(const CodeGenInstruction *I) { Insts.push_back(Elt: I); } |
163 | const auto &insts() const { return Insts; } |
164 | |
165 | void print(raw_ostream &OS, bool PrintName = true) const override; |
166 | |
167 | private: |
168 | SmallVector<const CodeGenInstruction *, 4> Insts; |
169 | }; |
170 | |
171 | //===- CXXPattern ---------------------------------------------------------===// |
172 | |
173 | /// Represents raw C++ code which may need some expansions. |
174 | /// |
175 | /// e.g. [{ return isFooBux(${src}.getReg()); }] |
176 | /// |
177 | /// For the expanded code, \see CXXPredicateCode. CXXPredicateCode objects are |
178 | /// created through `expandCode`. |
179 | /// |
180 | /// \see CodeExpander and \see CodeExpansions for more information on code |
181 | /// expansions. |
182 | /// |
183 | /// This object has two purposes: |
184 | /// - Represent C++ code as a pattern entry. |
185 | /// - Be a factory for expanded C++ code. |
186 | /// - It's immutable and only holds the raw code so we can expand the same |
187 | /// CXX pattern multiple times if we need to. |
188 | /// |
189 | /// Note that the code is always trimmed in the constructor, so leading and |
190 | /// trailing whitespaces are removed. This removes bloat in the output, avoids |
191 | /// formatting issues, but also allows us to check things like |
192 | /// `.startswith("return")` trivially without worrying about spaces. |
193 | class CXXPattern : public Pattern { |
194 | public: |
195 | CXXPattern(const StringInit &Code, StringRef Name); |
196 | |
197 | CXXPattern(StringRef Code, StringRef Name) |
198 | : Pattern(K_CXX, Name), RawCode(Code.trim().str()) {} |
199 | |
200 | static bool classof(const Pattern *P) { return P->getKind() == K_CXX; } |
201 | |
202 | void setIsApply(bool Value = true) { IsApply = Value; } |
203 | StringRef getRawCode() const { return RawCode; } |
204 | |
205 | /// Expands raw code, replacing things such as `${foo}` with their |
206 | /// substitution in \p CE. |
207 | /// |
208 | /// \param CE Map of Code Expansions |
209 | /// \param Locs SMLocs for the Code Expander, in case it needs to emit |
210 | /// diagnostics. |
211 | /// \param AddComment Optionally called to emit a comment before the expanded |
212 | /// code. |
213 | /// |
214 | /// \return A CXXPredicateCode object that contains the expanded code. Note |
215 | /// that this may or may not insert a new object. All CXXPredicateCode objects |
216 | /// are held in a set to avoid emitting duplicate C++ code. |
217 | const CXXPredicateCode & |
218 | expandCode(const CodeExpansions &CE, ArrayRef<SMLoc> Locs, |
219 | function_ref<void(raw_ostream &)> = {}) const; |
220 | |
221 | void print(raw_ostream &OS, bool PrintName = true) const override; |
222 | |
223 | private: |
224 | bool IsApply = false; |
225 | std::string RawCode; |
226 | }; |
227 | |
228 | //===- InstructionPattern ---------------------------------------------===// |
229 | |
230 | /// An operand for an InstructionPattern. |
231 | /// |
232 | /// Operands are composed of three elements: |
233 | /// - (Optional) Value |
234 | /// - (Optional) Name |
235 | /// - (Optional) Type |
236 | /// |
237 | /// Some examples: |
238 | /// (i32 0):$x -> V=int(0), Name='x', Type=i32 |
239 | /// 0:$x -> V=int(0), Name='x' |
240 | /// $x -> Name='x' |
241 | /// i32:$x -> Name='x', Type = i32 |
242 | class InstructionOperand { |
243 | public: |
244 | using IntImmTy = int64_t; |
245 | |
246 | InstructionOperand(IntImmTy Imm, StringRef Name, PatternType Type) |
247 | : Value(Imm), Name(Name), Type(Type) {} |
248 | |
249 | InstructionOperand(StringRef Name, PatternType Type) |
250 | : Name(Name), Type(Type) {} |
251 | |
252 | bool isNamedImmediate() const { return hasImmValue() && isNamedOperand(); } |
253 | |
254 | bool hasImmValue() const { return Value.has_value(); } |
255 | IntImmTy getImmValue() const { return *Value; } |
256 | |
257 | bool isNamedOperand() const { return !Name.empty(); } |
258 | StringRef getOperandName() const { |
259 | assert(isNamedOperand() && "Operand is unnamed" ); |
260 | return Name; |
261 | } |
262 | |
263 | InstructionOperand withNewName(StringRef NewName) const { |
264 | InstructionOperand Result = *this; |
265 | Result.Name = NewName; |
266 | return Result; |
267 | } |
268 | |
269 | void setIsDef(bool Value = true) { Def = Value; } |
270 | bool isDef() const { return Def; } |
271 | |
272 | void setType(PatternType NewType) { |
273 | assert((!Type || (Type == NewType)) && "Overwriting type!" ); |
274 | Type = NewType; |
275 | } |
276 | PatternType getType() const { return Type; } |
277 | |
278 | std::string describe() const; |
279 | void print(raw_ostream &OS) const; |
280 | |
281 | void dump() const; |
282 | |
283 | private: |
284 | std::optional<int64_t> Value; |
285 | StringRef Name; |
286 | PatternType Type; |
287 | bool Def = false; |
288 | }; |
289 | |
290 | /// Base class for CodeGenInstructionPattern & PatFragPattern, which handles all |
291 | /// the boilerplate for patterns that have a list of operands for some (pseudo) |
292 | /// instruction. |
293 | class InstructionPattern : public Pattern { |
294 | public: |
295 | virtual ~InstructionPattern() = default; |
296 | |
297 | static bool classof(const Pattern *P) { |
298 | return P->getKind() == K_CodeGenInstruction || P->getKind() == K_PatFrag || |
299 | P->getKind() == K_Builtin; |
300 | } |
301 | |
302 | template <typename... Ty> void addOperand(Ty &&...Init) { |
303 | Operands.emplace_back(std::forward<Ty>(Init)...); |
304 | } |
305 | |
306 | auto &operands() { return Operands; } |
307 | const auto &operands() const { return Operands; } |
308 | unsigned operands_size() const { return Operands.size(); } |
309 | InstructionOperand &getOperand(unsigned K) { return Operands[K]; } |
310 | const InstructionOperand &getOperand(unsigned K) const { return Operands[K]; } |
311 | |
312 | /// When this InstructionPattern is used as the match root, returns the |
313 | /// operands that must be redefined in the 'apply' pattern for the rule to be |
314 | /// valid. |
315 | /// |
316 | /// For most patterns, this just returns the defs. |
317 | /// For PatFrag this only returns the root of the PF. |
318 | /// |
319 | /// Returns an empty array on error. |
320 | virtual ArrayRef<InstructionOperand> getApplyDefsNeeded() const { |
321 | return {operands().begin(), getNumInstDefs()}; |
322 | } |
323 | |
324 | auto named_operands() { |
325 | return make_filter_range(Range&: Operands, |
326 | Pred: [&](auto &O) { return O.isNamedOperand(); }); |
327 | } |
328 | |
329 | auto named_operands() const { |
330 | return make_filter_range(Range: Operands, |
331 | Pred: [&](auto &O) { return O.isNamedOperand(); }); |
332 | } |
333 | |
334 | virtual bool isVariadic() const { return false; } |
335 | virtual unsigned getNumInstOperands() const = 0; |
336 | virtual unsigned getNumInstDefs() const = 0; |
337 | |
338 | bool hasAllDefs() const { return operands_size() >= getNumInstDefs(); } |
339 | |
340 | virtual StringRef getInstName() const = 0; |
341 | |
342 | /// Diagnoses all uses of special types in this Pattern and returns true if at |
343 | /// least one diagnostic was emitted. |
344 | bool diagnoseAllSpecialTypes(ArrayRef<SMLoc> Loc, Twine Msg) const; |
345 | |
346 | void reportUnreachable(ArrayRef<SMLoc> Locs) const; |
347 | virtual bool checkSemantics(ArrayRef<SMLoc> Loc); |
348 | |
349 | void print(raw_ostream &OS, bool PrintName = true) const override; |
350 | |
351 | protected: |
352 | InstructionPattern(unsigned K, StringRef Name) : Pattern(K, Name) {} |
353 | |
354 | virtual void (raw_ostream &OS) const {} |
355 | |
356 | SmallVector<InstructionOperand, 4> Operands; |
357 | }; |
358 | |
359 | //===- OperandTable -------------------------------------------------------===// |
360 | |
361 | /// Maps InstructionPattern operands to their definitions. This allows us to tie |
362 | /// different patterns of a (apply), (match) or (patterns) set of patterns |
363 | /// together. |
364 | class OperandTable { |
365 | public: |
366 | bool addPattern(InstructionPattern *P, |
367 | function_ref<void(StringRef)> DiagnoseRedef); |
368 | |
369 | struct LookupResult { |
370 | LookupResult() = default; |
371 | LookupResult(InstructionPattern *Def) : Found(true), Def(Def) {} |
372 | |
373 | bool Found = false; |
374 | InstructionPattern *Def = nullptr; |
375 | |
376 | bool isLiveIn() const { return Found && !Def; } |
377 | }; |
378 | |
379 | LookupResult lookup(StringRef OpName) const { |
380 | if (auto It = Table.find(Key: OpName); It != Table.end()) |
381 | return LookupResult(It->second); |
382 | return LookupResult(); |
383 | } |
384 | |
385 | InstructionPattern *getDef(StringRef OpName) const { |
386 | return lookup(OpName).Def; |
387 | } |
388 | |
389 | void print(raw_ostream &OS, StringRef Name = "" , StringRef Indent = "" ) const; |
390 | |
391 | auto begin() const { return Table.begin(); } |
392 | auto end() const { return Table.end(); } |
393 | |
394 | void dump() const; |
395 | |
396 | private: |
397 | StringMap<InstructionPattern *> Table; |
398 | }; |
399 | |
400 | //===- MIFlagsInfo --------------------------------------------------------===// |
401 | |
402 | /// Helper class to contain data associated with a MIFlags operand. |
403 | class MIFlagsInfo { |
404 | public: |
405 | void addSetFlag(const Record *R); |
406 | void addUnsetFlag(const Record *R); |
407 | void addCopyFlag(StringRef InstName); |
408 | |
409 | const auto &set_flags() const { return SetF; } |
410 | const auto &unset_flags() const { return UnsetF; } |
411 | const auto ©_flags() const { return CopyF; } |
412 | |
413 | private: |
414 | SetVector<StringRef> SetF, UnsetF, CopyF; |
415 | }; |
416 | |
417 | //===- CodeGenInstructionPattern ------------------------------------------===// |
418 | |
419 | /// Matches an instruction or intrinsic: |
420 | /// e.g. `G_ADD $x, $y, $z` or `int_amdgcn_cos $a` |
421 | /// |
422 | /// Intrinsics are just normal instructions with a special operand for intrinsic |
423 | /// ID. Despite G_INTRINSIC opcodes being variadic, we consider that the |
424 | /// Intrinsic's info takes priority. This means we return: |
425 | /// - false for isVariadic() and other variadic-related queries. |
426 | /// - getNumInstDefs and getNumInstOperands use the intrinsic's in/out |
427 | /// operands. |
428 | class CodeGenInstructionPattern : public InstructionPattern { |
429 | public: |
430 | CodeGenInstructionPattern(const CodeGenInstruction &I, StringRef Name) |
431 | : InstructionPattern(K_CodeGenInstruction, Name), I(I) {} |
432 | |
433 | static bool classof(const Pattern *P) { |
434 | return P->getKind() == K_CodeGenInstruction; |
435 | } |
436 | |
437 | bool is(StringRef OpcodeName) const; |
438 | |
439 | void setIntrinsic(const CodeGenIntrinsic *I) { IntrinInfo = I; } |
440 | const CodeGenIntrinsic *getIntrinsic() const { return IntrinInfo; } |
441 | bool isIntrinsic() const { return IntrinInfo; } |
442 | |
443 | bool hasVariadicDefs() const; |
444 | bool isVariadic() const override; |
445 | unsigned getNumInstDefs() const override; |
446 | unsigned getNumInstOperands() const override; |
447 | |
448 | MIFlagsInfo &getOrCreateMIFlagsInfo(); |
449 | const MIFlagsInfo *getMIFlagsInfo() const { return FI.get(); } |
450 | |
451 | const CodeGenInstruction &getInst() const { return I; } |
452 | StringRef getInstName() const override; |
453 | |
454 | private: |
455 | void (raw_ostream &OS) const override; |
456 | |
457 | const CodeGenInstruction &I; |
458 | const CodeGenIntrinsic *IntrinInfo = nullptr; |
459 | std::unique_ptr<MIFlagsInfo> FI; |
460 | }; |
461 | |
462 | //===- OperandTypeChecker -------------------------------------------------===// |
463 | |
464 | /// This is a trivial type checker for all operands in a set of |
465 | /// InstructionPatterns. |
466 | /// |
467 | /// It infers the type of each operand, check it's consistent with the known |
468 | /// type of the operand, and then sets all of the types in all operands in |
469 | /// propagateTypes. |
470 | /// |
471 | /// It also handles verifying correctness of special types. |
472 | class OperandTypeChecker { |
473 | public: |
474 | OperandTypeChecker(ArrayRef<SMLoc> DiagLoc) : DiagLoc(DiagLoc) {} |
475 | |
476 | /// Step 1: Check each pattern one by one. All patterns that pass through here |
477 | /// are added to a common worklist so propagateTypes can access them. |
478 | bool check(InstructionPattern &P, |
479 | std::function<bool(const PatternType &)> VerifyTypeOfOperand); |
480 | |
481 | /// Step 2: Propagate all types. e.g. if one use of "$a" has type i32, make |
482 | /// all uses of "$a" have type i32. |
483 | void propagateTypes(); |
484 | |
485 | protected: |
486 | ArrayRef<SMLoc> DiagLoc; |
487 | |
488 | private: |
489 | using InconsistentTypeDiagFn = std::function<void()>; |
490 | |
491 | void PrintSeenWithTypeIn(InstructionPattern &P, StringRef OpName, |
492 | PatternType Ty) const; |
493 | |
494 | struct OpTypeInfo { |
495 | PatternType Type; |
496 | InconsistentTypeDiagFn PrintTypeSrcNote = []() {}; |
497 | }; |
498 | |
499 | StringMap<OpTypeInfo> Types; |
500 | |
501 | SmallVector<InstructionPattern *, 16> Pats; |
502 | }; |
503 | |
504 | //===- PatFrag ------------------------------------------------------------===// |
505 | |
506 | /// Represents a parsed GICombinePatFrag. This can be thought of as the |
507 | /// equivalent of a CodeGenInstruction, but for PatFragPatterns. |
508 | /// |
509 | /// PatFrags are made of 3 things: |
510 | /// - Out parameters (defs) |
511 | /// - In parameters |
512 | /// - A set of pattern lists (alternatives). |
513 | /// |
514 | /// If the PatFrag uses instruction patterns, the root must be one of the defs. |
515 | /// |
516 | /// Note that this DOES NOT represent the use of the PatFrag, only its |
517 | /// definition. The use of the PatFrag in a Pattern is represented by |
518 | /// PatFragPattern. |
519 | /// |
520 | /// PatFrags use the term "parameter" instead of operand because they're |
521 | /// essentially macros, and using that name avoids confusion. Other than that, |
522 | /// they're structured similarly to a MachineInstruction - all parameters |
523 | /// (operands) are in the same list, with defs at the start. This helps mapping |
524 | /// parameters to values, because, param N of a PatFrag is always operand N of a |
525 | /// PatFragPattern. |
526 | class PatFrag { |
527 | public: |
528 | static constexpr StringLiteral ClassName = "GICombinePatFrag" ; |
529 | |
530 | enum ParamKind { |
531 | PK_Root, |
532 | PK_MachineOperand, |
533 | PK_Imm, |
534 | }; |
535 | |
536 | struct Param { |
537 | StringRef Name; |
538 | ParamKind Kind; |
539 | }; |
540 | |
541 | using ParamVec = SmallVector<Param, 4>; |
542 | using ParamIt = ParamVec::const_iterator; |
543 | |
544 | /// Represents an alternative of the PatFrag. When parsing a GICombinePatFrag, |
545 | /// this is created from its "Alternatives" list. Each alternative is a list |
546 | /// of patterns written wrapped in a `(pattern ...)` dag init. |
547 | /// |
548 | /// Each argument to the `pattern` DAG operator is parsed into a Pattern |
549 | /// instance. |
550 | struct Alternative { |
551 | OperandTable OpTable; |
552 | SmallVector<std::unique_ptr<Pattern>, 4> Pats; |
553 | }; |
554 | |
555 | explicit PatFrag(const Record &Def); |
556 | |
557 | static StringRef getParamKindStr(ParamKind OK); |
558 | |
559 | StringRef getName() const; |
560 | |
561 | const Record &getDef() const { return Def; } |
562 | ArrayRef<SMLoc> getLoc() const; |
563 | |
564 | Alternative &addAlternative() { return Alts.emplace_back(); } |
565 | const Alternative &getAlternative(unsigned K) const { return Alts[K]; } |
566 | unsigned num_alternatives() const { return Alts.size(); } |
567 | |
568 | void addInParam(StringRef Name, ParamKind Kind); |
569 | iterator_range<ParamIt> in_params() const; |
570 | unsigned num_in_params() const { return Params.size() - NumOutParams; } |
571 | |
572 | void addOutParam(StringRef Name, ParamKind Kind); |
573 | iterator_range<ParamIt> out_params() const; |
574 | unsigned num_out_params() const { return NumOutParams; } |
575 | |
576 | unsigned num_roots() const; |
577 | unsigned num_params() const { return num_in_params() + num_out_params(); } |
578 | |
579 | /// Finds the operand \p Name and returns its index or -1 if not found. |
580 | /// Remember that all params are part of the same list, with out params at the |
581 | /// start. This means that the index returned can be used to access operands |
582 | /// of InstructionPatterns. |
583 | unsigned getParamIdx(StringRef Name) const; |
584 | const Param &getParam(unsigned K) const { return Params[K]; } |
585 | |
586 | bool canBeMatchRoot() const { return num_roots() == 1; } |
587 | |
588 | void print(raw_ostream &OS, StringRef Indent = "" ) const; |
589 | void dump() const; |
590 | |
591 | /// Checks if the in-param \p ParamName can be unbound or not. |
592 | /// \p ArgName is the name of the argument passed to the PatFrag. |
593 | /// |
594 | /// An argument can be unbound only if, for all alternatives: |
595 | /// - There is no CXX pattern, OR: |
596 | /// - There is an InstructionPattern that binds the parameter. |
597 | /// |
598 | /// e.g. in (MyPatFrag $foo), if $foo has never been seen before (= it's |
599 | /// unbound), this checks if MyPatFrag supports it or not. |
600 | bool handleUnboundInParam(StringRef ParamName, StringRef ArgName, |
601 | ArrayRef<SMLoc> DiagLoc) const; |
602 | |
603 | bool checkSemantics(); |
604 | bool buildOperandsTables(); |
605 | |
606 | private: |
607 | static void printParamsList(raw_ostream &OS, iterator_range<ParamIt> Params); |
608 | |
609 | void PrintError(Twine Msg) const; |
610 | |
611 | const Record &Def; |
612 | unsigned NumOutParams = 0; |
613 | ParamVec Params; |
614 | SmallVector<Alternative, 2> Alts; |
615 | }; |
616 | |
617 | //===- PatFragPattern -----------------------------------------------------===// |
618 | |
619 | /// Represents a use of a GICombinePatFrag. |
620 | class PatFragPattern : public InstructionPattern { |
621 | public: |
622 | PatFragPattern(const PatFrag &PF, StringRef Name) |
623 | : InstructionPattern(K_PatFrag, Name), PF(PF) {} |
624 | |
625 | static bool classof(const Pattern *P) { return P->getKind() == K_PatFrag; } |
626 | |
627 | const PatFrag &getPatFrag() const { return PF; } |
628 | StringRef getInstName() const override { return PF.getName(); } |
629 | |
630 | unsigned getNumInstDefs() const override { return PF.num_out_params(); } |
631 | unsigned getNumInstOperands() const override { return PF.num_params(); } |
632 | |
633 | ArrayRef<InstructionOperand> getApplyDefsNeeded() const override; |
634 | |
635 | bool checkSemantics(ArrayRef<SMLoc> DiagLoc) override; |
636 | |
637 | /// Before emitting the patterns inside the PatFrag, add all necessary code |
638 | /// expansions to \p PatFragCEs imported from \p ParentCEs. |
639 | /// |
640 | /// For a MachineOperand PatFrag parameter, this will fetch the expansion for |
641 | /// that operand from \p ParentCEs and add it to \p PatFragCEs. Errors can be |
642 | /// emitted if the MachineOperand reference is unbound. |
643 | /// |
644 | /// For an Immediate PatFrag parameter this simply adds the integer value to |
645 | /// \p PatFragCEs as an expansion. |
646 | /// |
647 | /// \param ParentCEs Contains all of the code expansions declared by the other |
648 | /// patterns emitted so far in the pattern list containing |
649 | /// this PatFragPattern. |
650 | /// \param PatFragCEs Output Code Expansions (usually empty) |
651 | /// \param DiagLoc Diagnostic loc in case an error occurs. |
652 | /// \return `true` on success, `false` on failure. |
653 | bool mapInputCodeExpansions(const CodeExpansions &ParentCEs, |
654 | CodeExpansions &PatFragCEs, |
655 | ArrayRef<SMLoc> DiagLoc) const; |
656 | |
657 | private: |
658 | const PatFrag &PF; |
659 | }; |
660 | |
661 | //===- BuiltinPattern -----------------------------------------------------===// |
662 | |
663 | /// Represents builtin instructions such as "GIReplaceReg" and "GIEraseRoot". |
664 | enum BuiltinKind { |
665 | BI_ReplaceReg, |
666 | BI_EraseRoot, |
667 | }; |
668 | |
669 | class BuiltinPattern : public InstructionPattern { |
670 | struct BuiltinInfo { |
671 | StringLiteral DefName; |
672 | BuiltinKind Kind; |
673 | unsigned NumOps; |
674 | unsigned NumDefs; |
675 | }; |
676 | |
677 | static constexpr std::array<BuiltinInfo, 2> KnownBuiltins = {._M_elems: { |
678 | {.DefName: "GIReplaceReg" , .Kind: BI_ReplaceReg, .NumOps: 2, .NumDefs: 1}, |
679 | {.DefName: "GIEraseRoot" , .Kind: BI_EraseRoot, .NumOps: 0, .NumDefs: 0}, |
680 | }}; |
681 | |
682 | public: |
683 | static constexpr StringLiteral ClassName = "GIBuiltinInst" ; |
684 | |
685 | BuiltinPattern(const Record &Def, StringRef Name) |
686 | : InstructionPattern(K_Builtin, Name), I(getBuiltinInfo(Def)) {} |
687 | |
688 | static bool classof(const Pattern *P) { return P->getKind() == K_Builtin; } |
689 | |
690 | unsigned getNumInstOperands() const override { return I.NumOps; } |
691 | unsigned getNumInstDefs() const override { return I.NumDefs; } |
692 | StringRef getInstName() const override { return I.DefName; } |
693 | BuiltinKind getBuiltinKind() const { return I.Kind; } |
694 | |
695 | bool checkSemantics(ArrayRef<SMLoc> Loc) override; |
696 | |
697 | private: |
698 | static BuiltinInfo getBuiltinInfo(const Record &Def); |
699 | |
700 | BuiltinInfo I; |
701 | }; |
702 | |
703 | } // namespace gi |
704 | } // end namespace llvm |
705 | |
706 | #endif // ifndef LLVM_UTILS_GLOBALISEL_PATTERNS_H |
707 | |