Code Generation and Optimization
ALSU Textbook Chapters 8.4, 8.5, 8.7, 8.8, 9.1 Tsan-sheng Hsu
tshsu@iis.sinica.edu.tw http://www.iis.sinica.edu.tw/~tshsu
1
Code Generation and Optimization ALSU Textbook Chapters 8.4, 8.5, - - PowerPoint PPT Presentation
Code Generation and Optimization ALSU Textbook Chapters 8.4, 8.5, 8.7, 8.8, 9.1 Tsan-sheng Hsu tshsu@iis.sinica.edu.tw http://www.iis.sinica.edu.tw/~tshsu 1 Introduction For some compiler, the intermediate code is a pseudo code of a virtual
tshsu@iis.sinica.edu.tw http://www.iis.sinica.edu.tw/~tshsu
1
⊲ Pascal: P-code for the virtual P machine. ⊲ JAVA: Byte code for the virtual JAVA machine.
Compiler notes #8, 20070622, Tsan-sheng Hsu 2
⊲ Increment by 1. ⊲ Multiplying or dividing by 2. ⊲ Bit-wise manipulation. ⊲ Operators applied on a continuous block of memory space.
Compiler notes #8, 20070622, Tsan-sheng Hsu 3
Compiler notes #8, 20070622, Tsan-sheng Hsu 4
Compiler notes #8, 20070622, Tsan-sheng Hsu 5
E 0
E 1 E 3
✂E 4
✄E 5
☎E 6
✆reg#
reg# E2 1 E6 1 E3 2 E5 2 E5 3 E4 1 E6 4 E3 2 E4 3 E1 1 E1 2 E2 2 E0 1 E0 1
Compiler notes #8, 20070622, Tsan-sheng Hsu 6
⊲ t1 := a ∗ a ⊲ t2 := a ∗ b ⊲ t3 := 2 ∗ t2 ⊲ goto outter
B2
B1
Compiler notes #8, 20070622, Tsan-sheng Hsu 7
⊲ its target is a leader; ⊲ its next statement is also a leader.
Compiler notes #8, 20070622, Tsan-sheng Hsu 8
⊲ prod := 0 ⊲ i := 1 ⊲ loop: t1 := 4 ∗ i ⊲ t2 := a[t1] ⊲ t3 := 4 ∗ i ⊲ t4 := b[t3] ⊲ t5 := t2 ∗ t4 ⊲ t6 := prod + t5 ⊲ prod := t6 ⊲ t7 := i + 1 ⊲ i := t7 ⊲ if i ≤ 20 goto loop ⊲ · · ·
Compiler notes #8, 20070622, Tsan-sheng Hsu 9
+ * [] [] * + <= i 1 20 4 b a prod (1) t1 t2 t3 t4 t5 t6 t7 prod’ i’
Compiler notes #8, 20070622, Tsan-sheng Hsu 10
a := b+c b := a−d c := b+c d := a−d a := b+c b := a−d c := b+c d := b
⊲ MOV R0, a ⊲ MOV a, R0
⊲ Find loop-invariants inside a loop. ⊲ Obtain the values of loop-invariants outside the loop. ⊲ Example: t = limit - 2 while(i <= limit - 2) while (i <= t) ... ...
Compiler notes #8, 20070622, Tsan-sheng Hsu 11
⊲ De-reference a chain of variable copies. ⊲ Example: a = x; a = x; y = a; y = x; b = y; b = x;
⊲ De-reference a chain of goto’s. ⊲ Example: goto L1 · · · L1: goto L2 goto L2 · · · L1: goto L2
Compiler notes #8, 20070622, Tsan-sheng Hsu 12
⊲ Example: t1 := E1 t2 := const // swap t2 and tn ... tn := E1
⊲ Example: t1 := E1 t2 := t1 + tn // canoot swap t2 and tn ... tn := E1
Compiler notes #8, 20070622, Tsan-sheng Hsu 13
Compiler notes #8, 20070622, Tsan-sheng Hsu 14
E 0
E 1 E 3
✂E 4
✄E 5
☎E 6
✆E 0
E 1 E 3
✂E 4
✄E 5
☎E 6
✆LL LR
Compiler notes #8, 20070622, Tsan-sheng Hsu 15