2012-07-09 Tom de Vries <tom@codesourcery.com>
[official-gcc.git] / gcc / loop-invariant.c
blobf8405dde268c32f1b8134d604a6844f1df034a43
1 /* RTL-level loop invariant motion.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
10 later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* This implements the loop invariant motion pass. It is very simple
22 (no calls, no loads/stores, etc.). This should be sufficient to cleanup
23 things like address arithmetics -- other more complicated invariants should
24 be eliminated on GIMPLE either in tree-ssa-loop-im.c or in tree-ssa-pre.c.
26 We proceed loop by loop -- it is simpler than trying to handle things
27 globally and should not lose much. First we inspect all sets inside loop
28 and create a dependency graph on insns (saying "to move this insn, you must
29 also move the following insns").
31 We then need to determine what to move. We estimate the number of registers
32 used and move as many invariants as possible while we still have enough free
33 registers. We prefer the expensive invariants.
35 Then we move the selected invariants out of the loop, creating a new
36 temporaries for them if necessary. */
38 #include "config.h"
39 #include "system.h"
40 #include "coretypes.h"
41 #include "tm.h"
42 #include "hard-reg-set.h"
43 #include "rtl.h"
44 #include "tm_p.h"
45 #include "obstack.h"
46 #include "basic-block.h"
47 #include "cfgloop.h"
48 #include "expr.h"
49 #include "recog.h"
50 #include "function.h"
51 #include "flags.h"
52 #include "df.h"
53 #include "hashtab.h"
54 #include "except.h"
55 #include "params.h"
56 #include "regs.h"
57 #include "ira.h"
59 /* The data stored for the loop. */
61 struct loop_data
63 struct loop *outermost_exit; /* The outermost exit of the loop. */
64 bool has_call; /* True if the loop contains a call. */
65 /* Maximal register pressure inside loop for given register class
66 (defined only for the pressure classes). */
67 int max_reg_pressure[N_REG_CLASSES];
68 /* Loop regs referenced and live pseudo-registers. */
69 bitmap_head regs_ref;
70 bitmap_head regs_live;
73 #define LOOP_DATA(LOOP) ((struct loop_data *) (LOOP)->aux)
75 /* The description of an use. */
77 struct use
79 rtx *pos; /* Position of the use. */
80 rtx insn; /* The insn in that the use occurs. */
81 unsigned addr_use_p; /* Whether the use occurs in an address. */
82 struct use *next; /* Next use in the list. */
85 /* The description of a def. */
87 struct def
89 struct use *uses; /* The list of uses that are uniquely reached
90 by it. */
91 unsigned n_uses; /* Number of such uses. */
92 unsigned n_addr_uses; /* Number of uses in addresses. */
93 unsigned invno; /* The corresponding invariant. */
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 /* If we moved the invariant out of the loop, the register that contains its
107 value. */
108 rtx reg;
110 /* If we moved the invariant out of the loop, the original regno
111 that contained its value. */
112 int orig_regno;
114 /* The definition of the invariant. */
115 struct def *def;
117 /* The insn in that it is defined. */
118 rtx insn;
120 /* Whether it is always executed. */
121 bool always_executed;
123 /* Whether to move the invariant. */
124 bool move;
126 /* Whether the invariant is cheap when used as an address. */
127 bool cheap_address;
129 /* Cost of the invariant. */
130 unsigned cost;
132 /* The invariants it depends on. */
133 bitmap depends_on;
135 /* Used for detecting already visited invariants during determining
136 costs of movements. */
137 unsigned stamp;
140 /* Currently processed loop. */
141 static struct loop *curr_loop;
143 /* Table of invariants indexed by the df_ref uid field. */
145 static unsigned int invariant_table_size = 0;
146 static struct invariant ** invariant_table;
148 /* Entry for hash table of invariant expressions. */
150 struct invariant_expr_entry
152 /* The invariant. */
153 struct invariant *inv;
155 /* Its value. */
156 rtx expr;
158 /* Its mode. */
159 enum machine_mode mode;
161 /* Its hash. */
162 hashval_t hash;
165 /* The actual stamp for marking already visited invariants during determining
166 costs of movements. */
168 static unsigned actual_stamp;
170 typedef struct invariant *invariant_p;
172 DEF_VEC_P(invariant_p);
173 DEF_VEC_ALLOC_P(invariant_p, heap);
175 /* The invariants. */
177 static VEC(invariant_p,heap) *invariants;
179 /* Check the size of the invariant table and realloc if necessary. */
181 static void
182 check_invariant_table_size (void)
184 if (invariant_table_size < DF_DEFS_TABLE_SIZE())
186 unsigned int new_size = DF_DEFS_TABLE_SIZE () + (DF_DEFS_TABLE_SIZE () / 4);
187 invariant_table = XRESIZEVEC (struct invariant *, invariant_table, new_size);
188 memset (&invariant_table[invariant_table_size], 0,
189 (new_size - invariant_table_size) * sizeof (struct rtx_iv *));
190 invariant_table_size = new_size;
194 /* Test for possibility of invariantness of X. */
196 static bool
197 check_maybe_invariant (rtx x)
199 enum rtx_code code = GET_CODE (x);
200 int i, j;
201 const char *fmt;
203 switch (code)
205 case CONST_INT:
206 case CONST_DOUBLE:
207 case CONST_FIXED:
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, 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_INT:
305 case CONST_DOUBLE:
306 case CONST_FIXED:
307 case SYMBOL_REF:
308 case CONST:
309 case LABEL_REF:
310 return hash_rtx (x, GET_MODE (x), &do_not_record_p, NULL, false);
312 case REG:
313 use = df_find_use (insn, x);
314 if (!use)
315 return hash_rtx (x, GET_MODE (x), &do_not_record_p, NULL, false);
316 inv = invariant_for_use (use);
317 if (!inv)
318 return hash_rtx (x, GET_MODE (x), &do_not_record_p, NULL, false);
320 gcc_assert (inv->eqto != ~0u);
321 return inv->eqto;
323 default:
324 break;
327 fmt = GET_RTX_FORMAT (code);
328 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
330 if (fmt[i] == 'e')
331 val ^= hash_invariant_expr_1 (insn, XEXP (x, i));
332 else if (fmt[i] == 'E')
334 for (j = 0; j < XVECLEN (x, i); j++)
335 val ^= hash_invariant_expr_1 (insn, XVECEXP (x, i, j));
337 else if (fmt[i] == 'i' || fmt[i] == 'n')
338 val ^= XINT (x, i);
341 return val;
344 /* Returns true if the invariant expressions E1 and E2 used in insns INSN1
345 and INSN2 have always the same value. */
347 static bool
348 invariant_expr_equal_p (rtx insn1, rtx e1, rtx insn2, rtx e2)
350 enum rtx_code code = GET_CODE (e1);
351 int i, j;
352 const char *fmt;
353 df_ref use1, use2;
354 struct invariant *inv1 = NULL, *inv2 = NULL;
355 rtx sub1, sub2;
357 /* If mode of only one of the operands is VOIDmode, it is not equivalent to
358 the other one. If both are VOIDmode, we rely on the caller of this
359 function to verify that their modes are the same. */
360 if (code != GET_CODE (e2) || GET_MODE (e1) != GET_MODE (e2))
361 return false;
363 switch (code)
365 case CONST_INT:
366 case CONST_DOUBLE:
367 case CONST_FIXED:
368 case SYMBOL_REF:
369 case CONST:
370 case LABEL_REF:
371 return rtx_equal_p (e1, e2);
373 case REG:
374 use1 = df_find_use (insn1, e1);
375 use2 = df_find_use (insn2, e2);
376 if (use1)
377 inv1 = invariant_for_use (use1);
378 if (use2)
379 inv2 = invariant_for_use (use2);
381 if (!inv1 && !inv2)
382 return rtx_equal_p (e1, e2);
384 if (!inv1 || !inv2)
385 return false;
387 gcc_assert (inv1->eqto != ~0u);
388 gcc_assert (inv2->eqto != ~0u);
389 return inv1->eqto == inv2->eqto;
391 default:
392 break;
395 fmt = GET_RTX_FORMAT (code);
396 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
398 if (fmt[i] == 'e')
400 sub1 = XEXP (e1, i);
401 sub2 = XEXP (e2, i);
403 if (!invariant_expr_equal_p (insn1, sub1, insn2, sub2))
404 return false;
407 else if (fmt[i] == 'E')
409 if (XVECLEN (e1, i) != XVECLEN (e2, i))
410 return false;
412 for (j = 0; j < XVECLEN (e1, i); j++)
414 sub1 = XVECEXP (e1, i, j);
415 sub2 = XVECEXP (e2, i, j);
417 if (!invariant_expr_equal_p (insn1, sub1, insn2, sub2))
418 return false;
421 else if (fmt[i] == 'i' || fmt[i] == 'n')
423 if (XINT (e1, i) != XINT (e2, i))
424 return false;
426 /* Unhandled type of subexpression, we fail conservatively. */
427 else
428 return false;
431 return true;
434 /* Returns hash value for invariant expression entry E. */
436 static hashval_t
437 hash_invariant_expr (const void *e)
439 const struct invariant_expr_entry *const entry =
440 (const struct invariant_expr_entry *) e;
442 return entry->hash;
445 /* Compares invariant expression entries E1 and E2. */
447 static int
448 eq_invariant_expr (const void *e1, const void *e2)
450 const struct invariant_expr_entry *const entry1 =
451 (const struct invariant_expr_entry *) e1;
452 const struct invariant_expr_entry *const entry2 =
453 (const struct invariant_expr_entry *) e2;
455 if (entry1->mode != entry2->mode)
456 return 0;
458 return invariant_expr_equal_p (entry1->inv->insn, entry1->expr,
459 entry2->inv->insn, entry2->expr);
462 /* Checks whether invariant with value EXPR in machine mode MODE is
463 recorded in EQ. If this is the case, return the invariant. Otherwise
464 insert INV to the table for this expression and return INV. */
466 static struct invariant *
467 find_or_insert_inv (htab_t eq, rtx expr, enum machine_mode mode,
468 struct invariant *inv)
470 hashval_t hash = hash_invariant_expr_1 (inv->insn, expr);
471 struct invariant_expr_entry *entry;
472 struct invariant_expr_entry pentry;
473 PTR *slot;
475 pentry.expr = expr;
476 pentry.inv = inv;
477 pentry.mode = mode;
478 slot = htab_find_slot_with_hash (eq, &pentry, hash, INSERT);
479 entry = (struct invariant_expr_entry *) *slot;
481 if (entry)
482 return entry->inv;
484 entry = XNEW (struct invariant_expr_entry);
485 entry->inv = inv;
486 entry->expr = expr;
487 entry->mode = mode;
488 entry->hash = hash;
489 *slot = entry;
491 return inv;
494 /* Finds invariants identical to INV and records the equivalence. EQ is the
495 hash table of the invariants. */
497 static void
498 find_identical_invariants (htab_t eq, struct invariant *inv)
500 unsigned depno;
501 bitmap_iterator bi;
502 struct invariant *dep;
503 rtx expr, set;
504 enum machine_mode mode;
506 if (inv->eqto != ~0u)
507 return;
509 EXECUTE_IF_SET_IN_BITMAP (inv->depends_on, 0, depno, bi)
511 dep = VEC_index (invariant_p, 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));
520 inv->eqto = find_or_insert_inv (eq, expr, mode, inv)->invno;
522 if (dump_file && inv->eqto != inv->invno)
523 fprintf (dump_file,
524 "Invariant %d is equivalent to invariant %d.\n",
525 inv->invno, inv->eqto);
528 /* Find invariants with the same value and record the equivalences. */
530 static void
531 merge_identical_invariants (void)
533 unsigned i;
534 struct invariant *inv;
535 htab_t eq = htab_create (VEC_length (invariant_p, invariants),
536 hash_invariant_expr, eq_invariant_expr, free);
538 FOR_EACH_VEC_ELT (invariant_p, invariants, i, inv)
539 find_identical_invariants (eq, inv);
541 htab_delete (eq);
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;
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, basic_block *body)
661 unsigned i;
662 bitmap blocks = BITMAP_ALLOC (NULL);
664 for (i = 0; i < loop->num_nodes; i++)
665 bitmap_set_bit (blocks, body[i]->index);
667 df_remove_problem (df_chain);
668 df_process_deferred_rescans ();
669 df_chain_add_problem (DF_UD_CHAIN);
670 df_set_blocks (blocks);
671 df_analyze ();
673 if (dump_file)
675 df_dump_region (dump_file);
676 fprintf (dump_file, "*****starting processing of loop ******\n");
677 print_rtl_with_bb (dump_file, get_insns ());
678 fprintf (dump_file, "*****ending processing of loop ******\n");
680 check_invariant_table_size ();
682 BITMAP_FREE (blocks);
685 /* Creates a new invariant for definition DEF in INSN, depending on invariants
686 in DEPENDS_ON. ALWAYS_EXECUTED is true if the insn is always executed,
687 unless the program ends due to a function call. The newly created invariant
688 is returned. */
690 static struct invariant *
691 create_new_invariant (struct def *def, rtx insn, bitmap depends_on,
692 bool always_executed)
694 struct invariant *inv = XNEW (struct invariant);
695 rtx set = single_set (insn);
696 bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn));
698 inv->def = def;
699 inv->always_executed = always_executed;
700 inv->depends_on = depends_on;
702 /* If the set is simple, usually by moving it we move the whole store out of
703 the loop. Otherwise we save only cost of the computation. */
704 if (def)
706 inv->cost = set_rtx_cost (set, speed);
707 /* ??? Try to determine cheapness of address computation. Unfortunately
708 the address cost is only a relative measure, we can't really compare
709 it with any absolute number, but only with other address costs.
710 But here we don't have any other addresses, so compare with a magic
711 number anyway. It has to be large enough to not regress PR33928
712 (by avoiding to move reg+8,reg+16,reg+24 invariants), but small
713 enough to not regress 410.bwaves either (by still moving reg+reg
714 invariants).
715 See http://gcc.gnu.org/ml/gcc-patches/2009-10/msg01210.html . */
716 inv->cheap_address = address_cost (SET_SRC (set), word_mode,
717 ADDR_SPACE_GENERIC, speed) < 3;
719 else
721 inv->cost = set_src_cost (SET_SRC (set), speed);
722 inv->cheap_address = false;
725 inv->move = false;
726 inv->reg = NULL_RTX;
727 inv->orig_regno = -1;
728 inv->stamp = 0;
729 inv->insn = insn;
731 inv->invno = VEC_length (invariant_p, invariants);
732 inv->eqto = ~0u;
733 if (def)
734 def->invno = inv->invno;
735 VEC_safe_push (invariant_p, heap, invariants, inv);
737 if (dump_file)
739 fprintf (dump_file,
740 "Set in insn %d is invariant (%d), cost %d, depends on ",
741 INSN_UID (insn), inv->invno, inv->cost);
742 dump_bitmap (dump_file, inv->depends_on);
745 return inv;
748 /* Record USE at DEF. */
750 static void
751 record_use (struct def *def, df_ref use)
753 struct use *u = XNEW (struct use);
755 u->pos = DF_REF_REAL_LOC (use);
756 u->insn = DF_REF_INSN (use);
757 u->addr_use_p = (DF_REF_TYPE (use) == DF_REF_REG_MEM_LOAD
758 || DF_REF_TYPE (use) == DF_REF_REG_MEM_STORE);
759 u->next = def->uses;
760 def->uses = u;
761 def->n_uses++;
762 if (u->addr_use_p)
763 def->n_addr_uses++;
766 /* Finds the invariants USE depends on and store them to the DEPENDS_ON
767 bitmap. Returns true if all dependencies of USE are known to be
768 loop invariants, false otherwise. */
770 static bool
771 check_dependency (basic_block bb, df_ref use, bitmap depends_on)
773 df_ref def;
774 basic_block def_bb;
775 struct df_link *defs;
776 struct def *def_data;
777 struct invariant *inv;
779 if (DF_REF_FLAGS (use) & DF_REF_READ_WRITE)
780 return false;
782 defs = DF_REF_CHAIN (use);
783 if (!defs)
784 return true;
786 if (defs->next)
787 return false;
789 def = defs->ref;
790 check_invariant_table_size ();
791 inv = invariant_table[DF_REF_ID(def)];
792 if (!inv)
793 return false;
795 def_data = inv->def;
796 gcc_assert (def_data != NULL);
798 def_bb = DF_REF_BB (def);
799 /* Note that in case bb == def_bb, we know that the definition
800 dominates insn, because def has invariant_table[DF_REF_ID(def)]
801 defined and we process the insns in the basic block bb
802 sequentially. */
803 if (!dominated_by_p (CDI_DOMINATORS, bb, def_bb))
804 return false;
806 bitmap_set_bit (depends_on, def_data->invno);
807 return true;
811 /* Finds the invariants INSN depends on and store them to the DEPENDS_ON
812 bitmap. Returns true if all dependencies of INSN are known to be
813 loop invariants, false otherwise. */
815 static bool
816 check_dependencies (rtx insn, bitmap depends_on)
818 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
819 df_ref *use_rec;
820 basic_block bb = BLOCK_FOR_INSN (insn);
822 for (use_rec = DF_INSN_INFO_USES (insn_info); *use_rec; use_rec++)
823 if (!check_dependency (bb, *use_rec, depends_on))
824 return false;
825 for (use_rec = DF_INSN_INFO_EQ_USES (insn_info); *use_rec; use_rec++)
826 if (!check_dependency (bb, *use_rec, depends_on))
827 return false;
829 return true;
832 /* Finds invariant in INSN. ALWAYS_REACHED is true if the insn is always
833 executed. ALWAYS_EXECUTED is true if the insn is always executed,
834 unless the program ends due to a function call. */
836 static void
837 find_invariant_insn (rtx insn, bool always_reached, bool always_executed)
839 df_ref ref;
840 struct def *def;
841 bitmap depends_on;
842 rtx set, dest;
843 bool simple = true;
844 struct invariant *inv;
846 #ifdef HAVE_cc0
847 /* We can't move a CC0 setter without the user. */
848 if (sets_cc0_p (insn))
849 return;
850 #endif
852 set = single_set (insn);
853 if (!set)
854 return;
855 dest = SET_DEST (set);
857 if (!REG_P (dest)
858 || HARD_REGISTER_P (dest))
859 simple = false;
861 if (!may_assign_reg_p (SET_DEST (set))
862 || !check_maybe_invariant (SET_SRC (set)))
863 return;
865 /* If the insn can throw exception, we cannot move it at all without changing
866 cfg. */
867 if (can_throw_internal (insn))
868 return;
870 /* We cannot make trapping insn executed, unless it was executed before. */
871 if (may_trap_or_fault_p (PATTERN (insn)) && !always_reached)
872 return;
874 depends_on = BITMAP_ALLOC (NULL);
875 if (!check_dependencies (insn, depends_on))
877 BITMAP_FREE (depends_on);
878 return;
881 if (simple)
882 def = XCNEW (struct def);
883 else
884 def = NULL;
886 inv = create_new_invariant (def, insn, depends_on, always_executed);
888 if (simple)
890 ref = df_find_def (insn, dest);
891 check_invariant_table_size ();
892 invariant_table[DF_REF_ID(ref)] = inv;
896 /* Record registers used in INSN that have a unique invariant definition. */
898 static void
899 record_uses (rtx insn)
901 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
902 df_ref *use_rec;
903 struct invariant *inv;
905 for (use_rec = DF_INSN_INFO_USES (insn_info); *use_rec; use_rec++)
907 df_ref use = *use_rec;
908 inv = invariant_for_use (use);
909 if (inv)
910 record_use (inv->def, use);
912 for (use_rec = DF_INSN_INFO_EQ_USES (insn_info); *use_rec; use_rec++)
914 df_ref use = *use_rec;
915 inv = invariant_for_use (use);
916 if (inv)
917 record_use (inv->def, use);
921 /* Finds invariants in INSN. ALWAYS_REACHED is true if the insn is always
922 executed. ALWAYS_EXECUTED is true if the insn is always executed,
923 unless the program ends due to a function call. */
925 static void
926 find_invariants_insn (rtx insn, bool always_reached, bool always_executed)
928 find_invariant_insn (insn, always_reached, always_executed);
929 record_uses (insn);
932 /* Finds invariants in basic block BB. ALWAYS_REACHED is true if the
933 basic block is always executed. ALWAYS_EXECUTED is true if the basic
934 block is always executed, unless the program ends due to a function
935 call. */
937 static void
938 find_invariants_bb (basic_block bb, bool always_reached, bool always_executed)
940 rtx insn;
942 FOR_BB_INSNS (bb, insn)
944 if (!NONDEBUG_INSN_P (insn))
945 continue;
947 find_invariants_insn (insn, always_reached, always_executed);
949 if (always_reached
950 && CALL_P (insn)
951 && (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)
952 || ! RTL_CONST_OR_PURE_CALL_P (insn)))
953 always_reached = false;
957 /* Finds invariants in LOOP with body BODY. ALWAYS_REACHED is the bitmap of
958 basic blocks in BODY that are always executed. ALWAYS_EXECUTED is the
959 bitmap of basic blocks in BODY that are always executed unless the program
960 ends due to a function call. */
962 static void
963 find_invariants_body (struct loop *loop, basic_block *body,
964 bitmap always_reached, bitmap always_executed)
966 unsigned i;
968 for (i = 0; i < loop->num_nodes; i++)
969 find_invariants_bb (body[i],
970 bitmap_bit_p (always_reached, i),
971 bitmap_bit_p (always_executed, i));
974 /* Finds invariants in LOOP. */
976 static void
977 find_invariants (struct loop *loop)
979 bitmap may_exit = BITMAP_ALLOC (NULL);
980 bitmap always_reached = BITMAP_ALLOC (NULL);
981 bitmap has_exit = BITMAP_ALLOC (NULL);
982 bitmap always_executed = BITMAP_ALLOC (NULL);
983 basic_block *body = get_loop_body_in_dom_order (loop);
985 find_exits (loop, body, may_exit, has_exit);
986 compute_always_reached (loop, body, may_exit, always_reached);
987 compute_always_reached (loop, body, has_exit, always_executed);
989 find_defs (loop, body);
990 find_invariants_body (loop, body, always_reached, always_executed);
991 merge_identical_invariants ();
993 BITMAP_FREE (always_reached);
994 BITMAP_FREE (always_executed);
995 BITMAP_FREE (may_exit);
996 BITMAP_FREE (has_exit);
997 free (body);
1000 /* Frees a list of uses USE. */
1002 static void
1003 free_use_list (struct use *use)
1005 struct use *next;
1007 for (; use; use = next)
1009 next = use->next;
1010 free (use);
1014 /* Return pressure class and number of hard registers (through *NREGS)
1015 for destination of INSN. */
1016 static enum reg_class
1017 get_pressure_class_and_nregs (rtx insn, int *nregs)
1019 rtx reg;
1020 enum reg_class pressure_class;
1021 rtx set = single_set (insn);
1023 /* Considered invariant insns have only one set. */
1024 gcc_assert (set != NULL_RTX);
1025 reg = SET_DEST (set);
1026 if (GET_CODE (reg) == SUBREG)
1027 reg = SUBREG_REG (reg);
1028 if (MEM_P (reg))
1030 *nregs = 0;
1031 pressure_class = NO_REGS;
1033 else
1035 if (! REG_P (reg))
1036 reg = NULL_RTX;
1037 if (reg == NULL_RTX)
1038 pressure_class = GENERAL_REGS;
1039 else
1041 pressure_class = reg_allocno_class (REGNO (reg));
1042 pressure_class = ira_pressure_class_translate[pressure_class];
1044 *nregs
1045 = ira_reg_class_max_nregs[pressure_class][GET_MODE (SET_SRC (set))];
1047 return pressure_class;
1050 /* Calculates cost and number of registers needed for moving invariant INV
1051 out of the loop and stores them to *COST and *REGS_NEEDED. */
1053 static void
1054 get_inv_cost (struct invariant *inv, int *comp_cost, unsigned *regs_needed)
1056 int i, acomp_cost;
1057 unsigned aregs_needed[N_REG_CLASSES];
1058 unsigned depno;
1059 struct invariant *dep;
1060 bitmap_iterator bi;
1062 /* Find the representative of the class of the equivalent invariants. */
1063 inv = VEC_index (invariant_p, invariants, inv->eqto);
1065 *comp_cost = 0;
1066 if (! flag_ira_loop_pressure)
1067 regs_needed[0] = 0;
1068 else
1070 for (i = 0; i < ira_pressure_classes_num; i++)
1071 regs_needed[ira_pressure_classes[i]] = 0;
1074 if (inv->move
1075 || inv->stamp == actual_stamp)
1076 return;
1077 inv->stamp = actual_stamp;
1079 if (! flag_ira_loop_pressure)
1080 regs_needed[0]++;
1081 else
1083 int nregs;
1084 enum reg_class pressure_class;
1086 pressure_class = get_pressure_class_and_nregs (inv->insn, &nregs);
1087 regs_needed[pressure_class] += nregs;
1090 if (!inv->cheap_address
1091 || inv->def->n_addr_uses < inv->def->n_uses)
1092 (*comp_cost) += inv->cost;
1094 #ifdef STACK_REGS
1096 /* Hoisting constant pool constants into stack regs may cost more than
1097 just single register. On x87, the balance is affected both by the
1098 small number of FP registers, and by its register stack organization,
1099 that forces us to add compensation code in and around the loop to
1100 shuffle the operands to the top of stack before use, and pop them
1101 from the stack after the loop finishes.
1103 To model this effect, we increase the number of registers needed for
1104 stack registers by two: one register push, and one register pop.
1105 This usually has the effect that FP constant loads from the constant
1106 pool are not moved out of the loop.
1108 Note that this also means that dependent invariants can not be moved.
1109 However, the primary purpose of this pass is to move loop invariant
1110 address arithmetic out of loops, and address arithmetic that depends
1111 on floating point constants is unlikely to ever occur. */
1112 rtx set = single_set (inv->insn);
1113 if (set
1114 && IS_STACK_MODE (GET_MODE (SET_SRC (set)))
1115 && constant_pool_constant_p (SET_SRC (set)))
1117 if (flag_ira_loop_pressure)
1118 regs_needed[ira_stack_reg_pressure_class] += 2;
1119 else
1120 regs_needed[0] += 2;
1123 #endif
1125 EXECUTE_IF_SET_IN_BITMAP (inv->depends_on, 0, depno, bi)
1127 bool check_p;
1129 dep = VEC_index (invariant_p, invariants, depno);
1131 get_inv_cost (dep, &acomp_cost, aregs_needed);
1133 if (! flag_ira_loop_pressure)
1134 check_p = aregs_needed[0] != 0;
1135 else
1137 for (i = 0; i < ira_pressure_classes_num; i++)
1138 if (aregs_needed[ira_pressure_classes[i]] != 0)
1139 break;
1140 check_p = i < ira_pressure_classes_num;
1142 if (check_p
1143 /* We need to check always_executed, since if the original value of
1144 the invariant may be preserved, we may need to keep it in a
1145 separate register. TODO check whether the register has an
1146 use outside of the loop. */
1147 && dep->always_executed
1148 && !dep->def->uses->next)
1150 /* If this is a single use, after moving the dependency we will not
1151 need a new register. */
1152 if (! flag_ira_loop_pressure)
1153 aregs_needed[0]--;
1154 else
1156 int nregs;
1157 enum reg_class pressure_class;
1159 pressure_class = get_pressure_class_and_nregs (inv->insn, &nregs);
1160 aregs_needed[pressure_class] -= nregs;
1164 if (! flag_ira_loop_pressure)
1165 regs_needed[0] += aregs_needed[0];
1166 else
1168 for (i = 0; i < ira_pressure_classes_num; i++)
1169 regs_needed[ira_pressure_classes[i]]
1170 += aregs_needed[ira_pressure_classes[i]];
1172 (*comp_cost) += acomp_cost;
1176 /* Calculates gain for eliminating invariant INV. REGS_USED is the number
1177 of registers used in the loop, NEW_REGS is the number of new variables
1178 already added due to the invariant motion. The number of registers needed
1179 for it is stored in *REGS_NEEDED. SPEED and CALL_P are flags passed
1180 through to estimate_reg_pressure_cost. */
1182 static int
1183 gain_for_invariant (struct invariant *inv, unsigned *regs_needed,
1184 unsigned *new_regs, unsigned regs_used,
1185 bool speed, bool call_p)
1187 int comp_cost, size_cost;
1189 actual_stamp++;
1191 get_inv_cost (inv, &comp_cost, regs_needed);
1193 if (! flag_ira_loop_pressure)
1195 size_cost = (estimate_reg_pressure_cost (new_regs[0] + regs_needed[0],
1196 regs_used, speed, call_p)
1197 - estimate_reg_pressure_cost (new_regs[0],
1198 regs_used, speed, call_p));
1200 else
1202 int i;
1203 enum reg_class pressure_class;
1205 for (i = 0; i < ira_pressure_classes_num; i++)
1207 pressure_class = ira_pressure_classes[i];
1208 if ((int) new_regs[pressure_class]
1209 + (int) regs_needed[pressure_class]
1210 + LOOP_DATA (curr_loop)->max_reg_pressure[pressure_class]
1211 + IRA_LOOP_RESERVED_REGS
1212 > ira_class_hard_regs_num[pressure_class])
1213 break;
1215 if (i < ira_pressure_classes_num)
1216 /* There will be register pressure excess and we want not to
1217 make this loop invariant motion. All loop invariants with
1218 non-positive gains will be rejected in function
1219 find_invariants_to_move. Therefore we return the negative
1220 number here.
1222 One could think that this rejects also expensive loop
1223 invariant motions and this will hurt code performance.
1224 However numerous experiments with different heuristics
1225 taking invariant cost into account did not confirm this
1226 assumption. There are possible explanations for this
1227 result:
1228 o probably all expensive invariants were already moved out
1229 of the loop by PRE and gimple invariant motion pass.
1230 o expensive invariant execution will be hidden by insn
1231 scheduling or OOO processor hardware because usually such
1232 invariants have a lot of freedom to be executed
1233 out-of-order.
1234 Another reason for ignoring invariant cost vs spilling cost
1235 heuristics is also in difficulties to evaluate accurately
1236 spill cost at this stage. */
1237 return -1;
1238 else
1239 size_cost = 0;
1242 return comp_cost - size_cost;
1245 /* Finds invariant with best gain for moving. Returns the gain, stores
1246 the invariant in *BEST and number of registers needed for it to
1247 *REGS_NEEDED. REGS_USED is the number of registers used in the loop.
1248 NEW_REGS is the number of new variables already added due to invariant
1249 motion. */
1251 static int
1252 best_gain_for_invariant (struct invariant **best, unsigned *regs_needed,
1253 unsigned *new_regs, unsigned regs_used,
1254 bool speed, bool call_p)
1256 struct invariant *inv;
1257 int i, gain = 0, again;
1258 unsigned aregs_needed[N_REG_CLASSES], invno;
1260 FOR_EACH_VEC_ELT (invariant_p, invariants, invno, inv)
1262 if (inv->move)
1263 continue;
1265 /* Only consider the "representatives" of equivalent invariants. */
1266 if (inv->eqto != inv->invno)
1267 continue;
1269 again = gain_for_invariant (inv, aregs_needed, new_regs, regs_used,
1270 speed, call_p);
1271 if (again > gain)
1273 gain = again;
1274 *best = inv;
1275 if (! flag_ira_loop_pressure)
1276 regs_needed[0] = aregs_needed[0];
1277 else
1279 for (i = 0; i < ira_pressure_classes_num; i++)
1280 regs_needed[ira_pressure_classes[i]]
1281 = aregs_needed[ira_pressure_classes[i]];
1286 return gain;
1289 /* Marks invariant INVNO and all its dependencies for moving. */
1291 static void
1292 set_move_mark (unsigned invno, int gain)
1294 struct invariant *inv = VEC_index (invariant_p, invariants, invno);
1295 bitmap_iterator bi;
1297 /* Find the representative of the class of the equivalent invariants. */
1298 inv = VEC_index (invariant_p, invariants, inv->eqto);
1300 if (inv->move)
1301 return;
1302 inv->move = true;
1304 if (dump_file)
1306 if (gain >= 0)
1307 fprintf (dump_file, "Decided to move invariant %d -- gain %d\n",
1308 invno, gain);
1309 else
1310 fprintf (dump_file, "Decided to move dependent invariant %d\n",
1311 invno);
1314 EXECUTE_IF_SET_IN_BITMAP (inv->depends_on, 0, invno, bi)
1316 set_move_mark (invno, -1);
1320 /* Determines which invariants to move. */
1322 static void
1323 find_invariants_to_move (bool speed, bool call_p)
1325 int gain;
1326 unsigned i, regs_used, regs_needed[N_REG_CLASSES], new_regs[N_REG_CLASSES];
1327 struct invariant *inv = NULL;
1329 if (!VEC_length (invariant_p, invariants))
1330 return;
1332 if (flag_ira_loop_pressure)
1333 /* REGS_USED is actually never used when the flag is on. */
1334 regs_used = 0;
1335 else
1336 /* We do not really do a good job in estimating number of
1337 registers used; we put some initial bound here to stand for
1338 induction variables etc. that we do not detect. */
1340 unsigned int n_regs = DF_REG_SIZE (df);
1342 regs_used = 2;
1344 for (i = 0; i < n_regs; i++)
1346 if (!DF_REGNO_FIRST_DEF (i) && DF_REGNO_LAST_USE (i))
1348 /* This is a value that is used but not changed inside loop. */
1349 regs_used++;
1354 if (! flag_ira_loop_pressure)
1355 new_regs[0] = regs_needed[0] = 0;
1356 else
1358 for (i = 0; (int) i < ira_pressure_classes_num; i++)
1359 new_regs[ira_pressure_classes[i]] = 0;
1361 while ((gain = best_gain_for_invariant (&inv, regs_needed,
1362 new_regs, regs_used,
1363 speed, call_p)) > 0)
1365 set_move_mark (inv->invno, gain);
1366 if (! flag_ira_loop_pressure)
1367 new_regs[0] += regs_needed[0];
1368 else
1370 for (i = 0; (int) i < ira_pressure_classes_num; i++)
1371 new_regs[ira_pressure_classes[i]]
1372 += regs_needed[ira_pressure_classes[i]];
1377 /* Replace the uses, reached by the definition of invariant INV, by REG.
1379 IN_GROUP is nonzero if this is part of a group of changes that must be
1380 performed as a group. In that case, the changes will be stored. The
1381 function `apply_change_group' will validate and apply the changes. */
1383 static int
1384 replace_uses (struct invariant *inv, rtx reg, bool in_group)
1386 /* Replace the uses we know to be dominated. It saves work for copy
1387 propagation, and also it is necessary so that dependent invariants
1388 are computed right. */
1389 if (inv->def)
1391 struct use *use;
1392 for (use = inv->def->uses; use; use = use->next)
1393 validate_change (use->insn, use->pos, reg, true);
1395 /* If we aren't part of a larger group, apply the changes now. */
1396 if (!in_group)
1397 return apply_change_group ();
1400 return 1;
1403 /* Move invariant INVNO out of the LOOP. Returns true if this succeeds, false
1404 otherwise. */
1406 static bool
1407 move_invariant_reg (struct loop *loop, unsigned invno)
1409 struct invariant *inv = VEC_index (invariant_p, invariants, invno);
1410 struct invariant *repr = VEC_index (invariant_p, invariants, inv->eqto);
1411 unsigned i;
1412 basic_block preheader = loop_preheader_edge (loop)->src;
1413 rtx reg, set, dest, note;
1414 bitmap_iterator bi;
1415 int regno = -1;
1417 if (inv->reg)
1418 return true;
1419 if (!repr->move)
1420 return false;
1422 /* If this is a representative of the class of equivalent invariants,
1423 really move the invariant. Otherwise just replace its use with
1424 the register used for the representative. */
1425 if (inv == repr)
1427 if (inv->depends_on)
1429 EXECUTE_IF_SET_IN_BITMAP (inv->depends_on, 0, i, bi)
1431 if (!move_invariant_reg (loop, i))
1432 goto fail;
1436 /* Move the set out of the loop. If the set is always executed (we could
1437 omit this condition if we know that the register is unused outside of
1438 the loop, but it does not seem worth finding out) and it has no uses
1439 that would not be dominated by it, we may just move it (TODO).
1440 Otherwise we need to create a temporary register. */
1441 set = single_set (inv->insn);
1442 reg = dest = SET_DEST (set);
1443 if (GET_CODE (reg) == SUBREG)
1444 reg = SUBREG_REG (reg);
1445 if (REG_P (reg))
1446 regno = REGNO (reg);
1448 reg = gen_reg_rtx_and_attrs (dest);
1450 /* Try replacing the destination by a new pseudoregister. */
1451 validate_change (inv->insn, &SET_DEST (set), reg, true);
1453 /* As well as all the dominated uses. */
1454 replace_uses (inv, reg, true);
1456 /* And validate all the changes. */
1457 if (!apply_change_group ())
1458 goto fail;
1460 emit_insn_after (gen_move_insn (dest, reg), inv->insn);
1461 reorder_insns (inv->insn, inv->insn, BB_END (preheader));
1463 /* If there is a REG_EQUAL note on the insn we just moved, and the
1464 insn is in a basic block that is not always executed or the note
1465 contains something for which we don't know the invariant status,
1466 the note may no longer be valid after we move the insn. Note that
1467 uses in REG_EQUAL notes are taken into account in the computation
1468 of invariants, so it is safe to retain the note even if it contains
1469 register references for which we know the invariant status. */
1470 if ((note = find_reg_note (inv->insn, REG_EQUAL, NULL_RTX))
1471 && (!inv->always_executed
1472 || !check_maybe_invariant (XEXP (note, 0))))
1473 remove_note (inv->insn, note);
1475 else
1477 if (!move_invariant_reg (loop, repr->invno))
1478 goto fail;
1479 reg = repr->reg;
1480 regno = repr->orig_regno;
1481 if (!replace_uses (inv, reg, false))
1482 goto fail;
1483 set = single_set (inv->insn);
1484 emit_insn_after (gen_move_insn (SET_DEST (set), reg), inv->insn);
1485 delete_insn (inv->insn);
1488 inv->reg = reg;
1489 inv->orig_regno = regno;
1491 return true;
1493 fail:
1494 /* If we failed, clear move flag, so that we do not try to move inv
1495 again. */
1496 if (dump_file)
1497 fprintf (dump_file, "Failed to move invariant %d\n", invno);
1498 inv->move = false;
1499 inv->reg = NULL_RTX;
1500 inv->orig_regno = -1;
1502 return false;
1505 /* Move selected invariant out of the LOOP. Newly created regs are marked
1506 in TEMPORARY_REGS. */
1508 static void
1509 move_invariants (struct loop *loop)
1511 struct invariant *inv;
1512 unsigned i;
1514 FOR_EACH_VEC_ELT (invariant_p, invariants, i, inv)
1515 move_invariant_reg (loop, i);
1516 if (flag_ira_loop_pressure && resize_reg_info ())
1518 FOR_EACH_VEC_ELT (invariant_p, invariants, i, inv)
1519 if (inv->reg != NULL_RTX)
1521 if (inv->orig_regno >= 0)
1522 setup_reg_classes (REGNO (inv->reg),
1523 reg_preferred_class (inv->orig_regno),
1524 reg_alternate_class (inv->orig_regno),
1525 reg_allocno_class (inv->orig_regno));
1526 else
1527 setup_reg_classes (REGNO (inv->reg),
1528 GENERAL_REGS, NO_REGS, GENERAL_REGS);
1533 /* Initializes invariant motion data. */
1535 static void
1536 init_inv_motion_data (void)
1538 actual_stamp = 1;
1540 invariants = VEC_alloc (invariant_p, heap, 100);
1543 /* Frees the data allocated by invariant motion. */
1545 static void
1546 free_inv_motion_data (void)
1548 unsigned i;
1549 struct def *def;
1550 struct invariant *inv;
1552 check_invariant_table_size ();
1553 for (i = 0; i < DF_DEFS_TABLE_SIZE (); i++)
1555 inv = invariant_table[i];
1556 if (inv)
1558 def = inv->def;
1559 gcc_assert (def != NULL);
1561 free_use_list (def->uses);
1562 free (def);
1563 invariant_table[i] = NULL;
1567 FOR_EACH_VEC_ELT (invariant_p, invariants, i, inv)
1569 BITMAP_FREE (inv->depends_on);
1570 free (inv);
1572 VEC_free (invariant_p, heap, invariants);
1575 /* Move the invariants out of the LOOP. */
1577 static void
1578 move_single_loop_invariants (struct loop *loop)
1580 init_inv_motion_data ();
1582 find_invariants (loop);
1583 find_invariants_to_move (optimize_loop_for_speed_p (loop),
1584 LOOP_DATA (loop)->has_call);
1585 move_invariants (loop);
1587 free_inv_motion_data ();
1590 /* Releases the auxiliary data for LOOP. */
1592 static void
1593 free_loop_data (struct loop *loop)
1595 struct loop_data *data = LOOP_DATA (loop);
1596 if (!data)
1597 return;
1599 bitmap_clear (&LOOP_DATA (loop)->regs_ref);
1600 bitmap_clear (&LOOP_DATA (loop)->regs_live);
1601 free (data);
1602 loop->aux = NULL;
1607 /* Registers currently living. */
1608 static bitmap_head curr_regs_live;
1610 /* Current reg pressure for each pressure class. */
1611 static int curr_reg_pressure[N_REG_CLASSES];
1613 /* Record all regs that are set in any one insn. Communication from
1614 mark_reg_{store,clobber} and global_conflicts. Asm can refer to
1615 all hard-registers. */
1616 static rtx regs_set[(FIRST_PSEUDO_REGISTER > MAX_RECOG_OPERANDS
1617 ? FIRST_PSEUDO_REGISTER : MAX_RECOG_OPERANDS) * 2];
1618 /* Number of regs stored in the previous array. */
1619 static int n_regs_set;
1621 /* Return pressure class and number of needed hard registers (through
1622 *NREGS) of register REGNO. */
1623 static enum reg_class
1624 get_regno_pressure_class (int regno, int *nregs)
1626 if (regno >= FIRST_PSEUDO_REGISTER)
1628 enum reg_class pressure_class;
1630 pressure_class = reg_allocno_class (regno);
1631 pressure_class = ira_pressure_class_translate[pressure_class];
1632 *nregs
1633 = ira_reg_class_max_nregs[pressure_class][PSEUDO_REGNO_MODE (regno)];
1634 return pressure_class;
1636 else if (! TEST_HARD_REG_BIT (ira_no_alloc_regs, regno)
1637 && ! TEST_HARD_REG_BIT (eliminable_regset, regno))
1639 *nregs = 1;
1640 return ira_pressure_class_translate[REGNO_REG_CLASS (regno)];
1642 else
1644 *nregs = 0;
1645 return NO_REGS;
1649 /* Increase (if INCR_P) or decrease current register pressure for
1650 register REGNO. */
1651 static void
1652 change_pressure (int regno, bool incr_p)
1654 int nregs;
1655 enum reg_class pressure_class;
1657 pressure_class = get_regno_pressure_class (regno, &nregs);
1658 if (! incr_p)
1659 curr_reg_pressure[pressure_class] -= nregs;
1660 else
1662 curr_reg_pressure[pressure_class] += nregs;
1663 if (LOOP_DATA (curr_loop)->max_reg_pressure[pressure_class]
1664 < curr_reg_pressure[pressure_class])
1665 LOOP_DATA (curr_loop)->max_reg_pressure[pressure_class]
1666 = curr_reg_pressure[pressure_class];
1670 /* Mark REGNO birth. */
1671 static void
1672 mark_regno_live (int regno)
1674 struct loop *loop;
1676 for (loop = curr_loop;
1677 loop != current_loops->tree_root;
1678 loop = loop_outer (loop))
1679 bitmap_set_bit (&LOOP_DATA (loop)->regs_live, regno);
1680 if (!bitmap_set_bit (&curr_regs_live, regno))
1681 return;
1682 change_pressure (regno, true);
1685 /* Mark REGNO death. */
1686 static void
1687 mark_regno_death (int regno)
1689 if (! bitmap_clear_bit (&curr_regs_live, regno))
1690 return;
1691 change_pressure (regno, false);
1694 /* Mark setting register REG. */
1695 static void
1696 mark_reg_store (rtx reg, const_rtx setter ATTRIBUTE_UNUSED,
1697 void *data ATTRIBUTE_UNUSED)
1699 int regno;
1701 if (GET_CODE (reg) == SUBREG)
1702 reg = SUBREG_REG (reg);
1704 if (! REG_P (reg))
1705 return;
1707 regs_set[n_regs_set++] = reg;
1709 regno = REGNO (reg);
1711 if (regno >= FIRST_PSEUDO_REGISTER)
1712 mark_regno_live (regno);
1713 else
1715 int last = regno + hard_regno_nregs[regno][GET_MODE (reg)];
1717 while (regno < last)
1719 mark_regno_live (regno);
1720 regno++;
1725 /* Mark clobbering register REG. */
1726 static void
1727 mark_reg_clobber (rtx reg, const_rtx setter, void *data)
1729 if (GET_CODE (setter) == CLOBBER)
1730 mark_reg_store (reg, setter, data);
1733 /* Mark register REG death. */
1734 static void
1735 mark_reg_death (rtx reg)
1737 int regno = REGNO (reg);
1739 if (regno >= FIRST_PSEUDO_REGISTER)
1740 mark_regno_death (regno);
1741 else
1743 int last = regno + hard_regno_nregs[regno][GET_MODE (reg)];
1745 while (regno < last)
1747 mark_regno_death (regno);
1748 regno++;
1753 /* Mark occurrence of registers in X for the current loop. */
1754 static void
1755 mark_ref_regs (rtx x)
1757 RTX_CODE code;
1758 int i;
1759 const char *fmt;
1761 if (!x)
1762 return;
1764 code = GET_CODE (x);
1765 if (code == REG)
1767 struct loop *loop;
1769 for (loop = curr_loop;
1770 loop != current_loops->tree_root;
1771 loop = loop_outer (loop))
1772 bitmap_set_bit (&LOOP_DATA (loop)->regs_ref, REGNO (x));
1773 return;
1776 fmt = GET_RTX_FORMAT (code);
1777 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1778 if (fmt[i] == 'e')
1779 mark_ref_regs (XEXP (x, i));
1780 else if (fmt[i] == 'E')
1782 int j;
1784 for (j = 0; j < XVECLEN (x, i); j++)
1785 mark_ref_regs (XVECEXP (x, i, j));
1789 /* Calculate register pressure in the loops. */
1790 static void
1791 calculate_loop_reg_pressure (void)
1793 int i;
1794 unsigned int j;
1795 bitmap_iterator bi;
1796 basic_block bb;
1797 rtx insn, link;
1798 struct loop *loop, *parent;
1799 loop_iterator li;
1801 FOR_EACH_LOOP (li, loop, 0)
1802 if (loop->aux == NULL)
1804 loop->aux = xcalloc (1, sizeof (struct loop_data));
1805 bitmap_initialize (&LOOP_DATA (loop)->regs_ref, &reg_obstack);
1806 bitmap_initialize (&LOOP_DATA (loop)->regs_live, &reg_obstack);
1808 ira_setup_eliminable_regset ();
1809 bitmap_initialize (&curr_regs_live, &reg_obstack);
1810 FOR_EACH_BB (bb)
1812 curr_loop = bb->loop_father;
1813 if (curr_loop == current_loops->tree_root)
1814 continue;
1816 for (loop = curr_loop;
1817 loop != current_loops->tree_root;
1818 loop = loop_outer (loop))
1819 bitmap_ior_into (&LOOP_DATA (loop)->regs_live, DF_LR_IN (bb));
1821 bitmap_copy (&curr_regs_live, DF_LR_IN (bb));
1822 for (i = 0; i < ira_pressure_classes_num; i++)
1823 curr_reg_pressure[ira_pressure_classes[i]] = 0;
1824 EXECUTE_IF_SET_IN_BITMAP (&curr_regs_live, 0, j, bi)
1825 change_pressure (j, true);
1827 FOR_BB_INSNS (bb, insn)
1829 if (! NONDEBUG_INSN_P (insn))
1830 continue;
1832 mark_ref_regs (PATTERN (insn));
1833 n_regs_set = 0;
1834 note_stores (PATTERN (insn), mark_reg_clobber, NULL);
1836 /* Mark any registers dead after INSN as dead now. */
1838 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1839 if (REG_NOTE_KIND (link) == REG_DEAD)
1840 mark_reg_death (XEXP (link, 0));
1842 /* Mark any registers set in INSN as live,
1843 and mark them as conflicting with all other live regs.
1844 Clobbers are processed again, so they conflict with
1845 the registers that are set. */
1847 note_stores (PATTERN (insn), mark_reg_store, NULL);
1849 #ifdef AUTO_INC_DEC
1850 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1851 if (REG_NOTE_KIND (link) == REG_INC)
1852 mark_reg_store (XEXP (link, 0), NULL_RTX, NULL);
1853 #endif
1854 while (n_regs_set-- > 0)
1856 rtx note = find_regno_note (insn, REG_UNUSED,
1857 REGNO (regs_set[n_regs_set]));
1858 if (! note)
1859 continue;
1861 mark_reg_death (XEXP (note, 0));
1865 bitmap_clear (&curr_regs_live);
1866 if (flag_ira_region == IRA_REGION_MIXED
1867 || flag_ira_region == IRA_REGION_ALL)
1868 FOR_EACH_LOOP (li, loop, 0)
1870 EXECUTE_IF_SET_IN_BITMAP (&LOOP_DATA (loop)->regs_live, 0, j, bi)
1871 if (! bitmap_bit_p (&LOOP_DATA (loop)->regs_ref, j))
1873 enum reg_class pressure_class;
1874 int nregs;
1876 pressure_class = get_regno_pressure_class (j, &nregs);
1877 LOOP_DATA (loop)->max_reg_pressure[pressure_class] -= nregs;
1880 if (dump_file == NULL)
1881 return;
1882 FOR_EACH_LOOP (li, loop, 0)
1884 parent = loop_outer (loop);
1885 fprintf (dump_file, "\n Loop %d (parent %d, header bb%d, depth %d)\n",
1886 loop->num, (parent == NULL ? -1 : parent->num),
1887 loop->header->index, loop_depth (loop));
1888 fprintf (dump_file, "\n ref. regnos:");
1889 EXECUTE_IF_SET_IN_BITMAP (&LOOP_DATA (loop)->regs_ref, 0, j, bi)
1890 fprintf (dump_file, " %d", j);
1891 fprintf (dump_file, "\n live regnos:");
1892 EXECUTE_IF_SET_IN_BITMAP (&LOOP_DATA (loop)->regs_live, 0, j, bi)
1893 fprintf (dump_file, " %d", j);
1894 fprintf (dump_file, "\n Pressure:");
1895 for (i = 0; (int) i < ira_pressure_classes_num; i++)
1897 enum reg_class pressure_class;
1899 pressure_class = ira_pressure_classes[i];
1900 if (LOOP_DATA (loop)->max_reg_pressure[pressure_class] == 0)
1901 continue;
1902 fprintf (dump_file, " %s=%d", reg_class_names[pressure_class],
1903 LOOP_DATA (loop)->max_reg_pressure[pressure_class]);
1905 fprintf (dump_file, "\n");
1911 /* Move the invariants out of the loops. */
1913 void
1914 move_loop_invariants (void)
1916 struct loop *loop;
1917 loop_iterator li;
1919 if (flag_ira_loop_pressure)
1921 df_analyze ();
1922 regstat_init_n_sets_and_refs ();
1923 ira_set_pseudo_classes (dump_file);
1924 calculate_loop_reg_pressure ();
1925 regstat_free_n_sets_and_refs ();
1927 df_set_flags (DF_EQ_NOTES + DF_DEFER_INSN_RESCAN);
1928 /* Process the loops, innermost first. */
1929 FOR_EACH_LOOP (li, loop, LI_FROM_INNERMOST)
1931 curr_loop = loop;
1932 /* move_single_loop_invariants for very large loops
1933 is time consuming and might need a lot of memory. */
1934 if (loop->num_nodes <= (unsigned) LOOP_INVARIANT_MAX_BBS_IN_LOOP)
1935 move_single_loop_invariants (loop);
1938 FOR_EACH_LOOP (li, loop, 0)
1940 free_loop_data (loop);
1943 if (flag_ira_loop_pressure)
1944 /* There is no sense to keep this info because it was most
1945 probably outdated by subsequent passes. */
1946 free_reg_info ();
1947 free (invariant_table);
1948 invariant_table = NULL;
1949 invariant_table_size = 0;
1951 #ifdef ENABLE_CHECKING
1952 verify_flow_info ();
1953 #endif