Optimizations. Optimization Safety. Optimization Safety. Control Flow Graphs. Code transformations to improve program



Similar documents
CSE 504: Compiler Design. Data Flow Analysis

2) Write in detail the issues in the design of code generator.

Lecture 2 Introduction to Data Flow Analysis

Why have SSA? Birth Points (cont) Birth Points (a notion due to Tarjan)

Introduction to Data-flow analysis

Class One: Degree Sequences

Flowcharting, pseudocoding, and process design

Software Testing. Jeffrey Carver University of Alabama. April 7, 2016

GENERIC and GIMPLE: A New Tree Representation for Entire Functions

Using Eclipse CDT/PTP for Static Analysis

Review; questions Discussion of Semester Project Arbitrary interprocedural control flow Assign (see Schedule for links)

Advanced compiler construction. General course information. Teacher & assistant. Course goals. Evaluation. Grading scheme. Michel Schinz

Measuring the Performance of an Agent

Change Impact Analysis

Operations: search;; min;; max;; predecessor;; successor. Time O(h) with h height of the tree (more on later).

Reducing Clocks in Timed Automata while Preserving Bisimulation

Static Analysis. Find the Bug! : Analysis of Software Artifacts. Jonathan Aldrich. disable interrupts. ERROR: returning with interrupts disabled

Different Approaches to White Box Testing Technique for Finding Errors

Intrusion Detection via Static Analysis

6.045: Automata, Computability, and Complexity Or, Great Ideas in Theoretical Computer Science Spring, Class 4 Nancy Lynch

A Survey of Static Program Analysis Techniques

Automatic Test Data Synthesis using UML Sequence Diagrams

GameTime: A Toolkit for Timing Analysis of Software

Vector storage and access; algorithms in GIS. This is lecture 6

IE 680 Special Topics in Production Systems: Networks, Routing and Logistics*

Administration. Instruction scheduling. Modern processors. Examples. Simplified architecture model. CS 412 Introduction to Compilers

DATA STRUCTURES USING C

GSPIM: Graphical Visualization Tool for MIPS Assembly

How To Trace

Test case design techniques I: Whitebox testing CISS

Pragmatic guidelines for Business Process Modeling

SEQUENCES ARITHMETIC SEQUENCES. Examples

Symbol Tables. Introduction

Data Structures and Algorithms Written Examination

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

Online Construction of Dynamic Object Process Graphs

Glossary of Object Oriented Terms

Departamento de Investigación. LaST: Language Study Tool. Nº 143 Edgard Lindner y Enrique Molinari Coordinación: Graciela Matich

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Obfuscation: know your enemy

Data Structure with C

Y. Xiang, Constraint Satisfaction Problems

Using UML Part Two Behavioral Modeling Diagrams

Flowchart Techniques

A Business Process Driven Approach for Generating Software Modules

Standard for Software Component Testing

FACTORING ax 2 bx c. Factoring Trinomials with Leading Coefficient 1

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Class notes Program Analysis course given by Prof. Mooly Sagiv Computer Science Department, Tel Aviv University second lecture 8/3/2007

Test Case Design Techniques

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

CODE FACTORING IN GCC ON DIFFERENT INTERMEDIATE LANGUAGES

In this example the length of the vector is determined by D length and used for the index variable.

Stack Allocation. Run-Time Data Structures. Static Structures

Dynamic Programming. Lecture Overview Introduction

VisCG: Creating an Eclipse Call Graph Visualization Plug-in. Kenta Hasui, Undergraduate Student at Vassar College Class of 2015

AN IMPLEMENTATION OF SWING MODULO SCHEDULING WITH EXTENSIONS FOR SUPERBLOCKS TANYA M. LATTNER

Problem Set 7 Solutions

Process Modeling Notations and Workflow Patterns

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

Module-I Lecture-I Introduction to Digital VLSI Design Flow

EECS 583 Class 11 Instruction Scheduling Software Pipelining Intro

6. Control Structures

Efficient Data Structures for Decision Diagrams

AUTOMATED TEST GENERATION FOR SOFTWARE COMPONENTS

Mining Social Network Graphs

Chapter 2: Algorithm Discovery and Design. Invitation to Computer Science, C++ Version, Third Edition

A Formal Model of Program Dependences and Its Implications for Software Testing, Debugging, and Maintenance

Instruction Set Architecture (ISA)

How to Sandbox IIS Automatically without 0 False Positive and Negative

The ConTract Model. Helmut Wächter, Andreas Reuter. November 9, 1999

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

Why? A central concept in Computer Science. Algorithms are ubiquitous.

Compiler Construction

Lecture 1: Course overview, circuits, and formulas

Chapter 14 The Binary Search Tree


Where we are CS 4120 Introduction to Compilers Abstract Assembly Instruction selection mov e1 , e2 jmp e cmp e1 , e2 [jne je jgt ] l push e1 call e

A Fast Algorithm For Finding Hamilton Cycles

SYSM 6304: Risk and Decision Analysis Lecture 5: Methods of Risk Analysis

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Formal Languages and Automata Theory - Regular Expressions and Finite Automata -

Binary Trees and Huffman Encoding Binary Search Trees

Sources: On the Web: Slides will be available on:

CSE 4351/5351 Notes 7: Task Scheduling & Load Balancing

The PageRank Citation Ranking: Bring Order to the Web

Warshall s Algorithm: Transitive Closure

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

Transcription:

Optimizations Code transformations to improve program Mainly: improve execution time Also: reduce program size Control low Graphs 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 1 2 Optimization Safety Safety of code transformations usually requires certain information that may not be explicit in the code Example: dead code elimination (1) (2) (3) x=y+z; (4) (5) What statements are dead and can be removed? Optimization Safety Safety of code transformations usually requires certain information which may not explicit in the code Example: dead code elimination (1) (2) (3) x=y+z; (4) (5) Need to know whether 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 3 4 1

Dead Variable Example Add control flow to example: Is x = y+1 dead code? Is z = 1 dead code? Dead Variable Example Add control flow to example: Statement x = y+1 is not dead code! On some executions, value is used later 5 6 Dead Variable Example Add more control flow: Is x = y+1 dead code? Is z = 1 dead code? Dead Variable Example Add more control flow: Statement x = y+1 not dead (as before) Statement z = 1 not dead either! On some executions, value from z=1 is used later 7 8 2

Low-level Code Harder to eliminate dead code in low-level code: fjumpdl3 Are these statements dead? Low-level Code Harder to eliminate dead code in low-level code: fjumpdl3 9 10 Optimizations and Control low 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 11 Control low Graphs Control low Graph (CG) = graph representation of computation and control flow in the program framework for static analysis of program control-flow Nodes are basic blocks = straight-line, single- entry code, no branching except at end of sequence Edges represent possible flow of control from the end of one block to the beginning of the other here may be multiple incoming/outgoing edges for each block 12 3

Program x = z-2 ; { x = x+1; y=y+1; y+1; else { CG Example B 2 Control low Graph x = x+1; y = y+1; B 3 Basic Blocks Basic block = sequence of consecutive statements such that: Control enters only at beginning of sequence Control leaves only at end of sequence incoming control a = a+1; b = c*a; switch(b) outgoing control No branching in or out in the middle of basic blocks 13 14 Computation and Control low Multiple Program Executions Basic Blocks = Nodes in the graph = computation in the program B 2 Edges in the graph = control flow in the program Control low Graph x = x+1; y = y+1; B 3 CG models all program executions Possible execution = path in the graph Multiple paths = multiple possible program executions B 2 Control low Graph x = x+1; y = y+1; B 3 15 16 4

Execution 1 Execution 2 CG models all program executions Possible execution = path in the graph Execution 1: c is true Program executes basic blocks, B 2, B 2 Control low Graph x = x+1; y = y+1; CG models all program executions Possible execution = path in the graph Execution 2: c is false Program executes basic blocks, B 3, Control low Graph B 3 17 18 Infeasible Executions Edges Going Out CG models all program executions, and then some Possible execution = path in the graph Execution 2: c is false and true (?!) Program executes basic blocks, B 3, and the successor of Control low Graph B 3 19 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 20 5

Edges Coming In Multiple incoming edges Control may come from any of the predecessor basic blocks Each incoming edge = incoming flow of control in some execution of the program incoming edges Basic Block Building the CG Can construct CG for either high-level IR or the low-level IR of the program Build CG for high-level IR Construct CG for each high-level IR node Build CG for low-level IR Analyze jump and label statements 21 22 CG for High-level IR CG(S) = flow graph of high-level statement S CG(S) is single-entry, single-exit graph: one entry node (basic block) one exit node (basic block) CG(S) = Recursively define CG(S) Entry Exit CG for Block Statement CG( S1; S2; ; SN ) = CG(S1) CG(S2) CG(SN) 23 24 6

CG for If-then-else Statement CG for If-then Statement CG ( if (E) S1 else S2 ) CG( if (E) S ) if (E) if (E) CG(S1) CG(S2) CG(S1) Empty basic block 25 26 CG for While Statement Recursive CG Construction CG for: while (e) S Nested statements: recursively construct CG while traversing IR nodes Example: if (e) CG(S) x=y+1; 27 28 7

Recursive CG Construction Nested statements: recursively construct CG while traversing IR nodes Recursive CG Construction Nested statements: recursively construct CG while traversing IR nodes x=y+1; CG(while) CG(z=x) x=y+1; CG(body) z=x 29 30 Recursive CG Construction Nested statements: recursively construct CG while traversing IR nodes x=y+1; x = y+1 y = 2*z CG(if) z = 1 z = x Recursive CG Construction Simple algorithm to build CG Generated CG Each basic block has a single statement here are empty basic blocks Small basic blocks = inefficient Small blocks = many nodes in CG Compiler uses CG to perform optimization Many nodes in CG = compiler optimizations will be time- and space-consuming 31 32 8

Efficient CG Construction Basic blocks in CG: As few as possible As large as possible here 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 here should be no empty basic blocks Efficient CG: Example x = y+z x = y+1 y =2*z if (d) z = 1 z = x 33 34 CG for Low-level IR CG for Low-level IR Identify pre-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 fjump d L3 Basic block start: At label instructions After jump instructions Basic blocks end: At jump instructions Before label instructions fjump d L3 35 36 9

CG for Low-level IR CG for Low-level IR Conditional jump: 2 successors Unconditional jump: 1 successor fjump d L3 x = y+z x = y+1 y =2*z if (d) z = 1 z = x fjump d L3 37 38 10