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

Size: px
Start display at page:

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

Transcription

1 Plan for Today Quiz 2 How to automate the process of performance optimization LLVM: Intro to Intermediate Representation Loops as iteration spaces Data-flow Analysis Intro Control-flow graph terminology Liveness analysis for register allocation Concepts Next Time Some slides are from Calvin Lin s grad compiler slides. CS553 Lecture 2 Optimizations and LLVM 1

2 How do we automate the process? Represent programs in intermediate representations (e.g. LLVM) a = b + c // info about types of a, b, and c d = load *a Typed, 3-address code Program analyses needed to find data dependences between computations Data-flow analyses (dependencies between scalar variables) Data-dependence analysis (dependencies between iterations in loops) Specifying loop transformations and parallelization in the polyhedral framework Represent the loops within an integer tuple space Represent transformations as transformations in the integer tuple space CS553 Lecture 2 Optimizations and LLVM 2

3 LLVM Intermediate Representation and Compiler Infrastructure Typed, 3-address code Control-flow graphs with basic blocks Static Single Assignment (SSA) Compiler Infrastructure: Tools, Analysis and Transformation Passes clang frontend that translateds C/C++ into LLVM bit code opt analyze and transform LLVM bitcode llc code generator from LLVM bitcode to native code CS553 Lecture 2 Optimizations and LLVM 3 Figure from The Architecture of Open Source Applications: LLVM by Chris Lattner.

4 LLVM Pass Infrastructure Analyses and Transformations are Passes Dependencies between passes When registered passes indicate which other passes they depend upon by overriding the getanalysisusage method The PassManager figures out the dependency Graph Each Pass returns a boolean True indicates the pass has made a changed to the program. False indicates that no change was made. The PassManager runs until everyone stops. Pass classes to derive from FunctionPass, used in PA1 LoopPass, visits loops BasicBlockPass, visits basic blocks some others CS553 Lecture 2 Optimizations and LLVM 4

5 A Low-Level IR: 3-address code We want to do analysis on an Intermediate Representation: 3-address code Linear representation: assignments, labels, (conditional) jumps Typically language-independent and nearly corresponds to machine instructions, difference: no stack code, but (temporary) variables There are named variables (parameters, variables) and temporaries (expressions). We can name the temporaries assuming unbounded number of (symbolic) registers. Example operations Unary / binary op x = op z, x = v op z, t1 = t2 op t3 Address of p = & v Dereference x = *p, *p = x Pass param param t0 Call t1 = call f, 1 (conditinal) Branch goto L1, if t1 goto L1 (Indexed) copy x = y[i], y[i] = x, x = z, t1 = t2 CS553 Lecture 2 Optimizations and LLVM 5

6 LLVM (a typed 3-address code) Example operations Unary / binary op x = op z, x = v op z, t1 = t2 op t3 IN LLVM: %cmp = icmp sgt i32 %a, %b Address of p = & v IN LLVM: does not appear to have this Dereference x = *p, *p = x IN LLVM: %tmp = load i32* %X Pass param param t0 IN LLVM: see function calls Call t1 = call f, 1 IN LLVM: call void %foo(i8 97 signext) (conditinal) Branch goto L1, if t1 goto L1 IN LLVM: br i1 %cmp, label %if.then, label %if.else (Indexed) copy x = y[i], y[i] = x, x = z, t1 = t2 IN LLVM: %1 = getelementptr i32, i32* $a, i32 0, i32 1 CS553 Lecture 2 Optimizations and LLVM 6

7 LLVM Program Organization Program Representation Organization A Module is a compilation unit that contains functions and global variables and constants. A Function represents a function definition and contains basic blocks BasicBlocks contain Instructions Instructions contain operands, each of which is a Value. All of the above are Values except for Modules Control-Flow Graph Each basic block starts with a label Each branch instruction targets those labels llvm-as < t.ll opt -analyze -view-cfg llvm-as < t.ll opt dot-cfg CS553 Lecture 2 Optimizations and LLVM 7

8 C Code for Review Example int main() { int x,y,z; x = 0; y=1, z = 2; while (x<42) { if (x>0) { y++; x++; z++; continue; } y = z*y; } return 0; } CS553 Lecture Introduction to Data-flow Analysis 8

9 Control Flow Graph Review while.cond: %0 = load i32* %x, align 4 %cmp = icmp slt i32 %0, 42 br i1 %cmp, label %while.body, label %while.end if.end:... store i32 %mul, i32* %y, align 4 br label %while.cond while.end: ret i32 0 while.body: %1 = load i32* %x, align 4 %cmp1 = icmp sgt i32 %1, 0 br i1 %cmp1, label %if.then, label %if.end if.then:... br label %while.cond CS553 Lecture Introduction to Data-flow Analysis 9

10 Representing Computations with Iteration Spaces Idea Explicitly represent the iterations of a loop nest Example Iteration Space do i = 1,6 do j = 1,5 A(i,j) = A(i-1,j-1)+1 enddo enddo j Iteration Space i A set of integer tuples that represents the iterations of a loop Can visualize the dependences in an iteration space CS553 Lecture 2 Optimizations and LLVM 10

11 Loops as Polyhedra, Transformations as Affine Functions Original code do i = 1,6 do j = 1,5 A(i,j) = A(i-1,j+1)+1 enddo enddo Loop Skewing Transformation j i Bounds Transformed code do i = 1,6 do j = 1+i,5+i A(i,j -i ) = A(i -1,j -i +1)+1 enddo enddo CS553 Lecture 2 Optimizations and LLVM 11 j i

12 Concepts up to this point Optimal in name only Phase ordering problem Approach for Program Analysis and Transformation Represent code in typed, 3-address code and perform data-flow analysis and transformation. Represent loops with iteration spaces and transform those. Example Program Optimizations Loop-invariant code motion Induction variable elimination Loop unrolling, permutation, and parallelization Intermediate Representations : LLVM Typed, 3-address code Basic blocks and control flow graphs: optimization scope Pass architecture used in compiler infrastructures and LLVM CS553 Lecture 2 Optimizations and LLVM 12

13 Data-flow Analysis Idea Data-flow analysis derives information about the dynamic behavior of a program by only examining the static code Example How many registers do we need for the program on the right? Easy bound: the number of variables used (3) Better answer is found by considering the dynamic requirements of the program 1 a := 0 2 L1: b := a c := c + b 4 a := b * 2 5 if a < 9 goto L1 6 return c CS553 Lecture Introduction to Data-flow Analysis 13

14 Liveness Analysis Definition A variable is live at a particular point in the program if its value at that point will be used in the future (dead, otherwise). \ To compute liveness at a given point, we need to look into the future Motivation: Register Allocation A program contains an unbounded number of variables Must execute on a machine with a bounded number of registers Two variables can use the same register if they are never in use at the same time (i.e, never simultaneously live). \ Register allocation uses liveness information CS553 Lecture Introduction to Data-flow Analysis 14

15 Control Flow Graphs (CFGs) Definition A CFG is a graph whose nodes represent program statements and whose directed edges represent control flow Example 1 a := 0 2 L1: b := a c := c + b 4 a := b * 2 5 if a < 9 goto L1 6 return c a = 0 b = a + 1 c = c + b 4 a = b * 2 5 a<9 6 return c No Yes CS553 Lecture Introduction to Data-flow Analysis 15

16 Terminology Flow Graph Terms A CFG node has out-edges that lead to successor nodes and in-edges that come from predecessor nodes pred[n] is the set of all predecessors of node n succ[n] is the set of all successors of node n 1 a = 0 Examples Out-edges of node 5: succ[5] = {2,6} pred[5] = {4} pred[2] = {1,5} (5 6) and (5 2) b = a + 1 c = c + b 4 a = b * 2 a<9 6 return c No Yes CS553 Lecture Introduction to Data-flow Analysis 16

17 Liveness by Example What is the live range of b? Variable b is read in statement 4, so b is live on the (3 4) edge Since statement 3 does not assign into b, b is also live on the (2 3) edge Statement 2 assigns b, so any value of b on the (1 2) and (5 2) edges are not needed, so b is dead along these edges a = 0 b = a + 1 c = c + b 4 a = b * 2 5 a<9 b s live range is (2 3 4) 6 return c No Yes CS553 Lecture Introduction to Data-flow Analysis 17

18 Liveness by Example (cont) Live range of a a is live from (1 2) and again from (4 5 2) a is dead from (2 3 4) 1 2 a = 0 b = a + 1 Live range of b b is live from (2 3 4) Live range of c c is live from (entry , 5 6) 6 return c 3 5 c = c + b 4 a = b * 2 a<9 Variables a and b are never simultaneously live, so they can share a register No Yes CS553 Lecture Introduction to Data-flow Analysis 18

19 Uses and Defs Def (or definition) An assignment of a value to a variable def_node[v] = set of CFG nodes that define variable v def[n] = set of variables that are defined at node n a = 0 Use A read of a variable s value use_node[v] = set of CFG nodes that use variable v use[n] = set of variables that are used at node n a < 9? v live Ï def_node[v] More precise definition of liveness A variable v is live on a CFG edge if Î use_node[v] (1) $ a directed path from that edge to a use of v (node in use_node[v]), and (2) that path does not go through any def of v (no nodes in def_node[v]) CS553 Lecture Introduction to Data-flow Analysis 19

20 The Flow of Liveness Data-flow Liveness of variables is a property that flows through the edges of the CFG Direction of Flow Liveness flows backwards through the CFG, because the behavior at future nodes determines liveness at a given node Consider a Consider b Later, we ll see other properties that flow forward 1 a := b := a + 1 c := c + b a := b * 2 No 6 return c a < 9? Yes CS553 Lecture Introduction to Data-flow Analysis 20

21 Liveness at Nodes We have liveness on edges How do we talk about liveness at nodes? a = 0 edges program points just before computation just after computation Two More Definitions A variable is live-out at a node if it is live on any of that node s out-edges n live-out out-edges A variable is live-in at a node if it is live on any of that node s in-edges n live-in in-edges CS553 Lecture Introduction to Data-flow Analysis 21

22 Computing Liveness Rules for computing liveness (1) Generate liveness: If a variable is in use[n], it is live-in at node n n live-in use (2) Push liveness across edges: If a variable is live-in at a node n then it is live-out at all nodes in pred[n] (3) Push liveness across nodes: If a variable is live-out at node n and not in def[n] then the variable is also live-in at n Data-flow equations (1) in[n] = use[n] È (out[n] def[n]) (3) out[n] = È in[s] (2) s Î succ[n] live-out live-out n live-in live-out live-in n live-out pred[n] CS553 Lecture Introduction to Data-flow Analysis 22

23 Solving the Data-flow Equations Algorithm for each node n in CFG in[n] = Æ; out[n] = Æ repeat for each node n in CFG in [n] = in[n] out [n] = out[n] in[n] = use[n] È (out[n] def[n]) out[n] = È in[s] until in [n]=in[n] and out [n]=out[n] for all n s Î succ[n] initialize solutions save current results solve data-flow equations test for convergence This is iterative data-flow analysis (for liveness analysis) CS553 Lecture Introduction to Data-flow Analysis 23

24 Example node # 1 a 2 a b 3 bc c 5 a 1st 2nd 3rd 4th 5th 6th 7th use def in out in out in out in out in out in out in out 4 b a 6 c a bc b a a c c a a bc bc b b a a ac c a ac bc bc b b a ac ac c ac ac bc bc b b ac ac ac c ac ac bc bc b bc ac ac ac c c c ac ac bc bc bc bc ac ac ac c c ac ac bc bc bc bc ac ac ac 1 a := 0 2 b := a c := c + b 4 a := b * 2 Data-flow Equations for Liveness in[n] = use[n] È (out[n] def[n]) out[n] = È in[s] s Î succ[n] No 6 return c 5 a < 9? Yes CS553 Lecture Introduction to Data-flow Analysis 24

25 Do Liveness Example from Old Midterm CS553 Lecture Introduction to Data-flow Analysis 25

26 Concepts Intermediate Representations : LLVM Typed, 3-address code Basic blocks and control flow graphs: optimization scope Pass architecture used in compiler infrastructures and LLVM Liveness Used in register allocation Generating liveness Flow and direction Data-flow equations and analysis Control flow graph terminology CS553 Lecture 2 Optimizations and LLVM 26

27 Next Time Reading Assignments For Monday, DataFlow Analysis Notes by Susan Horwitz Wednesday reading assignment will be posted soon Homework Set up demo for this week if haven t already. CS553 Lecture 2 Optimizations and LLVM 27

Data Flow Analysis. Suman Jana. Adopted From U Penn CIS 570: Modern Programming Language Implementa=on (Autumn 2006)

Data Flow Analysis. Suman Jana. Adopted From U Penn CIS 570: Modern Programming Language Implementa=on (Autumn 2006) Data Flow Analysis Suman Jana Adopted From U Penn CIS 570: Modern Programming Language Implementa=on (Autumn 2006) Data flow analysis Derives informa=on about the dynamic behavior of a program by only

More information

An example of optimization in LLVM. Compiler construction Step 1: Naive translation to LLVM. Step 2: Translating to SSA form (opt -mem2reg)

An example of optimization in LLVM. Compiler construction Step 1: Naive translation to LLVM. Step 2: Translating to SSA form (opt -mem2reg) Compiler construction 2014 An example of optimization in LLVM Lecture 8 More on code optimization SSA form Constant propagation Common subexpression elimination Loop optimizations int f () { int i, j,

More information

LLVM Tutorial. John Criswell University of Rochester

LLVM Tutorial. John Criswell University of Rochester LLVM Tutorial John Criswell University of Rochester 1 Overview 2 History of LLVM Developed by Chris Lattner and Vikram Adve at the University of Illinois at Urbana-Champaign Released open-source in October

More information

Compilation 2013 Liveness Analysis

Compilation 2013 Liveness Analysis Compilation 2013 Liveness Analysis Erik Ernst Aarhus University For good complexity management, we have used an unbounded number of registers (temporaries) To save resources, many of them can be merged

More information

Register allocation. Register allocation: ffl have value in a register when used. ffl limited resources. ffl changes instruction choices

Register allocation. Register allocation: ffl have value in a register when used. ffl limited resources. ffl changes instruction choices Register allocation IR instruction selection register allocation machine code errors Register allocation: have value in a register when used limited resources changes instruction choices can move loads

More information

Introduction to LLVM compiler framework

Introduction to LLVM compiler framework Introduction to LLVM compiler framework Michele Scandale Politecnico di Milano April 8, 2015 This material is strongly based on Ettore Speziale s material for the previous year course. Michele Scandale

More information

Lecture 21 CIS 341: COMPILERS

Lecture 21 CIS 341: COMPILERS Lecture 21 CIS 341: COMPILERS Announcements HW6: Analysis & Optimizations Alias analysis, constant propagation, dead code elimination, register allocation Available Soon Due: Wednesday, April 25 th Zdancewic

More information

ECE 5775 (Fall 17) High-Level Digital Design Automation. Static Single Assignment

ECE 5775 (Fall 17) High-Level Digital Design Automation. Static Single Assignment ECE 5775 (Fall 17) High-Level Digital Design Automation Static Single Assignment Announcements HW 1 released (due Friday) Student-led discussions on Tuesday 9/26 Sign up on Piazza: 3 students / group Meet

More information

4/1/15 LLVM AND SSA. Low-Level Virtual Machine (LLVM) LLVM Compiler Infrastructure. LL: A Subset of LLVM. Basic Blocks

4/1/15 LLVM AND SSA. Low-Level Virtual Machine (LLVM) LLVM Compiler Infrastructure. LL: A Subset of LLVM. Basic Blocks 4//5 Low-Level Virtual Machine (LLVM) LLVM AND SSA Slides adapted from those prepared by Steve Zdancewic at Penn Open-Source Compiler Infrastructure see llvm.org for full documntation Created by Chris

More information

Lecture 2 Overview of the LLVM Compiler

Lecture 2 Overview of the LLVM Compiler Lecture 2 Overview of the LLVM Compiler Abhilasha Jain Thanks to: VikramAdve, Jonathan Burket, DebyKatz, David Koes, Chris Lattner, Gennady Pekhimenko, and Olatunji Ruwase, for their slides The LLVM Compiler

More information

15-411: LLVM. Jan Hoffmann. Substantial portions courtesy of Deby Katz

15-411: LLVM. Jan Hoffmann. Substantial portions courtesy of Deby Katz 15-411: LLVM Jan Hoffmann Substantial portions courtesy of Deby Katz and Gennady Pekhimenko, Olatunji Ruwase,Chris Lattner, Vikram Adve, and David Koes Carnegie What is LLVM? A collection of modular and

More information

Lecture 6 More on the LLVM Compiler

Lecture 6 More on the LLVM Compiler Lecture 6 More on the LLVM Compiler Jonathan Burket Special thanks to Deby Katz, Luke Zarko, and Gabe Weisz for their slides Visualizing the LLVM Compiler System C C++ Java Source Code Clang (Front End)

More information

Introduction to LLVM compiler framework

Introduction to LLVM compiler framework Introduction to LLVM compiler framework Stefano Cherubin Politecnico di Milano 12-04-2017 This material is strongly based on material produced by Michele Scandale and Ettore Speziale for the course `Code

More information

INTRODUCTION TO LLVM Bo Wang SA 2016 Fall

INTRODUCTION TO LLVM Bo Wang SA 2016 Fall INTRODUCTION TO LLVM Bo Wang SA 2016 Fall LLVM Basic LLVM IR LLVM Pass OUTLINE What is LLVM? LLVM is a compiler infrastructure designed as a set of reusable libraries with well-defined interfaces. Implemented

More information

A Brief Introduction to Using LLVM. Nick Sumner

A Brief Introduction to Using LLVM. Nick Sumner A Brief Introduction to Using LLVM Nick Sumner What is LLVM? A compiler? (clang) What is LLVM? A compiler? (clang) A set of formats, libraries, and tools. What is LLVM? A compiler? (clang) A set of formats,

More information

Lecture 20 CIS 341: COMPILERS

Lecture 20 CIS 341: COMPILERS Lecture 20 CIS 341: COMPILERS Announcements HW5: OAT v. 2.0 records, function pointers, type checking, array-bounds checks, etc. Due: TOMORROW Wednesday, April 11 th Zdancewic CIS 341: Compilers 2 A high-level

More information

Lecture Compiler Middle-End

Lecture Compiler Middle-End Lecture 16-18 18 Compiler Middle-End Jianwen Zhu Electrical and Computer Engineering University of Toronto Jianwen Zhu 2009 - P. 1 What We Have Done A lot! Compiler Frontend Defining language Generating

More information

Lecture 4 Overview of the LLVM Compiler

Lecture 4 Overview of the LLVM Compiler Lecture 4 Overview of the LLVM Compiler Pratik Fegade Thanks to: Vikram Adve, Jonathan Burket, Deby Katz, David Koes, Chris Lattner, Gennady Pekhimenko, and Olatunji Ruwase, for their slides Visualizing

More information

Lecture 3 Overview of the LLVM Compiler

Lecture 3 Overview of the LLVM Compiler LLVM Compiler System Lecture 3 Overview of the LLVM Compiler The LLVM Compiler Infrastructure - Provides reusable components for building compilers - Reduce the time/cost to build a new compiler - Build

More information

Lecture 3 Overview of the LLVM Compiler

Lecture 3 Overview of the LLVM Compiler Lecture 3 Overview of the LLVM Compiler Jonathan Burket Special thanks to Deby Katz, Gennady Pekhimenko, Olatunji Ruwase, Chris Lattner, Vikram Adve, and David Koes for their slides The LLVM Compiler Infrastructure

More information

Lecture 23 CIS 341: COMPILERS

Lecture 23 CIS 341: COMPILERS Lecture 23 CIS 341: COMPILERS Announcements HW6: Analysis & Optimizations Alias analysis, constant propagation, dead code elimination, register allocation Due: Wednesday, April 25 th Zdancewic CIS 341:

More information

Lecture 4 More on the LLVM Compiler

Lecture 4 More on the LLVM Compiler Lecture 4 More on the LLVM Compiler Abhilasha Jain Thanks to: Jonathan Burket, Deby Katz, Gabe Weisz, Luke Zarko, and Dominic Chen for their slides Visualizing the LLVM Compiler System C C++ Java Source

More information

Register allocation. CS Compiler Design. Liveness analysis. Register allocation. Liveness analysis and Register allocation. V.

Register allocation. CS Compiler Design. Liveness analysis. Register allocation. Liveness analysis and Register allocation. V. Register allocation CS3300 - Compiler Design Liveness analysis and Register allocation V. Krishna Nandivada IIT Madras Copyright c 2014 by Antony L. Hosking. Permission to make digital or hard copies of

More information

LLVM and IR Construction

LLVM and IR Construction LLVM and IR Construction Fabian Ritter based on slides by Christoph Mallon and Johannes Doerfert http://compilers.cs.uni-saarland.de Compiler Design Lab Saarland University 1 Project Progress source code

More information

Advanced Compiler Construction

Advanced Compiler Construction CS 526 Advanced Compiler Construction http://misailo.cs.illinois.edu/courses/cs526 Goals of the Course Develop a fundamental understanding of the major approaches to program analysis and optimization Understand

More information

Targeting LLVM IR. LLVM IR, code emission, assignment 4

Targeting LLVM IR. LLVM IR, code emission, assignment 4 Targeting LLVM IR LLVM IR, code emission, assignment 4 LLVM Overview Common set of tools & optimizations for compiling many languages to many architectures (x86, ARM, PPC, ASM.js). Integrates AOT & JIT

More information

CIS 341 Final Examination 4 May 2017

CIS 341 Final Examination 4 May 2017 CIS 341 Final Examination 4 May 2017 1 /14 2 /15 3 /12 4 /14 5 /34 6 /21 7 /10 Total /120 Do not begin the exam until you are told to do so. You have 120 minutes to complete the exam. There are 14 pages

More information

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

We can express this in dataflow equations using gen and kill sets, where the sets are now sets of expressions. Available expressions Suppose we want to do common-subexpression elimination; that is, given a program that computes x y more than once, can we eliminate one of the duplicate computations? To find places

More information

Liveness Analysis and Register Allocation

Liveness Analysis and Register Allocation Liveness Analysis and Register Allocation Leonidas Fegaras CSE 5317/4305 L10: Liveness Analysis and Register Allocation 1 Liveness Analysis So far we assumed that we have a very large number of temporary

More information

Visualizing code structure in LLVM

Visualizing code structure in LLVM Institute of Computational Science Visualizing code structure in LLVM Dmitry Mikushin dmitry.mikushin@usi.ch. December 5, 2013 Dmitry Mikushin Visualizing code structure in LLVM December 5, 2013 1 / 14

More information

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

We can express this in dataflow equations using gen and kill sets, where the sets are now sets of expressions. Available expressions Suppose we want to do common-subexpression elimination; that is, given a program that computes x y more than once, can we eliminate one of the duplicate computations? To find places

More information

Introduction to LLVM. UG3 Compiling Techniques Autumn 2018

Introduction to LLVM. UG3 Compiling Techniques Autumn 2018 Introduction to LLVM UG3 Compiling Techniques Autumn 2018 Contact Information Instructor: Aaron Smith Email: aaron.l.smith@ed.ac.uk Office: IF 1.29 TA for LLVM: Andrej Ivanis Email: andrej.ivanis@ed.ac.uk

More information

Loop Transformations, Dependences, and Parallelization

Loop Transformations, Dependences, and Parallelization Loop Transformations, Dependences, and Parallelization Announcements HW3 is due Wednesday February 15th Today HW3 intro Unimodular framework rehash with edits Skewing Smith-Waterman (the fix is in!), composing

More information

Calvin Lin The University of Texas at Austin

Calvin Lin The University of Texas at Austin Loop Invariant Code Motion Last Time Loop invariant code motion Value numbering Today Finish value numbering More reuse optimization Common subession elimination Partial redundancy elimination Next Time

More information

Compiler Design. Fall Data-Flow Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Compiler Design. Fall Data-Flow Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz Compiler Design Fall 2015 Data-Flow Analysis Sample Exercises and Solutions Prof. Pedro C. Diniz USC / Information Sciences Institute 4676 Admiralty Way, Suite 1001 Marina del Rey, California 90292 pedro@isi.edu

More information

An Overview of GCC Architecture (source: wikipedia) Control-Flow Analysis and Loop Detection

An Overview of GCC Architecture (source: wikipedia) Control-Flow Analysis and Loop Detection An Overview of GCC Architecture (source: wikipedia) CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA Control-Flow Analysis and Loop Detection Last time Lattice-theoretic framework for data-flow

More information

Induction Variable Identification (cont)

Induction Variable Identification (cont) Loop Invariant Code Motion Last Time Uses of SSA: reaching constants, dead-code elimination, induction variable identification Today Finish up induction variable identification Loop invariant code motion

More information

More Dataflow Analysis

More Dataflow Analysis More Dataflow Analysis Steps to building analysis Step 1: Choose lattice Step 2: Choose direction of dataflow (forward or backward) Step 3: Create transfer function Step 4: Choose confluence operator (i.e.,

More information

Compiler Construction: LLVMlite

Compiler Construction: LLVMlite Compiler Construction: LLVMlite Direct compilation Expressions X86lite Input Output Compile directly from expression language to x86 Syntax-directed compilation scheme Special cases can improve generated

More information

Compiler Optimization and Code Generation

Compiler Optimization and Code Generation Compiler Optimization and Code Generation Professor: Sc.D., Professor Vazgen Melikyan 1 Course Overview Introduction: Overview of Optimizations 1 lecture Intermediate-Code Generation 2 lectures Machine-Independent

More information

Intermediate representation

Intermediate representation Intermediate representation Goals: encode knowledge about the program facilitate analysis facilitate retargeting facilitate optimization scanning parsing HIR semantic analysis HIR intermediate code gen.

More information

Compiler Theory. (Intermediate Code Generation Abstract S yntax + 3 Address Code)

Compiler Theory. (Intermediate Code Generation Abstract S yntax + 3 Address Code) Compiler Theory (Intermediate Code Generation Abstract S yntax + 3 Address Code) 006 Why intermediate code? Details of the source language are confined to the frontend (analysis phase) of a compiler, while

More information

CS 426 Fall Machine Problem 4. Machine Problem 4. CS 426 Compiler Construction Fall Semester 2017

CS 426 Fall Machine Problem 4. Machine Problem 4. CS 426 Compiler Construction Fall Semester 2017 CS 426 Fall 2017 1 Machine Problem 4 Machine Problem 4 CS 426 Compiler Construction Fall Semester 2017 Handed out: November 16, 2017. Due: December 7, 2017, 5:00 p.m. This assignment deals with implementing

More information

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

Data Flow Analysis. Agenda CS738: Advanced Compiler Optimizations. 3-address Code Format. Assumptions Agenda CS738: Advanced Compiler Optimizations Data Flow Analysis Amey Karkare karkare@cse.iitk.ac.in http://www.cse.iitk.ac.in/~karkare/cs738 Department of CSE, IIT Kanpur Static analysis and compile-time

More information

Comp 204: Computer Systems and Their Implementation. Lecture 22: Code Generation and Optimisation

Comp 204: Computer Systems and Their Implementation. Lecture 22: Code Generation and Optimisation Comp 204: Computer Systems and Their Implementation Lecture 22: Code Generation and Optimisation 1 Today Code generation Three address code Code optimisation Techniques Classification of optimisations

More information

CS553 Lecture Generalizing Data-flow Analysis 3

CS553 Lecture Generalizing Data-flow Analysis 3 Generalizing Data-flow Analysis Announcements Project 2 writeup is available Read Stephenson paper Last Time Control-flow analysis Today C-Breeze Introduction Other types of data-flow analysis Reaching

More information

Programming Language Processor Theory

Programming Language Processor Theory Programming Language Processor Theory Munehiro Takimoto Course Descriptions Method of Evaluation: made through your technical reports Purposes: understanding various theories and implementations of modern

More information

Control flow and loop detection. TDT4205 Lecture 29

Control flow and loop detection. TDT4205 Lecture 29 1 Control flow and loop detection TDT4205 Lecture 29 2 Where we are We have a handful of different analysis instances None of them are optimizations, in and of themselves The objective now is to Show how

More information

Using Static Single Assignment Form

Using Static Single Assignment Form Using Static Single Assignment Form Announcements Project 2 schedule due today HW1 due Friday Last Time SSA Technicalities Today Constant propagation Loop invariant code motion Induction variables CS553

More information

Lecture 3 Local Optimizations, Intro to SSA

Lecture 3 Local Optimizations, Intro to SSA Lecture 3 Local Optimizations, Intro to SSA I. Basic blocks & Flow graphs II. Abstraction 1: DAG III. Abstraction 2: Value numbering IV. Intro to SSA ALSU 8.4-8.5, 6.2.4 Phillip B. Gibbons 15-745: Local

More information

Lecture 4 Introduction to Data Flow Analysis

Lecture 4 Introduction to Data Flow Analysis Lecture 4 Introduction to Data Flow Analysis I. Structure of data flow analysis II. Example 1: Reaching definition analysis III. Example 2: Liveness analysis IV. Generalization What is Data Flow Analysis?

More information

CS577 Modern Language Processors. Spring 2018 Lecture Optimization

CS577 Modern Language Processors. Spring 2018 Lecture Optimization CS577 Modern Language Processors Spring 2018 Lecture Optimization 1 GENERATING BETTER CODE What does a conventional compiler do to improve quality of generated code? Eliminate redundant computation Move

More information

Compiler Design. Fall Control-Flow Analysis. Prof. Pedro C. Diniz

Compiler Design. Fall Control-Flow Analysis. Prof. Pedro C. Diniz Compiler Design Fall 2015 Control-Flow Analysis Sample Exercises and Solutions Prof. Pedro C. Diniz USC / Information Sciences Institute 4676 Admiralty Way, Suite 1001 Marina del Rey, California 90292

More information

Tour of common optimizations

Tour of common optimizations Tour of common optimizations Simple example foo(z) { x := 3 + 6; y := x 5 return z * y } Simple example foo(z) { x := 3 + 6; y := x 5; return z * y } x:=9; Applying Constant Folding Simple example foo(z)

More information

Tiling: A Data Locality Optimizing Algorithm

Tiling: A Data Locality Optimizing Algorithm Tiling: A Data Locality Optimizing Algorithm Announcements Monday November 28th, Dr. Sanjay Rajopadhye is talking at BMAC Friday December 2nd, Dr. Sanjay Rajopadhye will be leading CS553 Last Monday Kelly

More information

CIS 341 Final Examination 30 April 2013

CIS 341 Final Examination 30 April 2013 CIS 341 Final Examination 30 April 2013 1 /40 2 /30 3 /30 4 /20 Total /120 Do not begin the exam until you are told to do so. You have 120 minutes to complete the exam. There are 12 pages in this exam.

More information

Data-flow Analysis. Y.N. Srikant. Department of Computer Science and Automation Indian Institute of Science Bangalore

Data-flow Analysis. Y.N. Srikant. Department of Computer Science and Automation Indian Institute of Science Bangalore Department of Computer Science and Automation Indian Institute of Science Bangalore 560 012 NPTEL Course on Compiler Design Data-flow analysis These are techniques that derive information about the flow

More information

Universidade Federal de Minas Gerais Department of Computer Science Programming Languages Laboratory WRITING AN LLVM PASS DCC 888

Universidade Federal de Minas Gerais Department of Computer Science Programming Languages Laboratory WRITING AN LLVM PASS DCC 888 Universidade Federal de Minas Gerais Department of Computer Science Programming Languages Laboratory WRITING AN LLVM PASS DCC 888 Passes LLVM applies a chain of analyses and transformaaons on the target

More information

CIS 341 Final Examination 3 May 2011

CIS 341 Final Examination 3 May 2011 CIS 341 Final Examination 3 May 2011 1 /16 2 /20 3 /40 4 /28 5 /16 Total /120 Do not begin the exam until you are told to do so. You have 120 minutes to complete the exam. There are 12 pages in this exam.

More information

ECE 5775 High-Level Digital Design Automation Fall More CFG Static Single Assignment

ECE 5775 High-Level Digital Design Automation Fall More CFG Static Single Assignment ECE 5775 High-Level Digital Design Automation Fall 2018 More CFG Static Single Assignment Announcements HW 1 due next Monday (9/17) Lab 2 will be released tonight (due 9/24) Instructor OH cancelled this

More information

LLVM & LLVM Bitcode Introduction

LLVM & LLVM Bitcode Introduction LLVM & LLVM Bitcode Introduction What is LLVM? (1/2) LLVM (Low Level Virtual Machine) is a compiler infrastructure Written by C++ & STL History The LLVM project started in 2000 at the University of Illinois

More information

Code optimization. Have we achieved optimal code? Impossible to answer! We make improvements to the code. Aim: faster code and/or less space

Code optimization. Have we achieved optimal code? Impossible to answer! We make improvements to the code. Aim: faster code and/or less space Code optimization Have we achieved optimal code? Impossible to answer! We make improvements to the code Aim: faster code and/or less space Types of optimization machine-independent In source code or internal

More information

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

Middle End. Code Improvement (or Optimization) Analyzes IR and rewrites (or transforms) IR Primary goal is to reduce running time of the compiled code Traditional Three-pass Compiler Source Code Front End IR Middle End IR Back End Machine code Errors Code Improvement (or Optimization) Analyzes IR and rewrites (or transforms) IR Primary goal is to reduce

More information

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

Compiler Structure. Data Flow Analysis. Control-Flow Graph. Available Expressions. Data Flow Facts Compiler Structure Source Code Abstract Syntax Tree Control Flow Graph Object Code CMSC 631 Program Analysis and Understanding Fall 2003 Data Flow Analysis Source code parsed to produce AST AST transformed

More information

Reuse Optimization. LLVM Compiler Infrastructure. Local Value Numbering. Local Value Numbering (cont)

Reuse Optimization. LLVM Compiler Infrastructure. Local Value Numbering. Local Value Numbering (cont) LLVM Compiler Infrastructure Source: LLVM: A Compilation Framework for Lifelong Program Analysis & Transformation by Lattner and Adve Reuse Optimization Eliminate redundant operations in the dynamic execution

More information

Loop Optimizations. Outline. Loop Invariant Code Motion. Induction Variables. Loop Invariant Code Motion. Loop Invariant Code Motion

Loop Optimizations. Outline. Loop Invariant Code Motion. Induction Variables. Loop Invariant Code Motion. Loop Invariant Code Motion Outline Loop Optimizations Induction Variables Recognition Induction Variables Combination of Analyses Copyright 2010, Pedro C Diniz, all rights reserved Students enrolled in the Compilers class at the

More information

Principles of Compiler Design

Principles of Compiler Design Principles of Compiler Design Intermediate Representation Compiler Lexical Analysis Syntax Analysis Semantic Analysis Source Program Token stream Abstract Syntax tree Unambiguous Program representation

More information

CS553 Lecture Introduction to Data-flow Analysis 1

CS553 Lecture Introduction to Data-flow Analysis 1 ! Ide Introdution to Dt-flow nlysis!lst Time! Implementing Mrk nd Sweep GC!Tody! Control flow grphs! Liveness nlysis! Register llotion CS553 Leture Introdution to Dt-flow Anlysis 1 Dt-flow Anlysis! Dt-flow

More information

Compiler Construction 2016/2017 Loop Optimizations

Compiler Construction 2016/2017 Loop Optimizations Compiler Construction 2016/2017 Loop Optimizations Peter Thiemann January 16, 2017 Outline 1 Loops 2 Dominators 3 Loop-Invariant Computations 4 Induction Variables 5 Array-Bounds Checks 6 Loop Unrolling

More information

Gil Rapaport and Ayal Zaks. Intel Corporation, Israel Development Center. March 27-28, 2017 European LLVM Developers Meeting

Gil Rapaport and Ayal Zaks. Intel Corporation, Israel Development Center. March 27-28, 2017 European LLVM Developers Meeting Gil Rapaport and Ayal Zaks Intel Corporation, Israel Development Center March 27-28, 2017 European LLVM Developers Meeting Saarland Informatics Campus, Saarbrücken, Germany Legal Disclaimer & INFORMATION

More information

The LLVM Compiler Framework and Infrastructure

The LLVM Compiler Framework and Infrastructure The LLVM Compiler Framework and Infrastructure Program Analysis Original Slides by David Koes (CMU) Substantial portions courtesy Chris Lattner and Vikram Adve LLVM Compiler System The LLVM Compiler Infrastructure

More information

Lecture 25 CIS 341: COMPILERS

Lecture 25 CIS 341: COMPILERS Lecture 25 CIS 341: COMPILERS Announcements HW6: Dataflow Analysis Due: Weds. April 26 th NOTE: See Piazza for an update TLDR: "simple" regalloc should not suffice. Change gradedtests.ml >= to > FINAL

More information

CS 406/534 Compiler Construction Putting It All Together

CS 406/534 Compiler Construction Putting It All Together CS 406/534 Compiler Construction Putting It All Together Prof. Li Xu Dept. of Computer Science UMass Lowell Fall 2004 Part of the course lecture notes are based on Prof. Keith Cooper, Prof. Ken Kennedy

More information

CONTROL FLOW ANALYSIS. The slides adapted from Vikram Adve

CONTROL FLOW ANALYSIS. The slides adapted from Vikram Adve CONTROL FLOW ANALYSIS The slides adapted from Vikram Adve Flow Graphs Flow Graph: A triple G=(N,A,s), where (N,A) is a (finite) directed graph, s N is a designated initial node, and there is a path from

More information

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13 Run-time Environments Lecture 13 by Prof. Vijay Ganesh) Lecture 13 1 What have we covered so far? We have covered the front-end phases Lexical analysis (Lexer, regular expressions,...) Parsing (CFG, Top-down,

More information

Languages and Compiler Design II IR Code Optimization

Languages and Compiler Design II IR Code Optimization Languages and Compiler Design II IR Code Optimization Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring 2010 rev.: 4/16/2010 PSU CS322 HM 1 Agenda IR Optimization

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation In the analysis-synthesis model of a compiler, the front end analyzes a source program and creates an intermediate representation, from which the back end generates target

More information

CS202 Compiler Construction

CS202 Compiler Construction S202 ompiler onstruction pril 15, 2003 S 202-32 1 ssignment 11 (last programming assignment) Loop optimizations S 202-32 today 2 ssignment 11 Final (necessary) step in compilation! Implement register allocation

More information

Announcements. Class 7: Intro to SRC Simulator Procedure Calls HLL -> Assembly. Agenda. SRC Procedure Calls. SRC Memory Layout. High Level Program

Announcements. Class 7: Intro to SRC Simulator Procedure Calls HLL -> Assembly. Agenda. SRC Procedure Calls. SRC Memory Layout. High Level Program Fall 2006 CS333: Computer Architecture University of Virginia Computer Science Michele Co Announcements Class 7: Intro to SRC Simulator Procedure Calls HLL -> Assembly Homework #2 Due next Wednesday, Sept.

More information

A Framework for Automatic OpenMP Code Generation

A Framework for Automatic OpenMP Code Generation 1/31 A Framework for Automatic OpenMP Code Generation Raghesh A (CS09M032) Guide: Dr. Shankar Balachandran May 2nd, 2011 Outline 2/31 The Framework An Example Necessary Background Polyhedral Model SCoP

More information

CMPT 379 Compilers. Anoop Sarkar. 11/13/07 1. TAC: Intermediate Representation. Language + Machine Independent TAC

CMPT 379 Compilers. Anoop Sarkar.  11/13/07 1. TAC: Intermediate Representation. Language + Machine Independent TAC CMPT 379 Compilers Anoop Sarkar http://www.cs.sfu.ca/~anoop 11/13/07 1 TAC: Intermediate Representation Language Specific Language + Machine Independent Machine Dependent Front End AST Intermediate Code

More information

Compiler Construction 2010/2011 Loop Optimizations

Compiler Construction 2010/2011 Loop Optimizations Compiler Construction 2010/2011 Loop Optimizations Peter Thiemann January 25, 2011 Outline 1 Loop Optimizations 2 Dominators 3 Loop-Invariant Computations 4 Induction Variables 5 Array-Bounds Checks 6

More information

Simone Campanoni Loop transformations

Simone Campanoni Loop transformations Simone Campanoni simonec@eecs.northwestern.edu Loop transformations Outline Simple loop transformations Loop invariants Induction variables Complex loop transformations Simple loop transformations Simple

More information

Languages and Compiler Design II IR Code Generation I

Languages and Compiler Design II IR Code Generation I Languages and Compiler Design II IR Code Generation I Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring 2010 rev.: 4/16/2010 PSU CS322 HM 1 Agenda Grammar G1

More information

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

Compiler Passes. Optimization. The Role of the Optimizer. Optimizations. The Optimizer (or Middle End) Traditional Three-pass Compiler Compiler Passes Analysis of input program (front-end) character stream Lexical Analysis Synthesis of output program (back-end) Intermediate Code Generation Optimization Before and after generating machine

More information

Loops. Lather, Rinse, Repeat. CS4410: Spring 2013

Loops. Lather, Rinse, Repeat. CS4410: Spring 2013 Loops or Lather, Rinse, Repeat CS4410: Spring 2013 Program Loops Reading: Appel Ch. 18 Loop = a computation repeatedly executed until a terminating condition is reached High-level loop constructs: While

More information

Lecture 7 CIS 341: COMPILERS

Lecture 7 CIS 341: COMPILERS Lecture 7 CIS 341: COMPILERS Announcements HW2: X86lite Available on the course web pages. Due: TOMORROW at 11:59:59pm NOTE: submission server was broken last night/this morning It should now support Ocaml

More information

Homework Assignment #1 Sample Solutions

Homework Assignment #1 Sample Solutions Homework Assignment # Sample Solutions Due Monday /, at the start of lecture. For the following control flow graph, for the purposes of a forward data flow analysis (such as available expressions), show

More information

CS553 Lecture Dynamic Optimizations 2

CS553 Lecture Dynamic Optimizations 2 Dynamic Optimizations Last time Predication and speculation Today Dynamic compilation CS553 Lecture Dynamic Optimizations 2 Motivation Limitations of static analysis Programs can have values and invariants

More information

Compiler Optimisation

Compiler Optimisation Compiler Optimisation 4 Dataflow Analysis Hugh Leather IF 1.18a hleather@inf.ed.ac.uk Institute for Computing Systems Architecture School of Informatics University of Edinburgh 2018 Introduction This lecture:

More information

Topic I (d): Static Single Assignment Form (SSA)

Topic I (d): Static Single Assignment Form (SSA) Topic I (d): Static Single Assignment Form (SSA) 621-10F/Topic-1d-SSA 1 Reading List Slides: Topic Ix Other readings as assigned in class 621-10F/Topic-1d-SSA 2 ABET Outcome Ability to apply knowledge

More information

Midterm 2. CMSC 430 Introduction to Compilers Fall Instructions Total 100. Name: November 21, 2016

Midterm 2. CMSC 430 Introduction to Compilers Fall Instructions Total 100. Name: November 21, 2016 Name: Midterm 2 CMSC 430 Introduction to Compilers Fall 2016 November 21, 2016 Instructions This exam contains 7 pages, including this one. Make sure you have all the pages. Write your name on the top

More information

6. Intermediate Representation

6. Intermediate Representation 6. Intermediate Representation Oscar Nierstrasz Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes. http://www.cs.ucla.edu/~palsberg/

More information

A main goal is to achieve a better performance. Code Optimization. Chapter 9

A main goal is to achieve a better performance. Code Optimization. Chapter 9 1 A main goal is to achieve a better performance Code Optimization Chapter 9 2 A main goal is to achieve a better performance source Code Front End Intermediate Code Code Gen target Code user Machineindependent

More information

Data Flow Analysis. CSCE Lecture 9-02/15/2018

Data Flow Analysis. CSCE Lecture 9-02/15/2018 Data Flow Analysis CSCE 747 - Lecture 9-02/15/2018 Data Flow Another view - program statements compute and transform data So, look at how that data is passed through the program. Reason about data dependence

More information

Loop Invariant Code Motion. Background: ud- and du-chains. Upward Exposed Uses. Identifying Loop Invariant Code. Last Time Control flow analysis

Loop Invariant Code Motion. Background: ud- and du-chains. Upward Exposed Uses. Identifying Loop Invariant Code. Last Time Control flow analysis Loop Invariant Code Motion Loop Invariant Code Motion Background: ud- and du-chains Last Time Control flow analysis Today Loop invariant code motion ud-chains A ud-chain connects a use of a variable to

More information

Intermediate Representations & Symbol Tables

Intermediate Representations & Symbol Tables Intermediate Representations & Symbol Tables Copyright 2014, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at the University of Southern California have explicit permission

More information

CS202 Compiler Construction

CS202 Compiler Construction CS202 Compiler Construction April 17, 2003 CS 202-33 1 Today: more optimizations Loop optimizations: induction variables New DF analysis: available expressions Common subexpression elimination Copy propogation

More information

Dependence and Data Flow Models. (c) 2007 Mauro Pezzè & Michal Young Ch 6, slide 1

Dependence and Data Flow Models. (c) 2007 Mauro Pezzè & Michal Young Ch 6, slide 1 Dependence and Data Flow Models (c) 2007 Mauro Pezzè & Michal Young Ch 6, slide 1 Why Data Flow Models? Models from Chapter 5 emphasized control Control flow graph, call graph, finite state machines We

More information

Baggy bounds with LLVM

Baggy bounds with LLVM Baggy bounds with LLVM Anton Anastasov Chirantan Ekbote Travis Hance 6.858 Project Final Report 1 Introduction Buffer overflows are a well-known security problem; a simple buffer-overflow bug can often

More information