Basic Blocks Mooly Sagiv Schrierber 317 03-640-7606 Wed 10:00-12:00 html://www.math.tau.ac.il/~msagiv/courses/wcc01.html Chapter 8.

Slides:



Advertisements
Similar presentations
CSE 5317/4305 L9: Instruction Selection1 Instruction Selection Leonidas Fegaras.
Advertisements

Code Generation for Control Flow Mooly Sagiv Ohad Shacham Chapter 6.4.
ANALYSIS OF PROG. LANG. PROGRAM ANALYSIS Instructors: Crista Lopes Copyright © Instructors. 1.
Intermediate Code Generation
The University of Adelaide, School of Computer Science
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.
Compilation 2011 Static Analysis Johnni Winther Michael I. Schwartzbach Aarhus University.
Register Allocation Mooly Sagiv Schrierber Wed 10:00-12:00 html://
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
3-Valued Logic Analyzer (TVP) Tal Lev-Ami and Mooly Sagiv.
Control Flow Analysis (Chapter 7) Mooly Sagiv (with Contributions by Hanne Riis Nielson)
COMPILERS Basic Blocks and Traces hussein suleman uct csc3005h 2006.
Intermediate Representation I High-Level to Low-Level IR Translation EECS 483 – Lecture 17 University of Michigan Monday, November 6, 2006.
Chapter 14: Building a Runnable Program Chapter 14: Building a runnable program 14.1 Back-End Compiler Structure 14.2 Intermediate Forms 14.3 Code.
CS412/413 Introduction to Compilers Radu Rugina Lecture 16: Efficient Translation to Low IR 25 Feb 02.
Code Generation Mooly Sagiv html:// Chapter 4.
Intermediate Code Generation Mooly Sagiv Schrierber Wed 10:00-12:00 html://
Register Allocation Mooly Sagiv html://
Introduction to Code Generation Mooly Sagiv html:// Chapter 4.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
Liveness Analysis Mooly Sagiv Schrierber Wed 10:00-12:00 html://
1 Control Flow Analysis Mooly Sagiv Tel Aviv University Textbook Chapter 3
Code Generation for Basic Blocks Introduction Mooly Sagiv html:// Chapter
Tentative Schedule 20/12 Interpreter+ Code Generation 27/12 Code Generation for Control Flow 3/1 Activation Records 10/1 Program Analysis 17/1 Register.
Run time vs. Compile time
Code Generation for Control Flow Mooly Sagiv html:// Chapter 6.4.
Instruction Selection Mooly Sagiv Schrierber Wed 10:00-12:00 html://
Improving Code Generation Honors Compilers April 16 th 2002.
Introduction to Code Generation Mooly Sagiv html:// Chapter 4.
Register Allocation Recap Mooly Sagiv html:// Special Office Hours Wednesday 12-14, Thursday 12-14, Schriber.
Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Overview of program analysis Mooly Sagiv html://
Register Allocation Mooly Sagiv html://
Code Generation for Control Flow Mooly Sagiv html:// Chapter 6.4.
Procedure Optimizations and Interprocedural Analysis Chapter 15, 19 Mooly Sagiv.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Data Flow Analysis Compiler Baojian Hua
Stacks & Recursion. Stack pushpop LIFO list - only top element is visible top.
1 Week 4 Questions / Concerns Comments about Lab1 What’s due: Lab1 check off this week (see schedule) Homework #3 due Wednesday (Define grammar for your.
1 Code Generation Part II Chapter 8 (1 st ed. Ch.9) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Activation Records (in Tiger) CS 471 October 24, 2007.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Abstract Syntax Mooly Sagiv Schrierber Wed 10:00-12:00 html://
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
CPS 506 Comparative Programming Languages Syntax Specification.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
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.
Instruction Selection Mooly Sagiv Schrierber Wed 10:00-12:00 html://
High Performance Embedded Computing © 2007 Elsevier Lecture 10: Code Generation Embedded Computing Systems Michael Schulte Based on slides and textbook.
CSC 4181 Compiler Construction
Code Generation for Control Flow Mooly Sagiv html:// Chapter
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
1 Topic 7: Intermediate Representations COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer.
CS 404 Introduction to Compiler Design
Compiler Design (40-414) Main Text Book:
Mooly Sagiv html://
Ch. 4 – Semantic Analysis Errors can arise in syntax, static semantics, dynamic semantics Some PL features are impossible or infeasible to specify in grammar.
COMPILERS Basic Blocks and Traces
COMPILERS Basic Blocks and Traces
Intermediate Code Generation
Compiler Construction
Presentation transcript:

Basic Blocks Mooly Sagiv Schrierber Wed 10:00-12:00 html:// Chapter 8

Already Studied Source program (string) lexical analysis syntax analysis semantic analysis Translate Tokens Abstract syntax tree Tree IR Abstract syntax tree

Mismatches between IR and Machine Languages CJUMP jumps into two labels –But typical machine instructions use one target BEQ Ri, Rj, L Optimizing IR programs is difficult due to side effects in expressions –ESEQ nodes –Call nodes Call nodes within expressions prevent passing arguments in registers

Mismatches between IR and Machine Languages Call nodes within expressions prevent passing arguments and results in registers binop pluscall Name f 1 exp 1 Name f 2 exp 2

Why can’t we be smarter? Avoid two-way jumps Do not use ESEQ expressions

Three Phase Solution Rewrite the tree into a list of canonical trees without SEQ or ESEQ nodes Group the list into basic blocks Order basic blocks into a set of traces –CJUMP is immediately followed by false label

nfact example function nfactor (n: int): int= if n = 0 then 1 else n * nfactor(n-1)

MOVE TEMP t103 ESEQ CJUMP EQ TEMP t128 CONST 0 ESEQ LABEL l0 ESEQ MOVE TEMP t129CONST 1 l1 l0 ESEQ JUMP NAME l2 ESEQ LABEL l1 ESEQ MOVE TEMP t129 BINOP TIMES TEMP t128 CALL nfactor BINOP MINUS TEMP t128 CONST 1 ESEQ LABEL l2 TEMP t129 Missing updates for static link

SEQ CJUMP EQ TEMP t128 CONST 0 LABEL l0 SEQ MOVE TEMP t129 CONST 1 l1 l0 SEQ JUMP NAME l2 SEQ LABEL l1 SEQ TEMP t129 BINOP TIMES TEMP t131 CALL nfactor BINOP MINUS TEMP t128CONST 1 MOVE TEMP t103 TEMP t129 SEQ LABEL l2 SEQ TEMP t130 MOVE TEMP t130 MOVE TEMP t128 TEMP t131 SEQ MOVE

LABEL(l3) CJUMP(EQ, TEMP t128, CONST 0, l0, l1) LABEL( l0) MOVE(TEMP t129, CONST 1) JUMP(NAME l2) LABEL( l1) MOVE(TEMP t131, TEMP t128) MOVE(TEMP t130, CALL(nfactor, BINOP(MINUS, TEMP t128, CONST 1))) MOVE(TEMP t129, BINOP(TIMES, TEMP t131, TEMP t30)) JUMP(NEME l2) LABEL( l2) MOVE(TEMP t103, TEMP t129) JUMP(NAME lend)

LABEL(l3) CJUMP(EQ, TEMP t128, CONST 0, l0, l1) LABEL( l0) MOVE(TEMP t129, CONST 1) JUMP(NAME l2) LABEL( l1) MOVE(TEMP t131, TEMP t128) MOVE(TEMP t130, CALL(nfactor, BINOP(MINUS, TEMP t128, CONST 1))) MOVE(TEMP t129, BINOP(TIMES, TEMP t131, TEMP t130)) /* JUMP(NAME l2) */ LABEL( l2) MOVE(TEMP t103, TEMP t129) JUMP(NAME lend)

Outline The Cannon Interface Phase 1: Removal of ESEQ nodes Phase 2: Basic Blocks Phase 3: Order traces –CJUMP is followed by a false label

/* canon.h */ typedef struct C_stmListList *C_stmListList; struct C_block {C_stmListList stmLists; Temp_label label;} struct C_stmListList_ { T_stmList head; C_stmListList tail;} T_stmList C_linearize(T_stm stm); /* Eliminate ESEQs */ struct C_block C_basicBlocks(T_stmList stmList); T_stmList C_traceSchedule(struct C_block b); /* main.c */ static void doProc(FILE *out, F_frame frame, T_stm body) { T_stmList stmList; AS_instrList iList; stmList = C_linearize(body); /* Removes ESEQs */ stmList = C_traceSchedule(C_basicBlocks(stmList)); iList = F_codegen(frame, stmList); /* 9 */ }

Canonical Trees (Phase 1) Rewrite the tree –No SEQ and ESEQ –The parent of each CALL is either EXP or MOVE(TEMP t, …) Apply “meaning preserving” rewriting rules Sometimes generates temporaries

ESEQ s1 ESEQ s2 e ESEQ SEQ s1 e s2

BINOP op e2 s e1 ESEQ s BINOP e2 op e1

MEM s e ESEQ s e MEM

JUMP s e ESEQ SEQ s e JUMP

CJUMP s e1 ESEQ op e2l1

BINOP op ESEQ s e2 e1 ESEQ s BINOP e2 op e1 When s and e1 commutes

Which statements commute? In general very difficult Example The compiler decides conservatively MEM e1 MOVE MEM e2 e3

Which statements commute? static bool commute(T_stm x, T_exp y) { if (isNop(x)) return TRUE; if (y->kind == T_NAME || y->kind == T_CONST) return TRUE; return FALSE; }

BINOP op ESEQ s e2 e1 ESEQ MOVE BINOP e2 op TEMP t When s and e1 may not commute s TEMP t e1 ESEQ

CJUMP s e2 e1 op ESEQl1 When s and e1 may not commute CJUMP s TEMP t op e2 l1 SEQ MOVE TEMP t e1 SEQ

MOVE TEMP t103 ESEQ CJUMP EQ TEMP t128 CONST 0 ESEQ LABEL l0 ESEQ MOVE TEMP t129CONST 1 l1 l0 ESEQ JUMP NAME l2 ESEQ LABEL l1 ESEQ MOVE TEMP t129 BINOP TIMES TEMP t128 CALL nfactor BINOP MINUS TEMP t128 CONST 1 ESEQ LABEL l2 TEMP t129

SEQ CJUMP EQ TEMP t128 CONST 0 ESEQ LABEL l0 ESEQ MOVE TEMP t129CONST 1 l1 l0 ESEQ JUMP NAME l2 ESEQ LABEL l1 ESEQ MOVE TEMP t129 BINOP TIMES TEMP t128 CALL nfactor BINOP MINUS TEMP t128 CONST 1 ESEQ LABEL l2 TEMP t129 TEMP t103 MOVE

SEQ CJUMP EQ TEMP t128 CONST 0 LABEL l0 ESEQ MOVE TEMP t129CONST 1 l1 l0 ESEQ JUMP NAME l2 ESEQ LABEL l1 ESEQ MOVE TEMP t129 BINOP TIMES TEMP t128 CALL nfactor BINOP MINUS TEMP t128 CONST 1 ESEQ LABEL l2 TEMP t129 TEMP t103 MOVE SEQ

CJUMP EQ TEMP t128 CONST 0 LABEL l0 SEQ MOVE TEMP t129 CONST 1 l1 l0 ESEQ JUMP NAME l2 ESEQ LABEL l1 ESEQ MOVE TEMP t129 BINOP TIMES TEMP t128 CALL nfactor BINOP MINUS TEMP t128 CONST 1 ESEQ LABEL l2 TEMP t129 SEQ TEMP t103 MOVE

SEQ CJUMP EQ TEMP t128 CONST 0 LABEL l0 SEQ MOVE TEMP t129 CONST 1 l1 l0 SEQ JUMP NAME l2 ESEQ LABEL l1 ESEQ MOVE TEMP t129 BINOP TIMES TEMP t128 CALL nfactor BINOP MINUS TEMP t128 CONST 1 ESEQ LABEL l2 TEMP t129 SEQ TEMP t103 MOVE

SEQ CJUMP EQ TEMP t128 CONST 0 LABEL l0 SEQ MOVE TEMP t129 CONST 1 l1 l0 SEQ JUMP NAME l2 SEQ LABEL l1 ESEQ MOVE TEMP t129 BINOP TIMES TEMP t128 CALL nfactor BINOP MINUS TEMP t128 CONST 1 ESEQ LABEL l2 TEMP t129 SEQ TEMP t103 MOVE

SEQ CJUMP EQ TEMP t128 CONST 0 LABEL l0 SEQ MOVE TEMP t129 CONST 1 l1 l0 SEQ JUMP NAME l2 SEQ LABEL l1 SEQ MOVE TEMP t129 BINOP TIMES TEMP t128 CALL nfactor BINOP MINUS TEMP t128 CONST 1 ESEQ LABEL l2 TEMP t129 SEQ TEMP t103 MOVE

SEQ CJUMP EQ TEMP t128 CONST 0 LABEL l0 SEQ MOVE TEMP t129 CONST 1 l1 l0 SEQ JUMP NAME l2 SEQ LABEL l1 SEQ MOVE TEMP t129 BINOP TIMES TEMP t128 CALL nfactor BINOP MINUS TEMP t128CONST 1 ESEQ LABEL l2 TEMP t129 SEQ TEMP t103 MOVE ESEQ TEMP t130 MOVE TEMP t130

SEQ CJUMP EQ TEMP t128 CONST 0 LABEL l0 SEQ MOVE TEMP t129 CONST 1 l1 l0 SEQ JUMP NAME l2 SEQ LABEL l1 SEQ MOVE TEMP t129 BINOP TIMES TEMP t131 CALL nfactor BINOP MINUS TEMP t128CONST 1 ESEQ LABEL l2 TEMP t129 SEQ TEMP t103 MOVE TEMP t130 MOVE TEMP t130 ESEQ MOVE TEMP t128 TEMP t131 ESEQ

SEQ CJUMP EQ TEMP t128 CONST 0 LABEL l0 SEQ MOVE TEMP t129 CONST 1 l1 l0 SEQ JUMP NAME l2 SEQ LABEL l1 SEQ TEMP t129 BINOP TIMES TEMP t131 CALL nfactor BINOP MINUS TEMP t128CONST 1 ESEQ LABEL l2 TEMP t129 SEQ TEMP t103 MOVE TEMP t130 MOVE TEMP t130 MOVE TEMP t128 TEMP t131 ESEQ MOVE

SEQ CJUMP EQ TEMP t128 CONST 0 LABEL l0 SEQ MOVE TEMP t129 CONST 1 l1 l0 SEQ JUMP NAME l2 SEQ LABEL l1 SEQ TEMP t129 BINOP TIMES TEMP t131 CALL nfactor BINOP MINUS TEMP t128CONST 1 ESEQ LABEL l2 TEMP t129 SEQ TEMP t103 MOVE TEMP t130 MOVE TEMP t130 MOVE TEMP t128 TEMP t131 SEQ MOVE

SEQ CJUMP EQ TEMP t128 CONST 0 LABEL l0 SEQ MOVE TEMP t129 CONST 1 l1 l0 SEQ JUMP NAME l2 SEQ LABEL l1 SEQ TEMP t129 BINOP TIMES TEMP t131 CALL nfactor BINOP MINUS TEMP t128CONST 1 MOVE TEMP t103 TEMP t129 SEQ LABEL l2 SEQ TEMP t130 MOVE TEMP t130 MOVE TEMP t128 TEMP t131 SEQ MOVE

A Theoretical Solution Apply rewriting rules until convergence The result need not be unique Efficiency and termination of the compiler

A Practical Solution Apply rewriting rules in “one” pass Two mutually recursive routines –do_stm(s) applies rewritings to s –do_exp(e) applies rewritings to e reorder(expRefList) –Returns the side effect statements in expRefList –Replaces expressions by temporaries Code distributed in “cannon.c”

Taming Conditional Brunch Reorder statements so that CJUMP is followed by a false label Two subphases: –Partition the statement list into basic blocks (straightline programs starting with a label and ending with a branch) –Reorder basic blocks (Traces)

Phase 2: Basic Blocks The compiler does not know which branch will be taken Conservatively analyze the control flow of the program A basic block –The first statement is a LABEL –The last statement is JUMP or CJUMP –There are no other LABELs, JUMPs, or CJUMPs

An Algorithm for Basic Blocks C_basicBlocks() Applied for each function body Scan the statement list from left to right Whenever a LABEL is found –a new block begins (and the previous block ends) Whenever JUMP or CJUMP are found –the current block ends (and the next block begins) When a block ends without JUMP or CJUMP – JUMP to the following LABEL When a block does not start with a LABEL –Add a LABEL At the end of the function body jump to the beginning of the epilogue

LABEL(l3) CJUMP(EQ, TEMP t128, CONST 0, l0, l1) LABEL( l0) MOVE(TEMP t129, CONST 1) JUMP(NAME l2) LABEL( l1) MOVE(TEMP t131, TEMP t128) MOVE(TEMP t130, CALL(nfactor, BINOP(MINUS, TEMP t128, CONST 1))) MOVE(TEMP t129, BINOP(TIMES, TEMP t131, TEMP t130)) LABEL( l2) MOVE(TEMP t103, TEMP t129) JUMP(NAME l2) JUMP(NAME lend)

Traces Reorder basic blocks –Every CJUMP is followed by a false label –Many of the unconditional jumps are followed by the corresponding labels can be eliminated A trace –a sequence of basic blocks that are executed sequentially A program has many overlapping traces Find a set of traces that exactly covers the program –every block appears in exactly one trace Minimize the number of traces

An Algorithm for Generating Traces C_traceSchedule() Put all the blocks of the program into a list Q while Q is not empty do Start a new (empty) trace, call it T Remove the head element b of Q while b is not marked do mark b append b to the end of the current trace T if there is an unmarked successor c of b b := c end of current trace T

LABEL(l3) CJUMP(EQ, TEMP t128, CONST 0, l0, l1) LABEL( l0) MOVE(TEMP t129, CONST 1) JUMP(NAME l2) LABEL( l1) MOVE(TEMP t131, TEMP t128) MOVE(TEMP t130, CALL(nfactor, BINOP(MINUS, TEMP t128, CONST 1))) MOVE(TEMP t129, BINOP(TIMES, TEMP t131, TEMP t130)) JUMP(NAME l2) LABEL( l2) MOVE(TEMP t103, TEMP t129) JUMP(NAME lend) T1 T2

Finishing-Up CJUMP followed by a false label is left alone JUMP (NAME l) that is followed by a label l is removed CJUMP followed by a true label –replace true and false labels and negate the condition If CJUMP(cond, a, b, l t, l f ) is not followed by l t or l f –Replace by: CJUMP(cond, a, b, l t, l' f ) LABEL(l' f ) JUMP(NAME l f ) At the end of the process flat basic blocks (trade simplicity for efficiency of the compiler and of the generated code)

LABEL(l3) CJUMP(EQ, TEMP t128, CONST 0, l0, l1) LABEL( l0) MOVE(TEMP t129, CONST 1) JUMP(NAME l2) LABEL( l1) MOVE(TEMP t131, TEMP t128) MOVE(TEMP t130, CALL(nfactor, BINOP(MINUS, TEMP t128, CONST 1))) MOVE(TEMP t129, BINOP(TIMES, TEMP t131, TEMP t130)) JUMP(NAME l2) LABEL( l2) MOVE(TEMP t103, TEMP t129) JUMP(NAME lend) T1 T2

LABEL(l3) CJUMP(EQ, TEMP t128, CONST 0, l0, l1) LABEL( l0) MOVE(TEMP t129, CONST 1) JUMP(NAME l2) LABEL( l1) MOVE(TEMP t131, TEMP t128) MOVE(TEMP t130, CALL(nfactor, BINOP(MINUS, TEMP t128, CONST 1))) MOVE(TEMP t129, BINOP(TIMES, TEMP t131, TEMP t130)) JUMP(NAME l2) LABEL( l2) MOVE(TEMP t103, TEMP t129) JUMP(NAME lend)

LABEL(l3) CJUMP(EQ, TEMP t128, CONST 0, l0, l1) LABEL( l0) MOVE(TEMP t129, CONST 1) JUMP(NAME l2) LABEL( l1) MOVE(TEMP t131, TEMP t128) MOVE(TEMP t130, CALL(nfactor, BINOP(MINUS, TEMP t128, CONST 1))) MOVE(TEMP t129, BINOP(TIMES, TEMP t131, TEMP t130)) /* JUMP(NAME l2) */ LABEL( l2) MOVE(TEMP t103, TEMP t129) JUMP(NAME lend)

Optimal-Traces Optimizing compilers locate traces for frequently executed instructions Minimize the (dynamic) number of jumps –Improve instruction cache performance Improves register allocation Optimize loops Sometimes use –Static heuristics –Profiling information –Dynamic compilation

prologue statements JUMP (NAME test) LABEL (test) CJ(>, i, N, done, body) LABEL(body) loop-body statements JUMP(NAME(test)) LABEL(done) epilogue statements prologue statements JUMP (NAME test) LABEL (test) CJ(<=, i, N, body, done) LABEL(done) epilogue statements LABEL(body) loop-body statements JUMP(NAME test) prologue statements JUMP (NAME test) LABEL (body) loop-body statements JUMP(NAME TEST) LABEL(TEST) CJ(<=, i, N, body, done) LABEL(done) epilogue statements

Summary Tree like IR saves temporary variables But ESEQ nodes may require some temporaries –Late decision Rewriting rules is a powerful mechanism Estimating “commuting” statements is a challenge Traces may effect the performance of the generated code