Homework Assignment #1 Sample Solutions

Similar documents
This test is not formatted for your answers. Submit your answers via to:

Data Flow Analysis. CSCE Lecture 9-02/15/2018

Compiler Structure. Data Flow Analysis. Control-Flow Graph. Available Expressions. Data Flow Facts

Compiler Design. Fall Data-Flow Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Data Flow Analysis. Agenda CS738: Advanced Compiler Optimizations. 3-address Code Format. Assumptions

Lecture 4 Introduction to Data Flow Analysis

Compiler Optimisation

Control Flow Analysis. Reading & Topics. Optimization Overview CS2210. Muchnick: chapter 7

Compiler Optimization and Code Generation

Lecture 5. Data Flow Analysis

Why Global Dataflow Analysis?

CSE 501 Midterm Exam: Sketch of Some Plausible Solutions Winter 1997

Data Flow Analysis. Program Analysis

More Dataflow Analysis

CS 4120 Lecture 31 Interprocedural analysis, fixed-point algorithms 9 November 2011 Lecturer: Andrew Myers

Compiler Design. Fall Control-Flow Analysis. Prof. Pedro C. Diniz

Lecture 2. Introduction to Data Flow Analysis

Lecture 6 Foundations of Data Flow Analysis

Lecture 6 Foundations of Data Flow Analysis

compile-time reasoning about the run-time ow represent facts about run-time behavior represent eect of executing each basic block

Lecture Compiler Middle-End

CSC D70: Compiler Optimization LICM: Loop Invariant Code Motion

CS153: Compilers Lecture 17: Control Flow Graph and Data Flow Analysis

Why Data Flow Models? Dependence and Data Flow Models. Learning objectives. Def-Use Pairs (1) Models from Chapter 5 emphasized control

Compiler Optimisation

Dependence and Data Flow Models. (c) 2007 Mauro Pezzè & Michal Young Ch 6, slide 1

Flow Analysis. Data-flow analysis, Control-flow analysis, Abstract interpretation, AAM

Lecture 4. More on Data Flow: Constant Propagation, Speed, Loops

Calvin Lin The University of Texas at Austin

Compiler Optimizations. Chapter 8, Section 8.5 Chapter 9, Section 9.1.7

Data Flow Analysis. Suman Jana. Adopted From U Penn CIS 570: Modern Programming Language Implementa=on (Autumn 2006)

Dataflow Analysis. Xiaokang Qiu Purdue University. October 17, 2018 ECE 468

Program Optimizations using Data-Flow Analysis

Example of Global Data-Flow Analysis

Tour of common optimizations

CS 406/534 Compiler Construction Putting It All Together

Compiler Optimizations. Chapter 8, Section 8.5 Chapter 9, Section 9.1.7

Program Static Analysis. Overview

A main goal is to achieve a better performance. Code Optimization. Chapter 9

Introduction to Machine-Independent Optimizations - 4

Intermediate Representations. Reading & Topics. Intermediate Representations CS2210

CS 432 Fall Mike Lam, Professor. Data-Flow Analysis

Dataflow analysis (ctd.)

CS577 Modern Language Processors. Spring 2018 Lecture Optimization

An Overview of GCC Architecture (source: wikipedia) Control-Flow Analysis and Loop Detection

CS553 Lecture Generalizing Data-flow Analysis 3

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Plan for Today. Concepts. Next Time. Some slides are from Calvin Lin s grad compiler slides. CS553 Lecture 2 Optimizations and LLVM 1

Code optimization. Have we achieved optimal code? Impossible to answer! We make improvements to the code. Aim: faster code and/or less space

Data-Flow Analysis Foundations

Symbolic Solver for Live Variable Analysis of High Level Design Languages

AUSPICE: Automatic Safety Property Verification for Unmodified Executables

4/24/18. Overview. Program Static Analysis. Has anyone done static analysis? What is static analysis? Why static analysis?

Goals of Program Optimization (1 of 2)

CS202 Compiler Construction

Intermediate Representation (IR)

Combining Analyses, Combining Optimizations - Summary

Data Flow Information. already computed

We can express this in dataflow equations using gen and kill sets, where the sets are now sets of expressions.

We can express this in dataflow equations using gen and kill sets, where the sets are now sets of expressions.

Lecture 8: Induction Variable Optimizations

0.1 Welcome. 0.2 Insertion sort. Jessica Su (some portions copied from CLRS)

Grammars. CS434 Lecture 15 Spring 2005 Department of Computer Science University of Alabama Joel Jones

Foundations of Dataflow Analysis

Department of Computer Sciences CS502. Purdue University is an Equal Opportunity/Equal Access institution.

Lecture Notes: Dataflow Analysis Examples

Compilers. Optimization. Yannis Smaragdakis, U. Athens

Data Flow Analysis and Computation of SSA

CMSC430 Spring 2009 Midterm 2 (Solutions)

Problem Score Max Score 1 Syntax directed translation & type

Dependence and Data Flow Models. (c) 2007 Mauro Pezzè & Michal Young Ch 6, slide 1

CS243 Homework 1. Winter Due: January 23, 2019 at 4:30 pm

Control-Flow Graphs & Dataflow Analysis. CS4410: Spring 2013

ABSTRACTION ISN T THE ENTIRE STORY

Program analysis for determining opportunities for optimization: 2. analysis: dataow Organization 1. What kind of optimizations are useful? lattice al

Review. Pat Morin COMP 3002

Lecture 2. 1 Introduction. 2 The Set Cover Problem. COMPSCI 632: Approximation Algorithms August 30, 2017

Lecture 9: Loop Invariant Computation and Code Motion

An Explicit-Continuation Metacircular Evaluator

Compiler Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

CSE P 501 Compilers. Intermediate Representations Hal Perkins Spring UW CSE P 501 Spring 2018 G-1

CIS 341 Final Examination 4 May 2017

Advanced Compiler Construction

A Bad Name. CS 2210: Optimization. Register Allocation. Optimization. Reaching Definitions. Dataflow Analyses 4/10/2013

Review; questions Basic Analyses (2) Assign (see Schedule for links)

Intermediate representation

Deferred Data-Flow Analysis : Algorithms, Proofs and Applications

EXCEL BASICS: MICROSOFT OFFICE 2010

Lecture 6. Abstract Interpretation

Code generation and local optimization

Iterative Program Analysis Abstract Interpretation

Final Examination. Winter Problem Points Score. Total 180

Code generation and local optimization

EXCEL BASICS: MICROSOFT OFFICE 2007

CSE 501: Compiler Construction. Course outline. Goals for language implementation. Why study compilers? Models of compilation

Efficient JIT to 32-bit Arches

Thursday, December 23, The attack model: Static Program Analysis

Introduction to Optimization. CS434 Compiler Construction Joel Jones Department of Computer Science University of Alabama

Compiler Passes. Optimization. The Role of the Optimizer. Optimizations. The Optimizer (or Middle End) Traditional Three-pass Compiler

CSE 401/M501 Compilers

Transcription:

Homework Assignment # Sample Solutions Due Monday /, at the start of lecture. For the following control flow graph, for the purposes of a forward data flow analysis (such as available expressions), show the dominator tree, identify any loops, and for each loop, identify the loop head node and the loop entry, exit, and back edges. Is this graph reducible? 7 8 9 0 Dominator tree: 8 0 7 9 Loop : nodes:,,,, 7, 9; head node: ; back edge: ; loop entry edge: ; loop exit edges: 7 0, 9 8 Loop : nodes:, ; head node:, back edge: ; loop entry edge: ; loop exit edge: 9 The graph is reducible.. Repeat problem for purposes of a backwards data flow analysis (such as live variables).

Reversed control flow graph: 9 0 7 8 Dominator tree: 0 9 7 8 Loop: nodes:, ; head node:, back edge: ; loop entry edge: 9 ; loop exit edge: (There s also a larger strongly connected component, but it has two loop head nodes, so it s not a true natural loop: nodes:,,,, 7, 9; head nodes: 7, 0; back edges (i.e., in-loop edges to a head): 9, 9 7; loop entry edges: 0 7, 8 9; loop exit edge: ) The graph is not reducible.. Build the DAG representation of the following basic block (where variables whose names start with t are assumed not to be referenced outside of the basic block). You should perform common subexpression elimination as part of this process, exploiting commutativity of operators. You should mark which nodes in the DAG are stored in which user variables at the end of the block. t := x * x := x + y := x

t := x * y := * y t,y: * x,y: + t: * x (Final assignments to source variables in bold.). Why can we not simply apply this same DAG-construction algorithm over the whole procedure body, across basic blocks? Because we d lose track of the control dependence conditions under which different computations are executed. E.g. operations may appear redundant (i.e., common subexpressions), but be executed in different control conditions (e.g. inside of different conditionals), and so we can t just combine them into a single operation without some sort of extra work. Also, the BB algorithm didn t address constructing cyclic graphs, as would be needed to represent loops, although it could be extended to handle loops without too much trouble. Stay tuned for the VDG representation, which can be thought of as one way to extend the DAG model to the whole procedure.. Consider the case where a forward analysis s flow functions for each kind of statement are represented in terms of constant KILL and GEN bit-vectors. Thus, each flow function has the form out = in - KILL + GEN, with KILL and GEN being constants independent of in or out. The combined effect of a whole basic block of instructions can be calculated by applying each instruction s flow function in turn, starting with the in bit-vector at the start of the basic block, and computing the out vector at the end of the basic block. I.e., for instruction i of the basic block, out i = in i - KILL i + GEN i, and for all instructions other than the first, in i = out i-. However, in this special case where KILL and GEN do not depend on in, it is possible to compute a single summary flow function with constant KILL and GEN bit-vectors, of the form out n = in - SUMMARY-KILL + SUMMARY-GEN (where in and out n are the points before and after the basic block, respectively). Give a pair of simple inductive equations for computing the SUMMARY-KILL i and SUMMARY-GEN i bit-vectors, representing the summary KILL and GEN bit-vectors for the first i instructions of a basic block, in terms of the SUMMARY-KILL i- and SUMMARY- GEN i- bit-vectors (if i>) and the KILL i and GEN i bit-vectors for the flow function of the ith instruction, i n. SUMMARY-KILL and SUMMARY-GEN for the whole basic block are then SUMMARY-KILL n and SUMMARY-GEN n, respectively. SUMMARY-KILL i = SUMMARY-KILL i- KILL i SUMMARY-GEN i = SUMMARY-GEN i- - KILL i GEN i

where SUMMARY-KILL 0 and SUMMARY-GEN 0 are (Note that SUMMARY-KILL i = SUMMARY-KILL i- - GEN i- KILL i looks more correct than the above equation, but they have the same end effect.) What is the chief advantage of this SUMMARY flow function over the original flow functions? It allows the effect of a whole basic block to be computed with a single flow function (that runs a lot faster than the sequence of per-instruction flow functions). This is useful during iterative analysis where we can recompute the effect of a basic block several times.. Define a data flow analysis that can be used to compute two sets of variables for each procedure: those which are definitely not defined before use, and those which may not be defined before use; these sets can be used to provide extra error checking of programs. Strive for the highest quality information while maintaining safety. Use a lattice-theoretic formalism to define your analysis. Argue that your analysis terminates. Explain how to use the information computed by your analysis to construct the two sets specified above for output to the user. It is possible to construct either a forward or a reverse dataflow problem to answer this question. We sketch out both possibilities; either one is a sufficient answer. We ll assume that any formal parameters are initialized via explicit assignments in the flow graph. The first approach is to perform a forward dataflow analysis computing for each program point a set of definitely defined variables and a set of possibly defined variables. After these sets are computed, we can then examine all of the uses in the program and report any errors. A variable is definitely not defined before it is used when the variable does not appear in the possibly defined variable set associated with the program point immediately preceding a statement in which the variable is used. A variable may not be defined before it is used if at the program point proceeding a statement in which it is used it is not an element of the definitely defined variable set. Note that a read through a pointer must be considered to be a use of every variable, due to the lack of more detailed pointer analysis. To simplify the formal definition we ll define two lattices, and then use the -Tuple constructor to compose them into a single lattice. Definitely Defined Variables: Elements: : meet: top: Pow(Variables), where Variables is the set of all variables in the CFG bottom: Flow Functions: Variables F x := Info succ = Info pred {x}

F *x := Info succ = Info pred Possibly Defined Variables: Elements: : meet: top: Pow(Variables) bottom: Variables Flow Functions: F x := F *x := Info succ = Info pred {x} Info succ = Info pred Variables = Variables The flow functions are monotonic and the lattices are finite height, so the analysis will terminate. (Each individual lattice has height O(N); when we compose them into a -Tuple we still have a O(N) height lattice. Each flow function can run in O(N) time (or less). So, we get an O(N ) analysis to compute the sets. The post-pass to examine the sets and produce warnings can run in O(N ) time (O(N) statements, O(N) work for each (O(N) for reads through a pointer).) Alternatively, one could define a reverse dataflow analysis that computes the set of possibly live and definitely live variables at each program point. All variables that are definitely live at the flow graph entry point are definitely used before they are defined; all variables that are possibly live at the flow graph entry point may be used before they are defined. Just as before, we ll define two lattices and then combine them using the -Tuple operator: Definitely Live Variables: Elements: : meet: top: Pow(Variables) bottom: Variables Example Flow Functions: F x := y + z F *x := y + z F x := *y + *z F *x := *y + *z Possibly Live Variables: Info succ = Info pred - {x} {y,z} Info succ = Info pred - Variables {y,z} = {y,z} Info succ = Info pred - {x} = Info pred - {x} Info succ = Info pred - Variables =

Elements: Pow(Variables) : meet: top: bottom: Variables Example Flow Functions: F x := y + z F *x := y + z F x := *y + *z F *x := *y + *z Info succ = Info pred - {x} {y,z} Info succ = Info pred - {y,z} = Info pred {y,z} Info succ = Info pred - {x} Variables = Variables Info succ = Info pred - Variables = Variables Termination and complexity are identical to the forward analysis.