OSDN Git Service

* bitmap.h (EXECUTE_IF_SET_IN_BITMAP, EXECUTE_IF_AND_COMPL_IN_BITMAP,
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-loop-ivopts.c
1 /* Induction variable optimizations.
2    Copyright (C) 2003, 2004 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, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.  */
20
21 /* This pass tries to find the optimal set of induction variables for the loop.
22    It optimizes just the basic linear induction variables (although adding
23    support for other types should not be too hard).  It includes the
24    optimizations commonly known as strength reduction, induction variable
25    coalescing and induction variable elimination.  It does it in the
26    following steps:
27
28    1) The interesting uses of induction variables are found.  This includes
29
30       -- uses of induction variables in non-linear expressions
31       -- addresses of arrays
32       -- comparisons of induction variables
33
34    2) Candidates for the induction variables are found.  This includes
35
36       -- old induction variables
37       -- the variables defined by expressions derived from the "interesting
38          uses" above
39
40    3) The optimal (w.r. to a cost function) set of variables is chosen.  The
41       cost function assigns a cost to sets of induction variables and consists
42       of three parts:
43
44       -- The use costs.  Each of the interesting uses chooses the best induction
45          variable in the set and adds its cost to the sum.  The cost reflects
46          the time spent on modifying the induction variables value to be usable
47          for the given purpose (adding base and offset for arrays, etc.).
48       -- The variable costs.  Each of the variables has a cost assigned that
49          reflects the costs associated with incrementing the value of the
50          variable.  The original variables are somewhat preferred.
51       -- The set cost.  Depending on the size of the set, extra cost may be
52          added to reflect register pressure.
53
54       All the costs are defined in a machine-specific way, using the target
55       hooks and machine descriptions to determine them.
56
57    4) The trees are transformed to use the new variables, the dead code is
58       removed.
59    
60    All of this is done loop by loop.  Doing it globally is theoretically
61    possible, it might give a better performance and it might enable us
62    to decide costs more precisely, but getting all the interactions right
63    would be complicated.  */
64
65 #include "config.h"
66 #include "system.h"
67 #include "coretypes.h"
68 #include "tm.h"
69 #include "tree.h"
70 #include "rtl.h"
71 #include "tm_p.h"
72 #include "hard-reg-set.h"
73 #include "basic-block.h"
74 #include "output.h"
75 #include "diagnostic.h"
76 #include "tree-flow.h"
77 #include "tree-dump.h"
78 #include "timevar.h"
79 #include "cfgloop.h"
80 #include "varray.h"
81 #include "expr.h"
82 #include "tree-pass.h"
83 #include "ggc.h"
84 #include "insn-config.h"
85 #include "recog.h"
86 #include "hashtab.h"
87 #include "tree-chrec.h"
88 #include "tree-scalar-evolution.h"
89 #include "cfgloop.h"
90 #include "params.h"
91
92 /* The infinite cost.  */
93 #define INFTY 10000000
94
95 /* The expected number of loop iterations.  TODO -- use profiling instead of
96    this.  */
97 #define AVG_LOOP_NITER(LOOP) 5
98
99 /* Just to shorten the ugly names.  */
100 #define EXEC_BINARY nondestructive_fold_binary_to_constant
101 #define EXEC_UNARY nondestructive_fold_unary_to_constant
102
103 /* Representation of the induction variable.  */
104 struct iv
105 {
106   tree base;            /* Initial value of the iv.  */
107   tree step;            /* Step of the iv (constant only).  */
108   tree ssa_name;        /* The ssa name with the value.  */
109   bool biv_p;           /* Is it a biv?  */
110   bool have_use_for;    /* Do we already have a use for it?  */
111   unsigned use_id;      /* The identifier in the use if it is the case.  */
112 };
113
114 /* Per-ssa version information (induction variable descriptions, etc.).  */
115 struct version_info
116 {
117   tree name;            /* The ssa name.  */
118   struct iv *iv;        /* Induction variable description.  */
119   bool has_nonlin_use;  /* For a loop-level invariant, whether it is used in
120                            an expression that is not an induction variable.  */
121   unsigned inv_id;      /* Id of an invariant.  */
122   bool preserve_biv;    /* For the original biv, whether to preserve it.  */
123 };
124
125 /* Information attached to loop.  */
126 struct loop_data
127 {
128   struct tree_niter_desc niter;
129                         /* Number of iterations.  */
130
131   unsigned regs_used;   /* Number of registers used.  */
132 };
133
134 /* Types of uses.  */
135 enum use_type
136 {
137   USE_NONLINEAR_EXPR,   /* Use in a nonlinear expression.  */
138   USE_OUTER,            /* The induction variable is used outside the loop.  */
139   USE_ADDRESS,          /* Use in an address.  */
140   USE_COMPARE           /* Use is a compare.  */
141 };
142
143 /* The candidate - cost pair.  */
144 struct cost_pair
145 {
146   struct iv_cand *cand; /* The candidate.  */
147   unsigned cost;        /* The cost.  */
148   bitmap depends_on;    /* The list of invariants that have to be
149                            preserved.  */
150 };
151
152 /* Use.  */
153 struct iv_use
154 {
155   unsigned id;          /* The id of the use.  */
156   enum use_type type;   /* Type of the use.  */
157   struct iv *iv;        /* The induction variable it is based on.  */
158   tree stmt;            /* Statement in that it occurs.  */
159   tree *op_p;           /* The place where it occurs.  */
160   bitmap related_cands; /* The set of "related" iv candidates.  */
161
162   unsigned n_map_members; /* Number of candidates in the cost_map list.  */
163   struct cost_pair *cost_map;
164                         /* The costs wrto the iv candidates.  */
165
166   struct iv_cand *selected;
167                         /* The selected candidate.  */
168 };
169
170 /* The position where the iv is computed.  */
171 enum iv_position
172 {
173   IP_NORMAL,            /* At the end, just before the exit condition.  */
174   IP_END,               /* At the end of the latch block.  */
175   IP_ORIGINAL           /* The original biv.  */
176 };
177
178 /* The induction variable candidate.  */
179 struct iv_cand
180 {
181   unsigned id;          /* The number of the candidate.  */
182   bool important;       /* Whether this is an "important" candidate, i.e. such
183                            that it should be considered by all uses.  */
184   enum iv_position pos; /* Where it is computed.  */
185   tree incremented_at;  /* For original biv, the statement where it is
186                            incremented.  */
187   tree var_before;      /* The variable used for it before increment.  */
188   tree var_after;       /* The variable used for it after increment.  */
189   struct iv *iv;        /* The value of the candidate.  NULL for
190                            "pseudocandidate" used to indicate the possibility
191                            to replace the final value of an iv by direct
192                            computation of the value.  */
193   unsigned cost;        /* Cost of the candidate.  */
194 };
195
196 /* The data used by the induction variable optimizations.  */
197
198 struct ivopts_data
199 {
200   /* The currently optimized loop.  */
201   struct loop *current_loop;
202
203   /* The size of version_info array allocated.  */
204   unsigned version_info_size;
205
206   /* The array of information for the ssa names.  */
207   struct version_info *version_info;
208
209   /* The bitmap of indices in version_info whose value was changed.  */
210   bitmap relevant;
211
212   /* The maximum invariant id.  */
213   unsigned max_inv_id;
214
215   /* The uses of induction variables.  */
216   varray_type iv_uses;
217
218   /* The candidates.  */
219   varray_type iv_candidates;
220
221   /* Whether to consider just related and important candidates when replacing a
222      use.  */
223   bool consider_all_candidates;
224 };
225
226 /* Bound on number of candidates below that all candidates are considered.  */
227
228 #define CONSIDER_ALL_CANDIDATES_BOUND \
229   ((unsigned) PARAM_VALUE (PARAM_IV_CONSIDER_ALL_CANDIDATES_BOUND))
230
231 /* If there are more iv occurrences, we just give up (it is quite unlikely that
232    optimizing such a loop would help, and it would take ages).  */
233
234 #define MAX_CONSIDERED_USES \
235   ((unsigned) PARAM_VALUE (PARAM_IV_MAX_CONSIDERED_USES))
236
237 /* The list of trees for that the decl_rtl field must be reset is stored
238    here.  */
239
240 static varray_type decl_rtl_to_reset;
241
242 /* Number of uses recorded in DATA.  */
243
244 static inline unsigned
245 n_iv_uses (struct ivopts_data *data)
246 {
247   return VARRAY_ACTIVE_SIZE (data->iv_uses);
248 }
249
250 /* Ith use recorded in DATA.  */
251
252 static inline struct iv_use *
253 iv_use (struct ivopts_data *data, unsigned i)
254 {
255   return VARRAY_GENERIC_PTR_NOGC (data->iv_uses, i);
256 }
257
258 /* Number of candidates recorded in DATA.  */
259
260 static inline unsigned
261 n_iv_cands (struct ivopts_data *data)
262 {
263   return VARRAY_ACTIVE_SIZE (data->iv_candidates);
264 }
265
266 /* Ith candidate recorded in DATA.  */
267
268 static inline struct iv_cand *
269 iv_cand (struct ivopts_data *data, unsigned i)
270 {
271   return VARRAY_GENERIC_PTR_NOGC (data->iv_candidates, i);
272 }
273
274 /* The data for LOOP.  */
275
276 static inline struct loop_data *
277 loop_data (struct loop *loop)
278 {
279   return loop->aux;
280 }
281
282 /* The single loop exit if it dominates the latch, NULL otherwise.  */
283
284 static edge
285 single_dom_exit (struct loop *loop)
286 {
287   edge exit = loop->single_exit;
288
289   if (!exit)
290     return NULL;
291
292   if (!just_once_each_iteration_p (loop, exit->src))
293     return NULL;
294
295   return exit;
296 }
297
298 /* Dumps information about the induction variable IV to FILE.  */
299
300 extern void dump_iv (FILE *, struct iv *);
301 void
302 dump_iv (FILE *file, struct iv *iv)
303 {
304   fprintf (file, "ssa name ");
305   print_generic_expr (file, iv->ssa_name, TDF_SLIM);
306   fprintf (file, "\n");
307
308   fprintf (file, "  type ");
309   print_generic_expr (file, TREE_TYPE (iv->base), TDF_SLIM);
310   fprintf (file, "\n");
311
312   if (iv->step)
313     {
314       fprintf (file, "  base ");
315       print_generic_expr (file, iv->base, TDF_SLIM);
316       fprintf (file, "\n");
317
318       fprintf (file, "  step ");
319       print_generic_expr (file, iv->step, TDF_SLIM);
320       fprintf (file, "\n");
321     }
322   else
323     {
324       fprintf (file, "  invariant ");
325       print_generic_expr (file, iv->base, TDF_SLIM);
326       fprintf (file, "\n");
327     }
328
329   if (iv->biv_p)
330     fprintf (file, "  is a biv\n");
331 }
332
333 /* Dumps information about the USE to FILE.  */
334
335 extern void dump_use (FILE *, struct iv_use *);
336 void
337 dump_use (FILE *file, struct iv_use *use)
338 {
339   struct iv *iv = use->iv;
340
341   fprintf (file, "use %d\n", use->id);
342
343   switch (use->type)
344     {
345     case USE_NONLINEAR_EXPR:
346       fprintf (file, "  generic\n");
347       break;
348
349     case USE_OUTER:
350       fprintf (file, "  outside\n");
351       break;
352
353     case USE_ADDRESS:
354       fprintf (file, "  address\n");
355       break;
356
357     case USE_COMPARE:
358       fprintf (file, "  compare\n");
359       break;
360
361     default:
362       gcc_unreachable ();
363     }
364
365   fprintf (file, "  in statement ");
366   print_generic_expr (file, use->stmt, TDF_SLIM);
367   fprintf (file, "\n");
368
369   fprintf (file, "  at position ");
370   if (use->op_p)
371     print_generic_expr (file, *use->op_p, TDF_SLIM);
372   fprintf (file, "\n");
373
374   fprintf (file, "  type ");
375   print_generic_expr (file, TREE_TYPE (iv->base), TDF_SLIM);
376   fprintf (file, "\n");
377
378   if (iv->step)
379     {
380       fprintf (file, "  base ");
381       print_generic_expr (file, iv->base, TDF_SLIM);
382       fprintf (file, "\n");
383
384       fprintf (file, "  step ");
385       print_generic_expr (file, iv->step, TDF_SLIM);
386       fprintf (file, "\n");
387     }
388   else
389     {
390       fprintf (file, "  invariant ");
391       print_generic_expr (file, iv->base, TDF_SLIM);
392       fprintf (file, "\n");
393     }
394
395   fprintf (file, "  related candidates ");
396   dump_bitmap (file, use->related_cands);
397 }
398
399 /* Dumps information about the uses to FILE.  */
400
401 extern void dump_uses (FILE *, struct ivopts_data *);
402 void
403 dump_uses (FILE *file, struct ivopts_data *data)
404 {
405   unsigned i;
406   struct iv_use *use;
407
408   for (i = 0; i < n_iv_uses (data); i++)
409     {
410       use = iv_use (data, i);
411
412       dump_use (file, use);
413       fprintf (file, "\n");
414     }
415 }
416
417 /* Dumps information about induction variable candidate CAND to FILE.  */
418
419 extern void dump_cand (FILE *, struct iv_cand *);
420 void
421 dump_cand (FILE *file, struct iv_cand *cand)
422 {
423   struct iv *iv = cand->iv;
424
425   fprintf (file, "candidate %d%s\n",
426            cand->id, cand->important ? " (important)" : "");
427
428   if (!iv)
429     {
430       fprintf (file, "  final value replacement\n");
431       return;
432     }
433
434   switch (cand->pos)
435     {
436     case IP_NORMAL:
437       fprintf (file, "  incremented before exit test\n");
438       break;
439
440     case IP_END:
441       fprintf (file, "  incremented at end\n");
442       break;
443
444     case IP_ORIGINAL:
445       fprintf (file, "  original biv\n");
446       break;
447     }
448
449   fprintf (file, "  type ");
450   print_generic_expr (file, TREE_TYPE (iv->base), TDF_SLIM);
451   fprintf (file, "\n");
452
453   if (iv->step)
454     {
455       fprintf (file, "  base ");
456       print_generic_expr (file, iv->base, TDF_SLIM);
457       fprintf (file, "\n");
458
459       fprintf (file, "  step ");
460       print_generic_expr (file, iv->step, TDF_SLIM);
461       fprintf (file, "\n");
462     }
463   else
464     {
465       fprintf (file, "  invariant ");
466       print_generic_expr (file, iv->base, TDF_SLIM);
467       fprintf (file, "\n");
468     }
469 }
470
471 /* Returns the info for ssa version VER.  */
472
473 static inline struct version_info *
474 ver_info (struct ivopts_data *data, unsigned ver)
475 {
476   return data->version_info + ver;
477 }
478
479 /* Returns the info for ssa name NAME.  */
480
481 static inline struct version_info *
482 name_info (struct ivopts_data *data, tree name)
483 {
484   return ver_info (data, SSA_NAME_VERSION (name));
485 }
486
487 /* Checks whether there exists number X such that X * B = A, counting modulo
488    2^BITS.  */
489
490 static bool
491 divide (unsigned bits, unsigned HOST_WIDE_INT a, unsigned HOST_WIDE_INT b,
492         HOST_WIDE_INT *x)
493 {
494   unsigned HOST_WIDE_INT mask = ~(~(unsigned HOST_WIDE_INT) 0 << (bits - 1) << 1);
495   unsigned HOST_WIDE_INT inv, ex, val;
496   unsigned i;
497
498   a &= mask;
499   b &= mask;
500
501   /* First divide the whole equation by 2 as long as possible.  */
502   while (!(a & 1) && !(b & 1))
503     {
504       a >>= 1;
505       b >>= 1;
506       bits--;
507       mask >>= 1;
508     }
509
510   if (!(b & 1))
511     {
512       /* If b is still even, a is odd and there is no such x.  */
513       return false;
514     }
515
516   /* Find the inverse of b.  We compute it as
517      b^(2^(bits - 1) - 1) (mod 2^bits).  */
518   inv = 1;
519   ex = b;
520   for (i = 0; i < bits - 1; i++)
521     {
522       inv = (inv * ex) & mask;
523       ex = (ex * ex) & mask;
524     }
525
526   val = (a * inv) & mask;
527
528   gcc_assert (((val * b) & mask) == a);
529
530   if ((val >> (bits - 1)) & 1)
531     val |= ~mask;
532
533   *x = val;
534
535   return true;
536 }
537
538 /* Returns true if STMT is after the place where the IP_NORMAL ivs will be
539    emitted in LOOP.  */
540
541 static bool
542 stmt_after_ip_normal_pos (struct loop *loop, tree stmt)
543 {
544   basic_block bb = ip_normal_pos (loop), sbb = bb_for_stmt (stmt);
545
546   gcc_assert (bb);
547
548   if (sbb == loop->latch)
549     return true;
550
551   if (sbb != bb)
552     return false;
553
554   return stmt == last_stmt (bb);
555 }
556
557 /* Returns true if STMT if after the place where the original induction
558    variable CAND is incremented.  */
559
560 static bool
561 stmt_after_ip_original_pos (struct iv_cand *cand, tree stmt)
562 {
563   basic_block cand_bb = bb_for_stmt (cand->incremented_at);
564   basic_block stmt_bb = bb_for_stmt (stmt);
565   block_stmt_iterator bsi;
566
567   if (!dominated_by_p (CDI_DOMINATORS, stmt_bb, cand_bb))
568     return false;
569
570   if (stmt_bb != cand_bb)
571     return true;
572
573   /* Scan the block from the end, since the original ivs are usually
574      incremented at the end of the loop body.  */
575   for (bsi = bsi_last (stmt_bb); ; bsi_prev (&bsi))
576     {
577       if (bsi_stmt (bsi) == cand->incremented_at)
578         return false;
579       if (bsi_stmt (bsi) == stmt)
580         return true;
581     }
582 }
583
584 /* Returns true if STMT if after the place where the induction variable
585    CAND is incremented in LOOP.  */
586
587 static bool
588 stmt_after_increment (struct loop *loop, struct iv_cand *cand, tree stmt)
589 {
590   switch (cand->pos)
591     {
592     case IP_END:
593       return false;
594
595     case IP_NORMAL:
596       return stmt_after_ip_normal_pos (loop, stmt);
597
598     case IP_ORIGINAL:
599       return stmt_after_ip_original_pos (cand, stmt);
600
601     default:
602       gcc_unreachable ();
603     }
604 }
605
606 /* Initializes data structures used by the iv optimization pass, stored
607    in DATA.  LOOPS is the loop tree.  */
608
609 static void
610 tree_ssa_iv_optimize_init (struct loops *loops, struct ivopts_data *data)
611 {
612   unsigned i;
613
614   data->version_info_size = 2 * num_ssa_names;
615   data->version_info = xcalloc (data->version_info_size,
616                                 sizeof (struct version_info));
617   data->relevant = BITMAP_XMALLOC ();
618   data->max_inv_id = 0;
619
620   for (i = 1; i < loops->num; i++)
621     if (loops->parray[i])
622       loops->parray[i]->aux = xcalloc (1, sizeof (struct loop_data));
623
624   VARRAY_GENERIC_PTR_NOGC_INIT (data->iv_uses, 20, "iv_uses");
625   VARRAY_GENERIC_PTR_NOGC_INIT (data->iv_candidates, 20, "iv_candidates");
626   VARRAY_GENERIC_PTR_NOGC_INIT (decl_rtl_to_reset, 20, "decl_rtl_to_reset");
627 }
628
629 /* Allocates an induction variable with given initial value BASE and step STEP
630    for loop LOOP.  */
631
632 static struct iv *
633 alloc_iv (tree base, tree step)
634 {
635   struct iv *iv = xcalloc (1, sizeof (struct iv));
636
637   if (step && integer_zerop (step))
638     step = NULL_TREE;
639
640   iv->base = base;
641   iv->step = step;
642   iv->biv_p = false;
643   iv->have_use_for = false;
644   iv->use_id = 0;
645   iv->ssa_name = NULL_TREE;
646
647   return iv;
648 }
649
650 /* Sets STEP and BASE for induction variable IV.  */
651
652 static void
653 set_iv (struct ivopts_data *data, tree iv, tree base, tree step)
654 {
655   struct version_info *info = name_info (data, iv);
656
657   gcc_assert (!info->iv);
658
659   bitmap_set_bit (data->relevant, SSA_NAME_VERSION (iv));
660   info->iv = alloc_iv (base, step);
661   info->iv->ssa_name = iv;
662 }
663
664 /* Finds induction variable declaration for VAR.  */
665
666 static struct iv *
667 get_iv (struct ivopts_data *data, tree var)
668 {
669   basic_block bb;
670   
671   if (!name_info (data, var)->iv)
672     {
673       bb = bb_for_stmt (SSA_NAME_DEF_STMT (var));
674
675       if (!bb
676           || !flow_bb_inside_loop_p (data->current_loop, bb))
677         set_iv (data, var, var, NULL_TREE);
678     }
679
680   return name_info (data, var)->iv;
681 }
682
683 /* Determines the step of a biv defined in PHI.  */
684
685 static tree
686 determine_biv_step (tree phi)
687 {
688   struct loop *loop = bb_for_stmt (phi)->loop_father;
689   tree name = PHI_RESULT (phi), base, step;
690   tree type = TREE_TYPE (name);
691
692   if (!is_gimple_reg (name))
693     return NULL_TREE;
694
695   if (!simple_iv (loop, phi, name, &base, &step))
696     return NULL_TREE;
697
698   if (!step)
699     return build_int_cst (type, 0);
700
701   return step;
702 }
703
704 /* Returns true if EXP is a ssa name that occurs in an abnormal phi node.  */
705
706 static bool
707 abnormal_ssa_name_p (tree exp)
708 {
709   if (!exp)
710     return false;
711
712   if (TREE_CODE (exp) != SSA_NAME)
713     return false;
714
715   return SSA_NAME_OCCURS_IN_ABNORMAL_PHI (exp) != 0;
716 }
717
718 /* Returns false if BASE or INDEX contains a ssa name that occurs in an
719    abnormal phi node.  Callback for for_each_index.  */
720
721 static bool
722 idx_contains_abnormal_ssa_name_p (tree base, tree *index,
723                                   void *data ATTRIBUTE_UNUSED)
724 {
725   if (TREE_CODE (base) == ARRAY_REF)
726     {
727       if (abnormal_ssa_name_p (TREE_OPERAND (base, 2)))
728         return false;
729       if (abnormal_ssa_name_p (TREE_OPERAND (base, 3)))
730         return false;
731     }
732
733   return !abnormal_ssa_name_p (*index);
734 }
735
736 /* Returns true if EXPR contains a ssa name that occurs in an
737    abnormal phi node.  */
738
739 static bool
740 contains_abnormal_ssa_name_p (tree expr)
741 {
742   enum tree_code code = TREE_CODE (expr);
743   enum tree_code_class class = TREE_CODE_CLASS (code);
744     
745   if (code == SSA_NAME)
746     return SSA_NAME_OCCURS_IN_ABNORMAL_PHI (expr) != 0;
747
748   if (code == INTEGER_CST
749       || is_gimple_min_invariant (expr))
750     return false;
751
752   if (code == ADDR_EXPR)
753     return !for_each_index (&TREE_OPERAND (expr, 1),
754                             idx_contains_abnormal_ssa_name_p,
755                             NULL);
756
757   switch (class)
758     {
759     case tcc_binary:
760     case tcc_comparison:
761       if (contains_abnormal_ssa_name_p (TREE_OPERAND (expr, 1)))
762         return true;
763
764       /* Fallthru.  */
765     case tcc_unary:
766       if (contains_abnormal_ssa_name_p (TREE_OPERAND (expr, 0)))
767         return true;
768
769       break;
770
771     default:
772       gcc_unreachable ();
773     }
774
775   return false;
776 }
777
778 /* Finds basic ivs.  */
779
780 static bool
781 find_bivs (struct ivopts_data *data)
782 {
783   tree phi, step, type, base;
784   bool found = false;
785   struct loop *loop = data->current_loop;
786
787   for (phi = phi_nodes (loop->header); phi; phi = TREE_CHAIN (phi))
788     {
789       if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
790         continue;
791
792       step = determine_biv_step (phi);
793
794       if (!step)
795         continue;
796       if (cst_and_fits_in_hwi (step)
797           && int_cst_value (step) == 0)
798         continue;
799
800       base = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop));
801       if (contains_abnormal_ssa_name_p (base))
802         continue;
803
804       type = TREE_TYPE (PHI_RESULT (phi));
805       base = fold_convert (type, base);
806       step = fold_convert (type, step);
807
808       /* FIXME: We do not handle induction variables whose step does
809          not satisfy cst_and_fits_in_hwi.  */
810       if (!cst_and_fits_in_hwi (step))
811         continue;
812
813       set_iv (data, PHI_RESULT (phi), base, step);
814       found = true;
815     }
816
817   return found;
818 }
819
820 /* Marks basic ivs.  */
821
822 static void
823 mark_bivs (struct ivopts_data *data)
824 {
825   tree phi, var;
826   struct iv *iv, *incr_iv;
827   struct loop *loop = data->current_loop;
828   basic_block incr_bb;
829
830   for (phi = phi_nodes (loop->header); phi; phi = TREE_CHAIN (phi))
831     {
832       iv = get_iv (data, PHI_RESULT (phi));
833       if (!iv)
834         continue;
835
836       var = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (loop));
837       incr_iv = get_iv (data, var);
838       if (!incr_iv)
839         continue;
840
841       /* If the increment is in the subloop, ignore it.  */
842       incr_bb = bb_for_stmt (SSA_NAME_DEF_STMT (var));
843       if (incr_bb->loop_father != data->current_loop
844           || (incr_bb->flags & BB_IRREDUCIBLE_LOOP))
845         continue;
846
847       iv->biv_p = true;
848       incr_iv->biv_p = true;
849     }
850 }
851
852 /* Checks whether STMT defines a linear induction variable and stores its
853    parameters to BASE and STEP.  */
854
855 static bool
856 find_givs_in_stmt_scev (struct ivopts_data *data, tree stmt,
857                         tree *base, tree *step)
858 {
859   tree lhs;
860   struct loop *loop = data->current_loop;
861
862   *base = NULL_TREE;
863   *step = NULL_TREE;
864
865   if (TREE_CODE (stmt) != MODIFY_EXPR)
866     return false;
867
868   lhs = TREE_OPERAND (stmt, 0);
869   if (TREE_CODE (lhs) != SSA_NAME)
870     return false;
871
872   if (!simple_iv (loop, stmt, TREE_OPERAND (stmt, 1), base, step))
873     return false;
874
875   /* FIXME: We do not handle induction variables whose step does
876      not satisfy cst_and_fits_in_hwi.  */
877   if (!zero_p (*step)
878       && !cst_and_fits_in_hwi (*step))
879     return false;
880
881   if (contains_abnormal_ssa_name_p (*base))
882     return false;
883
884   return true;
885 }
886
887 /* Finds general ivs in statement STMT.  */
888
889 static void
890 find_givs_in_stmt (struct ivopts_data *data, tree stmt)
891 {
892   tree base, step;
893
894   if (!find_givs_in_stmt_scev (data, stmt, &base, &step))
895     return;
896
897   set_iv (data, TREE_OPERAND (stmt, 0), base, step);
898 }
899
900 /* Finds general ivs in basic block BB.  */
901
902 static void
903 find_givs_in_bb (struct ivopts_data *data, basic_block bb)
904 {
905   block_stmt_iterator bsi;
906
907   for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
908     find_givs_in_stmt (data, bsi_stmt (bsi));
909 }
910
911 /* Finds general ivs.  */
912
913 static void
914 find_givs (struct ivopts_data *data)
915 {
916   struct loop *loop = data->current_loop;
917   basic_block *body = get_loop_body_in_dom_order (loop);
918   unsigned i;
919
920   for (i = 0; i < loop->num_nodes; i++)
921     find_givs_in_bb (data, body[i]);
922   free (body);
923 }
924
925 /* Determine the number of iterations of the current loop.  */
926
927 static void
928 determine_number_of_iterations (struct ivopts_data *data)
929 {
930   struct loop *loop = data->current_loop;
931   edge exit = single_dom_exit (loop);
932
933   if (!exit)
934     return;
935
936   number_of_iterations_exit (loop, exit, &loop_data (loop)->niter);
937 }
938
939 /* For each ssa name defined in LOOP determines whether it is an induction
940    variable and if so, its initial value and step.  */
941
942 static bool
943 find_induction_variables (struct ivopts_data *data)
944 {
945   unsigned i;
946   struct loop *loop = data->current_loop;
947   bitmap_iterator bi;
948
949   if (!find_bivs (data))
950     return false;
951
952   find_givs (data);
953   mark_bivs (data);
954   determine_number_of_iterations (data);
955
956   if (dump_file && (dump_flags & TDF_DETAILS))
957     {
958       if (loop_data (loop)->niter.niter)
959         {
960           fprintf (dump_file, "  number of iterations ");
961           print_generic_expr (dump_file, loop_data (loop)->niter.niter,
962                               TDF_SLIM);
963           fprintf (dump_file, "\n");
964
965           fprintf (dump_file, "  may be zero if ");
966           print_generic_expr (dump_file, loop_data (loop)->niter.may_be_zero,
967                               TDF_SLIM);
968           fprintf (dump_file, "\n");
969
970           fprintf (dump_file, "  bogus unless ");
971           print_generic_expr (dump_file, loop_data (loop)->niter.assumptions,
972                               TDF_SLIM);
973           fprintf (dump_file, "\n");
974           fprintf (dump_file, "\n");
975         };
976  
977       fprintf (dump_file, "Induction variables:\n\n");
978
979       EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, i, bi)
980         {
981           if (ver_info (data, i)->iv)
982             dump_iv (dump_file, ver_info (data, i)->iv);
983         }
984     }
985
986   return true;
987 }
988
989 /* Records a use of type USE_TYPE at *USE_P in STMT whose value is IV.  */
990
991 static struct iv_use *
992 record_use (struct ivopts_data *data, tree *use_p, struct iv *iv,
993             tree stmt, enum use_type use_type)
994 {
995   struct iv_use *use = xcalloc (1, sizeof (struct iv_use));
996
997   use->id = n_iv_uses (data);
998   use->type = use_type;
999   use->iv = iv;
1000   use->stmt = stmt;
1001   use->op_p = use_p;
1002   use->related_cands = BITMAP_XMALLOC ();
1003
1004   if (dump_file && (dump_flags & TDF_DETAILS))
1005     dump_use (dump_file, use);
1006
1007   VARRAY_PUSH_GENERIC_PTR_NOGC (data->iv_uses, use);
1008
1009   return use;
1010 }
1011
1012 /* Checks whether OP is a loop-level invariant and if so, records it.
1013    NONLINEAR_USE is true if the invariant is used in a way we do not
1014    handle specially.  */
1015
1016 static void
1017 record_invariant (struct ivopts_data *data, tree op, bool nonlinear_use)
1018 {
1019   basic_block bb;
1020   struct version_info *info;
1021
1022   if (TREE_CODE (op) != SSA_NAME
1023       || !is_gimple_reg (op))
1024     return;
1025
1026   bb = bb_for_stmt (SSA_NAME_DEF_STMT (op));
1027   if (bb
1028       && flow_bb_inside_loop_p (data->current_loop, bb))
1029     return;
1030
1031   info = name_info (data, op);
1032   info->name = op;
1033   info->has_nonlin_use |= nonlinear_use;
1034   if (!info->inv_id)
1035     info->inv_id = ++data->max_inv_id;
1036   bitmap_set_bit (data->relevant, SSA_NAME_VERSION (op));
1037 }
1038
1039 /* Checks whether the use OP is interesting and if so, records it
1040    as TYPE.  */
1041
1042 static struct iv_use *
1043 find_interesting_uses_outer_or_nonlin (struct ivopts_data *data, tree op,
1044                                        enum use_type type)
1045 {
1046   struct iv *iv;
1047   struct iv *civ;
1048   tree stmt;
1049   struct iv_use *use;
1050
1051   if (TREE_CODE (op) != SSA_NAME)
1052     return NULL;
1053
1054   iv = get_iv (data, op);
1055   if (!iv)
1056     return NULL;
1057   
1058   if (iv->have_use_for)
1059     {
1060       use = iv_use (data, iv->use_id);
1061
1062       gcc_assert (use->type == USE_NONLINEAR_EXPR
1063                   || use->type == USE_OUTER);
1064
1065       if (type == USE_NONLINEAR_EXPR)
1066         use->type = USE_NONLINEAR_EXPR;
1067       return use;
1068     }
1069
1070   if (zero_p (iv->step))
1071     {
1072       record_invariant (data, op, true);
1073       return NULL;
1074     }
1075   iv->have_use_for = true;
1076
1077   civ = xmalloc (sizeof (struct iv));
1078   *civ = *iv;
1079
1080   stmt = SSA_NAME_DEF_STMT (op);
1081   gcc_assert (TREE_CODE (stmt) == PHI_NODE
1082               || TREE_CODE (stmt) == MODIFY_EXPR);
1083
1084   use = record_use (data, NULL, civ, stmt, type);
1085   iv->use_id = use->id;
1086
1087   return use;
1088 }
1089
1090 /* Checks whether the use OP is interesting and if so, records it.  */
1091
1092 static struct iv_use *
1093 find_interesting_uses_op (struct ivopts_data *data, tree op)
1094 {
1095   return find_interesting_uses_outer_or_nonlin (data, op, USE_NONLINEAR_EXPR);
1096 }
1097
1098 /* Records a definition of induction variable OP that is used outside of the
1099    loop.  */
1100
1101 static struct iv_use *
1102 find_interesting_uses_outer (struct ivopts_data *data, tree op)
1103 {
1104   return find_interesting_uses_outer_or_nonlin (data, op, USE_OUTER);
1105 }
1106
1107 /* Checks whether the condition *COND_P in STMT is interesting
1108    and if so, records it.  */
1109
1110 static void
1111 find_interesting_uses_cond (struct ivopts_data *data, tree stmt, tree *cond_p)
1112 {
1113   tree *op0_p;
1114   tree *op1_p;
1115   struct iv *iv0 = NULL, *iv1 = NULL, *civ;
1116   struct iv const_iv;
1117   tree zero = integer_zero_node;
1118
1119   const_iv.step = NULL_TREE;
1120
1121   if (integer_zerop (*cond_p)
1122       || integer_nonzerop (*cond_p))
1123     return;
1124
1125   if (TREE_CODE (*cond_p) == SSA_NAME)
1126     {
1127       op0_p = cond_p;
1128       op1_p = &zero;
1129     }
1130   else
1131     {
1132       op0_p = &TREE_OPERAND (*cond_p, 0);
1133       op1_p = &TREE_OPERAND (*cond_p, 1);
1134     }
1135
1136   if (TREE_CODE (*op0_p) == SSA_NAME)
1137     iv0 = get_iv (data, *op0_p);
1138   else
1139     iv0 = &const_iv;
1140
1141   if (TREE_CODE (*op1_p) == SSA_NAME)
1142     iv1 = get_iv (data, *op1_p);
1143   else
1144     iv1 = &const_iv;
1145
1146   if (/* When comparing with non-invariant value, we may not do any senseful
1147          induction variable elimination.  */
1148       (!iv0 || !iv1)
1149       /* Eliminating condition based on two ivs would be nontrivial.
1150          ??? TODO -- it is not really important to handle this case.  */
1151       || (!zero_p (iv0->step) && !zero_p (iv1->step)))
1152     {
1153       find_interesting_uses_op (data, *op0_p);
1154       find_interesting_uses_op (data, *op1_p);
1155       return;
1156     }
1157
1158   if (zero_p (iv0->step) && zero_p (iv1->step))
1159     {
1160       /* If both are invariants, this is a work for unswitching.  */
1161       return;
1162     }
1163
1164   civ = xmalloc (sizeof (struct iv));
1165   *civ = zero_p (iv0->step) ? *iv1: *iv0;
1166   record_use (data, cond_p, civ, stmt, USE_COMPARE);
1167 }
1168
1169 /* Returns true if expression EXPR is obviously invariant in LOOP,
1170    i.e. if all its operands are defined outside of the LOOP.  */
1171
1172 static bool
1173 expr_invariant_in_loop_p (struct loop *loop, tree expr)
1174 {
1175   basic_block def_bb;
1176   unsigned i, len;
1177
1178   if (is_gimple_min_invariant (expr))
1179     return true;
1180
1181   if (TREE_CODE (expr) == SSA_NAME)
1182     {
1183       def_bb = bb_for_stmt (SSA_NAME_DEF_STMT (expr));
1184       if (def_bb
1185           && flow_bb_inside_loop_p (loop, def_bb))
1186         return false;
1187
1188       return true;
1189     }
1190
1191   if (!EXPR_P (expr))
1192     return false;
1193
1194   len = first_rtl_op (TREE_CODE (expr));
1195   for (i = 0; i < len; i++)
1196     if (!expr_invariant_in_loop_p (loop, TREE_OPERAND (expr, i)))
1197       return false;
1198
1199   return true;
1200 }
1201
1202 /* Cumulates the steps of indices into DATA and replaces their values with the
1203    initial ones.  Returns false when the value of the index cannot be determined.
1204    Callback for for_each_index.  */
1205
1206 struct ifs_ivopts_data
1207 {
1208   struct ivopts_data *ivopts_data;
1209   tree stmt;
1210   tree *step_p;
1211 };
1212
1213 static bool
1214 idx_find_step (tree base, tree *idx, void *data)
1215 {
1216   struct ifs_ivopts_data *dta = data;
1217   struct iv *iv;
1218   tree step, type, iv_type, iv_step, lbound, off;
1219   struct loop *loop = dta->ivopts_data->current_loop;
1220
1221   if (TREE_CODE (base) == MISALIGNED_INDIRECT_REF
1222       || TREE_CODE (base) == ALIGN_INDIRECT_REF)
1223     return false;
1224
1225   /* If base is a component ref, require that the offset of the reference
1226      is invariant.  */
1227   if (TREE_CODE (base) == COMPONENT_REF)
1228     {
1229       off = component_ref_field_offset (base);
1230       return expr_invariant_in_loop_p (loop, off);
1231     }
1232
1233   /* If base is array, first check whether we will be able to move the
1234      reference out of the loop (in order to take its address in strength
1235      reduction).  In order for this to work we need both lower bound
1236      and step to be loop invariants.  */
1237   if (TREE_CODE (base) == ARRAY_REF)
1238     {
1239       step = array_ref_element_size (base);
1240       lbound = array_ref_low_bound (base);
1241
1242       if (!expr_invariant_in_loop_p (loop, step)
1243           || !expr_invariant_in_loop_p (loop, lbound))
1244         return false;
1245     }
1246
1247   if (TREE_CODE (*idx) != SSA_NAME)
1248     return true;
1249
1250   iv = get_iv (dta->ivopts_data, *idx);
1251   if (!iv)
1252     return false;
1253
1254   *idx = iv->base;
1255
1256   if (!iv->step)
1257     return true;
1258
1259   iv_type = TREE_TYPE (iv->base);
1260   type = build_pointer_type (TREE_TYPE (base));
1261   if (TREE_CODE (base) == ARRAY_REF)
1262     {
1263       step = array_ref_element_size (base);
1264
1265       /* We only handle addresses whose step is an integer constant.  */
1266       if (TREE_CODE (step) != INTEGER_CST)
1267         return false;
1268     }
1269   else
1270     /* The step for pointer arithmetics already is 1 byte.  */
1271     step = build_int_cst (type, 1);
1272
1273   if (TYPE_PRECISION (iv_type) < TYPE_PRECISION (type))
1274     iv_step = can_count_iv_in_wider_type (dta->ivopts_data->current_loop,
1275                                           type, iv->base, iv->step, dta->stmt);
1276   else
1277     iv_step = fold_convert (iv_type, iv->step);
1278
1279   if (!iv_step)
1280     {
1281       /* The index might wrap.  */
1282       return false;
1283     }
1284
1285   step = EXEC_BINARY (MULT_EXPR, type, step, iv_step);
1286
1287   if (!*dta->step_p)
1288     *dta->step_p = step;
1289   else
1290     *dta->step_p = EXEC_BINARY (PLUS_EXPR, type, *dta->step_p, step);
1291
1292   return true;
1293 }
1294
1295 /* Records use in index IDX.  Callback for for_each_index.  Ivopts data
1296    object is passed to it in DATA.  */
1297
1298 static bool
1299 idx_record_use (tree base, tree *idx,
1300                 void *data)
1301 {
1302   find_interesting_uses_op (data, *idx);
1303   if (TREE_CODE (base) == ARRAY_REF)
1304     {
1305       find_interesting_uses_op (data, array_ref_element_size (base));
1306       find_interesting_uses_op (data, array_ref_low_bound (base));
1307     }
1308   return true;
1309 }
1310
1311 /* Finds addresses in *OP_P inside STMT.  */
1312
1313 static void
1314 find_interesting_uses_address (struct ivopts_data *data, tree stmt, tree *op_p)
1315 {
1316   tree base = unshare_expr (*op_p), step = NULL;
1317   struct iv *civ;
1318   struct ifs_ivopts_data ifs_ivopts_data;
1319
1320   /* Ignore bitfields for now.  Not really something terribly complicated
1321      to handle.  TODO.  */
1322   if (TREE_CODE (base) == COMPONENT_REF
1323       && DECL_NONADDRESSABLE_P (TREE_OPERAND (base, 1)))
1324     goto fail;
1325
1326   ifs_ivopts_data.ivopts_data = data;
1327   ifs_ivopts_data.stmt = stmt;
1328   ifs_ivopts_data.step_p = &step;
1329   if (!for_each_index (&base, idx_find_step, &ifs_ivopts_data)
1330       || zero_p (step))
1331     goto fail;
1332
1333   gcc_assert (TREE_CODE (base) != ALIGN_INDIRECT_REF);
1334   gcc_assert (TREE_CODE (base) != MISALIGNED_INDIRECT_REF);
1335
1336   if (TREE_CODE (base) == INDIRECT_REF)
1337     base = TREE_OPERAND (base, 0);
1338   else
1339     base = build_addr (base);
1340
1341   civ = alloc_iv (base, step);
1342   record_use (data, op_p, civ, stmt, USE_ADDRESS);
1343   return;
1344
1345 fail:
1346   for_each_index (op_p, idx_record_use, data);
1347 }
1348
1349 /* Finds and records invariants used in STMT.  */
1350
1351 static void
1352 find_invariants_stmt (struct ivopts_data *data, tree stmt)
1353 {
1354   use_optype uses = NULL;
1355   unsigned i, n;
1356   tree op;
1357
1358   if (TREE_CODE (stmt) == PHI_NODE)
1359     n = PHI_NUM_ARGS (stmt);
1360   else
1361     {
1362       get_stmt_operands (stmt);
1363       uses = STMT_USE_OPS (stmt);
1364       n = NUM_USES (uses);
1365     }
1366
1367   for (i = 0; i < n; i++)
1368     {
1369       if (TREE_CODE (stmt) == PHI_NODE)
1370         op = PHI_ARG_DEF (stmt, i);
1371       else
1372         op = USE_OP (uses, i);
1373
1374       record_invariant (data, op, false);
1375     }
1376 }
1377
1378 /* Finds interesting uses of induction variables in the statement STMT.  */
1379
1380 static void
1381 find_interesting_uses_stmt (struct ivopts_data *data, tree stmt)
1382 {
1383   struct iv *iv;
1384   tree op, lhs, rhs;
1385   use_optype uses = NULL;
1386   unsigned i, n;
1387
1388   find_invariants_stmt (data, stmt);
1389
1390   if (TREE_CODE (stmt) == COND_EXPR)
1391     {
1392       find_interesting_uses_cond (data, stmt, &COND_EXPR_COND (stmt));
1393       return;
1394     }
1395
1396   if (TREE_CODE (stmt) == MODIFY_EXPR)
1397     {
1398       lhs = TREE_OPERAND (stmt, 0);
1399       rhs = TREE_OPERAND (stmt, 1);
1400
1401       if (TREE_CODE (lhs) == SSA_NAME)
1402         {
1403           /* If the statement defines an induction variable, the uses are not
1404              interesting by themselves.  */
1405
1406           iv = get_iv (data, lhs);
1407
1408           if (iv && !zero_p (iv->step))
1409             return;
1410         }
1411
1412       switch (TREE_CODE_CLASS (TREE_CODE (rhs)))
1413         {
1414         case tcc_comparison:
1415           find_interesting_uses_cond (data, stmt, &TREE_OPERAND (stmt, 1));
1416           return;
1417
1418         case tcc_reference:
1419           find_interesting_uses_address (data, stmt, &TREE_OPERAND (stmt, 1));
1420           if (REFERENCE_CLASS_P (lhs))
1421             find_interesting_uses_address (data, stmt, &TREE_OPERAND (stmt, 0));
1422           return;
1423
1424         default: ;
1425         }
1426
1427       if (REFERENCE_CLASS_P (lhs)
1428           && is_gimple_val (rhs))
1429         {
1430           find_interesting_uses_address (data, stmt, &TREE_OPERAND (stmt, 0));
1431           find_interesting_uses_op (data, rhs);
1432           return;
1433         }
1434
1435       /* TODO -- we should also handle address uses of type
1436
1437          memory = call (whatever);
1438
1439          and
1440
1441          call (memory).  */
1442     }
1443
1444   if (TREE_CODE (stmt) == PHI_NODE
1445       && bb_for_stmt (stmt) == data->current_loop->header)
1446     {
1447       lhs = PHI_RESULT (stmt);
1448       iv = get_iv (data, lhs);
1449
1450       if (iv && !zero_p (iv->step))
1451         return;
1452     }
1453
1454   if (TREE_CODE (stmt) == PHI_NODE)
1455     n = PHI_NUM_ARGS (stmt);
1456   else
1457     {
1458       uses = STMT_USE_OPS (stmt);
1459       n = NUM_USES (uses);
1460     }
1461
1462   for (i = 0; i < n; i++)
1463     {
1464       if (TREE_CODE (stmt) == PHI_NODE)
1465         op = PHI_ARG_DEF (stmt, i);
1466       else
1467         op = USE_OP (uses, i);
1468
1469       if (TREE_CODE (op) != SSA_NAME)
1470         continue;
1471
1472       iv = get_iv (data, op);
1473       if (!iv)
1474         continue;
1475
1476       find_interesting_uses_op (data, op);
1477     }
1478 }
1479
1480 /* Finds interesting uses of induction variables outside of loops
1481    on loop exit edge EXIT.  */
1482
1483 static void
1484 find_interesting_uses_outside (struct ivopts_data *data, edge exit)
1485 {
1486   tree phi, def;
1487
1488   for (phi = phi_nodes (exit->dest); phi; phi = TREE_CHAIN (phi))
1489     {
1490       def = PHI_ARG_DEF_FROM_EDGE (phi, exit);
1491       find_interesting_uses_outer (data, def);
1492     }
1493 }
1494
1495 /* Finds uses of the induction variables that are interesting.  */
1496
1497 static void
1498 find_interesting_uses (struct ivopts_data *data)
1499 {
1500   basic_block bb;
1501   block_stmt_iterator bsi;
1502   tree phi;
1503   basic_block *body = get_loop_body (data->current_loop);
1504   unsigned i;
1505   struct version_info *info;
1506   edge e;
1507
1508   if (dump_file && (dump_flags & TDF_DETAILS))
1509     fprintf (dump_file, "Uses:\n\n");
1510
1511   for (i = 0; i < data->current_loop->num_nodes; i++)
1512     {
1513       bb = body[i];
1514
1515       for (e = bb->succ; e; e = e->succ_next)
1516         if (e->dest != EXIT_BLOCK_PTR
1517             && !flow_bb_inside_loop_p (data->current_loop, e->dest))
1518           find_interesting_uses_outside (data, e);
1519
1520       for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
1521         find_interesting_uses_stmt (data, phi);
1522       for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1523         find_interesting_uses_stmt (data, bsi_stmt (bsi));
1524     }
1525
1526   if (dump_file && (dump_flags & TDF_DETAILS))
1527     {
1528       bitmap_iterator bi;
1529
1530       fprintf (dump_file, "\n");
1531
1532       EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, i, bi)
1533         {
1534           info = ver_info (data, i);
1535           if (info->inv_id)
1536             {
1537               fprintf (dump_file, "  ");
1538               print_generic_expr (dump_file, info->name, TDF_SLIM);
1539               fprintf (dump_file, " is invariant (%d)%s\n",
1540                        info->inv_id, info->has_nonlin_use ? "" : ", eliminable");
1541             }
1542         }
1543
1544       fprintf (dump_file, "\n");
1545     }
1546
1547   free (body);
1548 }
1549
1550 /* Adds a candidate BASE + STEP * i.  Important field is set to IMPORTANT and
1551    position to POS.  If USE is not NULL, the candidate is set as related to
1552    it.  If both BASE and STEP are NULL, we add a pseudocandidate for the
1553    replacement of the final value of the iv by a direct computation.  */
1554
1555 static struct iv_cand *
1556 add_candidate_1 (struct ivopts_data *data,
1557                  tree base, tree step, bool important, enum iv_position pos,
1558                  struct iv_use *use, tree incremented_at)
1559 {
1560   unsigned i;
1561   struct iv_cand *cand = NULL;
1562   tree type;
1563   
1564   if (base)
1565     {
1566       type = TREE_TYPE (base);
1567       if (!TYPE_UNSIGNED (type))
1568         {
1569           type = unsigned_type_for (type);
1570           base = fold_convert (type, base);
1571           if (step)
1572             step = fold_convert (type, step);
1573         }
1574     }
1575
1576   for (i = 0; i < n_iv_cands (data); i++)
1577     {
1578       cand = iv_cand (data, i);
1579
1580       if (cand->pos != pos)
1581         continue;
1582
1583       if (cand->incremented_at != incremented_at)
1584         continue;
1585
1586       if (!cand->iv)
1587         {
1588           if (!base && !step)
1589             break;
1590
1591           continue;
1592         }
1593
1594       if (!base && !step)
1595         continue;
1596
1597       if (!operand_equal_p (base, cand->iv->base, 0))
1598         continue;
1599
1600       if (zero_p (cand->iv->step))
1601         {
1602           if (zero_p (step))
1603             break;
1604         }
1605       else
1606         {
1607           if (step && operand_equal_p (step, cand->iv->step, 0))
1608             break;
1609         }
1610     }
1611
1612   if (i == n_iv_cands (data))
1613     {
1614       cand = xcalloc (1, sizeof (struct iv_cand));
1615       cand->id = i;
1616
1617       if (!base && !step)
1618         cand->iv = NULL;
1619       else
1620         cand->iv = alloc_iv (base, step);
1621
1622       cand->pos = pos;
1623       if (pos != IP_ORIGINAL && cand->iv)
1624         {
1625           cand->var_before = create_tmp_var_raw (TREE_TYPE (base), "ivtmp");
1626           cand->var_after = cand->var_before;
1627         }
1628       cand->important = important;
1629       cand->incremented_at = incremented_at;
1630       VARRAY_PUSH_GENERIC_PTR_NOGC (data->iv_candidates, cand);
1631
1632       if (dump_file && (dump_flags & TDF_DETAILS))
1633         dump_cand (dump_file, cand);
1634     }
1635
1636   if (important && !cand->important)
1637     {
1638       cand->important = true;
1639       if (dump_file && (dump_flags & TDF_DETAILS))
1640         fprintf (dump_file, "Candidate %d is important\n", cand->id);
1641     }
1642
1643   if (use)
1644     {
1645       bitmap_set_bit (use->related_cands, i);
1646       if (dump_file && (dump_flags & TDF_DETAILS))
1647         fprintf (dump_file, "Candidate %d is related to use %d\n",
1648                  cand->id, use->id);
1649     }
1650
1651   return cand;
1652 }
1653
1654 /* Adds a candidate BASE + STEP * i.  Important field is set to IMPORTANT and
1655    position to POS.  If USE is not NULL, the candidate is set as related to
1656    it.  The candidate computation is scheduled on all available positions.  */
1657
1658 static void
1659 add_candidate (struct ivopts_data *data, 
1660                tree base, tree step, bool important, struct iv_use *use)
1661 {
1662   if (ip_normal_pos (data->current_loop))
1663     add_candidate_1 (data, base, step, important, IP_NORMAL, use, NULL_TREE);
1664   if (ip_end_pos (data->current_loop))
1665     add_candidate_1 (data, base, step, important, IP_END, use, NULL_TREE);
1666 }
1667
1668 /* Adds standard iv candidates.  */
1669
1670 static void
1671 add_standard_iv_candidates (struct ivopts_data *data)
1672 {
1673   /* Add 0 + 1 * iteration candidate.  */
1674   add_candidate (data,
1675                  build_int_cst (unsigned_intSI_type_node, 0),
1676                  build_int_cst (unsigned_intSI_type_node, 1),
1677                  true, NULL);
1678
1679   /* The same for a long type if it is still fast enough.  */
1680   if (BITS_PER_WORD > 32)
1681     add_candidate (data,
1682                    build_int_cst (unsigned_intDI_type_node, 0),
1683                    build_int_cst (unsigned_intDI_type_node, 1),
1684                    true, NULL);
1685 }
1686
1687
1688 /* Adds candidates bases on the old induction variable IV.  */
1689
1690 static void
1691 add_old_iv_candidates (struct ivopts_data *data, struct iv *iv)
1692 {
1693   tree phi, def;
1694   struct iv_cand *cand;
1695
1696   add_candidate (data, iv->base, iv->step, true, NULL);
1697
1698   /* The same, but with initial value zero.  */
1699   add_candidate (data,
1700                  build_int_cst (TREE_TYPE (iv->base), 0),
1701                  iv->step, true, NULL);
1702
1703   phi = SSA_NAME_DEF_STMT (iv->ssa_name);
1704   if (TREE_CODE (phi) == PHI_NODE)
1705     {
1706       /* Additionally record the possibility of leaving the original iv
1707          untouched.  */
1708       def = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (data->current_loop));
1709       cand = add_candidate_1 (data,
1710                               iv->base, iv->step, true, IP_ORIGINAL, NULL,
1711                               SSA_NAME_DEF_STMT (def));
1712       cand->var_before = iv->ssa_name;
1713       cand->var_after = def;
1714     }
1715 }
1716
1717 /* Adds candidates based on the old induction variables.  */
1718
1719 static void
1720 add_old_ivs_candidates (struct ivopts_data *data)
1721 {
1722   unsigned i;
1723   struct iv *iv;
1724   bitmap_iterator bi;
1725
1726   EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, i, bi)
1727     {
1728       iv = ver_info (data, i)->iv;
1729       if (iv && iv->biv_p && !zero_p (iv->step))
1730         add_old_iv_candidates (data, iv);
1731     }
1732 }
1733
1734 /* Adds candidates based on the value of the induction variable IV and USE.  */
1735
1736 static void
1737 add_iv_value_candidates (struct ivopts_data *data,
1738                          struct iv *iv, struct iv_use *use)
1739 {
1740   add_candidate (data, iv->base, iv->step, false, use);
1741
1742   /* The same, but with initial value zero.  */
1743   add_candidate (data, build_int_cst (TREE_TYPE (iv->base), 0),
1744                  iv->step, false, use);
1745 }
1746
1747 /* Adds candidates based on the address IV and USE.  */
1748
1749 static void
1750 add_address_candidates (struct ivopts_data *data,
1751                         struct iv *iv, struct iv_use *use)
1752 {
1753   tree base, abase, tmp, *act;
1754
1755   /* First, the trivial choices.  */
1756   add_iv_value_candidates (data, iv, use);
1757
1758   /* Second, try removing the COMPONENT_REFs.  */
1759   if (TREE_CODE (iv->base) == ADDR_EXPR)
1760     {
1761       base = TREE_OPERAND (iv->base, 0);
1762       while (TREE_CODE (base) == COMPONENT_REF
1763              || (TREE_CODE (base) == ARRAY_REF
1764                  && TREE_CODE (TREE_OPERAND (base, 1)) == INTEGER_CST))
1765         base = TREE_OPERAND (base, 0);
1766
1767       if (base != TREE_OPERAND (iv->base, 0))
1768         { 
1769           gcc_assert (TREE_CODE (base) != ALIGN_INDIRECT_REF);
1770           gcc_assert (TREE_CODE (base) != MISALIGNED_INDIRECT_REF);
1771
1772           if (TREE_CODE (base) == INDIRECT_REF)
1773             base = TREE_OPERAND (base, 0);
1774           else
1775             base = build_addr (base);
1776           add_candidate (data, base, iv->step, false, use);
1777         }
1778     }
1779
1780   /* Third, try removing the constant offset.  */
1781   abase = iv->base;
1782   while (TREE_CODE (abase) == PLUS_EXPR
1783          && TREE_CODE (TREE_OPERAND (abase, 1)) != INTEGER_CST)
1784     abase = TREE_OPERAND (abase, 0);
1785   /* We found the offset, so make the copy of the non-shared part and
1786      remove it.  */
1787   if (TREE_CODE (abase) == PLUS_EXPR)
1788     {
1789       tmp = iv->base;
1790       act = &base;
1791
1792       for (tmp = iv->base; tmp != abase; tmp = TREE_OPERAND (tmp, 0))
1793         {
1794           *act = build2 (PLUS_EXPR, TREE_TYPE (tmp),
1795                          NULL_TREE, TREE_OPERAND (tmp, 1));
1796           act = &TREE_OPERAND (*act, 0);
1797         }
1798       *act = TREE_OPERAND (tmp, 0);
1799
1800       add_candidate (data, base, iv->step, false, use);
1801     }
1802 }
1803
1804 /* Possibly adds pseudocandidate for replacing the final value of USE by
1805    a direct computation.  */
1806
1807 static void
1808 add_iv_outer_candidates (struct ivopts_data *data, struct iv_use *use)
1809 {
1810   struct tree_niter_desc *niter;
1811   struct loop *loop = data->current_loop;
1812
1813   /* We must know where we exit the loop and how many times does it roll.  */
1814   if (!single_dom_exit (loop))
1815     return;
1816
1817   niter = &loop_data (loop)->niter;
1818   if (!niter->niter
1819       || !operand_equal_p (niter->assumptions, boolean_true_node, 0)
1820       || !operand_equal_p (niter->may_be_zero, boolean_false_node, 0))
1821     return;
1822
1823   add_candidate_1 (data, NULL, NULL, false, IP_NORMAL, use, NULL_TREE);
1824 }
1825
1826 /* Adds candidates based on the uses.  */
1827
1828 static void
1829 add_derived_ivs_candidates (struct ivopts_data *data)
1830 {
1831   unsigned i;
1832
1833   for (i = 0; i < n_iv_uses (data); i++)
1834     {
1835       struct iv_use *use = iv_use (data, i);
1836
1837       if (!use)
1838         continue;
1839
1840       switch (use->type)
1841         {
1842         case USE_NONLINEAR_EXPR:
1843         case USE_COMPARE:
1844           /* Just add the ivs based on the value of the iv used here.  */
1845           add_iv_value_candidates (data, use->iv, use);
1846           break;
1847
1848         case USE_OUTER:
1849           add_iv_value_candidates (data, use->iv, use);
1850
1851           /* Additionally, add the pseudocandidate for the possibility to
1852              replace the final value by a direct computation.  */
1853           add_iv_outer_candidates (data, use);
1854           break;
1855
1856         case USE_ADDRESS:
1857           add_address_candidates (data, use->iv, use);
1858           break;
1859
1860         default:
1861           gcc_unreachable ();
1862         }
1863     }
1864 }
1865
1866 /* Finds the candidates for the induction variables.  */
1867
1868 static void
1869 find_iv_candidates (struct ivopts_data *data)
1870 {
1871   /* Add commonly used ivs.  */
1872   add_standard_iv_candidates (data);
1873
1874   /* Add old induction variables.  */
1875   add_old_ivs_candidates (data);
1876
1877   /* Add induction variables derived from uses.  */
1878   add_derived_ivs_candidates (data);
1879 }
1880
1881 /* Allocates the data structure mapping the (use, candidate) pairs to costs.
1882    If consider_all_candidates is true, we use a two-dimensional array, otherwise
1883    we allocate a simple list to every use.  */
1884
1885 static void
1886 alloc_use_cost_map (struct ivopts_data *data)
1887 {
1888   unsigned i, n_imp = 0, size, j;
1889
1890   if (!data->consider_all_candidates)
1891     {
1892       for (i = 0; i < n_iv_cands (data); i++)
1893         {
1894           struct iv_cand *cand = iv_cand (data, i);
1895           if (cand->important)
1896             n_imp++;
1897         }
1898     }
1899
1900   for (i = 0; i < n_iv_uses (data); i++)
1901     {
1902       struct iv_use *use = iv_use (data, i);
1903       bitmap_iterator bi;
1904
1905       if (data->consider_all_candidates)
1906         {
1907           size = n_iv_cands (data);
1908           use->n_map_members = size;
1909         }
1910       else
1911         {
1912           size = n_imp;
1913           EXECUTE_IF_SET_IN_BITMAP (use->related_cands, 0, j, bi)
1914             {
1915               size++;
1916             }
1917           use->n_map_members = 0;
1918         }
1919
1920       use->cost_map = xcalloc (size, sizeof (struct cost_pair));
1921     }
1922 }
1923
1924 /* Sets cost of (USE, CANDIDATE) pair to COST and record that it depends
1925    on invariants DEPENDS_ON.  */
1926
1927 static void
1928 set_use_iv_cost (struct ivopts_data *data,
1929                  struct iv_use *use, struct iv_cand *cand, unsigned cost,
1930                  bitmap depends_on)
1931 {
1932   if (cost == INFTY
1933       && depends_on)
1934     {
1935       BITMAP_XFREE (depends_on);
1936       depends_on = NULL;
1937     }
1938
1939   if (data->consider_all_candidates)
1940     {
1941       use->cost_map[cand->id].cand = cand;
1942       use->cost_map[cand->id].cost = cost;
1943       use->cost_map[cand->id].depends_on = depends_on;
1944       return;
1945     }
1946
1947   if (cost == INFTY)
1948     return;
1949
1950   use->cost_map[use->n_map_members].cand = cand;
1951   use->cost_map[use->n_map_members].cost = cost;
1952   use->cost_map[use->n_map_members].depends_on = depends_on;
1953   use->n_map_members++;
1954 }
1955
1956 /* Gets cost of (USE, CANDIDATE) pair.  Stores the bitmap of dependencies to
1957    DEPENDS_ON.  */
1958
1959 static unsigned
1960 get_use_iv_cost (struct ivopts_data *data,
1961                  struct iv_use *use, struct iv_cand *cand, bitmap *depends_on)
1962 {
1963   unsigned i;
1964
1965   if (!cand)
1966     return INFTY;
1967
1968   if (data->consider_all_candidates)
1969     i = cand->id;
1970   else
1971     {
1972       for (i = 0; i < use->n_map_members; i++)
1973         if (use->cost_map[i].cand == cand)
1974           break;
1975
1976       if (i == use->n_map_members)
1977         return INFTY;
1978     }
1979
1980   if (depends_on)
1981     *depends_on = use->cost_map[i].depends_on;
1982   return use->cost_map[i].cost;
1983 }
1984
1985 /* Returns estimate on cost of computing SEQ.  */
1986
1987 static unsigned
1988 seq_cost (rtx seq)
1989 {
1990   unsigned cost = 0;
1991   rtx set;
1992
1993   for (; seq; seq = NEXT_INSN (seq))
1994     {
1995       set = single_set (seq);
1996       if (set)
1997         cost += rtx_cost (set, SET);
1998       else
1999         cost++;
2000     }
2001
2002   return cost;
2003 }
2004
2005 /* Produce DECL_RTL for object obj so it looks like it is stored in memory.  */
2006 static rtx
2007 produce_memory_decl_rtl (tree obj, int *regno)
2008 {
2009   rtx x;
2010   if (!obj)
2011     abort ();
2012   if (TREE_STATIC (obj) || DECL_EXTERNAL (obj))
2013     {
2014       const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (obj));
2015       x = gen_rtx_SYMBOL_REF (Pmode, name);
2016     }
2017   else
2018     x = gen_raw_REG (Pmode, (*regno)++);
2019
2020   return gen_rtx_MEM (DECL_MODE (obj), x);
2021 }
2022
2023 /* Prepares decl_rtl for variables referred in *EXPR_P.  Callback for
2024    walk_tree.  DATA contains the actual fake register number.  */
2025
2026 static tree
2027 prepare_decl_rtl (tree *expr_p, int *ws, void *data)
2028 {
2029   tree obj = NULL_TREE;
2030   rtx x = NULL_RTX;
2031   int *regno = data;
2032
2033   switch (TREE_CODE (*expr_p))
2034     {
2035     case ADDR_EXPR:
2036       for (expr_p = &TREE_OPERAND (*expr_p, 0);
2037            (handled_component_p (*expr_p)
2038             || TREE_CODE (*expr_p) == REALPART_EXPR
2039             || TREE_CODE (*expr_p) == IMAGPART_EXPR);
2040            expr_p = &TREE_OPERAND (*expr_p, 0));
2041       obj = *expr_p;
2042       if (DECL_P (obj))
2043         x = produce_memory_decl_rtl (obj, regno);
2044       break;
2045
2046     case SSA_NAME:
2047       *ws = 0;
2048       obj = SSA_NAME_VAR (*expr_p);
2049       if (!DECL_RTL_SET_P (obj))
2050         x = gen_raw_REG (DECL_MODE (obj), (*regno)++);
2051       break;
2052
2053     case VAR_DECL:
2054     case PARM_DECL:
2055     case RESULT_DECL:
2056       *ws = 0;
2057       obj = *expr_p;
2058
2059       if (DECL_RTL_SET_P (obj))
2060         break;
2061
2062       if (DECL_MODE (obj) == BLKmode)
2063         x = produce_memory_decl_rtl (obj, regno);
2064       else
2065         x = gen_raw_REG (DECL_MODE (obj), (*regno)++);
2066
2067       break;
2068
2069     default:
2070       break;
2071     }
2072
2073   if (x)
2074     {
2075       VARRAY_PUSH_GENERIC_PTR_NOGC (decl_rtl_to_reset, obj);
2076       SET_DECL_RTL (obj, x);
2077     }
2078
2079   return NULL_TREE;
2080 }
2081
2082 /* Determines cost of the computation of EXPR.  */
2083
2084 static unsigned
2085 computation_cost (tree expr)
2086 {
2087   rtx seq, rslt;
2088   tree type = TREE_TYPE (expr);
2089   unsigned cost;
2090   int regno = 0;
2091
2092   walk_tree (&expr, prepare_decl_rtl, &regno, NULL);
2093   start_sequence ();
2094   rslt = expand_expr (expr, NULL_RTX, TYPE_MODE (type), EXPAND_NORMAL);
2095   seq = get_insns ();
2096   end_sequence ();
2097
2098   cost = seq_cost (seq);
2099   if (GET_CODE (rslt) == MEM)
2100     cost += address_cost (XEXP (rslt, 0), TYPE_MODE (type));
2101
2102   return cost;
2103 }
2104
2105 /* Returns variable containing the value of candidate CAND at statement AT.  */
2106
2107 static tree
2108 var_at_stmt (struct loop *loop, struct iv_cand *cand, tree stmt)
2109 {
2110   if (stmt_after_increment (loop, cand, stmt))
2111     return cand->var_after;
2112   else
2113     return cand->var_before;
2114 }
2115
2116 /* Determines the expression by that USE is expressed from induction variable
2117    CAND at statement AT in LOOP.  */
2118
2119 static tree
2120 get_computation_at (struct loop *loop,
2121                     struct iv_use *use, struct iv_cand *cand, tree at)
2122 {
2123   tree ubase = use->iv->base;
2124   tree ustep = use->iv->step;
2125   tree cbase = cand->iv->base;
2126   tree cstep = cand->iv->step;
2127   tree utype = TREE_TYPE (ubase), ctype = TREE_TYPE (cbase);
2128   tree uutype;
2129   tree expr, delta;
2130   tree ratio;
2131   unsigned HOST_WIDE_INT ustepi, cstepi;
2132   HOST_WIDE_INT ratioi;
2133
2134   if (TYPE_PRECISION (utype) > TYPE_PRECISION (ctype))
2135     {
2136       /* We do not have a precision to express the values of use.  */
2137       return NULL_TREE;
2138     }
2139
2140   expr = var_at_stmt (loop, cand, at);
2141
2142   if (TREE_TYPE (expr) != ctype)
2143     {
2144       /* This may happen with the original ivs.  */
2145       expr = fold_convert (ctype, expr);
2146     }
2147
2148   if (TYPE_UNSIGNED (utype))
2149     uutype = utype;
2150   else
2151     {
2152       uutype = unsigned_type_for (utype);
2153       ubase = fold_convert (uutype, ubase);
2154       ustep = fold_convert (uutype, ustep);
2155     }
2156
2157   if (uutype != ctype)
2158     {
2159       expr = fold_convert (uutype, expr);
2160       cbase = fold_convert (uutype, cbase);
2161       cstep = fold_convert (uutype, cstep);
2162     }
2163
2164   if (!cst_and_fits_in_hwi (cstep)
2165       || !cst_and_fits_in_hwi (ustep))
2166     return NULL_TREE;
2167
2168   ustepi = int_cst_value (ustep);
2169   cstepi = int_cst_value (cstep);
2170
2171   if (!divide (TYPE_PRECISION (uutype), ustepi, cstepi, &ratioi))
2172     {
2173       /* TODO maybe consider case when ustep divides cstep and the ratio is
2174          a power of 2 (so that the division is fast to execute)?  We would
2175          need to be much more careful with overflows etc. then.  */
2176       return NULL_TREE;
2177     }
2178
2179   /* We may need to shift the value if we are after the increment.  */
2180   if (stmt_after_increment (loop, cand, at))
2181     cbase = fold (build2 (PLUS_EXPR, uutype, cbase, cstep));
2182
2183   /* use = ubase + ratio * (var - cbase).  If either cbase is a constant
2184      or |ratio| == 1, it is better to handle this like
2185      
2186      ubase - ratio * cbase + ratio * var.  */
2187
2188   if (ratioi == 1)
2189     {
2190       delta = fold (build2 (MINUS_EXPR, uutype, ubase, cbase));
2191       expr = fold (build2 (PLUS_EXPR, uutype, expr, delta));
2192     }
2193   else if (ratioi == -1)
2194     {
2195       delta = fold (build2 (PLUS_EXPR, uutype, ubase, cbase));
2196       expr = fold (build2 (MINUS_EXPR, uutype, delta, expr));
2197     }
2198   else if (TREE_CODE (cbase) == INTEGER_CST)
2199     {
2200       ratio = build_int_cst_type (uutype, ratioi);
2201       delta = fold (build2 (MULT_EXPR, uutype, ratio, cbase));
2202       delta = fold (build2 (MINUS_EXPR, uutype, ubase, delta));
2203       expr = fold (build2 (MULT_EXPR, uutype, ratio, expr));
2204       expr = fold (build2 (PLUS_EXPR, uutype, delta, expr));
2205     }
2206   else
2207     {
2208       expr = fold (build2 (MINUS_EXPR, uutype, expr, cbase));
2209       ratio = build_int_cst_type (uutype, ratioi);
2210       expr = fold (build2 (MULT_EXPR, uutype, ratio, expr));
2211       expr = fold (build2 (PLUS_EXPR, uutype, ubase, expr));
2212     }
2213
2214   return fold_convert (utype, expr);
2215 }
2216
2217 /* Determines the expression by that USE is expressed from induction variable
2218    CAND in LOOP.  */
2219
2220 static tree
2221 get_computation (struct loop *loop, struct iv_use *use, struct iv_cand *cand)
2222 {
2223   return get_computation_at (loop, use, cand, use->stmt);
2224 }
2225
2226 /* Strips constant offsets from EXPR and adds them to OFFSET.  */
2227
2228 static void
2229 strip_offset (tree *expr, unsigned HOST_WIDE_INT *offset)
2230 {
2231   tree op0, op1;
2232   enum tree_code code;
2233   
2234   while (1)
2235     {
2236       if (cst_and_fits_in_hwi (*expr))
2237         {
2238           *offset += int_cst_value (*expr);
2239           *expr = integer_zero_node;
2240           return;
2241         }
2242
2243       code = TREE_CODE (*expr);
2244      
2245       if (code != PLUS_EXPR && code != MINUS_EXPR)
2246         return;
2247
2248       op0 = TREE_OPERAND (*expr, 0);
2249       op1 = TREE_OPERAND (*expr, 1);
2250
2251       if (cst_and_fits_in_hwi (op1))
2252         {
2253           if (code == PLUS_EXPR)
2254             *offset += int_cst_value (op1);
2255           else
2256             *offset -= int_cst_value (op1);
2257
2258           *expr = op0;
2259           continue;
2260         }
2261
2262       if (code != PLUS_EXPR)
2263         return;
2264
2265       if (!cst_and_fits_in_hwi (op0))
2266         return;
2267
2268       *offset += int_cst_value (op0);
2269       *expr = op1;
2270     }
2271 }
2272
2273 /* Returns cost of addition in MODE.  */
2274
2275 static unsigned
2276 add_cost (enum machine_mode mode)
2277 {
2278   static unsigned costs[NUM_MACHINE_MODES];
2279   rtx seq;
2280   unsigned cost;
2281
2282   if (costs[mode])
2283     return costs[mode];
2284
2285   start_sequence ();
2286   force_operand (gen_rtx_fmt_ee (PLUS, mode,
2287                                  gen_raw_REG (mode, FIRST_PSEUDO_REGISTER),
2288                                  gen_raw_REG (mode, FIRST_PSEUDO_REGISTER + 1)),
2289                  NULL_RTX);
2290   seq = get_insns ();
2291   end_sequence ();
2292
2293   cost = seq_cost (seq);
2294   if (!cost)
2295     cost = 1;
2296
2297   costs[mode] = cost;
2298       
2299   if (dump_file && (dump_flags & TDF_DETAILS))
2300     fprintf (dump_file, "Addition in %s costs %d\n",
2301              GET_MODE_NAME (mode), cost);
2302   return cost;
2303 }
2304
2305 /* Entry in a hashtable of already known costs for multiplication.  */
2306 struct mbc_entry
2307 {
2308   HOST_WIDE_INT cst;            /* The constant to multiply by.  */
2309   enum machine_mode mode;       /* In mode.  */
2310   unsigned cost;                /* The cost.  */
2311 };
2312
2313 /* Counts hash value for the ENTRY.  */
2314
2315 static hashval_t
2316 mbc_entry_hash (const void *entry)
2317 {
2318   const struct mbc_entry *e = entry;
2319
2320   return 57 * (hashval_t) e->mode + (hashval_t) (e->cst % 877);
2321 }
2322
2323 /* Compares the hash table entries ENTRY1 and ENTRY2.  */
2324
2325 static int
2326 mbc_entry_eq (const void *entry1, const void *entry2)
2327 {
2328   const struct mbc_entry *e1 = entry1;
2329   const struct mbc_entry *e2 = entry2;
2330
2331   return (e1->mode == e2->mode
2332           && e1->cst == e2->cst);
2333 }
2334
2335 /* Returns cost of multiplication by constant CST in MODE.  */
2336
2337 static unsigned
2338 multiply_by_cost (HOST_WIDE_INT cst, enum machine_mode mode)
2339 {
2340   static htab_t costs;
2341   struct mbc_entry **cached, act;
2342   rtx seq;
2343   unsigned cost;
2344
2345   if (!costs)
2346     costs = htab_create (100, mbc_entry_hash, mbc_entry_eq, free);
2347
2348   act.mode = mode;
2349   act.cst = cst;
2350   cached = (struct mbc_entry **) htab_find_slot (costs, &act, INSERT);
2351   if (*cached)
2352     return (*cached)->cost;
2353
2354   *cached = xmalloc (sizeof (struct mbc_entry));
2355   (*cached)->mode = mode;
2356   (*cached)->cst = cst;
2357
2358   start_sequence ();
2359   expand_mult (mode, gen_raw_REG (mode, FIRST_PSEUDO_REGISTER), GEN_INT (cst),
2360                NULL_RTX, 0);
2361   seq = get_insns ();
2362   end_sequence ();
2363   
2364   cost = seq_cost (seq);
2365
2366   if (dump_file && (dump_flags & TDF_DETAILS))
2367     fprintf (dump_file, "Multiplication by %d in %s costs %d\n",
2368              (int) cst, GET_MODE_NAME (mode), cost);
2369
2370   (*cached)->cost = cost;
2371
2372   return cost;
2373 }
2374
2375 /* Returns cost of address in shape symbol + var + OFFSET + RATIO * index.
2376    If SYMBOL_PRESENT is false, symbol is omitted.  If VAR_PRESENT is false,
2377    variable is omitted.  The created memory accesses MODE.
2378    
2379    TODO -- there must be some better way.  This all is quite crude.  */
2380
2381 static unsigned
2382 get_address_cost (bool symbol_present, bool var_present,
2383                   unsigned HOST_WIDE_INT offset, HOST_WIDE_INT ratio)
2384 {
2385 #define MAX_RATIO 128
2386   static sbitmap valid_mult;
2387   static HOST_WIDE_INT rat, off;
2388   static HOST_WIDE_INT min_offset, max_offset;
2389   static unsigned costs[2][2][2][2];
2390   unsigned cost, acost;
2391   rtx seq, addr, base;
2392   bool offset_p, ratio_p;
2393   rtx reg1;
2394   HOST_WIDE_INT s_offset;
2395   unsigned HOST_WIDE_INT mask;
2396   unsigned bits;
2397
2398   if (!valid_mult)
2399     {
2400       HOST_WIDE_INT i;
2401
2402       reg1 = gen_raw_REG (Pmode, FIRST_PSEUDO_REGISTER);
2403
2404       addr = gen_rtx_fmt_ee (PLUS, Pmode, reg1, NULL_RTX);
2405       for (i = 1; i <= 1 << 20; i <<= 1)
2406         {
2407           XEXP (addr, 1) = GEN_INT (i);
2408           if (!memory_address_p (Pmode, addr))
2409             break;
2410         }
2411       max_offset = i >> 1;
2412       off = max_offset;
2413
2414       for (i = 1; i <= 1 << 20; i <<= 1)
2415         {
2416           XEXP (addr, 1) = GEN_INT (-i);
2417           if (!memory_address_p (Pmode, addr))
2418             break;
2419         }
2420       min_offset = -(i >> 1);
2421
2422       if (dump_file && (dump_flags & TDF_DETAILS))
2423         {
2424           fprintf (dump_file, "get_address_cost:\n");
2425           fprintf (dump_file, "  min offset %d\n", (int) min_offset);
2426           fprintf (dump_file, "  max offset %d\n", (int) max_offset);
2427         }
2428
2429       valid_mult = sbitmap_alloc (2 * MAX_RATIO + 1);
2430       sbitmap_zero (valid_mult);
2431       rat = 1;
2432       addr = gen_rtx_fmt_ee (MULT, Pmode, reg1, NULL_RTX);
2433       for (i = -MAX_RATIO; i <= MAX_RATIO; i++)
2434         {
2435           XEXP (addr, 1) = GEN_INT (i);
2436           if (memory_address_p (Pmode, addr))
2437             {
2438               SET_BIT (valid_mult, i + MAX_RATIO);
2439               rat = i;
2440             }
2441         }
2442
2443       if (dump_file && (dump_flags & TDF_DETAILS))
2444         {
2445           fprintf (dump_file, "  allowed multipliers:");
2446           for (i = -MAX_RATIO; i <= MAX_RATIO; i++)
2447             if (TEST_BIT (valid_mult, i + MAX_RATIO))
2448               fprintf (dump_file, " %d", (int) i);
2449           fprintf (dump_file, "\n");
2450           fprintf (dump_file, "\n");
2451         }
2452     }
2453
2454   bits = GET_MODE_BITSIZE (Pmode);
2455   mask = ~(~(unsigned HOST_WIDE_INT) 0 << (bits - 1) << 1);
2456   offset &= mask;
2457   if ((offset >> (bits - 1) & 1))
2458     offset |= ~mask;
2459   s_offset = offset;
2460
2461   cost = 0;
2462   offset_p = (min_offset <= s_offset && s_offset <= max_offset);
2463   ratio_p = (ratio != 1
2464              && -MAX_RATIO <= ratio && ratio <= MAX_RATIO
2465              && TEST_BIT (valid_mult, ratio + MAX_RATIO));
2466
2467   if (ratio != 1 && !ratio_p)
2468     cost += multiply_by_cost (ratio, Pmode);
2469
2470   if (s_offset && !offset_p && !symbol_present)
2471     {
2472       cost += add_cost (Pmode);
2473       var_present = true;
2474     }
2475
2476   acost = costs[symbol_present][var_present][offset_p][ratio_p];
2477   if (!acost)
2478     {
2479       acost = 0;
2480       
2481       addr = gen_raw_REG (Pmode, FIRST_PSEUDO_REGISTER);
2482       reg1 = gen_raw_REG (Pmode, FIRST_PSEUDO_REGISTER + 1);
2483       if (ratio_p)
2484         addr = gen_rtx_fmt_ee (MULT, Pmode, addr, GEN_INT (rat));
2485
2486       if (symbol_present)
2487         {
2488           base = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (""));
2489           if (offset_p)
2490             base = gen_rtx_fmt_e (CONST, Pmode,
2491                                   gen_rtx_fmt_ee (PLUS, Pmode,
2492                                                   base,
2493                                                   GEN_INT (off)));
2494           if (var_present)
2495             base = gen_rtx_fmt_ee (PLUS, Pmode, reg1, base);
2496         }
2497
2498       else if (var_present)
2499         {
2500           base = reg1;
2501           if (offset_p)
2502             base = gen_rtx_fmt_ee (PLUS, Pmode, base, GEN_INT (off));
2503         }
2504       else if (offset_p)
2505         base = GEN_INT (off);
2506       else
2507         base = NULL_RTX;
2508     
2509       if (base)
2510         addr = gen_rtx_fmt_ee (PLUS, Pmode, base, addr);
2511   
2512       start_sequence ();
2513       addr = memory_address (Pmode, addr);
2514       seq = get_insns ();
2515       end_sequence ();
2516   
2517       acost = seq_cost (seq);
2518       acost += address_cost (addr, Pmode);
2519
2520       if (!acost)
2521         acost = 1;
2522       costs[symbol_present][var_present][offset_p][ratio_p] = acost;
2523     }
2524
2525   return cost + acost;
2526 }
2527
2528 /* Records invariants in *EXPR_P.  Callback for walk_tree.  DATA contains
2529    the bitmap to that we should store it.  */
2530
2531 static struct ivopts_data *fd_ivopts_data;
2532 static tree
2533 find_depends (tree *expr_p, int *ws ATTRIBUTE_UNUSED, void *data)
2534 {
2535   bitmap *depends_on = data;
2536   struct version_info *info;
2537
2538   if (TREE_CODE (*expr_p) != SSA_NAME)
2539     return NULL_TREE;
2540   info = name_info (fd_ivopts_data, *expr_p);
2541
2542   if (!info->inv_id || info->has_nonlin_use)
2543     return NULL_TREE;
2544
2545   if (!*depends_on)
2546     *depends_on = BITMAP_XMALLOC ();
2547   bitmap_set_bit (*depends_on, info->inv_id);
2548
2549   return NULL_TREE;
2550 }
2551
2552 /* Estimates cost of forcing EXPR into variable.  DEPENDS_ON is a set of the
2553    invariants the computation depends on.  */
2554
2555 static unsigned
2556 force_var_cost (struct ivopts_data *data,
2557                 tree expr, bitmap *depends_on)
2558 {
2559   static bool costs_initialized = false;
2560   static unsigned integer_cost;
2561   static unsigned symbol_cost;
2562   static unsigned address_cost;
2563
2564   if (!costs_initialized)
2565     {
2566       tree var = create_tmp_var_raw (integer_type_node, "test_var");
2567       rtx x = gen_rtx_MEM (DECL_MODE (var),
2568                            gen_rtx_SYMBOL_REF (Pmode, "test_var"));
2569       tree addr;
2570       tree type = build_pointer_type (integer_type_node);
2571
2572       integer_cost = computation_cost (build_int_cst_type (integer_type_node,
2573                                                            2000));
2574
2575       SET_DECL_RTL (var, x);
2576       TREE_STATIC (var) = 1;
2577       addr = build1 (ADDR_EXPR, type, var);
2578       symbol_cost = computation_cost (addr) + 1;
2579
2580       address_cost
2581         = computation_cost (build2 (PLUS_EXPR, type,
2582                                     addr,
2583                                     build_int_cst_type (type, 2000))) + 1;
2584       if (dump_file && (dump_flags & TDF_DETAILS))
2585         {
2586           fprintf (dump_file, "force_var_cost:\n");
2587           fprintf (dump_file, "  integer %d\n", (int) integer_cost);
2588           fprintf (dump_file, "  symbol %d\n", (int) symbol_cost);
2589           fprintf (dump_file, "  address %d\n", (int) address_cost);
2590           fprintf (dump_file, "  other %d\n", (int) target_spill_cost);
2591           fprintf (dump_file, "\n");
2592         }
2593
2594       costs_initialized = true;
2595     }
2596
2597   if (depends_on)
2598     {
2599       fd_ivopts_data = data;
2600       walk_tree (&expr, find_depends, depends_on, NULL);
2601     }
2602
2603   if (SSA_VAR_P (expr))
2604     return 0;
2605
2606   if (TREE_INVARIANT (expr))
2607     {
2608       if (TREE_CODE (expr) == INTEGER_CST)
2609         return integer_cost;
2610
2611       if (TREE_CODE (expr) == ADDR_EXPR)
2612         {
2613           tree obj = TREE_OPERAND (expr, 0);
2614
2615           if (TREE_CODE (obj) == VAR_DECL
2616               || TREE_CODE (obj) == PARM_DECL
2617               || TREE_CODE (obj) == RESULT_DECL)
2618             return symbol_cost;
2619         }
2620
2621       return address_cost;
2622     }
2623
2624   /* Just an arbitrary value, FIXME.  */
2625   return target_spill_cost;
2626 }
2627
2628 /* Estimates cost of expressing address ADDR  as var + symbol + offset.  The
2629    value of offset is added to OFFSET, SYMBOL_PRESENT and VAR_PRESENT are set
2630    to false if the corresponding part is missing.  DEPENDS_ON is a set of the
2631    invariants the computation depends on.  */
2632
2633 static unsigned
2634 split_address_cost (struct ivopts_data *data,
2635                     tree addr, bool *symbol_present, bool *var_present,
2636                     unsigned HOST_WIDE_INT *offset, bitmap *depends_on)
2637 {
2638   tree core;
2639   HOST_WIDE_INT bitsize;
2640   HOST_WIDE_INT bitpos;
2641   tree toffset;
2642   enum machine_mode mode;
2643   int unsignedp, volatilep;
2644   
2645   core = get_inner_reference (addr, &bitsize, &bitpos, &toffset, &mode,
2646                               &unsignedp, &volatilep);
2647
2648   if (toffset != 0
2649       || bitpos % BITS_PER_UNIT != 0
2650       || TREE_CODE (core) != VAR_DECL)
2651     {
2652       *symbol_present = false;
2653       *var_present = true;
2654       fd_ivopts_data = data;
2655       walk_tree (&addr, find_depends, depends_on, NULL);
2656       return target_spill_cost;
2657     }
2658
2659   *offset += bitpos / BITS_PER_UNIT;
2660   if (TREE_STATIC (core)
2661       || DECL_EXTERNAL (core))
2662     {
2663       *symbol_present = true;
2664       *var_present = false;
2665       return 0;
2666     }
2667       
2668   *symbol_present = false;
2669   *var_present = true;
2670   return 0;
2671 }
2672
2673 /* Estimates cost of expressing difference of addresses E1 - E2 as
2674    var + symbol + offset.  The value of offset is added to OFFSET,
2675    SYMBOL_PRESENT and VAR_PRESENT are set to false if the corresponding
2676    part is missing.  DEPENDS_ON is a set of the invariants the computation
2677    depends on.  */
2678
2679 static unsigned
2680 ptr_difference_cost (struct ivopts_data *data,
2681                      tree e1, tree e2, bool *symbol_present, bool *var_present,
2682                      unsigned HOST_WIDE_INT *offset, bitmap *depends_on)
2683 {
2684   HOST_WIDE_INT diff = 0;
2685   unsigned cost;
2686
2687   gcc_assert (TREE_CODE (e1) == ADDR_EXPR);
2688
2689   if (TREE_CODE (e2) == ADDR_EXPR
2690       && ptr_difference_const (TREE_OPERAND (e1, 0),
2691                                TREE_OPERAND (e2, 0), &diff))
2692     {
2693       *offset += diff;
2694       *symbol_present = false;
2695       *var_present = false;
2696       return 0;
2697     }
2698
2699   if (e2 == integer_zero_node)
2700     return split_address_cost (data, TREE_OPERAND (e1, 0),
2701                                symbol_present, var_present, offset, depends_on);
2702
2703   *symbol_present = false;
2704   *var_present = true;
2705   
2706   cost = force_var_cost (data, e1, depends_on);
2707   cost += force_var_cost (data, e2, depends_on);
2708   cost += add_cost (Pmode);
2709
2710   return cost;
2711 }
2712
2713 /* Estimates cost of expressing difference E1 - E2 as
2714    var + symbol + offset.  The value of offset is added to OFFSET,
2715    SYMBOL_PRESENT and VAR_PRESENT are set to false if the corresponding
2716    part is missing.  DEPENDS_ON is a set of the invariants the computation
2717    depends on.  */
2718
2719 static unsigned
2720 difference_cost (struct ivopts_data *data,
2721                  tree e1, tree e2, bool *symbol_present, bool *var_present,
2722                  unsigned HOST_WIDE_INT *offset, bitmap *depends_on)
2723 {
2724   unsigned cost;
2725   enum machine_mode mode = TYPE_MODE (TREE_TYPE (e1));
2726
2727   strip_offset (&e1, offset);
2728   *offset = -*offset;
2729   strip_offset (&e2, offset);
2730   *offset = -*offset;
2731
2732   if (TREE_CODE (e1) == ADDR_EXPR)
2733     return ptr_difference_cost (data, e1, e2, symbol_present, var_present, offset,
2734                                 depends_on);
2735   *symbol_present = false;
2736
2737   if (operand_equal_p (e1, e2, 0))
2738     {
2739       *var_present = false;
2740       return 0;
2741     }
2742   *var_present = true;
2743   if (zero_p (e2))
2744     return force_var_cost (data, e1, depends_on);
2745
2746   if (zero_p (e1))
2747     {
2748       cost = force_var_cost (data, e2, depends_on);
2749       cost += multiply_by_cost (-1, mode);
2750
2751       return cost;
2752     }
2753
2754   cost = force_var_cost (data, e1, depends_on);
2755   cost += force_var_cost (data, e2, depends_on);
2756   cost += add_cost (mode);
2757
2758   return cost;
2759 }
2760
2761 /* Determines the cost of the computation by that USE is expressed
2762    from induction variable CAND.  If ADDRESS_P is true, we just need
2763    to create an address from it, otherwise we want to get it into
2764    register.  A set of invariants we depend on is stored in
2765    DEPENDS_ON.  AT is the statement at that the value is computed.  */
2766
2767 static unsigned
2768 get_computation_cost_at (struct ivopts_data *data,
2769                          struct iv_use *use, struct iv_cand *cand,
2770                          bool address_p, bitmap *depends_on, tree at)
2771 {
2772   tree ubase = use->iv->base, ustep = use->iv->step;
2773   tree cbase, cstep;
2774   tree utype = TREE_TYPE (ubase), ctype;
2775   unsigned HOST_WIDE_INT ustepi, cstepi, offset = 0;
2776   HOST_WIDE_INT ratio, aratio;
2777   bool var_present, symbol_present;
2778   unsigned cost = 0, n_sums;
2779
2780   *depends_on = NULL;
2781
2782   /* Only consider real candidates.  */
2783   if (!cand->iv)
2784     return INFTY;
2785
2786   cbase = cand->iv->base;
2787   cstep = cand->iv->step;
2788   ctype = TREE_TYPE (cbase);
2789
2790   if (TYPE_PRECISION (utype) > TYPE_PRECISION (ctype))
2791     {
2792       /* We do not have a precision to express the values of use.  */
2793       return INFTY;
2794     }
2795
2796   if (!cst_and_fits_in_hwi (ustep)
2797       || !cst_and_fits_in_hwi (cstep))
2798     return INFTY;
2799
2800   if (TREE_CODE (ubase) == INTEGER_CST
2801       && !cst_and_fits_in_hwi (ubase))
2802     goto fallback;
2803
2804   if (TREE_CODE (cbase) == INTEGER_CST
2805       && !cst_and_fits_in_hwi (cbase))
2806     goto fallback;
2807     
2808   ustepi = int_cst_value (ustep);
2809   cstepi = int_cst_value (cstep);
2810
2811   if (TYPE_PRECISION (utype) != TYPE_PRECISION (ctype))
2812     {
2813       /* TODO -- add direct handling of this case.  */
2814       goto fallback;
2815     }
2816
2817   if (!divide (TYPE_PRECISION (utype), ustepi, cstepi, &ratio))
2818     return INFTY;
2819
2820   /* use = ubase + ratio * (var - cbase).  If either cbase is a constant
2821      or ratio == 1, it is better to handle this like
2822      
2823      ubase - ratio * cbase + ratio * var
2824      
2825      (also holds in the case ratio == -1, TODO.  */
2826
2827   if (TREE_CODE (cbase) == INTEGER_CST)
2828     {
2829       offset = - ratio * int_cst_value (cbase); 
2830       cost += difference_cost (data,
2831                                ubase, integer_zero_node,
2832                                &symbol_present, &var_present, &offset,
2833                                depends_on);
2834     }
2835   else if (ratio == 1)
2836     {
2837       cost += difference_cost (data,
2838                                ubase, cbase,
2839                                &symbol_present, &var_present, &offset,
2840                                depends_on);
2841     }
2842   else
2843     {
2844       cost += force_var_cost (data, cbase, depends_on);
2845       cost += add_cost (TYPE_MODE (ctype));
2846       cost += difference_cost (data,
2847                                ubase, integer_zero_node,
2848                                &symbol_present, &var_present, &offset,
2849                                depends_on);
2850     }
2851
2852   /* If we are after the increment, the value of the candidate is higher by
2853      one iteration.  */
2854   if (stmt_after_increment (data->current_loop, cand, at))
2855     offset -= ratio * cstepi;
2856
2857   /* Now the computation is in shape symbol + var1 + const + ratio * var2.
2858      (symbol/var/const parts may be omitted).  If we are looking for an address,
2859      find the cost of addressing this.  */
2860   if (address_p)
2861     return get_address_cost (symbol_present, var_present, offset, ratio);
2862
2863   /* Otherwise estimate the costs for computing the expression.  */
2864   aratio = ratio > 0 ? ratio : -ratio;
2865   if (!symbol_present && !var_present && !offset)
2866     {
2867       if (ratio != 1)
2868         cost += multiply_by_cost (ratio, TYPE_MODE (ctype));
2869
2870       return cost;
2871     }
2872
2873   if (aratio != 1)
2874     cost += multiply_by_cost (aratio, TYPE_MODE (ctype));
2875
2876   n_sums = 1;
2877   if (var_present
2878       /* Symbol + offset should be compile-time computable.  */
2879       && (symbol_present || offset))
2880     n_sums++;
2881
2882   return cost + n_sums * add_cost (TYPE_MODE (ctype));
2883
2884 fallback:
2885   {
2886     /* Just get the expression, expand it and measure the cost.  */
2887     tree comp = get_computation_at (data->current_loop, use, cand, at);
2888
2889     if (!comp)
2890       return INFTY;
2891
2892     if (address_p)
2893       comp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (comp)), comp);
2894
2895     return computation_cost (comp);
2896   }
2897 }
2898
2899 /* Determines the cost of the computation by that USE is expressed
2900    from induction variable CAND.  If ADDRESS_P is true, we just need
2901    to create an address from it, otherwise we want to get it into
2902    register.  A set of invariants we depend on is stored in
2903    DEPENDS_ON.  */
2904
2905 static unsigned
2906 get_computation_cost (struct ivopts_data *data,
2907                       struct iv_use *use, struct iv_cand *cand,
2908                       bool address_p, bitmap *depends_on)
2909 {
2910   return get_computation_cost_at (data,
2911                                   use, cand, address_p, depends_on, use->stmt);
2912 }
2913
2914 /* Determines cost of basing replacement of USE on CAND in a generic
2915    expression.  */
2916
2917 static void
2918 determine_use_iv_cost_generic (struct ivopts_data *data,
2919                                struct iv_use *use, struct iv_cand *cand)
2920 {
2921   bitmap depends_on;
2922   unsigned cost = get_computation_cost (data, use, cand, false, &depends_on);
2923
2924   set_use_iv_cost (data, use, cand, cost, depends_on);
2925 }
2926
2927 /* Determines cost of basing replacement of USE on CAND in an address.  */
2928
2929 static void
2930 determine_use_iv_cost_address (struct ivopts_data *data,
2931                                struct iv_use *use, struct iv_cand *cand)
2932 {
2933   bitmap depends_on;
2934   unsigned cost = get_computation_cost (data, use, cand, true, &depends_on);
2935
2936   set_use_iv_cost (data, use, cand, cost, depends_on);
2937 }
2938
2939 /* Computes value of induction variable IV in iteration NITER.  */
2940
2941 static tree
2942 iv_value (struct iv *iv, tree niter)
2943 {
2944   tree val;
2945   tree type = TREE_TYPE (iv->base);
2946
2947   niter = fold_convert (type, niter);
2948   val = fold (build2 (MULT_EXPR, type, iv->step, niter));
2949
2950   return fold (build2 (PLUS_EXPR, type, iv->base, val));
2951 }
2952
2953 /* Computes value of candidate CAND at position AT in iteration NITER.  */
2954
2955 static tree
2956 cand_value_at (struct loop *loop, struct iv_cand *cand, tree at, tree niter)
2957 {
2958   tree val = iv_value (cand->iv, niter);
2959   tree type = TREE_TYPE (cand->iv->base);
2960
2961   if (stmt_after_increment (loop, cand, at))
2962     val = fold (build2 (PLUS_EXPR, type, val, cand->iv->step));
2963
2964   return val;
2965 }
2966
2967 /* Check whether it is possible to express the condition in USE by comparison
2968    of candidate CAND.  If so, store the comparison code to COMPARE and the
2969    value compared with to BOUND.  */
2970
2971 static bool
2972 may_eliminate_iv (struct loop *loop,
2973                   struct iv_use *use, struct iv_cand *cand,
2974                   enum tree_code *compare, tree *bound)
2975 {
2976   edge exit;
2977   struct tree_niter_desc *niter, new_niter;
2978   tree wider_type, type, base;
2979
2980   /* For now just very primitive -- we work just for the single exit condition,
2981      and are quite conservative about the possible overflows.  TODO -- both of
2982      these can be improved.  */
2983   exit = single_dom_exit (loop);
2984   if (!exit)
2985     return false;
2986   if (use->stmt != last_stmt (exit->src))
2987     return false;
2988
2989   niter = &loop_data (loop)->niter;
2990   if (!niter->niter
2991       || !integer_nonzerop (niter->assumptions)
2992       || !integer_zerop (niter->may_be_zero))
2993     return false;
2994
2995   if (exit->flags & EDGE_TRUE_VALUE)
2996     *compare = EQ_EXPR;
2997   else
2998     *compare = NE_EXPR;
2999
3000   *bound = cand_value_at (loop, cand, use->stmt, niter->niter);
3001
3002   /* Let us check there is not some problem with overflows, by checking that
3003      the number of iterations is unchanged.  */
3004   base = cand->iv->base;
3005   type = TREE_TYPE (base);
3006   if (stmt_after_increment (loop, cand, use->stmt))
3007     base = fold (build2 (PLUS_EXPR, type, base, cand->iv->step));
3008
3009   new_niter.niter = NULL_TREE;
3010   number_of_iterations_cond (TREE_TYPE (cand->iv->base), base,
3011                              cand->iv->step, NE_EXPR, *bound, NULL_TREE,
3012                              &new_niter);
3013   if (!new_niter.niter
3014       || !integer_nonzerop (new_niter.assumptions)
3015       || !integer_zerop (new_niter.may_be_zero))
3016     return false;
3017
3018   wider_type = TREE_TYPE (new_niter.niter);
3019   if (TYPE_PRECISION (wider_type) < TYPE_PRECISION (TREE_TYPE (niter->niter)))
3020     wider_type = TREE_TYPE (niter->niter);
3021   if (!operand_equal_p (fold_convert (wider_type, niter->niter),
3022                         fold_convert (wider_type, new_niter.niter), 0))
3023     return false;
3024
3025   return true;
3026 }
3027
3028 /* Determines cost of basing replacement of USE on CAND in a condition.  */
3029
3030 static void
3031 determine_use_iv_cost_condition (struct ivopts_data *data,
3032                                  struct iv_use *use, struct iv_cand *cand)
3033 {
3034   tree bound;
3035   enum tree_code compare;
3036
3037   /* Only consider real candidates.  */
3038   if (!cand->iv)
3039     {
3040       set_use_iv_cost (data, use, cand, INFTY, NULL);
3041       return;
3042     }
3043
3044   if (may_eliminate_iv (data->current_loop, use, cand, &compare, &bound))
3045     {
3046       bitmap depends_on = NULL;
3047       unsigned cost = force_var_cost (data, bound, &depends_on);
3048
3049       set_use_iv_cost (data, use, cand, cost, depends_on);
3050       return;
3051     }
3052
3053   /* The induction variable elimination failed; just express the original
3054      giv.  If it is compared with an invariant, note that we cannot get
3055      rid of it.  */
3056   if (TREE_CODE (*use->op_p) == SSA_NAME)
3057     record_invariant (data, *use->op_p, true);
3058   else
3059     {
3060       record_invariant (data, TREE_OPERAND (*use->op_p, 0), true);
3061       record_invariant (data, TREE_OPERAND (*use->op_p, 1), true);
3062     }
3063
3064   determine_use_iv_cost_generic (data, use, cand);
3065 }
3066
3067 /* Checks whether it is possible to replace the final value of USE by
3068    a direct computation.  If so, the formula is stored to *VALUE.  */
3069
3070 static bool
3071 may_replace_final_value (struct loop *loop, struct iv_use *use, tree *value)
3072 {
3073   edge exit;
3074   struct tree_niter_desc *niter;
3075
3076   exit = single_dom_exit (loop);
3077   if (!exit)
3078     return false;
3079
3080   gcc_assert (dominated_by_p (CDI_DOMINATORS, exit->src,
3081                               bb_for_stmt (use->stmt)));
3082
3083   niter = &loop_data (loop)->niter;
3084   if (!niter->niter
3085       || !operand_equal_p (niter->assumptions, boolean_true_node, 0)
3086       || !operand_equal_p (niter->may_be_zero, boolean_false_node, 0))
3087     return false;
3088
3089   *value = iv_value (use->iv, niter->niter);
3090
3091   return true;
3092 }
3093
3094 /* Determines cost of replacing final value of USE using CAND.  */
3095
3096 static void
3097 determine_use_iv_cost_outer (struct ivopts_data *data,
3098                              struct iv_use *use, struct iv_cand *cand)
3099 {
3100   bitmap depends_on;
3101   unsigned cost;
3102   edge exit;
3103   tree value;
3104   struct loop *loop = data->current_loop;
3105           
3106   if (!cand->iv)
3107     {
3108       if (!may_replace_final_value (loop, use, &value))
3109         {
3110           set_use_iv_cost (data, use, cand, INFTY, NULL);
3111           return;
3112         }
3113
3114       depends_on = NULL;
3115       cost = force_var_cost (data, value, &depends_on);
3116
3117       cost /= AVG_LOOP_NITER (loop);
3118
3119       set_use_iv_cost (data, use, cand, cost, depends_on);
3120       return;
3121     }
3122
3123   exit = single_dom_exit (loop);
3124   if (exit)
3125     {
3126       /* If there is just a single exit, we may use value of the candidate
3127          after we take it to determine the value of use.  */
3128       cost = get_computation_cost_at (data, use, cand, false, &depends_on,
3129                                       last_stmt (exit->src));
3130       if (cost != INFTY)
3131         cost /= AVG_LOOP_NITER (loop);
3132     }
3133   else
3134     {
3135       /* Otherwise we just need to compute the iv.  */
3136       cost = get_computation_cost (data, use, cand, false, &depends_on);
3137     }
3138                                    
3139   set_use_iv_cost (data, use, cand, cost, depends_on);
3140 }
3141
3142 /* Determines cost of basing replacement of USE on CAND.  */
3143
3144 static void
3145 determine_use_iv_cost (struct ivopts_data *data,
3146                        struct iv_use *use, struct iv_cand *cand)
3147 {
3148   switch (use->type)
3149     {
3150     case USE_NONLINEAR_EXPR:
3151       determine_use_iv_cost_generic (data, use, cand);
3152       break;
3153
3154     case USE_OUTER:
3155       determine_use_iv_cost_outer (data, use, cand);
3156       break;
3157
3158     case USE_ADDRESS:
3159       determine_use_iv_cost_address (data, use, cand);
3160       break;
3161
3162     case USE_COMPARE:
3163       determine_use_iv_cost_condition (data, use, cand);
3164       break;
3165
3166     default:
3167       gcc_unreachable ();
3168     }
3169 }
3170
3171 /* Determines costs of basing the use of the iv on an iv candidate.  */
3172
3173 static void
3174 determine_use_iv_costs (struct ivopts_data *data)
3175 {
3176   unsigned i, j;
3177   struct iv_use *use;
3178   struct iv_cand *cand;
3179
3180   data->consider_all_candidates = (n_iv_cands (data)
3181                                    <= CONSIDER_ALL_CANDIDATES_BOUND);
3182
3183   alloc_use_cost_map (data);
3184
3185   if (!data->consider_all_candidates)
3186     {
3187       /* Add the important candidate entries.  */
3188       for (j = 0; j < n_iv_cands (data); j++)
3189         {
3190           cand = iv_cand (data, j);
3191           if (!cand->important)
3192             continue;
3193           for (i = 0; i < n_iv_uses (data); i++)
3194             {
3195               use = iv_use (data, i);
3196               determine_use_iv_cost (data, use, cand);
3197             }
3198         }
3199     }
3200
3201   for (i = 0; i < n_iv_uses (data); i++)
3202     {
3203       use = iv_use (data, i);
3204
3205       if (data->consider_all_candidates)
3206         {
3207           for (j = 0; j < n_iv_cands (data); j++)
3208             {
3209               cand = iv_cand (data, j);
3210               determine_use_iv_cost (data, use, cand);
3211             }
3212         }
3213       else
3214         {
3215           bitmap_iterator bi;
3216
3217           EXECUTE_IF_SET_IN_BITMAP (use->related_cands, 0, j, bi)
3218             {
3219               cand = iv_cand (data, j);
3220               if (!cand->important)
3221                 determine_use_iv_cost (data, use, cand);
3222             }
3223         }
3224     }
3225
3226   if (dump_file && (dump_flags & TDF_DETAILS))
3227     {
3228       fprintf (dump_file, "Use-candidate costs:\n");
3229
3230       for (i = 0; i < n_iv_uses (data); i++)
3231         {
3232           use = iv_use (data, i);
3233
3234           fprintf (dump_file, "Use %d:\n", i);
3235           fprintf (dump_file, "  cand\tcost\tdepends on\n");
3236           for (j = 0; j < use->n_map_members; j++)
3237             {
3238               if (!use->cost_map[j].cand
3239                   || use->cost_map[j].cost == INFTY)
3240                 continue;
3241
3242               fprintf (dump_file, "  %d\t%d\t",
3243                        use->cost_map[j].cand->id,
3244                        use->cost_map[j].cost);
3245               if (use->cost_map[j].depends_on)
3246                 bitmap_print (dump_file,
3247                               use->cost_map[j].depends_on, "","");
3248               fprintf (dump_file, "\n");
3249             }
3250
3251           fprintf (dump_file, "\n");
3252         }
3253       fprintf (dump_file, "\n");
3254     }
3255 }
3256
3257 /* Determines cost of the candidate CAND.  */
3258
3259 static void
3260 determine_iv_cost (struct ivopts_data *data, struct iv_cand *cand)
3261 {
3262   unsigned cost_base, cost_step;
3263   tree base, last;
3264   basic_block bb;
3265
3266   if (!cand->iv)
3267     {
3268       cand->cost = 0;
3269       return;
3270     }
3271
3272   /* There are two costs associated with the candidate -- its increment
3273      and its initialization.  The second is almost negligible for any loop
3274      that rolls enough, so we take it just very little into account.  */
3275
3276   base = cand->iv->base;
3277   cost_base = force_var_cost (data, base, NULL);
3278   cost_step = add_cost (TYPE_MODE (TREE_TYPE (base)));
3279
3280   cand->cost = cost_step + cost_base / AVG_LOOP_NITER (current_loop);
3281
3282   /* Prefer the original iv unless we may gain something by replacing it.  */
3283   if (cand->pos == IP_ORIGINAL)
3284     cand->cost--;
3285   
3286   /* Prefer not to insert statements into latch unless there are some
3287      already (so that we do not create unnecessary jumps).  */
3288   if (cand->pos == IP_END)
3289     {
3290       bb = ip_end_pos (data->current_loop);
3291       last = last_stmt (bb);
3292
3293       if (!last
3294           || TREE_CODE (last) == LABEL_EXPR)
3295         cand->cost++;
3296     }
3297 }
3298
3299 /* Determines costs of computation of the candidates.  */
3300
3301 static void
3302 determine_iv_costs (struct ivopts_data *data)
3303 {
3304   unsigned i;
3305
3306   if (dump_file && (dump_flags & TDF_DETAILS))
3307     {
3308       fprintf (dump_file, "Candidate costs:\n");
3309       fprintf (dump_file, "  cand\tcost\n");
3310     }
3311
3312   for (i = 0; i < n_iv_cands (data); i++)
3313     {
3314       struct iv_cand *cand = iv_cand (data, i);
3315
3316       determine_iv_cost (data, cand);
3317
3318       if (dump_file && (dump_flags & TDF_DETAILS))
3319         fprintf (dump_file, "  %d\t%d\n", i, cand->cost);
3320     }
3321   
3322 if (dump_file && (dump_flags & TDF_DETAILS))
3323       fprintf (dump_file, "\n");
3324 }
3325
3326 /* Calculates cost for having SIZE induction variables.  */
3327
3328 static unsigned
3329 ivopts_global_cost_for_size (struct ivopts_data *data, unsigned size)
3330 {
3331   return global_cost_for_size (size,
3332                                loop_data (data->current_loop)->regs_used,
3333                                n_iv_uses (data));
3334 }
3335
3336 /* For each size of the induction variable set determine the penalty.  */
3337
3338 static void
3339 determine_set_costs (struct ivopts_data *data)
3340 {
3341   unsigned j, n;
3342   tree phi, op;
3343   struct loop *loop = data->current_loop;
3344   bitmap_iterator bi;
3345
3346   /* We use the following model (definitely improvable, especially the
3347      cost function -- TODO):
3348
3349      We estimate the number of registers available (using MD data), name it A.
3350
3351      We estimate the number of registers used by the loop, name it U.  This
3352      number is obtained as the number of loop phi nodes (not counting virtual
3353      registers and bivs) + the number of variables from outside of the loop.
3354
3355      We set a reserve R (free regs that are used for temporary computations,
3356      etc.).  For now the reserve is a constant 3.
3357
3358      Let I be the number of induction variables.
3359      
3360      -- if U + I + R <= A, the cost is I * SMALL_COST (just not to encourage
3361         make a lot of ivs without a reason).
3362      -- if A - R < U + I <= A, the cost is I * PRES_COST
3363      -- if U + I > A, the cost is I * PRES_COST and
3364         number of uses * SPILL_COST * (U + I - A) / (U + I) is added.  */
3365
3366   if (dump_file && (dump_flags & TDF_DETAILS))
3367     {
3368       fprintf (dump_file, "Global costs:\n");
3369       fprintf (dump_file, "  target_avail_regs %d\n", target_avail_regs);
3370       fprintf (dump_file, "  target_small_cost %d\n", target_small_cost);
3371       fprintf (dump_file, "  target_pres_cost %d\n", target_pres_cost);
3372       fprintf (dump_file, "  target_spill_cost %d\n", target_spill_cost);
3373     }
3374
3375   n = 0;
3376   for (phi = phi_nodes (loop->header); phi; phi = TREE_CHAIN (phi))
3377     {
3378       op = PHI_RESULT (phi);
3379
3380       if (!is_gimple_reg (op))
3381         continue;
3382
3383       if (get_iv (data, op))
3384         continue;
3385
3386       n++;
3387     }
3388
3389   EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, j, bi)
3390     {
3391       struct version_info *info = ver_info (data, j);
3392
3393       if (info->inv_id && info->has_nonlin_use)
3394         n++;
3395     }
3396
3397   loop_data (loop)->regs_used = n;
3398   if (dump_file && (dump_flags & TDF_DETAILS))
3399     fprintf (dump_file, "  regs_used %d\n", n);
3400
3401   if (dump_file && (dump_flags & TDF_DETAILS))
3402     {
3403       fprintf (dump_file, "  cost for size:\n");
3404       fprintf (dump_file, "  ivs\tcost\n");
3405       for (j = 0; j <= 2 * target_avail_regs; j++)
3406         fprintf (dump_file, "  %d\t%d\n", j,
3407                  ivopts_global_cost_for_size (data, j));
3408       fprintf (dump_file, "\n");
3409     }
3410 }
3411
3412 /* Finds a best candidate for USE and stores it to CAND.  The candidates are
3413    taken from the set SOL and they may depend on invariants in the set INV.
3414    The really used candidate and invariants are noted to USED_IVS and
3415    USED_INV.  */
3416
3417 static unsigned
3418 find_best_candidate (struct ivopts_data *data,
3419                      struct iv_use *use, bitmap sol, bitmap inv,
3420                      bitmap used_ivs, bitmap used_inv, struct iv_cand **cand)
3421 {
3422   unsigned c, d;
3423   unsigned best_cost = INFTY, cost;
3424   struct iv_cand *cnd = NULL, *acnd;
3425   bitmap depends_on = NULL, asol;
3426   bitmap_iterator bi, bi1;
3427
3428   if (data->consider_all_candidates)
3429     asol = sol;
3430   else
3431     {
3432       asol = BITMAP_XMALLOC ();
3433       bitmap_a_and_b (asol, sol, use->related_cands);
3434     }
3435
3436   EXECUTE_IF_SET_IN_BITMAP (asol, 0, c, bi)
3437     {
3438       acnd = iv_cand (data, c);
3439       cost = get_use_iv_cost (data, use, acnd, &depends_on);
3440
3441       if (cost == INFTY)
3442         continue;
3443       if (cost > best_cost)
3444         continue;
3445       if (cost == best_cost)
3446         {
3447           /* Prefer the cheaper iv.  */
3448           if (acnd->cost >= cnd->cost)
3449             continue;
3450         }
3451
3452       if (depends_on)
3453         {
3454           EXECUTE_IF_AND_COMPL_IN_BITMAP (depends_on, inv, 0, d, bi1)
3455             {
3456               goto next_cand;
3457             }
3458           if (used_inv)
3459             bitmap_a_or_b (used_inv, used_inv, depends_on);
3460         }
3461
3462       cnd = acnd;
3463       best_cost = cost;
3464
3465 next_cand: ;
3466     }
3467
3468   if (cnd && used_ivs)
3469     bitmap_set_bit (used_ivs, cnd->id);
3470
3471   if (cand)
3472     *cand = cnd;
3473
3474   if (!data->consider_all_candidates)
3475     BITMAP_XFREE (asol);
3476
3477   return best_cost;
3478 }
3479
3480 /* Returns cost of set of ivs SOL + invariants INV.  Removes unnecessary
3481    induction variable candidates and invariants from the sets.  Only
3482    uses 0 .. MAX_USE - 1 are taken into account.  */
3483
3484 static unsigned
3485 set_cost_up_to (struct ivopts_data *data, bitmap sol, bitmap inv,
3486                 unsigned max_use)
3487 {
3488   unsigned i;
3489   unsigned cost = 0, size = 0, acost;
3490   struct iv_use *use;
3491   struct iv_cand *cand;
3492   bitmap used_ivs = BITMAP_XMALLOC (), used_inv = BITMAP_XMALLOC ();
3493   bitmap_iterator bi;
3494
3495   for (i = 0; i < max_use; i++)
3496     {
3497       use = iv_use (data, i);
3498       acost = find_best_candidate (data, use, sol, inv,
3499                                    used_ivs, used_inv, NULL);
3500       if (acost == INFTY)
3501         {
3502           BITMAP_XFREE (used_ivs);
3503           BITMAP_XFREE (used_inv);
3504           return INFTY;
3505         }
3506       cost += acost;
3507     }
3508
3509   EXECUTE_IF_SET_IN_BITMAP (used_ivs, 0, i, bi)
3510     {
3511       cand = iv_cand (data, i);
3512
3513       /* Do not count the pseudocandidates.  */
3514       if (cand->iv)
3515         size++;
3516
3517       cost += cand->cost;
3518     }
3519   EXECUTE_IF_SET_IN_BITMAP (used_inv, 0, i, bi)
3520     {
3521       size++;
3522     }
3523   cost += ivopts_global_cost_for_size (data, size);
3524
3525   bitmap_copy (sol, used_ivs);
3526   bitmap_copy (inv, used_inv);
3527
3528   BITMAP_XFREE (used_ivs);
3529   BITMAP_XFREE (used_inv);
3530
3531   return cost;
3532 }
3533
3534 /* Computes cost of set of ivs SOL + invariants INV.  Removes unnecessary
3535    induction variable candidates and invariants from the sets.  */
3536
3537 static unsigned
3538 set_cost (struct ivopts_data *data, bitmap sol, bitmap inv)
3539 {
3540   return set_cost_up_to (data, sol, inv, n_iv_uses (data));
3541 }
3542
3543 /* Tries to extend the sets IVS and INV in the best possible way in order
3544    to express the USE.  */
3545
3546 static bool
3547 try_add_cand_for (struct ivopts_data *data, bitmap ivs, bitmap inv,
3548                   struct iv_use *use)
3549 {
3550   unsigned best_cost = set_cost_up_to (data, ivs, inv, use->id + 1), act_cost;
3551   bitmap best_ivs = BITMAP_XMALLOC ();
3552   bitmap best_inv = BITMAP_XMALLOC ();
3553   bitmap act_ivs = BITMAP_XMALLOC ();
3554   bitmap act_inv = BITMAP_XMALLOC ();
3555   unsigned i;
3556   struct cost_pair *cp;
3557
3558   bitmap_copy (best_ivs, ivs);
3559   bitmap_copy (best_inv, inv);
3560
3561   for (i = 0; i < use->n_map_members; i++)
3562     {
3563       cp = use->cost_map + i;
3564       if (cp->cost == INFTY)
3565         continue;
3566
3567       bitmap_copy (act_ivs, ivs);
3568       bitmap_set_bit (act_ivs, cp->cand->id);
3569       if (cp->depends_on)
3570         bitmap_a_or_b (act_inv, inv, cp->depends_on);
3571       else
3572         bitmap_copy (act_inv, inv);
3573       act_cost = set_cost_up_to (data, act_ivs, act_inv, use->id + 1);
3574
3575       if (act_cost < best_cost)
3576         {
3577           best_cost = act_cost;
3578           bitmap_copy (best_ivs, act_ivs);
3579           bitmap_copy (best_inv, act_inv);
3580         }
3581     }
3582
3583   bitmap_copy (ivs, best_ivs);
3584   bitmap_copy (inv, best_inv);
3585
3586   BITMAP_XFREE (best_ivs);
3587   BITMAP_XFREE (best_inv);
3588   BITMAP_XFREE (act_ivs);
3589   BITMAP_XFREE (act_inv);
3590
3591   return (best_cost != INFTY);
3592 }
3593
3594 /* Finds an initial set of IVS and invariants INV.  We do this by simply
3595    choosing the best candidate for each use.  */
3596
3597 static unsigned
3598 get_initial_solution (struct ivopts_data *data, bitmap ivs, bitmap inv)
3599 {
3600   unsigned i;
3601
3602   for (i = 0; i < n_iv_uses (data); i++)
3603     if (!try_add_cand_for (data, ivs, inv, iv_use (data, i)))
3604       return INFTY;
3605
3606   return set_cost (data, ivs, inv);
3607 }
3608
3609 /* Tries to improve set of induction variables IVS and invariants INV to get
3610    it better than COST.  */
3611
3612 static bool
3613 try_improve_iv_set (struct ivopts_data *data,
3614                     bitmap ivs, bitmap inv, unsigned *cost)
3615 {
3616   unsigned i, acost;
3617   bitmap new_ivs = BITMAP_XMALLOC (), new_inv = BITMAP_XMALLOC ();
3618   bitmap best_new_ivs = NULL, best_new_inv = NULL;
3619
3620   /* Try altering the set of induction variables by one.  */
3621   for (i = 0; i < n_iv_cands (data); i++)
3622     {
3623       bitmap_copy (new_ivs, ivs);
3624       bitmap_copy (new_inv, inv);
3625
3626       if (bitmap_bit_p (ivs, i))
3627         bitmap_clear_bit (new_ivs, i);
3628       else
3629         bitmap_set_bit (new_ivs, i);
3630
3631       acost = set_cost (data, new_ivs, new_inv);
3632       if (acost >= *cost)
3633         continue;
3634
3635       if (!best_new_ivs)
3636         {
3637           best_new_ivs = BITMAP_XMALLOC ();
3638           best_new_inv = BITMAP_XMALLOC ();
3639         }
3640
3641       *cost = acost;
3642       bitmap_copy (best_new_ivs, new_ivs);
3643       bitmap_copy (best_new_inv, new_inv);
3644     }
3645
3646   /* Ditto for invariants.  */
3647   for (i = 1; i <= data->max_inv_id; i++)
3648     {
3649       if (ver_info (data, i)->has_nonlin_use)
3650         continue;
3651
3652       bitmap_copy (new_ivs, ivs);
3653       bitmap_copy (new_inv, inv);
3654
3655       if (bitmap_bit_p (inv, i))
3656         bitmap_clear_bit (new_inv, i);
3657       else
3658         bitmap_set_bit (new_inv, i);
3659
3660       acost = set_cost (data, new_ivs, new_inv);
3661       if (acost >= *cost)
3662         continue;
3663
3664       if (!best_new_ivs)
3665         {
3666           best_new_ivs = BITMAP_XMALLOC ();
3667           best_new_inv = BITMAP_XMALLOC ();
3668         }
3669
3670       *cost = acost;
3671       bitmap_copy (best_new_ivs, new_ivs);
3672       bitmap_copy (best_new_inv, new_inv);
3673     }
3674
3675   BITMAP_XFREE (new_ivs);
3676   BITMAP_XFREE (new_inv);
3677
3678   if (!best_new_ivs)
3679     return false;
3680
3681   bitmap_copy (ivs, best_new_ivs);
3682   bitmap_copy (inv, best_new_inv);
3683   BITMAP_XFREE (best_new_ivs);
3684   BITMAP_XFREE (best_new_inv);
3685   return true;
3686 }
3687
3688 /* Attempts to find the optimal set of induction variables.  We do simple
3689    greedy heuristic -- we try to replace at most one candidate in the selected
3690    solution and remove the unused ivs while this improves the cost.  */
3691
3692 static bitmap
3693 find_optimal_iv_set (struct ivopts_data *data)
3694 {
3695   unsigned cost, i;
3696   bitmap set = BITMAP_XMALLOC ();
3697   bitmap inv = BITMAP_XMALLOC ();
3698   struct iv_use *use;
3699
3700   /* Set the upper bound.  */
3701   cost = get_initial_solution (data, set, inv);
3702   if (cost == INFTY)
3703     {
3704       if (dump_file && (dump_flags & TDF_DETAILS))
3705         fprintf (dump_file, "Unable to substitute for ivs, failed.\n");
3706       BITMAP_XFREE (inv);
3707       BITMAP_XFREE (set);
3708       return NULL;
3709     }
3710
3711   if (dump_file && (dump_flags & TDF_DETAILS))
3712     {
3713       fprintf (dump_file, "Initial set of candidates (cost %d): ", cost);
3714       bitmap_print (dump_file, set, "", "");
3715       fprintf (dump_file, " invariants ");
3716       bitmap_print (dump_file, inv, "", "");
3717       fprintf (dump_file, "\n");
3718     }
3719
3720   while (try_improve_iv_set (data, set, inv, &cost))
3721     {
3722       if (dump_file && (dump_flags & TDF_DETAILS))
3723         {
3724           fprintf (dump_file, "Improved to (cost %d): ", cost);
3725           bitmap_print (dump_file, set, "", "");
3726           fprintf (dump_file, " invariants ");
3727           bitmap_print (dump_file, inv, "", "");
3728           fprintf (dump_file, "\n");
3729         }
3730     }
3731
3732   if (dump_file && (dump_flags & TDF_DETAILS))
3733     fprintf (dump_file, "Final cost %d\n\n", cost);
3734
3735   for (i = 0; i < n_iv_uses (data); i++)
3736     {
3737       use = iv_use (data, i);
3738       find_best_candidate (data, use, set, inv, NULL, NULL, &use->selected);
3739     }
3740
3741   BITMAP_XFREE (inv);
3742
3743   return set;
3744 }
3745
3746 /* Creates a new induction variable corresponding to CAND.  */
3747
3748 static void
3749 create_new_iv (struct ivopts_data *data, struct iv_cand *cand)
3750 {
3751   block_stmt_iterator incr_pos;
3752   tree base;
3753   bool after = false;
3754
3755   if (!cand->iv)
3756     return;
3757
3758   switch (cand->pos)
3759     {
3760     case IP_NORMAL:
3761       incr_pos = bsi_last (ip_normal_pos (data->current_loop));
3762       break;
3763
3764     case IP_END:
3765       incr_pos = bsi_last (ip_end_pos (data->current_loop));
3766       after = true;
3767       break;
3768
3769     case IP_ORIGINAL:
3770       /* Mark that the iv is preserved.  */
3771       name_info (data, cand->var_before)->preserve_biv = true;
3772       name_info (data, cand->var_after)->preserve_biv = true;
3773
3774       /* Rewrite the increment so that it uses var_before directly.  */
3775       find_interesting_uses_op (data, cand->var_after)->selected = cand;
3776       
3777       return;
3778     }
3779  
3780   gimple_add_tmp_var (cand->var_before);
3781   add_referenced_tmp_var (cand->var_before);
3782
3783   base = unshare_expr (cand->iv->base);
3784
3785   create_iv (base, cand->iv->step, cand->var_before, data->current_loop,
3786              &incr_pos, after, &cand->var_before, &cand->var_after);
3787 }
3788
3789 /* Creates new induction variables described in SET.  */
3790
3791 static void
3792 create_new_ivs (struct ivopts_data *data, bitmap set)
3793 {
3794   unsigned i;
3795   struct iv_cand *cand;
3796   bitmap_iterator bi;
3797
3798   EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
3799     {
3800       cand = iv_cand (data, i);
3801       create_new_iv (data, cand);
3802     }
3803 }
3804
3805 /* Removes statement STMT (real or a phi node).  If INCLUDING_DEFINED_NAME
3806    is true, remove also the ssa name defined by the statement.  */
3807
3808 static void
3809 remove_statement (tree stmt, bool including_defined_name)
3810 {
3811   if (TREE_CODE (stmt) == PHI_NODE)
3812     {
3813       if (!including_defined_name)
3814         {
3815           /* Prevent the ssa name defined by the statement from being removed.  */
3816           SET_PHI_RESULT (stmt, NULL);
3817         }
3818       remove_phi_node (stmt, NULL_TREE, bb_for_stmt (stmt));
3819     }
3820   else
3821     {
3822       block_stmt_iterator bsi = stmt_for_bsi (stmt);
3823
3824       bsi_remove (&bsi);
3825     }
3826 }
3827
3828 /* Rewrites USE (definition of iv used in a nonlinear expression)
3829    using candidate CAND.  */
3830
3831 static void
3832 rewrite_use_nonlinear_expr (struct ivopts_data *data,
3833                             struct iv_use *use, struct iv_cand *cand)
3834 {
3835   tree comp = unshare_expr (get_computation (data->current_loop,
3836                                              use, cand));
3837   tree op, stmts, tgt, ass;
3838   block_stmt_iterator bsi, pbsi;
3839  
3840   switch (TREE_CODE (use->stmt))
3841     {
3842     case PHI_NODE:
3843       tgt = PHI_RESULT (use->stmt);
3844
3845       /* If we should keep the biv, do not replace it.  */
3846       if (name_info (data, tgt)->preserve_biv)
3847         return;
3848
3849       pbsi = bsi = bsi_start (bb_for_stmt (use->stmt));
3850       while (!bsi_end_p (pbsi)
3851              && TREE_CODE (bsi_stmt (pbsi)) == LABEL_EXPR)
3852         {
3853           bsi = pbsi;
3854           bsi_next (&pbsi);
3855         }
3856       break;
3857
3858     case MODIFY_EXPR:
3859       tgt = TREE_OPERAND (use->stmt, 0);
3860       bsi = stmt_for_bsi (use->stmt);
3861       break;
3862
3863     default:
3864       gcc_unreachable ();
3865     }
3866
3867   op = force_gimple_operand (comp, &stmts, false, SSA_NAME_VAR (tgt));
3868
3869   if (TREE_CODE (use->stmt) == PHI_NODE)
3870     {
3871       if (stmts)
3872         bsi_insert_after (&bsi, stmts, BSI_CONTINUE_LINKING);
3873       ass = build2 (MODIFY_EXPR, TREE_TYPE (tgt), tgt, op);
3874       bsi_insert_after (&bsi, ass, BSI_NEW_STMT);
3875       remove_statement (use->stmt, false);
3876       SSA_NAME_DEF_STMT (tgt) = ass;
3877     }
3878   else
3879     {
3880       if (stmts)
3881         bsi_insert_before (&bsi, stmts, BSI_SAME_STMT);
3882       TREE_OPERAND (use->stmt, 1) = op;
3883     }
3884 }
3885
3886 /* Replaces ssa name in index IDX by its basic variable.  Callback for
3887    for_each_index.  */
3888
3889 static bool
3890 idx_remove_ssa_names (tree base, tree *idx,
3891                       void *data ATTRIBUTE_UNUSED)
3892 {
3893   tree *op;
3894
3895   if (TREE_CODE (*idx) == SSA_NAME)
3896     *idx = SSA_NAME_VAR (*idx);
3897
3898   if (TREE_CODE (base) == ARRAY_REF)
3899     {
3900       op = &TREE_OPERAND (base, 2);
3901       if (*op
3902           && TREE_CODE (*op) == SSA_NAME)
3903         *op = SSA_NAME_VAR (*op);
3904       op = &TREE_OPERAND (base, 3);
3905       if (*op
3906           && TREE_CODE (*op) == SSA_NAME)
3907         *op = SSA_NAME_VAR (*op);
3908     }
3909
3910   return true;
3911 }
3912
3913 /* Unshares REF and replaces ssa names inside it by their basic variables.  */
3914
3915 static tree
3916 unshare_and_remove_ssa_names (tree ref)
3917 {
3918   ref = unshare_expr (ref);
3919   for_each_index (&ref, idx_remove_ssa_names, NULL);
3920
3921   return ref;
3922 }
3923
3924 /* Rewrites base of memory access OP with expression WITH in statement
3925    pointed to by BSI.  */
3926
3927 static void
3928 rewrite_address_base (block_stmt_iterator *bsi, tree *op, tree with)
3929 {
3930   tree bvar, var, new_var, new_name, copy, name;
3931   tree orig;
3932
3933   var = bvar = get_base_address (*op);
3934
3935   if (!var || TREE_CODE (with) != SSA_NAME)
3936     goto do_rewrite;
3937
3938   gcc_assert (TREE_CODE (var) != ALIGN_INDIRECT_REF);
3939   gcc_assert (TREE_CODE (var) != MISALIGNED_INDIRECT_REF);
3940   if (TREE_CODE (var) == INDIRECT_REF)
3941     var = TREE_OPERAND (var, 0);
3942   if (TREE_CODE (var) == SSA_NAME)
3943     {
3944       name = var;
3945       var = SSA_NAME_VAR (var);
3946     }
3947   else if (DECL_P (var))
3948     name = NULL_TREE;
3949   else
3950     goto do_rewrite;
3951     
3952   if (var_ann (var)->type_mem_tag)
3953     var = var_ann (var)->type_mem_tag;
3954
3955   /* We need to add a memory tag for the variable.  But we do not want
3956      to add it to the temporary used for the computations, since this leads
3957      to problems in redundancy elimination when there are common parts
3958      in two computations referring to the different arrays.  So we copy
3959      the variable to a new temporary.  */
3960   copy = build2 (MODIFY_EXPR, void_type_node, NULL_TREE, with);
3961   if (name)
3962     new_name = duplicate_ssa_name (name, copy);
3963   else
3964     {
3965       new_var = create_tmp_var (TREE_TYPE (with), "ruatmp");
3966       add_referenced_tmp_var (new_var);
3967       var_ann (new_var)->type_mem_tag = var;
3968       new_name = make_ssa_name (new_var, copy);
3969     }
3970   TREE_OPERAND (copy, 0) = new_name;
3971   bsi_insert_before (bsi, copy, BSI_SAME_STMT);
3972   with = new_name;
3973
3974 do_rewrite:
3975
3976   orig = NULL_TREE;
3977   gcc_assert (TREE_CODE (*op) != ALIGN_INDIRECT_REF);
3978   gcc_assert (TREE_CODE (*op) != MISALIGNED_INDIRECT_REF);
3979
3980   if (TREE_CODE (*op) == INDIRECT_REF)
3981     orig = REF_ORIGINAL (*op);
3982   if (!orig)
3983     orig = unshare_and_remove_ssa_names (*op);
3984
3985   *op = build1 (INDIRECT_REF, TREE_TYPE (*op), with);
3986
3987   /* Record the original reference, for purposes of alias analysis.  */
3988   REF_ORIGINAL (*op) = orig;
3989 }
3990
3991 /* Rewrites USE (address that is an iv) using candidate CAND.  */
3992
3993 static void
3994 rewrite_use_address (struct ivopts_data *data,
3995                      struct iv_use *use, struct iv_cand *cand)
3996 {
3997   tree comp = unshare_expr (get_computation (data->current_loop,
3998                                              use, cand));
3999   block_stmt_iterator bsi = stmt_for_bsi (use->stmt);
4000   tree stmts;
4001   tree op = force_gimple_operand (comp, &stmts, true, NULL_TREE);
4002
4003   if (stmts)
4004     bsi_insert_before (&bsi, stmts, BSI_SAME_STMT);
4005
4006   rewrite_address_base (&bsi, use->op_p, op);
4007 }
4008
4009 /* Rewrites USE (the condition such that one of the arguments is an iv) using
4010    candidate CAND.  */
4011
4012 static void
4013 rewrite_use_compare (struct ivopts_data *data,
4014                      struct iv_use *use, struct iv_cand *cand)
4015 {
4016   tree comp;
4017   tree *op_p, cond, op, stmts, bound;
4018   block_stmt_iterator bsi = stmt_for_bsi (use->stmt);
4019   enum tree_code compare;
4020   
4021   if (may_eliminate_iv (data->current_loop,
4022                         use, cand, &compare, &bound))
4023     {
4024       op = force_gimple_operand (unshare_expr (bound), &stmts,
4025                                  true, NULL_TREE);
4026
4027       if (stmts)
4028         bsi_insert_before (&bsi, stmts, BSI_SAME_STMT);
4029
4030       *use->op_p = build2 (compare, boolean_type_node,
4031                           var_at_stmt (data->current_loop,
4032                                        cand, use->stmt), op);
4033       modify_stmt (use->stmt);
4034       return;
4035     }
4036
4037   /* The induction variable elimination failed; just express the original
4038      giv.  */
4039   comp = unshare_expr (get_computation (data->current_loop, use, cand));
4040
4041   cond = *use->op_p;
4042   op_p = &TREE_OPERAND (cond, 0);
4043   if (TREE_CODE (*op_p) != SSA_NAME
4044       || zero_p (get_iv (data, *op_p)->step))
4045     op_p = &TREE_OPERAND (cond, 1);
4046
4047   op = force_gimple_operand (comp, &stmts, true, SSA_NAME_VAR (*op_p));
4048   if (stmts)
4049     bsi_insert_before (&bsi, stmts, BSI_SAME_STMT);
4050
4051   *op_p = op;
4052 }
4053
4054 /* Ensure that operand *OP_P may be used at the end of EXIT without
4055    violating loop closed ssa form.  */
4056
4057 static void
4058 protect_loop_closed_ssa_form_use (edge exit, use_operand_p op_p)
4059 {
4060   basic_block def_bb;
4061   struct loop *def_loop;
4062   tree phi, use;
4063
4064   use = USE_FROM_PTR (op_p);
4065   if (TREE_CODE (use) != SSA_NAME)
4066     return;
4067
4068   def_bb = bb_for_stmt (SSA_NAME_DEF_STMT (use));
4069   if (!def_bb)
4070     return;
4071
4072   def_loop = def_bb->loop_father;
4073   if (flow_bb_inside_loop_p (def_loop, exit->dest))
4074     return;
4075
4076   /* Try finding a phi node that copies the value out of the loop.  */
4077   for (phi = phi_nodes (exit->dest); phi; phi = TREE_CHAIN (phi))
4078     if (PHI_ARG_DEF_FROM_EDGE (phi, exit) == use)
4079       break;
4080
4081   if (!phi)
4082     {
4083       /* Create such a phi node.  */
4084       tree new_name = duplicate_ssa_name (use, NULL);
4085
4086       phi = create_phi_node (new_name, exit->dest);
4087       SSA_NAME_DEF_STMT (new_name) = phi;
4088       add_phi_arg (&phi, use, exit);
4089     }
4090
4091   SET_USE (op_p, PHI_RESULT (phi));
4092 }
4093
4094 /* Ensure that operands of STMT may be used at the end of EXIT without
4095    violating loop closed ssa form.  */
4096
4097 static void
4098 protect_loop_closed_ssa_form (edge exit, tree stmt)
4099 {
4100   use_optype uses;
4101   vuse_optype vuses;
4102   v_may_def_optype v_may_defs;
4103   unsigned i;
4104
4105   get_stmt_operands (stmt);
4106
4107   uses = STMT_USE_OPS (stmt);
4108   for (i = 0; i < NUM_USES (uses); i++)
4109     protect_loop_closed_ssa_form_use (exit, USE_OP_PTR (uses, i));
4110
4111   vuses = STMT_VUSE_OPS (stmt);
4112   for (i = 0; i < NUM_VUSES (vuses); i++)
4113     protect_loop_closed_ssa_form_use (exit, VUSE_OP_PTR (vuses, i));
4114
4115   v_may_defs = STMT_V_MAY_DEF_OPS (stmt);
4116   for (i = 0; i < NUM_V_MAY_DEFS (v_may_defs); i++)
4117     protect_loop_closed_ssa_form_use (exit, V_MAY_DEF_OP_PTR (v_may_defs, i));
4118 }
4119
4120 /* STMTS compute a value of a phi argument OP on EXIT of a loop.  Arrange things
4121    so that they are emitted on the correct place, and so that the loop closed
4122    ssa form is preserved.  */
4123
4124 static void
4125 compute_phi_arg_on_exit (edge exit, tree stmts, tree op)
4126 {
4127   tree_stmt_iterator tsi;
4128   block_stmt_iterator bsi;
4129   tree phi, stmt, def, next;
4130
4131   if (exit->dest->pred->pred_next)
4132     split_loop_exit_edge (exit);
4133
4134   if (TREE_CODE (stmts) == STATEMENT_LIST)
4135     {
4136       for (tsi = tsi_start (stmts); !tsi_end_p (tsi); tsi_next (&tsi))
4137         protect_loop_closed_ssa_form (exit, tsi_stmt (tsi));
4138     }
4139   else
4140     protect_loop_closed_ssa_form (exit, stmts);
4141
4142   /* Ensure there is label in exit->dest, so that we can
4143      insert after it.  */
4144   tree_block_label (exit->dest);
4145   bsi = bsi_after_labels (exit->dest);
4146   bsi_insert_after (&bsi, stmts, BSI_CONTINUE_LINKING);
4147
4148   if (!op)
4149     return;
4150
4151   for (phi = phi_nodes (exit->dest); phi; phi = next)
4152     {
4153       next = TREE_CHAIN (phi);
4154
4155       if (PHI_ARG_DEF_FROM_EDGE (phi, exit) == op)
4156         {
4157           def = PHI_RESULT (phi);
4158           remove_statement (phi, false);
4159           stmt = build2 (MODIFY_EXPR, TREE_TYPE (op),
4160                         def, op);
4161           SSA_NAME_DEF_STMT (def) = stmt;
4162           bsi_insert_after (&bsi, stmt, BSI_CONTINUE_LINKING);
4163         }
4164     }
4165 }
4166
4167 /* Rewrites the final value of USE (that is only needed outside of the loop)
4168    using candidate CAND.  */
4169
4170 static void
4171 rewrite_use_outer (struct ivopts_data *data,
4172                    struct iv_use *use, struct iv_cand *cand)
4173 {
4174   edge exit;
4175   tree value, op, stmts, tgt;
4176   tree phi;
4177
4178   switch (TREE_CODE (use->stmt))
4179     {
4180     case PHI_NODE:
4181       tgt = PHI_RESULT (use->stmt);
4182       break;
4183     case MODIFY_EXPR:
4184       tgt = TREE_OPERAND (use->stmt, 0);
4185       break;
4186     default:
4187       gcc_unreachable ();
4188     }
4189
4190   exit = single_dom_exit (data->current_loop);
4191
4192   if (exit)
4193     {
4194       if (!cand->iv)
4195         {
4196           bool ok = may_replace_final_value (data->current_loop, use, &value);
4197           gcc_assert (ok);
4198         }
4199       else
4200         value = get_computation_at (data->current_loop,
4201                                     use, cand, last_stmt (exit->src));
4202
4203       value = unshare_expr (value);
4204       op = force_gimple_operand (value, &stmts, true, SSA_NAME_VAR (tgt));
4205           
4206       /* If we will preserve the iv anyway and we would need to perform
4207          some computation to replace the final value, do nothing.  */
4208       if (stmts && name_info (data, tgt)->preserve_biv)
4209         return;
4210
4211       for (phi = phi_nodes (exit->dest); phi; phi = TREE_CHAIN (phi))
4212         {
4213           use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, exit);
4214
4215           if (USE_FROM_PTR (use_p) == tgt)
4216             SET_USE (use_p, op);
4217         }
4218
4219       if (stmts)
4220         compute_phi_arg_on_exit (exit, stmts, op);
4221
4222       /* Enable removal of the statement.  We cannot remove it directly,
4223          since we may still need the aliasing information attached to the
4224          ssa name defined by it.  */
4225       name_info (data, tgt)->iv->have_use_for = false;
4226       return;
4227     }
4228
4229   /* If the variable is going to be preserved anyway, there is nothing to
4230      do.  */
4231   if (name_info (data, tgt)->preserve_biv)
4232     return;
4233
4234   /* Otherwise we just need to compute the iv.  */
4235   rewrite_use_nonlinear_expr (data, use, cand);
4236 }
4237
4238 /* Rewrites USE using candidate CAND.  */
4239
4240 static void
4241 rewrite_use (struct ivopts_data *data,
4242              struct iv_use *use, struct iv_cand *cand)
4243 {
4244   switch (use->type)
4245     {
4246       case USE_NONLINEAR_EXPR:
4247         rewrite_use_nonlinear_expr (data, use, cand);
4248         break;
4249
4250       case USE_OUTER:
4251         rewrite_use_outer (data, use, cand);
4252         break;
4253
4254       case USE_ADDRESS:
4255         rewrite_use_address (data, use, cand);
4256         break;
4257
4258       case USE_COMPARE:
4259         rewrite_use_compare (data, use, cand);
4260         break;
4261
4262       default:
4263         gcc_unreachable ();
4264     }
4265   modify_stmt (use->stmt);
4266 }
4267
4268 /* Rewrite the uses using the selected induction variables.  */
4269
4270 static void
4271 rewrite_uses (struct ivopts_data *data)
4272 {
4273   unsigned i;
4274   struct iv_cand *cand;
4275   struct iv_use *use;
4276
4277   for (i = 0; i < n_iv_uses (data); i++)
4278     {
4279       use = iv_use (data, i);
4280       cand = use->selected;
4281       gcc_assert (cand);
4282
4283       rewrite_use (data, use, cand);
4284     }
4285 }
4286
4287 /* Removes the ivs that are not used after rewriting.  */
4288
4289 static void
4290 remove_unused_ivs (struct ivopts_data *data)
4291 {
4292   unsigned j;
4293   bitmap_iterator bi;
4294
4295   EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, j, bi)
4296     {
4297       struct version_info *info;
4298
4299       info = ver_info (data, j);
4300       if (info->iv
4301           && !zero_p (info->iv->step)
4302           && !info->inv_id
4303           && !info->iv->have_use_for
4304           && !info->preserve_biv)
4305         remove_statement (SSA_NAME_DEF_STMT (info->iv->ssa_name), true);
4306     }
4307 }
4308
4309 /* Frees data allocated by the optimization of a single loop.  */
4310
4311 static void
4312 free_loop_data (struct ivopts_data *data)
4313 {
4314   unsigned i, j;
4315   bitmap_iterator bi;
4316
4317   EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, i, bi)
4318     {
4319       struct version_info *info;
4320
4321       info = ver_info (data, i);
4322       if (info->iv)
4323         free (info->iv);
4324       info->iv = NULL;
4325       info->has_nonlin_use = false;
4326       info->preserve_biv = false;
4327       info->inv_id = 0;
4328     }
4329   bitmap_clear (data->relevant);
4330
4331   for (i = 0; i < n_iv_uses (data); i++)
4332     {
4333       struct iv_use *use = iv_use (data, i);
4334
4335       free (use->iv);
4336       BITMAP_XFREE (use->related_cands);
4337       for (j = 0; j < use->n_map_members; j++)
4338         if (use->cost_map[j].depends_on)
4339           BITMAP_XFREE (use->cost_map[j].depends_on);
4340       free (use->cost_map);
4341       free (use);
4342     }
4343   VARRAY_POP_ALL (data->iv_uses);
4344
4345   for (i = 0; i < n_iv_cands (data); i++)
4346     {
4347       struct iv_cand *cand = iv_cand (data, i);
4348
4349       if (cand->iv)
4350         free (cand->iv);
4351       free (cand);
4352     }
4353   VARRAY_POP_ALL (data->iv_candidates);
4354
4355   if (data->version_info_size < num_ssa_names)
4356     {
4357       data->version_info_size = 2 * num_ssa_names;
4358       free (data->version_info);
4359       data->version_info = xcalloc (data->version_info_size,
4360                                     sizeof (struct version_info));
4361     }
4362
4363   data->max_inv_id = 0;
4364
4365   for (i = 0; i < VARRAY_ACTIVE_SIZE (decl_rtl_to_reset); i++)
4366     {
4367       tree obj = VARRAY_GENERIC_PTR_NOGC (decl_rtl_to_reset, i);
4368
4369       SET_DECL_RTL (obj, NULL_RTX);
4370     }
4371   VARRAY_POP_ALL (decl_rtl_to_reset);
4372 }
4373
4374 /* Finalizes data structures used by the iv optimization pass.  LOOPS is the
4375    loop tree.  */
4376
4377 static void
4378 tree_ssa_iv_optimize_finalize (struct loops *loops, struct ivopts_data *data)
4379 {
4380   unsigned i;
4381
4382   for (i = 1; i < loops->num; i++)
4383     if (loops->parray[i])
4384       {
4385         free (loops->parray[i]->aux);
4386         loops->parray[i]->aux = NULL;
4387       }
4388
4389   free_loop_data (data);
4390   free (data->version_info);
4391   BITMAP_XFREE (data->relevant);
4392
4393   VARRAY_FREE (decl_rtl_to_reset);
4394   VARRAY_FREE (data->iv_uses);
4395   VARRAY_FREE (data->iv_candidates);
4396 }
4397
4398 /* Optimizes the LOOP.  Returns true if anything changed.  */
4399
4400 static bool
4401 tree_ssa_iv_optimize_loop (struct ivopts_data *data, struct loop *loop)
4402 {
4403   bool changed = false;
4404   bitmap iv_set;
4405   edge exit;
4406
4407   data->current_loop = loop;
4408
4409   if (dump_file && (dump_flags & TDF_DETAILS))
4410     {
4411       fprintf (dump_file, "Processing loop %d\n", loop->num);
4412       
4413       exit = single_dom_exit (loop);
4414       if (exit)
4415         {
4416           fprintf (dump_file, "  single exit %d -> %d, exit condition ",
4417                    exit->src->index, exit->dest->index);
4418           print_generic_expr (dump_file, last_stmt (exit->src), TDF_SLIM);
4419           fprintf (dump_file, "\n");
4420         }
4421
4422       fprintf (dump_file, "\n");
4423     }
4424
4425   /* For each ssa name determines whether it behaves as an induction variable
4426      in some loop.  */
4427   if (!find_induction_variables (data))
4428     goto finish;
4429
4430   /* Finds interesting uses (item 1).  */
4431   find_interesting_uses (data);
4432   if (n_iv_uses (data) > MAX_CONSIDERED_USES)
4433     goto finish;
4434
4435   /* Finds candidates for the induction variables (item 2).  */
4436   find_iv_candidates (data);
4437
4438   /* Calculates the costs (item 3, part 1).  */
4439   determine_use_iv_costs (data);
4440   determine_iv_costs (data);
4441   determine_set_costs (data);
4442
4443   /* Find the optimal set of induction variables (item 3, part 2).  */
4444   iv_set = find_optimal_iv_set (data);
4445   if (!iv_set)
4446     goto finish;
4447   changed = true;
4448
4449   /* Create the new induction variables (item 4, part 1).  */
4450   create_new_ivs (data, iv_set);
4451   
4452   /* Rewrite the uses (item 4, part 2).  */
4453   rewrite_uses (data);
4454
4455   /* Remove the ivs that are unused after rewriting.  */
4456   remove_unused_ivs (data);
4457
4458   loop_commit_inserts ();
4459
4460   BITMAP_XFREE (iv_set);
4461
4462   /* We have changed the structure of induction variables; it might happen
4463      that definitions in the scev database refer to some of them that were
4464      eliminated.  */
4465   scev_reset ();
4466
4467 finish:
4468   free_loop_data (data);
4469
4470   return changed;
4471 }
4472
4473 /* Main entry point.  Optimizes induction variables in LOOPS.  */
4474
4475 void
4476 tree_ssa_iv_optimize (struct loops *loops)
4477 {
4478   struct loop *loop;
4479   struct ivopts_data data;
4480
4481   tree_ssa_iv_optimize_init (loops, &data);
4482
4483   /* Optimize the loops starting with the innermost ones.  */
4484   loop = loops->tree_root;
4485   while (loop->inner)
4486     loop = loop->inner;
4487
4488 #ifdef ENABLE_CHECKING
4489   verify_loop_closed_ssa ();
4490   verify_stmts ();
4491 #endif
4492
4493   /* Scan the loops, inner ones first.  */
4494   while (loop != loops->tree_root)
4495     {
4496       if (dump_file && (dump_flags & TDF_DETAILS))
4497         flow_loop_dump (loop, dump_file, NULL, 1);
4498       if (tree_ssa_iv_optimize_loop (&data, loop))
4499         {
4500 #ifdef ENABLE_CHECKING
4501           verify_loop_closed_ssa ();
4502           verify_stmts ();
4503 #endif
4504         }
4505
4506       if (loop->next)
4507         {
4508           loop = loop->next;
4509           while (loop->inner)
4510             loop = loop->inner;
4511         }
4512       else
4513         loop = loop->outer;
4514     }
4515
4516   tree_ssa_iv_optimize_finalize (loops, &data);
4517 }