Docsity

Low-Level Optimizations: CS 502 Lecture 15 - Prof. Suresh Jagannathan, Exams of Computer Science

Partial preview of the text

Download Low-Level Optimizations: CS 502 Lecture 15 - Prof. Suresh Jagannathan and more Exams Computer Science in PDF only on Docsity!

Low-Level Optimizations

CS 502

Lecture 15

Goals

 Devise optimizations specifically targeted to

improve performance on specific architectures

 Apply optimizations on low-level IR or assembly

code.

 Categories:

 General-purpose: dead-code elimination

 Control-flow:

 Branch predication

 loop optimizations

 Peephole

Unreachable and dead-code elimination  (^) Dead-code elimination:  (^) eliminate executable code that has no effect on the computation  (^) E.g., multiple stores of the same value to the same register with no intervening stores of different values  (^) Unreachable code elimination:  (^) remove code that is not reachable along any control-flow path

Straightening

 Apply to pairs of basic blocks:

 the first has no successor other than the second

 the second has no predecessor other than the first:

 second one either immediately follows the first,

or

 first must end with an unconditional branch to

the second

 For former case, nothing needs to be done; for

second case, replace branch with the second

block

Example

L1: …

a  b + c

b  c + 2

a  a + 1

if (c > 0) goto L

goto L

L6: …

goto L

L5: …

Merge block L2 into block L1. Note that an unconditional branch to L5 must be inserted to reflect the fall-through between L and L5 in the original program

If simplification

 Apply to conditional tests in which one or both

arms are empty:

 May occur as a result of code hoisting

 Cases:

 If then branch empty, reverse sense of

conditional, and eliminate the branch

 Constant-value conditional tests

 Common subexpressions in the conditional test

and in a subsequent dependent if.

Loop simplifications

 A loop whose body is empty can be eliminated

 Must ensure that iteration-control code has no

side effects, or side-effects are simple and can

be replaced by non-looping code

 Example: Loop increments/decrements a variable

by a constant, and checks against a final

constant:

 Compute final value at compile-time

 Loop unrolling

 Small number of known iterations

Example

L0: s  0

i  0

L2: if i > 4

goto L

i  i + 1

s  s + i

goto L

L1: …

L0: s  0

i  0

i  i + 1

s  s + i

i  i + 1

s  s + i

i  i + 1

s  s + i

i  i + 1

s  s + i

Example

Unswitching

 Move loop invariant conditional branches

out of loops

 May increase code space, but reduces

overall number of instructions executed

 If conditional has no else branch, must

supply one in the transformed code to set

the final value of the loop-control variable

Branch Optimizations

 (^) Eliminate branch-to-branch transfers:  (^) An unconditional branch to an unconditional branch can be replaced by a branch to the latter’s target  (^) A conditional branch to an unconditional branch can be replaced by a conditional branch to the latter’s target  (^) An unconditional branch to a conditional branch can be replaced by the conditional branch  (^) A conditional branch to a conditional branch can be replaced by a conditional branch with the former’s test and the latter’s target  (^) Latter condition must be true whenever the former is  (^) Example: if a = 0 goto L1 if a = 0 goto L .... L1: if a >= 0 goto L

Tail Merging/Cross Jumping

 Search for basic blocks with common

few last instructions

 Replace these instructions with a branch

to a common block

 Algorithm: scan backwards through

predecessors of blocks that have

multiple predecessors looking for

common instruction sequences

Conditional Moves

 A conditional move copies a source to a

target if and only if a specified condition

is satisfied.

 Found on many modern architectures

(Pentium, Sparc, etc.)

 Replace branching conditions

Example

if a > b

goto L
goto L

goto L

goto L

goto L

c  b

goto L

L1: c  a

L2: ...

t1  a > b

c  b

c  (t1) a