Docsity

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

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 **