Presentation is loading. Please wait.

Presentation is loading. Please wait.

A New Algorithm for the Extraction of Non-contiguous Code

Similar presentations


Presentation on theme: "A New Algorithm for the Extraction of Non-contiguous Code"— Presentation transcript:

1 A New Algorithm for the Extraction of Non-contiguous Code
כמה מילים על ייחודיות לעומת עבודות קודמות – נכשלים בהרבה מקרים (Tucking) ואנחנו רוצים להתגבר על זה Moshe Zemah The Academic College of Tel Aviv-Yaffo April 2013

2 Agenda Introduction Previous work: Tucking and Sliding
The new algorithm: Statements Sliding Implementation + Demo Evaluation Future work לדבר על ה מוטיבציה לפני שמראים את ה AGENDA ולהדגיש מה אני עשיתי בעבודה הזאת

3 Refactoring Reorganize the code
Altering the code’s internal structure without changing its external behavior (preserve semantics) Examples: Encapsulate Field Rename Method/Variables Extract Method Steps: Identifying potential places for refactoring Apply the appropriate refactor technique Making sure semantics was preserved להגיד שיש עוד הרבה טכניקות של ריפקטורינג – למשל Extract local field

4 Refactoring Simplification of the code: Maintainability
Code duplication Long methods Extensibility Code reuse Supporting tools: Eclipse Visual Studio IntelliJ פישוט הקוד: אם קיים שכפול קוד – החלפת הקוד החוזר בקריאה למתודה תהפוך את התוכנית לקצרה יותר, קריאה וקלה לתחזוקה (תיקון באגים רק במקום אחד) פיצול מתודות ארוכות למספר מתודות קצרות – קוד יותר קריא ואפשרות לשימוש חוזר בקוד (Code Reuse) הפרדת הקוד וחלוקתו למתודות יוצרת פעולות עצמאיות אשר יתרמו להפיכת הקוד ליותר מודולרי ומונחה עצמים כלים תומכים: Eclipse, Visual Studio

5 The Problem Today’s Extract method technique (e.g. in Eclipse) is limited: Only works on contiguous code Sometimes Semantics is not preserved Example for non-contiguous code: Question– How the original method will be changed (and semantics will be preserved)? להראות אחרי השקף הזה את מה שרוצים להגיע אליו (על הדוגמא הזאת)

6 Expected Result Slice CoSlice Extract Method on Slice
להראות אחרי השקף הזה את מה שרוצים להגיע אליו (על הדוגמא הזאת)

7 Agenda Introduction Previous work: Tucking and Sliding
The new algorithm: Statements Sliding Implementation + Demo Evaluation Future work

8 Tucking An arbitrary-method-extraction transformation Flow:
A. Lakhotia and J.-C. Deprez, 1998 An arbitrary-method-extraction transformation Flow: Selection of statements and scope Compute the slice and its complement Compose them sequentially along with some compensation (i.e. backup of initial values and variable renaming) Computing the complement: The complement computed through slicing from all non-extracted statements (in the selected scope) Reject if both the slice and its complement define a variable that is live on exit from scope להגיד מה זה Slicing

9 Tucking Example 1 i = 0; 2 while(i < a.length) {
sum = sum + a[i]; product = product * a[i]; i = i + 1; } 1 i = 0; 2 while(i < a.length) { sum = sum + a[i]; i = i + 1; } product = product * a[i]; Slice CoSlice Computing Slice & CoSlice Compose the Slice and its Complement i = 0; while(i < a.length) { sum = sum + a[i]; i = i + 1; } product = product * a[i];

10 Tucking - Limitations Marked statements = 3,5,7,8,10 (all assignments to result) להגיד שהמספרים מייצגים פקודות ולא שורות

11 Tucking – Limitations Slice
להוציא את int result גם מה Slice וגם מ CoSlice

12 Tucking – Limitations CoSlice CoSlice is whole method
להוציא את int result גם מה Slice וגם מ CoSlice CoSlice is whole method Slice is Dead Code

13 Program Sliding As in Tucking:
By Ran Ettinger – PhD thesis 2006 As in Tucking: Splits the code into two: a slice and its complement Offers corrective measures (compensation) Unlike Tucking: Takes a set of variable names V as input instead of selection of statements Allows data flow from the slice to its complement (reuse of final values of variables in V) להגיד שה Slice מחשב את הערך הסופי של המשתנים ב- V.

14 Program Sliding Example
V = {result}

15 Program Sliding Example
Original Method Slice (V = {result}) = Tucking Slice

16 Program Sliding Example
Original Method V = {result} CoSlice (Reuse of V) להגיד שהשורות ב CoSlice מגיעות לשם כי ה COSLICE תפקידו לחשב את שאר המשתנים Smaller CoSlice

17 V = Suggestions? Program Sliding – Limitation Slice (V = {result})
Original Method Slice (V = {result}) CoSlice V = Suggestions?

18 V = Program Sliding – Limitation {result} Changed Method
Original Method V = {result}

19 Program Sliding – Limitation
Changed Method Slice (V = {result}) CoSlice לא ייתכן שה USER יצטרך לשנות את התוכנית המקורית כדי לבצע EXTRACT We need a general solution

20 PDG-Based Sliding Algorithm
By Ran Ettinger – ECOOP 2012 Compute Slice CoSlice Compensation Input: PDG (N, E, nentry , nexit) Set of variables V Output: 1. Slice Nodes Coslice Nodes Compensations (Pen1, Pen2, Pen3) Sliding on variables Sliding on statements להוסיף ecoop 2012

21 Agenda Introduction Previous work: Tucking and Sliding
The new algorithm: Statements Sliding Implementation + Demo Evaluation Future work

22 Static Single Assignment (SSA)
Cytron et al. TOPLAS 1991 T F T F SSA De-SSA T F

23 Static Single Assignment (SSA)
Cytron et al. TOPLAS 1991 Every definition gets its own version (Local variables) Converting ordinary code into SSA form: Replacing each assignment with a new variable Replacing each use of a variable with the "version" of the variable reaching that point Conflicts at merge points resolved by a special operation (Φ -function)

24 SSA Example Convert method to SSA form Original SSA SSA Form
לא להוסיף _1 לפרמטרים

25 Sliding on Statements (Using SSA)
SSA Form Slice (V = {result_1, result_2}, result_4, result_5}) CoSlice

26 Returning from SSA Form (De-SSA)
Slice De-SSA

27 Returning from SSA Form (De-SSA)
CoSlice De-SSA

28 Statements Sliding – First Attempt
Reduction to Variables Sliding Input Method Marked Statements Slice CoSlice Compen- sations SSA SSA Form V De-SSA Build PDG Variables Sliding Run Sliding PDG Is it always possible to De-SSA (and preserve semantics)?

29 De-SSA Example 84.5 V = {grade_1, grade_2} 85 Slice Slice 84.5 85 95
CoSlice De-SSA CoSlice 95 לדבר על LIVE ANALYSIS מוטיבציה ל Vr Vnr 100 Semantics was not preserved! Why not?

30 PDG-Based Sliding Algorithm
Introducing Vr and Vnr Compute Slice CoSlice Compensation Input: PDG (N, E, nentry , nexit) Set of variables Vr,Vnr Output: 1. Slice Nodes Coslice Nodes Compensations (Pen1, Pen2, Pen3) Vr,Vnr) האלגוריתם המקורי הוא מקרה פרטי כאשר Vr = V ו Vnr ריק להסביר מה זה Vnr

31 Live Analysis and De-SSA Example
Vr = {} Vnr = {grade_1, grade_2} Vr = {grade_1, grade_2} Vnr = {} Slice De-SSA Slice CoSlice De-SSA Slice CoSlice SSA CoSlice להזכיר רגיסטרים

32 Live Variables Analysis
Determines for each “point” in a program what (variable’s) values are live at that point Program “points” are defined before and after each statement To know if a variable is live, we need to look at its future uses A variable is live if it is used on some path Liveness is a backwards analysis

33 Live Variables Analysis – Example
Backwards analysis Live Variables: {a, b, x, y} Live Variables: {a, b, x} Live Variables: {a, b} Live Variables: {a, b} Live Variables: {a, b} Live Variables: {x} Live Variables: { } Statement Kill Gen 1 x = a + b x a, b 2 y = a * b y 3 if (y > a) a, y 4 a = a + 1 a 5 6 return x Kill הוא לעובדות ולא לערכים פעפוע של מידע

34 Live Variables Analysis – Example
Backwards analysis Live Variables: {grade_2} Live Variables: {grade_1, grade_2} Live Variables: {} Live Variables: {grade_3} Live Variables: {grade_4} Live Variables: { } grade_1 and grade_2 simultaneously live

35 Vr1 = {Variables that prevent correct De-SSA}
Statements Sliding Reduction to Variables Sliding Input Live Analysis Method Vr1 = {Variables that prevent correct De-SSA} Marked Statements Slice CoSlice Compen- sations SSA Add to Vr local defs Vr1 not empty Vr1 empty SSA Form Vr Vnr Move all Vr1 from Vr to Vnr De-SSA Build PDG Variables Sliding Run Sliding PDG Slice CoSlice Compen- sations

36 Statements Sliding Reduction on PDG-Based Sliding Algorithm
Input: 1. A method CFG(N, E) A Set M of marked nodes. Output: 1. Slice Nodes Coslice Nodes Compensations (Pen1, Pen2, Pen3) Convert method to SSA form Build a PDG (N, E) from the SSA form For each marked line: Add an edge to the exit in the PDG graph, for every variable that is defined (has definition) in the marked line Insert into Vr every SSA variable that has def in the marked line. Initiate Vnr to be empty. Compute NVr , the Slice for Vr For each node in NVr add to Vr every local variable that has definition. Add to Vr every global variable that all of its definitions are in NVr. Run Sliding for Vr,Vnr on the PDG from 2. Calculate the variables Vr1 from Vr that prevent correct return from SSA (De-SSA ) – using Live analysis If Vr1 is not empty – move all Vr1 variables from Vr to Vnr and go to 8. Perform De-SSA to the computed Slice and CoSlice after the Sliding. Return the resulted Slice and CoSlice (after the De-SSA) and compensations (Pen1, Pen2, Pen3).

37 Statements Sliding – Assumptions
Method has at most one return statement (at the end of the method) Predicates have no side effects (i.e. no assignments in condition) The marked rows have assignments to local variables only No dead code in the method Sliding removes all dead code statements, but tucking doesn’t, so we assume no dead code for fairness in our comparison with tucking

38 Statements Sliding Vs Tucking
Slice (both)

39 Statements Sliding Vs Tucking
CoSlice (Tucking) 10 CoSlice (Statements Sliding) 7

40 Statements Sliding Vs Tucking
CoSlice (Reduced Scope Tucking) Reduced Scope 6 CoSlice (Statements Sliding) 7 (5)

41 Agenda Introduction Previous work: Tucking and Sliding
The new algorithm: Statements Sliding Implementation + Demo Evaluation Future work

42 Implementation Implementation of “Program Sliding” algorithm was written in Java on Eclipse IDE. This tool can be used as a plugin for eclipse, by selecting the desired method and marking the selected lines. The implementation is based on an existing infrastructure called WALA (Watson Libraries for Analysis). אנחנו חלק מ WALA ותורמים לה כל העבודה Open Source ב WALA

43 Implementation - Results Analyzer
In order to quickly analyze the results from the implemented algorithm, I’ve developed a tool that layouts the result in an ordered and easy to understand manner

44 Demo

45 Agenda Introduction Previous work: Tucking and Sliding
The new algorithm: Statements Sliding Implementation + Demo Evaluation Future work

46 Experimental Results 27 methods taken from classes in package
org.eclipse.jdt.internal.compiler.codegen

47 Agenda Introduction Previous work: Tucking and Sliding
The new algorithm: Statements Sliding Implementation + Demo Evaluation Future work

48 Future Work Better use of live analysis results: Move only one of the live variables to Vnr (instead of both); investigate all subsets Live analysis improvements: Refined simultaneous liveness analysis (Conditioned liveness, Replication theorem) Sliding again (on the slice) for comparison with Bucketing Sliding on reduced scope (i.e. without return statement) להזכיר את העבודה של שי מניה בנוגע ל Bucketing

49 References Ettinger, R.: Refactoring via Program Slicing and Sliding. Ph.D. thesis, University of Oxford, 2006 Ettinger, R.: Program Sliding, ECOOP 2012. A. Lakhotia and J.-C. Deprez. Restructuring Programs by Tucking Statements into Functions. Information and Software Technology, 1998. R. Cytron, J. Ferrante, B. K. Rosen, M. N.Wegman, and F. K. Zadeck. Efficiently Computing Static Single Assignment Form and the Control Dependence Graph. ACM Transactions on Programming Languages and Systems, 1991. WALA - Komondoor, R. and Horwitz, S.: Effective automatic procedure extraction, IWPC, 2003.

50 Thank you!


Download ppt "A New Algorithm for the Extraction of Non-contiguous Code"

Similar presentations


Ads by Google