global instruction selection -...

82
Global Instruction Selection Quentin Colombet Apple A Proposal

Upload: others

Post on 31-May-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

Global Instruction Selection

Quentin Colombet Apple

A Proposal

Page 2: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

What Is Instruction Selection?

Translation from target independent intermediate representation (IR) to target specific IR.

LLVM IR -> MachineInstr

2

Page 3: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

SelectionDAG (SD) ISel

MachineInstr

LLVM IR

FastISelFail

FastPath

SDBuilder

DAGCombiner

Legalize*

DAGCombiner

Select

Schedule

GoodPath

TargetHooks

SDNode

3

Page 4: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

SelectionDAG (SD) ISel

LLVM IR (-> SDNode) -> MachineInstr

MachineInstr

LLVM IR

FastISelFail

FastPath

SDBuilder

DAGCombiner

Legalize*

DAGCombiner

Select

Schedule

GoodPath

TargetHooks

SDNode

3

Page 5: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

Problems with SDISel

• Basic block scope

• SDNode IR, specific to instruction selection

• Monolithic

4

Page 6: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

Goals

• Global • Fast • Shared code for fast and good paths • IR that represents ISA concepts better • More flexible pipeline • Easier to maintain/understand • Self contained representation • No change to LLVM IR

5

Page 7: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

Non-Goals for the Prototype

• Reuse of InstCombine

• Improve TableGen

• Support target specific features

6

Page 8: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

Why Not Fix SDISel?

• Hard limitations of the underlying representation

• Would introduce SDNode IR to optimizers

• SDNode IR can be avoided

• Inherent overhead

7

Page 9: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

Global ISel

Page 10: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

LLVM IR

9

Page 11: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

IRTranslatorLLVM IR

9

Page 12: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

IRTranslatordefine double @foo(double %val, double* %addr) { %intval = bitcast double %val to i64 %loaded = load double, double* %addr %mask = bitcast double %loaded to i64 %and = and i64 %intval, %mask %res = bitcast i64 %and to double ret double %res }

• LLVM IR to generic (G) MachineInstr

10

Page 13: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

IRTranslatordefine double @foo(double %val, double* %addr) { %intval = bitcast double %val to i64 %loaded = load double, double* %addr %mask = bitcast double %loaded to i64 %and = and i64 %intval, %mask %res = bitcast i64 %and to double ret double %res }

foo: val = VMOVDRR R0,R1 addr = COPY R2 loaded(64) = gLD (double) addr and(64) = gAND (i64) val, loaded R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

• LLVM IR to generic (G) MachineInstr • One IR instruction to 0..* (G) MachineInstr • Virtual registers have a size • ABI lowering

10

Page 14: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

IRTranslatordefine double @foo(double %val, double* %addr) { %intval = bitcast double %val to i64 %loaded = load double, double* %addr %mask = bitcast double %loaded to i64 %and = and i64 %intval, %mask %res = bitcast i64 %and to double ret double %res }

foo: val = … VMOVDRR R0,R1 addr = … COPY R2 loaded(64) = gLD (double) addr and(64) = gAND (i64) val, loaded R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

• LLVM IR to generic (G) MachineInstr • One IR instruction to 0..* (G) MachineInstr • Virtual registers have a size • ABI lowering

10

Page 15: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

IRTranslatordefine double @foo(double %val, double* %addr) { %intval = bitcast double %val to i64 %loaded = load double, double* %addr %mask = bitcast double %loaded to i64 %and = and i64 %intval, %mask %res = bitcast i64 %and to double ret double %res }

foo: val = … VMOVDRR R0,R1 addr = … COPY R2 loaded = gLD addr and(64) = gAND (i64) val, loaded R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

• LLVM IR to generic (G) MachineInstr • One IR instruction to 0..* (G) MachineInstr • Virtual registers have a size • ABI lowering

10

Page 16: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

IRTranslatordefine double @foo(double %val, double* %addr) { %intval = bitcast double %val to i64 %loaded = load double, double* %addr %mask = bitcast double %loaded to i64 %and = and i64 %intval, %mask %res = bitcast i64 %and to double ret double %res }q

foo: val = … VMOVDRR R0,R1 addr = … COPY R2 loaded = gLD addr and(64) = gAND (i64) val, loaded R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

• LLVM IR to generic (G) MachineInstr • One IR instruction to 0..* (G) MachineInstr • MachineInstrs get a type • Virtual registers get a size • ABI lowering

New

10

Page 17: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

IRTranslatordefine double @foo(double %val, double* %addr) { %intval = bitcast double %val to i64 %loaded = load double, double* %addr %mask = bitcast double %loaded to i64 %and = and i64 %intval, %mask %res = bitcast i64 %and to double ret double %res }q

foo: val = … VMOVDRR R0,R1 addr = … COPY R2 loaded = gLD (double) addr and(64) = gAND (i64) val, loaded R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

• LLVM IR to generic (G) MachineInstr • One IR instruction to 0..* (G) MachineInstr • MachineInstrs get a type • Virtual registers get a size • ABI lowering

NEW

New

10

Page 18: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

IRTranslatordefine double @foo(double %val, double* %addr) { %intval = bitcast double %val to i64 %loaded = load double, double* %addr %mask = bitcast double %loaded to i64 %and = and i64 %intval, %mask %res = bitcast i64 %and to double ret double %res }q

foo: val = … VMOVDRR R0,R1 addr = … COPY R2 loaded(64) = gLD (double) addr and(64) = gAND (i64) val, loaded R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

• LLVM IR to generic (G) MachineInstr • One IR instruction to 0..* (G) MachineInstr • MachineInstrs get a type • Virtual registers get a size • ABI lowering

NEW

NEW

10

Page 19: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

IRTranslatordefine double @foo(double %val, double* %addr) { %intval = bitcast double %val to i64 %loaded = load double, double* %addr %mask = bitcast double %loaded to i64 %and = and i64 %intval, %mask %res = bitcast i64 %and to double ret double %res }q

foo: val(64) = … VMOVDRR R0,R1 addr(32) = … COPY R2 loaded(64) = gLD (double) addr and(64) = gAND (i64) val, loaded R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

• LLVM IR to generic (G) MachineInstr • One IR instruction to 0..* (G) MachineInstr • MachineInstrs get a type • Virtual registers get a size • ABI lowering

NEW

NEW

10

Page 20: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

IRTranslatordefine double @foo(double %val, double* %addr) { %intval = bitcast double %val to i64 %loaded = load double, double* %addr %mask = bitcast double %loaded to i64 %and = and i64 %intval, %mask %res = bitcast i64 %and to double ret double %res }

foo: val(64) = … VMOVDRR R0,R1 addr(32) = … COPY R2 loaded(64) = gLD (double) addr and(64) = gAND (i64) val, loaded R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

• LLVM IR to generic (G) MachineInstr • One IR instruction to 0..* (G) MachineInstr • MachineInstrs get a type • Virtual registers get a size • ABI lowering

NEW

NEW

10

Page 21: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

IRTranslatordefine double @foo(double %val, double* %addr) { %intval = bitcast double %val to i64 %loaded = load double, double* %addr %mask = bitcast double %loaded to i64 %and = and i64 %intval, %mask %res = bitcast i64 %and to double ret double %res }

foo: val(64) = … VMOVDRR R0,R1 addr(32) = … COPY R2 loaded(64) = gLD (double) addr and(64) = gAND (i64) val, loaded R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

• LLVM IR to generic (G) MachineInstr • One IR instruction to 0..* (G) MachineInstr • MachineInstrs get a type • Virtual registers get a size • ABI lowering

NEW

NEW

10

Page 22: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

IRTranslatordefine double @foo(double %val, double* %addr) { %intval = bitcast double %val to i64 %loaded = load double, double* %addr %mask = bitcast double %loaded to i64 %and = and i64 %intval, %mask %res = bitcast i64 %and to double ret double %res }

foo: val(64) = … VMOVDRR R0,R1 addr(32) = … COPY R2 loaded(64) = gLD (double) addr and(64) = gAND (i64) val, loaded = and tBX_RET R0<imp-use>,R1<imp-use>

• LLVM IR to generic (G) MachineInstr • One IR instruction to 0..* (G) MachineInstr • MachineInstrs get a type • Virtual registers get a size • ABI lowering

NEW

NEW

10

Page 23: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

IRTranslatordefine double @foo(double %val, double* %addr) { %intval = bitcast double %val to i64 %loaded = load double, double* %addr %mask = bitcast double %loaded to i64 %and = and i64 %intval, %mask %res = bitcast i64 %and to double ret double %res }

foo: val(64) = … VMOVDRR R0,R1 addr(32) = … COPY R2 loaded(64) = gLD (double) addr and(64) = gAND (i64) val, loaded … = and tBX_RET R0<imp-use>,R1<imp-use>

• LLVM IR to generic (G) MachineInstr • One IR instruction to 0..* (G) MachineInstr • MachineInstrs get a type • Virtual registers get a size • ABI lowering

NEW

NEW

10

Page 24: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

IRTranslatordefine double @foo(double %val, double* %addr) { %intval = bitcast double %val to i64 %loaded = load double, double* %addr %mask = bitcast double %loaded to i64 %and = and i64 %intval, %mask %res = bitcast i64 %and to double ret double %res }q

foo: val(64) = … R0,R1 addr(32) = … COPY R2 loaded(64) = gLD (double) addr and(64) = gAND (i64) val, loaded … = and tBX_RET R0<imp-use>,R1<imp-use>

• LLVM IR to generic (G) MachineInstr • One IR instruction to 0..* (G) MachineInstr • MachineInstrs get a type • Virtual registers get a size • ABI lowering

NEW

NEW

10

Page 25: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

IRTranslatordefine double @foo(double %val, double* %addr) { %intval = bitcast double %val to i64 %loaded = load double, double* %addr %mask = bitcast double %loaded to i64 %and = and i64 %intval, %mask %res = bitcast i64 %and to double ret double %res }q

foo: val(64) = … R0,R1 addr(32) = … COPY R2 loaded(64) = gLD (double) addr and(64) = gAND (i64) val, loaded … = and tBX_RET R0<imp-use>,R1<imp-use>

• LLVM IR to generic (G) MachineInstr • One IR instruction to 0..* (G) MachineInstr • MachineInstrs get a type • Virtual registers get a size • ABI lowering

NEW

NEW

10

Page 26: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

IRTranslatordefine double @foo(double %val, double* %addr) { %intval = bitcast double %val to i64 %loaded = load double, double* %addr %mask = bitcast double %loaded to i64 %and = and i64 %intval, %mask %res = bitcast i64 %and to double ret double %res }q

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = … R2 loaded(64) = gLD (double) addr and(64) = gAND (i64) val, loaded … = and tBX_RET R0<imp-use>,R1<imp-use>

• LLVM IR to generic (G) MachineInstr • One IR instruction to 0..* (G) MachineInstr • MachineInstrs get a type • Virtual registers get a size • ABI lowering

NEW

NEW

10

Page 27: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

IRTranslatordefine double @foo(double %val, double* %addr) { %intval = bitcast double %val to i64 %loaded = load double, double* %addr %mask = bitcast double %loaded to i64 %and = and i64 %intval, %mask %res = bitcast i64 %and to double ret double %res }q

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr and(64) = gAND (i64) val, loaded … = and tBX_RET R0<imp-use>,R1<imp-use>

• LLVM IR to generic (G) MachineInstr • One IR instruction to 0..* (G) MachineInstr • MachineInstrs get a type • Virtual registers get a size • ABI lowering

NEW

NEW

10

Page 28: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

IRTranslatordefine double @foo(double %val, double* %addr) { %intval = bitcast double %val to i64 %loaded = load double, double* %addr %mask = bitcast double %loaded to i64 %and = and i64 %intval, %mask %res = bitcast i64 %and to double ret double %res }q

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr and(64) = gAND (i64) val, loaded R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

• LLVM IR to generic (G) MachineInstr • One IR instruction to 0..* (G) MachineInstr • MachineInstrs get a type • Virtual registers get a size • ABI lowering

NEW

NEW

10

Page 29: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

• LLVM IR to generic (G) MachineInstr • One IR instruction to 0..* (G) MachineInstr • MachineInstrs get a type • Virtual registers get a size • ABI lowering

IRTranslatordefine double @foo(double %val, double* %addr) { %intval = bitcast double %val to i64 %loaded = load double, double* %addr %mask = bitcast double %loaded to i64 %and = and i64 %intval, %mask %res = bitcast i64 %and to double ret double %res }q

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr and(64) = gAND (i64) val, loaded R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

NEW

NEW

10

Page 30: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

IRTranslatorLLVM IR

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr and(64) = gAND (i64) val, loaded R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use> 11

Page 31: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

IRTranslator (G)MILLVM IR

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr and(64) = gAND (i64) val, loaded R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use> 11

Page 32: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr and(64) = gAND (i64) val, loaded R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

LegalizerIRTranslator (G)MILLVM IR

11

Page 33: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

Legalizer

• No illegal types, just illegal operations

• Illegal (G)MachineInstr to legal (G)MachineInstr • State expressed in the IR • Iterative process

• Set of transformations • Loop until no more changes

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr and(64) = gAND (i64) val, loaded R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

NEW

12

Page 34: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

Legalizer

• No illegal types, just illegal operations

• Illegal (G)MachineInstr to legal (G)MachineInstr • State expressed in the IR • Iterative process

• Set of transformations • Loop until no more changes

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr and(64) = gAND (i64) val, loaded R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr and(64) = gAND (i64) val, loaded R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

NEW

12

Page 35: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

Legalizer

• No illegal types, just illegal operations

• Illegal (G)MachineInstr to legal (G)MachineInstr • State expressed in the IR • Iterative process

• Set of transformations • Loop until no more changes

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr and(64) = gAND (i64) val, loaded R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr and(64) = gAND (i64) val, loaded R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

NEW

12

Page 36: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

Legalizer

• No illegal types, just illegal operations

• Illegal (G)MachineInstr to legal (G)MachineInstr • State expressed in the IR (extract, build_sequence) • Iterative process

• Set of transformations • Loop until no more changes

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr and(64) = gAND (i64) val, loaded R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr lval(32),hval(32) = extract val low(32),high(32) = extract loaded land(32) = gAND (i32) lval, low hand(32) = gAND (i32) hval, high and(64) = build_sequence land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

NEW

12

Page 37: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

Legalizer

• No illegal types, just illegal operations

• Illegal (G)MachineInstr to legal (G)MachineInstr • State expressed in the IR (extract, build_sequence) • Iterative process • Set of transformations • Iterate until no more changes

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr and(64) = gAND (i64) val, loaded R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr lval(32),hval(32) = extract val low(32),high(32) = extract loaded land(32) = gAND (i32) lval, low hand(32) = gAND (i32) hval, high and(64) = build_sequence land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr lval(32),hval(32) = extract val low(32),high(32) = extract loaded land(32) = gAND (i32) lval, low hand(32) = gAND (i32) hval, high and(64) = build_sequence land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

NEW

NEW

12

Page 38: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

Legalizer

• No illegal types, just illegal operations

• Illegal (G)MachineInstr to legal (G)MachineInstr • State expressed in the IR (extract, build_sequence) • Iterative process • Set of transformations • Iterate until no more changes

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr and(64) = gAND (i64) val, loaded R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr lval(32),hval(32) = extract val low(32),high(32) = extract loaded land(32) = gAND (i32) lval, low hand(32) = gAND (i32) hval, high and(64) = build_sequence land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

NEW

NEW

12

Page 39: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

Legalizerfoo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr lval(32),hval(32) = extract val low(32),high(32) = extract loaded land(32) = gAND (i32) lval, low hand(32) = gAND (i32) hval, high and(64) = build_sequence land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr lval(32),hval(32) = VMOVRRD val low(32),high(32) = VMOVRRD loaded land(32) = gAND (i32) lval, low hand(32) = gAND (i32) hval, high and(64) = VMOVDRR land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

• Introduce a “LegalizerToolkit” for (custom) lowering of legalization

NEW

13

Page 40: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

Legalizer

• Introduce a “LegalizerToolkit” for (custom) lowering of legalization

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr lval(32),hval(32) = VMOVRRD val low(32),high(32) = VMOVRRD loaded land(32) = gAND (i32) lval, low hand(32) = gAND (i32) hval, high and(64) = VMOVDRR land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr lval(32),hval(32) = extract val low(32),high(32) = extract loaded land(32) = gAND (i32) lval, low hand(32) = gAND (i32) hval, high and(64) = build_sequence land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

NEW

13

Page 41: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

LegalizerIRTranslator (G)MILLVM IR

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr lval(32),hval(32) = VMOVRRD val low(32),high(32) = VMOVRRD loaded land(32) = gAND (i32) lval, low hand(32) = gAND (i32) hval, high and(64) = VMOVDRR land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use> 14

Page 42: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

Legalizer RegBank IRTranslator (G)MILLVM IR

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr lval(32),hval(32) = VMOVRRD val low(32),high(32) = VMOVRRD loaded land(32) = gAND (i32) lval, low hand(32) = gAND (i32) hval, high and(64) = VMOVDRR land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

Select

NEW

14

Page 43: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

SelectRegBank foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr lval(32),hval(32) = VMOVRRD val low(32),high(32) = VMOVRRD loaded land(32) = gAND (i32) lval, low hand(32) = gAND (i32) hval, high and(64) = VMOVDRR land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

NEW

15

Page 44: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr lval(32),hval(32) = VMOVRRD val low(32),high(32) = VMOVRRD loaded land(32) = gAND (i32) lval, low hand(32) = gAND (i32) hval, high and(64) = VMOVDRR land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

• Assign virtual registers to register bank

• Avoid cross domain penalties

• Aware of register pressure

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr lval(32),hval(32) = VMOVRRD val low(32),high(32) = VMOVRRD loaded land(32) = gAND (i32) lval, low hand(32) = gAND (i32) hval, high and(64) = VMOVDRR land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

NEWRegBank Select

15

Page 45: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

• Assign virtual registers to register bank

• Avoid cross domain penalties

• Aware of register pressure

foo: val(FPR,64) = VMOVDRR R0,R1 addr(GPR,32) = COPY R2 loaded(FPR,64) = gLD (double) addr lval(GPR,32),hval(GPR,32) = VMOVRRD val low(GPR,32),high(GPR,32) = VMOVRRD loaded land(GPR,32) = gAND (i32) lval, low hand(GPR,32) = gAND (i32) hval, high and(FPR,64) = VMOVDRR land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

NEWRegBank Selectfoo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr lval(32),hval(32) = VMOVRRD val low(32),high(32) = VMOVRRD loaded land(32) = gAND (i32) lval, low hand(32) = gAND (i32) hval, high and(64) = VMOVDRR land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

15

Page 46: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr lval(32),hval(32) = VMOVRRD val low(32),high(32) = VMOVRRD loaded land(32) = gAND (i32) lval, low hand(32) = gAND (i32) hval, high and(64) = VMOVDRR land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

• RegBankSelect: • Asks the target what register banks are supported

for a given opcode • Assigns register banks to avoid cross bank copies • May use the LegalizerToolkit to change the code

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr lval(32),hval(32) = VMOVRRD val low(32),high(32) = VMOVRRD loaded land(32) = gAND (i32) lval, low hand(32) = gAND (i32) hval, high and(64) = VMOVDRR land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

[{(FPR,0xFF…FF),1},{(GPR,0xFF..00)(GPR,0x00..FF),0}]

NEWRegBank Select

16

Page 47: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

• RegBankSelect: • Asks the target what register banks are supported

for a given opcode • Assigns register banks to avoid cross bank copies • May use the LegalizerToolkit to change the code

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr lval(32),hval(32) = VMOVRRD val low(32),high(32) = VMOVRRD loaded land(32) = gAND (i32) lval, low hand(32) = gAND (i32) hval, high and(64) = VMOVDRR land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

[{(FPR,0xFF…FF),1},{(GPR,0xFFFF…0000)(GPR,0x0000…FFFF),0}]

NEWRegBank Select

16

Page 48: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

• RegBankSelect: • Asks the target what register banks are supported

for a given opcode • Assigns register banks to avoid cross bank copies • May use the LegalizerToolkit to change the code

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr lval(32),hval(32) = VMOVRRD val low(32),high(32) = VMOVRRD loaded land(32) = gAND (i32) lval, low hand(32) = gAND (i32) hval, high and(64) = VMOVDRR land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

[{(FPR,0xFF…FF),1},{(GPR,0xFFFF…0000)(GPR,0x0000…FFFF),0}]

{(FPR,0xFF…FF),1} {(GPR,0xFF..00)(GPR,0x00..FF),0}

NEWRegBank Select

16

Page 49: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

• RegBankSelect: • Asks the target what register banks are supported

for a given opcode • Assigns register banks to avoid cross bank copies • May use the LegalizerToolkit to change the code

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr lval(32),hval(32) = VMOVRRD val low(32),high(32) = VMOVRRD loaded land(32) = gAND (i32) lval, low hand(32) = gAND (i32) hval, high and(64) = VMOVDRR land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

[{(FPR,0xFF…FF),1},{(GPR,0xFFFF…0000)(GPR,0x0000…FFFF),0}]

{(FPR,0xFF…FF),1} {(GPR,0xFFFF…0000)(GPR,0x0000…FFFF),0}

NEWRegBank Select

16

Page 50: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

• RegBankSelect: • Asks the target what register banks are supported

for a given opcode • Assigns register banks to avoid cross bank copies • May use the LegalizerToolkit to change the code

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr lval(32),hval(32) = VMOVRRD val low(32),high(32) = VMOVRRD loaded land(32) = gAND (i32) lval, low hand(32) = gAND (i32) hval, high and(64) = VMOVDRR land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

[{(FPR,0xFF…FF),1},{(GPR,0xFFFF…0000)(GPR,0x0000…FFFF),0}]

{(FPR,0xFF…FF),1} {(GPR,0xFFFF…0000)(GPR,0x0000…FFFF),0}

NEWRegBank Select

16

Page 51: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

• RegBankSelect: • Asks the target what register banks are supported

for a given opcode

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr lval(32),hval(32) = VMOVRRD val low(32),high(32) = VMOVRRD loaded land(32) = gAND (i32) lval, low hand(32) = gAND (i32) hval, high and(64) = VMOVDRR land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr lval(32),hval(32) = VMOVRRD val low(32),high(32) = VMOVRRD loaded land(32) = gAND (i32) lval, low hand(32) = gAND (i32) hval, high and(64) = VMOVDRR land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

NEWRegBank Select

16

Page 52: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

• RegBankSelect: • Asks the target what register banks are supported

for a given opcode • Assigns register banks to avoid cross bank copies • May use the LegalizerToolkit to change the code

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr lval(32),hval(32) = VMOVRRD val low(32),high(32) = VMOVRRD loaded land(32) = gAND (i32) lval, low hand(32) = gAND (i32) hval, high and(64) = VMOVDRR land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

foo: val1(GPR,32),val2(GPR,32) = COPIES R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr lval(32),hval(32) = COPIES val1, val2 low(32),high(32) = VMOVRRD loaded land(32) = gAND (i32) lval, low hand(32) = gAND (i32) hval, high and(64) = VMOVDRR land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

NEWRegBank Select

16

Page 53: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

• RegBankSelect: • Asks the target what register banks are supported

for a given opcode • Assigns register banks to avoid cross bank copies • May use the LegalizerToolkit to change the code

foo: val(FPR,64) = VMOVDRR R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr lval(32),hval(32) = VMOVRRD val low(32),high(32) = VMOVRRD loaded land(32) = gAND (i32) lval, low hand(32) = gAND (i32) hval, high and(64) = VMOVDRR land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

foo: val1(GPR,32),val2(GPR,32) = COPIES R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr lval(32),hval(32) = COPIES val1, val2 low(32),high(32) = VMOVRRD loaded land(32) = gAND (i32) lval, low hand(32) = gAND (i32) hval, high and(64) = VMOVDRR land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

NEWRegBank Select

16

Page 54: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

foo: val1(GPR,32),val2(GPR,32) = COPIES R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr lval(32),hval(32) = COPIES val1, val2 low(32),high(32) = VMOVRRD loaded land(32) = gAND (i32) lval, low hand(32) = gAND (i32) hval, high and(64) = VMOVDRR land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

• RegBankSelect: • Asks the target what register banks are supported

for a given opcode • Assigns register banks to avoid cross bank copies • May use the LegalizerToolkit to change the code

foo: val1(GPR,32),val2(GPR,32) = COPIES R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr lval(32),hval(32) = COPIES val1, val2 low(32),high(32) = VMOVRRD loaded land(32) = gAND (i32) lval, low hand(32) = gAND (i32) hval, high and(64) = VMOVDRR land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

NEWRegBank Select

16

Page 55: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

foo: val1(GPR,32),val2(GPR,32) = COPIES R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr lval(32),hval(32) = COPIES val1, val2 low(32),high(32) = VMOVRRD loaded land(32) = gAND (i32) lval, low hand(32) = gAND (i32) hval, high and(64) = VMOVDRR land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

foo: val1(GPR,32),val2(GPR,32) = COPIES R0,R1 addr(32) = COPY R2 loaded1(GPR,32) = gLD (i32) addr loaded2(GPR,32) = gLD (i32) addr, #4 lval(32),hval(32) = COPIES val1, val2 low(32),high(32) = COPIES loaded1,loaded2 land(32) = gAND (i32) lval, low hand(32) = gAND (i32) hval, high and(64) = VMOVDRR land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

• RegBankSelect: • Asks the target what register banks are supported

for a given opcode • Assigns register banks to avoid cross bank copies • May use the LegalizerToolkit to change the code

NEWRegBank Select

16

Page 56: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

foo: val1(GPR,32),val2(GPR,32) = COPIES R0,R1 addr(32) = COPY R2 loaded(64) = gLD (double) addr lval(32),hval(32) = COPIES val1, val2 low(32),high(32) = VMOVRRD loaded land(32) = gAND (i32) lval, low hand(32) = gAND (i32) hval, high and(64) = VMOVDRR land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

foo: val1(GPR,32),val2(GPR,32) = COPIES R0,R1 addr(32) = COPY R2 loaded1(GPR,32) = gLD (i32) addr loaded2(GPR,32) = gLD (i32) addr, #4 lval(32),hval(32) = COPIES val1, val2 low(32),high(32) = COPIES loaded1,loaded2 land(32) = gAND (i32) lval, low hand(32) = gAND (i32) hval, high and(64) = VMOVDRR land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

• RegBankSelect: • Asks the target what register banks are supported

for a given opcode • Assigns register banks to avoid cross bank copies • May use the LegalizerToolkit to change the code

NEWRegBank Select

16

Page 57: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

foo: val1(GPR,32),val2(GPR,32) = COPIES R0,R1 addr(32) = COPY R2 loaded1(GPR,32) = gLD (i32) addr loaded2(GPR,32) = gLD (i32) addr, #4 lval(32),hval(32) = COPIES val1, val2 low(32),high(32) = COPIES loaded1,loaded2 land(32) = gAND (i32) lval, low hand(32) = gAND (i32) hval, high and(64) = VMOVDRR land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

foo: val1(GPR,32),val2(GPR,32) = COPIES R0,R1 addr(GPR,32) = COPY R2 loaded1(GPR,32) = gLD (i32) addr loaded2(GPR,32) = gLD (i32) addr, #4 lval(GPR,32),hval(GPR,32) = COPIES val1, val2 low(GPR,32),high(GPR,32) = COPIES loaded1,loaded2 land(GPR,32) = gAND (i32) lval, low hand(GPR,32) = gAND (i32) hval, high and1(GPR,32), and2(GPR,32) = COPIES land, hand R0,R1 = COPIES and1, and2 tBX_RET R0<imp-use>,R1<imp-use>

• RegBankSelect: • Asks the target what register banks are supported

for a given opcode • Assigns register banks to avoid cross bank copies • May use the LegalizerToolkit to change the code

NEWRegBank Select

16

Page 58: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

foo: val1(GPR,32),val2(GPR,32) = COPIES R0,R1 addr(32) = COPY R2 loaded1(GPR,32) = gLD (i32) addr loaded2(GPR,32) = gLD (i32) addr, #4 lval(32),hval(32) = COPIES val1, val2 low(32),high(32) = COPIES loaded1,loaded2 land(32) = gAND (i32) lval, low hand(32) = gAND (i32) hval, high and(64) = VMOVDRR land, hand R0,R1 = VMOVRRD and tBX_RET R0<imp-use>,R1<imp-use>

foo: val1(GPR,32),val2(GPR,32) = COPIES R0,R1 addr(GPR,32) = COPY R2 loaded1(GPR,32) = gLD (i32) addr loaded2(GPR,32) = gLD (i32) addr, #4 lval(GPR,32),hval(GPR,32) = COPIES val1, val2 low(GPR,32),high(GPR,32) = COPIES loaded1,loaded2 land(GPR,32) = gAND (i32) lval, low hand(GPR,32) = gAND (i32) hval, high and1(GPR,32), and2(GPR,32) = COPIES land, hand R0,R1 = COPIES and1, and2 tBX_RET R0<imp-use>,R1<imp-use>

• RegBankSelect: • Asks the target what register banks are supported

for a given opcode • Assigns register banks to avoid cross bank copies • May use the LegalizerToolkit to change the code

NEWRegBank Select

16

Page 59: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

RegBank IRTranslator (G)MILLVM IR Legalizer Select

foo: val1(GPR,32),val2(GPR,32) = COPIES R0,R1 addr(GPR,32) = COPY R2 loaded1(GPR,32) = gLD (i32) addr loaded2(GPR,32) = gLD (i32) addr, #4 lval(GPR,32),hval(GPR,32) = COPIES val1, val2 low(GPR,32),high(GPR,32) = COPIES loaded1,loaded2 land(GPR,32) = gAND (i32) lval, low hand(GPR,32) = gAND (i32) hval, high and1(GPR,32), and2(GPR,32) = COPIES land, hand R0,R1 = COPIES and1, and2 tBX_RET R0<imp-use>,R1<imp-use>

17

Page 60: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

RegBank IRTranslator (G)MILLVM IR Legalizer Select

foo: val1(GPR,32),val2(GPR,32) = COPIES R0,R1 addr(GPR,32) = COPY R2 loaded1(GPR,32) = gLD (i32) addr loaded2(GPR,32) = gLD (i32) addr, #4 lval(GPR,32),hval(GPR,32) = COPIES val1, val2 low(GPR,32),high(GPR,32) = COPIES loaded1,loaded2 land(GPR,32) = gAND (i32) lval, low hand(GPR,32) = gAND (i32) hval, high and1(GPR,32), and2(GPR,32) = COPIES land, hand R0,R1 = COPIES and1, and2 tBX_RET R0<imp-use>,R1<imp-use>

Select

17

Page 61: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

Select

• (G)MachineInstr to MachineInstr • Loop until everything is selected • State expressed in the IR. • In-place morphing. • State machine.

foo: val1(GPR,32),val2(GPR,32) = COPIES R0,R1 addr(GPR,32) = COPY R2 loaded1(GPR,32) = gLD (i32) addr loaded2(GPR,32) = gLD (i32) addr, #4 lval(GPR,32),hval(GPR,32) = COPIES val1, val2 low(GPR,32),high(GPR,32) = COPIES loaded1,loaded2 land(GPR,32) = gAND (i32) lval, low hand(GPR,32) = gAND (i32) hval, high and1(GPR,32), and2(GPR,32) = COPIES land, hand R0,R1 = COPIES and1, and2 tBX_RET R0<imp-use>,R1<imp-use>

18

Page 62: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

Select

• (G)MachineInstr to MachineInstr • In-place morphing • State expressed in the IR • State machine • Loop until everything is selected

foo: val1(GPR,32),val2(GPR,32) = COPIES R0,R1 addr(GPR,32) = COPY R2 loaded1(GPR,32) = gLD (i32) addr loaded2(GPR,32) = gLD (i32) addr, #4 lval(GPR,32),hval(GPR,32) = COPIES val1, val2 low(GPR,32),high(GPR,32) = COPIES loaded1,loaded2 land(GPR,32) = gAND (i32) lval, low hand(GPR,32) = gAND (i32) hval, high and1(GPR,32), and2(GPR,32) = COPIES land, hand R0,R1 = COPIES and1, and2 tBX_RET R0<imp-use>,R1<imp-use>

NEW

18

Page 63: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

• (G)MachineInstr to MachineInstr • In-place morphing • State expressed in the IR • State machine • Iterate until everything is selected • Combines across basic blocks

Selectfoo: val1(GPR,32),val2(GPR,32) = COPIES R0,R1 addr(GPR,32) = COPY R2 loaded1(GPR,32) = gLD (i32) addr loaded2(GPR,32) = gLD (i32) addr, #4 lval(GPR,32),hval(GPR,32) = COPIES val1, val2 low(GPR,32),high(GPR,32) = COPIES loaded1,loaded2 land(GPR,32) = gAND (i32) lval, low hand(GPR,32) = gAND (i32) hval, high and1(GPR,32), and2(GPR,32) = COPIES land, hand R0,R1 = COPIES and1, and2 tBX_RET R0<imp-use>,R1<imp-use>

foo: val1(GPR,32),val2(GPR,32) = COPIES R0,R1 addr(GPR,32) = COPY R2 loaded1(GPR,32) = gLD (i32) addr loaded2(GPR,32) = gLD (i32) addr, #4 lval(GPR,32),hval(GPR,32) = COPIES val1, val2 low(GPR,32),high(GPR,32) = COPIES loaded1,loaded2 land(GPR,32) = gAND (i32) lval, low hand(GPR,32) = gAND (i32) hval, high and1(GPR,32), and2(GPR,32) = COPIES land, hand R0,R1 = COPIES and1, and2 tBX_RET R0<imp-use>,R1<imp-use>

NEW

18

Page 64: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

Selectfoo: val1(GPR,32),val2(GPR,32) = COPIES R0,R1 addr(GPR,32) = COPY R2 loaded1(GPR,32) = gLD (i32) addr loaded2(GPR,32) = gLD (i32) addr, #4 lval(GPR,32),hval(GPR,32) = COPIES val1, val2 low(GPR,32),high(GPR,32) = COPIES loaded1,loaded2 land(GPR,32) = gAND (i32) lval, low hand(GPR,32) = gAND (i32) hval, high and1(GPR,32), and2(GPR,32) = COPIES land, hand R0,R1 = COPIES and1, and2 tBX_RET R0<imp-use>,R1<imp-use>

foo: val1(GPR,32),val2(GPR,32) = COPIES R0,R1 addr(GPR,32) = COPY R2 loaded1(GPR,32) = gLD (i32) addr loaded2(GPR,32) = gLD (i32) addr, #4 lval(GPR,32),hval(GPR,32) = COPIES val1, val2 low(GPR,32),high(GPR,32) = COPIES loaded1,loaded2 land(GPR,32) = gAND (i32) lval, low hand(GPR,32) = t2ANDrr (i32) hval, high and1(GPR,32), and2(GPR,32) = COPIES land, hand R0,R1 = COPIES and1, and2 tBX_RET R0<imp-use>,R1<imp-use>

• (G)MachineInstr to MachineInstr • In-place morphing • State expressed in the IR • State machine • Iterate until everything is selected • Combines across basic blocks

NEW

18

Page 65: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

Selectfoo: val1(GPR,32),val2(GPR,32) = COPIES R0,R1 addr(GPR,32) = COPY R2 loaded1(GPR,32) = gLD (i32) addr loaded2(GPR,32) = gLD (i32) addr, #4 lval(GPR,32),hval(GPR,32) = COPIES val1, val2 low(GPR,32),high(GPR,32) = COPIES loaded1,loaded2 land(GPR,32) = gAND (i32) lval, low hand(GPR,32) = gAND (i32) hval, high and1(GPR,32), and2(GPR,32) = COPIES land, hand R0,R1 = COPIES and1, and2 tBX_RET R0<imp-use>,R1<imp-use>

foo: val1(GPR,32),val2(GPR,32) = COPIES R0,R1 addr(GPR,32) = COPY R2 loaded1(GPR,32) = gLD (i32) addr loaded2(GPR,32) = gLD (i32) addr, #4 lval(GPR,32),hval(GPR,32) = COPIES val1, val2 low(GPR,32),high(GPR,32) = COPIES loaded1,loaded2 land(GPR,32) = gAND (i32) lval, low hand(GPR,32) = t2ANDrr (i32) hval, high and1(GPR,32), and2(GPR,32) = COPIES land, hand R0,R1 = COPIES and1, and2 tBX_RET R0<imp-use>,R1<imp-use>

• (G)MachineInstr to MachineInstr • In-place morphing • State expressed in the IR • State machine • Iterate until everything is selected • Combines across basic blocks

NEW

NEW

18

Page 66: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

Selectfoo: val1(GPR,32),val2(GPR,32) = COPIES R0,R1 addr(GPR,32) = COPY R2 loaded1(GPR,32) = gLD (i32) addr loaded2(GPR,32) = gLD (i32) addr, #4 lval(GPR,32),hval(GPR,32) = COPIES val1, val2 low(GPR,32),high(GPR,32) = COPIES loaded1,loaded2 land(GPR,32) = gAND (i32) lval, low hand(GPR,32) = gAND (i32) hval, high and1(GPR,32), and2(GPR,32) = COPIES land, hand R0,R1 = COPIES and1, and2 tBX_RET R0<imp-use>,R1<imp-use>

foo: val1(GPR,32),val2(GPR,32) = COPIES R0,R1 addr(GPR,32) = COPY R2 loaded1(GPR,32) = t2LDRi12 (i32) addr loaded2(GPR,32) = t2LDRi12 (i32) addr, #4 lval(GPR,32),hval(GPR,32) = COPIES val1, val2 low(GPR,32),high(GPR,32) = COPIES loaded1,loaded2 land(GPR,32) = t2ANDrr (i32) lval, low hand(GPR,32) = t2ANDrr (i32) hval, high and1(GPR,32), and2(GPR,32) = COPIES land, hand R0,R1 = COPIES and1, and2 tBX_RET R0<imp-use>,R1<imp-use>

• (G)MachineInstr to MachineInstr • In-place morphing • State expressed in the IR • State machine • Iterate until everything is selected • Combines across basic blocks

NEW

NEW

NEW

18

Page 67: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

Selectfoo: val1(GPR,32),val2(GPR,32) = COPIES R0,R1 addr(GPR,32) = COPY R2 loaded1(GPR,32) = gLD (i32) addr loaded2(GPR,32) = gLD (i32) addr, #4 lval(GPR,32),hval(GPR,32) = COPIES val1, val2 low(GPR,32),high(GPR,32) = COPIES loaded1,loaded2 land(GPR,32) = gAND (i32) lval, low hand(GPR,32) = gAND (i32) hval, high and1(GPR,32), and2(GPR,32) = COPIES land, hand R0,R1 = COPIES and1, and2 tBX_RET R0<imp-use>,R1<imp-use>

foo: val1(GPR,32),val2(GPR,32) = COPIES R0,R1 addr(GPR,32) = COPY R2 loaded1(GPR,32) = t2LDRi12 (i32) addr loaded2(GPR,32) = t2LDRi12 (i32) addr, #4 lval(GPR,32),hval(GPR,32) = COPIES val1, val2 low(GPR,32),high(GPR,32) = COPIES loaded1,loaded2 land(GPR,32) = t2ANDrr (i32) lval, low hand(GPR,32) = t2ANDrr (i32) hval, high and1(GPR,32), and2(GPR,32) = COPIES land, hand R0,R1 = COPIES and1, and2 tBX_RET R0<imp-use>,R1<imp-use>

• (G)MachineInstr to MachineInstr • In-place morphing • State expressed in the IR • State machine • Iterate until everything is selected • Combines across basic blocks

NEW

NEW

NEW

18

Page 68: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

(G)MI Legalizer RegBank Select SelectIRTranslatorLLVM IR

19

Page 69: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

(G)MI Legalizer RegBank Select SelectIRTranslatorLLVM IR MI

19

Page 70: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

Summary

MI

(G)MI

Legalizer

RegBank Select

Select

IRTranslator

LLVM IR

20

Page 71: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

Summary

Legalizer

RegBank Select

MI

Select

(G)MI

IRTranslator

LLVM IR

20

Page 72: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

Summary

Opt

Legalizer

RegBank Select

MI

Select

(G)MI

IRTranslator

LLVM IR

20

Page 73: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

Summary

Legalizer

RegBank Select

MI

Select

(G)MI

IRTranslator

LLVM IR

20

Page 74: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

Summary

Opt

Legalizer

RegBank Select

MI

Select

(G)MI

IRTranslator

LLVM IR

20

Page 75: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

Summary

Legalizer

RegBank Select

MI

Select

(G)MI

IRTranslator

LLVM IR

20

Page 76: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

Summary

Opt

Legalizer

RegBank Select

MI

Select

(G)MI

IRTranslator

LLVM IR

20

Page 77: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

Summary

Legalizer ToolkitCan use

TargetHooks

Can tune

Hooks

Legalizer

RegBank Select

MI

Select

(G)MI

IRTranslator

LLVM IR

20

Page 78: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

Global ISel• Work at function scope

• Global

• Break down the process in passes • More flexible pipeline • Easier to understand/maintain • Shared code for fast and good paths

• Introduce new generic opcodes for MachineInstr • Fast • IR that represents ISA concepts better • No change to LLVM IR • Self contained machine representation MI

(G)MI

Legalizer

RegBank Select

Select

IRTranslator

LLVM IR

21

Page 79: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

1. Start of prototyping • Perform the translation • Lower the ABI • Complex instructions are not supported

2. Basic selector • Abort on illegal types • Selector patterns written in C++ • Simple bank selection

3. Simple legalization • Scalar operations • Some vector operations

End of prototyping

How Do We Get There?1. Rename the SDISel into LegacyISel

1

2

2

3

1

MI

(G)MI

Legalizer

RegBank Select

Select

IRTranslator

LLVM IR

MI

Legalizer

RegBank Select

Select

22

Page 80: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

Then What?• Productize on what we learnt

• Discuss timeline to remove:

• SDISel, FastISel

• CodeGenPrepare, ConstantHoisting

• ExeDepsFix, PeepholeOptimizer

• Present a status report next year23

Page 81: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

References

• Jakob’s initial proposal for global-isel: http://lists.llvm.org/pipermail/llvm-dev/2013-August/064696.html

24

Page 82: Global Instruction Selection - LLVMllvm.org/devmtg/2015-10/slides/Colombet-GlobalInstructionSelection.pdf%res = bitcast i64 %and to double ret double %res }q foo: val = … VMOVDRR

Questions?