Clean up some minor white space issues in trans-decl.c and trans-expr.c
[official-gcc.git] / gcc / loop-invariant.c
blob707f044f6dc13e37e49a305337589816b96730d7
1 /* RTL-level loop invariant motion.
2 Copyright (C) 2004-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 implements the loop invariant motion pass. It is very simple
21 (no calls, no loads/stores, etc.). This should be sufficient to cleanup
22 things like address arithmetics -- other more complicated invariants should
23 be eliminated on GIMPLE either in tree-ssa-loop-im.c or in tree-ssa-pre.c.
25 We proceed loop by loop -- it is simpler than trying to handle things
26 globally and should not lose much. First we inspect all sets inside loop
27 and create a dependency graph on insns (saying "to move this insn, you must
28 also move the following insns").
30 We then need to determine what to move. We estimate the number of registers
31 used and move as many invariants as possible while we still have enough free
32 registers. We prefer the expensive invariants.
34 Then we move the selected invariants out of the loop, creating a new
35 temporaries for them if necessary. */
37 #include "config.h"
38 #include "system.h"
39 #include "coretypes.h"
40 #include "backend.h"
41 #include "target.h"
42 #include "rtl.h"
43 #include "tree.h"
44 #include "cfghooks.h"
45 #include "df.h"
46 #include "tm_p.h"
47 #include "insn-config.h"
48 #include "regs.h"
49 #include "ira.h"
50 #include "recog.h"
51 #include "cfgrtl.h"
52 #include "cfgloop.h"
53 #include "expr.h"
54 #include "params.h"
55 #include "dumpfile.h"
57 /* The data stored for the loop. */
59 struct loop_data
61 struct loop *outermost_exit; /* The outermost exit of the loop. */
62 bool has_call; /* True if the loop contains a call. */
63 /* Maximal register pressure inside loop for given register class
64 (defined only for the pressure classes). */
65 int max_reg_pressure[N_REG_CLASSES];
66 /* Loop regs referenced and live pseudo-registers. */
67 bitmap_head regs_ref;
68 bitmap_head regs_live;
71 #define LOOP_DATA(LOOP) ((struct loop_data *) (LOOP)->aux)
73 /* The description of an use. */
75 struct use
77 rtx *pos; /* Position of the use. */
78 rtx_insn *insn; /* The insn in that the use occurs. */
79 unsigned addr_use_p; /* Whether the use occurs in an address. */
80 struct use *next; /* Next use in the list. */
83 /* The description of a def. */
85 struct def
87 struct use *uses; /* The list of uses that are uniquely reached
88 by it. */
89 unsigned n_uses; /* Number of such uses. */
90 unsigned n_addr_uses; /* Number of uses in addresses. */
91 unsigned invno; /* The corresponding invariant. */
92 bool can_prop_to_addr_uses; /* True if the corresponding inv can be
93 propagated into its address uses. */
96 /* The data stored for each invariant. */
98 struct invariant
100 /* The number of the invariant. */
101 unsigned invno;
103 /* The number of the invariant with the same value. */
104 unsigned eqto;
106 /* The number of invariants which eqto this. */
107 unsigned eqno;
109 /* If we moved the invariant out of the loop, the register that contains its
110 value. */
111 rtx reg;
113 /* If we moved the invariant out of the loop, the original regno
114 that contained its value. */
115 int orig_regno;
117 /* The definition of the invariant. */
118 struct def *def;
120 /* The insn in that it is defined. */
121 rtx_insn *insn;
123 /* Whether it is always executed. */
124 bool always_executed;
126 /* Whether to move the invariant. */
127 bool move;
129 /* Whether the invariant is cheap when used as an address. */
130 bool cheap_address;
132 /* Cost of the invariant. */
133 unsigned cost;
135 /* The invariants it depends on. */
136 bitmap depends_on;
138 /* Used for detecting already visited invariants during determining
139 costs of movements. */
140 unsigned stamp;
143 /* Currently processed loop. */
144 static struct loop *curr_loop;
146 /* Table of invariants indexed by the df_ref uid field. */
148 static unsigned int invariant_table_size = 0;
149 static struct invariant ** invariant_table;
151 /* Entry for hash table of invariant expressions. */
153 struct invariant_expr_entry
155 /* The invariant. */
156 struct invariant *inv;
158 /* Its value. */
159 rtx expr;
161 /* Its mode. */
162 machine_mode mode;
164 /* Its hash. */
165 hashval_t hash;
168 /* The actual stamp for marking already visited invariants during determining
169 costs of movements. */
171 static unsigned actual_stamp;
173 typedef struct invariant *invariant_p;
176 /* The invariants. */
178 static vec<invariant_p> invariants;
180 /* Check the size of the invariant table and realloc if necessary. */
182 static void
183 check_invariant_table_size (void)
185 if (invariant_table_size < DF_DEFS_TABLE_SIZE ())
187 unsigned int new_size = DF_DEFS_TABLE_SIZE () + (DF_DEFS_TABLE_SIZE () / 4);
188 invariant_table = XRESIZEVEC (struct invariant *, invariant_table, new_size);
189 memset (&invariant_table[invariant_table_size], 0,
190 (new_size - invariant_table_size) * sizeof (struct invariant *));
191 invariant_table_size = new_size;
195 /* Test for possibility of invariantness of X. */
197 static bool
198 check_maybe_invariant (rtx x)
200 enum rtx_code code = GET_CODE (x);
201 int i, j;
202 const char *fmt;
204 switch (code)
206 CASE_CONST_ANY:
207 case SYMBOL_REF:
208 case CONST:
209 case LABEL_REF:
210 return true;
212 case PC:
213 case CC0:
214 case UNSPEC_VOLATILE:
215 case CALL:
216 return false;
218 case REG:
219 return true;
221 case MEM:
222 /* Load/store motion is done elsewhere. ??? Perhaps also add it here?
223 It should not be hard, and might be faster than "elsewhere". */
225 /* Just handle the most trivial case where we load from an unchanging
226 location (most importantly, pic tables). */
227 if (MEM_READONLY_P (x) && !MEM_VOLATILE_P (x))
228 break;
230 return false;
232 case ASM_OPERANDS:
233 /* Don't mess with insns declared volatile. */
234 if (MEM_VOLATILE_P (x))
235 return false;
236 break;
238 default:
239 break;
242 fmt = GET_RTX_FORMAT (code);
243 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
245 if (fmt[i] == 'e')
247 if (!check_maybe_invariant (XEXP (x, i)))
248 return false;
250 else if (fmt[i] == 'E')
252 for (j = 0; j < XVECLEN (x, i); j++)
253 if (!check_maybe_invariant (XVECEXP (x, i, j)))
254 return false;
258 return true;
261 /* Returns the invariant definition for USE, or NULL if USE is not
262 invariant. */
264 static struct invariant *
265 invariant_for_use (df_ref use)
267 struct df_link *defs;
268 df_ref def;
269 basic_block bb = DF_REF_BB (use), def_bb;
271 if (DF_REF_FLAGS (use) & DF_REF_READ_WRITE)
272 return NULL;
274 defs = DF_REF_CHAIN (use);
275 if (!defs || defs->next)
276 return NULL;
277 def = defs->ref;
278 check_invariant_table_size ();
279 if (!invariant_table[DF_REF_ID (def)])
280 return NULL;
282 def_bb = DF_REF_BB (def);
283 if (!dominated_by_p (CDI_DOMINATORS, bb, def_bb))
284 return NULL;
285 return invariant_table[DF_REF_ID (def)];
288 /* Computes hash value for invariant expression X in INSN. */
290 static hashval_t
291 hash_invariant_expr_1 (rtx_insn *insn, rtx x)
293 enum rtx_code code = GET_CODE (x);
294 int i, j;
295 const char *fmt;
296 hashval_t val = code;
297 int do_not_record_p;
298 df_ref use;
299 struct invariant *inv;
301 switch (code)
303 CASE_CONST_ANY:
304 case SYMBOL_REF:
305 case CONST:
306 case LABEL_REF:
307 return hash_rtx (x, GET_MODE (x), &do_not_record_p, NULL, false);
309 case REG:
310 use = df_find_use (insn, x);
311 if (!use)
312 return hash_rtx (x, GET_MODE (x), &do_not_record_p, NULL, false);
313 inv = invariant_for_use (use);
314 if (!inv)
315 return hash_rtx (x, GET_MODE (x), &do_not_record_p, NULL, false);
317 gcc_assert (inv->eqto != ~0u);
318 return inv->eqto;
320 default:
321 break;
324 fmt = GET_RTX_FORMAT (code);
325 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
327 if (fmt[i] == 'e')
328 val ^= hash_invariant_expr_1 (insn, XEXP (x, i));
329 else if (fmt[i] == 'E')
331 for (j = 0; j < XVECLEN (x, i); j++)
332 val ^= hash_invariant_expr_1 (insn, XVECEXP (x, i, j));
334 else if (fmt[i] == 'i' || fmt[i] == 'n')
335 val ^= XINT (x, i);
338 return val;
341 /* Returns true if the invariant expressions E1 and E2 used in insns INSN1
342 and INSN2 have always the same value. */
344 static bool
345 invariant_expr_equal_p (rtx_insn *insn1, rtx e1, rtx_insn *insn2, rtx e2)
347 enum rtx_code code = GET_CODE (e1);
348 int i, j;
349 const char *fmt;
350 df_ref use1, use2;
351 struct invariant *inv1 = NULL, *inv2 = NULL;
352 rtx sub1, sub2;
354 /* If mode of only one of the operands is VOIDmode, it is not equivalent to
355 the other one. If both are VOIDmode, we rely on the caller of this
356 function to verify that their modes are the same. */
357 if (code != GET_CODE (e2) || GET_MODE (e1) != GET_MODE (e2))
358 return false;
360 switch (code)
362 CASE_CONST_ANY:
363 case SYMBOL_REF:
364 case CONST:
365 case LABEL_REF:
366 return rtx_equal_p (e1, e2);
368 case REG:
369 use1 = df_find_use (insn1, e1);
370 use2 = df_find_use (insn2, e2);
371 if (use1)
372 inv1 = invariant_for_use (use1);
373 if (use2)
374 inv2 = invariant_for_use (use2);
376 if (!inv1 && !inv2)
377 return rtx_equal_p (e1, e2);
379 if (!inv1 || !inv2)
380 return false;
382 gcc_assert (inv1->eqto != ~0u);
383 gcc_assert (inv2->eqto != ~0u);
384 return inv1->eqto == inv2->eqto;
386 default:
387 break;
390 fmt = GET_RTX_FORMAT (code);
391 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
393 if (fmt[i] == 'e')
395 sub1 = XEXP (e1, i);
396 sub2 = XEXP (e2, i);
398 if (!invariant_expr_equal_p (insn1, sub1, insn2, sub2))
399 return false;
402 else if (fmt[i] == 'E')
404 if (XVECLEN (e1, i) != XVECLEN (e2, i))
405 return false;
407 for (j = 0; j < XVECLEN (e1, i); j++)
409 sub1 = XVECEXP (e1, i, j);
410 sub2 = XVECEXP (e2, i, j);
412 if (!invariant_expr_equal_p (insn1, sub1, insn2, sub2))
413 return false;
416 else if (fmt[i] == 'i' || fmt[i] == 'n')
418 if (XINT (e1, i) != XINT (e2, i))
419 return false;
421 /* Unhandled type of subexpression, we fail conservatively. */
422 else
423 return false;
426 return true;
429 struct invariant_expr_hasher : free_ptr_hash <invariant_expr_entry>
431 static inline hashval_t hash (const invariant_expr_entry *);
432 static inline bool equal (const invariant_expr_entry *,
433 const invariant_expr_entry *);
436 /* Returns hash value for invariant expression entry ENTRY. */
438 inline hashval_t
439 invariant_expr_hasher::hash (const invariant_expr_entry *entry)
441 return entry->hash;
444 /* Compares invariant expression entries ENTRY1 and ENTRY2. */
446 inline bool
447 invariant_expr_hasher::equal (const invariant_expr_entry *entry1,
448 const invariant_expr_entry *entry2)
450 if (entry1->mode != entry2->mode)
451 return 0;
453 return invariant_expr_equal_p (entry1->inv->insn, entry1->expr,
454 entry2->inv->insn, entry2->expr);
457 typedef hash_table<invariant_expr_hasher> invariant_htab_type;
459 /* Checks whether invariant with value EXPR in machine mode MODE is
460 recorded in EQ. If this is the case, return the invariant. Otherwise
461 insert INV to the table for this expression and return INV. */
463 static struct invariant *
464 find_or_insert_inv (invariant_htab_type *eq, rtx expr, machine_mode mode,
465 struct invariant *inv)
467 hashval_t hash = hash_invariant_expr_1 (inv->insn, expr);
468 struct invariant_expr_entry *entry;
469 struct invariant_expr_entry pentry;
470 invariant_expr_entry **slot;
472 pentry.expr = expr;
473 pentry.inv = inv;
474 pentry.mode = mode;
475 slot = eq->find_slot_with_hash (&pentry, hash, INSERT);
476 entry = *slot;
478 if (entry)
479 return entry->inv;
481 entry = XNEW (struct invariant_expr_entry);
482 entry->inv = inv;
483 entry->expr = expr;
484 entry->mode = mode;
485 entry->hash = hash;
486 *slot = entry;
488 return inv;
491 /* Finds invariants identical to INV and records the equivalence. EQ is the
492 hash table of the invariants. */
494 static void
495 find_identical_invariants (invariant_htab_type *eq, struct invariant *inv)
497 unsigned depno;
498 bitmap_iterator bi;
499 struct invariant *dep;
500 rtx expr, set;
501 machine_mode mode;
502 struct invariant *tmp;
504 if (inv->eqto != ~0u)
505 return;
507 EXECUTE_IF_SET_IN_BITMAP (inv->depends_on, 0, depno, bi)
509 dep = invariants[depno];
510 find_identical_invariants (eq, dep);
513 set = single_set (inv->insn);
514 expr = SET_SRC (set);
515 mode = GET_MODE (expr);
516 if (mode == VOIDmode)
517 mode = GET_MODE (SET_DEST (set));
519 tmp = find_or_insert_inv (eq, expr, mode, inv);
520 inv->eqto = tmp->invno;
522 if (tmp->invno != inv->invno && inv->always_executed)
523 tmp->eqno++;
525 if (dump_file && inv->eqto != inv->invno)
526 fprintf (dump_file,
527 "Invariant %d is equivalent to invariant %d.\n",
528 inv->invno, inv->eqto);
531 /* Find invariants with the same value and record the equivalences. */
533 static void
534 merge_identical_invariants (void)
536 unsigned i;
537 struct invariant *inv;
538 invariant_htab_type eq (invariants.length ());
540 FOR_EACH_VEC_ELT (invariants, i, inv)
541 find_identical_invariants (&eq, inv);
544 /* Determines the basic blocks inside LOOP that are always executed and
545 stores their bitmap to ALWAYS_REACHED. MAY_EXIT is a bitmap of
546 basic blocks that may either exit the loop, or contain the call that
547 does not have to return. BODY is body of the loop obtained by
548 get_loop_body_in_dom_order. */
550 static void
551 compute_always_reached (struct loop *loop, basic_block *body,
552 bitmap may_exit, bitmap always_reached)
554 unsigned i;
556 for (i = 0; i < loop->num_nodes; i++)
558 if (dominated_by_p (CDI_DOMINATORS, loop->latch, body[i]))
559 bitmap_set_bit (always_reached, i);
561 if (bitmap_bit_p (may_exit, i))
562 return;
566 /* Finds exits out of the LOOP with body BODY. Marks blocks in that we may
567 exit the loop by cfg edge to HAS_EXIT and MAY_EXIT. In MAY_EXIT
568 additionally mark blocks that may exit due to a call. */
570 static void
571 find_exits (struct loop *loop, basic_block *body,
572 bitmap may_exit, bitmap has_exit)
574 unsigned i;
575 edge_iterator ei;
576 edge e;
577 struct loop *outermost_exit = loop, *aexit;
578 bool has_call = false;
579 rtx_insn *insn;
581 for (i = 0; i < loop->num_nodes; i++)
583 if (body[i]->loop_father == loop)
585 FOR_BB_INSNS (body[i], insn)
587 if (CALL_P (insn)
588 && (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)
589 || !RTL_CONST_OR_PURE_CALL_P (insn)))
591 has_call = true;
592 bitmap_set_bit (may_exit, i);
593 break;
597 FOR_EACH_EDGE (e, ei, body[i]->succs)
599 if (flow_bb_inside_loop_p (loop, e->dest))
600 continue;
602 bitmap_set_bit (may_exit, i);
603 bitmap_set_bit (has_exit, i);
604 outermost_exit = find_common_loop (outermost_exit,
605 e->dest->loop_father);
607 continue;
610 /* Use the data stored for the subloop to decide whether we may exit
611 through it. It is sufficient to do this for header of the loop,
612 as other basic blocks inside it must be dominated by it. */
613 if (body[i]->loop_father->header != body[i])
614 continue;
616 if (LOOP_DATA (body[i]->loop_father)->has_call)
618 has_call = true;
619 bitmap_set_bit (may_exit, i);
621 aexit = LOOP_DATA (body[i]->loop_father)->outermost_exit;
622 if (aexit != loop)
624 bitmap_set_bit (may_exit, i);
625 bitmap_set_bit (has_exit, i);
627 if (flow_loop_nested_p (aexit, outermost_exit))
628 outermost_exit = aexit;
632 if (loop->aux == NULL)
634 loop->aux = xcalloc (1, sizeof (struct loop_data));
635 bitmap_initialize (&LOOP_DATA (loop)->regs_ref, &reg_obstack);
636 bitmap_initialize (&LOOP_DATA (loop)->regs_live, &reg_obstack);
638 LOOP_DATA (loop)->outermost_exit = outermost_exit;
639 LOOP_DATA (loop)->has_call = has_call;
642 /* Check whether we may assign a value to X from a register. */
644 static bool
645 may_assign_reg_p (rtx x)
647 return (GET_MODE (x) != VOIDmode
648 && GET_MODE (x) != BLKmode
649 && can_copy_p (GET_MODE (x))
650 && (!REG_P (x)
651 || !HARD_REGISTER_P (x)
652 || REGNO_REG_CLASS (REGNO (x)) != NO_REGS));
655 /* Finds definitions that may correspond to invariants in LOOP with body
656 BODY. */
658 static void
659 find_defs (struct loop *loop)
661 if (dump_file)
663 fprintf (dump_file,
664 "*****starting processing of loop %d ******\n",
665 loop->num);
668 df_remove_problem (df_chain);
669 df_process_deferred_rescans ();
670 df_chain_add_problem (DF_UD_CHAIN);
671 df_live_add_problem ();
672 df_live_set_all_dirty ();
673 df_set_flags (DF_RD_PRUNE_DEAD_DEFS);
674 df_analyze_loop (loop);
675 check_invariant_table_size ();
677 if (dump_file)
679 df_dump_region (dump_file);
680 fprintf (dump_file,
681 "*****ending processing of loop %d ******\n",
682 loop->num);
686 /* Creates a new invariant for definition DEF in INSN, depending on invariants
687 in DEPENDS_ON. ALWAYS_EXECUTED is true if the insn is always executed,
688 unless the program ends due to a function call. The newly created invariant
689 is returned. */
691 static struct invariant *
692 create_new_invariant (struct def *def, rtx_insn *insn, bitmap depends_on,
693 bool always_executed)
695 struct invariant *inv = XNEW (struct invariant);
696 rtx set = single_set (insn);
697 bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn));
699 inv->def = def;
700 inv->always_executed = always_executed;
701 inv->depends_on = depends_on;
703 /* If the set is simple, usually by moving it we move the whole store out of
704 the loop. Otherwise we save only cost of the computation. */
705 if (def)
707 inv->cost = set_rtx_cost (set, speed);
708 /* ??? Try to determine cheapness of address computation. Unfortunately
709 the address cost is only a relative measure, we can't really compare
710 it with any absolute number, but only with other address costs.
711 But here we don't have any other addresses, so compare with a magic
712 number anyway. It has to be large enough to not regress PR33928
713 (by avoiding to move reg+8,reg+16,reg+24 invariants), but small
714 enough to not regress 410.bwaves either (by still moving reg+reg
715 invariants).
716 See http://gcc.gnu.org/ml/gcc-patches/2009-10/msg01210.html . */
717 if (SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set))))
718 inv->cheap_address = address_cost (SET_SRC (set), word_mode,
719 ADDR_SPACE_GENERIC, speed) < 3;
720 else
721 inv->cheap_address = false;
723 else
725 inv->cost = set_src_cost (SET_SRC (set), GET_MODE (SET_DEST (set)),
726 speed);
727 inv->cheap_address = false;
730 inv->move = false;
731 inv->reg = NULL_RTX;
732 inv->orig_regno = -1;
733 inv->stamp = 0;
734 inv->insn = insn;
736 inv->invno = invariants.length ();
737 inv->eqto = ~0u;
739 /* Itself. */
740 inv->eqno = 1;
742 if (def)
743 def->invno = inv->invno;
744 invariants.safe_push (inv);
746 if (dump_file)
748 fprintf (dump_file,
749 "Set in insn %d is invariant (%d), cost %d, depends on ",
750 INSN_UID (insn), inv->invno, inv->cost);
751 dump_bitmap (dump_file, inv->depends_on);
754 return inv;
757 /* Given invariant DEF and its address USE, check if the corresponding
758 invariant expr can be propagated into the use or not. */
760 static bool
761 inv_can_prop_to_addr_use (struct def *def, df_ref use)
763 struct invariant *inv;
764 rtx *pos = DF_REF_REAL_LOC (use), def_set;
765 rtx_insn *use_insn = DF_REF_INSN (use);
766 rtx_insn *def_insn;
767 bool ok;
769 inv = invariants[def->invno];
770 /* No need to check if address expression is expensive. */
771 if (!inv->cheap_address)
772 return false;
774 def_insn = inv->insn;
775 def_set = single_set (def_insn);
776 if (!def_set)
777 return false;
779 validate_unshare_change (use_insn, pos, SET_SRC (def_set), true);
780 ok = verify_changes (0);
781 cancel_changes (0);
782 return ok;
785 /* Record USE at DEF. */
787 static void
788 record_use (struct def *def, df_ref use)
790 struct use *u = XNEW (struct use);
792 u->pos = DF_REF_REAL_LOC (use);
793 u->insn = DF_REF_INSN (use);
794 u->addr_use_p = (DF_REF_TYPE (use) == DF_REF_REG_MEM_LOAD
795 || DF_REF_TYPE (use) == DF_REF_REG_MEM_STORE);
796 u->next = def->uses;
797 def->uses = u;
798 def->n_uses++;
799 if (u->addr_use_p)
801 /* Initialize propagation information if this is the first addr
802 use of the inv def. */
803 if (def->n_addr_uses == 0)
804 def->can_prop_to_addr_uses = true;
806 def->n_addr_uses++;
807 if (def->can_prop_to_addr_uses && !inv_can_prop_to_addr_use (def, use))
808 def->can_prop_to_addr_uses = false;
812 /* Finds the invariants USE depends on and store them to the DEPENDS_ON
813 bitmap. Returns true if all dependencies of USE are known to be
814 loop invariants, false otherwise. */
816 static bool
817 check_dependency (basic_block bb, df_ref use, bitmap depends_on)
819 df_ref def;
820 basic_block def_bb;
821 struct df_link *defs;
822 struct def *def_data;
823 struct invariant *inv;
825 if (DF_REF_FLAGS (use) & DF_REF_READ_WRITE)
826 return false;
828 defs = DF_REF_CHAIN (use);
829 if (!defs)
831 unsigned int regno = DF_REF_REGNO (use);
833 /* If this is the use of an uninitialized argument register that is
834 likely to be spilled, do not move it lest this might extend its
835 lifetime and cause reload to die. This can occur for a call to
836 a function taking complex number arguments and moving the insns
837 preparing the arguments without moving the call itself wouldn't
838 gain much in practice. */
839 if ((DF_REF_FLAGS (use) & DF_HARD_REG_LIVE)
840 && FUNCTION_ARG_REGNO_P (regno)
841 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno)))
842 return false;
844 return true;
847 if (defs->next)
848 return false;
850 def = defs->ref;
851 check_invariant_table_size ();
852 inv = invariant_table[DF_REF_ID (def)];
853 if (!inv)
854 return false;
856 def_data = inv->def;
857 gcc_assert (def_data != NULL);
859 def_bb = DF_REF_BB (def);
860 /* Note that in case bb == def_bb, we know that the definition
861 dominates insn, because def has invariant_table[DF_REF_ID(def)]
862 defined and we process the insns in the basic block bb
863 sequentially. */
864 if (!dominated_by_p (CDI_DOMINATORS, bb, def_bb))
865 return false;
867 bitmap_set_bit (depends_on, def_data->invno);
868 return true;
872 /* Finds the invariants INSN depends on and store them to the DEPENDS_ON
873 bitmap. Returns true if all dependencies of INSN are known to be
874 loop invariants, false otherwise. */
876 static bool
877 check_dependencies (rtx_insn *insn, bitmap depends_on)
879 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
880 df_ref use;
881 basic_block bb = BLOCK_FOR_INSN (insn);
883 FOR_EACH_INSN_INFO_USE (use, insn_info)
884 if (!check_dependency (bb, use, depends_on))
885 return false;
886 FOR_EACH_INSN_INFO_EQ_USE (use, insn_info)
887 if (!check_dependency (bb, use, depends_on))
888 return false;
890 return true;
893 /* Pre-check candidate DEST to skip the one which can not make a valid insn
894 during move_invariant_reg. SIMPLE is to skip HARD_REGISTER. */
895 static bool
896 pre_check_invariant_p (bool simple, rtx dest)
898 if (simple && REG_P (dest) && DF_REG_DEF_COUNT (REGNO (dest)) > 1)
900 df_ref use;
901 unsigned int i = REGNO (dest);
902 struct df_insn_info *insn_info;
903 df_ref def_rec;
905 for (use = DF_REG_USE_CHAIN (i); use; use = DF_REF_NEXT_REG (use))
907 rtx_insn *ref = DF_REF_INSN (use);
908 insn_info = DF_INSN_INFO_GET (ref);
910 FOR_EACH_INSN_INFO_DEF (def_rec, insn_info)
911 if (DF_REF_REGNO (def_rec) == i)
913 /* Multi definitions at this stage, most likely are due to
914 instruction constraints, which requires both read and write
915 on the same register. Since move_invariant_reg is not
916 powerful enough to handle such cases, just ignore the INV
917 and leave the chance to others. */
918 return false;
922 return true;
925 /* Finds invariant in INSN. ALWAYS_REACHED is true if the insn is always
926 executed. ALWAYS_EXECUTED is true if the insn is always executed,
927 unless the program ends due to a function call. */
929 static void
930 find_invariant_insn (rtx_insn *insn, bool always_reached, bool always_executed)
932 df_ref ref;
933 struct def *def;
934 bitmap depends_on;
935 rtx set, dest;
936 bool simple = true;
937 struct invariant *inv;
939 /* We can't move a CC0 setter without the user. */
940 if (HAVE_cc0 && sets_cc0_p (insn))
941 return;
943 set = single_set (insn);
944 if (!set)
945 return;
946 dest = SET_DEST (set);
948 if (!REG_P (dest)
949 || HARD_REGISTER_P (dest))
950 simple = false;
952 if (!may_assign_reg_p (dest)
953 || !pre_check_invariant_p (simple, dest)
954 || !check_maybe_invariant (SET_SRC (set)))
955 return;
957 /* If the insn can throw exception, we cannot move it at all without changing
958 cfg. */
959 if (can_throw_internal (insn))
960 return;
962 /* We cannot make trapping insn executed, unless it was executed before. */
963 if (may_trap_or_fault_p (PATTERN (insn)) && !always_reached)
964 return;
966 depends_on = BITMAP_ALLOC (NULL);
967 if (!check_dependencies (insn, depends_on))
969 BITMAP_FREE (depends_on);
970 return;
973 if (simple)
974 def = XCNEW (struct def);
975 else
976 def = NULL;
978 inv = create_new_invariant (def, insn, depends_on, always_executed);
980 if (simple)
982 ref = df_find_def (insn, dest);
983 check_invariant_table_size ();
984 invariant_table[DF_REF_ID (ref)] = inv;
988 /* Record registers used in INSN that have a unique invariant definition. */
990 static void
991 record_uses (rtx_insn *insn)
993 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
994 df_ref use;
995 struct invariant *inv;
997 FOR_EACH_INSN_INFO_USE (use, insn_info)
999 inv = invariant_for_use (use);
1000 if (inv)
1001 record_use (inv->def, use);
1003 FOR_EACH_INSN_INFO_EQ_USE (use, insn_info)
1005 inv = invariant_for_use (use);
1006 if (inv)
1007 record_use (inv->def, use);
1011 /* Finds invariants in INSN. ALWAYS_REACHED is true if the insn is always
1012 executed. ALWAYS_EXECUTED is true if the insn is always executed,
1013 unless the program ends due to a function call. */
1015 static void
1016 find_invariants_insn (rtx_insn *insn, bool always_reached, bool always_executed)
1018 find_invariant_insn (insn, always_reached, always_executed);
1019 record_uses (insn);
1022 /* Finds invariants in basic block BB. ALWAYS_REACHED is true if the
1023 basic block is always executed. ALWAYS_EXECUTED is true if the basic
1024 block is always executed, unless the program ends due to a function
1025 call. */
1027 static void
1028 find_invariants_bb (basic_block bb, bool always_reached, bool always_executed)
1030 rtx_insn *insn;
1032 FOR_BB_INSNS (bb, insn)
1034 if (!NONDEBUG_INSN_P (insn))
1035 continue;
1037 find_invariants_insn (insn, always_reached, always_executed);
1039 if (always_reached
1040 && CALL_P (insn)
1041 && (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)
1042 || ! RTL_CONST_OR_PURE_CALL_P (insn)))
1043 always_reached = false;
1047 /* Finds invariants in LOOP with body BODY. ALWAYS_REACHED is the bitmap of
1048 basic blocks in BODY that are always executed. ALWAYS_EXECUTED is the
1049 bitmap of basic blocks in BODY that are always executed unless the program
1050 ends due to a function call. */
1052 static void
1053 find_invariants_body (struct loop *loop, basic_block *body,
1054 bitmap always_reached, bitmap always_executed)
1056 unsigned i;
1058 for (i = 0; i < loop->num_nodes; i++)
1059 find_invariants_bb (body[i],
1060 bitmap_bit_p (always_reached, i),
1061 bitmap_bit_p (always_executed, i));
1064 /* Finds invariants in LOOP. */
1066 static void
1067 find_invariants (struct loop *loop)
1069 bitmap may_exit = BITMAP_ALLOC (NULL);
1070 bitmap always_reached = BITMAP_ALLOC (NULL);
1071 bitmap has_exit = BITMAP_ALLOC (NULL);
1072 bitmap always_executed = BITMAP_ALLOC (NULL);
1073 basic_block *body = get_loop_body_in_dom_order (loop);
1075 find_exits (loop, body, may_exit, has_exit);
1076 compute_always_reached (loop, body, may_exit, always_reached);
1077 compute_always_reached (loop, body, has_exit, always_executed);
1079 find_defs (loop);
1080 find_invariants_body (loop, body, always_reached, always_executed);
1081 merge_identical_invariants ();
1083 BITMAP_FREE (always_reached);
1084 BITMAP_FREE (always_executed);
1085 BITMAP_FREE (may_exit);
1086 BITMAP_FREE (has_exit);
1087 free (body);
1090 /* Frees a list of uses USE. */
1092 static void
1093 free_use_list (struct use *use)
1095 struct use *next;
1097 for (; use; use = next)
1099 next = use->next;
1100 free (use);
1104 /* Return pressure class and number of hard registers (through *NREGS)
1105 for destination of INSN. */
1106 static enum reg_class
1107 get_pressure_class_and_nregs (rtx_insn *insn, int *nregs)
1109 rtx reg;
1110 enum reg_class pressure_class;
1111 rtx set = single_set (insn);
1113 /* Considered invariant insns have only one set. */
1114 gcc_assert (set != NULL_RTX);
1115 reg = SET_DEST (set);
1116 if (GET_CODE (reg) == SUBREG)
1117 reg = SUBREG_REG (reg);
1118 if (MEM_P (reg))
1120 *nregs = 0;
1121 pressure_class = NO_REGS;
1123 else
1125 if (! REG_P (reg))
1126 reg = NULL_RTX;
1127 if (reg == NULL_RTX)
1128 pressure_class = GENERAL_REGS;
1129 else
1131 pressure_class = reg_allocno_class (REGNO (reg));
1132 pressure_class = ira_pressure_class_translate[pressure_class];
1134 *nregs
1135 = ira_reg_class_max_nregs[pressure_class][GET_MODE (SET_SRC (set))];
1137 return pressure_class;
1140 /* Calculates cost and number of registers needed for moving invariant INV
1141 out of the loop and stores them to *COST and *REGS_NEEDED. *CL will be
1142 the REG_CLASS of INV. Return
1143 -1: if INV is invalid.
1144 0: if INV and its depends_on have same reg_class
1145 1: if INV and its depends_on have different reg_classes. */
1147 static int
1148 get_inv_cost (struct invariant *inv, int *comp_cost, unsigned *regs_needed,
1149 enum reg_class *cl)
1151 int i, acomp_cost;
1152 unsigned aregs_needed[N_REG_CLASSES];
1153 unsigned depno;
1154 struct invariant *dep;
1155 bitmap_iterator bi;
1156 int ret = 1;
1158 /* Find the representative of the class of the equivalent invariants. */
1159 inv = invariants[inv->eqto];
1161 *comp_cost = 0;
1162 if (! flag_ira_loop_pressure)
1163 regs_needed[0] = 0;
1164 else
1166 for (i = 0; i < ira_pressure_classes_num; i++)
1167 regs_needed[ira_pressure_classes[i]] = 0;
1170 if (inv->move
1171 || inv->stamp == actual_stamp)
1172 return -1;
1173 inv->stamp = actual_stamp;
1175 if (! flag_ira_loop_pressure)
1176 regs_needed[0]++;
1177 else
1179 int nregs;
1180 enum reg_class pressure_class;
1182 pressure_class = get_pressure_class_and_nregs (inv->insn, &nregs);
1183 regs_needed[pressure_class] += nregs;
1184 *cl = pressure_class;
1185 ret = 0;
1188 if (!inv->cheap_address
1189 || inv->def->n_uses == 0
1190 || inv->def->n_addr_uses < inv->def->n_uses
1191 /* Count cost if the inv can't be propagated into address uses. */
1192 || !inv->def->can_prop_to_addr_uses)
1193 (*comp_cost) += inv->cost * inv->eqno;
1195 #ifdef STACK_REGS
1197 /* Hoisting constant pool constants into stack regs may cost more than
1198 just single register. On x87, the balance is affected both by the
1199 small number of FP registers, and by its register stack organization,
1200 that forces us to add compensation code in and around the loop to
1201 shuffle the operands to the top of stack before use, and pop them
1202 from the stack after the loop finishes.
1204 To model this effect, we increase the number of registers needed for
1205 stack registers by two: one register push, and one register pop.
1206 This usually has the effect that FP constant loads from the constant
1207 pool are not moved out of the loop.
1209 Note that this also means that dependent invariants can not be moved.
1210 However, the primary purpose of this pass is to move loop invariant
1211 address arithmetic out of loops, and address arithmetic that depends
1212 on floating point constants is unlikely to ever occur. */
1213 rtx set = single_set (inv->insn);
1214 if (set
1215 && IS_STACK_MODE (GET_MODE (SET_SRC (set)))
1216 && constant_pool_constant_p (SET_SRC (set)))
1218 if (flag_ira_loop_pressure)
1219 regs_needed[ira_stack_reg_pressure_class] += 2;
1220 else
1221 regs_needed[0] += 2;
1224 #endif
1226 EXECUTE_IF_SET_IN_BITMAP (inv->depends_on, 0, depno, bi)
1228 bool check_p;
1229 enum reg_class dep_cl = ALL_REGS;
1230 int dep_ret;
1232 dep = invariants[depno];
1234 /* If DEP is moved out of the loop, it is not a depends_on any more. */
1235 if (dep->move)
1236 continue;
1238 dep_ret = get_inv_cost (dep, &acomp_cost, aregs_needed, &dep_cl);
1240 if (! flag_ira_loop_pressure)
1241 check_p = aregs_needed[0] != 0;
1242 else
1244 for (i = 0; i < ira_pressure_classes_num; i++)
1245 if (aregs_needed[ira_pressure_classes[i]] != 0)
1246 break;
1247 check_p = i < ira_pressure_classes_num;
1249 if ((dep_ret == 1) || ((dep_ret == 0) && (*cl != dep_cl)))
1251 *cl = ALL_REGS;
1252 ret = 1;
1255 if (check_p
1256 /* We need to check always_executed, since if the original value of
1257 the invariant may be preserved, we may need to keep it in a
1258 separate register. TODO check whether the register has an
1259 use outside of the loop. */
1260 && dep->always_executed
1261 && !dep->def->uses->next)
1263 /* If this is a single use, after moving the dependency we will not
1264 need a new register. */
1265 if (! flag_ira_loop_pressure)
1266 aregs_needed[0]--;
1267 else
1269 int nregs;
1270 enum reg_class pressure_class;
1272 pressure_class = get_pressure_class_and_nregs (inv->insn, &nregs);
1273 aregs_needed[pressure_class] -= nregs;
1277 if (! flag_ira_loop_pressure)
1278 regs_needed[0] += aregs_needed[0];
1279 else
1281 for (i = 0; i < ira_pressure_classes_num; i++)
1282 regs_needed[ira_pressure_classes[i]]
1283 += aregs_needed[ira_pressure_classes[i]];
1285 (*comp_cost) += acomp_cost;
1287 return ret;
1290 /* Calculates gain for eliminating invariant INV. REGS_USED is the number
1291 of registers used in the loop, NEW_REGS is the number of new variables
1292 already added due to the invariant motion. The number of registers needed
1293 for it is stored in *REGS_NEEDED. SPEED and CALL_P are flags passed
1294 through to estimate_reg_pressure_cost. */
1296 static int
1297 gain_for_invariant (struct invariant *inv, unsigned *regs_needed,
1298 unsigned *new_regs, unsigned regs_used,
1299 bool speed, bool call_p)
1301 int comp_cost, size_cost;
1302 /* Workaround -Wmaybe-uninitialized false positive during
1303 profiledbootstrap by initializing it. */
1304 enum reg_class cl = NO_REGS;
1305 int ret;
1307 actual_stamp++;
1309 ret = get_inv_cost (inv, &comp_cost, regs_needed, &cl);
1311 if (! flag_ira_loop_pressure)
1313 size_cost = (estimate_reg_pressure_cost (new_regs[0] + regs_needed[0],
1314 regs_used, speed, call_p)
1315 - estimate_reg_pressure_cost (new_regs[0],
1316 regs_used, speed, call_p));
1318 else if (ret < 0)
1319 return -1;
1320 else if ((ret == 0) && (cl == NO_REGS))
1321 /* Hoist it anyway since it does not impact register pressure. */
1322 return 1;
1323 else
1325 int i;
1326 enum reg_class pressure_class;
1328 for (i = 0; i < ira_pressure_classes_num; i++)
1330 pressure_class = ira_pressure_classes[i];
1332 if (!reg_classes_intersect_p (pressure_class, cl))
1333 continue;
1335 if ((int) new_regs[pressure_class]
1336 + (int) regs_needed[pressure_class]
1337 + LOOP_DATA (curr_loop)->max_reg_pressure[pressure_class]
1338 + IRA_LOOP_RESERVED_REGS
1339 > ira_class_hard_regs_num[pressure_class])
1340 break;
1342 if (i < ira_pressure_classes_num)
1343 /* There will be register pressure excess and we want not to
1344 make this loop invariant motion. All loop invariants with
1345 non-positive gains will be rejected in function
1346 find_invariants_to_move. Therefore we return the negative
1347 number here.
1349 One could think that this rejects also expensive loop
1350 invariant motions and this will hurt code performance.
1351 However numerous experiments with different heuristics
1352 taking invariant cost into account did not confirm this
1353 assumption. There are possible explanations for this
1354 result:
1355 o probably all expensive invariants were already moved out
1356 of the loop by PRE and gimple invariant motion pass.
1357 o expensive invariant execution will be hidden by insn
1358 scheduling or OOO processor hardware because usually such
1359 invariants have a lot of freedom to be executed
1360 out-of-order.
1361 Another reason for ignoring invariant cost vs spilling cost
1362 heuristics is also in difficulties to evaluate accurately
1363 spill cost at this stage. */
1364 return -1;
1365 else
1366 size_cost = 0;
1369 return comp_cost - size_cost;
1372 /* Finds invariant with best gain for moving. Returns the gain, stores
1373 the invariant in *BEST and number of registers needed for it to
1374 *REGS_NEEDED. REGS_USED is the number of registers used in the loop.
1375 NEW_REGS is the number of new variables already added due to invariant
1376 motion. */
1378 static int
1379 best_gain_for_invariant (struct invariant **best, unsigned *regs_needed,
1380 unsigned *new_regs, unsigned regs_used,
1381 bool speed, bool call_p)
1383 struct invariant *inv;
1384 int i, gain = 0, again;
1385 unsigned aregs_needed[N_REG_CLASSES], invno;
1387 FOR_EACH_VEC_ELT (invariants, invno, inv)
1389 if (inv->move)
1390 continue;
1392 /* Only consider the "representatives" of equivalent invariants. */
1393 if (inv->eqto != inv->invno)
1394 continue;
1396 again = gain_for_invariant (inv, aregs_needed, new_regs, regs_used,
1397 speed, call_p);
1398 if (again > gain)
1400 gain = again;
1401 *best = inv;
1402 if (! flag_ira_loop_pressure)
1403 regs_needed[0] = aregs_needed[0];
1404 else
1406 for (i = 0; i < ira_pressure_classes_num; i++)
1407 regs_needed[ira_pressure_classes[i]]
1408 = aregs_needed[ira_pressure_classes[i]];
1413 return gain;
1416 /* Marks invariant INVNO and all its dependencies for moving. */
1418 static void
1419 set_move_mark (unsigned invno, int gain)
1421 struct invariant *inv = invariants[invno];
1422 bitmap_iterator bi;
1424 /* Find the representative of the class of the equivalent invariants. */
1425 inv = invariants[inv->eqto];
1427 if (inv->move)
1428 return;
1429 inv->move = true;
1431 if (dump_file)
1433 if (gain >= 0)
1434 fprintf (dump_file, "Decided to move invariant %d -- gain %d\n",
1435 invno, gain);
1436 else
1437 fprintf (dump_file, "Decided to move dependent invariant %d\n",
1438 invno);
1441 EXECUTE_IF_SET_IN_BITMAP (inv->depends_on, 0, invno, bi)
1443 set_move_mark (invno, -1);
1447 /* Determines which invariants to move. */
1449 static void
1450 find_invariants_to_move (bool speed, bool call_p)
1452 int gain;
1453 unsigned i, regs_used, regs_needed[N_REG_CLASSES], new_regs[N_REG_CLASSES];
1454 struct invariant *inv = NULL;
1456 if (!invariants.length ())
1457 return;
1459 if (flag_ira_loop_pressure)
1460 /* REGS_USED is actually never used when the flag is on. */
1461 regs_used = 0;
1462 else
1463 /* We do not really do a good job in estimating number of
1464 registers used; we put some initial bound here to stand for
1465 induction variables etc. that we do not detect. */
1467 unsigned int n_regs = DF_REG_SIZE (df);
1469 regs_used = 2;
1471 for (i = 0; i < n_regs; i++)
1473 if (!DF_REGNO_FIRST_DEF (i) && DF_REGNO_LAST_USE (i))
1475 /* This is a value that is used but not changed inside loop. */
1476 regs_used++;
1481 if (! flag_ira_loop_pressure)
1482 new_regs[0] = regs_needed[0] = 0;
1483 else
1485 for (i = 0; (int) i < ira_pressure_classes_num; i++)
1486 new_regs[ira_pressure_classes[i]] = 0;
1488 while ((gain = best_gain_for_invariant (&inv, regs_needed,
1489 new_regs, regs_used,
1490 speed, call_p)) > 0)
1492 set_move_mark (inv->invno, gain);
1493 if (! flag_ira_loop_pressure)
1494 new_regs[0] += regs_needed[0];
1495 else
1497 for (i = 0; (int) i < ira_pressure_classes_num; i++)
1498 new_regs[ira_pressure_classes[i]]
1499 += regs_needed[ira_pressure_classes[i]];
1504 /* Replace the uses, reached by the definition of invariant INV, by REG.
1506 IN_GROUP is nonzero if this is part of a group of changes that must be
1507 performed as a group. In that case, the changes will be stored. The
1508 function `apply_change_group' will validate and apply the changes. */
1510 static int
1511 replace_uses (struct invariant *inv, rtx reg, bool in_group)
1513 /* Replace the uses we know to be dominated. It saves work for copy
1514 propagation, and also it is necessary so that dependent invariants
1515 are computed right. */
1516 if (inv->def)
1518 struct use *use;
1519 for (use = inv->def->uses; use; use = use->next)
1520 validate_change (use->insn, use->pos, reg, true);
1522 /* If we aren't part of a larger group, apply the changes now. */
1523 if (!in_group)
1524 return apply_change_group ();
1527 return 1;
1530 /* Whether invariant INV setting REG can be moved out of LOOP, at the end of
1531 the block preceding its header. */
1533 static bool
1534 can_move_invariant_reg (struct loop *loop, struct invariant *inv, rtx reg)
1536 df_ref def, use;
1537 unsigned int dest_regno, defs_in_loop_count = 0;
1538 rtx_insn *insn = inv->insn;
1539 basic_block bb = BLOCK_FOR_INSN (inv->insn);
1541 /* We ignore hard register and memory access for cost and complexity reasons.
1542 Hard register are few at this stage and expensive to consider as they
1543 require building a separate data flow. Memory access would require using
1544 df_simulate_* and can_move_insns_across functions and is more complex. */
1545 if (!REG_P (reg) || HARD_REGISTER_P (reg))
1546 return false;
1548 /* Check whether the set is always executed. We could omit this condition if
1549 we know that the register is unused outside of the loop, but it does not
1550 seem worth finding out. */
1551 if (!inv->always_executed)
1552 return false;
1554 /* Check that all uses that would be dominated by def are already dominated
1555 by it. */
1556 dest_regno = REGNO (reg);
1557 for (use = DF_REG_USE_CHAIN (dest_regno); use; use = DF_REF_NEXT_REG (use))
1559 rtx_insn *use_insn;
1560 basic_block use_bb;
1562 use_insn = DF_REF_INSN (use);
1563 use_bb = BLOCK_FOR_INSN (use_insn);
1565 /* Ignore instruction considered for moving. */
1566 if (use_insn == insn)
1567 continue;
1569 /* Don't consider uses outside loop. */
1570 if (!flow_bb_inside_loop_p (loop, use_bb))
1571 continue;
1573 /* Don't move if a use is not dominated by def in insn. */
1574 if (use_bb == bb && DF_INSN_LUID (insn) >= DF_INSN_LUID (use_insn))
1575 return false;
1576 if (!dominated_by_p (CDI_DOMINATORS, use_bb, bb))
1577 return false;
1580 /* Check for other defs. Any other def in the loop might reach a use
1581 currently reached by the def in insn. */
1582 for (def = DF_REG_DEF_CHAIN (dest_regno); def; def = DF_REF_NEXT_REG (def))
1584 basic_block def_bb = DF_REF_BB (def);
1586 /* Defs in exit block cannot reach a use they weren't already. */
1587 if (single_succ_p (def_bb))
1589 basic_block def_bb_succ;
1591 def_bb_succ = single_succ (def_bb);
1592 if (!flow_bb_inside_loop_p (loop, def_bb_succ))
1593 continue;
1596 if (++defs_in_loop_count > 1)
1597 return false;
1600 return true;
1603 /* Move invariant INVNO out of the LOOP. Returns true if this succeeds, false
1604 otherwise. */
1606 static bool
1607 move_invariant_reg (struct loop *loop, unsigned invno)
1609 struct invariant *inv = invariants[invno];
1610 struct invariant *repr = invariants[inv->eqto];
1611 unsigned i;
1612 basic_block preheader = loop_preheader_edge (loop)->src;
1613 rtx reg, set, dest, note;
1614 bitmap_iterator bi;
1615 int regno = -1;
1617 if (inv->reg)
1618 return true;
1619 if (!repr->move)
1620 return false;
1622 /* If this is a representative of the class of equivalent invariants,
1623 really move the invariant. Otherwise just replace its use with
1624 the register used for the representative. */
1625 if (inv == repr)
1627 if (inv->depends_on)
1629 EXECUTE_IF_SET_IN_BITMAP (inv->depends_on, 0, i, bi)
1631 if (!move_invariant_reg (loop, i))
1632 goto fail;
1636 /* If possible, just move the set out of the loop. Otherwise, we
1637 need to create a temporary register. */
1638 set = single_set (inv->insn);
1639 reg = dest = SET_DEST (set);
1640 if (GET_CODE (reg) == SUBREG)
1641 reg = SUBREG_REG (reg);
1642 if (REG_P (reg))
1643 regno = REGNO (reg);
1645 if (!can_move_invariant_reg (loop, inv, dest))
1647 reg = gen_reg_rtx_and_attrs (dest);
1649 /* Try replacing the destination by a new pseudoregister. */
1650 validate_change (inv->insn, &SET_DEST (set), reg, true);
1652 /* As well as all the dominated uses. */
1653 replace_uses (inv, reg, true);
1655 /* And validate all the changes. */
1656 if (!apply_change_group ())
1657 goto fail;
1659 emit_insn_after (gen_move_insn (dest, reg), inv->insn);
1661 else if (dump_file)
1662 fprintf (dump_file, "Invariant %d moved without introducing a new "
1663 "temporary register\n", invno);
1664 reorder_insns (inv->insn, inv->insn, BB_END (preheader));
1665 df_recompute_luids (preheader);
1667 /* If there is a REG_EQUAL note on the insn we just moved, and the
1668 insn is in a basic block that is not always executed or the note
1669 contains something for which we don't know the invariant status,
1670 the note may no longer be valid after we move the insn. Note that
1671 uses in REG_EQUAL notes are taken into account in the computation
1672 of invariants, so it is safe to retain the note even if it contains
1673 register references for which we know the invariant status. */
1674 if ((note = find_reg_note (inv->insn, REG_EQUAL, NULL_RTX))
1675 && (!inv->always_executed
1676 || !check_maybe_invariant (XEXP (note, 0))))
1677 remove_note (inv->insn, note);
1679 else
1681 if (!move_invariant_reg (loop, repr->invno))
1682 goto fail;
1683 reg = repr->reg;
1684 regno = repr->orig_regno;
1685 if (!replace_uses (inv, reg, false))
1686 goto fail;
1687 set = single_set (inv->insn);
1688 emit_insn_after (gen_move_insn (SET_DEST (set), reg), inv->insn);
1689 delete_insn (inv->insn);
1692 inv->reg = reg;
1693 inv->orig_regno = regno;
1695 return true;
1697 fail:
1698 /* If we failed, clear move flag, so that we do not try to move inv
1699 again. */
1700 if (dump_file)
1701 fprintf (dump_file, "Failed to move invariant %d\n", invno);
1702 inv->move = false;
1703 inv->reg = NULL_RTX;
1704 inv->orig_regno = -1;
1706 return false;
1709 /* Move selected invariant out of the LOOP. Newly created regs are marked
1710 in TEMPORARY_REGS. */
1712 static void
1713 move_invariants (struct loop *loop)
1715 struct invariant *inv;
1716 unsigned i;
1718 FOR_EACH_VEC_ELT (invariants, i, inv)
1719 move_invariant_reg (loop, i);
1720 if (flag_ira_loop_pressure && resize_reg_info ())
1722 FOR_EACH_VEC_ELT (invariants, i, inv)
1723 if (inv->reg != NULL_RTX)
1725 if (inv->orig_regno >= 0)
1726 setup_reg_classes (REGNO (inv->reg),
1727 reg_preferred_class (inv->orig_regno),
1728 reg_alternate_class (inv->orig_regno),
1729 reg_allocno_class (inv->orig_regno));
1730 else
1731 setup_reg_classes (REGNO (inv->reg),
1732 GENERAL_REGS, NO_REGS, GENERAL_REGS);
1737 /* Initializes invariant motion data. */
1739 static void
1740 init_inv_motion_data (void)
1742 actual_stamp = 1;
1744 invariants.create (100);
1747 /* Frees the data allocated by invariant motion. */
1749 static void
1750 free_inv_motion_data (void)
1752 unsigned i;
1753 struct def *def;
1754 struct invariant *inv;
1756 check_invariant_table_size ();
1757 for (i = 0; i < DF_DEFS_TABLE_SIZE (); i++)
1759 inv = invariant_table[i];
1760 if (inv)
1762 def = inv->def;
1763 gcc_assert (def != NULL);
1765 free_use_list (def->uses);
1766 free (def);
1767 invariant_table[i] = NULL;
1771 FOR_EACH_VEC_ELT (invariants, i, inv)
1773 BITMAP_FREE (inv->depends_on);
1774 free (inv);
1776 invariants.release ();
1779 /* Move the invariants out of the LOOP. */
1781 static void
1782 move_single_loop_invariants (struct loop *loop)
1784 init_inv_motion_data ();
1786 find_invariants (loop);
1787 find_invariants_to_move (optimize_loop_for_speed_p (loop),
1788 LOOP_DATA (loop)->has_call);
1789 move_invariants (loop);
1791 free_inv_motion_data ();
1794 /* Releases the auxiliary data for LOOP. */
1796 static void
1797 free_loop_data (struct loop *loop)
1799 struct loop_data *data = LOOP_DATA (loop);
1800 if (!data)
1801 return;
1803 bitmap_clear (&LOOP_DATA (loop)->regs_ref);
1804 bitmap_clear (&LOOP_DATA (loop)->regs_live);
1805 free (data);
1806 loop->aux = NULL;
1811 /* Registers currently living. */
1812 static bitmap_head curr_regs_live;
1814 /* Current reg pressure for each pressure class. */
1815 static int curr_reg_pressure[N_REG_CLASSES];
1817 /* Record all regs that are set in any one insn. Communication from
1818 mark_reg_{store,clobber} and global_conflicts. Asm can refer to
1819 all hard-registers. */
1820 static rtx regs_set[(FIRST_PSEUDO_REGISTER > MAX_RECOG_OPERANDS
1821 ? FIRST_PSEUDO_REGISTER : MAX_RECOG_OPERANDS) * 2];
1822 /* Number of regs stored in the previous array. */
1823 static int n_regs_set;
1825 /* Return pressure class and number of needed hard registers (through
1826 *NREGS) of register REGNO. */
1827 static enum reg_class
1828 get_regno_pressure_class (int regno, int *nregs)
1830 if (regno >= FIRST_PSEUDO_REGISTER)
1832 enum reg_class pressure_class;
1834 pressure_class = reg_allocno_class (regno);
1835 pressure_class = ira_pressure_class_translate[pressure_class];
1836 *nregs
1837 = ira_reg_class_max_nregs[pressure_class][PSEUDO_REGNO_MODE (regno)];
1838 return pressure_class;
1840 else if (! TEST_HARD_REG_BIT (ira_no_alloc_regs, regno)
1841 && ! TEST_HARD_REG_BIT (eliminable_regset, regno))
1843 *nregs = 1;
1844 return ira_pressure_class_translate[REGNO_REG_CLASS (regno)];
1846 else
1848 *nregs = 0;
1849 return NO_REGS;
1853 /* Increase (if INCR_P) or decrease current register pressure for
1854 register REGNO. */
1855 static void
1856 change_pressure (int regno, bool incr_p)
1858 int nregs;
1859 enum reg_class pressure_class;
1861 pressure_class = get_regno_pressure_class (regno, &nregs);
1862 if (! incr_p)
1863 curr_reg_pressure[pressure_class] -= nregs;
1864 else
1866 curr_reg_pressure[pressure_class] += nregs;
1867 if (LOOP_DATA (curr_loop)->max_reg_pressure[pressure_class]
1868 < curr_reg_pressure[pressure_class])
1869 LOOP_DATA (curr_loop)->max_reg_pressure[pressure_class]
1870 = curr_reg_pressure[pressure_class];
1874 /* Mark REGNO birth. */
1875 static void
1876 mark_regno_live (int regno)
1878 struct loop *loop;
1880 for (loop = curr_loop;
1881 loop != current_loops->tree_root;
1882 loop = loop_outer (loop))
1883 bitmap_set_bit (&LOOP_DATA (loop)->regs_live, regno);
1884 if (!bitmap_set_bit (&curr_regs_live, regno))
1885 return;
1886 change_pressure (regno, true);
1889 /* Mark REGNO death. */
1890 static void
1891 mark_regno_death (int regno)
1893 if (! bitmap_clear_bit (&curr_regs_live, regno))
1894 return;
1895 change_pressure (regno, false);
1898 /* Mark setting register REG. */
1899 static void
1900 mark_reg_store (rtx reg, const_rtx setter ATTRIBUTE_UNUSED,
1901 void *data ATTRIBUTE_UNUSED)
1903 if (GET_CODE (reg) == SUBREG)
1904 reg = SUBREG_REG (reg);
1906 if (! REG_P (reg))
1907 return;
1909 regs_set[n_regs_set++] = reg;
1911 unsigned int end_regno = END_REGNO (reg);
1912 for (unsigned int regno = REGNO (reg); regno < end_regno; ++regno)
1913 mark_regno_live (regno);
1916 /* Mark clobbering register REG. */
1917 static void
1918 mark_reg_clobber (rtx reg, const_rtx setter, void *data)
1920 if (GET_CODE (setter) == CLOBBER)
1921 mark_reg_store (reg, setter, data);
1924 /* Mark register REG death. */
1925 static void
1926 mark_reg_death (rtx reg)
1928 unsigned int end_regno = END_REGNO (reg);
1929 for (unsigned int regno = REGNO (reg); regno < end_regno; ++regno)
1930 mark_regno_death (regno);
1933 /* Mark occurrence of registers in X for the current loop. */
1934 static void
1935 mark_ref_regs (rtx x)
1937 RTX_CODE code;
1938 int i;
1939 const char *fmt;
1941 if (!x)
1942 return;
1944 code = GET_CODE (x);
1945 if (code == REG)
1947 struct loop *loop;
1949 for (loop = curr_loop;
1950 loop != current_loops->tree_root;
1951 loop = loop_outer (loop))
1952 bitmap_set_bit (&LOOP_DATA (loop)->regs_ref, REGNO (x));
1953 return;
1956 fmt = GET_RTX_FORMAT (code);
1957 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1958 if (fmt[i] == 'e')
1959 mark_ref_regs (XEXP (x, i));
1960 else if (fmt[i] == 'E')
1962 int j;
1964 for (j = 0; j < XVECLEN (x, i); j++)
1965 mark_ref_regs (XVECEXP (x, i, j));
1969 /* Calculate register pressure in the loops. */
1970 static void
1971 calculate_loop_reg_pressure (void)
1973 int i;
1974 unsigned int j;
1975 bitmap_iterator bi;
1976 basic_block bb;
1977 rtx_insn *insn;
1978 rtx link;
1979 struct loop *loop, *parent;
1981 FOR_EACH_LOOP (loop, 0)
1982 if (loop->aux == NULL)
1984 loop->aux = xcalloc (1, sizeof (struct loop_data));
1985 bitmap_initialize (&LOOP_DATA (loop)->regs_ref, &reg_obstack);
1986 bitmap_initialize (&LOOP_DATA (loop)->regs_live, &reg_obstack);
1988 ira_setup_eliminable_regset ();
1989 bitmap_initialize (&curr_regs_live, &reg_obstack);
1990 FOR_EACH_BB_FN (bb, cfun)
1992 curr_loop = bb->loop_father;
1993 if (curr_loop == current_loops->tree_root)
1994 continue;
1996 for (loop = curr_loop;
1997 loop != current_loops->tree_root;
1998 loop = loop_outer (loop))
1999 bitmap_ior_into (&LOOP_DATA (loop)->regs_live, DF_LR_IN (bb));
2001 bitmap_copy (&curr_regs_live, DF_LR_IN (bb));
2002 for (i = 0; i < ira_pressure_classes_num; i++)
2003 curr_reg_pressure[ira_pressure_classes[i]] = 0;
2004 EXECUTE_IF_SET_IN_BITMAP (&curr_regs_live, 0, j, bi)
2005 change_pressure (j, true);
2007 FOR_BB_INSNS (bb, insn)
2009 if (! NONDEBUG_INSN_P (insn))
2010 continue;
2012 mark_ref_regs (PATTERN (insn));
2013 n_regs_set = 0;
2014 note_stores (PATTERN (insn), mark_reg_clobber, NULL);
2016 /* Mark any registers dead after INSN as dead now. */
2018 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2019 if (REG_NOTE_KIND (link) == REG_DEAD)
2020 mark_reg_death (XEXP (link, 0));
2022 /* Mark any registers set in INSN as live,
2023 and mark them as conflicting with all other live regs.
2024 Clobbers are processed again, so they conflict with
2025 the registers that are set. */
2027 note_stores (PATTERN (insn), mark_reg_store, NULL);
2029 if (AUTO_INC_DEC)
2030 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2031 if (REG_NOTE_KIND (link) == REG_INC)
2032 mark_reg_store (XEXP (link, 0), NULL_RTX, NULL);
2034 while (n_regs_set-- > 0)
2036 rtx note = find_regno_note (insn, REG_UNUSED,
2037 REGNO (regs_set[n_regs_set]));
2038 if (! note)
2039 continue;
2041 mark_reg_death (XEXP (note, 0));
2045 bitmap_clear (&curr_regs_live);
2046 if (flag_ira_region == IRA_REGION_MIXED
2047 || flag_ira_region == IRA_REGION_ALL)
2048 FOR_EACH_LOOP (loop, 0)
2050 EXECUTE_IF_SET_IN_BITMAP (&LOOP_DATA (loop)->regs_live, 0, j, bi)
2051 if (! bitmap_bit_p (&LOOP_DATA (loop)->regs_ref, j))
2053 enum reg_class pressure_class;
2054 int nregs;
2056 pressure_class = get_regno_pressure_class (j, &nregs);
2057 LOOP_DATA (loop)->max_reg_pressure[pressure_class] -= nregs;
2060 if (dump_file == NULL)
2061 return;
2062 FOR_EACH_LOOP (loop, 0)
2064 parent = loop_outer (loop);
2065 fprintf (dump_file, "\n Loop %d (parent %d, header bb%d, depth %d)\n",
2066 loop->num, (parent == NULL ? -1 : parent->num),
2067 loop->header->index, loop_depth (loop));
2068 fprintf (dump_file, "\n ref. regnos:");
2069 EXECUTE_IF_SET_IN_BITMAP (&LOOP_DATA (loop)->regs_ref, 0, j, bi)
2070 fprintf (dump_file, " %d", j);
2071 fprintf (dump_file, "\n live regnos:");
2072 EXECUTE_IF_SET_IN_BITMAP (&LOOP_DATA (loop)->regs_live, 0, j, bi)
2073 fprintf (dump_file, " %d", j);
2074 fprintf (dump_file, "\n Pressure:");
2075 for (i = 0; (int) i < ira_pressure_classes_num; i++)
2077 enum reg_class pressure_class;
2079 pressure_class = ira_pressure_classes[i];
2080 if (LOOP_DATA (loop)->max_reg_pressure[pressure_class] == 0)
2081 continue;
2082 fprintf (dump_file, " %s=%d", reg_class_names[pressure_class],
2083 LOOP_DATA (loop)->max_reg_pressure[pressure_class]);
2085 fprintf (dump_file, "\n");
2091 /* Move the invariants out of the loops. */
2093 void
2094 move_loop_invariants (void)
2096 struct loop *loop;
2098 if (flag_ira_loop_pressure)
2100 df_analyze ();
2101 regstat_init_n_sets_and_refs ();
2102 ira_set_pseudo_classes (true, dump_file);
2103 calculate_loop_reg_pressure ();
2104 regstat_free_n_sets_and_refs ();
2106 df_set_flags (DF_EQ_NOTES + DF_DEFER_INSN_RESCAN);
2107 /* Process the loops, innermost first. */
2108 FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
2110 curr_loop = loop;
2111 /* move_single_loop_invariants for very large loops
2112 is time consuming and might need a lot of memory. */
2113 if (loop->num_nodes <= (unsigned) LOOP_INVARIANT_MAX_BBS_IN_LOOP)
2114 move_single_loop_invariants (loop);
2117 FOR_EACH_LOOP (loop, 0)
2119 free_loop_data (loop);
2122 if (flag_ira_loop_pressure)
2123 /* There is no sense to keep this info because it was most
2124 probably outdated by subsequent passes. */
2125 free_reg_info ();
2126 free (invariant_table);
2127 invariant_table = NULL;
2128 invariant_table_size = 0;
2130 checking_verify_flow_info ();