OSDN Git Service

* cfglayout.c, cgraphunit.c, config/avr/avr.c, fold-const.c,
[pf3gnuchains/gcc-fork.git] / gcc / tree-predcom.c
1 /* Predictive commoning.
2    Copyright (C) 2005 Free Software Foundation, Inc.
3    
4 This file is part of GCC.
5    
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 2, or (at your option) any
9 later version.
10    
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.
15    
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING.  If not, write to the Free
18 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, USA.  */
20
21 /* This file implements the predictive commoning optimization.  Predictive
22    commoning can be viewed as CSE around a loop, and with some improvements,
23    as generalized strength reduction-- i.e., reusing values computed in
24    earlier iterations of a loop in the later ones.  So far, the pass only
25    handles the most useful case, that is, reusing values of memory references.
26    If you think this is all just a special case of PRE, you are sort of right;
27    however, concentrating on loops is simpler, and makes it possible to
28    incorporate data dependence analysis to detect the opportunities, perform
29    loop unrolling to avoid copies together with renaming immediately,
30    and if needed, we could also take register pressure into account.
31
32    Let us demonstrate what is done on an example:
33    
34    for (i = 0; i < 100; i++)
35      {
36        a[i+2] = a[i] + a[i+1];
37        b[10] = b[10] + i;
38        c[i] = c[99 - i];
39        d[i] = d[i + 1];
40      }
41
42    1) We find data references in the loop, and split them to mutually
43       independent groups (i.e., we find components of a data dependence
44       graph).  We ignore read-read dependences whose distance is not constant.
45       (TODO -- we could also ignore antidependences).  In this example, we
46       find the following groups:
47
48       a[i]{read}, a[i+1]{read}, a[i+2]{write}
49       b[10]{read}, b[10]{write}
50       c[99 - i]{read}, c[i]{write}
51       d[i + 1]{read}, d[i]{write}
52
53    2) Inside each of the group, we verify several conditions:
54       a) all the references must differ in indices only, and the indices
55          must all have the same step
56       b) the references must dominate loop latch (and thus, they must be
57          ordered by dominance relation).
58       c) the distance of the indices must be a small multiple of the step
59       We are then able to compute the difference of the references (# of
60       iterations before they point to the same place as the first of them).
61       Also, in case there are writes in the loop, we split the groups into
62       chains whose head is the write whose values are used by the reads in
63       the same chain.  The chains are then processed independently,
64       making the further transformations simpler.  Also, the shorter chains
65       need the same number of registers, but may require lower unrolling
66       factor in order to get rid of the copies on the loop latch.
67       
68       In our example, we get the following chains (the chain for c is invalid).
69
70       a[i]{read,+0}, a[i+1]{read,-1}, a[i+2]{write,-2}
71       b[10]{read,+0}, b[10]{write,+0}
72       d[i + 1]{read,+0}, d[i]{write,+1}
73
74    3) For each read, we determine the read or write whose value it reuses,
75       together with the distance of this reuse.  I.e. we take the last
76       reference before it with distance 0, or the last of the references
77       with the smallest positive distance to the read.  Then, we remove
78       the references that are not used in any of these chains, discard the
79       empty groups, and propagate all the links so that they point to the
80       single root reference of the chain (adjusting their distance 
81       appropriately).  Some extra care needs to be taken for references with
82       step 0.  In our example (the numbers indicate the distance of the
83       reuse),
84
85       a[i] --> (*) 2, a[i+1] --> (*) 1, a[i+2] (*)
86       b[10] --> (*) 1, b[10] (*)
87
88    4) The chains are combined together if possible.  If the corresponding
89       elements of two chains are always combined together with the same
90       operator, we remember just the result of this combination, instead
91       of remembering the values separately.  We may need to perform
92       reassociation to enable combining, for example
93
94       e[i] + f[i+1] + e[i+1] + f[i]
95
96       can be reassociated as
97
98       (e[i] + f[i]) + (e[i+1] + f[i+1])
99
100       and we can combine the chains for e and f into one chain.
101
102    5) For each root reference (end of the chain) R, let N be maximum distance
103       of a reference reusing its value.  Variables R0 upto RN are created,
104       together with phi nodes that transfer values from R1 .. RN to
105       R0 .. R(N-1).
106       Initial values are loaded to R0..R(N-1) (in case not all references
107       must necessarily be accessed and they may trap, we may fail here;
108       TODO sometimes, the loads could be guarded by a check for the number
109       of iterations).  Values loaded/stored in roots are also copied to
110       RN.  Other reads are replaced with the appropriate variable Ri.
111       Everything is put to SSA form.
112
113       As a small improvement, if R0 is dead after the root (i.e., all uses of
114       the value with the maximum distance dominate the root), we can avoid
115       creating RN and use R0 instead of it.
116
117       In our example, we get (only the parts concerning a and b are shown):
118       for (i = 0; i < 100; i++)
119         {
120           f = phi (a[0], s);
121           s = phi (a[1], f);
122           x = phi (b[10], x);
123
124           f = f + s;
125           a[i+2] = f;
126           x = x + i;
127           b[10] = x;
128         }
129
130    6) Factor F for unrolling is determined as the smallest common multiple of
131       (N + 1) for each root reference (N for references for that we avoided
132       creating RN).  If F and the loop is small enough, loop is unrolled F
133       times.  The stores to RN (R0) in the copies of the loop body are
134       periodically replaced with R0, R1, ... (R1, R2, ...), so that they can
135       be coalesced and the copies can be eliminated.
136       
137       TODO -- copy propagation and other optimizations may change the live
138       ranges of the temporary registers and prevent them from being coalesced;
139       this may increase the register pressure.
140
141       In our case, F = 2 and the (main loop of the) result is
142
143       for (i = 0; i < ...; i += 2)
144         {
145           f = phi (a[0], f);
146           s = phi (a[1], s);
147           x = phi (b[10], x);
148
149           f = f + s;
150           a[i+2] = f;
151           x = x + i;
152           b[10] = x;
153
154           s = s + f;
155           a[i+3] = s;
156           x = x + i;
157           b[10] = x;
158        }
159
160    TODO -- stores killing other stores can be taken into account, e.g.,
161    for (i = 0; i < n; i++)
162      {
163        a[i] = 1;
164        a[i+2] = 2;
165      }
166
167    can be replaced with
168
169    t0 = a[0];
170    t1 = a[1];
171    for (i = 0; i < n; i++)
172      {
173        a[i] = 1;
174        t2 = 2;
175        t0 = t1;
176        t1 = t2;
177      }
178    a[n] = t0;
179    a[n+1] = t1;
180
181    The interesting part is that this would generalize store motion; still, since
182    sm is performed elsewhere, it does not seem that important.
183
184    Predictive commoning can be generalized for arbitrary computations (not
185    just memory loads), and also nontrivial transfer functions (e.g., replacing
186    i * i with ii_last + 2 * i + 1), to generalize strength reduction.  */
187
188 #include "config.h"
189 #include "system.h"
190 #include "coretypes.h"
191 #include "tm.h"
192 #include "tree.h"
193 #include "tm_p.h"
194 #include "cfgloop.h"
195 #include "tree-flow.h"
196 #include "ggc.h"
197 #include "tree-data-ref.h"
198 #include "tree-scalar-evolution.h"
199 #include "tree-chrec.h"
200 #include "params.h"
201 #include "diagnostic.h"
202 #include "tree-pass.h"
203 #include "tree-affine.h"
204 #include "tree-inline.h"
205
206 /* The maximum number of iterations between the considered memory
207    references.  */
208
209 #define MAX_DISTANCE (target_avail_regs < 16 ? 4 : 8)
210    
211 /* Data references.  */
212
213 typedef struct dref
214 {
215   /* The reference itself.  */
216   struct data_reference *ref;
217
218   /* The statement in that the reference appears.  */
219   tree stmt;
220
221   /* Distance of the reference from the root of the chain (in number of
222      iterations of the loop).  */
223   unsigned distance;
224
225   /* Number of iterations offset from the first reference in the component.  */
226   double_int offset;
227
228   /* Number of the reference in a component, in dominance ordering.  */
229   unsigned pos;
230
231   /* True if the memory reference is always accessed when the loop is
232      entered.  */
233   unsigned always_accessed : 1;
234 } *dref;
235
236 DEF_VEC_P (dref);
237 DEF_VEC_ALLOC_P (dref, heap);
238
239 /* Type of the chain of the references.  */
240
241 enum chain_type
242 {
243   /* The addresses of the references in the chain are constant.  */
244   CT_INVARIANT,
245
246   /* There are only loads in the chain.  */
247   CT_LOAD,
248
249   /* Root of the chain is store, the rest are loads.  */
250   CT_STORE_LOAD,
251
252   /* A combination of two chains.  */
253   CT_COMBINATION
254 };
255
256 /* Chains of data references.  */
257
258 typedef struct chain
259 {
260   /* Type of the chain.  */
261   enum chain_type type;
262
263   /* For combination chains, the operator and the two chains that are
264      combined, and the type of the result.  */
265   enum tree_code operator;
266   tree rslt_type;
267   struct chain *ch1, *ch2;
268
269   /* The references in the chain.  */
270   VEC(dref,heap) *refs;
271
272   /* The maximum distance of the reference in the chain from the root.  */
273   unsigned length;
274
275   /* The variables used to copy the value throughout iterations.  */
276   VEC(tree,heap) *vars;
277
278   /* Initializers for the variables.  */
279   VEC(tree,heap) *inits;
280
281   /* True if there is a use of a variable with the maximal distance
282      that comes after the root in the loop.  */
283   unsigned has_max_use_after : 1;
284
285   /* True if all the memory references in the chain are always accessed.  */
286   unsigned all_always_accessed : 1;
287
288   /* True if this chain was combined together with some other chain.  */
289   unsigned combined : 1;
290 } *chain_p;
291
292 DEF_VEC_P (chain_p);
293 DEF_VEC_ALLOC_P (chain_p, heap);
294
295 /* Describes the knowledge about the step of the memory references in
296    the component.  */
297
298 enum ref_step_type
299 {
300   /* The step is zero.  */
301   RS_INVARIANT,
302
303   /* The step is nonzero.  */
304   RS_NONZERO,
305
306   /* The step may or may not be nonzero.  */
307   RS_ANY
308 };
309
310 /* Components of the data dependence graph.  */
311
312 struct component
313 {
314   /* The references in the component.  */
315   VEC(dref,heap) *refs;
316
317   /* What we know about the step of the references in the component.  */
318   enum ref_step_type comp_step;
319
320   /* Next component in the list.  */
321   struct component *next;
322 };
323
324 /* Bitmap of ssa names defined by looparound phi nodes covered by chains.  */
325
326 static bitmap looparound_phis;
327
328 /* Cache used by tree_to_aff_combination_expand.  */
329
330 static struct pointer_map_t *name_expansions;
331
332 /* Dumps data reference REF to FILE.  */
333
334 extern void dump_dref (FILE *, dref);
335 void
336 dump_dref (FILE *file, dref ref)
337 {
338   if (ref->ref)
339     {
340       fprintf (file, "    ");
341       print_generic_expr (file, DR_REF (ref->ref), TDF_SLIM);
342       fprintf (file, " (id %u%s)\n", ref->pos,
343                DR_IS_READ (ref->ref) ? "" : ", write");
344
345       fprintf (file, "      offset ");
346       dump_double_int (file, ref->offset, false);
347       fprintf (file, "\n");
348
349       fprintf (file, "      distance %u\n", ref->distance);
350     }
351   else
352     {
353       if (TREE_CODE (ref->stmt) == PHI_NODE)
354         fprintf (file, "    looparound ref\n");
355       else
356         fprintf (file, "    combination ref\n");
357       fprintf (file, "      in statement ");
358       print_generic_expr (file, ref->stmt, TDF_SLIM);
359       fprintf (file, "\n");
360       fprintf (file, "      distance %u\n", ref->distance);
361     }
362
363 }
364
365 /* Dumps CHAIN to FILE.  */
366
367 extern void dump_chain (FILE *, chain_p);
368 void
369 dump_chain (FILE *file, chain_p chain)
370 {
371   dref a;
372   const char *chain_type;
373   unsigned i;
374   tree var;
375
376   switch (chain->type)
377     {
378     case CT_INVARIANT:
379       chain_type = "Load motion";
380       break;
381
382     case CT_LOAD:
383       chain_type = "Loads-only";
384       break;
385
386     case CT_STORE_LOAD:
387       chain_type = "Store-loads";
388       break;
389
390     case CT_COMBINATION:
391       chain_type = "Combination";
392       break;
393
394     default:
395       gcc_unreachable ();
396     }
397
398   fprintf (file, "%s chain %p%s\n", chain_type, (void *) chain,
399            chain->combined ? " (combined)" : "");
400   if (chain->type != CT_INVARIANT)
401     fprintf (file, "  max distance %u%s\n", chain->length,
402              chain->has_max_use_after ? "" : ", may reuse first");
403
404   if (chain->type == CT_COMBINATION)
405     {
406       fprintf (file, "  equal to %p %s %p in type ",
407                (void *) chain->ch1, op_symbol_code (chain->operator),
408                (void *) chain->ch2);
409       print_generic_expr (file, chain->rslt_type, TDF_SLIM);
410       fprintf (file, "\n");
411     }
412
413   if (chain->vars)
414     {
415       fprintf (file, "  vars");
416       for (i = 0; VEC_iterate (tree, chain->vars, i, var); i++)
417         {
418           fprintf (file, " ");
419           print_generic_expr (file, var, TDF_SLIM);
420         }
421       fprintf (file, "\n");
422     }
423
424   if (chain->inits)
425     {
426       fprintf (file, "  inits");
427       for (i = 0; VEC_iterate (tree, chain->inits, i, var); i++)
428         {
429           fprintf (file, " ");
430           print_generic_expr (file, var, TDF_SLIM);
431         }
432       fprintf (file, "\n");
433     }
434
435   fprintf (file, "  references:\n");
436   for (i = 0; VEC_iterate (dref, chain->refs, i, a); i++)
437     dump_dref (file, a);
438
439   fprintf (file, "\n");
440 }
441
442 /* Dumps CHAINS to FILE.  */
443
444 extern void dump_chains (FILE *, VEC (chain_p, heap) *);
445 void
446 dump_chains (FILE *file, VEC (chain_p, heap) *chains)
447 {
448   chain_p chain;
449   unsigned i;
450
451   for (i = 0; VEC_iterate (chain_p, chains, i, chain); i++)
452     dump_chain (file, chain);
453 }
454
455 /* Dumps COMP to FILE.  */
456
457 extern void dump_component (FILE *, struct component *);
458 void
459 dump_component (FILE *file, struct component *comp)
460 {
461   dref a;
462   unsigned i;
463
464   fprintf (file, "Component%s:\n",
465            comp->comp_step == RS_INVARIANT ? " (invariant)" : "");
466   for (i = 0; VEC_iterate (dref, comp->refs, i, a); i++)
467     dump_dref (file, a);
468   fprintf (file, "\n");
469 }
470
471 /* Dumps COMPS to FILE.  */
472
473 extern void dump_components (FILE *, struct component *);
474 void
475 dump_components (FILE *file, struct component *comps)
476 {
477   struct component *comp;
478
479   for (comp = comps; comp; comp = comp->next)
480     dump_component (file, comp);
481 }
482
483 /* Frees a chain CHAIN.  */
484
485 static void
486 release_chain (chain_p chain)
487 {
488   dref ref;
489   unsigned i;
490
491   if (chain == NULL)
492     return;
493
494   for (i = 0; VEC_iterate (dref, chain->refs, i, ref); i++)
495     free (ref);
496
497   VEC_free (dref, heap, chain->refs);
498   VEC_free (tree, heap, chain->vars);
499   VEC_free (tree, heap, chain->inits);
500
501   free (chain);
502 }
503
504 /* Frees CHAINS.  */
505
506 static void
507 release_chains (VEC (chain_p, heap) *chains)
508 {
509   unsigned i;
510   chain_p chain;
511
512   for (i = 0; VEC_iterate (chain_p, chains, i, chain); i++)
513     release_chain (chain);
514   VEC_free (chain_p, heap, chains);
515 }
516
517 /* Frees a component COMP.  */
518
519 static void
520 release_component (struct component *comp)
521 {
522   VEC_free (dref, heap, comp->refs);
523   free (comp);
524 }
525
526 /* Frees list of components COMPS.  */
527
528 static void
529 release_components (struct component *comps)
530 {
531   struct component *act, *next;
532
533   for (act = comps; act; act = next)
534     {
535       next = act->next;
536       release_component (act);
537     }
538 }
539
540 /* Finds a root of tree given by FATHERS containing A, and performs path
541    shortening.  */
542
543 static unsigned
544 component_of (unsigned fathers[], unsigned a)
545 {
546   unsigned root, n;
547
548   for (root = a; root != fathers[root]; root = fathers[root])
549     continue;
550
551   for (; a != root; a = n)
552     {
553       n = fathers[a];
554       fathers[a] = root;
555     }
556
557   return root;
558 }
559
560 /* Join operation for DFU.  FATHERS gives the tree, SIZES are sizes of the
561    components, A and B are components to merge.  */
562
563 static void
564 merge_comps (unsigned fathers[], unsigned sizes[], unsigned a, unsigned b)
565 {
566   unsigned ca = component_of (fathers, a);
567   unsigned cb = component_of (fathers, b);
568
569   if (ca == cb)
570     return;
571
572   if (sizes[ca] < sizes[cb])
573     {
574       sizes[cb] += sizes[ca];
575       fathers[ca] = cb;
576     }
577   else
578     {
579       sizes[ca] += sizes[cb];
580       fathers[cb] = ca;
581     }
582 }
583
584 /* Returns true if A is a reference that is suitable for predictive commoning
585    in the innermost loop that contains it.  REF_STEP is set according to the
586    step of the reference A.  */
587
588 static bool
589 suitable_reference_p (struct data_reference *a, enum ref_step_type *ref_step)
590 {
591   tree ref = DR_REF (a), step = DR_STEP (a);
592
593   if (!step
594       || !is_gimple_reg_type (TREE_TYPE (ref)))
595     return false;
596
597   if (integer_zerop (step))
598     *ref_step = RS_INVARIANT;
599   else if (integer_nonzerop (step))
600     *ref_step = RS_NONZERO;
601   else
602     *ref_step = RS_ANY;
603
604   return true;
605 }
606
607 /* Stores DR_OFFSET (DR) + DR_INIT (DR) to OFFSET.  */
608
609 static void
610 aff_combination_dr_offset (struct data_reference *dr, aff_tree *offset)
611 {
612   aff_tree delta;
613
614   tree_to_aff_combination_expand (DR_OFFSET (dr), sizetype, offset,
615                                   &name_expansions);
616   aff_combination_const (&delta, sizetype, tree_to_double_int (DR_INIT (dr)));
617   aff_combination_add (offset, &delta);
618 }
619
620 /* Determines number of iterations of the innermost enclosing loop before B
621    refers to exactly the same location as A and stores it to OFF.  If A and
622    B do not have the same step, they never meet, or anything else fails,
623    returns false, otherwise returns true.  Both A and B are assumed to
624    satisfy suitable_reference_p.  */
625
626 static bool
627 determine_offset (struct data_reference *a, struct data_reference *b,
628                   double_int *off)
629 {
630   aff_tree diff, baseb, step;
631
632   /* Check whether the base address and the step of both references is the
633      same.  */
634   if (!operand_equal_p (DR_STEP (a), DR_STEP (b), 0)
635       || !operand_equal_p (DR_BASE_ADDRESS (a), DR_BASE_ADDRESS (b), 0))
636     return false;
637
638   if (integer_zerop (DR_STEP (a)))
639     {
640       /* If the references have loop invariant address, check that they access
641          exactly the same location.  */
642       *off = double_int_zero;
643       return (operand_equal_p (DR_OFFSET (a), DR_OFFSET (b), 0)
644               && operand_equal_p (DR_INIT (a), DR_INIT (b), 0));
645     }
646
647   /* Compare the offsets of the addresses, and check whether the difference
648      is a multiple of step.  */
649   aff_combination_dr_offset (a, &diff);
650   aff_combination_dr_offset (b, &baseb);
651   aff_combination_scale (&baseb, double_int_minus_one);
652   aff_combination_add (&diff, &baseb);
653
654   tree_to_aff_combination_expand (DR_STEP (a), sizetype,
655                                   &step, &name_expansions);
656   return aff_combination_constant_multiple_p (&diff, &step, off);
657 }
658
659 /* Returns the last basic block in LOOP for that we are sure that
660    it is executed whenever the loop is entered.  */
661
662 static basic_block
663 last_always_executed_block (struct loop *loop)
664 {
665   unsigned i;
666   VEC (edge, heap) *exits = get_loop_exit_edges (loop);
667   edge ex;
668   basic_block last = loop->latch;
669
670   for (i = 0; VEC_iterate (edge, exits, i, ex); i++)
671     last = nearest_common_dominator (CDI_DOMINATORS, last, ex->src);
672   VEC_free (edge, heap, exits);
673
674   return last;
675 }
676
677 /* Splits dependence graph on DATAREFS described by DEPENDS to components.  */
678
679 static struct component *
680 split_data_refs_to_components (struct loop *loop,
681                                VEC (data_reference_p, heap) *datarefs,
682                                VEC (ddr_p, heap) *depends)
683 {
684   unsigned i, n = VEC_length (data_reference_p, datarefs);
685   unsigned ca, ia, ib, bad;
686   unsigned *comp_father = XNEWVEC (unsigned, n + 1);
687   unsigned *comp_size = XNEWVEC (unsigned, n + 1);
688   struct component **comps;
689   struct data_reference *dr, *dra, *drb;
690   struct data_dependence_relation *ddr;
691   struct component *comp_list = NULL, *comp;
692   dref dataref;
693   basic_block last_always_executed = last_always_executed_block (loop);
694  
695   for (i = 0; VEC_iterate (data_reference_p, datarefs, i, dr); i++)
696     {
697       if (!DR_REF (dr))
698         {
699           /* A fake reference for call or asm_expr that may clobber memory;
700              just fail.  */
701           goto end;
702         }
703       dr->aux = i;
704       comp_father[i] = i;
705       comp_size[i] = 1;
706     }
707
708   /* A component reserved for the "bad" data references.  */
709   comp_father[n] = n;
710   comp_size[n] = 1;
711
712   for (i = 0; VEC_iterate (data_reference_p, datarefs, i, dr); i++)
713     {
714       enum ref_step_type dummy;
715
716       if (!suitable_reference_p (dr, &dummy))
717         {
718           ia = dr->aux;
719           merge_comps (comp_father, comp_size, n, ia);
720         }
721     }
722
723   for (i = 0; VEC_iterate (ddr_p, depends, i, ddr); i++)
724     {
725       double_int dummy_off;
726
727       if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
728         continue;
729
730       dra = DDR_A (ddr);
731       drb = DDR_B (ddr);
732       ia = component_of (comp_father, dra->aux);
733       ib = component_of (comp_father, drb->aux);
734       if (ia == ib)
735         continue;
736
737       bad = component_of (comp_father, n);
738
739       /* If both A and B are reads, we may ignore unsuitable dependences.  */
740       if (DR_IS_READ (dra) && DR_IS_READ (drb)
741           && (ia == bad || ib == bad
742               || !determine_offset (dra, drb, &dummy_off)))
743         continue;
744           
745       merge_comps (comp_father, comp_size, ia, ib);
746     }
747
748   comps = XCNEWVEC (struct component *, n);
749   bad = component_of (comp_father, n);
750   for (i = 0; VEC_iterate (data_reference_p, datarefs, i, dr); i++)
751     {
752       ia = dr->aux;
753       ca = component_of (comp_father, ia);
754       if (ca == bad)
755         continue;
756
757       comp = comps[ca];
758       if (!comp)
759         {
760           comp = XCNEW (struct component);
761           comp->refs = VEC_alloc (dref, heap, comp_size[ca]);
762           comps[ca] = comp;
763         }
764
765       dataref = XCNEW (struct dref);
766       dataref->ref = dr;
767       dataref->stmt = DR_STMT (dr);
768       dataref->offset = double_int_zero;
769       dataref->distance = 0;
770
771       dataref->always_accessed
772               = dominated_by_p (CDI_DOMINATORS, last_always_executed,
773                                 bb_for_stmt (dataref->stmt));
774       dataref->pos = VEC_length (dref, comp->refs);
775       VEC_quick_push (dref, comp->refs, dataref);
776     }
777
778   for (i = 0; i < n; i++)
779     {
780       comp = comps[i];
781       if (comp)
782         {
783           comp->next = comp_list;
784           comp_list = comp;
785         }
786     }
787   free (comps);
788
789 end:
790   free (comp_father);
791   free (comp_size);
792   return comp_list;
793 }
794
795 /* Returns true if the component COMP satisfies the conditions
796    described in 2) at the beginning of this file.  LOOP is the current
797    loop.  */
798       
799 static bool
800 suitable_component_p (struct loop *loop, struct component *comp)
801 {
802   unsigned i;
803   dref a, first;
804   basic_block ba, bp = loop->header;
805   bool ok, has_write = false;
806
807   for (i = 0; VEC_iterate (dref, comp->refs, i, a); i++)
808     {
809       ba = bb_for_stmt (a->stmt);
810
811       if (!just_once_each_iteration_p (loop, ba))
812         return false;
813
814       gcc_assert (dominated_by_p (CDI_DOMINATORS, ba, bp));
815       bp = ba;
816
817       if (!DR_IS_READ (a->ref))
818         has_write = true;
819     }
820
821   first = VEC_index (dref, comp->refs, 0);
822   ok = suitable_reference_p (first->ref, &comp->comp_step);
823   gcc_assert (ok);
824   first->offset = double_int_zero;
825
826   for (i = 1; VEC_iterate (dref, comp->refs, i, a); i++)
827     {
828       if (!determine_offset (first->ref, a->ref, &a->offset))
829         return false;
830
831 #ifdef ENABLE_CHECKING
832       {
833         enum ref_step_type a_step;
834         ok = suitable_reference_p (a->ref, &a_step);
835         gcc_assert (ok && a_step == comp->comp_step);
836       }
837 #endif
838     }
839
840   /* If there is a write inside the component, we must know whether the
841      step is nonzero or not -- we would not otherwise be able to recognize
842      whether the value accessed by reads comes from the OFFSET-th iteration
843      or the previous one.  */
844   if (has_write && comp->comp_step == RS_ANY)
845     return false;
846
847   return true;
848 }
849       
850 /* Check the conditions on references inside each of components COMPS,
851    and remove the unsuitable components from the list.  The new list
852    of components is returned.  The conditions are described in 2) at
853    the beginning of this file.  LOOP is the current loop.  */
854
855 static struct component *
856 filter_suitable_components (struct loop *loop, struct component *comps)
857 {
858   struct component **comp, *act;
859
860   for (comp = &comps; *comp; )
861     {
862       act = *comp;
863       if (suitable_component_p (loop, act))
864         comp = &act->next;
865       else
866         {
867           *comp = act->next;
868           release_component (act);
869         }
870     }
871
872   return comps;
873 }
874
875 /* Compares two drefs A and B by their offset and position.  Callback for
876    qsort.  */
877
878 static int
879 order_drefs (const void *a, const void *b)
880 {
881   const dref *da = a;
882   const dref *db = b;
883   int offcmp = double_int_scmp ((*da)->offset, (*db)->offset);
884
885   if (offcmp != 0)
886     return offcmp;
887
888   return (*da)->pos - (*db)->pos;
889 }
890
891 /* Returns root of the CHAIN.  */
892
893 static inline dref
894 get_chain_root (chain_p chain)
895 {
896   return VEC_index (dref, chain->refs, 0);
897 }
898
899 /* Adds REF to the chain CHAIN.  */
900
901 static void
902 add_ref_to_chain (chain_p chain, dref ref)
903 {
904   dref root = get_chain_root (chain);
905   double_int dist;
906
907   gcc_assert (double_int_scmp (root->offset, ref->offset) <= 0);
908   dist = double_int_add (ref->offset, double_int_neg (root->offset));
909   if (double_int_ucmp (uhwi_to_double_int (MAX_DISTANCE), dist) <= 0)
910     return;
911   gcc_assert (double_int_fits_in_uhwi_p (dist));
912
913   VEC_safe_push (dref, heap, chain->refs, ref);
914
915   ref->distance = double_int_to_uhwi (dist);
916
917   if (ref->distance >= chain->length)
918     {
919       chain->length = ref->distance;
920       chain->has_max_use_after = false;
921     }
922
923   if (ref->distance == chain->length
924       && ref->pos > root->pos)
925     chain->has_max_use_after = true;
926
927   chain->all_always_accessed &= ref->always_accessed;
928 }
929
930 /* Returns the chain for invariant component COMP.  */
931
932 static chain_p
933 make_invariant_chain (struct component *comp)
934 {
935   chain_p chain = XCNEW (struct chain);
936   unsigned i;
937   dref ref;
938
939   chain->type = CT_INVARIANT;
940
941   chain->all_always_accessed = true;
942
943   for (i = 0; VEC_iterate (dref, comp->refs, i, ref); i++)
944     {
945       VEC_safe_push (dref, heap, chain->refs, ref);
946       chain->all_always_accessed &= ref->always_accessed;
947     }
948
949   return chain;
950 }
951
952 /* Make a new chain rooted at REF.  */
953
954 static chain_p
955 make_rooted_chain (dref ref)
956 {
957   chain_p chain = XCNEW (struct chain);
958
959   chain->type = DR_IS_READ (ref->ref) ? CT_LOAD : CT_STORE_LOAD;
960
961   VEC_safe_push (dref, heap, chain->refs, ref);
962   chain->all_always_accessed = ref->always_accessed;
963
964   ref->distance = 0;
965
966   return chain;
967 }
968
969 /* Returns true if CHAIN is not trivial.  */
970
971 static bool
972 nontrivial_chain_p (chain_p chain)
973 {
974   return chain != NULL && VEC_length (dref, chain->refs) > 1;
975 }
976
977 /* Returns the ssa name that contains the value of REF, or NULL_TREE if there
978    is no such name.  */
979
980 static tree
981 name_for_ref (dref ref)
982 {
983   tree name;
984
985   if (TREE_CODE (ref->stmt) == GIMPLE_MODIFY_STMT)
986     {
987       if (!ref->ref || DR_IS_READ (ref->ref))
988         name = GIMPLE_STMT_OPERAND (ref->stmt, 0);
989       else
990         name = GIMPLE_STMT_OPERAND (ref->stmt, 1);
991     }
992   else
993     name = PHI_RESULT (ref->stmt);
994
995   return (TREE_CODE (name) == SSA_NAME ? name : NULL_TREE);
996 }
997
998 /* Returns true if REF is a valid initializer for ROOT with given DISTANCE (in
999    iterations of the innermost enclosing loop).  */
1000
1001 static bool
1002 valid_initializer_p (struct data_reference *ref,
1003                      unsigned distance, struct data_reference *root)
1004 {
1005   aff_tree diff, base, step;
1006   double_int off;
1007
1008   if (!DR_BASE_ADDRESS (ref))
1009     return false;
1010
1011   /* Both REF and ROOT must be accessing the same object.  */
1012   if (!operand_equal_p (DR_BASE_ADDRESS (ref), DR_BASE_ADDRESS (root), 0))
1013     return false;
1014
1015   /* The initializer is defined outside of loop, hence its address must be
1016      invariant inside the loop.  */
1017   gcc_assert (integer_zerop (DR_STEP (ref)));
1018
1019   /* If the address of the reference is invariant, initializer must access
1020      exactly the same location.  */
1021   if (integer_zerop (DR_STEP (root)))
1022     return (operand_equal_p (DR_OFFSET (ref), DR_OFFSET (root), 0)
1023             && operand_equal_p (DR_INIT (ref), DR_INIT (root), 0));
1024
1025   /* Verify that this index of REF is equal to the root's index at
1026      -DISTANCE-th iteration.  */
1027   aff_combination_dr_offset (root, &diff);
1028   aff_combination_dr_offset (ref, &base);
1029   aff_combination_scale (&base, double_int_minus_one);
1030   aff_combination_add (&diff, &base);
1031
1032   tree_to_aff_combination_expand (DR_STEP (root), sizetype, &step,
1033                                   &name_expansions);
1034   if (!aff_combination_constant_multiple_p (&diff, &step, &off))
1035     return false;
1036
1037   if (!double_int_equal_p (off, uhwi_to_double_int (distance)))
1038     return false;
1039
1040   return true;
1041 }
1042
1043 /* Finds looparound phi node of LOOP that copies the value of REF, and if its
1044    initial value is correct (equal to initial value of REF shifted by one
1045    iteration), returns the phi node.  Otherwise, NULL_TREE is returned.  ROOT
1046    is the root of the current chain.  */
1047
1048 static tree
1049 find_looparound_phi (struct loop *loop, dref ref, dref root)
1050 {
1051   tree name, phi, init, init_stmt, init_ref;
1052   edge latch = loop_latch_edge (loop);
1053   struct data_reference init_dr;
1054
1055   if (TREE_CODE (ref->stmt) == GIMPLE_MODIFY_STMT)
1056     {
1057       if (DR_IS_READ (ref->ref))
1058         name = GIMPLE_STMT_OPERAND (ref->stmt, 0);
1059       else
1060         name = GIMPLE_STMT_OPERAND (ref->stmt, 1);
1061     }
1062   else
1063     name = PHI_RESULT (ref->stmt);
1064   if (!name)
1065     return NULL_TREE;
1066
1067   for (phi = phi_nodes (loop->header); phi; phi = PHI_CHAIN (phi))
1068     if (PHI_ARG_DEF_FROM_EDGE (phi, latch) == name)
1069       break;
1070
1071   if (!phi)
1072     return NULL_TREE;
1073
1074   init = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop));
1075   if (TREE_CODE (init) != SSA_NAME)
1076     return NULL_TREE;
1077   init_stmt = SSA_NAME_DEF_STMT (init);
1078   if (TREE_CODE (init_stmt) != GIMPLE_MODIFY_STMT)
1079     return NULL_TREE;
1080   gcc_assert (GIMPLE_STMT_OPERAND (init_stmt, 0) == init);
1081
1082   init_ref = GIMPLE_STMT_OPERAND (init_stmt, 1);
1083   if (!REFERENCE_CLASS_P (init_ref)
1084       && !DECL_P (init_ref))
1085     return NULL_TREE;
1086
1087   /* Analyze the behavior of INIT_REF with respect to LOOP (innermost
1088      loop enclosing PHI).  */
1089   memset (&init_dr, 0, sizeof (struct data_reference));
1090   DR_REF (&init_dr) = init_ref;
1091   DR_STMT (&init_dr) = phi;
1092   dr_analyze_innermost (&init_dr);
1093
1094   if (!valid_initializer_p (&init_dr, ref->distance + 1, root->ref))
1095     return NULL_TREE;
1096
1097   return phi;
1098 }
1099
1100 /* Adds a reference for the looparound copy of REF in PHI to CHAIN.  */
1101
1102 static void
1103 insert_looparound_copy (chain_p chain, dref ref, tree phi)
1104 {
1105   dref nw = XCNEW (struct dref), aref;
1106   unsigned i;
1107
1108   nw->stmt = phi;
1109   nw->distance = ref->distance + 1;
1110   nw->always_accessed = 1;
1111
1112   for (i = 0; VEC_iterate (dref, chain->refs, i, aref); i++)
1113     if (aref->distance >= nw->distance)
1114       break;
1115   VEC_safe_insert (dref, heap, chain->refs, i, nw);
1116
1117   if (nw->distance > chain->length)
1118     {
1119       chain->length = nw->distance;
1120       chain->has_max_use_after = false;
1121     }
1122 }
1123
1124 /* For references in CHAIN that are copied around the LOOP (created previously
1125    by PRE, or by user), add the results of such copies to the chain.  This
1126    enables us to remove the copies by unrolling, and may need less registers
1127    (also, it may allow us to combine chains together).  */
1128
1129 static void
1130 add_looparound_copies (struct loop *loop, chain_p chain)
1131 {
1132   unsigned i;
1133   dref ref, root = get_chain_root (chain);
1134   tree phi;
1135
1136   for (i = 0; VEC_iterate (dref, chain->refs, i, ref); i++)
1137     {
1138       phi = find_looparound_phi (loop, ref, root);
1139       if (!phi)
1140         continue;
1141
1142       bitmap_set_bit (looparound_phis, SSA_NAME_VERSION (PHI_RESULT (phi)));
1143       insert_looparound_copy (chain, ref, phi);
1144     }
1145 }
1146
1147 /* Find roots of the values and determine distances in the component COMP.
1148    The references are redistributed into CHAINS.  LOOP is the current
1149    loop.  */
1150
1151 static void
1152 determine_roots_comp (struct loop *loop,
1153                       struct component *comp,
1154                       VEC (chain_p, heap) **chains)
1155 {
1156   unsigned i;
1157   dref a;
1158   chain_p chain = NULL;
1159
1160   /* Invariants are handled specially.  */
1161   if (comp->comp_step == RS_INVARIANT)
1162     {
1163       chain = make_invariant_chain (comp);
1164       VEC_safe_push (chain_p, heap, *chains, chain);
1165       return;
1166     }
1167
1168   qsort (VEC_address (dref, comp->refs), VEC_length (dref, comp->refs),
1169          sizeof (dref), order_drefs);
1170
1171   for (i = 0; VEC_iterate (dref, comp->refs, i, a); i++)
1172     {
1173       if (!chain || !DR_IS_READ (a->ref))
1174         {
1175           if (nontrivial_chain_p (chain))
1176             VEC_safe_push (chain_p, heap, *chains, chain);
1177           else
1178             release_chain (chain);
1179           chain = make_rooted_chain (a);
1180           continue;
1181         }
1182
1183       add_ref_to_chain (chain, a);
1184     }
1185
1186   if (nontrivial_chain_p (chain))
1187     {
1188       add_looparound_copies (loop, chain);
1189       VEC_safe_push (chain_p, heap, *chains, chain);
1190     }
1191   else
1192     release_chain (chain);
1193 }
1194
1195 /* Find roots of the values and determine distances in components COMPS, and
1196    separates the references to CHAINS.  LOOP is the current loop.  */
1197
1198 static void
1199 determine_roots (struct loop *loop,
1200                  struct component *comps, VEC (chain_p, heap) **chains)
1201 {
1202   struct component *comp;
1203
1204   for (comp = comps; comp; comp = comp->next)
1205     determine_roots_comp (loop, comp, chains);
1206 }
1207
1208 /* Replace the reference in statement STMT with temporary variable
1209    NEW.  If SET is true, NEW is instead initialized to the value of
1210    the reference in the statement.  IN_LHS is true if the reference
1211    is in the lhs of STMT, false if it is in rhs.  */
1212
1213 static void
1214 replace_ref_with (tree stmt, tree new, bool set, bool in_lhs)
1215 {
1216   tree val, new_stmt;
1217   block_stmt_iterator bsi;
1218
1219   if (TREE_CODE (stmt) == PHI_NODE)
1220     {
1221       gcc_assert (!in_lhs && !set);
1222
1223       val = PHI_RESULT (stmt);
1224       bsi = bsi_after_labels (bb_for_stmt (stmt));
1225       remove_phi_node (stmt, NULL_TREE, false);
1226
1227       /* Turn the phi node into GIMPLE_MODIFY_STMT.  */
1228       new_stmt = build_gimple_modify_stmt_stat (val, new);
1229       SSA_NAME_DEF_STMT (val) = new_stmt;
1230       bsi_insert_before (&bsi, new_stmt, BSI_NEW_STMT);
1231       return;
1232     }
1233       
1234   /* Since the reference is of gimple_reg type, it should only
1235      appear as lhs or rhs of modify statement.  */
1236   gcc_assert (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT);
1237
1238   /* If we do not need to initialize NEW, just replace the use of OLD.  */
1239   if (!set)
1240     {
1241       gcc_assert (!in_lhs);
1242       GIMPLE_STMT_OPERAND (stmt, 1) = new;
1243       update_stmt (stmt);
1244       return;
1245     }
1246
1247   bsi = bsi_for_stmt (stmt);
1248   if (in_lhs)
1249     {
1250       val = GIMPLE_STMT_OPERAND (stmt, 1);
1251
1252       /* OLD = VAL
1253
1254          is transformed to
1255
1256          OLD = VAL
1257          NEW = VAL
1258
1259          (since the reference is of gimple_reg type, VAL is either gimple
1260          invariant or ssa name).  */
1261     }
1262   else
1263     {
1264       val = GIMPLE_STMT_OPERAND (stmt, 0);
1265
1266       /* VAL = OLD
1267
1268          is transformed to
1269
1270          VAL = OLD
1271          NEW = VAL  */
1272     }
1273
1274   new_stmt = build_gimple_modify_stmt_stat (new, unshare_expr (val));
1275   bsi_insert_after (&bsi, new_stmt, BSI_NEW_STMT);
1276   SSA_NAME_DEF_STMT (new) = new_stmt;
1277 }
1278
1279 /* Returns the reference to the address of REF in the ITER-th iteration of
1280    LOOP, or NULL if we fail to determine it (ITER may be negative).  We
1281    try to preserve the original shape of the reference (not rewrite it
1282    as an indirect ref to the address), to make tree_could_trap_p in
1283    prepare_initializers_chain return false more often.  */
1284
1285 static tree
1286 ref_at_iteration (struct loop *loop, tree ref, int iter)
1287 {
1288   tree idx, *idx_p, type, val, op0 = NULL_TREE, ret;
1289   affine_iv iv;
1290   bool ok;
1291
1292   if (handled_component_p (ref))
1293     {
1294       op0 = ref_at_iteration (loop, TREE_OPERAND (ref, 0), iter);
1295       if (!op0)
1296         return NULL_TREE;
1297     }
1298   else if (!INDIRECT_REF_P (ref))
1299     return unshare_expr (ref);
1300
1301   if (TREE_CODE (ref) == INDIRECT_REF)
1302     {
1303       ret = build1 (INDIRECT_REF, TREE_TYPE (ref), NULL_TREE);
1304       idx = TREE_OPERAND (ref, 0);
1305       idx_p = &TREE_OPERAND (ret, 0);
1306     }
1307   else if (TREE_CODE (ref) == COMPONENT_REF)
1308     {
1309       /* Check that the offset is loop invariant.  */
1310       if (TREE_OPERAND (ref, 2)
1311           && !expr_invariant_in_loop_p (loop, TREE_OPERAND (ref, 2)))
1312         return NULL_TREE;
1313
1314       return build3 (COMPONENT_REF, TREE_TYPE (ref), op0,
1315                      unshare_expr (TREE_OPERAND (ref, 1)),
1316                      unshare_expr (TREE_OPERAND (ref, 2)));
1317     }
1318   else if (TREE_CODE (ref) == ARRAY_REF)
1319     {
1320       /* Check that the lower bound and the step are loop invariant.  */
1321       if (TREE_OPERAND (ref, 2)
1322           && !expr_invariant_in_loop_p (loop, TREE_OPERAND (ref, 2)))
1323         return NULL_TREE;
1324       if (TREE_OPERAND (ref, 3)
1325           && !expr_invariant_in_loop_p (loop, TREE_OPERAND (ref, 3)))
1326         return NULL_TREE;
1327
1328       ret = build4 (ARRAY_REF, TREE_TYPE (ref), op0, NULL_TREE,
1329                     unshare_expr (TREE_OPERAND (ref, 2)),
1330                     unshare_expr (TREE_OPERAND (ref, 3)));
1331       idx = TREE_OPERAND (ref, 1);
1332       idx_p = &TREE_OPERAND (ret, 1);
1333     }
1334   else
1335     return NULL_TREE;
1336
1337   ok = simple_iv (loop, first_stmt (loop->header), idx, &iv, true);
1338   if (!ok)
1339     return NULL_TREE;
1340   iv.base = expand_simple_operations (iv.base);
1341   if (integer_zerop (iv.step))
1342     *idx_p = unshare_expr (iv.base);
1343   else
1344     {
1345       type = TREE_TYPE (iv.base);
1346       val = fold_build2 (MULT_EXPR, type, iv.step,
1347                          build_int_cst_type (type, iter));
1348       val = fold_build2 (PLUS_EXPR, type, iv.base, val);
1349       *idx_p = unshare_expr (val);
1350     }
1351
1352   return ret;
1353 }
1354
1355 /* Get the initialization expression for the INDEX-th temporary variable
1356    of CHAIN.  */
1357
1358 static tree
1359 get_init_expr (chain_p chain, unsigned index)
1360 {
1361   if (chain->type == CT_COMBINATION)
1362     {
1363       tree e1 = get_init_expr (chain->ch1, index);
1364       tree e2 = get_init_expr (chain->ch2, index);
1365
1366       return fold_build2 (chain->operator, chain->rslt_type, e1, e2);
1367     }
1368   else
1369     return VEC_index (tree, chain->inits, index);
1370 }
1371
1372 /* Marks all virtual operands of statement STMT for renaming.  */
1373
1374 static void
1375 mark_virtual_ops_for_renaming (tree stmt)
1376 {
1377   ssa_op_iter iter;
1378   tree var;
1379
1380   if (TREE_CODE (stmt) == PHI_NODE)
1381     return;
1382
1383   update_stmt (stmt);
1384
1385   FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_ALL_VIRTUALS)
1386     {
1387       if (TREE_CODE (var) == SSA_NAME)
1388         var = SSA_NAME_VAR (var);
1389       mark_sym_for_renaming (var);
1390     }
1391 }
1392
1393 /* Calls mark_virtual_ops_for_renaming for all members of LIST.  */
1394
1395 static void
1396 mark_virtual_ops_for_renaming_list (tree list)
1397 {
1398   tree_stmt_iterator tsi;
1399
1400   for (tsi = tsi_start (list); !tsi_end_p (tsi); tsi_next (&tsi))
1401     mark_virtual_ops_for_renaming (tsi_stmt (tsi));
1402 }
1403
1404 /* Creates the variables for CHAIN, as well as phi nodes for them and
1405    initialization on entry to LOOP.  Uids of the newly created
1406    temporary variables are marked in TMP_VARS.  */
1407
1408 static void
1409 initialize_root_vars (struct loop *loop, chain_p chain, bitmap tmp_vars)
1410 {
1411   unsigned i;
1412   unsigned n = chain->length;
1413   dref root = get_chain_root (chain);
1414   bool reuse_first = !chain->has_max_use_after;
1415   tree ref, init, var, next, stmts;
1416   tree phi;
1417   edge entry = loop_preheader_edge (loop), latch = loop_latch_edge (loop);
1418
1419   /* If N == 0, then all the references are within the single iteration.  And
1420      since this is an nonempty chain, reuse_first cannot be true.  */
1421   gcc_assert (n > 0 || !reuse_first);
1422
1423   chain->vars = VEC_alloc (tree, heap, n + 1);
1424
1425   if (chain->type == CT_COMBINATION)
1426     ref = GIMPLE_STMT_OPERAND (root->stmt, 0);
1427   else
1428     ref = DR_REF (root->ref);
1429
1430   for (i = 0; i < n + (reuse_first ? 0 : 1); i++)
1431     {
1432       var = create_tmp_var (TREE_TYPE (ref), get_lsm_tmp_name (ref, i));
1433       add_referenced_var (var);
1434       bitmap_set_bit (tmp_vars, DECL_UID (var));
1435       VEC_quick_push (tree, chain->vars, var);
1436     }
1437   if (reuse_first)
1438     VEC_quick_push (tree, chain->vars, VEC_index (tree, chain->vars, 0));
1439   
1440   for (i = 0; VEC_iterate (tree, chain->vars, i, var); i++)
1441     VEC_replace (tree, chain->vars, i, make_ssa_name (var, NULL_TREE));
1442
1443   for (i = 0; i < n; i++)
1444     {
1445       var = VEC_index (tree, chain->vars, i);
1446       next = VEC_index (tree, chain->vars, i + 1);
1447       init = get_init_expr (chain, i);
1448
1449       init = force_gimple_operand (init, &stmts, true, NULL_TREE);
1450       if (stmts)
1451         {
1452           mark_virtual_ops_for_renaming_list (stmts);
1453           bsi_insert_on_edge_immediate (entry, stmts);
1454         }
1455
1456       phi = create_phi_node (var, loop->header);
1457       SSA_NAME_DEF_STMT (var) = phi;
1458       add_phi_arg (phi, init, entry);
1459       add_phi_arg (phi, next, latch);
1460     }
1461 }
1462
1463 /* Create the variables and initialization statement for root of chain
1464    CHAIN.  Uids of the newly created temporary variables are marked
1465    in TMP_VARS.  */
1466
1467 static void
1468 initialize_root (struct loop *loop, chain_p chain, bitmap tmp_vars)
1469 {
1470   dref root = get_chain_root (chain);
1471   bool in_lhs = (chain->type == CT_STORE_LOAD
1472                  || chain->type == CT_COMBINATION);
1473
1474   initialize_root_vars (loop, chain, tmp_vars);
1475   replace_ref_with (root->stmt,
1476                     VEC_index (tree, chain->vars, chain->length),
1477                     true, in_lhs);
1478 }
1479
1480 /* Initializes a variable for load motion for ROOT and prepares phi nodes and
1481    initialization on entry to LOOP if necessary.  The ssa name for the variable
1482    is stored in VARS.  If WRITTEN is true, also a phi node to copy its value
1483    around the loop is created.  Uid of the newly created temporary variable
1484    is marked in TMP_VARS.  INITS is the list containing the (single)
1485    initializer.  */
1486
1487 static void
1488 initialize_root_vars_lm (struct loop *loop, dref root, bool written,
1489                          VEC(tree, heap) **vars, VEC(tree, heap) *inits,
1490                          bitmap tmp_vars)
1491 {
1492   unsigned i;
1493   tree ref = DR_REF (root->ref), init, var, next, stmts;
1494   tree phi;
1495   edge entry = loop_preheader_edge (loop), latch = loop_latch_edge (loop);
1496
1497   /* Find the initializer for the variable, and check that it cannot
1498      trap.  */
1499   init = VEC_index (tree, inits, 0);
1500
1501   *vars = VEC_alloc (tree, heap, written ? 2 : 1);
1502   var = create_tmp_var (TREE_TYPE (ref), get_lsm_tmp_name (ref, 0));
1503   add_referenced_var (var);
1504   bitmap_set_bit (tmp_vars, DECL_UID (var));
1505   VEC_quick_push (tree, *vars, var);
1506   if (written)
1507     VEC_quick_push (tree, *vars, VEC_index (tree, *vars, 0));
1508   
1509   for (i = 0; VEC_iterate (tree, *vars, i, var); i++)
1510     VEC_replace (tree, *vars, i, make_ssa_name (var, NULL_TREE));
1511
1512   var = VEC_index (tree, *vars, 0);
1513       
1514   init = force_gimple_operand (init, &stmts, written, NULL_TREE);
1515   if (stmts)
1516     {
1517       mark_virtual_ops_for_renaming_list (stmts);
1518       bsi_insert_on_edge_immediate (entry, stmts);
1519     }
1520
1521   if (written)
1522     {
1523       next = VEC_index (tree, *vars, 1);
1524       phi = create_phi_node (var, loop->header);
1525       SSA_NAME_DEF_STMT (var) = phi;
1526       add_phi_arg (phi, init, entry);
1527       add_phi_arg (phi, next, latch);
1528     }
1529   else
1530     {
1531       init = build_gimple_modify_stmt_stat (var, init);
1532       SSA_NAME_DEF_STMT (var) = init;
1533       mark_virtual_ops_for_renaming (init);
1534       bsi_insert_on_edge_immediate (entry, init);
1535     }
1536 }
1537
1538
1539 /* Execute load motion for references in chain CHAIN.  Uids of the newly
1540    created temporary variables are marked in TMP_VARS.  */
1541
1542 static void
1543 execute_load_motion (struct loop *loop, chain_p chain, bitmap tmp_vars)
1544 {
1545   VEC (tree, heap) *vars;
1546   dref a;
1547   unsigned n_writes = 0, ridx, i;
1548   tree var;
1549
1550   gcc_assert (chain->type == CT_INVARIANT);
1551   gcc_assert (!chain->combined);
1552   for (i = 0; VEC_iterate (dref, chain->refs, i, a); i++)
1553     if (!DR_IS_READ (a->ref))
1554       n_writes++;
1555   
1556   /* If there are no reads in the loop, there is nothing to do.  */
1557   if (n_writes == VEC_length (dref, chain->refs))
1558     return;
1559
1560   initialize_root_vars_lm (loop, get_chain_root (chain), n_writes > 0,
1561                            &vars, chain->inits, tmp_vars);
1562
1563   ridx = 0;
1564   for (i = 0; VEC_iterate (dref, chain->refs, i, a); i++)
1565     {
1566       bool is_read = DR_IS_READ (a->ref);
1567       mark_virtual_ops_for_renaming (a->stmt);
1568
1569       if (!DR_IS_READ (a->ref))
1570         {
1571           n_writes--;
1572           if (n_writes)
1573             {
1574               var = VEC_index (tree, vars, 0);
1575               var = make_ssa_name (SSA_NAME_VAR (var), NULL_TREE);
1576               VEC_replace (tree, vars, 0, var);
1577             }
1578           else
1579             ridx = 1;
1580         }
1581           
1582       replace_ref_with (a->stmt, VEC_index (tree, vars, ridx),
1583                         !is_read, !is_read);
1584     }
1585
1586   VEC_free (tree, heap, vars);
1587 }
1588
1589 /* Returns the single statement in that NAME is used, excepting
1590    the looparound phi nodes contained in one of the chains.  If there is no
1591    such statement, or more statements, NULL_TREE is returned.  */
1592
1593 static tree
1594 single_nonlooparound_use (tree name)
1595 {
1596   use_operand_p use;
1597   imm_use_iterator it;
1598   tree stmt, ret = NULL_TREE;
1599
1600   FOR_EACH_IMM_USE_FAST (use, it, name)
1601     {
1602       stmt = USE_STMT (use);
1603
1604       if (TREE_CODE (stmt) == PHI_NODE)
1605         {
1606           /* Ignore uses in looparound phi nodes.  Uses in other phi nodes
1607              could not be processed anyway, so just fail for them.  */
1608           if (bitmap_bit_p (looparound_phis,
1609                             SSA_NAME_VERSION (PHI_RESULT (stmt))))
1610             continue;
1611
1612           return NULL_TREE;
1613         }
1614       else if (ret != NULL_TREE)
1615         return NULL_TREE;
1616       else
1617         ret = stmt;
1618     }
1619
1620   return ret;
1621 }
1622
1623 /* Remove statement STMT, as well as the chain of assignments in that it is
1624    used.  */
1625
1626 static void
1627 remove_stmt (tree stmt)
1628 {
1629   tree next, name;
1630
1631   if (TREE_CODE (stmt) == PHI_NODE)
1632     {
1633       name = PHI_RESULT (stmt);
1634       next = single_nonlooparound_use (name);
1635       remove_phi_node (stmt, NULL_TREE, true);
1636
1637       if (!next
1638           || TREE_CODE (next) != GIMPLE_MODIFY_STMT
1639           || GIMPLE_STMT_OPERAND (next, 1) != name)
1640         return;
1641
1642       stmt = next;
1643     }
1644
1645   while (1)
1646     {
1647       block_stmt_iterator bsi;
1648     
1649       bsi = bsi_for_stmt (stmt);
1650
1651       name = GIMPLE_STMT_OPERAND (stmt, 0);
1652       gcc_assert (TREE_CODE (name) == SSA_NAME);
1653
1654       next = single_nonlooparound_use (name);
1655
1656       mark_virtual_ops_for_renaming (stmt);
1657       bsi_remove (&bsi, true);
1658
1659       if (!next
1660           || TREE_CODE (next) != GIMPLE_MODIFY_STMT
1661           || GIMPLE_STMT_OPERAND (next, 1) != name)
1662         return;
1663
1664       stmt = next;
1665     }
1666 }
1667
1668 /* Perform the predictive commoning optimization for a chain CHAIN.
1669    Uids of the newly created temporary variables are marked in TMP_VARS.*/
1670
1671 static void
1672 execute_pred_commoning_chain (struct loop *loop, chain_p chain,
1673                              bitmap tmp_vars)
1674 {
1675   unsigned i;
1676   dref a, root;
1677   tree var;
1678
1679   if (chain->combined)
1680     {
1681       /* For combined chains, just remove the statements that are used to
1682          compute the values of the expression (except for the root one).  */
1683       for (i = 1; VEC_iterate (dref, chain->refs, i, a); i++)
1684         remove_stmt (a->stmt);
1685     }
1686   else
1687     {
1688       /* For non-combined chains, set up the variables that hold its value,
1689          and replace the uses of the original references by these
1690          variables.  */
1691       root = get_chain_root (chain);
1692       mark_virtual_ops_for_renaming (root->stmt);
1693
1694       initialize_root (loop, chain, tmp_vars);
1695       for (i = 1; VEC_iterate (dref, chain->refs, i, a); i++)
1696         {
1697           mark_virtual_ops_for_renaming (a->stmt);
1698           var = VEC_index (tree, chain->vars, chain->length - a->distance);
1699           replace_ref_with (a->stmt, var, false, false);
1700         }
1701     }
1702 }
1703
1704 /* Determines the unroll factor necessary to remove as many temporary variable
1705    copies as possible.  CHAINS is the list of chains that will be
1706    optimized.  */
1707
1708 static unsigned
1709 determine_unroll_factor (VEC (chain_p, heap) *chains)
1710 {
1711   chain_p chain;
1712   unsigned factor = 1, af, nfactor, i;
1713   unsigned max = PARAM_VALUE (PARAM_MAX_UNROLL_TIMES);
1714
1715   for (i = 0; VEC_iterate (chain_p, chains, i, chain); i++)
1716     {
1717       if (chain->type == CT_INVARIANT || chain->combined)
1718         continue;
1719
1720       /* The best unroll factor for this chain is equal to the number of
1721          temporary variables that we create for it.  */
1722       af = chain->length;
1723       if (chain->has_max_use_after)
1724         af++;
1725
1726       nfactor = factor * af / gcd (factor, af);
1727       if (nfactor <= max)
1728         factor = nfactor;
1729     }
1730
1731   return factor;
1732 }
1733
1734 /* Perform the predictive commoning optimization for CHAINS.
1735    Uids of the newly created temporary variables are marked in TMP_VARS.  */
1736
1737 static void
1738 execute_pred_commoning (struct loop *loop, VEC (chain_p, heap) *chains,
1739                         bitmap tmp_vars)
1740 {
1741   chain_p chain;
1742   unsigned i;
1743
1744   for (i = 0; VEC_iterate (chain_p, chains, i, chain); i++)
1745     {
1746       if (chain->type == CT_INVARIANT)
1747         execute_load_motion (loop, chain, tmp_vars);
1748       else
1749         execute_pred_commoning_chain (loop, chain, tmp_vars);
1750     }
1751   
1752   update_ssa (TODO_update_ssa_only_virtuals);
1753 }
1754
1755 /* For each reference in CHAINS, if its defining statement is
1756    ssa name, set it to phi node that defines it.  */
1757
1758 static void
1759 replace_phis_by_defined_names (VEC (chain_p, heap) *chains)
1760 {
1761   chain_p chain;
1762   dref a;
1763   unsigned i, j;
1764
1765   for (i = 0; VEC_iterate (chain_p, chains, i, chain); i++)
1766     for (j = 0; VEC_iterate (dref, chain->refs, j, a); j++)
1767       {
1768         gcc_assert (TREE_CODE (a->stmt) != SSA_NAME);
1769         if (TREE_CODE (a->stmt) == PHI_NODE)
1770           a->stmt = PHI_RESULT (a->stmt);
1771       }
1772 }
1773
1774 /* For each reference in CHAINS, if its defining statement is
1775    phi node, set it to the ssa name that is defined by it.  */
1776
1777 static void
1778 replace_names_by_phis (VEC (chain_p, heap) *chains)
1779 {
1780   chain_p chain;
1781   dref a;
1782   unsigned i, j;
1783
1784   for (i = 0; VEC_iterate (chain_p, chains, i, chain); i++)
1785     for (j = 0; VEC_iterate (dref, chain->refs, j, a); j++)
1786       if (TREE_CODE (a->stmt) == SSA_NAME)
1787         {
1788           a->stmt = SSA_NAME_DEF_STMT (a->stmt);
1789           gcc_assert (TREE_CODE (a->stmt) == PHI_NODE);
1790         }
1791 }
1792
1793 /* Wrapper over execute_pred_commoning, to pass it as a callback
1794    to tree_transform_and_unroll_loop.  */
1795
1796 struct epcc_data
1797 {
1798   VEC (chain_p, heap) *chains;
1799   bitmap tmp_vars;
1800 };
1801
1802 static void
1803 execute_pred_commoning_cbck (struct loop *loop, void *data)
1804 {
1805   struct epcc_data *dta = data;
1806
1807   /* Restore phi nodes that were replaced by ssa names before
1808      tree_transform_and_unroll_loop (see detailed description in
1809      tree_predictive_commoning_loop).  */
1810   replace_names_by_phis (dta->chains);
1811   execute_pred_commoning (loop, dta->chains, dta->tmp_vars);
1812 }
1813
1814 /* Returns true if we can and should unroll LOOP FACTOR times.  Number
1815    of iterations of the loop is returned in NITER.  */
1816
1817 static bool
1818 should_unroll_loop_p (struct loop *loop, unsigned factor,
1819                       struct tree_niter_desc *niter)
1820 {
1821   edge exit;
1822
1823   if (factor == 1)
1824     return false;
1825
1826   /* Check whether unrolling is possible.  We only want to unroll loops
1827      for that we are able to determine number of iterations.  We also
1828      want to split the extra iterations of the loop from its end,
1829      therefore we require that the loop has precisely one
1830      exit.  */
1831
1832   exit = single_dom_exit (loop);
1833   if (!exit)
1834     return false;
1835
1836   if (!number_of_iterations_exit (loop, exit, niter, false))
1837     return false;
1838
1839   /* And of course, we must be able to duplicate the loop.  */
1840   if (!can_duplicate_loop_p (loop))
1841     return false;
1842
1843   /* The final loop should be small enough.  */
1844   if (tree_num_loop_insns (loop, &eni_size_weights) * factor
1845       > (unsigned) PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS))
1846     return false;
1847
1848   return true;
1849 }
1850
1851 /* Base NAME and all the names in the chain of phi nodes that use it
1852    on variable VAR.  The phi nodes are recognized by being in the copies of
1853    the header of the LOOP.  */
1854
1855 static void
1856 base_names_in_chain_on (struct loop *loop, tree name, tree var)
1857 {
1858   tree stmt, phi;
1859   imm_use_iterator iter;
1860   edge e;
1861
1862   SSA_NAME_VAR (name) = var;
1863
1864   while (1)
1865     {
1866       phi = NULL;
1867       FOR_EACH_IMM_USE_STMT (stmt, iter, name)
1868         {
1869           if (TREE_CODE (stmt) == PHI_NODE
1870               && flow_bb_inside_loop_p (loop, bb_for_stmt (stmt)))
1871             {
1872               phi = stmt;
1873               BREAK_FROM_IMM_USE_STMT (iter);
1874             }
1875         }
1876       if (!phi)
1877         return;
1878
1879       if (bb_for_stmt (phi) == loop->header)
1880         e = loop_latch_edge (loop);
1881       else
1882         e = single_pred_edge (bb_for_stmt (stmt));
1883
1884       name = PHI_RESULT (phi);
1885       SSA_NAME_VAR (name) = var;
1886     }
1887 }
1888
1889 /* Given an unrolled LOOP after predictive commoning, remove the
1890    register copies arising from phi nodes by changing the base
1891    variables of SSA names.  TMP_VARS is the set of the temporary variables
1892    for those we want to perform this.  */
1893
1894 static void
1895 eliminate_temp_copies (struct loop *loop, bitmap tmp_vars)
1896 {
1897   edge e;
1898   tree phi, name, use, var, stmt;
1899
1900   e = loop_latch_edge (loop);
1901   for (phi = phi_nodes (loop->header); phi; phi = PHI_CHAIN (phi))
1902     {
1903       name = PHI_RESULT (phi);
1904       var = SSA_NAME_VAR (name);
1905       if (!bitmap_bit_p (tmp_vars, DECL_UID (var)))
1906         continue;
1907       use = PHI_ARG_DEF_FROM_EDGE (phi, e);
1908       gcc_assert (TREE_CODE (use) == SSA_NAME);
1909
1910       /* Base all the ssa names in the ud and du chain of NAME on VAR.  */
1911       stmt = SSA_NAME_DEF_STMT (use);
1912       while (TREE_CODE (stmt) == PHI_NODE)
1913         {
1914           gcc_assert (single_pred_p (bb_for_stmt (stmt)));
1915           use = PHI_ARG_DEF (stmt, 0);
1916           stmt = SSA_NAME_DEF_STMT (use);
1917         }
1918
1919       base_names_in_chain_on (loop, use, var);
1920     }
1921 }
1922
1923 /* Returns true if CHAIN is suitable to be combined.  */
1924
1925 static bool
1926 chain_can_be_combined_p (chain_p chain)
1927 {
1928   return (!chain->combined
1929           && (chain->type == CT_LOAD || chain->type == CT_COMBINATION));
1930 }
1931
1932 /* Returns the modify statement that uses NAME.  Skips over assignment
1933    statements, NAME is replaced with the actual name used in the returned
1934    statement.  */
1935
1936 static tree
1937 find_use_stmt (tree *name)
1938 {
1939   tree stmt, rhs, lhs;
1940
1941   /* Skip over assignments.  */
1942   while (1)
1943     {
1944       stmt = single_nonlooparound_use (*name);
1945       if (!stmt)
1946         return NULL_TREE;
1947
1948       if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT)
1949         return NULL_TREE;
1950
1951       lhs = GIMPLE_STMT_OPERAND (stmt, 0);
1952       if (TREE_CODE (lhs) != SSA_NAME)
1953         return NULL_TREE;
1954
1955       rhs = GIMPLE_STMT_OPERAND (stmt, 1);
1956       if (rhs != *name)
1957         break;
1958
1959       *name = lhs;
1960     }
1961
1962   if (!EXPR_P (rhs)
1963       || REFERENCE_CLASS_P (rhs)
1964       || TREE_CODE_LENGTH (TREE_CODE (rhs)) != 2)
1965     return NULL_TREE;
1966
1967   return stmt;
1968 }
1969
1970 /* Returns true if we may perform reassociation for operation CODE in TYPE.  */
1971
1972 static bool
1973 may_reassociate_p (tree type, enum tree_code code)
1974 {
1975   if (FLOAT_TYPE_P (type)
1976       && !flag_unsafe_math_optimizations)
1977     return false;
1978
1979   return (commutative_tree_code (code)
1980           && associative_tree_code (code));
1981 }
1982
1983 /* If the operation used in STMT is associative and commutative, go through the
1984    tree of the same operations and returns its root.  Distance to the root
1985    is stored in DISTANCE.  */
1986
1987 static tree
1988 find_associative_operation_root (tree stmt, unsigned *distance)
1989 {
1990   tree rhs = GIMPLE_STMT_OPERAND (stmt, 1), lhs, next;
1991   enum tree_code code = TREE_CODE (rhs);
1992   unsigned dist = 0;
1993
1994   if (!may_reassociate_p (TREE_TYPE (rhs), code))
1995     return NULL_TREE;
1996
1997   while (1)
1998     {
1999       lhs = GIMPLE_STMT_OPERAND (stmt, 0);
2000       gcc_assert (TREE_CODE (lhs) == SSA_NAME);
2001
2002       next = find_use_stmt (&lhs);
2003       if (!next)
2004         break;
2005
2006       rhs = GIMPLE_STMT_OPERAND (next, 1);
2007       if (TREE_CODE (rhs) != code)
2008         break;
2009
2010       stmt = next;
2011       dist++;
2012     }
2013
2014   if (distance)
2015     *distance = dist;
2016   return stmt;
2017 }
2018
2019 /* Returns the common statement in that NAME1 and NAME2 have a use.  If there
2020    is no such statement, returns NULL_TREE.  In case the operation used on
2021    NAME1 and NAME2 is associative and commutative, returns the root of the
2022    tree formed by this operation instead of the statement that uses NAME1 or
2023    NAME2.  */
2024
2025 static tree
2026 find_common_use_stmt (tree *name1, tree *name2)
2027 {
2028   tree stmt1, stmt2;
2029
2030   stmt1 = find_use_stmt (name1);
2031   if (!stmt1)
2032     return NULL_TREE;
2033
2034   stmt2 = find_use_stmt (name2);
2035   if (!stmt2)
2036     return NULL_TREE;
2037
2038   if (stmt1 == stmt2)
2039     return stmt1;
2040
2041   stmt1 = find_associative_operation_root (stmt1, NULL);
2042   if (!stmt1)
2043     return NULL_TREE;
2044   stmt2 = find_associative_operation_root (stmt2, NULL);
2045   if (!stmt2)
2046     return NULL_TREE;
2047
2048   return (stmt1 == stmt2 ? stmt1 : NULL_TREE);
2049 }
2050
2051 /* Checks whether R1 and R2 are combined together using CODE, with the result
2052    in RSLT_TYPE, in order R1 CODE R2 if SWAP is false and in order R2 CODE R1
2053    if it is true.  If CODE is ERROR_MARK, set these values instead.  */
2054
2055 static bool
2056 combinable_refs_p (dref r1, dref r2,
2057                    enum tree_code *code, bool *swap, tree *rslt_type)
2058 {
2059   enum tree_code acode;
2060   bool aswap;
2061   tree atype;
2062   tree name1, name2, stmt, rhs;
2063
2064   name1 = name_for_ref (r1);
2065   name2 = name_for_ref (r2);
2066   gcc_assert (name1 != NULL_TREE && name2 != NULL_TREE);
2067
2068   stmt = find_common_use_stmt (&name1, &name2);
2069
2070   if (!stmt)
2071     return false;
2072
2073   rhs = GIMPLE_STMT_OPERAND (stmt, 1);
2074   acode = TREE_CODE (rhs);
2075   aswap = (!commutative_tree_code (acode)
2076            && TREE_OPERAND (rhs, 0) != name1);
2077   atype = TREE_TYPE (rhs);
2078
2079   if (*code == ERROR_MARK)
2080     {
2081       *code = acode;
2082       *swap = aswap;
2083       *rslt_type = atype;
2084       return true;
2085     }
2086
2087   return (*code == acode
2088           && *swap == aswap
2089           && *rslt_type == atype);
2090 }
2091
2092 /* Remove OP from the operation on rhs of STMT, and replace STMT with
2093    an assignment of the remaining operand.  */
2094
2095 static void
2096 remove_name_from_operation (tree stmt, tree op)
2097 {
2098   tree *rhs;
2099
2100   gcc_assert (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT);
2101
2102   rhs = &GIMPLE_STMT_OPERAND (stmt, 1);
2103   if (TREE_OPERAND (*rhs, 0) == op)
2104     *rhs = TREE_OPERAND (*rhs, 1);
2105   else if (TREE_OPERAND (*rhs, 1) == op)
2106     *rhs = TREE_OPERAND (*rhs, 0);
2107   else
2108     gcc_unreachable ();
2109   update_stmt (stmt);
2110 }
2111
2112 /* Reassociates the expression in that NAME1 and NAME2 are used so that they
2113    are combined in a single statement, and returns this statement.  */
2114
2115 static tree
2116 reassociate_to_the_same_stmt (tree name1, tree name2)
2117 {
2118   tree stmt1, stmt2, root1, root2, r1, r2, s1, s2;
2119   tree new_stmt, tmp_stmt, new_name, tmp_name, var;
2120   unsigned dist1, dist2;
2121   enum tree_code code;
2122   tree type = TREE_TYPE (name1);
2123   block_stmt_iterator bsi;
2124
2125   stmt1 = find_use_stmt (&name1);
2126   stmt2 = find_use_stmt (&name2);
2127   root1 = find_associative_operation_root (stmt1, &dist1);
2128   root2 = find_associative_operation_root (stmt2, &dist2);
2129   code = TREE_CODE (GIMPLE_STMT_OPERAND (stmt1, 1));
2130
2131   gcc_assert (root1 && root2 && root1 == root2
2132               && code == TREE_CODE (GIMPLE_STMT_OPERAND (stmt2, 1)));
2133
2134   /* Find the root of the nearest expression in that both NAME1 and NAME2
2135      are used.  */
2136   r1 = name1;
2137   s1 = stmt1;
2138   r2 = name2;
2139   s2 = stmt2;
2140
2141   while (dist1 > dist2)
2142     {
2143       s1 = find_use_stmt (&r1);
2144       r1 = GIMPLE_STMT_OPERAND (s1, 0);
2145       dist1--;
2146     }
2147   while (dist2 > dist1)
2148     {
2149       s2 = find_use_stmt (&r2);
2150       r2 = GIMPLE_STMT_OPERAND (s2, 0);
2151       dist2--;
2152     }
2153
2154   while (s1 != s2)
2155     {
2156       s1 = find_use_stmt (&r1);
2157       r1 = GIMPLE_STMT_OPERAND (s1, 0);
2158       s2 = find_use_stmt (&r2);
2159       r2 = GIMPLE_STMT_OPERAND (s2, 0);
2160     }
2161
2162   /* Remove NAME1 and NAME2 from the statements in that they are used
2163      currently.  */
2164   remove_name_from_operation (stmt1, name1);
2165   remove_name_from_operation (stmt2, name2);
2166
2167   /* Insert the new statement combining NAME1 and NAME2 before S1, and
2168      combine it with the rhs of S1.  */
2169   var = create_tmp_var (type, "predreastmp");
2170   add_referenced_var (var);
2171   new_name = make_ssa_name (var, NULL_TREE);
2172   new_stmt = build_gimple_modify_stmt_stat (new_name,
2173                             fold_build2 (code, type, name1, name2));
2174   SSA_NAME_DEF_STMT (new_name) = new_stmt;
2175
2176   var = create_tmp_var (type, "predreastmp");
2177   add_referenced_var (var);
2178   tmp_name = make_ssa_name (var, NULL_TREE);
2179   tmp_stmt = build_gimple_modify_stmt_stat (tmp_name,
2180                                             GIMPLE_STMT_OPERAND (s1, 1));
2181   SSA_NAME_DEF_STMT (tmp_name) = tmp_stmt;
2182
2183   GIMPLE_STMT_OPERAND (s1, 1) = fold_build2 (code, type, new_name, tmp_name);
2184   update_stmt (s1);
2185
2186   bsi = bsi_for_stmt (s1);
2187   bsi_insert_before (&bsi, new_stmt, BSI_SAME_STMT);
2188   bsi_insert_before (&bsi, tmp_stmt, BSI_SAME_STMT);
2189
2190   return new_stmt;
2191 }
2192
2193 /* Returns the statement that combines references R1 and R2.  In case R1
2194    and R2 are not used in the same statement, but they are used with an
2195    associative and commutative operation in the same expression, reassociate
2196    the expression so that they are used in the same statement.  */
2197
2198 static tree
2199 stmt_combining_refs (dref r1, dref r2)
2200 {
2201   tree stmt1, stmt2;
2202   tree name1 = name_for_ref (r1);
2203   tree name2 = name_for_ref (r2);
2204
2205   stmt1 = find_use_stmt (&name1);
2206   stmt2 = find_use_stmt (&name2);
2207   if (stmt1 == stmt2)
2208     return stmt1;
2209
2210   return reassociate_to_the_same_stmt (name1, name2);
2211 }
2212
2213 /* Tries to combine chains CH1 and CH2 together.  If this succeeds, the
2214    description of the new chain is returned, otherwise we return NULL.  */
2215
2216 static chain_p
2217 combine_chains (chain_p ch1, chain_p ch2)
2218 {
2219   dref r1, r2, nw;
2220   enum tree_code op = ERROR_MARK;
2221   bool swap = false;
2222   chain_p new_chain;
2223   unsigned i;
2224   tree root_stmt;
2225   tree rslt_type = NULL_TREE;
2226
2227   if (ch1 == ch2)
2228     return false;
2229   if (ch1->length != ch2->length)
2230     return NULL;
2231
2232   if (VEC_length (dref, ch1->refs) != VEC_length (dref, ch2->refs))
2233     return NULL;
2234
2235   for (i = 0; (VEC_iterate (dref, ch1->refs, i, r1)
2236                && VEC_iterate (dref, ch2->refs, i, r2)); i++)
2237     {
2238       if (r1->distance != r2->distance)
2239         return NULL;
2240
2241       if (!combinable_refs_p (r1, r2, &op, &swap, &rslt_type))
2242         return NULL;
2243     }
2244
2245   if (swap)
2246     {
2247       chain_p tmp = ch1;
2248       ch1 = ch2;
2249       ch2 = tmp;
2250     }
2251
2252   new_chain = XCNEW (struct chain);
2253   new_chain->type = CT_COMBINATION;
2254   new_chain->operator = op;
2255   new_chain->ch1 = ch1;
2256   new_chain->ch2 = ch2;
2257   new_chain->rslt_type = rslt_type;
2258   new_chain->length = ch1->length;
2259
2260   for (i = 0; (VEC_iterate (dref, ch1->refs, i, r1)
2261                && VEC_iterate (dref, ch2->refs, i, r2)); i++)
2262     {
2263       nw = XCNEW (struct dref);
2264       nw->stmt = stmt_combining_refs (r1, r2);
2265       nw->distance = r1->distance;
2266
2267       VEC_safe_push (dref, heap, new_chain->refs, nw);
2268     }
2269
2270   new_chain->has_max_use_after = false;
2271   root_stmt = get_chain_root (new_chain)->stmt;
2272   for (i = 1; VEC_iterate (dref, new_chain->refs, i, nw); i++)
2273     {
2274       if (nw->distance == new_chain->length
2275           && !stmt_dominates_stmt_p (nw->stmt, root_stmt))
2276         {
2277           new_chain->has_max_use_after = true;
2278           break;
2279         }
2280     }
2281
2282   ch1->combined = true;
2283   ch2->combined = true;
2284   return new_chain;
2285 }
2286
2287 /* Try to combine the CHAINS.  */
2288
2289 static void
2290 try_combine_chains (VEC (chain_p, heap) **chains)
2291 {
2292   unsigned i, j;
2293   chain_p ch1, ch2, cch;
2294   VEC (chain_p, heap) *worklist = NULL;
2295
2296   for (i = 0; VEC_iterate (chain_p, *chains, i, ch1); i++)
2297     if (chain_can_be_combined_p (ch1))
2298       VEC_safe_push (chain_p, heap, worklist, ch1);
2299
2300   while (!VEC_empty (chain_p, worklist))
2301     {
2302       ch1 = VEC_pop (chain_p, worklist);
2303       if (!chain_can_be_combined_p (ch1))
2304         continue;
2305
2306       for (j = 0; VEC_iterate (chain_p, *chains, j, ch2); j++)
2307         {
2308           if (!chain_can_be_combined_p (ch2))
2309             continue;
2310
2311           cch = combine_chains (ch1, ch2);
2312           if (cch)
2313             {
2314               VEC_safe_push (chain_p, heap, worklist, cch);
2315               VEC_safe_push (chain_p, heap, *chains, cch);
2316               break;
2317             }
2318         }
2319     }
2320 }
2321
2322 /* Sets alias information based on data reference DR for REF,
2323    if necessary.  */
2324
2325 static void
2326 set_alias_info (tree ref, struct data_reference *dr)
2327 {
2328   tree var;
2329   tree tag = DR_SYMBOL_TAG (dr);
2330
2331   gcc_assert (tag != NULL_TREE);
2332
2333   ref = get_base_address (ref);
2334   if (!ref || !INDIRECT_REF_P (ref))
2335     return;
2336
2337   var = SSA_NAME_VAR (TREE_OPERAND (ref, 0));
2338   if (var_ann (var)->symbol_mem_tag)
2339     return;
2340
2341   if (!MTAG_P (tag))
2342     new_type_alias (var, tag, ref);
2343   else
2344     var_ann (var)->symbol_mem_tag = tag;
2345
2346   var_ann (var)->subvars = DR_SUBVARS (dr);
2347 }
2348
2349 /* Prepare initializers for CHAIN in LOOP.  Returns false if this is
2350    impossible because one of these initializers may trap, true otherwise.  */
2351
2352 static bool
2353 prepare_initializers_chain (struct loop *loop, chain_p chain)
2354 {
2355   unsigned i, n = (chain->type == CT_INVARIANT) ? 1 : chain->length;
2356   struct data_reference *dr = get_chain_root (chain)->ref;
2357   tree init, stmts;
2358   dref laref;
2359   edge entry = loop_preheader_edge (loop);
2360
2361   /* Find the initializers for the variables, and check that they cannot
2362      trap.  */
2363   chain->inits = VEC_alloc (tree, heap, n);
2364   for (i = 0; i < n; i++)
2365     VEC_quick_push (tree, chain->inits, NULL_TREE);
2366
2367   /* If we have replaced some looparound phi nodes, use their initializers
2368      instead of creating our own.  */
2369   for (i = 0; VEC_iterate (dref, chain->refs, i, laref); i++)
2370     {
2371       if (TREE_CODE (laref->stmt) != PHI_NODE)
2372         continue;
2373
2374       gcc_assert (laref->distance > 0);
2375       VEC_replace (tree, chain->inits, n - laref->distance,
2376                    PHI_ARG_DEF_FROM_EDGE (laref->stmt, entry));
2377     }
2378
2379   for (i = 0; i < n; i++)
2380     {
2381       if (VEC_index (tree, chain->inits, i) != NULL_TREE)
2382         continue;
2383
2384       init = ref_at_iteration (loop, DR_REF (dr), (int) i - n);
2385       if (!init)
2386         return false;
2387       
2388       if (!chain->all_always_accessed && tree_could_trap_p (init))
2389         return false;
2390
2391       init = force_gimple_operand (init, &stmts, false, NULL_TREE);
2392       if (stmts)
2393         {
2394           mark_virtual_ops_for_renaming_list (stmts);
2395           bsi_insert_on_edge_immediate (entry, stmts);
2396         }
2397       set_alias_info (init, dr);
2398
2399       VEC_replace (tree, chain->inits, i, init);
2400     }
2401
2402   return true;
2403 }
2404
2405 /* Prepare initializers for CHAINS in LOOP, and free chains that cannot
2406    be used because the initializers might trap.  */
2407
2408 static void
2409 prepare_initializers (struct loop *loop, VEC (chain_p, heap) *chains)
2410 {
2411   chain_p chain;
2412   unsigned i;
2413
2414   for (i = 0; i < VEC_length (chain_p, chains); )
2415     {
2416       chain = VEC_index (chain_p, chains, i);
2417       if (prepare_initializers_chain (loop, chain))
2418         i++;
2419       else
2420         {
2421           release_chain (chain);
2422           VEC_unordered_remove (chain_p, chains, i);
2423         }
2424     }
2425 }
2426
2427 /* Performs predictive commoning for LOOP.  Returns true if LOOP was
2428    unrolled.  */
2429
2430 static bool
2431 tree_predictive_commoning_loop (struct loop *loop)
2432 {
2433   VEC (data_reference_p, heap) *datarefs;
2434   VEC (ddr_p, heap) *dependences;
2435   struct component *components;
2436   VEC (chain_p, heap) *chains = NULL;
2437   unsigned unroll_factor;
2438   struct tree_niter_desc desc;
2439   bool unroll = false;
2440   edge exit;
2441   bitmap tmp_vars;
2442
2443   if (dump_file && (dump_flags & TDF_DETAILS))
2444     fprintf (dump_file, "Processing loop %d\n",  loop->num);
2445
2446   /* Find the data references and split them into components according to their
2447      dependence relations.  */
2448   datarefs = VEC_alloc (data_reference_p, heap, 10);
2449   dependences = VEC_alloc (ddr_p, heap, 10);
2450   compute_data_dependences_for_loop (loop, true, &datarefs, &dependences);
2451   if (dump_file && (dump_flags & TDF_DETAILS))
2452     dump_data_dependence_relations (dump_file, dependences);
2453
2454   components = split_data_refs_to_components (loop, datarefs, dependences);
2455   free_dependence_relations (dependences);
2456   if (!components)
2457     {
2458       free_data_refs (datarefs);
2459       return false;
2460     }
2461
2462   if (dump_file && (dump_flags & TDF_DETAILS))
2463     {
2464       fprintf (dump_file, "Initial state:\n\n");
2465       dump_components (dump_file, components);
2466     }
2467
2468   /* Find the suitable components and split them into chains.  */
2469   components = filter_suitable_components (loop, components);
2470
2471   tmp_vars = BITMAP_ALLOC (NULL);
2472   looparound_phis = BITMAP_ALLOC (NULL);
2473   determine_roots (loop, components, &chains);
2474   release_components (components);
2475
2476   if (!chains)
2477     {
2478       if (dump_file && (dump_flags & TDF_DETAILS))
2479         fprintf (dump_file,
2480                  "Predictive commoning failed: no suitable chains\n");
2481       goto end;
2482     }
2483   prepare_initializers (loop, chains);
2484
2485   /* Try to combine the chains that are always worked with together.  */
2486   try_combine_chains (&chains);
2487
2488   if (dump_file && (dump_flags & TDF_DETAILS))
2489     {
2490       fprintf (dump_file, "Before commoning:\n\n");
2491       dump_chains (dump_file, chains);
2492     }
2493
2494   /* Determine the unroll factor, and if the loop should be unrolled, ensure
2495      that its number of iterations is divisible by the factor.  */
2496   unroll_factor = determine_unroll_factor (chains);
2497   scev_reset ();
2498   unroll = should_unroll_loop_p (loop, unroll_factor, &desc);
2499   exit = single_dom_exit (loop);
2500
2501   /* Execute the predictive commoning transformations, and possibly unroll the
2502      loop.  */
2503   if (unroll)
2504     {
2505       struct epcc_data dta;
2506
2507       if (dump_file && (dump_flags & TDF_DETAILS))
2508         fprintf (dump_file, "Unrolling %u times.\n", unroll_factor);
2509
2510       dta.chains = chains;
2511       dta.tmp_vars = tmp_vars;
2512       
2513       update_ssa (TODO_update_ssa_only_virtuals);
2514
2515       /* Cfg manipulations performed in tree_transform_and_unroll_loop before
2516          execute_pred_commoning_cbck is called may cause phi nodes to be
2517          reallocated, which is a problem since CHAINS may point to these
2518          statements.  To fix this, we store the ssa names defined by the
2519          phi nodes here instead of the phi nodes themselves, and restore
2520          the phi nodes in execute_pred_commoning_cbck.  A bit hacky.  */
2521       replace_phis_by_defined_names (chains);
2522
2523       tree_transform_and_unroll_loop (loop, unroll_factor, exit, &desc,
2524                                       execute_pred_commoning_cbck, &dta);
2525       eliminate_temp_copies (loop, tmp_vars);
2526     }
2527   else
2528     {
2529       if (dump_file && (dump_flags & TDF_DETAILS))
2530         fprintf (dump_file,
2531                  "Executing predictive commoning without unrolling.\n");
2532       execute_pred_commoning (loop, chains, tmp_vars);
2533     }
2534
2535 end: ;
2536   release_chains (chains);
2537   free_data_refs (datarefs);
2538   BITMAP_FREE (tmp_vars);
2539   BITMAP_FREE (looparound_phis);
2540
2541   free_affine_expand_cache (&name_expansions);
2542
2543   return unroll;
2544 }
2545
2546 /* Runs predictive commoning.  */
2547
2548 void
2549 tree_predictive_commoning (void)
2550 {
2551   bool unrolled = false;
2552   struct loop *loop;
2553   loop_iterator li;
2554
2555   initialize_original_copy_tables ();
2556   FOR_EACH_LOOP (li, loop, LI_ONLY_INNERMOST)
2557     {
2558       unrolled |= tree_predictive_commoning_loop (loop);
2559     }
2560
2561   if (unrolled)
2562     {
2563       scev_reset ();
2564       cleanup_tree_cfg_loop ();
2565     }
2566   free_original_copy_tables ();
2567 }