annotate llvm/docs/GlobalISel/InstructionSelect.rst @ 201:a96fbbdf2d0f

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Fri, 04 Jun 2021 21:07:06 +0900
parents 1d019706d866
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1
anatofuz
parents:
diff changeset
2 .. _instructionselect:
anatofuz
parents:
diff changeset
3
anatofuz
parents:
diff changeset
4 InstructionSelect
anatofuz
parents:
diff changeset
5 -----------------
anatofuz
parents:
diff changeset
6
anatofuz
parents:
diff changeset
7 This pass transforms generic machine instructions into equivalent
anatofuz
parents:
diff changeset
8 target-specific instructions. It traverses the ``MachineFunction`` bottom-up,
anatofuz
parents:
diff changeset
9 selecting uses before definitions, enabling trivial dead code elimination.
anatofuz
parents:
diff changeset
10
anatofuz
parents:
diff changeset
11 .. _api-instructionselector:
anatofuz
parents:
diff changeset
12
anatofuz
parents:
diff changeset
13 API: InstructionSelector
anatofuz
parents:
diff changeset
14 ^^^^^^^^^^^^^^^^^^^^^^^^
anatofuz
parents:
diff changeset
15
anatofuz
parents:
diff changeset
16 The target implements the ``InstructionSelector`` class, containing the
anatofuz
parents:
diff changeset
17 target-specific selection logic proper.
anatofuz
parents:
diff changeset
18
anatofuz
parents:
diff changeset
19 The instance is provided by the subtarget, so that it can specialize the
anatofuz
parents:
diff changeset
20 selector by subtarget feature (with, e.g., a vector selector overriding parts
anatofuz
parents:
diff changeset
21 of a general-purpose common selector).
anatofuz
parents:
diff changeset
22 We might also want to parameterize it by MachineFunction, to enable selector
anatofuz
parents:
diff changeset
23 variants based on function attributes like optsize.
anatofuz
parents:
diff changeset
24
anatofuz
parents:
diff changeset
25 The simple API consists of:
anatofuz
parents:
diff changeset
26
anatofuz
parents:
diff changeset
27 .. code-block:: c++
anatofuz
parents:
diff changeset
28
anatofuz
parents:
diff changeset
29 virtual bool select(MachineInstr &MI)
anatofuz
parents:
diff changeset
30
anatofuz
parents:
diff changeset
31 This target-provided method is responsible for mutating (or replacing) a
anatofuz
parents:
diff changeset
32 possibly-generic MI into a fully target-specific equivalent.
anatofuz
parents:
diff changeset
33 It is also responsible for doing the necessary constraining of gvregs into the
anatofuz
parents:
diff changeset
34 appropriate register classes as well as passing through COPY instructions to
anatofuz
parents:
diff changeset
35 the register allocator.
anatofuz
parents:
diff changeset
36
anatofuz
parents:
diff changeset
37 The ``InstructionSelector`` can fold other instructions into the selected MI,
anatofuz
parents:
diff changeset
38 by walking the use-def chain of the vreg operands.
anatofuz
parents:
diff changeset
39 As GlobalISel is Global, this folding can occur across basic blocks.
anatofuz
parents:
diff changeset
40
anatofuz
parents:
diff changeset
41 SelectionDAG Rule Imports
anatofuz
parents:
diff changeset
42 ^^^^^^^^^^^^^^^^^^^^^^^^^
anatofuz
parents:
diff changeset
43
anatofuz
parents:
diff changeset
44 TableGen will import SelectionDAG rules and provide the following function to
anatofuz
parents:
diff changeset
45 execute them:
anatofuz
parents:
diff changeset
46
anatofuz
parents:
diff changeset
47 .. code-block:: c++
anatofuz
parents:
diff changeset
48
anatofuz
parents:
diff changeset
49 bool selectImpl(MachineInstr &MI)
anatofuz
parents:
diff changeset
50
anatofuz
parents:
diff changeset
51 The ``--stats`` option can be used to determine what proportion of rules were
anatofuz
parents:
diff changeset
52 successfully imported. The easiest way to use this is to copy the
anatofuz
parents:
diff changeset
53 ``-gen-globalisel`` tablegen command from ``ninja -v`` and modify it.
anatofuz
parents:
diff changeset
54
anatofuz
parents:
diff changeset
55 Similarly, the ``--warn-on-skipped-patterns`` option can be used to obtain the
anatofuz
parents:
diff changeset
56 reasons that rules weren't imported. This can be used to focus on the most
anatofuz
parents:
diff changeset
57 important rejection reasons.
anatofuz
parents:
diff changeset
58
anatofuz
parents:
diff changeset
59 PatLeaf Predicates
anatofuz
parents:
diff changeset
60 ^^^^^^^^^^^^^^^^^^
anatofuz
parents:
diff changeset
61
anatofuz
parents:
diff changeset
62 PatLeafs cannot be imported because their C++ is implemented in terms of
anatofuz
parents:
diff changeset
63 ``SDNode`` objects. PatLeafs that handle immediate predicates should be
anatofuz
parents:
diff changeset
64 replaced by ``ImmLeaf``, ``IntImmLeaf``, or ``FPImmLeaf`` as appropriate.
anatofuz
parents:
diff changeset
65
anatofuz
parents:
diff changeset
66 There's no standard answer for other PatLeafs. Some standard predicates have
anatofuz
parents:
diff changeset
67 been baked into TableGen but this should not generally be done.
anatofuz
parents:
diff changeset
68
anatofuz
parents:
diff changeset
69 Custom SDNodes
anatofuz
parents:
diff changeset
70 ^^^^^^^^^^^^^^
anatofuz
parents:
diff changeset
71
anatofuz
parents:
diff changeset
72 Custom SDNodes should be mapped to Target Pseudos using ``GINodeEquiv``. This
anatofuz
parents:
diff changeset
73 will cause the instruction selector to import them but you will also need to
anatofuz
parents:
diff changeset
74 ensure the target pseudo is introduced to the MIR before the instruction
anatofuz
parents:
diff changeset
75 selector. Any preceding pass is suitable but the legalizer will be a
anatofuz
parents:
diff changeset
76 particularly common choice.
anatofuz
parents:
diff changeset
77
anatofuz
parents:
diff changeset
78 ComplexPatterns
anatofuz
parents:
diff changeset
79 ^^^^^^^^^^^^^^^
anatofuz
parents:
diff changeset
80
anatofuz
parents:
diff changeset
81 ComplexPatterns cannot be imported because their C++ is implemented in terms of
anatofuz
parents:
diff changeset
82 ``SDNode`` objects. GlobalISel versions should be defined with
anatofuz
parents:
diff changeset
83 ``GIComplexOperandMatcher`` and mapped to ComplexPattern with
anatofuz
parents:
diff changeset
84 ``GIComplexPatternEquiv``.
anatofuz
parents:
diff changeset
85
anatofuz
parents:
diff changeset
86 The following predicates are useful for porting ComplexPattern:
anatofuz
parents:
diff changeset
87
anatofuz
parents:
diff changeset
88 * isBaseWithConstantOffset() - Check for base+offset structures
anatofuz
parents:
diff changeset
89 * isOperandImmEqual() - Check for a particular constant
anatofuz
parents:
diff changeset
90 * isObviouslySafeToFold() - Check for reasons an instruction can't be sunk and folded into another.
anatofuz
parents:
diff changeset
91
anatofuz
parents:
diff changeset
92 There are some important points for the C++ implementation:
anatofuz
parents:
diff changeset
93
anatofuz
parents:
diff changeset
94 * Don't modify MIR in the predicate
anatofuz
parents:
diff changeset
95 * Renderer lambdas should capture by value to avoid use-after-free. They will be used after the predicate returns.
anatofuz
parents:
diff changeset
96 * Only create instructions in a renderer lambda. GlobalISel won't clean up things you create but don't use.
anatofuz
parents:
diff changeset
97
anatofuz
parents:
diff changeset
98