Introduction to Advanced Topics Chapter 1

28
Introduction to Advanced Topics Chapter 1 Mooly Sagiv Schrierber 317 03-640-7606 www.math.tau.ac.il/~sagiv/ courses/acd.html

description

Introduction to Advanced Topics Chapter 1. Mooly Sagiv Schrierber 317 03-640-7606 www.math.tau.ac.il/~sagiv/courses/acd.html. Outline. Course requirements Review of compiler structure Advanced issues in elementary topics The importance of optimizations Structure of optimizing compilers - PowerPoint PPT Presentation

Transcript of Introduction to Advanced Topics Chapter 1

Page 1: Introduction to  Advanced Topics Chapter 1

Introduction to Advanced Topics

Chapter 1

Mooly SagivSchrierber 31703-640-7606

www.math.tau.ac.il/~sagiv/courses/acd.html

Page 2: Introduction to  Advanced Topics Chapter 1

Outline• Course requirements• Review of compiler structure• Advanced issues in

elementary topics• The importance of optimizations• Structure of optimizing compilers• Placement of optimizations • Advanced topics

Page 3: Introduction to  Advanced Topics Chapter 1

Course Requirements

• Prepare course notes 10%

• Theoretical assignments 30%

• Final home exam 60%

Page 4: Introduction to  Advanced Topics Chapter 1

Compiler Structure

Symbol table and

access routines

OS

Interface

String of characters

Scanner

tokens

Semantic

analyzer

Parser

Code Generator

IR

AST

Object code

Page 5: Introduction to  Advanced Topics Chapter 1

Advanced Issues inElementary Topics

• Symbol table management(3)– Efficiency– Overloading

• Type Inference• Intermediate Language Selection(4)• Run-Time Support(5)• Producing Code Generators (6)

Page 6: Introduction to  Advanced Topics Chapter 1

procedure BACH is procedure put (x: boolean) is begin null; end; procedure put (x: float) is begin null; end; procedure put (x: integer) is begin null; end; package x is type boolean is (false, true); function f return boolean; -- (D1) end x; package body x is function f return boolean is begin null; end; end x; function f return float is begin null; end; -- (D2) use x; begin put (f); -- (A1) A: declare f: integer; -- (D3) begin put (f); -- (A2) B: declare function f return integer is begin null; end; -- (D4) begin put (f); -- (A3) end B; end A; end BACH;

Page 7: Introduction to  Advanced Topics Chapter 1

Advanced Issues inElementary Topics

• Symbol table management(3)– Efficiency– Overloading

• Type Inference• Intermediate Language Selection(4)• Run-Time Support(5)• Producing Code Generators (6)

Page 8: Introduction to  Advanced Topics Chapter 1

Advanced Issues inElementary Topics

• Symbol table management(3)– Efficiency– Overloading

• Type Inference• Intermediate Language Selection(4)• Run-Time Support(5)• Producing Code Generators (6)

Page 9: Introduction to  Advanced Topics Chapter 1

Intermediate Language Selection• Low Vs. High level control flow structures• Flat Vs. Hierarchical (tree)• Machine Vs. High level of instructions• (Symbolic) Registers Vs. Stack• Normal forms (SSA)• Intermediate forms: Control Flow Graph, Call

Graph, Program Dependence Graph• Issues: Engineering, efficiency, portability,

optimization level

Page 10: Introduction to  Advanced Topics Chapter 1

HIR

for v v1 by v2 to v3 do

a[i] :=2

endfor

MIR

v v1

t2 v2

t3 v3

L1: if v >t3 goto L2

t4 addr a

t5 4*i

t6 t4+t5

*t6 2

v v + t2

goto L1

L2:

LIR

s2 s1

s4 s3

s6 s5

L1: if s2 >s6 goto L2

s7 addr a

s8 4*s9

s10 s7+s8

[s10] 2

s2 s2 + s4

goto L1

L2:

IRs in the Book

Page 11: Introduction to  Advanced Topics Chapter 1

Single Static Assignment Form (SSA) s2 s1

s4 s3

s6 s5

L1: if s2 >s6 goto L2

s7 addr a

s8 4*s9

s10 s7+s8

[s10] 2

s2 s2 + s4

L2:

B2

s21 s1

s4 s3

s4 s3

B1

s22 (s21 , s23)

s22>=s6

N

s7 addr a

s8 4*s9

s10 s7+s8

[s10] 2

s23 s22 + s4

Y

B3

Page 12: Introduction to  Advanced Topics Chapter 1

Advanced Issues inElementary Topics

• Symbol table management(3)– Efficiency– Overloading

• Type Inference• Intermediate Language Selection(4)• Run-Time Support(5)• Producing Code Generators (6)

Page 13: Introduction to  Advanced Topics Chapter 1

Run-Time Support

• Data representation and instructions

• Register usage

• Stack frames (activation records)

• Parameter passing disciplines

• Symbolic and polymorphic language support

• Garbage Collection

Page 14: Introduction to  Advanced Topics Chapter 1

Advanced Issues inElementary Topics

• Symbol table management(3)– Efficiency– Overloading

• Type Inference• Intermediate Language Selection(4)• Run-Time Support(5)• Producing Code Generators (6)

Page 15: Introduction to  Advanced Topics Chapter 1

The Importance of Optimizations

• One pass compilers produce slow code

• Much of the time spent in loops – optimize loops

• Machines can be simpler and faster if optimizing compilers are used (RISC, VLIW)

• Programs are complex and general

• Compilers need to be modular

Page 16: Introduction to  Advanced Topics Chapter 1

C code

int a, b, c, d;

c = a + b;

d = c + 1;

SPARC code

ldw a, r1

ldw b, r2

add r1, r2, r3

stw r3, c

ldw c, r3

add r3, 1, r4

stw r4, d

Optimized

SPARC code

add r1, r2, r3

add r3, 1, r4

10 cycles

2 cycles

Page 17: Introduction to  Advanced Topics Chapter 1

Application Dependent Optimizations

• Functional programs– replacement of recursion by loops

(tail-calls elimination)– replacement of heap by stack

• Object Oriented Programs– Dead member elimination– Replacement of virtual by static function

• Numeric Code

• Database Code

Page 18: Introduction to  Advanced Topics Chapter 1

String

Object

Scanner

Parser

Semantic

LIR generator

Optimizer

Fin. assembly

Tokens

AST

AST

LIR

LIR

Object

String

Scanner

Parser

Semantic

IR generator

Optimizer

Code generator

Tokens

AST

AST

MIR

MIR

LIRPost. optimizer

Page 19: Introduction to  Advanced Topics Chapter 1

Mixed vs. Low Level Optimizers

• Mixed– Sun-SPARC, Dec. Alpha, SGI-MIPS,

Intel’s 386Easier to portMore efficient compilation?Supports CISC

• Low Level• HP-PA-RISC/IBM-Power PCMore efficient codeConceptually simpler (in RISC)

• Used for multiple programming languages

Page 20: Introduction to  Advanced Topics Chapter 1

Translation by Preprocessing

• Some programming languages are translated into C– Elimination of includes, C++(cfront), Haskel

• Quick and portable solution

• C supports some indirect source level debugging

• Other examples: Fortran into “vector” Fortran program

Page 21: Introduction to  Advanced Topics Chapter 1

Data-Cache Optimizations(20)String

Scanner

Parser

Semantic

Data-cache optimizer

Tokens

AST

HIR

MIR

Page 22: Introduction to  Advanced Topics Chapter 1

IBM PowerPC compilerString

Object

Scanner

Parser

Semantic

LIR generator

Optimizer

Fin. assembly

Tokens

AST

AST

LIR=XIL

LIR

Data-cache optimizer

Low-to-High

High-to-Low

HIR=YIL

HIR=YIL

Page 23: Introduction to  Advanced Topics Chapter 1

Outline• Review of compiler structure

• Advanced issues inelementary topics

• The importance of optimizations

• Structure of optimizing compilers

• Placement of optimizations

• Advanced Topics

Page 24: Introduction to  Advanced Topics Chapter 1

Scalar replacement of array referencesData-cache optimizations

Procedure integration

Tail-call elimination

Scalar replacement of aggregates

Sparse constant propagation

Interprocedural constant propagation

Procedure specialization and cloning

Sparse conditional constant propagation

Global value numbering

Local and global copy propagation

Sparse conditional constant propagation

Dead code elimination

Common Subexpression Elimination

Loop invariant code motionPartial redundency Elimination

Inline expansion

Leaf-routine optimizations

Instruction Scheduling 1

Register allocation

Instruction Scheduling 2

Intraprocedural I-cache optimizations

Instruction prefetching

Data prefertching

Branch predication

Interprocedural register allocation

Interprocedural I-cache optimization

Page 25: Introduction to  Advanced Topics Chapter 1

Scalar replacement after constant propagation

a 1

a= 1

Y N

B.x 1.0

B.y 1.0read B.x

read B.y

B.x = 1.0

Y N

c

c

c B.x

Page 26: Introduction to  Advanced Topics Chapter 1

Scalar replacement before constant propagation

B.x 1.0

c 2

read B.y

B.x = 1.0

Y N

B.x B.x + c

d

d

d d

d B.x

Page 27: Introduction to  Advanced Topics Chapter 1

Theoretically Open Questions

• Picking the “right” order

• Combining optimizations

• Proving correctness

Page 28: Introduction to  Advanced Topics Chapter 1

Advanced Topics

• Code Profiling

• Parallelization and vectorization

• Just in time compilation

• Optimize for Power

• Trusted compilers