Mercurial > hg > CbC > CbC_gcc
annotate gcc/tree-predcom.c @ 128:fe568345ddd5
fix CbC-example
author | mir3636 |
---|---|
date | Wed, 11 Apr 2018 19:32:28 +0900 |
parents | 04ced10e8804 |
children | 84e7813d76e9 |
rev | line source |
---|---|
0 | 1 /* Predictive commoning. |
111 | 2 Copyright (C) 2005-2017 Free Software Foundation, Inc. |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
3 |
0 | 4 This file is part of GCC. |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
5 |
0 | 6 GCC is free software; you can redistribute it and/or modify it |
7 under the terms of the GNU General Public License as published by the | |
8 Free Software Foundation; either version 3, or (at your option) any | |
9 later version. | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
10 |
0 | 11 GCC is distributed in the hope that it will be useful, but WITHOUT |
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 for more details. | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
15 |
0 | 16 You should have received a copy of the GNU General Public License |
17 along with GCC; see the file COPYING3. If not see | |
18 <http://www.gnu.org/licenses/>. */ | |
19 | |
20 /* This file implements the predictive commoning optimization. Predictive | |
21 commoning can be viewed as CSE around a loop, and with some improvements, | |
22 as generalized strength reduction-- i.e., reusing values computed in | |
23 earlier iterations of a loop in the later ones. So far, the pass only | |
24 handles the most useful case, that is, reusing values of memory references. | |
25 If you think this is all just a special case of PRE, you are sort of right; | |
26 however, concentrating on loops is simpler, and makes it possible to | |
27 incorporate data dependence analysis to detect the opportunities, perform | |
28 loop unrolling to avoid copies together with renaming immediately, | |
29 and if needed, we could also take register pressure into account. | |
30 | |
31 Let us demonstrate what is done on an example: | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
32 |
0 | 33 for (i = 0; i < 100; i++) |
34 { | |
35 a[i+2] = a[i] + a[i+1]; | |
36 b[10] = b[10] + i; | |
37 c[i] = c[99 - i]; | |
38 d[i] = d[i + 1]; | |
39 } | |
40 | |
41 1) We find data references in the loop, and split them to mutually | |
42 independent groups (i.e., we find components of a data dependence | |
43 graph). We ignore read-read dependences whose distance is not constant. | |
44 (TODO -- we could also ignore antidependences). In this example, we | |
45 find the following groups: | |
46 | |
47 a[i]{read}, a[i+1]{read}, a[i+2]{write} | |
48 b[10]{read}, b[10]{write} | |
49 c[99 - i]{read}, c[i]{write} | |
50 d[i + 1]{read}, d[i]{write} | |
51 | |
52 2) Inside each of the group, we verify several conditions: | |
53 a) all the references must differ in indices only, and the indices | |
54 must all have the same step | |
55 b) the references must dominate loop latch (and thus, they must be | |
56 ordered by dominance relation). | |
57 c) the distance of the indices must be a small multiple of the step | |
58 We are then able to compute the difference of the references (# of | |
59 iterations before they point to the same place as the first of them). | |
60 Also, in case there are writes in the loop, we split the groups into | |
61 chains whose head is the write whose values are used by the reads in | |
62 the same chain. The chains are then processed independently, | |
63 making the further transformations simpler. Also, the shorter chains | |
64 need the same number of registers, but may require lower unrolling | |
65 factor in order to get rid of the copies on the loop latch. | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
66 |
0 | 67 In our example, we get the following chains (the chain for c is invalid). |
68 | |
69 a[i]{read,+0}, a[i+1]{read,-1}, a[i+2]{write,-2} | |
70 b[10]{read,+0}, b[10]{write,+0} | |
71 d[i + 1]{read,+0}, d[i]{write,+1} | |
72 | |
73 3) For each read, we determine the read or write whose value it reuses, | |
74 together with the distance of this reuse. I.e. we take the last | |
75 reference before it with distance 0, or the last of the references | |
76 with the smallest positive distance to the read. Then, we remove | |
77 the references that are not used in any of these chains, discard the | |
78 empty groups, and propagate all the links so that they point to the | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
79 single root reference of the chain (adjusting their distance |
0 | 80 appropriately). Some extra care needs to be taken for references with |
81 step 0. In our example (the numbers indicate the distance of the | |
82 reuse), | |
83 | |
84 a[i] --> (*) 2, a[i+1] --> (*) 1, a[i+2] (*) | |
85 b[10] --> (*) 1, b[10] (*) | |
86 | |
87 4) The chains are combined together if possible. If the corresponding | |
88 elements of two chains are always combined together with the same | |
89 operator, we remember just the result of this combination, instead | |
90 of remembering the values separately. We may need to perform | |
91 reassociation to enable combining, for example | |
92 | |
93 e[i] + f[i+1] + e[i+1] + f[i] | |
94 | |
95 can be reassociated as | |
96 | |
97 (e[i] + f[i]) + (e[i+1] + f[i+1]) | |
98 | |
99 and we can combine the chains for e and f into one chain. | |
100 | |
101 5) For each root reference (end of the chain) R, let N be maximum distance | |
111 | 102 of a reference reusing its value. Variables R0 up to RN are created, |
0 | 103 together with phi nodes that transfer values from R1 .. RN to |
104 R0 .. R(N-1). | |
105 Initial values are loaded to R0..R(N-1) (in case not all references | |
106 must necessarily be accessed and they may trap, we may fail here; | |
107 TODO sometimes, the loads could be guarded by a check for the number | |
108 of iterations). Values loaded/stored in roots are also copied to | |
109 RN. Other reads are replaced with the appropriate variable Ri. | |
110 Everything is put to SSA form. | |
111 | |
112 As a small improvement, if R0 is dead after the root (i.e., all uses of | |
113 the value with the maximum distance dominate the root), we can avoid | |
114 creating RN and use R0 instead of it. | |
115 | |
116 In our example, we get (only the parts concerning a and b are shown): | |
117 for (i = 0; i < 100; i++) | |
118 { | |
119 f = phi (a[0], s); | |
120 s = phi (a[1], f); | |
121 x = phi (b[10], x); | |
122 | |
123 f = f + s; | |
124 a[i+2] = f; | |
125 x = x + i; | |
126 b[10] = x; | |
127 } | |
128 | |
129 6) Factor F for unrolling is determined as the smallest common multiple of | |
130 (N + 1) for each root reference (N for references for that we avoided | |
131 creating RN). If F and the loop is small enough, loop is unrolled F | |
132 times. The stores to RN (R0) in the copies of the loop body are | |
133 periodically replaced with R0, R1, ... (R1, R2, ...), so that they can | |
134 be coalesced and the copies can be eliminated. | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
135 |
0 | 136 TODO -- copy propagation and other optimizations may change the live |
137 ranges of the temporary registers and prevent them from being coalesced; | |
138 this may increase the register pressure. | |
139 | |
140 In our case, F = 2 and the (main loop of the) result is | |
141 | |
142 for (i = 0; i < ...; i += 2) | |
143 { | |
144 f = phi (a[0], f); | |
145 s = phi (a[1], s); | |
146 x = phi (b[10], x); | |
147 | |
148 f = f + s; | |
149 a[i+2] = f; | |
150 x = x + i; | |
151 b[10] = x; | |
152 | |
153 s = s + f; | |
154 a[i+3] = s; | |
155 x = x + i; | |
156 b[10] = x; | |
157 } | |
158 | |
111 | 159 Apart from predictive commoning on Load-Load and Store-Load chains, we |
160 also support Store-Store chains -- stores killed by other store can be | |
161 eliminated. Given below example: | |
162 | |
163 for (i = 0; i < n; i++) | |
164 { | |
165 a[i] = 1; | |
166 a[i+2] = 2; | |
167 } | |
168 | |
169 It can be replaced with: | |
170 | |
171 t0 = a[0]; | |
172 t1 = a[1]; | |
173 for (i = 0; i < n; i++) | |
174 { | |
175 a[i] = 1; | |
176 t2 = 2; | |
177 t0 = t1; | |
178 t1 = t2; | |
179 } | |
180 a[n] = t0; | |
181 a[n+1] = t1; | |
182 | |
183 If the loop runs more than 1 iterations, it can be further simplified into: | |
184 | |
185 for (i = 0; i < n; i++) | |
186 { | |
187 a[i] = 1; | |
188 } | |
189 a[n] = 2; | |
190 a[n+1] = 2; | |
191 | |
192 The interesting part is this can be viewed either as general store motion | |
193 or general dead store elimination in either intra/inter-iterations way. | |
194 | |
195 TODO: For now, we don't support store-store chains in multi-exit loops. We | |
196 force to not unroll in case of store-store chain even if other chains might | |
197 ask for unroll. | |
0 | 198 |
199 Predictive commoning can be generalized for arbitrary computations (not | |
200 just memory loads), and also nontrivial transfer functions (e.g., replacing | |
201 i * i with ii_last + 2 * i + 1), to generalize strength reduction. */ | |
202 | |
203 #include "config.h" | |
204 #include "system.h" | |
205 #include "coretypes.h" | |
111 | 206 #include "backend.h" |
207 #include "rtl.h" | |
0 | 208 #include "tree.h" |
111 | 209 #include "gimple.h" |
210 #include "predict.h" | |
211 #include "tree-pass.h" | |
212 #include "ssa.h" | |
213 #include "gimple-pretty-print.h" | |
214 #include "alias.h" | |
215 #include "fold-const.h" | |
0 | 216 #include "cfgloop.h" |
111 | 217 #include "tree-eh.h" |
218 #include "gimplify.h" | |
219 #include "gimple-iterator.h" | |
220 #include "gimplify-me.h" | |
221 #include "tree-ssa-loop-ivopts.h" | |
222 #include "tree-ssa-loop-manip.h" | |
223 #include "tree-ssa-loop-niter.h" | |
224 #include "tree-ssa-loop.h" | |
225 #include "tree-into-ssa.h" | |
226 #include "tree-dfa.h" | |
227 #include "tree-ssa.h" | |
0 | 228 #include "tree-data-ref.h" |
229 #include "tree-scalar-evolution.h" | |
230 #include "params.h" | |
231 #include "tree-affine.h" | |
111 | 232 #include "builtins.h" |
0 | 233 |
234 /* The maximum number of iterations between the considered memory | |
235 references. */ | |
236 | |
237 #define MAX_DISTANCE (target_avail_regs < 16 ? 4 : 8) | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
238 |
0 | 239 /* Data references (or phi nodes that carry data reference values across |
240 loop iterations). */ | |
241 | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
242 typedef struct dref_d |
0 | 243 { |
244 /* The reference itself. */ | |
245 struct data_reference *ref; | |
246 | |
247 /* The statement in that the reference appears. */ | |
111 | 248 gimple *stmt; |
0 | 249 |
250 /* In case that STMT is a phi node, this field is set to the SSA name | |
251 defined by it in replace_phis_by_defined_names (in order to avoid | |
252 pointing to phi node that got reallocated in the meantime). */ | |
253 tree name_defined_by_phi; | |
254 | |
255 /* Distance of the reference from the root of the chain (in number of | |
256 iterations of the loop). */ | |
257 unsigned distance; | |
258 | |
259 /* Number of iterations offset from the first reference in the component. */ | |
111 | 260 widest_int offset; |
0 | 261 |
262 /* Number of the reference in a component, in dominance ordering. */ | |
263 unsigned pos; | |
264 | |
265 /* True if the memory reference is always accessed when the loop is | |
266 entered. */ | |
267 unsigned always_accessed : 1; | |
268 } *dref; | |
269 | |
270 | |
271 /* Type of the chain of the references. */ | |
272 | |
273 enum chain_type | |
274 { | |
275 /* The addresses of the references in the chain are constant. */ | |
276 CT_INVARIANT, | |
277 | |
278 /* There are only loads in the chain. */ | |
279 CT_LOAD, | |
280 | |
281 /* Root of the chain is store, the rest are loads. */ | |
282 CT_STORE_LOAD, | |
283 | |
111 | 284 /* There are only stores in the chain. */ |
285 CT_STORE_STORE, | |
286 | |
0 | 287 /* A combination of two chains. */ |
288 CT_COMBINATION | |
289 }; | |
290 | |
291 /* Chains of data references. */ | |
292 | |
293 typedef struct chain | |
294 { | |
295 /* Type of the chain. */ | |
296 enum chain_type type; | |
297 | |
298 /* For combination chains, the operator and the two chains that are | |
299 combined, and the type of the result. */ | |
300 enum tree_code op; | |
301 tree rslt_type; | |
302 struct chain *ch1, *ch2; | |
303 | |
304 /* The references in the chain. */ | |
111 | 305 vec<dref> refs; |
0 | 306 |
307 /* The maximum distance of the reference in the chain from the root. */ | |
308 unsigned length; | |
309 | |
310 /* The variables used to copy the value throughout iterations. */ | |
111 | 311 vec<tree> vars; |
0 | 312 |
313 /* Initializers for the variables. */ | |
111 | 314 vec<tree> inits; |
315 | |
316 /* Finalizers for the eliminated stores. */ | |
317 vec<tree> finis; | |
318 | |
319 /* gimple stmts intializing the initial variables of the chain. */ | |
320 gimple_seq init_seq; | |
321 | |
322 /* gimple stmts finalizing the eliminated stores of the chain. */ | |
323 gimple_seq fini_seq; | |
0 | 324 |
325 /* True if there is a use of a variable with the maximal distance | |
326 that comes after the root in the loop. */ | |
327 unsigned has_max_use_after : 1; | |
328 | |
329 /* True if all the memory references in the chain are always accessed. */ | |
330 unsigned all_always_accessed : 1; | |
331 | |
332 /* True if this chain was combined together with some other chain. */ | |
333 unsigned combined : 1; | |
111 | 334 |
335 /* True if this is store elimination chain and eliminated stores store | |
336 loop invariant value into memory. */ | |
337 unsigned inv_store_elimination : 1; | |
0 | 338 } *chain_p; |
339 | |
340 | |
341 /* Describes the knowledge about the step of the memory references in | |
342 the component. */ | |
343 | |
344 enum ref_step_type | |
345 { | |
346 /* The step is zero. */ | |
347 RS_INVARIANT, | |
348 | |
349 /* The step is nonzero. */ | |
350 RS_NONZERO, | |
351 | |
352 /* The step may or may not be nonzero. */ | |
353 RS_ANY | |
354 }; | |
355 | |
356 /* Components of the data dependence graph. */ | |
357 | |
358 struct component | |
359 { | |
360 /* The references in the component. */ | |
111 | 361 vec<dref> refs; |
0 | 362 |
363 /* What we know about the step of the references in the component. */ | |
364 enum ref_step_type comp_step; | |
365 | |
111 | 366 /* True if all references in component are stores and we try to do |
367 intra/inter loop iteration dead store elimination. */ | |
368 bool eliminate_store_p; | |
369 | |
0 | 370 /* Next component in the list. */ |
371 struct component *next; | |
372 }; | |
373 | |
374 /* Bitmap of ssa names defined by looparound phi nodes covered by chains. */ | |
375 | |
376 static bitmap looparound_phis; | |
377 | |
378 /* Cache used by tree_to_aff_combination_expand. */ | |
379 | |
111 | 380 static hash_map<tree, name_expansion *> *name_expansions; |
0 | 381 |
382 /* Dumps data reference REF to FILE. */ | |
383 | |
384 extern void dump_dref (FILE *, dref); | |
385 void | |
386 dump_dref (FILE *file, dref ref) | |
387 { | |
388 if (ref->ref) | |
389 { | |
390 fprintf (file, " "); | |
391 print_generic_expr (file, DR_REF (ref->ref), TDF_SLIM); | |
392 fprintf (file, " (id %u%s)\n", ref->pos, | |
393 DR_IS_READ (ref->ref) ? "" : ", write"); | |
394 | |
395 fprintf (file, " offset "); | |
111 | 396 print_decs (ref->offset, file); |
0 | 397 fprintf (file, "\n"); |
398 | |
399 fprintf (file, " distance %u\n", ref->distance); | |
400 } | |
401 else | |
402 { | |
403 if (gimple_code (ref->stmt) == GIMPLE_PHI) | |
404 fprintf (file, " looparound ref\n"); | |
405 else | |
406 fprintf (file, " combination ref\n"); | |
407 fprintf (file, " in statement "); | |
408 print_gimple_stmt (file, ref->stmt, 0, TDF_SLIM); | |
409 fprintf (file, "\n"); | |
410 fprintf (file, " distance %u\n", ref->distance); | |
411 } | |
412 | |
413 } | |
414 | |
415 /* Dumps CHAIN to FILE. */ | |
416 | |
417 extern void dump_chain (FILE *, chain_p); | |
418 void | |
419 dump_chain (FILE *file, chain_p chain) | |
420 { | |
421 dref a; | |
422 const char *chain_type; | |
423 unsigned i; | |
424 tree var; | |
425 | |
426 switch (chain->type) | |
427 { | |
428 case CT_INVARIANT: | |
429 chain_type = "Load motion"; | |
430 break; | |
431 | |
432 case CT_LOAD: | |
433 chain_type = "Loads-only"; | |
434 break; | |
435 | |
436 case CT_STORE_LOAD: | |
437 chain_type = "Store-loads"; | |
438 break; | |
439 | |
111 | 440 case CT_STORE_STORE: |
441 chain_type = "Store-stores"; | |
442 break; | |
443 | |
0 | 444 case CT_COMBINATION: |
445 chain_type = "Combination"; | |
446 break; | |
447 | |
448 default: | |
449 gcc_unreachable (); | |
450 } | |
451 | |
452 fprintf (file, "%s chain %p%s\n", chain_type, (void *) chain, | |
453 chain->combined ? " (combined)" : ""); | |
454 if (chain->type != CT_INVARIANT) | |
455 fprintf (file, " max distance %u%s\n", chain->length, | |
456 chain->has_max_use_after ? "" : ", may reuse first"); | |
457 | |
458 if (chain->type == CT_COMBINATION) | |
459 { | |
460 fprintf (file, " equal to %p %s %p in type ", | |
461 (void *) chain->ch1, op_symbol_code (chain->op), | |
462 (void *) chain->ch2); | |
463 print_generic_expr (file, chain->rslt_type, TDF_SLIM); | |
464 fprintf (file, "\n"); | |
465 } | |
466 | |
111 | 467 if (chain->vars.exists ()) |
0 | 468 { |
469 fprintf (file, " vars"); | |
111 | 470 FOR_EACH_VEC_ELT (chain->vars, i, var) |
0 | 471 { |
472 fprintf (file, " "); | |
473 print_generic_expr (file, var, TDF_SLIM); | |
474 } | |
475 fprintf (file, "\n"); | |
476 } | |
477 | |
111 | 478 if (chain->inits.exists ()) |
0 | 479 { |
480 fprintf (file, " inits"); | |
111 | 481 FOR_EACH_VEC_ELT (chain->inits, i, var) |
0 | 482 { |
483 fprintf (file, " "); | |
484 print_generic_expr (file, var, TDF_SLIM); | |
485 } | |
486 fprintf (file, "\n"); | |
487 } | |
488 | |
489 fprintf (file, " references:\n"); | |
111 | 490 FOR_EACH_VEC_ELT (chain->refs, i, a) |
0 | 491 dump_dref (file, a); |
492 | |
493 fprintf (file, "\n"); | |
494 } | |
495 | |
496 /* Dumps CHAINS to FILE. */ | |
497 | |
111 | 498 extern void dump_chains (FILE *, vec<chain_p> ); |
0 | 499 void |
111 | 500 dump_chains (FILE *file, vec<chain_p> chains) |
0 | 501 { |
502 chain_p chain; | |
503 unsigned i; | |
504 | |
111 | 505 FOR_EACH_VEC_ELT (chains, i, chain) |
0 | 506 dump_chain (file, chain); |
507 } | |
508 | |
509 /* Dumps COMP to FILE. */ | |
510 | |
511 extern void dump_component (FILE *, struct component *); | |
512 void | |
513 dump_component (FILE *file, struct component *comp) | |
514 { | |
515 dref a; | |
516 unsigned i; | |
517 | |
518 fprintf (file, "Component%s:\n", | |
519 comp->comp_step == RS_INVARIANT ? " (invariant)" : ""); | |
111 | 520 FOR_EACH_VEC_ELT (comp->refs, i, a) |
0 | 521 dump_dref (file, a); |
522 fprintf (file, "\n"); | |
523 } | |
524 | |
525 /* Dumps COMPS to FILE. */ | |
526 | |
527 extern void dump_components (FILE *, struct component *); | |
528 void | |
529 dump_components (FILE *file, struct component *comps) | |
530 { | |
531 struct component *comp; | |
532 | |
533 for (comp = comps; comp; comp = comp->next) | |
534 dump_component (file, comp); | |
535 } | |
536 | |
537 /* Frees a chain CHAIN. */ | |
538 | |
539 static void | |
540 release_chain (chain_p chain) | |
541 { | |
542 dref ref; | |
543 unsigned i; | |
544 | |
545 if (chain == NULL) | |
546 return; | |
547 | |
111 | 548 FOR_EACH_VEC_ELT (chain->refs, i, ref) |
0 | 549 free (ref); |
550 | |
111 | 551 chain->refs.release (); |
552 chain->vars.release (); | |
553 chain->inits.release (); | |
554 if (chain->init_seq) | |
555 gimple_seq_discard (chain->init_seq); | |
556 | |
557 chain->finis.release (); | |
558 if (chain->fini_seq) | |
559 gimple_seq_discard (chain->fini_seq); | |
0 | 560 |
561 free (chain); | |
562 } | |
563 | |
564 /* Frees CHAINS. */ | |
565 | |
566 static void | |
111 | 567 release_chains (vec<chain_p> chains) |
0 | 568 { |
569 unsigned i; | |
570 chain_p chain; | |
571 | |
111 | 572 FOR_EACH_VEC_ELT (chains, i, chain) |
0 | 573 release_chain (chain); |
111 | 574 chains.release (); |
0 | 575 } |
576 | |
577 /* Frees a component COMP. */ | |
578 | |
579 static void | |
580 release_component (struct component *comp) | |
581 { | |
111 | 582 comp->refs.release (); |
0 | 583 free (comp); |
584 } | |
585 | |
586 /* Frees list of components COMPS. */ | |
587 | |
588 static void | |
589 release_components (struct component *comps) | |
590 { | |
591 struct component *act, *next; | |
592 | |
593 for (act = comps; act; act = next) | |
594 { | |
595 next = act->next; | |
596 release_component (act); | |
597 } | |
598 } | |
599 | |
600 /* Finds a root of tree given by FATHERS containing A, and performs path | |
601 shortening. */ | |
602 | |
603 static unsigned | |
604 component_of (unsigned fathers[], unsigned a) | |
605 { | |
606 unsigned root, n; | |
607 | |
608 for (root = a; root != fathers[root]; root = fathers[root]) | |
609 continue; | |
610 | |
611 for (; a != root; a = n) | |
612 { | |
613 n = fathers[a]; | |
614 fathers[a] = root; | |
615 } | |
616 | |
617 return root; | |
618 } | |
619 | |
620 /* Join operation for DFU. FATHERS gives the tree, SIZES are sizes of the | |
621 components, A and B are components to merge. */ | |
622 | |
623 static void | |
624 merge_comps (unsigned fathers[], unsigned sizes[], unsigned a, unsigned b) | |
625 { | |
626 unsigned ca = component_of (fathers, a); | |
627 unsigned cb = component_of (fathers, b); | |
628 | |
629 if (ca == cb) | |
630 return; | |
631 | |
632 if (sizes[ca] < sizes[cb]) | |
633 { | |
634 sizes[cb] += sizes[ca]; | |
635 fathers[ca] = cb; | |
636 } | |
637 else | |
638 { | |
639 sizes[ca] += sizes[cb]; | |
640 fathers[cb] = ca; | |
641 } | |
642 } | |
643 | |
644 /* Returns true if A is a reference that is suitable for predictive commoning | |
645 in the innermost loop that contains it. REF_STEP is set according to the | |
646 step of the reference A. */ | |
647 | |
648 static bool | |
649 suitable_reference_p (struct data_reference *a, enum ref_step_type *ref_step) | |
650 { | |
651 tree ref = DR_REF (a), step = DR_STEP (a); | |
652 | |
653 if (!step | |
111 | 654 || TREE_THIS_VOLATILE (ref) |
0 | 655 || !is_gimple_reg_type (TREE_TYPE (ref)) |
656 || tree_could_throw_p (ref)) | |
657 return false; | |
658 | |
659 if (integer_zerop (step)) | |
660 *ref_step = RS_INVARIANT; | |
661 else if (integer_nonzerop (step)) | |
662 *ref_step = RS_NONZERO; | |
663 else | |
664 *ref_step = RS_ANY; | |
665 | |
666 return true; | |
667 } | |
668 | |
669 /* Stores DR_OFFSET (DR) + DR_INIT (DR) to OFFSET. */ | |
670 | |
671 static void | |
672 aff_combination_dr_offset (struct data_reference *dr, aff_tree *offset) | |
673 { | |
111 | 674 tree type = TREE_TYPE (DR_OFFSET (dr)); |
0 | 675 aff_tree delta; |
676 | |
111 | 677 tree_to_aff_combination_expand (DR_OFFSET (dr), type, offset, |
0 | 678 &name_expansions); |
111 | 679 aff_combination_const (&delta, type, wi::to_widest (DR_INIT (dr))); |
0 | 680 aff_combination_add (offset, &delta); |
681 } | |
682 | |
683 /* Determines number of iterations of the innermost enclosing loop before B | |
684 refers to exactly the same location as A and stores it to OFF. If A and | |
685 B do not have the same step, they never meet, or anything else fails, | |
686 returns false, otherwise returns true. Both A and B are assumed to | |
687 satisfy suitable_reference_p. */ | |
688 | |
689 static bool | |
690 determine_offset (struct data_reference *a, struct data_reference *b, | |
111 | 691 widest_int *off) |
0 | 692 { |
693 aff_tree diff, baseb, step; | |
694 tree typea, typeb; | |
695 | |
696 /* Check that both the references access the location in the same type. */ | |
697 typea = TREE_TYPE (DR_REF (a)); | |
698 typeb = TREE_TYPE (DR_REF (b)); | |
699 if (!useless_type_conversion_p (typeb, typea)) | |
700 return false; | |
701 | |
702 /* Check whether the base address and the step of both references is the | |
703 same. */ | |
704 if (!operand_equal_p (DR_STEP (a), DR_STEP (b), 0) | |
705 || !operand_equal_p (DR_BASE_ADDRESS (a), DR_BASE_ADDRESS (b), 0)) | |
706 return false; | |
707 | |
708 if (integer_zerop (DR_STEP (a))) | |
709 { | |
710 /* If the references have loop invariant address, check that they access | |
711 exactly the same location. */ | |
111 | 712 *off = 0; |
0 | 713 return (operand_equal_p (DR_OFFSET (a), DR_OFFSET (b), 0) |
714 && operand_equal_p (DR_INIT (a), DR_INIT (b), 0)); | |
715 } | |
716 | |
717 /* Compare the offsets of the addresses, and check whether the difference | |
718 is a multiple of step. */ | |
719 aff_combination_dr_offset (a, &diff); | |
720 aff_combination_dr_offset (b, &baseb); | |
111 | 721 aff_combination_scale (&baseb, -1); |
0 | 722 aff_combination_add (&diff, &baseb); |
723 | |
111 | 724 tree_to_aff_combination_expand (DR_STEP (a), TREE_TYPE (DR_STEP (a)), |
0 | 725 &step, &name_expansions); |
726 return aff_combination_constant_multiple_p (&diff, &step, off); | |
727 } | |
728 | |
729 /* Returns the last basic block in LOOP for that we are sure that | |
730 it is executed whenever the loop is entered. */ | |
731 | |
732 static basic_block | |
733 last_always_executed_block (struct loop *loop) | |
734 { | |
735 unsigned i; | |
111 | 736 vec<edge> exits = get_loop_exit_edges (loop); |
0 | 737 edge ex; |
738 basic_block last = loop->latch; | |
739 | |
111 | 740 FOR_EACH_VEC_ELT (exits, i, ex) |
0 | 741 last = nearest_common_dominator (CDI_DOMINATORS, last, ex->src); |
111 | 742 exits.release (); |
0 | 743 |
744 return last; | |
745 } | |
746 | |
747 /* Splits dependence graph on DATAREFS described by DEPENDS to components. */ | |
748 | |
749 static struct component * | |
750 split_data_refs_to_components (struct loop *loop, | |
111 | 751 vec<data_reference_p> datarefs, |
752 vec<ddr_p> depends) | |
0 | 753 { |
111 | 754 unsigned i, n = datarefs.length (); |
0 | 755 unsigned ca, ia, ib, bad; |
756 unsigned *comp_father = XNEWVEC (unsigned, n + 1); | |
757 unsigned *comp_size = XNEWVEC (unsigned, n + 1); | |
758 struct component **comps; | |
759 struct data_reference *dr, *dra, *drb; | |
760 struct data_dependence_relation *ddr; | |
761 struct component *comp_list = NULL, *comp; | |
762 dref dataref; | |
111 | 763 /* Don't do store elimination if loop has multiple exit edges. */ |
764 bool eliminate_store_p = single_exit (loop) != NULL; | |
0 | 765 basic_block last_always_executed = last_always_executed_block (loop); |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
766 |
111 | 767 FOR_EACH_VEC_ELT (datarefs, i, dr) |
0 | 768 { |
769 if (!DR_REF (dr)) | |
770 { | |
771 /* A fake reference for call or asm_expr that may clobber memory; | |
772 just fail. */ | |
773 goto end; | |
774 } | |
111 | 775 /* predcom pass isn't prepared to handle calls with data references. */ |
776 if (is_gimple_call (DR_STMT (dr))) | |
777 goto end; | |
0 | 778 dr->aux = (void *) (size_t) i; |
779 comp_father[i] = i; | |
780 comp_size[i] = 1; | |
781 } | |
782 | |
783 /* A component reserved for the "bad" data references. */ | |
784 comp_father[n] = n; | |
785 comp_size[n] = 1; | |
786 | |
111 | 787 FOR_EACH_VEC_ELT (datarefs, i, dr) |
0 | 788 { |
789 enum ref_step_type dummy; | |
790 | |
791 if (!suitable_reference_p (dr, &dummy)) | |
792 { | |
793 ia = (unsigned) (size_t) dr->aux; | |
794 merge_comps (comp_father, comp_size, n, ia); | |
795 } | |
796 } | |
797 | |
111 | 798 FOR_EACH_VEC_ELT (depends, i, ddr) |
0 | 799 { |
111 | 800 widest_int dummy_off; |
0 | 801 |
802 if (DDR_ARE_DEPENDENT (ddr) == chrec_known) | |
803 continue; | |
804 | |
805 dra = DDR_A (ddr); | |
806 drb = DDR_B (ddr); | |
111 | 807 |
808 /* Don't do store elimination if there is any unknown dependence for | |
809 any store data reference. */ | |
810 if ((DR_IS_WRITE (dra) || DR_IS_WRITE (drb)) | |
811 && (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know | |
812 || DDR_NUM_DIST_VECTS (ddr) == 0)) | |
813 eliminate_store_p = false; | |
814 | |
0 | 815 ia = component_of (comp_father, (unsigned) (size_t) dra->aux); |
816 ib = component_of (comp_father, (unsigned) (size_t) drb->aux); | |
817 if (ia == ib) | |
818 continue; | |
819 | |
820 bad = component_of (comp_father, n); | |
821 | |
822 /* If both A and B are reads, we may ignore unsuitable dependences. */ | |
111 | 823 if (DR_IS_READ (dra) && DR_IS_READ (drb)) |
824 { | |
825 if (ia == bad || ib == bad | |
826 || !determine_offset (dra, drb, &dummy_off)) | |
827 continue; | |
828 } | |
829 /* If A is read and B write or vice versa and there is unsuitable | |
830 dependence, instead of merging both components into a component | |
831 that will certainly not pass suitable_component_p, just put the | |
832 read into bad component, perhaps at least the write together with | |
833 all the other data refs in it's component will be optimizable. */ | |
834 else if (DR_IS_READ (dra) && ib != bad) | |
835 { | |
836 if (ia == bad) | |
837 continue; | |
838 else if (!determine_offset (dra, drb, &dummy_off)) | |
839 { | |
840 merge_comps (comp_father, comp_size, bad, ia); | |
841 continue; | |
842 } | |
843 } | |
844 else if (DR_IS_READ (drb) && ia != bad) | |
845 { | |
846 if (ib == bad) | |
847 continue; | |
848 else if (!determine_offset (dra, drb, &dummy_off)) | |
849 { | |
850 merge_comps (comp_father, comp_size, bad, ib); | |
851 continue; | |
852 } | |
853 } | |
854 else if (DR_IS_WRITE (dra) && DR_IS_WRITE (drb) | |
855 && ia != bad && ib != bad | |
856 && !determine_offset (dra, drb, &dummy_off)) | |
857 { | |
858 merge_comps (comp_father, comp_size, bad, ia); | |
859 merge_comps (comp_father, comp_size, bad, ib); | |
860 continue; | |
861 } | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
862 |
0 | 863 merge_comps (comp_father, comp_size, ia, ib); |
864 } | |
865 | |
111 | 866 if (eliminate_store_p) |
867 { | |
868 tree niters = number_of_latch_executions (loop); | |
869 | |
870 /* Don't do store elimination if niters info is unknown because stores | |
871 in the last iteration can't be eliminated and we need to recover it | |
872 after loop. */ | |
873 eliminate_store_p = (niters != NULL_TREE && niters != chrec_dont_know); | |
874 } | |
875 | |
0 | 876 comps = XCNEWVEC (struct component *, n); |
877 bad = component_of (comp_father, n); | |
111 | 878 FOR_EACH_VEC_ELT (datarefs, i, dr) |
0 | 879 { |
880 ia = (unsigned) (size_t) dr->aux; | |
881 ca = component_of (comp_father, ia); | |
882 if (ca == bad) | |
883 continue; | |
884 | |
885 comp = comps[ca]; | |
886 if (!comp) | |
887 { | |
888 comp = XCNEW (struct component); | |
111 | 889 comp->refs.create (comp_size[ca]); |
890 comp->eliminate_store_p = eliminate_store_p; | |
0 | 891 comps[ca] = comp; |
892 } | |
893 | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
894 dataref = XCNEW (struct dref_d); |
0 | 895 dataref->ref = dr; |
896 dataref->stmt = DR_STMT (dr); | |
111 | 897 dataref->offset = 0; |
0 | 898 dataref->distance = 0; |
899 | |
900 dataref->always_accessed | |
901 = dominated_by_p (CDI_DOMINATORS, last_always_executed, | |
902 gimple_bb (dataref->stmt)); | |
111 | 903 dataref->pos = comp->refs.length (); |
904 comp->refs.quick_push (dataref); | |
905 if (DR_IS_READ (dr)) | |
906 comp->eliminate_store_p = false; | |
0 | 907 } |
908 | |
909 for (i = 0; i < n; i++) | |
910 { | |
911 comp = comps[i]; | |
912 if (comp) | |
913 { | |
914 comp->next = comp_list; | |
915 comp_list = comp; | |
916 } | |
917 } | |
918 free (comps); | |
919 | |
920 end: | |
921 free (comp_father); | |
922 free (comp_size); | |
923 return comp_list; | |
924 } | |
925 | |
926 /* Returns true if the component COMP satisfies the conditions | |
927 described in 2) at the beginning of this file. LOOP is the current | |
928 loop. */ | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
929 |
0 | 930 static bool |
931 suitable_component_p (struct loop *loop, struct component *comp) | |
932 { | |
933 unsigned i; | |
934 dref a, first; | |
935 basic_block ba, bp = loop->header; | |
936 bool ok, has_write = false; | |
937 | |
111 | 938 FOR_EACH_VEC_ELT (comp->refs, i, a) |
0 | 939 { |
940 ba = gimple_bb (a->stmt); | |
941 | |
942 if (!just_once_each_iteration_p (loop, ba)) | |
943 return false; | |
944 | |
945 gcc_assert (dominated_by_p (CDI_DOMINATORS, ba, bp)); | |
946 bp = ba; | |
947 | |
67
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
948 if (DR_IS_WRITE (a->ref)) |
0 | 949 has_write = true; |
950 } | |
951 | |
111 | 952 first = comp->refs[0]; |
0 | 953 ok = suitable_reference_p (first->ref, &comp->comp_step); |
954 gcc_assert (ok); | |
111 | 955 first->offset = 0; |
956 | |
957 for (i = 1; comp->refs.iterate (i, &a); i++) | |
0 | 958 { |
959 if (!determine_offset (first->ref, a->ref, &a->offset)) | |
960 return false; | |
961 | |
111 | 962 enum ref_step_type a_step; |
963 gcc_checking_assert (suitable_reference_p (a->ref, &a_step) | |
964 && a_step == comp->comp_step); | |
0 | 965 } |
966 | |
967 /* If there is a write inside the component, we must know whether the | |
968 step is nonzero or not -- we would not otherwise be able to recognize | |
969 whether the value accessed by reads comes from the OFFSET-th iteration | |
970 or the previous one. */ | |
971 if (has_write && comp->comp_step == RS_ANY) | |
972 return false; | |
973 | |
974 return true; | |
975 } | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
976 |
0 | 977 /* Check the conditions on references inside each of components COMPS, |
978 and remove the unsuitable components from the list. The new list | |
979 of components is returned. The conditions are described in 2) at | |
980 the beginning of this file. LOOP is the current loop. */ | |
981 | |
982 static struct component * | |
983 filter_suitable_components (struct loop *loop, struct component *comps) | |
984 { | |
985 struct component **comp, *act; | |
986 | |
987 for (comp = &comps; *comp; ) | |
988 { | |
989 act = *comp; | |
990 if (suitable_component_p (loop, act)) | |
991 comp = &act->next; | |
992 else | |
993 { | |
994 dref ref; | |
995 unsigned i; | |
996 | |
997 *comp = act->next; | |
111 | 998 FOR_EACH_VEC_ELT (act->refs, i, ref) |
0 | 999 free (ref); |
1000 release_component (act); | |
1001 } | |
1002 } | |
1003 | |
1004 return comps; | |
1005 } | |
1006 | |
1007 /* Compares two drefs A and B by their offset and position. Callback for | |
1008 qsort. */ | |
1009 | |
1010 static int | |
1011 order_drefs (const void *a, const void *b) | |
1012 { | |
1013 const dref *const da = (const dref *) a; | |
1014 const dref *const db = (const dref *) b; | |
111 | 1015 int offcmp = wi::cmps ((*da)->offset, (*db)->offset); |
0 | 1016 |
1017 if (offcmp != 0) | |
1018 return offcmp; | |
1019 | |
1020 return (*da)->pos - (*db)->pos; | |
1021 } | |
1022 | |
1023 /* Returns root of the CHAIN. */ | |
1024 | |
1025 static inline dref | |
1026 get_chain_root (chain_p chain) | |
1027 { | |
111 | 1028 return chain->refs[0]; |
1029 } | |
1030 | |
1031 /* Given CHAIN, returns the last ref at DISTANCE, or NULL if it doesn't | |
1032 exist. */ | |
1033 | |
1034 static inline dref | |
1035 get_chain_last_ref_at (chain_p chain, unsigned distance) | |
1036 { | |
1037 unsigned i; | |
1038 | |
1039 for (i = chain->refs.length (); i > 0; i--) | |
1040 if (distance == chain->refs[i - 1]->distance) | |
1041 break; | |
1042 | |
1043 return (i > 0) ? chain->refs[i - 1] : NULL; | |
0 | 1044 } |
1045 | |
1046 /* Adds REF to the chain CHAIN. */ | |
1047 | |
1048 static void | |
1049 add_ref_to_chain (chain_p chain, dref ref) | |
1050 { | |
1051 dref root = get_chain_root (chain); | |
111 | 1052 |
1053 gcc_assert (wi::les_p (root->offset, ref->offset)); | |
1054 widest_int dist = ref->offset - root->offset; | |
1055 if (wi::leu_p (MAX_DISTANCE, dist)) | |
0 | 1056 { |
1057 free (ref); | |
1058 return; | |
1059 } | |
111 | 1060 gcc_assert (wi::fits_uhwi_p (dist)); |
1061 | |
1062 chain->refs.safe_push (ref); | |
1063 | |
1064 ref->distance = dist.to_uhwi (); | |
0 | 1065 |
1066 if (ref->distance >= chain->length) | |
1067 { | |
1068 chain->length = ref->distance; | |
1069 chain->has_max_use_after = false; | |
1070 } | |
1071 | |
111 | 1072 /* Don't set the flag for store-store chain since there is no use. */ |
1073 if (chain->type != CT_STORE_STORE | |
1074 && ref->distance == chain->length | |
0 | 1075 && ref->pos > root->pos) |
1076 chain->has_max_use_after = true; | |
1077 | |
1078 chain->all_always_accessed &= ref->always_accessed; | |
1079 } | |
1080 | |
1081 /* Returns the chain for invariant component COMP. */ | |
1082 | |
1083 static chain_p | |
1084 make_invariant_chain (struct component *comp) | |
1085 { | |
1086 chain_p chain = XCNEW (struct chain); | |
1087 unsigned i; | |
1088 dref ref; | |
1089 | |
1090 chain->type = CT_INVARIANT; | |
1091 | |
1092 chain->all_always_accessed = true; | |
1093 | |
111 | 1094 FOR_EACH_VEC_ELT (comp->refs, i, ref) |
0 | 1095 { |
111 | 1096 chain->refs.safe_push (ref); |
0 | 1097 chain->all_always_accessed &= ref->always_accessed; |
1098 } | |
1099 | |
111 | 1100 chain->inits = vNULL; |
1101 chain->finis = vNULL; | |
1102 | |
0 | 1103 return chain; |
1104 } | |
1105 | |
111 | 1106 /* Make a new chain of type TYPE rooted at REF. */ |
0 | 1107 |
1108 static chain_p | |
111 | 1109 make_rooted_chain (dref ref, enum chain_type type) |
0 | 1110 { |
1111 chain_p chain = XCNEW (struct chain); | |
1112 | |
111 | 1113 chain->type = type; |
1114 chain->refs.safe_push (ref); | |
0 | 1115 chain->all_always_accessed = ref->always_accessed; |
1116 ref->distance = 0; | |
1117 | |
111 | 1118 chain->inits = vNULL; |
1119 chain->finis = vNULL; | |
1120 | |
0 | 1121 return chain; |
1122 } | |
1123 | |
1124 /* Returns true if CHAIN is not trivial. */ | |
1125 | |
1126 static bool | |
1127 nontrivial_chain_p (chain_p chain) | |
1128 { | |
111 | 1129 return chain != NULL && chain->refs.length () > 1; |
0 | 1130 } |
1131 | |
1132 /* Returns the ssa name that contains the value of REF, or NULL_TREE if there | |
1133 is no such name. */ | |
1134 | |
1135 static tree | |
1136 name_for_ref (dref ref) | |
1137 { | |
1138 tree name; | |
1139 | |
1140 if (is_gimple_assign (ref->stmt)) | |
1141 { | |
1142 if (!ref->ref || DR_IS_READ (ref->ref)) | |
1143 name = gimple_assign_lhs (ref->stmt); | |
1144 else | |
1145 name = gimple_assign_rhs1 (ref->stmt); | |
1146 } | |
1147 else | |
1148 name = PHI_RESULT (ref->stmt); | |
1149 | |
1150 return (TREE_CODE (name) == SSA_NAME ? name : NULL_TREE); | |
1151 } | |
1152 | |
1153 /* Returns true if REF is a valid initializer for ROOT with given DISTANCE (in | |
1154 iterations of the innermost enclosing loop). */ | |
1155 | |
1156 static bool | |
1157 valid_initializer_p (struct data_reference *ref, | |
1158 unsigned distance, struct data_reference *root) | |
1159 { | |
1160 aff_tree diff, base, step; | |
111 | 1161 widest_int off; |
0 | 1162 |
1163 /* Both REF and ROOT must be accessing the same object. */ | |
1164 if (!operand_equal_p (DR_BASE_ADDRESS (ref), DR_BASE_ADDRESS (root), 0)) | |
1165 return false; | |
1166 | |
1167 /* The initializer is defined outside of loop, hence its address must be | |
1168 invariant inside the loop. */ | |
1169 gcc_assert (integer_zerop (DR_STEP (ref))); | |
1170 | |
1171 /* If the address of the reference is invariant, initializer must access | |
1172 exactly the same location. */ | |
1173 if (integer_zerop (DR_STEP (root))) | |
1174 return (operand_equal_p (DR_OFFSET (ref), DR_OFFSET (root), 0) | |
1175 && operand_equal_p (DR_INIT (ref), DR_INIT (root), 0)); | |
1176 | |
1177 /* Verify that this index of REF is equal to the root's index at | |
1178 -DISTANCE-th iteration. */ | |
1179 aff_combination_dr_offset (root, &diff); | |
1180 aff_combination_dr_offset (ref, &base); | |
111 | 1181 aff_combination_scale (&base, -1); |
0 | 1182 aff_combination_add (&diff, &base); |
1183 | |
111 | 1184 tree_to_aff_combination_expand (DR_STEP (root), TREE_TYPE (DR_STEP (root)), |
1185 &step, &name_expansions); | |
0 | 1186 if (!aff_combination_constant_multiple_p (&diff, &step, &off)) |
1187 return false; | |
1188 | |
111 | 1189 if (off != distance) |
0 | 1190 return false; |
1191 | |
1192 return true; | |
1193 } | |
1194 | |
1195 /* Finds looparound phi node of LOOP that copies the value of REF, and if its | |
1196 initial value is correct (equal to initial value of REF shifted by one | |
1197 iteration), returns the phi node. Otherwise, NULL_TREE is returned. ROOT | |
1198 is the root of the current chain. */ | |
1199 | |
111 | 1200 static gphi * |
0 | 1201 find_looparound_phi (struct loop *loop, dref ref, dref root) |
1202 { | |
1203 tree name, init, init_ref; | |
111 | 1204 gphi *phi = NULL; |
1205 gimple *init_stmt; | |
0 | 1206 edge latch = loop_latch_edge (loop); |
1207 struct data_reference init_dr; | |
111 | 1208 gphi_iterator psi; |
0 | 1209 |
1210 if (is_gimple_assign (ref->stmt)) | |
1211 { | |
1212 if (DR_IS_READ (ref->ref)) | |
1213 name = gimple_assign_lhs (ref->stmt); | |
1214 else | |
1215 name = gimple_assign_rhs1 (ref->stmt); | |
1216 } | |
1217 else | |
1218 name = PHI_RESULT (ref->stmt); | |
1219 if (!name) | |
1220 return NULL; | |
1221 | |
1222 for (psi = gsi_start_phis (loop->header); !gsi_end_p (psi); gsi_next (&psi)) | |
1223 { | |
111 | 1224 phi = psi.phi (); |
0 | 1225 if (PHI_ARG_DEF_FROM_EDGE (phi, latch) == name) |
1226 break; | |
1227 } | |
1228 | |
1229 if (gsi_end_p (psi)) | |
1230 return NULL; | |
1231 | |
1232 init = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop)); | |
1233 if (TREE_CODE (init) != SSA_NAME) | |
1234 return NULL; | |
1235 init_stmt = SSA_NAME_DEF_STMT (init); | |
1236 if (gimple_code (init_stmt) != GIMPLE_ASSIGN) | |
1237 return NULL; | |
1238 gcc_assert (gimple_assign_lhs (init_stmt) == init); | |
1239 | |
1240 init_ref = gimple_assign_rhs1 (init_stmt); | |
1241 if (!REFERENCE_CLASS_P (init_ref) | |
1242 && !DECL_P (init_ref)) | |
1243 return NULL; | |
1244 | |
1245 /* Analyze the behavior of INIT_REF with respect to LOOP (innermost | |
1246 loop enclosing PHI). */ | |
1247 memset (&init_dr, 0, sizeof (struct data_reference)); | |
1248 DR_REF (&init_dr) = init_ref; | |
1249 DR_STMT (&init_dr) = phi; | |
111 | 1250 if (!dr_analyze_innermost (&DR_INNERMOST (&init_dr), init_ref, loop)) |
0 | 1251 return NULL; |
1252 | |
1253 if (!valid_initializer_p (&init_dr, ref->distance + 1, root->ref)) | |
1254 return NULL; | |
1255 | |
1256 return phi; | |
1257 } | |
1258 | |
1259 /* Adds a reference for the looparound copy of REF in PHI to CHAIN. */ | |
1260 | |
1261 static void | |
111 | 1262 insert_looparound_copy (chain_p chain, dref ref, gphi *phi) |
0 | 1263 { |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1264 dref nw = XCNEW (struct dref_d), aref; |
0 | 1265 unsigned i; |
1266 | |
1267 nw->stmt = phi; | |
1268 nw->distance = ref->distance + 1; | |
1269 nw->always_accessed = 1; | |
1270 | |
111 | 1271 FOR_EACH_VEC_ELT (chain->refs, i, aref) |
0 | 1272 if (aref->distance >= nw->distance) |
1273 break; | |
111 | 1274 chain->refs.safe_insert (i, nw); |
0 | 1275 |
1276 if (nw->distance > chain->length) | |
1277 { | |
1278 chain->length = nw->distance; | |
1279 chain->has_max_use_after = false; | |
1280 } | |
1281 } | |
1282 | |
1283 /* For references in CHAIN that are copied around the LOOP (created previously | |
1284 by PRE, or by user), add the results of such copies to the chain. This | |
1285 enables us to remove the copies by unrolling, and may need less registers | |
1286 (also, it may allow us to combine chains together). */ | |
1287 | |
1288 static void | |
1289 add_looparound_copies (struct loop *loop, chain_p chain) | |
1290 { | |
1291 unsigned i; | |
1292 dref ref, root = get_chain_root (chain); | |
111 | 1293 gphi *phi; |
1294 | |
1295 if (chain->type == CT_STORE_STORE) | |
1296 return; | |
1297 | |
1298 FOR_EACH_VEC_ELT (chain->refs, i, ref) | |
0 | 1299 { |
1300 phi = find_looparound_phi (loop, ref, root); | |
1301 if (!phi) | |
1302 continue; | |
1303 | |
1304 bitmap_set_bit (looparound_phis, SSA_NAME_VERSION (PHI_RESULT (phi))); | |
1305 insert_looparound_copy (chain, ref, phi); | |
1306 } | |
1307 } | |
1308 | |
1309 /* Find roots of the values and determine distances in the component COMP. | |
1310 The references are redistributed into CHAINS. LOOP is the current | |
1311 loop. */ | |
1312 | |
1313 static void | |
1314 determine_roots_comp (struct loop *loop, | |
1315 struct component *comp, | |
111 | 1316 vec<chain_p> *chains) |
0 | 1317 { |
1318 unsigned i; | |
1319 dref a; | |
1320 chain_p chain = NULL; | |
111 | 1321 widest_int last_ofs = 0; |
1322 enum chain_type type; | |
0 | 1323 |
1324 /* Invariants are handled specially. */ | |
1325 if (comp->comp_step == RS_INVARIANT) | |
1326 { | |
1327 chain = make_invariant_chain (comp); | |
111 | 1328 chains->safe_push (chain); |
0 | 1329 return; |
1330 } | |
1331 | |
111 | 1332 /* Trivial component. */ |
1333 if (comp->refs.length () <= 1) | |
1334 return; | |
1335 | |
1336 comp->refs.qsort (order_drefs); | |
1337 FOR_EACH_VEC_ELT (comp->refs, i, a) | |
0 | 1338 { |
111 | 1339 if (!chain |
1340 || (!comp->eliminate_store_p && DR_IS_WRITE (a->ref)) | |
1341 || wi::leu_p (MAX_DISTANCE, a->offset - last_ofs)) | |
0 | 1342 { |
1343 if (nontrivial_chain_p (chain)) | |
63
b7f97abdc517
update gcc from gcc-4.5.0 to gcc-4.6
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
1344 { |
b7f97abdc517
update gcc from gcc-4.5.0 to gcc-4.6
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
1345 add_looparound_copies (loop, chain); |
111 | 1346 chains->safe_push (chain); |
63
b7f97abdc517
update gcc from gcc-4.5.0 to gcc-4.6
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
1347 } |
0 | 1348 else |
1349 release_chain (chain); | |
111 | 1350 |
1351 if (DR_IS_READ (a->ref)) | |
1352 type = CT_LOAD; | |
1353 else | |
1354 type = comp->eliminate_store_p ? CT_STORE_STORE : CT_STORE_LOAD; | |
1355 | |
1356 chain = make_rooted_chain (a, type); | |
63
b7f97abdc517
update gcc from gcc-4.5.0 to gcc-4.6
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
1357 last_ofs = a->offset; |
0 | 1358 continue; |
1359 } | |
1360 | |
1361 add_ref_to_chain (chain, a); | |
1362 } | |
1363 | |
1364 if (nontrivial_chain_p (chain)) | |
1365 { | |
1366 add_looparound_copies (loop, chain); | |
111 | 1367 chains->safe_push (chain); |
0 | 1368 } |
1369 else | |
1370 release_chain (chain); | |
1371 } | |
1372 | |
1373 /* Find roots of the values and determine distances in components COMPS, and | |
1374 separates the references to CHAINS. LOOP is the current loop. */ | |
1375 | |
1376 static void | |
1377 determine_roots (struct loop *loop, | |
111 | 1378 struct component *comps, vec<chain_p> *chains) |
0 | 1379 { |
1380 struct component *comp; | |
1381 | |
1382 for (comp = comps; comp; comp = comp->next) | |
1383 determine_roots_comp (loop, comp, chains); | |
1384 } | |
1385 | |
1386 /* Replace the reference in statement STMT with temporary variable | |
1387 NEW_TREE. If SET is true, NEW_TREE is instead initialized to the value of | |
1388 the reference in the statement. IN_LHS is true if the reference | |
1389 is in the lhs of STMT, false if it is in rhs. */ | |
1390 | |
1391 static void | |
111 | 1392 replace_ref_with (gimple *stmt, tree new_tree, bool set, bool in_lhs) |
0 | 1393 { |
1394 tree val; | |
111 | 1395 gassign *new_stmt; |
0 | 1396 gimple_stmt_iterator bsi, psi; |
1397 | |
1398 if (gimple_code (stmt) == GIMPLE_PHI) | |
1399 { | |
1400 gcc_assert (!in_lhs && !set); | |
1401 | |
1402 val = PHI_RESULT (stmt); | |
1403 bsi = gsi_after_labels (gimple_bb (stmt)); | |
1404 psi = gsi_for_stmt (stmt); | |
1405 remove_phi_node (&psi, false); | |
1406 | |
1407 /* Turn the phi node into GIMPLE_ASSIGN. */ | |
1408 new_stmt = gimple_build_assign (val, new_tree); | |
1409 gsi_insert_before (&bsi, new_stmt, GSI_NEW_STMT); | |
1410 return; | |
1411 } | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1412 |
0 | 1413 /* Since the reference is of gimple_reg type, it should only |
1414 appear as lhs or rhs of modify statement. */ | |
1415 gcc_assert (is_gimple_assign (stmt)); | |
1416 | |
1417 bsi = gsi_for_stmt (stmt); | |
1418 | |
1419 /* If we do not need to initialize NEW_TREE, just replace the use of OLD. */ | |
1420 if (!set) | |
1421 { | |
1422 gcc_assert (!in_lhs); | |
1423 gimple_assign_set_rhs_from_tree (&bsi, new_tree); | |
1424 stmt = gsi_stmt (bsi); | |
1425 update_stmt (stmt); | |
1426 return; | |
1427 } | |
1428 | |
1429 if (in_lhs) | |
1430 { | |
1431 /* We have statement | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1432 |
0 | 1433 OLD = VAL |
1434 | |
1435 If OLD is a memory reference, then VAL is gimple_val, and we transform | |
1436 this to | |
1437 | |
1438 OLD = VAL | |
1439 NEW = VAL | |
1440 | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1441 Otherwise, we are replacing a combination chain, |
0 | 1442 VAL is the expression that performs the combination, and OLD is an |
1443 SSA name. In this case, we transform the assignment to | |
1444 | |
1445 OLD = VAL | |
1446 NEW = OLD | |
1447 | |
1448 */ | |
1449 | |
1450 val = gimple_assign_lhs (stmt); | |
1451 if (TREE_CODE (val) != SSA_NAME) | |
1452 { | |
1453 val = gimple_assign_rhs1 (stmt); | |
111 | 1454 gcc_assert (gimple_assign_single_p (stmt)); |
1455 if (TREE_CLOBBER_P (val)) | |
1456 val = get_or_create_ssa_default_def (cfun, SSA_NAME_VAR (new_tree)); | |
1457 else | |
1458 gcc_assert (gimple_assign_copy_p (stmt)); | |
0 | 1459 } |
1460 } | |
1461 else | |
1462 { | |
1463 /* VAL = OLD | |
1464 | |
1465 is transformed to | |
1466 | |
1467 VAL = OLD | |
1468 NEW = VAL */ | |
1469 | |
1470 val = gimple_assign_lhs (stmt); | |
1471 } | |
1472 | |
1473 new_stmt = gimple_build_assign (new_tree, unshare_expr (val)); | |
1474 gsi_insert_after (&bsi, new_stmt, GSI_NEW_STMT); | |
1475 } | |
1476 | |
111 | 1477 /* Returns a memory reference to DR in the (NITERS + ITER)-th iteration |
1478 of the loop it was analyzed in. Append init stmts to STMTS. */ | |
0 | 1479 |
1480 static tree | |
111 | 1481 ref_at_iteration (data_reference_p dr, int iter, |
1482 gimple_seq *stmts, tree niters = NULL_TREE) | |
0 | 1483 { |
111 | 1484 tree off = DR_OFFSET (dr); |
1485 tree coff = DR_INIT (dr); | |
1486 tree ref = DR_REF (dr); | |
1487 enum tree_code ref_code = ERROR_MARK; | |
1488 tree ref_type = NULL_TREE; | |
1489 tree ref_op1 = NULL_TREE; | |
1490 tree ref_op2 = NULL_TREE; | |
1491 tree new_offset; | |
1492 | |
1493 if (iter != 0) | |
0 | 1494 { |
111 | 1495 new_offset = size_binop (MULT_EXPR, DR_STEP (dr), ssize_int (iter)); |
1496 if (TREE_CODE (new_offset) == INTEGER_CST) | |
1497 coff = size_binop (PLUS_EXPR, coff, new_offset); | |
1498 else | |
1499 off = size_binop (PLUS_EXPR, off, new_offset); | |
0 | 1500 } |
111 | 1501 |
1502 if (niters != NULL_TREE) | |
0 | 1503 { |
111 | 1504 niters = fold_convert (ssizetype, niters); |
1505 new_offset = size_binop (MULT_EXPR, DR_STEP (dr), niters); | |
1506 if (TREE_CODE (niters) == INTEGER_CST) | |
1507 coff = size_binop (PLUS_EXPR, coff, new_offset); | |
1508 else | |
1509 off = size_binop (PLUS_EXPR, off, new_offset); | |
0 | 1510 } |
111 | 1511 |
1512 /* While data-ref analysis punts on bit offsets it still handles | |
1513 bitfield accesses at byte boundaries. Cope with that. Note that | |
1514 if the bitfield object also starts at a byte-boundary we can simply | |
1515 replicate the COMPONENT_REF, but we have to subtract the component's | |
1516 byte-offset from the MEM_REF address first. | |
1517 Otherwise we simply build a BIT_FIELD_REF knowing that the bits | |
1518 start at offset zero. */ | |
1519 if (TREE_CODE (ref) == COMPONENT_REF | |
1520 && DECL_BIT_FIELD (TREE_OPERAND (ref, 1))) | |
0 | 1521 { |
111 | 1522 unsigned HOST_WIDE_INT boff; |
1523 tree field = TREE_OPERAND (ref, 1); | |
1524 tree offset = component_ref_field_offset (ref); | |
1525 ref_type = TREE_TYPE (ref); | |
1526 boff = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field)); | |
1527 /* This can occur in Ada. See the comment in get_bit_range. */ | |
1528 if (boff % BITS_PER_UNIT != 0 | |
1529 || !tree_fits_uhwi_p (offset)) | |
0 | 1530 { |
111 | 1531 ref_code = BIT_FIELD_REF; |
1532 ref_op1 = DECL_SIZE (field); | |
1533 ref_op2 = bitsize_zero_node; | |
0 | 1534 } |
1535 else | |
1536 { | |
111 | 1537 boff >>= LOG2_BITS_PER_UNIT; |
1538 boff += tree_to_uhwi (offset); | |
1539 coff = size_binop (MINUS_EXPR, coff, ssize_int (boff)); | |
1540 ref_code = COMPONENT_REF; | |
1541 ref_op1 = field; | |
1542 ref_op2 = TREE_OPERAND (ref, 2); | |
1543 ref = TREE_OPERAND (ref, 0); | |
0 | 1544 } |
1545 } | |
111 | 1546 tree addr = fold_build_pointer_plus (DR_BASE_ADDRESS (dr), off); |
1547 addr = force_gimple_operand_1 (unshare_expr (addr), stmts, | |
1548 is_gimple_mem_ref_addr, NULL_TREE); | |
1549 tree alias_ptr = fold_convert (reference_alias_ptr_type (ref), coff); | |
1550 tree type = build_aligned_type (TREE_TYPE (ref), | |
1551 get_object_alignment (ref)); | |
1552 ref = build2 (MEM_REF, type, addr, alias_ptr); | |
1553 if (ref_type) | |
1554 ref = build3 (ref_code, ref_type, ref, ref_op1, ref_op2); | |
1555 return ref; | |
0 | 1556 } |
1557 | |
1558 /* Get the initialization expression for the INDEX-th temporary variable | |
1559 of CHAIN. */ | |
1560 | |
1561 static tree | |
1562 get_init_expr (chain_p chain, unsigned index) | |
1563 { | |
1564 if (chain->type == CT_COMBINATION) | |
1565 { | |
1566 tree e1 = get_init_expr (chain->ch1, index); | |
1567 tree e2 = get_init_expr (chain->ch2, index); | |
1568 | |
1569 return fold_build2 (chain->op, chain->rslt_type, e1, e2); | |
1570 } | |
1571 else | |
111 | 1572 return chain->inits[index]; |
0 | 1573 } |
1574 | |
1575 /* Returns a new temporary variable used for the I-th variable carrying | |
1576 value of REF. The variable's uid is marked in TMP_VARS. */ | |
1577 | |
1578 static tree | |
1579 predcom_tmp_var (tree ref, unsigned i, bitmap tmp_vars) | |
1580 { | |
1581 tree type = TREE_TYPE (ref); | |
1582 /* We never access the components of the temporary variable in predictive | |
1583 commoning. */ | |
63
b7f97abdc517
update gcc from gcc-4.5.0 to gcc-4.6
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
1584 tree var = create_tmp_reg (type, get_lsm_tmp_name (ref, i)); |
0 | 1585 bitmap_set_bit (tmp_vars, DECL_UID (var)); |
1586 return var; | |
1587 } | |
1588 | |
1589 /* Creates the variables for CHAIN, as well as phi nodes for them and | |
1590 initialization on entry to LOOP. Uids of the newly created | |
1591 temporary variables are marked in TMP_VARS. */ | |
1592 | |
1593 static void | |
1594 initialize_root_vars (struct loop *loop, chain_p chain, bitmap tmp_vars) | |
1595 { | |
1596 unsigned i; | |
1597 unsigned n = chain->length; | |
1598 dref root = get_chain_root (chain); | |
1599 bool reuse_first = !chain->has_max_use_after; | |
1600 tree ref, init, var, next; | |
111 | 1601 gphi *phi; |
0 | 1602 gimple_seq stmts; |
1603 edge entry = loop_preheader_edge (loop), latch = loop_latch_edge (loop); | |
1604 | |
1605 /* If N == 0, then all the references are within the single iteration. And | |
1606 since this is an nonempty chain, reuse_first cannot be true. */ | |
1607 gcc_assert (n > 0 || !reuse_first); | |
1608 | |
111 | 1609 chain->vars.create (n + 1); |
0 | 1610 |
1611 if (chain->type == CT_COMBINATION) | |
1612 ref = gimple_assign_lhs (root->stmt); | |
1613 else | |
1614 ref = DR_REF (root->ref); | |
1615 | |
1616 for (i = 0; i < n + (reuse_first ? 0 : 1); i++) | |
1617 { | |
1618 var = predcom_tmp_var (ref, i, tmp_vars); | |
111 | 1619 chain->vars.quick_push (var); |
0 | 1620 } |
1621 if (reuse_first) | |
111 | 1622 chain->vars.quick_push (chain->vars[0]); |
1623 | |
1624 FOR_EACH_VEC_ELT (chain->vars, i, var) | |
1625 chain->vars[i] = make_ssa_name (var); | |
0 | 1626 |
1627 for (i = 0; i < n; i++) | |
1628 { | |
111 | 1629 var = chain->vars[i]; |
1630 next = chain->vars[i + 1]; | |
0 | 1631 init = get_init_expr (chain, i); |
1632 | |
1633 init = force_gimple_operand (init, &stmts, true, NULL_TREE); | |
1634 if (stmts) | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1635 gsi_insert_seq_on_edge_immediate (entry, stmts); |
0 | 1636 |
1637 phi = create_phi_node (var, loop->header); | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1638 add_phi_arg (phi, init, entry, UNKNOWN_LOCATION); |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1639 add_phi_arg (phi, next, latch, UNKNOWN_LOCATION); |
0 | 1640 } |
1641 } | |
1642 | |
111 | 1643 /* For inter-iteration store elimination CHAIN in LOOP, returns true if |
1644 all stores to be eliminated store loop invariant values into memory. | |
1645 In this case, we can use these invariant values directly after LOOP. */ | |
1646 | |
1647 static bool | |
1648 is_inv_store_elimination_chain (struct loop *loop, chain_p chain) | |
1649 { | |
1650 if (chain->length == 0 || chain->type != CT_STORE_STORE) | |
1651 return false; | |
1652 | |
1653 gcc_assert (!chain->has_max_use_after); | |
1654 | |
1655 /* If loop iterates for unknown times or fewer times than chain->lenght, | |
1656 we still need to setup root variable and propagate it with PHI node. */ | |
1657 tree niters = number_of_latch_executions (loop); | |
1658 if (TREE_CODE (niters) != INTEGER_CST | |
1659 || wi::leu_p (wi::to_wide (niters), chain->length)) | |
1660 return false; | |
1661 | |
1662 /* Check stores in chain for elimination if they only store loop invariant | |
1663 values. */ | |
1664 for (unsigned i = 0; i < chain->length; i++) | |
1665 { | |
1666 dref a = get_chain_last_ref_at (chain, i); | |
1667 if (a == NULL) | |
1668 continue; | |
1669 | |
1670 gimple *def_stmt, *stmt = a->stmt; | |
1671 if (!gimple_assign_single_p (stmt)) | |
1672 return false; | |
1673 | |
1674 tree val = gimple_assign_rhs1 (stmt); | |
1675 if (TREE_CLOBBER_P (val)) | |
1676 return false; | |
1677 | |
1678 if (CONSTANT_CLASS_P (val)) | |
1679 continue; | |
1680 | |
1681 if (TREE_CODE (val) != SSA_NAME) | |
1682 return false; | |
1683 | |
1684 def_stmt = SSA_NAME_DEF_STMT (val); | |
1685 if (gimple_nop_p (def_stmt)) | |
1686 continue; | |
1687 | |
1688 if (flow_bb_inside_loop_p (loop, gimple_bb (def_stmt))) | |
1689 return false; | |
1690 } | |
1691 return true; | |
1692 } | |
1693 | |
1694 /* Creates root variables for store elimination CHAIN in which stores for | |
1695 elimination only store loop invariant values. In this case, we neither | |
1696 need to load root variables before loop nor propagate it with PHI nodes. */ | |
1697 | |
1698 static void | |
1699 initialize_root_vars_store_elim_1 (chain_p chain) | |
1700 { | |
1701 tree var; | |
1702 unsigned i, n = chain->length; | |
1703 | |
1704 chain->vars.create (n); | |
1705 chain->vars.safe_grow_cleared (n); | |
1706 | |
1707 /* Initialize root value for eliminated stores at each distance. */ | |
1708 for (i = 0; i < n; i++) | |
1709 { | |
1710 dref a = get_chain_last_ref_at (chain, i); | |
1711 if (a == NULL) | |
1712 continue; | |
1713 | |
1714 var = gimple_assign_rhs1 (a->stmt); | |
1715 chain->vars[a->distance] = var; | |
1716 } | |
1717 | |
1718 /* We don't propagate values with PHI nodes, so manually propagate value | |
1719 to bubble positions. */ | |
1720 var = chain->vars[0]; | |
1721 for (i = 1; i < n; i++) | |
1722 { | |
1723 if (chain->vars[i] != NULL_TREE) | |
1724 { | |
1725 var = chain->vars[i]; | |
1726 continue; | |
1727 } | |
1728 chain->vars[i] = var; | |
1729 } | |
1730 | |
1731 /* Revert the vector. */ | |
1732 for (i = 0; i < n / 2; i++) | |
1733 std::swap (chain->vars[i], chain->vars[n - i - 1]); | |
1734 } | |
1735 | |
1736 /* Creates root variables for store elimination CHAIN in which stores for | |
1737 elimination store loop variant values. In this case, we may need to | |
1738 load root variables before LOOP and propagate it with PHI nodes. Uids | |
1739 of the newly created root variables are marked in TMP_VARS. */ | |
0 | 1740 |
1741 static void | |
111 | 1742 initialize_root_vars_store_elim_2 (struct loop *loop, |
1743 chain_p chain, bitmap tmp_vars) | |
0 | 1744 { |
111 | 1745 unsigned i, n = chain->length; |
1746 tree ref, init, var, next, val, phi_result; | |
1747 gimple *stmt; | |
1748 gimple_seq stmts; | |
1749 | |
1750 chain->vars.create (n); | |
1751 | |
1752 ref = DR_REF (get_chain_root (chain)->ref); | |
1753 for (i = 0; i < n; i++) | |
1754 { | |
1755 var = predcom_tmp_var (ref, i, tmp_vars); | |
1756 chain->vars.quick_push (var); | |
1757 } | |
1758 | |
1759 FOR_EACH_VEC_ELT (chain->vars, i, var) | |
1760 chain->vars[i] = make_ssa_name (var); | |
1761 | |
1762 /* Root values are either rhs operand of stores to be eliminated, or | |
1763 loaded from memory before loop. */ | |
1764 auto_vec<tree> vtemps; | |
1765 vtemps.safe_grow_cleared (n); | |
1766 for (i = 0; i < n; i++) | |
1767 { | |
1768 init = get_init_expr (chain, i); | |
1769 if (init == NULL_TREE) | |
1770 { | |
1771 /* Root value is rhs operand of the store to be eliminated if | |
1772 it isn't loaded from memory before loop. */ | |
1773 dref a = get_chain_last_ref_at (chain, i); | |
1774 val = gimple_assign_rhs1 (a->stmt); | |
1775 if (TREE_CLOBBER_P (val)) | |
1776 val = get_or_create_ssa_default_def (cfun, SSA_NAME_VAR (var)); | |
1777 | |
1778 vtemps[n - i - 1] = val; | |
1779 } | |
1780 else | |
1781 { | |
1782 edge latch = loop_latch_edge (loop); | |
1783 edge entry = loop_preheader_edge (loop); | |
1784 | |
1785 /* Root value is loaded from memory before loop, we also need | |
1786 to add PHI nodes to propagate the value across iterations. */ | |
1787 init = force_gimple_operand (init, &stmts, true, NULL_TREE); | |
1788 if (stmts) | |
1789 gsi_insert_seq_on_edge_immediate (entry, stmts); | |
1790 | |
1791 next = chain->vars[n - i]; | |
1792 phi_result = copy_ssa_name (next); | |
1793 gphi *phi = create_phi_node (phi_result, loop->header); | |
1794 add_phi_arg (phi, init, entry, UNKNOWN_LOCATION); | |
1795 add_phi_arg (phi, next, latch, UNKNOWN_LOCATION); | |
1796 vtemps[n - i - 1] = phi_result; | |
1797 } | |
1798 } | |
1799 | |
1800 /* Find the insertion position. */ | |
1801 dref last = get_chain_root (chain); | |
1802 for (i = 0; i < chain->refs.length (); i++) | |
1803 { | |
1804 if (chain->refs[i]->pos > last->pos) | |
1805 last = chain->refs[i]; | |
1806 } | |
1807 | |
1808 gimple_stmt_iterator gsi = gsi_for_stmt (last->stmt); | |
1809 | |
1810 /* Insert statements copying root value to root variable. */ | |
1811 for (i = 0; i < n; i++) | |
1812 { | |
1813 var = chain->vars[i]; | |
1814 val = vtemps[i]; | |
1815 stmt = gimple_build_assign (var, val); | |
1816 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT); | |
1817 } | |
1818 } | |
1819 | |
1820 /* Generates stores for CHAIN's eliminated stores in LOOP's last | |
1821 (CHAIN->length - 1) iterations. */ | |
1822 | |
1823 static void | |
1824 finalize_eliminated_stores (struct loop *loop, chain_p chain) | |
1825 { | |
1826 unsigned i, n = chain->length; | |
1827 | |
1828 for (i = 0; i < n; i++) | |
1829 { | |
1830 tree var = chain->vars[i]; | |
1831 tree fini = chain->finis[n - i - 1]; | |
1832 gimple *stmt = gimple_build_assign (fini, var); | |
1833 | |
1834 gimple_seq_add_stmt_without_update (&chain->fini_seq, stmt); | |
1835 } | |
1836 | |
1837 if (chain->fini_seq) | |
1838 { | |
1839 gsi_insert_seq_on_edge_immediate (single_exit (loop), chain->fini_seq); | |
1840 chain->fini_seq = NULL; | |
1841 } | |
0 | 1842 } |
1843 | |
1844 /* Initializes a variable for load motion for ROOT and prepares phi nodes and | |
1845 initialization on entry to LOOP if necessary. The ssa name for the variable | |
1846 is stored in VARS. If WRITTEN is true, also a phi node to copy its value | |
1847 around the loop is created. Uid of the newly created temporary variable | |
1848 is marked in TMP_VARS. INITS is the list containing the (single) | |
1849 initializer. */ | |
1850 | |
1851 static void | |
1852 initialize_root_vars_lm (struct loop *loop, dref root, bool written, | |
111 | 1853 vec<tree> *vars, vec<tree> inits, |
0 | 1854 bitmap tmp_vars) |
1855 { | |
1856 unsigned i; | |
1857 tree ref = DR_REF (root->ref), init, var, next; | |
1858 gimple_seq stmts; | |
111 | 1859 gphi *phi; |
0 | 1860 edge entry = loop_preheader_edge (loop), latch = loop_latch_edge (loop); |
1861 | |
1862 /* Find the initializer for the variable, and check that it cannot | |
1863 trap. */ | |
111 | 1864 init = inits[0]; |
1865 | |
1866 vars->create (written ? 2 : 1); | |
0 | 1867 var = predcom_tmp_var (ref, 0, tmp_vars); |
111 | 1868 vars->quick_push (var); |
0 | 1869 if (written) |
111 | 1870 vars->quick_push ((*vars)[0]); |
1871 | |
1872 FOR_EACH_VEC_ELT (*vars, i, var) | |
1873 (*vars)[i] = make_ssa_name (var); | |
1874 | |
1875 var = (*vars)[0]; | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1876 |
0 | 1877 init = force_gimple_operand (init, &stmts, written, NULL_TREE); |
1878 if (stmts) | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1879 gsi_insert_seq_on_edge_immediate (entry, stmts); |
0 | 1880 |
1881 if (written) | |
1882 { | |
111 | 1883 next = (*vars)[1]; |
0 | 1884 phi = create_phi_node (var, loop->header); |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1885 add_phi_arg (phi, init, entry, UNKNOWN_LOCATION); |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1886 add_phi_arg (phi, next, latch, UNKNOWN_LOCATION); |
0 | 1887 } |
1888 else | |
1889 { | |
111 | 1890 gassign *init_stmt = gimple_build_assign (var, init); |
0 | 1891 gsi_insert_on_edge_immediate (entry, init_stmt); |
1892 } | |
1893 } | |
1894 | |
1895 | |
1896 /* Execute load motion for references in chain CHAIN. Uids of the newly | |
1897 created temporary variables are marked in TMP_VARS. */ | |
1898 | |
1899 static void | |
1900 execute_load_motion (struct loop *loop, chain_p chain, bitmap tmp_vars) | |
1901 { | |
111 | 1902 auto_vec<tree> vars; |
0 | 1903 dref a; |
1904 unsigned n_writes = 0, ridx, i; | |
1905 tree var; | |
1906 | |
1907 gcc_assert (chain->type == CT_INVARIANT); | |
1908 gcc_assert (!chain->combined); | |
111 | 1909 FOR_EACH_VEC_ELT (chain->refs, i, a) |
67
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
1910 if (DR_IS_WRITE (a->ref)) |
0 | 1911 n_writes++; |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1912 |
0 | 1913 /* If there are no reads in the loop, there is nothing to do. */ |
111 | 1914 if (n_writes == chain->refs.length ()) |
0 | 1915 return; |
1916 | |
1917 initialize_root_vars_lm (loop, get_chain_root (chain), n_writes > 0, | |
1918 &vars, chain->inits, tmp_vars); | |
1919 | |
1920 ridx = 0; | |
111 | 1921 FOR_EACH_VEC_ELT (chain->refs, i, a) |
0 | 1922 { |
1923 bool is_read = DR_IS_READ (a->ref); | |
1924 | |
67
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
1925 if (DR_IS_WRITE (a->ref)) |
0 | 1926 { |
1927 n_writes--; | |
1928 if (n_writes) | |
1929 { | |
111 | 1930 var = vars[0]; |
1931 var = make_ssa_name (SSA_NAME_VAR (var)); | |
1932 vars[0] = var; | |
0 | 1933 } |
1934 else | |
1935 ridx = 1; | |
1936 } | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1937 |
111 | 1938 replace_ref_with (a->stmt, vars[ridx], |
0 | 1939 !is_read, !is_read); |
1940 } | |
1941 } | |
1942 | |
1943 /* Returns the single statement in that NAME is used, excepting | |
1944 the looparound phi nodes contained in one of the chains. If there is no | |
1945 such statement, or more statements, NULL is returned. */ | |
1946 | |
111 | 1947 static gimple * |
0 | 1948 single_nonlooparound_use (tree name) |
1949 { | |
1950 use_operand_p use; | |
1951 imm_use_iterator it; | |
111 | 1952 gimple *stmt, *ret = NULL; |
0 | 1953 |
1954 FOR_EACH_IMM_USE_FAST (use, it, name) | |
1955 { | |
1956 stmt = USE_STMT (use); | |
1957 | |
1958 if (gimple_code (stmt) == GIMPLE_PHI) | |
1959 { | |
1960 /* Ignore uses in looparound phi nodes. Uses in other phi nodes | |
1961 could not be processed anyway, so just fail for them. */ | |
1962 if (bitmap_bit_p (looparound_phis, | |
1963 SSA_NAME_VERSION (PHI_RESULT (stmt)))) | |
1964 continue; | |
1965 | |
1966 return NULL; | |
1967 } | |
67
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
1968 else if (is_gimple_debug (stmt)) |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
1969 continue; |
0 | 1970 else if (ret != NULL) |
1971 return NULL; | |
1972 else | |
1973 ret = stmt; | |
1974 } | |
1975 | |
1976 return ret; | |
1977 } | |
1978 | |
1979 /* Remove statement STMT, as well as the chain of assignments in that it is | |
1980 used. */ | |
1981 | |
1982 static void | |
111 | 1983 remove_stmt (gimple *stmt) |
0 | 1984 { |
1985 tree name; | |
111 | 1986 gimple *next; |
0 | 1987 gimple_stmt_iterator psi; |
1988 | |
1989 if (gimple_code (stmt) == GIMPLE_PHI) | |
1990 { | |
1991 name = PHI_RESULT (stmt); | |
1992 next = single_nonlooparound_use (name); | |
111 | 1993 reset_debug_uses (stmt); |
0 | 1994 psi = gsi_for_stmt (stmt); |
1995 remove_phi_node (&psi, true); | |
1996 | |
1997 if (!next | |
1998 || !gimple_assign_ssa_name_copy_p (next) | |
1999 || gimple_assign_rhs1 (next) != name) | |
2000 return; | |
2001 | |
2002 stmt = next; | |
2003 } | |
2004 | |
2005 while (1) | |
2006 { | |
2007 gimple_stmt_iterator bsi; | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
2008 |
0 | 2009 bsi = gsi_for_stmt (stmt); |
2010 | |
2011 name = gimple_assign_lhs (stmt); | |
111 | 2012 if (TREE_CODE (name) == SSA_NAME) |
2013 { | |
2014 next = single_nonlooparound_use (name); | |
2015 reset_debug_uses (stmt); | |
2016 } | |
2017 else | |
2018 { | |
2019 /* This is a store to be eliminated. */ | |
2020 gcc_assert (gimple_vdef (stmt) != NULL); | |
2021 next = NULL; | |
2022 } | |
2023 | |
2024 unlink_stmt_vdef (stmt); | |
0 | 2025 gsi_remove (&bsi, true); |
2026 release_defs (stmt); | |
2027 | |
2028 if (!next | |
2029 || !gimple_assign_ssa_name_copy_p (next) | |
2030 || gimple_assign_rhs1 (next) != name) | |
2031 return; | |
2032 | |
2033 stmt = next; | |
2034 } | |
2035 } | |
2036 | |
2037 /* Perform the predictive commoning optimization for a chain CHAIN. | |
2038 Uids of the newly created temporary variables are marked in TMP_VARS.*/ | |
2039 | |
2040 static void | |
2041 execute_pred_commoning_chain (struct loop *loop, chain_p chain, | |
111 | 2042 bitmap tmp_vars) |
0 | 2043 { |
111 | 2044 unsigned i, n; |
2045 dref a; | |
0 | 2046 tree var; |
111 | 2047 bool in_lhs; |
0 | 2048 |
2049 if (chain->combined) | |
2050 { | |
2051 /* For combined chains, just remove the statements that are used to | |
111 | 2052 compute the values of the expression (except for the root one). |
2053 We delay this until after all chains are processed. */ | |
2054 } | |
2055 else if (chain->type == CT_STORE_STORE) | |
2056 { | |
2057 if (chain->length > 0) | |
2058 { | |
2059 if (chain->inv_store_elimination) | |
2060 { | |
2061 /* If dead stores in this chain only store loop invariant | |
2062 values, we can simply record the invariant value and use | |
2063 it directly after loop. */ | |
2064 initialize_root_vars_store_elim_1 (chain); | |
2065 } | |
2066 else | |
2067 { | |
2068 /* If dead stores in this chain store loop variant values, | |
2069 we need to set up the variables by loading from memory | |
2070 before loop and propagating it with PHI nodes. */ | |
2071 initialize_root_vars_store_elim_2 (loop, chain, tmp_vars); | |
2072 } | |
2073 | |
2074 /* For inter-iteration store elimination chain, stores at each | |
2075 distance in loop's last (chain->length - 1) iterations can't | |
2076 be eliminated, because there is no following killing store. | |
2077 We need to generate these stores after loop. */ | |
2078 finalize_eliminated_stores (loop, chain); | |
2079 } | |
2080 | |
2081 /* Eliminate the stores killed by following store. */ | |
2082 n = chain->refs.length (); | |
2083 for (i = 0; i < n - 1; i++) | |
2084 remove_stmt (chain->refs[i]->stmt); | |
0 | 2085 } |
2086 else | |
2087 { | |
111 | 2088 /* For non-combined chains, set up the variables that hold its value. */ |
2089 initialize_root_vars (loop, chain, tmp_vars); | |
2090 a = get_chain_root (chain); | |
2091 in_lhs = (chain->type == CT_STORE_LOAD | |
2092 || chain->type == CT_COMBINATION); | |
2093 replace_ref_with (a->stmt, chain->vars[chain->length], true, in_lhs); | |
2094 | |
2095 /* Replace the uses of the original references by these variables. */ | |
2096 for (i = 1; chain->refs.iterate (i, &a); i++) | |
0 | 2097 { |
111 | 2098 var = chain->vars[chain->length - a->distance]; |
0 | 2099 replace_ref_with (a->stmt, var, false, false); |
2100 } | |
2101 } | |
2102 } | |
2103 | |
2104 /* Determines the unroll factor necessary to remove as many temporary variable | |
2105 copies as possible. CHAINS is the list of chains that will be | |
2106 optimized. */ | |
2107 | |
2108 static unsigned | |
111 | 2109 determine_unroll_factor (vec<chain_p> chains) |
0 | 2110 { |
2111 chain_p chain; | |
2112 unsigned factor = 1, af, nfactor, i; | |
2113 unsigned max = PARAM_VALUE (PARAM_MAX_UNROLL_TIMES); | |
2114 | |
111 | 2115 FOR_EACH_VEC_ELT (chains, i, chain) |
0 | 2116 { |
111 | 2117 if (chain->type == CT_INVARIANT) |
0 | 2118 continue; |
111 | 2119 /* For now we can't handle unrolling when eliminating stores. */ |
2120 else if (chain->type == CT_STORE_STORE) | |
2121 return 1; | |
2122 | |
2123 if (chain->combined) | |
2124 { | |
2125 /* For combined chains, we can't handle unrolling if we replace | |
2126 looparound PHIs. */ | |
2127 dref a; | |
2128 unsigned j; | |
2129 for (j = 1; chain->refs.iterate (j, &a); j++) | |
2130 if (gimple_code (a->stmt) == GIMPLE_PHI) | |
2131 return 1; | |
2132 continue; | |
2133 } | |
0 | 2134 |
2135 /* The best unroll factor for this chain is equal to the number of | |
2136 temporary variables that we create for it. */ | |
2137 af = chain->length; | |
2138 if (chain->has_max_use_after) | |
2139 af++; | |
2140 | |
2141 nfactor = factor * af / gcd (factor, af); | |
2142 if (nfactor <= max) | |
2143 factor = nfactor; | |
2144 } | |
2145 | |
2146 return factor; | |
2147 } | |
2148 | |
2149 /* Perform the predictive commoning optimization for CHAINS. | |
2150 Uids of the newly created temporary variables are marked in TMP_VARS. */ | |
2151 | |
2152 static void | |
111 | 2153 execute_pred_commoning (struct loop *loop, vec<chain_p> chains, |
0 | 2154 bitmap tmp_vars) |
2155 { | |
2156 chain_p chain; | |
2157 unsigned i; | |
2158 | |
111 | 2159 FOR_EACH_VEC_ELT (chains, i, chain) |
0 | 2160 { |
2161 if (chain->type == CT_INVARIANT) | |
2162 execute_load_motion (loop, chain, tmp_vars); | |
2163 else | |
2164 execute_pred_commoning_chain (loop, chain, tmp_vars); | |
2165 } | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
2166 |
111 | 2167 FOR_EACH_VEC_ELT (chains, i, chain) |
2168 { | |
2169 if (chain->type == CT_INVARIANT) | |
2170 ; | |
2171 else if (chain->combined) | |
2172 { | |
2173 /* For combined chains, just remove the statements that are used to | |
2174 compute the values of the expression (except for the root one). */ | |
2175 dref a; | |
2176 unsigned j; | |
2177 for (j = 1; chain->refs.iterate (j, &a); j++) | |
2178 remove_stmt (a->stmt); | |
2179 } | |
2180 } | |
2181 | |
0 | 2182 update_ssa (TODO_update_ssa_only_virtuals); |
2183 } | |
2184 | |
2185 /* For each reference in CHAINS, if its defining statement is | |
2186 phi node, record the ssa name that is defined by it. */ | |
2187 | |
2188 static void | |
111 | 2189 replace_phis_by_defined_names (vec<chain_p> chains) |
0 | 2190 { |
2191 chain_p chain; | |
2192 dref a; | |
2193 unsigned i, j; | |
2194 | |
111 | 2195 FOR_EACH_VEC_ELT (chains, i, chain) |
2196 FOR_EACH_VEC_ELT (chain->refs, j, a) | |
0 | 2197 { |
2198 if (gimple_code (a->stmt) == GIMPLE_PHI) | |
2199 { | |
2200 a->name_defined_by_phi = PHI_RESULT (a->stmt); | |
2201 a->stmt = NULL; | |
2202 } | |
2203 } | |
2204 } | |
2205 | |
2206 /* For each reference in CHAINS, if name_defined_by_phi is not | |
2207 NULL, use it to set the stmt field. */ | |
2208 | |
2209 static void | |
111 | 2210 replace_names_by_phis (vec<chain_p> chains) |
0 | 2211 { |
2212 chain_p chain; | |
2213 dref a; | |
2214 unsigned i, j; | |
2215 | |
111 | 2216 FOR_EACH_VEC_ELT (chains, i, chain) |
2217 FOR_EACH_VEC_ELT (chain->refs, j, a) | |
0 | 2218 if (a->stmt == NULL) |
2219 { | |
2220 a->stmt = SSA_NAME_DEF_STMT (a->name_defined_by_phi); | |
2221 gcc_assert (gimple_code (a->stmt) == GIMPLE_PHI); | |
2222 a->name_defined_by_phi = NULL_TREE; | |
2223 } | |
2224 } | |
2225 | |
2226 /* Wrapper over execute_pred_commoning, to pass it as a callback | |
2227 to tree_transform_and_unroll_loop. */ | |
2228 | |
2229 struct epcc_data | |
2230 { | |
111 | 2231 vec<chain_p> chains; |
0 | 2232 bitmap tmp_vars; |
2233 }; | |
2234 | |
2235 static void | |
2236 execute_pred_commoning_cbck (struct loop *loop, void *data) | |
2237 { | |
2238 struct epcc_data *const dta = (struct epcc_data *) data; | |
2239 | |
2240 /* Restore phi nodes that were replaced by ssa names before | |
2241 tree_transform_and_unroll_loop (see detailed description in | |
2242 tree_predictive_commoning_loop). */ | |
2243 replace_names_by_phis (dta->chains); | |
2244 execute_pred_commoning (loop, dta->chains, dta->tmp_vars); | |
2245 } | |
2246 | |
2247 /* Base NAME and all the names in the chain of phi nodes that use it | |
2248 on variable VAR. The phi nodes are recognized by being in the copies of | |
2249 the header of the LOOP. */ | |
2250 | |
2251 static void | |
2252 base_names_in_chain_on (struct loop *loop, tree name, tree var) | |
2253 { | |
111 | 2254 gimple *stmt, *phi; |
0 | 2255 imm_use_iterator iter; |
2256 | |
111 | 2257 replace_ssa_name_symbol (name, var); |
0 | 2258 |
2259 while (1) | |
2260 { | |
2261 phi = NULL; | |
2262 FOR_EACH_IMM_USE_STMT (stmt, iter, name) | |
2263 { | |
2264 if (gimple_code (stmt) == GIMPLE_PHI | |
2265 && flow_bb_inside_loop_p (loop, gimple_bb (stmt))) | |
2266 { | |
2267 phi = stmt; | |
2268 BREAK_FROM_IMM_USE_STMT (iter); | |
2269 } | |
2270 } | |
2271 if (!phi) | |
2272 return; | |
2273 | |
2274 name = PHI_RESULT (phi); | |
111 | 2275 replace_ssa_name_symbol (name, var); |
0 | 2276 } |
2277 } | |
2278 | |
2279 /* Given an unrolled LOOP after predictive commoning, remove the | |
2280 register copies arising from phi nodes by changing the base | |
2281 variables of SSA names. TMP_VARS is the set of the temporary variables | |
2282 for those we want to perform this. */ | |
2283 | |
2284 static void | |
2285 eliminate_temp_copies (struct loop *loop, bitmap tmp_vars) | |
2286 { | |
2287 edge e; | |
111 | 2288 gphi *phi; |
2289 gimple *stmt; | |
0 | 2290 tree name, use, var; |
111 | 2291 gphi_iterator psi; |
0 | 2292 |
2293 e = loop_latch_edge (loop); | |
2294 for (psi = gsi_start_phis (loop->header); !gsi_end_p (psi); gsi_next (&psi)) | |
2295 { | |
111 | 2296 phi = psi.phi (); |
0 | 2297 name = PHI_RESULT (phi); |
2298 var = SSA_NAME_VAR (name); | |
111 | 2299 if (!var || !bitmap_bit_p (tmp_vars, DECL_UID (var))) |
0 | 2300 continue; |
2301 use = PHI_ARG_DEF_FROM_EDGE (phi, e); | |
2302 gcc_assert (TREE_CODE (use) == SSA_NAME); | |
2303 | |
2304 /* Base all the ssa names in the ud and du chain of NAME on VAR. */ | |
2305 stmt = SSA_NAME_DEF_STMT (use); | |
2306 while (gimple_code (stmt) == GIMPLE_PHI | |
2307 /* In case we could not unroll the loop enough to eliminate | |
2308 all copies, we may reach the loop header before the defining | |
2309 statement (in that case, some register copies will be present | |
2310 in loop latch in the final code, corresponding to the newly | |
2311 created looparound phi nodes). */ | |
2312 && gimple_bb (stmt) != loop->header) | |
2313 { | |
2314 gcc_assert (single_pred_p (gimple_bb (stmt))); | |
2315 use = PHI_ARG_DEF (stmt, 0); | |
2316 stmt = SSA_NAME_DEF_STMT (use); | |
2317 } | |
2318 | |
2319 base_names_in_chain_on (loop, use, var); | |
2320 } | |
2321 } | |
2322 | |
2323 /* Returns true if CHAIN is suitable to be combined. */ | |
2324 | |
2325 static bool | |
2326 chain_can_be_combined_p (chain_p chain) | |
2327 { | |
2328 return (!chain->combined | |
2329 && (chain->type == CT_LOAD || chain->type == CT_COMBINATION)); | |
2330 } | |
2331 | |
2332 /* Returns the modify statement that uses NAME. Skips over assignment | |
2333 statements, NAME is replaced with the actual name used in the returned | |
2334 statement. */ | |
2335 | |
111 | 2336 static gimple * |
0 | 2337 find_use_stmt (tree *name) |
2338 { | |
111 | 2339 gimple *stmt; |
0 | 2340 tree rhs, lhs; |
2341 | |
2342 /* Skip over assignments. */ | |
2343 while (1) | |
2344 { | |
2345 stmt = single_nonlooparound_use (*name); | |
2346 if (!stmt) | |
2347 return NULL; | |
2348 | |
2349 if (gimple_code (stmt) != GIMPLE_ASSIGN) | |
2350 return NULL; | |
2351 | |
2352 lhs = gimple_assign_lhs (stmt); | |
2353 if (TREE_CODE (lhs) != SSA_NAME) | |
2354 return NULL; | |
2355 | |
2356 if (gimple_assign_copy_p (stmt)) | |
2357 { | |
2358 rhs = gimple_assign_rhs1 (stmt); | |
2359 if (rhs != *name) | |
2360 return NULL; | |
2361 | |
2362 *name = lhs; | |
2363 } | |
2364 else if (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)) | |
2365 == GIMPLE_BINARY_RHS) | |
2366 return stmt; | |
2367 else | |
2368 return NULL; | |
2369 } | |
2370 } | |
2371 | |
2372 /* Returns true if we may perform reassociation for operation CODE in TYPE. */ | |
2373 | |
2374 static bool | |
2375 may_reassociate_p (tree type, enum tree_code code) | |
2376 { | |
2377 if (FLOAT_TYPE_P (type) | |
2378 && !flag_unsafe_math_optimizations) | |
2379 return false; | |
2380 | |
2381 return (commutative_tree_code (code) | |
2382 && associative_tree_code (code)); | |
2383 } | |
2384 | |
2385 /* If the operation used in STMT is associative and commutative, go through the | |
2386 tree of the same operations and returns its root. Distance to the root | |
2387 is stored in DISTANCE. */ | |
2388 | |
111 | 2389 static gimple * |
2390 find_associative_operation_root (gimple *stmt, unsigned *distance) | |
0 | 2391 { |
2392 tree lhs; | |
111 | 2393 gimple *next; |
0 | 2394 enum tree_code code = gimple_assign_rhs_code (stmt); |
2395 tree type = TREE_TYPE (gimple_assign_lhs (stmt)); | |
2396 unsigned dist = 0; | |
2397 | |
2398 if (!may_reassociate_p (type, code)) | |
2399 return NULL; | |
2400 | |
2401 while (1) | |
2402 { | |
2403 lhs = gimple_assign_lhs (stmt); | |
2404 gcc_assert (TREE_CODE (lhs) == SSA_NAME); | |
2405 | |
2406 next = find_use_stmt (&lhs); | |
2407 if (!next | |
2408 || gimple_assign_rhs_code (next) != code) | |
2409 break; | |
2410 | |
2411 stmt = next; | |
2412 dist++; | |
2413 } | |
2414 | |
2415 if (distance) | |
2416 *distance = dist; | |
2417 return stmt; | |
2418 } | |
2419 | |
2420 /* Returns the common statement in that NAME1 and NAME2 have a use. If there | |
2421 is no such statement, returns NULL_TREE. In case the operation used on | |
2422 NAME1 and NAME2 is associative and commutative, returns the root of the | |
2423 tree formed by this operation instead of the statement that uses NAME1 or | |
2424 NAME2. */ | |
2425 | |
111 | 2426 static gimple * |
0 | 2427 find_common_use_stmt (tree *name1, tree *name2) |
2428 { | |
111 | 2429 gimple *stmt1, *stmt2; |
0 | 2430 |
2431 stmt1 = find_use_stmt (name1); | |
2432 if (!stmt1) | |
2433 return NULL; | |
2434 | |
2435 stmt2 = find_use_stmt (name2); | |
2436 if (!stmt2) | |
2437 return NULL; | |
2438 | |
2439 if (stmt1 == stmt2) | |
2440 return stmt1; | |
2441 | |
2442 stmt1 = find_associative_operation_root (stmt1, NULL); | |
2443 if (!stmt1) | |
2444 return NULL; | |
2445 stmt2 = find_associative_operation_root (stmt2, NULL); | |
2446 if (!stmt2) | |
2447 return NULL; | |
2448 | |
2449 return (stmt1 == stmt2 ? stmt1 : NULL); | |
2450 } | |
2451 | |
2452 /* Checks whether R1 and R2 are combined together using CODE, with the result | |
2453 in RSLT_TYPE, in order R1 CODE R2 if SWAP is false and in order R2 CODE R1 | |
2454 if it is true. If CODE is ERROR_MARK, set these values instead. */ | |
2455 | |
2456 static bool | |
2457 combinable_refs_p (dref r1, dref r2, | |
2458 enum tree_code *code, bool *swap, tree *rslt_type) | |
2459 { | |
2460 enum tree_code acode; | |
2461 bool aswap; | |
2462 tree atype; | |
2463 tree name1, name2; | |
111 | 2464 gimple *stmt; |
0 | 2465 |
2466 name1 = name_for_ref (r1); | |
2467 name2 = name_for_ref (r2); | |
2468 gcc_assert (name1 != NULL_TREE && name2 != NULL_TREE); | |
2469 | |
2470 stmt = find_common_use_stmt (&name1, &name2); | |
2471 | |
111 | 2472 if (!stmt |
2473 /* A simple post-dominance check - make sure the combination | |
2474 is executed under the same condition as the references. */ | |
2475 || (gimple_bb (stmt) != gimple_bb (r1->stmt) | |
2476 && gimple_bb (stmt) != gimple_bb (r2->stmt))) | |
0 | 2477 return false; |
2478 | |
2479 acode = gimple_assign_rhs_code (stmt); | |
2480 aswap = (!commutative_tree_code (acode) | |
2481 && gimple_assign_rhs1 (stmt) != name1); | |
2482 atype = TREE_TYPE (gimple_assign_lhs (stmt)); | |
2483 | |
2484 if (*code == ERROR_MARK) | |
2485 { | |
2486 *code = acode; | |
2487 *swap = aswap; | |
2488 *rslt_type = atype; | |
2489 return true; | |
2490 } | |
2491 | |
2492 return (*code == acode | |
2493 && *swap == aswap | |
2494 && *rslt_type == atype); | |
2495 } | |
2496 | |
2497 /* Remove OP from the operation on rhs of STMT, and replace STMT with | |
2498 an assignment of the remaining operand. */ | |
2499 | |
2500 static void | |
111 | 2501 remove_name_from_operation (gimple *stmt, tree op) |
0 | 2502 { |
2503 tree other_op; | |
2504 gimple_stmt_iterator si; | |
2505 | |
2506 gcc_assert (is_gimple_assign (stmt)); | |
2507 | |
2508 if (gimple_assign_rhs1 (stmt) == op) | |
2509 other_op = gimple_assign_rhs2 (stmt); | |
2510 else | |
2511 other_op = gimple_assign_rhs1 (stmt); | |
2512 | |
2513 si = gsi_for_stmt (stmt); | |
2514 gimple_assign_set_rhs_from_tree (&si, other_op); | |
2515 | |
2516 /* We should not have reallocated STMT. */ | |
2517 gcc_assert (gsi_stmt (si) == stmt); | |
2518 | |
2519 update_stmt (stmt); | |
2520 } | |
2521 | |
2522 /* Reassociates the expression in that NAME1 and NAME2 are used so that they | |
111 | 2523 are combined in a single statement, and returns this statement. Note the |
2524 statement is inserted before INSERT_BEFORE if it's not NULL. */ | |
2525 | |
2526 static gimple * | |
2527 reassociate_to_the_same_stmt (tree name1, tree name2, gimple *insert_before) | |
0 | 2528 { |
111 | 2529 gimple *stmt1, *stmt2, *root1, *root2, *s1, *s2; |
2530 gassign *new_stmt, *tmp_stmt; | |
0 | 2531 tree new_name, tmp_name, var, r1, r2; |
2532 unsigned dist1, dist2; | |
2533 enum tree_code code; | |
2534 tree type = TREE_TYPE (name1); | |
2535 gimple_stmt_iterator bsi; | |
2536 | |
2537 stmt1 = find_use_stmt (&name1); | |
2538 stmt2 = find_use_stmt (&name2); | |
2539 root1 = find_associative_operation_root (stmt1, &dist1); | |
2540 root2 = find_associative_operation_root (stmt2, &dist2); | |
2541 code = gimple_assign_rhs_code (stmt1); | |
2542 | |
2543 gcc_assert (root1 && root2 && root1 == root2 | |
2544 && code == gimple_assign_rhs_code (stmt2)); | |
2545 | |
2546 /* Find the root of the nearest expression in that both NAME1 and NAME2 | |
2547 are used. */ | |
2548 r1 = name1; | |
2549 s1 = stmt1; | |
2550 r2 = name2; | |
2551 s2 = stmt2; | |
2552 | |
2553 while (dist1 > dist2) | |
2554 { | |
2555 s1 = find_use_stmt (&r1); | |
2556 r1 = gimple_assign_lhs (s1); | |
2557 dist1--; | |
2558 } | |
2559 while (dist2 > dist1) | |
2560 { | |
2561 s2 = find_use_stmt (&r2); | |
2562 r2 = gimple_assign_lhs (s2); | |
2563 dist2--; | |
2564 } | |
2565 | |
2566 while (s1 != s2) | |
2567 { | |
2568 s1 = find_use_stmt (&r1); | |
2569 r1 = gimple_assign_lhs (s1); | |
2570 s2 = find_use_stmt (&r2); | |
2571 r2 = gimple_assign_lhs (s2); | |
2572 } | |
2573 | |
2574 /* Remove NAME1 and NAME2 from the statements in that they are used | |
2575 currently. */ | |
2576 remove_name_from_operation (stmt1, name1); | |
2577 remove_name_from_operation (stmt2, name2); | |
2578 | |
2579 /* Insert the new statement combining NAME1 and NAME2 before S1, and | |
2580 combine it with the rhs of S1. */ | |
63
b7f97abdc517
update gcc from gcc-4.5.0 to gcc-4.6
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
2581 var = create_tmp_reg (type, "predreastmp"); |
111 | 2582 new_name = make_ssa_name (var); |
2583 new_stmt = gimple_build_assign (new_name, code, name1, name2); | |
2584 if (insert_before && stmt_dominates_stmt_p (insert_before, s1)) | |
2585 bsi = gsi_for_stmt (insert_before); | |
2586 else | |
2587 bsi = gsi_for_stmt (s1); | |
2588 | |
2589 gsi_insert_before (&bsi, new_stmt, GSI_SAME_STMT); | |
0 | 2590 |
63
b7f97abdc517
update gcc from gcc-4.5.0 to gcc-4.6
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
2591 var = create_tmp_reg (type, "predreastmp"); |
111 | 2592 tmp_name = make_ssa_name (var); |
0 | 2593 |
2594 /* Rhs of S1 may now be either a binary expression with operation | |
2595 CODE, or gimple_val (in case that stmt1 == s1 or stmt2 == s1, | |
2596 so that name1 or name2 was removed from it). */ | |
111 | 2597 tmp_stmt = gimple_build_assign (tmp_name, gimple_assign_rhs_code (s1), |
2598 gimple_assign_rhs1 (s1), | |
2599 gimple_assign_rhs2 (s1)); | |
0 | 2600 |
2601 bsi = gsi_for_stmt (s1); | |
2602 gimple_assign_set_rhs_with_ops (&bsi, code, new_name, tmp_name); | |
2603 s1 = gsi_stmt (bsi); | |
2604 update_stmt (s1); | |
2605 | |
2606 gsi_insert_before (&bsi, tmp_stmt, GSI_SAME_STMT); | |
2607 | |
2608 return new_stmt; | |
2609 } | |
2610 | |
2611 /* Returns the statement that combines references R1 and R2. In case R1 | |
2612 and R2 are not used in the same statement, but they are used with an | |
2613 associative and commutative operation in the same expression, reassociate | |
111 | 2614 the expression so that they are used in the same statement. The combined |
2615 statement is inserted before INSERT_BEFORE if it's not NULL. */ | |
2616 | |
2617 static gimple * | |
2618 stmt_combining_refs (dref r1, dref r2, gimple *insert_before) | |
0 | 2619 { |
111 | 2620 gimple *stmt1, *stmt2; |
0 | 2621 tree name1 = name_for_ref (r1); |
2622 tree name2 = name_for_ref (r2); | |
2623 | |
2624 stmt1 = find_use_stmt (&name1); | |
2625 stmt2 = find_use_stmt (&name2); | |
2626 if (stmt1 == stmt2) | |
2627 return stmt1; | |
2628 | |
111 | 2629 return reassociate_to_the_same_stmt (name1, name2, insert_before); |
0 | 2630 } |
2631 | |
2632 /* Tries to combine chains CH1 and CH2 together. If this succeeds, the | |
2633 description of the new chain is returned, otherwise we return NULL. */ | |
2634 | |
2635 static chain_p | |
2636 combine_chains (chain_p ch1, chain_p ch2) | |
2637 { | |
2638 dref r1, r2, nw; | |
2639 enum tree_code op = ERROR_MARK; | |
2640 bool swap = false; | |
2641 chain_p new_chain; | |
111 | 2642 int i, j, num; |
2643 gimple *root_stmt; | |
0 | 2644 tree rslt_type = NULL_TREE; |
2645 | |
2646 if (ch1 == ch2) | |
63
b7f97abdc517
update gcc from gcc-4.5.0 to gcc-4.6
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
2647 return NULL; |
0 | 2648 if (ch1->length != ch2->length) |
2649 return NULL; | |
2650 | |
111 | 2651 if (ch1->refs.length () != ch2->refs.length ()) |
0 | 2652 return NULL; |
2653 | |
111 | 2654 for (i = 0; (ch1->refs.iterate (i, &r1) |
2655 && ch2->refs.iterate (i, &r2)); i++) | |
0 | 2656 { |
2657 if (r1->distance != r2->distance) | |
2658 return NULL; | |
2659 | |
2660 if (!combinable_refs_p (r1, r2, &op, &swap, &rslt_type)) | |
2661 return NULL; | |
2662 } | |
2663 | |
111 | 2664 ch1->combined = true; |
2665 ch2->combined = true; | |
2666 | |
0 | 2667 if (swap) |
111 | 2668 std::swap (ch1, ch2); |
0 | 2669 |
2670 new_chain = XCNEW (struct chain); | |
2671 new_chain->type = CT_COMBINATION; | |
2672 new_chain->op = op; | |
2673 new_chain->ch1 = ch1; | |
2674 new_chain->ch2 = ch2; | |
2675 new_chain->rslt_type = rslt_type; | |
2676 new_chain->length = ch1->length; | |
2677 | |
111 | 2678 gimple *insert = NULL; |
2679 num = ch1->refs.length (); | |
2680 i = (new_chain->length == 0) ? num - 1 : 0; | |
2681 j = (new_chain->length == 0) ? -1 : 1; | |
2682 /* For ZERO length chain, process refs in reverse order so that dominant | |
2683 position is ready when it comes to the root ref. | |
2684 For non-ZERO length chain, process refs in order. See PR79663. */ | |
2685 for (; num > 0; num--, i += j) | |
0 | 2686 { |
111 | 2687 r1 = ch1->refs[i]; |
2688 r2 = ch2->refs[i]; | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
2689 nw = XCNEW (struct dref_d); |
0 | 2690 nw->distance = r1->distance; |
2691 | |
111 | 2692 /* For ZERO length chain, insert combined stmt of root ref at dominant |
2693 position. */ | |
2694 nw->stmt = stmt_combining_refs (r1, r2, i == 0 ? insert : NULL); | |
2695 /* For ZERO length chain, record dominant position where combined stmt | |
2696 of root ref should be inserted. In this case, though all root refs | |
2697 dominate following ones, it's possible that combined stmt doesn't. | |
2698 See PR70754. */ | |
2699 if (new_chain->length == 0 | |
2700 && (insert == NULL || stmt_dominates_stmt_p (nw->stmt, insert))) | |
2701 insert = nw->stmt; | |
2702 | |
2703 new_chain->refs.safe_push (nw); | |
2704 } | |
2705 if (new_chain->length == 0) | |
2706 { | |
2707 /* Restore the order for ZERO length chain's refs. */ | |
2708 num = new_chain->refs.length () >> 1; | |
2709 for (i = 0, j = new_chain->refs.length () - 1; i < num; i++, j--) | |
2710 std::swap (new_chain->refs[i], new_chain->refs[j]); | |
2711 | |
2712 /* For ZERO length chain, has_max_use_after must be true since root | |
2713 combined stmt must dominates others. */ | |
2714 new_chain->has_max_use_after = true; | |
2715 return new_chain; | |
0 | 2716 } |
2717 | |
2718 new_chain->has_max_use_after = false; | |
2719 root_stmt = get_chain_root (new_chain)->stmt; | |
111 | 2720 for (i = 1; new_chain->refs.iterate (i, &nw); i++) |
0 | 2721 { |
2722 if (nw->distance == new_chain->length | |
2723 && !stmt_dominates_stmt_p (nw->stmt, root_stmt)) | |
2724 { | |
2725 new_chain->has_max_use_after = true; | |
2726 break; | |
2727 } | |
2728 } | |
2729 | |
2730 return new_chain; | |
2731 } | |
2732 | |
2733 /* Try to combine the CHAINS. */ | |
2734 | |
2735 static void | |
111 | 2736 try_combine_chains (vec<chain_p> *chains) |
0 | 2737 { |
2738 unsigned i, j; | |
2739 chain_p ch1, ch2, cch; | |
111 | 2740 auto_vec<chain_p> worklist; |
2741 | |
2742 FOR_EACH_VEC_ELT (*chains, i, ch1) | |
0 | 2743 if (chain_can_be_combined_p (ch1)) |
111 | 2744 worklist.safe_push (ch1); |
2745 | |
2746 while (!worklist.is_empty ()) | |
0 | 2747 { |
111 | 2748 ch1 = worklist.pop (); |
0 | 2749 if (!chain_can_be_combined_p (ch1)) |
2750 continue; | |
2751 | |
111 | 2752 FOR_EACH_VEC_ELT (*chains, j, ch2) |
0 | 2753 { |
2754 if (!chain_can_be_combined_p (ch2)) | |
2755 continue; | |
2756 | |
2757 cch = combine_chains (ch1, ch2); | |
2758 if (cch) | |
2759 { | |
111 | 2760 worklist.safe_push (cch); |
2761 chains->safe_push (cch); | |
0 | 2762 break; |
2763 } | |
2764 } | |
2765 } | |
2766 } | |
2767 | |
111 | 2768 /* Prepare initializers for store elimination CHAIN in LOOP. Returns false |
2769 if this is impossible because one of these initializers may trap, true | |
2770 otherwise. */ | |
2771 | |
2772 static bool | |
2773 prepare_initializers_chain_store_elim (struct loop *loop, chain_p chain) | |
2774 { | |
2775 unsigned i, n = chain->length; | |
2776 | |
2777 /* For now we can't eliminate stores if some of them are conditional | |
2778 executed. */ | |
2779 if (!chain->all_always_accessed) | |
2780 return false; | |
2781 | |
2782 /* Nothing to intialize for intra-iteration store elimination. */ | |
2783 if (n == 0 && chain->type == CT_STORE_STORE) | |
2784 return true; | |
2785 | |
2786 /* For store elimination chain, there is nothing to initialize if stores | |
2787 to be eliminated only store loop invariant values into memory. */ | |
2788 if (chain->type == CT_STORE_STORE | |
2789 && is_inv_store_elimination_chain (loop, chain)) | |
2790 { | |
2791 chain->inv_store_elimination = true; | |
2792 return true; | |
2793 } | |
2794 | |
2795 chain->inits.create (n); | |
2796 chain->inits.safe_grow_cleared (n); | |
2797 | |
2798 /* For store eliminatin chain like below: | |
2799 | |
2800 for (i = 0; i < len; i++) | |
2801 { | |
2802 a[i] = 1; | |
2803 // a[i + 1] = ... | |
2804 a[i + 2] = 3; | |
2805 } | |
2806 | |
2807 store to a[i + 1] is missed in loop body, it acts like bubbles. The | |
2808 content of a[i + 1] remain the same if the loop iterates fewer times | |
2809 than chain->length. We need to set up root variables for such stores | |
2810 by loading from memory before loop. Note we only need to load bubble | |
2811 elements because loop body is guaranteed to be executed at least once | |
2812 after loop's preheader edge. */ | |
2813 auto_vec<bool> bubbles; | |
2814 bubbles.safe_grow_cleared (n + 1); | |
2815 for (i = 0; i < chain->refs.length (); i++) | |
2816 bubbles[chain->refs[i]->distance] = true; | |
2817 | |
2818 struct data_reference *dr = get_chain_root (chain)->ref; | |
2819 for (i = 0; i < n; i++) | |
2820 { | |
2821 if (bubbles[i]) | |
2822 continue; | |
2823 | |
2824 gimple_seq stmts = NULL; | |
2825 | |
2826 tree init = ref_at_iteration (dr, (int) 0 - i, &stmts); | |
2827 if (stmts) | |
2828 gimple_seq_add_seq_without_update (&chain->init_seq, stmts); | |
2829 | |
2830 chain->inits[i] = init; | |
2831 } | |
2832 | |
2833 return true; | |
2834 } | |
2835 | |
0 | 2836 /* Prepare initializers for CHAIN in LOOP. Returns false if this is |
2837 impossible because one of these initializers may trap, true otherwise. */ | |
2838 | |
2839 static bool | |
2840 prepare_initializers_chain (struct loop *loop, chain_p chain) | |
2841 { | |
2842 unsigned i, n = (chain->type == CT_INVARIANT) ? 1 : chain->length; | |
2843 struct data_reference *dr = get_chain_root (chain)->ref; | |
2844 tree init; | |
2845 dref laref; | |
2846 edge entry = loop_preheader_edge (loop); | |
2847 | |
111 | 2848 if (chain->type == CT_STORE_STORE) |
2849 return prepare_initializers_chain_store_elim (loop, chain); | |
2850 | |
0 | 2851 /* Find the initializers for the variables, and check that they cannot |
2852 trap. */ | |
111 | 2853 chain->inits.create (n); |
0 | 2854 for (i = 0; i < n; i++) |
111 | 2855 chain->inits.quick_push (NULL_TREE); |
0 | 2856 |
2857 /* If we have replaced some looparound phi nodes, use their initializers | |
2858 instead of creating our own. */ | |
111 | 2859 FOR_EACH_VEC_ELT (chain->refs, i, laref) |
0 | 2860 { |
2861 if (gimple_code (laref->stmt) != GIMPLE_PHI) | |
2862 continue; | |
2863 | |
2864 gcc_assert (laref->distance > 0); | |
111 | 2865 chain->inits[n - laref->distance] |
2866 = PHI_ARG_DEF_FROM_EDGE (laref->stmt, entry); | |
0 | 2867 } |
2868 | |
2869 for (i = 0; i < n; i++) | |
2870 { | |
111 | 2871 gimple_seq stmts = NULL; |
2872 | |
2873 if (chain->inits[i] != NULL_TREE) | |
0 | 2874 continue; |
2875 | |
111 | 2876 init = ref_at_iteration (dr, (int) i - n, &stmts); |
0 | 2877 if (!chain->all_always_accessed && tree_could_trap_p (init)) |
111 | 2878 { |
2879 gimple_seq_discard (stmts); | |
2880 return false; | |
2881 } | |
2882 | |
0 | 2883 if (stmts) |
111 | 2884 gimple_seq_add_seq_without_update (&chain->init_seq, stmts); |
2885 | |
2886 chain->inits[i] = init; | |
0 | 2887 } |
2888 | |
2889 return true; | |
2890 } | |
2891 | |
2892 /* Prepare initializers for CHAINS in LOOP, and free chains that cannot | |
2893 be used because the initializers might trap. */ | |
2894 | |
2895 static void | |
111 | 2896 prepare_initializers (struct loop *loop, vec<chain_p> chains) |
0 | 2897 { |
2898 chain_p chain; | |
2899 unsigned i; | |
2900 | |
111 | 2901 for (i = 0; i < chains.length (); ) |
0 | 2902 { |
111 | 2903 chain = chains[i]; |
0 | 2904 if (prepare_initializers_chain (loop, chain)) |
2905 i++; | |
2906 else | |
2907 { | |
2908 release_chain (chain); | |
111 | 2909 chains.unordered_remove (i); |
0 | 2910 } |
2911 } | |
2912 } | |
2913 | |
111 | 2914 /* Generates finalizer memory references for CHAIN in LOOP. Returns true |
2915 if finalizer code for CHAIN can be generated, otherwise false. */ | |
2916 | |
2917 static bool | |
2918 prepare_finalizers_chain (struct loop *loop, chain_p chain) | |
2919 { | |
2920 unsigned i, n = chain->length; | |
2921 struct data_reference *dr = get_chain_root (chain)->ref; | |
2922 tree fini, niters = number_of_latch_executions (loop); | |
2923 | |
2924 /* For now we can't eliminate stores if some of them are conditional | |
2925 executed. */ | |
2926 if (!chain->all_always_accessed) | |
2927 return false; | |
2928 | |
2929 chain->finis.create (n); | |
2930 for (i = 0; i < n; i++) | |
2931 chain->finis.quick_push (NULL_TREE); | |
2932 | |
2933 /* We never use looparound phi node for store elimination chains. */ | |
2934 | |
2935 /* Find the finalizers for the variables, and check that they cannot | |
2936 trap. */ | |
2937 for (i = 0; i < n; i++) | |
2938 { | |
2939 gimple_seq stmts = NULL; | |
2940 gcc_assert (chain->finis[i] == NULL_TREE); | |
2941 | |
2942 if (TREE_CODE (niters) != INTEGER_CST && TREE_CODE (niters) != SSA_NAME) | |
2943 { | |
2944 niters = unshare_expr (niters); | |
2945 niters = force_gimple_operand (niters, &stmts, true, NULL); | |
2946 if (stmts) | |
2947 { | |
2948 gimple_seq_add_seq_without_update (&chain->fini_seq, stmts); | |
2949 stmts = NULL; | |
2950 } | |
2951 } | |
2952 fini = ref_at_iteration (dr, (int) 0 - i, &stmts, niters); | |
2953 if (stmts) | |
2954 gimple_seq_add_seq_without_update (&chain->fini_seq, stmts); | |
2955 | |
2956 chain->finis[i] = fini; | |
2957 } | |
2958 | |
2959 return true; | |
2960 } | |
2961 | |
2962 /* Generates finalizer memory reference for CHAINS in LOOP. Returns true | |
2963 if finalizer code generation for CHAINS breaks loop closed ssa form. */ | |
0 | 2964 |
2965 static bool | |
111 | 2966 prepare_finalizers (struct loop *loop, vec<chain_p> chains) |
2967 { | |
2968 chain_p chain; | |
2969 unsigned i; | |
2970 bool loop_closed_ssa = false; | |
2971 | |
2972 for (i = 0; i < chains.length ();) | |
2973 { | |
2974 chain = chains[i]; | |
2975 | |
2976 /* Finalizer is only necessary for inter-iteration store elimination | |
2977 chains. */ | |
2978 if (chain->length == 0 || chain->type != CT_STORE_STORE) | |
2979 { | |
2980 i++; | |
2981 continue; | |
2982 } | |
2983 | |
2984 if (prepare_finalizers_chain (loop, chain)) | |
2985 { | |
2986 i++; | |
2987 /* Be conservative, assume loop closed ssa form is corrupted | |
2988 by store-store chain. Though it's not always the case if | |
2989 eliminated stores only store loop invariant values into | |
2990 memory. */ | |
2991 loop_closed_ssa = true; | |
2992 } | |
2993 else | |
2994 { | |
2995 release_chain (chain); | |
2996 chains.unordered_remove (i); | |
2997 } | |
2998 } | |
2999 return loop_closed_ssa; | |
3000 } | |
3001 | |
3002 /* Insert all initializing gimple stmts into loop's entry edge. */ | |
3003 | |
3004 static void | |
3005 insert_init_seqs (struct loop *loop, vec<chain_p> chains) | |
3006 { | |
3007 unsigned i; | |
3008 edge entry = loop_preheader_edge (loop); | |
3009 | |
3010 for (i = 0; i < chains.length (); ++i) | |
3011 if (chains[i]->init_seq) | |
3012 { | |
3013 gsi_insert_seq_on_edge_immediate (entry, chains[i]->init_seq); | |
3014 chains[i]->init_seq = NULL; | |
3015 } | |
3016 } | |
3017 | |
3018 /* Performs predictive commoning for LOOP. Sets bit 1<<0 of return value | |
3019 if LOOP was unrolled; Sets bit 1<<1 of return value if loop closed ssa | |
3020 form was corrupted. */ | |
3021 | |
3022 static unsigned | |
0 | 3023 tree_predictive_commoning_loop (struct loop *loop) |
3024 { | |
111 | 3025 vec<data_reference_p> datarefs; |
3026 vec<ddr_p> dependences; | |
0 | 3027 struct component *components; |
111 | 3028 vec<chain_p> chains = vNULL; |
0 | 3029 unsigned unroll_factor; |
3030 struct tree_niter_desc desc; | |
111 | 3031 bool unroll = false, loop_closed_ssa = false; |
0 | 3032 edge exit; |
3033 | |
3034 if (dump_file && (dump_flags & TDF_DETAILS)) | |
3035 fprintf (dump_file, "Processing loop %d\n", loop->num); | |
3036 | |
111 | 3037 /* Nothing for predicitive commoning if loop only iterates 1 time. */ |
3038 if (get_max_loop_iterations_int (loop) == 0) | |
3039 { | |
3040 if (dump_file && (dump_flags & TDF_DETAILS)) | |
3041 fprintf (dump_file, "Loop iterates only 1 time, nothing to do.\n"); | |
3042 | |
3043 return 0; | |
3044 } | |
3045 | |
0 | 3046 /* Find the data references and split them into components according to their |
3047 dependence relations. */ | |
111 | 3048 auto_vec<loop_p, 3> loop_nest; |
3049 dependences.create (10); | |
3050 datarefs.create (10); | |
3051 if (! compute_data_dependences_for_loop (loop, true, &loop_nest, &datarefs, | |
3052 &dependences)) | |
3053 { | |
3054 if (dump_file && (dump_flags & TDF_DETAILS)) | |
3055 fprintf (dump_file, "Cannot analyze data dependencies\n"); | |
3056 free_data_refs (datarefs); | |
3057 free_dependence_relations (dependences); | |
3058 return 0; | |
3059 } | |
3060 | |
0 | 3061 if (dump_file && (dump_flags & TDF_DETAILS)) |
3062 dump_data_dependence_relations (dump_file, dependences); | |
3063 | |
3064 components = split_data_refs_to_components (loop, datarefs, dependences); | |
111 | 3065 loop_nest.release (); |
0 | 3066 free_dependence_relations (dependences); |
3067 if (!components) | |
3068 { | |
3069 free_data_refs (datarefs); | |
111 | 3070 free_affine_expand_cache (&name_expansions); |
3071 return 0; | |
0 | 3072 } |
3073 | |
3074 if (dump_file && (dump_flags & TDF_DETAILS)) | |
3075 { | |
3076 fprintf (dump_file, "Initial state:\n\n"); | |
3077 dump_components (dump_file, components); | |
3078 } | |
3079 | |
3080 /* Find the suitable components and split them into chains. */ | |
3081 components = filter_suitable_components (loop, components); | |
3082 | |
111 | 3083 auto_bitmap tmp_vars; |
0 | 3084 looparound_phis = BITMAP_ALLOC (NULL); |
3085 determine_roots (loop, components, &chains); | |
3086 release_components (components); | |
3087 | |
111 | 3088 if (!chains.exists ()) |
0 | 3089 { |
3090 if (dump_file && (dump_flags & TDF_DETAILS)) | |
3091 fprintf (dump_file, | |
3092 "Predictive commoning failed: no suitable chains\n"); | |
3093 goto end; | |
3094 } | |
3095 prepare_initializers (loop, chains); | |
111 | 3096 loop_closed_ssa = prepare_finalizers (loop, chains); |
0 | 3097 |
3098 /* Try to combine the chains that are always worked with together. */ | |
3099 try_combine_chains (&chains); | |
3100 | |
111 | 3101 insert_init_seqs (loop, chains); |
3102 | |
0 | 3103 if (dump_file && (dump_flags & TDF_DETAILS)) |
3104 { | |
3105 fprintf (dump_file, "Before commoning:\n\n"); | |
3106 dump_chains (dump_file, chains); | |
3107 } | |
3108 | |
3109 /* Determine the unroll factor, and if the loop should be unrolled, ensure | |
3110 that its number of iterations is divisible by the factor. */ | |
3111 unroll_factor = determine_unroll_factor (chains); | |
3112 scev_reset (); | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
3113 unroll = (unroll_factor > 1 |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
3114 && can_unroll_loop_p (loop, unroll_factor, &desc)); |
0 | 3115 exit = single_dom_exit (loop); |
3116 | |
3117 /* Execute the predictive commoning transformations, and possibly unroll the | |
3118 loop. */ | |
3119 if (unroll) | |
3120 { | |
3121 struct epcc_data dta; | |
3122 | |
3123 if (dump_file && (dump_flags & TDF_DETAILS)) | |
3124 fprintf (dump_file, "Unrolling %u times.\n", unroll_factor); | |
3125 | |
3126 dta.chains = chains; | |
3127 dta.tmp_vars = tmp_vars; | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
3128 |
0 | 3129 update_ssa (TODO_update_ssa_only_virtuals); |
3130 | |
3131 /* Cfg manipulations performed in tree_transform_and_unroll_loop before | |
3132 execute_pred_commoning_cbck is called may cause phi nodes to be | |
3133 reallocated, which is a problem since CHAINS may point to these | |
3134 statements. To fix this, we store the ssa names defined by the | |
3135 phi nodes here instead of the phi nodes themselves, and restore | |
3136 the phi nodes in execute_pred_commoning_cbck. A bit hacky. */ | |
3137 replace_phis_by_defined_names (chains); | |
3138 | |
3139 tree_transform_and_unroll_loop (loop, unroll_factor, exit, &desc, | |
3140 execute_pred_commoning_cbck, &dta); | |
3141 eliminate_temp_copies (loop, tmp_vars); | |
3142 } | |
3143 else | |
3144 { | |
3145 if (dump_file && (dump_flags & TDF_DETAILS)) | |
3146 fprintf (dump_file, | |
3147 "Executing predictive commoning without unrolling.\n"); | |
3148 execute_pred_commoning (loop, chains, tmp_vars); | |
3149 } | |
3150 | |
3151 end: ; | |
3152 release_chains (chains); | |
3153 free_data_refs (datarefs); | |
3154 BITMAP_FREE (looparound_phis); | |
3155 | |
3156 free_affine_expand_cache (&name_expansions); | |
3157 | |
111 | 3158 return (unroll ? 1 : 0) | (loop_closed_ssa ? 2 : 0); |
0 | 3159 } |
3160 | |
3161 /* Runs predictive commoning. */ | |
3162 | |
3163 unsigned | |
3164 tree_predictive_commoning (void) | |
3165 { | |
3166 struct loop *loop; | |
111 | 3167 unsigned ret = 0, changed = 0; |
0 | 3168 |
3169 initialize_original_copy_tables (); | |
111 | 3170 FOR_EACH_LOOP (loop, LI_ONLY_INNERMOST) |
0 | 3171 if (optimize_loop_for_speed_p (loop)) |
3172 { | |
111 | 3173 changed |= tree_predictive_commoning_loop (loop); |
0 | 3174 } |
111 | 3175 free_original_copy_tables (); |
3176 | |
3177 if (changed > 0) | |
0 | 3178 { |
3179 scev_reset (); | |
111 | 3180 |
3181 if (changed > 1) | |
3182 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa); | |
3183 | |
0 | 3184 ret = TODO_cleanup_cfg; |
3185 } | |
3186 | |
3187 return ret; | |
3188 } | |
111 | 3189 |
3190 /* Predictive commoning Pass. */ | |
3191 | |
3192 static unsigned | |
3193 run_tree_predictive_commoning (struct function *fun) | |
3194 { | |
3195 if (number_of_loops (fun) <= 1) | |
3196 return 0; | |
3197 | |
3198 return tree_predictive_commoning (); | |
3199 } | |
3200 | |
3201 namespace { | |
3202 | |
3203 const pass_data pass_data_predcom = | |
3204 { | |
3205 GIMPLE_PASS, /* type */ | |
3206 "pcom", /* name */ | |
3207 OPTGROUP_LOOP, /* optinfo_flags */ | |
3208 TV_PREDCOM, /* tv_id */ | |
3209 PROP_cfg, /* properties_required */ | |
3210 0, /* properties_provided */ | |
3211 0, /* properties_destroyed */ | |
3212 0, /* todo_flags_start */ | |
3213 TODO_update_ssa_only_virtuals, /* todo_flags_finish */ | |
3214 }; | |
3215 | |
3216 class pass_predcom : public gimple_opt_pass | |
3217 { | |
3218 public: | |
3219 pass_predcom (gcc::context *ctxt) | |
3220 : gimple_opt_pass (pass_data_predcom, ctxt) | |
3221 {} | |
3222 | |
3223 /* opt_pass methods: */ | |
3224 virtual bool gate (function *) { return flag_predictive_commoning != 0; } | |
3225 virtual unsigned int execute (function *fun) | |
3226 { | |
3227 return run_tree_predictive_commoning (fun); | |
3228 } | |
3229 | |
3230 }; // class pass_predcom | |
3231 | |
3232 } // anon namespace | |
3233 | |
3234 gimple_opt_pass * | |
3235 make_pass_predcom (gcc::context *ctxt) | |
3236 { | |
3237 return new pass_predcom (ctxt); | |
3238 } | |
3239 | |
3240 |