8
Idea of Graph Coloring Register Interference Graph (RIG): – indicates whether there exists a point of time where both variables are live – look at the sets of live variables at all progrma points after running live-variable analysis – if two variables occur together, draw an edge – we aim to assign different registers to such these variables – finding assignment of variables to K registers: corresponds to coloring graph using K colors

9
All we need to do is solve graph coloring problem NP hard In practice, we have heuristics that work for typical graphs If we cannot fit it all variables into registers, perform a spill: store variable into memory and load later when needed y yz x z xz xy r

10
Heuristic for Coloring with K Colors Simplify: If there is a node with less than K neighbors, we will always be able to color it! So we can remove such node from the graph (if it exists, otherwise remove other node) This reduces graph size. It is useful, even though incomplete (e.g. planar can be colored by at most 4 colors, yet can have nodes with many neighbors) y yz x z xz xy r y yz x z xz xy y yz x z xy y x z y z y z z

11
Heuristic for Coloring with K Colors Select Assign colors backwards, adding nodes that were removed If the node was removed because it had
{
"@context": "http://schema.org",
"@type": "ImageObject",
"contentUrl": "http://images.slideplayer.com/11/3336090/slides/slide_11.jpg",
"name": "Heuristic for Coloring with K Colors Select Assign colors backwards, adding nodes that were removed If the node was removed because it had

13
Summary of Heuristic for Coloring Simplify (forward, safe): If there is a node with less than K neighbors, we will always be able to color it! so we can remove it from the graph Potential Spill (forward, speculative): If every node has K or more neighbors, we still remove one of them we mark it as node for potential spilling. Then remove it and continue Select (backward): Assign colors backwards, adding nodes that were removed If we find a node that was spilled, we check if we are lucky, that we can color it. if yes, continue if not, insert instructions to save and load values from memory (actual spill). Restart with new graph (a graph is now easier to color as we killed a variable)

14
Conservative Coalescing Suppose variables tmp1 and tmp2 are both assigned to the same register R and the program has an instruction: tmp2 = tmp1 which moves the value of tmp1 into tmp2. This instruction then becomes R = R which can be simply omitted! How to force a register allocator to assign tmp1 and tmp2 to same register? merge the nodes for tmp1 and tmp2 in the interference graph! this is called coalescing But: if we coalesce non-interfering nodes when there are assignments, then our graph may become more difficult to color, and we may in fact need more registers! Conservative coalescing: coalesce only if merged node of tmp1 and tmp2 will have a small degree so that we are sure that we will be able to color it (e.g. resulting node has degree < K)

23
Typing object-oriented language The goal of our type system is preventing null-pointer exceptions. We introduce for each regular object type T the null- annotated types : T + meaning that a variable of this type may also be null T - meaning that a variable of this type cannot be null Of course, T -
{
"@context": "http://schema.org",
"@type": "ImageObject",
"contentUrl": "http://images.slideplayer.com/11/3336090/slides/slide_23.jpg",
"name": "Typing object-oriented language The goal of our type system is preventing null-pointer exceptions.",
"description": "We introduce for each regular object type T the null- annotated types : T + meaning that a variable of this type may also be null T - meaning that a variable of this type cannot be null Of course, T -

26
Typing object-oriented language Induction proof: P(n): For every tree of height ≤ n, (a) any expression type checking to T - can never be null. (b) If the expression type checks for any type, then it will not raise nullPointerException. P(1): If the rule is for null, then it type checks to T + so (a) is true. (b) is true as well. If the rule is for new, then it type checks to T - and is never null so (a) is true. (b) is true as well.

27
Typing object-oriented language Induction proof: P(n): For every tree of height ≤ n, (a) any expression type checking to T - can never be null. (b) If the expression type checks for any type, then it will not raise nullPointerException. Suppose the rule true for P(n), and let D be a derivation tree of size n+1 of an expressions which type checks. Depending on the last rule applied: if the rule is for methods, then it type checks to T+ so (a) is true. Because P(n)(a) and x type checks to T-, x cannot be null so (b) is true. if the rule is for assignment +, then (a) will be true because we do not change variable which type is T-. (b) is true.

28
Typing object-oriented language Induction proof: P(n): For every tree of height ≤ n, (a) any expression type checking to T - can never be null. (b) If the expression type checks for any type, then it will not raise nullPointerException. Suppose the rule true for P(n), and let D be a derivation tree of size n+1 of an expressions which type checks. Depending on the last rule applied: if the rule is for assignment -, then e will not be null according to P(n) so after assignment x will not be null so (a) is true. (b) is also true. similar proof for assignment with simultaneous declaration. Therefore, P(n+1) and we conclude the proof.