Title: Procedure calls:
1- Procedure calls
- Must generate the calling sequence and returning
sequence. - Can be done either at the calling or at the
called procedure depending on machine and OS. - A simple example
- S-gtcall id (Elist) for each item p on queue do
- emit(param p)
- emit(call
id.place) - Elist-gtElist1, E append E.place to the end of
the queue - Elist-gtE initialize queue to contain only
E.place
2- Code optimization
- A transformation to a program to make it run
faster and/or take up less space - Optimization should be safe, preserve the meaning
of a program. - Example peephole optimization.
- A simple technique to improve target code.
- Peephole a small moving window to the target
program. - Technique example a short sequence of target
instructions (peephole) and try to replace it
with a faster or shorter sequence
3- Peephole optimization
- Redundant instruction elimination
- Flow of control optimization
- Algebraic simplifications
- Instruction selection
- Examples
- Redundant loads and stores
- MOV R0, a
- MOV a, R0
- Unreachable code
- If debug 1 goto L1
- Goto L2
- L1 print debugging info
- L2
4- Examples
- Flow of control optimization
-
-
goto L1 L1 goto L2
goto L2 L1 goto L2
if a lt b goto L1 L1 goto L2
if altb goto L2 L1 goto L2
goto L1 L1 if a lt b goto L2
if a lt b goto L2 goto L3
L1 L3
5- Algebraic simplification
- x x0
- x x1 ? nop
- Reduction in strength
- X2 ? x x
- X 4 ? x ltlt 2
- Instruction selection
- Sometimes some hardware instructions can
implement certain operation efficiently.
6- Code optimization can either be high level or low
level - High level code optimizations
- Loop unrolling, loop fusion, procedure inlining
- Low level code optimizations
- Instruction selection, register allocation
- Some optimization can be done in both levels
- Common subexpression elimination, strength
reduction, etc. - Flow graph is a common intermediate
representation for code optimization.
7- Basic block a sequence of consecutive statements
with exactly 1 entry and 1 exit. - Flow graph a directed graph where the nodes are
basic blocks and block B1? block B2 if and only
if B2 can be executed immediately after B1 - Algorithm to construct flow graph
- Finding leaders of the basic blocks
- The first statement is a leader
- Any statement that is the target of a conditional
or unconditional goto is a leader - Any statement that immediately follows a goto or
conditional goto statement is a leader - For each leader, its basic block consists all
statements up to the next leader. - B1?B2 if and only if B2 can be executed
immediately after B1.
8- Example
- 100 sum 0
- 101 j 0
- 102 goto 107
- 103 t1 j ltlt 2
- 104 t2 addr(a)
- 105 t3 t2t1
- 106 sum sum t3
- 107 if j lt n goto 103
9- Optimizations within a basic block is called
local optimization. - Optimizations across basic blocks is called
global optimization. - Some common optimizations
- Instruction selection
- Register allocation
- Common subexpression elimination
- Code motion
- Strength reduction
- Induction variable elimination
- Dead code elimination
- Branch chaining
- Jump elimination
- Instruction scheduling
- Procedure inlining
- Loop unrolling
- Loop fusing
- Code hoisting
10- Instruction selection
- Using a more efficient instruction to replace a
sequence of instructions (space and speed). - Example
- Mov R2, (R3)
- Add R2, 1, R2
- Mov (R3), R2 ? Add (R3), 1,
(R3)
11- Register allocation allocate variables to
registers (speed) - Example
MR13sum 0 MR13j 0
GOTO L18 L19 R0 MR13j ltlt 2
MR13sum MR13sum
MR0_a MR13j
MR13j1 L18 NZ MR13j - M_n
if NZ lt 0 goto L19
R2 0 R1 0 GOTO
L18 L19 R0 R1 ltlt 2 R2
R2MR0_a R1 R11 L18 NZ
R1 - M_n if NZ lt 0 goto L19
12- Code motion move a loop invariant computation
before the loop - Example
R2 0 R1 0 GOTO
L18 L19 R0 R1 ltlt 2 R2
R2MR0_a R1 R11 L18 NZ
R1 - M_n if NZ lt 0 goto L19
R2 0 R1 0 R4
M_n GOTO L18 L19 R0 R1 ltlt
2 R2 R2MR0_a R1
R11 L18 NZ R1 R4 if NZ lt
0 goto L19
13- Strength reduction replace expensive operation
by equivalent cheaper operations - Example
R2 0 R1 0 R4
M_n GOTO L18 L19 R0 R1 ltlt
2 R2 R2MR0_a R1
R11 L18 NZ R1 R4 if NZ lt
0 goto L19
R2 0 R1 0 R4
M_n R3 _a GOTO L18 L19
R2 R2MR3 R3 R3 4 R1
R11 L18 NZ R1 R4 if NZ
lt 0 goto L19
14- Induction variable elimination can induce value
from another variable. - Example
R2 0 R1 0 R4
M_n R3 _a GOTO L18 L19
R2 R2MR3 R3 R3 4 R1
R11 L18 NZ R1 R4 if NZ
lt 0 goto L19
R2 0 R4 M_n ltlt 2
R3 _a GOTO L18 L19 R2
R2MR3 R3 R3 4 L18 NZ
R3 R4 if NZ lt 0 goto L19
15- Common subexpression eliminationan expression
was previously calculated and the variables in
the expression have not changed. Can avoid
recomputing the expression. - Example
R1 MR13I ltlt 2 R1 MR1_b R2 MR13I ltlt
2 R2 MR2_b
R1MR13I ltlt 2 R1 MR1_b R2 R1