Questions: Register Allocation

5 questions to test your understanding

Score: 0 / 5
Question 1 Multiple Choice

Two variables x and y both appear in the same function. Variable x holds the result of a computation that is only used at line 5, and y is first assigned at line 8. Can they safely share a register?

ANo — two variables can never share a register in the same function
BYes — their live ranges don't overlap, so they don't interfere and can share a register
COnly if they have the same data type
DOnly if the compiler can prove they hold the same value
Question 2 Multiple Choice

The graph coloring heuristic for register allocation works by repeatedly removing nodes with fewer than k neighbors. Why does this guarantee those nodes can always be colored at assignment time?

ANodes with fewer neighbors interfere with fewer variables, so they are less important and can be spilled cheaply
BA node with fewer than k neighbors will always have at least one color available no matter what colors its neighbors receive, since there are k colors and fewer than k constraints
CLow-degree nodes are always assigned to callee-saved registers, which are always available
DThe heuristic does not guarantee colorability — it just provides an approximation
Question 3 True / False

Spilling a variable that is used inside a deeply nested loop is more expensive than spilling a variable used once outside any loop.

TTrue
FFalse
Question 4 True / False

Two variables that are seldom simultaneously live can still interfere and is expected to be given different registers if they are both used in the same basic block.

TTrue
FFalse
Question 5 Short Answer

Why is live variable analysis a required prerequisite for register allocation, and what would go wrong if you tried to allocate registers without it?

Think about your answer, then reveal below.