optloadmodifystore.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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,compinnr,
  27. defutil,defcmp,htypechk,pass_1,
  28. nadd,ncal,ncnv,ninl,nld;
  29. function try_opt_assignmentnode(assignmentnode: tassignmentnode): tnode;
  30. var
  31. newinlinenodetype: tinlinenumber;
  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. result.localswitches:=localswitches;
  53. tinlinenode(right).left:=nil;
  54. exit;
  55. end;
  56. { replace i:=i+k by inc(i,k)
  57. i:=i-k by dec(i,k)
  58. i:=i and/or/xor k by in_[and/or/xor]_assign_x_y(i,k)
  59. this handles the case, where there are no implicit type conversions }
  60. if (right.nodetype in [addn,subn,andn,orn,xorn]) and
  61. (taddnode(right).left.isequal(left)) and
  62. is_integer(taddnode(right).left.resultdef) and
  63. is_integer(taddnode(right).right.resultdef) and
  64. ((localswitches*[cs_check_overflow,cs_check_range])=[]) and
  65. ((right.localswitches*[cs_check_overflow,cs_check_range])=[]) and
  66. valid_for_var(taddnode(right).left,false) and
  67. not(might_have_sideeffects(taddnode(right).left)) then
  68. begin
  69. case right.nodetype of
  70. addn:
  71. newinlinenodetype:=in_inc_x;
  72. subn:
  73. newinlinenodetype:=in_dec_x;
  74. andn:
  75. newinlinenodetype:=in_and_assign_x_y;
  76. orn:
  77. newinlinenodetype:=in_or_assign_x_y;
  78. xorn:
  79. newinlinenodetype:=in_xor_assign_x_y;
  80. else
  81. internalerror(2017032901);
  82. end;
  83. if right.nodetype in [addn,subn] then
  84. result:=cinlinenode.createintern(
  85. newinlinenodetype,false,ccallparanode.create(
  86. taddnode(right).left,ccallparanode.create(taddnode(right).right,nil)))
  87. else
  88. result:=cinlinenode.createintern(
  89. newinlinenodetype,false,ccallparanode.create(
  90. taddnode(right).right,ccallparanode.create(taddnode(right).left,nil)));
  91. result.localswitches:=localswitches;
  92. taddnode(right).left:=nil;
  93. taddnode(right).right:=nil;
  94. exit;
  95. end;
  96. { replace i:=i+k by inc(i,k)
  97. i:=i-k by dec(i,k)
  98. i:=i and/or/xor k by in_[and/or/xor]_assign_x_y(i,k)
  99. this handles the case with two conversions (outer and inner):
  100. outer typeconv: right
  101. add/sub/and/or/xor: ttypeconvnode(right).left
  102. inner typeconv: taddnode(ttypeconvnode(right).left).left
  103. right side 'i': ttypeconvnode(taddnode(ttypeconvnode(right).left).left).left
  104. right side 'k': taddnode(ttypeconvnode(right).left).right }
  105. if (right.nodetype=typeconvn) and
  106. (ttypeconvnode(right).convtype=tc_int_2_int) and
  107. (ttypeconvnode(right).left.nodetype in [addn,subn,andn,orn,xorn]) and
  108. is_integer(ttypeconvnode(right).left.resultdef) and
  109. (right.resultdef.size<=ttypeconvnode(right).left.resultdef.size) and
  110. (taddnode(ttypeconvnode(right).left).left.nodetype=typeconvn) and
  111. (ttypeconvnode(taddnode(ttypeconvnode(right).left).left).convtype=tc_int_2_int) and
  112. are_equal_ints(right.resultdef,ttypeconvnode(taddnode(ttypeconvnode(right).left).left).left.resultdef) and
  113. ttypeconvnode(taddnode(ttypeconvnode(right).left).left).left.isequal(left) and
  114. is_integer(taddnode(ttypeconvnode(right).left).left.resultdef) and
  115. is_integer(taddnode(ttypeconvnode(right).left).right.resultdef) and
  116. is_integer(ttypeconvnode(taddnode(ttypeconvnode(right).left).left).left.resultdef) and
  117. ((localswitches*[cs_check_overflow,cs_check_range])=[]) and
  118. ((right.localswitches*[cs_check_overflow,cs_check_range])=[]) and
  119. valid_for_var(ttypeconvnode(taddnode(ttypeconvnode(right).left).left).left,false) and
  120. not(might_have_sideeffects(ttypeconvnode(taddnode(ttypeconvnode(right).left).left).left)) then
  121. begin
  122. case ttypeconvnode(right).left.nodetype of
  123. addn:
  124. newinlinenodetype:=in_inc_x;
  125. subn:
  126. newinlinenodetype:=in_dec_x;
  127. andn:
  128. newinlinenodetype:=in_and_assign_x_y;
  129. orn:
  130. newinlinenodetype:=in_or_assign_x_y;
  131. xorn:
  132. newinlinenodetype:=in_xor_assign_x_y;
  133. else
  134. internalerror(2017032901);
  135. end;
  136. inserttypeconv_internal(taddnode(ttypeconvnode(right).left).right,left.resultdef);
  137. if ttypeconvnode(right).left.nodetype in [addn,subn] then
  138. result:=cinlinenode.createintern(
  139. newinlinenodetype,false,ccallparanode.create(
  140. ttypeconvnode(taddnode(ttypeconvnode(right).left).left).left,ccallparanode.create(taddnode(ttypeconvnode(right).left).right,nil)))
  141. else
  142. result:=cinlinenode.createintern(
  143. newinlinenodetype,false,ccallparanode.create(
  144. taddnode(ttypeconvnode(right).left).right,ccallparanode.create(ttypeconvnode(taddnode(ttypeconvnode(right).left).left).left,nil)));
  145. result.localswitches:=localswitches;
  146. ttypeconvnode(taddnode(ttypeconvnode(right).left).left).left:=nil;
  147. taddnode(ttypeconvnode(right).left).right:=nil;
  148. exit;
  149. end;
  150. { replace i:=k+i by inc(i,k)
  151. i:=k and/or/xor i by in_[and/or/xor]_assign_x_y(i,k)
  152. this handles the case, where there are no implicit type conversions }
  153. if (right.nodetype in [addn,andn,orn,xorn]) and
  154. (taddnode(right).right.isequal(left)) and
  155. is_integer(taddnode(right).left.resultdef) and
  156. is_integer(taddnode(right).right.resultdef) and
  157. ((localswitches*[cs_check_overflow,cs_check_range])=[]) and
  158. ((right.localswitches*[cs_check_overflow,cs_check_range])=[]) and
  159. valid_for_var(taddnode(right).right,false) and
  160. not(might_have_sideeffects(taddnode(right).right)) then
  161. begin
  162. case right.nodetype of
  163. addn:
  164. newinlinenodetype:=in_inc_x;
  165. andn:
  166. newinlinenodetype:=in_and_assign_x_y;
  167. orn:
  168. newinlinenodetype:=in_or_assign_x_y;
  169. xorn:
  170. newinlinenodetype:=in_xor_assign_x_y;
  171. else
  172. internalerror(2017032902);
  173. end;
  174. if right.nodetype=addn then
  175. result:=cinlinenode.createintern(
  176. newinlinenodetype,false,ccallparanode.create(
  177. taddnode(right).right,ccallparanode.create(taddnode(right).left,nil)))
  178. else
  179. result:=cinlinenode.createintern(
  180. newinlinenodetype,false,ccallparanode.create(
  181. taddnode(right).left,ccallparanode.create(taddnode(right).right,nil)));
  182. result.localswitches:=localswitches;
  183. taddnode(right).right:=nil;
  184. taddnode(right).left:=nil;
  185. exit;
  186. end;
  187. { replace i:=k+i by inc(i,k)
  188. i:=k and/or/xor i by in_[and/or/xor]_assign_x_y(i,k)
  189. this handles the case with two conversions (outer and inner):
  190. outer typeconv: right
  191. add/and/or/xor: ttypeconvnode(right).left
  192. inner typeconv: taddnode(ttypeconvnode(right).left).right
  193. right side 'i': ttypeconvnode(taddnode(ttypeconvnode(right).left).right).left
  194. right side 'k': taddnode(ttypeconvnode(right).left).left }
  195. if (right.nodetype=typeconvn) and
  196. (ttypeconvnode(right).convtype=tc_int_2_int) and
  197. (ttypeconvnode(right).left.nodetype in [addn,andn,orn,xorn]) and
  198. is_integer(ttypeconvnode(right).left.resultdef) and
  199. (right.resultdef.size<=ttypeconvnode(right).left.resultdef.size) and
  200. (taddnode(ttypeconvnode(right).left).right.nodetype=typeconvn) and
  201. (ttypeconvnode(taddnode(ttypeconvnode(right).left).right).convtype=tc_int_2_int) and
  202. are_equal_ints(right.resultdef,ttypeconvnode(taddnode(ttypeconvnode(right).left).right).left.resultdef) and
  203. ttypeconvnode(taddnode(ttypeconvnode(right).left).right).left.isequal(left) and
  204. is_integer(taddnode(ttypeconvnode(right).left).left.resultdef) and
  205. is_integer(taddnode(ttypeconvnode(right).left).right.resultdef) and
  206. is_integer(ttypeconvnode(taddnode(ttypeconvnode(right).left).right).left.resultdef) and
  207. ((localswitches*[cs_check_overflow,cs_check_range])=[]) and
  208. ((right.localswitches*[cs_check_overflow,cs_check_range])=[]) and
  209. valid_for_var(ttypeconvnode(taddnode(ttypeconvnode(right).left).right).left,false) and
  210. not(might_have_sideeffects(ttypeconvnode(taddnode(ttypeconvnode(right).left).right).left)) then
  211. begin
  212. case ttypeconvnode(right).left.nodetype of
  213. addn:
  214. newinlinenodetype:=in_inc_x;
  215. andn:
  216. newinlinenodetype:=in_and_assign_x_y;
  217. orn:
  218. newinlinenodetype:=in_or_assign_x_y;
  219. xorn:
  220. newinlinenodetype:=in_xor_assign_x_y;
  221. else
  222. internalerror(2017051101);
  223. end;
  224. inserttypeconv_internal(taddnode(ttypeconvnode(right).left).left,left.resultdef);
  225. if ttypeconvnode(right).left.nodetype=addn then
  226. result:=cinlinenode.createintern(
  227. newinlinenodetype,false,ccallparanode.create(
  228. ttypeconvnode(taddnode(ttypeconvnode(right).left).right).left,ccallparanode.create(taddnode(ttypeconvnode(right).left).left,nil)))
  229. else
  230. result:=cinlinenode.createintern(
  231. newinlinenodetype,false,ccallparanode.create(
  232. taddnode(ttypeconvnode(right).left).left,ccallparanode.create(ttypeconvnode(taddnode(ttypeconvnode(right).left).right).left,nil)));
  233. result.localswitches:=localswitches;
  234. ttypeconvnode(taddnode(ttypeconvnode(right).left).right).left:=nil;
  235. taddnode(ttypeconvnode(right).left).left:=nil;
  236. exit;
  237. end;
  238. { replace i:=not i by in_not_assign_x(i)
  239. i:=-i by in_neg_assign_x(i)
  240. this handles the case, where there are no implicit type conversions }
  241. if (right.nodetype in [notn,unaryminusn]) and
  242. (tunarynode(right).left.isequal(left)) and
  243. is_integer(tunarynode(right).left.resultdef) and
  244. ((localswitches*[cs_check_overflow,cs_check_range])=[]) and
  245. ((right.localswitches*[cs_check_overflow,cs_check_range])=[]) and
  246. valid_for_var(tunarynode(right).left,false) and
  247. not(might_have_sideeffects(tunarynode(right).left)) then
  248. begin
  249. if right.nodetype=notn then
  250. newinlinenodetype:=in_not_assign_x
  251. else
  252. newinlinenodetype:=in_neg_assign_x;
  253. result:=cinlinenode.createintern(
  254. newinlinenodetype,false,tunarynode(right).left);
  255. result.localswitches:=localswitches;
  256. tunarynode(right).left:=nil;
  257. exit;
  258. end;
  259. { replace i:=not i by in_not_assign_x(i)
  260. i:=-i by in_neg_assign_x(i)
  261. this handles the case with type conversions:
  262. outer typeconv: right
  263. neg/not: ttypeconvnode(right).left
  264. inner typeconv: tunarynode(ttypeconvnode(right).left).left
  265. right side 'i': ttypeconvnode(tunarynode(ttypeconvnode(right).left).left).left }
  266. if (right.nodetype=typeconvn) and
  267. (ttypeconvnode(right).convtype=tc_int_2_int) and
  268. (ttypeconvnode(right).left.nodetype in [notn,unaryminusn]) and
  269. is_integer(ttypeconvnode(right).left.resultdef) and
  270. (right.resultdef.size<=ttypeconvnode(right).left.resultdef.size) and
  271. (tunarynode(ttypeconvnode(right).left).left.nodetype=typeconvn) and
  272. (ttypeconvnode(tunarynode(ttypeconvnode(right).left).left).convtype=tc_int_2_int) and
  273. are_equal_ints(right.resultdef,ttypeconvnode(tunarynode(ttypeconvnode(right).left).left).left.resultdef) and
  274. ttypeconvnode(tunarynode(ttypeconvnode(right).left).left).left.isequal(left) and
  275. is_integer(ttypeconvnode(tunarynode(ttypeconvnode(right).left).left).left.resultdef) and
  276. ((localswitches*[cs_check_overflow,cs_check_range])=[]) and
  277. ((right.localswitches*[cs_check_overflow,cs_check_range])=[]) and
  278. valid_for_var(ttypeconvnode(tunarynode(ttypeconvnode(right).left).left).left,false) and
  279. not(might_have_sideeffects(ttypeconvnode(tunarynode(ttypeconvnode(right).left).left).left)) then
  280. begin
  281. if ttypeconvnode(right).left.nodetype=notn then
  282. newinlinenodetype:=in_not_assign_x
  283. else
  284. newinlinenodetype:=in_neg_assign_x;
  285. result:=cinlinenode.createintern(
  286. newinlinenodetype,false,ttypeconvnode(tunarynode(ttypeconvnode(right).left).left).left);
  287. result.localswitches:=localswitches;
  288. ttypeconvnode(tunarynode(ttypeconvnode(right).left).left).left:=nil;
  289. exit;
  290. end;
  291. end;
  292. end;
  293. function try_opt_node(var n: tnode; arg: pointer): foreachnoderesult;
  294. var
  295. hn : tnode;
  296. begin
  297. result:=fen_false;
  298. if n.nodetype=assignn then
  299. begin
  300. hn:=try_opt_assignmentnode(tassignmentnode(n));
  301. if assigned(hn) then
  302. begin
  303. n.free;
  304. n:=hn;
  305. typecheckpass(n);
  306. do_firstpass(n);
  307. end;
  308. end;
  309. end;
  310. procedure do_optloadmodifystore(var rootnode : tnode);
  311. begin
  312. foreachnodestatic(pm_postprocess,rootnode,@try_opt_node,nil);
  313. end;
  314. end.