Programming Language Processor Theory

Similar documents
CS 132 Compiler Construction

Fundamentals of Compilation

CS 352: Compilers: Principles and Practice

Compiler Construction

PART ONE Fundamentals of Compilation

Compiler Design (40-414)

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

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

7. Optimization! Prof. O. Nierstrasz! Lecture notes by Marcus Denker!

Data Structures and Algorithms in Compiler Optimization. Comp314 Lecture Dave Peixotto

PSD3A Principles of Compiler Design Unit : I-V. PSD3A- Principles of Compiler Design

Tour of common optimizations

CS577 Modern Language Processors. Spring 2018 Lecture Optimization

Building a Runnable Program and Code Improvement. Dario Marasco, Greg Klepic, Tess DiStefano

Intermediate representation

VIVA QUESTIONS WITH ANSWERS

Middle End. Code Improvement (or Optimization) Analyzes IR and rewrites (or transforms) IR Primary goal is to reduce running time of the compiled code

CA Compiler Construction

The analysis part breaks up the source program into constituent pieces and creates an intermediate representation of the source program.

CS426 Compiler Construction Fall 2006

Lecture 21 CIS 341: COMPILERS

CS 406/534 Compiler Construction Putting It All Together

Where we are. What makes a good IR? Intermediate Code. CS 4120 Introduction to Compilers

Sardar Vallabhbhai Patel Institute of Technology (SVIT), Vasad M.C.A. Department COSMOS LECTURE SERIES ( ) (ODD) Code Optimization

Intermediate Code Generation

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

LECTURE NOTES ON COMPILER DESIGN P a g e 2

Register Allocation. CS 502 Lecture 14 11/25/08

Itree Stmts and Exprs. Back-End Code Generation. Summary: IR -> Machine Code. Side-Effects

Compiler Design Overview. Compiler Design 1

Lecture Notes on Liveness Analysis

Modern Compiler Implementation in ML

Group B Assignment 9. Code generation using DAG. Title of Assignment: Problem Definition: Code generation using DAG / labeled tree.

6. Intermediate Representation

PRINCIPLES OF COMPILER DESIGN

Topic 7: Intermediate Representations

Lecture Notes on Dataflow Analysis


COP5621 Exam 4 - Spring 2005

What Do Compilers Do? How Can the Compiler Improve Performance? What Do We Mean By Optimization?

CS606- compiler instruction Solved MCQS From Midterm Papers

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

CS5363 Final Review. cs5363 1

6. Intermediate Representation!

Formal Languages and Compilers Lecture I: Introduction to Compilers

Lecture Compiler Middle-End

CST-402(T): Language Processors

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

Compilers and Code Optimization EDOARDO FUSELLA

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

Principles of Compiler Design

Compiler Construction 2016/2017 Loop Optimizations

Compilers Crash Course

IR Optimization. May 15th, Tuesday, May 14, 13

CPSC 510 (Fall 2007, Winter 2008) Compiler Construction II

SYLLABUS UNIT - I UNIT - II UNIT - III UNIT - IV CHAPTER - 1 : INTRODUCTION CHAPTER - 4 : SYNTAX AX-DIRECTED TRANSLATION TION CHAPTER - 7 : STORA

CHAPTER ONE OVERVIEW. 1.1 Continuation-passing style

USC 227 Office hours: 3-4 Monday and Wednesday CS553 Lecture 1 Introduction 4

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

BIRLA INSTITUTE OF TECHNOLOGY AND SCIENCE, Pilani Pilani Campus Instruction Division

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.

Lecture Notes on Alias Analysis

An Overview to Compiler Design. 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 1

Lecture Notes on Intermediate Representation

Compiler Construction 1. Introduction. Oscar Nierstrasz

Principles of Compiler Design

SRM UNIVERSITY FACULTY OF ENGINEERING AND TECHNOLOGY SCHOOL OF COMPUTING DEPARTMENT OF CSE COURSE PLAN

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

Lecture Notes on Intermediate Representation

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI

An Overview of Compilation

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

About the Authors... iii Introduction... xvii. Chapter 1: System Software... 1

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

CS 432 Fall Mike Lam, Professor. Code Generation

Compilers. Intermediate representations and code generation. Yannis Smaragdakis, U. Athens (original slides by Sam

Static Single Information from a Functional Perspective

G.PULLAIH COLLEGE OF ENGINEERING & TECHNOLOGY

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

Goals of Program Optimization (1 of 2)

Office Hours: Mon/Wed 3:30-4:30 GDC Office Hours: Tue 3:30-4:30 Thu 3:30-4:30 GDC 5.

Introduction to Compilers

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

CS 4120 and 5120 are really the same course. CS 4121 (5121) is required! Outline CS 4120 / 4121 CS 5120/ = 5 & 0 = 1. Course Information

SRM UNIVERSITY FACULTY OF ENGINEERING AND TECHNOLOGY

Control-Flow Analysis

Group B Assignment 8. Title of Assignment: Problem Definition: Code optimization using DAG Perquisite: Lex, Yacc, Compiler Construction

Compilers and Interpreters

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

Compiler Design. Computer Science & Information Technology (CS) Rank under AIR 100

Code Optimization. Code Optimization

Data Flow Analysis. Program Analysis

Lecture Notes on Loop-Invariant Code Motion

CSE 401/M501 Compilers

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

G Compiler Construction Lecture 12: Code Generation I. Mohamed Zahran (aka Z)

Compiler Internals. Reminders. Course infrastructure. Registering for the course

CS 4201 Compilers 2014/2015 Handout: Lab 1

Compiler Construction 2010/2011 Loop Optimizations

ECE573 Introduction to Compilers & Translators

Transcription:

Programming Language Processor Theory Munehiro Takimoto Course Descriptions Method of Evaluation: made through your technical reports Purposes: understanding various theories and implementations of modern compiler construction Reference Books Preface 1. Andrew W. Appel, Modern Compiler Implementation in ML, Cambridge University Press 2. A. V. Aho and M. S. Lam and R. Sethi and J. D. Ullman, Compilers: Principles, Techniques, and Tools Second Edition, Addison Wesley 3. A. W. 4. < 2 > A. V. R. J. D. M. S. 5. 6. Over the past decade, there have been several shifts in the way for building compilers: 1

1. New kinds of programming languages are being used: object-oriented languages with dynamic methods, functional languages with nested scope and first-class function closures. Many of these languages require garbage collection, 2. New machines have large register sets and a high penalty for memory access. and can often run much faster with compiler assistance: scheduling instructions and, managing instructions data for cache locality. 1 Introduction 1. A modern compiler is often organized into many phases, each operating on a different abstract language. 2. Figure 1 shows the phases in a typical compiler. Each phase is implemented as one or more software modules. 3. The interfaces between modules of the compiler are almost as important as the algorithms inside the modules. 1.1 Modules and interfaces Any large software system is much easier to understand and implement if the designer takes care with the fundamental abstractions and interfaces. Breaking the compiler into this many pieces allows for reuse of the components. Modules 1. The modules up through Translate are called front end, and the other modules are called back end 2. To change the source language being compiled, only the front end need to be changed. 2

Source Program Lex Tokens Parse Reductions Parsing Actions Abstract Syntax Environ- ments Tables Semantic Translate Translate Frame Frame Layout IR Trees Canon- icalize IR Trees Instruction Selection Assem Assem Control Flow Flow Graph Data Flow Interference Graph Register Allocation Register Assignment Code Emission Assembly Language Assembler Relocatable Object Code Linker Machine Language Figure 1: Phases of a compiler, and interfaces between them. 3. To change the target-machine for which the compiler produces machine language, it suffices to replace just the Frame Layout and Instruction Selection modules of the back end. The compiler can be attached to a language-oriented syntax editor at the Abstract Syntax interface. Interfaces 1. Abstract Syntax, IR Trees, and Assem, take the form of data structures. Ex. the Parsing Actions phase builds an Abstract Syntax data structure and passes it to the Semantic phase. 2. Other interfaces through Instruction Selection are abstract data types. Ex. the Translate interface is a set of functions that the Semantic phase can call, and the Tokens interface takes the form of a function that the Parser calls to get the next token of the input program. 3

Phase Lex Parse Semantic Actions Semantic Frame Layout Translate Canonicalize Instruction Selection Control Flow Dataflow Register Allocation Code Emission Description Break the source file into individual words, or tokens. Analyze the phrase structure of the program. Build a piece of abstract syntax tree corresponding to each phrase. Determine what each phrase means, relate uses of variables to their definitions, check types of expressions, request translation of each phrase. Place variables, function-parameters, etc. into activation records (stack frames) in a machine-dependent way. Produce intermediate representation trees (IR trees), a notation that is not tied to any particular source language or target-machine architecture. Hoist side effects out of expressions, and clean up conditional branches, for the convenience of the next phases. Group the IR-tree nodes into clumps that correspond to the actions of target-machine instructions. Analyze the sequence of instructions into a control flow graph that shows all the possible flows of control the program might follow when it executes. Gather information about the flow of information through variables of the program; for example, liveness analysis calculates the places where each program variable holds a still-needed value (is live). Choose a register to hold each of the variables and temporary values used by the program; variables not live at the same time can share the same register. Replace the temporary names in each machine instruction with machine registers. Figure 2: Description of compiler phases. 4

1.2 Overview In the first half, I will give some basic theories of a control analysis and a dataflow analysis, and talk about the usage of information collected by the dataflow analysis. Then, I will explain some techniques making the dataflow analysis efficient. In the second half, I will talk about other data structures for code optimizations e.g. a dominator tree and a SSA form. Also, as a technique of extracting instruction level parallelism, I will introduce you an instruction scheduling and a software pipelining. If we have extra time, I will mention important techniques of the front end e.g. a garbage collection and a function closure. I practically use a compiler infrastructure, COINS in order to illustrate the implementations of the static analyses. Regarding details of COINS, you can get information from the following Web site: http://coins-compiler.sourceforge.jp/international/ For information of this class, refer the followings: http://www.cs.is.noda.tus.ac.jp/ mune/master/14/ 2 Dataflow An optimizing compiler transforms programs to improve their efficiency without changing their semantics. There are many transformations that improve efficiency: Common-subexpression elimimination: If an expression is computed more than once, eliminate redundant ones of the computations. Dead-code elimination: Delete a computation whose result will never be used. Register allocation: Keep two nonoverlapping temporaries in the same register. Constant folding: If the operands of an expression are constants, the expression can be replaced with a constant value by computing it at compile time. 5

Figure 3: The structure of COINS This is not a complete list of optimizations. In fact, there can never be a complete list. 2.1 No magic bullet Computability theory shows that it will always be possible to invent new optimizing transformations, where we assume that optimizing is for program size instead of speed to simplify the discussion. 1. Define that fully optimizing compiler is one that transforms each program P to a program Opt(P ) that is the smallest program with the same input/output behavior as P. 2. For any program Q that produces no output and never halts, Opt(Q) is short and easily recognizable: L 1 : goto L 1 6

3. Therefore, if we had a fully optimizing compiler, we could use it to solve the halting problem; to see if there exists an input on which P halts, just see if Opt(P ) is the one-line infinite loop. But we know that no computable algorithm can always tell whether programs halt. a fully optimizing compiler cannot be written either. Instead of fully optimizing compiler, we can build optimizing compilers. An optimizing compiler : transforms P into a program P that always has the same input/output behavior as P, and might be smaller or faster. We hope that P runs faster than the optimized programs produced by our competitors compilers. No matter what optimizing compiler we consider, there must always exist another (usually bigger) optimizing compiler that does a better job. Ex. suppose we have an optimizing compiler A. 1. There must be some program P x which does not halt, such that A(P x ) Opt(P x ). 2. There exists a better compiler B: B(P ) = if P = P x then [L : goto L] else A(P ) This theorem, which tells that for any optimizing compiler there exists a better one, is known as the full employment theorem for compiler writers. 7

2.2 Intermediate representation for flow analysis we will consider intraprocedural global optimization. Intraprocedural global optimization: means the analysis stays within a single procedure or function; global means that the analysis spans all the statements within that procedure. Interprocedural optimization: operates on several procedures and functions at once. Peephole optimization: operates only on pairs of adjacent instructions. Each of the optimizing transformations listed at the beginning of the section can be applied using the following generic recipe: 1. Dataflow analysis: Traverse the flow graph, gathering information about what may happen at run time. 2. Transformation: Modify the program to make it faster in some way; the information gathered by analysis will guarantee that the program s semantics is unchanged. There are many dataflow analyses that can provide useful information for optimizing transformations. Quadruples Most can be described by dataflow equations. We will use an intermediate representation simplified by ensuring that each expression includes only a single mem or binop. We can easily turn ordinary expressions into simplified ones. 1. Whenever there is a nested expression of one binop or mem inside another, or a binop or mem inside a jump or cjump, we split it introducing a new temporary: v e1 + e2 e3 t e2 e3 ; v e1 + t 8

a b binop c goto L L : a b a M[b] M[a] b if a relop b goto L 1 else goto L 2 f(a 1,..., a n ) b f(a 1,..., a n ) Figure 4: Table: Statements in quadruples. Occurrences of a, b, c, f, L denote temp, const, or label only. 2. We also introduce new temporaries to ensure that any store statement (that is, a move whose left-hand side is a mem) has only a temp or a const on its right-hand side, and only a temp or const under the mem. mem[ e1 e2 ] e3 + e4 t1 e1 e2 ; t2 e3 + e4 ; mem[t1] t2 These simple statements are often called quadruples, because the typical statement is a b c with four components (a, b, c, ). We use to stand for an arbitrary binop. They are also called three-address code. The statements take one of the forms shown in Table 4. The optimizer may move, insert, delete, and modify the quadruples. After the optimizations are completed, there will be many move statements that define temporaries that are used only once. It will be necessary to find these and turn them back into nested expressions. Control-Flow Graph To perform analyses on a program, it is often useful to make a controlflow graph. 1. Each statement in the program is a node in the flow graph. 2. If statement x can be followed by statement y, there is an edge from x to y. 9

1 a := 0 a 0 L 1 : b a + 1 c c + b a b 2 if a < N goto L 1 return c 2 3 4 5 b := a+1 c := c+b a := b*2 a<n 6 return c Figure 5: Graph: Control-flow graph of a program. Graph 5 shows the flow graph for a simple loop. Flow graph terminology. 1. A flow-graph node has out-edges that lead to successor nodes, and in-edges that come from predecessor nodes. 2. The set pred[n] is a set of all the predecessors of node n, and succ[n] is a set of all the successors. In Graph 5, the out-edges of node 5 are 5 6 and 5 2, and succ[5] = {2, 6}. The in-edges of 2 are 5 2 and 1 2, and pred[2] = {1, 5}. 2.3 Various dataflow analyses A dataflow analysis of a control flow graph of quadruples collects information about the execution of the program. The results of these analyses can be used to make optimizing transformations of the program. 10