![Page 1: More Code Generation and Optimizationmorin/teaching/3002/notes/various.pdf · 2009-11-16 · Basic Block Example a := b + c b := b - d c := c + d e := b + c • Draw the DAG for this](https://reader034.vdocument.in/reader034/viewer/2022042213/5eb67a6a84caca306e212eef/html5/thumbnails/1.jpg)
More Code Generationand Optimization
Pat Morin
COMP 3002
![Page 2: More Code Generation and Optimizationmorin/teaching/3002/notes/various.pdf · 2009-11-16 · Basic Block Example a := b + c b := b - d c := c + d e := b + c • Draw the DAG for this](https://reader034.vdocument.in/reader034/viewer/2022042213/5eb67a6a84caca306e212eef/html5/thumbnails/2.jpg)
2
Outline
• DAG representation of basic blocks
• Peephole optimization
• Register allocation by graph coloring
![Page 3: More Code Generation and Optimizationmorin/teaching/3002/notes/various.pdf · 2009-11-16 · Basic Block Example a := b + c b := b - d c := c + d e := b + c • Draw the DAG for this](https://reader034.vdocument.in/reader034/viewer/2022042213/5eb67a6a84caca306e212eef/html5/thumbnails/3.jpg)
3
Basic Blocks as DAGs
![Page 4: More Code Generation and Optimizationmorin/teaching/3002/notes/various.pdf · 2009-11-16 · Basic Block Example a := b + c b := b - d c := c + d e := b + c • Draw the DAG for this](https://reader034.vdocument.in/reader034/viewer/2022042213/5eb67a6a84caca306e212eef/html5/thumbnails/4.jpg)
4
Basic Blocks as DAGs• Like expressions, we can represent basic
blocks as DAGs– Create a node for the initial value of each variable
that appears in the block– For each statement s, create a node N whose
children are the nodes defining the operands of s– Each node N is labelled by the operation used– Each node N has a list of variables that it defines– A node N is marked as an output node if its value is
used outside the basic block
![Page 5: More Code Generation and Optimizationmorin/teaching/3002/notes/various.pdf · 2009-11-16 · Basic Block Example a := b + c b := b - d c := c + d e := b + c • Draw the DAG for this](https://reader034.vdocument.in/reader034/viewer/2022042213/5eb67a6a84caca306e212eef/html5/thumbnails/5.jpg)
5
Basic Block Example
a := b + cb := a - dc := b + cd := a - d
b0 c0d0
+ (a)
- (b,d)
+ (c)
![Page 6: More Code Generation and Optimizationmorin/teaching/3002/notes/various.pdf · 2009-11-16 · Basic Block Example a := b + c b := b - d c := c + d e := b + c • Draw the DAG for this](https://reader034.vdocument.in/reader034/viewer/2022042213/5eb67a6a84caca306e212eef/html5/thumbnails/6.jpg)
6
Basic Block Example
a := b + cb := b - dc := c + de := b + c
• Draw the DAG for this basic block
![Page 7: More Code Generation and Optimizationmorin/teaching/3002/notes/various.pdf · 2009-11-16 · Basic Block Example a := b + c b := b - d c := c + d e := b + c • Draw the DAG for this](https://reader034.vdocument.in/reader034/viewer/2022042213/5eb67a6a84caca306e212eef/html5/thumbnails/7.jpg)
7
Dead Code Elimination
• Dead code is code that computes a value that is never used
• Inspecting the flow graph we can tell if the value of a variable computed within a basic block is used outside that block– These are called live variables or live on exit
• Algorithm– While some node N has no ancestor and no live
variable attached• Delete N and its outgoing edges
![Page 8: More Code Generation and Optimizationmorin/teaching/3002/notes/various.pdf · 2009-11-16 · Basic Block Example a := b + c b := b - d c := c + d e := b + c • Draw the DAG for this](https://reader034.vdocument.in/reader034/viewer/2022042213/5eb67a6a84caca306e212eef/html5/thumbnails/8.jpg)
8
Dead Code Elimination - Example• Remove dead code assuming c is the only
live variable
b0 c0d0
+ (a)
- (b,d)
+ (c)
+ (x)
+ (y)
![Page 9: More Code Generation and Optimizationmorin/teaching/3002/notes/various.pdf · 2009-11-16 · Basic Block Example a := b + c b := b - d c := c + d e := b + c • Draw the DAG for this](https://reader034.vdocument.in/reader034/viewer/2022042213/5eb67a6a84caca306e212eef/html5/thumbnails/9.jpg)
9
Using Algebraic Identities• The DAG representation can allow constant
folding– When a subexpression involves 2 or more constants
its value can sometimes be computed at compile time
• Commutativity: x * y = y * x– When creating a node for x * y we should check if
there already exists a node named x * y or a node named y * x
• Associativity: (a + b) + c = a + (b + c)
![Page 10: More Code Generation and Optimizationmorin/teaching/3002/notes/various.pdf · 2009-11-16 · Basic Block Example a := b + c b := b - d c := c + d e := b + c • Draw the DAG for this](https://reader034.vdocument.in/reader034/viewer/2022042213/5eb67a6a84caca306e212eef/html5/thumbnails/10.jpg)
10
Associativity
Source Code:a = b + c;e + c + d + b;
3-AI (slow):a := b + ct := c + de := t + b
3-AI (fast):a := b + ce := a + b
• An example of using associativity
![Page 11: More Code Generation and Optimizationmorin/teaching/3002/notes/various.pdf · 2009-11-16 · Basic Block Example a := b + c b := b - d c := c + d e := b + c • Draw the DAG for this](https://reader034.vdocument.in/reader034/viewer/2022042213/5eb67a6a84caca306e212eef/html5/thumbnails/11.jpg)
11
Array References• Array references are a different kind of
operator
• Assigning to an array location can change any element of the array
x = a[i]a[j] = yz = a[i]
a0 i0
=[] (x,z)
j0
[]= (a[j])
y0
WRONG
![Page 12: More Code Generation and Optimizationmorin/teaching/3002/notes/various.pdf · 2009-11-16 · Basic Block Example a := b + c b := b - d c := c + d e := b + c • Draw the DAG for this](https://reader034.vdocument.in/reader034/viewer/2022042213/5eb67a6a84caca306e212eef/html5/thumbnails/12.jpg)
12
Arrays References• The right way
– x = a[i] creates a node with two children (a and i) that defines x
– a[j] = y creates a node with 3 children• It has the side-effect of killing any node whose
value depends on a
x = a[i]a[j] = yz = a[i]
a0 i0
=[] (x)
j0
[]=
y0
=[] z
![Page 13: More Code Generation and Optimizationmorin/teaching/3002/notes/various.pdf · 2009-11-16 · Basic Block Example a := b + c b := b - d c := c + d e := b + c • Draw the DAG for this](https://reader034.vdocument.in/reader034/viewer/2022042213/5eb67a6a84caca306e212eef/html5/thumbnails/13.jpg)
13
Pointer References• Pointer references are similar to array
references– assigning to a pointer kills all nodes– changing any variable kills all pointer references
x = *pt = a + bz = *p ; maybe p points to t*q = y x = a + b ; maybe q points to a or b
![Page 14: More Code Generation and Optimizationmorin/teaching/3002/notes/various.pdf · 2009-11-16 · Basic Block Example a := b + c b := b - d c := c + d e := b + c • Draw the DAG for this](https://reader034.vdocument.in/reader034/viewer/2022042213/5eb67a6a84caca306e212eef/html5/thumbnails/14.jpg)
14
Reassembling Basic Blocks• After converting a basic block into a DAG
and applying optimizations we must reassemble the basic block
• Rules:1)The order of instructions must obey the order of the
DAG2)Assigning to an array must follow any previous
assignments to the same array3)Evaluations of an array must follow any previous
assignments to the same array4)Any use of a variable must follow any previous
indirect (pointer) assignments5)Any indirect assignment must follow all previous
evaluations of variables
![Page 15: More Code Generation and Optimizationmorin/teaching/3002/notes/various.pdf · 2009-11-16 · Basic Block Example a := b + c b := b - d c := c + d e := b + c • Draw the DAG for this](https://reader034.vdocument.in/reader034/viewer/2022042213/5eb67a6a84caca306e212eef/html5/thumbnails/15.jpg)
15
Rearrangement Order Example
a0 i0
=[] (x)
j0
[]=
y0
=[] zx = a[i]a[j] = yz = a[i]
• Extra (dashed) edges implement rules 2-5
![Page 16: More Code Generation and Optimizationmorin/teaching/3002/notes/various.pdf · 2009-11-16 · Basic Block Example a := b + c b := b - d c := c + d e := b + c • Draw the DAG for this](https://reader034.vdocument.in/reader034/viewer/2022042213/5eb67a6a84caca306e212eef/html5/thumbnails/16.jpg)
16
Basic Blocks as DAGs - Examples• For each of the following lines of C
– x = a + b * c;– x = a / (b+c) - d * (e+f);– x = a[i] + 1;– a[i] = b[c[i]];– *p++ = *q++;
• Write a basic block of 3-address instructions
• Convert the basic block into a DAG
• Convert the DAG back into a basic block
![Page 17: More Code Generation and Optimizationmorin/teaching/3002/notes/various.pdf · 2009-11-16 · Basic Block Example a := b + c b := b - d c := c + d e := b + c • Draw the DAG for this](https://reader034.vdocument.in/reader034/viewer/2022042213/5eb67a6a84caca306e212eef/html5/thumbnails/17.jpg)
17
Peephole Optimization
![Page 18: More Code Generation and Optimizationmorin/teaching/3002/notes/various.pdf · 2009-11-16 · Basic Block Example a := b + c b := b - d c := c + d e := b + c • Draw the DAG for this](https://reader034.vdocument.in/reader034/viewer/2022042213/5eb67a6a84caca306e212eef/html5/thumbnails/18.jpg)
18
Peephole Optimization• Peephole optimization is done by looking at
a small number of lines of code at a time– We are looking at a small piece of code (the
peephole)
• Some rules are applied to simplify or speed up various patterns
• Can be applied to intermediate code or machine code
![Page 19: More Code Generation and Optimizationmorin/teaching/3002/notes/various.pdf · 2009-11-16 · Basic Block Example a := b + c b := b - d c := c + d e := b + c • Draw the DAG for this](https://reader034.vdocument.in/reader034/viewer/2022042213/5eb67a6a84caca306e212eef/html5/thumbnails/19.jpg)
19
Redundant Loads and Stores• An adjacent pair of load and store
operations (to the same variable) is unnecessary and can be eliminatedmov x, R0mov R0, x
fload 4fstore 4
fload 4pop
![Page 20: More Code Generation and Optimizationmorin/teaching/3002/notes/various.pdf · 2009-11-16 · Basic Block Example a := b + c b := b - d c := c + d e := b + c • Draw the DAG for this](https://reader034.vdocument.in/reader034/viewer/2022042213/5eb67a6a84caca306e212eef/html5/thumbnails/20.jpg)
20
Algebraic Simplification and Reduction in Strength• Peephole optimization may recognize
algebraic simplification and reduction in strength– x = x + 1 => inc x [machine instruction]– x = x + 0 => unnecessary– x = x * 1 => unnecessary– x = 2 * x => x = x + x
• To do this in the JVM we need a 3 line peephole
![Page 21: More Code Generation and Optimizationmorin/teaching/3002/notes/various.pdf · 2009-11-16 · Basic Block Example a := b + c b := b - d c := c + d e := b + c • Draw the DAG for this](https://reader034.vdocument.in/reader034/viewer/2022042213/5eb67a6a84caca306e212eef/html5/thumbnails/21.jpg)
21
Unreachable Code• Examining the code surrounding jump
statements can often help
if debug == 1 goto L1 goto L2L1: some debug code...L2:
if debug != 1 goto L2 some debug code...L2:
if 0 != 1 goto L2 some debug code...L2:
goto L2 some debug code...L2:
goto L2L2:
![Page 22: More Code Generation and Optimizationmorin/teaching/3002/notes/various.pdf · 2009-11-16 · Basic Block Example a := b + c b := b - d c := c + d e := b + c • Draw the DAG for this](https://reader034.vdocument.in/reader034/viewer/2022042213/5eb67a6a84caca306e212eef/html5/thumbnails/22.jpg)
22
Flow of Control Optimization
goto L1 ...L1: goto L2
goto L2 ...L1: goto L2
if a < b goto L1 ...L1: goto L2
if a < b goto L2 ...L1: goto L2
goto L1 ...L1: if a < b goto L2L3:
if a < b goto L2 goto L3 ...L3:[saves a goto when a < b]
![Page 23: More Code Generation and Optimizationmorin/teaching/3002/notes/various.pdf · 2009-11-16 · Basic Block Example a := b + c b := b - d c := c + d e := b + c • Draw the DAG for this](https://reader034.vdocument.in/reader034/viewer/2022042213/5eb67a6a84caca306e212eef/html5/thumbnails/23.jpg)
23
Register Allocation by Graph Coloring
![Page 24: More Code Generation and Optimizationmorin/teaching/3002/notes/various.pdf · 2009-11-16 · Basic Block Example a := b + c b := b - d c := c + d e := b + c • Draw the DAG for this](https://reader034.vdocument.in/reader034/viewer/2022042213/5eb67a6a84caca306e212eef/html5/thumbnails/24.jpg)
24
Register Allocation By Graph Coloring• We have seen how to manage registers
within a basic block – lazy load and store algorithm– spill (or flush) a register back to memory only when
necessary
• We have seen how to assign register variables across blocks (savings calculations)
• Both these methods are heuristics
![Page 25: More Code Generation and Optimizationmorin/teaching/3002/notes/various.pdf · 2009-11-16 · Basic Block Example a := b + c b := b - d c := c + d e := b + c • Draw the DAG for this](https://reader034.vdocument.in/reader034/viewer/2022042213/5eb67a6a84caca306e212eef/html5/thumbnails/25.jpg)
25
Register Allocation by Graph Coloring• Step 1: Generate code as if we have
infinitely many registers R1,R2,R3,....– R1, R2, R3,... are just like variable names
• Step 2: Create the register interference graph– Nodes are R1, R2, R3, ....– Two nodes are adjacent if they are active at the
same time
• Step 3: Properly color the register interference graph with the "colors" 1,2,3,...,k where k is as small as possible
![Page 26: More Code Generation and Optimizationmorin/teaching/3002/notes/various.pdf · 2009-11-16 · Basic Block Example a := b + c b := b - d c := c + d e := b + c • Draw the DAG for this](https://reader034.vdocument.in/reader034/viewer/2022042213/5eb67a6a84caca306e212eef/html5/thumbnails/26.jpg)
26
Register Allocation by Graph Coloring• A virtual register Ri that is colored with
color c is assigned to the real register R(c mod r) where r is the number of real registers
• If k <= r then there are no conflicts and every variable gets its own register
• Otherwise, conflicts results in extra loads/stores
![Page 27: More Code Generation and Optimizationmorin/teaching/3002/notes/various.pdf · 2009-11-16 · Basic Block Example a := b + c b := b - d c := c + d e := b + c • Draw the DAG for this](https://reader034.vdocument.in/reader034/viewer/2022042213/5eb67a6a84caca306e212eef/html5/thumbnails/27.jpg)
27
The Register Interference Graph• For each register Ri, determine which lines
of which basic blocks the register Ri is active in
• Ri conflicts with Rj if they are active at the same time
![Page 28: More Code Generation and Optimizationmorin/teaching/3002/notes/various.pdf · 2009-11-16 · Basic Block Example a := b + c b := b - d c := c + d e := b + c • Draw the DAG for this](https://reader034.vdocument.in/reader034/viewer/2022042213/5eb67a6a84caca306e212eef/html5/thumbnails/28.jpg)
28
Coloring a Graph• Coloring a graph with the fewest colors is
NP-hard
• The following heuristic is used to color a graph G=(V,E) with the colors 1,2,3,...– Find the vertex v with smallest degree (say d)– Recursively color the graph G \ {v}– Color v with smallest color not used by one of G's
neighbours
• (Note: v will receive color at most d+1)
![Page 29: More Code Generation and Optimizationmorin/teaching/3002/notes/various.pdf · 2009-11-16 · Basic Block Example a := b + c b := b - d c := c + d e := b + c • Draw the DAG for this](https://reader034.vdocument.in/reader034/viewer/2022042213/5eb67a6a84caca306e212eef/html5/thumbnails/29.jpg)
29
Graph Color Example
![Page 30: More Code Generation and Optimizationmorin/teaching/3002/notes/various.pdf · 2009-11-16 · Basic Block Example a := b + c b := b - d c := c + d e := b + c • Draw the DAG for this](https://reader034.vdocument.in/reader034/viewer/2022042213/5eb67a6a84caca306e212eef/html5/thumbnails/30.jpg)
30
Graph Coloring Example
![Page 31: More Code Generation and Optimizationmorin/teaching/3002/notes/various.pdf · 2009-11-16 · Basic Block Example a := b + c b := b - d c := c + d e := b + c • Draw the DAG for this](https://reader034.vdocument.in/reader034/viewer/2022042213/5eb67a6a84caca306e212eef/html5/thumbnails/31.jpg)
31
Graph Coloring Example
![Page 32: More Code Generation and Optimizationmorin/teaching/3002/notes/various.pdf · 2009-11-16 · Basic Block Example a := b + c b := b - d c := c + d e := b + c • Draw the DAG for this](https://reader034.vdocument.in/reader034/viewer/2022042213/5eb67a6a84caca306e212eef/html5/thumbnails/32.jpg)
32
Notes on Graph Coloring• What we are actually trying to find is a
balanced ordering on the vertices of G– If at most d neighbours of v appear before v in the
ordering then v will be colored with color at most d+1
• Thorup (1995) has shown that structured programming languages generate register interference graphs with small treewidth– Optimally coloring a graph with tree width t can be
done in O(f(t) n) where f(t) is a really fast growing function of t.
– Optimal graph coloring can be done in linear time!
![Page 33: More Code Generation and Optimizationmorin/teaching/3002/notes/various.pdf · 2009-11-16 · Basic Block Example a := b + c b := b - d c := c + d e := b + c • Draw the DAG for this](https://reader034.vdocument.in/reader034/viewer/2022042213/5eb67a6a84caca306e212eef/html5/thumbnails/33.jpg)
33
Summary• We've seen some more code generation
and optimization techniques– Representing basic blocks as DAGs allows:
• Common subexpression elimination• Dead code elimination• Use of algebraic identities• Reordering of instructions
– Peephole optimization allows:• Useless code elimination• Use of algebraic identities and machine idioms
– Register allocation by graph color• Can do "optimal" register allocation