SlideShare ist ein Scribd-Unternehmen logo
1 von 12
Downloaden Sie, um offline zu lesen
Control-Flow Analysis

Last time
 – Undergraduate compilers in a day

Today
 – Control-flow analysis
     – Building basic blocks
     – Building control-flow graphs
     – Loops




CIS570 Lecture 3               Control-Flow Analysis                2




Context

Data-flow
 – Flow of data values from defs to uses

Control-flow
 – Sequencing of operations
   e.g., Evaluation of then-code and else-code depends on if-test




CIS570 Lecture 3               Control-Flow Analysis                3




                                                                        1
Representing Control-Flow

High-level representation
 – Control flow is implicit in an AST

Low-level representation:
 – Use a Control-flow graph (CFG)
     – Nodes represent statements (low-level linear IR)
     – Edges represent explicit flow of control

Other options
 – Control dependences in program dependence graph (PDG) [Ferrante87]
 – Dependences on explicit state in value dependence graph (VDG) [Weise 94]




CIS570 Lecture 3              Control-Flow Analysis                              4




What Is Control-Flow Analysis?

Control-flow analysis discovers the flow of control within a procedure
(e.g., builds a CFG, identifies loops)
                                                  1 a := 0
Example                                             b := a * b
1         a := 0
                                                          3    c := b/d
2         b := a *    b                                        c < x?
3    L1: c := b/d
4         if c < x    goto L2                                      5    e := b / c
5         e := b /    c                                                 f := e + 1
6         f := e +    1
7    L2: g := f                                       7   g := f
8         h := t -    g                                   h := t - g
9         if e > 0    goto L3                             e > 0?
10   goto L1                                              No             Yes
11   L3: return                         10   goto                  11   return
CIS570 Lecture 3              Control-Flow Analysis                              5




                                                                                     2
Basic Blocks

Definition
 – A basic block is a sequence of straight line code that can be entered only
    at the beginning and exited only at the end


                                g := f
                                h := t - g
                                e > 0?
                                   No   Yes
Building basic blocks
 – Identify leaders
      – The first instruction in a procedure, or
      – The target of any branch, or
      – An instruction immediately following a branch (implicit target)
 – Gobble all subsequent instructions until the next leader


CIS570 Lecture 3                  Control-Flow Analysis                         6




Algorithm for Building Basic Blocks

Input: List of n instructions (instr[i] = ith instruction)
Output: Set of leaders & list of basic blocks
        (block[x] is block with leader x)

leaders = {1}                    // First instruction is a leader
for i = 1 to n                   // Find all leaders
     if instr[i] is a branch
          leaders = leaders âˆȘ set of potential targets of instr[i]
foreach x ∈ leaders
     block[x] = { x }
     i = x+1                     // Fill out x’s basic block
     while i ≀ n and i ∉ leaders
          block[x] = block[x] âˆȘ { i }
          i=i+1


CIS570 Lecture 3                  Control-Flow Analysis                         7




                                                                                    3
Building Basic Blocks Example

Example                                     Leaders?
1        a := 0                              – {1, 3, 5, 7, 10, 11}
2        b := a *      b
3    L1: c := b/d                           Blocks?
4        if c < x      goto L2              – {1, 2}
5        e := b /      c                    – {3, 4}
6        f := e +      1                    – {5, 6}
7    L2: g := f                             – {7, 8, 9}
8        h := t -      g                    – {10}
9        if e > 0      goto L3              – {11}
10       goto L1
11   L3: return




CIS570 Lecture 3               Control-Flow Analysis                  8




Extended Basic Blocks

Extended basic blocks
 – A maximal sequence of instructions that
    has no merge points in it (except
    perhaps in the leader)
 – Single entry, multiple exits

How are these useful?
 – Increases the scope of any local analysis
   or transformation that “flows forwards”
   (e.g., copy propagation, register
   renaming, instruction scheduling)

Reverse extended basic blocks
 – Useful for “backward flow” problems

CIS570 Lecture 3               Control-Flow Analysis                  9




                                                                          4
Building a CFG from Basic Blocks

Construction
 – Each CFG node represents a basic block
 – There is an edge from node i to j if
     – Last statement of block i branches to the first statement of j, or
     – Block i does not end with an unconditional branch and is
       immediately followed in program order by block j (fall through)
Input: A list of m basic blocks (block)
Output: A CFG where each node is a basic block1                      1
                                                          goto L1:
for i = 1 to m
     x = last instruction of block[i]                                2

     if instr x is a branch                              L1:
                                                    5
          for each target (to block j) of instr x
                create an edge from block i to block j
     if instr x is not an unconditional branch
          create an edge from block i to block i+1
CIS570 Lecture 3                 Control-Flow Analysis                      10




Details

Multiple edges between two nodes
                ...
        if (a<b) goto L2
 L2: ...
 – Combine these edges into one edge

Unreachable code
       ...                       – Perform DFS from entry node
       goto L1                   – Mark each basic block as it is visited
 L0: a = 10                      – Unmarked blocks are unreachable
 L1: ...




CIS570 Lecture 3                 Control-Flow Analysis                      11




                                                                                 5
Challenges

When is CFG construction more challenging?

Languages where jump targets may be unknown
 – e.g., Executable code
   ld $8, 104($7)
   jmp $8

Languages with user-defined control structures
 – e.g., Cecil

     if ( &{x = 3}, &{a := a + 1}, &{a := a - 1} );




CIS570 Lecture 3            Control-Flow Analysis                          12




Loop Concepts

Loop:                   Strongly connected component of CFG
Loop entry edge:        Source not in loop & target in loop
Loop exit edge:         Source in loop & target not in loop
Loop header node:       Target of loop entry edge
Natural loop:           Loop with only a single loop header
Back edge:              Target is loop header & source is in the loop
Loop tail node:         Source of back edge
Loop preheader node:    Single node that’s source of the loop entry edge
Nested loop:            Loop whose header is inside another loop
Reducible flow graph:   CFG whose loops are all natural loops

CIS570 Lecture 3            Control-Flow Analysis                          13




                                                                                6
Picturing Loop Terminology


                                     preheader
                   back edge
                                             entry edge
                                        head




                                                                  loop
                                                              (natural)
                           tail

                                                                 exit edge


CIS570 Lecture 3                  Control-Flow Analysis                               14




The Value of Preheader Nodes

Not all loops have preheaders
 – Sometimes it is useful to create them                  h


Without preheader node                                    t
 – There can be multiple entry edges
                                                                          p   pre-header
With single preheader node
 – There is only one entry edge                                           h

Useful when moving code outside the loop
                                                                          t
 – Don’t have to replicate code for multiple entry
    edges



CIS570 Lecture 3                  Control-Flow Analysis                               15




                                                                                           7
Identifying Loops

Why?
 – Most execution time spent in loops, so optimizing loops will often give
   most benefit

Many approaches
 – Interval analysis
     – Exploit the natural hierarchical structure of programs
     – Decompose the program into nested regions called intervals
 – Structural analysis: a generalization of interval analysis
 – Identify dominators to discover loops

We’ll look at the dominator-based approach



CIS570 Lecture 3                  Control-Flow Analysis                                 16




Dominator Terminology
                                                               entry
Dominators
 d dom i if all paths from entry to node i include d
                                                                 d     d dom i
Strict dominators
  d sdom i if d dom i and d ≠ i                                                  entry
                                                                 i
Immediate dominators
  a idom b if a sdom b and there does not exist a node c              a idom b      a
  such that c ≠ a, c ≠ b, a dom c, and c dom b
                                        not ∃ c, a sdom c and c sdom b
                                                                                    b
Post dominators
                                                                  i
  p pdom i if every possible path from i to exit includes
  p (p dom i in the flow graph whose arcs are reversed
  and entry and exit are interchanged)
                                                                 p       p pdom i


                                                                exit
CIS570 Lecture 3                  Control-Flow Analysis                                 17




                                                                                             8
Identifying Natural Loops with Dominators
Back edges                                                 t
 A back edge of a natural loop is one whose target                       back edge
 dominates its source
                                                           s
Natural loop
 The natural loop of a back edge (m→n), where n            n
 dominates m, is the set of nodes x such that n                          natural
 dominates x and there is a path from x to m not                          loop
 containing n                                              m
                                                                              a
                           a               The target, c, of the              b
Example
                                           edge (d→c) does not
 This loop has two         b                                                  c
                                           dominate its source, d,
 entry points,                             so (d→c) does not
 c and d              c        d                                              d
                                           define a natural loop
                               e                                              e
CIS570 Lecture 3               Control-Flow Analysis                                 18




Computing Dominators

Input: Set of nodes N (in CFG) and an entry node s
Output: Dom[i] = set of all nodes that dominate node i

Dom(s) = {s}                               Key Idea
for each n ∈ N – {s}                         If a node dominates all
     Dom[n] = N                              predecessors of node n, then it
repeat                                       also dominates node n
     change = false
     for each n ∈ N – {s}                     p1       p2     p3        pred[n]
          D = {n} âˆȘ (∩p∈pred(n) Dom[p])
          if D ≠ Dom[n]                                n
               change = true
               Dom[n] = D
until !change             x ∈ Dom(p ) ^ x ∈ Dom(p ) ^ x ∈ Dom(p ) ⇒ x ∈ Dom(n)
                                   1                   2             3


CIS570 Lecture 3               Control-Flow Analysis                                 19




                                                                                          9
Computing Dominators (example)

Input: Set of nodes N and an entry node s
Output: Dom[i] = set of all nodes that dominate node i         s   {s}

Dom(s) = {s}                          {n, p, q, r, s} q               r {n, p, q, r, s}
for each n ∈ N – {s}
                                                              p {n, p, q, r, s}
     Dom[n] = N
repeat                                                        n {n, p, q, r, s}
     change = false                           Initially
     for each n ∈ N – {s}                            Dom[s] = {s}
          D = {n} âˆȘ (∩p∈pred(n) Dom[p])              Dom[q] = {n, p, q, r, s}. . .
          if D ≠ Dom[n]                       Finally
               change = true                         Dom[q] = {q, s}
               Dom[n] = D                            Dom[r] = {r, s}
until !change                                        Dom[p] = {p, s}
                                                     Dom[n] = {n, p, s}
CIS570 Lecture 3                 Control-Flow Analysis                          20




Reducibility

Definition
 – A CFG is reducible (well-structured) if we can partition its edges into
    two disjoint sets, the forward edges and the back edges, such that
      – The forward edges form an acyclic graph in which every node can be
         reached from the entry node
      – The back edges consist only of edges whose targets dominate their
         sources
 – Non-natural loops ⇔ irreducibility
Structured control-flow constructs give rise to reducible CFGs
Value of reducibility
 – Dominance useful in identifying loops
 – Simplifies code transformations (every loop has a single header)
 – Permits interval analysis



CIS570 Lecture 3                 Control-Flow Analysis                          21




                                                                                          10
Handling Irreducible CFG’s

Node splitting
 – Can turn irreducible CFGs into reducible CFGs

                       a                                     a
                       b                                     b

                   c        d                           c        d

                            e                           dâ€Č       e
General idea
 – Reduce graph (iteratively rem. self edges, merge nodes with single pred)
 – More than one node => irreducible
     – Split any multi-parent node and start over



CIS570 Lecture 3                Control-Flow Analysis                         22




Why Go To All This Trouble?

Modern languages provide structured control flow
 – Shouldn’t the compiler remember this information rather than throw it
   away and then re-compute it?

Answers?
 – We may want to work on the binary code in which case such information
   is unavailable
 – Most modern languages still provide a goto statement
 – Languages typically provide multiple types of loops. This analysis lets us
   treat them all uniformly
 – We may want a compiler with multiple front ends for multiple languages;
   rather than translate each language to a CFG, translate each language to a
   canonical LIR, and translate that representation once to a CFG



CIS570 Lecture 3                Control-Flow Analysis                         23




                                                                                   11
Concepts

Control-flow analysis
Basic blocks
  – Computing basic blocks
  – Extended basic blocks
Control-flow graph (CFG)
Loop terminology
Identifying loops
Dominators
Reducibility




CIS570 Lecture 3               Control-Flow Analysis   24




Next Time

Discussion
 – “Binary Translation” by Sites et al.
 – Discussion questions online
 – Come prepared

After that: lecture
 – Introduction to data-flow analysis




CIS570 Lecture 3               Control-Flow Analysis   25




                                                            12

Weitere Àhnliche Inhalte

Was ist angesagt?

Compiler Construction | Lecture 13 | Code Generation
Compiler Construction | Lecture 13 | Code GenerationCompiler Construction | Lecture 13 | Code Generation
Compiler Construction | Lecture 13 | Code GenerationEelco Visser
 
TMPA-2015: Implementing the MetaVCG Approach in the C-light System
TMPA-2015: Implementing the MetaVCG Approach in the C-light SystemTMPA-2015: Implementing the MetaVCG Approach in the C-light System
TMPA-2015: Implementing the MetaVCG Approach in the C-light SystemIosif Itkin
 
Programming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture NotesProgramming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture NotesSreedhar Chowdam
 
The Goal and The Journey - Turning back on one year of C++14 Migration
The Goal and The Journey - Turning back on one year of C++14 MigrationThe Goal and The Journey - Turning back on one year of C++14 Migration
The Goal and The Journey - Turning back on one year of C++14 MigrationJoel Falcou
 
TMPA-2017: The Quest for Average Response Time
TMPA-2017: The Quest for Average Response TimeTMPA-2017: The Quest for Average Response Time
TMPA-2017: The Quest for Average Response TimeIosif Itkin
 
Macro
MacroMacro
MacroGoogle
 
Sentence-to-Code Traceability Recovery with Domain Ontologies
Sentence-to-Code Traceability Recovery with Domain OntologiesSentence-to-Code Traceability Recovery with Domain Ontologies
Sentence-to-Code Traceability Recovery with Domain OntologiesShinpei Hayashi
 
Day2 Verilog HDL Basic
Day2 Verilog HDL BasicDay2 Verilog HDL Basic
Day2 Verilog HDL BasicRon Liu
 
Digital Circuit Verification Hardware Descriptive Language Verilog
Digital Circuit Verification Hardware Descriptive Language VerilogDigital Circuit Verification Hardware Descriptive Language Verilog
Digital Circuit Verification Hardware Descriptive Language VerilogAbhiraj Bohra
 
Verilog tutorial
Verilog tutorialVerilog tutorial
Verilog tutorialAbhiraj Bohra
 
Verilog overview
Verilog overviewVerilog overview
Verilog overviewposdege
 
Verilog tutorial
Verilog tutorialVerilog tutorial
Verilog tutorialraju reddy
 
Parametrized Model Checking of Fault Tolerant Distributed Algorithms by Abstr...
Parametrized Model Checking of Fault Tolerant Distributed Algorithms by Abstr...Parametrized Model Checking of Fault Tolerant Distributed Algorithms by Abstr...
Parametrized Model Checking of Fault Tolerant Distributed Algorithms by Abstr...Iosif Itkin
 
Automatically Documenting Program Changes
Automatically Documenting Program ChangesAutomatically Documenting Program Changes
Automatically Documenting Program ChangesRay Buse
 
VHDL- gate level modelling
VHDL- gate level modellingVHDL- gate level modelling
VHDL- gate level modellingVandanaPagar1
 

Was ist angesagt? (20)

Compiler Construction | Lecture 13 | Code Generation
Compiler Construction | Lecture 13 | Code GenerationCompiler Construction | Lecture 13 | Code Generation
Compiler Construction | Lecture 13 | Code Generation
 
TMPA-2015: Implementing the MetaVCG Approach in the C-light System
TMPA-2015: Implementing the MetaVCG Approach in the C-light SystemTMPA-2015: Implementing the MetaVCG Approach in the C-light System
TMPA-2015: Implementing the MetaVCG Approach in the C-light System
 
Programming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture NotesProgramming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture Notes
 
Crash course in verilog
Crash course in verilogCrash course in verilog
Crash course in verilog
 
The Goal and The Journey - Turning back on one year of C++14 Migration
The Goal and The Journey - Turning back on one year of C++14 MigrationThe Goal and The Journey - Turning back on one year of C++14 Migration
The Goal and The Journey - Turning back on one year of C++14 Migration
 
Verilog hdl
Verilog hdlVerilog hdl
Verilog hdl
 
TMPA-2017: The Quest for Average Response Time
TMPA-2017: The Quest for Average Response TimeTMPA-2017: The Quest for Average Response Time
TMPA-2017: The Quest for Average Response Time
 
Macro
MacroMacro
Macro
 
Verilog hdl
Verilog hdlVerilog hdl
Verilog hdl
 
Sentence-to-Code Traceability Recovery with Domain Ontologies
Sentence-to-Code Traceability Recovery with Domain OntologiesSentence-to-Code Traceability Recovery with Domain Ontologies
Sentence-to-Code Traceability Recovery with Domain Ontologies
 
Day2 Verilog HDL Basic
Day2 Verilog HDL BasicDay2 Verilog HDL Basic
Day2 Verilog HDL Basic
 
Digital Circuit Verification Hardware Descriptive Language Verilog
Digital Circuit Verification Hardware Descriptive Language VerilogDigital Circuit Verification Hardware Descriptive Language Verilog
Digital Circuit Verification Hardware Descriptive Language Verilog
 
Verilog tutorial
Verilog tutorialVerilog tutorial
Verilog tutorial
 
Verilog overview
Verilog overviewVerilog overview
Verilog overview
 
verilog
verilogverilog
verilog
 
Verilog tutorial
Verilog tutorialVerilog tutorial
Verilog tutorial
 
Parametrized Model Checking of Fault Tolerant Distributed Algorithms by Abstr...
Parametrized Model Checking of Fault Tolerant Distributed Algorithms by Abstr...Parametrized Model Checking of Fault Tolerant Distributed Algorithms by Abstr...
Parametrized Model Checking of Fault Tolerant Distributed Algorithms by Abstr...
 
Automatically Documenting Program Changes
Automatically Documenting Program ChangesAutomatically Documenting Program Changes
Automatically Documenting Program Changes
 
Verilog tutorial
Verilog tutorialVerilog tutorial
Verilog tutorial
 
VHDL- gate level modelling
VHDL- gate level modellingVHDL- gate level modelling
VHDL- gate level modelling
 

Andere mochten auch

Lecture07
Lecture07Lecture07
Lecture07sean chen
 
Example my hdl
Example my hdlExample my hdl
Example my hdlsean chen
 
Lecture04
Lecture04Lecture04
Lecture04sean chen
 
Uvm presentation dac2011_final
Uvm presentation dac2011_finalUvm presentation dac2011_final
Uvm presentation dac2011_finalsean chen
 

Andere mochten auch (7)

Lecture07
Lecture07Lecture07
Lecture07
 
Demo
DemoDemo
Demo
 
Example my hdl
Example my hdlExample my hdl
Example my hdl
 
Lecture04
Lecture04Lecture04
Lecture04
 
I2 c
I2 cI2 c
I2 c
 
Uvm presentation dac2011_final
Uvm presentation dac2011_finalUvm presentation dac2011_final
Uvm presentation dac2011_final
 
Uart
UartUart
Uart
 

Ähnlich wie Lecture03

Control Flow Graphs
Control Flow GraphsControl Flow Graphs
Control Flow Graphsdaimk2020
 
Control Flow Graphs
Control Flow GraphsControl Flow Graphs
Control Flow Graphsdaimk2020
 
Control Flow Analysis
Control Flow AnalysisControl Flow Analysis
Control Flow AnalysisEdgar Barbosa
 
Good news, everybody! Guile 2.2 performance notes (FOSDEM 2016)
Good news, everybody! Guile 2.2 performance notes (FOSDEM 2016)Good news, everybody! Guile 2.2 performance notes (FOSDEM 2016)
Good news, everybody! Guile 2.2 performance notes (FOSDEM 2016)Igalia
 
Principal Sources of Optimization in compiler design
Principal Sources of Optimization in compiler design Principal Sources of Optimization in compiler design
Principal Sources of Optimization in compiler design LogsAk
 
Code optimization lecture
Code optimization lectureCode optimization lecture
Code optimization lecturePrashant Singh
 
CS540-2-lecture11 - Copy.ppt
CS540-2-lecture11 - Copy.pptCS540-2-lecture11 - Copy.ppt
CS540-2-lecture11 - Copy.pptssuser0be977
 
Code Optimization.ppt
Code Optimization.pptCode Optimization.ppt
Code Optimization.pptJohnSamuel280314
 
Update on C++ Core Guidelines Lifetime Analysis. GĂĄbor HorvĂĄth. CoreHard Spri...
Update on C++ Core Guidelines Lifetime Analysis. GĂĄbor HorvĂĄth. CoreHard Spri...Update on C++ Core Guidelines Lifetime Analysis. GĂĄbor HorvĂĄth. CoreHard Spri...
Update on C++ Core Guidelines Lifetime Analysis. GĂĄbor HorvĂĄth. CoreHard Spri...corehard_by
 
Python programming –part 3
Python programming –part 3Python programming –part 3
Python programming –part 3Megha V
 
Reverse Engineering Dojo: Enhancing Assembly Reading Skills
Reverse Engineering Dojo: Enhancing Assembly Reading SkillsReverse Engineering Dojo: Enhancing Assembly Reading Skills
Reverse Engineering Dojo: Enhancing Assembly Reading SkillsAsuka Nakajima
 
07 control+structures
07 control+structures07 control+structures
07 control+structuresbaran19901990
 
Caffe studying 2017
Caffe studying 2017Caffe studying 2017
Caffe studying 2017Te-Yen Liu
 
Basic blocks and control flow graphs
Basic blocks and control flow graphsBasic blocks and control flow graphs
Basic blocks and control flow graphsTilakpoudel2
 
EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5PRADEEP
 
Introduction to VHDL
Introduction to VHDLIntroduction to VHDL
Introduction to VHDLYaser Kalifa
 
Peephole Optimization
Peephole OptimizationPeephole Optimization
Peephole OptimizationMeghaj Mallick
 
Beginner's guide to linkers
Beginner's guide to linkersBeginner's guide to linkers
Beginner's guide to linkersPinkus Chang
 

Ähnlich wie Lecture03 (20)

Control Flow Graphs
Control Flow GraphsControl Flow Graphs
Control Flow Graphs
 
Control Flow Graphs
Control Flow GraphsControl Flow Graphs
Control Flow Graphs
 
Control Flow Analysis
Control Flow AnalysisControl Flow Analysis
Control Flow Analysis
 
Good news, everybody! Guile 2.2 performance notes (FOSDEM 2016)
Good news, everybody! Guile 2.2 performance notes (FOSDEM 2016)Good news, everybody! Guile 2.2 performance notes (FOSDEM 2016)
Good news, everybody! Guile 2.2 performance notes (FOSDEM 2016)
 
Principal Sources of Optimization in compiler design
Principal Sources of Optimization in compiler design Principal Sources of Optimization in compiler design
Principal Sources of Optimization in compiler design
 
Code optimization lecture
Code optimization lectureCode optimization lecture
Code optimization lecture
 
CS540-2-lecture11 - Copy.ppt
CS540-2-lecture11 - Copy.pptCS540-2-lecture11 - Copy.ppt
CS540-2-lecture11 - Copy.ppt
 
Code Optimization.ppt
Code Optimization.pptCode Optimization.ppt
Code Optimization.ppt
 
1.ppt
1.ppt1.ppt
1.ppt
 
Update on C++ Core Guidelines Lifetime Analysis. GĂĄbor HorvĂĄth. CoreHard Spri...
Update on C++ Core Guidelines Lifetime Analysis. GĂĄbor HorvĂĄth. CoreHard Spri...Update on C++ Core Guidelines Lifetime Analysis. GĂĄbor HorvĂĄth. CoreHard Spri...
Update on C++ Core Guidelines Lifetime Analysis. GĂĄbor HorvĂĄth. CoreHard Spri...
 
Python programming –part 3
Python programming –part 3Python programming –part 3
Python programming –part 3
 
Reverse Engineering Dojo: Enhancing Assembly Reading Skills
Reverse Engineering Dojo: Enhancing Assembly Reading SkillsReverse Engineering Dojo: Enhancing Assembly Reading Skills
Reverse Engineering Dojo: Enhancing Assembly Reading Skills
 
07 control+structures
07 control+structures07 control+structures
07 control+structures
 
Caffe studying 2017
Caffe studying 2017Caffe studying 2017
Caffe studying 2017
 
Basic blocks and control flow graphs
Basic blocks and control flow graphsBasic blocks and control flow graphs
Basic blocks and control flow graphs
 
EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5
 
Introduction to VHDL
Introduction to VHDLIntroduction to VHDL
Introduction to VHDL
 
Peephole Optimization
Peephole OptimizationPeephole Optimization
Peephole Optimization
 
Vlsi lab2
Vlsi lab2Vlsi lab2
Vlsi lab2
 
Beginner's guide to linkers
Beginner's guide to linkersBeginner's guide to linkers
Beginner's guide to linkers
 

Mehr von sean chen

Uvm dcon2013
Uvm dcon2013Uvm dcon2013
Uvm dcon2013sean chen
 
0021.system partitioning
0021.system partitioning0021.system partitioning
0021.system partitioningsean chen
 
0015.register allocation-graph-coloring
0015.register allocation-graph-coloring0015.register allocation-graph-coloring
0015.register allocation-graph-coloringsean chen
 
0006.scheduling not-ilp-not-force
0006.scheduling not-ilp-not-force0006.scheduling not-ilp-not-force
0006.scheduling not-ilp-not-forcesean chen
 
Dominator tree
Dominator treeDominator tree
Dominator treesean chen
 
Work items
Work itemsWork items
Work itemssean chen
 
Work items
Work itemsWork items
Work itemssean chen
 
ocelot
ocelotocelot
ocelotsean chen
 
Lect.10.arm soc.4 neon
Lect.10.arm soc.4 neonLect.10.arm soc.4 neon
Lect.10.arm soc.4 neonsean chen
 
Image scalar hw_algorithm
Image scalar hw_algorithmImage scalar hw_algorithm
Image scalar hw_algorithmsean chen
 
Virtual platform
Virtual platformVirtual platform
Virtual platformsean chen
 
Serializer
SerializerSerializer
Serializersean chen
 
Defense
DefenseDefense
Defensesean chen
 
Defense
DefenseDefense
Defensesean chen
 
Linux mouse
Linux mouseLinux mouse
Linux mousesean chen
 

Mehr von sean chen (16)

Uvm dcon2013
Uvm dcon2013Uvm dcon2013
Uvm dcon2013
 
0021.system partitioning
0021.system partitioning0021.system partitioning
0021.system partitioning
 
0015.register allocation-graph-coloring
0015.register allocation-graph-coloring0015.register allocation-graph-coloring
0015.register allocation-graph-coloring
 
0006.scheduling not-ilp-not-force
0006.scheduling not-ilp-not-force0006.scheduling not-ilp-not-force
0006.scheduling not-ilp-not-force
 
Dominator tree
Dominator treeDominator tree
Dominator tree
 
Work items
Work itemsWork items
Work items
 
Work items
Work itemsWork items
Work items
 
ocelot
ocelotocelot
ocelot
 
Lect.10.arm soc.4 neon
Lect.10.arm soc.4 neonLect.10.arm soc.4 neon
Lect.10.arm soc.4 neon
 
Image scalar hw_algorithm
Image scalar hw_algorithmImage scalar hw_algorithm
Image scalar hw_algorithm
 
Virtual platform
Virtual platformVirtual platform
Virtual platform
 
Spi
SpiSpi
Spi
 
Serializer
SerializerSerializer
Serializer
 
Defense
DefenseDefense
Defense
 
Defense
DefenseDefense
Defense
 
Linux mouse
Linux mouseLinux mouse
Linux mouse
 

KĂŒrzlich hochgeladen

Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Visit to a blind student's school🧑‍🩯🧑‍🩯(community medicine)
Visit to a blind student's school🧑‍🩯🧑‍🩯(community medicine)Visit to a blind student's school🧑‍🩯🧑‍🩯(community medicine)
Visit to a blind student's school🧑‍🩯🧑‍🩯(community medicine)lakshayb543
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsManeerUddin
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 

KĂŒrzlich hochgeladen (20)

Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Visit to a blind student's school🧑‍🩯🧑‍🩯(community medicine)
Visit to a blind student's school🧑‍🩯🧑‍🩯(community medicine)Visit to a blind student's school🧑‍🩯🧑‍🩯(community medicine)
Visit to a blind student's school🧑‍🩯🧑‍🩯(community medicine)
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture hons
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 

Lecture03

  • 1. Control-Flow Analysis Last time – Undergraduate compilers in a day Today – Control-flow analysis – Building basic blocks – Building control-flow graphs – Loops CIS570 Lecture 3 Control-Flow Analysis 2 Context Data-flow – Flow of data values from defs to uses Control-flow – Sequencing of operations e.g., Evaluation of then-code and else-code depends on if-test CIS570 Lecture 3 Control-Flow Analysis 3 1
  • 2. Representing Control-Flow High-level representation – Control flow is implicit in an AST Low-level representation: – Use a Control-flow graph (CFG) – Nodes represent statements (low-level linear IR) – Edges represent explicit flow of control Other options – Control dependences in program dependence graph (PDG) [Ferrante87] – Dependences on explicit state in value dependence graph (VDG) [Weise 94] CIS570 Lecture 3 Control-Flow Analysis 4 What Is Control-Flow Analysis? Control-flow analysis discovers the flow of control within a procedure (e.g., builds a CFG, identifies loops) 1 a := 0 Example b := a * b 1 a := 0 3 c := b/d 2 b := a * b c < x? 3 L1: c := b/d 4 if c < x goto L2 5 e := b / c 5 e := b / c f := e + 1 6 f := e + 1 7 L2: g := f 7 g := f 8 h := t - g h := t - g 9 if e > 0 goto L3 e > 0? 10 goto L1 No Yes 11 L3: return 10 goto 11 return CIS570 Lecture 3 Control-Flow Analysis 5 2
  • 3. Basic Blocks Definition – A basic block is a sequence of straight line code that can be entered only at the beginning and exited only at the end g := f h := t - g e > 0? No Yes Building basic blocks – Identify leaders – The first instruction in a procedure, or – The target of any branch, or – An instruction immediately following a branch (implicit target) – Gobble all subsequent instructions until the next leader CIS570 Lecture 3 Control-Flow Analysis 6 Algorithm for Building Basic Blocks Input: List of n instructions (instr[i] = ith instruction) Output: Set of leaders & list of basic blocks (block[x] is block with leader x) leaders = {1} // First instruction is a leader for i = 1 to n // Find all leaders if instr[i] is a branch leaders = leaders âˆȘ set of potential targets of instr[i] foreach x ∈ leaders block[x] = { x } i = x+1 // Fill out x’s basic block while i ≀ n and i ∉ leaders block[x] = block[x] âˆȘ { i } i=i+1 CIS570 Lecture 3 Control-Flow Analysis 7 3
  • 4. Building Basic Blocks Example Example Leaders? 1 a := 0 – {1, 3, 5, 7, 10, 11} 2 b := a * b 3 L1: c := b/d Blocks? 4 if c < x goto L2 – {1, 2} 5 e := b / c – {3, 4} 6 f := e + 1 – {5, 6} 7 L2: g := f – {7, 8, 9} 8 h := t - g – {10} 9 if e > 0 goto L3 – {11} 10 goto L1 11 L3: return CIS570 Lecture 3 Control-Flow Analysis 8 Extended Basic Blocks Extended basic blocks – A maximal sequence of instructions that has no merge points in it (except perhaps in the leader) – Single entry, multiple exits How are these useful? – Increases the scope of any local analysis or transformation that “flows forwards” (e.g., copy propagation, register renaming, instruction scheduling) Reverse extended basic blocks – Useful for “backward flow” problems CIS570 Lecture 3 Control-Flow Analysis 9 4
  • 5. Building a CFG from Basic Blocks Construction – Each CFG node represents a basic block – There is an edge from node i to j if – Last statement of block i branches to the first statement of j, or – Block i does not end with an unconditional branch and is immediately followed in program order by block j (fall through) Input: A list of m basic blocks (block) Output: A CFG where each node is a basic block1 1 goto L1: for i = 1 to m x = last instruction of block[i] 2 if instr x is a branch L1: 5 for each target (to block j) of instr x create an edge from block i to block j if instr x is not an unconditional branch create an edge from block i to block i+1 CIS570 Lecture 3 Control-Flow Analysis 10 Details Multiple edges between two nodes ... if (a<b) goto L2 L2: ... – Combine these edges into one edge Unreachable code ... – Perform DFS from entry node goto L1 – Mark each basic block as it is visited L0: a = 10 – Unmarked blocks are unreachable L1: ... CIS570 Lecture 3 Control-Flow Analysis 11 5
  • 6. Challenges When is CFG construction more challenging? Languages where jump targets may be unknown – e.g., Executable code ld $8, 104($7) jmp $8 Languages with user-defined control structures – e.g., Cecil if ( &{x = 3}, &{a := a + 1}, &{a := a - 1} ); CIS570 Lecture 3 Control-Flow Analysis 12 Loop Concepts Loop: Strongly connected component of CFG Loop entry edge: Source not in loop & target in loop Loop exit edge: Source in loop & target not in loop Loop header node: Target of loop entry edge Natural loop: Loop with only a single loop header Back edge: Target is loop header & source is in the loop Loop tail node: Source of back edge Loop preheader node: Single node that’s source of the loop entry edge Nested loop: Loop whose header is inside another loop Reducible flow graph: CFG whose loops are all natural loops CIS570 Lecture 3 Control-Flow Analysis 13 6
  • 7. Picturing Loop Terminology preheader back edge entry edge head loop (natural) tail exit edge CIS570 Lecture 3 Control-Flow Analysis 14 The Value of Preheader Nodes Not all loops have preheaders – Sometimes it is useful to create them h Without preheader node t – There can be multiple entry edges p pre-header With single preheader node – There is only one entry edge h Useful when moving code outside the loop t – Don’t have to replicate code for multiple entry edges CIS570 Lecture 3 Control-Flow Analysis 15 7
  • 8. Identifying Loops Why? – Most execution time spent in loops, so optimizing loops will often give most benefit Many approaches – Interval analysis – Exploit the natural hierarchical structure of programs – Decompose the program into nested regions called intervals – Structural analysis: a generalization of interval analysis – Identify dominators to discover loops We’ll look at the dominator-based approach CIS570 Lecture 3 Control-Flow Analysis 16 Dominator Terminology entry Dominators d dom i if all paths from entry to node i include d d d dom i Strict dominators d sdom i if d dom i and d ≠ i entry i Immediate dominators a idom b if a sdom b and there does not exist a node c a idom b a such that c ≠ a, c ≠ b, a dom c, and c dom b not ∃ c, a sdom c and c sdom b b Post dominators i p pdom i if every possible path from i to exit includes p (p dom i in the flow graph whose arcs are reversed and entry and exit are interchanged) p p pdom i exit CIS570 Lecture 3 Control-Flow Analysis 17 8
  • 9. Identifying Natural Loops with Dominators Back edges t A back edge of a natural loop is one whose target back edge dominates its source s Natural loop The natural loop of a back edge (m→n), where n n dominates m, is the set of nodes x such that n natural dominates x and there is a path from x to m not loop containing n m a a The target, c, of the b Example edge (d→c) does not This loop has two b c dominate its source, d, entry points, so (d→c) does not c and d c d d define a natural loop e e CIS570 Lecture 3 Control-Flow Analysis 18 Computing Dominators Input: Set of nodes N (in CFG) and an entry node s Output: Dom[i] = set of all nodes that dominate node i Dom(s) = {s} Key Idea for each n ∈ N – {s} If a node dominates all Dom[n] = N predecessors of node n, then it repeat also dominates node n change = false for each n ∈ N – {s} p1 p2 p3 pred[n] D = {n} âˆȘ (∩p∈pred(n) Dom[p]) if D ≠ Dom[n] n change = true Dom[n] = D until !change x ∈ Dom(p ) ^ x ∈ Dom(p ) ^ x ∈ Dom(p ) ⇒ x ∈ Dom(n) 1 2 3 CIS570 Lecture 3 Control-Flow Analysis 19 9
  • 10. Computing Dominators (example) Input: Set of nodes N and an entry node s Output: Dom[i] = set of all nodes that dominate node i s {s} Dom(s) = {s} {n, p, q, r, s} q r {n, p, q, r, s} for each n ∈ N – {s} p {n, p, q, r, s} Dom[n] = N repeat n {n, p, q, r, s} change = false Initially for each n ∈ N – {s} Dom[s] = {s} D = {n} âˆȘ (∩p∈pred(n) Dom[p]) Dom[q] = {n, p, q, r, s}. . . if D ≠ Dom[n] Finally change = true Dom[q] = {q, s} Dom[n] = D Dom[r] = {r, s} until !change Dom[p] = {p, s} Dom[n] = {n, p, s} CIS570 Lecture 3 Control-Flow Analysis 20 Reducibility Definition – A CFG is reducible (well-structured) if we can partition its edges into two disjoint sets, the forward edges and the back edges, such that – The forward edges form an acyclic graph in which every node can be reached from the entry node – The back edges consist only of edges whose targets dominate their sources – Non-natural loops ⇔ irreducibility Structured control-flow constructs give rise to reducible CFGs Value of reducibility – Dominance useful in identifying loops – Simplifies code transformations (every loop has a single header) – Permits interval analysis CIS570 Lecture 3 Control-Flow Analysis 21 10
  • 11. Handling Irreducible CFG’s Node splitting – Can turn irreducible CFGs into reducible CFGs a a b b c d c d e dâ€Č e General idea – Reduce graph (iteratively rem. self edges, merge nodes with single pred) – More than one node => irreducible – Split any multi-parent node and start over CIS570 Lecture 3 Control-Flow Analysis 22 Why Go To All This Trouble? Modern languages provide structured control flow – Shouldn’t the compiler remember this information rather than throw it away and then re-compute it? Answers? – We may want to work on the binary code in which case such information is unavailable – Most modern languages still provide a goto statement – Languages typically provide multiple types of loops. This analysis lets us treat them all uniformly – We may want a compiler with multiple front ends for multiple languages; rather than translate each language to a CFG, translate each language to a canonical LIR, and translate that representation once to a CFG CIS570 Lecture 3 Control-Flow Analysis 23 11
  • 12. Concepts Control-flow analysis Basic blocks – Computing basic blocks – Extended basic blocks Control-flow graph (CFG) Loop terminology Identifying loops Dominators Reducibility CIS570 Lecture 3 Control-Flow Analysis 24 Next Time Discussion – “Binary Translation” by Sites et al. – Discussion questions online – Come prepared After that: lecture – Introduction to data-flow analysis CIS570 Lecture 3 Control-Flow Analysis 25 12