1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
|
{
Loop optimization
Copyright (c) 2005 by Florian Klaempfl
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
****************************************************************************
}
unit optloop;
{$i fpcdefs.inc}
{ $define DEBUG_OPTSTRENGTH}
interface
uses
node;
function unroll_loop(node : tnode) : tnode;
function OptimizeInductionVariables(node : tnode) : boolean;
implementation
uses
cutils,cclasses,
globtype,globals,constexp,
verbose,
symdef,symsym,
defutil,
cpuinfo,
nutils,
nadd,nbas,nflw,ncon,ninl,ncal,nld,nmem,ncnv,
ncgmem,
pass_1,
optbase,optutils,
procinfo;
function number_unrolls(node : tnode) : cardinal;
begin
{$ifdef i386}
{ multiply by 2 for CPUs with a long pipeline }
if current_settings.optimizecputype in [cpu_Pentium4] then
number_unrolls:=60 div node_count(node)
else
{$endif i386}
number_unrolls:=30 div node_count(node);
if number_unrolls=0 then
number_unrolls:=1;
end;
type
treplaceinfo = record
node : tnode;
value : Tconstexprint;
end;
preplaceinfo = ^treplaceinfo;
function checkbreakcontinue(var n:tnode; arg: pointer): foreachnoderesult;
begin
if n.nodetype in [breakn,continuen] then
result:=fen_norecurse_true
else
result:=fen_false;
end;
function replaceloadnodes(var n: tnode; arg: pointer): foreachnoderesult;
begin
if ((n.nodetype=loadn) and (preplaceinfo(arg)^.node.nodetype=loadn) and
(tloadnode(n).symtableentry=tloadnode(preplaceinfo(arg)^.node).symtableentry)) or
((n.nodetype=temprefn) and (preplaceinfo(arg)^.node.nodetype=temprefn) and
(ttemprefnode(n).tempinfo=ttemprefnode(preplaceinfo(arg)^.node).tempinfo)) then
begin
if n.flags*[nf_modify,nf_write,nf_address_taken]<>[] then
internalerror(2012090402);
n.free;
n:=cordconstnode.create(preplaceinfo(arg)^.value,preplaceinfo(arg)^.node.resultdef,false);
end;
result:=fen_false;
end;
function unroll_loop(node : tnode) : tnode;
var
unrolls,i : cardinal;
counts : qword;
unrollstatement,newforstatement : tstatementnode;
unrollblock : tblocknode;
getridoffor : boolean;
replaceinfo : treplaceinfo;
usesbreakcontinue : boolean;
begin
result:=nil;
if (cs_opt_size in current_settings.optimizerswitches) then
exit;
if not(node.nodetype in [forn]) then
exit;
unrolls:=number_unrolls(tfornode(node).t2);
if unrolls>1 then
begin
{ number of executions known? }
if (tfornode(node).right.nodetype=ordconstn) and (tfornode(node).t1.nodetype=ordconstn) then
begin
if lnf_backward in tfornode(node).loopflags then
counts:=tordconstnode(tfornode(node).right).value-tordconstnode(tfornode(node).t1).value+1
else
counts:=tordconstnode(tfornode(node).t1).value-tordconstnode(tfornode(node).right).value+1;
usesbreakcontinue:=foreachnodestatic(tfornode(node).t2,@checkbreakcontinue,nil);
{ don't unroll more than we need,
multiply unroll by two here because we can get rid
of the counter variable completely and replace it by a constant
if unrolls=counts }
if unrolls*2>counts then
unrolls:=counts;
{ create block statement }
unrollblock:=internalstatements(unrollstatement);
{ can we get rid completly of the for ? }
getridoffor:=(unrolls=counts) and not(usesbreakcontinue);
if getridoffor then
begin
if not(tfornode(node).left.nodetype in [temprefn,loadn]) then
internalerror(2012090301);
replaceinfo.node:=tfornode(node).left;
replaceinfo.value:=tordconstnode(tfornode(node).right).value;
end;
{ let's unroll (and rock of course) }
for i:=1 to unrolls do
begin
{ create and insert copy of the statement block }
addstatement(unrollstatement,tfornode(node).t2.getcopy);
{ set and insert entry label? }
if (counts mod unrolls<>0) and
((counts mod unrolls)=unrolls-i) then
begin
tfornode(node).entrylabel:=clabelnode.create(cnothingnode.create,tlabelsym.create('$optunrol'));
addstatement(unrollstatement,tfornode(node).entrylabel);
end;
if getridoffor then
begin
foreachnodestatic(tnode(unrollstatement),@replaceloadnodes,@replaceinfo);
if lnf_backward in tfornode(node).loopflags then
replaceinfo.value:=replaceinfo.value-1
else
replaceinfo.value:=replaceinfo.value+1;
end
else
begin
{ for itself increases at the last iteration }
if i<unrolls then
begin
{ insert incr/decrementation of counter var }
if lnf_backward in tfornode(node).loopflags then
addstatement(unrollstatement,
geninlinenode(in_dec_x,false,ccallparanode.create(tfornode(node).left.getcopy,nil)))
else
addstatement(unrollstatement,
geninlinenode(in_inc_x,false,ccallparanode.create(tfornode(node).left.getcopy,nil)));
end;
end;
end;
{ can we get rid of the for statement? }
if getridoffor then
begin
{ create block statement }
result:=internalstatements(newforstatement);
addstatement(newforstatement,unrollblock);
end;
end
else
begin
{ unrolling is a little bit more tricky if we don't know the
loop count at compile time, but the solution is to use a jump table
which is indexed by "loop count mod unrolls" at run time and which
jumps then at the appropriate place inside the loop. Because
a module division is expensive, we can use only unroll counts dividable
by 2 }
case unrolls of
1..2:
;
3:
unrolls:=2;
4..7:
unrolls:=4;
{ unrolls>4 already make no sense imo, but who knows (FK) }
8..15:
unrolls:=8;
16..31:
unrolls:=16;
32..63:
unrolls:=32;
64..$7fff:
unrolls:=64;
else
exit;
end;
{ we don't handle this yet }
exit;
end;
if not(assigned(result)) then
begin
tfornode(node).t2.free;
tfornode(node).t2:=unrollblock;
end;
end;
end;
var
initcode,
calccode,
deletecode : tblocknode;
initcodestatements,
calccodestatements,
deletecodestatements: tstatementnode;
templist : tfplist;
inductionexprs : tfplist;
changedforloop,
containsnestedforloop : boolean;
function is_loop_invariant(loop : tnode;expr : tnode) : boolean;
begin
result:=is_constnode(expr);
case expr.nodetype of
loadn:
begin
if (pi_dfaavailable in current_procinfo.flags) and
assigned(loop.optinfo) and
assigned(expr.optinfo) then
{ no aliasing? }
result:=not(tabstractvarsym(tloadnode(expr).symtableentry).addr_taken) and
{ no definition in the loop? }
not(DFASetIn(loop.optinfo^.defsum,expr.optinfo^.index));
end;
vecn:
begin
result:=((tvecnode(expr).left.nodetype=loadn) or is_loop_invariant(loop,tvecnode(expr).left)) and
is_loop_invariant(loop,tvecnode(expr).right);
end;
typeconvn:
result:=is_loop_invariant(loop,ttypeconvnode(expr).left);
end;
end;
{ checks if the strength of n can be recuded, arg is the tforloop being considered }
function dostrengthreductiontest(var n: tnode; arg: pointer): foreachnoderesult;
function findpreviousstrengthreduction : boolean;
var
i : longint;
begin
result:=false;
for i:=0 to inductionexprs.count-1 do
begin
{ do we already maintain one expression? }
if tnode(inductionexprs[i]).isequal(n) then
begin
n.free;
case n.nodetype of
muln:
n:=ctemprefnode.create(ttempcreatenode(templist[i]));
vecn:
n:=ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(
ttempcreatenode(templist[i]))),n.resultdef);
else
internalerror(200809211);
end;
result:=true;
exit;
end;
end;
end;
procedure CreateNodes;
begin
if not assigned(initcode) then
begin
initcode:=internalstatements(initcodestatements);
calccode:=internalstatements(calccodestatements);
deletecode:=internalstatements(deletecodestatements);
end;
end;
var
tempnode : ttempcreatenode;
dummy : longint;
begin
result:=fen_false;
case n.nodetype of
forn:
{ inform for loop search routine, that it needs to search more deeply }
containsnestedforloop:=true;
muln:
begin
if (taddnode(n).right.nodetype=loadn) and
taddnode(n).right.isequal(tfornode(arg).left) and
{ plain read of the loop variable? }
not(nf_write in taddnode(n).right.flags) and
not(nf_modify in taddnode(n).right.flags) and
is_loop_invariant(tfornode(arg),taddnode(n).left) and
{ for now, we can handle only constant lower borders }
is_constnode(tfornode(arg).right) then
taddnode(n).swapleftright;
if (taddnode(n).left.nodetype=loadn) and
taddnode(n).left.isequal(tfornode(arg).left) and
{ plain read of the loop variable? }
not(nf_write in taddnode(n).left.flags) and
not(nf_modify in taddnode(n).left.flags) and
is_loop_invariant(tfornode(arg),taddnode(n).right) and
{ for now, we can handle only constant lower borders }
is_constnode(tfornode(arg).right) then
begin
changedforloop:=true;
{ did we use the same expression before already? }
if not(findpreviousstrengthreduction) then
begin
tempnode:=ctempcreatenode.create(n.resultdef,n.resultdef.size,tt_persistent,
tstoreddef(n.resultdef).is_intregable or tstoreddef(n.resultdef).is_fpuregable);
templist.Add(tempnode);
inductionexprs.Add(n);
CreateNodes;
if lnf_backward in tfornode(arg).loopflags then
addstatement(calccodestatements,
geninlinenode(in_dec_x,false,
ccallparanode.create(ctemprefnode.create(tempnode),ccallparanode.create(taddnode(n).right.getcopy,nil))))
else
addstatement(calccodestatements,
geninlinenode(in_inc_x,false,
ccallparanode.create(ctemprefnode.create(tempnode),ccallparanode.create(taddnode(n).right.getcopy,nil))));
addstatement(initcodestatements,tempnode);
addstatement(initcodestatements,cassignmentnode.create(ctemprefnode.create(tempnode),
caddnode.create(muln,
caddnode.create(subn,tfornode(arg).right.getcopy,cordconstnode.create(1,tfornode(arg).right.resultdef,false)),
taddnode(n).right.getcopy)
)
);
{ finally replace the node by a temp. ref }
n:=ctemprefnode.create(tempnode);
{ ... and add a temp. release node }
addstatement(deletecodestatements,ctempdeletenode.create(tempnode));
end;
{ set types }
do_firstpass(n);
result:=fen_norecurse_false;
end;
end;
vecn:
begin
{ is the index the counter variable? }
if not(is_special_array(tvecnode(n).left.resultdef)) and
(tvecnode(n).right.isequal(tfornode(arg).left) or
{ fpc usually creates a type cast to access an array }
((tvecnode(n).right.nodetype=typeconvn) and
ttypeconvnode(tvecnode(n).right).left.isequal(tfornode(arg).left)
)
) and
{ plain read of the loop variable? }
not(nf_write in tvecnode(n).right.flags) and
not(nf_modify in tvecnode(n).right.flags) and
{ direct array access? }
((tvecnode(n).left.nodetype=loadn) or
{ ... or loop invariant expression? }
is_loop_invariant(tfornode(arg),tvecnode(n).right)) and
{ removing the multiplication is only worth the
effort if it's not a simple shift }
not(ispowerof2(tcgvecnode(n).get_mul_size,dummy)) then
begin
changedforloop:=true;
{ did we use the same expression before already? }
if not(findpreviousstrengthreduction) then
begin
{$ifdef DEBUG_OPTSTRENGTH}
writeln('**********************************************************************************');
writeln('Found expression for strength reduction: ');
printnode(n);
writeln('**********************************************************************************');
{$endif DEBUG_OPTSTRENGTH}
tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
templist.Add(tempnode);
inductionexprs.Add(n);
CreateNodes;
if lnf_backward in tfornode(arg).loopflags then
addstatement(calccodestatements,
geninlinenode(in_dec_x,false,
ccallparanode.create(ctemprefnode.create(tempnode),ccallparanode.create(
cordconstnode.create(tcgvecnode(n).get_mul_size,tfornode(arg).right.resultdef,false),nil))))
else
addstatement(calccodestatements,
geninlinenode(in_inc_x,false,
ccallparanode.create(ctemprefnode.create(tempnode),ccallparanode.create(
cordconstnode.create(tcgvecnode(n).get_mul_size,tfornode(arg).right.resultdef,false),nil))));
addstatement(initcodestatements,tempnode);
addstatement(initcodestatements,cassignmentnode.create(ctemprefnode.create(tempnode),
caddrnode.create(
cvecnode.create(tvecnode(n).left.getcopy,tfornode(arg).right.getcopy)
)
));
{ finally replace the node by a temp. ref }
n:=ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),n.resultdef);
{ ... and add a temp. release node }
addstatement(deletecodestatements,ctempdeletenode.create(tempnode));
end;
{ set types }
do_firstpass(n);
result:=fen_norecurse_false;
end;
end;
end;
end;
function OptimizeInductionVariablesSingleForLoop(node : tnode) : tnode;
var
loopcode : tblocknode;
loopcodestatements,
newcodestatements : tstatementnode;
fornode : tfornode;
begin
result:=nil;
if node.nodetype<>forn then
exit;
templist:=TFPList.Create;
inductionexprs:=TFPList.Create;
initcode:=nil;
calccode:=nil;
deletecode:=nil;
initcodestatements:=nil;
calccodestatements:=nil;
deletecodestatements:=nil;
{ find all expressions being candidates for strength reduction
and replace them }
foreachnodestatic(pm_postprocess,node,@dostrengthreductiontest,node);
{ clue everything together }
if assigned(initcode) then
begin
do_firstpass(tnode(initcode));
do_firstpass(tnode(calccode));
do_firstpass(tnode(deletecode));
{ create a new for node, the old one will be released by the compiler }
with tfornode(node) do
begin
fornode:=cfornode.create(left,right,t1,t2,lnf_backward in loopflags);
left:=nil;
right:=nil;
t1:=nil;
t2:=nil;
end;
node:=fornode;
loopcode:=internalstatements(loopcodestatements);
addstatement(loopcodestatements,tfornode(node).t2);
tfornode(node).t2:=loopcode;
do_firstpass(node);
addstatement(loopcodestatements,calccode);
result:=internalstatements(newcodestatements);
addstatement(newcodestatements,initcode);
addstatement(newcodestatements,node);
addstatement(newcodestatements,deletecode);
end;
templist.Free;
inductionexprs.Free;
end;
function iterforloops(var n: tnode; arg: pointer): foreachnoderesult;
var
hp : tnode;
begin
Result:=fen_false;
if n.nodetype=forn then
begin
{ do we have DFA available? }
if pi_dfaavailable in current_procinfo.flags then
begin
CalcDefSum(tfornode(n).t2);
end;
containsnestedforloop:=false;
hp:=OptimizeInductionVariablesSingleForLoop(n);
if assigned(hp) then
begin
n.Free;
n:=hp;
end;
{ can we avoid further searching? }
if not(containsnestedforloop) then
Result:=fen_norecurse_false;
end;
end;
function OptimizeInductionVariables(node : tnode) : boolean;
begin
changedforloop:=false;
foreachnodestatic(pm_postprocess,node,@iterforloops,nil);
Result:=changedforloop;
end;
end.
|