]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/loop-invariant.c
backport: As described in http://gcc.gnu.org/ml/gcc/2012-08/msg00015.html...
[thirdparty/gcc.git] / gcc / loop-invariant.c
CommitLineData
cb20f7e8 1/* RTL-level loop invariant motion.
c75c517d 2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
b1fb9f56 3 Free Software Foundation, Inc.
cb20f7e8 4
5e962776 5This file is part of GCC.
cb20f7e8 6
5e962776
ZD
7GCC is free software; you can redistribute it and/or modify it
8under the terms of the GNU General Public License as published by the
9dcd6f09 9Free Software Foundation; either version 3, or (at your option) any
5e962776 10later version.
cb20f7e8 11
5e962776
ZD
12GCC is distributed in the hope that it will be useful, but WITHOUT
13ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
cb20f7e8 16
5e962776 17You should have received a copy of the GNU General Public License
9dcd6f09
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
5e962776
ZD
20
21/* This implements the loop invariant motion pass. It is very simple
4a8cae83
SB
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.
cb20f7e8 25
5e962776
ZD
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").
30
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.
cb20f7e8 34
5e962776
ZD
35 Then we move the selected invariants out of the loop, creating a new
36 temporaries for them if necessary. */
37
38#include "config.h"
39#include "system.h"
40#include "coretypes.h"
41#include "tm.h"
1833192f 42#include "hard-reg-set.h"
5e962776 43#include "rtl.h"
3912d291 44#include "tm_p.h"
7932a3db 45#include "obstack.h"
5e962776
ZD
46#include "basic-block.h"
47#include "cfgloop.h"
48#include "expr.h"
1052bd54 49#include "recog.h"
5e962776
ZD
50#include "function.h"
51#include "flags.h"
52#include "df.h"
1052bd54 53#include "hashtab.h"
28749cfb 54#include "except.h"
b1fb9f56 55#include "params.h"
1833192f
VM
56#include "regs.h"
57#include "ira.h"
7ee2468b 58#include "dumpfile.h"
5e962776
ZD
59
60/* The data stored for the loop. */
61
62struct loop_data
63{
64 struct loop *outermost_exit; /* The outermost exit of the loop. */
65 bool has_call; /* True if the loop contains a call. */
1833192f 66 /* Maximal register pressure inside loop for given register class
1756cb66 67 (defined only for the pressure classes). */
1833192f
VM
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;
5e962776
ZD
72};
73
74#define LOOP_DATA(LOOP) ((struct loop_data *) (LOOP)->aux)
75
76/* The description of an use. */
77
78struct use
79{
80 rtx *pos; /* Position of the use. */
81 rtx insn; /* The insn in that the use occurs. */
1bfdbb29 82 unsigned addr_use_p; /* Whether the use occurs in an address. */
5e962776
ZD
83 struct use *next; /* Next use in the list. */
84};
85
86/* The description of a def. */
87
88struct def
89{
90 struct use *uses; /* The list of uses that are uniquely reached
91 by it. */
92 unsigned n_uses; /* Number of such uses. */
1bfdbb29 93 unsigned n_addr_uses; /* Number of uses in addresses. */
5e962776
ZD
94 unsigned invno; /* The corresponding invariant. */
95};
96
97/* The data stored for each invariant. */
98
99struct invariant
100{
101 /* The number of the invariant. */
102 unsigned invno;
103
1052bd54
ZD
104 /* The number of the invariant with the same value. */
105 unsigned eqto;
106
107 /* If we moved the invariant out of the loop, the register that contains its
108 value. */
109 rtx reg;
5e962776 110
1833192f
VM
111 /* If we moved the invariant out of the loop, the original regno
112 that contained its value. */
113 int orig_regno;
114
5e962776
ZD
115 /* The definition of the invariant. */
116 struct def *def;
117
118 /* The insn in that it is defined. */
119 rtx insn;
120
121 /* Whether it is always executed. */
122 bool always_executed;
123
124 /* Whether to move the invariant. */
125 bool move;
126
1bfdbb29
PB
127 /* Whether the invariant is cheap when used as an address. */
128 bool cheap_address;
129
cb20f7e8 130 /* Cost of the invariant. */
5e962776
ZD
131 unsigned cost;
132
133 /* The invariants it depends on. */
134 bitmap depends_on;
135
136 /* Used for detecting already visited invariants during determining
137 costs of movements. */
138 unsigned stamp;
139};
140
1833192f
VM
141/* Currently processed loop. */
142static struct loop *curr_loop;
143
6fb5fa3c
DB
144/* Table of invariants indexed by the df_ref uid field. */
145
146static unsigned int invariant_table_size = 0;
147static struct invariant ** invariant_table;
148
1052bd54
ZD
149/* Entry for hash table of invariant expressions. */
150
151struct invariant_expr_entry
152{
153 /* The invariant. */
154 struct invariant *inv;
155
156 /* Its value. */
157 rtx expr;
158
159 /* Its mode. */
160 enum machine_mode mode;
161
162 /* Its hash. */
163 hashval_t hash;
164};
165
5e962776
ZD
166/* The actual stamp for marking already visited invariants during determining
167 costs of movements. */
168
169static unsigned actual_stamp;
170
edd954e6
KH
171typedef struct invariant *invariant_p;
172
173DEF_VEC_P(invariant_p);
174DEF_VEC_ALLOC_P(invariant_p, heap);
175
5e962776
ZD
176/* The invariants. */
177
edd954e6 178static VEC(invariant_p,heap) *invariants;
5e962776 179
6fb5fa3c 180/* Check the size of the invariant table and realloc if necessary. */
cb20f7e8 181
b8698a0f 182static void
6fb5fa3c
DB
183check_invariant_table_size (void)
184{
185 if (invariant_table_size < DF_DEFS_TABLE_SIZE())
186 {
187 unsigned int new_size = DF_DEFS_TABLE_SIZE () + (DF_DEFS_TABLE_SIZE () / 4);
d3bfe4de 188 invariant_table = XRESIZEVEC (struct invariant *, invariant_table, new_size);
b8698a0f 189 memset (&invariant_table[invariant_table_size], 0,
6fb5fa3c
DB
190 (new_size - invariant_table_size) * sizeof (struct rtx_iv *));
191 invariant_table_size = new_size;
192 }
193}
cb20f7e8 194
5e962776
ZD
195/* Test for possibility of invariantness of X. */
196
197static bool
198check_maybe_invariant (rtx x)
199{
200 enum rtx_code code = GET_CODE (x);
201 int i, j;
202 const char *fmt;
203
204 switch (code)
205 {
206 case CONST_INT:
207 case CONST_DOUBLE:
091a3ac7 208 case CONST_FIXED:
5e962776
ZD
209 case SYMBOL_REF:
210 case CONST:
211 case LABEL_REF:
212 return true;
213
214 case PC:
215 case CC0:
216 case UNSPEC_VOLATILE:
217 case CALL:
218 return false;
219
220 case REG:
221 return true;
222
223 case MEM:
224 /* Load/store motion is done elsewhere. ??? Perhaps also add it here?
225 It should not be hard, and might be faster than "elsewhere". */
226
227 /* Just handle the most trivial case where we load from an unchanging
228 location (most importantly, pic tables). */
66f91b93 229 if (MEM_READONLY_P (x) && !MEM_VOLATILE_P (x))
5e962776
ZD
230 break;
231
232 return false;
233
234 case ASM_OPERANDS:
235 /* Don't mess with insns declared volatile. */
236 if (MEM_VOLATILE_P (x))
237 return false;
238 break;
239
240 default:
241 break;
242 }
243
244 fmt = GET_RTX_FORMAT (code);
245 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
246 {
247 if (fmt[i] == 'e')
248 {
249 if (!check_maybe_invariant (XEXP (x, i)))
250 return false;
251 }
252 else if (fmt[i] == 'E')
253 {
254 for (j = 0; j < XVECLEN (x, i); j++)
255 if (!check_maybe_invariant (XVECEXP (x, i, j)))
256 return false;
257 }
258 }
259
260 return true;
261}
262
1052bd54
ZD
263/* Returns the invariant definition for USE, or NULL if USE is not
264 invariant. */
265
266static struct invariant *
57512f53 267invariant_for_use (df_ref use)
1052bd54
ZD
268{
269 struct df_link *defs;
57512f53 270 df_ref def;
50e94c7e 271 basic_block bb = DF_REF_BB (use), def_bb;
1052bd54 272
57512f53 273 if (DF_REF_FLAGS (use) & DF_REF_READ_WRITE)
b6c9b9bc
ZD
274 return NULL;
275
1052bd54
ZD
276 defs = DF_REF_CHAIN (use);
277 if (!defs || defs->next)
278 return NULL;
279 def = defs->ref;
6fb5fa3c
DB
280 check_invariant_table_size ();
281 if (!invariant_table[DF_REF_ID(def)])
1052bd54
ZD
282 return NULL;
283
284 def_bb = DF_REF_BB (def);
285 if (!dominated_by_p (CDI_DOMINATORS, bb, def_bb))
286 return NULL;
6fb5fa3c 287 return invariant_table[DF_REF_ID(def)];
1052bd54
ZD
288}
289
290/* Computes hash value for invariant expression X in INSN. */
291
292static hashval_t
293hash_invariant_expr_1 (rtx insn, rtx x)
294{
295 enum rtx_code code = GET_CODE (x);
296 int i, j;
297 const char *fmt;
298 hashval_t val = code;
299 int do_not_record_p;
57512f53 300 df_ref use;
1052bd54
ZD
301 struct invariant *inv;
302
303 switch (code)
304 {
305 case CONST_INT:
306 case CONST_DOUBLE:
091a3ac7 307 case CONST_FIXED:
1052bd54
ZD
308 case SYMBOL_REF:
309 case CONST:
310 case LABEL_REF:
311 return hash_rtx (x, GET_MODE (x), &do_not_record_p, NULL, false);
312
313 case REG:
6fb5fa3c 314 use = df_find_use (insn, x);
1052bd54
ZD
315 if (!use)
316 return hash_rtx (x, GET_MODE (x), &do_not_record_p, NULL, false);
317 inv = invariant_for_use (use);
318 if (!inv)
319 return hash_rtx (x, GET_MODE (x), &do_not_record_p, NULL, false);
320
321 gcc_assert (inv->eqto != ~0u);
322 return inv->eqto;
323
324 default:
325 break;
326 }
327
328 fmt = GET_RTX_FORMAT (code);
329 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
330 {
331 if (fmt[i] == 'e')
332 val ^= hash_invariant_expr_1 (insn, XEXP (x, i));
333 else if (fmt[i] == 'E')
334 {
335 for (j = 0; j < XVECLEN (x, i); j++)
336 val ^= hash_invariant_expr_1 (insn, XVECEXP (x, i, j));
337 }
8e1409e8
ZD
338 else if (fmt[i] == 'i' || fmt[i] == 'n')
339 val ^= XINT (x, i);
1052bd54
ZD
340 }
341
342 return val;
343}
344
345/* Returns true if the invariant expressions E1 and E2 used in insns INSN1
346 and INSN2 have always the same value. */
347
348static bool
349invariant_expr_equal_p (rtx insn1, rtx e1, rtx insn2, rtx e2)
350{
351 enum rtx_code code = GET_CODE (e1);
352 int i, j;
353 const char *fmt;
57512f53 354 df_ref use1, use2;
1052bd54
ZD
355 struct invariant *inv1 = NULL, *inv2 = NULL;
356 rtx sub1, sub2;
357
358 /* If mode of only one of the operands is VOIDmode, it is not equivalent to
359 the other one. If both are VOIDmode, we rely on the caller of this
360 function to verify that their modes are the same. */
361 if (code != GET_CODE (e2) || GET_MODE (e1) != GET_MODE (e2))
362 return false;
363
364 switch (code)
365 {
366 case CONST_INT:
367 case CONST_DOUBLE:
091a3ac7 368 case CONST_FIXED:
1052bd54
ZD
369 case SYMBOL_REF:
370 case CONST:
371 case LABEL_REF:
372 return rtx_equal_p (e1, e2);
373
374 case REG:
6fb5fa3c
DB
375 use1 = df_find_use (insn1, e1);
376 use2 = df_find_use (insn2, e2);
1052bd54
ZD
377 if (use1)
378 inv1 = invariant_for_use (use1);
379 if (use2)
380 inv2 = invariant_for_use (use2);
381
382 if (!inv1 && !inv2)
383 return rtx_equal_p (e1, e2);
384
385 if (!inv1 || !inv2)
386 return false;
387
388 gcc_assert (inv1->eqto != ~0u);
389 gcc_assert (inv2->eqto != ~0u);
390 return inv1->eqto == inv2->eqto;
391
392 default:
393 break;
394 }
395
396 fmt = GET_RTX_FORMAT (code);
397 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
398 {
399 if (fmt[i] == 'e')
400 {
401 sub1 = XEXP (e1, i);
402 sub2 = XEXP (e2, i);
403
404 if (!invariant_expr_equal_p (insn1, sub1, insn2, sub2))
405 return false;
406 }
407
408 else if (fmt[i] == 'E')
409 {
410 if (XVECLEN (e1, i) != XVECLEN (e2, i))
411 return false;
412
413 for (j = 0; j < XVECLEN (e1, i); j++)
414 {
415 sub1 = XVECEXP (e1, i, j);
416 sub2 = XVECEXP (e2, i, j);
417
418 if (!invariant_expr_equal_p (insn1, sub1, insn2, sub2))
419 return false;
420 }
421 }
8e1409e8
ZD
422 else if (fmt[i] == 'i' || fmt[i] == 'n')
423 {
424 if (XINT (e1, i) != XINT (e2, i))
425 return false;
426 }
427 /* Unhandled type of subexpression, we fail conservatively. */
428 else
429 return false;
1052bd54
ZD
430 }
431
432 return true;
433}
434
435/* Returns hash value for invariant expression entry E. */
436
437static hashval_t
438hash_invariant_expr (const void *e)
439{
d3bfe4de
KG
440 const struct invariant_expr_entry *const entry =
441 (const struct invariant_expr_entry *) e;
1052bd54
ZD
442
443 return entry->hash;
444}
445
446/* Compares invariant expression entries E1 and E2. */
447
448static int
449eq_invariant_expr (const void *e1, const void *e2)
450{
d3bfe4de
KG
451 const struct invariant_expr_entry *const entry1 =
452 (const struct invariant_expr_entry *) e1;
453 const struct invariant_expr_entry *const entry2 =
454 (const struct invariant_expr_entry *) e2;
1052bd54
ZD
455
456 if (entry1->mode != entry2->mode)
457 return 0;
458
459 return invariant_expr_equal_p (entry1->inv->insn, entry1->expr,
460 entry2->inv->insn, entry2->expr);
461}
462
463/* Checks whether invariant with value EXPR in machine mode MODE is
464 recorded in EQ. If this is the case, return the invariant. Otherwise
465 insert INV to the table for this expression and return INV. */
466
467static struct invariant *
468find_or_insert_inv (htab_t eq, rtx expr, enum machine_mode mode,
469 struct invariant *inv)
470{
471 hashval_t hash = hash_invariant_expr_1 (inv->insn, expr);
472 struct invariant_expr_entry *entry;
473 struct invariant_expr_entry pentry;
474 PTR *slot;
475
476 pentry.expr = expr;
477 pentry.inv = inv;
478 pentry.mode = mode;
479 slot = htab_find_slot_with_hash (eq, &pentry, hash, INSERT);
d3bfe4de 480 entry = (struct invariant_expr_entry *) *slot;
1052bd54
ZD
481
482 if (entry)
483 return entry->inv;
484
5ed6ace5 485 entry = XNEW (struct invariant_expr_entry);
1052bd54
ZD
486 entry->inv = inv;
487 entry->expr = expr;
488 entry->mode = mode;
489 entry->hash = hash;
490 *slot = entry;
491
492 return inv;
493}
494
495/* Finds invariants identical to INV and records the equivalence. EQ is the
496 hash table of the invariants. */
497
498static void
499find_identical_invariants (htab_t eq, struct invariant *inv)
500{
501 unsigned depno;
502 bitmap_iterator bi;
503 struct invariant *dep;
504 rtx expr, set;
505 enum machine_mode mode;
506
507 if (inv->eqto != ~0u)
508 return;
509
510 EXECUTE_IF_SET_IN_BITMAP (inv->depends_on, 0, depno, bi)
511 {
512 dep = VEC_index (invariant_p, invariants, depno);
513 find_identical_invariants (eq, dep);
514 }
515
516 set = single_set (inv->insn);
517 expr = SET_SRC (set);
518 mode = GET_MODE (expr);
519 if (mode == VOIDmode)
520 mode = GET_MODE (SET_DEST (set));
521 inv->eqto = find_or_insert_inv (eq, expr, mode, inv)->invno;
522
523 if (dump_file && inv->eqto != inv->invno)
524 fprintf (dump_file,
e755fcf5 525 "Invariant %d is equivalent to invariant %d.\n",
1052bd54
ZD
526 inv->invno, inv->eqto);
527}
528
529/* Find invariants with the same value and record the equivalences. */
530
531static void
532merge_identical_invariants (void)
533{
534 unsigned i;
535 struct invariant *inv;
536 htab_t eq = htab_create (VEC_length (invariant_p, invariants),
537 hash_invariant_expr, eq_invariant_expr, free);
538
ac47786e 539 FOR_EACH_VEC_ELT (invariant_p, invariants, i, inv)
1052bd54
ZD
540 find_identical_invariants (eq, inv);
541
542 htab_delete (eq);
543}
544
5e962776
ZD
545/* Determines the basic blocks inside LOOP that are always executed and
546 stores their bitmap to ALWAYS_REACHED. MAY_EXIT is a bitmap of
547 basic blocks that may either exit the loop, or contain the call that
548 does not have to return. BODY is body of the loop obtained by
549 get_loop_body_in_dom_order. */
550
551static void
552compute_always_reached (struct loop *loop, basic_block *body,
553 bitmap may_exit, bitmap always_reached)
554{
555 unsigned i;
556
557 for (i = 0; i < loop->num_nodes; i++)
558 {
559 if (dominated_by_p (CDI_DOMINATORS, loop->latch, body[i]))
560 bitmap_set_bit (always_reached, i);
561
562 if (bitmap_bit_p (may_exit, i))
563 return;
564 }
565}
566
567/* Finds exits out of the LOOP with body BODY. Marks blocks in that we may
568 exit the loop by cfg edge to HAS_EXIT and MAY_EXIT. In MAY_EXIT
569 additionally mark blocks that may exit due to a call. */
570
571static void
572find_exits (struct loop *loop, basic_block *body,
573 bitmap may_exit, bitmap has_exit)
574{
575 unsigned i;
628f6a4e 576 edge_iterator ei;
5e962776
ZD
577 edge e;
578 struct loop *outermost_exit = loop, *aexit;
579 bool has_call = false;
580 rtx insn;
581
582 for (i = 0; i < loop->num_nodes; i++)
583 {
584 if (body[i]->loop_father == loop)
585 {
586 FOR_BB_INSNS (body[i], insn)
587 {
4b4bf941 588 if (CALL_P (insn)
becfd6e5
KZ
589 && (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)
590 || !RTL_CONST_OR_PURE_CALL_P (insn)))
5e962776
ZD
591 {
592 has_call = true;
593 bitmap_set_bit (may_exit, i);
594 break;
595 }
596 }
597
628f6a4e 598 FOR_EACH_EDGE (e, ei, body[i]->succs)
5e962776
ZD
599 {
600 if (flow_bb_inside_loop_p (loop, e->dest))
601 continue;
602
603 bitmap_set_bit (may_exit, i);
604 bitmap_set_bit (has_exit, i);
605 outermost_exit = find_common_loop (outermost_exit,
606 e->dest->loop_father);
607 }
608 continue;
609 }
cb20f7e8 610
5e962776
ZD
611 /* Use the data stored for the subloop to decide whether we may exit
612 through it. It is sufficient to do this for header of the loop,
613 as other basic blocks inside it must be dominated by it. */
614 if (body[i]->loop_father->header != body[i])
615 continue;
616
617 if (LOOP_DATA (body[i]->loop_father)->has_call)
618 {
619 has_call = true;
620 bitmap_set_bit (may_exit, i);
621 }
622 aexit = LOOP_DATA (body[i]->loop_father)->outermost_exit;
623 if (aexit != loop)
624 {
625 bitmap_set_bit (may_exit, i);
626 bitmap_set_bit (has_exit, i);
627
628 if (flow_loop_nested_p (aexit, outermost_exit))
629 outermost_exit = aexit;
630 }
631 }
632
1833192f
VM
633 if (loop->aux == NULL)
634 {
635 loop->aux = xcalloc (1, sizeof (struct loop_data));
636 bitmap_initialize (&LOOP_DATA (loop)->regs_ref, &reg_obstack);
637 bitmap_initialize (&LOOP_DATA (loop)->regs_live, &reg_obstack);
638 }
5e962776
ZD
639 LOOP_DATA (loop)->outermost_exit = outermost_exit;
640 LOOP_DATA (loop)->has_call = has_call;
641}
642
643/* Check whether we may assign a value to X from a register. */
644
645static bool
646may_assign_reg_p (rtx x)
647{
bd361d85 648 return (GET_MODE (x) != VOIDmode
4b06592a 649 && GET_MODE (x) != BLKmode
bd361d85 650 && can_copy_p (GET_MODE (x))
a7f4ccb1
SB
651 && (!REG_P (x)
652 || !HARD_REGISTER_P (x)
653 || REGNO_REG_CLASS (REGNO (x)) != NO_REGS));
5e962776
ZD
654}
655
cb20f7e8
ZD
656/* Finds definitions that may correspond to invariants in LOOP with body
657 BODY. */
5e962776
ZD
658
659static void
cb20f7e8 660find_defs (struct loop *loop, basic_block *body)
5e962776
ZD
661{
662 unsigned i;
8bdbfff5 663 bitmap blocks = BITMAP_ALLOC (NULL);
5e962776
ZD
664
665 for (i = 0; i < loop->num_nodes; i++)
666 bitmap_set_bit (blocks, body[i]->index);
667
6fb5fa3c
DB
668 df_remove_problem (df_chain);
669 df_process_deferred_rescans ();
670 df_chain_add_problem (DF_UD_CHAIN);
671 df_set_blocks (blocks);
672 df_analyze ();
673
674 if (dump_file)
675 {
ffd640ed 676 df_dump_region (dump_file);
6fb5fa3c 677 fprintf (dump_file, "*****starting processing of loop ******\n");
a315c44c 678 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
6fb5fa3c
DB
679 fprintf (dump_file, "*****ending processing of loop ******\n");
680 }
681 check_invariant_table_size ();
682
8bdbfff5 683 BITMAP_FREE (blocks);
5e962776
ZD
684}
685
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,
1052bd54
ZD
688 unless the program ends due to a function call. The newly created invariant
689 is returned. */
5e962776 690
1052bd54 691static struct invariant *
5e962776
ZD
692create_new_invariant (struct def *def, rtx insn, bitmap depends_on,
693 bool always_executed)
694{
5ed6ace5 695 struct invariant *inv = XNEW (struct invariant);
5e962776 696 rtx set = single_set (insn);
f40751dd 697 bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn));
5e962776
ZD
698
699 inv->def = def;
700 inv->always_executed = always_executed;
701 inv->depends_on = depends_on;
702
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)
1bfdbb29 706 {
d51102f3 707 inv->cost = set_rtx_cost (set, speed);
1578e910
MM
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 . */
1bfdbb29 717 inv->cheap_address = address_cost (SET_SRC (set), word_mode,
09e881c9 718 ADDR_SPACE_GENERIC, speed) < 3;
1bfdbb29 719 }
5e962776 720 else
1bfdbb29 721 {
5e8f01f4 722 inv->cost = set_src_cost (SET_SRC (set), speed);
1bfdbb29
PB
723 inv->cheap_address = false;
724 }
5e962776
ZD
725
726 inv->move = false;
1052bd54 727 inv->reg = NULL_RTX;
1833192f 728 inv->orig_regno = -1;
5e962776
ZD
729 inv->stamp = 0;
730 inv->insn = insn;
731
edd954e6 732 inv->invno = VEC_length (invariant_p, invariants);
1052bd54 733 inv->eqto = ~0u;
5e962776
ZD
734 if (def)
735 def->invno = inv->invno;
edd954e6 736 VEC_safe_push (invariant_p, heap, invariants, inv);
5e962776
ZD
737
738 if (dump_file)
739 {
740 fprintf (dump_file,
741 "Set in insn %d is invariant (%d), cost %d, depends on ",
742 INSN_UID (insn), inv->invno, inv->cost);
743 dump_bitmap (dump_file, inv->depends_on);
744 }
1052bd54
ZD
745
746 return inv;
5e962776
ZD
747}
748
749/* Record USE at DEF. */
750
751static void
1bfdbb29 752record_use (struct def *def, df_ref use)
5e962776 753{
5ed6ace5 754 struct use *u = XNEW (struct use);
5e962776 755
1bfdbb29
PB
756 u->pos = DF_REF_REAL_LOC (use);
757 u->insn = DF_REF_INSN (use);
758 u->addr_use_p = (DF_REF_TYPE (use) == DF_REF_REG_MEM_LOAD
3e807ffc 759 || DF_REF_TYPE (use) == DF_REF_REG_MEM_STORE);
5e962776
ZD
760 u->next = def->uses;
761 def->uses = u;
762 def->n_uses++;
1bfdbb29
PB
763 if (u->addr_use_p)
764 def->n_addr_uses++;
5e962776
ZD
765}
766
6fb5fa3c
DB
767/* Finds the invariants USE depends on and store them to the DEPENDS_ON
768 bitmap. Returns true if all dependencies of USE are known to be
b6c9b9bc 769 loop invariants, false otherwise. */
5e962776
ZD
770
771static bool
57512f53 772check_dependency (basic_block bb, df_ref use, bitmap depends_on)
5e962776 773{
57512f53 774 df_ref def;
6fb5fa3c 775 basic_block def_bb;
4d779342 776 struct df_link *defs;
5e962776 777 struct def *def_data;
1052bd54 778 struct invariant *inv;
b8698a0f 779
57512f53 780 if (DF_REF_FLAGS (use) & DF_REF_READ_WRITE)
6fb5fa3c 781 return false;
b8698a0f 782
6fb5fa3c
DB
783 defs = DF_REF_CHAIN (use);
784 if (!defs)
785 return true;
b8698a0f 786
6fb5fa3c
DB
787 if (defs->next)
788 return false;
b8698a0f 789
6fb5fa3c
DB
790 def = defs->ref;
791 check_invariant_table_size ();
792 inv = invariant_table[DF_REF_ID(def)];
793 if (!inv)
794 return false;
b8698a0f 795
6fb5fa3c
DB
796 def_data = inv->def;
797 gcc_assert (def_data != NULL);
b8698a0f 798
6fb5fa3c
DB
799 def_bb = DF_REF_BB (def);
800 /* Note that in case bb == def_bb, we know that the definition
801 dominates insn, because def has invariant_table[DF_REF_ID(def)]
802 defined and we process the insns in the basic block bb
803 sequentially. */
804 if (!dominated_by_p (CDI_DOMINATORS, bb, def_bb))
805 return false;
b8698a0f 806
6fb5fa3c
DB
807 bitmap_set_bit (depends_on, def_data->invno);
808 return true;
809}
1052bd54 810
1052bd54 811
6fb5fa3c
DB
812/* Finds the invariants INSN depends on and store them to the DEPENDS_ON
813 bitmap. Returns true if all dependencies of INSN are known to be
814 loop invariants, false otherwise. */
5e962776 815
6fb5fa3c
DB
816static bool
817check_dependencies (rtx insn, bitmap depends_on)
818{
50e94c7e 819 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
57512f53 820 df_ref *use_rec;
6fb5fa3c 821 basic_block bb = BLOCK_FOR_INSN (insn);
5e962776 822
50e94c7e 823 for (use_rec = DF_INSN_INFO_USES (insn_info); *use_rec; use_rec++)
6fb5fa3c
DB
824 if (!check_dependency (bb, *use_rec, depends_on))
825 return false;
50e94c7e 826 for (use_rec = DF_INSN_INFO_EQ_USES (insn_info); *use_rec; use_rec++)
6fb5fa3c
DB
827 if (!check_dependency (bb, *use_rec, depends_on))
828 return false;
b8698a0f 829
5e962776
ZD
830 return true;
831}
832
833/* Finds invariant in INSN. ALWAYS_REACHED is true if the insn is always
834 executed. ALWAYS_EXECUTED is true if the insn is always executed,
cb20f7e8 835 unless the program ends due to a function call. */
5e962776
ZD
836
837static void
cb20f7e8 838find_invariant_insn (rtx insn, bool always_reached, bool always_executed)
5e962776 839{
57512f53 840 df_ref ref;
5e962776
ZD
841 struct def *def;
842 bitmap depends_on;
843 rtx set, dest;
844 bool simple = true;
1052bd54 845 struct invariant *inv;
5e962776 846
00f70f98
ZD
847#ifdef HAVE_cc0
848 /* We can't move a CC0 setter without the user. */
849 if (sets_cc0_p (insn))
850 return;
851#endif
852
5e962776
ZD
853 set = single_set (insn);
854 if (!set)
855 return;
856 dest = SET_DEST (set);
857
2ca202e7 858 if (!REG_P (dest)
5e962776
ZD
859 || HARD_REGISTER_P (dest))
860 simple = false;
861
a7f4ccb1
SB
862 if (!may_assign_reg_p (SET_DEST (set))
863 || !check_maybe_invariant (SET_SRC (set)))
5e962776
ZD
864 return;
865
28749cfb
ZD
866 /* If the insn can throw exception, we cannot move it at all without changing
867 cfg. */
868 if (can_throw_internal (insn))
869 return;
5e962776 870
28749cfb 871 /* We cannot make trapping insn executed, unless it was executed before. */
48e8382e 872 if (may_trap_or_fault_p (PATTERN (insn)) && !always_reached)
28749cfb 873 return;
5e962776 874
8bdbfff5 875 depends_on = BITMAP_ALLOC (NULL);
cb20f7e8 876 if (!check_dependencies (insn, depends_on))
5e962776 877 {
8bdbfff5 878 BITMAP_FREE (depends_on);
5e962776
ZD
879 return;
880 }
881
882 if (simple)
5ed6ace5 883 def = XCNEW (struct def);
5e962776
ZD
884 else
885 def = NULL;
886
1052bd54
ZD
887 inv = create_new_invariant (def, insn, depends_on, always_executed);
888
889 if (simple)
890 {
6fb5fa3c
DB
891 ref = df_find_def (insn, dest);
892 check_invariant_table_size ();
893 invariant_table[DF_REF_ID(ref)] = inv;
1052bd54 894 }
5e962776
ZD
895}
896
cb20f7e8 897/* Record registers used in INSN that have a unique invariant definition. */
5e962776
ZD
898
899static void
cb20f7e8 900record_uses (rtx insn)
5e962776 901{
50e94c7e 902 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
57512f53 903 df_ref *use_rec;
1052bd54
ZD
904 struct invariant *inv;
905
50e94c7e 906 for (use_rec = DF_INSN_INFO_USES (insn_info); *use_rec; use_rec++)
6fb5fa3c 907 {
57512f53 908 df_ref use = *use_rec;
6fb5fa3c
DB
909 inv = invariant_for_use (use);
910 if (inv)
1bfdbb29 911 record_use (inv->def, use);
6fb5fa3c 912 }
50e94c7e 913 for (use_rec = DF_INSN_INFO_EQ_USES (insn_info); *use_rec; use_rec++)
5e962776 914 {
57512f53 915 df_ref use = *use_rec;
1052bd54
ZD
916 inv = invariant_for_use (use);
917 if (inv)
1bfdbb29 918 record_use (inv->def, use);
5e962776
ZD
919 }
920}
921
922/* Finds invariants in INSN. ALWAYS_REACHED is true if the insn is always
923 executed. ALWAYS_EXECUTED is true if the insn is always executed,
cb20f7e8 924 unless the program ends due to a function call. */
5e962776
ZD
925
926static void
cb20f7e8 927find_invariants_insn (rtx insn, bool always_reached, bool always_executed)
5e962776 928{
cb20f7e8
ZD
929 find_invariant_insn (insn, always_reached, always_executed);
930 record_uses (insn);
5e962776
ZD
931}
932
933/* Finds invariants in basic block BB. ALWAYS_REACHED is true if the
934 basic block is always executed. ALWAYS_EXECUTED is true if the basic
935 block is always executed, unless the program ends due to a function
cb20f7e8 936 call. */
5e962776
ZD
937
938static void
cb20f7e8 939find_invariants_bb (basic_block bb, bool always_reached, bool always_executed)
5e962776
ZD
940{
941 rtx insn;
942
943 FOR_BB_INSNS (bb, insn)
944 {
b5b8b0ac 945 if (!NONDEBUG_INSN_P (insn))
5e962776
ZD
946 continue;
947
cb20f7e8 948 find_invariants_insn (insn, always_reached, always_executed);
5e962776
ZD
949
950 if (always_reached
4b4bf941 951 && CALL_P (insn)
becfd6e5
KZ
952 && (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)
953 || ! RTL_CONST_OR_PURE_CALL_P (insn)))
5e962776
ZD
954 always_reached = false;
955 }
956}
957
958/* Finds invariants in LOOP with body BODY. ALWAYS_REACHED is the bitmap of
959 basic blocks in BODY that are always executed. ALWAYS_EXECUTED is the
960 bitmap of basic blocks in BODY that are always executed unless the program
cb20f7e8 961 ends due to a function call. */
5e962776
ZD
962
963static void
964find_invariants_body (struct loop *loop, basic_block *body,
cb20f7e8 965 bitmap always_reached, bitmap always_executed)
5e962776
ZD
966{
967 unsigned i;
968
969 for (i = 0; i < loop->num_nodes; i++)
970 find_invariants_bb (body[i],
971 bitmap_bit_p (always_reached, i),
cb20f7e8 972 bitmap_bit_p (always_executed, i));
5e962776
ZD
973}
974
cb20f7e8 975/* Finds invariants in LOOP. */
5e962776
ZD
976
977static void
cb20f7e8 978find_invariants (struct loop *loop)
5e962776 979{
8bdbfff5
NS
980 bitmap may_exit = BITMAP_ALLOC (NULL);
981 bitmap always_reached = BITMAP_ALLOC (NULL);
982 bitmap has_exit = BITMAP_ALLOC (NULL);
983 bitmap always_executed = BITMAP_ALLOC (NULL);
5e962776
ZD
984 basic_block *body = get_loop_body_in_dom_order (loop);
985
986 find_exits (loop, body, may_exit, has_exit);
987 compute_always_reached (loop, body, may_exit, always_reached);
988 compute_always_reached (loop, body, has_exit, always_executed);
989
cb20f7e8
ZD
990 find_defs (loop, body);
991 find_invariants_body (loop, body, always_reached, always_executed);
1052bd54 992 merge_identical_invariants ();
5e962776 993
8bdbfff5
NS
994 BITMAP_FREE (always_reached);
995 BITMAP_FREE (always_executed);
996 BITMAP_FREE (may_exit);
997 BITMAP_FREE (has_exit);
5e962776
ZD
998 free (body);
999}
1000
1001/* Frees a list of uses USE. */
1002
1003static void
1004free_use_list (struct use *use)
1005{
1006 struct use *next;
1007
1008 for (; use; use = next)
1009 {
1010 next = use->next;
1011 free (use);
1012 }
1013}
1014
1756cb66 1015/* Return pressure class and number of hard registers (through *NREGS)
1833192f
VM
1016 for destination of INSN. */
1017static enum reg_class
1756cb66 1018get_pressure_class_and_nregs (rtx insn, int *nregs)
1833192f
VM
1019{
1020 rtx reg;
1756cb66 1021 enum reg_class pressure_class;
1833192f 1022 rtx set = single_set (insn);
b8698a0f 1023
1833192f
VM
1024 /* Considered invariant insns have only one set. */
1025 gcc_assert (set != NULL_RTX);
1026 reg = SET_DEST (set);
1027 if (GET_CODE (reg) == SUBREG)
1028 reg = SUBREG_REG (reg);
1029 if (MEM_P (reg))
1030 {
1031 *nregs = 0;
1756cb66 1032 pressure_class = NO_REGS;
1833192f
VM
1033 }
1034 else
1035 {
1036 if (! REG_P (reg))
1037 reg = NULL_RTX;
1038 if (reg == NULL_RTX)
1756cb66 1039 pressure_class = GENERAL_REGS;
1833192f 1040 else
1756cb66
VM
1041 {
1042 pressure_class = reg_allocno_class (REGNO (reg));
1043 pressure_class = ira_pressure_class_translate[pressure_class];
1044 }
1045 *nregs
1046 = ira_reg_class_max_nregs[pressure_class][GET_MODE (SET_SRC (set))];
1833192f 1047 }
1756cb66 1048 return pressure_class;
1833192f
VM
1049}
1050
5e962776
ZD
1051/* Calculates cost and number of registers needed for moving invariant INV
1052 out of the loop and stores them to *COST and *REGS_NEEDED. */
1053
1054static void
1055get_inv_cost (struct invariant *inv, int *comp_cost, unsigned *regs_needed)
1056{
1833192f
VM
1057 int i, acomp_cost;
1058 unsigned aregs_needed[N_REG_CLASSES];
5e962776
ZD
1059 unsigned depno;
1060 struct invariant *dep;
87c476a2 1061 bitmap_iterator bi;
5e962776 1062
1052bd54
ZD
1063 /* Find the representative of the class of the equivalent invariants. */
1064 inv = VEC_index (invariant_p, invariants, inv->eqto);
1065
5e962776 1066 *comp_cost = 0;
1833192f
VM
1067 if (! flag_ira_loop_pressure)
1068 regs_needed[0] = 0;
1069 else
1070 {
1756cb66
VM
1071 for (i = 0; i < ira_pressure_classes_num; i++)
1072 regs_needed[ira_pressure_classes[i]] = 0;
1833192f
VM
1073 }
1074
5e962776
ZD
1075 if (inv->move
1076 || inv->stamp == actual_stamp)
1077 return;
1078 inv->stamp = actual_stamp;
1079
1833192f
VM
1080 if (! flag_ira_loop_pressure)
1081 regs_needed[0]++;
1082 else
1083 {
1084 int nregs;
1756cb66 1085 enum reg_class pressure_class;
1833192f 1086
1756cb66
VM
1087 pressure_class = get_pressure_class_and_nregs (inv->insn, &nregs);
1088 regs_needed[pressure_class] += nregs;
1833192f
VM
1089 }
1090
1bfdbb29
PB
1091 if (!inv->cheap_address
1092 || inv->def->n_addr_uses < inv->def->n_uses)
1093 (*comp_cost) += inv->cost;
5e962776 1094
3d8504ac
RS
1095#ifdef STACK_REGS
1096 {
1097 /* Hoisting constant pool constants into stack regs may cost more than
1098 just single register. On x87, the balance is affected both by the
c0220ea4 1099 small number of FP registers, and by its register stack organization,
3d8504ac
RS
1100 that forces us to add compensation code in and around the loop to
1101 shuffle the operands to the top of stack before use, and pop them
1102 from the stack after the loop finishes.
1103
1104 To model this effect, we increase the number of registers needed for
1105 stack registers by two: one register push, and one register pop.
1106 This usually has the effect that FP constant loads from the constant
1107 pool are not moved out of the loop.
1108
1109 Note that this also means that dependent invariants can not be moved.
1110 However, the primary purpose of this pass is to move loop invariant
1111 address arithmetic out of loops, and address arithmetic that depends
1112 on floating point constants is unlikely to ever occur. */
1113 rtx set = single_set (inv->insn);
1114 if (set
1833192f
VM
1115 && IS_STACK_MODE (GET_MODE (SET_SRC (set)))
1116 && constant_pool_constant_p (SET_SRC (set)))
1117 {
1118 if (flag_ira_loop_pressure)
1756cb66 1119 regs_needed[ira_stack_reg_pressure_class] += 2;
1833192f
VM
1120 else
1121 regs_needed[0] += 2;
1122 }
3d8504ac
RS
1123 }
1124#endif
1125
87c476a2 1126 EXECUTE_IF_SET_IN_BITMAP (inv->depends_on, 0, depno, bi)
5e962776 1127 {
1833192f
VM
1128 bool check_p;
1129
edd954e6 1130 dep = VEC_index (invariant_p, invariants, depno);
5e962776 1131
1833192f 1132 get_inv_cost (dep, &acomp_cost, aregs_needed);
5e962776 1133
1833192f
VM
1134 if (! flag_ira_loop_pressure)
1135 check_p = aregs_needed[0] != 0;
1136 else
1137 {
1756cb66
VM
1138 for (i = 0; i < ira_pressure_classes_num; i++)
1139 if (aregs_needed[ira_pressure_classes[i]] != 0)
1833192f 1140 break;
1756cb66 1141 check_p = i < ira_pressure_classes_num;
1833192f
VM
1142 }
1143 if (check_p
5e962776
ZD
1144 /* We need to check always_executed, since if the original value of
1145 the invariant may be preserved, we may need to keep it in a
1146 separate register. TODO check whether the register has an
1147 use outside of the loop. */
1148 && dep->always_executed
1149 && !dep->def->uses->next)
1150 {
1151 /* If this is a single use, after moving the dependency we will not
1152 need a new register. */
1833192f
VM
1153 if (! flag_ira_loop_pressure)
1154 aregs_needed[0]--;
1155 else
1156 {
1157 int nregs;
1756cb66 1158 enum reg_class pressure_class;
1833192f 1159
1756cb66
VM
1160 pressure_class = get_pressure_class_and_nregs (inv->insn, &nregs);
1161 aregs_needed[pressure_class] -= nregs;
1833192f 1162 }
5e962776
ZD
1163 }
1164
1833192f
VM
1165 if (! flag_ira_loop_pressure)
1166 regs_needed[0] += aregs_needed[0];
1167 else
1168 {
1756cb66
VM
1169 for (i = 0; i < ira_pressure_classes_num; i++)
1170 regs_needed[ira_pressure_classes[i]]
1171 += aregs_needed[ira_pressure_classes[i]];
1833192f 1172 }
5e962776 1173 (*comp_cost) += acomp_cost;
87c476a2 1174 }
5e962776
ZD
1175}
1176
1177/* Calculates gain for eliminating invariant INV. REGS_USED is the number
a154b43a
ZD
1178 of registers used in the loop, NEW_REGS is the number of new variables
1179 already added due to the invariant motion. The number of registers needed
bec922f0
SL
1180 for it is stored in *REGS_NEEDED. SPEED and CALL_P are flags passed
1181 through to estimate_reg_pressure_cost. */
5e962776
ZD
1182
1183static int
1184gain_for_invariant (struct invariant *inv, unsigned *regs_needed,
bec922f0
SL
1185 unsigned *new_regs, unsigned regs_used,
1186 bool speed, bool call_p)
5e962776
ZD
1187{
1188 int comp_cost, size_cost;
1189
5e962776
ZD
1190 actual_stamp++;
1191
1833192f
VM
1192 get_inv_cost (inv, &comp_cost, regs_needed);
1193
1194 if (! flag_ira_loop_pressure)
1195 {
1196 size_cost = (estimate_reg_pressure_cost (new_regs[0] + regs_needed[0],
bec922f0 1197 regs_used, speed, call_p)
1833192f 1198 - estimate_reg_pressure_cost (new_regs[0],
bec922f0 1199 regs_used, speed, call_p));
1833192f
VM
1200 }
1201 else
1202 {
1203 int i;
1756cb66 1204 enum reg_class pressure_class;
1833192f 1205
1756cb66 1206 for (i = 0; i < ira_pressure_classes_num; i++)
1833192f 1207 {
1756cb66
VM
1208 pressure_class = ira_pressure_classes[i];
1209 if ((int) new_regs[pressure_class]
1210 + (int) regs_needed[pressure_class]
1211 + LOOP_DATA (curr_loop)->max_reg_pressure[pressure_class]
1833192f 1212 + IRA_LOOP_RESERVED_REGS
f508f827 1213 > ira_class_hard_regs_num[pressure_class])
1833192f
VM
1214 break;
1215 }
1756cb66 1216 if (i < ira_pressure_classes_num)
1833192f
VM
1217 /* There will be register pressure excess and we want not to
1218 make this loop invariant motion. All loop invariants with
1219 non-positive gains will be rejected in function
1220 find_invariants_to_move. Therefore we return the negative
1221 number here.
1222
1223 One could think that this rejects also expensive loop
1224 invariant motions and this will hurt code performance.
1225 However numerous experiments with different heuristics
1226 taking invariant cost into account did not confirm this
1227 assumption. There are possible explanations for this
1228 result:
1229 o probably all expensive invariants were already moved out
1230 of the loop by PRE and gimple invariant motion pass.
1231 o expensive invariant execution will be hidden by insn
1232 scheduling or OOO processor hardware because usually such
1233 invariants have a lot of freedom to be executed
1234 out-of-order.
1235 Another reason for ignoring invariant cost vs spilling cost
1236 heuristics is also in difficulties to evaluate accurately
1237 spill cost at this stage. */
1238 return -1;
1239 else
1240 size_cost = 0;
1241 }
5e962776
ZD
1242
1243 return comp_cost - size_cost;
1244}
1245
1246/* Finds invariant with best gain for moving. Returns the gain, stores
1247 the invariant in *BEST and number of registers needed for it to
a154b43a
ZD
1248 *REGS_NEEDED. REGS_USED is the number of registers used in the loop.
1249 NEW_REGS is the number of new variables already added due to invariant
1250 motion. */
5e962776
ZD
1251
1252static int
1253best_gain_for_invariant (struct invariant **best, unsigned *regs_needed,
bec922f0
SL
1254 unsigned *new_regs, unsigned regs_used,
1255 bool speed, bool call_p)
5e962776
ZD
1256{
1257 struct invariant *inv;
1833192f
VM
1258 int i, gain = 0, again;
1259 unsigned aregs_needed[N_REG_CLASSES], invno;
5e962776 1260
ac47786e 1261 FOR_EACH_VEC_ELT (invariant_p, invariants, invno, inv)
5e962776 1262 {
5e962776
ZD
1263 if (inv->move)
1264 continue;
1265
1052bd54
ZD
1266 /* Only consider the "representatives" of equivalent invariants. */
1267 if (inv->eqto != inv->invno)
1268 continue;
1269
1833192f 1270 again = gain_for_invariant (inv, aregs_needed, new_regs, regs_used,
bec922f0 1271 speed, call_p);
5e962776
ZD
1272 if (again > gain)
1273 {
1274 gain = again;
1275 *best = inv;
1833192f
VM
1276 if (! flag_ira_loop_pressure)
1277 regs_needed[0] = aregs_needed[0];
1278 else
1279 {
1756cb66
VM
1280 for (i = 0; i < ira_pressure_classes_num; i++)
1281 regs_needed[ira_pressure_classes[i]]
1282 = aregs_needed[ira_pressure_classes[i]];
1833192f 1283 }
5e962776
ZD
1284 }
1285 }
1286
1287 return gain;
1288}
1289
1290/* Marks invariant INVNO and all its dependencies for moving. */
1291
1292static void
1833192f 1293set_move_mark (unsigned invno, int gain)
5e962776 1294{
edd954e6 1295 struct invariant *inv = VEC_index (invariant_p, invariants, invno);
87c476a2 1296 bitmap_iterator bi;
5e962776 1297
1052bd54
ZD
1298 /* Find the representative of the class of the equivalent invariants. */
1299 inv = VEC_index (invariant_p, invariants, inv->eqto);
1300
5e962776
ZD
1301 if (inv->move)
1302 return;
1303 inv->move = true;
1304
1305 if (dump_file)
1833192f
VM
1306 {
1307 if (gain >= 0)
1308 fprintf (dump_file, "Decided to move invariant %d -- gain %d\n",
1309 invno, gain);
1310 else
1311 fprintf (dump_file, "Decided to move dependent invariant %d\n",
1312 invno);
1313 };
5e962776 1314
87c476a2
ZD
1315 EXECUTE_IF_SET_IN_BITMAP (inv->depends_on, 0, invno, bi)
1316 {
1833192f 1317 set_move_mark (invno, -1);
87c476a2 1318 }
5e962776
ZD
1319}
1320
cb20f7e8 1321/* Determines which invariants to move. */
5e962776
ZD
1322
1323static void
bec922f0 1324find_invariants_to_move (bool speed, bool call_p)
5e962776 1325{
1833192f
VM
1326 int gain;
1327 unsigned i, regs_used, regs_needed[N_REG_CLASSES], new_regs[N_REG_CLASSES];
5e962776
ZD
1328 struct invariant *inv = NULL;
1329
edd954e6 1330 if (!VEC_length (invariant_p, invariants))
5e962776
ZD
1331 return;
1332
1833192f 1333 if (flag_ira_loop_pressure)
b8698a0f 1334 /* REGS_USED is actually never used when the flag is on. */
1833192f
VM
1335 regs_used = 0;
1336 else
1337 /* We do not really do a good job in estimating number of
1338 registers used; we put some initial bound here to stand for
1339 induction variables etc. that we do not detect. */
5e962776 1340 {
1833192f
VM
1341 unsigned int n_regs = DF_REG_SIZE (df);
1342
1343 regs_used = 2;
b8698a0f 1344
1833192f 1345 for (i = 0; i < n_regs; i++)
5e962776 1346 {
1833192f
VM
1347 if (!DF_REGNO_FIRST_DEF (i) && DF_REGNO_LAST_USE (i))
1348 {
1349 /* This is a value that is used but not changed inside loop. */
1350 regs_used++;
1351 }
5e962776
ZD
1352 }
1353 }
1354
1833192f
VM
1355 if (! flag_ira_loop_pressure)
1356 new_regs[0] = regs_needed[0] = 0;
1357 else
5e962776 1358 {
1756cb66
VM
1359 for (i = 0; (int) i < ira_pressure_classes_num; i++)
1360 new_regs[ira_pressure_classes[i]] = 0;
1833192f
VM
1361 }
1362 while ((gain = best_gain_for_invariant (&inv, regs_needed,
bec922f0
SL
1363 new_regs, regs_used,
1364 speed, call_p)) > 0)
1833192f
VM
1365 {
1366 set_move_mark (inv->invno, gain);
1367 if (! flag_ira_loop_pressure)
1368 new_regs[0] += regs_needed[0];
1369 else
1370 {
1756cb66
VM
1371 for (i = 0; (int) i < ira_pressure_classes_num; i++)
1372 new_regs[ira_pressure_classes[i]]
1373 += regs_needed[ira_pressure_classes[i]];
1833192f 1374 }
5e962776
ZD
1375 }
1376}
1377
43ba743c
EB
1378/* Replace the uses, reached by the definition of invariant INV, by REG.
1379
1380 IN_GROUP is nonzero if this is part of a group of changes that must be
1381 performed as a group. In that case, the changes will be stored. The
1382 function `apply_change_group' will validate and apply the changes. */
1383
1384static int
1385replace_uses (struct invariant *inv, rtx reg, bool in_group)
1386{
1387 /* Replace the uses we know to be dominated. It saves work for copy
1388 propagation, and also it is necessary so that dependent invariants
1389 are computed right. */
1390 if (inv->def)
1391 {
1392 struct use *use;
1393 for (use = inv->def->uses; use; use = use->next)
1394 validate_change (use->insn, use->pos, reg, true);
1395
1396 /* If we aren't part of a larger group, apply the changes now. */
1397 if (!in_group)
1398 return apply_change_group ();
1399 }
1400
1401 return 1;
1402}
1403
ba946209
ZD
1404/* Move invariant INVNO out of the LOOP. Returns true if this succeeds, false
1405 otherwise. */
1406
1407static bool
cb20f7e8 1408move_invariant_reg (struct loop *loop, unsigned invno)
5e962776 1409{
edd954e6 1410 struct invariant *inv = VEC_index (invariant_p, invariants, invno);
1052bd54 1411 struct invariant *repr = VEC_index (invariant_p, invariants, inv->eqto);
5e962776
ZD
1412 unsigned i;
1413 basic_block preheader = loop_preheader_edge (loop)->src;
90b1c344 1414 rtx reg, set, dest, note;
87c476a2 1415 bitmap_iterator bi;
43ba743c 1416 int regno = -1;
5e962776 1417
ba946209
ZD
1418 if (inv->reg)
1419 return true;
1420 if (!repr->move)
1421 return false;
43ba743c 1422
1052bd54
ZD
1423 /* If this is a representative of the class of equivalent invariants,
1424 really move the invariant. Otherwise just replace its use with
1425 the register used for the representative. */
1426 if (inv == repr)
5e962776 1427 {
1052bd54 1428 if (inv->depends_on)
5e962776 1429 {
1052bd54
ZD
1430 EXECUTE_IF_SET_IN_BITMAP (inv->depends_on, 0, i, bi)
1431 {
ba946209
ZD
1432 if (!move_invariant_reg (loop, i))
1433 goto fail;
1052bd54 1434 }
87c476a2 1435 }
5e962776 1436
1052bd54 1437 /* Move the set out of the loop. If the set is always executed (we could
43ba743c
EB
1438 omit this condition if we know that the register is unused outside of
1439 the loop, but it does not seem worth finding out) and it has no uses
1440 that would not be dominated by it, we may just move it (TODO).
1441 Otherwise we need to create a temporary register. */
1052bd54 1442 set = single_set (inv->insn);
1833192f
VM
1443 reg = dest = SET_DEST (set);
1444 if (GET_CODE (reg) == SUBREG)
1445 reg = SUBREG_REG (reg);
1446 if (REG_P (reg))
1447 regno = REGNO (reg);
1448
46b71b03 1449 reg = gen_reg_rtx_and_attrs (dest);
1052bd54 1450
90b1c344 1451 /* Try replacing the destination by a new pseudoregister. */
43ba743c
EB
1452 validate_change (inv->insn, &SET_DEST (set), reg, true);
1453
1454 /* As well as all the dominated uses. */
1455 replace_uses (inv, reg, true);
1456
1457 /* And validate all the changes. */
1458 if (!apply_change_group ())
90b1c344 1459 goto fail;
90b1c344
ZD
1460
1461 emit_insn_after (gen_move_insn (dest, reg), inv->insn);
1462 reorder_insns (inv->insn, inv->insn, BB_END (preheader));
1463
82fa5f8a
L
1464 /* If there is a REG_EQUAL note on the insn we just moved, and the
1465 insn is in a basic block that is not always executed or the note
1466 contains something for which we don't know the invariant status,
1467 the note may no longer be valid after we move the insn. Note that
1468 uses in REG_EQUAL notes are taken into account in the computation
1469 of invariants, so it is safe to retain the note even if it contains
1470 register references for which we know the invariant status. */
1471 if ((note = find_reg_note (inv->insn, REG_EQUAL, NULL_RTX))
1472 && (!inv->always_executed
1473 || !check_maybe_invariant (XEXP (note, 0))))
90b1c344 1474 remove_note (inv->insn, note);
b644b211
SB
1475 }
1476 else
1477 {
ba946209
ZD
1478 if (!move_invariant_reg (loop, repr->invno))
1479 goto fail;
1052bd54 1480 reg = repr->reg;
1833192f 1481 regno = repr->orig_regno;
43ba743c
EB
1482 if (!replace_uses (inv, reg, false))
1483 goto fail;
1052bd54 1484 set = single_set (inv->insn);
4d779342
DB
1485 emit_insn_after (gen_move_insn (SET_DEST (set), reg), inv->insn);
1486 delete_insn (inv->insn);
b644b211 1487 }
5e962776 1488
1052bd54 1489 inv->reg = reg;
1833192f 1490 inv->orig_regno = regno;
1052bd54 1491
ba946209
ZD
1492 return true;
1493
1494fail:
1495 /* If we failed, clear move flag, so that we do not try to move inv
1496 again. */
1497 if (dump_file)
1498 fprintf (dump_file, "Failed to move invariant %d\n", invno);
1499 inv->move = false;
1500 inv->reg = NULL_RTX;
1833192f 1501 inv->orig_regno = -1;
6fb5fa3c 1502
ba946209 1503 return false;
5e962776
ZD
1504}
1505
1506/* Move selected invariant out of the LOOP. Newly created regs are marked
cb20f7e8 1507 in TEMPORARY_REGS. */
5e962776
ZD
1508
1509static void
cb20f7e8 1510move_invariants (struct loop *loop)
5e962776
ZD
1511{
1512 struct invariant *inv;
1513 unsigned i;
1514
ac47786e 1515 FOR_EACH_VEC_ELT (invariant_p, invariants, i, inv)
1052bd54 1516 move_invariant_reg (loop, i);
1833192f
VM
1517 if (flag_ira_loop_pressure && resize_reg_info ())
1518 {
ac47786e 1519 FOR_EACH_VEC_ELT (invariant_p, invariants, i, inv)
1833192f
VM
1520 if (inv->reg != NULL_RTX)
1521 {
1522 if (inv->orig_regno >= 0)
1523 setup_reg_classes (REGNO (inv->reg),
1524 reg_preferred_class (inv->orig_regno),
1525 reg_alternate_class (inv->orig_regno),
1756cb66 1526 reg_allocno_class (inv->orig_regno));
1833192f
VM
1527 else
1528 setup_reg_classes (REGNO (inv->reg),
1529 GENERAL_REGS, NO_REGS, GENERAL_REGS);
1530 }
1531 }
5e962776
ZD
1532}
1533
1534/* Initializes invariant motion data. */
1535
1536static void
1537init_inv_motion_data (void)
1538{
1539 actual_stamp = 1;
1540
edd954e6 1541 invariants = VEC_alloc (invariant_p, heap, 100);
5e962776
ZD
1542}
1543
cb20f7e8 1544/* Frees the data allocated by invariant motion. */
5e962776
ZD
1545
1546static void
cb20f7e8 1547free_inv_motion_data (void)
5e962776
ZD
1548{
1549 unsigned i;
1550 struct def *def;
1551 struct invariant *inv;
1552
6fb5fa3c
DB
1553 check_invariant_table_size ();
1554 for (i = 0; i < DF_DEFS_TABLE_SIZE (); i++)
5e962776 1555 {
6fb5fa3c
DB
1556 inv = invariant_table[i];
1557 if (inv)
1558 {
1559 def = inv->def;
1560 gcc_assert (def != NULL);
b8698a0f 1561
6fb5fa3c
DB
1562 free_use_list (def->uses);
1563 free (def);
1564 invariant_table[i] = NULL;
1565 }
5e962776
ZD
1566 }
1567
ac47786e 1568 FOR_EACH_VEC_ELT (invariant_p, invariants, i, inv)
5e962776 1569 {
8bdbfff5 1570 BITMAP_FREE (inv->depends_on);
5e962776
ZD
1571 free (inv);
1572 }
edd954e6 1573 VEC_free (invariant_p, heap, invariants);
5e962776
ZD
1574}
1575
cb20f7e8 1576/* Move the invariants out of the LOOP. */
5e962776
ZD
1577
1578static void
cb20f7e8 1579move_single_loop_invariants (struct loop *loop)
5e962776
ZD
1580{
1581 init_inv_motion_data ();
1582
cb20f7e8 1583 find_invariants (loop);
bec922f0
SL
1584 find_invariants_to_move (optimize_loop_for_speed_p (loop),
1585 LOOP_DATA (loop)->has_call);
cb20f7e8 1586 move_invariants (loop);
5e962776 1587
cb20f7e8 1588 free_inv_motion_data ();
5e962776
ZD
1589}
1590
1591/* Releases the auxiliary data for LOOP. */
1592
1593static void
1594free_loop_data (struct loop *loop)
1595{
1596 struct loop_data *data = LOOP_DATA (loop);
eb149440
RG
1597 if (!data)
1598 return;
5e962776 1599
1833192f
VM
1600 bitmap_clear (&LOOP_DATA (loop)->regs_ref);
1601 bitmap_clear (&LOOP_DATA (loop)->regs_live);
5e962776
ZD
1602 free (data);
1603 loop->aux = NULL;
1604}
1605
1833192f
VM
1606\f
1607
1608/* Registers currently living. */
1609static bitmap_head curr_regs_live;
1610
1756cb66 1611/* Current reg pressure for each pressure class. */
1833192f
VM
1612static int curr_reg_pressure[N_REG_CLASSES];
1613
1614/* Record all regs that are set in any one insn. Communication from
1615 mark_reg_{store,clobber} and global_conflicts. Asm can refer to
1616 all hard-registers. */
1617static rtx regs_set[(FIRST_PSEUDO_REGISTER > MAX_RECOG_OPERANDS
1618 ? FIRST_PSEUDO_REGISTER : MAX_RECOG_OPERANDS) * 2];
1619/* Number of regs stored in the previous array. */
1620static int n_regs_set;
1621
1756cb66 1622/* Return pressure class and number of needed hard registers (through
b8698a0f 1623 *NREGS) of register REGNO. */
1833192f 1624static enum reg_class
1756cb66 1625get_regno_pressure_class (int regno, int *nregs)
1833192f
VM
1626{
1627 if (regno >= FIRST_PSEUDO_REGISTER)
1628 {
1756cb66 1629 enum reg_class pressure_class;
1833192f 1630
1756cb66
VM
1631 pressure_class = reg_allocno_class (regno);
1632 pressure_class = ira_pressure_class_translate[pressure_class];
1633 *nregs
1634 = ira_reg_class_max_nregs[pressure_class][PSEUDO_REGNO_MODE (regno)];
1635 return pressure_class;
1833192f
VM
1636 }
1637 else if (! TEST_HARD_REG_BIT (ira_no_alloc_regs, regno)
1638 && ! TEST_HARD_REG_BIT (eliminable_regset, regno))
1639 {
1640 *nregs = 1;
1756cb66 1641 return ira_pressure_class_translate[REGNO_REG_CLASS (regno)];
1833192f
VM
1642 }
1643 else
1644 {
1645 *nregs = 0;
1646 return NO_REGS;
1647 }
1648}
1649
1650/* Increase (if INCR_P) or decrease current register pressure for
1651 register REGNO. */
1652static void
1653change_pressure (int regno, bool incr_p)
1654{
1655 int nregs;
1756cb66 1656 enum reg_class pressure_class;
1833192f 1657
1756cb66 1658 pressure_class = get_regno_pressure_class (regno, &nregs);
1833192f 1659 if (! incr_p)
1756cb66 1660 curr_reg_pressure[pressure_class] -= nregs;
1833192f
VM
1661 else
1662 {
1756cb66
VM
1663 curr_reg_pressure[pressure_class] += nregs;
1664 if (LOOP_DATA (curr_loop)->max_reg_pressure[pressure_class]
1665 < curr_reg_pressure[pressure_class])
1666 LOOP_DATA (curr_loop)->max_reg_pressure[pressure_class]
1667 = curr_reg_pressure[pressure_class];
1833192f
VM
1668 }
1669}
1670
1671/* Mark REGNO birth. */
1672static void
1673mark_regno_live (int regno)
1674{
1675 struct loop *loop;
1676
1677 for (loop = curr_loop;
1678 loop != current_loops->tree_root;
1679 loop = loop_outer (loop))
1680 bitmap_set_bit (&LOOP_DATA (loop)->regs_live, regno);
fcaa4ca4 1681 if (!bitmap_set_bit (&curr_regs_live, regno))
1833192f 1682 return;
1833192f
VM
1683 change_pressure (regno, true);
1684}
1685
1686/* Mark REGNO death. */
1687static void
1688mark_regno_death (int regno)
1689{
fcaa4ca4 1690 if (! bitmap_clear_bit (&curr_regs_live, regno))
1833192f 1691 return;
1833192f
VM
1692 change_pressure (regno, false);
1693}
1694
1695/* Mark setting register REG. */
1696static void
1697mark_reg_store (rtx reg, const_rtx setter ATTRIBUTE_UNUSED,
1698 void *data ATTRIBUTE_UNUSED)
1699{
1700 int regno;
1701
1702 if (GET_CODE (reg) == SUBREG)
1703 reg = SUBREG_REG (reg);
1704
1705 if (! REG_P (reg))
1706 return;
1707
1708 regs_set[n_regs_set++] = reg;
1709
1710 regno = REGNO (reg);
1711
1712 if (regno >= FIRST_PSEUDO_REGISTER)
1713 mark_regno_live (regno);
1714 else
1715 {
1716 int last = regno + hard_regno_nregs[regno][GET_MODE (reg)];
1717
1718 while (regno < last)
1719 {
1720 mark_regno_live (regno);
1721 regno++;
1722 }
1723 }
1724}
1725
1726/* Mark clobbering register REG. */
1727static void
1728mark_reg_clobber (rtx reg, const_rtx setter, void *data)
1729{
1730 if (GET_CODE (setter) == CLOBBER)
1731 mark_reg_store (reg, setter, data);
1732}
1733
1734/* Mark register REG death. */
1735static void
1736mark_reg_death (rtx reg)
1737{
1738 int regno = REGNO (reg);
1739
1740 if (regno >= FIRST_PSEUDO_REGISTER)
1741 mark_regno_death (regno);
1742 else
1743 {
1744 int last = regno + hard_regno_nregs[regno][GET_MODE (reg)];
1745
1746 while (regno < last)
1747 {
1748 mark_regno_death (regno);
1749 regno++;
1750 }
1751 }
1752}
1753
1754/* Mark occurrence of registers in X for the current loop. */
1755static void
1756mark_ref_regs (rtx x)
1757{
1758 RTX_CODE code;
1759 int i;
1760 const char *fmt;
1761
1762 if (!x)
1763 return;
1764
1765 code = GET_CODE (x);
1766 if (code == REG)
1767 {
1768 struct loop *loop;
b8698a0f 1769
1833192f
VM
1770 for (loop = curr_loop;
1771 loop != current_loops->tree_root;
1772 loop = loop_outer (loop))
1773 bitmap_set_bit (&LOOP_DATA (loop)->regs_ref, REGNO (x));
1774 return;
1775 }
1776
1777 fmt = GET_RTX_FORMAT (code);
1778 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1779 if (fmt[i] == 'e')
1780 mark_ref_regs (XEXP (x, i));
1781 else if (fmt[i] == 'E')
1782 {
1783 int j;
b8698a0f 1784
1833192f
VM
1785 for (j = 0; j < XVECLEN (x, i); j++)
1786 mark_ref_regs (XVECEXP (x, i, j));
1787 }
1788}
1789
1790/* Calculate register pressure in the loops. */
1791static void
1792calculate_loop_reg_pressure (void)
1793{
1794 int i;
1795 unsigned int j;
1796 bitmap_iterator bi;
1797 basic_block bb;
1798 rtx insn, link;
1799 struct loop *loop, *parent;
1800 loop_iterator li;
1801
1802 FOR_EACH_LOOP (li, loop, 0)
1803 if (loop->aux == NULL)
1804 {
1805 loop->aux = xcalloc (1, sizeof (struct loop_data));
1806 bitmap_initialize (&LOOP_DATA (loop)->regs_ref, &reg_obstack);
1807 bitmap_initialize (&LOOP_DATA (loop)->regs_live, &reg_obstack);
1808 }
1809 ira_setup_eliminable_regset ();
1810 bitmap_initialize (&curr_regs_live, &reg_obstack);
1811 FOR_EACH_BB (bb)
1812 {
1813 curr_loop = bb->loop_father;
1814 if (curr_loop == current_loops->tree_root)
1815 continue;
1816
1817 for (loop = curr_loop;
1818 loop != current_loops->tree_root;
1819 loop = loop_outer (loop))
1820 bitmap_ior_into (&LOOP_DATA (loop)->regs_live, DF_LR_IN (bb));
1821
1822 bitmap_copy (&curr_regs_live, DF_LR_IN (bb));
1756cb66
VM
1823 for (i = 0; i < ira_pressure_classes_num; i++)
1824 curr_reg_pressure[ira_pressure_classes[i]] = 0;
1833192f
VM
1825 EXECUTE_IF_SET_IN_BITMAP (&curr_regs_live, 0, j, bi)
1826 change_pressure (j, true);
1827
1828 FOR_BB_INSNS (bb, insn)
1829 {
dd8c071d 1830 if (! NONDEBUG_INSN_P (insn))
1833192f
VM
1831 continue;
1832
1833 mark_ref_regs (PATTERN (insn));
1834 n_regs_set = 0;
1835 note_stores (PATTERN (insn), mark_reg_clobber, NULL);
b8698a0f 1836
1833192f 1837 /* Mark any registers dead after INSN as dead now. */
b8698a0f 1838
1833192f
VM
1839 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1840 if (REG_NOTE_KIND (link) == REG_DEAD)
1841 mark_reg_death (XEXP (link, 0));
b8698a0f 1842
1833192f
VM
1843 /* Mark any registers set in INSN as live,
1844 and mark them as conflicting with all other live regs.
1845 Clobbers are processed again, so they conflict with
1846 the registers that are set. */
b8698a0f 1847
1833192f 1848 note_stores (PATTERN (insn), mark_reg_store, NULL);
b8698a0f 1849
1833192f
VM
1850#ifdef AUTO_INC_DEC
1851 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1852 if (REG_NOTE_KIND (link) == REG_INC)
1853 mark_reg_store (XEXP (link, 0), NULL_RTX, NULL);
1854#endif
1855 while (n_regs_set-- > 0)
1856 {
1857 rtx note = find_regno_note (insn, REG_UNUSED,
1858 REGNO (regs_set[n_regs_set]));
1859 if (! note)
1860 continue;
b8698a0f 1861
1833192f
VM
1862 mark_reg_death (XEXP (note, 0));
1863 }
1864 }
1865 }
1866 bitmap_clear (&curr_regs_live);
1867 if (flag_ira_region == IRA_REGION_MIXED
1868 || flag_ira_region == IRA_REGION_ALL)
1869 FOR_EACH_LOOP (li, loop, 0)
1870 {
1871 EXECUTE_IF_SET_IN_BITMAP (&LOOP_DATA (loop)->regs_live, 0, j, bi)
1872 if (! bitmap_bit_p (&LOOP_DATA (loop)->regs_ref, j))
1873 {
1756cb66 1874 enum reg_class pressure_class;
1833192f
VM
1875 int nregs;
1876
1756cb66
VM
1877 pressure_class = get_regno_pressure_class (j, &nregs);
1878 LOOP_DATA (loop)->max_reg_pressure[pressure_class] -= nregs;
1833192f
VM
1879 }
1880 }
1881 if (dump_file == NULL)
1882 return;
1883 FOR_EACH_LOOP (li, loop, 0)
1884 {
1885 parent = loop_outer (loop);
1886 fprintf (dump_file, "\n Loop %d (parent %d, header bb%d, depth %d)\n",
1887 loop->num, (parent == NULL ? -1 : parent->num),
1888 loop->header->index, loop_depth (loop));
1889 fprintf (dump_file, "\n ref. regnos:");
1890 EXECUTE_IF_SET_IN_BITMAP (&LOOP_DATA (loop)->regs_ref, 0, j, bi)
1891 fprintf (dump_file, " %d", j);
1892 fprintf (dump_file, "\n live regnos:");
1893 EXECUTE_IF_SET_IN_BITMAP (&LOOP_DATA (loop)->regs_live, 0, j, bi)
1894 fprintf (dump_file, " %d", j);
1895 fprintf (dump_file, "\n Pressure:");
1756cb66 1896 for (i = 0; (int) i < ira_pressure_classes_num; i++)
1833192f 1897 {
1756cb66 1898 enum reg_class pressure_class;
b8698a0f 1899
1756cb66
VM
1900 pressure_class = ira_pressure_classes[i];
1901 if (LOOP_DATA (loop)->max_reg_pressure[pressure_class] == 0)
1833192f 1902 continue;
1756cb66
VM
1903 fprintf (dump_file, " %s=%d", reg_class_names[pressure_class],
1904 LOOP_DATA (loop)->max_reg_pressure[pressure_class]);
1833192f
VM
1905 }
1906 fprintf (dump_file, "\n");
1907 }
1908}
1909
1910\f
1911
d73be268 1912/* Move the invariants out of the loops. */
5e962776
ZD
1913
1914void
d73be268 1915move_loop_invariants (void)
5e962776
ZD
1916{
1917 struct loop *loop;
42fd6772 1918 loop_iterator li;
cb20f7e8 1919
1833192f
VM
1920 if (flag_ira_loop_pressure)
1921 {
1922 df_analyze ();
1756cb66 1923 regstat_init_n_sets_and_refs ();
1833192f
VM
1924 ira_set_pseudo_classes (dump_file);
1925 calculate_loop_reg_pressure ();
1756cb66 1926 regstat_free_n_sets_and_refs ();
1833192f 1927 }
6fb5fa3c 1928 df_set_flags (DF_EQ_NOTES + DF_DEFER_INSN_RESCAN);
5e962776 1929 /* Process the loops, innermost first. */
42fd6772 1930 FOR_EACH_LOOP (li, loop, LI_FROM_INNERMOST)
5e962776 1931 {
1833192f 1932 curr_loop = loop;
b1fb9f56
JJ
1933 /* move_single_loop_invariants for very large loops
1934 is time consuming and might need a lot of memory. */
1935 if (loop->num_nodes <= (unsigned) LOOP_INVARIANT_MAX_BBS_IN_LOOP)
1936 move_single_loop_invariants (loop);
5e962776
ZD
1937 }
1938
42fd6772
ZD
1939 FOR_EACH_LOOP (li, loop, 0)
1940 {
1941 free_loop_data (loop);
1942 }
5e962776 1943
1833192f
VM
1944 if (flag_ira_loop_pressure)
1945 /* There is no sense to keep this info because it was most
1946 probably outdated by subsequent passes. */
1947 free_reg_info ();
6fb5fa3c
DB
1948 free (invariant_table);
1949 invariant_table = NULL;
1950 invariant_table_size = 0;
a7f4ccb1
SB
1951
1952#ifdef ENABLE_CHECKING
1953 verify_flow_info ();
1954#endif
5e962776 1955}