35
Course Outline • Traditional Static Program Analysis – Theory • Today: Compiler Optimizations, Control Flow Graphs • Data-flow Analysis – Classic analyses and applications • Software Testing • Dynamic Program Analysis

Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Embed Size (px)

Citation preview

Page 1: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Course Outline• Traditional Static Program Analysis

– Theory• Today: Compiler Optimizations, Control Flow Graphs

• Data-flow Analysis

– Classic analyses and applications

• Software Testing

• Dynamic Program Analysis

Page 2: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Textbooks

• Compilers: Principles, Techniques and Tools by Aho, Lam, Sethi and UllmanAddison-Wesley 2007

• Principles of Program Analysis by Nielsen, Nielsen and Hankin, Springer 1999

• Notes, handouts and papers

Page 3: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Static Program Analysis

• Analyzes the source code of the program and reasons about the run-time program behavior

• Many uses – Traditionally in compilers in order to perform

optimizing, semantics-preserving transformations– Recently in software tools for testing and

validation: our focus

Page 4: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Outline

• An Example: basic compiler optimizations• Control flow graphs• Local optimizations --- within basic blocks• Global optimizations --- across basic blocks

• Reading: Compilers: Principles, Techniques and Tools, by Aho, Lam, Sethi and Ullman, Chapter 9.1

Page 5: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Compilation

front end

symbol table

code generat

or

code optimiz

er

source program

intermediate code

intermediate code

target program

An optimization is a semantics-preserving transformation

Page 6: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Example

• Define classical optimizations using an example Fortran loop

• Opportunities result from table-driven code generation … sum = 0 do 10 i = 1, n

10 sum = sum + a[i]*a[i] …

Page 7: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Three Address Code1. sum = 0 initialize sum2. i = 1 initialize loop counter3. if i > n goto 15 loop test, check for limit4. t1 = addr(a) – 45. t2 = i * 4 a[i]6. t3 = t1[t2]7. t4 = addr(a) – 48. t5 = i * 4 a[i]9. t6 = t4[t5]10. t7 = t3 * t6 a[i]*a[i]11. t8 = sum + t712. sum = t8 increment sum13. i = i + 1 increment loop counter14. goto 3

15. …

Page 8: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Control Flow Graph (CFG)

1. sum = 02. i = 1

3. if i > n goto 15

4. t1 = addr(a) – 45. t2 = i*4 6. t3 = t1[t2]7. t4 = addr(a) – 48. t5 = i*49. t6 = t4[t5]10. t7 = t3*t611. t8 = sum + t712. sum = t813. i = i + 114. goto 3

15. …T

F

Page 9: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Common Subexpression Elimination1. sum = 0 1. sum = 0

2. i = 1 2. i = 13. if i > n goto 15 3. if i > n goto 154. t1 = addr(a) – 4 4. t1 = addr(a) – 45. t2 = i*4 5. t2 = i*46. t3 = t1[t2] 6. t3 = t1[t2]7. t4 = addr(a) – 4 7. t4 = addr(a) – 48. t5 = i*4 8. t5 = i*49. t6 = t4[t5] 9. t6 = t4[t5]10. t7 = t3*t6 10. t7 = t3*t611. t8 = sum + t7 10a t7 = t3*t312. sum = t8 11. t8 = sum + t713. i = i + 1 11a sum = sum + t714. goto 3 12. sum = t815. … 13. i = i + 1

14. goto 3

Page 10: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Invariant Code Motion1. sum = 0 1. sum = 0 2. i = 1 2. i = 13. if i > n goto 15 2a t1 = addr(a) - 44. t1 = addr(a) – 4 3. if i > n goto 155. t2 = i * 4 4. t1 = addr(a) - 46. t3 = t1[t2] 5. t2 = i * 410a t7 = t3 * t3 6. t3 = t1[t2]11a sum = sum + t7 10a t7 = t3 * t313. i = i + 1 11a sum = sum + t714. goto 3 13. i = i + 115. … 14. goto 3

15. …

Page 11: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Strength Reduction

1. sum = 0 1. sum = 02. i = 1 2. i = 12a t1 = addr(a) – 4 2a t1 = addr(a) - 43. if i > n goto 15 2b t2 = i * 45. t2 = i * 4 3. if i > n goto 156. t3 = t1[t2] 5. t2 = i * 410a t7 = t3 * t3 6. t3 = t1[t2]11a sum = sum + t7 10a t7 = t3 * t313. i = i + 1 11a sum = sum + t714. goto 3 11b t2 = t2 + 415. … 13. i = i + 1

14. goto 3

15. …

Page 12: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Test Elision and Induction Variable Elimination

1. sum = 0 1. sum = 02. i = 1 2. i = 12a t1 = addr(a) – 4 2a t1 = addr(a) – 42b t2 = i * 4 2b t2 = i * 43. if i > n goto 15 2c t9 = n * 46. t3 = t1[t2] 3. if i > n goto 1510a t7 = t3 * t3 3a if t2 > t9 goto 1511a sum = sum + t7 6. t3 = t1[t2]11b t2 = t2 + 4 10a t7 = t3 * t313. i = i + 1 11a sum = sum + t714. goto 3 11b t2 = t2 + 415. … 13. i = i + 1

14. goto 3a15. …

Page 13: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Constant Propagation and Dead Code Elimination

1. sum = 0 1. sum = 02. i = 1 2. i = 1 2a t1 = addr(a) – 4 2a t1 = addr(a) - 4 2b t2 = i * 4 2b t2 = i * 42c t9 = n * 4 2c t9 = n * 43a if t2 > t9 goto 15 2d t2 = 46. t3 = t1[t2] 3a if t2 > t9 goto 1510a t7 = t3 * t3 6. t3 = t1[t2]11a sum = sum + t7 10a t7 = t3 * t311b t2 = t2 + 4 11a sum = sum + t714. goto 3a 11b t2 = t2 + 415. … 14. goto 3a

15. …

Page 14: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

New Control Flow Graph1. sum = 02. t1 = addr(a) - 4 3. t9 = n * 44. t2 = 4

5. if t2 > t9 goto 11

6. t3 = t1[t2]7. t7 = t3 * t38. sum = sum + t79. t2 = t2 + 410. goto 5

11. …

F

T

Page 15: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Building Control Flow Graph

• Partition into basic blocks1. Determine the leader statements

(i) First program statement(ii) Targets of conditional or unconditional goto’s(iii) Any statement following a goto

2. For each leader, its basic block consists of the leader and all statements up to but not including the next leader or the end of the program

Page 16: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Building Control Flow Graph

• Add flow-of-control information

• There is a directed edge from basic block B1 to block B2 if B2 can immediately follow B1 in some execution sequence

– B2 immediately follows B1 and B1 does not end in an unconditional jump

– There is a jump from the last statement in B1 to the first statement in B2

Page 17: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Leader Statements and Basic Blocks

1. sum = 02. i = 13. if i > n goto 154. t1 = addr(a) – 45. t2 = i*46. t3 = t1[t2]7. t4 = addr(a) – 48. t5 = i*49. t6 = t5[t5]10. t7 = t3*t611. t8 = sum + t712. sum = t813. i = i + 114. goto 315. …

Page 18: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Analysis and optimizing transformations

• Local optimizations – performed by local analysis of a basic block

• Global optimizations – requires analysis of statements outside a basic block

• Local optimizations are performed first, followed by global optimizations

Page 19: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Local optimizations --- optimizing transformations of a basic blocks

• Local common subexpression elimination

• Dead code elimination

• Copy propagation

• Constant propagation

• Renaming of compiler-generated temporaries to share storage

Page 20: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Example 1: Local Common Subexpression Elimination

1. t1 = 4 * i2. t2 = a [ t1 ]3. t3 = 4 * i4. t4 = b [ t3 ]5. t5 = t2 * t46. t6 = prod * t57. prod = t68. t7 = i + 19. i = t710.if i <= 20 goto 1

Page 21: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Example 2: Local Dead Code Elimination

1. a = y + 2 1’. a = y + 2

2. z = x + w 2’. x = a

3. x = y + 2 3’. z = b + c

4. z = b + c 4’. b = a

5. b = y + 2

Page 22: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Example 3: Local Constant Propagation

1. t1 = 1 Assuming a, k, t3, and t4 are used beyond:

2. a = t1 1’. a = 1

3. t2 = 1 + a 2’. k = 2

4. k = t2 3’. t4 = 8.2

5. t3 = cvttoreal(k) 4’. t3 = 8.2

6. t4 = 6.2 + t3

7. t3 = t4 D. Gries’ algorithm:•Process 3-address statements in order•Check if operand is constant; if so, substitute•If all operands are constant,

Do operation, and add value to table associated with LHS•If not all operands constant

Delete any table entry for LHS

Page 23: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Problems• Troubles with arrays and pointers. Consider:

x = a[k];

a[j] = y;

z = a[k];

Can we transform this code into the following?

x = a[k];

a[j] = y;

z = x;

Page 24: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Global optimizations --- require analysis outside of basic blocks

• Global common subexpression elimination

• Dead code elimination

• Constant propagation

• Loop optimizations– Loop invariant code motion– Strength reduction– Induction variable elimination

Page 25: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Global optimizations --- depend on data-flow analysis

• Data-flow analysis refers to a body of techniques that derive information about the flow of data along program execution paths

• For example, in order to perform global subexpression elimination we need to determine that 2 textually identical expressions evaluate to the same result along any possible execution path

Page 26: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Introduction to Data-flow Analysis

• Collects information about the flow of data along execution paths– E.g., at some point we needed to know where a

variable was last defined

• Data-flow information

• Data-flow analysis

Page 27: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Data-flow Analysis1

2

3

4

5 6

7

8

9 10

• G = (N, E, ρ)

• Data-flow equations (also referred as transfer functions):

out(i) = gen(i) (in(i) – kill(i))

• Equations can be defined over basic blocks or over single statements. We will use equations over single statements

Page 28: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Four Classical Data-flow Problems

• Reaching definitions (Reach)• Live uses of variables (Live)• Available expressions (Avail)• Very Busy Expressions (VeryB)• Def-use chains built from Reach, and the dual

Use-def chains, built from Live, play role in many optimizations

• Avail enables global common subexpression elimination

• VeryB is used for conservative code motion

Page 29: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Reaching Definitions

• Definition A statement that may change the value of a variable (e.g., x = i+5)

• A definition of a variable x at node k reaches node n if there is a path clear of a definition of x from k to n.

k

n

x = …

… = x

x = …

Page 30: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Live Uses of Variables

• Use Appearance of a variable as an operand of a 3-address statement (e.g., y=x+4)

• A use of a variable x at node n is live on exit from k if there is a path from k to n clear of definition of x.

k

n

x = …

… = x

x = …

Page 31: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Def-use Relations

• Use-def chain links an use to a definition that reaches that use

• Def-use chain links a definition to an use that it reaches

k

n

x = …

… = x

x = …

Page 32: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Optimizations Enabled

• Dead code elimination (Def-use)

• Code motion (Use-def)

• Constant propagation (Use-def)

• Strength reduction (Use-def)

• Test elision (Use-def)

• Copy propagation (Def-use)

Page 33: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Dead Code Elimination1. sum = 02. i = 1

3. if i > n goto 15

4. t1 = addr(a)–45. t2 = i * 46. i = i + 1

T

F

After strength reduction, test elision and constant propagation, the def-use links from i=1 disappear. It becomes dead code.

Page 34: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Constant Propagation1. i = 1

2. i = 1

3. i = 2

4. p = i*25. i = 1

6. q = 5*i+3 = 8

Page 35: Course Outline Traditional Static Program Analysis –Theory Today: Compiler Optimizations, Control Flow Graphs Data-flow Analysis –Classic analyses and

Terms

• Control flow graph (CFG)

• Basic block

• Local optimization

• Global optimization

• Data-flow analysis