machine-independent optimizations - part 3 -...

21
Machine-Independent Optimizations - Part 3 Y.N. Srikant Department of Computer Science Indian Institute of Science Bangalore 560 012 NPTEL Course on Compiler Design Y.N. Srikant Machine-Independent Optimizations

Upload: trinhque

Post on 16-Jun-2018

230 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Machine-Independent Optimizations - Part 3 - NPTELnptel.ac.in/courses/106108052/module10/opt-3.pdf · Machine-Independent Optimizations - Part 3 Y.N. Srikant Department of Computer

Machine-Independent Optimizations - Part 3

Y.N. Srikant

Department of Computer ScienceIndian Institute of Science

Bangalore 560 012

NPTEL Course on Compiler Design

Y.N. Srikant Machine-Independent Optimizations

Page 2: Machine-Independent Optimizations - Part 3 - NPTELnptel.ac.in/courses/106108052/module10/opt-3.pdf · Machine-Independent Optimizations - Part 3 Y.N. Srikant Department of Computer

Outline of the Lecture

Global common sub-expression eliminationCopy propagationLoop invariant code motionInduction variable elimination and strength reductionRegion based data-flow analysis

In part 1 of the lecture, we covered topics 1,2,3, and parts of 4.

Y.N. Srikant Machine-Independent Optimizations

Page 3: Machine-Independent Optimizations - Part 3 - NPTELnptel.ac.in/courses/106108052/module10/opt-3.pdf · Machine-Independent Optimizations - Part 3 Y.N. Srikant Department of Computer

Region Based Data-flow Analysis

Region: A set of nodes N that includes a header, whichdominates all other nodes in the regionAll edges between nodes in N are in the region, except(possibly) for some of those that enter the headerAll intervals are regions but there are regions that are notintervals

A region may omit some nodes that an interval wouldinclude or they may omit some edges back to the headerFor example, I(7) = {7,8,9,10,11}, but {8,9,10} could bea region (see next slide)

A region may have multiple exitsWe shall compute genR,B and killR,B of definitionsgenerated and killed (resp.), along paths within the regionR, from the header to the end of the block B

Y.N. Srikant Machine-Independent Optimizations

Page 4: Machine-Independent Optimizations - Part 3 - NPTELnptel.ac.in/courses/106108052/module10/opt-3.pdf · Machine-Independent Optimizations - Part 3 Y.N. Srikant Department of Computer

Region Based Data-flow Analysis (2)

These will be used to define a transfer functiontransR,B(S), that tells for any set S of definitions, whatsubset of definitions reach the end of B by travelling alongpaths wholly within R, assuming that all and only thedefinitions in S reach the header of RtransR,B(S) = genR,B

⋃(S − killR,B)

transU,B(φ) = OUT [B] = genU,B, where U is the regionconsisting of the entire flow graphWe need to provide a method to compute the transferfunctions transR,B, for progressively larger regions definedby some (T1 − T2) transformation of a CFGSince OUT [B] = genU,B, we need to compute only genR,Band killR,B, for each basic block, for progressively largerregionsInterestingly, this approach does not compute IN[B] at all

Y.N. Srikant Machine-Independent Optimizations

Page 5: Machine-Independent Optimizations - Part 3 - NPTELnptel.ac.in/courses/106108052/module10/opt-3.pdf · Machine-Independent Optimizations - Part 3 Y.N. Srikant Department of Computer

Region Based Data-flow Analysis (3)

As we reduce a flow graph G by T1 and T2 transformations,at all times, the following conditions are true

1 A node represents a region of G2 An edge from a to b in a reduced graph represents a set of

edges3 Each node and edge of G is represented by exactly one

node or edge of the current graph

Region based DFA can be compared to syntax-directedtranslation, with the structure being provided by thehierarchy of regionsWe consider data-flow analysis for reaching definitionsIt should be emphasized that all data-flow values whichreach the header of a region will surely flow to all theconstituent regions and basic blocks, since all basic blocksare reacheable from the header of the enclosing region

Y.N. Srikant Machine-Independent Optimizations

Page 6: Machine-Independent Optimizations - Part 3 - NPTELnptel.ac.in/courses/106108052/module10/opt-3.pdf · Machine-Independent Optimizations - Part 3 Y.N. Srikant Department of Computer

Region Example

Y.N. Srikant Machine-Independent Optimizations

Page 7: Machine-Independent Optimizations - Part 3 - NPTELnptel.ac.in/courses/106108052/module10/opt-3.pdf · Machine-Independent Optimizations - Part 3 Y.N. Srikant Department of Computer

Region Building by T2 Trans. - Reaching Def

Y.N. Srikant Machine-Independent Optimizations

Page 8: Machine-Independent Optimizations - Part 3 - NPTELnptel.ac.in/courses/106108052/module10/opt-3.pdf · Machine-Independent Optimizations - Part 3 Y.N. Srikant Department of Computer

Region Building by T1 Trans. - Reaching Def

Y.N. Srikant Machine-Independent Optimizations

Page 9: Machine-Independent Optimizations - Part 3 - NPTELnptel.ac.in/courses/106108052/module10/opt-3.pdf · Machine-Independent Optimizations - Part 3 Y.N. Srikant Department of Computer

Region Based RD Analysis - An Example (1)

Y.N. Srikant Machine-Independent Optimizations

Page 10: Machine-Independent Optimizations - Part 3 - NPTELnptel.ac.in/courses/106108052/module10/opt-3.pdf · Machine-Independent Optimizations - Part 3 Y.N. Srikant Department of Computer

Region Based RD Analysis - An Example (2)

Building region R from regions C and D by T2 transf.genR,C = genC,C = 000; killR,C = killC,C = 010Header of D is D and pred. of D in C is CG = genC,C = 000 and K = killC,C = 010genR,D = genD,D ∪ (G− killD,D) = 001+(000− 000) = 001killR,D = killD,D ∪ (K − genD,D) = 000 + (010− 001) = 010

Y.N. Srikant Machine-Independent Optimizations

Page 11: Machine-Independent Optimizations - Part 3 - NPTELnptel.ac.in/courses/106108052/module10/opt-3.pdf · Machine-Independent Optimizations - Part 3 Y.N. Srikant Department of Computer

Region Based RD Analysis - An Example (3)

Building region S from region R by T1 transformationThe only predecessor of the header C, within S is DTherefore, G = genR,D = 001killS,C = killR,C = 010; killS,D = killR,D = 010genS,C = genR,C ∪ (G− killR,C) = 000+(001− 010) = 001genS,D = genR,D ∪ (G− killR,D) = 001+ (001− 010) = 001

Y.N. Srikant Machine-Independent Optimizations

Page 12: Machine-Independent Optimizations - Part 3 - NPTELnptel.ac.in/courses/106108052/module10/opt-3.pdf · Machine-Independent Optimizations - Part 3 Y.N. Srikant Department of Computer

Region Based RD Analysis - An Example (4)

Building region T from regions A and B by T2 transf.genT ,A = genA,A = 100; killT ,A = killA,A = 010Header of B is B and pred. of B in A is AG = genA,A = 100 and K = killA,A = 010genT ,B = genB,B ∪ (G− killB,B) = 010 + (100− 101) = 010killT ,B = killB,B ∪ (K − genB,B) = 101 + (010− 010) = 101

Y.N. Srikant Machine-Independent Optimizations

Page 13: Machine-Independent Optimizations - Part 3 - NPTELnptel.ac.in/courses/106108052/module10/opt-3.pdf · Machine-Independent Optimizations - Part 3 Y.N. Srikant Department of Computer

Region Based RD Analysis - An Example (5)

Building region U from regions T and S by T2 transf.genU,A = genT ,A = 100; killU,A = killT ,A = 010genU,B = genT ,B = 010; killU,B = killT ,B = 101

Y.N. Srikant Machine-Independent Optimizations

Page 14: Machine-Independent Optimizations - Part 3 - NPTELnptel.ac.in/courses/106108052/module10/opt-3.pdf · Machine-Independent Optimizations - Part 3 Y.N. Srikant Department of Computer

Region Based RD Analysis - An Example (6)

Building region U from regions T and S by T2 transf.Header of S is C and pred. of C in T are A and BG = genT ,A ∪ genT ,B = 110 andK = killT ,A ∩ killT ,B = 000genU,C = genS,C ∪ (G− killS,C) = 001+ (110− 010) = 101killU,C = killS,C ∪ (K − genS,C) = 010 + (000− 001) = 010genU,D = genS,D ∪ (G− killS,D) = 001+ (110− 010) = 101killU,D = killS,D ∪ (K − genS,D) = 010 + (000− 001) = 010

Y.N. Srikant Machine-Independent Optimizations

Page 15: Machine-Independent Optimizations - Part 3 - NPTELnptel.ac.in/courses/106108052/module10/opt-3.pdf · Machine-Independent Optimizations - Part 3 Y.N. Srikant Department of Computer

Region Based RD Analysis - An Example (7)

Building region V from region V by T1 transf.Header of U is A and pred. of A in U are C and DG = genU,C ∪ genU,D = 101genV ,C = genU,C ∪ (G− killU,C) = 101+(101−010) = 101genV ,D = genU,D ∪ (G− killU,D) = 101+(101−010) = 101killV ,C = killU,C = 010; killV ,D = killU,D = 010

Y.N. Srikant Machine-Independent Optimizations

Page 16: Machine-Independent Optimizations - Part 3 - NPTELnptel.ac.in/courses/106108052/module10/opt-3.pdf · Machine-Independent Optimizations - Part 3 Y.N. Srikant Department of Computer

Region Based RD Analysis - An Example (8)

Building region V from region V by T1 transf.Header of U is A and pred. of A in U are C and DG = genU,C ∪ genU,D = 101genV ,A = genU,A ∪ (G− killU,A) = 100+ (101− 010) = 101genV ,B = genU,B ∪ (G− killU,B) = 010+ (101− 101) = 010killV ,A = killU,A = 010; killV ,B = killU,B = 101

Y.N. Srikant Machine-Independent Optimizations

Page 17: Machine-Independent Optimizations - Part 3 - NPTELnptel.ac.in/courses/106108052/module10/opt-3.pdf · Machine-Independent Optimizations - Part 3 Y.N. Srikant Department of Computer

Results from Iterative RD DFA for the same example

Y.N. Srikant Machine-Independent Optimizations

Page 18: Machine-Independent Optimizations - Part 3 - NPTELnptel.ac.in/courses/106108052/module10/opt-3.pdf · Machine-Independent Optimizations - Part 3 Y.N. Srikant Department of Computer

Region Building by T2 Trans. - Available Exp

Y.N. Srikant Machine-Independent Optimizations

Page 19: Machine-Independent Optimizations - Part 3 - NPTELnptel.ac.in/courses/106108052/module10/opt-3.pdf · Machine-Independent Optimizations - Part 3 Y.N. Srikant Department of Computer

Region Building by T1 Trans. - Available Exp

Y.N. Srikant Machine-Independent Optimizations

Page 20: Machine-Independent Optimizations - Part 3 - NPTELnptel.ac.in/courses/106108052/module10/opt-3.pdf · Machine-Independent Optimizations - Part 3 Y.N. Srikant Department of Computer

Results from Iterative AE DFA for the same example

Y.N. Srikant Machine-Independent Optimizations

Page 21: Machine-Independent Optimizations - Part 3 - NPTELnptel.ac.in/courses/106108052/module10/opt-3.pdf · Machine-Independent Optimizations - Part 3 Y.N. Srikant Department of Computer

Handling Irreducible Flow-Graphs

At some point of reduction in T1 − T2 analysis, no furtherreduction is possible if the graph is irreducibleAt this point, we split nodes (regions are now nodes) andduplicate them as explained earlierWe then continue our analysisIf we wish to retain the original graph with no splitting, thenafter analyzing the split graph, we computeIN[B] = IN[B1] ∧ IN[B2] ∧ ... ∧ IN[Bk ], where, Bi , 1 ≤ i ≤ kare the siblings of the split node BSplitting regions may be some times beneficial tooptimizations since data-flow information may becomemore precise after splitting

For example, fewer definitions may reach each of theduplicated blocks than that reach the original block

Y.N. Srikant Machine-Independent Optimizations