Line data Source code
1 : /* RTL dead zero/sign extension (code) elimination.
2 : Copyright (C) 2000-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 "rtl.h"
25 : #include "tree.h"
26 : #include "memmodel.h"
27 : #include "insn-config.h"
28 : #include "emit-rtl.h"
29 : #include "expr.h"
30 : #include "recog.h"
31 : #include "cfganal.h"
32 : #include "tree-pass.h"
33 : #include "cfgrtl.h"
34 : #include "rtl-iter.h"
35 : #include "df.h"
36 : #include "print-rtl.h"
37 : #include "dbgcnt.h"
38 : #include "diagnostic-core.h"
39 : #include "target.h"
40 : #include "regs.h"
41 :
42 : /* These should probably move into a C++ class. */
43 : static vec<bitmap_head> livein;
44 : static bitmap all_blocks;
45 : static bitmap livenow;
46 : static bitmap changed_pseudos;
47 : static bool modify;
48 :
49 : /* Chain detection for promotion: we defer promotions and only apply them
50 : when they form chains (one candidate's result feeds another's operand).
51 : Standalone promotions are skipped as they cause regressions on targets
52 : with free sign extension (e.g., RISC-V W-suffix instructions). */
53 : struct promotion_candidate_info {
54 : rtx_insn *insn;
55 : rtx set;
56 : };
57 :
58 : static vec<promotion_candidate_info> promotion_candidates;
59 : static bitmap promotable_dests;
60 : static bitmap consumed_by_candidate;
61 :
62 : /* Copy pairs seen during the reverse scan (from optimized extensions).
63 : Used to propagate chain info transitively. */
64 : struct copy_info {
65 : unsigned int dest_regno;
66 : unsigned int src_regno;
67 : };
68 : static vec<copy_info> promotion_copies;
69 :
70 : /* We consider four bit groups for liveness:
71 : bit 0..7 (least significant byte)
72 : bit 8..15 (second least significant byte)
73 : bit 16..31
74 : bit 32..BITS_PER_WORD-1 */
75 :
76 : /* For the given REG, return the number of bit groups implied by the
77 : size of the REG's mode, up to a maximum of 4 (number of bit groups
78 : tracked by this pass).
79 :
80 : For partial integer and variable sized modes also return 4. This
81 : could possibly be refined for something like PSI mode, but it
82 : does not seem worth the effort. */
83 :
84 : static int
85 152700070 : group_limit (const_rtx reg)
86 : {
87 152700070 : machine_mode mode = GET_MODE (reg);
88 :
89 152700070 : if (!GET_MODE_BITSIZE (mode).is_constant ())
90 : return 4;
91 :
92 152700070 : int size = GET_MODE_SIZE (mode).to_constant ();
93 :
94 152700070 : size = exact_log2 (size);
95 :
96 152608380 : if (size < 0)
97 : return 4;
98 :
99 152608380 : size++;
100 152608380 : return (size > 4 ? 4 : size);
101 : }
102 :
103 : /* Make all bit groups live for REGNO in bitmap BMAP. For hard regs,
104 : we assume all groups are live. For a pseudo we consider the size
105 : of the pseudo to avoid creating unnecessarily live chunks of data. */
106 :
107 : static void
108 4794350 : make_reg_live (bitmap bmap, int regno)
109 : {
110 4794350 : int limit;
111 :
112 : /* For pseudos we can use the mode to limit how many bit groups
113 : are marked as live since a pseudo only has one mode. Hard
114 : registers have to be handled more conservatively. */
115 4794350 : if (regno > FIRST_PSEUDO_REGISTER)
116 : {
117 938289 : rtx reg = regno_reg_rtx[regno];
118 938289 : limit = group_limit (reg);
119 : }
120 : else
121 : limit = 4;
122 :
123 23630566 : for (int i = 0; i < limit; i++)
124 18836216 : bitmap_set_bit (bmap, regno * 4 + i);
125 4794350 : }
126 :
127 : /* Note this pass could be used to narrow memory loads too. It's
128 : not clear if that's profitable or not in general. */
129 :
130 : #define UNSPEC_P(X) (GET_CODE (X) == UNSPEC || GET_CODE (X) == UNSPEC_VOLATILE)
131 :
132 : /* If we know the destination of CODE only uses some low bits
133 : (say just the QI bits of an SI operation), then return true
134 : if we can propagate the need for just the subset of bits
135 : from the destination to the sources.
136 :
137 : FIXME: This is safe for operands 1 and 2 of an IF_THEN_ELSE, but not
138 : operand 0. Thus is likely would need some special casing to handle. */
139 :
140 : static bool
141 146585003 : safe_for_live_propagation (rtx_code code)
142 : {
143 : /* First handle rtx classes which as a whole are known to
144 : be either safe or unsafe. */
145 146585003 : switch (GET_RTX_CLASS (code))
146 : {
147 : case RTX_OBJ:
148 : case RTX_CONST_OBJ:
149 : return true;
150 :
151 : case RTX_COMPARE:
152 : case RTX_COMM_COMPARE:
153 : case RTX_TERNARY:
154 : return false;
155 :
156 75771279 : default:
157 75771279 : break;
158 : }
159 :
160 : /* What's left are specific codes. We only need to identify those
161 : which are safe. */
162 75771279 : switch (code)
163 : {
164 : /* These are trivially safe. */
165 : case SUBREG:
166 : case NOT:
167 : case ZERO_EXTEND:
168 : case SIGN_EXTEND:
169 : case TRUNCATE:
170 : case PLUS:
171 : case MINUS:
172 : case MULT:
173 : case SMUL_HIGHPART:
174 : case UMUL_HIGHPART:
175 : case AND:
176 : case IOR:
177 : case XOR:
178 : return true;
179 :
180 : /* We can propagate for the shifted operand, but not the shift
181 : count. The count is handled specially. */
182 : case ASHIFT:
183 : case LSHIFTRT:
184 : case ASHIFTRT:
185 : case SS_ASHIFT:
186 : case US_ASHIFT:
187 : return true;
188 :
189 : /* There may be other safe codes. If so they can be added
190 : individually when discovered. */
191 : default:
192 : return false;
193 : }
194 : }
195 :
196 : /* Clear bits in LIVENOW and set bits in LIVE_TMP for objects
197 : set/clobbered by OBJ contained in INSN.
198 :
199 : Conceptually it is always safe to ignore a particular destination
200 : here as that will result in more chunks of data being considered
201 : live. That's what happens when we "continue" the main loop when
202 : we see something we don't know how to handle such as a vector
203 : mode destination.
204 :
205 : The more accurate we are in identifying what objects (and chunks
206 : within an object) are set by INSN, the more aggressive the
207 : optimization phase during use handling will be. */
208 :
209 : static bool
210 141819231 : ext_dce_process_sets (rtx_insn *insn, rtx obj, bitmap live_tmp)
211 : {
212 141819231 : bool skipped_dest = false;
213 :
214 141819231 : subrtx_iterator::array_type array;
215 406653074 : FOR_EACH_SUBRTX (iter, array, obj, NONCONST)
216 : {
217 264833843 : const_rtx x = *iter;
218 :
219 : /* An EXPR_LIST (from call fusage) ends in NULL_RTX. */
220 264833843 : if (x == NULL_RTX)
221 9828375 : continue;
222 :
223 255005468 : if (UNSPEC_P (x))
224 571483 : continue;
225 :
226 254433985 : if (GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
227 : {
228 146687838 : unsigned bit = 0;
229 146687838 : x = SET_DEST (x);
230 :
231 : /* We don't support vector destinations or destinations
232 : wider than DImode. */
233 146687838 : scalar_mode outer_mode;
234 149911719 : if (!is_a <scalar_mode> (GET_MODE (x), &outer_mode)
235 92963064 : || GET_MODE_BITSIZE (outer_mode) > HOST_BITS_PER_WIDE_INT)
236 : {
237 : /* Skip the subrtxs of this destination. There is
238 : little value in iterating into the subobjects, so
239 : just skip them for a bit of efficiency. */
240 56948655 : skipped_dest = true;
241 56948655 : iter.skip_subrtxes ();
242 264833843 : continue;
243 : }
244 :
245 : /* We could have (strict_low_part (subreg ...)). We can not just
246 : strip the STRICT_LOW_PART as that would result in clearing
247 : some bits in LIVENOW that are still live. So process the
248 : STRICT_LOW_PART specially. */
249 89739183 : if (GET_CODE (x) == STRICT_LOW_PART)
250 : {
251 0 : x = XEXP (x, 0);
252 :
253 : /* The only valid operand of a STRICT_LOW_PART is a non
254 : paradoxical SUBREG. */
255 0 : gcc_assert (SUBREG_P (x)
256 : && !paradoxical_subreg_p (x)
257 : && SUBREG_BYTE (x).is_constant ());
258 :
259 : /* I think we should always see a REG here. But let's
260 : be sure. */
261 0 : gcc_assert (REG_P (SUBREG_REG (x)));
262 :
263 : /* The inner mode might be larger, just punt for
264 : that case. Remember, we can not just continue to process
265 : the inner RTXs due to the STRICT_LOW_PART. */
266 0 : if (!is_a <scalar_mode> (GET_MODE (SUBREG_REG (x)), &outer_mode)
267 0 : || GET_MODE_BITSIZE (outer_mode) > HOST_BITS_PER_WIDE_INT)
268 : {
269 : /* Skip the subrtxs of the STRICT_LOW_PART. We can't
270 : process them because it'll set objects as no longer
271 : live when they are in fact still live. */
272 0 : skipped_dest = true;
273 0 : iter.skip_subrtxes ();
274 0 : continue;
275 : }
276 :
277 : /* LIVE_TMP contains the set groups that are live-out and set in
278 : this insn. It is used to narrow the groups live-in for the
279 : inputs of this insn.
280 :
281 : The simple thing to do is mark all the groups as live, but
282 : that will significantly inhibit optimization.
283 :
284 : We also need to be careful in the case where we have an in-out
285 : operand. If we're not careful we'd clear LIVE_TMP
286 : incorrectly. */
287 0 : HOST_WIDE_INT rn = REGNO (SUBREG_REG (x));
288 0 : int limit = group_limit (SUBREG_REG (x));
289 0 : for (HOST_WIDE_INT i = 4 * rn; i < 4 * rn + limit; i++)
290 0 : if (bitmap_bit_p (livenow, i))
291 0 : bitmap_set_bit (live_tmp, i);
292 :
293 0 : if (bitmap_empty_p (live_tmp))
294 0 : make_reg_live (live_tmp, rn);
295 :
296 : /* The mode of the SUBREG tells us how many bits we can
297 : clear. */
298 0 : machine_mode mode = GET_MODE (x);
299 0 : HOST_WIDE_INT size
300 0 : = exact_log2 (GET_MODE_SIZE (mode).to_constant ()) + 1;
301 0 : bitmap_clear_range (livenow, 4 * rn, size);
302 :
303 : /* We have fully processed this destination. */
304 0 : iter.skip_subrtxes ();
305 0 : continue;
306 0 : }
307 :
308 : /* Phase one of destination handling. First remove any wrapper
309 : such as SUBREG or ZERO_EXTRACT. */
310 89739183 : unsigned HOST_WIDE_INT mask
311 89739183 : = GET_MODE_MASK (GET_MODE_INNER (GET_MODE (x)));
312 89739183 : if (SUBREG_P (x))
313 : {
314 : /* If we have a SUBREG destination that is too wide, just
315 : skip the destination rather than continuing this iterator.
316 : While continuing would be better, we'd need to strip the
317 : subreg and restart within the SET processing rather than
318 : the top of the loop which just complicates the flow even
319 : more. */
320 590920 : if (!is_a <scalar_mode> (GET_MODE (SUBREG_REG (x)), &outer_mode)
321 590920 : || GET_MODE_BITSIZE (outer_mode) > HOST_BITS_PER_WIDE_INT)
322 : {
323 142537 : skipped_dest = true;
324 142537 : iter.skip_subrtxes ();
325 142537 : continue;
326 : }
327 :
328 : /* We can safely strip a paradoxical subreg. The inner mode will
329 : be narrower than the outer mode. We'll clear fewer bits in
330 : LIVENOW than we'd like, but that's always safe. */
331 448383 : if (paradoxical_subreg_p (x))
332 : x = XEXP (x, 0);
333 437286 : else if (SUBREG_BYTE (x).is_constant ())
334 : {
335 437286 : bit = subreg_lsb (x).to_constant ();
336 437286 : mask = GET_MODE_MASK (GET_MODE (SUBREG_REG (x))) << bit;
337 437286 : gcc_assert (mask);
338 : x = SUBREG_REG (x);
339 : }
340 : else
341 : gcc_unreachable ();
342 : }
343 :
344 89596646 : if (GET_CODE (x) == ZERO_EXTRACT)
345 : {
346 : /* Unlike a SUBREG destination, a set of a ZERO_EXTRACT only
347 : modifies the bits referenced in the ZERO_EXTRACT, the rest
348 : remain the same. Thus we can not continue here, we must
349 : either figure out what part of the destination is modified
350 : or skip the sub-rtxs. */
351 3615 : skipped_dest = true;
352 3615 : iter.skip_subrtxes ();
353 3615 : continue;
354 : }
355 :
356 : /* BIT >= 64 indicates something went horribly wrong. */
357 89593031 : gcc_assert (bit <= HOST_BITS_PER_WIDE_INT - 1);
358 :
359 : /* Now handle the actual object that was changed. */
360 89593031 : if (REG_P (x))
361 : {
362 : /* LIVE_TMP contains the set groups that are live-out and set in
363 : this insn. It is used to narrow the groups live-in for the
364 : inputs of this insn.
365 :
366 : The simple thing to do is mark all the groups as live, but
367 : that will significantly inhibit optimization.
368 :
369 : We also need to be careful in the case where we have an in-out
370 : operand. If we're not careful we'd clear LIVE_TMP
371 : incorrectly. */
372 75083735 : HOST_WIDE_INT rn = REGNO (x);
373 75083735 : int limit = group_limit (x);
374 336034880 : for (HOST_WIDE_INT i = 4 * rn; i < 4 * rn + limit; i++)
375 260951145 : if (bitmap_bit_p (livenow, i))
376 253382168 : bitmap_set_bit (live_tmp, i);
377 :
378 75083735 : if (bitmap_empty_p (live_tmp))
379 1256228 : make_reg_live (live_tmp, rn);
380 :
381 : /* Now clear the bits known written by this instruction.
382 : Note that BIT need not be a power of two, consider a
383 : ZERO_EXTRACT destination. */
384 75083735 : int start = (bit < 8 ? 0 : bit < 16 ? 1 : bit < 32 ? 2 : 3);
385 80086801 : int end = ((mask & ~HOST_WIDE_INT_UC (0xffffffff)) ? 4
386 28517858 : : (mask & HOST_WIDE_INT_UC (0xffff0000)) ? 3
387 5846858 : : (mask & 0xff00) ? 2 : 1);
388 75083735 : bitmap_clear_range (livenow, 4 * rn + start, end - start);
389 : }
390 : /* Some ports generate (clobber (const_int)). */
391 14509296 : else if (CONST_INT_P (x))
392 0 : continue;
393 : else
394 14509296 : gcc_assert (CALL_P (insn)
395 : || MEM_P (x)
396 : || x == pc_rtx
397 : || GET_CODE (x) == SCRATCH);
398 :
399 89593031 : iter.skip_subrtxes ();
400 89593031 : }
401 107746147 : else if (GET_CODE (x) == COND_EXEC)
402 : {
403 : /* This isn't ideal, but may not be so bad in practice. */
404 0 : skipped_dest = true;
405 0 : iter.skip_subrtxes ();
406 : }
407 : }
408 141819231 : return skipped_dest;
409 141819231 : }
410 :
411 : /* INSN is a right shift and the second insn in a shift pair that is a
412 : sign or zero extension (SET is the single set associated with INSN).
413 :
414 : Replace the source of SET with NEW_SRC which is a source register
415 : from NEW_SRC_INSN (the left shift in the pair). This is effectively
416 : the same as the replacement we do for ZERO/SIGN extends on targets
417 : that support those insns. */
418 : static void
419 0 : ext_dce_try_optimize_rshift (rtx_insn *insn, rtx set, rtx new_src, rtx_insn *new_src_insn)
420 : {
421 : /* If the modes are not the same or one is a hard register, then
422 : conservatively do nothing. */
423 0 : if (GET_MODE (SET_SRC (set)) != GET_MODE (new_src)
424 0 : || !REG_P (XEXP (SET_SRC (set), 0))
425 0 : || !REG_P (new_src)
426 0 : || REGNO (XEXP (SET_SRC (set), 0)) < FIRST_PSEUDO_REGISTER
427 0 : || REGNO (new_src) < FIRST_PSEUDO_REGISTER)
428 : return;
429 :
430 0 : if (dump_file)
431 : {
432 0 : fprintf (dump_file, "Processing insn:\n");
433 0 : dump_insn_slim (dump_file, insn);
434 0 : fprintf (dump_file, "Trying to simplify pattern:\n");
435 0 : print_rtl_single (dump_file, SET_SRC (set));
436 : }
437 :
438 : /* We decided to turn do the optimization but allow it to be rejected for
439 : bisection purposes. */
440 0 : if (!dbg_cnt (::ext_dce))
441 : {
442 0 : if (dump_file)
443 0 : fprintf (dump_file, "Rejected due to debug counter.\n");
444 : return;
445 : }
446 :
447 : /* We're going to generate a fresh insn for the move, so put it
448 : into a sequence that we can emit after the current insn. */
449 0 : start_sequence ();
450 0 : emit_move_insn (SET_DEST (set), new_src);
451 0 : rtx_insn *seq = end_sequence ();
452 0 : emit_insn_after (seq, insn);
453 :
454 : /* Mark the destination as changed. */
455 0 : rtx x = SET_DEST (set);
456 0 : while (SUBREG_P (x) || GET_CODE (x) == ZERO_EXTRACT)
457 0 : x = XEXP (x, 0);
458 0 : gcc_assert (REG_P (x));
459 0 : bitmap_set_bit (changed_pseudos, REGNO (x));
460 :
461 0 : if (dump_file)
462 : {
463 0 : fprintf (dump_file, "Successfully transformed to:\n");
464 0 : print_rtl_single (dump_file, PATTERN (seq));
465 0 : fprintf (dump_file, "\n");
466 : }
467 :
468 0 : delete_insn (insn);
469 :
470 : /* If NEW_SRC died in its prior location, then we need to remove the
471 : death note and move it to the new location. */
472 0 : rtx note = find_regno_note (new_src_insn, REG_DEAD, REGNO (new_src));
473 0 : if (note)
474 : {
475 0 : remove_note (new_src_insn, note);
476 0 : add_reg_note (insn, REG_DEAD, new_src);
477 : }
478 : }
479 :
480 : /* Return true if the extension of INNER to OUTER_MODE is already
481 : guaranteed by its definition, making the SIGN_EXTEND redundant. */
482 : static bool
483 132 : is_trivially_redundant_extension (rtx inner, scalar_int_mode inner_mode,
484 : scalar_int_mode outer_mode)
485 : {
486 132 : rtx def_reg = inner;
487 :
488 132 : if (SUBREG_P (def_reg) && subreg_lowpart_p (def_reg))
489 22 : def_reg = SUBREG_REG (def_reg);
490 :
491 132 : if (!REG_P (def_reg))
492 : return false;
493 :
494 132 : unsigned int regno = REGNO (def_reg);
495 132 : if (HARD_REGISTER_NUM_P (regno) || DF_REG_DEF_COUNT (regno) != 1)
496 : return false;
497 :
498 68 : df_ref def = DF_REG_DEF_CHAIN (regno);
499 68 : if (!def)
500 : return false;
501 :
502 68 : rtx_insn *def_insn = DF_REF_INSN (def);
503 68 : rtx def_set = def_insn ? single_set (def_insn) : NULL_RTX;
504 68 : if (!def_set)
505 : return false;
506 :
507 68 : rtx def_src = SET_SRC (def_set);
508 :
509 68 : unsigned int diff = GET_MODE_PRECISION (outer_mode)
510 68 : - GET_MODE_PRECISION (inner_mode);
511 :
512 68 : if (num_sign_bit_copies (def_src, outer_mode) > diff)
513 0 : return true;
514 :
515 : return false;
516 : }
517 :
518 : /* INSN has a sign/zero extended source inside SET that we will
519 : try to turn into a SUBREG. If NEW_SRC is non-null, use that
520 : for the new source of INSN's set. That scenario only happens
521 : when we're optimizing a shift pair. */
522 : static void
523 8945 : ext_dce_try_optimize_extension (rtx_insn *insn, rtx set)
524 : {
525 8945 : rtx src = SET_SRC (set);
526 8945 : rtx inner = XEXP (src, 0);
527 :
528 : /* For sign-extending loads from memory, try to replace with a
529 : zero-extending load when the upper bits are dead. E.g. on RISC-V
530 : this turns lh+zext.h into just lhu. */
531 8945 : if (MEM_P (inner) && GET_CODE (src) == SIGN_EXTEND)
532 : {
533 36 : if (dump_file)
534 : {
535 0 : fprintf (dump_file, "Processing insn:\n");
536 0 : dump_insn_slim (dump_file, insn);
537 0 : fprintf (dump_file, "Trying to narrow sign_extend to zero_extend:\n");
538 0 : print_rtl_single (dump_file, SET_SRC (set));
539 : }
540 :
541 36 : if (!dbg_cnt (::ext_dce))
542 : {
543 0 : if (dump_file)
544 0 : fprintf (dump_file, "Rejected due to debug counter.\n");
545 2847 : return;
546 : }
547 :
548 36 : rtx new_pattern = gen_rtx_ZERO_EXTEND (GET_MODE (src), inner);
549 36 : int ok = validate_change (insn, &SET_SRC (set), new_pattern, false);
550 :
551 36 : rtx x = SET_DEST (set);
552 36 : while (SUBREG_P (x) || GET_CODE (x) == ZERO_EXTRACT)
553 0 : x = XEXP (x, 0);
554 :
555 36 : gcc_assert (REG_P (x));
556 36 : if (ok)
557 : {
558 36 : bitmap_set_bit (changed_pseudos, REGNO (x));
559 36 : remove_reg_equal_equiv_notes (insn, false);
560 : }
561 :
562 36 : if (dump_file)
563 : {
564 0 : if (ok)
565 0 : fprintf (dump_file, "Successfully transformed to:\n");
566 : else
567 0 : fprintf (dump_file, "Failed transformation to:\n");
568 0 : print_rtl_single (dump_file, new_pattern);
569 0 : fprintf (dump_file, "\n");
570 : }
571 : return;
572 : }
573 :
574 : /* Avoid (subreg (mem)) and other constructs which may be valid RTL, but
575 : not useful for this optimization. */
576 8909 : if (!(REG_P (inner) || (SUBREG_P (inner) && REG_P (SUBREG_REG (inner)))))
577 : return;
578 :
579 : /* If the extension is trivially redundant, then we leave it in the IL
580 : as we know combine can easily remove it. By doing so those sign
581 : bits keep a known state rather than unknown state which helps combine
582 : make more improvements. */
583 6098 : scalar_int_mode inner_mode, outer_mode;
584 6098 : if (GET_CODE (src) == SIGN_EXTEND
585 132 : && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
586 6230 : && is_a <scalar_int_mode> (GET_MODE (src), &outer_mode))
587 : {
588 132 : if (is_trivially_redundant_extension (inner, inner_mode, outer_mode))
589 : return;
590 : }
591 6098 : rtx new_pattern;
592 6098 : if (dump_file)
593 : {
594 0 : fprintf (dump_file, "Processing insn:\n");
595 0 : dump_insn_slim (dump_file, insn);
596 0 : fprintf (dump_file, "Trying to simplify pattern:\n");
597 0 : print_rtl_single (dump_file, SET_SRC (set));
598 : }
599 :
600 : /* We decided to turn do the optimization but allow it to be rejected for
601 : bisection purposes. */
602 6098 : if (!dbg_cnt (::ext_dce))
603 : {
604 0 : if (dump_file)
605 0 : fprintf (dump_file, "Rejected due to debug counter.\n");
606 : return;
607 : }
608 :
609 12196 : new_pattern = simplify_gen_subreg (GET_MODE (src), inner,
610 6098 : GET_MODE (inner), 0);
611 : /* simplify_gen_subreg may fail in which case NEW_PATTERN will be NULL.
612 : We must not pass that as a replacement pattern to validate_change. */
613 6098 : if (new_pattern)
614 : {
615 6098 : int ok = validate_change (insn, &SET_SRC (set), new_pattern, false);
616 :
617 6098 : rtx x = SET_DEST (set);
618 6098 : while (SUBREG_P (x) || GET_CODE (x) == ZERO_EXTRACT)
619 0 : x = XEXP (x, 0);
620 :
621 6098 : gcc_assert (REG_P (x));
622 6098 : if (ok)
623 6098 : bitmap_set_bit (changed_pseudos, REGNO (x));
624 :
625 6098 : if (dump_file)
626 : {
627 0 : if (ok)
628 0 : fprintf (dump_file, "Successfully transformed to:\n");
629 : else
630 0 : fprintf (dump_file, "Failed transformation to:\n");
631 :
632 0 : print_rtl_single (dump_file, new_pattern);
633 0 : fprintf (dump_file, "\n");
634 : }
635 :
636 : /* INSN may have a REG_EQUAL note indicating that the value was
637 : sign or zero extended. That note is no longer valid since we've
638 : just removed the extension. Just wipe the notes. */
639 6098 : if (ok)
640 6098 : remove_reg_equal_equiv_notes (insn, false);
641 : }
642 : else
643 : {
644 0 : if (dump_file)
645 0 : fprintf (dump_file, "Unable to generate valid SUBREG expression.\n");
646 : }
647 : }
648 :
649 : /* Try to promote a narrow-mode operation wrapped in a sign/zero extension
650 : to the wider mode when the extended bits are dead. For example,
651 : (sign_extend:DI (plus:SI (x) (y))) -> (plus:DI (x') (y'))
652 : where x' and y' are the operands promoted to DI mode.
653 :
654 : This enables the combine pass to match wider-mode target patterns
655 : (e.g., sh2add on RISC-V) that cannot match the narrow-mode operation. */
656 :
657 : static void
658 0 : ext_dce_try_promote_operation (rtx_insn *insn, rtx set)
659 : {
660 0 : rtx src = SET_SRC (set);
661 :
662 : /* If the extension was already optimized away, nothing to do. */
663 0 : if (GET_CODE (src) != SIGN_EXTEND && GET_CODE (src) != ZERO_EXTEND)
664 0 : return;
665 :
666 0 : machine_mode outer_mode = GET_MODE (src);
667 0 : rtx inner = XEXP (src, 0);
668 :
669 : /* Only handle binary and unary arithmetic/logic operations. */
670 0 : if (!BINARY_P (inner) && !UNARY_P (inner))
671 : return;
672 :
673 0 : rtx_code inner_code = GET_CODE (inner);
674 :
675 : /* Restrict to operations whose result in the low bits is identical
676 : regardless of input width (i.e., no high-bit dependencies). */
677 0 : switch (inner_code)
678 : {
679 0 : case PLUS:
680 0 : case MINUS:
681 0 : case MULT:
682 0 : case NEG:
683 0 : case AND:
684 0 : case IOR:
685 0 : case XOR:
686 0 : case NOT:
687 0 : case ASHIFT:
688 0 : break;
689 : default:
690 : return;
691 : }
692 :
693 : /* Promote each operand to the outer mode. */
694 0 : int nops = BINARY_P (inner) ? 2 : 1;
695 0 : rtx new_ops[2];
696 :
697 0 : for (int i = 0; i < nops; i++)
698 : {
699 0 : rtx op = XEXP (inner, i);
700 :
701 0 : if (CONST_INT_P (op))
702 0 : new_ops[i] = op;
703 0 : else if (REG_P (op))
704 : {
705 0 : new_ops[i] = simplify_gen_subreg (outer_mode, op,
706 0 : GET_MODE (op), 0);
707 0 : if (!new_ops[i])
708 : return;
709 : }
710 0 : else if (SUBREG_P (op) && REG_P (SUBREG_REG (op)))
711 : {
712 : /* The inner register may already be in the target mode
713 : (e.g., subreg:SI (reg:DI ...) 0). Extract it directly
714 : rather than creating a paradoxical subreg of a subreg,
715 : which simplify_gen_subreg rejects. */
716 0 : rtx inner_reg = SUBREG_REG (op);
717 0 : if (GET_MODE (inner_reg) == outer_mode)
718 0 : new_ops[i] = inner_reg;
719 : else
720 : {
721 0 : new_ops[i] = simplify_gen_subreg (outer_mode, inner_reg,
722 : GET_MODE (inner_reg), 0);
723 0 : if (!new_ops[i])
724 : return;
725 : }
726 : }
727 : else
728 : return;
729 : }
730 :
731 : /* Build the promoted operation. */
732 0 : rtx new_src;
733 0 : if (BINARY_P (inner))
734 0 : new_src = gen_rtx_fmt_ee (inner_code, outer_mode,
735 : new_ops[0], new_ops[1]);
736 : else
737 0 : new_src = gen_rtx_fmt_e (inner_code, outer_mode, new_ops[0]);
738 :
739 0 : if (dump_file)
740 : {
741 0 : fprintf (dump_file, "Processing insn:\n");
742 0 : dump_insn_slim (dump_file, insn);
743 0 : fprintf (dump_file, "Trying to promote to wider mode:\n");
744 0 : print_rtl_single (dump_file, new_src);
745 : }
746 :
747 : /* We decided to try the promotion but allow it to be rejected for
748 : bisection purposes. */
749 0 : if (!dbg_cnt (::ext_dce))
750 : {
751 0 : if (dump_file)
752 0 : fprintf (dump_file, "Rejected due to debug counter.\n");
753 : return;
754 : }
755 :
756 0 : int ok = validate_change (insn, &SET_SRC (set), new_src, false);
757 :
758 0 : rtx x = SET_DEST (set);
759 0 : while (SUBREG_P (x) || GET_CODE (x) == ZERO_EXTRACT)
760 0 : x = XEXP (x, 0);
761 :
762 0 : gcc_assert (REG_P (x));
763 0 : if (ok)
764 0 : bitmap_set_bit (changed_pseudos, REGNO (x));
765 :
766 0 : if (dump_file)
767 : {
768 0 : if (ok)
769 0 : fprintf (dump_file, "Successfully promoted to:\n");
770 : else
771 0 : fprintf (dump_file, "Failed promotion to:\n");
772 0 : print_rtl_single (dump_file, new_src);
773 0 : fprintf (dump_file, "\n");
774 : }
775 :
776 0 : if (ok)
777 0 : remove_reg_equal_equiv_notes (insn, false);
778 : }
779 :
780 : /* Record INSN as a promotion candidate if it passes the same validity
781 : checks as ext_dce_try_promote_operation. We defer actual promotion
782 : until we can determine whether the candidate is part of a chain. */
783 :
784 : static void
785 2847 : ext_dce_record_promotion_candidate (rtx_insn *insn, rtx set)
786 : {
787 2847 : rtx src = SET_SRC (set);
788 :
789 2847 : if (GET_CODE (src) != SIGN_EXTEND && GET_CODE (src) != ZERO_EXTEND)
790 : return;
791 :
792 2847 : machine_mode outer_mode = GET_MODE (src);
793 2847 : rtx inner = XEXP (src, 0);
794 :
795 2847 : if (!BINARY_P (inner) && !UNARY_P (inner))
796 : return;
797 :
798 1806 : rtx_code inner_code = GET_CODE (inner);
799 :
800 1806 : switch (inner_code)
801 : {
802 0 : case PLUS:
803 0 : case MINUS:
804 0 : case MULT:
805 0 : case NEG:
806 0 : case AND:
807 0 : case IOR:
808 0 : case XOR:
809 0 : case NOT:
810 0 : case ASHIFT:
811 0 : break;
812 : default:
813 : return;
814 : }
815 :
816 : /* Dry-run: check that all operands can be promoted. */
817 0 : int nops = BINARY_P (inner) ? 2 : 1;
818 0 : for (int i = 0; i < nops; i++)
819 : {
820 0 : rtx op = XEXP (inner, i);
821 0 : if (CONST_INT_P (op))
822 0 : continue;
823 0 : else if (REG_P (op))
824 : {
825 0 : if (!simplify_gen_subreg (outer_mode, op, GET_MODE (op), 0))
826 : return;
827 : }
828 0 : else if (SUBREG_P (op) && REG_P (SUBREG_REG (op)))
829 : {
830 0 : rtx inner_reg = SUBREG_REG (op);
831 0 : if (GET_MODE (inner_reg) != outer_mode
832 0 : && !simplify_gen_subreg (outer_mode, inner_reg,
833 : GET_MODE (inner_reg), 0))
834 : return;
835 : }
836 : else
837 : return;
838 : }
839 :
840 : /* Find the destination register. */
841 0 : rtx dest = SET_DEST (set);
842 0 : while (SUBREG_P (dest) || GET_CODE (dest) == ZERO_EXTRACT)
843 0 : dest = XEXP (dest, 0);
844 0 : if (!REG_P (dest))
845 : return;
846 0 : unsigned int dest_regno = REGNO (dest);
847 :
848 : /* Record the candidate. */
849 0 : promotion_candidates.safe_push ({insn, set});
850 0 : bitmap_set_bit (promotable_dests, dest_regno);
851 :
852 : /* Mark register operands as consumed by a candidate. */
853 0 : for (int i = 0; i < nops; i++)
854 : {
855 0 : rtx op = XEXP (inner, i);
856 0 : if (REG_P (op))
857 0 : bitmap_set_bit (consumed_by_candidate, REGNO (op));
858 0 : else if (SUBREG_P (op) && REG_P (SUBREG_REG (op)))
859 0 : bitmap_set_bit (consumed_by_candidate, REGNO (SUBREG_REG (op)));
860 : }
861 : }
862 :
863 : /* Promote candidates that form chains: a candidate whose result feeds
864 : into another candidate's operand, or whose operand comes from another
865 : candidate's result. Skip standalone (isolated) promotions. */
866 :
867 : static void
868 10024676 : ext_dce_promote_chained_candidates (void)
869 : {
870 : /* Propagate chain info through copies recorded during the reverse scan.
871 : Since copies are recorded in reverse order, iterate forward to propagate
872 : promotable_dests (which was set late in the scan) through copies that
873 : were seen earlier. */
874 10024676 : unsigned cix;
875 10024676 : copy_info *cp;
876 10030774 : FOR_EACH_VEC_ELT (promotion_copies, cix, cp)
877 : {
878 6098 : if (bitmap_bit_p (promotable_dests, cp->src_regno))
879 0 : bitmap_set_bit (promotable_dests, cp->dest_regno);
880 6098 : if (bitmap_bit_p (consumed_by_candidate, cp->dest_regno))
881 0 : bitmap_set_bit (consumed_by_candidate, cp->src_regno);
882 : }
883 :
884 : unsigned ix;
885 : promotion_candidate_info *cand;
886 :
887 10024676 : FOR_EACH_VEC_ELT (promotion_candidates, ix, cand)
888 : {
889 : /* Find destination register. */
890 0 : rtx dest = SET_DEST (cand->set);
891 0 : while (SUBREG_P (dest) || GET_CODE (dest) == ZERO_EXTRACT)
892 0 : dest = XEXP (dest, 0);
893 0 : unsigned int dest_regno = REGNO (dest);
894 :
895 : /* Check if this candidate's result feeds into another candidate. */
896 0 : bool is_chained = bitmap_bit_p (consumed_by_candidate, dest_regno);
897 :
898 : /* Check if any operand comes from another candidate's result. */
899 0 : if (!is_chained)
900 : {
901 0 : rtx inner = XEXP (SET_SRC (cand->set), 0);
902 0 : int nops = BINARY_P (inner) ? 2 : 1;
903 0 : for (int i = 0; i < nops && !is_chained; i++)
904 : {
905 0 : rtx op = XEXP (inner, i);
906 0 : if (REG_P (op))
907 0 : is_chained = bitmap_bit_p (promotable_dests, REGNO (op));
908 0 : else if (SUBREG_P (op) && REG_P (SUBREG_REG (op)))
909 0 : is_chained = bitmap_bit_p (promotable_dests,
910 0 : REGNO (SUBREG_REG (op)));
911 : }
912 : }
913 :
914 0 : if (is_chained)
915 0 : ext_dce_try_promote_operation (cand->insn, cand->set);
916 0 : else if (dump_file)
917 : {
918 0 : fprintf (dump_file, "Skipping standalone promotion for insn:\n");
919 0 : dump_insn_slim (dump_file, cand->insn);
920 0 : fprintf (dump_file, "\n");
921 : }
922 : }
923 :
924 10024676 : promotion_candidates.truncate (0);
925 10024676 : promotion_copies.truncate (0);
926 10024676 : bitmap_clear (promotable_dests);
927 10024676 : bitmap_clear (consumed_by_candidate);
928 10024676 : }
929 :
930 : /* Some operators imply that their second operand is fully live,
931 : regardless of how many bits in the output are live. An example
932 : would be the shift count on a target without SHIFT_COUNT_TRUNCATED
933 : defined.
934 :
935 : Return TRUE if CODE is such an operator. FALSE otherwise. */
936 :
937 : static bool
938 78917085 : binop_implies_op2_fully_live (rtx_code code)
939 : {
940 0 : switch (code)
941 : {
942 : case ASHIFT:
943 : case LSHIFTRT:
944 : case ASHIFTRT:
945 : case ROTATE:
946 : case ROTATERT:
947 : case SS_ASHIFT:
948 : case US_ASHIFT:
949 : return !SHIFT_COUNT_TRUNCATED;
950 :
951 0 : default:
952 0 : return false;
953 : }
954 : }
955 :
956 : /* X, with code CODE, is an operation for which safe_for_live_propagation
957 : holds true, and bits set in MASK are live in the result. Compute a
958 : mask of (potentially) live bits in the non-constant inputs. In case of
959 : binop_implies_op2_fully_live (e.g. shifts), the computed mask may
960 : exclusively pertain to the first operand.
961 :
962 : This looks wrong as we may have some important operations embedded as
963 : operands of another operation. For example, we might have an extension
964 : wrapping a shift. It really feels like this needs to be recursing down
965 : into operands much more often. */
966 :
967 : unsigned HOST_WIDE_INT
968 73600464 : carry_backpropagate (unsigned HOST_WIDE_INT mask, enum rtx_code code, rtx x)
969 : {
970 75326373 : if (mask == 0)
971 : return 0;
972 :
973 : /* Consider a vector operation, the bits live are the element bits live
974 : broadcasted across the vector. So there can be holes (consider a
975 : logical shift).
976 :
977 : Vector modes aren't likely to represent cases we can optimize with
978 : any regularity. It seems sensible to just punt that case in a
979 : conservatively correct way.
980 :
981 : The conservatively corect choice here would be to return the mode
982 : mask for the outer mode. We're already doing that for modes larger
983 : than HOST_BITS_PER_WIDE_INT, so it should be safe for larger vectors
984 : as well as something like V2HI. */
985 75326322 : if (VECTOR_MODE_P (GET_MODE (x)) || COMPLEX_MODE_P (GET_MODE (x)))
986 4066626 : return GET_MODE_MASK (GET_MODE (x));
987 :
988 :
989 71259696 : enum machine_mode mode = GET_MODE (x);
990 71259696 : unsigned HOST_WIDE_INT mmask = GET_MODE_MASK (mode);
991 :
992 : /* While we don't try to optimize operations on types larger
993 : than 64 bits, we do want to make sure not to invoke undefined
994 : behavior when presented with such operations during use
995 : processing. The safe thing to do is to just return mmask
996 : for that scenario indicating every possible chunk is life. */
997 71259696 : scalar_int_mode smode;
998 71259696 : if (!is_a <scalar_int_mode> (mode, &smode)
999 59472194 : || GET_MODE_BITSIZE (smode) > HOST_BITS_PER_WIDE_INT)
1000 : return mmask;
1001 :
1002 58486804 : switch (code)
1003 : {
1004 16553387 : case PLUS:
1005 16553387 : case MINUS:
1006 16553387 : case MULT:
1007 16553387 : return (HOST_WIDE_INT_UC (2) << floor_log2 (mask)) - 1;
1008 :
1009 : /* We propagate for the shifted operand, but not the shift
1010 : count. The count is handled specially. */
1011 1404660 : case ASHIFT:
1012 1404660 : if (CONST_INT_P (XEXP (x, 1))
1013 2737554 : && UINTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (smode))
1014 1332866 : return (HOST_WIDE_INT) mask >> INTVAL (XEXP (x, 1));
1015 71794 : return (HOST_WIDE_INT_UC (2) << floor_log2 (mask)) - 1;
1016 :
1017 : /* We propagate for the shifted operand, but not the shift
1018 : count. The count is handled specially. */
1019 629973 : case LSHIFTRT:
1020 629973 : if (CONST_INT_P (XEXP (x, 1))
1021 1229228 : && UINTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (smode))
1022 599227 : return mmask & (mask << INTVAL (XEXP (x, 1)));
1023 : return mmask;
1024 :
1025 : /* We propagate for the shifted operand, but not the shift
1026 : count. The count is handled specially. */
1027 310375 : case ASHIFTRT:
1028 310375 : if (CONST_INT_P (XEXP (x, 1))
1029 605775 : && UINTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (smode))
1030 : {
1031 295400 : HOST_WIDE_INT sign = 0;
1032 295400 : if (HOST_BITS_PER_WIDE_INT - clz_hwi (mask) + INTVAL (XEXP (x, 1))
1033 295400 : > GET_MODE_BITSIZE (smode))
1034 590800 : sign = HOST_WIDE_INT_1U << (GET_MODE_BITSIZE (smode) - 1);
1035 295400 : return sign | (mmask & (mask << INTVAL (XEXP (x, 1))));
1036 : }
1037 : return mmask;
1038 :
1039 43416 : case SMUL_HIGHPART:
1040 43416 : case UMUL_HIGHPART:
1041 43416 : if (XEXP (x, 1) == const0_rtx)
1042 : return 0;
1043 43416 : if (XEXP (x, 1) == const1_rtx)
1044 : return mmask;
1045 43416 : if (CONST_INT_P (XEXP (x, 1)))
1046 : {
1047 0 : if (pow2p_hwi (INTVAL (XEXP (x, 1))))
1048 0 : return mmask & (mask << (GET_MODE_BITSIZE (smode)
1049 0 : - exact_log2 (INTVAL (XEXP (x, 1)))));
1050 :
1051 0 : int bits = (HOST_BITS_PER_WIDE_INT + GET_MODE_BITSIZE (smode)
1052 0 : - clz_hwi (mask) - ctz_hwi (INTVAL (XEXP (x, 1))));
1053 0 : if (bits < GET_MODE_BITSIZE (smode))
1054 0 : return (HOST_WIDE_INT_1U << bits) - 1;
1055 : }
1056 : return mmask;
1057 :
1058 574457 : case SIGN_EXTEND:
1059 574457 : if (!GET_MODE_BITSIZE (GET_MODE (x)).is_constant ()
1060 574457 : || !GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))).is_constant ())
1061 : return -1;
1062 :
1063 : /* See note about vector modes near the start of this function. */
1064 574457 : if (VECTOR_MODE_P (GET_MODE (XEXP (x, 0)))
1065 574457 : || COMPLEX_MODE_P (GET_MODE (XEXP (x, 0))))
1066 0 : return GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
1067 :
1068 : /* We want the mode of the inner object. We need to ensure its
1069 : sign bit is on in MASK. */
1070 574457 : mode = GET_MODE_INNER (GET_MODE (XEXP (x, 0)));
1071 574457 : if (mask & ~GET_MODE_MASK (mode))
1072 573919 : mask |= HOST_WIDE_INT_1U << (GET_MODE_BITSIZE (mode).to_constant ()
1073 573919 : - 1);
1074 :
1075 : /* Recurse into the operand. */
1076 574457 : return carry_backpropagate (mask, GET_CODE (XEXP (x, 0)), XEXP (x, 0));
1077 :
1078 1151452 : case ZERO_EXTEND:
1079 1151452 : if (!GET_MODE_BITSIZE (GET_MODE (x)).is_constant ()
1080 1151452 : || !GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))).is_constant ())
1081 : return -1;
1082 :
1083 : /* Recurse into the operand. */
1084 1151452 : return carry_backpropagate (mask, GET_CODE (XEXP (x, 0)), XEXP (x, 0));
1085 :
1086 : /* If an AND/IOR unconditionally clears/sets bits in the output, then
1087 : we do not care about the liveness of those bits in the inputs.
1088 :
1089 : So AND MASK with the constant for AND and with the complemented constant
1090 : for IOR. */
1091 1282064 : case AND:
1092 1282064 : case IOR:
1093 1282064 : if (CONST_INT_P (XEXP (x, 1)))
1094 787004 : mask &= (code == AND
1095 787004 : ? UINTVAL (XEXP (x, 1))
1096 71107 : : ~UINTVAL (XEXP (x, 1)));
1097 : return mask;
1098 :
1099 : /* We propagate for the shifted operand, but not the shift
1100 : count. The count is handled specially. */
1101 0 : case SS_ASHIFT:
1102 0 : case US_ASHIFT:
1103 0 : if (CONST_INT_P (XEXP (x, 1))
1104 0 : && UINTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (smode))
1105 : {
1106 0 : return ((mmask & ~((unsigned HOST_WIDE_INT) mmask
1107 0 : >> (INTVAL (XEXP (x, 1))
1108 0 : + (XEXP (x, 1) != const0_rtx
1109 0 : && code == SS_ASHIFT))))
1110 0 : | ((HOST_WIDE_INT) mask >> INTVAL (XEXP (x, 1))));
1111 : }
1112 : return mmask;
1113 :
1114 : default:
1115 : return mask;
1116 : }
1117 : }
1118 :
1119 : /* Process uses in INSN contained in OBJ. Set appropriate bits in LIVENOW
1120 : for any chunks of pseudos that become live, potentially filtering using
1121 : bits from LIVE_TMP.
1122 :
1123 : If MODIFY is true, then optimize sign/zero extensions to SUBREGs when
1124 : the extended bits are never read and mark pseudos which had extensions
1125 : eliminated in CHANGED_PSEUDOS. */
1126 :
1127 : static void
1128 141819231 : ext_dce_process_uses (rtx_insn *insn, rtx obj,
1129 : bitmap live_tmp, bool skipped_dest)
1130 : {
1131 141819231 : subrtx_var_iterator::array_type array_var;
1132 775822847 : FOR_EACH_SUBRTX_VAR (iter, array_var, obj, NONCONST)
1133 : {
1134 : /* An EXPR_LIST (from call fusage) ends in NULL_RTX. */
1135 634003616 : rtx x = *iter;
1136 634003616 : if (x == NULL_RTX)
1137 9828375 : continue;
1138 :
1139 : /* So the basic idea in this FOR_EACH_SUBRTX_VAR loop is to
1140 : handle SETs explicitly, possibly propagating live information
1141 : into the uses.
1142 :
1143 : We may continue the loop at various points which will cause
1144 : iteration into the next level of RTL. Breaking from the loop
1145 : is never safe as it can lead us to fail to process some of the
1146 : RTL and thus not make objects live when necessary. */
1147 624175241 : enum rtx_code xcode = GET_CODE (x);
1148 624175241 : if (xcode == SET)
1149 : {
1150 125402731 : const_rtx dst = SET_DEST (x);
1151 125402731 : rtx src = SET_SRC (x);
1152 125402731 : const_rtx y;
1153 125402731 : unsigned HOST_WIDE_INT bit = 0;
1154 :
1155 : /* The code of the RHS of a SET. */
1156 125402731 : enum rtx_code code = GET_CODE (src);
1157 :
1158 : /* ?!? How much of this should mirror SET handling, potentially
1159 : being shared? */
1160 125402731 : if (SUBREG_P (dst) && subreg_lsb (dst).is_constant (&bit))
1161 : {
1162 635963 : if (bit >= HOST_BITS_PER_WIDE_INT)
1163 : bit = HOST_BITS_PER_WIDE_INT - 1;
1164 635963 : dst = SUBREG_REG (dst);
1165 : }
1166 124766768 : else if (GET_CODE (dst) == STRICT_LOW_PART)
1167 10736 : dst = XEXP (dst, 0);
1168 :
1169 : /* Main processing of the uses. Two major goals here.
1170 :
1171 : First, we want to try and propagate liveness (or the lack
1172 : thereof) from the destination register to the source
1173 : register(s).
1174 :
1175 : Second, if the source is an extension, try to optimize
1176 : it into a SUBREG. The SUBREG form indicates we don't
1177 : care about the upper bits and will usually be copy
1178 : propagated away.
1179 :
1180 : If we fail to handle something in here, the expectation
1181 : is the iterator will dive into the sub-components and
1182 : mark all the chunks in any found REGs as live. */
1183 125402731 : if (REG_P (dst) && safe_for_live_propagation (code))
1184 : {
1185 : /* Create a mask representing the bits of this output
1186 : operand that are live after this insn. We can use
1187 : this information to refine the live in state of
1188 : inputs to this insn in many cases.
1189 :
1190 : We have to do this on a per SET basis, we might have
1191 : an INSN with multiple SETS, some of which can narrow
1192 : the source operand liveness, some of which may not. */
1193 73600464 : unsigned HOST_WIDE_INT dst_mask = 0;
1194 73600464 : HOST_WIDE_INT rn = REGNO (dst);
1195 73600464 : unsigned HOST_WIDE_INT mask_array[]
1196 : = { 0xff, 0xff00, HOST_WIDE_INT_UC (0xffff0000),
1197 : -HOST_WIDE_INT_UC (0x100000000) };
1198 368002320 : for (int i = 0; i < 4; i++)
1199 294401856 : if (bitmap_bit_p (live_tmp, 4 * rn + i))
1200 236500543 : dst_mask |= mask_array[i];
1201 73600464 : dst_mask >>= bit;
1202 :
1203 : /* If we ignored a destination during set processing, then
1204 : consider all the bits live. */
1205 73600464 : if (skipped_dest)
1206 25625868 : dst_mask = -1;
1207 :
1208 73600464 : dst_mask = carry_backpropagate (dst_mask, code, src);
1209 :
1210 : /* ??? Could also handle ZERO_EXTRACT / SIGN_EXTRACT
1211 : of the source specially to improve optimization. */
1212 73600464 : if (code == SIGN_EXTEND || code == ZERO_EXTEND)
1213 : {
1214 1754021 : rtx inner = XEXP (src, 0);
1215 1754021 : unsigned HOST_WIDE_INT src_mask
1216 1754021 : = GET_MODE_MASK (GET_MODE (inner));
1217 :
1218 : /* DST_MASK could be zero if we had something in the SET
1219 : that we couldn't handle. */
1220 1754021 : if (modify && !skipped_dest && (dst_mask & ~src_mask) == 0)
1221 : {
1222 8945 : ext_dce_try_optimize_extension (insn, x);
1223 :
1224 : /* If the extension was optimized to a copy, propagate
1225 : chain info through it: if the dest is consumed by a
1226 : promotion candidate (seen later in reverse scan),
1227 : the source register is transitively consumed too. */
1228 8945 : rtx opt_src = SET_SRC (x);
1229 8945 : if (GET_CODE (opt_src) != SIGN_EXTEND
1230 8945 : && GET_CODE (opt_src) != ZERO_EXTEND)
1231 : {
1232 6098 : rtx copy_dest = SET_DEST (x);
1233 6098 : while (SUBREG_P (copy_dest)
1234 6098 : || GET_CODE (copy_dest) == ZERO_EXTRACT)
1235 0 : copy_dest = XEXP (copy_dest, 0);
1236 :
1237 6098 : rtx copy_src = opt_src;
1238 6098 : if (SUBREG_P (copy_src))
1239 5770 : copy_src = SUBREG_REG (copy_src);
1240 :
1241 6098 : if (REG_P (copy_dest) && REG_P (copy_src))
1242 : {
1243 6098 : if (bitmap_bit_p (consumed_by_candidate,
1244 6098 : REGNO (copy_dest)))
1245 0 : bitmap_set_bit (consumed_by_candidate,
1246 0 : REGNO (copy_src));
1247 6098 : if (bitmap_bit_p (promotable_dests,
1248 6098 : REGNO (copy_src)))
1249 0 : bitmap_set_bit (promotable_dests,
1250 0 : REGNO (copy_dest));
1251 6098 : promotion_copies.safe_push (
1252 6098 : {REGNO (copy_dest), REGNO (copy_src)});
1253 : }
1254 : }
1255 : else
1256 2847 : ext_dce_record_promotion_candidate (insn, x);
1257 : }
1258 :
1259 : /* Stripping the extension here just seems wrong on multiple
1260 : levels. It's source side handling, so it seems like it
1261 : belongs in the loop below. Stripping here also makes it
1262 : harder than necessary to properly handle live bit groups
1263 : for (ANY_EXTEND (SUBREG)) where the SUBREG has
1264 : SUBREG_PROMOTED state. */
1265 1754021 : dst_mask &= src_mask;
1266 1754021 : src = XEXP (src, 0);
1267 1754021 : code = GET_CODE (src);
1268 : }
1269 :
1270 : /* Special case for (sub)targets that do not have extension
1271 : insns (and thus use shifts). We want to detect when we have
1272 : a shift pair and treat the pair as-if was an extension.
1273 :
1274 : Key on the right shift and use (for now) simplistic tests
1275 : to find the corresponding left shift. */
1276 73600464 : scalar_mode outer_mode;
1277 73600464 : if ((code == LSHIFTRT || code == ASHIFTRT)
1278 1098974 : && CONST_INT_P (XEXP (src, 1))
1279 1204833 : && (INTVAL (XEXP (src, 1)) == BITS_PER_WORD - 8
1280 1200735 : || INTVAL (XEXP (src, 1)) == BITS_PER_WORD - 16
1281 1035679 : || INTVAL (XEXP (src, 1)) == BITS_PER_WORD - 32)
1282 128772 : && is_a <scalar_mode> (GET_MODE (src), &outer_mode)
1283 74784600 : && GET_MODE_BITSIZE (outer_mode) <= HOST_BITS_PER_WIDE_INT)
1284 : {
1285 : /* So we have a right shift that could correspond to
1286 : the second in a pair implementing QI, HI or SI -> DI
1287 : extension. See if we can find the left shift. For
1288 : now, just look one real instruction back. */
1289 85162 : rtx_insn *prev_insn = prev_nonnote_nondebug_insn_bb (insn);
1290 :
1291 : /* The previous insn must be a left shift by the same
1292 : amount. */
1293 85162 : rtx prev_set;
1294 85162 : if (prev_insn
1295 82562 : && (prev_set = single_set (prev_insn))
1296 : /* The destination of the left shift must be the
1297 : source of the right shift. */
1298 82484 : && SET_DEST (prev_set) == XEXP (src, 0)
1299 45576 : && GET_CODE (SET_SRC (prev_set)) == ASHIFT
1300 299 : && CONST_INT_P (XEXP (SET_SRC (prev_set), 1))
1301 : /* The counts must match. */
1302 85162 : && (INTVAL (XEXP (src, 1))
1303 285 : == INTVAL (XEXP (SET_SRC (prev_set), 1))))
1304 : {
1305 15 : unsigned HOST_WIDE_INT src_mask = GET_MODE_BITSIZE (GET_MODE (src)).to_constant ();
1306 15 : src_mask -= INTVAL (XEXP (src, 1));
1307 15 : src_mask = (HOST_WIDE_INT_1U << src_mask) - 1;
1308 :
1309 : /* DST_MASK has been adjusted for INSN. We need its original value. */
1310 15 : unsigned HOST_WIDE_INT tmp_mask = 0;
1311 75 : for (int i = 0; i < 4; i++)
1312 60 : if (bitmap_bit_p (live_tmp, 4 * rn + i))
1313 15 : tmp_mask |= mask_array[i];
1314 15 : tmp_mask >>= bit;
1315 :
1316 15 : if (modify && !skipped_dest && (tmp_mask & ~src_mask) == 0)
1317 : {
1318 0 : ext_dce_try_optimize_rshift (insn, x, XEXP (SET_SRC (prev_set), 0), prev_insn);
1319 :
1320 : /* These may not strictly be necessary, but we might as well try and be
1321 : as accurate as possible. The RHS is now a simple REG. */
1322 0 : dst_mask = src_mask;
1323 0 : src = XEXP (SET_SRC (prev_set), 0);
1324 0 : code = GET_CODE (src);
1325 : }
1326 : }
1327 : }
1328 :
1329 : /* Optimization is done at this point. We just want to make
1330 : sure everything that should get marked as live is marked
1331 : from here onward. */
1332 :
1333 : /* We will handle the other operand of a binary operator
1334 : at the bottom of the loop by resetting Y. */
1335 73600464 : if (BINARY_P (src))
1336 23004440 : y = XEXP (src, 0);
1337 : else
1338 : y = src;
1339 :
1340 : /* We're inside a SET and want to process the source operands
1341 : making things live. Breaking from this loop will cause
1342 : the iterator to work on sub-rtxs, so it is safe to break
1343 : if we see something we don't know how to handle.
1344 :
1345 : This code is just hokey as it really just handles trivial
1346 : unary and binary cases. Otherwise the loop exits and we
1347 : continue iterating on sub-rtxs, but outside the set context. */
1348 73600464 : unsigned HOST_WIDE_INT save_mask = dst_mask;
1349 118559062 : for (;;)
1350 : {
1351 : /* In general we want to restore DST_MASK before each loop
1352 : iteration. The exception is when the opcode implies that
1353 : the other operand is fully live. That's handled by
1354 : changing SAVE_MASK below. */
1355 96079763 : dst_mask = save_mask;
1356 : /* Strip an outer paradoxical subreg. The bits outside
1357 : the inner mode are don't cares. So we can just strip
1358 : and process the inner object. */
1359 96079763 : if (paradoxical_subreg_p (y))
1360 : y = XEXP (y, 0);
1361 95978786 : else if (SUBREG_P (y) && subreg_lsb (y).is_constant (&bit))
1362 : {
1363 : /* If !TRULY_NOOP_TRUNCATION_MODES_P, the mode
1364 : change performed by Y would normally need to be a
1365 : TRUNCATE rather than a SUBREG. It is probably the
1366 : guarantee provided by SUBREG_PROMOTED_VAR_P that
1367 : allows the SUBREG in Y as an exception. We must
1368 : therefore preserve that guarantee and treat the
1369 : upper bits of the inner register as live
1370 : regardless of the outer code. See PR 120050. */
1371 1980919 : if (!REG_P (SUBREG_REG (y))
1372 1980919 : || (SUBREG_PROMOTED_VAR_P (y)
1373 14216 : && (!TRULY_NOOP_TRUNCATION_MODES_P (
1374 : GET_MODE (y),
1375 : GET_MODE (SUBREG_REG (y))))))
1376 : break;
1377 :
1378 : /* If this is a wide object (more bits than we can fit
1379 : in a HOST_WIDE_INT), then just break from the SET
1380 : context. That will cause the iterator to walk down
1381 : into the subrtx and if we land on a REG we'll mark
1382 : the whole think live. */
1383 1979825 : if (bit >= HOST_BITS_PER_WIDE_INT)
1384 : break;
1385 :
1386 : /* The SUBREG's mode determines the live width. */
1387 1762117 : if (dst_mask)
1388 : {
1389 1761945 : dst_mask <<= bit;
1390 1761945 : if (!dst_mask)
1391 0 : dst_mask = -HOST_WIDE_INT_UC (0x100000000);
1392 : }
1393 1762117 : y = SUBREG_REG (y);
1394 : }
1395 :
1396 95860961 : if (REG_P (y))
1397 : {
1398 : /* We have found the use of a register. We need to mark
1399 : the appropriate chunks of the register live. The mode
1400 : of the REG is a starting point. We may refine that
1401 : based on what chunks in the output were live. */
1402 51253067 : rn = 4 * REGNO (y);
1403 51253067 : unsigned HOST_WIDE_INT tmp_mask = dst_mask;
1404 :
1405 : /* If the RTX code for the SET_SRC is not one we can
1406 : propagate destination liveness through, then just
1407 : set the mask to the mode's mask. */
1408 51253067 : if (!safe_for_live_propagation (code))
1409 34634 : tmp_mask = GET_MODE_MASK (GET_MODE (y));
1410 :
1411 51253067 : if (tmp_mask & 0xff)
1412 50778706 : bitmap_set_bit (livenow, rn);
1413 51253067 : if (tmp_mask & 0xff00)
1414 48890959 : bitmap_set_bit (livenow, rn + 1);
1415 51253067 : if (tmp_mask & HOST_WIDE_INT_UC (0xffff0000))
1416 48631808 : bitmap_set_bit (livenow, rn + 2);
1417 51253067 : if (tmp_mask & -HOST_WIDE_INT_UC (0x100000000))
1418 42402722 : bitmap_set_bit (livenow, rn + 3);
1419 : }
1420 44607894 : else if (!CONSTANT_P (y))
1421 : break;
1422 :
1423 : /* We might have (ashift (const_int 1) (reg...))
1424 : By setting dst_mask we can continue iterating on the
1425 : the next operand and it will be considered fully live.
1426 :
1427 : Note that since we restore DST_MASK from SAVE_MASK at the
1428 : top of the loop, we have to change SAVE_MASK to get the
1429 : semantics we want. */
1430 78917085 : if (binop_implies_op2_fully_live (GET_CODE (src)))
1431 2573655 : save_mask = -1;
1432 :
1433 : /* If this was anything but a binary operand, break the inner
1434 : loop. This is conservatively correct as it will cause the
1435 : iterator to look at the sub-rtxs outside the SET context. */
1436 78917085 : if (!BINARY_P (src))
1437 : break;
1438 :
1439 : /* We processed the first operand of a binary operator. Now
1440 : handle the second. */
1441 22479299 : y = XEXP (src, 1), src = pc_rtx;
1442 22479299 : }
1443 :
1444 : /* These are leaf nodes, no need to iterate down into them. */
1445 73600464 : if (REG_P (y) || CONSTANT_P (y))
1446 56437786 : iter.skip_subrtxes ();
1447 : }
1448 : }
1449 : /* If we are reading the low part of a SUBREG, then we can
1450 : refine liveness of the input register, otherwise let the
1451 : iterator continue into SUBREG_REG. */
1452 498772510 : else if (SUBREG_P (x)
1453 1470930 : && REG_P (SUBREG_REG (x))
1454 1469016 : && !paradoxical_subreg_p (x)
1455 1440970 : && subreg_lowpart_p (x)
1456 1143693 : && GET_MODE_BITSIZE (GET_MODE (x)).is_constant ()
1457 501059896 : && GET_MODE_BITSIZE (GET_MODE (x)).to_constant () <= 32)
1458 : {
1459 569491 : HOST_WIDE_INT size = GET_MODE_BITSIZE (GET_MODE (x)).to_constant ();
1460 569491 : HOST_WIDE_INT rn = 4 * REGNO (SUBREG_REG (x));
1461 :
1462 : /* If this is a promoted subreg, then more of it may be live than
1463 : is otherwise obvious. */
1464 569491 : if (SUBREG_PROMOTED_VAR_P (x))
1465 8904 : size = GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))).to_constant ();
1466 :
1467 569491 : bitmap_set_bit (livenow, rn);
1468 569491 : if (size > 8)
1469 395014 : bitmap_set_bit (livenow, rn + 1);
1470 395014 : if (size > 16)
1471 353796 : bitmap_set_bit (livenow, rn + 2);
1472 353796 : if (size > 32)
1473 74 : bitmap_set_bit (livenow, rn + 3);
1474 569491 : iter.skip_subrtxes ();
1475 : }
1476 : /* If we have a register reference that is not otherwise handled,
1477 : just assume all the chunks are live. */
1478 498203019 : else if (REG_P (x))
1479 : {
1480 165067161 : if (HARD_REGISTER_P (x))
1481 : {
1482 88389115 : unsigned int end = end_hard_regno (GET_MODE (x), REGNO (x));
1483 177214948 : for (unsigned int r = REGNO (x); r < end; r++)
1484 88825833 : bitmap_set_range (livenow, r * 4, 4);
1485 : }
1486 : else
1487 76678046 : bitmap_set_range (livenow, REGNO (x) * 4, group_limit (x));
1488 : }
1489 : }
1490 141819231 : }
1491 :
1492 : /* Process a single basic block BB with current liveness information
1493 : in LIVENOW, returning updated liveness information.
1494 :
1495 : If MODIFY is true, then this is the last pass and unnecessary
1496 : extensions should be eliminated when possible. If an extension
1497 : is removed, the source pseudo is marked in CHANGED_PSEUDOS. */
1498 :
1499 : static void
1500 23481650 : ext_dce_process_bb (basic_block bb)
1501 : {
1502 23481650 : rtx_insn *insn;
1503 :
1504 318018566 : FOR_BB_INSNS_REVERSE (bb, insn)
1505 : {
1506 294536916 : if (!NONDEBUG_INSN_P (insn))
1507 162546060 : continue;
1508 :
1509 : /* Live-out state of the destination of this insn. We can
1510 : use this to refine the live-in state of the sources of
1511 : this insn in many cases. */
1512 131990856 : bitmap live_tmp = BITMAP_ALLOC (NULL);
1513 :
1514 : /* First process any sets/clobbers in INSN. */
1515 131990856 : bool skipped_dest = ext_dce_process_sets (insn, PATTERN (insn), live_tmp);
1516 :
1517 : /* CALL_INSNs need processing their fusage data. */
1518 131990856 : if (auto call = dyn_cast<rtx_call_insn *> (insn))
1519 9828375 : skipped_dest |= ext_dce_process_sets (call,
1520 : CALL_INSN_FUNCTION_USAGE (call),
1521 : live_tmp);
1522 :
1523 : /* And now uses, optimizing away SIGN/ZERO extensions as we go. */
1524 131990856 : ext_dce_process_uses (insn, PATTERN (insn), live_tmp, skipped_dest);
1525 :
1526 : /* A nonlocal goto implicitly uses the frame pointer. */
1527 131990856 : if (JUMP_P (insn) && find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
1528 : {
1529 1148 : bitmap_set_range (livenow, FRAME_POINTER_REGNUM * 4, 4);
1530 1148 : if (!HARD_FRAME_POINTER_IS_FRAME_POINTER)
1531 1148 : bitmap_set_range (livenow, HARD_FRAME_POINTER_REGNUM * 4, 4);
1532 : }
1533 :
1534 : /* And process fusage data for the use as well. */
1535 131990856 : if (auto call = dyn_cast<rtx_call_insn *> (insn))
1536 : {
1537 9828375 : if (!FAKE_CALL_P (call))
1538 9828315 : bitmap_set_range (livenow, STACK_POINTER_REGNUM * 4, 4);
1539 :
1540 : /* If this is not a call to a const function, then assume it
1541 : can read any global register. */
1542 9828375 : if (!RTL_CONST_CALL_P (call))
1543 904141790 : for (unsigned i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1544 894624508 : if (global_regs[i])
1545 230 : bitmap_set_range (livenow, i * 4, 4);
1546 :
1547 9828375 : ext_dce_process_uses (call, CALL_INSN_FUNCTION_USAGE (call),
1548 : live_tmp, false);
1549 : }
1550 :
1551 131990856 : BITMAP_FREE (live_tmp);
1552 : }
1553 :
1554 23481650 : if (modify)
1555 10024676 : ext_dce_promote_chained_candidates ();
1556 23481650 : }
1557 :
1558 : /* SUBREG_PROMOTED_VAR_P is set by the gimple->rtl optimizers and
1559 : is usually helpful. However, in some cases setting the value when
1560 : it not strictly needed can cause this pass to miss optimizations.
1561 :
1562 : Specifically consider (set (mem) (subreg (reg))). If set in that
1563 : case it will cause more bit groups to be live for REG than would
1564 : be strictly necessary which in turn can inhibit extension removal.
1565 :
1566 : So do a pass over the IL wiping the SUBREG_PROMOTED_VAR_P when it
1567 : is obviously not needed. */
1568 :
1569 : static void
1570 982741 : maybe_clear_subreg_promoted_p (void)
1571 : {
1572 125801030 : for (rtx_insn *insn = get_insns(); insn; insn = NEXT_INSN (insn))
1573 : {
1574 124818289 : if (!NONDEBUG_INSN_P (insn))
1575 68356635 : continue;
1576 :
1577 56461654 : rtx set = single_set (insn);
1578 56461654 : if (!set)
1579 3770557 : continue;
1580 :
1581 : /* There may be other cases where we should clear, but for
1582 : now, this is the only known case where it causes problems. */
1583 52691097 : if (MEM_P (SET_DEST (set)) && SUBREG_P (SET_SRC (set))
1584 87465 : && GET_MODE (SET_DEST (set)) <= GET_MODE (SUBREG_REG (SET_SRC (set))))
1585 77079 : SUBREG_PROMOTED_VAR_P (SET_SRC (set)) = 0;
1586 : }
1587 982741 : }
1588 :
1589 : /* Walk the IL and build the transitive closure of all the REGs tied
1590 : together by copies where either the source or destination is
1591 : marked in CHANGED_PSEUDOS. */
1592 :
1593 : static void
1594 982741 : expand_changed_pseudos (void)
1595 : {
1596 : /* Build a vector of registers related by a copy. This is meant to
1597 : speed up the next step by avoiding full IL walks. */
1598 982741 : struct copy_pair { rtx first; rtx second; };
1599 982741 : auto_vec<copy_pair> pairs;
1600 125801030 : for (rtx_insn *insn = get_insns(); insn; insn = NEXT_INSN (insn))
1601 : {
1602 124818289 : if (!NONDEBUG_INSN_P (insn))
1603 68356635 : continue;
1604 :
1605 56461654 : rtx pat = PATTERN (insn);
1606 :
1607 : /* Simple copies to a REG from another REG or SUBREG of a REG. */
1608 56461654 : if (GET_CODE (pat) == SET
1609 44552745 : && REG_P (SET_DEST (pat))
1610 31781150 : && (REG_P (SET_SRC (pat))
1611 22958488 : || (SUBREG_P (SET_SRC (pat))
1612 398414 : && REG_P (SUBREG_REG (SET_SRC (pat))))))
1613 : {
1614 397960 : rtx src = (REG_P (SET_SRC (pat))
1615 9220622 : ? SET_SRC (pat)
1616 : : SUBREG_REG (SET_SRC (pat)));
1617 9220622 : pairs.safe_push ({ SET_DEST (pat), src });
1618 : }
1619 :
1620 : /* Simple copies to a REG from another REG or SUBREG of a REG
1621 : held inside a PARALLEL. */
1622 56461654 : if (GET_CODE (pat) == PARALLEL)
1623 : {
1624 25654641 : for (int i = XVECLEN (pat, 0) - 1; i >= 0; i--)
1625 : {
1626 17227716 : rtx elem = XVECEXP (pat, 0, i);
1627 :
1628 17227716 : if (GET_CODE (elem) == SET
1629 8675864 : && REG_P (SET_DEST (elem))
1630 8512721 : && (REG_P (SET_SRC (elem))
1631 8512721 : || (SUBREG_P (SET_SRC (elem))
1632 0 : && REG_P (SUBREG_REG (SET_SRC (elem))))))
1633 : {
1634 0 : rtx src = (REG_P (SET_SRC (elem))
1635 0 : ? SET_SRC (elem)
1636 : : SUBREG_REG (SET_SRC (elem)));
1637 0 : pairs.safe_push ({ SET_DEST (elem), src });
1638 : }
1639 : }
1640 8426925 : continue;
1641 8426925 : }
1642 : }
1643 :
1644 : /* Now we have a vector with copy pairs. Iterate over that list
1645 : updating CHANGED_PSEUDOS as we go. Eliminate copies from the
1646 : list as we go as they don't need further processing. */
1647 : bool changed = true;
1648 1965575 : while (changed)
1649 : {
1650 : changed = false;
1651 : unsigned int i;
1652 : copy_pair *p;
1653 11186876 : FOR_EACH_VEC_ELT (pairs, i, p)
1654 : {
1655 9221301 : if (bitmap_bit_p (changed_pseudos, REGNO (p->second))
1656 9221301 : && bitmap_set_bit (changed_pseudos, REGNO (p->first)))
1657 : {
1658 106 : pairs.unordered_remove (i);
1659 106 : changed = true;
1660 : }
1661 : }
1662 : }
1663 982741 : }
1664 :
1665 : /* We optimize away sign/zero extensions in this pass and replace
1666 : them with SUBREGs indicating certain bits are don't cares.
1667 :
1668 : This changes the SUBREG_PROMOTED_VAR_P state of the object.
1669 : It is fairly painful to fix this on the fly, so we have
1670 : recorded which pseudos are affected and we look for SUBREGs
1671 : of those pseudos and fix them up. */
1672 :
1673 : static void
1674 982741 : reset_subreg_promoted_p (void)
1675 : {
1676 : /* This pass eliminates zero/sign extensions on pseudo regs found
1677 : in CHANGED_PSEUDOS. Elimination of those extensions changes if
1678 : the pseudos are known to hold values extended to wider modes
1679 : via SUBREG_PROMOTED_VAR. So we wipe the SUBREG_PROMOTED_VAR
1680 : state on all affected pseudos.
1681 :
1682 : But that is insufficient. We might have a copy from one REG
1683 : to another (possibly with the source register wrapped with a
1684 : SUBREG). We need to wipe SUBREG_PROMOTED_VAR on the transitive
1685 : closure of the original CHANGED_PSEUDOS and registers they're
1686 : connected to via copies. So expand the set. */
1687 982741 : expand_changed_pseudos ();
1688 :
1689 : /* If we removed an extension, that changed the promoted state
1690 : of the destination of that extension. Thus we need to go
1691 : find any SUBREGs that reference that pseudo and adjust their
1692 : SUBREG_PROMOTED_P state. */
1693 125801030 : for (rtx_insn *insn = get_insns(); insn; insn = NEXT_INSN (insn))
1694 : {
1695 124818289 : if (!NONDEBUG_INSN_P (insn))
1696 68356635 : continue;
1697 :
1698 56461654 : rtx pat = PATTERN (insn);
1699 56461654 : subrtx_var_iterator::array_type array;
1700 359964705 : FOR_EACH_SUBRTX_VAR (iter, array, pat, NONCONST)
1701 : {
1702 303503051 : rtx sub = *iter;
1703 :
1704 : /* We only care about SUBREGs. */
1705 303503051 : if (GET_CODE (sub) != SUBREG)
1706 301857470 : continue;
1707 :
1708 1645581 : const_rtx x = SUBREG_REG (sub);
1709 :
1710 : /* We only care if the inner object is a REG. */
1711 1645581 : if (!REG_P (x))
1712 814 : continue;
1713 :
1714 : /* And only if the SUBREG is a promoted var. */
1715 1644767 : if (!SUBREG_PROMOTED_VAR_P (sub))
1716 1637954 : continue;
1717 :
1718 6813 : if (bitmap_bit_p (changed_pseudos, REGNO (x)))
1719 0 : SUBREG_PROMOTED_VAR_P (sub) = 0;
1720 : }
1721 56461654 : }
1722 982741 : }
1723 :
1724 : /* Initialization of the ext-dce pass. Primarily this means
1725 : setting up the various bitmaps we utilize. */
1726 :
1727 : static void
1728 982741 : ext_dce_init (void)
1729 : {
1730 982741 : livein.create (last_basic_block_for_fn (cfun));
1731 982741 : livein.quick_grow_cleared (last_basic_block_for_fn (cfun));
1732 13963594 : for (int i = 0; i < last_basic_block_for_fn (cfun); i++)
1733 11998112 : bitmap_initialize (&livein[i], &bitmap_default_obstack);
1734 :
1735 982741 : auto_bitmap refs (&bitmap_default_obstack);
1736 982741 : df_get_exit_block_use_set (refs);
1737 :
1738 982741 : unsigned i;
1739 982741 : bitmap_iterator bi;
1740 4520863 : EXECUTE_IF_SET_IN_BITMAP (refs, 0, i, bi)
1741 3538122 : make_reg_live (&livein[EXIT_BLOCK], i);
1742 :
1743 982741 : livenow = BITMAP_ALLOC (NULL);
1744 982741 : all_blocks = BITMAP_ALLOC (NULL);
1745 982741 : changed_pseudos = BITMAP_ALLOC (NULL);
1746 982741 : promotable_dests = BITMAP_ALLOC (NULL);
1747 982741 : consumed_by_candidate = BITMAP_ALLOC (NULL);
1748 :
1749 12980853 : for (int i = 0; i < last_basic_block_for_fn (cfun); i++)
1750 11998112 : if (i != ENTRY_BLOCK && i != EXIT_BLOCK)
1751 10032630 : bitmap_set_bit (all_blocks, i);
1752 :
1753 982741 : modify = false;
1754 982741 : }
1755 :
1756 : /* Finalization of the ext-dce pass. Primarily this means
1757 : releasing up the various bitmaps we utilize. */
1758 :
1759 : static void
1760 982741 : ext_dce_finish (void)
1761 : {
1762 12980853 : for (unsigned i = 0; i < livein.length (); i++)
1763 11998112 : bitmap_clear (&livein[i]);
1764 982741 : livein.release ();
1765 :
1766 982741 : BITMAP_FREE (livenow);
1767 982741 : BITMAP_FREE (changed_pseudos);
1768 982741 : BITMAP_FREE (all_blocks);
1769 982741 : BITMAP_FREE (promotable_dests);
1770 982741 : BITMAP_FREE (consumed_by_candidate);
1771 982741 : promotion_candidates.release ();
1772 982741 : promotion_copies.release ();
1773 982741 : }
1774 :
1775 : /* Process block number BB_INDEX as part of the backward
1776 : simple dataflow analysis. Return TRUE if something in
1777 : this block changed or FALSE otherwise. */
1778 :
1779 : static bool
1780 27412614 : ext_dce_rd_transfer_n (int bb_index)
1781 : {
1782 : /* The ENTRY/EXIT blocks never change. */
1783 27412614 : if (bb_index == ENTRY_BLOCK || bb_index == EXIT_BLOCK)
1784 : return false;
1785 :
1786 23481650 : basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
1787 :
1788 : /* Make everything live that's live in the successors. */
1789 23481650 : bitmap_clear (livenow);
1790 23481650 : edge_iterator ei;
1791 23481650 : edge e;
1792 :
1793 59071465 : FOR_EACH_EDGE (e, ei, bb->succs)
1794 35589815 : bitmap_ior_into (livenow, &livein[e->dest->index]);
1795 :
1796 23481650 : ext_dce_process_bb (bb);
1797 :
1798 : /* We only allow widening the set of objects live at the start
1799 : of a block. Otherwise we run the risk of not converging. */
1800 23481650 : return bitmap_ior_into (&livein[bb_index], livenow);
1801 : }
1802 :
1803 : /* Dummy function for the df_simple_dataflow API. */
1804 34201725 : static bool ext_dce_rd_confluence_n (edge) { return true; }
1805 :
1806 : /* Use lifetime analysis to identify extensions that set bits that
1807 : are never read. Turn such extensions into SUBREGs instead which
1808 : can often be propagated away. */
1809 :
1810 : void
1811 982741 : ext_dce_execute (void)
1812 : {
1813 : /* Limit the amount of memory we use for livein, with 4 bits per
1814 : reg per basic-block including overhead that maps to one byte
1815 : per reg per basic-block. */
1816 982741 : uint64_t memory_request
1817 982741 : = (uint64_t)n_basic_blocks_for_fn (cfun) * max_reg_num ();
1818 982741 : if (memory_request / 1024 > (uint64_t)param_max_gcse_memory)
1819 : {
1820 0 : warning (OPT_Wdisabled_optimization,
1821 : "ext-dce disabled: %d basic blocks and %d registers; "
1822 : "increase %<--param max-gcse-memory%> above %wu",
1823 0 : n_basic_blocks_for_fn (cfun), max_reg_num (),
1824 : memory_request / 1024);
1825 0 : return;
1826 : }
1827 :
1828 : /* Some settings of SUBREG_PROMOTED_VAR_P are actively harmful
1829 : to this pass. Clear it for those cases. */
1830 982741 : maybe_clear_subreg_promoted_p ();
1831 982741 : df_analyze ();
1832 982741 : ext_dce_init ();
1833 :
1834 3930964 : do
1835 : {
1836 1965482 : df_simple_dataflow (DF_BACKWARD, NULL, NULL,
1837 : ext_dce_rd_confluence_n, ext_dce_rd_transfer_n,
1838 : all_blocks, df_get_postorder (DF_BACKWARD),
1839 : df_get_n_blocks (DF_BACKWARD));
1840 1965482 : modify = !modify;
1841 : }
1842 : while (modify);
1843 :
1844 982741 : reset_subreg_promoted_p ();
1845 :
1846 982741 : ext_dce_finish ();
1847 : }
1848 :
1849 :
1850 : namespace {
1851 :
1852 : const pass_data pass_data_ext_dce =
1853 : {
1854 : RTL_PASS, /* type */
1855 : "ext_dce", /* name */
1856 : OPTGROUP_NONE, /* optinfo_flags */
1857 : TV_EXT_DCE, /* tv_id */
1858 : PROP_cfglayout, /* properties_required */
1859 : 0, /* properties_provided */
1860 : 0, /* properties_destroyed */
1861 : 0, /* todo_flags_start */
1862 : TODO_df_finish, /* todo_flags_finish */
1863 : };
1864 :
1865 : class pass_ext_dce : public rtl_opt_pass
1866 : {
1867 : public:
1868 297180 : pass_ext_dce (gcc::context *ctxt)
1869 594360 : : rtl_opt_pass (pass_data_ext_dce, ctxt)
1870 : {}
1871 :
1872 : /* opt_pass methods: */
1873 1517135 : virtual bool gate (function *) { return flag_ext_dce && optimize > 0; }
1874 982741 : virtual unsigned int execute (function *)
1875 : {
1876 982741 : ext_dce_execute ();
1877 982741 : return 0;
1878 : }
1879 :
1880 : }; // class pass_combine
1881 :
1882 : } // anon namespace
1883 :
1884 : rtl_opt_pass *
1885 297180 : make_pass_ext_dce (gcc::context *ctxt)
1886 : {
1887 297180 : return new pass_ext_dce (ctxt);
1888 : }
|