263-2810: Advanced Compiler Design 2.0 Sta>c Single Assignment Form Thomas R. Gross Computer Science Department ETH Zurich, Switzerland
2.0 Sta>c Single Assignment IR § SSA: Sta>c single assignment § There is one assignment statement that writes a variable/ field/memory loca>on § Assignment for short: statement, expression, …. § Sta6c: in the source/IR § Single: each statement writes a different variable § SSA makes data dependences explicit 2
Example Instead of x = a + b d = x + 1 b = a + c we have x 1 = a 0 + b 0 d 1 = x 1 + 1 b 1 = a 0 + c 0 3
Example x 1 = a 0 + b 0 d 1 = x 1 + 1 b 1 = a 0 + c 0 § Statement 1 produces a value for statement 2 § Only true dependences recorded § No constraints due to variable names 5
Outline SSA form is used in many produc>on compilers § 2.1 SSA form for straight line code § How to turn a (JavaLi/C/Java/…) program into SSA form § 2.2 Condi>onal statements § 2.3 Benefits of SSA form § 2.4 SSA for well-structured programs § Only selected subset of control flow constructs allowed § “goto”-free programs § C – longjmp & friends § 3.0 SSA for arbitrary programs 6
2.1 SSA for a basic block § Assump>on: Program(method) translated into basic blocks, forest of IR trees for each basic block § E.g., AST or similar IR § All sources and des6na6ons of opera6ons visible § Program wriYen without considera6on of SSA format § Goal: transform one basic block into SSA format § Approach: consider all statements (IR trees) in sequence § Consider each operand § Simplifica>on : consider only method-local scalar operands § Arrays, objects, structs, records: later 7
§ For each variable X: Counter C X § C X ini6alized to 0 at start of method § C X indicates the “current” version § Given a statement or expression D = S ⊗ T or S ⊗ T 1. Lookup C S and C T 1. Yields current version, say S n and T m 2. (for stmts) Increment C D 1. Yields new version (D k ) 3. Replace S, T, D : D k = S n ⊗ T m or S n ⊗ T m 8
Example x = a + b y = x + 2 z = a + 1 x = c * 2 w = x + 1 9
Example x 1 = a 0 + b 0 y 1 = x 1 + 2 z 1 = a 0 + 1 x 2 = c 0 * 2 w 1 = x 2 + 1 10
Example Turn the following example into SSA form (3 min) a 1 = a 0 + x 0 x 1 = a 1 + b 0 c 1 = a 1 + b 0 x 2 = c 1 + 2 a 2 = x 2 + b 0 12
§ Each basic block can be handled that way …. § Only the variable names are changed § Otherwise use trees as before § Could use any other IR § Some>mes variable a j is called a version of a § Need right value for counters C X at the start of a basic block 13
16
Finding the current version § Simple example a = 1; a 1 = 1; if (b ≠ 0) { if (b 0 ≠ 0) { a = 0; a 2 = 0; } } x = a ; x 1 = a ??? ; Can’t use a 1 Can’t use a 2 17
Solu>on: φ func>on § Introduce a “magic” func>on φ § φ func>on delivers the correct version § (in the example) § If (b 0 = 0) : returns a 1 § If (b 0 ≠ 0) : returns a 2 § The func>on picks the correct version depending on the path taken to reach BB2 § More precisely: the path that requires us to use either a 1 or a 2 19
φ func>on § Result (return value) depends on path taken § Result value assigned to a new version of variable § Arguments are possible return values § Different versions of (conceptually) the same variable a 1 = 1; if (b 0 ≠ 0) { a 2 = 0; } a 3 = φ (a 2 , a 1 ) x 1 = a 3 ; 21
23
φ func>on -- Notes § φ func>on appears only on the right hand side of an assignment § φ func>on placed at beginning of basic block § Not mandatory but simplifies reading examples § Simplifies op6miza6ons/transforma6ons § No other statements or expressions appear before φ func6on in a basic block 24
φ func>on -- Notes § φ func>on does not evaluate all arguments § Only the single argument that is returned is evaluated § Why does this maYer? § Precise and fine-grained informa6on 26
29
§ a2 is read (and therefore live) at the end of BB1 § a1 is read (and therefore live) at the end of BB2 § Neither a1 nor a2 is read (live) in BB3 § No need to find register § Only a3 a candidate for register alloca6on 30
Conver>ng to SSA § Given a CFG with ENTRY node, forest of IR trees or AST § Start with ENTRY node § Convert the opera6ons in this basic block § Insert φ func>ons as needed § More on this later § Process next basic block un>l all blocks have been processed 31
Implemen>ng φ func>ons BB0 if (b 0 ≠ 0) BB1 BB2 a 2 = 0 a 1 = 1 BB2 a 3 = φ (a 2 , a 1 ) 32
§ Assign a 1 and a 2 to the same register § Both a 1 and a 2 are allocated to a register and they get the same register § Useful if a 3 is read before it is spilled § Create a temporary t (stored in memory) and insert copy statements a 2 = 0 a 1 = 1 BB1 BB2 t = a 2 t = a 1 BB2 a 3 = t 33
2.3 Benefits of SSA form 1. Some op>miza>ons are easy resp. obvious 2. Efficient representa>on of dependences 34
SSA-based op>miza>ons § Elimina>on of common subexpressions (CSE) § The evalua>on of an expression “ a + b” at point P can be eliminated if “ a + b” is evaluated on all paths leading to P § Must consider all paths § There cannot be an assignment to a or b along any paths aner a+b has been evaluated 35
SSA-based op>miza>ons § Elimina>on of common subexpressions (CSE) § First step: iden6fy common subexpressions t = a + b ; v = a + b ; 36
CSE § Must consider complete program § “All paths ….” § “No assignments to operands …” 37
SSA-based op>miza>ons § SSA form immediately provides the answer t = a i + b j ; v = a m + b n ; If m=i and n=j the expression “a+b” is the same § Candidate for removal 38
Example = a + b; if ( … ) { x = 0; } = a + b; 39
Example = a i + b j ; if ( … ) { x 2 = 0; } = a i + b j ; 40
43
Use-Def (ud) chains § Given a “use” of a variable, would like to know where the value was wrifen (“defined”) § Useful to iden6fy register alloca6on candidates § SSA form makes it easy – there is one defini>on for each variable § Easily maintained mapping 44
Def-Use (du) chains § For a given defini>on, find all uses of the value computed § SSA form makes it easy: can easily iden>fy uses § No extra work needed a i = … = a i + … = a i + a k = … = a k = a m + … 45
SSA efficient representa>on § Consider ud-chains and du-chains § Storage space for links directly propor>onal to the number of uses 46
SSA efficient representa>on § Old: Global dataflow equa>ons are solved by itera>on § Use a bit vector to represent § Variables § Defini6ons § Expressions … 47
Available Expressions: Finding IN(B) and OUT(B) § gen B and kill B capture what happens inside a basic block § Sets of expressions “generated” and “killed”! § We need IN and OUT for each basic block § IN(B) = ∩ Bi, Bi is predecessor of B in CFG OUT(Bi) § OUT(B) = gen B ∪ (IN(B) – kill B ) § N basic blocks, 2×N sets IN / OUT 48
Finding IN(B) and OUT(B) § N basic blocks, 2×N sets IN / OUT § System with 2×N unknowns § Solve by itera6ng un6l a fixed point is found § How to start itera>on? Safe assump6on OUT[ENTRY] = ∅ 49
Finding IN(B) and OUT(B) § Safe assump>on OUT[ENTRY] = ∅ § What about OUT[Bi] for Bi ≠ ENTRY? § For reaching defini6ons, we wanted smallest set of defini6ons that “reach” § OK if we say d reaches but it does not § For available expressions, we want largest set of expressions that “reach” § OK if expr is available but not included in set § So start with a large approxima>on and remove expressions that are clearly not available § OUT[Bi] = U � § U U is the set of all expressions that appear in the program � 50
Finding available expressions OUT[ENTRY] = ∅ Ini6alize OUT[B] = U U for ∀ B ≠ ENTRY while (changes to any OUT(B)) { for (each basic block B ≠ ENTRY) { IN(B) = ∩ Bi, Bi is predecessor of B in CFG OUT(Bi) OUT(B) = gen B ∪ (IN(B) – kill B ) } } 51
Comments § The order of visi>ng nodes of the control flow graph mafers § For speed of convergence, not correctness § Needs sets to hold all expressions that appear in func>on/ method § Possibly large § Bit vector representa6on allows fast implementa6on of set opera6ons § Need mul6-word set representa6on § Compiler may limit size of bit vector § Not all instruc6ons/variables/expressions will be considered 52
SSA efficient representa>on § Common subexpressions iden>fied easily § Cost of representa>on reduced in prac9ce 53
2.4 SSA for well-structured programs § Well-structured programs contain only “nice” control flow constructs § Programming language enforces property that program is well- structured § Syntax-directed transla>on to SSA format § Insert φ func6ons § We say “insert φ node” – a tree node with the φ func6on § Rename variables § Use correct version 54
Prepara>on § Augment symbol table to record for each variable § Current version (integer) § Next version (integer) § We some6mes use the term “version number” 55
Recommend
More recommend