G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP...

69
G53CMP: Lecture 20 LLVM: A Real Compiler Backend Henrik Nilsson University of Nottingham, UK G53CMP: Lecture 20 – p.1/23

Transcript of G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP...

Page 1: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

G53CMP: Lecture 20LLVM: A Real Compiler Backend

Henrik Nilsson

University of Nottingham, UK

G53CMP: Lecture 20 – p.1/23

Page 2: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

Result SEM G53CMP 2016/17 (1)

Average scores.Scale: 5 is agree/positive; 1 is disagree/negative.

# Question G53CMP All modules

1 Clear objectives 4.43 4.12

2 Teaching methods 4.29 4.01

3 Assessment methods 4.00 4.00

4 Module level 4.07 4.05

5 Module workload 3.79 4.05

G53CMP: Lecture 20 – p.2/23

Page 3: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

Result SEM G53CMP 2016/17 (2)

• On the whole, you were happy with the module:

G53CMP: Lecture 20 – p.3/23

Page 4: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

Result SEM G53CMP 2016/17 (2)

• On the whole, you were happy with the module:

- All but one would recommend it to otherstudents.

G53CMP: Lecture 20 – p.3/23

Page 5: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

Result SEM G53CMP 2016/17 (2)

• On the whole, you were happy with the module:

- All but one would recommend it to otherstudents.

- Dissenting concern: more about Haskellthan compilers.

G53CMP: Lecture 20 – p.3/23

Page 6: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

Result SEM G53CMP 2016/17 (2)

• On the whole, you were happy with the module:

- All but one would recommend it to otherstudents.

- Dissenting concern: more about Haskellthan compilers.

• Most disagreement over module workload:

G53CMP: Lecture 20 – p.3/23

Page 7: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

Result SEM G53CMP 2016/17 (2)

• On the whole, you were happy with the module:

- All but one would recommend it to otherstudents.

- Dissenting concern: more about Haskellthan compilers.

• Most disagreement over module workload:

- Most were happy, but 2 students stronglyfelt too much content and/or work.

G53CMP: Lecture 20 – p.3/23

Page 8: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

Result SEM G53CMP 2016/17 (2)

• On the whole, you were happy with the module:

- All but one would recommend it to otherstudents.

- Dissenting concern: more about Haskellthan compilers.

• Most disagreement over module workload:

- Most were happy, but 2 students stronglyfelt too much content and/or work.

- Key question: Overall, around 100 h of work?

G53CMP: Lecture 20 – p.3/23

Page 9: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

Result SEM G53CMP 2016/17 (3)

• Specific comments on the coursework weight:

25 % too little, should be 50 %.

G53CMP: Lecture 20 – p.4/23

Page 10: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

Result SEM G53CMP 2016/17 (3)

• Specific comments on the coursework weight:

25 % too little, should be 50 %.

• I agree! And weight is effectively 50 % as partof the exam is directly related to coursework.

G53CMP: Lecture 20 – p.4/23

Page 11: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

Result SEM G53CMP 2016/17 (3)

• Specific comments on the coursework weight:

25 % too little, should be 50 %.

• I agree! And weight is effectively 50 % as partof the exam is directly related to coursework.

• Generally, coursework weight not necessarilyindicative of amount of work involved: themark is mainly to encourage engagement,and the question is if the amount of work for amodule overall is in line with 10 h/credit.

G53CMP: Lecture 20 – p.4/23

Page 12: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

LLVM (1)

LLVM (formerly Low Level Virtual Machine) is acompiler infrastructure project:

G53CMP: Lecture 20 – p.5/23

Page 13: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

LLVM (1)

LLVM (formerly Low Level Virtual Machine) is acompiler infrastructure project:

• Highly modular and extensible; at its core:

- Set of reusable libraries

- Well-defined interfaces

G53CMP: Lecture 20 – p.5/23

Page 14: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

LLVM (1)

LLVM (formerly Low Level Virtual Machine) is acompiler infrastructure project:

• Highly modular and extensible; at its core:

- Set of reusable libraries

- Well-defined interfaces

• Designed for static and dynamic (JIT)compilation and optimzation: compile-time,link-time, load/installation-time, run-time.

G53CMP: Lecture 20 – p.5/23

Page 15: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

LLVM (1)

LLVM (formerly Low Level Virtual Machine) is acompiler infrastructure project:

• Highly modular and extensible; at its core:

- Set of reusable libraries

- Well-defined interfaces

• Designed for static and dynamic (JIT)compilation and optimzation: compile-time,link-time, load/installation-time, run-time.

• Language agnostic: LLVM-based compilersfor Ada, C, C++, Fortran, Haskell, Java bytecode,OpenGL Shading Language, Python, Scala.

G53CMP: Lecture 20 – p.5/23

Page 16: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

LLVM (2)

Some background:

• The LLVM project started in 2000 at theUniversity of Illinois at Urbana-Champaign.

• Directed by Vikram Adve and Chris Lattner.

• Lattner later hired by Apple Inc.

• LLVM integral part of Apple’s developmenttools for OS X and iOS.

• LLVM is Open Source.

• Adve, Lattner, and Evan Cheng awarded theACM Software System Award for LLVM in 2012.

G53CMP: Lecture 20 – p.6/23

Page 17: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

Motivations for LLVM (1)

When LLVM started:

G53CMP: Lecture 20 – p.7/23

Page 18: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

Motivations for LLVM (1)

When LLVM started:

• Open-source language implementationstended to be monolithic; e.g. GCC:

- Extremely hard to reuse individual parts

- Not even a self-contained intermediaterepresentation

G53CMP: Lecture 20 – p.7/23

Page 19: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

Motivations for LLVM (1)

When LLVM started:

• Open-source language implementationstended to be monolithic; e.g. GCC:

- Extremely hard to reuse individual parts

- Not even a self-contained intermediaterepresentation

• Implementations tended to either supportstatic or JIT compilation.

G53CMP: Lecture 20 – p.7/23

Page 20: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

Motivations for LLVM (1)

When LLVM started:

• Open-source language implementationstended to be monolithic; e.g. GCC:

- Extremely hard to reuse individual parts

- Not even a self-contained intermediaterepresentation

• Implementations tended to either supportstatic or JIT compilation.

• The text-book vision of multiple independentfront-ends and back-ends around a sharedcompiler core hardly ever realised in practice.

G53CMP: Lecture 20 – p.7/23

Page 21: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

LLVM IR (1)

The LLVM Intermediate Representation (IR) isthe “glue” that holds LLVM together.

G53CMP: Lecture 20 – p.8/23

Page 22: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

LLVM IR (1)

The LLVM Intermediate Representation (IR) isthe “glue” that holds LLVM together.

• Complete, self-contained representation: afirst-class language with well-definedsemantics.

G53CMP: Lecture 20 – p.8/23

Page 23: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

LLVM IR (1)

The LLVM Intermediate Representation (IR) isthe “glue” that holds LLVM together.

• Complete, self-contained representation: afirst-class language with well-definedsemantics.

• Designed to host mid-level analyses andtransformations.

G53CMP: Lecture 20 – p.8/23

Page 24: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

LLVM IR (1)

The LLVM Intermediate Representation (IR) isthe “glue” that holds LLVM together.

• Complete, self-contained representation: afirst-class language with well-definedsemantics.

• Designed to host mid-level analyses andtransformations.

• RISC-like code.

G53CMP: Lecture 20 – p.8/23

Page 25: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

LLVM IR (1)

The LLVM Intermediate Representation (IR) isthe “glue” that holds LLVM together.

• Complete, self-contained representation: afirst-class language with well-definedsemantics.

• Designed to host mid-level analyses andtransformations.

• RISC-like code.

• Sufficiently low-level to be a suitabletranslation target for any language.

G53CMP: Lecture 20 – p.8/23

Page 26: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

LLVM IR (2)

• Sufficiently high-level to allow targetingarbitrary concrete architectures; e.g.:

G53CMP: Lecture 20 – p.9/23

Page 27: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

LLVM IR (2)

• Sufficiently high-level to allow targetingarbitrary concrete architectures; e.g.:

- Unbounded number of registers

G53CMP: Lecture 20 – p.9/23

Page 28: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

LLVM IR (2)

• Sufficiently high-level to allow targetingarbitrary concrete architectures; e.g.:

- Unbounded number of registers

- Abstraction over calling conventions

G53CMP: Lecture 20 – p.9/23

Page 29: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

LLVM IR (2)

• Sufficiently high-level to allow targetingarbitrary concrete architectures; e.g.:

- Unbounded number of registers

- Abstraction over calling conventions

• Typed:

G53CMP: Lecture 20 – p.9/23

Page 30: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

LLVM IR (2)

• Sufficiently high-level to allow targetingarbitrary concrete architectures; e.g.:

- Unbounded number of registers

- Abstraction over calling conventions

• Typed:

- Base types: integers (of different sizes),floating point numbers

G53CMP: Lecture 20 – p.9/23

Page 31: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

LLVM IR (2)

• Sufficiently high-level to allow targetingarbitrary concrete architectures; e.g.:

- Unbounded number of registers

- Abstraction over calling conventions

• Typed:

- Base types: integers (of different sizes),floating point numbers

- Derived types: pointers, arrays, vectors,structures, functions

G53CMP: Lecture 20 – p.9/23

Page 32: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

LLVM IR (2)

• Sufficiently high-level to allow targetingarbitrary concrete architectures; e.g.:

- Unbounded number of registers

- Abstraction over calling conventions

• Typed:

- Base types: integers (of different sizes),floating point numbers

- Derived types: pointers, arrays, vectors,structures, functions

• Static Single Assignment (SSA): SSA formfor all scalar registers (everything except memory).

G53CMP: Lecture 20 – p.9/23

Page 33: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

LLVM IR (3)

• Three isomorphic forms:

- Textual format (.ll)

- Compact, on-disk, “bitcode” format (.bc)

- In-memory data structure.

G53CMP: Lecture 20 – p.10/23

Page 34: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

LLVM IR (3)

• Three isomorphic forms:

- Textual format (.ll)

- Compact, on-disk, “bitcode” format (.bc)

- In-memory data structure.

Some tools:

- llvm-as: .ll⇒ .bc

- llvm-dis: .bc ⇒ .ll

G53CMP: Lecture 20 – p.10/23

Page 35: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

LLVM Modularity (1)

• Each LLVM pass, such as optimizations, is alibrary component transforming LLVM IR; e.g.

- constant folding

- loop unrolling

- motion of loop-invariant code

- inliner

G53CMP: Lecture 20 – p.11/23

Page 36: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

LLVM Modularity (1)

• Each LLVM pass, such as optimizations, is alibrary component transforming LLVM IR; e.g.

- constant folding

- loop unrolling

- motion of loop-invariant code

- inliner

• Passes are written to be as independent aspossible; any dependences are declaredexplicitly.

G53CMP: Lecture 20 – p.11/23

Page 37: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

LLVM Modularity (2)

• A pass manager can run the availablepasses in a suitable order, subject only todeclared constraints.

G53CMP: Lecture 20 – p.12/23

Page 38: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

LLVM Modularity (2)

• A pass manager can run the availablepasses in a suitable order, subject only todeclared constraints.

• Any particular application only needs toinclude exactly the those passes that arerelevant making for small footprint.

G53CMP: Lecture 20 – p.12/23

Page 39: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

SSA Form (1)

Static Single Assignment (SSA):• SSA form is a property of intermediate

representations where:

G53CMP: Lecture 20 – p.13/23

Page 40: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

SSA Form (1)

Static Single Assignment (SSA):• SSA form is a property of intermediate

representations where:

- each variable is assigned exactly once

G53CMP: Lecture 20 – p.13/23

Page 41: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

SSA Form (1)

Static Single Assignment (SSA):• SSA form is a property of intermediate

representations where:

- each variable is assigned exactly once

- every variable defined before used.

G53CMP: Lecture 20 – p.13/23

Page 42: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

SSA Form (1)

Static Single Assignment (SSA):• SSA form is a property of intermediate

representations where:

- each variable is assigned exactly once

- every variable defined before used.

• Developed at IBM in the 1980s by researchersRon Cytron, Jeanne Ferrante, Barry K.Rosen, Mark N. Wegman, Kenneth Zadeck.

G53CMP: Lecture 20 – p.13/23

Page 43: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

SSA Form (1)

Static Single Assignment (SSA):• SSA form is a property of intermediate

representations where:

- each variable is assigned exactly once

- every variable defined before used.

• Developed at IBM in the 1980s by researchersRon Cytron, Jeanne Ferrante, Barry K.Rosen, Mark N. Wegman, Kenneth Zadeck.

• Compilers using SSA include: GCC, LLVM,Oracle’s HotSpot JVM, Android’s Dalvik andRuntime.

G53CMP: Lecture 20 – p.13/23

Page 44: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

SSA Form (2)

• SSA form can for some purposes be seen asa purely functional representation.

G53CMP: Lecture 20 – p.14/23

Page 45: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

SSA Form (2)

• SSA form can for some purposes be seen asa purely functional representation.

• Indeed, there is a formal correspondencebetween SSA form and purely functionalrepresentations, notably ContinuationPassing Style (CPS).

G53CMP: Lecture 20 – p.14/23

Page 46: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

SSA Form (2)

• SSA form can for some purposes be seen asa purely functional representation.

• Indeed, there is a formal correspondencebetween SSA form and purely functionalrepresentations, notably ContinuationPassing Style (CPS).

• As a result, many compiler optimizations aresimplified and improved.

G53CMP: Lecture 20 – p.14/23

Page 47: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

Static Single Assignment (SSA) Form (3)

Conversion to SSA form by splitting eachvariable into versions. For example:

y := 1; y := 2; x := y

G53CMP: Lecture 20 – p.15/23

Page 48: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

Static Single Assignment (SSA) Form (3)

Conversion to SSA form by splitting eachvariable into versions. For example:

y := 1; y := 2; x := y

In SSA form:

y1 := 1; y2 := 2; x1 := y2

G53CMP: Lecture 20 – p.15/23

Page 49: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

Static Single Assignment (SSA) Form (3)

Conversion to SSA form by splitting eachvariable into versions. For example:

y := 1; y := 2; x := y

In SSA form:

y1 := 1; y2 := 2; x1 := y2

Note that it now is manifest (no flow analysisneeded) where the value assigned to x comesfrom and that the first assignment to y is deadcode.

G53CMP: Lecture 20 – p.15/23

Page 50: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

What about Control Flow Joins? (1)

The obvious question is how to handle joins inthe control flow.

Consider:

Before SSA conversion:

x := ...;

if x > 0 then

x := 1

else

x := 2;

y := x;

G53CMP: Lecture 20 – p.16/23

Page 51: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

What about Control Flow Joins? (1)

The obvious question is how to handle joins inthe control flow.

Consider:

Before SSA conversion:

x := ...;

if x > 0 then

x := 1

else

x := 2;

y := x;

SSA form:

x1 := ...;

if x1 > 0 then

x2 := 1

else

x3 := 2;

y1 := x???;

G53CMP: Lecture 20 – p.16/23

Page 52: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

What about Control Flow Joins (2)

Or consider:

Before SSA conversion:

x := ...;

while x < 100 do

x := x * 2;

y := x

G53CMP: Lecture 20 – p.17/23

Page 53: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

What about Control Flow Joins (2)

Or consider:

Before SSA conversion:

x := ...;

while x < 100 do

x := x * 2;

y := x

SSA form:

x1 := ...;

while x??? < 100 do

x2 := x??? * 2;

y1 := x???

G53CMP: Lecture 20 – p.17/23

Page 54: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

φ-Functions (1)

A φ-function (originally “phoney function”) selectsand returns exactly one of its arguments.Assume first it always picks the “right” argument.Then we can solve our dilemma as follows:

x1 := ...;

if x1 > 0 then

x2 := 1

else

x3 := 2;

x4 := φ(x2,x3);

y1 := x4

G53CMP: Lecture 20 – p.18/23

Page 55: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

φ-Functions (1)

A φ-function (originally “phoney function”) selectsand returns exactly one of its arguments.Assume first it always picks the “right” argument.Then we can solve our dilemma as follows:

x1 := ...;

if x1 > 0 then

x2 := 1

else Clearly in SSA form!

x3 := 2;

x4 := φ(x2,x3);

y1 := x4

G53CMP: Lecture 20 – p.18/23

Page 56: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

φ-Functions (2)

And:

x1 := ...;

while (x2 := φ1(x1,x3), x2 < 100) do

x3 := x2 * 2;

y1 := x2

Also clearly in SSA form!

G53CMP: Lecture 20 – p.19/23

Page 57: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

φ-Functions (3)

• A φ-function selects an argument according tothe dynamically preceding basic block: fromwhere did the control reach the φ-function?

G53CMP: Lecture 20 – p.20/23

Page 58: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

φ-Functions (3)

• A φ-function selects an argument according tothe dynamically preceding basic block: fromwhere did the control reach the φ-function?

• “Translating out of” SSA is essentially amatter of joining up the different versions of avariable.

G53CMP: Lecture 20 – p.20/23

Page 59: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

φ-Functions (3)

• A φ-function selects an argument according tothe dynamically preceding basic block: fromwhere did the control reach the φ-function?

• “Translating out of” SSA is essentially amatter of joining up the different versions of avariable.

• A φ-function translates into no code if thearguments and results can be stored in thesame place (register).

G53CMP: Lecture 20 – p.20/23

Page 60: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

φ-Functions (3)

• A φ-function selects an argument according tothe dynamically preceding basic block: fromwhere did the control reach the φ-function?

• “Translating out of” SSA is essentially amatter of joining up the different versions of avariable.

• A φ-function translates into no code if thearguments and results can be stored in thesame place (register).

• Otherwise extra copy instructions (assignments)are needed to translate out of SSA.

G53CMP: Lecture 20 – p.20/23

Page 61: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

Where Do φ-Functions Go? (1)

φ-functions are placed by constructing andanalysing the control flow graph:

G53CMP: Lecture 20 – p.21/23

Page 62: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

Where Do φ-Functions Go? (1)

φ-functions are placed by constructing andanalysing the control flow graph:

• A node A strictly dominates a different nodeB iff all paths to B go through A.

G53CMP: Lecture 20 – p.21/23

Page 63: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

Where Do φ-Functions Go? (1)

φ-functions are placed by constructing andanalysing the control flow graph:

• A node A strictly dominates a different nodeB iff all paths to B go through A.

• A node A dominates B iff A strictlydominates B or A = B.

G53CMP: Lecture 20 – p.21/23

Page 64: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

Where Do φ-Functions Go? (1)

φ-functions are placed by constructing andanalysing the control flow graph:

• A node A strictly dominates a different nodeB iff all paths to B go through A.

• A node A dominates B iff A strictlydominates B or A = B.

• A node B is in the dominance frontier of a nodeA iff A does not strictly dominate B, but doesdominate an immediate predecessor of B.

G53CMP: Lecture 20 – p.21/23

Page 65: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

Where Do φ-Functions Go? (1)

φ-functions are placed by constructing andanalysing the control flow graph:

• A node A strictly dominates a different nodeB iff all paths to B go through A.

• A node A dominates B iff A strictlydominates B or A = B.

• A node B is in the dominance frontier of a nodeA iff A does not strictly dominate B, but doesdominate an immediate predecessor of B.

φ-functions are placed on the dominance frontier.

G53CMP: Lecture 20 – p.21/23

Page 66: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

Where Do φ-Functions Go? (2)

Observation: we only need φ-functions for livevariables.

G53CMP: Lecture 20 – p.22/23

Page 67: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

Where Do φ-Functions Go? (2)

Observation: we only need φ-functions for livevariables.

• Pruned SSA: Use live-variable information todecide whether a particular φ-function isneeded. Expensive computation.

G53CMP: Lecture 20 – p.22/23

Page 68: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

Where Do φ-Functions Go? (2)

Observation: we only need φ-functions for livevariables.

• Pruned SSA: Use live-variable information todecide whether a particular φ-function isneeded. Expensive computation.

• Semi-pruned SSA: Identify variables that arenever live on entry to a block and omitφ-functions for such “block-local” variables.Cheaper to compute.

G53CMP: Lecture 20 – p.22/23

Page 69: G53CMP: Lecture 20 - Nottinghampsznhn/G53CMP/LectureNotes-2016/lecture20.pdf · Result SEM G53CMP 2016/17 (2) • On the whole, you were happy with the module:-All but one would recommend

LLVM Demo

We will translate the following C-code into LLVMIR using the Clang compiler, study the result andrun some optimizations on it.

int i, m, n;

int main(int argc, char* argv[]) {

sscanf(argv[1], "%d", &m);

for (i = 0; i < m; i++) {

n += i;

}

printf("n = %d\n", n);

return 0;

}G53CMP: Lecture 20 – p.23/23