Legalizing the generic machine instructions – Instruction Selection
By Peggy Johnston / August 28, 2024 / No Comments / Adding the M88k backend to LLVM, Creating the disassembler, Emitting machine instructions, Exams of IT, ITCertification Exams
The translation from LLVM IR to generic machine code is mostly fixed. As a result, instructions can be generated that use unsupported data types, among other challenges. The task of the legalizer pass is to define which operations and instructions are legal. With this information, the GlobalISel framework tries to transform the instructions into a legal form. For example, the m88k architecture only has 32-bit registers, so a bitwise and operation with 64-bit values is not legal. However, if we split the 64-bit value into two 32-bit values, and use two bitwise and operations instead, then we have legal code. This can be translated into a legalization rule:
getActionDefinitionsBuilder({G_AND, G_OR, G_XOR})
.legalFor({S32})
.clampScalar(0, S32, S32);
Whenever the legalizer pass processes a G_AND instruction, then it is legal if all operands are 32-bit wide. Otherwise, the operands are clamped to 32-bit, effectively splitting larger values into multiple 32-bit values, and the rule is applied again. If an instruction cannot be legalized, then the backend terminates with an error message.
All legalization rules are defined in the constructor of the M88kLegalizerInfo class, which makes the class very simple.
What does legal mean?
In GlobalISel, a generic instruction is legal if it can be translated by the instruction selector. This gives us more freedom in the implementation. For example, we can state that an instruction works on a bit value, even if the hardware only operates on 32-bit values, so long as the instruction selector can handle the type correctly.
The next pass we need to look at is the register bank selector.