OSDN Git Service

2008-08-04 Richard Guenther <rguenther@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-loop-ivopts.c
1 /* Induction variable optimizations.
2    Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Free Software
3    Foundation, Inc.
4    
5 This file is part of GCC.
6    
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
10 later version.
11    
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16    
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
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 "pointer-set.h"
87 #include "hashtab.h"
88 #include "tree-chrec.h"
89 #include "tree-scalar-evolution.h"
90 #include "cfgloop.h"
91 #include "params.h"
92 #include "langhooks.h"
93 #include "tree-affine.h"
94 #include "target.h"
95
96 /* The infinite cost.  */
97 #define INFTY 10000000
98
99 /* The expected number of loop iterations.  TODO -- use profiling instead of
100    this.  */
101 #define AVG_LOOP_NITER(LOOP) 5
102
103
104 /* Representation of the induction variable.  */
105 struct iv
106 {
107   tree base;            /* Initial value of the iv.  */
108   tree base_object;     /* A memory object to that the induction variable points.  */
109   tree step;            /* Step of the iv (constant only).  */
110   tree ssa_name;        /* The ssa name with the value.  */
111   bool biv_p;           /* Is it a biv?  */
112   bool have_use_for;    /* Do we already have a use for it?  */
113   unsigned use_id;      /* The identifier in the use if it is the case.  */
114 };
115
116 /* Per-ssa version information (induction variable descriptions, etc.).  */
117 struct version_info
118 {
119   tree name;            /* The ssa name.  */
120   struct iv *iv;        /* Induction variable description.  */
121   bool has_nonlin_use;  /* For a loop-level invariant, whether it is used in
122                            an expression that is not an induction variable.  */
123   unsigned inv_id;      /* Id of an invariant.  */
124   bool preserve_biv;    /* For the original biv, whether to preserve it.  */
125 };
126
127 /* Types of uses.  */
128 enum use_type
129 {
130   USE_NONLINEAR_EXPR,   /* Use in a nonlinear expression.  */
131   USE_ADDRESS,          /* Use in an address.  */
132   USE_COMPARE           /* Use is a compare.  */
133 };
134
135 /* Cost of a computation.  */
136 typedef struct
137 {
138   unsigned cost;        /* The runtime cost.  */
139   unsigned complexity;  /* The estimate of the complexity of the code for
140                            the computation (in no concrete units --
141                            complexity field should be larger for more
142                            complex expressions and addressing modes).  */
143 } comp_cost;
144
145 static const comp_cost zero_cost = {0, 0};
146 static const comp_cost infinite_cost = {INFTY, INFTY};
147
148 /* The candidate - cost pair.  */
149 struct cost_pair
150 {
151   struct iv_cand *cand; /* The candidate.  */
152   comp_cost cost;       /* The cost.  */
153   bitmap depends_on;    /* The list of invariants that have to be
154                            preserved.  */
155   tree value;           /* For final value elimination, the expression for
156                            the final value of the iv.  For iv elimination,
157                            the new bound to compare with.  */
158 };
159
160 /* Use.  */
161 struct iv_use
162 {
163   unsigned id;          /* The id of the use.  */
164   enum use_type type;   /* Type of the use.  */
165   struct iv *iv;        /* The induction variable it is based on.  */
166   gimple stmt;          /* Statement in that it occurs.  */
167   tree *op_p;           /* The place where it occurs.  */
168   bitmap related_cands; /* The set of "related" iv candidates, plus the common
169                            important ones.  */
170
171   unsigned n_map_members; /* Number of candidates in the cost_map list.  */
172   struct cost_pair *cost_map;
173                         /* The costs wrto the iv candidates.  */
174
175   struct iv_cand *selected;
176                         /* The selected candidate.  */
177 };
178
179 /* The position where the iv is computed.  */
180 enum iv_position
181 {
182   IP_NORMAL,            /* At the end, just before the exit condition.  */
183   IP_END,               /* At the end of the latch block.  */
184   IP_ORIGINAL           /* The original biv.  */
185 };
186
187 /* The induction variable candidate.  */
188 struct iv_cand
189 {
190   unsigned id;          /* The number of the candidate.  */
191   bool important;       /* Whether this is an "important" candidate, i.e. such
192                            that it should be considered by all uses.  */
193   enum iv_position pos; /* Where it is computed.  */
194   gimple incremented_at;/* For original biv, the statement where it is
195                            incremented.  */
196   tree var_before;      /* The variable used for it before increment.  */
197   tree var_after;       /* The variable used for it after increment.  */
198   struct iv *iv;        /* The value of the candidate.  NULL for
199                            "pseudocandidate" used to indicate the possibility
200                            to replace the final value of an iv by direct
201                            computation of the value.  */
202   unsigned cost;        /* Cost of the candidate.  */
203   bitmap depends_on;    /* The list of invariants that are used in step of the
204                            biv.  */
205 };
206
207 /* The data used by the induction variable optimizations.  */
208
209 typedef struct iv_use *iv_use_p;
210 DEF_VEC_P(iv_use_p);
211 DEF_VEC_ALLOC_P(iv_use_p,heap);
212
213 typedef struct iv_cand *iv_cand_p;
214 DEF_VEC_P(iv_cand_p);
215 DEF_VEC_ALLOC_P(iv_cand_p,heap);
216
217 struct ivopts_data
218 {
219   /* The currently optimized loop.  */
220   struct loop *current_loop;
221
222   /* Number of registers used in it.  */
223   unsigned regs_used;
224
225   /* Numbers of iterations for all exits of the current loop.  */
226   struct pointer_map_t *niters;
227
228   /* The size of version_info array allocated.  */
229   unsigned version_info_size;
230
231   /* The array of information for the ssa names.  */
232   struct version_info *version_info;
233
234   /* The bitmap of indices in version_info whose value was changed.  */
235   bitmap relevant;
236
237   /* The maximum invariant id.  */
238   unsigned max_inv_id;
239
240   /* The uses of induction variables.  */
241   VEC(iv_use_p,heap) *iv_uses;
242
243   /* The candidates.  */
244   VEC(iv_cand_p,heap) *iv_candidates;
245
246   /* A bitmap of important candidates.  */
247   bitmap important_candidates;
248
249   /* Whether to consider just related and important candidates when replacing a
250      use.  */
251   bool consider_all_candidates;
252 };
253
254 /* An assignment of iv candidates to uses.  */
255
256 struct iv_ca
257 {
258   /* The number of uses covered by the assignment.  */
259   unsigned upto;
260
261   /* Number of uses that cannot be expressed by the candidates in the set.  */
262   unsigned bad_uses;
263
264   /* Candidate assigned to a use, together with the related costs.  */
265   struct cost_pair **cand_for_use;
266
267   /* Number of times each candidate is used.  */
268   unsigned *n_cand_uses;
269
270   /* The candidates used.  */
271   bitmap cands;
272
273   /* The number of candidates in the set.  */
274   unsigned n_cands;
275
276   /* Total number of registers needed.  */
277   unsigned n_regs;
278
279   /* Total cost of expressing uses.  */
280   comp_cost cand_use_cost;
281
282   /* Total cost of candidates.  */
283   unsigned cand_cost;
284
285   /* Number of times each invariant is used.  */
286   unsigned *n_invariant_uses;
287
288   /* Total cost of the assignment.  */
289   comp_cost cost;
290 };
291
292 /* Difference of two iv candidate assignments.  */
293
294 struct iv_ca_delta
295 {
296   /* Changed use.  */
297   struct iv_use *use;
298
299   /* An old assignment (for rollback purposes).  */
300   struct cost_pair *old_cp;
301
302   /* A new assignment.  */
303   struct cost_pair *new_cp;
304
305   /* Next change in the list.  */
306   struct iv_ca_delta *next_change;
307 };
308
309 /* Bound on number of candidates below that all candidates are considered.  */
310
311 #define CONSIDER_ALL_CANDIDATES_BOUND \
312   ((unsigned) PARAM_VALUE (PARAM_IV_CONSIDER_ALL_CANDIDATES_BOUND))
313
314 /* If there are more iv occurrences, we just give up (it is quite unlikely that
315    optimizing such a loop would help, and it would take ages).  */
316
317 #define MAX_CONSIDERED_USES \
318   ((unsigned) PARAM_VALUE (PARAM_IV_MAX_CONSIDERED_USES))
319
320 /* If there are at most this number of ivs in the set, try removing unnecessary
321    ivs from the set always.  */
322
323 #define ALWAYS_PRUNE_CAND_SET_BOUND \
324   ((unsigned) PARAM_VALUE (PARAM_IV_ALWAYS_PRUNE_CAND_SET_BOUND))
325
326 /* The list of trees for that the decl_rtl field must be reset is stored
327    here.  */
328
329 static VEC(tree,heap) *decl_rtl_to_reset;
330
331 /* Number of uses recorded in DATA.  */
332
333 static inline unsigned
334 n_iv_uses (struct ivopts_data *data)
335 {
336   return VEC_length (iv_use_p, data->iv_uses);
337 }
338
339 /* Ith use recorded in DATA.  */
340
341 static inline struct iv_use *
342 iv_use (struct ivopts_data *data, unsigned i)
343 {
344   return VEC_index (iv_use_p, data->iv_uses, i);
345 }
346
347 /* Number of candidates recorded in DATA.  */
348
349 static inline unsigned
350 n_iv_cands (struct ivopts_data *data)
351 {
352   return VEC_length (iv_cand_p, data->iv_candidates);
353 }
354
355 /* Ith candidate recorded in DATA.  */
356
357 static inline struct iv_cand *
358 iv_cand (struct ivopts_data *data, unsigned i)
359 {
360   return VEC_index (iv_cand_p, data->iv_candidates, i);
361 }
362
363 /* The single loop exit if it dominates the latch, NULL otherwise.  */
364
365 edge
366 single_dom_exit (struct loop *loop)
367 {
368   edge exit = single_exit (loop);
369
370   if (!exit)
371     return NULL;
372
373   if (!just_once_each_iteration_p (loop, exit->src))
374     return NULL;
375
376   return exit;
377 }
378
379 /* Dumps information about the induction variable IV to FILE.  */
380
381 extern void dump_iv (FILE *, struct iv *);
382 void
383 dump_iv (FILE *file, struct iv *iv)
384 {
385   if (iv->ssa_name)
386     {
387       fprintf (file, "ssa name ");
388       print_generic_expr (file, iv->ssa_name, TDF_SLIM);
389       fprintf (file, "\n");
390     }
391
392   fprintf (file, "  type ");
393   print_generic_expr (file, TREE_TYPE (iv->base), TDF_SLIM);
394   fprintf (file, "\n");
395
396   if (iv->step)
397     {
398       fprintf (file, "  base ");
399       print_generic_expr (file, iv->base, TDF_SLIM);
400       fprintf (file, "\n");
401
402       fprintf (file, "  step ");
403       print_generic_expr (file, iv->step, TDF_SLIM);
404       fprintf (file, "\n");
405     }
406   else
407     {
408       fprintf (file, "  invariant ");
409       print_generic_expr (file, iv->base, TDF_SLIM);
410       fprintf (file, "\n");
411     }
412
413   if (iv->base_object)
414     {
415       fprintf (file, "  base object ");
416       print_generic_expr (file, iv->base_object, TDF_SLIM);
417       fprintf (file, "\n");
418     }
419
420   if (iv->biv_p)
421     fprintf (file, "  is a biv\n");
422 }
423
424 /* Dumps information about the USE to FILE.  */
425
426 extern void dump_use (FILE *, struct iv_use *);
427 void
428 dump_use (FILE *file, struct iv_use *use)
429 {
430   fprintf (file, "use %d\n", use->id);
431
432   switch (use->type)
433     {
434     case USE_NONLINEAR_EXPR:
435       fprintf (file, "  generic\n");
436       break;
437
438     case USE_ADDRESS:
439       fprintf (file, "  address\n");
440       break;
441
442     case USE_COMPARE:
443       fprintf (file, "  compare\n");
444       break;
445
446     default:
447       gcc_unreachable ();
448     }
449
450   fprintf (file, "  in statement ");
451   print_gimple_stmt (file, use->stmt, 0, 0);
452   fprintf (file, "\n");
453
454   fprintf (file, "  at position ");
455   if (use->op_p)
456     print_generic_expr (file, *use->op_p, TDF_SLIM);
457   fprintf (file, "\n");
458
459   dump_iv (file, use->iv);
460
461   if (use->related_cands)
462     {
463       fprintf (file, "  related candidates ");
464       dump_bitmap (file, use->related_cands);
465     }
466 }
467
468 /* Dumps information about the uses to FILE.  */
469
470 extern void dump_uses (FILE *, struct ivopts_data *);
471 void
472 dump_uses (FILE *file, struct ivopts_data *data)
473 {
474   unsigned i;
475   struct iv_use *use;
476
477   for (i = 0; i < n_iv_uses (data); i++)
478     {
479       use = iv_use (data, i);
480
481       dump_use (file, use);
482       fprintf (file, "\n");
483     }
484 }
485
486 /* Dumps information about induction variable candidate CAND to FILE.  */
487
488 extern void dump_cand (FILE *, struct iv_cand *);
489 void
490 dump_cand (FILE *file, struct iv_cand *cand)
491 {
492   struct iv *iv = cand->iv;
493
494   fprintf (file, "candidate %d%s\n",
495            cand->id, cand->important ? " (important)" : "");
496
497   if (cand->depends_on)
498     {
499       fprintf (file, "  depends on ");
500       dump_bitmap (file, cand->depends_on);
501     }
502
503   if (!iv)
504     {
505       fprintf (file, "  final value replacement\n");
506       return;
507     }
508
509   switch (cand->pos)
510     {
511     case IP_NORMAL:
512       fprintf (file, "  incremented before exit test\n");
513       break;
514
515     case IP_END:
516       fprintf (file, "  incremented at end\n");
517       break;
518
519     case IP_ORIGINAL:
520       fprintf (file, "  original biv\n");
521       break;
522     }
523
524   dump_iv (file, iv);
525 }
526
527 /* Returns the info for ssa version VER.  */
528
529 static inline struct version_info *
530 ver_info (struct ivopts_data *data, unsigned ver)
531 {
532   return data->version_info + ver;
533 }
534
535 /* Returns the info for ssa name NAME.  */
536
537 static inline struct version_info *
538 name_info (struct ivopts_data *data, tree name)
539 {
540   return ver_info (data, SSA_NAME_VERSION (name));
541 }
542
543 /* Returns true if STMT is after the place where the IP_NORMAL ivs will be
544    emitted in LOOP.  */
545
546 static bool
547 stmt_after_ip_normal_pos (struct loop *loop, gimple stmt)
548 {
549   basic_block bb = ip_normal_pos (loop), sbb = gimple_bb (stmt);
550
551   gcc_assert (bb);
552
553   if (sbb == loop->latch)
554     return true;
555
556   if (sbb != bb)
557     return false;
558
559   return stmt == last_stmt (bb);
560 }
561
562 /* Returns true if STMT if after the place where the original induction
563    variable CAND is incremented.  */
564
565 static bool
566 stmt_after_ip_original_pos (struct iv_cand *cand, gimple stmt)
567 {
568   basic_block cand_bb = gimple_bb (cand->incremented_at);
569   basic_block stmt_bb = gimple_bb (stmt);
570   gimple_stmt_iterator bsi;
571
572   if (!dominated_by_p (CDI_DOMINATORS, stmt_bb, cand_bb))
573     return false;
574
575   if (stmt_bb != cand_bb)
576     return true;
577
578   /* Scan the block from the end, since the original ivs are usually
579      incremented at the end of the loop body.  */
580   for (bsi = gsi_last_bb (stmt_bb); ; gsi_prev (&bsi))
581     {
582       if (gsi_stmt (bsi) == cand->incremented_at)
583         return false;
584       if (gsi_stmt (bsi) == stmt)
585         return true;
586     }
587 }
588
589 /* Returns true if STMT if after the place where the induction variable
590    CAND is incremented in LOOP.  */
591
592 static bool
593 stmt_after_increment (struct loop *loop, struct iv_cand *cand, gimple stmt)
594 {
595   switch (cand->pos)
596     {
597     case IP_END:
598       return false;
599
600     case IP_NORMAL:
601       return stmt_after_ip_normal_pos (loop, stmt);
602
603     case IP_ORIGINAL:
604       return stmt_after_ip_original_pos (cand, stmt);
605
606     default:
607       gcc_unreachable ();
608     }
609 }
610
611 /* Returns true if EXP is a ssa name that occurs in an abnormal phi node.  */
612
613 static bool
614 abnormal_ssa_name_p (tree exp)
615 {
616   if (!exp)
617     return false;
618
619   if (TREE_CODE (exp) != SSA_NAME)
620     return false;
621
622   return SSA_NAME_OCCURS_IN_ABNORMAL_PHI (exp) != 0;
623 }
624
625 /* Returns false if BASE or INDEX contains a ssa name that occurs in an
626    abnormal phi node.  Callback for for_each_index.  */
627
628 static bool
629 idx_contains_abnormal_ssa_name_p (tree base, tree *index,
630                                   void *data ATTRIBUTE_UNUSED)
631 {
632   if (TREE_CODE (base) == ARRAY_REF)
633     {
634       if (abnormal_ssa_name_p (TREE_OPERAND (base, 2)))
635         return false;
636       if (abnormal_ssa_name_p (TREE_OPERAND (base, 3)))
637         return false;
638     }
639
640   return !abnormal_ssa_name_p (*index);
641 }
642
643 /* Returns true if EXPR contains a ssa name that occurs in an
644    abnormal phi node.  */
645
646 bool
647 contains_abnormal_ssa_name_p (tree expr)
648 {
649   enum tree_code code;
650   enum tree_code_class codeclass;
651
652   if (!expr)
653     return false;
654
655   code = TREE_CODE (expr);
656   codeclass = TREE_CODE_CLASS (code);
657
658   if (code == SSA_NAME)
659     return SSA_NAME_OCCURS_IN_ABNORMAL_PHI (expr) != 0;
660
661   if (code == INTEGER_CST
662       || is_gimple_min_invariant (expr))
663     return false;
664
665   if (code == ADDR_EXPR)
666     return !for_each_index (&TREE_OPERAND (expr, 0),
667                             idx_contains_abnormal_ssa_name_p,
668                             NULL);
669
670   switch (codeclass)
671     {
672     case tcc_binary:
673     case tcc_comparison:
674       if (contains_abnormal_ssa_name_p (TREE_OPERAND (expr, 1)))
675         return true;
676
677       /* Fallthru.  */
678     case tcc_unary:
679       if (contains_abnormal_ssa_name_p (TREE_OPERAND (expr, 0)))
680         return true;
681
682       break;
683
684     default:
685       gcc_unreachable ();
686     }
687
688   return false;
689 }
690
691 /*  Returns tree describing number of iterations determined from
692     EXIT of DATA->current_loop, or NULL if something goes wrong.  */
693
694 static tree
695 niter_for_exit (struct ivopts_data *data, edge exit)
696 {
697   struct tree_niter_desc desc;
698   tree niter;
699   void **slot;
700
701   if (!data->niters)
702     {
703       data->niters = pointer_map_create ();
704       slot = NULL;
705     }
706   else
707     slot = pointer_map_contains (data->niters, exit);
708
709   if (!slot)
710     {
711       /* Try to determine number of iterations.  We must know it
712          unconditionally (i.e., without possibility of # of iterations
713          being zero).  Also, we cannot safely work with ssa names that
714          appear in phi nodes on abnormal edges, so that we do not create
715          overlapping life ranges for them (PR 27283).  */
716       if (number_of_iterations_exit (data->current_loop,
717                                      exit, &desc, true)
718           && integer_zerop (desc.may_be_zero)
719           && !contains_abnormal_ssa_name_p (desc.niter))
720         niter = desc.niter;
721       else
722         niter = NULL_TREE;
723
724       *pointer_map_insert (data->niters, exit) = niter;
725     }
726   else
727     niter = (tree) *slot;
728
729   return niter;
730 }
731
732 /* Returns tree describing number of iterations determined from
733    single dominating exit of DATA->current_loop, or NULL if something
734    goes wrong.  */
735
736 static tree
737 niter_for_single_dom_exit (struct ivopts_data *data)
738 {
739   edge exit = single_dom_exit (data->current_loop);
740
741   if (!exit)
742     return NULL;
743
744   return niter_for_exit (data, exit);
745 }
746
747 /* Initializes data structures used by the iv optimization pass, stored
748    in DATA.  */
749
750 static void
751 tree_ssa_iv_optimize_init (struct ivopts_data *data)
752 {
753   data->version_info_size = 2 * num_ssa_names;
754   data->version_info = XCNEWVEC (struct version_info, data->version_info_size);
755   data->relevant = BITMAP_ALLOC (NULL);
756   data->important_candidates = BITMAP_ALLOC (NULL);
757   data->max_inv_id = 0;
758   data->niters = NULL;
759   data->iv_uses = VEC_alloc (iv_use_p, heap, 20);
760   data->iv_candidates = VEC_alloc (iv_cand_p, heap, 20);
761   decl_rtl_to_reset = VEC_alloc (tree, heap, 20);
762 }
763
764 /* Returns a memory object to that EXPR points.  In case we are able to
765    determine that it does not point to any such object, NULL is returned.  */
766
767 static tree
768 determine_base_object (tree expr)
769 {
770   enum tree_code code = TREE_CODE (expr);
771   tree base, obj;
772
773   /* If this is a pointer casted to any type, we need to determine
774      the base object for the pointer; so handle conversions before
775      throwing away non-pointer expressions.  */
776   if (CONVERT_EXPR_P (expr))
777     return determine_base_object (TREE_OPERAND (expr, 0));
778
779   if (!POINTER_TYPE_P (TREE_TYPE (expr)))
780     return NULL_TREE;
781
782   switch (code)
783     {
784     case INTEGER_CST:
785       return NULL_TREE;
786
787     case ADDR_EXPR:
788       obj = TREE_OPERAND (expr, 0);
789       base = get_base_address (obj);
790
791       if (!base)
792         return expr;
793
794       if (TREE_CODE (base) == INDIRECT_REF)
795         return determine_base_object (TREE_OPERAND (base, 0));
796
797       return fold_convert (ptr_type_node,
798                            build_fold_addr_expr (base));
799
800     case POINTER_PLUS_EXPR:
801       return determine_base_object (TREE_OPERAND (expr, 0));
802
803     case PLUS_EXPR:
804     case MINUS_EXPR:
805       /* Pointer addition is done solely using POINTER_PLUS_EXPR.  */
806       gcc_unreachable ();
807
808     default:
809       return fold_convert (ptr_type_node, expr);
810     }
811 }
812
813 /* Allocates an induction variable with given initial value BASE and step STEP
814    for loop LOOP.  */
815
816 static struct iv *
817 alloc_iv (tree base, tree step)
818 {
819   struct iv *iv = XCNEW (struct iv);
820   gcc_assert (step != NULL_TREE);
821
822   iv->base = base;
823   iv->base_object = determine_base_object (base);
824   iv->step = step;
825   iv->biv_p = false;
826   iv->have_use_for = false;
827   iv->use_id = 0;
828   iv->ssa_name = NULL_TREE;
829
830   return iv;
831 }
832
833 /* Sets STEP and BASE for induction variable IV.  */
834
835 static void
836 set_iv (struct ivopts_data *data, tree iv, tree base, tree step)
837 {
838   struct version_info *info = name_info (data, iv);
839
840   gcc_assert (!info->iv);
841
842   bitmap_set_bit (data->relevant, SSA_NAME_VERSION (iv));
843   info->iv = alloc_iv (base, step);
844   info->iv->ssa_name = iv;
845 }
846
847 /* Finds induction variable declaration for VAR.  */
848
849 static struct iv *
850 get_iv (struct ivopts_data *data, tree var)
851 {
852   basic_block bb;
853   tree type = TREE_TYPE (var);
854
855   if (!POINTER_TYPE_P (type)
856       && !INTEGRAL_TYPE_P (type))
857     return NULL;
858
859   if (!name_info (data, var)->iv)
860     {
861       bb = gimple_bb (SSA_NAME_DEF_STMT (var));
862
863       if (!bb
864           || !flow_bb_inside_loop_p (data->current_loop, bb))
865         set_iv (data, var, var, build_int_cst (type, 0));
866     }
867
868   return name_info (data, var)->iv;
869 }
870
871 /* Determines the step of a biv defined in PHI.  Returns NULL if PHI does
872    not define a simple affine biv with nonzero step.  */
873
874 static tree
875 determine_biv_step (gimple phi)
876 {
877   struct loop *loop = gimple_bb (phi)->loop_father;
878   tree name = PHI_RESULT (phi);
879   affine_iv iv;
880
881   if (!is_gimple_reg (name))
882     return NULL_TREE;
883
884   if (!simple_iv (loop, phi, name, &iv, true))
885     return NULL_TREE;
886
887   return integer_zerop (iv.step) ? NULL_TREE : iv.step;
888 }
889
890 /* Finds basic ivs.  */
891
892 static bool
893 find_bivs (struct ivopts_data *data)
894 {
895   gimple phi;
896   tree step, type, base;
897   bool found = false;
898   struct loop *loop = data->current_loop;
899   gimple_stmt_iterator psi;
900
901   for (psi = gsi_start_phis (loop->header); !gsi_end_p (psi); gsi_next (&psi))
902     {
903       phi = gsi_stmt (psi);
904
905       if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
906         continue;
907
908       step = determine_biv_step (phi);
909       if (!step)
910         continue;
911
912       base = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop));
913       base = expand_simple_operations (base);
914       if (contains_abnormal_ssa_name_p (base)
915           || contains_abnormal_ssa_name_p (step))
916         continue;
917
918       type = TREE_TYPE (PHI_RESULT (phi));
919       base = fold_convert (type, base);
920       if (step)
921         {
922           if (POINTER_TYPE_P (type))
923             step = fold_convert (sizetype, step);
924           else
925             step = fold_convert (type, step);
926         }
927
928       set_iv (data, PHI_RESULT (phi), base, step);
929       found = true;
930     }
931
932   return found;
933 }
934
935 /* Marks basic ivs.  */
936
937 static void
938 mark_bivs (struct ivopts_data *data)
939 {
940   gimple phi;
941   tree var;
942   struct iv *iv, *incr_iv;
943   struct loop *loop = data->current_loop;
944   basic_block incr_bb;
945   gimple_stmt_iterator psi;
946
947   for (psi = gsi_start_phis (loop->header); !gsi_end_p (psi); gsi_next (&psi))
948     {
949       phi = gsi_stmt (psi);
950
951       iv = get_iv (data, PHI_RESULT (phi));
952       if (!iv)
953         continue;
954
955       var = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (loop));
956       incr_iv = get_iv (data, var);
957       if (!incr_iv)
958         continue;
959
960       /* If the increment is in the subloop, ignore it.  */
961       incr_bb = gimple_bb (SSA_NAME_DEF_STMT (var));
962       if (incr_bb->loop_father != data->current_loop
963           || (incr_bb->flags & BB_IRREDUCIBLE_LOOP))
964         continue;
965
966       iv->biv_p = true;
967       incr_iv->biv_p = true;
968     }
969 }
970
971 /* Checks whether STMT defines a linear induction variable and stores its
972    parameters to IV.  */
973
974 static bool
975 find_givs_in_stmt_scev (struct ivopts_data *data, gimple stmt, affine_iv *iv)
976 {
977   tree lhs;
978   struct loop *loop = data->current_loop;
979
980   iv->base = NULL_TREE;
981   iv->step = NULL_TREE;
982
983   if (gimple_code (stmt) != GIMPLE_ASSIGN)
984     return false;
985
986   lhs = gimple_assign_lhs (stmt);
987   if (TREE_CODE (lhs) != SSA_NAME)
988     return false;
989
990   if (!simple_iv (loop, stmt, lhs, iv, true))
991     return false;
992   iv->base = expand_simple_operations (iv->base);
993
994   if (contains_abnormal_ssa_name_p (iv->base)
995       || contains_abnormal_ssa_name_p (iv->step))
996     return false;
997
998   return true;
999 }
1000
1001 /* Finds general ivs in statement STMT.  */
1002
1003 static void
1004 find_givs_in_stmt (struct ivopts_data *data, gimple stmt)
1005 {
1006   affine_iv iv;
1007
1008   if (!find_givs_in_stmt_scev (data, stmt, &iv))
1009     return;
1010
1011   set_iv (data, gimple_assign_lhs (stmt), iv.base, iv.step);
1012 }
1013
1014 /* Finds general ivs in basic block BB.  */
1015
1016 static void
1017 find_givs_in_bb (struct ivopts_data *data, basic_block bb)
1018 {
1019   gimple_stmt_iterator bsi;
1020
1021   for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1022     find_givs_in_stmt (data, gsi_stmt (bsi));
1023 }
1024
1025 /* Finds general ivs.  */
1026
1027 static void
1028 find_givs (struct ivopts_data *data)
1029 {
1030   struct loop *loop = data->current_loop;
1031   basic_block *body = get_loop_body_in_dom_order (loop);
1032   unsigned i;
1033
1034   for (i = 0; i < loop->num_nodes; i++)
1035     find_givs_in_bb (data, body[i]);
1036   free (body);
1037 }
1038
1039 /* For each ssa name defined in LOOP determines whether it is an induction
1040    variable and if so, its initial value and step.  */
1041
1042 static bool
1043 find_induction_variables (struct ivopts_data *data)
1044 {
1045   unsigned i;
1046   bitmap_iterator bi;
1047
1048   if (!find_bivs (data))
1049     return false;
1050
1051   find_givs (data);
1052   mark_bivs (data);
1053
1054   if (dump_file && (dump_flags & TDF_DETAILS))
1055     {
1056       tree niter = niter_for_single_dom_exit (data);
1057
1058       if (niter)
1059         {
1060           fprintf (dump_file, "  number of iterations ");
1061           print_generic_expr (dump_file, niter, TDF_SLIM);
1062           fprintf (dump_file, "\n\n");
1063         };
1064  
1065       fprintf (dump_file, "Induction variables:\n\n");
1066
1067       EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, i, bi)
1068         {
1069           if (ver_info (data, i)->iv)
1070             dump_iv (dump_file, ver_info (data, i)->iv);
1071         }
1072     }
1073
1074   return true;
1075 }
1076
1077 /* Records a use of type USE_TYPE at *USE_P in STMT whose value is IV.  */
1078
1079 static struct iv_use *
1080 record_use (struct ivopts_data *data, tree *use_p, struct iv *iv,
1081             gimple stmt, enum use_type use_type)
1082 {
1083   struct iv_use *use = XCNEW (struct iv_use);
1084
1085   use->id = n_iv_uses (data);
1086   use->type = use_type;
1087   use->iv = iv;
1088   use->stmt = stmt;
1089   use->op_p = use_p;
1090   use->related_cands = BITMAP_ALLOC (NULL);
1091
1092   /* To avoid showing ssa name in the dumps, if it was not reset by the
1093      caller.  */
1094   iv->ssa_name = NULL_TREE;
1095
1096   if (dump_file && (dump_flags & TDF_DETAILS))
1097     dump_use (dump_file, use);
1098
1099   VEC_safe_push (iv_use_p, heap, data->iv_uses, use);
1100
1101   return use;
1102 }
1103
1104 /* Checks whether OP is a loop-level invariant and if so, records it.
1105    NONLINEAR_USE is true if the invariant is used in a way we do not
1106    handle specially.  */
1107
1108 static void
1109 record_invariant (struct ivopts_data *data, tree op, bool nonlinear_use)
1110 {
1111   basic_block bb;
1112   struct version_info *info;
1113
1114   if (TREE_CODE (op) != SSA_NAME
1115       || !is_gimple_reg (op))
1116     return;
1117
1118   bb = gimple_bb (SSA_NAME_DEF_STMT (op));
1119   if (bb
1120       && flow_bb_inside_loop_p (data->current_loop, bb))
1121     return;
1122
1123   info = name_info (data, op);
1124   info->name = op;
1125   info->has_nonlin_use |= nonlinear_use;
1126   if (!info->inv_id)
1127     info->inv_id = ++data->max_inv_id;
1128   bitmap_set_bit (data->relevant, SSA_NAME_VERSION (op));
1129 }
1130
1131 /* Checks whether the use OP is interesting and if so, records it.  */
1132
1133 static struct iv_use *
1134 find_interesting_uses_op (struct ivopts_data *data, tree op)
1135 {
1136   struct iv *iv;
1137   struct iv *civ;
1138   gimple stmt;
1139   struct iv_use *use;
1140
1141   if (TREE_CODE (op) != SSA_NAME)
1142     return NULL;
1143
1144   iv = get_iv (data, op);
1145   if (!iv)
1146     return NULL;
1147   
1148   if (iv->have_use_for)
1149     {
1150       use = iv_use (data, iv->use_id);
1151
1152       gcc_assert (use->type == USE_NONLINEAR_EXPR);
1153       return use;
1154     }
1155
1156   if (integer_zerop (iv->step))
1157     {
1158       record_invariant (data, op, true);
1159       return NULL;
1160     }
1161   iv->have_use_for = true;
1162
1163   civ = XNEW (struct iv);
1164   *civ = *iv;
1165
1166   stmt = SSA_NAME_DEF_STMT (op);
1167   gcc_assert (gimple_code (stmt) == GIMPLE_PHI
1168               || is_gimple_assign (stmt));
1169
1170   use = record_use (data, NULL, civ, stmt, USE_NONLINEAR_EXPR);
1171   iv->use_id = use->id;
1172
1173   return use;
1174 }
1175
1176 /* Given a condition in statement STMT, checks whether it is a compare
1177    of an induction variable and an invariant.  If this is the case,
1178    CONTROL_VAR is set to location of the iv, BOUND to the location of
1179    the invariant, IV_VAR and IV_BOUND are set to the corresponding
1180    induction variable descriptions, and true is returned.  If this is not
1181    the case, CONTROL_VAR and BOUND are set to the arguments of the
1182    condition and false is returned.  */
1183
1184 static bool
1185 extract_cond_operands (struct ivopts_data *data, gimple stmt,
1186                        tree **control_var, tree **bound,
1187                        struct iv **iv_var, struct iv **iv_bound)
1188 {
1189   /* The objects returned when COND has constant operands.  */
1190   static struct iv const_iv;
1191   static tree zero;
1192   tree *op0 = &zero, *op1 = &zero, *tmp_op;
1193   struct iv *iv0 = &const_iv, *iv1 = &const_iv, *tmp_iv;
1194   bool ret = false;
1195
1196   if (gimple_code (stmt) == GIMPLE_COND)
1197     {
1198       op0 = gimple_cond_lhs_ptr (stmt);
1199       op1 = gimple_cond_rhs_ptr (stmt);
1200     }
1201   else
1202     {
1203       op0 = gimple_assign_rhs1_ptr (stmt);
1204       op1 = gimple_assign_rhs2_ptr (stmt);
1205     }
1206
1207   zero = integer_zero_node;
1208   const_iv.step = integer_zero_node;
1209
1210   if (TREE_CODE (*op0) == SSA_NAME)
1211     iv0 = get_iv (data, *op0);
1212   if (TREE_CODE (*op1) == SSA_NAME)
1213     iv1 = get_iv (data, *op1);
1214
1215   /* Exactly one of the compared values must be an iv, and the other one must
1216      be an invariant.  */
1217   if (!iv0 || !iv1)
1218     goto end;
1219
1220   if (integer_zerop (iv0->step))
1221     {
1222       /* Control variable may be on the other side.  */
1223       tmp_op = op0; op0 = op1; op1 = tmp_op;
1224       tmp_iv = iv0; iv0 = iv1; iv1 = tmp_iv;
1225     }
1226   ret = !integer_zerop (iv0->step) && integer_zerop (iv1->step);
1227
1228 end:
1229   if (control_var)
1230     *control_var = op0;;
1231   if (iv_var)
1232     *iv_var = iv0;;
1233   if (bound)
1234     *bound = op1;
1235   if (iv_bound)
1236     *iv_bound = iv1;
1237
1238   return ret;
1239 }
1240
1241 /* Checks whether the condition in STMT is interesting and if so,
1242    records it.  */
1243
1244 static void
1245 find_interesting_uses_cond (struct ivopts_data *data, gimple stmt)
1246 {
1247   tree *var_p, *bound_p;
1248   struct iv *var_iv, *civ;
1249
1250   if (!extract_cond_operands (data, stmt, &var_p, &bound_p, &var_iv, NULL))
1251     {
1252       find_interesting_uses_op (data, *var_p);
1253       find_interesting_uses_op (data, *bound_p);
1254       return;
1255     }
1256
1257   civ = XNEW (struct iv);
1258   *civ = *var_iv;
1259   record_use (data, NULL, civ, stmt, USE_COMPARE);
1260 }
1261
1262 /* Returns true if expression EXPR is obviously invariant in LOOP,
1263    i.e. if all its operands are defined outside of the LOOP.  LOOP
1264    should not be the function body.  */
1265
1266 bool
1267 expr_invariant_in_loop_p (struct loop *loop, tree expr)
1268 {
1269   basic_block def_bb;
1270   unsigned i, len;
1271
1272   gcc_assert (loop_depth (loop) > 0);
1273
1274   if (is_gimple_min_invariant (expr))
1275     return true;
1276
1277   if (TREE_CODE (expr) == SSA_NAME)
1278     {
1279       def_bb = gimple_bb (SSA_NAME_DEF_STMT (expr));
1280       if (def_bb
1281           && flow_bb_inside_loop_p (loop, def_bb))
1282         return false;
1283
1284       return true;
1285     }
1286
1287   if (!EXPR_P (expr))
1288     return false;
1289
1290   len = TREE_OPERAND_LENGTH (expr);
1291   for (i = 0; i < len; i++)
1292     if (!expr_invariant_in_loop_p (loop, TREE_OPERAND (expr, i)))
1293       return false;
1294
1295   return true;
1296 }
1297
1298 /* Returns true if statement STMT is obviously invariant in LOOP,
1299    i.e. if all its operands on the RHS are defined outside of the LOOP.
1300    LOOP should not be the function body.  */
1301
1302 bool
1303 stmt_invariant_in_loop_p (struct loop *loop, gimple stmt)
1304 {
1305   unsigned i;
1306   tree lhs;
1307
1308   gcc_assert (loop_depth (loop) > 0);
1309
1310   lhs = gimple_get_lhs (stmt);
1311   for (i = 0; i < gimple_num_ops (stmt); i++)
1312     {
1313       tree op = gimple_op (stmt, i);
1314       if (op != lhs && !expr_invariant_in_loop_p (loop, op))
1315         return false;
1316     }
1317
1318   return true;
1319 }
1320
1321 /* Cumulates the steps of indices into DATA and replaces their values with the
1322    initial ones.  Returns false when the value of the index cannot be determined.
1323    Callback for for_each_index.  */
1324
1325 struct ifs_ivopts_data
1326 {
1327   struct ivopts_data *ivopts_data;
1328   gimple stmt;
1329   tree step;
1330 };
1331
1332 static bool
1333 idx_find_step (tree base, tree *idx, void *data)
1334 {
1335   struct ifs_ivopts_data *dta = (struct ifs_ivopts_data *) data;
1336   struct iv *iv;
1337   tree step, iv_base, iv_step, lbound, off;
1338   struct loop *loop = dta->ivopts_data->current_loop;
1339
1340   if (TREE_CODE (base) == MISALIGNED_INDIRECT_REF
1341       || TREE_CODE (base) == ALIGN_INDIRECT_REF)
1342     return false;
1343
1344   /* If base is a component ref, require that the offset of the reference
1345      be invariant.  */
1346   if (TREE_CODE (base) == COMPONENT_REF)
1347     {
1348       off = component_ref_field_offset (base);
1349       return expr_invariant_in_loop_p (loop, off);
1350     }
1351
1352   /* If base is array, first check whether we will be able to move the
1353      reference out of the loop (in order to take its address in strength
1354      reduction).  In order for this to work we need both lower bound
1355      and step to be loop invariants.  */
1356   if (TREE_CODE (base) == ARRAY_REF)
1357     {
1358       step = array_ref_element_size (base);
1359       lbound = array_ref_low_bound (base);
1360
1361       if (!expr_invariant_in_loop_p (loop, step)
1362           || !expr_invariant_in_loop_p (loop, lbound))
1363         return false;
1364     }
1365
1366   if (TREE_CODE (*idx) != SSA_NAME)
1367     return true;
1368
1369   iv = get_iv (dta->ivopts_data, *idx);
1370   if (!iv)
1371     return false;
1372
1373   /* XXX  We produce for a base of *D42 with iv->base being &x[0]
1374           *&x[0], which is not folded and does not trigger the
1375           ARRAY_REF path below.  */
1376   *idx = iv->base;
1377
1378   if (integer_zerop (iv->step))
1379     return true;
1380
1381   if (TREE_CODE (base) == ARRAY_REF)
1382     {
1383       step = array_ref_element_size (base);
1384
1385       /* We only handle addresses whose step is an integer constant.  */
1386       if (TREE_CODE (step) != INTEGER_CST)
1387         return false;
1388     }
1389   else
1390     /* The step for pointer arithmetics already is 1 byte.  */
1391     step = build_int_cst (sizetype, 1);
1392
1393   iv_base = iv->base;
1394   iv_step = iv->step;
1395   if (!convert_affine_scev (dta->ivopts_data->current_loop,
1396                             sizetype, &iv_base, &iv_step, dta->stmt,
1397                             false))
1398     {
1399       /* The index might wrap.  */
1400       return false;
1401     }
1402
1403   step = fold_build2 (MULT_EXPR, sizetype, step, iv_step);
1404   dta->step = fold_build2 (PLUS_EXPR, sizetype, dta->step, step);
1405
1406   return true;
1407 }
1408
1409 /* Records use in index IDX.  Callback for for_each_index.  Ivopts data
1410    object is passed to it in DATA.  */
1411
1412 static bool
1413 idx_record_use (tree base, tree *idx,
1414                 void *vdata)
1415 {
1416   struct ivopts_data *data = (struct ivopts_data *) vdata;
1417   find_interesting_uses_op (data, *idx);
1418   if (TREE_CODE (base) == ARRAY_REF)
1419     {
1420       find_interesting_uses_op (data, array_ref_element_size (base));
1421       find_interesting_uses_op (data, array_ref_low_bound (base));
1422     }
1423   return true;
1424 }
1425
1426 /* If we can prove that TOP = cst * BOT for some constant cst,
1427    store cst to MUL and return true.  Otherwise return false.
1428    The returned value is always sign-extended, regardless of the
1429    signedness of TOP and BOT.  */
1430
1431 static bool
1432 constant_multiple_of (tree top, tree bot, double_int *mul)
1433 {
1434   tree mby;
1435   enum tree_code code;
1436   double_int res, p0, p1;
1437   unsigned precision = TYPE_PRECISION (TREE_TYPE (top));
1438
1439   STRIP_NOPS (top);
1440   STRIP_NOPS (bot);
1441
1442   if (operand_equal_p (top, bot, 0))
1443     {
1444       *mul = double_int_one;
1445       return true;
1446     }
1447
1448   code = TREE_CODE (top);
1449   switch (code)
1450     {
1451     case MULT_EXPR:
1452       mby = TREE_OPERAND (top, 1);
1453       if (TREE_CODE (mby) != INTEGER_CST)
1454         return false;
1455
1456       if (!constant_multiple_of (TREE_OPERAND (top, 0), bot, &res))
1457         return false;
1458
1459       *mul = double_int_sext (double_int_mul (res, tree_to_double_int (mby)),
1460                               precision);
1461       return true;
1462
1463     case PLUS_EXPR:
1464     case MINUS_EXPR:
1465       if (!constant_multiple_of (TREE_OPERAND (top, 0), bot, &p0)
1466           || !constant_multiple_of (TREE_OPERAND (top, 1), bot, &p1))
1467         return false;
1468
1469       if (code == MINUS_EXPR)
1470         p1 = double_int_neg (p1);
1471       *mul = double_int_sext (double_int_add (p0, p1), precision);
1472       return true;
1473
1474     case INTEGER_CST:
1475       if (TREE_CODE (bot) != INTEGER_CST)
1476         return false;
1477
1478       p0 = double_int_sext (tree_to_double_int (top), precision);
1479       p1 = double_int_sext (tree_to_double_int (bot), precision);
1480       if (double_int_zero_p (p1))
1481         return false;
1482       *mul = double_int_sext (double_int_sdivmod (p0, p1, FLOOR_DIV_EXPR, &res),
1483                               precision);
1484       return double_int_zero_p (res);
1485
1486     default:
1487       return false;
1488     }
1489 }
1490
1491 /* Returns true if memory reference REF with step STEP may be unaligned.  */
1492
1493 static bool
1494 may_be_unaligned_p (tree ref, tree step)
1495 {
1496   tree base;
1497   tree base_type;
1498   HOST_WIDE_INT bitsize;
1499   HOST_WIDE_INT bitpos;
1500   tree toffset;
1501   enum machine_mode mode;
1502   int unsignedp, volatilep;
1503   unsigned base_align;
1504
1505   /* TARGET_MEM_REFs are translated directly to valid MEMs on the target,
1506      thus they are not misaligned.  */
1507   if (TREE_CODE (ref) == TARGET_MEM_REF)
1508     return false;
1509
1510   /* The test below is basically copy of what expr.c:normal_inner_ref
1511      does to check whether the object must be loaded by parts when
1512      STRICT_ALIGNMENT is true.  */
1513   base = get_inner_reference (ref, &bitsize, &bitpos, &toffset, &mode,
1514                               &unsignedp, &volatilep, true);
1515   base_type = TREE_TYPE (base);
1516   base_align = TYPE_ALIGN (base_type);
1517
1518   if (mode != BLKmode)
1519     {
1520       double_int mul;
1521       tree al = build_int_cst (TREE_TYPE (step),
1522                                GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT);
1523
1524       if (base_align < GET_MODE_ALIGNMENT (mode)
1525           || bitpos % GET_MODE_ALIGNMENT (mode) != 0
1526           || bitpos % BITS_PER_UNIT != 0)
1527         return true;
1528     
1529       if (!constant_multiple_of (step, al, &mul))
1530         return true;
1531     }
1532
1533   return false;
1534 }
1535
1536 /* Return true if EXPR may be non-addressable.   */
1537
1538 static bool
1539 may_be_nonaddressable_p (tree expr)
1540 {
1541   switch (TREE_CODE (expr))
1542     {
1543     case TARGET_MEM_REF:
1544       /* TARGET_MEM_REFs are translated directly to valid MEMs on the
1545          target, thus they are always addressable.  */
1546       return false;
1547
1548     case COMPONENT_REF:
1549       return DECL_NONADDRESSABLE_P (TREE_OPERAND (expr, 1))
1550              || may_be_nonaddressable_p (TREE_OPERAND (expr, 0));
1551
1552     case VIEW_CONVERT_EXPR:
1553       /* This kind of view-conversions may wrap non-addressable objects
1554          and make them look addressable.  After some processing the
1555          non-addressability may be uncovered again, causing ADDR_EXPRs
1556          of inappropriate objects to be built.  */
1557       if (is_gimple_reg (TREE_OPERAND (expr, 0))
1558           || is_gimple_min_invariant (TREE_OPERAND (expr, 0)))
1559         return true;
1560
1561       /* ... fall through ... */
1562
1563     case ARRAY_REF:
1564     case ARRAY_RANGE_REF:
1565       return may_be_nonaddressable_p (TREE_OPERAND (expr, 0));
1566
1567     CASE_CONVERT:
1568       return true;
1569
1570     default:
1571       break;
1572     }
1573
1574   return false;
1575 }
1576
1577 /* Finds addresses in *OP_P inside STMT.  */
1578
1579 static void
1580 find_interesting_uses_address (struct ivopts_data *data, gimple stmt, tree *op_p)
1581 {
1582   tree base = *op_p, step = build_int_cst (sizetype, 0);
1583   struct iv *civ;
1584   struct ifs_ivopts_data ifs_ivopts_data;
1585
1586   /* Do not play with volatile memory references.  A bit too conservative,
1587      perhaps, but safe.  */
1588   if (gimple_has_volatile_ops (stmt))
1589     goto fail;
1590
1591   /* Ignore bitfields for now.  Not really something terribly complicated
1592      to handle.  TODO.  */
1593   if (TREE_CODE (base) == BIT_FIELD_REF)
1594     goto fail;
1595
1596   base = unshare_expr (base);
1597
1598   if (TREE_CODE (base) == TARGET_MEM_REF)
1599     {
1600       tree type = build_pointer_type (TREE_TYPE (base));
1601       tree astep;
1602
1603       if (TMR_BASE (base)
1604           && TREE_CODE (TMR_BASE (base)) == SSA_NAME)
1605         {
1606           civ = get_iv (data, TMR_BASE (base));
1607           if (!civ)
1608             goto fail;
1609
1610           TMR_BASE (base) = civ->base;
1611           step = civ->step;
1612         }
1613       if (TMR_INDEX (base)
1614           && TREE_CODE (TMR_INDEX (base)) == SSA_NAME)
1615         {
1616           civ = get_iv (data, TMR_INDEX (base));
1617           if (!civ)
1618             goto fail;
1619
1620           TMR_INDEX (base) = civ->base;
1621           astep = civ->step;
1622
1623           if (astep)
1624             {
1625               if (TMR_STEP (base))
1626                 astep = fold_build2 (MULT_EXPR, type, TMR_STEP (base), astep);
1627
1628               step = fold_build2 (PLUS_EXPR, type, step, astep);
1629             }
1630         }
1631
1632       if (integer_zerop (step))
1633         goto fail;
1634       base = tree_mem_ref_addr (type, base);
1635     }
1636   else
1637     {
1638       ifs_ivopts_data.ivopts_data = data;
1639       ifs_ivopts_data.stmt = stmt;
1640       ifs_ivopts_data.step = build_int_cst (sizetype, 0);
1641       if (!for_each_index (&base, idx_find_step, &ifs_ivopts_data)
1642           || integer_zerop (ifs_ivopts_data.step))
1643         goto fail;
1644       step = ifs_ivopts_data.step;
1645
1646       gcc_assert (TREE_CODE (base) != ALIGN_INDIRECT_REF);
1647       gcc_assert (TREE_CODE (base) != MISALIGNED_INDIRECT_REF);
1648
1649       /* Check that the base expression is addressable.  This needs
1650          to be done after substituting bases of IVs into it.  */
1651       if (may_be_nonaddressable_p (base))
1652         goto fail;
1653
1654       /* Moreover, on strict alignment platforms, check that it is
1655          sufficiently aligned.  */
1656       if (STRICT_ALIGNMENT && may_be_unaligned_p (base, step))
1657         goto fail;
1658
1659       base = build_fold_addr_expr (base);
1660
1661       /* Substituting bases of IVs into the base expression might
1662          have caused folding opportunities.  */
1663       if (TREE_CODE (base) == ADDR_EXPR)
1664         {
1665           tree *ref = &TREE_OPERAND (base, 0);
1666           while (handled_component_p (*ref))
1667             ref = &TREE_OPERAND (*ref, 0);
1668           if (TREE_CODE (*ref) == INDIRECT_REF)
1669             *ref = fold_indirect_ref (*ref);
1670         }
1671     }
1672
1673   civ = alloc_iv (base, step);
1674   record_use (data, op_p, civ, stmt, USE_ADDRESS);
1675   return;
1676
1677 fail:
1678   for_each_index (op_p, idx_record_use, data);
1679 }
1680
1681 /* Finds and records invariants used in STMT.  */
1682
1683 static void
1684 find_invariants_stmt (struct ivopts_data *data, gimple stmt)
1685 {
1686   ssa_op_iter iter;
1687   use_operand_p use_p;
1688   tree op;
1689
1690   FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_USE)
1691     {
1692       op = USE_FROM_PTR (use_p);
1693       record_invariant (data, op, false);
1694     }
1695 }
1696
1697 /* Finds interesting uses of induction variables in the statement STMT.  */
1698
1699 static void
1700 find_interesting_uses_stmt (struct ivopts_data *data, gimple stmt)
1701 {
1702   struct iv *iv;
1703   tree op, *lhs, *rhs;
1704   ssa_op_iter iter;
1705   use_operand_p use_p;
1706   enum tree_code code;
1707
1708   find_invariants_stmt (data, stmt);
1709
1710   if (gimple_code (stmt) == GIMPLE_COND)
1711     {
1712       find_interesting_uses_cond (data, stmt);
1713       return;
1714     }
1715
1716   if (is_gimple_assign (stmt))
1717     {
1718       lhs = gimple_assign_lhs_ptr (stmt);
1719       rhs = gimple_assign_rhs1_ptr (stmt);
1720
1721       if (TREE_CODE (*lhs) == SSA_NAME)
1722         {
1723           /* If the statement defines an induction variable, the uses are not
1724              interesting by themselves.  */
1725
1726           iv = get_iv (data, *lhs);
1727
1728           if (iv && !integer_zerop (iv->step))
1729             return;
1730         }
1731
1732       code = gimple_assign_rhs_code (stmt);
1733       if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
1734           && (REFERENCE_CLASS_P (*rhs)
1735               || is_gimple_val (*rhs)))
1736         {
1737           if (REFERENCE_CLASS_P (*rhs))
1738             find_interesting_uses_address (data, stmt, rhs);
1739           else
1740             find_interesting_uses_op (data, *rhs);
1741
1742           if (REFERENCE_CLASS_P (*lhs))
1743             find_interesting_uses_address (data, stmt, lhs);
1744           return;
1745         }
1746       else if (TREE_CODE_CLASS (code) == tcc_comparison)
1747         {
1748           find_interesting_uses_cond (data, stmt);
1749           return;
1750         }
1751
1752       /* TODO -- we should also handle address uses of type
1753
1754          memory = call (whatever);
1755
1756          and
1757
1758          call (memory).  */
1759     }
1760
1761   if (gimple_code (stmt) == GIMPLE_PHI
1762       && gimple_bb (stmt) == data->current_loop->header)
1763     {
1764       iv = get_iv (data, PHI_RESULT (stmt));
1765
1766       if (iv && !integer_zerop (iv->step))
1767         return;
1768     }
1769
1770   FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_USE)
1771     {
1772       op = USE_FROM_PTR (use_p);
1773
1774       if (TREE_CODE (op) != SSA_NAME)
1775         continue;
1776
1777       iv = get_iv (data, op);
1778       if (!iv)
1779         continue;
1780
1781       find_interesting_uses_op (data, op);
1782     }
1783 }
1784
1785 /* Finds interesting uses of induction variables outside of loops
1786    on loop exit edge EXIT.  */
1787
1788 static void
1789 find_interesting_uses_outside (struct ivopts_data *data, edge exit)
1790 {
1791   gimple phi;
1792   gimple_stmt_iterator psi;
1793   tree def;
1794
1795   for (psi = gsi_start_phis (exit->dest); !gsi_end_p (psi); gsi_next (&psi))
1796     {
1797       phi = gsi_stmt (psi);
1798       def = PHI_ARG_DEF_FROM_EDGE (phi, exit);
1799       if (is_gimple_reg (def))
1800         find_interesting_uses_op (data, def);
1801     }
1802 }
1803
1804 /* Finds uses of the induction variables that are interesting.  */
1805
1806 static void
1807 find_interesting_uses (struct ivopts_data *data)
1808 {
1809   basic_block bb;
1810   gimple_stmt_iterator bsi;
1811   basic_block *body = get_loop_body (data->current_loop);
1812   unsigned i;
1813   struct version_info *info;
1814   edge e;
1815
1816   if (dump_file && (dump_flags & TDF_DETAILS))
1817     fprintf (dump_file, "Uses:\n\n");
1818
1819   for (i = 0; i < data->current_loop->num_nodes; i++)
1820     {
1821       edge_iterator ei;
1822       bb = body[i];
1823
1824       FOR_EACH_EDGE (e, ei, bb->succs)
1825         if (e->dest != EXIT_BLOCK_PTR
1826             && !flow_bb_inside_loop_p (data->current_loop, e->dest))
1827           find_interesting_uses_outside (data, e);
1828
1829       for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1830         find_interesting_uses_stmt (data, gsi_stmt (bsi));
1831       for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1832         find_interesting_uses_stmt (data, gsi_stmt (bsi));
1833     }
1834
1835   if (dump_file && (dump_flags & TDF_DETAILS))
1836     {
1837       bitmap_iterator bi;
1838
1839       fprintf (dump_file, "\n");
1840
1841       EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, i, bi)
1842         {
1843           info = ver_info (data, i);
1844           if (info->inv_id)
1845             {
1846               fprintf (dump_file, "  ");
1847               print_generic_expr (dump_file, info->name, TDF_SLIM);
1848               fprintf (dump_file, " is invariant (%d)%s\n",
1849                        info->inv_id, info->has_nonlin_use ? "" : ", eliminable");
1850             }
1851         }
1852
1853       fprintf (dump_file, "\n");
1854     }
1855
1856   free (body);
1857 }
1858
1859 /* Strips constant offsets from EXPR and stores them to OFFSET.  If INSIDE_ADDR
1860    is true, assume we are inside an address.  If TOP_COMPREF is true, assume
1861    we are at the top-level of the processed address.  */
1862
1863 static tree
1864 strip_offset_1 (tree expr, bool inside_addr, bool top_compref,
1865                 unsigned HOST_WIDE_INT *offset)
1866 {
1867   tree op0 = NULL_TREE, op1 = NULL_TREE, tmp, step;
1868   enum tree_code code;
1869   tree type, orig_type = TREE_TYPE (expr);
1870   unsigned HOST_WIDE_INT off0, off1, st;
1871   tree orig_expr = expr;
1872
1873   STRIP_NOPS (expr);
1874
1875   type = TREE_TYPE (expr);
1876   code = TREE_CODE (expr);
1877   *offset = 0;
1878
1879   switch (code)
1880     {
1881     case INTEGER_CST:
1882       if (!cst_and_fits_in_hwi (expr)
1883           || integer_zerop (expr))
1884         return orig_expr;
1885
1886       *offset = int_cst_value (expr);
1887       return build_int_cst (orig_type, 0);
1888
1889     case POINTER_PLUS_EXPR:
1890     case PLUS_EXPR:
1891     case MINUS_EXPR:
1892       op0 = TREE_OPERAND (expr, 0);
1893       op1 = TREE_OPERAND (expr, 1);
1894
1895       op0 = strip_offset_1 (op0, false, false, &off0);
1896       op1 = strip_offset_1 (op1, false, false, &off1);
1897
1898       *offset = (code == MINUS_EXPR ? off0 - off1 : off0 + off1);
1899       if (op0 == TREE_OPERAND (expr, 0)
1900           && op1 == TREE_OPERAND (expr, 1))
1901         return orig_expr;
1902
1903       if (integer_zerop (op1))
1904         expr = op0;
1905       else if (integer_zerop (op0))
1906         {
1907           if (code == MINUS_EXPR)
1908             expr = fold_build1 (NEGATE_EXPR, type, op1);
1909           else
1910             expr = op1;
1911         }
1912       else
1913         expr = fold_build2 (code, type, op0, op1);
1914
1915       return fold_convert (orig_type, expr);
1916
1917     case ARRAY_REF:
1918       if (!inside_addr)
1919         return orig_expr;
1920
1921       step = array_ref_element_size (expr);
1922       if (!cst_and_fits_in_hwi (step))
1923         break;
1924
1925       st = int_cst_value (step);
1926       op1 = TREE_OPERAND (expr, 1);
1927       op1 = strip_offset_1 (op1, false, false, &off1);
1928       *offset = off1 * st;
1929
1930       if (top_compref
1931           && integer_zerop (op1))
1932         {
1933           /* Strip the component reference completely.  */
1934           op0 = TREE_OPERAND (expr, 0);
1935           op0 = strip_offset_1 (op0, inside_addr, top_compref, &off0);
1936           *offset += off0;
1937           return op0;
1938         }
1939       break;
1940
1941     case COMPONENT_REF:
1942       if (!inside_addr)
1943         return orig_expr;
1944
1945       tmp = component_ref_field_offset (expr);
1946       if (top_compref
1947           && cst_and_fits_in_hwi (tmp))
1948         {
1949           /* Strip the component reference completely.  */
1950           op0 = TREE_OPERAND (expr, 0);
1951           op0 = strip_offset_1 (op0, inside_addr, top_compref, &off0);
1952           *offset = off0 + int_cst_value (tmp);
1953           return op0;
1954         }
1955       break;
1956
1957     case ADDR_EXPR:
1958       op0 = TREE_OPERAND (expr, 0);
1959       op0 = strip_offset_1 (op0, true, true, &off0);
1960       *offset += off0;
1961
1962       if (op0 == TREE_OPERAND (expr, 0))
1963         return orig_expr;
1964
1965       expr = build_fold_addr_expr (op0);
1966       return fold_convert (orig_type, expr);
1967
1968     case INDIRECT_REF:
1969       inside_addr = false;
1970       break;
1971
1972     default:
1973       return orig_expr;
1974     }
1975
1976   /* Default handling of expressions for that we want to recurse into
1977      the first operand.  */
1978   op0 = TREE_OPERAND (expr, 0);
1979   op0 = strip_offset_1 (op0, inside_addr, false, &off0);
1980   *offset += off0;
1981
1982   if (op0 == TREE_OPERAND (expr, 0)
1983       && (!op1 || op1 == TREE_OPERAND (expr, 1)))
1984     return orig_expr;
1985
1986   expr = copy_node (expr);
1987   TREE_OPERAND (expr, 0) = op0;
1988   if (op1)
1989     TREE_OPERAND (expr, 1) = op1;
1990
1991   /* Inside address, we might strip the top level component references,
1992      thus changing type of the expression.  Handling of ADDR_EXPR
1993      will fix that.  */
1994   expr = fold_convert (orig_type, expr);
1995
1996   return expr;
1997 }
1998
1999 /* Strips constant offsets from EXPR and stores them to OFFSET.  */
2000
2001 static tree
2002 strip_offset (tree expr, unsigned HOST_WIDE_INT *offset)
2003 {
2004   return strip_offset_1 (expr, false, false, offset);
2005 }
2006
2007 /* Returns variant of TYPE that can be used as base for different uses.
2008    We return unsigned type with the same precision, which avoids problems
2009    with overflows.  */
2010
2011 static tree
2012 generic_type_for (tree type)
2013 {
2014   if (POINTER_TYPE_P (type))
2015     return unsigned_type_for (type);
2016
2017   if (TYPE_UNSIGNED (type))
2018     return type;
2019
2020   return unsigned_type_for (type);
2021 }
2022
2023 /* Records invariants in *EXPR_P.  Callback for walk_tree.  DATA contains
2024    the bitmap to that we should store it.  */
2025
2026 static struct ivopts_data *fd_ivopts_data;
2027 static tree
2028 find_depends (tree *expr_p, int *ws ATTRIBUTE_UNUSED, void *data)
2029 {
2030   bitmap *depends_on = (bitmap *) data;
2031   struct version_info *info;
2032
2033   if (TREE_CODE (*expr_p) != SSA_NAME)
2034     return NULL_TREE;
2035   info = name_info (fd_ivopts_data, *expr_p);
2036
2037   if (!info->inv_id || info->has_nonlin_use)
2038     return NULL_TREE;
2039
2040   if (!*depends_on)
2041     *depends_on = BITMAP_ALLOC (NULL);
2042   bitmap_set_bit (*depends_on, info->inv_id);
2043
2044   return NULL_TREE;
2045 }
2046
2047 /* Adds a candidate BASE + STEP * i.  Important field is set to IMPORTANT and
2048    position to POS.  If USE is not NULL, the candidate is set as related to
2049    it.  If both BASE and STEP are NULL, we add a pseudocandidate for the
2050    replacement of the final value of the iv by a direct computation.  */
2051
2052 static struct iv_cand *
2053 add_candidate_1 (struct ivopts_data *data,
2054                  tree base, tree step, bool important, enum iv_position pos,
2055                  struct iv_use *use, gimple incremented_at)
2056 {
2057   unsigned i;
2058   struct iv_cand *cand = NULL;
2059   tree type, orig_type;
2060   
2061   if (base)
2062     {
2063       orig_type = TREE_TYPE (base);
2064       type = generic_type_for (orig_type);
2065       /* Don't convert the base to the generic type for pointers as the generic
2066          type is an integer type with the same size as the pointer type.  */
2067       if (type != orig_type && !POINTER_TYPE_P (orig_type))
2068         {
2069           base = fold_convert (type, base);
2070           step = fold_convert (type, step);
2071         }
2072     }
2073
2074   for (i = 0; i < n_iv_cands (data); i++)
2075     {
2076       cand = iv_cand (data, i);
2077
2078       if (cand->pos != pos)
2079         continue;
2080
2081       if (cand->incremented_at != incremented_at)
2082         continue;
2083
2084       if (!cand->iv)
2085         {
2086           if (!base && !step)
2087             break;
2088
2089           continue;
2090         }
2091
2092       if (!base && !step)
2093         continue;
2094
2095       if (operand_equal_p (base, cand->iv->base, 0)
2096           && operand_equal_p (step, cand->iv->step, 0))
2097         break;
2098     }
2099
2100   if (i == n_iv_cands (data))
2101     {
2102       cand = XCNEW (struct iv_cand);
2103       cand->id = i;
2104
2105       if (!base && !step)
2106         cand->iv = NULL;
2107       else
2108         cand->iv = alloc_iv (base, step);
2109
2110       cand->pos = pos;
2111       if (pos != IP_ORIGINAL && cand->iv)
2112         {
2113           cand->var_before = create_tmp_var_raw (TREE_TYPE (base), "ivtmp");
2114           cand->var_after = cand->var_before;
2115         }
2116       cand->important = important;
2117       cand->incremented_at = incremented_at;
2118       VEC_safe_push (iv_cand_p, heap, data->iv_candidates, cand);
2119
2120       if (step
2121           && TREE_CODE (step) != INTEGER_CST)
2122         {
2123           fd_ivopts_data = data;
2124           walk_tree (&step, find_depends, &cand->depends_on, NULL);
2125         }
2126
2127       if (dump_file && (dump_flags & TDF_DETAILS))
2128         dump_cand (dump_file, cand);
2129     }
2130
2131   if (important && !cand->important)
2132     {
2133       cand->important = true;
2134       if (dump_file && (dump_flags & TDF_DETAILS))
2135         fprintf (dump_file, "Candidate %d is important\n", cand->id);
2136     }
2137
2138   if (use)
2139     {
2140       bitmap_set_bit (use->related_cands, i);
2141       if (dump_file && (dump_flags & TDF_DETAILS))
2142         fprintf (dump_file, "Candidate %d is related to use %d\n",
2143                  cand->id, use->id);
2144     }
2145
2146   return cand;
2147 }
2148
2149 /* Returns true if incrementing the induction variable at the end of the LOOP
2150    is allowed.
2151
2152    The purpose is to avoid splitting latch edge with a biv increment, thus
2153    creating a jump, possibly confusing other optimization passes and leaving
2154    less freedom to scheduler.  So we allow IP_END_POS only if IP_NORMAL_POS
2155    is not available (so we do not have a better alternative), or if the latch
2156    edge is already nonempty.  */
2157
2158 static bool
2159 allow_ip_end_pos_p (struct loop *loop)
2160 {
2161   if (!ip_normal_pos (loop))
2162     return true;
2163
2164   if (!empty_block_p (ip_end_pos (loop)))
2165     return true;
2166
2167   return false;
2168 }
2169
2170 /* Adds a candidate BASE + STEP * i.  Important field is set to IMPORTANT and
2171    position to POS.  If USE is not NULL, the candidate is set as related to
2172    it.  The candidate computation is scheduled on all available positions.  */
2173
2174 static void
2175 add_candidate (struct ivopts_data *data, 
2176                tree base, tree step, bool important, struct iv_use *use)
2177 {
2178   if (ip_normal_pos (data->current_loop))
2179     add_candidate_1 (data, base, step, important, IP_NORMAL, use, NULL);
2180   if (ip_end_pos (data->current_loop)
2181       && allow_ip_end_pos_p (data->current_loop))
2182     add_candidate_1 (data, base, step, important, IP_END, use, NULL);
2183 }
2184
2185 /* Add a standard "0 + 1 * iteration" iv candidate for a
2186    type with SIZE bits.  */
2187
2188 static void
2189 add_standard_iv_candidates_for_size (struct ivopts_data *data,
2190                                      unsigned int size)
2191 {
2192   tree type = lang_hooks.types.type_for_size (size, true);
2193   add_candidate (data, build_int_cst (type, 0), build_int_cst (type, 1),
2194                  true, NULL);
2195 }
2196
2197 /* Adds standard iv candidates.  */
2198
2199 static void
2200 add_standard_iv_candidates (struct ivopts_data *data)
2201 {
2202   add_standard_iv_candidates_for_size (data, INT_TYPE_SIZE);
2203
2204   /* The same for a double-integer type if it is still fast enough.  */
2205   if (BITS_PER_WORD >= INT_TYPE_SIZE * 2)
2206     add_standard_iv_candidates_for_size (data, INT_TYPE_SIZE * 2);
2207 }
2208
2209
2210 /* Adds candidates bases on the old induction variable IV.  */
2211
2212 static void
2213 add_old_iv_candidates (struct ivopts_data *data, struct iv *iv)
2214 {
2215   gimple phi;
2216   tree def;
2217   struct iv_cand *cand;
2218
2219   add_candidate (data, iv->base, iv->step, true, NULL);
2220
2221   /* The same, but with initial value zero.  */
2222   add_candidate (data,
2223                  build_int_cst (TREE_TYPE (iv->base), 0),
2224                  iv->step, true, NULL);
2225
2226   phi = SSA_NAME_DEF_STMT (iv->ssa_name);
2227   if (gimple_code (phi) == GIMPLE_PHI)
2228     {
2229       /* Additionally record the possibility of leaving the original iv
2230          untouched.  */
2231       def = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (data->current_loop));
2232       cand = add_candidate_1 (data,
2233                               iv->base, iv->step, true, IP_ORIGINAL, NULL,
2234                               SSA_NAME_DEF_STMT (def));
2235       cand->var_before = iv->ssa_name;
2236       cand->var_after = def;
2237     }
2238 }
2239
2240 /* Adds candidates based on the old induction variables.  */
2241
2242 static void
2243 add_old_ivs_candidates (struct ivopts_data *data)
2244 {
2245   unsigned i;
2246   struct iv *iv;
2247   bitmap_iterator bi;
2248
2249   EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, i, bi)
2250     {
2251       iv = ver_info (data, i)->iv;
2252       if (iv && iv->biv_p && !integer_zerop (iv->step))
2253         add_old_iv_candidates (data, iv);
2254     }
2255 }
2256
2257 /* Adds candidates based on the value of the induction variable IV and USE.  */
2258
2259 static void
2260 add_iv_value_candidates (struct ivopts_data *data,
2261                          struct iv *iv, struct iv_use *use)
2262 {
2263   unsigned HOST_WIDE_INT offset;
2264   tree base;
2265   tree basetype;
2266
2267   add_candidate (data, iv->base, iv->step, false, use);
2268
2269   /* The same, but with initial value zero.  Make such variable important,
2270      since it is generic enough so that possibly many uses may be based
2271      on it.  */
2272   basetype = TREE_TYPE (iv->base);
2273   if (POINTER_TYPE_P (basetype))
2274     basetype = sizetype;
2275   add_candidate (data, build_int_cst (basetype, 0),
2276                  iv->step, true, use);
2277
2278   /* Third, try removing the constant offset.  Make sure to even
2279      add a candidate for &a[0] vs. (T *)&a.  */
2280   base = strip_offset (iv->base, &offset);
2281   if (offset
2282       || base != iv->base)
2283     add_candidate (data, base, iv->step, false, use);
2284 }
2285
2286 /* Adds candidates based on the uses.  */
2287
2288 static void
2289 add_derived_ivs_candidates (struct ivopts_data *data)
2290 {
2291   unsigned i;
2292
2293   for (i = 0; i < n_iv_uses (data); i++)
2294     {
2295       struct iv_use *use = iv_use (data, i);
2296
2297       if (!use)
2298         continue;
2299
2300       switch (use->type)
2301         {
2302         case USE_NONLINEAR_EXPR:
2303         case USE_COMPARE:
2304         case USE_ADDRESS:
2305           /* Just add the ivs based on the value of the iv used here.  */
2306           add_iv_value_candidates (data, use->iv, use);
2307           break;
2308
2309         default:
2310           gcc_unreachable ();
2311         }
2312     }
2313 }
2314
2315 /* Record important candidates and add them to related_cands bitmaps
2316    if needed.  */
2317
2318 static void
2319 record_important_candidates (struct ivopts_data *data)
2320 {
2321   unsigned i;
2322   struct iv_use *use;
2323
2324   for (i = 0; i < n_iv_cands (data); i++)
2325     {
2326       struct iv_cand *cand = iv_cand (data, i);
2327
2328       if (cand->important)
2329         bitmap_set_bit (data->important_candidates, i);
2330     }
2331
2332   data->consider_all_candidates = (n_iv_cands (data)
2333                                    <= CONSIDER_ALL_CANDIDATES_BOUND);
2334
2335   if (data->consider_all_candidates)
2336     {
2337       /* We will not need "related_cands" bitmaps in this case,
2338          so release them to decrease peak memory consumption.  */
2339       for (i = 0; i < n_iv_uses (data); i++)
2340         {
2341           use = iv_use (data, i);
2342           BITMAP_FREE (use->related_cands);
2343         }
2344     }
2345   else
2346     {
2347       /* Add important candidates to the related_cands bitmaps.  */
2348       for (i = 0; i < n_iv_uses (data); i++)
2349         bitmap_ior_into (iv_use (data, i)->related_cands,
2350                          data->important_candidates);
2351     }
2352 }
2353
2354 /* Finds the candidates for the induction variables.  */
2355
2356 static void
2357 find_iv_candidates (struct ivopts_data *data)
2358 {
2359   /* Add commonly used ivs.  */
2360   add_standard_iv_candidates (data);
2361
2362   /* Add old induction variables.  */
2363   add_old_ivs_candidates (data);
2364
2365   /* Add induction variables derived from uses.  */
2366   add_derived_ivs_candidates (data);
2367
2368   /* Record the important candidates.  */
2369   record_important_candidates (data);
2370 }
2371
2372 /* Allocates the data structure mapping the (use, candidate) pairs to costs.
2373    If consider_all_candidates is true, we use a two-dimensional array, otherwise
2374    we allocate a simple list to every use.  */
2375
2376 static void
2377 alloc_use_cost_map (struct ivopts_data *data)
2378 {
2379   unsigned i, size, s, j;
2380
2381   for (i = 0; i < n_iv_uses (data); i++)
2382     {
2383       struct iv_use *use = iv_use (data, i);
2384       bitmap_iterator bi;
2385
2386       if (data->consider_all_candidates)
2387         size = n_iv_cands (data);
2388       else
2389         {
2390           s = 0;
2391           EXECUTE_IF_SET_IN_BITMAP (use->related_cands, 0, j, bi)
2392             {
2393               s++;
2394             }
2395
2396           /* Round up to the power of two, so that moduling by it is fast.  */
2397           for (size = 1; size < s; size <<= 1)
2398             continue;
2399         }
2400
2401       use->n_map_members = size;
2402       use->cost_map = XCNEWVEC (struct cost_pair, size);
2403     }
2404 }
2405
2406 /* Returns description of computation cost of expression whose runtime
2407    cost is RUNTIME and complexity corresponds to COMPLEXITY.  */
2408
2409 static comp_cost
2410 new_cost (unsigned runtime, unsigned complexity)
2411 {
2412   comp_cost cost;
2413
2414   cost.cost = runtime;
2415   cost.complexity = complexity;
2416
2417   return cost;
2418 }
2419
2420 /* Adds costs COST1 and COST2.  */
2421
2422 static comp_cost
2423 add_costs (comp_cost cost1, comp_cost cost2)
2424 {
2425   cost1.cost += cost2.cost;
2426   cost1.complexity += cost2.complexity;
2427
2428   return cost1;
2429 }
2430 /* Subtracts costs COST1 and COST2.  */
2431
2432 static comp_cost
2433 sub_costs (comp_cost cost1, comp_cost cost2)
2434 {
2435   cost1.cost -= cost2.cost;
2436   cost1.complexity -= cost2.complexity;
2437
2438   return cost1;
2439 }
2440
2441 /* Returns a negative number if COST1 < COST2, a positive number if
2442    COST1 > COST2, and 0 if COST1 = COST2.  */
2443
2444 static int
2445 compare_costs (comp_cost cost1, comp_cost cost2)
2446 {
2447   if (cost1.cost == cost2.cost)
2448     return cost1.complexity - cost2.complexity;
2449
2450   return cost1.cost - cost2.cost;
2451 }
2452
2453 /* Returns true if COST is infinite.  */
2454
2455 static bool
2456 infinite_cost_p (comp_cost cost)
2457 {
2458   return cost.cost == INFTY;
2459 }
2460
2461 /* Sets cost of (USE, CANDIDATE) pair to COST and record that it depends
2462    on invariants DEPENDS_ON and that the value used in expressing it
2463    is VALUE.*/
2464
2465 static void
2466 set_use_iv_cost (struct ivopts_data *data,
2467                  struct iv_use *use, struct iv_cand *cand,
2468                  comp_cost cost, bitmap depends_on, tree value)
2469 {
2470   unsigned i, s;
2471
2472   if (infinite_cost_p (cost))
2473     {
2474       BITMAP_FREE (depends_on);
2475       return;
2476     }
2477
2478   if (data->consider_all_candidates)
2479     {
2480       use->cost_map[cand->id].cand = cand;
2481       use->cost_map[cand->id].cost = cost;
2482       use->cost_map[cand->id].depends_on = depends_on;
2483       use->cost_map[cand->id].value = value;
2484       return;
2485     }
2486
2487   /* n_map_members is a power of two, so this computes modulo.  */
2488   s = cand->id & (use->n_map_members - 1);
2489   for (i = s; i < use->n_map_members; i++)
2490     if (!use->cost_map[i].cand)
2491       goto found;
2492   for (i = 0; i < s; i++)
2493     if (!use->cost_map[i].cand)
2494       goto found;
2495
2496   gcc_unreachable ();
2497
2498 found:
2499   use->cost_map[i].cand = cand;
2500   use->cost_map[i].cost = cost;
2501   use->cost_map[i].depends_on = depends_on;
2502   use->cost_map[i].value = value;
2503 }
2504
2505 /* Gets cost of (USE, CANDIDATE) pair.  */
2506
2507 static struct cost_pair *
2508 get_use_iv_cost (struct ivopts_data *data, struct iv_use *use,
2509                  struct iv_cand *cand)
2510 {
2511   unsigned i, s;
2512   struct cost_pair *ret;
2513
2514   if (!cand)
2515     return NULL;
2516
2517   if (data->consider_all_candidates)
2518     {
2519       ret = use->cost_map + cand->id;
2520       if (!ret->cand)
2521         return NULL;
2522
2523       return ret;
2524     }
2525       
2526   /* n_map_members is a power of two, so this computes modulo.  */
2527   s = cand->id & (use->n_map_members - 1);
2528   for (i = s; i < use->n_map_members; i++)
2529     if (use->cost_map[i].cand == cand)
2530       return use->cost_map + i;
2531
2532   for (i = 0; i < s; i++)
2533     if (use->cost_map[i].cand == cand)
2534       return use->cost_map + i;
2535
2536   return NULL;
2537 }
2538
2539 /* Returns estimate on cost of computing SEQ.  */
2540
2541 static unsigned
2542 seq_cost (rtx seq)
2543 {
2544   unsigned cost = 0;
2545   rtx set;
2546
2547   for (; seq; seq = NEXT_INSN (seq))
2548     {
2549       set = single_set (seq);
2550       if (set)
2551         cost += rtx_cost (set, SET);
2552       else
2553         cost++;
2554     }
2555
2556   return cost;
2557 }
2558
2559 /* Produce DECL_RTL for object obj so it looks like it is stored in memory.  */
2560 static rtx
2561 produce_memory_decl_rtl (tree obj, int *regno)
2562 {
2563   rtx x;
2564   
2565   gcc_assert (obj);
2566   if (TREE_STATIC (obj) || DECL_EXTERNAL (obj))
2567     {
2568       const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (obj));
2569       x = gen_rtx_SYMBOL_REF (Pmode, name);
2570       SET_SYMBOL_REF_DECL (x, obj);
2571       x = gen_rtx_MEM (DECL_MODE (obj), x);
2572       targetm.encode_section_info (obj, x, true);
2573     }
2574   else
2575     {
2576       x = gen_raw_REG (Pmode, (*regno)++);
2577       x = gen_rtx_MEM (DECL_MODE (obj), x);
2578     }
2579
2580   return x;
2581 }
2582
2583 /* Prepares decl_rtl for variables referred in *EXPR_P.  Callback for
2584    walk_tree.  DATA contains the actual fake register number.  */
2585
2586 static tree
2587 prepare_decl_rtl (tree *expr_p, int *ws, void *data)
2588 {
2589   tree obj = NULL_TREE;
2590   rtx x = NULL_RTX;
2591   int *regno = (int *) data;
2592
2593   switch (TREE_CODE (*expr_p))
2594     {
2595     case ADDR_EXPR:
2596       for (expr_p = &TREE_OPERAND (*expr_p, 0);
2597            handled_component_p (*expr_p);
2598            expr_p = &TREE_OPERAND (*expr_p, 0))
2599         continue;
2600       obj = *expr_p;
2601       if (DECL_P (obj) && !DECL_RTL_SET_P (obj))
2602         x = produce_memory_decl_rtl (obj, regno);
2603       break;
2604
2605     case SSA_NAME:
2606       *ws = 0;
2607       obj = SSA_NAME_VAR (*expr_p);
2608       if (!DECL_RTL_SET_P (obj))
2609         x = gen_raw_REG (DECL_MODE (obj), (*regno)++);
2610       break;
2611
2612     case VAR_DECL:
2613     case PARM_DECL:
2614     case RESULT_DECL:
2615       *ws = 0;
2616       obj = *expr_p;
2617
2618       if (DECL_RTL_SET_P (obj))
2619         break;
2620
2621       if (DECL_MODE (obj) == BLKmode)
2622         x = produce_memory_decl_rtl (obj, regno);
2623       else
2624         x = gen_raw_REG (DECL_MODE (obj), (*regno)++);
2625
2626       break;
2627
2628     default:
2629       break;
2630     }
2631
2632   if (x)
2633     {
2634       VEC_safe_push (tree, heap, decl_rtl_to_reset, obj);
2635       SET_DECL_RTL (obj, x);
2636     }
2637
2638   return NULL_TREE;
2639 }
2640
2641 /* Determines cost of the computation of EXPR.  */
2642
2643 static unsigned
2644 computation_cost (tree expr)
2645 {
2646   rtx seq, rslt;
2647   tree type = TREE_TYPE (expr);
2648   unsigned cost;
2649   /* Avoid using hard regs in ways which may be unsupported.  */
2650   int regno = LAST_VIRTUAL_REGISTER + 1;
2651
2652   walk_tree (&expr, prepare_decl_rtl, &regno, NULL);
2653   start_sequence ();
2654   rslt = expand_expr (expr, NULL_RTX, TYPE_MODE (type), EXPAND_NORMAL);
2655   seq = get_insns ();
2656   end_sequence ();
2657
2658   cost = seq_cost (seq);
2659   if (MEM_P (rslt))
2660     cost += address_cost (XEXP (rslt, 0), TYPE_MODE (type));
2661
2662   return cost;
2663 }
2664
2665 /* Returns variable containing the value of candidate CAND at statement AT.  */
2666
2667 static tree
2668 var_at_stmt (struct loop *loop, struct iv_cand *cand, gimple stmt)
2669 {
2670   if (stmt_after_increment (loop, cand, stmt))
2671     return cand->var_after;
2672   else
2673     return cand->var_before;
2674 }
2675
2676 /* Return the most significant (sign) bit of T.  Similar to tree_int_cst_msb,
2677    but the bit is determined from TYPE_PRECISION, not MODE_BITSIZE.  */
2678
2679 int
2680 tree_int_cst_sign_bit (const_tree t)
2681 {
2682   unsigned bitno = TYPE_PRECISION (TREE_TYPE (t)) - 1;
2683   unsigned HOST_WIDE_INT w;
2684
2685   if (bitno < HOST_BITS_PER_WIDE_INT)
2686     w = TREE_INT_CST_LOW (t);
2687   else
2688     {
2689       w = TREE_INT_CST_HIGH (t);
2690       bitno -= HOST_BITS_PER_WIDE_INT;
2691     }
2692
2693   return (w >> bitno) & 1;
2694 }
2695
2696 /* If A is (TYPE) BA and B is (TYPE) BB, and the types of BA and BB have the
2697    same precision that is at least as wide as the precision of TYPE, stores
2698    BA to A and BB to B, and returns the type of BA.  Otherwise, returns the
2699    type of A and B.  */
2700
2701 static tree
2702 determine_common_wider_type (tree *a, tree *b)
2703 {
2704   tree wider_type = NULL;
2705   tree suba, subb;
2706   tree atype = TREE_TYPE (*a);
2707
2708   if (CONVERT_EXPR_P (*a))
2709     {
2710       suba = TREE_OPERAND (*a, 0);
2711       wider_type = TREE_TYPE (suba);
2712       if (TYPE_PRECISION (wider_type) < TYPE_PRECISION (atype))
2713         return atype;
2714     }
2715   else
2716     return atype;
2717
2718   if (CONVERT_EXPR_P (*b))
2719     {
2720       subb = TREE_OPERAND (*b, 0);
2721       if (TYPE_PRECISION (wider_type) != TYPE_PRECISION (TREE_TYPE (subb)))
2722         return atype;
2723     }
2724   else
2725     return atype;
2726
2727   *a = suba;
2728   *b = subb;
2729   return wider_type;
2730 }
2731
2732 /* Determines the expression by that USE is expressed from induction variable
2733    CAND at statement AT in LOOP.  The expression is stored in a decomposed
2734    form into AFF.  Returns false if USE cannot be expressed using CAND.  */
2735
2736 static bool
2737 get_computation_aff (struct loop *loop,
2738                      struct iv_use *use, struct iv_cand *cand, gimple at,
2739                      struct affine_tree_combination *aff)
2740 {
2741   tree ubase = use->iv->base;
2742   tree ustep = use->iv->step;
2743   tree cbase = cand->iv->base;
2744   tree cstep = cand->iv->step, cstep_common;
2745   tree utype = TREE_TYPE (ubase), ctype = TREE_TYPE (cbase);
2746   tree common_type, var;
2747   tree uutype;
2748   aff_tree cbase_aff, var_aff;
2749   double_int rat;
2750
2751   if (TYPE_PRECISION (utype) > TYPE_PRECISION (ctype))
2752     {
2753       /* We do not have a precision to express the values of use.  */
2754       return false;
2755     }
2756
2757   var = var_at_stmt (loop, cand, at);
2758   uutype = unsigned_type_for (utype);
2759
2760   /* If the conversion is not noop, perform it.  */
2761   if (TYPE_PRECISION (utype) < TYPE_PRECISION (ctype))
2762     {
2763       cstep = fold_convert (uutype, cstep);
2764       cbase = fold_convert (uutype, cbase);
2765       var = fold_convert (uutype, var);
2766     }
2767
2768   if (!constant_multiple_of (ustep, cstep, &rat))
2769     return false;
2770
2771   /* In case both UBASE and CBASE are shortened to UUTYPE from some common
2772      type, we achieve better folding by computing their difference in this
2773      wider type, and cast the result to UUTYPE.  We do not need to worry about
2774      overflows, as all the arithmetics will in the end be performed in UUTYPE
2775      anyway.  */
2776   common_type = determine_common_wider_type (&ubase, &cbase);
2777
2778   /* use = ubase - ratio * cbase + ratio * var.  */
2779   tree_to_aff_combination (ubase, common_type, aff);
2780   tree_to_aff_combination (cbase, common_type, &cbase_aff);
2781   tree_to_aff_combination (var, uutype, &var_aff);
2782
2783   /* We need to shift the value if we are after the increment.  */
2784   if (stmt_after_increment (loop, cand, at))
2785     {
2786       aff_tree cstep_aff;
2787   
2788       if (common_type != uutype)
2789         cstep_common = fold_convert (common_type, cstep);
2790       else
2791         cstep_common = cstep;
2792
2793       tree_to_aff_combination (cstep_common, common_type, &cstep_aff);
2794       aff_combination_add (&cbase_aff, &cstep_aff);
2795     }
2796
2797   aff_combination_scale (&cbase_aff, double_int_neg (rat));
2798   aff_combination_add (aff, &cbase_aff);
2799   if (common_type != uutype)
2800     aff_combination_convert (aff, uutype);
2801
2802   aff_combination_scale (&var_aff, rat);
2803   aff_combination_add (aff, &var_aff);
2804
2805   return true;
2806 }
2807
2808 /* Determines the expression by that USE is expressed from induction variable
2809    CAND at statement AT in LOOP.  The computation is unshared.  */
2810
2811 static tree
2812 get_computation_at (struct loop *loop,
2813                     struct iv_use *use, struct iv_cand *cand, gimple at)
2814 {
2815   aff_tree aff;
2816   tree type = TREE_TYPE (use->iv->base);
2817
2818   if (!get_computation_aff (loop, use, cand, at, &aff))
2819     return NULL_TREE;
2820   unshare_aff_combination (&aff);
2821   return fold_convert (type, aff_combination_to_tree (&aff));
2822 }
2823
2824 /* Determines the expression by that USE is expressed from induction variable
2825    CAND in LOOP.  The computation is unshared.  */
2826
2827 static tree
2828 get_computation (struct loop *loop, struct iv_use *use, struct iv_cand *cand)
2829 {
2830   return get_computation_at (loop, use, cand, use->stmt);
2831 }
2832
2833 /* Returns cost of addition in MODE.  */
2834
2835 static unsigned
2836 add_cost (enum machine_mode mode)
2837 {
2838   static unsigned costs[NUM_MACHINE_MODES];
2839   rtx seq;
2840   unsigned cost;
2841
2842   if (costs[mode])
2843     return costs[mode];
2844
2845   start_sequence ();
2846   force_operand (gen_rtx_fmt_ee (PLUS, mode,
2847                                  gen_raw_REG (mode, LAST_VIRTUAL_REGISTER + 1),
2848                                  gen_raw_REG (mode, LAST_VIRTUAL_REGISTER + 2)),
2849                  NULL_RTX);
2850   seq = get_insns ();
2851   end_sequence ();
2852
2853   cost = seq_cost (seq);
2854   if (!cost)
2855     cost = 1;
2856
2857   costs[mode] = cost;
2858       
2859   if (dump_file && (dump_flags & TDF_DETAILS))
2860     fprintf (dump_file, "Addition in %s costs %d\n",
2861              GET_MODE_NAME (mode), cost);
2862   return cost;
2863 }
2864
2865 /* Entry in a hashtable of already known costs for multiplication.  */
2866 struct mbc_entry
2867 {
2868   HOST_WIDE_INT cst;            /* The constant to multiply by.  */
2869   enum machine_mode mode;       /* In mode.  */
2870   unsigned cost;                /* The cost.  */
2871 };
2872
2873 /* Counts hash value for the ENTRY.  */
2874
2875 static hashval_t
2876 mbc_entry_hash (const void *entry)
2877 {
2878   const struct mbc_entry *e = (const struct mbc_entry *) entry;
2879
2880   return 57 * (hashval_t) e->mode + (hashval_t) (e->cst % 877);
2881 }
2882
2883 /* Compares the hash table entries ENTRY1 and ENTRY2.  */
2884
2885 static int
2886 mbc_entry_eq (const void *entry1, const void *entry2)
2887 {
2888   const struct mbc_entry *e1 = (const struct mbc_entry *) entry1;
2889   const struct mbc_entry *e2 = (const struct mbc_entry *) entry2;
2890
2891   return (e1->mode == e2->mode
2892           && e1->cst == e2->cst);
2893 }
2894
2895 /* Returns cost of multiplication by constant CST in MODE.  */
2896
2897 unsigned
2898 multiply_by_cost (HOST_WIDE_INT cst, enum machine_mode mode)
2899 {
2900   static htab_t costs;
2901   struct mbc_entry **cached, act;
2902   rtx seq;
2903   unsigned cost;
2904
2905   if (!costs)
2906     costs = htab_create (100, mbc_entry_hash, mbc_entry_eq, free);
2907
2908   act.mode = mode;
2909   act.cst = cst;
2910   cached = (struct mbc_entry **) htab_find_slot (costs, &act, INSERT);
2911   if (*cached)
2912     return (*cached)->cost;
2913
2914   *cached = XNEW (struct mbc_entry);
2915   (*cached)->mode = mode;
2916   (*cached)->cst = cst;
2917
2918   start_sequence ();
2919   expand_mult (mode, gen_raw_REG (mode, LAST_VIRTUAL_REGISTER + 1),
2920                gen_int_mode (cst, mode), NULL_RTX, 0);
2921   seq = get_insns ();
2922   end_sequence ();
2923   
2924   cost = seq_cost (seq);
2925
2926   if (dump_file && (dump_flags & TDF_DETAILS))
2927     fprintf (dump_file, "Multiplication by %d in %s costs %d\n",
2928              (int) cst, GET_MODE_NAME (mode), cost);
2929
2930   (*cached)->cost = cost;
2931
2932   return cost;
2933 }
2934
2935 /* Returns true if multiplying by RATIO is allowed in an address.  Test the
2936    validity for a memory reference accessing memory of mode MODE.  */
2937
2938 bool
2939 multiplier_allowed_in_address_p (HOST_WIDE_INT ratio, enum machine_mode mode)
2940 {
2941 #define MAX_RATIO 128
2942   static sbitmap valid_mult[MAX_MACHINE_MODE];
2943   
2944   if (!valid_mult[mode])
2945     {
2946       rtx reg1 = gen_raw_REG (Pmode, LAST_VIRTUAL_REGISTER + 1);
2947       rtx addr;
2948       HOST_WIDE_INT i;
2949
2950       valid_mult[mode] = sbitmap_alloc (2 * MAX_RATIO + 1);
2951       sbitmap_zero (valid_mult[mode]);
2952       addr = gen_rtx_fmt_ee (MULT, Pmode, reg1, NULL_RTX);
2953       for (i = -MAX_RATIO; i <= MAX_RATIO; i++)
2954         {
2955           XEXP (addr, 1) = gen_int_mode (i, Pmode);
2956           if (memory_address_p (mode, addr))
2957             SET_BIT (valid_mult[mode], i + MAX_RATIO);
2958         }
2959
2960       if (dump_file && (dump_flags & TDF_DETAILS))
2961         {
2962           fprintf (dump_file, "  allowed multipliers:");
2963           for (i = -MAX_RATIO; i <= MAX_RATIO; i++)
2964             if (TEST_BIT (valid_mult[mode], i + MAX_RATIO))
2965               fprintf (dump_file, " %d", (int) i);
2966           fprintf (dump_file, "\n");
2967           fprintf (dump_file, "\n");
2968         }
2969     }
2970
2971   if (ratio > MAX_RATIO || ratio < -MAX_RATIO)
2972     return false;
2973
2974   return TEST_BIT (valid_mult[mode], ratio + MAX_RATIO);
2975 }
2976
2977 /* Returns cost of address in shape symbol + var + OFFSET + RATIO * index.
2978    If SYMBOL_PRESENT is false, symbol is omitted.  If VAR_PRESENT is false,
2979    variable is omitted.  Compute the cost for a memory reference that accesses
2980    a memory location of mode MEM_MODE.
2981
2982    TODO -- there must be some better way.  This all is quite crude.  */
2983
2984 static comp_cost
2985 get_address_cost (bool symbol_present, bool var_present,
2986                   unsigned HOST_WIDE_INT offset, HOST_WIDE_INT ratio,
2987                   enum machine_mode mem_mode)
2988 {
2989   static bool initialized[MAX_MACHINE_MODE];
2990   static HOST_WIDE_INT rat[MAX_MACHINE_MODE], off[MAX_MACHINE_MODE];
2991   static HOST_WIDE_INT min_offset[MAX_MACHINE_MODE], max_offset[MAX_MACHINE_MODE];
2992   static unsigned costs[MAX_MACHINE_MODE][2][2][2][2];
2993   unsigned cost, acost, complexity;
2994   bool offset_p, ratio_p;
2995   HOST_WIDE_INT s_offset;
2996   unsigned HOST_WIDE_INT mask;
2997   unsigned bits;
2998
2999   if (!initialized[mem_mode])
3000     {
3001       HOST_WIDE_INT i;
3002       HOST_WIDE_INT start = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
3003       int old_cse_not_expected;
3004       unsigned sym_p, var_p, off_p, rat_p, add_c;
3005       rtx seq, addr, base;
3006       rtx reg0, reg1;
3007
3008       initialized[mem_mode] = true;
3009
3010       reg1 = gen_raw_REG (Pmode, LAST_VIRTUAL_REGISTER + 1);
3011
3012       addr = gen_rtx_fmt_ee (PLUS, Pmode, reg1, NULL_RTX);
3013       for (i = start; i <= 1 << 20; i <<= 1)
3014         {
3015           XEXP (addr, 1) = gen_int_mode (i, Pmode);
3016           if (!memory_address_p (mem_mode, addr))
3017             break;
3018         }
3019       max_offset[mem_mode] = i == start ? 0 : i >> 1;
3020       off[mem_mode] = max_offset[mem_mode];
3021
3022       for (i = start; i <= 1 << 20; i <<= 1)
3023         {
3024           XEXP (addr, 1) = gen_int_mode (-i, Pmode);
3025           if (!memory_address_p (mem_mode, addr))
3026             break;
3027         }
3028       min_offset[mem_mode] = i == start ? 0 : -(i >> 1);
3029
3030       if (dump_file && (dump_flags & TDF_DETAILS))
3031         {
3032           fprintf (dump_file, "get_address_cost:\n");
3033           fprintf (dump_file, "  min offset %s %d\n",
3034                    GET_MODE_NAME (mem_mode),
3035                    (int) min_offset[mem_mode]);
3036           fprintf (dump_file, "  max offset %s %d\n",
3037                    GET_MODE_NAME (mem_mode),
3038                    (int) max_offset[mem_mode]);
3039         }
3040
3041       rat[mem_mode] = 1;
3042       for (i = 2; i <= MAX_RATIO; i++)
3043         if (multiplier_allowed_in_address_p (i, mem_mode))
3044           {
3045             rat[mem_mode] = i;
3046             break;
3047           }
3048
3049       /* Compute the cost of various addressing modes.  */
3050       acost = 0;
3051       reg0 = gen_raw_REG (Pmode, LAST_VIRTUAL_REGISTER + 1);
3052       reg1 = gen_raw_REG (Pmode, LAST_VIRTUAL_REGISTER + 2);
3053
3054       for (i = 0; i < 16; i++)
3055         {
3056           sym_p = i & 1;
3057           var_p = (i >> 1) & 1;
3058           off_p = (i >> 2) & 1;
3059           rat_p = (i >> 3) & 1;
3060
3061           addr = reg0;
3062           if (rat_p)
3063             addr = gen_rtx_fmt_ee (MULT, Pmode, addr,
3064                                    gen_int_mode (rat[mem_mode], Pmode));
3065
3066           if (var_p)
3067             addr = gen_rtx_fmt_ee (PLUS, Pmode, addr, reg1);
3068
3069           if (sym_p)
3070             {
3071               base = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (""));
3072               /* ??? We can run into trouble with some backends by presenting
3073                  it with symbols which haven't been properly passed through
3074                  targetm.encode_section_info.  By setting the local bit, we
3075                  enhance the probability of things working.  */
3076               SYMBOL_REF_FLAGS (base) = SYMBOL_FLAG_LOCAL;
3077
3078               if (off_p)
3079                 base = gen_rtx_fmt_e (CONST, Pmode,
3080                                       gen_rtx_fmt_ee (PLUS, Pmode,
3081                                                       base,
3082                                                       gen_int_mode (off[mem_mode],
3083                                                                     Pmode)));
3084             }
3085           else if (off_p)
3086             base = gen_int_mode (off[mem_mode], Pmode);
3087           else
3088             base = NULL_RTX;
3089     
3090           if (base)
3091             addr = gen_rtx_fmt_ee (PLUS, Pmode, addr, base);
3092   
3093           start_sequence ();
3094           /* To avoid splitting addressing modes, pretend that no cse will
3095              follow.  */
3096           old_cse_not_expected = cse_not_expected;
3097           cse_not_expected = true;
3098           addr = memory_address (mem_mode, addr);
3099           cse_not_expected = old_cse_not_expected;
3100           seq = get_insns ();
3101           end_sequence ();
3102
3103           acost = seq_cost (seq);
3104           acost += address_cost (addr, mem_mode);
3105
3106           if (!acost)
3107             acost = 1;
3108           costs[mem_mode][sym_p][var_p][off_p][rat_p] = acost;
3109         }
3110
3111       /* On some targets, it is quite expensive to load symbol to a register,
3112          which makes addresses that contain symbols look much more expensive.
3113          However, the symbol will have to be loaded in any case before the
3114          loop (and quite likely we have it in register already), so it does not
3115          make much sense to penalize them too heavily.  So make some final
3116          tweaks for the SYMBOL_PRESENT modes:
3117
3118          If VAR_PRESENT is false, and the mode obtained by changing symbol to
3119          var is cheaper, use this mode with small penalty.
3120          If VAR_PRESENT is true, try whether the mode with
3121          SYMBOL_PRESENT = false is cheaper even with cost of addition, and
3122          if this is the case, use it.  */
3123       add_c = add_cost (Pmode);
3124       for (i = 0; i < 8; i++)
3125         {
3126           var_p = i & 1;
3127           off_p = (i >> 1) & 1;
3128           rat_p = (i >> 2) & 1;
3129
3130           acost = costs[mem_mode][0][1][off_p][rat_p] + 1;
3131           if (var_p)
3132             acost += add_c;
3133
3134           if (acost < costs[mem_mode][1][var_p][off_p][rat_p])
3135             costs[mem_mode][1][var_p][off_p][rat_p] = acost;
3136         }
3137   
3138       if (dump_file && (dump_flags & TDF_DETAILS))
3139         {
3140           fprintf (dump_file, "Address costs:\n");
3141       
3142           for (i = 0; i < 16; i++)
3143             {
3144               sym_p = i & 1;
3145               var_p = (i >> 1) & 1;
3146               off_p = (i >> 2) & 1;
3147               rat_p = (i >> 3) & 1;
3148
3149               fprintf (dump_file, "  ");
3150               if (sym_p)
3151                 fprintf (dump_file, "sym + ");
3152               if (var_p)
3153                 fprintf (dump_file, "var + ");
3154               if (off_p)
3155                 fprintf (dump_file, "cst + ");
3156               if (rat_p)
3157                 fprintf (dump_file, "rat * ");
3158
3159               acost = costs[mem_mode][sym_p][var_p][off_p][rat_p];
3160               fprintf (dump_file, "index costs %d\n", acost);
3161             }
3162           fprintf (dump_file, "\n");
3163         }
3164     }
3165
3166   bits = GET_MODE_BITSIZE (Pmode);
3167   mask = ~(~(unsigned HOST_WIDE_INT) 0 << (bits - 1) << 1);
3168   offset &= mask;
3169   if ((offset >> (bits - 1) & 1))
3170     offset |= ~mask;
3171   s_offset = offset;
3172
3173   cost = 0;
3174   offset_p = (s_offset != 0
3175               && min_offset[mem_mode] <= s_offset
3176               && s_offset <= max_offset[mem_mode]);
3177   ratio_p = (ratio != 1
3178              && multiplier_allowed_in_address_p (ratio, mem_mode));
3179
3180   if (ratio != 1 && !ratio_p)
3181     cost += multiply_by_cost (ratio, Pmode);
3182
3183   if (s_offset && !offset_p && !symbol_present)
3184     cost += add_cost (Pmode);
3185
3186   acost = costs[mem_mode][symbol_present][var_present][offset_p][ratio_p];
3187   complexity = (symbol_present != 0) + (var_present != 0) + offset_p + ratio_p;
3188   return new_cost (cost + acost, complexity);
3189 }
3190
3191 /* Estimates cost of forcing expression EXPR into a variable.  */
3192
3193 static comp_cost
3194 force_expr_to_var_cost (tree expr)
3195 {
3196   static bool costs_initialized = false;
3197   static unsigned integer_cost;
3198   static unsigned symbol_cost;
3199   static unsigned address_cost;
3200   tree op0, op1;
3201   comp_cost cost0, cost1, cost;
3202   enum machine_mode mode;
3203
3204   if (!costs_initialized)
3205     {
3206       tree type = build_pointer_type (integer_type_node);
3207       tree var, addr;
3208       rtx x;
3209
3210       var = create_tmp_var_raw (integer_type_node, "test_var");
3211       TREE_STATIC (var) = 1;
3212       x = produce_memory_decl_rtl (var, NULL);
3213       SET_DECL_RTL (var, x);
3214
3215       integer_cost = computation_cost (build_int_cst (integer_type_node,
3216                                                       2000));
3217
3218       addr = build1 (ADDR_EXPR, type, var);
3219       symbol_cost = computation_cost (addr) + 1;
3220
3221       address_cost
3222         = computation_cost (build2 (POINTER_PLUS_EXPR, type,
3223                                     addr,
3224                                     build_int_cst (sizetype, 2000))) + 1;
3225       if (dump_file && (dump_flags & TDF_DETAILS))
3226         {
3227           fprintf (dump_file, "force_expr_to_var_cost:\n");
3228           fprintf (dump_file, "  integer %d\n", (int) integer_cost);
3229           fprintf (dump_file, "  symbol %d\n", (int) symbol_cost);
3230           fprintf (dump_file, "  address %d\n", (int) address_cost);
3231           fprintf (dump_file, "  other %d\n", (int) target_spill_cost);
3232           fprintf (dump_file, "\n");
3233         }
3234
3235       costs_initialized = true;
3236     }
3237
3238   STRIP_NOPS (expr);
3239
3240   if (SSA_VAR_P (expr))
3241     return zero_cost;
3242
3243   if (is_gimple_min_invariant (expr))
3244     {
3245       if (TREE_CODE (expr) == INTEGER_CST)
3246         return new_cost (integer_cost, 0);
3247
3248       if (TREE_CODE (expr) == ADDR_EXPR)
3249         {
3250           tree obj = TREE_OPERAND (expr, 0);
3251
3252           if (TREE_CODE (obj) == VAR_DECL
3253               || TREE_CODE (obj) == PARM_DECL
3254               || TREE_CODE (obj) == RESULT_DECL)
3255             return new_cost (symbol_cost, 0);
3256         }
3257
3258       return new_cost (address_cost, 0);
3259     }
3260
3261   switch (TREE_CODE (expr))
3262     {
3263     case POINTER_PLUS_EXPR:
3264     case PLUS_EXPR:
3265     case MINUS_EXPR:
3266     case MULT_EXPR:
3267       op0 = TREE_OPERAND (expr, 0);
3268       op1 = TREE_OPERAND (expr, 1);
3269       STRIP_NOPS (op0);
3270       STRIP_NOPS (op1);
3271
3272       if (is_gimple_val (op0))
3273         cost0 = zero_cost;
3274       else
3275         cost0 = force_expr_to_var_cost (op0);
3276
3277       if (is_gimple_val (op1))
3278         cost1 = zero_cost;
3279       else
3280         cost1 = force_expr_to_var_cost (op1);
3281
3282       break;
3283
3284     default:
3285       /* Just an arbitrary value, FIXME.  */
3286       return new_cost (target_spill_cost, 0);
3287     }
3288
3289   mode = TYPE_MODE (TREE_TYPE (expr));
3290   switch (TREE_CODE (expr))
3291     {
3292     case POINTER_PLUS_EXPR:
3293     case PLUS_EXPR:
3294     case MINUS_EXPR:
3295       cost = new_cost (add_cost (mode), 0);
3296       break;
3297
3298     case MULT_EXPR:
3299       if (cst_and_fits_in_hwi (op0))
3300         cost = new_cost (multiply_by_cost (int_cst_value (op0), mode), 0);
3301       else if (cst_and_fits_in_hwi (op1))
3302         cost = new_cost (multiply_by_cost (int_cst_value (op1), mode), 0);
3303       else
3304         return new_cost (target_spill_cost, 0);
3305       break;
3306
3307     default:
3308       gcc_unreachable ();
3309     }
3310
3311   cost = add_costs (cost, cost0);
3312   cost = add_costs (cost, cost1);
3313
3314   /* Bound the cost by target_spill_cost.  The parts of complicated
3315      computations often are either loop invariant or at least can
3316      be shared between several iv uses, so letting this grow without
3317      limits would not give reasonable results.  */
3318   if (cost.cost > target_spill_cost)
3319     cost.cost = target_spill_cost;
3320
3321   return cost;
3322 }
3323
3324 /* Estimates cost of forcing EXPR into a variable.  DEPENDS_ON is a set of the
3325    invariants the computation depends on.  */
3326
3327 static comp_cost
3328 force_var_cost (struct ivopts_data *data,
3329                 tree expr, bitmap *depends_on)
3330 {
3331   if (depends_on)
3332     {
3333       fd_ivopts_data = data;
3334       walk_tree (&expr, find_depends, depends_on, NULL);
3335     }
3336
3337   return force_expr_to_var_cost (expr);
3338 }
3339
3340 /* Estimates cost of expressing address ADDR  as var + symbol + offset.  The
3341    value of offset is added to OFFSET, SYMBOL_PRESENT and VAR_PRESENT are set
3342    to false if the corresponding part is missing.  DEPENDS_ON is a set of the
3343    invariants the computation depends on.  */
3344
3345 static comp_cost
3346 split_address_cost (struct ivopts_data *data,
3347                     tree addr, bool *symbol_present, bool *var_present,
3348                     unsigned HOST_WIDE_INT *offset, bitmap *depends_on)
3349 {
3350   tree core;
3351   HOST_WIDE_INT bitsize;
3352   HOST_WIDE_INT bitpos;
3353   tree toffset;
3354   enum machine_mode mode;
3355   int unsignedp, volatilep;
3356   
3357   core = get_inner_reference (addr, &bitsize, &bitpos, &toffset, &mode,
3358                               &unsignedp, &volatilep, false);
3359
3360   if (toffset != 0
3361       || bitpos % BITS_PER_UNIT != 0
3362       || TREE_CODE (core) != VAR_DECL)
3363     {
3364       *symbol_present = false;
3365       *var_present = true;
3366       fd_ivopts_data = data;
3367       walk_tree (&addr, find_depends, depends_on, NULL);
3368       return new_cost (target_spill_cost, 0);
3369     }
3370
3371   *offset += bitpos / BITS_PER_UNIT;
3372   if (TREE_STATIC (core)
3373       || DECL_EXTERNAL (core))
3374     {
3375       *symbol_present = true;
3376       *var_present = false;
3377       return zero_cost;
3378     }
3379       
3380   *symbol_present = false;
3381   *var_present = true;
3382   return zero_cost;
3383 }
3384
3385 /* Estimates cost of expressing difference of addresses E1 - E2 as
3386    var + symbol + offset.  The value of offset is added to OFFSET,
3387    SYMBOL_PRESENT and VAR_PRESENT are set to false if the corresponding
3388    part is missing.  DEPENDS_ON is a set of the invariants the computation
3389    depends on.  */
3390
3391 static comp_cost
3392 ptr_difference_cost (struct ivopts_data *data,
3393                      tree e1, tree e2, bool *symbol_present, bool *var_present,
3394                      unsigned HOST_WIDE_INT *offset, bitmap *depends_on)
3395 {
3396   HOST_WIDE_INT diff = 0;
3397   comp_cost cost;
3398
3399   gcc_assert (TREE_CODE (e1) == ADDR_EXPR);
3400
3401   if (ptr_difference_const (e1, e2, &diff))
3402     {
3403       *offset += diff;
3404       *symbol_present = false;
3405       *var_present = false;
3406       return zero_cost;
3407     }
3408
3409   if (integer_zerop (e2))
3410     return split_address_cost (data, TREE_OPERAND (e1, 0),
3411                                symbol_present, var_present, offset, depends_on);
3412
3413   *symbol_present = false;
3414   *var_present = true;
3415   
3416   cost = force_var_cost (data, e1, depends_on);
3417   cost = add_costs (cost, force_var_cost (data, e2, depends_on));
3418   cost.cost += add_cost (Pmode);
3419
3420   return cost;
3421 }
3422
3423 /* Estimates cost of expressing difference E1 - E2 as
3424    var + symbol + offset.  The value of offset is added to OFFSET,
3425    SYMBOL_PRESENT and VAR_PRESENT are set to false if the corresponding
3426    part is missing.  DEPENDS_ON is a set of the invariants the computation
3427    depends on.  */
3428
3429 static comp_cost
3430 difference_cost (struct ivopts_data *data,
3431                  tree e1, tree e2, bool *symbol_present, bool *var_present,
3432                  unsigned HOST_WIDE_INT *offset, bitmap *depends_on)
3433 {
3434   comp_cost cost;
3435   enum machine_mode mode = TYPE_MODE (TREE_TYPE (e1));
3436   unsigned HOST_WIDE_INT off1, off2;
3437
3438   e1 = strip_offset (e1, &off1);
3439   e2 = strip_offset (e2, &off2);
3440   *offset += off1 - off2;
3441
3442   STRIP_NOPS (e1);
3443   STRIP_NOPS (e2);
3444
3445   if (TREE_CODE (e1) == ADDR_EXPR)
3446     return ptr_difference_cost (data, e1, e2, symbol_present, var_present, offset,
3447                                 depends_on);
3448   *symbol_present = false;
3449
3450   if (operand_equal_p (e1, e2, 0))
3451     {
3452       *var_present = false;
3453       return zero_cost;
3454     }
3455   *var_present = true;
3456   if (integer_zerop (e2))
3457     return force_var_cost (data, e1, depends_on);
3458
3459   if (integer_zerop (e1))
3460     {
3461       cost = force_var_cost (data, e2, depends_on);
3462       cost.cost += multiply_by_cost (-1, mode);
3463
3464       return cost;
3465     }
3466
3467   cost = force_var_cost (data, e1, depends_on);
3468   cost = add_costs (cost, force_var_cost (data, e2, depends_on));
3469   cost.cost += add_cost (mode);
3470
3471   return cost;
3472 }
3473
3474 /* Determines the cost of the computation by that USE is expressed
3475    from induction variable CAND.  If ADDRESS_P is true, we just need
3476    to create an address from it, otherwise we want to get it into
3477    register.  A set of invariants we depend on is stored in
3478    DEPENDS_ON.  AT is the statement at that the value is computed.  */
3479
3480 static comp_cost
3481 get_computation_cost_at (struct ivopts_data *data,
3482                          struct iv_use *use, struct iv_cand *cand,
3483                          bool address_p, bitmap *depends_on, gimple at)
3484 {
3485   tree ubase = use->iv->base, ustep = use->iv->step;
3486   tree cbase, cstep;
3487   tree utype = TREE_TYPE (ubase), ctype;
3488   unsigned HOST_WIDE_INT cstepi, offset = 0;
3489   HOST_WIDE_INT ratio, aratio;
3490   bool var_present, symbol_present;
3491   comp_cost cost;
3492   unsigned n_sums;
3493   double_int rat;
3494
3495   *depends_on = NULL;
3496
3497   /* Only consider real candidates.  */
3498   if (!cand->iv)
3499     return infinite_cost;
3500
3501   cbase = cand->iv->base;
3502   cstep = cand->iv->step;
3503   ctype = TREE_TYPE (cbase);
3504
3505   if (TYPE_PRECISION (utype) > TYPE_PRECISION (ctype))
3506     {
3507       /* We do not have a precision to express the values of use.  */
3508       return infinite_cost;
3509     }
3510
3511   if (address_p)
3512     {
3513       /* Do not try to express address of an object with computation based
3514          on address of a different object.  This may cause problems in rtl
3515          level alias analysis (that does not expect this to be happening,
3516          as this is illegal in C), and would be unlikely to be useful
3517          anyway.  */
3518       if (use->iv->base_object
3519           && cand->iv->base_object
3520           && !operand_equal_p (use->iv->base_object, cand->iv->base_object, 0))
3521         return infinite_cost;
3522     }
3523
3524   if (TYPE_PRECISION (utype) != TYPE_PRECISION (ctype))
3525     {
3526       /* TODO -- add direct handling of this case.  */
3527       goto fallback;
3528     }
3529
3530   /* CSTEPI is removed from the offset in case statement is after the
3531      increment.  If the step is not constant, we use zero instead.
3532      This is a bit imprecise (there is the extra addition), but
3533      redundancy elimination is likely to transform the code so that
3534      it uses value of the variable before increment anyway,
3535      so it is not that much unrealistic.  */
3536   if (cst_and_fits_in_hwi (cstep))
3537     cstepi = int_cst_value (cstep);
3538   else
3539     cstepi = 0;
3540
3541   if (!constant_multiple_of (ustep, cstep, &rat))
3542     return infinite_cost;
3543     
3544   if (double_int_fits_in_shwi_p (rat))
3545     ratio = double_int_to_shwi (rat);
3546   else
3547     return infinite_cost;
3548
3549   /* use = ubase + ratio * (var - cbase).  If either cbase is a constant
3550      or ratio == 1, it is better to handle this like
3551      
3552      ubase - ratio * cbase + ratio * var
3553      
3554      (also holds in the case ratio == -1, TODO.  */
3555
3556   if (cst_and_fits_in_hwi (cbase))
3557     {
3558       offset = - ratio * int_cst_value (cbase); 
3559       cost = difference_cost (data,
3560                               ubase, build_int_cst (utype, 0),
3561                               &symbol_present, &var_present, &offset,
3562                               depends_on);
3563     }
3564   else if (ratio == 1)
3565     {
3566       cost = difference_cost (data,
3567                               ubase, cbase,
3568                               &symbol_present, &var_present, &offset,
3569                               depends_on);
3570     }
3571   else
3572     {
3573       cost = force_var_cost (data, cbase, depends_on);
3574       cost.cost += add_cost (TYPE_MODE (ctype));
3575       cost = add_costs (cost,
3576                         difference_cost (data,
3577                                          ubase, build_int_cst (utype, 0),
3578                                          &symbol_present, &var_present,
3579                                          &offset, depends_on));
3580     }
3581
3582   /* If we are after the increment, the value of the candidate is higher by
3583      one iteration.  */
3584   if (stmt_after_increment (data->current_loop, cand, at))
3585     offset -= ratio * cstepi;
3586
3587   /* Now the computation is in shape symbol + var1 + const + ratio * var2.
3588      (symbol/var/const parts may be omitted).  If we are looking for an address,
3589      find the cost of addressing this.  */
3590   if (address_p)
3591     return add_costs (cost, get_address_cost (symbol_present, var_present,
3592                                 offset, ratio,
3593                                 TYPE_MODE (TREE_TYPE (*use->op_p))));
3594
3595   /* Otherwise estimate the costs for computing the expression.  */
3596   aratio = ratio > 0 ? ratio : -ratio;
3597   if (!symbol_present && !var_present && !offset)
3598     {
3599       if (ratio != 1)
3600         cost.cost += multiply_by_cost (ratio, TYPE_MODE (ctype));
3601
3602       return cost;
3603     }
3604
3605   if (aratio != 1)
3606     cost.cost += multiply_by_cost (aratio, TYPE_MODE (ctype));
3607
3608   n_sums = 1;
3609   if (var_present
3610       /* Symbol + offset should be compile-time computable.  */
3611       && (symbol_present || offset))
3612     n_sums++;
3613
3614   /* Having offset does not affect runtime cost in case it is added to
3615      symbol, but it increases complexity.  */
3616   if (offset)
3617     cost.complexity++;
3618
3619   cost.cost += n_sums * add_cost (TYPE_MODE (ctype));
3620   return cost;
3621
3622 fallback:
3623   {
3624     /* Just get the expression, expand it and measure the cost.  */
3625     tree comp = get_computation_at (data->current_loop, use, cand, at);
3626
3627     if (!comp)
3628       return infinite_cost;
3629
3630     if (address_p)
3631       comp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (comp)), comp);
3632
3633     return new_cost (computation_cost (comp), 0);
3634   }
3635 }
3636
3637 /* Determines the cost of the computation by that USE is expressed
3638    from induction variable CAND.  If ADDRESS_P is true, we just need
3639    to create an address from it, otherwise we want to get it into
3640    register.  A set of invariants we depend on is stored in
3641    DEPENDS_ON.  */
3642
3643 static comp_cost
3644 get_computation_cost (struct ivopts_data *data,
3645                       struct iv_use *use, struct iv_cand *cand,
3646                       bool address_p, bitmap *depends_on)
3647 {
3648   return get_computation_cost_at (data,
3649                                   use, cand, address_p, depends_on, use->stmt);
3650 }
3651
3652 /* Determines cost of basing replacement of USE on CAND in a generic
3653    expression.  */
3654
3655 static bool
3656 determine_use_iv_cost_generic (struct ivopts_data *data,
3657                                struct iv_use *use, struct iv_cand *cand)
3658 {
3659   bitmap depends_on;
3660   comp_cost cost;
3661
3662   /* The simple case first -- if we need to express value of the preserved
3663      original biv, the cost is 0.  This also prevents us from counting the
3664      cost of increment twice -- once at this use and once in the cost of
3665      the candidate.  */
3666   if (cand->pos == IP_ORIGINAL
3667       && cand->incremented_at == use->stmt)
3668     {
3669       set_use_iv_cost (data, use, cand, zero_cost, NULL, NULL_TREE);
3670       return true;
3671     }
3672
3673   cost = get_computation_cost (data, use, cand, false, &depends_on);
3674   set_use_iv_cost (data, use, cand, cost, depends_on, NULL_TREE);
3675
3676   return !infinite_cost_p (cost);
3677 }
3678
3679 /* Determines cost of basing replacement of USE on CAND in an address.  */
3680
3681 static bool
3682 determine_use_iv_cost_address (struct ivopts_data *data,
3683                                struct iv_use *use, struct iv_cand *cand)
3684 {
3685   bitmap depends_on;
3686   comp_cost cost = get_computation_cost (data, use, cand, true, &depends_on);
3687
3688   set_use_iv_cost (data, use, cand, cost, depends_on, NULL_TREE);
3689
3690   return !infinite_cost_p (cost);
3691 }
3692
3693 /* Computes value of candidate CAND at position AT in iteration NITER, and
3694    stores it to VAL.  */
3695
3696 static void
3697 cand_value_at (struct loop *loop, struct iv_cand *cand, gimple at, tree niter,
3698                aff_tree *val)
3699 {
3700   aff_tree step, delta, nit;
3701   struct iv *iv = cand->iv;
3702   tree type = TREE_TYPE (iv->base);
3703   tree steptype = type;
3704   if (POINTER_TYPE_P (type))
3705     steptype = sizetype;
3706
3707   tree_to_aff_combination (iv->step, steptype, &step);
3708   tree_to_aff_combination (niter, TREE_TYPE (niter), &nit);
3709   aff_combination_convert (&nit, steptype);
3710   aff_combination_mult (&nit, &step, &delta);
3711   if (stmt_after_increment (loop, cand, at))
3712     aff_combination_add (&delta, &step);
3713
3714   tree_to_aff_combination (iv->base, type, val);
3715   aff_combination_add (val, &delta);
3716 }
3717
3718 /* Returns period of induction variable iv.  */
3719
3720 static tree
3721 iv_period (struct iv *iv)
3722 {
3723   tree step = iv->step, period, type;
3724   tree pow2div;
3725
3726   gcc_assert (step && TREE_CODE (step) == INTEGER_CST);
3727
3728   /* Period of the iv is gcd (step, type range).  Since type range is power
3729      of two, it suffices to determine the maximum power of two that divides
3730      step.  */
3731   pow2div = num_ending_zeros (step);
3732   type = unsigned_type_for (TREE_TYPE (step));
3733
3734   period = build_low_bits_mask (type,
3735                                 (TYPE_PRECISION (type)
3736                                  - tree_low_cst (pow2div, 1)));
3737
3738   return period;
3739 }
3740
3741 /* Returns the comparison operator used when eliminating the iv USE.  */
3742
3743 static enum tree_code
3744 iv_elimination_compare (struct ivopts_data *data, struct iv_use *use)
3745 {
3746   struct loop *loop = data->current_loop;
3747   basic_block ex_bb;
3748   edge exit;
3749
3750   ex_bb = gimple_bb (use->stmt);
3751   exit = EDGE_SUCC (ex_bb, 0);
3752   if (flow_bb_inside_loop_p (loop, exit->dest))
3753     exit = EDGE_SUCC (ex_bb, 1);
3754
3755   return (exit->flags & EDGE_TRUE_VALUE ? EQ_EXPR : NE_EXPR);
3756 }
3757
3758 /* Check whether it is possible to express the condition in USE by comparison
3759    of candidate CAND.  If so, store the value compared with to BOUND.  */
3760
3761 static bool
3762 may_eliminate_iv (struct ivopts_data *data,
3763                   struct iv_use *use, struct iv_cand *cand, tree *bound)
3764 {
3765   basic_block ex_bb;
3766   edge exit;
3767   tree nit, period;
3768   struct loop *loop = data->current_loop;
3769   aff_tree bnd;
3770
3771   if (TREE_CODE (cand->iv->step) != INTEGER_CST)
3772     return false;
3773
3774   /* For now works only for exits that dominate the loop latch.
3775      TODO: extend to other conditions inside loop body.  */
3776   ex_bb = gimple_bb (use->stmt);
3777   if (use->stmt != last_stmt (ex_bb)
3778       || gimple_code (use->stmt) != GIMPLE_COND
3779       || !dominated_by_p (CDI_DOMINATORS, loop->latch, ex_bb))
3780     return false;
3781
3782   exit = EDGE_SUCC (ex_bb, 0);
3783   if (flow_bb_inside_loop_p (loop, exit->dest))
3784     exit = EDGE_SUCC (ex_bb, 1);
3785   if (flow_bb_inside_loop_p (loop, exit->dest))
3786     return false;
3787
3788   nit = niter_for_exit (data, exit);
3789   if (!nit)
3790     return false;
3791
3792   /* Determine whether we can use the variable to test the exit condition.
3793      This is the case iff the period of the induction variable is greater
3794      than the number of iterations for which the exit condition is true.  */
3795   period = iv_period (cand->iv);
3796
3797   /* If the number of iterations is constant, compare against it directly.  */
3798   if (TREE_CODE (nit) == INTEGER_CST)
3799     {
3800       if (!tree_int_cst_lt (nit, period))
3801         return false;
3802     }
3803
3804   /* If not, and if this is the only possible exit of the loop, see whether
3805      we can get a conservative estimate on the number of iterations of the
3806      entire loop and compare against that instead.  */
3807   else if (loop_only_exit_p (loop, exit))
3808     {
3809       double_int period_value, max_niter;
3810       if (!estimated_loop_iterations (loop, true, &max_niter))
3811         return false;
3812       period_value = tree_to_double_int (period);
3813       if (double_int_ucmp (max_niter, period_value) >= 0)
3814         return false;
3815     }
3816
3817   /* Otherwise, punt.  */
3818   else
3819     return false;
3820
3821   cand_value_at (loop, cand, use->stmt, nit, &bnd);
3822   *bound = aff_combination_to_tree (&bnd);
3823   return true;
3824 }
3825
3826 /* Determines cost of basing replacement of USE on CAND in a condition.  */
3827
3828 static bool
3829 determine_use_iv_cost_condition (struct ivopts_data *data,
3830                                  struct iv_use *use, struct iv_cand *cand)
3831 {
3832   tree bound = NULL_TREE;
3833   struct iv *cmp_iv;
3834   bitmap depends_on_elim = NULL, depends_on_express = NULL, depends_on;
3835   comp_cost elim_cost, express_cost, cost;
3836   bool ok;
3837
3838   /* Only consider real candidates.  */
3839   if (!cand->iv)
3840     {
3841       set_use_iv_cost (data, use, cand, infinite_cost, NULL, NULL_TREE);
3842       return false;
3843     }
3844
3845   /* Try iv elimination.  */
3846   if (may_eliminate_iv (data, use, cand, &bound))
3847     {
3848       elim_cost = force_var_cost (data, bound, &depends_on_elim);
3849       /* The bound is a loop invariant, so it will be only computed
3850          once.  */
3851       elim_cost.cost /= AVG_LOOP_NITER (data->current_loop);
3852     }
3853   else
3854     elim_cost = infinite_cost;
3855
3856   /* Try expressing the original giv.  If it is compared with an invariant,
3857      note that we cannot get rid of it.  */
3858   ok = extract_cond_operands (data, use->stmt, NULL, NULL, NULL, &cmp_iv);
3859   gcc_assert (ok);
3860
3861   express_cost = get_computation_cost (data, use, cand, false,
3862                                        &depends_on_express);
3863   fd_ivopts_data = data;
3864   walk_tree (&cmp_iv->base, find_depends, &depends_on_express, NULL);
3865
3866   /* Choose the better approach.  */
3867   if (compare_costs (elim_cost, express_cost) < 0)
3868     {
3869       cost = elim_cost;
3870       depends_on = depends_on_elim;
3871       depends_on_elim = NULL;
3872     }
3873   else
3874     {
3875       cost = express_cost;
3876       depends_on = depends_on_express;
3877       depends_on_express = NULL;
3878       bound = NULL_TREE;
3879     }
3880
3881   set_use_iv_cost (data, use, cand, cost, depends_on, bound);
3882
3883   if (depends_on_elim)
3884     BITMAP_FREE (depends_on_elim);
3885   if (depends_on_express)
3886     BITMAP_FREE (depends_on_express);
3887
3888   return !infinite_cost_p (cost);
3889 }
3890
3891 /* Determines cost of basing replacement of USE on CAND.  Returns false
3892    if USE cannot be based on CAND.  */
3893
3894 static bool
3895 determine_use_iv_cost (struct ivopts_data *data,
3896                        struct iv_use *use, struct iv_cand *cand)
3897 {
3898   switch (use->type)
3899     {
3900     case USE_NONLINEAR_EXPR:
3901       return determine_use_iv_cost_generic (data, use, cand);
3902
3903     case USE_ADDRESS:
3904       return determine_use_iv_cost_address (data, use, cand);
3905
3906     case USE_COMPARE:
3907       return determine_use_iv_cost_condition (data, use, cand);
3908
3909     default:
3910       gcc_unreachable ();
3911     }
3912 }
3913
3914 /* Determines costs of basing the use of the iv on an iv candidate.  */
3915
3916 static void
3917 determine_use_iv_costs (struct ivopts_data *data)
3918 {
3919   unsigned i, j;
3920   struct iv_use *use;
3921   struct iv_cand *cand;
3922   bitmap to_clear = BITMAP_ALLOC (NULL);
3923
3924   alloc_use_cost_map (data);
3925
3926   for (i = 0; i < n_iv_uses (data); i++)
3927     {
3928       use = iv_use (data, i);
3929
3930       if (data->consider_all_candidates)
3931         {
3932           for (j = 0; j < n_iv_cands (data); j++)
3933             {
3934               cand = iv_cand (data, j);
3935               determine_use_iv_cost (data, use, cand);
3936             }
3937         }
3938       else
3939         {
3940           bitmap_iterator bi;
3941
3942           EXECUTE_IF_SET_IN_BITMAP (use->related_cands, 0, j, bi)
3943             {
3944               cand = iv_cand (data, j);
3945               if (!determine_use_iv_cost (data, use, cand))
3946                 bitmap_set_bit (to_clear, j);
3947             }
3948
3949           /* Remove the candidates for that the cost is infinite from
3950              the list of related candidates.  */
3951           bitmap_and_compl_into (use->related_cands, to_clear);
3952           bitmap_clear (to_clear);
3953         }
3954     }
3955
3956   BITMAP_FREE (to_clear);
3957
3958   if (dump_file && (dump_flags & TDF_DETAILS))
3959     {
3960       fprintf (dump_file, "Use-candidate costs:\n");
3961
3962       for (i = 0; i < n_iv_uses (data); i++)
3963         {
3964           use = iv_use (data, i);
3965
3966           fprintf (dump_file, "Use %d:\n", i);
3967           fprintf (dump_file, "  cand\tcost\tcompl.\tdepends on\n");
3968           for (j = 0; j < use->n_map_members; j++)
3969             {
3970               if (!use->cost_map[j].cand
3971                   || infinite_cost_p (use->cost_map[j].cost))
3972                 continue;
3973
3974               fprintf (dump_file, "  %d\t%d\t%d\t",
3975                        use->cost_map[j].cand->id,
3976                        use->cost_map[j].cost.cost,
3977                        use->cost_map[j].cost.complexity);
3978               if (use->cost_map[j].depends_on)
3979                 bitmap_print (dump_file,
3980                               use->cost_map[j].depends_on, "","");
3981               fprintf (dump_file, "\n");
3982             }
3983
3984           fprintf (dump_file, "\n");
3985         }
3986       fprintf (dump_file, "\n");
3987     }
3988 }
3989
3990 /* Determines cost of the candidate CAND.  */
3991
3992 static void
3993 determine_iv_cost (struct ivopts_data *data, struct iv_cand *cand)
3994 {
3995   comp_cost cost_base;
3996   unsigned cost, cost_step;
3997   tree base;
3998
3999   if (!cand->iv)
4000     {
4001       cand->cost = 0;
4002       return;
4003     }
4004
4005   /* There are two costs associated with the candidate -- its increment
4006      and its initialization.  The second is almost negligible for any loop
4007      that rolls enough, so we take it just very little into account.  */
4008
4009   base = cand->iv->base;
4010   cost_base = force_var_cost (data, base, NULL);
4011   cost_step = add_cost (TYPE_MODE (TREE_TYPE (base)));
4012
4013   cost = cost_step + cost_base.cost / AVG_LOOP_NITER (current_loop);
4014
4015   /* Prefer the original ivs unless we may gain something by replacing it.
4016      The reason is to make debugging simpler; so this is not relevant for
4017      artificial ivs created by other optimization passes.  */
4018   if (cand->pos != IP_ORIGINAL
4019       || DECL_ARTIFICIAL (SSA_NAME_VAR (cand->var_before)))
4020     cost++;
4021   
4022   /* Prefer not to insert statements into latch unless there are some
4023      already (so that we do not create unnecessary jumps).  */
4024   if (cand->pos == IP_END
4025       && empty_block_p (ip_end_pos (data->current_loop)))
4026     cost++;
4027
4028   cand->cost = cost;
4029 }
4030
4031 /* Determines costs of computation of the candidates.  */
4032
4033 static void
4034 determine_iv_costs (struct ivopts_data *data)
4035 {
4036   unsigned i;
4037
4038   if (dump_file && (dump_flags & TDF_DETAILS))
4039     {
4040       fprintf (dump_file, "Candidate costs:\n");
4041       fprintf (dump_file, "  cand\tcost\n");
4042     }
4043
4044   for (i = 0; i < n_iv_cands (data); i++)
4045     {
4046       struct iv_cand *cand = iv_cand (data, i);
4047
4048       determine_iv_cost (data, cand);
4049
4050       if (dump_file && (dump_flags & TDF_DETAILS))
4051         fprintf (dump_file, "  %d\t%d\n", i, cand->cost);
4052     }
4053   
4054   if (dump_file && (dump_flags & TDF_DETAILS))
4055     fprintf (dump_file, "\n");
4056 }
4057
4058 /* Calculates cost for having SIZE induction variables.  */
4059
4060 static unsigned
4061 ivopts_global_cost_for_size (struct ivopts_data *data, unsigned size)
4062 {
4063   /* We add size to the cost, so that we prefer eliminating ivs
4064      if possible.  */
4065   return size + estimate_reg_pressure_cost (size, data->regs_used);
4066 }
4067
4068 /* For each size of the induction variable set determine the penalty.  */
4069
4070 static void
4071 determine_set_costs (struct ivopts_data *data)
4072 {
4073   unsigned j, n;
4074   gimple phi;
4075   gimple_stmt_iterator psi;
4076   tree op;
4077   struct loop *loop = data->current_loop;
4078   bitmap_iterator bi;
4079
4080   /* We use the following model (definitely improvable, especially the
4081      cost function -- TODO):
4082
4083      We estimate the number of registers available (using MD data), name it A.
4084
4085      We estimate the number of registers used by the loop, name it U.  This
4086      number is obtained as the number of loop phi nodes (not counting virtual
4087      registers and bivs) + the number of variables from outside of the loop.
4088
4089      We set a reserve R (free regs that are used for temporary computations,
4090      etc.).  For now the reserve is a constant 3.
4091
4092      Let I be the number of induction variables.
4093      
4094      -- if U + I + R <= A, the cost is I * SMALL_COST (just not to encourage
4095         make a lot of ivs without a reason).
4096      -- if A - R < U + I <= A, the cost is I * PRES_COST
4097      -- if U + I > A, the cost is I * PRES_COST and
4098         number of uses * SPILL_COST * (U + I - A) / (U + I) is added.  */
4099
4100   if (dump_file && (dump_flags & TDF_DETAILS))
4101     {
4102       fprintf (dump_file, "Global costs:\n");
4103       fprintf (dump_file, "  target_avail_regs %d\n", target_avail_regs);
4104       fprintf (dump_file, "  target_reg_cost %d\n", target_reg_cost);
4105       fprintf (dump_file, "  target_spill_cost %d\n", target_spill_cost);
4106     }
4107
4108   n = 0;
4109   for (psi = gsi_start_phis (loop->header); !gsi_end_p (psi); gsi_next (&psi))
4110     {
4111       phi = gsi_stmt (psi);
4112       op = PHI_RESULT (phi);
4113
4114       if (!is_gimple_reg (op))
4115         continue;
4116
4117       if (get_iv (data, op))
4118         continue;
4119
4120       n++;
4121     }
4122
4123   EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, j, bi)
4124     {
4125       struct version_info *info = ver_info (data, j);
4126
4127       if (info->inv_id && info->has_nonlin_use)
4128         n++;
4129     }
4130
4131   data->regs_used = n;
4132   if (dump_file && (dump_flags & TDF_DETAILS))
4133     fprintf (dump_file, "  regs_used %d\n", n);
4134
4135   if (dump_file && (dump_flags & TDF_DETAILS))
4136     {
4137       fprintf (dump_file, "  cost for size:\n");
4138       fprintf (dump_file, "  ivs\tcost\n");
4139       for (j = 0; j <= 2 * target_avail_regs; j++)
4140         fprintf (dump_file, "  %d\t%d\n", j,
4141                  ivopts_global_cost_for_size (data, j));
4142       fprintf (dump_file, "\n");
4143     }
4144 }
4145
4146 /* Returns true if A is a cheaper cost pair than B.  */
4147
4148 static bool
4149 cheaper_cost_pair (struct cost_pair *a, struct cost_pair *b)
4150 {
4151   int cmp;
4152
4153   if (!a)
4154     return false;
4155
4156   if (!b)
4157     return true;
4158
4159   cmp = compare_costs (a->cost, b->cost);
4160   if (cmp < 0)
4161     return true;
4162
4163   if (cmp > 0)
4164     return false;
4165
4166   /* In case the costs are the same, prefer the cheaper candidate.  */
4167   if (a->cand->cost < b->cand->cost)
4168     return true;
4169
4170   return false;
4171 }
4172
4173 /* Computes the cost field of IVS structure.  */
4174
4175 static void
4176 iv_ca_recount_cost (struct ivopts_data *data, struct iv_ca *ivs)
4177 {
4178   comp_cost cost = ivs->cand_use_cost;
4179   cost.cost += ivs->cand_cost;
4180   cost.cost += ivopts_global_cost_for_size (data, ivs->n_regs);
4181
4182   ivs->cost = cost;
4183 }
4184
4185 /* Remove invariants in set INVS to set IVS.  */
4186
4187 static void
4188 iv_ca_set_remove_invariants (struct iv_ca *ivs, bitmap invs)
4189 {
4190   bitmap_iterator bi;
4191   unsigned iid;
4192
4193   if (!invs)
4194     return;
4195
4196   EXECUTE_IF_SET_IN_BITMAP (invs, 0, iid, bi)
4197     {
4198       ivs->n_invariant_uses[iid]--;
4199       if (ivs->n_invariant_uses[iid] == 0)
4200         ivs->n_regs--;
4201     }
4202 }
4203
4204 /* Set USE not to be expressed by any candidate in IVS.  */
4205
4206 static void
4207 iv_ca_set_no_cp (struct ivopts_data *data, struct iv_ca *ivs,
4208                  struct iv_use *use)
4209 {
4210   unsigned uid = use->id, cid;
4211   struct cost_pair *cp;
4212
4213   cp = ivs->cand_for_use[uid];
4214   if (!cp)
4215     return;
4216   cid = cp->cand->id;
4217
4218   ivs->bad_uses++;
4219   ivs->cand_for_use[uid] = NULL;
4220   ivs->n_cand_uses[cid]--;
4221
4222   if (ivs->n_cand_uses[cid] == 0)
4223     {
4224       bitmap_clear_bit (ivs->cands, cid);
4225       /* Do not count the pseudocandidates.  */
4226       if (cp->cand->iv)
4227         ivs->n_regs--;
4228       ivs->n_cands--;
4229       ivs->cand_cost -= cp->cand->cost;
4230
4231       iv_ca_set_remove_invariants (ivs, cp->cand->depends_on);
4232     }
4233
4234   ivs->cand_use_cost = sub_costs (ivs->cand_use_cost, cp->cost);
4235
4236   iv_ca_set_remove_invariants (ivs, cp->depends_on);
4237   iv_ca_recount_cost (data, ivs);
4238 }
4239
4240 /* Add invariants in set INVS to set IVS.  */
4241
4242 static void
4243 iv_ca_set_add_invariants (struct iv_ca *ivs, bitmap invs)
4244 {
4245   bitmap_iterator bi;
4246   unsigned iid;
4247
4248   if (!invs)
4249     return;
4250
4251   EXECUTE_IF_SET_IN_BITMAP (invs, 0, iid, bi)
4252     {
4253       ivs->n_invariant_uses[iid]++;
4254       if (ivs->n_invariant_uses[iid] == 1)
4255         ivs->n_regs++;
4256     }
4257 }
4258
4259 /* Set cost pair for USE in set IVS to CP.  */
4260
4261 static void
4262 iv_ca_set_cp (struct ivopts_data *data, struct iv_ca *ivs,
4263               struct iv_use *use, struct cost_pair *cp)
4264 {
4265   unsigned uid = use->id, cid;
4266
4267   if (ivs->cand_for_use[uid] == cp)
4268     return;
4269
4270   if (ivs->cand_for_use[uid])
4271     iv_ca_set_no_cp (data, ivs, use);
4272
4273   if (cp)
4274     {
4275       cid = cp->cand->id;
4276
4277       ivs->bad_uses--;
4278       ivs->cand_for_use[uid] = cp;
4279       ivs->n_cand_uses[cid]++;
4280       if (ivs->n_cand_uses[cid] == 1)
4281         {
4282           bitmap_set_bit (ivs->cands, cid);
4283           /* Do not count the pseudocandidates.  */
4284           if (cp->cand->iv)
4285             ivs->n_regs++;
4286           ivs->n_cands++;
4287           ivs->cand_cost += cp->cand->cost;
4288
4289           iv_ca_set_add_invariants (ivs, cp->cand->depends_on);
4290         }
4291
4292       ivs->cand_use_cost = add_costs (ivs->cand_use_cost, cp->cost);
4293       iv_ca_set_add_invariants (ivs, cp->depends_on);
4294       iv_ca_recount_cost (data, ivs);
4295     }
4296 }
4297
4298 /* Extend set IVS by expressing USE by some of the candidates in it
4299    if possible.  */
4300
4301 static void
4302 iv_ca_add_use (struct ivopts_data *data, struct iv_ca *ivs,
4303                struct iv_use *use)
4304 {
4305   struct cost_pair *best_cp = NULL, *cp;
4306   bitmap_iterator bi;
4307   unsigned i;
4308
4309   gcc_assert (ivs->upto >= use->id);
4310
4311   if (ivs->upto == use->id)
4312     {
4313       ivs->upto++;
4314       ivs->bad_uses++;
4315     }
4316
4317   EXECUTE_IF_SET_IN_BITMAP (ivs->cands, 0, i, bi)
4318     {
4319       cp = get_use_iv_cost (data, use, iv_cand (data, i));
4320
4321       if (cheaper_cost_pair (cp, best_cp))
4322         best_cp = cp;
4323     }
4324
4325   iv_ca_set_cp (data, ivs, use, best_cp);
4326 }
4327
4328 /* Get cost for assignment IVS.  */
4329
4330 static comp_cost
4331 iv_ca_cost (struct iv_ca *ivs)
4332 {
4333   return (ivs->bad_uses ? infinite_cost : ivs->cost);
4334 }
4335
4336 /* Returns true if all dependences of CP are among invariants in IVS.  */
4337
4338 static bool
4339 iv_ca_has_deps (struct iv_ca *ivs, struct cost_pair *cp)
4340 {
4341   unsigned i;
4342   bitmap_iterator bi;
4343
4344   if (!cp->depends_on)
4345     return true;
4346
4347   EXECUTE_IF_SET_IN_BITMAP (cp->depends_on, 0, i, bi)
4348     {
4349       if (ivs->n_invariant_uses[i] == 0)
4350         return false;
4351     }
4352
4353   return true;
4354 }
4355
4356 /* Creates change of expressing USE by NEW_CP instead of OLD_CP and chains
4357    it before NEXT_CHANGE.  */
4358
4359 static struct iv_ca_delta *
4360 iv_ca_delta_add (struct iv_use *use, struct cost_pair *old_cp,
4361                  struct cost_pair *new_cp, struct iv_ca_delta *next_change)
4362 {
4363   struct iv_ca_delta *change = XNEW (struct iv_ca_delta);
4364
4365   change->use = use;
4366   change->old_cp = old_cp;
4367   change->new_cp = new_cp;
4368   change->next_change = next_change;
4369
4370   return change;
4371 }
4372
4373 /* Joins two lists of changes L1 and L2.  Destructive -- old lists
4374    are rewritten.  */
4375
4376 static struct iv_ca_delta *
4377 iv_ca_delta_join (struct iv_ca_delta *l1, struct iv_ca_delta *l2)
4378 {
4379   struct iv_ca_delta *last;
4380
4381   if (!l2)
4382     return l1;
4383
4384   if (!l1)
4385     return l2;
4386
4387   for (last = l1; last->next_change; last = last->next_change)
4388     continue;
4389   last->next_change = l2;
4390
4391   return l1;
4392 }
4393
4394 /* Returns candidate by that USE is expressed in IVS.  */
4395
4396 static struct cost_pair *
4397 iv_ca_cand_for_use (struct iv_ca *ivs, struct iv_use *use)
4398 {
4399   return ivs->cand_for_use[use->id];
4400 }
4401
4402 /* Reverse the list of changes DELTA, forming the inverse to it.  */
4403
4404 static struct iv_ca_delta *
4405 iv_ca_delta_reverse (struct iv_ca_delta *delta)
4406 {
4407   struct iv_ca_delta *act, *next, *prev = NULL;
4408   struct cost_pair *tmp;
4409
4410   for (act = delta; act; act = next)
4411     {
4412       next = act->next_change;
4413       act->next_change = prev;
4414       prev = act;
4415
4416       tmp = act->old_cp;
4417       act->old_cp = act->new_cp;
4418       act->new_cp = tmp;
4419     }
4420
4421   return prev;
4422 }
4423
4424 /* Commit changes in DELTA to IVS.  If FORWARD is false, the changes are
4425    reverted instead.  */
4426
4427 static void
4428 iv_ca_delta_commit (struct ivopts_data *data, struct iv_ca *ivs,
4429                     struct iv_ca_delta *delta, bool forward)
4430 {
4431   struct cost_pair *from, *to;
4432   struct iv_ca_delta *act;
4433
4434   if (!forward)
4435     delta = iv_ca_delta_reverse (delta);
4436
4437   for (act = delta; act; act = act->next_change)
4438     {
4439       from = act->old_cp;
4440       to = act->new_cp;
4441       gcc_assert (iv_ca_cand_for_use (ivs, act->use) == from);
4442       iv_ca_set_cp (data, ivs, act->use, to);
4443     }
4444
4445   if (!forward)
4446     iv_ca_delta_reverse (delta);
4447 }
4448
4449 /* Returns true if CAND is used in IVS.  */
4450
4451 static bool
4452 iv_ca_cand_used_p (struct iv_ca *ivs, struct iv_cand *cand)
4453 {
4454   return ivs->n_cand_uses[cand->id] > 0;
4455 }
4456
4457 /* Returns number of induction variable candidates in the set IVS.  */
4458
4459 static unsigned
4460 iv_ca_n_cands (struct iv_ca *ivs)
4461 {
4462   return ivs->n_cands;
4463 }
4464
4465 /* Free the list of changes DELTA.  */
4466
4467 static void
4468 iv_ca_delta_free (struct iv_ca_delta **delta)
4469 {
4470   struct iv_ca_delta *act, *next;
4471
4472   for (act = *delta; act; act = next)
4473     {
4474       next = act->next_change;
4475       free (act);
4476     }
4477
4478   *delta = NULL;
4479 }
4480
4481 /* Allocates new iv candidates assignment.  */
4482
4483 static struct iv_ca *
4484 iv_ca_new (struct ivopts_data *data)
4485 {
4486   struct iv_ca *nw = XNEW (struct iv_ca);
4487
4488   nw->upto = 0;
4489   nw->bad_uses = 0;
4490   nw->cand_for_use = XCNEWVEC (struct cost_pair *, n_iv_uses (data));
4491   nw->n_cand_uses = XCNEWVEC (unsigned, n_iv_cands (data));
4492   nw->cands = BITMAP_ALLOC (NULL);
4493   nw->n_cands = 0;
4494   nw->n_regs = 0;
4495   nw->cand_use_cost = zero_cost;
4496   nw->cand_cost = 0;
4497   nw->n_invariant_uses = XCNEWVEC (unsigned, data->max_inv_id + 1);
4498   nw->cost = zero_cost;
4499
4500   return nw;
4501 }
4502
4503 /* Free memory occupied by the set IVS.  */
4504
4505 static void
4506 iv_ca_free (struct iv_ca **ivs)
4507 {
4508   free ((*ivs)->cand_for_use);
4509   free ((*ivs)->n_cand_uses);
4510   BITMAP_FREE ((*ivs)->cands);
4511   free ((*ivs)->n_invariant_uses);
4512   free (*ivs);
4513   *ivs = NULL;
4514 }
4515
4516 /* Dumps IVS to FILE.  */
4517
4518 static void
4519 iv_ca_dump (struct ivopts_data *data, FILE *file, struct iv_ca *ivs)
4520 {
4521   const char *pref = "  invariants ";
4522   unsigned i;
4523   comp_cost cost = iv_ca_cost (ivs);
4524
4525   fprintf (file, "  cost %d (complexity %d)\n", cost.cost, cost.complexity);
4526   bitmap_print (file, ivs->cands, "  candidates ","\n");
4527
4528   for (i = 1; i <= data->max_inv_id; i++)
4529     if (ivs->n_invariant_uses[i])
4530       {
4531         fprintf (file, "%s%d", pref, i);
4532         pref = ", ";
4533       }
4534   fprintf (file, "\n");
4535 }
4536
4537 /* Try changing candidate in IVS to CAND for each use.  Return cost of the
4538    new set, and store differences in DELTA.  Number of induction variables
4539    in the new set is stored to N_IVS.  */
4540
4541 static comp_cost
4542 iv_ca_extend (struct ivopts_data *data, struct iv_ca *ivs,
4543               struct iv_cand *cand, struct iv_ca_delta **delta,
4544               unsigned *n_ivs)
4545 {
4546   unsigned i;
4547   comp_cost cost;
4548   struct iv_use *use;
4549   struct cost_pair *old_cp, *new_cp;
4550
4551   *delta = NULL;
4552   for (i = 0; i < ivs->upto; i++)
4553     {
4554       use = iv_use (data, i);
4555       old_cp = iv_ca_cand_for_use (ivs, use);
4556
4557       if (old_cp
4558           && old_cp->cand == cand)
4559         continue;
4560
4561       new_cp = get_use_iv_cost (data, use, cand);
4562       if (!new_cp)
4563         continue;
4564
4565       if (!iv_ca_has_deps (ivs, new_cp))
4566         continue;
4567       
4568       if (!cheaper_cost_pair (new_cp, old_cp))
4569         continue;
4570
4571       *delta = iv_ca_delta_add (use, old_cp, new_cp, *delta);
4572     }
4573
4574   iv_ca_delta_commit (data, ivs, *delta, true);
4575   cost = iv_ca_cost (ivs);
4576   if (n_ivs)
4577     *n_ivs = iv_ca_n_cands (ivs);
4578   iv_ca_delta_commit (data, ivs, *delta, false);
4579
4580   return cost;
4581 }
4582
4583 /* Try narrowing set IVS by removing CAND.  Return the cost of
4584    the new set and store the differences in DELTA.  */
4585
4586 static comp_cost
4587 iv_ca_narrow (struct ivopts_data *data, struct iv_ca *ivs,
4588               struct iv_cand *cand, struct iv_ca_delta **delta)
4589 {
4590   unsigned i, ci;
4591   struct iv_use *use;
4592   struct cost_pair *old_cp, *new_cp, *cp;
4593   bitmap_iterator bi;
4594   struct iv_cand *cnd;
4595   comp_cost cost;
4596
4597   *delta = NULL;
4598   for (i = 0; i < n_iv_uses (data); i++)
4599     {
4600       use = iv_use (data, i);
4601
4602       old_cp = iv_ca_cand_for_use (ivs, use);
4603       if (old_cp->cand != cand)
4604         continue;
4605
4606       new_cp = NULL;
4607
4608       if (data->consider_all_candidates)
4609         {
4610           EXECUTE_IF_SET_IN_BITMAP (ivs->cands, 0, ci, bi)
4611             {
4612               if (ci == cand->id)
4613                 continue;
4614
4615               cnd = iv_cand (data, ci);
4616
4617               cp = get_use_iv_cost (data, use, cnd);
4618               if (!cp)
4619                 continue;
4620               if (!iv_ca_has_deps (ivs, cp))
4621                 continue;
4622       
4623               if (!cheaper_cost_pair (cp, new_cp))
4624                 continue;
4625
4626               new_cp = cp;
4627             }
4628         }
4629       else
4630         {
4631           EXECUTE_IF_AND_IN_BITMAP (use->related_cands, ivs->cands, 0, ci, bi)
4632             {
4633               if (ci == cand->id)
4634                 continue;
4635
4636               cnd = iv_cand (data, ci);
4637
4638               cp = get_use_iv_cost (data, use, cnd);
4639               if (!cp)
4640                 continue;
4641               if (!iv_ca_has_deps (ivs, cp))
4642                 continue;
4643       
4644               if (!cheaper_cost_pair (cp, new_cp))
4645                 continue;
4646
4647               new_cp = cp;
4648             }
4649         }
4650
4651       if (!new_cp)
4652         {
4653           iv_ca_delta_free (delta);
4654           return infinite_cost;
4655         }
4656
4657       *delta = iv_ca_delta_add (use, old_cp, new_cp, *delta);
4658     }
4659
4660   iv_ca_delta_commit (data, ivs, *delta, true);
4661   cost = iv_ca_cost (ivs);
4662   iv_ca_delta_commit (data, ivs, *delta, false);
4663
4664   return cost;
4665 }
4666
4667 /* Try optimizing the set of candidates IVS by removing candidates different
4668    from to EXCEPT_CAND from it.  Return cost of the new set, and store
4669    differences in DELTA.  */
4670
4671 static comp_cost
4672 iv_ca_prune (struct ivopts_data *data, struct iv_ca *ivs,
4673              struct iv_cand *except_cand, struct iv_ca_delta **delta)
4674 {
4675   bitmap_iterator bi;
4676   struct iv_ca_delta *act_delta, *best_delta;
4677   unsigned i;
4678   comp_cost best_cost, acost;
4679   struct iv_cand *cand;
4680
4681   best_delta = NULL;
4682   best_cost = iv_ca_cost (ivs);
4683
4684   EXECUTE_IF_SET_IN_BITMAP (ivs->cands, 0, i, bi)
4685     {
4686       cand = iv_cand (data, i);
4687
4688       if (cand == except_cand)
4689         continue;
4690
4691       acost = iv_ca_narrow (data, ivs, cand, &act_delta);
4692
4693       if (compare_costs (acost, best_cost) < 0)
4694         {
4695           best_cost = acost;
4696           iv_ca_delta_free (&best_delta);
4697           best_delta = act_delta;
4698         }
4699       else
4700         iv_ca_delta_free (&act_delta);
4701     }
4702
4703   if (!best_delta)
4704     {
4705       *delta = NULL;
4706       return best_cost;
4707     }
4708
4709   /* Recurse to possibly remove other unnecessary ivs.  */
4710   iv_ca_delta_commit (data, ivs, best_delta, true);
4711   best_cost = iv_ca_prune (data, ivs, except_cand, delta);
4712   iv_ca_delta_commit (data, ivs, best_delta, false);
4713   *delta = iv_ca_delta_join (best_delta, *delta);
4714   return best_cost;
4715 }
4716
4717 /* Tries to extend the sets IVS in the best possible way in order
4718    to express the USE.  */
4719
4720 static bool
4721 try_add_cand_for (struct ivopts_data *data, struct iv_ca *ivs,
4722                   struct iv_use *use)
4723 {
4724   comp_cost best_cost, act_cost;
4725   unsigned i;
4726   bitmap_iterator bi;
4727   struct iv_cand *cand;
4728   struct iv_ca_delta *best_delta = NULL, *act_delta;
4729   struct cost_pair *cp;
4730
4731   iv_ca_add_use (data, ivs, use);
4732   best_cost = iv_ca_cost (ivs);
4733
4734   cp = iv_ca_cand_for_use (ivs, use);
4735   if (cp)
4736     {
4737       best_delta = iv_ca_delta_add (use, NULL, cp, NULL);
4738       iv_ca_set_no_cp (data, ivs, use);
4739     }
4740
4741   /* First try important candidates not based on any memory object.  Only if
4742      this fails, try the specific ones.  Rationale -- in loops with many
4743      variables the best choice often is to use just one generic biv.  If we
4744      added here many ivs specific to the uses, the optimization algorithm later
4745      would be likely to get stuck in a local minimum, thus causing us to create
4746      too many ivs.  The approach from few ivs to more seems more likely to be
4747      successful -- starting from few ivs, replacing an expensive use by a
4748      specific iv should always be a win.  */
4749   EXECUTE_IF_SET_IN_BITMAP (data->important_candidates, 0, i, bi)
4750     {
4751       cand = iv_cand (data, i);
4752
4753       if (cand->iv->base_object != NULL_TREE)
4754         continue;
4755
4756       if (iv_ca_cand_used_p (ivs, cand))
4757         continue;
4758
4759       cp = get_use_iv_cost (data, use, cand);
4760       if (!cp)
4761         continue;
4762
4763       iv_ca_set_cp (data, ivs, use, cp);
4764       act_cost = iv_ca_extend (data, ivs, cand, &act_delta, NULL);
4765       iv_ca_set_no_cp (data, ivs, use);
4766       act_delta = iv_ca_delta_add (use, NULL, cp, act_delta);
4767
4768       if (compare_costs (act_cost, best_cost) < 0)
4769         {
4770           best_cost = act_cost;
4771
4772           iv_ca_delta_free (&best_delta);
4773           best_delta = act_delta;
4774         }
4775       else
4776         iv_ca_delta_free (&act_delta);
4777     }
4778
4779   if (infinite_cost_p (best_cost))
4780     {
4781       for (i = 0; i < use->n_map_members; i++)
4782         {
4783           cp = use->cost_map + i;
4784           cand = cp->cand;
4785           if (!cand)
4786             continue;
4787
4788           /* Already tried this.  */
4789           if (cand->important && cand->iv->base_object == NULL_TREE)
4790             continue;
4791       
4792           if (iv_ca_cand_used_p (ivs, cand))
4793             continue;
4794
4795           act_delta = NULL;
4796           iv_ca_set_cp (data, ivs, use, cp);
4797           act_cost = iv_ca_extend (data, ivs, cand, &act_delta, NULL);
4798           iv_ca_set_no_cp (data, ivs, use);
4799           act_delta = iv_ca_delta_add (use, iv_ca_cand_for_use (ivs, use),
4800                                        cp, act_delta);
4801
4802           if (compare_costs (act_cost, best_cost) < 0)
4803             {
4804               best_cost = act_cost;
4805
4806               if (best_delta)
4807                 iv_ca_delta_free (&best_delta);
4808               best_delta = act_delta;
4809             }
4810           else
4811             iv_ca_delta_free (&act_delta);
4812         }
4813     }
4814
4815   iv_ca_delta_commit (data, ivs, best_delta, true);
4816   iv_ca_delta_free (&best_delta);
4817
4818   return !infinite_cost_p (best_cost);
4819 }
4820
4821 /* Finds an initial assignment of candidates to uses.  */
4822
4823 static struct iv_ca *
4824 get_initial_solution (struct ivopts_data *data)
4825 {
4826   struct iv_ca *ivs = iv_ca_new (data);
4827   unsigned i;
4828
4829   for (i = 0; i < n_iv_uses (data); i++)
4830     if (!try_add_cand_for (data, ivs, iv_use (data, i)))
4831       {
4832         iv_ca_free (&ivs);
4833         return NULL;
4834       }
4835
4836   return ivs;
4837 }
4838
4839 /* Tries to improve set of induction variables IVS.  */
4840
4841 static bool
4842 try_improve_iv_set (struct ivopts_data *data, struct iv_ca *ivs)
4843 {
4844   unsigned i, n_ivs;
4845   comp_cost acost, best_cost = iv_ca_cost (ivs);
4846   struct iv_ca_delta *best_delta = NULL, *act_delta, *tmp_delta;
4847   struct iv_cand *cand;
4848
4849   /* Try extending the set of induction variables by one.  */
4850   for (i = 0; i < n_iv_cands (data); i++)
4851     {
4852       cand = iv_cand (data, i);
4853       
4854       if (iv_ca_cand_used_p (ivs, cand))
4855         continue;
4856
4857       acost = iv_ca_extend (data, ivs, cand, &act_delta, &n_ivs);
4858       if (!act_delta)
4859         continue;
4860
4861       /* If we successfully added the candidate and the set is small enough,
4862          try optimizing it by removing other candidates.  */
4863       if (n_ivs <= ALWAYS_PRUNE_CAND_SET_BOUND)
4864         {
4865           iv_ca_delta_commit (data, ivs, act_delta, true);
4866           acost = iv_ca_prune (data, ivs, cand, &tmp_delta);
4867           iv_ca_delta_commit (data, ivs, act_delta, false);
4868           act_delta = iv_ca_delta_join (act_delta, tmp_delta);
4869         }
4870
4871       if (compare_costs (acost, best_cost) < 0)
4872         {
4873           best_cost = acost;
4874           iv_ca_delta_free (&best_delta);
4875           best_delta = act_delta;
4876         }
4877       else
4878         iv_ca_delta_free (&act_delta);
4879     }
4880
4881   if (!best_delta)
4882     {
4883       /* Try removing the candidates from the set instead.  */
4884       best_cost = iv_ca_prune (data, ivs, NULL, &best_delta);
4885
4886       /* Nothing more we can do.  */
4887       if (!best_delta)
4888         return false;
4889     }
4890
4891   iv_ca_delta_commit (data, ivs, best_delta, true);
4892   gcc_assert (compare_costs (best_cost, iv_ca_cost (ivs)) == 0);
4893   iv_ca_delta_free (&best_delta);
4894   return true;
4895 }
4896
4897 /* Attempts to find the optimal set of induction variables.  We do simple
4898    greedy heuristic -- we try to replace at most one candidate in the selected
4899    solution and remove the unused ivs while this improves the cost.  */
4900
4901 static struct iv_ca *
4902 find_optimal_iv_set (struct ivopts_data *data)
4903 {
4904   unsigned i;
4905   struct iv_ca *set;
4906   struct iv_use *use;
4907
4908   /* Get the initial solution.  */
4909   set = get_initial_solution (data);
4910   if (!set)
4911     {
4912       if (dump_file && (dump_flags & TDF_DETAILS))
4913         fprintf (dump_file, "Unable to substitute for ivs, failed.\n");
4914       return NULL;
4915     }
4916
4917   if (dump_file && (dump_flags & TDF_DETAILS))
4918     {
4919       fprintf (dump_file, "Initial set of candidates:\n");
4920       iv_ca_dump (data, dump_file, set);
4921     }
4922
4923   while (try_improve_iv_set (data, set))
4924     {
4925       if (dump_file && (dump_flags & TDF_DETAILS))
4926         {
4927           fprintf (dump_file, "Improved to:\n");
4928           iv_ca_dump (data, dump_file, set);
4929         }
4930     }
4931
4932   if (dump_file && (dump_flags & TDF_DETAILS))
4933     {
4934       comp_cost cost = iv_ca_cost (set);
4935       fprintf (dump_file, "Final cost %d (complexity %d)\n\n", cost.cost, cost.complexity);
4936     }
4937
4938   for (i = 0; i < n_iv_uses (data); i++)
4939     {
4940       use = iv_use (data, i);
4941       use->selected = iv_ca_cand_for_use (set, use)->cand;
4942     }
4943
4944   return set;
4945 }
4946
4947 /* Creates a new induction variable corresponding to CAND.  */
4948
4949 static void
4950 create_new_iv (struct ivopts_data *data, struct iv_cand *cand)
4951 {
4952   gimple_stmt_iterator incr_pos;
4953   tree base;
4954   bool after = false;
4955
4956   if (!cand->iv)
4957     return;
4958
4959   switch (cand->pos)
4960     {
4961     case IP_NORMAL:
4962       incr_pos = gsi_last_bb (ip_normal_pos (data->current_loop));
4963       break;
4964
4965     case IP_END:
4966       incr_pos = gsi_last_bb (ip_end_pos (data->current_loop));
4967       after = true;
4968       break;
4969
4970     case IP_ORIGINAL:
4971       /* Mark that the iv is preserved.  */
4972       name_info (data, cand->var_before)->preserve_biv = true;
4973       name_info (data, cand->var_after)->preserve_biv = true;
4974
4975       /* Rewrite the increment so that it uses var_before directly.  */
4976       find_interesting_uses_op (data, cand->var_after)->selected = cand;
4977       
4978       return;
4979     }
4980  
4981   gimple_add_tmp_var (cand->var_before);
4982   add_referenced_var (cand->var_before);
4983
4984   base = unshare_expr (cand->iv->base);
4985
4986   create_iv (base, unshare_expr (cand->iv->step),
4987              cand->var_before, data->current_loop,
4988              &incr_pos, after, &cand->var_before, &cand->var_after);
4989 }
4990
4991 /* Creates new induction variables described in SET.  */
4992
4993 static void
4994 create_new_ivs (struct ivopts_data *data, struct iv_ca *set)
4995 {
4996   unsigned i;
4997   struct iv_cand *cand;
4998   bitmap_iterator bi;
4999
5000   EXECUTE_IF_SET_IN_BITMAP (set->cands, 0, i, bi)
5001     {
5002       cand = iv_cand (data, i);
5003       create_new_iv (data, cand);
5004     }
5005 }
5006
5007 /* Removes statement STMT (real or a phi node).  If INCLUDING_DEFINED_NAME
5008    is true, remove also the ssa name defined by the statement.  */
5009
5010 static void
5011 remove_statement (gimple stmt, bool including_defined_name)
5012 {
5013   gimple_stmt_iterator bsi = gsi_for_stmt (stmt);
5014
5015   if (gimple_code (stmt) == GIMPLE_PHI)
5016     remove_phi_node (&bsi, including_defined_name);
5017   else
5018     {
5019       gsi_remove (&bsi, true);
5020       release_defs (stmt); 
5021     }
5022 }
5023
5024 /* Rewrites USE (definition of iv used in a nonlinear expression)
5025    using candidate CAND.  */
5026
5027 static void
5028 rewrite_use_nonlinear_expr (struct ivopts_data *data,
5029                             struct iv_use *use, struct iv_cand *cand)
5030 {
5031   tree comp;
5032   tree op, tgt;
5033   gimple ass;
5034   gimple_stmt_iterator bsi;
5035
5036   /* An important special case -- if we are asked to express value of
5037      the original iv by itself, just exit; there is no need to
5038      introduce a new computation (that might also need casting the
5039      variable to unsigned and back).  */
5040   if (cand->pos == IP_ORIGINAL
5041       && cand->incremented_at == use->stmt)
5042     {
5043       tree step, ctype, utype;
5044       enum tree_code incr_code = PLUS_EXPR, old_code;
5045
5046       gcc_assert (is_gimple_assign (use->stmt));
5047       gcc_assert (gimple_assign_lhs (use->stmt) == cand->var_after);
5048
5049       step = cand->iv->step;
5050       ctype = TREE_TYPE (step);
5051       utype = TREE_TYPE (cand->var_after);
5052       if (TREE_CODE (step) == NEGATE_EXPR)
5053         {
5054           incr_code = MINUS_EXPR;
5055           step = TREE_OPERAND (step, 0);
5056         }
5057
5058       /* Check whether we may leave the computation unchanged.
5059          This is the case only if it does not rely on other
5060          computations in the loop -- otherwise, the computation
5061          we rely upon may be removed in remove_unused_ivs,
5062          thus leading to ICE.  */
5063       old_code = gimple_assign_rhs_code (use->stmt);
5064       if (old_code == PLUS_EXPR
5065           || old_code == MINUS_EXPR
5066           || old_code == POINTER_PLUS_EXPR)
5067         {
5068           if (gimple_assign_rhs1 (use->stmt) == cand->var_before)
5069             op = gimple_assign_rhs2 (use->stmt);
5070           else if (old_code != MINUS_EXPR
5071                    && gimple_assign_rhs2 (use->stmt) == cand->var_before)
5072             op = gimple_assign_rhs1 (use->stmt);
5073           else
5074             op = NULL_TREE;
5075         }
5076       else
5077         op = NULL_TREE;
5078
5079       if (op
5080           && (TREE_CODE (op) == INTEGER_CST
5081               || operand_equal_p (op, step, 0)))
5082         return;
5083
5084       /* Otherwise, add the necessary computations to express
5085          the iv.  */
5086       op = fold_convert (ctype, cand->var_before);
5087       comp = fold_convert (utype,
5088                            build2 (incr_code, ctype, op,
5089                                    unshare_expr (step)));
5090     }
5091   else
5092     {
5093       comp = get_computation (data->current_loop, use, cand);
5094       gcc_assert (comp != NULL_TREE);
5095     }
5096
5097   switch (gimple_code (use->stmt))
5098     {
5099     case GIMPLE_PHI:
5100       tgt = PHI_RESULT (use->stmt);
5101
5102       /* If we should keep the biv, do not replace it.  */
5103       if (name_info (data, tgt)->preserve_biv)
5104         return;
5105
5106       bsi = gsi_after_labels (gimple_bb (use->stmt));
5107       break;
5108
5109     case GIMPLE_ASSIGN:
5110       tgt = gimple_assign_lhs (use->stmt);
5111       bsi = gsi_for_stmt (use->stmt);
5112       break;
5113
5114     default:
5115       gcc_unreachable ();
5116     }
5117
5118   op = force_gimple_operand_gsi (&bsi, comp, false, SSA_NAME_VAR (tgt),
5119                                  true, GSI_SAME_STMT);
5120
5121   if (gimple_code (use->stmt) == GIMPLE_PHI)
5122     {
5123       ass = gimple_build_assign (tgt, op);
5124       gsi_insert_before (&bsi, ass, GSI_SAME_STMT);
5125       remove_statement (use->stmt, false);
5126     }
5127   else
5128     {
5129       gimple_assign_set_rhs_from_tree (&bsi, op);
5130       use->stmt = gsi_stmt (bsi);
5131     }
5132 }
5133
5134 /* Replaces ssa name in index IDX by its basic variable.  Callback for
5135    for_each_index.  */
5136
5137 static bool
5138 idx_remove_ssa_names (tree base, tree *idx,
5139                       void *data ATTRIBUTE_UNUSED)
5140 {
5141   tree *op;
5142
5143   if (TREE_CODE (*idx) == SSA_NAME)
5144     *idx = SSA_NAME_VAR (*idx);
5145
5146   if (TREE_CODE (base) == ARRAY_REF)
5147     {
5148       op = &TREE_OPERAND (base, 2);
5149       if (*op
5150           && TREE_CODE (*op) == SSA_NAME)
5151         *op = SSA_NAME_VAR (*op);
5152       op = &TREE_OPERAND (base, 3);
5153       if (*op
5154           && TREE_CODE (*op) == SSA_NAME)
5155         *op = SSA_NAME_VAR (*op);
5156     }
5157
5158   return true;
5159 }
5160
5161 /* Unshares REF and replaces ssa names inside it by their basic variables.  */
5162
5163 static tree
5164 unshare_and_remove_ssa_names (tree ref)
5165 {
5166   ref = unshare_expr (ref);
5167   for_each_index (&ref, idx_remove_ssa_names, NULL);
5168
5169   return ref;
5170 }
5171
5172 /* Extract the alias analysis info for the memory reference REF.  There are
5173    several ways how this information may be stored and what precisely is
5174    its semantics depending on the type of the reference, but there always is
5175    somewhere hidden one _DECL node that is used to determine the set of
5176    virtual operands for the reference.  The code below deciphers this jungle
5177    and extracts this single useful piece of information.  */
5178
5179 static tree
5180 get_ref_tag (tree ref, tree orig)
5181 {
5182   tree var = get_base_address (ref);
5183   tree aref = NULL_TREE, tag, sv;
5184   HOST_WIDE_INT offset, size, maxsize;
5185
5186   for (sv = orig; handled_component_p (sv); sv = TREE_OPERAND (sv, 0))
5187     {
5188       aref = get_ref_base_and_extent (sv, &offset, &size, &maxsize);
5189       if (ref)
5190         break;
5191     }
5192
5193   if (!var)
5194     return NULL_TREE;
5195
5196   if (TREE_CODE (var) == INDIRECT_REF)
5197     {
5198       /* If the base is a dereference of a pointer, first check its name memory
5199          tag.  If it does not have one, use its symbol memory tag.  */
5200       var = TREE_OPERAND (var, 0);
5201       if (TREE_CODE (var) != SSA_NAME)
5202         return NULL_TREE;
5203
5204       if (SSA_NAME_PTR_INFO (var))
5205         {
5206           tag = SSA_NAME_PTR_INFO (var)->name_mem_tag;
5207           if (tag)
5208             return tag;
5209         }
5210  
5211       var = SSA_NAME_VAR (var);
5212       tag = symbol_mem_tag (var);
5213       gcc_assert (tag != NULL_TREE);
5214       return tag;
5215     }
5216   else
5217     { 
5218       if (!DECL_P (var))
5219         return NULL_TREE;
5220
5221       tag = symbol_mem_tag (var);
5222       if (tag)
5223         return tag;
5224
5225       return var;
5226     }
5227 }
5228
5229 /* Copies the reference information from OLD_REF to NEW_REF.  */
5230
5231 static void
5232 copy_ref_info (tree new_ref, tree old_ref)
5233 {
5234   if (TREE_CODE (old_ref) == TARGET_MEM_REF)
5235     copy_mem_ref_info (new_ref, old_ref);
5236   else
5237     {
5238       TMR_ORIGINAL (new_ref) = unshare_and_remove_ssa_names (old_ref);
5239       TMR_TAG (new_ref) = get_ref_tag (old_ref, TMR_ORIGINAL (new_ref));
5240     }
5241 }
5242
5243 /* Rewrites USE (address that is an iv) using candidate CAND.  */
5244
5245 static void
5246 rewrite_use_address (struct ivopts_data *data,
5247                      struct iv_use *use, struct iv_cand *cand)
5248 {
5249   aff_tree aff;
5250   gimple_stmt_iterator bsi = gsi_for_stmt (use->stmt);
5251   tree ref;
5252   bool ok;
5253
5254   ok = get_computation_aff (data->current_loop, use, cand, use->stmt, &aff);
5255   gcc_assert (ok);
5256   unshare_aff_combination (&aff);
5257
5258   ref = create_mem_ref (&bsi, TREE_TYPE (*use->op_p), &aff);
5259   copy_ref_info (ref, *use->op_p);
5260   *use->op_p = ref;
5261 }
5262
5263 /* Rewrites USE (the condition such that one of the arguments is an iv) using
5264    candidate CAND.  */
5265
5266 static void
5267 rewrite_use_compare (struct ivopts_data *data,
5268                      struct iv_use *use, struct iv_cand *cand)
5269 {
5270   tree comp, *var_p, op, bound;
5271   gimple_stmt_iterator bsi = gsi_for_stmt (use->stmt);
5272   enum tree_code compare;
5273   struct cost_pair *cp = get_use_iv_cost (data, use, cand);
5274   bool ok;
5275
5276   bound = cp->value;
5277   if (bound)
5278     {
5279       tree var = var_at_stmt (data->current_loop, cand, use->stmt);
5280       tree var_type = TREE_TYPE (var);
5281
5282       compare = iv_elimination_compare (data, use);
5283       bound = unshare_expr (fold_convert (var_type, bound));
5284       op = force_gimple_operand_gsi (&bsi, bound, true, NULL_TREE,
5285                                      true, GSI_SAME_STMT);
5286
5287       gimple_cond_set_lhs (use->stmt, var);
5288       gimple_cond_set_code (use->stmt, compare);
5289       gimple_cond_set_rhs (use->stmt, op);
5290       return;
5291     }
5292
5293   /* The induction variable elimination failed; just express the original
5294      giv.  */
5295   comp = get_computation (data->current_loop, use, cand);
5296   gcc_assert (comp != NULL_TREE);
5297
5298   ok = extract_cond_operands (data, use->stmt, &var_p, NULL, NULL, NULL);
5299   gcc_assert (ok);
5300
5301   *var_p = force_gimple_operand_gsi (&bsi, comp, true, SSA_NAME_VAR (*var_p),
5302                                      true, GSI_SAME_STMT);
5303 }
5304
5305 /* Rewrites USE using candidate CAND.  */
5306
5307 static void
5308 rewrite_use (struct ivopts_data *data, struct iv_use *use, struct iv_cand *cand)
5309 {
5310   push_stmt_changes (&use->stmt);
5311
5312   switch (use->type)
5313     {
5314       case USE_NONLINEAR_EXPR:
5315         rewrite_use_nonlinear_expr (data, use, cand);
5316         break;
5317
5318       case USE_ADDRESS:
5319         rewrite_use_address (data, use, cand);
5320         break;
5321
5322       case USE_COMPARE:
5323         rewrite_use_compare (data, use, cand);
5324         break;
5325
5326       default:
5327         gcc_unreachable ();
5328     }
5329
5330   pop_stmt_changes (&use->stmt);
5331 }
5332
5333 /* Rewrite the uses using the selected induction variables.  */
5334
5335 static void
5336 rewrite_uses (struct ivopts_data *data)
5337 {
5338   unsigned i;
5339   struct iv_cand *cand;
5340   struct iv_use *use;
5341
5342   for (i = 0; i < n_iv_uses (data); i++)
5343     {
5344       use = iv_use (data, i);
5345       cand = use->selected;
5346       gcc_assert (cand);
5347
5348       rewrite_use (data, use, cand);
5349     }
5350 }
5351
5352 /* Removes the ivs that are not used after rewriting.  */
5353
5354 static void
5355 remove_unused_ivs (struct ivopts_data *data)
5356 {
5357   unsigned j;
5358   bitmap_iterator bi;
5359
5360   EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, j, bi)
5361     {
5362       struct version_info *info;
5363
5364       info = ver_info (data, j);
5365       if (info->iv
5366           && !integer_zerop (info->iv->step)
5367           && !info->inv_id
5368           && !info->iv->have_use_for
5369           && !info->preserve_biv)
5370         remove_statement (SSA_NAME_DEF_STMT (info->iv->ssa_name), true);
5371     }
5372 }
5373
5374 /* Frees data allocated by the optimization of a single loop.  */
5375
5376 static void
5377 free_loop_data (struct ivopts_data *data)
5378 {
5379   unsigned i, j;
5380   bitmap_iterator bi;
5381   tree obj;
5382
5383   if (data->niters)
5384     {
5385       pointer_map_destroy (data->niters);
5386       data->niters = NULL;
5387     }
5388
5389   EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, i, bi)
5390     {
5391       struct version_info *info;
5392
5393       info = ver_info (data, i);
5394       if (info->iv)
5395         free (info->iv);
5396       info->iv = NULL;
5397       info->has_nonlin_use = false;
5398       info->preserve_biv = false;
5399       info->inv_id = 0;
5400     }
5401   bitmap_clear (data->relevant);
5402   bitmap_clear (data->important_candidates);
5403
5404   for (i = 0; i < n_iv_uses (data); i++)
5405     {
5406       struct iv_use *use = iv_use (data, i);
5407
5408       free (use->iv);
5409       BITMAP_FREE (use->related_cands);
5410       for (j = 0; j < use->n_map_members; j++)
5411         if (use->cost_map[j].depends_on)
5412           BITMAP_FREE (use->cost_map[j].depends_on);
5413       free (use->cost_map);
5414       free (use);
5415     }
5416   VEC_truncate (iv_use_p, data->iv_uses, 0);
5417
5418   for (i = 0; i < n_iv_cands (data); i++)
5419     {
5420       struct iv_cand *cand = iv_cand (data, i);
5421
5422       if (cand->iv)
5423         free (cand->iv);
5424       if (cand->depends_on)
5425         BITMAP_FREE (cand->depends_on);
5426       free (cand);
5427     }
5428   VEC_truncate (iv_cand_p, data->iv_candidates, 0);
5429
5430   if (data->version_info_size < num_ssa_names)
5431     {
5432       data->version_info_size = 2 * num_ssa_names;
5433       free (data->version_info);
5434       data->version_info = XCNEWVEC (struct version_info, data->version_info_size);
5435     }
5436
5437   data->max_inv_id = 0;
5438
5439   for (i = 0; VEC_iterate (tree, decl_rtl_to_reset, i, obj); i++)
5440     SET_DECL_RTL (obj, NULL_RTX);
5441
5442   VEC_truncate (tree, decl_rtl_to_reset, 0);
5443 }
5444
5445 /* Finalizes data structures used by the iv optimization pass.  LOOPS is the
5446    loop tree.  */
5447
5448 static void
5449 tree_ssa_iv_optimize_finalize (struct ivopts_data *data)
5450 {
5451   free_loop_data (data);
5452   free (data->version_info);
5453   BITMAP_FREE (data->relevant);
5454   BITMAP_FREE (data->important_candidates);
5455
5456   VEC_free (tree, heap, decl_rtl_to_reset);
5457   VEC_free (iv_use_p, heap, data->iv_uses);
5458   VEC_free (iv_cand_p, heap, data->iv_candidates);
5459 }
5460
5461 /* Optimizes the LOOP.  Returns true if anything changed.  */
5462
5463 static bool
5464 tree_ssa_iv_optimize_loop (struct ivopts_data *data, struct loop *loop)
5465 {
5466   bool changed = false;
5467   struct iv_ca *iv_ca;
5468   edge exit;
5469
5470   gcc_assert (!data->niters);
5471   data->current_loop = loop;
5472
5473   if (dump_file && (dump_flags & TDF_DETAILS))
5474     {
5475       fprintf (dump_file, "Processing loop %d\n", loop->num);
5476       
5477       exit = single_dom_exit (loop);
5478       if (exit)
5479         {
5480           fprintf (dump_file, "  single exit %d -> %d, exit condition ",
5481                    exit->src->index, exit->dest->index);
5482           print_gimple_stmt (dump_file, last_stmt (exit->src), 0, TDF_SLIM);
5483           fprintf (dump_file, "\n");
5484         }
5485
5486       fprintf (dump_file, "\n");
5487     }
5488
5489   /* For each ssa name determines whether it behaves as an induction variable
5490      in some loop.  */
5491   if (!find_induction_variables (data))
5492     goto finish;
5493
5494   /* Finds interesting uses (item 1).  */
5495   find_interesting_uses (data);
5496   if (n_iv_uses (data) > MAX_CONSIDERED_USES)
5497     goto finish;
5498
5499   /* Finds candidates for the induction variables (item 2).  */
5500   find_iv_candidates (data);
5501
5502   /* Calculates the costs (item 3, part 1).  */
5503   determine_use_iv_costs (data);
5504   determine_iv_costs (data);
5505   determine_set_costs (data);
5506
5507   /* Find the optimal set of induction variables (item 3, part 2).  */
5508   iv_ca = find_optimal_iv_set (data);
5509   if (!iv_ca)
5510     goto finish;
5511   changed = true;
5512
5513   /* Create the new induction variables (item 4, part 1).  */
5514   create_new_ivs (data, iv_ca);
5515   iv_ca_free (&iv_ca);
5516   
5517   /* Rewrite the uses (item 4, part 2).  */
5518   rewrite_uses (data);
5519
5520   /* Remove the ivs that are unused after rewriting.  */
5521   remove_unused_ivs (data);
5522
5523   /* We have changed the structure of induction variables; it might happen
5524      that definitions in the scev database refer to some of them that were
5525      eliminated.  */
5526   scev_reset ();
5527
5528 finish:
5529   free_loop_data (data);
5530
5531   return changed;
5532 }
5533
5534 /* Main entry point.  Optimizes induction variables in loops.  */
5535
5536 void
5537 tree_ssa_iv_optimize (void)
5538 {
5539   struct loop *loop;
5540   struct ivopts_data data;
5541   loop_iterator li;
5542
5543   tree_ssa_iv_optimize_init (&data);
5544
5545   /* Optimize the loops starting with the innermost ones.  */
5546   FOR_EACH_LOOP (li, loop, LI_FROM_INNERMOST)
5547     {
5548       if (dump_file && (dump_flags & TDF_DETAILS))
5549         flow_loop_dump (loop, dump_file, NULL, 1);
5550
5551       tree_ssa_iv_optimize_loop (&data, loop);
5552     }
5553
5554   tree_ssa_iv_optimize_finalize (&data);
5555 }