Always-Canonical Intermediate Representation Eric Drew Fritz University of Wisconsin-Milwaukee
Total Page:16
File Type:pdf, Size:1020Kb
University of Wisconsin Milwaukee UWM Digital Commons Theses and Dissertations December 2018 Waddle - Always-canonical Intermediate Representation Eric Drew Fritz University of Wisconsin-Milwaukee Follow this and additional works at: https://dc.uwm.edu/etd Part of the Computer Sciences Commons Recommended Citation Fritz, Eric Drew, "Waddle - Always-canonical Intermediate Representation" (2018). Theses and Dissertations. 1989. https://dc.uwm.edu/etd/1989 This Dissertation is brought to you for free and open access by UWM Digital Commons. It has been accepted for inclusion in Theses and Dissertations by an authorized administrator of UWM Digital Commons. For more information, please contact [email protected]. WADDLE – ALWAYS-CANONICAL INTERMEDIATE REPRESENTATION by Eric Fritz A Dissertation Submitted in Partial Fulfillment of the Requirements for the Degree of Doctor of Philosophy in Engineering at The University of Wisconsin-Milwaukee December 2018 ABSTRACT WADDLE – ALWAYS-CANONICAL INTERMEDIATE REPRESENTATION by Eric Fritz The University of Wisconsin-Milwaukee, 2018 Under the Supervision of Professor John Boyland Program transformations that are able to rely on the presence of canonical properties of the program undergoing optimization can be written to be more robust and efficient than an equivalent but generalized transformation that also handles non-canonical programs. If a canonical property is required but broken earlier in an earlier transformation, it must be rebuilt (often from scratch). This additional work can be a dominating factor in compilation time when many transformations are applied over large programs. This dissertation intro- duces a methodology for constructing program transformations so that the program remains in an always-canonical form as the program is mutated, making only local changes to restore broken properties. ii © Copyright by Eric Fritz, 2018 All Rights Reserved iii TABLE OF CONTENTS Abstract ii List of Figures x 1 Introduction 2 1.1 Motivation ..................................... 2 1.2 Research Contributions .............................. 4 1.3 Organization ................................... 5 2 Preliminaries 6 2.1 Sequences ..................................... 6 2.2 Multisets ..................................... 6 2.3 Labels ....................................... 7 2.4 Control Flow Graph ............................... 8 2.4.1 Reducibility ................................ 9 2.4.2 Induced Trees ............................... 11 2.4.3 Depth-First Spanning Tree ....................... 12 2.5 Domination .................................... 12 2.5.1 Dominator Tree .............................. 13 2.6 Loops ....................................... 13 iv 2.6.1 Loop Nesting ............................... 15 2.6.2 Loop Nesting Forest ........................... 15 2.6.3 Loop Deconstruction ........................... 17 2.6.4 Identification of Reducible Loops .................... 18 2.6.5 Identification of Irreducible Loops .................... 18 3 Internal Representation 20 3.1 Syntax ....................................... 21 3.1.1 Blocks, Functions, and Programs .................... 21 3.1.2 Values and Expressions ......................... 22 3.1.3 Block Components ............................ 23 3.1.3.1 Block Parameters and Implicit Parameters ......... 24 3.1.3.2 Instructions ........................... 26 3.1.3.3 Terminators .......................... 28 3.2 Semantics ..................................... 29 3.2.1 Function Cloning ............................. 29 3.2.2 Environments ............................... 29 3.2.3 Evaluation ................................ 32 3.3 Type System ................................... 38 Proof Appendix 43 3.A Soundness ..................................... 43 4 Properties 50 4.1 Static Single Assignment Form ......................... 50 4.2 Loop-Closed Static Single Assignment Form .................. 53 4.3 Canonical Form .................................. 54 v 5 Related Work 57 5.1 Dominator Tree Construction .......................... 57 5.1.1 Iterative Algorithms ........................... 57 5.1.2 Lengauer-Tarjan Algorithm ....................... 59 5.1.3 Semi-NCA ................................. 61 5.1.4 Linear Time Algorithms ......................... 62 5.2 Dominator Tree Reconstruction ......................... 64 5.2.1 Ramalingam-Reps Algorithm ...................... 65 5.2.2 Dynamic SNCA Algorithm ....................... 66 5.2.3 Depth-Based Heuristic .......................... 68 5.3 SSA Construction ................................. 68 5.4 SSA Reconstruction ............................... 71 6 Transformations 75 6.1 Notation ...................................... 75 6.2 Theorems ..................................... 76 6.2.1 Symmetric Evaluation .......................... 77 6.2.2 Structural Theorems ........................... 81 Proof Appendix 83 6.A Proof Template for Maintenance of Evaluation ................. 83 6.B Symmetric Evaluation .............................. 90 6.B.1 Symmetric Instructions ......................... 90 6.B.2 Symmetric Function Calls ........................ 93 6.B.3 Symmetric Branch ............................ 94 6.B.4 Symmetric Return ............................ 95 vi 6.C Common Lemmas ................................. 97 7 Canonicalization 99 7.1 SSA Reconstruction ............................... 100 7.2 LCSSA Reconstruction .............................. 105 7.3 Edge Set Splitting ................................ 107 7.4 Repairing Violations ............................... 109 7.4.1 Property 4.3.1 – Unique latch ...................... 109 7.4.2 Property 4.3.2 – Dedicated preheader .................. 110 7.4.3 Property 4.3.3 – Dedicated exits .................... 111 Proof Appendix 114 7.A SSA Reconstruction ............................... 114 7.B LCSSA Reconstruction .............................. 125 7.C Edge Set Splitting ................................ 134 7.D Unique Latch ................................... 142 7.E Dedicated Preheader ............................... 143 7.F Dedicated Exits .................................. 148 8 Operations 150 8.1 Block Ejection .................................. 150 8.2 Edge Deletion ................................... 152 8.2.1 Change in Path Multiplicity ....................... 155 8.2.2 Change in Paths ............................. 156 8.3 Loop Duplication ................................. 158 Proof Appendix 162 vii 8.A Block Ejection .................................. 162 8.B Delete Edge .................................... 167 9 Optimizations 177 9.1 Straightening ................................... 177 9.2 If Simplification .................................. 179 9.3 Jump Simplification ............................... 181 9.4 Function Inlining ................................. 185 9.5 Loop Unswitching ................................. 188 9.6 Loop Unrolling .................................. 192 9.7 Loop Peeling ................................... 195 Proof Appendix 198 9.A Straightening ................................... 198 9.B If Simplification .................................. 206 9.C Jump Simplification ............................... 212 9.D Function Inlining ................................. 218 9.E Loop Unswitching ................................. 234 9.F Loop Unrolling .................................. 243 9.G Loop Peeling ................................... 252 10 Evaluation 261 10.1 Source Programs ................................. 261 10.2 Methodology ................................... 263 10.3 Single Pass .................................... 265 10.4 Pass Sequence ................................... 267 viii 11 Future Directions 270 Bibliography 274 Curriculum Vitae 281 ix LIST OF FIGURES 2.1 An example control flow graph containing a loop and a one-armed conditional. Block content is presented in Section 3.1. .................... 9 2.2 The process of collapsing a reducible graph into its limit graph (incomplete). 10 2.3 The process of collapsing an irreducible graph into its limit graph (incom- plete). Transformation (T3) is applied to block b so that transformations (T1) and (T2) can further reduce the graph (incomplete). .......... 11 2.4 A control flow graph with its dominator tree. An edge (b1; b2) in the dominator tree indicates that idom(b2) = b1. ........................ 14 2.5 A control flow graph containing three loops (one nested) shown with itsloop nesting forest. In this example, the set of abstract loops are lb = (b; fb; c; dg; feg), lc = (c; fc; dg; fbg), and le = (e; fe; fg; ;). .................... 16 2.6 An irreducible graph. ............................... 18 3.1 Syntax of values, expressions, and types. .................... 23 3.2 A graph annotated with implicit parameters (the second set of parameters). 26 3.3 Implicit parameters for the graph in Figure 3.2. ................ 26 3.4 The syntax of instructions. ............................ 27 3.5 The syntax of terminators. ............................ 28 3.6 Syntax of terms, environments, nondeterminsm state, and effects. ...... 30 x 3.7 Evaluation rules for instructions excluding call. ................ 33 3.8 Evaluation rules for instructions evaluating abnormally. ............ 34 3.9 Evaluation rules for the invocation of an intrinsic. ............... 35 3.10 Evaluation rules for the switch terminator. .................. 36 3.11 Evaluation rules for the call instruction and return terminator. ...... 37 3.12 Evaluation rule for invoking a program function externally.