LLVM 24.0.0git
GIMatchTableExecutor.h
Go to the documentation of this file.
1//===- llvm/CodeGen/GlobalISel/GIMatchTableExecutor.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 This file declares the GIMatchTableExecutor API, the opcodes supported
10/// by the match table, and some associated data structures used by the
11/// executor's implementation (see `GIMatchTableExecutorImpl.h`).
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CODEGEN_GLOBALISEL_GIMATCHTABLEEXECUTOR_H
16#define LLVM_CODEGEN_GLOBALISEL_GIMATCHTABLEEXECUTOR_H
17
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/Bitset.h"
20#include "llvm/ADT/DenseMap.h"
21#include "llvm/ADT/DenseSet.h"
28#include "llvm/IR/Function.h"
31#include <bitset>
32#include <cstddef>
33#include <cstdint>
34#include <functional>
35#include <initializer_list>
36#include <optional>
37#include <vector>
38
39namespace llvm {
40
42class CodeGenCoverage;
45class APInt;
46class APFloat;
48class MachineInstr;
51class MachineFunction;
52class MachineOperand;
55class TargetInstrInfo;
57
58enum {
61};
62
63/// The MatchTable is encoded as an array of bytes.
64/// Thus, opcodes are expected to be <255.
65///
66/// Operands can be variable-sized, their size is always after their name
67/// in the docs, e.g. "Foo(4)" means that "Foo" takes 4 entries in the table,
68/// so 4 bytes. "Foo()"
69///
70/// As a general rule of thumb:
71/// - Instruction & Operand IDs are ULEB128
72/// - LLT IDs are 1 byte
73/// - Predicates and target opcodes, register and register class IDs are 2
74/// bytes.
75/// - Indexes into the table are 4 bytes.
76/// - Inline constants are 8 bytes
77///
78/// Design notes:
79/// - Inst/Op IDs have to be LEB128 because some targets generate
80/// extremely long patterns which need more than 255 temporaries.
81/// We could just use 2 bytes everytime, but then some targets like
82/// X86/AMDGPU that have no need for it will pay the price all the time.
83enum {
84 /// Begin a try-block to attempt a match and jump to OnFail if it is
85 /// unsuccessful.
86 /// - OnFail(4) - The MatchTable entry at which to resume if the match fails.
87 ///
88 /// FIXME: This ought to take an argument indicating the number of try-blocks
89 /// to exit on failure. It's usually one but the last match attempt of
90 /// a block will need more. The (implemented) alternative is to tack a
91 /// GIM_Reject on the end of each try-block which is simpler but
92 /// requires an extra opcode and iteration in the interpreter on each
93 /// failed match.
95
96 /// GIM_Try only if the feature bits match.
97 /// - OnFail(4) - The MatchTable entry at which to resume if the match fails.
98 /// - Feature(2) - Expected features
100
101 /// Switch over the opcode on the specified instruction
102 /// - InsnID(ULEB128) - Instruction ID
103 /// - LowerBound(2) - numerically minimum opcode supported
104 /// - UpperBound(2) - numerically maximum + 1 opcode supported
105 /// - Default(4) - failure jump target
106 /// - JumpTable(4)... - (UpperBound - LowerBound) (at least 2) jump targets
108
109 /// Switch over the LLT on the specified instruction operand
110 /// - InsnID(ULEB128) - Instruction ID
111 /// - OpIdx(ULEB128) - Operand index
112 /// - LowerBound(2) - numerically minimum Type ID supported
113 /// - UpperBound(2) - numerically maximum + 1 Type ID supported
114 /// - Default(4) - failure jump target
115 /// - JumpTable(4)... - (UpperBound - LowerBound) (at least 2) jump targets
117
118 /// Switch over the shape of an LLT on the specified instruction operand
119 /// - InsnID(ULEB128) - Instruction ID
120 /// - OpIdx(ULEB128) - Operand index
121 /// - LowerBound(2) - numerically minimum Type ID supported
122 /// - UpperBound(2) - numerically maximum + 1 Type ID supported
123 /// - Default(4) - failure jump target
124 /// - JumpTable(4)... - (UpperBound - LowerBound) (at least 2) jump targets
126
127 /// Record the specified instruction.
128 /// The IgnoreCopies variant ignores COPY instructions.
129 /// - NewInsnID(ULEB128) - Instruction ID to define
130 /// - InsnID(ULEB128) - Instruction ID
131 /// - OpIdx(ULEB128) - Operand index
134
135 /// Check the opcode on the specified instruction
136 /// - InsnID(ULEB128) - Instruction ID
137 /// - Opc(2) - Expected opcode
139
140 /// Check the opcode on the specified instruction, checking 2 acceptable
141 /// alternatives.
142 /// - InsnID(ULEB128) - Instruction ID
143 /// - Opc(2) - Expected opcode
144 /// - Opc(2) - Alternative expected opcode
146
147 /// Check the instruction has the right number of operands
148 /// - InsnID(ULEB128) - Instruction ID
149 /// - Ops(ULEB128) - Expected number of operands
151
152 /// Check the instruction has a number of operands <= or >= than given number.
153 /// - InsnID(ULEB128) - Instruction ID
154 /// - Ops(ULEB128) - Number of operands
157
158 /// Check an immediate predicate on the specified instruction
159 /// - InsnID(ULEB128) - Instruction ID
160 /// - Pred(2) - The predicate to test
162 /// Check an immediate predicate on the specified instruction via an APInt.
163 /// - InsnID(ULEB128) - Instruction ID
164 /// - Pred(2) - The predicate to test
166 /// Check a floating point immediate predicate on the specified instruction.
167 /// - InsnID(ULEB128) - Instruction ID
168 /// - Pred(2) - The predicate to test
170 /// Check an immediate predicate on the specified instruction
171 /// - InsnID(ULEB128) - Instruction ID
172 /// - OpIdx(ULEB128) - Operand index
173 /// - Pred(2) - The predicate to test
175
176 /// Check a leaf predicate on the specified instruction.
177 /// - InsnID(ULEB128) - Instruction ID
178 /// - OpIdx(ULEB128) - Operand index
179 /// - Pred(2) - The predicate to test
181
182 /// Check a memory operation has the specified atomic ordering.
183 /// - InsnID(ULEB128) - Instruction ID
184 /// - Ordering(ULEB128) - The AtomicOrdering value
188
189 /// Check the size of the memory access for the given machine memory operand.
190 /// - InsnID(ULEB128) - Instruction ID
191 /// - MMOIdx(ULEB128) - MMO index
192 /// - Size(4) - The size in bytes of the memory access
194
195 /// Check the address space of the memory access for the given machine memory
196 /// operand.
197 /// - InsnID(ULEB128) - Instruction ID
198 /// - MMOIdx(ULEB128) - MMO index
199 /// - NumAddrSpace(1) - Number of valid address spaces
200 /// - AddrSpaceN(ULEB128) - An allowed space of the memory access
201 /// - AddrSpaceN+1 ...
203
204 /// Check the minimum alignment of the memory access for the given machine
205 /// memory operand.
206 /// - InsnID(ULEB128) - Instruction ID
207 /// - MMOIdx(ULEB128) - MMO index
208 /// - MinAlign(1) - Minimum acceptable alignment
210
211 /// Check the size of the memory access for the given machine memory operand
212 /// against the size of an operand.
213 /// - InsnID(ULEB128) - Instruction ID
214 /// - MMOIdx(ULEB128) - MMO index
215 /// - OpIdx(ULEB128) - The operand index to compare the MMO against
219
220 /// Check if this is a vector that can be treated as a vector splat
221 /// constant. This is valid for both G_BUILD_VECTOR as well as
222 /// G_BUILD_VECTOR_TRUNC. For AllOnes refers to individual bits, so a -1
223 /// element.
224 /// - InsnID(ULEB128) - Instruction ID
227
228 /// Check a trivial predicate which takes no arguments.
229 /// This can be used by executors to implement custom flags that don't fit in
230 /// target features.
231 /// - Pred(2) - Predicate ID to check.
233
234 /// Check a generic C++ instruction predicate
235 /// - InsnID(ULEB128) - Instruction ID
236 /// - PredicateID(2) - The ID of the predicate function to call
238
239 /// Check if there's no use of the first result.
240 /// - InsnID(ULEB128) - Instruction ID
242
243 /// Check if there's one use of the first result.
244 /// - InsnID(ULEB128) - Instruction ID
246
247 /// Check the type for the specified operand
248 /// - InsnID(ULEB128) - Instruction ID
249 /// - OpIdx(ULEB128) - Operand index
250 /// - Ty(1) - Expected type
252 /// GIM_CheckType but InsnID is omitted and defaults to zero.
254
255 /// Check the type for the specified operand where the type depends on the
256 /// HwMode.
257 /// - InsnID(ULEB128) - Instruction ID
258 /// - OpIdx(ULEB128) - Operand index
259 /// - Idx(1) - Index passed to getLLTForHwMode
261 /// GIM_CheckTypeByHwMode but InsnID is omitted and defaults to zero.
263
264 /// Check the type of a pointer to any address space.
265 /// - InsnID(ULEB128) - Instruction ID
266 /// - OpIdx(ULEB128) - Operand index
267 /// - SizeInBits(ULEB128) - The size of the pointer value in bits.
269
270 /// Check the type of a pointer to any address space where the size of the
271 /// pointer depends on the HwMode.
272 /// - InsnID(ULEB128) - Instruction ID
273 /// - OpIdx(ULEB128) - Operand index
274 /// - Idx(1) - Index passed to getLLTForHwMode. The size of the returned type
275 /// is the size of the pointer value in bits.
277
278 /// Check the register bank for the specified operand
279 /// - InsnID(ULEB128) - Instruction ID
280 /// - OpIdx(ULEB128) - Operand index
281 /// - RC(2) - Expected register bank (specified as a register class)
283 /// GIM_CheckRegBankForClass but InsnID is omitted and defaults to zero.
285
286 /// Check the operand matches a complex predicate
287 /// - InsnID(ULEB128) - Instruction ID
288 /// - OpIdx(ULEB128) - Operand index
289 /// - RendererID(2) - The renderer to hold the result
290 /// - Pred(2) - Complex predicate ID
292
293 /// Check the operand is a specific integer
294 /// - InsnID(ULEB128) - Instruction ID
295 /// - OpIdx(ULEB128) - Operand index
296 /// - Val(8) Expected integer
298
299 /// Check the operand is a specific 8-bit signed integer
300 /// - InsnID(ULEB128) - Instruction ID
301 /// - OpIdx(ULEB128) - Operand index
302 /// - Val(1) Expected integer
304
305 /// Check the operand is a specific literal integer (i.e. MO.isImm() or
306 /// MO.isCImm() is true).
307 /// - InsnID(ULEB128) - Instruction ID
308 /// - OpIdx(ULEB128) - Operand index
309 /// - Val(8) - Expected integer
311
312 /// Check the operand is a specific intrinsic ID
313 /// - InsnID(ULEB128) - Instruction ID
314 /// - OpIdx(ULEB128) - Operand index
315 /// - IID(2) - Expected Intrinsic ID
317
318 /// Check the operand is a specific predicate
319 /// - InsnID(ULEB128) - Instruction ID
320 /// - OpIdx(ULEB128) - Operand index
321 /// - Pred(2) - Expected predicate
323
324 /// Check the specified operand is an MBB
325 /// - InsnID(ULEB128) - Instruction ID
326 /// - OpIdx(ULEB128) - Operand index
328
329 /// Check the specified operand is an Imm
330 /// - InsnID(ULEB128) - Instruction ID
331 /// - OpIdx(ULEB128) - Operand index
333
334 /// Checks if the matched instructions numbered [1, 1+N) can
335 /// be folded into the root (inst 0).
336 /// - Num(1)
338
339 /// Check the specified operands are identical.
340 /// The IgnoreCopies variant looks through COPY instructions before
341 /// comparing the operands.
342 /// The "All" variants check all operands starting from the index.
343 /// - InsnID(ULEB128) - Instruction ID
344 /// - OpIdx(ULEB128) - Operand index
345 /// - OtherInsnID(ULEB128) - Other instruction ID
346 /// - OtherOpIdx(ULEB128) - Other operand index
351
352 /// Check we can replace all uses of a register with another.
353 /// - OldInsnID(ULEB128)
354 /// - OldOpIdx(ULEB128)
355 /// - NewInsnID(ULEB128)
356 /// - NewOpIdx(ULEB128)
358
359 /// Check that a matched instruction has, or doesn't have a MIFlag.
360 ///
361 /// - InsnID(ULEB128) - Instruction to check.
362 /// - Flags(4) - (can be one or more flags OR'd together)
365
366 /// Predicates with 'let PredicateCodeUsesOperands = 1' need to examine some
367 /// named operands that will be recorded in RecordedOperands. Names of these
368 /// operands are referenced in predicate argument list. Emitter determines
369 /// StoreIdx(corresponds to the order in which names appear in argument list).
370 /// - InsnID(ULEB128) - Instruction ID
371 /// - OpIdx(ULEB128) - Operand index
372 /// - StoreIdx(ULEB128) - Store location in RecordedOperands.
374
375 /// Records an operand's register type into the set of temporary types.
376 /// - InsnID(ULEB128) - Instruction ID
377 /// - OpIdx(ULEB128) - Operand index
378 /// - TempTypeIdx(1) - Temp Type Index, always negative.
380
381 /// Fail the current try-block, or completely fail to match if there is no
382 /// current try-block.
384
385 //=== Renderers ===
386
387 /// Mutate an instruction
388 /// - NewInsnID(ULEB128) - Instruction ID to define
389 /// - OldInsnID(ULEB128) - Instruction ID to mutate
390 /// - NewOpcode(2) - The new opcode to use
392
393 /// Build a new instruction
394 /// - InsnID(ULEB128) - Instruction ID to define
395 /// - Opcode(2) - The new opcode to use
397 /// GIR_BuildMI but InsnID is omitted and defaults to zero.
399
400 /// Builds a constant and stores its result in a TempReg.
401 /// - TempRegID(ULEB128) - Temp Register to define.
402 /// - Imm(8) - The immediate to add
404
405 /// Copy an operand to the specified instruction
406 /// - NewInsnID(ULEB128) - Instruction ID to modify
407 /// - OldInsnID(ULEB128) - Instruction ID to copy from
408 /// - OpIdx(ULEB128) - The operand to copy
410 /// GIR_Copy but with both New/OldInsnIDs omitted and defaulting to zero.
412
413 /// Copies all operand starting from OpIdx in OldInsnID into the new
414 /// instruction NewInsnID.
415 /// - NewInsnID(ULEB128) - Instruction ID to modify
416 /// - OldInsnID(ULEB128) - Instruction ID to copy from
417 /// - OpIdx(ULEB128) - The first operand to copy
419
420 /// Copy an operand to the specified instruction or add a zero register if the
421 /// operand is a zero immediate.
422 /// - NewInsnID(ULEB128) - Instruction ID to modify
423 /// - OldInsnID(ULEB128) - Instruction ID to copy from
424 /// - OpIdx(ULEB128) - The operand to copy
425 /// - ZeroReg(2) - The zero register to use
427 /// Copy an operand to the specified instruction
428 /// - NewInsnID(ULEB128) - Instruction ID to modify
429 /// - OldInsnID(ULEB128) - Instruction ID to copy from
430 /// - OpIdx(ULEB128) - The operand to copy
431 /// - SubRegIdx(2) - The subregister to copy
433
434 /// Add an implicit register def to the specified instruction
435 /// - InsnID(ULEB128) - Instruction ID to modify
436 /// - RegNum(2) - The register to add
437 /// - Flags(2) - Register Flags
439 /// Add an implicit register use to the specified instruction
440 /// - InsnID(ULEB128) - Instruction ID to modify
441 /// - RegNum(2) - The register to add
443 /// Add an register to the specified instruction
444 /// - InsnID(ULEB128) - Instruction ID to modify
445 /// - RegNum(2) - The register to add
446 /// - Flags(2) - Register Flags
448
449 /// Adds an intrinsic ID to the specified instruction.
450 /// - InsnID(ULEB128) - Instruction ID to modify
451 /// - IID(2) - Intrinsic ID
453
454 /// Marks the implicit def of a register as dead.
455 /// - InsnID(ULEB128) - Instruction ID to modify
456 /// - OpIdx(ULEB128) - The implicit def operand index
457 ///
458 /// OpIdx starts at 0 for the first implicit def.
460
461 /// Set or unset a MIFlag on an instruction.
462 ///
463 /// - InsnID(ULEB128) - Instruction to modify.
464 /// - Flags(4) - (can be one or more flags OR'd together)
467
468 /// Copy the MIFlags of a matched instruction into an
469 /// output instruction. The flags are OR'd together.
470 ///
471 /// - InsnID(ULEB128) - Instruction to modify.
472 /// - OldInsnID(ULEB128) - Matched instruction to copy flags from.
474
475 /// Add a temporary register to the specified instruction
476 /// - InsnID(ULEB128) - Instruction ID to modify
477 /// - TempRegID(ULEB128) - The temporary register ID to add
478 /// - TempRegFlags(2) - The register flags to set
480
481 /// Add a temporary register to the specified instruction without
482 /// setting any flags.
483 /// - InsnID(ULEB128) - Instruction ID to modify
484 /// - TempRegID(ULEB128) - The temporary register ID to add
486
487 /// Add a temporary register to the specified instruction
488 /// - InsnID(ULEB128) - Instruction ID to modify
489 /// - TempRegID(ULEB128) - The temporary register ID to add
490 /// - TempRegFlags(2) - The register flags to set
491 /// - SubRegIndex(2) - The subregister index to set
493
494 /// Add an immediate to the specified instruction
495 /// - InsnID(ULEB128) - Instruction ID to modify
496 /// - Imm(8) - The immediate to add
498
499 /// Add signed 8 bit immediate to the specified instruction
500 /// - InsnID(ULEB128) - Instruction ID to modify
501 /// - Imm(1) - The immediate to add
503
504 /// Add an CImm to the specified instruction
505 /// - InsnID(ULEB128) - Instruction ID to modify
506 /// - Ty(1) - Type of the constant immediate.
507 /// - Imm(8) - The immediate to add
509
510 /// Add a floating-point immediate to the specified instruction.
511 /// Imm is the IEEE bit pattern of a value with Ty's scalar semantics; it is
512 /// not converted from an integer or a double.
513 /// - InsnID(ULEB128) - Instruction ID to modify
514 /// - Ty(1) - Type of the constant immediate.
515 /// - Imm(8) - Bit pattern of the floating-point immediate
517
518 /// Render complex operands to the specified instruction
519 /// - InsnID(ULEB128) - Instruction ID to modify
520 /// - RendererID(2) - The renderer to call
522 /// Render sub-operands of complex operands to the specified instruction
523 /// - InsnID(ULEB128) - Instruction ID to modify
524 /// - RendererID(2) - The renderer to call
525 /// - RenderOpID(ULEB128) - The suboperand to render.
527 /// Render subregisters of suboperands of complex operands to the
528 /// specified instruction
529 /// - InsnID(ULEB128) - Instruction ID to modify
530 /// - RendererID(2) - The renderer to call
531 /// - RenderOpID(ULEB128) - The suboperand to render
532 /// - SubRegIdx(2) - The subregister to extract
534
535 /// Render operands to the specified instruction using a custom function
536 /// - InsnID(ULEB128) - Instruction ID to modify
537 /// - OldInsnID(ULEB128) - Instruction ID to get the matched operand from
538 /// - RendererFnID(2) - Custom renderer function to call
540
541 /// Calls a C++ function that concludes the current match.
542 /// The C++ function is free to return false and reject the match, or
543 /// return true and mutate the instruction(s) (or do nothing, even).
544 /// Poison-generating flags left on OutMIs by the custom action are treated as
545 /// explicitly preserved.
546 /// - FnID(2) - The function to call.
548
549 /// Render operands to the specified instruction using a custom function,
550 /// reading from a specific operand.
551 /// - InsnID(ULEB128) - Instruction ID to modify
552 /// - OldInsnID(ULEB128) - Instruction ID to get the matched operand from
553 /// - OpIdx(ULEB128) - Operand index in OldInsnID the render function should
554 /// read
555 /// from..
556 /// - RendererFnID(2) - Custom renderer function to call
558
559 /// Render a G_CONSTANT operator as a sign-extended immediate.
560 /// - NewInsnID(ULEB128) - Instruction ID to modify
561 /// - OldInsnID(ULEB128) - Instruction ID to copy from
562 /// The operand index is implicitly 1.
564
565 /// Render a G_FCONSTANT operator as a sign-extended immediate.
566 /// - NewInsnID(ULEB128) - Instruction ID to modify
567 /// - OldInsnID(ULEB128) - Instruction ID to copy from
568 /// The operand index is implicitly 1.
570
571 /// Constrain an instruction operand to a register class.
572 /// - InsnID(ULEB128) - Instruction ID to modify
573 /// - OpIdx(ULEB128) - Operand index
574 /// - RCEnum(2) - Register class enumeration value
576
577 /// Constrain an instructions operands according to the instruction
578 /// description.
579 /// - InsnID(ULEB128) - Instruction ID to modify
581 /// GIR_ConstrainSelectedInstOperands but InsnID is omitted and defaults to
582 /// zero.
584
585 /// Merge all memory operands into instruction.
586 /// - InsnID(ULEB128) - Instruction ID to modify
587 /// - NumInsnID(1) - Number of instruction IDs following this argument
588 /// - MergeInsnID(ULEB128)... - One or more Instruction ID to merge into the
589 /// result.
591
592 /// Erase from parent.
593 /// - InsnID(ULEB128) - Instruction ID to erase
595
596 /// Combines both a GIR_EraseFromParent 0 + GIR_Done
598
599 /// Create a new temporary register that's not constrained.
600 /// - TempRegID(ULEB128) - The temporary register ID to initialize.
601 /// - Ty(1) - Expected type
603
604 /// Create a new temporary register that's not constrained where the type
605 /// depends on the HwMode.
606 /// - TempRegID(ULEB128) - The temporary register ID to initialize.
607 /// - Idx(1) - Index passed to getLLTForHwMode
609
610 /// Replaces all references to a register from an instruction
611 /// with another register from another instruction.
612 /// - OldInsnID(ULEB128)
613 /// - OldOpIdx(ULEB128)
614 /// - NewInsnID(ULEB128)
615 /// - NewOpIdx(ULEB128)
617
618 /// Replaces all references to a register with a temporary register.
619 /// - OldInsnID(ULEB128)
620 /// - OldOpIdx(ULEB128)
621 /// - TempRegIdx(ULEB128)
623
624 /// A successful emission
626
627 /// Increment the rule coverage counter.
628 /// - RuleID(4) - The ID of the rule that was covered.
630
631 /// Keeping track of the number of the GI opcodes. Must be the last entry.
633};
634
635/// Provides the logic to execute GlobalISel match tables, which are used by the
636/// instruction selector and instruction combiners as their engine to match and
637/// apply MIR patterns.
639public:
640 virtual ~GIMatchTableExecutor() = default;
641
644 MachineFunction *MF = nullptr;
647 // For some predicates, we need to track the current MBB.
649
650 // HwMode of the current subtarget. Used by getLLTForHwMode.
651 unsigned HwMode = 0;
652
654
655 /// Setup per-MF executor state.
657 CodeGenCoverage *covinfo = nullptr,
658 ProfileSummaryInfo *psi = nullptr,
659 BlockFrequencyInfo *bfi = nullptr) {
660 CoverageInfo = covinfo;
661 VT = vt;
662 MF = &mf;
663 PSI = psi;
664 BFI = bfi;
665 CurMBB = nullptr;
668 }
669
670protected:
672 std::optional<SmallVector<std::function<void(MachineInstrBuilder &)>, 4>>;
675
677 std::vector<ComplexRendererFns::value_type> Renderers;
680 /// Named operands that predicate with 'let PredicateCodeUsesOperands = 1'
681 /// referenced in its argument list. Operands are inserted at index set by
682 /// emitter, it corresponds to the order in which names appear in argument
683 /// list. Currently such predicates don't have more then 3 arguments.
684 std::array<const MachineOperand *, 3> RecordedOperands;
685
686 /// Types extracted from an instruction's operand.
687 /// Whenever a type index is negative, we look here instead.
689
690 LLVM_ABI MatcherState(unsigned MaxRenderers);
691 };
692
694 const auto &F = MF->getFunction();
695 if (F.hasOptSize())
696 return true;
697 if (CurMBB)
698 if (auto *BB = CurMBB->getBasicBlock())
700 return false;
701 }
702
703public:
704 template <class PredicateBitset, class ComplexMatcherMemFn,
705 class CustomRendererFn>
706 struct ExecInfoTy {
707 ExecInfoTy(const LLT *TypeObjects, size_t NumTypeObjects,
708 const PredicateBitset *FeatureBitsets,
709 const ComplexMatcherMemFn *ComplexPredicates,
710 const CustomRendererFn *CustomRenderers)
714
715 for (size_t I = 0; I < NumTypeObjects; ++I)
716 TypeIDMap[TypeObjects[I].getUniqueRAWLLTData()] = I;
717 }
719 const PredicateBitset *FeatureBitsets;
720 const ComplexMatcherMemFn *ComplexPredicates;
721 const CustomRendererFn *CustomRenderers;
722
724 };
725
726protected:
728
729 /// Execute a given matcher table and return true if the match was successful
730 /// and false otherwise.
731 template <class TgtExecutor, class PredicateBitset, class ComplexMatcherMemFn,
732 class CustomRendererFn>
733 bool executeMatchTable(TgtExecutor &Exec, MatcherState &State,
734 const ExecInfoTy<PredicateBitset, ComplexMatcherMemFn,
735 CustomRendererFn> &ExecInfo,
736 MachineIRBuilder &Builder, const uint8_t *MatchTable,
738 const TargetRegisterInfo &TRI,
739 const RegisterBankInfo &RBI,
740 const PredicateBitset &AvailableFeatures,
742
743 virtual const uint8_t *getMatchTable() const {
744 llvm_unreachable("Should have been overridden by tablegen if used");
745 }
746
747 virtual LLT getLLTForHwMode(unsigned Index) const {
748 llvm_unreachable("Should have been overridden by tablegen if used");
749 }
750
751 virtual bool testImmPredicate_I64(unsigned, int64_t) const {
753 "Subclasses must override this with a tablegen-erated function");
754 }
755 virtual bool testImmPredicate_APInt(unsigned, const APInt &) const {
757 "Subclasses must override this with a tablegen-erated function");
758 }
759 virtual bool testImmPredicate_APFloat(unsigned, const APFloat &) const {
761 "Subclasses must override this with a tablegen-erated function");
762 }
763 virtual bool testMIPredicate_MI(unsigned, const MachineInstr &,
764 const MatcherState &State) const {
766 "Subclasses must override this with a tablegen-erated function");
767 }
768
769 virtual bool testMOPredicate_MO(unsigned, const MachineOperand &,
770 const MatcherState &State) const {
772 "Subclasses must override this with a tablegen-erated function");
773 }
774
775 virtual bool testSimplePredicate(unsigned) const {
776 llvm_unreachable("Subclass does not implement testSimplePredicate!");
777 }
778
779 virtual bool runCustomAction(unsigned, const MatcherState &State,
780 NewMIVector &OutMIs) const {
781 llvm_unreachable("Subclass does not implement runCustomAction!");
782 }
783
784 virtual uint32_t getRootFlagsToDrop() const { return 0; }
785
786 LLVM_ABI bool isOperandImmEqual(const MachineOperand &MO, int64_t Value,
788 bool Splat = false) const;
789
790 /// Return true if the specified operand is a G_PTR_ADD with a G_CONSTANT on
791 /// the right-hand side. GlobalISel's separation of pointer and integer types
792 /// means that we don't need to worry about G_OR with equivalent semantics.
794 const MachineRegisterInfo &MRI) const;
795
796 /// Return true if MI can obviously be folded into IntoMI.
797 /// MI and IntoMI do not need to be in the same basic blocks, but MI must
798 /// preceed IntoMI.
800 MachineInstr &IntoMI) const;
801
802 template <typename Ty> static Ty readBytesAs(const uint8_t *MatchTable) {
803 Ty Ret;
804 memcpy(&Ret, MatchTable, sizeof(Ret));
805 return Ret;
806 }
807
809 unsigned FirstVarOp) {
810 auto Operands = drop_begin(MI.operands(), FirstVarOp);
811 return {Operands.begin(), Operands.end()};
812 }
813
814public:
815 // Faster ULEB128 decoder tailored for the Match Table Executor.
816 //
817 // - Arguments are fixed to avoid mid-function checks.
818 // - Unchecked execution, assumes no error.
819 // - Fast common case handling (1 byte values).
822 uint64_t &CurrentIdx) {
823 uint64_t Value = MatchTable[CurrentIdx++];
824 if (LLVM_UNLIKELY(Value >= 128)) {
825 Value &= 0x7f;
826 unsigned Shift = 7;
827 do {
828 uint64_t Slice = MatchTable[CurrentIdx] & 0x7f;
829 Value += Slice << Shift;
830 Shift += 7;
831 } while (MatchTable[CurrentIdx++] >= 128);
832 }
833 return Value;
834 }
835};
836
837} // end namespace llvm
838
839#endif // LLVM_CODEGEN_GLOBALISEL_GIMATCHTABLEEXECUTOR_H
uint64_t uint64_t
#define LLVM_UNLIKELY(EXPR)
Definition Compiler.h:348
#define LLVM_ATTRIBUTE_ALWAYS_INLINE
LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do so, mark a method "always...
Definition Compiler.h:376
#define LLVM_ATTRIBUTE_RESTRICT
LLVM_ATTRIBUTE_RESTRICT - Annotates a pointer to tell the compiler that it is not aliased in the curr...
Definition Compiler.h:410
#define LLVM_ABI
Definition Compiler.h:211
This file defines the DenseMap class.
This file defines the DenseSet and SmallDenseSet classes.
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
Implement a low-level type suitable for MachineInstr level instruction selection.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Register const TargetRegisterInfo * TRI
MachineRegisterInfo & MRI
SI Fold Operands
This file defines the SmallVector class.
Class for arbitrary precision integers.
Definition APInt.h:78
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
virtual bool testSimplePredicate(unsigned) const
bool executeMatchTable(TgtExecutor &Exec, MatcherState &State, const ExecInfoTy< PredicateBitset, ComplexMatcherMemFn, CustomRendererFn > &ExecInfo, MachineIRBuilder &Builder, const uint8_t *MatchTable, const TargetInstrInfo &TII, MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI, const RegisterBankInfo &RBI, const PredicateBitset &AvailableFeatures, CodeGenCoverage *CoverageInfo) const
Execute a given matcher table and return true if the match was successful and false otherwise.
virtual bool testImmPredicate_APFloat(unsigned, const APFloat &) const
virtual bool testMOPredicate_MO(unsigned, const MachineOperand &, const MatcherState &State) const
virtual uint32_t getRootFlagsToDrop() const
virtual const uint8_t * getMatchTable() const
virtual bool testImmPredicate_APInt(unsigned, const APInt &) const
virtual bool testMIPredicate_MI(unsigned, const MachineInstr &, const MatcherState &State) const
LLVM_ABI bool isBaseWithConstantOffset(const MachineOperand &Root, const MachineRegisterInfo &MRI) const
Return true if the specified operand is a G_PTR_ADD with a G_CONSTANT on the right-hand side.
virtual bool testImmPredicate_I64(unsigned, int64_t) const
SmallVector< MachineInstrBuilder, 4 > NewMIVector
static Ty readBytesAs(const uint8_t *MatchTable)
virtual LLT getLLTForHwMode(unsigned Index) const
std::optional< SmallVector< std::function< void(MachineInstrBuilder &)>, 4 > > ComplexRendererFns
SmallVector< MachineInstr *, 4 > RecordedMIVector
bool shouldOptForSize(const MachineFunction *MF) const
static LLVM_ATTRIBUTE_ALWAYS_INLINE uint64_t fastDecodeULEB128(const uint8_t *LLVM_ATTRIBUTE_RESTRICT MatchTable, uint64_t &CurrentIdx)
LLVM_ABI bool isOperandImmEqual(const MachineOperand &MO, int64_t Value, const MachineRegisterInfo &MRI, bool Splat=false) const
virtual ~GIMatchTableExecutor()=default
LLVM_ABI bool isObviouslySafeToFold(MachineInstr &MI, MachineInstr &IntoMI) const
Return true if MI can obviously be folded into IntoMI.
virtual void setupMF(MachineFunction &mf, GISelValueTracking *vt, CodeGenCoverage *covinfo=nullptr, ProfileSummaryInfo *psi=nullptr, BlockFrequencyInfo *bfi=nullptr)
Setup per-MF executor state.
virtual bool runCustomAction(unsigned, const MatcherState &State, NewMIVector &OutMIs) const
static ArrayRef< MachineOperand > getRemainingOperands(const MachineInstr &MI, unsigned FirstVarOp)
virtual void setupGeneratedPerFunctionState(MachineFunction &MF)=0
virtual unsigned getHwMode(enum HwModeType type=HwMode_Default) const
HwMode ID corresponding to the 'type' parameter is retrieved from the HwMode bit set of the current s...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Helper class to build MachineInstr.
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Analysis providing profile information.
Holds all the information related to register banks.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
LLVM Value Representation.
Definition Value.h:75
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:316
LLVM_ABI bool shouldOptimizeForSize(const MachineFunction *MF, ProfileSummaryInfo *PSI, const MachineBlockFrequencyInfo *BFI, PGSOQueryType QueryType=PGSOQueryType::Other)
Returns true if machine function MF is suggested to be size-optimized based on the profile.
@ GICXXCustomAction_Invalid
@ GIR_AddIntrinsicID
Adds an intrinsic ID to the specified instruction.
@ GIR_ComplexRenderer
Render complex operands to the specified instruction.
@ GIR_ReplaceRegWithTempReg
Replaces all references to a register with a temporary register.
@ GIR_ComplexSubOperandRenderer
Render sub-operands of complex operands to the specified instruction.
@ GIR_MakeTempReg
Create a new temporary register that's not constrained.
@ GIM_CheckTypeByHwMode
Check the type for the specified operand where the type depends on the HwMode.
@ GIM_CheckMemorySizeEqualTo
Check the size of the memory access for the given machine memory operand.
@ GIR_MakeTempRegByHwMode
Create a new temporary register that's not constrained where the type depends on the HwMode.
@ GIM_RootCheckType
GIM_CheckType but InsnID is omitted and defaults to zero.
@ GIM_RootCheckRegBankForClass
GIM_CheckRegBankForClass but InsnID is omitted and defaults to zero.
@ GIR_Done
A successful emission.
@ GIM_RecordNamedOperand
Predicates with 'let PredicateCodeUsesOperands = 1' need to examine some named operands that will be ...
@ GIM_Try
Begin a try-block to attempt a match and jump to OnFail if it is unsuccessful.
@ GIR_RootConstrainSelectedInstOperands
GIR_ConstrainSelectedInstOperands but InsnID is omitted and defaults to zero.
@ GIM_CheckIsBuildVectorAllOnes
Check if this is a vector that can be treated as a vector splat constant.
@ GIM_CheckNumOperands
Check the instruction has the right number of operands.
@ GIR_AddCImm
Add an CImm to the specified instruction.
@ GIR_AddCFPImm
Add a floating-point immediate to the specified instruction.
@ GIR_ConstrainOperandRC
Constrain an instruction operand to a register class.
@ GIM_CheckI64ImmPredicate
Check an immediate predicate on the specified instruction.
@ GIR_AddImplicitDef
Add an implicit register def to the specified instruction.
@ GIM_CheckAPIntImmPredicate
Check an immediate predicate on the specified instruction via an APInt.
@ GIM_CheckHasNoUse
Check if there's no use of the first result.
@ GIM_CheckPointerToAny
Check the type of a pointer to any address space.
@ GIM_CheckMemorySizeEqualToLLT
Check the size of the memory access for the given machine memory operand against the size of an opera...
@ GIM_CheckComplexPattern
Check the operand matches a complex predicate.
@ GIR_CopyConstantAsSImm
Render a G_CONSTANT operator as a sign-extended immediate.
@ GIR_EraseFromParent
Erase from parent.
@ GIM_SwitchType
Switch over the LLT on the specified instruction operand.
@ GIR_CopySubReg
Copy an operand to the specified instruction.
@ GIR_MutateOpcode
Mutate an instruction.
@ GIM_CheckIsBuildVectorAllZeros
@ GIM_CheckAtomicOrderingOrStrongerThan
@ GIR_AddRegister
Add an register to the specified instruction.
@ GIR_AddTempSubRegister
Add a temporary register to the specified instruction.
@ GIM_CheckIsSafeToFold
Checks if the matched instructions numbered [1, 1+N) can be folded into the root (inst 0).
@ GIM_RootCheckTypeByHwMode
GIM_CheckTypeByHwMode but InsnID is omitted and defaults to zero.
@ GIM_CheckOpcode
Check the opcode on the specified instruction.
@ GIR_ReplaceReg
Replaces all references to a register from an instruction with another register from another instruct...
@ GIM_CheckPointerToAnyByHwMode
Check the type of a pointer to any address space where the size of the pointer depends on the HwMode.
@ GIM_SwitchOpcode
Switch over the opcode on the specified instruction.
@ GIM_CheckAPFloatImmPredicate
Check a floating point immediate predicate on the specified instruction.
@ GIM_CheckAllSameOperandIgnoreCopies
@ GIM_Reject
Fail the current try-block, or completely fail to match if there is no current try-block.
@ GIR_AddSimpleTempRegister
Add a temporary register to the specified instruction without setting any flags.
@ GIR_AddTempRegister
Add a temporary register to the specified instruction.
@ GIR_Copy
Copy an operand to the specified instruction.
@ GIR_AddImm
Add an immediate to the specified instruction.
@ GIR_CopyFConstantAsFPImm
Render a G_FCONSTANT operator as a sign-extended immediate.
@ GIU_NumOpcodes
Keeping track of the number of the GI opcodes. Must be the last entry.
@ GIR_CopyRemaining
Copies all operand starting from OpIdx in OldInsnID into the new instruction NewInsnID.
@ GIM_MIFlags
Check that a matched instruction has, or doesn't have a MIFlag.
@ GIR_CopyOrAddZeroReg
Copy an operand to the specified instruction or add a zero register if the operand is a zero immediat...
@ GIM_CheckMemoryAlignment
Check the minimum alignment of the memory access for the given machine memory operand.
@ GIM_CheckIsSameOperand
Check the specified operands are identical.
@ GIR_AddImm8
Add signed 8 bit immediate to the specified instruction.
@ GIM_CheckIsSameOperandIgnoreCopies
@ GIM_CheckIsMBB
Check the specified operand is an MBB.
@ GIM_CheckNumOperandsLE
Check the instruction has a number of operands <= or >= than given number.
@ GIM_Try_CheckFeatures
GIM_Try only if the feature bits match.
@ GIM_CheckMemorySizeGreaterThanLLT
@ GIM_CheckRegBankForClass
Check the register bank for the specified operand.
@ GIM_CheckLiteralInt
Check the operand is a specific literal integer (i.e.
@ GIM_CheckMemorySizeLessThanLLT
@ GIM_RecordRegType
Records an operand's register type into the set of temporary types.
@ GIM_CheckLeafOperandPredicate
Check a leaf predicate on the specified instruction.
@ GIM_CheckHasOneUse
Check if there's one use of the first result.
@ GIR_EraseRootFromParent_Done
Combines both a GIR_EraseFromParent 0 + GIR_Done.
@ GIR_CopyMIFlags
Copy the MIFlags of a matched instruction into an output instruction.
@ GIR_DoneWithCustomAction
Calls a C++ function that concludes the current match.
@ GIR_BuildMI
Build a new instruction.
@ GIM_RecordInsn
Record the specified instruction.
@ GIM_CheckIsImm
Check the specified operand is an Imm.
@ GIR_BuildRootMI
GIR_BuildMI but InsnID is omitted and defaults to zero.
@ GIM_CheckCanReplaceReg
Check we can replace all uses of a register with another.
@ GIM_CheckMemoryAddressSpace
Check the address space of the memory access for the given machine memory operand.
@ GIR_CustomRenderer
Render operands to the specified instruction using a custom function.
@ GIM_CheckAtomicOrdering
Check a memory operation has the specified atomic ordering.
@ GIM_CheckType
Check the type for the specified operand.
@ GIM_CheckConstantInt8
Check the operand is a specific 8-bit signed integer.
@ GIM_CheckCmpPredicate
Check the operand is a specific predicate.
@ GIM_CheckOpcodeIsEither
Check the opcode on the specified instruction, checking 2 acceptable alternatives.
@ GIR_SetImplicitDefDead
Marks the implicit def of a register as dead.
@ GIR_BuildConstant
Builds a constant and stores its result in a TempReg.
@ GIR_AddImplicitUse
Add an implicit register use to the specified instruction.
@ GIR_Coverage
Increment the rule coverage counter.
@ GIR_MergeMemOperands
Merge all memory operands into instruction.
@ GIM_CheckImmOperandPredicate
Check an immediate predicate on the specified instruction.
@ GIM_CheckAtomicOrderingWeakerThan
@ GIR_SetMIFlags
Set or unset a MIFlag on an instruction.
@ GIM_CheckIntrinsicID
Check the operand is a specific intrinsic ID.
@ GIM_CheckConstantInt
Check the operand is a specific integer.
@ GIM_SwitchTypeShape
Switch over the shape of an LLT on the specified instruction operand.
@ GIR_RootToRootCopy
GIR_Copy but with both New/OldInsnIDs omitted and defaulting to zero.
@ GIR_ComplexSubOperandSubRegRenderer
Render subregisters of suboperands of complex operands to the specified instruction.
@ GIM_RecordInsnIgnoreCopies
@ GIR_CustomOperandRenderer
Render operands to the specified instruction using a custom function, reading from a specific operand...
@ GIR_ConstrainSelectedInstOperands
Constrain an instructions operands according to the instruction description.
@ GIM_CheckCxxInsnPredicate
Check a generic C++ instruction predicate.
@ GIM_CheckSimplePredicate
Check a trivial predicate which takes no arguments.
SmallDenseMap< uint64_t, unsigned, 64 > TypeIDMap
ExecInfoTy(const LLT *TypeObjects, size_t NumTypeObjects, const PredicateBitset *FeatureBitsets, const ComplexMatcherMemFn *ComplexPredicates, const CustomRendererFn *CustomRenderers)
const ComplexMatcherMemFn * ComplexPredicates
std::array< const MachineOperand *, 3 > RecordedOperands
Named operands that predicate with 'let PredicateCodeUsesOperands = 1' referenced in its argument lis...
SmallVector< LLT, 4 > RecordedTypes
Types extracted from an instruction's operand.
LLVM_ABI MatcherState(unsigned MaxRenderers)
DenseMap< unsigned, Register > TempRegisters
std::vector< ComplexRendererFns::value_type > Renderers