Intro to Optim 1

download Intro to Optim 1

of 19

Transcript of Intro to Optim 1

  • 7/28/2019 Intro to Optim 1

    1/19

    Introduction toMachine-Independent OptimizationsPart 1

    Y.N. Srikant

    Department of Computer ScienceIndian Institute of Science

    Bangalore 560 012

    NPTEL Course on Compiler Design

    Y.N. Srikant Introduction to Optimizations

    http://find/http://goback/
  • 7/28/2019 Intro to Optim 1

    2/19

    Outline of the Lecture

    What is code optimization?

    Types of code optimizationsIllustrations of code optimizations

    Y.N. Srikant Introduction to Optimizations

    http://find/http://goback/
  • 7/28/2019 Intro to Optim 1

    3/19

    Machine-independent Code Optimization

    Intermediate code generation process introduces many

    inefcienciesExtra copies of variables, using variables instead ofconstants, repeated evaluation of expressions, etc.

    Code optimization removes such inefciencies andimproves codeImprovement may be time, space, or power consumptionIt changes the structure of programs, sometimes of beyondrecognition

    Inlines functions, unrolls loops, eliminates some

    programmer-dened variables, etc.Code optimization consists of a bunch of heuristics andpercentage of improvement depends on programs (may bezero also)

    Y.N. Srikant Introduction to Optimizations

    http://find/
  • 7/28/2019 Intro to Optim 1

    4/19

    Examples of Machine-Independant Optimizations

    Global common sub-expression eliminationCopy propagationConstant propagation and constant foldingLoop invariant code motion

    Induction variable elimination and strength reductionPartial redundancy eliminationLoop unrollingFunction inlining

    Tail recursion removalVectorization and ConcurrentizationLoop interchange, and loop blocking

    Y.N. Srikant Introduction to Optimizations

    http://find/
  • 7/28/2019 Intro to Optim 1

    5/19

    Data-ow Analysis

    Code optimization needs information about the programwhich expressions are being recomputed in a function?Which expressions are partially redundant?which denitions reach a point?Which copies and constants can be propagated? Etc.

    All such information is gathered through data-ow analysis

    Y.N. Srikant Introduction to Optimizations

    http://find/
  • 7/28/2019 Intro to Optim 1

    6/19

    Bubble Sort Running Example

    Y.N. Srikant Introduction to Optimizations

    http://find/
  • 7/28/2019 Intro to Optim 1

    7/19

  • 7/28/2019 Intro to Optim 1

    8/19

    GCSE Conceptual Example

    Y.N. Srikant Introduction to Optimizations

    http://find/http://goback/
  • 7/28/2019 Intro to Optim 1

    9/19

    GCSE on Running Example - 1

    Y.N. Srikant Introduction to Optimizations

    l

    http://find/
  • 7/28/2019 Intro to Optim 1

    10/19

    GCSE on Running Example - 2

    Y.N. Srikant Introduction to Optimizations

    C P i R i E l

    http://find/
  • 7/28/2019 Intro to Optim 1

    11/19

    Copy Propagation on Running Example

    Y.N. Srikant Introduction to Optimizations

    GCSE d C P i R i E l

    http://find/
  • 7/28/2019 Intro to Optim 1

    12/19

    GCSE and Copy Propagation on Running Example

    Y.N. Srikant Introduction to Optimizations

    C t t P ti d F ldi E l

    http://find/
  • 7/28/2019 Intro to Optim 1

    13/19

    Constant Propagation and Folding Example

    Y.N. Srikant Introduction to Optimizations

    L I i t C d ti E l

    http://find/
  • 7/28/2019 Intro to Optim 1

    14/19

    Loop Invariant Code motion Example

    Y.N. Srikant Introduction to Optimizations

    Strength Reduction

    http://find/http://goback/
  • 7/28/2019 Intro to Optim 1

    15/19

    Strength Reduction

    Y.N. Srikant Introduction to Optimizations

    Induction Variable Elimination

    http://find/http://goback/
  • 7/28/2019 Intro to Optim 1

    16/19

    Induction Variable Elimination

    Y.N. Srikant Introduction to Optimizations

    Induction Variable Elimination and Strength Reduction

    http://find/http://goback/
  • 7/28/2019 Intro to Optim 1

    17/19

    Induction Variable Elimination and Strength Reduction

    Y.N. Srikant Introduction to Optimizations

    Partial Redundancy Elimination

    http://find/http://goback/
  • 7/28/2019 Intro to Optim 1

    18/19

    Partial Redundancy Elimination

    Y.N. Srikant Introduction to Optimizations

    Unrolling a For-loop

    http://find/http://goback/
  • 7/28/2019 Intro to Optim 1

    19/19

    Unrolling a For-loop

    Y.N. Srikant Introduction to Optimizations

    http://find/