Download presentation
Presentation is loading. Please wait.
1
CS 201 Compiler Construction
Code Optimizations: Constant Propagation & Folding
2
Constant Propagation & Folding
1. Use of var replaced by a constant value - propagation. 2. Exp. is evaluated or folded if all of its operands are constants.
3
Constant Propagation & Folding
Evaluation of branch outcomes may be enabled. 2. Can take advantage of non-executable paths.
4
Problem Characteristics
Not Partitionable Values of variables are interdependent. if Y > 0 then X = 2 endif Z = X + 1 Z depends upon X and Y. Not a bit-vector problem The solution is not true/false rather can be any constant value.
5
Data Flow Analysis Constant Prop. Lattice for a Single variable.
6
Contd.. Meet Operator for a Single variable.
7
Contd.. Lattice for all variables (L,∧‘) VAR – set of variables
VAL – set of values (all constants, Τ, ) L = {β: β is a total function β: VARVAL} i.e., set of all states of variables. forall v ε VAR β1 ∧’ β2(v) = β1 (v) ∧ β2 (v)
8
Contd.. Transfer Function S: A = B op C
fS(β) – transfer function of S. fS(β) =
9
Contd.. Transfer Function S: A = c, constant assignment
S: A = B, copy statement S: A < B, conditional
10
Example
11
Worklist Algorithm Each node is a single statement
IN and OUT contain the lattice values at node entry and exit Algorithm will take advantage of resolved branches. How ? Mark edges as executable or non-executable and propagate information only along executable edges Branch (true) (false) (true or false) – mark edges executable accordingly
12
Algorithm: Initialization
Mark all edges as unexecutable Worklist { n0 } (start node in the CFG) IN[n0] = β (none of the variables is a constant) ∨ n ε N – {n0}, IN[n] = OUT[n] = β (all undefined) Τ
13
Algorithm: Analysis p ε Pred(n) While Worklist ≠ Φ do
get n from Worklist IN[n] = Λ’ OUT[p] st p n is executable if n is an assignment statement then n: A = B op C or A = c or A = B OUT[n] = fn ( IN[n] ) mark outgoing edges as executable else – n is a predicate evaluate condition: value is true or false or not-a-constant mark appropriate edges executable true edge or false edge or both endif if OUT[n] changed then add Succ(n) to Worklist endwhile p ε Pred(n)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.