Docsity

Really Basic Stuff - Advanced Compiling Techniques - Lecture Slides, Slides of Compilers

Partial preview of the text

Download Really Basic Stuff - Advanced Compiling Techniques - Lecture Slides and more Slides Compilers in PDF only on Docsity!

Really Basic Stuff

Flow Graphs Constant Folding Global Common Subexpressions

Induction Variables/Reduction in Strength

Dawn of Code Optimization

  • A never-published Stanford technical report by Fran Allen in 1968.
  • Flow graphs of intermediate code.
  • Key things worth doing.

Basic Blocks

4

i = 0

if i>=n goto …

t1 = 8*i A[t1] = 1 i = i+

Induction Variables

  • x is an induction variable in a loop if it takes on a linear sequence of values each time through the loop.
  • Common case: loop index like i and computed array index like t1.
  • Eliminate “superfluous” induction variables.
  • Replace multiplication by addition ( reduction in strength ).

Loop-Invariant Code Motion

  • Sometimes, a computation is done each time around a loop.
  • Move it before the loop to save n- computations. - Be careful: could n=0? I.e., the loop is typically executed 0 times.

Example

8

i = 0

if i>=n goto …

t1 = y+z x = x+t i = i+

i = 0 t1 = y+z

if i>=n goto …

x = x+t i = i+

Example

10

i = 0 n = 100

if i>=n goto …

t1 = 8*i A[t1] = 1 i = i+

t1 = 0

if t1>=800 goto …

A[t1] = 1 t1 = t1+

Global Common Subexpressions

  • Suppose block B has a computation of x+y.
  • Suppose we are sure that when we reach this computation, we are sure to have:
  1. Computed x+y, and
  2. Not subsequently reassigned x or y.
  • Then we can hold the value of x+y and use it in B.

Example --- Even Better

13

t = x+y a = t

b = t

c = t

t = x+y a = t

t = x+y b = t

c = t

t = x+y b = t