248 commits 3/5/2014 introduction csc 255/455shuleli/download/15-interprocedural-cfl.pdf ·...

7
CSC 255/455 Beyond Dataflow: Interprocedural Analysis and CFL reachability Instructor: Chen Ding Reading: Problem classification: Allen-Kennedy book, Chapter 11 (11.2.1-2 required). CFL-reachability: Reps’ TR http://research.cs.wisc.edu/ wpis/papers/tr1386.pdf Mercurial graph slides for instruction scheduling, register allocation and partial redundancy removal slides on ssa uses Mergeassignment 2 submission, the files are in /assignment/2_pass/base/gccbranch merge updatebranch commit branch mergeYanan Zhang's Homeworkmergeanymerge248 commits 3/5/2014 Introduction • Interprocedural Analysis • Gathering information about the whole program instead of a single procedure • Do you know any such analysis? • Interprocedural Optimization • Program transformation involving more than one function • Do you know a transformation? • Other names of the procedure construct • function in C, subroutine in Fortran, method, lambda 2 Enter main Call p Call p Enter p Can we simply model procedure call/ return as control flow jumps? Enter main sum = 0 i = 1 while(i < 11) printf(sum) printf(i) Call add Call add x in = sum y in = i sum = x out x in = i y in = 1 i = x out Enter add x = x in y = y in x = x + y x out = x Call-by-value Parameter Passing Overview: Interprocedural Analysis • Examples of Interprocedural problems • Classification of Interprocedural problems • Side-effect Analysis • Jump functions in constant propagation • Symbolic analysis • Pointer analysis 5 Some Interprocedural Problems Modification and Reference Side-effect COMMON X,Y ... DO I = 1, N S0: CALL P S1: X(I) = X(I) + Y(I) ENDDO Can S1 be parallelized? 6

Upload: others

Post on 12-Jun-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 248 commits 3/5/2014 Introduction CSC 255/455shuleli/download/15-interprocedural-cfl.pdf · –Model-checking recursive HFSMs •Formal-language theory –CF-, 2DPDA-, 2NPDA-recognition

CSC 255/455

Beyond Dataflow: Interprocedural Analysis and CFL reachability

Instructor: Chen DingReading: Problem classification: Allen-Kennedy book, Chapter 11 (11.2.1-2 required).CFL-reachability: Reps’ TR http://research.cs.wisc.edu/wpis/papers/tr1386.pdf

2/26/2014 /Users/cding/cs255hg: revision graph

http://localhost:8000/graph/b48f23a0aea8 1/3

log

graphtags

bookmarks

branches

changeset

browse

help

Mercurialgraph

less more | rev 241: (0) -100 -60 tip

slides for instruction scheduling, register allocation and partial redundancy removal default tip

slides on ssa uses

Merge yzh145

assignment 2 submission, the files are in /assignment/2_pass/base/gcc yzh145

branch merge update yzh145

branch commit yzh145

branch merge yzh145

Yanan Zhang's Homework yzh145

merge pli

any pli

merge ysong23

merge xjin6

Add GC Fixed Point notes

merge xjin6

readd test.rb

clean up the default branch

add summary of counts pli

assign. 2 pli

merge xhu9

update makefile xhu9

Merge from default hxu

merge

before merge

merge

homework1 added

merge default pli

merge pli

merged

2 minutes ago, by Chen Ding

16 minutes ago, by Chen Ding

5 days ago, by Yanan, Zhang

11 days ago, by Yanan, Zhang

12 days ago, by Yanan, Zhang

2 weeks ago, by Yanan, Zhang

2 weeks ago, by Yanan, Zhang

3 weeks ago, by Yanan, Zhang

20 hours ago, by Pengcheng, Li

20 hours ago, by Pengcheng, Li

5 days ago, by Yang, Song

5 days ago, by Xi Jin

5 days ago, by Gernhardt, Brian

6 days ago, by Xi Jin

6 days ago, by Lingxiang Xiang

6 days ago, by Lingxiang Xiang

6 days ago, by Pengcheng, Li

6 days ago, by Pengcheng, Li

6 days ago, by Xiaoyu, Hu

6 days ago, by Xiaoyu, Hu

6 days ago, by Hao, Xu

6 days ago, by cs255

6 days ago, by cs255

6 days ago, by Lingxiang, Xiang

6 days ago, by Lingxiang, Xiang

6 days ago, by Pengcheng, Li

13 days ago, by Pengcheng, Li

6 days ago, by Chen Ding

248 commits3/5/2014 Introduction

• Interprocedural Analysis• Gathering information about the whole program instead of a

single procedure• Do you know any such analysis?

• Interprocedural Optimization• Program transformation involving more than one function• Do you know a transformation?

• Other names of the procedure construct• function in C, subroutine in Fortran, method, lambda

2

Enter main

Call p Call p

Enter p

Can we simply model procedure call/return as control flow jumps?

Enter main

sum = 0 i = 1 while(i < 11) printf(sum) printf(i)

Call add Call add

xin = sum yin = i sum = xout xin = i yin= 1 i = xout

Enter add

x = xin y = yin x = x + y xout = x

Call-by-value Parameter Passing

Overview: Interprocedural Analysis

• Examples of Interprocedural problems• Classification of Interprocedural problems• Side-effect Analysis• Jump functions in constant propagation• Symbolic analysis• Pointer analysis

5

Some Interprocedural Problems

• Modification and Reference Side-effect COMMON X,Y ... DO I = 1, N

S0: CALL P S1: X(I) = X(I) + Y(I)

ENDDO

• Can S1 be parallelized?

6

Page 2: 248 commits 3/5/2014 Introduction CSC 255/455shuleli/download/15-interprocedural-cfl.pdf · –Model-checking recursive HFSMs •Formal-language theory –CF-, 2DPDA-, 2NPDA-recognition

Alias Analysis

SUBROUTINE S(A,X,N) COMMON Y DO I = 1, N S0: X = X + Y*A(I) ENDDO END• Could have kept X and Y in different registers and stored in X

outside the loop• What happens when there is a call, CALL S(A,Y,N)?

• Then Y is aliased to X on entry to S• Can’t delay update to X in the loop any more

• ALIAS(p,x): set of variables that may refer to the same location as formal parameter x on entry to p

7

Call Graph Construction

• Call Graph G=(N,E)• N: one vertex for each procedure• E: one edge for each possible call

• Edge (p,q) is in E if procedure p calls procedure q• Looks easy• Construction difficult in presence of procedure parameters• A call site

• a control flow node inside the caller procedure• a procedure may have multiple call sites

8

Live and Use Analysis

• Solve Live analysis using Use Analysis• USE(s): set of variables having an upward exposed use in

procedure p called at s• At a call site, s is in a single basic block(b), x is live if either

• x in USE(s) or• P doesn’t assign a new value to x and x is live in some control

flow successor of b

9

Interprocedural Problem Classification

• May and Must problems• MOD, REF and USE are ‘May’ problems• KILL is a ‘Must’ problem

• Flow sensitive and flow insensitive problems• Flow sensitive: control flow info important• Flow insensitive: control flow info unimportant

10

A

BBA

R1 R2

MOD(R1) = MOD(A)∪MOD(B)MOD(R2) = MOD(A)∪MOD(B)

REF(R1) = REF(A)∪ REF(B)REF(R2) = REF(A)∪ REF(B)

KILL(R1) = KILL(A)∪ KILL(B)KILL(R2) = KILL(A)∩ KILL(B)

USE(R1) =USE(A)∪ (¬KILL(A)∩USE(B))USE(R2) =USE(A)∪USE(B)

Flow Sensitive vs Flow Insensitive

11

Classification (continued)

• A problem is flow insensitive iff solution of both sequential and alternately composed regions is determined by taking union of subregions

• Side-effect vs Propagation problems• MOD, REF, KILL and USE are side-effect• ALIAS, CALL and CONST are propagation

12

Page 3: 248 commits 3/5/2014 Introduction CSC 255/455shuleli/download/15-interprocedural-cfl.pdf · –Model-checking recursive HFSMs •Formal-language theory –CF-, 2DPDA-, 2NPDA-recognition

Flow Insensitive Side-effect Analysis

• Assumptions• No procedure nesting• All parameters passed by reference• Size of the parameter list bounded by a constant,

• MOD(s) can be solved in linear time• see the textbook

13

SUBROUTINE FOO(N) INTEGER N,M CALL INIT(M,N) DO I = 1,P B(M*I + 1) = 2*B(1) ENDDOEND

SUBROUTINE INIT(M,N) M = NEND

If N = 0 on entry to FOO, the loop is a reduction.Otherwise, we can vectorize the loop.

Constant Propagation

• Propagating constants between procedures can cause significant improvements.

• Dependence testing can be made more precise.

14

• Definition: Let s = (p,q) be a call site in procedure p, and let x be a parameter of q. Then , the jump function for x at s, gives the value of x in terms of parameters of p.

• The support of is the set of p-parameters that is dependent on.

Jsx

JsxJs

x

Constant Propagation

15

• Instead of a Def-Use graph, we construct an interprocedural value graph:• Add a node to the graph for each jump function • If x belongs to the support of , where t lies in the procedure

q, then add an edge between and for every call site s =(p,q) for some p.

• We can now apply the constant propagation algorithm to this graph. • Might want to iterate with global propagation

JsxJty

Jsx

Jty

Constant Propagation

16

JαX

JβVJβ

U

JαY

The constant-propagation algorithm willEventually converge to above values.(might need to iterate with global)

1 2

-13

Example

PROGRAM MAIN INTEGER A,B A = 1 B = 2 α CALL S(A,B) END SUBROUTINE S(X,Y) INTEGER X,Y,Z,W Z = X + Y W = X - Y β CALL T(Z,W) END SUBROUTINE T(U,V) PRINT U,V END

17

Symbolic Analysis

• Prove facts about variables other than constancy:• Find a symbolic expression for a variable in terms of other

variables.• Establish a relationship between pairs of variables at some

point in program.• Establish a range of values for a variable at a given point.

• Array section analysis

18

Page 4: 248 commits 3/5/2014 Introduction CSC 255/455shuleli/download/15-interprocedural-cfl.pdf · –Model-checking recursive HFSMs •Formal-language theory –CF-, 2DPDA-, 2NPDA-recognition

Program Analysis via CFL Reachability

http://research.cs.wisc.edu/wpis/papers/tr1386.pdf

3/17/12 Home Page of Prof. Thomas W. Reps

1/52pages.cs.wisc.edu/~reps/

My UW | UW Search

Computer Science HomePage> ~reps

Contact Information

Biography

Current Research

Summary of Past Research

Recent Items of Note(Last updated: 12/19/11)

Categorized Index toPublications

Disclaimer

List of Publications· Books · Journal Publications · Invited Papers · Book Chapters · Reprinted in Collections · Edited Books · Conference Publications · Patents · Pending Submissions · Magazine Articles · Other Publications andReports

Software

Visitors, Post-DoctoralAssociates, and Students

C.S. Dept. Home Page

Thomas W. RepsProfessorComputer Sciences DepartmentUniversity of Wisconsin-Madison1210 West Dayton StreetMadison, WI 53706-1685USA

Contact Information:

E-mail: reps at cs.wisc.eduTelephone: (608) 262-2091Department: (608) 262-1204Fax: (608) 262-9777

Some Maps: Campus Map | Campus-to-Capitol Map | City Map

Ph.D., Cornell University, 1982 (CV, Biography, Current Research, Summary of PastResearch)

Research Interests:

Program slicing, differencing, and mergingInterprocedural dataflow analysisAlias analysis, pointer analysis, and shape analysisAnalysis of multi-threaded programs

Possibly Uninitialized VariablesStart

x = 3

if . . .

y = xy = w

w = 8

printf(y)

{w,x,y}

{w,y}

{w,y}

{w,y}

{w}

{w,y}{}

{w,y}

{}

Precise Intraprocedural Analysis

start n

Cx = 3

p(x,y)

return from p

printf(y)

start main

exit main

start p(a,b)

if . . .

b = a

p(a,b)

return from p

printf(b)

exit p

(

)]

(

call site 1 call

site 2

Precise Interprocedural Analysis

start n

C ret

( )

[Sharir & Pnueli 81]

Representing Dataflow Functions

Identity Function

Constant Function

a b c

a b c

Page 5: 248 commits 3/5/2014 Introduction CSC 255/455shuleli/download/15-interprocedural-cfl.pdf · –Model-checking recursive HFSMs •Formal-language theory –CF-, 2DPDA-, 2NPDA-recognition

Representing Dataflow Functions“Gen/Kill” Function

Non-“Gen/Kill” Function a b c

a b cx = 3

p(x,y)

return from p

printf(y)

start main

exit main

start p(a,b)

if . . .

b = a

p(a,b)

return from p

printf(b)

exit p

x y a b

a b c

b ca

Composing Dataflow Functionsb ca

x = 3

p(x,y)

return from p

start main

exit main

start p(a,b)

if . . .

b = a

p(a,b)

return from p

exit p

x y a b

printf(y)

Might b beuninitializedhere?

printf(b) NO!

(

]

Might y beuninitializedhere?

YES!

(

)

Exhaustive Versus Demand Analysis

• Exhaustive analysis: All facts at all points• Optimization: Concentrate on inner loops• Program-understanding tools: Only some facts

are of interest

What Are Slices Useful For?• Understanding Programs

–What is affected by what?• Restructuring Programs

–Isolation of separate “computational threads”• Program Specialization and Reuse

–Slices = specialized programs–Only reuse needed slices

• Program Differencing–Compare slices to identify changes

• Testing–What new test cases would improve coverage?–What regression tests must be rerun after a change?

Page 6: 248 commits 3/5/2014 Introduction CSC 255/455shuleli/download/15-interprocedural-cfl.pdf · –Model-checking recursive HFSMs •Formal-language theory –CF-, 2DPDA-, 2NPDA-recognition

Line-Character-Count Programvoid line_char_count(FILE *f) { int lines = 0; int chars; BOOL eof_flag = FALSE; int n; extern void scan_line(FILE *f, BOOL *bptr, int *iptr); scan_line(f, &eof_flag, &n); chars = n; while(eof_flag == FALSE){ lines = lines + 1; scan_line(f, &eof_flag, &n); chars = chars + n; } printf(“lines = %d\n”, lines); printf(“chars = %d\n”, chars);}

Character-Count Programvoid char_count(FILE *f) { int lines = 0; int chars; BOOL eof_flag = FALSE; int n; extern void scan_line(FILE *f, BOOL *bptr, int *iptr); scan_line(f, &eof_flag, &n); chars = n; while(eof_flag == FALSE){ lines = lines + 1; scan_line(f, &eof_flag, &n); chars = chars + n; } printf(“lines = %d\n”, lines); printf(“chars = %d\n”, chars);}

Line-Character-Count Programvoid line_char_count(FILE *f) { int lines = 0; int chars; BOOL eof_flag = FALSE; int n; extern void scan_line(FILE *f, BOOL *bptr, int *iptr); scan_line(f, &eof_flag, &n); chars = n; while(eof_flag == FALSE){ lines = lines + 1; scan_line(f, &eof_flag, &n); chars = chars + n; } printf(“lines = %d\n”, lines); printf(“chars = %d\n”, chars);}

Line-Count Programvoid line_count(FILE *f) { int lines = 0; int chars; BOOL eof_flag = FALSE; int n; extern void scan_line2(FILE *f, BOOL *bptr, int *iptr); scan_line2(f, &eof_flag, &n); chars = n; while(eof_flag == FALSE){ lines = lines + 1; scan_line2(f, &eof_flag, &n); chars = chars + n; } printf(“lines = %d\n”, lines); printf(“chars = %d\n”, chars);}

Exhaustive Versus Demand Analysis

• Demand analysis:–Does a given fact hold at a given point?–Which facts hold at a given point?–At which points does a given fact hold?

• Demand analysis via CFL-reachability–single-source/single-target CFL-reachability–single-source/multi-target CFL-reachability–multi-source/single-target CFL-reachability

x = 3

p(x,y)

return from p

printf(y)

start main

exit main

start p(a,b)

if . . .

b = a

p(a,b)

return from p

printf(b)

exit p

x y a b

YES!

(

)

NO!

“Semi-exhaustive”:All “appropriate” demands

Might y beuninitializedhere?

Might b beuninitializedhere?

Page 7: 248 commits 3/5/2014 Introduction CSC 255/455shuleli/download/15-interprocedural-cfl.pdf · –Model-checking recursive HFSMs •Formal-language theory –CF-, 2DPDA-, 2NPDA-recognition

CFL-Reachability: Scope of Applicability• Static analysis

–Slicing, DFA, structure-transmitted dep., points-to analysis

• Verification–Security of crypto-based protocols for distributed

systems [Dolev, Even, & Karp 83]–Model-checking recursive HFSMs

• Formal-language theory–CF-, 2DPDA-, 2NPDA-recognition–Attribute-grammar analysis

CFL-Reachability: Benefits• Algorithms

–Exhaustive & demand• Complexity

–Linear-time and cubic-time algorithms–PTIME-completeness–Variants that are undecidable

• Complementary to–Equations–Set constraints–Types–. . .