reducing code size using outlining
play

Reducing Code Size Using Outlining Jessica Paquette Apple Outline - PowerPoint PPT Presentation

Reducing Code Size Using Outlining Jessica Paquette Apple Outline Code size Outlining Results Future work Motivating Example callq _printf movl $2, %edx movl -8(%rbp), %esi addl $1, %esi movl %esi, -8(%rbp) movl


  1. Reducing Code Size Using Outlining Jessica Paquette Apple

  2. Outline • Code size • Outlining • Results • Future work

  3. Motivating Example

  4. callq _printf movl $2, %edx movl -8(%rbp), %esi addl $1, %esi movl %esi, -8(%rbp) … movl -20(%rbp), %ecx movl $2, %edx movl -8(%rbp), %esi addl $1, %esi movl %esi, -8(%rbp) … movl $2, %edx movl -8(%rbp), %esi addl $1, %esi movl %esi, -8(%rbp)

  5. callq _printf movl $2, %edx movl -8(%rbp), %esi addl $1, %esi movl %esi, -8(%rbp) … NEW_FUNC: movl -20(%rbp), %ecx movl $2, %edx movl $2, %edx movl -8(%rbp), %esi movl -8(%rbp), %esi addl $1, %esi addl $1, %esi movl %esi, -8(%rbp) movl %esi, -8(%rbp) retq … movl $2, %edx movl -8(%rbp), %esi addl $1, %esi movl %esi, -8(%rbp)

  6. callq _printf movl $2, %edx movl -8(%rbp), %esi addl $1, %esi movl %esi, -8(%rbp) callq _printf … callq NEW_FUNC NEW_FUNC: movl -20(%rbp), %ecx movl $2, %edx … movl $2, %edx movl -8(%rbp), %esi movl -20(%rbp), %ecx movl -8(%rbp), %esi addl $1, %esi addl $1, %esi callq NEW_FUNC movl %esi, -8(%rbp) … movl %esi, -8(%rbp) retq … callq NEW_FUNC movl $2, %edx movl -8(%rbp), %esi addl $1, %esi movl %esi, -8(%rbp)

  7. callq _printf callq NEW_FUNC NEW_FUNC: movl $2, %edx … movl -8(%rbp), %esi movl -20(%rbp), %ecx addl $1, %esi callq NEW_FUNC movl %esi, -8(%rbp) … retq callq NEW_FUNC

  8. Outlining Replacing repeated sequences of instructions with calls to equivalent functions

  9. Outliner A pass that finds repeated instruction sequences and outlines them.

  10. G callq _printf G movl $2, %edx C movl -8(%rbp), %esi A addl $1, %esi T movl %esi, -8(%rbp) … C movl -20(%rbp), %ecx G movl $2, %edx C movl -8(%rbp), %esi A addl $1, %esi T movl %esi, -8(%rbp) … G movl $2, %edx C movl -8(%rbp), %esi A addl $1, %esi T movl %esi, -8(%rbp)

  11. A callq _printf B movl $2, %edx C movl -8(%rbp), %esi D addl $1, %esi E movl %esi, -8(%rbp) … F movl -20(%rbp), %ecx B movl $2, %edx movl -8(%rbp), %esi C addl $1, %esi D movl %esi, -8(%rbp) E … movl $2, %edx B movl -8(%rbp), %esi C addl $1, %esi D movl %esi, -8(%rbp) E

  12. Programs are like strings

  13. Find repeated substrings

  14. Suffix Tree A data structure for string searching (Not a suffix trie!)

  15. A B C A B $0 $0 C A B B $ AB 0 C A B $ 0 CAB$0 $0 0 $

  16. A B C A B $0 $0 C A B B $ AB 0 C A B $ 0 CAB$0 $0 0 $

  17. A B C A B $0 $0 C A B B $ AB 0 C A B $ 0 CAB$0 $0 0 $

  18. A B C A B $0 $0 C A B B $ AB 0 C A B $ 0 CAB$0 $0 0 $

  19. A B C A B $0 $0 C A B B $ AB 0 C A B $ 0 CAB$0 $0 0 $

  20. A B C A B $0 $0 C A B B $ AB 0 C A B $ 0 CAB$0 $0 0 $

  21. Advantages Given a string of length L… • O(L) construction • O(L) longest repeated substring • O(L) time most frequent substring

  22. Simplified Suffix Tree Construction

  23. A B C A B $0 Su ffi xes

  24. A B C A B $0 Su ffi xes

  25. A B C A B $0 Su ffi xes A

  26. A B C A B $0 Su ffi xes A

  27. A B C A B $0 Su ffi xes B, AB A

  28. A B C A B $0 Su ffi xes B AB

  29. A B C A B $0 Su ffi xes B AB

  30. A B C A B $0 Su ffi xes C C ABC B

  31. A B C A B $0 Su ffi xes C C ABC B

  32. A B C A B $0 Su ffi xes A C A A ABCA C B

  33. A B C A B $0 Su ffi xes A C A A ABCA C B

  34. A B C A B $0 Su ffi xes A B B B C ABCAB A A C B B

  35. A B C A B $0 Su ffi xes A B B B C ABCAB A A C B B

  36. A B C A B $0 Su ffi xes A B $0 0 ABCAB$0 $ B $0 B C A A C B B $0 $ 0

  37. A B C A B $0 Su ffi xes B $0 0 $ $0 B C A A C B B $ AB 0 CAB$0 0 $

  38. A B C A B $0 Su ffi xes B $0 0 $ $0 B C A A C B B $ AB 0 CAB$0 0 $

  39. A B C A B $0 Su ffi xes $0 C A B B $ AB 0 C A B $ 0 CAB$0 $0 0 $

  40. A B C A B $0 $0 Su ffi xes C A B B $ AB 0 C A B $ 0 CAB$0 $0 0 $

  41. $0 *Note: Real construction is more complex C A B B $ AB 0 Need to store links C A B $ 0 between internal nodes CAB$0 $0 0 $

  42. Representation

  43. Su ffi x Tree Struct struct SuffixTree { Node *Root; size_t LeafEnd; ActiveState Active; … };

  44. Su ffi x Tree Struct struct SuffixTree { Node *Root; size_t LeafEnd; ActiveState Active; StringType longestRepeatedSubstring(); void findOccurrences(std::vector<int> &Occurrences, const StringType &QueryStr); void prune(const StringType &Str); … };

  45. Node Struct struct Node { Node *Parent; std::map<CharacterType, Node *> Children; size_t StartIdx; size_t EndIdx; size_t SuffixIndex; … };

  46. Node Struct struct Node { Node *Parent; std::map<CharacterType, Node *> Children; size_t StartIdx; size_t EndIdx; size_t SuffixIndex; bool Valid; … };

  47. Outlining Example

  48. FOO ( ) A R1 = 0xDEADBEEF B R3 = R2 + R1 C R1 = *R5 D R7 = 0xFEEDFACE E R1 = R1 - 1 BAR ( ) F R7 = R3 + R2 A R1 = 0xDEADBEEF B R3 = R2 + R1 C R1 = *R5 G R7 = 0xFACEFEED A R1 = 0xDEADBEEF B R3 = R2 + R1 C R1 = *R5 E R1 = R1 - 1

  49. FOO ( ) String Encoding A R1 = 0xDEADBEEF B R3 = R2 + R1 C R1 = *R5 A B C D E D R7 = 0xFEEDFACE E R1 = R1 - 1 BAR ( ) F R7 = R3 + R2 A R1 = 0xDEADBEEF F A B C G A B C E B R3 = R2 + R1 C R1 = *R5 G R7 = 0xFACEFEED A R1 = 0xDEADBEEF B R3 = R2 + R1 C R1 = *R5 E R1 = R1 - 1

  50. FOO ( ) String Encoding A R1 = 0xDEADBEEF B R3 = R2 + R1 C R1 = *R5 A B C D E $0 Unique terminators D R7 = 0xFEEDFACE E R1 = R1 - 1 BAR ( ) F R7 = R3 + R2 A R1 = 0xDEADBEEF F A B C G A B C E $1 B R3 = R2 + R1 C R1 = *R5 G R7 = 0xFACEFEED A R1 = 0xDEADBEEF B R3 = R2 + R1 C R1 = *R5 E R1 = R1 - 1

  51. String Encoding A B C D E $0 F A B C G A B C E $1

  52. String Encoding A B C D E $0 F A B C G A B C E $1

  53. Find Candidates

  54. A B C D E $0 F A B C G A B C E $1 D E $0 F A B C G A B C E $1 G A B C E $1 G A B C E $ 1 E$1 D E $0 F A B C G A B C E $1 C G A B C E $1 A B C E$1 D E $0 F A B C G A B C E $1 B C D E $0 F A B C G A B C E $1 G A B C E $1 E$1 E F A B C G A B C E $0 F A B C G A B C E $1 $1

  55. Occurrences Length Longest repeated substring A B C D E $0 F A B C G A B C E $1 D E $0 F A B C G A B C E $1 G A B C E $1 G A B C E $ 1 E$1 0 0 None D E $0 F A B C G A B C E $1 C G A B C E $1 A B C E$1 D E $0 F A B C G A B C E $1 B C D E $0 F A B C G A B C E $1 G A B C E $1 E$1 E F A B C G A B C E $0 F A B C G A B C E $1 $1

  56. Occurrences Length Longest repeated substring A B C D E $0 F A B C G A B C E $1 D E $0 F A B C G A B C E $1 G A B C E $1 G A B C E $ 1 E$1 0 0 None D E $0 F A B C G A B C E $1 C G A B C E $1 A B C E$1 D E $0 F A B C G A B C E $1 B C D E $0 F A B C G A B C E $1 G A B C E $1 E$1 E F A B C G A B C E $0 F A B C G A B C E $1 $1

  57. Occurrences Length Longest repeated substring A B C D E $0 F A B C G A B C E $1 D E $0 F A B C G A B C E $1 G A B C E $1 G A B C E $ 1 E$1 3 1 C D E $0 F A B C G A B C E $1 C G A B C E $1 A B C E$1 D E $0 F A B C G A B C E $1 B C D E $0 F A B C G A B C E $1 G A B C E $1 E$1 E F A B C G A B C E $0 F A B C G A B C E $1 $1

  58. Occurrences Length Longest repeated substring A B C D E $0 F A B C G A B C E $1 D E $0 F A B C G A B C E $1 G A B C E $1 G A B C E $ 1 E$1 3 3 ABC D E $0 F A B C G A B C E $1 C G A B C E $1 A B C E$1 D E $0 F A B C G A B C E $1 B C D E $0 F A B C G A B C E $1 G A B C E $1 E$1 E F A B C G A B C E $0 F A B C G A B C E $1 $1

  59. Occurrences Length Longest repeated substring A B C D E $0 F A B C G A B C E $1 D E $0 F A B C G A B C E $1 G A B C E $1 G A B C E $ 1 E$1 3 3 ABC D E $0 F A B C G A B C E $1 C G A B C E $1 A B C E$1 D E $0 F A B C G A B C E $1 B C D E $0 F A B C G A B C E $1 G A B C E $1 E$1 E F A B C G A B C E $0 F A B C G A B C E $1 $1

  60. Occurrences Length Longest repeated substring A B C D E $0 F A B C G A B C E $1 D E $0 F A B C G A B C E $1 G A B C E $1 G A B C E $ 1 E$1 3 3 ABC D E $0 F A B C G A B C E $1 C G A B C E $1 A B C E$1 D E $0 F A B C G A B C E $1 B C D E $0 F A B C G A B C E $1 G A B C E $1 E$1 E F A B C G A B C E $0 F A B C G A B C E $1 $1

  61. Occurrences Length Longest repeated substring A B C D E $0 F A B C G A B C E $1 D E $0 F A B C G A B C E $1 G A B C E $1 G A B C E $ 1 E$1 3 3 ABC D E $0 F A B C G A B C E $1 C G A B C E $1 A B C E$1 D E $0 F A B C G A B C E $1 B C D E $0 F A B C G A B C E $1 G A B C E $1 E$1 E F A B C G A B C E $0 F A B C G A B C E $1 $1

  62. Occurrences Length Longest repeated substring A B C D E $0 F A B C G A B C E $1 D E $0 F A B C G A B C E $1 G A B C E $1 G A B C E $ 1 E$1 3 3 ABC D E $0 F A B C G A B C E $1 C G A B C E $1 A B C E$1 D E $0 F A B C G A B C E $1 B C D E $0 F A B C G A B C E $1 G A B C E $1 E$1 E F A B C G A B C E $0 F A B C G A B C E $1 $1

Recommend


More recommend