170 likes | 314 Views
CS 201 Compiler Construction. Lecture 11 Machine Code Generation. Code Generation – Naïve Approach. Load A, R1 ADD C, R1 Store R1, A Load A, R1 -- redundant ADD B, R1 Store R1, M. A=B+C M=A+B.
E N D
CS 201Compiler Construction Lecture 11 Machine Code Generation
Code Generation – Naïve Approach Load A, R1 ADD C, R1 Store R1, A Load A, R1 -- redundant ADD B, R1 Store R1, M A=B+C M=A+B Macro expand each intermediate code statement into a sequence of target machine instructions.
Naïve Approach Contd.. Assumption:All registers are free immediately preceding and following an intermediate code statement. + each intermediate code statement can be translated independently • code is of poor quality – too many loads and stores. To obtain good quality code multiple intermediate code statements should be examined together and register allocation and assignment should be performed to minimize loads and stores.
Register Allocation & Assignment Local Register Allocation: Examine each basic block independently, i.e. all register are free at basic block boundaries. Global Register Allocation: Examine the code for each function independently, i.e. all registers are free at function boundaries. Allocation: What variables should be put into registers? Assignment: What specific registers should be assigned at each program point?
Register Allocation & Assignment • Generating code for A=B+C assuming B is in R1 • ADD C,R1 – neither B or C available in register • Load C, R2 – C available in R2; B not available • ADD R2, R1 • 3. Load C, R2 – C not available; B available in R1 • ADD R1, R2 • Usage of B and C after A=B+C determines the preferable choice.
Order of Statements Matters 1.Load A, R1 1.Add B, R1 2.Load X, R2 2.Mult Z, R2 Store R1, T1 3.Load C, R1 3.Sub R2, R1 Load T1, R2 4.Sub R1, R2 4.Store R2, T4 1.Load X, R1 1.Mult Z, R1 2.Load C, R2 2.Sub R1, R2 3.Load A, R1 3.Add B, R1 4.Sub R2, R1 4.Store R1, T4 1.T1 = A + B 2.T2 = X * Z 3.T3 = C - T2 4.T4 = T1 – T3 1.T2 = X * Z 2.T3 = C - T2 3.T1 = A + B 4.T4 = T1 – T3 Spill Code (A+B) – (C – X*Z)
Order of Statements Contd… The ordering of statements effects maximum number of simultaneously live variables and hence the minimum number of registers needed to avoid spill code. Rest of the discussion: + Code reordering within basic blocks + Global register allocation
Reordering Code Statements DAG – Directed Acyclic Graph A representation to assist in code reordering. + Nodes are operations + Edges represent dependences Nodes are labeled as follows: • Leaves with variables or constants – subscript 0 is used to distinguish initial value of the variable from other values. • Interior nodes with operators and list of variables whose values are computed by the node.
DAGs Contd.. DAGs are useful for: • Removing common local sub-expressions. • Renaming temporaries. • Finding names used inside the block but evaluated outside. • Finding statements in the block that could have their computed values used outside the block. • Statements that can be reordered (or executed in parallel).
DAG Construction Given a function node(identifier) – it returns the node currently associated with the identifier. Process one statement at a time as follows: Statement types: x = y op z; x = op y; x = y; • If node(y) is undefined then create a leaf labeled yo and this is now node(y) in case of x = y op z; do the same for z. • Determine if there is a node labeled “op” with node(y) & node(z) as the left and right children in case of x = y op z.
DAG Construction Contd.. 2 Contd. In case of x = op y, check if there is a node labeled “op” with single child node(y). If not, create such a node. Let the node found or created by n. • Delete x from the list of identifiers attached to node(x) & append x to the list of identifiers attached to node n. Set node(x) to n.
Example of DAG Construction T1 = J * 2 T2 = X[T1] T3 = T2 + N T4 = J * 2 T5 = Y[T4] T6 = T5 + T3 ANS = T6 T7 = J+ 1 J = T7 ANS = J If J<10 goto L
Code Reordering DAG captures all legal re-orderings. Which ordering should we select? Try to place computation & use of a value next to each other so that the register used by the value is freed immediately. Next we will look at a heuristic that attempts, as far as possible, to make the evaluation of a node immediately follow the evaluation of the leftmost argument.
Code Reordering Contd.. Algorithm: List nodes in reverse order. While unlisted interior nodes remain Do select an unlisted node n, all of whose parents have been listed list n While leftmost child m of n has no unlisted parents and is not a leaf Do list m n = m End while End while