Docsity

Optimization - Programming Language Translation - Lecture Slides, Slides of Programming Languages

Partial preview of the text

Download Optimization - Programming Language Translation - Lecture Slides and more Slides Programming Languages in PDF only on Docsity!

Overview

 Motivation and Background

 Code Level Optimization

 Common Sub-expression elimination  Copy Propagation  Dead-code elimination

 Peephole optimization

 Load/Store elimination  Unreachable code  Flow of Control Optimization  Algebraic simplification  Strength Reduction

 Concluding Remarks/Looking Ahead

Motivation

 What we achieved

 We have working machine code

 What is missing

 Code generation does not see the “big” picture  We can generate poor instruction sequences

 What we need

 A simple way to locally improve the code quality

 Goal:

 Transition from “Lousy” Intermediate Code to More Effective and Efficient Code  Response Time, Performance (Algorithms), Memory Usage  Measured in terms of Number of Variables Saved, Operands Saved, Memory Accesses, etc.

Code Level Optimization

 First Look at Optimization

 Section 9.4 in 1 st^ Edition  Introduce and Discuss Basic Blocks

 Requirements for Optimization

 Section 10.1 in 1 st^ Edition  Basic Blocks, Flow Graphs

 Indepth Examination of Optimization

 Section 10.2 in 1 st^ Edition  Function Preserving Transformations  Loop Optimizations

First Look at Optimization

 Optimization Applied to 3 Address Coding (3AC)

Version of Source Program - Examples:  A + B[i] * c t1 = b[i] t2 = t1 * a t3 = t2 * c

First Look at Optimization

 Steps 1 to 12 from two Slides Back Represented as:

 Optimization Works with Basic Blocks and Flow

Graph to Perform Transformations that:  Generate Equivalent Flow Graph w/Improved Perf.

First Look at Optimization

 Optimization will Perform Transformations on Basic

Blocks/Flow Graph

 Resulting Graph(s) Passed Through to Final Code

Generation to Obtain More Optimal Code

 Two Fold Goal of Optimization

 Reduce Time  Reduce Space

 Optimization Used to Come at a Cost:

 In “Old Days” Turning on Optimizer Could Double the Compilation Time  From 2 hours to 4 hours

 Is this an Issue Today?

Structure Preserving Transformations

 Common Sub-Expression Elimination

 How can Following Code be Improved? a = b + c b = a – d c = b + c d = a – d  What Must Make Sure Doesn’t happen?

 Dead-Code Elimination

 If x is not Used in Block, Can it be Removed? x = y + z  What are the Possible Ramifications if so?

d = b

Structure Preserving Transformations

 Renaming Temporary Variables

 Consider the code t = b + c  Can be Changed to u = b + c  May Reduce the Number of temporaries  Make Change from all t’s to all u’s

 Interchange of Statements

 Consider and Change to: t1 = b + c t2 = x + y t2 = x + y t1 = b + c  This can Occur as Long as:  x and y not t  b and c not t  What Do you have to Check?

Requirements for Optimization

 Criteria for Transformations

 Preserve Meaning of Code Don’t Change Output, Introduce Errors, etc.  Speed up Programs by Measurable Amount (on Average for Entire Code)  Must be Work the Effort Stick to Meaningful, Useful Transformations  Provide Different Versions of Compiler  Non-Optimizing  Optimizing  Extra Optimization on Demand

Requirements for Optimization

 Beware that Some Optimization Directives are

Ignored!  In C, Define variable as “register int I;”  While a Feature of Language, cc States that these Instructions are Ignored and Compiler Controls Use of Registers

Example Source Code

Generated Three Address Coding

Indepth Examination of Optimization

 Code-Transformation Techniques:

 Local – within a “Basic Block”  Global – between “Basic Blocks”

 Data Flow Dependencies Determined by Inspection

what do i, a, and v refer to?

 Dependent in Another Basic Block

 Scoping is Very Critical

Indepth Examination of Optimization

 Function Preserving Transformations

 Common Subexpressions  Copy Propagation  Deal Code Elimination

 Loop Optimizations

 Code Motion  Induction Variables  Strength Reduction