ECE1724F Compiler Primer Sept. 18, 2002.

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.
ANALYSIS OF PROG. LANG. PROGRAM ANALYSIS Instructors: Crista Lopes Copyright © Instructors. 1.
1 Authors: Vugranam C. Sreedhar, Roy Dz-Ching Ju, David M. Gilles and Vatsa Santhanam Reader: Pushpinder Kaur Chouhan Translating Out of Static Single.
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.
Data Flow Analysis. Goal: make assertions about the data usage in a program Use these assertions to determine if and when optimizations are legal Local:
ECE 454 Computer Systems Programming Compiler and Optimization (I) Ding Yuan ECE Dept., University of Toronto
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
Control Flow Analysis. Construct representations for the structure of flow-of-control of programs Control flow graphs represent the structure of flow-of-control.
1 Code Optimization Code produced by compilation algorithms can often be improved (ideally optimized) in terms of run-time speed and the amount of memory.
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2011 More Control Flow John Cavazos University.
1 Code Optimization. 2 The Code Optimizer Control flow analysis: control flow graph Data-flow analysis Transformations Front end Code generator Code optimizer.
Control Flow Analysis (Chapter 7) Mooly Sagiv (with Contributions by Hanne Riis Nielson)
Program Representations. Representing programs Goals.
1 Constant Propagation for Loops with Factored Use-Def Chains Reporter : Lai, Yen-Chang.
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
Program Representations Xiangyu Zhang. CS590F Software Reliability Why Program Representations  Initial representations Source code (across languages).
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
Cpeg421-08S/final-review1 Course Review Tom St. John.
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.
1 Intermediate representation Goals: encode knowledge about the program facilitate analysis facilitate retargeting facilitate optimization scanning parsing.
Lecture 6 Program Flow Analysis Forrest Brewer Ryan Kastner Jose Amaral.
2015/6/24\course\cpeg421-10F\Topic1-b.ppt1 Topic 1b: Flow Analysis Some slides come from Prof. J. N. Amaral
Program Representations Xiangyu Zhang. CS590Z Software Defect Analysis Program Representations  Static program representations Abstract syntax tree;
CS 412/413 Spring 2007Introduction to Compilers1 Lecture 29: Control Flow Analysis 9 Apr 07 CS412/413 Introduction to Compilers Tim Teitelbaum.
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.
ECE1724F Compiler Primer Sept. 18, 2002.
Prof. Fateman CS164 Lecture 211 Local Optimizations Lecture 21.
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.
PSUCS322 HM 1 Languages and Compiler Design II IR Code Optimization Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving.
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.
1 Code Optimization Chapter 9 (1 st ed. Ch.10) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Software (Program) Analysis. Automated Static Analysis Static analyzers are software tools for source text processing They parse the program text and.
What’s in an optimizing compiler?
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By:
Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? , Semester 1,
Intermediate Code Representations
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)
Program Representations. Representing programs Goals.
CS 614: Theory and Construction of Compilers Lecture 15 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
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.
Control Flow Analysis Compiler Baojian Hua
Machine-Independent Optimizations Ⅳ CS308 Compiler Theory1.
Loops Simone Campanoni
1 Chapter10: Code generator. 2 Code Generator Source Program Target Program Semantic Analyzer Intermediate Code Generator Code Optimizer Code Generator.
Simone Campanoni CFA Simone Campanoni
Basic Program Analysis
High-level optimization Jakub Yaghob
CSC D70: Compiler Optimization LICM: Loop Invariant Code Motion
Factored Use-Def Chains and Static Single Assignment Forms
Control Flow Analysis CS 4501 Baishakhi Ray.
Code Optimization Chapter 10
Topic 4: Flow Analysis Some slides come from Prof. J. N. Amaral
Control Flow Analysis (Chapter 7)
Interval Partitioning of a Flow Graph
Easy IR generation Kai Nacke 3 February 2019 LLVM dev FOSDEM‘19
Taken largely from University of Delaware Compiler Notes
CSC D70: Compiler Optimization LICM: Loop Invariant Code Motion
Code Generation Part II
Presentation transcript:

ECE1724F Compiler Primer Sept. 18, 2002

What’s in an optimizing compiler? Front EndOptimizer High-level language (C, C++, Java) Low-level language (mc68000, ia32, etc…) HLLIR (Usually very naive) IR (Better, we hope) Code Generator LLL CS488 ECE540

What are compiler optimizations? Optimization:the transformation of a program P into a program P’, that has the same input/output behavior, but is somehow “better”. “better” means:  faster  or smaller  or uses less power  or whatever you care about P’ is not optimal, may even be worse than P

An optimizations must: Preserve correctness  the speed of an incorrect program is irrelevant On average improve performance  P’ is not optimal, but it should usually be better Be “worth the effort”  1 person-year of work, 2x increase in compilation time, a 0.1% improvement in speed?  Find the bottlenecks  90/10 rule: 90% of the gain for 10% of the work

Compiler High-level language C, C++, Java Low-level language mc68000 Lexical Analyzer Parser IR Code Generator Optimizer m/c Code Generator tokensASTIR IR: Intermediate Representation Compiler Phases (Passes)

Control Flow Analysis

Purpose of Control Flow Analysis Determine the control structure of a program  determine possible control flow paths  find basic blocks and loops Intraprocedural: within a procedure Interprocedural: across procedures  Whole program  Maybe just within the same file cc –c file1.c cc –c file2.c cc –o myprogram file1.o file2.o -l mylib

All about Control flow analysis: Finding basic blocks Creating a control flow graph Finding dominators  dominators, proper dominators, direct dominators Finding post-dominators Finding loops

Basic Blocks A Basic Block (BB) is a maximal section of “straight-line” code which can only be entered via the first instruction and can only be existed via the last instruction. S1read L S2n = 0 S3k = 0 S4m = 1 S5k = k + m S6c = k > L S7if (c) goto S11 S8n = n +1 S9m = m +2 S10goto S5 S11write n BB 1 BB 2 BB 3 BB 4

Control Flow Graphs The Control Flow Graph (CFG) of a program is a directed graph G=(N, E) whose nodes N represent the basic blocks in the program and whose edges E represent transfers of control between basic blocks. S1read L S2n = 0 S3k = 0 S4m = 1 S5k = k + m S6c = k > L S7if (c) goto S11 S8n = n +1 S9m = m +2 S10goto S5 S11write n BB 1 BB 2 BB 3 BB 4 BB 1 BB 2 BB 3BB 4

Control Flow Graphs (continued) An entry node in G is one which has no predecessors. An exit node in G is one which has no successors. BB 1 BB 2 BB 3BB 4 Entry Exit It is common and convenient to add an entry node Entry and an exit node Exit to the CFG. Given G = (N, E) and a basic block b  N. The successors of b, denoted by succ(b), is the set of basic blocks that can be reached from b by traversing one edge: succ(b) = {n  N | (b,n)  E} The predecessors of b, denoted by pred(b), is the set of basic blocks that can reach b by traversing one edge: pred(b) = {m  N | (m,b)  E}

Dominators Let G=(N, E) denote a CFG. Let n, n’  N. n is said to dominate n’, denoted n dom n’, iff every path from Entry to n’ contains n. 1 dom  1 ; 1  dom  2 ; 1 dom 3 ; 1 dom 4 ; 2 dom 2 ; 2 dom 3 ; 2 dom 4 ; 3 dom 3 ; 4 dom 4. BB 1 BB 2 BB 3BB 4 Entry Exit

Post-Dominators Let G=(N, E) denote a CFG. Let n, n’  N. Then: n is said to post-dominate n’, denoted n pdom n’, iff every path from n’ to Exit contains n. 1 pdom 1 ; 2  pdom 1 ; 4 pdom 1 ; 2 pdom 2 ; 4 pdom 2 ; 3 pdom 3; 2 pdom 3 ; 4 pdom 3; 4 pdom 4. BB 1 BB 2 BB 3BB 4 Entry Exit

Loops Goal: find loops in CFG irrespective of input syntax  DO, while, for, goto, etc. Intuitively, a loop is the set of nodes in a CFG that form a cycle. However, not every cycle is a “loop”. A “natural” loop has a single entry node h  N and a tail node t  N, such that (t,h)  E; loop can be entered only through h; the loop contains h and all nodes that can reach t without going through h. BB 1 BB 2 BB 3 BB 4 Entry Exit BB 1 BB 2 BB 4 Entry Exit BB 3 BB 5 BB 6 BB 1 Entry Exit BB 3BB 2 BB 4

Loop Pre-Headers Several optimizations require that code be moved “before the header”. It is convenient to create a new block called the pre-header. The pre-header has only the header as successor. All edges that formerly entered the header instead enter the pre- header, with the exception of edges from inside the loop. See that you have a store of an expression to a temporary followed by an assignment to a variable. If the expression operands are not changed to point of substitution replace with expression. header pre- header