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