High-level optimization Jakub Yaghob

Slides:



Advertisements
Similar presentations
CSC 4181 Compiler Construction Code Generation & Optimization.
Advertisements

Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 3 Developed By:
1 Optimization Optimization = transformation that improves the performance of the target code Optimization must not change the output must not cause errors.
7. Optimization Prof. O. Nierstrasz Lecture notes by Marcus Denker.
Course Outline Traditional Static Program Analysis Software Testing
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.
ECE 454 Computer Systems Programming Compiler and Optimization (I) Ding Yuan ECE Dept., University of Toronto
Jeffrey D. Ullman Stanford University. 2  A never-published Stanford technical report by Fran Allen in  Fran won the Turing award in  Flow.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
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-
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.
1 Code Optimization. 2 The Code Optimizer Control flow analysis: control flow graph Data-flow analysis Transformations Front end Code generator Code optimizer.
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 5 Code Optimizations: Copy Propagation & Elimination.
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.
1 CS 201 Compiler Construction Lecture 1 Introduction.
2015/6/24\course\cpeg421-10F\Topic1-b.ppt1 Topic 1b: Flow Analysis Some slides come from Prof. J. N. Amaral
Intermediate Code. Local Optimizations
Improving Code Generation Honors Compilers April 16 th 2002.
Prof. Fateman CS164 Lecture 211 Local Optimizations Lecture 21.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
Compiler Construction A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University Second Semester 2008/2009.
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.
Optimizing Compilers Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University.
ECE355 Fall 2004Software Reliability1 ECE-355 Tutorial Jie Lian.
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Introduction For some compiler, the intermediate code is a pseudo code of a virtual machine. Interpreter of the virtual machine is invoked to execute the.
What’s in an optimizing compiler?
CSc 453 Final Code Generation Saumya Debray The University of Arizona Tucson.
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
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.
Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By:
Compilers Modern Compiler Design
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)
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.
Code Optimization Code produced by compilation algorithms can often be improved (ideally optimized) in terms of run-time speed and the amount of memory.
Code Optimization Overview and Examples
Code Optimization.
Optimization Code Optimization ©SoftMoore Consulting.
CSC D70: Compiler Optimization
Fall Compiler Principles Lecture 8: Loop Optimizations
Machine-Independent Optimization
Code Generation Part III
Unit IV Code Generation
CS 201 Compiler Construction
Topic 4: Flow Analysis Some slides come from Prof. J. N. Amaral
Code Optimization Overview and Examples Control Flow Graph
Code Generation Part III
Examples of Basic Blocks
Fall Compiler Principles Lecture 10: Loop Optimizations
Interval Partitioning of a Flow Graph
8 Code Generation Topics A simple code generator algorithm
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Intermediate Code Generation
Taken largely from University of Delaware Compiler Notes
Code Generation Part II
TARGET CODE GENERATION
Code Optimization.
CS 201 Compiler Construction
Presentation transcript:

High-level optimization Jakub Yaghob Compiler principles High-level optimization Jakub Yaghob

Optimization Ideal situation – generated code is equal to manually written code Is it valid today? Reality Only in several defined situations It is difficult Optimization Program transformation For speed or size High-level optimization Intermediate code

Criteria for transformation Preserve the meaning of programs Output and program behavior must not be changed by a transformation – always use safe approach Speed up programs by a measurable amount Sometimes space reduction Occasionally the optimization can slow down a program, on the average it improves it A transformation must be worth the effort Complex/slow optimization with negligible effect are not worth the effort

Organization of the code optimizer Control-flow analysis Construct the control flow graph Data-flow analysis Live variables Front end Code optimizer Code generator Control-flow analysis Data-flow analysis Transfor- mations

Control flow graph Three-address code graph representation Nodes represent computation Directed edges represent control flow Important for optimization and representation

Basic block A sequence of consecutive three-address statements in which flow of control enters at the beginning and leaves at the end without halt or possibility of branching except at the end Algorithm for partitioning a sequence of three-address statements into basic blocks Determine the set of leaders The first statement is a leader Any statement that is the target of a jump is a leader Any statement that immediately follows a jump is a leader For each leader, its basic block consists of the leader and all statements up to but not including the next leader or the end of the sequence

Construction of control flow graph A directed graph The nodes are the basic blocks One node is initial – a block whose leader is the first statement in the sequence There is a directed edge from block B1 to block B2, if one of the following condition holds The last statement of B1 is a jump to the leader of B2 B2 immediately follows B1 and B1 does not end in an unconditional jump

Example of control flow graph for(V1;V2;V3) P V1 V2 P V3 V1 V2 P V3

Data-flow analysis Assign live variables to nodes of the control-flow graph Variables need not to be live in the whole basic block Compute exact lifespan inside the basic block Evaluation order Topological sort of DAG Nodes are definitions and uses of a variable Oriented edges are from a use to a definition in a computation Pointer modeling Pointer aliasing Function calls modeling Interprocedural optimization

Live variable analysis Three-address instruction x := y op z defines x and uses y and z A variable is alive at some point of the intermediate code, if the point lies on a path from the point, where it is defined, to a point, where it is used, at the same time there are no other definitions on the path

Kinds of optimizations Inside a basic block No jumps Inside a function Code movement between basic blocks Create, remove basic blocks Whole program (interprocedural) Speed up calls between functions During linker phase with delayed compilation

Local optimization Inside a one basic block Common subexpression elimination (CSE) Copy propagation Dead-code elimination x = y+z, and x is not used any more Constant folding Algebraic transformations/identities x = x + 0, x = x * 1

Local CSE a = b + c b = a – d c = b + c d = a – d a = b + c b = a – d d = b There is a hidden problem Pointer aliasing ap = &a b = a + c *ap = d e = a + c

Global optimization Among more basic blocks, possible movement of a code between BB, control-flow graph modification Common subexpression elimination Copy propagation Dead-code elimination Loop optimization Invariant code motion Reduction in strength of operation Removing induction variable

Loop optimization – 1 Invariant code motion while(i<limit-5) S Expression yields the same result independent of the number of times a loop is executed while(i<limit-5) S t = limit – 5; while(i<t) S

Loop optimization – 2 Reduction in strength of operation Transform multiplication to addition Removing induction variable Only one induction variable for one loop Usually removed during reduction in strength for(i=3;i<8;i+=3) { j = i*2; a[j] = j; } for(t=6;t<16;t+=6) a[t] = t;

Example – C code i=m-1; j=n; v=a[n]; for(;;) { do ++i; while(a[i]<v); do --j; while(a[j]>v); if(i>=j) break; x=a[i]; a[i]=a[j]; a[j]=x; } x=a[i]; a[i]=a[n]; a[n]=x;

Example – three-address code i = m-1 j = n t1 = 4*n v = a[t1] i = i+1 t2 = 4*i t3 = a[t2] if t3<v goto 5 j = j-1 t4 = 4*j t5 = a[t4] if t5>v goto 9 if i>=j goto 23 t6 = 4*i x = a[t6] t7 = 4*i t8 = 4*j t9 = a[t8] a[t7] = t9 t10 = 4*j a[t10] = x goto 5 t11 = 4*i x = a[t11] t12 = 4*i t13 = 4*n t14 = a[t13] a[t12] = t14 t15 = 4*n a[t15] = x

Example – control-flow graph B1 B4 i = m-1 j = n t1 = 4*n v = a[t1] if i>=j goto B6 B5 B6 t6 = 4*i x = a[t6] t7 = 4*i t8 = 4*j t9 = a[t8] a[t7] = t9 t10 = 4*j a[t10] = x goto B2 t11 = 4*i x = a[t11] t12 = 4*i t13 = 4*n t14 = a[t13] a[t12] = t14 t15 = 4*n a[t15] = x B2 i = i+1 t2 = 4*i t3 = a[t2] if t3<v goto B2 B3 j = j-1 t4 = 4*j t5 = a[t4] if t5>v goto B3

Example - LCSE B5 B5 t6 = 4*i x = a[t6] t7 = 4*i t8 = 4*j t9 = a[t8] a[t10] = x goto B2 t6 = 4*i x = a[t6] t8 = 4*j t9 = a[t8] a[t6] = t9 a[t8] = x goto B2

Example – GCSE B1 B4 i = m-1 j = n t1 = 4*n v = a[t1] if i>=j goto B6 B5 B6 x = t3 a[t2] = t5 a[t4] = x goto B2 x = t3 t14 = a[t1] a[t2] = t14 a[t1] = x B2 i = i+1 t2 = 4*i t3 = a[t2] if t3<v goto B2 B3 j = j-1 t4 = 4*j t5 = a[t4] if t5>v goto B3

Example – copy propagation and dead-code elimination B5 B5 x = t3 a[t2] = t5 a[t4] = x goto B2 x = t3 a[t2] = t5 a[t4] = t3 goto B2 B5 B5 x = t3 a[t2] = t5 a[t4] = t3 goto B2 a[t2] = t5 a[t4] = t3 goto B2

Example – reduction in strength B1 B1 i = m-1 j = n t1 = 4*n v = a[t1] i = m-1 j = n t1 = 4*n v = a[t1] t4 = 4*j B2 B2 B3 B3 j = j-1 t4 = 4*j t5 = a[t4] if t5>v goto B3 j = j-1 t4 = t4-4 t5 = a[t4] if t5>v goto B3 B4 B4 if i>=j goto B6 if i>=j goto B6 B5 B6 B5 B6

Example – removing induction variable i = m-1 j = n t1 = 4*n v = a[t1] t4 = 4*j i = m-1 j = n t1 = 4*n v = a[t1] t4 = 4*j B2 B2 B3 B3 j = j-1 t4 = t4-4 t5 = a[t4] if t5>v goto B3 t4 = t4-4 t5 = a[t4] if t5>v goto B3 B4 B4 if i>=j goto B6 if i>=j goto B6 B5 B6 B5 B6

Example – result Is it OK? B1 B4 i = m-1 j = n t1 = 4*n v = a[t1] t4 = 4*j if t2>=t4 goto B6 B5 B6 a[t2] = t5 a[t4] = t3 goto B2 t14 = a[t1] a[t2] = t14 a[t1] = t3 B2 t2 = t2+4 t3 = a[t2] if t3<v goto B2 B3 t4 = t4-4 t5 = a[t4] if t5>v goto B3 Is it OK?

Example – final result Another application of LCSE on B1 B1 B4 i = m-1 t1 = 4*n v = a[t1] t2 = 4*i t4 = t1 if t2>=t4 goto B6 B5 B6 a[t2] = t5 a[t4] = t3 goto B2 t14 = a[t1] a[t2] = t14 a[t1] = t3 B2 t2 = t2+4 t3 = a[t2] if t3<v goto B2 B3 t4 = t4-4 t5 = a[t4] if t5>v goto B3 Another application of LCSE on B1

Parallelization and vectorization Implicit × explicit Code parts allowing concurrent execution Prefetch variables to a cache Vectorization Expression parallelization using SIMD operations

Profile Guided Optimization Three phases Instrumentation Specially compiled code with calls to “collecting” functions at the start and the end of each basic block Profiling Run the instrumented code with a “typical” input Collect statistical data about visiting collecting functions Optimization Collected data influence optimization Used in optimization upon intermediate code and code Adding weights to edges in control-flow graph Looking for most important paths in control-flow graph Register assignment