Guest User

Untitled

a guest
Feb 16th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 KB | None | 0 0
  1. From 713ddaae591a9d9ae26119548704b7d0be316d2f Mon Sep 17 00:00:00 2001
  2. From: Benedikt Meurer <benedikt.meurer@googlemail.com>
  3. Date: Sat, 20 Aug 2011 12:53:50 +0200
  4. Subject: [PATCH 1/3] Optimize Ccheckbound during cmmgen.
  5.  
  6. Attempt to replace Cmm constructs like
  7.  
  8. (checkbound (>>u arg n) m)
  9.  
  10. where n and m are integer constants with
  11.  
  12. (checkbound arg (m << m + (1 << n - 1)))
  13.  
  14. which is both shorter and results in faster code. On amd64 and i386 this
  15. replaces a sequence of mov,shr,cmp,jbe with a sequence of mov,cmp,jbe.
  16. This could be further optimized to a sequence of cmp,jbe in the backends.
  17.  
  18. Signed-off-by: Benedikt Meurer <benedikt.meurer@googlemail.com>
  19. ---
  20. asmcomp/cmmgen.ml | 33 ++++++++++++++++++++-------------
  21. 1 files changed, 20 insertions(+), 13 deletions(-)
  22.  
  23. diff --git a/asmcomp/cmmgen.ml b/asmcomp/cmmgen.ml
  24. index ca9d2f0..b8589d1 100644
  25. --- a/asmcomp/cmmgen.ml
  26. +++ b/asmcomp/cmmgen.ml
  27. @@ -369,6 +369,14 @@ let make_float_alloc tag args =
  28. make_alloc_generic float_array_set tag
  29. (List.length args * size_float / size_addr) args
  30.  
  31. +(* Bounds checking *)
  32. +
  33. +let make_checkbound dbg = function
  34. + | [Cop(Clsr, [a1; Cconst_int n]); Cconst_int m] when (m lsl n) > n ->
  35. + Cop(Ccheckbound dbg, [a1; Cconst_int(m lsl n + 1 lsl n - 1)])
  36. + | args ->
  37. + Cop(Ccheckbound dbg, args)
  38. +
  39. (* To compile "let rec" over values *)
  40.  
  41. let fundecls_size fundecls =
  42. @@ -534,7 +542,7 @@ let bigarray_elt_size = function
  43.  
  44. let bigarray_indexing unsafe elt_kind layout b args dbg =
  45. let check_bound a1 a2 k =
  46. - if unsafe then k else Csequence(Cop(Ccheckbound dbg, [a1;a2]), k) in
  47. + if unsafe then k else Csequence(make_checkbound dbg [a1;a2], k) in
  48. let rec ba_indexing dim_ofs delta_ofs = function
  49. [] -> assert false
  50. | [arg] ->
  51. @@ -1207,7 +1215,7 @@ and transl_prim_2 p arg1 arg2 dbg =
  52. (bind "str" (transl arg1) (fun str ->
  53. bind "index" (untag_int (transl arg2)) (fun idx ->
  54. Csequence(
  55. - Cop(Ccheckbound dbg, [string_length str; idx]),
  56. + make_checkbound dbg [string_length str; idx],
  57. Cop(Cload Byte_unsigned, [add_int str idx])))))
  58.  
  59. (* Array operations *)
  60. @@ -1231,21 +1239,20 @@ and transl_prim_2 p arg1 arg2 dbg =
  61. bind "arr" (transl arg1) (fun arr ->
  62. bind "header" (header arr) (fun hdr ->
  63. Cifthenelse(is_addr_array_hdr hdr,
  64. - Csequence(Cop(Ccheckbound dbg, [addr_array_length hdr; idx]),
  65. + Csequence(make_checkbound dbg [addr_array_length hdr; idx],
  66. addr_array_ref arr idx),
  67. - Csequence(Cop(Ccheckbound dbg, [float_array_length hdr; idx]),
  68. + Csequence(make_checkbound dbg [float_array_length hdr; idx],
  69. float_array_ref arr idx)))))
  70. | Paddrarray | Pintarray ->
  71. bind "index" (transl arg2) (fun idx ->
  72. bind "arr" (transl arg1) (fun arr ->
  73. - Csequence(Cop(Ccheckbound dbg, [addr_array_length(header arr); idx]),
  74. + Csequence(make_checkbound dbg [addr_array_length(header arr); idx],
  75. addr_array_ref arr idx)))
  76. | Pfloatarray ->
  77. box_float(
  78. bind "index" (transl arg2) (fun idx ->
  79. bind "arr" (transl arg1) (fun arr ->
  80. - Csequence(Cop(Ccheckbound dbg,
  81. - [float_array_length(header arr); idx]),
  82. + Csequence(make_checkbound dbg [float_array_length(header arr); idx],
  83. unboxed_float_array_ref arr idx))))
  84. end
  85.  
  86. @@ -1314,7 +1321,7 @@ and transl_prim_3 p arg1 arg2 arg3 dbg =
  87. (bind "str" (transl arg1) (fun str ->
  88. bind "index" (untag_int (transl arg2)) (fun idx ->
  89. Csequence(
  90. - Cop(Ccheckbound dbg, [string_length str; idx]),
  91. + make_checkbound dbg [string_length str; idx],
  92. Cop(Cstore Byte_unsigned,
  93. [add_int str idx; untag_int(transl arg3)])))))
  94.  
  95. @@ -1343,25 +1350,25 @@ and transl_prim_3 p arg1 arg2 arg3 dbg =
  96. bind "arr" (transl arg1) (fun arr ->
  97. bind "header" (header arr) (fun hdr ->
  98. Cifthenelse(is_addr_array_hdr hdr,
  99. - Csequence(Cop(Ccheckbound dbg, [addr_array_length hdr; idx]),
  100. + Csequence(make_checkbound dbg [addr_array_length hdr; idx],
  101. addr_array_set arr idx newval),
  102. - Csequence(Cop(Ccheckbound dbg, [float_array_length hdr; idx]),
  103. + Csequence(make_checkbound dbg [float_array_length hdr; idx],
  104. float_array_set arr idx
  105. (unbox_float newval)))))))
  106. | Paddrarray ->
  107. bind "index" (transl arg2) (fun idx ->
  108. bind "arr" (transl arg1) (fun arr ->
  109. - Csequence(Cop(Ccheckbound dbg, [addr_array_length(header arr); idx]),
  110. + Csequence(make_checkbound dbg [addr_array_length(header arr); idx],
  111. addr_array_set arr idx (transl arg3))))
  112. | Pintarray ->
  113. bind "index" (transl arg2) (fun idx ->
  114. bind "arr" (transl arg1) (fun arr ->
  115. - Csequence(Cop(Ccheckbound dbg, [addr_array_length(header arr); idx]),
  116. + Csequence(make_checkbound dbg [addr_array_length(header arr); idx],
  117. int_array_set arr idx (transl arg3))))
  118. | Pfloatarray ->
  119. bind "index" (transl arg2) (fun idx ->
  120. bind "arr" (transl arg1) (fun arr ->
  121. - Csequence(Cop(Ccheckbound dbg, [float_array_length(header arr);idx]),
  122. + Csequence(make_checkbound dbg [float_array_length(header arr);idx],
  123. float_array_set arr idx (transl_unbox_float arg3))))
  124. end)
  125. | _ ->
  126. --
  127. 1.7.4.4
Add Comment
Please, Sign In to add comment