TARGET CODE -Next Usage Rajat Sethi 04CS3022 TARGET CODE -Next Usage
Topics to be covered Next Use Information Code Generation Computation using Backward Scan Code Generation Register and Address Descriptor Algorithm getreg() function Example
Next Usage This technique is employed in dead code optimization. The instructions with no ‘next use’ are removed from the block. If the name in a register is no longer needed, then the register can be assigned to some other name. We collect next-use information about names in a basic block. Note that we consider only the variables in the same block.
Backward Scan: statement i : x := y op z STEP 1: Attach to statement i, attribute information of next use of x, y and z. STEP 2: In symbol table, set x to “no next use” and “not live” STEP 3: In symbol table set the next uses of y and z to I and also to “live” NOTE : The orders of Step 2 and Step 3 should not be interchanged because x may be y or z. e.g.:- x := x+1
Register & Address Descriptors Register Descriptor: It keeps track of what is in each register. It is consulted whenever a new register is needed. E.g.: R0 contains t, y, z. Address Descriptor: It keeps tract of the location(s) where the current value of the name can be found at the runtime. The location might be register, stack or memory location.
Code Generation Algorithm STEP 1 Find space L to store value of x using function getreg(). For Y, see whether it is stored in L. If yes, perform op Z,L Else perform mov Y,L; op Z,L Generate : op Z,L Update address descriptor of X. If X stored in any other register, remove it. If the current values of Y and Z have no next use then free the registers. STEP 2 STEP 3 STEP 4
Function getreg(); STEP 1: If the name y is in a register , that holds the value of no other names (in other words no other names point to the same register as y does), and y is not live and has no next use after the execution of y = x op z, then :- return L. Update the address descriptor of y , so that y is no longer in L. STEP 2: Failing (1), return an empty register for L if it exists. STEP 3: Failing (2), if x has a next use in the block, or if op requires a register then :- find an occupied register R. MOV(R,M) if value of R is not in proper M. If R holds value of many variables, generate a MOV for each of the variables. STEP 4: Failing (3), select the memory location of x as L.
Example: Statements Code Generated Register Descriptor Address t = a-b d=(a-b) + (a-c) + (a-c) Statements Code Generated Register Descriptor Address t = a-b MOV a, R0 SUB b, R0 R0 -> t t -> R0 u= a-c MOV a, R1 SUB c, R1 R1 -> u u -> R1 v= t+u ADD R1,R0 R0 -> v v -> R0 d= v+u MOV R0,d R0 -> d d -> R0
THANK YOU