Compiler Chapter 11 Code Optimization SungDong Kim Dept

  • Slides: 26
Download presentation
Compiler Chapter 11. Code Optimization Sung-Dong Kim, Dept. of Computer Engineering, Hansung University

Compiler Chapter 11. Code Optimization Sung-Dong Kim, Dept. of Computer Engineering, Hansung University

Introduction (1) • Code optimization —Equivalent meaning more efficient code —Reduce # of computation

Introduction (1) • Code optimization —Equivalent meaning more efficient code —Reduce # of computation —Use faster instruction —Minimize memory request (2011 -1) Compiler 2

Introduction (2) • Criteria —Preserve programming meaning —Speed up on average —Be worth the

Introduction (2) • Criteria —Preserve programming meaning —Speed up on average —Be worth the effort (2011 -1) Compiler 3

Introduction (3) • Kinds —Local optimization —Global optimization —Loop optimization —Precode optimization: machine-independent optimization

Introduction (3) • Kinds —Local optimization —Global optimization —Loop optimization —Precode optimization: machine-independent optimization —Postcode optimization: machine-dependent optimization (2011 -1) Compiler 4

Introduction (4) • Basic concepts —Basic block —Flow graph (2011 -1) Compiler 5

Introduction (4) • Basic concepts —Basic block —Flow graph (2011 -1) Compiler 5

1. Basic block (2) • Finding basic block —Finding all leaders —From leader to

1. Basic block (2) • Finding basic block —Finding all leaders —From leader to before leader = basic block • Flow graph —Basic block + information for control flow —Important information for global optimization (2011 -1) Compiler 7

1. Basic block (3) for (i=0; i<N; i++) statement 1; statement 2; L 1:

1. Basic block (3) for (i=0; i<N; i++) statement 1; statement 2; L 1: L 2: i = 0; goto L 2; statement 1; i++; if (i<N) goto L 1; statement 2; Block 1 i = 0; goto L 2; Block 2 L 1: statement 1; i++; Block 3 L 2: if (i<N) goto L 1; Block 4 statement 2; (2011 -1) Compiler 8

1. Basic block (5) initial Block 1 Block 3 Block 2 Block 4 (2011

1. Basic block (5) initial Block 1 Block 3 Block 2 Block 4 (2011 -1) Compiler 10

2. Local optimization (1) • Local optimization —부분적인 관점에서 비효율적인 코드를 구분 —좀 더

2. Local optimization (1) • Local optimization —부분적인 관점에서 비효율적인 코드를 구분 —좀 더 효율적인 코드로 개선 • Removal of common sub-expression t = b + c; x = a + t; y = t + d; z=t*e x = a + b + c; y = b + c + d; z = (b + c) * e; (2011 -1) Compiler 11

2. Local optimization (2) • Operation strength reduction —Cost of operator: time to execute

2. Local optimization (2) • Operation strength reduction —Cost of operator: time to execute operation a = b * b; x = a + a; y = a * 0. 2; y = a shift-right 2 a = b ** 2; x = 3 * a; y = a / 5; y = a / 4; (2011 -1) Compiler 12

2. Local optimization (4) i = 2; … t 1 = 4 * i;

2. Local optimization (4) i = 2; … t 1 = 4 * i; i = 2; … t 1 = 4 * 2; i = 2; … t 1 = 8; (2011 -1) Compiler 14

2. Local optimization (5) • Algebraic simplification —Algebraic law x = y + 0;

2. Local optimization (5) • Algebraic simplification —Algebraic law x = y + 0; x = 1 * y; Constant * Symbol x = y; Symbol * Constant (2011 -1) Compiler 15

2. Local optimization (6) • Substitution t 1 = 4 * j + 1;

2. Local optimization (6) • Substitution t 1 = 4 * j + 1; … t 7 = 1; t 1 = 4 * j + 1; … t 7 = t 1 – 4 * j; (2011 -1) Compiler 16

3. Loop optimization (2) • 연산 강도 경감 • Loop unrolling for (i=0; i<N;

3. Loop optimization (2) • 연산 강도 경감 • Loop unrolling for (i=0; i<N; i+=2) { a[i] = 0; a[i+1] = 0; } for (i=0; i<N; i++) a[i] = 0; (2011 -1) Compiler 18

3. Loop optimization (3) • Loop fusion for (i=0; i<N; i++) a[i] = 0;

3. Loop optimization (3) • Loop fusion for (i=0; i<N; i++) a[i] = 0; for (i=0; i<N; i++) b[i] = c[i] + y; for (i=0; i<N; i++) { a[i] = 0; b[i] = c[i] + y; } • 0으로 카운트 for (i=0; i<N; i++) a[i] = 0; for (i=N-1; i >= 0; i--) a[i] = 0; (2011 -1) Compiler 19

4. Global optimization (1) • Flow analysis • 기본 블록간의 정보 + 흐름 그래프

4. Global optimization (1) • Flow analysis • 기본 블록간의 정보 + 흐름 그래프 이용 • Removal of common expression x 1 = bb * i 2 x 2 = a +bb * i 2 t = bb * i 2 x 1 = t x 3 = a/2 + bb * i 2 t = bb * i 2 x 2 = a + t x 3 = a/2 + t • Global constant folding & propagation (2011 -1) Compiler 20

TRUE … a=1 b=2 t=a*b if (t = 2) t=2 if (2 = 2)

TRUE … a=1 b=2 t=a*b if (t = 2) t=2 if (2 = 2) FALSE TRUE … … FALSE … a=1 b=2 t=2 TRUE … (2011 -1) Compiler 21

4. Global optimization (2) • 도달될 수 없는 코드 제거 • 조건문 재구성 if

4. Global optimization (2) • 도달될 수 없는 코드 제거 • 조건문 재구성 if (true) statement 1 else statement 2 statement 1; if (x > 1) ; else statement; if (x <= 1) statement; goto label. A … label. A: goto label. B • 연속된 GOTO의 축약 (2011 -1) Compiler 22

5. Machine dependent optimization (1) • 중복된 load 제거 a : = b; c

5. Machine dependent optimization (1) • 중복된 load 제거 a : = b; c = a + 1; load r 1, b ; load register 1 from b store r 1, a ; store register 1 in a load r 1, a ; load register 1 from a add r 1, =1 ; add immediate store r 1, c ; store register 1 in c • 효율적인 명령어 선택 —a = a + 1 – load r 1, a; – add r 1, 1 – store r 1, a inc a (2011 -1) Compiler 23

5. Machine dependent optimization (3) • 연산 순서 —더 적은 register를 필요로 하는 순서로

5. Machine dependent optimization (3) • 연산 순서 —더 적은 register를 필요로 하는 순서로 연산을 한다. • Pattern matching —패턴과 대치 (replacement) 쌍 (2011 -1) Compiler 25