Download - Example in SSA
![Page 1: Example in SSA](https://reader035.vdocument.in/reader035/viewer/2022062409/56814830550346895db5509a/html5/thumbnails/1.jpg)
Example in SSA
X := Y op Z
in
out
FX := Y op Z(in) = in [ { X ! Y op Z }
X := (Y,Z)
in0
out
FX := (in0, in1) = (in0 Å in1 ) [ { X ! E | Y ! E 2 in0 Æ Z ! E 2 in1 }
in1
![Page 2: Example in SSA](https://reader035.vdocument.in/reader035/viewer/2022062409/56814830550346895db5509a/html5/thumbnails/2.jpg)
Example in SSA
![Page 3: Example in SSA](https://reader035.vdocument.in/reader035/viewer/2022062409/56814830550346895db5509a/html5/thumbnails/3.jpg)
Example in SSA
![Page 4: Example in SSA](https://reader035.vdocument.in/reader035/viewer/2022062409/56814830550346895db5509a/html5/thumbnails/4.jpg)
What about pointers?
• Pointers complicate SSA. Several options.
• Option 1: don’t use SSA for pointed to variables
• Option 2: adapt SSA to account for pointers
• Option 3: define src language so that variables cannot be pointed to (eg: Java)
![Page 5: Example in SSA](https://reader035.vdocument.in/reader035/viewer/2022062409/56814830550346895db5509a/html5/thumbnails/5.jpg)
SSA helps us with CSE
• Let’s see what else SSA can help us with
• Loop-invariant code motion
![Page 6: Example in SSA](https://reader035.vdocument.in/reader035/viewer/2022062409/56814830550346895db5509a/html5/thumbnails/6.jpg)
Loop-invariant code motion
• Two steps: analysis and transformations
• Step1: find invariant computations in loop– invariant: computes same result each time evaluated
• Step 2: move them outside loop– to top if used within loop: code hoisting– to bottom if used after loop: code sinking
![Page 7: Example in SSA](https://reader035.vdocument.in/reader035/viewer/2022062409/56814830550346895db5509a/html5/thumbnails/7.jpg)
Example
![Page 8: Example in SSA](https://reader035.vdocument.in/reader035/viewer/2022062409/56814830550346895db5509a/html5/thumbnails/8.jpg)
Example
![Page 9: Example in SSA](https://reader035.vdocument.in/reader035/viewer/2022062409/56814830550346895db5509a/html5/thumbnails/9.jpg)
Detecting loop invariants
• An expression is invariant in a loop L iff:
(base cases)– it’s a constant– it’s a variable use, all of whose defs are outside of L
(inductive cases)– it’s a pure computation all of whose args are loop-
invariant– it’s a variable use with only one reaching def, and the
rhs of that def is loop-invariant
![Page 10: Example in SSA](https://reader035.vdocument.in/reader035/viewer/2022062409/56814830550346895db5509a/html5/thumbnails/10.jpg)
Computing loop invariants
• Option 1: iterative dataflow analysis– optimistically assume all expressions loop-invariant,
and propagate
• Option 2: build def/use chains– follow chains to identify and propagate invariant
expressions
• Option 3: SSA– like option 2, but using SSA instead of ef/use chains
![Page 11: Example in SSA](https://reader035.vdocument.in/reader035/viewer/2022062409/56814830550346895db5509a/html5/thumbnails/11.jpg)
Example using def/use chains
• An expression is invariant in a loop L iff:
(base cases)– it’s a constant– it’s a variable use, all of
whose defs are outside of L
(inductive cases)– it’s a pure computation all of
whose args are loop-invariant– it’s a variable use with only
one reaching def, and the rhs of that def is loop-invariant
![Page 12: Example in SSA](https://reader035.vdocument.in/reader035/viewer/2022062409/56814830550346895db5509a/html5/thumbnails/12.jpg)
Example using def/use chains
• An expression is invariant in a loop L iff:
(base cases)– it’s a constant– it’s a variable use, all of
whose defs are outside of L
(inductive cases)– it’s a pure computation all of
whose args are loop-invariant– it’s a variable use with only
one reaching def, and the rhs of that def is loop-invariant
![Page 13: Example in SSA](https://reader035.vdocument.in/reader035/viewer/2022062409/56814830550346895db5509a/html5/thumbnails/13.jpg)
Example using def/use chains
• An expression is invariant in a loop L iff:
(base cases)– it’s a constant– it’s a variable use, all of
whose defs are outside of L
(inductive cases)– it’s a pure computation all of
whose args are loop-invariant– it’s a variable use with only
one reaching def, and the rhs of that def is loop-invariant
![Page 14: Example in SSA](https://reader035.vdocument.in/reader035/viewer/2022062409/56814830550346895db5509a/html5/thumbnails/14.jpg)
Loop invariant detection using SSA
• An expression is invariant in a loop L iff:
(base cases)– it’s a constant– it’s a variable use, all of whose single defs are outside
of L
(inductive cases)– it’s a pure computation all of whose args are loop-
invariant– it’s a variable use whose single reaching def, and the
rhs of that def is loop-invariant
• functions are not pure
![Page 15: Example in SSA](https://reader035.vdocument.in/reader035/viewer/2022062409/56814830550346895db5509a/html5/thumbnails/15.jpg)
Example using SSA
• An expression is invariant in a loop L iff:
(base cases)– it’s a constant– it’s a variable use, all of
whose single defs are outside of L
(inductive cases)– it’s a pure computation all of
whose args are loop-invariant– it’s a variable use whose
single reaching def, and the rhs of that def is loop-invariant
• functions are not pure
![Page 16: Example in SSA](https://reader035.vdocument.in/reader035/viewer/2022062409/56814830550346895db5509a/html5/thumbnails/16.jpg)
Example using SSA and preheader
• An expression is invariant in a loop L iff:
(base cases)– it’s a constant– it’s a variable use, all of
whose single defs are outside of L
(inductive cases)– it’s a pure computation all of
whose args are loop-invariant– it’s a variable use whose
single reaching def, and the rhs of that def is loop-invariant
• functions are not pure
![Page 17: Example in SSA](https://reader035.vdocument.in/reader035/viewer/2022062409/56814830550346895db5509a/html5/thumbnails/17.jpg)
Summary: Loop-invariant code motion
• Two steps: analysis and transformations
• Step1: find invariant computations in loop– invariant: computes same result each time evaluated
• Step 2: move them outside loop– to top if used within loop: code hoisting– to bottom if used after loop: code sinking
![Page 18: Example in SSA](https://reader035.vdocument.in/reader035/viewer/2022062409/56814830550346895db5509a/html5/thumbnails/18.jpg)
Code motion
![Page 19: Example in SSA](https://reader035.vdocument.in/reader035/viewer/2022062409/56814830550346895db5509a/html5/thumbnails/19.jpg)
Example
![Page 20: Example in SSA](https://reader035.vdocument.in/reader035/viewer/2022062409/56814830550346895db5509a/html5/thumbnails/20.jpg)
Lesson from example: domination restriction
![Page 21: Example in SSA](https://reader035.vdocument.in/reader035/viewer/2022062409/56814830550346895db5509a/html5/thumbnails/21.jpg)
Domination restriction in for loops
![Page 22: Example in SSA](https://reader035.vdocument.in/reader035/viewer/2022062409/56814830550346895db5509a/html5/thumbnails/22.jpg)
Domination restriction in for loops
![Page 23: Example in SSA](https://reader035.vdocument.in/reader035/viewer/2022062409/56814830550346895db5509a/html5/thumbnails/23.jpg)
Avoiding domination restriction
![Page 24: Example in SSA](https://reader035.vdocument.in/reader035/viewer/2022062409/56814830550346895db5509a/html5/thumbnails/24.jpg)
Another example
![Page 25: Example in SSA](https://reader035.vdocument.in/reader035/viewer/2022062409/56814830550346895db5509a/html5/thumbnails/25.jpg)
Data dependence restriction
![Page 26: Example in SSA](https://reader035.vdocument.in/reader035/viewer/2022062409/56814830550346895db5509a/html5/thumbnails/26.jpg)
Avoiding data restriction
![Page 27: Example in SSA](https://reader035.vdocument.in/reader035/viewer/2022062409/56814830550346895db5509a/html5/thumbnails/27.jpg)
Summary of Data dependencies
• We’ve seen SSA, a way to encode data dependencies better than just def/use chains– makes CSE easier– makes loop invariant detection easier– makes code motion easier
• Now we move on to looking at how to encode control dependencies
![Page 28: Example in SSA](https://reader035.vdocument.in/reader035/viewer/2022062409/56814830550346895db5509a/html5/thumbnails/28.jpg)
Control Dependencies
• A node (basic block) Y is control-dependent on another X iff X determines whether Y executes– there exists a path from X to Y s.t. every node in the
path other than X and Y is post-dominated by Y– X is not post-dominated by Y
![Page 29: Example in SSA](https://reader035.vdocument.in/reader035/viewer/2022062409/56814830550346895db5509a/html5/thumbnails/29.jpg)
Control Dependencies
• A node (basic block) Y is control-dependent on another X iff X determines whether Y executes– there exists a path from X to Y s.t. every node in the
path other than X and Y is post-dominated by Y– X is not post-dominated by Y
![Page 30: Example in SSA](https://reader035.vdocument.in/reader035/viewer/2022062409/56814830550346895db5509a/html5/thumbnails/30.jpg)
Example
![Page 31: Example in SSA](https://reader035.vdocument.in/reader035/viewer/2022062409/56814830550346895db5509a/html5/thumbnails/31.jpg)
Example
![Page 32: Example in SSA](https://reader035.vdocument.in/reader035/viewer/2022062409/56814830550346895db5509a/html5/thumbnails/32.jpg)
Control Dependence Graph
• Control dependence graph: Y descendent of X iff Y is control dependent on X– label each child edge with required condition– group all children with same condition under region
node
• Program dependence graph: super-impose dataflow graph (in SSA form or not) on top of the control dependence graph
![Page 33: Example in SSA](https://reader035.vdocument.in/reader035/viewer/2022062409/56814830550346895db5509a/html5/thumbnails/33.jpg)
Example
![Page 34: Example in SSA](https://reader035.vdocument.in/reader035/viewer/2022062409/56814830550346895db5509a/html5/thumbnails/34.jpg)
Example