Line data Source code
1 : /* Post reload partially redundant load elimination
2 : Copyright (C) 2004-2026 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 "backend.h"
24 : #include "target.h"
25 : #include "rtl.h"
26 : #include "tree.h"
27 : #include "predict.h"
28 : #include "df.h"
29 : #include "memmodel.h"
30 : #include "tm_p.h"
31 : #include "insn-config.h"
32 : #include "emit-rtl.h"
33 : #include "recog.h"
34 :
35 : #include "cfgrtl.h"
36 : #include "cfgcleanup.h"
37 : #include "profile.h"
38 : #include "expr.h"
39 : #include "tree-pass.h"
40 : #include "dbgcnt.h"
41 : #include "intl.h"
42 : #include "gcse-common.h"
43 : #include "gcse.h"
44 : #include "lcm.h"
45 : #include "regs.h"
46 : #include "function-abi.h"
47 :
48 : /* The following code implements gcse after reload, the purpose of this
49 : pass is to cleanup redundant loads generated by reload and other
50 : optimizations that come after gcse. It searches for simple inter-block
51 : redundancies and tries to eliminate them by adding moves and loads
52 : in cold places.
53 :
54 : Perform partially redundant load elimination, try to eliminate redundant
55 : loads created by the reload pass. We try to look for full or partial
56 : redundant loads fed by one or more loads/stores in predecessor BBs,
57 : and try adding loads to make them fully redundant. We also check if
58 : it's worth adding loads to be able to delete the redundant load.
59 :
60 : Algorithm:
61 : 1. Build available expressions hash table:
62 : For each load/store instruction, if the loaded/stored memory didn't
63 : change until the end of the basic block add this memory expression to
64 : the hash table.
65 : 2. Perform Redundancy elimination:
66 : Compute where the holding register of each available memory
67 : expression still contains the memory value.
68 : For each load instruction do the following:
69 : if the same register holds the value of the load on all incoming
70 : paths, replace the load with a copy from that register. Otherwise
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 :
83 :
84 : /* Keep statistics of this pass. */
85 : static struct
86 : {
87 : int moves_inserted;
88 : int copies_inserted;
89 : int insns_deleted;
90 : int loads_forwarded;
91 : } stats;
92 :
93 : /* We need to keep a hash table of expressions. The table entries are of
94 : type 'struct expr', and for each expression there is a single linked
95 : list of occurrences. */
96 :
97 : /* Expression elements in the hash table. */
98 : struct expr
99 : {
100 : /* The expression (SET_SRC for expressions, PATTERN for assignments). */
101 : rtx expr;
102 :
103 : /* The same hash for this entry. */
104 : hashval_t hash;
105 :
106 : /* Index in the transparent bitmaps. */
107 : unsigned int bitmap_index;
108 :
109 : /* The availability facts of this expression, one for each distinct
110 : holding register, have the indices FIRST_FACT to
111 : FIRST_FACT + NUM_FACTS - 1. */
112 : unsigned int first_fact;
113 : unsigned int num_facts;
114 :
115 : /* List of available occurrence in basic blocks in the function. */
116 : struct occr *avail_occr;
117 : };
118 :
119 : /* Hashtable helpers. */
120 :
121 : struct expr_hasher : nofree_ptr_hash <expr>
122 : {
123 : static inline hashval_t hash (const expr *);
124 : static inline bool equal (const expr *, const expr *);
125 : };
126 :
127 :
128 : /* Hash expression X.
129 : DO_NOT_RECORD_P is a boolean indicating if a volatile operand is found
130 : or if the expression contains something we don't want to insert in the
131 : table. */
132 :
133 : static hashval_t
134 1041790 : hash_expr (rtx x, int *do_not_record_p)
135 : {
136 1041790 : *do_not_record_p = 0;
137 1041790 : return hash_rtx (x, GET_MODE (x), do_not_record_p,
138 1041790 : NULL, /*have_reg_qty=*/false);
139 : }
140 :
141 : /* Callback for hashtab.
142 : Return the hash value for expression EXP. We don't actually hash
143 : here, we just return the cached hash value. */
144 :
145 : inline hashval_t
146 2066448 : expr_hasher::hash (const expr *exp)
147 : {
148 2066448 : return exp->hash;
149 : }
150 :
151 : /* Callback for hashtab.
152 : Return nonzero if exp1 is equivalent to exp2. */
153 :
154 : inline bool
155 2985792 : expr_hasher::equal (const expr *exp1, const expr *exp2)
156 : {
157 2985792 : bool equiv_p = exp_equiv_p (exp1->expr, exp2->expr, 0, true);
158 :
159 2985792 : gcc_assert (!equiv_p || exp1->hash == exp2->hash);
160 2985792 : return equiv_p;
161 : }
162 :
163 : /* The table itself. */
164 : static hash_table<expr_hasher> *expr_table;
165 :
166 :
167 : static struct obstack expr_obstack;
168 :
169 : /* Occurrence of an expression.
170 : There is at most one occurrence per basic block. If a pattern appears
171 : more than once, the last appearance is used. */
172 :
173 : struct occr
174 : {
175 : /* Next occurrence of this expression. */
176 : struct occr *next;
177 : /* The insn that computes the expression. */
178 : rtx_insn *insn;
179 : /* Nonzero if this [anticipatable] occurrence has been deleted. */
180 : char deleted_p;
181 : /* True if the loaded or stored register of INSN keeps its value until
182 : the end of the block. */
183 : bool reg_available_p;
184 : };
185 :
186 : static struct obstack occr_obstack;
187 :
188 : /* The following structure holds the information about the occurrences of
189 : the redundant instructions. */
190 : struct unoccr
191 : {
192 : struct unoccr *next;
193 : edge pred;
194 : rtx_insn *insn;
195 : };
196 :
197 : static struct obstack unoccr_obstack;
198 :
199 : /* Array where each element is the CUID if the insn that last set the hard
200 : register with the number of the element, since the start of the current
201 : basic block.
202 :
203 : This array is used during the building of the hash table (step 1) to
204 : determine if a reg is killed before the end of a basic block.
205 :
206 : It is also used when eliminating partial redundancies (step 2) to see
207 : if a reg was modified since the start of a basic block. */
208 : static int *reg_avail_info;
209 :
210 : /* The hard registers that REG_AVAIL_INFO records as set. */
211 : static HARD_REG_SET regs_set_in_block;
212 :
213 : /* A list of insns that may modify memory within the current basic block. */
214 : struct modifies_mem
215 : {
216 : rtx_insn *insn;
217 : struct modifies_mem *next;
218 : };
219 : static struct modifies_mem *modifies_mem_list;
220 :
221 : /* The modifies_mem structs also go on an obstack, only this obstack is
222 : freed each time after completing the analysis or transformations on
223 : a basic block. So we allocate a dummy modifies_mem_obstack_bottom
224 : object on the obstack to keep track of the bottom of the obstack. */
225 : static struct obstack modifies_mem_obstack;
226 : static struct modifies_mem *modifies_mem_obstack_bottom;
227 :
228 : /* Mapping of insn UIDs to CUIDs.
229 : CUIDs are like UIDs except they increase monotonically in each basic
230 : block, have no gaps, and only apply to real insns. */
231 : static int *uid_cuid;
232 : #define INSN_CUID(INSN) (uid_cuid[INSN_UID (INSN)])
233 :
234 : /* Bitmap of blocks which have memory stores. */
235 : static bitmap modify_mem_list_set;
236 :
237 : /* Bitmap of blocks which have calls. */
238 : static bitmap blocks_with_calls;
239 :
240 : /* Vector indexed by block # with a list of all the insns that
241 : modify memory within the block. */
242 : static vec<rtx_insn *> *modify_mem_list;
243 :
244 : /* Vector indexed by block # with a canonicalized list of insns
245 : that modify memory in the block. */
246 : static vec<modify_pair> *canon_modify_mem_list;
247 :
248 : /* Vector of simple bitmaps indexed by block number. Each component sbitmap
249 : indicates which expressions are transparent through the block. */
250 : static sbitmap *transp;
251 :
252 : /* Vector indexed by block number of the hard registers that the block
253 : modifies, including partial call clobbers. */
254 : static HARD_REG_SET *modified_regs;
255 :
256 : /* For each availability fact, the hard register that holds the value of
257 : the expression. */
258 : static vec<rtx> fact_regs;
259 :
260 : /* Vector indexed by block number of the availability facts at the start of
261 : the block, or null if they are not known. */
262 : static sbitmap *fact_avin;
263 :
264 :
265 : /* Helpers for memory allocation/freeing. */
266 : static void alloc_mem (void);
267 : static void free_mem (void);
268 :
269 : /* Support for hash table construction and transformations. */
270 : static bool reg_changed_after_insn_p (rtx, int);
271 : static bool oprs_unchanged_p (rtx, rtx_insn *, bool);
272 : static void record_last_reg_set_info (rtx_insn *, rtx);
273 : static void record_last_reg_set_info_regno (rtx_insn *, int);
274 : static void record_last_mem_set_info (rtx_insn *);
275 : static void record_last_set_info (rtx, const_rtx, void *);
276 : static void record_opr_changes (rtx_insn *);
277 :
278 : static void find_mem_conflicts (rtx, const_rtx, void *);
279 : static bool load_killed_in_block_p (int, rtx, bool);
280 : static void reset_opr_set_tables (void);
281 :
282 : /* Hash table support. */
283 : static hashval_t hash_expr (rtx, int *);
284 : static void insert_expr_in_table (rtx, rtx_insn *);
285 : static struct expr *lookup_expr_in_table (rtx);
286 : static void dump_hash_table (FILE *);
287 :
288 : /* Helpers for eliminate_partially_redundant_load. */
289 : static bool reg_killed_on_edge (rtx, edge);
290 : static bool reg_used_on_edge (rtx, edge);
291 :
292 : static rtx get_avail_load_store_reg (rtx_insn *);
293 :
294 : static bool bb_has_well_behaved_predecessors (basic_block);
295 : static struct occr* get_bb_avail_insn (basic_block, struct occr *, int);
296 : static void hash_scan_set (rtx_insn *);
297 : static void compute_hash_table (void);
298 :
299 : /* The work horses of this pass. */
300 : static void eliminate_partially_redundant_load (basic_block,
301 : rtx_insn *,
302 : struct expr *);
303 : static void eliminate_partially_redundant_loads (void);
304 :
305 :
306 : /* Allocate memory for the CUID mapping array and register/memory
307 : tracking tables. */
308 :
309 : static void
310 94969 : alloc_mem (void)
311 : {
312 94969 : int i;
313 94969 : basic_block bb;
314 94969 : rtx_insn *insn;
315 :
316 : /* Find the largest UID and create a mapping from UIDs to CUIDs. */
317 94969 : uid_cuid = XCNEWVEC (int, get_max_uid () + 1);
318 94969 : i = 1;
319 1279819 : FOR_EACH_BB_FN (bb, cfun)
320 10888272 : FOR_BB_INSNS (bb, insn)
321 : {
322 9703422 : if (INSN_P (insn))
323 7231854 : uid_cuid[INSN_UID (insn)] = i++;
324 : else
325 2471568 : uid_cuid[INSN_UID (insn)] = i;
326 : }
327 :
328 : /* Allocate the available expressions hash table. We don't want to
329 : make the hash table too small, but unnecessarily making it too large
330 : also doesn't help. The i/4 is a gcse.cc relic, and seems like a
331 : reasonable choice. */
332 94969 : expr_table = new hash_table<expr_hasher> (MAX (i / 4, 13));
333 :
334 : /* We allocate everything on obstacks because we often can roll back
335 : the whole obstack to some point. Freeing obstacks is very fast. */
336 94969 : gcc_obstack_init (&expr_obstack);
337 94969 : gcc_obstack_init (&occr_obstack);
338 94969 : gcc_obstack_init (&unoccr_obstack);
339 94969 : gcc_obstack_init (&modifies_mem_obstack);
340 :
341 : /* Working array used to track the last set for each register
342 : in the current block. */
343 94969 : reg_avail_info = (int *) xmalloc (FIRST_PSEUDO_REGISTER * sizeof (int));
344 :
345 : /* Put a dummy modifies_mem object on the modifies_mem_obstack, so we
346 : can roll it back in reset_opr_set_tables. */
347 189938 : modifies_mem_obstack_bottom =
348 94969 : (struct modifies_mem *) obstack_alloc (&modifies_mem_obstack,
349 : sizeof (struct modifies_mem));
350 :
351 94969 : blocks_with_calls = BITMAP_ALLOC (NULL);
352 94969 : modify_mem_list_set = BITMAP_ALLOC (NULL);
353 :
354 94969 : modify_mem_list = (vec_rtx_heap *) xcalloc (last_basic_block_for_fn (cfun),
355 : sizeof (vec_rtx_heap));
356 94969 : canon_modify_mem_list
357 94969 : = (vec_modify_pair_heap *) xcalloc (last_basic_block_for_fn (cfun),
358 : sizeof (vec_modify_pair_heap));
359 94969 : modified_regs = XNEWVEC (HARD_REG_SET, last_basic_block_for_fn (cfun));
360 94969 : }
361 :
362 : /* Free memory allocated by alloc_mem. */
363 :
364 : static void
365 94969 : free_mem (void)
366 : {
367 94969 : free (uid_cuid);
368 :
369 94969 : delete expr_table;
370 94969 : expr_table = NULL;
371 :
372 94969 : obstack_free (&expr_obstack, NULL);
373 94969 : obstack_free (&occr_obstack, NULL);
374 94969 : obstack_free (&unoccr_obstack, NULL);
375 94969 : obstack_free (&modifies_mem_obstack, NULL);
376 :
377 94969 : unsigned i;
378 94969 : bitmap_iterator bi;
379 507781 : EXECUTE_IF_SET_IN_BITMAP (modify_mem_list_set, 0, i, bi)
380 : {
381 412812 : modify_mem_list[i].release ();
382 412812 : canon_modify_mem_list[i].release ();
383 : }
384 :
385 94969 : BITMAP_FREE (blocks_with_calls);
386 94969 : BITMAP_FREE (modify_mem_list_set);
387 94969 : free (reg_avail_info);
388 94969 : free (modify_mem_list);
389 94969 : free (canon_modify_mem_list);
390 94969 : free (modified_regs);
391 94969 : }
392 :
393 :
394 : /* Insert expression X in INSN in the hash TABLE.
395 : If it is already present, record it as the last occurrence in INSN's
396 : basic block. */
397 :
398 : static void
399 674662 : insert_expr_in_table (rtx x, rtx_insn *insn)
400 : {
401 674662 : int do_not_record_p;
402 674662 : hashval_t hash;
403 674662 : struct expr *cur_expr, **slot;
404 674662 : struct occr *avail_occr;
405 :
406 674662 : hash = hash_expr (x, &do_not_record_p);
407 :
408 : /* Do not insert expression in the table if it contains volatile operands,
409 : or if hash_expr determines the expression is something we don't want
410 : to or can't handle. */
411 674662 : if (do_not_record_p)
412 31197 : return;
413 :
414 : /* We anticipate that redundant expressions are rare, so for convenience
415 : allocate a new hash table element here already and set its fields.
416 : If we don't do this, we need a hack with a static struct expr. Anyway,
417 : obstack_free is really fast and one more obstack_alloc doesn't hurt if
418 : we're going to see more expressions later on. */
419 643465 : cur_expr = (struct expr *) obstack_alloc (&expr_obstack,
420 : sizeof (struct expr));
421 643465 : cur_expr->expr = x;
422 643465 : cur_expr->hash = hash;
423 643465 : cur_expr->avail_occr = NULL;
424 :
425 643465 : slot = expr_table->find_slot_with_hash (cur_expr, hash, INSERT);
426 :
427 643465 : if (! (*slot))
428 : {
429 : /* The expression isn't found, so insert it. */
430 399620 : *slot = cur_expr;
431 :
432 : /* Anytime we add an entry to the table, record the index
433 : of the new entry. The bitmap index starts counting
434 : at zero. */
435 399620 : cur_expr->bitmap_index = expr_table->elements () - 1;
436 : }
437 : else
438 : {
439 : /* The expression is already in the table, so roll back the
440 : obstack and use the existing table entry. */
441 243845 : obstack_free (&expr_obstack, cur_expr);
442 243845 : cur_expr = *slot;
443 : }
444 :
445 : /* Search for another occurrence in the same basic block. We insert
446 : insns blockwise from start to end, so keep appending to the
447 : start of the list so we have to check only a single element. */
448 643465 : avail_occr = cur_expr->avail_occr;
449 643465 : if (avail_occr
450 643465 : && BLOCK_FOR_INSN (avail_occr->insn) == BLOCK_FOR_INSN (insn))
451 13048 : avail_occr->insn = insn;
452 : else
453 : {
454 : /* First occurrence of this expression in this basic block. */
455 630417 : avail_occr = (struct occr *) obstack_alloc (&occr_obstack,
456 : sizeof (struct occr));
457 630417 : avail_occr->insn = insn;
458 630417 : avail_occr->next = cur_expr->avail_occr;
459 630417 : avail_occr->deleted_p = 0;
460 630417 : cur_expr->avail_occr = avail_occr;
461 : }
462 :
463 : /* REG_AVAIL_INFO describes the whole block at this point. */
464 643465 : avail_occr->reg_available_p
465 643465 : = !reg_changed_after_insn_p (get_avail_load_store_reg (insn),
466 643465 : INSN_CUID (insn));
467 : }
468 :
469 :
470 : /* Lookup pattern PAT in the expression hash table.
471 : The result is a pointer to the table entry, or NULL if not found. */
472 :
473 : static struct expr *
474 367128 : lookup_expr_in_table (rtx pat)
475 : {
476 367128 : int do_not_record_p;
477 367128 : struct expr **slot, *tmp_expr;
478 367128 : hashval_t hash = hash_expr (pat, &do_not_record_p);
479 :
480 367128 : if (do_not_record_p)
481 : return NULL;
482 :
483 367128 : tmp_expr = (struct expr *) obstack_alloc (&expr_obstack,
484 : sizeof (struct expr));
485 367128 : tmp_expr->expr = pat;
486 367128 : tmp_expr->hash = hash;
487 367128 : tmp_expr->avail_occr = NULL;
488 :
489 367128 : slot = expr_table->find_slot_with_hash (tmp_expr, hash, NO_INSERT);
490 367128 : obstack_free (&expr_obstack, tmp_expr);
491 :
492 367128 : if (!slot)
493 : return NULL;
494 : else
495 271147 : return (*slot);
496 : }
497 :
498 :
499 : /* Dump all expressions and occurrences that are currently in the
500 : expression hash table to FILE. */
501 :
502 : /* This helper is called via htab_traverse. */
503 : int
504 20 : dump_expr_hash_table_entry (expr **slot, FILE *file)
505 : {
506 20 : struct expr *exprs = *slot;
507 20 : struct occr *occr;
508 :
509 20 : fprintf (file, "expr: ");
510 20 : print_rtl (file, exprs->expr);
511 20 : fprintf (file,"\nhashcode: %u\n", exprs->hash);
512 20 : fprintf (file,"list of occurrences:\n");
513 20 : occr = exprs->avail_occr;
514 40 : while (occr)
515 : {
516 20 : rtx_insn *insn = occr->insn;
517 20 : print_rtl_single (file, insn);
518 20 : fprintf (file, "\n");
519 20 : occr = occr->next;
520 : }
521 20 : fprintf (file, "\n");
522 20 : return 1;
523 : }
524 :
525 : static void
526 8 : dump_hash_table (FILE *file)
527 : {
528 8 : fprintf (file, "\n\nexpression hash table\n");
529 8 : fprintf (file, "size " HOST_SIZE_T_PRINT_DEC ", " HOST_SIZE_T_PRINT_DEC
530 : " elements, %f collision/search ratio\n",
531 8 : (fmt_size_t) expr_table->size (),
532 8 : (fmt_size_t) expr_table->elements (),
533 : expr_table->collisions ());
534 8 : if (!expr_table->is_empty ())
535 : {
536 8 : fprintf (file, "\n\ntable entries:\n");
537 28 : expr_table->traverse <FILE *, dump_expr_hash_table_entry> (file);
538 : }
539 8 : fprintf (file, "\n");
540 8 : }
541 :
542 : /* Return true if register X is recorded as being set by an instruction
543 : whose CUID is greater than the one given. */
544 :
545 : static bool
546 2086241 : reg_changed_after_insn_p (rtx x, int cuid)
547 : {
548 2086241 : unsigned int regno, end_regno;
549 :
550 2086241 : regno = REGNO (x);
551 2086241 : end_regno = END_REGNO (x);
552 2086973 : do
553 2086973 : if (reg_avail_info[regno] > cuid)
554 : return true;
555 1653962 : while (++regno < end_regno);
556 : return false;
557 : }
558 :
559 : /* Return nonzero if the operands of expression X are unchanged
560 : 1) from the start of INSN's basic block up to but not including INSN
561 : if AFTER_INSN is false, or
562 : 2) from INSN to the end of INSN's basic block if AFTER_INSN is true. */
563 :
564 : static bool
565 3726656 : oprs_unchanged_p (rtx x, rtx_insn *insn, bool after_insn)
566 : {
567 4659227 : int i, j;
568 4659227 : enum rtx_code code;
569 4659227 : const char *fmt;
570 :
571 4659227 : if (x == 0)
572 : return true;
573 :
574 4659227 : code = GET_CODE (x);
575 4659227 : switch (code)
576 : {
577 1196231 : case REG:
578 : /* We are called after register allocation. */
579 1196231 : gcc_assert (REGNO (x) < FIRST_PSEUDO_REGISTER);
580 1196231 : if (after_insn)
581 788567 : return !reg_changed_after_insn_p (x, INSN_CUID (insn) - 1);
582 : else
583 407664 : return !reg_changed_after_insn_p (x, 0);
584 :
585 1285808 : case MEM:
586 1285808 : if (load_killed_in_block_p (INSN_CUID (insn), x, after_insn))
587 : return false;
588 : else
589 932571 : return oprs_unchanged_p (XEXP (x, 0), insn, after_insn);
590 :
591 : case PC:
592 : case CONST:
593 : CASE_CONST_ANY:
594 : case SYMBOL_REF:
595 : case LABEL_REF:
596 : case ADDR_VEC:
597 : case ADDR_DIFF_VEC:
598 : return true;
599 :
600 0 : case PRE_DEC:
601 0 : case PRE_INC:
602 0 : case POST_DEC:
603 0 : case POST_INC:
604 0 : case PRE_MODIFY:
605 0 : case POST_MODIFY:
606 0 : if (after_insn)
607 : return false;
608 : break;
609 :
610 : default:
611 : break;
612 : }
613 :
614 2977594 : for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
615 : {
616 2121298 : if (fmt[i] == 'e')
617 : {
618 2121298 : if (! oprs_unchanged_p (XEXP (x, i), insn, after_insn))
619 : return false;
620 : }
621 0 : else if (fmt[i] == 'E')
622 0 : for (j = 0; j < XVECLEN (x, i); j++)
623 0 : if (! oprs_unchanged_p (XVECEXP (x, i, j), insn, after_insn))
624 : return false;
625 : }
626 :
627 : return true;
628 : }
629 :
630 :
631 : /* Used for communication between find_mem_conflicts and
632 : load_killed_in_block_p. Nonzero if find_mem_conflicts finds a
633 : conflict between two memory references.
634 : This is a bit of a hack to work around the limitations of note_stores. */
635 : static int mems_conflict_p;
636 :
637 : /* DEST is the output of an instruction. If it is a memory reference, and
638 : possibly conflicts with the load found in DATA, then set mems_conflict_p
639 : to a nonzero value. */
640 :
641 : static void
642 7456251 : find_mem_conflicts (rtx dest, const_rtx setter ATTRIBUTE_UNUSED,
643 : void *data)
644 : {
645 7456251 : rtx mem_op = (rtx) data;
646 :
647 7456251 : while (GET_CODE (dest) == SUBREG
648 7456251 : || GET_CODE (dest) == ZERO_EXTRACT
649 14912502 : || GET_CODE (dest) == STRICT_LOW_PART)
650 0 : dest = XEXP (dest, 0);
651 :
652 : /* If DEST is not a MEM, then it will not conflict with the load. Note
653 : that function calls are assumed to clobber memory, but are handled
654 : elsewhere. */
655 7456251 : if (! MEM_P (dest))
656 : return;
657 :
658 7408882 : if (true_dependence (dest, GET_MODE (dest), mem_op))
659 179406 : mems_conflict_p = 1;
660 : }
661 :
662 :
663 : /* Return nonzero if the expression in X (a memory reference) is killed
664 : in the current basic block before (if AFTER_INSN is false) or after
665 : (if AFTER_INSN is true) the insn with the CUID in UID_LIMIT.
666 :
667 : This function assumes that the modifies_mem table is flushed when
668 : the hash table construction or redundancy elimination phases start
669 : processing a new basic block. */
670 :
671 : static bool
672 1892607 : load_killed_in_block_p (int uid_limit, rtx x, bool after_insn)
673 : {
674 1892607 : struct modifies_mem *list_entry = modifies_mem_list;
675 :
676 16650353 : while (list_entry)
677 : {
678 15398232 : rtx_insn *setter = list_entry->insn;
679 :
680 : /* Ignore entries in the list that do not apply. */
681 22926586 : if ((after_insn
682 13908437 : && INSN_CUID (setter) < uid_limit)
683 15398232 : || (! after_insn
684 1489795 : && INSN_CUID (setter) > uid_limit))
685 : {
686 7528354 : list_entry = list_entry->next;
687 7528354 : continue;
688 : }
689 :
690 : /* If SETTER is a call everything is clobbered. Note that calls
691 : to pure functions are never put on the list, so we need not
692 : worry about them. */
693 7869878 : if (CALL_P (setter))
694 : return true;
695 :
696 : /* SETTER must be an insn of some kind that sets memory. Call
697 : note_stores to examine each hunk of memory that is modified.
698 : It will set mems_conflict_p to nonzero if there may be a
699 : conflict between X and SETTER. */
700 7408798 : mems_conflict_p = 0;
701 7408798 : note_stores (setter, find_mem_conflicts, x);
702 7408798 : if (mems_conflict_p)
703 : return true;
704 :
705 7229392 : list_entry = list_entry->next;
706 : }
707 : return false;
708 : }
709 :
710 :
711 : /* Record register first/last/block set information for REGNO in INSN. */
712 :
713 : static inline void
714 8943962 : record_last_reg_set_info (rtx_insn *insn, rtx reg)
715 : {
716 8943962 : unsigned int regno, end_regno;
717 :
718 8943962 : regno = REGNO (reg);
719 8943962 : end_regno = END_REGNO (reg);
720 8968438 : do
721 : {
722 8968438 : reg_avail_info[regno] = INSN_CUID (insn);
723 8968438 : SET_HARD_REG_BIT (regs_set_in_block, regno);
724 : }
725 8968438 : while (++regno < end_regno);
726 8943962 : }
727 :
728 : static inline void
729 44170923 : record_last_reg_set_info_regno (rtx_insn *insn, int regno)
730 : {
731 44170923 : reg_avail_info[regno] = INSN_CUID (insn);
732 44170923 : SET_HARD_REG_BIT (regs_set_in_block, regno);
733 44170923 : }
734 :
735 :
736 : /* Record memory modification information for INSN. We do not actually care
737 : about the memory location(s) that are set, or even how they are set (consider
738 : a CALL_INSN). We merely need to record which insns modify memory. */
739 :
740 : static void
741 2007128 : record_last_mem_set_info (rtx_insn *insn)
742 : {
743 2007128 : struct modifies_mem *list_entry;
744 :
745 2007128 : list_entry = (struct modifies_mem *) obstack_alloc (&modifies_mem_obstack,
746 : sizeof (struct modifies_mem));
747 2007128 : list_entry->insn = insn;
748 2007128 : list_entry->next = modifies_mem_list;
749 2007128 : modifies_mem_list = list_entry;
750 :
751 2007128 : record_last_mem_set_info_common (insn, modify_mem_list,
752 : canon_modify_mem_list,
753 : modify_mem_list_set,
754 : blocks_with_calls);
755 2007128 : }
756 :
757 : /* Called from compute_hash_table via note_stores to handle one
758 : SET or CLOBBER in an insn. DATA is really the instruction in which
759 : the SET is taking place. */
760 :
761 : static void
762 12114099 : record_last_set_info (rtx dest, const_rtx setter ATTRIBUTE_UNUSED, void *data)
763 : {
764 12114099 : rtx_insn *last_set_insn = (rtx_insn *) data;
765 :
766 12114099 : if (GET_CODE (dest) == SUBREG)
767 785 : dest = SUBREG_REG (dest);
768 :
769 12114099 : if (REG_P (dest))
770 8943962 : record_last_reg_set_info (last_set_insn, dest);
771 3170137 : else if (MEM_P (dest))
772 : {
773 : /* Ignore pushes, they don't clobber memory. They may still
774 : clobber the stack pointer though. Some targets do argument
775 : pushes without adding REG_INC notes. See e.g. PR25196,
776 : where a pushsi2 on i386 doesn't have REG_INC notes. Note
777 : such changes here too. */
778 1567528 : if (! push_operand (dest, GET_MODE (dest)))
779 1531523 : record_last_mem_set_info (last_set_insn);
780 : else
781 36005 : record_last_reg_set_info_regno (last_set_insn, STACK_POINTER_REGNUM);
782 : }
783 12114099 : }
784 :
785 :
786 : /* Reset tables used to keep track of what's still available since the
787 : start of the block. */
788 :
789 : static void
790 2101602 : reset_opr_set_tables (void)
791 : {
792 2101602 : memset (reg_avail_info, 0, FIRST_PSEUDO_REGISTER * sizeof (int));
793 2101602 : CLEAR_HARD_REG_SET (regs_set_in_block);
794 2101602 : obstack_free (&modifies_mem_obstack, modifies_mem_obstack_bottom);
795 2101602 : modifies_mem_list = NULL;
796 2101602 : }
797 :
798 :
799 : /* Record things set by INSN.
800 : This data is used by oprs_unchanged_p. */
801 :
802 : static void
803 12362671 : record_opr_changes (rtx_insn *insn)
804 : {
805 12362671 : rtx note;
806 :
807 : /* Find all stores and record them. */
808 12362671 : note_stores (insn, record_last_set_info, insn);
809 :
810 : /* Also record autoincremented REGs for this insn as changed. */
811 15663629 : for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
812 3300958 : if (REG_NOTE_KIND (note) == REG_INC)
813 0 : record_last_reg_set_info (insn, XEXP (note, 0));
814 :
815 : /* Finally, if this is a call, record all call clobbers. */
816 12362671 : if (auto call = dyn_cast<rtx_call_insn *> (insn))
817 : {
818 534233 : unsigned int regno;
819 534233 : hard_reg_set_iterator hrsi;
820 : /* We don't track modes of hard registers, so we need to be
821 : conservative and assume that partial kills are full kills. */
822 534233 : HARD_REG_SET callee_clobbers
823 534233 : = insn_callee_abi (call).full_and_partial_reg_clobbers ();
824 44669151 : EXECUTE_IF_SET_IN_HARD_REG_SET (callee_clobbers, 0, regno, hrsi)
825 44134918 : record_last_reg_set_info_regno (call, regno);
826 :
827 1036935 : if (! RTL_CONST_OR_PURE_CALL_P (call)
828 69303 : || RTL_LOOPING_CONST_OR_PURE_CALL_P (call)
829 593003 : || can_throw_external (call))
830 475605 : record_last_mem_set_info (call);
831 : }
832 12362671 : }
833 :
834 :
835 : /* Scan the pattern of INSN and add an entry to the hash TABLE.
836 : After reload we are interested in loads/stores only. */
837 :
838 : static void
839 5147742 : hash_scan_set (rtx_insn *insn)
840 : {
841 5147742 : rtx pat = PATTERN (insn);
842 5147742 : rtx src = SET_SRC (pat);
843 5147742 : rtx dest = SET_DEST (pat);
844 :
845 : /* We are only interested in loads and stores. */
846 5147742 : if (! MEM_P (src) && ! MEM_P (dest))
847 : return;
848 :
849 : /* Don't mess with jumps and nops. */
850 1639103 : if (JUMP_P (insn) || set_noop_p (pat))
851 : return;
852 :
853 1639070 : if (REG_P (dest))
854 : {
855 748352 : if (/* Don't CSE something if we can't do a reg/reg copy. */
856 748352 : can_copy_p (GET_MODE (dest))
857 : /* Is SET_SRC something we want to gcse? */
858 748352 : && general_operand (src, GET_MODE (src))
859 : #ifdef STACK_REGS
860 : /* Never consider insns touching the register stack. It may
861 : create situations that reg-stack cannot handle (e.g. a stack
862 : register live across an abnormal edge). */
863 748348 : && (REGNO (dest) < FIRST_STACK_REG || REGNO (dest) > LAST_STACK_REG)
864 : #endif
865 : /* An expression is not available if its operands are
866 : subsequently modified, including this insn. */
867 1477768 : && oprs_unchanged_p (src, insn, true))
868 : {
869 420459 : insert_expr_in_table (src, insn);
870 : }
871 : }
872 890718 : else if (REG_P (src))
873 : {
874 : /* Only record sets of pseudo-regs in the hash table. */
875 620725 : if (/* Don't CSE something if we can't do a reg/reg copy. */
876 620725 : can_copy_p (GET_MODE (src))
877 : /* Is SET_DEST something we want to gcse? */
878 620725 : && general_operand (dest, GET_MODE (dest))
879 : #ifdef STACK_REGS
880 : /* As above for STACK_REGS. */
881 613016 : && (REGNO (src) < FIRST_STACK_REG || REGNO (src) > LAST_STACK_REG)
882 : #endif
883 607131 : && ! (flag_float_store && FLOAT_MODE_P (GET_MODE (dest)))
884 : /* Check if the memory expression is killed after insn. */
885 606799 : && ! load_killed_in_block_p (INSN_CUID (insn) + 1, dest, true)
886 940275 : && oprs_unchanged_p (XEXP (dest, 0), insn, true))
887 : {
888 254203 : insert_expr_in_table (dest, insn);
889 : }
890 : }
891 : }
892 :
893 :
894 : /* Create hash table of memory expressions available at end of basic
895 : blocks. Basically you should think of this hash table as the
896 : representation of AVAIL_OUT. This is the set of expressions that
897 : is generated in a basic block and not killed before the end of the
898 : same basic block. Notice that this is really a local computation.
899 : Also record in MODIFIED_REGS the hard registers that each block sets. */
900 :
901 : static void
902 94969 : compute_hash_table (void)
903 : {
904 94969 : basic_block bb;
905 :
906 1279819 : FOR_EACH_BB_FN (bb, cfun)
907 : {
908 1184850 : rtx_insn *insn;
909 :
910 : /* First pass over the instructions records information used to
911 : determine when registers and memory are last set.
912 : Since we compute a "local" AVAIL_OUT, reset the tables that
913 : help us keep track of what has been modified since the start
914 : of the block. */
915 1184850 : reset_opr_set_tables ();
916 10888272 : FOR_BB_INSNS (bb, insn)
917 : {
918 9703422 : if (INSN_P (insn))
919 7231854 : record_opr_changes (insn);
920 : }
921 :
922 1184850 : modified_regs[bb->index] = regs_set_in_block;
923 :
924 : /* The next pass actually builds the hash table. */
925 10888272 : FOR_BB_INSNS (bb, insn)
926 9703422 : if (INSN_P (insn) && GET_CODE (PATTERN (insn)) == SET)
927 5147742 : hash_scan_set (insn);
928 : }
929 94969 : }
930 :
931 :
932 : /* Check if register REG is killed in any insn waiting to be inserted on
933 : edge E. This function is required to check that our data flow analysis
934 : is still valid prior to commit_edge_insertions. */
935 :
936 : static bool
937 24859 : reg_killed_on_edge (rtx reg, edge e)
938 : {
939 24859 : rtx_insn *insn;
940 :
941 24880 : for (insn = e->insns.r; insn; insn = NEXT_INSN (insn))
942 29 : if (INSN_P (insn) && reg_set_p (reg, insn))
943 : return true;
944 :
945 : return false;
946 : }
947 :
948 : /* Similar to above - check if register REG is used in any insn waiting
949 : to be inserted on edge E.
950 : Assumes no such insn can be a CALL_INSN; if so call reg_used_between_p
951 : with PREV(insn),NEXT(insn) instead of calling reg_overlap_mentioned_p. */
952 :
953 : static bool
954 13531 : reg_used_on_edge (rtx reg, edge e)
955 : {
956 13531 : rtx_insn *insn;
957 :
958 13543 : for (insn = e->insns.r; insn; insn = NEXT_INSN (insn))
959 13 : if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
960 : return true;
961 :
962 : return false;
963 : }
964 :
965 : /* Return the loaded/stored register of a load/store instruction. */
966 :
967 : static rtx
968 1574906 : get_avail_load_store_reg (rtx_insn *insn)
969 : {
970 1574906 : if (REG_P (SET_DEST (PATTERN (insn))))
971 : /* A load. */
972 : return SET_DEST (PATTERN (insn));
973 : else
974 : {
975 : /* A store. */
976 602037 : gcc_assert (REG_P (SET_SRC (PATTERN (insn))));
977 : return SET_SRC (PATTERN (insn));
978 : }
979 : }
980 :
981 : /* Return true if the predecessors of BB are "well behaved". */
982 :
983 : static bool
984 986619 : bb_has_well_behaved_predecessors (basic_block bb)
985 : {
986 986619 : edge pred;
987 986619 : edge_iterator ei;
988 :
989 986619 : if (EDGE_COUNT (bb->preds) == 0)
990 : return false;
991 :
992 2520408 : FOR_EACH_EDGE (pred, ei, bb->preds)
993 : {
994 : /* commit_one_edge_insertion refuses to insert on abnormal edges even if
995 : the source has only one successor so EDGE_CRITICAL_P is too weak. */
996 1535461 : if ((pred->flags & EDGE_ABNORMAL) && !single_pred_p (pred->dest))
997 : return false;
998 :
999 1534988 : if ((pred->flags & EDGE_ABNORMAL_CALL) && cfun->has_nonlocal_label)
1000 : return false;
1001 :
1002 1534928 : if (tablejump_p (BB_END (pred->src), NULL, NULL))
1003 : return false;
1004 : }
1005 : return true;
1006 : }
1007 :
1008 :
1009 : /* Search for the occurrences of expression in BB. */
1010 :
1011 : static struct occr*
1012 1138920 : get_bb_avail_insn (basic_block bb, struct occr *orig_occr, int bitmap_index)
1013 : {
1014 1138920 : struct occr *occr = orig_occr;
1015 :
1016 8989163 : for (; occr != NULL; occr = occr->next)
1017 7871695 : if (BLOCK_FOR_INSN (occr->insn) == bb)
1018 : return occr;
1019 :
1020 : /* If we could not find an occurrence in BB, see if BB
1021 : has a single predecessor with an occurrence that is
1022 : transparent through BB. */
1023 1117468 : if (transp
1024 1098321 : && single_pred_p (bb)
1025 919801 : && bitmap_bit_p (transp[bb->index], bitmap_index)
1026 1948638 : && (occr = get_bb_avail_insn (single_pred (bb), orig_occr, bitmap_index)))
1027 : {
1028 18723 : rtx avail_reg = get_avail_load_store_reg (occr->insn);
1029 37446 : if (!reg_set_between_p (avail_reg,
1030 18723 : PREV_INSN (BB_HEAD (bb)),
1031 18723 : NEXT_INSN (BB_END (bb)))
1032 18723 : && !reg_killed_on_edge (avail_reg, single_pred_edge (bb)))
1033 11320 : return occr;
1034 : }
1035 :
1036 : return NULL;
1037 : }
1038 :
1039 :
1040 : /* This helper is called via htab_traverse. */
1041 : int
1042 399620 : compute_expr_transp (expr **slot, FILE *dump_file ATTRIBUTE_UNUSED)
1043 : {
1044 399620 : struct expr *expr = *slot;
1045 :
1046 399620 : compute_transp (expr->expr, expr->bitmap_index, transp,
1047 : blocks_with_calls, modify_mem_list_set,
1048 : canon_modify_mem_list);
1049 399620 : return 1;
1050 : }
1051 :
1052 : /* Return the hash table entry for the memory that INSN loads, if INSN is
1053 : a load whose memory operands are unchanged since the start of its block.
1054 : Return null otherwise. */
1055 :
1056 : static struct expr *
1057 6982812 : lookup_load_in_table (rtx_insn *insn)
1058 : {
1059 : /* Is it a load - of the form (set (reg) (mem))? */
1060 6982812 : if (!NONJUMP_INSN_P (insn)
1061 3728705 : || GET_CODE (PATTERN (insn)) != SET
1062 3021467 : || !REG_P (SET_DEST (PATTERN (insn)))
1063 9383023 : || !MEM_P (SET_SRC (PATTERN (insn))))
1064 : return NULL;
1065 :
1066 592839 : rtx src = SET_SRC (PATTERN (insn));
1067 592839 : if (MEM_VOLATILE_P (src)
1068 556393 : || GET_MODE (src) == BLKmode
1069 556393 : || !general_operand (src, GET_MODE (src))
1070 : /* Are the operands unchanged since the start of the block? */
1071 556392 : || !oprs_unchanged_p (src, insn, false)
1072 367594 : || (cfun->can_throw_non_call_exceptions && may_trap_p (src))
1073 959967 : || side_effects_p (src))
1074 : return NULL;
1075 :
1076 : /* Is the expression recorded? */
1077 367128 : return lookup_expr_in_table (src);
1078 : }
1079 :
1080 : /* Return the index of the availability fact for EXPR held in REG, adding
1081 : REG to FACT_REGS if necessary. */
1082 :
1083 : static unsigned int
1084 898548 : get_fact (struct expr *expr, rtx reg)
1085 : {
1086 898548 : unsigned int i = expr->first_fact;
1087 1881563 : while (i < fact_regs.length () && REGNO (fact_regs[i]) != REGNO (reg))
1088 209250 : i++;
1089 1762249 : if (i == fact_regs.length ())
1090 334033 : fact_regs.safe_push (reg);
1091 898548 : return i;
1092 : }
1093 :
1094 : /* Compute FACT_AVIN, where each fact is a pair of an expression and a hard
1095 : register that holds its value. The available occurrences whose register
1096 : survives to the end of their block generate the facts. A block kills a
1097 : fact if the expression is not transparent through it or if it sets the
1098 : register.
1099 :
1100 : The facts describe the instructions before the redundancy elimination.
1101 : For each load that eliminate_partially_redundant_load removes, it adds
1102 : instructions only to the incoming edges of the load's block. They only
1103 : set the destination of the load, which the block does not use or set
1104 : before the load. The facts at the start of a block therefore remain
1105 : valid for the loads that the elimination processes later. */
1106 :
1107 : static void
1108 38260 : compute_fact_availability (void)
1109 : {
1110 : /* Number the distinct holding registers of each expression. */
1111 38260 : unsigned int n_exprs = expr_table->elements ();
1112 38260 : auto_vec<struct expr *> exprs;
1113 38260 : exprs.safe_grow_cleared (n_exprs);
1114 437880 : for (struct expr *expr : *expr_table)
1115 : {
1116 399620 : exprs[expr->bitmap_index] = expr;
1117 399620 : expr->first_fact = fact_regs.length ();
1118 1030037 : for (struct occr *occr = expr->avail_occr; occr; occr = occr->next)
1119 630417 : if (occr->reg_available_p)
1120 449274 : get_fact (expr, get_avail_load_store_reg (occr->insn));
1121 776859 : expr->num_facts = fact_regs.length () - expr->first_fact;
1122 : }
1123 :
1124 38260 : unsigned int n_facts = fact_regs.length ();
1125 34847 : if (n_facts == 0)
1126 : return;
1127 :
1128 : /* Use the GCSE memory limit for the bitmap vectors of the problem, four
1129 : indexed by block and one indexed by hard register. */
1130 34847 : int n_blocks = last_basic_block_for_fn (cfun);
1131 34847 : unsigned HOST_WIDE_INT memory_request
1132 34847 : = ((4 * (unsigned HOST_WIDE_INT) n_blocks + FIRST_PSEUDO_REGISTER)
1133 34847 : * SBITMAP_SET_SIZE (n_facts) * sizeof (SBITMAP_ELT_TYPE));
1134 34847 : if (memory_request / 1024 > (unsigned HOST_WIDE_INT) param_max_gcse_memory)
1135 : {
1136 0 : if (dump_file)
1137 0 : fprintf (dump_file, "not computing the availability of %u values in "
1138 : "registers: needs " HOST_WIDE_INT_PRINT_UNSIGNED "kB, more "
1139 : "than max-gcse-memory\n", n_facts, memory_request / 1024);
1140 : return;
1141 : }
1142 :
1143 34847 : sbitmap *avloc = sbitmap_vector_alloc (n_blocks, n_facts);
1144 34847 : sbitmap *kill = sbitmap_vector_alloc (n_blocks, n_facts);
1145 34847 : sbitmap *avout = sbitmap_vector_alloc (n_blocks, n_facts);
1146 34847 : fact_avin = sbitmap_vector_alloc (n_blocks, n_facts);
1147 34847 : bitmap_vector_clear (avloc, n_blocks);
1148 34847 : bitmap_vector_clear (kill, n_blocks);
1149 :
1150 : /* Record the facts held in each hard register. */
1151 34847 : sbitmap *reg_facts = sbitmap_vector_alloc (FIRST_PSEUDO_REGISTER, n_facts);
1152 34847 : bitmap_vector_clear (reg_facts, FIRST_PSEUDO_REGISTER);
1153 34847 : HARD_REG_SET fact_hard_regs;
1154 34847 : CLEAR_HARD_REG_SET (fact_hard_regs);
1155 368880 : for (unsigned int i = 0; i < n_facts; i++)
1156 668359 : for (unsigned int regno = REGNO (fact_regs[i]);
1157 668359 : regno < END_REGNO (fact_regs[i]); regno++)
1158 : {
1159 334326 : bitmap_set_bit (reg_facts[regno], i);
1160 334326 : SET_HARD_REG_BIT (fact_hard_regs, regno);
1161 : }
1162 :
1163 34847 : auto_sbitmap has_facts (n_exprs);
1164 34847 : bitmap_clear (has_facts);
1165 426567 : for (struct expr *expr : *expr_table)
1166 : {
1167 391720 : if (expr->num_facts)
1168 289139 : bitmap_set_bit (has_facts, expr->bitmap_index);
1169 1009661 : for (struct occr *occr = expr->avail_occr; occr; occr = occr->next)
1170 617941 : if (occr->reg_available_p)
1171 : {
1172 449274 : unsigned int i = get_fact (expr,
1173 : get_avail_load_store_reg (occr->insn));
1174 449274 : gcc_checking_assert (i < expr->first_fact + expr->num_facts);
1175 449274 : bitmap_set_bit (avloc[BLOCK_FOR_INSN (occr->insn)->index], i);
1176 : }
1177 : }
1178 :
1179 : /* Registers need not keep their values on abnormal edges, for example
1180 : on those to a setjmp receiver. */
1181 34847 : auto_sbitmap opaque (n_exprs);
1182 34847 : basic_block bb;
1183 1039368 : FOR_EACH_BB_FN (bb, cfun)
1184 : {
1185 1004521 : sbitmap bb_kill = kill[bb->index];
1186 1004521 : if (bb_has_abnormal_pred (bb))
1187 : {
1188 1529 : bitmap_ones (bb_kill);
1189 1529 : continue;
1190 : }
1191 :
1192 1002992 : unsigned int i;
1193 1002992 : sbitmap_iterator sbi;
1194 1002992 : bitmap_and_compl (opaque, has_facts, transp[bb->index]);
1195 15027146 : EXECUTE_IF_SET_IN_BITMAP (opaque, 0, i, sbi)
1196 13021162 : bitmap_set_range (bb_kill, exprs[i]->first_fact, exprs[i]->num_facts);
1197 :
1198 1002992 : hard_reg_set_iterator hrsi;
1199 1002992 : HARD_REG_SET regs = modified_regs[bb->index] & fact_hard_regs;
1200 2760567 : EXECUTE_IF_SET_IN_HARD_REG_SET (regs, 0, i, hrsi)
1201 1757575 : bitmap_ior (bb_kill, bb_kill, reg_facts[i]);
1202 : }
1203 :
1204 34847 : compute_available (avloc, kill, avout, fact_avin);
1205 34847 : sbitmap_vector_free (reg_facts);
1206 34847 : sbitmap_vector_free (avloc);
1207 34847 : sbitmap_vector_free (kill);
1208 34847 : sbitmap_vector_free (avout);
1209 38260 : }
1210 :
1211 : /* Try to replace the source of INSN, a load in BB of the memory in EXPR,
1212 : with a register that holds the same value. AVIN contains the facts that
1213 : are available at the start of BB. The register must not have changed
1214 : since the start of BB. Keep INSN and its destination, so that the
1215 : occurrences of EXPR remain valid. Return true on success. */
1216 :
1217 : static bool
1218 265702 : replace_fully_redundant_load (basic_block bb, rtx_insn *insn,
1219 : struct expr *expr, sbitmap avin)
1220 : {
1221 265702 : rtx set = PATTERN (insn);
1222 633316 : for (unsigned int i = expr->first_fact;
1223 633316 : i < expr->first_fact + expr->num_facts; i++)
1224 : {
1225 372033 : rtx reg = fact_regs[i];
1226 372033 : if (!bitmap_bit_p (avin, i) || reg_changed_after_insn_p (reg, 0))
1227 367610 : continue;
1228 :
1229 : /* In blocks optimized for size, require a copy that is no larger
1230 : than the load. */
1231 4423 : if (optimize_bb_for_size_p (bb)
1232 4423 : && (set_rtx_cost (gen_rtx_SET (SET_DEST (set), reg), false)
1233 660 : > set_rtx_cost (set, false)))
1234 0 : continue;
1235 :
1236 4423 : if (validate_change (insn, &SET_SRC (set), copy_rtx (reg), 0))
1237 : {
1238 4419 : stats.loads_forwarded++;
1239 4419 : if (dump_file)
1240 : {
1241 0 : fprintf (dump_file, "forwarding load from available register: ");
1242 0 : print_rtl_single (dump_file, insn);
1243 : }
1244 : return true;
1245 : }
1246 : }
1247 : return false;
1248 : }
1249 :
1250 : /* This handles the case where several stores feed a partially redundant
1251 : load. It checks if the redundancy elimination is possible and if it's
1252 : worth it.
1253 :
1254 : Redundancy elimination is possible if,
1255 : 1) None of the operands of an insn have been modified since the start
1256 : of the current basic block.
1257 : 2) In any predecessor of the current basic block, the same expression
1258 : is generated.
1259 :
1260 : See the function body for the heuristics that determine if eliminating
1261 : a redundancy is also worth doing, assuming it is possible. */
1262 :
1263 : static void
1264 239764 : eliminate_partially_redundant_load (basic_block bb, rtx_insn *insn,
1265 : struct expr *expr)
1266 : {
1267 239764 : edge pred;
1268 239764 : rtx_insn *avail_insn = NULL;
1269 239764 : rtx avail_reg;
1270 239764 : rtx dest, pat;
1271 239764 : struct occr *a_occr;
1272 239764 : struct unoccr *occr, *avail_occrs = NULL;
1273 239764 : struct unoccr *unoccr, *unavail_occrs = NULL, *rollback_unoccr = NULL;
1274 239764 : int npred_ok = 0;
1275 239764 : profile_count ok_count = profile_count::zero ();
1276 : /* Redundant load execution count. */
1277 239764 : profile_count critical_count = profile_count::zero ();
1278 : /* Execution count of critical edges. */
1279 239764 : edge_iterator ei;
1280 239764 : bool critical_edge_split = false;
1281 :
1282 : /* The execution count of the loads to be added to make the
1283 : load fully redundant. */
1284 239764 : profile_count not_ok_count = profile_count::zero ();
1285 239764 : basic_block pred_bb;
1286 :
1287 239764 : pat = PATTERN (insn);
1288 239764 : dest = SET_DEST (pat);
1289 :
1290 : /* Check that the loaded register is not used, set, or killed from the
1291 : beginning of the block. */
1292 239764 : if (reg_changed_after_insn_p (dest, 0)
1293 239764 : || reg_used_between_p (dest, PREV_INSN (BB_HEAD (bb)), insn))
1294 34539 : return;
1295 :
1296 : /* Check potential for replacing load with copy for predecessors. */
1297 504091 : FOR_EACH_EDGE (pred, ei, bb->preds)
1298 : {
1299 298866 : rtx_insn *next_pred_bb_end;
1300 :
1301 298866 : avail_insn = NULL;
1302 298866 : avail_reg = NULL_RTX;
1303 298866 : pred_bb = pred->src;
1304 307133 : for (a_occr = get_bb_avail_insn (pred_bb,
1305 : expr->avail_occr,
1306 298866 : expr->bitmap_index);
1307 307133 : a_occr;
1308 8267 : a_occr = get_bb_avail_insn (pred_bb,
1309 : a_occr->next,
1310 8267 : expr->bitmap_index))
1311 : {
1312 : /* Check if the loaded register is not used. */
1313 13543 : avail_insn = a_occr->insn;
1314 13543 : avail_reg = get_avail_load_store_reg (avail_insn);
1315 13543 : gcc_assert (avail_reg);
1316 :
1317 : /* Make sure we can generate a move from register avail_reg to
1318 : dest. */
1319 13543 : rtx_insn *move = gen_move_insn (copy_rtx (dest),
1320 : copy_rtx (avail_reg));
1321 13543 : extract_insn (move);
1322 13543 : if (! constrain_operands (1, get_preferred_alternatives (insn,
1323 : pred_bb))
1324 13539 : || reg_killed_on_edge (avail_reg, pred)
1325 27074 : || reg_used_on_edge (dest, pred))
1326 : {
1327 13 : avail_insn = NULL;
1328 13 : continue;
1329 : }
1330 13530 : next_pred_bb_end = NEXT_INSN (BB_END (BLOCK_FOR_INSN (avail_insn)));
1331 13530 : if (!reg_set_between_p (avail_reg, avail_insn, next_pred_bb_end))
1332 : /* AVAIL_INSN remains non-null. */
1333 : break;
1334 : else
1335 : avail_insn = NULL;
1336 : }
1337 :
1338 298866 : if (EDGE_CRITICAL_P (pred) && pred->count ().initialized_p ())
1339 65093 : critical_count += pred->count ();
1340 :
1341 298866 : if (avail_insn != NULL_RTX)
1342 : {
1343 5276 : npred_ok++;
1344 5276 : if (pred->count ().initialized_p ())
1345 5263 : ok_count = ok_count + pred->count ();
1346 5276 : if (! set_noop_p (PATTERN (gen_move_insn (copy_rtx (dest),
1347 : copy_rtx (avail_reg)))))
1348 : {
1349 : /* Check if there is going to be a split. */
1350 3037 : if (EDGE_CRITICAL_P (pred))
1351 : critical_edge_split = true;
1352 : }
1353 : else /* Its a dead move no need to generate. */
1354 2239 : continue;
1355 3037 : occr = (struct unoccr *) obstack_alloc (&unoccr_obstack,
1356 : sizeof (struct unoccr));
1357 3037 : occr->insn = avail_insn;
1358 3037 : occr->pred = pred;
1359 3037 : occr->next = avail_occrs;
1360 3037 : avail_occrs = occr;
1361 3037 : if (! rollback_unoccr)
1362 904 : rollback_unoccr = occr;
1363 : }
1364 : else
1365 : {
1366 : /* Adding a load on a critical edge will cause a split. */
1367 293590 : if (EDGE_CRITICAL_P (pred))
1368 : critical_edge_split = true;
1369 293590 : if (pred->count ().initialized_p ())
1370 293450 : not_ok_count = not_ok_count + pred->count ();
1371 293590 : unoccr = (struct unoccr *) obstack_alloc (&unoccr_obstack,
1372 : sizeof (struct unoccr));
1373 293590 : unoccr->insn = NULL;
1374 293590 : unoccr->pred = pred;
1375 293590 : unoccr->next = unavail_occrs;
1376 293590 : unavail_occrs = unoccr;
1377 293590 : if (! rollback_unoccr)
1378 204321 : rollback_unoccr = unoccr;
1379 : }
1380 : }
1381 :
1382 205225 : if (/* No load can be replaced by copy. */
1383 : npred_ok == 0
1384 : /* Prevent exploding the code. */
1385 4434 : || (optimize_bb_for_size_p (bb) && npred_ok > 1)
1386 : /* If we don't have profile information we cannot tell if splitting
1387 : a critical edge is profitable or not so don't do it. */
1388 209659 : || ((!profile_info || profile_status_for_fn (cfun) != PROFILE_READ
1389 0 : || targetm.cannot_modify_jumps_p ())
1390 4434 : && critical_edge_split))
1391 202388 : goto cleanup;
1392 :
1393 : /* Check if it's worth applying the partial redundancy elimination. */
1394 2837 : if (ok_count.to_gcov_type ()
1395 2837 : < param_gcse_after_reload_partial_fraction * not_ok_count.to_gcov_type ())
1396 2095 : goto cleanup;
1397 :
1398 742 : gcov_type threshold;
1399 : #if (GCC_VERSION >= 5000)
1400 742 : if (__builtin_mul_overflow (param_gcse_after_reload_critical_fraction,
1401 : critical_count.to_gcov_type (), &threshold))
1402 0 : threshold = profile_count::max_count;
1403 : #else
1404 : threshold
1405 : = (param_gcse_after_reload_critical_fraction
1406 : * critical_count.to_gcov_type ());
1407 : #endif
1408 :
1409 742 : if (ok_count.to_gcov_type () < threshold)
1410 130 : goto cleanup;
1411 :
1412 : /* Generate moves to the loaded register from where
1413 : the memory is available. */
1414 1239 : for (occr = avail_occrs; occr; occr = occr->next)
1415 : {
1416 627 : avail_insn = occr->insn;
1417 627 : pred = occr->pred;
1418 : /* Set avail_reg to be the register having the value of the
1419 : memory. */
1420 627 : avail_reg = get_avail_load_store_reg (avail_insn);
1421 627 : gcc_assert (avail_reg);
1422 :
1423 627 : insert_insn_on_edge (gen_move_insn (copy_rtx (dest),
1424 : copy_rtx (avail_reg)),
1425 : pred);
1426 627 : stats.moves_inserted++;
1427 :
1428 627 : if (dump_file)
1429 0 : fprintf (dump_file,
1430 : "generating move from %d to %d on edge from %d to %d\n",
1431 : REGNO (avail_reg),
1432 : REGNO (dest),
1433 0 : pred->src->index,
1434 0 : pred->dest->index);
1435 : }
1436 :
1437 : /* Regenerate loads where the memory is unavailable. */
1438 966 : for (unoccr = unavail_occrs; unoccr; unoccr = unoccr->next)
1439 : {
1440 354 : pred = unoccr->pred;
1441 354 : insert_insn_on_edge (copy_insn (PATTERN (insn)), pred);
1442 354 : stats.copies_inserted++;
1443 :
1444 354 : if (dump_file)
1445 : {
1446 0 : fprintf (dump_file,
1447 : "generating on edge from %d to %d a copy of load: ",
1448 0 : pred->src->index,
1449 0 : pred->dest->index);
1450 0 : print_rtl (dump_file, PATTERN (insn));
1451 0 : fprintf (dump_file, "\n");
1452 : }
1453 : }
1454 :
1455 : /* Delete the insn if it is not available in this block and mark it
1456 : for deletion if it is available. If insn is available it may help
1457 : discover additional redundancies, so mark it for later deletion. */
1458 612 : for (a_occr = get_bb_avail_insn (bb, expr->avail_occr, expr->bitmap_index);
1459 617 : a_occr && (a_occr->insn != insn);
1460 5 : a_occr = get_bb_avail_insn (bb, a_occr->next, expr->bitmap_index))
1461 : ;
1462 :
1463 612 : if (!a_occr)
1464 : {
1465 111 : stats.insns_deleted++;
1466 :
1467 111 : if (dump_file)
1468 : {
1469 0 : fprintf (dump_file, "deleting insn:\n");
1470 0 : print_rtl_single (dump_file, insn);
1471 0 : fprintf (dump_file, "\n");
1472 : }
1473 111 : delete_insn (insn);
1474 : }
1475 : else
1476 501 : a_occr->deleted_p = 1;
1477 :
1478 205225 : cleanup:
1479 205225 : if (rollback_unoccr)
1480 205225 : obstack_free (&unoccr_obstack, rollback_unoccr);
1481 : }
1482 :
1483 : /* Performing the redundancy elimination as described before. */
1484 :
1485 : static void
1486 38260 : eliminate_partially_redundant_loads (void)
1487 : {
1488 38260 : rtx_insn *insn;
1489 38260 : basic_block bb;
1490 :
1491 : /* Note we start at block 1. */
1492 :
1493 38260 : if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1494 : return;
1495 :
1496 1024879 : FOR_BB_BETWEEN (bb,
1497 : ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->next_bb,
1498 : EXIT_BLOCK_PTR_FOR_FN (cfun),
1499 : next_bb)
1500 : {
1501 : /* Don't try anything on basic blocks with strange predecessors. */
1502 986619 : if (! bb_has_well_behaved_predecessors (bb))
1503 1672 : continue;
1504 :
1505 : /* Registers need not keep their values on abnormal edges, for
1506 : example on EH edges from instructions that can trap. */
1507 984947 : sbitmap avin = NULL;
1508 984947 : if (fact_avin && !bb_has_abnormal_pred (bb))
1509 967106 : avin = fact_avin[bb->index];
1510 :
1511 : /* In cold basic blocks, only try to replace loads with copies. */
1512 984947 : bool cold_p = optimize_bb_for_size_p (bb);
1513 984947 : if (cold_p && (!avin || bitmap_empty_p (avin)))
1514 68195 : continue;
1515 :
1516 : /* Reset the table of things changed since the start of the current
1517 : basic block. */
1518 916752 : reset_opr_set_tables ();
1519 :
1520 : /* Look at all insns in the current basic block and see if there are
1521 : any loads in it that we can record. */
1522 7899564 : FOR_BB_INSNS (bb, insn)
1523 : {
1524 : /* If INSN loads an expression that is available at the end of
1525 : some block, try to remove the load if it is redundant. */
1526 6982812 : if (struct expr *expr = lookup_load_in_table (insn))
1527 265702 : if (!(avin && replace_fully_redundant_load (bb, insn, expr, avin))
1528 532430 : && !cold_p)
1529 239764 : eliminate_partially_redundant_load (bb, insn, expr);
1530 :
1531 : /* Keep track of everything modified by this insn, so that we
1532 : know what has been modified since the start of the current
1533 : basic block. */
1534 6982812 : if (INSN_P (insn))
1535 5130817 : record_opr_changes (insn);
1536 : }
1537 : }
1538 :
1539 38260 : commit_edge_insertions ();
1540 : }
1541 :
1542 : /* Go over the expression hash table and delete insns that were
1543 : marked for later deletion. */
1544 :
1545 : /* This helper is called via htab_traverse. */
1546 : int
1547 399620 : delete_redundant_insns_1 (expr **slot, void *data ATTRIBUTE_UNUSED)
1548 : {
1549 399620 : struct expr *exprs = *slot;
1550 399620 : struct occr *occr;
1551 :
1552 1030037 : for (occr = exprs->avail_occr; occr != NULL; occr = occr->next)
1553 : {
1554 630417 : if (occr->deleted_p && dbg_cnt (gcse2_delete))
1555 : {
1556 501 : delete_insn (occr->insn);
1557 501 : stats.insns_deleted++;
1558 :
1559 501 : if (dump_file)
1560 : {
1561 0 : fprintf (dump_file, "deleting insn:\n");
1562 0 : print_rtl_single (dump_file, occr->insn);
1563 0 : fprintf (dump_file, "\n");
1564 : }
1565 : }
1566 : }
1567 :
1568 399620 : return 1;
1569 : }
1570 :
1571 : static void
1572 38260 : delete_redundant_insns (void)
1573 : {
1574 437880 : expr_table->traverse <void *, delete_redundant_insns_1> (NULL);
1575 38260 : if (dump_file)
1576 8 : fprintf (dump_file, "\n");
1577 38260 : }
1578 :
1579 : /* Main entry point of the GCSE after reload - clean some redundant loads
1580 : due to spilling. */
1581 :
1582 : static void
1583 94969 : gcse_after_reload_main (rtx_insn *f ATTRIBUTE_UNUSED)
1584 : {
1585 : /* compute_available requires all blocks to be reachable. */
1586 94969 : delete_unreachable_blocks ();
1587 :
1588 : /* Disable computing transparentness if it is too expensive. */
1589 94969 : bool do_transp
1590 94969 : = !gcse_or_cprop_is_too_expensive (_("using simple load CSE after register "
1591 94969 : "allocation"));
1592 :
1593 94969 : memset (&stats, 0, sizeof (stats));
1594 :
1595 : /* Allocate memory for this pass.
1596 : Also computes and initializes the insns' CUIDs. */
1597 94969 : alloc_mem ();
1598 :
1599 : /* We need alias analysis. */
1600 94969 : init_alias_analysis ();
1601 :
1602 94969 : compute_hash_table ();
1603 :
1604 94969 : if (dump_file)
1605 8 : dump_hash_table (dump_file);
1606 :
1607 94969 : if (!expr_table->is_empty ())
1608 : {
1609 : /* Knowing which MEMs are transparent through a block can significantly
1610 : increase the number of redundant loads found. So compute transparency
1611 : information for each memory expression in the hash table. */
1612 38260 : df_analyze ();
1613 38260 : if (do_transp)
1614 : {
1615 : /* This cannot be part of the normal allocation routine because
1616 : we have to know the number of elements in the hash table. */
1617 76520 : transp = sbitmap_vector_alloc (last_basic_block_for_fn (cfun),
1618 38260 : expr_table->elements ());
1619 38260 : bitmap_vector_ones (transp, last_basic_block_for_fn (cfun));
1620 437880 : expr_table->traverse <FILE *, compute_expr_transp> (dump_file);
1621 38260 : compute_fact_availability ();
1622 : }
1623 : else
1624 0 : transp = NULL;
1625 38260 : eliminate_partially_redundant_loads ();
1626 38260 : delete_redundant_insns ();
1627 38260 : if (do_transp)
1628 38260 : sbitmap_vector_free (transp);
1629 38260 : if (fact_avin)
1630 34847 : sbitmap_vector_free (fact_avin);
1631 38260 : fact_avin = NULL;
1632 38260 : fact_regs.release ();
1633 :
1634 38260 : if (dump_file)
1635 : {
1636 8 : fprintf (dump_file, "GCSE AFTER RELOAD stats:\n");
1637 8 : fprintf (dump_file, "loads forwarded: %d\n", stats.loads_forwarded);
1638 8 : fprintf (dump_file, "copies inserted: %d\n", stats.copies_inserted);
1639 8 : fprintf (dump_file, "moves inserted: %d\n", stats.moves_inserted);
1640 8 : fprintf (dump_file, "insns deleted: %d\n", stats.insns_deleted);
1641 8 : fprintf (dump_file, "\n\n");
1642 : }
1643 :
1644 38260 : statistics_counter_event (cfun, "loads forwarded",
1645 : stats.loads_forwarded);
1646 38260 : statistics_counter_event (cfun, "copies inserted",
1647 : stats.copies_inserted);
1648 38260 : statistics_counter_event (cfun, "moves inserted",
1649 : stats.moves_inserted);
1650 38260 : statistics_counter_event (cfun, "insns deleted",
1651 : stats.insns_deleted);
1652 : }
1653 :
1654 : /* We are finished with alias. */
1655 94969 : end_alias_analysis ();
1656 :
1657 94969 : free_mem ();
1658 94969 : }
1659 :
1660 :
1661 :
1662 : static void
1663 94969 : rest_of_handle_gcse2 (void)
1664 : {
1665 94969 : gcse_after_reload_main (get_insns ());
1666 94969 : rebuild_jump_labels (get_insns ());
1667 94969 : }
1668 :
1669 : namespace {
1670 :
1671 : const pass_data pass_data_gcse2 =
1672 : {
1673 : RTL_PASS, /* type */
1674 : "gcse2", /* name */
1675 : OPTGROUP_NONE, /* optinfo_flags */
1676 : TV_GCSE_AFTER_RELOAD, /* tv_id */
1677 : 0, /* properties_required */
1678 : 0, /* properties_provided */
1679 : 0, /* properties_destroyed */
1680 : 0, /* todo_flags_start */
1681 : 0, /* todo_flags_finish */
1682 : };
1683 :
1684 : class pass_gcse2 : public rtl_opt_pass
1685 : {
1686 : public:
1687 302985 : pass_gcse2 (gcc::context *ctxt)
1688 605970 : : rtl_opt_pass (pass_data_gcse2, ctxt)
1689 : {}
1690 :
1691 : /* opt_pass methods: */
1692 1531131 : bool gate (function *fun) final override
1693 : {
1694 1070229 : return (optimize > 0 && flag_gcse_after_reload
1695 1626587 : && optimize_function_for_speed_p (fun));
1696 : }
1697 :
1698 94969 : unsigned int execute (function *) final override
1699 : {
1700 94969 : rest_of_handle_gcse2 ();
1701 94969 : return 0;
1702 : }
1703 :
1704 : }; // class pass_gcse2
1705 :
1706 : } // anon namespace
1707 :
1708 : rtl_opt_pass *
1709 302985 : make_pass_gcse2 (gcc::context *ctxt)
1710 : {
1711 302985 : return new pass_gcse2 (ctxt);
1712 : }
|