CS453 Register Allocation
Quick quiz In C, how can r=(a+b)+(c+d) be evaluated? e.g. as follows? t1 = a+d t2 = b+c r = t1+t2 (page 207 of ANSI and traditional C ref. manual 3 rd edition) How about in Java? CS453 Lecture Register Allocation 2
Register Use so far So far we have used registers mostly as temporaries: 1. operator Expression: pop operand(s) into register(s) perform operation into result register push result register 2. method call caller passes actual parameters through registers to callee r24:r25 down to r8:r9 callee pushes them into the stack frame 3. this is loaded from stack location (Y+1, Y+2) into r30:r31 (Z) when needed Longer lived: SP has one specific role FP (r28:r39) lives during one method activation, caller FP gets saved in callee stack frame CS453 Lecture Register Allocation 3
More register use We have more registers and want to use these WHY? to avoid push / pop data movement (executing fewer instructions) register access is faster than memory access We want to minimize the number of registers to be used WHY? there are only a few registers (32 in AVR) fewer registers require fewer moves more variables can be associated with registers CS453 Lecture Register Allocation 4
Two new forms of register use 1. registers for (temporary) expressions - these don t have to have an identifier associated with them i+1 - currently live on the RTS if they live in a register, push and pop can be avoided 2. registers for parameters and locals - longer lifetime (method activation) - may need to be saved on the stack when another method is called who saves which registers? (callee saved / caller saved) CS453 Lecture Register Allocation 5
Register allocation for expressions What is the smallest number of registers (or temporary positions) we need for a certain expression? a+b a*b+c*d for this expresssion tree? CS453 Lecture Register Allocation 6
Register allocation for expressions What is the smallest number of registers (or temporary positions) we need for a certain expression? a+b a*b+c*d for this expresssion tree? r1 r1 r2 r1 r2 r2 r3 r1 r2 r3 r4 CS453 Lecture Register Allocation 7
Code generation for expressions with (some) registers - if k registers available, associate as many expressions with these registers as possible (using the symbol table) - now some expressions have a register associated with them and some don t - adjust regpair helper functions storeexpression() and loadexpression() to deal with this regpair loadexpression(backup_reg(s)) if expression associated with reg(s) return these else pop values into backup reg(s) and return these storeexpression(src_reg(s)) if expression associated with reg(s) move src_reg(s) into these else push src_reg(s) CS453 Lecture Register Allocation 8
Callee and caller saved registers Register saving Callee: if callee is going to use certain callee saved registers, then it saves at function prolog (and restores at function epilog) their current value same for caller saved registers (save just before call, restore right after) Callee saving occurs once per method activation Why callee and caller saved registers? so we can play optimization games e.g. method f calls method h many times (e.g. in a loop) method h calls no other methods then f can use callee saved regs and h can use caller saved regs (no need for saving these) CS453 Lecture Register Allocation 9
Register allocation for parameters and locals We can now assign left over registers to locals and parameters - Instead of fetching and storing locals from to stack we use their allocated registers - Incoming parameter values are moved into registers instead of copied to the stack - Adjust regpair loadvar() and storevar() helper functions: regpair loadvar(backup_reg(s)) if var in reg(s) return these else fetch Var (base+offset) and copy to backup_reg(s) regpair storevar(src_reg(s)) if var in reg(s) copy src_reg(s) to these else copy src_reg(s) to Var (base+offset) CS453 Lecture Register Allocation 10