SlideShare a Scribd company logo
5
Most read
7
Most read
12
Most read
LLVM Instruction Selection
Instruction Selection
• What is instruction selection ?
– To match the Compiler IR (intermediate
representation) to Target instruction.
Instruction Selection
• Why need Compiler IR ?
– In the beginning, compiler developer use “macro
expansion”
• Directly mapping high level language to assembly code
Disadvantage: Tightly coupling the code generator to a particular programming
language.
Create Machine independent IR to isolate backend and programming language
detail.
LLVM retargetablity design
LLVM IR come from frontend (clang/llvm-gcc/GHC)
LLVM Optimization Pass
LLVM IR Pass
LLVM DAG Pass
Emit target assembly code
Instruction Selection
Flags to dump pass sequence:
clang -mllvm -debug-pass=Structure {input}.c
Target Independent Passes
Target Dependent Passes
Flags to dump optimization detail for each pass:
clang -mllvm -debug -mllvm -print-after-all {input}.c
What is SelectionDAG ?
• LLVM IR will transfer to SelectionDag before
Instruction Selection
• a Directed-Acyclic-Graph to present
operations
– Node present operation (E.g. add/sub/or)
– Edge present define/use relationship
t15 hi20(sym)
t16
(lo12(sym))
t17 (or)
t3 (add)
198 (const)
Instruction selection in LLVM would be:
Mapping SDnode-> Target ISA
SDnode : selection DAG node
Define Target Instruction
• Define ADDI
– By ADDIForm (inherent by Instruction class)
• User could inherent and then specialize for different
kind ISA
– (outs GPR:$Rt)
• Output: 1 GPR
– (ins GPR:$Ra, imm15s:$imm)
• Inputs: 1 GPR, 1 imm15s constant
– Imm15s is a user defined operand constraint.
– “addit$Rt, $Rt, $imm”
• Assembly emit pattern
Selection DAG with CSE
• Have chance to deal with CSE.
+
+ +
y x8
Assume we have several instruction
With different cost.
Move reg, constant : cost 5
Add reg, reg, reg: cost 1
Addi reg, reg, constant: cost 5
There are two way to mapping instructions
R1 = y + 8; cost 5
R2 = x + 8 cost 5
R3 = R1 + R2 cost 1
Total cost 11
R1 = 8; cost 5
R2 = x + R1 cost 1
R3 = y + R1 cost 1
R4 = R2+R3 cost 1
Total cost 8
+
+ +
y x8
+
+ +
y x8
Selection DAG with CSE
• Dag->Target Dag selection only the one of the
factor influence optimization result.
– Even if Selection DAG not deal with CSE, we could
leave it to later pass to do it!
– In fact, LLVM move CSE consideration out of DAG
selection now.
– Currently, LLVM DAG Selection is purely greedy
selection and matching
Greedy DAG Selection
• LLVM would give each Target ISA “Complexity”
– Complexity big means it could cover more nodes
in the DAG
• Which means it may have chance to produce fewer
instruction if we choose Complexity big instruction
– In most of the case, few instructions could get better code
quality.
Greedy DAG Selection
• LLVM would give each Target ISA “Complexity”
– By default, complexity calculate by operation
number and operand kind in Target ISA
• E.g.
– Complexity (add R, R, Const) > Complexity (add R, R, R)
– Complexity (store R, [R + R << 2]) > Complexity (store R, [R])
– Complexity (add_shift) > Complexity (add)
• User also could modify their Target instruction
Complexity by AddedComplexity.
Greedy DAG Selection
• LLVM DAG selection is a bottom up method on
each basic block
– When try to match a node from the DAG
• LLVM will use a match table to select Target ISA
– E.g.
» Switch (Node kind)
• Case Add:
• If (op0 is reg && op1 is reg)
• If (op2 is reg) choose ADD
• If (op2is const) chose ADDI
– If Target have same semantic instructions
» Choose Complexity bigger first.
Instruction Matching Table
• LLVM will read Instruction pattern define in
*.td and generate Instruction matching table
– In build folder {Target}GenDAGISel.inc
• Generate by tablegen tools
• Contain the match table
• Implement as a interpreter to execute pattern match
Instruction Selection in dump file
Legalize SelectionDag
• Legalize Selection
DAG
– Legalize
• Support for the
target
– Types
• Check each Dag
Node type support
by the target
– Operations
• Check each Dag
Node operation
support by the target
Legalize SelectionDag
• How to specify target support types and
operations ?
– Defined in target
inherent TargetLowering constructor.
Load to 1 bit is not legal, promote to wider integer type
Instruction selection in LLVM
• Each SelectionDAG operation could specify as
– Legal
• The target natively supports this operation.
– Promote
• This operation should be executed in a larger type.
– Expand
• Try to expand(by llvm general code) this to other
operations, otherwise use a libcall.
– Custom
• Target should defined custom lowering method for the
operation
• Call the costom lowering method by
XXXTargetLowering::LowerOperation() target hook
Instruction selection in LLVM
CTPOP (counts the number of bits) for i32/i64 type is legal
Expand CTTZ (counts trailing zero) to other SDNodes for i32/i64 type
Customize BR_JT (branch to jump table)
Reference
• A deeper look into the LLVM code generator,
Part 1
– http://eli.thegreenplace.net/2013/02/25/a-
deeper-look-into-the-llvm-code-generator-part-1
• LLVM Legalization and Lowering
– https://wiki.aalto.fi/display/t1065450/LLVM+Legal
ization+and+Lowering
• Near-Optimal Instruction Selection on DAGs
– https://llvm.org/svn/llvm-project/www-
pubs/trunk/2008-CGO-DagISel.pdf
• Instruction Selection - Principles, Methods,

More Related Content

PDF
LLVM Register Allocation (2nd Version)
PDF
Instruction Combine in LLVM
PDF
淺談探索 Linux 系統設計之道
PDF
Let's trace Linux Lernel with KGDB @ COSCUP 2021
PPTX
LLVM Backend Porting
PPTX
为啥别读HotSpot VM的源码(2012-03-03)
PPTX
Graal in GraalVM - A New JIT Compiler
PDF
LLVM Register Allocation
LLVM Register Allocation (2nd Version)
Instruction Combine in LLVM
淺談探索 Linux 系統設計之道
Let's trace Linux Lernel with KGDB @ COSCUP 2021
LLVM Backend Porting
为啥别读HotSpot VM的源码(2012-03-03)
Graal in GraalVM - A New JIT Compiler
LLVM Register Allocation

What's hot (20)

PPTX
Integrated Register Allocation introduction
PDF
JVM code reading -- C2
PDF
Kernel Recipes 2019 - Faster IO through io_uring
PDF
Learn C Programming Language by Using GDB
PDF
Explore Android Internals
PPT
Gcc porting
PDF
ARM Trusted FirmwareのBL31を単体で使う!
PDF
Q2.12: Debugging with GDB
PDF
Valgrind
PDF
Design and Implementation of GCC Register Allocation
PDF
Making Linux do Hard Real-time
PDF
Address/Thread/Memory Sanitizer
PPTX
Linux Network Stack
PDF
How A Compiler Works: GNU Toolchain
PDF
JVM Mechanics: When Does the JVM JIT & Deoptimize?
PPT
Introduction to gdb
PDF
from Source to Binary: How GNU Toolchain Works
PDF
The Linux Block Layer - Built for Fast Storage
PDF
Process Address Space: The way to create virtual address (page table) of user...
PDF
Process Scheduler and Balancer in Linux Kernel
Integrated Register Allocation introduction
JVM code reading -- C2
Kernel Recipes 2019 - Faster IO through io_uring
Learn C Programming Language by Using GDB
Explore Android Internals
Gcc porting
ARM Trusted FirmwareのBL31を単体で使う!
Q2.12: Debugging with GDB
Valgrind
Design and Implementation of GCC Register Allocation
Making Linux do Hard Real-time
Address/Thread/Memory Sanitizer
Linux Network Stack
How A Compiler Works: GNU Toolchain
JVM Mechanics: When Does the JVM JIT & Deoptimize?
Introduction to gdb
from Source to Binary: How GNU Toolchain Works
The Linux Block Layer - Built for Fast Storage
Process Address Space: The way to create virtual address (page table) of user...
Process Scheduler and Balancer in Linux Kernel
Ad

Similar to LLVM Instruction Selection (20)

PPTX
07 140430-ipp-languages used in llvm during compilation
PDF
Part II: LLVM Intermediate Representation
PDF
BUD17-302: LLVM Internals #2
PDF
LAS16-501: Introduction to LLVM - Projects, Components, Integration, Internals
PDF
[COSCUP 2021] LLVM Project: The Good, The Bad, and The Ugly
PPT
PDF
Handling inline assembly in Clang and LLVM
PDF
A taste of GlobalISel
PDF
TMPA-2017: Vellvm - Verifying the LLVM
PPT
Introduction to llvm
PPTX
Computer Architecture and Organization
PDF
BerlinMeetup22-TableGen.pdf
PDF
Enscape 3D 3.6.6 License Key Crack Full Version
PDF
Wondershare Filmora Crack 12.0.10 With Latest 2025
PDF
Wondershare UniConverter Crack Download Latest 2025
PDF
Skype 125.0.201 Crack key Free Download
PPT
Code_generatio.lk,jhgfdcxzcvgfhjkmnjhgfcxvfghjmh
PDF
Compiler2016 by abcdabcd987
PPT
COMPILER_DESIGN_CLASS 2.ppt
07 140430-ipp-languages used in llvm during compilation
Part II: LLVM Intermediate Representation
BUD17-302: LLVM Internals #2
LAS16-501: Introduction to LLVM - Projects, Components, Integration, Internals
[COSCUP 2021] LLVM Project: The Good, The Bad, and The Ugly
Handling inline assembly in Clang and LLVM
A taste of GlobalISel
TMPA-2017: Vellvm - Verifying the LLVM
Introduction to llvm
Computer Architecture and Organization
BerlinMeetup22-TableGen.pdf
Enscape 3D 3.6.6 License Key Crack Full Version
Wondershare Filmora Crack 12.0.10 With Latest 2025
Wondershare UniConverter Crack Download Latest 2025
Skype 125.0.201 Crack key Free Download
Code_generatio.lk,jhgfdcxzcvgfhjkmnjhgfcxvfghjmh
Compiler2016 by abcdabcd987
COMPILER_DESIGN_CLASS 2.ppt
Ad

Recently uploaded (20)

PPTX
neck nodes and dissection types and lymph nodes levels
PPTX
microscope-Lecturecjchchchchcuvuvhc.pptx
PDF
IFIT3 RNA-binding activity primores influenza A viruz infection and translati...
PPTX
ECG_Course_Presentation د.محمد صقران ppt
PDF
An interstellar mission to test astrophysical black holes
PPTX
EPIDURAL ANESTHESIA ANATOMY AND PHYSIOLOGY.pptx
PDF
ELS_Q1_Module-11_Formation-of-Rock-Layers_v2.pdf
PDF
The scientific heritage No 166 (166) (2025)
PPTX
Introduction to Fisheries Biotechnology_Lesson 1.pptx
PPTX
cpcsea ppt.pptxssssssssssssssjjdjdndndddd
PPTX
Vitamins & Minerals: Complete Guide to Functions, Food Sources, Deficiency Si...
PDF
SEHH2274 Organic Chemistry Notes 1 Structure and Bonding.pdf
PDF
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
PPT
protein biochemistry.ppt for university classes
PDF
Mastering Bioreactors and Media Sterilization: A Complete Guide to Sterile Fe...
PPTX
TOTAL hIP ARTHROPLASTY Presentation.pptx
PPTX
INTRODUCTION TO EVS | Concept of sustainability
PDF
diccionario toefl examen de ingles para principiante
PPTX
famous lake in india and its disturibution and importance
PPTX
The KM-GBF monitoring framework – status & key messages.pptx
neck nodes and dissection types and lymph nodes levels
microscope-Lecturecjchchchchcuvuvhc.pptx
IFIT3 RNA-binding activity primores influenza A viruz infection and translati...
ECG_Course_Presentation د.محمد صقران ppt
An interstellar mission to test astrophysical black holes
EPIDURAL ANESTHESIA ANATOMY AND PHYSIOLOGY.pptx
ELS_Q1_Module-11_Formation-of-Rock-Layers_v2.pdf
The scientific heritage No 166 (166) (2025)
Introduction to Fisheries Biotechnology_Lesson 1.pptx
cpcsea ppt.pptxssssssssssssssjjdjdndndddd
Vitamins & Minerals: Complete Guide to Functions, Food Sources, Deficiency Si...
SEHH2274 Organic Chemistry Notes 1 Structure and Bonding.pdf
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
protein biochemistry.ppt for university classes
Mastering Bioreactors and Media Sterilization: A Complete Guide to Sterile Fe...
TOTAL hIP ARTHROPLASTY Presentation.pptx
INTRODUCTION TO EVS | Concept of sustainability
diccionario toefl examen de ingles para principiante
famous lake in india and its disturibution and importance
The KM-GBF monitoring framework – status & key messages.pptx

LLVM Instruction Selection

  • 2. Instruction Selection • What is instruction selection ? – To match the Compiler IR (intermediate representation) to Target instruction.
  • 3. Instruction Selection • Why need Compiler IR ? – In the beginning, compiler developer use “macro expansion” • Directly mapping high level language to assembly code Disadvantage: Tightly coupling the code generator to a particular programming language. Create Machine independent IR to isolate backend and programming language detail.
  • 4. LLVM retargetablity design LLVM IR come from frontend (clang/llvm-gcc/GHC)
  • 5. LLVM Optimization Pass LLVM IR Pass LLVM DAG Pass Emit target assembly code Instruction Selection Flags to dump pass sequence: clang -mllvm -debug-pass=Structure {input}.c Target Independent Passes Target Dependent Passes Flags to dump optimization detail for each pass: clang -mllvm -debug -mllvm -print-after-all {input}.c
  • 6. What is SelectionDAG ? • LLVM IR will transfer to SelectionDag before Instruction Selection • a Directed-Acyclic-Graph to present operations – Node present operation (E.g. add/sub/or) – Edge present define/use relationship t15 hi20(sym) t16 (lo12(sym)) t17 (or) t3 (add) 198 (const) Instruction selection in LLVM would be: Mapping SDnode-> Target ISA SDnode : selection DAG node
  • 7. Define Target Instruction • Define ADDI – By ADDIForm (inherent by Instruction class) • User could inherent and then specialize for different kind ISA – (outs GPR:$Rt) • Output: 1 GPR – (ins GPR:$Ra, imm15s:$imm) • Inputs: 1 GPR, 1 imm15s constant – Imm15s is a user defined operand constraint. – “addit$Rt, $Rt, $imm” • Assembly emit pattern
  • 8. Selection DAG with CSE • Have chance to deal with CSE. + + + y x8 Assume we have several instruction With different cost. Move reg, constant : cost 5 Add reg, reg, reg: cost 1 Addi reg, reg, constant: cost 5 There are two way to mapping instructions R1 = y + 8; cost 5 R2 = x + 8 cost 5 R3 = R1 + R2 cost 1 Total cost 11 R1 = 8; cost 5 R2 = x + R1 cost 1 R3 = y + R1 cost 1 R4 = R2+R3 cost 1 Total cost 8 + + + y x8 + + + y x8
  • 9. Selection DAG with CSE • Dag->Target Dag selection only the one of the factor influence optimization result. – Even if Selection DAG not deal with CSE, we could leave it to later pass to do it! – In fact, LLVM move CSE consideration out of DAG selection now. – Currently, LLVM DAG Selection is purely greedy selection and matching
  • 10. Greedy DAG Selection • LLVM would give each Target ISA “Complexity” – Complexity big means it could cover more nodes in the DAG • Which means it may have chance to produce fewer instruction if we choose Complexity big instruction – In most of the case, few instructions could get better code quality.
  • 11. Greedy DAG Selection • LLVM would give each Target ISA “Complexity” – By default, complexity calculate by operation number and operand kind in Target ISA • E.g. – Complexity (add R, R, Const) > Complexity (add R, R, R) – Complexity (store R, [R + R << 2]) > Complexity (store R, [R]) – Complexity (add_shift) > Complexity (add) • User also could modify their Target instruction Complexity by AddedComplexity.
  • 12. Greedy DAG Selection • LLVM DAG selection is a bottom up method on each basic block – When try to match a node from the DAG • LLVM will use a match table to select Target ISA – E.g. » Switch (Node kind) • Case Add: • If (op0 is reg && op1 is reg) • If (op2 is reg) choose ADD • If (op2is const) chose ADDI – If Target have same semantic instructions » Choose Complexity bigger first.
  • 13. Instruction Matching Table • LLVM will read Instruction pattern define in *.td and generate Instruction matching table – In build folder {Target}GenDAGISel.inc • Generate by tablegen tools • Contain the match table • Implement as a interpreter to execute pattern match
  • 15. Legalize SelectionDag • Legalize Selection DAG – Legalize • Support for the target – Types • Check each Dag Node type support by the target – Operations • Check each Dag Node operation support by the target
  • 16. Legalize SelectionDag • How to specify target support types and operations ? – Defined in target inherent TargetLowering constructor. Load to 1 bit is not legal, promote to wider integer type
  • 17. Instruction selection in LLVM • Each SelectionDAG operation could specify as – Legal • The target natively supports this operation. – Promote • This operation should be executed in a larger type. – Expand • Try to expand(by llvm general code) this to other operations, otherwise use a libcall. – Custom • Target should defined custom lowering method for the operation • Call the costom lowering method by XXXTargetLowering::LowerOperation() target hook
  • 18. Instruction selection in LLVM CTPOP (counts the number of bits) for i32/i64 type is legal Expand CTTZ (counts trailing zero) to other SDNodes for i32/i64 type Customize BR_JT (branch to jump table)
  • 19. Reference • A deeper look into the LLVM code generator, Part 1 – http://eli.thegreenplace.net/2013/02/25/a- deeper-look-into-the-llvm-code-generator-part-1 • LLVM Legalization and Lowering – https://wiki.aalto.fi/display/t1065450/LLVM+Legal ization+and+Lowering • Near-Optimal Instruction Selection on DAGs – https://llvm.org/svn/llvm-project/www- pubs/trunk/2008-CGO-DagISel.pdf • Instruction Selection - Principles, Methods,