1 / 50

A New Algorithm for the Extraction of Non-contiguous Code

This paper presents a new algorithm for extracting non-contiguous code, addressing limitations of existing techniques. The algorithm, called statements sliding, provides a way to extract code while preserving semantics. The implementation, evaluation, and future work are also discussed.

mayo
Download Presentation

A New Algorithm for the Extraction of Non-contiguous Code

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A New Algorithm for the Extraction of Non-contiguous Code 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

  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

  4. Refactoring • Simplification of the code: • Maintainability • Code duplication • Long methods • Extensibility • Code reuse • Supporting tools: • Eclipse • Visual Studio • IntelliJ

  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 Extract Method on Slice Slice CoSlice

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

  8. Tucking 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

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

  10. Tucking - Limitations Marked statements = 3,5,7,8,10 (all assignments to result)

  11. Tucking – Limitations Slice

  12. Tucking – Limitations CoSlice • CoSlice is whole method • Slice is Dead Code

  13. Program Sliding 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)

  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) Smaller CoSlice

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

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

  19. Program Sliding – Limitation Slice (V = {result}) CoSlice Changed Method We need a general solution

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

  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 SSA De-SSA T F 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

  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 Variables Sliding SSA Form V De-SSA Build PDG PDG Run Sliding Is it always possible to De-SSA (and preserve semantics)?

  29. De-SSA Example 84.5 V = {grade_1, grade_2} 85 Slice Slice De-SSA 84.5 85 95 CoSlice CoSlice De-SSA SSA 95 100 Semantics was not preserved! Why not?

  30. PDG-Based Sliding Algorithm Introducing Vr and Vnr Output: 1. Slice Nodes 2. Coslice Nodes 3. Compensations (Pen1, Pen2, Pen3) Input: PDG (N, E, nentry , nexit) Set of variables Vr,Vnr Vr,Vnr) Compute Slice Compute CoSlice Compute Compensation

  31. Live Analysis and De-SSA Example Vr = {grade_1, grade_2} Vnr = {} Vr = {} Vnr = {grade_1, grade_2} Slice Slice Slice De-SSA De-SSA CoSlice CoSlice CoSlice De-SSA De-SSA SSA

  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: { } Live Variables: {x} Live Variables: {a, b} Live Variables: {a, b} Live Variables: {a, b, x, y} Live Variables: {a, b, x} Live Variables: {a, b}

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

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

  36. Statements Sliding Reduction on PDG-Based Sliding Algorithm Input: 1. A method CFG(N, E) 2. A Set M of marked nodes. Output: 1. Slice Nodes 2. Coslice Nodes 3. Compensations (Pen1, Pen2, Pen3) Convert method to SSA form Build a PDG (N, E) from the SSA form For each marked line:3.1. Add an edge to the exit in the PDG graph, for every variable that is defined (has definition) in the marked line.3.2. Insert into Vr every SSA variable that has defin 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). http://wala.sourceforge.net/wiki/index.php/Main_Page

  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)

  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 - http://wala.sourceforge.net/wiki/index.php/Main_Page • Komondoor, R. and Horwitz, S.: Effective automatic procedure extraction, IWPC, 2003.

  50. Thank you!

More Related