int arr10000 void Binky int i for i0

  • Slides: 78
Download presentation

判断那一段代码效率更高? int arr[10000]; void Binky() { int i; for (i=0; i < 10000; i++)

判断那一段代码效率更高? int arr[10000]; void Binky() { int i; for (i=0; i < 10000; i++) arr[i] = 1; } 2/20/2021 int arr[10000]; void Winky() { register int *p; for (p = arr; p < arr + 10000; p++) *p = 1; } 4

11. 1. 1 优化技术简介 (1)常数合并 a = 10 * 5 + 6 - b;

11. 1. 1 优化技术简介 (1)常数合并 a = 10 * 5 + 6 - b; _tmp 0 = 10 ; _tmp 1 = 5 ; _tmp 2 = _tmp 0 * _tmp 1 ; _tmp 3 = 6 ; _tmp 4 = _tmp 2 + _tmp 3 ; _tmp 5 = _tmp 4 – b; a = _tmp 5 ; –b; 2/20/2021 _tmp 0 = 56 ; _tmp 1 = _tmp 0 a = _tmp 1 ; 6

(2)常数传播 _tmp 4 = 0 ; f 0 = _tmp 4 ; _tmp 5

(2)常数传播 _tmp 4 = 0 ; f 0 = _tmp 4 ; _tmp 5 = 1 ; f 1 = _tmp 5 ; _tmp 6 = 2 ; i = _tmp 6 ; 2/20/2021 f 0 = 0 ; f 1 = 1 ; i=2; 7

(3)—代数简化 x+0 = x 0+x = x x*1 = x 1*x = x 0/x

(3)—代数简化 x+0 = x 0+x = x x*1 = x 1*x = x 0/x = 0 x-0 = x b && true = b b && false = false b || true = true b || false = b 2/20/2021 8

代数简化举例 b = 5 + a + 10 ; _tmp 0 = 5 ;

代数简化举例 b = 5 + a + 10 ; _tmp 0 = 5 ; _tmp 1 = _tmp 0 + a ; _tmp 2 = _tmp 1 + 10 ; b = _tmp 2 ; _tmp 0 = 15 ; _tmp 1 = a + b = _tmp 1 ; 2/20/2021 9

(4)降低运算强度 a) i*2 = 2*i = i+i = i<<2 b) i/2 = (int)(i*0. 5)

(4)降低运算强度 a) i*2 = 2*i = i+i = i<<2 b) i/2 = (int)(i*0. 5) c) 0 -1 = -1 d) f*2 = 2. 0 * f = f + f e) f/2. 0 = f*0. 5 2/20/2021 10

(5)复写传播 tmp 2 = tmp 1 ; tmp 3 = tmp 2 * tmp

(5)复写传播 tmp 2 = tmp 1 ; tmp 3 = tmp 2 * tmp 1; tmp 4 = tmp 3 ; tmp 5 = tmp 3 * tmp 2 ; c = tmp 5 + tmp 4 ; 2/20/2021 tmp 3 = tmp 1 * tmp 1 ; tmp 5 = tmp 3 * tmp 1 ; c = tmp 5 + tmp 3 ; 11

(1)P: =0 (2)I: =1 (3)T 1: =4*I (4)T 2: =addr(A)-4 (5)T 3: =T 2[T

(1)P: =0 (2)I: =1 (3)T 1: =4*I (4)T 2: =addr(A)-4 (5)T 3: =T 2[T 1] (6)T 4: =4*I (7)T 5: =addr(B)-4 (8)T 6: =T 5[T 4] (9)T 7: =T 3*T 6 (10)P: =P+T 7 (11)I: =I+1 (12)if I<=20 goto(3) (1)P: =0 (2)I: =1 (4)T 2: =addr(A)-4 (7)T 5: =addr(B)-4 (3)T 1: =4*I (5)T 3: =T 2[T 1] (6)T 4: =T 1 (8)T 6: =T 5[T 4] (9)T 7: =T 3*T 6 (10)P: =P+T 7 (11)I: =I+1 (12)if I<=20 goto(3) 删除多余运算(4*I) 2/20/2021 代码外提 13

(1)P: =0 (2)I: =1 (4)T 2: =addr(A)-4 (7)T 5: =addr(B)-4 (3)T 1: =4*I (5)T

(1)P: =0 (2)I: =1 (4)T 2: =addr(A)-4 (7)T 5: =addr(B)-4 (3)T 1: =4*I (5)T 3: =T 2[T 1] (6)T 4: =T 1 (8)T 6: =T 5[T 4] (9)T 7: =T 3*T 6 (10)P: =P+T 7 (11)I: =I+1 (12)if I<=20 goto(3) 2/20/2021 (1)P: =0 (2)I: =1 (4)T 2: =addr(A)-4 (7)T 5: =addr(B)-4 (3)T 1: =4*I (5)T 3: =T 2[T 1] (6)T 4: =T 1 (8)T 6: =T 5[T 4] (9)T 7: =T 3*T 6 (10)P: =P+T 7 (11)I: =I+1 (3‘)T 1: =T 1+4 (12)if I<=20 goto(5) 强度削弱 14

(1)P: =0 (2)I: =1 (4)T 2: =addr(A)-4 (7)T 5: =addr(B)-4 (3)T 1: =4*I (1)P:

(1)P: =0 (2)I: =1 (4)T 2: =addr(A)-4 (7)T 5: =addr(B)-4 (3)T 1: =4*I (1)P: =0 (2)I: =1 (4)T 2: =addr(A)-4 (7)T 5: =addr(B)-4 (5)T 3: =T 2[T 1] (6)T 4: =T 1 (8)T 6: =T 5[T 4] (9)T 7: =T 3*T 6 (10)P: =P+T 7 (11)I: =I+1 (3’)T 1: =T 1+4 (12)if I<=20 goto(5) (5)T 3: =T 2[T 1] (6)T 4: =T 1 (8)T 6: =T 5 T 1 [ ] (9)T 7: =T 3*T 6 (10)P: =P+T 7 (11)I: =I+1 2/20/2021 (3)T 1: =4 (3’)T 1: =T 1+4 (12)if. T 1 <=80 goto(5) 变换循环控制条件、合 并已知量 15

(1)P: =0 (2)I: =1 (4)T 2: =addr(A)-4 (7)T 5: =addr(B)-4 (3)T 1: =4 (1)P:

(1)P: =0 (2)I: =1 (4)T 2: =addr(A)-4 (7)T 5: =addr(B)-4 (3)T 1: =4 (1)P: =0 (4)T 2: =addr(A)-4 (7)T 5: =addr(B)-4 (3)T 1: =4 (5)T 3: =T 2[T 1] (6)T 4: =T 1 (8)T 6: =T 5[T 1] (9)T 7: =T 3*T 6 (10)P: =P+T 7 (11)I: =I+1 (3’)T 1: =T 1+4 (12)if T 1<=80 goto(5) (5)T 3: =T 2[T 1] (8)T 6: =T 5[T 1] (9)T 7: =T 3*T 6 (10)P: =P+T 7 (3’)T 1: =T 1+4 (12)if T 1<=80 goto(5) 删除无用赋值 2/20/2021 16

2/20/2021 17

2/20/2021 17

11. 2. 1基本块的划分 2/20/2021 18

11. 2. 1基本块的划分 2/20/2021 18

(1) read (C) (2) A: = 0 (3) B: = 1 (4) L 1:

(1) read (C) (2) A: = 0 (3) B: = 1 (4) L 1: A: =A + B (5) if B>= C goto L 2 (6) B: =B+1 (7) goto L 1 (8) L 2: write (A) (9) halt 入口语句: 1、4、6、8 2/20/2021 19

2/20/2021 20

2/20/2021 20

 • 用DAG 进行基本块的优化 DAG 结点 四元式 n 1 0 型:A: =B(: =, B,

• 用DAG 进行基本块的优化 DAG 结点 四元式 n 1 0 型:A: =B(: =, B, —, A) n 1 A B 1 型: A: =op B(op, B, —, A) n 2 A n 2 op n 1 B 2 型: A: =B op C(op, B, C, A) n 3 A n 3 op n 2 n 1 n 2 B C 2/20/2021 23

A: =B [ C] 4型 if B rop C goto (S) 3型D[C]=B n 1

A: =B [ C] 4型 if B rop C goto (S) 3型D[C]=B n 1 D goto (s) 2/20/2021 n 3 A =[] (s) n 2 C n 4 []= n 2 n 3 B C n 1 (s) 24

2/20/2021 25

2/20/2021 25

2/20/2021 26

2/20/2021 26

2/20/2021 27

2/20/2021 27

2/20/2021 28

2/20/2021 28

2/20/2021 29

2/20/2021 29

2/20/2021 30

2/20/2021 30

2/20/2021 31

2/20/2021 31

2/20/2021 32

2/20/2021 32

2/20/2021 33

2/20/2021 33

2/20/2021 38

2/20/2021 38

2/20/2021 39

2/20/2021 39

2/20/2021 40

2/20/2021 40

2/20/2021 41

2/20/2021 41

必经结点集 D(1)={1} D(2)={1, 2} D(3)={1, 2, 3} D(4)={1, 2, 4} D(5)={1, 2, 4, 5}

必经结点集 D(1)={1} D(2)={1, 2} D(3)={1, 2, 3} D(4)={1, 2, 4} D(5)={1, 2, 4, 5} D(6)={1, 2, 4, 6} D(7)={1, 2, 4, 7} 1 2 3 3 4 5 6 7 7 2/20/2021 45

(1)P: =0 (2)I: =1 (3)T 1: =4*I (4)T 2: =addr(A)-4 (5)T 3: =T 2[T

(1)P: =0 (2)I: =1 (3)T 1: =4*I (4)T 2: =addr(A)-4 (5)T 3: =T 2[T 1] (6)T 4: =4*I (7)T 5: =addr(B)-4 (8)T 6: =T 5[T 4] (9)T 7: =T 3*T 6 (10)P: =P+T 7 (11)I: =I+1 (12)if I<=20 goto(3) (1)P: =0 (2)I: =1 (4)T 2: =addr(A)-4 (7)T 5: =addr(B)-4 (3)T 1: =4*I (5)T 3: =T 2[T 1] (6)T 4: =T 1 (8)T 6: =T 5[T 4] (9)T 7: =T 3*T 6 (10)P: =P+T 7 (11)I: =I+1 (12)if I<=20 goto(3) 删除多余运算(4*I) 2/20/2021 代码外提 53

2. 强度削弱与删除归纳变量 (1)P: =0 (2)I: =1 (4)T 2: =addr(A)-4 (7)T 5: =addr(B)-4 (3)T 1:

2. 强度削弱与删除归纳变量 (1)P: =0 (2)I: =1 (4)T 2: =addr(A)-4 (7)T 5: =addr(B)-4 (3)T 1: =4*I (5)T 3: =T 2[T 1] (6)T 4: =T 1 (8)T 6: =T 5[T 4] (9)T 7: =T 3*T 6 (10)P: =P+T 7 (11)I: =I+1 (12)if I<=20 goto(3) 2/20/2021 • • (1)P: =0 (2)I: =1 (4)T 2: =addr(A)-4 (7)T 5: =addr(B)-4 (3)T 1: =4*I • • • (5)T 3: =T 2[T 1] (6)T 4: =T 1 (8)T 6: =T 5[T 4] (9)T 7: =T 3*T 6 (10)P: =P+T 7 (11)I: =I+1 (3‘)T 1: =T 1+4 • (12)if I<=20 goto(5) 强度削弱 59

2/20/2021 67

2/20/2021 67

B 1 B 2 B 3 B 4 B 5 GEN {d 1, d

B 1 B 2 B 3 B 4 B 5 GEN {d 1, d 2} {d 3} {d 4} {d 5} { } B 1 B 2 B 3 B 4 B 5 2/20/2021 KILL 位向量 1100000 {d 3, d 4, d 5} 0011100 0010000 {d 1} 1000000 0001000 {d 2, d 5} 0100100 0000100 {d 2, d 4} 0101000 0000000 IN[B] 0111100 1111100 0011000 0011100 OUT[B] 1100000 0111100 0011000 0010100 0011100 68

In[B 1]= In[B 2]=…= In[B 5]=0000000 Out[B 1]=1100000 Out[B 4]=0000100 Out[B 2]=0010000 Out[B 3]=0001000

In[B 1]= In[B 2]=…= In[B 5]=0000000 Out[B 1]=1100000 Out[B 4]=0000100 Out[B 2]=0010000 Out[B 3]=0001000 Out[B 2]=0000000 对于B 1:Newin=0010000 In[B 1]= Newin=0010000 Out[B 1] = In[B 1]-kill [B 1]UGen [B 1]=1100000 对于B 2:Newin=1100000 In[B 2]= Newin=1100000 Out[B 2] = In[B 2]-kill [B 2]UGen [B 2]=0110000 对于B 3:Newin=0110000 In[B 3]= Newin=0110000 Out[B 3] = In[B 3]-kill [B 3]UGen [B 3]=0011000 …… P 262 图 11. 26 2/20/2021 69

例: 基本 Def 块 B 1 {a} Use {b} Ø B 3 {c} Ø

例: 基本 Def 块 B 1 {a} Use {b} Ø B 3 {c} Ø {d} {a, b } 2/20/2021 a: =1 if a=b goto B 2 {b} B 2 B 4 B 1 B 2 b: =1 B 3 c: =1 d: =a+b B 4 75

从最后一个基本块开始由后往前计算,可以得到一定的解 In(B)= Use(B)∪(Out(B)- Def(B)) Out(B)=∪In(i) i∈s(B) Out(B 4)=Ø,因此: In(B 4)= Use(B 4)-Def(B 4)= {a,

从最后一个基本块开始由后往前计算,可以得到一定的解 In(B)= Use(B)∪(Out(B)- Def(B)) Out(B)=∪In(i) i∈s(B) Out(B 4)=Ø,因此: In(B 4)= Use(B 4)-Def(B 4)= {a, b}{d}= {a, b} 现在 Out(B 2)=In (B 4)={a, b} Out(B 3)=In (B 4)= {a, b} In(B 2)= Out(B 2)-Def(B 2)= {a, b}-{b} In(B 3)=Out(B 3)-Def(B 3)= {a, b}– {c} Out(B 1) = In (B 2) ∪In (B 3)= {a}∪ {a, b} = {a, b} 最后 In(B 1)=Use(B 1)∪(Out(B 1) Def(B 1))= {b}∪({a, b} – {a}) = {b} 2/20/2021 B 1 a: =1 if a=b goto B 2 b: =1 B 3 c: =1 d: =a+b B 4 76

11. 4. 6 复写传播 p 266 2/20/2021 78

11. 4. 6 复写传播 p 266 2/20/2021 78