A global optimization Changes an Entire Method - Slides | CS 4610, Study notes of Programming Languages

Material Type: Notes; Professor: Weimer; Class: Programming Languages; Subject: Computer Science; University: University of Virginia; Term: Spring 2008;

Typology: Study notes

Pre 2010

Uploaded on 03/19/2009

koofers-user-1c8-2
koofers-user-1c8-2 🇺🇸

9 documents

1 / 68

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
#1
(How
(How Not
Not To Do)
To Do)
Global Optimizations
Global Optimizations
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44

Partial preview of the text

Download A global optimization Changes an Entire Method - Slides | CS 4610 and more Study notes Programming Languages in PDF only on Docsity!

(How (How NotNot To Do)To Do)

Global OptimizationsGlobal Optimizations

One-Slide Summary

  • (^) A global optimization changes an entire method (consisting of multiple basic blocks).
  • (^) We must be conservative and only apply global optimizations when they preserve the original semantics.
  • (^) We use global flow analyses to determine if it is OK to apply an optimization.
  • (^) Flow analyses are built out of simple transfer functions and can work forwards or backwards.

Local Optimization

Recall the simple basic-block optimizations

  • (^) Constant propagation
  • (^) Dead code elimination X := 3 Y := Z * W Q := X + Y X := 3 Y := Z * W Q := 3 + Y Y := Z * W Q := 3 + Y

Global Optimization

These optimizations can be extended to an entire control-flow graph X := 3 B > 0 Y := Z + W Y := 0 A := 2 * X

Global Optimization

These optimizations can be extended to an entire control-flow graph X := 3 B > 0 Y := Z + W Y := 0 A := 2 * 3

Correctness

  • (^) How do we know it is OK to globally propagate constants?
  • (^) There are situations where it is incorrect: X := 3 B > 0 Y := Z + W X := 4 Y := 0 A := 2 * X

Example 1 Revisited

X := 3 B > 0 Y := Z + W Y := 0 A := 2 * X

Example 2 Revisited

X := 3 B > 0 Y := Z + W X := 4 Y := 0 A := 2 * X

Global Analysis

Global optimization tasks share several traits:

  • (^) The optimization depends on knowing a property P at a particular point in program execution
  • (^) Proving P at any point requires knowledge of the entire method body
  • (^) Property P is typically undecidable****!

Undecidability

of Program Properties

  • (^) Rice’s Theorem : Most interesting dynamic properties of a program are undecidable: - (^) Does the program halt on all (some) inputs? - (^) This is called the halting problem - (^) Is the result of a function F always positive? - Assume we can answer this question precisely - Take function H and find out if it halts by testing function F(x) { H(x); return 1; } whether it has positive result - Contradition!
  • (^) Syntactic properties are decidable!
    • (^) e.g., How many occurrences of “x” are there?
  • (^) Programs without looping are also decidable!

Conservative Program Analyses

  • (^) So, we cannot tell for sure that “x” is always 3
    • (^) Then, how can we apply constant propagation?
  • (^) It is OK to be conservative. If the optimization requires P to be true, then want to know either - (^) P is definitely true - (^) Don’t know if P is true
  • (^) Let's call this truthiness

Conservative Program Analyses

  • (^) So, we cannot tell for sure that “x” is always 3
    • (^) Then, how can we apply constant propagation?
  • (^) It is OK to be conservative. If the optimization requires P to be true, then want to know either - (^) P is definitely true - (^) Don’t know if P is true
  • (^) It is always correct to say “don’t know”
    • (^) We try to say don’t know as rarely as possible
  • (^) All program analyses are conservative

Global Optimization: Review

X := 3 B > 0 Y := Z + W Y := 0 A := 2 * 3 X := 3 B > 0 Y := Z + W X := 4 Y := 0 A := 2 * X

Global Optimization: Review

X := 3 B > 0 Y := Z + W Y := 0 A := 2 * 3 X := 3 B > 0 Y := Z + W X := 4 Y := 0 A := 2 * 3

  • (^) To replace a use of x by a constant k we must know that: On every path to the use of x, the last assignment to x is x := k **