170 likes | 186 Views
Learn about constant propagation analysis, a static technique used in modern compilers to optimize program execution by determining possible values at different program points. Discover how to compute values for expressions, maintain sets, and perform transfer functions in dataflow analysis.
E N D
Constant Propagation Harry Xu CS 142 (b) 02/19/2013
Dataflow Analysis • A static technique for gathering information about the possible set of values calculated at various points in a program (wikipedia) • Widely used in modern compilers • Performed on top of a control flow graph • Iterate over the statements of a function to solve dataflow equations • Liveness analysis is a type of dataflow analysis
Constant Propagation • If the result of an expression is known at compile time, we do not need to execute it a = 123; b = a – 12; c = a + b; d = c * b + a; • a = 123; • b = 111; • c = 234; • d = 26097;
How to Compute Values for Expressions • At each statement, maintain a set C of values for each variable • C is a singleton set for constant propagation • C contains either a constant value v, or a symbol ⊥(bottom), or a symbol ⊤ (top) • Meanings of the values • v: the variable must have this constant value during execution • ⊥:the initial (known) value of each variable • ⊤: the variable may have different values during execution
Transfer Function and Join • Transfer function defines how to update dataflow facts at each statement • C[s: a = b + c] = valuea [s] • valuea [s] = {valueb[s] + valuec[s]} if they are both values {⊤} otherwise • Join operation defines what happens when different control flow paths merge • valuea[s] = join s1,s2 ∊prec(s)(valuea[s1], valuea[s2] ) join s1,s2 ∊prec(s)(valueb[s1], valueb[s2] ) = valueb[s1] if valueb[s1] = valueb[s2] = {⊤} otherwise
Constant Propagation Analysis • C1 = {⊥} • C2 = {⊥} • C3 = {⊥} • C4 = {⊥} • C5 = {⊥} • C6 = {⊥} • C7 = {⊥} • C8 = {⊥} • C9 = {⊥} • C10 = {⊥} L1: a = 3; L2: b = 5; L3: d = 4; L4: x = 100; L5: if a > b then L6: c = a + b; L7: d = 2; L8: endif L9: c = 4; L10:return b * d + c; C[s: a = b + c] = valuea [s] valueb[s] = join s1,s2 ∊prec(s)(valueb[s1], valueb[s2] ) • Worklist = {L1}
Constant Propagation Analysis • C1 = {3} • C2 = {⊥} • C3 = {⊥} • C4 = {⊥} • C5 = {⊥} • C6 = {⊥} • C7 = {⊥} • C8 = {⊥} • C9 = {⊥} • C10 = {⊥} L1: a = 3; L2: b = 5; L3: d = 4; L4: x = 100; L5: if a > b then L6: c = a + b; L7: d = 2; L8: endif L9: c = 4; L10:return b * d + c; • valuea[L1]= {3} • Valueb[L1]= {⊥} • Valued[L1]= {⊥} • valuex[L1] = {⊥} • valuec[L1] = {⊥} • Worklist = {L2} C[s: a = b + c] = valuea [s] valueb[s] = join s1,s2 ∊prec(s)(valueb[s1], valueb[s2] )
Constant Propagation Analysis • C1 = {3} • C2 = {5} • C3 = {⊥} • C4 = {⊥} • C5 = {⊥} • C6 = {⊥} • C7 = {⊥} • C8 = {⊥} • C9 = {⊥} • C10 = {⊥} L1: a = 3; L2: b = 5; L3: d = 4; L4: x = 100; L5: if a > b then L6: c = a + b; L7: d = 2; L8: endif L9: c = 4; L10:return b * d + c; • valuea[L2]= {3} • valueb[L2]= {5} • valued[L2]= {⊥} • valuex[L2]= {⊥} • valuec[L2]= {⊥} • Worklist = {L3} C[s: a = b + c] = valuea [s] valueb[s] = join s1,s2 ∊prec(s)(valueb[s1], valueb[s2] )
Constant Propagation Analysis • C1 = {3} • C2 = {5} • C3 = {4} • C4 = {⊥} • C5 = {⊥} • C6 = {⊥} • C7 = {⊥} • C8 = {⊥} • C9 = {⊥} • C10 = {⊥} L1: a = 3; L2: b = 5; L3: d = 4; L4: x = 100; L5: if a > b then L6: c = a + b; L7: d = 2; L8: endif L9: c = 4; L10:return b * d + c; • valuea[L3] = {3} • valueb[L3] = {5} • valued[L3] = {4} • valuex[L3] = {⊥} • valuec[L3] = {⊥} • Worklist = {L4} C[s: a = b + c] = valuea [s] valueb[s] = join s1,s2 ∊prec(s)(valueb[s1], valueb[s2] )
Constant Propagation Analysis • C1 = {3} • C2 = {5} • C3 = {4} • C4 = {100} • C5 = {⊥} • C6 = {⊥} • C7 = {⊥} • C8 = {⊥} • C9 = {⊥} • C10 = {⊥} L1: a = 3; L2: b = 5; L3: d = 4; L4: x = 100; L5: if a > b then L6: c = a + b; L7: d = 2; L8: endif L9: c = 4; L10:return b * d + c; • valuea[L4] = {3} • valueb[L4] = {5} • valued[L4] = {4} • valuex[L4] = {100} • valuec[L4] = {⊥} • Worklist = {L5} C[s: a = b + c] = valuea [s] valueb[s] = join s1,s2 ∊prec(s)(valueb[s1], valueb[s2] )
Constant Propagation Analysis • C1 = {3} • C2 = {5} • C3 = {4} • C4 = {100} • C5 = {⊥} • C6 = {⊥} • C7 = {⊥} • C8 = {⊥} • C9 = {⊥} • C10 = {⊥} L1: a = 3; L2: b = 5; L3: d = 4; L4: x = 100; L5: if a > b then L6: c = a + b; L7: d = 2; L8: endif L9: c = 4; L10:return b * d + c; • valuea[L5] = {3} • valueb[L5] = {5} • valued[L5] = {4} • valuex[L5] = {100} • valuec[L5] = {⊥} • Worklist = {L6} C[s: a = b + c] = valuea [s] valueb[s] = join s1,s2 ∊prec(s)(valueb[s1], valueb[s2] )
Constant Propagation Analysis • C1 = {3} • C2 = {5} • C3 = {4} • C4 = {100} • C5 = {⊥} • C6 = {8} • C7 = {⊥} • C8 = {⊥} • C9 = {⊥} • C10 = {⊥} L1: a = 3; L2: b = 5; L3: d = 4; L4: x = 100; L5: if a > b then L6: c = a + b; L7: d = 2; L8: endif L9: c = 4; L10:return b * d + c; • valuea[L6] = {3} • valueb[L6] = {5} • valued[L6] = {4} • valuex[L6] = {100} • valuec[L6] = {8} • Worklist = {L7} C[s: a = b + c] = valuea [s] valueb[s] = join s1,s2 ∊prec(s)(valueb[s1], valueb[s2] )
Constant Propagation Analysis • C1 = {3} • C2 = {5} • C3 = {4} • C4 = {100} • C5 = {⊥} • C6 = {8} • C7 = {2} • C8 = {⊥} • C9 = {⊥} • C10 = {⊥} L1: a = 3; L2: b = 5; L3: d = 4; L4: x = 100; L5: if a > b then L6: c = a + b; L7: d = 2; L8: endif L9: c = 4; L10:return b * d + c; • valuea[L7] = {3} • valueb[L7] = {5} • valued[L7] = {2} • valuex[L7] = {100} • valuec[L7] = {8} • Worklist = {L8} C[s: a = b + c] = valuea [s] valueb[s] = join s1,s2 ∊prec(s)(valueb[s1], valueb[s2] )
Constant Propagation Analysis • C1 = {3} • C2 = {5} • C3 = {4} • C4 = {100} • C5 = {⊥} • C6 = {8} • C7 = {2} • C8 = {⊥} • C9 = {⊥} • C10 = {⊥} L1: a = 3; L2: b = 5; L3: d = 4; L4: x = 100; L5: if a > b then L6: c = a + b; L7: d = 2; L8: endif L9: c = 4; L10:return b * d + c; • valuea[L8] = {3} • valueb[L8] = {5} • valued[L8] = {2} • valuex[L8] = {100} • valuec[L8] = {8} • Worklist = {L9} C[s: a = b + c] = valuea [s] valueb[s] = join s1,s2 ∊prec(s)(valueb[s1], valueb[s2] )
Constant Propagation Analysis • C1 = {3} • C2 = {5} • C3 = {4} • C4 = {100} • C5 = {⊥} • C6 = {8} • C7 = {2} • C8 = {⊥} • C9 = {4} • C10 = {⊥} L1: a = 3; L2: b = 5; L3: d = 4; L4: x = 100; L5: if a > b then L6: c = a + b; L7: d = 2; L8: endif L9: c = 4; L10:return b * d + c; • valuea[L9] = {3} • valueb[L9] = {5} • valued[L9] = {⊤} • valuex[L9] = {100} • valuec[L9] = {4} • Worklist = {L10} C[s: a = b + c] = valuea [s] valueb[s] = join s1,s2 ∊prec(s)(valueb[s1], valueb[s2] )
Constant Propagation Analysis • C1 = {3} • C2 = {5} • C3 = {4} • C4 = {100} • C5 = {⊥} • C6 = {8} • C7 = {2} • C8 = {⊥} • C9 = {4} • C10 = {⊤} L1: a = 3; L2: b = 5; L3: d = 4; L4: x = 100; L5: if a > b then L6: c = a + b; L7: d = 2; L8: endif L9: c = 4; L10:return b * d + c; • valuea[L10] = {3} • valueb[L10] = {5} • valued[L10] = {⊤} • valuex[L10] = {100} • valuec[L10] = {4} • Worklist = {} C[s: a = b + c] = valuea [s] valueb[s] = join s1,s2 ∊prec(s)(valueb[s1], valueb[s2] )
Handling of Loop • A fixed-point computation is needed • Keep performing join at loop merge points until the value sets no longer change • If a statement s: a = … has a constant value set CS ={v}, it should be replaced with a new statement a = v;