Fine Slicing Theory and Applications for Computation Extraction

  • Slides: 37
Download presentation
Fine Slicing: Theory and Applications for Computation Extraction Aharon Abadi, Ran Ettinger, Yishai A.

Fine Slicing: Theory and Applications for Computation Extraction Aharon Abadi, Ran Ettinger, Yishai A. Feldman IBM Research – Haifa 1

Program Slicing Program Slice Start Slice x : = exp The same sequence of

Program Slicing Program Slice Start Slice x : = exp The same sequence of values 2

Example x : = 6 y : = 3 f(x, y) x : =

Example x : = 6 y : = 3 f(x, y) x : = 9 y: =2 w : = x*y w : = 18 x : = 6 y : = 2 f(x, y) x : = 9 y: =2 w : = x*y z : = w x : = 6 y : = 3 x : = 9 y: =2 w : = x*y w : = 18 x : = 9 y: =2 w : = x*y z : = w 3

Control-Flow Path Compression test X if-zero-go-to A. . . L: test Y if-zero- go-to

Control-Flow Path Compression test X if-zero-go-to A. . . L: test Y if-zero- go-to B. . . go-to L A: Z 0. . . B: Work in two stages: - Compute the ‘traditional’ slice - Control dependences - Data Dependences - Compute the necessary branches to prevent infeasible control paths 4

Control-Flow Path Compression test X if-zero-go-to A . . . go-to B A: Z

Control-Flow Path Compression test X if-zero-go-to A . . . go-to B A: Z 0 L: test Y if-zero- go-to B B: . . . go-to L A: Z 0. . . B: This algorithm: - preserves behavior - yields a sub-program - one version may turn conditional branches into unconditional ones (“rhetorization”) 5

Data-Flow Path Compression Start: R 2: =0 R 7: =exp 1 Loop: R 2:

Data-Flow Path Compression Start: R 2: =0 R 7: =exp 1 Loop: R 2: =R 2 + 1 compare R 2, R 9 R 7: =exp 1 if-not-less-go-to Out Out: R 0: =R 7 + 1 Temp: =R 7 use R 7: =Temp: =R 7; spill R 7 to memory go-to Loop Out: R 0: =R 7 + 1 … ; code that uses R 7: =exp 1 ; all registers R 7: =Temp; restore R 7 go-to Loop Out: R 0: = R 7 + 1 The result is too large The value of R 7 does not depend on the loop 6

Control-Flow Path Compression if (x<11) goto A 4 F x : = x+1 goto

Control-Flow Path Compression if (x<11) goto A 4 F x : = x+1 goto A 2 A 4: if (x<9) goto A 3 x : = x-1 A 1: if (y<T) goto A 2 y : = y– 1 x<11 T goto A 4 x: =x+1 T goto A 2 x<9 F goto A 3 x: =x+2 x: =x-1 goto A 1 T A 3: x : = x+2 y<T F goto A 2: print(x) y: =y-1 goto A 7

Compute the ‘Traditional’ Slice if (x<11) goto A 4 F x : = x+1

Compute the ‘Traditional’ Slice if (x<11) goto A 4 F x : = x+1 goto A 2 A 4: if (x<9) goto A 3 x : = x-1 A 1: if (y<T) goto A 2 y : = y– 1 x<11 T goto A 4 x: =x+1 T goto A 2 x<9 F goto A 3 x: =x+2 x: =x-1 goto A 1 T A 3: x : = x+2 y<T F goto A 2: print(x) y: =y-1 goto A 8

Completing Control Flow Paths: Main Lemma All paths from the same point in the

Completing Control Flow Paths: Main Lemma All paths from the same point in the slice enter the slice at a single point • precisely identifies the possible sets of branches that may be added to the slice • any path in the original program can be chosen • optimizations can be performed 9

Compute the Necessary Branches F if(x<11) goto A 4 x: =x+1 goto A 2

Compute the Necessary Branches F if(x<11) goto A 4 x: =x+1 goto A 2 A 4: if(x<9) goto A 3 x: =x-1 A 1: if(y<T) goto A 2 y: =y– 1 x<11 T x: =x+1 goto A 4 goto A 2 T x<9 F goto A 3 x: =x+2 x: =x-1 goto A 1 T A 3: x: =x+2 y<T F goto A 2: print(x) y: =y-1 goto A 10

Data-Flow Path Compression Start: R 2: =0 R 7: =exp 1 Loop: R 2:

Data-Flow Path Compression Start: R 2: =0 R 7: =exp 1 Loop: R 2: =R 2 + 1 compare R 2, R 9 if-not-less-go-to Out use R 7 Temp: =R 7; spill R 7 to ; memory … ; code that uses ; all registers R 7: =Temp; restore R 7 go-to Loop Out: R 0: =R 7 + 1 R 2: =0 R 7: =exp 1 R 2: =R 2+1 compare R 2, R 9 if-not-less use R 7 go-to Out Temp: =R 7 R 7: =exp 1 Out: R 0: =R 7 + 1 +1 R 0: =R 7+1 R 7: =Temp goto Loop exit 11

Data-Flow Path Compression Start: R 2: =0 R 7: =exp 1 Loop: R 2:

Data-Flow Path Compression Start: R 2: =0 R 7: =exp 1 Loop: R 2: =R 2 + 1 compare R 2, R 9 if-not-less-go-to Out use R 7 Temp: =R 7; spill R 7 to ; memory … ; code that uses ; all registers R 7: =Temp; restore R 7 go-to Loop Out: R 0: =R 7+1 R 2: =0 0 d 1 d 2 R 7: =exp 1 d 1 R 2: =R 2+1 ++ in data port holds the next value compare R 2, R 9 out data port holds the last value if-not-less use R 7 go-to Out Temp: =R 7 • The Plan Calculus: The Programmer’s Apprentice, Rich and Waters, 1990 • R 7, Temp carry the value of exp 1 R 7: =R 7+1 ++ R 7: =Temp • Use data edges instead of variables goto-Loop exit 12

Start: R 2: =0 R 7: =exp 1 Loop: R 2: =R 2 +

Start: R 2: =0 R 7: =exp 1 Loop: R 2: =R 2 + 1 compare R 2, R 9 if-not-less-go-to Out use R 7 Temp: =R 7; spill R 7 to ; memory … ; code that uses ; all registers R 7: =Temp; restore R 7 go-to Loop Out: R 0: =R 7 + 1 entry exp 1 0 R 2 R 9 R 2 ++ R 7 compare R 2, R 9 if-not-less R 7: = exp 1 R 0: =R 7 + 1 T F use R 7 ++ R 0 exit 13

Decompression Start: R 2: =0 R 7: =exp 1 Loop: R 2: =R 2

Decompression Start: R 2: =0 R 7: =exp 1 Loop: R 2: =R 2 + 1 compare R 2, R 9 if-not-less- go-to Out use R 7 Temp: =R 7; spill R 7 to ; memory … ; code that uses ; all registers R 7: =Temp; restore R 7 go-to Loop Out: R 0: =R 7 + 1 entry exp 1 0 R 2 R 9 R 2 ++ R 7 compare R 2, R 9 if-not-less T R 7: =exp 1 F use R 7 go-to Out: R 0: =R 7 + 1 ++ R 0 exit 14

If(q) goto Skip If(q) i : = 0 x : = 1 Loop: If(i>10)

If(q) goto Skip If(q) i : = 0 x : = 1 Loop: If(i>10) goto Out i : = i + 1 goto loop i If(q) goto Skip 0 i : = 0 x : = 1 goto Skip goto Out Skip: x : = 2 Out: print(x) 2 Skip: x : = 2 Out: print(x) x : = 2 If(i>10) 1 + goto out x : = 2 x : = 1 print(x) 15

Fine Slicing Motivating Example Automatically extracts Out computation (The view model) out. println("<table border=0>");

Fine Slicing Motivating Example Automatically extracts Out computation (The view model) out. println("<table border=0>"); int start = page * 20; int end = start + 20; end = Math. min(end, album. get. Pictures(). size()); for (int i = start; i < end; i++) { Picture picture = album. get. Picture(i); print. Picture(out, picture); } out. println("</table>"); int start = page * 20; int end = start + 20; end = Math. min(end, album. get. Pictures(). size()); Queue<Picture> pictures = new Linked. List<Picture>(); for (int i = start; I < end; i++) { Picture picture = album. get. Picture(i); pictures. add(picture); } display(out, start, end, pictures); void display(Print. Stream out, int start, int end, Queue<Picture> pictures) { out. println("<table border=0>"); for (int i = start; i < end; i++) { print. Picture(out, pictures. remove()); } out. println("</table>"); } 16

slice (v. ): to cut with or as if with a knife slice (n.

slice (v. ): to cut with or as if with a knife slice (n. ): a thin flat piece cut from something 17 Merriam-Webster

Fine Slicing • A generalization of traditional program slicing • Fine slices can be

Fine Slicing • A generalization of traditional program slicing • Fine slices can be precisely bounded – Slicing criteria include set of data and control dependences to ignore • Fine slices are executable and extractable • Complement slices (co-slices) are also fine slices • Oracle-based semantics for fine slices • Algorithm for computing data-structure representing the oracle 18

Extract Computation • • A new refactoring Extracts a fine slice into contiguous code

Extract Computation • • A new refactoring Extracts a fine slice into contiguous code Computes the co-slice Computation can then be extracted into a separate method using Extract Method • Passes necessary “oracle” variables between slice and co-slice • Generates new containers if series of values need to be passed 19

entry out. println("<table border=0>"); int start = page * 20; int end = start

entry out. println("<table border=0>"); int start = page * 20; int end = start + 20; end = Math. min(end, album. get. Pictures(). size()); for (int i = start; i < end; i++) { Picture picture = album. get. Picture(i); print. Picture(out, picture); } out. println("</table>"); "<table border=0>" out println page Token Semantics 20 * start + out album out get. Pictures i size end min get. Picture p 2 p 1 end "</table>" out i out p 2 p 1 < print. Picture T ++ println F exit 20

entry out. println("<table border=0>"); int start = page * 20; int end = start

entry out. println("<table border=0>"); int start = page * 20; int end = start + 20; end = Math. min(end, album. get. Pictures(). size()); for (int i = start; i < end; i++) { Picture picture = album. get. Picture(i); print. Picture(out, picture); } out. println("</table>"); "<table border=0>" out println page Fine Slicing 20 * start + out album out get. Pictures i size end min get. Picture end "</table>" out i out < print. Picture T ++ println F exit 21

entry out. println("<table border=0>"); for (int i = start; i < end; i++) {

entry out. println("<table border=0>"); for (int i = start; i < end; i++) { print. Picture(out, picture); } out. println("</table>"); "<table border=0>" out println start picture p 1 p 2 p 3 end The Fine Slice out i "</table>" out i out < print. Picture T ++ println F exit 22

entry out. println("<table border=0>"); int start = page * 20; int end = start

entry out. println("<table border=0>"); int start = page * 20; int end = start + 20; end = Math. min(end, album. get. Pictures(). size()); for (int i = start; i < end; i++) { Picture picture = album. get. Picture(i); print. Picture(out, picture); } out. println("</table>"); "<table border=0>" out println page Co-Slicing 20 * start + out album out get. Pictures i size end min get. Picture end "</table>" out i out < print. Picture T ++ println F exit 23

The Co. Slice entry int start = page * 20; int end = start

The Co. Slice entry int start = page * 20; int end = start + 20; end = Math. min(end, album. get. Pictures(). size()); for (int i = start; i < end; i++) { Picture picture = album. get. Picture(i); } page 20 * start + album get. Pictures i size end min get. Picture i start picture out < end T ++ F exit 24

Co-slice Fine slice entry page entry 20 "<table border=0>" * out start println +

Co-slice Fine slice entry page entry 20 "<table border=0>" * out start println + end start picture album get. Pictures i end size min get. Picture p 1 p 2 p 3 picture start i i out < "</table>" < print. Picture T ++ end F T println out exit ++ F A place for The call out 25 exit

How do we distinguish between sequence of values and single value? entry "<table border=0>"

How do we distinguish between sequence of values and single value? entry "<table border=0>" out println page 20 * start + out album out Every cycle of control that passes through entry out din does not pass through println dout Therefore din requires single value start picture get. Pictures "<table border=0>" end out i i size end min get. Picture end out "</table>" i out < print. Picture T ++ println F exit T println F exit ++ 26

How do we distinguish between sequence of values and single value? entry "<table border=0>"

How do we distinguish between sequence of values and single value? entry "<table border=0>" out println page 20 * start + out album out There is a cycle of control that passes through din and dout Therefore din may require multiple single value picture get. Pictures entry out "<table border=0>" println start end out i i size end min get. Picture end out "</table>" i out < print. Picture T ++ println F exit T println F exit ++ 27

Adding Container out. println("<table border=0>"); int start = page * 20; int end =

Adding Container out. println("<table border=0>"); int start = page * 20; int end = start + 20; end = Math. min(end, album. get. Pictures(). size()); for (int i = start; i < end; i++) { Picture picture = album. get. Picture(i); print. Picture(out, picture); } out. println("</table>"); void display(Print. Stream out, int start, int end, Picture picture) { out. println("<table border=0>"); for (int i = start; i < end; i++) { print. Picture(out, picture); } out. println("</table>"); } out. println("<table border=0>"); int start = page * 20; void display(Print. Stream out, int start, int end = start + 20; int end, Queue<Picture> pictures) { end = Math. min(end, out. println("<table border=0>"); album. get. Pictures(). size()); Queue<Picture> pictures = new Linked. List<Picture>(); for (int i = start; i < end; i++) {{ for (int i = start; i < end; print. Picture(out, pictures. remove()); Picture picture = album. get. Picture(i); } pictures. add(picture); out. println("</table>"); print. Picture(out, pictures. remove()); } } out. println("</table>"); 28

Adding Container out. println("<table border=0>"); void display(Print. Stream out, int start = page *

Adding Container out. println("<table border=0>"); void display(Print. Stream out, int start = page * 20; int end, Queue<Picture> pictures) { int end = start + 20; out. println("<table border=0>"); end = Math. min(end, for (int i = start; i < end; i++) { album. get. Pictures(). size()); print. Picture(out, pictures. remove()); Queue<Picture> pictures = new Linked. List<Picture>(); for (int i = start; i < end; i++) { } Picture picture = album. get. Picture(i); out. println("</table>"); pictures. add(picture); } print. Picture(out, pictures. remove()); } out. println("</table>"); int start = page * 20; int end = start + 20; end = Math. min(end, album. get. Pictures(). size()); Queue<Picture> pictures = new Linked. List<Picture>(); for (int i = start; I < end; i++) { Picture picture = album. get. Picture(i); pictures. add(picture); } display(out, start, end, pictures); 29

out. println("<table border=0>"); entry "<table border=0>" int start = page * 20; out int

out. println("<table border=0>"); entry "<table border=0>" int start = page * 20; out int end = start + 20; println end = Math. min(end, album. get. Pictures(). size()); page 20 Queue<Picture> pictures = new Linked. List<Picture>(); for (int i = start; i < end; i++) { * Picture picture = album. get. Picture(i); start pictures. add(picture); print. Picture(out, pictures. remove()); } + out album out. println("</table>"); out Adding a Container get. Pictures i pictures size end min get. Picture new pictures add end pictures remove "</table>" out i out picture < print. Picture T ++ println F exit 30

entry void display(Print. Stream out, int start, int end, Queue<Picture> pictures){ out. println("<table border=0>");

entry void display(Print. Stream out, int start, int end, Queue<Picture> pictures){ out. println("<table border=0>"); for (int i = start; i < end; i++) { print. Picture(out, pictures. remove()); } out. println("</table>"); } "<table border=0>" out The Fine Slice println end start pictures out i pictures "</table>" remove out i out picture < print. Picture T ++ println F exit 31

out. println("<table border=0>"); entry "<table border=0>" int start = page * 20; out int

out. println("<table border=0>"); entry "<table border=0>" int start = page * 20; out int end = start + 20; println end = Math. min(end, album. get. Pictures(). size()); page 20 Queue<Picture> pictures = new Linked. List<Picture>(); for (int i = start; i < end; i++) { * Picture picture = album. get. Picture(i); start pictures. add(picture); print. Picture(out, pictures. remove()); } + out album out. println("</table>"); out Program with Container get. Pictures i pictures size end min get. Picture new pictures add end pictures remove "</table>" out i out picture < print. Picture T ++ println F exit 32

int start = page * 20; int end = start + 20; end =

int start = page * 20; int end = start + 20; end = Math. min(end, album. get. Pictures(). size()); Queue<Picture> pictures = new Linked. List<Picture>(); for (int i = start; i < end; i++) { Picture picture = album. get. Picture(i); pictures. add(picture); } display(out, start, end, pictures); entry out page The Co. Slice 20 * start + album get. Pictures start i pictures size end min get. Picture new pictures picture add pictures end pictures i < display T ++ F out exit 33

Fine Slicing Motivating Example Automatically extracts Out computation (The view model) out. println("<table border=0>");

Fine Slicing Motivating Example Automatically extracts Out computation (The view model) out. println("<table border=0>"); int start = page * 20; int end = start + 20; end = Math. min(end, album. get. Pictures(). size()); for (int i = start; i < end; i++) { Picture picture = album. get. Picture(i); print. Picture(out, picture); } out. println("</table>"); int start = page * 20; int end = start + 20; end = Math. min(end, album. get. Pictures(). size()); Queue<Picture> pictures = new Linked. List<Picture>(); for (int i = start; I < end; i++) { Picture picture = album. get. Picture(i); pictures. add(picture); } display(out, start, end, pictures); void display(Print. Stream out, int start, int end, Queue<Picture> pictures) { out. println("<table border=0>"); for (int i = start; i < end; i++) { print. Picture(out, pictures. remove()); } out. println("</table>"); } 34

Fine Slicing Algorithm • Compute the core slice by following unfiltered data and control

Fine Slicing Algorithm • Compute the core slice by following unfiltered data and control dependence relations backwards in the plan. • (Semantic Restoration) Add necessary tests to make the slice executable. out. println("<table border=0>"); int start = page * 20; int end = start + 20; end = Math. min(end, album. get. Pictures(). size()); for (int i = start; i < end; i++) { Picture picture = album. get. Picture(i); print. Picture(out, picture); } out. println("</table>"); 35

Fine Slicing Algorithm • Compute the core slice by following unfiltered data and control

Fine Slicing Algorithm • Compute the core slice by following unfiltered data and control dependence relations backwards in the plan. • (Semantic Restoration) Add necessary tests to make the slice executable. Stat 1; if(q){ x = x + 1; ; }else{ y = x + 2; stat 1; x = x + 1; ; y = x + 2; } 36

Extract Computation • Get as input the program (P) and slicing criteria (SC) •

Extract Computation • Get as input the program (P) and slicing criteria (SC) • Compute the fine slice (FS) corresponding to P and SC • Compute the Co-Slice: – Compute fine slice starting from PFS – Disconnect the return values according to SC • Add containers to the fine slice and co-slice • Add the call from co-slice to the fine slice 37