1 /* Store motion via Lazy Code Motion on the reverse CFG.
2 Copyright (C) 1997-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
24 #include "diagnostic-core.h"
33 #include "hard-reg-set.h"
35 #include "insn-config.h"
39 #include "dominance.h"
44 #include "cfgcleanup.h"
45 #include "basic-block.h"
56 #include "tree-pass.h"
61 /* This pass implements downward store motion.
62 As of May 1, 2009, the pass is not enabled by default on any target,
63 but bootstrap completes on ia64 and x86_64 with the pass enabled. */
66 - remove_reachable_equiv_notes is an incomprehensible pile of goo and
67 a compile time hog that needs a rewrite (maybe cache st_exprs to
68 invalidate REG_EQUAL/REG_EQUIV notes for?).
69 - pattern_regs in st_expr should be a regset (on its own obstack).
70 - antic_stores and avail_stores should be VECs instead of lists.
71 - store_motion_mems should be a vec instead of a list.
72 - there should be an alloc pool for struct st_expr objects.
73 - investigate whether it is helpful to make the address of an st_expr
75 - when GIMPLE alias information is exported, the effectiveness of this
76 pass should be re-evaluated.
79 /* This is a list of store expressions (MEMs). The structure is used
80 as an expression table to track stores which look interesting, and
81 might be moveable towards the exit block. */
85 /* Pattern of this mem. */
87 /* List of registers mentioned by the mem. */
89 /* INSN list of stores that are locally anticipatable. */
90 rtx_insn_list
*antic_stores
;
91 /* INSN list of stores that are locally available. */
92 rtx_insn_list
*avail_stores
;
93 /* Next in the list. */
94 struct st_expr
* next
;
95 /* Store ID in the dataflow bitmaps. */
97 /* Hash value for the hash table. */
98 unsigned int hash_index
;
99 /* Register holding the stored expression when a store is moved.
100 This field is also used as a cache in find_moveable_store, see
101 LAST_AVAIL_CHECK_FAILURE below. */
105 /* Head of the list of load/store memory refs. */
106 static struct st_expr
* store_motion_mems
= NULL
;
108 /* These bitmaps will hold the local dataflow properties per basic block. */
109 static sbitmap
*st_kill
, *st_avloc
, *st_antloc
, *st_transp
;
111 /* Nonzero for expressions which should be inserted on a specific edge. */
112 static sbitmap
*st_insert_map
;
114 /* Nonzero for expressions which should be deleted in a specific block. */
115 static sbitmap
*st_delete_map
;
117 /* Global holding the number of store expressions we are dealing with. */
118 static int num_stores
;
120 /* Contains the edge_list returned by pre_edge_lcm. */
121 static struct edge_list
*edge_list
;
123 /* Hashtable helpers. */
125 struct st_expr_hasher
: typed_noop_remove
<st_expr
>
127 typedef st_expr
*value_type
;
128 typedef st_expr
*compare_type
;
129 static inline hashval_t
hash (const st_expr
*);
130 static inline bool equal (const st_expr
*, const st_expr
*);
134 st_expr_hasher::hash (const st_expr
*x
)
136 int do_not_record_p
= 0;
137 return hash_rtx (x
->pattern
, GET_MODE (x
->pattern
), &do_not_record_p
, NULL
, false);
141 st_expr_hasher::equal (const st_expr
*ptr1
, const st_expr
*ptr2
)
143 return exp_equiv_p (ptr1
->pattern
, ptr2
->pattern
, 0, true);
146 /* Hashtable for the load/store memory refs. */
147 static hash_table
<st_expr_hasher
> *store_motion_mems_table
;
149 /* This will search the st_expr list for a matching expression. If it
150 doesn't find one, we create one and initialize it. */
152 static struct st_expr
*
153 st_expr_entry (rtx x
)
155 int do_not_record_p
= 0;
156 struct st_expr
* ptr
;
161 hash
= hash_rtx (x
, GET_MODE (x
), &do_not_record_p
,
162 NULL
, /*have_reg_qty=*/false);
165 slot
= store_motion_mems_table
->find_slot_with_hash (&e
, hash
, INSERT
);
169 ptr
= XNEW (struct st_expr
);
171 ptr
->next
= store_motion_mems
;
173 ptr
->pattern_regs
= NULL_RTX
;
174 ptr
->antic_stores
= NULL
;
175 ptr
->avail_stores
= NULL
;
176 ptr
->reaching_reg
= NULL_RTX
;
178 ptr
->hash_index
= hash
;
179 store_motion_mems
= ptr
;
185 /* Free up an individual st_expr entry. */
188 free_st_expr_entry (struct st_expr
* ptr
)
190 free_INSN_LIST_list (& ptr
->antic_stores
);
191 free_INSN_LIST_list (& ptr
->avail_stores
);
196 /* Free up all memory associated with the st_expr list. */
199 free_store_motion_mems (void)
201 delete store_motion_mems_table
;
202 store_motion_mems_table
= NULL
;
204 while (store_motion_mems
)
206 struct st_expr
* tmp
= store_motion_mems
;
207 store_motion_mems
= store_motion_mems
->next
;
208 free_st_expr_entry (tmp
);
210 store_motion_mems
= NULL
;
213 /* Assign each element of the list of mems a monotonically increasing value. */
216 enumerate_store_motion_mems (void)
218 struct st_expr
* ptr
;
221 for (ptr
= store_motion_mems
; ptr
!= NULL
; ptr
= ptr
->next
)
227 /* Return first item in the list. */
229 static inline struct st_expr
*
232 return store_motion_mems
;
235 /* Return the next item in the list after the specified one. */
237 static inline struct st_expr
*
238 next_st_expr (struct st_expr
* ptr
)
243 /* Dump debugging info about the store_motion_mems list. */
246 print_store_motion_mems (FILE * file
)
248 struct st_expr
* ptr
;
250 fprintf (dump_file
, "STORE_MOTION list of MEM exprs considered:\n");
252 for (ptr
= first_st_expr (); ptr
!= NULL
; ptr
= next_st_expr (ptr
))
254 fprintf (file
, " Pattern (%3d): ", ptr
->index
);
256 print_rtl (file
, ptr
->pattern
);
258 fprintf (file
, "\n ANTIC stores : ");
260 if (ptr
->antic_stores
)
261 print_rtl (file
, ptr
->antic_stores
);
263 fprintf (file
, "(nil)");
265 fprintf (file
, "\n AVAIL stores : ");
267 if (ptr
->avail_stores
)
268 print_rtl (file
, ptr
->avail_stores
);
270 fprintf (file
, "(nil)");
272 fprintf (file
, "\n\n");
275 fprintf (file
, "\n");
278 /* Return zero if some of the registers in list X are killed
279 due to set of registers in bitmap REGS_SET. */
282 store_ops_ok (const_rtx x
, int *regs_set
)
286 for (; x
; x
= XEXP (x
, 1))
289 if (regs_set
[REGNO (reg
)])
296 /* Returns a list of registers mentioned in X.
297 FIXME: A regset would be prettier and less expensive. */
299 static rtx_expr_list
*
300 extract_mentioned_regs (rtx x
)
302 rtx_expr_list
*mentioned_regs
= NULL
;
303 subrtx_var_iterator::array_type array
;
304 FOR_EACH_SUBRTX_VAR (iter
, array
, x
, NONCONST
)
308 mentioned_regs
= alloc_EXPR_LIST (0, x
, mentioned_regs
);
310 return mentioned_regs
;
313 /* Check to see if the load X is aliased with STORE_PATTERN.
314 AFTER is true if we are checking the case when STORE_PATTERN occurs
318 load_kills_store (const_rtx x
, const_rtx store_pattern
, int after
)
321 return anti_dependence (x
, store_pattern
);
323 return true_dependence (store_pattern
, GET_MODE (store_pattern
), x
);
326 /* Go through the entire rtx X, looking for any loads which might alias
327 STORE_PATTERN. Return true if found.
328 AFTER is true if we are checking the case when STORE_PATTERN occurs
332 find_loads (const_rtx x
, const_rtx store_pattern
, int after
)
341 if (GET_CODE (x
) == SET
)
346 if (load_kills_store (x
, store_pattern
, after
))
350 /* Recursively process the insn. */
351 fmt
= GET_RTX_FORMAT (GET_CODE (x
));
353 for (i
= GET_RTX_LENGTH (GET_CODE (x
)) - 1; i
>= 0 && !ret
; i
--)
356 ret
|= find_loads (XEXP (x
, i
), store_pattern
, after
);
357 else if (fmt
[i
] == 'E')
358 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
359 ret
|= find_loads (XVECEXP (x
, i
, j
), store_pattern
, after
);
364 /* Go through pattern PAT looking for any loads which might kill the
365 store in X. Return true if found.
366 AFTER is true if we are checking the case when loads kill X occurs
367 after the insn for PAT. */
370 store_killed_in_pat (const_rtx x
, const_rtx pat
, int after
)
372 if (GET_CODE (pat
) == SET
)
374 rtx dest
= SET_DEST (pat
);
376 if (GET_CODE (dest
) == ZERO_EXTRACT
)
377 dest
= XEXP (dest
, 0);
379 /* Check for memory stores to aliased objects. */
381 && !exp_equiv_p (dest
, x
, 0, true))
385 if (output_dependence (dest
, x
))
390 if (output_dependence (x
, dest
))
396 if (find_loads (pat
, x
, after
))
402 /* Check if INSN kills the store pattern X (is aliased with it).
403 AFTER is true if we are checking the case when store X occurs
404 after the insn. Return true if it does. */
407 store_killed_in_insn (const_rtx x
, const_rtx x_regs
, const rtx_insn
*insn
, int after
)
409 const_rtx reg
, note
, pat
;
411 if (! NONDEBUG_INSN_P (insn
))
416 /* A normal or pure call might read from pattern,
417 but a const call will not. */
418 if (!RTL_CONST_CALL_P (insn
))
421 /* But even a const call reads its parameters. Check whether the
422 base of some of registers used in mem is stack pointer. */
423 for (reg
= x_regs
; reg
; reg
= XEXP (reg
, 1))
424 if (may_be_sp_based_p (XEXP (reg
, 0)))
430 pat
= PATTERN (insn
);
431 if (GET_CODE (pat
) == SET
)
433 if (store_killed_in_pat (x
, pat
, after
))
436 else if (GET_CODE (pat
) == PARALLEL
)
440 for (i
= 0; i
< XVECLEN (pat
, 0); i
++)
441 if (store_killed_in_pat (x
, XVECEXP (pat
, 0, i
), after
))
444 else if (find_loads (PATTERN (insn
), x
, after
))
447 /* If this insn has a REG_EQUAL or REG_EQUIV note referencing a memory
448 location aliased with X, then this insn kills X. */
449 note
= find_reg_equal_equiv_note (insn
);
452 note
= XEXP (note
, 0);
454 /* However, if the note represents a must alias rather than a may
455 alias relationship, then it does not kill X. */
456 if (exp_equiv_p (note
, x
, 0, true))
459 /* See if there are any aliased loads in the note. */
460 return find_loads (note
, x
, after
);
463 /* Returns true if the expression X is loaded or clobbered on or after INSN
464 within basic block BB. REGS_SET_AFTER is bitmap of registers set in
465 or after the insn. X_REGS is list of registers mentioned in X. If the store
466 is killed, return the last insn in that it occurs in FAIL_INSN. */
469 store_killed_after (const_rtx x
, const_rtx x_regs
, const rtx_insn
*insn
,
470 const_basic_block bb
,
471 int *regs_set_after
, rtx
*fail_insn
)
473 rtx_insn
*last
= BB_END (bb
), *act
;
475 if (!store_ops_ok (x_regs
, regs_set_after
))
477 /* We do not know where it will happen. */
479 *fail_insn
= NULL_RTX
;
483 /* Scan from the end, so that fail_insn is determined correctly. */
484 for (act
= last
; act
!= PREV_INSN (insn
); act
= PREV_INSN (act
))
485 if (store_killed_in_insn (x
, x_regs
, act
, false))
495 /* Returns true if the expression X is loaded or clobbered on or before INSN
496 within basic block BB. X_REGS is list of registers mentioned in X.
497 REGS_SET_BEFORE is bitmap of registers set before or in this insn. */
499 store_killed_before (const_rtx x
, const_rtx x_regs
, const rtx_insn
*insn
,
500 const_basic_block bb
, int *regs_set_before
)
502 rtx_insn
*first
= BB_HEAD (bb
);
504 if (!store_ops_ok (x_regs
, regs_set_before
))
507 for ( ; insn
!= PREV_INSN (first
); insn
= PREV_INSN (insn
))
508 if (store_killed_in_insn (x
, x_regs
, insn
, true))
514 /* The last insn in the basic block that compute_store_table is processing,
515 where store_killed_after is true for X.
516 Since we go through the basic block from BB_END to BB_HEAD, this is
517 also the available store at the end of the basic block. Therefore
518 this is in effect a cache, to avoid calling store_killed_after for
519 equivalent aliasing store expressions.
520 This value is only meaningful during the computation of the store
521 table. We hi-jack the REACHING_REG field of struct st_expr to save
523 #define LAST_AVAIL_CHECK_FAILURE(x) ((x)->reaching_reg)
525 /* Determine whether INSN is MEM store pattern that we will consider moving.
526 REGS_SET_BEFORE is bitmap of registers set before (and including) the
527 current insn, REGS_SET_AFTER is bitmap of registers set after (and
528 including) the insn in this basic block. We must be passing through BB from
529 head to end, as we are using this fact to speed things up.
531 The results are stored this way:
533 -- the first anticipatable expression is added into ANTIC_STORES
534 -- if the processed expression is not anticipatable, NULL_RTX is added
535 there instead, so that we can use it as indicator that no further
536 expression of this type may be anticipatable
537 -- if the expression is available, it is added as head of AVAIL_STORES;
538 consequently, all of them but this head are dead and may be deleted.
539 -- if the expression is not available, the insn due to that it fails to be
540 available is stored in REACHING_REG (via LAST_AVAIL_CHECK_FAILURE).
542 The things are complicated a bit by fact that there already may be stores
543 to the same MEM from other blocks; also caller must take care of the
544 necessary cleanup of the temporary markers after end of the basic block.
548 find_moveable_store (rtx_insn
*insn
, int *regs_set_before
, int *regs_set_after
)
550 struct st_expr
* ptr
;
552 int check_anticipatable
, check_available
;
553 basic_block bb
= BLOCK_FOR_INSN (insn
);
555 set
= single_set (insn
);
559 dest
= SET_DEST (set
);
561 if (! MEM_P (dest
) || MEM_VOLATILE_P (dest
)
562 || GET_MODE (dest
) == BLKmode
)
565 if (side_effects_p (dest
))
568 /* If we are handling exceptions, we must be careful with memory references
569 that may trap. If we are not, the behavior is undefined, so we may just
571 if (cfun
->can_throw_non_call_exceptions
&& may_trap_p (dest
))
574 /* Even if the destination cannot trap, the source may. In this case we'd
575 need to handle updating the REG_EH_REGION note. */
576 if (find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
))
579 /* Make sure that the SET_SRC of this store insns can be assigned to
580 a register, or we will fail later on in replace_store_insn, which
581 assumes that we can do this. But sometimes the target machine has
582 oddities like MEM read-modify-write instruction. See for example
584 if (!can_assign_to_reg_without_clobbers_p (SET_SRC (set
)))
587 ptr
= st_expr_entry (dest
);
588 if (!ptr
->pattern_regs
)
589 ptr
->pattern_regs
= extract_mentioned_regs (dest
);
591 /* Do not check for anticipatability if we either found one anticipatable
592 store already, or tested for one and found out that it was killed. */
593 check_anticipatable
= 0;
594 if (!ptr
->antic_stores
)
595 check_anticipatable
= 1;
598 rtx_insn
*tmp
= ptr
->antic_stores
->insn ();
600 && BLOCK_FOR_INSN (tmp
) != bb
)
601 check_anticipatable
= 1;
603 if (check_anticipatable
)
606 if (store_killed_before (dest
, ptr
->pattern_regs
, insn
, bb
, regs_set_before
))
610 ptr
->antic_stores
= alloc_INSN_LIST (tmp
, ptr
->antic_stores
);
613 /* It is not necessary to check whether store is available if we did
614 it successfully before; if we failed before, do not bother to check
615 until we reach the insn that caused us to fail. */
617 if (!ptr
->avail_stores
)
621 rtx_insn
*tmp
= ptr
->avail_stores
->insn ();
622 if (BLOCK_FOR_INSN (tmp
) != bb
)
627 /* Check that we have already reached the insn at that the check
629 if (LAST_AVAIL_CHECK_FAILURE (ptr
))
632 for (tmp
= BB_END (bb
);
633 tmp
!= insn
&& tmp
!= LAST_AVAIL_CHECK_FAILURE (ptr
);
634 tmp
= PREV_INSN (tmp
))
640 check_available
= store_killed_after (dest
, ptr
->pattern_regs
, insn
,
642 &LAST_AVAIL_CHECK_FAILURE (ptr
));
644 if (!check_available
)
645 ptr
->avail_stores
= alloc_INSN_LIST (insn
, ptr
->avail_stores
);
648 /* Find available and anticipatable stores. */
651 compute_store_table (void)
655 #ifdef ENABLE_CHECKING
661 int *last_set_in
, *already_set
;
662 struct st_expr
* ptr
, **prev_next_ptr_ptr
;
663 unsigned int max_gcse_regno
= max_reg_num ();
665 store_motion_mems
= NULL
;
666 store_motion_mems_table
= new hash_table
<st_expr_hasher
> (13);
667 last_set_in
= XCNEWVEC (int, max_gcse_regno
);
668 already_set
= XNEWVEC (int, max_gcse_regno
);
670 /* Find all the stores we care about. */
671 FOR_EACH_BB_FN (bb
, cfun
)
673 /* First compute the registers set in this block. */
674 FOR_BB_INSNS (bb
, insn
)
677 if (! NONDEBUG_INSN_P (insn
))
680 FOR_EACH_INSN_DEF (def
, insn
)
681 last_set_in
[DF_REF_REGNO (def
)] = INSN_UID (insn
);
684 /* Now find the stores. */
685 memset (already_set
, 0, sizeof (int) * max_gcse_regno
);
686 FOR_BB_INSNS (bb
, insn
)
688 if (! NONDEBUG_INSN_P (insn
))
691 FOR_EACH_INSN_DEF (def
, insn
)
692 already_set
[DF_REF_REGNO (def
)] = INSN_UID (insn
);
694 /* Now that we've marked regs, look for stores. */
695 find_moveable_store (insn
, already_set
, last_set_in
);
697 /* Unmark regs that are no longer set. */
698 FOR_EACH_INSN_DEF (def
, insn
)
699 if (last_set_in
[DF_REF_REGNO (def
)] == INSN_UID (insn
))
700 last_set_in
[DF_REF_REGNO (def
)] = 0;
703 #ifdef ENABLE_CHECKING
704 /* last_set_in should now be all-zero. */
705 for (regno
= 0; regno
< max_gcse_regno
; regno
++)
706 gcc_assert (!last_set_in
[regno
]);
709 /* Clear temporary marks. */
710 for (ptr
= first_st_expr (); ptr
!= NULL
; ptr
= next_st_expr (ptr
))
712 LAST_AVAIL_CHECK_FAILURE (ptr
) = NULL_RTX
;
713 if (ptr
->antic_stores
714 && (tmp
= ptr
->antic_stores
->insn ()) == NULL_RTX
)
715 ptr
->antic_stores
= ptr
->antic_stores
->next ();
719 /* Remove the stores that are not available anywhere, as there will
720 be no opportunity to optimize them. */
721 for (ptr
= store_motion_mems
, prev_next_ptr_ptr
= &store_motion_mems
;
723 ptr
= *prev_next_ptr_ptr
)
725 if (! ptr
->avail_stores
)
727 *prev_next_ptr_ptr
= ptr
->next
;
728 store_motion_mems_table
->remove_elt_with_hash (ptr
, ptr
->hash_index
);
729 free_st_expr_entry (ptr
);
732 prev_next_ptr_ptr
= &ptr
->next
;
735 ret
= enumerate_store_motion_mems ();
738 print_store_motion_mems (dump_file
);
745 /* In all code following after this, REACHING_REG has its original
746 meaning again. Avoid confusion, and undef the accessor macro for
747 the temporary marks usage in compute_store_table. */
748 #undef LAST_AVAIL_CHECK_FAILURE
750 /* Insert an instruction at the beginning of a basic block, and update
751 the BB_HEAD if needed. */
754 insert_insn_start_basic_block (rtx_insn
*insn
, basic_block bb
)
756 /* Insert at start of successor block. */
757 rtx_insn
*prev
= PREV_INSN (BB_HEAD (bb
));
758 rtx_insn
*before
= BB_HEAD (bb
);
761 if (! LABEL_P (before
)
762 && !NOTE_INSN_BASIC_BLOCK_P (before
))
765 if (prev
== BB_END (bb
))
767 before
= NEXT_INSN (before
);
770 insn
= emit_insn_after_noloc (insn
, prev
, bb
);
774 fprintf (dump_file
, "STORE_MOTION insert store at start of BB %d:\n",
776 print_inline_rtx (dump_file
, insn
, 6);
777 fprintf (dump_file
, "\n");
781 /* This routine will insert a store on an edge. EXPR is the st_expr entry for
782 the memory reference, and E is the edge to insert it on. Returns nonzero
783 if an edge insertion was performed. */
786 insert_store (struct st_expr
* expr
, edge e
)
794 /* We did all the deleted before this insert, so if we didn't delete a
795 store, then we haven't set the reaching reg yet either. */
796 if (expr
->reaching_reg
== NULL_RTX
)
799 if (e
->flags
& EDGE_FAKE
)
802 reg
= expr
->reaching_reg
;
803 insn
= gen_move_insn (copy_rtx (expr
->pattern
), reg
);
805 /* If we are inserting this expression on ALL predecessor edges of a BB,
806 insert it at the start of the BB, and reset the insert bits on the other
807 edges so we don't try to insert it on the other edges. */
809 FOR_EACH_EDGE (tmp
, ei
, e
->dest
->preds
)
810 if (!(tmp
->flags
& EDGE_FAKE
))
812 int index
= EDGE_INDEX (edge_list
, tmp
->src
, tmp
->dest
);
814 gcc_assert (index
!= EDGE_INDEX_NO_EDGE
);
815 if (! bitmap_bit_p (st_insert_map
[index
], expr
->index
))
819 /* If tmp is NULL, we found an insertion on every edge, blank the
820 insertion vector for these edges, and insert at the start of the BB. */
821 if (!tmp
&& bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
823 FOR_EACH_EDGE (tmp
, ei
, e
->dest
->preds
)
825 int index
= EDGE_INDEX (edge_list
, tmp
->src
, tmp
->dest
);
826 bitmap_clear_bit (st_insert_map
[index
], expr
->index
);
828 insert_insn_start_basic_block (insn
, bb
);
832 /* We can't put stores in the front of blocks pointed to by abnormal
833 edges since that may put a store where one didn't used to be. */
834 gcc_assert (!(e
->flags
& EDGE_ABNORMAL
));
836 insert_insn_on_edge (insn
, e
);
840 fprintf (dump_file
, "STORE_MOTION insert insn on edge (%d, %d):\n",
841 e
->src
->index
, e
->dest
->index
);
842 print_inline_rtx (dump_file
, insn
, 6);
843 fprintf (dump_file
, "\n");
849 /* Remove any REG_EQUAL or REG_EQUIV notes containing a reference to the
850 memory location in SMEXPR set in basic block BB.
852 This could be rather expensive. */
855 remove_reachable_equiv_notes (basic_block bb
, struct st_expr
*smexpr
)
857 edge_iterator
*stack
, ei
;
860 sbitmap visited
= sbitmap_alloc (last_basic_block_for_fn (cfun
));
863 rtx mem
= smexpr
->pattern
;
865 stack
= XNEWVEC (edge_iterator
, n_basic_blocks_for_fn (cfun
));
867 ei
= ei_start (bb
->succs
);
869 bitmap_clear (visited
);
871 act
= (EDGE_COUNT (ei_container (ei
)) > 0 ? EDGE_I (ei_container (ei
), 0) : NULL
);
879 sbitmap_free (visited
);
882 act
= ei_edge (stack
[--sp
]);
886 if (bb
== EXIT_BLOCK_PTR_FOR_FN (cfun
)
887 || bitmap_bit_p (visited
, bb
->index
))
891 act
= (! ei_end_p (ei
)) ? ei_edge (ei
) : NULL
;
894 bitmap_set_bit (visited
, bb
->index
);
896 if (bitmap_bit_p (st_antloc
[bb
->index
], smexpr
->index
))
898 for (last
= smexpr
->antic_stores
;
899 BLOCK_FOR_INSN (XEXP (last
, 0)) != bb
;
900 last
= XEXP (last
, 1))
902 last
= XEXP (last
, 0);
905 last
= NEXT_INSN (BB_END (bb
));
907 for (insn
= BB_HEAD (bb
); insn
!= last
; insn
= NEXT_INSN (insn
))
908 if (NONDEBUG_INSN_P (insn
))
910 note
= find_reg_equal_equiv_note (insn
);
911 if (!note
|| !exp_equiv_p (XEXP (note
, 0), mem
, 0, true))
915 fprintf (dump_file
, "STORE_MOTION drop REG_EQUAL note at insn %d:\n",
917 remove_note (insn
, note
);
922 act
= (! ei_end_p (ei
)) ? ei_edge (ei
) : NULL
;
924 if (EDGE_COUNT (bb
->succs
) > 0)
928 ei
= ei_start (bb
->succs
);
929 act
= (EDGE_COUNT (ei_container (ei
)) > 0 ? EDGE_I (ei_container (ei
), 0) : NULL
);
934 /* This routine will replace a store with a SET to a specified register. */
937 replace_store_insn (rtx reg
, rtx_insn
*del
, basic_block bb
,
938 struct st_expr
*smexpr
)
941 rtx mem
, note
, set
, ptr
;
943 mem
= smexpr
->pattern
;
944 insn
= gen_move_insn (reg
, SET_SRC (single_set (del
)));
946 for (ptr
= smexpr
->antic_stores
; ptr
; ptr
= XEXP (ptr
, 1))
947 if (XEXP (ptr
, 0) == del
)
949 XEXP (ptr
, 0) = insn
;
953 /* Move the notes from the deleted insn to its replacement. */
954 REG_NOTES (insn
) = REG_NOTES (del
);
956 /* Emit the insn AFTER all the notes are transferred.
957 This is cheaper since we avoid df rescanning for the note change. */
958 insn
= emit_insn_after (insn
, del
);
963 "STORE_MOTION delete insn in BB %d:\n ", bb
->index
);
964 print_inline_rtx (dump_file
, del
, 6);
965 fprintf (dump_file
, "\nSTORE_MOTION replaced with insn:\n ");
966 print_inline_rtx (dump_file
, insn
, 6);
967 fprintf (dump_file
, "\n");
972 /* Now we must handle REG_EQUAL notes whose contents is equal to the mem;
973 they are no longer accurate provided that they are reached by this
974 definition, so drop them. */
975 for (; insn
!= NEXT_INSN (BB_END (bb
)); insn
= NEXT_INSN (insn
))
976 if (NONDEBUG_INSN_P (insn
))
978 set
= single_set (insn
);
981 if (exp_equiv_p (SET_DEST (set
), mem
, 0, true))
983 note
= find_reg_equal_equiv_note (insn
);
984 if (!note
|| !exp_equiv_p (XEXP (note
, 0), mem
, 0, true))
988 fprintf (dump_file
, "STORE_MOTION drop REG_EQUAL note at insn %d:\n",
990 remove_note (insn
, note
);
992 remove_reachable_equiv_notes (bb
, smexpr
);
996 /* Delete a store, but copy the value that would have been stored into
997 the reaching_reg for later storing. */
1000 delete_store (struct st_expr
* expr
, basic_block bb
)
1004 if (expr
->reaching_reg
== NULL_RTX
)
1005 expr
->reaching_reg
= gen_reg_rtx_and_attrs (expr
->pattern
);
1007 reg
= expr
->reaching_reg
;
1009 for (rtx_insn_list
*i
= expr
->avail_stores
; i
; i
= i
->next ())
1011 rtx_insn
*del
= i
->insn ();
1012 if (BLOCK_FOR_INSN (del
) == bb
)
1014 /* We know there is only one since we deleted redundant
1015 ones during the available computation. */
1016 replace_store_insn (reg
, del
, bb
, expr
);
1022 /* Fill in available, anticipatable, transparent and kill vectors in
1023 STORE_DATA, based on lists of available and anticipatable stores. */
1025 build_store_vectors (void)
1028 int *regs_set_in_block
;
1031 struct st_expr
* ptr
;
1032 unsigned int max_gcse_regno
= max_reg_num ();
1034 /* Build the gen_vector. This is any store in the table which is not killed
1035 by aliasing later in its block. */
1036 st_avloc
= sbitmap_vector_alloc (last_basic_block_for_fn (cfun
),
1038 bitmap_vector_clear (st_avloc
, last_basic_block_for_fn (cfun
));
1040 st_antloc
= sbitmap_vector_alloc (last_basic_block_for_fn (cfun
),
1042 bitmap_vector_clear (st_antloc
, last_basic_block_for_fn (cfun
));
1044 for (ptr
= first_st_expr (); ptr
!= NULL
; ptr
= next_st_expr (ptr
))
1046 for (st
= ptr
->avail_stores
; st
!= NULL
; st
= st
->next ())
1049 bb
= BLOCK_FOR_INSN (insn
);
1051 /* If we've already seen an available expression in this block,
1052 we can delete this one (It occurs earlier in the block). We'll
1053 copy the SRC expression to an unused register in case there
1054 are any side effects. */
1055 if (bitmap_bit_p (st_avloc
[bb
->index
], ptr
->index
))
1057 rtx r
= gen_reg_rtx_and_attrs (ptr
->pattern
);
1059 fprintf (dump_file
, "Removing redundant store:\n");
1060 replace_store_insn (r
, st
->insn (), bb
, ptr
);
1063 bitmap_set_bit (st_avloc
[bb
->index
], ptr
->index
);
1066 for (st
= ptr
->antic_stores
; st
!= NULL
; st
= st
->next ())
1069 bb
= BLOCK_FOR_INSN (insn
);
1070 bitmap_set_bit (st_antloc
[bb
->index
], ptr
->index
);
1074 st_kill
= sbitmap_vector_alloc (last_basic_block_for_fn (cfun
), num_stores
);
1075 bitmap_vector_clear (st_kill
, last_basic_block_for_fn (cfun
));
1077 st_transp
= sbitmap_vector_alloc (last_basic_block_for_fn (cfun
), num_stores
);
1078 bitmap_vector_clear (st_transp
, last_basic_block_for_fn (cfun
));
1079 regs_set_in_block
= XNEWVEC (int, max_gcse_regno
);
1081 FOR_EACH_BB_FN (bb
, cfun
)
1083 memset (regs_set_in_block
, 0, sizeof (int) * max_gcse_regno
);
1085 FOR_BB_INSNS (bb
, insn
)
1086 if (NONDEBUG_INSN_P (insn
))
1089 FOR_EACH_INSN_DEF (def
, insn
)
1091 unsigned int ref_regno
= DF_REF_REGNO (def
);
1092 if (ref_regno
< max_gcse_regno
)
1093 regs_set_in_block
[DF_REF_REGNO (def
)] = 1;
1097 for (ptr
= first_st_expr (); ptr
!= NULL
; ptr
= next_st_expr (ptr
))
1099 if (store_killed_after (ptr
->pattern
, ptr
->pattern_regs
, BB_HEAD (bb
),
1100 bb
, regs_set_in_block
, NULL
))
1102 /* It should not be necessary to consider the expression
1103 killed if it is both anticipatable and available. */
1104 if (!bitmap_bit_p (st_antloc
[bb
->index
], ptr
->index
)
1105 || !bitmap_bit_p (st_avloc
[bb
->index
], ptr
->index
))
1106 bitmap_set_bit (st_kill
[bb
->index
], ptr
->index
);
1109 bitmap_set_bit (st_transp
[bb
->index
], ptr
->index
);
1113 free (regs_set_in_block
);
1117 dump_bitmap_vector (dump_file
, "st_antloc", "", st_antloc
,
1118 last_basic_block_for_fn (cfun
));
1119 dump_bitmap_vector (dump_file
, "st_kill", "", st_kill
,
1120 last_basic_block_for_fn (cfun
));
1121 dump_bitmap_vector (dump_file
, "st_transp", "", st_transp
,
1122 last_basic_block_for_fn (cfun
));
1123 dump_bitmap_vector (dump_file
, "st_avloc", "", st_avloc
,
1124 last_basic_block_for_fn (cfun
));
1128 /* Free memory used by store motion. */
1131 free_store_memory (void)
1133 free_store_motion_mems ();
1136 sbitmap_vector_free (st_avloc
);
1138 sbitmap_vector_free (st_kill
);
1140 sbitmap_vector_free (st_transp
);
1142 sbitmap_vector_free (st_antloc
);
1144 sbitmap_vector_free (st_insert_map
);
1146 sbitmap_vector_free (st_delete_map
);
1148 st_avloc
= st_kill
= st_transp
= st_antloc
= NULL
;
1149 st_insert_map
= st_delete_map
= NULL
;
1152 /* Perform store motion. Much like gcse, except we move expressions the
1153 other way by looking at the flowgraph in reverse.
1154 Return non-zero if transformations are performed by the pass. */
1157 one_store_motion_pass (void)
1161 struct st_expr
* ptr
;
1162 int did_edge_inserts
= 0;
1163 int n_stores_deleted
= 0;
1164 int n_stores_created
= 0;
1166 init_alias_analysis ();
1168 /* Find all the available and anticipatable stores. */
1169 num_stores
= compute_store_table ();
1170 if (num_stores
== 0)
1172 delete store_motion_mems_table
;
1173 store_motion_mems_table
= NULL
;
1174 end_alias_analysis ();
1178 /* Now compute kill & transp vectors. */
1179 build_store_vectors ();
1180 add_noreturn_fake_exit_edges ();
1181 connect_infinite_loops_to_exit ();
1183 edge_list
= pre_edge_rev_lcm (num_stores
, st_transp
, st_avloc
,
1184 st_antloc
, st_kill
, &st_insert_map
,
1187 /* Now we want to insert the new stores which are going to be needed. */
1188 for (ptr
= first_st_expr (); ptr
!= NULL
; ptr
= next_st_expr (ptr
))
1190 /* If any of the edges we have above are abnormal, we can't move this
1192 for (x
= NUM_EDGES (edge_list
) - 1; x
>= 0; x
--)
1193 if (bitmap_bit_p (st_insert_map
[x
], ptr
->index
)
1194 && (INDEX_EDGE (edge_list
, x
)->flags
& EDGE_ABNORMAL
))
1199 if (dump_file
!= NULL
)
1201 "Can't replace store %d: abnormal edge from %d to %d\n",
1202 ptr
->index
, INDEX_EDGE (edge_list
, x
)->src
->index
,
1203 INDEX_EDGE (edge_list
, x
)->dest
->index
);
1207 /* Now we want to insert the new stores which are going to be needed. */
1209 FOR_EACH_BB_FN (bb
, cfun
)
1210 if (bitmap_bit_p (st_delete_map
[bb
->index
], ptr
->index
))
1212 delete_store (ptr
, bb
);
1216 for (x
= 0; x
< NUM_EDGES (edge_list
); x
++)
1217 if (bitmap_bit_p (st_insert_map
[x
], ptr
->index
))
1219 did_edge_inserts
|= insert_store (ptr
, INDEX_EDGE (edge_list
, x
));
1224 if (did_edge_inserts
)
1225 commit_edge_insertions ();
1227 free_store_memory ();
1228 free_edge_list (edge_list
);
1229 remove_fake_exit_edges ();
1230 end_alias_analysis ();
1234 fprintf (dump_file
, "STORE_MOTION of %s, %d basic blocks, ",
1235 current_function_name (), n_basic_blocks_for_fn (cfun
));
1236 fprintf (dump_file
, "%d insns deleted, %d insns created\n",
1237 n_stores_deleted
, n_stores_created
);
1240 return (n_stores_deleted
> 0 || n_stores_created
> 0);
1245 execute_rtl_store_motion (void)
1247 delete_unreachable_blocks ();
1249 flag_rerun_cse_after_global_opts
|= one_store_motion_pass ();
1255 const pass_data pass_data_rtl_store_motion
=
1257 RTL_PASS
, /* type */
1258 "store_motion", /* name */
1259 OPTGROUP_NONE
, /* optinfo_flags */
1261 PROP_cfglayout
, /* properties_required */
1262 0, /* properties_provided */
1263 0, /* properties_destroyed */
1264 0, /* todo_flags_start */
1265 TODO_df_finish
, /* todo_flags_finish */
1268 class pass_rtl_store_motion
: public rtl_opt_pass
1271 pass_rtl_store_motion (gcc::context
*ctxt
)
1272 : rtl_opt_pass (pass_data_rtl_store_motion
, ctxt
)
1275 /* opt_pass methods: */
1276 virtual bool gate (function
*);
1277 virtual unsigned int execute (function
*)
1279 return execute_rtl_store_motion ();
1282 }; // class pass_rtl_store_motion
1285 pass_rtl_store_motion::gate (function
*fun
)
1287 return optimize
> 0 && flag_gcse_sm
1288 && !fun
->calls_setjmp
1289 && optimize_function_for_speed_p (fun
)
1290 && dbg_cnt (store_motion
);
1296 make_pass_rtl_store_motion (gcc::context
*ctxt
)
1298 return new pass_rtl_store_motion (ctxt
);