except.c: Use rtx_sequence
[official-gcc.git] / gcc / loop-invariant.c
blob1ac2ebdf86c9a4357fedcdf38fffbfa79966c238
1 /* RTL-level loop invariant motion.
2 Copyright (C) 2004-2014 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 "tm.h"
41 #include "hard-reg-set.h"
42 #include "rtl.h"
43 #include "tm_p.h"
44 #include "obstack.h"
45 #include "basic-block.h"
46 #include "cfgloop.h"
47 #include "expr.h"
48 #include "recog.h"
49 #include "target.h"
50 #include "function.h"
51 #include "flags.h"
52 #include "df.h"
53 #include "hash-table.h"
54 #include "except.h"
55 #include "params.h"
56 #include "regs.h"
57 #include "ira.h"
58 #include "dumpfile.h"
60 /* The data stored for the loop. */
62 struct loop_data
64 struct loop *outermost_exit; /* The outermost exit of the loop. */
65 bool has_call; /* True if the loop contains a call. */
66 /* Maximal register pressure inside loop for given register class
67 (defined only for the pressure classes). */
68 int max_reg_pressure[N_REG_CLASSES];
69 /* Loop regs referenced and live pseudo-registers. */
70 bitmap_head regs_ref;
71 bitmap_head regs_live;
74 #define LOOP_DATA(LOOP) ((struct loop_data *) (LOOP)->aux)
76 /* The description of an use. */
78 struct use
80 rtx *pos; /* Position of the use. */
81 rtx_insn *insn; /* The insn in that the use occurs. */
82 unsigned addr_use_p; /* Whether the use occurs in an address. */
83 struct use *next; /* Next use in the list. */
86 /* The description of a def. */
88 struct def
90 struct use *uses; /* The list of uses that are uniquely reached
91 by it. */
92 unsigned n_uses; /* Number of such uses. */
93 unsigned n_addr_uses; /* Number of uses in addresses. */
94 unsigned invno; /* The corresponding invariant. */
97 /* The data stored for each invariant. */
99 struct invariant
101 /* The number of the invariant. */
102 unsigned invno;
104 /* The number of the invariant with the same value. */
105 unsigned eqto;
107 /* The number of invariants which eqto this. */
108 unsigned eqno;
110 /* If we moved the invariant out of the loop, the register that contains its
111 value. */
112 rtx reg;
114 /* If we moved the invariant out of the loop, the original regno
115 that contained its value. */
116 int orig_regno;
118 /* The definition of the invariant. */
119 struct def *def;
121 /* The insn in that it is defined. */
122 rtx_insn *insn;
124 /* Whether it is always executed. */
125 bool always_executed;
127 /* Whether to move the invariant. */
128 bool move;
130 /* Whether the invariant is cheap when used as an address. */
131 bool cheap_address;
133 /* Cost of the invariant. */
134 unsigned cost;
136 /* The invariants it depends on. */
137 bitmap depends_on;
139 /* Used for detecting already visited invariants during determining
140 costs of movements. */
141 unsigned stamp;
144 /* Currently processed loop. */
145 static struct loop *curr_loop;
147 /* Table of invariants indexed by the df_ref uid field. */
149 static unsigned int invariant_table_size = 0;
150 static struct invariant ** invariant_table;
152 /* Entry for hash table of invariant expressions. */
154 struct invariant_expr_entry
156 /* The invariant. */
157 struct invariant *inv;
159 /* Its value. */
160 rtx expr;
162 /* Its mode. */
163 enum machine_mode mode;
165 /* Its hash. */
166 hashval_t hash;
169 /* The actual stamp for marking already visited invariants during determining
170 costs of movements. */
172 static unsigned actual_stamp;
174 typedef struct invariant *invariant_p;
177 /* The invariants. */
179 static vec<invariant_p> invariants;
181 /* Check the size of the invariant table and realloc if necessary. */
183 static void
184 check_invariant_table_size (void)
186 if (invariant_table_size < DF_DEFS_TABLE_SIZE ())
188 unsigned int new_size = DF_DEFS_TABLE_SIZE () + (DF_DEFS_TABLE_SIZE () / 4);
189 invariant_table = XRESIZEVEC (struct invariant *, invariant_table, new_size);
190 memset (&invariant_table[invariant_table_size], 0,
191 (new_size - invariant_table_size) * sizeof (struct invariant *));
192 invariant_table_size = new_size;
196 /* Test for possibility of invariantness of X. */
198 static bool
199 check_maybe_invariant (rtx x)
201 enum rtx_code code = GET_CODE (x);
202 int i, j;
203 const char *fmt;
205 switch (code)
207 CASE_CONST_ANY:
208 case SYMBOL_REF:
209 case CONST:
210 case LABEL_REF:
211 return true;
213 case PC:
214 case CC0:
215 case UNSPEC_VOLATILE:
216 case CALL:
217 return false;
219 case REG:
220 return true;
222 case MEM:
223 /* Load/store motion is done elsewhere. ??? Perhaps also add it here?
224 It should not be hard, and might be faster than "elsewhere". */
226 /* Just handle the most trivial case where we load from an unchanging
227 location (most importantly, pic tables). */
228 if (MEM_READONLY_P (x) && !MEM_VOLATILE_P (x))
229 break;
231 return false;
233 case ASM_OPERANDS:
234 /* Don't mess with insns declared volatile. */
235 if (MEM_VOLATILE_P (x))
236 return false;
237 break;
239 default:
240 break;
243 fmt = GET_RTX_FORMAT (code);
244 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
246 if (fmt[i] == 'e')
248 if (!check_maybe_invariant (XEXP (x, i)))
249 return false;
251 else if (fmt[i] == 'E')
253 for (j = 0; j < XVECLEN (x, i); j++)
254 if (!check_maybe_invariant (XVECEXP (x, i, j)))
255 return false;
259 return true;
262 /* Returns the invariant definition for USE, or NULL if USE is not
263 invariant. */
265 static struct invariant *
266 invariant_for_use (df_ref use)
268 struct df_link *defs;
269 df_ref def;
270 basic_block bb = DF_REF_BB (use), def_bb;
272 if (DF_REF_FLAGS (use) & DF_REF_READ_WRITE)
273 return NULL;
275 defs = DF_REF_CHAIN (use);
276 if (!defs || defs->next)
277 return NULL;
278 def = defs->ref;
279 check_invariant_table_size ();
280 if (!invariant_table[DF_REF_ID (def)])
281 return NULL;
283 def_bb = DF_REF_BB (def);
284 if (!dominated_by_p (CDI_DOMINATORS, bb, def_bb))
285 return NULL;
286 return invariant_table[DF_REF_ID (def)];
289 /* Computes hash value for invariant expression X in INSN. */
291 static hashval_t
292 hash_invariant_expr_1 (rtx_insn *insn, rtx x)
294 enum rtx_code code = GET_CODE (x);
295 int i, j;
296 const char *fmt;
297 hashval_t val = code;
298 int do_not_record_p;
299 df_ref use;
300 struct invariant *inv;
302 switch (code)
304 CASE_CONST_ANY:
305 case SYMBOL_REF:
306 case CONST:
307 case LABEL_REF:
308 return hash_rtx (x, GET_MODE (x), &do_not_record_p, NULL, false);
310 case REG:
311 use = df_find_use (insn, x);
312 if (!use)
313 return hash_rtx (x, GET_MODE (x), &do_not_record_p, NULL, false);
314 inv = invariant_for_use (use);
315 if (!inv)
316 return hash_rtx (x, GET_MODE (x), &do_not_record_p, NULL, false);
318 gcc_assert (inv->eqto != ~0u);
319 return inv->eqto;
321 default:
322 break;
325 fmt = GET_RTX_FORMAT (code);
326 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
328 if (fmt[i] == 'e')
329 val ^= hash_invariant_expr_1 (insn, XEXP (x, i));
330 else if (fmt[i] == 'E')
332 for (j = 0; j < XVECLEN (x, i); j++)
333 val ^= hash_invariant_expr_1 (insn, XVECEXP (x, i, j));
335 else if (fmt[i] == 'i' || fmt[i] == 'n')
336 val ^= XINT (x, i);
339 return val;
342 /* Returns true if the invariant expressions E1 and E2 used in insns INSN1
343 and INSN2 have always the same value. */
345 static bool
346 invariant_expr_equal_p (rtx_insn *insn1, rtx e1, rtx_insn *insn2, rtx e2)
348 enum rtx_code code = GET_CODE (e1);
349 int i, j;
350 const char *fmt;
351 df_ref use1, use2;
352 struct invariant *inv1 = NULL, *inv2 = NULL;
353 rtx sub1, sub2;
355 /* If mode of only one of the operands is VOIDmode, it is not equivalent to
356 the other one. If both are VOIDmode, we rely on the caller of this
357 function to verify that their modes are the same. */
358 if (code != GET_CODE (e2) || GET_MODE (e1) != GET_MODE (e2))
359 return false;
361 switch (code)
363 CASE_CONST_ANY:
364 case SYMBOL_REF:
365 case CONST:
366 case LABEL_REF:
367 return rtx_equal_p (e1, e2);
369 case REG:
370 use1 = df_find_use (insn1, e1);
371 use2 = df_find_use (insn2, e2);
372 if (use1)
373 inv1 = invariant_for_use (use1);
374 if (use2)
375 inv2 = invariant_for_use (use2);
377 if (!inv1 && !inv2)
378 return rtx_equal_p (e1, e2);
380 if (!inv1 || !inv2)
381 return false;
383 gcc_assert (inv1->eqto != ~0u);
384 gcc_assert (inv2->eqto != ~0u);
385 return inv1->eqto == inv2->eqto;
387 default:
388 break;
391 fmt = GET_RTX_FORMAT (code);
392 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
394 if (fmt[i] == 'e')
396 sub1 = XEXP (e1, i);
397 sub2 = XEXP (e2, i);
399 if (!invariant_expr_equal_p (insn1, sub1, insn2, sub2))
400 return false;
403 else if (fmt[i] == 'E')
405 if (XVECLEN (e1, i) != XVECLEN (e2, i))
406 return false;
408 for (j = 0; j < XVECLEN (e1, i); j++)
410 sub1 = XVECEXP (e1, i, j);
411 sub2 = XVECEXP (e2, i, j);
413 if (!invariant_expr_equal_p (insn1, sub1, insn2, sub2))
414 return false;
417 else if (fmt[i] == 'i' || fmt[i] == 'n')
419 if (XINT (e1, i) != XINT (e2, i))
420 return false;
422 /* Unhandled type of subexpression, we fail conservatively. */
423 else
424 return false;
427 return true;
430 struct invariant_expr_hasher : typed_free_remove <invariant_expr_entry>
432 typedef invariant_expr_entry value_type;
433 typedef invariant_expr_entry compare_type;
434 static inline hashval_t hash (const value_type *);
435 static inline bool equal (const value_type *, const compare_type *);
438 /* Returns hash value for invariant expression entry ENTRY. */
440 inline hashval_t
441 invariant_expr_hasher::hash (const value_type *entry)
443 return entry->hash;
446 /* Compares invariant expression entries ENTRY1 and ENTRY2. */
448 inline bool
449 invariant_expr_hasher::equal (const value_type *entry1,
450 const compare_type *entry2)
452 if (entry1->mode != entry2->mode)
453 return 0;
455 return invariant_expr_equal_p (entry1->inv->insn, entry1->expr,
456 entry2->inv->insn, entry2->expr);
459 typedef hash_table<invariant_expr_hasher> invariant_htab_type;
461 /* Checks whether invariant with value EXPR in machine mode MODE is
462 recorded in EQ. If this is the case, return the invariant. Otherwise
463 insert INV to the table for this expression and return INV. */
465 static struct invariant *
466 find_or_insert_inv (invariant_htab_type *eq, rtx expr, enum machine_mode mode,
467 struct invariant *inv)
469 hashval_t hash = hash_invariant_expr_1 (inv->insn, expr);
470 struct invariant_expr_entry *entry;
471 struct invariant_expr_entry pentry;
472 invariant_expr_entry **slot;
474 pentry.expr = expr;
475 pentry.inv = inv;
476 pentry.mode = mode;
477 slot = eq->find_slot_with_hash (&pentry, hash, INSERT);
478 entry = *slot;
480 if (entry)
481 return entry->inv;
483 entry = XNEW (struct invariant_expr_entry);
484 entry->inv = inv;
485 entry->expr = expr;
486 entry->mode = mode;
487 entry->hash = hash;
488 *slot = entry;
490 return inv;
493 /* Finds invariants identical to INV and records the equivalence. EQ is the
494 hash table of the invariants. */
496 static void
497 find_identical_invariants (invariant_htab_type *eq, struct invariant *inv)
499 unsigned depno;
500 bitmap_iterator bi;
501 struct invariant *dep;
502 rtx expr, set;
503 enum machine_mode mode;
504 struct invariant *tmp;
506 if (inv->eqto != ~0u)
507 return;
509 EXECUTE_IF_SET_IN_BITMAP (inv->depends_on, 0, depno, bi)
511 dep = invariants[depno];
512 find_identical_invariants (eq, dep);
515 set = single_set (inv->insn);
516 expr = SET_SRC (set);
517 mode = GET_MODE (expr);
518 if (mode == VOIDmode)
519 mode = GET_MODE (SET_DEST (set));
521 tmp = find_or_insert_inv (eq, expr, mode, inv);
522 inv->eqto = tmp->invno;
524 if (tmp->invno != inv->invno && inv->always_executed)
525 tmp->eqno++;
527 if (dump_file && inv->eqto != inv->invno)
528 fprintf (dump_file,
529 "Invariant %d is equivalent to invariant %d.\n",
530 inv->invno, inv->eqto);
533 /* Find invariants with the same value and record the equivalences. */
535 static void
536 merge_identical_invariants (void)
538 unsigned i;
539 struct invariant *inv;
540 invariant_htab_type eq (invariants.length ());
542 FOR_EACH_VEC_ELT (invariants, i, inv)
543 find_identical_invariants (&eq, inv);
546 /* Determines the basic blocks inside LOOP that are always executed and
547 stores their bitmap to ALWAYS_REACHED. MAY_EXIT is a bitmap of
548 basic blocks that may either exit the loop, or contain the call that
549 does not have to return. BODY is body of the loop obtained by
550 get_loop_body_in_dom_order. */
552 static void
553 compute_always_reached (struct loop *loop, basic_block *body,
554 bitmap may_exit, bitmap always_reached)
556 unsigned i;
558 for (i = 0; i < loop->num_nodes; i++)
560 if (dominated_by_p (CDI_DOMINATORS, loop->latch, body[i]))
561 bitmap_set_bit (always_reached, i);
563 if (bitmap_bit_p (may_exit, i))
564 return;
568 /* Finds exits out of the LOOP with body BODY. Marks blocks in that we may
569 exit the loop by cfg edge to HAS_EXIT and MAY_EXIT. In MAY_EXIT
570 additionally mark blocks that may exit due to a call. */
572 static void
573 find_exits (struct loop *loop, basic_block *body,
574 bitmap may_exit, bitmap has_exit)
576 unsigned i;
577 edge_iterator ei;
578 edge e;
579 struct loop *outermost_exit = loop, *aexit;
580 bool has_call = false;
581 rtx_insn *insn;
583 for (i = 0; i < loop->num_nodes; i++)
585 if (body[i]->loop_father == loop)
587 FOR_BB_INSNS (body[i], insn)
589 if (CALL_P (insn)
590 && (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)
591 || !RTL_CONST_OR_PURE_CALL_P (insn)))
593 has_call = true;
594 bitmap_set_bit (may_exit, i);
595 break;
599 FOR_EACH_EDGE (e, ei, body[i]->succs)
601 if (flow_bb_inside_loop_p (loop, e->dest))
602 continue;
604 bitmap_set_bit (may_exit, i);
605 bitmap_set_bit (has_exit, i);
606 outermost_exit = find_common_loop (outermost_exit,
607 e->dest->loop_father);
609 continue;
612 /* Use the data stored for the subloop to decide whether we may exit
613 through it. It is sufficient to do this for header of the loop,
614 as other basic blocks inside it must be dominated by it. */
615 if (body[i]->loop_father->header != body[i])
616 continue;
618 if (LOOP_DATA (body[i]->loop_father)->has_call)
620 has_call = true;
621 bitmap_set_bit (may_exit, i);
623 aexit = LOOP_DATA (body[i]->loop_father)->outermost_exit;
624 if (aexit != loop)
626 bitmap_set_bit (may_exit, i);
627 bitmap_set_bit (has_exit, i);
629 if (flow_loop_nested_p (aexit, outermost_exit))
630 outermost_exit = aexit;
634 if (loop->aux == NULL)
636 loop->aux = xcalloc (1, sizeof (struct loop_data));
637 bitmap_initialize (&LOOP_DATA (loop)->regs_ref, &reg_obstack);
638 bitmap_initialize (&LOOP_DATA (loop)->regs_live, &reg_obstack);
640 LOOP_DATA (loop)->outermost_exit = outermost_exit;
641 LOOP_DATA (loop)->has_call = has_call;
644 /* Check whether we may assign a value to X from a register. */
646 static bool
647 may_assign_reg_p (rtx x)
649 return (GET_MODE (x) != VOIDmode
650 && GET_MODE (x) != BLKmode
651 && can_copy_p (GET_MODE (x))
652 && (!REG_P (x)
653 || !HARD_REGISTER_P (x)
654 || REGNO_REG_CLASS (REGNO (x)) != NO_REGS));
657 /* Finds definitions that may correspond to invariants in LOOP with body
658 BODY. */
660 static void
661 find_defs (struct loop *loop)
663 if (dump_file)
665 fprintf (dump_file,
666 "*****starting processing of loop %d ******\n",
667 loop->num);
670 df_remove_problem (df_chain);
671 df_process_deferred_rescans ();
672 df_chain_add_problem (DF_UD_CHAIN);
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 inv->cheap_address = address_cost (SET_SRC (set), word_mode,
718 ADDR_SPACE_GENERIC, speed) < 3;
720 else
722 inv->cost = set_src_cost (SET_SRC (set), speed);
723 inv->cheap_address = false;
726 inv->move = false;
727 inv->reg = NULL_RTX;
728 inv->orig_regno = -1;
729 inv->stamp = 0;
730 inv->insn = insn;
732 inv->invno = invariants.length ();
733 inv->eqto = ~0u;
735 /* Itself. */
736 inv->eqno = 1;
738 if (def)
739 def->invno = inv->invno;
740 invariants.safe_push (inv);
742 if (dump_file)
744 fprintf (dump_file,
745 "Set in insn %d is invariant (%d), cost %d, depends on ",
746 INSN_UID (insn), inv->invno, inv->cost);
747 dump_bitmap (dump_file, inv->depends_on);
750 return inv;
753 /* Record USE at DEF. */
755 static void
756 record_use (struct def *def, df_ref use)
758 struct use *u = XNEW (struct use);
760 u->pos = DF_REF_REAL_LOC (use);
761 u->insn = DF_REF_INSN (use);
762 u->addr_use_p = (DF_REF_TYPE (use) == DF_REF_REG_MEM_LOAD
763 || DF_REF_TYPE (use) == DF_REF_REG_MEM_STORE);
764 u->next = def->uses;
765 def->uses = u;
766 def->n_uses++;
767 if (u->addr_use_p)
768 def->n_addr_uses++;
771 /* Finds the invariants USE depends on and store them to the DEPENDS_ON
772 bitmap. Returns true if all dependencies of USE are known to be
773 loop invariants, false otherwise. */
775 static bool
776 check_dependency (basic_block bb, df_ref use, bitmap depends_on)
778 df_ref def;
779 basic_block def_bb;
780 struct df_link *defs;
781 struct def *def_data;
782 struct invariant *inv;
784 if (DF_REF_FLAGS (use) & DF_REF_READ_WRITE)
785 return false;
787 defs = DF_REF_CHAIN (use);
788 if (!defs)
790 unsigned int regno = DF_REF_REGNO (use);
792 /* If this is the use of an uninitialized argument register that is
793 likely to be spilled, do not move it lest this might extend its
794 lifetime and cause reload to die. This can occur for a call to
795 a function taking complex number arguments and moving the insns
796 preparing the arguments without moving the call itself wouldn't
797 gain much in practice. */
798 if ((DF_REF_FLAGS (use) & DF_HARD_REG_LIVE)
799 && FUNCTION_ARG_REGNO_P (regno)
800 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno)))
801 return false;
803 return true;
806 if (defs->next)
807 return false;
809 def = defs->ref;
810 check_invariant_table_size ();
811 inv = invariant_table[DF_REF_ID (def)];
812 if (!inv)
813 return false;
815 def_data = inv->def;
816 gcc_assert (def_data != NULL);
818 def_bb = DF_REF_BB (def);
819 /* Note that in case bb == def_bb, we know that the definition
820 dominates insn, because def has invariant_table[DF_REF_ID(def)]
821 defined and we process the insns in the basic block bb
822 sequentially. */
823 if (!dominated_by_p (CDI_DOMINATORS, bb, def_bb))
824 return false;
826 bitmap_set_bit (depends_on, def_data->invno);
827 return true;
831 /* Finds the invariants INSN depends on and store them to the DEPENDS_ON
832 bitmap. Returns true if all dependencies of INSN are known to be
833 loop invariants, false otherwise. */
835 static bool
836 check_dependencies (rtx_insn *insn, bitmap depends_on)
838 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
839 df_ref use;
840 basic_block bb = BLOCK_FOR_INSN (insn);
842 FOR_EACH_INSN_INFO_USE (use, insn_info)
843 if (!check_dependency (bb, use, depends_on))
844 return false;
845 FOR_EACH_INSN_INFO_EQ_USE (use, insn_info)
846 if (!check_dependency (bb, use, depends_on))
847 return false;
849 return true;
852 /* Pre-check candidate DEST to skip the one which can not make a valid insn
853 during move_invariant_reg. SIMPLE is to skip HARD_REGISTER. */
854 static bool
855 pre_check_invariant_p (bool simple, rtx dest)
857 if (simple && REG_P (dest) && DF_REG_DEF_COUNT (REGNO (dest)) > 1)
859 df_ref use;
860 rtx ref;
861 unsigned int i = REGNO (dest);
862 struct df_insn_info *insn_info;
863 df_ref def_rec;
865 for (use = DF_REG_USE_CHAIN (i); use; use = DF_REF_NEXT_REG (use))
867 ref = DF_REF_INSN (use);
868 insn_info = DF_INSN_INFO_GET (ref);
870 FOR_EACH_INSN_INFO_DEF (def_rec, insn_info)
871 if (DF_REF_REGNO (def_rec) == i)
873 /* Multi definitions at this stage, most likely are due to
874 instruction constraints, which requires both read and write
875 on the same register. Since move_invariant_reg is not
876 powerful enough to handle such cases, just ignore the INV
877 and leave the chance to others. */
878 return false;
882 return true;
885 /* Finds invariant in INSN. ALWAYS_REACHED is true if the insn is always
886 executed. ALWAYS_EXECUTED is true if the insn is always executed,
887 unless the program ends due to a function call. */
889 static void
890 find_invariant_insn (rtx_insn *insn, bool always_reached, bool always_executed)
892 df_ref ref;
893 struct def *def;
894 bitmap depends_on;
895 rtx set, dest;
896 bool simple = true;
897 struct invariant *inv;
899 #ifdef HAVE_cc0
900 /* We can't move a CC0 setter without the user. */
901 if (sets_cc0_p (insn))
902 return;
903 #endif
905 set = single_set (insn);
906 if (!set)
907 return;
908 dest = SET_DEST (set);
910 if (!REG_P (dest)
911 || HARD_REGISTER_P (dest))
912 simple = false;
914 if (!may_assign_reg_p (dest)
915 || !pre_check_invariant_p (simple, dest)
916 || !check_maybe_invariant (SET_SRC (set)))
917 return;
919 /* If the insn can throw exception, we cannot move it at all without changing
920 cfg. */
921 if (can_throw_internal (insn))
922 return;
924 /* We cannot make trapping insn executed, unless it was executed before. */
925 if (may_trap_or_fault_p (PATTERN (insn)) && !always_reached)
926 return;
928 depends_on = BITMAP_ALLOC (NULL);
929 if (!check_dependencies (insn, depends_on))
931 BITMAP_FREE (depends_on);
932 return;
935 if (simple)
936 def = XCNEW (struct def);
937 else
938 def = NULL;
940 inv = create_new_invariant (def, insn, depends_on, always_executed);
942 if (simple)
944 ref = df_find_def (insn, dest);
945 check_invariant_table_size ();
946 invariant_table[DF_REF_ID (ref)] = inv;
950 /* Record registers used in INSN that have a unique invariant definition. */
952 static void
953 record_uses (rtx_insn *insn)
955 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
956 df_ref use;
957 struct invariant *inv;
959 FOR_EACH_INSN_INFO_USE (use, insn_info)
961 inv = invariant_for_use (use);
962 if (inv)
963 record_use (inv->def, use);
965 FOR_EACH_INSN_INFO_EQ_USE (use, insn_info)
967 inv = invariant_for_use (use);
968 if (inv)
969 record_use (inv->def, use);
973 /* Finds invariants in INSN. ALWAYS_REACHED is true if the insn is always
974 executed. ALWAYS_EXECUTED is true if the insn is always executed,
975 unless the program ends due to a function call. */
977 static void
978 find_invariants_insn (rtx_insn *insn, bool always_reached, bool always_executed)
980 find_invariant_insn (insn, always_reached, always_executed);
981 record_uses (insn);
984 /* Finds invariants in basic block BB. ALWAYS_REACHED is true if the
985 basic block is always executed. ALWAYS_EXECUTED is true if the basic
986 block is always executed, unless the program ends due to a function
987 call. */
989 static void
990 find_invariants_bb (basic_block bb, bool always_reached, bool always_executed)
992 rtx_insn *insn;
994 FOR_BB_INSNS (bb, insn)
996 if (!NONDEBUG_INSN_P (insn))
997 continue;
999 find_invariants_insn (insn, always_reached, always_executed);
1001 if (always_reached
1002 && CALL_P (insn)
1003 && (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)
1004 || ! RTL_CONST_OR_PURE_CALL_P (insn)))
1005 always_reached = false;
1009 /* Finds invariants in LOOP with body BODY. ALWAYS_REACHED is the bitmap of
1010 basic blocks in BODY that are always executed. ALWAYS_EXECUTED is the
1011 bitmap of basic blocks in BODY that are always executed unless the program
1012 ends due to a function call. */
1014 static void
1015 find_invariants_body (struct loop *loop, basic_block *body,
1016 bitmap always_reached, bitmap always_executed)
1018 unsigned i;
1020 for (i = 0; i < loop->num_nodes; i++)
1021 find_invariants_bb (body[i],
1022 bitmap_bit_p (always_reached, i),
1023 bitmap_bit_p (always_executed, i));
1026 /* Finds invariants in LOOP. */
1028 static void
1029 find_invariants (struct loop *loop)
1031 bitmap may_exit = BITMAP_ALLOC (NULL);
1032 bitmap always_reached = BITMAP_ALLOC (NULL);
1033 bitmap has_exit = BITMAP_ALLOC (NULL);
1034 bitmap always_executed = BITMAP_ALLOC (NULL);
1035 basic_block *body = get_loop_body_in_dom_order (loop);
1037 find_exits (loop, body, may_exit, has_exit);
1038 compute_always_reached (loop, body, may_exit, always_reached);
1039 compute_always_reached (loop, body, has_exit, always_executed);
1041 find_defs (loop);
1042 find_invariants_body (loop, body, always_reached, always_executed);
1043 merge_identical_invariants ();
1045 BITMAP_FREE (always_reached);
1046 BITMAP_FREE (always_executed);
1047 BITMAP_FREE (may_exit);
1048 BITMAP_FREE (has_exit);
1049 free (body);
1052 /* Frees a list of uses USE. */
1054 static void
1055 free_use_list (struct use *use)
1057 struct use *next;
1059 for (; use; use = next)
1061 next = use->next;
1062 free (use);
1066 /* Return pressure class and number of hard registers (through *NREGS)
1067 for destination of INSN. */
1068 static enum reg_class
1069 get_pressure_class_and_nregs (rtx_insn *insn, int *nregs)
1071 rtx reg;
1072 enum reg_class pressure_class;
1073 rtx set = single_set (insn);
1075 /* Considered invariant insns have only one set. */
1076 gcc_assert (set != NULL_RTX);
1077 reg = SET_DEST (set);
1078 if (GET_CODE (reg) == SUBREG)
1079 reg = SUBREG_REG (reg);
1080 if (MEM_P (reg))
1082 *nregs = 0;
1083 pressure_class = NO_REGS;
1085 else
1087 if (! REG_P (reg))
1088 reg = NULL_RTX;
1089 if (reg == NULL_RTX)
1090 pressure_class = GENERAL_REGS;
1091 else
1093 pressure_class = reg_allocno_class (REGNO (reg));
1094 pressure_class = ira_pressure_class_translate[pressure_class];
1096 *nregs
1097 = ira_reg_class_max_nregs[pressure_class][GET_MODE (SET_SRC (set))];
1099 return pressure_class;
1102 /* Calculates cost and number of registers needed for moving invariant INV
1103 out of the loop and stores them to *COST and *REGS_NEEDED. *CL will be
1104 the REG_CLASS of INV. Return
1105 -1: if INV is invalid.
1106 0: if INV and its depends_on have same reg_class
1107 1: if INV and its depends_on have different reg_classes. */
1109 static int
1110 get_inv_cost (struct invariant *inv, int *comp_cost, unsigned *regs_needed,
1111 enum reg_class *cl)
1113 int i, acomp_cost;
1114 unsigned aregs_needed[N_REG_CLASSES];
1115 unsigned depno;
1116 struct invariant *dep;
1117 bitmap_iterator bi;
1118 int ret = 1;
1120 /* Find the representative of the class of the equivalent invariants. */
1121 inv = invariants[inv->eqto];
1123 *comp_cost = 0;
1124 if (! flag_ira_loop_pressure)
1125 regs_needed[0] = 0;
1126 else
1128 for (i = 0; i < ira_pressure_classes_num; i++)
1129 regs_needed[ira_pressure_classes[i]] = 0;
1132 if (inv->move
1133 || inv->stamp == actual_stamp)
1134 return -1;
1135 inv->stamp = actual_stamp;
1137 if (! flag_ira_loop_pressure)
1138 regs_needed[0]++;
1139 else
1141 int nregs;
1142 enum reg_class pressure_class;
1144 pressure_class = get_pressure_class_and_nregs (inv->insn, &nregs);
1145 regs_needed[pressure_class] += nregs;
1146 *cl = pressure_class;
1147 ret = 0;
1150 if (!inv->cheap_address
1151 || inv->def->n_addr_uses < inv->def->n_uses)
1152 (*comp_cost) += inv->cost * inv->eqno;
1154 #ifdef STACK_REGS
1156 /* Hoisting constant pool constants into stack regs may cost more than
1157 just single register. On x87, the balance is affected both by the
1158 small number of FP registers, and by its register stack organization,
1159 that forces us to add compensation code in and around the loop to
1160 shuffle the operands to the top of stack before use, and pop them
1161 from the stack after the loop finishes.
1163 To model this effect, we increase the number of registers needed for
1164 stack registers by two: one register push, and one register pop.
1165 This usually has the effect that FP constant loads from the constant
1166 pool are not moved out of the loop.
1168 Note that this also means that dependent invariants can not be moved.
1169 However, the primary purpose of this pass is to move loop invariant
1170 address arithmetic out of loops, and address arithmetic that depends
1171 on floating point constants is unlikely to ever occur. */
1172 rtx set = single_set (inv->insn);
1173 if (set
1174 && IS_STACK_MODE (GET_MODE (SET_SRC (set)))
1175 && constant_pool_constant_p (SET_SRC (set)))
1177 if (flag_ira_loop_pressure)
1178 regs_needed[ira_stack_reg_pressure_class] += 2;
1179 else
1180 regs_needed[0] += 2;
1183 #endif
1185 EXECUTE_IF_SET_IN_BITMAP (inv->depends_on, 0, depno, bi)
1187 bool check_p;
1188 enum reg_class dep_cl = ALL_REGS;
1189 int dep_ret;
1191 dep = invariants[depno];
1193 /* If DEP is moved out of the loop, it is not a depends_on any more. */
1194 if (dep->move)
1195 continue;
1197 dep_ret = get_inv_cost (dep, &acomp_cost, aregs_needed, &dep_cl);
1199 if (! flag_ira_loop_pressure)
1200 check_p = aregs_needed[0] != 0;
1201 else
1203 for (i = 0; i < ira_pressure_classes_num; i++)
1204 if (aregs_needed[ira_pressure_classes[i]] != 0)
1205 break;
1206 check_p = i < ira_pressure_classes_num;
1208 if ((dep_ret == 1) || ((dep_ret == 0) && (*cl != dep_cl)))
1210 *cl = ALL_REGS;
1211 ret = 1;
1214 if (check_p
1215 /* We need to check always_executed, since if the original value of
1216 the invariant may be preserved, we may need to keep it in a
1217 separate register. TODO check whether the register has an
1218 use outside of the loop. */
1219 && dep->always_executed
1220 && !dep->def->uses->next)
1222 /* If this is a single use, after moving the dependency we will not
1223 need a new register. */
1224 if (! flag_ira_loop_pressure)
1225 aregs_needed[0]--;
1226 else
1228 int nregs;
1229 enum reg_class pressure_class;
1231 pressure_class = get_pressure_class_and_nregs (inv->insn, &nregs);
1232 aregs_needed[pressure_class] -= nregs;
1236 if (! flag_ira_loop_pressure)
1237 regs_needed[0] += aregs_needed[0];
1238 else
1240 for (i = 0; i < ira_pressure_classes_num; i++)
1241 regs_needed[ira_pressure_classes[i]]
1242 += aregs_needed[ira_pressure_classes[i]];
1244 (*comp_cost) += acomp_cost;
1246 return ret;
1249 /* Calculates gain for eliminating invariant INV. REGS_USED is the number
1250 of registers used in the loop, NEW_REGS is the number of new variables
1251 already added due to the invariant motion. The number of registers needed
1252 for it is stored in *REGS_NEEDED. SPEED and CALL_P are flags passed
1253 through to estimate_reg_pressure_cost. */
1255 static int
1256 gain_for_invariant (struct invariant *inv, unsigned *regs_needed,
1257 unsigned *new_regs, unsigned regs_used,
1258 bool speed, bool call_p)
1260 int comp_cost, size_cost;
1261 enum reg_class cl;
1262 int ret;
1264 actual_stamp++;
1266 ret = get_inv_cost (inv, &comp_cost, regs_needed, &cl);
1268 if (! flag_ira_loop_pressure)
1270 size_cost = (estimate_reg_pressure_cost (new_regs[0] + regs_needed[0],
1271 regs_used, speed, call_p)
1272 - estimate_reg_pressure_cost (new_regs[0],
1273 regs_used, speed, call_p));
1275 else if (ret < 0)
1276 return -1;
1277 else if ((ret == 0) && (cl == NO_REGS))
1278 /* Hoist it anyway since it does not impact register pressure. */
1279 return 1;
1280 else
1282 int i;
1283 enum reg_class pressure_class;
1285 for (i = 0; i < ira_pressure_classes_num; i++)
1287 pressure_class = ira_pressure_classes[i];
1289 if (!reg_classes_intersect_p (pressure_class, cl))
1290 continue;
1292 if ((int) new_regs[pressure_class]
1293 + (int) regs_needed[pressure_class]
1294 + LOOP_DATA (curr_loop)->max_reg_pressure[pressure_class]
1295 + IRA_LOOP_RESERVED_REGS
1296 > ira_class_hard_regs_num[pressure_class])
1297 break;
1299 if (i < ira_pressure_classes_num)
1300 /* There will be register pressure excess and we want not to
1301 make this loop invariant motion. All loop invariants with
1302 non-positive gains will be rejected in function
1303 find_invariants_to_move. Therefore we return the negative
1304 number here.
1306 One could think that this rejects also expensive loop
1307 invariant motions and this will hurt code performance.
1308 However numerous experiments with different heuristics
1309 taking invariant cost into account did not confirm this
1310 assumption. There are possible explanations for this
1311 result:
1312 o probably all expensive invariants were already moved out
1313 of the loop by PRE and gimple invariant motion pass.
1314 o expensive invariant execution will be hidden by insn
1315 scheduling or OOO processor hardware because usually such
1316 invariants have a lot of freedom to be executed
1317 out-of-order.
1318 Another reason for ignoring invariant cost vs spilling cost
1319 heuristics is also in difficulties to evaluate accurately
1320 spill cost at this stage. */
1321 return -1;
1322 else
1323 size_cost = 0;
1326 return comp_cost - size_cost;
1329 /* Finds invariant with best gain for moving. Returns the gain, stores
1330 the invariant in *BEST and number of registers needed for it to
1331 *REGS_NEEDED. REGS_USED is the number of registers used in the loop.
1332 NEW_REGS is the number of new variables already added due to invariant
1333 motion. */
1335 static int
1336 best_gain_for_invariant (struct invariant **best, unsigned *regs_needed,
1337 unsigned *new_regs, unsigned regs_used,
1338 bool speed, bool call_p)
1340 struct invariant *inv;
1341 int i, gain = 0, again;
1342 unsigned aregs_needed[N_REG_CLASSES], invno;
1344 FOR_EACH_VEC_ELT (invariants, invno, inv)
1346 if (inv->move)
1347 continue;
1349 /* Only consider the "representatives" of equivalent invariants. */
1350 if (inv->eqto != inv->invno)
1351 continue;
1353 again = gain_for_invariant (inv, aregs_needed, new_regs, regs_used,
1354 speed, call_p);
1355 if (again > gain)
1357 gain = again;
1358 *best = inv;
1359 if (! flag_ira_loop_pressure)
1360 regs_needed[0] = aregs_needed[0];
1361 else
1363 for (i = 0; i < ira_pressure_classes_num; i++)
1364 regs_needed[ira_pressure_classes[i]]
1365 = aregs_needed[ira_pressure_classes[i]];
1370 return gain;
1373 /* Marks invariant INVNO and all its dependencies for moving. */
1375 static void
1376 set_move_mark (unsigned invno, int gain)
1378 struct invariant *inv = invariants[invno];
1379 bitmap_iterator bi;
1381 /* Find the representative of the class of the equivalent invariants. */
1382 inv = invariants[inv->eqto];
1384 if (inv->move)
1385 return;
1386 inv->move = true;
1388 if (dump_file)
1390 if (gain >= 0)
1391 fprintf (dump_file, "Decided to move invariant %d -- gain %d\n",
1392 invno, gain);
1393 else
1394 fprintf (dump_file, "Decided to move dependent invariant %d\n",
1395 invno);
1398 EXECUTE_IF_SET_IN_BITMAP (inv->depends_on, 0, invno, bi)
1400 set_move_mark (invno, -1);
1404 /* Determines which invariants to move. */
1406 static void
1407 find_invariants_to_move (bool speed, bool call_p)
1409 int gain;
1410 unsigned i, regs_used, regs_needed[N_REG_CLASSES], new_regs[N_REG_CLASSES];
1411 struct invariant *inv = NULL;
1413 if (!invariants.length ())
1414 return;
1416 if (flag_ira_loop_pressure)
1417 /* REGS_USED is actually never used when the flag is on. */
1418 regs_used = 0;
1419 else
1420 /* We do not really do a good job in estimating number of
1421 registers used; we put some initial bound here to stand for
1422 induction variables etc. that we do not detect. */
1424 unsigned int n_regs = DF_REG_SIZE (df);
1426 regs_used = 2;
1428 for (i = 0; i < n_regs; i++)
1430 if (!DF_REGNO_FIRST_DEF (i) && DF_REGNO_LAST_USE (i))
1432 /* This is a value that is used but not changed inside loop. */
1433 regs_used++;
1438 if (! flag_ira_loop_pressure)
1439 new_regs[0] = regs_needed[0] = 0;
1440 else
1442 for (i = 0; (int) i < ira_pressure_classes_num; i++)
1443 new_regs[ira_pressure_classes[i]] = 0;
1445 while ((gain = best_gain_for_invariant (&inv, regs_needed,
1446 new_regs, regs_used,
1447 speed, call_p)) > 0)
1449 set_move_mark (inv->invno, gain);
1450 if (! flag_ira_loop_pressure)
1451 new_regs[0] += regs_needed[0];
1452 else
1454 for (i = 0; (int) i < ira_pressure_classes_num; i++)
1455 new_regs[ira_pressure_classes[i]]
1456 += regs_needed[ira_pressure_classes[i]];
1461 /* Replace the uses, reached by the definition of invariant INV, by REG.
1463 IN_GROUP is nonzero if this is part of a group of changes that must be
1464 performed as a group. In that case, the changes will be stored. The
1465 function `apply_change_group' will validate and apply the changes. */
1467 static int
1468 replace_uses (struct invariant *inv, rtx reg, bool in_group)
1470 /* Replace the uses we know to be dominated. It saves work for copy
1471 propagation, and also it is necessary so that dependent invariants
1472 are computed right. */
1473 if (inv->def)
1475 struct use *use;
1476 for (use = inv->def->uses; use; use = use->next)
1477 validate_change (use->insn, use->pos, reg, true);
1479 /* If we aren't part of a larger group, apply the changes now. */
1480 if (!in_group)
1481 return apply_change_group ();
1484 return 1;
1487 /* Move invariant INVNO out of the LOOP. Returns true if this succeeds, false
1488 otherwise. */
1490 static bool
1491 move_invariant_reg (struct loop *loop, unsigned invno)
1493 struct invariant *inv = invariants[invno];
1494 struct invariant *repr = invariants[inv->eqto];
1495 unsigned i;
1496 basic_block preheader = loop_preheader_edge (loop)->src;
1497 rtx reg, set, dest, note;
1498 bitmap_iterator bi;
1499 int regno = -1;
1501 if (inv->reg)
1502 return true;
1503 if (!repr->move)
1504 return false;
1506 /* If this is a representative of the class of equivalent invariants,
1507 really move the invariant. Otherwise just replace its use with
1508 the register used for the representative. */
1509 if (inv == repr)
1511 if (inv->depends_on)
1513 EXECUTE_IF_SET_IN_BITMAP (inv->depends_on, 0, i, bi)
1515 if (!move_invariant_reg (loop, i))
1516 goto fail;
1520 /* Move the set out of the loop. If the set is always executed (we could
1521 omit this condition if we know that the register is unused outside of
1522 the loop, but it does not seem worth finding out) and it has no uses
1523 that would not be dominated by it, we may just move it (TODO).
1524 Otherwise we need to create a temporary register. */
1525 set = single_set (inv->insn);
1526 reg = dest = SET_DEST (set);
1527 if (GET_CODE (reg) == SUBREG)
1528 reg = SUBREG_REG (reg);
1529 if (REG_P (reg))
1530 regno = REGNO (reg);
1532 reg = gen_reg_rtx_and_attrs (dest);
1534 /* Try replacing the destination by a new pseudoregister. */
1535 validate_change (inv->insn, &SET_DEST (set), reg, true);
1537 /* As well as all the dominated uses. */
1538 replace_uses (inv, reg, true);
1540 /* And validate all the changes. */
1541 if (!apply_change_group ())
1542 goto fail;
1544 emit_insn_after (gen_move_insn (dest, reg), inv->insn);
1545 reorder_insns (inv->insn, inv->insn, BB_END (preheader));
1547 /* If there is a REG_EQUAL note on the insn we just moved, and the
1548 insn is in a basic block that is not always executed or the note
1549 contains something for which we don't know the invariant status,
1550 the note may no longer be valid after we move the insn. Note that
1551 uses in REG_EQUAL notes are taken into account in the computation
1552 of invariants, so it is safe to retain the note even if it contains
1553 register references for which we know the invariant status. */
1554 if ((note = find_reg_note (inv->insn, REG_EQUAL, NULL_RTX))
1555 && (!inv->always_executed
1556 || !check_maybe_invariant (XEXP (note, 0))))
1557 remove_note (inv->insn, note);
1559 else
1561 if (!move_invariant_reg (loop, repr->invno))
1562 goto fail;
1563 reg = repr->reg;
1564 regno = repr->orig_regno;
1565 if (!replace_uses (inv, reg, false))
1566 goto fail;
1567 set = single_set (inv->insn);
1568 emit_insn_after (gen_move_insn (SET_DEST (set), reg), inv->insn);
1569 delete_insn (inv->insn);
1572 inv->reg = reg;
1573 inv->orig_regno = regno;
1575 return true;
1577 fail:
1578 /* If we failed, clear move flag, so that we do not try to move inv
1579 again. */
1580 if (dump_file)
1581 fprintf (dump_file, "Failed to move invariant %d\n", invno);
1582 inv->move = false;
1583 inv->reg = NULL_RTX;
1584 inv->orig_regno = -1;
1586 return false;
1589 /* Move selected invariant out of the LOOP. Newly created regs are marked
1590 in TEMPORARY_REGS. */
1592 static void
1593 move_invariants (struct loop *loop)
1595 struct invariant *inv;
1596 unsigned i;
1598 FOR_EACH_VEC_ELT (invariants, i, inv)
1599 move_invariant_reg (loop, i);
1600 if (flag_ira_loop_pressure && resize_reg_info ())
1602 FOR_EACH_VEC_ELT (invariants, i, inv)
1603 if (inv->reg != NULL_RTX)
1605 if (inv->orig_regno >= 0)
1606 setup_reg_classes (REGNO (inv->reg),
1607 reg_preferred_class (inv->orig_regno),
1608 reg_alternate_class (inv->orig_regno),
1609 reg_allocno_class (inv->orig_regno));
1610 else
1611 setup_reg_classes (REGNO (inv->reg),
1612 GENERAL_REGS, NO_REGS, GENERAL_REGS);
1617 /* Initializes invariant motion data. */
1619 static void
1620 init_inv_motion_data (void)
1622 actual_stamp = 1;
1624 invariants.create (100);
1627 /* Frees the data allocated by invariant motion. */
1629 static void
1630 free_inv_motion_data (void)
1632 unsigned i;
1633 struct def *def;
1634 struct invariant *inv;
1636 check_invariant_table_size ();
1637 for (i = 0; i < DF_DEFS_TABLE_SIZE (); i++)
1639 inv = invariant_table[i];
1640 if (inv)
1642 def = inv->def;
1643 gcc_assert (def != NULL);
1645 free_use_list (def->uses);
1646 free (def);
1647 invariant_table[i] = NULL;
1651 FOR_EACH_VEC_ELT (invariants, i, inv)
1653 BITMAP_FREE (inv->depends_on);
1654 free (inv);
1656 invariants.release ();
1659 /* Move the invariants out of the LOOP. */
1661 static void
1662 move_single_loop_invariants (struct loop *loop)
1664 init_inv_motion_data ();
1666 find_invariants (loop);
1667 find_invariants_to_move (optimize_loop_for_speed_p (loop),
1668 LOOP_DATA (loop)->has_call);
1669 move_invariants (loop);
1671 free_inv_motion_data ();
1674 /* Releases the auxiliary data for LOOP. */
1676 static void
1677 free_loop_data (struct loop *loop)
1679 struct loop_data *data = LOOP_DATA (loop);
1680 if (!data)
1681 return;
1683 bitmap_clear (&LOOP_DATA (loop)->regs_ref);
1684 bitmap_clear (&LOOP_DATA (loop)->regs_live);
1685 free (data);
1686 loop->aux = NULL;
1691 /* Registers currently living. */
1692 static bitmap_head curr_regs_live;
1694 /* Current reg pressure for each pressure class. */
1695 static int curr_reg_pressure[N_REG_CLASSES];
1697 /* Record all regs that are set in any one insn. Communication from
1698 mark_reg_{store,clobber} and global_conflicts. Asm can refer to
1699 all hard-registers. */
1700 static rtx regs_set[(FIRST_PSEUDO_REGISTER > MAX_RECOG_OPERANDS
1701 ? FIRST_PSEUDO_REGISTER : MAX_RECOG_OPERANDS) * 2];
1702 /* Number of regs stored in the previous array. */
1703 static int n_regs_set;
1705 /* Return pressure class and number of needed hard registers (through
1706 *NREGS) of register REGNO. */
1707 static enum reg_class
1708 get_regno_pressure_class (int regno, int *nregs)
1710 if (regno >= FIRST_PSEUDO_REGISTER)
1712 enum reg_class pressure_class;
1714 pressure_class = reg_allocno_class (regno);
1715 pressure_class = ira_pressure_class_translate[pressure_class];
1716 *nregs
1717 = ira_reg_class_max_nregs[pressure_class][PSEUDO_REGNO_MODE (regno)];
1718 return pressure_class;
1720 else if (! TEST_HARD_REG_BIT (ira_no_alloc_regs, regno)
1721 && ! TEST_HARD_REG_BIT (eliminable_regset, regno))
1723 *nregs = 1;
1724 return ira_pressure_class_translate[REGNO_REG_CLASS (regno)];
1726 else
1728 *nregs = 0;
1729 return NO_REGS;
1733 /* Increase (if INCR_P) or decrease current register pressure for
1734 register REGNO. */
1735 static void
1736 change_pressure (int regno, bool incr_p)
1738 int nregs;
1739 enum reg_class pressure_class;
1741 pressure_class = get_regno_pressure_class (regno, &nregs);
1742 if (! incr_p)
1743 curr_reg_pressure[pressure_class] -= nregs;
1744 else
1746 curr_reg_pressure[pressure_class] += nregs;
1747 if (LOOP_DATA (curr_loop)->max_reg_pressure[pressure_class]
1748 < curr_reg_pressure[pressure_class])
1749 LOOP_DATA (curr_loop)->max_reg_pressure[pressure_class]
1750 = curr_reg_pressure[pressure_class];
1754 /* Mark REGNO birth. */
1755 static void
1756 mark_regno_live (int regno)
1758 struct loop *loop;
1760 for (loop = curr_loop;
1761 loop != current_loops->tree_root;
1762 loop = loop_outer (loop))
1763 bitmap_set_bit (&LOOP_DATA (loop)->regs_live, regno);
1764 if (!bitmap_set_bit (&curr_regs_live, regno))
1765 return;
1766 change_pressure (regno, true);
1769 /* Mark REGNO death. */
1770 static void
1771 mark_regno_death (int regno)
1773 if (! bitmap_clear_bit (&curr_regs_live, regno))
1774 return;
1775 change_pressure (regno, false);
1778 /* Mark setting register REG. */
1779 static void
1780 mark_reg_store (rtx reg, const_rtx setter ATTRIBUTE_UNUSED,
1781 void *data ATTRIBUTE_UNUSED)
1783 int regno;
1785 if (GET_CODE (reg) == SUBREG)
1786 reg = SUBREG_REG (reg);
1788 if (! REG_P (reg))
1789 return;
1791 regs_set[n_regs_set++] = reg;
1793 regno = REGNO (reg);
1795 if (regno >= FIRST_PSEUDO_REGISTER)
1796 mark_regno_live (regno);
1797 else
1799 int last = regno + hard_regno_nregs[regno][GET_MODE (reg)];
1801 while (regno < last)
1803 mark_regno_live (regno);
1804 regno++;
1809 /* Mark clobbering register REG. */
1810 static void
1811 mark_reg_clobber (rtx reg, const_rtx setter, void *data)
1813 if (GET_CODE (setter) == CLOBBER)
1814 mark_reg_store (reg, setter, data);
1817 /* Mark register REG death. */
1818 static void
1819 mark_reg_death (rtx reg)
1821 int regno = REGNO (reg);
1823 if (regno >= FIRST_PSEUDO_REGISTER)
1824 mark_regno_death (regno);
1825 else
1827 int last = regno + hard_regno_nregs[regno][GET_MODE (reg)];
1829 while (regno < last)
1831 mark_regno_death (regno);
1832 regno++;
1837 /* Mark occurrence of registers in X for the current loop. */
1838 static void
1839 mark_ref_regs (rtx x)
1841 RTX_CODE code;
1842 int i;
1843 const char *fmt;
1845 if (!x)
1846 return;
1848 code = GET_CODE (x);
1849 if (code == REG)
1851 struct loop *loop;
1853 for (loop = curr_loop;
1854 loop != current_loops->tree_root;
1855 loop = loop_outer (loop))
1856 bitmap_set_bit (&LOOP_DATA (loop)->regs_ref, REGNO (x));
1857 return;
1860 fmt = GET_RTX_FORMAT (code);
1861 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1862 if (fmt[i] == 'e')
1863 mark_ref_regs (XEXP (x, i));
1864 else if (fmt[i] == 'E')
1866 int j;
1868 for (j = 0; j < XVECLEN (x, i); j++)
1869 mark_ref_regs (XVECEXP (x, i, j));
1873 /* Calculate register pressure in the loops. */
1874 static void
1875 calculate_loop_reg_pressure (void)
1877 int i;
1878 unsigned int j;
1879 bitmap_iterator bi;
1880 basic_block bb;
1881 rtx_insn *insn;
1882 rtx link;
1883 struct loop *loop, *parent;
1885 FOR_EACH_LOOP (loop, 0)
1886 if (loop->aux == NULL)
1888 loop->aux = xcalloc (1, sizeof (struct loop_data));
1889 bitmap_initialize (&LOOP_DATA (loop)->regs_ref, &reg_obstack);
1890 bitmap_initialize (&LOOP_DATA (loop)->regs_live, &reg_obstack);
1892 ira_setup_eliminable_regset ();
1893 bitmap_initialize (&curr_regs_live, &reg_obstack);
1894 FOR_EACH_BB_FN (bb, cfun)
1896 curr_loop = bb->loop_father;
1897 if (curr_loop == current_loops->tree_root)
1898 continue;
1900 for (loop = curr_loop;
1901 loop != current_loops->tree_root;
1902 loop = loop_outer (loop))
1903 bitmap_ior_into (&LOOP_DATA (loop)->regs_live, DF_LR_IN (bb));
1905 bitmap_copy (&curr_regs_live, DF_LR_IN (bb));
1906 for (i = 0; i < ira_pressure_classes_num; i++)
1907 curr_reg_pressure[ira_pressure_classes[i]] = 0;
1908 EXECUTE_IF_SET_IN_BITMAP (&curr_regs_live, 0, j, bi)
1909 change_pressure (j, true);
1911 FOR_BB_INSNS (bb, insn)
1913 if (! NONDEBUG_INSN_P (insn))
1914 continue;
1916 mark_ref_regs (PATTERN (insn));
1917 n_regs_set = 0;
1918 note_stores (PATTERN (insn), mark_reg_clobber, NULL);
1920 /* Mark any registers dead after INSN as dead now. */
1922 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1923 if (REG_NOTE_KIND (link) == REG_DEAD)
1924 mark_reg_death (XEXP (link, 0));
1926 /* Mark any registers set in INSN as live,
1927 and mark them as conflicting with all other live regs.
1928 Clobbers are processed again, so they conflict with
1929 the registers that are set. */
1931 note_stores (PATTERN (insn), mark_reg_store, NULL);
1933 #ifdef AUTO_INC_DEC
1934 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1935 if (REG_NOTE_KIND (link) == REG_INC)
1936 mark_reg_store (XEXP (link, 0), NULL_RTX, NULL);
1937 #endif
1938 while (n_regs_set-- > 0)
1940 rtx note = find_regno_note (insn, REG_UNUSED,
1941 REGNO (regs_set[n_regs_set]));
1942 if (! note)
1943 continue;
1945 mark_reg_death (XEXP (note, 0));
1949 bitmap_clear (&curr_regs_live);
1950 if (flag_ira_region == IRA_REGION_MIXED
1951 || flag_ira_region == IRA_REGION_ALL)
1952 FOR_EACH_LOOP (loop, 0)
1954 EXECUTE_IF_SET_IN_BITMAP (&LOOP_DATA (loop)->regs_live, 0, j, bi)
1955 if (! bitmap_bit_p (&LOOP_DATA (loop)->regs_ref, j))
1957 enum reg_class pressure_class;
1958 int nregs;
1960 pressure_class = get_regno_pressure_class (j, &nregs);
1961 LOOP_DATA (loop)->max_reg_pressure[pressure_class] -= nregs;
1964 if (dump_file == NULL)
1965 return;
1966 FOR_EACH_LOOP (loop, 0)
1968 parent = loop_outer (loop);
1969 fprintf (dump_file, "\n Loop %d (parent %d, header bb%d, depth %d)\n",
1970 loop->num, (parent == NULL ? -1 : parent->num),
1971 loop->header->index, loop_depth (loop));
1972 fprintf (dump_file, "\n ref. regnos:");
1973 EXECUTE_IF_SET_IN_BITMAP (&LOOP_DATA (loop)->regs_ref, 0, j, bi)
1974 fprintf (dump_file, " %d", j);
1975 fprintf (dump_file, "\n live regnos:");
1976 EXECUTE_IF_SET_IN_BITMAP (&LOOP_DATA (loop)->regs_live, 0, j, bi)
1977 fprintf (dump_file, " %d", j);
1978 fprintf (dump_file, "\n Pressure:");
1979 for (i = 0; (int) i < ira_pressure_classes_num; i++)
1981 enum reg_class pressure_class;
1983 pressure_class = ira_pressure_classes[i];
1984 if (LOOP_DATA (loop)->max_reg_pressure[pressure_class] == 0)
1985 continue;
1986 fprintf (dump_file, " %s=%d", reg_class_names[pressure_class],
1987 LOOP_DATA (loop)->max_reg_pressure[pressure_class]);
1989 fprintf (dump_file, "\n");
1995 /* Move the invariants out of the loops. */
1997 void
1998 move_loop_invariants (void)
2000 struct loop *loop;
2002 if (flag_ira_loop_pressure)
2004 df_analyze ();
2005 regstat_init_n_sets_and_refs ();
2006 ira_set_pseudo_classes (true, dump_file);
2007 calculate_loop_reg_pressure ();
2008 regstat_free_n_sets_and_refs ();
2010 df_set_flags (DF_EQ_NOTES + DF_DEFER_INSN_RESCAN);
2011 /* Process the loops, innermost first. */
2012 FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
2014 curr_loop = loop;
2015 /* move_single_loop_invariants for very large loops
2016 is time consuming and might need a lot of memory. */
2017 if (loop->num_nodes <= (unsigned) LOOP_INVARIANT_MAX_BBS_IN_LOOP)
2018 move_single_loop_invariants (loop);
2021 FOR_EACH_LOOP (loop, 0)
2023 free_loop_data (loop);
2026 if (flag_ira_loop_pressure)
2027 /* There is no sense to keep this info because it was most
2028 probably outdated by subsequent passes. */
2029 free_reg_info ();
2030 free (invariant_table);
2031 invariant_table = NULL;
2032 invariant_table_size = 0;
2034 #ifdef ENABLE_CHECKING
2035 verify_flow_info ();
2036 #endif