UNITIV Code Optimization Topics Overview Principal Sources of





















- Slides: 21

UNIT-IV Code Optimization Topics • • • Overview Principal Sources of Optimization Basic Blocks Flow Graph Global Data Flow Analysis References Compiler Design Dept. of CSE, VFSTR University 1

Optimization • Transform the program to improve efficiency • Performance: faster execution • Size: smaller executable, smaller memory footprint Compiler Design Dept. of CSE, VFSTR University 2

Optimization • • • Principal Sources of Optimization Constant Folding / Propagation Copy Propagation Algebraic Simplifications Strength Reduction Dead Code Elimination – Structure Simplifications • Loop Optimizations • Partial Redundancy Elimination • Code Inlining Compiler Design Dept. of CSE, VFSTR University 3

Optimization Constant Folding • Evaluate constant expressions at compile time • Only possible when side-effect freeness guaranteed c: = 1 + 3 c: = 4 true not false Caveat: Floats — implementation could be different between machines! Compiler Design Dept. of CSE, VFSTR University 4

Optimization Constant Propagation • Variables that have constant value, e. g. c : = 3 – Later uses of c can be replaced by the constant – If no change of c between! b : = 3 c : = 1 + b d : = b + c b : = 3 c : = 1 + 3 d : = 3 + c Analysis needed, as b can be assigned more than once! Compiler Design Dept. of CSE, VFSTR University 5

Optimization Copy Propagation • for a statement x : = y • replace later uses of x with y, if x and y have not been changed. x : = y c : = 1 + x d : = x + c x : = y c : = 1 + y d : = y + c Analysis needed, as y and x can be assigned more than once! Compiler Design Dept. of CSE, VFSTR University 6

Optimization Algebraic Simplifications • Use algebraic properties to simplify expressions -(-i) i b or: true Important to simplify code for later optimizations Compiler Design Dept. of CSE, VFSTR University 7

Optimization Strength Reduction • Replace expensive operations with simpler ones • Example: Multiplications replaced by additions y : = x * 2 y : = x + x Peephole optimizations are often strength reductions Compiler Design Dept. of CSE, VFSTR University 8

Optimization Dead Code • Remove unnecessary code – e. g. variables assigned but never read b : = 3 c : = 1 + 3 d : = 3 + c > c : = 1 + 3 d : = 3 + c Remove code never reached if (false) {a : = 5} Compiler Design if (false) {} Dept. of CSE, VFSTR University 9

Optimization Simplify Structure • Similar to dead code: Simplify CFG Structure • Optimizations will degenerate CFG • Needs to be cleaned to simplify further optimization! Compiler Design Dept. of CSE, VFSTR University 10

Basic blocks and Flow graphs • Partition the intermediate code into basic blocks – The flow of control can only enter the basic block through the first instruction in the block. That is, there are no jumps into the middle of the block. – Control will leave the block without halting or branching, except possibly at the last instruction in the block. • The basic blocks become the nodes of a flow graph Compiler Design Dept. of CSE, VFSTR University 11

Rules for finding leaders • The first three-address instruction in the intermediate code is a leader. • Any instruction that is the target of a conditional or unconditional jump is a leader. • Any instruction that immediately follows a conditional or unconditional jump is a leader. Compiler Design Dept. of CSE, VFSTR University 12

Intermediate code to set a 10*10 matrix to an identity matrix Compiler Design Dept. of CSE, VFSTR University 13

Flow graph based on Basic Blocks Compiler Design Dept. of CSE, VFSTR University 14

Optimization Common Subexpression Elimination (CSE) Common Subexpression: - There is another occurrence of the expression whose evaluation always precedes this one - operands remain unchanged Local (inside one basic block): When building IR Global (complete flow-graph) Compiler Design Dept. of CSE, VFSTR University 15

Optimization Example CSE t 1 : = a + 2 b : = t 1 c : = 4 * b b < c? b : = a + 2 c : = 4 * b b < c? b : = 1 d : = a + 2 Compiler Design d : = t 1 Dept. of CSE, VFSTR University 16

Optimization Loop Optimizations • Optimizing code in loops is important – often executed, large payoff • All optimizations help when applied to loopbodies • Some optimizations are loop specific Compiler Design Dept. of CSE, VFSTR University 17

Optimization Loop Invariant Code Motion • Move expressions that are constant over all iterations out of the loop Compiler Design Dept. of CSE, VFSTR University 18

Optimization Induction Variable Optimizations • Values of variables form an arithmetic progression integer a(100) t 1 : = 202 do i = 1, 100 t 1 : = t 1 - 2 a(i) = t 1 endo integer a(100) do i = 1, 100 a(i) = 202 - 2 * i endo value assigned to a decreases by 2 Compiler Design uses Strength Reduction Dept. of CSE, VFSTR University 19

Summary • • Principal Sources of Optimization Basic Blocks Flow Graph Global Data Flow Analysis Compiler Design Dept. of CSE, VFSTR University 20

Acknowledgement • http: //www. cs. fsu. edu/~engelen/courses/COP 5621/ • www. facweb. iitkgp. ernet. in/~niloy/COURSE/. . . /Compiler/ppt/ intro 04 CS 3019. ppt Compiler Design Dept. of CSE, VFSTR University 21