fold-const.c (fold_binary_loc): Fix copy-and-pasto.
[gcc.git] / gcc / postreload-gcse.c
1 /* Post reload partially redundant load elimination
2 Copyright (C) 2004-2014 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "diagnostic-core.h"
25
26 #include "hash-table.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "tm_p.h"
30 #include "regs.h"
31 #include "hard-reg-set.h"
32 #include "flags.h"
33 #include "insn-config.h"
34 #include "recog.h"
35 #include "basic-block.h"
36 #include "hashtab.h"
37 #include "hash-set.h"
38 #include "vec.h"
39 #include "machmode.h"
40 #include "input.h"
41 #include "function.h"
42 #include "profile.h"
43 #include "expr.h"
44 #include "except.h"
45 #include "intl.h"
46 #include "obstack.h"
47 #include "params.h"
48 #include "target.h"
49 #include "tree-pass.h"
50 #include "dbgcnt.h"
51
52 /* The following code implements gcse after reload, the purpose of this
53 pass is to cleanup redundant loads generated by reload and other
54 optimizations that come after gcse. It searches for simple inter-block
55 redundancies and tries to eliminate them by adding moves and loads
56 in cold places.
57
58 Perform partially redundant load elimination, try to eliminate redundant
59 loads created by the reload pass. We try to look for full or partial
60 redundant loads fed by one or more loads/stores in predecessor BBs,
61 and try adding loads to make them fully redundant. We also check if
62 it's worth adding loads to be able to delete the redundant load.
63
64 Algorithm:
65 1. Build available expressions hash table:
66 For each load/store instruction, if the loaded/stored memory didn't
67 change until the end of the basic block add this memory expression to
68 the hash table.
69 2. Perform Redundancy elimination:
70 For each load instruction do the following:
71 perform partial redundancy elimination, check if it's worth adding
72 loads to make the load fully redundant. If so add loads and
73 register copies and delete the load.
74 3. Delete instructions made redundant in step 2.
75
76 Future enhancement:
77 If the loaded register is used/defined between load and some store,
78 look for some other free register between load and all its stores,
79 and replace the load with a copy from this register to the loaded
80 register.
81 */
82 \f
83
84 /* Keep statistics of this pass. */
85 static struct
86 {
87 int moves_inserted;
88 int copies_inserted;
89 int insns_deleted;
90 } stats;
91
92 /* We need to keep a hash table of expressions. The table entries are of
93 type 'struct expr', and for each expression there is a single linked
94 list of occurrences. */
95
96 /* Expression elements in the hash table. */
97 struct expr
98 {
99 /* The expression (SET_SRC for expressions, PATTERN for assignments). */
100 rtx expr;
101
102 /* The same hash for this entry. */
103 hashval_t hash;
104
105 /* List of available occurrence in basic blocks in the function. */
106 struct occr *avail_occr;
107 };
108
109 /* Hashtable helpers. */
110
111 struct expr_hasher : typed_noop_remove <expr>
112 {
113 typedef expr value_type;
114 typedef expr compare_type;
115 static inline hashval_t hash (const value_type *);
116 static inline bool equal (const value_type *, const compare_type *);
117 };
118
119
120 /* Hash expression X.
121 DO_NOT_RECORD_P is a boolean indicating if a volatile operand is found
122 or if the expression contains something we don't want to insert in the
123 table. */
124
125 static hashval_t
126 hash_expr (rtx x, int *do_not_record_p)
127 {
128 *do_not_record_p = 0;
129 return hash_rtx (x, GET_MODE (x), do_not_record_p,
130 NULL, /*have_reg_qty=*/false);
131 }
132
133 /* Callback for hashtab.
134 Return the hash value for expression EXP. We don't actually hash
135 here, we just return the cached hash value. */
136
137 inline hashval_t
138 expr_hasher::hash (const value_type *exp)
139 {
140 return exp->hash;
141 }
142
143 /* Callback for hashtab.
144 Return nonzero if exp1 is equivalent to exp2. */
145
146 inline bool
147 expr_hasher::equal (const value_type *exp1, const compare_type *exp2)
148 {
149 int equiv_p = exp_equiv_p (exp1->expr, exp2->expr, 0, true);
150
151 gcc_assert (!equiv_p || exp1->hash == exp2->hash);
152 return equiv_p;
153 }
154
155 /* The table itself. */
156 static hash_table<expr_hasher> *expr_table;
157 \f
158
159 static struct obstack expr_obstack;
160
161 /* Occurrence of an expression.
162 There is at most one occurrence per basic block. If a pattern appears
163 more than once, the last appearance is used. */
164
165 struct occr
166 {
167 /* Next occurrence of this expression. */
168 struct occr *next;
169 /* The insn that computes the expression. */
170 rtx_insn *insn;
171 /* Nonzero if this [anticipatable] occurrence has been deleted. */
172 char deleted_p;
173 };
174
175 static struct obstack occr_obstack;
176
177 /* The following structure holds the information about the occurrences of
178 the redundant instructions. */
179 struct unoccr
180 {
181 struct unoccr *next;
182 edge pred;
183 rtx_insn *insn;
184 };
185
186 static struct obstack unoccr_obstack;
187
188 /* Array where each element is the CUID if the insn that last set the hard
189 register with the number of the element, since the start of the current
190 basic block.
191
192 This array is used during the building of the hash table (step 1) to
193 determine if a reg is killed before the end of a basic block.
194
195 It is also used when eliminating partial redundancies (step 2) to see
196 if a reg was modified since the start of a basic block. */
197 static int *reg_avail_info;
198
199 /* A list of insns that may modify memory within the current basic block. */
200 struct modifies_mem
201 {
202 rtx_insn *insn;
203 struct modifies_mem *next;
204 };
205 static struct modifies_mem *modifies_mem_list;
206
207 /* The modifies_mem structs also go on an obstack, only this obstack is
208 freed each time after completing the analysis or transformations on
209 a basic block. So we allocate a dummy modifies_mem_obstack_bottom
210 object on the obstack to keep track of the bottom of the obstack. */
211 static struct obstack modifies_mem_obstack;
212 static struct modifies_mem *modifies_mem_obstack_bottom;
213
214 /* Mapping of insn UIDs to CUIDs.
215 CUIDs are like UIDs except they increase monotonically in each basic
216 block, have no gaps, and only apply to real insns. */
217 static int *uid_cuid;
218 #define INSN_CUID(INSN) (uid_cuid[INSN_UID (INSN)])
219 \f
220
221 /* Helpers for memory allocation/freeing. */
222 static void alloc_mem (void);
223 static void free_mem (void);
224
225 /* Support for hash table construction and transformations. */
226 static bool oprs_unchanged_p (rtx, rtx_insn *, bool);
227 static void record_last_reg_set_info (rtx_insn *, rtx);
228 static void record_last_reg_set_info_regno (rtx_insn *, int);
229 static void record_last_mem_set_info (rtx_insn *);
230 static void record_last_set_info (rtx, const_rtx, void *);
231 static void record_opr_changes (rtx_insn *);
232
233 static void find_mem_conflicts (rtx, const_rtx, void *);
234 static int load_killed_in_block_p (int, rtx, bool);
235 static void reset_opr_set_tables (void);
236
237 /* Hash table support. */
238 static hashval_t hash_expr (rtx, int *);
239 static void insert_expr_in_table (rtx, rtx_insn *);
240 static struct expr *lookup_expr_in_table (rtx);
241 static void dump_hash_table (FILE *);
242
243 /* Helpers for eliminate_partially_redundant_load. */
244 static bool reg_killed_on_edge (rtx, edge);
245 static bool reg_used_on_edge (rtx, edge);
246
247 static rtx get_avail_load_store_reg (rtx_insn *);
248
249 static bool bb_has_well_behaved_predecessors (basic_block);
250 static struct occr* get_bb_avail_insn (basic_block, struct occr *);
251 static void hash_scan_set (rtx_insn *);
252 static void compute_hash_table (void);
253
254 /* The work horses of this pass. */
255 static void eliminate_partially_redundant_load (basic_block,
256 rtx_insn *,
257 struct expr *);
258 static void eliminate_partially_redundant_loads (void);
259 \f
260
261 /* Allocate memory for the CUID mapping array and register/memory
262 tracking tables. */
263
264 static void
265 alloc_mem (void)
266 {
267 int i;
268 basic_block bb;
269 rtx_insn *insn;
270
271 /* Find the largest UID and create a mapping from UIDs to CUIDs. */
272 uid_cuid = XCNEWVEC (int, get_max_uid () + 1);
273 i = 1;
274 FOR_EACH_BB_FN (bb, cfun)
275 FOR_BB_INSNS (bb, insn)
276 {
277 if (INSN_P (insn))
278 uid_cuid[INSN_UID (insn)] = i++;
279 else
280 uid_cuid[INSN_UID (insn)] = i;
281 }
282
283 /* Allocate the available expressions hash table. We don't want to
284 make the hash table too small, but unnecessarily making it too large
285 also doesn't help. The i/4 is a gcse.c relic, and seems like a
286 reasonable choice. */
287 expr_table = new hash_table<expr_hasher> (MAX (i / 4, 13));
288
289 /* We allocate everything on obstacks because we often can roll back
290 the whole obstack to some point. Freeing obstacks is very fast. */
291 gcc_obstack_init (&expr_obstack);
292 gcc_obstack_init (&occr_obstack);
293 gcc_obstack_init (&unoccr_obstack);
294 gcc_obstack_init (&modifies_mem_obstack);
295
296 /* Working array used to track the last set for each register
297 in the current block. */
298 reg_avail_info = (int *) xmalloc (FIRST_PSEUDO_REGISTER * sizeof (int));
299
300 /* Put a dummy modifies_mem object on the modifies_mem_obstack, so we
301 can roll it back in reset_opr_set_tables. */
302 modifies_mem_obstack_bottom =
303 (struct modifies_mem *) obstack_alloc (&modifies_mem_obstack,
304 sizeof (struct modifies_mem));
305 }
306
307 /* Free memory allocated by alloc_mem. */
308
309 static void
310 free_mem (void)
311 {
312 free (uid_cuid);
313
314 delete expr_table;
315 expr_table = NULL;
316
317 obstack_free (&expr_obstack, NULL);
318 obstack_free (&occr_obstack, NULL);
319 obstack_free (&unoccr_obstack, NULL);
320 obstack_free (&modifies_mem_obstack, NULL);
321
322 free (reg_avail_info);
323 }
324 \f
325
326 /* Insert expression X in INSN in the hash TABLE.
327 If it is already present, record it as the last occurrence in INSN's
328 basic block. */
329
330 static void
331 insert_expr_in_table (rtx x, rtx_insn *insn)
332 {
333 int do_not_record_p;
334 hashval_t hash;
335 struct expr *cur_expr, **slot;
336 struct occr *avail_occr, *last_occr = NULL;
337
338 hash = hash_expr (x, &do_not_record_p);
339
340 /* Do not insert expression in the table if it contains volatile operands,
341 or if hash_expr determines the expression is something we don't want
342 to or can't handle. */
343 if (do_not_record_p)
344 return;
345
346 /* We anticipate that redundant expressions are rare, so for convenience
347 allocate a new hash table element here already and set its fields.
348 If we don't do this, we need a hack with a static struct expr. Anyway,
349 obstack_free is really fast and one more obstack_alloc doesn't hurt if
350 we're going to see more expressions later on. */
351 cur_expr = (struct expr *) obstack_alloc (&expr_obstack,
352 sizeof (struct expr));
353 cur_expr->expr = x;
354 cur_expr->hash = hash;
355 cur_expr->avail_occr = NULL;
356
357 slot = expr_table->find_slot_with_hash (cur_expr, hash, INSERT);
358
359 if (! (*slot))
360 /* The expression isn't found, so insert it. */
361 *slot = cur_expr;
362 else
363 {
364 /* The expression is already in the table, so roll back the
365 obstack and use the existing table entry. */
366 obstack_free (&expr_obstack, cur_expr);
367 cur_expr = *slot;
368 }
369
370 /* Search for another occurrence in the same basic block. */
371 avail_occr = cur_expr->avail_occr;
372 while (avail_occr
373 && BLOCK_FOR_INSN (avail_occr->insn) != BLOCK_FOR_INSN (insn))
374 {
375 /* If an occurrence isn't found, save a pointer to the end of
376 the list. */
377 last_occr = avail_occr;
378 avail_occr = avail_occr->next;
379 }
380
381 if (avail_occr)
382 /* Found another instance of the expression in the same basic block.
383 Prefer this occurrence to the currently recorded one. We want
384 the last one in the block and the block is scanned from start
385 to end. */
386 avail_occr->insn = insn;
387 else
388 {
389 /* First occurrence of this expression in this basic block. */
390 avail_occr = (struct occr *) obstack_alloc (&occr_obstack,
391 sizeof (struct occr));
392
393 /* First occurrence of this expression in any block? */
394 if (cur_expr->avail_occr == NULL)
395 cur_expr->avail_occr = avail_occr;
396 else
397 last_occr->next = avail_occr;
398
399 avail_occr->insn = insn;
400 avail_occr->next = NULL;
401 avail_occr->deleted_p = 0;
402 }
403 }
404 \f
405
406 /* Lookup pattern PAT in the expression hash table.
407 The result is a pointer to the table entry, or NULL if not found. */
408
409 static struct expr *
410 lookup_expr_in_table (rtx pat)
411 {
412 int do_not_record_p;
413 struct expr **slot, *tmp_expr;
414 hashval_t hash = hash_expr (pat, &do_not_record_p);
415
416 if (do_not_record_p)
417 return NULL;
418
419 tmp_expr = (struct expr *) obstack_alloc (&expr_obstack,
420 sizeof (struct expr));
421 tmp_expr->expr = pat;
422 tmp_expr->hash = hash;
423 tmp_expr->avail_occr = NULL;
424
425 slot = expr_table->find_slot_with_hash (tmp_expr, hash, INSERT);
426 obstack_free (&expr_obstack, tmp_expr);
427
428 if (!slot)
429 return NULL;
430 else
431 return (*slot);
432 }
433 \f
434
435 /* Dump all expressions and occurrences that are currently in the
436 expression hash table to FILE. */
437
438 /* This helper is called via htab_traverse. */
439 int
440 dump_expr_hash_table_entry (expr **slot, FILE *file)
441 {
442 struct expr *exprs = *slot;
443 struct occr *occr;
444
445 fprintf (file, "expr: ");
446 print_rtl (file, exprs->expr);
447 fprintf (file,"\nhashcode: %u\n", exprs->hash);
448 fprintf (file,"list of occurrences:\n");
449 occr = exprs->avail_occr;
450 while (occr)
451 {
452 rtx_insn *insn = occr->insn;
453 print_rtl_single (file, insn);
454 fprintf (file, "\n");
455 occr = occr->next;
456 }
457 fprintf (file, "\n");
458 return 1;
459 }
460
461 static void
462 dump_hash_table (FILE *file)
463 {
464 fprintf (file, "\n\nexpression hash table\n");
465 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
466 (long) expr_table->size (),
467 (long) expr_table->elements (),
468 expr_table->collisions ());
469 if (expr_table->elements () > 0)
470 {
471 fprintf (file, "\n\ntable entries:\n");
472 expr_table->traverse <FILE *, dump_expr_hash_table_entry> (file);
473 }
474 fprintf (file, "\n");
475 }
476 \f
477 /* Return true if register X is recorded as being set by an instruction
478 whose CUID is greater than the one given. */
479
480 static bool
481 reg_changed_after_insn_p (rtx x, int cuid)
482 {
483 unsigned int regno, end_regno;
484
485 regno = REGNO (x);
486 end_regno = END_HARD_REGNO (x);
487 do
488 if (reg_avail_info[regno] > cuid)
489 return true;
490 while (++regno < end_regno);
491 return false;
492 }
493
494 /* Return nonzero if the operands of expression X are unchanged
495 1) from the start of INSN's basic block up to but not including INSN
496 if AFTER_INSN is false, or
497 2) from INSN to the end of INSN's basic block if AFTER_INSN is true. */
498
499 static bool
500 oprs_unchanged_p (rtx x, rtx_insn *insn, bool after_insn)
501 {
502 int i, j;
503 enum rtx_code code;
504 const char *fmt;
505
506 if (x == 0)
507 return 1;
508
509 code = GET_CODE (x);
510 switch (code)
511 {
512 case REG:
513 /* We are called after register allocation. */
514 gcc_assert (REGNO (x) < FIRST_PSEUDO_REGISTER);
515 if (after_insn)
516 return !reg_changed_after_insn_p (x, INSN_CUID (insn) - 1);
517 else
518 return !reg_changed_after_insn_p (x, 0);
519
520 case MEM:
521 if (load_killed_in_block_p (INSN_CUID (insn), x, after_insn))
522 return 0;
523 else
524 return oprs_unchanged_p (XEXP (x, 0), insn, after_insn);
525
526 case PC:
527 case CC0: /*FIXME*/
528 case CONST:
529 CASE_CONST_ANY:
530 case SYMBOL_REF:
531 case LABEL_REF:
532 case ADDR_VEC:
533 case ADDR_DIFF_VEC:
534 return 1;
535
536 case PRE_DEC:
537 case PRE_INC:
538 case POST_DEC:
539 case POST_INC:
540 case PRE_MODIFY:
541 case POST_MODIFY:
542 if (after_insn)
543 return 0;
544 break;
545
546 default:
547 break;
548 }
549
550 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
551 {
552 if (fmt[i] == 'e')
553 {
554 if (! oprs_unchanged_p (XEXP (x, i), insn, after_insn))
555 return 0;
556 }
557 else if (fmt[i] == 'E')
558 for (j = 0; j < XVECLEN (x, i); j++)
559 if (! oprs_unchanged_p (XVECEXP (x, i, j), insn, after_insn))
560 return 0;
561 }
562
563 return 1;
564 }
565 \f
566
567 /* Used for communication between find_mem_conflicts and
568 load_killed_in_block_p. Nonzero if find_mem_conflicts finds a
569 conflict between two memory references.
570 This is a bit of a hack to work around the limitations of note_stores. */
571 static int mems_conflict_p;
572
573 /* DEST is the output of an instruction. If it is a memory reference, and
574 possibly conflicts with the load found in DATA, then set mems_conflict_p
575 to a nonzero value. */
576
577 static void
578 find_mem_conflicts (rtx dest, const_rtx setter ATTRIBUTE_UNUSED,
579 void *data)
580 {
581 rtx mem_op = (rtx) data;
582
583 while (GET_CODE (dest) == SUBREG
584 || GET_CODE (dest) == ZERO_EXTRACT
585 || GET_CODE (dest) == STRICT_LOW_PART)
586 dest = XEXP (dest, 0);
587
588 /* If DEST is not a MEM, then it will not conflict with the load. Note
589 that function calls are assumed to clobber memory, but are handled
590 elsewhere. */
591 if (! MEM_P (dest))
592 return;
593
594 if (true_dependence (dest, GET_MODE (dest), mem_op))
595 mems_conflict_p = 1;
596 }
597 \f
598
599 /* Return nonzero if the expression in X (a memory reference) is killed
600 in the current basic block before (if AFTER_INSN is false) or after
601 (if AFTER_INSN is true) the insn with the CUID in UID_LIMIT.
602
603 This function assumes that the modifies_mem table is flushed when
604 the hash table construction or redundancy elimination phases start
605 processing a new basic block. */
606
607 static int
608 load_killed_in_block_p (int uid_limit, rtx x, bool after_insn)
609 {
610 struct modifies_mem *list_entry = modifies_mem_list;
611
612 while (list_entry)
613 {
614 rtx_insn *setter = list_entry->insn;
615
616 /* Ignore entries in the list that do not apply. */
617 if ((after_insn
618 && INSN_CUID (setter) < uid_limit)
619 || (! after_insn
620 && INSN_CUID (setter) > uid_limit))
621 {
622 list_entry = list_entry->next;
623 continue;
624 }
625
626 /* If SETTER is a call everything is clobbered. Note that calls
627 to pure functions are never put on the list, so we need not
628 worry about them. */
629 if (CALL_P (setter))
630 return 1;
631
632 /* SETTER must be an insn of some kind that sets memory. Call
633 note_stores to examine each hunk of memory that is modified.
634 It will set mems_conflict_p to nonzero if there may be a
635 conflict between X and SETTER. */
636 mems_conflict_p = 0;
637 note_stores (PATTERN (setter), find_mem_conflicts, x);
638 if (mems_conflict_p)
639 return 1;
640
641 list_entry = list_entry->next;
642 }
643 return 0;
644 }
645 \f
646
647 /* Record register first/last/block set information for REGNO in INSN. */
648
649 static inline void
650 record_last_reg_set_info (rtx_insn *insn, rtx reg)
651 {
652 unsigned int regno, end_regno;
653
654 regno = REGNO (reg);
655 end_regno = END_HARD_REGNO (reg);
656 do
657 reg_avail_info[regno] = INSN_CUID (insn);
658 while (++regno < end_regno);
659 }
660
661 static inline void
662 record_last_reg_set_info_regno (rtx_insn *insn, int regno)
663 {
664 reg_avail_info[regno] = INSN_CUID (insn);
665 }
666
667
668 /* Record memory modification information for INSN. We do not actually care
669 about the memory location(s) that are set, or even how they are set (consider
670 a CALL_INSN). We merely need to record which insns modify memory. */
671
672 static void
673 record_last_mem_set_info (rtx_insn *insn)
674 {
675 struct modifies_mem *list_entry;
676
677 list_entry = (struct modifies_mem *) obstack_alloc (&modifies_mem_obstack,
678 sizeof (struct modifies_mem));
679 list_entry->insn = insn;
680 list_entry->next = modifies_mem_list;
681 modifies_mem_list = list_entry;
682 }
683
684 /* Called from compute_hash_table via note_stores to handle one
685 SET or CLOBBER in an insn. DATA is really the instruction in which
686 the SET is taking place. */
687
688 static void
689 record_last_set_info (rtx dest, const_rtx setter ATTRIBUTE_UNUSED, void *data)
690 {
691 rtx_insn *last_set_insn = (rtx_insn *) data;
692
693 if (GET_CODE (dest) == SUBREG)
694 dest = SUBREG_REG (dest);
695
696 if (REG_P (dest))
697 record_last_reg_set_info (last_set_insn, dest);
698 else if (MEM_P (dest))
699 {
700 /* Ignore pushes, they don't clobber memory. They may still
701 clobber the stack pointer though. Some targets do argument
702 pushes without adding REG_INC notes. See e.g. PR25196,
703 where a pushsi2 on i386 doesn't have REG_INC notes. Note
704 such changes here too. */
705 if (! push_operand (dest, GET_MODE (dest)))
706 record_last_mem_set_info (last_set_insn);
707 else
708 record_last_reg_set_info_regno (last_set_insn, STACK_POINTER_REGNUM);
709 }
710 }
711
712
713 /* Reset tables used to keep track of what's still available since the
714 start of the block. */
715
716 static void
717 reset_opr_set_tables (void)
718 {
719 memset (reg_avail_info, 0, FIRST_PSEUDO_REGISTER * sizeof (int));
720 obstack_free (&modifies_mem_obstack, modifies_mem_obstack_bottom);
721 modifies_mem_list = NULL;
722 }
723 \f
724
725 /* Record things set by INSN.
726 This data is used by oprs_unchanged_p. */
727
728 static void
729 record_opr_changes (rtx_insn *insn)
730 {
731 rtx note;
732
733 /* Find all stores and record them. */
734 note_stores (PATTERN (insn), record_last_set_info, insn);
735
736 /* Also record autoincremented REGs for this insn as changed. */
737 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
738 if (REG_NOTE_KIND (note) == REG_INC)
739 record_last_reg_set_info (insn, XEXP (note, 0));
740
741 /* Finally, if this is a call, record all call clobbers. */
742 if (CALL_P (insn))
743 {
744 unsigned int regno;
745 rtx link, x;
746 hard_reg_set_iterator hrsi;
747 EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call, 0, regno, hrsi)
748 record_last_reg_set_info_regno (insn, regno);
749
750 for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
751 if (GET_CODE (XEXP (link, 0)) == CLOBBER)
752 {
753 x = XEXP (XEXP (link, 0), 0);
754 if (REG_P (x))
755 {
756 gcc_assert (HARD_REGISTER_P (x));
757 record_last_reg_set_info (insn, x);
758 }
759 }
760
761 if (! RTL_CONST_OR_PURE_CALL_P (insn))
762 record_last_mem_set_info (insn);
763 }
764 }
765 \f
766
767 /* Scan the pattern of INSN and add an entry to the hash TABLE.
768 After reload we are interested in loads/stores only. */
769
770 static void
771 hash_scan_set (rtx_insn *insn)
772 {
773 rtx pat = PATTERN (insn);
774 rtx src = SET_SRC (pat);
775 rtx dest = SET_DEST (pat);
776
777 /* We are only interested in loads and stores. */
778 if (! MEM_P (src) && ! MEM_P (dest))
779 return;
780
781 /* Don't mess with jumps and nops. */
782 if (JUMP_P (insn) || set_noop_p (pat))
783 return;
784
785 if (REG_P (dest))
786 {
787 if (/* Don't CSE something if we can't do a reg/reg copy. */
788 can_copy_p (GET_MODE (dest))
789 /* Is SET_SRC something we want to gcse? */
790 && general_operand (src, GET_MODE (src))
791 #ifdef STACK_REGS
792 /* Never consider insns touching the register stack. It may
793 create situations that reg-stack cannot handle (e.g. a stack
794 register live across an abnormal edge). */
795 && (REGNO (dest) < FIRST_STACK_REG || REGNO (dest) > LAST_STACK_REG)
796 #endif
797 /* An expression is not available if its operands are
798 subsequently modified, including this insn. */
799 && oprs_unchanged_p (src, insn, true))
800 {
801 insert_expr_in_table (src, insn);
802 }
803 }
804 else if (REG_P (src))
805 {
806 /* Only record sets of pseudo-regs in the hash table. */
807 if (/* Don't CSE something if we can't do a reg/reg copy. */
808 can_copy_p (GET_MODE (src))
809 /* Is SET_DEST something we want to gcse? */
810 && general_operand (dest, GET_MODE (dest))
811 #ifdef STACK_REGS
812 /* As above for STACK_REGS. */
813 && (REGNO (src) < FIRST_STACK_REG || REGNO (src) > LAST_STACK_REG)
814 #endif
815 && ! (flag_float_store && FLOAT_MODE_P (GET_MODE (dest)))
816 /* Check if the memory expression is killed after insn. */
817 && ! load_killed_in_block_p (INSN_CUID (insn) + 1, dest, true)
818 && oprs_unchanged_p (XEXP (dest, 0), insn, true))
819 {
820 insert_expr_in_table (dest, insn);
821 }
822 }
823 }
824 \f
825
826 /* Create hash table of memory expressions available at end of basic
827 blocks. Basically you should think of this hash table as the
828 representation of AVAIL_OUT. This is the set of expressions that
829 is generated in a basic block and not killed before the end of the
830 same basic block. Notice that this is really a local computation. */
831
832 static void
833 compute_hash_table (void)
834 {
835 basic_block bb;
836
837 FOR_EACH_BB_FN (bb, cfun)
838 {
839 rtx_insn *insn;
840
841 /* First pass over the instructions records information used to
842 determine when registers and memory are last set.
843 Since we compute a "local" AVAIL_OUT, reset the tables that
844 help us keep track of what has been modified since the start
845 of the block. */
846 reset_opr_set_tables ();
847 FOR_BB_INSNS (bb, insn)
848 {
849 if (INSN_P (insn))
850 record_opr_changes (insn);
851 }
852
853 /* The next pass actually builds the hash table. */
854 FOR_BB_INSNS (bb, insn)
855 if (INSN_P (insn) && GET_CODE (PATTERN (insn)) == SET)
856 hash_scan_set (insn);
857 }
858 }
859 \f
860
861 /* Check if register REG is killed in any insn waiting to be inserted on
862 edge E. This function is required to check that our data flow analysis
863 is still valid prior to commit_edge_insertions. */
864
865 static bool
866 reg_killed_on_edge (rtx reg, edge e)
867 {
868 rtx_insn *insn;
869
870 for (insn = e->insns.r; insn; insn = NEXT_INSN (insn))
871 if (INSN_P (insn) && reg_set_p (reg, insn))
872 return true;
873
874 return false;
875 }
876
877 /* Similar to above - check if register REG is used in any insn waiting
878 to be inserted on edge E.
879 Assumes no such insn can be a CALL_INSN; if so call reg_used_between_p
880 with PREV(insn),NEXT(insn) instead of calling reg_overlap_mentioned_p. */
881
882 static bool
883 reg_used_on_edge (rtx reg, edge e)
884 {
885 rtx_insn *insn;
886
887 for (insn = e->insns.r; insn; insn = NEXT_INSN (insn))
888 if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
889 return true;
890
891 return false;
892 }
893 \f
894 /* Return the loaded/stored register of a load/store instruction. */
895
896 static rtx
897 get_avail_load_store_reg (rtx_insn *insn)
898 {
899 if (REG_P (SET_DEST (PATTERN (insn))))
900 /* A load. */
901 return SET_DEST (PATTERN (insn));
902 else
903 {
904 /* A store. */
905 gcc_assert (REG_P (SET_SRC (PATTERN (insn))));
906 return SET_SRC (PATTERN (insn));
907 }
908 }
909
910 /* Return nonzero if the predecessors of BB are "well behaved". */
911
912 static bool
913 bb_has_well_behaved_predecessors (basic_block bb)
914 {
915 edge pred;
916 edge_iterator ei;
917
918 if (EDGE_COUNT (bb->preds) == 0)
919 return false;
920
921 FOR_EACH_EDGE (pred, ei, bb->preds)
922 {
923 if ((pred->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (pred))
924 return false;
925
926 if ((pred->flags & EDGE_ABNORMAL_CALL) && cfun->has_nonlocal_label)
927 return false;
928
929 if (tablejump_p (BB_END (pred->src), NULL, NULL))
930 return false;
931 }
932 return true;
933 }
934
935
936 /* Search for the occurrences of expression in BB. */
937
938 static struct occr*
939 get_bb_avail_insn (basic_block bb, struct occr *occr)
940 {
941 for (; occr != NULL; occr = occr->next)
942 if (BLOCK_FOR_INSN (occr->insn) == bb)
943 return occr;
944 return NULL;
945 }
946
947
948 /* This handles the case where several stores feed a partially redundant
949 load. It checks if the redundancy elimination is possible and if it's
950 worth it.
951
952 Redundancy elimination is possible if,
953 1) None of the operands of an insn have been modified since the start
954 of the current basic block.
955 2) In any predecessor of the current basic block, the same expression
956 is generated.
957
958 See the function body for the heuristics that determine if eliminating
959 a redundancy is also worth doing, assuming it is possible. */
960
961 static void
962 eliminate_partially_redundant_load (basic_block bb, rtx_insn *insn,
963 struct expr *expr)
964 {
965 edge pred;
966 rtx_insn *avail_insn = NULL;
967 rtx avail_reg;
968 rtx dest, pat;
969 struct occr *a_occr;
970 struct unoccr *occr, *avail_occrs = NULL;
971 struct unoccr *unoccr, *unavail_occrs = NULL, *rollback_unoccr = NULL;
972 int npred_ok = 0;
973 gcov_type ok_count = 0; /* Redundant load execution count. */
974 gcov_type critical_count = 0; /* Execution count of critical edges. */
975 edge_iterator ei;
976 bool critical_edge_split = false;
977
978 /* The execution count of the loads to be added to make the
979 load fully redundant. */
980 gcov_type not_ok_count = 0;
981 basic_block pred_bb;
982
983 pat = PATTERN (insn);
984 dest = SET_DEST (pat);
985
986 /* Check that the loaded register is not used, set, or killed from the
987 beginning of the block. */
988 if (reg_changed_after_insn_p (dest, 0)
989 || reg_used_between_p (dest, PREV_INSN (BB_HEAD (bb)), insn))
990 return;
991
992 /* Check potential for replacing load with copy for predecessors. */
993 FOR_EACH_EDGE (pred, ei, bb->preds)
994 {
995 rtx_insn *next_pred_bb_end;
996
997 avail_insn = NULL;
998 avail_reg = NULL_RTX;
999 pred_bb = pred->src;
1000 next_pred_bb_end = NEXT_INSN (BB_END (pred_bb));
1001 for (a_occr = get_bb_avail_insn (pred_bb, expr->avail_occr); a_occr;
1002 a_occr = get_bb_avail_insn (pred_bb, a_occr->next))
1003 {
1004 /* Check if the loaded register is not used. */
1005 avail_insn = a_occr->insn;
1006 avail_reg = get_avail_load_store_reg (avail_insn);
1007 gcc_assert (avail_reg);
1008
1009 /* Make sure we can generate a move from register avail_reg to
1010 dest. */
1011 rtx_insn *move = as_a <rtx_insn *>
1012 (gen_move_insn (copy_rtx (dest), copy_rtx (avail_reg)));
1013 extract_insn (move);
1014 if (! constrain_operands (1, get_preferred_alternatives (insn,
1015 pred_bb))
1016 || reg_killed_on_edge (avail_reg, pred)
1017 || reg_used_on_edge (dest, pred))
1018 {
1019 avail_insn = NULL;
1020 continue;
1021 }
1022 if (!reg_set_between_p (avail_reg, avail_insn, next_pred_bb_end))
1023 /* AVAIL_INSN remains non-null. */
1024 break;
1025 else
1026 avail_insn = NULL;
1027 }
1028
1029 if (EDGE_CRITICAL_P (pred))
1030 critical_count += pred->count;
1031
1032 if (avail_insn != NULL_RTX)
1033 {
1034 npred_ok++;
1035 ok_count += pred->count;
1036 if (! set_noop_p (PATTERN (gen_move_insn (copy_rtx (dest),
1037 copy_rtx (avail_reg)))))
1038 {
1039 /* Check if there is going to be a split. */
1040 if (EDGE_CRITICAL_P (pred))
1041 critical_edge_split = true;
1042 }
1043 else /* Its a dead move no need to generate. */
1044 continue;
1045 occr = (struct unoccr *) obstack_alloc (&unoccr_obstack,
1046 sizeof (struct unoccr));
1047 occr->insn = avail_insn;
1048 occr->pred = pred;
1049 occr->next = avail_occrs;
1050 avail_occrs = occr;
1051 if (! rollback_unoccr)
1052 rollback_unoccr = occr;
1053 }
1054 else
1055 {
1056 /* Adding a load on a critical edge will cause a split. */
1057 if (EDGE_CRITICAL_P (pred))
1058 critical_edge_split = true;
1059 not_ok_count += pred->count;
1060 unoccr = (struct unoccr *) obstack_alloc (&unoccr_obstack,
1061 sizeof (struct unoccr));
1062 unoccr->insn = NULL;
1063 unoccr->pred = pred;
1064 unoccr->next = unavail_occrs;
1065 unavail_occrs = unoccr;
1066 if (! rollback_unoccr)
1067 rollback_unoccr = unoccr;
1068 }
1069 }
1070
1071 if (/* No load can be replaced by copy. */
1072 npred_ok == 0
1073 /* Prevent exploding the code. */
1074 || (optimize_bb_for_size_p (bb) && npred_ok > 1)
1075 /* If we don't have profile information we cannot tell if splitting
1076 a critical edge is profitable or not so don't do it. */
1077 || ((! profile_info || ! flag_branch_probabilities
1078 || targetm.cannot_modify_jumps_p ())
1079 && critical_edge_split))
1080 goto cleanup;
1081
1082 /* Check if it's worth applying the partial redundancy elimination. */
1083 if (ok_count < GCSE_AFTER_RELOAD_PARTIAL_FRACTION * not_ok_count)
1084 goto cleanup;
1085 if (ok_count < GCSE_AFTER_RELOAD_CRITICAL_FRACTION * critical_count)
1086 goto cleanup;
1087
1088 /* Generate moves to the loaded register from where
1089 the memory is available. */
1090 for (occr = avail_occrs; occr; occr = occr->next)
1091 {
1092 avail_insn = occr->insn;
1093 pred = occr->pred;
1094 /* Set avail_reg to be the register having the value of the
1095 memory. */
1096 avail_reg = get_avail_load_store_reg (avail_insn);
1097 gcc_assert (avail_reg);
1098
1099 insert_insn_on_edge (gen_move_insn (copy_rtx (dest),
1100 copy_rtx (avail_reg)),
1101 pred);
1102 stats.moves_inserted++;
1103
1104 if (dump_file)
1105 fprintf (dump_file,
1106 "generating move from %d to %d on edge from %d to %d\n",
1107 REGNO (avail_reg),
1108 REGNO (dest),
1109 pred->src->index,
1110 pred->dest->index);
1111 }
1112
1113 /* Regenerate loads where the memory is unavailable. */
1114 for (unoccr = unavail_occrs; unoccr; unoccr = unoccr->next)
1115 {
1116 pred = unoccr->pred;
1117 insert_insn_on_edge (copy_insn (PATTERN (insn)), pred);
1118 stats.copies_inserted++;
1119
1120 if (dump_file)
1121 {
1122 fprintf (dump_file,
1123 "generating on edge from %d to %d a copy of load: ",
1124 pred->src->index,
1125 pred->dest->index);
1126 print_rtl (dump_file, PATTERN (insn));
1127 fprintf (dump_file, "\n");
1128 }
1129 }
1130
1131 /* Delete the insn if it is not available in this block and mark it
1132 for deletion if it is available. If insn is available it may help
1133 discover additional redundancies, so mark it for later deletion. */
1134 for (a_occr = get_bb_avail_insn (bb, expr->avail_occr);
1135 a_occr && (a_occr->insn != insn);
1136 a_occr = get_bb_avail_insn (bb, a_occr->next))
1137 ;
1138
1139 if (!a_occr)
1140 {
1141 stats.insns_deleted++;
1142
1143 if (dump_file)
1144 {
1145 fprintf (dump_file, "deleting insn:\n");
1146 print_rtl_single (dump_file, insn);
1147 fprintf (dump_file, "\n");
1148 }
1149 delete_insn (insn);
1150 }
1151 else
1152 a_occr->deleted_p = 1;
1153
1154 cleanup:
1155 if (rollback_unoccr)
1156 obstack_free (&unoccr_obstack, rollback_unoccr);
1157 }
1158
1159 /* Performing the redundancy elimination as described before. */
1160
1161 static void
1162 eliminate_partially_redundant_loads (void)
1163 {
1164 rtx_insn *insn;
1165 basic_block bb;
1166
1167 /* Note we start at block 1. */
1168
1169 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1170 return;
1171
1172 FOR_BB_BETWEEN (bb,
1173 ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->next_bb,
1174 EXIT_BLOCK_PTR_FOR_FN (cfun),
1175 next_bb)
1176 {
1177 /* Don't try anything on basic blocks with strange predecessors. */
1178 if (! bb_has_well_behaved_predecessors (bb))
1179 continue;
1180
1181 /* Do not try anything on cold basic blocks. */
1182 if (optimize_bb_for_size_p (bb))
1183 continue;
1184
1185 /* Reset the table of things changed since the start of the current
1186 basic block. */
1187 reset_opr_set_tables ();
1188
1189 /* Look at all insns in the current basic block and see if there are
1190 any loads in it that we can record. */
1191 FOR_BB_INSNS (bb, insn)
1192 {
1193 /* Is it a load - of the form (set (reg) (mem))? */
1194 if (NONJUMP_INSN_P (insn)
1195 && GET_CODE (PATTERN (insn)) == SET
1196 && REG_P (SET_DEST (PATTERN (insn)))
1197 && MEM_P (SET_SRC (PATTERN (insn))))
1198 {
1199 rtx pat = PATTERN (insn);
1200 rtx src = SET_SRC (pat);
1201 struct expr *expr;
1202
1203 if (!MEM_VOLATILE_P (src)
1204 && GET_MODE (src) != BLKmode
1205 && general_operand (src, GET_MODE (src))
1206 /* Are the operands unchanged since the start of the
1207 block? */
1208 && oprs_unchanged_p (src, insn, false)
1209 && !(cfun->can_throw_non_call_exceptions && may_trap_p (src))
1210 && !side_effects_p (src)
1211 /* Is the expression recorded? */
1212 && (expr = lookup_expr_in_table (src)) != NULL)
1213 {
1214 /* We now have a load (insn) and an available memory at
1215 its BB start (expr). Try to remove the loads if it is
1216 redundant. */
1217 eliminate_partially_redundant_load (bb, insn, expr);
1218 }
1219 }
1220
1221 /* Keep track of everything modified by this insn, so that we
1222 know what has been modified since the start of the current
1223 basic block. */
1224 if (INSN_P (insn))
1225 record_opr_changes (insn);
1226 }
1227 }
1228
1229 commit_edge_insertions ();
1230 }
1231
1232 /* Go over the expression hash table and delete insns that were
1233 marked for later deletion. */
1234
1235 /* This helper is called via htab_traverse. */
1236 int
1237 delete_redundant_insns_1 (expr **slot, void *data ATTRIBUTE_UNUSED)
1238 {
1239 struct expr *exprs = *slot;
1240 struct occr *occr;
1241
1242 for (occr = exprs->avail_occr; occr != NULL; occr = occr->next)
1243 {
1244 if (occr->deleted_p && dbg_cnt (gcse2_delete))
1245 {
1246 delete_insn (occr->insn);
1247 stats.insns_deleted++;
1248
1249 if (dump_file)
1250 {
1251 fprintf (dump_file, "deleting insn:\n");
1252 print_rtl_single (dump_file, occr->insn);
1253 fprintf (dump_file, "\n");
1254 }
1255 }
1256 }
1257
1258 return 1;
1259 }
1260
1261 static void
1262 delete_redundant_insns (void)
1263 {
1264 expr_table->traverse <void *, delete_redundant_insns_1> (NULL);
1265 if (dump_file)
1266 fprintf (dump_file, "\n");
1267 }
1268
1269 /* Main entry point of the GCSE after reload - clean some redundant loads
1270 due to spilling. */
1271
1272 static void
1273 gcse_after_reload_main (rtx f ATTRIBUTE_UNUSED)
1274 {
1275
1276 memset (&stats, 0, sizeof (stats));
1277
1278 /* Allocate memory for this pass.
1279 Also computes and initializes the insns' CUIDs. */
1280 alloc_mem ();
1281
1282 /* We need alias analysis. */
1283 init_alias_analysis ();
1284
1285 compute_hash_table ();
1286
1287 if (dump_file)
1288 dump_hash_table (dump_file);
1289
1290 if (expr_table->elements () > 0)
1291 {
1292 eliminate_partially_redundant_loads ();
1293 delete_redundant_insns ();
1294
1295 if (dump_file)
1296 {
1297 fprintf (dump_file, "GCSE AFTER RELOAD stats:\n");
1298 fprintf (dump_file, "copies inserted: %d\n", stats.copies_inserted);
1299 fprintf (dump_file, "moves inserted: %d\n", stats.moves_inserted);
1300 fprintf (dump_file, "insns deleted: %d\n", stats.insns_deleted);
1301 fprintf (dump_file, "\n\n");
1302 }
1303
1304 statistics_counter_event (cfun, "copies inserted",
1305 stats.copies_inserted);
1306 statistics_counter_event (cfun, "moves inserted",
1307 stats.moves_inserted);
1308 statistics_counter_event (cfun, "insns deleted",
1309 stats.insns_deleted);
1310 }
1311
1312 /* We are finished with alias. */
1313 end_alias_analysis ();
1314
1315 free_mem ();
1316 }
1317
1318 \f
1319
1320 static unsigned int
1321 rest_of_handle_gcse2 (void)
1322 {
1323 gcse_after_reload_main (get_insns ());
1324 rebuild_jump_labels (get_insns ());
1325 return 0;
1326 }
1327
1328 namespace {
1329
1330 const pass_data pass_data_gcse2 =
1331 {
1332 RTL_PASS, /* type */
1333 "gcse2", /* name */
1334 OPTGROUP_NONE, /* optinfo_flags */
1335 TV_GCSE_AFTER_RELOAD, /* tv_id */
1336 0, /* properties_required */
1337 0, /* properties_provided */
1338 0, /* properties_destroyed */
1339 0, /* todo_flags_start */
1340 0, /* todo_flags_finish */
1341 };
1342
1343 class pass_gcse2 : public rtl_opt_pass
1344 {
1345 public:
1346 pass_gcse2 (gcc::context *ctxt)
1347 : rtl_opt_pass (pass_data_gcse2, ctxt)
1348 {}
1349
1350 /* opt_pass methods: */
1351 virtual bool gate (function *fun)
1352 {
1353 return (optimize > 0 && flag_gcse_after_reload
1354 && optimize_function_for_speed_p (fun));
1355 }
1356
1357 virtual unsigned int execute (function *) { return rest_of_handle_gcse2 (); }
1358
1359 }; // class pass_gcse2
1360
1361 } // anon namespace
1362
1363 rtl_opt_pass *
1364 make_pass_gcse2 (gcc::context *ctxt)
1365 {
1366 return new pass_gcse2 (ctxt);
1367 }