Docsity

Compiler Optimizations: Techniques and Techniques for Improving Code Performance, Assignments of Computer Science

Toggle sidebar
bg1
Concepts Introduced in Chapter 9
๎˜introduction to compiler optimizations
๎˜basic blocks and control flow graphs
๎˜local optimizations
๎˜global optimizations
Compiler Optimizations
๎˜Compiler optimization is a misnomer.
๎˜A code-improving transformation consists of a
sequence of changes that preserves the semantic
behavior (i.e. are safe).
๎˜A code-improving transformation attempts to make
the program
โ€“run faster
โ€“take up less space
โ€“consume less power
๎˜An optimization phase consists of a sequence of
code-improving transformations of the same type.
Types of Compiler Optimizations
๎˜Function call
๎˜Loop
๎˜Memory access
๎˜Control flow
๎˜Data flow
๎˜Machine specific
Function Call Optimizations
๎˜Procedure integration or inlining
๎˜Procedure specialization or cloning
๎˜Tail recursion elimination
๎˜Function memoization
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Compiler Optimizations: Techniques and Techniques for Improving Code Performance and more Assignments Computer Science in PDF only on Docsity!

Concepts Introduced in Chapter 9  introduction to compiler optimizations  basic blocks and control flow graphs  local optimizations  global optimizations

Compiler Optimizations

^ Compiler optimization is a misnomer. ^ A code-improving transformation consists of asequence of changes that preserves the semanticbehavior (i.e. are safe). ^ A code-improving transformation attempts to makethe program^ รฑ^ run faster^ รฑ^ take up less space^ รฑ^ consume less power ^ An optimization phase consists of a sequence ofcode-improving transformations of the same type.

Types of Compiler Optimizations  Function call  Loop  Memory access  Control flow  Data flow  Machine specific

Function Call Optimizations  Procedure integration or inlining  Procedure specialization or cloning  Tail recursion elimination  Function memoization

Loop Optimizations

^ Invariant code motion ^ Strength reduction ^ Induction variable elimination ^ Unrolling ^ Collapsing ^ Fusion ^ Software pipelining

Memory Access Optimizations  Register allocation  Memory hierarchy improvement รฑ Array padding รฑ Scalar replacement รฑ Loop interchange รฑ Prefetching

Control Flow Optimizations  Jump elimination รฑ Branch chaining รฑ Reversing branches รฑ Code positioning รฑ Loop inversion รฑ Useless jump elimination  Unreachable code elimination

Data Flow Optimizations

^ Common subexpression elimination ^ Partial redundancy elimination ^ Dead assignment elimination ^ Evaluation order determination ^ Recurrence elimination

Optimizations after Code Generation

Instruction Selection

^ Accomplished by combining RTLs. ^ Data dependences (links) are detected betweenRTLs. ^ Pairs or triples of RTLs are symbolically merged. ^ Legality is checked via a machine description.

Combining a Pair of RTLs

r[1] = r[30]+i; 27 {26}

r[2] = M[r[1]]; r[1]: ^ r[2] = M[r[30]+i]; r[1] = r[30]+i; r[1]:orr[2] = M[r[30]+i];

r[1]:

Combining Three RTLs

r[2] = M[r[3]]; 32 {31}

r[2] = r[2]+1; 33 {32}

M[r[3]] = r[2]; r[2]: ^ M[r[3]] = M[r[3]]+1; r[2] = M[r[3]]+1; r[2]:orM[r[3]] = M[r[3]]+1; r[2]:

Cascading Instruction Selection Actual example on PDP-11 (2 address machine)

r[36]=r[5]; 39 {38}

r[36]=r[36]+i; 40

r[37]=r[5]; 41 {40}

r[37]=r[37]+i; 42 {41}

r[40]=M[r[37]];

r[37]:

r[41]=1; 44 {42}

r[42]=r[40];

r[40]:

r[42]=r[42]+r[41];

r[41]:

M[r[36]]=r[42];

r[42]:r[36]:

Cascading Instruction Selection

(cont.)

r[36]=r[5]; 39 {38}

r[36]=r[36]+i; 40

r[37]=r[5]; 42 {40}

r[40]=M[r[37]+i]];

r[37]:

r[41]=1; 44 {42}

r[42]=r[40];

r[40]:

r[42]=r[42]+r[41];

r[41]:

M[r[36]]=r[42];

r[42]:r[36]:

Cascading Instruction Selection

(cont.)

r[36]=r[5]; 39 {38}

r[36]=r[36]+i; 42

r[40]=M[r[5]+i]]; 43

r[41]=1; 44 {42}

r[42]=r[40];

r[40]:

r[42]=r[42]+r[41];

r[41]:

M[r[36]]=r[42];

r[42]:r[36]:

Cascading Instruction Selection

(cont.)

r[36]=r[5]; 39 {38}

r[36]=r[36]+i; 43

r[41]=1; 44

r[42]=M[r[5]+i]]; 45 {43,44}

r[42]=r[42]+r[41];

r[41]:

M[r[36]]=r[42];

r[42]:r[36]:

Example Sequence of Optimizations

for (sum=0, j = 0; j < n; j++)sum = sum + a[j]; ^ after instruction selectionM[r[13] + sum] = 0;M[r[13] + j] = 0;PC = L18;L19r[0] = M[r[13] + j] <<2;M[r[13] + sum] = M[r[13] + sum] + M[r[0] + _a];M[r[13] + j] = M[r[13] + j] + 1;L18IC = M[r[13] + j]? M[_n];PC = IC < 0

โ†’L19;

Example Sequence of Optimizations

(cont.)

^ after register allocation^ r[2]^ = 0;^ r[1]^ = 0;PC = L18;L19r[0] =

r[1]^ << 2; r[2] =^ r[2]^ + M[r[0] + _a]; r[1] =^ r[1]^ + 1; L18IC =^

r[1]^? M[_n];PC = IC < 0^ โ†’

L19;

Example Sequence of Optimizations

(cont.)

^ after code motionr[2] = 0;r[1] = 0;^ r[4] = M[_n];^ PC = L18L19r[0] = r[1] << 2;r[2] = r[2] + M[r[0] + _a];r[1] = r[1] + 1;L18IC = r[1]?

r[4] ; PC = IC < 0

โ†’^ L19;

Example Sequence of Optimizations (cont.)^ 

after loop strength reductionr[2] = 0;r[1] = 0;r[4] = M[_n];^ r[3] = _a;^ PC = L18L19r[2] = r[2] + M[

r[3] ]; r[3] = r[3] + 4; r[1] = r[1] + 1;L18IC = r[1]? r[4];PC = IC < 0

โ†’^ L19;

Example Sequence of Optimizations

(cont.)

^ after induction variable eliminationr[2] = 0;r[4] = M[_n] << 2;r[3] = _a;^ r[4] = r[4] + r[3];^ PC = L18;L19r[2] = r[2] + M[r[3]];r[3] = r[3] + 4;L18IC =^

r[3]^?^ r[4]

; PC = IC < 0

โ†’^ L19;

Example of Common Subexpression

Elimination

r[1] = M[r[13] + i] << 2;r[1] = M[r[1] + _b];r[2] = M[r[13] + i] << 2;r[2] = M[r[2] + _b];  r[1] = M[r[13] + i] << 2;r[1] = M[r[1] + _b];r[2] = r[1];

Example of Unreachable Code

Elimination

 PC = L12;r[1] = M[r[13] + i];r[1] = r[5] + r[1];M[r[13] + j] = r[1];L13   PC = L12;L13 

Example of Branch Chaining

IF a THEN b

While c DO d

compiles into

compiles into

a^

L2:^ c

PC = IC != 0

โ†’L1;^

PC = IC != 0

โ†’L3;

b^

d

L1:^

PC = L2;L3: