Daily bump.
[official-gcc.git] / gcc / resource.c
bloba8b449fd304fc11e785dc9350309a06de6909ab4
1 /* Definitions for computing resource usage of specific insns.
2 Copyright (C) 1999-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "backend.h"
24 #include "rtl.h"
25 #include "df.h"
26 #include "diagnostic-core.h"
27 #include "tm_p.h"
28 #include "regs.h"
29 #include "flags.h"
30 #include "output.h"
31 #include "resource.h"
32 #include "except.h"
33 #include "insn-attr.h"
34 #include "params.h"
35 #include "emit-rtl.h"
37 /* This structure is used to record liveness information at the targets or
38 fallthrough insns of branches. We will most likely need the information
39 at targets again, so save them in a hash table rather than recomputing them
40 each time. */
42 struct target_info
44 int uid; /* INSN_UID of target. */
45 struct target_info *next; /* Next info for same hash bucket. */
46 HARD_REG_SET live_regs; /* Registers live at target. */
47 int block; /* Basic block number containing target. */
48 int bb_tick; /* Generation count of basic block info. */
51 #define TARGET_HASH_PRIME 257
53 /* Indicates what resources are required at the beginning of the epilogue. */
54 static struct resources start_of_epilogue_needs;
56 /* Indicates what resources are required at function end. */
57 static struct resources end_of_function_needs;
59 /* Define the hash table itself. */
60 static struct target_info **target_hash_table = NULL;
62 /* For each basic block, we maintain a generation number of its basic
63 block info, which is updated each time we move an insn from the
64 target of a jump. This is the generation number indexed by block
65 number. */
67 static int *bb_ticks;
69 /* Marks registers possibly live at the current place being scanned by
70 mark_target_live_regs. Also used by update_live_status. */
72 static HARD_REG_SET current_live_regs;
74 /* Marks registers for which we have seen a REG_DEAD note but no assignment.
75 Also only used by the next two functions. */
77 static HARD_REG_SET pending_dead_regs;
79 static void update_live_status (rtx, const_rtx, void *);
80 static int find_basic_block (rtx_insn *, int);
81 static rtx_insn *next_insn_no_annul (rtx_insn *);
82 static rtx_insn *find_dead_or_set_registers (rtx_insn *, struct resources*,
83 rtx *, int, struct resources,
84 struct resources);
86 /* Utility function called from mark_target_live_regs via note_stores.
87 It deadens any CLOBBERed registers and livens any SET registers. */
89 static void
90 update_live_status (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
92 int first_regno, last_regno;
93 int i;
95 if (!REG_P (dest)
96 && (GET_CODE (dest) != SUBREG || !REG_P (SUBREG_REG (dest))))
97 return;
99 if (GET_CODE (dest) == SUBREG)
101 first_regno = subreg_regno (dest);
102 last_regno = first_regno + subreg_nregs (dest);
105 else
107 first_regno = REGNO (dest);
108 last_regno = END_REGNO (dest);
111 if (GET_CODE (x) == CLOBBER)
112 for (i = first_regno; i < last_regno; i++)
113 CLEAR_HARD_REG_BIT (current_live_regs, i);
114 else
115 for (i = first_regno; i < last_regno; i++)
117 SET_HARD_REG_BIT (current_live_regs, i);
118 CLEAR_HARD_REG_BIT (pending_dead_regs, i);
122 /* Find the number of the basic block with correct live register
123 information that starts closest to INSN. Return -1 if we couldn't
124 find such a basic block or the beginning is more than
125 SEARCH_LIMIT instructions before INSN. Use SEARCH_LIMIT = -1 for
126 an unlimited search.
128 The delay slot filling code destroys the control-flow graph so,
129 instead of finding the basic block containing INSN, we search
130 backwards toward a BARRIER where the live register information is
131 correct. */
133 static int
134 find_basic_block (rtx_insn *insn, int search_limit)
136 /* Scan backwards to the previous BARRIER. Then see if we can find a
137 label that starts a basic block. Return the basic block number. */
138 for (insn = prev_nonnote_insn (insn);
139 insn && !BARRIER_P (insn) && search_limit != 0;
140 insn = prev_nonnote_insn (insn), --search_limit)
143 /* The closest BARRIER is too far away. */
144 if (search_limit == 0)
145 return -1;
147 /* The start of the function. */
148 else if (insn == 0)
149 return ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->index;
151 /* See if any of the upcoming CODE_LABELs start a basic block. If we reach
152 anything other than a CODE_LABEL or note, we can't find this code. */
153 for (insn = next_nonnote_insn (insn);
154 insn && LABEL_P (insn);
155 insn = next_nonnote_insn (insn))
156 if (BLOCK_FOR_INSN (insn))
157 return BLOCK_FOR_INSN (insn)->index;
159 return -1;
162 /* Similar to next_insn, but ignores insns in the delay slots of
163 an annulled branch. */
165 static rtx_insn *
166 next_insn_no_annul (rtx_insn *insn)
168 if (insn)
170 /* If INSN is an annulled branch, skip any insns from the target
171 of the branch. */
172 if (JUMP_P (insn)
173 && INSN_ANNULLED_BRANCH_P (insn)
174 && NEXT_INSN (PREV_INSN (insn)) != insn)
176 rtx_insn *next = NEXT_INSN (insn);
178 while ((NONJUMP_INSN_P (next) || JUMP_P (next) || CALL_P (next))
179 && INSN_FROM_TARGET_P (next))
181 insn = next;
182 next = NEXT_INSN (insn);
186 insn = NEXT_INSN (insn);
187 if (insn && NONJUMP_INSN_P (insn)
188 && GET_CODE (PATTERN (insn)) == SEQUENCE)
189 insn = as_a <rtx_sequence *> (PATTERN (insn))->insn (0);
192 return insn;
195 /* Given X, some rtl, and RES, a pointer to a `struct resource', mark
196 which resources are referenced by the insn. If INCLUDE_DELAYED_EFFECTS
197 is TRUE, resources used by the called routine will be included for
198 CALL_INSNs. */
200 void
201 mark_referenced_resources (rtx x, struct resources *res,
202 bool include_delayed_effects)
204 enum rtx_code code = GET_CODE (x);
205 int i, j;
206 unsigned int r;
207 const char *format_ptr;
209 /* Handle leaf items for which we set resource flags. Also, special-case
210 CALL, SET and CLOBBER operators. */
211 switch (code)
213 case CONST:
214 CASE_CONST_ANY:
215 case PC:
216 case SYMBOL_REF:
217 case LABEL_REF:
218 return;
220 case SUBREG:
221 if (!REG_P (SUBREG_REG (x)))
222 mark_referenced_resources (SUBREG_REG (x), res, false);
223 else
225 unsigned int regno = subreg_regno (x);
226 unsigned int last_regno = regno + subreg_nregs (x);
228 gcc_assert (last_regno <= FIRST_PSEUDO_REGISTER);
229 for (r = regno; r < last_regno; r++)
230 SET_HARD_REG_BIT (res->regs, r);
232 return;
234 case REG:
235 gcc_assert (HARD_REGISTER_P (x));
236 add_to_hard_reg_set (&res->regs, GET_MODE (x), REGNO (x));
237 return;
239 case MEM:
240 /* If this memory shouldn't change, it really isn't referencing
241 memory. */
242 if (! MEM_READONLY_P (x))
243 res->memory = 1;
244 res->volatil |= MEM_VOLATILE_P (x);
246 /* Mark registers used to access memory. */
247 mark_referenced_resources (XEXP (x, 0), res, false);
248 return;
250 case CC0:
251 res->cc = 1;
252 return;
254 case UNSPEC_VOLATILE:
255 case TRAP_IF:
256 case ASM_INPUT:
257 /* Traditional asm's are always volatile. */
258 res->volatil = 1;
259 break;
261 case ASM_OPERANDS:
262 res->volatil |= MEM_VOLATILE_P (x);
264 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
265 We can not just fall through here since then we would be confused
266 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
267 traditional asms unlike their normal usage. */
269 for (i = 0; i < ASM_OPERANDS_INPUT_LENGTH (x); i++)
270 mark_referenced_resources (ASM_OPERANDS_INPUT (x, i), res, false);
271 return;
273 case CALL:
274 /* The first operand will be a (MEM (xxx)) but doesn't really reference
275 memory. The second operand may be referenced, though. */
276 mark_referenced_resources (XEXP (XEXP (x, 0), 0), res, false);
277 mark_referenced_resources (XEXP (x, 1), res, false);
278 return;
280 case SET:
281 /* Usually, the first operand of SET is set, not referenced. But
282 registers used to access memory are referenced. SET_DEST is
283 also referenced if it is a ZERO_EXTRACT. */
285 mark_referenced_resources (SET_SRC (x), res, false);
287 x = SET_DEST (x);
288 if (GET_CODE (x) == ZERO_EXTRACT
289 || GET_CODE (x) == STRICT_LOW_PART)
290 mark_referenced_resources (x, res, false);
291 else if (GET_CODE (x) == SUBREG)
292 x = SUBREG_REG (x);
293 if (MEM_P (x))
294 mark_referenced_resources (XEXP (x, 0), res, false);
295 return;
297 case CLOBBER:
298 return;
300 case CALL_INSN:
301 if (include_delayed_effects)
303 /* A CALL references memory, the frame pointer if it exists, the
304 stack pointer, any global registers and any registers given in
305 USE insns immediately in front of the CALL.
307 However, we may have moved some of the parameter loading insns
308 into the delay slot of this CALL. If so, the USE's for them
309 don't count and should be skipped. */
310 rtx_insn *insn = PREV_INSN (as_a <rtx_insn *> (x));
311 rtx_sequence *sequence = 0;
312 int seq_size = 0;
313 int i;
315 /* If we are part of a delay slot sequence, point at the SEQUENCE. */
316 if (NEXT_INSN (insn) != x)
318 sequence = as_a <rtx_sequence *> (PATTERN (NEXT_INSN (insn)));
319 seq_size = sequence->len ();
320 gcc_assert (GET_CODE (sequence) == SEQUENCE);
323 res->memory = 1;
324 SET_HARD_REG_BIT (res->regs, STACK_POINTER_REGNUM);
325 if (frame_pointer_needed)
327 SET_HARD_REG_BIT (res->regs, FRAME_POINTER_REGNUM);
328 if (!HARD_FRAME_POINTER_IS_FRAME_POINTER)
329 SET_HARD_REG_BIT (res->regs, HARD_FRAME_POINTER_REGNUM);
332 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
333 if (global_regs[i])
334 SET_HARD_REG_BIT (res->regs, i);
336 /* Check for a REG_SETJMP. If it exists, then we must
337 assume that this call can need any register.
339 This is done to be more conservative about how we handle setjmp.
340 We assume that they both use and set all registers. Using all
341 registers ensures that a register will not be considered dead
342 just because it crosses a setjmp call. A register should be
343 considered dead only if the setjmp call returns nonzero. */
344 if (find_reg_note (x, REG_SETJMP, NULL))
345 SET_HARD_REG_SET (res->regs);
348 rtx link;
350 for (link = CALL_INSN_FUNCTION_USAGE (x);
351 link;
352 link = XEXP (link, 1))
353 if (GET_CODE (XEXP (link, 0)) == USE)
355 for (i = 1; i < seq_size; i++)
357 rtx slot_pat = PATTERN (sequence->element (i));
358 if (GET_CODE (slot_pat) == SET
359 && rtx_equal_p (SET_DEST (slot_pat),
360 XEXP (XEXP (link, 0), 0)))
361 break;
363 if (i >= seq_size)
364 mark_referenced_resources (XEXP (XEXP (link, 0), 0),
365 res, false);
370 /* ... fall through to other INSN processing ... */
372 case INSN:
373 case JUMP_INSN:
375 if (GET_CODE (PATTERN (x)) == COND_EXEC)
376 /* In addition to the usual references, also consider all outputs
377 as referenced, to compensate for mark_set_resources treating
378 them as killed. This is similar to ZERO_EXTRACT / STRICT_LOW_PART
379 handling, execpt that we got a partial incidence instead of a partial
380 width. */
381 mark_set_resources (x, res, 0,
382 include_delayed_effects
383 ? MARK_SRC_DEST_CALL : MARK_SRC_DEST);
385 if (! include_delayed_effects
386 && INSN_REFERENCES_ARE_DELAYED (as_a <rtx_insn *> (x)))
387 return;
389 /* No special processing, just speed up. */
390 mark_referenced_resources (PATTERN (x), res, include_delayed_effects);
391 return;
393 default:
394 break;
397 /* Process each sub-expression and flag what it needs. */
398 format_ptr = GET_RTX_FORMAT (code);
399 for (i = 0; i < GET_RTX_LENGTH (code); i++)
400 switch (*format_ptr++)
402 case 'e':
403 mark_referenced_resources (XEXP (x, i), res, include_delayed_effects);
404 break;
406 case 'E':
407 for (j = 0; j < XVECLEN (x, i); j++)
408 mark_referenced_resources (XVECEXP (x, i, j), res,
409 include_delayed_effects);
410 break;
414 /* A subroutine of mark_target_live_regs. Search forward from TARGET
415 looking for registers that are set before they are used. These are dead.
416 Stop after passing a few conditional jumps, and/or a small
417 number of unconditional branches. */
419 static rtx_insn *
420 find_dead_or_set_registers (rtx_insn *target, struct resources *res,
421 rtx *jump_target, int jump_count,
422 struct resources set, struct resources needed)
424 HARD_REG_SET scratch;
425 rtx_insn *insn;
426 rtx_insn *next_insn;
427 rtx_insn *jump_insn = 0;
428 int i;
430 for (insn = target; insn; insn = next_insn)
432 rtx_insn *this_insn = insn;
434 next_insn = NEXT_INSN (insn);
436 /* If this instruction can throw an exception, then we don't
437 know where we might end up next. That means that we have to
438 assume that whatever we have already marked as live really is
439 live. */
440 if (can_throw_internal (insn))
441 break;
443 switch (GET_CODE (insn))
445 case CODE_LABEL:
446 /* After a label, any pending dead registers that weren't yet
447 used can be made dead. */
448 AND_COMPL_HARD_REG_SET (pending_dead_regs, needed.regs);
449 AND_COMPL_HARD_REG_SET (res->regs, pending_dead_regs);
450 CLEAR_HARD_REG_SET (pending_dead_regs);
452 continue;
454 case BARRIER:
455 case NOTE:
456 continue;
458 case INSN:
459 if (GET_CODE (PATTERN (insn)) == USE)
461 /* If INSN is a USE made by update_block, we care about the
462 underlying insn. Any registers set by the underlying insn
463 are live since the insn is being done somewhere else. */
464 if (INSN_P (XEXP (PATTERN (insn), 0)))
465 mark_set_resources (XEXP (PATTERN (insn), 0), res, 0,
466 MARK_SRC_DEST_CALL);
468 /* All other USE insns are to be ignored. */
469 continue;
471 else if (GET_CODE (PATTERN (insn)) == CLOBBER)
472 continue;
473 else if (rtx_sequence *seq =
474 dyn_cast <rtx_sequence *> (PATTERN (insn)))
476 /* An unconditional jump can be used to fill the delay slot
477 of a call, so search for a JUMP_INSN in any position. */
478 for (i = 0; i < seq->len (); i++)
480 this_insn = seq->insn (i);
481 if (JUMP_P (this_insn))
482 break;
486 default:
487 break;
490 if (rtx_jump_insn *this_jump_insn =
491 dyn_cast <rtx_jump_insn *> (this_insn))
493 if (jump_count++ < 10)
495 if (any_uncondjump_p (this_jump_insn)
496 || ANY_RETURN_P (PATTERN (this_jump_insn)))
498 rtx lab_or_return = this_jump_insn->jump_label ();
499 if (ANY_RETURN_P (lab_or_return))
500 next_insn = NULL;
501 else
502 next_insn = as_a <rtx_insn *> (lab_or_return);
503 if (jump_insn == 0)
505 jump_insn = insn;
506 if (jump_target)
507 *jump_target = JUMP_LABEL (this_jump_insn);
510 else if (any_condjump_p (this_jump_insn))
512 struct resources target_set, target_res;
513 struct resources fallthrough_res;
515 /* We can handle conditional branches here by following
516 both paths, and then IOR the results of the two paths
517 together, which will give us registers that are dead
518 on both paths. Since this is expensive, we give it
519 a much higher cost than unconditional branches. The
520 cost was chosen so that we will follow at most 1
521 conditional branch. */
523 jump_count += 4;
524 if (jump_count >= 10)
525 break;
527 mark_referenced_resources (insn, &needed, true);
529 /* For an annulled branch, mark_set_resources ignores slots
530 filled by instructions from the target. This is correct
531 if the branch is not taken. Since we are following both
532 paths from the branch, we must also compute correct info
533 if the branch is taken. We do this by inverting all of
534 the INSN_FROM_TARGET_P bits, calling mark_set_resources,
535 and then inverting the INSN_FROM_TARGET_P bits again. */
537 if (GET_CODE (PATTERN (insn)) == SEQUENCE
538 && INSN_ANNULLED_BRANCH_P (this_jump_insn))
540 rtx_sequence *seq = as_a <rtx_sequence *> (PATTERN (insn));
541 for (i = 1; i < seq->len (); i++)
542 INSN_FROM_TARGET_P (seq->element (i))
543 = ! INSN_FROM_TARGET_P (seq->element (i));
545 target_set = set;
546 mark_set_resources (insn, &target_set, 0,
547 MARK_SRC_DEST_CALL);
549 for (i = 1; i < seq->len (); i++)
550 INSN_FROM_TARGET_P (seq->element (i))
551 = ! INSN_FROM_TARGET_P (seq->element (i));
553 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
555 else
557 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
558 target_set = set;
561 target_res = *res;
562 COPY_HARD_REG_SET (scratch, target_set.regs);
563 AND_COMPL_HARD_REG_SET (scratch, needed.regs);
564 AND_COMPL_HARD_REG_SET (target_res.regs, scratch);
566 fallthrough_res = *res;
567 COPY_HARD_REG_SET (scratch, set.regs);
568 AND_COMPL_HARD_REG_SET (scratch, needed.regs);
569 AND_COMPL_HARD_REG_SET (fallthrough_res.regs, scratch);
571 if (!ANY_RETURN_P (this_jump_insn->jump_label ()))
572 find_dead_or_set_registers
573 (this_jump_insn->jump_target (),
574 &target_res, 0, jump_count, target_set, needed);
575 find_dead_or_set_registers (next_insn,
576 &fallthrough_res, 0, jump_count,
577 set, needed);
578 IOR_HARD_REG_SET (fallthrough_res.regs, target_res.regs);
579 AND_HARD_REG_SET (res->regs, fallthrough_res.regs);
580 break;
582 else
583 break;
585 else
587 /* Don't try this optimization if we expired our jump count
588 above, since that would mean there may be an infinite loop
589 in the function being compiled. */
590 jump_insn = 0;
591 break;
595 mark_referenced_resources (insn, &needed, true);
596 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
598 COPY_HARD_REG_SET (scratch, set.regs);
599 AND_COMPL_HARD_REG_SET (scratch, needed.regs);
600 AND_COMPL_HARD_REG_SET (res->regs, scratch);
603 return jump_insn;
606 /* Given X, a part of an insn, and a pointer to a `struct resource',
607 RES, indicate which resources are modified by the insn. If
608 MARK_TYPE is MARK_SRC_DEST_CALL, also mark resources potentially
609 set by the called routine.
611 If IN_DEST is nonzero, it means we are inside a SET. Otherwise,
612 objects are being referenced instead of set.
614 We never mark the insn as modifying the condition code unless it explicitly
615 SETs CC0 even though this is not totally correct. The reason for this is
616 that we require a SET of CC0 to immediately precede the reference to CC0.
617 So if some other insn sets CC0 as a side-effect, we know it cannot affect
618 our computation and thus may be placed in a delay slot. */
620 void
621 mark_set_resources (rtx x, struct resources *res, int in_dest,
622 enum mark_resource_type mark_type)
624 enum rtx_code code;
625 int i, j;
626 unsigned int r;
627 const char *format_ptr;
629 restart:
631 code = GET_CODE (x);
633 switch (code)
635 case NOTE:
636 case BARRIER:
637 case CODE_LABEL:
638 case USE:
639 CASE_CONST_ANY:
640 case LABEL_REF:
641 case SYMBOL_REF:
642 case CONST:
643 case PC:
644 /* These don't set any resources. */
645 return;
647 case CC0:
648 if (in_dest)
649 res->cc = 1;
650 return;
652 case CALL_INSN:
653 /* Called routine modifies the condition code, memory, any registers
654 that aren't saved across calls, global registers and anything
655 explicitly CLOBBERed immediately after the CALL_INSN. */
657 if (mark_type == MARK_SRC_DEST_CALL)
659 rtx_call_insn *call_insn = as_a <rtx_call_insn *> (x);
660 rtx link;
661 HARD_REG_SET regs;
663 res->cc = res->memory = 1;
665 get_call_reg_set_usage (call_insn, &regs, regs_invalidated_by_call);
666 IOR_HARD_REG_SET (res->regs, regs);
668 for (link = CALL_INSN_FUNCTION_USAGE (call_insn);
669 link; link = XEXP (link, 1))
670 if (GET_CODE (XEXP (link, 0)) == CLOBBER)
671 mark_set_resources (SET_DEST (XEXP (link, 0)), res, 1,
672 MARK_SRC_DEST);
674 /* Check for a REG_SETJMP. If it exists, then we must
675 assume that this call can clobber any register. */
676 if (find_reg_note (call_insn, REG_SETJMP, NULL))
677 SET_HARD_REG_SET (res->regs);
680 /* ... and also what its RTL says it modifies, if anything. */
682 case JUMP_INSN:
683 case INSN:
685 /* An insn consisting of just a CLOBBER (or USE) is just for flow
686 and doesn't actually do anything, so we ignore it. */
688 if (mark_type != MARK_SRC_DEST_CALL
689 && INSN_SETS_ARE_DELAYED (as_a <rtx_insn *> (x)))
690 return;
692 x = PATTERN (x);
693 if (GET_CODE (x) != USE && GET_CODE (x) != CLOBBER)
694 goto restart;
695 return;
697 case SET:
698 /* If the source of a SET is a CALL, this is actually done by
699 the called routine. So only include it if we are to include the
700 effects of the calling routine. */
702 mark_set_resources (SET_DEST (x), res,
703 (mark_type == MARK_SRC_DEST_CALL
704 || GET_CODE (SET_SRC (x)) != CALL),
705 mark_type);
707 mark_set_resources (SET_SRC (x), res, 0, MARK_SRC_DEST);
708 return;
710 case CLOBBER:
711 mark_set_resources (XEXP (x, 0), res, 1, MARK_SRC_DEST);
712 return;
714 case SEQUENCE:
716 rtx_sequence *seq = as_a <rtx_sequence *> (x);
717 rtx control = seq->element (0);
718 bool annul_p = JUMP_P (control) && INSN_ANNULLED_BRANCH_P (control);
720 mark_set_resources (control, res, 0, mark_type);
721 for (i = seq->len () - 1; i >= 0; --i)
723 rtx elt = seq->element (i);
724 if (!annul_p && INSN_FROM_TARGET_P (elt))
725 mark_set_resources (elt, res, 0, mark_type);
728 return;
730 case POST_INC:
731 case PRE_INC:
732 case POST_DEC:
733 case PRE_DEC:
734 mark_set_resources (XEXP (x, 0), res, 1, MARK_SRC_DEST);
735 return;
737 case PRE_MODIFY:
738 case POST_MODIFY:
739 mark_set_resources (XEXP (x, 0), res, 1, MARK_SRC_DEST);
740 mark_set_resources (XEXP (XEXP (x, 1), 0), res, 0, MARK_SRC_DEST);
741 mark_set_resources (XEXP (XEXP (x, 1), 1), res, 0, MARK_SRC_DEST);
742 return;
744 case SIGN_EXTRACT:
745 case ZERO_EXTRACT:
746 mark_set_resources (XEXP (x, 0), res, in_dest, MARK_SRC_DEST);
747 mark_set_resources (XEXP (x, 1), res, 0, MARK_SRC_DEST);
748 mark_set_resources (XEXP (x, 2), res, 0, MARK_SRC_DEST);
749 return;
751 case MEM:
752 if (in_dest)
754 res->memory = 1;
755 res->volatil |= MEM_VOLATILE_P (x);
758 mark_set_resources (XEXP (x, 0), res, 0, MARK_SRC_DEST);
759 return;
761 case SUBREG:
762 if (in_dest)
764 if (!REG_P (SUBREG_REG (x)))
765 mark_set_resources (SUBREG_REG (x), res, in_dest, mark_type);
766 else
768 unsigned int regno = subreg_regno (x);
769 unsigned int last_regno = regno + subreg_nregs (x);
771 gcc_assert (last_regno <= FIRST_PSEUDO_REGISTER);
772 for (r = regno; r < last_regno; r++)
773 SET_HARD_REG_BIT (res->regs, r);
776 return;
778 case REG:
779 if (in_dest)
781 gcc_assert (HARD_REGISTER_P (x));
782 add_to_hard_reg_set (&res->regs, GET_MODE (x), REGNO (x));
784 return;
786 case UNSPEC_VOLATILE:
787 case ASM_INPUT:
788 /* Traditional asm's are always volatile. */
789 res->volatil = 1;
790 return;
792 case TRAP_IF:
793 res->volatil = 1;
794 break;
796 case ASM_OPERANDS:
797 res->volatil |= MEM_VOLATILE_P (x);
799 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
800 We can not just fall through here since then we would be confused
801 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
802 traditional asms unlike their normal usage. */
804 for (i = 0; i < ASM_OPERANDS_INPUT_LENGTH (x); i++)
805 mark_set_resources (ASM_OPERANDS_INPUT (x, i), res, in_dest,
806 MARK_SRC_DEST);
807 return;
809 default:
810 break;
813 /* Process each sub-expression and flag what it needs. */
814 format_ptr = GET_RTX_FORMAT (code);
815 for (i = 0; i < GET_RTX_LENGTH (code); i++)
816 switch (*format_ptr++)
818 case 'e':
819 mark_set_resources (XEXP (x, i), res, in_dest, mark_type);
820 break;
822 case 'E':
823 for (j = 0; j < XVECLEN (x, i); j++)
824 mark_set_resources (XVECEXP (x, i, j), res, in_dest, mark_type);
825 break;
829 /* Return TRUE if INSN is a return, possibly with a filled delay slot. */
831 static bool
832 return_insn_p (const_rtx insn)
834 if (JUMP_P (insn) && ANY_RETURN_P (PATTERN (insn)))
835 return true;
837 if (NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == SEQUENCE)
838 return return_insn_p (XVECEXP (PATTERN (insn), 0, 0));
840 return false;
843 /* Set the resources that are live at TARGET.
845 If TARGET is zero, we refer to the end of the current function and can
846 return our precomputed value.
848 Otherwise, we try to find out what is live by consulting the basic block
849 information. This is tricky, because we must consider the actions of
850 reload and jump optimization, which occur after the basic block information
851 has been computed.
853 Accordingly, we proceed as follows::
855 We find the previous BARRIER and look at all immediately following labels
856 (with no intervening active insns) to see if any of them start a basic
857 block. If we hit the start of the function first, we use block 0.
859 Once we have found a basic block and a corresponding first insn, we can
860 accurately compute the live status (by starting at a label following a
861 BARRIER, we are immune to actions taken by reload and jump.) Then we
862 scan all insns between that point and our target. For each CLOBBER (or
863 for call-clobbered regs when we pass a CALL_INSN), mark the appropriate
864 registers are dead. For a SET, mark them as live.
866 We have to be careful when using REG_DEAD notes because they are not
867 updated by such things as find_equiv_reg. So keep track of registers
868 marked as dead that haven't been assigned to, and mark them dead at the
869 next CODE_LABEL since reload and jump won't propagate values across labels.
871 If we cannot find the start of a basic block (should be a very rare
872 case, if it can happen at all), mark everything as potentially live.
874 Next, scan forward from TARGET looking for things set or clobbered
875 before they are used. These are not live.
877 Because we can be called many times on the same target, save our results
878 in a hash table indexed by INSN_UID. This is only done if the function
879 init_resource_info () was invoked before we are called. */
881 void
882 mark_target_live_regs (rtx_insn *insns, rtx target_maybe_return, struct resources *res)
884 int b = -1;
885 unsigned int i;
886 struct target_info *tinfo = NULL;
887 rtx_insn *insn;
888 rtx jump_target;
889 HARD_REG_SET scratch;
890 struct resources set, needed;
892 /* Handle end of function. */
893 if (target_maybe_return == 0 || ANY_RETURN_P (target_maybe_return))
895 *res = end_of_function_needs;
896 return;
899 /* We've handled the case of RETURN/SIMPLE_RETURN; we should now have an
900 instruction. */
901 rtx_insn *target = as_a <rtx_insn *> (target_maybe_return);
903 /* Handle return insn. */
904 if (return_insn_p (target))
906 *res = end_of_function_needs;
907 mark_referenced_resources (target, res, false);
908 return;
911 /* We have to assume memory is needed, but the CC isn't. */
912 res->memory = 1;
913 res->volatil = 0;
914 res->cc = 0;
916 /* See if we have computed this value already. */
917 if (target_hash_table != NULL)
919 for (tinfo = target_hash_table[INSN_UID (target) % TARGET_HASH_PRIME];
920 tinfo; tinfo = tinfo->next)
921 if (tinfo->uid == INSN_UID (target))
922 break;
924 /* Start by getting the basic block number. If we have saved
925 information, we can get it from there unless the insn at the
926 start of the basic block has been deleted. */
927 if (tinfo && tinfo->block != -1
928 && ! BB_HEAD (BASIC_BLOCK_FOR_FN (cfun, tinfo->block))->deleted ())
929 b = tinfo->block;
932 if (b == -1)
933 b = find_basic_block (target, MAX_DELAY_SLOT_LIVE_SEARCH);
935 if (target_hash_table != NULL)
937 if (tinfo)
939 /* If the information is up-to-date, use it. Otherwise, we will
940 update it below. */
941 if (b == tinfo->block && b != -1 && tinfo->bb_tick == bb_ticks[b])
943 COPY_HARD_REG_SET (res->regs, tinfo->live_regs);
944 return;
947 else
949 /* Allocate a place to put our results and chain it into the
950 hash table. */
951 tinfo = XNEW (struct target_info);
952 tinfo->uid = INSN_UID (target);
953 tinfo->block = b;
954 tinfo->next
955 = target_hash_table[INSN_UID (target) % TARGET_HASH_PRIME];
956 target_hash_table[INSN_UID (target) % TARGET_HASH_PRIME] = tinfo;
960 CLEAR_HARD_REG_SET (pending_dead_regs);
962 /* If we found a basic block, get the live registers from it and update
963 them with anything set or killed between its start and the insn before
964 TARGET; this custom life analysis is really about registers so we need
965 to use the LR problem. Otherwise, we must assume everything is live. */
966 if (b != -1)
968 regset regs_live = DF_LR_IN (BASIC_BLOCK_FOR_FN (cfun, b));
969 rtx_insn *start_insn, *stop_insn;
971 /* Compute hard regs live at start of block. */
972 REG_SET_TO_HARD_REG_SET (current_live_regs, regs_live);
974 /* Get starting and ending insn, handling the case where each might
975 be a SEQUENCE. */
976 start_insn = (b == ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->index ?
977 insns : BB_HEAD (BASIC_BLOCK_FOR_FN (cfun, b)));
978 stop_insn = target;
980 if (NONJUMP_INSN_P (start_insn)
981 && GET_CODE (PATTERN (start_insn)) == SEQUENCE)
982 start_insn = as_a <rtx_sequence *> (PATTERN (start_insn))->insn (0);
984 if (NONJUMP_INSN_P (stop_insn)
985 && GET_CODE (PATTERN (stop_insn)) == SEQUENCE)
986 stop_insn = next_insn (PREV_INSN (stop_insn));
988 for (insn = start_insn; insn != stop_insn;
989 insn = next_insn_no_annul (insn))
991 rtx link;
992 rtx_insn *real_insn = insn;
993 enum rtx_code code = GET_CODE (insn);
995 if (DEBUG_INSN_P (insn))
996 continue;
998 /* If this insn is from the target of a branch, it isn't going to
999 be used in the sequel. If it is used in both cases, this
1000 test will not be true. */
1001 if ((code == INSN || code == JUMP_INSN || code == CALL_INSN)
1002 && INSN_FROM_TARGET_P (insn))
1003 continue;
1005 /* If this insn is a USE made by update_block, we care about the
1006 underlying insn. */
1007 if (code == INSN
1008 && GET_CODE (PATTERN (insn)) == USE
1009 && INSN_P (XEXP (PATTERN (insn), 0)))
1010 real_insn = as_a <rtx_insn *> (XEXP (PATTERN (insn), 0));
1012 if (CALL_P (real_insn))
1014 /* Values in call-clobbered registers survive a COND_EXEC CALL
1015 if that is not executed; this matters for resoure use because
1016 they may be used by a complementarily (or more strictly)
1017 predicated instruction, or if the CALL is NORETURN. */
1018 if (GET_CODE (PATTERN (real_insn)) != COND_EXEC)
1020 HARD_REG_SET regs_invalidated_by_this_call;
1021 get_call_reg_set_usage (real_insn,
1022 &regs_invalidated_by_this_call,
1023 regs_invalidated_by_call);
1024 /* CALL clobbers all call-used regs that aren't fixed except
1025 sp, ap, and fp. Do this before setting the result of the
1026 call live. */
1027 AND_COMPL_HARD_REG_SET (current_live_regs,
1028 regs_invalidated_by_this_call);
1031 /* A CALL_INSN sets any global register live, since it may
1032 have been modified by the call. */
1033 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1034 if (global_regs[i])
1035 SET_HARD_REG_BIT (current_live_regs, i);
1038 /* Mark anything killed in an insn to be deadened at the next
1039 label. Ignore USE insns; the only REG_DEAD notes will be for
1040 parameters. But they might be early. A CALL_INSN will usually
1041 clobber registers used for parameters. It isn't worth bothering
1042 with the unlikely case when it won't. */
1043 if ((NONJUMP_INSN_P (real_insn)
1044 && GET_CODE (PATTERN (real_insn)) != USE
1045 && GET_CODE (PATTERN (real_insn)) != CLOBBER)
1046 || JUMP_P (real_insn)
1047 || CALL_P (real_insn))
1049 for (link = REG_NOTES (real_insn); link; link = XEXP (link, 1))
1050 if (REG_NOTE_KIND (link) == REG_DEAD
1051 && REG_P (XEXP (link, 0))
1052 && REGNO (XEXP (link, 0)) < FIRST_PSEUDO_REGISTER)
1053 add_to_hard_reg_set (&pending_dead_regs,
1054 GET_MODE (XEXP (link, 0)),
1055 REGNO (XEXP (link, 0)));
1057 note_stores (PATTERN (real_insn), update_live_status, NULL);
1059 /* If any registers were unused after this insn, kill them.
1060 These notes will always be accurate. */
1061 for (link = REG_NOTES (real_insn); link; link = XEXP (link, 1))
1062 if (REG_NOTE_KIND (link) == REG_UNUSED
1063 && REG_P (XEXP (link, 0))
1064 && REGNO (XEXP (link, 0)) < FIRST_PSEUDO_REGISTER)
1065 remove_from_hard_reg_set (&current_live_regs,
1066 GET_MODE (XEXP (link, 0)),
1067 REGNO (XEXP (link, 0)));
1070 else if (LABEL_P (real_insn))
1072 basic_block bb;
1074 /* A label clobbers the pending dead registers since neither
1075 reload nor jump will propagate a value across a label. */
1076 AND_COMPL_HARD_REG_SET (current_live_regs, pending_dead_regs);
1077 CLEAR_HARD_REG_SET (pending_dead_regs);
1079 /* We must conservatively assume that all registers that used
1080 to be live here still are. The fallthrough edge may have
1081 left a live register uninitialized. */
1082 bb = BLOCK_FOR_INSN (real_insn);
1083 if (bb)
1085 HARD_REG_SET extra_live;
1087 REG_SET_TO_HARD_REG_SET (extra_live, DF_LR_IN (bb));
1088 IOR_HARD_REG_SET (current_live_regs, extra_live);
1092 /* The beginning of the epilogue corresponds to the end of the
1093 RTL chain when there are no epilogue insns. Certain resources
1094 are implicitly required at that point. */
1095 else if (NOTE_P (real_insn)
1096 && NOTE_KIND (real_insn) == NOTE_INSN_EPILOGUE_BEG)
1097 IOR_HARD_REG_SET (current_live_regs, start_of_epilogue_needs.regs);
1100 COPY_HARD_REG_SET (res->regs, current_live_regs);
1101 if (tinfo != NULL)
1103 tinfo->block = b;
1104 tinfo->bb_tick = bb_ticks[b];
1107 else
1108 /* We didn't find the start of a basic block. Assume everything
1109 in use. This should happen only extremely rarely. */
1110 SET_HARD_REG_SET (res->regs);
1112 CLEAR_RESOURCE (&set);
1113 CLEAR_RESOURCE (&needed);
1115 rtx_insn *jump_insn = find_dead_or_set_registers (target, res, &jump_target,
1116 0, set, needed);
1118 /* If we hit an unconditional branch, we have another way of finding out
1119 what is live: we can see what is live at the branch target and include
1120 anything used but not set before the branch. We add the live
1121 resources found using the test below to those found until now. */
1123 if (jump_insn)
1125 struct resources new_resources;
1126 rtx_insn *stop_insn = next_active_insn (jump_insn);
1128 if (!ANY_RETURN_P (jump_target))
1129 jump_target = next_active_insn (jump_target);
1130 mark_target_live_regs (insns, jump_target, &new_resources);
1131 CLEAR_RESOURCE (&set);
1132 CLEAR_RESOURCE (&needed);
1134 /* Include JUMP_INSN in the needed registers. */
1135 for (insn = target; insn != stop_insn; insn = next_active_insn (insn))
1137 mark_referenced_resources (insn, &needed, true);
1139 COPY_HARD_REG_SET (scratch, needed.regs);
1140 AND_COMPL_HARD_REG_SET (scratch, set.regs);
1141 IOR_HARD_REG_SET (new_resources.regs, scratch);
1143 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
1146 IOR_HARD_REG_SET (res->regs, new_resources.regs);
1149 if (tinfo != NULL)
1151 COPY_HARD_REG_SET (tinfo->live_regs, res->regs);
1155 /* Initialize the resources required by mark_target_live_regs ().
1156 This should be invoked before the first call to mark_target_live_regs. */
1158 void
1159 init_resource_info (rtx_insn *epilogue_insn)
1161 int i;
1162 basic_block bb;
1164 /* Indicate what resources are required to be valid at the end of the current
1165 function. The condition code never is and memory always is.
1166 The stack pointer is needed unless EXIT_IGNORE_STACK is true
1167 and there is an epilogue that restores the original stack pointer
1168 from the frame pointer. Registers used to return the function value
1169 are needed. Registers holding global variables are needed. */
1171 end_of_function_needs.cc = 0;
1172 end_of_function_needs.memory = 1;
1173 CLEAR_HARD_REG_SET (end_of_function_needs.regs);
1175 if (frame_pointer_needed)
1177 SET_HARD_REG_BIT (end_of_function_needs.regs, FRAME_POINTER_REGNUM);
1178 if (!HARD_FRAME_POINTER_IS_FRAME_POINTER)
1179 SET_HARD_REG_BIT (end_of_function_needs.regs,
1180 HARD_FRAME_POINTER_REGNUM);
1182 if (!(frame_pointer_needed
1183 && EXIT_IGNORE_STACK
1184 && epilogue_insn
1185 && !crtl->sp_is_unchanging))
1186 SET_HARD_REG_BIT (end_of_function_needs.regs, STACK_POINTER_REGNUM);
1188 if (crtl->return_rtx != 0)
1189 mark_referenced_resources (crtl->return_rtx,
1190 &end_of_function_needs, true);
1192 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1193 if (global_regs[i] || EPILOGUE_USES (i))
1194 SET_HARD_REG_BIT (end_of_function_needs.regs, i);
1196 /* The registers required to be live at the end of the function are
1197 represented in the flow information as being dead just prior to
1198 reaching the end of the function. For example, the return of a value
1199 might be represented by a USE of the return register immediately
1200 followed by an unconditional jump to the return label where the
1201 return label is the end of the RTL chain. The end of the RTL chain
1202 is then taken to mean that the return register is live.
1204 This sequence is no longer maintained when epilogue instructions are
1205 added to the RTL chain. To reconstruct the original meaning, the
1206 start of the epilogue (NOTE_INSN_EPILOGUE_BEG) is regarded as the
1207 point where these registers become live (start_of_epilogue_needs).
1208 If epilogue instructions are present, the registers set by those
1209 instructions won't have been processed by flow. Thus, those
1210 registers are additionally required at the end of the RTL chain
1211 (end_of_function_needs). */
1213 start_of_epilogue_needs = end_of_function_needs;
1215 while ((epilogue_insn = next_nonnote_insn (epilogue_insn)))
1217 mark_set_resources (epilogue_insn, &end_of_function_needs, 0,
1218 MARK_SRC_DEST_CALL);
1219 if (return_insn_p (epilogue_insn))
1220 break;
1223 /* Allocate and initialize the tables used by mark_target_live_regs. */
1224 target_hash_table = XCNEWVEC (struct target_info *, TARGET_HASH_PRIME);
1225 bb_ticks = XCNEWVEC (int, last_basic_block_for_fn (cfun));
1227 /* Set the BLOCK_FOR_INSN of each label that starts a basic block. */
1228 FOR_EACH_BB_FN (bb, cfun)
1229 if (LABEL_P (BB_HEAD (bb)))
1230 BLOCK_FOR_INSN (BB_HEAD (bb)) = bb;
1233 /* Free up the resources allocated to mark_target_live_regs (). This
1234 should be invoked after the last call to mark_target_live_regs (). */
1236 void
1237 free_resource_info (void)
1239 basic_block bb;
1241 if (target_hash_table != NULL)
1243 int i;
1245 for (i = 0; i < TARGET_HASH_PRIME; ++i)
1247 struct target_info *ti = target_hash_table[i];
1249 while (ti)
1251 struct target_info *next = ti->next;
1252 free (ti);
1253 ti = next;
1257 free (target_hash_table);
1258 target_hash_table = NULL;
1261 if (bb_ticks != NULL)
1263 free (bb_ticks);
1264 bb_ticks = NULL;
1267 FOR_EACH_BB_FN (bb, cfun)
1268 if (LABEL_P (BB_HEAD (bb)))
1269 BLOCK_FOR_INSN (BB_HEAD (bb)) = NULL;
1272 /* Clear any hashed information that we have stored for INSN. */
1274 void
1275 clear_hashed_info_for_insn (rtx_insn *insn)
1277 struct target_info *tinfo;
1279 if (target_hash_table != NULL)
1281 for (tinfo = target_hash_table[INSN_UID (insn) % TARGET_HASH_PRIME];
1282 tinfo; tinfo = tinfo->next)
1283 if (tinfo->uid == INSN_UID (insn))
1284 break;
1286 if (tinfo)
1287 tinfo->block = -1;
1291 /* Increment the tick count for the basic block that contains INSN. */
1293 void
1294 incr_ticks_for_insn (rtx_insn *insn)
1296 int b = find_basic_block (insn, MAX_DELAY_SLOT_LIVE_SEARCH);
1298 if (b != -1)
1299 bb_ticks[b]++;
1302 /* Add TRIAL to the set of resources used at the end of the current
1303 function. */
1304 void
1305 mark_end_of_function_resources (rtx trial, bool include_delayed_effects)
1307 mark_referenced_resources (trial, &end_of_function_needs,
1308 include_delayed_effects);