OSDN Git Service

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