optloadmodifystore.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. {
  2. Copyright (c) 2017 by Nikolay Nikolov
  3. Optimizations for making use of load-modify-store operations in CISC-like
  4. instruction set architectures (such as x86)
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit optloadmodifystore;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node;
  23. procedure do_optloadmodifystore(var rootnode : tnode);
  24. implementation
  25. uses
  26. globtype,verbose,nutils,
  27. defutil,defcmp,htypechk,pass_1,
  28. nadd,ncal,ncnv,ninl,nld;
  29. function try_opt_assignmentnode(assignmentnode: tassignmentnode): tnode;
  30. var
  31. newinlinenodetype: byte;
  32. begin
  33. result:=nil;
  34. with assignmentnode do
  35. begin
  36. { replace i:=succ/pred(i) by inc/dec(i)? }
  37. if (right.nodetype=inlinen) and
  38. ((tinlinenode(right).inlinenumber=in_succ_x) or (tinlinenode(right).inlinenumber=in_pred_x)) and
  39. (tinlinenode(right).left.isequal(left)) and
  40. ((localswitches*[cs_check_overflow,cs_check_range])=[]) and
  41. ((right.localswitches*[cs_check_overflow,cs_check_range])=[]) and
  42. valid_for_var(tinlinenode(right).left,false) and
  43. not(might_have_sideeffects(tinlinenode(right).left)) then
  44. begin
  45. if tinlinenode(right).inlinenumber=in_succ_x then
  46. newinlinenodetype:=in_inc_x
  47. else
  48. newinlinenodetype:=in_dec_x;
  49. result:=cinlinenode.createintern(
  50. newinlinenodetype,false,ccallparanode.create(
  51. tinlinenode(right).left,nil));
  52. tinlinenode(right).left:=nil;
  53. exit;
  54. end;
  55. { replace i:=i+k by inc(i,k)
  56. i:=i-k by dec(i,k)
  57. i:=i and/or/xor k by in_[and/or/xor]_assign_x_y(i,k)
  58. this handles the case, where there are no implicit type conversions }
  59. if (right.nodetype in [addn,subn,andn,orn,xorn]) and
  60. (taddnode(right).left.isequal(left)) and
  61. is_integer(taddnode(right).left.resultdef) and
  62. is_integer(taddnode(right).right.resultdef) and
  63. ((localswitches*[cs_check_overflow,cs_check_range])=[]) and
  64. ((right.localswitches*[cs_check_overflow,cs_check_range])=[]) and
  65. valid_for_var(taddnode(right).left,false) and
  66. not(might_have_sideeffects(taddnode(right).left)) then
  67. begin
  68. case right.nodetype of
  69. addn:
  70. newinlinenodetype:=in_inc_x;
  71. subn:
  72. newinlinenodetype:=in_dec_x;
  73. andn:
  74. newinlinenodetype:=in_and_assign_x_y;
  75. orn:
  76. newinlinenodetype:=in_or_assign_x_y;
  77. xorn:
  78. newinlinenodetype:=in_xor_assign_x_y;
  79. else
  80. internalerror(2017032901);
  81. end;
  82. if right.nodetype in [addn,subn] then
  83. result:=cinlinenode.createintern(
  84. newinlinenodetype,false,ccallparanode.create(
  85. taddnode(right).left,ccallparanode.create(taddnode(right).right,nil)))
  86. else
  87. result:=cinlinenode.createintern(
  88. newinlinenodetype,false,ccallparanode.create(
  89. taddnode(right).right,ccallparanode.create(taddnode(right).left,nil)));
  90. taddnode(right).left:=nil;
  91. taddnode(right).right:=nil;
  92. exit;
  93. end;
  94. { replace i:=i+k by inc(i,k)
  95. i:=i-k by dec(i,k)
  96. i:=i and/or/xor k by in_[and/or/xor]_assign_x_y(i,k)
  97. this handles the case with two conversions (outer and inner):
  98. outer typeconv: right
  99. add/sub/and/or/xor: ttypeconvnode(right).left
  100. inner typeconv: taddnode(ttypeconvnode(right).left).left
  101. right side 'i': ttypeconvnode(taddnode(ttypeconvnode(right).left).left).left
  102. right side 'k': taddnode(ttypeconvnode(right).left).right }
  103. if (right.nodetype=typeconvn) and
  104. (ttypeconvnode(right).convtype=tc_int_2_int) and
  105. (ttypeconvnode(right).left.nodetype in [addn,subn,andn,orn,xorn]) and
  106. is_integer(ttypeconvnode(right).left.resultdef) and
  107. (right.resultdef.size<=ttypeconvnode(right).left.resultdef.size) and
  108. (taddnode(ttypeconvnode(right).left).left.nodetype=typeconvn) and
  109. (ttypeconvnode(taddnode(ttypeconvnode(right).left).left).convtype=tc_int_2_int) and
  110. are_equal_ints(right.resultdef,ttypeconvnode(taddnode(ttypeconvnode(right).left).left).left.resultdef) and
  111. ttypeconvnode(taddnode(ttypeconvnode(right).left).left).left.isequal(left) and
  112. is_integer(taddnode(ttypeconvnode(right).left).left.resultdef) and
  113. is_integer(taddnode(ttypeconvnode(right).left).right.resultdef) and
  114. is_integer(ttypeconvnode(taddnode(ttypeconvnode(right).left).left).left.resultdef) and
  115. ((localswitches*[cs_check_overflow,cs_check_range])=[]) and
  116. ((right.localswitches*[cs_check_overflow,cs_check_range])=[]) and
  117. valid_for_var(ttypeconvnode(taddnode(ttypeconvnode(right).left).left).left,false) and
  118. not(might_have_sideeffects(ttypeconvnode(taddnode(ttypeconvnode(right).left).left).left)) then
  119. begin
  120. case ttypeconvnode(right).left.nodetype of
  121. addn:
  122. newinlinenodetype:=in_inc_x;
  123. subn:
  124. newinlinenodetype:=in_dec_x;
  125. andn:
  126. newinlinenodetype:=in_and_assign_x_y;
  127. orn:
  128. newinlinenodetype:=in_or_assign_x_y;
  129. xorn:
  130. newinlinenodetype:=in_xor_assign_x_y;
  131. else
  132. internalerror(2017032901);
  133. end;
  134. inserttypeconv_internal(taddnode(ttypeconvnode(right).left).right,left.resultdef);
  135. if ttypeconvnode(right).left.nodetype in [addn,subn] then
  136. result:=cinlinenode.createintern(
  137. newinlinenodetype,false,ccallparanode.create(
  138. ttypeconvnode(taddnode(ttypeconvnode(right).left).left).left,ccallparanode.create(taddnode(ttypeconvnode(right).left).right,nil)))
  139. else
  140. result:=cinlinenode.createintern(
  141. newinlinenodetype,false,ccallparanode.create(
  142. taddnode(ttypeconvnode(right).left).right,ccallparanode.create(ttypeconvnode(taddnode(ttypeconvnode(right).left).left).left,nil)));
  143. ttypeconvnode(taddnode(ttypeconvnode(right).left).left).left:=nil;
  144. taddnode(ttypeconvnode(right).left).right:=nil;
  145. exit;
  146. end;
  147. { replace i:=k+i by inc(i,k)
  148. i:=k and/or/xor i by in_[and/or/xor]_assign_x_y(i,k)
  149. todo: for some integer types, there are extra implicit
  150. typecasts inserted by the compiler; this code should be
  151. updated to handle them as well }
  152. if (right.nodetype in [addn,andn,orn,xorn]) and
  153. (taddnode(right).right.isequal(left)) and
  154. is_integer(taddnode(right).left.resultdef) and
  155. is_integer(taddnode(right).right.resultdef) and
  156. ((localswitches*[cs_check_overflow,cs_check_range])=[]) and
  157. ((right.localswitches*[cs_check_overflow,cs_check_range])=[]) and
  158. valid_for_var(taddnode(right).right,false) and
  159. not(might_have_sideeffects(taddnode(right).right)) then
  160. begin
  161. case right.nodetype of
  162. addn:
  163. newinlinenodetype:=in_inc_x;
  164. andn:
  165. newinlinenodetype:=in_and_assign_x_y;
  166. orn:
  167. newinlinenodetype:=in_or_assign_x_y;
  168. xorn:
  169. newinlinenodetype:=in_xor_assign_x_y;
  170. else
  171. internalerror(2017032902);
  172. end;
  173. if right.nodetype=addn then
  174. result:=cinlinenode.createintern(
  175. newinlinenodetype,false,ccallparanode.create(
  176. taddnode(right).right,ccallparanode.create(taddnode(right).left,nil)))
  177. else
  178. result:=cinlinenode.createintern(
  179. newinlinenodetype,false,ccallparanode.create(
  180. taddnode(right).left,ccallparanode.create(taddnode(right).right,nil)));
  181. taddnode(right).right:=nil;
  182. taddnode(right).left:=nil;
  183. exit;
  184. end;
  185. { replace i:=not i by in_not_assign_x(i)
  186. i:=-i by in_neg_assign_x(i)
  187. this handles the case, where there are no implicit type conversions }
  188. if (right.nodetype in [notn,unaryminusn]) and
  189. (tunarynode(right).left.isequal(left)) and
  190. is_integer(tunarynode(right).left.resultdef) and
  191. ((localswitches*[cs_check_overflow,cs_check_range])=[]) and
  192. ((right.localswitches*[cs_check_overflow,cs_check_range])=[]) and
  193. valid_for_var(tunarynode(right).left,false) and
  194. not(might_have_sideeffects(tunarynode(right).left)) then
  195. begin
  196. if right.nodetype=notn then
  197. newinlinenodetype:=in_not_assign_x
  198. else
  199. newinlinenodetype:=in_neg_assign_x;
  200. result:=cinlinenode.createintern(
  201. newinlinenodetype,false,tunarynode(right).left);
  202. tunarynode(right).left:=nil;
  203. exit;
  204. end;
  205. { replace i:=not i by in_not_assign_x(i)
  206. i:=-i by in_neg_assign_x(i)
  207. this handles the case with type conversions:
  208. outer typeconv: right
  209. neg/not: ttypeconvnode(right).left
  210. inner typeconv: tunarynode(ttypeconvnode(right).left).left
  211. right side 'i': ttypeconvnode(tunarynode(ttypeconvnode(right).left).left).left }
  212. if (right.nodetype=typeconvn) and
  213. (ttypeconvnode(right).convtype=tc_int_2_int) and
  214. (ttypeconvnode(right).left.nodetype in [notn,unaryminusn]) and
  215. is_integer(ttypeconvnode(right).left.resultdef) and
  216. (right.resultdef.size<=ttypeconvnode(right).left.resultdef.size) and
  217. (tunarynode(ttypeconvnode(right).left).left.nodetype=typeconvn) and
  218. (ttypeconvnode(tunarynode(ttypeconvnode(right).left).left).convtype=tc_int_2_int) and
  219. are_equal_ints(right.resultdef,ttypeconvnode(tunarynode(ttypeconvnode(right).left).left).left.resultdef) and
  220. ttypeconvnode(tunarynode(ttypeconvnode(right).left).left).left.isequal(left) and
  221. is_integer(ttypeconvnode(tunarynode(ttypeconvnode(right).left).left).left.resultdef) and
  222. ((localswitches*[cs_check_overflow,cs_check_range])=[]) and
  223. ((right.localswitches*[cs_check_overflow,cs_check_range])=[]) and
  224. valid_for_var(ttypeconvnode(tunarynode(ttypeconvnode(right).left).left).left,false) and
  225. not(might_have_sideeffects(ttypeconvnode(tunarynode(ttypeconvnode(right).left).left).left)) then
  226. begin
  227. if ttypeconvnode(right).left.nodetype=notn then
  228. newinlinenodetype:=in_not_assign_x
  229. else
  230. newinlinenodetype:=in_neg_assign_x;
  231. result:=cinlinenode.createintern(
  232. newinlinenodetype,false,ttypeconvnode(tunarynode(ttypeconvnode(right).left).left).left);
  233. ttypeconvnode(tunarynode(ttypeconvnode(right).left).left).left:=nil;
  234. exit;
  235. end;
  236. end;
  237. end;
  238. function try_opt_node(var n: tnode; arg: pointer): foreachnoderesult;
  239. var
  240. hn : tnode;
  241. begin
  242. result:=fen_false;
  243. if n.nodetype=assignn then
  244. begin
  245. hn:=try_opt_assignmentnode(tassignmentnode(n));
  246. if assigned(hn) then
  247. begin
  248. n.free;
  249. n:=hn;
  250. typecheckpass(n);
  251. do_firstpass(n);
  252. end;
  253. end;
  254. end;
  255. procedure do_optloadmodifystore(var rootnode : tnode);
  256. begin
  257. foreachnodestatic(pm_postprocess,rootnode,@try_opt_node,nil);
  258. end;
  259. end.