1 / 51

Local Optimizations

Local Optimizations. Code generation. Object x; int a; int b; int c ; x = new Object; a = 4; c = a + b; x.fn (a + b);. Code generation. _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _ Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = 4 ;

rufina
Download Presentation

Local Optimizations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Local Optimizations

  2. Code generation Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  3. Code generation _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = 4 ; a = _tmp3 ; _tmp4 = a + b ; c = _tmp4 ; _tmp5 = a + b ; _tmp6 = *(x) ; _tmp7 = *(_tmp6) ; PushParam _tmp5 ; PushParam x ; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  4. Common SubexpressionElimination • If we have two variable assignments v1 = a op b … v2 = a op b and the values of v1, a, and b have not changed between the assignments, rewrite the code as v1 = a op b … v2 = v1 • Eliminates useless recalculation. • Paves the way for later optimizations.

  5. Common SubexpressionElimination _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = 4 ; a = _tmp3 ; _tmp4 = a + b ; c = _tmp4 ; _tmp5 = a + b ; _tmp6 = *(x) ; _tmp7 = *(_tmp6) ; PushParam _tmp5 ; PushParam x ; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  6. Common SubexpressionElimination _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = 4 ; a = _tmp3 ; _tmp4 = a + b ; c = _tmp4 ; _tmp5 = _tmp4 ; _tmp6 = *(x) ; _tmp7 = *(_tmp6) ; PushParam _tmp5 ; PushParam x ; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  7. Common SubexpressionElimination _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = 4 ; a = _tmp3 ; _tmp4 = a + b ; c = _tmp4 ; _tmp5 = _tmp4 ; _tmp6 = *(x) ; _tmp7 = *(_tmp6) ; PushParam _tmp5 ; PushParam x ; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  8. Common SubexpressionElimination _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0; a = _tmp3 ; _tmp4 = a + b ; c = _tmp4 ; _tmp5 = _tmp4 ; _tmp6 = *(x) ; _tmp7 = *(_tmp6) ; PushParam _tmp5 ; PushParam x ; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  9. Common SubexpressionElimination _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0; a = _tmp3 ; _tmp4 = a + b ; c = _tmp4 ; _tmp5 = _tmp4 ; _tmp6 = *(x) ; _tmp7 = *(_tmp6) ; PushParam _tmp5 ; PushParam x ; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  10. Common SubexpressionElimination _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0; a = _tmp3 ; _tmp4 = a + b ; c = _tmp4 ; _tmp5 = c; _tmp6 = *(x) ; _tmp7 = *(_tmp6) ; PushParam _tmp5 ; PushParam x ; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  11. Common SubexpressionElimination _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0; a = _tmp3 ; _tmp4 = a + b ; c = _tmp4 ; _tmp5 = c; _tmp6 = *(x) ; _tmp7 = *(_tmp6) ; PushParam _tmp5 ; PushParam x ; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  12. Copy Propagation • If we have a variable assignment v1 = v2 then as long as v1 and v2 are not reassigned, we can rewrite expressions of the form a = … v1 … as a = … v2 … provided that such a rewrite is legal. • This will help immensely later on, as you'll see.

  13. Copy Propagation _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0; a = _tmp3 ; _tmp4 = a + b ; c = _tmp4 ; _tmp5 = c; _tmp6 = *(x) ; _tmp7 = *(_tmp6) ; PushParam _tmp5 ; PushParam x ; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  14. Copy Propagation _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0; a = _tmp3 ; _tmp4 = a + b ; c = _tmp4 ; _tmp5 = c; _tmp6 = *(x) ; _tmp7 = *(_tmp6) ; PushParam _tmp5 ; PushParamx ; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  15. Copy Propagation _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0; a = _tmp3 ; _tmp4 = a + b ; c = _tmp4 ; _tmp5 = c; _tmp6 = *(_tmp1 ) ; _tmp7 = *(_tmp6) ; PushParam _tmp5 ; PushParam_tmp1; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  16. Copy Propagation _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0; a = _tmp3 ; _tmp4 = a + b ; c = _tmp4 ; _tmp5 = c; _tmp6 = *(_tmp1 ) ; _tmp7 = *(_tmp6) ; PushParam _tmp5 ; PushParam_tmp1; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  17. Copy Propagation _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0; a = _tmp3 ; _tmp4 = _tmp3+ b ; c = _tmp4 ; _tmp5 = c; _tmp6 = *(_tmp1 ) ; _tmp7 = *(_tmp6) ; PushParam _tmp5 ; PushParam_tmp1; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  18. Copy Propagation _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0; a = _tmp3 ; _tmp4 = _tmp3+ b ; c = _tmp4 ; _tmp5 = c; _tmp6 = *(_tmp1 ) ; _tmp7 = *(_tmp6) ; PushParam_tmp5 ; PushParam_tmp1; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  19. Copy Propagation _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0; a = _tmp3 ; _tmp4 = _tmp3+ b ; c = _tmp4 ; _tmp5 = c; _tmp6 = *(_tmp1 ) ; _tmp7 = *(_tmp6) ; PushParamc; PushParam_tmp1; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  20. Copy Propagation _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0; a = _tmp3 ; _tmp4 = _tmp3+ b ; c = _tmp4 ; _tmp5 = c; _tmp6 = *(_tmp1) ; _tmp7 = *(_tmp6) ; PushParamc; PushParam_tmp1; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  21. Copy Propagation _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0; a = _tmp3 ; _tmp4 = _tmp3+ b ; c = _tmp4 ; _tmp5 = c; _tmp6 = _tmp2 ; _tmp7 = *(_tmp6) ; PushParamc; PushParam_tmp1; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  22. Copy Propagation _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0; a = _tmp3 ; _tmp4 = _tmp3+ b ; c = _tmp4 ; _tmp5 = c; _tmp6 = _tmp2 ; _tmp7 = *(_tmp6) ; PushParamc; PushParam_tmp1; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  23. Copy Propagation _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0; a = _tmp3 ; _tmp4 = _tmp3+ b ; c = _tmp4 ; _tmp5 = c; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2 ) ; PushParamc; PushParam_tmp1; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  24. Copy Propagation _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0; a = _tmp3 ; _tmp4 = _tmp3+ b ; c = _tmp4 ; _tmp5 = c; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2 ) ; PushParamc; PushParam_tmp1; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  25. Copy Propagation _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0; a = _tmp0 ; _tmp4 = _tmp0+ b ; c = _tmp4 ; _tmp5 = c; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2 ) ; PushParamc; PushParam_tmp1; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  26. Copy Propagation _tmp0 = 4 ; PushParam_tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = _tmp0; a = _tmp0 ; _tmp4 =_tmp0+ b ; c = _tmp4 ; _tmp5 = c; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2 ) ; PushParamc; PushParam_tmp1; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  27. Copy Propagation _tmp0 = 4 ; PushParam_tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = 4 ; a = 4 ; _tmp4 =_tmp0+ b ; c = _tmp4 ; _tmp5 = c; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2 ) ; PushParamc; PushParam_tmp1; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  28. Copy Propagation _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = 4 ; a = 4; _tmp4 =_tmp0+ b ; c = _tmp4 ; _tmp5 = c; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2 ) ; PushParamc; PushParam_tmp1; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  29. Copy Propagation _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = 4 ; a = 4; _tmp4 =_tmp0+ b ; c = _tmp4 ; _tmp5 = _tmp4 ; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2 ) ; PushParam_tmp4 ; PushParam_tmp1; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  30. Copy Propagation _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = 4 ; a = 4; _tmp4 =_tmp0+ b ; c = _tmp4 ; _tmp5 = _tmp4 ; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2 ) ; PushParam_tmp4 ; PushParam_tmp1; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  31. Dead Code Elimination • An assignment to a variable v is called dead if the value of that assignment is never read anywhere. • Dead code elimination removes dead assignments from IR. • Determining whether an assignment is dead depends on what variable is being assigned to and when it's being assigned.

  32. Dead Code Elimination _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = 4 ; a = 4; _tmp4 =_tmp0+ b ; c = _tmp4 ; _tmp5 = _tmp4 ; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2 ) ; PushParam_tmp4 ; PushParam_tmp1; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  33. Dead Code Elimination _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; x = _tmp1 ; _tmp3 = 4 ; a = 4; _tmp4 =_tmp0+ b ; c = _tmp4 ; _tmp5 = _tmp4 ; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2 ) ; PushParam_tmp4 ; PushParam_tmp1; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  34. Dead Code Elimination _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; _tmp3 = 4 ; a = 4; _tmp4 =_tmp0+ b ; c = _tmp4 ; _tmp5 = _tmp4 ; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2 ) ; PushParam_tmp4 ; PushParam_tmp1; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  35. Dead Code Elimination _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; _tmp3 = 4 ; a = 4; _tmp4 =_tmp0+ b ; c = _tmp4 ; _tmp5 = _tmp4 ; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2 ) ; PushParam_tmp4 ; PushParam_tmp1; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  36. Dead Code Elimination _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; a = 4; _tmp4 =_tmp0+ b ; c = _tmp4 ; _tmp5 = _tmp4 ; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2 ) ; PushParam_tmp4 ; PushParam_tmp1; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  37. Dead Code Elimination _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; a = 4 ; _tmp4 =_tmp0+ b ; c = _tmp4 ; _tmp5 = _tmp4 ; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2 ) ; PushParam_tmp4 ; PushParam_tmp1; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  38. Dead Code Elimination _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; _tmp4 =_tmp0+ b ; c = _tmp4 ; _tmp5 = _tmp4 ; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2 ) ; PushParam_tmp4 ; PushParam_tmp1; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  39. Dead Code Elimination _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; _tmp4 =_tmp0+ b ; c = _tmp4 ; _tmp5 = _tmp4 ; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2 ) ; PushParam_tmp4 ; PushParam_tmp1; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  40. Dead Code Elimination _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; _tmp4 =_tmp0+ b ; _tmp5 = _tmp4 ; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2 ) ; PushParam_tmp4 ; PushParam_tmp1; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  41. Dead Code Elimination _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; _tmp4 =_tmp0+ b ; _tmp5 = _tmp4 ; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2 ) ; PushParam_tmp4 ; PushParam_tmp1; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  42. Dead Code Elimination _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; _tmp4 =_tmp0+ b ; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2 ) ; PushParam_tmp4 ; PushParam_tmp1; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  43. Dead Code Elimination _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; _tmp4 =_tmp0+ b ; _tmp6 = _tmp2 ; _tmp7 = *(_tmp2 ) ; PushParam_tmp4 ; PushParam_tmp1; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  44. Dead Code Elimination _tmp0 = 4 ; PushParam _tmp0 ; _tmp1 = LCall _Alloc ; PopParams 4 ; _tmp2 = Object ; *(_tmp1) = _tmp2 ; _tmp4 =_tmp0+ b ; _tmp7 = *(_tmp2 ) ; PushParam_tmp4 ; PushParam_tmp1; ACall _tmp7 ; PopParams 8 ; Object x; int a; int b; int c; x = new Object; a = 4; c = a + b; x.fn(a + b);

  45. Register allocation • When generating code for arithmetic expressions, the compiler has to decide which is the best way to translate the expression in terms of number of instructions used as well as number of registers needed to evaluate a certain subtree. • Especially in the case that free registers are scarce, the order of evaluation can be important to the length of the generated code, because different orderings may lead to larger or smaller numbers of intermediate values being spilled to memory and then restored.

  46. Simple Sethi–Ullman algorithm Traverse the abstract syntax tree in pre- or postorder: • For every non-constant leaf node, assign a 1 (i.e. 1 register is needed to hold the variable/field/etc.). For every constant leaf node (RHS of an operation – literals, values), assign a 0. • For every non-leaf node n, assign the number of registers needed to evaluate the respective subtrees of n. • If the number of registers needed in the left subtree (l) are not equal to the number of registers needed in the right subtree (r), the number of registers needed for the current node n is max(l, r). • If l == r, then the number of registers needed for the current node is l + 1.

  47. Simple Sethi–Ullman algorithm

  48. Simple Sethi–Ullman algorithm

  49. Simple Sethi–Ullman algorithm Code emission: • If the number of registers needed to compute the left subtree of node n is bigger than the number of registers for the right subtree, then the left subtree is evaluated first (since it may be possible that the one more register needed by the right subtree to save the result makes the left subtreespill). • If the right subtree needs more registers than the left subtree, the right subtree is evaluated first accordingly. If both subtrees need equal as much registers, then the order of evaluation is irrelevant.

  50. Simple Sethi–Ullman algorithm * + + + * d 3 b c f g

More Related