Instruction Selection

The SelectionDAG at this phase is optimized and legalized. However, the instructions are still not in machine code form. These instructions need to be mapped to architecture-specific instructions in the SelectionDAG itself. The TableGen class helps select target-specific instructions.

The CodeGenAndEmitDAG() function calls the DoInstructionSelection() function that visits each DAG node and calls the Select() function for each node. The Select() function is the main hook targets implement to select a node. The Select() function is a virtual method to be implemented by the targets.

For consideration, assume our target architecture is X86. The X86DAGToDAGISel::Select() function intercepts some nodes for manual matching, but delegates the bulk of the work to the X86DAGToDAGISel::SelectCode() function. The X86DAGToDAGISel::SelectCode() function is auto generated by TableGen. It contains the matcher table, followed by a call to the generic SelectionDAGISel::SelectCodeCommon() function, passing it the table.

SDNode *ResNode = SelectCode(Node);

For example, consider the following:

$ cat test.ll
define i32 @test(i32 %a, i32 %b, i32 %c) {
%add = add nsw i32 %a, %b
%div = sdiv i32 %add, %c
ret i32 %div
}

Before instruction selection, the SDAG looks like the following:

$ llc –view-isel-dags test.ll
Instruction Selection

After Instruction Selection, SDAG looks like the following:

$ llc –view-sched-dags test.ll
Instruction Selection
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.144.56