1 CS 201 Compiler Construction Code Optimizations: Partial Dead Code Elimination.

Slides:



Advertisements
Similar presentations
Example of Constructing the DAG (1)t 1 := 4 * iStep (1):create node 4 and i 0 Step (2):create node Step (3):attach identifier t 1 (2)t 2 := a[t 1 ]Step.
Advertisements

Overview Structural Testing Introduction – General Concepts
Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
Lecture 11: Code Optimization CS 540 George Mason University.
Chapter 9 Code optimization Section 0 overview 1.Position of code optimizer 2.Purpose of code optimizer to get better efficiency –Run faster –Take less.
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
Static Single Assignment CS 540. Spring Efficient Representations for Reachability Efficiency is measured in terms of the size of the representation.
Data-Flow Analysis Framework Domain – What kind of solution is the analysis looking for? Ex. Variables have not yet been defined – Algorithm assigns a.
1 Introduction to Data Flow Analysis. 2 Data Flow Analysis Construct representations for the structure of flow-of-data of programs based on the structure.
1 CS 201 Compiler Construction Lecture 7 Code Optimizations: Partial Redundancy Elimination.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Partial Redundancy Elimination Guo, Yao.
05 May Lazy Code Motion in an SSA World A CS 526 Course Project Patrick Meredith Steven Lauterburg 05 May 2006.
Λλ Fernando Magno Quintão Pereira P ROGRAMMING L ANGUAGES L ABORATORY Universidade Federal de Minas Gerais - Department of Computer Science P ROGRAM A.
1 Data flow analysis Goal : collect information about how a procedure manipulates its data This information is used in various optimizations For example,
1 CS 201 Compiler Construction Lecture 12 Global Register Allocation.
1 CS 201 Compiler Construction Lecture 5 Code Optimizations: Copy Propagation & Elimination.
1 Data flow analysis Goal : –collect information about how a procedure manipulates its data This information is used in various optimizations –For example,
CS 536 Spring Global Optimizations Lecture 23.
1 CS 201 Compiler Construction Lecture 13 Instruction Scheduling: Trace Scheduler.
4/25/08Prof. Hilfinger CS164 Lecture 371 Global Optimization Lecture 37 (From notes by R. Bodik & G. Necula)
1 CS 201 Compiler Construction Lecture 8 Code Optimizations: Partial Dead Code Elimination.
CMPUT Compiler Design and Optimization1 CMPUT680 - Winter 2006 Topic H: SSA for Predicated Code José Nelson Amaral
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
CS 412/413 Spring 2007Introduction to Compilers1 Lecture 29: Control Flow Analysis 9 Apr 07 CS412/413 Introduction to Compilers Tim Teitelbaum.
1 CS 201 Compiler Construction Lecture 6 Code Optimizations: Constant Propagation & Folding.
Prof. Fateman CS 164 Lecture 221 Global Optimization Lecture 22.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
Prof. Bodik CS 164 Lecture 16, Fall Global Optimization Lecture 16.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
1 CS 201 Compiler Construction Data Flow Analysis.
1 ECE 453 – CS 447 – SE 465 Software Testing & Quality Assurance Instructor Kostas Kontogiannis.
Computer Science & Engineering, Indian Institute of Technology, Bombay Code optimization by partial redundancy elimination using Eliminatability paths.
White-Box Testing Techniques II Originals prepared by Stephen M. Thebaut, Ph.D. University of Florida Dataflow Testing.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Dataflow Analysis Topic today Data flow analysis: Section 3 of Representation and Analysis Paper (Section 3) NOTE we finished through slide 30 on Friday.
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
1 Data Flow Analysis Data flow analysis is used to collect information about the flow of data values across basic blocks. Dominator analysis collected.
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
Final Code Generation and Code Optimization.
CS412/413 Introduction to Compilers Radu Rugina Lecture 18: Control Flow Graphs 29 Feb 02.
1 Control Flow Graphs. 2 Optimizations Code transformations to improve program –Mainly: improve execution time –Also: reduce program size Can be done.
1 Software Testing & Quality Assurance Lecture 13 Created by: Paulo Alencar Modified by: Frank Xu.
Iterative Structures (Loops) CS 1401 Spring 2013 Shirley Moore, Instructor February 28, 2013.
Copyright Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
CS 598 Scripting Languages Design and Implementation 9. Constant propagation and Type Inference.
Control Structures WHILE Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied.
Code Optimization Overview and Examples
Data Flow Analysis Suman Jana
EQUATION IN TWO VARIABLES:
Dataflow Testing G. Rothermel.
University Of Virginia
CS 201 Compiler Construction
1. Reaching Definitions Definition d of variable v: a statement d that assigns a value to v. Use of variable v: reference to value of v in an expression.
1) C program development 2) Selection structure
CS 201 Compiler Construction
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Topic 5a Partial Redundancy Elimination and SSA Form
Sudipto Ghosh CS 406 Fall 99 November 16, 1999
Optimizations using SSA
Interval Partitioning of a Flow Graph
Final Code Generation and Code Optimization
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
SSA-based Optimizations
Theory of Computation Lecture 4: Programs and Computable Functions II
Prof. Dhananjay M Dhamdhere
Unit III – Chapter 3 Path Testing.
CS 201 Compiler Construction
Presentation transcript:

1 CS 201 Compiler Construction Code Optimizations: Partial Dead Code Elimination

Dead Code Elimination Dead Code is code that is either never executed or, if it is executed, its result is never used by the program. Therefore dead code can be eliminated from the program. 2 dead code x = … y = y+1 x = y+1 write x dead code A = 1 B = 5 If A<B ……. write x ……. TF

Dead Code: More Examples 3 Values used only by dead statements. x = … y = x+1 read y write y+1 x = … …..…x = x+1

Partially Dead Code Value computed by partially dead statement is sometimes used and sometimes not used. 4 x is partially dead here x is live here x is dead here x =... x = x +1 write x x = y +1

Code Motion Reqd. for Elimination 5 x =... x = x +1 write x x = y +1 x = … x = x +1 write x x = … x = y +1 x = … x = x +1 write x x = y +1

Removal of Code From Loops 6 … = x.…..… x = y+1 no use of x x = y+1 … = x.…..… …….....

Critical Edges 7 X = …. …… = XX = ….. critical edge should be split ……... …… = X X = ….. X = ….

Second Order Effects 8 a=c+d cannot be moved till x=a+b is moved

Algorithm Repeat Perform dead/faint variable analysis and eliminate dead/faint code Perform delayability analysis for sinking candidates Place statements in new positions Until Program Stabilizes 9

Definitions Dead Code: S: x = t is dead, if x is dead following S, i.e. every path from S to every right hand side occurrence of x following S is preceded by a redefinition of x. Faint Code: S: x = t is faint, if x is faint following S, i.e. every path from S to every right hand side occurrence of x following S is either preceded by a redefinition of x or is in a statement whose left hand side variable is faint. 10

Local Predicates USED s (x) – x is rhs variable of statement s RELV-USED s (x) – x is a rhs variable of relevant statement s; relevant statements are those that cannot be eliminated – conditionals like if x<y and output statements like write x. ASSIGN-USED s (x) – x is a rhs variable of assignment statement s. MOD s (x) – x is lhs variable of statement s. 11

Dead Variable Analysis Bit vector analysis for all variables. 12 N-DEAD s = USED s ( X-DEAD s V MOD s ) V V X-DEAD s = N-DEAD i i ε succ(s)

Faint Variable Analysis Not bit vector analysis. Perform following analysis for each x 13 V X-FAINT s (x) = N-FAINT i (x) i ε succ(s) N-FAINT s (x) = RELV-USED s (x) ∧ ( X-FAINT s (x) ∨ MOD s (x) ) ∧ ( X-FAINT s (lhs s ) ∨ ASSIGN-USED s (x) )

Analysis of Equation 14 N-FAINT S (x) is false if any of the following conditions is true: 1.the statement s uses x and s cannot ever be marked faint, i.e. 2. the statement s does not modify x and x is not faint on exit, i.e. 3. x is used by statement s and lhs of s is not faint, i.e. REL-USED s (x) is true ( X-FAINT s (x) ∧ MOD s (x) ) is true ( X-FAINT s (lhs) ∧ ASSIGN-USED s (x) ) is true REL-USED s (x) ∨ ( X-FAINT s (x) ∧ MOD s (x) ) ∨ ( X-FAINT s (lhs) ∧ ASSIGN-USED s (x) ) N-FAINT s (x) =

Elimination Following dead/faint variable analysis we can eliminate dead/faint code  Eliminate n: x=.. assignment if x is dead/faint at exit of n. The above elimination rule eliminates fully dead/faint code. To eliminate partially dead code, we develop delayability analysis  using this analysis partially dead statement is moved to program points where it is fully dead or fully live. Iterative application of dead/faint code elimination and delaying partially dead assignments leads to optimization. 15

Delayability 16 x = a+b ……… write x x = c+d ………. …….… write x x = c+d x = a+b Delayability Analysis fully dead fully live

Delayability Analysis 17 LOCDELAYED n (α): α is a sinking candidate that ε n. LOCBLOCKED n (α): sinking of α is blocked by code in n. N-DELAYED n /X-DELAYED n indicate whether α can be delayed to the entry/exit point of n. N-DELAYED n (α) = X-DELAYED n (α) = LOCDELAYED n (α) ∨ ( N-DELAYED n (α) ∧ LOCBLOCKED n (α) ) falseif n = s otherwise ∧ X-DELAYED m (α) m ε pred(n)

Contd.. 18 Placing α at its new positions. If N-INSERT n (α)/X-INSERT n (α) is true then place α at n’s entry/exit point. Essentially we are moving α to the latest points to which it can be delayed. N-INSERT n (α) = N-DELAYED n (α) ∧ LOCBLOCKED n (α) X-INSERT n (α) = X-DELAYED n (α) ∧ ( ∨ N-DELAYED m (α) ) m ε succ(s)

Sample Problem Apply the PDE algorithm to the given code example. 19 P=P+1 B=P+1 P = 1 output B P=E+1 input E output A A=B+1