CS412/413 Introduction to Compilers Radu Rugina Lecture 18: Control Flow Graphs 29 Feb 02.

Slides:



Advertisements
Similar presentations
Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
Advertisements

School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) SSA Guo, Yao.
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.
SSA.
Static Single Assignment CS 540. Spring Efficient Representations for Reachability Efficiency is measured in terms of the size of the representation.
CS412/413 Introduction to Compilers Radu Rugina Lecture 37: DU Chains and SSA Form 29 Apr 02.
Chapter 10 Code Optimization. A main goal is to achieve a better performance Front End Code Gen Intermediate Code source Code target Code user Machine-
Control Flow Analysis. Construct representations for the structure of flow-of-control of programs Control flow graphs represent the structure of flow-of-control.
Components of representation Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements occur in right order Data.
Program Representations. Representing programs Goals.
1 CS 201 Compiler Construction Lecture 7 Code Optimizations: Partial Redundancy Elimination.
CS412/413 Introduction to Compilers Radu Rugina Lecture 16: Efficient Translation to Low IR 25 Feb 02.
6/9/2015© Hal Perkins & UW CSEU-1 CSE P 501 – Compilers SSA Hal Perkins Winter 2008.
1 CS 201 Compiler Construction Lecture 5 Code Optimizations: Copy Propagation & Elimination.
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
More Dataflow Analysis CS153: Compilers Greg Morrisett.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
PSUCS322 HM 1 Languages and Compiler Design II Basic Blocks Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
Code Generation Professor Yihjia Tsai Tamkang University.
1 Intermediate representation Goals: encode knowledge about the program facilitate analysis facilitate retargeting facilitate optimization scanning parsing.
1 CS 201 Compiler Construction Lecture 1 Introduction.
Another example p := &x; *p := 5 y := x + 1;. Another example p := &x; *p := 5 y := x + 1; x := 5; *p := 3 y := x + 1; ???
2015/6/24\course\cpeg421-10F\Topic1-b.ppt1 Topic 1b: Flow Analysis Some slides come from Prof. J. N. Amaral
Data Flow Analysis Compiler Design October 5, 2004 These slides live on the Web. I obtained them from Jeff Foster and he said that he obtained.
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.
1 Copy Propagation What does it mean? – Given an assignment x = y, replace later uses of x with uses of y, provided there are no intervening assignments.
Intermediate Code. Local Optimizations
Improving Code Generation Honors Compilers April 16 th 2002.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
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.
Ben Livshits Based in part of Stanford class slides from
Precision Going back to constant prop, in what cases would we lose precision?
1 ECE 453 – CS 447 – SE 465 Software Testing & Quality Assurance Instructor Kostas Kontogiannis.
CS412/413 Introduction to Compilers Radu Rugina Lecture 15: Translating High IR to Low IR 22 Feb 02.
Software (Program) Analysis. Automated Static Analysis Static analyzers are software tools for source text processing They parse the program text and.
Optimization software for apeNEXT Max Lukyanov,  apeNEXT : a VLIW architecture  Optimization basics  Software optimizer for apeNEXT  Current.
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
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 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room Tel: (951) Office.
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
Cleaning up the CFG Eliminating useless nodes & edges C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon,
Compiler Optimizations ECE 454 Computer Systems Programming Topics: The Role of the Compiler Common Compiler (Automatic) Code Optimizations Cristiana Amza.
1 Control Flow Analysis Topic today Representation and Analysis Paper (Sections 1, 2) For next class: Read Representation and Analysis Paper (Section 3)
Dead Code Elimination This lecture presents the algorithm Dead from EaC2e, Chapter 10. That algorithm derives, in turn, from Rob Shillner’s unpublished.
CS 614: Theory and Construction of Compilers Lecture 15 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 Control Flow Graphs. 2 Optimizations Code transformations to improve program –Mainly: improve execution time –Also: reduce program size Can be done.
Control Flow Analysis Compiler Baojian Hua
Cleaning up the CFG Eliminating useless nodes & edges This lecture describes the algorithm Clean, presented in Chapter 10 of EaC2e. The algorithm is due.
Homework 4 Basic Blocks and Control Flow Graphs CS4430 Spring 2012 Due: April 2 nd by 2pm* * There will be absolutely no extensions for this assignment.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Compiler Principles Fall Compiler Principles Lecture 8: Dataflow & Optimizations 1 Roman Manevich Ben-Gurion University of the Negev.
Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.
Simone Campanoni CFA Simone Campanoni
High-level optimization Jakub Yaghob
Code Optimization.
Control Flow Analysis CS 4501 Baishakhi Ray.
CS 201 Compiler Construction
Static Single Assignment Form (SSA)
Optimizations using SSA
Data Flow Analysis Compiler Design
Final Code Generation and Code Optimization
Live variables and copy propagation
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
CS 201 Compiler Construction
Presentation transcript:

CS412/413 Introduction to Compilers Radu Rugina Lecture 18: Control Flow Graphs 29 Feb 02

CS 412/413 Spring 2002 Introduction to Compilers2 Optimizations Code transformations to improve program –Mainly: improve execution time –Also: reduce program size Can be done at high level or low level –E.g. constant folding Optimizations must be safe –Execution of transformed code must yield same results as the original code for all possible executions

CS 412/413 Spring 2002 Introduction to Compilers3 Optimization Safety Safety of code transformations usually requires certain information which may not explicit in the code Example: dead code elimination (1) x = y + 1; (2) y = 2 * z; (3) x = y + z; (4) z = x; (5) z = 1; What statements are dead and can be removed?

CS 412/413 Spring 2002 Introduction to Compilers4 Optimization Safety Safety of code transformations usually requires certain information which may not explicit in the code Example: dead code elimination (1) x = y + 1; (2) y = 2 * z; (3) x = y + z; (4) z = x; (5) z = 1; Need to know what values assigned to x at (1) is never used later (i.e. x is dead at statement (1)) –Obvious for this simple example (with no control flow) –Not obvious for complex flow of control

CS 412/413 Spring 2002 Introduction to Compilers5 Dead Code Example Add control flow to example: x = y + 1; y = 2 * z; if (d) x = y+z; z = 1; z = x; Is ‘x = y+1’ dead code? Is ‘z = x’ dead code?

CS 412/413 Spring 2002 Introduction to Compilers6 Dead Code Example Add control flow to example: x = y + 1; y = 2 * z; if (d) x = y+z; z = 1; z = x; Statement x = y+1 is not dead code! On some executions, value is used later

CS 412/413 Spring 2002 Introduction to Compilers7 Dead Code Example Add more control flow: while (c) { x = y + 1; y = 2 * z; if (d) x = y+z; z = 1; } z = x; Is ‘x = y+1’ dead code? Is ‘z = x’ dead code?

CS 412/413 Spring 2002 Introduction to Compilers8 Dead Code Example Add more control flow: while (c) { x = y + 1; y = 2 * z; if (d) x = y+z; z = 1; } z = x; Statement ‘x = y+1’ not dead (as before) Statement ‘z = 1’ not dead either! On some executions, value from ‘z=1’ is used later

CS 412/413 Spring 2002 Introduction to Compilers9 Low-level Code Much harder to eliminate dead code in low-level code: label L1 fjump c L2 x = y + 1; y = 2 * z; fjump d L3 x = y+z; label L3 z = 1; jump L1 label L2 z = x; Are these statements dead?

CS 412/413 Spring 2002 Introduction to Compilers10 Low-level Code Much harder to eliminate dead code in low-level code: label L1 fjump c L2 x = y + 1; y = 2 * z; fjump d L3 x = y+z; label L3 z = 1; jump L1 label L2 z = x; It is harder to analyze flow of control in low level code

CS 412/413 Spring 2002 Introduction to Compilers11 Optimizations and Control Flow Application of optimizations requires information –Dead code elimination: need to know if variables are dead when assigned values Required information: –Not explicit in the program –Must compute it statically (at compile-time) –Must characterize all dynamic (run-time) executions Control flow makes it hard to extract information –Branches and loops in the program –Different executions = different branches taken, different number of loop iterations executed

CS 412/413 Spring 2002 Introduction to Compilers12 Control Flow Graphs Control Flow Graph (CFG) = graph representation of computation and control flow in the program –framework to statically analyze program control-flow Nodes are basic blocks = sequences of consecutive non-branching statements Edges represent possible flow of control from the end of one block to the beginning of the other –There may be multiple incoming/outgoing edges for each block

CS 412/413 Spring 2002 Introduction to Compilers13 CFG Example x = z - 2 ; y = 2 *z; if (c) { x = x+1; y = y+1; } else { x = x-1; y = y-1; } z = x+y; Control Flow Graph Program x = z-2; y = 2*z; if (c) x = x+1; y = y+1; x = x-1; y = y-1; z = x+y; T F B1B1 B2B2 B4B4 B3B3

CS 412/413 Spring 2002 Introduction to Compilers14 Basic Blocks Basic block = sequence of consecutive statements such that: –Control enters only at beginning of sequence –Control leaves only at end of sequence No branching in or out in the middle of basic blocks a = a+1; b = c*a; d = c-b; incoming control outgoing control

CS 412/413 Spring 2002 Introduction to Compilers15 Computation and Control Flow Basic Blocks = Nodes in the graph = computation in the program Edges in the graph = control flow in the program Control Flow Graph x = z-2; y = 2*z; if (c) x = x+1; y = y+1; x = x-1; y = y-1; z = x+y; T F B1B1 B2B2 B4B4 B3B3

CS 412/413 Spring 2002 Introduction to Compilers16 Multiple Program Executions CFG models all program executions Possible execution= path in the graph Multiple paths = multiple possible program executions Control Flow Graph x = z-2; y = 2*z; if (c) x = x+1; y = y+1; x = x-1; y = y-1; z = x+y; T F B1B1 B2B2 B4B4 B3B3

CS 412/413 Spring 2002 Introduction to Compilers17 Execution 1 CFG models all program executions Possible execution= path in the graph Execution 1: –C is true –Program executes basic blocks B 1, B 2, B 4 Control Flow Graph x = z-2; y = 2*z; if (c) x = x+1; y = y+1; z = x+y; T B1B1 B2B2 B4B4

CS 412/413 Spring 2002 Introduction to Compilers18 Execution 2 CFG models all program executions Possible execution= path in the graph Execution 2: –C is false –Program executes basic blocks B 1, B 3, B 4 Control Flow Graph x = z-2; y = 2*z; if (c) x = x-1; y = y-1; z = x+y; F B1B1 B4B4 B3B3

CS 412/413 Spring 2002 Introduction to Compilers19 Edges Going Out Multiple outgoing edges Basic block executed next may be one of the successor basic blocks Each outgoing edge = outgoing flow of control in some execution of the program Basic Block outgoing edges

CS 412/413 Spring 2002 Introduction to Compilers20 Edges Coming In Multiple incoming edges Control may come from any of the successor basic blocks Each incoming edge = incoming flow of control in some execution of the program Basic Block incoming edges

CS 412/413 Spring 2002 Introduction to Compilers21 Building the CFG Currently the compiler represents the program using either High IR or low IR Can construct CFG for either of the two intermediate representations Build CFG for High IR –Construct CFG for each High IR node Build CFG for Low IR –Analyze jump and label statements

CS 412/413 Spring 2002 Introduction to Compilers22 CFG for High-level IR CFG(S)= flow graph of high level statement S CFG (S) is single-entry, single-exit graph: – one entry node (basic block) – one exit node (basic block) Recursively define CFG(S) CFG(S) Entry Exit … =

CS 412/413 Spring 2002 Introduction to Compilers23 CFG for Block Statement CFG( S1; S2; …; SN ) = CFG(S2) CFG(S1) CFG(SN) …

CS 412/413 Spring 2002 Introduction to Compilers24 CFG for If-then-else Statement CFG ( if (E) S1 else S2 ) if (E) CFG(S2) T F CFG(S1) Empty basic block

CS 412/413 Spring 2002 Introduction to Compilers25 CFG for If-then Statement CFG( if (E) S ) if (E) T F CFG(S1)

CS 412/413 Spring 2002 Introduction to Compilers26 CFG for While Statement CFG for: while (e) S if (e) T F CFG(S)

CS 412/413 Spring 2002 Introduction to Compilers27 Recursive CFG Construction Nested statements: recursively construct CFG while traversing IR nodes Example: while (c) { x = y + 1; y = 2 * z; if (d) x = y+z; z = 1; } z = x;

CS 412/413 Spring 2002 Introduction to Compilers28 Recursive CFG Construction Nested statements: recursively construct CFG while traversing IR nodes CFG(z=x) CFG(while) while (c) { x = y + 1; y = 2 * z; if (d) x = y+z; z = 1; } z = x;

CS 412/413 Spring 2002 Introduction to Compilers29 Recursive CFG Construction Nested statements: recursively construct CFG while traversing IR nodes z=x if (c) T F CFG(body) while (c) { x = y + 1; y = 2 * z; if (d) x = y+z; z = 1; } z = x;

CS 412/413 Spring 2002 Introduction to Compilers30 Recursive CFG Construction Nested statements: recursively construct CFG while traversing IR nodes z = x if (c) F y = 2*z CFG(if) z = 1 x = y+1 while (c) { x = y + 1; y = 2 * z; if (d) x = y+z; z = 1; } z = x;

CS 412/413 Spring 2002 Introduction to Compilers31 Recursive CFG Construction Simple algorithm to build CFG Generated CFG –Each basic block has a single statement –There are empty basic blocks Small basic blocks = inefficient –Small blocks = many nodes in CFG –Compiler uses CFG to perform optimization –Many nodes in CFG = compiler optimizations will be time- and space-consuming

CS 412/413 Spring 2002 Introduction to Compilers32 Efficient CFG Construction Basic blocks in CFG: –As few as possible –As large as possible There should be no pair of basic blocks (B1,B2) such that: –B2 is a successor of B1 –B1 has one outgoing edge –B2 has one incoming edge There should be no empty basic blocks

CS 412/413 Spring 2002 Introduction to Compilers33 x = y+1 y =2*z if (d) Example Efficient CFG: z = x if (c) x = y+z z = 1 while (c) { x = y + 1; y = 2 * z; if (d) x = y+z; z = 1; } z = x;

CS 412/413 Spring 2002 Introduction to Compilers34 CFG for Low-level IR Identify basic blocks as sequences of: –Non-branching instructions –Non-label instructions No branches (jump) instructions = control doesn’t flow out of basic blocks No labels instructions = control doesn’t flow into blocks label L1 fjump c L2 x = y + 1; y = 2 * z; fjump d L3 x = y+z; label L3 z = 1; jump L1 label L2 z = x;

CS 412/413 Spring 2002 Introduction to Compilers35 CFG for Low-level IR Basic block start: –At label instructions –After jump instructions Basic blocks end: –At jump instructions –Before label instructions label L1 fjump c L2 x = y + 1; y = 2 * z; fjump d L3 x = y+z; label L3 z = 1; jump L1 label L2 z = x;

CS 412/413 Spring 2002 Introduction to Compilers36 CFG for Low-level IR Conditional jump: 2 successors Unconditional jump: 1 successor label L1 fjump c L2 x = y + 1; y = 2 * z; fjump d L3 x = y+z; label L3 z = 1; jump L1 label L2 z = x;

CS 412/413 Spring 2002 Introduction to Compilers37 CFG for Low-level IR x = y+1 y =2*z if (d) z = x if (c) x = y+z z = 1 label L1 fjump c L2 x = y + 1; y = 2 * z; fjump d L3 x = y+z; label L3 z = 1; jump L1 label L2 z = x;