peephole.ml 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. open Printf
  2. open Types
  3. open Util
  4. let rec strip_comments = function
  5. | Comment _ :: tl -> strip_comments tl
  6. | InlineComment (EmptyLine, _) :: tl -> strip_comments tl
  7. | InlineComment (instr, _) :: tl -> strip_comments (instr :: tl)
  8. | hd :: tl -> hd :: (strip_comments tl)
  9. | [] -> []
  10. let rec peephole = function
  11. (* Constant load before branch becomes a jump when the branch condition
  12. * matches the loaded value ... *)
  13. | LoadImm (BoolVal b) :: Branch (cond, tgt) :: tl when cond = b ->
  14. InlineComment (Jump tgt, "branch -> jump") :: (peephole tl)
  15. (* ... otherwise, both instructions can be removed *)
  16. | LoadImm (BoolVal _) :: Branch (_, tgt) :: tl ->
  17. InlineComment (EmptyLine, "load + branch removed") :: peephole tl
  18. (* Transform addition/subtraction by constant to increment/decrement:
  19. * iload L | iload L
  20. * iloadc[_ ]C | iloadc_1
  21. * i{add,sub} | i{add,sub}
  22. * istore L | istore L
  23. * | |
  24. * v v
  25. * i{inc,dec} L C | i{inc,dec}_1 L
  26. *)
  27. | (Load (Int, Current, index) :: LoadConst (_, i) :: Op (Add, Int) ::
  28. Store (Int, Current, store) :: tl
  29. | LoadConst (_, i) :: Load (Int, Current, index) :: Op (Add, Int) ::
  30. Store (Int, Current, store) :: tl) when store = index ->
  31. InlineComment (Inc (index, i), "add -> inc") :: (peephole tl)
  32. | (Load (Int, Current, index) :: LoadConst (_, i) :: Op (Sub, Int) ::
  33. Store (Int, Current, store) :: tl
  34. | LoadConst (_, i) :: Load (Int, Current, index) :: Op (Sub, Int) ::
  35. Store (Int, Current, store) :: tl) when store = index ->
  36. InlineComment (Dec (index, i), "sub -> dec") :: (peephole tl)
  37. | (Load (Int, Current, index) :: LoadImm (IntVal 1) :: Op (Add, Int) ::
  38. Store (Int, Current, store) :: tl
  39. | LoadImm (IntVal 1) :: Load (Int, Current, index) :: Op (Add, Int) ::
  40. Store (Int, Current, store) :: tl) when store = index ->
  41. InlineComment (IncOne index, "add -> inc") :: (peephole tl)
  42. | (Load (Int, Current, index) :: LoadImm (IntVal 1) :: Op (Sub, Int) ::
  43. Store (Int, Current, store) :: tl
  44. | LoadImm (IntVal 1) :: Load (Int, Current, index) :: Op (Sub, Int) ::
  45. Store (Int, Current, store) :: tl) when store = index ->
  46. InlineComment (DecOne index, "sub -> dec") :: (peephole tl)
  47. | hd :: tl -> hd :: (peephole tl)
  48. | [] -> []
  49. (* Count actual instructions, ignoring comments and labels *)
  50. let count_instrs instrs =
  51. let rec trav n = function
  52. | [] -> n
  53. | (Comment _ | Label _ | EmptyLine) :: tl -> trav n tl
  54. | InlineComment (hd, _) :: tl -> trav (trav n [hd]) tl
  55. | hd :: tl -> trav (n + 1) tl
  56. in trav 0 instrs
  57. let phase input =
  58. match input with
  59. | Assembly instrs ->
  60. if args.optimize then (
  61. log_line 1 "- Peephole optimization";
  62. let oldcount = count_instrs instrs in
  63. let instrs = peephole (strip_comments instrs) in
  64. let newcount = count_instrs instrs in
  65. log_line 1 (sprintf
  66. " Optimized %d to %d instructions (%d difference)"
  67. oldcount newcount (newcount - oldcount)
  68. );
  69. Assembly instrs
  70. ) else
  71. input
  72. | _ -> raise (InvalidInput "peephole")