For instruction selection via the selection DAG, we added patterns to the target description, which use DAG operations and operands. To reuse those patterns, a mapping from DAG node types to generic machine instructions was introduced. For example, the DAG and operation maps to the generic G_AND machine instruction. Not all DAG operations have an equivalent generic machine instruction; however, the most common cases are covered. Therefore, it is beneficial to define all code selection patterns in the target description.
Most of the implementation of the M88kInstructionSelector class, which can be found in the GISel/M88kInstructionSelector.cpp file, is generated from the target description. However, we need to override the select() method, which allows us to translate generic machine instructions that are not covered by the patterns in the target description. Since we only support a very small subset of generic instructions, we can simply call the generated pattern matcher:
bool M88kInstructionSelector::select(MachineInstr &I) {
if (selectImpl(I, *CoverageInfo))
return true;
return false;
}
With the instruction selection implemented, we can translate LLVM IR using GlobalISel!
Running an example
To translate LLVM IR using GlobalISel, we need to add the -global-isel option to the command line of llc. For example, you can use the previously defined IR file, and.ll:
$ llc -mtriple m88k-openbsd -global-isel < and.ll
The printed assembly text is the same. To convince ourselves that the translation uses GlobalISel, we must take advantage of the fact that we can stop the translation after a specified pass is run with the -stop-after= option. For example, to see the generic instructions after legalization, you would run the following command:
$ llc -mtriple m88k-openbsd -global-isel < and.ll \
-stop-after=legalizer
The ability to stop after (or before) a pass is run is another advantage of GlobalISel because it makes it easy to debug and test the implementation.
At this point, we have a working backend that can translate some LLVM IR into machine code for the m88k architecture. Let’s think about how to move from here to a more complete backend.