Line data Source code
1 : /* Perform simple optimizations to clean up the result of reload.
2 : Copyright (C) 1987-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 "optabs.h"
32 : #include "regs.h"
33 : #include "emit-rtl.h"
34 : #include "recog.h"
35 :
36 : #include "cfghooks.h"
37 : #include "cfgrtl.h"
38 : #include "cfgbuild.h"
39 : #include "cfgcleanup.h"
40 : #include "reload.h"
41 : #include "cselib.h"
42 : #include "tree-pass.h"
43 : #include "dbgcnt.h"
44 : #include "function-abi.h"
45 : #include "rtl-iter.h"
46 :
47 : static bool reload_cse_simplify (rtx_insn *, rtx);
48 : static void reload_cse_regs_1 (void);
49 : static int reload_cse_simplify_set (rtx, rtx_insn *);
50 : static int reload_cse_simplify_operands (rtx_insn *, rtx);
51 :
52 : static void reload_combine (void);
53 : static void reload_combine_note_use (rtx *, rtx_insn *, int, rtx);
54 : static void reload_combine_note_store (rtx, const_rtx, void *);
55 :
56 : static bool reload_cse_move2add (rtx_insn *);
57 : static void move2add_note_store (rtx, const_rtx, void *);
58 :
59 : /* Call cse / combine like post-reload optimization phases.
60 : FIRST is the first instruction. */
61 :
62 : static void
63 1070228 : reload_cse_regs (rtx_insn *first ATTRIBUTE_UNUSED)
64 : {
65 1070228 : bool moves_converted;
66 1070228 : reload_cse_regs_1 ();
67 1070228 : reload_combine ();
68 1070228 : moves_converted = reload_cse_move2add (first);
69 1070228 : if (flag_expensive_optimizations)
70 : {
71 987447 : if (moves_converted)
72 6270 : reload_combine ();
73 987447 : reload_cse_regs_1 ();
74 : }
75 1070228 : }
76 :
77 : /* Try to simplify INSN. Return true if the CFG may have changed. */
78 : static bool
79 221058688 : reload_cse_simplify (rtx_insn *insn, rtx testreg)
80 : {
81 221058688 : rtx body = PATTERN (insn);
82 221058688 : basic_block insn_bb = BLOCK_FOR_INSN (insn);
83 221058688 : unsigned insn_bb_succs = EDGE_COUNT (insn_bb->succs);
84 :
85 : /* If NO_FUNCTION_CSE has been set by the target, then we should not try
86 : to cse function calls. */
87 221058688 : if (NO_FUNCTION_CSE && CALL_P (insn))
88 : return false;
89 :
90 : /* Remember if this insn has been sp += const_int. */
91 211724597 : rtx sp_set = set_for_reg_notes (insn);
92 211724597 : rtx sp_addend = NULL_RTX;
93 211724597 : if (sp_set
94 70700147 : && SET_DEST (sp_set) == stack_pointer_rtx
95 3349433 : && GET_CODE (SET_SRC (sp_set)) == PLUS
96 3318783 : && XEXP (SET_SRC (sp_set), 0) == stack_pointer_rtx
97 3318680 : && CONST_INT_P (XEXP (SET_SRC (sp_set), 1)))
98 211724597 : sp_addend = XEXP (SET_SRC (sp_set), 1);
99 :
100 211724597 : if (GET_CODE (body) == SET)
101 : {
102 91177791 : int count = 0;
103 :
104 : /* Simplify even if we may think it is a no-op.
105 : We may think a memory load of a value smaller than WORD_SIZE
106 : is redundant because we haven't taken into account possible
107 : implicit extension. reload_cse_simplify_set() will bring
108 : this out, so it's safer to simplify before we delete. */
109 91177791 : count += reload_cse_simplify_set (body, insn);
110 :
111 91177791 : if (!count && cselib_redundant_set_p (body))
112 : {
113 241122 : if (check_for_inc_dec (insn))
114 241122 : delete_insn_and_edges (insn);
115 : /* We're done with this insn. */
116 241122 : goto done;
117 : }
118 :
119 90936669 : if (count > 0)
120 160167 : apply_change_group ();
121 : else
122 90776502 : reload_cse_simplify_operands (insn, testreg);
123 : }
124 120546806 : else if (GET_CODE (body) == PARALLEL)
125 : {
126 14615242 : int i;
127 14615242 : int count = 0;
128 14615242 : rtx value = NULL_RTX;
129 :
130 : /* Registers mentioned in the clobber list for an asm cannot be reused
131 : within the body of the asm. Invalidate those registers now so that
132 : we don't try to substitute values for them. */
133 14615242 : if (asm_noperands (body) >= 0)
134 : {
135 588329 : for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
136 : {
137 453754 : rtx part = XVECEXP (body, 0, i);
138 453754 : if (GET_CODE (part) == CLOBBER && REG_P (XEXP (part, 0)))
139 189638 : cselib_invalidate_rtx (XEXP (part, 0));
140 : }
141 : }
142 :
143 : /* If every action in a PARALLEL is a noop, we can delete
144 : the entire PARALLEL. */
145 29222596 : for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
146 : {
147 29218613 : rtx part = XVECEXP (body, 0, i);
148 29218613 : if (GET_CODE (part) == SET)
149 : {
150 14524034 : if (! cselib_redundant_set_p (part))
151 : break;
152 4048 : if (REG_P (SET_DEST (part))
153 4048 : && REG_FUNCTION_VALUE_P (SET_DEST (part)))
154 : {
155 0 : if (value)
156 : break;
157 : value = SET_DEST (part);
158 : }
159 : }
160 14694579 : else if (GET_CODE (part) != CLOBBER && GET_CODE (part) != USE)
161 : break;
162 : }
163 :
164 14615242 : if (i < 0)
165 : {
166 3983 : if (check_for_inc_dec (insn))
167 3983 : delete_insn_and_edges (insn);
168 : /* We're done with this insn. */
169 3983 : goto done;
170 : }
171 :
172 : /* It's not a no-op, but we can try to simplify it. */
173 44648849 : for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
174 30037590 : if (GET_CODE (XVECEXP (body, 0, i)) == SET)
175 15346936 : count += reload_cse_simplify_set (XVECEXP (body, 0, i), insn);
176 :
177 14611259 : if (count > 0)
178 5822 : apply_change_group ();
179 : else
180 14605437 : reload_cse_simplify_operands (insn, testreg);
181 : }
182 :
183 : /* If sp += const_int insn is changed into sp = reg;, add REG_EQUAL
184 : note so that the stack_adjustments pass can undo it if beneficial. */
185 211479492 : if (sp_addend
186 3318680 : && SET_DEST (sp_set) == stack_pointer_rtx
187 3318680 : && REG_P (SET_SRC (sp_set)))
188 3051 : set_dst_reg_note (insn, REG_EQUAL,
189 3051 : gen_rtx_PLUS (Pmode, stack_pointer_rtx,
190 : sp_addend), stack_pointer_rtx);
191 :
192 211476441 : done:
193 423444543 : return (EDGE_COUNT (insn_bb->succs) != insn_bb_succs);
194 : }
195 :
196 : /* Do a very simple CSE pass over the hard registers.
197 :
198 : This function detects no-op moves where we happened to assign two
199 : different pseudo-registers to the same hard register, and then
200 : copied one to the other. Reload will generate a useless
201 : instruction copying a register to itself.
202 :
203 : This function also detects cases where we load a value from memory
204 : into two different registers, and (if memory is more expensive than
205 : registers) changes it to simply copy the first register into the
206 : second register.
207 :
208 : Another optimization is performed that scans the operands of each
209 : instruction to see whether the value is already available in a
210 : hard register. It then replaces the operand with the hard register
211 : if possible, much like an optional reload would. */
212 :
213 : static void
214 2057675 : reload_cse_regs_1 (void)
215 : {
216 2057675 : bool cfg_changed = false;
217 2057675 : basic_block bb;
218 2057675 : rtx_insn *insn;
219 2057675 : rtx testreg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
220 :
221 2057675 : cselib_init (CSELIB_RECORD_MEMORY);
222 2057675 : init_alias_analysis ();
223 :
224 23931385 : FOR_EACH_BB_FN (bb, cfun)
225 : {
226 : /* If BB has a small number of predecessors, see if each of the
227 : has the same implicit set. If so, record that implicit set so
228 : that we can add it to the cselib tables. */
229 21873710 : rtx_insn *implicit_set;
230 :
231 21873710 : implicit_set = NULL;
232 21873710 : if (EDGE_COUNT (bb->preds) <= 3)
233 : {
234 21315367 : edge e;
235 21315367 : edge_iterator ei;
236 21315367 : rtx src = NULL_RTX;
237 21315367 : rtx dest = NULL_RTX;
238 :
239 : /* Iterate over each incoming edge and see if they
240 : all have the same implicit set. */
241 24964963 : FOR_EACH_EDGE (e, ei, bb->preds)
242 : {
243 : /* Skip the entry/exit block. */
244 22045366 : if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
245 : break;
246 :
247 : /* Verify this block ends with a suitable condjump */
248 19987744 : rtx_insn *condjump = BB_END (e->src);
249 19987744 : if (!condjump || ! any_condjump_p (condjump))
250 : break;
251 :
252 : /* get_condition reverses the condition only when the else arm
253 : is a LABEL_REF, so a jump that returns in one arm would give
254 : the condition of the other edge. */
255 15259579 : if (!JUMP_LABEL (condjump) || !LABEL_P (JUMP_LABEL (condjump)))
256 : break;
257 :
258 : /* This predecessor ends with a possible equivalence
259 : producing conditional branch. Extract the condition
260 : and try to use it to create an equivalence. */
261 15259579 : rtx cond = get_condition (condjump, NULL, false, true);
262 15259579 : if (!cond)
263 : break;
264 :
265 13997162 : if ((e->flags & EDGE_FALLTHRU) != 0
266 13997162 : ? GET_CODE (cond) == NE
267 5665430 : : GET_CODE (cond) == EQ)
268 : {
269 : /* If this is the first time through record
270 : the source and destination. */
271 5709477 : if (!dest)
272 : {
273 5537956 : dest = XEXP (cond, 0);
274 5537956 : src = XEXP (cond, 1);
275 : }
276 : /* If this is not the first time through, then
277 : verify the source and destination match. */
278 171521 : else if (rtx_equal_p (dest, XEXP (cond, 0))
279 171521 : && rtx_equal_p (src, XEXP (cond, 1)))
280 : ;
281 : else
282 : break;
283 :
284 : /* A few more checks. First make sure we're dealing with
285 : integer modes, second make sure the values aren't clobbered
286 : by the conditional branch itself. Do this for every
287 : conditional jump participating in creation of the
288 : equivalence. */
289 5609713 : if (!REG_P (dest)
290 3916827 : || !(REG_P (src) || CONST_INT_P (src))
291 3649600 : || GET_MODE_CLASS (GET_MODE (dest)) != MODE_INT
292 3649596 : || reg_set_p (dest, condjump)
293 9259309 : || reg_set_p (src, condjump))
294 : break;
295 :
296 : }
297 : else
298 : break;
299 : }
300 :
301 : /* If all the incoming edges had the same implicit
302 : set, then create a dummy insn for that set.
303 :
304 : It will be entered into the cselib tables before
305 : we process the first real insn in this block. */
306 21315367 : if (dest && ei_end_p (ei))
307 : {
308 2919597 : rtx_insn *i;
309 2919597 : bool useful = false;
310 22059722 : FOR_BB_INSNS (bb, i)
311 : {
312 20338206 : if (!NONDEBUG_INSN_P (i))
313 12228473 : continue;
314 8109733 : rtx s = single_set (i);
315 8109733 : if (s
316 7701184 : && rtx_equal_p (SET_DEST (s), dest)
317 8629600 : && rtx_equal_p (SET_SRC (s), src))
318 : {
319 : useful = true;
320 : break;
321 : }
322 8086566 : if (reg_set_p (dest, i))
323 : break;
324 : }
325 2919597 : if (useful)
326 23167 : implicit_set = make_insn_raw (gen_rtx_SET (dest, src));
327 : }
328 : }
329 :
330 288538206 : FOR_BB_INSNS (bb, insn)
331 : {
332 266664496 : if (INSN_P (insn))
333 : {
334 : /* If we recorded an implicit set, enter it
335 : into the tables before the first real insn.
336 :
337 : We have to do it this way because a CODE_LABEL
338 : will flush the cselib tables. */
339 221058688 : if (implicit_set)
340 : {
341 23167 : cselib_process_insn (implicit_set);
342 23167 : implicit_set = NULL;
343 : }
344 221058688 : cfg_changed |= reload_cse_simplify (insn, testreg);
345 : }
346 :
347 266664496 : cselib_process_insn (insn);
348 : }
349 : }
350 :
351 : /* Clean up. */
352 2057675 : end_alias_analysis ();
353 2057675 : cselib_finish ();
354 2057675 : if (cfg_changed)
355 85 : cleanup_cfg (0);
356 2057675 : }
357 :
358 : /* Add to LOCS the hard registers that hold the contents of MEM, a memory
359 : reference, in other modes of the same size. Express each register
360 : in the mode of MEM. */
361 :
362 : static void
363 15653733 : reload_cse_add_mem_regs (rtx mem, vec<rtx> &locs)
364 : {
365 15653733 : auto_vec<cselib_val *, 4> values;
366 15653733 : cselib_lookup_mem_other_modes (mem, values);
367 46986437 : for (cselib_val *v : values)
368 88337 : for (elt_loc_list *l = v->locs; l; l = l->next)
369 63099 : if (REG_P (l->loc)
370 : /* The target must allow the bitcast in the register. */
371 63099 : && REG_CAN_CHANGE_MODE_P (REGNO (l->loc), GET_MODE (l->loc),
372 : GET_MODE (mem)))
373 : {
374 26954 : rtx reg = simplify_subreg (GET_MODE (mem), l->loc,
375 13477 : GET_MODE (l->loc), 0);
376 13477 : if (reg && REG_P (reg))
377 12750 : locs.safe_push (reg);
378 : }
379 15653733 : }
380 :
381 : /* Try to simplify a single SET instruction. SET is the set pattern.
382 : INSN is the instruction it came from.
383 : This function only handles one case: if we set a register to a value
384 : which is not a register, we try to find that value in some other register
385 : and change the set into a register copy. */
386 :
387 : static int
388 106524727 : reload_cse_simplify_set (rtx set, rtx_insn *insn)
389 : {
390 106524727 : int did_change = 0;
391 106524727 : int dreg;
392 106524727 : rtx src;
393 106524727 : reg_class_t dclass;
394 106524727 : int old_cost;
395 106524727 : cselib_val *val;
396 106524727 : struct elt_loc_list *l;
397 106524727 : enum rtx_code extend_op = UNKNOWN;
398 106524727 : bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn));
399 :
400 106524727 : dreg = true_regnum (SET_DEST (set));
401 106524727 : if (dreg < 0)
402 : return 0;
403 :
404 72031649 : src = SET_SRC (set);
405 72031649 : if (side_effects_p (src) || true_regnum (src) >= 0)
406 : return 0;
407 :
408 60616820 : dclass = REGNO_REG_CLASS (dreg);
409 :
410 : /* When replacing a memory with a register, we need to honor assumptions
411 : that combine made wrt the contents of sign bits. We'll do this by
412 : generating an extend instruction instead of a reg->reg copy. Thus
413 : the destination must be a register that we can widen. */
414 60616820 : if (MEM_P (src)
415 : && (extend_op = load_extend_op (GET_MODE (src))) != UNKNOWN
416 : && !REG_P (SET_DEST (set)))
417 : return 0;
418 :
419 60616820 : val = cselib_lookup (src, GET_MODE (SET_DEST (set)), 0, VOIDmode);
420 60616820 : auto_vec<rtx, 8> locs;
421 60616820 : if (val)
422 11603641 : for (l = val->locs; l; l = l->next)
423 5797782 : locs.safe_push (l->loc);
424 60616820 : if (MEM_P (src))
425 15653733 : reload_cse_add_mem_regs (src, locs);
426 121233640 : if (locs.is_empty ())
427 : return 0;
428 :
429 : /* If memory loads are cheaper than register copies, don't change them. */
430 4306752 : if (MEM_P (src))
431 657470 : old_cost = memory_move_cost (GET_MODE (src), dclass, true);
432 3649282 : else if (REG_P (src))
433 0 : old_cost = register_move_cost (GET_MODE (src),
434 0 : REGNO_REG_CLASS (REGNO (src)), dclass);
435 : else
436 3649282 : old_cost = set_src_cost (src, GET_MODE (SET_DEST (set)), speed);
437 :
438 18730788 : for (rtx this_rtx : locs)
439 : {
440 5810532 : int this_cost;
441 :
442 5810532 : if (CONSTANT_P (this_rtx) && ! references_value_p (this_rtx))
443 : {
444 2012239 : if (extend_op != UNKNOWN)
445 : {
446 : wide_int result;
447 :
448 : if (!CONST_SCALAR_INT_P (this_rtx))
449 : continue;
450 :
451 : switch (extend_op)
452 : {
453 : case ZERO_EXTEND:
454 : result = wide_int::from (rtx_mode_t (this_rtx,
455 : GET_MODE (src)),
456 : BITS_PER_WORD, UNSIGNED);
457 : break;
458 : case SIGN_EXTEND:
459 : result = wide_int::from (rtx_mode_t (this_rtx,
460 : GET_MODE (src)),
461 : BITS_PER_WORD, SIGNED);
462 : break;
463 : default:
464 : gcc_unreachable ();
465 : }
466 : this_rtx = immed_wide_int_const (result, word_mode);
467 : }
468 :
469 2012239 : this_cost = set_src_cost (this_rtx, GET_MODE (SET_DEST (set)), speed);
470 : }
471 3798293 : else if (REG_P (this_rtx))
472 : {
473 852828 : if (extend_op != UNKNOWN)
474 : {
475 : this_rtx = gen_rtx_fmt_e (extend_op, word_mode, this_rtx);
476 : this_cost = set_src_cost (this_rtx, word_mode, speed);
477 : }
478 : else
479 852828 : this_cost = register_move_cost (GET_MODE (this_rtx),
480 852828 : REGNO_REG_CLASS (REGNO (this_rtx)),
481 : dclass);
482 : }
483 : else
484 2945465 : continue;
485 :
486 : /* If equal costs, prefer registers over anything else. That
487 : tends to lead to smaller instructions on some machines. */
488 2865067 : if (this_cost < old_cost
489 2699926 : || (this_cost == old_cost
490 2019381 : && REG_P (this_rtx)
491 14974 : && !REG_P (SET_SRC (set))))
492 : {
493 166239 : if (extend_op != UNKNOWN
494 : && REG_CAN_CHANGE_MODE_P (REGNO (SET_DEST (set)),
495 : GET_MODE (SET_DEST (set)), word_mode))
496 : {
497 : rtx wide_dest = gen_rtx_REG (word_mode, REGNO (SET_DEST (set)));
498 : ORIGINAL_REGNO (wide_dest) = ORIGINAL_REGNO (SET_DEST (set));
499 : validate_change (insn, &SET_DEST (set), wide_dest, 1);
500 : }
501 :
502 166239 : validate_unshare_change (insn, &SET_SRC (set), this_rtx, 1);
503 166239 : old_cost = this_cost, did_change = 1;
504 : }
505 : }
506 :
507 : return did_change;
508 60616820 : }
509 :
510 : /* Try to replace operands in INSN with equivalent values that are already
511 : in registers. This can be viewed as optional reloading.
512 :
513 : For each non-register operand in the insn, see if any hard regs are
514 : known to be equivalent to that operand. Record the alternatives which
515 : can accept these hard registers. Among all alternatives, select the
516 : ones which are better or equal to the one currently matching, where
517 : "better" is in terms of '?' and '!' constraints. Among the remaining
518 : alternatives, select the one which replaces most operands with
519 : hard registers. */
520 :
521 : static int
522 105381939 : reload_cse_simplify_operands (rtx_insn *insn, rtx testreg)
523 : {
524 105381939 : int i, j;
525 :
526 : /* For each operand, all registers that are equivalent to it. */
527 105381939 : HARD_REG_SET equiv_regs[MAX_RECOG_OPERANDS];
528 :
529 105381939 : const char *constraints[MAX_RECOG_OPERANDS];
530 :
531 : /* Vector recording how bad an alternative is. */
532 105381939 : int *alternative_reject;
533 : /* Vector recording how many registers can be introduced by choosing
534 : this alternative. */
535 105381939 : int *alternative_nregs;
536 : /* Array of vectors recording, for each operand and each alternative,
537 : which hard register to substitute, or -1 if the operand should be
538 : left as it is. */
539 105381939 : int *op_alt_regno[MAX_RECOG_OPERANDS];
540 : /* Array of alternatives, sorted in order of decreasing desirability. */
541 105381939 : int *alternative_order;
542 :
543 105381939 : extract_constrain_insn (insn);
544 :
545 105381939 : if (recog_data.n_alternatives == 0 || recog_data.n_operands == 0)
546 : return 0;
547 :
548 91168586 : alternative_reject = XALLOCAVEC (int, recog_data.n_alternatives);
549 91168586 : alternative_nregs = XALLOCAVEC (int, recog_data.n_alternatives);
550 91168586 : alternative_order = XALLOCAVEC (int, recog_data.n_alternatives);
551 91168586 : memset (alternative_reject, 0, recog_data.n_alternatives * sizeof (int));
552 91168586 : memset (alternative_nregs, 0, recog_data.n_alternatives * sizeof (int));
553 :
554 : /* For each operand, find out which regs are equivalent. */
555 294937598 : for (i = 0; i < recog_data.n_operands; i++)
556 : {
557 : cselib_val *v;
558 : struct elt_loc_list *l;
559 : rtx op;
560 :
561 203769012 : CLEAR_HARD_REG_SET (equiv_regs[i]);
562 :
563 : /* cselib blows up on CODE_LABELs. Trying to fix that doesn't seem
564 : right, so avoid the problem here. Similarly NOTE_INSN_DELETED_LABEL.
565 : Likewise if we have a constant and the insn pattern doesn't tell us
566 : the mode we need. */
567 203769012 : if (LABEL_P (recog_data.operand[i])
568 203753894 : || (NOTE_P (recog_data.operand[i])
569 116 : && NOTE_KIND (recog_data.operand[i]) == NOTE_INSN_DELETED_LABEL)
570 203753778 : || (CONSTANT_P (recog_data.operand[i])
571 34896840 : && recog_data.operand_mode[i] == VOIDmode))
572 647546 : continue;
573 :
574 203121466 : op = recog_data.operand[i];
575 203121466 : if (MEM_P (op) && load_extend_op (GET_MODE (op)) != UNKNOWN)
576 : {
577 : rtx set = single_set (insn);
578 :
579 : /* We might have multiple sets, some of which do implicit
580 : extension. Punt on this for now. */
581 : if (! set)
582 : continue;
583 : /* If the destination is also a MEM or a STRICT_LOW_PART, no
584 : extension applies.
585 : Also, if there is an explicit extension, we don't have to
586 : worry about an implicit one. */
587 : else if (MEM_P (SET_DEST (set))
588 : || GET_CODE (SET_DEST (set)) == STRICT_LOW_PART
589 : || GET_CODE (SET_SRC (set)) == ZERO_EXTEND
590 : || GET_CODE (SET_SRC (set)) == SIGN_EXTEND)
591 : ; /* Continue ordinary processing. */
592 : /* If the register cannot change mode to word_mode, it follows that
593 : it cannot have been used in word_mode. */
594 : else if (REG_P (SET_DEST (set))
595 : && !REG_CAN_CHANGE_MODE_P (REGNO (SET_DEST (set)),
596 : GET_MODE (SET_DEST (set)),
597 : word_mode))
598 : ; /* Continue ordinary processing. */
599 : /* If this is a straight load, make the extension explicit. */
600 : else if (REG_P (SET_DEST (set))
601 : && recog_data.n_operands == 2
602 : && SET_SRC (set) == op
603 : && SET_DEST (set) == recog_data.operand[1-i])
604 : {
605 : validate_change (insn, recog_data.operand_loc[i],
606 : gen_rtx_fmt_e (load_extend_op (GET_MODE (op)),
607 : word_mode, op),
608 : 1);
609 : validate_change (insn, recog_data.operand_loc[1-i],
610 : gen_rtx_REG (word_mode, REGNO (SET_DEST (set))),
611 : 1);
612 : if (! apply_change_group ())
613 : return 0;
614 : return reload_cse_simplify_operands (insn, testreg);
615 : }
616 : else
617 : /* ??? There might be arithmetic operations with memory that are
618 : safe to optimize, but is it worth the trouble? */
619 : continue;
620 : }
621 :
622 203121466 : if (side_effects_p (op))
623 4692945 : continue;
624 198428521 : v = cselib_lookup (op, recog_data.operand_mode[i], 0, VOIDmode);
625 198428521 : if (! v)
626 131336288 : continue;
627 :
628 199571148 : for (l = v->locs; l; l = l->next)
629 132478915 : if (REG_P (l->loc))
630 69841362 : SET_HARD_REG_BIT (equiv_regs[i], REGNO (l->loc));
631 : }
632 :
633 91168586 : alternative_mask preferred = get_preferred_alternatives (insn);
634 386106184 : for (i = 0; i < recog_data.n_operands; i++)
635 : {
636 203769012 : machine_mode mode;
637 203769012 : int regno;
638 203769012 : const char *p;
639 :
640 203769012 : op_alt_regno[i] = XALLOCAVEC (int, recog_data.n_alternatives);
641 3037037471 : for (j = 0; j < recog_data.n_alternatives; j++)
642 2833268459 : op_alt_regno[i][j] = -1;
643 :
644 203769012 : p = constraints[i] = recog_data.constraints[i];
645 203769012 : mode = recog_data.operand_mode[i];
646 :
647 : /* Add the reject values for each alternative given by the constraints
648 : for this operand. */
649 203769012 : j = 0;
650 8165950051 : while (*p != '\0')
651 : {
652 7962181039 : char c = *p++;
653 7962181039 : if (c == ',')
654 2619509473 : j++;
655 5342671566 : else if (c == '?')
656 641654620 : alternative_reject[j] += 3;
657 4701016946 : else if (c == '!')
658 18324862 : alternative_reject[j] += 300;
659 : }
660 :
661 : /* We won't change operands which are already registers. We
662 : also don't want to modify output operands. */
663 203769012 : regno = true_regnum (recog_data.operand[i]);
664 203769012 : if (regno >= 0
665 81238788 : || constraints[i][0] == '='
666 61112336 : || constraints[i][0] == '+')
667 142773897 : continue;
668 :
669 5794535925 : for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
670 : {
671 5733540810 : enum reg_class rclass = NO_REGS;
672 :
673 5733540810 : if (! TEST_HARD_REG_BIT (equiv_regs[i], regno))
674 5732773685 : continue;
675 :
676 767125 : set_mode_and_regno (testreg, mode, regno);
677 :
678 : /* We found a register equal to this operand. Now look for all
679 : alternatives that can accept this register and have not been
680 : assigned a register they can use yet. */
681 767125 : j = 0;
682 767125 : p = constraints[i];
683 41866633 : for (;;)
684 : {
685 41866633 : char c = *p;
686 :
687 41866633 : switch (c)
688 : {
689 196819 : case 'g':
690 196819 : rclass = reg_class_subunion[rclass][GENERAL_REGS];
691 196819 : break;
692 :
693 26497598 : default:
694 26497598 : rclass
695 26497598 : = (reg_class_subunion
696 26497598 : [rclass]
697 26497598 : [reg_class_for_constraint (lookup_constraint (p))]);
698 26497598 : break;
699 :
700 15172216 : case ',': case '\0':
701 : /* See if REGNO fits this alternative, and set it up as the
702 : replacement register if we don't have one for this
703 : alternative yet and the operand being replaced is not
704 : a cheap CONST_INT. */
705 15172216 : if (op_alt_regno[i][j] == -1
706 15103854 : && TEST_BIT (preferred, j)
707 10290852 : && reg_fits_class_p (testreg, rclass, 0, mode)
708 17685202 : && (!CONST_INT_P (recog_data.operand[i])
709 2387048 : || (set_src_cost (recog_data.operand[i], mode,
710 : optimize_bb_for_speed_p
711 2387048 : (BLOCK_FOR_INSN (insn)))
712 2387048 : > set_src_cost (testreg, mode,
713 : optimize_bb_for_speed_p
714 2387048 : (BLOCK_FOR_INSN (insn))))))
715 : {
716 126626 : alternative_nregs[j]++;
717 126626 : op_alt_regno[i][j] = regno;
718 : }
719 15172216 : j++;
720 15172216 : rclass = NO_REGS;
721 15172216 : break;
722 : }
723 41866633 : p += CONSTRAINT_LEN (c, p);
724 :
725 41866633 : if (c == '\0')
726 : break;
727 : }
728 : }
729 : }
730 :
731 : /* The loop below sets alternative_order[0] but -Wmaybe-uninitialized
732 : can't know that. Clear it here to avoid the warning. */
733 91168586 : alternative_order[0] = 0;
734 91168586 : gcc_assert (!recog_data.n_alternatives
735 : || (which_alternative >= 0
736 : && which_alternative < recog_data.n_alternatives));
737 :
738 : /* Record all alternatives which are better or equal to the currently
739 : matching one in the alternative_order array. */
740 1439630006 : for (i = j = 0; i < recog_data.n_alternatives; i++)
741 1348461420 : if (alternative_reject[i] <= alternative_reject[which_alternative])
742 784863693 : alternative_order[j++] = i;
743 91168586 : recog_data.n_alternatives = j;
744 :
745 : /* Sort it. Given a small number of alternatives, a dumb algorithm
746 : won't hurt too much. */
747 784863693 : for (i = 0; i < recog_data.n_alternatives - 1; i++)
748 : {
749 693695107 : int best = i;
750 693695107 : int best_reject = alternative_reject[alternative_order[i]];
751 693695107 : int best_nregs = alternative_nregs[alternative_order[i]];
752 :
753 4821941211 : for (j = i + 1; j < recog_data.n_alternatives; j++)
754 : {
755 4128246104 : int this_reject = alternative_reject[alternative_order[j]];
756 4128246104 : int this_nregs = alternative_nregs[alternative_order[j]];
757 :
758 4128246104 : if (this_reject < best_reject
759 4121803502 : || (this_reject == best_reject && this_nregs > best_nregs))
760 : {
761 6516196 : best = j;
762 6516196 : best_reject = this_reject;
763 6516196 : best_nregs = this_nregs;
764 : }
765 : }
766 :
767 693695107 : std::swap (alternative_order[best], alternative_order[i]);
768 : }
769 :
770 : /* Substitute the operands as determined by op_alt_regno for the best
771 : alternative. */
772 91168586 : j = alternative_order[0];
773 :
774 294937598 : for (i = 0; i < recog_data.n_operands; i++)
775 : {
776 203769012 : machine_mode mode = recog_data.operand_mode[i];
777 203769012 : if (op_alt_regno[i][j] == -1)
778 203720230 : continue;
779 :
780 48782 : validate_change (insn, recog_data.operand_loc[i],
781 : gen_rtx_REG (mode, op_alt_regno[i][j]), 1);
782 : }
783 :
784 92602591 : for (i = recog_data.n_dups - 1; i >= 0; i--)
785 : {
786 1434005 : int op = recog_data.dup_num[i];
787 1434005 : machine_mode mode = recog_data.operand_mode[op];
788 :
789 1434005 : if (op_alt_regno[op][j] == -1)
790 1433621 : continue;
791 :
792 384 : validate_change (insn, recog_data.dup_loc[i],
793 : gen_rtx_REG (mode, op_alt_regno[op][j]), 1);
794 : }
795 :
796 91168586 : return apply_change_group ();
797 : }
798 :
799 : /* If reload couldn't use reg+reg+offset addressing, try to use reg+reg
800 : addressing now.
801 : This code might also be useful when reload gave up on reg+reg addressing
802 : because of clashes between the return register and INDEX_REG_CLASS. */
803 :
804 : /* The maximum number of uses of a register we can keep track of to
805 : replace them with reg+reg addressing. */
806 : #define RELOAD_COMBINE_MAX_USES 16
807 :
808 : /* Describes a recorded use of a register. */
809 : struct reg_use
810 : {
811 : /* The insn where a register has been used. */
812 : rtx_insn *insn;
813 : /* Points to the memory reference enclosing the use, if any, NULL_RTX
814 : otherwise. */
815 : rtx containing_mem;
816 : /* Location of the register within INSN. */
817 : rtx *usep;
818 : /* The reverse uid of the insn. */
819 : int ruid;
820 : };
821 :
822 : /* If the register is used in some unknown fashion, USE_INDEX is negative.
823 : If it is dead, USE_INDEX is RELOAD_COMBINE_MAX_USES, and STORE_RUID
824 : indicates where it is first set or clobbered.
825 : Otherwise, USE_INDEX is the index of the last encountered use of the
826 : register (which is first among these we have seen since we scan backwards).
827 : USE_RUID indicates the first encountered, i.e. last, of these uses.
828 : If ALL_OFFSETS_MATCH is true, all encountered uses were inside a PLUS
829 : with a constant offset; OFFSET contains this constant in that case.
830 : STORE_RUID is always meaningful if we only want to use a value in a
831 : register in a different place: it denotes the next insn in the insn
832 : stream (i.e. the last encountered) that sets or clobbers the register.
833 : REAL_STORE_RUID is similar, but clobbers are ignored when updating it.
834 : EXPR is the expression used when storing the register. */
835 : static struct
836 : {
837 : struct reg_use reg_use[RELOAD_COMBINE_MAX_USES];
838 : rtx offset;
839 : int use_index;
840 : int store_ruid;
841 : int real_store_ruid;
842 : int use_ruid;
843 : bool all_offsets_match;
844 : rtx expr;
845 : } reg_state[FIRST_PSEUDO_REGISTER];
846 :
847 : /* Reverse linear uid. This is increased in reload_combine while scanning
848 : the instructions from last to first. It is used to set last_label_ruid
849 : and the store_ruid / use_ruid fields in reg_state. */
850 : static int reload_combine_ruid;
851 :
852 : /* The RUID of the last label we encountered in reload_combine. */
853 : static int last_label_ruid;
854 :
855 : /* The RUID of the last jump we encountered in reload_combine. */
856 : static int last_jump_ruid;
857 :
858 : /* The register numbers of the first and last index register. A value of
859 : -1 in LAST_INDEX_REG indicates that we've previously computed these
860 : values and found no suitable index registers. */
861 : static int first_index_reg = -1;
862 : static int last_index_reg;
863 :
864 : #define LABEL_LIVE(LABEL) \
865 : (label_live[CODE_LABEL_NUMBER (LABEL) - min_labelno])
866 :
867 : /* Subroutine of reload_combine_split_ruids, called to fix up a single
868 : ruid pointed to by *PRUID if it is higher than SPLIT_RUID. */
869 :
870 : static inline void
871 176723 : reload_combine_split_one_ruid (int *pruid, int split_ruid)
872 : {
873 176723 : if (*pruid > split_ruid)
874 6965 : (*pruid)++;
875 : }
876 :
877 : /* Called when we insert a new insn in a position we've already passed in
878 : the scan. Examine all our state, increasing all ruids that are higher
879 : than SPLIT_RUID by one in order to make room for a new insn. */
880 :
881 : static void
882 613 : reload_combine_split_ruids (int split_ruid)
883 : {
884 613 : unsigned i;
885 :
886 613 : reload_combine_split_one_ruid (&reload_combine_ruid, split_ruid);
887 613 : reload_combine_split_one_ruid (&last_label_ruid, split_ruid);
888 613 : reload_combine_split_one_ruid (&last_jump_ruid, split_ruid);
889 :
890 58235 : for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
891 : {
892 57622 : int j, idx = reg_state[i].use_index;
893 57622 : reload_combine_split_one_ruid (®_state[i].use_ruid, split_ruid);
894 57622 : reload_combine_split_one_ruid (®_state[i].store_ruid, split_ruid);
895 57622 : reload_combine_split_one_ruid (®_state[i].real_store_ruid,
896 : split_ruid);
897 57622 : if (idx < 0)
898 26210 : continue;
899 33430 : for (j = idx; j < RELOAD_COMBINE_MAX_USES; j++)
900 : {
901 3322 : reload_combine_split_one_ruid (®_state[i].reg_use[j].ruid,
902 : split_ruid);
903 : }
904 : }
905 613 : }
906 :
907 : /* Called when we are about to rescan a previously encountered insn with
908 : reload_combine_note_use after modifying some part of it. This clears all
909 : information about uses in that particular insn. */
910 :
911 : static void
912 5384 : reload_combine_purge_insn_uses (rtx_insn *insn)
913 : {
914 5384 : unsigned i;
915 :
916 511480 : for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
917 : {
918 506096 : int j, k, idx = reg_state[i].use_index;
919 506096 : if (idx < 0)
920 94703 : continue;
921 : j = k = RELOAD_COMBINE_MAX_USES;
922 451888 : while (j-- > idx)
923 : {
924 40495 : if (reg_state[i].reg_use[j].insn != insn)
925 : {
926 34441 : k--;
927 34441 : if (k != j)
928 4099 : reg_state[i].reg_use[k] = reg_state[i].reg_use[j];
929 : }
930 : }
931 411393 : reg_state[i].use_index = k;
932 : }
933 5384 : }
934 :
935 : /* Called when we need to forget about all uses of REGNO after an insn
936 : which is identified by RUID. */
937 :
938 : static void
939 613 : reload_combine_purge_reg_uses_after_ruid (unsigned regno, int ruid)
940 : {
941 613 : int j, k, idx = reg_state[regno].use_index;
942 613 : if (idx < 0)
943 : return;
944 : j = k = RELOAD_COMBINE_MAX_USES;
945 2358 : while (j-- > idx)
946 : {
947 1745 : if (reg_state[regno].reg_use[j].ruid >= ruid)
948 : {
949 729 : k--;
950 729 : if (k != j)
951 585 : reg_state[regno].reg_use[k] = reg_state[regno].reg_use[j];
952 : }
953 : }
954 613 : reg_state[regno].use_index = k;
955 : }
956 :
957 : /* Find the use of REGNO with the ruid that is highest among those
958 : lower than RUID_LIMIT, and return it if it is the only use of this
959 : reg in the insn. Return NULL otherwise. */
960 :
961 : static struct reg_use *
962 3864024 : reload_combine_closest_single_use (unsigned regno, int ruid_limit)
963 : {
964 3864024 : int i, best_ruid = 0;
965 3864024 : int use_idx = reg_state[regno].use_index;
966 3864024 : struct reg_use *retval;
967 :
968 3864024 : if (use_idx < 0)
969 : return NULL;
970 : retval = NULL;
971 3800521 : for (i = use_idx; i < RELOAD_COMBINE_MAX_USES; i++)
972 : {
973 2207036 : struct reg_use *use = reg_state[regno].reg_use + i;
974 2207036 : int this_ruid = use->ruid;
975 2207036 : if (this_ruid >= ruid_limit)
976 810707 : continue;
977 1396329 : if (this_ruid > best_ruid)
978 : {
979 : best_ruid = this_ruid;
980 : retval = use;
981 : }
982 377369 : else if (this_ruid == best_ruid)
983 2207036 : retval = NULL;
984 : }
985 1593485 : if (last_label_ruid >= best_ruid)
986 656320 : return NULL;
987 : return retval;
988 : }
989 :
990 : /* After we've moved an add insn, fix up any debug insns that occur
991 : between the old location of the add and the new location. REG is
992 : the destination register of the add insn; REPLACEMENT is the
993 : SET_SRC of the add. FROM and TO specify the range in which we
994 : should make this change on debug insns. */
995 :
996 : static void
997 815 : fixup_debug_insns (rtx reg, rtx replacement, rtx_insn *from, rtx_insn *to)
998 : {
999 815 : rtx_insn *insn;
1000 6883 : for (insn = from; insn != to; insn = NEXT_INSN (insn))
1001 : {
1002 6068 : rtx t;
1003 :
1004 6068 : if (!DEBUG_BIND_INSN_P (insn))
1005 4799 : continue;
1006 :
1007 1269 : t = INSN_VAR_LOCATION_LOC (insn);
1008 1269 : t = simplify_replace_rtx (t, reg, replacement);
1009 1269 : validate_change (insn, &INSN_VAR_LOCATION_LOC (insn), t, 0);
1010 : }
1011 815 : }
1012 :
1013 : /* Subroutine of reload_combine_recognize_const_pattern. Try to replace REG
1014 : with SRC in the insn described by USE, taking costs into account. Return
1015 : true if we made the replacement. */
1016 :
1017 : static bool
1018 797076 : try_replace_in_use (struct reg_use *use, rtx reg, rtx src)
1019 : {
1020 797076 : rtx_insn *use_insn = use->insn;
1021 797076 : rtx mem = use->containing_mem;
1022 797076 : bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (use_insn));
1023 :
1024 797076 : if (mem != NULL_RTX)
1025 : {
1026 14536 : addr_space_t as = MEM_ADDR_SPACE (mem);
1027 14536 : rtx oldaddr = XEXP (mem, 0);
1028 14536 : rtx newaddr = NULL_RTX;
1029 14536 : int old_cost = address_cost (oldaddr, GET_MODE (mem), as, speed);
1030 14536 : int new_cost;
1031 :
1032 14536 : newaddr = simplify_replace_rtx (oldaddr, reg, src);
1033 14536 : if (memory_address_addr_space_p (GET_MODE (mem), newaddr, as))
1034 : {
1035 4866 : XEXP (mem, 0) = newaddr;
1036 4866 : new_cost = address_cost (newaddr, GET_MODE (mem), as, speed);
1037 4866 : XEXP (mem, 0) = oldaddr;
1038 4866 : if (new_cost <= old_cost
1039 4866 : && validate_change (use_insn,
1040 : &XEXP (mem, 0), newaddr, 0))
1041 : return true;
1042 : }
1043 : }
1044 : else
1045 : {
1046 782540 : rtx new_set = single_set (use_insn);
1047 782540 : if (new_set
1048 781561 : && REG_P (SET_DEST (new_set))
1049 319574 : && GET_CODE (SET_SRC (new_set)) == PLUS
1050 21308 : && REG_P (XEXP (SET_SRC (new_set), 0))
1051 19196 : && CONSTANT_P (XEXP (SET_SRC (new_set), 1)))
1052 : {
1053 1690 : rtx new_src;
1054 1690 : machine_mode mode = GET_MODE (SET_DEST (new_set));
1055 1690 : int old_cost = set_src_cost (SET_SRC (new_set), mode, speed);
1056 :
1057 1690 : gcc_assert (rtx_equal_p (XEXP (SET_SRC (new_set), 0), reg));
1058 1690 : new_src = simplify_replace_rtx (SET_SRC (new_set), reg, src);
1059 :
1060 1690 : if (set_src_cost (new_src, mode, speed) <= old_cost
1061 1690 : && validate_change (use_insn, &SET_SRC (new_set),
1062 : new_src, 0))
1063 : return true;
1064 : }
1065 : }
1066 : return false;
1067 : }
1068 :
1069 : /* Called by reload_combine when scanning INSN. This function tries to detect
1070 : patterns where a constant is added to a register, and the result is used
1071 : in an address.
1072 : Return true if no further processing is needed on INSN; false if it wasn't
1073 : recognized and should be handled normally. */
1074 :
1075 : static bool
1076 62854439 : reload_combine_recognize_const_pattern (rtx_insn *insn)
1077 : {
1078 62854439 : int from_ruid = reload_combine_ruid;
1079 62854439 : rtx set, pat, reg, src, addreg;
1080 62854439 : unsigned int regno;
1081 62854439 : struct reg_use *use;
1082 62854439 : bool must_move_add;
1083 62854439 : rtx_insn *add_moved_after_insn = NULL;
1084 62854439 : int add_moved_after_ruid = 0;
1085 62854439 : int clobbered_regno = -1;
1086 :
1087 62854439 : set = single_set (insn);
1088 62854439 : if (set == NULL_RTX)
1089 : return false;
1090 :
1091 58614618 : reg = SET_DEST (set);
1092 58614618 : src = SET_SRC (set);
1093 58614618 : if (!REG_P (reg)
1094 40020707 : || REG_NREGS (reg) != 1
1095 45625837 : || GET_MODE (reg) != Pmode
1096 81445822 : || reg == stack_pointer_rtx)
1097 : return false;
1098 :
1099 21133280 : regno = REGNO (reg);
1100 :
1101 : /* We look for a REG1 = REG2 + CONSTANT insn, followed by either
1102 : uses of REG1 inside an address, or inside another add insn. If
1103 : possible and profitable, merge the addition into subsequent
1104 : uses. */
1105 21133280 : if (GET_CODE (src) != PLUS
1106 4129113 : || !REG_P (XEXP (src, 0))
1107 3867065 : || !CONSTANT_P (XEXP (src, 1)))
1108 : return false;
1109 :
1110 3181022 : addreg = XEXP (src, 0);
1111 3181022 : must_move_add = rtx_equal_p (reg, addreg);
1112 :
1113 3181022 : pat = PATTERN (insn);
1114 3181022 : if (must_move_add && set != pat)
1115 : {
1116 : /* We have to be careful when moving the add; apart from the
1117 : single_set there may also be clobbers. Recognize one special
1118 : case, that of one clobber alongside the set (likely a clobber
1119 : of the CC register). */
1120 882002 : gcc_assert (GET_CODE (PATTERN (insn)) == PARALLEL);
1121 882002 : if (XVECLEN (pat, 0) != 2 || XVECEXP (pat, 0, 0) != set
1122 882002 : || GET_CODE (XVECEXP (pat, 0, 1)) != CLOBBER
1123 882002 : || !REG_P (XEXP (XVECEXP (pat, 0, 1), 0)))
1124 : return false;
1125 882002 : clobbered_regno = REGNO (XEXP (XVECEXP (pat, 0, 1), 0));
1126 : }
1127 :
1128 3864024 : do
1129 : {
1130 3864024 : use = reload_combine_closest_single_use (regno, from_ruid);
1131 :
1132 3864024 : if (use)
1133 : /* Start the search for the next use from here. */
1134 914291 : from_ruid = use->ruid;
1135 :
1136 1270466 : if (use && GET_MODE (*use->usep) == Pmode)
1137 : {
1138 908178 : bool delete_add = false;
1139 908178 : rtx_insn *use_insn = use->insn;
1140 908178 : int use_ruid = use->ruid;
1141 :
1142 : /* Avoid moving the add insn past a jump. */
1143 908178 : if (must_move_add && use_ruid <= last_jump_ruid)
1144 : break;
1145 :
1146 : /* If the add clobbers another hard reg in parallel, don't move
1147 : it past a real set of this hard reg. */
1148 907945 : if (must_move_add && clobbered_regno >= 0
1149 146643 : && reg_state[clobbered_regno].real_store_ruid >= use_ruid)
1150 : break;
1151 :
1152 868796 : gcc_assert (reg_state[regno].store_ruid <= use_ruid);
1153 : /* Avoid moving a use of ADDREG past a point where it is stored. */
1154 868796 : if (reg_state[REGNO (addreg)].store_ruid > use_ruid)
1155 : break;
1156 :
1157 : /* We also must not move the addition past an insn that sets
1158 : the same register, unless we can combine two add insns. */
1159 797081 : if (must_move_add && reg_state[regno].store_ruid == use_ruid)
1160 : {
1161 30944 : if (use->containing_mem == NULL_RTX)
1162 : delete_add = true;
1163 : else
1164 : break;
1165 : }
1166 :
1167 797076 : if (try_replace_in_use (use, reg, src))
1168 : {
1169 5384 : reload_combine_purge_insn_uses (use_insn);
1170 5384 : reload_combine_note_use (&PATTERN (use_insn), use_insn,
1171 : use_ruid, NULL_RTX);
1172 :
1173 5384 : if (delete_add)
1174 : {
1175 84 : fixup_debug_insns (reg, src, insn, use_insn);
1176 84 : delete_insn (insn);
1177 84 : return true;
1178 : }
1179 5300 : if (must_move_add)
1180 : {
1181 1102 : add_moved_after_insn = use_insn;
1182 1102 : add_moved_after_ruid = use_ruid;
1183 : }
1184 5300 : continue;
1185 : }
1186 : }
1187 : /* If we get here, we couldn't handle this use. */
1188 3747538 : if (must_move_add)
1189 : break;
1190 : }
1191 2879154 : while (use);
1192 :
1193 3180938 : if (!must_move_add || add_moved_after_insn == NULL_RTX)
1194 : /* Process the add normally. */
1195 : return false;
1196 :
1197 613 : fixup_debug_insns (reg, src, insn, add_moved_after_insn);
1198 :
1199 613 : reorder_insns (insn, insn, add_moved_after_insn);
1200 613 : reload_combine_purge_reg_uses_after_ruid (regno, add_moved_after_ruid);
1201 613 : reload_combine_split_ruids (add_moved_after_ruid - 1);
1202 613 : reload_combine_note_use (&PATTERN (insn), insn,
1203 : add_moved_after_ruid, NULL_RTX);
1204 613 : reg_state[regno].store_ruid = add_moved_after_ruid;
1205 :
1206 613 : return true;
1207 : }
1208 :
1209 : /* Called by reload_combine when scanning INSN. Try to detect a pattern we
1210 : can handle and improve. Return true if no further processing is needed on
1211 : INSN; false if it wasn't recognized and should be handled normally. */
1212 :
1213 : static bool
1214 62853742 : reload_combine_recognize_pattern (rtx_insn *insn)
1215 : {
1216 62853742 : rtx set, reg, src;
1217 :
1218 62853742 : set = single_set (insn);
1219 62853742 : if (set == NULL_RTX)
1220 : return false;
1221 :
1222 58613921 : reg = SET_DEST (set);
1223 58613921 : src = SET_SRC (set);
1224 58613921 : if (!REG_P (reg) || REG_NREGS (reg) != 1)
1225 : return false;
1226 :
1227 39295940 : unsigned int regno = REGNO (reg);
1228 39295940 : machine_mode mode = GET_MODE (reg);
1229 :
1230 39295940 : if (reg_state[regno].use_index < 0
1231 39295940 : || reg_state[regno].use_index >= RELOAD_COMBINE_MAX_USES)
1232 : return false;
1233 :
1234 25435575 : for (int i = reg_state[regno].use_index;
1235 46510246 : i < RELOAD_COMBINE_MAX_USES; i++)
1236 : {
1237 26282403 : struct reg_use *use = reg_state[regno].reg_use + i;
1238 26282403 : if (GET_MODE (*use->usep) != mode)
1239 : return false;
1240 : /* Don't try to adjust (use (REGX)). */
1241 25439326 : if (GET_CODE (PATTERN (use->insn)) == USE
1242 25439326 : && &XEXP (PATTERN (use->insn), 0) == use->usep)
1243 : return false;
1244 : }
1245 :
1246 : /* Look for (set (REGX) (CONST_INT))
1247 : (set (REGX) (PLUS (REGX) (REGY)))
1248 : ...
1249 : ... (MEM (REGX)) ...
1250 : and convert it to
1251 : (set (REGZ) (CONST_INT))
1252 : ...
1253 : ... (MEM (PLUS (REGZ) (REGY)))... .
1254 :
1255 : First, check that we have (set (REGX) (PLUS (REGX) (REGY)))
1256 : and that we know all uses of REGX before it dies.
1257 : Also, explicitly check that REGX != REGY; our life information
1258 : does not yet show whether REGY changes in this insn. */
1259 :
1260 20227843 : if (GET_CODE (src) == PLUS
1261 2433818 : && reg_state[regno].all_offsets_match
1262 2167150 : && last_index_reg != -1
1263 2167150 : && REG_P (XEXP (src, 1))
1264 735795 : && rtx_equal_p (XEXP (src, 0), reg)
1265 535485 : && !rtx_equal_p (XEXP (src, 1), reg)
1266 20754865 : && last_label_ruid < reg_state[regno].use_ruid)
1267 : {
1268 491475 : rtx base = XEXP (src, 1);
1269 491475 : rtx_insn *prev = prev_nonnote_nondebug_insn (insn);
1270 491475 : rtx prev_set = prev ? single_set (prev) : NULL_RTX;
1271 491475 : rtx index_reg = NULL_RTX;
1272 491475 : rtx reg_sum = NULL_RTX;
1273 491475 : int i;
1274 :
1275 : /* Now we need to set INDEX_REG to an index register (denoted as
1276 : REGZ in the illustration above) and REG_SUM to the expression
1277 : register+register that we want to use to substitute uses of REG
1278 : (typically in MEMs) with. First check REG and BASE for being
1279 : index registers; we can use them even if they are not dead. */
1280 491475 : if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], regno)
1281 491475 : || TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS],
1282 : REGNO (base)))
1283 : {
1284 : index_reg = reg;
1285 : reg_sum = src;
1286 : }
1287 : else
1288 : {
1289 : /* Otherwise, look for a free index register. Since we have
1290 : checked above that neither REG nor BASE are index registers,
1291 : if we find anything at all, it will be different from these
1292 : two registers. */
1293 8876858 : for (i = first_index_reg; i <= last_index_reg; i++)
1294 : {
1295 8790264 : if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], i)
1296 3025325 : && reg_state[i].use_index == RELOAD_COMBINE_MAX_USES
1297 1686511 : && reg_state[i].store_ruid <= reg_state[regno].use_ruid
1298 1670347 : && (crtl->abi->clobbers_full_reg_p (i)
1299 325156 : || df_regs_ever_live_p (i))
1300 1408284 : && (!frame_pointer_needed || i != HARD_FRAME_POINTER_REGNUM)
1301 1407243 : && !fixed_regs[i] && !global_regs[i]
1302 515899 : && hard_regno_nregs (i, GET_MODE (reg)) == 1
1303 8925893 : && targetm.hard_regno_scratch_ok (i))
1304 : {
1305 135629 : index_reg = gen_rtx_REG (GET_MODE (reg), i);
1306 135629 : reg_sum = gen_rtx_PLUS (GET_MODE (reg), index_reg, base);
1307 135629 : break;
1308 : }
1309 : }
1310 : }
1311 :
1312 : /* Check that PREV_SET is indeed (set (REGX) (CONST_INT)) and that
1313 : (REGY), i.e. BASE, is not clobbered before the last use we'll
1314 : create. */
1315 491475 : if (reg_sum
1316 491475 : && prev_set
1317 398036 : && CONST_INT_P (SET_SRC (prev_set))
1318 9965 : && rtx_equal_p (SET_DEST (prev_set), reg)
1319 491475 : && (reg_state[REGNO (base)].store_ruid
1320 3479 : <= reg_state[regno].use_ruid))
1321 : {
1322 : /* Change destination register and, if necessary, the constant
1323 : value in PREV, the constant loading instruction. */
1324 3373 : validate_change (prev, &SET_DEST (prev_set), index_reg, 1);
1325 3373 : if (reg_state[regno].offset != const0_rtx)
1326 : {
1327 0 : HOST_WIDE_INT c
1328 0 : = trunc_int_for_mode (UINTVAL (SET_SRC (prev_set))
1329 0 : + UINTVAL (reg_state[regno].offset),
1330 0 : GET_MODE (index_reg));
1331 0 : validate_change (prev, &SET_SRC (prev_set), GEN_INT (c), 1);
1332 : }
1333 :
1334 : /* Now for every use of REG that we have recorded, replace REG
1335 : with REG_SUM. */
1336 6824 : for (i = reg_state[regno].use_index;
1337 6824 : i < RELOAD_COMBINE_MAX_USES; i++)
1338 3451 : validate_unshare_change (reg_state[regno].reg_use[i].insn,
1339 : reg_state[regno].reg_use[i].usep,
1340 : /* Each change must have its own
1341 : replacement. */
1342 : reg_sum, 1);
1343 :
1344 3373 : if (apply_change_group ())
1345 : {
1346 118 : struct reg_use *lowest_ruid = NULL;
1347 :
1348 : /* For every new use of REG_SUM, we have to record the use
1349 : of BASE therein, i.e. operand 1. */
1350 258 : for (i = reg_state[regno].use_index;
1351 258 : i < RELOAD_COMBINE_MAX_USES; i++)
1352 : {
1353 140 : struct reg_use *use = reg_state[regno].reg_use + i;
1354 140 : reload_combine_note_use (&XEXP (*use->usep, 1), use->insn,
1355 : use->ruid, use->containing_mem);
1356 140 : if (lowest_ruid == NULL || use->ruid < lowest_ruid->ruid)
1357 140 : lowest_ruid = use;
1358 : }
1359 :
1360 118 : fixup_debug_insns (reg, reg_sum, insn, lowest_ruid->insn);
1361 :
1362 : /* Delete the reg-reg addition. */
1363 118 : delete_insn (insn);
1364 :
1365 118 : if (reg_state[regno].offset != const0_rtx)
1366 : /* Previous REG_EQUIV / REG_EQUAL notes for PREV
1367 : are now invalid. */
1368 0 : remove_reg_equal_equiv_notes (prev);
1369 :
1370 118 : reg_state[regno].use_index = RELOAD_COMBINE_MAX_USES;
1371 118 : return true;
1372 : }
1373 : }
1374 : }
1375 : return false;
1376 : }
1377 :
1378 : static void
1379 1076498 : reload_combine (void)
1380 : {
1381 1076498 : rtx_insn *insn, *prev;
1382 1076498 : basic_block bb;
1383 1076498 : unsigned int r;
1384 1076498 : int min_labelno, n_labels;
1385 1076498 : HARD_REG_SET ever_live_at_start, *label_live;
1386 :
1387 : /* To avoid wasting too much time later searching for an index register,
1388 : determine the minimum and maximum index register numbers. */
1389 1076498 : if (INDEX_REG_CLASS == NO_REGS)
1390 : last_index_reg = -1;
1391 1076498 : else if (first_index_reg == -1 && last_index_reg == 0)
1392 : {
1393 150040 : hard_reg_set_iterator hrsi1;
1394 4801280 : EXECUTE_IF_SET_IN_HARD_REG_SET
1395 : (reg_class_contents[INDEX_REG_CLASS], 0, r, hrsi1)
1396 : {
1397 4651240 : if (first_index_reg == -1)
1398 150040 : first_index_reg = r;
1399 :
1400 4651240 : last_index_reg = r;
1401 : }
1402 :
1403 : /* If no index register is available, we can quit now. Set LAST_INDEX_REG
1404 : to -1 so we'll know to quit early the next time we get here. */
1405 150040 : if (first_index_reg == -1)
1406 : {
1407 0 : last_index_reg = -1;
1408 0 : return;
1409 : }
1410 : }
1411 :
1412 : /* Set up LABEL_LIVE and EVER_LIVE_AT_START. The register lifetime
1413 : information is a bit fuzzy immediately after reload, but it's
1414 : still good enough to determine which registers are live at a jump
1415 : destination. */
1416 1076498 : min_labelno = get_first_label_num ();
1417 1076498 : n_labels = max_label_num () - min_labelno;
1418 1076498 : label_live = XNEWVEC (HARD_REG_SET, n_labels);
1419 1076498 : CLEAR_HARD_REG_SET (ever_live_at_start);
1420 :
1421 12824204 : FOR_EACH_BB_REVERSE_FN (bb, cfun)
1422 : {
1423 11747706 : insn = BB_HEAD (bb);
1424 11747706 : if (LABEL_P (insn))
1425 : {
1426 5476513 : HARD_REG_SET live;
1427 5476513 : bitmap live_in = df_get_live_in (bb);
1428 :
1429 10953026 : REG_SET_TO_HARD_REG_SET (live, live_in);
1430 5476513 : compute_use_by_pseudos (&live, live_in);
1431 5476513 : LABEL_LIVE (insn) = live;
1432 10953026 : ever_live_at_start |= live;
1433 : }
1434 : }
1435 :
1436 : /* Initialize last_label_ruid, reload_combine_ruid and reg_state. */
1437 1076498 : last_label_ruid = last_jump_ruid = reload_combine_ruid = 0;
1438 102267310 : for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
1439 : {
1440 101190812 : reg_state[r].store_ruid = 0;
1441 101190812 : reg_state[r].real_store_ruid = 0;
1442 101190812 : if (fixed_regs[r])
1443 : reg_state[r].use_index = -1;
1444 : else
1445 50056921 : reg_state[r].use_index = RELOAD_COMBINE_MAX_USES;
1446 : }
1447 :
1448 148698550 : for (insn = get_last_insn (); insn; insn = prev)
1449 : {
1450 147622052 : bool control_flow_insn;
1451 147622052 : rtx note;
1452 :
1453 147622052 : prev = PREV_INSN (insn);
1454 :
1455 : /* We cannot do our optimization across labels. Invalidating all the use
1456 : information we have would be costly, so we just note where the label
1457 : is and then later disable any optimization that would cross it. */
1458 147622052 : if (LABEL_P (insn))
1459 5484197 : last_label_ruid = reload_combine_ruid;
1460 142137855 : else if (BARRIER_P (insn))
1461 : {
1462 : /* Crossing a barrier resets all the use information. */
1463 299378820 : for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
1464 296227464 : if (! fixed_regs[r])
1465 141591911 : reg_state[r].use_index = RELOAD_COMBINE_MAX_USES;
1466 : }
1467 138986499 : else if (INSN_P (insn) && volatile_insn_p (PATTERN (insn)))
1468 : /* Optimizations across insns being marked as volatile must be
1469 : prevented. All the usage information is invalidated
1470 : here. */
1471 42581755 : for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
1472 42133526 : if (! fixed_regs[r]
1473 19372941 : && reg_state[r].use_index != RELOAD_COMBINE_MAX_USES)
1474 651773 : reg_state[r].use_index = -1;
1475 :
1476 147622052 : if (! NONDEBUG_INSN_P (insn))
1477 84767613 : continue;
1478 :
1479 62854439 : reload_combine_ruid++;
1480 :
1481 62854439 : control_flow_insn = control_flow_insn_p (insn);
1482 62854439 : if (control_flow_insn)
1483 8989502 : last_jump_ruid = reload_combine_ruid;
1484 :
1485 62854439 : if (reload_combine_recognize_const_pattern (insn)
1486 62854439 : || reload_combine_recognize_pattern (insn))
1487 815 : continue;
1488 :
1489 62853624 : note_stores (insn, reload_combine_note_store, NULL);
1490 :
1491 62853624 : if (auto call = dyn_cast<rtx_call_insn *> (insn))
1492 : {
1493 4977166 : rtx link;
1494 4977166 : HARD_REG_SET used_regs = insn_callee_abi (call).full_reg_clobbers ();
1495 :
1496 4977166 : hard_reg_set_iterator hrsi2;
1497 421477465 : EXECUTE_IF_SET_IN_HARD_REG_SET (used_regs, 0, r, hrsi2)
1498 : {
1499 416500299 : reg_state[r].use_index = RELOAD_COMBINE_MAX_USES;
1500 416500299 : reg_state[r].store_ruid = reload_combine_ruid;
1501 : }
1502 :
1503 15038073 : for (link = CALL_INSN_FUNCTION_USAGE (call); link;
1504 10060907 : link = XEXP (link, 1))
1505 : {
1506 10060907 : rtx setuse = XEXP (link, 0);
1507 10060907 : rtx usage_rtx = XEXP (setuse, 0);
1508 :
1509 10060907 : if (GET_CODE (setuse) == USE && REG_P (usage_rtx))
1510 : {
1511 9079551 : unsigned int end_regno = END_REGNO (usage_rtx);
1512 18216957 : for (unsigned int i = REGNO (usage_rtx); i < end_regno; ++i)
1513 9137406 : reg_state[i].use_index = -1;
1514 : }
1515 : }
1516 : }
1517 :
1518 62853624 : if (control_flow_insn && !ANY_RETURN_P (PATTERN (insn)))
1519 : {
1520 : /* Non-spill registers might be used at the call destination in
1521 : some unknown fashion, so we have to mark the unknown use. */
1522 8989502 : HARD_REG_SET *live;
1523 :
1524 10440056 : if ((condjump_p (insn) || condjump_in_parallel_p (insn))
1525 8989573 : && JUMP_LABEL (insn))
1526 : {
1527 7539019 : if (ANY_RETURN_P (JUMP_LABEL (insn)))
1528 : live = NULL;
1529 : else
1530 7539019 : live = &LABEL_LIVE (JUMP_LABEL (insn));
1531 : }
1532 : else
1533 : live = &ever_live_at_start;
1534 :
1535 7539019 : if (live)
1536 : {
1537 8989502 : hard_reg_set_iterator hrsi3;
1538 54754565 : EXECUTE_IF_SET_IN_HARD_REG_SET (*live, 0, r, hrsi3)
1539 45765063 : reg_state[r].use_index = -1;
1540 : }
1541 : }
1542 :
1543 62853624 : reload_combine_note_use (&PATTERN (insn), insn, reload_combine_ruid,
1544 : NULL_RTX);
1545 :
1546 87379810 : for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1547 : {
1548 24526186 : if (REG_NOTE_KIND (note) == REG_INC && REG_P (XEXP (note, 0)))
1549 : {
1550 0 : int regno = REGNO (XEXP (note, 0));
1551 0 : reg_state[regno].store_ruid = reload_combine_ruid;
1552 0 : reg_state[regno].real_store_ruid = reload_combine_ruid;
1553 0 : reg_state[regno].use_index = -1;
1554 : }
1555 : }
1556 : }
1557 :
1558 1076498 : free (label_live);
1559 : }
1560 :
1561 : /* Check if DST is a register or a subreg of a register; if it is,
1562 : update store_ruid, real_store_ruid and use_index in the reg_state
1563 : structure accordingly. Called via note_stores from reload_combine. */
1564 :
1565 : static void
1566 67376560 : reload_combine_note_store (rtx dst, const_rtx set, void *data ATTRIBUTE_UNUSED)
1567 : {
1568 67376560 : int regno = 0;
1569 67376560 : int i;
1570 67376560 : machine_mode mode = GET_MODE (dst);
1571 :
1572 67376560 : if (GET_CODE (dst) == SUBREG)
1573 : {
1574 5514 : regno = subreg_regno_offset (REGNO (SUBREG_REG (dst)),
1575 2757 : GET_MODE (SUBREG_REG (dst)),
1576 2757 : SUBREG_BYTE (dst),
1577 : GET_MODE (dst));
1578 2757 : dst = SUBREG_REG (dst);
1579 : }
1580 :
1581 : /* Some targets do argument pushes without adding REG_INC notes. */
1582 :
1583 67376560 : if (MEM_P (dst))
1584 : {
1585 11155864 : dst = XEXP (dst, 0);
1586 11155864 : if (GET_CODE (dst) == PRE_INC || GET_CODE (dst) == POST_INC
1587 11155864 : || GET_CODE (dst) == PRE_DEC || GET_CODE (dst) == POST_DEC
1588 9527825 : || GET_CODE (dst) == PRE_MODIFY || GET_CODE (dst) == POST_MODIFY)
1589 : {
1590 1698998 : unsigned int end_regno = END_REGNO (XEXP (dst, 0));
1591 3397996 : for (unsigned int i = REGNO (XEXP (dst, 0)); i < end_regno; ++i)
1592 : {
1593 : /* We could probably do better, but for now mark the register
1594 : as used in an unknown fashion and set/clobbered at this
1595 : insn. */
1596 1698998 : reg_state[i].use_index = -1;
1597 1698998 : reg_state[i].store_ruid = reload_combine_ruid;
1598 1698998 : reg_state[i].real_store_ruid = reload_combine_ruid;
1599 : }
1600 : }
1601 : else
1602 : return;
1603 : }
1604 :
1605 57919694 : if (!REG_P (dst))
1606 : return;
1607 48669029 : regno += REGNO (dst);
1608 :
1609 : /* note_stores might have stripped a STRICT_LOW_PART, so we have to be
1610 : careful with registers / register parts that are not full words.
1611 : Similarly for ZERO_EXTRACT. */
1612 48669029 : if (GET_CODE (SET_DEST (set)) == ZERO_EXTRACT
1613 48667863 : || GET_CODE (SET_DEST (set)) == STRICT_LOW_PART)
1614 : {
1615 9136 : for (i = end_hard_regno (mode, regno) - 1; i >= regno; i--)
1616 : {
1617 4568 : reg_state[i].use_index = -1;
1618 4568 : reg_state[i].store_ruid = reload_combine_ruid;
1619 4568 : reg_state[i].real_store_ruid = reload_combine_ruid;
1620 : }
1621 : }
1622 : else
1623 : {
1624 98061620 : for (i = end_hard_regno (mode, regno) - 1; i >= regno; i--)
1625 : {
1626 49397159 : reg_state[i].store_ruid = reload_combine_ruid;
1627 49397159 : if (GET_CODE (set) == SET)
1628 41641115 : reg_state[i].real_store_ruid = reload_combine_ruid;
1629 49397159 : reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
1630 : }
1631 : }
1632 : }
1633 :
1634 : /* XP points to a piece of rtl that has to be checked for any uses of
1635 : registers.
1636 : *XP is the pattern of INSN, or a part of it.
1637 : Called from reload_combine, and recursively by itself. */
1638 : static void
1639 223599212 : reload_combine_note_use (rtx *xp, rtx_insn *insn, int ruid, rtx containing_mem)
1640 : {
1641 264496962 : rtx x = *xp;
1642 264496962 : enum rtx_code code = x->code;
1643 264496962 : const char *fmt;
1644 264496962 : int i, j;
1645 264496962 : rtx offset = const0_rtx; /* For the REG case below. */
1646 :
1647 264496962 : switch (code)
1648 : {
1649 59571439 : case SET:
1650 59571439 : if (REG_P (SET_DEST (x)))
1651 : {
1652 40897750 : reload_combine_note_use (&SET_SRC (x), insn, ruid, NULL_RTX);
1653 40897750 : return;
1654 : }
1655 : break;
1656 :
1657 664215 : case USE:
1658 : /* If this is the USE of a return value, we can't change it. */
1659 664215 : if (REG_P (XEXP (x, 0)) && REG_FUNCTION_VALUE_P (XEXP (x, 0)))
1660 : {
1661 : /* Mark the return register as used in an unknown fashion. */
1662 573602 : rtx reg = XEXP (x, 0);
1663 573602 : unsigned int end_regno = END_REGNO (reg);
1664 1176046 : for (unsigned int regno = REGNO (reg); regno < end_regno; ++regno)
1665 602444 : reg_state[regno].use_index = -1;
1666 : return;
1667 : }
1668 : break;
1669 :
1670 7806512 : case CLOBBER:
1671 7806512 : if (REG_P (SET_DEST (x)))
1672 : {
1673 : /* No spurious CLOBBERs of pseudo registers may remain. */
1674 7754755 : gcc_assert (REGNO (SET_DEST (x)) < FIRST_PSEUDO_REGISTER);
1675 : return;
1676 : }
1677 : break;
1678 :
1679 23963225 : case PLUS:
1680 : /* We are interested in (plus (reg) (const_int)) . */
1681 23963225 : if (!REG_P (XEXP (x, 0))
1682 21494044 : || !CONST_INT_P (XEXP (x, 1)))
1683 : break;
1684 : offset = XEXP (x, 1);
1685 : x = XEXP (x, 0);
1686 : /* Fall through. */
1687 61900972 : case REG:
1688 61900972 : {
1689 61900972 : int regno = REGNO (x);
1690 61900972 : int use_index;
1691 61900972 : int nregs;
1692 :
1693 : /* No spurious USEs of pseudo registers may remain. */
1694 61900972 : gcc_assert (regno < FIRST_PSEUDO_REGISTER);
1695 :
1696 61900972 : nregs = REG_NREGS (x);
1697 :
1698 : /* We can't substitute into multi-hard-reg uses. */
1699 61900972 : if (nregs > 1)
1700 : {
1701 1572201 : while (--nregs >= 0)
1702 1048134 : reg_state[regno + nregs].use_index = -1;
1703 : return;
1704 : }
1705 :
1706 : /* We may be called to update uses in previously seen insns.
1707 : Don't add uses beyond the last store we saw. */
1708 61376905 : if (ruid < reg_state[regno].store_ruid)
1709 : return;
1710 :
1711 : /* If this register is already used in some unknown fashion, we
1712 : can't do anything.
1713 : If we decrement the index from zero to -1, we can't store more
1714 : uses, so this register becomes used in an unknown fashion. */
1715 61375615 : use_index = --reg_state[regno].use_index;
1716 61375615 : if (use_index < 0)
1717 : return;
1718 :
1719 37163992 : if (use_index == RELOAD_COMBINE_MAX_USES - 1)
1720 : {
1721 : /* This is the first use of this register we have seen since we
1722 : marked it as dead. */
1723 28120046 : reg_state[regno].offset = offset;
1724 28120046 : reg_state[regno].all_offsets_match = true;
1725 28120046 : reg_state[regno].use_ruid = ruid;
1726 : }
1727 : else
1728 : {
1729 9043946 : if (reg_state[regno].use_ruid > ruid)
1730 354 : reg_state[regno].use_ruid = ruid;
1731 :
1732 9043946 : if (! rtx_equal_p (offset, reg_state[regno].offset))
1733 4024738 : reg_state[regno].all_offsets_match = false;
1734 : }
1735 :
1736 37163992 : reg_state[regno].reg_use[use_index].insn = insn;
1737 37163992 : reg_state[regno].reg_use[use_index].ruid = ruid;
1738 37163992 : reg_state[regno].reg_use[use_index].containing_mem = containing_mem;
1739 37163992 : reg_state[regno].reg_use[use_index].usep = xp;
1740 37163992 : return;
1741 : }
1742 :
1743 : case MEM:
1744 153369883 : containing_mem = x;
1745 : break;
1746 :
1747 : default:
1748 : break;
1749 : }
1750 :
1751 : /* Recursively process the components of X. */
1752 153369883 : fmt = GET_RTX_FORMAT (code);
1753 391951179 : for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1754 : {
1755 238581296 : if (fmt[i] == 'e')
1756 139594464 : reload_combine_note_use (&XEXP (x, i), insn, ruid, containing_mem);
1757 98986832 : else if (fmt[i] == 'E')
1758 : {
1759 31334358 : for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1760 21144987 : reload_combine_note_use (&XVECEXP (x, i, j), insn, ruid,
1761 : containing_mem);
1762 : }
1763 : }
1764 : }
1765 :
1766 : /* See if we can reduce the cost of a constant by replacing a move
1767 : with an add. We track situations in which a register is set to a
1768 : constant or to a register plus a constant. */
1769 : /* We cannot do our optimization across labels. Invalidating all the
1770 : information about register contents we have would be costly, so we
1771 : use move2add_last_label_luid to note where the label is and then
1772 : later disable any optimization that would cross it.
1773 : reg_offset[n] / reg_base_reg[n] / reg_symbol_ref[n] / reg_mode[n]
1774 : are only valid if reg_set_luid[n] is greater than
1775 : move2add_last_label_luid.
1776 : For a set that established a new (potential) base register with
1777 : non-constant value, we use move2add_luid from the place where the
1778 : setting insn is encountered; registers based off that base then
1779 : get the same reg_set_luid. Constants all get
1780 : move2add_last_label_luid + 1 as their reg_set_luid. */
1781 : static int reg_set_luid[FIRST_PSEUDO_REGISTER];
1782 :
1783 : /* If reg_base_reg[n] is negative, register n has been set to
1784 : reg_offset[n] or reg_symbol_ref[n] + reg_offset[n] in mode reg_mode[n].
1785 : If reg_base_reg[n] is non-negative, register n has been set to the
1786 : sum of reg_offset[n] and the value of register reg_base_reg[n]
1787 : before reg_set_luid[n], calculated in mode reg_mode[n] .
1788 : For multi-hard-register registers, all but the first one are
1789 : recorded as BLKmode in reg_mode. Setting reg_mode to VOIDmode
1790 : marks it as invalid. */
1791 : static HOST_WIDE_INT reg_offset[FIRST_PSEUDO_REGISTER];
1792 : static int reg_base_reg[FIRST_PSEUDO_REGISTER];
1793 : static rtx reg_symbol_ref[FIRST_PSEUDO_REGISTER];
1794 : static machine_mode reg_mode[FIRST_PSEUDO_REGISTER];
1795 :
1796 : /* move2add_luid is linearly increased while scanning the instructions
1797 : from first to last. It is used to set reg_set_luid in
1798 : reload_cse_move2add and move2add_note_store. */
1799 : static int move2add_luid;
1800 :
1801 : /* move2add_last_label_luid is set whenever a label is found. Labels
1802 : invalidate all previously collected reg_offset data. */
1803 : static int move2add_last_label_luid;
1804 :
1805 : /* ??? We don't know how zero / sign extension is handled, hence we
1806 : can't go from a narrower to a wider mode. */
1807 : #define MODES_OK_FOR_MOVE2ADD(OUTMODE, INMODE) \
1808 : (GET_MODE_SIZE (OUTMODE) == GET_MODE_SIZE (INMODE) \
1809 : || (GET_MODE_SIZE (OUTMODE) <= GET_MODE_SIZE (INMODE) \
1810 : && TRULY_NOOP_TRUNCATION_MODES_P (OUTMODE, INMODE)))
1811 :
1812 : /* Record that REG is being set to a value with the mode of REG. */
1813 :
1814 : static void
1815 54821168 : move2add_record_mode (rtx reg)
1816 : {
1817 54821168 : int regno, nregs;
1818 54821168 : machine_mode mode = GET_MODE (reg);
1819 :
1820 54821168 : if (GET_CODE (reg) == SUBREG)
1821 : {
1822 2699 : regno = subreg_regno (reg);
1823 2699 : nregs = subreg_nregs (reg);
1824 : }
1825 54818469 : else if (REG_P (reg))
1826 : {
1827 54818469 : regno = REGNO (reg);
1828 54818469 : nregs = REG_NREGS (reg);
1829 : }
1830 : else
1831 0 : gcc_unreachable ();
1832 55535723 : for (int i = nregs - 1; i > 0; i--)
1833 714555 : reg_mode[regno + i] = BLKmode;
1834 54821168 : reg_mode[regno] = mode;
1835 54821168 : }
1836 :
1837 : /* Record that REG is being set to the sum of SYM and OFF. */
1838 :
1839 : static void
1840 2212175 : move2add_record_sym_value (rtx reg, rtx sym, rtx off)
1841 : {
1842 2212175 : int regno = REGNO (reg);
1843 :
1844 2212175 : move2add_record_mode (reg);
1845 2212175 : reg_set_luid[regno] = move2add_luid;
1846 2212175 : reg_base_reg[regno] = -1;
1847 2212175 : reg_symbol_ref[regno] = sym;
1848 2212175 : reg_offset[regno] = INTVAL (off);
1849 2212175 : }
1850 :
1851 : /* Check if REGNO contains a valid value in MODE. */
1852 :
1853 : static bool
1854 225938295 : move2add_valid_value_p (int regno, scalar_int_mode mode)
1855 : {
1856 225938295 : if (reg_set_luid[regno] <= move2add_last_label_luid)
1857 : return false;
1858 :
1859 22653906 : if (mode != reg_mode[regno])
1860 : {
1861 12199985 : scalar_int_mode old_mode;
1862 12199985 : if (!is_a <scalar_int_mode> (reg_mode[regno], &old_mode)
1863 17968391 : || !MODES_OK_FOR_MOVE2ADD (mode, old_mode)
1864 557130 : || !REG_CAN_CHANGE_MODE_P (regno, old_mode, mode))
1865 225938295 : return false;
1866 : /* The value loaded into regno in reg_mode[regno] is also valid in
1867 : mode after truncation only if (REG:mode regno) is the lowpart of
1868 : (REG:reg_mode[regno] regno). Now, for big endian, the starting
1869 : regno of the lowpart might be different. */
1870 557130 : poly_int64 s_off = subreg_lowpart_offset (mode, old_mode);
1871 557130 : s_off = subreg_regno_offset (regno, old_mode, s_off, mode);
1872 557130 : if (maybe_ne (s_off, 0))
1873 : /* We could in principle adjust regno, check reg_mode[regno] to be
1874 : BLKmode, and return s_off to the caller (vs. -1 for failure),
1875 : but we currently have no callers that could make use of this
1876 : information. */
1877 : return false;
1878 : }
1879 :
1880 11076819 : for (int i = end_hard_regno (mode, regno) - 1; i > regno; i--)
1881 71903 : if (reg_mode[i] != BLKmode)
1882 : return false;
1883 : return true;
1884 : }
1885 :
1886 : /* This function is called with INSN that sets REG (of mode MODE)
1887 : to (SYM + OFF), while REG is known to already have value (SYM + offset).
1888 : This function tries to change INSN into an add instruction
1889 : (set (REG) (plus (REG) (OFF - offset))) using the known value.
1890 : It also updates the information about REG's known value.
1891 : Return true if we made a change. */
1892 :
1893 : static bool
1894 149786 : move2add_use_add2_insn (scalar_int_mode mode, rtx reg, rtx sym, rtx off,
1895 : rtx_insn *insn)
1896 : {
1897 149786 : rtx set = single_set (insn);
1898 149786 : rtx src = SET_SRC (set);
1899 149786 : int regno = REGNO (reg);
1900 149786 : rtx new_src = gen_int_mode (UINTVAL (off) - reg_offset[regno], mode);
1901 149786 : bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn));
1902 149786 : bool changed = false;
1903 :
1904 : /* (set (reg) (plus (reg) (const_int 0))) is not canonical;
1905 : use (set (reg) (reg)) instead.
1906 : We don't delete this insn, nor do we convert it into a
1907 : note, to avoid losing register notes or the return
1908 : value flag. jump2 already knows how to get rid of
1909 : no-op moves. */
1910 149786 : if (new_src == const0_rtx)
1911 : {
1912 : /* If the constants are different, this is a
1913 : truncation, that, if turned into (set (reg)
1914 : (reg)), would be discarded. Maybe we should
1915 : try a truncMN pattern? */
1916 12720 : if (INTVAL (off) == reg_offset [regno])
1917 10314 : changed = validate_change (insn, &SET_SRC (set), reg, 0);
1918 : }
1919 : else
1920 : {
1921 137066 : struct full_rtx_costs oldcst, newcst;
1922 137066 : rtx tem = gen_rtx_PLUS (mode, reg, new_src);
1923 :
1924 137066 : get_full_set_rtx_cost (set, &oldcst);
1925 137066 : SET_SRC (set) = tem;
1926 137066 : get_full_set_rtx_cost (set, &newcst);
1927 137066 : SET_SRC (set) = src;
1928 :
1929 137066 : if (costs_lt_p (&newcst, &oldcst, speed)
1930 137066 : && have_add2_insn (reg, new_src))
1931 3648 : changed = validate_change (insn, &SET_SRC (set), tem, 0);
1932 133418 : else if (sym == NULL_RTX && mode != BImode)
1933 : {
1934 131492 : scalar_int_mode narrow_mode;
1935 447225 : FOR_EACH_MODE_UNTIL (narrow_mode, mode)
1936 : {
1937 315733 : if (have_insn_for (STRICT_LOW_PART, narrow_mode)
1938 315733 : && ((reg_offset[regno] & ~GET_MODE_MASK (narrow_mode))
1939 233181 : == (INTVAL (off) & ~GET_MODE_MASK (narrow_mode))))
1940 : {
1941 95607 : rtx narrow_reg = gen_lowpart_common (narrow_mode, reg);
1942 95607 : rtx narrow_src = gen_int_mode (INTVAL (off),
1943 : narrow_mode);
1944 95607 : rtx new_set
1945 95607 : = gen_rtx_SET (gen_rtx_STRICT_LOW_PART (VOIDmode,
1946 : narrow_reg),
1947 : narrow_src);
1948 95607 : get_full_set_rtx_cost (new_set, &newcst);
1949 :
1950 : /* We perform this replacement only if NEXT is either a
1951 : naked SET, or else its single_set is the first element
1952 : in a PARALLEL. */
1953 95607 : rtx *setloc = GET_CODE (PATTERN (insn)) == PARALLEL
1954 95607 : ? &XVECEXP (PATTERN (insn), 0, 0) : &PATTERN (insn);
1955 95607 : if (*setloc == set && costs_lt_p (&newcst, &oldcst, speed))
1956 : {
1957 0 : changed = validate_change (insn, setloc, new_set, 0);
1958 0 : if (changed)
1959 : break;
1960 : }
1961 : }
1962 : }
1963 : }
1964 : }
1965 149786 : move2add_record_sym_value (reg, sym, off);
1966 149786 : return changed;
1967 : }
1968 :
1969 :
1970 : /* This function is called with INSN that sets REG (of mode MODE) to
1971 : (SYM + OFF), but REG doesn't have known value (SYM + offset). This
1972 : function tries to find another register which is known to already have
1973 : value (SYM + offset) and change INSN into an add instruction
1974 : (set (REG) (plus (the found register) (OFF - offset))) if such
1975 : a register is found. It also updates the information about
1976 : REG's known value.
1977 : Return true iff we made a change. */
1978 :
1979 : static bool
1980 1962970 : move2add_use_add3_insn (scalar_int_mode mode, rtx reg, rtx sym, rtx off,
1981 : rtx_insn *insn)
1982 : {
1983 1962970 : rtx set = single_set (insn);
1984 1962970 : rtx src = SET_SRC (set);
1985 1962970 : int regno = REGNO (reg);
1986 1962970 : int min_regno = 0;
1987 1962970 : bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn));
1988 1962970 : int i;
1989 1962970 : bool changed = false;
1990 1962970 : struct full_rtx_costs oldcst, newcst, mincst;
1991 1962970 : rtx plus_expr;
1992 :
1993 1962970 : init_costs_to_max (&mincst);
1994 1962970 : get_full_set_rtx_cost (set, &oldcst);
1995 :
1996 1962970 : plus_expr = gen_rtx_PLUS (GET_MODE (reg), reg, const0_rtx);
1997 1962970 : SET_SRC (set) = plus_expr;
1998 :
1999 186481719 : for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2000 184518755 : if (move2add_valid_value_p (i, mode)
2001 3907963 : && reg_base_reg[i] < 0
2002 1546188 : && reg_symbol_ref[i] != NULL_RTX
2003 185576791 : && rtx_equal_p (sym, reg_symbol_ref[i]))
2004 : {
2005 39920 : rtx new_src = gen_int_mode (UINTVAL (off) - reg_offset[i],
2006 19960 : GET_MODE (reg));
2007 : /* (set (reg) (plus (reg) (const_int 0))) is not canonical;
2008 : use (set (reg) (reg)) instead.
2009 : We don't delete this insn, nor do we convert it into a
2010 : note, to avoid losing register notes or the return
2011 : value flag. jump2 already knows how to get rid of
2012 : no-op moves. */
2013 19960 : if (new_src == const0_rtx)
2014 : {
2015 6 : init_costs_to_zero (&mincst);
2016 6 : min_regno = i;
2017 6 : break;
2018 : }
2019 : else
2020 : {
2021 19954 : XEXP (plus_expr, 1) = new_src;
2022 19954 : get_full_set_rtx_cost (set, &newcst);
2023 :
2024 19954 : if (costs_lt_p (&newcst, &mincst, speed))
2025 : {
2026 19402 : mincst = newcst;
2027 19402 : min_regno = i;
2028 : }
2029 : }
2030 : }
2031 1962970 : SET_SRC (set) = src;
2032 :
2033 1962970 : if (costs_lt_p (&mincst, &oldcst, speed))
2034 : {
2035 47 : rtx tem;
2036 :
2037 47 : tem = gen_rtx_REG (GET_MODE (reg), min_regno);
2038 47 : if (i != min_regno)
2039 : {
2040 82 : rtx new_src = gen_int_mode (UINTVAL (off) - reg_offset[min_regno],
2041 41 : GET_MODE (reg));
2042 41 : tem = gen_rtx_PLUS (GET_MODE (reg), tem, new_src);
2043 : }
2044 47 : if (validate_change (insn, &SET_SRC (set), tem, 0))
2045 1962970 : changed = true;
2046 : }
2047 1962970 : reg_set_luid[regno] = move2add_luid;
2048 1962970 : move2add_record_sym_value (reg, sym, off);
2049 1962970 : return changed;
2050 : }
2051 :
2052 : /* Perform any invalidations necessary for INSN. */
2053 :
2054 : static void
2055 110223042 : reload_cse_move2add_invalidate (rtx_insn *insn)
2056 : {
2057 133518149 : for (rtx note = REG_NOTES (insn); note; note = XEXP (note, 1))
2058 : {
2059 23295107 : if (REG_NOTE_KIND (note) == REG_INC
2060 0 : && REG_P (XEXP (note, 0)))
2061 : {
2062 : /* Reset the information about this register. */
2063 0 : int regno = REGNO (XEXP (note, 0));
2064 0 : if (regno < FIRST_PSEUDO_REGISTER)
2065 : {
2066 0 : move2add_record_mode (XEXP (note, 0));
2067 0 : reg_mode[regno] = VOIDmode;
2068 : }
2069 : }
2070 : }
2071 :
2072 : /* There are no REG_INC notes for SP autoinc. */
2073 110223042 : subrtx_var_iterator::array_type array;
2074 561298336 : FOR_EACH_SUBRTX_VAR (iter, array, PATTERN (insn), NONCONST)
2075 : {
2076 451075294 : rtx mem = *iter;
2077 451075294 : if (mem
2078 451075294 : && MEM_P (mem)
2079 28588659 : && GET_RTX_CLASS (GET_CODE (XEXP (mem, 0))) == RTX_AUTOINC)
2080 : {
2081 1683846 : if (XEXP (XEXP (mem, 0), 0) == stack_pointer_rtx)
2082 1683846 : reg_mode[STACK_POINTER_REGNUM] = VOIDmode;
2083 : }
2084 : }
2085 :
2086 110223042 : note_stores (insn, move2add_note_store, insn);
2087 :
2088 : /* If INSN is a conditional branch, we try to extract an
2089 : implicit set out of it. */
2090 110223042 : if (any_condjump_p (insn))
2091 : {
2092 4927679 : rtx cnd = fis_get_condition (insn);
2093 :
2094 4927679 : if (cnd != NULL_RTX
2095 4411928 : && GET_CODE (cnd) == NE
2096 1983777 : && REG_P (XEXP (cnd, 0))
2097 1331869 : && !reg_set_p (XEXP (cnd, 0), insn)
2098 : /* The following two checks, which are also in
2099 : move2add_note_store, are intended to reduce the
2100 : number of calls to gen_rtx_SET to avoid memory
2101 : allocation if possible. */
2102 1331869 : && SCALAR_INT_MODE_P (GET_MODE (XEXP (cnd, 0)))
2103 1331867 : && REG_NREGS (XEXP (cnd, 0)) == 1
2104 6259546 : && CONST_INT_P (XEXP (cnd, 1)))
2105 : {
2106 888749 : rtx implicit_set = gen_rtx_SET (XEXP (cnd, 0), XEXP (cnd, 1));
2107 888749 : move2add_note_store (SET_DEST (implicit_set), implicit_set, insn);
2108 : }
2109 : }
2110 :
2111 : /* If this is a CALL_INSN, all call used registers are stored with
2112 : unknown values. */
2113 115031424 : if (auto call = dyn_cast<rtx_call_insn *> (insn))
2114 : {
2115 4808382 : function_abi callee_abi = insn_callee_abi (call);
2116 461604672 : for (int i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
2117 451987908 : if (reg_mode[i] != VOIDmode
2118 22803832 : && reg_mode[i] != BLKmode
2119 474225620 : && callee_abi.clobbers_reg_p (reg_mode[i], i))
2120 : /* Reset the information about this register. */
2121 8414854 : reg_mode[i] = VOIDmode;
2122 : }
2123 110223042 : }
2124 :
2125 : /* Convert move insns with constant inputs to additions if they are cheaper.
2126 : Return true if any changes were made. */
2127 : static bool
2128 1070228 : reload_cse_move2add (rtx_insn *first)
2129 : {
2130 1070228 : int i;
2131 1070228 : rtx_insn *insn;
2132 1070228 : bool changed = false;
2133 :
2134 101671660 : for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
2135 : {
2136 100601432 : reg_set_luid[i] = 0;
2137 100601432 : reg_offset[i] = 0;
2138 100601432 : reg_base_reg[i] = 0;
2139 100601432 : reg_symbol_ref[i] = NULL_RTX;
2140 100601432 : reg_mode[i] = VOIDmode;
2141 : }
2142 :
2143 1070228 : move2add_last_label_luid = 0;
2144 1070228 : move2add_luid = 2;
2145 142082566 : for (insn = first; insn; insn = NEXT_INSN (insn), move2add_luid++)
2146 : {
2147 141012338 : rtx set;
2148 :
2149 141012338 : if (LABEL_P (insn))
2150 : {
2151 5262154 : move2add_last_label_luid = move2add_luid;
2152 : /* We're going to increment move2add_luid twice after a
2153 : label, so that we can use move2add_last_label_luid + 1 as
2154 : the luid for constants. */
2155 5262154 : move2add_luid++;
2156 141012338 : continue;
2157 : }
2158 135750184 : if (! INSN_P (insn))
2159 23414386 : continue;
2160 112335798 : set = single_set (insn);
2161 : /* For simplicity, we only perform this optimization on
2162 : single-sets. */
2163 112335798 : scalar_int_mode mode;
2164 112335798 : if (set
2165 56072693 : && REG_P (SET_DEST (set))
2166 150662343 : && is_a <scalar_int_mode> (GET_MODE (SET_DEST (set)), &mode))
2167 : {
2168 28502820 : rtx reg = SET_DEST (set);
2169 28502820 : int regno = REGNO (reg);
2170 28502820 : rtx src = SET_SRC (set);
2171 :
2172 : /* Check if we have valid information on the contents of this
2173 : register in the mode of REG. */
2174 28502820 : if (move2add_valid_value_p (regno, mode)
2175 28502820 : && dbg_cnt (cse2_move2add))
2176 : {
2177 : /* Try to transform (set (REGX) (CONST_INT A))
2178 : ...
2179 : (set (REGX) (CONST_INT B))
2180 : to
2181 : (set (REGX) (CONST_INT A))
2182 : ...
2183 : (set (REGX) (plus (REGX) (CONST_INT B-A)))
2184 : or
2185 : (set (REGX) (CONST_INT A))
2186 : ...
2187 : (set (STRICT_LOW_PART (REGX)) (CONST_INT B))
2188 : */
2189 :
2190 4010998 : if (CONST_INT_P (src)
2191 339976 : && reg_base_reg[regno] < 0
2192 150923 : && reg_symbol_ref[regno] == NULL_RTX)
2193 : {
2194 147542 : changed |= move2add_use_add2_insn (mode, reg, NULL_RTX,
2195 : src, insn);
2196 147542 : continue;
2197 : }
2198 :
2199 : /* Try to transform (set (REGX) (REGY))
2200 : (set (REGX) (PLUS (REGX) (CONST_INT A)))
2201 : ...
2202 : (set (REGX) (REGY))
2203 : (set (REGX) (PLUS (REGX) (CONST_INT B)))
2204 : to
2205 : (set (REGX) (REGY))
2206 : (set (REGX) (PLUS (REGX) (CONST_INT A)))
2207 : ...
2208 : (set (REGX) (plus (REGX) (CONST_INT B-A))) */
2209 3863456 : else if (REG_P (src)
2210 496178 : && reg_set_luid[regno] == reg_set_luid[REGNO (src)]
2211 126574 : && reg_base_reg[regno] == reg_base_reg[REGNO (src)]
2212 3990030 : && move2add_valid_value_p (REGNO (src), mode))
2213 : {
2214 93513 : rtx_insn *next = next_nonnote_nondebug_insn (insn);
2215 93513 : rtx set = NULL_RTX;
2216 93513 : if (next)
2217 93501 : set = single_set (next);
2218 93501 : if (set
2219 71891 : && SET_DEST (set) == reg
2220 7575 : && GET_CODE (SET_SRC (set)) == PLUS
2221 676 : && XEXP (SET_SRC (set), 0) == reg
2222 668 : && CONST_INT_P (XEXP (SET_SRC (set), 1)))
2223 : {
2224 415 : rtx src3 = XEXP (SET_SRC (set), 1);
2225 415 : unsigned HOST_WIDE_INT added_offset = UINTVAL (src3);
2226 415 : HOST_WIDE_INT base_offset = reg_offset[REGNO (src)];
2227 415 : HOST_WIDE_INT regno_offset = reg_offset[regno];
2228 415 : rtx new_src =
2229 830 : gen_int_mode (added_offset
2230 415 : + base_offset
2231 415 : - regno_offset,
2232 : mode);
2233 415 : bool success = false;
2234 415 : bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn));
2235 :
2236 415 : if (new_src == const0_rtx)
2237 : /* See above why we create (set (reg) (reg)) here. */
2238 45 : success
2239 45 : = validate_change (next, &SET_SRC (set), reg, 0);
2240 : else
2241 : {
2242 370 : rtx old_src = SET_SRC (set);
2243 370 : struct full_rtx_costs oldcst, newcst;
2244 370 : rtx tem = gen_rtx_PLUS (mode, reg, new_src);
2245 :
2246 370 : get_full_set_rtx_cost (set, &oldcst);
2247 370 : SET_SRC (set) = tem;
2248 370 : get_full_set_src_cost (tem, mode, &newcst);
2249 370 : SET_SRC (set) = old_src;
2250 370 : costs_add_n_insns (&oldcst, 1);
2251 :
2252 370 : rtx *setloc = GET_CODE (PATTERN (next)) == PARALLEL
2253 370 : ? &XVECEXP (PATTERN (next), 0, 0) : &PATTERN (next);
2254 370 : if (*setloc == set
2255 370 : && costs_lt_p (&newcst, &oldcst, speed)
2256 740 : && have_add2_insn (reg, new_src))
2257 : {
2258 370 : rtx newpat = gen_rtx_SET (reg, tem);
2259 370 : success
2260 370 : = validate_change (next, setloc, newpat, 0);
2261 : }
2262 : }
2263 415 : if (success)
2264 415 : delete_insn (insn);
2265 415 : changed |= success;
2266 415 : insn = next;
2267 : /* Make sure to perform any invalidations related to
2268 : NEXT/INSN since we're going to bypass the normal
2269 : flow with the continue below.
2270 :
2271 : Do this before recording the new mode/offset. */
2272 415 : reload_cse_move2add_invalidate (insn);
2273 415 : move2add_record_mode (reg);
2274 415 : reg_offset[regno]
2275 415 : = trunc_int_for_mode (added_offset + base_offset,
2276 : mode);
2277 415 : continue;
2278 415 : }
2279 : }
2280 : }
2281 :
2282 : /* Try to transform
2283 : (set (REGX) (CONST (PLUS (SYMBOL_REF) (CONST_INT A))))
2284 : ...
2285 : (set (REGY) (CONST (PLUS (SYMBOL_REF) (CONST_INT B))))
2286 : to
2287 : (set (REGX) (CONST (PLUS (SYMBOL_REF) (CONST_INT A))))
2288 : ...
2289 : (set (REGY) (CONST (PLUS (REGX) (CONST_INT B-A)))) */
2290 28354863 : if ((GET_CODE (src) == SYMBOL_REF
2291 26453585 : || (GET_CODE (src) == CONST
2292 64188 : && GET_CODE (XEXP (src, 0)) == PLUS
2293 63958 : && GET_CODE (XEXP (XEXP (src, 0), 0)) == SYMBOL_REF
2294 63936 : && CONST_INT_P (XEXP (XEXP (src, 0), 1))))
2295 28418799 : && dbg_cnt (cse2_move2add))
2296 : {
2297 1965214 : rtx sym, off;
2298 :
2299 1965214 : if (GET_CODE (src) == SYMBOL_REF)
2300 : {
2301 1901278 : sym = src;
2302 1901278 : off = const0_rtx;
2303 : }
2304 : else
2305 : {
2306 63936 : sym = XEXP (XEXP (src, 0), 0);
2307 63936 : off = XEXP (XEXP (src, 0), 1);
2308 : }
2309 :
2310 : /* If the reg already contains the value which is sum of
2311 : sym and some constant value, we can use an add2 insn. */
2312 1965214 : if (move2add_valid_value_p (regno, mode)
2313 200803 : && reg_base_reg[regno] < 0
2314 129747 : && reg_symbol_ref[regno] != NULL_RTX
2315 2019252 : && rtx_equal_p (sym, reg_symbol_ref[regno]))
2316 2244 : changed |= move2add_use_add2_insn (mode, reg, sym, off, insn);
2317 :
2318 : /* Otherwise, we have to find a register whose value is sum
2319 : of sym and some constant value. */
2320 : else
2321 1962970 : changed |= move2add_use_add3_insn (mode, reg, sym, off, insn);
2322 :
2323 1965214 : continue;
2324 1965214 : }
2325 : }
2326 110222627 : reload_cse_move2add_invalidate (insn);
2327 : }
2328 1070228 : return changed;
2329 : }
2330 :
2331 : /* SET is a SET or CLOBBER that sets DST. DATA is the insn which
2332 : contains SET.
2333 : Update reg_set_luid, reg_offset and reg_base_reg accordingly.
2334 : Called from reload_cse_move2add via note_stores. */
2335 :
2336 : static void
2337 63237413 : move2add_note_store (rtx dst, const_rtx set, void *data)
2338 : {
2339 63237413 : rtx_insn *insn = (rtx_insn *) data;
2340 63237413 : unsigned int regno = 0;
2341 63237413 : scalar_int_mode mode;
2342 :
2343 63237413 : if (GET_CODE (dst) == SUBREG)
2344 2699 : regno = subreg_regno (dst);
2345 63234714 : else if (REG_P (dst))
2346 45381967 : regno = REGNO (dst);
2347 : else
2348 : return;
2349 :
2350 45384666 : if (!is_a <scalar_int_mode> (GET_MODE (dst), &mode))
2351 17332468 : goto invalidate;
2352 :
2353 28052198 : if (GET_CODE (set) == SET)
2354 : {
2355 27914641 : rtx note, sym = NULL_RTX;
2356 27914641 : rtx off;
2357 :
2358 27914641 : note = find_reg_equal_equiv_note (insn);
2359 27914641 : if (note && GET_CODE (XEXP (note, 0)) == SYMBOL_REF)
2360 : {
2361 89045 : sym = XEXP (note, 0);
2362 89045 : off = const0_rtx;
2363 : }
2364 4317190 : else if (note && GET_CODE (XEXP (note, 0)) == CONST
2365 10608 : && GET_CODE (XEXP (XEXP (note, 0), 0)) == PLUS
2366 10396 : && GET_CODE (XEXP (XEXP (XEXP (note, 0), 0), 0)) == SYMBOL_REF
2367 10374 : && CONST_INT_P (XEXP (XEXP (XEXP (note, 0), 0), 1)))
2368 : {
2369 : sym = XEXP (XEXP (XEXP (note, 0), 0), 0);
2370 : off = XEXP (XEXP (XEXP (note, 0), 0), 1);
2371 : }
2372 :
2373 99419 : if (sym != NULL_RTX)
2374 : {
2375 99419 : move2add_record_sym_value (dst, sym, off);
2376 99419 : return;
2377 : }
2378 : }
2379 :
2380 27952779 : if (GET_CODE (set) == SET
2381 27815222 : && GET_CODE (SET_DEST (set)) != ZERO_EXTRACT
2382 27814125 : && GET_CODE (SET_DEST (set)) != STRICT_LOW_PART)
2383 : {
2384 27810859 : rtx src = SET_SRC (set);
2385 27810859 : rtx base_reg;
2386 27810859 : unsigned HOST_WIDE_INT offset;
2387 27810859 : int base_regno;
2388 :
2389 27810859 : switch (GET_CODE (src))
2390 : {
2391 6247506 : case PLUS:
2392 6247506 : if (REG_P (XEXP (src, 0)))
2393 : {
2394 5941024 : base_reg = XEXP (src, 0);
2395 :
2396 5941024 : if (CONST_INT_P (XEXP (src, 1)))
2397 5089960 : offset = UINTVAL (XEXP (src, 1));
2398 851064 : else if (REG_P (XEXP (src, 1))
2399 851064 : && move2add_valid_value_p (REGNO (XEXP (src, 1)), mode))
2400 : {
2401 92411 : if (reg_base_reg[REGNO (XEXP (src, 1))] < 0
2402 92411 : && reg_symbol_ref[REGNO (XEXP (src, 1))] == NULL_RTX)
2403 9589 : offset = reg_offset[REGNO (XEXP (src, 1))];
2404 : /* Maybe the first register is known to be a
2405 : constant. */
2406 82822 : else if (move2add_valid_value_p (REGNO (base_reg), mode)
2407 24327 : && reg_base_reg[REGNO (base_reg)] < 0
2408 83766 : && reg_symbol_ref[REGNO (base_reg)] == NULL_RTX)
2409 : {
2410 790 : offset = reg_offset[REGNO (base_reg)];
2411 790 : base_reg = XEXP (src, 1);
2412 : }
2413 : else
2414 82032 : goto invalidate;
2415 : }
2416 : else
2417 758653 : goto invalidate;
2418 :
2419 : break;
2420 : }
2421 :
2422 306482 : goto invalidate;
2423 :
2424 : case REG:
2425 : base_reg = src;
2426 : offset = 0;
2427 : break;
2428 :
2429 4536806 : case CONST_INT:
2430 : /* Start tracking the register as a constant. */
2431 4536806 : reg_base_reg[regno] = -1;
2432 4536806 : reg_symbol_ref[regno] = NULL_RTX;
2433 4536806 : reg_offset[regno] = INTVAL (SET_SRC (set));
2434 : /* We assign the same luid to all registers set to constants. */
2435 4536806 : reg_set_luid[regno] = move2add_last_label_luid + 1;
2436 4536806 : move2add_record_mode (dst);
2437 4536806 : return;
2438 :
2439 12128654 : default:
2440 12128654 : goto invalidate;
2441 : }
2442 :
2443 9998232 : base_regno = REGNO (base_reg);
2444 : /* If information about the base register is not valid, set it
2445 : up as a new base register, pretending its value is known
2446 : starting from the current insn. */
2447 9998232 : if (!move2add_valid_value_p (base_regno, mode))
2448 : {
2449 7323331 : reg_base_reg[base_regno] = base_regno;
2450 7323331 : reg_symbol_ref[base_regno] = NULL_RTX;
2451 7323331 : reg_offset[base_regno] = 0;
2452 7323331 : reg_set_luid[base_regno] = move2add_luid;
2453 7323331 : gcc_assert (GET_MODE (base_reg) == mode);
2454 7323331 : move2add_record_mode (base_reg);
2455 : }
2456 :
2457 : /* Copy base information from our base register. */
2458 9998232 : reg_set_luid[regno] = reg_set_luid[base_regno];
2459 9998232 : reg_base_reg[regno] = reg_base_reg[base_regno];
2460 9998232 : reg_symbol_ref[regno] = reg_symbol_ref[base_regno];
2461 :
2462 : /* Compute the sum of the offsets or constants. */
2463 9998232 : reg_offset[regno]
2464 9998232 : = trunc_int_for_mode (offset + reg_offset[base_regno], mode);
2465 :
2466 9998232 : move2add_record_mode (dst);
2467 9998232 : }
2468 : else
2469 : {
2470 141920 : invalidate:
2471 : /* Invalidate the contents of the register. */
2472 30750209 : move2add_record_mode (dst);
2473 30750209 : reg_mode[regno] = VOIDmode;
2474 : }
2475 : }
2476 :
2477 : namespace {
2478 :
2479 : const pass_data pass_data_postreload_cse =
2480 : {
2481 : RTL_PASS, /* type */
2482 : "postreload", /* name */
2483 : OPTGROUP_NONE, /* optinfo_flags */
2484 : TV_RELOAD_CSE_REGS, /* tv_id */
2485 : 0, /* properties_required */
2486 : 0, /* properties_provided */
2487 : 0, /* properties_destroyed */
2488 : 0, /* todo_flags_start */
2489 : TODO_df_finish, /* todo_flags_finish */
2490 : };
2491 :
2492 : class pass_postreload_cse : public rtl_opt_pass
2493 : {
2494 : public:
2495 302985 : pass_postreload_cse (gcc::context *ctxt)
2496 605970 : : rtl_opt_pass (pass_data_postreload_cse, ctxt)
2497 : {}
2498 :
2499 : /* opt_pass methods: */
2500 1531131 : bool gate (function *) final override
2501 : {
2502 1531131 : return (optimize > 0 && reload_completed);
2503 : }
2504 :
2505 : unsigned int execute (function *) final override;
2506 :
2507 : }; // class pass_postreload_cse
2508 :
2509 : unsigned int
2510 1070228 : pass_postreload_cse::execute (function *fun)
2511 : {
2512 1070228 : if (!dbg_cnt (postreload_cse))
2513 : return 0;
2514 :
2515 : /* Do a very simple CSE pass over just the hard registers. */
2516 1070228 : reload_cse_regs (get_insns ());
2517 : /* Reload_cse_regs can eliminate potentially-trapping MEMs.
2518 : Remove any EH edges associated with them. */
2519 1070228 : if (fun->can_throw_non_call_exceptions
2520 1070228 : && purge_all_dead_edges ())
2521 115 : cleanup_cfg (0);
2522 :
2523 : return 0;
2524 : }
2525 :
2526 : } // anon namespace
2527 :
2528 : rtl_opt_pass *
2529 302985 : make_pass_postreload_cse (gcc::context *ctxt)
2530 : {
2531 302985 : return new pass_postreload_cse (ctxt);
2532 : }
|