COMPILERS Basic Blocks and Traces

Slides:



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

IF statement (i) Single statement. IF ( logical expression ) statement Example: read(*,*) a if (a. lt. 0) a = -a write(*,*) a Or read(*,*) a if (a < 0)
Code Generation for Control Flow Mooly Sagiv Ohad Shacham Chapter 6.4.
Intermediate Code Generation
Architecture-dependent optimizations Functional units, delay slots and dependency analysis.
1 CS 201 Compiler Construction Machine Code Generation.
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
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-
Code Generation Steve Johnson. May 23, 2005Copyright (c) Stephen C. Johnson The Problem Given an expression tree and a machine architecture, generate.
8 Intermediate code generation
PSUCS322 HM 1 Languages and Compiler Design II IR Code Generation I Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
COMPILERS Basic Blocks and Traces hussein suleman uct csc3005h 2006.
ITEC113 Algorithms and Programming Techniques
Intermediate Representation I High-Level to Low-Level IR Translation EECS 483 – Lecture 17 University of Michigan Monday, November 6, 2006.
CS412/413 Introduction to Compilers Radu Rugina Lecture 16: Efficient Translation to Low IR 25 Feb 02.
Basic Blocks Mooly Sagiv Schrierber Wed 10:00-12:00 html:// Chapter 8.
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.
Program Design and Development
1 CS 201 Compiler Construction Lecture 1 Introduction.
Code Generation for Basic Blocks Introduction Mooly Sagiv html:// Chapter
Improving Code Generation Honors Compilers April 16 th 2002.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
Branching and Looping Examples, cont’d. Remember the generic triple jump world…
CS412/413 Introduction to Compilers Radu Rugina Lecture 15: Translating High IR to Low IR 22 Feb 02.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 8 Intermediate Code Zhang Jing, Wang HaiLing College of Computer Science & Technology Harbin Engineering University.
Compiler Chapter# 5 Intermediate code generation.
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
CPS120: Introduction to Computer Science Decision Making in Programs.
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.
PPL Applicative and Normal Form Verification, Type Checking and Inference.
1 Basic Block and Trace Chapter 8. 2 Tree IR (1) Semantic gap (2) IR is not proper for optimization analysis Machine Languages Eg: - Some expressions.
COMPILERS Intermediate Code hussein suleman uct csc3003s 2007.
Decision Making and Branching (cont.)
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.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
CPS120: Introduction to Computer Science Decision Making in Programs.
Intermediate Code Generation CS 671 February 14, 2008.
COMPILERS Intermediate Code hussein suleman uct csc3003s 2009.
More Code Generation and Optimization Pat Morin COMP 3002.
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.
COMPILERS Instruction Selection
A Simple Syntax-Directed Translator
Parsing and Parser Parsing methods: top-down & bottom-up
COMPILERS Basic Blocks and Traces
Stacks Chapter 4.
Compiler Construction
Chapter 20: Binary Trees.
COMPILERS Intermediate Code
Chapter 21: Binary Trees.
Unit IV Code Generation
The Metacircular Evaluator
CS 201 Compiler Construction
CS 201 Compiler Construction
The Metacircular Evaluator
Code Optimization Overview and Examples Control Flow Graph
Algorithm and Ambiguity
Data Flow Analysis Compiler Design
8 Code Generation Topics A simple code generator algorithm
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Intermediate Code Generation
Compiler Construction
COMPILERS Semantic Analysis
CPS120: Introduction to Computer Science
CS 201 Compiler Construction
Presentation transcript:

COMPILERS Basic Blocks and Traces hussein suleman uct csc3003s 2009

Evaluation Order Its useful to evaluate the subexpressions of an expression in any order. Some IR trees can contain side effects. ESEQ and CALL can contain side effects assignment I/O It there were no side effects in these statements then the order of evaluation would not matter.

IR/MC mismatches CJUMP jumps to one of two labels not one label and next instruction. ESEQ nodes within expressions make order of evaluation significant. CALL nodes within expressions make order of evaluation significant. CALL nodes within the argument of other CALL nodes make allocation of formal- parameter registers difficult.

Canonical Trees 1: No SEQ or ESEQ 2: CALL can only be subtree of EXP(. .) or MOVE(TEMP t,. .) Transformations: lift ESEQs up tree until they can become SEQs turn SEQs into linear list

Simplification Rules ESEQ(s1, ESEQ(s2, e)) => ESEQ(SEQ(s1,s2), e) BINOP(op, ESEQ(s, e1), e2) => ESEQ(s, BINOP(op, e1, e2)) MEM(ESEQ(s, e1)) => ESEQ(s, MEM(e1)) JUMP(ESEQ(s, e1)) => SEQ(s, JUMP(e1)) CJUMP(op, ESEQ(s, e1), e2, l1, l1) => SEQ(s, CJUMP(op, e1, e2, l1, l2)) MOVE(ESEQ(s, e1), e2) = SEQ(s, MOVE(e1, e2)) BINOP(op, e1, ESEQ(s, e2)) => ESEQ(MOVE(TEMP t, e1), ESEQ (s, BINOP(op,TEMP t, e2))) CJUMP(op, e1, ESEQ(s, e2), l1, l2) => SEQ(MOVE(TEMP t, e1), SEQ(s, CJUMP(op,TEMP t, e2, l1, l2))) CALL(f , a) = ESEQ(MOVE(TEMP t, CALL( f , a)), TEMP(t))

General Technique For subexpressions of a node, e1..en, [e1, e2, … ESEQ(s,ei), … , en-1, en] if s commutes with e1..ei-1 (independent), (s; [e1, e2, … ei, … , en-1, en] otherwise, SEQ(MOVE(TEMP t1, e1), SEQ(MOVE(TEMP t2, e2), … SEQ(MOVE(TEMP ti-1, ei-1),s)) [TEMP t1, TEMP t2, … TEMP ti-1, ei, … , en- 1, en] In general, extract children, reorder and then reinsert children

Basic Blocks Divide linear sequence of nodes in each subprogram into basic blocks, where: execution always starts at top and stops at bottom first statement is a LABEL last statement is a JUMP or CJUMP no intervening LABELs, JUMPs or CJUMPs Basic blocks are easier to work with for future optimisations since they can be rearranged, while maintaining logic.

Basic Blocks Algorithm Scan sequence of statements from start to end If LABEL, start new block If JUMP or CJUMP, end block If a block does not start with a LABEL Create new LABEL If a block does not end with JUMP/CJUMP Create new JUMP to next LABEL Add terminal “JUMP done” for end of subprogram.

Traces We want to rearrange basic blocks to optimise the number and nature of jumps. A trace is a sequence of statements that can be consecutively executed during the program execution (e.g., b1, b3, b6 below) block b1: LABEL a … JUMP b block b3: LABEL b … JUMP c block b6: LABEL c … CJUMP ?,a Every program has many overlapping traces – we want a single set that covers all the instructions.

Trace Generation Put all basic blocks into a list Q while Q is not empty Start a new (empty) trace T Remove an element b from Q while b is not marked Mark b Append b to T Check succesors if b for unmarked node and make this the new b End the trace T

JUMP considerations We prefer CJUMP followed by its false label, since this translates to MC conditional jump. If CJUMP followed by its true label, switch true and false labels, and negate conditonal If CJUMP (cond, a, b, lt, lf) followed by some other label, replace with: CJUMP (cond, a, b, lt, lfprime) LABEL lfprime JUMP (NAME lf) Remove all JUMPs followed by their target LABELs.