Download presentation

Presentation is loading. Please wait.

Published byJourney Hunnings Modified about 1 year ago

1
Advanced Compiler Techniques LIU Xianhua School of EECS, Peking University Partial Redundancy Elimination

2
REVIEW Foundations Data Flow Framework Lattice - Theoretic Formulation Meet - Over - Paths Solution Extensions Other DFA Methods 2 “Advanced Compiler Techniques”

3
REVIEW 3 “Advanced Compiler Techniques” Available Expressions Analysis Live Variables Analysis ≤ is ⊆, ∧ is ∩≤ is ⊇, ∧ is ∪

4
REVIEW 4 All possible assignments All safe assignments All fixed point solutions Meet Over Paths Assignment Maximum Fixed Point Least Fixed Point ∀ i, In i = Out i = ⊤ ∀ i, Ini = Outi = ⊥ “Advanced Compiler Techniques”

5
REVIEW 5 Entry B Since f ( x ∧ y ) ≤ f ( x ) ∧ f ( y ), it is as if we added nonexistent paths, but we ’ re safe. f OUT = x OUT = y IN = x ∧ y OUT = f(x ∧ y) In MFP, Values x and y get combined too soon. f(x) f(y) MOP considers paths independen tly and and combines at the last possible moment. OUT = f(x) ∧ f(y) “Advanced Compiler Techniques”

6
Summary of DFA Methods MethodSpeedSimple?StructureBoth Way?Graph Class IterativeO(n 2 )SimpleNoYesAll IntervalO(n 2 )MiddleYes Reducible Balance TreeO(nlogn)ComplicatedYesNoReducible Path Comp.O(nlogn)MiddleSemiYesReducible Node ListO(nlogn)MiddleNoYesReducible Balance PathO(n α (n,n))ComplicatedNo?Reducible GrammarnMiddleYes L(Grammar) High LevelnSimpleYes Parse Trees 6 “Advanced Compiler Techniques”

7
Outline Forms of redundancy global common sub - expression loop invariant partial redundancy expression Lazy Code Motion Algorithm A set of four analysis 7

8
“Advanced Compiler Techniques” Role of PRE Goal : Minimize the number of expression evaluations. Keep the value of evaluation in a temporary variable and use it later. Sources of redundancy : Global common sub - expressions Loop - invariant computations True partial redundancy : an expression is sometimes available, sometimes not 8

9
Partial redundancy elimination One of the most complex dataflow analysis Subsumes common sub - expression elimination and loop invariant code motion Originally proposed in 1979 by Morel and Renvoise, Used a bi - directional dataflow analysis Reformulated by Knoop, Rüthing and Steffen in 1992, Uses a backward dataflow analysis followed by a forward analysis We will discuss this latter formulation 9 “Advanced Compiler Techniques”

10
10 Convention Throughout, assume that neither argument of an expression x + y is modified unless we explicitly assign to x or y. And of course, we assume x + y is the only expression anyone would ever want to compute. Can easily extend this to multiple expressions by using a bit vector lattice. “Advanced Compiler Techniques”

11
Example: Global CSE A common expression may have different values on different paths. On every path reaching p, expression x + y has been computed x, y not overwritten after the expression a = x+yb = x+y c = x+y t = x+y a = t t = x+y b = t c = t 11 a=x+y d=x+y a=x+y x=7 d=x+y a=x+y x=7 f=x+y d=x+y

12
“Advanced Compiler Techniques” Example: Loop-Invariant Code Motion Given an expression ( x + y ) inside a loop, does the value of x + y change inside the loop? is the code executed at least once? t = x+y a = x+y a = t 12

13
“Advanced Compiler Techniques” Example: True Partial Redundancy Can we place calculations of x + y such that no path re - executes the same expression? a = x+y b = x+y t = x+y a = t t = x+y b = t 13

14
14 Modifying the Flow Graph We could : 1. Add a new block along an edge. Only necessary if the edge enters a block with several predecessors. 2. Duplicate blocks so an expression x + y is evaluated only along paths where it is needed. “Advanced Compiler Techniques”

15
Example: Node Splitting 15 t = x+y = t = x+y t = x+y “Advanced Compiler Techniques”

16
Can All Redundancy Be Eliminated? Critical edges source basic block has multiple successors destination basic block has multiple predecessors 16

17
“Advanced Compiler Techniques” Code Duplication 17

18
“Advanced Compiler Techniques” Problem With Node-Splitting Can exponentiate the number of nodes. Our PRE algorithm needs to move code to new blocks along edges, but will not split blocks. Convention : All new instructions are either inserted at the beginning of a block or placed in a new block. 18

19
“Advanced Compiler Techniques” Lazy Code Motion Problem Desired properties of a PRE algorithm All redundant computations of expressions that can be eliminated without code duplication are eliminated. The optimized program does not perform any computation that is not in the original program execution. Expressions are computed at the latest possible time. 19

20
“Advanced Compiler Techniques” Full Redundancy Full redundancy at p : expression a + b redundant on all paths cutset : nodes that separate entry from p cutset contains calculation of a + b a, b, not redefined 20

21
“Advanced Compiler Techniques” Partial Redundancy Partial redundancy at p : redundant on some but not all paths Add operations to create a cutset containing a + b Note : Moving operations up can eliminate redundancy 21

22
“Advanced Compiler Techniques” The Plan 1. Determine for each expression the earliest place ( s ) it can be computed while still being sure that it will be used. 2. Postpone the expressions as long as possible without introducing redundancy. We trade space for time --- an expression can be computed in many places, but never if it is already computed. 22

23
“Advanced Compiler Techniques” The Guarantee No expression is computed at a place where it its value might have been computed previously, and preserved instead. Even along a subset of the possible paths. 23

24
The Plan – (2) 3. Determine those places where it is really necessary to store x + y in a temporary rather than compute it when needed. Example : If x + y is computed in only one place. 24 “Advanced Compiler Techniques”

25
More about the plan Don ’ t introduce / insert new operations didn ’ t exist originally : Anticipate the range of code motion Eliminate as many redundant calculations of an expression as possible, without duplicating code Move it up as early as possible Delay computation as much as possible to minimize register Lifetimes move it down unless it creates redundancy ( lazy code motion ) Remove temporary assignment 25 “Advanced Compiler Techniques”

26
More About the Plan We use four data - flow analysis, in succession, plus some set operations on the results of these analysis. Anticipated Expressions Available Expressions Postponable Expressions Used Expressions After the first, each analysis uses the results of the previous ones. 26

27
“Advanced Compiler Techniques” Assumptions Assume every statement is a basic block Only place statements at the beginning of a basic block Add a basic block for every edge that leads to a basic block with multiple predecessors 27

28
“Advanced Compiler Techniques” Anticipated Expressions Expression x + y is anticipated at a point if x + y is certain to be evaluated along any computation path, before any recomputation of x or y. Copies of an expression must be placed only at program points where the expression is anticipated. The earlier an expression is placed, the more redundancy can be removed 28

29
Example: Anticipated Expressions 29 = x+y x+y is anticipated here and could be computed now rather than later. = x+y x+y is anticipated here, but is also available. No computa- tion is needed. “Advanced Compiler Techniques”

30
Computing Anticipated Expressions Use ( B ) = set of expressions x + y evaluated in B before any assignment to x or y. Def ( B ) = set of expressions one of whose arguments is assigned in B. 30

31
“Advanced Compiler Techniques” Computing Anticipated Expressions Direction = backwards. Join ( or Meet ) = intersection. Boundary condition : IN [ exit ] = ∅. Transfer function : IN [ B ] = ( OUT [ B ] – Def ( B )) ∪ Use ( B ) 31

32
Example: Anticipated Expressions 32 = x+y Anticipated Backwards; Intersection; IN[B] = (OUT[B] – Def(B)) ∪ Use(B) “Advanced Compiler Techniques”

33
33 “Available” Expressions Modification of the usual AE. x + y is “ available ” at a point if either : 1. It is available in the usual sense ; i. e., it has been computed and not killed, or 2. It is anticipated ; i. e., it could be available if we chose to precompute it there. “Advanced Compiler Techniques”

34
34 “Available” Expressions x + y is in Kill ( B ) if x or y is defined, and x + y is not recomputed later in B ( same as previously ). Direction = Forward Meet = intersection. Transfer function : OUT [ B ] = ( IN [ B ] ∪ IN ANTICIPATED [ B ]) – Kill ( B ) “Advanced Compiler Techniques”

35
Earliest Placement x + y is in Earliest [ B ] if it is anticipated at the beginning of B but not “ available ” there. That is : when we compute anticipated expressions, x + y is in IN [ B ], but When we compute “ available ” expressions, x + y is not in IN [ B ]. I. e., x + y is anticipated at B, but not anticipated at OUT of some predecessor. 35 “Advanced Compiler Techniques”

36
Example: Available/Earliest 36 = x+y Anticipated “Available” Earliest = anticipated but not available Forward; Intersection; OUT[B] = (IN[B] ∪ IN ANTICIPATED [B]) – Kill(B) “Advanced Compiler Techniques”

37
37 Postponable Expressions Now, we need to delay the evaluation of expressions as long as possible, but … 1. Not past the use of the expression. 2. Not so far that we wind up computing an expression that is already evaluated. Note viewpoint : It is OK to use code space if we save register use. “Advanced Compiler Techniques”

38
Example t = b+c a = t d = t 38

39
“Advanced Compiler Techniques” Example t = b+c a = t d = t 39

40
Postponable Expressions – (2) x + y is postponable to a point p if on every path from the entry to p : 1. There is a block B for which x + y is in earliest [ B ], and 2. After that block, there is no use of x + y. 40 “Advanced Compiler Techniques”

41
Postponable Expressions – (3) Computed like “ available ” expressions, with two differences : 1. In place of killing an expression ( assigning to one of its arguments ): Use ( B ), the set of expressions used in block B. 2. In place of IN ANTICIPATED [ B ]: earliest [ B ]. 41 “Advanced Compiler Techniques”

42
Postponable Expressions – (4) Direction = forward. Meet = intersection. Transfer function : OUT [ B ] = ( IN [ B ] ∪ earliest [ B ]) – Use ( B ) 42 “Advanced Compiler Techniques”

43
Example: Postponable Expressions 43 = x+y Earliest Postponable Three places to compute x+y Forward; Intersection; OUT[B] = (IN[B] ∪ earliest[B]) – Use(B) “Advanced Compiler Techniques”

44
44 Latest Placement We want to postpone as far as possible. How do we compute the “ winners ” – the blocks such that we can postpone no further? Remember – postponing stops at a use or at a block with another predecessor where x + y is not postponable. “Advanced Compiler Techniques”

45
45 Latest[B] For x + y to be in latest [ B ]: 1. x + y is either in earliest [ B ] or in IN POSTPONABLE [ B ]. I. e., we can place the computation at B. 2. x + y is either used in B or there is some successor of B for which (1) does not hold. I. e., we cannot postpone further along all branches. “Advanced Compiler Techniques”

46
Example: Latest 46 = x+y Earliest Or Postponable to beginning Used Or has a suc- cessor not red. Latest = Blue and red. “Advanced Compiler Techniques”

47
Final Touch – Used Expressions We ’ re now ready to introduce a temporary t to hold the value of expression x + y everywhere. But there is a small glitch : t may be totally unnecessary. E. g., x + y is computed in exactly one place. 47 “Advanced Compiler Techniques”

48
Used Expressions An expression is used at point p if There exists a path leading from p that uses the expression before the value is reevaluated. Essentially liveness analysis for expressions rather than for variables. 48 “Advanced Compiler Techniques”

49
Example: Used 49 = x+y Recall: Latest Used Backwards; Union; IN[B] = (OUT[B] ∪ e-used[B]) – Latest(B) “Advanced Compiler Techniques”

50
50 Used Expressions – (2) used [ B ] = expressions used along some path from the end of B. Direction = backward. Meet = union. Transfer function : IN [ B ] = ( OUT [ B ] ∪ e - used [ B ]) – Latest ( B ) e - used = “ expression is used in B.” “Advanced Compiler Techniques”

51
Rules for Introducing Temporaries If x + y is in both Latest [ B ] and OUT USED [ B ], introduce t = x+y at the beginning of B. 2. If x + y is used in B, but either 1. Is not in Latest [ B ] or 2. Is in OUT USED [ B ], replace the use ( s ) of x + y by uses of t. “Advanced Compiler Techniques”

52
Example: Where is a Temporary Used? 52 = x+y Recall: Latest Recall OUT USED Create temp- orary here Use it here But not here --- x+y is in Latest and not in OUT USED “Advanced Compiler Techniques”

53
Example: Here’s Where t is Used 53 = x+y t = x+y = t t = x+y “Advanced Compiler Techniques”

54
Summary Cannot execute any operations not executed originally Pass 1: Anticipation : range of code motion Eliminate as many redundant calculations of an expression as possible, without duplicating code Pass 2: Availability : move it up as early as possible Delay computation as much as possible to minimize register lifetimes Pass 3: Postponable : move it down unless it creates redundancy ( lazy code motion ) Pass 4: Remove temporary assignment 54

55
Data Flow Analysis of PRE 55 “Advanced Compiler Techniques”

56
Data Flow Analysis of PRE 56 “Advanced Compiler Techniques”

57
Algorithm of Lazy Code Motion INPUT : A flow graph for which e_useB and e_killB have been computed for each block B. OUTPUT : A modified flow graph satisfying the four lazy code motion conditions. METHOD : Insert an empty block along all edges entering a block with more than one predecessor Find anticipated [ B ]. in for all blocks B Find available [ B ]. in for all blocks B Compute the earliest placements for all blocks B Find postponable [ B ]. in for all blocks B Compute the latest placements for all blocks B Find used [ B ]. out for all blocks B 57 “Advanced Compiler Techniques”

58
Algorithm of Lazy Code Motion METHOD : 8. For each expression, say x + y, computed by the program, do the following : Create a new temporary, say t, for x + y. For all blocks B such that x + y is in latest [ B ] ∩ used [ B ]. out, add t = x + y at the beginning of B. For all blocks B such that x + y is in e_use B ∩ ( ┐latest [ B ] ∪ used. out [ B ] ) replace every original x + y by t. 58 “Advanced Compiler Techniques”

59
Example 59 “Advanced Compiler Techniques”

60
More about PRE Don ’ t need heuristic Dhamdhere, Drechsler - Stadel, Knoop, et. al. use restricted flow graph or allow edge placements. Data flow can be separated into unidirectional passes Dhamdhere, Knoop, et. al. Improvement still tied to accuracy of computational model Assumes performance depends only on the number of computations along any path. Ignores resource constraint issues : register alloc., etc. Knoop, et. al. give “ earliest ” and “ latest ” placement algorithms which begin to address this. Further issues : more than one expression at once, strength reduction, redundant assignments, redundant stores With GVN, SSA… 60 “Advanced Compiler Techniques”

61
Next Time Homework ， Loops Dragon Book : §

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google