Debian Codegraph @

gcc/rtlanal.cc

raw
1 /* Analyze RTL for GNU compiler.
2 Copyright (C) 1987-2025 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
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "target.h"
26 #include "rtl.h"
27 #include "rtlanal.h"
28 #include "tree.h"
29 #include "predict.h"
30 #include "df.h"
31 #include "memmodel.h"
32 #include "tm_p.h"
33 #include "insn-config.h"
34 #include "regs.h"
35 #include "emit-rtl.h" /* FIXME: Can go away once crtl is moved to rtl.h. */
36 #include "recog.h"
37 #include "addresses.h"
38 #include "rtl-iter.h"
39 #include "hard-reg-set.h"
40 #include "function-abi.h"
41
42 /* Forward declarations */
43 static void set_of_1 (rtx, const_rtx, void *);
44 static bool covers_regno_p (const_rtx, unsigned int);
45 static bool covers_regno_no_parallel_p (const_rtx, unsigned int);
46 static bool computed_jump_p_1 (const_rtx);
47 static void parms_set (rtx, const_rtx, void *);
48
49 static unsigned HOST_WIDE_INT cached_nonzero_bits (const_rtx, scalar_int_mode,
50 const_rtx, machine_mode,
51 unsigned HOST_WIDE_INT);
52 static unsigned HOST_WIDE_INT nonzero_bits1 (const_rtx, scalar_int_mode,
53 const_rtx, machine_mode,
54 unsigned HOST_WIDE_INT);
55 static unsigned int cached_num_sign_bit_copies (const_rtx, scalar_int_mode,
56 const_rtx, machine_mode,
57 unsigned int);
58 static unsigned int num_sign_bit_copies1 (const_rtx, scalar_int_mode,
59 const_rtx, machine_mode,
60 unsigned int);
61
62 rtx_subrtx_bound_info rtx_all_subrtx_bounds[NUM_RTX_CODE];
63 rtx_subrtx_bound_info rtx_nonconst_subrtx_bounds[NUM_RTX_CODE];
64
65 /* Truncation narrows the mode from SOURCE mode to DESTINATION mode.
66 If TARGET_MODE_REP_EXTENDED (DESTINATION, DESTINATION_REP) is
67 SIGN_EXTEND then while narrowing we also have to enforce the
68 representation and sign-extend the value to mode DESTINATION_REP.
69
70 If the value is already sign-extended to DESTINATION_REP mode we
71 can just switch to DESTINATION mode on it. For each pair of
72 integral modes SOURCE and DESTINATION, when truncating from SOURCE
73 to DESTINATION, NUM_SIGN_BIT_COPIES_IN_REP[SOURCE][DESTINATION]
74 contains the number of high-order bits in SOURCE that have to be
75 copies of the sign-bit so that we can do this mode-switch to
76 DESTINATION. */
77
78 static unsigned int
79 num_sign_bit_copies_in_rep[MAX_MODE_INT + 1][MAX_MODE_INT + 1];
80
81 /* Store X into index I of ARRAY. ARRAY is known to have at least I
82 elements. Return the new base of ARRAY. */
83
84 template <typename T>
85 typename T::value_type *
86 generic_subrtx_iterator <T>::add_single_to_queue (array_type &array,
87 value_type *base,
88 size_t i, value_type x)
89 {
90 if (base == array.stack)
91 {
92 if (i < LOCAL_ELEMS)
93 {
94 base[i] = x;
95 return base;
96 }
97 gcc_checking_assert (i == LOCAL_ELEMS);
98 /* A previous iteration might also have moved from the stack to the
99 heap, in which case the heap array will already be big enough. */
100 if (vec_safe_length (array.heap) <= i)
101 vec_safe_grow (array.heap, i + 1, true);
102 base = array.heap->address ();
103 memcpy (base, array.stack, sizeof (array.stack));
104 base[LOCAL_ELEMS] = x;
105 return base;
106 }
107 unsigned int length = array.heap->length ();
108 if (length > i)
109 {
110 gcc_checking_assert (base == array.heap->address ());
111 base[i] = x;
112 return base;
113 }
114 else
115 {
116 gcc_checking_assert (i == length);
117 vec_safe_push (array.heap, x);
118 return array.heap->address ();
119 }
120 }
121
122 /* Add the subrtxes of X to worklist ARRAY, starting at END. Return the
123 number of elements added to the worklist. */
124
125 template <typename T>
126 size_t
127 generic_subrtx_iterator <T>::add_subrtxes_to_queue (array_type &array,
128 value_type *base,
129 size_t end, rtx_type x)
130 {
131 enum rtx_code code = GET_CODE (x);
132 const char *format = GET_RTX_FORMAT (code);
133 size_t orig_end = end;
134 if (UNLIKELY (INSN_P (x)))
135 {
136 /* Put the pattern at the top of the queue, since that's what
137 we're likely to want most. It also allows for the SEQUENCE
138 code below. */
139 for (int i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; --i)
140 if (format[i] == 'e')
141 {
142 value_type subx = T::get_value (x->u.fld[i].rt_rtx);
143 if (LIKELY (end < LOCAL_ELEMS))
144 base[end++] = subx;
145 else
146 base = add_single_to_queue (array, base, end++, subx);
147 }
148 }
149 else
150 for (int i = 0; format[i]; ++i)
151 if (format[i] == 'e')
152 {
153 value_type subx = T::get_value (x->u.fld[i].rt_rtx);
154 if (LIKELY (end < LOCAL_ELEMS))
155 base[end++] = subx;
156 else
157 base = add_single_to_queue (array, base, end++, subx);
158 }
159 else if (format[i] == 'E')
160 {
161 unsigned int length = GET_NUM_ELEM (x->u.fld[i].rt_rtvec);
162 rtx *vec = x->u.fld[i].rt_rtvec->elem;
163 if (LIKELY (end + length <= LOCAL_ELEMS))
164 for (unsigned int j = 0; j < length; j++)
165 base[end++] = T::get_value (vec[j]);
166 else
167 for (unsigned int j = 0; j < length; j++)
168 base = add_single_to_queue (array, base, end++,
169 T::get_value (vec[j]));
170 if (code == SEQUENCE && end == length)
171 /* If the subrtxes of the sequence fill the entire array then
172 we know that no other parts of a containing insn are queued.
173 The caller is therefore iterating over the sequence as a
174 PATTERN (...), so we also want the patterns of the
175 subinstructions. */
176 for (unsigned int j = 0; j < length; j++)
177 {
178 typename T::rtx_type x = T::get_rtx (base[j]);
179 if (INSN_P (x))
180 base[j] = T::get_value (PATTERN (x));
181 }
182 }
183 return end - orig_end;
184 }
185
186 template <typename T>
187 void
188 generic_subrtx_iterator <T>::free_array (array_type &array)
189 {
190 vec_free (array.heap);
191 }
192
193 template <typename T>
194 const size_t generic_subrtx_iterator <T>::LOCAL_ELEMS;
195
196 template class generic_subrtx_iterator <const_rtx_accessor>;
197 template class generic_subrtx_iterator <rtx_var_accessor>;
198 template class generic_subrtx_iterator <rtx_ptr_accessor>;
199
200 /* Return true if the value of X is unstable
201 (would be different at a different point in the program).
202 The frame pointer, arg pointer, etc. are considered stable
203 (within one function) and so is anything marked `unchanging'. */
204
205 bool
206 rtx_unstable_p (const_rtx x)
207 {
208 const RTX_CODE code = GET_CODE (x);
209 int i;
210 const char *fmt;
211
212 switch (code)
213 {
214 case MEM:
215 return !MEM_READONLY_P (x) || rtx_unstable_p (XEXP (x, 0));
216
217 case CONST:
218 CASE_CONST_ANY:
219 case SYMBOL_REF:
220 case LABEL_REF:
221 return false;
222
223 case REG:
224 /* As in rtx_varies_p, we have to use the actual rtx, not reg number. */
225 if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
226 /* The arg pointer varies if it is not a fixed register. */
227 || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM]))
228 return false;
229 /* ??? When call-clobbered, the value is stable modulo the restore
230 that must happen after a call. This currently screws up local-alloc
231 into believing that the restore is not needed. */
232 if (!PIC_OFFSET_TABLE_REG_CALL_CLOBBERED && x == pic_offset_table_rtx)
233 return false;
234 return true;
235
236 case ASM_OPERANDS:
237 if (MEM_VOLATILE_P (x))
238 return true;
239
240 /* Fall through. */
241
242 default:
243 break;
244 }
245
246 fmt = GET_RTX_FORMAT (code);
247 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
248 if (fmt[i] == 'e')
249 {
250 if (rtx_unstable_p (XEXP (x, i)))
251 return true;
252 }
253 else if (fmt[i] == 'E')
254 {
255 int j;
256 for (j = 0; j < XVECLEN (x, i); j++)
257 if (rtx_unstable_p (XVECEXP (x, i, j)))
258 return true;
259 }
260
261 return false;
262 }
263
264 /* Return true if X has a value that can vary even between two
265 executions of the program. false means X can be compared reliably
266 against certain constants or near-constants.
267 FOR_ALIAS is nonzero if we are called from alias analysis; if it is
268 zero, we are slightly more conservative.
269 The frame pointer and the arg pointer are considered constant. */
270
271 bool
272 rtx_varies_p (const_rtx x, bool for_alias)
273 {
274 RTX_CODE code;
275 int i;
276 const char *fmt;
277
278 if (!x)
279 return false;
280
281 code = GET_CODE (x);
282 switch (code)
283 {
284 case MEM:
285 return !MEM_READONLY_P (x) || rtx_varies_p (XEXP (x, 0), for_alias);
286
287 case CONST:
288 CASE_CONST_ANY:
289 case SYMBOL_REF:
290 case LABEL_REF:
291 return false;
292
293 case REG:
294 /* Note that we have to test for the actual rtx used for the frame
295 and arg pointers and not just the register number in case we have
296 eliminated the frame and/or arg pointer and are using it
297 for pseudos. */
298 if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
299 /* The arg pointer varies if it is not a fixed register. */
300 || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM]))
301 return false;
302 if (x == pic_offset_table_rtx
303 /* ??? When call-clobbered, the value is stable modulo the restore
304 that must happen after a call. This currently screws up
305 local-alloc into believing that the restore is not needed, so we
306 must return 0 only if we are called from alias analysis. */
307 && (!PIC_OFFSET_TABLE_REG_CALL_CLOBBERED || for_alias))
308 return false;
309 return true;
310
311 case LO_SUM:
312 /* The operand 0 of a LO_SUM is considered constant
313 (in fact it is related specifically to operand 1)
314 during alias analysis. */
315 return (! for_alias && rtx_varies_p (XEXP (x, 0), for_alias))
316 || rtx_varies_p (XEXP (x, 1), for_alias);
317
318 case ASM_OPERANDS:
319 if (MEM_VOLATILE_P (x))
320 return true;
321
322 /* Fall through. */
323
324 default:
325 break;
326 }
327
328 fmt = GET_RTX_FORMAT (code);
329 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
330 if (fmt[i] == 'e')
331 {
332 if (rtx_varies_p (XEXP (x, i), for_alias))
333 return true;
334 }
335 else if (fmt[i] == 'E')
336 {
337 int j;
338 for (j = 0; j < XVECLEN (x, i); j++)
339 if (rtx_varies_p (XVECEXP (x, i, j), for_alias))
340 return true;
341 }
342
343 return false;
344 }
345
346 /* Compute an approximation for the offset between the register
347 FROM and TO for the current function, as it was at the start
348 of the routine. */
349
350 static poly_int64
351 get_initial_register_offset (int from, int to)
352 {
353 static const struct elim_table_t
354 {
355 const int from;
356 const int to;
357 } table[] = ELIMINABLE_REGS;
358 poly_int64 offset1, offset2;
359 unsigned int i, j;
360
361 if (to == from)
362 return 0;
363
364 /* It is not safe to call INITIAL_ELIMINATION_OFFSET before the epilogue
365 is completed, but we need to give at least an estimate for the stack
366 pointer based on the frame size. */
367 if (!epilogue_completed)
368 {
369 offset1 = crtl->outgoing_args_size + get_frame_size ();
370 #if !STACK_GROWS_DOWNWARD
371 offset1 = - offset1;
372 #endif
373 if (to == STACK_POINTER_REGNUM)
374 return offset1;
375 else if (from == STACK_POINTER_REGNUM)
376 return - offset1;
377 else
378 return 0;
379 }
380
381 for (i = 0; i < ARRAY_SIZE (table); i++)
382 if (table[i].from == from)
383 {
384 if (table[i].to == to)
385 {
386 INITIAL_ELIMINATION_OFFSET (table[i].from, table[i].to,
387 offset1);
388 return offset1;
389 }
390 for (j = 0; j < ARRAY_SIZE (table); j++)
391 {
392 if (table[j].to == to
393 && table[j].from == table[i].to)
394 {
395 INITIAL_ELIMINATION_OFFSET (table[i].from, table[i].to,
396 offset1);
397 INITIAL_ELIMINATION_OFFSET (table[j].from, table[j].to,
398 offset2);
399 return offset1 + offset2;
400 }
401 if (table[j].from == to
402 && table[j].to == table[i].to)
403 {
404 INITIAL_ELIMINATION_OFFSET (table[i].from, table[i].to,
405 offset1);
406 INITIAL_ELIMINATION_OFFSET (table[j].from, table[j].to,
407 offset2);
408 return offset1 - offset2;
409 }
410 }
411 }
412 else if (table[i].to == from)
413 {
414 if (table[i].from == to)
415 {
416 INITIAL_ELIMINATION_OFFSET (table[i].from, table[i].to,
417 offset1);
418 return - offset1;
419 }
420 for (j = 0; j < ARRAY_SIZE (table); j++)
421 {
422 if (table[j].to == to
423 && table[j].from == table[i].from)
424 {
425 INITIAL_ELIMINATION_OFFSET (table[i].from, table[i].to,
426 offset1);
427 INITIAL_ELIMINATION_OFFSET (table[j].from, table[j].to,
428 offset2);
429 return - offset1 + offset2;
430 }
431 if (table[j].from == to
432 && table[j].to == table[i].from)
433 {
434 INITIAL_ELIMINATION_OFFSET (table[i].from, table[i].to,
435 offset1);
436 INITIAL_ELIMINATION_OFFSET (table[j].from, table[j].to,
437 offset2);
438 return - offset1 - offset2;
439 }
440 }
441 }
442
443 /* If the requested register combination was not found,
444 try a different more simple combination. */
445 if (from == ARG_POINTER_REGNUM)
446 return get_initial_register_offset (HARD_FRAME_POINTER_REGNUM, to);
447 else if (to == ARG_POINTER_REGNUM)
448 return get_initial_register_offset (from, HARD_FRAME_POINTER_REGNUM);
449 else if (from == HARD_FRAME_POINTER_REGNUM)
450 return get_initial_register_offset (FRAME_POINTER_REGNUM, to);
451 else if (to == HARD_FRAME_POINTER_REGNUM)
452 return get_initial_register_offset (from, FRAME_POINTER_REGNUM);
453 else
454 return 0;
455 }
456
457 /* Return true if the use of X+OFFSET as an address in a MEM with SIZE
458 bytes can cause a trap. MODE is the mode of the MEM (not that of X) and
459 UNALIGNED_MEMS controls whether true is returned for unaligned memory
460 references on strict alignment machines. */
461
462 static bool
463 rtx_addr_can_trap_p_1 (const_rtx x, poly_int64 offset, poly_int64 size,
464 machine_mode mode, bool unaligned_mems)
465 {
466 enum rtx_code code = GET_CODE (x);
467 gcc_checking_assert (mode == BLKmode
468 || mode == VOIDmode
469 || known_size_p (size));
470 poly_int64 const_x1;
471
472 /* The offset must be a multiple of the mode size if we are considering
473 unaligned memory references on strict alignment machines. */
474 if (STRICT_ALIGNMENT
475 && unaligned_mems
476 && mode != BLKmode
477 && mode != VOIDmode)
478 {
479 poly_int64 actual_offset = offset;
480
481 #ifdef SPARC_STACK_BOUNDARY_HACK
482 /* ??? The SPARC port may claim a STACK_BOUNDARY higher than
483 the real alignment of %sp. However, when it does this, the
484 alignment of %sp+STACK_POINTER_OFFSET is STACK_BOUNDARY. */
485 if (SPARC_STACK_BOUNDARY_HACK
486 && (x == stack_pointer_rtx || x == hard_frame_pointer_rtx))
487 actual_offset -= STACK_POINTER_OFFSET;
488 #endif
489
490 if (!multiple_p (actual_offset, GET_MODE_SIZE (mode)))
491 return true;
492 }
493
494 switch (code)
495 {
496 case SYMBOL_REF:
497 if (SYMBOL_REF_WEAK (x))
498 return true;
499 if (!CONSTANT_POOL_ADDRESS_P (x) && !SYMBOL_REF_FUNCTION_P (x))
500 {
501 tree decl;
502 poly_int64 decl_size;
503
504 if (maybe_lt (offset, 0))
505 return true;
506 if (!known_size_p (size))
507 return maybe_ne (offset, 0);
508
509 /* If the size of the access or of the symbol is unknown,
510 assume the worst. */
511 decl = SYMBOL_REF_DECL (x);
512
513 /* Else check that the access is in bounds. TODO: restructure
514 expr_size/tree_expr_size/int_expr_size and just use the latter. */
515 if (!decl)
516 decl_size = -1;
517 else if (DECL_P (decl) && DECL_SIZE_UNIT (decl))
518 {
519 if (!poly_int_tree_p (DECL_SIZE_UNIT (decl), &decl_size))
520 decl_size = -1;
521 }
522 else if (TREE_CODE (decl) == STRING_CST)
523 decl_size = TREE_STRING_LENGTH (decl);
524 else if (TYPE_SIZE_UNIT (TREE_TYPE (decl)))
525 decl_size = int_size_in_bytes (TREE_TYPE (decl));
526 else
527 decl_size = -1;
528
529 return (!known_size_p (decl_size) || known_eq (decl_size, 0)
530 ? maybe_ne (offset, 0)
531 : !known_subrange_p (offset, size, 0, decl_size));
532 }
533
534 return false;
535
536 case LABEL_REF:
537 return false;
538
539 case REG:
540 /* Stack references are assumed not to trap, but we need to deal with
541 nonsensical offsets. */
542 if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
543 || x == stack_pointer_rtx
544 /* The arg pointer varies if it is not a fixed register. */
545 || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM]))
546 {
547 #ifdef RED_ZONE_SIZE
548 poly_int64 red_zone_size = RED_ZONE_SIZE;
549 #else
550 poly_int64 red_zone_size = 0;
551 #endif
552 poly_int64 stack_boundary = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
553 poly_int64 low_bound, high_bound;
554
555 if (!known_size_p (size))
556 return true;
557
558 if (x == frame_pointer_rtx)
559 {
560 if (FRAME_GROWS_DOWNWARD)
561 {
562 high_bound = targetm.starting_frame_offset ();
563 low_bound = high_bound - get_frame_size ();
564 }
565 else
566 {
567 low_bound = targetm.starting_frame_offset ();
568 high_bound = low_bound + get_frame_size ();
569 }
570 }
571 else if (x == hard_frame_pointer_rtx)
572 {
573 poly_int64 sp_offset
574 = get_initial_register_offset (STACK_POINTER_REGNUM,
575 HARD_FRAME_POINTER_REGNUM);
576 poly_int64 ap_offset
577 = get_initial_register_offset (ARG_POINTER_REGNUM,
578 HARD_FRAME_POINTER_REGNUM);
579
580 #if STACK_GROWS_DOWNWARD
581 low_bound = sp_offset - red_zone_size - stack_boundary;
582 high_bound = ap_offset
583 + FIRST_PARM_OFFSET (current_function_decl)
584 #if !ARGS_GROW_DOWNWARD
585 + crtl->args.size
586 #endif
587 + stack_boundary;
588 #else
589 high_bound = sp_offset + red_zone_size + stack_boundary;
590 low_bound = ap_offset
591 + FIRST_PARM_OFFSET (current_function_decl)
592 #if ARGS_GROW_DOWNWARD
593 - crtl->args.size
594 #endif
595 - stack_boundary;
596 #endif
597 }
598 else if (x == stack_pointer_rtx)
599 {
600 poly_int64 ap_offset
601 = get_initial_register_offset (ARG_POINTER_REGNUM,
602 STACK_POINTER_REGNUM);
603
604 #if STACK_GROWS_DOWNWARD
605 low_bound = - red_zone_size - stack_boundary;
606 high_bound = ap_offset
607 + FIRST_PARM_OFFSET (current_function_decl)
608 #if !ARGS_GROW_DOWNWARD
609 + crtl->args.size
610 #endif
611 + stack_boundary;
612 #else
613 high_bound = red_zone_size + stack_boundary;
614 low_bound = ap_offset
615 + FIRST_PARM_OFFSET (current_function_decl)
616 #if ARGS_GROW_DOWNWARD
617 - crtl->args.size
618 #endif
619 - stack_boundary;
620 #endif
621 }
622 else
623 {
624 /* We assume that accesses are safe to at least the
625 next stack boundary.
626 Examples are varargs and __builtin_return_address. */
627 #if ARGS_GROW_DOWNWARD
628 high_bound = FIRST_PARM_OFFSET (current_function_decl)
629 + stack_boundary;
630 low_bound = FIRST_PARM_OFFSET (current_function_decl)
631 - crtl->args.size - stack_boundary;
632 #else
633 low_bound = FIRST_PARM_OFFSET (current_function_decl)
634 - stack_boundary;
635 high_bound = FIRST_PARM_OFFSET (current_function_decl)
636 + crtl->args.size + stack_boundary;
637 #endif
638 }
639
640 if (known_ge (offset, low_bound)
641 && known_le (offset, high_bound - size))
642 return false;
643 return true;
644 }
645 /* All of the virtual frame registers are stack references. */
646 if (VIRTUAL_REGISTER_P (x))
647 return false;
648 return true;
649
650 case CONST:
651 return rtx_addr_can_trap_p_1 (XEXP (x, 0), offset, size,
652 mode, unaligned_mems);
653
654 case PLUS:
655 /* An address is assumed not to trap if:
656 - it is the pic register plus a const unspec without offset. */
657 if (XEXP (x, 0) == pic_offset_table_rtx
658 && GET_CODE (XEXP (x, 1)) == CONST
659 && GET_CODE (XEXP (XEXP (x, 1), 0)) == UNSPEC
660 && known_eq (offset, 0))
661 return false;
662
663 /* - or it is an address that can't trap plus a constant integer. */
664 if (poly_int_rtx_p (XEXP (x, 1), &const_x1)
665 && !rtx_addr_can_trap_p_1 (XEXP (x, 0), offset + const_x1,
666 size, mode, unaligned_mems))
667 return false;
668
669 return true;
670
671 case LO_SUM:
672 case PRE_MODIFY:
673 return rtx_addr_can_trap_p_1 (XEXP (x, 1), offset, size,
674 mode, unaligned_mems);
675
676 case PRE_DEC:
677 case PRE_INC:
678 case POST_DEC:
679 case POST_INC:
680 case POST_MODIFY:
681 return rtx_addr_can_trap_p_1 (XEXP (x, 0), offset, size,
682 mode, unaligned_mems);
683
684 default:
685 break;
686 }
687
688 /* If it isn't one of the case above, it can cause a trap. */
689 return true;
690 }
691
692 /* Return true if the use of X as an address in a MEM can cause a trap. */
693
694 bool
695 rtx_addr_can_trap_p (const_rtx x)
696 {
697 return rtx_addr_can_trap_p_1 (x, 0, -1, BLKmode, false);
698 }
699
700 /* Return true if X contains a MEM subrtx. */
701
702 bool
703 contains_mem_rtx_p (rtx x)
704 {
705 subrtx_iterator::array_type array;
706 FOR_EACH_SUBRTX (iter, array, x, ALL)
707 if (MEM_P (*iter))
708 return true;
709
710 return false;
711 }
712
713 /* Return true if X is an address that is known to not be zero. */
714
715 bool
716 nonzero_address_p (const_rtx x)
717 {
718 const enum rtx_code code = GET_CODE (x);
719
720 switch (code)
721 {
722 case SYMBOL_REF:
723 return flag_delete_null_pointer_checks && !SYMBOL_REF_WEAK (x);
724
725 case LABEL_REF:
726 return true;
727
728 case REG:
729 /* As in rtx_varies_p, we have to use the actual rtx, not reg number. */
730 if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
731 || x == stack_pointer_rtx
732 || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM]))
733 return true;
734 /* All of the virtual frame registers are stack references. */
735 if (VIRTUAL_REGISTER_P (x))
736 return true;
737 return false;
738
739 case CONST:
740 return nonzero_address_p (XEXP (x, 0));
741
742 case PLUS:
743 /* Handle PIC references. */
744 if (XEXP (x, 0) == pic_offset_table_rtx
745 && CONSTANT_P (XEXP (x, 1)))
746 return true;
747 return false;
748
749 case PRE_MODIFY:
750 /* Similar to the above; allow positive offsets. Further, since
751 auto-inc is only allowed in memories, the register must be a
752 pointer. */
753 if (CONST_INT_P (XEXP (x, 1))
754 && INTVAL (XEXP (x, 1)) > 0)
755 return true;
756 return nonzero_address_p (XEXP (x, 0));
757
758 case PRE_INC:
759 /* Similarly. Further, the offset is always positive. */
760 return true;
761
762 case PRE_DEC:
763 case POST_DEC:
764 case POST_INC:
765 case POST_MODIFY:
766 return nonzero_address_p (XEXP (x, 0));
767
768 case LO_SUM:
769 return nonzero_address_p (XEXP (x, 1));
770
771 default:
772 break;
773 }
774
775 /* If it isn't one of the case above, might be zero. */
776 return false;
777 }
778
779 /* Return true if X refers to a memory location whose address
780 cannot be compared reliably with constant addresses,
781 or if X refers to a BLKmode memory object.
782 FOR_ALIAS is nonzero if we are called from alias analysis; if it is
783 zero, we are slightly more conservative. */
784
785 bool
786 rtx_addr_varies_p (const_rtx x, bool for_alias)
787 {
788 enum rtx_code code;
789 int i;
790 const char *fmt;
791
792 if (x == 0)
793 return false;
794
795 code = GET_CODE (x);
796 if (code == MEM)
797 return GET_MODE (x) == BLKmode || rtx_varies_p (XEXP (x, 0), for_alias);
798
799 fmt = GET_RTX_FORMAT (code);
800 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
801 if (fmt[i] == 'e')
802 {
803 if (rtx_addr_varies_p (XEXP (x, i), for_alias))
804 return true;
805 }
806 else if (fmt[i] == 'E')
807 {
808 int j;
809 for (j = 0; j < XVECLEN (x, i); j++)
810 if (rtx_addr_varies_p (XVECEXP (x, i, j), for_alias))
811 return true;
812 }
813 return false;
814 }
815
816 /* Return the CALL in X if there is one. */
817
818 rtx
819 get_call_rtx_from (const rtx_insn *insn)
820 {
821 rtx x = PATTERN (insn);
822 if (GET_CODE (x) == PARALLEL)
823 x = XVECEXP (x, 0, 0);
824 if (GET_CODE (x) == SET)
825 x = SET_SRC (x);
826 if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
827 return x;
828 return NULL_RTX;
829 }
830
831 /* Get the declaration of the function called by INSN. */
832
833 tree
834 get_call_fndecl (const rtx_insn *insn)
835 {
836 rtx note, datum;
837
838 note = find_reg_note (insn, REG_CALL_DECL, NULL_RTX);
839 if (note == NULL_RTX)
840 return NULL_TREE;
841
842 datum = XEXP (note, 0);
843 if (datum != NULL_RTX)
844 return SYMBOL_REF_DECL (datum);
845
846 return NULL_TREE;
847 }
848
849 /* Return the value of the integer term in X, if one is apparent;
850 otherwise return 0.
851 Only obvious integer terms are detected.
852 This is used in cse.cc with the `related_value' field. */
853
854 HOST_WIDE_INT
855 get_integer_term (const_rtx x)
856 {
857 if (GET_CODE (x) == CONST)
858 x = XEXP (x, 0);
859
860 if (GET_CODE (x) == MINUS
861 && CONST_INT_P (XEXP (x, 1)))
862 return - INTVAL (XEXP (x, 1));
863 if (GET_CODE (x) == PLUS
864 && CONST_INT_P (XEXP (x, 1)))
865 return INTVAL (XEXP (x, 1));
866 return 0;
867 }
868
869 /* If X is a constant, return the value sans apparent integer term;
870 otherwise return 0.
871 Only obvious integer terms are detected. */
872
873 rtx
874 get_related_value (const_rtx x)
875 {
876 if (GET_CODE (x) != CONST)
877 return 0;
878 x = XEXP (x, 0);
879 if (GET_CODE (x) == PLUS
880 && CONST_INT_P (XEXP (x, 1)))
881 return XEXP (x, 0);
882 else if (GET_CODE (x) == MINUS
883 && CONST_INT_P (XEXP (x, 1)))
884 return XEXP (x, 0);
885 return 0;
886 }
887
888 /* Return true if SYMBOL is a SYMBOL_REF and OFFSET + SYMBOL points
889 to somewhere in the same object or object_block as SYMBOL. */
890
891 bool
892 offset_within_block_p (const_rtx symbol, HOST_WIDE_INT offset)
893 {
894 tree decl;
895
896 if (GET_CODE (symbol) != SYMBOL_REF)
897 return false;
898
899 if (offset == 0)
900 return true;
901
902 if (offset > 0)
903 {
904 if (CONSTANT_POOL_ADDRESS_P (symbol)
905 && offset < (int) GET_MODE_SIZE (get_pool_mode (symbol)))
906 return true;
907
908 decl = SYMBOL_REF_DECL (symbol);
909 if (decl && offset < int_size_in_bytes (TREE_TYPE (decl)))
910 return true;
911 }
912
913 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol)
914 && SYMBOL_REF_BLOCK (symbol)
915 && SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0
916 && ((unsigned HOST_WIDE_INT) offset + SYMBOL_REF_BLOCK_OFFSET (symbol)
917 < (unsigned HOST_WIDE_INT) SYMBOL_REF_BLOCK (symbol)->size))
918 return true;
919
920 return false;
921 }
922
923 /* Split X into a base and a constant offset, storing them in *BASE_OUT
924 and *OFFSET_OUT respectively. */
925
926 void
927 split_const (rtx x, rtx *base_out, rtx *offset_out)
928 {
929 if (GET_CODE (x) == CONST)
930 {
931 x = XEXP (x, 0);
932 if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1)))
933 {
934 *base_out = XEXP (x, 0);
935 *offset_out = XEXP (x, 1);
936 return;
937 }
938 }
939 *base_out = x;
940 *offset_out = const0_rtx;
941 }
942
943 /* Express integer value X as some value Y plus a polynomial offset,
944 where Y is either const0_rtx, X or something within X (as opposed
945 to a new rtx). Return the Y and store the offset in *OFFSET_OUT. */
946
947 rtx
948 strip_offset (rtx x, poly_int64 *offset_out)
949 {
950 rtx base = const0_rtx;
951 rtx test = x;
952 if (GET_CODE (test) == CONST)
953 test = XEXP (test, 0);
954 if (GET_CODE (test) == PLUS)
955 {
956 base = XEXP (test, 0);
957 test = XEXP (test, 1);
958 }
959 if (poly_int_rtx_p (test, offset_out))
960 return base;
961 *offset_out = 0;
962 return x;
963 }
964
965 /* Return the argument size in REG_ARGS_SIZE note X. */
966
967 poly_int64
968 get_args_size (const_rtx x)
969 {
970 gcc_checking_assert (REG_NOTE_KIND (x) == REG_ARGS_SIZE);
971 return rtx_to_poly_int64 (XEXP (x, 0));
972 }
973
974 /* Return the number of places FIND appears within X. If COUNT_DEST is
975 zero, we do not count occurrences inside the destination of a SET. */
976
977 int
978 count_occurrences (const_rtx x, const_rtx find, int count_dest)
979 {
980 int i, j;
981 enum rtx_code code;
982 const char *format_ptr;
983 int count;
984
985 if (x == find)
986 return 1;
987
988 code = GET_CODE (x);
989
990 switch (code)
991 {
992 case REG:
993 CASE_CONST_ANY:
994 case SYMBOL_REF:
995 case CODE_LABEL:
996 case PC:
997 return 0;
998
999 case EXPR_LIST:
1000 count = count_occurrences (XEXP (x, 0), find, count_dest);
1001 if (XEXP (x, 1))
1002 count += count_occurrences (XEXP (x, 1), find, count_dest);
1003 return count;
1004
1005 case MEM:
1006 if (MEM_P (find) && rtx_equal_p (x, find))
1007 return 1;
1008 break;
1009
1010 case SET:
1011 if (SET_DEST (x) == find && ! count_dest)
1012 return count_occurrences (SET_SRC (x), find, count_dest);
1013 break;
1014
1015 default:
1016 break;
1017 }
1018
1019 format_ptr = GET_RTX_FORMAT (code);
1020 count = 0;
1021
1022 for (i = 0; i < GET_RTX_LENGTH (code); i++)
1023 {
1024 switch (*format_ptr++)
1025 {
1026 case 'e':
1027 count += count_occurrences (XEXP (x, i), find, count_dest);
1028 break;
1029
1030 case 'E':
1031 for (j = 0; j < XVECLEN (x, i); j++)
1032 count += count_occurrences (XVECEXP (x, i, j), find, count_dest);
1033 break;
1034 }
1035 }
1036 return count;
1037 }
1038
1039
1040 /* Return TRUE if OP is a register or subreg of a register that
1041 holds an unsigned quantity. Otherwise, return FALSE. */
1042
1043 bool
1044 unsigned_reg_p (rtx op)
1045 {
1046 if (REG_P (op)
1047 && REG_EXPR (op)
1048 && TYPE_UNSIGNED (TREE_TYPE (REG_EXPR (op))))
1049 return true;
1050
1051 if (GET_CODE (op) == SUBREG
1052 && SUBREG_PROMOTED_SIGN (op))
1053 return true;
1054
1055 return false;
1056 }
1057
1058
1059 /* Return true if register REG appears somewhere within IN.
1060 Also works if REG is not a register; in this case it checks
1061 for a subexpression of IN that is Lisp "equal" to REG. */
1062
1063 bool
1064 reg_mentioned_p (const_rtx reg, const_rtx in)
1065 {
1066 const char *fmt;
1067 int i;
1068 enum rtx_code code;
1069
1070 if (in == 0)
1071 return false;
1072
1073 if (reg == in)
1074 return true;
1075
1076 if (GET_CODE (in) == LABEL_REF)
1077 return reg == label_ref_label (in);
1078
1079 code = GET_CODE (in);
1080
1081 switch (code)
1082 {
1083 /* Compare registers by number. */
1084 case REG:
1085 return REG_P (reg) && REGNO (in) == REGNO (reg);
1086
1087 /* These codes have no constituent expressions
1088 and are unique. */
1089 case SCRATCH:
1090 case PC:
1091 return false;
1092
1093 CASE_CONST_ANY:
1094 /* These are kept unique for a given value. */
1095 return false;
1096
1097 default:
1098 break;
1099 }
1100
1101 if (GET_CODE (reg) == code && rtx_equal_p (reg, in))
1102 return true;
1103
1104 fmt = GET_RTX_FORMAT (code);
1105
1106 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1107 {
1108 if (fmt[i] == 'E')
1109 {
1110 int j;
1111 for (j = XVECLEN (in, i) - 1; j >= 0; j--)
1112 if (reg_mentioned_p (reg, XVECEXP (in, i, j)))
1113 return true;
1114 }
1115 else if (fmt[i] == 'e'
1116 && reg_mentioned_p (reg, XEXP (in, i)))
1117 return true;
1118 }
1119 return false;
1120 }
1121
1122 /* Return true if in between BEG and END, exclusive of BEG and END, there is
1123 no CODE_LABEL insn. */
1124
1125 bool
1126 no_labels_between_p (const rtx_insn *beg, const rtx_insn *end)
1127 {
1128 rtx_insn *p;
1129 if (beg == end)
1130 return false;
1131 for (p = NEXT_INSN (beg); p != end; p = NEXT_INSN (p))
1132 if (LABEL_P (p))
1133 return false;
1134 return true;
1135 }
1136
1137 /* Return true if register REG is used in an insn between
1138 FROM_INSN and TO_INSN (exclusive of those two). */
1139
1140 bool
1141 reg_used_between_p (const_rtx reg, const rtx_insn *from_insn,
1142 const rtx_insn *to_insn)
1143 {
1144 rtx_insn *insn;
1145
1146 if (from_insn == to_insn)
1147 return false;
1148
1149 for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
1150 if (NONDEBUG_INSN_P (insn)
1151 && (reg_overlap_mentioned_p (reg, PATTERN (insn))
1152 || (CALL_P (insn) && find_reg_fusage (insn, USE, reg))))
1153 return true;
1154 return false;
1155 }
1156
1157 /* Return true if the old value of X, a register, is referenced in BODY. If X
1158 is entirely replaced by a new value and the only use is as a SET_DEST,
1159 we do not consider it a reference. */
1160
1161 bool
1162 reg_referenced_p (const_rtx x, const_rtx body)
1163 {
1164 int i;
1165
1166 switch (GET_CODE (body))
1167 {
1168 case SET:
1169 if (reg_overlap_mentioned_p (x, SET_SRC (body)))
1170 return true;
1171
1172 /* If the destination is anything other than PC, a REG or a SUBREG
1173 of a REG that occupies all of the REG, the insn references X if
1174 it is mentioned in the destination. */
1175 if (GET_CODE (SET_DEST (body)) != PC
1176 && !REG_P (SET_DEST (body))
1177 && ! (GET_CODE (SET_DEST (body)) == SUBREG
1178 && REG_P (SUBREG_REG (SET_DEST (body)))
1179 && !read_modify_subreg_p (SET_DEST (body)))
1180 && reg_overlap_mentioned_p (x, SET_DEST (body)))
1181 return true;
1182 return false;
1183
1184 case ASM_OPERANDS:
1185 for (i = ASM_OPERANDS_INPUT_LENGTH (body) - 1; i >= 0; i--)
1186 if (reg_overlap_mentioned_p (x, ASM_OPERANDS_INPUT (body, i)))
1187 return true;
1188 return false;
1189
1190 case CALL:
1191 case USE:
1192 case IF_THEN_ELSE:
1193 return reg_overlap_mentioned_p (x, body);
1194
1195 case TRAP_IF:
1196 return reg_overlap_mentioned_p (x, TRAP_CONDITION (body));
1197
1198 case PREFETCH:
1199 return reg_overlap_mentioned_p (x, XEXP (body, 0));
1200
1201 case UNSPEC:
1202 case UNSPEC_VOLATILE:
1203 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
1204 if (reg_overlap_mentioned_p (x, XVECEXP (body, 0, i)))
1205 return true;
1206 return false;
1207
1208 case PARALLEL:
1209 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
1210 if (reg_referenced_p (x, XVECEXP (body, 0, i)))
1211 return true;
1212 return false;
1213
1214 case CLOBBER:
1215 if (MEM_P (XEXP (body, 0)))
1216 if (reg_overlap_mentioned_p (x, XEXP (XEXP (body, 0), 0)))
1217 return true;
1218 return false;
1219
1220 case COND_EXEC:
1221 if (reg_overlap_mentioned_p (x, COND_EXEC_TEST (body)))
1222 return true;
1223 return reg_referenced_p (x, COND_EXEC_CODE (body));
1224
1225 default:
1226 return false;
1227 }
1228 }
1229
1230 /* Return true if register REG is set or clobbered in an insn between
1231 FROM_INSN and TO_INSN (exclusive of those two). */
1232
1233 bool
1234 reg_set_between_p (const_rtx reg, const rtx_insn *from_insn,
1235 const rtx_insn *to_insn)
1236 {
1237 const rtx_insn *insn;
1238
1239 if (from_insn == to_insn)
1240 return false;
1241
1242 for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
1243 if (INSN_P (insn) && reg_set_p (reg, insn))
1244 return true;
1245 return false;
1246 }
1247
1248 /* Return true if REG is set or clobbered inside INSN. */
1249
1250 bool
1251 reg_set_p (const_rtx reg, const_rtx insn)
1252 {
1253 /* After delay slot handling, call and branch insns might be in a
1254 sequence. Check all the elements there. */
1255 if (INSN_P (insn) && GET_CODE (PATTERN (insn)) == SEQUENCE)
1256 {
1257 for (int i = 0; i < XVECLEN (PATTERN (insn), 0); ++i)
1258 if (reg_set_p (reg, XVECEXP (PATTERN (insn), 0, i)))
1259 return true;
1260
1261 return false;
1262 }
1263
1264 /* We can be passed an insn or part of one. If we are passed an insn,
1265 check if a side-effect of the insn clobbers REG. */
1266 if (INSN_P (insn)
1267 && (FIND_REG_INC_NOTE (insn, reg)
1268 || (CALL_P (insn)
1269 && ((REG_P (reg)
1270 && REGNO (reg) < FIRST_PSEUDO_REGISTER
1271 && (insn_callee_abi (as_a<const rtx_insn *> (insn))
1272 .clobbers_reg_p (GET_MODE (reg), REGNO (reg))))
1273 || MEM_P (reg)
1274 || find_reg_fusage (insn, CLOBBER, reg)))))
1275 return true;
1276
1277 /* There are no REG_INC notes for SP autoinc. */
1278 if (reg == stack_pointer_rtx && INSN_P (insn))
1279 {
1280 subrtx_var_iterator::array_type array;
1281 FOR_EACH_SUBRTX_VAR (iter, array, PATTERN (insn), NONCONST)
1282 {
1283 rtx mem = *iter;
1284 if (mem
1285 && MEM_P (mem)
1286 && GET_RTX_CLASS (GET_CODE (XEXP (mem, 0))) == RTX_AUTOINC)
1287 {
1288 if (XEXP (XEXP (mem, 0), 0) == stack_pointer_rtx)
1289 return true;
1290 iter.skip_subrtxes ();
1291 }
1292 }
1293 }
1294
1295 return set_of (reg, insn) != NULL_RTX;
1296 }
1297
1298 /* Similar to reg_set_between_p, but check all registers in X. Return false
1299 only if none of them are modified between START and END. Return true if
1300 X contains a MEM; this routine does use memory aliasing. */
1301
1302 bool
1303 modified_between_p (const_rtx x, const rtx_insn *start, const rtx_insn *end)
1304 {
1305 const enum rtx_code code = GET_CODE (x);
1306 const char *fmt;
1307 int i, j;
1308 rtx_insn *insn;
1309
1310 if (start == end)
1311 return false;
1312
1313 switch (code)
1314 {
1315 CASE_CONST_ANY:
1316 case CONST:
1317 case SYMBOL_REF:
1318 case LABEL_REF:
1319 return false;
1320
1321 case PC:
1322 return true;
1323
1324 case MEM:
1325 if (modified_between_p (XEXP (x, 0), start, end))
1326 return true;
1327 if (MEM_READONLY_P (x))
1328 return false;
1329 for (insn = NEXT_INSN (start); insn != end; insn = NEXT_INSN (insn))
1330 if (memory_modified_in_insn_p (x, insn))
1331 return true;
1332 return false;
1333
1334 case REG:
1335 return reg_set_between_p (x, start, end);
1336
1337 default:
1338 break;
1339 }
1340
1341 fmt = GET_RTX_FORMAT (code);
1342 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1343 {
1344 if (fmt[i] == 'e' && modified_between_p (XEXP (x, i), start, end))
1345 return true;
1346
1347 else if (fmt[i] == 'E')
1348 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1349 if (modified_between_p (XVECEXP (x, i, j), start, end))
1350 return true;
1351 }
1352
1353 return false;
1354 }
1355
1356 /* Similar to reg_set_p, but check all registers in X. Return false only if
1357 none of them are modified in INSN. Return true if X contains a MEM; this
1358 routine does use memory aliasing. */
1359
1360 bool
1361 modified_in_p (const_rtx x, const_rtx insn)
1362 {
1363 const enum rtx_code code = GET_CODE (x);
1364 const char *fmt;
1365 int i, j;
1366
1367 switch (code)
1368 {
1369 CASE_CONST_ANY:
1370 case CONST:
1371 case SYMBOL_REF:
1372 case LABEL_REF:
1373 return false;
1374
1375 case PC:
1376 return true;
1377
1378 case MEM:
1379 if (modified_in_p (XEXP (x, 0), insn))
1380 return true;
1381 if (MEM_READONLY_P (x))
1382 return false;
1383 if (memory_modified_in_insn_p (x, insn))
1384 return true;
1385 return false;
1386
1387 case REG:
1388 return reg_set_p (x, insn);
1389
1390 default:
1391 break;
1392 }
1393
1394 fmt = GET_RTX_FORMAT (code);
1395 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1396 {
1397 if (fmt[i] == 'e' && modified_in_p (XEXP (x, i), insn))
1398 return true;
1399
1400 else if (fmt[i] == 'E')
1401 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1402 if (modified_in_p (XVECEXP (x, i, j), insn))
1403 return true;
1404 }
1405
1406 return false;
1407 }
1408
1409 /* Return true if X is a SUBREG and if storing a value to X would
1410 preserve some of its SUBREG_REG. For example, on a normal 32-bit
1411 target, using a SUBREG to store to one half of a DImode REG would
1412 preserve the other half. */
1413
1414 bool
1415 read_modify_subreg_p (const_rtx x)
1416 {
1417 if (GET_CODE (x) != SUBREG)
1418 return false;
1419 poly_uint64 isize = GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)));
1420 poly_uint64 osize = GET_MODE_SIZE (GET_MODE (x));
1421 poly_uint64 regsize = REGMODE_NATURAL_SIZE (GET_MODE (SUBREG_REG (x)));
1422 /* The inner and outer modes of a subreg must be ordered, so that we
1423 can tell whether they're paradoxical or partial. */
1424 gcc_checking_assert (ordered_p (isize, osize));
1425 return (maybe_gt (isize, osize) && maybe_gt (isize, regsize));
1426 }
1427
1428 /* Helper function for set_of. */
1429 struct set_of_data
1430 {
1431 const_rtx found;
1432 const_rtx pat;
1433 };
1434
1435 static void
1436 set_of_1 (rtx x, const_rtx pat, void *data1)
1437 {
1438 struct set_of_data *const data = (struct set_of_data *) (data1);
1439 if (rtx_equal_p (x, data->pat)
1440 || (!MEM_P (x) && reg_overlap_mentioned_p (data->pat, x)))
1441 data->found = pat;
1442 }
1443
1444 /* Give an INSN, return a SET or CLOBBER expression that does modify PAT
1445 (either directly or via STRICT_LOW_PART and similar modifiers). */
1446 const_rtx
1447 set_of (const_rtx pat, const_rtx insn)
1448 {
1449 struct set_of_data data;
1450 data.found = NULL_RTX;
1451 data.pat = pat;
1452 note_pattern_stores (INSN_P (insn) ? PATTERN (insn) : insn, set_of_1, &data);
1453 return data.found;
1454 }
1455
1456 /* Check whether instruction pattern PAT contains a SET with the following
1457 properties:
1458
1459 - the SET is executed unconditionally; and
1460 - either:
1461 - the destination of the SET is a REG that contains REGNO; or
1462 - both:
1463 - the destination of the SET is a SUBREG of such a REG; and
1464 - writing to the subreg clobbers all of the SUBREG_REG
1465 (in other words, read_modify_subreg_p is false).
1466
1467 If PAT does have a SET like that, return the set, otherwise return null.
1468
1469 This is intended to be an alternative to single_set for passes that
1470 can handle patterns with multiple_sets. */
1471 rtx
1472 simple_regno_set (rtx pat, unsigned int regno)
1473 {
1474 if (GET_CODE (pat) == PARALLEL)
1475 {
1476 int last = XVECLEN (pat, 0) - 1;
1477 for (int i = 0; i < last; ++i)
1478 if (rtx set = simple_regno_set (XVECEXP (pat, 0, i), regno))
1479 return set;
1480
1481 pat = XVECEXP (pat, 0, last);
1482 }
1483
1484 if (GET_CODE (pat) == SET
1485 && covers_regno_no_parallel_p (SET_DEST (pat), regno))
1486 return pat;
1487
1488 return nullptr;
1489 }
1490
1491 /* Add all hard register in X to *PSET. */
1492 void
1493 find_all_hard_regs (const_rtx x, HARD_REG_SET *pset)
1494 {
1495 subrtx_iterator::array_type array;
1496 FOR_EACH_SUBRTX (iter, array, x, NONCONST)
1497 {
1498 const_rtx x = *iter;
1499 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
1500 add_to_hard_reg_set (pset, GET_MODE (x), REGNO (x));
1501 }
1502 }
1503
1504 /* This function, called through note_stores, collects sets and
1505 clobbers of hard registers in a HARD_REG_SET, which is pointed to
1506 by DATA. */
1507 void
1508 record_hard_reg_sets (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
1509 {
1510 HARD_REG_SET *pset = (HARD_REG_SET *)data;
1511 if (REG_P (x) && HARD_REGISTER_P (x))
1512 add_to_hard_reg_set (pset, GET_MODE (x), REGNO (x));
1513 }
1514
1515 /* Examine INSN, and compute the set of hard registers written by it.
1516 Store it in *PSET. Should only be called after reload.
1517
1518 IMPLICIT is true if we should include registers that are fully-clobbered
1519 by calls. This should be used with caution, since it doesn't include
1520 partially-clobbered registers. */
1521 void
1522 find_all_hard_reg_sets (const rtx_insn *insn, HARD_REG_SET *pset, bool implicit)
1523 {
1524 rtx link;
1525
1526 CLEAR_HARD_REG_SET (*pset);
1527 note_stores (insn, record_hard_reg_sets, pset);
1528 if (CALL_P (insn) && implicit)
1529 *pset |= insn_callee_abi (insn).full_reg_clobbers ();
1530 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1531 if (REG_NOTE_KIND (link) == REG_INC)
1532 record_hard_reg_sets (XEXP (link, 0), NULL, pset);
1533 }
1534
1535 /* Like record_hard_reg_sets, but called through note_uses. */
1536 void
1537 record_hard_reg_uses (rtx *px, void *data)
1538 {
1539 find_all_hard_regs (*px, (HARD_REG_SET *) data);
1540 }
1541
1542 /* Given an INSN, return a SET expression if this insn has only a single SET.
1543 It may also have CLOBBERs, USEs, or SET whose output
1544 will not be used, which we ignore. */
1545
1546 rtx
1547 single_set_2 (const rtx_insn *insn, const_rtx pat)
1548 {
1549 rtx set = NULL;
1550 int set_verified = 1;
1551 int i;
1552
1553 if (GET_CODE (pat) == PARALLEL)
1554 {
1555 for (i = 0; i < XVECLEN (pat, 0); i++)
1556 {
1557 rtx sub = XVECEXP (pat, 0, i);
1558 switch (GET_CODE (sub))
1559 {
1560 case USE:
1561 case CLOBBER:
1562 break;
1563
1564 case SET:
1565 /* We can consider insns having multiple sets, where all
1566 but one are dead as single set insns. In common case
1567 only single set is present in the pattern so we want
1568 to avoid checking for REG_UNUSED notes unless necessary.
1569
1570 When we reach set first time, we just expect this is
1571 the single set we are looking for and only when more
1572 sets are found in the insn, we check them. */
1573 if (!set_verified)
1574 {
1575 if (find_reg_note (insn, REG_UNUSED, SET_DEST (set))
1576 && !side_effects_p (set))
1577 set = NULL;
1578 else
1579 set_verified = 1;
1580 }
1581 if (!set)
1582 set = sub, set_verified = 0;
1583 else if (!find_reg_note (insn, REG_UNUSED, SET_DEST (sub))
1584 || side_effects_p (sub))
1585 return NULL_RTX;
1586 break;
1587
1588 default:
1589 return NULL_RTX;
1590 }
1591 }
1592 }
1593 return set;
1594 }
1595
1596 /* Given an INSN, return true if it has more than one SET, else return
1597 false. */
1598
1599 bool
1600 multiple_sets (const_rtx insn)
1601 {
1602 bool found;
1603 int i;
1604
1605 /* INSN must be an insn. */
1606 if (! INSN_P (insn))
1607 return false;
1608
1609 /* Only a PARALLEL can have multiple SETs. */
1610 if (GET_CODE (PATTERN (insn)) == PARALLEL)
1611 {
1612 for (i = 0, found = false; i < XVECLEN (PATTERN (insn), 0); i++)
1613 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1614 {
1615 /* If we have already found a SET, then return now. */
1616 if (found)
1617 return true;
1618 else
1619 found = true;
1620 }
1621 }
1622
1623 /* Either zero or one SET. */
1624 return false;
1625 }
1626
1627 /* Return true if the destination of SET equals the source
1628 and there are no side effects. */
1629
1630 bool
1631 set_noop_p (const_rtx set)
1632 {
1633 rtx src = SET_SRC (set);
1634 rtx dst = SET_DEST (set);
1635
1636 if (dst == pc_rtx && src == pc_rtx)
1637 return true;
1638
1639 if (MEM_P (dst) && MEM_P (src))
1640 return (rtx_equal_p (dst, src)
1641 && !side_effects_p (dst)
1642 && !side_effects_p (src));
1643
1644 if (GET_CODE (dst) == ZERO_EXTRACT)
1645 return (rtx_equal_p (XEXP (dst, 0), src)
1646 && !BITS_BIG_ENDIAN && XEXP (dst, 2) == const0_rtx
1647 && !side_effects_p (src)
1648 && !side_effects_p (XEXP (dst, 0)));
1649
1650 if (GET_CODE (dst) == STRICT_LOW_PART)
1651 dst = XEXP (dst, 0);
1652
1653 if (GET_CODE (src) == SUBREG && GET_CODE (dst) == SUBREG)
1654 {
1655 if (maybe_ne (SUBREG_BYTE (src), SUBREG_BYTE (dst)))
1656 return false;
1657 src = SUBREG_REG (src);
1658 dst = SUBREG_REG (dst);
1659 if (GET_MODE (src) != GET_MODE (dst))
1660 /* It is hard to tell whether subregs refer to the same bits, so act
1661 conservatively and return false. */
1662 return false;
1663 }
1664
1665 /* It is a NOOP if destination overlaps with selected src vector
1666 elements. */
1667 if (GET_CODE (src) == VEC_SELECT
1668 && REG_P (XEXP (src, 0)) && REG_P (dst)
1669 && HARD_REGISTER_P (XEXP (src, 0))
1670 && HARD_REGISTER_P (dst))
1671 {
1672 int i;
1673 rtx par = XEXP (src, 1);
1674 rtx src0 = XEXP (src, 0);
1675 poly_int64 c0;
1676 if (!poly_int_rtx_p (XVECEXP (par, 0, 0), &c0))
1677 return false;
1678 poly_int64 offset = GET_MODE_UNIT_SIZE (GET_MODE (src0)) * c0;
1679
1680 for (i = 1; i < XVECLEN (par, 0); i++)
1681 {
1682 poly_int64 c0i;
1683 if (!poly_int_rtx_p (XVECEXP (par, 0, i), &c0i)
1684 || maybe_ne (c0i, c0 + i))
1685 return false;
1686 }
1687 return
1688 REG_CAN_CHANGE_MODE_P (REGNO (dst), GET_MODE (src0), GET_MODE (dst))
1689 && validate_subreg (GET_MODE (dst), GET_MODE (src0), src0, offset)
1690 && simplify_subreg_regno (REGNO (src0), GET_MODE (src0),
1691 offset, GET_MODE (dst)) == (int) REGNO (dst);
1692 }
1693
1694 return (REG_P (src) && REG_P (dst)
1695 && REGNO (src) == REGNO (dst));
1696 }
1697
1698 /* Return true if an insn consists only of SETs, each of which only sets a
1699 value to itself. */
1700
1701 bool
1702 noop_move_p (const rtx_insn *insn)
1703 {
1704 rtx pat = PATTERN (insn);
1705
1706 if (INSN_CODE (insn) == NOOP_MOVE_INSN_CODE)
1707 return true;
1708
1709 /* Check the code to be executed for COND_EXEC. */
1710 if (GET_CODE (pat) == COND_EXEC)
1711 pat = COND_EXEC_CODE (pat);
1712
1713 if (GET_CODE (pat) == SET && set_noop_p (pat))
1714 return true;
1715
1716 if (GET_CODE (pat) == PARALLEL)
1717 {
1718 int i;
1719 /* If nothing but SETs of registers to themselves,
1720 this insn can also be deleted. */
1721 for (i = 0; i < XVECLEN (pat, 0); i++)
1722 {
1723 rtx tem = XVECEXP (pat, 0, i);
1724
1725 if (GET_CODE (tem) == USE || GET_CODE (tem) == CLOBBER)
1726 continue;
1727
1728 if (GET_CODE (tem) != SET || ! set_noop_p (tem))
1729 return false;
1730 }
1731
1732 return true;
1733 }
1734 return false;
1735 }
1736
1737
1738 /* Return true if register in range [REGNO, ENDREGNO)
1739 appears either explicitly or implicitly in X
1740 other than being stored into.
1741
1742 References contained within the substructure at LOC do not count.
1743 LOC may be zero, meaning don't ignore anything. */
1744
1745 bool
1746 refers_to_regno_p (unsigned int regno, unsigned int endregno, const_rtx x,
1747 rtx *loc)
1748 {
1749 int i;
1750 unsigned int x_regno;
1751 RTX_CODE code;
1752 const char *fmt;
1753
1754 repeat:
1755 /* The contents of a REG_NONNEG note is always zero, so we must come here
1756 upon repeat in case the last REG_NOTE is a REG_NONNEG note. */
1757 if (x == 0)
1758 return false;
1759
1760 code = GET_CODE (x);
1761
1762 switch (code)
1763 {
1764 case REG:
1765 x_regno = REGNO (x);
1766
1767 /* If we modifying the stack, frame, or argument pointer, it will
1768 clobber a virtual register. In fact, we could be more precise,
1769 but it isn't worth it. */
1770 if ((x_regno == STACK_POINTER_REGNUM
1771 || (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1772 && x_regno == ARG_POINTER_REGNUM)
1773 || x_regno == FRAME_POINTER_REGNUM)
1774 && VIRTUAL_REGISTER_NUM_P (regno))
1775 return true;
1776
1777 return endregno > x_regno && regno < END_REGNO (x);
1778
1779 case SUBREG:
1780 /* If this is a SUBREG of a hard reg, we can see exactly which
1781 registers are being modified. Otherwise, handle normally. */
1782 if (REG_P (SUBREG_REG (x))
1783 && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER)
1784 {
1785 unsigned int inner_regno = subreg_regno (x);
1786 unsigned int inner_endregno
1787 = inner_regno + (inner_regno < FIRST_PSEUDO_REGISTER
1788 ? subreg_nregs (x) : 1);
1789
1790 return endregno > inner_regno && regno < inner_endregno;
1791 }
1792 break;
1793
1794 case CLOBBER:
1795 case SET:
1796 if (&SET_DEST (x) != loc
1797 /* Note setting a SUBREG counts as referring to the REG it is in for
1798 a pseudo but not for hard registers since we can
1799 treat each word individually. */
1800 && ((GET_CODE (SET_DEST (x)) == SUBREG
1801 && loc != &SUBREG_REG (SET_DEST (x))
1802 && REG_P (SUBREG_REG (SET_DEST (x)))
1803 && REGNO (SUBREG_REG (SET_DEST (x))) >= FIRST_PSEUDO_REGISTER
1804 && refers_to_regno_p (regno, endregno,
1805 SUBREG_REG (SET_DEST (x)), loc))
1806 || (!REG_P (SET_DEST (x))
1807 && refers_to_regno_p (regno, endregno, SET_DEST (x), loc))))
1808 return true;
1809
1810 if (code == CLOBBER || loc == &SET_SRC (x))
1811 return false;
1812 x = SET_SRC (x);
1813 goto repeat;
1814
1815 default:
1816 break;
1817 }
1818
1819 /* X does not match, so try its subexpressions. */
1820
1821 fmt = GET_RTX_FORMAT (code);
1822 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1823 {
1824 if (fmt[i] == 'e' && loc != &XEXP (x, i))
1825 {
1826 if (i == 0)
1827 {
1828 x = XEXP (x, 0);
1829 goto repeat;
1830 }
1831 else
1832 if (refers_to_regno_p (regno, endregno, XEXP (x, i), loc))
1833 return true;
1834 }
1835 else if (fmt[i] == 'E')
1836 {
1837 int j;
1838 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1839 if (loc != &XVECEXP (x, i, j)
1840 && refers_to_regno_p (regno, endregno, XVECEXP (x, i, j), loc))
1841 return true;
1842 }
1843 }
1844 return false;
1845 }
1846
1847 /* Rreturn true if modifying X will affect IN. If X is a register or a SUBREG,
1848 we check if any register number in X conflicts with the relevant register
1849 numbers. If X is a constant, return false. If X is a MEM, return true iff
1850 IN contains a MEM (we don't bother checking for memory addresses that can't
1851 conflict because we expect this to be a rare case. */
1852
1853 bool
1854 reg_overlap_mentioned_p (const_rtx x, const_rtx in)
1855 {
1856 unsigned int regno, endregno;
1857
1858 /* If either argument is a constant, then modifying X cannot
1859 affect IN. Here we look at IN, we can profitably combine
1860 CONSTANT_P (x) with the switch statement below. */
1861 if (CONSTANT_P (in))
1862 return false;
1863
1864 recurse:
1865 switch (GET_CODE (x))
1866 {
1867 case CLOBBER:
1868 case STRICT_LOW_PART:
1869 case ZERO_EXTRACT:
1870 case SIGN_EXTRACT:
1871 /* Overly conservative. */
1872 x = XEXP (x, 0);
1873 goto recurse;
1874
1875 case SUBREG:
1876 regno = REGNO (SUBREG_REG (x));
1877 if (regno < FIRST_PSEUDO_REGISTER)
1878 regno = subreg_regno (x);
1879 endregno = regno + (regno < FIRST_PSEUDO_REGISTER
1880 ? subreg_nregs (x) : 1);
1881 goto do_reg;
1882
1883 case REG:
1884 regno = REGNO (x);
1885 endregno = END_REGNO (x);
1886 do_reg:
1887 return refers_to_regno_p (regno, endregno, in, (rtx*) 0);
1888
1889 case MEM:
1890 {
1891 const char *fmt;
1892 int i;
1893
1894 if (MEM_P (in))
1895 return true;
1896
1897 fmt = GET_RTX_FORMAT (GET_CODE (in));
1898 for (i = GET_RTX_LENGTH (GET_CODE (in)) - 1; i >= 0; i--)
1899 if (fmt[i] == 'e')
1900 {
1901 if (reg_overlap_mentioned_p (x, XEXP (in, i)))
1902 return true;
1903 }
1904 else if (fmt[i] == 'E')
1905 {
1906 int j;
1907 for (j = XVECLEN (in, i) - 1; j >= 0; --j)
1908 if (reg_overlap_mentioned_p (x, XVECEXP (in, i, j)))
1909 return true;
1910 }
1911
1912 return false;
1913 }
1914
1915 case SCRATCH:
1916 case PC:
1917 return reg_mentioned_p (x, in);
1918
1919 case PARALLEL:
1920 {
1921 int i;
1922
1923 /* If any register in here refers to it we return true. */
1924 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
1925 if (XEXP (XVECEXP (x, 0, i), 0) != 0
1926 && reg_overlap_mentioned_p (XEXP (XVECEXP (x, 0, i), 0), in))
1927 return true;
1928 return false;
1929 }
1930
1931 default:
1932 gcc_assert (CONSTANT_P (x));
1933 return false;
1934 }
1935 }
1936
1937 /* Call FUN on each register or MEM that is stored into or clobbered by X.
1938 (X would be the pattern of an insn). DATA is an arbitrary pointer,
1939 ignored by note_stores, but passed to FUN.
1940
1941 FUN receives three arguments:
1942 1. the REG, MEM or PC being stored in or clobbered,
1943 2. the SET or CLOBBER rtx that does the store,
1944 3. the pointer DATA provided to note_stores.
1945
1946 If the item being stored in or clobbered is a SUBREG of a hard register,
1947 the SUBREG will be passed. */
1948
1949 void
1950 note_pattern_stores (const_rtx x,
1951 void (*fun) (rtx, const_rtx, void *), void *data)
1952 {
1953 int i;
1954
1955 if (GET_CODE (x) == COND_EXEC)
1956 x = COND_EXEC_CODE (x);
1957
1958 if (GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
1959 {
1960 rtx dest = SET_DEST (x);
1961
1962 while ((GET_CODE (dest) == SUBREG
1963 && (!REG_P (SUBREG_REG (dest))
1964 || REGNO (SUBREG_REG (dest)) >= FIRST_PSEUDO_REGISTER))
1965 || GET_CODE (dest) == ZERO_EXTRACT
1966 || GET_CODE (dest) == STRICT_LOW_PART)
1967 dest = XEXP (dest, 0);
1968
1969 /* If we have a PARALLEL, SET_DEST is a list of EXPR_LIST expressions,
1970 each of whose first operand is a register. */
1971 if (GET_CODE (dest) == PARALLEL)
1972 {
1973 for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
1974 if (XEXP (XVECEXP (dest, 0, i), 0) != 0)
1975 (*fun) (XEXP (XVECEXP (dest, 0, i), 0), x, data);
1976 }
1977 else
1978 (*fun) (dest, x, data);
1979 }
1980
1981 else if (GET_CODE (x) == PARALLEL)
1982 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
1983 note_pattern_stores (XVECEXP (x, 0, i), fun, data);
1984 }
1985
1986 /* Same, but for an instruction. If the instruction is a call, include
1987 any CLOBBERs in its CALL_INSN_FUNCTION_USAGE. */
1988
1989 void
1990 note_stores (const rtx_insn *insn,
1991 void (*fun) (rtx, const_rtx, void *), void *data)
1992 {
1993 if (CALL_P (insn))
1994 for (rtx link = CALL_INSN_FUNCTION_USAGE (insn);
1995 link; link = XEXP (link, 1))
1996 if (GET_CODE (XEXP (link, 0)) == CLOBBER)
1997 note_pattern_stores (XEXP (link, 0), fun, data);
1998 note_pattern_stores (PATTERN (insn), fun, data);
1999 }
2000
2001 /* Like notes_stores, but call FUN for each expression that is being
2002 referenced in PBODY, a pointer to the PATTERN of an insn. We only call
2003 FUN for each expression, not any interior subexpressions. FUN receives a
2004 pointer to the expression and the DATA passed to this function.
2005
2006 Note that this is not quite the same test as that done in reg_referenced_p
2007 since that considers something as being referenced if it is being
2008 partially set, while we do not. */
2009
2010 void
2011 note_uses (rtx *pbody, void (*fun) (rtx *, void *), void *data)
2012 {
2013 rtx body = *pbody;
2014 int i;
2015
2016 switch (GET_CODE (body))
2017 {
2018 case COND_EXEC:
2019 (*fun) (&COND_EXEC_TEST (body), data);
2020 note_uses (&COND_EXEC_CODE (body), fun, data);
2021 return;
2022
2023 case PARALLEL:
2024 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
2025 note_uses (&XVECEXP (body, 0, i), fun, data);
2026 return;
2027
2028 case SEQUENCE:
2029 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
2030 note_uses (&PATTERN (XVECEXP (body, 0, i)), fun, data);
2031 return;
2032
2033 case USE:
2034 (*fun) (&XEXP (body, 0), data);
2035 return;
2036
2037 case ASM_OPERANDS:
2038 for (i = ASM_OPERANDS_INPUT_LENGTH (body) - 1; i >= 0; i--)
2039 (*fun) (&ASM_OPERANDS_INPUT (body, i), data);
2040 return;
2041
2042 case TRAP_IF:
2043 (*fun) (&TRAP_CONDITION (body), data);
2044 return;
2045
2046 case PREFETCH:
2047 (*fun) (&XEXP (body, 0), data);
2048 return;
2049
2050 case UNSPEC:
2051 case UNSPEC_VOLATILE:
2052 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
2053 (*fun) (&XVECEXP (body, 0, i), data);
2054 return;
2055
2056 case CLOBBER:
2057 if (MEM_P (XEXP (body, 0)))
2058 (*fun) (&XEXP (XEXP (body, 0), 0), data);
2059 return;
2060
2061 case SET:
2062 {
2063 rtx dest = SET_DEST (body);
2064
2065 /* For sets we replace everything in source plus registers in memory
2066 expression in store and operands of a ZERO_EXTRACT. */
2067 (*fun) (&SET_SRC (body), data);
2068
2069 if (GET_CODE (dest) == ZERO_EXTRACT)
2070 {
2071 (*fun) (&XEXP (dest, 1), data);
2072 (*fun) (&XEXP (dest, 2), data);
2073 }
2074
2075 while (GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART)
2076 dest = XEXP (dest, 0);
2077
2078 if (MEM_P (dest))
2079 (*fun) (&XEXP (dest, 0), data);
2080 }
2081 return;
2082
2083 default:
2084 /* All the other possibilities never store. */
2085 (*fun) (pbody, data);
2086 return;
2087 }
2088 }
2089
2090 /* Try to add a description of REG X to this object, stopping once
2091 the REF_END limit has been reached. FLAGS is a bitmask of
2092 rtx_obj_reference flags that describe the context. */
2093
2094 void
2095 rtx_properties::try_to_add_reg (const_rtx x, unsigned int flags)
2096 {
2097 if (REG_NREGS (x) != 1)
2098 flags |= rtx_obj_flags::IS_MULTIREG;
2099 machine_mode mode = GET_MODE (x);
2100 unsigned int start_regno = REGNO (x);
2101 unsigned int end_regno = END_REGNO (x);
2102 for (unsigned int regno = start_regno; regno < end_regno; ++regno)
2103 if (ref_iter != ref_end)
2104 *ref_iter++ = rtx_obj_reference (regno, flags, mode,
2105 regno - start_regno);
2106 }
2107
2108 /* Add a description of destination X to this object. FLAGS is a bitmask
2109 of rtx_obj_reference flags that describe the context.
2110
2111 This routine accepts all rtxes that can legitimately appear in a
2112 SET_DEST. */
2113
2114 void
2115 rtx_properties::try_to_add_dest (const_rtx x, unsigned int flags)
2116 {
2117 /* If we have a PARALLEL, SET_DEST is a list of EXPR_LIST expressions,
2118 each of whose first operand is a register. */
2119 if (UNLIKELY (GET_CODE (x) == PARALLEL))
2120 {
2121 for (int i = XVECLEN (x, 0) - 1; i >= 0; --i)
2122 if (rtx dest = XEXP (XVECEXP (x, 0, i), 0))
2123 try_to_add_dest (dest, flags);
2124 return;
2125 }
2126
2127 unsigned int base_flags = flags & rtx_obj_flags::STICKY_FLAGS;
2128 flags |= rtx_obj_flags::IS_WRITE;
2129 for (;;)
2130 if (GET_CODE (x) == ZERO_EXTRACT)
2131 {
2132 try_to_add_src (XEXP (x, 1), base_flags);
2133 try_to_add_src (XEXP (x, 2), base_flags);
2134 flags |= rtx_obj_flags::IS_READ;
2135 x = XEXP (x, 0);
2136 }
2137 else if (GET_CODE (x) == STRICT_LOW_PART)
2138 {
2139 flags |= rtx_obj_flags::IS_READ;
2140 x = XEXP (x, 0);
2141 }
2142 else if (GET_CODE (x) == SUBREG)
2143 {
2144 flags |= rtx_obj_flags::IN_SUBREG;
2145 if (read_modify_subreg_p (x))
2146 flags |= rtx_obj_flags::IS_READ;
2147 x = SUBREG_REG (x);
2148 }
2149 else
2150 break;
2151
2152 if (MEM_P (x))
2153 {
2154 if (ref_iter != ref_end)
2155 *ref_iter++ = rtx_obj_reference (MEM_REGNO, flags, GET_MODE (x));
2156
2157 unsigned int addr_flags = base_flags | rtx_obj_flags::IN_MEM_STORE;
2158 if (flags & rtx_obj_flags::IS_READ)
2159 addr_flags |= rtx_obj_flags::IN_MEM_LOAD;
2160 try_to_add_src (XEXP (x, 0), addr_flags);
2161 return;
2162 }
2163
2164 if (LIKELY (REG_P (x)))
2165 {
2166 if (REGNO (x) == STACK_POINTER_REGNUM)
2167 {
2168 /* Stack accesses are dependent on previous allocations and
2169 anti-dependent on later deallocations, so both types of
2170 stack operation are akin to a memory write. */
2171 if (ref_iter != ref_end)
2172 *ref_iter++ = rtx_obj_reference (MEM_REGNO, flags, BLKmode);
2173
2174 /* We want to keep sp alive everywhere - by making all
2175 writes to sp also use sp. */
2176 flags |= rtx_obj_flags::IS_READ;
2177 }
2178 try_to_add_reg (x, flags);
2179 return;
2180 }
2181 }
2182
2183 /* Try to add a description of source X to this object, stopping once
2184 the REF_END limit has been reached. FLAGS is a bitmask of
2185 rtx_obj_reference flags that describe the context.
2186
2187 This routine accepts all rtxes that can legitimately appear in a SET_SRC. */
2188
2189 void
2190 rtx_properties::try_to_add_src (const_rtx x, unsigned int flags)
2191 {
2192 unsigned int base_flags = flags & rtx_obj_flags::STICKY_FLAGS;
2193 subrtx_iterator::array_type array;
2194 FOR_EACH_SUBRTX (iter, array, x, NONCONST)
2195 {
2196 const_rtx x = *iter;
2197 rtx_code code = GET_CODE (x);
2198 if (code == REG)
2199 try_to_add_reg (x, flags | rtx_obj_flags::IS_READ);
2200 else if (code == MEM)
2201 {
2202 if (MEM_VOLATILE_P (x))
2203 has_volatile_refs = true;
2204
2205 if (!MEM_READONLY_P (x) && ref_iter != ref_end)
2206 {
2207 auto mem_flags = flags | rtx_obj_flags::IS_READ;
2208 *ref_iter++ = rtx_obj_reference (MEM_REGNO, mem_flags,
2209 GET_MODE (x));
2210 }
2211
2212 try_to_add_src (XEXP (x, 0),
2213 base_flags | rtx_obj_flags::IN_MEM_LOAD);
2214 iter.skip_subrtxes ();
2215 }
2216 else if (code == SUBREG)
2217 {
2218 try_to_add_src (SUBREG_REG (x), flags | rtx_obj_flags::IN_SUBREG);
2219 iter.skip_subrtxes ();
2220 }
2221 else if (code == UNSPEC_VOLATILE)
2222 has_volatile_refs = true;
2223 else if (code == ASM_INPUT || code == ASM_OPERANDS)
2224 {
2225 has_asm = true;
2226 if (MEM_VOLATILE_P (x))
2227 has_volatile_refs = true;
2228 }
2229 else if (code == PRE_INC
2230 || code == PRE_DEC
2231 || code == POST_INC
2232 || code == POST_DEC
2233 || code == PRE_MODIFY
2234 || code == POST_MODIFY)
2235 {
2236 has_pre_post_modify = true;
2237
2238 unsigned int addr_flags = (flags
2239 | rtx_obj_flags::IS_PRE_POST_MODIFY
2240 | rtx_obj_flags::IS_READ);
2241 try_to_add_dest (XEXP (x, 0), addr_flags);
2242 if (code == PRE_MODIFY || code == POST_MODIFY)
2243 iter.substitute (XEXP (XEXP (x, 1), 1));
2244 else
2245 iter.skip_subrtxes ();
2246 }
2247 else if (code == CALL)
2248 has_call = true;
2249 }
2250 }
2251
2252 /* Try to add a description of instruction pattern PAT to this object,
2253 stopping once the REF_END limit has been reached. */
2254
2255 void
2256 rtx_properties::try_to_add_pattern (const_rtx pat)
2257 {
2258 switch (GET_CODE (pat))
2259 {
2260 case COND_EXEC:
2261 try_to_add_src (COND_EXEC_TEST (pat));
2262 try_to_add_pattern (COND_EXEC_CODE (pat));
2263 break;
2264
2265 case PARALLEL:
2266 {
2267 int last = XVECLEN (pat, 0) - 1;
2268 for (int i = 0; i < last; ++i)
2269 try_to_add_pattern (XVECEXP (pat, 0, i));
2270 try_to_add_pattern (XVECEXP (pat, 0, last));
2271 break;
2272 }
2273
2274 case ASM_OPERANDS:
2275 for (int i = 0, len = ASM_OPERANDS_INPUT_LENGTH (pat); i < len; ++i)
2276 try_to_add_src (ASM_OPERANDS_INPUT (pat, i));
2277 break;
2278
2279 case CLOBBER:
2280 try_to_add_dest (XEXP (pat, 0), rtx_obj_flags::IS_CLOBBER);
2281 break;
2282
2283 case SET:
2284 try_to_add_dest (SET_DEST (pat));
2285 try_to_add_src (SET_SRC (pat));
2286 break;
2287
2288 default:
2289 /* All the other possibilities never store and can use a normal
2290 rtx walk. This includes:
2291
2292 - USE
2293 - TRAP_IF
2294 - PREFETCH
2295 - UNSPEC
2296 - UNSPEC_VOLATILE. */
2297 try_to_add_src (pat);
2298 break;
2299 }
2300 }
2301
2302 /* Try to add a description of INSN to this object, stopping once
2303 the REF_END limit has been reached. INCLUDE_NOTES is true if the
2304 description should include REG_EQUAL and REG_EQUIV notes; all such
2305 references will then be marked with rtx_obj_flags::IN_NOTE.
2306
2307 For calls, this description includes all accesses in
2308 CALL_INSN_FUNCTION_USAGE. It also include all implicit accesses
2309 to global registers by the target function. However, it does not
2310 include clobbers performed by the target function; callers that want
2311 this information should instead use the function_abi interface. */
2312
2313 void
2314 rtx_properties::try_to_add_insn (const rtx_insn *insn, bool include_notes)
2315 {
2316 if (CALL_P (insn))
2317 {
2318 /* Non-const functions can read from global registers. Impure
2319 functions can also set them.
2320
2321 Adding the global registers first removes a situation in which
2322 a fixed-form clobber of register R could come before a real set
2323 of register R. */
2324 if (!hard_reg_set_empty_p (global_reg_set)
2325 && !RTL_CONST_CALL_P (insn))
2326 {
2327 unsigned int flags = rtx_obj_flags::IS_READ;
2328 if (!RTL_PURE_CALL_P (insn))
2329 flags |= rtx_obj_flags::IS_WRITE;
2330 for (unsigned int regno = 0; regno < FIRST_PSEUDO_REGISTER; ++regno)
2331 /* As a special case, the stack pointer is invariant across calls
2332 even if it has been marked global; see the corresponding
2333 handling in df_get_call_refs. */
2334 if (regno != STACK_POINTER_REGNUM
2335 && global_regs[regno]
2336 && ref_iter != ref_end)
2337 *ref_iter++ = rtx_obj_reference (regno, flags,
2338 reg_raw_mode[regno], 0);
2339 }
2340 /* Untyped calls implicitly set all function value registers.
2341 Again, we add them first in case the main pattern contains
2342 a fixed-form clobber. */
2343 if (find_reg_note (insn, REG_UNTYPED_CALL, NULL_RTX))
2344 for (unsigned int regno = 0; regno < FIRST_PSEUDO_REGISTER; ++regno)
2345 if (targetm.calls.function_value_regno_p (regno)
2346 && ref_iter != ref_end)
2347 *ref_iter++ = rtx_obj_reference (regno, rtx_obj_flags::IS_WRITE,
2348 reg_raw_mode[regno], 0);
2349 if (ref_iter != ref_end && !RTL_CONST_CALL_P (insn))
2350 {
2351 auto mem_flags = rtx_obj_flags::IS_READ;
2352 if (!RTL_PURE_CALL_P (insn))
2353 mem_flags |= rtx_obj_flags::IS_WRITE;
2354 *ref_iter++ = rtx_obj_reference (MEM_REGNO, mem_flags, BLKmode);
2355 }
2356 try_to_add_pattern (PATTERN (insn));
2357 for (rtx link = CALL_INSN_FUNCTION_USAGE (insn); link;
2358 link = XEXP (link, 1))
2359 {
2360 rtx x = XEXP (link, 0);
2361 if (GET_CODE (x) == CLOBBER)
2362 try_to_add_dest (XEXP (x, 0), rtx_obj_flags::IS_CLOBBER);
2363 else if (GET_CODE (x) == USE)
2364 try_to_add_src (XEXP (x, 0));
2365 }
2366 }
2367 else
2368 try_to_add_pattern (PATTERN (insn));
2369
2370 if (include_notes)
2371 for (rtx note = REG_NOTES (insn); note; note = XEXP (note, 1))
2372 if (REG_NOTE_KIND (note) == REG_EQUAL
2373 || REG_NOTE_KIND (note) == REG_EQUIV)
2374 try_to_add_note (XEXP (note, 0));
2375 }
2376
2377 /* Grow the storage by a bit while keeping the contents of the first
2378 START elements. */
2379
2380 void
2381 vec_rtx_properties_base::grow (ptrdiff_t start)
2382 {
2383 /* The same heuristic that vec uses. */
2384 ptrdiff_t new_elems = (ref_end - ref_begin) * 3 / 2;
2385 if (ref_begin == m_storage)
2386 {
2387 ref_begin = XNEWVEC (rtx_obj_reference, new_elems);
2388 if (start)
2389 memcpy (ref_begin, m_storage, start * sizeof (rtx_obj_reference));
2390 }
2391 else
2392 ref_begin = reinterpret_cast<rtx_obj_reference *>
2393 (xrealloc (ref_begin, new_elems * sizeof (rtx_obj_reference)));
2394 ref_iter = ref_begin + start;
2395 ref_end = ref_begin + new_elems;
2396 }
2397
2398 /* Return true if X's old contents don't survive after INSN.
2399 This will be true if X is a register and X dies in INSN or because
2400 INSN entirely sets X.
2401
2402 "Entirely set" means set directly and not through a SUBREG, or
2403 ZERO_EXTRACT, so no trace of the old contents remains.
2404 Likewise, REG_INC does not count.
2405
2406 REG may be a hard or pseudo reg. Renumbering is not taken into account,
2407 but for this use that makes no difference, since regs don't overlap
2408 during their lifetimes. Therefore, this function may be used
2409 at any time after deaths have been computed.
2410
2411 If REG is a hard reg that occupies multiple machine registers, this
2412 function will only return true if each of those registers will be replaced
2413 by INSN. */
2414
2415 bool
2416 dead_or_set_p (const rtx_insn *insn, const_rtx x)
2417 {
2418 unsigned int regno, end_regno;
2419 unsigned int i;
2420
2421 gcc_assert (REG_P (x));
2422
2423 regno = REGNO (x);
2424 end_regno = END_REGNO (x);
2425 for (i = regno; i < end_regno; i++)
2426 if (! dead_or_set_regno_p (insn, i))
2427 return false;
2428
2429 return true;
2430 }
2431
2432 /* Return TRUE iff DEST is a register or subreg of a register, is a
2433 complete rather than read-modify-write destination, and contains
2434 register TEST_REGNO. */
2435
2436 static bool
2437 covers_regno_no_parallel_p (const_rtx dest, unsigned int test_regno)
2438 {
2439 unsigned int regno, endregno;
2440
2441 if (GET_CODE (dest) == SUBREG && !read_modify_subreg_p (dest))
2442 dest = SUBREG_REG (dest);
2443
2444 if (!REG_P (dest))
2445 return false;
2446
2447 regno = REGNO (dest);
2448 endregno = END_REGNO (dest);
2449 return (test_regno >= regno && test_regno < endregno);
2450 }
2451
2452 /* Like covers_regno_no_parallel_p, but also handles PARALLELs where
2453 any member matches the covers_regno_no_parallel_p criteria. */
2454
2455 static bool
2456 covers_regno_p (const_rtx dest, unsigned int test_regno)
2457 {
2458 if (GET_CODE (dest) == PARALLEL)
2459 {
2460 /* Some targets place small structures in registers for return
2461 values of functions, and those registers are wrapped in
2462 PARALLELs that we may see as the destination of a SET. */
2463 int i;
2464
2465 for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
2466 {
2467 rtx inner = XEXP (XVECEXP (dest, 0, i), 0);
2468 if (inner != NULL_RTX
2469 && covers_regno_no_parallel_p (inner, test_regno))
2470 return true;
2471 }
2472
2473 return false;
2474 }
2475 else
2476 return covers_regno_no_parallel_p (dest, test_regno);
2477 }
2478
2479 /* Utility function for dead_or_set_p to check an individual register. */
2480
2481 bool
2482 dead_or_set_regno_p (const rtx_insn *insn, unsigned int test_regno)
2483 {
2484 const_rtx pattern;
2485
2486 /* See if there is a death note for something that includes TEST_REGNO. */
2487 if (find_regno_note (insn, REG_DEAD, test_regno))
2488 return true;
2489
2490 if (CALL_P (insn)
2491 && find_regno_fusage (insn, CLOBBER, test_regno))
2492 return true;
2493
2494 pattern = PATTERN (insn);
2495
2496 /* If a COND_EXEC is not executed, the value survives. */
2497 if (GET_CODE (pattern) == COND_EXEC)
2498 return false;
2499
2500 if (GET_CODE (pattern) == SET || GET_CODE (pattern) == CLOBBER)
2501 return covers_regno_p (SET_DEST (pattern), test_regno);
2502 else if (GET_CODE (pattern) == PARALLEL)
2503 {
2504 int i;
2505
2506 for (i = XVECLEN (pattern, 0) - 1; i >= 0; i--)
2507 {
2508 rtx body = XVECEXP (pattern, 0, i);
2509
2510 if (GET_CODE (body) == COND_EXEC)
2511 body = COND_EXEC_CODE (body);
2512
2513 if ((GET_CODE (body) == SET || GET_CODE (body) == CLOBBER)
2514 && covers_regno_p (SET_DEST (body), test_regno))
2515 return true;
2516 }
2517 }
2518
2519 return false;
2520 }
2521
2522 /* Return the reg-note of kind KIND in insn INSN, if there is one.
2523 If DATUM is nonzero, look for one whose datum is DATUM. */
2524
2525 rtx
2526 find_reg_note (const_rtx insn, enum reg_note kind, const_rtx datum)
2527 {
2528 rtx link;
2529
2530 gcc_checking_assert (insn);
2531
2532 /* Ignore anything that is not an INSN, JUMP_INSN or CALL_INSN. */
2533 if (! INSN_P (insn))
2534 return 0;
2535 if (datum == 0)
2536 {
2537 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2538 if (REG_NOTE_KIND (link) == kind)
2539 return link;
2540 return 0;
2541 }
2542
2543 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2544 if (REG_NOTE_KIND (link) == kind && datum == XEXP (link, 0))
2545 return link;
2546 return 0;
2547 }
2548
2549 /* Return the reg-note of kind KIND in insn INSN which applies to register
2550 number REGNO, if any. Return 0 if there is no such reg-note. Note that
2551 the REGNO of this NOTE need not be REGNO if REGNO is a hard register;
2552 it might be the case that the note overlaps REGNO. */
2553
2554 rtx
2555 find_regno_note (const_rtx insn, enum reg_note kind, unsigned int regno)
2556 {
2557 rtx link;
2558
2559 /* Ignore anything that is not an INSN, JUMP_INSN or CALL_INSN. */
2560 if (! INSN_P (insn))
2561 return 0;
2562
2563 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2564 if (REG_NOTE_KIND (link) == kind
2565 /* Verify that it is a register, so that scratch and MEM won't cause a
2566 problem here. */
2567 && REG_P (XEXP (link, 0))
2568 && REGNO (XEXP (link, 0)) <= regno
2569 && END_REGNO (XEXP (link, 0)) > regno)
2570 return link;
2571 return 0;
2572 }
2573
2574 /* Return a REG_EQUIV or REG_EQUAL note if insn has only a single set and
2575 has such a note. */
2576
2577 rtx
2578 find_reg_equal_equiv_note (const_rtx insn)
2579 {
2580 rtx link;
2581
2582 if (!INSN_P (insn))
2583 return 0;
2584
2585 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2586 if (REG_NOTE_KIND (link) == REG_EQUAL
2587 || REG_NOTE_KIND (link) == REG_EQUIV)
2588 {
2589 /* FIXME: We should never have REG_EQUAL/REG_EQUIV notes on
2590 insns that have multiple sets. Checking single_set to
2591 make sure of this is not the proper check, as explained
2592 in the comment in set_unique_reg_note.
2593
2594 This should be changed into an assert. */
2595 if (GET_CODE (PATTERN (insn)) == PARALLEL && multiple_sets (insn))
2596 return 0;
2597 return link;
2598 }
2599 return NULL;
2600 }
2601
2602 /* Check whether INSN is a single_set whose source is known to be
2603 equivalent to a constant. Return that constant if so, otherwise
2604 return null. */
2605
2606 rtx
2607 find_constant_src (const rtx_insn *insn)
2608 {
2609 rtx note, set, x;
2610
2611 set = single_set (insn);
2612 if (set)
2613 {
2614 x = avoid_constant_pool_reference (SET_SRC (set));
2615 if (CONSTANT_P (x))
2616 return x;
2617 }
2618
2619 note = find_reg_equal_equiv_note (insn);
2620 if (note && CONSTANT_P (XEXP (note, 0)))
2621 return XEXP (note, 0);
2622
2623 return NULL_RTX;
2624 }
2625
2626 /* Return true if DATUM, or any overlap of DATUM, of kind CODE is found
2627 in the CALL_INSN_FUNCTION_USAGE information of INSN. */
2628
2629 bool
2630 find_reg_fusage (const_rtx insn, enum rtx_code code, const_rtx datum)
2631 {
2632 /* If it's not a CALL_INSN, it can't possibly have a
2633 CALL_INSN_FUNCTION_USAGE field, so don't bother checking. */
2634 if (!CALL_P (insn))
2635 return false;
2636
2637 gcc_assert (datum);
2638
2639 if (!REG_P (datum))
2640 {
2641 rtx link;
2642
2643 for (link = CALL_INSN_FUNCTION_USAGE (insn);
2644 link;
2645 link = XEXP (link, 1))
2646 if (GET_CODE (XEXP (link, 0)) == code
2647 && rtx_equal_p (datum, XEXP (XEXP (link, 0), 0)))
2648 return true;
2649 }
2650 else
2651 {
2652 unsigned int regno = REGNO (datum);
2653
2654 /* CALL_INSN_FUNCTION_USAGE information cannot contain references
2655 to pseudo registers, so don't bother checking. */
2656
2657 if (regno < FIRST_PSEUDO_REGISTER)
2658 {
2659 unsigned int end_regno = END_REGNO (datum);
2660 unsigned int i;
2661
2662 for (i = regno; i < end_regno; i++)
2663 if (find_regno_fusage (insn, code, i))
2664 return true;
2665 }
2666 }
2667
2668 return false;
2669 }
2670
2671 /* Return true if REGNO, or any overlap of REGNO, of kind CODE is found
2672 in the CALL_INSN_FUNCTION_USAGE information of INSN. */
2673
2674 bool
2675 find_regno_fusage (const_rtx insn, enum rtx_code code, unsigned int regno)
2676 {
2677 rtx link;
2678
2679 /* CALL_INSN_FUNCTION_USAGE information cannot contain references
2680 to pseudo registers, so don't bother checking. */
2681
2682 if (regno >= FIRST_PSEUDO_REGISTER
2683 || !CALL_P (insn) )
2684 return false;
2685
2686 for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
2687 {
2688 rtx op, reg;
2689
2690 if (GET_CODE (op = XEXP (link, 0)) == code
2691 && REG_P (reg = XEXP (op, 0))
2692 && REGNO (reg) <= regno
2693 && END_REGNO (reg) > regno)
2694 return true;
2695 }
2696
2697 return false;
2698 }
2699
2700
2701 /* Return true if KIND is an integer REG_NOTE. */
2702
2703 static bool
2704 int_reg_note_p (enum reg_note kind)
2705 {
2706 return kind == REG_BR_PROB;
2707 }
2708
2709 /* Allocate a register note with kind KIND and datum DATUM. LIST is
2710 stored as the pointer to the next register note. */
2711
2712 rtx
2713 alloc_reg_note (enum reg_note kind, rtx datum, rtx list)
2714 {
2715 rtx note;
2716
2717 gcc_checking_assert (!int_reg_note_p (kind));
2718 switch (kind)
2719 {
2720 case REG_LABEL_TARGET:
2721 case REG_LABEL_OPERAND:
2722 case REG_TM:
2723 /* These types of register notes use an INSN_LIST rather than an
2724 EXPR_LIST, so that copying is done right and dumps look
2725 better. */
2726 note = alloc_INSN_LIST (datum, list);
2727 PUT_REG_NOTE_KIND (note, kind);
2728 break;
2729
2730 default:
2731 note = alloc_EXPR_LIST (kind, datum, list);
2732 break;
2733 }
2734
2735 return note;
2736 }
2737
2738 /* Add register note with kind KIND and datum DATUM to INSN. */
2739
2740 void
2741 add_reg_note (rtx insn, enum reg_note kind, rtx datum)
2742 {
2743 REG_NOTES (insn) = alloc_reg_note (kind, datum, REG_NOTES (insn));
2744 }
2745
2746 /* Add an integer register note with kind KIND and datum DATUM to INSN. */
2747
2748 void
2749 add_int_reg_note (rtx_insn *insn, enum reg_note kind, int datum)
2750 {
2751 gcc_checking_assert (int_reg_note_p (kind));
2752 REG_NOTES (insn) = gen_rtx_INT_LIST ((machine_mode) kind,
2753 datum, REG_NOTES (insn));
2754 }
2755
2756 /* Add a REG_ARGS_SIZE note to INSN with value VALUE. */
2757
2758 void
2759 add_args_size_note (rtx_insn *insn, poly_int64 value)
2760 {
2761 gcc_checking_assert (!find_reg_note (insn, REG_ARGS_SIZE, NULL_RTX));
2762 add_reg_note (insn, REG_ARGS_SIZE, gen_int_mode (value, Pmode));
2763 }
2764
2765 /* Add a register note like NOTE to INSN. */
2766
2767 void
2768 add_shallow_copy_of_reg_note (rtx_insn *insn, rtx note)
2769 {
2770 if (GET_CODE (note) == INT_LIST)
2771 add_int_reg_note (insn, REG_NOTE_KIND (note), XINT (note, 0));
2772 else
2773 add_reg_note (insn, REG_NOTE_KIND (note), XEXP (note, 0));
2774 }
2775
2776 /* Duplicate NOTE and return the copy. */
2777 rtx
2778 duplicate_reg_note (rtx note)
2779 {
2780 reg_note kind = REG_NOTE_KIND (note);
2781
2782 if (GET_CODE (note) == INT_LIST)
2783 return gen_rtx_INT_LIST ((machine_mode) kind, XINT (note, 0), NULL_RTX);
2784 else if (GET_CODE (note) == EXPR_LIST)
2785 return alloc_reg_note (kind, copy_insn_1 (XEXP (note, 0)), NULL_RTX);
2786 else
2787 return alloc_reg_note (kind, XEXP (note, 0), NULL_RTX);
2788 }
2789
2790 /* Remove register note NOTE from the REG_NOTES of INSN. */
2791
2792 void
2793 remove_note (rtx_insn *insn, const_rtx note)
2794 {
2795 rtx link;
2796
2797 if (note == NULL_RTX)
2798 return;
2799
2800 if (REG_NOTES (insn) == note)
2801 REG_NOTES (insn) = XEXP (note, 1);
2802 else
2803 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2804 if (XEXP (link, 1) == note)
2805 {
2806 XEXP (link, 1) = XEXP (note, 1);
2807 break;
2808 }
2809
2810 switch (REG_NOTE_KIND (note))
2811 {
2812 case REG_EQUAL:
2813 case REG_EQUIV:
2814 df_notes_rescan (insn);
2815 break;
2816 default:
2817 break;
2818 }
2819 }
2820
2821 /* Remove REG_EQUAL and/or REG_EQUIV notes if INSN has such notes.
2822 If NO_RESCAN is false and any notes were removed, call
2823 df_notes_rescan. Return true if any note has been removed. */
2824
2825 bool
2826 remove_reg_equal_equiv_notes (rtx_insn *insn, bool no_rescan)
2827 {
2828 rtx *loc;
2829 bool ret = false;
2830
2831 loc = &REG_NOTES (insn);
2832 while (*loc)
2833 {
2834 enum reg_note kind = REG_NOTE_KIND (*loc);
2835 if (kind == REG_EQUAL || kind == REG_EQUIV)
2836 {
2837 *loc = XEXP (*loc, 1);
2838 ret = true;
2839 }
2840 else
2841 loc = &XEXP (*loc, 1);
2842 }
2843 if (ret && !no_rescan)
2844 df_notes_rescan (insn);
2845 return ret;
2846 }
2847
2848 /* Remove all REG_EQUAL and REG_EQUIV notes referring to REGNO. */
2849
2850 void
2851 remove_reg_equal_equiv_notes_for_regno (unsigned int regno)
2852 {
2853 df_ref eq_use;
2854
2855 if (!df)
2856 return;
2857
2858 /* This loop is a little tricky. We cannot just go down the chain because
2859 it is being modified by some actions in the loop. So we just iterate
2860 over the head. We plan to drain the list anyway. */
2861 while ((eq_use = DF_REG_EQ_USE_CHAIN (regno)) != NULL)
2862 {
2863 rtx_insn *insn = DF_REF_INSN (eq_use);
2864 rtx note = find_reg_equal_equiv_note (insn);
2865
2866 /* This assert is generally triggered when someone deletes a REG_EQUAL
2867 or REG_EQUIV note by hacking the list manually rather than calling
2868 remove_note. */
2869 gcc_assert (note);
2870
2871 remove_note (insn, note);
2872 }
2873 }
2874
2875 /* Search LISTP (an EXPR_LIST) for an entry whose first operand is NODE and
2876 return 1 if it is found. A simple equality test is used to determine if
2877 NODE matches. */
2878
2879 bool
2880 in_insn_list_p (const rtx_insn_list *listp, const rtx_insn *node)
2881 {
2882 const_rtx x;
2883
2884 for (x = listp; x; x = XEXP (x, 1))
2885 if (node == XEXP (x, 0))
2886 return true;
2887
2888 return false;
2889 }
2890
2891 /* Search LISTP (an INSN_LIST) for an entry whose first operand is NODE and
2892 remove that entry from the list if it is found.
2893
2894 A simple equality test is used to determine if NODE matches. */
2895
2896 void
2897 remove_node_from_insn_list (const rtx_insn *node, rtx_insn_list **listp)
2898 {
2899 rtx_insn_list *temp = *listp;
2900 rtx_insn_list *prev = NULL;
2901
2902 while (temp)
2903 {
2904 if (node == temp->insn ())
2905 {
2906 /* Splice the node out of the list. */
2907 if (prev)
2908 XEXP (prev, 1) = temp->next ();
2909 else
2910 *listp = temp->next ();
2911
2912 gcc_checking_assert (!in_insn_list_p (temp->next (), node));
2913 return;
2914 }
2915
2916 prev = temp;
2917 temp = temp->next ();
2918 }
2919 }
2920
2921 /* Return true if X contains any volatile instructions. These are instructions
2922 which may cause unpredictable machine state instructions, and thus no
2923 instructions or register uses should be moved or combined across them.
2924 This includes only volatile asms and UNSPEC_VOLATILE instructions. */
2925
2926 bool
2927 volatile_insn_p (const_rtx x)
2928 {
2929 const RTX_CODE code = GET_CODE (x);
2930 switch (code)
2931 {
2932 case LABEL_REF:
2933 case SYMBOL_REF:
2934 case CONST:
2935 CASE_CONST_ANY:
2936 case PC:
2937 case REG:
2938 case SCRATCH:
2939 case CLOBBER:
2940 case ADDR_VEC:
2941 case ADDR_DIFF_VEC:
2942 case CALL:
2943 case MEM:
2944 return false;
2945
2946 case UNSPEC_VOLATILE:
2947 return true;
2948
2949 case ASM_INPUT:
2950 case ASM_OPERANDS:
2951 if (MEM_VOLATILE_P (x))
2952 return true;
2953
2954 default:
2955 break;
2956 }
2957
2958 /* Recursively scan the operands of this expression. */
2959
2960 {
2961 const char *const fmt = GET_RTX_FORMAT (code);
2962 int i;
2963
2964 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2965 {
2966 if (fmt[i] == 'e')
2967 {
2968 if (volatile_insn_p (XEXP (x, i)))
2969 return true;
2970 }
2971 else if (fmt[i] == 'E')
2972 {
2973 int j;
2974 for (j = 0; j < XVECLEN (x, i); j++)
2975 if (volatile_insn_p (XVECEXP (x, i, j)))
2976 return true;
2977 }
2978 }
2979 }
2980 return false;
2981 }
2982
2983 /* Return true if X contains any volatile memory references
2984 UNSPEC_VOLATILE operations or volatile ASM_OPERANDS expressions. */
2985
2986 bool
2987 volatile_refs_p (const_rtx x)
2988 {
2989 const RTX_CODE code = GET_CODE (x);
2990 switch (code)
2991 {
2992 case LABEL_REF:
2993 case SYMBOL_REF:
2994 case CONST:
2995 CASE_CONST_ANY:
2996 case PC:
2997 case REG:
2998 case SCRATCH:
2999 case CLOBBER:
3000 case ADDR_VEC:
3001 case ADDR_DIFF_VEC:
3002 return false;
3003
3004 case UNSPEC_VOLATILE:
3005 return true;
3006
3007 case MEM:
3008 case ASM_INPUT:
3009 case ASM_OPERANDS:
3010 if (MEM_VOLATILE_P (x))
3011 return true;
3012
3013 default:
3014 break;
3015 }
3016
3017 /* Recursively scan the operands of this expression. */
3018
3019 {
3020 const char *const fmt = GET_RTX_FORMAT (code);
3021 int i;
3022
3023 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3024 {
3025 if (fmt[i] == 'e')
3026 {
3027 if (volatile_refs_p (XEXP (x, i)))
3028 return true;
3029 }
3030 else if (fmt[i] == 'E')
3031 {
3032 int j;
3033 for (j = 0; j < XVECLEN (x, i); j++)
3034 if (volatile_refs_p (XVECEXP (x, i, j)))
3035 return true;
3036 }
3037 }
3038 }
3039 return false;
3040 }
3041
3042 /* Similar to above, except that it also rejects register pre- and post-
3043 incrementing. */
3044
3045 bool
3046 side_effects_p (const_rtx x)
3047 {
3048 const RTX_CODE code = GET_CODE (x);
3049 switch (code)
3050 {
3051 case LABEL_REF:
3052 case SYMBOL_REF:
3053 case CONST:
3054 CASE_CONST_ANY:
3055 case PC:
3056 case REG:
3057 case SCRATCH:
3058 case ADDR_VEC:
3059 case ADDR_DIFF_VEC:
3060 case VAR_LOCATION:
3061 return false;
3062
3063 case CLOBBER:
3064 /* Reject CLOBBER with a non-VOID mode. These are made by combine.cc
3065 when some combination can't be done. If we see one, don't think
3066 that we can simplify the expression. */
3067 return (GET_MODE (x) != VOIDmode);
3068
3069 case PRE_INC:
3070 case PRE_DEC:
3071 case POST_INC:
3072 case POST_DEC:
3073 case PRE_MODIFY:
3074 case POST_MODIFY:
3075 case CALL:
3076 case UNSPEC_VOLATILE:
3077 return true;
3078
3079 case MEM:
3080 case ASM_INPUT:
3081 case ASM_OPERANDS:
3082 if (MEM_VOLATILE_P (x))
3083 return true;
3084
3085 default:
3086 break;
3087 }
3088
3089 /* Recursively scan the operands of this expression. */
3090
3091 {
3092 const char *fmt = GET_RTX_FORMAT (code);
3093 int i;
3094
3095 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3096 {
3097 if (fmt[i] == 'e')
3098 {
3099 if (side_effects_p (XEXP (x, i)))
3100 return true;
3101 }
3102 else if (fmt[i] == 'E')
3103 {
3104 int j;
3105 for (j = 0; j < XVECLEN (x, i); j++)
3106 if (side_effects_p (XVECEXP (x, i, j)))
3107 return true;
3108 }
3109 }
3110 }
3111 return false;
3112 }
3113
3114 /* Return true if evaluating rtx X might cause a trap.
3115 FLAGS controls how to consider MEMs. A true means the context
3116 of the access may have changed from the original, such that the
3117 address may have become invalid. */
3118
3119 bool
3120 may_trap_p_1 (const_rtx x, unsigned flags)
3121 {
3122 int i;
3123 enum rtx_code code;
3124 const char *fmt;
3125
3126 /* We make no distinction currently, but this function is part of
3127 the internal target-hooks ABI so we keep the parameter as
3128 "unsigned flags". */
3129 bool code_changed = flags != 0;
3130
3131 if (x == 0)
3132 return false;
3133 code = GET_CODE (x);
3134 switch (code)
3135 {
3136 /* Handle these cases quickly. */
3137 CASE_CONST_ANY:
3138 case SYMBOL_REF:
3139 case LABEL_REF:
3140 case CONST:
3141 case PC:
3142 case REG:
3143 case SCRATCH:
3144 return false;
3145
3146 case UNSPEC:
3147 return targetm.unspec_may_trap_p (x, flags);
3148
3149 case UNSPEC_VOLATILE:
3150 case ASM_INPUT:
3151 case TRAP_IF:
3152 return true;
3153
3154 case ASM_OPERANDS:
3155 return MEM_VOLATILE_P (x);
3156
3157 /* Memory ref can trap unless it's a static var or a stack slot. */
3158 case MEM:
3159 /* Recognize specific pattern of stack checking probes. */
3160 if (flag_stack_check
3161 && MEM_VOLATILE_P (x)
3162 && XEXP (x, 0) == stack_pointer_rtx)
3163 return true;
3164 if (/* MEM_NOTRAP_P only relates to the actual position of the memory
3165 reference; moving it out of context such as when moving code
3166 when optimizing, might cause its address to become invalid. */
3167 code_changed
3168 || !MEM_NOTRAP_P (x))
3169 {
3170 poly_int64 size = MEM_SIZE_KNOWN_P (x) ? MEM_SIZE (x) : -1;
3171 return rtx_addr_can_trap_p_1 (XEXP (x, 0), 0, size,
3172 GET_MODE (x), code_changed);
3173 }
3174
3175 return false;
3176
3177 /* Division by a non-constant might trap. */
3178 case DIV:
3179 case MOD:
3180 case UDIV:
3181 case UMOD:
3182 if (HONOR_SNANS (x))
3183 return true;
3184 if (FLOAT_MODE_P (GET_MODE (x)))
3185 return flag_trapping_math;
3186 if (!CONSTANT_P (XEXP (x, 1)) || (XEXP (x, 1) == const0_rtx))
3187 return true;
3188 if (GET_CODE (XEXP (x, 1)) == CONST_VECTOR)
3189 {
3190 /* For CONST_VECTOR, return 1 if any element is or might be zero. */
3191 unsigned int n_elts;
3192 rtx op = XEXP (x, 1);
3193 if (!GET_MODE_NUNITS (GET_MODE (op)).is_constant (&n_elts))
3194 {
3195 if (!CONST_VECTOR_DUPLICATE_P (op))
3196 return true;
3197 for (unsigned i = 0; i < (unsigned int) XVECLEN (op, 0); i++)
3198 if (CONST_VECTOR_ENCODED_ELT (op, i) == const0_rtx)
3199 return true;
3200 }
3201 else
3202 for (unsigned i = 0; i < n_elts; i++)
3203 if (CONST_VECTOR_ELT (op, i) == const0_rtx)
3204 return true;
3205 }
3206 break;
3207
3208 case EXPR_LIST:
3209 /* An EXPR_LIST is used to represent a function call. This
3210 certainly may trap. */
3211 return true;
3212
3213 case GE:
3214 case GT:
3215 case LE:
3216 case LT:
3217 case LTGT:
3218 case COMPARE:
3219 /* Treat min/max similar as comparisons. */
3220 case SMIN:
3221 case SMAX:
3222 /* Some floating point comparisons may trap. */
3223 if (!flag_trapping_math)
3224 break;
3225 /* ??? There is no machine independent way to check for tests that trap
3226 when COMPARE is used, though many targets do make this distinction.
3227 For instance, sparc uses CCFPE for compares which generate exceptions
3228 and CCFP for compares which do not generate exceptions. */
3229 if (HONOR_NANS (x))
3230 return true;
3231 /* But often the compare has some CC mode, so check operand
3232 modes as well. */
3233 if (HONOR_NANS (XEXP (x, 0))
3234 || HONOR_NANS (XEXP (x, 1)))
3235 return true;
3236 break;
3237
3238 case EQ:
3239 case NE:
3240 if (HONOR_SNANS (x))
3241 return true;
3242 /* Often comparison is CC mode, so check operand modes. */
3243 if (HONOR_SNANS (XEXP (x, 0))
3244 || HONOR_SNANS (XEXP (x, 1)))
3245 return true;
3246 break;
3247
3248 case FIX:
3249 case UNSIGNED_FIX:
3250 /* Conversion of floating point might trap. */
3251 if (flag_trapping_math && HONOR_NANS (XEXP (x, 0)))
3252 return true;
3253 break;
3254
3255 case PARALLEL:
3256 case NEG:
3257 case ABS:
3258 case SUBREG:
3259 case VEC_MERGE:
3260 case VEC_SELECT:
3261 case VEC_CONCAT:
3262 case VEC_DUPLICATE:
3263 /* These operations don't trap even with floating point. */
3264 break;
3265
3266 default:
3267 /* Any floating arithmetic may trap. */
3268 if (FLOAT_MODE_P (GET_MODE (x)) && flag_trapping_math)
3269 return true;
3270 }
3271
3272 fmt = GET_RTX_FORMAT (code);
3273 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3274 {
3275 if (fmt[i] == 'e')
3276 {
3277 if (may_trap_p_1 (XEXP (x, i), flags))
3278 return true;
3279 }
3280 else if (fmt[i] == 'E')
3281 {
3282 int j;
3283 for (j = 0; j < XVECLEN (x, i); j++)
3284 if (may_trap_p_1 (XVECEXP (x, i, j), flags))
3285 return true;
3286 }
3287 }
3288 return false;
3289 }
3290
3291 /* Return true if evaluating rtx X might cause a trap. */
3292
3293 bool
3294 may_trap_p (const_rtx x)
3295 {
3296 return may_trap_p_1 (x, 0);
3297 }
3298
3299 /* Same as above, but additionally return true if evaluating rtx X might
3300 cause a fault. We define a fault for the purpose of this function as a
3301 erroneous execution condition that cannot be encountered during the normal
3302 execution of a valid program; the typical example is an unaligned memory
3303 access on a strict alignment machine. The compiler guarantees that it
3304 doesn't generate code that will fault from a valid program, but this
3305 guarantee doesn't mean anything for individual instructions. Consider
3306 the following example:
3307
3308 struct S { int d; union { char *cp; int *ip; }; };
3309
3310 int foo(struct S *s)
3311 {
3312 if (s->d == 1)
3313 return *s->ip;
3314 else
3315 return *s->cp;
3316 }
3317
3318 on a strict alignment machine. In a valid program, foo will never be
3319 invoked on a structure for which d is equal to 1 and the underlying
3320 unique field of the union not aligned on a 4-byte boundary, but the
3321 expression *s->ip might cause a fault if considered individually.
3322
3323 At the RTL level, potentially problematic expressions will almost always
3324 verify may_trap_p; for example, the above dereference can be emitted as
3325 (mem:SI (reg:P)) and this expression is may_trap_p for a generic register.
3326 However, suppose that foo is inlined in a caller that causes s->cp to
3327 point to a local character variable and guarantees that s->d is not set
3328 to 1; foo may have been effectively translated into pseudo-RTL as:
3329
3330 if ((reg:SI) == 1)
3331 (set (reg:SI) (mem:SI (%fp - 7)))
3332 else
3333 (set (reg:QI) (mem:QI (%fp - 7)))
3334
3335 Now (mem:SI (%fp - 7)) is considered as not may_trap_p since it is a
3336 memory reference to a stack slot, but it will certainly cause a fault
3337 on a strict alignment machine. */
3338
3339 bool
3340 may_trap_or_fault_p (const_rtx x)
3341 {
3342 return may_trap_p_1 (x, 1);
3343 }
3344
3345 /* Replace any occurrence of FROM in X with TO. The function does
3346 not enter into CONST_DOUBLE for the replace.
3347
3348 Note that copying is not done so X must not be shared unless all copies
3349 are to be modified.
3350
3351 ALL_REGS is true if we want to replace all REGs equal to FROM, not just
3352 those pointer-equal ones. */
3353
3354 rtx
3355 replace_rtx (rtx x, rtx from, rtx to, bool all_regs)
3356 {
3357 int i, j;
3358 const char *fmt;
3359
3360 if (x == from)
3361 return to;
3362
3363 /* Allow this function to make replacements in EXPR_LISTs. */
3364 if (x == 0)
3365 return 0;
3366
3367 if (all_regs
3368 && REG_P (x)
3369 && REG_P (from)
3370 && REGNO (x) == REGNO (from))
3371 {
3372 gcc_assert (GET_MODE (x) == GET_MODE (from));
3373 return to;
3374 }
3375 else if (GET_CODE (x) == SUBREG)
3376 {
3377 rtx new_rtx = replace_rtx (SUBREG_REG (x), from, to, all_regs);
3378
3379 if (CONST_SCALAR_INT_P (new_rtx))
3380 {
3381 x = simplify_subreg (GET_MODE (x), new_rtx,
3382 GET_MODE (SUBREG_REG (x)),
3383 SUBREG_BYTE (x));
3384 gcc_assert (x);
3385 }
3386 else
3387 SUBREG_REG (x) = new_rtx;
3388
3389 return x;
3390 }
3391 else if (GET_CODE (x) == ZERO_EXTEND)
3392 {
3393 rtx new_rtx = replace_rtx (XEXP (x, 0), from, to, all_regs);
3394
3395 if (CONST_SCALAR_INT_P (new_rtx))
3396 {
3397 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
3398 new_rtx, GET_MODE (XEXP (x, 0)));
3399 gcc_assert (x);
3400 }
3401 else
3402 XEXP (x, 0) = new_rtx;
3403
3404 return x;
3405 }
3406
3407 fmt = GET_RTX_FORMAT (GET_CODE (x));
3408 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
3409 {
3410 if (fmt[i] == 'e')
3411 XEXP (x, i) = replace_rtx (XEXP (x, i), from, to, all_regs);
3412 else if (fmt[i] == 'E')
3413 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3414 XVECEXP (x, i, j) = replace_rtx (XVECEXP (x, i, j),
3415 from, to, all_regs);
3416 }
3417
3418 return x;
3419 }
3420
3421 /* Replace occurrences of the OLD_LABEL in *LOC with NEW_LABEL. Also track
3422 the change in LABEL_NUSES if UPDATE_LABEL_NUSES. */
3423
3424 void
3425 replace_label (rtx *loc, rtx old_label, rtx new_label, bool update_label_nuses)
3426 {
3427 /* Handle jump tables specially, since ADDR_{DIFF_,}VECs can be long. */
3428 rtx x = *loc;
3429 if (JUMP_TABLE_DATA_P (x))
3430 {
3431 x = PATTERN (x);
3432 rtvec vec = XVEC (x, GET_CODE (x) == ADDR_DIFF_VEC);
3433 int len = GET_NUM_ELEM (vec);
3434 for (int i = 0; i < len; ++i)
3435 {
3436 rtx ref = RTVEC_ELT (vec, i);
3437 if (XEXP (ref, 0) == old_label)
3438 {
3439 XEXP (ref, 0) = new_label;
3440 if (update_label_nuses)
3441 {
3442 ++LABEL_NUSES (new_label);
3443 --LABEL_NUSES (old_label);
3444 }
3445 }
3446 }
3447 return;
3448 }
3449
3450 /* If this is a JUMP_INSN, then we also need to fix the JUMP_LABEL
3451 field. This is not handled by the iterator because it doesn't
3452 handle unprinted ('0') fields. */
3453 if (JUMP_P (x) && JUMP_LABEL (x) == old_label)
3454 JUMP_LABEL (x) = new_label;
3455
3456 subrtx_ptr_iterator::array_type array;
3457 FOR_EACH_SUBRTX_PTR (iter, array, loc, ALL)
3458 {
3459 rtx *loc = *iter;
3460 if (rtx x = *loc)
3461 {
3462 if (GET_CODE (x) == SYMBOL_REF
3463 && CONSTANT_POOL_ADDRESS_P (x))
3464 {
3465 rtx c = get_pool_constant (x);
3466 if (rtx_referenced_p (old_label, c))
3467 {
3468 /* Create a copy of constant C; replace the label inside
3469 but do not update LABEL_NUSES because uses in constant pool
3470 are not counted. */
3471 rtx new_c = copy_rtx (c);
3472 replace_label (&new_c, old_label, new_label, false);
3473
3474 /* Add the new constant NEW_C to constant pool and replace
3475 the old reference to constant by new reference. */
3476 rtx new_mem = force_const_mem (get_pool_mode (x), new_c);
3477 *loc = replace_rtx (x, x, XEXP (new_mem, 0));
3478 }
3479 }
3480
3481 if ((GET_CODE (x) == LABEL_REF
3482 || GET_CODE (x) == INSN_LIST)
3483 && XEXP (x, 0) == old_label)
3484 {
3485 XEXP (x, 0) = new_label;
3486 if (update_label_nuses)
3487 {
3488 ++LABEL_NUSES (new_label);
3489 --LABEL_NUSES (old_label);
3490 }
3491 }
3492 }
3493 }
3494 }
3495
3496 void
3497 replace_label_in_insn (rtx_insn *insn, rtx_insn *old_label,
3498 rtx_insn *new_label, bool update_label_nuses)
3499 {
3500 rtx insn_as_rtx = insn;
3501 replace_label (&insn_as_rtx, old_label, new_label, update_label_nuses);
3502 gcc_checking_assert (insn_as_rtx == insn);
3503 }
3504
3505 /* Return true if X is referenced in BODY. */
3506
3507 bool
3508 rtx_referenced_p (const_rtx x, const_rtx body)
3509 {
3510 subrtx_iterator::array_type array;
3511 FOR_EACH_SUBRTX (iter, array, body, ALL)
3512 if (const_rtx y = *iter)
3513 {
3514 /* Check if a label_ref Y refers to label X. */
3515 if (GET_CODE (y) == LABEL_REF
3516 && LABEL_P (x)
3517 && label_ref_label (y) == x)
3518 return true;
3519
3520 if (rtx_equal_p (x, y))
3521 return true;
3522
3523 /* If Y is a reference to pool constant traverse the constant. */
3524 if (GET_CODE (y) == SYMBOL_REF
3525 && CONSTANT_POOL_ADDRESS_P (y))
3526 iter.substitute (get_pool_constant (y));
3527 }
3528 return false;
3529 }
3530
3531 /* If INSN is a tablejump return true and store the label (before jump table) to
3532 *LABELP and the jump table to *TABLEP. LABELP and TABLEP may be NULL. */
3533
3534 bool
3535 tablejump_p (const rtx_insn *insn, rtx_insn **labelp,
3536 rtx_jump_table_data **tablep)
3537 {
3538 if (!JUMP_P (insn))
3539 return false;
3540
3541 rtx target = JUMP_LABEL (insn);
3542 if (target == NULL_RTX || ANY_RETURN_P (target))
3543 return false;
3544
3545 rtx_insn *label = as_a<rtx_insn *> (target);
3546 rtx_insn *table = next_insn (label);
3547 if (table == NULL_RTX || !JUMP_TABLE_DATA_P (table))
3548 return false;
3549
3550 if (labelp)
3551 *labelp = label;
3552 if (tablep)
3553 *tablep = as_a <rtx_jump_table_data *> (table);
3554 return true;
3555 }
3556
3557 /* For INSN known to satisfy tablejump_p, determine if it actually is a
3558 CASESI. Return the insn pattern if so, NULL_RTX otherwise. */
3559
3560 rtx
3561 tablejump_casesi_pattern (const rtx_insn *insn)
3562 {
3563 rtx tmp;
3564
3565 if ((tmp = single_set (insn)) != NULL
3566 && SET_DEST (tmp) == pc_rtx
3567 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
3568 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
3569 return tmp;
3570
3571 return NULL_RTX;
3572 }
3573
3574 /* A subroutine of computed_jump_p, return true if X contains a REG or MEM or
3575 constant that is not in the constant pool and not in the condition
3576 of an IF_THEN_ELSE. */
3577
3578 static bool
3579 computed_jump_p_1 (const_rtx x)
3580 {
3581 const enum rtx_code code = GET_CODE (x);
3582 int i, j;
3583 const char *fmt;
3584
3585 switch (code)
3586 {
3587 case LABEL_REF:
3588 case PC:
3589 return false;
3590
3591 case CONST:
3592 CASE_CONST_ANY:
3593 case SYMBOL_REF:
3594 case REG:
3595 return true;
3596
3597 case MEM:
3598 return ! (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
3599 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)));
3600
3601 case IF_THEN_ELSE:
3602 return (computed_jump_p_1 (XEXP (x, 1))
3603 || computed_jump_p_1 (XEXP (x, 2)));
3604
3605 default:
3606 break;
3607 }
3608
3609 fmt = GET_RTX_FORMAT (code);
3610 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3611 {
3612 if (fmt[i] == 'e'
3613 && computed_jump_p_1 (XEXP (x, i)))
3614 return true;
3615
3616 else if (fmt[i] == 'E')
3617 for (j = 0; j < XVECLEN (x, i); j++)
3618 if (computed_jump_p_1 (XVECEXP (x, i, j)))
3619 return true;
3620 }
3621
3622 return false;
3623 }
3624
3625 /* Return true if INSN is an indirect jump (aka computed jump).
3626
3627 Tablejumps and casesi insns are not considered indirect jumps;
3628 we can recognize them by a (use (label_ref)). */
3629
3630 bool
3631 computed_jump_p (const rtx_insn *insn)
3632 {
3633 int i;
3634 if (JUMP_P (insn))
3635 {
3636 rtx pat = PATTERN (insn);
3637
3638 /* If we have a JUMP_LABEL set, we're not a computed jump. */
3639 if (JUMP_LABEL (insn) != NULL)
3640 return false;
3641
3642 if (GET_CODE (pat) == PARALLEL)
3643 {
3644 int len = XVECLEN (pat, 0);
3645 bool has_use_labelref = false;
3646
3647 for (i = len - 1; i >= 0; i--)
3648 if (GET_CODE (XVECEXP (pat, 0, i)) == USE
3649 && (GET_CODE (XEXP (XVECEXP (pat, 0, i), 0))
3650 == LABEL_REF))
3651 {
3652 has_use_labelref = true;
3653 break;
3654 }
3655
3656 if (! has_use_labelref)
3657 for (i = len - 1; i >= 0; i--)
3658 if (GET_CODE (XVECEXP (pat, 0, i)) == SET
3659 && SET_DEST (XVECEXP (pat, 0, i)) == pc_rtx
3660 && computed_jump_p_1 (SET_SRC (XVECEXP (pat, 0, i))))
3661 return true;
3662 }
3663 else if (GET_CODE (pat) == SET
3664 && SET_DEST (pat) == pc_rtx
3665 && computed_jump_p_1 (SET_SRC (pat)))
3666 return true;
3667 }
3668 return false;
3669 }
3670
3671
3672
3673 /* MEM has a PRE/POST-INC/DEC/MODIFY address X. Extract the operands of
3674 the equivalent add insn and pass the result to FN, using DATA as the
3675 final argument. */
3676
3677 static int
3678 for_each_inc_dec_find_inc_dec (rtx mem, for_each_inc_dec_fn fn, void *data)
3679 {
3680 rtx x = XEXP (mem, 0);
3681 switch (GET_CODE (x))
3682 {
3683 case PRE_INC:
3684 case POST_INC:
3685 {
3686 poly_int64 size = GET_MODE_SIZE (GET_MODE (mem));
3687 rtx r1 = XEXP (x, 0);
3688 rtx c = gen_int_mode (size, GET_MODE (r1));
3689 return fn (mem, x, r1, r1, c, data);
3690 }
3691
3692 case PRE_DEC:
3693 case POST_DEC:
3694 {
3695 poly_int64 size = GET_MODE_SIZE (GET_MODE (mem));
3696 rtx r1 = XEXP (x, 0);
3697 rtx c = gen_int_mode (-size, GET_MODE (r1));
3698 return fn (mem, x, r1, r1, c, data);
3699 }
3700
3701 case PRE_MODIFY:
3702 case POST_MODIFY:
3703 {
3704 rtx r1 = XEXP (x, 0);
3705 rtx add = XEXP (x, 1);
3706 return fn (mem, x, r1, add, NULL, data);
3707 }
3708
3709 default:
3710 gcc_unreachable ();
3711 }
3712 }
3713
3714 /* Traverse *LOC looking for MEMs that have autoinc addresses.
3715 For each such autoinc operation found, call FN, passing it
3716 the innermost enclosing MEM, the operation itself, the RTX modified
3717 by the operation, two RTXs (the second may be NULL) that, once
3718 added, represent the value to be held by the modified RTX
3719 afterwards, and DATA. FN is to return 0 to continue the
3720 traversal or any other value to have it returned to the caller of
3721 for_each_inc_dec. */
3722
3723 int
3724 for_each_inc_dec (rtx x,
3725 for_each_inc_dec_fn fn,
3726 void *data)
3727 {
3728 subrtx_var_iterator::array_type array;
3729 FOR_EACH_SUBRTX_VAR (iter, array, x, NONCONST)
3730 {
3731 rtx mem = *iter;
3732 if (mem
3733 && MEM_P (mem)
3734 && GET_RTX_CLASS (GET_CODE (XEXP (mem, 0))) == RTX_AUTOINC)
3735 {
3736 int res = for_each_inc_dec_find_inc_dec (mem, fn, data);
3737 if (res != 0)
3738 return res;
3739 iter.skip_subrtxes ();
3740 }
3741 }
3742 return 0;
3743 }
3744
3745
3746 /* Searches X for any reference to REGNO, returning the rtx of the
3747 reference found if any. Otherwise, returns NULL_RTX. */
3748
3749 rtx
3750 regno_use_in (unsigned int regno, rtx x)
3751 {
3752 const char *fmt;
3753 int i, j;
3754 rtx tem;
3755
3756 if (REG_P (x) && REGNO (x) == regno)
3757 return x;
3758
3759 fmt = GET_RTX_FORMAT (GET_CODE (x));
3760 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
3761 {
3762 if (fmt[i] == 'e')
3763 {
3764 if ((tem = regno_use_in (regno, XEXP (x, i))))
3765 return tem;
3766 }
3767 else if (fmt[i] == 'E')
3768 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3769 if ((tem = regno_use_in (regno , XVECEXP (x, i, j))))
3770 return tem;
3771 }
3772
3773 return NULL_RTX;
3774 }
3775
3776 /* Return a value indicating whether OP, an operand of a commutative
3777 operation, is preferred as the first or second operand. The more
3778 positive the value, the stronger the preference for being the first
3779 operand. */
3780
3781 int
3782 commutative_operand_precedence (rtx op)
3783 {
3784 enum rtx_code code = GET_CODE (op);
3785
3786 /* Constants always become the second operand. Prefer "nice" constants. */
3787 if (code == CONST_INT)
3788 return -10;
3789 if (code == CONST_WIDE_INT)
3790 return -9;
3791 if (code == CONST_POLY_INT)
3792 return -8;
3793 if (code == CONST_DOUBLE)
3794 return -8;
3795 if (code == CONST_FIXED)
3796 return -8;
3797 op = avoid_constant_pool_reference (op);
3798 code = GET_CODE (op);
3799
3800 switch (GET_RTX_CLASS (code))
3801 {
3802 case RTX_CONST_OBJ:
3803 if (code == CONST_INT)
3804 return -7;
3805 if (code == CONST_WIDE_INT)
3806 return -6;
3807 if (code == CONST_POLY_INT)
3808 return -5;
3809 if (code == CONST_DOUBLE)
3810 return -5;
3811 if (code == CONST_FIXED)
3812 return -5;
3813 return -4;
3814
3815 case RTX_EXTRA:
3816 /* SUBREGs of objects should come second. */
3817 if (code == SUBREG && OBJECT_P (SUBREG_REG (op)))
3818 return -3;
3819 return 0;
3820
3821 case RTX_OBJ:
3822 /* Complex expressions should be the first, so decrease priority
3823 of objects. Prefer pointer objects over non pointer objects. */
3824 if ((REG_P (op) && REG_POINTER (op))
3825 || (MEM_P (op) && MEM_POINTER (op)))
3826 return -1;
3827 return -2;
3828
3829 case RTX_COMM_ARITH:
3830 /* Prefer operands that are themselves commutative to be first.
3831 This helps to make things linear. In particular,
3832 (and (and (reg) (reg)) (not (reg))) is canonical. */
3833 return 4;
3834
3835 case RTX_BIN_ARITH:
3836 /* If only one operand is a binary expression, it will be the first
3837 operand. In particular, (plus (minus (reg) (reg)) (neg (reg)))
3838 is canonical, although it will usually be further simplified. */
3839 return 2;
3840
3841 case RTX_UNARY:
3842 /* Then prefer NEG and NOT. */
3843 if (code == NEG || code == NOT)
3844 return 1;
3845 /* FALLTHRU */
3846
3847 default:
3848 return 0;
3849 }
3850 }
3851
3852 /* Return true iff it is necessary to swap operands of commutative operation
3853 in order to canonicalize expression. */
3854
3855 bool
3856 swap_commutative_operands_p (rtx x, rtx y)
3857 {
3858 return (commutative_operand_precedence (x)
3859 < commutative_operand_precedence (y));
3860 }
3861
3862 /* Return true if X is an autoincrement side effect and the register is
3863 not the stack pointer. */
3864 bool
3865 auto_inc_p (const_rtx x)
3866 {
3867 switch (GET_CODE (x))
3868 {
3869 case PRE_INC:
3870 case POST_INC:
3871 case PRE_DEC:
3872 case POST_DEC:
3873 case PRE_MODIFY:
3874 case POST_MODIFY:
3875 /* There are no REG_INC notes for SP. */
3876 if (XEXP (x, 0) != stack_pointer_rtx)
3877 return true;
3878 default:
3879 break;
3880 }
3881 return false;
3882 }
3883
3884 /* Return true if IN contains a piece of rtl that has the address LOC. */
3885 bool
3886 loc_mentioned_in_p (rtx *loc, const_rtx in)
3887 {
3888 enum rtx_code code;
3889 const char *fmt;
3890 int i, j;
3891
3892 if (!in)
3893 return false;
3894
3895 code = GET_CODE (in);
3896 fmt = GET_RTX_FORMAT (code);
3897 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3898 {
3899 if (fmt[i] == 'e')
3900 {
3901 if (loc == &XEXP (in, i) || loc_mentioned_in_p (loc, XEXP (in, i)))
3902 return true;
3903 }
3904 else if (fmt[i] == 'E')
3905 for (j = XVECLEN (in, i) - 1; j >= 0; j--)
3906 if (loc == &XVECEXP (in, i, j)
3907 || loc_mentioned_in_p (loc, XVECEXP (in, i, j)))
3908 return true;
3909 }
3910 return false;
3911 }
3912
3913 /* Reinterpret a subreg as a bit extraction from an integer and return
3914 the position of the least significant bit of the extracted value.
3915 In other words, if the extraction were performed as a shift right
3916 and mask, return the number of bits to shift right.
3917
3918 The outer value of the subreg has OUTER_BYTES bytes and starts at
3919 byte offset SUBREG_BYTE within an inner value of INNER_BYTES bytes. */
3920
3921 poly_uint64
3922 subreg_size_lsb (poly_uint64 outer_bytes,
3923 poly_uint64 inner_bytes,
3924 poly_uint64 subreg_byte)
3925 {
3926 poly_uint64 subreg_end, trailing_bytes, byte_pos;
3927
3928 /* A paradoxical subreg begins at bit position 0. */
3929 gcc_checking_assert (ordered_p (outer_bytes, inner_bytes));
3930 if (maybe_gt (outer_bytes, inner_bytes))
3931 {
3932 gcc_checking_assert (known_eq (subreg_byte, 0U));
3933 return 0;
3934 }
3935
3936 subreg_end = subreg_byte + outer_bytes;
3937 trailing_bytes = inner_bytes - subreg_end;
3938 if (WORDS_BIG_ENDIAN && BYTES_BIG_ENDIAN)
3939 byte_pos = trailing_bytes;
3940 else if (!WORDS_BIG_ENDIAN && !BYTES_BIG_ENDIAN)
3941 byte_pos = subreg_byte;
3942 else
3943 {
3944 /* When bytes and words have opposite endianness, we must be able
3945 to split offsets into words and bytes at compile time. */
3946 poly_uint64 leading_word_part
3947 = force_align_down (subreg_byte, UNITS_PER_WORD);
3948 poly_uint64 trailing_word_part
3949 = force_align_down (trailing_bytes, UNITS_PER_WORD);
3950 /* If the subreg crosses a word boundary ensure that
3951 it also begins and ends on a word boundary. */
3952 gcc_assert (known_le (subreg_end - leading_word_part,
3953 (unsigned int) UNITS_PER_WORD)
3954 || (known_eq (leading_word_part, subreg_byte)
3955 && known_eq (trailing_word_part, trailing_bytes)));
3956 if (WORDS_BIG_ENDIAN)
3957 byte_pos = trailing_word_part + (subreg_byte - leading_word_part);
3958 else
3959 byte_pos = leading_word_part + (trailing_bytes - trailing_word_part);
3960 }
3961
3962 return byte_pos * BITS_PER_UNIT;
3963 }
3964
3965 /* Given a subreg X, return the bit offset where the subreg begins
3966 (counting from the least significant bit of the reg). */
3967
3968 poly_uint64
3969 subreg_lsb (const_rtx x)
3970 {
3971 return subreg_lsb_1 (GET_MODE (x), GET_MODE (SUBREG_REG (x)),
3972 SUBREG_BYTE (x));
3973 }
3974
3975 /* Return the subreg byte offset for a subreg whose outer value has
3976 OUTER_BYTES bytes, whose inner value has INNER_BYTES bytes, and where
3977 there are LSB_SHIFT *bits* between the lsb of the outer value and the
3978 lsb of the inner value. This is the inverse of the calculation
3979 performed by subreg_lsb_1 (which converts byte offsets to bit shifts). */
3980
3981 poly_uint64
3982 subreg_size_offset_from_lsb (poly_uint64 outer_bytes, poly_uint64 inner_bytes,
3983 poly_uint64 lsb_shift)
3984 {
3985 /* A paradoxical subreg begins at bit position 0. */
3986 gcc_checking_assert (ordered_p (outer_bytes, inner_bytes));
3987 if (maybe_gt (outer_bytes, inner_bytes))
3988 {
3989 gcc_checking_assert (known_eq (lsb_shift, 0U));
3990 return 0;
3991 }
3992
3993 poly_uint64 lower_bytes = exact_div (lsb_shift, BITS_PER_UNIT);
3994 poly_uint64 upper_bytes = inner_bytes - (lower_bytes + outer_bytes);
3995 if (WORDS_BIG_ENDIAN && BYTES_BIG_ENDIAN)
3996 return upper_bytes;
3997 else if (!WORDS_BIG_ENDIAN && !BYTES_BIG_ENDIAN)
3998 return lower_bytes;
3999 else
4000 {
4001 /* When bytes and words have opposite endianness, we must be able
4002 to split offsets into words and bytes at compile time. */
4003 poly_uint64 lower_word_part = force_align_down (lower_bytes,
4004 UNITS_PER_WORD);
4005 poly_uint64 upper_word_part = force_align_down (upper_bytes,
4006 UNITS_PER_WORD);
4007 if (WORDS_BIG_ENDIAN)
4008 return upper_word_part + (lower_bytes - lower_word_part);
4009 else
4010 return lower_word_part + (upper_bytes - upper_word_part);
4011 }
4012 }
4013
4014 /* Fill in information about a subreg of a hard register.
4015 xregno - A regno of an inner hard subreg_reg (or what will become one).
4016 xmode - The mode of xregno.
4017 offset - The byte offset.
4018 ymode - The mode of a top level SUBREG (or what may become one).
4019 info - Pointer to structure to fill in.
4020
4021 Rather than considering one particular inner register (and thus one
4022 particular "outer" register) in isolation, this function really uses
4023 XREGNO as a model for a sequence of isomorphic hard registers. Thus the
4024 function does not check whether adding INFO->offset to XREGNO gives
4025 a valid hard register; even if INFO->offset + XREGNO is out of range,
4026 there might be another register of the same type that is in range.
4027 Likewise it doesn't check whether targetm.hard_regno_mode_ok accepts
4028 the new register, since that can depend on things like whether the final
4029 register number is even or odd. Callers that want to check whether
4030 this particular subreg can be replaced by a simple (reg ...) should
4031 use simplify_subreg_regno. */
4032
4033 void
4034 subreg_get_info (unsigned int xregno, machine_mode xmode,
4035 poly_uint64 offset, machine_mode ymode,
4036 struct subreg_info *info)
4037 {
4038 unsigned int nregs_xmode, nregs_ymode;
4039
4040 gcc_assert (xregno < FIRST_PSEUDO_REGISTER);
4041
4042 poly_uint64 xsize = GET_MODE_SIZE (xmode);
4043 poly_uint64 ysize = GET_MODE_SIZE (ymode);
4044
4045 bool rknown = false;
4046
4047 /* If the register representation of a non-scalar mode has holes in it,
4048 we expect the scalar units to be concatenated together, with the holes
4049 distributed evenly among the scalar units. Each scalar unit must occupy
4050 at least one register. */
4051 if (HARD_REGNO_NREGS_HAS_PADDING (xregno, xmode))
4052 {
4053 /* As a consequence, we must be dealing with a constant number of
4054 scalars, and thus a constant offset and number of units. */
4055 HOST_WIDE_INT coffset = offset.to_constant ();
4056 HOST_WIDE_INT cysize = ysize.to_constant ();
4057 nregs_xmode = HARD_REGNO_NREGS_WITH_PADDING (xregno, xmode);
4058 unsigned int nunits = GET_MODE_NUNITS (xmode).to_constant ();
4059 scalar_mode xmode_unit = GET_MODE_INNER (xmode);
4060 gcc_assert (HARD_REGNO_NREGS_HAS_PADDING (xregno, xmode_unit));
4061 gcc_assert (nregs_xmode
4062 == (nunits
4063 * HARD_REGNO_NREGS_WITH_PADDING (xregno, xmode_unit)));
4064 gcc_assert (hard_regno_nregs (xregno, xmode)
4065 == hard_regno_nregs (xregno, xmode_unit) * nunits);
4066
4067 /* You can only ask for a SUBREG of a value with holes in the middle
4068 if you don't cross the holes. (Such a SUBREG should be done by
4069 picking a different register class, or doing it in memory if
4070 necessary.) An example of a value with holes is XCmode on 32-bit
4071 x86 with -m128bit-long-double; it's represented in 6 32-bit registers,
4072 3 for each part, but in memory it's two 128-bit parts.
4073 Padding is assumed to be at the end (not necessarily the 'high part')
4074 of each unit. */
4075 if ((coffset / GET_MODE_SIZE (xmode_unit) + 1 < nunits)
4076 && (coffset / GET_MODE_SIZE (xmode_unit)
4077 != ((coffset + cysize - 1) / GET_MODE_SIZE (xmode_unit))))
4078 {
4079 info->representable_p = false;
4080 rknown = true;
4081 }
4082 }
4083 else
4084 nregs_xmode = hard_regno_nregs (xregno, xmode);
4085
4086 nregs_ymode = hard_regno_nregs (xregno, ymode);
4087
4088 /* Subreg sizes must be ordered, so that we can tell whether they are
4089 partial, paradoxical or complete. */
4090 gcc_checking_assert (ordered_p (xsize, ysize));
4091
4092 /* Paradoxical subregs are otherwise valid. */
4093 if (!rknown && known_eq (offset, 0U) && maybe_gt (ysize, xsize))
4094 {
4095 info->representable_p = true;
4096 /* If this is a big endian paradoxical subreg, which uses more
4097 actual hard registers than the original register, we must
4098 return a negative offset so that we find the proper highpart
4099 of the register.
4100
4101 We assume that the ordering of registers within a multi-register
4102 value has a consistent endianness: if bytes and register words
4103 have different endianness, the hard registers that make up a
4104 multi-register value must be at least word-sized. */
4105 if (REG_WORDS_BIG_ENDIAN)
4106 info->offset = (int) nregs_xmode - (int) nregs_ymode;
4107 else
4108 info->offset = 0;
4109 info->nregs = nregs_ymode;
4110 return;
4111 }
4112
4113 /* If registers store different numbers of bits in the different
4114 modes, we cannot generally form this subreg. */
4115 poly_uint64 regsize_xmode, regsize_ymode;
4116 if (!HARD_REGNO_NREGS_HAS_PADDING (xregno, xmode)
4117 && !HARD_REGNO_NREGS_HAS_PADDING (xregno, ymode)
4118 && multiple_p (xsize, nregs_xmode, &regsize_xmode)
4119 && multiple_p (ysize, nregs_ymode, &regsize_ymode))
4120 {
4121 if (!rknown
4122 && ((nregs_ymode > 1 && maybe_gt (regsize_xmode, regsize_ymode))
4123 || (nregs_xmode > 1 && maybe_gt (regsize_ymode, regsize_xmode))))
4124 {
4125 info->representable_p = false;
4126 if (!can_div_away_from_zero_p (ysize, regsize_xmode, &info->nregs)
4127 || !can_div_trunc_p (offset, regsize_xmode, &info->offset))
4128 /* Checked by validate_subreg. We must know at compile time
4129 which inner registers are being accessed. */
4130 gcc_unreachable ();
4131 return;
4132 }
4133 /* It's not valid to extract a subreg of mode YMODE at OFFSET that
4134 would go outside of XMODE. */
4135 if (!rknown && maybe_gt (ysize + offset, xsize))
4136 {
4137 info->representable_p = false;
4138 info->nregs = nregs_ymode;
4139 if (!can_div_trunc_p (offset, regsize_xmode, &info->offset))
4140 /* Checked by validate_subreg. We must know at compile time
4141 which inner registers are being accessed. */
4142 gcc_unreachable ();
4143 return;
4144 }
4145 /* Quick exit for the simple and common case of extracting whole
4146 subregisters from a multiregister value. */
4147 /* ??? It would be better to integrate this into the code below,
4148 if we can generalize the concept enough and figure out how
4149 odd-sized modes can coexist with the other weird cases we support. */
4150 HOST_WIDE_INT count;
4151 if (!rknown
4152 && WORDS_BIG_ENDIAN == REG_WORDS_BIG_ENDIAN
4153 && known_eq (regsize_xmode, regsize_ymode)
4154 && constant_multiple_p (offset, regsize_ymode, &count))
4155 {
4156 info->representable_p = true;
4157 info->nregs = nregs_ymode;
4158 info->offset = count;
4159 gcc_assert (info->offset + info->nregs <= (int) nregs_xmode);
4160 return;
4161 }
4162 }
4163
4164 /* Lowpart subregs are otherwise valid. */
4165 if (!rknown && known_eq (offset, subreg_lowpart_offset (ymode, xmode)))
4166 {
4167 info->representable_p = true;
4168 rknown = true;
4169
4170 if (known_eq (offset, 0U) || nregs_xmode == nregs_ymode)
4171 {
4172 info->offset = 0;
4173 info->nregs = nregs_ymode;
4174 return;
4175 }
4176 }
4177
4178 /* Set NUM_BLOCKS to the number of independently-representable YMODE
4179 values there are in (reg:XMODE XREGNO). We can view the register
4180 as consisting of this number of independent "blocks", where each
4181 block occupies NREGS_YMODE registers and contains exactly one
4182 representable YMODE value. */
4183 gcc_assert ((nregs_xmode % nregs_ymode) == 0);
4184 unsigned int num_blocks = nregs_xmode / nregs_ymode;
4185
4186 /* Calculate the number of bytes in each block. This must always
4187 be exact, otherwise we don't know how to verify the constraint.
4188 These conditions may be relaxed but subreg_regno_offset would
4189 need to be redesigned. */
4190 poly_uint64 bytes_per_block = exact_div (xsize, num_blocks);
4191
4192 /* Get the number of the first block that contains the subreg and the byte
4193 offset of the subreg from the start of that block. */
4194 unsigned int block_number;
4195 poly_uint64 subblock_offset;
4196 if (!can_div_trunc_p (offset, bytes_per_block, &block_number,
4197 &subblock_offset))
4198 /* Checked by validate_subreg. We must know at compile time which
4199 inner registers are being accessed. */
4200 gcc_unreachable ();
4201
4202 if (!rknown)
4203 {
4204 /* Only the lowpart of each block is representable. */
4205 info->representable_p
4206 = known_eq (subblock_offset,
4207 subreg_size_lowpart_offset (ysize, bytes_per_block));
4208 rknown = true;
4209 }
4210
4211 /* We assume that the ordering of registers within a multi-register
4212 value has a consistent endianness: if bytes and register words
4213 have different endianness, the hard registers that make up a
4214 multi-register value must be at least word-sized. */
4215 if (WORDS_BIG_ENDIAN != REG_WORDS_BIG_ENDIAN)
4216 /* The block number we calculated above followed memory endianness.
4217 Convert it to register endianness by counting back from the end.
4218 (Note that, because of the assumption above, each block must be
4219 at least word-sized.) */
4220 info->offset = (num_blocks - block_number - 1) * nregs_ymode;
4221 else
4222 info->offset = block_number * nregs_ymode;
4223 info->nregs = nregs_ymode;
4224 }
4225
4226 /* This function returns the regno offset of a subreg expression.
4227 xregno - A regno of an inner hard subreg_reg (or what will become one).
4228 xmode - The mode of xregno.
4229 offset - The byte offset.
4230 ymode - The mode of a top level SUBREG (or what may become one).
4231 RETURN - The regno offset which would be used. */
4232 unsigned int
4233 subreg_regno_offset (unsigned int xregno, machine_mode xmode,
4234 poly_uint64 offset, machine_mode ymode)
4235 {
4236 struct subreg_info info;
4237 subreg_get_info (xregno, xmode, offset, ymode, &info);
4238 return info.offset;
4239 }
4240
4241 /* This function returns true when the offset is representable via
4242 subreg_offset in the given regno.
4243 xregno - A regno of an inner hard subreg_reg (or what will become one).
4244 xmode - The mode of xregno.
4245 offset - The byte offset.
4246 ymode - The mode of a top level SUBREG (or what may become one).
4247 RETURN - Whether the offset is representable. */
4248 bool
4249 subreg_offset_representable_p (unsigned int xregno, machine_mode xmode,
4250 poly_uint64 offset, machine_mode ymode)
4251 {
4252 struct subreg_info info;
4253 subreg_get_info (xregno, xmode, offset, ymode, &info);
4254 return info.representable_p;
4255 }
4256
4257 /* Return the number of a YMODE register to which
4258
4259 (subreg:YMODE (reg:XMODE XREGNO) OFFSET)
4260
4261 can be simplified. Return -1 if the subreg can't be simplified.
4262
4263 XREGNO is a hard register number. */
4264
4265 int
4266 simplify_subreg_regno (unsigned int xregno, machine_mode xmode,
4267 poly_uint64 offset, machine_mode ymode)
4268 {
4269 struct subreg_info info;
4270 unsigned int yregno;
4271
4272 /* Give the backend a chance to disallow the mode change. */
4273 if (GET_MODE_CLASS (xmode) != MODE_COMPLEX_INT
4274 && GET_MODE_CLASS (xmode) != MODE_COMPLEX_FLOAT
4275 && !REG_CAN_CHANGE_MODE_P (xregno, xmode, ymode))
4276 return -1;
4277
4278 /* We shouldn't simplify stack-related registers. */
4279 if ((!reload_completed || frame_pointer_needed)
4280 && xregno == FRAME_POINTER_REGNUM)
4281 return -1;
4282
4283 if (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
4284 && xregno == ARG_POINTER_REGNUM)
4285 return -1;
4286
4287 if (xregno == STACK_POINTER_REGNUM
4288 /* We should convert hard stack register in LRA if it is
4289 possible. */
4290 && ! lra_in_progress)
4291 return -1;
4292
4293 /* Try to get the register offset. */
4294 subreg_get_info (xregno, xmode, offset, ymode, &info);
4295 if (!info.representable_p)
4296 return -1;
4297
4298 /* Make sure that the offsetted register value is in range. */
4299 yregno = xregno + info.offset;
4300 if (!HARD_REGISTER_NUM_P (yregno))
4301 return -1;
4302
4303 /* See whether (reg:YMODE YREGNO) is valid.
4304
4305 ??? We allow invalid registers if (reg:XMODE XREGNO) is also invalid.
4306 This is a kludge to work around how complex FP arguments are passed
4307 on IA-64 and should be fixed. See PR target/49226. */
4308 if (!targetm.hard_regno_mode_ok (yregno, ymode)
4309 && targetm.hard_regno_mode_ok (xregno, xmode))
4310 return -1;
4311
4312 return (int) yregno;
4313 }
4314
4315 /* A wrapper around simplify_subreg_regno that uses subreg_lowpart_offset
4316 (xmode, ymode) as the offset. */
4317
4318 int
4319 lowpart_subreg_regno (unsigned int regno, machine_mode xmode,
4320 machine_mode ymode)
4321 {
4322 poly_uint64 offset = subreg_lowpart_offset (xmode, ymode);
4323 return simplify_subreg_regno (regno, xmode, offset, ymode);
4324 }
4325
4326 /* Return the final regno that a subreg expression refers to. */
4327 unsigned int
4328 subreg_regno (const_rtx x)
4329 {
4330 unsigned int ret;
4331 rtx subreg = SUBREG_REG (x);
4332 int regno = REGNO (subreg);
4333
4334 ret = regno + subreg_regno_offset (regno,
4335 GET_MODE (subreg),
4336 SUBREG_BYTE (x),
4337 GET_MODE (x));
4338 return ret;
4339
4340 }
4341
4342 /* Return the number of registers that a subreg expression refers
4343 to. */
4344 unsigned int
4345 subreg_nregs (const_rtx x)
4346 {
4347 return subreg_nregs_with_regno (REGNO (SUBREG_REG (x)), x);
4348 }
4349
4350 /* Return the number of registers that a subreg REG with REGNO
4351 expression refers to. This is a copy of the rtlanal.cc:subreg_nregs
4352 changed so that the regno can be passed in. */
4353
4354 unsigned int
4355 subreg_nregs_with_regno (unsigned int regno, const_rtx x)
4356 {
4357 struct subreg_info info;
4358 rtx subreg = SUBREG_REG (x);
4359
4360 subreg_get_info (regno, GET_MODE (subreg), SUBREG_BYTE (x), GET_MODE (x),
4361 &info);
4362 return info.nregs;
4363 }
4364
4365 struct parms_set_data
4366 {
4367 int nregs;
4368 HARD_REG_SET regs;
4369 };
4370
4371 /* Helper function for noticing stores to parameter registers. */
4372 static void
4373 parms_set (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
4374 {
4375 struct parms_set_data *const d = (struct parms_set_data *) data;
4376 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4377 && TEST_HARD_REG_BIT (d->regs, REGNO (x)))
4378 {
4379 CLEAR_HARD_REG_BIT (d->regs, REGNO (x));
4380 d->nregs--;
4381 }
4382 }
4383
4384 /* Look backward for first parameter to be loaded.
4385 Note that loads of all parameters will not necessarily be
4386 found if CSE has eliminated some of them (e.g., an argument
4387 to the outer function is passed down as a parameter).
4388 Do not skip BOUNDARY. */
4389 rtx_insn *
4390 find_first_parameter_load (rtx_insn *call_insn, rtx_insn *boundary)
4391 {
4392 struct parms_set_data parm;
4393 rtx p;
4394 rtx_insn *before, *first_set;
4395
4396 /* Since different machines initialize their parameter registers
4397 in different orders, assume nothing. Collect the set of all
4398 parameter registers. */
4399 CLEAR_HARD_REG_SET (parm.regs);
4400 parm.nregs = 0;
4401 for (p = CALL_INSN_FUNCTION_USAGE (call_insn); p; p = XEXP (p, 1))
4402 if (GET_CODE (XEXP (p, 0)) == USE
4403 && REG_P (XEXP (XEXP (p, 0), 0))
4404 && !STATIC_CHAIN_REG_P (XEXP (XEXP (p, 0), 0)))
4405 {
4406 gcc_assert (REGNO (XEXP (XEXP (p, 0), 0)) < FIRST_PSEUDO_REGISTER);
4407
4408 /* We only care about registers which can hold function
4409 arguments. */
4410 if (!FUNCTION_ARG_REGNO_P (REGNO (XEXP (XEXP (p, 0), 0))))
4411 continue;
4412
4413 SET_HARD_REG_BIT (parm.regs, REGNO (XEXP (XEXP (p, 0), 0)));
4414 parm.nregs++;
4415 }
4416 before = call_insn;
4417 first_set = call_insn;
4418
4419 /* Search backward for the first set of a register in this set. */
4420 while (parm.nregs && before != boundary)
4421 {
4422 before = PREV_INSN (before);
4423
4424 /* It is possible that some loads got CSEed from one call to
4425 another. Stop in that case. */
4426 if (CALL_P (before))
4427 break;
4428
4429 /* Our caller needs either ensure that we will find all sets
4430 (in case code has not been optimized yet), or take care
4431 for possible labels in a way by setting boundary to preceding
4432 CODE_LABEL. */
4433 if (LABEL_P (before))
4434 {
4435 gcc_assert (before == boundary);
4436 break;
4437 }
4438
4439 if (INSN_P (before))
4440 {
4441 int nregs_old = parm.nregs;
4442 note_stores (before, parms_set, &parm);
4443 /* If we found something that did not set a parameter reg,
4444 we're done. Do not keep going, as that might result
4445 in hoisting an insn before the setting of a pseudo
4446 that is used by the hoisted insn. */
4447 if (nregs_old != parm.nregs)
4448 first_set = before;
4449 else
4450 break;
4451 }
4452 }
4453 return first_set;
4454 }
4455
4456 /* Return true if we should avoid inserting code between INSN and preceding
4457 call instruction. */
4458
4459 bool
4460 keep_with_call_p (const rtx_insn *insn)
4461 {
4462 rtx set;
4463
4464 if (INSN_P (insn) && (set = single_set (insn)) != NULL)
4465 {
4466 if (REG_P (SET_DEST (set))
4467 && REGNO (SET_DEST (set)) < FIRST_PSEUDO_REGISTER
4468 && fixed_regs[REGNO (SET_DEST (set))]
4469 && general_operand (SET_SRC (set), VOIDmode))
4470 return true;
4471 if (REG_P (SET_SRC (set))
4472 && targetm.calls.function_value_regno_p (REGNO (SET_SRC (set)))
4473 && REG_P (SET_DEST (set))
4474 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
4475 return true;
4476 /* There may be a stack pop just after the call and before the store
4477 of the return register. Search for the actual store when deciding
4478 if we can break or not. */
4479 if (SET_DEST (set) == stack_pointer_rtx)
4480 {
4481 /* This CONST_CAST is okay because next_nonnote_insn just
4482 returns its argument and we assign it to a const_rtx
4483 variable. */
4484 const rtx_insn *i2
4485 = next_nonnote_insn (const_cast<rtx_insn *> (insn));
4486 if (i2 && keep_with_call_p (i2))
4487 return true;
4488 }
4489 }
4490 return false;
4491 }
4492
4493 /* Return true if LABEL is a target of JUMP_INSN. This applies only
4494 to non-complex jumps. That is, direct unconditional, conditional,
4495 and tablejumps, but not computed jumps or returns. It also does
4496 not apply to the fallthru case of a conditional jump. */
4497
4498 bool
4499 label_is_jump_target_p (const_rtx label, const rtx_insn *jump_insn)
4500 {
4501 rtx tmp = JUMP_LABEL (jump_insn);
4502 rtx_jump_table_data *table;
4503
4504 if (label == tmp)
4505 return true;
4506
4507 if (tablejump_p (jump_insn, NULL, &table))
4508 {
4509 rtvec vec = table->get_labels ();
4510 int i, veclen = GET_NUM_ELEM (vec);
4511
4512 for (i = 0; i < veclen; ++i)
4513 if (XEXP (RTVEC_ELT (vec, i), 0) == label)
4514 return true;
4515 }
4516
4517 if (find_reg_note (jump_insn, REG_LABEL_TARGET, label))
4518 return true;
4519
4520 return false;
4521 }
4522
4523
4524 /* Return an estimate of the cost of computing rtx X.
4525 One use is in cse, to decide which expression to keep in the hash table.
4526 Another is in rtl generation, to pick the cheapest way to multiply.
4527 Other uses like the latter are expected in the future.
4528
4529 X appears as operand OPNO in an expression with code OUTER_CODE.
4530 SPEED specifies whether costs optimized for speed or size should
4531 be returned. */
4532
4533 int
4534 rtx_cost (rtx x, machine_mode mode, enum rtx_code outer_code,
4535 int opno, bool speed)
4536 {
4537 int i, j;
4538 enum rtx_code code;
4539 const char *fmt;
4540 int total;
4541 int factor;
4542 unsigned mode_size;
4543
4544 if (x == 0)
4545 return 0;
4546
4547 if (GET_CODE (x) == SET)
4548 /* A SET doesn't have a mode, so let's look at the SET_DEST to get
4549 the mode for the factor. */
4550 mode = GET_MODE (SET_DEST (x));
4551 else if (GET_MODE (x) != VOIDmode)
4552 mode = GET_MODE (x);
4553
4554 mode_size = estimated_poly_value (GET_MODE_SIZE (mode));
4555
4556 /* A size N times larger than UNITS_PER_WORD likely needs N times as
4557 many insns, taking N times as long. */
4558 factor = mode_size > UNITS_PER_WORD ? mode_size / UNITS_PER_WORD : 1;
4559
4560 /* Compute the default costs of certain things.
4561 Note that targetm.rtx_costs can override the defaults. */
4562
4563 code = GET_CODE (x);
4564 switch (code)
4565 {
4566 case MULT:
4567 case FMA:
4568 case SS_MULT:
4569 case US_MULT:
4570 case SMUL_HIGHPART:
4571 case UMUL_HIGHPART:
4572 /* Multiplication has time-complexity O(N*N), where N is the
4573 number of units (translated from digits) when using
4574 schoolbook long multiplication. */
4575 total = factor * factor * COSTS_N_INSNS (5);
4576 break;
4577 case DIV:
4578 case UDIV:
4579 case MOD:
4580 case UMOD:
4581 case SS_DIV:
4582 case US_DIV:
4583 /* Similarly, complexity for schoolbook long division. */
4584 total = factor * factor * COSTS_N_INSNS (7);
4585 break;
4586 case USE:
4587 /* Used in combine.cc as a marker. */
4588 total = 0;
4589 break;
4590 default:
4591 total = factor * COSTS_N_INSNS (1);
4592 }
4593
4594 switch (code)
4595 {
4596 case REG:
4597 return 0;
4598
4599 case SUBREG:
4600 total = 0;
4601 /* If we can't tie these modes, make this expensive. The larger
4602 the mode, the more expensive it is. */
4603 if (!targetm.modes_tieable_p (mode, GET_MODE (SUBREG_REG (x))))
4604 return COSTS_N_INSNS (2 + factor);
4605 break;
4606
4607 case TRUNCATE:
4608 if (targetm.modes_tieable_p (mode, GET_MODE (XEXP (x, 0))))
4609 {
4610 total = 0;
4611 break;
4612 }
4613 /* FALLTHRU */
4614 default:
4615 if (targetm.rtx_costs (x, mode, outer_code, opno, &total, speed))
4616 return total;
4617 break;
4618 }
4619
4620 /* Sum the costs of the sub-rtx's, plus cost of this operation,
4621 which is already in total. */
4622
4623 fmt = GET_RTX_FORMAT (code);
4624 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4625 if (fmt[i] == 'e')
4626 total += rtx_cost (XEXP (x, i), mode, code, i, speed);
4627 else if (fmt[i] == 'E')
4628 for (j = 0; j < XVECLEN (x, i); j++)
4629 total += rtx_cost (XVECEXP (x, i, j), mode, code, i, speed);
4630
4631 return total;
4632 }
4633
4634 /* Fill in the structure C with information about both speed and size rtx
4635 costs for X, which is operand OPNO in an expression with code OUTER. */
4636
4637 void
4638 get_full_rtx_cost (rtx x, machine_mode mode, enum rtx_code outer, int opno,
4639 struct full_rtx_costs *c)
4640 {
4641 c->speed = rtx_cost (x, mode, outer, opno, true);
4642 c->size = rtx_cost (x, mode, outer, opno, false);
4643 }
4644
4645
4646 /* Return cost of address expression X.
4647 Expect that X is properly formed address reference.
4648
4649 SPEED parameter specify whether costs optimized for speed or size should
4650 be returned. */
4651
4652 int
4653 address_cost (rtx x, machine_mode mode, addr_space_t as, bool speed)
4654 {
4655 /* We may be asked for cost of various unusual addresses, such as operands
4656 of push instruction. It is not worthwhile to complicate writing
4657 of the target hook by such cases. */
4658
4659 if (!memory_address_addr_space_p (mode, x, as))
4660 return 1000;
4661
4662 return targetm.address_cost (x, mode, as, speed);
4663 }
4664
4665 /* If the target doesn't override, compute the cost as with arithmetic. */
4666
4667 int
4668 default_address_cost (rtx x, machine_mode, addr_space_t, bool speed)
4669 {
4670 return rtx_cost (x, Pmode, MEM, 0, speed);
4671 }
4672
4673
4674 unsigned HOST_WIDE_INT
4675 nonzero_bits (const_rtx x, machine_mode mode)
4676 {
4677 if (mode == VOIDmode)
4678 mode = GET_MODE (x);
4679 scalar_int_mode int_mode;
4680 if (!is_a <scalar_int_mode> (mode, &int_mode))
4681 return GET_MODE_MASK (mode);
4682 return cached_nonzero_bits (x, int_mode, NULL_RTX, VOIDmode, 0);
4683 }
4684
4685 unsigned int
4686 num_sign_bit_copies (const_rtx x, machine_mode mode)
4687 {
4688 if (mode == VOIDmode)
4689 mode = GET_MODE (x);
4690 scalar_int_mode int_mode;
4691 if (!is_a <scalar_int_mode> (mode, &int_mode))
4692 return 1;
4693 return cached_num_sign_bit_copies (x, int_mode, NULL_RTX, VOIDmode, 0);
4694 }
4695
4696 /* Return true if nonzero_bits1 might recurse into both operands
4697 of X. */
4698
4699 static inline bool
4700 nonzero_bits_binary_arith_p (const_rtx x)
4701 {
4702 if (!ARITHMETIC_P (x))
4703 return false;
4704 switch (GET_CODE (x))
4705 {
4706 case AND:
4707 case XOR:
4708 case IOR:
4709 case UMIN:
4710 case UMAX:
4711 case SMIN:
4712 case SMAX:
4713 case PLUS:
4714 case MINUS:
4715 case MULT:
4716 case DIV:
4717 case UDIV:
4718 case MOD:
4719 case UMOD:
4720 return true;
4721 default:
4722 return false;
4723 }
4724 }
4725
4726 /* The function cached_nonzero_bits is a wrapper around nonzero_bits1.
4727 It avoids exponential behavior in nonzero_bits1 when X has
4728 identical subexpressions on the first or the second level. */
4729
4730 static unsigned HOST_WIDE_INT
4731 cached_nonzero_bits (const_rtx x, scalar_int_mode mode, const_rtx known_x,
4732 machine_mode known_mode,
4733 unsigned HOST_WIDE_INT known_ret)
4734 {
4735 if (x == known_x && mode == known_mode)
4736 return known_ret;
4737
4738 /* Try to find identical subexpressions. If found call
4739 nonzero_bits1 on X with the subexpressions as KNOWN_X and the
4740 precomputed value for the subexpression as KNOWN_RET. */
4741
4742 if (nonzero_bits_binary_arith_p (x))
4743 {
4744 rtx x0 = XEXP (x, 0);
4745 rtx x1 = XEXP (x, 1);
4746
4747 /* Check the first level. */
4748 if (x0 == x1)
4749 return nonzero_bits1 (x, mode, x0, mode,
4750 cached_nonzero_bits (x0, mode, known_x,
4751 known_mode, known_ret));
4752
4753 /* Check the second level. */
4754 if (nonzero_bits_binary_arith_p (x0)
4755 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
4756 return nonzero_bits1 (x, mode, x1, mode,
4757 cached_nonzero_bits (x1, mode, known_x,
4758 known_mode, known_ret));
4759
4760 if (nonzero_bits_binary_arith_p (x1)
4761 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
4762 return nonzero_bits1 (x, mode, x0, mode,
4763 cached_nonzero_bits (x0, mode, known_x,
4764 known_mode, known_ret));
4765 }
4766
4767 return nonzero_bits1 (x, mode, known_x, known_mode, known_ret);
4768 }
4769
4770 /* We let num_sign_bit_copies recur into nonzero_bits as that is useful.
4771 We don't let nonzero_bits recur into num_sign_bit_copies, because that
4772 is less useful. We can't allow both, because that results in exponential
4773 run time recursion. There is a nullstone testcase that triggered
4774 this. This macro avoids accidental uses of num_sign_bit_copies. */
4775 #define cached_num_sign_bit_copies sorry_i_am_preventing_exponential_behavior
4776
4777 /* Given an expression, X, compute which bits in X can be nonzero.
4778 We don't care about bits outside of those defined in MODE.
4779
4780 For most X this is simply GET_MODE_MASK (GET_MODE (X)), but if X is
4781 an arithmetic operation, we can do better. */
4782
4783 static unsigned HOST_WIDE_INT
4784 nonzero_bits1 (const_rtx x, scalar_int_mode mode, const_rtx known_x,
4785 machine_mode known_mode,
4786 unsigned HOST_WIDE_INT known_ret)
4787 {
4788 unsigned HOST_WIDE_INT nonzero = GET_MODE_MASK (mode);
4789 unsigned HOST_WIDE_INT inner_nz;
4790 enum rtx_code code = GET_CODE (x);
4791 machine_mode inner_mode;
4792 unsigned int inner_width;
4793 scalar_int_mode xmode;
4794
4795 unsigned int mode_width = GET_MODE_PRECISION (mode);
4796
4797 if (CONST_INT_P (x))
4798 {
4799 if (SHORT_IMMEDIATES_SIGN_EXTEND
4800 && INTVAL (x) > 0
4801 && mode_width < BITS_PER_WORD
4802 && (UINTVAL (x) & (HOST_WIDE_INT_1U << (mode_width - 1))) != 0)
4803 return UINTVAL (x) | (HOST_WIDE_INT_M1U << mode_width);
4804
4805 return UINTVAL (x);
4806 }
4807
4808 if (!is_a <scalar_int_mode> (GET_MODE (x), &xmode))
4809 return nonzero;
4810 unsigned int xmode_width = GET_MODE_PRECISION (xmode);
4811
4812 /* If X is wider than MODE, use its mode instead. */
4813 if (xmode_width > mode_width)
4814 {
4815 mode = xmode;
4816 nonzero = GET_MODE_MASK (mode);
4817 mode_width = xmode_width;
4818 }
4819
4820 if (mode_width > HOST_BITS_PER_WIDE_INT)
4821 /* Our only callers in this case look for single bit values. So
4822 just return the mode mask. Those tests will then be false. */
4823 return nonzero;
4824
4825 /* If MODE is wider than X, but both are a single word for both the host
4826 and target machines, we can compute this from which bits of the object
4827 might be nonzero in its own mode, taking into account the fact that, on
4828 CISC machines, accessing an object in a wider mode generally causes the
4829 high-order bits to become undefined, so they are not known to be zero.
4830 We extend this reasoning to RISC machines for operations that might not
4831 operate on the full registers. */
4832 if (mode_width > xmode_width
4833 && xmode_width <= BITS_PER_WORD
4834 && xmode_width <= HOST_BITS_PER_WIDE_INT
4835 && !(WORD_REGISTER_OPERATIONS && word_register_operation_p (x)))
4836 {
4837 nonzero &= cached_nonzero_bits (x, xmode,
4838 known_x, known_mode, known_ret);
4839 nonzero |= GET_MODE_MASK (mode) & ~GET_MODE_MASK (xmode);
4840 return nonzero;
4841 }
4842
4843 /* Please keep nonzero_bits_binary_arith_p above in sync with
4844 the code in the switch below. */
4845 switch (code)
4846 {
4847 case REG:
4848 #if defined(POINTERS_EXTEND_UNSIGNED)
4849 /* If pointers extend unsigned and this is a pointer in Pmode, say that
4850 all the bits above ptr_mode are known to be zero. */
4851 /* As we do not know which address space the pointer is referring to,
4852 we can do this only if the target does not support different pointer
4853 or address modes depending on the address space. */
4854 if (target_default_pointer_address_modes_p ()
4855 && POINTERS_EXTEND_UNSIGNED
4856 && xmode == Pmode
4857 && REG_POINTER (x)
4858 && !targetm.have_ptr_extend ())
4859 nonzero &= GET_MODE_MASK (ptr_mode);
4860 #endif
4861
4862 /* Include declared information about alignment of pointers. */
4863 /* ??? We don't properly preserve REG_POINTER changes across
4864 pointer-to-integer casts, so we can't trust it except for
4865 things that we know must be pointers. See execute/960116-1.c. */
4866 if ((x == stack_pointer_rtx
4867 || x == frame_pointer_rtx
4868 || x == arg_pointer_rtx)
4869 && REGNO_POINTER_ALIGN (REGNO (x)))
4870 {
4871 unsigned HOST_WIDE_INT alignment
4872 = REGNO_POINTER_ALIGN (REGNO (x)) / BITS_PER_UNIT;
4873
4874 #ifdef PUSH_ROUNDING
4875 /* If PUSH_ROUNDING is defined, it is possible for the
4876 stack to be momentarily aligned only to that amount,
4877 so we pick the least alignment. */
4878 if (x == stack_pointer_rtx && targetm.calls.push_argument (0))
4879 {
4880 poly_uint64 rounded_1 = PUSH_ROUNDING (poly_int64 (1));
4881 alignment = MIN (known_alignment (rounded_1), alignment);
4882 }
4883 #endif
4884
4885 nonzero &= ~(alignment - 1);
4886 }
4887
4888 {
4889 unsigned HOST_WIDE_INT nonzero_for_hook = nonzero;
4890 rtx new_rtx = rtl_hooks.reg_nonzero_bits (x, xmode, mode,
4891 &nonzero_for_hook);
4892
4893 if (new_rtx)
4894 nonzero_for_hook &= cached_nonzero_bits (new_rtx, mode, known_x,
4895 known_mode, known_ret);
4896
4897 return nonzero_for_hook;
4898 }
4899
4900 case MEM:
4901 /* In many, if not most, RISC machines, reading a byte from memory
4902 zeros the rest of the register. Noticing that fact saves a lot
4903 of extra zero-extends. */
4904 if (load_extend_op (xmode) == ZERO_EXTEND)
4905 nonzero &= GET_MODE_MASK (xmode);
4906 break;
4907
4908 case EQ: case NE:
4909 case UNEQ: case LTGT:
4910 case GT: case GTU: case UNGT:
4911 case LT: case LTU: case UNLT:
4912 case GE: case GEU: case UNGE:
4913 case LE: case LEU: case UNLE:
4914 case UNORDERED: case ORDERED:
4915 /* If this produces an integer result, we know which bits are set.
4916 Code here used to clear bits outside the mode of X, but that is
4917 now done above. */
4918 /* Mind that MODE is the mode the caller wants to look at this
4919 operation in, and not the actual operation mode. We can wind
4920 up with (subreg:DI (gt:V4HI x y)), and we don't have anything
4921 that describes the results of a vector compare. */
4922 if (GET_MODE_CLASS (xmode) == MODE_INT
4923 && mode_width <= HOST_BITS_PER_WIDE_INT)
4924 nonzero = STORE_FLAG_VALUE;
4925 break;
4926
4927 case NEG:
4928 #if 0
4929 /* Disabled to avoid exponential mutual recursion between nonzero_bits
4930 and num_sign_bit_copies. */
4931 if (num_sign_bit_copies (XEXP (x, 0), xmode) == xmode_width)
4932 nonzero = 1;
4933 #endif
4934
4935 if (xmode_width < mode_width)
4936 nonzero |= (GET_MODE_MASK (mode) & ~GET_MODE_MASK (xmode));
4937 break;
4938
4939 case ABS:
4940 #if 0
4941 /* Disabled to avoid exponential mutual recursion between nonzero_bits
4942 and num_sign_bit_copies. */
4943 if (num_sign_bit_copies (XEXP (x, 0), xmode) == xmode_width)
4944 nonzero = 1;
4945 #endif
4946 break;
4947
4948 case TRUNCATE:
4949 nonzero &= (cached_nonzero_bits (XEXP (x, 0), mode,
4950 known_x, known_mode, known_ret)
4951 & GET_MODE_MASK (mode));
4952 break;
4953
4954 case ZERO_EXTEND:
4955 nonzero &= cached_nonzero_bits (XEXP (x, 0), mode,
4956 known_x, known_mode, known_ret);
4957 if (GET_MODE (XEXP (x, 0)) != VOIDmode)
4958 nonzero &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
4959 break;
4960
4961 case SIGN_EXTEND:
4962 /* If the sign bit is known clear, this is the same as ZERO_EXTEND.
4963 Otherwise, show all the bits in the outer mode but not the inner
4964 may be nonzero. */
4965 inner_nz = cached_nonzero_bits (XEXP (x, 0), mode,
4966 known_x, known_mode, known_ret);
4967 if (GET_MODE (XEXP (x, 0)) != VOIDmode)
4968 {
4969 inner_nz &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
4970 if (val_signbit_known_set_p (GET_MODE (XEXP (x, 0)), inner_nz))
4971 inner_nz |= (GET_MODE_MASK (mode)
4972 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0))));
4973 }
4974
4975 nonzero &= inner_nz;
4976 break;
4977
4978 case AND:
4979 nonzero &= cached_nonzero_bits (XEXP (x, 0), mode,
4980 known_x, known_mode, known_ret)
4981 & cached_nonzero_bits (XEXP (x, 1), mode,
4982 known_x, known_mode, known_ret);
4983 break;
4984
4985 case XOR: case IOR:
4986 case UMIN: case UMAX: case SMIN: case SMAX:
4987 {
4988 unsigned HOST_WIDE_INT nonzero0
4989 = cached_nonzero_bits (XEXP (x, 0), mode,
4990 known_x, known_mode, known_ret);
4991
4992 /* Don't call nonzero_bits for the second time if it cannot change
4993 anything. */
4994 if ((nonzero & nonzero0) != nonzero)
4995 nonzero &= nonzero0
4996 | cached_nonzero_bits (XEXP (x, 1), mode,
4997 known_x, known_mode, known_ret);
4998 }
4999 break;
5000
5001 case PLUS: case MINUS:
5002 case MULT:
5003 case DIV: case UDIV:
5004 case MOD: case UMOD:
5005 /* We can apply the rules of arithmetic to compute the number of
5006 high- and low-order zero bits of these operations. We start by
5007 computing the width (position of the highest-order nonzero bit)
5008 and the number of low-order zero bits for each value. */
5009 {
5010 unsigned HOST_WIDE_INT nz0
5011 = cached_nonzero_bits (XEXP (x, 0), mode,
5012 known_x, known_mode, known_ret);
5013 unsigned HOST_WIDE_INT nz1
5014 = cached_nonzero_bits (XEXP (x, 1), mode,
5015 known_x, known_mode, known_ret);
5016 int sign_index = xmode_width - 1;
5017 int width0 = floor_log2 (nz0) + 1;
5018 int width1 = floor_log2 (nz1) + 1;
5019 int low0 = ctz_or_zero (nz0);
5020 int low1 = ctz_or_zero (nz1);
5021 unsigned HOST_WIDE_INT op0_maybe_minusp
5022 = nz0 & (HOST_WIDE_INT_1U << sign_index);
5023 unsigned HOST_WIDE_INT op1_maybe_minusp
5024 = nz1 & (HOST_WIDE_INT_1U << sign_index);
5025 unsigned int result_width = mode_width;
5026 int result_low = 0;
5027
5028 switch (code)
5029 {
5030 case PLUS:
5031 result_width = MAX (width0, width1) + 1;
5032 result_low = MIN (low0, low1);
5033 break;
5034 case MINUS:
5035 result_low = MIN (low0, low1);
5036 break;
5037 case MULT:
5038 result_width = width0 + width1;
5039 result_low = low0 + low1;
5040 break;
5041 case DIV:
5042 if (width1 == 0)
5043 break;
5044 if (!op0_maybe_minusp && !op1_maybe_minusp)
5045 result_width = width0;
5046 break;
5047 case UDIV:
5048 if (width1 == 0)
5049 break;
5050 result_width = width0;
5051 break;
5052 case MOD:
5053 if (width1 == 0)
5054 break;
5055 if (!op0_maybe_minusp && !op1_maybe_minusp)
5056 result_width = MIN (width0, width1);
5057 result_low = MIN (low0, low1);
5058 break;
5059 case UMOD:
5060 if (width1 == 0)
5061 break;
5062 result_width = MIN (width0, width1);
5063 result_low = MIN (low0, low1);
5064 break;
5065 default:
5066 gcc_unreachable ();
5067 }
5068
5069 /* Note that mode_width <= HOST_BITS_PER_WIDE_INT, see above. */
5070 if (result_width < mode_width)
5071 nonzero &= (HOST_WIDE_INT_1U << result_width) - 1;
5072
5073 if (result_low > 0)
5074 {
5075 if (result_low < HOST_BITS_PER_WIDE_INT)
5076 nonzero &= ~((HOST_WIDE_INT_1U << result_low) - 1);
5077 else
5078 nonzero = 0;
5079 }
5080 }
5081 break;
5082
5083 case ZERO_EXTRACT:
5084 if (CONST_INT_P (XEXP (x, 1))
5085 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
5086 nonzero &= (HOST_WIDE_INT_1U << INTVAL (XEXP (x, 1))) - 1;
5087 break;
5088
5089 case SUBREG:
5090 /* If this is a SUBREG formed for a promoted variable that has
5091 been zero-extended, we know that at least the high-order bits
5092 are zero, though others might be too. */
5093 if (SUBREG_PROMOTED_VAR_P (x) && SUBREG_PROMOTED_UNSIGNED_P (x))
5094 nonzero = GET_MODE_MASK (xmode)
5095 & cached_nonzero_bits (SUBREG_REG (x), xmode,
5096 known_x, known_mode, known_ret);
5097
5098 /* If the inner mode is a single word for both the host and target
5099 machines, we can compute this from which bits of the inner
5100 object might be nonzero. */
5101 inner_mode = GET_MODE (SUBREG_REG (x));
5102 if (GET_MODE_PRECISION (inner_mode).is_constant (&inner_width)
5103 && inner_width <= BITS_PER_WORD
5104 && inner_width <= HOST_BITS_PER_WIDE_INT)
5105 {
5106 nonzero &= cached_nonzero_bits (SUBREG_REG (x), mode,
5107 known_x, known_mode, known_ret);
5108
5109 /* On a typical CISC machine, accessing an object in a wider mode
5110 causes the high-order bits to become undefined. So they are
5111 not known to be zero.
5112
5113 On a typical RISC machine, we only have to worry about the way
5114 loads are extended. Otherwise, if we get a reload for the inner
5115 part, it may be loaded from the stack, and then we may lose all
5116 the zero bits that existed before the store to the stack. */
5117 rtx_code extend_op;
5118 if ((!WORD_REGISTER_OPERATIONS
5119 || ((extend_op = load_extend_op (inner_mode)) == SIGN_EXTEND
5120 ? val_signbit_known_set_p (inner_mode, nonzero)
5121 : extend_op != ZERO_EXTEND)
5122 || !MEM_P (SUBREG_REG (x)))
5123 && xmode_width > inner_width)
5124 nonzero
5125 |= (GET_MODE_MASK (GET_MODE (x)) & ~GET_MODE_MASK (inner_mode));
5126 }
5127 break;
5128
5129 case ASHIFT:
5130 case ASHIFTRT:
5131 case LSHIFTRT:
5132 case ROTATE:
5133 case ROTATERT:
5134 /* The nonzero bits are in two classes: any bits within MODE
5135 that aren't in xmode are always significant. The rest of the
5136 nonzero bits are those that are significant in the operand of
5137 the shift when shifted the appropriate number of bits. This
5138 shows that high-order bits are cleared by the right shift and
5139 low-order bits by left shifts. */
5140 if (CONST_INT_P (XEXP (x, 1))
5141 && INTVAL (XEXP (x, 1)) >= 0
5142 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
5143 && INTVAL (XEXP (x, 1)) < xmode_width)
5144 {
5145 int count = INTVAL (XEXP (x, 1));
5146 unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (xmode);
5147 unsigned HOST_WIDE_INT op_nonzero
5148 = cached_nonzero_bits (XEXP (x, 0), mode,
5149 known_x, known_mode, known_ret);
5150 unsigned HOST_WIDE_INT inner = op_nonzero & mode_mask;
5151 unsigned HOST_WIDE_INT outer = 0;
5152
5153 if (mode_width > xmode_width)
5154 outer = (op_nonzero & nonzero & ~mode_mask);
5155
5156 switch (code)
5157 {
5158 case ASHIFT:
5159 inner <<= count;
5160 break;
5161
5162 case LSHIFTRT:
5163 inner >>= count;
5164 break;
5165
5166 case ASHIFTRT:
5167 inner >>= count;
5168
5169 /* If the sign bit may have been nonzero before the shift, we
5170 need to mark all the places it could have been copied to
5171 by the shift as possibly nonzero. */
5172 if (inner & (HOST_WIDE_INT_1U << (xmode_width - 1 - count)))
5173 inner |= (((HOST_WIDE_INT_1U << count) - 1)
5174 << (xmode_width - count));
5175 break;
5176
5177 case ROTATE:
5178 inner = (inner << (count % xmode_width)
5179 | (inner >> (xmode_width - (count % xmode_width))))
5180 & mode_mask;
5181 break;
5182
5183 case ROTATERT:
5184 inner = (inner >> (count % xmode_width)
5185 | (inner << (xmode_width - (count % xmode_width))))
5186 & mode_mask;
5187 break;
5188
5189 default:
5190 gcc_unreachable ();
5191 }
5192
5193 nonzero &= (outer | inner);
5194 }
5195 break;
5196
5197 case FFS:
5198 case POPCOUNT:
5199 /* This is at most the number of bits in the mode. */
5200 nonzero = (HOST_WIDE_INT_UC (2) << (floor_log2 (mode_width))) - 1;
5201 break;
5202
5203 case CLZ:
5204 /* If CLZ has a known value at zero, then the nonzero bits are
5205 that value, plus the number of bits in the mode minus one. */
5206 if (CLZ_DEFINED_VALUE_AT_ZERO (mode, nonzero))
5207 nonzero
5208 |= (HOST_WIDE_INT_1U << (floor_log2 (mode_width))) - 1;
5209 else
5210 nonzero = -1;
5211 break;
5212
5213 case CTZ:
5214 /* If CTZ has a known value at zero, then the nonzero bits are
5215 that value, plus the number of bits in the mode minus one. */
5216 if (CTZ_DEFINED_VALUE_AT_ZERO (mode, nonzero))
5217 nonzero
5218 |= (HOST_WIDE_INT_1U << (floor_log2 (mode_width))) - 1;
5219 else
5220 nonzero = -1;
5221 break;
5222
5223 case CLRSB:
5224 /* This is at most the number of bits in the mode minus 1. */
5225 nonzero = (HOST_WIDE_INT_1U << (floor_log2 (mode_width))) - 1;
5226 break;
5227
5228 case PARITY:
5229 nonzero = 1;
5230 break;
5231
5232 case IF_THEN_ELSE:
5233 {
5234 unsigned HOST_WIDE_INT nonzero_true
5235 = cached_nonzero_bits (XEXP (x, 1), mode,
5236 known_x, known_mode, known_ret);
5237
5238 /* Don't call nonzero_bits for the second time if it cannot change
5239 anything. */
5240 if ((nonzero & nonzero_true) != nonzero)
5241 nonzero &= nonzero_true
5242 | cached_nonzero_bits (XEXP (x, 2), mode,
5243 known_x, known_mode, known_ret);
5244 }
5245 break;
5246
5247 default:
5248 break;
5249 }
5250
5251 return nonzero;
5252 }
5253
5254 /* See the macro definition above. */
5255 #undef cached_num_sign_bit_copies
5256
5257
5258 /* Return true if num_sign_bit_copies1 might recurse into both operands
5259 of X. */
5260
5261 static inline bool
5262 num_sign_bit_copies_binary_arith_p (const_rtx x)
5263 {
5264 if (!ARITHMETIC_P (x))
5265 return false;
5266 switch (GET_CODE (x))
5267 {
5268 case IOR:
5269 case AND:
5270 case XOR:
5271 case SMIN:
5272 case SMAX:
5273 case UMIN:
5274 case UMAX:
5275 case PLUS:
5276 case MINUS:
5277 case MULT:
5278 return true;
5279 default:
5280 return false;
5281 }
5282 }
5283
5284 /* The function cached_num_sign_bit_copies is a wrapper around
5285 num_sign_bit_copies1. It avoids exponential behavior in
5286 num_sign_bit_copies1 when X has identical subexpressions on the
5287 first or the second level. */
5288
5289 static unsigned int
5290 cached_num_sign_bit_copies (const_rtx x, scalar_int_mode mode,
5291 const_rtx known_x, machine_mode known_mode,
5292 unsigned int known_ret)
5293 {
5294 if (x == known_x && mode == known_mode)
5295 return known_ret;
5296
5297 /* Try to find identical subexpressions. If found call
5298 num_sign_bit_copies1 on X with the subexpressions as KNOWN_X and
5299 the precomputed value for the subexpression as KNOWN_RET. */
5300
5301 if (num_sign_bit_copies_binary_arith_p (x))
5302 {
5303 rtx x0 = XEXP (x, 0);
5304 rtx x1 = XEXP (x, 1);
5305
5306 /* Check the first level. */
5307 if (x0 == x1)
5308 return
5309 num_sign_bit_copies1 (x, mode, x0, mode,
5310 cached_num_sign_bit_copies (x0, mode, known_x,
5311 known_mode,
5312 known_ret));
5313
5314 /* Check the second level. */
5315 if (num_sign_bit_copies_binary_arith_p (x0)
5316 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
5317 return
5318 num_sign_bit_copies1 (x, mode, x1, mode,
5319 cached_num_sign_bit_copies (x1, mode, known_x,
5320 known_mode,
5321 known_ret));
5322
5323 if (num_sign_bit_copies_binary_arith_p (x1)
5324 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
5325 return
5326 num_sign_bit_copies1 (x, mode, x0, mode,
5327 cached_num_sign_bit_copies (x0, mode, known_x,
5328 known_mode,
5329 known_ret));
5330 }
5331
5332 return num_sign_bit_copies1 (x, mode, known_x, known_mode, known_ret);
5333 }
5334
5335 /* Return the number of bits at the high-order end of X that are known to
5336 be equal to the sign bit. X will be used in mode MODE. The returned
5337 value will always be between 1 and the number of bits in MODE. */
5338
5339 static unsigned int
5340 num_sign_bit_copies1 (const_rtx x, scalar_int_mode mode, const_rtx known_x,
5341 machine_mode known_mode,
5342 unsigned int known_ret)
5343 {
5344 enum rtx_code code = GET_CODE (x);
5345 unsigned int bitwidth = GET_MODE_PRECISION (mode);
5346 int num0, num1, result;
5347 unsigned HOST_WIDE_INT nonzero;
5348
5349 if (CONST_INT_P (x))
5350 {
5351 /* If the constant is negative, take its 1's complement and remask.
5352 Then see how many zero bits we have. */
5353 nonzero = UINTVAL (x) & GET_MODE_MASK (mode);
5354 if (bitwidth <= HOST_BITS_PER_WIDE_INT
5355 && (nonzero & (HOST_WIDE_INT_1U << (bitwidth - 1))) != 0)
5356 nonzero = (~nonzero) & GET_MODE_MASK (mode);
5357
5358 return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
5359 }
5360
5361 scalar_int_mode xmode, inner_mode;
5362 if (!is_a <scalar_int_mode> (GET_MODE (x), &xmode))
5363 return 1;
5364
5365 unsigned int xmode_width = GET_MODE_PRECISION (xmode);
5366
5367 /* For a smaller mode, just ignore the high bits. */
5368 if (bitwidth < xmode_width)
5369 {
5370 num0 = cached_num_sign_bit_copies (x, xmode,
5371 known_x, known_mode, known_ret);
5372 return MAX (1, num0 - (int) (xmode_width - bitwidth));
5373 }
5374
5375 if (bitwidth > xmode_width)
5376 {
5377 /* If this machine does not do all register operations on the entire
5378 register and MODE is wider than the mode of X, we can say nothing
5379 at all about the high-order bits. We extend this reasoning to RISC
5380 machines for operations that might not operate on full registers. */
5381 if (!(WORD_REGISTER_OPERATIONS && word_register_operation_p (x)))
5382 return 1;
5383
5384 /* Likewise on machines that do, if the mode of the object is smaller
5385 than a word and loads of that size don't sign extend, we can say
5386 nothing about the high order bits. */
5387 if (xmode_width < BITS_PER_WORD
5388 && load_extend_op (xmode) != SIGN_EXTEND)
5389 return 1;
5390 }
5391
5392 /* Please keep num_sign_bit_copies_binary_arith_p above in sync with
5393 the code in the switch below. */
5394 switch (code)
5395 {
5396 case REG:
5397
5398 #if defined(POINTERS_EXTEND_UNSIGNED)
5399 /* If pointers extend signed and this is a pointer in Pmode, say that
5400 all the bits above ptr_mode are known to be sign bit copies. */
5401 /* As we do not know which address space the pointer is referring to,
5402 we can do this only if the target does not support different pointer
5403 or address modes depending on the address space. */
5404 if (target_default_pointer_address_modes_p ()
5405 && ! POINTERS_EXTEND_UNSIGNED && xmode == Pmode
5406 && mode == Pmode && REG_POINTER (x)
5407 && !targetm.have_ptr_extend ())
5408 return GET_MODE_PRECISION (Pmode) - GET_MODE_PRECISION (ptr_mode) + 1;
5409 #endif
5410
5411 {
5412 unsigned int copies_for_hook = 1, copies = 1;
5413 rtx new_rtx = rtl_hooks.reg_num_sign_bit_copies (x, xmode, mode,
5414 &copies_for_hook);
5415
5416 if (new_rtx)
5417 copies = cached_num_sign_bit_copies (new_rtx, mode, known_x,
5418 known_mode, known_ret);
5419
5420 if (copies > 1 || copies_for_hook > 1)
5421 return MAX (copies, copies_for_hook);
5422
5423 /* Else, use nonzero_bits to guess num_sign_bit_copies (see below). */
5424 }
5425 break;
5426
5427 case MEM:
5428 /* Some RISC machines sign-extend all loads of smaller than a word. */
5429 if (load_extend_op (xmode) == SIGN_EXTEND)
5430 return MAX (1, ((int) bitwidth - (int) xmode_width + 1));
5431 break;
5432
5433 case SUBREG:
5434 /* If this is a SUBREG for a promoted object that is sign-extended
5435 and we are looking at it in a wider mode, we know that at least the
5436 high-order bits are known to be sign bit copies. */
5437
5438 if (SUBREG_PROMOTED_VAR_P (x) && SUBREG_PROMOTED_SIGNED_P (x))
5439 {
5440 num0 = cached_num_sign_bit_copies (SUBREG_REG (x), mode,
5441 known_x, known_mode, known_ret);
5442 return MAX ((int) bitwidth - (int) xmode_width + 1, num0);
5443 }
5444
5445 if (is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (x)), &inner_mode))
5446 {
5447 /* For a smaller object, just ignore the high bits. */
5448 if (bitwidth <= GET_MODE_PRECISION (inner_mode))
5449 {
5450 num0 = cached_num_sign_bit_copies (SUBREG_REG (x), inner_mode,
5451 known_x, known_mode,
5452 known_ret);
5453 return MAX (1, num0 - (int) (GET_MODE_PRECISION (inner_mode)
5454 - bitwidth));
5455 }
5456
5457 /* For paradoxical SUBREGs on machines where all register operations
5458 affect the entire register, just look inside. Note that we are
5459 passing MODE to the recursive call, so the number of sign bit
5460 copies will remain relative to that mode, not the inner mode.
5461
5462 This works only if loads sign extend. Otherwise, if we get a
5463 reload for the inner part, it may be loaded from the stack, and
5464 then we lose all sign bit copies that existed before the store
5465 to the stack. */
5466 if (WORD_REGISTER_OPERATIONS
5467 && load_extend_op (inner_mode) == SIGN_EXTEND
5468 && paradoxical_subreg_p (x)
5469 && MEM_P (SUBREG_REG (x)))
5470 return cached_num_sign_bit_copies (SUBREG_REG (x), mode,
5471 known_x, known_mode, known_ret);
5472 }
5473 break;
5474
5475 case SIGN_EXTRACT:
5476 if (CONST_INT_P (XEXP (x, 1)))
5477 return MAX (1, (int) bitwidth - INTVAL (XEXP (x, 1)));
5478 break;
5479
5480 case SIGN_EXTEND:
5481 if (is_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)), &inner_mode))
5482 return (bitwidth - GET_MODE_PRECISION (inner_mode)
5483 + cached_num_sign_bit_copies (XEXP (x, 0), inner_mode,
5484 known_x, known_mode, known_ret));
5485 break;
5486
5487 case TRUNCATE:
5488 /* For a smaller object, just ignore the high bits. */
5489 inner_mode = as_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)));
5490 num0 = cached_num_sign_bit_copies (XEXP (x, 0), inner_mode,
5491 known_x, known_mode, known_ret);
5492 return MAX (1, (num0 - (int) (GET_MODE_PRECISION (inner_mode)
5493 - bitwidth)));
5494
5495 case NOT:
5496 return cached_num_sign_bit_copies (XEXP (x, 0), mode,
5497 known_x, known_mode, known_ret);
5498
5499 case ROTATE: case ROTATERT:
5500 /* If we are rotating left by a number of bits less than the number
5501 of sign bit copies, we can just subtract that amount from the
5502 number. */
5503 if (CONST_INT_P (XEXP (x, 1))
5504 && INTVAL (XEXP (x, 1)) >= 0
5505 && INTVAL (XEXP (x, 1)) < (int) bitwidth)
5506 {
5507 num0 = cached_num_sign_bit_copies (XEXP (x, 0), mode,
5508 known_x, known_mode, known_ret);
5509 return MAX (1, num0 - (code == ROTATE ? INTVAL (XEXP (x, 1))
5510 : (int) bitwidth - INTVAL (XEXP (x, 1))));
5511 }
5512 break;
5513
5514 case NEG:
5515 /* In general, this subtracts one sign bit copy. But if the value
5516 is known to be positive, the number of sign bit copies is the
5517 same as that of the input. Finally, if the input has just one bit
5518 that might be nonzero, all the bits are copies of the sign bit. */
5519 num0 = cached_num_sign_bit_copies (XEXP (x, 0), mode,
5520 known_x, known_mode, known_ret);
5521 if (bitwidth > HOST_BITS_PER_WIDE_INT)
5522 return num0 > 1 ? num0 - 1 : 1;
5523
5524 nonzero = nonzero_bits (XEXP (x, 0), mode);
5525 if (nonzero == 1)
5526 return bitwidth;
5527
5528 if (num0 > 1
5529 && ((HOST_WIDE_INT_1U << (bitwidth - 1)) & nonzero))
5530 num0--;
5531
5532 return num0;
5533
5534 case IOR: case AND: case XOR:
5535 case SMIN: case SMAX: case UMIN: case UMAX:
5536 /* Logical operations will preserve the number of sign-bit copies.
5537 MIN and MAX operations always return one of the operands. */
5538 num0 = cached_num_sign_bit_copies (XEXP (x, 0), mode,
5539 known_x, known_mode, known_ret);
5540 num1 = cached_num_sign_bit_copies (XEXP (x, 1), mode,
5541 known_x, known_mode, known_ret);
5542
5543 /* If num1 is clearing some of the top bits then regardless of
5544 the other term, we are guaranteed to have at least that many
5545 high-order zero bits. */
5546 if (code == AND
5547 && num1 > 1
5548 && bitwidth <= HOST_BITS_PER_WIDE_INT
5549 && CONST_INT_P (XEXP (x, 1))
5550 && (UINTVAL (XEXP (x, 1))
5551 & (HOST_WIDE_INT_1U << (bitwidth - 1))) == 0)
5552 return num1;
5553
5554 /* Similarly for IOR when setting high-order bits. */
5555 if (code == IOR
5556 && num1 > 1
5557 && bitwidth <= HOST_BITS_PER_WIDE_INT
5558 && CONST_INT_P (XEXP (x, 1))
5559 && (UINTVAL (XEXP (x, 1))
5560 & (HOST_WIDE_INT_1U << (bitwidth - 1))) != 0)
5561 return num1;
5562
5563 return MIN (num0, num1);
5564
5565 case PLUS: case MINUS:
5566 /* For addition and subtraction, we can have a 1-bit carry. However,
5567 if we are subtracting 1 from a positive number, there will not
5568 be such a carry. Furthermore, if the positive number is known to
5569 be 0 or 1, we know the result is either -1 or 0. */
5570
5571 if (code == PLUS && XEXP (x, 1) == constm1_rtx
5572 && bitwidth <= HOST_BITS_PER_WIDE_INT)
5573 {
5574 nonzero = nonzero_bits (XEXP (x, 0), mode);
5575 if (((HOST_WIDE_INT_1U << (bitwidth - 1)) & nonzero) == 0)
5576 return (nonzero == 1 || nonzero == 0 ? bitwidth
5577 : bitwidth - floor_log2 (nonzero) - 1);
5578 }
5579
5580 num0 = cached_num_sign_bit_copies (XEXP (x, 0), mode,
5581 known_x, known_mode, known_ret);
5582 num1 = cached_num_sign_bit_copies (XEXP (x, 1), mode,
5583 known_x, known_mode, known_ret);
5584 result = MAX (1, MIN (num0, num1) - 1);
5585
5586 return result;
5587
5588 case MULT:
5589 /* The number of bits of the product is the sum of the number of
5590 bits of both terms. However, unless one of the terms if known
5591 to be positive, we must allow for an additional bit since negating
5592 a negative number can remove one sign bit copy. */
5593
5594 num0 = cached_num_sign_bit_copies (XEXP (x, 0), mode,
5595 known_x, known_mode, known_ret);
5596 num1 = cached_num_sign_bit_copies (XEXP (x, 1), mode,
5597 known_x, known_mode, known_ret);
5598
5599 result = bitwidth - (bitwidth - num0) - (bitwidth - num1);
5600 if (result > 0
5601 && (bitwidth > HOST_BITS_PER_WIDE_INT
5602 || (((nonzero_bits (XEXP (x, 0), mode)
5603 & (HOST_WIDE_INT_1U << (bitwidth - 1))) != 0)
5604 && ((nonzero_bits (XEXP (x, 1), mode)
5605 & (HOST_WIDE_INT_1U << (bitwidth - 1)))
5606 != 0))))
5607 result--;
5608
5609 return MAX (1, result);
5610
5611 case UDIV:
5612 /* The result must be <= the first operand. If the first operand
5613 has the high bit set, we know nothing about the number of sign
5614 bit copies. */
5615 if (bitwidth > HOST_BITS_PER_WIDE_INT)
5616 return 1;
5617 else if ((nonzero_bits (XEXP (x, 0), mode)
5618 & (HOST_WIDE_INT_1U << (bitwidth - 1))) != 0)
5619 return 1;
5620 else
5621 return cached_num_sign_bit_copies (XEXP (x, 0), mode,
5622 known_x, known_mode, known_ret);
5623
5624 case UMOD:
5625 /* The result must be <= the second operand. If the second operand
5626 has (or just might have) the high bit set, we know nothing about
5627 the number of sign bit copies. */
5628 if (bitwidth > HOST_BITS_PER_WIDE_INT)
5629 return 1;
5630 else if ((nonzero_bits (XEXP (x, 1), mode)
5631 & (HOST_WIDE_INT_1U << (bitwidth - 1))) != 0)
5632 return 1;
5633 else
5634 return cached_num_sign_bit_copies (XEXP (x, 1), mode,
5635 known_x, known_mode, known_ret);
5636
5637 case DIV:
5638 /* Similar to unsigned division, except that we have to worry about
5639 the case where the divisor is negative, in which case we have
5640 to add 1. */
5641 result = cached_num_sign_bit_copies (XEXP (x, 0), mode,
5642 known_x, known_mode, known_ret);
5643 if (result > 1
5644 && (bitwidth > HOST_BITS_PER_WIDE_INT
5645 || (nonzero_bits (XEXP (x, 1), mode)
5646 & (HOST_WIDE_INT_1U << (bitwidth - 1))) != 0))
5647 result--;
5648
5649 return result;
5650
5651 case MOD:
5652 result = cached_num_sign_bit_copies (XEXP (x, 1), mode,
5653 known_x, known_mode, known_ret);
5654 if (result > 1
5655 && (bitwidth > HOST_BITS_PER_WIDE_INT
5656 || (nonzero_bits (XEXP (x, 1), mode)
5657 & (HOST_WIDE_INT_1U << (bitwidth - 1))) != 0))
5658 result--;
5659
5660 return result;
5661
5662 case ASHIFTRT:
5663 /* Shifts by a constant add to the number of bits equal to the
5664 sign bit. */
5665 num0 = cached_num_sign_bit_copies (XEXP (x, 0), mode,
5666 known_x, known_mode, known_ret);
5667 if (CONST_INT_P (XEXP (x, 1))
5668 && INTVAL (XEXP (x, 1)) > 0
5669 && INTVAL (XEXP (x, 1)) < xmode_width)
5670 num0 = MIN ((int) bitwidth, num0 + INTVAL (XEXP (x, 1)));
5671
5672 return num0;
5673
5674 case ASHIFT:
5675 /* Left shifts destroy copies. */
5676 if (!CONST_INT_P (XEXP (x, 1))
5677 || INTVAL (XEXP (x, 1)) < 0
5678 || INTVAL (XEXP (x, 1)) >= (int) bitwidth
5679 || INTVAL (XEXP (x, 1)) >= xmode_width)
5680 return 1;
5681
5682 num0 = cached_num_sign_bit_copies (XEXP (x, 0), mode,
5683 known_x, known_mode, known_ret);
5684 return MAX (1, num0 - INTVAL (XEXP (x, 1)));
5685
5686 case IF_THEN_ELSE:
5687 num0 = cached_num_sign_bit_copies (XEXP (x, 1), mode,
5688 known_x, known_mode, known_ret);
5689 num1 = cached_num_sign_bit_copies (XEXP (x, 2), mode,
5690 known_x, known_mode, known_ret);
5691 return MIN (num0, num1);
5692
5693 case EQ: case NE: case GE: case GT: case LE: case LT:
5694 case UNEQ: case LTGT: case UNGE: case UNGT: case UNLE: case UNLT:
5695 case GEU: case GTU: case LEU: case LTU:
5696 case UNORDERED: case ORDERED:
5697 /* If the constant is negative, take its 1's complement and remask.
5698 Then see how many zero bits we have. */
5699 nonzero = STORE_FLAG_VALUE;
5700 if (bitwidth <= HOST_BITS_PER_WIDE_INT
5701 && (nonzero & (HOST_WIDE_INT_1U << (bitwidth - 1))) != 0)
5702 nonzero = (~nonzero) & GET_MODE_MASK (mode);
5703
5704 return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
5705
5706 default:
5707 break;
5708 }
5709
5710 /* If we haven't been able to figure it out by one of the above rules,
5711 see if some of the high-order bits are known to be zero. If so,
5712 count those bits and return one less than that amount. If we can't
5713 safely compute the mask for this mode, always return BITWIDTH. */
5714
5715 bitwidth = GET_MODE_PRECISION (mode);
5716 if (bitwidth > HOST_BITS_PER_WIDE_INT)
5717 return 1;
5718
5719 nonzero = nonzero_bits (x, mode);
5720 return nonzero & (HOST_WIDE_INT_1U << (bitwidth - 1))
5721 ? 1 : bitwidth - floor_log2 (nonzero) - 1;
5722 }
5723
5724 /* Calculate the rtx_cost of a single instruction pattern. A return value of
5725 zero indicates an instruction pattern without a known cost. */
5726
5727 int
5728 pattern_cost (rtx pat, bool speed)
5729 {
5730 int i, cost;
5731 rtx set;
5732
5733 /* Extract the single set rtx from the instruction pattern. We
5734 can't use single_set since we only have the pattern. We also
5735 consider PARALLELs of a normal set and a single comparison. In
5736 that case we use the cost of the non-comparison SET operation,
5737 which is most-likely to be the real cost of this operation. */
5738 if (GET_CODE (pat) == SET)
5739 set = pat;
5740 else if (GET_CODE (pat) == PARALLEL)
5741 {
5742 set = NULL_RTX;
5743 rtx comparison = NULL_RTX;
5744
5745 for (i = 0; i < XVECLEN (pat, 0); i++)
5746 {
5747 rtx x = XVECEXP (pat, 0, i);
5748 if (GET_CODE (x) == SET)
5749 {
5750 if (GET_CODE (SET_SRC (x)) == COMPARE)
5751 {
5752 if (comparison)
5753 return 0;
5754 comparison = x;
5755 }
5756 else
5757 {
5758 if (set)
5759 return 0;
5760 set = x;
5761 }
5762 }
5763 }
5764
5765 if (!set && comparison)
5766 set = comparison;
5767
5768 if (!set)
5769 return 0;
5770 }
5771 else
5772 return 0;
5773
5774 cost = set_src_cost (SET_SRC (set), GET_MODE (SET_DEST (set)), speed);
5775 return MAX (COSTS_N_INSNS (1), cost);
5776 }
5777
5778 /* Calculate the cost of a single instruction. A return value of zero
5779 indicates an instruction pattern without a known cost. */
5780
5781 int
5782 insn_cost (rtx_insn *insn, bool speed)
5783 {
5784 if (targetm.insn_cost)
5785 return targetm.insn_cost (insn, speed);
5786
5787 return pattern_cost (PATTERN (insn), speed);
5788 }
5789
5790 /* Returns estimate on cost of computing SEQ. */
5791
5792 unsigned
5793 seq_cost (const rtx_insn *seq, bool speed)
5794 {
5795 unsigned cost = 0;
5796 rtx set;
5797
5798 for (; seq; seq = NEXT_INSN (seq))
5799 {
5800 set = single_set (seq);
5801 if (set)
5802 cost += set_rtx_cost (set, speed);
5803 else if (NONDEBUG_INSN_P (seq))
5804 {
5805 int this_cost = insn_cost (CONST_CAST_RTX_INSN (seq), speed);
5806 if (this_cost > 0)
5807 cost += this_cost;
5808 else
5809 cost++;
5810 }
5811 }
5812
5813 return cost;
5814 }
5815
5816 /* Given an insn INSN and condition COND, return the condition in a
5817 canonical form to simplify testing by callers. Specifically:
5818
5819 (1) The code will always be a comparison operation (EQ, NE, GT, etc.).
5820 (2) Both operands will be machine operands.
5821 (3) If an operand is a constant, it will be the second operand.
5822 (4) (LE x const) will be replaced with (LT x <const+1>) and similarly
5823 for GE, GEU, and LEU.
5824
5825 If the condition cannot be understood, or is an inequality floating-point
5826 comparison which needs to be reversed, 0 will be returned.
5827
5828 If REVERSE is nonzero, then reverse the condition prior to canonizing it.
5829
5830 If EARLIEST is nonzero, it is a pointer to a place where the earliest
5831 insn used in locating the condition was found. If a replacement test
5832 of the condition is desired, it should be placed in front of that
5833 insn and we will be sure that the inputs are still valid.
5834
5835 If WANT_REG is nonzero, we wish the condition to be relative to that
5836 register, if possible. Therefore, do not canonicalize the condition
5837 further. If ALLOW_CC_MODE is nonzero, allow the condition returned
5838 to be a compare to a CC mode register.
5839
5840 If VALID_AT_INSN_P, the condition must be valid at both *EARLIEST
5841 and at INSN. */
5842
5843 rtx
5844 canonicalize_condition (rtx_insn *insn, rtx cond, int reverse,
5845 rtx_insn **earliest,
5846 rtx want_reg, int allow_cc_mode, int valid_at_insn_p)
5847 {
5848 enum rtx_code code;
5849 rtx_insn *prev = insn;
5850 const_rtx set;
5851 rtx tem;
5852 rtx op0, op1;
5853 int reverse_code = 0;
5854 machine_mode mode;
5855 basic_block bb = BLOCK_FOR_INSN (insn);
5856
5857 code = GET_CODE (cond);
5858 mode = GET_MODE (cond);
5859 op0 = XEXP (cond, 0);
5860 op1 = XEXP (cond, 1);
5861
5862 if (reverse)
5863 code = reversed_comparison_code (cond, insn);
5864 if (code == UNKNOWN)
5865 return 0;
5866
5867 if (earliest)
5868 *earliest = insn;
5869
5870 /* If we are comparing a register with zero, see if the register is set
5871 in the previous insn to a COMPARE or a comparison operation. Perform
5872 the same tests as a function of STORE_FLAG_VALUE as find_comparison_args
5873 in cse.cc */
5874
5875 while ((GET_RTX_CLASS (code) == RTX_COMPARE
5876 || GET_RTX_CLASS (code) == RTX_COMM_COMPARE)
5877 && op1 == CONST0_RTX (GET_MODE (op0))
5878 && op0 != want_reg)
5879 {
5880 /* Set nonzero when we find something of interest. */
5881 rtx x = 0;
5882
5883 /* If this is a COMPARE, pick up the two things being compared. */
5884 if (GET_CODE (op0) == COMPARE)
5885 {
5886 op1 = XEXP (op0, 1);
5887 op0 = XEXP (op0, 0);
5888 continue;
5889 }
5890 else if (!REG_P (op0))
5891 break;
5892
5893 /* Go back to the previous insn. Stop if it is not an INSN. We also
5894 stop if it isn't a single set or if it has a REG_INC note because
5895 we don't want to bother dealing with it. */
5896
5897 prev = prev_nonnote_nondebug_insn (prev);
5898
5899 if (prev == 0
5900 || !NONJUMP_INSN_P (prev)
5901 || FIND_REG_INC_NOTE (prev, NULL_RTX)
5902 /* In cfglayout mode, there do not have to be labels at the
5903 beginning of a block, or jumps at the end, so the previous
5904 conditions would not stop us when we reach bb boundary. */
5905 || BLOCK_FOR_INSN (prev) != bb)
5906 break;
5907
5908 set = set_of (op0, prev);
5909
5910 if (set
5911 && (GET_CODE (set) != SET
5912 || !rtx_equal_p (SET_DEST (set), op0)))
5913 break;
5914
5915 /* If this is setting OP0, get what it sets it to if it looks
5916 relevant. */
5917 if (set)
5918 {
5919 machine_mode inner_mode = GET_MODE (SET_DEST (set));
5920 #ifdef FLOAT_STORE_FLAG_VALUE
5921 REAL_VALUE_TYPE fsfv;
5922 #endif
5923
5924 /* ??? We may not combine comparisons done in a CCmode with
5925 comparisons not done in a CCmode. This is to aid targets
5926 like Alpha that have an IEEE compliant EQ instruction, and
5927 a non-IEEE compliant BEQ instruction. The use of CCmode is
5928 actually artificial, simply to prevent the combination, but
5929 should not affect other platforms.
5930
5931 However, we must allow VOIDmode comparisons to match either
5932 CCmode or non-CCmode comparison, because some ports have
5933 modeless comparisons inside branch patterns.
5934
5935 ??? This mode check should perhaps look more like the mode check
5936 in simplify_comparison in combine. */
5937 if (((GET_MODE_CLASS (mode) == MODE_CC)
5938 != (GET_MODE_CLASS (inner_mode) == MODE_CC))
5939 && mode != VOIDmode
5940 && inner_mode != VOIDmode)
5941 break;
5942 if (GET_CODE (SET_SRC (set)) == COMPARE
5943 || (((code == NE
5944 || (code == LT
5945 && val_signbit_known_set_p (inner_mode,
5946 STORE_FLAG_VALUE))
5947 #ifdef FLOAT_STORE_FLAG_VALUE
5948 || (code == LT
5949 && SCALAR_FLOAT_MODE_P (inner_mode)
5950 && (fsfv = FLOAT_STORE_FLAG_VALUE (inner_mode),
5951 REAL_VALUE_NEGATIVE (fsfv)))
5952 #endif
5953 ))
5954 && COMPARISON_P (SET_SRC (set))))
5955 x = SET_SRC (set);
5956 else if (((code == EQ
5957 || (code == GE
5958 && val_signbit_known_set_p (inner_mode,
5959 STORE_FLAG_VALUE))
5960 #ifdef FLOAT_STORE_FLAG_VALUE
5961 || (code == GE
5962 && SCALAR_FLOAT_MODE_P (inner_mode)
5963 && (fsfv = FLOAT_STORE_FLAG_VALUE (inner_mode),
5964 REAL_VALUE_NEGATIVE (fsfv)))
5965 #endif
5966 ))
5967 && COMPARISON_P (SET_SRC (set)))
5968 {
5969 reverse_code = 1;
5970 x = SET_SRC (set);
5971 }
5972 else if ((code == EQ || code == NE)
5973 && GET_CODE (SET_SRC (set)) == XOR)
5974 /* Handle sequences like:
5975
5976 (set op0 (xor X Y))
5977 ...(eq|ne op0 (const_int 0))...
5978
5979 in which case:
5980
5981 (eq op0 (const_int 0)) reduces to (eq X Y)
5982 (ne op0 (const_int 0)) reduces to (ne X Y)
5983
5984 This is the form used by MIPS16, for example. */
5985 x = SET_SRC (set);
5986 else
5987 break;
5988 }
5989
5990 else if (reg_set_p (op0, prev))
5991 /* If this sets OP0, but not directly, we have to give up. */
5992 break;
5993
5994 if (x)
5995 {
5996 /* If the caller is expecting the condition to be valid at INSN,
5997 make sure X doesn't change before INSN. */
5998 if (valid_at_insn_p)
5999 if (modified_in_p (x, prev) || modified_between_p (x, prev, insn))
6000 break;
6001 if (COMPARISON_P (x))
6002 code = GET_CODE (x);
6003 if (reverse_code)
6004 {
6005 code = reversed_comparison_code (x, prev);
6006 if (code == UNKNOWN)
6007 return 0;
6008 reverse_code = 0;
6009 }
6010
6011 op0 = XEXP (x, 0), op1 = XEXP (x, 1);
6012 if (earliest)
6013 *earliest = prev;
6014 }
6015 }
6016
6017 /* If constant is first, put it last. */
6018 if (CONSTANT_P (op0))
6019 code = swap_condition (code), tem = op0, op0 = op1, op1 = tem;
6020
6021 /* If OP0 is the result of a comparison, we weren't able to find what
6022 was really being compared, so fail. */
6023 if (!allow_cc_mode
6024 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6025 return 0;
6026
6027 /* Canonicalize any ordered comparison with integers involving equality
6028 if we can do computations in the relevant mode and we do not
6029 overflow. */
6030
6031 scalar_int_mode op0_mode;
6032 if (CONST_INT_P (op1)
6033 && is_a <scalar_int_mode> (GET_MODE (op0), &op0_mode)
6034 && GET_MODE_PRECISION (op0_mode) <= HOST_BITS_PER_WIDE_INT)
6035 {
6036 HOST_WIDE_INT const_val = INTVAL (op1);
6037 unsigned HOST_WIDE_INT uconst_val = const_val;
6038 unsigned HOST_WIDE_INT max_val
6039 = (unsigned HOST_WIDE_INT) GET_MODE_MASK (op0_mode);
6040
6041 switch (code)
6042 {
6043 case LE:
6044 if ((unsigned HOST_WIDE_INT) const_val != max_val >> 1)
6045 code = LT, op1 = gen_int_mode (const_val + 1, op0_mode);
6046 break;
6047
6048 /* When cross-compiling, const_val might be sign-extended from
6049 BITS_PER_WORD to HOST_BITS_PER_WIDE_INT */
6050 case GE:
6051 if ((const_val & max_val)
6052 != (HOST_WIDE_INT_1U << (GET_MODE_PRECISION (op0_mode) - 1)))
6053 code = GT, op1 = gen_int_mode (const_val - 1, op0_mode);
6054 break;
6055
6056 case LEU:
6057 if (uconst_val < max_val)
6058 code = LTU, op1 = gen_int_mode (uconst_val + 1, op0_mode);
6059 break;
6060
6061 case GEU:
6062 if (uconst_val != 0)
6063 code = GTU, op1 = gen_int_mode (uconst_val - 1, op0_mode);
6064 break;
6065
6066 default:
6067 break;
6068 }
6069 }
6070
6071 /* We promised to return a comparison. */
6072 rtx ret = gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
6073 if (COMPARISON_P (ret))
6074 return ret;
6075 return 0;
6076 }
6077
6078 /* Given a jump insn JUMP, return the condition that will cause it to branch
6079 to its JUMP_LABEL. If the condition cannot be understood, or is an
6080 inequality floating-point comparison which needs to be reversed, 0 will
6081 be returned.
6082
6083 If EARLIEST is nonzero, it is a pointer to a place where the earliest
6084 insn used in locating the condition was found. If a replacement test
6085 of the condition is desired, it should be placed in front of that
6086 insn and we will be sure that the inputs are still valid. If EARLIEST
6087 is null, the returned condition will be valid at INSN.
6088
6089 If ALLOW_CC_MODE is nonzero, allow the condition returned to be a
6090 compare CC mode register.
6091
6092 VALID_AT_INSN_P is the same as for canonicalize_condition. */
6093
6094 rtx
6095 get_condition (rtx_insn *jump, rtx_insn **earliest, int allow_cc_mode,
6096 int valid_at_insn_p)
6097 {
6098 rtx cond;
6099 int reverse;
6100 rtx set;
6101
6102 /* If this is not a standard conditional jump, we can't parse it. */
6103 if (!JUMP_P (jump)
6104 || ! any_condjump_p (jump))
6105 return 0;
6106 set = pc_set (jump);
6107
6108 cond = XEXP (SET_SRC (set), 0);
6109
6110 /* If this branches to JUMP_LABEL when the condition is false, reverse
6111 the condition. */
6112 reverse
6113 = GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
6114 && label_ref_label (XEXP (SET_SRC (set), 2)) == JUMP_LABEL (jump);
6115
6116 return canonicalize_condition (jump, cond, reverse, earliest, NULL_RTX,
6117 allow_cc_mode, valid_at_insn_p);
6118 }
6119
6120 /* Initialize the table NUM_SIGN_BIT_COPIES_IN_REP based on
6121 TARGET_MODE_REP_EXTENDED.
6122
6123 Note that we assume that the property of
6124 TARGET_MODE_REP_EXTENDED(B, C) is sticky to the integral modes
6125 narrower than mode B. I.e., if A is a mode narrower than B then in
6126 order to be able to operate on it in mode B, mode A needs to
6127 satisfy the requirements set by the representation of mode B. */
6128
6129 static void
6130 init_num_sign_bit_copies_in_rep (void)
6131 {
6132 opt_scalar_int_mode in_mode_iter;
6133 scalar_int_mode mode;
6134
6135 FOR_EACH_MODE_IN_CLASS (in_mode_iter, MODE_INT)
6136 FOR_EACH_MODE_UNTIL (mode, in_mode_iter.require ())
6137 {
6138 scalar_int_mode in_mode = in_mode_iter.require ();
6139 scalar_int_mode i;
6140
6141 /* Currently, it is assumed that TARGET_MODE_REP_EXTENDED
6142 extends to the next widest mode. */
6143 gcc_assert (targetm.mode_rep_extended (mode, in_mode) == UNKNOWN
6144 || GET_MODE_WIDER_MODE (mode).require () == in_mode);
6145
6146 /* We are in in_mode. Count how many bits outside of mode
6147 have to be copies of the sign-bit. */
6148 FOR_EACH_MODE (i, mode, in_mode)
6149 {
6150 /* This must always exist (for the last iteration it will be
6151 IN_MODE). */
6152 scalar_int_mode wider = GET_MODE_WIDER_MODE (i).require ();
6153
6154 if (targetm.mode_rep_extended (i, wider) == SIGN_EXTEND
6155 /* We can only check sign-bit copies starting from the
6156 top-bit. In order to be able to check the bits we
6157 have already seen we pretend that subsequent bits
6158 have to be sign-bit copies too. */
6159 || num_sign_bit_copies_in_rep [in_mode][mode])
6160 num_sign_bit_copies_in_rep [in_mode][mode]
6161 += GET_MODE_PRECISION (wider) - GET_MODE_PRECISION (i);
6162 }
6163 }
6164 }
6165
6166 /* Suppose that truncation from the machine mode of X to MODE is not a
6167 no-op. See if there is anything special about X so that we can
6168 assume it already contains a truncated value of MODE. */
6169
6170 bool
6171 truncated_to_mode (machine_mode mode, const_rtx x)
6172 {
6173 /* This register has already been used in MODE without explicit
6174 truncation. */
6175 if (REG_P (x) && rtl_hooks.reg_truncated_to_mode (mode, x))
6176 return true;
6177
6178 /* See if we already satisfy the requirements of MODE. If yes we
6179 can just switch to MODE. */
6180 if (num_sign_bit_copies_in_rep[GET_MODE (x)][mode]
6181 && (num_sign_bit_copies (x, GET_MODE (x))
6182 >= num_sign_bit_copies_in_rep[GET_MODE (x)][mode] + 1))
6183 return true;
6184
6185 return false;
6186 }
6187
6188 /* Return true if RTX code CODE has a single sequence of zero or more
6189 "e" operands and no rtvec operands. Initialize its rtx_all_subrtx_bounds
6190 entry in that case. */
6191
6192 static bool
6193 setup_reg_subrtx_bounds (unsigned int code)
6194 {
6195 const char *format = GET_RTX_FORMAT ((enum rtx_code) code);
6196 unsigned int i = 0;
6197 for (; format[i] != 'e'; ++i)
6198 {
6199 if (!format[i])
6200 /* No subrtxes. Leave start and count as 0. */
6201 return true;
6202 if (format[i] == 'E' || format[i] == 'V')
6203 return false;
6204 }
6205
6206 /* Record the sequence of 'e's. */
6207 rtx_all_subrtx_bounds[code].start = i;
6208 do
6209 ++i;
6210 while (format[i] == 'e');
6211 rtx_all_subrtx_bounds[code].count = i - rtx_all_subrtx_bounds[code].start;
6212 /* rtl-iter.h relies on this. */
6213 gcc_checking_assert (rtx_all_subrtx_bounds[code].count <= 3);
6214
6215 for (; format[i]; ++i)
6216 if (format[i] == 'E' || format[i] == 'V' || format[i] == 'e')
6217 return false;
6218
6219 return true;
6220 }
6221
6222 /* Initialize rtx_all_subrtx_bounds. */
6223 void
6224 init_rtlanal (void)
6225 {
6226 int i;
6227 for (i = 0; i < NUM_RTX_CODE; i++)
6228 {
6229 if (!setup_reg_subrtx_bounds (i))
6230 rtx_all_subrtx_bounds[i].count = UCHAR_MAX;
6231 if (GET_RTX_CLASS (i) != RTX_CONST_OBJ)
6232 rtx_nonconst_subrtx_bounds[i] = rtx_all_subrtx_bounds[i];
6233 }
6234
6235 init_num_sign_bit_copies_in_rep ();
6236 }
6237
6238 /* Check whether this is a constant pool constant. */
6239 bool
6240 constant_pool_constant_p (rtx x)
6241 {
6242 x = avoid_constant_pool_reference (x);
6243 return CONST_DOUBLE_P (x);
6244 }
6245
6246 /* If M is a bitmask that selects a field of low-order bits within an item but
6247 not the entire word, return the length of the field. Return -1 otherwise.
6248 M is used in machine mode MODE. */
6249
6250 int
6251 low_bitmask_len (machine_mode mode, unsigned HOST_WIDE_INT m)
6252 {
6253 if (mode != VOIDmode)
6254 {
6255 if (!HWI_COMPUTABLE_MODE_P (mode))
6256 return -1;
6257 m &= GET_MODE_MASK (mode);
6258 }
6259
6260 return exact_log2 (m + 1);
6261 }
6262
6263 /* Return the mode of MEM's address. */
6264
6265 scalar_int_mode
6266 get_address_mode (rtx mem)
6267 {
6268 machine_mode mode;
6269
6270 gcc_assert (MEM_P (mem));
6271 mode = GET_MODE (XEXP (mem, 0));
6272 if (mode != VOIDmode)
6273 return as_a <scalar_int_mode> (mode);
6274 return targetm.addr_space.address_mode (MEM_ADDR_SPACE (mem));
6275 }
6276
6277 /* Split up a CONST_DOUBLE or integer constant rtx
6278 into two rtx's for single words,
6279 storing in *FIRST the word that comes first in memory in the target
6280 and in *SECOND the other.
6281
6282 TODO: This function needs to be rewritten to work on any size
6283 integer. */
6284
6285 void
6286 split_double (rtx value, rtx *first, rtx *second)
6287 {
6288 if (CONST_INT_P (value))
6289 {
6290 if (HOST_BITS_PER_WIDE_INT >= (2 * BITS_PER_WORD))
6291 {
6292 /* In this case the CONST_INT holds both target words.
6293 Extract the bits from it into two word-sized pieces.
6294 Sign extend each half to HOST_WIDE_INT. */
6295 unsigned HOST_WIDE_INT low, high;
6296 unsigned HOST_WIDE_INT mask, sign_bit, sign_extend;
6297 unsigned bits_per_word = BITS_PER_WORD;
6298
6299 /* Set sign_bit to the most significant bit of a word. */
6300 sign_bit = 1;
6301 sign_bit <<= bits_per_word - 1;
6302
6303 /* Set mask so that all bits of the word are set. We could
6304 have used 1 << BITS_PER_WORD instead of basing the
6305 calculation on sign_bit. However, on machines where
6306 HOST_BITS_PER_WIDE_INT == BITS_PER_WORD, it could cause a
6307 compiler warning, even though the code would never be
6308 executed. */
6309 mask = sign_bit << 1;
6310 mask--;
6311
6312 /* Set sign_extend as any remaining bits. */
6313 sign_extend = ~mask;
6314
6315 /* Pick the lower word and sign-extend it. */
6316 low = INTVAL (value);
6317 low &= mask;
6318 if (low & sign_bit)
6319 low |= sign_extend;
6320
6321 /* Pick the higher word, shifted to the least significant
6322 bits, and sign-extend it. */
6323 high = INTVAL (value);
6324 high >>= bits_per_word - 1;
6325 high >>= 1;
6326 high &= mask;
6327 if (high & sign_bit)
6328 high |= sign_extend;
6329
6330 /* Store the words in the target machine order. */
6331 if (WORDS_BIG_ENDIAN)
6332 {
6333 *first = GEN_INT (high);
6334 *second = GEN_INT (low);
6335 }
6336 else
6337 {
6338 *first = GEN_INT (low);
6339 *second = GEN_INT (high);
6340 }
6341 }
6342 else
6343 {
6344 /* The rule for using CONST_INT for a wider mode
6345 is that we regard the value as signed.
6346 So sign-extend it. */
6347 rtx high = (INTVAL (value) < 0 ? constm1_rtx : const0_rtx);
6348 if (WORDS_BIG_ENDIAN)
6349 {
6350 *first = high;
6351 *second = value;
6352 }
6353 else
6354 {
6355 *first = value;
6356 *second = high;
6357 }
6358 }
6359 }
6360 else if (GET_CODE (value) == CONST_WIDE_INT)
6361 {
6362 /* All of this is scary code and needs to be converted to
6363 properly work with any size integer. */
6364 gcc_assert (CONST_WIDE_INT_NUNITS (value) == 2);
6365 if (WORDS_BIG_ENDIAN)
6366 {
6367 *first = GEN_INT (CONST_WIDE_INT_ELT (value, 1));
6368 *second = GEN_INT (CONST_WIDE_INT_ELT (value, 0));
6369 }
6370 else
6371 {
6372 *first = GEN_INT (CONST_WIDE_INT_ELT (value, 0));
6373 *second = GEN_INT (CONST_WIDE_INT_ELT (value, 1));
6374 }
6375 }
6376 else if (!CONST_DOUBLE_P (value))
6377 {
6378 if (WORDS_BIG_ENDIAN)
6379 {
6380 *first = const0_rtx;
6381 *second = value;
6382 }
6383 else
6384 {
6385 *first = value;
6386 *second = const0_rtx;
6387 }
6388 }
6389 else if (GET_MODE (value) == VOIDmode
6390 /* This is the old way we did CONST_DOUBLE integers. */
6391 || GET_MODE_CLASS (GET_MODE (value)) == MODE_INT)
6392 {
6393 /* In an integer, the words are defined as most and least significant.
6394 So order them by the target's convention. */
6395 if (WORDS_BIG_ENDIAN)
6396 {
6397 *first = GEN_INT (CONST_DOUBLE_HIGH (value));
6398 *second = GEN_INT (CONST_DOUBLE_LOW (value));
6399 }
6400 else
6401 {
6402 *first = GEN_INT (CONST_DOUBLE_LOW (value));
6403 *second = GEN_INT (CONST_DOUBLE_HIGH (value));
6404 }
6405 }
6406 else
6407 {
6408 long l[2];
6409
6410 /* Note, this converts the REAL_VALUE_TYPE to the target's
6411 format, splits up the floating point double and outputs
6412 exactly 32 bits of it into each of l[0] and l[1] --
6413 not necessarily BITS_PER_WORD bits. */
6414 REAL_VALUE_TO_TARGET_DOUBLE (*CONST_DOUBLE_REAL_VALUE (value), l);
6415
6416 /* If 32 bits is an entire word for the target, but not for the host,
6417 then sign-extend on the host so that the number will look the same
6418 way on the host that it would on the target. See for instance
6419 simplify_unary_operation. The #if is needed to avoid compiler
6420 warnings. */
6421
6422 #if HOST_BITS_PER_LONG > 32
6423 if (BITS_PER_WORD < HOST_BITS_PER_LONG && BITS_PER_WORD == 32)
6424 {
6425 if (l[0] & ((long) 1 << 31))
6426 l[0] |= ((unsigned long) (-1) << 32);
6427 if (l[1] & ((long) 1 << 31))
6428 l[1] |= ((unsigned long) (-1) << 32);
6429 }
6430 #endif
6431
6432 *first = GEN_INT (l[0]);
6433 *second = GEN_INT (l[1]);
6434 }
6435 }
6436
6437 /* Return true if X is a sign_extract or zero_extract from the least
6438 significant bit. */
6439
6440 static bool
6441 lsb_bitfield_op_p (rtx x)
6442 {
6443 if (GET_RTX_CLASS (GET_CODE (x)) == RTX_BITFIELD_OPS)
6444 {
6445 machine_mode mode = GET_MODE (XEXP (x, 0));
6446 HOST_WIDE_INT len = INTVAL (XEXP (x, 1));
6447 HOST_WIDE_INT pos = INTVAL (XEXP (x, 2));
6448 poly_int64 remaining_bits = GET_MODE_PRECISION (mode) - len;
6449
6450 return known_eq (pos, BITS_BIG_ENDIAN ? remaining_bits : 0);
6451 }
6452 return false;
6453 }
6454
6455 /* Strip outer address "mutations" from LOC and return a pointer to the
6456 inner value. If OUTER_CODE is nonnull, store the code of the innermost
6457 stripped expression there.
6458
6459 "Mutations" either convert between modes or apply some kind of
6460 extension, truncation or alignment. */
6461
6462 rtx *
6463 strip_address_mutations (rtx *loc, enum rtx_code *outer_code)
6464 {
6465 for (;;)
6466 {
6467 enum rtx_code code = GET_CODE (*loc);
6468 if (GET_RTX_CLASS (code) == RTX_UNARY)
6469 /* Things like SIGN_EXTEND, ZERO_EXTEND and TRUNCATE can be
6470 used to convert between pointer sizes. */
6471 loc = &XEXP (*loc, 0);
6472 else if (lsb_bitfield_op_p (*loc))
6473 /* A [SIGN|ZERO]_EXTRACT from the least significant bit effectively
6474 acts as a combined truncation and extension. */
6475 loc = &XEXP (*loc, 0);
6476 else if (code == AND && CONST_INT_P (XEXP (*loc, 1)))
6477 /* (and ... (const_int -X)) is used to align to X bytes. */
6478 loc = &XEXP (*loc, 0);
6479 else if (code == SUBREG
6480 && (!OBJECT_P (SUBREG_REG (*loc))
6481 || CONSTANT_P (SUBREG_REG (*loc)))
6482 && subreg_lowpart_p (*loc))
6483 /* (subreg (operator ...) ...) inside AND is used for mode
6484 conversion too. It is also used for load-address operations
6485 in which an extension can be done for free, such as:
6486
6487 (zero_extend:DI
6488 (subreg:SI (plus:DI (reg:DI R) (symbol_ref:DI "foo") 0)))
6489
6490 The latter usage also covers subregs of plain "displacements",
6491 such as:
6492
6493 (zero_extend:DI (subreg:SI (symbol_ref:DI "foo") 0))
6494
6495 The inner address should then be the symbol_ref, not the subreg,
6496 similarly to the plus case above.
6497
6498 In contrast, the subreg in:
6499
6500 (zero_extend:DI (subreg:SI (reg:DI R) 0))
6501
6502 should be treated as the base, since it should be replaced by
6503 an SImode hard register during register allocation. */
6504 loc = &SUBREG_REG (*loc);
6505 else
6506 return loc;
6507 if (outer_code)
6508 *outer_code = code;
6509 }
6510 }
6511
6512 /* Return true if CODE applies some kind of scale. The scaled value is
6513 is the first operand and the scale is the second. */
6514
6515 static bool
6516 binary_scale_code_p (enum rtx_code code)
6517 {
6518 return (code == MULT
6519 || code == ASHIFT
6520 /* Needed by ARM targets. */
6521 || code == ASHIFTRT
6522 || code == LSHIFTRT
6523 || code == ROTATE
6524 || code == ROTATERT);
6525 }
6526
6527 /* Return true if X appears to be a valid base or index term. */
6528 static bool
6529 valid_base_or_index_term_p (rtx x)
6530 {
6531 if (GET_CODE (x) == SCRATCH)
6532 return true;
6533 /* Handle what appear to be eliminated forms of a register. If we reach
6534 here, the elimination occurs outside of the outermost PLUS tree,
6535 and so the elimination offset cannot be treated as a displacement
6536 of the main address. Instead, we need to treat the whole PLUS as
6537 the base or index term. The address can only be made legitimate by
6538 reloading the PLUS. */
6539 if (GET_CODE (x) == PLUS && CONST_SCALAR_INT_P (XEXP (x, 1)))
6540 x = XEXP (x, 0);
6541 if (GET_CODE (x) == SUBREG)
6542 x = SUBREG_REG (x);
6543 return REG_P (x) || MEM_P (x);
6544 }
6545
6546 /* If *INNER can be interpreted as a base, return a pointer to the inner term
6547 (see address_info). Return null otherwise. */
6548
6549 static rtx *
6550 get_base_term (rtx *inner)
6551 {
6552 if (GET_CODE (*inner) == LO_SUM)
6553 inner = strip_address_mutations (&XEXP (*inner, 0));
6554 if (valid_base_or_index_term_p (*inner))
6555 return inner;
6556 return 0;
6557 }
6558
6559 /* If *INNER can be interpreted as an index, return a pointer to the inner term
6560 (see address_info). Return null otherwise. */
6561
6562 static rtx *
6563 get_index_term (rtx *inner)
6564 {
6565 /* At present, only constant scales are allowed. */
6566 if (binary_scale_code_p (GET_CODE (*inner)) && CONSTANT_P (XEXP (*inner, 1)))
6567 inner = strip_address_mutations (&XEXP (*inner, 0));
6568 if (valid_base_or_index_term_p (*inner))
6569 return inner;
6570 return 0;
6571 }
6572
6573 /* Set the segment part of address INFO to LOC, given that INNER is the
6574 unmutated value. */
6575
6576 static void
6577 set_address_segment (struct address_info *info, rtx *loc, rtx *inner)
6578 {
6579 gcc_assert (!info->segment);
6580 info->segment = loc;
6581 info->segment_term = inner;
6582 }
6583
6584 /* Set the base part of address INFO to LOC, given that INNER is the
6585 unmutated value. */
6586
6587 static void
6588 set_address_base (struct address_info *info, rtx *loc, rtx *inner)
6589 {
6590 gcc_assert (!info->base);
6591 info->base = loc;
6592 info->base_term = inner;
6593 }
6594
6595 /* Set the index part of address INFO to LOC, given that INNER is the
6596 unmutated value. */
6597
6598 static void
6599 set_address_index (struct address_info *info, rtx *loc, rtx *inner)
6600 {
6601 gcc_assert (!info->index);
6602 info->index = loc;
6603 info->index_term = inner;
6604 }
6605
6606 /* Set the displacement part of address INFO to LOC, given that INNER
6607 is the constant term. */
6608
6609 static void
6610 set_address_disp (struct address_info *info, rtx *loc, rtx *inner)
6611 {
6612 gcc_assert (!info->disp);
6613 info->disp = loc;
6614 info->disp_term = inner;
6615 }
6616
6617 /* INFO->INNER describes a {PRE,POST}_{INC,DEC} address. Set up the
6618 rest of INFO accordingly. */
6619
6620 static void
6621 decompose_incdec_address (struct address_info *info)
6622 {
6623 info->autoinc_p = true;
6624
6625 rtx *base = &XEXP (*info->inner, 0);
6626 set_address_base (info, base, base);
6627 gcc_checking_assert (info->base == info->base_term);
6628
6629 /* These addresses are only valid when the size of the addressed
6630 value is known. */
6631 gcc_checking_assert (info->mode != VOIDmode);
6632 }
6633
6634 /* INFO->INNER describes a {PRE,POST}_MODIFY address. Set up the rest
6635 of INFO accordingly. */
6636
6637 static void
6638 decompose_automod_address (struct address_info *info)
6639 {
6640 info->autoinc_p = true;
6641
6642 rtx *base = &XEXP (*info->inner, 0);
6643 set_address_base (info, base, base);
6644 gcc_checking_assert (info->base == info->base_term);
6645
6646 rtx plus = XEXP (*info->inner, 1);
6647 gcc_assert (GET_CODE (plus) == PLUS);
6648
6649 info->base_term2 = &XEXP (plus, 0);
6650 gcc_checking_assert (rtx_equal_p (*info->base_term, *info->base_term2));
6651
6652 rtx *step = &XEXP (plus, 1);
6653 rtx *inner_step = strip_address_mutations (step);
6654 if (CONSTANT_P (*inner_step))
6655 set_address_disp (info, step, inner_step);
6656 else
6657 set_address_index (info, step, inner_step);
6658 }
6659
6660 /* Treat *LOC as a tree of PLUS operands and store pointers to the summed
6661 values in [PTR, END). Return a pointer to the end of the used array. */
6662
6663 static rtx **
6664 extract_plus_operands (rtx *loc, rtx **ptr, rtx **end)
6665 {
6666 rtx x = *loc;
6667 if (GET_CODE (x) == PLUS)
6668 {
6669 ptr = extract_plus_operands (&XEXP (x, 0), ptr, end);
6670 ptr = extract_plus_operands (&XEXP (x, 1), ptr, end);
6671 }
6672 else
6673 {
6674 gcc_assert (ptr != end);
6675 *ptr++ = loc;
6676 }
6677 return ptr;
6678 }
6679
6680 /* Evaluate the likelihood of X being a base or index value, returning
6681 positive if it is likely to be a base, negative if it is likely to be
6682 an index, and 0 if we can't tell. Make the magnitude of the return
6683 value reflect the amount of confidence we have in the answer.
6684
6685 MODE, AS, OUTER_CODE and INDEX_CODE are as for ok_for_base_p_1. */
6686
6687 static int
6688 baseness (rtx x, machine_mode mode, addr_space_t as,
6689 enum rtx_code outer_code, enum rtx_code index_code)
6690 {
6691 /* Believe *_POINTER unless the address shape requires otherwise. */
6692 if (REG_P (x) && REG_POINTER (x))
6693 return 2;
6694 if (MEM_P (x) && MEM_POINTER (x))
6695 return 2;
6696
6697 if (REG_P (x) && HARD_REGISTER_P (x))
6698 {
6699 /* X is a hard register. If it only fits one of the base
6700 or index classes, choose that interpretation. */
6701 int regno = REGNO (x);
6702 bool base_p = ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
6703 bool index_p = REGNO_OK_FOR_INDEX_P (regno);
6704 if (base_p != index_p)
6705 return base_p ? 1 : -1;
6706 }
6707 return 0;
6708 }
6709
6710 /* INFO->INNER describes a normal, non-automodified address.
6711 Fill in the rest of INFO accordingly. */
6712
6713 static void
6714 decompose_normal_address (struct address_info *info)
6715 {
6716 /* Treat the address as the sum of up to four values. */
6717 rtx *ops[4];
6718 size_t n_ops = extract_plus_operands (info->inner, ops,
6719 ops + ARRAY_SIZE (ops)) - ops;
6720
6721 /* If there is more than one component, any base component is in a PLUS. */
6722 if (n_ops > 1)
6723 info->base_outer_code = PLUS;
6724
6725 /* Try to classify each sum operand now. Leave those that could be
6726 either a base or an index in OPS. */
6727 rtx *inner_ops[4];
6728 size_t out = 0;
6729 for (size_t in = 0; in < n_ops; ++in)
6730 {
6731 rtx *loc = ops[in];
6732 rtx *inner = strip_address_mutations (loc);
6733 if (CONSTANT_P (*inner))
6734 set_address_disp (info, loc, inner);
6735 else if (GET_CODE (*inner) == UNSPEC)
6736 set_address_segment (info, loc, inner);
6737 else
6738 {
6739 /* The only other possibilities are a base or an index. */
6740 rtx *base_term = get_base_term (inner);
6741 rtx *index_term = get_index_term (inner);
6742 gcc_assert (base_term || index_term);
6743 if (!base_term)
6744 set_address_index (info, loc, index_term);
6745 else if (!index_term)
6746 set_address_base (info, loc, base_term);
6747 else
6748 {
6749 gcc_assert (base_term == index_term);
6750 ops[out] = loc;
6751 inner_ops[out] = base_term;
6752 ++out;
6753 }
6754 }
6755 }
6756
6757 /* Classify the remaining OPS members as bases and indexes. */
6758 if (out == 1)
6759 {
6760 /* If we haven't seen a base or an index yet, assume that this is
6761 the base. If we were confident that another term was the base
6762 or index, treat the remaining operand as the other kind. */
6763 if (!info->base)
6764 set_address_base (info, ops[0], inner_ops[0]);
6765 else
6766 set_address_index (info, ops[0], inner_ops[0]);
6767 }
6768 else if (out == 2)
6769 {
6770 auto address_mode = targetm.addr_space.address_mode (info->as);
6771 rtx inner_op0 = *inner_ops[0];
6772 rtx inner_op1 = *inner_ops[1];
6773 int base;
6774 /* If one inner operand has the expected mode for a base and the other
6775 doesn't, assume that the other one is the index. This is useful
6776 for addresses such as:
6777
6778 (plus (zero_extend X) Y)
6779
6780 zero_extend is not in itself enough to assume an index, since bases
6781 can be zero-extended on POINTERS_EXTEND_UNSIGNED targets. But if
6782 Y has address mode and X doesn't, there should be little doubt that
6783 Y is the base. */
6784 if (GET_MODE (inner_op0) == address_mode
6785 && GET_MODE (inner_op1) != address_mode)
6786 base = 0;
6787 else if (GET_MODE (inner_op1) == address_mode
6788 && GET_MODE (inner_op0) != address_mode)
6789 base = 1;
6790 /* In the event of a tie, assume the base comes first. */
6791 else if (baseness (inner_op0, info->mode, info->as, PLUS,
6792 GET_CODE (*ops[1]))
6793 >= baseness (inner_op1, info->mode, info->as, PLUS,
6794 GET_CODE (*ops[0])))
6795 base = 0;
6796 else
6797 base = 1;
6798 set_address_base (info, ops[base], inner_ops[base]);
6799 set_address_index (info, ops[1 - base], inner_ops[1 - base]);
6800 }
6801 else
6802 gcc_assert (out == 0);
6803 }
6804
6805 /* Describe address *LOC in *INFO. MODE is the mode of the addressed value,
6806 or VOIDmode if not known. AS is the address space associated with LOC.
6807 OUTER_CODE is MEM if *LOC is a MEM address and ADDRESS otherwise. */
6808
6809 void
6810 decompose_address (struct address_info *info, rtx *loc, machine_mode mode,
6811 addr_space_t as, enum rtx_code outer_code)
6812 {
6813 memset (info, 0, sizeof (*info));
6814 info->mode = mode;
6815 info->as = as;
6816 info->addr_outer_code = outer_code;
6817 info->outer = loc;
6818 info->inner = strip_address_mutations (loc, &outer_code);
6819 info->base_outer_code = outer_code;
6820 switch (GET_CODE (*info->inner))
6821 {
6822 case PRE_DEC:
6823 case PRE_INC:
6824 case POST_DEC:
6825 case POST_INC:
6826 decompose_incdec_address (info);
6827 break;
6828
6829 case PRE_MODIFY:
6830 case POST_MODIFY:
6831 decompose_automod_address (info);
6832 break;
6833
6834 default:
6835 decompose_normal_address (info);
6836 break;
6837 }
6838 }
6839
6840 /* Describe address operand LOC in INFO. */
6841
6842 void
6843 decompose_lea_address (struct address_info *info, rtx *loc)
6844 {
6845 decompose_address (info, loc, VOIDmode, ADDR_SPACE_GENERIC, ADDRESS);
6846 }
6847
6848 /* Describe the address of MEM X in INFO. */
6849
6850 void
6851 decompose_mem_address (struct address_info *info, rtx x)
6852 {
6853 gcc_assert (MEM_P (x));
6854 decompose_address (info, &XEXP (x, 0), GET_MODE (x),
6855 MEM_ADDR_SPACE (x), MEM);
6856 }
6857
6858 /* Update INFO after a change to the address it describes. */
6859
6860 void
6861 update_address (struct address_info *info)
6862 {
6863 decompose_address (info, info->outer, info->mode, info->as,
6864 info->addr_outer_code);
6865 }
6866
6867 /* Return the scale applied to *INFO->INDEX_TERM, or 0 if the index is
6868 more complicated than that. */
6869
6870 HOST_WIDE_INT
6871 get_index_scale (const struct address_info *info)
6872 {
6873 rtx index = *info->index;
6874 if (GET_CODE (index) == MULT
6875 && CONST_INT_P (XEXP (index, 1))
6876 && info->index_term == &XEXP (index, 0))
6877 return INTVAL (XEXP (index, 1));
6878
6879 if (GET_CODE (index) == ASHIFT
6880 && CONST_INT_P (XEXP (index, 1))
6881 && info->index_term == &XEXP (index, 0))
6882 return HOST_WIDE_INT_1 << INTVAL (XEXP (index, 1));
6883
6884 if (info->index == info->index_term)
6885 return 1;
6886
6887 return 0;
6888 }
6889
6890 /* Return the "index code" of INFO, in the form required by
6891 ok_for_base_p_1. */
6892
6893 enum rtx_code
6894 get_index_code (const struct address_info *info)
6895 {
6896 if (info->index)
6897 return GET_CODE (*info->index);
6898
6899 if (info->disp)
6900 return GET_CODE (*info->disp);
6901
6902 return SCRATCH;
6903 }
6904
6905 /* Return true if RTL X contains a SYMBOL_REF. */
6906
6907 bool
6908 contains_symbol_ref_p (const_rtx x)
6909 {
6910 subrtx_iterator::array_type array;
6911 FOR_EACH_SUBRTX (iter, array, x, ALL)
6912 if (SYMBOL_REF_P (*iter))
6913 return true;
6914
6915 return false;
6916 }
6917
6918 /* Return true if RTL X contains a SYMBOL_REF or LABEL_REF. */
6919
6920 bool
6921 contains_symbolic_reference_p (const_rtx x)
6922 {
6923 subrtx_iterator::array_type array;
6924 FOR_EACH_SUBRTX (iter, array, x, ALL)
6925 if (SYMBOL_REF_P (*iter) || GET_CODE (*iter) == LABEL_REF)
6926 return true;
6927
6928 return false;
6929 }
6930
6931 /* Return true if RTL X contains a constant pool address. */
6932
6933 bool
6934 contains_constant_pool_address_p (const_rtx x)
6935 {
6936 subrtx_iterator::array_type array;
6937 FOR_EACH_SUBRTX (iter, array, x, ALL)
6938 if (SYMBOL_REF_P (*iter) && CONSTANT_POOL_ADDRESS_P (*iter))
6939 return true;
6940
6941 return false;
6942 }
6943
6944
6945 /* Return true if X contains a thread-local symbol. */
6946
6947 bool
6948 tls_referenced_p (const_rtx x)
6949 {
6950 if (!targetm.have_tls)
6951 return false;
6952
6953 subrtx_iterator::array_type array;
6954 FOR_EACH_SUBRTX (iter, array, x, ALL)
6955 if (GET_CODE (*iter) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (*iter) != 0)
6956 return true;
6957 return false;
6958 }
6959
6960 /* Process recursively X of INSN and add REG_INC notes if necessary. */
6961 void
6962 add_auto_inc_notes (rtx_insn *insn, rtx x)
6963 {
6964 enum rtx_code code = GET_CODE (x);
6965 const char *fmt;
6966 int i, j;
6967
6968 if (code == MEM && auto_inc_p (XEXP (x, 0)))
6969 {
6970 add_reg_note (insn, REG_INC, XEXP (XEXP (x, 0), 0));
6971 return;
6972 }
6973
6974 /* Scan all X sub-expressions. */
6975 fmt = GET_RTX_FORMAT (code);
6976 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6977 {
6978 if (fmt[i] == 'e')
6979 add_auto_inc_notes (insn, XEXP (x, i));
6980 else if (fmt[i] == 'E')
6981 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6982 add_auto_inc_notes (insn, XVECEXP (x, i, j));
6983 }
6984 }
6985
6986 /* Return true if X is register asm. */
6987
6988 bool
6989 register_asm_p (const_rtx x)
6990 {
6991 return (REG_P (x)
6992 && REG_EXPR (x) != NULL_TREE
6993 && HAS_DECL_ASSEMBLER_NAME_P (REG_EXPR (x))
6994 && DECL_ASSEMBLER_NAME_SET_P (REG_EXPR (x))
6995 && DECL_REGISTER (REG_EXPR (x)));
6996 }
6997
6998 /* Return true if, for all OP of mode OP_MODE:
6999
7000 (vec_select:RESULT_MODE OP SEL)
7001
7002 is equivalent to the highpart RESULT_MODE of OP. */
7003
7004 bool
7005 vec_series_highpart_p (machine_mode result_mode, machine_mode op_mode, rtx sel)
7006 {
7007 int nunits;
7008 if (GET_MODE_NUNITS (op_mode).is_constant (&nunits)
7009 && targetm.can_change_mode_class (op_mode, result_mode, ALL_REGS))
7010 {
7011 int offset = BYTES_BIG_ENDIAN ? 0 : nunits - XVECLEN (sel, 0);
7012 return rtvec_series_p (XVEC (sel, 0), offset);
7013 }
7014 return false;
7015 }
7016
7017 /* Return true if, for all OP of mode OP_MODE:
7018
7019 (vec_select:RESULT_MODE OP SEL)
7020
7021 is equivalent to the lowpart RESULT_MODE of OP. */
7022
7023 bool
7024 vec_series_lowpart_p (machine_mode result_mode, machine_mode op_mode, rtx sel)
7025 {
7026 int nunits;
7027 if (GET_MODE_NUNITS (op_mode).is_constant (&nunits)
7028 && targetm.can_change_mode_class (op_mode, result_mode, ALL_REGS))
7029 {
7030 int offset = BYTES_BIG_ENDIAN ? nunits - XVECLEN (sel, 0) : 0;
7031 return rtvec_series_p (XVEC (sel, 0), offset);
7032 }
7033 return false;
7034 }
7035
7036 /* Return true if X contains a paradoxical subreg. */
7037
7038 bool
7039 contains_paradoxical_subreg_p (rtx x)
7040 {
7041 subrtx_var_iterator::array_type array;
7042 FOR_EACH_SUBRTX_VAR (iter, array, x, NONCONST)
7043 {
7044 x = *iter;
7045 if (SUBREG_P (x) && paradoxical_subreg_p (x))
7046 return true;
7047 }
7048 return false;
7049 }