Revert the attempted fix for c++/69855, it breaks bootstrap.
[official-gcc.git] / gcc / store-motion.c
blob301b69be86d91ee3d85f64fa4d14af8d227d5f30
1 /* Store motion via Lazy Code Motion on the reverse CFG.
2 Copyright (C) 1997-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it 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
9 version.
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
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "backend.h"
24 #include "rtl.h"
25 #include "tree.h"
26 #include "predict.h"
27 #include "df.h"
28 #include "toplev.h"
30 #include "cfgrtl.h"
31 #include "cfganal.h"
32 #include "lcm.h"
33 #include "cfgcleanup.h"
34 #include "expr.h"
35 #include "tree-pass.h"
36 #include "dbgcnt.h"
37 #include "rtl-iter.h"
38 #include "print-rtl.h"
40 /* This pass implements downward store motion.
41 As of May 1, 2009, the pass is not enabled by default on any target,
42 but bootstrap completes on ia64 and x86_64 with the pass enabled. */
44 /* TODO:
45 - remove_reachable_equiv_notes is an incomprehensible pile of goo and
46 a compile time hog that needs a rewrite (maybe cache st_exprs to
47 invalidate REG_EQUAL/REG_EQUIV notes for?).
48 - pattern_regs in st_expr should be a regset (on its own obstack).
49 - antic_stores and avail_stores should be VECs instead of lists.
50 - store_motion_mems should be a vec instead of a list.
51 - there should be an alloc pool for struct st_expr objects.
52 - investigate whether it is helpful to make the address of an st_expr
53 a cselib VALUE.
54 - when GIMPLE alias information is exported, the effectiveness of this
55 pass should be re-evaluated.
58 /* This is a list of store expressions (MEMs). The structure is used
59 as an expression table to track stores which look interesting, and
60 might be moveable towards the exit block. */
62 struct st_expr
64 /* Pattern of this mem. */
65 rtx pattern;
66 /* List of registers mentioned by the mem. */
67 rtx pattern_regs;
68 /* INSN list of stores that are locally anticipatable. */
69 rtx_insn_list *antic_stores;
70 /* INSN list of stores that are locally available. */
71 vec<rtx_insn *> avail_stores;
72 /* Next in the list. */
73 struct st_expr * next;
74 /* Store ID in the dataflow bitmaps. */
75 int index;
76 /* Hash value for the hash table. */
77 unsigned int hash_index;
78 /* Register holding the stored expression when a store is moved.
79 This field is also used as a cache in find_moveable_store, see
80 LAST_AVAIL_CHECK_FAILURE below. */
81 rtx reaching_reg;
84 /* Head of the list of load/store memory refs. */
85 static struct st_expr * store_motion_mems = NULL;
87 /* These bitmaps will hold the local dataflow properties per basic block. */
88 static sbitmap *st_kill, *st_avloc, *st_antloc, *st_transp;
90 /* Nonzero for expressions which should be inserted on a specific edge. */
91 static sbitmap *st_insert_map;
93 /* Nonzero for expressions which should be deleted in a specific block. */
94 static sbitmap *st_delete_map;
96 /* Global holding the number of store expressions we are dealing with. */
97 static int num_stores;
99 /* Contains the edge_list returned by pre_edge_lcm. */
100 static struct edge_list *edge_list;
102 /* Hashtable helpers. */
104 struct st_expr_hasher : nofree_ptr_hash <st_expr>
106 static inline hashval_t hash (const st_expr *);
107 static inline bool equal (const st_expr *, const st_expr *);
110 inline hashval_t
111 st_expr_hasher::hash (const st_expr *x)
113 int do_not_record_p = 0;
114 return hash_rtx (x->pattern, GET_MODE (x->pattern), &do_not_record_p, NULL, false);
117 inline bool
118 st_expr_hasher::equal (const st_expr *ptr1, const st_expr *ptr2)
120 return exp_equiv_p (ptr1->pattern, ptr2->pattern, 0, true);
123 /* Hashtable for the load/store memory refs. */
124 static hash_table<st_expr_hasher> *store_motion_mems_table;
126 /* This will search the st_expr list for a matching expression. If it
127 doesn't find one, we create one and initialize it. */
129 static struct st_expr *
130 st_expr_entry (rtx x)
132 int do_not_record_p = 0;
133 struct st_expr * ptr;
134 unsigned int hash;
135 st_expr **slot;
136 struct st_expr e;
138 hash = hash_rtx (x, GET_MODE (x), &do_not_record_p,
139 NULL, /*have_reg_qty=*/false);
141 e.pattern = x;
142 slot = store_motion_mems_table->find_slot_with_hash (&e, hash, INSERT);
143 if (*slot)
144 return *slot;
146 ptr = XNEW (struct st_expr);
148 ptr->next = store_motion_mems;
149 ptr->pattern = x;
150 ptr->pattern_regs = NULL_RTX;
151 ptr->antic_stores = NULL;
152 ptr->avail_stores.create (0);
153 ptr->reaching_reg = NULL_RTX;
154 ptr->index = 0;
155 ptr->hash_index = hash;
156 store_motion_mems = ptr;
157 *slot = ptr;
159 return ptr;
162 /* Free up an individual st_expr entry. */
164 static void
165 free_st_expr_entry (struct st_expr * ptr)
167 free_INSN_LIST_list (& ptr->antic_stores);
168 ptr->avail_stores.release ();
170 free (ptr);
173 /* Free up all memory associated with the st_expr list. */
175 static void
176 free_store_motion_mems (void)
178 delete store_motion_mems_table;
179 store_motion_mems_table = NULL;
181 while (store_motion_mems)
183 struct st_expr * tmp = store_motion_mems;
184 store_motion_mems = store_motion_mems->next;
185 free_st_expr_entry (tmp);
187 store_motion_mems = NULL;
190 /* Assign each element of the list of mems a monotonically increasing value. */
192 static int
193 enumerate_store_motion_mems (void)
195 struct st_expr * ptr;
196 int n = 0;
198 for (ptr = store_motion_mems; ptr != NULL; ptr = ptr->next)
199 ptr->index = n++;
201 return n;
204 /* Return first item in the list. */
206 static inline struct st_expr *
207 first_st_expr (void)
209 return store_motion_mems;
212 /* Return the next item in the list after the specified one. */
214 static inline struct st_expr *
215 next_st_expr (struct st_expr * ptr)
217 return ptr->next;
220 /* Dump debugging info about the store_motion_mems list. */
222 static void
223 print_store_motion_mems (FILE * file)
225 struct st_expr * ptr;
227 fprintf (dump_file, "STORE_MOTION list of MEM exprs considered:\n");
229 for (ptr = first_st_expr (); ptr != NULL; ptr = next_st_expr (ptr))
231 fprintf (file, " Pattern (%3d): ", ptr->index);
233 print_rtl (file, ptr->pattern);
235 fprintf (file, "\n ANTIC stores : ");
237 if (ptr->antic_stores)
238 print_rtl (file, ptr->antic_stores);
239 else
240 fprintf (file, "(nil)");
242 fprintf (file, "\n AVAIL stores : ");
244 print_rtx_insn_vec (file, ptr->avail_stores);
246 fprintf (file, "\n\n");
249 fprintf (file, "\n");
252 /* Return zero if some of the registers in list X are killed
253 due to set of registers in bitmap REGS_SET. */
255 static bool
256 store_ops_ok (const_rtx x, int *regs_set)
258 const_rtx reg;
260 for (; x; x = XEXP (x, 1))
262 reg = XEXP (x, 0);
263 if (regs_set[REGNO (reg)])
264 return false;
267 return true;
270 /* Returns a list of registers mentioned in X.
271 FIXME: A regset would be prettier and less expensive. */
273 static rtx_expr_list *
274 extract_mentioned_regs (rtx x)
276 rtx_expr_list *mentioned_regs = NULL;
277 subrtx_var_iterator::array_type array;
278 FOR_EACH_SUBRTX_VAR (iter, array, x, NONCONST)
280 rtx x = *iter;
281 if (REG_P (x))
282 mentioned_regs = alloc_EXPR_LIST (0, x, mentioned_regs);
284 return mentioned_regs;
287 /* Check to see if the load X is aliased with STORE_PATTERN.
288 AFTER is true if we are checking the case when STORE_PATTERN occurs
289 after the X. */
291 static bool
292 load_kills_store (const_rtx x, const_rtx store_pattern, int after)
294 if (after)
295 return anti_dependence (x, store_pattern);
296 else
297 return true_dependence (store_pattern, GET_MODE (store_pattern), x);
300 /* Go through the entire rtx X, looking for any loads which might alias
301 STORE_PATTERN. Return true if found.
302 AFTER is true if we are checking the case when STORE_PATTERN occurs
303 after the insn X. */
305 static bool
306 find_loads (const_rtx x, const_rtx store_pattern, int after)
308 const char * fmt;
309 int i, j;
310 int ret = false;
312 if (!x)
313 return false;
315 if (GET_CODE (x) == SET)
316 x = SET_SRC (x);
318 if (MEM_P (x))
320 if (load_kills_store (x, store_pattern, after))
321 return true;
324 /* Recursively process the insn. */
325 fmt = GET_RTX_FORMAT (GET_CODE (x));
327 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0 && !ret; i--)
329 if (fmt[i] == 'e')
330 ret |= find_loads (XEXP (x, i), store_pattern, after);
331 else if (fmt[i] == 'E')
332 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
333 ret |= find_loads (XVECEXP (x, i, j), store_pattern, after);
335 return ret;
338 /* Go through pattern PAT looking for any loads which might kill the
339 store in X. Return true if found.
340 AFTER is true if we are checking the case when loads kill X occurs
341 after the insn for PAT. */
343 static inline bool
344 store_killed_in_pat (const_rtx x, const_rtx pat, int after)
346 if (GET_CODE (pat) == SET)
348 rtx dest = SET_DEST (pat);
350 if (GET_CODE (dest) == ZERO_EXTRACT)
351 dest = XEXP (dest, 0);
353 /* Check for memory stores to aliased objects. */
354 if (MEM_P (dest)
355 && !exp_equiv_p (dest, x, 0, true))
357 if (after)
359 if (output_dependence (dest, x))
360 return true;
362 else
364 if (output_dependence (x, dest))
365 return true;
370 if (find_loads (pat, x, after))
371 return true;
373 return false;
376 /* Check if INSN kills the store pattern X (is aliased with it).
377 AFTER is true if we are checking the case when store X occurs
378 after the insn. Return true if it does. */
380 static bool
381 store_killed_in_insn (const_rtx x, const_rtx x_regs, const rtx_insn *insn, int after)
383 const_rtx reg, note, pat;
385 if (! NONDEBUG_INSN_P (insn))
386 return false;
388 if (CALL_P (insn))
390 /* A normal or pure call might read from pattern,
391 but a const call will not. */
392 if (!RTL_CONST_CALL_P (insn))
393 return true;
395 /* But even a const call reads its parameters. Check whether the
396 base of some of registers used in mem is stack pointer. */
397 for (reg = x_regs; reg; reg = XEXP (reg, 1))
398 if (may_be_sp_based_p (XEXP (reg, 0)))
399 return true;
401 return false;
404 pat = PATTERN (insn);
405 if (GET_CODE (pat) == SET)
407 if (store_killed_in_pat (x, pat, after))
408 return true;
410 else if (GET_CODE (pat) == PARALLEL)
412 int i;
414 for (i = 0; i < XVECLEN (pat, 0); i++)
415 if (store_killed_in_pat (x, XVECEXP (pat, 0, i), after))
416 return true;
418 else if (find_loads (PATTERN (insn), x, after))
419 return true;
421 /* If this insn has a REG_EQUAL or REG_EQUIV note referencing a memory
422 location aliased with X, then this insn kills X. */
423 note = find_reg_equal_equiv_note (insn);
424 if (! note)
425 return false;
426 note = XEXP (note, 0);
428 /* However, if the note represents a must alias rather than a may
429 alias relationship, then it does not kill X. */
430 if (exp_equiv_p (note, x, 0, true))
431 return false;
433 /* See if there are any aliased loads in the note. */
434 return find_loads (note, x, after);
437 /* Returns true if the expression X is loaded or clobbered on or after INSN
438 within basic block BB. REGS_SET_AFTER is bitmap of registers set in
439 or after the insn. X_REGS is list of registers mentioned in X. If the store
440 is killed, return the last insn in that it occurs in FAIL_INSN. */
442 static bool
443 store_killed_after (const_rtx x, const_rtx x_regs, const rtx_insn *insn,
444 const_basic_block bb,
445 int *regs_set_after, rtx *fail_insn)
447 rtx_insn *last = BB_END (bb), *act;
449 if (!store_ops_ok (x_regs, regs_set_after))
451 /* We do not know where it will happen. */
452 if (fail_insn)
453 *fail_insn = NULL_RTX;
454 return true;
457 /* Scan from the end, so that fail_insn is determined correctly. */
458 for (act = last; act != PREV_INSN (insn); act = PREV_INSN (act))
459 if (store_killed_in_insn (x, x_regs, act, false))
461 if (fail_insn)
462 *fail_insn = act;
463 return true;
466 return false;
469 /* Returns true if the expression X is loaded or clobbered on or before INSN
470 within basic block BB. X_REGS is list of registers mentioned in X.
471 REGS_SET_BEFORE is bitmap of registers set before or in this insn. */
472 static bool
473 store_killed_before (const_rtx x, const_rtx x_regs, const rtx_insn *insn,
474 const_basic_block bb, int *regs_set_before)
476 rtx_insn *first = BB_HEAD (bb);
478 if (!store_ops_ok (x_regs, regs_set_before))
479 return true;
481 for ( ; insn != PREV_INSN (first); insn = PREV_INSN (insn))
482 if (store_killed_in_insn (x, x_regs, insn, true))
483 return true;
485 return false;
488 /* The last insn in the basic block that compute_store_table is processing,
489 where store_killed_after is true for X.
490 Since we go through the basic block from BB_END to BB_HEAD, this is
491 also the available store at the end of the basic block. Therefore
492 this is in effect a cache, to avoid calling store_killed_after for
493 equivalent aliasing store expressions.
494 This value is only meaningful during the computation of the store
495 table. We hi-jack the REACHING_REG field of struct st_expr to save
496 a bit of memory. */
497 #define LAST_AVAIL_CHECK_FAILURE(x) ((x)->reaching_reg)
499 /* Determine whether INSN is MEM store pattern that we will consider moving.
500 REGS_SET_BEFORE is bitmap of registers set before (and including) the
501 current insn, REGS_SET_AFTER is bitmap of registers set after (and
502 including) the insn in this basic block. We must be passing through BB from
503 head to end, as we are using this fact to speed things up.
505 The results are stored this way:
507 -- the first anticipatable expression is added into ANTIC_STORES
508 -- if the processed expression is not anticipatable, NULL_RTX is added
509 there instead, so that we can use it as indicator that no further
510 expression of this type may be anticipatable
511 -- if the expression is available, it is added as head of AVAIL_STORES;
512 consequently, all of them but this head are dead and may be deleted.
513 -- if the expression is not available, the insn due to that it fails to be
514 available is stored in REACHING_REG (via LAST_AVAIL_CHECK_FAILURE).
516 The things are complicated a bit by fact that there already may be stores
517 to the same MEM from other blocks; also caller must take care of the
518 necessary cleanup of the temporary markers after end of the basic block.
521 static void
522 find_moveable_store (rtx_insn *insn, int *regs_set_before, int *regs_set_after)
524 struct st_expr * ptr;
525 rtx dest, set;
526 int check_anticipatable, check_available;
527 basic_block bb = BLOCK_FOR_INSN (insn);
529 set = single_set (insn);
530 if (!set)
531 return;
533 dest = SET_DEST (set);
535 if (! MEM_P (dest) || MEM_VOLATILE_P (dest)
536 || GET_MODE (dest) == BLKmode)
537 return;
539 if (side_effects_p (dest))
540 return;
542 /* If we are handling exceptions, we must be careful with memory references
543 that may trap. If we are not, the behavior is undefined, so we may just
544 continue. */
545 if (cfun->can_throw_non_call_exceptions && may_trap_p (dest))
546 return;
548 /* Even if the destination cannot trap, the source may. In this case we'd
549 need to handle updating the REG_EH_REGION note. */
550 if (find_reg_note (insn, REG_EH_REGION, NULL_RTX))
551 return;
553 /* Make sure that the SET_SRC of this store insns can be assigned to
554 a register, or we will fail later on in replace_store_insn, which
555 assumes that we can do this. But sometimes the target machine has
556 oddities like MEM read-modify-write instruction. See for example
557 PR24257. */
558 if (!can_assign_to_reg_without_clobbers_p (SET_SRC (set),
559 GET_MODE (SET_SRC (set))))
560 return;
562 ptr = st_expr_entry (dest);
563 if (!ptr->pattern_regs)
564 ptr->pattern_regs = extract_mentioned_regs (dest);
566 /* Do not check for anticipatability if we either found one anticipatable
567 store already, or tested for one and found out that it was killed. */
568 check_anticipatable = 0;
569 if (!ptr->antic_stores)
570 check_anticipatable = 1;
571 else
573 rtx_insn *tmp = ptr->antic_stores->insn ();
574 if (tmp != NULL_RTX
575 && BLOCK_FOR_INSN (tmp) != bb)
576 check_anticipatable = 1;
578 if (check_anticipatable)
580 rtx_insn *tmp;
581 if (store_killed_before (dest, ptr->pattern_regs, insn, bb, regs_set_before))
582 tmp = NULL;
583 else
584 tmp = insn;
585 ptr->antic_stores = alloc_INSN_LIST (tmp, ptr->antic_stores);
588 /* It is not necessary to check whether store is available if we did
589 it successfully before; if we failed before, do not bother to check
590 until we reach the insn that caused us to fail. */
591 check_available = 0;
592 if (ptr->avail_stores.is_empty ())
593 check_available = 1;
594 else
596 rtx_insn *tmp = ptr->avail_stores.last ();
597 if (BLOCK_FOR_INSN (tmp) != bb)
598 check_available = 1;
600 if (check_available)
602 /* Check that we have already reached the insn at that the check
603 failed last time. */
604 if (LAST_AVAIL_CHECK_FAILURE (ptr))
606 rtx_insn *tmp;
607 for (tmp = BB_END (bb);
608 tmp != insn && tmp != LAST_AVAIL_CHECK_FAILURE (ptr);
609 tmp = PREV_INSN (tmp))
610 continue;
611 if (tmp == insn)
612 check_available = 0;
614 else
615 check_available = store_killed_after (dest, ptr->pattern_regs, insn,
616 bb, regs_set_after,
617 &LAST_AVAIL_CHECK_FAILURE (ptr));
619 if (!check_available)
620 ptr->avail_stores.safe_push (insn);
623 /* Find available and anticipatable stores. */
625 static int
626 compute_store_table (void)
628 int ret;
629 basic_block bb;
630 rtx_insn *insn;
631 rtx_insn *tmp;
632 df_ref def;
633 int *last_set_in, *already_set;
634 struct st_expr * ptr, **prev_next_ptr_ptr;
635 unsigned int max_gcse_regno = max_reg_num ();
637 store_motion_mems = NULL;
638 store_motion_mems_table = new hash_table<st_expr_hasher> (13);
639 last_set_in = XCNEWVEC (int, max_gcse_regno);
640 already_set = XNEWVEC (int, max_gcse_regno);
642 /* Find all the stores we care about. */
643 FOR_EACH_BB_FN (bb, cfun)
645 /* First compute the registers set in this block. */
646 FOR_BB_INSNS (bb, insn)
649 if (! NONDEBUG_INSN_P (insn))
650 continue;
652 FOR_EACH_INSN_DEF (def, insn)
653 last_set_in[DF_REF_REGNO (def)] = INSN_UID (insn);
656 /* Now find the stores. */
657 memset (already_set, 0, sizeof (int) * max_gcse_regno);
658 FOR_BB_INSNS (bb, insn)
660 if (! NONDEBUG_INSN_P (insn))
661 continue;
663 FOR_EACH_INSN_DEF (def, insn)
664 already_set[DF_REF_REGNO (def)] = INSN_UID (insn);
666 /* Now that we've marked regs, look for stores. */
667 find_moveable_store (insn, already_set, last_set_in);
669 /* Unmark regs that are no longer set. */
670 FOR_EACH_INSN_DEF (def, insn)
671 if (last_set_in[DF_REF_REGNO (def)] == INSN_UID (insn))
672 last_set_in[DF_REF_REGNO (def)] = 0;
675 if (flag_checking)
677 /* last_set_in should now be all-zero. */
678 for (unsigned regno = 0; regno < max_gcse_regno; regno++)
679 gcc_assert (!last_set_in[regno]);
682 /* Clear temporary marks. */
683 for (ptr = first_st_expr (); ptr != NULL; ptr = next_st_expr (ptr))
685 LAST_AVAIL_CHECK_FAILURE (ptr) = NULL_RTX;
686 if (ptr->antic_stores
687 && (tmp = ptr->antic_stores->insn ()) == NULL_RTX)
688 ptr->antic_stores = ptr->antic_stores->next ();
692 /* Remove the stores that are not available anywhere, as there will
693 be no opportunity to optimize them. */
694 for (ptr = store_motion_mems, prev_next_ptr_ptr = &store_motion_mems;
695 ptr != NULL;
696 ptr = *prev_next_ptr_ptr)
698 if (ptr->avail_stores.is_empty ())
700 *prev_next_ptr_ptr = ptr->next;
701 store_motion_mems_table->remove_elt_with_hash (ptr, ptr->hash_index);
702 free_st_expr_entry (ptr);
704 else
705 prev_next_ptr_ptr = &ptr->next;
708 ret = enumerate_store_motion_mems ();
710 if (dump_file)
711 print_store_motion_mems (dump_file);
713 free (last_set_in);
714 free (already_set);
715 return ret;
718 /* In all code following after this, REACHING_REG has its original
719 meaning again. Avoid confusion, and undef the accessor macro for
720 the temporary marks usage in compute_store_table. */
721 #undef LAST_AVAIL_CHECK_FAILURE
723 /* Insert an instruction at the beginning of a basic block, and update
724 the BB_HEAD if needed. */
726 static void
727 insert_insn_start_basic_block (rtx_insn *insn, basic_block bb)
729 /* Insert at start of successor block. */
730 rtx_insn *prev = PREV_INSN (BB_HEAD (bb));
731 rtx_insn *before = BB_HEAD (bb);
732 while (before != 0)
734 if (! LABEL_P (before)
735 && !NOTE_INSN_BASIC_BLOCK_P (before))
736 break;
737 prev = before;
738 if (prev == BB_END (bb))
739 break;
740 before = NEXT_INSN (before);
743 insn = emit_insn_after_noloc (insn, prev, bb);
745 if (dump_file)
747 fprintf (dump_file, "STORE_MOTION insert store at start of BB %d:\n",
748 bb->index);
749 print_inline_rtx (dump_file, insn, 6);
750 fprintf (dump_file, "\n");
754 /* This routine will insert a store on an edge. EXPR is the st_expr entry for
755 the memory reference, and E is the edge to insert it on. Returns nonzero
756 if an edge insertion was performed. */
758 static int
759 insert_store (struct st_expr * expr, edge e)
761 rtx reg;
762 rtx_insn *insn;
763 basic_block bb;
764 edge tmp;
765 edge_iterator ei;
767 /* We did all the deleted before this insert, so if we didn't delete a
768 store, then we haven't set the reaching reg yet either. */
769 if (expr->reaching_reg == NULL_RTX)
770 return 0;
772 if (e->flags & EDGE_FAKE)
773 return 0;
775 reg = expr->reaching_reg;
776 insn = gen_move_insn (copy_rtx (expr->pattern), reg);
778 /* If we are inserting this expression on ALL predecessor edges of a BB,
779 insert it at the start of the BB, and reset the insert bits on the other
780 edges so we don't try to insert it on the other edges. */
781 bb = e->dest;
782 FOR_EACH_EDGE (tmp, ei, e->dest->preds)
783 if (!(tmp->flags & EDGE_FAKE))
785 int index = EDGE_INDEX (edge_list, tmp->src, tmp->dest);
787 gcc_assert (index != EDGE_INDEX_NO_EDGE);
788 if (! bitmap_bit_p (st_insert_map[index], expr->index))
789 break;
792 /* If tmp is NULL, we found an insertion on every edge, blank the
793 insertion vector for these edges, and insert at the start of the BB. */
794 if (!tmp && bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
796 FOR_EACH_EDGE (tmp, ei, e->dest->preds)
798 int index = EDGE_INDEX (edge_list, tmp->src, tmp->dest);
799 bitmap_clear_bit (st_insert_map[index], expr->index);
801 insert_insn_start_basic_block (insn, bb);
802 return 0;
805 /* We can't put stores in the front of blocks pointed to by abnormal
806 edges since that may put a store where one didn't used to be. */
807 gcc_assert (!(e->flags & EDGE_ABNORMAL));
809 insert_insn_on_edge (insn, e);
811 if (dump_file)
813 fprintf (dump_file, "STORE_MOTION insert insn on edge (%d, %d):\n",
814 e->src->index, e->dest->index);
815 print_inline_rtx (dump_file, insn, 6);
816 fprintf (dump_file, "\n");
819 return 1;
822 /* Remove any REG_EQUAL or REG_EQUIV notes containing a reference to the
823 memory location in SMEXPR set in basic block BB.
825 This could be rather expensive. */
827 static void
828 remove_reachable_equiv_notes (basic_block bb, struct st_expr *smexpr)
830 edge_iterator *stack, ei;
831 int sp;
832 edge act;
833 sbitmap visited = sbitmap_alloc (last_basic_block_for_fn (cfun));
834 rtx last, note;
835 rtx_insn *insn;
836 rtx mem = smexpr->pattern;
838 stack = XNEWVEC (edge_iterator, n_basic_blocks_for_fn (cfun));
839 sp = 0;
840 ei = ei_start (bb->succs);
842 bitmap_clear (visited);
844 act = (EDGE_COUNT (ei_container (ei)) > 0 ? EDGE_I (ei_container (ei), 0) : NULL);
845 while (1)
847 if (!act)
849 if (!sp)
851 free (stack);
852 sbitmap_free (visited);
853 return;
855 act = ei_edge (stack[--sp]);
857 bb = act->dest;
859 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
860 || bitmap_bit_p (visited, bb->index))
862 if (!ei_end_p (ei))
863 ei_next (&ei);
864 act = (! ei_end_p (ei)) ? ei_edge (ei) : NULL;
865 continue;
867 bitmap_set_bit (visited, bb->index);
869 if (bitmap_bit_p (st_antloc[bb->index], smexpr->index))
871 for (last = smexpr->antic_stores;
872 BLOCK_FOR_INSN (XEXP (last, 0)) != bb;
873 last = XEXP (last, 1))
874 continue;
875 last = XEXP (last, 0);
877 else
878 last = NEXT_INSN (BB_END (bb));
880 for (insn = BB_HEAD (bb); insn != last; insn = NEXT_INSN (insn))
881 if (NONDEBUG_INSN_P (insn))
883 note = find_reg_equal_equiv_note (insn);
884 if (!note || !exp_equiv_p (XEXP (note, 0), mem, 0, true))
885 continue;
887 if (dump_file)
888 fprintf (dump_file, "STORE_MOTION drop REG_EQUAL note at insn %d:\n",
889 INSN_UID (insn));
890 remove_note (insn, note);
893 if (!ei_end_p (ei))
894 ei_next (&ei);
895 act = (! ei_end_p (ei)) ? ei_edge (ei) : NULL;
897 if (EDGE_COUNT (bb->succs) > 0)
899 if (act)
900 stack[sp++] = ei;
901 ei = ei_start (bb->succs);
902 act = (EDGE_COUNT (ei_container (ei)) > 0 ? EDGE_I (ei_container (ei), 0) : NULL);
907 /* This routine will replace a store with a SET to a specified register. */
909 static void
910 replace_store_insn (rtx reg, rtx_insn *del, basic_block bb,
911 struct st_expr *smexpr)
913 rtx_insn *insn;
914 rtx mem, note, set, ptr;
916 mem = smexpr->pattern;
917 insn = gen_move_insn (reg, SET_SRC (single_set (del)));
919 for (ptr = smexpr->antic_stores; ptr; ptr = XEXP (ptr, 1))
920 if (XEXP (ptr, 0) == del)
922 XEXP (ptr, 0) = insn;
923 break;
926 /* Move the notes from the deleted insn to its replacement. */
927 REG_NOTES (insn) = REG_NOTES (del);
929 /* Emit the insn AFTER all the notes are transferred.
930 This is cheaper since we avoid df rescanning for the note change. */
931 insn = emit_insn_after (insn, del);
933 if (dump_file)
935 fprintf (dump_file,
936 "STORE_MOTION delete insn in BB %d:\n ", bb->index);
937 print_inline_rtx (dump_file, del, 6);
938 fprintf (dump_file, "\nSTORE_MOTION replaced with insn:\n ");
939 print_inline_rtx (dump_file, insn, 6);
940 fprintf (dump_file, "\n");
943 delete_insn (del);
945 /* Now we must handle REG_EQUAL notes whose contents is equal to the mem;
946 they are no longer accurate provided that they are reached by this
947 definition, so drop them. */
948 for (; insn != NEXT_INSN (BB_END (bb)); insn = NEXT_INSN (insn))
949 if (NONDEBUG_INSN_P (insn))
951 set = single_set (insn);
952 if (!set)
953 continue;
954 if (exp_equiv_p (SET_DEST (set), mem, 0, true))
955 return;
956 note = find_reg_equal_equiv_note (insn);
957 if (!note || !exp_equiv_p (XEXP (note, 0), mem, 0, true))
958 continue;
960 if (dump_file)
961 fprintf (dump_file, "STORE_MOTION drop REG_EQUAL note at insn %d:\n",
962 INSN_UID (insn));
963 remove_note (insn, note);
965 remove_reachable_equiv_notes (bb, smexpr);
969 /* Delete a store, but copy the value that would have been stored into
970 the reaching_reg for later storing. */
972 static void
973 delete_store (struct st_expr * expr, basic_block bb)
975 rtx reg;
977 if (expr->reaching_reg == NULL_RTX)
978 expr->reaching_reg = gen_reg_rtx_and_attrs (expr->pattern);
980 reg = expr->reaching_reg;
982 unsigned int len = expr->avail_stores.length ();
983 for (unsigned int i = len - 1; i < len; i--)
985 rtx_insn *del = expr->avail_stores[i];
986 if (BLOCK_FOR_INSN (del) == bb)
988 /* We know there is only one since we deleted redundant
989 ones during the available computation. */
990 replace_store_insn (reg, del, bb, expr);
991 break;
996 /* Fill in available, anticipatable, transparent and kill vectors in
997 STORE_DATA, based on lists of available and anticipatable stores. */
998 static void
999 build_store_vectors (void)
1001 basic_block bb;
1002 int *regs_set_in_block;
1003 rtx_insn *insn;
1004 rtx_insn_list *st;
1005 struct st_expr * ptr;
1006 unsigned int max_gcse_regno = max_reg_num ();
1008 /* Build the gen_vector. This is any store in the table which is not killed
1009 by aliasing later in its block. */
1010 st_avloc = sbitmap_vector_alloc (last_basic_block_for_fn (cfun),
1011 num_stores);
1012 bitmap_vector_clear (st_avloc, last_basic_block_for_fn (cfun));
1014 st_antloc = sbitmap_vector_alloc (last_basic_block_for_fn (cfun),
1015 num_stores);
1016 bitmap_vector_clear (st_antloc, last_basic_block_for_fn (cfun));
1018 for (ptr = first_st_expr (); ptr != NULL; ptr = next_st_expr (ptr))
1020 unsigned int len = ptr->avail_stores.length ();
1021 for (unsigned int i = len - 1; i < len; i--)
1023 insn = ptr->avail_stores[i];
1024 bb = BLOCK_FOR_INSN (insn);
1026 /* If we've already seen an available expression in this block,
1027 we can delete this one (It occurs earlier in the block). We'll
1028 copy the SRC expression to an unused register in case there
1029 are any side effects. */
1030 if (bitmap_bit_p (st_avloc[bb->index], ptr->index))
1032 rtx r = gen_reg_rtx_and_attrs (ptr->pattern);
1033 if (dump_file)
1034 fprintf (dump_file, "Removing redundant store:\n");
1035 replace_store_insn (r, insn, bb, ptr);
1036 continue;
1038 bitmap_set_bit (st_avloc[bb->index], ptr->index);
1041 for (st = ptr->antic_stores; st != NULL; st = st->next ())
1043 insn = st->insn ();
1044 bb = BLOCK_FOR_INSN (insn);
1045 bitmap_set_bit (st_antloc[bb->index], ptr->index);
1049 st_kill = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), num_stores);
1050 bitmap_vector_clear (st_kill, last_basic_block_for_fn (cfun));
1052 st_transp = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), num_stores);
1053 bitmap_vector_clear (st_transp, last_basic_block_for_fn (cfun));
1054 regs_set_in_block = XNEWVEC (int, max_gcse_regno);
1056 FOR_EACH_BB_FN (bb, cfun)
1058 memset (regs_set_in_block, 0, sizeof (int) * max_gcse_regno);
1060 FOR_BB_INSNS (bb, insn)
1061 if (NONDEBUG_INSN_P (insn))
1063 df_ref def;
1064 FOR_EACH_INSN_DEF (def, insn)
1066 unsigned int ref_regno = DF_REF_REGNO (def);
1067 if (ref_regno < max_gcse_regno)
1068 regs_set_in_block[DF_REF_REGNO (def)] = 1;
1072 for (ptr = first_st_expr (); ptr != NULL; ptr = next_st_expr (ptr))
1074 if (store_killed_after (ptr->pattern, ptr->pattern_regs, BB_HEAD (bb),
1075 bb, regs_set_in_block, NULL))
1077 /* It should not be necessary to consider the expression
1078 killed if it is both anticipatable and available. */
1079 if (!bitmap_bit_p (st_antloc[bb->index], ptr->index)
1080 || !bitmap_bit_p (st_avloc[bb->index], ptr->index))
1081 bitmap_set_bit (st_kill[bb->index], ptr->index);
1083 else
1084 bitmap_set_bit (st_transp[bb->index], ptr->index);
1088 free (regs_set_in_block);
1090 if (dump_file)
1092 dump_bitmap_vector (dump_file, "st_antloc", "", st_antloc,
1093 last_basic_block_for_fn (cfun));
1094 dump_bitmap_vector (dump_file, "st_kill", "", st_kill,
1095 last_basic_block_for_fn (cfun));
1096 dump_bitmap_vector (dump_file, "st_transp", "", st_transp,
1097 last_basic_block_for_fn (cfun));
1098 dump_bitmap_vector (dump_file, "st_avloc", "", st_avloc,
1099 last_basic_block_for_fn (cfun));
1103 /* Free memory used by store motion. */
1105 static void
1106 free_store_memory (void)
1108 free_store_motion_mems ();
1110 if (st_avloc)
1111 sbitmap_vector_free (st_avloc);
1112 if (st_kill)
1113 sbitmap_vector_free (st_kill);
1114 if (st_transp)
1115 sbitmap_vector_free (st_transp);
1116 if (st_antloc)
1117 sbitmap_vector_free (st_antloc);
1118 if (st_insert_map)
1119 sbitmap_vector_free (st_insert_map);
1120 if (st_delete_map)
1121 sbitmap_vector_free (st_delete_map);
1123 st_avloc = st_kill = st_transp = st_antloc = NULL;
1124 st_insert_map = st_delete_map = NULL;
1127 /* Perform store motion. Much like gcse, except we move expressions the
1128 other way by looking at the flowgraph in reverse.
1129 Return non-zero if transformations are performed by the pass. */
1131 static int
1132 one_store_motion_pass (void)
1134 basic_block bb;
1135 int x;
1136 struct st_expr * ptr;
1137 int did_edge_inserts = 0;
1138 int n_stores_deleted = 0;
1139 int n_stores_created = 0;
1141 init_alias_analysis ();
1143 /* Find all the available and anticipatable stores. */
1144 num_stores = compute_store_table ();
1145 if (num_stores == 0)
1147 delete store_motion_mems_table;
1148 store_motion_mems_table = NULL;
1149 end_alias_analysis ();
1150 return 0;
1153 /* Now compute kill & transp vectors. */
1154 build_store_vectors ();
1155 add_noreturn_fake_exit_edges ();
1156 connect_infinite_loops_to_exit ();
1158 edge_list = pre_edge_rev_lcm (num_stores, st_transp, st_avloc,
1159 st_antloc, st_kill, &st_insert_map,
1160 &st_delete_map);
1162 /* Now we want to insert the new stores which are going to be needed. */
1163 for (ptr = first_st_expr (); ptr != NULL; ptr = next_st_expr (ptr))
1165 /* If any of the edges we have above are abnormal, we can't move this
1166 store. */
1167 for (x = NUM_EDGES (edge_list) - 1; x >= 0; x--)
1168 if (bitmap_bit_p (st_insert_map[x], ptr->index)
1169 && (INDEX_EDGE (edge_list, x)->flags & EDGE_ABNORMAL))
1170 break;
1172 if (x >= 0)
1174 if (dump_file != NULL)
1175 fprintf (dump_file,
1176 "Can't replace store %d: abnormal edge from %d to %d\n",
1177 ptr->index, INDEX_EDGE (edge_list, x)->src->index,
1178 INDEX_EDGE (edge_list, x)->dest->index);
1179 continue;
1182 /* Now we want to insert the new stores which are going to be needed. */
1184 FOR_EACH_BB_FN (bb, cfun)
1185 if (bitmap_bit_p (st_delete_map[bb->index], ptr->index))
1187 delete_store (ptr, bb);
1188 n_stores_deleted++;
1191 for (x = 0; x < NUM_EDGES (edge_list); x++)
1192 if (bitmap_bit_p (st_insert_map[x], ptr->index))
1194 did_edge_inserts |= insert_store (ptr, INDEX_EDGE (edge_list, x));
1195 n_stores_created++;
1199 if (did_edge_inserts)
1200 commit_edge_insertions ();
1202 free_store_memory ();
1203 free_edge_list (edge_list);
1204 remove_fake_exit_edges ();
1205 end_alias_analysis ();
1207 if (dump_file)
1209 fprintf (dump_file, "STORE_MOTION of %s, %d basic blocks, ",
1210 current_function_name (), n_basic_blocks_for_fn (cfun));
1211 fprintf (dump_file, "%d insns deleted, %d insns created\n",
1212 n_stores_deleted, n_stores_created);
1215 return (n_stores_deleted > 0 || n_stores_created > 0);
1219 static unsigned int
1220 execute_rtl_store_motion (void)
1222 delete_unreachable_blocks ();
1223 df_analyze ();
1224 flag_rerun_cse_after_global_opts |= one_store_motion_pass ();
1225 return 0;
1228 namespace {
1230 const pass_data pass_data_rtl_store_motion =
1232 RTL_PASS, /* type */
1233 "store_motion", /* name */
1234 OPTGROUP_NONE, /* optinfo_flags */
1235 TV_LSM, /* tv_id */
1236 PROP_cfglayout, /* properties_required */
1237 0, /* properties_provided */
1238 0, /* properties_destroyed */
1239 0, /* todo_flags_start */
1240 TODO_df_finish, /* todo_flags_finish */
1243 class pass_rtl_store_motion : public rtl_opt_pass
1245 public:
1246 pass_rtl_store_motion (gcc::context *ctxt)
1247 : rtl_opt_pass (pass_data_rtl_store_motion, ctxt)
1250 /* opt_pass methods: */
1251 virtual bool gate (function *);
1252 virtual unsigned int execute (function *)
1254 return execute_rtl_store_motion ();
1257 }; // class pass_rtl_store_motion
1259 bool
1260 pass_rtl_store_motion::gate (function *fun)
1262 return optimize > 0 && flag_gcse_sm
1263 && !fun->calls_setjmp
1264 && optimize_function_for_speed_p (fun)
1265 && dbg_cnt (store_motion);
1268 } // anon namespace
1270 rtl_opt_pass *
1271 make_pass_rtl_store_motion (gcc::context *ctxt)
1273 return new pass_rtl_store_motion (ctxt);