• No results found

Workshop on Essential Abstractions in GCC

N/A
N/A
Protected

Academic year: 2022

Share "Workshop on Essential Abstractions in GCC"

Copied!
25
0
0

Loading.... (view fulltext now)

Full text

(1)

Workshop on Essential Abstractions in GCC

Graybox Probing for Machine Independent Optimizations

GCC Resource Center

(www.cse.iitb.ac.in/grc)

Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

July 2010

July 2010 Machine Independent Optimizations: Outline 1/30

Outline

• Example 1

Constant Propagation

Copy Propagation

Dead Code Elimination

Loop unrolling

• Example 2

Partial Redundancy Elimination

Copy Propagation

Dead Code Elimination

July 2010 Machine Independent Optimizations: Outline 1/30

Outline

N o te s

(2)

Part 1

First Example Program

July 2010 Machine Independent Optimizations: First Example Program 2/30

Example Program 1 int main()

{ int a, b, c, n;

a = 1;

b = 2;

c = 3;

n = c*2;

while (a <= n) {

a = a+1;

}

if (a < 12) a = a+b+c;

return a;

}

• What does this program return?

• 12

• We use this program to illustrate various shades of the following optimizations:

Constant propagation, Copy propagation, Loop unrolling, Dead code elimination

July 2010 Machine Independent Optimizations: First Example Program 2/30

Example Program 1

N o te s

(3)

Compilation Command

$gcc -fdump-tree-all -fdump-rtl-all -O2 ccp.c

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Compilation Command

N o te s

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

July 2010 Machine Independent Optimizations: First Example Program 4/30

Example Program 1

Program ccp.c Control flow graph

int main()

{ int a, b, c, n;

a = 1;

b = 2;

c = 3;

n = c*2;

while (a <= n) {

a = a+1;

}

if (a < 12) a = a+b+c;

return a;

}

B2 a = 1 b = 2 c = 3 n = c ∗ 2

B2

B4 if a ≤ n goto B3 B4 B3 a = a + 1 B3 B5 if a ≤ 11 goto B6

B6 D.1200 = a + b a = D.1200 + c B6

B7 D.1201 = a return D.1201

F T

T F

July 2010 Machine Independent Optimizations: First Example Program 4/30

Example Program 1

N o te s

(4)

Control Flow Graph: Pictorial and Textual View Control flow graph Dump file ccp.c.012t.cfg

B2 a = 1 b = 2 c = 3 n = c ∗ 2

B2

B4 if a ≤ n goto B3 B4

B3 a = a + 1 B3 B5 if a ≤ 11 goto B6

B6 D.1200 = a + b a = D.1200 + c B6

B7 D.1201 = a return D.1201

T F

T F

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Control Flow Graph: Pictorial and Textual View

N o te s

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

July 2010 Machine Independent Optimizations: First Example Program 5/30

Control Flow Graph: Pictorial and Textual View Control flow graph Dump file ccp.c.012t.cfg

B2 a = 1 b = 2 c = 3 n = c ∗ 2

B2

B4 if a ≤ n goto B3 B4 B3 a = a + 1 B3 B5 if a ≤ 11 goto B6

B6 D.1200 = a + b a = D.1200 + c B6

B7 D.1201 = a return D.1201

F T

T F

<bb 2>:

a = 1;

b = 2;

c = 3;

n = c * 2;

goto <bb 4>;

July 2010 Machine Independent Optimizations: First Example Program 5/30

Control Flow Graph: Pictorial and Textual View

N o te s

(5)

Control Flow Graph: Pictorial and Textual View Control flow graph Dump file ccp.c.012t.cfg

B2 a = 1 b = 2 c = 3 n = c ∗ 2

B2

B4 if a ≤ n goto B3 B4

B3 a = a + 1 B3 B5 if a ≤ 11 goto B6

B6 D.1200 = a + b a = D.1200 + c B6

B7 D.1201 = a return D.1201

T F

T F

<bb 3>:

a = a + 1;

<bb 4>:

if (a <= n) goto <bb 3>;

else

goto <bb 5>;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Control Flow Graph: Pictorial and Textual View

N o te s

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

July 2010 Machine Independent Optimizations: First Example Program 5/30

Control Flow Graph: Pictorial and Textual View Control flow graph Dump file ccp.c.012t.cfg

B2 a = 1 b = 2 c = 3 n = c ∗ 2

B2

B4 if a ≤ n goto B3 B4 B3 a = a + 1 B3 B5 if a ≤ 11 goto B6

B6 D.1200 = a + b a = D.1200 + c B6

B7 D.1201 = a return D.1201

F T

T F

<bb 5>:

if (a <= 11) goto <bb 6>;

else

goto <bb 7>;

<bb 6>:

D.1200 = a + b;

a = D.1200 + c;

July 2010 Machine Independent Optimizations: First Example Program 5/30

Control Flow Graph: Pictorial and Textual View

N o te s

(6)

Control Flow Graph: Pictorial and Textual View Control flow graph Dump file ccp.c.012t.cfg

B2 a = 1 b = 2 c = 3 n = c ∗ 2

B2

B4 if a ≤ n goto B3 B4

B3 a = a + 1 B3 B5 if a ≤ 11 goto B6

B6 D.1200 = a + b a = D.1200 + c B6

B7 D.1201 = a return D.1201

T F

T F

<bb 7>:

D.1201 = a;

return D.1201;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Control Flow Graph: Pictorial and Textual View

N o te s

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

July 2010 Machine Independent Optimizations: First Example Program 6/30

Single Static Assignment (SSA) Form

Control flow graph SSA Form

B2 a = 1; b = 2 c = 3; n = c ∗ 2 B2

B4 if a ≤ n goto B3 B4 B3 a = a + 1 B3 B5 if a ≤ 11 goto B6

B6 D.1200 = a + b a = D.1200 + c B6

B7 D.1201 = a return D.1201

T F

T F

B2 a 3 = 1; b 4 = 2

c 5 = 3; n 6 = c 5 ∗ 2 B2

B4 a 1 = φ (a 3, a 7) if a 1 ≤ n 6 B4

B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11

B6 D.1200 8 = a 1 + b 4 a 9 = D.1200 8 + c 5 B6

B7

a 2 = φ (a 1, a 9) D.1201 10 = a 2 return D.1201 10

T F

T F

July 2010 Machine Independent Optimizations: First Example Program 6/30

Single Static Assignment (SSA) Form

N o te s

(7)

Single Static Assignment (SSA) Form

Control flow graph SSA Form

B2 a = 1; b = 2 c = 3; n = c ∗ 2 B2

B4 if a ≤ n goto B3 B4 B3 a = a + 1 B3 B5 if a ≤ 11 goto B6

B6 D.1200 = a + b a = D.1200 + c B6

B7 D.1201 = a return D.1201

T F

T F

B2 a 3 = 1; b 4 = 2

c 5 = 3; n 6 = c 5 ∗ 2 B2

B4 a 1 = φ (a 3, a 7) if a 1 ≤ n 6 B4

B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11

B6 D.1200 8 = a 1 + b 4 a 9 = D.1200 8 + c 5 B6

B7

a 2 = φ (a 1, a 9) D.1201 10 = a 2 return D.1201 10

T F

T F

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Single Static Assignment (SSA) Form

N o te s

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

July 2010 Machine Independent Optimizations: First Example Program 6/30

Single Static Assignment (SSA) Form

Control flow graph SSA Form

B2 a = 1; b = 2 c = 3; n = c ∗ 2 B2

B4 if a ≤ n goto B3 B4 B3 a = a + 1 B3 B5 if a ≤ 11 goto B6

B6 D.1200 = a + b a = D.1200 + c B6

B7 D.1201 = a return D.1201

T F

T F

B2 a 3 = 1; b 4 = 2

c 5 = 3; n 6 = c 5 ∗ 2 B2

B4 a 1 = φ (a 3, a 7) if a 1 ≤ n 6 B4

B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11

B6 D.1200 8 = a 1 + b 4 a 9 = D.1200 8 + c 5 B6

B7

a 2 = φ (a 1, a 9) D.1201 10 = a 2 return D.1201 10

T F

T F

July 2010 Machine Independent Optimizations: First Example Program 6/30

Single Static Assignment (SSA) Form

N o te s

(8)

Single Static Assignment (SSA) Form

Control flow graph SSA Form

B2 a = 1; b = 2 c = 3; n = c ∗ 2 B2

B4 if a ≤ n goto B3 B4 B3 a = a + 1 B3 B5 if a ≤ 11 goto B6

B6 D.1200 = a + b a = D.1200 + c B6

B7 D.1201 = a return D.1201

T F

T F

B2 a 3 = 1; b 4 = 2

c 5 = 3; n 6 = c 5 ∗ 2 B2

B4 a 1 = φ (a 3, a 7) if a 1 ≤ n 6 B4

B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11

B6 D.1200 8 = a 1 + b 4 a 9 = D.1200 8 + c 5 B6

B7

a 2 = φ (a 1, a 9) D.1201 10 = a 2 return D.1201 10

T F

T F

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Single Static Assignment (SSA) Form

N o te s

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

July 2010 Machine Independent Optimizations: First Example Program 6/30

Single Static Assignment (SSA) Form

Control flow graph SSA Form

B2 a = 1; b = 2 c = 3; n = c ∗ 2 B2

B4 if a ≤ n goto B3 B4 B3 a = a + 1 B3 B5 if a ≤ 11 goto B6

B6 D.1200 = a + b a = D.1200 + c B6

B7 D.1201 = a return D.1201

T F

T F

B2 a 3 = 1; b 4 = 2

c 5 = 3; n 6 = c 5 ∗ 2 B2

B4 a 1 = φ (a 3, a 7) if a 1 ≤ n 6 B4

B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11

B6 D.1200 8 = a 1 + b 4 a 9 = D.1200 8 + c 5 B6

B7

a 2 = φ (a 1, a 9) D.1201 10 = a 2 return D.1201 10

T F

T F

July 2010 Machine Independent Optimizations: First Example Program 6/30

Single Static Assignment (SSA) Form

N o te s

(9)

Single Static Assignment (SSA) Form

Control flow graph SSA Form

B2 a = 1; b = 2 c = 3; n = c ∗ 2 B2

B4 if a ≤ n goto B3 B4 B3 a = a + 1 B3 B5 if a ≤ 11 goto B6

B6 D.1200 = a + b a = D.1200 + c B6

B7 D.1201 = a return D.1201

T F

T F

B2 a 3 = 1; b 4 = 2

c 5 = 3; n 6 = c 5 ∗ 2 B2

B4 a 1 = φ (a 3, a 7) if a 1 ≤ n 6 B4

B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11

B6 D.1200 8 = a 1 + b 4 a 9 = D.1200 8 + c 5 B6

B7

a 2 = φ (a 1, a 9) D.1201 10 = a 2 return D.1201 10

T F

T F

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Single Static Assignment (SSA) Form

N o te s

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

July 2010 Machine Independent Optimizations: First Example Program 7/30

Properties of SSA Form

B2 a 3 = 1; b 4 = 2

c 5 = 3; n 6 = c 5 ∗ 2 B2

B4 a 1 = φ (a 3, a 7) if a 1 ≤ n 6 B4

B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11

B6 D.1200 8 = a 1 + b 4 a 9 = D.1200 8 + c 5 B6

B7

a 2 = φ (a 1, a 9) D.1201 10 = a 2 return D.1201 10

T F

T F

• A φ function is a multiplexer or a selection function

• Every use of a variable corresponds to a unique definition of the variable

• For every use, the definition is guaranteed to appear on every path leading to the use SSA construction algorithm is expected to insert as few φ functions as possible to ensure the above properties

July 2010 Machine Independent Optimizations: First Example Program 7/30

Properties of SSA Form

N o te s

(10)

SSA Form: Pictorial and Textual View

CFG in SSA form Dump file ccp.c.023t.ssa

B2 a 3 = 1; b 4 = 2

c 5 = 3; n 6 = c 5 ∗ 2 B2

B4 a 1 = φ (a 3, a 7) if a 1 ≤ n 6 B4

B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11

B6 D.1200 8 = a 1 + b 4 a 9 = D.1200 8 + c 5 B6

B7

a 2 = φ (a 1, a 9) D.1201 10 = a 2 return D.1201 10

T F

T F

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

SSA Form: Pictorial and Textual View

N o te s

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

July 2010 Machine Independent Optimizations: First Example Program 8/30

SSA Form: Pictorial and Textual View

CFG in SSA form Dump file ccp.c.023t.ssa

B2 a 3 = 1; b 4 = 2

c 5 = 3; n 6 = c 5 ∗ 2 B2

B4 a 1 = φ (a 3, a 7) if a 1 ≤ n 6 B4

B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11

B6 D.1200 8 = a 1 + b 4 a 9 = D.1200 8 + c 5 B6

B7

a 2 = φ (a 1, a 9) D.1201 10 = a 2 return D.1201 10

T F

T F

<bb 2>:

a 3 = 1;

b 4 = 2;

c 5 = 3;

n 6 = c 5 * 2;

goto <bb 4>;

<bb 3>:

a 7 = a 1 + 1;

July 2010 Machine Independent Optimizations: First Example Program 8/30

SSA Form: Pictorial and Textual View

N o te s

(11)

SSA Form: Pictorial and Textual View

CFG in SSA form Dump file ccp.c.023t.ssa

B2 a 3 = 1; b 4 = 2

c 5 = 3; n 6 = c 5 ∗ 2 B2

B4 a 1 = φ (a 3, a 7) if a 1 ≤ n 6 B4

B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11

B6 D.1200 8 = a 1 + b 4 a 9 = D.1200 8 + c 5 B6

B7

a 2 = φ (a 1, a 9) D.1201 10 = a 2 return D.1201 10

T F

T F

<bb 4>:

# a 1 = PHI <a 3(2), a 7(3)>

if (a 1 <= n 6) goto <bb 3>;

else

goto <bb 5>;

<bb 5>:

if (a 1 <= 11) goto <bb 6>;

else

goto <bb 7>;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

SSA Form: Pictorial and Textual View

N o te s

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

July 2010 Machine Independent Optimizations: First Example Program 8/30

SSA Form: Pictorial and Textual View

CFG in SSA form Dump file ccp.c.023t.ssa

B2 a 3 = 1; b 4 = 2

c 5 = 3; n 6 = c 5 ∗ 2 B2

B4 a 1 = φ (a 3, a 7) if a 1 ≤ n 6 B4

B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11

B6 D.1200 8 = a 1 + b 4 a 9 = D.1200 8 + c 5 B6

B7

a 2 = φ (a 1, a 9) D.1201 10 = a 2 return D.1201 10

T F

T F

<bb 6>:

D.1200 8 = a 1 + b 4;

a 9 = D.1200 8 + c 5;

<bb 7>:

# a 2 = PHI <a 1(5), a 9(6)>

D.1201 10 = a 2;

return D.1201 10;

July 2010 Machine Independent Optimizations: First Example Program 8/30

SSA Form: Pictorial and Textual View

N o te s

(12)

A Comparison of CFG and SSA Dumps Dump file ccp.c.012t.cfg Dump file ccp.c.023t.ssa

<bb 2>:

a = 1;

b = 2;

c = 3;

n = c * 2;

goto <bb 4>;

<bb 3>:

a = a + 1;

<bb 2>:

a 3 = 1;

b 4 = 2;

c 5 = 3;

n 6 = c 5 * 2;

goto <bb 4>;

<bb 3>:

a 7 = a 1 + 1;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

A Comparison of CFG and SSA Dumps

N o te s

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

July 2010 Machine Independent Optimizations: First Example Program 9/30

A Comparison of CFG and SSA Dumps Dump file ccp.c.012t.cfg Dump file ccp.c.023t.ssa

<bb 4>:

if (a <= n) goto <bb 3>;

else

goto <bb 5>;

<bb 5>:

if (a <= 11) goto <bb 6>;

else

goto <bb 7>;

<bb 4>:

# a 1 = PHI <a 3(2), a 7(3)>

if (a 1 <= n 6) goto <bb 3>;

else

goto <bb 5>;

<bb 5>:

if (a 1 <= 11) goto <bb 6>;

else

goto <bb 7>;

July 2010 Machine Independent Optimizations: First Example Program 9/30

A Comparison of CFG and SSA Dumps

N o te s

(13)

A Comparison of CFG and SSA Dumps Dump file ccp.c.012t.cfg Dump file ccp.c.023t.ssa

<bb 6>:

D.1200 8 = a 1 + b 4;

a 9 = D.1200 8 + c 5;

<bb 7>:

# a 2 = PHI <a 1(5), a 9(6)>

D.1201 10 = a 2;

return D.1201 10;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

A Comparison of CFG and SSA Dumps

N o te s

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

July 2010 Machine Independent Optimizations: First Example Program 10/30

Copy Renamimg

Input dump: ccp.c.023t.ssa Output dump: ccp.c.026t.copyrename1

<bb 7>:

# a 2 = PHI <a 1(5), a 9(6)>

D.1201 10 = a 2;

return D.1201 10;

<bb 7>:

# a 2 = PHI <a 1(5), a 9(6)>

a 10 = a 2;

return a 10;

July 2010 Machine Independent Optimizations: First Example Program 10/30

Copy Renamimg

N o te s

(14)

First Level Constant and Copy Propagation

Input dump: ccp.c.026t.copyrename1 Output dump: ccp.c.027t.cpp1

<bb 2>:

a 3 = 1;

b 4 = 2;

c 5 = 3;

n 6 = c 5 * 2;

goto <bb 4>;

<bb 3>:

a 7 = a 1 + 1;

<bb 4>:

# a 1 = PHI < a 3(2), a 7(3)>

if (a 1 <= n 6) goto <bb 3>;

else

goto <bb 5>;

<bb 2>:

a 3 = 1;

b 4 = 2;

c 5 = 3;

n 6 = 6;

goto <bb 4>;

<bb 3>:

a 7 = a 1 + 1;

<bb 4>:

# a 1 = PHI < 1(2), a 7(3)>

if (a 1 <= 6) goto <bb 3>;

else

goto <bb 5>;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

First Level Constant and Copy Propagation

N o te s

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

July 2010 Machine Independent Optimizations: First Example Program 11/30

First Level Constant and Copy Propagation

Input dump: ccp.c.026t.copyrename1 Output dump: ccp.c.027t.cpp1

<bb 2>:

a 3 = 1;

b 4 = 2;

c 5 = 3;

n 6 = 6;

goto <bb 4>;

...

<bb 6>:

D.1200 8 = a 1 + b 4;

a 9 = D.1200 8 + c 5;

<bb 2>:

a 3 = 1;

b 4 = 2;

c 5 = 3;

n 6 = 6;

goto <bb 4>;

...

<bb 6>:

D.1200 8 = a 1 + 2;

a 9 = D.1200 8 + 3;

July 2010 Machine Independent Optimizations: First Example Program 11/30

First Level Constant and Copy Propagation

N o te s

(15)

Second Level Copy Propagation

Input dump: ccp.c.029t.ccp1 Output dump: ccp.c.031t.copyprop1

<bb 7>:

# a 2 = PHI <a 1(5), a 9(6)>

a 10 = a 2;

return a 10;

<bb 7>:

# a 2 = PHI <a 1(5), a 9(6)>

return a 2;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Second Level Copy Propagation

N o te s

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

July 2010 Machine Independent Optimizations: First Example Program 13/30

The Result of Copy Propagation and Renaming

B2 a 3 = 1; b 4 = 2

c 5 = 3; n 6 = c 5 ∗ 2 B2

B4 a 1 = φ (a 3, a 7) if a 1 ≤ n 6 B4

B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11

B6 D.1200 8 = a 1 + b 4 a 9 = D.1200 8 + c 5 B6

B7

a 2 = φ (a 1, a 9) D.1201 10 = a 2 return D.1201 10

T F

T F

B2 a 3 = 1; b 4 = 2 c 5 = 3; n 6 = 6 B2

B4 a 1 = φ (1, a 7) if a 1 ≤ 6 B4

B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11

B6 D.1200 8 = a 1 + 2 a 9 = D.1200 8 + 3 B6

B7 a 2 = φ (a 1, a 9) return a 2

T F

T F

July 2010 Machine Independent Optimizations: First Example Program 13/30

The Result of Copy Propagation and Renaming

N o te s

(16)

The Result of Copy Propagation and Renaming

B2 a 3 = 1; b 4 = 2 c 5 = 3; n 6 = 6 B2

B4 a 1 = φ (1, a 7) if a 1 ≤ 6 B4

B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11

B6 D.1200 8 = a 1 + 2 a 9 = D.1200 8 + 3 B6

B7 a 2 = φ (a 1, a 9) return a 2

T F

T F

• No uses for variables a 3, b 4, c 5, and n 6

• Assignments to these variables can be deleted

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

The Result of Copy Propagation and Renaming

N o te s

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

July 2010 Machine Independent Optimizations: First Example Program 15/30

Dead Code Elimination Using Control Dependence Dump file ccp.c.033t.cddce1

B2 a 3 = 1; b 4 = 2 c 5 = 3; n 6 = 6 B2

B4 a 1 = φ (1, a 7) if a 1 ≤ 6 B4

B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11

B6 D.1200 8 = a 1 + 2 a 9 = D.1200 8 + 3 B6

B7 a 2 = φ (a 1, a 9) return a 2

T F

T F

<bb 2>:

goto <bb 4>;

<bb 3>:

a 7 = a 1 + 1;

<bb 4>:

# a 1 = PHI <1(2), a 7(3)>

if (a 1 <= 6) goto <bb 3>;

else goto <bb 5>;

<bb 5>:

if (a 1 <= 11) goto <bb 6>;

else goto <bb 7>;

<bb 6>:

D.1200 8 = a 1 + 2;

a 9 = D.1200 8 + 3;

<bb 7>:

# a 2 = PHI <a 1(5), a 9(6)>

return a 2;

July 2010 Machine Independent Optimizations: First Example Program 15/30

Dead Code Elimination Using Control Dependence

N o te s

(17)

Loop Unrolling

B2 a 3 = 1; b 4 = 2 c 5 = 3; n 6 = 6 B2

B4 a 1 = φ (1, a 7) if a 1 ≤ 6 B4

B3 a 7 = a 1 + 1 B5 if a 1 ≤ 11

B6 D.1200 8 = a 1 + 2 a 9 = D.1200 8 + 3 B6

B7 a 2 = φ (a 1, a 9) return a 2

T F

T F

B4 a = 2 a = a + 1 a = a + 1 a = a + 1 a = a + 1 a = a + 1

B4

B5 if a 1 ≤ 11

B6 D.1200 8 = a 1 + 2 a 9 = D.1200 8 + 3 B6

B7 a 2 = φ (a 1, a 9) return a 2

T F

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Loop Unrolling

N o te s

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

July 2010 Machine Independent Optimizations: First Example Program 17/30

Complete Unrolling of Inner Loops

Dump file: ccp.c.058t.cunrolli

<bb 2>:

a_12 = 2;

a_14 = a_12 + 1;

a_16 = a_14 + 1;

a_18 = a_16 + 1;

a_20 = a_18 + 1;

a_22 = a_20 + 1;

if (a_22 <= 11) goto <bb 3>;

else goto <bb 4>;

<bb 3>:

D.1959_8 = a_22 + 2;

a_9 = D.1959_8 + 3;

<bb 4>:

# a_2 = PHI <a_22(2), a_9(3)>

return a_2;

B2

a 12 = 2 a 14 = a 12 + 1 a 16 = a 14 + 1 a 18 = a 16 + 1 a 20 = a 18 + 1 a 22 = a 20 + 1 if a 22 ≤ 11

B3 D.1200 8 = a 22 + 2 a 9 = D.1200 8 + 3 B3

B4 a 2 = φ (a 22, a 9) return a 2

T F

July 2010 Machine Independent Optimizations: First Example Program 17/30

Complete Unrolling of Inner Loops

N o te s

(18)

Another Round of Constant Propagation

Input Dump file: ccp.c.059t.ccp2

B2

a 12 = 2 a 14 = a 12 + 1 a 16 = a 14 + 1 a 18 = a 16 + 1 a 20 = a 18 + 1 a 22 = a 20 + 1 if a 22 ≤ 11

B3 D.1200 8 = a 22 + 2 a 9 = D.1200 8 + 3 B3

B4 a 2 = φ (a 22, a 9) return a 2

T F

<bb 2>:

a_22 = 7;

a_9 = 12;

return 12;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Another Round of Constant Propagation

N o te s

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

July 2010 Machine Independent Optimizations: First Example Program 19/30

Dead Code Elimination Using Copy Propagation

Dump file: ccp.c.059t.ccp2 Dump file: ccp.c.066t.copyprop2 a_22 = 7;

a_9 = 12;

return 12;

<bb 2>:

return 12;

July 2010 Machine Independent Optimizations: First Example Program 19/30

Dead Code Elimination Using Copy Propagation

N o te s

(19)

Part 2

Second Example Program

July 2010 Machine Independent Optimizations: Second Example Program 20/30

Example Program 2

int f(int b, int c, int n) { int a;

do {

a = b+c;

}

while (a <= n);

return a;

}

We use this program to illustrate the following optimizations:

Partial Redundancy Elimination, Copy Propagation, Dead Code Elimination

July 2010 Machine Independent Optimizations: Second Example Program 20/30

Example Program 2

N o te s

(20)

Compilation Command

$gcc -fdump-tree-all -fdump-rtl-all -O2 -S ccp.c

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Compilation Command

N o te s

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

July 2010 Machine Independent Optimizations: Second Example Program 22/30

Example Program 2

loop.c Control Flow Graph

int f(int b, int c, int n) { int a;

do {

a = b+c;

}

while (a <= n);

return a;

}

a = 1

a = b + c if (a ≤ n)

return a

July 2010 Machine Independent Optimizations: Second Example Program 22/30

Example Program 2

N o te s

(21)

Dump of Input to PRE Pass

Control Flow Graph loop.c.091t.crited

a = 1 B2

a = b + c if (a ≤ n) B3

retur

B6 B5 retur

return a B4

<bb 2>:

<bb 3>:

a_3 = c_2(D) + b_1(D);

if (a_3 <= n_4(D)) goto <bb 5>;

else goto <bb 6>;

<bb 5>:

goto <bb 3>;

<bb 6>:

<bb 4>:

# a_6 = PHI <a_3(6)>

return a_6;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Dump of Input to PRE Pass

N o te s

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

July 2010 Machine Independent Optimizations: Second Example Program 24/30

Input and Output of PRE Pass loop.c.091t.crited loop.c.092t.pre

<bb 2>:

<bb 3>:

a_3 = c_2(D) + b_1(D);

if (a_3 <= n_4(D)) goto <bb 5>;

else goto <bb 6>;

<bb 5>:

goto <bb 3>;

<bb 6>:

<bb 4>:

# a_6 = PHI <a_3(6)>

return a_6;

<bb 2>:

pretmp.2_7 = b_1(D) + c_2(D);

<bb 3>:

a_3 = pretmp.2_7;

if (a_3 <= n_4(D)) goto <bb 5>;

else goto <bb 6>;

<bb 5>:

goto <bb 3>;

<bb 6>:

<bb 4>:

# a_6 = PHI <a_3(6)>

return a_6;

July 2010 Machine Independent Optimizations: Second Example Program 24/30

Input and Output of PRE Pass

N o te s

(22)

Copy Propagation after PRE

loop.c.092t.pre loop.c.096t.copyprop4

<bb 2>:

pretmp.2_7 = b_1(D) + c_2(D);

<bb 3>:

a_3 = pretmp.2_7;

if ( a_3 <= n_4(D)) goto <bb 5>;

else goto <bb 6>;

<bb 5>:

goto <bb 3>;

<bb 6>:

<bb 4>:

# a_6 = PHI <a_3(6)>

return a_6;

<bb 2>:

pretmp.2_7 = b_1(D) + c_2(D);

<bb 3>:

a_3 = pretmp.2_7;

if ( n_4(D) >= pretmp.2_7) goto <bb 4>;

else

goto <bb 5>;

<bb 4>:

goto <bb 3>;

<bb 5>:

# a_8 = PHI <pretmp.2_7(3)>

return a_8;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Copy Propagation after PRE

N o te s

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

July 2010 Machine Independent Optimizations: Second Example Program 26/30

Dead Code Elimination

loop.c.096t.copyprop4 loop.c.097t.dceloop1

<bb 2>:

pretmp.2_7 = b_1(D) + c_2(D);

<bb 3>:

a_3 = pretmp.2_7;

if (n_4(D) >= pretmp.2_7) goto <bb 4>;

else

goto <bb 5>;

<bb 4>:

goto <bb 3>;

<bb 5>:

# a_8 = PHI <pretmp.2_7(3)>

return a_8;

<bb 2>:

pretmp.2_7 = b_1(D) + c_2(D);

<bb 3>:

if (n_4(D) >= pretmp.2_7) goto <bb 4>;

else

goto <bb 5>;

<bb 4>:

goto <bb 3>;

<bb 5>:

# a_8 = PHI <pretmp.2_7(3)>

return a_8;

July 2010 Machine Independent Optimizations: Second Example Program 26/30

Dead Code Elimination

N o te s

(23)

Redundant φ Function Elimination and Copy Propagation

loop.c.097t.dceloop1 loop.c.124t.phicprop2

<bb 2>:

pretmp.2_7 = b_1(D) + c_2(D);

<bb 3>:

if (n_4(D) >= pretmp.2_7) goto <bb 4>;

else

goto <bb 5>;

<bb 4>:

goto <bb 3>;

<bb 5>:

# a_8 = PHI <pretmp.2_7(3)>

return a_8;

<bb 2>:

pretmp.2_7 = c_2(D) + b_1(D);

if (n_4(D) >= pretmp.2_7) goto <bb 4>;

else

goto <bb 3>;

<bb 3>:

return pretmp.2_7;

<bb 4>:

goto <bb 4>;

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Redundant φ Function Elimination and Copy Propagation

N o te s

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

July 2010 Machine Independent Optimizations: Second Example Program 28/30

Final Assembly Program

loop.c.124t.phicprop2 loop.s

<bb 2>:

pretmp.2_7 = c_2(D) + b_1(D);

if (n_4(D) >= pretmp.2_7) goto <bb 4>;

else

goto <bb 3>;

<bb 3>:

return pretmp.2_7;

<bb 4>:

goto <bb 4>;

pushl %ebp movl %esp, %ebp movl 12(%ebp), %eax addl 8(%ebp), %eax cmpl %eax, 16(%ebp)

jge .L2

popl %ebp

ret .L2:

.L3:

jmp .L3

Why infinite loop?

July 2010 Machine Independent Optimizations: Second Example Program 28/30

Final Assembly Program

N o te s

(24)

Infinite Loop in Example Program 2

int f(int b, int c, int n) { int a;

do {

a = b+c;

}

while (a <= n);

return a;

}

The program does not terminate unless a > n

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Infinite Loop in Example Program 2

N o te s

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Part 3

Conclusions

(25)

Conclusions

• GCC performs many machine independent optimizations

• The dumps of optimizations are easy to follow, particularly at the GIMPLE level

• It is easy to prepare interesting test cases and observe the effect of transformations

• One optimization often leads to another

Hence GCC performs many optimizations repeatedly (eg. copy propagation, dead code elimination)

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

Conclusions

N o te s

Essential Abstractions in GCC GCC Resource Center, IIT Bombay

References

Related documents

July 2010 GIMPLE and RTL: Adding a GIMPLE Pass to GCC 16/38. An Intraprocedural

Common Code (executed twice for each function in the input program for single process LTO. Once during LGEN and then during WPA + LTRANS) cgraph optimize.

Generating target specific code = populating these data structures... Assume movsi is supported but movsf is

30 June 2011 Machine Independent Optimizations: Second Example Program 20/29.

Essential Abstractions in GCC GCC Resource Center, IIT Bombay.. 1 July 2013 Retargetability Model: Generating the Code Generators 13/18. Explicit Calls to

Essential Abstractions in GCC GCC Resource Center, IIT Bombay.. • Create directories ${BUILD D} and in a tree not rooted at ${SOURCE D}... • Change the directory to ${BUILD D}

Essential Abstractions in GCC GCC Resource Center, IIT Bombay.. July 2010 Spim MD Levels 0,1: Retargeting GCC to Spim: A Recap 13/39. Building a Cross-Compiler

• Since rules become composable, tree tiling based instruction selection algorithms can be used. Currently rules are non-composable and GCC uses full tree