optunrol.pas 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 aktcputype 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. entrylabel : tlabelnode;
  62. begin
  63. result:=nil;
  64. if (cs_opt_size in aktoptimizerswitches) then
  65. exit;
  66. if not(node.nodetype in [forn]) then
  67. exit;
  68. unrolls:=number_unrolls(tfornode(node).t2);
  69. if unrolls>1 then
  70. begin
  71. { number of executions known? }
  72. if (tfornode(node).right.nodetype=ordconstn) and (tfornode(node).t1.nodetype=ordconstn) then
  73. begin
  74. if lnf_backward in tfornode(node).loopflags then
  75. counts:=tordconstnode(tfornode(node).right).value-tordconstnode(tfornode(node).t1).value+1
  76. else
  77. counts:=tordconstnode(tfornode(node).t1).value-tordconstnode(tfornode(node).right).value+1;
  78. { don't unroll more than we need }
  79. if unrolls>counts then
  80. unrolls:=counts;
  81. { create block statement }
  82. unrollblock:=internalstatements(unrollstatement);
  83. { let's unroll (and rock of course) }
  84. for i:=1 to unrolls do
  85. begin
  86. { create and insert copy of the statement block }
  87. addstatement(unrollstatement,tfornode(tfornode(node).t2).getcopy);
  88. { set and insert entry label? }
  89. if (counts mod unrolls<>0) and
  90. ((counts mod unrolls)=unrolls-i) then
  91. begin
  92. tfornode(node).entrylabel:=clabelnode.create(cnothingnode.create);
  93. addstatement(unrollstatement,tfornode(node).entrylabel);
  94. end;
  95. { for itself increases at the last iteration }
  96. if i<unrolls then
  97. begin
  98. { insert incrementation of counter var }
  99. addstatement(unrollstatement,
  100. geninlinenode(in_inc_x,false,ccallparanode.create(tfornode(node).left.getcopy,nil)));
  101. end;
  102. end;
  103. { can we get rid of the for statement? }
  104. if unrolls=counts then
  105. result:=unrollblock;
  106. end
  107. else
  108. begin
  109. { unrolling is a little bit more tricky if we don't know the
  110. loop count at compile time, but the solution is to use a jump table
  111. which is indexed by "loop count mod unrolls" at run time and which
  112. jumps then at the appropriate place inside the loop. Because
  113. a module division is expensive, we can use only unroll counts dividable
  114. by 2 }
  115. case unrolls of
  116. 1..2:
  117. ;
  118. 3:
  119. unrolls:=2;
  120. 4..7:
  121. unrolls:=4;
  122. { unrolls>4 already make no sense imo, but who knows (FK) }
  123. 8..15:
  124. unrolls:=8;
  125. 16..31:
  126. unrolls:=16;
  127. 32..63:
  128. unrolls:=32;
  129. 64..$7fff:
  130. unrolls:=64;
  131. else
  132. exit;
  133. end;
  134. { we don't handle this yet }
  135. exit;
  136. end;
  137. if not(assigned(result)) then
  138. begin
  139. tfornode(node).t2.free;
  140. tfornode(node).t2:=unrollblock;
  141. end;
  142. end;
  143. end;
  144. end.