OSDN Git Service

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