[AArch64] Properly handle mvn-register and add EON+shift pattern and cost appropriately
[official-gcc.git] / gcc / cfgloop.h
blob1d845729e5ea3b046c391579bf394ad39726df82
1 /* Natural loop functions
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 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 #ifndef GCC_CFGLOOP_H
21 #define GCC_CFGLOOP_H
23 #include "double-int.h"
24 #include "wide-int.h"
25 #include "bitmap.h"
26 #include "sbitmap.h"
27 #include "hashtab.h"
28 #include "hash-set.h"
29 #include "vec.h"
30 #include "machmode.h"
31 #include "tm.h"
32 #include "hard-reg-set.h"
33 #include "input.h"
34 #include "function.h"
35 #include "cfgloopmanip.h"
37 /* Structure to hold decision about unrolling/peeling. */
38 enum lpt_dec
40 LPT_NONE,
41 LPT_UNROLL_CONSTANT,
42 LPT_UNROLL_RUNTIME,
43 LPT_UNROLL_STUPID
46 struct GTY (()) lpt_decision {
47 enum lpt_dec decision;
48 unsigned times;
51 /* The type of extend applied to an IV. */
52 enum iv_extend_code
54 IV_SIGN_EXTEND,
55 IV_ZERO_EXTEND,
56 IV_UNKNOWN_EXTEND
59 /* The structure describing a bound on number of iterations of a loop. */
61 struct GTY ((chain_next ("%h.next"))) nb_iter_bound {
62 /* The statement STMT is executed at most ... */
63 gimple stmt;
65 /* ... BOUND + 1 times (BOUND must be an unsigned constant).
66 The + 1 is added for the following reasons:
68 a) 0 would otherwise be unused, while we would need to care more about
69 overflows (as MAX + 1 is sometimes produced as the estimate on number
70 of executions of STMT).
71 b) it is consistent with the result of number_of_iterations_exit. */
72 widest_int bound;
74 /* True if the statement will cause the loop to be leaved the (at most)
75 BOUND + 1-st time it is executed, that is, all the statements after it
76 are executed at most BOUND times. */
77 bool is_exit;
79 /* The next bound in the list. */
80 struct nb_iter_bound *next;
83 /* Description of the loop exit. */
85 struct GTY ((for_user)) loop_exit {
86 /* The exit edge. */
87 edge e;
89 /* Previous and next exit in the list of the exits of the loop. */
90 struct loop_exit *prev;
91 struct loop_exit *next;
93 /* Next element in the list of loops from that E exits. */
94 struct loop_exit *next_e;
97 struct loop_exit_hasher : ggc_hasher<loop_exit *>
99 typedef edge compare_type;
101 static hashval_t hash (loop_exit *);
102 static bool equal (loop_exit *, edge);
103 static void remove (loop_exit *);
106 typedef struct loop *loop_p;
108 /* An integer estimation of the number of iterations. Estimate_state
109 describes what is the state of the estimation. */
110 enum loop_estimation
112 /* Estimate was not computed yet. */
113 EST_NOT_COMPUTED,
114 /* Estimate is ready. */
115 EST_AVAILABLE,
116 EST_LAST
119 /* Structure to hold information for each natural loop. */
120 struct GTY ((chain_next ("%h.next"))) loop {
121 /* Index into loops array. */
122 int num;
124 /* Number of loop insns. */
125 unsigned ninsns;
127 /* Basic block of loop header. */
128 basic_block header;
130 /* Basic block of loop latch. */
131 basic_block latch;
133 /* For loop unrolling/peeling decision. */
134 struct lpt_decision lpt_decision;
136 /* Average number of executed insns per iteration. */
137 unsigned av_ninsns;
139 /* Number of blocks contained within the loop. */
140 unsigned num_nodes;
142 /* Superloops of the loop, starting with the outermost loop. */
143 vec<loop_p, va_gc> *superloops;
145 /* The first inner (child) loop or NULL if innermost loop. */
146 struct loop *inner;
148 /* Link to the next (sibling) loop. */
149 struct loop *next;
151 /* Auxiliary info specific to a pass. */
152 PTR GTY ((skip (""))) aux;
154 /* The number of times the latch of the loop is executed. This can be an
155 INTEGER_CST, or a symbolic expression representing the number of
156 iterations like "N - 1", or a COND_EXPR containing the runtime
157 conditions under which the number of iterations is non zero.
159 Don't access this field directly: number_of_latch_executions
160 computes and caches the computed information in this field. */
161 tree nb_iterations;
163 /* An integer guaranteed to be greater or equal to nb_iterations. Only
164 valid if any_upper_bound is true. */
165 widest_int nb_iterations_upper_bound;
167 /* An integer giving an estimate on nb_iterations. Unlike
168 nb_iterations_upper_bound, there is no guarantee that it is at least
169 nb_iterations. */
170 widest_int nb_iterations_estimate;
172 bool any_upper_bound;
173 bool any_estimate;
175 /* True if the loop can be parallel. */
176 bool can_be_parallel;
178 /* True if -Waggressive-loop-optimizations warned about this loop
179 already. */
180 bool warned_aggressive_loop_optimizations;
182 /* An integer estimation of the number of iterations. Estimate_state
183 describes what is the state of the estimation. */
184 enum loop_estimation estimate_state;
186 /* If > 0, an integer, where the user asserted that for any
187 I in [ 0, nb_iterations ) and for any J in
188 [ I, min ( I + safelen, nb_iterations ) ), the Ith and Jth iterations
189 of the loop can be safely evaluated concurrently. */
190 int safelen;
192 /* True if this loop should never be vectorized. */
193 bool dont_vectorize;
195 /* True if we should try harder to vectorize this loop. */
196 bool force_vectorize;
198 /* For SIMD loops, this is a unique identifier of the loop, referenced
199 by IFN_GOMP_SIMD_VF, IFN_GOMP_SIMD_LANE and IFN_GOMP_SIMD_LAST_LANE
200 builtins. */
201 tree simduid;
203 /* Upper bound on number of iterations of a loop. */
204 struct nb_iter_bound *bounds;
206 /* Head of the cyclic list of the exits of the loop. */
207 struct loop_exit *exits;
209 /* Number of iteration analysis data for RTL. */
210 struct niter_desc *simple_loop_desc;
212 /* For sanity checking during loop fixup we record here the former
213 loop header for loops marked for removal. Note that this prevents
214 the basic-block from being collected but its index can still be
215 reused. */
216 basic_block former_header;
219 /* Flags for state of loop structure. */
220 enum
222 LOOPS_HAVE_PREHEADERS = 1,
223 LOOPS_HAVE_SIMPLE_LATCHES = 2,
224 LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS = 4,
225 LOOPS_HAVE_RECORDED_EXITS = 8,
226 LOOPS_MAY_HAVE_MULTIPLE_LATCHES = 16,
227 LOOP_CLOSED_SSA = 32,
228 LOOPS_NEED_FIXUP = 64,
229 LOOPS_HAVE_FALLTHRU_PREHEADERS = 128
232 #define LOOPS_NORMAL (LOOPS_HAVE_PREHEADERS | LOOPS_HAVE_SIMPLE_LATCHES \
233 | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
234 #define AVOID_CFG_MODIFICATIONS (LOOPS_MAY_HAVE_MULTIPLE_LATCHES)
236 /* Structure to hold CFG information about natural loops within a function. */
237 struct GTY (()) loops {
238 /* State of loops. */
239 int state;
241 /* Array of the loops. */
242 vec<loop_p, va_gc> *larray;
244 /* Maps edges to the list of their descriptions as loop exits. Edges
245 whose sources or destinations have loop_father == NULL (which may
246 happen during the cfg manipulations) should not appear in EXITS. */
247 hash_table<loop_exit_hasher> *GTY(()) exits;
249 /* Pointer to root of loop hierarchy tree. */
250 struct loop *tree_root;
253 /* Loop recognition. */
254 bool bb_loop_header_p (basic_block);
255 void init_loops_structure (struct function *, struct loops *, unsigned);
256 extern struct loops *flow_loops_find (struct loops *);
257 extern void disambiguate_loops_with_multiple_latches (void);
258 extern void flow_loops_free (struct loops *);
259 extern void flow_loops_dump (FILE *,
260 void (*)(const struct loop *, FILE *, int), int);
261 extern void flow_loop_dump (const struct loop *, FILE *,
262 void (*)(const struct loop *, FILE *, int), int);
263 struct loop *alloc_loop (void);
264 extern void flow_loop_free (struct loop *);
265 int flow_loop_nodes_find (basic_block, struct loop *);
266 unsigned fix_loop_structure (bitmap changed_bbs);
267 bool mark_irreducible_loops (void);
268 void release_recorded_exits (void);
269 void record_loop_exits (void);
270 void rescan_loop_exit (edge, bool, bool);
272 /* Loop data structure manipulation/querying. */
273 extern void flow_loop_tree_node_add (struct loop *, struct loop *);
274 extern void flow_loop_tree_node_remove (struct loop *);
275 extern bool flow_loop_nested_p (const struct loop *, const struct loop *);
276 extern bool flow_bb_inside_loop_p (const struct loop *, const_basic_block);
277 extern struct loop * find_common_loop (struct loop *, struct loop *);
278 struct loop *superloop_at_depth (struct loop *, unsigned);
279 struct eni_weights_d;
280 extern int num_loop_insns (const struct loop *);
281 extern int average_num_loop_insns (const struct loop *);
282 extern unsigned get_loop_level (const struct loop *);
283 extern bool loop_exit_edge_p (const struct loop *, const_edge);
284 extern bool loop_exits_to_bb_p (struct loop *, basic_block);
285 extern bool loop_exits_from_bb_p (struct loop *, basic_block);
286 extern void mark_loop_exit_edges (void);
287 extern location_t get_loop_location (struct loop *loop);
289 /* Loops & cfg manipulation. */
290 extern basic_block *get_loop_body (const struct loop *);
291 extern unsigned get_loop_body_with_size (const struct loop *, basic_block *,
292 unsigned);
293 extern basic_block *get_loop_body_in_dom_order (const struct loop *);
294 extern basic_block *get_loop_body_in_bfs_order (const struct loop *);
295 extern basic_block *get_loop_body_in_custom_order (const struct loop *,
296 int (*) (const void *, const void *));
298 extern vec<edge> get_loop_exit_edges (const struct loop *);
299 extern edge single_exit (const struct loop *);
300 extern edge single_likely_exit (struct loop *loop);
301 extern unsigned num_loop_branches (const struct loop *);
303 extern edge loop_preheader_edge (const struct loop *);
304 extern edge loop_latch_edge (const struct loop *);
306 extern void add_bb_to_loop (basic_block, struct loop *);
307 extern void remove_bb_from_loops (basic_block);
309 extern void cancel_loop_tree (struct loop *);
310 extern void delete_loop (struct loop *);
313 extern void verify_loop_structure (void);
315 /* Loop analysis. */
316 extern bool just_once_each_iteration_p (const struct loop *, const_basic_block);
317 gcov_type expected_loop_iterations_unbounded (const struct loop *);
318 extern unsigned expected_loop_iterations (const struct loop *);
319 extern rtx doloop_condition_get (rtx);
321 void mark_loop_for_removal (loop_p);
323 /* Induction variable analysis. */
325 /* The description of induction variable. The things are a bit complicated
326 due to need to handle subregs and extends. The value of the object described
327 by it can be obtained as follows (all computations are done in extend_mode):
329 Value in i-th iteration is
330 delta + mult * extend_{extend_mode} (subreg_{mode} (base + i * step)).
332 If first_special is true, the value in the first iteration is
333 delta + mult * base
335 If extend = UNKNOWN, first_special must be false, delta 0, mult 1 and value is
336 subreg_{mode} (base + i * step)
338 The get_iv_value function can be used to obtain these expressions.
340 ??? Add a third mode field that would specify the mode in that inner
341 computation is done, which would enable it to be different from the
342 outer one? */
344 struct rtx_iv
346 /* Its base and step (mode of base and step is supposed to be extend_mode,
347 see the description above). */
348 rtx base, step;
350 /* The type of extend applied to it (IV_SIGN_EXTEND, IV_ZERO_EXTEND,
351 or IV_UNKNOWN_EXTEND). */
352 enum iv_extend_code extend;
354 /* Operations applied in the extended mode. */
355 rtx delta, mult;
357 /* The mode it is extended to. */
358 machine_mode extend_mode;
360 /* The mode the variable iterates in. */
361 machine_mode mode;
363 /* Whether the first iteration needs to be handled specially. */
364 unsigned first_special : 1;
367 /* The description of an exit from the loop and of the number of iterations
368 till we take the exit. */
370 struct GTY(()) niter_desc
372 /* The edge out of the loop. */
373 edge out_edge;
375 /* The other edge leading from the condition. */
376 edge in_edge;
378 /* True if we are able to say anything about number of iterations of the
379 loop. */
380 bool simple_p;
382 /* True if the loop iterates the constant number of times. */
383 bool const_iter;
385 /* Number of iterations if constant. */
386 uint64_t niter;
388 /* Assumptions under that the rest of the information is valid. */
389 rtx assumptions;
391 /* Assumptions under that the loop ends before reaching the latch,
392 even if value of niter_expr says otherwise. */
393 rtx noloop_assumptions;
395 /* Condition under that the loop is infinite. */
396 rtx infinite;
398 /* Whether the comparison is signed. */
399 bool signed_p;
401 /* The mode in that niter_expr should be computed. */
402 machine_mode mode;
404 /* The number of iterations of the loop. */
405 rtx niter_expr;
408 extern void iv_analysis_loop_init (struct loop *);
409 extern bool iv_analyze (rtx_insn *, rtx, struct rtx_iv *);
410 extern bool iv_analyze_result (rtx_insn *, rtx, struct rtx_iv *);
411 extern bool iv_analyze_expr (rtx_insn *, rtx, machine_mode,
412 struct rtx_iv *);
413 extern rtx get_iv_value (struct rtx_iv *, rtx);
414 extern bool biv_p (rtx_insn *, rtx);
415 extern void find_simple_exit (struct loop *, struct niter_desc *);
416 extern void iv_analysis_done (void);
418 extern struct niter_desc *get_simple_loop_desc (struct loop *loop);
419 extern void free_simple_loop_desc (struct loop *loop);
421 static inline struct niter_desc *
422 simple_loop_desc (struct loop *loop)
424 return loop->simple_loop_desc;
427 /* Accessors for the loop structures. */
429 /* Returns the loop with index NUM from FNs loop tree. */
431 static inline struct loop *
432 get_loop (struct function *fn, unsigned num)
434 return (*loops_for_fn (fn)->larray)[num];
437 /* Returns the number of superloops of LOOP. */
439 static inline unsigned
440 loop_depth (const struct loop *loop)
442 return vec_safe_length (loop->superloops);
445 /* Returns the immediate superloop of LOOP, or NULL if LOOP is the outermost
446 loop. */
448 static inline struct loop *
449 loop_outer (const struct loop *loop)
451 unsigned n = vec_safe_length (loop->superloops);
453 if (n == 0)
454 return NULL;
456 return (*loop->superloops)[n - 1];
459 /* Returns true if LOOP has at least one exit edge. */
461 static inline bool
462 loop_has_exit_edges (const struct loop *loop)
464 return loop->exits->next->e != NULL;
467 /* Returns the list of loops in FN. */
469 inline vec<loop_p, va_gc> *
470 get_loops (struct function *fn)
472 struct loops *loops = loops_for_fn (fn);
473 if (!loops)
474 return NULL;
476 return loops->larray;
479 /* Returns the number of loops in FN (including the removed
480 ones and the fake loop that forms the root of the loop tree). */
482 static inline unsigned
483 number_of_loops (struct function *fn)
485 struct loops *loops = loops_for_fn (fn);
486 if (!loops)
487 return 0;
489 return vec_safe_length (loops->larray);
492 /* Returns true if state of the loops satisfies all properties
493 described by FLAGS. */
495 static inline bool
496 loops_state_satisfies_p (unsigned flags)
498 return (current_loops->state & flags) == flags;
501 /* Sets FLAGS to the loops state. */
503 static inline void
504 loops_state_set (unsigned flags)
506 current_loops->state |= flags;
509 /* Clears FLAGS from the loops state. */
511 static inline void
512 loops_state_clear (unsigned flags)
514 if (!current_loops)
515 return;
516 current_loops->state &= ~flags;
519 /* Loop iterators. */
521 /* Flags for loop iteration. */
523 enum li_flags
525 LI_INCLUDE_ROOT = 1, /* Include the fake root of the loop tree. */
526 LI_FROM_INNERMOST = 2, /* Iterate over the loops in the reverse order,
527 starting from innermost ones. */
528 LI_ONLY_INNERMOST = 4 /* Iterate only over innermost loops. */
531 /* The iterator for loops. */
533 struct loop_iterator
535 loop_iterator (loop_p *loop, unsigned flags);
536 ~loop_iterator ();
538 inline loop_p next ();
540 /* The list of loops to visit. */
541 vec<int> to_visit;
543 /* The index of the actual loop. */
544 unsigned idx;
547 inline loop_p
548 loop_iterator::next ()
550 int anum;
552 while (this->to_visit.iterate (this->idx, &anum))
554 this->idx++;
555 loop_p loop = get_loop (cfun, anum);
556 if (loop)
557 return loop;
560 return NULL;
563 inline
564 loop_iterator::loop_iterator (loop_p *loop, unsigned flags)
566 struct loop *aloop;
567 unsigned i;
568 int mn;
570 this->idx = 0;
571 if (!current_loops)
573 this->to_visit.create (0);
574 *loop = NULL;
575 return;
578 this->to_visit.create (number_of_loops (cfun));
579 mn = (flags & LI_INCLUDE_ROOT) ? 0 : 1;
581 if (flags & LI_ONLY_INNERMOST)
583 for (i = 0; vec_safe_iterate (current_loops->larray, i, &aloop); i++)
584 if (aloop != NULL
585 && aloop->inner == NULL
586 && aloop->num >= mn)
587 this->to_visit.quick_push (aloop->num);
589 else if (flags & LI_FROM_INNERMOST)
591 /* Push the loops to LI->TO_VISIT in postorder. */
592 for (aloop = current_loops->tree_root;
593 aloop->inner != NULL;
594 aloop = aloop->inner)
595 continue;
597 while (1)
599 if (aloop->num >= mn)
600 this->to_visit.quick_push (aloop->num);
602 if (aloop->next)
604 for (aloop = aloop->next;
605 aloop->inner != NULL;
606 aloop = aloop->inner)
607 continue;
609 else if (!loop_outer (aloop))
610 break;
611 else
612 aloop = loop_outer (aloop);
615 else
617 /* Push the loops to LI->TO_VISIT in preorder. */
618 aloop = current_loops->tree_root;
619 while (1)
621 if (aloop->num >= mn)
622 this->to_visit.quick_push (aloop->num);
624 if (aloop->inner != NULL)
625 aloop = aloop->inner;
626 else
628 while (aloop != NULL && aloop->next == NULL)
629 aloop = loop_outer (aloop);
630 if (aloop == NULL)
631 break;
632 aloop = aloop->next;
637 *loop = this->next ();
640 inline
641 loop_iterator::~loop_iterator ()
643 this->to_visit.release ();
646 #define FOR_EACH_LOOP(LOOP, FLAGS) \
647 for (loop_iterator li(&(LOOP), FLAGS); \
648 (LOOP); \
649 (LOOP) = li.next ())
651 /* The properties of the target. */
652 struct target_cfgloop {
653 /* Number of available registers. */
654 unsigned x_target_avail_regs;
656 /* Number of available registers that are call-clobbered. */
657 unsigned x_target_clobbered_regs;
659 /* Number of registers reserved for temporary expressions. */
660 unsigned x_target_res_regs;
662 /* The cost for register when there still is some reserve, but we are
663 approaching the number of available registers. */
664 unsigned x_target_reg_cost[2];
666 /* The cost for register when we need to spill. */
667 unsigned x_target_spill_cost[2];
670 extern struct target_cfgloop default_target_cfgloop;
671 #if SWITCHABLE_TARGET
672 extern struct target_cfgloop *this_target_cfgloop;
673 #else
674 #define this_target_cfgloop (&default_target_cfgloop)
675 #endif
677 #define target_avail_regs \
678 (this_target_cfgloop->x_target_avail_regs)
679 #define target_clobbered_regs \
680 (this_target_cfgloop->x_target_clobbered_regs)
681 #define target_res_regs \
682 (this_target_cfgloop->x_target_res_regs)
683 #define target_reg_cost \
684 (this_target_cfgloop->x_target_reg_cost)
685 #define target_spill_cost \
686 (this_target_cfgloop->x_target_spill_cost)
688 /* Register pressure estimation for induction variable optimizations & loop
689 invariant motion. */
690 extern unsigned estimate_reg_pressure_cost (unsigned, unsigned, bool, bool);
691 extern void init_set_costs (void);
693 /* Loop optimizer initialization. */
694 extern void loop_optimizer_init (unsigned);
695 extern void loop_optimizer_finalize (void);
697 /* Optimization passes. */
698 enum
700 UAP_UNROLL = 1, /* Enables unrolling of loops if it seems profitable. */
701 UAP_UNROLL_ALL = 2 /* Enables unrolling of all loops. */
704 extern void doloop_optimize_loops (void);
705 extern void move_loop_invariants (void);
706 extern vec<basic_block> get_loop_hot_path (const struct loop *loop);
708 /* Returns the outermost loop of the loop nest that contains LOOP.*/
709 static inline struct loop *
710 loop_outermost (struct loop *loop)
712 unsigned n = vec_safe_length (loop->superloops);
714 if (n <= 1)
715 return loop;
717 return (*loop->superloops)[1];
720 extern void record_niter_bound (struct loop *, const widest_int &, bool, bool);
721 extern HOST_WIDE_INT get_estimated_loop_iterations_int (struct loop *);
722 extern HOST_WIDE_INT get_max_loop_iterations_int (struct loop *);
723 extern bool get_estimated_loop_iterations (struct loop *loop, widest_int *nit);
724 extern bool get_max_loop_iterations (struct loop *loop, widest_int *nit);
725 extern int bb_loop_depth (const_basic_block);
727 /* Converts VAL to widest_int. */
729 static inline widest_int
730 gcov_type_to_wide_int (gcov_type val)
732 HOST_WIDE_INT a[2];
734 a[0] = (unsigned HOST_WIDE_INT) val;
735 /* If HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_WIDEST_INT, avoid shifting by
736 the size of type. */
737 val >>= HOST_BITS_PER_WIDE_INT - 1;
738 val >>= 1;
739 a[1] = (unsigned HOST_WIDE_INT) val;
741 return widest_int::from_array (a, 2);
743 #endif /* GCC_CFGLOOP_H */