Simone Campanoni simonec@eecs.northwestern.edu CFA Simone Campanoni simonec@eecs.northwestern.edu.

Slides:



Advertisements
Similar presentations
SSA and CPS CS153: Compilers Greg Morrisett. Monadic Form vs CFGs Consider CFG available exp. analysis: statement gen's kill's x:=v 1 p v 2 x:=v 1 p v.
Advertisements

Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
8. Static Single Assignment Form Marcus Denker. © Marcus Denker SSA Roadmap  Static Single Assignment Form (SSA)  Converting to SSA Form  Examples.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) SSA Guo, Yao.
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.
Architecture-dependent optimizations Functional units, delay slots and dependency analysis.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
SSA.
CS412/413 Introduction to Compilers Radu Rugina Lecture 37: DU Chains and SSA Form 29 Apr 02.
1 Code Optimization. 2 The Code Optimizer Control flow analysis: control flow graph Data-flow analysis Transformations Front end Code generator Code optimizer.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
6/9/2015© Hal Perkins & UW CSEU-1 CSE P 501 – Compilers SSA Hal Perkins Winter 2008.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
PSUCS322 HM 1 Languages and Compiler Design II Basic Blocks Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
Code Generation Professor Yihjia Tsai Tamkang University.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Advanced Compilers CMPSCI 710 Spring 2003 Computing SSA Emery Berger University.
Data Flow Analysis Compiler Design October 5, 2004 These slides live on the Web. I obtained them from Jeff Foster and he said that he obtained.
CS 412/413 Spring 2007Introduction to Compilers1 Lecture 29: Control Flow Analysis 9 Apr 07 CS412/413 Introduction to Compilers Tim Teitelbaum.
1 Copy Propagation What does it mean? – Given an assignment x = y, replace later uses of x with uses of y, provided there are no intervening assignments.
Intermediate Code. Local Optimizations
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
Software (Program) Analysis. Automated Static Analysis Static analyzers are software tools for source text processing They parse the program text and.
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.
Dataflow Analysis Topic today Data flow analysis: Section 3 of Representation and Analysis Paper (Section 3) NOTE we finished through slide 30 on Friday.
1 Control Flow Analysis Topic today Representation and Analysis Paper (Sections 1, 2) For next class: Read Representation and Analysis Paper (Section 3)
Dead Code Elimination This lecture presents the algorithm Dead from EaC2e, Chapter 10. That algorithm derives, in turn, from Rob Shillner’s unpublished.
CS 614: Theory and Construction of Compilers Lecture 15 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
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.
Control Flow Analysis Compiler Baojian Hua
Cleaning up the CFG Eliminating useless nodes & edges This lecture describes the algorithm Clean, presented in Chapter 10 of EaC2e. The algorithm is due.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
Optimization Simone Campanoni
Loops Simone Campanoni
Code Optimization More Optimization Techniques. More Optimization Techniques  Loop optimization  Code motion  Strength reduction for induction variables.
Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.
Simone Campanoni SSA Simone Campanoni
Simone Campanoni LLVM Simone Campanoni
Basic Program Analysis
Simone Campanoni Dependences Simone Campanoni
CS 201 Compiler Construction
Software Testing and Maintenance 1
Simone Campanoni Loop transformations Simone Campanoni
Static Single Assignment
Efficiently Computing SSA
Princeton University Spring 2016
CS 201 Compiler Construction
Topic 10: Dataflow Analysis
Factored Use-Def Chains and Static Single Assignment Forms
Control Flow Analysis CS 4501 Baishakhi Ray.
Code Optimization Chapter 10
Code Optimization Chapter 9 (1st ed. Ch.10)
Topic 4: Flow Analysis Some slides come from Prof. J. N. Amaral
Code Optimization Overview and Examples Control Flow Graph
Static Single Assignment Form (SSA)
Optimizations using SSA
Control Flow Analysis (Chapter 7)
Interval Partitioning of a Flow Graph
Data Flow Analysis Compiler Design
Static Single Assignment
SSA-based Optimizations
Taken largely from University of Delaware Compiler Notes
Building SSA Harry Xu CS 142 (b) 04/22/2018.
Code Generation Part II
Live variables and copy propagation
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
Presentation transcript:

Simone Campanoni simonec@eecs.northwestern.edu CFA Simone Campanoni simonec@eecs.northwestern.edu

Problems with Canvas. Problems with slides. Problems with H0 Problems with Canvas? Problems with slides? Problems with H0? Any problems?

CFA Outline Why do we need Control Flow Analysis? Basic blocks Control flow graph

Functions and instructions runOnFunction’s job is to analyze/transform a function F … by analyzing/transforming its instructions The order of instructions isn’t the execution one

Storing order ≠ executing order Common pitfall 2: if instruction i1 has been stored before i2, then i2 can execute after i1 int myF (int a){ int x = a + 1; if (a > 5){ x++; } else { x--; } return x; } int x = a + 1 tmp = a > 5 branch_ifnot tmp L1 x++ branch L2 L1: x-- L2: return x int x = a + 1 tmp = a > 5 branch_if tmp L1 x-- branch L2 L1: x++ L2: return x To improve/transform the code, we need to analyze the execution paths This is the job of Control Flow Analysis Common pitfall 1: if instruction i1 has been stored before i2, then i2 is always executed after i1 When the storing order is chosen (compile time), the execution order isn’t known

Optimization example:constant propagation int sumcalc (int a, int b, int N){ int x,y; x = 0; y = 0; for (int i=0; i <= N; i++){ x = x + (a * b); x = x + b*y; } return x; Replace a variable use with a constant while preserving the original semantics of the code

Constant propagation and CFA Find a constant expression Instruction i: varX = CONSTANT_EXPRESSION Replace the use of the variable defined in a constant expression with that constant if All paths to the use of varX passes its assignment I There are no intervening definition of that variable We need to know the “control flow” of the program Control flow: sequence of instructions in a program ignoring data values and arithmetic operations Control Flow Analysis discovers facts about control flows

CFA Outline Why do we need Control Flow Analysis? Basic blocks Control flow graph

Representing the control flow of the program Most instructions Jump instructions Branch instructions

Representing the control flow of the program A graph where nodes are instructions Very large Lot of straight-line connections Can we simplify it? Basic block Sequence of instructions that is always entered at the beginning and exited at the end

Basic blocks A basic block is a maximal sequence of instructions such that Only the first one can be reached from outside this basic block All instructions within are executed consecutively if the first one get executed Only the last instruction can be a branch/jump Only the first instruction can be a label Is the storing sequence = execution order in a basic block?

Basic blocks in compilers Inst = F.entryPoint() B = new BasicBlock() While (Inst){ if Inst is Label { } B.add(Inst) if Inst is Branch/Jump{ Inst = F.nextInst(Inst) Add missing labels Add explicit jumps Delete empty basic blocks Basic blocks in compilers Automatically identified Algorithm: Code changes trigger the re-identification Increase the compilation time Enforced by design Instruction exists only within the context of its basic block To define a function: you define its basic blocks first Then you define the instructions of each basic block What about calls? Program exits Exceptions

Basic blocks in LLVM Every basic block in LLVM must Have a label associated to it Have a “terminator” at the end of it The first basic block of LLVM (entry point) cannot have predecessors LLVM organizes “compiler concepts” in containers A basic block is a container of ordered LLVM instructions A function is a container of basic blocks A module is a container of functions

Basic blocks in LLVM (2) LLVM C++ Class “BasicBlock” Uses: BasicBlock *b = … ; Function *f = b.getParent(); Module *m = b.getModule(); Instruction *i = b.getTerminator(); Instruction *i = b.front(); size_t b.size();

Basic blocks in LLVM in action Bitcode generation Bitcode generation Bitcode generation Bitcode generation Bitcode generation Bitcode generation

CFA Outline Why do we need Control Flow Analysis? Basic blocks Control flow graph

Control Flow Graph (CFG) A CFG is a graph G = <Nodes, Edges> Nodes: Basic blocks Edges: (x,y) ϵ Edges iff first instruction in basic block y might be executed just after the last instruction of the basic block x Predecessor … ... Ix Successor Iy ...

Control Flow Graph (CFG) Entry node: block with the first instruction of the function Exit nodes: blocks with the return instruction Some compilers make a single exit node by adding a special node ret ret

CFG in LLVM Differences? Bitcode generation opt -view-cfg F.viewCFG();

Navigating the CFG in LLVM Successors of a basic block Predecessors of a basic block

Navigating the CFG in LLVM (2)

H0/tests/test1

Sometimes “might” isn’t enough How to differentiate between the two situations by using only successor/predecessor relations? … ... y = 0 y = 3 x = y ...

Dominators Definition: Node d dominates node n in a graph (d dom n) if every path from the start node to n goes through d. Every node dominates itself. What is the relation between basic blocks 1, 2, and 3? What is the relation between basic blocks 1 and 2? What is the relation between instructions within a basic block? 1 2 3

Finding dominators p1 pk n Consider a node n with k predecessors p1, …, pk Observation 1: if d dominates each pi (1<=i<=k), then d dominates n Observation 2: if d dominates n, then it must dominate all pi D[n] = {n} ∪ (∩p∈predecessors(n) D[p]) To compute it: By iteration Initialize each D[n] to include every one

Immediate dominators Dominator tree CFG Immediate dominators Definition: the immediate dominator of a node n is the unique node that strictly dominates n (i.e., it isn’t n) but does not strictly dominate another node that strictly dominates n 1 1 1 Dominator tree 2 2 2 3 3 3 CFG Immediate dominators

Post-dominators Immediate post-dominator tree CFG Assumption: Single exit node in CFG Definition: Node d post-dominates node n in a graph if every path from n to the exit node goes through d B D B: if (par1 > 5) C: varX = par1 + 1 D: print(varX) C C B Immediate post-dominator tree How to compute post-dominators? D CFG

Post-dominators Immediate post-dominator tree CFG Assumption: Single exit node in CFG Definition: Node d post-dominates node n in a graph if every path from n to the exit node goes through d B D B: if (par1 > 5) C: varX = par1 + 1 C2: … D: print(varX) C C2 B C2 C D Immediate post-dominator tree CFG