2016-08-24 Michael Collison <michael.collison@linaro.org>
[official-gcc.git] / gcc / tree-ssa-loop-ivopts.c
blob62ba71bd18a398e7bc449c51d6243aa218007e9a
1 /* Induction variable optimizations.
2 Copyright (C) 2003-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This pass tries to find the optimal set of induction variables for the loop.
21 It optimizes just the basic linear induction variables (although adding
22 support for other types should not be too hard). It includes the
23 optimizations commonly known as strength reduction, induction variable
24 coalescing and induction variable elimination. It does it in the
25 following steps:
27 1) The interesting uses of induction variables are found. This includes
29 -- uses of induction variables in non-linear expressions
30 -- addresses of arrays
31 -- comparisons of induction variables
33 Note the interesting uses are categorized and handled in group.
34 Generally, address type uses are grouped together if their iv bases
35 are different in constant offset.
37 2) Candidates for the induction variables are found. This includes
39 -- old induction variables
40 -- the variables defined by expressions derived from the "interesting
41 groups/uses" above
43 3) The optimal (w.r. to a cost function) set of variables is chosen. The
44 cost function assigns a cost to sets of induction variables and consists
45 of three parts:
47 -- The group/use costs. Each of the interesting groups/uses chooses
48 the best induction variable in the set and adds its cost to the sum.
49 The cost reflects the time spent on modifying the induction variables
50 value to be usable for the given purpose (adding base and offset for
51 arrays, etc.).
52 -- The variable costs. Each of the variables has a cost assigned that
53 reflects the costs associated with incrementing the value of the
54 variable. The original variables are somewhat preferred.
55 -- The set cost. Depending on the size of the set, extra cost may be
56 added to reflect register pressure.
58 All the costs are defined in a machine-specific way, using the target
59 hooks and machine descriptions to determine them.
61 4) The trees are transformed to use the new variables, the dead code is
62 removed.
64 All of this is done loop by loop. Doing it globally is theoretically
65 possible, it might give a better performance and it might enable us
66 to decide costs more precisely, but getting all the interactions right
67 would be complicated. */
69 #include "config.h"
70 #include "system.h"
71 #include "coretypes.h"
72 #include "backend.h"
73 #include "rtl.h"
74 #include "tree.h"
75 #include "gimple.h"
76 #include "cfghooks.h"
77 #include "tree-pass.h"
78 #include "tm_p.h"
79 #include "ssa.h"
80 #include "expmed.h"
81 #include "insn-config.h"
82 #include "emit-rtl.h"
83 #include "recog.h"
84 #include "cgraph.h"
85 #include "gimple-pretty-print.h"
86 #include "alias.h"
87 #include "fold-const.h"
88 #include "stor-layout.h"
89 #include "tree-eh.h"
90 #include "gimplify.h"
91 #include "gimple-iterator.h"
92 #include "gimplify-me.h"
93 #include "tree-cfg.h"
94 #include "tree-ssa-loop-ivopts.h"
95 #include "tree-ssa-loop-manip.h"
96 #include "tree-ssa-loop-niter.h"
97 #include "tree-ssa-loop.h"
98 #include "explow.h"
99 #include "expr.h"
100 #include "tree-dfa.h"
101 #include "tree-ssa.h"
102 #include "cfgloop.h"
103 #include "tree-scalar-evolution.h"
104 #include "params.h"
105 #include "tree-affine.h"
106 #include "tree-ssa-propagate.h"
107 #include "tree-ssa-address.h"
108 #include "builtins.h"
109 #include "tree-vectorizer.h"
111 /* FIXME: Expressions are expanded to RTL in this pass to determine the
112 cost of different addressing modes. This should be moved to a TBD
113 interface between the GIMPLE and RTL worlds. */
115 /* The infinite cost. */
116 #define INFTY 10000000
118 /* Returns the expected number of loop iterations for LOOP.
119 The average trip count is computed from profile data if it
120 exists. */
122 static inline HOST_WIDE_INT
123 avg_loop_niter (struct loop *loop)
125 HOST_WIDE_INT niter = estimated_stmt_executions_int (loop);
126 if (niter == -1)
128 niter = likely_max_stmt_executions_int (loop);
130 if (niter == -1 || niter > PARAM_VALUE (PARAM_AVG_LOOP_NITER))
131 return PARAM_VALUE (PARAM_AVG_LOOP_NITER);
134 return niter;
137 struct iv_use;
139 /* Representation of the induction variable. */
140 struct iv
142 tree base; /* Initial value of the iv. */
143 tree base_object; /* A memory object to that the induction variable points. */
144 tree step; /* Step of the iv (constant only). */
145 tree ssa_name; /* The ssa name with the value. */
146 struct iv_use *nonlin_use; /* The identifier in the use if it is the case. */
147 bool biv_p; /* Is it a biv? */
148 bool no_overflow; /* True if the iv doesn't overflow. */
149 bool have_address_use;/* For biv, indicate if it's used in any address
150 type use. */
153 /* Per-ssa version information (induction variable descriptions, etc.). */
154 struct version_info
156 tree name; /* The ssa name. */
157 struct iv *iv; /* Induction variable description. */
158 bool has_nonlin_use; /* For a loop-level invariant, whether it is used in
159 an expression that is not an induction variable. */
160 bool preserve_biv; /* For the original biv, whether to preserve it. */
161 unsigned inv_id; /* Id of an invariant. */
164 /* Types of uses. */
165 enum use_type
167 USE_NONLINEAR_EXPR, /* Use in a nonlinear expression. */
168 USE_ADDRESS, /* Use in an address. */
169 USE_COMPARE /* Use is a compare. */
172 /* Cost of a computation. */
173 struct comp_cost
175 comp_cost (): cost (0), complexity (0), scratch (0)
178 comp_cost (int cost, unsigned complexity, int scratch = 0)
179 : cost (cost), complexity (complexity), scratch (scratch)
182 /* Returns true if COST is infinite. */
183 bool infinite_cost_p ();
185 /* Adds costs COST1 and COST2. */
186 friend comp_cost operator+ (comp_cost cost1, comp_cost cost2);
188 /* Adds COST to the comp_cost. */
189 comp_cost operator+= (comp_cost cost);
191 /* Adds constant C to this comp_cost. */
192 comp_cost operator+= (HOST_WIDE_INT c);
194 /* Subtracts constant C to this comp_cost. */
195 comp_cost operator-= (HOST_WIDE_INT c);
197 /* Divide the comp_cost by constant C. */
198 comp_cost operator/= (HOST_WIDE_INT c);
200 /* Multiply the comp_cost by constant C. */
201 comp_cost operator*= (HOST_WIDE_INT c);
203 /* Subtracts costs COST1 and COST2. */
204 friend comp_cost operator- (comp_cost cost1, comp_cost cost2);
206 /* Subtracts COST from this comp_cost. */
207 comp_cost operator-= (comp_cost cost);
209 /* Returns true if COST1 is smaller than COST2. */
210 friend bool operator< (comp_cost cost1, comp_cost cost2);
212 /* Returns true if COST1 and COST2 are equal. */
213 friend bool operator== (comp_cost cost1, comp_cost cost2);
215 /* Returns true if COST1 is smaller or equal than COST2. */
216 friend bool operator<= (comp_cost cost1, comp_cost cost2);
218 int cost; /* The runtime cost. */
219 unsigned complexity; /* The estimate of the complexity of the code for
220 the computation (in no concrete units --
221 complexity field should be larger for more
222 complex expressions and addressing modes). */
223 int scratch; /* Scratch used during cost computation. */
226 static const comp_cost no_cost;
227 static const comp_cost infinite_cost (INFTY, INFTY, INFTY);
229 bool
230 comp_cost::infinite_cost_p ()
232 return cost == INFTY;
235 comp_cost
236 operator+ (comp_cost cost1, comp_cost cost2)
238 if (cost1.infinite_cost_p () || cost2.infinite_cost_p ())
239 return infinite_cost;
241 cost1.cost += cost2.cost;
242 cost1.complexity += cost2.complexity;
244 return cost1;
247 comp_cost
248 operator- (comp_cost cost1, comp_cost cost2)
250 if (cost1.infinite_cost_p ())
251 return infinite_cost;
253 gcc_assert (!cost2.infinite_cost_p ());
255 cost1.cost -= cost2.cost;
256 cost1.complexity -= cost2.complexity;
258 return cost1;
261 comp_cost
262 comp_cost::operator+= (comp_cost cost)
264 *this = *this + cost;
265 return *this;
268 comp_cost
269 comp_cost::operator+= (HOST_WIDE_INT c)
271 if (infinite_cost_p ())
272 return *this;
274 this->cost += c;
276 return *this;
279 comp_cost
280 comp_cost::operator-= (HOST_WIDE_INT c)
282 if (infinite_cost_p ())
283 return *this;
285 this->cost -= c;
287 return *this;
290 comp_cost
291 comp_cost::operator/= (HOST_WIDE_INT c)
293 if (infinite_cost_p ())
294 return *this;
296 this->cost /= c;
298 return *this;
301 comp_cost
302 comp_cost::operator*= (HOST_WIDE_INT c)
304 if (infinite_cost_p ())
305 return *this;
307 this->cost *= c;
309 return *this;
312 comp_cost
313 comp_cost::operator-= (comp_cost cost)
315 *this = *this - cost;
316 return *this;
319 bool
320 operator< (comp_cost cost1, comp_cost cost2)
322 if (cost1.cost == cost2.cost)
323 return cost1.complexity < cost2.complexity;
325 return cost1.cost < cost2.cost;
328 bool
329 operator== (comp_cost cost1, comp_cost cost2)
331 return cost1.cost == cost2.cost
332 && cost1.complexity == cost2.complexity;
335 bool
336 operator<= (comp_cost cost1, comp_cost cost2)
338 return cost1 < cost2 || cost1 == cost2;
341 struct iv_inv_expr_ent;
343 /* The candidate - cost pair. */
344 struct cost_pair
346 struct iv_cand *cand; /* The candidate. */
347 comp_cost cost; /* The cost. */
348 bitmap depends_on; /* The list of invariants that have to be
349 preserved. */
350 tree value; /* For final value elimination, the expression for
351 the final value of the iv. For iv elimination,
352 the new bound to compare with. */
353 enum tree_code comp; /* For iv elimination, the comparison. */
354 iv_inv_expr_ent *inv_expr; /* Loop invariant expression. */
357 /* Use. */
358 struct iv_use
360 unsigned id; /* The id of the use. */
361 unsigned group_id; /* The group id the use belongs to. */
362 enum use_type type; /* Type of the use. */
363 struct iv *iv; /* The induction variable it is based on. */
364 gimple *stmt; /* Statement in that it occurs. */
365 tree *op_p; /* The place where it occurs. */
367 tree addr_base; /* Base address with const offset stripped. */
368 unsigned HOST_WIDE_INT addr_offset;
369 /* Const offset stripped from base address. */
372 /* Group of uses. */
373 struct iv_group
375 /* The id of the group. */
376 unsigned id;
377 /* Uses of the group are of the same type. */
378 enum use_type type;
379 /* The set of "related" IV candidates, plus the important ones. */
380 bitmap related_cands;
381 /* Number of IV candidates in the cost_map. */
382 unsigned n_map_members;
383 /* The costs wrto the iv candidates. */
384 struct cost_pair *cost_map;
385 /* The selected candidate for the group. */
386 struct iv_cand *selected;
387 /* Uses in the group. */
388 vec<struct iv_use *> vuses;
391 /* The position where the iv is computed. */
392 enum iv_position
394 IP_NORMAL, /* At the end, just before the exit condition. */
395 IP_END, /* At the end of the latch block. */
396 IP_BEFORE_USE, /* Immediately before a specific use. */
397 IP_AFTER_USE, /* Immediately after a specific use. */
398 IP_ORIGINAL /* The original biv. */
401 /* The induction variable candidate. */
402 struct iv_cand
404 unsigned id; /* The number of the candidate. */
405 bool important; /* Whether this is an "important" candidate, i.e. such
406 that it should be considered by all uses. */
407 ENUM_BITFIELD(iv_position) pos : 8; /* Where it is computed. */
408 gimple *incremented_at;/* For original biv, the statement where it is
409 incremented. */
410 tree var_before; /* The variable used for it before increment. */
411 tree var_after; /* The variable used for it after increment. */
412 struct iv *iv; /* The value of the candidate. NULL for
413 "pseudocandidate" used to indicate the possibility
414 to replace the final value of an iv by direct
415 computation of the value. */
416 unsigned cost; /* Cost of the candidate. */
417 unsigned cost_step; /* Cost of the candidate's increment operation. */
418 struct iv_use *ainc_use; /* For IP_{BEFORE,AFTER}_USE candidates, the place
419 where it is incremented. */
420 bitmap depends_on; /* The list of invariants that are used in step of the
421 biv. */
422 struct iv *orig_iv; /* The original iv if this cand is added from biv with
423 smaller type. */
426 /* Hashtable entry for common candidate derived from iv uses. */
427 struct iv_common_cand
429 tree base;
430 tree step;
431 /* IV uses from which this common candidate is derived. */
432 auto_vec<struct iv_use *> uses;
433 hashval_t hash;
436 /* Hashtable helpers. */
438 struct iv_common_cand_hasher : delete_ptr_hash <iv_common_cand>
440 static inline hashval_t hash (const iv_common_cand *);
441 static inline bool equal (const iv_common_cand *, const iv_common_cand *);
444 /* Hash function for possible common candidates. */
446 inline hashval_t
447 iv_common_cand_hasher::hash (const iv_common_cand *ccand)
449 return ccand->hash;
452 /* Hash table equality function for common candidates. */
454 inline bool
455 iv_common_cand_hasher::equal (const iv_common_cand *ccand1,
456 const iv_common_cand *ccand2)
458 return (ccand1->hash == ccand2->hash
459 && operand_equal_p (ccand1->base, ccand2->base, 0)
460 && operand_equal_p (ccand1->step, ccand2->step, 0)
461 && (TYPE_PRECISION (TREE_TYPE (ccand1->base))
462 == TYPE_PRECISION (TREE_TYPE (ccand2->base))));
465 /* Loop invariant expression hashtable entry. */
467 struct iv_inv_expr_ent
469 /* Tree expression of the entry. */
470 tree expr;
471 /* Unique indentifier. */
472 int id;
473 /* Hash value. */
474 hashval_t hash;
477 /* Sort iv_inv_expr_ent pair A and B by id field. */
479 static int
480 sort_iv_inv_expr_ent (const void *a, const void *b)
482 const iv_inv_expr_ent * const *e1 = (const iv_inv_expr_ent * const *) (a);
483 const iv_inv_expr_ent * const *e2 = (const iv_inv_expr_ent * const *) (b);
485 unsigned id1 = (*e1)->id;
486 unsigned id2 = (*e2)->id;
488 if (id1 < id2)
489 return -1;
490 else if (id1 > id2)
491 return 1;
492 else
493 return 0;
496 /* Hashtable helpers. */
498 struct iv_inv_expr_hasher : free_ptr_hash <iv_inv_expr_ent>
500 static inline hashval_t hash (const iv_inv_expr_ent *);
501 static inline bool equal (const iv_inv_expr_ent *, const iv_inv_expr_ent *);
504 /* Hash function for loop invariant expressions. */
506 inline hashval_t
507 iv_inv_expr_hasher::hash (const iv_inv_expr_ent *expr)
509 return expr->hash;
512 /* Hash table equality function for expressions. */
514 inline bool
515 iv_inv_expr_hasher::equal (const iv_inv_expr_ent *expr1,
516 const iv_inv_expr_ent *expr2)
518 return expr1->hash == expr2->hash
519 && operand_equal_p (expr1->expr, expr2->expr, 0);
522 struct ivopts_data
524 /* The currently optimized loop. */
525 struct loop *current_loop;
526 source_location loop_loc;
528 /* Numbers of iterations for all exits of the current loop. */
529 hash_map<edge, tree_niter_desc *> *niters;
531 /* Number of registers used in it. */
532 unsigned regs_used;
534 /* The size of version_info array allocated. */
535 unsigned version_info_size;
537 /* The array of information for the ssa names. */
538 struct version_info *version_info;
540 /* The hashtable of loop invariant expressions created
541 by ivopt. */
542 hash_table<iv_inv_expr_hasher> *inv_expr_tab;
544 /* Loop invariant expression id. */
545 int max_inv_expr_id;
547 /* The bitmap of indices in version_info whose value was changed. */
548 bitmap relevant;
550 /* The uses of induction variables. */
551 vec<iv_group *> vgroups;
553 /* The candidates. */
554 vec<iv_cand *> vcands;
556 /* A bitmap of important candidates. */
557 bitmap important_candidates;
559 /* Cache used by tree_to_aff_combination_expand. */
560 hash_map<tree, name_expansion *> *name_expansion_cache;
562 /* The hashtable of common candidates derived from iv uses. */
563 hash_table<iv_common_cand_hasher> *iv_common_cand_tab;
565 /* The common candidates. */
566 vec<iv_common_cand *> iv_common_cands;
568 /* The maximum invariant id. */
569 unsigned max_inv_id;
571 /* Number of no_overflow BIVs which are not used in memory address. */
572 unsigned bivs_not_used_in_addr;
574 /* Obstack for iv structure. */
575 struct obstack iv_obstack;
577 /* Whether to consider just related and important candidates when replacing a
578 use. */
579 bool consider_all_candidates;
581 /* Are we optimizing for speed? */
582 bool speed;
584 /* Whether the loop body includes any function calls. */
585 bool body_includes_call;
587 /* Whether the loop body can only be exited via single exit. */
588 bool loop_single_exit_p;
591 /* An assignment of iv candidates to uses. */
593 struct iv_ca
595 /* The number of uses covered by the assignment. */
596 unsigned upto;
598 /* Number of uses that cannot be expressed by the candidates in the set. */
599 unsigned bad_groups;
601 /* Candidate assigned to a use, together with the related costs. */
602 struct cost_pair **cand_for_group;
604 /* Number of times each candidate is used. */
605 unsigned *n_cand_uses;
607 /* The candidates used. */
608 bitmap cands;
610 /* The number of candidates in the set. */
611 unsigned n_cands;
613 /* Total number of registers needed. */
614 unsigned n_regs;
616 /* Total cost of expressing uses. */
617 comp_cost cand_use_cost;
619 /* Total cost of candidates. */
620 unsigned cand_cost;
622 /* Number of times each invariant is used. */
623 unsigned *n_invariant_uses;
625 /* Hash set with used invariant expression. */
626 hash_map <iv_inv_expr_ent *, unsigned> *used_inv_exprs;
628 /* Total cost of the assignment. */
629 comp_cost cost;
632 /* Difference of two iv candidate assignments. */
634 struct iv_ca_delta
636 /* Changed group. */
637 struct iv_group *group;
639 /* An old assignment (for rollback purposes). */
640 struct cost_pair *old_cp;
642 /* A new assignment. */
643 struct cost_pair *new_cp;
645 /* Next change in the list. */
646 struct iv_ca_delta *next;
649 /* Bound on number of candidates below that all candidates are considered. */
651 #define CONSIDER_ALL_CANDIDATES_BOUND \
652 ((unsigned) PARAM_VALUE (PARAM_IV_CONSIDER_ALL_CANDIDATES_BOUND))
654 /* If there are more iv occurrences, we just give up (it is quite unlikely that
655 optimizing such a loop would help, and it would take ages). */
657 #define MAX_CONSIDERED_GROUPS \
658 ((unsigned) PARAM_VALUE (PARAM_IV_MAX_CONSIDERED_USES))
660 /* If there are at most this number of ivs in the set, try removing unnecessary
661 ivs from the set always. */
663 #define ALWAYS_PRUNE_CAND_SET_BOUND \
664 ((unsigned) PARAM_VALUE (PARAM_IV_ALWAYS_PRUNE_CAND_SET_BOUND))
666 /* The list of trees for that the decl_rtl field must be reset is stored
667 here. */
669 static vec<tree> decl_rtl_to_reset;
671 static comp_cost force_expr_to_var_cost (tree, bool);
673 /* The single loop exit if it dominates the latch, NULL otherwise. */
675 edge
676 single_dom_exit (struct loop *loop)
678 edge exit = single_exit (loop);
680 if (!exit)
681 return NULL;
683 if (!just_once_each_iteration_p (loop, exit->src))
684 return NULL;
686 return exit;
689 /* Dumps information about the induction variable IV to FILE. Don't dump
690 variable's name if DUMP_NAME is FALSE. The information is dumped with
691 preceding spaces indicated by INDENT_LEVEL. */
693 void
694 dump_iv (FILE *file, struct iv *iv, bool dump_name, unsigned indent_level)
696 const char *p;
697 const char spaces[9] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\0'};
699 if (indent_level > 4)
700 indent_level = 4;
701 p = spaces + 8 - (indent_level << 1);
703 fprintf (file, "%sIV struct:\n", p);
704 if (iv->ssa_name && dump_name)
706 fprintf (file, "%s SSA_NAME:\t", p);
707 print_generic_expr (file, iv->ssa_name, TDF_SLIM);
708 fprintf (file, "\n");
711 fprintf (file, "%s Type:\t", p);
712 print_generic_expr (file, TREE_TYPE (iv->base), TDF_SLIM);
713 fprintf (file, "\n");
715 fprintf (file, "%s Base:\t", p);
716 print_generic_expr (file, iv->base, TDF_SLIM);
717 fprintf (file, "\n");
719 fprintf (file, "%s Step:\t", p);
720 print_generic_expr (file, iv->step, TDF_SLIM);
721 fprintf (file, "\n");
723 if (iv->base_object)
725 fprintf (file, "%s Object:\t", p);
726 print_generic_expr (file, iv->base_object, TDF_SLIM);
727 fprintf (file, "\n");
730 fprintf (file, "%s Biv:\t%c\n", p, iv->biv_p ? 'Y' : 'N');
732 fprintf (file, "%s Overflowness wrto loop niter:\t%s\n",
733 p, iv->no_overflow ? "No-overflow" : "Overflow");
736 /* Dumps information about the USE to FILE. */
738 void
739 dump_use (FILE *file, struct iv_use *use)
741 fprintf (file, " Use %d.%d:\n", use->group_id, use->id);
742 fprintf (file, " At stmt:\t");
743 print_gimple_stmt (file, use->stmt, 0, 0);
744 fprintf (file, " At pos:\t");
745 if (use->op_p)
746 print_generic_expr (file, *use->op_p, TDF_SLIM);
747 fprintf (file, "\n");
748 dump_iv (file, use->iv, false, 2);
751 /* Dumps information about the uses to FILE. */
753 void
754 dump_groups (FILE *file, struct ivopts_data *data)
756 unsigned i, j;
757 struct iv_group *group;
759 for (i = 0; i < data->vgroups.length (); i++)
761 group = data->vgroups[i];
762 fprintf (file, "Group %d:\n", group->id);
763 if (group->type == USE_NONLINEAR_EXPR)
764 fprintf (file, " Type:\tGENERIC\n");
765 else if (group->type == USE_ADDRESS)
766 fprintf (file, " Type:\tADDRESS\n");
767 else
769 gcc_assert (group->type == USE_COMPARE);
770 fprintf (file, " Type:\tCOMPARE\n");
772 for (j = 0; j < group->vuses.length (); j++)
773 dump_use (file, group->vuses[j]);
777 /* Dumps information about induction variable candidate CAND to FILE. */
779 void
780 dump_cand (FILE *file, struct iv_cand *cand)
782 struct iv *iv = cand->iv;
784 fprintf (file, "Candidate %d:\n", cand->id);
785 if (cand->depends_on)
787 fprintf (file, " Depend on: ");
788 dump_bitmap (file, cand->depends_on);
791 if (cand->var_before)
793 fprintf (file, " Var befor: ");
794 print_generic_expr (file, cand->var_before, TDF_SLIM);
795 fprintf (file, "\n");
797 if (cand->var_after)
799 fprintf (file, " Var after: ");
800 print_generic_expr (file, cand->var_after, TDF_SLIM);
801 fprintf (file, "\n");
804 switch (cand->pos)
806 case IP_NORMAL:
807 fprintf (file, " Incr POS: before exit test\n");
808 break;
810 case IP_BEFORE_USE:
811 fprintf (file, " Incr POS: before use %d\n", cand->ainc_use->id);
812 break;
814 case IP_AFTER_USE:
815 fprintf (file, " Incr POS: after use %d\n", cand->ainc_use->id);
816 break;
818 case IP_END:
819 fprintf (file, " Incr POS: at end\n");
820 break;
822 case IP_ORIGINAL:
823 fprintf (file, " Incr POS: orig biv\n");
824 break;
827 dump_iv (file, iv, false, 1);
830 /* Returns the info for ssa version VER. */
832 static inline struct version_info *
833 ver_info (struct ivopts_data *data, unsigned ver)
835 return data->version_info + ver;
838 /* Returns the info for ssa name NAME. */
840 static inline struct version_info *
841 name_info (struct ivopts_data *data, tree name)
843 return ver_info (data, SSA_NAME_VERSION (name));
846 /* Returns true if STMT is after the place where the IP_NORMAL ivs will be
847 emitted in LOOP. */
849 static bool
850 stmt_after_ip_normal_pos (struct loop *loop, gimple *stmt)
852 basic_block bb = ip_normal_pos (loop), sbb = gimple_bb (stmt);
854 gcc_assert (bb);
856 if (sbb == loop->latch)
857 return true;
859 if (sbb != bb)
860 return false;
862 return stmt == last_stmt (bb);
865 /* Returns true if STMT if after the place where the original induction
866 variable CAND is incremented. If TRUE_IF_EQUAL is set, we return true
867 if the positions are identical. */
869 static bool
870 stmt_after_inc_pos (struct iv_cand *cand, gimple *stmt, bool true_if_equal)
872 basic_block cand_bb = gimple_bb (cand->incremented_at);
873 basic_block stmt_bb = gimple_bb (stmt);
875 if (!dominated_by_p (CDI_DOMINATORS, stmt_bb, cand_bb))
876 return false;
878 if (stmt_bb != cand_bb)
879 return true;
881 if (true_if_equal
882 && gimple_uid (stmt) == gimple_uid (cand->incremented_at))
883 return true;
884 return gimple_uid (stmt) > gimple_uid (cand->incremented_at);
887 /* Returns true if STMT if after the place where the induction variable
888 CAND is incremented in LOOP. */
890 static bool
891 stmt_after_increment (struct loop *loop, struct iv_cand *cand, gimple *stmt)
893 switch (cand->pos)
895 case IP_END:
896 return false;
898 case IP_NORMAL:
899 return stmt_after_ip_normal_pos (loop, stmt);
901 case IP_ORIGINAL:
902 case IP_AFTER_USE:
903 return stmt_after_inc_pos (cand, stmt, false);
905 case IP_BEFORE_USE:
906 return stmt_after_inc_pos (cand, stmt, true);
908 default:
909 gcc_unreachable ();
913 /* Returns true if EXP is a ssa name that occurs in an abnormal phi node. */
915 static bool
916 abnormal_ssa_name_p (tree exp)
918 if (!exp)
919 return false;
921 if (TREE_CODE (exp) != SSA_NAME)
922 return false;
924 return SSA_NAME_OCCURS_IN_ABNORMAL_PHI (exp) != 0;
927 /* Returns false if BASE or INDEX contains a ssa name that occurs in an
928 abnormal phi node. Callback for for_each_index. */
930 static bool
931 idx_contains_abnormal_ssa_name_p (tree base, tree *index,
932 void *data ATTRIBUTE_UNUSED)
934 if (TREE_CODE (base) == ARRAY_REF || TREE_CODE (base) == ARRAY_RANGE_REF)
936 if (abnormal_ssa_name_p (TREE_OPERAND (base, 2)))
937 return false;
938 if (abnormal_ssa_name_p (TREE_OPERAND (base, 3)))
939 return false;
942 return !abnormal_ssa_name_p (*index);
945 /* Returns true if EXPR contains a ssa name that occurs in an
946 abnormal phi node. */
948 bool
949 contains_abnormal_ssa_name_p (tree expr)
951 enum tree_code code;
952 enum tree_code_class codeclass;
954 if (!expr)
955 return false;
957 code = TREE_CODE (expr);
958 codeclass = TREE_CODE_CLASS (code);
960 if (code == SSA_NAME)
961 return SSA_NAME_OCCURS_IN_ABNORMAL_PHI (expr) != 0;
963 if (code == INTEGER_CST
964 || is_gimple_min_invariant (expr))
965 return false;
967 if (code == ADDR_EXPR)
968 return !for_each_index (&TREE_OPERAND (expr, 0),
969 idx_contains_abnormal_ssa_name_p,
970 NULL);
972 if (code == COND_EXPR)
973 return contains_abnormal_ssa_name_p (TREE_OPERAND (expr, 0))
974 || contains_abnormal_ssa_name_p (TREE_OPERAND (expr, 1))
975 || contains_abnormal_ssa_name_p (TREE_OPERAND (expr, 2));
977 switch (codeclass)
979 case tcc_binary:
980 case tcc_comparison:
981 if (contains_abnormal_ssa_name_p (TREE_OPERAND (expr, 1)))
982 return true;
984 /* Fallthru. */
985 case tcc_unary:
986 if (contains_abnormal_ssa_name_p (TREE_OPERAND (expr, 0)))
987 return true;
989 break;
991 default:
992 gcc_unreachable ();
995 return false;
998 /* Returns the structure describing number of iterations determined from
999 EXIT of DATA->current_loop, or NULL if something goes wrong. */
1001 static struct tree_niter_desc *
1002 niter_for_exit (struct ivopts_data *data, edge exit)
1004 struct tree_niter_desc *desc;
1005 tree_niter_desc **slot;
1007 if (!data->niters)
1009 data->niters = new hash_map<edge, tree_niter_desc *>;
1010 slot = NULL;
1012 else
1013 slot = data->niters->get (exit);
1015 if (!slot)
1017 /* Try to determine number of iterations. We cannot safely work with ssa
1018 names that appear in phi nodes on abnormal edges, so that we do not
1019 create overlapping life ranges for them (PR 27283). */
1020 desc = XNEW (struct tree_niter_desc);
1021 if (!number_of_iterations_exit (data->current_loop,
1022 exit, desc, true)
1023 || contains_abnormal_ssa_name_p (desc->niter))
1025 XDELETE (desc);
1026 desc = NULL;
1028 data->niters->put (exit, desc);
1030 else
1031 desc = *slot;
1033 return desc;
1036 /* Returns the structure describing number of iterations determined from
1037 single dominating exit of DATA->current_loop, or NULL if something
1038 goes wrong. */
1040 static struct tree_niter_desc *
1041 niter_for_single_dom_exit (struct ivopts_data *data)
1043 edge exit = single_dom_exit (data->current_loop);
1045 if (!exit)
1046 return NULL;
1048 return niter_for_exit (data, exit);
1051 /* Initializes data structures used by the iv optimization pass, stored
1052 in DATA. */
1054 static void
1055 tree_ssa_iv_optimize_init (struct ivopts_data *data)
1057 data->version_info_size = 2 * num_ssa_names;
1058 data->version_info = XCNEWVEC (struct version_info, data->version_info_size);
1059 data->relevant = BITMAP_ALLOC (NULL);
1060 data->important_candidates = BITMAP_ALLOC (NULL);
1061 data->max_inv_id = 0;
1062 data->niters = NULL;
1063 data->vgroups.create (20);
1064 data->vcands.create (20);
1065 data->inv_expr_tab = new hash_table<iv_inv_expr_hasher> (10);
1066 data->max_inv_expr_id = 0;
1067 data->name_expansion_cache = NULL;
1068 data->iv_common_cand_tab = new hash_table<iv_common_cand_hasher> (10);
1069 data->iv_common_cands.create (20);
1070 decl_rtl_to_reset.create (20);
1071 gcc_obstack_init (&data->iv_obstack);
1074 /* Returns a memory object to that EXPR points. In case we are able to
1075 determine that it does not point to any such object, NULL is returned. */
1077 static tree
1078 determine_base_object (tree expr)
1080 enum tree_code code = TREE_CODE (expr);
1081 tree base, obj;
1083 /* If this is a pointer casted to any type, we need to determine
1084 the base object for the pointer; so handle conversions before
1085 throwing away non-pointer expressions. */
1086 if (CONVERT_EXPR_P (expr))
1087 return determine_base_object (TREE_OPERAND (expr, 0));
1089 if (!POINTER_TYPE_P (TREE_TYPE (expr)))
1090 return NULL_TREE;
1092 switch (code)
1094 case INTEGER_CST:
1095 return NULL_TREE;
1097 case ADDR_EXPR:
1098 obj = TREE_OPERAND (expr, 0);
1099 base = get_base_address (obj);
1101 if (!base)
1102 return expr;
1104 if (TREE_CODE (base) == MEM_REF)
1105 return determine_base_object (TREE_OPERAND (base, 0));
1107 return fold_convert (ptr_type_node,
1108 build_fold_addr_expr (base));
1110 case POINTER_PLUS_EXPR:
1111 return determine_base_object (TREE_OPERAND (expr, 0));
1113 case PLUS_EXPR:
1114 case MINUS_EXPR:
1115 /* Pointer addition is done solely using POINTER_PLUS_EXPR. */
1116 gcc_unreachable ();
1118 default:
1119 return fold_convert (ptr_type_node, expr);
1123 /* Return true if address expression with non-DECL_P operand appears
1124 in EXPR. */
1126 static bool
1127 contain_complex_addr_expr (tree expr)
1129 bool res = false;
1131 STRIP_NOPS (expr);
1132 switch (TREE_CODE (expr))
1134 case POINTER_PLUS_EXPR:
1135 case PLUS_EXPR:
1136 case MINUS_EXPR:
1137 res |= contain_complex_addr_expr (TREE_OPERAND (expr, 0));
1138 res |= contain_complex_addr_expr (TREE_OPERAND (expr, 1));
1139 break;
1141 case ADDR_EXPR:
1142 return (!DECL_P (TREE_OPERAND (expr, 0)));
1144 default:
1145 return false;
1148 return res;
1151 /* Allocates an induction variable with given initial value BASE and step STEP
1152 for loop LOOP. NO_OVERFLOW implies the iv doesn't overflow. */
1154 static struct iv *
1155 alloc_iv (struct ivopts_data *data, tree base, tree step,
1156 bool no_overflow = false)
1158 tree expr = base;
1159 struct iv *iv = (struct iv*) obstack_alloc (&data->iv_obstack,
1160 sizeof (struct iv));
1161 gcc_assert (step != NULL_TREE);
1163 /* Lower address expression in base except ones with DECL_P as operand.
1164 By doing this:
1165 1) More accurate cost can be computed for address expressions;
1166 2) Duplicate candidates won't be created for bases in different
1167 forms, like &a[0] and &a. */
1168 STRIP_NOPS (expr);
1169 if ((TREE_CODE (expr) == ADDR_EXPR && !DECL_P (TREE_OPERAND (expr, 0)))
1170 || contain_complex_addr_expr (expr))
1172 aff_tree comb;
1173 tree_to_aff_combination (expr, TREE_TYPE (base), &comb);
1174 base = fold_convert (TREE_TYPE (base), aff_combination_to_tree (&comb));
1177 iv->base = base;
1178 iv->base_object = determine_base_object (base);
1179 iv->step = step;
1180 iv->biv_p = false;
1181 iv->nonlin_use = NULL;
1182 iv->ssa_name = NULL_TREE;
1183 if (!no_overflow
1184 && !iv_can_overflow_p (data->current_loop, TREE_TYPE (base),
1185 base, step))
1186 no_overflow = true;
1187 iv->no_overflow = no_overflow;
1188 iv->have_address_use = false;
1190 return iv;
1193 /* Sets STEP and BASE for induction variable IV. NO_OVERFLOW implies the IV
1194 doesn't overflow. */
1196 static void
1197 set_iv (struct ivopts_data *data, tree iv, tree base, tree step,
1198 bool no_overflow)
1200 struct version_info *info = name_info (data, iv);
1202 gcc_assert (!info->iv);
1204 bitmap_set_bit (data->relevant, SSA_NAME_VERSION (iv));
1205 info->iv = alloc_iv (data, base, step, no_overflow);
1206 info->iv->ssa_name = iv;
1209 /* Finds induction variable declaration for VAR. */
1211 static struct iv *
1212 get_iv (struct ivopts_data *data, tree var)
1214 basic_block bb;
1215 tree type = TREE_TYPE (var);
1217 if (!POINTER_TYPE_P (type)
1218 && !INTEGRAL_TYPE_P (type))
1219 return NULL;
1221 if (!name_info (data, var)->iv)
1223 bb = gimple_bb (SSA_NAME_DEF_STMT (var));
1225 if (!bb
1226 || !flow_bb_inside_loop_p (data->current_loop, bb))
1227 set_iv (data, var, var, build_int_cst (type, 0), true);
1230 return name_info (data, var)->iv;
1233 /* Return the first non-invariant ssa var found in EXPR. */
1235 static tree
1236 extract_single_var_from_expr (tree expr)
1238 int i, n;
1239 tree tmp;
1240 enum tree_code code;
1242 if (!expr || is_gimple_min_invariant (expr))
1243 return NULL;
1245 code = TREE_CODE (expr);
1246 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
1248 n = TREE_OPERAND_LENGTH (expr);
1249 for (i = 0; i < n; i++)
1251 tmp = extract_single_var_from_expr (TREE_OPERAND (expr, i));
1253 if (tmp)
1254 return tmp;
1257 return (TREE_CODE (expr) == SSA_NAME) ? expr : NULL;
1260 /* Finds basic ivs. */
1262 static bool
1263 find_bivs (struct ivopts_data *data)
1265 gphi *phi;
1266 affine_iv iv;
1267 tree step, type, base, stop;
1268 bool found = false;
1269 struct loop *loop = data->current_loop;
1270 gphi_iterator psi;
1272 for (psi = gsi_start_phis (loop->header); !gsi_end_p (psi); gsi_next (&psi))
1274 phi = psi.phi ();
1276 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
1277 continue;
1279 if (virtual_operand_p (PHI_RESULT (phi)))
1280 continue;
1282 if (!simple_iv (loop, loop, PHI_RESULT (phi), &iv, true))
1283 continue;
1285 if (integer_zerop (iv.step))
1286 continue;
1288 step = iv.step;
1289 base = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop));
1290 /* Stop expanding iv base at the first ssa var referred by iv step.
1291 Ideally we should stop at any ssa var, because that's expensive
1292 and unusual to happen, we just do it on the first one.
1294 See PR64705 for the rationale. */
1295 stop = extract_single_var_from_expr (step);
1296 base = expand_simple_operations (base, stop);
1297 if (contains_abnormal_ssa_name_p (base)
1298 || contains_abnormal_ssa_name_p (step))
1299 continue;
1301 type = TREE_TYPE (PHI_RESULT (phi));
1302 base = fold_convert (type, base);
1303 if (step)
1305 if (POINTER_TYPE_P (type))
1306 step = convert_to_ptrofftype (step);
1307 else
1308 step = fold_convert (type, step);
1311 set_iv (data, PHI_RESULT (phi), base, step, iv.no_overflow);
1312 found = true;
1315 return found;
1318 /* Marks basic ivs. */
1320 static void
1321 mark_bivs (struct ivopts_data *data)
1323 gphi *phi;
1324 gimple *def;
1325 tree var;
1326 struct iv *iv, *incr_iv;
1327 struct loop *loop = data->current_loop;
1328 basic_block incr_bb;
1329 gphi_iterator psi;
1331 data->bivs_not_used_in_addr = 0;
1332 for (psi = gsi_start_phis (loop->header); !gsi_end_p (psi); gsi_next (&psi))
1334 phi = psi.phi ();
1336 iv = get_iv (data, PHI_RESULT (phi));
1337 if (!iv)
1338 continue;
1340 var = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (loop));
1341 def = SSA_NAME_DEF_STMT (var);
1342 /* Don't mark iv peeled from other one as biv. */
1343 if (def
1344 && gimple_code (def) == GIMPLE_PHI
1345 && gimple_bb (def) == loop->header)
1346 continue;
1348 incr_iv = get_iv (data, var);
1349 if (!incr_iv)
1350 continue;
1352 /* If the increment is in the subloop, ignore it. */
1353 incr_bb = gimple_bb (SSA_NAME_DEF_STMT (var));
1354 if (incr_bb->loop_father != data->current_loop
1355 || (incr_bb->flags & BB_IRREDUCIBLE_LOOP))
1356 continue;
1358 iv->biv_p = true;
1359 incr_iv->biv_p = true;
1360 if (iv->no_overflow)
1361 data->bivs_not_used_in_addr++;
1362 if (incr_iv->no_overflow)
1363 data->bivs_not_used_in_addr++;
1367 /* Checks whether STMT defines a linear induction variable and stores its
1368 parameters to IV. */
1370 static bool
1371 find_givs_in_stmt_scev (struct ivopts_data *data, gimple *stmt, affine_iv *iv)
1373 tree lhs, stop;
1374 struct loop *loop = data->current_loop;
1376 iv->base = NULL_TREE;
1377 iv->step = NULL_TREE;
1379 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1380 return false;
1382 lhs = gimple_assign_lhs (stmt);
1383 if (TREE_CODE (lhs) != SSA_NAME)
1384 return false;
1386 if (!simple_iv (loop, loop_containing_stmt (stmt), lhs, iv, true))
1387 return false;
1389 /* Stop expanding iv base at the first ssa var referred by iv step.
1390 Ideally we should stop at any ssa var, because that's expensive
1391 and unusual to happen, we just do it on the first one.
1393 See PR64705 for the rationale. */
1394 stop = extract_single_var_from_expr (iv->step);
1395 iv->base = expand_simple_operations (iv->base, stop);
1396 if (contains_abnormal_ssa_name_p (iv->base)
1397 || contains_abnormal_ssa_name_p (iv->step))
1398 return false;
1400 /* If STMT could throw, then do not consider STMT as defining a GIV.
1401 While this will suppress optimizations, we can not safely delete this
1402 GIV and associated statements, even if it appears it is not used. */
1403 if (stmt_could_throw_p (stmt))
1404 return false;
1406 return true;
1409 /* Finds general ivs in statement STMT. */
1411 static void
1412 find_givs_in_stmt (struct ivopts_data *data, gimple *stmt)
1414 affine_iv iv;
1416 if (!find_givs_in_stmt_scev (data, stmt, &iv))
1417 return;
1419 set_iv (data, gimple_assign_lhs (stmt), iv.base, iv.step, iv.no_overflow);
1422 /* Finds general ivs in basic block BB. */
1424 static void
1425 find_givs_in_bb (struct ivopts_data *data, basic_block bb)
1427 gimple_stmt_iterator bsi;
1429 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1430 find_givs_in_stmt (data, gsi_stmt (bsi));
1433 /* Finds general ivs. */
1435 static void
1436 find_givs (struct ivopts_data *data)
1438 struct loop *loop = data->current_loop;
1439 basic_block *body = get_loop_body_in_dom_order (loop);
1440 unsigned i;
1442 for (i = 0; i < loop->num_nodes; i++)
1443 find_givs_in_bb (data, body[i]);
1444 free (body);
1447 /* For each ssa name defined in LOOP determines whether it is an induction
1448 variable and if so, its initial value and step. */
1450 static bool
1451 find_induction_variables (struct ivopts_data *data)
1453 unsigned i;
1454 bitmap_iterator bi;
1456 if (!find_bivs (data))
1457 return false;
1459 find_givs (data);
1460 mark_bivs (data);
1462 if (dump_file && (dump_flags & TDF_DETAILS))
1464 struct tree_niter_desc *niter = niter_for_single_dom_exit (data);
1466 if (niter)
1468 fprintf (dump_file, " number of iterations ");
1469 print_generic_expr (dump_file, niter->niter, TDF_SLIM);
1470 if (!integer_zerop (niter->may_be_zero))
1472 fprintf (dump_file, "; zero if ");
1473 print_generic_expr (dump_file, niter->may_be_zero, TDF_SLIM);
1475 fprintf (dump_file, "\n");
1478 fprintf (dump_file, "\n<Induction Vars>:\n");
1479 EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, i, bi)
1481 struct version_info *info = ver_info (data, i);
1482 if (info->iv && info->iv->step && !integer_zerop (info->iv->step))
1483 dump_iv (dump_file, ver_info (data, i)->iv, true, 0);
1487 return true;
1490 /* Records a use of TYPE at *USE_P in STMT whose value is IV in GROUP.
1491 For address type use, ADDR_BASE is the stripped IV base, ADDR_OFFSET
1492 is the const offset stripped from IV base; for other types use, both
1493 are zero by default. */
1495 static struct iv_use *
1496 record_use (struct iv_group *group, tree *use_p, struct iv *iv,
1497 gimple *stmt, enum use_type type, tree addr_base,
1498 unsigned HOST_WIDE_INT addr_offset)
1500 struct iv_use *use = XCNEW (struct iv_use);
1502 use->id = group->vuses.length ();
1503 use->group_id = group->id;
1504 use->type = type;
1505 use->iv = iv;
1506 use->stmt = stmt;
1507 use->op_p = use_p;
1508 use->addr_base = addr_base;
1509 use->addr_offset = addr_offset;
1511 group->vuses.safe_push (use);
1512 return use;
1515 /* Checks whether OP is a loop-level invariant and if so, records it.
1516 NONLINEAR_USE is true if the invariant is used in a way we do not
1517 handle specially. */
1519 static void
1520 record_invariant (struct ivopts_data *data, tree op, bool nonlinear_use)
1522 basic_block bb;
1523 struct version_info *info;
1525 if (TREE_CODE (op) != SSA_NAME
1526 || virtual_operand_p (op))
1527 return;
1529 bb = gimple_bb (SSA_NAME_DEF_STMT (op));
1530 if (bb
1531 && flow_bb_inside_loop_p (data->current_loop, bb))
1532 return;
1534 info = name_info (data, op);
1535 info->name = op;
1536 info->has_nonlin_use |= nonlinear_use;
1537 if (!info->inv_id)
1538 info->inv_id = ++data->max_inv_id;
1539 bitmap_set_bit (data->relevant, SSA_NAME_VERSION (op));
1542 static tree
1543 strip_offset (tree expr, unsigned HOST_WIDE_INT *offset);
1545 /* Record a group of TYPE. */
1547 static struct iv_group *
1548 record_group (struct ivopts_data *data, enum use_type type)
1550 struct iv_group *group = XCNEW (struct iv_group);
1552 group->id = data->vgroups.length ();
1553 group->type = type;
1554 group->related_cands = BITMAP_ALLOC (NULL);
1555 group->vuses.create (1);
1557 data->vgroups.safe_push (group);
1558 return group;
1561 /* Record a use of TYPE at *USE_P in STMT whose value is IV in a group.
1562 New group will be created if there is no existing group for the use. */
1564 static struct iv_use *
1565 record_group_use (struct ivopts_data *data, tree *use_p,
1566 struct iv *iv, gimple *stmt, enum use_type type)
1568 tree addr_base = NULL;
1569 struct iv_group *group = NULL;
1570 unsigned HOST_WIDE_INT addr_offset = 0;
1572 /* Record non address type use in a new group. */
1573 if (type == USE_ADDRESS && iv->base_object)
1575 unsigned int i;
1577 addr_base = strip_offset (iv->base, &addr_offset);
1578 for (i = 0; i < data->vgroups.length (); i++)
1580 struct iv_use *use;
1582 group = data->vgroups[i];
1583 use = group->vuses[0];
1584 if (use->type != USE_ADDRESS || !use->iv->base_object)
1585 continue;
1587 /* Check if it has the same stripped base and step. */
1588 if (operand_equal_p (iv->base_object, use->iv->base_object, 0)
1589 && operand_equal_p (iv->step, use->iv->step, 0)
1590 && operand_equal_p (addr_base, use->addr_base, 0))
1591 break;
1593 if (i == data->vgroups.length ())
1594 group = NULL;
1597 if (!group)
1598 group = record_group (data, type);
1600 return record_use (group, use_p, iv, stmt, type, addr_base, addr_offset);
1603 /* Checks whether the use OP is interesting and if so, records it. */
1605 static struct iv_use *
1606 find_interesting_uses_op (struct ivopts_data *data, tree op)
1608 struct iv *iv;
1609 gimple *stmt;
1610 struct iv_use *use;
1612 if (TREE_CODE (op) != SSA_NAME)
1613 return NULL;
1615 iv = get_iv (data, op);
1616 if (!iv)
1617 return NULL;
1619 if (iv->nonlin_use)
1621 gcc_assert (iv->nonlin_use->type == USE_NONLINEAR_EXPR);
1622 return iv->nonlin_use;
1625 if (integer_zerop (iv->step))
1627 record_invariant (data, op, true);
1628 return NULL;
1631 stmt = SSA_NAME_DEF_STMT (op);
1632 gcc_assert (gimple_code (stmt) == GIMPLE_PHI || is_gimple_assign (stmt));
1634 use = record_group_use (data, NULL, iv, stmt, USE_NONLINEAR_EXPR);
1635 iv->nonlin_use = use;
1636 return use;
1639 /* Given a condition in statement STMT, checks whether it is a compare
1640 of an induction variable and an invariant. If this is the case,
1641 CONTROL_VAR is set to location of the iv, BOUND to the location of
1642 the invariant, IV_VAR and IV_BOUND are set to the corresponding
1643 induction variable descriptions, and true is returned. If this is not
1644 the case, CONTROL_VAR and BOUND are set to the arguments of the
1645 condition and false is returned. */
1647 static bool
1648 extract_cond_operands (struct ivopts_data *data, gimple *stmt,
1649 tree **control_var, tree **bound,
1650 struct iv **iv_var, struct iv **iv_bound)
1652 /* The objects returned when COND has constant operands. */
1653 static struct iv const_iv;
1654 static tree zero;
1655 tree *op0 = &zero, *op1 = &zero;
1656 struct iv *iv0 = &const_iv, *iv1 = &const_iv;
1657 bool ret = false;
1659 if (gimple_code (stmt) == GIMPLE_COND)
1661 gcond *cond_stmt = as_a <gcond *> (stmt);
1662 op0 = gimple_cond_lhs_ptr (cond_stmt);
1663 op1 = gimple_cond_rhs_ptr (cond_stmt);
1665 else
1667 op0 = gimple_assign_rhs1_ptr (stmt);
1668 op1 = gimple_assign_rhs2_ptr (stmt);
1671 zero = integer_zero_node;
1672 const_iv.step = integer_zero_node;
1674 if (TREE_CODE (*op0) == SSA_NAME)
1675 iv0 = get_iv (data, *op0);
1676 if (TREE_CODE (*op1) == SSA_NAME)
1677 iv1 = get_iv (data, *op1);
1679 /* Exactly one of the compared values must be an iv, and the other one must
1680 be an invariant. */
1681 if (!iv0 || !iv1)
1682 goto end;
1684 if (integer_zerop (iv0->step))
1686 /* Control variable may be on the other side. */
1687 std::swap (op0, op1);
1688 std::swap (iv0, iv1);
1690 ret = !integer_zerop (iv0->step) && integer_zerop (iv1->step);
1692 end:
1693 if (control_var)
1694 *control_var = op0;
1695 if (iv_var)
1696 *iv_var = iv0;
1697 if (bound)
1698 *bound = op1;
1699 if (iv_bound)
1700 *iv_bound = iv1;
1702 return ret;
1705 /* Checks whether the condition in STMT is interesting and if so,
1706 records it. */
1708 static void
1709 find_interesting_uses_cond (struct ivopts_data *data, gimple *stmt)
1711 tree *var_p, *bound_p;
1712 struct iv *var_iv;
1714 if (!extract_cond_operands (data, stmt, &var_p, &bound_p, &var_iv, NULL))
1716 find_interesting_uses_op (data, *var_p);
1717 find_interesting_uses_op (data, *bound_p);
1718 return;
1721 record_group_use (data, NULL, var_iv, stmt, USE_COMPARE);
1724 /* Returns the outermost loop EXPR is obviously invariant in
1725 relative to the loop LOOP, i.e. if all its operands are defined
1726 outside of the returned loop. Returns NULL if EXPR is not
1727 even obviously invariant in LOOP. */
1729 struct loop *
1730 outermost_invariant_loop_for_expr (struct loop *loop, tree expr)
1732 basic_block def_bb;
1733 unsigned i, len;
1735 if (is_gimple_min_invariant (expr))
1736 return current_loops->tree_root;
1738 if (TREE_CODE (expr) == SSA_NAME)
1740 def_bb = gimple_bb (SSA_NAME_DEF_STMT (expr));
1741 if (def_bb)
1743 if (flow_bb_inside_loop_p (loop, def_bb))
1744 return NULL;
1745 return superloop_at_depth (loop,
1746 loop_depth (def_bb->loop_father) + 1);
1749 return current_loops->tree_root;
1752 if (!EXPR_P (expr))
1753 return NULL;
1755 unsigned maxdepth = 0;
1756 len = TREE_OPERAND_LENGTH (expr);
1757 for (i = 0; i < len; i++)
1759 struct loop *ivloop;
1760 if (!TREE_OPERAND (expr, i))
1761 continue;
1763 ivloop = outermost_invariant_loop_for_expr (loop, TREE_OPERAND (expr, i));
1764 if (!ivloop)
1765 return NULL;
1766 maxdepth = MAX (maxdepth, loop_depth (ivloop));
1769 return superloop_at_depth (loop, maxdepth);
1772 /* Returns true if expression EXPR is obviously invariant in LOOP,
1773 i.e. if all its operands are defined outside of the LOOP. LOOP
1774 should not be the function body. */
1776 bool
1777 expr_invariant_in_loop_p (struct loop *loop, tree expr)
1779 basic_block def_bb;
1780 unsigned i, len;
1782 gcc_assert (loop_depth (loop) > 0);
1784 if (is_gimple_min_invariant (expr))
1785 return true;
1787 if (TREE_CODE (expr) == SSA_NAME)
1789 def_bb = gimple_bb (SSA_NAME_DEF_STMT (expr));
1790 if (def_bb
1791 && flow_bb_inside_loop_p (loop, def_bb))
1792 return false;
1794 return true;
1797 if (!EXPR_P (expr))
1798 return false;
1800 len = TREE_OPERAND_LENGTH (expr);
1801 for (i = 0; i < len; i++)
1802 if (TREE_OPERAND (expr, i)
1803 && !expr_invariant_in_loop_p (loop, TREE_OPERAND (expr, i)))
1804 return false;
1806 return true;
1809 /* Given expression EXPR which computes inductive values with respect
1810 to loop recorded in DATA, this function returns biv from which EXPR
1811 is derived by tracing definition chains of ssa variables in EXPR. */
1813 static struct iv*
1814 find_deriving_biv_for_expr (struct ivopts_data *data, tree expr)
1816 struct iv *iv;
1817 unsigned i, n;
1818 tree e2, e1;
1819 enum tree_code code;
1820 gimple *stmt;
1822 if (expr == NULL_TREE)
1823 return NULL;
1825 if (is_gimple_min_invariant (expr))
1826 return NULL;
1828 code = TREE_CODE (expr);
1829 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
1831 n = TREE_OPERAND_LENGTH (expr);
1832 for (i = 0; i < n; i++)
1834 iv = find_deriving_biv_for_expr (data, TREE_OPERAND (expr, i));
1835 if (iv)
1836 return iv;
1840 /* Stop if it's not ssa name. */
1841 if (code != SSA_NAME)
1842 return NULL;
1844 iv = get_iv (data, expr);
1845 if (!iv || integer_zerop (iv->step))
1846 return NULL;
1847 else if (iv->biv_p)
1848 return iv;
1850 stmt = SSA_NAME_DEF_STMT (expr);
1851 if (gphi *phi = dyn_cast <gphi *> (stmt))
1853 ssa_op_iter iter;
1854 use_operand_p use_p;
1856 if (virtual_operand_p (gimple_phi_result (phi)))
1857 return NULL;
1859 FOR_EACH_PHI_ARG (use_p, phi, iter, SSA_OP_USE)
1861 tree use = USE_FROM_PTR (use_p);
1862 iv = find_deriving_biv_for_expr (data, use);
1863 if (iv)
1864 return iv;
1866 return NULL;
1868 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1869 return NULL;
1871 e1 = gimple_assign_rhs1 (stmt);
1872 code = gimple_assign_rhs_code (stmt);
1873 if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS)
1874 return find_deriving_biv_for_expr (data, e1);
1876 switch (code)
1878 case MULT_EXPR:
1879 case PLUS_EXPR:
1880 case MINUS_EXPR:
1881 case POINTER_PLUS_EXPR:
1882 /* Increments, decrements and multiplications by a constant
1883 are simple. */
1884 e2 = gimple_assign_rhs2 (stmt);
1885 iv = find_deriving_biv_for_expr (data, e2);
1886 if (iv)
1887 return iv;
1889 /* Fallthru. */
1890 CASE_CONVERT:
1891 /* Casts are simple. */
1892 return find_deriving_biv_for_expr (data, e1);
1894 default:
1895 break;
1898 return NULL;
1901 /* Record BIV, its predecessor and successor that they are used in
1902 address type uses. */
1904 static void
1905 record_biv_for_address_use (struct ivopts_data *data, struct iv *biv)
1907 unsigned i;
1908 tree type, base_1, base_2;
1909 bitmap_iterator bi;
1911 if (!biv || !biv->biv_p || integer_zerop (biv->step)
1912 || biv->have_address_use || !biv->no_overflow)
1913 return;
1915 type = TREE_TYPE (biv->base);
1916 if (!INTEGRAL_TYPE_P (type))
1917 return;
1919 biv->have_address_use = true;
1920 data->bivs_not_used_in_addr--;
1921 base_1 = fold_build2 (PLUS_EXPR, type, biv->base, biv->step);
1922 EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, i, bi)
1924 struct iv *iv = ver_info (data, i)->iv;
1926 if (!iv || !iv->biv_p || integer_zerop (iv->step)
1927 || iv->have_address_use || !iv->no_overflow)
1928 continue;
1930 if (type != TREE_TYPE (iv->base)
1931 || !INTEGRAL_TYPE_P (TREE_TYPE (iv->base)))
1932 continue;
1934 if (!operand_equal_p (biv->step, iv->step, 0))
1935 continue;
1937 base_2 = fold_build2 (PLUS_EXPR, type, iv->base, iv->step);
1938 if (operand_equal_p (base_1, iv->base, 0)
1939 || operand_equal_p (base_2, biv->base, 0))
1941 iv->have_address_use = true;
1942 data->bivs_not_used_in_addr--;
1947 /* Cumulates the steps of indices into DATA and replaces their values with the
1948 initial ones. Returns false when the value of the index cannot be determined.
1949 Callback for for_each_index. */
1951 struct ifs_ivopts_data
1953 struct ivopts_data *ivopts_data;
1954 gimple *stmt;
1955 tree step;
1958 static bool
1959 idx_find_step (tree base, tree *idx, void *data)
1961 struct ifs_ivopts_data *dta = (struct ifs_ivopts_data *) data;
1962 struct iv *iv;
1963 bool use_overflow_semantics = false;
1964 tree step, iv_base, iv_step, lbound, off;
1965 struct loop *loop = dta->ivopts_data->current_loop;
1967 /* If base is a component ref, require that the offset of the reference
1968 be invariant. */
1969 if (TREE_CODE (base) == COMPONENT_REF)
1971 off = component_ref_field_offset (base);
1972 return expr_invariant_in_loop_p (loop, off);
1975 /* If base is array, first check whether we will be able to move the
1976 reference out of the loop (in order to take its address in strength
1977 reduction). In order for this to work we need both lower bound
1978 and step to be loop invariants. */
1979 if (TREE_CODE (base) == ARRAY_REF || TREE_CODE (base) == ARRAY_RANGE_REF)
1981 /* Moreover, for a range, the size needs to be invariant as well. */
1982 if (TREE_CODE (base) == ARRAY_RANGE_REF
1983 && !expr_invariant_in_loop_p (loop, TYPE_SIZE (TREE_TYPE (base))))
1984 return false;
1986 step = array_ref_element_size (base);
1987 lbound = array_ref_low_bound (base);
1989 if (!expr_invariant_in_loop_p (loop, step)
1990 || !expr_invariant_in_loop_p (loop, lbound))
1991 return false;
1994 if (TREE_CODE (*idx) != SSA_NAME)
1995 return true;
1997 iv = get_iv (dta->ivopts_data, *idx);
1998 if (!iv)
1999 return false;
2001 /* XXX We produce for a base of *D42 with iv->base being &x[0]
2002 *&x[0], which is not folded and does not trigger the
2003 ARRAY_REF path below. */
2004 *idx = iv->base;
2006 if (integer_zerop (iv->step))
2007 return true;
2009 if (TREE_CODE (base) == ARRAY_REF || TREE_CODE (base) == ARRAY_RANGE_REF)
2011 step = array_ref_element_size (base);
2013 /* We only handle addresses whose step is an integer constant. */
2014 if (TREE_CODE (step) != INTEGER_CST)
2015 return false;
2017 else
2018 /* The step for pointer arithmetics already is 1 byte. */
2019 step = size_one_node;
2021 iv_base = iv->base;
2022 iv_step = iv->step;
2023 if (iv->no_overflow && nowrap_type_p (TREE_TYPE (iv_step)))
2024 use_overflow_semantics = true;
2026 if (!convert_affine_scev (dta->ivopts_data->current_loop,
2027 sizetype, &iv_base, &iv_step, dta->stmt,
2028 use_overflow_semantics))
2030 /* The index might wrap. */
2031 return false;
2034 step = fold_build2 (MULT_EXPR, sizetype, step, iv_step);
2035 dta->step = fold_build2 (PLUS_EXPR, sizetype, dta->step, step);
2037 if (dta->ivopts_data->bivs_not_used_in_addr)
2039 if (!iv->biv_p)
2040 iv = find_deriving_biv_for_expr (dta->ivopts_data, iv->ssa_name);
2042 record_biv_for_address_use (dta->ivopts_data, iv);
2044 return true;
2047 /* Records use in index IDX. Callback for for_each_index. Ivopts data
2048 object is passed to it in DATA. */
2050 static bool
2051 idx_record_use (tree base, tree *idx,
2052 void *vdata)
2054 struct ivopts_data *data = (struct ivopts_data *) vdata;
2055 find_interesting_uses_op (data, *idx);
2056 if (TREE_CODE (base) == ARRAY_REF || TREE_CODE (base) == ARRAY_RANGE_REF)
2058 find_interesting_uses_op (data, array_ref_element_size (base));
2059 find_interesting_uses_op (data, array_ref_low_bound (base));
2061 return true;
2064 /* If we can prove that TOP = cst * BOT for some constant cst,
2065 store cst to MUL and return true. Otherwise return false.
2066 The returned value is always sign-extended, regardless of the
2067 signedness of TOP and BOT. */
2069 static bool
2070 constant_multiple_of (tree top, tree bot, widest_int *mul)
2072 tree mby;
2073 enum tree_code code;
2074 unsigned precision = TYPE_PRECISION (TREE_TYPE (top));
2075 widest_int res, p0, p1;
2077 STRIP_NOPS (top);
2078 STRIP_NOPS (bot);
2080 if (operand_equal_p (top, bot, 0))
2082 *mul = 1;
2083 return true;
2086 code = TREE_CODE (top);
2087 switch (code)
2089 case MULT_EXPR:
2090 mby = TREE_OPERAND (top, 1);
2091 if (TREE_CODE (mby) != INTEGER_CST)
2092 return false;
2094 if (!constant_multiple_of (TREE_OPERAND (top, 0), bot, &res))
2095 return false;
2097 *mul = wi::sext (res * wi::to_widest (mby), precision);
2098 return true;
2100 case PLUS_EXPR:
2101 case MINUS_EXPR:
2102 if (!constant_multiple_of (TREE_OPERAND (top, 0), bot, &p0)
2103 || !constant_multiple_of (TREE_OPERAND (top, 1), bot, &p1))
2104 return false;
2106 if (code == MINUS_EXPR)
2107 p1 = -p1;
2108 *mul = wi::sext (p0 + p1, precision);
2109 return true;
2111 case INTEGER_CST:
2112 if (TREE_CODE (bot) != INTEGER_CST)
2113 return false;
2115 p0 = widest_int::from (top, SIGNED);
2116 p1 = widest_int::from (bot, SIGNED);
2117 if (p1 == 0)
2118 return false;
2119 *mul = wi::sext (wi::divmod_trunc (p0, p1, SIGNED, &res), precision);
2120 return res == 0;
2122 default:
2123 return false;
2127 /* Return true if memory reference REF with step STEP may be unaligned. */
2129 static bool
2130 may_be_unaligned_p (tree ref, tree step)
2132 /* TARGET_MEM_REFs are translated directly to valid MEMs on the target,
2133 thus they are not misaligned. */
2134 if (TREE_CODE (ref) == TARGET_MEM_REF)
2135 return false;
2137 unsigned int align = TYPE_ALIGN (TREE_TYPE (ref));
2138 if (GET_MODE_ALIGNMENT (TYPE_MODE (TREE_TYPE (ref))) > align)
2139 align = GET_MODE_ALIGNMENT (TYPE_MODE (TREE_TYPE (ref)));
2141 unsigned HOST_WIDE_INT bitpos;
2142 unsigned int ref_align;
2143 get_object_alignment_1 (ref, &ref_align, &bitpos);
2144 if (ref_align < align
2145 || (bitpos % align) != 0
2146 || (bitpos % BITS_PER_UNIT) != 0)
2147 return true;
2149 unsigned int trailing_zeros = tree_ctz (step);
2150 if (trailing_zeros < HOST_BITS_PER_INT
2151 && (1U << trailing_zeros) * BITS_PER_UNIT < align)
2152 return true;
2154 return false;
2157 /* Return true if EXPR may be non-addressable. */
2159 bool
2160 may_be_nonaddressable_p (tree expr)
2162 switch (TREE_CODE (expr))
2164 case TARGET_MEM_REF:
2165 /* TARGET_MEM_REFs are translated directly to valid MEMs on the
2166 target, thus they are always addressable. */
2167 return false;
2169 case MEM_REF:
2170 /* Likewise for MEM_REFs, modulo the storage order. */
2171 return REF_REVERSE_STORAGE_ORDER (expr);
2173 case BIT_FIELD_REF:
2174 if (REF_REVERSE_STORAGE_ORDER (expr))
2175 return true;
2176 return may_be_nonaddressable_p (TREE_OPERAND (expr, 0));
2178 case COMPONENT_REF:
2179 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (expr, 0))))
2180 return true;
2181 return DECL_NONADDRESSABLE_P (TREE_OPERAND (expr, 1))
2182 || may_be_nonaddressable_p (TREE_OPERAND (expr, 0));
2184 case ARRAY_REF:
2185 case ARRAY_RANGE_REF:
2186 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (expr, 0))))
2187 return true;
2188 return may_be_nonaddressable_p (TREE_OPERAND (expr, 0));
2190 case VIEW_CONVERT_EXPR:
2191 /* This kind of view-conversions may wrap non-addressable objects
2192 and make them look addressable. After some processing the
2193 non-addressability may be uncovered again, causing ADDR_EXPRs
2194 of inappropriate objects to be built. */
2195 if (is_gimple_reg (TREE_OPERAND (expr, 0))
2196 || !is_gimple_addressable (TREE_OPERAND (expr, 0)))
2197 return true;
2198 return may_be_nonaddressable_p (TREE_OPERAND (expr, 0));
2200 CASE_CONVERT:
2201 return true;
2203 default:
2204 break;
2207 return false;
2210 /* Finds addresses in *OP_P inside STMT. */
2212 static void
2213 find_interesting_uses_address (struct ivopts_data *data, gimple *stmt,
2214 tree *op_p)
2216 tree base = *op_p, step = size_zero_node;
2217 struct iv *civ;
2218 struct ifs_ivopts_data ifs_ivopts_data;
2220 /* Do not play with volatile memory references. A bit too conservative,
2221 perhaps, but safe. */
2222 if (gimple_has_volatile_ops (stmt))
2223 goto fail;
2225 /* Ignore bitfields for now. Not really something terribly complicated
2226 to handle. TODO. */
2227 if (TREE_CODE (base) == BIT_FIELD_REF)
2228 goto fail;
2230 base = unshare_expr (base);
2232 if (TREE_CODE (base) == TARGET_MEM_REF)
2234 tree type = build_pointer_type (TREE_TYPE (base));
2235 tree astep;
2237 if (TMR_BASE (base)
2238 && TREE_CODE (TMR_BASE (base)) == SSA_NAME)
2240 civ = get_iv (data, TMR_BASE (base));
2241 if (!civ)
2242 goto fail;
2244 TMR_BASE (base) = civ->base;
2245 step = civ->step;
2247 if (TMR_INDEX2 (base)
2248 && TREE_CODE (TMR_INDEX2 (base)) == SSA_NAME)
2250 civ = get_iv (data, TMR_INDEX2 (base));
2251 if (!civ)
2252 goto fail;
2254 TMR_INDEX2 (base) = civ->base;
2255 step = civ->step;
2257 if (TMR_INDEX (base)
2258 && TREE_CODE (TMR_INDEX (base)) == SSA_NAME)
2260 civ = get_iv (data, TMR_INDEX (base));
2261 if (!civ)
2262 goto fail;
2264 TMR_INDEX (base) = civ->base;
2265 astep = civ->step;
2267 if (astep)
2269 if (TMR_STEP (base))
2270 astep = fold_build2 (MULT_EXPR, type, TMR_STEP (base), astep);
2272 step = fold_build2 (PLUS_EXPR, type, step, astep);
2276 if (integer_zerop (step))
2277 goto fail;
2278 base = tree_mem_ref_addr (type, base);
2280 else
2282 ifs_ivopts_data.ivopts_data = data;
2283 ifs_ivopts_data.stmt = stmt;
2284 ifs_ivopts_data.step = size_zero_node;
2285 if (!for_each_index (&base, idx_find_step, &ifs_ivopts_data)
2286 || integer_zerop (ifs_ivopts_data.step))
2287 goto fail;
2288 step = ifs_ivopts_data.step;
2290 /* Check that the base expression is addressable. This needs
2291 to be done after substituting bases of IVs into it. */
2292 if (may_be_nonaddressable_p (base))
2293 goto fail;
2295 /* Moreover, on strict alignment platforms, check that it is
2296 sufficiently aligned. */
2297 if (STRICT_ALIGNMENT && may_be_unaligned_p (base, step))
2298 goto fail;
2300 base = build_fold_addr_expr (base);
2302 /* Substituting bases of IVs into the base expression might
2303 have caused folding opportunities. */
2304 if (TREE_CODE (base) == ADDR_EXPR)
2306 tree *ref = &TREE_OPERAND (base, 0);
2307 while (handled_component_p (*ref))
2308 ref = &TREE_OPERAND (*ref, 0);
2309 if (TREE_CODE (*ref) == MEM_REF)
2311 tree tem = fold_binary (MEM_REF, TREE_TYPE (*ref),
2312 TREE_OPERAND (*ref, 0),
2313 TREE_OPERAND (*ref, 1));
2314 if (tem)
2315 *ref = tem;
2320 civ = alloc_iv (data, base, step);
2321 record_group_use (data, op_p, civ, stmt, USE_ADDRESS);
2322 return;
2324 fail:
2325 for_each_index (op_p, idx_record_use, data);
2328 /* Finds and records invariants used in STMT. */
2330 static void
2331 find_invariants_stmt (struct ivopts_data *data, gimple *stmt)
2333 ssa_op_iter iter;
2334 use_operand_p use_p;
2335 tree op;
2337 FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_USE)
2339 op = USE_FROM_PTR (use_p);
2340 record_invariant (data, op, false);
2344 /* Finds interesting uses of induction variables in the statement STMT. */
2346 static void
2347 find_interesting_uses_stmt (struct ivopts_data *data, gimple *stmt)
2349 struct iv *iv;
2350 tree op, *lhs, *rhs;
2351 ssa_op_iter iter;
2352 use_operand_p use_p;
2353 enum tree_code code;
2355 find_invariants_stmt (data, stmt);
2357 if (gimple_code (stmt) == GIMPLE_COND)
2359 find_interesting_uses_cond (data, stmt);
2360 return;
2363 if (is_gimple_assign (stmt))
2365 lhs = gimple_assign_lhs_ptr (stmt);
2366 rhs = gimple_assign_rhs1_ptr (stmt);
2368 if (TREE_CODE (*lhs) == SSA_NAME)
2370 /* If the statement defines an induction variable, the uses are not
2371 interesting by themselves. */
2373 iv = get_iv (data, *lhs);
2375 if (iv && !integer_zerop (iv->step))
2376 return;
2379 code = gimple_assign_rhs_code (stmt);
2380 if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
2381 && (REFERENCE_CLASS_P (*rhs)
2382 || is_gimple_val (*rhs)))
2384 if (REFERENCE_CLASS_P (*rhs))
2385 find_interesting_uses_address (data, stmt, rhs);
2386 else
2387 find_interesting_uses_op (data, *rhs);
2389 if (REFERENCE_CLASS_P (*lhs))
2390 find_interesting_uses_address (data, stmt, lhs);
2391 return;
2393 else if (TREE_CODE_CLASS (code) == tcc_comparison)
2395 find_interesting_uses_cond (data, stmt);
2396 return;
2399 /* TODO -- we should also handle address uses of type
2401 memory = call (whatever);
2405 call (memory). */
2408 if (gimple_code (stmt) == GIMPLE_PHI
2409 && gimple_bb (stmt) == data->current_loop->header)
2411 iv = get_iv (data, PHI_RESULT (stmt));
2413 if (iv && !integer_zerop (iv->step))
2414 return;
2417 FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_USE)
2419 op = USE_FROM_PTR (use_p);
2421 if (TREE_CODE (op) != SSA_NAME)
2422 continue;
2424 iv = get_iv (data, op);
2425 if (!iv)
2426 continue;
2428 find_interesting_uses_op (data, op);
2432 /* Finds interesting uses of induction variables outside of loops
2433 on loop exit edge EXIT. */
2435 static void
2436 find_interesting_uses_outside (struct ivopts_data *data, edge exit)
2438 gphi *phi;
2439 gphi_iterator psi;
2440 tree def;
2442 for (psi = gsi_start_phis (exit->dest); !gsi_end_p (psi); gsi_next (&psi))
2444 phi = psi.phi ();
2445 def = PHI_ARG_DEF_FROM_EDGE (phi, exit);
2446 if (!virtual_operand_p (def))
2447 find_interesting_uses_op (data, def);
2451 /* Compute maximum offset of [base + offset] addressing mode
2452 for memory reference represented by USE. */
2454 static HOST_WIDE_INT
2455 compute_max_addr_offset (struct iv_use *use)
2457 int width;
2458 rtx reg, addr;
2459 HOST_WIDE_INT i, off;
2460 unsigned list_index, num;
2461 addr_space_t as;
2462 machine_mode mem_mode, addr_mode;
2463 static vec<HOST_WIDE_INT> max_offset_list;
2465 as = TYPE_ADDR_SPACE (TREE_TYPE (use->iv->base));
2466 mem_mode = TYPE_MODE (TREE_TYPE (*use->op_p));
2468 num = max_offset_list.length ();
2469 list_index = (unsigned) as * MAX_MACHINE_MODE + (unsigned) mem_mode;
2470 if (list_index >= num)
2472 max_offset_list.safe_grow (list_index + MAX_MACHINE_MODE);
2473 for (; num < max_offset_list.length (); num++)
2474 max_offset_list[num] = -1;
2477 off = max_offset_list[list_index];
2478 if (off != -1)
2479 return off;
2481 addr_mode = targetm.addr_space.address_mode (as);
2482 reg = gen_raw_REG (addr_mode, LAST_VIRTUAL_REGISTER + 1);
2483 addr = gen_rtx_fmt_ee (PLUS, addr_mode, reg, NULL_RTX);
2485 width = GET_MODE_BITSIZE (addr_mode) - 1;
2486 if (width > (HOST_BITS_PER_WIDE_INT - 1))
2487 width = HOST_BITS_PER_WIDE_INT - 1;
2489 for (i = width; i > 0; i--)
2491 off = (HOST_WIDE_INT_1U << i) - 1;
2492 XEXP (addr, 1) = gen_int_mode (off, addr_mode);
2493 if (memory_address_addr_space_p (mem_mode, addr, as))
2494 break;
2496 /* For some strict-alignment targets, the offset must be naturally
2497 aligned. Try an aligned offset if mem_mode is not QImode. */
2498 off = (HOST_WIDE_INT_1U << i);
2499 if (off > GET_MODE_SIZE (mem_mode) && mem_mode != QImode)
2501 off -= GET_MODE_SIZE (mem_mode);
2502 XEXP (addr, 1) = gen_int_mode (off, addr_mode);
2503 if (memory_address_addr_space_p (mem_mode, addr, as))
2504 break;
2507 if (i == 0)
2508 off = 0;
2510 max_offset_list[list_index] = off;
2511 return off;
2514 /* Comparison function to sort group in ascending order of addr_offset. */
2516 static int
2517 group_compare_offset (const void *a, const void *b)
2519 const struct iv_use *const *u1 = (const struct iv_use *const *) a;
2520 const struct iv_use *const *u2 = (const struct iv_use *const *) b;
2522 if ((*u1)->addr_offset != (*u2)->addr_offset)
2523 return (*u1)->addr_offset < (*u2)->addr_offset ? -1 : 1;
2524 else
2525 return 0;
2528 /* Check if small groups should be split. Return true if no group
2529 contains more than two uses with distinct addr_offsets. Return
2530 false otherwise. We want to split such groups because:
2532 1) Small groups don't have much benefit and may interfer with
2533 general candidate selection.
2534 2) Size for problem with only small groups is usually small and
2535 general algorithm can handle it well.
2537 TODO -- Above claim may not hold when we want to merge memory
2538 accesses with conseuctive addresses. */
2540 static bool
2541 split_small_address_groups_p (struct ivopts_data *data)
2543 unsigned int i, j, distinct = 1;
2544 struct iv_use *pre;
2545 struct iv_group *group;
2547 for (i = 0; i < data->vgroups.length (); i++)
2549 group = data->vgroups[i];
2550 if (group->vuses.length () == 1)
2551 continue;
2553 gcc_assert (group->type == USE_ADDRESS);
2554 if (group->vuses.length () == 2)
2556 if (group->vuses[0]->addr_offset > group->vuses[1]->addr_offset)
2557 std::swap (group->vuses[0], group->vuses[1]);
2559 else
2560 group->vuses.qsort (group_compare_offset);
2562 if (distinct > 2)
2563 continue;
2565 distinct = 1;
2566 for (pre = group->vuses[0], j = 1; j < group->vuses.length (); j++)
2568 if (group->vuses[j]->addr_offset != pre->addr_offset)
2570 pre = group->vuses[j];
2571 distinct++;
2574 if (distinct > 2)
2575 break;
2579 return (distinct <= 2);
2582 /* For each group of address type uses, this function further groups
2583 these uses according to the maximum offset supported by target's
2584 [base + offset] addressing mode. */
2586 static void
2587 split_address_groups (struct ivopts_data *data)
2589 unsigned int i, j;
2590 HOST_WIDE_INT max_offset = -1;
2592 /* Reset max offset to split all small groups. */
2593 if (split_small_address_groups_p (data))
2594 max_offset = 0;
2596 for (i = 0; i < data->vgroups.length (); i++)
2598 struct iv_group *group = data->vgroups[i];
2599 struct iv_use *use = group->vuses[0];
2601 use->id = 0;
2602 use->group_id = group->id;
2603 if (group->vuses.length () == 1)
2604 continue;
2606 if (max_offset != 0)
2607 max_offset = compute_max_addr_offset (use);
2609 for (j = 1; j < group->vuses.length (); j++)
2611 struct iv_use *next = group->vuses[j];
2613 /* Only uses with offset that can fit in offset part against
2614 the first use can be grouped together. */
2615 if (next->addr_offset - use->addr_offset
2616 > (unsigned HOST_WIDE_INT) max_offset)
2617 break;
2619 next->id = j;
2620 next->group_id = group->id;
2622 /* Split group. */
2623 if (j < group->vuses.length ())
2625 struct iv_group *new_group = record_group (data, group->type);
2626 new_group->vuses.safe_splice (group->vuses);
2627 new_group->vuses.block_remove (0, j);
2628 group->vuses.truncate (j);
2633 /* Finds uses of the induction variables that are interesting. */
2635 static void
2636 find_interesting_uses (struct ivopts_data *data)
2638 basic_block bb;
2639 gimple_stmt_iterator bsi;
2640 basic_block *body = get_loop_body (data->current_loop);
2641 unsigned i;
2642 edge e;
2644 for (i = 0; i < data->current_loop->num_nodes; i++)
2646 edge_iterator ei;
2647 bb = body[i];
2649 FOR_EACH_EDGE (e, ei, bb->succs)
2650 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
2651 && !flow_bb_inside_loop_p (data->current_loop, e->dest))
2652 find_interesting_uses_outside (data, e);
2654 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2655 find_interesting_uses_stmt (data, gsi_stmt (bsi));
2656 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2657 if (!is_gimple_debug (gsi_stmt (bsi)))
2658 find_interesting_uses_stmt (data, gsi_stmt (bsi));
2661 split_address_groups (data);
2663 if (dump_file && (dump_flags & TDF_DETAILS))
2665 bitmap_iterator bi;
2667 fprintf (dump_file, "\n<Invariant Vars>:\n");
2668 EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, i, bi)
2670 struct version_info *info = ver_info (data, i);
2671 if (info->inv_id)
2673 fprintf (dump_file, "Inv %d:\t", info->inv_id);
2674 print_generic_expr (dump_file, info->name, TDF_SLIM);
2675 fprintf (dump_file, "%s\n",
2676 info->has_nonlin_use ? "" : "\t(eliminable)");
2680 fprintf (dump_file, "\n<IV Groups>:\n");
2681 dump_groups (dump_file, data);
2682 fprintf (dump_file, "\n");
2685 free (body);
2688 /* Strips constant offsets from EXPR and stores them to OFFSET. If INSIDE_ADDR
2689 is true, assume we are inside an address. If TOP_COMPREF is true, assume
2690 we are at the top-level of the processed address. */
2692 static tree
2693 strip_offset_1 (tree expr, bool inside_addr, bool top_compref,
2694 HOST_WIDE_INT *offset)
2696 tree op0 = NULL_TREE, op1 = NULL_TREE, tmp, step;
2697 enum tree_code code;
2698 tree type, orig_type = TREE_TYPE (expr);
2699 HOST_WIDE_INT off0, off1, st;
2700 tree orig_expr = expr;
2702 STRIP_NOPS (expr);
2704 type = TREE_TYPE (expr);
2705 code = TREE_CODE (expr);
2706 *offset = 0;
2708 switch (code)
2710 case INTEGER_CST:
2711 if (!cst_and_fits_in_hwi (expr)
2712 || integer_zerop (expr))
2713 return orig_expr;
2715 *offset = int_cst_value (expr);
2716 return build_int_cst (orig_type, 0);
2718 case POINTER_PLUS_EXPR:
2719 case PLUS_EXPR:
2720 case MINUS_EXPR:
2721 op0 = TREE_OPERAND (expr, 0);
2722 op1 = TREE_OPERAND (expr, 1);
2724 op0 = strip_offset_1 (op0, false, false, &off0);
2725 op1 = strip_offset_1 (op1, false, false, &off1);
2727 *offset = (code == MINUS_EXPR ? off0 - off1 : off0 + off1);
2728 if (op0 == TREE_OPERAND (expr, 0)
2729 && op1 == TREE_OPERAND (expr, 1))
2730 return orig_expr;
2732 if (integer_zerop (op1))
2733 expr = op0;
2734 else if (integer_zerop (op0))
2736 if (code == MINUS_EXPR)
2737 expr = fold_build1 (NEGATE_EXPR, type, op1);
2738 else
2739 expr = op1;
2741 else
2742 expr = fold_build2 (code, type, op0, op1);
2744 return fold_convert (orig_type, expr);
2746 case MULT_EXPR:
2747 op1 = TREE_OPERAND (expr, 1);
2748 if (!cst_and_fits_in_hwi (op1))
2749 return orig_expr;
2751 op0 = TREE_OPERAND (expr, 0);
2752 op0 = strip_offset_1 (op0, false, false, &off0);
2753 if (op0 == TREE_OPERAND (expr, 0))
2754 return orig_expr;
2756 *offset = off0 * int_cst_value (op1);
2757 if (integer_zerop (op0))
2758 expr = op0;
2759 else
2760 expr = fold_build2 (MULT_EXPR, type, op0, op1);
2762 return fold_convert (orig_type, expr);
2764 case ARRAY_REF:
2765 case ARRAY_RANGE_REF:
2766 if (!inside_addr)
2767 return orig_expr;
2769 step = array_ref_element_size (expr);
2770 if (!cst_and_fits_in_hwi (step))
2771 break;
2773 st = int_cst_value (step);
2774 op1 = TREE_OPERAND (expr, 1);
2775 op1 = strip_offset_1 (op1, false, false, &off1);
2776 *offset = off1 * st;
2778 if (top_compref
2779 && integer_zerop (op1))
2781 /* Strip the component reference completely. */
2782 op0 = TREE_OPERAND (expr, 0);
2783 op0 = strip_offset_1 (op0, inside_addr, top_compref, &off0);
2784 *offset += off0;
2785 return op0;
2787 break;
2789 case COMPONENT_REF:
2791 tree field;
2793 if (!inside_addr)
2794 return orig_expr;
2796 tmp = component_ref_field_offset (expr);
2797 field = TREE_OPERAND (expr, 1);
2798 if (top_compref
2799 && cst_and_fits_in_hwi (tmp)
2800 && cst_and_fits_in_hwi (DECL_FIELD_BIT_OFFSET (field)))
2802 HOST_WIDE_INT boffset, abs_off;
2804 /* Strip the component reference completely. */
2805 op0 = TREE_OPERAND (expr, 0);
2806 op0 = strip_offset_1 (op0, inside_addr, top_compref, &off0);
2807 boffset = int_cst_value (DECL_FIELD_BIT_OFFSET (field));
2808 abs_off = abs_hwi (boffset) / BITS_PER_UNIT;
2809 if (boffset < 0)
2810 abs_off = -abs_off;
2812 *offset = off0 + int_cst_value (tmp) + abs_off;
2813 return op0;
2816 break;
2818 case ADDR_EXPR:
2819 op0 = TREE_OPERAND (expr, 0);
2820 op0 = strip_offset_1 (op0, true, true, &off0);
2821 *offset += off0;
2823 if (op0 == TREE_OPERAND (expr, 0))
2824 return orig_expr;
2826 expr = build_fold_addr_expr (op0);
2827 return fold_convert (orig_type, expr);
2829 case MEM_REF:
2830 /* ??? Offset operand? */
2831 inside_addr = false;
2832 break;
2834 default:
2835 return orig_expr;
2838 /* Default handling of expressions for that we want to recurse into
2839 the first operand. */
2840 op0 = TREE_OPERAND (expr, 0);
2841 op0 = strip_offset_1 (op0, inside_addr, false, &off0);
2842 *offset += off0;
2844 if (op0 == TREE_OPERAND (expr, 0)
2845 && (!op1 || op1 == TREE_OPERAND (expr, 1)))
2846 return orig_expr;
2848 expr = copy_node (expr);
2849 TREE_OPERAND (expr, 0) = op0;
2850 if (op1)
2851 TREE_OPERAND (expr, 1) = op1;
2853 /* Inside address, we might strip the top level component references,
2854 thus changing type of the expression. Handling of ADDR_EXPR
2855 will fix that. */
2856 expr = fold_convert (orig_type, expr);
2858 return expr;
2861 /* Strips constant offsets from EXPR and stores them to OFFSET. */
2863 static tree
2864 strip_offset (tree expr, unsigned HOST_WIDE_INT *offset)
2866 HOST_WIDE_INT off;
2867 tree core = strip_offset_1 (expr, false, false, &off);
2868 *offset = off;
2869 return core;
2872 /* Returns variant of TYPE that can be used as base for different uses.
2873 We return unsigned type with the same precision, which avoids problems
2874 with overflows. */
2876 static tree
2877 generic_type_for (tree type)
2879 if (POINTER_TYPE_P (type))
2880 return unsigned_type_for (type);
2882 if (TYPE_UNSIGNED (type))
2883 return type;
2885 return unsigned_type_for (type);
2888 /* Records invariants in *EXPR_P. Callback for walk_tree. DATA contains
2889 the bitmap to that we should store it. */
2891 static struct ivopts_data *fd_ivopts_data;
2892 static tree
2893 find_depends (tree *expr_p, int *ws ATTRIBUTE_UNUSED, void *data)
2895 bitmap *depends_on = (bitmap *) data;
2896 struct version_info *info;
2898 if (TREE_CODE (*expr_p) != SSA_NAME)
2899 return NULL_TREE;
2900 info = name_info (fd_ivopts_data, *expr_p);
2902 if (!info->inv_id || info->has_nonlin_use)
2903 return NULL_TREE;
2905 if (!*depends_on)
2906 *depends_on = BITMAP_ALLOC (NULL);
2907 bitmap_set_bit (*depends_on, info->inv_id);
2909 return NULL_TREE;
2912 /* Adds a candidate BASE + STEP * i. Important field is set to IMPORTANT and
2913 position to POS. If USE is not NULL, the candidate is set as related to
2914 it. If both BASE and STEP are NULL, we add a pseudocandidate for the
2915 replacement of the final value of the iv by a direct computation. */
2917 static struct iv_cand *
2918 add_candidate_1 (struct ivopts_data *data,
2919 tree base, tree step, bool important, enum iv_position pos,
2920 struct iv_use *use, gimple *incremented_at,
2921 struct iv *orig_iv = NULL)
2923 unsigned i;
2924 struct iv_cand *cand = NULL;
2925 tree type, orig_type;
2927 gcc_assert (base && step);
2929 /* -fkeep-gc-roots-live means that we have to keep a real pointer
2930 live, but the ivopts code may replace a real pointer with one
2931 pointing before or after the memory block that is then adjusted
2932 into the memory block during the loop. FIXME: It would likely be
2933 better to actually force the pointer live and still use ivopts;
2934 for example, it would be enough to write the pointer into memory
2935 and keep it there until after the loop. */
2936 if (flag_keep_gc_roots_live && POINTER_TYPE_P (TREE_TYPE (base)))
2937 return NULL;
2939 /* For non-original variables, make sure their values are computed in a type
2940 that does not invoke undefined behavior on overflows (since in general,
2941 we cannot prove that these induction variables are non-wrapping). */
2942 if (pos != IP_ORIGINAL)
2944 orig_type = TREE_TYPE (base);
2945 type = generic_type_for (orig_type);
2946 if (type != orig_type)
2948 base = fold_convert (type, base);
2949 step = fold_convert (type, step);
2953 for (i = 0; i < data->vcands.length (); i++)
2955 cand = data->vcands[i];
2957 if (cand->pos != pos)
2958 continue;
2960 if (cand->incremented_at != incremented_at
2961 || ((pos == IP_AFTER_USE || pos == IP_BEFORE_USE)
2962 && cand->ainc_use != use))
2963 continue;
2965 if (operand_equal_p (base, cand->iv->base, 0)
2966 && operand_equal_p (step, cand->iv->step, 0)
2967 && (TYPE_PRECISION (TREE_TYPE (base))
2968 == TYPE_PRECISION (TREE_TYPE (cand->iv->base))))
2969 break;
2972 if (i == data->vcands.length ())
2974 cand = XCNEW (struct iv_cand);
2975 cand->id = i;
2976 cand->iv = alloc_iv (data, base, step);
2977 cand->pos = pos;
2978 if (pos != IP_ORIGINAL)
2980 cand->var_before = create_tmp_var_raw (TREE_TYPE (base), "ivtmp");
2981 cand->var_after = cand->var_before;
2983 cand->important = important;
2984 cand->incremented_at = incremented_at;
2985 data->vcands.safe_push (cand);
2987 if (TREE_CODE (step) != INTEGER_CST)
2989 fd_ivopts_data = data;
2990 walk_tree (&step, find_depends, &cand->depends_on, NULL);
2993 if (pos == IP_AFTER_USE || pos == IP_BEFORE_USE)
2994 cand->ainc_use = use;
2995 else
2996 cand->ainc_use = NULL;
2998 cand->orig_iv = orig_iv;
2999 if (dump_file && (dump_flags & TDF_DETAILS))
3000 dump_cand (dump_file, cand);
3003 cand->important |= important;
3005 /* Relate candidate to the group for which it is added. */
3006 if (use)
3007 bitmap_set_bit (data->vgroups[use->group_id]->related_cands, i);
3009 return cand;
3012 /* Returns true if incrementing the induction variable at the end of the LOOP
3013 is allowed.
3015 The purpose is to avoid splitting latch edge with a biv increment, thus
3016 creating a jump, possibly confusing other optimization passes and leaving
3017 less freedom to scheduler. So we allow IP_END_POS only if IP_NORMAL_POS
3018 is not available (so we do not have a better alternative), or if the latch
3019 edge is already nonempty. */
3021 static bool
3022 allow_ip_end_pos_p (struct loop *loop)
3024 if (!ip_normal_pos (loop))
3025 return true;
3027 if (!empty_block_p (ip_end_pos (loop)))
3028 return true;
3030 return false;
3033 /* If possible, adds autoincrement candidates BASE + STEP * i based on use USE.
3034 Important field is set to IMPORTANT. */
3036 static void
3037 add_autoinc_candidates (struct ivopts_data *data, tree base, tree step,
3038 bool important, struct iv_use *use)
3040 basic_block use_bb = gimple_bb (use->stmt);
3041 machine_mode mem_mode;
3042 unsigned HOST_WIDE_INT cstepi;
3044 /* If we insert the increment in any position other than the standard
3045 ones, we must ensure that it is incremented once per iteration.
3046 It must not be in an inner nested loop, or one side of an if
3047 statement. */
3048 if (use_bb->loop_father != data->current_loop
3049 || !dominated_by_p (CDI_DOMINATORS, data->current_loop->latch, use_bb)
3050 || stmt_could_throw_p (use->stmt)
3051 || !cst_and_fits_in_hwi (step))
3052 return;
3054 cstepi = int_cst_value (step);
3056 mem_mode = TYPE_MODE (TREE_TYPE (*use->op_p));
3057 if (((USE_LOAD_PRE_INCREMENT (mem_mode)
3058 || USE_STORE_PRE_INCREMENT (mem_mode))
3059 && GET_MODE_SIZE (mem_mode) == cstepi)
3060 || ((USE_LOAD_PRE_DECREMENT (mem_mode)
3061 || USE_STORE_PRE_DECREMENT (mem_mode))
3062 && GET_MODE_SIZE (mem_mode) == -cstepi))
3064 enum tree_code code = MINUS_EXPR;
3065 tree new_base;
3066 tree new_step = step;
3068 if (POINTER_TYPE_P (TREE_TYPE (base)))
3070 new_step = fold_build1 (NEGATE_EXPR, TREE_TYPE (step), step);
3071 code = POINTER_PLUS_EXPR;
3073 else
3074 new_step = fold_convert (TREE_TYPE (base), new_step);
3075 new_base = fold_build2 (code, TREE_TYPE (base), base, new_step);
3076 add_candidate_1 (data, new_base, step, important, IP_BEFORE_USE, use,
3077 use->stmt);
3079 if (((USE_LOAD_POST_INCREMENT (mem_mode)
3080 || USE_STORE_POST_INCREMENT (mem_mode))
3081 && GET_MODE_SIZE (mem_mode) == cstepi)
3082 || ((USE_LOAD_POST_DECREMENT (mem_mode)
3083 || USE_STORE_POST_DECREMENT (mem_mode))
3084 && GET_MODE_SIZE (mem_mode) == -cstepi))
3086 add_candidate_1 (data, base, step, important, IP_AFTER_USE, use,
3087 use->stmt);
3091 /* Adds a candidate BASE + STEP * i. Important field is set to IMPORTANT and
3092 position to POS. If USE is not NULL, the candidate is set as related to
3093 it. The candidate computation is scheduled before exit condition and at
3094 the end of loop. */
3096 static void
3097 add_candidate (struct ivopts_data *data,
3098 tree base, tree step, bool important, struct iv_use *use,
3099 struct iv *orig_iv = NULL)
3101 if (ip_normal_pos (data->current_loop))
3102 add_candidate_1 (data, base, step, important,
3103 IP_NORMAL, use, NULL, orig_iv);
3104 if (ip_end_pos (data->current_loop)
3105 && allow_ip_end_pos_p (data->current_loop))
3106 add_candidate_1 (data, base, step, important, IP_END, use, NULL, orig_iv);
3109 /* Adds standard iv candidates. */
3111 static void
3112 add_standard_iv_candidates (struct ivopts_data *data)
3114 add_candidate (data, integer_zero_node, integer_one_node, true, NULL);
3116 /* The same for a double-integer type if it is still fast enough. */
3117 if (TYPE_PRECISION
3118 (long_integer_type_node) > TYPE_PRECISION (integer_type_node)
3119 && TYPE_PRECISION (long_integer_type_node) <= BITS_PER_WORD)
3120 add_candidate (data, build_int_cst (long_integer_type_node, 0),
3121 build_int_cst (long_integer_type_node, 1), true, NULL);
3123 /* The same for a double-integer type if it is still fast enough. */
3124 if (TYPE_PRECISION
3125 (long_long_integer_type_node) > TYPE_PRECISION (long_integer_type_node)
3126 && TYPE_PRECISION (long_long_integer_type_node) <= BITS_PER_WORD)
3127 add_candidate (data, build_int_cst (long_long_integer_type_node, 0),
3128 build_int_cst (long_long_integer_type_node, 1), true, NULL);
3132 /* Adds candidates bases on the old induction variable IV. */
3134 static void
3135 add_iv_candidate_for_biv (struct ivopts_data *data, struct iv *iv)
3137 gimple *phi;
3138 tree def;
3139 struct iv_cand *cand;
3141 /* Check if this biv is used in address type use. */
3142 if (iv->no_overflow && iv->have_address_use
3143 && INTEGRAL_TYPE_P (TREE_TYPE (iv->base))
3144 && TYPE_PRECISION (TREE_TYPE (iv->base)) < TYPE_PRECISION (sizetype))
3146 tree base = fold_convert (sizetype, iv->base);
3147 tree step = fold_convert (sizetype, iv->step);
3149 /* Add iv cand of same precision as index part in TARGET_MEM_REF. */
3150 add_candidate (data, base, step, true, NULL, iv);
3151 /* Add iv cand of the original type only if it has nonlinear use. */
3152 if (iv->nonlin_use)
3153 add_candidate (data, iv->base, iv->step, true, NULL);
3155 else
3156 add_candidate (data, iv->base, iv->step, true, NULL);
3158 /* The same, but with initial value zero. */
3159 if (POINTER_TYPE_P (TREE_TYPE (iv->base)))
3160 add_candidate (data, size_int (0), iv->step, true, NULL);
3161 else
3162 add_candidate (data, build_int_cst (TREE_TYPE (iv->base), 0),
3163 iv->step, true, NULL);
3165 phi = SSA_NAME_DEF_STMT (iv->ssa_name);
3166 if (gimple_code (phi) == GIMPLE_PHI)
3168 /* Additionally record the possibility of leaving the original iv
3169 untouched. */
3170 def = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (data->current_loop));
3171 /* Don't add candidate if it's from another PHI node because
3172 it's an affine iv appearing in the form of PEELED_CHREC. */
3173 phi = SSA_NAME_DEF_STMT (def);
3174 if (gimple_code (phi) != GIMPLE_PHI)
3176 cand = add_candidate_1 (data,
3177 iv->base, iv->step, true, IP_ORIGINAL, NULL,
3178 SSA_NAME_DEF_STMT (def));
3179 if (cand)
3181 cand->var_before = iv->ssa_name;
3182 cand->var_after = def;
3185 else
3186 gcc_assert (gimple_bb (phi) == data->current_loop->header);
3190 /* Adds candidates based on the old induction variables. */
3192 static void
3193 add_iv_candidate_for_bivs (struct ivopts_data *data)
3195 unsigned i;
3196 struct iv *iv;
3197 bitmap_iterator bi;
3199 EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, i, bi)
3201 iv = ver_info (data, i)->iv;
3202 if (iv && iv->biv_p && !integer_zerop (iv->step))
3203 add_iv_candidate_for_biv (data, iv);
3207 /* Record common candidate {BASE, STEP} derived from USE in hashtable. */
3209 static void
3210 record_common_cand (struct ivopts_data *data, tree base,
3211 tree step, struct iv_use *use)
3213 struct iv_common_cand ent;
3214 struct iv_common_cand **slot;
3216 ent.base = base;
3217 ent.step = step;
3218 ent.hash = iterative_hash_expr (base, 0);
3219 ent.hash = iterative_hash_expr (step, ent.hash);
3221 slot = data->iv_common_cand_tab->find_slot (&ent, INSERT);
3222 if (*slot == NULL)
3224 *slot = new iv_common_cand ();
3225 (*slot)->base = base;
3226 (*slot)->step = step;
3227 (*slot)->uses.create (8);
3228 (*slot)->hash = ent.hash;
3229 data->iv_common_cands.safe_push ((*slot));
3232 gcc_assert (use != NULL);
3233 (*slot)->uses.safe_push (use);
3234 return;
3237 /* Comparison function used to sort common candidates. */
3239 static int
3240 common_cand_cmp (const void *p1, const void *p2)
3242 unsigned n1, n2;
3243 const struct iv_common_cand *const *const ccand1
3244 = (const struct iv_common_cand *const *)p1;
3245 const struct iv_common_cand *const *const ccand2
3246 = (const struct iv_common_cand *const *)p2;
3248 n1 = (*ccand1)->uses.length ();
3249 n2 = (*ccand2)->uses.length ();
3250 return n2 - n1;
3253 /* Adds IV candidates based on common candidated recorded. */
3255 static void
3256 add_iv_candidate_derived_from_uses (struct ivopts_data *data)
3258 unsigned i, j;
3259 struct iv_cand *cand_1, *cand_2;
3261 data->iv_common_cands.qsort (common_cand_cmp);
3262 for (i = 0; i < data->iv_common_cands.length (); i++)
3264 struct iv_common_cand *ptr = data->iv_common_cands[i];
3266 /* Only add IV candidate if it's derived from multiple uses. */
3267 if (ptr->uses.length () <= 1)
3268 break;
3270 cand_1 = NULL;
3271 cand_2 = NULL;
3272 if (ip_normal_pos (data->current_loop))
3273 cand_1 = add_candidate_1 (data, ptr->base, ptr->step,
3274 false, IP_NORMAL, NULL, NULL);
3276 if (ip_end_pos (data->current_loop)
3277 && allow_ip_end_pos_p (data->current_loop))
3278 cand_2 = add_candidate_1 (data, ptr->base, ptr->step,
3279 false, IP_END, NULL, NULL);
3281 /* Bind deriving uses and the new candidates. */
3282 for (j = 0; j < ptr->uses.length (); j++)
3284 struct iv_group *group = data->vgroups[ptr->uses[j]->group_id];
3285 if (cand_1)
3286 bitmap_set_bit (group->related_cands, cand_1->id);
3287 if (cand_2)
3288 bitmap_set_bit (group->related_cands, cand_2->id);
3292 /* Release data since it is useless from this point. */
3293 data->iv_common_cand_tab->empty ();
3294 data->iv_common_cands.truncate (0);
3297 /* Adds candidates based on the value of USE's iv. */
3299 static void
3300 add_iv_candidate_for_use (struct ivopts_data *data, struct iv_use *use)
3302 unsigned HOST_WIDE_INT offset;
3303 tree base;
3304 tree basetype;
3305 struct iv *iv = use->iv;
3307 add_candidate (data, iv->base, iv->step, false, use);
3309 /* Record common candidate for use in case it can be shared by others. */
3310 record_common_cand (data, iv->base, iv->step, use);
3312 /* Record common candidate with initial value zero. */
3313 basetype = TREE_TYPE (iv->base);
3314 if (POINTER_TYPE_P (basetype))
3315 basetype = sizetype;
3316 record_common_cand (data, build_int_cst (basetype, 0), iv->step, use);
3318 /* Record common candidate with constant offset stripped in base.
3319 Like the use itself, we also add candidate directly for it. */
3320 base = strip_offset (iv->base, &offset);
3321 if (offset || base != iv->base)
3323 record_common_cand (data, base, iv->step, use);
3324 add_candidate (data, base, iv->step, false, use);
3327 /* Record common candidate with base_object removed in base. */
3328 if (iv->base_object != NULL)
3330 unsigned i;
3331 aff_tree aff_base;
3332 tree step, base_object = iv->base_object;
3334 base = iv->base;
3335 step = iv->step;
3336 STRIP_NOPS (base);
3337 STRIP_NOPS (step);
3338 STRIP_NOPS (base_object);
3339 tree_to_aff_combination (base, TREE_TYPE (base), &aff_base);
3340 for (i = 0; i < aff_base.n; i++)
3342 if (aff_base.elts[i].coef != 1)
3343 continue;
3345 if (operand_equal_p (aff_base.elts[i].val, base_object, 0))
3346 break;
3348 if (i < aff_base.n)
3350 aff_combination_remove_elt (&aff_base, i);
3351 base = aff_combination_to_tree (&aff_base);
3352 basetype = TREE_TYPE (base);
3353 if (POINTER_TYPE_P (basetype))
3354 basetype = sizetype;
3356 step = fold_convert (basetype, step);
3357 record_common_cand (data, base, step, use);
3358 /* Also record common candidate with offset stripped. */
3359 base = strip_offset (base, &offset);
3360 if (offset)
3361 record_common_cand (data, base, step, use);
3365 /* At last, add auto-incremental candidates. Make such variables
3366 important since other iv uses with same base object may be based
3367 on it. */
3368 if (use != NULL && use->type == USE_ADDRESS)
3369 add_autoinc_candidates (data, iv->base, iv->step, true, use);
3372 /* Adds candidates based on the uses. */
3374 static void
3375 add_iv_candidate_for_groups (struct ivopts_data *data)
3377 unsigned i;
3379 /* Only add candidate for the first use in group. */
3380 for (i = 0; i < data->vgroups.length (); i++)
3382 struct iv_group *group = data->vgroups[i];
3384 gcc_assert (group->vuses[0] != NULL);
3385 add_iv_candidate_for_use (data, group->vuses[0]);
3387 add_iv_candidate_derived_from_uses (data);
3390 /* Record important candidates and add them to related_cands bitmaps. */
3392 static void
3393 record_important_candidates (struct ivopts_data *data)
3395 unsigned i;
3396 struct iv_group *group;
3398 for (i = 0; i < data->vcands.length (); i++)
3400 struct iv_cand *cand = data->vcands[i];
3402 if (cand->important)
3403 bitmap_set_bit (data->important_candidates, i);
3406 data->consider_all_candidates = (data->vcands.length ()
3407 <= CONSIDER_ALL_CANDIDATES_BOUND);
3409 /* Add important candidates to groups' related_cands bitmaps. */
3410 for (i = 0; i < data->vgroups.length (); i++)
3412 group = data->vgroups[i];
3413 bitmap_ior_into (group->related_cands, data->important_candidates);
3417 /* Allocates the data structure mapping the (use, candidate) pairs to costs.
3418 If consider_all_candidates is true, we use a two-dimensional array, otherwise
3419 we allocate a simple list to every use. */
3421 static void
3422 alloc_use_cost_map (struct ivopts_data *data)
3424 unsigned i, size, s;
3426 for (i = 0; i < data->vgroups.length (); i++)
3428 struct iv_group *group = data->vgroups[i];
3430 if (data->consider_all_candidates)
3431 size = data->vcands.length ();
3432 else
3434 s = bitmap_count_bits (group->related_cands);
3436 /* Round up to the power of two, so that moduling by it is fast. */
3437 size = s ? (1 << ceil_log2 (s)) : 1;
3440 group->n_map_members = size;
3441 group->cost_map = XCNEWVEC (struct cost_pair, size);
3445 /* Sets cost of (GROUP, CAND) pair to COST and record that it depends
3446 on invariants DEPENDS_ON and that the value used in expressing it
3447 is VALUE, and in case of iv elimination the comparison operator is COMP. */
3449 static void
3450 set_group_iv_cost (struct ivopts_data *data,
3451 struct iv_group *group, struct iv_cand *cand,
3452 comp_cost cost, bitmap depends_on, tree value,
3453 enum tree_code comp, iv_inv_expr_ent *inv_expr)
3455 unsigned i, s;
3457 if (cost.infinite_cost_p ())
3459 BITMAP_FREE (depends_on);
3460 return;
3463 if (data->consider_all_candidates)
3465 group->cost_map[cand->id].cand = cand;
3466 group->cost_map[cand->id].cost = cost;
3467 group->cost_map[cand->id].depends_on = depends_on;
3468 group->cost_map[cand->id].value = value;
3469 group->cost_map[cand->id].comp = comp;
3470 group->cost_map[cand->id].inv_expr = inv_expr;
3471 return;
3474 /* n_map_members is a power of two, so this computes modulo. */
3475 s = cand->id & (group->n_map_members - 1);
3476 for (i = s; i < group->n_map_members; i++)
3477 if (!group->cost_map[i].cand)
3478 goto found;
3479 for (i = 0; i < s; i++)
3480 if (!group->cost_map[i].cand)
3481 goto found;
3483 gcc_unreachable ();
3485 found:
3486 group->cost_map[i].cand = cand;
3487 group->cost_map[i].cost = cost;
3488 group->cost_map[i].depends_on = depends_on;
3489 group->cost_map[i].value = value;
3490 group->cost_map[i].comp = comp;
3491 group->cost_map[i].inv_expr = inv_expr;
3494 /* Gets cost of (GROUP, CAND) pair. */
3496 static struct cost_pair *
3497 get_group_iv_cost (struct ivopts_data *data, struct iv_group *group,
3498 struct iv_cand *cand)
3500 unsigned i, s;
3501 struct cost_pair *ret;
3503 if (!cand)
3504 return NULL;
3506 if (data->consider_all_candidates)
3508 ret = group->cost_map + cand->id;
3509 if (!ret->cand)
3510 return NULL;
3512 return ret;
3515 /* n_map_members is a power of two, so this computes modulo. */
3516 s = cand->id & (group->n_map_members - 1);
3517 for (i = s; i < group->n_map_members; i++)
3518 if (group->cost_map[i].cand == cand)
3519 return group->cost_map + i;
3520 else if (group->cost_map[i].cand == NULL)
3521 return NULL;
3522 for (i = 0; i < s; i++)
3523 if (group->cost_map[i].cand == cand)
3524 return group->cost_map + i;
3525 else if (group->cost_map[i].cand == NULL)
3526 return NULL;
3528 return NULL;
3531 /* Produce DECL_RTL for object obj so it looks like it is stored in memory. */
3532 static rtx
3533 produce_memory_decl_rtl (tree obj, int *regno)
3535 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (obj));
3536 machine_mode address_mode = targetm.addr_space.address_mode (as);
3537 rtx x;
3539 gcc_assert (obj);
3540 if (TREE_STATIC (obj) || DECL_EXTERNAL (obj))
3542 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (obj));
3543 x = gen_rtx_SYMBOL_REF (address_mode, name);
3544 SET_SYMBOL_REF_DECL (x, obj);
3545 x = gen_rtx_MEM (DECL_MODE (obj), x);
3546 set_mem_addr_space (x, as);
3547 targetm.encode_section_info (obj, x, true);
3549 else
3551 x = gen_raw_REG (address_mode, (*regno)++);
3552 x = gen_rtx_MEM (DECL_MODE (obj), x);
3553 set_mem_addr_space (x, as);
3556 return x;
3559 /* Prepares decl_rtl for variables referred in *EXPR_P. Callback for
3560 walk_tree. DATA contains the actual fake register number. */
3562 static tree
3563 prepare_decl_rtl (tree *expr_p, int *ws, void *data)
3565 tree obj = NULL_TREE;
3566 rtx x = NULL_RTX;
3567 int *regno = (int *) data;
3569 switch (TREE_CODE (*expr_p))
3571 case ADDR_EXPR:
3572 for (expr_p = &TREE_OPERAND (*expr_p, 0);
3573 handled_component_p (*expr_p);
3574 expr_p = &TREE_OPERAND (*expr_p, 0))
3575 continue;
3576 obj = *expr_p;
3577 if (DECL_P (obj) && HAS_RTL_P (obj) && !DECL_RTL_SET_P (obj))
3578 x = produce_memory_decl_rtl (obj, regno);
3579 break;
3581 case SSA_NAME:
3582 *ws = 0;
3583 obj = SSA_NAME_VAR (*expr_p);
3584 /* Defer handling of anonymous SSA_NAMEs to the expander. */
3585 if (!obj)
3586 return NULL_TREE;
3587 if (!DECL_RTL_SET_P (obj))
3588 x = gen_raw_REG (DECL_MODE (obj), (*regno)++);
3589 break;
3591 case VAR_DECL:
3592 case PARM_DECL:
3593 case RESULT_DECL:
3594 *ws = 0;
3595 obj = *expr_p;
3597 if (DECL_RTL_SET_P (obj))
3598 break;
3600 if (DECL_MODE (obj) == BLKmode)
3601 x = produce_memory_decl_rtl (obj, regno);
3602 else
3603 x = gen_raw_REG (DECL_MODE (obj), (*regno)++);
3605 break;
3607 default:
3608 break;
3611 if (x)
3613 decl_rtl_to_reset.safe_push (obj);
3614 SET_DECL_RTL (obj, x);
3617 return NULL_TREE;
3620 /* Determines cost of the computation of EXPR. */
3622 static unsigned
3623 computation_cost (tree expr, bool speed)
3625 rtx_insn *seq;
3626 rtx rslt;
3627 tree type = TREE_TYPE (expr);
3628 unsigned cost;
3629 /* Avoid using hard regs in ways which may be unsupported. */
3630 int regno = LAST_VIRTUAL_REGISTER + 1;
3631 struct cgraph_node *node = cgraph_node::get (current_function_decl);
3632 enum node_frequency real_frequency = node->frequency;
3634 node->frequency = NODE_FREQUENCY_NORMAL;
3635 crtl->maybe_hot_insn_p = speed;
3636 walk_tree (&expr, prepare_decl_rtl, &regno, NULL);
3637 start_sequence ();
3638 rslt = expand_expr (expr, NULL_RTX, TYPE_MODE (type), EXPAND_NORMAL);
3639 seq = get_insns ();
3640 end_sequence ();
3641 default_rtl_profile ();
3642 node->frequency = real_frequency;
3644 cost = seq_cost (seq, speed);
3645 if (MEM_P (rslt))
3646 cost += address_cost (XEXP (rslt, 0), TYPE_MODE (type),
3647 TYPE_ADDR_SPACE (type), speed);
3648 else if (!REG_P (rslt))
3649 cost += set_src_cost (rslt, TYPE_MODE (type), speed);
3651 return cost;
3654 /* Returns variable containing the value of candidate CAND at statement AT. */
3656 static tree
3657 var_at_stmt (struct loop *loop, struct iv_cand *cand, gimple *stmt)
3659 if (stmt_after_increment (loop, cand, stmt))
3660 return cand->var_after;
3661 else
3662 return cand->var_before;
3665 /* If A is (TYPE) BA and B is (TYPE) BB, and the types of BA and BB have the
3666 same precision that is at least as wide as the precision of TYPE, stores
3667 BA to A and BB to B, and returns the type of BA. Otherwise, returns the
3668 type of A and B. */
3670 static tree
3671 determine_common_wider_type (tree *a, tree *b)
3673 tree wider_type = NULL;
3674 tree suba, subb;
3675 tree atype = TREE_TYPE (*a);
3677 if (CONVERT_EXPR_P (*a))
3679 suba = TREE_OPERAND (*a, 0);
3680 wider_type = TREE_TYPE (suba);
3681 if (TYPE_PRECISION (wider_type) < TYPE_PRECISION (atype))
3682 return atype;
3684 else
3685 return atype;
3687 if (CONVERT_EXPR_P (*b))
3689 subb = TREE_OPERAND (*b, 0);
3690 if (TYPE_PRECISION (wider_type) != TYPE_PRECISION (TREE_TYPE (subb)))
3691 return atype;
3693 else
3694 return atype;
3696 *a = suba;
3697 *b = subb;
3698 return wider_type;
3701 /* Determines the expression by that USE is expressed from induction variable
3702 CAND at statement AT in LOOP. The expression is stored in a decomposed
3703 form into AFF. Returns false if USE cannot be expressed using CAND. */
3705 static bool
3706 get_computation_aff (struct loop *loop,
3707 struct iv_use *use, struct iv_cand *cand, gimple *at,
3708 struct aff_tree *aff)
3710 tree ubase = use->iv->base;
3711 tree ustep = use->iv->step;
3712 tree cbase = cand->iv->base;
3713 tree cstep = cand->iv->step, cstep_common;
3714 tree utype = TREE_TYPE (ubase), ctype = TREE_TYPE (cbase);
3715 tree common_type, var;
3716 tree uutype;
3717 aff_tree cbase_aff, var_aff;
3718 widest_int rat;
3720 if (TYPE_PRECISION (utype) > TYPE_PRECISION (ctype))
3722 /* We do not have a precision to express the values of use. */
3723 return false;
3726 var = var_at_stmt (loop, cand, at);
3727 uutype = unsigned_type_for (utype);
3729 /* If the conversion is not noop, perform it. */
3730 if (TYPE_PRECISION (utype) < TYPE_PRECISION (ctype))
3732 if (cand->orig_iv != NULL && CONVERT_EXPR_P (cbase)
3733 && (CONVERT_EXPR_P (cstep) || TREE_CODE (cstep) == INTEGER_CST))
3735 tree inner_base, inner_step, inner_type;
3736 inner_base = TREE_OPERAND (cbase, 0);
3737 if (CONVERT_EXPR_P (cstep))
3738 inner_step = TREE_OPERAND (cstep, 0);
3739 else
3740 inner_step = cstep;
3742 inner_type = TREE_TYPE (inner_base);
3743 /* If candidate is added from a biv whose type is smaller than
3744 ctype, we know both candidate and the biv won't overflow.
3745 In this case, it's safe to skip the convertion in candidate.
3746 As an example, (unsigned short)((unsigned long)A) equals to
3747 (unsigned short)A, if A has a type no larger than short. */
3748 if (TYPE_PRECISION (inner_type) <= TYPE_PRECISION (uutype))
3750 cbase = inner_base;
3751 cstep = inner_step;
3754 cstep = fold_convert (uutype, cstep);
3755 cbase = fold_convert (uutype, cbase);
3756 var = fold_convert (uutype, var);
3759 /* Ratio is 1 when computing the value of biv cand by itself.
3760 We can't rely on constant_multiple_of in this case because the
3761 use is created after the original biv is selected. The call
3762 could fail because of inconsistent fold behavior. See PR68021
3763 for more information. */
3764 if (cand->pos == IP_ORIGINAL && cand->incremented_at == use->stmt)
3766 gcc_assert (is_gimple_assign (use->stmt));
3767 gcc_assert (use->iv->ssa_name == cand->var_after);
3768 gcc_assert (gimple_assign_lhs (use->stmt) == cand->var_after);
3769 rat = 1;
3771 else if (!constant_multiple_of (ustep, cstep, &rat))
3772 return false;
3774 /* In case both UBASE and CBASE are shortened to UUTYPE from some common
3775 type, we achieve better folding by computing their difference in this
3776 wider type, and cast the result to UUTYPE. We do not need to worry about
3777 overflows, as all the arithmetics will in the end be performed in UUTYPE
3778 anyway. */
3779 common_type = determine_common_wider_type (&ubase, &cbase);
3781 /* use = ubase - ratio * cbase + ratio * var. */
3782 tree_to_aff_combination (ubase, common_type, aff);
3783 tree_to_aff_combination (cbase, common_type, &cbase_aff);
3784 tree_to_aff_combination (var, uutype, &var_aff);
3786 /* We need to shift the value if we are after the increment. */
3787 if (stmt_after_increment (loop, cand, at))
3789 aff_tree cstep_aff;
3791 if (common_type != uutype)
3792 cstep_common = fold_convert (common_type, cstep);
3793 else
3794 cstep_common = cstep;
3796 tree_to_aff_combination (cstep_common, common_type, &cstep_aff);
3797 aff_combination_add (&cbase_aff, &cstep_aff);
3800 aff_combination_scale (&cbase_aff, -rat);
3801 aff_combination_add (aff, &cbase_aff);
3802 if (common_type != uutype)
3803 aff_combination_convert (aff, uutype);
3805 aff_combination_scale (&var_aff, rat);
3806 aff_combination_add (aff, &var_aff);
3808 return true;
3811 /* Return the type of USE. */
3813 static tree
3814 get_use_type (struct iv_use *use)
3816 tree base_type = TREE_TYPE (use->iv->base);
3817 tree type;
3819 if (use->type == USE_ADDRESS)
3821 /* The base_type may be a void pointer. Create a pointer type based on
3822 the mem_ref instead. */
3823 type = build_pointer_type (TREE_TYPE (*use->op_p));
3824 gcc_assert (TYPE_ADDR_SPACE (TREE_TYPE (type))
3825 == TYPE_ADDR_SPACE (TREE_TYPE (base_type)));
3827 else
3828 type = base_type;
3830 return type;
3833 /* Determines the expression by that USE is expressed from induction variable
3834 CAND at statement AT in LOOP. The computation is unshared. */
3836 static tree
3837 get_computation_at (struct loop *loop,
3838 struct iv_use *use, struct iv_cand *cand, gimple *at)
3840 aff_tree aff;
3841 tree type = get_use_type (use);
3843 if (!get_computation_aff (loop, use, cand, at, &aff))
3844 return NULL_TREE;
3845 unshare_aff_combination (&aff);
3846 return fold_convert (type, aff_combination_to_tree (&aff));
3849 /* Determines the expression by that USE is expressed from induction variable
3850 CAND in LOOP. The computation is unshared. */
3852 static tree
3853 get_computation (struct loop *loop, struct iv_use *use, struct iv_cand *cand)
3855 return get_computation_at (loop, use, cand, use->stmt);
3858 /* Adjust the cost COST for being in loop setup rather than loop body.
3859 If we're optimizing for space, the loop setup overhead is constant;
3860 if we're optimizing for speed, amortize it over the per-iteration cost. */
3861 static unsigned
3862 adjust_setup_cost (struct ivopts_data *data, unsigned cost)
3864 if (cost == INFTY)
3865 return cost;
3866 else if (optimize_loop_for_speed_p (data->current_loop))
3867 return cost / avg_loop_niter (data->current_loop);
3868 else
3869 return cost;
3872 /* Returns true if multiplying by RATIO is allowed in an address. Test the
3873 validity for a memory reference accessing memory of mode MODE in
3874 address space AS. */
3877 bool
3878 multiplier_allowed_in_address_p (HOST_WIDE_INT ratio, machine_mode mode,
3879 addr_space_t as)
3881 #define MAX_RATIO 128
3882 unsigned int data_index = (int) as * MAX_MACHINE_MODE + (int) mode;
3883 static vec<sbitmap> valid_mult_list;
3884 sbitmap valid_mult;
3886 if (data_index >= valid_mult_list.length ())
3887 valid_mult_list.safe_grow_cleared (data_index + 1);
3889 valid_mult = valid_mult_list[data_index];
3890 if (!valid_mult)
3892 machine_mode address_mode = targetm.addr_space.address_mode (as);
3893 rtx reg1 = gen_raw_REG (address_mode, LAST_VIRTUAL_REGISTER + 1);
3894 rtx reg2 = gen_raw_REG (address_mode, LAST_VIRTUAL_REGISTER + 2);
3895 rtx addr, scaled;
3896 HOST_WIDE_INT i;
3898 valid_mult = sbitmap_alloc (2 * MAX_RATIO + 1);
3899 bitmap_clear (valid_mult);
3900 scaled = gen_rtx_fmt_ee (MULT, address_mode, reg1, NULL_RTX);
3901 addr = gen_rtx_fmt_ee (PLUS, address_mode, scaled, reg2);
3902 for (i = -MAX_RATIO; i <= MAX_RATIO; i++)
3904 XEXP (scaled, 1) = gen_int_mode (i, address_mode);
3905 if (memory_address_addr_space_p (mode, addr, as)
3906 || memory_address_addr_space_p (mode, scaled, as))
3907 bitmap_set_bit (valid_mult, i + MAX_RATIO);
3910 if (dump_file && (dump_flags & TDF_DETAILS))
3912 fprintf (dump_file, " allowed multipliers:");
3913 for (i = -MAX_RATIO; i <= MAX_RATIO; i++)
3914 if (bitmap_bit_p (valid_mult, i + MAX_RATIO))
3915 fprintf (dump_file, " %d", (int) i);
3916 fprintf (dump_file, "\n");
3917 fprintf (dump_file, "\n");
3920 valid_mult_list[data_index] = valid_mult;
3923 if (ratio > MAX_RATIO || ratio < -MAX_RATIO)
3924 return false;
3926 return bitmap_bit_p (valid_mult, ratio + MAX_RATIO);
3929 /* Returns cost of address in shape symbol + var + OFFSET + RATIO * index.
3930 If SYMBOL_PRESENT is false, symbol is omitted. If VAR_PRESENT is false,
3931 variable is omitted. Compute the cost for a memory reference that accesses
3932 a memory location of mode MEM_MODE in address space AS.
3934 MAY_AUTOINC is set to true if the autoincrement (increasing index by
3935 size of MEM_MODE / RATIO) is available. To make this determination, we
3936 look at the size of the increment to be made, which is given in CSTEP.
3937 CSTEP may be zero if the step is unknown.
3938 STMT_AFTER_INC is true iff the statement we're looking at is after the
3939 increment of the original biv.
3941 TODO -- there must be some better way. This all is quite crude. */
3943 enum ainc_type
3945 AINC_PRE_INC, /* Pre increment. */
3946 AINC_PRE_DEC, /* Pre decrement. */
3947 AINC_POST_INC, /* Post increment. */
3948 AINC_POST_DEC, /* Post decrement. */
3949 AINC_NONE /* Also the number of auto increment types. */
3952 struct address_cost_data
3954 HOST_WIDE_INT min_offset, max_offset;
3955 unsigned costs[2][2][2][2];
3956 unsigned ainc_costs[AINC_NONE];
3960 static comp_cost
3961 get_address_cost (bool symbol_present, bool var_present,
3962 unsigned HOST_WIDE_INT offset, HOST_WIDE_INT ratio,
3963 HOST_WIDE_INT cstep, machine_mode mem_mode,
3964 addr_space_t as, bool speed,
3965 bool stmt_after_inc, bool *may_autoinc)
3967 machine_mode address_mode = targetm.addr_space.address_mode (as);
3968 static vec<address_cost_data *> address_cost_data_list;
3969 unsigned int data_index = (int) as * MAX_MACHINE_MODE + (int) mem_mode;
3970 address_cost_data *data;
3971 static bool has_preinc[MAX_MACHINE_MODE], has_postinc[MAX_MACHINE_MODE];
3972 static bool has_predec[MAX_MACHINE_MODE], has_postdec[MAX_MACHINE_MODE];
3973 unsigned cost, acost, complexity;
3974 enum ainc_type autoinc_type;
3975 bool offset_p, ratio_p, autoinc;
3976 HOST_WIDE_INT s_offset, autoinc_offset, msize;
3977 unsigned HOST_WIDE_INT mask;
3978 unsigned bits;
3980 if (data_index >= address_cost_data_list.length ())
3981 address_cost_data_list.safe_grow_cleared (data_index + 1);
3983 data = address_cost_data_list[data_index];
3984 if (!data)
3986 HOST_WIDE_INT i;
3987 HOST_WIDE_INT rat, off = 0;
3988 int old_cse_not_expected, width;
3989 unsigned sym_p, var_p, off_p, rat_p, add_c;
3990 rtx_insn *seq;
3991 rtx addr, base;
3992 rtx reg0, reg1;
3994 data = (address_cost_data *) xcalloc (1, sizeof (*data));
3996 reg1 = gen_raw_REG (address_mode, LAST_VIRTUAL_REGISTER + 1);
3998 width = GET_MODE_BITSIZE (address_mode) - 1;
3999 if (width > (HOST_BITS_PER_WIDE_INT - 1))
4000 width = HOST_BITS_PER_WIDE_INT - 1;
4001 addr = gen_rtx_fmt_ee (PLUS, address_mode, reg1, NULL_RTX);
4003 for (i = width; i >= 0; i--)
4005 off = -(HOST_WIDE_INT_1U << i);
4006 XEXP (addr, 1) = gen_int_mode (off, address_mode);
4007 if (memory_address_addr_space_p (mem_mode, addr, as))
4008 break;
4010 data->min_offset = (i == -1? 0 : off);
4012 for (i = width; i >= 0; i--)
4014 off = (HOST_WIDE_INT_1U << i) - 1;
4015 XEXP (addr, 1) = gen_int_mode (off, address_mode);
4016 if (memory_address_addr_space_p (mem_mode, addr, as))
4017 break;
4018 /* For some strict-alignment targets, the offset must be naturally
4019 aligned. Try an aligned offset if mem_mode is not QImode. */
4020 off = mem_mode != QImode
4021 ? (HOST_WIDE_INT_1U << i)
4022 - GET_MODE_SIZE (mem_mode)
4023 : 0;
4024 if (off > 0)
4026 XEXP (addr, 1) = gen_int_mode (off, address_mode);
4027 if (memory_address_addr_space_p (mem_mode, addr, as))
4028 break;
4031 if (i == -1)
4032 off = 0;
4033 data->max_offset = off;
4035 if (dump_file && (dump_flags & TDF_DETAILS))
4037 fprintf (dump_file, "get_address_cost:\n");
4038 fprintf (dump_file, " min offset %s " HOST_WIDE_INT_PRINT_DEC "\n",
4039 GET_MODE_NAME (mem_mode),
4040 data->min_offset);
4041 fprintf (dump_file, " max offset %s " HOST_WIDE_INT_PRINT_DEC "\n",
4042 GET_MODE_NAME (mem_mode),
4043 data->max_offset);
4046 rat = 1;
4047 for (i = 2; i <= MAX_RATIO; i++)
4048 if (multiplier_allowed_in_address_p (i, mem_mode, as))
4050 rat = i;
4051 break;
4054 /* Compute the cost of various addressing modes. */
4055 acost = 0;
4056 reg0 = gen_raw_REG (address_mode, LAST_VIRTUAL_REGISTER + 1);
4057 reg1 = gen_raw_REG (address_mode, LAST_VIRTUAL_REGISTER + 2);
4059 if (USE_LOAD_PRE_DECREMENT (mem_mode)
4060 || USE_STORE_PRE_DECREMENT (mem_mode))
4062 addr = gen_rtx_PRE_DEC (address_mode, reg0);
4063 has_predec[mem_mode]
4064 = memory_address_addr_space_p (mem_mode, addr, as);
4066 if (has_predec[mem_mode])
4067 data->ainc_costs[AINC_PRE_DEC]
4068 = address_cost (addr, mem_mode, as, speed);
4070 if (USE_LOAD_POST_DECREMENT (mem_mode)
4071 || USE_STORE_POST_DECREMENT (mem_mode))
4073 addr = gen_rtx_POST_DEC (address_mode, reg0);
4074 has_postdec[mem_mode]
4075 = memory_address_addr_space_p (mem_mode, addr, as);
4077 if (has_postdec[mem_mode])
4078 data->ainc_costs[AINC_POST_DEC]
4079 = address_cost (addr, mem_mode, as, speed);
4081 if (USE_LOAD_PRE_INCREMENT (mem_mode)
4082 || USE_STORE_PRE_DECREMENT (mem_mode))
4084 addr = gen_rtx_PRE_INC (address_mode, reg0);
4085 has_preinc[mem_mode]
4086 = memory_address_addr_space_p (mem_mode, addr, as);
4088 if (has_preinc[mem_mode])
4089 data->ainc_costs[AINC_PRE_INC]
4090 = address_cost (addr, mem_mode, as, speed);
4092 if (USE_LOAD_POST_INCREMENT (mem_mode)
4093 || USE_STORE_POST_INCREMENT (mem_mode))
4095 addr = gen_rtx_POST_INC (address_mode, reg0);
4096 has_postinc[mem_mode]
4097 = memory_address_addr_space_p (mem_mode, addr, as);
4099 if (has_postinc[mem_mode])
4100 data->ainc_costs[AINC_POST_INC]
4101 = address_cost (addr, mem_mode, as, speed);
4103 for (i = 0; i < 16; i++)
4105 sym_p = i & 1;
4106 var_p = (i >> 1) & 1;
4107 off_p = (i >> 2) & 1;
4108 rat_p = (i >> 3) & 1;
4110 addr = reg0;
4111 if (rat_p)
4112 addr = gen_rtx_fmt_ee (MULT, address_mode, addr,
4113 gen_int_mode (rat, address_mode));
4115 if (var_p)
4116 addr = gen_rtx_fmt_ee (PLUS, address_mode, addr, reg1);
4118 if (sym_p)
4120 base = gen_rtx_SYMBOL_REF (address_mode, ggc_strdup (""));
4121 /* ??? We can run into trouble with some backends by presenting
4122 it with symbols which haven't been properly passed through
4123 targetm.encode_section_info. By setting the local bit, we
4124 enhance the probability of things working. */
4125 SYMBOL_REF_FLAGS (base) = SYMBOL_FLAG_LOCAL;
4127 if (off_p)
4128 base = gen_rtx_fmt_e (CONST, address_mode,
4129 gen_rtx_fmt_ee
4130 (PLUS, address_mode, base,
4131 gen_int_mode (off, address_mode)));
4133 else if (off_p)
4134 base = gen_int_mode (off, address_mode);
4135 else
4136 base = NULL_RTX;
4138 if (base)
4139 addr = gen_rtx_fmt_ee (PLUS, address_mode, addr, base);
4141 start_sequence ();
4142 /* To avoid splitting addressing modes, pretend that no cse will
4143 follow. */
4144 old_cse_not_expected = cse_not_expected;
4145 cse_not_expected = true;
4146 addr = memory_address_addr_space (mem_mode, addr, as);
4147 cse_not_expected = old_cse_not_expected;
4148 seq = get_insns ();
4149 end_sequence ();
4151 acost = seq_cost (seq, speed);
4152 acost += address_cost (addr, mem_mode, as, speed);
4154 if (!acost)
4155 acost = 1;
4156 data->costs[sym_p][var_p][off_p][rat_p] = acost;
4159 /* On some targets, it is quite expensive to load symbol to a register,
4160 which makes addresses that contain symbols look much more expensive.
4161 However, the symbol will have to be loaded in any case before the
4162 loop (and quite likely we have it in register already), so it does not
4163 make much sense to penalize them too heavily. So make some final
4164 tweaks for the SYMBOL_PRESENT modes:
4166 If VAR_PRESENT is false, and the mode obtained by changing symbol to
4167 var is cheaper, use this mode with small penalty.
4168 If VAR_PRESENT is true, try whether the mode with
4169 SYMBOL_PRESENT = false is cheaper even with cost of addition, and
4170 if this is the case, use it. */
4171 add_c = add_cost (speed, address_mode);
4172 for (i = 0; i < 8; i++)
4174 var_p = i & 1;
4175 off_p = (i >> 1) & 1;
4176 rat_p = (i >> 2) & 1;
4178 acost = data->costs[0][1][off_p][rat_p] + 1;
4179 if (var_p)
4180 acost += add_c;
4182 if (acost < data->costs[1][var_p][off_p][rat_p])
4183 data->costs[1][var_p][off_p][rat_p] = acost;
4186 if (dump_file && (dump_flags & TDF_DETAILS))
4188 fprintf (dump_file, "<Address Costs>:\n");
4190 for (i = 0; i < 16; i++)
4192 sym_p = i & 1;
4193 var_p = (i >> 1) & 1;
4194 off_p = (i >> 2) & 1;
4195 rat_p = (i >> 3) & 1;
4197 fprintf (dump_file, " ");
4198 if (sym_p)
4199 fprintf (dump_file, "sym + ");
4200 if (var_p)
4201 fprintf (dump_file, "var + ");
4202 if (off_p)
4203 fprintf (dump_file, "cst + ");
4204 if (rat_p)
4205 fprintf (dump_file, "rat * ");
4207 acost = data->costs[sym_p][var_p][off_p][rat_p];
4208 fprintf (dump_file, "index costs %d\n", acost);
4210 if (has_predec[mem_mode] || has_postdec[mem_mode]
4211 || has_preinc[mem_mode] || has_postinc[mem_mode])
4212 fprintf (dump_file, " May include autoinc/dec\n");
4213 fprintf (dump_file, "\n");
4216 address_cost_data_list[data_index] = data;
4219 bits = GET_MODE_BITSIZE (address_mode);
4220 mask = ~(HOST_WIDE_INT_M1U << (bits - 1) << 1);
4221 offset &= mask;
4222 if ((offset >> (bits - 1) & 1))
4223 offset |= ~mask;
4224 s_offset = offset;
4226 autoinc = false;
4227 autoinc_type = AINC_NONE;
4228 msize = GET_MODE_SIZE (mem_mode);
4229 autoinc_offset = offset;
4230 if (stmt_after_inc)
4231 autoinc_offset += ratio * cstep;
4232 if (symbol_present || var_present || ratio != 1)
4233 autoinc = false;
4234 else
4236 if (has_postinc[mem_mode] && autoinc_offset == 0
4237 && msize == cstep)
4238 autoinc_type = AINC_POST_INC;
4239 else if (has_postdec[mem_mode] && autoinc_offset == 0
4240 && msize == -cstep)
4241 autoinc_type = AINC_POST_DEC;
4242 else if (has_preinc[mem_mode] && autoinc_offset == msize
4243 && msize == cstep)
4244 autoinc_type = AINC_PRE_INC;
4245 else if (has_predec[mem_mode] && autoinc_offset == -msize
4246 && msize == -cstep)
4247 autoinc_type = AINC_PRE_DEC;
4249 if (autoinc_type != AINC_NONE)
4250 autoinc = true;
4253 cost = 0;
4254 offset_p = (s_offset != 0
4255 && data->min_offset <= s_offset
4256 && s_offset <= data->max_offset);
4257 ratio_p = (ratio != 1
4258 && multiplier_allowed_in_address_p (ratio, mem_mode, as));
4260 if (ratio != 1 && !ratio_p)
4261 cost += mult_by_coeff_cost (ratio, address_mode, speed);
4263 if (s_offset && !offset_p && !symbol_present)
4264 cost += add_cost (speed, address_mode);
4266 if (may_autoinc)
4267 *may_autoinc = autoinc;
4268 if (autoinc)
4269 acost = data->ainc_costs[autoinc_type];
4270 else
4271 acost = data->costs[symbol_present][var_present][offset_p][ratio_p];
4272 complexity = (symbol_present != 0) + (var_present != 0) + offset_p + ratio_p;
4273 return comp_cost (cost + acost, complexity);
4276 /* Calculate the SPEED or size cost of shiftadd EXPR in MODE. MULT is the
4277 EXPR operand holding the shift. COST0 and COST1 are the costs for
4278 calculating the operands of EXPR. Returns true if successful, and returns
4279 the cost in COST. */
4281 static bool
4282 get_shiftadd_cost (tree expr, machine_mode mode, comp_cost cost0,
4283 comp_cost cost1, tree mult, bool speed, comp_cost *cost)
4285 comp_cost res;
4286 tree op1 = TREE_OPERAND (expr, 1);
4287 tree cst = TREE_OPERAND (mult, 1);
4288 tree multop = TREE_OPERAND (mult, 0);
4289 int m = exact_log2 (int_cst_value (cst));
4290 int maxm = MIN (BITS_PER_WORD, GET_MODE_BITSIZE (mode));
4291 int as_cost, sa_cost;
4292 bool mult_in_op1;
4294 if (!(m >= 0 && m < maxm))
4295 return false;
4297 STRIP_NOPS (op1);
4298 mult_in_op1 = operand_equal_p (op1, mult, 0);
4300 as_cost = add_cost (speed, mode) + shift_cost (speed, mode, m);
4302 /* If the target has a cheap shift-and-add or shift-and-sub instruction,
4303 use that in preference to a shift insn followed by an add insn. */
4304 sa_cost = (TREE_CODE (expr) != MINUS_EXPR
4305 ? shiftadd_cost (speed, mode, m)
4306 : (mult_in_op1
4307 ? shiftsub1_cost (speed, mode, m)
4308 : shiftsub0_cost (speed, mode, m)));
4310 res = comp_cost (MIN (as_cost, sa_cost), 0);
4311 res += (mult_in_op1 ? cost0 : cost1);
4313 STRIP_NOPS (multop);
4314 if (!is_gimple_val (multop))
4315 res += force_expr_to_var_cost (multop, speed);
4317 *cost = res;
4318 return true;
4321 /* Estimates cost of forcing expression EXPR into a variable. */
4323 static comp_cost
4324 force_expr_to_var_cost (tree expr, bool speed)
4326 static bool costs_initialized = false;
4327 static unsigned integer_cost [2];
4328 static unsigned symbol_cost [2];
4329 static unsigned address_cost [2];
4330 tree op0, op1;
4331 comp_cost cost0, cost1, cost;
4332 machine_mode mode;
4334 if (!costs_initialized)
4336 tree type = build_pointer_type (integer_type_node);
4337 tree var, addr;
4338 rtx x;
4339 int i;
4341 var = create_tmp_var_raw (integer_type_node, "test_var");
4342 TREE_STATIC (var) = 1;
4343 x = produce_memory_decl_rtl (var, NULL);
4344 SET_DECL_RTL (var, x);
4346 addr = build1 (ADDR_EXPR, type, var);
4349 for (i = 0; i < 2; i++)
4351 integer_cost[i] = computation_cost (build_int_cst (integer_type_node,
4352 2000), i);
4354 symbol_cost[i] = computation_cost (addr, i) + 1;
4356 address_cost[i]
4357 = computation_cost (fold_build_pointer_plus_hwi (addr, 2000), i) + 1;
4358 if (dump_file && (dump_flags & TDF_DETAILS))
4360 fprintf (dump_file, "force_expr_to_var_cost %s costs:\n", i ? "speed" : "size");
4361 fprintf (dump_file, " integer %d\n", (int) integer_cost[i]);
4362 fprintf (dump_file, " symbol %d\n", (int) symbol_cost[i]);
4363 fprintf (dump_file, " address %d\n", (int) address_cost[i]);
4364 fprintf (dump_file, " other %d\n", (int) target_spill_cost[i]);
4365 fprintf (dump_file, "\n");
4369 costs_initialized = true;
4372 STRIP_NOPS (expr);
4374 if (SSA_VAR_P (expr))
4375 return no_cost;
4377 if (is_gimple_min_invariant (expr))
4379 if (TREE_CODE (expr) == INTEGER_CST)
4380 return comp_cost (integer_cost [speed], 0);
4382 if (TREE_CODE (expr) == ADDR_EXPR)
4384 tree obj = TREE_OPERAND (expr, 0);
4386 if (TREE_CODE (obj) == VAR_DECL
4387 || TREE_CODE (obj) == PARM_DECL
4388 || TREE_CODE (obj) == RESULT_DECL)
4389 return comp_cost (symbol_cost [speed], 0);
4392 return comp_cost (address_cost [speed], 0);
4395 switch (TREE_CODE (expr))
4397 case POINTER_PLUS_EXPR:
4398 case PLUS_EXPR:
4399 case MINUS_EXPR:
4400 case MULT_EXPR:
4401 op0 = TREE_OPERAND (expr, 0);
4402 op1 = TREE_OPERAND (expr, 1);
4403 STRIP_NOPS (op0);
4404 STRIP_NOPS (op1);
4405 break;
4407 CASE_CONVERT:
4408 case NEGATE_EXPR:
4409 op0 = TREE_OPERAND (expr, 0);
4410 STRIP_NOPS (op0);
4411 op1 = NULL_TREE;
4412 break;
4414 default:
4415 /* Just an arbitrary value, FIXME. */
4416 return comp_cost (target_spill_cost[speed], 0);
4419 if (op0 == NULL_TREE
4420 || TREE_CODE (op0) == SSA_NAME || CONSTANT_CLASS_P (op0))
4421 cost0 = no_cost;
4422 else
4423 cost0 = force_expr_to_var_cost (op0, speed);
4425 if (op1 == NULL_TREE
4426 || TREE_CODE (op1) == SSA_NAME || CONSTANT_CLASS_P (op1))
4427 cost1 = no_cost;
4428 else
4429 cost1 = force_expr_to_var_cost (op1, speed);
4431 mode = TYPE_MODE (TREE_TYPE (expr));
4432 switch (TREE_CODE (expr))
4434 case POINTER_PLUS_EXPR:
4435 case PLUS_EXPR:
4436 case MINUS_EXPR:
4437 case NEGATE_EXPR:
4438 cost = comp_cost (add_cost (speed, mode), 0);
4439 if (TREE_CODE (expr) != NEGATE_EXPR)
4441 tree mult = NULL_TREE;
4442 comp_cost sa_cost;
4443 if (TREE_CODE (op1) == MULT_EXPR)
4444 mult = op1;
4445 else if (TREE_CODE (op0) == MULT_EXPR)
4446 mult = op0;
4448 if (mult != NULL_TREE
4449 && cst_and_fits_in_hwi (TREE_OPERAND (mult, 1))
4450 && get_shiftadd_cost (expr, mode, cost0, cost1, mult,
4451 speed, &sa_cost))
4452 return sa_cost;
4454 break;
4456 CASE_CONVERT:
4458 tree inner_mode, outer_mode;
4459 outer_mode = TREE_TYPE (expr);
4460 inner_mode = TREE_TYPE (op0);
4461 cost = comp_cost (convert_cost (TYPE_MODE (outer_mode),
4462 TYPE_MODE (inner_mode), speed), 0);
4464 break;
4466 case MULT_EXPR:
4467 if (cst_and_fits_in_hwi (op0))
4468 cost = comp_cost (mult_by_coeff_cost (int_cst_value (op0),
4469 mode, speed), 0);
4470 else if (cst_and_fits_in_hwi (op1))
4471 cost = comp_cost (mult_by_coeff_cost (int_cst_value (op1),
4472 mode, speed), 0);
4473 else
4474 return comp_cost (target_spill_cost [speed], 0);
4475 break;
4477 default:
4478 gcc_unreachable ();
4481 cost += cost0;
4482 cost += cost1;
4484 /* Bound the cost by target_spill_cost. The parts of complicated
4485 computations often are either loop invariant or at least can
4486 be shared between several iv uses, so letting this grow without
4487 limits would not give reasonable results. */
4488 if (cost.cost > (int) target_spill_cost [speed])
4489 cost.cost = target_spill_cost [speed];
4491 return cost;
4494 /* Estimates cost of forcing EXPR into a variable. DEPENDS_ON is a set of the
4495 invariants the computation depends on. */
4497 static comp_cost
4498 force_var_cost (struct ivopts_data *data,
4499 tree expr, bitmap *depends_on)
4501 if (depends_on)
4503 fd_ivopts_data = data;
4504 walk_tree (&expr, find_depends, depends_on, NULL);
4507 return force_expr_to_var_cost (expr, data->speed);
4510 /* Estimates cost of expressing address ADDR as var + symbol + offset. The
4511 value of offset is added to OFFSET, SYMBOL_PRESENT and VAR_PRESENT are set
4512 to false if the corresponding part is missing. DEPENDS_ON is a set of the
4513 invariants the computation depends on. */
4515 static comp_cost
4516 split_address_cost (struct ivopts_data *data,
4517 tree addr, bool *symbol_present, bool *var_present,
4518 unsigned HOST_WIDE_INT *offset, bitmap *depends_on)
4520 tree core;
4521 HOST_WIDE_INT bitsize;
4522 HOST_WIDE_INT bitpos;
4523 tree toffset;
4524 machine_mode mode;
4525 int unsignedp, reversep, volatilep;
4527 core = get_inner_reference (addr, &bitsize, &bitpos, &toffset, &mode,
4528 &unsignedp, &reversep, &volatilep);
4530 if (toffset != 0
4531 || bitpos % BITS_PER_UNIT != 0
4532 || reversep
4533 || TREE_CODE (core) != VAR_DECL)
4535 *symbol_present = false;
4536 *var_present = true;
4537 fd_ivopts_data = data;
4538 if (depends_on)
4539 walk_tree (&addr, find_depends, depends_on, NULL);
4541 return comp_cost (target_spill_cost[data->speed], 0);
4544 *offset += bitpos / BITS_PER_UNIT;
4545 if (TREE_STATIC (core)
4546 || DECL_EXTERNAL (core))
4548 *symbol_present = true;
4549 *var_present = false;
4550 return no_cost;
4553 *symbol_present = false;
4554 *var_present = true;
4555 return no_cost;
4558 /* Estimates cost of expressing difference of addresses E1 - E2 as
4559 var + symbol + offset. The value of offset is added to OFFSET,
4560 SYMBOL_PRESENT and VAR_PRESENT are set to false if the corresponding
4561 part is missing. DEPENDS_ON is a set of the invariants the computation
4562 depends on. */
4564 static comp_cost
4565 ptr_difference_cost (struct ivopts_data *data,
4566 tree e1, tree e2, bool *symbol_present, bool *var_present,
4567 unsigned HOST_WIDE_INT *offset, bitmap *depends_on)
4569 HOST_WIDE_INT diff = 0;
4570 aff_tree aff_e1, aff_e2;
4571 tree type;
4573 gcc_assert (TREE_CODE (e1) == ADDR_EXPR);
4575 if (ptr_difference_const (e1, e2, &diff))
4577 *offset += diff;
4578 *symbol_present = false;
4579 *var_present = false;
4580 return no_cost;
4583 if (integer_zerop (e2))
4584 return split_address_cost (data, TREE_OPERAND (e1, 0),
4585 symbol_present, var_present, offset, depends_on);
4587 *symbol_present = false;
4588 *var_present = true;
4590 type = signed_type_for (TREE_TYPE (e1));
4591 tree_to_aff_combination (e1, type, &aff_e1);
4592 tree_to_aff_combination (e2, type, &aff_e2);
4593 aff_combination_scale (&aff_e2, -1);
4594 aff_combination_add (&aff_e1, &aff_e2);
4596 return force_var_cost (data, aff_combination_to_tree (&aff_e1), depends_on);
4599 /* Estimates cost of expressing difference E1 - E2 as
4600 var + symbol + offset. The value of offset is added to OFFSET,
4601 SYMBOL_PRESENT and VAR_PRESENT are set to false if the corresponding
4602 part is missing. DEPENDS_ON is a set of the invariants the computation
4603 depends on. */
4605 static comp_cost
4606 difference_cost (struct ivopts_data *data,
4607 tree e1, tree e2, bool *symbol_present, bool *var_present,
4608 unsigned HOST_WIDE_INT *offset, bitmap *depends_on)
4610 machine_mode mode = TYPE_MODE (TREE_TYPE (e1));
4611 unsigned HOST_WIDE_INT off1, off2;
4612 aff_tree aff_e1, aff_e2;
4613 tree type;
4615 e1 = strip_offset (e1, &off1);
4616 e2 = strip_offset (e2, &off2);
4617 *offset += off1 - off2;
4619 STRIP_NOPS (e1);
4620 STRIP_NOPS (e2);
4622 if (TREE_CODE (e1) == ADDR_EXPR)
4623 return ptr_difference_cost (data, e1, e2, symbol_present, var_present,
4624 offset, depends_on);
4625 *symbol_present = false;
4627 if (operand_equal_p (e1, e2, 0))
4629 *var_present = false;
4630 return no_cost;
4633 *var_present = true;
4635 if (integer_zerop (e2))
4636 return force_var_cost (data, e1, depends_on);
4638 if (integer_zerop (e1))
4640 comp_cost cost = force_var_cost (data, e2, depends_on);
4641 cost += mult_by_coeff_cost (-1, mode, data->speed);
4642 return cost;
4645 type = signed_type_for (TREE_TYPE (e1));
4646 tree_to_aff_combination (e1, type, &aff_e1);
4647 tree_to_aff_combination (e2, type, &aff_e2);
4648 aff_combination_scale (&aff_e2, -1);
4649 aff_combination_add (&aff_e1, &aff_e2);
4651 return force_var_cost (data, aff_combination_to_tree (&aff_e1), depends_on);
4654 /* Returns true if AFF1 and AFF2 are identical. */
4656 static bool
4657 compare_aff_trees (aff_tree *aff1, aff_tree *aff2)
4659 unsigned i;
4661 if (aff1->n != aff2->n)
4662 return false;
4664 for (i = 0; i < aff1->n; i++)
4666 if (aff1->elts[i].coef != aff2->elts[i].coef)
4667 return false;
4669 if (!operand_equal_p (aff1->elts[i].val, aff2->elts[i].val, 0))
4670 return false;
4672 return true;
4675 /* Stores EXPR in DATA->inv_expr_tab, return pointer to iv_inv_expr_ent. */
4677 static iv_inv_expr_ent *
4678 record_inv_expr (struct ivopts_data *data, tree expr)
4680 struct iv_inv_expr_ent ent;
4681 struct iv_inv_expr_ent **slot;
4683 ent.expr = expr;
4684 ent.hash = iterative_hash_expr (expr, 0);
4685 slot = data->inv_expr_tab->find_slot (&ent, INSERT);
4687 if (!*slot)
4689 *slot = XNEW (struct iv_inv_expr_ent);
4690 (*slot)->expr = expr;
4691 (*slot)->hash = ent.hash;
4692 (*slot)->id = data->max_inv_expr_id++;
4695 return *slot;
4698 /* Returns the invariant expression if expression UBASE - RATIO * CBASE
4699 requires a new compiler generated temporary. Returns -1 otherwise.
4700 ADDRESS_P is a flag indicating if the expression is for address
4701 computation. */
4703 static iv_inv_expr_ent *
4704 get_loop_invariant_expr (struct ivopts_data *data, tree ubase,
4705 tree cbase, HOST_WIDE_INT ratio,
4706 bool address_p)
4708 aff_tree ubase_aff, cbase_aff;
4709 tree expr, ub, cb;
4711 STRIP_NOPS (ubase);
4712 STRIP_NOPS (cbase);
4713 ub = ubase;
4714 cb = cbase;
4716 if ((TREE_CODE (ubase) == INTEGER_CST)
4717 && (TREE_CODE (cbase) == INTEGER_CST))
4718 return NULL;
4720 /* Strips the constant part. */
4721 if (TREE_CODE (ubase) == PLUS_EXPR
4722 || TREE_CODE (ubase) == MINUS_EXPR
4723 || TREE_CODE (ubase) == POINTER_PLUS_EXPR)
4725 if (TREE_CODE (TREE_OPERAND (ubase, 1)) == INTEGER_CST)
4726 ubase = TREE_OPERAND (ubase, 0);
4729 /* Strips the constant part. */
4730 if (TREE_CODE (cbase) == PLUS_EXPR
4731 || TREE_CODE (cbase) == MINUS_EXPR
4732 || TREE_CODE (cbase) == POINTER_PLUS_EXPR)
4734 if (TREE_CODE (TREE_OPERAND (cbase, 1)) == INTEGER_CST)
4735 cbase = TREE_OPERAND (cbase, 0);
4738 if (address_p)
4740 if (((TREE_CODE (ubase) == SSA_NAME)
4741 || (TREE_CODE (ubase) == ADDR_EXPR
4742 && is_gimple_min_invariant (ubase)))
4743 && (TREE_CODE (cbase) == INTEGER_CST))
4744 return NULL;
4746 if (((TREE_CODE (cbase) == SSA_NAME)
4747 || (TREE_CODE (cbase) == ADDR_EXPR
4748 && is_gimple_min_invariant (cbase)))
4749 && (TREE_CODE (ubase) == INTEGER_CST))
4750 return NULL;
4753 if (ratio == 1)
4755 if (operand_equal_p (ubase, cbase, 0))
4756 return NULL;
4758 if (TREE_CODE (ubase) == ADDR_EXPR
4759 && TREE_CODE (cbase) == ADDR_EXPR)
4761 tree usym, csym;
4763 usym = TREE_OPERAND (ubase, 0);
4764 csym = TREE_OPERAND (cbase, 0);
4765 if (TREE_CODE (usym) == ARRAY_REF)
4767 tree ind = TREE_OPERAND (usym, 1);
4768 if (TREE_CODE (ind) == INTEGER_CST
4769 && tree_fits_shwi_p (ind)
4770 && tree_to_shwi (ind) == 0)
4771 usym = TREE_OPERAND (usym, 0);
4773 if (TREE_CODE (csym) == ARRAY_REF)
4775 tree ind = TREE_OPERAND (csym, 1);
4776 if (TREE_CODE (ind) == INTEGER_CST
4777 && tree_fits_shwi_p (ind)
4778 && tree_to_shwi (ind) == 0)
4779 csym = TREE_OPERAND (csym, 0);
4781 if (operand_equal_p (usym, csym, 0))
4782 return NULL;
4784 /* Now do more complex comparison */
4785 tree_to_aff_combination (ubase, TREE_TYPE (ubase), &ubase_aff);
4786 tree_to_aff_combination (cbase, TREE_TYPE (cbase), &cbase_aff);
4787 if (compare_aff_trees (&ubase_aff, &cbase_aff))
4788 return NULL;
4791 tree_to_aff_combination (ub, TREE_TYPE (ub), &ubase_aff);
4792 tree_to_aff_combination (cb, TREE_TYPE (cb), &cbase_aff);
4794 aff_combination_scale (&cbase_aff, -1 * ratio);
4795 aff_combination_add (&ubase_aff, &cbase_aff);
4796 expr = aff_combination_to_tree (&ubase_aff);
4797 return record_inv_expr (data, expr);
4800 /* Scale (multiply) the computed COST (except scratch part that should be
4801 hoisted out a loop) by header->frequency / AT->frequency,
4802 which makes expected cost more accurate. */
4804 static comp_cost
4805 get_scaled_computation_cost_at (ivopts_data *data, gimple *at, iv_cand *cand,
4806 comp_cost cost)
4808 int loop_freq = data->current_loop->header->frequency;
4809 int bb_freq = at->bb->frequency;
4810 if (loop_freq != 0)
4812 gcc_assert (cost.scratch <= cost.cost);
4813 int scaled_cost
4814 = cost.scratch + (cost.cost - cost.scratch) * bb_freq / loop_freq;
4816 if (dump_file && (dump_flags & TDF_DETAILS))
4817 fprintf (dump_file, "Scaling iv_use based on cand %d "
4818 "by %2.2f: %d (scratch: %d) -> %d (%d/%d)\n",
4819 cand->id, 1.0f * bb_freq / loop_freq, cost.cost,
4820 cost.scratch, scaled_cost, bb_freq, loop_freq);
4822 cost.cost = scaled_cost;
4825 return cost;
4828 /* Determines the cost of the computation by that USE is expressed
4829 from induction variable CAND. If ADDRESS_P is true, we just need
4830 to create an address from it, otherwise we want to get it into
4831 register. A set of invariants we depend on is stored in
4832 DEPENDS_ON. AT is the statement at that the value is computed.
4833 If CAN_AUTOINC is nonnull, use it to record whether autoinc
4834 addressing is likely. */
4836 static comp_cost
4837 get_computation_cost_at (struct ivopts_data *data,
4838 struct iv_use *use, struct iv_cand *cand,
4839 bool address_p, bitmap *depends_on, gimple *at,
4840 bool *can_autoinc,
4841 iv_inv_expr_ent **inv_expr)
4843 tree ubase = use->iv->base, ustep = use->iv->step;
4844 tree cbase, cstep;
4845 tree utype = TREE_TYPE (ubase), ctype;
4846 unsigned HOST_WIDE_INT cstepi, offset = 0;
4847 HOST_WIDE_INT ratio, aratio;
4848 bool var_present, symbol_present, stmt_is_after_inc;
4849 comp_cost cost;
4850 widest_int rat;
4851 bool speed = optimize_bb_for_speed_p (gimple_bb (at));
4852 machine_mode mem_mode = (address_p
4853 ? TYPE_MODE (TREE_TYPE (*use->op_p))
4854 : VOIDmode);
4856 if (depends_on)
4857 *depends_on = NULL;
4859 /* Only consider real candidates. */
4860 if (!cand->iv)
4861 return infinite_cost;
4863 cbase = cand->iv->base;
4864 cstep = cand->iv->step;
4865 ctype = TREE_TYPE (cbase);
4867 if (TYPE_PRECISION (utype) > TYPE_PRECISION (ctype))
4869 /* We do not have a precision to express the values of use. */
4870 return infinite_cost;
4873 if (address_p
4874 || (use->iv->base_object
4875 && cand->iv->base_object
4876 && POINTER_TYPE_P (TREE_TYPE (use->iv->base_object))
4877 && POINTER_TYPE_P (TREE_TYPE (cand->iv->base_object))))
4879 /* Do not try to express address of an object with computation based
4880 on address of a different object. This may cause problems in rtl
4881 level alias analysis (that does not expect this to be happening,
4882 as this is illegal in C), and would be unlikely to be useful
4883 anyway. */
4884 if (use->iv->base_object
4885 && cand->iv->base_object
4886 && !operand_equal_p (use->iv->base_object, cand->iv->base_object, 0))
4887 return infinite_cost;
4890 if (TYPE_PRECISION (utype) < TYPE_PRECISION (ctype))
4892 /* TODO -- add direct handling of this case. */
4893 goto fallback;
4896 /* CSTEPI is removed from the offset in case statement is after the
4897 increment. If the step is not constant, we use zero instead.
4898 This is a bit imprecise (there is the extra addition), but
4899 redundancy elimination is likely to transform the code so that
4900 it uses value of the variable before increment anyway,
4901 so it is not that much unrealistic. */
4902 if (cst_and_fits_in_hwi (cstep))
4903 cstepi = int_cst_value (cstep);
4904 else
4905 cstepi = 0;
4907 if (!constant_multiple_of (ustep, cstep, &rat))
4908 return infinite_cost;
4910 if (wi::fits_shwi_p (rat))
4911 ratio = rat.to_shwi ();
4912 else
4913 return infinite_cost;
4915 STRIP_NOPS (cbase);
4916 ctype = TREE_TYPE (cbase);
4918 stmt_is_after_inc = stmt_after_increment (data->current_loop, cand, at);
4920 /* use = ubase + ratio * (var - cbase). If either cbase is a constant
4921 or ratio == 1, it is better to handle this like
4923 ubase - ratio * cbase + ratio * var
4925 (also holds in the case ratio == -1, TODO. */
4927 if (cst_and_fits_in_hwi (cbase))
4929 offset = - ratio * (unsigned HOST_WIDE_INT) int_cst_value (cbase);
4930 cost = difference_cost (data,
4931 ubase, build_int_cst (utype, 0),
4932 &symbol_present, &var_present, &offset,
4933 depends_on);
4934 cost /= avg_loop_niter (data->current_loop);
4936 else if (ratio == 1)
4938 tree real_cbase = cbase;
4940 /* Check to see if any adjustment is needed. */
4941 if (cstepi == 0 && stmt_is_after_inc)
4943 aff_tree real_cbase_aff;
4944 aff_tree cstep_aff;
4946 tree_to_aff_combination (cbase, TREE_TYPE (real_cbase),
4947 &real_cbase_aff);
4948 tree_to_aff_combination (cstep, TREE_TYPE (cstep), &cstep_aff);
4950 aff_combination_add (&real_cbase_aff, &cstep_aff);
4951 real_cbase = aff_combination_to_tree (&real_cbase_aff);
4954 cost = difference_cost (data,
4955 ubase, real_cbase,
4956 &symbol_present, &var_present, &offset,
4957 depends_on);
4958 cost /= avg_loop_niter (data->current_loop);
4960 else if (address_p
4961 && !POINTER_TYPE_P (ctype)
4962 && multiplier_allowed_in_address_p
4963 (ratio, mem_mode,
4964 TYPE_ADDR_SPACE (TREE_TYPE (utype))))
4966 tree real_cbase = cbase;
4968 if (cstepi == 0 && stmt_is_after_inc)
4970 if (POINTER_TYPE_P (ctype))
4971 real_cbase = fold_build2 (POINTER_PLUS_EXPR, ctype, cbase, cstep);
4972 else
4973 real_cbase = fold_build2 (PLUS_EXPR, ctype, cbase, cstep);
4975 real_cbase = fold_build2 (MULT_EXPR, ctype, real_cbase,
4976 build_int_cst (ctype, ratio));
4977 cost = difference_cost (data,
4978 ubase, real_cbase,
4979 &symbol_present, &var_present, &offset,
4980 depends_on);
4981 cost /= avg_loop_niter (data->current_loop);
4983 else
4985 cost = force_var_cost (data, cbase, depends_on);
4986 cost += difference_cost (data, ubase, build_int_cst (utype, 0),
4987 &symbol_present, &var_present, &offset,
4988 depends_on);
4989 cost /= avg_loop_niter (data->current_loop);
4990 cost += add_cost (data->speed, TYPE_MODE (ctype));
4993 /* Record setup cost in scratch field. */
4994 cost.scratch = cost.cost;
4996 if (inv_expr && depends_on && *depends_on)
4998 *inv_expr = get_loop_invariant_expr (data, ubase, cbase, ratio,
4999 address_p);
5000 /* Clear depends on. */
5001 if (*inv_expr != NULL)
5002 bitmap_clear (*depends_on);
5005 /* If we are after the increment, the value of the candidate is higher by
5006 one iteration. */
5007 if (stmt_is_after_inc)
5008 offset -= ratio * cstepi;
5010 /* Now the computation is in shape symbol + var1 + const + ratio * var2.
5011 (symbol/var1/const parts may be omitted). If we are looking for an
5012 address, find the cost of addressing this. */
5013 if (address_p)
5015 cost += get_address_cost (symbol_present, var_present,
5016 offset, ratio, cstepi,
5017 mem_mode,
5018 TYPE_ADDR_SPACE (TREE_TYPE (utype)),
5019 speed, stmt_is_after_inc, can_autoinc);
5020 return get_scaled_computation_cost_at (data, at, cand, cost);
5023 /* Otherwise estimate the costs for computing the expression. */
5024 if (!symbol_present && !var_present && !offset)
5026 if (ratio != 1)
5027 cost += mult_by_coeff_cost (ratio, TYPE_MODE (ctype), speed);
5028 return get_scaled_computation_cost_at (data, at, cand, cost);
5031 /* Symbol + offset should be compile-time computable so consider that they
5032 are added once to the variable, if present. */
5033 if (var_present && (symbol_present || offset))
5034 cost += adjust_setup_cost (data,
5035 add_cost (speed, TYPE_MODE (ctype)));
5037 /* Having offset does not affect runtime cost in case it is added to
5038 symbol, but it increases complexity. */
5039 if (offset)
5040 cost.complexity++;
5042 cost += add_cost (speed, TYPE_MODE (ctype));
5044 aratio = ratio > 0 ? ratio : -ratio;
5045 if (aratio != 1)
5046 cost += mult_by_coeff_cost (aratio, TYPE_MODE (ctype), speed);
5048 return get_scaled_computation_cost_at (data, at, cand, cost);
5050 fallback:
5051 if (can_autoinc)
5052 *can_autoinc = false;
5054 /* Just get the expression, expand it and measure the cost. */
5055 tree comp = get_computation_at (data->current_loop, use, cand, at);
5057 if (!comp)
5058 return infinite_cost;
5060 if (address_p)
5061 comp = build_simple_mem_ref (comp);
5063 cost = comp_cost (computation_cost (comp, speed), 0);
5065 return get_scaled_computation_cost_at (data, at, cand, cost);
5068 /* Determines the cost of the computation by that USE is expressed
5069 from induction variable CAND. If ADDRESS_P is true, we just need
5070 to create an address from it, otherwise we want to get it into
5071 register. A set of invariants we depend on is stored in
5072 DEPENDS_ON. If CAN_AUTOINC is nonnull, use it to record whether
5073 autoinc addressing is likely. */
5075 static comp_cost
5076 get_computation_cost (struct ivopts_data *data,
5077 struct iv_use *use, struct iv_cand *cand,
5078 bool address_p, bitmap *depends_on,
5079 bool *can_autoinc, iv_inv_expr_ent **inv_expr)
5081 return get_computation_cost_at (data,
5082 use, cand, address_p, depends_on, use->stmt,
5083 can_autoinc, inv_expr);
5086 /* Determines cost of computing the use in GROUP with CAND in a generic
5087 expression. */
5089 static bool
5090 determine_group_iv_cost_generic (struct ivopts_data *data,
5091 struct iv_group *group, struct iv_cand *cand)
5093 comp_cost cost;
5094 iv_inv_expr_ent *inv_expr = NULL;
5095 bitmap depends_on = NULL;
5096 struct iv_use *use = group->vuses[0];
5098 /* The simple case first -- if we need to express value of the preserved
5099 original biv, the cost is 0. This also prevents us from counting the
5100 cost of increment twice -- once at this use and once in the cost of
5101 the candidate. */
5102 if (cand->pos == IP_ORIGINAL && cand->incremented_at == use->stmt)
5103 cost = no_cost;
5104 else
5105 cost = get_computation_cost (data, use, cand, false,
5106 &depends_on, NULL, &inv_expr);
5108 set_group_iv_cost (data, group, cand, cost, depends_on,
5109 NULL_TREE, ERROR_MARK, inv_expr);
5110 return !cost.infinite_cost_p ();
5113 /* Determines cost of computing uses in GROUP with CAND in addresses. */
5115 static bool
5116 determine_group_iv_cost_address (struct ivopts_data *data,
5117 struct iv_group *group, struct iv_cand *cand)
5119 unsigned i;
5120 bitmap depends_on;
5121 bool can_autoinc;
5122 iv_inv_expr_ent *inv_expr = NULL;
5123 struct iv_use *use = group->vuses[0];
5124 comp_cost sum_cost = no_cost, cost;
5126 cost = get_computation_cost (data, use, cand, true,
5127 &depends_on, &can_autoinc, &inv_expr);
5129 sum_cost = cost;
5130 if (!sum_cost.infinite_cost_p () && cand->ainc_use == use)
5132 if (can_autoinc)
5133 sum_cost -= cand->cost_step;
5134 /* If we generated the candidate solely for exploiting autoincrement
5135 opportunities, and it turns out it can't be used, set the cost to
5136 infinity to make sure we ignore it. */
5137 else if (cand->pos == IP_AFTER_USE || cand->pos == IP_BEFORE_USE)
5138 sum_cost = infinite_cost;
5141 /* Uses in a group can share setup code, so only add setup cost once. */
5142 cost -= cost.scratch;
5143 /* Compute and add costs for rest uses of this group. */
5144 for (i = 1; i < group->vuses.length () && !sum_cost.infinite_cost_p (); i++)
5146 struct iv_use *next = group->vuses[i];
5148 /* TODO: We could skip computing cost for sub iv_use when it has the
5149 same cost as the first iv_use, but the cost really depends on the
5150 offset and where the iv_use is. */
5151 cost = get_computation_cost (data, next, cand, true,
5152 NULL, &can_autoinc, NULL);
5153 sum_cost += cost;
5155 set_group_iv_cost (data, group, cand, sum_cost, depends_on,
5156 NULL_TREE, ERROR_MARK, inv_expr);
5158 return !sum_cost.infinite_cost_p ();
5161 /* Computes value of candidate CAND at position AT in iteration NITER, and
5162 stores it to VAL. */
5164 static void
5165 cand_value_at (struct loop *loop, struct iv_cand *cand, gimple *at, tree niter,
5166 aff_tree *val)
5168 aff_tree step, delta, nit;
5169 struct iv *iv = cand->iv;
5170 tree type = TREE_TYPE (iv->base);
5171 tree steptype = type;
5172 if (POINTER_TYPE_P (type))
5173 steptype = sizetype;
5174 steptype = unsigned_type_for (type);
5176 tree_to_aff_combination (iv->step, TREE_TYPE (iv->step), &step);
5177 aff_combination_convert (&step, steptype);
5178 tree_to_aff_combination (niter, TREE_TYPE (niter), &nit);
5179 aff_combination_convert (&nit, steptype);
5180 aff_combination_mult (&nit, &step, &delta);
5181 if (stmt_after_increment (loop, cand, at))
5182 aff_combination_add (&delta, &step);
5184 tree_to_aff_combination (iv->base, type, val);
5185 if (!POINTER_TYPE_P (type))
5186 aff_combination_convert (val, steptype);
5187 aff_combination_add (val, &delta);
5190 /* Returns period of induction variable iv. */
5192 static tree
5193 iv_period (struct iv *iv)
5195 tree step = iv->step, period, type;
5196 tree pow2div;
5198 gcc_assert (step && TREE_CODE (step) == INTEGER_CST);
5200 type = unsigned_type_for (TREE_TYPE (step));
5201 /* Period of the iv is lcm (step, type_range)/step -1,
5202 i.e., N*type_range/step - 1. Since type range is power
5203 of two, N == (step >> num_of_ending_zeros_binary (step),
5204 so the final result is
5206 (type_range >> num_of_ending_zeros_binary (step)) - 1
5209 pow2div = num_ending_zeros (step);
5211 period = build_low_bits_mask (type,
5212 (TYPE_PRECISION (type)
5213 - tree_to_uhwi (pow2div)));
5215 return period;
5218 /* Returns the comparison operator used when eliminating the iv USE. */
5220 static enum tree_code
5221 iv_elimination_compare (struct ivopts_data *data, struct iv_use *use)
5223 struct loop *loop = data->current_loop;
5224 basic_block ex_bb;
5225 edge exit;
5227 ex_bb = gimple_bb (use->stmt);
5228 exit = EDGE_SUCC (ex_bb, 0);
5229 if (flow_bb_inside_loop_p (loop, exit->dest))
5230 exit = EDGE_SUCC (ex_bb, 1);
5232 return (exit->flags & EDGE_TRUE_VALUE ? EQ_EXPR : NE_EXPR);
5235 /* Returns true if we can prove that BASE - OFFSET does not overflow. For now,
5236 we only detect the situation that BASE = SOMETHING + OFFSET, where the
5237 calculation is performed in non-wrapping type.
5239 TODO: More generally, we could test for the situation that
5240 BASE = SOMETHING + OFFSET' and OFFSET is between OFFSET' and zero.
5241 This would require knowing the sign of OFFSET. */
5243 static bool
5244 difference_cannot_overflow_p (struct ivopts_data *data, tree base, tree offset)
5246 enum tree_code code;
5247 tree e1, e2;
5248 aff_tree aff_e1, aff_e2, aff_offset;
5250 if (!nowrap_type_p (TREE_TYPE (base)))
5251 return false;
5253 base = expand_simple_operations (base);
5255 if (TREE_CODE (base) == SSA_NAME)
5257 gimple *stmt = SSA_NAME_DEF_STMT (base);
5259 if (gimple_code (stmt) != GIMPLE_ASSIGN)
5260 return false;
5262 code = gimple_assign_rhs_code (stmt);
5263 if (get_gimple_rhs_class (code) != GIMPLE_BINARY_RHS)
5264 return false;
5266 e1 = gimple_assign_rhs1 (stmt);
5267 e2 = gimple_assign_rhs2 (stmt);
5269 else
5271 code = TREE_CODE (base);
5272 if (get_gimple_rhs_class (code) != GIMPLE_BINARY_RHS)
5273 return false;
5274 e1 = TREE_OPERAND (base, 0);
5275 e2 = TREE_OPERAND (base, 1);
5278 /* Use affine expansion as deeper inspection to prove the equality. */
5279 tree_to_aff_combination_expand (e2, TREE_TYPE (e2),
5280 &aff_e2, &data->name_expansion_cache);
5281 tree_to_aff_combination_expand (offset, TREE_TYPE (offset),
5282 &aff_offset, &data->name_expansion_cache);
5283 aff_combination_scale (&aff_offset, -1);
5284 switch (code)
5286 case PLUS_EXPR:
5287 aff_combination_add (&aff_e2, &aff_offset);
5288 if (aff_combination_zero_p (&aff_e2))
5289 return true;
5291 tree_to_aff_combination_expand (e1, TREE_TYPE (e1),
5292 &aff_e1, &data->name_expansion_cache);
5293 aff_combination_add (&aff_e1, &aff_offset);
5294 return aff_combination_zero_p (&aff_e1);
5296 case POINTER_PLUS_EXPR:
5297 aff_combination_add (&aff_e2, &aff_offset);
5298 return aff_combination_zero_p (&aff_e2);
5300 default:
5301 return false;
5305 /* Tries to replace loop exit by one formulated in terms of a LT_EXPR
5306 comparison with CAND. NITER describes the number of iterations of
5307 the loops. If successful, the comparison in COMP_P is altered accordingly.
5309 We aim to handle the following situation:
5311 sometype *base, *p;
5312 int a, b, i;
5314 i = a;
5315 p = p_0 = base + a;
5319 bla (*p);
5320 p++;
5321 i++;
5323 while (i < b);
5325 Here, the number of iterations of the loop is (a + 1 > b) ? 0 : b - a - 1.
5326 We aim to optimize this to
5328 p = p_0 = base + a;
5331 bla (*p);
5332 p++;
5334 while (p < p_0 - a + b);
5336 This preserves the correctness, since the pointer arithmetics does not
5337 overflow. More precisely:
5339 1) if a + 1 <= b, then p_0 - a + b is the final value of p, hence there is no
5340 overflow in computing it or the values of p.
5341 2) if a + 1 > b, then we need to verify that the expression p_0 - a does not
5342 overflow. To prove this, we use the fact that p_0 = base + a. */
5344 static bool
5345 iv_elimination_compare_lt (struct ivopts_data *data,
5346 struct iv_cand *cand, enum tree_code *comp_p,
5347 struct tree_niter_desc *niter)
5349 tree cand_type, a, b, mbz, nit_type = TREE_TYPE (niter->niter), offset;
5350 struct aff_tree nit, tmpa, tmpb;
5351 enum tree_code comp;
5352 HOST_WIDE_INT step;
5354 /* We need to know that the candidate induction variable does not overflow.
5355 While more complex analysis may be used to prove this, for now just
5356 check that the variable appears in the original program and that it
5357 is computed in a type that guarantees no overflows. */
5358 cand_type = TREE_TYPE (cand->iv->base);
5359 if (cand->pos != IP_ORIGINAL || !nowrap_type_p (cand_type))
5360 return false;
5362 /* Make sure that the loop iterates till the loop bound is hit, as otherwise
5363 the calculation of the BOUND could overflow, making the comparison
5364 invalid. */
5365 if (!data->loop_single_exit_p)
5366 return false;
5368 /* We need to be able to decide whether candidate is increasing or decreasing
5369 in order to choose the right comparison operator. */
5370 if (!cst_and_fits_in_hwi (cand->iv->step))
5371 return false;
5372 step = int_cst_value (cand->iv->step);
5374 /* Check that the number of iterations matches the expected pattern:
5375 a + 1 > b ? 0 : b - a - 1. */
5376 mbz = niter->may_be_zero;
5377 if (TREE_CODE (mbz) == GT_EXPR)
5379 /* Handle a + 1 > b. */
5380 tree op0 = TREE_OPERAND (mbz, 0);
5381 if (TREE_CODE (op0) == PLUS_EXPR && integer_onep (TREE_OPERAND (op0, 1)))
5383 a = TREE_OPERAND (op0, 0);
5384 b = TREE_OPERAND (mbz, 1);
5386 else
5387 return false;
5389 else if (TREE_CODE (mbz) == LT_EXPR)
5391 tree op1 = TREE_OPERAND (mbz, 1);
5393 /* Handle b < a + 1. */
5394 if (TREE_CODE (op1) == PLUS_EXPR && integer_onep (TREE_OPERAND (op1, 1)))
5396 a = TREE_OPERAND (op1, 0);
5397 b = TREE_OPERAND (mbz, 0);
5399 else
5400 return false;
5402 else
5403 return false;
5405 /* Expected number of iterations is B - A - 1. Check that it matches
5406 the actual number, i.e., that B - A - NITER = 1. */
5407 tree_to_aff_combination (niter->niter, nit_type, &nit);
5408 tree_to_aff_combination (fold_convert (nit_type, a), nit_type, &tmpa);
5409 tree_to_aff_combination (fold_convert (nit_type, b), nit_type, &tmpb);
5410 aff_combination_scale (&nit, -1);
5411 aff_combination_scale (&tmpa, -1);
5412 aff_combination_add (&tmpb, &tmpa);
5413 aff_combination_add (&tmpb, &nit);
5414 if (tmpb.n != 0 || tmpb.offset != 1)
5415 return false;
5417 /* Finally, check that CAND->IV->BASE - CAND->IV->STEP * A does not
5418 overflow. */
5419 offset = fold_build2 (MULT_EXPR, TREE_TYPE (cand->iv->step),
5420 cand->iv->step,
5421 fold_convert (TREE_TYPE (cand->iv->step), a));
5422 if (!difference_cannot_overflow_p (data, cand->iv->base, offset))
5423 return false;
5425 /* Determine the new comparison operator. */
5426 comp = step < 0 ? GT_EXPR : LT_EXPR;
5427 if (*comp_p == NE_EXPR)
5428 *comp_p = comp;
5429 else if (*comp_p == EQ_EXPR)
5430 *comp_p = invert_tree_comparison (comp, false);
5431 else
5432 gcc_unreachable ();
5434 return true;
5437 /* Check whether it is possible to express the condition in USE by comparison
5438 of candidate CAND. If so, store the value compared with to BOUND, and the
5439 comparison operator to COMP. */
5441 static bool
5442 may_eliminate_iv (struct ivopts_data *data,
5443 struct iv_use *use, struct iv_cand *cand, tree *bound,
5444 enum tree_code *comp)
5446 basic_block ex_bb;
5447 edge exit;
5448 tree period;
5449 struct loop *loop = data->current_loop;
5450 aff_tree bnd;
5451 struct tree_niter_desc *desc = NULL;
5453 if (TREE_CODE (cand->iv->step) != INTEGER_CST)
5454 return false;
5456 /* For now works only for exits that dominate the loop latch.
5457 TODO: extend to other conditions inside loop body. */
5458 ex_bb = gimple_bb (use->stmt);
5459 if (use->stmt != last_stmt (ex_bb)
5460 || gimple_code (use->stmt) != GIMPLE_COND
5461 || !dominated_by_p (CDI_DOMINATORS, loop->latch, ex_bb))
5462 return false;
5464 exit = EDGE_SUCC (ex_bb, 0);
5465 if (flow_bb_inside_loop_p (loop, exit->dest))
5466 exit = EDGE_SUCC (ex_bb, 1);
5467 if (flow_bb_inside_loop_p (loop, exit->dest))
5468 return false;
5470 desc = niter_for_exit (data, exit);
5471 if (!desc)
5472 return false;
5474 /* Determine whether we can use the variable to test the exit condition.
5475 This is the case iff the period of the induction variable is greater
5476 than the number of iterations for which the exit condition is true. */
5477 period = iv_period (cand->iv);
5479 /* If the number of iterations is constant, compare against it directly. */
5480 if (TREE_CODE (desc->niter) == INTEGER_CST)
5482 /* See cand_value_at. */
5483 if (stmt_after_increment (loop, cand, use->stmt))
5485 if (!tree_int_cst_lt (desc->niter, period))
5486 return false;
5488 else
5490 if (tree_int_cst_lt (period, desc->niter))
5491 return false;
5495 /* If not, and if this is the only possible exit of the loop, see whether
5496 we can get a conservative estimate on the number of iterations of the
5497 entire loop and compare against that instead. */
5498 else
5500 widest_int period_value, max_niter;
5502 max_niter = desc->max;
5503 if (stmt_after_increment (loop, cand, use->stmt))
5504 max_niter += 1;
5505 period_value = wi::to_widest (period);
5506 if (wi::gtu_p (max_niter, period_value))
5508 /* See if we can take advantage of inferred loop bound
5509 information. */
5510 if (data->loop_single_exit_p)
5512 if (!max_loop_iterations (loop, &max_niter))
5513 return false;
5514 /* The loop bound is already adjusted by adding 1. */
5515 if (wi::gtu_p (max_niter, period_value))
5516 return false;
5518 else
5519 return false;
5523 cand_value_at (loop, cand, use->stmt, desc->niter, &bnd);
5525 *bound = fold_convert (TREE_TYPE (cand->iv->base),
5526 aff_combination_to_tree (&bnd));
5527 *comp = iv_elimination_compare (data, use);
5529 /* It is unlikely that computing the number of iterations using division
5530 would be more profitable than keeping the original induction variable. */
5531 if (expression_expensive_p (*bound))
5532 return false;
5534 /* Sometimes, it is possible to handle the situation that the number of
5535 iterations may be zero unless additional assumtions by using <
5536 instead of != in the exit condition.
5538 TODO: we could also calculate the value MAY_BE_ZERO ? 0 : NITER and
5539 base the exit condition on it. However, that is often too
5540 expensive. */
5541 if (!integer_zerop (desc->may_be_zero))
5542 return iv_elimination_compare_lt (data, cand, comp, desc);
5544 return true;
5547 /* Calculates the cost of BOUND, if it is a PARM_DECL. A PARM_DECL must
5548 be copied, if it is used in the loop body and DATA->body_includes_call. */
5550 static int
5551 parm_decl_cost (struct ivopts_data *data, tree bound)
5553 tree sbound = bound;
5554 STRIP_NOPS (sbound);
5556 if (TREE_CODE (sbound) == SSA_NAME
5557 && SSA_NAME_IS_DEFAULT_DEF (sbound)
5558 && TREE_CODE (SSA_NAME_VAR (sbound)) == PARM_DECL
5559 && data->body_includes_call)
5560 return COSTS_N_INSNS (1);
5562 return 0;
5565 /* Determines cost of computing the use in GROUP with CAND in a condition. */
5567 static bool
5568 determine_group_iv_cost_cond (struct ivopts_data *data,
5569 struct iv_group *group, struct iv_cand *cand)
5571 tree bound = NULL_TREE;
5572 struct iv *cmp_iv;
5573 bitmap depends_on_elim = NULL, depends_on_express = NULL, depends_on;
5574 comp_cost elim_cost, express_cost, cost, bound_cost;
5575 bool ok;
5576 iv_inv_expr_ent *elim_inv_expr = NULL, *express_inv_expr = NULL, *inv_expr;
5577 tree *control_var, *bound_cst;
5578 enum tree_code comp = ERROR_MARK;
5579 struct iv_use *use = group->vuses[0];
5581 gcc_assert (cand->iv);
5583 /* Try iv elimination. */
5584 if (may_eliminate_iv (data, use, cand, &bound, &comp))
5586 elim_cost = force_var_cost (data, bound, &depends_on_elim);
5587 if (elim_cost.cost == 0)
5588 elim_cost.cost = parm_decl_cost (data, bound);
5589 else if (TREE_CODE (bound) == INTEGER_CST)
5590 elim_cost.cost = 0;
5591 /* If we replace a loop condition 'i < n' with 'p < base + n',
5592 depends_on_elim will have 'base' and 'n' set, which implies
5593 that both 'base' and 'n' will be live during the loop. More likely,
5594 'base + n' will be loop invariant, resulting in only one live value
5595 during the loop. So in that case we clear depends_on_elim and set
5596 elim_inv_expr_id instead. */
5597 if (depends_on_elim && bitmap_count_bits (depends_on_elim) > 1)
5599 elim_inv_expr = record_inv_expr (data, bound);
5600 bitmap_clear (depends_on_elim);
5602 /* The bound is a loop invariant, so it will be only computed
5603 once. */
5604 elim_cost.cost = adjust_setup_cost (data, elim_cost.cost);
5606 else
5607 elim_cost = infinite_cost;
5609 /* Try expressing the original giv. If it is compared with an invariant,
5610 note that we cannot get rid of it. */
5611 ok = extract_cond_operands (data, use->stmt, &control_var, &bound_cst,
5612 NULL, &cmp_iv);
5613 gcc_assert (ok);
5615 /* When the condition is a comparison of the candidate IV against
5616 zero, prefer this IV.
5618 TODO: The constant that we're subtracting from the cost should
5619 be target-dependent. This information should be added to the
5620 target costs for each backend. */
5621 if (!elim_cost.infinite_cost_p () /* Do not try to decrease infinite! */
5622 && integer_zerop (*bound_cst)
5623 && (operand_equal_p (*control_var, cand->var_after, 0)
5624 || operand_equal_p (*control_var, cand->var_before, 0)))
5625 elim_cost -= 1;
5627 express_cost = get_computation_cost (data, use, cand, false,
5628 &depends_on_express, NULL,
5629 &express_inv_expr);
5630 fd_ivopts_data = data;
5631 walk_tree (&cmp_iv->base, find_depends, &depends_on_express, NULL);
5633 /* Count the cost of the original bound as well. */
5634 bound_cost = force_var_cost (data, *bound_cst, NULL);
5635 if (bound_cost.cost == 0)
5636 bound_cost.cost = parm_decl_cost (data, *bound_cst);
5637 else if (TREE_CODE (*bound_cst) == INTEGER_CST)
5638 bound_cost.cost = 0;
5639 express_cost += bound_cost;
5641 /* Choose the better approach, preferring the eliminated IV. */
5642 if (elim_cost <= express_cost)
5644 cost = elim_cost;
5645 depends_on = depends_on_elim;
5646 depends_on_elim = NULL;
5647 inv_expr = elim_inv_expr;
5649 else
5651 cost = express_cost;
5652 depends_on = depends_on_express;
5653 depends_on_express = NULL;
5654 bound = NULL_TREE;
5655 comp = ERROR_MARK;
5656 inv_expr = express_inv_expr;
5659 set_group_iv_cost (data, group, cand, cost,
5660 depends_on, bound, comp, inv_expr);
5662 if (depends_on_elim)
5663 BITMAP_FREE (depends_on_elim);
5664 if (depends_on_express)
5665 BITMAP_FREE (depends_on_express);
5667 return !cost.infinite_cost_p ();
5670 /* Determines cost of computing uses in GROUP with CAND. Returns false
5671 if USE cannot be represented with CAND. */
5673 static bool
5674 determine_group_iv_cost (struct ivopts_data *data,
5675 struct iv_group *group, struct iv_cand *cand)
5677 switch (group->type)
5679 case USE_NONLINEAR_EXPR:
5680 return determine_group_iv_cost_generic (data, group, cand);
5682 case USE_ADDRESS:
5683 return determine_group_iv_cost_address (data, group, cand);
5685 case USE_COMPARE:
5686 return determine_group_iv_cost_cond (data, group, cand);
5688 default:
5689 gcc_unreachable ();
5693 /* Return true if get_computation_cost indicates that autoincrement is
5694 a possibility for the pair of USE and CAND, false otherwise. */
5696 static bool
5697 autoinc_possible_for_pair (struct ivopts_data *data, struct iv_use *use,
5698 struct iv_cand *cand)
5700 bitmap depends_on;
5701 bool can_autoinc;
5702 comp_cost cost;
5704 if (use->type != USE_ADDRESS)
5705 return false;
5707 cost = get_computation_cost (data, use, cand, true, &depends_on,
5708 &can_autoinc, NULL);
5710 BITMAP_FREE (depends_on);
5712 return !cost.infinite_cost_p () && can_autoinc;
5715 /* Examine IP_ORIGINAL candidates to see if they are incremented next to a
5716 use that allows autoincrement, and set their AINC_USE if possible. */
5718 static void
5719 set_autoinc_for_original_candidates (struct ivopts_data *data)
5721 unsigned i, j;
5723 for (i = 0; i < data->vcands.length (); i++)
5725 struct iv_cand *cand = data->vcands[i];
5726 struct iv_use *closest_before = NULL;
5727 struct iv_use *closest_after = NULL;
5728 if (cand->pos != IP_ORIGINAL)
5729 continue;
5731 for (j = 0; j < data->vgroups.length (); j++)
5733 struct iv_group *group = data->vgroups[j];
5734 struct iv_use *use = group->vuses[0];
5735 unsigned uid = gimple_uid (use->stmt);
5737 if (gimple_bb (use->stmt) != gimple_bb (cand->incremented_at))
5738 continue;
5740 if (uid < gimple_uid (cand->incremented_at)
5741 && (closest_before == NULL
5742 || uid > gimple_uid (closest_before->stmt)))
5743 closest_before = use;
5745 if (uid > gimple_uid (cand->incremented_at)
5746 && (closest_after == NULL
5747 || uid < gimple_uid (closest_after->stmt)))
5748 closest_after = use;
5751 if (closest_before != NULL
5752 && autoinc_possible_for_pair (data, closest_before, cand))
5753 cand->ainc_use = closest_before;
5754 else if (closest_after != NULL
5755 && autoinc_possible_for_pair (data, closest_after, cand))
5756 cand->ainc_use = closest_after;
5760 /* Finds the candidates for the induction variables. */
5762 static void
5763 find_iv_candidates (struct ivopts_data *data)
5765 /* Add commonly used ivs. */
5766 add_standard_iv_candidates (data);
5768 /* Add old induction variables. */
5769 add_iv_candidate_for_bivs (data);
5771 /* Add induction variables derived from uses. */
5772 add_iv_candidate_for_groups (data);
5774 set_autoinc_for_original_candidates (data);
5776 /* Record the important candidates. */
5777 record_important_candidates (data);
5779 if (dump_file && (dump_flags & TDF_DETAILS))
5781 unsigned i;
5783 fprintf (dump_file, "\n<Important Candidates>:\t");
5784 for (i = 0; i < data->vcands.length (); i++)
5785 if (data->vcands[i]->important)
5786 fprintf (dump_file, " %d,", data->vcands[i]->id);
5787 fprintf (dump_file, "\n");
5789 fprintf (dump_file, "\n<Group, Cand> Related:\n");
5790 for (i = 0; i < data->vgroups.length (); i++)
5792 struct iv_group *group = data->vgroups[i];
5794 if (group->related_cands)
5796 fprintf (dump_file, " Group %d:\t", group->id);
5797 dump_bitmap (dump_file, group->related_cands);
5800 fprintf (dump_file, "\n");
5804 /* Determines costs of computing use of iv with an iv candidate. */
5806 static void
5807 determine_group_iv_costs (struct ivopts_data *data)
5809 unsigned i, j;
5810 struct iv_cand *cand;
5811 struct iv_group *group;
5812 bitmap to_clear = BITMAP_ALLOC (NULL);
5814 alloc_use_cost_map (data);
5816 for (i = 0; i < data->vgroups.length (); i++)
5818 group = data->vgroups[i];
5820 if (data->consider_all_candidates)
5822 for (j = 0; j < data->vcands.length (); j++)
5824 cand = data->vcands[j];
5825 determine_group_iv_cost (data, group, cand);
5828 else
5830 bitmap_iterator bi;
5832 EXECUTE_IF_SET_IN_BITMAP (group->related_cands, 0, j, bi)
5834 cand = data->vcands[j];
5835 if (!determine_group_iv_cost (data, group, cand))
5836 bitmap_set_bit (to_clear, j);
5839 /* Remove the candidates for that the cost is infinite from
5840 the list of related candidates. */
5841 bitmap_and_compl_into (group->related_cands, to_clear);
5842 bitmap_clear (to_clear);
5846 BITMAP_FREE (to_clear);
5848 if (dump_file && (dump_flags & TDF_DETAILS))
5850 fprintf (dump_file, "\n<Invariant Expressions>:\n");
5851 auto_vec <iv_inv_expr_ent *> list (data->inv_expr_tab->elements ());
5853 for (hash_table<iv_inv_expr_hasher>::iterator it
5854 = data->inv_expr_tab->begin (); it != data->inv_expr_tab->end ();
5855 ++it)
5856 list.safe_push (*it);
5858 list.qsort (sort_iv_inv_expr_ent);
5860 for (i = 0; i < list.length (); ++i)
5862 fprintf (dump_file, "inv_expr %d: \t", i);
5863 print_generic_expr (dump_file, list[i]->expr, TDF_SLIM);
5864 fprintf (dump_file, "\n");
5867 fprintf (dump_file, "\n<Group-candidate Costs>:\n");
5869 for (i = 0; i < data->vgroups.length (); i++)
5871 group = data->vgroups[i];
5873 fprintf (dump_file, "Group %d:\n", i);
5874 fprintf (dump_file, " cand\tcost\tcompl.\tinv.ex.\tdepends on\n");
5875 for (j = 0; j < group->n_map_members; j++)
5877 if (!group->cost_map[j].cand
5878 || group->cost_map[j].cost.infinite_cost_p ())
5879 continue;
5881 fprintf (dump_file, " %d\t%d\t%d\t",
5882 group->cost_map[j].cand->id,
5883 group->cost_map[j].cost.cost,
5884 group->cost_map[j].cost.complexity);
5885 if (group->cost_map[j].inv_expr != NULL)
5886 fprintf (dump_file, "%d\t",
5887 group->cost_map[j].inv_expr->id);
5888 else
5889 fprintf (dump_file, "\t");
5890 if (group->cost_map[j].depends_on)
5891 bitmap_print (dump_file,
5892 group->cost_map[j].depends_on, "","");
5893 fprintf (dump_file, "\n");
5896 fprintf (dump_file, "\n");
5898 fprintf (dump_file, "\n");
5902 /* Determines cost of the candidate CAND. */
5904 static void
5905 determine_iv_cost (struct ivopts_data *data, struct iv_cand *cand)
5907 comp_cost cost_base;
5908 unsigned cost, cost_step;
5909 tree base;
5911 if (!cand->iv)
5913 cand->cost = 0;
5914 return;
5917 /* There are two costs associated with the candidate -- its increment
5918 and its initialization. The second is almost negligible for any loop
5919 that rolls enough, so we take it just very little into account. */
5921 base = cand->iv->base;
5922 cost_base = force_var_cost (data, base, NULL);
5923 /* It will be exceptional that the iv register happens to be initialized with
5924 the proper value at no cost. In general, there will at least be a regcopy
5925 or a const set. */
5926 if (cost_base.cost == 0)
5927 cost_base.cost = COSTS_N_INSNS (1);
5928 cost_step = add_cost (data->speed, TYPE_MODE (TREE_TYPE (base)));
5930 cost = cost_step + adjust_setup_cost (data, cost_base.cost);
5932 /* Prefer the original ivs unless we may gain something by replacing it.
5933 The reason is to make debugging simpler; so this is not relevant for
5934 artificial ivs created by other optimization passes. */
5935 if (cand->pos != IP_ORIGINAL
5936 || !SSA_NAME_VAR (cand->var_before)
5937 || DECL_ARTIFICIAL (SSA_NAME_VAR (cand->var_before)))
5938 cost++;
5940 /* Prefer not to insert statements into latch unless there are some
5941 already (so that we do not create unnecessary jumps). */
5942 if (cand->pos == IP_END
5943 && empty_block_p (ip_end_pos (data->current_loop)))
5944 cost++;
5946 cand->cost = cost;
5947 cand->cost_step = cost_step;
5950 /* Determines costs of computation of the candidates. */
5952 static void
5953 determine_iv_costs (struct ivopts_data *data)
5955 unsigned i;
5957 if (dump_file && (dump_flags & TDF_DETAILS))
5959 fprintf (dump_file, "<Candidate Costs>:\n");
5960 fprintf (dump_file, " cand\tcost\n");
5963 for (i = 0; i < data->vcands.length (); i++)
5965 struct iv_cand *cand = data->vcands[i];
5967 determine_iv_cost (data, cand);
5969 if (dump_file && (dump_flags & TDF_DETAILS))
5970 fprintf (dump_file, " %d\t%d\n", i, cand->cost);
5973 if (dump_file && (dump_flags & TDF_DETAILS))
5974 fprintf (dump_file, "\n");
5977 /* Calculates cost for having SIZE induction variables. */
5979 static unsigned
5980 ivopts_global_cost_for_size (struct ivopts_data *data, unsigned size)
5982 /* We add size to the cost, so that we prefer eliminating ivs
5983 if possible. */
5984 return size + estimate_reg_pressure_cost (size, data->regs_used, data->speed,
5985 data->body_includes_call);
5988 /* For each size of the induction variable set determine the penalty. */
5990 static void
5991 determine_set_costs (struct ivopts_data *data)
5993 unsigned j, n;
5994 gphi *phi;
5995 gphi_iterator psi;
5996 tree op;
5997 struct loop *loop = data->current_loop;
5998 bitmap_iterator bi;
6000 if (dump_file && (dump_flags & TDF_DETAILS))
6002 fprintf (dump_file, "<Global Costs>:\n");
6003 fprintf (dump_file, " target_avail_regs %d\n", target_avail_regs);
6004 fprintf (dump_file, " target_clobbered_regs %d\n", target_clobbered_regs);
6005 fprintf (dump_file, " target_reg_cost %d\n", target_reg_cost[data->speed]);
6006 fprintf (dump_file, " target_spill_cost %d\n", target_spill_cost[data->speed]);
6009 n = 0;
6010 for (psi = gsi_start_phis (loop->header); !gsi_end_p (psi); gsi_next (&psi))
6012 phi = psi.phi ();
6013 op = PHI_RESULT (phi);
6015 if (virtual_operand_p (op))
6016 continue;
6018 if (get_iv (data, op))
6019 continue;
6021 n++;
6024 EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, j, bi)
6026 struct version_info *info = ver_info (data, j);
6028 if (info->inv_id && info->has_nonlin_use)
6029 n++;
6032 data->regs_used = n;
6033 if (dump_file && (dump_flags & TDF_DETAILS))
6034 fprintf (dump_file, " regs_used %d\n", n);
6036 if (dump_file && (dump_flags & TDF_DETAILS))
6038 fprintf (dump_file, " cost for size:\n");
6039 fprintf (dump_file, " ivs\tcost\n");
6040 for (j = 0; j <= 2 * target_avail_regs; j++)
6041 fprintf (dump_file, " %d\t%d\n", j,
6042 ivopts_global_cost_for_size (data, j));
6043 fprintf (dump_file, "\n");
6047 /* Returns true if A is a cheaper cost pair than B. */
6049 static bool
6050 cheaper_cost_pair (struct cost_pair *a, struct cost_pair *b)
6052 if (!a)
6053 return false;
6055 if (!b)
6056 return true;
6058 if (a->cost < b->cost)
6059 return true;
6061 if (b->cost < a->cost)
6062 return false;
6064 /* In case the costs are the same, prefer the cheaper candidate. */
6065 if (a->cand->cost < b->cand->cost)
6066 return true;
6068 return false;
6072 /* Returns candidate by that USE is expressed in IVS. */
6074 static struct cost_pair *
6075 iv_ca_cand_for_group (struct iv_ca *ivs, struct iv_group *group)
6077 return ivs->cand_for_group[group->id];
6080 /* Computes the cost field of IVS structure. */
6082 static void
6083 iv_ca_recount_cost (struct ivopts_data *data, struct iv_ca *ivs)
6085 comp_cost cost = ivs->cand_use_cost;
6087 cost += ivs->cand_cost;
6089 cost += ivopts_global_cost_for_size (data,
6090 ivs->n_regs
6091 + ivs->used_inv_exprs->elements ());
6093 ivs->cost = cost;
6096 /* Remove invariants in set INVS to set IVS. */
6098 static void
6099 iv_ca_set_remove_invariants (struct iv_ca *ivs, bitmap invs)
6101 bitmap_iterator bi;
6102 unsigned iid;
6104 if (!invs)
6105 return;
6107 EXECUTE_IF_SET_IN_BITMAP (invs, 0, iid, bi)
6109 ivs->n_invariant_uses[iid]--;
6110 if (ivs->n_invariant_uses[iid] == 0)
6111 ivs->n_regs--;
6115 /* Set USE not to be expressed by any candidate in IVS. */
6117 static void
6118 iv_ca_set_no_cp (struct ivopts_data *data, struct iv_ca *ivs,
6119 struct iv_group *group)
6121 unsigned gid = group->id, cid;
6122 struct cost_pair *cp;
6124 cp = ivs->cand_for_group[gid];
6125 if (!cp)
6126 return;
6127 cid = cp->cand->id;
6129 ivs->bad_groups++;
6130 ivs->cand_for_group[gid] = NULL;
6131 ivs->n_cand_uses[cid]--;
6133 if (ivs->n_cand_uses[cid] == 0)
6135 bitmap_clear_bit (ivs->cands, cid);
6136 /* Do not count the pseudocandidates. */
6137 if (cp->cand->iv)
6138 ivs->n_regs--;
6139 ivs->n_cands--;
6140 ivs->cand_cost -= cp->cand->cost;
6142 iv_ca_set_remove_invariants (ivs, cp->cand->depends_on);
6145 ivs->cand_use_cost -= cp->cost;
6147 iv_ca_set_remove_invariants (ivs, cp->depends_on);
6149 if (cp->inv_expr != NULL)
6151 unsigned *slot = ivs->used_inv_exprs->get (cp->inv_expr);
6152 --(*slot);
6153 if (*slot == 0)
6154 ivs->used_inv_exprs->remove (cp->inv_expr);
6156 iv_ca_recount_cost (data, ivs);
6159 /* Add invariants in set INVS to set IVS. */
6161 static void
6162 iv_ca_set_add_invariants (struct iv_ca *ivs, bitmap invs)
6164 bitmap_iterator bi;
6165 unsigned iid;
6167 if (!invs)
6168 return;
6170 EXECUTE_IF_SET_IN_BITMAP (invs, 0, iid, bi)
6172 ivs->n_invariant_uses[iid]++;
6173 if (ivs->n_invariant_uses[iid] == 1)
6174 ivs->n_regs++;
6178 /* Set cost pair for GROUP in set IVS to CP. */
6180 static void
6181 iv_ca_set_cp (struct ivopts_data *data, struct iv_ca *ivs,
6182 struct iv_group *group, struct cost_pair *cp)
6184 unsigned gid = group->id, cid;
6186 if (ivs->cand_for_group[gid] == cp)
6187 return;
6189 if (ivs->cand_for_group[gid])
6190 iv_ca_set_no_cp (data, ivs, group);
6192 if (cp)
6194 cid = cp->cand->id;
6196 ivs->bad_groups--;
6197 ivs->cand_for_group[gid] = cp;
6198 ivs->n_cand_uses[cid]++;
6199 if (ivs->n_cand_uses[cid] == 1)
6201 bitmap_set_bit (ivs->cands, cid);
6202 /* Do not count the pseudocandidates. */
6203 if (cp->cand->iv)
6204 ivs->n_regs++;
6205 ivs->n_cands++;
6206 ivs->cand_cost += cp->cand->cost;
6208 iv_ca_set_add_invariants (ivs, cp->cand->depends_on);
6211 ivs->cand_use_cost += cp->cost;
6212 iv_ca_set_add_invariants (ivs, cp->depends_on);
6214 if (cp->inv_expr != NULL)
6216 unsigned *slot = &ivs->used_inv_exprs->get_or_insert (cp->inv_expr);
6217 ++(*slot);
6219 iv_ca_recount_cost (data, ivs);
6223 /* Extend set IVS by expressing USE by some of the candidates in it
6224 if possible. Consider all important candidates if candidates in
6225 set IVS don't give any result. */
6227 static void
6228 iv_ca_add_group (struct ivopts_data *data, struct iv_ca *ivs,
6229 struct iv_group *group)
6231 struct cost_pair *best_cp = NULL, *cp;
6232 bitmap_iterator bi;
6233 unsigned i;
6234 struct iv_cand *cand;
6236 gcc_assert (ivs->upto >= group->id);
6237 ivs->upto++;
6238 ivs->bad_groups++;
6240 EXECUTE_IF_SET_IN_BITMAP (ivs->cands, 0, i, bi)
6242 cand = data->vcands[i];
6243 cp = get_group_iv_cost (data, group, cand);
6244 if (cheaper_cost_pair (cp, best_cp))
6245 best_cp = cp;
6248 if (best_cp == NULL)
6250 EXECUTE_IF_SET_IN_BITMAP (data->important_candidates, 0, i, bi)
6252 cand = data->vcands[i];
6253 cp = get_group_iv_cost (data, group, cand);
6254 if (cheaper_cost_pair (cp, best_cp))
6255 best_cp = cp;
6259 iv_ca_set_cp (data, ivs, group, best_cp);
6262 /* Get cost for assignment IVS. */
6264 static comp_cost
6265 iv_ca_cost (struct iv_ca *ivs)
6267 /* This was a conditional expression but it triggered a bug in
6268 Sun C 5.5. */
6269 if (ivs->bad_groups)
6270 return infinite_cost;
6271 else
6272 return ivs->cost;
6275 /* Returns true if all dependences of CP are among invariants in IVS. */
6277 static bool
6278 iv_ca_has_deps (struct iv_ca *ivs, struct cost_pair *cp)
6280 unsigned i;
6281 bitmap_iterator bi;
6283 if (!cp->depends_on)
6284 return true;
6286 EXECUTE_IF_SET_IN_BITMAP (cp->depends_on, 0, i, bi)
6288 if (ivs->n_invariant_uses[i] == 0)
6289 return false;
6292 return true;
6295 /* Creates change of expressing GROUP by NEW_CP instead of OLD_CP and chains
6296 it before NEXT. */
6298 static struct iv_ca_delta *
6299 iv_ca_delta_add (struct iv_group *group, struct cost_pair *old_cp,
6300 struct cost_pair *new_cp, struct iv_ca_delta *next)
6302 struct iv_ca_delta *change = XNEW (struct iv_ca_delta);
6304 change->group = group;
6305 change->old_cp = old_cp;
6306 change->new_cp = new_cp;
6307 change->next = next;
6309 return change;
6312 /* Joins two lists of changes L1 and L2. Destructive -- old lists
6313 are rewritten. */
6315 static struct iv_ca_delta *
6316 iv_ca_delta_join (struct iv_ca_delta *l1, struct iv_ca_delta *l2)
6318 struct iv_ca_delta *last;
6320 if (!l2)
6321 return l1;
6323 if (!l1)
6324 return l2;
6326 for (last = l1; last->next; last = last->next)
6327 continue;
6328 last->next = l2;
6330 return l1;
6333 /* Reverse the list of changes DELTA, forming the inverse to it. */
6335 static struct iv_ca_delta *
6336 iv_ca_delta_reverse (struct iv_ca_delta *delta)
6338 struct iv_ca_delta *act, *next, *prev = NULL;
6340 for (act = delta; act; act = next)
6342 next = act->next;
6343 act->next = prev;
6344 prev = act;
6346 std::swap (act->old_cp, act->new_cp);
6349 return prev;
6352 /* Commit changes in DELTA to IVS. If FORWARD is false, the changes are
6353 reverted instead. */
6355 static void
6356 iv_ca_delta_commit (struct ivopts_data *data, struct iv_ca *ivs,
6357 struct iv_ca_delta *delta, bool forward)
6359 struct cost_pair *from, *to;
6360 struct iv_ca_delta *act;
6362 if (!forward)
6363 delta = iv_ca_delta_reverse (delta);
6365 for (act = delta; act; act = act->next)
6367 from = act->old_cp;
6368 to = act->new_cp;
6369 gcc_assert (iv_ca_cand_for_group (ivs, act->group) == from);
6370 iv_ca_set_cp (data, ivs, act->group, to);
6373 if (!forward)
6374 iv_ca_delta_reverse (delta);
6377 /* Returns true if CAND is used in IVS. */
6379 static bool
6380 iv_ca_cand_used_p (struct iv_ca *ivs, struct iv_cand *cand)
6382 return ivs->n_cand_uses[cand->id] > 0;
6385 /* Returns number of induction variable candidates in the set IVS. */
6387 static unsigned
6388 iv_ca_n_cands (struct iv_ca *ivs)
6390 return ivs->n_cands;
6393 /* Free the list of changes DELTA. */
6395 static void
6396 iv_ca_delta_free (struct iv_ca_delta **delta)
6398 struct iv_ca_delta *act, *next;
6400 for (act = *delta; act; act = next)
6402 next = act->next;
6403 free (act);
6406 *delta = NULL;
6409 /* Allocates new iv candidates assignment. */
6411 static struct iv_ca *
6412 iv_ca_new (struct ivopts_data *data)
6414 struct iv_ca *nw = XNEW (struct iv_ca);
6416 nw->upto = 0;
6417 nw->bad_groups = 0;
6418 nw->cand_for_group = XCNEWVEC (struct cost_pair *,
6419 data->vgroups.length ());
6420 nw->n_cand_uses = XCNEWVEC (unsigned, data->vcands.length ());
6421 nw->cands = BITMAP_ALLOC (NULL);
6422 nw->n_cands = 0;
6423 nw->n_regs = 0;
6424 nw->cand_use_cost = no_cost;
6425 nw->cand_cost = 0;
6426 nw->n_invariant_uses = XCNEWVEC (unsigned, data->max_inv_id + 1);
6427 nw->used_inv_exprs = new hash_map <iv_inv_expr_ent *, unsigned> (13);
6428 nw->cost = no_cost;
6430 return nw;
6433 /* Free memory occupied by the set IVS. */
6435 static void
6436 iv_ca_free (struct iv_ca **ivs)
6438 free ((*ivs)->cand_for_group);
6439 free ((*ivs)->n_cand_uses);
6440 BITMAP_FREE ((*ivs)->cands);
6441 free ((*ivs)->n_invariant_uses);
6442 delete ((*ivs)->used_inv_exprs);
6443 free (*ivs);
6444 *ivs = NULL;
6447 /* Dumps IVS to FILE. */
6449 static void
6450 iv_ca_dump (struct ivopts_data *data, FILE *file, struct iv_ca *ivs)
6452 unsigned i;
6453 comp_cost cost = iv_ca_cost (ivs);
6455 fprintf (file, " cost: %d (complexity %d)\n", cost.cost,
6456 cost.complexity);
6457 fprintf (file, " cand_cost: %d\n cand_group_cost: %d (complexity %d)\n",
6458 ivs->cand_cost, ivs->cand_use_cost.cost,
6459 ivs->cand_use_cost.complexity);
6460 bitmap_print (file, ivs->cands, " candidates: ","\n");
6462 for (i = 0; i < ivs->upto; i++)
6464 struct iv_group *group = data->vgroups[i];
6465 struct cost_pair *cp = iv_ca_cand_for_group (ivs, group);
6466 if (cp)
6467 fprintf (file, " group:%d --> iv_cand:%d, cost=(%d,%d)\n",
6468 group->id, cp->cand->id, cp->cost.cost,
6469 cp->cost.complexity);
6470 else
6471 fprintf (file, " group:%d --> ??\n", group->id);
6474 const char *pref = "";
6475 fprintf (file, " invariant variables: ");
6476 for (i = 1; i <= data->max_inv_id; i++)
6477 if (ivs->n_invariant_uses[i])
6479 fprintf (file, "%s%d", pref, i);
6480 pref = ", ";
6483 pref = "";
6484 fprintf (file, "\n invariant expressions: ");
6485 for (hash_map<iv_inv_expr_ent *, unsigned>::iterator it
6486 = ivs->used_inv_exprs->begin (); it != ivs->used_inv_exprs->end (); ++it)
6488 fprintf (file, "%s%d", pref, (*it).first->id);
6489 pref = ", ";
6492 fprintf (file, "\n\n");
6495 /* Try changing candidate in IVS to CAND for each use. Return cost of the
6496 new set, and store differences in DELTA. Number of induction variables
6497 in the new set is stored to N_IVS. MIN_NCAND is a flag. When it is true
6498 the function will try to find a solution with mimimal iv candidates. */
6500 static comp_cost
6501 iv_ca_extend (struct ivopts_data *data, struct iv_ca *ivs,
6502 struct iv_cand *cand, struct iv_ca_delta **delta,
6503 unsigned *n_ivs, bool min_ncand)
6505 unsigned i;
6506 comp_cost cost;
6507 struct iv_group *group;
6508 struct cost_pair *old_cp, *new_cp;
6510 *delta = NULL;
6511 for (i = 0; i < ivs->upto; i++)
6513 group = data->vgroups[i];
6514 old_cp = iv_ca_cand_for_group (ivs, group);
6516 if (old_cp
6517 && old_cp->cand == cand)
6518 continue;
6520 new_cp = get_group_iv_cost (data, group, cand);
6521 if (!new_cp)
6522 continue;
6524 if (!min_ncand && !iv_ca_has_deps (ivs, new_cp))
6525 continue;
6527 if (!min_ncand && !cheaper_cost_pair (new_cp, old_cp))
6528 continue;
6530 *delta = iv_ca_delta_add (group, old_cp, new_cp, *delta);
6533 iv_ca_delta_commit (data, ivs, *delta, true);
6534 cost = iv_ca_cost (ivs);
6535 if (n_ivs)
6536 *n_ivs = iv_ca_n_cands (ivs);
6537 iv_ca_delta_commit (data, ivs, *delta, false);
6539 return cost;
6542 /* Try narrowing set IVS by removing CAND. Return the cost of
6543 the new set and store the differences in DELTA. START is
6544 the candidate with which we start narrowing. */
6546 static comp_cost
6547 iv_ca_narrow (struct ivopts_data *data, struct iv_ca *ivs,
6548 struct iv_cand *cand, struct iv_cand *start,
6549 struct iv_ca_delta **delta)
6551 unsigned i, ci;
6552 struct iv_group *group;
6553 struct cost_pair *old_cp, *new_cp, *cp;
6554 bitmap_iterator bi;
6555 struct iv_cand *cnd;
6556 comp_cost cost, best_cost, acost;
6558 *delta = NULL;
6559 for (i = 0; i < data->vgroups.length (); i++)
6561 group = data->vgroups[i];
6563 old_cp = iv_ca_cand_for_group (ivs, group);
6564 if (old_cp->cand != cand)
6565 continue;
6567 best_cost = iv_ca_cost (ivs);
6568 /* Start narrowing with START. */
6569 new_cp = get_group_iv_cost (data, group, start);
6571 if (data->consider_all_candidates)
6573 EXECUTE_IF_SET_IN_BITMAP (ivs->cands, 0, ci, bi)
6575 if (ci == cand->id || (start && ci == start->id))
6576 continue;
6578 cnd = data->vcands[ci];
6580 cp = get_group_iv_cost (data, group, cnd);
6581 if (!cp)
6582 continue;
6584 iv_ca_set_cp (data, ivs, group, cp);
6585 acost = iv_ca_cost (ivs);
6587 if (acost < best_cost)
6589 best_cost = acost;
6590 new_cp = cp;
6594 else
6596 EXECUTE_IF_AND_IN_BITMAP (group->related_cands, ivs->cands, 0, ci, bi)
6598 if (ci == cand->id || (start && ci == start->id))
6599 continue;
6601 cnd = data->vcands[ci];
6603 cp = get_group_iv_cost (data, group, cnd);
6604 if (!cp)
6605 continue;
6607 iv_ca_set_cp (data, ivs, group, cp);
6608 acost = iv_ca_cost (ivs);
6610 if (acost < best_cost)
6612 best_cost = acost;
6613 new_cp = cp;
6617 /* Restore to old cp for use. */
6618 iv_ca_set_cp (data, ivs, group, old_cp);
6620 if (!new_cp)
6622 iv_ca_delta_free (delta);
6623 return infinite_cost;
6626 *delta = iv_ca_delta_add (group, old_cp, new_cp, *delta);
6629 iv_ca_delta_commit (data, ivs, *delta, true);
6630 cost = iv_ca_cost (ivs);
6631 iv_ca_delta_commit (data, ivs, *delta, false);
6633 return cost;
6636 /* Try optimizing the set of candidates IVS by removing candidates different
6637 from to EXCEPT_CAND from it. Return cost of the new set, and store
6638 differences in DELTA. */
6640 static comp_cost
6641 iv_ca_prune (struct ivopts_data *data, struct iv_ca *ivs,
6642 struct iv_cand *except_cand, struct iv_ca_delta **delta)
6644 bitmap_iterator bi;
6645 struct iv_ca_delta *act_delta, *best_delta;
6646 unsigned i;
6647 comp_cost best_cost, acost;
6648 struct iv_cand *cand;
6650 best_delta = NULL;
6651 best_cost = iv_ca_cost (ivs);
6653 EXECUTE_IF_SET_IN_BITMAP (ivs->cands, 0, i, bi)
6655 cand = data->vcands[i];
6657 if (cand == except_cand)
6658 continue;
6660 acost = iv_ca_narrow (data, ivs, cand, except_cand, &act_delta);
6662 if (acost < best_cost)
6664 best_cost = acost;
6665 iv_ca_delta_free (&best_delta);
6666 best_delta = act_delta;
6668 else
6669 iv_ca_delta_free (&act_delta);
6672 if (!best_delta)
6674 *delta = NULL;
6675 return best_cost;
6678 /* Recurse to possibly remove other unnecessary ivs. */
6679 iv_ca_delta_commit (data, ivs, best_delta, true);
6680 best_cost = iv_ca_prune (data, ivs, except_cand, delta);
6681 iv_ca_delta_commit (data, ivs, best_delta, false);
6682 *delta = iv_ca_delta_join (best_delta, *delta);
6683 return best_cost;
6686 /* Check if CAND_IDX is a candidate other than OLD_CAND and has
6687 cheaper local cost for GROUP than BEST_CP. Return pointer to
6688 the corresponding cost_pair, otherwise just return BEST_CP. */
6690 static struct cost_pair*
6691 cheaper_cost_with_cand (struct ivopts_data *data, struct iv_group *group,
6692 unsigned int cand_idx, struct iv_cand *old_cand,
6693 struct cost_pair *best_cp)
6695 struct iv_cand *cand;
6696 struct cost_pair *cp;
6698 gcc_assert (old_cand != NULL && best_cp != NULL);
6699 if (cand_idx == old_cand->id)
6700 return best_cp;
6702 cand = data->vcands[cand_idx];
6703 cp = get_group_iv_cost (data, group, cand);
6704 if (cp != NULL && cheaper_cost_pair (cp, best_cp))
6705 return cp;
6707 return best_cp;
6710 /* Try breaking local optimal fixed-point for IVS by replacing candidates
6711 which are used by more than one iv uses. For each of those candidates,
6712 this function tries to represent iv uses under that candidate using
6713 other ones with lower local cost, then tries to prune the new set.
6714 If the new set has lower cost, It returns the new cost after recording
6715 candidate replacement in list DELTA. */
6717 static comp_cost
6718 iv_ca_replace (struct ivopts_data *data, struct iv_ca *ivs,
6719 struct iv_ca_delta **delta)
6721 bitmap_iterator bi, bj;
6722 unsigned int i, j, k;
6723 struct iv_cand *cand;
6724 comp_cost orig_cost, acost;
6725 struct iv_ca_delta *act_delta, *tmp_delta;
6726 struct cost_pair *old_cp, *best_cp = NULL;
6728 *delta = NULL;
6729 orig_cost = iv_ca_cost (ivs);
6731 EXECUTE_IF_SET_IN_BITMAP (ivs->cands, 0, i, bi)
6733 if (ivs->n_cand_uses[i] == 1
6734 || ivs->n_cand_uses[i] > ALWAYS_PRUNE_CAND_SET_BOUND)
6735 continue;
6737 cand = data->vcands[i];
6739 act_delta = NULL;
6740 /* Represent uses under current candidate using other ones with
6741 lower local cost. */
6742 for (j = 0; j < ivs->upto; j++)
6744 struct iv_group *group = data->vgroups[j];
6745 old_cp = iv_ca_cand_for_group (ivs, group);
6747 if (old_cp->cand != cand)
6748 continue;
6750 best_cp = old_cp;
6751 if (data->consider_all_candidates)
6752 for (k = 0; k < data->vcands.length (); k++)
6753 best_cp = cheaper_cost_with_cand (data, group, k,
6754 old_cp->cand, best_cp);
6755 else
6756 EXECUTE_IF_SET_IN_BITMAP (group->related_cands, 0, k, bj)
6757 best_cp = cheaper_cost_with_cand (data, group, k,
6758 old_cp->cand, best_cp);
6760 if (best_cp == old_cp)
6761 continue;
6763 act_delta = iv_ca_delta_add (group, old_cp, best_cp, act_delta);
6765 /* No need for further prune. */
6766 if (!act_delta)
6767 continue;
6769 /* Prune the new candidate set. */
6770 iv_ca_delta_commit (data, ivs, act_delta, true);
6771 acost = iv_ca_prune (data, ivs, NULL, &tmp_delta);
6772 iv_ca_delta_commit (data, ivs, act_delta, false);
6773 act_delta = iv_ca_delta_join (act_delta, tmp_delta);
6775 if (acost < orig_cost)
6777 *delta = act_delta;
6778 return acost;
6780 else
6781 iv_ca_delta_free (&act_delta);
6784 return orig_cost;
6787 /* Tries to extend the sets IVS in the best possible way in order to
6788 express the GROUP. If ORIGINALP is true, prefer candidates from
6789 the original set of IVs, otherwise favor important candidates not
6790 based on any memory object. */
6792 static bool
6793 try_add_cand_for (struct ivopts_data *data, struct iv_ca *ivs,
6794 struct iv_group *group, bool originalp)
6796 comp_cost best_cost, act_cost;
6797 unsigned i;
6798 bitmap_iterator bi;
6799 struct iv_cand *cand;
6800 struct iv_ca_delta *best_delta = NULL, *act_delta;
6801 struct cost_pair *cp;
6803 iv_ca_add_group (data, ivs, group);
6804 best_cost = iv_ca_cost (ivs);
6805 cp = iv_ca_cand_for_group (ivs, group);
6806 if (cp)
6808 best_delta = iv_ca_delta_add (group, NULL, cp, NULL);
6809 iv_ca_set_no_cp (data, ivs, group);
6812 /* If ORIGINALP is true, try to find the original IV for the use. Otherwise
6813 first try important candidates not based on any memory object. Only if
6814 this fails, try the specific ones. Rationale -- in loops with many
6815 variables the best choice often is to use just one generic biv. If we
6816 added here many ivs specific to the uses, the optimization algorithm later
6817 would be likely to get stuck in a local minimum, thus causing us to create
6818 too many ivs. The approach from few ivs to more seems more likely to be
6819 successful -- starting from few ivs, replacing an expensive use by a
6820 specific iv should always be a win. */
6821 EXECUTE_IF_SET_IN_BITMAP (group->related_cands, 0, i, bi)
6823 cand = data->vcands[i];
6825 if (originalp && cand->pos !=IP_ORIGINAL)
6826 continue;
6828 if (!originalp && cand->iv->base_object != NULL_TREE)
6829 continue;
6831 if (iv_ca_cand_used_p (ivs, cand))
6832 continue;
6834 cp = get_group_iv_cost (data, group, cand);
6835 if (!cp)
6836 continue;
6838 iv_ca_set_cp (data, ivs, group, cp);
6839 act_cost = iv_ca_extend (data, ivs, cand, &act_delta, NULL,
6840 true);
6841 iv_ca_set_no_cp (data, ivs, group);
6842 act_delta = iv_ca_delta_add (group, NULL, cp, act_delta);
6844 if (act_cost < best_cost)
6846 best_cost = act_cost;
6848 iv_ca_delta_free (&best_delta);
6849 best_delta = act_delta;
6851 else
6852 iv_ca_delta_free (&act_delta);
6855 if (best_cost.infinite_cost_p ())
6857 for (i = 0; i < group->n_map_members; i++)
6859 cp = group->cost_map + i;
6860 cand = cp->cand;
6861 if (!cand)
6862 continue;
6864 /* Already tried this. */
6865 if (cand->important)
6867 if (originalp && cand->pos == IP_ORIGINAL)
6868 continue;
6869 if (!originalp && cand->iv->base_object == NULL_TREE)
6870 continue;
6873 if (iv_ca_cand_used_p (ivs, cand))
6874 continue;
6876 act_delta = NULL;
6877 iv_ca_set_cp (data, ivs, group, cp);
6878 act_cost = iv_ca_extend (data, ivs, cand, &act_delta, NULL, true);
6879 iv_ca_set_no_cp (data, ivs, group);
6880 act_delta = iv_ca_delta_add (group,
6881 iv_ca_cand_for_group (ivs, group),
6882 cp, act_delta);
6884 if (act_cost < best_cost)
6886 best_cost = act_cost;
6888 if (best_delta)
6889 iv_ca_delta_free (&best_delta);
6890 best_delta = act_delta;
6892 else
6893 iv_ca_delta_free (&act_delta);
6897 iv_ca_delta_commit (data, ivs, best_delta, true);
6898 iv_ca_delta_free (&best_delta);
6900 return !best_cost.infinite_cost_p ();
6903 /* Finds an initial assignment of candidates to uses. */
6905 static struct iv_ca *
6906 get_initial_solution (struct ivopts_data *data, bool originalp)
6908 unsigned i;
6909 struct iv_ca *ivs = iv_ca_new (data);
6911 for (i = 0; i < data->vgroups.length (); i++)
6912 if (!try_add_cand_for (data, ivs, data->vgroups[i], originalp))
6914 iv_ca_free (&ivs);
6915 return NULL;
6918 return ivs;
6921 /* Tries to improve set of induction variables IVS. TRY_REPLACE_P
6922 points to a bool variable, this function tries to break local
6923 optimal fixed-point by replacing candidates in IVS if it's true. */
6925 static bool
6926 try_improve_iv_set (struct ivopts_data *data,
6927 struct iv_ca *ivs, bool *try_replace_p)
6929 unsigned i, n_ivs;
6930 comp_cost acost, best_cost = iv_ca_cost (ivs);
6931 struct iv_ca_delta *best_delta = NULL, *act_delta, *tmp_delta;
6932 struct iv_cand *cand;
6934 /* Try extending the set of induction variables by one. */
6935 for (i = 0; i < data->vcands.length (); i++)
6937 cand = data->vcands[i];
6939 if (iv_ca_cand_used_p (ivs, cand))
6940 continue;
6942 acost = iv_ca_extend (data, ivs, cand, &act_delta, &n_ivs, false);
6943 if (!act_delta)
6944 continue;
6946 /* If we successfully added the candidate and the set is small enough,
6947 try optimizing it by removing other candidates. */
6948 if (n_ivs <= ALWAYS_PRUNE_CAND_SET_BOUND)
6950 iv_ca_delta_commit (data, ivs, act_delta, true);
6951 acost = iv_ca_prune (data, ivs, cand, &tmp_delta);
6952 iv_ca_delta_commit (data, ivs, act_delta, false);
6953 act_delta = iv_ca_delta_join (act_delta, tmp_delta);
6956 if (acost < best_cost)
6958 best_cost = acost;
6959 iv_ca_delta_free (&best_delta);
6960 best_delta = act_delta;
6962 else
6963 iv_ca_delta_free (&act_delta);
6966 if (!best_delta)
6968 /* Try removing the candidates from the set instead. */
6969 best_cost = iv_ca_prune (data, ivs, NULL, &best_delta);
6971 if (!best_delta && *try_replace_p)
6973 *try_replace_p = false;
6974 /* So far candidate selecting algorithm tends to choose fewer IVs
6975 so that it can handle cases in which loops have many variables
6976 but the best choice is often to use only one general biv. One
6977 weakness is it can't handle opposite cases, in which different
6978 candidates should be chosen with respect to each use. To solve
6979 the problem, we replace candidates in a manner described by the
6980 comments of iv_ca_replace, thus give general algorithm a chance
6981 to break local optimal fixed-point in these cases. */
6982 best_cost = iv_ca_replace (data, ivs, &best_delta);
6985 if (!best_delta)
6986 return false;
6989 iv_ca_delta_commit (data, ivs, best_delta, true);
6990 gcc_assert (best_cost == iv_ca_cost (ivs));
6991 iv_ca_delta_free (&best_delta);
6992 return true;
6995 /* Attempts to find the optimal set of induction variables. We do simple
6996 greedy heuristic -- we try to replace at most one candidate in the selected
6997 solution and remove the unused ivs while this improves the cost. */
6999 static struct iv_ca *
7000 find_optimal_iv_set_1 (struct ivopts_data *data, bool originalp)
7002 struct iv_ca *set;
7003 bool try_replace_p = true;
7005 /* Get the initial solution. */
7006 set = get_initial_solution (data, originalp);
7007 if (!set)
7009 if (dump_file && (dump_flags & TDF_DETAILS))
7010 fprintf (dump_file, "Unable to substitute for ivs, failed.\n");
7011 return NULL;
7014 if (dump_file && (dump_flags & TDF_DETAILS))
7016 fprintf (dump_file, "Initial set of candidates:\n");
7017 iv_ca_dump (data, dump_file, set);
7020 while (try_improve_iv_set (data, set, &try_replace_p))
7022 if (dump_file && (dump_flags & TDF_DETAILS))
7024 fprintf (dump_file, "Improved to:\n");
7025 iv_ca_dump (data, dump_file, set);
7029 return set;
7032 static struct iv_ca *
7033 find_optimal_iv_set (struct ivopts_data *data)
7035 unsigned i;
7036 comp_cost cost, origcost;
7037 struct iv_ca *set, *origset;
7039 /* Determine the cost based on a strategy that starts with original IVs,
7040 and try again using a strategy that prefers candidates not based
7041 on any IVs. */
7042 origset = find_optimal_iv_set_1 (data, true);
7043 set = find_optimal_iv_set_1 (data, false);
7045 if (!origset && !set)
7046 return NULL;
7048 origcost = origset ? iv_ca_cost (origset) : infinite_cost;
7049 cost = set ? iv_ca_cost (set) : infinite_cost;
7051 if (dump_file && (dump_flags & TDF_DETAILS))
7053 fprintf (dump_file, "Original cost %d (complexity %d)\n\n",
7054 origcost.cost, origcost.complexity);
7055 fprintf (dump_file, "Final cost %d (complexity %d)\n\n",
7056 cost.cost, cost.complexity);
7059 /* Choose the one with the best cost. */
7060 if (origcost <= cost)
7062 if (set)
7063 iv_ca_free (&set);
7064 set = origset;
7066 else if (origset)
7067 iv_ca_free (&origset);
7069 for (i = 0; i < data->vgroups.length (); i++)
7071 struct iv_group *group = data->vgroups[i];
7072 group->selected = iv_ca_cand_for_group (set, group)->cand;
7075 return set;
7078 /* Creates a new induction variable corresponding to CAND. */
7080 static void
7081 create_new_iv (struct ivopts_data *data, struct iv_cand *cand)
7083 gimple_stmt_iterator incr_pos;
7084 tree base;
7085 struct iv_use *use;
7086 struct iv_group *group;
7087 bool after = false;
7089 if (!cand->iv)
7090 return;
7092 switch (cand->pos)
7094 case IP_NORMAL:
7095 incr_pos = gsi_last_bb (ip_normal_pos (data->current_loop));
7096 break;
7098 case IP_END:
7099 incr_pos = gsi_last_bb (ip_end_pos (data->current_loop));
7100 after = true;
7101 break;
7103 case IP_AFTER_USE:
7104 after = true;
7105 /* fall through */
7106 case IP_BEFORE_USE:
7107 incr_pos = gsi_for_stmt (cand->incremented_at);
7108 break;
7110 case IP_ORIGINAL:
7111 /* Mark that the iv is preserved. */
7112 name_info (data, cand->var_before)->preserve_biv = true;
7113 name_info (data, cand->var_after)->preserve_biv = true;
7115 /* Rewrite the increment so that it uses var_before directly. */
7116 use = find_interesting_uses_op (data, cand->var_after);
7117 group = data->vgroups[use->group_id];
7118 group->selected = cand;
7119 return;
7122 gimple_add_tmp_var (cand->var_before);
7124 base = unshare_expr (cand->iv->base);
7126 create_iv (base, unshare_expr (cand->iv->step),
7127 cand->var_before, data->current_loop,
7128 &incr_pos, after, &cand->var_before, &cand->var_after);
7131 /* Creates new induction variables described in SET. */
7133 static void
7134 create_new_ivs (struct ivopts_data *data, struct iv_ca *set)
7136 unsigned i;
7137 struct iv_cand *cand;
7138 bitmap_iterator bi;
7140 EXECUTE_IF_SET_IN_BITMAP (set->cands, 0, i, bi)
7142 cand = data->vcands[i];
7143 create_new_iv (data, cand);
7146 if (dump_file && (dump_flags & TDF_DETAILS))
7148 fprintf (dump_file, "Selected IV set for loop %d",
7149 data->current_loop->num);
7150 if (data->loop_loc != UNKNOWN_LOCATION)
7151 fprintf (dump_file, " at %s:%d", LOCATION_FILE (data->loop_loc),
7152 LOCATION_LINE (data->loop_loc));
7153 fprintf (dump_file, ", " HOST_WIDE_INT_PRINT_DEC " avg niters",
7154 avg_loop_niter (data->current_loop));
7155 fprintf (dump_file, ", " HOST_WIDE_INT_PRINT_UNSIGNED " expressions",
7156 (unsigned HOST_WIDE_INT) set->used_inv_exprs->elements ());
7157 fprintf (dump_file, ", %lu IVs:\n", bitmap_count_bits (set->cands));
7158 EXECUTE_IF_SET_IN_BITMAP (set->cands, 0, i, bi)
7160 cand = data->vcands[i];
7161 dump_cand (dump_file, cand);
7163 fprintf (dump_file, "\n");
7167 /* Rewrites USE (definition of iv used in a nonlinear expression)
7168 using candidate CAND. */
7170 static void
7171 rewrite_use_nonlinear_expr (struct ivopts_data *data,
7172 struct iv_use *use, struct iv_cand *cand)
7174 tree comp;
7175 tree op, tgt;
7176 gassign *ass;
7177 gimple_stmt_iterator bsi;
7179 /* An important special case -- if we are asked to express value of
7180 the original iv by itself, just exit; there is no need to
7181 introduce a new computation (that might also need casting the
7182 variable to unsigned and back). */
7183 if (cand->pos == IP_ORIGINAL
7184 && cand->incremented_at == use->stmt)
7186 enum tree_code stmt_code;
7188 gcc_assert (is_gimple_assign (use->stmt));
7189 gcc_assert (gimple_assign_lhs (use->stmt) == cand->var_after);
7191 /* Check whether we may leave the computation unchanged.
7192 This is the case only if it does not rely on other
7193 computations in the loop -- otherwise, the computation
7194 we rely upon may be removed in remove_unused_ivs,
7195 thus leading to ICE. */
7196 stmt_code = gimple_assign_rhs_code (use->stmt);
7197 if (stmt_code == PLUS_EXPR
7198 || stmt_code == MINUS_EXPR
7199 || stmt_code == POINTER_PLUS_EXPR)
7201 if (gimple_assign_rhs1 (use->stmt) == cand->var_before)
7202 op = gimple_assign_rhs2 (use->stmt);
7203 else if (gimple_assign_rhs2 (use->stmt) == cand->var_before)
7204 op = gimple_assign_rhs1 (use->stmt);
7205 else
7206 op = NULL_TREE;
7208 else
7209 op = NULL_TREE;
7211 if (op && expr_invariant_in_loop_p (data->current_loop, op))
7212 return;
7215 comp = get_computation (data->current_loop, use, cand);
7216 gcc_assert (comp != NULL_TREE);
7218 switch (gimple_code (use->stmt))
7220 case GIMPLE_PHI:
7221 tgt = PHI_RESULT (use->stmt);
7223 /* If we should keep the biv, do not replace it. */
7224 if (name_info (data, tgt)->preserve_biv)
7225 return;
7227 bsi = gsi_after_labels (gimple_bb (use->stmt));
7228 break;
7230 case GIMPLE_ASSIGN:
7231 tgt = gimple_assign_lhs (use->stmt);
7232 bsi = gsi_for_stmt (use->stmt);
7233 break;
7235 default:
7236 gcc_unreachable ();
7239 if (!valid_gimple_rhs_p (comp)
7240 || (gimple_code (use->stmt) != GIMPLE_PHI
7241 /* We can't allow re-allocating the stmt as it might be pointed
7242 to still. */
7243 && (get_gimple_rhs_num_ops (TREE_CODE (comp))
7244 >= gimple_num_ops (gsi_stmt (bsi)))))
7246 comp = force_gimple_operand_gsi (&bsi, comp, true, NULL_TREE,
7247 true, GSI_SAME_STMT);
7248 if (POINTER_TYPE_P (TREE_TYPE (tgt)))
7250 duplicate_ssa_name_ptr_info (comp, SSA_NAME_PTR_INFO (tgt));
7251 /* As this isn't a plain copy we have to reset alignment
7252 information. */
7253 if (SSA_NAME_PTR_INFO (comp))
7254 mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (comp));
7258 if (gimple_code (use->stmt) == GIMPLE_PHI)
7260 ass = gimple_build_assign (tgt, comp);
7261 gsi_insert_before (&bsi, ass, GSI_SAME_STMT);
7263 bsi = gsi_for_stmt (use->stmt);
7264 remove_phi_node (&bsi, false);
7266 else
7268 gimple_assign_set_rhs_from_tree (&bsi, comp);
7269 use->stmt = gsi_stmt (bsi);
7273 /* Performs a peephole optimization to reorder the iv update statement with
7274 a mem ref to enable instruction combining in later phases. The mem ref uses
7275 the iv value before the update, so the reordering transformation requires
7276 adjustment of the offset. CAND is the selected IV_CAND.
7278 Example:
7280 t = MEM_REF (base, iv1, 8, 16); // base, index, stride, offset
7281 iv2 = iv1 + 1;
7283 if (t < val) (1)
7284 goto L;
7285 goto Head;
7288 directly propagating t over to (1) will introduce overlapping live range
7289 thus increase register pressure. This peephole transform it into:
7292 iv2 = iv1 + 1;
7293 t = MEM_REF (base, iv2, 8, 8);
7294 if (t < val)
7295 goto L;
7296 goto Head;
7299 static void
7300 adjust_iv_update_pos (struct iv_cand *cand, struct iv_use *use)
7302 tree var_after;
7303 gimple *iv_update, *stmt;
7304 basic_block bb;
7305 gimple_stmt_iterator gsi, gsi_iv;
7307 if (cand->pos != IP_NORMAL)
7308 return;
7310 var_after = cand->var_after;
7311 iv_update = SSA_NAME_DEF_STMT (var_after);
7313 bb = gimple_bb (iv_update);
7314 gsi = gsi_last_nondebug_bb (bb);
7315 stmt = gsi_stmt (gsi);
7317 /* Only handle conditional statement for now. */
7318 if (gimple_code (stmt) != GIMPLE_COND)
7319 return;
7321 gsi_prev_nondebug (&gsi);
7322 stmt = gsi_stmt (gsi);
7323 if (stmt != iv_update)
7324 return;
7326 gsi_prev_nondebug (&gsi);
7327 if (gsi_end_p (gsi))
7328 return;
7330 stmt = gsi_stmt (gsi);
7331 if (gimple_code (stmt) != GIMPLE_ASSIGN)
7332 return;
7334 if (stmt != use->stmt)
7335 return;
7337 if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
7338 return;
7340 if (dump_file && (dump_flags & TDF_DETAILS))
7342 fprintf (dump_file, "Reordering \n");
7343 print_gimple_stmt (dump_file, iv_update, 0, 0);
7344 print_gimple_stmt (dump_file, use->stmt, 0, 0);
7345 fprintf (dump_file, "\n");
7348 gsi = gsi_for_stmt (use->stmt);
7349 gsi_iv = gsi_for_stmt (iv_update);
7350 gsi_move_before (&gsi_iv, &gsi);
7352 cand->pos = IP_BEFORE_USE;
7353 cand->incremented_at = use->stmt;
7356 /* Rewrites USE (address that is an iv) using candidate CAND. */
7358 static void
7359 rewrite_use_address (struct ivopts_data *data,
7360 struct iv_use *use, struct iv_cand *cand)
7362 aff_tree aff;
7363 gimple_stmt_iterator bsi = gsi_for_stmt (use->stmt);
7364 tree base_hint = NULL_TREE;
7365 tree ref, iv;
7366 bool ok;
7368 adjust_iv_update_pos (cand, use);
7369 ok = get_computation_aff (data->current_loop, use, cand, use->stmt, &aff);
7370 gcc_assert (ok);
7371 unshare_aff_combination (&aff);
7373 /* To avoid undefined overflow problems, all IV candidates use unsigned
7374 integer types. The drawback is that this makes it impossible for
7375 create_mem_ref to distinguish an IV that is based on a memory object
7376 from one that represents simply an offset.
7378 To work around this problem, we pass a hint to create_mem_ref that
7379 indicates which variable (if any) in aff is an IV based on a memory
7380 object. Note that we only consider the candidate. If this is not
7381 based on an object, the base of the reference is in some subexpression
7382 of the use -- but these will use pointer types, so they are recognized
7383 by the create_mem_ref heuristics anyway. */
7384 if (cand->iv->base_object)
7385 base_hint = var_at_stmt (data->current_loop, cand, use->stmt);
7387 iv = var_at_stmt (data->current_loop, cand, use->stmt);
7388 ref = create_mem_ref (&bsi, TREE_TYPE (*use->op_p), &aff,
7389 reference_alias_ptr_type (*use->op_p),
7390 iv, base_hint, data->speed);
7391 copy_ref_info (ref, *use->op_p);
7392 *use->op_p = ref;
7395 /* Rewrites USE (the condition such that one of the arguments is an iv) using
7396 candidate CAND. */
7398 static void
7399 rewrite_use_compare (struct ivopts_data *data,
7400 struct iv_use *use, struct iv_cand *cand)
7402 tree comp, *var_p, op, bound;
7403 gimple_stmt_iterator bsi = gsi_for_stmt (use->stmt);
7404 enum tree_code compare;
7405 struct iv_group *group = data->vgroups[use->group_id];
7406 struct cost_pair *cp = get_group_iv_cost (data, group, cand);
7407 bool ok;
7409 bound = cp->value;
7410 if (bound)
7412 tree var = var_at_stmt (data->current_loop, cand, use->stmt);
7413 tree var_type = TREE_TYPE (var);
7414 gimple_seq stmts;
7416 if (dump_file && (dump_flags & TDF_DETAILS))
7418 fprintf (dump_file, "Replacing exit test: ");
7419 print_gimple_stmt (dump_file, use->stmt, 0, TDF_SLIM);
7421 compare = cp->comp;
7422 bound = unshare_expr (fold_convert (var_type, bound));
7423 op = force_gimple_operand (bound, &stmts, true, NULL_TREE);
7424 if (stmts)
7425 gsi_insert_seq_on_edge_immediate (
7426 loop_preheader_edge (data->current_loop),
7427 stmts);
7429 gcond *cond_stmt = as_a <gcond *> (use->stmt);
7430 gimple_cond_set_lhs (cond_stmt, var);
7431 gimple_cond_set_code (cond_stmt, compare);
7432 gimple_cond_set_rhs (cond_stmt, op);
7433 return;
7436 /* The induction variable elimination failed; just express the original
7437 giv. */
7438 comp = get_computation (data->current_loop, use, cand);
7439 gcc_assert (comp != NULL_TREE);
7441 ok = extract_cond_operands (data, use->stmt, &var_p, NULL, NULL, NULL);
7442 gcc_assert (ok);
7444 *var_p = force_gimple_operand_gsi (&bsi, comp, true, SSA_NAME_VAR (*var_p),
7445 true, GSI_SAME_STMT);
7448 /* Rewrite the groups using the selected induction variables. */
7450 static void
7451 rewrite_groups (struct ivopts_data *data)
7453 unsigned i, j;
7455 for (i = 0; i < data->vgroups.length (); i++)
7457 struct iv_group *group = data->vgroups[i];
7458 struct iv_cand *cand = group->selected;
7460 gcc_assert (cand);
7462 if (group->type == USE_NONLINEAR_EXPR)
7464 for (j = 0; j < group->vuses.length (); j++)
7466 rewrite_use_nonlinear_expr (data, group->vuses[j], cand);
7467 update_stmt (group->vuses[j]->stmt);
7470 else if (group->type == USE_ADDRESS)
7472 for (j = 0; j < group->vuses.length (); j++)
7474 rewrite_use_address (data, group->vuses[j], cand);
7475 update_stmt (group->vuses[j]->stmt);
7478 else
7480 gcc_assert (group->type == USE_COMPARE);
7482 for (j = 0; j < group->vuses.length (); j++)
7484 rewrite_use_compare (data, group->vuses[j], cand);
7485 update_stmt (group->vuses[j]->stmt);
7491 /* Removes the ivs that are not used after rewriting. */
7493 static void
7494 remove_unused_ivs (struct ivopts_data *data)
7496 unsigned j;
7497 bitmap_iterator bi;
7498 bitmap toremove = BITMAP_ALLOC (NULL);
7500 /* Figure out an order in which to release SSA DEFs so that we don't
7501 release something that we'd have to propagate into a debug stmt
7502 afterwards. */
7503 EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, j, bi)
7505 struct version_info *info;
7507 info = ver_info (data, j);
7508 if (info->iv
7509 && !integer_zerop (info->iv->step)
7510 && !info->inv_id
7511 && !info->iv->nonlin_use
7512 && !info->preserve_biv)
7514 bitmap_set_bit (toremove, SSA_NAME_VERSION (info->iv->ssa_name));
7516 tree def = info->iv->ssa_name;
7518 if (MAY_HAVE_DEBUG_STMTS && SSA_NAME_DEF_STMT (def))
7520 imm_use_iterator imm_iter;
7521 use_operand_p use_p;
7522 gimple *stmt;
7523 int count = 0;
7525 FOR_EACH_IMM_USE_STMT (stmt, imm_iter, def)
7527 if (!gimple_debug_bind_p (stmt))
7528 continue;
7530 /* We just want to determine whether to do nothing
7531 (count == 0), to substitute the computed
7532 expression into a single use of the SSA DEF by
7533 itself (count == 1), or to use a debug temp
7534 because the SSA DEF is used multiple times or as
7535 part of a larger expression (count > 1). */
7536 count++;
7537 if (gimple_debug_bind_get_value (stmt) != def)
7538 count++;
7540 if (count > 1)
7541 BREAK_FROM_IMM_USE_STMT (imm_iter);
7544 if (!count)
7545 continue;
7547 struct iv_use dummy_use;
7548 struct iv_cand *best_cand = NULL, *cand;
7549 unsigned i, best_pref = 0, cand_pref;
7551 memset (&dummy_use, 0, sizeof (dummy_use));
7552 dummy_use.iv = info->iv;
7553 for (i = 0; i < data->vgroups.length () && i < 64; i++)
7555 cand = data->vgroups[i]->selected;
7556 if (cand == best_cand)
7557 continue;
7558 cand_pref = operand_equal_p (cand->iv->step,
7559 info->iv->step, 0)
7560 ? 4 : 0;
7561 cand_pref
7562 += TYPE_MODE (TREE_TYPE (cand->iv->base))
7563 == TYPE_MODE (TREE_TYPE (info->iv->base))
7564 ? 2 : 0;
7565 cand_pref
7566 += TREE_CODE (cand->iv->base) == INTEGER_CST
7567 ? 1 : 0;
7568 if (best_cand == NULL || best_pref < cand_pref)
7570 best_cand = cand;
7571 best_pref = cand_pref;
7575 if (!best_cand)
7576 continue;
7578 tree comp = get_computation_at (data->current_loop,
7579 &dummy_use, best_cand,
7580 SSA_NAME_DEF_STMT (def));
7581 if (!comp)
7582 continue;
7584 if (count > 1)
7586 tree vexpr = make_node (DEBUG_EXPR_DECL);
7587 DECL_ARTIFICIAL (vexpr) = 1;
7588 TREE_TYPE (vexpr) = TREE_TYPE (comp);
7589 if (SSA_NAME_VAR (def))
7590 DECL_MODE (vexpr) = DECL_MODE (SSA_NAME_VAR (def));
7591 else
7592 DECL_MODE (vexpr) = TYPE_MODE (TREE_TYPE (vexpr));
7593 gdebug *def_temp
7594 = gimple_build_debug_bind (vexpr, comp, NULL);
7595 gimple_stmt_iterator gsi;
7597 if (gimple_code (SSA_NAME_DEF_STMT (def)) == GIMPLE_PHI)
7598 gsi = gsi_after_labels (gimple_bb
7599 (SSA_NAME_DEF_STMT (def)));
7600 else
7601 gsi = gsi_for_stmt (SSA_NAME_DEF_STMT (def));
7603 gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT);
7604 comp = vexpr;
7607 FOR_EACH_IMM_USE_STMT (stmt, imm_iter, def)
7609 if (!gimple_debug_bind_p (stmt))
7610 continue;
7612 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
7613 SET_USE (use_p, comp);
7615 update_stmt (stmt);
7621 release_defs_bitset (toremove);
7623 BITMAP_FREE (toremove);
7626 /* Frees memory occupied by struct tree_niter_desc in *VALUE. Callback
7627 for hash_map::traverse. */
7629 bool
7630 free_tree_niter_desc (edge const &, tree_niter_desc *const &value, void *)
7632 free (value);
7633 return true;
7636 /* Frees data allocated by the optimization of a single loop. */
7638 static void
7639 free_loop_data (struct ivopts_data *data)
7641 unsigned i, j;
7642 bitmap_iterator bi;
7643 tree obj;
7645 if (data->niters)
7647 data->niters->traverse<void *, free_tree_niter_desc> (NULL);
7648 delete data->niters;
7649 data->niters = NULL;
7652 EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, i, bi)
7654 struct version_info *info;
7656 info = ver_info (data, i);
7657 info->iv = NULL;
7658 info->has_nonlin_use = false;
7659 info->preserve_biv = false;
7660 info->inv_id = 0;
7662 bitmap_clear (data->relevant);
7663 bitmap_clear (data->important_candidates);
7665 for (i = 0; i < data->vgroups.length (); i++)
7667 struct iv_group *group = data->vgroups[i];
7669 for (j = 0; j < group->vuses.length (); j++)
7670 free (group->vuses[j]);
7671 group->vuses.release ();
7673 BITMAP_FREE (group->related_cands);
7674 for (j = 0; j < group->n_map_members; j++)
7675 if (group->cost_map[j].depends_on)
7676 BITMAP_FREE (group->cost_map[j].depends_on);
7678 free (group->cost_map);
7679 free (group);
7681 data->vgroups.truncate (0);
7683 for (i = 0; i < data->vcands.length (); i++)
7685 struct iv_cand *cand = data->vcands[i];
7687 if (cand->depends_on)
7688 BITMAP_FREE (cand->depends_on);
7689 free (cand);
7691 data->vcands.truncate (0);
7693 if (data->version_info_size < num_ssa_names)
7695 data->version_info_size = 2 * num_ssa_names;
7696 free (data->version_info);
7697 data->version_info = XCNEWVEC (struct version_info, data->version_info_size);
7700 data->max_inv_id = 0;
7702 FOR_EACH_VEC_ELT (decl_rtl_to_reset, i, obj)
7703 SET_DECL_RTL (obj, NULL_RTX);
7705 decl_rtl_to_reset.truncate (0);
7707 data->inv_expr_tab->empty ();
7708 data->max_inv_expr_id = 0;
7710 data->iv_common_cand_tab->empty ();
7711 data->iv_common_cands.truncate (0);
7714 /* Finalizes data structures used by the iv optimization pass. LOOPS is the
7715 loop tree. */
7717 static void
7718 tree_ssa_iv_optimize_finalize (struct ivopts_data *data)
7720 free_loop_data (data);
7721 free (data->version_info);
7722 BITMAP_FREE (data->relevant);
7723 BITMAP_FREE (data->important_candidates);
7725 decl_rtl_to_reset.release ();
7726 data->vgroups.release ();
7727 data->vcands.release ();
7728 delete data->inv_expr_tab;
7729 data->inv_expr_tab = NULL;
7730 free_affine_expand_cache (&data->name_expansion_cache);
7731 delete data->iv_common_cand_tab;
7732 data->iv_common_cand_tab = NULL;
7733 data->iv_common_cands.release ();
7734 obstack_free (&data->iv_obstack, NULL);
7737 /* Returns true if the loop body BODY includes any function calls. */
7739 static bool
7740 loop_body_includes_call (basic_block *body, unsigned num_nodes)
7742 gimple_stmt_iterator gsi;
7743 unsigned i;
7745 for (i = 0; i < num_nodes; i++)
7746 for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi); gsi_next (&gsi))
7748 gimple *stmt = gsi_stmt (gsi);
7749 if (is_gimple_call (stmt)
7750 && !gimple_call_internal_p (stmt)
7751 && !is_inexpensive_builtin (gimple_call_fndecl (stmt)))
7752 return true;
7754 return false;
7757 /* Optimizes the LOOP. Returns true if anything changed. */
7759 static bool
7760 tree_ssa_iv_optimize_loop (struct ivopts_data *data, struct loop *loop)
7762 bool changed = false;
7763 struct iv_ca *iv_ca;
7764 edge exit = single_dom_exit (loop);
7765 basic_block *body;
7767 gcc_assert (!data->niters);
7768 data->current_loop = loop;
7769 data->loop_loc = find_loop_location (loop);
7770 data->speed = optimize_loop_for_speed_p (loop);
7772 if (dump_file && (dump_flags & TDF_DETAILS))
7774 fprintf (dump_file, "Processing loop %d", loop->num);
7775 if (data->loop_loc != UNKNOWN_LOCATION)
7776 fprintf (dump_file, " at %s:%d", LOCATION_FILE (data->loop_loc),
7777 LOCATION_LINE (data->loop_loc));
7778 fprintf (dump_file, "\n");
7780 if (exit)
7782 fprintf (dump_file, " single exit %d -> %d, exit condition ",
7783 exit->src->index, exit->dest->index);
7784 print_gimple_stmt (dump_file, last_stmt (exit->src), 0, TDF_SLIM);
7785 fprintf (dump_file, "\n");
7788 fprintf (dump_file, "\n");
7791 body = get_loop_body (loop);
7792 data->body_includes_call = loop_body_includes_call (body, loop->num_nodes);
7793 renumber_gimple_stmt_uids_in_blocks (body, loop->num_nodes);
7794 free (body);
7796 data->loop_single_exit_p = exit != NULL && loop_only_exit_p (loop, exit);
7798 /* For each ssa name determines whether it behaves as an induction variable
7799 in some loop. */
7800 if (!find_induction_variables (data))
7801 goto finish;
7803 /* Finds interesting uses (item 1). */
7804 find_interesting_uses (data);
7805 if (data->vgroups.length () > MAX_CONSIDERED_GROUPS)
7806 goto finish;
7808 /* Finds candidates for the induction variables (item 2). */
7809 find_iv_candidates (data);
7811 /* Calculates the costs (item 3, part 1). */
7812 determine_iv_costs (data);
7813 determine_group_iv_costs (data);
7814 determine_set_costs (data);
7816 /* Find the optimal set of induction variables (item 3, part 2). */
7817 iv_ca = find_optimal_iv_set (data);
7818 if (!iv_ca)
7819 goto finish;
7820 changed = true;
7822 /* Create the new induction variables (item 4, part 1). */
7823 create_new_ivs (data, iv_ca);
7824 iv_ca_free (&iv_ca);
7826 /* Rewrite the uses (item 4, part 2). */
7827 rewrite_groups (data);
7829 /* Remove the ivs that are unused after rewriting. */
7830 remove_unused_ivs (data);
7832 /* We have changed the structure of induction variables; it might happen
7833 that definitions in the scev database refer to some of them that were
7834 eliminated. */
7835 scev_reset ();
7837 finish:
7838 free_loop_data (data);
7840 return changed;
7843 /* Main entry point. Optimizes induction variables in loops. */
7845 void
7846 tree_ssa_iv_optimize (void)
7848 struct loop *loop;
7849 struct ivopts_data data;
7851 tree_ssa_iv_optimize_init (&data);
7853 /* Optimize the loops starting with the innermost ones. */
7854 FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
7856 if (dump_file && (dump_flags & TDF_DETAILS))
7857 flow_loop_dump (loop, dump_file, NULL, 1);
7859 tree_ssa_iv_optimize_loop (&data, loop);
7862 tree_ssa_iv_optimize_finalize (&data);