brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.3 KiB · 5513824 Raw
113 lines · plain
1 2.. _instructionselect:3 4InstructionSelect5-----------------6 7This pass transforms generic machine instructions into equivalent8target-specific instructions.  9 10The legacy instruction selector, SelectionDAG, iterated over each function's 11basic block and constructed a dataflow graph. Every backend defines 12tree patterns in the ``XXXInstrInfo.td``. The legacy selector started13at the bottom and replaced the SDNodes greedily. 14 15The GlobalISel's instruction selector traverses the ``MachineFunction`` 16bottom-up, selecting uses before definitions, enabling trivial dead code 17elimination. It does that by iterating over the basic blocks in post-order. 18Each gMIR instruction is then replaced by a MIR instruction when a matching 19pattern is found. So, when there is a 1:1 mapping between gMIR and MIR, where 20is the benefit of the global scope? Even in the case of a 1:1 mapping, 21GlobalISel includes a combiner that can match and fuse multiple gMIR 22instructions. The scope of the combination is not limited to a basic block, 23but can extend across the entire function.24 25.. _api-instructionselector:26 27API: InstructionSelector28^^^^^^^^^^^^^^^^^^^^^^^^29 30The target implements the ``InstructionSelector`` class, containing the31target-specific selection logic proper.32 33The instance is provided by the subtarget, so that it can specialize the34selector by subtarget feature (with, e.g., a vector selector overriding parts35of a general-purpose common selector).36We might also want to parameterize it by MachineFunction, to enable selector37variants based on function attributes like optsize.38 39The simple API consists of:40 41  .. code-block:: c++42 43    virtual bool select(MachineInstr &MI)44 45This target-provided method is responsible for mutating (or replacing) a46possibly-generic MI into a fully target-specific equivalent.47It is also responsible for doing the necessary constraining of gvregs into the48appropriate register classes as well as passing through COPY instructions to49the register allocator.50 51The ``InstructionSelector`` can fold other instructions into the selected MI,52by walking the use-def chain of the vreg operands.53As GlobalISel is Global, this folding can occur across basic blocks.54 55SelectionDAG Rule Imports56^^^^^^^^^^^^^^^^^^^^^^^^^57 58TableGen will import SelectionDAG rules and provide the following function to59execute them:60 61  .. code-block:: c++62 63    bool selectImpl(MachineInstr &MI)64 65The ``--stats`` option can be used to determine what proportion of rules were66successfully imported. The easiest way to use this is to copy the67``-gen-globalisel`` tablegen command from ``ninja -v`` and modify it.68 69Similarly, the ``--warn-on-skipped-patterns`` option can be used to obtain the70reasons that rules weren't imported. This can be used to focus on the most71important rejection reasons.72 73PatLeaf Predicates74^^^^^^^^^^^^^^^^^^75 76PatLeafs cannot be imported because their C++ is implemented in terms of77``SDNode`` objects. PatLeafs that handle immediate predicates should be78replaced by ``ImmLeaf``, ``IntImmLeaf``, or ``FPImmLeaf`` as appropriate.79 80There's no standard answer for other PatLeafs. Some standard predicates have81been baked into TableGen but this should not generally be done.82 83Custom SDNodes84^^^^^^^^^^^^^^85 86Custom SDNodes should be mapped to Target Pseudos using ``GINodeEquiv``. This87will cause the instruction selector to import them but you will also need to88ensure the target pseudo is introduced to the MIR before the instruction89selector. Any preceding pass is suitable but the legalizer will be a90particularly common choice.91 92ComplexPatterns93^^^^^^^^^^^^^^^94 95ComplexPatterns cannot be imported because their C++ is implemented in terms of96``SDNode`` objects. GlobalISel versions should be defined with97``GIComplexOperandMatcher`` and mapped to ComplexPattern with98``GIComplexPatternEquiv``.99 100The following predicates are useful for porting ComplexPattern:101 102* isBaseWithConstantOffset() - Check for base+offset structures103* isOperandImmEqual() - Check for a particular constant104* isObviouslySafeToFold() - Check for reasons an instruction can't be sunk and folded into another.105 106There are some important points for the C++ implementation:107 108* Don't modify MIR in the predicate109* Renderer lambdas should capture by value to avoid use-after-free. They will be used after the predicate returns.110* Only create instructions in a renderer lambda. GlobalISel won't clean up things you create but don't use.111 112 113