optunrol.pas 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. {
  2. Loop unrolling
  3. Copyright (c) 2005 by Florian Klaempfl
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit optunrol;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node;
  22. function unroll_loop(node : tnode) : tnode;
  23. implementation
  24. uses
  25. globtype,globals,
  26. cpuinfo,
  27. nutils,
  28. nbas,nflw,ncon,ninl,ncal;
  29. var
  30. nodecount : aint;
  31. function donodecount(var n: tnode; arg: pointer): foreachnoderesult;
  32. begin
  33. inc(nodecount);
  34. result:=fen_false;
  35. end;
  36. { rough estimation how large the tree "node" is }
  37. function countnodes(node : tnode) : aint;
  38. begin
  39. nodecount:=0;
  40. foreachnodestatic(node,@donodecount,nil);
  41. result:=nodecount;
  42. end;
  43. function number_unrolls(node : tnode) : integer;
  44. begin
  45. {$ifdef i386}
  46. { multiply by 2 for CPUs with a long pipeline }
  47. if current_settings.cputype in [cpu_Pentium4] then
  48. number_unrolls:=60 div countnodes(node)
  49. else
  50. {$endif i386}
  51. number_unrolls:=30 div countnodes(node);
  52. if number_unrolls=0 then
  53. number_unrolls:=1;
  54. end;
  55. function unroll_loop(node : tnode) : tnode;
  56. var
  57. unrolls,i : integer;
  58. counts : qword;
  59. unrollstatement : tstatementnode;
  60. unrollblock : tblocknode;
  61. begin
  62. result:=nil;
  63. if (cs_opt_size in current_settings.optimizerswitches) then
  64. exit;
  65. if not(node.nodetype in [forn]) then
  66. exit;
  67. unrolls:=number_unrolls(tfornode(node).t2);
  68. if unrolls>1 then
  69. begin
  70. { number of executions known? }
  71. if (tfornode(node).right.nodetype=ordconstn) and (tfornode(node).t1.nodetype=ordconstn) then
  72. begin
  73. if lnf_backward in tfornode(node).loopflags then
  74. counts:=tordconstnode(tfornode(node).right).value-tordconstnode(tfornode(node).t1).value+1
  75. else
  76. counts:=tordconstnode(tfornode(node).t1).value-tordconstnode(tfornode(node).right).value+1;
  77. { don't unroll more than we need }
  78. if unrolls>counts then
  79. unrolls:=counts;
  80. { create block statement }
  81. unrollblock:=internalstatements(unrollstatement);
  82. { let's unroll (and rock of course) }
  83. for i:=1 to unrolls do
  84. begin
  85. { create and insert copy of the statement block }
  86. addstatement(unrollstatement,tfornode(tfornode(node).t2).getcopy);
  87. { set and insert entry label? }
  88. if (counts mod unrolls<>0) and
  89. ((counts mod unrolls)=unrolls-i) then
  90. begin
  91. tfornode(node).entrylabel:=clabelnode.create(cnothingnode.create);
  92. addstatement(unrollstatement,tfornode(node).entrylabel);
  93. end;
  94. { for itself increases at the last iteration }
  95. if i<unrolls then
  96. begin
  97. { insert incrementation of counter var }
  98. addstatement(unrollstatement,
  99. geninlinenode(in_inc_x,false,ccallparanode.create(tfornode(node).left.getcopy,nil)));
  100. end;
  101. end;
  102. { can we get rid of the for statement? }
  103. if unrolls=counts then
  104. result:=unrollblock;
  105. end
  106. else
  107. begin
  108. { unrolling is a little bit more tricky if we don't know the
  109. loop count at compile time, but the solution is to use a jump table
  110. which is indexed by "loop count mod unrolls" at run time and which
  111. jumps then at the appropriate place inside the loop. Because
  112. a module division is expensive, we can use only unroll counts dividable
  113. by 2 }
  114. case unrolls of
  115. 1..2:
  116. ;
  117. 3:
  118. unrolls:=2;
  119. 4..7:
  120. unrolls:=4;
  121. { unrolls>4 already make no sense imo, but who knows (FK) }
  122. 8..15:
  123. unrolls:=8;
  124. 16..31:
  125. unrolls:=16;
  126. 32..63:
  127. unrolls:=32;
  128. 64..$7fff:
  129. unrolls:=64;
  130. else
  131. exit;
  132. end;
  133. { we don't handle this yet }
  134. exit;
  135. end;
  136. if not(assigned(result)) then
  137. begin
  138. tfornode(node).t2.free;
  139. tfornode(node).t2:=unrollblock;
  140. end;
  141. end;
  142. end;
  143. end.