comparison gcc/reload1.c @ 67:f6334be47118

update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
author nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
date Tue, 22 Mar 2011 17:18:12 +0900
parents b7f97abdc517
children 04ced10e8804
comparison
equal deleted inserted replaced
65:65488c3d617d 67:f6334be47118
1 /* Reload pseudo regs into hard regs for insns that require hard regs. 1 /* Reload pseudo regs into hard regs for insns that require hard regs.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 Free Software Foundation, Inc. 4 2011 Free Software Foundation, Inc.
5 5
6 This file is part of GCC. 6 This file is part of GCC.
7 7
8 GCC is free software; you can redistribute it and/or modify it under 8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free 9 the terms of the GNU General Public License as published by the Free
24 #include "coretypes.h" 24 #include "coretypes.h"
25 #include "tm.h" 25 #include "tm.h"
26 26
27 #include "machmode.h" 27 #include "machmode.h"
28 #include "hard-reg-set.h" 28 #include "hard-reg-set.h"
29 #include "rtl.h" 29 #include "rtl-error.h"
30 #include "tm_p.h" 30 #include "tm_p.h"
31 #include "obstack.h" 31 #include "obstack.h"
32 #include "insn-config.h" 32 #include "insn-config.h"
33 #include "flags.h" 33 #include "flags.h"
34 #include "function.h" 34 #include "function.h"
39 #include "basic-block.h" 39 #include "basic-block.h"
40 #include "df.h" 40 #include "df.h"
41 #include "reload.h" 41 #include "reload.h"
42 #include "recog.h" 42 #include "recog.h"
43 #include "output.h" 43 #include "output.h"
44 #include "toplev.h"
45 #include "except.h" 44 #include "except.h"
46 #include "tree.h" 45 #include "tree.h"
47 #include "ira.h" 46 #include "ira.h"
48 #include "target.h" 47 #include "target.h"
49 #include "emit-rtl.h" 48 #include "emit-rtl.h"
80 is in charge of scanning the entire rtl code, accumulating the 79 is in charge of scanning the entire rtl code, accumulating the
81 reload needs, spilling, assigning reload registers to use for 80 reload needs, spilling, assigning reload registers to use for
82 fixing up each insn, and generating the new insns to copy values 81 fixing up each insn, and generating the new insns to copy values
83 into the reload registers. */ 82 into the reload registers. */
84 83
84 struct target_reload default_target_reload;
85 #if SWITCHABLE_TARGET
86 struct target_reload *this_target_reload = &default_target_reload;
87 #endif
88
89 #define spill_indirect_levels \
90 (this_target_reload->x_spill_indirect_levels)
91
85 /* During reload_as_needed, element N contains a REG rtx for the hard reg 92 /* During reload_as_needed, element N contains a REG rtx for the hard reg
86 into which reg N has been reloaded (perhaps for a previous insn). */ 93 into which reg N has been reloaded (perhaps for a previous insn). */
87 static rtx *reg_last_reload_reg; 94 static rtx *reg_last_reload_reg;
88 95
89 /* Elt N nonzero if reg_last_reload_reg[N] has been set in this insn 96 /* Elt N nonzero if reg_last_reload_reg[N] has been set in this insn
230 237
231 /* Index of last register assigned as a spill register. We allocate in 238 /* Index of last register assigned as a spill register. We allocate in
232 a round-robin fashion. */ 239 a round-robin fashion. */
233 static int last_spill_reg; 240 static int last_spill_reg;
234 241
235 /* Nonzero if indirect addressing is supported on the machine; this means
236 that spilling (REG n) does not require reloading it into a register in
237 order to do (MEM (REG n)) or (MEM (PLUS (REG n) (CONST_INT c))). The
238 value indicates the level of indirect addressing supported, e.g., two
239 means that (MEM (MEM (REG n))) is also valid if (REG n) does not get
240 a hard register. */
241 static char spill_indirect_levels;
242
243 /* Nonzero if indirect addressing is supported when the innermost MEM is
244 of the form (MEM (SYMBOL_REF sym)). It is assumed that the level to
245 which these are valid is the same as spill_indirect_levels, above. */
246 char indirect_symref_ok;
247
248 /* Nonzero if an address (plus (reg frame_pointer) (reg ...)) is valid. */
249 char double_reg_address_ok;
250
251 /* Record the stack slot for each spilled hard register. */ 242 /* Record the stack slot for each spilled hard register. */
252 static rtx spill_stack_slot[FIRST_PSEUDO_REGISTER]; 243 static rtx spill_stack_slot[FIRST_PSEUDO_REGISTER];
253 244
254 /* Width allocated so far for that stack slot. */ 245 /* Width allocated so far for that stack slot. */
255 static unsigned int spill_stack_slot_width[FIRST_PSEUDO_REGISTER]; 246 static unsigned int spill_stack_slot_width[FIRST_PSEUDO_REGISTER];
273 int caller_save_needed; 264 int caller_save_needed;
274 265
275 /* Set to 1 while reload_as_needed is operating. 266 /* Set to 1 while reload_as_needed is operating.
276 Required by some machines to handle any generated moves differently. */ 267 Required by some machines to handle any generated moves differently. */
277 int reload_in_progress = 0; 268 int reload_in_progress = 0;
278
279 /* These arrays record the insn_code of insns that may be needed to
280 perform input and output reloads of special objects. They provide a
281 place to pass a scratch register. */
282 enum insn_code reload_in_optab[NUM_MACHINE_MODES];
283 enum insn_code reload_out_optab[NUM_MACHINE_MODES];
284 269
285 /* This obstack is used for allocation of rtl during register elimination. 270 /* This obstack is used for allocation of rtl during register elimination.
286 The allocated storage can be freed once find_reloads has processed the 271 The allocated storage can be freed once find_reloads has processed the
287 insn. */ 272 insn. */
288 static struct obstack reload_obstack; 273 static struct obstack reload_obstack;
410 static void delete_dead_insn (rtx); 395 static void delete_dead_insn (rtx);
411 static void alter_reg (int, int, bool); 396 static void alter_reg (int, int, bool);
412 static void set_label_offsets (rtx, rtx, int); 397 static void set_label_offsets (rtx, rtx, int);
413 static void check_eliminable_occurrences (rtx); 398 static void check_eliminable_occurrences (rtx);
414 static void elimination_effects (rtx, enum machine_mode); 399 static void elimination_effects (rtx, enum machine_mode);
400 static rtx eliminate_regs_1 (rtx, enum machine_mode, rtx, bool, bool);
415 static int eliminate_regs_in_insn (rtx, int); 401 static int eliminate_regs_in_insn (rtx, int);
416 static void update_eliminable_offsets (void); 402 static void update_eliminable_offsets (void);
417 static void mark_not_eliminable (rtx, const_rtx, void *); 403 static void mark_not_eliminable (rtx, const_rtx, void *);
418 static void set_initial_elim_offsets (void); 404 static void set_initial_elim_offsets (void);
419 static bool verify_initial_elim_offsets (void); 405 static bool verify_initial_elim_offsets (void);
420 static void set_initial_label_offsets (void); 406 static void set_initial_label_offsets (void);
421 static void set_offsets_for_label (rtx); 407 static void set_offsets_for_label (rtx);
408 static void init_eliminable_invariants (rtx, bool);
422 static void init_elim_table (void); 409 static void init_elim_table (void);
410 static void free_reg_equiv (void);
423 static void update_eliminables (HARD_REG_SET *); 411 static void update_eliminables (HARD_REG_SET *);
412 static void elimination_costs_in_insn (rtx);
424 static void spill_hard_reg (unsigned int, int); 413 static void spill_hard_reg (unsigned int, int);
425 static int finish_spills (int); 414 static int finish_spills (int);
426 static void scan_paradoxical_subregs (rtx); 415 static void scan_paradoxical_subregs (rtx);
427 static void count_pseudo (int); 416 static void count_pseudo (int);
428 static void order_regs_for_reload (struct insn_chain *); 417 static void order_regs_for_reload (struct insn_chain *);
444 static int conflicts_with_override (rtx); 433 static int conflicts_with_override (rtx);
445 static void failed_reload (rtx, int); 434 static void failed_reload (rtx, int);
446 static int set_reload_reg (int, int); 435 static int set_reload_reg (int, int);
447 static void choose_reload_regs_init (struct insn_chain *, rtx *); 436 static void choose_reload_regs_init (struct insn_chain *, rtx *);
448 static void choose_reload_regs (struct insn_chain *); 437 static void choose_reload_regs (struct insn_chain *);
449 static void merge_assigned_reloads (rtx);
450 static void emit_input_reload_insns (struct insn_chain *, struct reload *, 438 static void emit_input_reload_insns (struct insn_chain *, struct reload *,
451 rtx, int); 439 rtx, int);
452 static void emit_output_reload_insns (struct insn_chain *, struct reload *, 440 static void emit_output_reload_insns (struct insn_chain *, struct reload *,
453 int); 441 int);
454 static void do_input_reload (struct insn_chain *, struct reload *, int); 442 static void do_input_reload (struct insn_chain *, struct reload *, int);
598 unsigned int regno = REGNO (x); 586 unsigned int regno = REGNO (x);
599 587
600 if (regno < FIRST_PSEUDO_REGISTER) 588 if (regno < FIRST_PSEUDO_REGISTER)
601 return; 589 return;
602 590
603 x = eliminate_regs (x, mem_mode, usage); 591 x = eliminate_regs_1 (x, mem_mode, usage, true, false);
604 if (x != *loc) 592 if (x != *loc)
605 { 593 {
606 *loc = x; 594 *loc = x;
607 replace_pseudos_in (loc, mem_mode, usage); 595 replace_pseudos_in (loc, mem_mode, usage);
608 return; 596 return;
609 } 597 }
610 598
611 if (reg_equiv_constant[regno]) 599 if (reg_equiv_constant[regno])
612 *loc = reg_equiv_constant[regno]; 600 *loc = reg_equiv_constant[regno];
601 else if (reg_equiv_invariant[regno])
602 *loc = reg_equiv_invariant[regno];
613 else if (reg_equiv_mem[regno]) 603 else if (reg_equiv_mem[regno])
614 *loc = reg_equiv_mem[regno]; 604 *loc = reg_equiv_mem[regno];
615 else if (reg_equiv_address[regno]) 605 else if (reg_equiv_address[regno])
616 *loc = gen_rtx_MEM (GET_MODE (x), reg_equiv_address[regno]); 606 *loc = gen_rtx_MEM (GET_MODE (x), reg_equiv_address[regno]);
617 else 607 else
683 free (worklist); 673 free (worklist);
684 674
685 /* Now see if there's a reachable block with an exceptional incoming 675 /* Now see if there's a reachable block with an exceptional incoming
686 edge. */ 676 edge. */
687 FOR_EACH_BB (bb) 677 FOR_EACH_BB (bb)
688 if (bb->flags & BB_REACHABLE) 678 if (bb->flags & BB_REACHABLE && bb_has_abnormal_pred (bb))
689 FOR_EACH_EDGE (e, ei, bb->preds) 679 return true;
690 if (e->flags & EDGE_ABNORMAL)
691 return true;
692 680
693 /* No exceptional block reached exit unexceptionally. */ 681 /* No exceptional block reached exit unexceptionally. */
694 return false; 682 return false;
695 } 683 }
696 684
697 685
698 /* Global variables used by reload and its subroutines. */ 686 /* Global variables used by reload and its subroutines. */
687
688 /* The current basic block while in calculate_elim_costs_all_insns. */
689 static basic_block elim_bb;
699 690
700 /* Set during calculate_needs if an insn needs register elimination. */ 691 /* Set during calculate_needs if an insn needs register elimination. */
701 static int something_needs_elimination; 692 static int something_needs_elimination;
702 /* Set during calculate_needs if an insn needs an operand changed. */ 693 /* Set during calculate_needs if an insn needs an operand changed. */
703 static int something_needs_operands_changed; 694 static int something_needs_operands_changed;
775 if (crtl->saves_all_registers) 766 if (crtl->saves_all_registers)
776 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++) 767 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
777 if (! call_used_regs[i] && ! fixed_regs[i] && ! LOCAL_REGNO (i)) 768 if (! call_used_regs[i] && ! fixed_regs[i] && ! LOCAL_REGNO (i))
778 df_set_regs_ever_live (i, true); 769 df_set_regs_ever_live (i, true);
779 770
780 /* Find all the pseudo registers that didn't get hard regs 771 reg_old_renumber = XCNEWVEC (short, max_regno);
781 but do have known equivalent constants or memory slots. 772 memcpy (reg_old_renumber, reg_renumber, max_regno * sizeof (short));
782 These include parameters (known equivalent to parameter slots) 773 pseudo_forbidden_regs = XNEWVEC (HARD_REG_SET, max_regno);
783 and cse'd or loop-moved constant memory addresses. 774 pseudo_previous_regs = XCNEWVEC (HARD_REG_SET, max_regno);
784 775
785 Record constant equivalents in reg_equiv_constant 776 CLEAR_HARD_REG_SET (bad_spill_regs_global);
786 so they will be substituted by find_reloads. 777
787 Record memory equivalents in reg_mem_equiv so they can 778 init_eliminable_invariants (first, true);
788 be substituted eventually by altering the REG-rtx's. */ 779 init_elim_table ();
780
781 /* Alter each pseudo-reg rtx to contain its hard reg number. Assign
782 stack slots to the pseudos that lack hard regs or equivalents.
783 Do not touch virtual registers. */
784
785 temp_pseudo_reg_arr = XNEWVEC (int, max_regno - LAST_VIRTUAL_REGISTER - 1);
786 for (n = 0, i = LAST_VIRTUAL_REGISTER + 1; i < max_regno; i++)
787 temp_pseudo_reg_arr[n++] = i;
788
789 if (ira_conflicts_p)
790 /* Ask IRA to order pseudo-registers for better stack slot
791 sharing. */
792 ira_sort_regnos_for_alter_reg (temp_pseudo_reg_arr, n, reg_max_ref_width);
793
794 for (i = 0; i < n; i++)
795 alter_reg (temp_pseudo_reg_arr[i], -1, false);
796
797 /* If we have some registers we think can be eliminated, scan all insns to
798 see if there is an insn that sets one of these registers to something
799 other than itself plus a constant. If so, the register cannot be
800 eliminated. Doing this scan here eliminates an extra pass through the
801 main reload loop in the most common case where register elimination
802 cannot be done. */
803 for (insn = first; insn && num_eliminable; insn = NEXT_INSN (insn))
804 if (INSN_P (insn))
805 note_stores (PATTERN (insn), mark_not_eliminable, NULL);
806
807 maybe_fix_stack_asms ();
808
809 insns_need_reload = 0;
810 something_needs_elimination = 0;
811
812 /* Initialize to -1, which means take the first spill register. */
813 last_spill_reg = -1;
814
815 /* Spill any hard regs that we know we can't eliminate. */
816 CLEAR_HARD_REG_SET (used_spill_regs);
817 /* There can be multiple ways to eliminate a register;
818 they should be listed adjacently.
819 Elimination for any register fails only if all possible ways fail. */
820 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; )
821 {
822 int from = ep->from;
823 int can_eliminate = 0;
824 do
825 {
826 can_eliminate |= ep->can_eliminate;
827 ep++;
828 }
829 while (ep < &reg_eliminate[NUM_ELIMINABLE_REGS] && ep->from == from);
830 if (! can_eliminate)
831 spill_hard_reg (from, 1);
832 }
833
834 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
835 if (frame_pointer_needed)
836 spill_hard_reg (HARD_FRAME_POINTER_REGNUM, 1);
837 #endif
838 finish_spills (global);
839
840 /* From now on, we may need to generate moves differently. We may also
841 allow modifications of insns which cause them to not be recognized.
842 Any such modifications will be cleaned up during reload itself. */
843 reload_in_progress = 1;
844
845 /* This loop scans the entire function each go-round
846 and repeats until one repetition spills no additional hard regs. */
847 for (;;)
848 {
849 int something_changed;
850 int did_spill;
851 HOST_WIDE_INT starting_frame_size;
852
853 starting_frame_size = get_frame_size ();
854 something_was_spilled = false;
855
856 set_initial_elim_offsets ();
857 set_initial_label_offsets ();
858
859 /* For each pseudo register that has an equivalent location defined,
860 try to eliminate any eliminable registers (such as the frame pointer)
861 assuming initial offsets for the replacement register, which
862 is the normal case.
863
864 If the resulting location is directly addressable, substitute
865 the MEM we just got directly for the old REG.
866
867 If it is not addressable but is a constant or the sum of a hard reg
868 and constant, it is probably not addressable because the constant is
869 out of range, in that case record the address; we will generate
870 hairy code to compute the address in a register each time it is
871 needed. Similarly if it is a hard register, but one that is not
872 valid as an address register.
873
874 If the location is not addressable, but does not have one of the
875 above forms, assign a stack slot. We have to do this to avoid the
876 potential of producing lots of reloads if, e.g., a location involves
877 a pseudo that didn't get a hard register and has an equivalent memory
878 location that also involves a pseudo that didn't get a hard register.
879
880 Perhaps at some point we will improve reload_when_needed handling
881 so this problem goes away. But that's very hairy. */
882
883 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
884 if (reg_renumber[i] < 0 && reg_equiv_memory_loc[i])
885 {
886 rtx x = eliminate_regs (reg_equiv_memory_loc[i], VOIDmode,
887 NULL_RTX);
888
889 if (strict_memory_address_addr_space_p
890 (GET_MODE (regno_reg_rtx[i]), XEXP (x, 0),
891 MEM_ADDR_SPACE (x)))
892 reg_equiv_mem[i] = x, reg_equiv_address[i] = 0;
893 else if (CONSTANT_P (XEXP (x, 0))
894 || (REG_P (XEXP (x, 0))
895 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
896 || (GET_CODE (XEXP (x, 0)) == PLUS
897 && REG_P (XEXP (XEXP (x, 0), 0))
898 && (REGNO (XEXP (XEXP (x, 0), 0))
899 < FIRST_PSEUDO_REGISTER)
900 && CONSTANT_P (XEXP (XEXP (x, 0), 1))))
901 reg_equiv_address[i] = XEXP (x, 0), reg_equiv_mem[i] = 0;
902 else
903 {
904 /* Make a new stack slot. Then indicate that something
905 changed so we go back and recompute offsets for
906 eliminable registers because the allocation of memory
907 below might change some offset. reg_equiv_{mem,address}
908 will be set up for this pseudo on the next pass around
909 the loop. */
910 reg_equiv_memory_loc[i] = 0;
911 reg_equiv_init[i] = 0;
912 alter_reg (i, -1, true);
913 }
914 }
915
916 if (caller_save_needed)
917 setup_save_areas ();
918
919 /* If we allocated another stack slot, redo elimination bookkeeping. */
920 if (something_was_spilled || starting_frame_size != get_frame_size ())
921 continue;
922 if (starting_frame_size && crtl->stack_alignment_needed)
923 {
924 /* If we have a stack frame, we must align it now. The
925 stack size may be a part of the offset computation for
926 register elimination. So if this changes the stack size,
927 then repeat the elimination bookkeeping. We don't
928 realign when there is no stack, as that will cause a
929 stack frame when none is needed should
930 STARTING_FRAME_OFFSET not be already aligned to
931 STACK_BOUNDARY. */
932 assign_stack_local (BLKmode, 0, crtl->stack_alignment_needed);
933 if (starting_frame_size != get_frame_size ())
934 continue;
935 }
936
937 if (caller_save_needed)
938 {
939 save_call_clobbered_regs ();
940 /* That might have allocated new insn_chain structures. */
941 reload_firstobj = XOBNEWVAR (&reload_obstack, char, 0);
942 }
943
944 calculate_needs_all_insns (global);
945
946 if (! ira_conflicts_p)
947 /* Don't do it for IRA. We need this info because we don't
948 change live_throughout and dead_or_set for chains when IRA
949 is used. */
950 CLEAR_REG_SET (&spilled_pseudos);
951
952 did_spill = 0;
953
954 something_changed = 0;
955
956 /* If we allocated any new memory locations, make another pass
957 since it might have changed elimination offsets. */
958 if (something_was_spilled || starting_frame_size != get_frame_size ())
959 something_changed = 1;
960
961 /* Even if the frame size remained the same, we might still have
962 changed elimination offsets, e.g. if find_reloads called
963 force_const_mem requiring the back end to allocate a constant
964 pool base register that needs to be saved on the stack. */
965 else if (!verify_initial_elim_offsets ())
966 something_changed = 1;
967
968 {
969 HARD_REG_SET to_spill;
970 CLEAR_HARD_REG_SET (to_spill);
971 update_eliminables (&to_spill);
972 AND_COMPL_HARD_REG_SET (used_spill_regs, to_spill);
973
974 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
975 if (TEST_HARD_REG_BIT (to_spill, i))
976 {
977 spill_hard_reg (i, 1);
978 did_spill = 1;
979
980 /* Regardless of the state of spills, if we previously had
981 a register that we thought we could eliminate, but now can
982 not eliminate, we must run another pass.
983
984 Consider pseudos which have an entry in reg_equiv_* which
985 reference an eliminable register. We must make another pass
986 to update reg_equiv_* so that we do not substitute in the
987 old value from when we thought the elimination could be
988 performed. */
989 something_changed = 1;
990 }
991 }
992
993 select_reload_regs ();
994 if (failure)
995 goto failed;
996
997 if (insns_need_reload != 0 || did_spill)
998 something_changed |= finish_spills (global);
999
1000 if (! something_changed)
1001 break;
1002
1003 if (caller_save_needed)
1004 delete_caller_save_insns ();
1005
1006 obstack_free (&reload_obstack, reload_firstobj);
1007 }
1008
1009 /* If global-alloc was run, notify it of any register eliminations we have
1010 done. */
1011 if (global)
1012 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1013 if (ep->can_eliminate)
1014 mark_elimination (ep->from, ep->to);
1015
1016 /* If a pseudo has no hard reg, delete the insns that made the equivalence.
1017 If that insn didn't set the register (i.e., it copied the register to
1018 memory), just delete that insn instead of the equivalencing insn plus
1019 anything now dead. If we call delete_dead_insn on that insn, we may
1020 delete the insn that actually sets the register if the register dies
1021 there and that is incorrect. */
1022
1023 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1024 {
1025 if (reg_renumber[i] < 0 && reg_equiv_init[i] != 0)
1026 {
1027 rtx list;
1028 for (list = reg_equiv_init[i]; list; list = XEXP (list, 1))
1029 {
1030 rtx equiv_insn = XEXP (list, 0);
1031
1032 /* If we already deleted the insn or if it may trap, we can't
1033 delete it. The latter case shouldn't happen, but can
1034 if an insn has a variable address, gets a REG_EH_REGION
1035 note added to it, and then gets converted into a load
1036 from a constant address. */
1037 if (NOTE_P (equiv_insn)
1038 || can_throw_internal (equiv_insn))
1039 ;
1040 else if (reg_set_p (regno_reg_rtx[i], PATTERN (equiv_insn)))
1041 delete_dead_insn (equiv_insn);
1042 else
1043 SET_INSN_DELETED (equiv_insn);
1044 }
1045 }
1046 }
1047
1048 /* Use the reload registers where necessary
1049 by generating move instructions to move the must-be-register
1050 values into or out of the reload registers. */
1051
1052 if (insns_need_reload != 0 || something_needs_elimination
1053 || something_needs_operands_changed)
1054 {
1055 HOST_WIDE_INT old_frame_size = get_frame_size ();
1056
1057 reload_as_needed (global);
1058
1059 gcc_assert (old_frame_size == get_frame_size ());
1060
1061 gcc_assert (verify_initial_elim_offsets ());
1062 }
1063
1064 /* If we were able to eliminate the frame pointer, show that it is no
1065 longer live at the start of any basic block. If it ls live by
1066 virtue of being in a pseudo, that pseudo will be marked live
1067 and hence the frame pointer will be known to be live via that
1068 pseudo. */
1069
1070 if (! frame_pointer_needed)
1071 FOR_EACH_BB (bb)
1072 bitmap_clear_bit (df_get_live_in (bb), HARD_FRAME_POINTER_REGNUM);
1073
1074 /* Come here (with failure set nonzero) if we can't get enough spill
1075 regs. */
1076 failed:
1077
1078 CLEAR_REG_SET (&changed_allocation_pseudos);
1079 CLEAR_REG_SET (&spilled_pseudos);
1080 reload_in_progress = 0;
1081
1082 /* Now eliminate all pseudo regs by modifying them into
1083 their equivalent memory references.
1084 The REG-rtx's for the pseudos are modified in place,
1085 so all insns that used to refer to them now refer to memory.
1086
1087 For a reg that has a reg_equiv_address, all those insns
1088 were changed by reloading so that no insns refer to it any longer;
1089 but the DECL_RTL of a variable decl may refer to it,
1090 and if so this causes the debugging info to mention the variable. */
1091
1092 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1093 {
1094 rtx addr = 0;
1095
1096 if (reg_equiv_mem[i])
1097 addr = XEXP (reg_equiv_mem[i], 0);
1098
1099 if (reg_equiv_address[i])
1100 addr = reg_equiv_address[i];
1101
1102 if (addr)
1103 {
1104 if (reg_renumber[i] < 0)
1105 {
1106 rtx reg = regno_reg_rtx[i];
1107
1108 REG_USERVAR_P (reg) = 0;
1109 PUT_CODE (reg, MEM);
1110 XEXP (reg, 0) = addr;
1111 if (reg_equiv_memory_loc[i])
1112 MEM_COPY_ATTRIBUTES (reg, reg_equiv_memory_loc[i]);
1113 else
1114 {
1115 MEM_IN_STRUCT_P (reg) = MEM_SCALAR_P (reg) = 0;
1116 MEM_ATTRS (reg) = 0;
1117 }
1118 MEM_NOTRAP_P (reg) = 1;
1119 }
1120 else if (reg_equiv_mem[i])
1121 XEXP (reg_equiv_mem[i], 0) = addr;
1122 }
1123
1124 /* We don't want complex addressing modes in debug insns
1125 if simpler ones will do, so delegitimize equivalences
1126 in debug insns. */
1127 if (MAY_HAVE_DEBUG_INSNS && reg_renumber[i] < 0)
1128 {
1129 rtx reg = regno_reg_rtx[i];
1130 rtx equiv = 0;
1131 df_ref use, next;
1132
1133 if (reg_equiv_constant[i])
1134 equiv = reg_equiv_constant[i];
1135 else if (reg_equiv_invariant[i])
1136 equiv = reg_equiv_invariant[i];
1137 else if (reg && MEM_P (reg))
1138 equiv = targetm.delegitimize_address (reg);
1139 else if (reg && REG_P (reg) && (int)REGNO (reg) != i)
1140 equiv = reg;
1141
1142 if (equiv == reg)
1143 continue;
1144
1145 for (use = DF_REG_USE_CHAIN (i); use; use = next)
1146 {
1147 insn = DF_REF_INSN (use);
1148
1149 /* Make sure the next ref is for a different instruction,
1150 so that we're not affected by the rescan. */
1151 next = DF_REF_NEXT_REG (use);
1152 while (next && DF_REF_INSN (next) == insn)
1153 next = DF_REF_NEXT_REG (next);
1154
1155 if (DEBUG_INSN_P (insn))
1156 {
1157 if (!equiv)
1158 {
1159 INSN_VAR_LOCATION_LOC (insn) = gen_rtx_UNKNOWN_VAR_LOC ();
1160 df_insn_rescan_debug_internal (insn);
1161 }
1162 else
1163 INSN_VAR_LOCATION_LOC (insn)
1164 = simplify_replace_rtx (INSN_VAR_LOCATION_LOC (insn),
1165 reg, equiv);
1166 }
1167 }
1168 }
1169 }
1170
1171 /* We must set reload_completed now since the cleanup_subreg_operands call
1172 below will re-recognize each insn and reload may have generated insns
1173 which are only valid during and after reload. */
1174 reload_completed = 1;
1175
1176 /* Make a pass over all the insns and delete all USEs which we inserted
1177 only to tag a REG_EQUAL note on them. Remove all REG_DEAD and REG_UNUSED
1178 notes. Delete all CLOBBER insns, except those that refer to the return
1179 value and the special mem:BLK CLOBBERs added to prevent the scheduler
1180 from misarranging variable-array code, and simplify (subreg (reg))
1181 operands. Strip and regenerate REG_INC notes that may have been moved
1182 around. */
1183
1184 for (insn = first; insn; insn = NEXT_INSN (insn))
1185 if (INSN_P (insn))
1186 {
1187 rtx *pnote;
1188
1189 if (CALL_P (insn))
1190 replace_pseudos_in (& CALL_INSN_FUNCTION_USAGE (insn),
1191 VOIDmode, CALL_INSN_FUNCTION_USAGE (insn));
1192
1193 if ((GET_CODE (PATTERN (insn)) == USE
1194 /* We mark with QImode USEs introduced by reload itself. */
1195 && (GET_MODE (insn) == QImode
1196 || find_reg_note (insn, REG_EQUAL, NULL_RTX)))
1197 || (GET_CODE (PATTERN (insn)) == CLOBBER
1198 && (!MEM_P (XEXP (PATTERN (insn), 0))
1199 || GET_MODE (XEXP (PATTERN (insn), 0)) != BLKmode
1200 || (GET_CODE (XEXP (XEXP (PATTERN (insn), 0), 0)) != SCRATCH
1201 && XEXP (XEXP (PATTERN (insn), 0), 0)
1202 != stack_pointer_rtx))
1203 && (!REG_P (XEXP (PATTERN (insn), 0))
1204 || ! REG_FUNCTION_VALUE_P (XEXP (PATTERN (insn), 0)))))
1205 {
1206 delete_insn (insn);
1207 continue;
1208 }
1209
1210 /* Some CLOBBERs may survive until here and still reference unassigned
1211 pseudos with const equivalent, which may in turn cause ICE in later
1212 passes if the reference remains in place. */
1213 if (GET_CODE (PATTERN (insn)) == CLOBBER)
1214 replace_pseudos_in (& XEXP (PATTERN (insn), 0),
1215 VOIDmode, PATTERN (insn));
1216
1217 /* Discard obvious no-ops, even without -O. This optimization
1218 is fast and doesn't interfere with debugging. */
1219 if (NONJUMP_INSN_P (insn)
1220 && GET_CODE (PATTERN (insn)) == SET
1221 && REG_P (SET_SRC (PATTERN (insn)))
1222 && REG_P (SET_DEST (PATTERN (insn)))
1223 && (REGNO (SET_SRC (PATTERN (insn)))
1224 == REGNO (SET_DEST (PATTERN (insn)))))
1225 {
1226 delete_insn (insn);
1227 continue;
1228 }
1229
1230 pnote = &REG_NOTES (insn);
1231 while (*pnote != 0)
1232 {
1233 if (REG_NOTE_KIND (*pnote) == REG_DEAD
1234 || REG_NOTE_KIND (*pnote) == REG_UNUSED
1235 || REG_NOTE_KIND (*pnote) == REG_INC)
1236 *pnote = XEXP (*pnote, 1);
1237 else
1238 pnote = &XEXP (*pnote, 1);
1239 }
1240
1241 #ifdef AUTO_INC_DEC
1242 add_auto_inc_notes (insn, PATTERN (insn));
1243 #endif
1244
1245 /* Simplify (subreg (reg)) if it appears as an operand. */
1246 cleanup_subreg_operands (insn);
1247
1248 /* Clean up invalid ASMs so that they don't confuse later passes.
1249 See PR 21299. */
1250 if (asm_noperands (PATTERN (insn)) >= 0)
1251 {
1252 extract_insn (insn);
1253 if (!constrain_operands (1))
1254 {
1255 error_for_asm (insn,
1256 "%<asm%> operand has impossible constraints");
1257 delete_insn (insn);
1258 continue;
1259 }
1260 }
1261 }
1262
1263 /* If we are doing generic stack checking, give a warning if this
1264 function's frame size is larger than we expect. */
1265 if (flag_stack_check == GENERIC_STACK_CHECK)
1266 {
1267 HOST_WIDE_INT size = get_frame_size () + STACK_CHECK_FIXED_FRAME_SIZE;
1268 static int verbose_warned = 0;
1269
1270 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1271 if (df_regs_ever_live_p (i) && ! fixed_regs[i] && call_used_regs[i])
1272 size += UNITS_PER_WORD;
1273
1274 if (size > STACK_CHECK_MAX_FRAME_SIZE)
1275 {
1276 warning (0, "frame size too large for reliable stack checking");
1277 if (! verbose_warned)
1278 {
1279 warning (0, "try reducing the number of local variables");
1280 verbose_warned = 1;
1281 }
1282 }
1283 }
1284
1285 free (temp_pseudo_reg_arr);
1286
1287 /* Indicate that we no longer have known memory locations or constants. */
1288 free_reg_equiv ();
1289 reg_equiv_init = 0;
1290 free (reg_max_ref_width);
1291 free (reg_old_renumber);
1292 free (pseudo_previous_regs);
1293 free (pseudo_forbidden_regs);
1294
1295 CLEAR_HARD_REG_SET (used_spill_regs);
1296 for (i = 0; i < n_spills; i++)
1297 SET_HARD_REG_BIT (used_spill_regs, spill_regs[i]);
1298
1299 /* Free all the insn_chain structures at once. */
1300 obstack_free (&reload_obstack, reload_startobj);
1301 unused_insn_chains = 0;
1302 fixup_abnormal_edges ();
1303
1304 /* Replacing pseudos with their memory equivalents might have
1305 created shared rtx. Subsequent passes would get confused
1306 by this, so unshare everything here. */
1307 unshare_all_rtl_again (first);
1308
1309 #ifdef STACK_BOUNDARY
1310 /* init_emit has set the alignment of the hard frame pointer
1311 to STACK_BOUNDARY. It is very likely no longer valid if
1312 the hard frame pointer was used for register allocation. */
1313 if (!frame_pointer_needed)
1314 REGNO_POINTER_ALIGN (HARD_FRAME_POINTER_REGNUM) = BITS_PER_UNIT;
1315 #endif
1316
1317 VEC_free (rtx_p, heap, substitute_stack);
1318
1319 gcc_assert (bitmap_empty_p (&spilled_pseudos));
1320
1321 return failure;
1322 }
1323
1324 /* Yet another special case. Unfortunately, reg-stack forces people to
1325 write incorrect clobbers in asm statements. These clobbers must not
1326 cause the register to appear in bad_spill_regs, otherwise we'll call
1327 fatal_insn later. We clear the corresponding regnos in the live
1328 register sets to avoid this.
1329 The whole thing is rather sick, I'm afraid. */
1330
1331 static void
1332 maybe_fix_stack_asms (void)
1333 {
1334 #ifdef STACK_REGS
1335 const char *constraints[MAX_RECOG_OPERANDS];
1336 enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
1337 struct insn_chain *chain;
1338
1339 for (chain = reload_insn_chain; chain != 0; chain = chain->next)
1340 {
1341 int i, noperands;
1342 HARD_REG_SET clobbered, allowed;
1343 rtx pat;
1344
1345 if (! INSN_P (chain->insn)
1346 || (noperands = asm_noperands (PATTERN (chain->insn))) < 0)
1347 continue;
1348 pat = PATTERN (chain->insn);
1349 if (GET_CODE (pat) != PARALLEL)
1350 continue;
1351
1352 CLEAR_HARD_REG_SET (clobbered);
1353 CLEAR_HARD_REG_SET (allowed);
1354
1355 /* First, make a mask of all stack regs that are clobbered. */
1356 for (i = 0; i < XVECLEN (pat, 0); i++)
1357 {
1358 rtx t = XVECEXP (pat, 0, i);
1359 if (GET_CODE (t) == CLOBBER && STACK_REG_P (XEXP (t, 0)))
1360 SET_HARD_REG_BIT (clobbered, REGNO (XEXP (t, 0)));
1361 }
1362
1363 /* Get the operand values and constraints out of the insn. */
1364 decode_asm_operands (pat, recog_data.operand, recog_data.operand_loc,
1365 constraints, operand_mode, NULL);
1366
1367 /* For every operand, see what registers are allowed. */
1368 for (i = 0; i < noperands; i++)
1369 {
1370 const char *p = constraints[i];
1371 /* For every alternative, we compute the class of registers allowed
1372 for reloading in CLS, and merge its contents into the reg set
1373 ALLOWED. */
1374 int cls = (int) NO_REGS;
1375
1376 for (;;)
1377 {
1378 char c = *p;
1379
1380 if (c == '\0' || c == ',' || c == '#')
1381 {
1382 /* End of one alternative - mark the regs in the current
1383 class, and reset the class. */
1384 IOR_HARD_REG_SET (allowed, reg_class_contents[cls]);
1385 cls = NO_REGS;
1386 p++;
1387 if (c == '#')
1388 do {
1389 c = *p++;
1390 } while (c != '\0' && c != ',');
1391 if (c == '\0')
1392 break;
1393 continue;
1394 }
1395
1396 switch (c)
1397 {
1398 case '=': case '+': case '*': case '%': case '?': case '!':
1399 case '0': case '1': case '2': case '3': case '4': case '<':
1400 case '>': case 'V': case 'o': case '&': case 'E': case 'F':
1401 case 's': case 'i': case 'n': case 'X': case 'I': case 'J':
1402 case 'K': case 'L': case 'M': case 'N': case 'O': case 'P':
1403 case TARGET_MEM_CONSTRAINT:
1404 break;
1405
1406 case 'p':
1407 cls = (int) reg_class_subunion[cls]
1408 [(int) base_reg_class (VOIDmode, ADDRESS, SCRATCH)];
1409 break;
1410
1411 case 'g':
1412 case 'r':
1413 cls = (int) reg_class_subunion[cls][(int) GENERAL_REGS];
1414 break;
1415
1416 default:
1417 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1418 cls = (int) reg_class_subunion[cls]
1419 [(int) base_reg_class (VOIDmode, ADDRESS, SCRATCH)];
1420 else
1421 cls = (int) reg_class_subunion[cls]
1422 [(int) REG_CLASS_FROM_CONSTRAINT (c, p)];
1423 }
1424 p += CONSTRAINT_LEN (c, p);
1425 }
1426 }
1427 /* Those of the registers which are clobbered, but allowed by the
1428 constraints, must be usable as reload registers. So clear them
1429 out of the life information. */
1430 AND_HARD_REG_SET (allowed, clobbered);
1431 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1432 if (TEST_HARD_REG_BIT (allowed, i))
1433 {
1434 CLEAR_REGNO_REG_SET (&chain->live_throughout, i);
1435 CLEAR_REGNO_REG_SET (&chain->dead_or_set, i);
1436 }
1437 }
1438
1439 #endif
1440 }
1441
1442 /* Copy the global variables n_reloads and rld into the corresponding elts
1443 of CHAIN. */
1444 static void
1445 copy_reloads (struct insn_chain *chain)
1446 {
1447 chain->n_reloads = n_reloads;
1448 chain->rld = XOBNEWVEC (&reload_obstack, struct reload, n_reloads);
1449 memcpy (chain->rld, rld, n_reloads * sizeof (struct reload));
1450 reload_insn_firstobj = XOBNEWVAR (&reload_obstack, char, 0);
1451 }
1452
1453 /* Walk the chain of insns, and determine for each whether it needs reloads
1454 and/or eliminations. Build the corresponding insns_need_reload list, and
1455 set something_needs_elimination as appropriate. */
1456 static void
1457 calculate_needs_all_insns (int global)
1458 {
1459 struct insn_chain **pprev_reload = &insns_need_reload;
1460 struct insn_chain *chain, *next = 0;
1461
1462 something_needs_elimination = 0;
1463
1464 reload_insn_firstobj = XOBNEWVAR (&reload_obstack, char, 0);
1465 for (chain = reload_insn_chain; chain != 0; chain = next)
1466 {
1467 rtx insn = chain->insn;
1468
1469 next = chain->next;
1470
1471 /* Clear out the shortcuts. */
1472 chain->n_reloads = 0;
1473 chain->need_elim = 0;
1474 chain->need_reload = 0;
1475 chain->need_operand_change = 0;
1476
1477 /* If this is a label, a JUMP_INSN, or has REG_NOTES (which might
1478 include REG_LABEL_OPERAND and REG_LABEL_TARGET), we need to see
1479 what effects this has on the known offsets at labels. */
1480
1481 if (LABEL_P (insn) || JUMP_P (insn)
1482 || (INSN_P (insn) && REG_NOTES (insn) != 0))
1483 set_label_offsets (insn, insn, 0);
1484
1485 if (INSN_P (insn))
1486 {
1487 rtx old_body = PATTERN (insn);
1488 int old_code = INSN_CODE (insn);
1489 rtx old_notes = REG_NOTES (insn);
1490 int did_elimination = 0;
1491 int operands_changed = 0;
1492 rtx set = single_set (insn);
1493
1494 /* Skip insns that only set an equivalence. */
1495 if (set && REG_P (SET_DEST (set))
1496 && reg_renumber[REGNO (SET_DEST (set))] < 0
1497 && (reg_equiv_constant[REGNO (SET_DEST (set))]
1498 || (reg_equiv_invariant[REGNO (SET_DEST (set))]))
1499 && reg_equiv_init[REGNO (SET_DEST (set))])
1500 continue;
1501
1502 /* If needed, eliminate any eliminable registers. */
1503 if (num_eliminable || num_eliminable_invariants)
1504 did_elimination = eliminate_regs_in_insn (insn, 0);
1505
1506 /* Analyze the instruction. */
1507 operands_changed = find_reloads (insn, 0, spill_indirect_levels,
1508 global, spill_reg_order);
1509
1510 /* If a no-op set needs more than one reload, this is likely
1511 to be something that needs input address reloads. We
1512 can't get rid of this cleanly later, and it is of no use
1513 anyway, so discard it now.
1514 We only do this when expensive_optimizations is enabled,
1515 since this complements reload inheritance / output
1516 reload deletion, and it can make debugging harder. */
1517 if (flag_expensive_optimizations && n_reloads > 1)
1518 {
1519 rtx set = single_set (insn);
1520 if (set
1521 &&
1522 ((SET_SRC (set) == SET_DEST (set)
1523 && REG_P (SET_SRC (set))
1524 && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
1525 || (REG_P (SET_SRC (set)) && REG_P (SET_DEST (set))
1526 && reg_renumber[REGNO (SET_SRC (set))] < 0
1527 && reg_renumber[REGNO (SET_DEST (set))] < 0
1528 && reg_equiv_memory_loc[REGNO (SET_SRC (set))] != NULL
1529 && reg_equiv_memory_loc[REGNO (SET_DEST (set))] != NULL
1530 && rtx_equal_p (reg_equiv_memory_loc
1531 [REGNO (SET_SRC (set))],
1532 reg_equiv_memory_loc
1533 [REGNO (SET_DEST (set))]))))
1534 {
1535 if (ira_conflicts_p)
1536 /* Inform IRA about the insn deletion. */
1537 ira_mark_memory_move_deletion (REGNO (SET_DEST (set)),
1538 REGNO (SET_SRC (set)));
1539 delete_insn (insn);
1540 /* Delete it from the reload chain. */
1541 if (chain->prev)
1542 chain->prev->next = next;
1543 else
1544 reload_insn_chain = next;
1545 if (next)
1546 next->prev = chain->prev;
1547 chain->next = unused_insn_chains;
1548 unused_insn_chains = chain;
1549 continue;
1550 }
1551 }
1552 if (num_eliminable)
1553 update_eliminable_offsets ();
1554
1555 /* Remember for later shortcuts which insns had any reloads or
1556 register eliminations. */
1557 chain->need_elim = did_elimination;
1558 chain->need_reload = n_reloads > 0;
1559 chain->need_operand_change = operands_changed;
1560
1561 /* Discard any register replacements done. */
1562 if (did_elimination)
1563 {
1564 obstack_free (&reload_obstack, reload_insn_firstobj);
1565 PATTERN (insn) = old_body;
1566 INSN_CODE (insn) = old_code;
1567 REG_NOTES (insn) = old_notes;
1568 something_needs_elimination = 1;
1569 }
1570
1571 something_needs_operands_changed |= operands_changed;
1572
1573 if (n_reloads != 0)
1574 {
1575 copy_reloads (chain);
1576 *pprev_reload = chain;
1577 pprev_reload = &chain->next_need_reload;
1578 }
1579 }
1580 }
1581 *pprev_reload = 0;
1582 }
1583
1584 /* This function is called from the register allocator to set up estimates
1585 for the cost of eliminating pseudos which have REG_EQUIV equivalences to
1586 an invariant. The structure is similar to calculate_needs_all_insns. */
1587
1588 void
1589 calculate_elim_costs_all_insns (void)
1590 {
1591 int *reg_equiv_init_cost;
1592 basic_block bb;
1593 int i;
1594
1595 reg_equiv_init_cost = XCNEWVEC (int, max_regno);
1596 init_elim_table ();
1597 init_eliminable_invariants (get_insns (), false);
1598
1599 set_initial_elim_offsets ();
1600 set_initial_label_offsets ();
1601
1602 FOR_EACH_BB (bb)
1603 {
1604 rtx insn;
1605 elim_bb = bb;
1606
1607 FOR_BB_INSNS (bb, insn)
1608 {
1609 /* If this is a label, a JUMP_INSN, or has REG_NOTES (which might
1610 include REG_LABEL_OPERAND and REG_LABEL_TARGET), we need to see
1611 what effects this has on the known offsets at labels. */
1612
1613 if (LABEL_P (insn) || JUMP_P (insn)
1614 || (INSN_P (insn) && REG_NOTES (insn) != 0))
1615 set_label_offsets (insn, insn, 0);
1616
1617 if (INSN_P (insn))
1618 {
1619 rtx set = single_set (insn);
1620
1621 /* Skip insns that only set an equivalence. */
1622 if (set && REG_P (SET_DEST (set))
1623 && reg_renumber[REGNO (SET_DEST (set))] < 0
1624 && (reg_equiv_constant[REGNO (SET_DEST (set))]
1625 || (reg_equiv_invariant[REGNO (SET_DEST (set))])))
1626 {
1627 unsigned regno = REGNO (SET_DEST (set));
1628 rtx init = reg_equiv_init[regno];
1629 if (init)
1630 {
1631 rtx t = eliminate_regs_1 (SET_SRC (set), VOIDmode, insn,
1632 false, true);
1633 int cost = rtx_cost (t, SET,
1634 optimize_bb_for_speed_p (bb));
1635 int freq = REG_FREQ_FROM_BB (bb);
1636
1637 reg_equiv_init_cost[regno] = cost * freq;
1638 continue;
1639 }
1640 }
1641 /* If needed, eliminate any eliminable registers. */
1642 if (num_eliminable || num_eliminable_invariants)
1643 elimination_costs_in_insn (insn);
1644
1645 if (num_eliminable)
1646 update_eliminable_offsets ();
1647 }
1648 }
1649 }
1650 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1651 {
1652 if (reg_equiv_invariant[i])
1653 {
1654 if (reg_equiv_init[i])
1655 {
1656 int cost = reg_equiv_init_cost[i];
1657 if (dump_file)
1658 fprintf (dump_file,
1659 "Reg %d has equivalence, initial gains %d\n", i, cost);
1660 if (cost != 0)
1661 ira_adjust_equiv_reg_cost (i, cost);
1662 }
1663 else
1664 {
1665 if (dump_file)
1666 fprintf (dump_file,
1667 "Reg %d had equivalence, but can't be eliminated\n",
1668 i);
1669 ira_adjust_equiv_reg_cost (i, 0);
1670 }
1671 }
1672 }
1673
1674 free_reg_equiv ();
1675 free (reg_equiv_init_cost);
1676 }
1677
1678 /* Comparison function for qsort to decide which of two reloads
1679 should be handled first. *P1 and *P2 are the reload numbers. */
1680
1681 static int
1682 reload_reg_class_lower (const void *r1p, const void *r2p)
1683 {
1684 int r1 = *(const short *) r1p, r2 = *(const short *) r2p;
1685 int t;
1686
1687 /* Consider required reloads before optional ones. */
1688 t = rld[r1].optional - rld[r2].optional;
1689 if (t != 0)
1690 return t;
1691
1692 /* Count all solitary classes before non-solitary ones. */
1693 t = ((reg_class_size[(int) rld[r2].rclass] == 1)
1694 - (reg_class_size[(int) rld[r1].rclass] == 1));
1695 if (t != 0)
1696 return t;
1697
1698 /* Aside from solitaires, consider all multi-reg groups first. */
1699 t = rld[r2].nregs - rld[r1].nregs;
1700 if (t != 0)
1701 return t;
1702
1703 /* Consider reloads in order of increasing reg-class number. */
1704 t = (int) rld[r1].rclass - (int) rld[r2].rclass;
1705 if (t != 0)
1706 return t;
1707
1708 /* If reloads are equally urgent, sort by reload number,
1709 so that the results of qsort leave nothing to chance. */
1710 return r1 - r2;
1711 }
1712
1713 /* The cost of spilling each hard reg. */
1714 static int spill_cost[FIRST_PSEUDO_REGISTER];
1715
1716 /* When spilling multiple hard registers, we use SPILL_COST for the first
1717 spilled hard reg and SPILL_ADD_COST for subsequent regs. SPILL_ADD_COST
1718 only the first hard reg for a multi-reg pseudo. */
1719 static int spill_add_cost[FIRST_PSEUDO_REGISTER];
1720
1721 /* Map of hard regno to pseudo regno currently occupying the hard
1722 reg. */
1723 static int hard_regno_to_pseudo_regno[FIRST_PSEUDO_REGISTER];
1724
1725 /* Update the spill cost arrays, considering that pseudo REG is live. */
1726
1727 static void
1728 count_pseudo (int reg)
1729 {
1730 int freq = REG_FREQ (reg);
1731 int r = reg_renumber[reg];
1732 int nregs;
1733
1734 if (REGNO_REG_SET_P (&pseudos_counted, reg)
1735 || REGNO_REG_SET_P (&spilled_pseudos, reg)
1736 /* Ignore spilled pseudo-registers which can be here only if IRA
1737 is used. */
1738 || (ira_conflicts_p && r < 0))
1739 return;
1740
1741 SET_REGNO_REG_SET (&pseudos_counted, reg);
1742
1743 gcc_assert (r >= 0);
1744
1745 spill_add_cost[r] += freq;
1746 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (reg)];
1747 while (nregs-- > 0)
1748 {
1749 hard_regno_to_pseudo_regno[r + nregs] = reg;
1750 spill_cost[r + nregs] += freq;
1751 }
1752 }
1753
1754 /* Calculate the SPILL_COST and SPILL_ADD_COST arrays and determine the
1755 contents of BAD_SPILL_REGS for the insn described by CHAIN. */
1756
1757 static void
1758 order_regs_for_reload (struct insn_chain *chain)
1759 {
1760 unsigned i;
1761 HARD_REG_SET used_by_pseudos;
1762 HARD_REG_SET used_by_pseudos2;
1763 reg_set_iterator rsi;
1764
1765 COPY_HARD_REG_SET (bad_spill_regs, fixed_reg_set);
1766
1767 memset (spill_cost, 0, sizeof spill_cost);
1768 memset (spill_add_cost, 0, sizeof spill_add_cost);
1769 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1770 hard_regno_to_pseudo_regno[i] = -1;
1771
1772 /* Count number of uses of each hard reg by pseudo regs allocated to it
1773 and then order them by decreasing use. First exclude hard registers
1774 that are live in or across this insn. */
1775
1776 REG_SET_TO_HARD_REG_SET (used_by_pseudos, &chain->live_throughout);
1777 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, &chain->dead_or_set);
1778 IOR_HARD_REG_SET (bad_spill_regs, used_by_pseudos);
1779 IOR_HARD_REG_SET (bad_spill_regs, used_by_pseudos2);
1780
1781 /* Now find out which pseudos are allocated to it, and update
1782 hard_reg_n_uses. */
1783 CLEAR_REG_SET (&pseudos_counted);
1784
1785 EXECUTE_IF_SET_IN_REG_SET
1786 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, i, rsi)
1787 {
1788 count_pseudo (i);
1789 }
1790 EXECUTE_IF_SET_IN_REG_SET
1791 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, i, rsi)
1792 {
1793 count_pseudo (i);
1794 }
1795 CLEAR_REG_SET (&pseudos_counted);
1796 }
1797
1798 /* Vector of reload-numbers showing the order in which the reloads should
1799 be processed. */
1800 static short reload_order[MAX_RELOADS];
1801
1802 /* This is used to keep track of the spill regs used in one insn. */
1803 static HARD_REG_SET used_spill_regs_local;
1804
1805 /* We decided to spill hard register SPILLED, which has a size of
1806 SPILLED_NREGS. Determine how pseudo REG, which is live during the insn,
1807 is affected. We will add it to SPILLED_PSEUDOS if necessary, and we will
1808 update SPILL_COST/SPILL_ADD_COST. */
1809
1810 static void
1811 count_spilled_pseudo (int spilled, int spilled_nregs, int reg)
1812 {
1813 int freq = REG_FREQ (reg);
1814 int r = reg_renumber[reg];
1815 int nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (reg)];
1816
1817 /* Ignore spilled pseudo-registers which can be here only if IRA is
1818 used. */
1819 if ((ira_conflicts_p && r < 0)
1820 || REGNO_REG_SET_P (&spilled_pseudos, reg)
1821 || spilled + spilled_nregs <= r || r + nregs <= spilled)
1822 return;
1823
1824 SET_REGNO_REG_SET (&spilled_pseudos, reg);
1825
1826 spill_add_cost[r] -= freq;
1827 while (nregs-- > 0)
1828 {
1829 hard_regno_to_pseudo_regno[r + nregs] = -1;
1830 spill_cost[r + nregs] -= freq;
1831 }
1832 }
1833
1834 /* Find reload register to use for reload number ORDER. */
1835
1836 static int
1837 find_reg (struct insn_chain *chain, int order)
1838 {
1839 int rnum = reload_order[order];
1840 struct reload *rl = rld + rnum;
1841 int best_cost = INT_MAX;
1842 int best_reg = -1;
1843 unsigned int i, j, n;
1844 int k;
1845 HARD_REG_SET not_usable;
1846 HARD_REG_SET used_by_other_reload;
1847 reg_set_iterator rsi;
1848 static int regno_pseudo_regs[FIRST_PSEUDO_REGISTER];
1849 static int best_regno_pseudo_regs[FIRST_PSEUDO_REGISTER];
1850
1851 COPY_HARD_REG_SET (not_usable, bad_spill_regs);
1852 IOR_HARD_REG_SET (not_usable, bad_spill_regs_global);
1853 IOR_COMPL_HARD_REG_SET (not_usable, reg_class_contents[rl->rclass]);
1854
1855 CLEAR_HARD_REG_SET (used_by_other_reload);
1856 for (k = 0; k < order; k++)
1857 {
1858 int other = reload_order[k];
1859
1860 if (rld[other].regno >= 0 && reloads_conflict (other, rnum))
1861 for (j = 0; j < rld[other].nregs; j++)
1862 SET_HARD_REG_BIT (used_by_other_reload, rld[other].regno + j);
1863 }
1864
1865 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1866 {
1867 #ifdef REG_ALLOC_ORDER
1868 unsigned int regno = reg_alloc_order[i];
1869 #else
1870 unsigned int regno = i;
1871 #endif
1872
1873 if (! TEST_HARD_REG_BIT (not_usable, regno)
1874 && ! TEST_HARD_REG_BIT (used_by_other_reload, regno)
1875 && HARD_REGNO_MODE_OK (regno, rl->mode))
1876 {
1877 int this_cost = spill_cost[regno];
1878 int ok = 1;
1879 unsigned int this_nregs = hard_regno_nregs[regno][rl->mode];
1880
1881 for (j = 1; j < this_nregs; j++)
1882 {
1883 this_cost += spill_add_cost[regno + j];
1884 if ((TEST_HARD_REG_BIT (not_usable, regno + j))
1885 || TEST_HARD_REG_BIT (used_by_other_reload, regno + j))
1886 ok = 0;
1887 }
1888 if (! ok)
1889 continue;
1890
1891 if (ira_conflicts_p)
1892 {
1893 /* Ask IRA to find a better pseudo-register for
1894 spilling. */
1895 for (n = j = 0; j < this_nregs; j++)
1896 {
1897 int r = hard_regno_to_pseudo_regno[regno + j];
1898
1899 if (r < 0)
1900 continue;
1901 if (n == 0 || regno_pseudo_regs[n - 1] != r)
1902 regno_pseudo_regs[n++] = r;
1903 }
1904 regno_pseudo_regs[n++] = -1;
1905 if (best_reg < 0
1906 || ira_better_spill_reload_regno_p (regno_pseudo_regs,
1907 best_regno_pseudo_regs,
1908 rl->in, rl->out,
1909 chain->insn))
1910 {
1911 best_reg = regno;
1912 for (j = 0;; j++)
1913 {
1914 best_regno_pseudo_regs[j] = regno_pseudo_regs[j];
1915 if (regno_pseudo_regs[j] < 0)
1916 break;
1917 }
1918 }
1919 continue;
1920 }
1921
1922 if (rl->in && REG_P (rl->in) && REGNO (rl->in) == regno)
1923 this_cost--;
1924 if (rl->out && REG_P (rl->out) && REGNO (rl->out) == regno)
1925 this_cost--;
1926 if (this_cost < best_cost
1927 /* Among registers with equal cost, prefer caller-saved ones, or
1928 use REG_ALLOC_ORDER if it is defined. */
1929 || (this_cost == best_cost
1930 #ifdef REG_ALLOC_ORDER
1931 && (inv_reg_alloc_order[regno]
1932 < inv_reg_alloc_order[best_reg])
1933 #else
1934 && call_used_regs[regno]
1935 && ! call_used_regs[best_reg]
1936 #endif
1937 ))
1938 {
1939 best_reg = regno;
1940 best_cost = this_cost;
1941 }
1942 }
1943 }
1944 if (best_reg == -1)
1945 return 0;
1946
1947 if (dump_file)
1948 fprintf (dump_file, "Using reg %d for reload %d\n", best_reg, rnum);
1949
1950 rl->nregs = hard_regno_nregs[best_reg][rl->mode];
1951 rl->regno = best_reg;
1952
1953 EXECUTE_IF_SET_IN_REG_SET
1954 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, j, rsi)
1955 {
1956 count_spilled_pseudo (best_reg, rl->nregs, j);
1957 }
1958
1959 EXECUTE_IF_SET_IN_REG_SET
1960 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, j, rsi)
1961 {
1962 count_spilled_pseudo (best_reg, rl->nregs, j);
1963 }
1964
1965 for (i = 0; i < rl->nregs; i++)
1966 {
1967 gcc_assert (spill_cost[best_reg + i] == 0);
1968 gcc_assert (spill_add_cost[best_reg + i] == 0);
1969 gcc_assert (hard_regno_to_pseudo_regno[best_reg + i] == -1);
1970 SET_HARD_REG_BIT (used_spill_regs_local, best_reg + i);
1971 }
1972 return 1;
1973 }
1974
1975 /* Find more reload regs to satisfy the remaining need of an insn, which
1976 is given by CHAIN.
1977 Do it by ascending class number, since otherwise a reg
1978 might be spilled for a big class and might fail to count
1979 for a smaller class even though it belongs to that class. */
1980
1981 static void
1982 find_reload_regs (struct insn_chain *chain)
1983 {
1984 int i;
1985
1986 /* In order to be certain of getting the registers we need,
1987 we must sort the reloads into order of increasing register class.
1988 Then our grabbing of reload registers will parallel the process
1989 that provided the reload registers. */
1990 for (i = 0; i < chain->n_reloads; i++)
1991 {
1992 /* Show whether this reload already has a hard reg. */
1993 if (chain->rld[i].reg_rtx)
1994 {
1995 int regno = REGNO (chain->rld[i].reg_rtx);
1996 chain->rld[i].regno = regno;
1997 chain->rld[i].nregs
1998 = hard_regno_nregs[regno][GET_MODE (chain->rld[i].reg_rtx)];
1999 }
2000 else
2001 chain->rld[i].regno = -1;
2002 reload_order[i] = i;
2003 }
2004
2005 n_reloads = chain->n_reloads;
2006 memcpy (rld, chain->rld, n_reloads * sizeof (struct reload));
2007
2008 CLEAR_HARD_REG_SET (used_spill_regs_local);
2009
2010 if (dump_file)
2011 fprintf (dump_file, "Spilling for insn %d.\n", INSN_UID (chain->insn));
2012
2013 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
2014
2015 /* Compute the order of preference for hard registers to spill. */
2016
2017 order_regs_for_reload (chain);
2018
2019 for (i = 0; i < n_reloads; i++)
2020 {
2021 int r = reload_order[i];
2022
2023 /* Ignore reloads that got marked inoperative. */
2024 if ((rld[r].out != 0 || rld[r].in != 0 || rld[r].secondary_p)
2025 && ! rld[r].optional
2026 && rld[r].regno == -1)
2027 if (! find_reg (chain, i))
2028 {
2029 if (dump_file)
2030 fprintf (dump_file, "reload failure for reload %d\n", r);
2031 spill_failure (chain->insn, rld[r].rclass);
2032 failure = 1;
2033 return;
2034 }
2035 }
2036
2037 COPY_HARD_REG_SET (chain->used_spill_regs, used_spill_regs_local);
2038 IOR_HARD_REG_SET (used_spill_regs, used_spill_regs_local);
2039
2040 memcpy (chain->rld, rld, n_reloads * sizeof (struct reload));
2041 }
2042
2043 static void
2044 select_reload_regs (void)
2045 {
2046 struct insn_chain *chain;
2047
2048 /* Try to satisfy the needs for each insn. */
2049 for (chain = insns_need_reload; chain != 0;
2050 chain = chain->next_need_reload)
2051 find_reload_regs (chain);
2052 }
2053
2054 /* Delete all insns that were inserted by emit_caller_save_insns during
2055 this iteration. */
2056 static void
2057 delete_caller_save_insns (void)
2058 {
2059 struct insn_chain *c = reload_insn_chain;
2060
2061 while (c != 0)
2062 {
2063 while (c != 0 && c->is_caller_save_insn)
2064 {
2065 struct insn_chain *next = c->next;
2066 rtx insn = c->insn;
2067
2068 if (c == reload_insn_chain)
2069 reload_insn_chain = next;
2070 delete_insn (insn);
2071
2072 if (next)
2073 next->prev = c->prev;
2074 if (c->prev)
2075 c->prev->next = next;
2076 c->next = unused_insn_chains;
2077 unused_insn_chains = c;
2078 c = next;
2079 }
2080 if (c != 0)
2081 c = c->next;
2082 }
2083 }
2084
2085 /* Handle the failure to find a register to spill.
2086 INSN should be one of the insns which needed this particular spill reg. */
2087
2088 static void
2089 spill_failure (rtx insn, enum reg_class rclass)
2090 {
2091 if (asm_noperands (PATTERN (insn)) >= 0)
2092 error_for_asm (insn, "can%'t find a register in class %qs while "
2093 "reloading %<asm%>",
2094 reg_class_names[rclass]);
2095 else
2096 {
2097 error ("unable to find a register to spill in class %qs",
2098 reg_class_names[rclass]);
2099
2100 if (dump_file)
2101 {
2102 fprintf (dump_file, "\nReloads for insn # %d\n", INSN_UID (insn));
2103 debug_reload_to_stream (dump_file);
2104 }
2105 fatal_insn ("this is the insn:", insn);
2106 }
2107 }
2108
2109 /* Delete an unneeded INSN and any previous insns who sole purpose is loading
2110 data that is dead in INSN. */
2111
2112 static void
2113 delete_dead_insn (rtx insn)
2114 {
2115 rtx prev = prev_active_insn (insn);
2116 rtx prev_dest;
2117
2118 /* If the previous insn sets a register that dies in our insn, delete it
2119 too. */
2120 if (prev && GET_CODE (PATTERN (prev)) == SET
2121 && (prev_dest = SET_DEST (PATTERN (prev)), REG_P (prev_dest))
2122 && reg_mentioned_p (prev_dest, PATTERN (insn))
2123 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
2124 && ! side_effects_p (SET_SRC (PATTERN (prev))))
2125 delete_dead_insn (prev);
2126
2127 SET_INSN_DELETED (insn);
2128 }
2129
2130 /* Modify the home of pseudo-reg I.
2131 The new home is present in reg_renumber[I].
2132
2133 FROM_REG may be the hard reg that the pseudo-reg is being spilled from;
2134 or it may be -1, meaning there is none or it is not relevant.
2135 This is used so that all pseudos spilled from a given hard reg
2136 can share one stack slot. */
2137
2138 static void
2139 alter_reg (int i, int from_reg, bool dont_share_p)
2140 {
2141 /* When outputting an inline function, this can happen
2142 for a reg that isn't actually used. */
2143 if (regno_reg_rtx[i] == 0)
2144 return;
2145
2146 /* If the reg got changed to a MEM at rtl-generation time,
2147 ignore it. */
2148 if (!REG_P (regno_reg_rtx[i]))
2149 return;
2150
2151 /* Modify the reg-rtx to contain the new hard reg
2152 number or else to contain its pseudo reg number. */
2153 SET_REGNO (regno_reg_rtx[i],
2154 reg_renumber[i] >= 0 ? reg_renumber[i] : i);
2155
2156 /* If we have a pseudo that is needed but has no hard reg or equivalent,
2157 allocate a stack slot for it. */
2158
2159 if (reg_renumber[i] < 0
2160 && REG_N_REFS (i) > 0
2161 && reg_equiv_constant[i] == 0
2162 && (reg_equiv_invariant[i] == 0 || reg_equiv_init[i] == 0)
2163 && reg_equiv_memory_loc[i] == 0)
2164 {
2165 rtx x = NULL_RTX;
2166 enum machine_mode mode = GET_MODE (regno_reg_rtx[i]);
2167 unsigned int inherent_size = PSEUDO_REGNO_BYTES (i);
2168 unsigned int inherent_align = GET_MODE_ALIGNMENT (mode);
2169 unsigned int total_size = MAX (inherent_size, reg_max_ref_width[i]);
2170 unsigned int min_align = reg_max_ref_width[i] * BITS_PER_UNIT;
2171 int adjust = 0;
2172
2173 something_was_spilled = true;
2174
2175 if (ira_conflicts_p)
2176 {
2177 /* Mark the spill for IRA. */
2178 SET_REGNO_REG_SET (&spilled_pseudos, i);
2179 if (!dont_share_p)
2180 x = ira_reuse_stack_slot (i, inherent_size, total_size);
2181 }
2182
2183 if (x)
2184 ;
2185
2186 /* Each pseudo reg has an inherent size which comes from its own mode,
2187 and a total size which provides room for paradoxical subregs
2188 which refer to the pseudo reg in wider modes.
2189
2190 We can use a slot already allocated if it provides both
2191 enough inherent space and enough total space.
2192 Otherwise, we allocate a new slot, making sure that it has no less
2193 inherent space, and no less total space, then the previous slot. */
2194 else if (from_reg == -1 || (!dont_share_p && ira_conflicts_p))
2195 {
2196 rtx stack_slot;
2197
2198 /* No known place to spill from => no slot to reuse. */
2199 x = assign_stack_local (mode, total_size,
2200 min_align > inherent_align
2201 || total_size > inherent_size ? -1 : 0);
2202
2203 stack_slot = x;
2204
2205 /* Cancel the big-endian correction done in assign_stack_local.
2206 Get the address of the beginning of the slot. This is so we
2207 can do a big-endian correction unconditionally below. */
2208 if (BYTES_BIG_ENDIAN)
2209 {
2210 adjust = inherent_size - total_size;
2211 if (adjust)
2212 stack_slot
2213 = adjust_address_nv (x, mode_for_size (total_size
2214 * BITS_PER_UNIT,
2215 MODE_INT, 1),
2216 adjust);
2217 }
2218
2219 if (! dont_share_p && ira_conflicts_p)
2220 /* Inform IRA about allocation a new stack slot. */
2221 ira_mark_new_stack_slot (stack_slot, i, total_size);
2222 }
2223
2224 /* Reuse a stack slot if possible. */
2225 else if (spill_stack_slot[from_reg] != 0
2226 && spill_stack_slot_width[from_reg] >= total_size
2227 && (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2228 >= inherent_size)
2229 && MEM_ALIGN (spill_stack_slot[from_reg]) >= min_align)
2230 x = spill_stack_slot[from_reg];
2231
2232 /* Allocate a bigger slot. */
2233 else
2234 {
2235 /* Compute maximum size needed, both for inherent size
2236 and for total size. */
2237 rtx stack_slot;
2238
2239 if (spill_stack_slot[from_reg])
2240 {
2241 if (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2242 > inherent_size)
2243 mode = GET_MODE (spill_stack_slot[from_reg]);
2244 if (spill_stack_slot_width[from_reg] > total_size)
2245 total_size = spill_stack_slot_width[from_reg];
2246 if (MEM_ALIGN (spill_stack_slot[from_reg]) > min_align)
2247 min_align = MEM_ALIGN (spill_stack_slot[from_reg]);
2248 }
2249
2250 /* Make a slot with that size. */
2251 x = assign_stack_local (mode, total_size,
2252 min_align > inherent_align
2253 || total_size > inherent_size ? -1 : 0);
2254 stack_slot = x;
2255
2256 /* Cancel the big-endian correction done in assign_stack_local.
2257 Get the address of the beginning of the slot. This is so we
2258 can do a big-endian correction unconditionally below. */
2259 if (BYTES_BIG_ENDIAN)
2260 {
2261 adjust = GET_MODE_SIZE (mode) - total_size;
2262 if (adjust)
2263 stack_slot
2264 = adjust_address_nv (x, mode_for_size (total_size
2265 * BITS_PER_UNIT,
2266 MODE_INT, 1),
2267 adjust);
2268 }
2269
2270 spill_stack_slot[from_reg] = stack_slot;
2271 spill_stack_slot_width[from_reg] = total_size;
2272 }
2273
2274 /* On a big endian machine, the "address" of the slot
2275 is the address of the low part that fits its inherent mode. */
2276 if (BYTES_BIG_ENDIAN && inherent_size < total_size)
2277 adjust += (total_size - inherent_size);
2278
2279 /* If we have any adjustment to make, or if the stack slot is the
2280 wrong mode, make a new stack slot. */
2281 x = adjust_address_nv (x, GET_MODE (regno_reg_rtx[i]), adjust);
2282
2283 /* Set all of the memory attributes as appropriate for a spill. */
2284 set_mem_attrs_for_spill (x);
2285
2286 /* Save the stack slot for later. */
2287 reg_equiv_memory_loc[i] = x;
2288 }
2289 }
2290
2291 /* Mark the slots in regs_ever_live for the hard regs used by
2292 pseudo-reg number REGNO, accessed in MODE. */
2293
2294 static void
2295 mark_home_live_1 (int regno, enum machine_mode mode)
2296 {
2297 int i, lim;
2298
2299 i = reg_renumber[regno];
2300 if (i < 0)
2301 return;
2302 lim = end_hard_regno (mode, i);
2303 while (i < lim)
2304 df_set_regs_ever_live(i++, true);
2305 }
2306
2307 /* Mark the slots in regs_ever_live for the hard regs
2308 used by pseudo-reg number REGNO. */
2309
2310 void
2311 mark_home_live (int regno)
2312 {
2313 if (reg_renumber[regno] >= 0)
2314 mark_home_live_1 (regno, PSEUDO_REGNO_MODE (regno));
2315 }
2316
2317 /* This function handles the tracking of elimination offsets around branches.
2318
2319 X is a piece of RTL being scanned.
2320
2321 INSN is the insn that it came from, if any.
2322
2323 INITIAL_P is nonzero if we are to set the offset to be the initial
2324 offset and zero if we are setting the offset of the label to be the
2325 current offset. */
2326
2327 static void
2328 set_label_offsets (rtx x, rtx insn, int initial_p)
2329 {
2330 enum rtx_code code = GET_CODE (x);
2331 rtx tem;
2332 unsigned int i;
2333 struct elim_table *p;
2334
2335 switch (code)
2336 {
2337 case LABEL_REF:
2338 if (LABEL_REF_NONLOCAL_P (x))
2339 return;
2340
2341 x = XEXP (x, 0);
2342
2343 /* ... fall through ... */
2344
2345 case CODE_LABEL:
2346 /* If we know nothing about this label, set the desired offsets. Note
2347 that this sets the offset at a label to be the offset before a label
2348 if we don't know anything about the label. This is not correct for
2349 the label after a BARRIER, but is the best guess we can make. If
2350 we guessed wrong, we will suppress an elimination that might have
2351 been possible had we been able to guess correctly. */
2352
2353 if (! offsets_known_at[CODE_LABEL_NUMBER (x) - first_label_num])
2354 {
2355 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2356 offsets_at[CODE_LABEL_NUMBER (x) - first_label_num][i]
2357 = (initial_p ? reg_eliminate[i].initial_offset
2358 : reg_eliminate[i].offset);
2359 offsets_known_at[CODE_LABEL_NUMBER (x) - first_label_num] = 1;
2360 }
2361
2362 /* Otherwise, if this is the definition of a label and it is
2363 preceded by a BARRIER, set our offsets to the known offset of
2364 that label. */
2365
2366 else if (x == insn
2367 && (tem = prev_nonnote_insn (insn)) != 0
2368 && BARRIER_P (tem))
2369 set_offsets_for_label (insn);
2370 else
2371 /* If neither of the above cases is true, compare each offset
2372 with those previously recorded and suppress any eliminations
2373 where the offsets disagree. */
2374
2375 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2376 if (offsets_at[CODE_LABEL_NUMBER (x) - first_label_num][i]
2377 != (initial_p ? reg_eliminate[i].initial_offset
2378 : reg_eliminate[i].offset))
2379 reg_eliminate[i].can_eliminate = 0;
2380
2381 return;
2382
2383 case JUMP_INSN:
2384 set_label_offsets (PATTERN (insn), insn, initial_p);
2385
2386 /* ... fall through ... */
2387
2388 case INSN:
2389 case CALL_INSN:
2390 /* Any labels mentioned in REG_LABEL_OPERAND notes can be branched
2391 to indirectly and hence must have all eliminations at their
2392 initial offsets. */
2393 for (tem = REG_NOTES (x); tem; tem = XEXP (tem, 1))
2394 if (REG_NOTE_KIND (tem) == REG_LABEL_OPERAND)
2395 set_label_offsets (XEXP (tem, 0), insn, 1);
2396 return;
2397
2398 case PARALLEL:
2399 case ADDR_VEC:
2400 case ADDR_DIFF_VEC:
2401 /* Each of the labels in the parallel or address vector must be
2402 at their initial offsets. We want the first field for PARALLEL
2403 and ADDR_VEC and the second field for ADDR_DIFF_VEC. */
2404
2405 for (i = 0; i < (unsigned) XVECLEN (x, code == ADDR_DIFF_VEC); i++)
2406 set_label_offsets (XVECEXP (x, code == ADDR_DIFF_VEC, i),
2407 insn, initial_p);
2408 return;
2409
2410 case SET:
2411 /* We only care about setting PC. If the source is not RETURN,
2412 IF_THEN_ELSE, or a label, disable any eliminations not at
2413 their initial offsets. Similarly if any arm of the IF_THEN_ELSE
2414 isn't one of those possibilities. For branches to a label,
2415 call ourselves recursively.
2416
2417 Note that this can disable elimination unnecessarily when we have
2418 a non-local goto since it will look like a non-constant jump to
2419 someplace in the current function. This isn't a significant
2420 problem since such jumps will normally be when all elimination
2421 pairs are back to their initial offsets. */
2422
2423 if (SET_DEST (x) != pc_rtx)
2424 return;
2425
2426 switch (GET_CODE (SET_SRC (x)))
2427 {
2428 case PC:
2429 case RETURN:
2430 return;
2431
2432 case LABEL_REF:
2433 set_label_offsets (SET_SRC (x), insn, initial_p);
2434 return;
2435
2436 case IF_THEN_ELSE:
2437 tem = XEXP (SET_SRC (x), 1);
2438 if (GET_CODE (tem) == LABEL_REF)
2439 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2440 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2441 break;
2442
2443 tem = XEXP (SET_SRC (x), 2);
2444 if (GET_CODE (tem) == LABEL_REF)
2445 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2446 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2447 break;
2448 return;
2449
2450 default:
2451 break;
2452 }
2453
2454 /* If we reach here, all eliminations must be at their initial
2455 offset because we are doing a jump to a variable address. */
2456 for (p = reg_eliminate; p < &reg_eliminate[NUM_ELIMINABLE_REGS]; p++)
2457 if (p->offset != p->initial_offset)
2458 p->can_eliminate = 0;
2459 break;
2460
2461 default:
2462 break;
2463 }
2464 }
2465
2466 /* Called through for_each_rtx, this function examines every reg that occurs
2467 in PX and adjusts the costs for its elimination which are gathered by IRA.
2468 DATA is the insn in which PX occurs. We do not recurse into MEM
2469 expressions. */
2470
2471 static int
2472 note_reg_elim_costly (rtx *px, void *data)
2473 {
2474 rtx insn = (rtx)data;
2475 rtx x = *px;
2476
2477 if (MEM_P (x))
2478 return -1;
2479
2480 if (REG_P (x)
2481 && REGNO (x) >= FIRST_PSEUDO_REGISTER
2482 && reg_equiv_init[REGNO (x)]
2483 && reg_equiv_invariant[REGNO (x)])
2484 {
2485 rtx t = reg_equiv_invariant[REGNO (x)];
2486 rtx new_rtx = eliminate_regs_1 (t, Pmode, insn, true, true);
2487 int cost = rtx_cost (new_rtx, SET, optimize_bb_for_speed_p (elim_bb));
2488 int freq = REG_FREQ_FROM_BB (elim_bb);
2489
2490 if (cost != 0)
2491 ira_adjust_equiv_reg_cost (REGNO (x), -cost * freq);
2492 }
2493 return 0;
2494 }
2495
2496 /* Scan X and replace any eliminable registers (such as fp) with a
2497 replacement (such as sp), plus an offset.
2498
2499 MEM_MODE is the mode of an enclosing MEM. We need this to know how
2500 much to adjust a register for, e.g., PRE_DEC. Also, if we are inside a
2501 MEM, we are allowed to replace a sum of a register and the constant zero
2502 with the register, which we cannot do outside a MEM. In addition, we need
2503 to record the fact that a register is referenced outside a MEM.
2504
2505 If INSN is an insn, it is the insn containing X. If we replace a REG
2506 in a SET_DEST with an equivalent MEM and INSN is nonzero, write a
2507 CLOBBER of the pseudo after INSN so find_equiv_regs will know that
2508 the REG is being modified.
2509
2510 Alternatively, INSN may be a note (an EXPR_LIST or INSN_LIST).
2511 That's used when we eliminate in expressions stored in notes.
2512 This means, do not set ref_outside_mem even if the reference
2513 is outside of MEMs.
2514
2515 If FOR_COSTS is true, we are being called before reload in order to
2516 estimate the costs of keeping registers with an equivalence unallocated.
2517
2518 REG_EQUIV_MEM and REG_EQUIV_ADDRESS contain address that have had
2519 replacements done assuming all offsets are at their initial values. If
2520 they are not, or if REG_EQUIV_ADDRESS is nonzero for a pseudo we
2521 encounter, return the actual location so that find_reloads will do
2522 the proper thing. */
2523
2524 static rtx
2525 eliminate_regs_1 (rtx x, enum machine_mode mem_mode, rtx insn,
2526 bool may_use_invariant, bool for_costs)
2527 {
2528 enum rtx_code code = GET_CODE (x);
2529 struct elim_table *ep;
2530 int regno;
2531 rtx new_rtx;
2532 int i, j;
2533 const char *fmt;
2534 int copied = 0;
2535
2536 if (! current_function_decl)
2537 return x;
2538
2539 switch (code)
2540 {
2541 case CONST_INT:
2542 case CONST_DOUBLE:
2543 case CONST_FIXED:
2544 case CONST_VECTOR:
2545 case CONST:
2546 case SYMBOL_REF:
2547 case CODE_LABEL:
2548 case PC:
2549 case CC0:
2550 case ASM_INPUT:
2551 case ADDR_VEC:
2552 case ADDR_DIFF_VEC:
2553 case RETURN:
2554 return x;
2555
2556 case REG:
2557 regno = REGNO (x);
2558
2559 /* First handle the case where we encounter a bare register that
2560 is eliminable. Replace it with a PLUS. */
2561 if (regno < FIRST_PSEUDO_REGISTER)
2562 {
2563 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2564 ep++)
2565 if (ep->from_rtx == x && ep->can_eliminate)
2566 return plus_constant (ep->to_rtx, ep->previous_offset);
2567
2568 }
2569 else if (reg_renumber && reg_renumber[regno] < 0
2570 && reg_equiv_invariant && reg_equiv_invariant[regno])
2571 {
2572 if (may_use_invariant || (insn && DEBUG_INSN_P (insn)))
2573 return eliminate_regs_1 (copy_rtx (reg_equiv_invariant[regno]),
2574 mem_mode, insn, true, for_costs);
2575 /* There exists at least one use of REGNO that cannot be
2576 eliminated. Prevent the defining insn from being deleted. */
2577 reg_equiv_init[regno] = NULL_RTX;
2578 if (!for_costs)
2579 alter_reg (regno, -1, true);
2580 }
2581 return x;
2582
2583 /* You might think handling MINUS in a manner similar to PLUS is a
2584 good idea. It is not. It has been tried multiple times and every
2585 time the change has had to have been reverted.
2586
2587 Other parts of reload know a PLUS is special (gen_reload for example)
2588 and require special code to handle code a reloaded PLUS operand.
2589
2590 Also consider backends where the flags register is clobbered by a
2591 MINUS, but we can emit a PLUS that does not clobber flags (IA-32,
2592 lea instruction comes to mind). If we try to reload a MINUS, we
2593 may kill the flags register that was holding a useful value.
2594
2595 So, please before trying to handle MINUS, consider reload as a
2596 whole instead of this little section as well as the backend issues. */
2597 case PLUS:
2598 /* If this is the sum of an eliminable register and a constant, rework
2599 the sum. */
2600 if (REG_P (XEXP (x, 0))
2601 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2602 && CONSTANT_P (XEXP (x, 1)))
2603 {
2604 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2605 ep++)
2606 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2607 {
2608 /* The only time we want to replace a PLUS with a REG (this
2609 occurs when the constant operand of the PLUS is the negative
2610 of the offset) is when we are inside a MEM. We won't want
2611 to do so at other times because that would change the
2612 structure of the insn in a way that reload can't handle.
2613 We special-case the commonest situation in
2614 eliminate_regs_in_insn, so just replace a PLUS with a
2615 PLUS here, unless inside a MEM. */
2616 if (mem_mode != 0 && CONST_INT_P (XEXP (x, 1))
2617 && INTVAL (XEXP (x, 1)) == - ep->previous_offset)
2618 return ep->to_rtx;
2619 else
2620 return gen_rtx_PLUS (Pmode, ep->to_rtx,
2621 plus_constant (XEXP (x, 1),
2622 ep->previous_offset));
2623 }
2624
2625 /* If the register is not eliminable, we are done since the other
2626 operand is a constant. */
2627 return x;
2628 }
2629
2630 /* If this is part of an address, we want to bring any constant to the
2631 outermost PLUS. We will do this by doing register replacement in
2632 our operands and seeing if a constant shows up in one of them.
2633
2634 Note that there is no risk of modifying the structure of the insn,
2635 since we only get called for its operands, thus we are either
2636 modifying the address inside a MEM, or something like an address
2637 operand of a load-address insn. */
2638
2639 {
2640 rtx new0 = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, true,
2641 for_costs);
2642 rtx new1 = eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, true,
2643 for_costs);
2644
2645 if (reg_renumber && (new0 != XEXP (x, 0) || new1 != XEXP (x, 1)))
2646 {
2647 /* If one side is a PLUS and the other side is a pseudo that
2648 didn't get a hard register but has a reg_equiv_constant,
2649 we must replace the constant here since it may no longer
2650 be in the position of any operand. */
2651 if (GET_CODE (new0) == PLUS && REG_P (new1)
2652 && REGNO (new1) >= FIRST_PSEUDO_REGISTER
2653 && reg_renumber[REGNO (new1)] < 0
2654 && reg_equiv_constant != 0
2655 && reg_equiv_constant[REGNO (new1)] != 0)
2656 new1 = reg_equiv_constant[REGNO (new1)];
2657 else if (GET_CODE (new1) == PLUS && REG_P (new0)
2658 && REGNO (new0) >= FIRST_PSEUDO_REGISTER
2659 && reg_renumber[REGNO (new0)] < 0
2660 && reg_equiv_constant[REGNO (new0)] != 0)
2661 new0 = reg_equiv_constant[REGNO (new0)];
2662
2663 new_rtx = form_sum (GET_MODE (x), new0, new1);
2664
2665 /* As above, if we are not inside a MEM we do not want to
2666 turn a PLUS into something else. We might try to do so here
2667 for an addition of 0 if we aren't optimizing. */
2668 if (! mem_mode && GET_CODE (new_rtx) != PLUS)
2669 return gen_rtx_PLUS (GET_MODE (x), new_rtx, const0_rtx);
2670 else
2671 return new_rtx;
2672 }
2673 }
2674 return x;
2675
2676 case MULT:
2677 /* If this is the product of an eliminable register and a
2678 constant, apply the distribute law and move the constant out
2679 so that we have (plus (mult ..) ..). This is needed in order
2680 to keep load-address insns valid. This case is pathological.
2681 We ignore the possibility of overflow here. */
2682 if (REG_P (XEXP (x, 0))
2683 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2684 && CONST_INT_P (XEXP (x, 1)))
2685 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2686 ep++)
2687 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2688 {
2689 if (! mem_mode
2690 /* Refs inside notes or in DEBUG_INSNs don't count for
2691 this purpose. */
2692 && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2693 || GET_CODE (insn) == INSN_LIST
2694 || DEBUG_INSN_P (insn))))
2695 ep->ref_outside_mem = 1;
2696
2697 return
2698 plus_constant (gen_rtx_MULT (Pmode, ep->to_rtx, XEXP (x, 1)),
2699 ep->previous_offset * INTVAL (XEXP (x, 1)));
2700 }
2701
2702 /* ... fall through ... */
2703
2704 case CALL:
2705 case COMPARE:
2706 /* See comments before PLUS about handling MINUS. */
2707 case MINUS:
2708 case DIV: case UDIV:
2709 case MOD: case UMOD:
2710 case AND: case IOR: case XOR:
2711 case ROTATERT: case ROTATE:
2712 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
2713 case NE: case EQ:
2714 case GE: case GT: case GEU: case GTU:
2715 case LE: case LT: case LEU: case LTU:
2716 {
2717 rtx new0 = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, false,
2718 for_costs);
2719 rtx new1 = XEXP (x, 1)
2720 ? eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, false,
2721 for_costs) : 0;
2722
2723 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2724 return gen_rtx_fmt_ee (code, GET_MODE (x), new0, new1);
2725 }
2726 return x;
2727
2728 case EXPR_LIST:
2729 /* If we have something in XEXP (x, 0), the usual case, eliminate it. */
2730 if (XEXP (x, 0))
2731 {
2732 new_rtx = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, true,
2733 for_costs);
2734 if (new_rtx != XEXP (x, 0))
2735 {
2736 /* If this is a REG_DEAD note, it is not valid anymore.
2737 Using the eliminated version could result in creating a
2738 REG_DEAD note for the stack or frame pointer. */
2739 if (REG_NOTE_KIND (x) == REG_DEAD)
2740 return (XEXP (x, 1)
2741 ? eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, true,
2742 for_costs)
2743 : NULL_RTX);
2744
2745 x = alloc_reg_note (REG_NOTE_KIND (x), new_rtx, XEXP (x, 1));
2746 }
2747 }
2748
2749 /* ... fall through ... */
2750
2751 case INSN_LIST:
2752 /* Now do eliminations in the rest of the chain. If this was
2753 an EXPR_LIST, this might result in allocating more memory than is
2754 strictly needed, but it simplifies the code. */
2755 if (XEXP (x, 1))
2756 {
2757 new_rtx = eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, true,
2758 for_costs);
2759 if (new_rtx != XEXP (x, 1))
2760 return
2761 gen_rtx_fmt_ee (GET_CODE (x), GET_MODE (x), XEXP (x, 0), new_rtx);
2762 }
2763 return x;
2764
2765 case PRE_INC:
2766 case POST_INC:
2767 case PRE_DEC:
2768 case POST_DEC:
2769 /* We do not support elimination of a register that is modified.
2770 elimination_effects has already make sure that this does not
2771 happen. */
2772 return x;
2773
2774 case PRE_MODIFY:
2775 case POST_MODIFY:
2776 /* We do not support elimination of a register that is modified.
2777 elimination_effects has already make sure that this does not
2778 happen. The only remaining case we need to consider here is
2779 that the increment value may be an eliminable register. */
2780 if (GET_CODE (XEXP (x, 1)) == PLUS
2781 && XEXP (XEXP (x, 1), 0) == XEXP (x, 0))
2782 {
2783 rtx new_rtx = eliminate_regs_1 (XEXP (XEXP (x, 1), 1), mem_mode,
2784 insn, true, for_costs);
2785
2786 if (new_rtx != XEXP (XEXP (x, 1), 1))
2787 return gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (x, 0),
2788 gen_rtx_PLUS (GET_MODE (x),
2789 XEXP (x, 0), new_rtx));
2790 }
2791 return x;
2792
2793 case STRICT_LOW_PART:
2794 case NEG: case NOT:
2795 case SIGN_EXTEND: case ZERO_EXTEND:
2796 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2797 case FLOAT: case FIX:
2798 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2799 case ABS:
2800 case SQRT:
2801 case FFS:
2802 case CLZ:
2803 case CTZ:
2804 case POPCOUNT:
2805 case PARITY:
2806 case BSWAP:
2807 new_rtx = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, false,
2808 for_costs);
2809 if (new_rtx != XEXP (x, 0))
2810 return gen_rtx_fmt_e (code, GET_MODE (x), new_rtx);
2811 return x;
2812
2813 case SUBREG:
2814 /* Similar to above processing, but preserve SUBREG_BYTE.
2815 Convert (subreg (mem)) to (mem) if not paradoxical.
2816 Also, if we have a non-paradoxical (subreg (pseudo)) and the
2817 pseudo didn't get a hard reg, we must replace this with the
2818 eliminated version of the memory location because push_reload
2819 may do the replacement in certain circumstances. */
2820 if (REG_P (SUBREG_REG (x))
2821 && (GET_MODE_SIZE (GET_MODE (x))
2822 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2823 && reg_equiv_memory_loc != 0
2824 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
2825 {
2826 new_rtx = SUBREG_REG (x);
2827 }
2828 else
2829 new_rtx = eliminate_regs_1 (SUBREG_REG (x), mem_mode, insn, false,
2830 for_costs);
2831
2832 if (new_rtx != SUBREG_REG (x))
2833 {
2834 int x_size = GET_MODE_SIZE (GET_MODE (x));
2835 int new_size = GET_MODE_SIZE (GET_MODE (new_rtx));
2836
2837 if (MEM_P (new_rtx)
2838 && ((x_size < new_size
2839 #ifdef WORD_REGISTER_OPERATIONS
2840 /* On these machines, combine can create rtl of the form
2841 (set (subreg:m1 (reg:m2 R) 0) ...)
2842 where m1 < m2, and expects something interesting to
2843 happen to the entire word. Moreover, it will use the
2844 (reg:m2 R) later, expecting all bits to be preserved.
2845 So if the number of words is the same, preserve the
2846 subreg so that push_reload can see it. */
2847 && ! ((x_size - 1) / UNITS_PER_WORD
2848 == (new_size -1 ) / UNITS_PER_WORD)
2849 #endif
2850 )
2851 || x_size == new_size)
2852 )
2853 return adjust_address_nv (new_rtx, GET_MODE (x), SUBREG_BYTE (x));
2854 else
2855 return gen_rtx_SUBREG (GET_MODE (x), new_rtx, SUBREG_BYTE (x));
2856 }
2857
2858 return x;
2859
2860 case MEM:
2861 /* Our only special processing is to pass the mode of the MEM to our
2862 recursive call and copy the flags. While we are here, handle this
2863 case more efficiently. */
2864
2865 new_rtx = eliminate_regs_1 (XEXP (x, 0), GET_MODE (x), insn, true,
2866 for_costs);
2867 if (for_costs
2868 && memory_address_p (GET_MODE (x), XEXP (x, 0))
2869 && !memory_address_p (GET_MODE (x), new_rtx))
2870 for_each_rtx (&XEXP (x, 0), note_reg_elim_costly, insn);
2871
2872 return replace_equiv_address_nv (x, new_rtx);
2873
2874 case USE:
2875 /* Handle insn_list USE that a call to a pure function may generate. */
2876 new_rtx = eliminate_regs_1 (XEXP (x, 0), VOIDmode, insn, false,
2877 for_costs);
2878 if (new_rtx != XEXP (x, 0))
2879 return gen_rtx_USE (GET_MODE (x), new_rtx);
2880 return x;
2881
2882 case CLOBBER:
2883 case ASM_OPERANDS:
2884 gcc_assert (insn && DEBUG_INSN_P (insn));
2885 break;
2886
2887 case SET:
2888 gcc_unreachable ();
2889
2890 default:
2891 break;
2892 }
2893
2894 /* Process each of our operands recursively. If any have changed, make a
2895 copy of the rtx. */
2896 fmt = GET_RTX_FORMAT (code);
2897 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2898 {
2899 if (*fmt == 'e')
2900 {
2901 new_rtx = eliminate_regs_1 (XEXP (x, i), mem_mode, insn, false,
2902 for_costs);
2903 if (new_rtx != XEXP (x, i) && ! copied)
2904 {
2905 x = shallow_copy_rtx (x);
2906 copied = 1;
2907 }
2908 XEXP (x, i) = new_rtx;
2909 }
2910 else if (*fmt == 'E')
2911 {
2912 int copied_vec = 0;
2913 for (j = 0; j < XVECLEN (x, i); j++)
2914 {
2915 new_rtx = eliminate_regs_1 (XVECEXP (x, i, j), mem_mode, insn, false,
2916 for_costs);
2917 if (new_rtx != XVECEXP (x, i, j) && ! copied_vec)
2918 {
2919 rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
2920 XVEC (x, i)->elem);
2921 if (! copied)
2922 {
2923 x = shallow_copy_rtx (x);
2924 copied = 1;
2925 }
2926 XVEC (x, i) = new_v;
2927 copied_vec = 1;
2928 }
2929 XVECEXP (x, i, j) = new_rtx;
2930 }
2931 }
2932 }
2933
2934 return x;
2935 }
2936
2937 rtx
2938 eliminate_regs (rtx x, enum machine_mode mem_mode, rtx insn)
2939 {
2940 return eliminate_regs_1 (x, mem_mode, insn, false, false);
2941 }
2942
2943 /* Scan rtx X for modifications of elimination target registers. Update
2944 the table of eliminables to reflect the changed state. MEM_MODE is
2945 the mode of an enclosing MEM rtx, or VOIDmode if not within a MEM. */
2946
2947 static void
2948 elimination_effects (rtx x, enum machine_mode mem_mode)
2949 {
2950 enum rtx_code code = GET_CODE (x);
2951 struct elim_table *ep;
2952 int regno;
2953 int i, j;
2954 const char *fmt;
2955
2956 switch (code)
2957 {
2958 case CONST_INT:
2959 case CONST_DOUBLE:
2960 case CONST_FIXED:
2961 case CONST_VECTOR:
2962 case CONST:
2963 case SYMBOL_REF:
2964 case CODE_LABEL:
2965 case PC:
2966 case CC0:
2967 case ASM_INPUT:
2968 case ADDR_VEC:
2969 case ADDR_DIFF_VEC:
2970 case RETURN:
2971 return;
2972
2973 case REG:
2974 regno = REGNO (x);
2975
2976 /* First handle the case where we encounter a bare register that
2977 is eliminable. Replace it with a PLUS. */
2978 if (regno < FIRST_PSEUDO_REGISTER)
2979 {
2980 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2981 ep++)
2982 if (ep->from_rtx == x && ep->can_eliminate)
2983 {
2984 if (! mem_mode)
2985 ep->ref_outside_mem = 1;
2986 return;
2987 }
2988
2989 }
2990 else if (reg_renumber[regno] < 0 && reg_equiv_constant
2991 && reg_equiv_constant[regno]
2992 && ! function_invariant_p (reg_equiv_constant[regno]))
2993 elimination_effects (reg_equiv_constant[regno], mem_mode);
2994 return;
2995
2996 case PRE_INC:
2997 case POST_INC:
2998 case PRE_DEC:
2999 case POST_DEC:
3000 case POST_MODIFY:
3001 case PRE_MODIFY:
3002 /* If we modify the source of an elimination rule, disable it. */
3003 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3004 if (ep->from_rtx == XEXP (x, 0))
3005 ep->can_eliminate = 0;
3006
3007 /* If we modify the target of an elimination rule by adding a constant,
3008 update its offset. If we modify the target in any other way, we'll
3009 have to disable the rule as well. */
3010 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3011 if (ep->to_rtx == XEXP (x, 0))
3012 {
3013 int size = GET_MODE_SIZE (mem_mode);
3014
3015 /* If more bytes than MEM_MODE are pushed, account for them. */
3016 #ifdef PUSH_ROUNDING
3017 if (ep->to_rtx == stack_pointer_rtx)
3018 size = PUSH_ROUNDING (size);
3019 #endif
3020 if (code == PRE_DEC || code == POST_DEC)
3021 ep->offset += size;
3022 else if (code == PRE_INC || code == POST_INC)
3023 ep->offset -= size;
3024 else if (code == PRE_MODIFY || code == POST_MODIFY)
3025 {
3026 if (GET_CODE (XEXP (x, 1)) == PLUS
3027 && XEXP (x, 0) == XEXP (XEXP (x, 1), 0)
3028 && CONST_INT_P (XEXP (XEXP (x, 1), 1)))
3029 ep->offset -= INTVAL (XEXP (XEXP (x, 1), 1));
3030 else
3031 ep->can_eliminate = 0;
3032 }
3033 }
3034
3035 /* These two aren't unary operators. */
3036 if (code == POST_MODIFY || code == PRE_MODIFY)
3037 break;
3038
3039 /* Fall through to generic unary operation case. */
3040 case STRICT_LOW_PART:
3041 case NEG: case NOT:
3042 case SIGN_EXTEND: case ZERO_EXTEND:
3043 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
3044 case FLOAT: case FIX:
3045 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
3046 case ABS:
3047 case SQRT:
3048 case FFS:
3049 case CLZ:
3050 case CTZ:
3051 case POPCOUNT:
3052 case PARITY:
3053 case BSWAP:
3054 elimination_effects (XEXP (x, 0), mem_mode);
3055 return;
3056
3057 case SUBREG:
3058 if (REG_P (SUBREG_REG (x))
3059 && (GET_MODE_SIZE (GET_MODE (x))
3060 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
3061 && reg_equiv_memory_loc != 0
3062 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
3063 return;
3064
3065 elimination_effects (SUBREG_REG (x), mem_mode);
3066 return;
3067
3068 case USE:
3069 /* If using a register that is the source of an eliminate we still
3070 think can be performed, note it cannot be performed since we don't
3071 know how this register is used. */
3072 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3073 if (ep->from_rtx == XEXP (x, 0))
3074 ep->can_eliminate = 0;
3075
3076 elimination_effects (XEXP (x, 0), mem_mode);
3077 return;
3078
3079 case CLOBBER:
3080 /* If clobbering a register that is the replacement register for an
3081 elimination we still think can be performed, note that it cannot
3082 be performed. Otherwise, we need not be concerned about it. */
3083 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3084 if (ep->to_rtx == XEXP (x, 0))
3085 ep->can_eliminate = 0;
3086
3087 elimination_effects (XEXP (x, 0), mem_mode);
3088 return;
3089
3090 case SET:
3091 /* Check for setting a register that we know about. */
3092 if (REG_P (SET_DEST (x)))
3093 {
3094 /* See if this is setting the replacement register for an
3095 elimination.
3096
3097 If DEST is the hard frame pointer, we do nothing because we
3098 assume that all assignments to the frame pointer are for
3099 non-local gotos and are being done at a time when they are valid
3100 and do not disturb anything else. Some machines want to
3101 eliminate a fake argument pointer (or even a fake frame pointer)
3102 with either the real frame or the stack pointer. Assignments to
3103 the hard frame pointer must not prevent this elimination. */
3104
3105 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3106 ep++)
3107 if (ep->to_rtx == SET_DEST (x)
3108 && SET_DEST (x) != hard_frame_pointer_rtx)
3109 {
3110 /* If it is being incremented, adjust the offset. Otherwise,
3111 this elimination can't be done. */
3112 rtx src = SET_SRC (x);
3113
3114 if (GET_CODE (src) == PLUS
3115 && XEXP (src, 0) == SET_DEST (x)
3116 && CONST_INT_P (XEXP (src, 1)))
3117 ep->offset -= INTVAL (XEXP (src, 1));
3118 else
3119 ep->can_eliminate = 0;
3120 }
3121 }
3122
3123 elimination_effects (SET_DEST (x), VOIDmode);
3124 elimination_effects (SET_SRC (x), VOIDmode);
3125 return;
3126
3127 case MEM:
3128 /* Our only special processing is to pass the mode of the MEM to our
3129 recursive call. */
3130 elimination_effects (XEXP (x, 0), GET_MODE (x));
3131 return;
3132
3133 default:
3134 break;
3135 }
3136
3137 fmt = GET_RTX_FORMAT (code);
3138 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3139 {
3140 if (*fmt == 'e')
3141 elimination_effects (XEXP (x, i), mem_mode);
3142 else if (*fmt == 'E')
3143 for (j = 0; j < XVECLEN (x, i); j++)
3144 elimination_effects (XVECEXP (x, i, j), mem_mode);
3145 }
3146 }
3147
3148 /* Descend through rtx X and verify that no references to eliminable registers
3149 remain. If any do remain, mark the involved register as not
3150 eliminable. */
3151
3152 static void
3153 check_eliminable_occurrences (rtx x)
3154 {
3155 const char *fmt;
3156 int i;
3157 enum rtx_code code;
3158
3159 if (x == 0)
3160 return;
3161
3162 code = GET_CODE (x);
3163
3164 if (code == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
3165 {
3166 struct elim_table *ep;
3167
3168 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3169 if (ep->from_rtx == x)
3170 ep->can_eliminate = 0;
3171 return;
3172 }
3173
3174 fmt = GET_RTX_FORMAT (code);
3175 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3176 {
3177 if (*fmt == 'e')
3178 check_eliminable_occurrences (XEXP (x, i));
3179 else if (*fmt == 'E')
3180 {
3181 int j;
3182 for (j = 0; j < XVECLEN (x, i); j++)
3183 check_eliminable_occurrences (XVECEXP (x, i, j));
3184 }
3185 }
3186 }
3187
3188 /* Scan INSN and eliminate all eliminable registers in it.
3189
3190 If REPLACE is nonzero, do the replacement destructively. Also
3191 delete the insn as dead it if it is setting an eliminable register.
3192
3193 If REPLACE is zero, do all our allocations in reload_obstack.
3194
3195 If no eliminations were done and this insn doesn't require any elimination
3196 processing (these are not identical conditions: it might be updating sp,
3197 but not referencing fp; this needs to be seen during reload_as_needed so
3198 that the offset between fp and sp can be taken into consideration), zero
3199 is returned. Otherwise, 1 is returned. */
3200
3201 static int
3202 eliminate_regs_in_insn (rtx insn, int replace)
3203 {
3204 int icode = recog_memoized (insn);
3205 rtx old_body = PATTERN (insn);
3206 int insn_is_asm = asm_noperands (old_body) >= 0;
3207 rtx old_set = single_set (insn);
3208 rtx new_body;
3209 int val = 0;
3210 int i;
3211 rtx substed_operand[MAX_RECOG_OPERANDS];
3212 rtx orig_operand[MAX_RECOG_OPERANDS];
3213 struct elim_table *ep;
3214 rtx plus_src, plus_cst_src;
3215
3216 if (! insn_is_asm && icode < 0)
3217 {
3218 gcc_assert (GET_CODE (PATTERN (insn)) == USE
3219 || GET_CODE (PATTERN (insn)) == CLOBBER
3220 || GET_CODE (PATTERN (insn)) == ADDR_VEC
3221 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
3222 || GET_CODE (PATTERN (insn)) == ASM_INPUT
3223 || DEBUG_INSN_P (insn));
3224 if (DEBUG_INSN_P (insn))
3225 INSN_VAR_LOCATION_LOC (insn)
3226 = eliminate_regs (INSN_VAR_LOCATION_LOC (insn), VOIDmode, insn);
3227 return 0;
3228 }
3229
3230 if (old_set != 0 && REG_P (SET_DEST (old_set))
3231 && REGNO (SET_DEST (old_set)) < FIRST_PSEUDO_REGISTER)
3232 {
3233 /* Check for setting an eliminable register. */
3234 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3235 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
3236 {
3237 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
3238 /* If this is setting the frame pointer register to the
3239 hardware frame pointer register and this is an elimination
3240 that will be done (tested above), this insn is really
3241 adjusting the frame pointer downward to compensate for
3242 the adjustment done before a nonlocal goto. */
3243 if (ep->from == FRAME_POINTER_REGNUM
3244 && ep->to == HARD_FRAME_POINTER_REGNUM)
3245 {
3246 rtx base = SET_SRC (old_set);
3247 rtx base_insn = insn;
3248 HOST_WIDE_INT offset = 0;
3249
3250 while (base != ep->to_rtx)
3251 {
3252 rtx prev_insn, prev_set;
3253
3254 if (GET_CODE (base) == PLUS
3255 && CONST_INT_P (XEXP (base, 1)))
3256 {
3257 offset += INTVAL (XEXP (base, 1));
3258 base = XEXP (base, 0);
3259 }
3260 else if ((prev_insn = prev_nonnote_insn (base_insn)) != 0
3261 && (prev_set = single_set (prev_insn)) != 0
3262 && rtx_equal_p (SET_DEST (prev_set), base))
3263 {
3264 base = SET_SRC (prev_set);
3265 base_insn = prev_insn;
3266 }
3267 else
3268 break;
3269 }
3270
3271 if (base == ep->to_rtx)
3272 {
3273 rtx src
3274 = plus_constant (ep->to_rtx, offset - ep->offset);
3275
3276 new_body = old_body;
3277 if (! replace)
3278 {
3279 new_body = copy_insn (old_body);
3280 if (REG_NOTES (insn))
3281 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3282 }
3283 PATTERN (insn) = new_body;
3284 old_set = single_set (insn);
3285
3286 /* First see if this insn remains valid when we
3287 make the change. If not, keep the INSN_CODE
3288 the same and let reload fit it up. */
3289 validate_change (insn, &SET_SRC (old_set), src, 1);
3290 validate_change (insn, &SET_DEST (old_set),
3291 ep->to_rtx, 1);
3292 if (! apply_change_group ())
3293 {
3294 SET_SRC (old_set) = src;
3295 SET_DEST (old_set) = ep->to_rtx;
3296 }
3297
3298 val = 1;
3299 goto done;
3300 }
3301 }
3302 #endif
3303
3304 /* In this case this insn isn't serving a useful purpose. We
3305 will delete it in reload_as_needed once we know that this
3306 elimination is, in fact, being done.
3307
3308 If REPLACE isn't set, we can't delete this insn, but needn't
3309 process it since it won't be used unless something changes. */
3310 if (replace)
3311 {
3312 delete_dead_insn (insn);
3313 return 1;
3314 }
3315 val = 1;
3316 goto done;
3317 }
3318 }
3319
3320 /* We allow one special case which happens to work on all machines we
3321 currently support: a single set with the source or a REG_EQUAL
3322 note being a PLUS of an eliminable register and a constant. */
3323 plus_src = plus_cst_src = 0;
3324 if (old_set && REG_P (SET_DEST (old_set)))
3325 {
3326 if (GET_CODE (SET_SRC (old_set)) == PLUS)
3327 plus_src = SET_SRC (old_set);
3328 /* First see if the source is of the form (plus (...) CST). */
3329 if (plus_src
3330 && CONST_INT_P (XEXP (plus_src, 1)))
3331 plus_cst_src = plus_src;
3332 else if (REG_P (SET_SRC (old_set))
3333 || plus_src)
3334 {
3335 /* Otherwise, see if we have a REG_EQUAL note of the form
3336 (plus (...) CST). */
3337 rtx links;
3338 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
3339 {
3340 if ((REG_NOTE_KIND (links) == REG_EQUAL
3341 || REG_NOTE_KIND (links) == REG_EQUIV)
3342 && GET_CODE (XEXP (links, 0)) == PLUS
3343 && CONST_INT_P (XEXP (XEXP (links, 0), 1)))
3344 {
3345 plus_cst_src = XEXP (links, 0);
3346 break;
3347 }
3348 }
3349 }
3350
3351 /* Check that the first operand of the PLUS is a hard reg or
3352 the lowpart subreg of one. */
3353 if (plus_cst_src)
3354 {
3355 rtx reg = XEXP (plus_cst_src, 0);
3356 if (GET_CODE (reg) == SUBREG && subreg_lowpart_p (reg))
3357 reg = SUBREG_REG (reg);
3358
3359 if (!REG_P (reg) || REGNO (reg) >= FIRST_PSEUDO_REGISTER)
3360 plus_cst_src = 0;
3361 }
3362 }
3363 if (plus_cst_src)
3364 {
3365 rtx reg = XEXP (plus_cst_src, 0);
3366 HOST_WIDE_INT offset = INTVAL (XEXP (plus_cst_src, 1));
3367
3368 if (GET_CODE (reg) == SUBREG)
3369 reg = SUBREG_REG (reg);
3370
3371 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3372 if (ep->from_rtx == reg && ep->can_eliminate)
3373 {
3374 rtx to_rtx = ep->to_rtx;
3375 offset += ep->offset;
3376 offset = trunc_int_for_mode (offset, GET_MODE (plus_cst_src));
3377
3378 if (GET_CODE (XEXP (plus_cst_src, 0)) == SUBREG)
3379 to_rtx = gen_lowpart (GET_MODE (XEXP (plus_cst_src, 0)),
3380 to_rtx);
3381 /* If we have a nonzero offset, and the source is already
3382 a simple REG, the following transformation would
3383 increase the cost of the insn by replacing a simple REG
3384 with (plus (reg sp) CST). So try only when we already
3385 had a PLUS before. */
3386 if (offset == 0 || plus_src)
3387 {
3388 rtx new_src = plus_constant (to_rtx, offset);
3389
3390 new_body = old_body;
3391 if (! replace)
3392 {
3393 new_body = copy_insn (old_body);
3394 if (REG_NOTES (insn))
3395 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3396 }
3397 PATTERN (insn) = new_body;
3398 old_set = single_set (insn);
3399
3400 /* First see if this insn remains valid when we make the
3401 change. If not, try to replace the whole pattern with
3402 a simple set (this may help if the original insn was a
3403 PARALLEL that was only recognized as single_set due to
3404 REG_UNUSED notes). If this isn't valid either, keep
3405 the INSN_CODE the same and let reload fix it up. */
3406 if (!validate_change (insn, &SET_SRC (old_set), new_src, 0))
3407 {
3408 rtx new_pat = gen_rtx_SET (VOIDmode,
3409 SET_DEST (old_set), new_src);
3410
3411 if (!validate_change (insn, &PATTERN (insn), new_pat, 0))
3412 SET_SRC (old_set) = new_src;
3413 }
3414 }
3415 else
3416 break;
3417
3418 val = 1;
3419 /* This can't have an effect on elimination offsets, so skip right
3420 to the end. */
3421 goto done;
3422 }
3423 }
3424
3425 /* Determine the effects of this insn on elimination offsets. */
3426 elimination_effects (old_body, VOIDmode);
3427
3428 /* Eliminate all eliminable registers occurring in operands that
3429 can be handled by reload. */
3430 extract_insn (insn);
3431 for (i = 0; i < recog_data.n_operands; i++)
3432 {
3433 orig_operand[i] = recog_data.operand[i];
3434 substed_operand[i] = recog_data.operand[i];
3435
3436 /* For an asm statement, every operand is eliminable. */
3437 if (insn_is_asm || insn_data[icode].operand[i].eliminable)
3438 {
3439 bool is_set_src, in_plus;
3440
3441 /* Check for setting a register that we know about. */
3442 if (recog_data.operand_type[i] != OP_IN
3443 && REG_P (orig_operand[i]))
3444 {
3445 /* If we are assigning to a register that can be eliminated, it
3446 must be as part of a PARALLEL, since the code above handles
3447 single SETs. We must indicate that we can no longer
3448 eliminate this reg. */
3449 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3450 ep++)
3451 if (ep->from_rtx == orig_operand[i])
3452 ep->can_eliminate = 0;
3453 }
3454
3455 /* Companion to the above plus substitution, we can allow
3456 invariants as the source of a plain move. */
3457 is_set_src = false;
3458 if (old_set
3459 && recog_data.operand_loc[i] == &SET_SRC (old_set))
3460 is_set_src = true;
3461 in_plus = false;
3462 if (plus_src
3463 && (recog_data.operand_loc[i] == &XEXP (plus_src, 0)
3464 || recog_data.operand_loc[i] == &XEXP (plus_src, 1)))
3465 in_plus = true;
3466
3467 substed_operand[i]
3468 = eliminate_regs_1 (recog_data.operand[i], VOIDmode,
3469 replace ? insn : NULL_RTX,
3470 is_set_src || in_plus, false);
3471 if (substed_operand[i] != orig_operand[i])
3472 val = 1;
3473 /* Terminate the search in check_eliminable_occurrences at
3474 this point. */
3475 *recog_data.operand_loc[i] = 0;
3476
3477 /* If an output operand changed from a REG to a MEM and INSN is an
3478 insn, write a CLOBBER insn. */
3479 if (recog_data.operand_type[i] != OP_IN
3480 && REG_P (orig_operand[i])
3481 && MEM_P (substed_operand[i])
3482 && replace)
3483 emit_insn_after (gen_clobber (orig_operand[i]), insn);
3484 }
3485 }
3486
3487 for (i = 0; i < recog_data.n_dups; i++)
3488 *recog_data.dup_loc[i]
3489 = *recog_data.operand_loc[(int) recog_data.dup_num[i]];
3490
3491 /* If any eliminable remain, they aren't eliminable anymore. */
3492 check_eliminable_occurrences (old_body);
3493
3494 /* Substitute the operands; the new values are in the substed_operand
3495 array. */
3496 for (i = 0; i < recog_data.n_operands; i++)
3497 *recog_data.operand_loc[i] = substed_operand[i];
3498 for (i = 0; i < recog_data.n_dups; i++)
3499 *recog_data.dup_loc[i] = substed_operand[(int) recog_data.dup_num[i]];
3500
3501 /* If we are replacing a body that was a (set X (plus Y Z)), try to
3502 re-recognize the insn. We do this in case we had a simple addition
3503 but now can do this as a load-address. This saves an insn in this
3504 common case.
3505 If re-recognition fails, the old insn code number will still be used,
3506 and some register operands may have changed into PLUS expressions.
3507 These will be handled by find_reloads by loading them into a register
3508 again. */
3509
3510 if (val)
3511 {
3512 /* If we aren't replacing things permanently and we changed something,
3513 make another copy to ensure that all the RTL is new. Otherwise
3514 things can go wrong if find_reload swaps commutative operands
3515 and one is inside RTL that has been copied while the other is not. */
3516 new_body = old_body;
3517 if (! replace)
3518 {
3519 new_body = copy_insn (old_body);
3520 if (REG_NOTES (insn))
3521 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3522 }
3523 PATTERN (insn) = new_body;
3524
3525 /* If we had a move insn but now we don't, rerecognize it. This will
3526 cause spurious re-recognition if the old move had a PARALLEL since
3527 the new one still will, but we can't call single_set without
3528 having put NEW_BODY into the insn and the re-recognition won't
3529 hurt in this rare case. */
3530 /* ??? Why this huge if statement - why don't we just rerecognize the
3531 thing always? */
3532 if (! insn_is_asm
3533 && old_set != 0
3534 && ((REG_P (SET_SRC (old_set))
3535 && (GET_CODE (new_body) != SET
3536 || !REG_P (SET_SRC (new_body))))
3537 /* If this was a load from or store to memory, compare
3538 the MEM in recog_data.operand to the one in the insn.
3539 If they are not equal, then rerecognize the insn. */
3540 || (old_set != 0
3541 && ((MEM_P (SET_SRC (old_set))
3542 && SET_SRC (old_set) != recog_data.operand[1])
3543 || (MEM_P (SET_DEST (old_set))
3544 && SET_DEST (old_set) != recog_data.operand[0])))
3545 /* If this was an add insn before, rerecognize. */
3546 || GET_CODE (SET_SRC (old_set)) == PLUS))
3547 {
3548 int new_icode = recog (PATTERN (insn), insn, 0);
3549 if (new_icode >= 0)
3550 INSN_CODE (insn) = new_icode;
3551 }
3552 }
3553
3554 /* Restore the old body. If there were any changes to it, we made a copy
3555 of it while the changes were still in place, so we'll correctly return
3556 a modified insn below. */
3557 if (! replace)
3558 {
3559 /* Restore the old body. */
3560 for (i = 0; i < recog_data.n_operands; i++)
3561 /* Restoring a top-level match_parallel would clobber the new_body
3562 we installed in the insn. */
3563 if (recog_data.operand_loc[i] != &PATTERN (insn))
3564 *recog_data.operand_loc[i] = orig_operand[i];
3565 for (i = 0; i < recog_data.n_dups; i++)
3566 *recog_data.dup_loc[i] = orig_operand[(int) recog_data.dup_num[i]];
3567 }
3568
3569 /* Update all elimination pairs to reflect the status after the current
3570 insn. The changes we make were determined by the earlier call to
3571 elimination_effects.
3572
3573 We also detect cases where register elimination cannot be done,
3574 namely, if a register would be both changed and referenced outside a MEM
3575 in the resulting insn since such an insn is often undefined and, even if
3576 not, we cannot know what meaning will be given to it. Note that it is
3577 valid to have a register used in an address in an insn that changes it
3578 (presumably with a pre- or post-increment or decrement).
3579
3580 If anything changes, return nonzero. */
3581
3582 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3583 {
3584 if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
3585 ep->can_eliminate = 0;
3586
3587 ep->ref_outside_mem = 0;
3588
3589 if (ep->previous_offset != ep->offset)
3590 val = 1;
3591 }
3592
3593 done:
3594 /* If we changed something, perform elimination in REG_NOTES. This is
3595 needed even when REPLACE is zero because a REG_DEAD note might refer
3596 to a register that we eliminate and could cause a different number
3597 of spill registers to be needed in the final reload pass than in
3598 the pre-passes. */
3599 if (val && REG_NOTES (insn) != 0)
3600 REG_NOTES (insn)
3601 = eliminate_regs_1 (REG_NOTES (insn), VOIDmode, REG_NOTES (insn), true,
3602 false);
3603
3604 return val;
3605 }
3606
3607 /* Like eliminate_regs_in_insn, but only estimate costs for the use of the
3608 register allocator. INSN is the instruction we need to examine, we perform
3609 eliminations in its operands and record cases where eliminating a reg with
3610 an invariant equivalence would add extra cost. */
3611
3612 static void
3613 elimination_costs_in_insn (rtx insn)
3614 {
3615 int icode = recog_memoized (insn);
3616 rtx old_body = PATTERN (insn);
3617 int insn_is_asm = asm_noperands (old_body) >= 0;
3618 rtx old_set = single_set (insn);
3619 int i;
3620 rtx orig_operand[MAX_RECOG_OPERANDS];
3621 rtx orig_dup[MAX_RECOG_OPERANDS];
3622 struct elim_table *ep;
3623 rtx plus_src, plus_cst_src;
3624 bool sets_reg_p;
3625
3626 if (! insn_is_asm && icode < 0)
3627 {
3628 gcc_assert (GET_CODE (PATTERN (insn)) == USE
3629 || GET_CODE (PATTERN (insn)) == CLOBBER
3630 || GET_CODE (PATTERN (insn)) == ADDR_VEC
3631 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
3632 || GET_CODE (PATTERN (insn)) == ASM_INPUT
3633 || DEBUG_INSN_P (insn));
3634 return;
3635 }
3636
3637 if (old_set != 0 && REG_P (SET_DEST (old_set))
3638 && REGNO (SET_DEST (old_set)) < FIRST_PSEUDO_REGISTER)
3639 {
3640 /* Check for setting an eliminable register. */
3641 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3642 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
3643 return;
3644 }
3645
3646 /* We allow one special case which happens to work on all machines we
3647 currently support: a single set with the source or a REG_EQUAL
3648 note being a PLUS of an eliminable register and a constant. */
3649 plus_src = plus_cst_src = 0;
3650 sets_reg_p = false;
3651 if (old_set && REG_P (SET_DEST (old_set)))
3652 {
3653 sets_reg_p = true;
3654 if (GET_CODE (SET_SRC (old_set)) == PLUS)
3655 plus_src = SET_SRC (old_set);
3656 /* First see if the source is of the form (plus (...) CST). */
3657 if (plus_src
3658 && CONST_INT_P (XEXP (plus_src, 1)))
3659 plus_cst_src = plus_src;
3660 else if (REG_P (SET_SRC (old_set))
3661 || plus_src)
3662 {
3663 /* Otherwise, see if we have a REG_EQUAL note of the form
3664 (plus (...) CST). */
3665 rtx links;
3666 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
3667 {
3668 if ((REG_NOTE_KIND (links) == REG_EQUAL
3669 || REG_NOTE_KIND (links) == REG_EQUIV)
3670 && GET_CODE (XEXP (links, 0)) == PLUS
3671 && CONST_INT_P (XEXP (XEXP (links, 0), 1)))
3672 {
3673 plus_cst_src = XEXP (links, 0);
3674 break;
3675 }
3676 }
3677 }
3678 }
3679
3680 /* Determine the effects of this insn on elimination offsets. */
3681 elimination_effects (old_body, VOIDmode);
3682
3683 /* Eliminate all eliminable registers occurring in operands that
3684 can be handled by reload. */
3685 extract_insn (insn);
3686 for (i = 0; i < recog_data.n_dups; i++)
3687 orig_dup[i] = *recog_data.dup_loc[i];
3688
3689 for (i = 0; i < recog_data.n_operands; i++)
3690 {
3691 orig_operand[i] = recog_data.operand[i];
3692
3693 /* For an asm statement, every operand is eliminable. */
3694 if (insn_is_asm || insn_data[icode].operand[i].eliminable)
3695 {
3696 bool is_set_src, in_plus;
3697
3698 /* Check for setting a register that we know about. */
3699 if (recog_data.operand_type[i] != OP_IN
3700 && REG_P (orig_operand[i]))
3701 {
3702 /* If we are assigning to a register that can be eliminated, it
3703 must be as part of a PARALLEL, since the code above handles
3704 single SETs. We must indicate that we can no longer
3705 eliminate this reg. */
3706 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3707 ep++)
3708 if (ep->from_rtx == orig_operand[i])
3709 ep->can_eliminate = 0;
3710 }
3711
3712 /* Companion to the above plus substitution, we can allow
3713 invariants as the source of a plain move. */
3714 is_set_src = false;
3715 if (old_set && recog_data.operand_loc[i] == &SET_SRC (old_set))
3716 is_set_src = true;
3717 if (is_set_src && !sets_reg_p)
3718 note_reg_elim_costly (&SET_SRC (old_set), insn);
3719 in_plus = false;
3720 if (plus_src && sets_reg_p
3721 && (recog_data.operand_loc[i] == &XEXP (plus_src, 0)
3722 || recog_data.operand_loc[i] == &XEXP (plus_src, 1)))
3723 in_plus = true;
3724
3725 eliminate_regs_1 (recog_data.operand[i], VOIDmode,
3726 NULL_RTX,
3727 is_set_src || in_plus, true);
3728 /* Terminate the search in check_eliminable_occurrences at
3729 this point. */
3730 *recog_data.operand_loc[i] = 0;
3731 }
3732 }
3733
3734 for (i = 0; i < recog_data.n_dups; i++)
3735 *recog_data.dup_loc[i]
3736 = *recog_data.operand_loc[(int) recog_data.dup_num[i]];
3737
3738 /* If any eliminable remain, they aren't eliminable anymore. */
3739 check_eliminable_occurrences (old_body);
3740
3741 /* Restore the old body. */
3742 for (i = 0; i < recog_data.n_operands; i++)
3743 *recog_data.operand_loc[i] = orig_operand[i];
3744 for (i = 0; i < recog_data.n_dups; i++)
3745 *recog_data.dup_loc[i] = orig_dup[i];
3746
3747 /* Update all elimination pairs to reflect the status after the current
3748 insn. The changes we make were determined by the earlier call to
3749 elimination_effects. */
3750
3751 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3752 {
3753 if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
3754 ep->can_eliminate = 0;
3755
3756 ep->ref_outside_mem = 0;
3757 }
3758
3759 return;
3760 }
3761
3762 /* Loop through all elimination pairs.
3763 Recalculate the number not at initial offset.
3764
3765 Compute the maximum offset (minimum offset if the stack does not
3766 grow downward) for each elimination pair. */
3767
3768 static void
3769 update_eliminable_offsets (void)
3770 {
3771 struct elim_table *ep;
3772
3773 num_not_at_initial_offset = 0;
3774 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3775 {
3776 ep->previous_offset = ep->offset;
3777 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3778 num_not_at_initial_offset++;
3779 }
3780 }
3781
3782 /* Given X, a SET or CLOBBER of DEST, if DEST is the target of a register
3783 replacement we currently believe is valid, mark it as not eliminable if X
3784 modifies DEST in any way other than by adding a constant integer to it.
3785
3786 If DEST is the frame pointer, we do nothing because we assume that
3787 all assignments to the hard frame pointer are nonlocal gotos and are being
3788 done at a time when they are valid and do not disturb anything else.
3789 Some machines want to eliminate a fake argument pointer with either the
3790 frame or stack pointer. Assignments to the hard frame pointer must not
3791 prevent this elimination.
3792
3793 Called via note_stores from reload before starting its passes to scan
3794 the insns of the function. */
3795
3796 static void
3797 mark_not_eliminable (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
3798 {
3799 unsigned int i;
3800
3801 /* A SUBREG of a hard register here is just changing its mode. We should
3802 not see a SUBREG of an eliminable hard register, but check just in
3803 case. */
3804 if (GET_CODE (dest) == SUBREG)
3805 dest = SUBREG_REG (dest);
3806
3807 if (dest == hard_frame_pointer_rtx)
3808 return;
3809
3810 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3811 if (reg_eliminate[i].can_eliminate && dest == reg_eliminate[i].to_rtx
3812 && (GET_CODE (x) != SET
3813 || GET_CODE (SET_SRC (x)) != PLUS
3814 || XEXP (SET_SRC (x), 0) != dest
3815 || !CONST_INT_P (XEXP (SET_SRC (x), 1))))
3816 {
3817 reg_eliminate[i].can_eliminate_previous
3818 = reg_eliminate[i].can_eliminate = 0;
3819 num_eliminable--;
3820 }
3821 }
3822
3823 /* Verify that the initial elimination offsets did not change since the
3824 last call to set_initial_elim_offsets. This is used to catch cases
3825 where something illegal happened during reload_as_needed that could
3826 cause incorrect code to be generated if we did not check for it. */
3827
3828 static bool
3829 verify_initial_elim_offsets (void)
3830 {
3831 HOST_WIDE_INT t;
3832
3833 if (!num_eliminable)
3834 return true;
3835
3836 #ifdef ELIMINABLE_REGS
3837 {
3838 struct elim_table *ep;
3839
3840 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3841 {
3842 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, t);
3843 if (t != ep->initial_offset)
3844 return false;
3845 }
3846 }
3847 #else
3848 INITIAL_FRAME_POINTER_OFFSET (t);
3849 if (t != reg_eliminate[0].initial_offset)
3850 return false;
3851 #endif
3852
3853 return true;
3854 }
3855
3856 /* Reset all offsets on eliminable registers to their initial values. */
3857
3858 static void
3859 set_initial_elim_offsets (void)
3860 {
3861 struct elim_table *ep = reg_eliminate;
3862
3863 #ifdef ELIMINABLE_REGS
3864 for (; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3865 {
3866 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->initial_offset);
3867 ep->previous_offset = ep->offset = ep->initial_offset;
3868 }
3869 #else
3870 INITIAL_FRAME_POINTER_OFFSET (ep->initial_offset);
3871 ep->previous_offset = ep->offset = ep->initial_offset;
3872 #endif
3873
3874 num_not_at_initial_offset = 0;
3875 }
3876
3877 /* Subroutine of set_initial_label_offsets called via for_each_eh_label. */
3878
3879 static void
3880 set_initial_eh_label_offset (rtx label)
3881 {
3882 set_label_offsets (label, NULL_RTX, 1);
3883 }
3884
3885 /* Initialize the known label offsets.
3886 Set a known offset for each forced label to be at the initial offset
3887 of each elimination. We do this because we assume that all
3888 computed jumps occur from a location where each elimination is
3889 at its initial offset.
3890 For all other labels, show that we don't know the offsets. */
3891
3892 static void
3893 set_initial_label_offsets (void)
3894 {
3895 rtx x;
3896 memset (offsets_known_at, 0, num_labels);
3897
3898 for (x = forced_labels; x; x = XEXP (x, 1))
3899 if (XEXP (x, 0))
3900 set_label_offsets (XEXP (x, 0), NULL_RTX, 1);
3901
3902 for_each_eh_label (set_initial_eh_label_offset);
3903 }
3904
3905 /* Set all elimination offsets to the known values for the code label given
3906 by INSN. */
3907
3908 static void
3909 set_offsets_for_label (rtx insn)
3910 {
3911 unsigned int i;
3912 int label_nr = CODE_LABEL_NUMBER (insn);
3913 struct elim_table *ep;
3914
3915 num_not_at_initial_offset = 0;
3916 for (i = 0, ep = reg_eliminate; i < NUM_ELIMINABLE_REGS; ep++, i++)
3917 {
3918 ep->offset = ep->previous_offset
3919 = offsets_at[label_nr - first_label_num][i];
3920 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3921 num_not_at_initial_offset++;
3922 }
3923 }
3924
3925 /* See if anything that happened changes which eliminations are valid.
3926 For example, on the SPARC, whether or not the frame pointer can
3927 be eliminated can depend on what registers have been used. We need
3928 not check some conditions again (such as flag_omit_frame_pointer)
3929 since they can't have changed. */
3930
3931 static void
3932 update_eliminables (HARD_REG_SET *pset)
3933 {
3934 int previous_frame_pointer_needed = frame_pointer_needed;
3935 struct elim_table *ep;
3936
3937 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3938 if ((ep->from == HARD_FRAME_POINTER_REGNUM
3939 && targetm.frame_pointer_required ())
3940 #ifdef ELIMINABLE_REGS
3941 || ! targetm.can_eliminate (ep->from, ep->to)
3942 #endif
3943 )
3944 ep->can_eliminate = 0;
3945
3946 /* Look for the case where we have discovered that we can't replace
3947 register A with register B and that means that we will now be
3948 trying to replace register A with register C. This means we can
3949 no longer replace register C with register B and we need to disable
3950 such an elimination, if it exists. This occurs often with A == ap,
3951 B == sp, and C == fp. */
3952
3953 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3954 {
3955 struct elim_table *op;
3956 int new_to = -1;
3957
3958 if (! ep->can_eliminate && ep->can_eliminate_previous)
3959 {
3960 /* Find the current elimination for ep->from, if there is a
3961 new one. */
3962 for (op = reg_eliminate;
3963 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3964 if (op->from == ep->from && op->can_eliminate)
3965 {
3966 new_to = op->to;
3967 break;
3968 }
3969
3970 /* See if there is an elimination of NEW_TO -> EP->TO. If so,
3971 disable it. */
3972 for (op = reg_eliminate;
3973 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3974 if (op->from == new_to && op->to == ep->to)
3975 op->can_eliminate = 0;
3976 }
3977 }
3978
3979 /* See if any registers that we thought we could eliminate the previous
3980 time are no longer eliminable. If so, something has changed and we
3981 must spill the register. Also, recompute the number of eliminable
3982 registers and see if the frame pointer is needed; it is if there is
3983 no elimination of the frame pointer that we can perform. */
3984
3985 frame_pointer_needed = 1;
3986 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3987 {
3988 if (ep->can_eliminate
3989 && ep->from == FRAME_POINTER_REGNUM
3990 && ep->to != HARD_FRAME_POINTER_REGNUM
3991 && (! SUPPORTS_STACK_ALIGNMENT
3992 || ! crtl->stack_realign_needed))
3993 frame_pointer_needed = 0;
3994
3995 if (! ep->can_eliminate && ep->can_eliminate_previous)
3996 {
3997 ep->can_eliminate_previous = 0;
3998 SET_HARD_REG_BIT (*pset, ep->from);
3999 num_eliminable--;
4000 }
4001 }
4002
4003 /* If we didn't need a frame pointer last time, but we do now, spill
4004 the hard frame pointer. */
4005 if (frame_pointer_needed && ! previous_frame_pointer_needed)
4006 SET_HARD_REG_BIT (*pset, HARD_FRAME_POINTER_REGNUM);
4007 }
4008
4009 /* Return true if X is used as the target register of an elimination. */
4010
4011 bool
4012 elimination_target_reg_p (rtx x)
4013 {
4014 struct elim_table *ep;
4015
4016 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
4017 if (ep->to_rtx == x && ep->can_eliminate)
4018 return true;
4019
4020 return false;
4021 }
4022
4023 /* Initialize the table of registers to eliminate.
4024 Pre-condition: global flag frame_pointer_needed has been set before
4025 calling this function. */
4026
4027 static void
4028 init_elim_table (void)
4029 {
4030 struct elim_table *ep;
4031 #ifdef ELIMINABLE_REGS
4032 const struct elim_table_1 *ep1;
4033 #endif
4034
4035 if (!reg_eliminate)
4036 reg_eliminate = XCNEWVEC (struct elim_table, NUM_ELIMINABLE_REGS);
4037
4038 num_eliminable = 0;
4039
4040 #ifdef ELIMINABLE_REGS
4041 for (ep = reg_eliminate, ep1 = reg_eliminate_1;
4042 ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++, ep1++)
4043 {
4044 ep->from = ep1->from;
4045 ep->to = ep1->to;
4046 ep->can_eliminate = ep->can_eliminate_previous
4047 = (targetm.can_eliminate (ep->from, ep->to)
4048 && ! (ep->to == STACK_POINTER_REGNUM
4049 && frame_pointer_needed
4050 && (! SUPPORTS_STACK_ALIGNMENT
4051 || ! stack_realign_fp)));
4052 }
4053 #else
4054 reg_eliminate[0].from = reg_eliminate_1[0].from;
4055 reg_eliminate[0].to = reg_eliminate_1[0].to;
4056 reg_eliminate[0].can_eliminate = reg_eliminate[0].can_eliminate_previous
4057 = ! frame_pointer_needed;
4058 #endif
4059
4060 /* Count the number of eliminable registers and build the FROM and TO
4061 REG rtx's. Note that code in gen_rtx_REG will cause, e.g.,
4062 gen_rtx_REG (Pmode, STACK_POINTER_REGNUM) to equal stack_pointer_rtx.
4063 We depend on this. */
4064 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
4065 {
4066 num_eliminable += ep->can_eliminate;
4067 ep->from_rtx = gen_rtx_REG (Pmode, ep->from);
4068 ep->to_rtx = gen_rtx_REG (Pmode, ep->to);
4069 }
4070 }
4071
4072 /* Find all the pseudo registers that didn't get hard regs
4073 but do have known equivalent constants or memory slots.
4074 These include parameters (known equivalent to parameter slots)
4075 and cse'd or loop-moved constant memory addresses.
4076
4077 Record constant equivalents in reg_equiv_constant
4078 so they will be substituted by find_reloads.
4079 Record memory equivalents in reg_mem_equiv so they can
4080 be substituted eventually by altering the REG-rtx's. */
4081
4082 static void
4083 init_eliminable_invariants (rtx first, bool do_subregs)
4084 {
4085 int i;
4086 rtx insn;
789 4087
790 reg_equiv_constant = XCNEWVEC (rtx, max_regno); 4088 reg_equiv_constant = XCNEWVEC (rtx, max_regno);
791 reg_equiv_invariant = XCNEWVEC (rtx, max_regno); 4089 reg_equiv_invariant = XCNEWVEC (rtx, max_regno);
792 reg_equiv_mem = XCNEWVEC (rtx, max_regno); 4090 reg_equiv_mem = XCNEWVEC (rtx, max_regno);
793 reg_equiv_alt_mem_list = XCNEWVEC (rtx, max_regno); 4091 reg_equiv_alt_mem_list = XCNEWVEC (rtx, max_regno);
794 reg_equiv_address = XCNEWVEC (rtx, max_regno); 4092 reg_equiv_address = XCNEWVEC (rtx, max_regno);
795 reg_max_ref_width = XCNEWVEC (unsigned int, max_regno); 4093 if (do_subregs)
796 reg_old_renumber = XCNEWVEC (short, max_regno); 4094 reg_max_ref_width = XCNEWVEC (unsigned int, max_regno);
797 memcpy (reg_old_renumber, reg_renumber, max_regno * sizeof (short)); 4095 else
798 pseudo_forbidden_regs = XNEWVEC (HARD_REG_SET, max_regno); 4096 reg_max_ref_width = NULL;
799 pseudo_previous_regs = XCNEWVEC (HARD_REG_SET, max_regno);
800
801 CLEAR_HARD_REG_SET (bad_spill_regs_global);
802
803 /* Look for REG_EQUIV notes; record what each pseudo is equivalent
804 to. Also find all paradoxical subregs and find largest such for
805 each pseudo. */
806 4097
807 num_eliminable_invariants = 0; 4098 num_eliminable_invariants = 0;
4099
4100 first_label_num = get_first_label_num ();
4101 num_labels = max_label_num () - first_label_num;
4102
4103 /* Allocate the tables used to store offset information at labels. */
4104 offsets_known_at = XNEWVEC (char, num_labels);
4105 offsets_at = (HOST_WIDE_INT (*)[NUM_ELIMINABLE_REGS]) xmalloc (num_labels * NUM_ELIMINABLE_REGS * sizeof (HOST_WIDE_INT));
4106
4107 /* Look for REG_EQUIV notes; record what each pseudo is equivalent
4108 to. If DO_SUBREGS is true, also find all paradoxical subregs and
4109 find largest such for each pseudo. FIRST is the head of the insn
4110 list. */
4111
808 for (insn = first; insn; insn = NEXT_INSN (insn)) 4112 for (insn = first; insn; insn = NEXT_INSN (insn))
809 { 4113 {
810 rtx set = single_set (insn); 4114 rtx set = single_set (insn);
811 4115
812 /* We may introduce USEs that we want to remove at the end, so 4116 /* We may introduce USEs that we want to remove at the end, so
814 previously-marked insns left by say regmove. */ 4118 previously-marked insns left by say regmove. */
815 if (INSN_P (insn) && GET_CODE (PATTERN (insn)) == USE 4119 if (INSN_P (insn) && GET_CODE (PATTERN (insn)) == USE
816 && GET_MODE (insn) != VOIDmode) 4120 && GET_MODE (insn) != VOIDmode)
817 PUT_MODE (insn, VOIDmode); 4121 PUT_MODE (insn, VOIDmode);
818 4122
819 if (NONDEBUG_INSN_P (insn)) 4123 if (do_subregs && NONDEBUG_INSN_P (insn))
820 scan_paradoxical_subregs (PATTERN (insn)); 4124 scan_paradoxical_subregs (PATTERN (insn));
821 4125
822 if (set != 0 && REG_P (SET_DEST (set))) 4126 if (set != 0 && REG_P (SET_DEST (set)))
823 { 4127 {
824 rtx note = find_reg_note (insn, REG_EQUIV, NULL_RTX); 4128 rtx note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
831 x = XEXP (note, 0); 4135 x = XEXP (note, 0);
832 4136
833 if (i <= LAST_VIRTUAL_REGISTER) 4137 if (i <= LAST_VIRTUAL_REGISTER)
834 continue; 4138 continue;
835 4139
836 if (! function_invariant_p (x) 4140 /* If flag_pic and we have constant, verify it's legitimate. */
837 || ! flag_pic 4141 if (!CONSTANT_P (x)
838 /* A function invariant is often CONSTANT_P but may 4142 || !flag_pic || LEGITIMATE_PIC_OPERAND_P (x))
839 include a register. We promise to only pass
840 CONSTANT_P objects to LEGITIMATE_PIC_OPERAND_P. */
841 || (CONSTANT_P (x)
842 && LEGITIMATE_PIC_OPERAND_P (x)))
843 { 4143 {
844 /* It can happen that a REG_EQUIV note contains a MEM 4144 /* It can happen that a REG_EQUIV note contains a MEM
845 that is not a legitimate memory operand. As later 4145 that is not a legitimate memory operand. As later
846 stages of reload assume that all addresses found 4146 stages of reload assume that all addresses found
847 in the reg_equiv_* arrays were originally legitimate, 4147 in the reg_equiv_* arrays were originally legitimate,
894 { 4194 {
895 fprintf (dump_file, "init_insns for %u: ", i); 4195 fprintf (dump_file, "init_insns for %u: ", i);
896 print_inline_rtx (dump_file, reg_equiv_init[i], 20); 4196 print_inline_rtx (dump_file, reg_equiv_init[i], 20);
897 fprintf (dump_file, "\n"); 4197 fprintf (dump_file, "\n");
898 } 4198 }
899 4199 }
900 init_elim_table (); 4200
901 4201 /* Indicate that we no longer have known memory locations or constants.
902 first_label_num = get_first_label_num (); 4202 Free all data involved in tracking these. */
903 num_labels = max_label_num () - first_label_num; 4203
904 4204 static void
905 /* Allocate the tables used to store offset information at labels. */ 4205 free_reg_equiv (void)
906 /* We used to use alloca here, but the size of what it would try to 4206 {
907 allocate would occasionally cause it to exceed the stack limit and 4207 int i;
908 cause a core dump. */ 4208
909 offsets_known_at = XNEWVEC (char, num_labels);
910 offsets_at = (HOST_WIDE_INT (*)[NUM_ELIMINABLE_REGS]) xmalloc (num_labels * NUM_ELIMINABLE_REGS * sizeof (HOST_WIDE_INT));
911
912 /* Alter each pseudo-reg rtx to contain its hard reg number. Assign
913 stack slots to the pseudos that lack hard regs or equivalents.
914 Do not touch virtual registers. */
915
916 temp_pseudo_reg_arr = XNEWVEC (int, max_regno - LAST_VIRTUAL_REGISTER - 1);
917 for (n = 0, i = LAST_VIRTUAL_REGISTER + 1; i < max_regno; i++)
918 temp_pseudo_reg_arr[n++] = i;
919
920 if (ira_conflicts_p)
921 /* Ask IRA to order pseudo-registers for better stack slot
922 sharing. */
923 ira_sort_regnos_for_alter_reg (temp_pseudo_reg_arr, n, reg_max_ref_width);
924
925 for (i = 0; i < n; i++)
926 alter_reg (temp_pseudo_reg_arr[i], -1, false);
927
928 /* If we have some registers we think can be eliminated, scan all insns to
929 see if there is an insn that sets one of these registers to something
930 other than itself plus a constant. If so, the register cannot be
931 eliminated. Doing this scan here eliminates an extra pass through the
932 main reload loop in the most common case where register elimination
933 cannot be done. */
934 for (insn = first; insn && num_eliminable; insn = NEXT_INSN (insn))
935 if (INSN_P (insn))
936 note_stores (PATTERN (insn), mark_not_eliminable, NULL);
937
938 maybe_fix_stack_asms ();
939
940 insns_need_reload = 0;
941 something_needs_elimination = 0;
942
943 /* Initialize to -1, which means take the first spill register. */
944 last_spill_reg = -1;
945
946 /* Spill any hard regs that we know we can't eliminate. */
947 CLEAR_HARD_REG_SET (used_spill_regs);
948 /* There can be multiple ways to eliminate a register;
949 they should be listed adjacently.
950 Elimination for any register fails only if all possible ways fail. */
951 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; )
952 {
953 int from = ep->from;
954 int can_eliminate = 0;
955 do
956 {
957 can_eliminate |= ep->can_eliminate;
958 ep++;
959 }
960 while (ep < &reg_eliminate[NUM_ELIMINABLE_REGS] && ep->from == from);
961 if (! can_eliminate)
962 spill_hard_reg (from, 1);
963 }
964
965 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
966 if (frame_pointer_needed)
967 spill_hard_reg (HARD_FRAME_POINTER_REGNUM, 1);
968 #endif
969 finish_spills (global);
970
971 /* From now on, we may need to generate moves differently. We may also
972 allow modifications of insns which cause them to not be recognized.
973 Any such modifications will be cleaned up during reload itself. */
974 reload_in_progress = 1;
975
976 /* This loop scans the entire function each go-round
977 and repeats until one repetition spills no additional hard regs. */
978 for (;;)
979 {
980 int something_changed;
981 int did_spill;
982 HOST_WIDE_INT starting_frame_size;
983
984 starting_frame_size = get_frame_size ();
985 something_was_spilled = false;
986
987 set_initial_elim_offsets ();
988 set_initial_label_offsets ();
989
990 /* For each pseudo register that has an equivalent location defined,
991 try to eliminate any eliminable registers (such as the frame pointer)
992 assuming initial offsets for the replacement register, which
993 is the normal case.
994
995 If the resulting location is directly addressable, substitute
996 the MEM we just got directly for the old REG.
997
998 If it is not addressable but is a constant or the sum of a hard reg
999 and constant, it is probably not addressable because the constant is
1000 out of range, in that case record the address; we will generate
1001 hairy code to compute the address in a register each time it is
1002 needed. Similarly if it is a hard register, but one that is not
1003 valid as an address register.
1004
1005 If the location is not addressable, but does not have one of the
1006 above forms, assign a stack slot. We have to do this to avoid the
1007 potential of producing lots of reloads if, e.g., a location involves
1008 a pseudo that didn't get a hard register and has an equivalent memory
1009 location that also involves a pseudo that didn't get a hard register.
1010
1011 Perhaps at some point we will improve reload_when_needed handling
1012 so this problem goes away. But that's very hairy. */
1013
1014 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1015 if (reg_renumber[i] < 0 && reg_equiv_memory_loc[i])
1016 {
1017 rtx x = eliminate_regs (reg_equiv_memory_loc[i], VOIDmode,
1018 NULL_RTX);
1019
1020 if (strict_memory_address_addr_space_p
1021 (GET_MODE (regno_reg_rtx[i]), XEXP (x, 0),
1022 MEM_ADDR_SPACE (x)))
1023 reg_equiv_mem[i] = x, reg_equiv_address[i] = 0;
1024 else if (CONSTANT_P (XEXP (x, 0))
1025 || (REG_P (XEXP (x, 0))
1026 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
1027 || (GET_CODE (XEXP (x, 0)) == PLUS
1028 && REG_P (XEXP (XEXP (x, 0), 0))
1029 && (REGNO (XEXP (XEXP (x, 0), 0))
1030 < FIRST_PSEUDO_REGISTER)
1031 && CONSTANT_P (XEXP (XEXP (x, 0), 1))))
1032 reg_equiv_address[i] = XEXP (x, 0), reg_equiv_mem[i] = 0;
1033 else
1034 {
1035 /* Make a new stack slot. Then indicate that something
1036 changed so we go back and recompute offsets for
1037 eliminable registers because the allocation of memory
1038 below might change some offset. reg_equiv_{mem,address}
1039 will be set up for this pseudo on the next pass around
1040 the loop. */
1041 reg_equiv_memory_loc[i] = 0;
1042 reg_equiv_init[i] = 0;
1043 alter_reg (i, -1, true);
1044 }
1045 }
1046
1047 if (caller_save_needed)
1048 setup_save_areas ();
1049
1050 /* If we allocated another stack slot, redo elimination bookkeeping. */
1051 if (something_was_spilled || starting_frame_size != get_frame_size ())
1052 continue;
1053 if (starting_frame_size && crtl->stack_alignment_needed)
1054 {
1055 /* If we have a stack frame, we must align it now. The
1056 stack size may be a part of the offset computation for
1057 register elimination. So if this changes the stack size,
1058 then repeat the elimination bookkeeping. We don't
1059 realign when there is no stack, as that will cause a
1060 stack frame when none is needed should
1061 STARTING_FRAME_OFFSET not be already aligned to
1062 STACK_BOUNDARY. */
1063 assign_stack_local (BLKmode, 0, crtl->stack_alignment_needed);
1064 if (starting_frame_size != get_frame_size ())
1065 continue;
1066 }
1067
1068 if (caller_save_needed)
1069 {
1070 save_call_clobbered_regs ();
1071 /* That might have allocated new insn_chain structures. */
1072 reload_firstobj = XOBNEWVAR (&reload_obstack, char, 0);
1073 }
1074
1075 calculate_needs_all_insns (global);
1076
1077 if (! ira_conflicts_p)
1078 /* Don't do it for IRA. We need this info because we don't
1079 change live_throughout and dead_or_set for chains when IRA
1080 is used. */
1081 CLEAR_REG_SET (&spilled_pseudos);
1082
1083 did_spill = 0;
1084
1085 something_changed = 0;
1086
1087 /* If we allocated any new memory locations, make another pass
1088 since it might have changed elimination offsets. */
1089 if (something_was_spilled || starting_frame_size != get_frame_size ())
1090 something_changed = 1;
1091
1092 /* Even if the frame size remained the same, we might still have
1093 changed elimination offsets, e.g. if find_reloads called
1094 force_const_mem requiring the back end to allocate a constant
1095 pool base register that needs to be saved on the stack. */
1096 else if (!verify_initial_elim_offsets ())
1097 something_changed = 1;
1098
1099 {
1100 HARD_REG_SET to_spill;
1101 CLEAR_HARD_REG_SET (to_spill);
1102 update_eliminables (&to_spill);
1103 AND_COMPL_HARD_REG_SET (used_spill_regs, to_spill);
1104
1105 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1106 if (TEST_HARD_REG_BIT (to_spill, i))
1107 {
1108 spill_hard_reg (i, 1);
1109 did_spill = 1;
1110
1111 /* Regardless of the state of spills, if we previously had
1112 a register that we thought we could eliminate, but now can
1113 not eliminate, we must run another pass.
1114
1115 Consider pseudos which have an entry in reg_equiv_* which
1116 reference an eliminable register. We must make another pass
1117 to update reg_equiv_* so that we do not substitute in the
1118 old value from when we thought the elimination could be
1119 performed. */
1120 something_changed = 1;
1121 }
1122 }
1123
1124 select_reload_regs ();
1125 if (failure)
1126 goto failed;
1127
1128 if (insns_need_reload != 0 || did_spill)
1129 something_changed |= finish_spills (global);
1130
1131 if (! something_changed)
1132 break;
1133
1134 if (caller_save_needed)
1135 delete_caller_save_insns ();
1136
1137 obstack_free (&reload_obstack, reload_firstobj);
1138 }
1139
1140 /* If global-alloc was run, notify it of any register eliminations we have
1141 done. */
1142 if (global)
1143 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1144 if (ep->can_eliminate)
1145 mark_elimination (ep->from, ep->to);
1146
1147 /* If a pseudo has no hard reg, delete the insns that made the equivalence.
1148 If that insn didn't set the register (i.e., it copied the register to
1149 memory), just delete that insn instead of the equivalencing insn plus
1150 anything now dead. If we call delete_dead_insn on that insn, we may
1151 delete the insn that actually sets the register if the register dies
1152 there and that is incorrect. */
1153
1154 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1155 {
1156 if (reg_renumber[i] < 0 && reg_equiv_init[i] != 0)
1157 {
1158 rtx list;
1159 for (list = reg_equiv_init[i]; list; list = XEXP (list, 1))
1160 {
1161 rtx equiv_insn = XEXP (list, 0);
1162
1163 /* If we already deleted the insn or if it may trap, we can't
1164 delete it. The latter case shouldn't happen, but can
1165 if an insn has a variable address, gets a REG_EH_REGION
1166 note added to it, and then gets converted into a load
1167 from a constant address. */
1168 if (NOTE_P (equiv_insn)
1169 || can_throw_internal (equiv_insn))
1170 ;
1171 else if (reg_set_p (regno_reg_rtx[i], PATTERN (equiv_insn)))
1172 delete_dead_insn (equiv_insn);
1173 else
1174 SET_INSN_DELETED (equiv_insn);
1175 }
1176 }
1177 }
1178
1179 /* Use the reload registers where necessary
1180 by generating move instructions to move the must-be-register
1181 values into or out of the reload registers. */
1182
1183 if (insns_need_reload != 0 || something_needs_elimination
1184 || something_needs_operands_changed)
1185 {
1186 HOST_WIDE_INT old_frame_size = get_frame_size ();
1187
1188 reload_as_needed (global);
1189
1190 gcc_assert (old_frame_size == get_frame_size ());
1191
1192 gcc_assert (verify_initial_elim_offsets ());
1193 }
1194
1195 /* If we were able to eliminate the frame pointer, show that it is no
1196 longer live at the start of any basic block. If it ls live by
1197 virtue of being in a pseudo, that pseudo will be marked live
1198 and hence the frame pointer will be known to be live via that
1199 pseudo. */
1200
1201 if (! frame_pointer_needed)
1202 FOR_EACH_BB (bb)
1203 bitmap_clear_bit (df_get_live_in (bb), HARD_FRAME_POINTER_REGNUM);
1204
1205 /* Come here (with failure set nonzero) if we can't get enough spill
1206 regs. */
1207 failed:
1208
1209 CLEAR_REG_SET (&changed_allocation_pseudos);
1210 CLEAR_REG_SET (&spilled_pseudos);
1211 reload_in_progress = 0;
1212
1213 /* Now eliminate all pseudo regs by modifying them into
1214 their equivalent memory references.
1215 The REG-rtx's for the pseudos are modified in place,
1216 so all insns that used to refer to them now refer to memory.
1217
1218 For a reg that has a reg_equiv_address, all those insns
1219 were changed by reloading so that no insns refer to it any longer;
1220 but the DECL_RTL of a variable decl may refer to it,
1221 and if so this causes the debugging info to mention the variable. */
1222
1223 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1224 {
1225 rtx addr = 0;
1226
1227 if (reg_equiv_mem[i])
1228 addr = XEXP (reg_equiv_mem[i], 0);
1229
1230 if (reg_equiv_address[i])
1231 addr = reg_equiv_address[i];
1232
1233 if (addr)
1234 {
1235 if (reg_renumber[i] < 0)
1236 {
1237 rtx reg = regno_reg_rtx[i];
1238
1239 REG_USERVAR_P (reg) = 0;
1240 PUT_CODE (reg, MEM);
1241 XEXP (reg, 0) = addr;
1242 if (reg_equiv_memory_loc[i])
1243 MEM_COPY_ATTRIBUTES (reg, reg_equiv_memory_loc[i]);
1244 else
1245 {
1246 MEM_IN_STRUCT_P (reg) = MEM_SCALAR_P (reg) = 0;
1247 MEM_ATTRS (reg) = 0;
1248 }
1249 MEM_NOTRAP_P (reg) = 1;
1250 }
1251 else if (reg_equiv_mem[i])
1252 XEXP (reg_equiv_mem[i], 0) = addr;
1253 }
1254
1255 /* We don't want complex addressing modes in debug insns
1256 if simpler ones will do, so delegitimize equivalences
1257 in debug insns. */
1258 if (MAY_HAVE_DEBUG_INSNS && reg_renumber[i] < 0)
1259 {
1260 rtx reg = regno_reg_rtx[i];
1261 rtx equiv = 0;
1262 df_ref use, next;
1263
1264 if (reg_equiv_constant[i])
1265 equiv = reg_equiv_constant[i];
1266 else if (reg_equiv_invariant[i])
1267 equiv = reg_equiv_invariant[i];
1268 else if (reg && MEM_P (reg))
1269 equiv = targetm.delegitimize_address (reg);
1270 else if (reg && REG_P (reg) && (int)REGNO (reg) != i)
1271 equiv = reg;
1272
1273 if (equiv == reg)
1274 continue;
1275
1276 for (use = DF_REG_USE_CHAIN (i); use; use = next)
1277 {
1278 insn = DF_REF_INSN (use);
1279
1280 /* Make sure the next ref is for a different instruction,
1281 so that we're not affected by the rescan. */
1282 next = DF_REF_NEXT_REG (use);
1283 while (next && DF_REF_INSN (next) == insn)
1284 next = DF_REF_NEXT_REG (next);
1285
1286 if (DEBUG_INSN_P (insn))
1287 {
1288 if (!equiv)
1289 {
1290 INSN_VAR_LOCATION_LOC (insn) = gen_rtx_UNKNOWN_VAR_LOC ();
1291 df_insn_rescan_debug_internal (insn);
1292 }
1293 else
1294 INSN_VAR_LOCATION_LOC (insn)
1295 = simplify_replace_rtx (INSN_VAR_LOCATION_LOC (insn),
1296 reg, equiv);
1297 }
1298 }
1299 }
1300 }
1301
1302 /* We must set reload_completed now since the cleanup_subreg_operands call
1303 below will re-recognize each insn and reload may have generated insns
1304 which are only valid during and after reload. */
1305 reload_completed = 1;
1306
1307 /* Make a pass over all the insns and delete all USEs which we inserted
1308 only to tag a REG_EQUAL note on them. Remove all REG_DEAD and REG_UNUSED
1309 notes. Delete all CLOBBER insns, except those that refer to the return
1310 value and the special mem:BLK CLOBBERs added to prevent the scheduler
1311 from misarranging variable-array code, and simplify (subreg (reg))
1312 operands. Strip and regenerate REG_INC notes that may have been moved
1313 around. */
1314
1315 for (insn = first; insn; insn = NEXT_INSN (insn))
1316 if (INSN_P (insn))
1317 {
1318 rtx *pnote;
1319
1320 if (CALL_P (insn))
1321 replace_pseudos_in (& CALL_INSN_FUNCTION_USAGE (insn),
1322 VOIDmode, CALL_INSN_FUNCTION_USAGE (insn));
1323
1324 if ((GET_CODE (PATTERN (insn)) == USE
1325 /* We mark with QImode USEs introduced by reload itself. */
1326 && (GET_MODE (insn) == QImode
1327 || find_reg_note (insn, REG_EQUAL, NULL_RTX)))
1328 || (GET_CODE (PATTERN (insn)) == CLOBBER
1329 && (!MEM_P (XEXP (PATTERN (insn), 0))
1330 || GET_MODE (XEXP (PATTERN (insn), 0)) != BLKmode
1331 || (GET_CODE (XEXP (XEXP (PATTERN (insn), 0), 0)) != SCRATCH
1332 && XEXP (XEXP (PATTERN (insn), 0), 0)
1333 != stack_pointer_rtx))
1334 && (!REG_P (XEXP (PATTERN (insn), 0))
1335 || ! REG_FUNCTION_VALUE_P (XEXP (PATTERN (insn), 0)))))
1336 {
1337 delete_insn (insn);
1338 continue;
1339 }
1340
1341 /* Some CLOBBERs may survive until here and still reference unassigned
1342 pseudos with const equivalent, which may in turn cause ICE in later
1343 passes if the reference remains in place. */
1344 if (GET_CODE (PATTERN (insn)) == CLOBBER)
1345 replace_pseudos_in (& XEXP (PATTERN (insn), 0),
1346 VOIDmode, PATTERN (insn));
1347
1348 /* Discard obvious no-ops, even without -O. This optimization
1349 is fast and doesn't interfere with debugging. */
1350 if (NONJUMP_INSN_P (insn)
1351 && GET_CODE (PATTERN (insn)) == SET
1352 && REG_P (SET_SRC (PATTERN (insn)))
1353 && REG_P (SET_DEST (PATTERN (insn)))
1354 && (REGNO (SET_SRC (PATTERN (insn)))
1355 == REGNO (SET_DEST (PATTERN (insn)))))
1356 {
1357 delete_insn (insn);
1358 continue;
1359 }
1360
1361 pnote = &REG_NOTES (insn);
1362 while (*pnote != 0)
1363 {
1364 if (REG_NOTE_KIND (*pnote) == REG_DEAD
1365 || REG_NOTE_KIND (*pnote) == REG_UNUSED
1366 || REG_NOTE_KIND (*pnote) == REG_INC)
1367 *pnote = XEXP (*pnote, 1);
1368 else
1369 pnote = &XEXP (*pnote, 1);
1370 }
1371
1372 #ifdef AUTO_INC_DEC
1373 add_auto_inc_notes (insn, PATTERN (insn));
1374 #endif
1375
1376 /* Simplify (subreg (reg)) if it appears as an operand. */
1377 cleanup_subreg_operands (insn);
1378
1379 /* Clean up invalid ASMs so that they don't confuse later passes.
1380 See PR 21299. */
1381 if (asm_noperands (PATTERN (insn)) >= 0)
1382 {
1383 extract_insn (insn);
1384 if (!constrain_operands (1))
1385 {
1386 error_for_asm (insn,
1387 "%<asm%> operand has impossible constraints");
1388 delete_insn (insn);
1389 continue;
1390 }
1391 }
1392 }
1393
1394 /* If we are doing generic stack checking, give a warning if this
1395 function's frame size is larger than we expect. */
1396 if (flag_stack_check == GENERIC_STACK_CHECK)
1397 {
1398 HOST_WIDE_INT size = get_frame_size () + STACK_CHECK_FIXED_FRAME_SIZE;
1399 static int verbose_warned = 0;
1400
1401 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1402 if (df_regs_ever_live_p (i) && ! fixed_regs[i] && call_used_regs[i])
1403 size += UNITS_PER_WORD;
1404
1405 if (size > STACK_CHECK_MAX_FRAME_SIZE)
1406 {
1407 warning (0, "frame size too large for reliable stack checking");
1408 if (! verbose_warned)
1409 {
1410 warning (0, "try reducing the number of local variables");
1411 verbose_warned = 1;
1412 }
1413 }
1414 }
1415
1416 /* Indicate that we no longer have known memory locations or constants. */
1417 if (reg_equiv_constant) 4209 if (reg_equiv_constant)
1418 free (reg_equiv_constant); 4210 free (reg_equiv_constant);
1419 if (reg_equiv_invariant) 4211 if (reg_equiv_invariant)
1420 free (reg_equiv_invariant); 4212 free (reg_equiv_invariant);
1421 reg_equiv_constant = 0; 4213 reg_equiv_constant = 0;
1422 reg_equiv_invariant = 0; 4214 reg_equiv_invariant = 0;
1423 VEC_free (rtx, gc, reg_equiv_memory_loc_vec); 4215 VEC_free (rtx, gc, reg_equiv_memory_loc_vec);
1424 reg_equiv_memory_loc = 0; 4216 reg_equiv_memory_loc = 0;
1425 4217
1426 free (temp_pseudo_reg_arr);
1427
1428 if (offsets_known_at) 4218 if (offsets_known_at)
1429 free (offsets_known_at); 4219 free (offsets_known_at);
1430 if (offsets_at) 4220 if (offsets_at)
1431 free (offsets_at); 4221 free (offsets_at);
4222 offsets_at = 0;
4223 offsets_known_at = 0;
1432 4224
1433 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++) 4225 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1434 if (reg_equiv_alt_mem_list[i]) 4226 if (reg_equiv_alt_mem_list[i])
1435 free_EXPR_LIST_list (&reg_equiv_alt_mem_list[i]); 4227 free_EXPR_LIST_list (&reg_equiv_alt_mem_list[i]);
1436 free (reg_equiv_alt_mem_list); 4228 free (reg_equiv_alt_mem_list);
1437 4229
1438 free (reg_equiv_mem); 4230 free (reg_equiv_mem);
1439 reg_equiv_init = 0;
1440 free (reg_equiv_address); 4231 free (reg_equiv_address);
1441 free (reg_max_ref_width);
1442 free (reg_old_renumber);
1443 free (pseudo_previous_regs);
1444 free (pseudo_forbidden_regs);
1445
1446 CLEAR_HARD_REG_SET (used_spill_regs);
1447 for (i = 0; i < n_spills; i++)
1448 SET_HARD_REG_BIT (used_spill_regs, spill_regs[i]);
1449
1450 /* Free all the insn_chain structures at once. */
1451 obstack_free (&reload_obstack, reload_startobj);
1452 unused_insn_chains = 0;
1453 fixup_abnormal_edges ();
1454
1455 /* Replacing pseudos with their memory equivalents might have
1456 created shared rtx. Subsequent passes would get confused
1457 by this, so unshare everything here. */
1458 unshare_all_rtl_again (first);
1459
1460 #ifdef STACK_BOUNDARY
1461 /* init_emit has set the alignment of the hard frame pointer
1462 to STACK_BOUNDARY. It is very likely no longer valid if
1463 the hard frame pointer was used for register allocation. */
1464 if (!frame_pointer_needed)
1465 REGNO_POINTER_ALIGN (HARD_FRAME_POINTER_REGNUM) = BITS_PER_UNIT;
1466 #endif
1467
1468 VEC_free (rtx_p, heap, substitute_stack);
1469
1470 return failure;
1471 }
1472
1473 /* Yet another special case. Unfortunately, reg-stack forces people to
1474 write incorrect clobbers in asm statements. These clobbers must not
1475 cause the register to appear in bad_spill_regs, otherwise we'll call
1476 fatal_insn later. We clear the corresponding regnos in the live
1477 register sets to avoid this.
1478 The whole thing is rather sick, I'm afraid. */
1479
1480 static void
1481 maybe_fix_stack_asms (void)
1482 {
1483 #ifdef STACK_REGS
1484 const char *constraints[MAX_RECOG_OPERANDS];
1485 enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
1486 struct insn_chain *chain;
1487
1488 for (chain = reload_insn_chain; chain != 0; chain = chain->next)
1489 {
1490 int i, noperands;
1491 HARD_REG_SET clobbered, allowed;
1492 rtx pat;
1493
1494 if (! INSN_P (chain->insn)
1495 || (noperands = asm_noperands (PATTERN (chain->insn))) < 0)
1496 continue;
1497 pat = PATTERN (chain->insn);
1498 if (GET_CODE (pat) != PARALLEL)
1499 continue;
1500
1501 CLEAR_HARD_REG_SET (clobbered);
1502 CLEAR_HARD_REG_SET (allowed);
1503
1504 /* First, make a mask of all stack regs that are clobbered. */
1505 for (i = 0; i < XVECLEN (pat, 0); i++)
1506 {
1507 rtx t = XVECEXP (pat, 0, i);
1508 if (GET_CODE (t) == CLOBBER && STACK_REG_P (XEXP (t, 0)))
1509 SET_HARD_REG_BIT (clobbered, REGNO (XEXP (t, 0)));
1510 }
1511
1512 /* Get the operand values and constraints out of the insn. */
1513 decode_asm_operands (pat, recog_data.operand, recog_data.operand_loc,
1514 constraints, operand_mode, NULL);
1515
1516 /* For every operand, see what registers are allowed. */
1517 for (i = 0; i < noperands; i++)
1518 {
1519 const char *p = constraints[i];
1520 /* For every alternative, we compute the class of registers allowed
1521 for reloading in CLS, and merge its contents into the reg set
1522 ALLOWED. */
1523 int cls = (int) NO_REGS;
1524
1525 for (;;)
1526 {
1527 char c = *p;
1528
1529 if (c == '\0' || c == ',' || c == '#')
1530 {
1531 /* End of one alternative - mark the regs in the current
1532 class, and reset the class. */
1533 IOR_HARD_REG_SET (allowed, reg_class_contents[cls]);
1534 cls = NO_REGS;
1535 p++;
1536 if (c == '#')
1537 do {
1538 c = *p++;
1539 } while (c != '\0' && c != ',');
1540 if (c == '\0')
1541 break;
1542 continue;
1543 }
1544
1545 switch (c)
1546 {
1547 case '=': case '+': case '*': case '%': case '?': case '!':
1548 case '0': case '1': case '2': case '3': case '4': case '<':
1549 case '>': case 'V': case 'o': case '&': case 'E': case 'F':
1550 case 's': case 'i': case 'n': case 'X': case 'I': case 'J':
1551 case 'K': case 'L': case 'M': case 'N': case 'O': case 'P':
1552 case TARGET_MEM_CONSTRAINT:
1553 break;
1554
1555 case 'p':
1556 cls = (int) reg_class_subunion[cls]
1557 [(int) base_reg_class (VOIDmode, ADDRESS, SCRATCH)];
1558 break;
1559
1560 case 'g':
1561 case 'r':
1562 cls = (int) reg_class_subunion[cls][(int) GENERAL_REGS];
1563 break;
1564
1565 default:
1566 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1567 cls = (int) reg_class_subunion[cls]
1568 [(int) base_reg_class (VOIDmode, ADDRESS, SCRATCH)];
1569 else
1570 cls = (int) reg_class_subunion[cls]
1571 [(int) REG_CLASS_FROM_CONSTRAINT (c, p)];
1572 }
1573 p += CONSTRAINT_LEN (c, p);
1574 }
1575 }
1576 /* Those of the registers which are clobbered, but allowed by the
1577 constraints, must be usable as reload registers. So clear them
1578 out of the life information. */
1579 AND_HARD_REG_SET (allowed, clobbered);
1580 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1581 if (TEST_HARD_REG_BIT (allowed, i))
1582 {
1583 CLEAR_REGNO_REG_SET (&chain->live_throughout, i);
1584 CLEAR_REGNO_REG_SET (&chain->dead_or_set, i);
1585 }
1586 }
1587
1588 #endif
1589 }
1590
1591 /* Copy the global variables n_reloads and rld into the corresponding elts
1592 of CHAIN. */
1593 static void
1594 copy_reloads (struct insn_chain *chain)
1595 {
1596 chain->n_reloads = n_reloads;
1597 chain->rld = XOBNEWVEC (&reload_obstack, struct reload, n_reloads);
1598 memcpy (chain->rld, rld, n_reloads * sizeof (struct reload));
1599 reload_insn_firstobj = XOBNEWVAR (&reload_obstack, char, 0);
1600 }
1601
1602 /* Walk the chain of insns, and determine for each whether it needs reloads
1603 and/or eliminations. Build the corresponding insns_need_reload list, and
1604 set something_needs_elimination as appropriate. */
1605 static void
1606 calculate_needs_all_insns (int global)
1607 {
1608 struct insn_chain **pprev_reload = &insns_need_reload;
1609 struct insn_chain *chain, *next = 0;
1610
1611 something_needs_elimination = 0;
1612
1613 reload_insn_firstobj = XOBNEWVAR (&reload_obstack, char, 0);
1614 for (chain = reload_insn_chain; chain != 0; chain = next)
1615 {
1616 rtx insn = chain->insn;
1617
1618 next = chain->next;
1619
1620 /* Clear out the shortcuts. */
1621 chain->n_reloads = 0;
1622 chain->need_elim = 0;
1623 chain->need_reload = 0;
1624 chain->need_operand_change = 0;
1625
1626 /* If this is a label, a JUMP_INSN, or has REG_NOTES (which might
1627 include REG_LABEL_OPERAND and REG_LABEL_TARGET), we need to see
1628 what effects this has on the known offsets at labels. */
1629
1630 if (LABEL_P (insn) || JUMP_P (insn)
1631 || (INSN_P (insn) && REG_NOTES (insn) != 0))
1632 set_label_offsets (insn, insn, 0);
1633
1634 if (INSN_P (insn))
1635 {
1636 rtx old_body = PATTERN (insn);
1637 int old_code = INSN_CODE (insn);
1638 rtx old_notes = REG_NOTES (insn);
1639 int did_elimination = 0;
1640 int operands_changed = 0;
1641 rtx set = single_set (insn);
1642
1643 /* Skip insns that only set an equivalence. */
1644 if (set && REG_P (SET_DEST (set))
1645 && reg_renumber[REGNO (SET_DEST (set))] < 0
1646 && (reg_equiv_constant[REGNO (SET_DEST (set))]
1647 || (reg_equiv_invariant[REGNO (SET_DEST (set))]))
1648 && reg_equiv_init[REGNO (SET_DEST (set))])
1649 continue;
1650
1651 /* If needed, eliminate any eliminable registers. */
1652 if (num_eliminable || num_eliminable_invariants)
1653 did_elimination = eliminate_regs_in_insn (insn, 0);
1654
1655 /* Analyze the instruction. */
1656 operands_changed = find_reloads (insn, 0, spill_indirect_levels,
1657 global, spill_reg_order);
1658
1659 /* If a no-op set needs more than one reload, this is likely
1660 to be something that needs input address reloads. We
1661 can't get rid of this cleanly later, and it is of no use
1662 anyway, so discard it now.
1663 We only do this when expensive_optimizations is enabled,
1664 since this complements reload inheritance / output
1665 reload deletion, and it can make debugging harder. */
1666 if (flag_expensive_optimizations && n_reloads > 1)
1667 {
1668 rtx set = single_set (insn);
1669 if (set
1670 &&
1671 ((SET_SRC (set) == SET_DEST (set)
1672 && REG_P (SET_SRC (set))
1673 && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
1674 || (REG_P (SET_SRC (set)) && REG_P (SET_DEST (set))
1675 && reg_renumber[REGNO (SET_SRC (set))] < 0
1676 && reg_renumber[REGNO (SET_DEST (set))] < 0
1677 && reg_equiv_memory_loc[REGNO (SET_SRC (set))] != NULL
1678 && reg_equiv_memory_loc[REGNO (SET_DEST (set))] != NULL
1679 && rtx_equal_p (reg_equiv_memory_loc
1680 [REGNO (SET_SRC (set))],
1681 reg_equiv_memory_loc
1682 [REGNO (SET_DEST (set))]))))
1683 {
1684 if (ira_conflicts_p)
1685 /* Inform IRA about the insn deletion. */
1686 ira_mark_memory_move_deletion (REGNO (SET_DEST (set)),
1687 REGNO (SET_SRC (set)));
1688 delete_insn (insn);
1689 /* Delete it from the reload chain. */
1690 if (chain->prev)
1691 chain->prev->next = next;
1692 else
1693 reload_insn_chain = next;
1694 if (next)
1695 next->prev = chain->prev;
1696 chain->next = unused_insn_chains;
1697 unused_insn_chains = chain;
1698 continue;
1699 }
1700 }
1701 if (num_eliminable)
1702 update_eliminable_offsets ();
1703
1704 /* Remember for later shortcuts which insns had any reloads or
1705 register eliminations. */
1706 chain->need_elim = did_elimination;
1707 chain->need_reload = n_reloads > 0;
1708 chain->need_operand_change = operands_changed;
1709
1710 /* Discard any register replacements done. */
1711 if (did_elimination)
1712 {
1713 obstack_free (&reload_obstack, reload_insn_firstobj);
1714 PATTERN (insn) = old_body;
1715 INSN_CODE (insn) = old_code;
1716 REG_NOTES (insn) = old_notes;
1717 something_needs_elimination = 1;
1718 }
1719
1720 something_needs_operands_changed |= operands_changed;
1721
1722 if (n_reloads != 0)
1723 {
1724 copy_reloads (chain);
1725 *pprev_reload = chain;
1726 pprev_reload = &chain->next_need_reload;
1727 }
1728 }
1729 }
1730 *pprev_reload = 0;
1731 }
1732
1733 /* Comparison function for qsort to decide which of two reloads
1734 should be handled first. *P1 and *P2 are the reload numbers. */
1735
1736 static int
1737 reload_reg_class_lower (const void *r1p, const void *r2p)
1738 {
1739 int r1 = *(const short *) r1p, r2 = *(const short *) r2p;
1740 int t;
1741
1742 /* Consider required reloads before optional ones. */
1743 t = rld[r1].optional - rld[r2].optional;
1744 if (t != 0)
1745 return t;
1746
1747 /* Count all solitary classes before non-solitary ones. */
1748 t = ((reg_class_size[(int) rld[r2].rclass] == 1)
1749 - (reg_class_size[(int) rld[r1].rclass] == 1));
1750 if (t != 0)
1751 return t;
1752
1753 /* Aside from solitaires, consider all multi-reg groups first. */
1754 t = rld[r2].nregs - rld[r1].nregs;
1755 if (t != 0)
1756 return t;
1757
1758 /* Consider reloads in order of increasing reg-class number. */
1759 t = (int) rld[r1].rclass - (int) rld[r2].rclass;
1760 if (t != 0)
1761 return t;
1762
1763 /* If reloads are equally urgent, sort by reload number,
1764 so that the results of qsort leave nothing to chance. */
1765 return r1 - r2;
1766 }
1767
1768 /* The cost of spilling each hard reg. */
1769 static int spill_cost[FIRST_PSEUDO_REGISTER];
1770
1771 /* When spilling multiple hard registers, we use SPILL_COST for the first
1772 spilled hard reg and SPILL_ADD_COST for subsequent regs. SPILL_ADD_COST
1773 only the first hard reg for a multi-reg pseudo. */
1774 static int spill_add_cost[FIRST_PSEUDO_REGISTER];
1775
1776 /* Map of hard regno to pseudo regno currently occupying the hard
1777 reg. */
1778 static int hard_regno_to_pseudo_regno[FIRST_PSEUDO_REGISTER];
1779
1780 /* Update the spill cost arrays, considering that pseudo REG is live. */
1781
1782 static void
1783 count_pseudo (int reg)
1784 {
1785 int freq = REG_FREQ (reg);
1786 int r = reg_renumber[reg];
1787 int nregs;
1788
1789 if (REGNO_REG_SET_P (&pseudos_counted, reg)
1790 || REGNO_REG_SET_P (&spilled_pseudos, reg)
1791 /* Ignore spilled pseudo-registers which can be here only if IRA
1792 is used. */
1793 || (ira_conflicts_p && r < 0))
1794 return;
1795
1796 SET_REGNO_REG_SET (&pseudos_counted, reg);
1797
1798 gcc_assert (r >= 0);
1799
1800 spill_add_cost[r] += freq;
1801 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (reg)];
1802 while (nregs-- > 0)
1803 {
1804 hard_regno_to_pseudo_regno[r + nregs] = reg;
1805 spill_cost[r + nregs] += freq;
1806 }
1807 }
1808
1809 /* Calculate the SPILL_COST and SPILL_ADD_COST arrays and determine the
1810 contents of BAD_SPILL_REGS for the insn described by CHAIN. */
1811
1812 static void
1813 order_regs_for_reload (struct insn_chain *chain)
1814 {
1815 unsigned i;
1816 HARD_REG_SET used_by_pseudos;
1817 HARD_REG_SET used_by_pseudos2;
1818 reg_set_iterator rsi;
1819
1820 COPY_HARD_REG_SET (bad_spill_regs, fixed_reg_set);
1821
1822 memset (spill_cost, 0, sizeof spill_cost);
1823 memset (spill_add_cost, 0, sizeof spill_add_cost);
1824 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1825 hard_regno_to_pseudo_regno[i] = -1;
1826
1827 /* Count number of uses of each hard reg by pseudo regs allocated to it
1828 and then order them by decreasing use. First exclude hard registers
1829 that are live in or across this insn. */
1830
1831 REG_SET_TO_HARD_REG_SET (used_by_pseudos, &chain->live_throughout);
1832 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, &chain->dead_or_set);
1833 IOR_HARD_REG_SET (bad_spill_regs, used_by_pseudos);
1834 IOR_HARD_REG_SET (bad_spill_regs, used_by_pseudos2);
1835
1836 /* Now find out which pseudos are allocated to it, and update
1837 hard_reg_n_uses. */
1838 CLEAR_REG_SET (&pseudos_counted);
1839
1840 EXECUTE_IF_SET_IN_REG_SET
1841 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, i, rsi)
1842 {
1843 count_pseudo (i);
1844 }
1845 EXECUTE_IF_SET_IN_REG_SET
1846 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, i, rsi)
1847 {
1848 count_pseudo (i);
1849 }
1850 CLEAR_REG_SET (&pseudos_counted);
1851 }
1852
1853 /* Vector of reload-numbers showing the order in which the reloads should
1854 be processed. */
1855 static short reload_order[MAX_RELOADS];
1856
1857 /* This is used to keep track of the spill regs used in one insn. */
1858 static HARD_REG_SET used_spill_regs_local;
1859
1860 /* We decided to spill hard register SPILLED, which has a size of
1861 SPILLED_NREGS. Determine how pseudo REG, which is live during the insn,
1862 is affected. We will add it to SPILLED_PSEUDOS if necessary, and we will
1863 update SPILL_COST/SPILL_ADD_COST. */
1864
1865 static void
1866 count_spilled_pseudo (int spilled, int spilled_nregs, int reg)
1867 {
1868 int freq = REG_FREQ (reg);
1869 int r = reg_renumber[reg];
1870 int nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (reg)];
1871
1872 /* Ignore spilled pseudo-registers which can be here only if IRA is
1873 used. */
1874 if ((ira_conflicts_p && r < 0)
1875 || REGNO_REG_SET_P (&spilled_pseudos, reg)
1876 || spilled + spilled_nregs <= r || r + nregs <= spilled)
1877 return;
1878
1879 SET_REGNO_REG_SET (&spilled_pseudos, reg);
1880
1881 spill_add_cost[r] -= freq;
1882 while (nregs-- > 0)
1883 {
1884 hard_regno_to_pseudo_regno[r + nregs] = -1;
1885 spill_cost[r + nregs] -= freq;
1886 }
1887 }
1888
1889 /* Find reload register to use for reload number ORDER. */
1890
1891 static int
1892 find_reg (struct insn_chain *chain, int order)
1893 {
1894 int rnum = reload_order[order];
1895 struct reload *rl = rld + rnum;
1896 int best_cost = INT_MAX;
1897 int best_reg = -1;
1898 unsigned int i, j, n;
1899 int k;
1900 HARD_REG_SET not_usable;
1901 HARD_REG_SET used_by_other_reload;
1902 reg_set_iterator rsi;
1903 static int regno_pseudo_regs[FIRST_PSEUDO_REGISTER];
1904 static int best_regno_pseudo_regs[FIRST_PSEUDO_REGISTER];
1905
1906 COPY_HARD_REG_SET (not_usable, bad_spill_regs);
1907 IOR_HARD_REG_SET (not_usable, bad_spill_regs_global);
1908 IOR_COMPL_HARD_REG_SET (not_usable, reg_class_contents[rl->rclass]);
1909
1910 CLEAR_HARD_REG_SET (used_by_other_reload);
1911 for (k = 0; k < order; k++)
1912 {
1913 int other = reload_order[k];
1914
1915 if (rld[other].regno >= 0 && reloads_conflict (other, rnum))
1916 for (j = 0; j < rld[other].nregs; j++)
1917 SET_HARD_REG_BIT (used_by_other_reload, rld[other].regno + j);
1918 }
1919
1920 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1921 {
1922 #ifdef REG_ALLOC_ORDER
1923 unsigned int regno = reg_alloc_order[i];
1924 #else
1925 unsigned int regno = i;
1926 #endif
1927
1928 if (! TEST_HARD_REG_BIT (not_usable, regno)
1929 && ! TEST_HARD_REG_BIT (used_by_other_reload, regno)
1930 && HARD_REGNO_MODE_OK (regno, rl->mode))
1931 {
1932 int this_cost = spill_cost[regno];
1933 int ok = 1;
1934 unsigned int this_nregs = hard_regno_nregs[regno][rl->mode];
1935
1936 for (j = 1; j < this_nregs; j++)
1937 {
1938 this_cost += spill_add_cost[regno + j];
1939 if ((TEST_HARD_REG_BIT (not_usable, regno + j))
1940 || TEST_HARD_REG_BIT (used_by_other_reload, regno + j))
1941 ok = 0;
1942 }
1943 if (! ok)
1944 continue;
1945
1946 if (ira_conflicts_p)
1947 {
1948 /* Ask IRA to find a better pseudo-register for
1949 spilling. */
1950 for (n = j = 0; j < this_nregs; j++)
1951 {
1952 int r = hard_regno_to_pseudo_regno[regno + j];
1953
1954 if (r < 0)
1955 continue;
1956 if (n == 0 || regno_pseudo_regs[n - 1] != r)
1957 regno_pseudo_regs[n++] = r;
1958 }
1959 regno_pseudo_regs[n++] = -1;
1960 if (best_reg < 0
1961 || ira_better_spill_reload_regno_p (regno_pseudo_regs,
1962 best_regno_pseudo_regs,
1963 rl->in, rl->out,
1964 chain->insn))
1965 {
1966 best_reg = regno;
1967 for (j = 0;; j++)
1968 {
1969 best_regno_pseudo_regs[j] = regno_pseudo_regs[j];
1970 if (regno_pseudo_regs[j] < 0)
1971 break;
1972 }
1973 }
1974 continue;
1975 }
1976
1977 if (rl->in && REG_P (rl->in) && REGNO (rl->in) == regno)
1978 this_cost--;
1979 if (rl->out && REG_P (rl->out) && REGNO (rl->out) == regno)
1980 this_cost--;
1981 if (this_cost < best_cost
1982 /* Among registers with equal cost, prefer caller-saved ones, or
1983 use REG_ALLOC_ORDER if it is defined. */
1984 || (this_cost == best_cost
1985 #ifdef REG_ALLOC_ORDER
1986 && (inv_reg_alloc_order[regno]
1987 < inv_reg_alloc_order[best_reg])
1988 #else
1989 && call_used_regs[regno]
1990 && ! call_used_regs[best_reg]
1991 #endif
1992 ))
1993 {
1994 best_reg = regno;
1995 best_cost = this_cost;
1996 }
1997 }
1998 }
1999 if (best_reg == -1)
2000 return 0;
2001
2002 if (dump_file)
2003 fprintf (dump_file, "Using reg %d for reload %d\n", best_reg, rnum);
2004
2005 rl->nregs = hard_regno_nregs[best_reg][rl->mode];
2006 rl->regno = best_reg;
2007
2008 EXECUTE_IF_SET_IN_REG_SET
2009 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, j, rsi)
2010 {
2011 count_spilled_pseudo (best_reg, rl->nregs, j);
2012 }
2013
2014 EXECUTE_IF_SET_IN_REG_SET
2015 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, j, rsi)
2016 {
2017 count_spilled_pseudo (best_reg, rl->nregs, j);
2018 }
2019
2020 for (i = 0; i < rl->nregs; i++)
2021 {
2022 gcc_assert (spill_cost[best_reg + i] == 0);
2023 gcc_assert (spill_add_cost[best_reg + i] == 0);
2024 gcc_assert (hard_regno_to_pseudo_regno[best_reg + i] == -1);
2025 SET_HARD_REG_BIT (used_spill_regs_local, best_reg + i);
2026 }
2027 return 1;
2028 }
2029
2030 /* Find more reload regs to satisfy the remaining need of an insn, which
2031 is given by CHAIN.
2032 Do it by ascending class number, since otherwise a reg
2033 might be spilled for a big class and might fail to count
2034 for a smaller class even though it belongs to that class. */
2035
2036 static void
2037 find_reload_regs (struct insn_chain *chain)
2038 {
2039 int i;
2040
2041 /* In order to be certain of getting the registers we need,
2042 we must sort the reloads into order of increasing register class.
2043 Then our grabbing of reload registers will parallel the process
2044 that provided the reload registers. */
2045 for (i = 0; i < chain->n_reloads; i++)
2046 {
2047 /* Show whether this reload already has a hard reg. */
2048 if (chain->rld[i].reg_rtx)
2049 {
2050 int regno = REGNO (chain->rld[i].reg_rtx);
2051 chain->rld[i].regno = regno;
2052 chain->rld[i].nregs
2053 = hard_regno_nregs[regno][GET_MODE (chain->rld[i].reg_rtx)];
2054 }
2055 else
2056 chain->rld[i].regno = -1;
2057 reload_order[i] = i;
2058 }
2059
2060 n_reloads = chain->n_reloads;
2061 memcpy (rld, chain->rld, n_reloads * sizeof (struct reload));
2062
2063 CLEAR_HARD_REG_SET (used_spill_regs_local);
2064
2065 if (dump_file)
2066 fprintf (dump_file, "Spilling for insn %d.\n", INSN_UID (chain->insn));
2067
2068 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
2069
2070 /* Compute the order of preference for hard registers to spill. */
2071
2072 order_regs_for_reload (chain);
2073
2074 for (i = 0; i < n_reloads; i++)
2075 {
2076 int r = reload_order[i];
2077
2078 /* Ignore reloads that got marked inoperative. */
2079 if ((rld[r].out != 0 || rld[r].in != 0 || rld[r].secondary_p)
2080 && ! rld[r].optional
2081 && rld[r].regno == -1)
2082 if (! find_reg (chain, i))
2083 {
2084 if (dump_file)
2085 fprintf (dump_file, "reload failure for reload %d\n", r);
2086 spill_failure (chain->insn, rld[r].rclass);
2087 failure = 1;
2088 return;
2089 }
2090 }
2091
2092 COPY_HARD_REG_SET (chain->used_spill_regs, used_spill_regs_local);
2093 IOR_HARD_REG_SET (used_spill_regs, used_spill_regs_local);
2094
2095 memcpy (chain->rld, rld, n_reloads * sizeof (struct reload));
2096 }
2097
2098 static void
2099 select_reload_regs (void)
2100 {
2101 struct insn_chain *chain;
2102
2103 /* Try to satisfy the needs for each insn. */
2104 for (chain = insns_need_reload; chain != 0;
2105 chain = chain->next_need_reload)
2106 find_reload_regs (chain);
2107 }
2108
2109 /* Delete all insns that were inserted by emit_caller_save_insns during
2110 this iteration. */
2111 static void
2112 delete_caller_save_insns (void)
2113 {
2114 struct insn_chain *c = reload_insn_chain;
2115
2116 while (c != 0)
2117 {
2118 while (c != 0 && c->is_caller_save_insn)
2119 {
2120 struct insn_chain *next = c->next;
2121 rtx insn = c->insn;
2122
2123 if (c == reload_insn_chain)
2124 reload_insn_chain = next;
2125 delete_insn (insn);
2126
2127 if (next)
2128 next->prev = c->prev;
2129 if (c->prev)
2130 c->prev->next = next;
2131 c->next = unused_insn_chains;
2132 unused_insn_chains = c;
2133 c = next;
2134 }
2135 if (c != 0)
2136 c = c->next;
2137 }
2138 }
2139
2140 /* Handle the failure to find a register to spill.
2141 INSN should be one of the insns which needed this particular spill reg. */
2142
2143 static void
2144 spill_failure (rtx insn, enum reg_class rclass)
2145 {
2146 if (asm_noperands (PATTERN (insn)) >= 0)
2147 error_for_asm (insn, "can't find a register in class %qs while "
2148 "reloading %<asm%>",
2149 reg_class_names[rclass]);
2150 else
2151 {
2152 error ("unable to find a register to spill in class %qs",
2153 reg_class_names[rclass]);
2154
2155 if (dump_file)
2156 {
2157 fprintf (dump_file, "\nReloads for insn # %d\n", INSN_UID (insn));
2158 debug_reload_to_stream (dump_file);
2159 }
2160 fatal_insn ("this is the insn:", insn);
2161 }
2162 }
2163
2164 /* Delete an unneeded INSN and any previous insns who sole purpose is loading
2165 data that is dead in INSN. */
2166
2167 static void
2168 delete_dead_insn (rtx insn)
2169 {
2170 rtx prev = prev_real_insn (insn);
2171 rtx prev_dest;
2172
2173 /* If the previous insn sets a register that dies in our insn, delete it
2174 too. */
2175 if (prev && GET_CODE (PATTERN (prev)) == SET
2176 && (prev_dest = SET_DEST (PATTERN (prev)), REG_P (prev_dest))
2177 && reg_mentioned_p (prev_dest, PATTERN (insn))
2178 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
2179 && ! side_effects_p (SET_SRC (PATTERN (prev))))
2180 delete_dead_insn (prev);
2181
2182 SET_INSN_DELETED (insn);
2183 }
2184
2185 /* Modify the home of pseudo-reg I.
2186 The new home is present in reg_renumber[I].
2187
2188 FROM_REG may be the hard reg that the pseudo-reg is being spilled from;
2189 or it may be -1, meaning there is none or it is not relevant.
2190 This is used so that all pseudos spilled from a given hard reg
2191 can share one stack slot. */
2192
2193 static void
2194 alter_reg (int i, int from_reg, bool dont_share_p)
2195 {
2196 /* When outputting an inline function, this can happen
2197 for a reg that isn't actually used. */
2198 if (regno_reg_rtx[i] == 0)
2199 return;
2200
2201 /* If the reg got changed to a MEM at rtl-generation time,
2202 ignore it. */
2203 if (!REG_P (regno_reg_rtx[i]))
2204 return;
2205
2206 /* Modify the reg-rtx to contain the new hard reg
2207 number or else to contain its pseudo reg number. */
2208 SET_REGNO (regno_reg_rtx[i],
2209 reg_renumber[i] >= 0 ? reg_renumber[i] : i);
2210
2211 /* If we have a pseudo that is needed but has no hard reg or equivalent,
2212 allocate a stack slot for it. */
2213
2214 if (reg_renumber[i] < 0
2215 && REG_N_REFS (i) > 0
2216 && reg_equiv_constant[i] == 0
2217 && (reg_equiv_invariant[i] == 0 || reg_equiv_init[i] == 0)
2218 && reg_equiv_memory_loc[i] == 0)
2219 {
2220 rtx x = NULL_RTX;
2221 enum machine_mode mode = GET_MODE (regno_reg_rtx[i]);
2222 unsigned int inherent_size = PSEUDO_REGNO_BYTES (i);
2223 unsigned int inherent_align = GET_MODE_ALIGNMENT (mode);
2224 unsigned int total_size = MAX (inherent_size, reg_max_ref_width[i]);
2225 unsigned int min_align = reg_max_ref_width[i] * BITS_PER_UNIT;
2226 int adjust = 0;
2227
2228 something_was_spilled = true;
2229
2230 if (ira_conflicts_p)
2231 {
2232 /* Mark the spill for IRA. */
2233 SET_REGNO_REG_SET (&spilled_pseudos, i);
2234 if (!dont_share_p)
2235 x = ira_reuse_stack_slot (i, inherent_size, total_size);
2236 }
2237
2238 if (x)
2239 ;
2240
2241 /* Each pseudo reg has an inherent size which comes from its own mode,
2242 and a total size which provides room for paradoxical subregs
2243 which refer to the pseudo reg in wider modes.
2244
2245 We can use a slot already allocated if it provides both
2246 enough inherent space and enough total space.
2247 Otherwise, we allocate a new slot, making sure that it has no less
2248 inherent space, and no less total space, then the previous slot. */
2249 else if (from_reg == -1 || (!dont_share_p && ira_conflicts_p))
2250 {
2251 rtx stack_slot;
2252
2253 /* No known place to spill from => no slot to reuse. */
2254 x = assign_stack_local (mode, total_size,
2255 min_align > inherent_align
2256 || total_size > inherent_size ? -1 : 0);
2257
2258 stack_slot = x;
2259
2260 /* Cancel the big-endian correction done in assign_stack_local.
2261 Get the address of the beginning of the slot. This is so we
2262 can do a big-endian correction unconditionally below. */
2263 if (BYTES_BIG_ENDIAN)
2264 {
2265 adjust = inherent_size - total_size;
2266 if (adjust)
2267 stack_slot
2268 = adjust_address_nv (x, mode_for_size (total_size
2269 * BITS_PER_UNIT,
2270 MODE_INT, 1),
2271 adjust);
2272 }
2273
2274 if (! dont_share_p && ira_conflicts_p)
2275 /* Inform IRA about allocation a new stack slot. */
2276 ira_mark_new_stack_slot (stack_slot, i, total_size);
2277 }
2278
2279 /* Reuse a stack slot if possible. */
2280 else if (spill_stack_slot[from_reg] != 0
2281 && spill_stack_slot_width[from_reg] >= total_size
2282 && (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2283 >= inherent_size)
2284 && MEM_ALIGN (spill_stack_slot[from_reg]) >= min_align)
2285 x = spill_stack_slot[from_reg];
2286
2287 /* Allocate a bigger slot. */
2288 else
2289 {
2290 /* Compute maximum size needed, both for inherent size
2291 and for total size. */
2292 rtx stack_slot;
2293
2294 if (spill_stack_slot[from_reg])
2295 {
2296 if (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2297 > inherent_size)
2298 mode = GET_MODE (spill_stack_slot[from_reg]);
2299 if (spill_stack_slot_width[from_reg] > total_size)
2300 total_size = spill_stack_slot_width[from_reg];
2301 if (MEM_ALIGN (spill_stack_slot[from_reg]) > min_align)
2302 min_align = MEM_ALIGN (spill_stack_slot[from_reg]);
2303 }
2304
2305 /* Make a slot with that size. */
2306 x = assign_stack_local (mode, total_size,
2307 min_align > inherent_align
2308 || total_size > inherent_size ? -1 : 0);
2309 stack_slot = x;
2310
2311 /* Cancel the big-endian correction done in assign_stack_local.
2312 Get the address of the beginning of the slot. This is so we
2313 can do a big-endian correction unconditionally below. */
2314 if (BYTES_BIG_ENDIAN)
2315 {
2316 adjust = GET_MODE_SIZE (mode) - total_size;
2317 if (adjust)
2318 stack_slot
2319 = adjust_address_nv (x, mode_for_size (total_size
2320 * BITS_PER_UNIT,
2321 MODE_INT, 1),
2322 adjust);
2323 }
2324
2325 spill_stack_slot[from_reg] = stack_slot;
2326 spill_stack_slot_width[from_reg] = total_size;
2327 }
2328
2329 /* On a big endian machine, the "address" of the slot
2330 is the address of the low part that fits its inherent mode. */
2331 if (BYTES_BIG_ENDIAN && inherent_size < total_size)
2332 adjust += (total_size - inherent_size);
2333
2334 /* If we have any adjustment to make, or if the stack slot is the
2335 wrong mode, make a new stack slot. */
2336 x = adjust_address_nv (x, GET_MODE (regno_reg_rtx[i]), adjust);
2337
2338 /* Set all of the memory attributes as appropriate for a spill. */
2339 set_mem_attrs_for_spill (x);
2340
2341 /* Save the stack slot for later. */
2342 reg_equiv_memory_loc[i] = x;
2343 }
2344 }
2345
2346 /* Mark the slots in regs_ever_live for the hard regs used by
2347 pseudo-reg number REGNO, accessed in MODE. */
2348
2349 static void
2350 mark_home_live_1 (int regno, enum machine_mode mode)
2351 {
2352 int i, lim;
2353
2354 i = reg_renumber[regno];
2355 if (i < 0)
2356 return;
2357 lim = end_hard_regno (mode, i);
2358 while (i < lim)
2359 df_set_regs_ever_live(i++, true);
2360 }
2361
2362 /* Mark the slots in regs_ever_live for the hard regs
2363 used by pseudo-reg number REGNO. */
2364
2365 void
2366 mark_home_live (int regno)
2367 {
2368 if (reg_renumber[regno] >= 0)
2369 mark_home_live_1 (regno, PSEUDO_REGNO_MODE (regno));
2370 }
2371
2372 /* This function handles the tracking of elimination offsets around branches.
2373
2374 X is a piece of RTL being scanned.
2375
2376 INSN is the insn that it came from, if any.
2377
2378 INITIAL_P is nonzero if we are to set the offset to be the initial
2379 offset and zero if we are setting the offset of the label to be the
2380 current offset. */
2381
2382 static void
2383 set_label_offsets (rtx x, rtx insn, int initial_p)
2384 {
2385 enum rtx_code code = GET_CODE (x);
2386 rtx tem;
2387 unsigned int i;
2388 struct elim_table *p;
2389
2390 switch (code)
2391 {
2392 case LABEL_REF:
2393 if (LABEL_REF_NONLOCAL_P (x))
2394 return;
2395
2396 x = XEXP (x, 0);
2397
2398 /* ... fall through ... */
2399
2400 case CODE_LABEL:
2401 /* If we know nothing about this label, set the desired offsets. Note
2402 that this sets the offset at a label to be the offset before a label
2403 if we don't know anything about the label. This is not correct for
2404 the label after a BARRIER, but is the best guess we can make. If
2405 we guessed wrong, we will suppress an elimination that might have
2406 been possible had we been able to guess correctly. */
2407
2408 if (! offsets_known_at[CODE_LABEL_NUMBER (x) - first_label_num])
2409 {
2410 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2411 offsets_at[CODE_LABEL_NUMBER (x) - first_label_num][i]
2412 = (initial_p ? reg_eliminate[i].initial_offset
2413 : reg_eliminate[i].offset);
2414 offsets_known_at[CODE_LABEL_NUMBER (x) - first_label_num] = 1;
2415 }
2416
2417 /* Otherwise, if this is the definition of a label and it is
2418 preceded by a BARRIER, set our offsets to the known offset of
2419 that label. */
2420
2421 else if (x == insn
2422 && (tem = prev_nonnote_insn (insn)) != 0
2423 && BARRIER_P (tem))
2424 set_offsets_for_label (insn);
2425 else
2426 /* If neither of the above cases is true, compare each offset
2427 with those previously recorded and suppress any eliminations
2428 where the offsets disagree. */
2429
2430 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2431 if (offsets_at[CODE_LABEL_NUMBER (x) - first_label_num][i]
2432 != (initial_p ? reg_eliminate[i].initial_offset
2433 : reg_eliminate[i].offset))
2434 reg_eliminate[i].can_eliminate = 0;
2435
2436 return;
2437
2438 case JUMP_INSN:
2439 set_label_offsets (PATTERN (insn), insn, initial_p);
2440
2441 /* ... fall through ... */
2442
2443 case INSN:
2444 case CALL_INSN:
2445 /* Any labels mentioned in REG_LABEL_OPERAND notes can be branched
2446 to indirectly and hence must have all eliminations at their
2447 initial offsets. */
2448 for (tem = REG_NOTES (x); tem; tem = XEXP (tem, 1))
2449 if (REG_NOTE_KIND (tem) == REG_LABEL_OPERAND)
2450 set_label_offsets (XEXP (tem, 0), insn, 1);
2451 return;
2452
2453 case PARALLEL:
2454 case ADDR_VEC:
2455 case ADDR_DIFF_VEC:
2456 /* Each of the labels in the parallel or address vector must be
2457 at their initial offsets. We want the first field for PARALLEL
2458 and ADDR_VEC and the second field for ADDR_DIFF_VEC. */
2459
2460 for (i = 0; i < (unsigned) XVECLEN (x, code == ADDR_DIFF_VEC); i++)
2461 set_label_offsets (XVECEXP (x, code == ADDR_DIFF_VEC, i),
2462 insn, initial_p);
2463 return;
2464
2465 case SET:
2466 /* We only care about setting PC. If the source is not RETURN,
2467 IF_THEN_ELSE, or a label, disable any eliminations not at
2468 their initial offsets. Similarly if any arm of the IF_THEN_ELSE
2469 isn't one of those possibilities. For branches to a label,
2470 call ourselves recursively.
2471
2472 Note that this can disable elimination unnecessarily when we have
2473 a non-local goto since it will look like a non-constant jump to
2474 someplace in the current function. This isn't a significant
2475 problem since such jumps will normally be when all elimination
2476 pairs are back to their initial offsets. */
2477
2478 if (SET_DEST (x) != pc_rtx)
2479 return;
2480
2481 switch (GET_CODE (SET_SRC (x)))
2482 {
2483 case PC:
2484 case RETURN:
2485 return;
2486
2487 case LABEL_REF:
2488 set_label_offsets (SET_SRC (x), insn, initial_p);
2489 return;
2490
2491 case IF_THEN_ELSE:
2492 tem = XEXP (SET_SRC (x), 1);
2493 if (GET_CODE (tem) == LABEL_REF)
2494 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2495 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2496 break;
2497
2498 tem = XEXP (SET_SRC (x), 2);
2499 if (GET_CODE (tem) == LABEL_REF)
2500 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2501 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2502 break;
2503 return;
2504
2505 default:
2506 break;
2507 }
2508
2509 /* If we reach here, all eliminations must be at their initial
2510 offset because we are doing a jump to a variable address. */
2511 for (p = reg_eliminate; p < &reg_eliminate[NUM_ELIMINABLE_REGS]; p++)
2512 if (p->offset != p->initial_offset)
2513 p->can_eliminate = 0;
2514 break;
2515
2516 default:
2517 break;
2518 }
2519 }
2520
2521 /* Scan X and replace any eliminable registers (such as fp) with a
2522 replacement (such as sp), plus an offset.
2523
2524 MEM_MODE is the mode of an enclosing MEM. We need this to know how
2525 much to adjust a register for, e.g., PRE_DEC. Also, if we are inside a
2526 MEM, we are allowed to replace a sum of a register and the constant zero
2527 with the register, which we cannot do outside a MEM. In addition, we need
2528 to record the fact that a register is referenced outside a MEM.
2529
2530 If INSN is an insn, it is the insn containing X. If we replace a REG
2531 in a SET_DEST with an equivalent MEM and INSN is nonzero, write a
2532 CLOBBER of the pseudo after INSN so find_equiv_regs will know that
2533 the REG is being modified.
2534
2535 Alternatively, INSN may be a note (an EXPR_LIST or INSN_LIST).
2536 That's used when we eliminate in expressions stored in notes.
2537 This means, do not set ref_outside_mem even if the reference
2538 is outside of MEMs.
2539
2540 REG_EQUIV_MEM and REG_EQUIV_ADDRESS contain address that have had
2541 replacements done assuming all offsets are at their initial values. If
2542 they are not, or if REG_EQUIV_ADDRESS is nonzero for a pseudo we
2543 encounter, return the actual location so that find_reloads will do
2544 the proper thing. */
2545
2546 static rtx
2547 eliminate_regs_1 (rtx x, enum machine_mode mem_mode, rtx insn,
2548 bool may_use_invariant)
2549 {
2550 enum rtx_code code = GET_CODE (x);
2551 struct elim_table *ep;
2552 int regno;
2553 rtx new_rtx;
2554 int i, j;
2555 const char *fmt;
2556 int copied = 0;
2557
2558 if (! current_function_decl)
2559 return x;
2560
2561 switch (code)
2562 {
2563 case CONST_INT:
2564 case CONST_DOUBLE:
2565 case CONST_FIXED:
2566 case CONST_VECTOR:
2567 case CONST:
2568 case SYMBOL_REF:
2569 case CODE_LABEL:
2570 case PC:
2571 case CC0:
2572 case ASM_INPUT:
2573 case ADDR_VEC:
2574 case ADDR_DIFF_VEC:
2575 case RETURN:
2576 return x;
2577
2578 case REG:
2579 regno = REGNO (x);
2580
2581 /* First handle the case where we encounter a bare register that
2582 is eliminable. Replace it with a PLUS. */
2583 if (regno < FIRST_PSEUDO_REGISTER)
2584 {
2585 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2586 ep++)
2587 if (ep->from_rtx == x && ep->can_eliminate)
2588 return plus_constant (ep->to_rtx, ep->previous_offset);
2589
2590 }
2591 else if (reg_renumber && reg_renumber[regno] < 0
2592 && reg_equiv_invariant && reg_equiv_invariant[regno])
2593 {
2594 if (may_use_invariant || (insn && DEBUG_INSN_P (insn)))
2595 return eliminate_regs_1 (copy_rtx (reg_equiv_invariant[regno]),
2596 mem_mode, insn, true);
2597 /* There exists at least one use of REGNO that cannot be
2598 eliminated. Prevent the defining insn from being deleted. */
2599 reg_equiv_init[regno] = NULL_RTX;
2600 alter_reg (regno, -1, true);
2601 }
2602 return x;
2603
2604 /* You might think handling MINUS in a manner similar to PLUS is a
2605 good idea. It is not. It has been tried multiple times and every
2606 time the change has had to have been reverted.
2607
2608 Other parts of reload know a PLUS is special (gen_reload for example)
2609 and require special code to handle code a reloaded PLUS operand.
2610
2611 Also consider backends where the flags register is clobbered by a
2612 MINUS, but we can emit a PLUS that does not clobber flags (IA-32,
2613 lea instruction comes to mind). If we try to reload a MINUS, we
2614 may kill the flags register that was holding a useful value.
2615
2616 So, please before trying to handle MINUS, consider reload as a
2617 whole instead of this little section as well as the backend issues. */
2618 case PLUS:
2619 /* If this is the sum of an eliminable register and a constant, rework
2620 the sum. */
2621 if (REG_P (XEXP (x, 0))
2622 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2623 && CONSTANT_P (XEXP (x, 1)))
2624 {
2625 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2626 ep++)
2627 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2628 {
2629 /* The only time we want to replace a PLUS with a REG (this
2630 occurs when the constant operand of the PLUS is the negative
2631 of the offset) is when we are inside a MEM. We won't want
2632 to do so at other times because that would change the
2633 structure of the insn in a way that reload can't handle.
2634 We special-case the commonest situation in
2635 eliminate_regs_in_insn, so just replace a PLUS with a
2636 PLUS here, unless inside a MEM. */
2637 if (mem_mode != 0 && CONST_INT_P (XEXP (x, 1))
2638 && INTVAL (XEXP (x, 1)) == - ep->previous_offset)
2639 return ep->to_rtx;
2640 else
2641 return gen_rtx_PLUS (Pmode, ep->to_rtx,
2642 plus_constant (XEXP (x, 1),
2643 ep->previous_offset));
2644 }
2645
2646 /* If the register is not eliminable, we are done since the other
2647 operand is a constant. */
2648 return x;
2649 }
2650
2651 /* If this is part of an address, we want to bring any constant to the
2652 outermost PLUS. We will do this by doing register replacement in
2653 our operands and seeing if a constant shows up in one of them.
2654
2655 Note that there is no risk of modifying the structure of the insn,
2656 since we only get called for its operands, thus we are either
2657 modifying the address inside a MEM, or something like an address
2658 operand of a load-address insn. */
2659
2660 {
2661 rtx new0 = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, true);
2662 rtx new1 = eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, true);
2663
2664 if (reg_renumber && (new0 != XEXP (x, 0) || new1 != XEXP (x, 1)))
2665 {
2666 /* If one side is a PLUS and the other side is a pseudo that
2667 didn't get a hard register but has a reg_equiv_constant,
2668 we must replace the constant here since it may no longer
2669 be in the position of any operand. */
2670 if (GET_CODE (new0) == PLUS && REG_P (new1)
2671 && REGNO (new1) >= FIRST_PSEUDO_REGISTER
2672 && reg_renumber[REGNO (new1)] < 0
2673 && reg_equiv_constant != 0
2674 && reg_equiv_constant[REGNO (new1)] != 0)
2675 new1 = reg_equiv_constant[REGNO (new1)];
2676 else if (GET_CODE (new1) == PLUS && REG_P (new0)
2677 && REGNO (new0) >= FIRST_PSEUDO_REGISTER
2678 && reg_renumber[REGNO (new0)] < 0
2679 && reg_equiv_constant[REGNO (new0)] != 0)
2680 new0 = reg_equiv_constant[REGNO (new0)];
2681
2682 new_rtx = form_sum (GET_MODE (x), new0, new1);
2683
2684 /* As above, if we are not inside a MEM we do not want to
2685 turn a PLUS into something else. We might try to do so here
2686 for an addition of 0 if we aren't optimizing. */
2687 if (! mem_mode && GET_CODE (new_rtx) != PLUS)
2688 return gen_rtx_PLUS (GET_MODE (x), new_rtx, const0_rtx);
2689 else
2690 return new_rtx;
2691 }
2692 }
2693 return x;
2694
2695 case MULT:
2696 /* If this is the product of an eliminable register and a
2697 constant, apply the distribute law and move the constant out
2698 so that we have (plus (mult ..) ..). This is needed in order
2699 to keep load-address insns valid. This case is pathological.
2700 We ignore the possibility of overflow here. */
2701 if (REG_P (XEXP (x, 0))
2702 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2703 && CONST_INT_P (XEXP (x, 1)))
2704 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2705 ep++)
2706 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2707 {
2708 if (! mem_mode
2709 /* Refs inside notes or in DEBUG_INSNs don't count for
2710 this purpose. */
2711 && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2712 || GET_CODE (insn) == INSN_LIST
2713 || DEBUG_INSN_P (insn))))
2714 ep->ref_outside_mem = 1;
2715
2716 return
2717 plus_constant (gen_rtx_MULT (Pmode, ep->to_rtx, XEXP (x, 1)),
2718 ep->previous_offset * INTVAL (XEXP (x, 1)));
2719 }
2720
2721 /* ... fall through ... */
2722
2723 case CALL:
2724 case COMPARE:
2725 /* See comments before PLUS about handling MINUS. */
2726 case MINUS:
2727 case DIV: case UDIV:
2728 case MOD: case UMOD:
2729 case AND: case IOR: case XOR:
2730 case ROTATERT: case ROTATE:
2731 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
2732 case NE: case EQ:
2733 case GE: case GT: case GEU: case GTU:
2734 case LE: case LT: case LEU: case LTU:
2735 {
2736 rtx new0 = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, false);
2737 rtx new1 = XEXP (x, 1)
2738 ? eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, false) : 0;
2739
2740 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2741 return gen_rtx_fmt_ee (code, GET_MODE (x), new0, new1);
2742 }
2743 return x;
2744
2745 case EXPR_LIST:
2746 /* If we have something in XEXP (x, 0), the usual case, eliminate it. */
2747 if (XEXP (x, 0))
2748 {
2749 new_rtx = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, true);
2750 if (new_rtx != XEXP (x, 0))
2751 {
2752 /* If this is a REG_DEAD note, it is not valid anymore.
2753 Using the eliminated version could result in creating a
2754 REG_DEAD note for the stack or frame pointer. */
2755 if (REG_NOTE_KIND (x) == REG_DEAD)
2756 return (XEXP (x, 1)
2757 ? eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, true)
2758 : NULL_RTX);
2759
2760 x = alloc_reg_note (REG_NOTE_KIND (x), new_rtx, XEXP (x, 1));
2761 }
2762 }
2763
2764 /* ... fall through ... */
2765
2766 case INSN_LIST:
2767 /* Now do eliminations in the rest of the chain. If this was
2768 an EXPR_LIST, this might result in allocating more memory than is
2769 strictly needed, but it simplifies the code. */
2770 if (XEXP (x, 1))
2771 {
2772 new_rtx = eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, true);
2773 if (new_rtx != XEXP (x, 1))
2774 return
2775 gen_rtx_fmt_ee (GET_CODE (x), GET_MODE (x), XEXP (x, 0), new_rtx);
2776 }
2777 return x;
2778
2779 case PRE_INC:
2780 case POST_INC:
2781 case PRE_DEC:
2782 case POST_DEC:
2783 /* We do not support elimination of a register that is modified.
2784 elimination_effects has already make sure that this does not
2785 happen. */
2786 return x;
2787
2788 case PRE_MODIFY:
2789 case POST_MODIFY:
2790 /* We do not support elimination of a register that is modified.
2791 elimination_effects has already make sure that this does not
2792 happen. The only remaining case we need to consider here is
2793 that the increment value may be an eliminable register. */
2794 if (GET_CODE (XEXP (x, 1)) == PLUS
2795 && XEXP (XEXP (x, 1), 0) == XEXP (x, 0))
2796 {
2797 rtx new_rtx = eliminate_regs_1 (XEXP (XEXP (x, 1), 1), mem_mode,
2798 insn, true);
2799
2800 if (new_rtx != XEXP (XEXP (x, 1), 1))
2801 return gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (x, 0),
2802 gen_rtx_PLUS (GET_MODE (x),
2803 XEXP (x, 0), new_rtx));
2804 }
2805 return x;
2806
2807 case STRICT_LOW_PART:
2808 case NEG: case NOT:
2809 case SIGN_EXTEND: case ZERO_EXTEND:
2810 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2811 case FLOAT: case FIX:
2812 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2813 case ABS:
2814 case SQRT:
2815 case FFS:
2816 case CLZ:
2817 case CTZ:
2818 case POPCOUNT:
2819 case PARITY:
2820 case BSWAP:
2821 new_rtx = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, false);
2822 if (new_rtx != XEXP (x, 0))
2823 return gen_rtx_fmt_e (code, GET_MODE (x), new_rtx);
2824 return x;
2825
2826 case SUBREG:
2827 /* Similar to above processing, but preserve SUBREG_BYTE.
2828 Convert (subreg (mem)) to (mem) if not paradoxical.
2829 Also, if we have a non-paradoxical (subreg (pseudo)) and the
2830 pseudo didn't get a hard reg, we must replace this with the
2831 eliminated version of the memory location because push_reload
2832 may do the replacement in certain circumstances. */
2833 if (REG_P (SUBREG_REG (x))
2834 && (GET_MODE_SIZE (GET_MODE (x))
2835 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2836 && reg_equiv_memory_loc != 0
2837 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
2838 {
2839 new_rtx = SUBREG_REG (x);
2840 }
2841 else
2842 new_rtx = eliminate_regs_1 (SUBREG_REG (x), mem_mode, insn, false);
2843
2844 if (new_rtx != SUBREG_REG (x))
2845 {
2846 int x_size = GET_MODE_SIZE (GET_MODE (x));
2847 int new_size = GET_MODE_SIZE (GET_MODE (new_rtx));
2848
2849 if (MEM_P (new_rtx)
2850 && ((x_size < new_size
2851 #ifdef WORD_REGISTER_OPERATIONS
2852 /* On these machines, combine can create rtl of the form
2853 (set (subreg:m1 (reg:m2 R) 0) ...)
2854 where m1 < m2, and expects something interesting to
2855 happen to the entire word. Moreover, it will use the
2856 (reg:m2 R) later, expecting all bits to be preserved.
2857 So if the number of words is the same, preserve the
2858 subreg so that push_reload can see it. */
2859 && ! ((x_size - 1) / UNITS_PER_WORD
2860 == (new_size -1 ) / UNITS_PER_WORD)
2861 #endif
2862 )
2863 || x_size == new_size)
2864 )
2865 return adjust_address_nv (new_rtx, GET_MODE (x), SUBREG_BYTE (x));
2866 else
2867 return gen_rtx_SUBREG (GET_MODE (x), new_rtx, SUBREG_BYTE (x));
2868 }
2869
2870 return x;
2871
2872 case MEM:
2873 /* Our only special processing is to pass the mode of the MEM to our
2874 recursive call and copy the flags. While we are here, handle this
2875 case more efficiently. */
2876 return
2877 replace_equiv_address_nv (x,
2878 eliminate_regs_1 (XEXP (x, 0), GET_MODE (x),
2879 insn, true));
2880
2881 case USE:
2882 /* Handle insn_list USE that a call to a pure function may generate. */
2883 new_rtx = eliminate_regs_1 (XEXP (x, 0), VOIDmode, insn, false);
2884 if (new_rtx != XEXP (x, 0))
2885 return gen_rtx_USE (GET_MODE (x), new_rtx);
2886 return x;
2887
2888 case CLOBBER:
2889 gcc_assert (insn && DEBUG_INSN_P (insn));
2890 break;
2891
2892 case ASM_OPERANDS:
2893 case SET:
2894 gcc_unreachable ();
2895
2896 default:
2897 break;
2898 }
2899
2900 /* Process each of our operands recursively. If any have changed, make a
2901 copy of the rtx. */
2902 fmt = GET_RTX_FORMAT (code);
2903 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2904 {
2905 if (*fmt == 'e')
2906 {
2907 new_rtx = eliminate_regs_1 (XEXP (x, i), mem_mode, insn, false);
2908 if (new_rtx != XEXP (x, i) && ! copied)
2909 {
2910 x = shallow_copy_rtx (x);
2911 copied = 1;
2912 }
2913 XEXP (x, i) = new_rtx;
2914 }
2915 else if (*fmt == 'E')
2916 {
2917 int copied_vec = 0;
2918 for (j = 0; j < XVECLEN (x, i); j++)
2919 {
2920 new_rtx = eliminate_regs_1 (XVECEXP (x, i, j), mem_mode, insn, false);
2921 if (new_rtx != XVECEXP (x, i, j) && ! copied_vec)
2922 {
2923 rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
2924 XVEC (x, i)->elem);
2925 if (! copied)
2926 {
2927 x = shallow_copy_rtx (x);
2928 copied = 1;
2929 }
2930 XVEC (x, i) = new_v;
2931 copied_vec = 1;
2932 }
2933 XVECEXP (x, i, j) = new_rtx;
2934 }
2935 }
2936 }
2937
2938 return x;
2939 }
2940
2941 rtx
2942 eliminate_regs (rtx x, enum machine_mode mem_mode, rtx insn)
2943 {
2944 return eliminate_regs_1 (x, mem_mode, insn, false);
2945 }
2946
2947 /* Scan rtx X for modifications of elimination target registers. Update
2948 the table of eliminables to reflect the changed state. MEM_MODE is
2949 the mode of an enclosing MEM rtx, or VOIDmode if not within a MEM. */
2950
2951 static void
2952 elimination_effects (rtx x, enum machine_mode mem_mode)
2953 {
2954 enum rtx_code code = GET_CODE (x);
2955 struct elim_table *ep;
2956 int regno;
2957 int i, j;
2958 const char *fmt;
2959
2960 switch (code)
2961 {
2962 case CONST_INT:
2963 case CONST_DOUBLE:
2964 case CONST_FIXED:
2965 case CONST_VECTOR:
2966 case CONST:
2967 case SYMBOL_REF:
2968 case CODE_LABEL:
2969 case PC:
2970 case CC0:
2971 case ASM_INPUT:
2972 case ADDR_VEC:
2973 case ADDR_DIFF_VEC:
2974 case RETURN:
2975 return;
2976
2977 case REG:
2978 regno = REGNO (x);
2979
2980 /* First handle the case where we encounter a bare register that
2981 is eliminable. Replace it with a PLUS. */
2982 if (regno < FIRST_PSEUDO_REGISTER)
2983 {
2984 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2985 ep++)
2986 if (ep->from_rtx == x && ep->can_eliminate)
2987 {
2988 if (! mem_mode)
2989 ep->ref_outside_mem = 1;
2990 return;
2991 }
2992
2993 }
2994 else if (reg_renumber[regno] < 0 && reg_equiv_constant
2995 && reg_equiv_constant[regno]
2996 && ! function_invariant_p (reg_equiv_constant[regno]))
2997 elimination_effects (reg_equiv_constant[regno], mem_mode);
2998 return;
2999
3000 case PRE_INC:
3001 case POST_INC:
3002 case PRE_DEC:
3003 case POST_DEC:
3004 case POST_MODIFY:
3005 case PRE_MODIFY:
3006 /* If we modify the source of an elimination rule, disable it. */
3007 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3008 if (ep->from_rtx == XEXP (x, 0))
3009 ep->can_eliminate = 0;
3010
3011 /* If we modify the target of an elimination rule by adding a constant,
3012 update its offset. If we modify the target in any other way, we'll
3013 have to disable the rule as well. */
3014 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3015 if (ep->to_rtx == XEXP (x, 0))
3016 {
3017 int size = GET_MODE_SIZE (mem_mode);
3018
3019 /* If more bytes than MEM_MODE are pushed, account for them. */
3020 #ifdef PUSH_ROUNDING
3021 if (ep->to_rtx == stack_pointer_rtx)
3022 size = PUSH_ROUNDING (size);
3023 #endif
3024 if (code == PRE_DEC || code == POST_DEC)
3025 ep->offset += size;
3026 else if (code == PRE_INC || code == POST_INC)
3027 ep->offset -= size;
3028 else if (code == PRE_MODIFY || code == POST_MODIFY)
3029 {
3030 if (GET_CODE (XEXP (x, 1)) == PLUS
3031 && XEXP (x, 0) == XEXP (XEXP (x, 1), 0)
3032 && CONST_INT_P (XEXP (XEXP (x, 1), 1)))
3033 ep->offset -= INTVAL (XEXP (XEXP (x, 1), 1));
3034 else
3035 ep->can_eliminate = 0;
3036 }
3037 }
3038
3039 /* These two aren't unary operators. */
3040 if (code == POST_MODIFY || code == PRE_MODIFY)
3041 break;
3042
3043 /* Fall through to generic unary operation case. */
3044 case STRICT_LOW_PART:
3045 case NEG: case NOT:
3046 case SIGN_EXTEND: case ZERO_EXTEND:
3047 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
3048 case FLOAT: case FIX:
3049 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
3050 case ABS:
3051 case SQRT:
3052 case FFS:
3053 case CLZ:
3054 case CTZ:
3055 case POPCOUNT:
3056 case PARITY:
3057 case BSWAP:
3058 elimination_effects (XEXP (x, 0), mem_mode);
3059 return;
3060
3061 case SUBREG:
3062 if (REG_P (SUBREG_REG (x))
3063 && (GET_MODE_SIZE (GET_MODE (x))
3064 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
3065 && reg_equiv_memory_loc != 0
3066 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
3067 return;
3068
3069 elimination_effects (SUBREG_REG (x), mem_mode);
3070 return;
3071
3072 case USE:
3073 /* If using a register that is the source of an eliminate we still
3074 think can be performed, note it cannot be performed since we don't
3075 know how this register is used. */
3076 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3077 if (ep->from_rtx == XEXP (x, 0))
3078 ep->can_eliminate = 0;
3079
3080 elimination_effects (XEXP (x, 0), mem_mode);
3081 return;
3082
3083 case CLOBBER:
3084 /* If clobbering a register that is the replacement register for an
3085 elimination we still think can be performed, note that it cannot
3086 be performed. Otherwise, we need not be concerned about it. */
3087 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3088 if (ep->to_rtx == XEXP (x, 0))
3089 ep->can_eliminate = 0;
3090
3091 elimination_effects (XEXP (x, 0), mem_mode);
3092 return;
3093
3094 case SET:
3095 /* Check for setting a register that we know about. */
3096 if (REG_P (SET_DEST (x)))
3097 {
3098 /* See if this is setting the replacement register for an
3099 elimination.
3100
3101 If DEST is the hard frame pointer, we do nothing because we
3102 assume that all assignments to the frame pointer are for
3103 non-local gotos and are being done at a time when they are valid
3104 and do not disturb anything else. Some machines want to
3105 eliminate a fake argument pointer (or even a fake frame pointer)
3106 with either the real frame or the stack pointer. Assignments to
3107 the hard frame pointer must not prevent this elimination. */
3108
3109 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3110 ep++)
3111 if (ep->to_rtx == SET_DEST (x)
3112 && SET_DEST (x) != hard_frame_pointer_rtx)
3113 {
3114 /* If it is being incremented, adjust the offset. Otherwise,
3115 this elimination can't be done. */
3116 rtx src = SET_SRC (x);
3117
3118 if (GET_CODE (src) == PLUS
3119 && XEXP (src, 0) == SET_DEST (x)
3120 && CONST_INT_P (XEXP (src, 1)))
3121 ep->offset -= INTVAL (XEXP (src, 1));
3122 else
3123 ep->can_eliminate = 0;
3124 }
3125 }
3126
3127 elimination_effects (SET_DEST (x), VOIDmode);
3128 elimination_effects (SET_SRC (x), VOIDmode);
3129 return;
3130
3131 case MEM:
3132 /* Our only special processing is to pass the mode of the MEM to our
3133 recursive call. */
3134 elimination_effects (XEXP (x, 0), GET_MODE (x));
3135 return;
3136
3137 default:
3138 break;
3139 }
3140
3141 fmt = GET_RTX_FORMAT (code);
3142 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3143 {
3144 if (*fmt == 'e')
3145 elimination_effects (XEXP (x, i), mem_mode);
3146 else if (*fmt == 'E')
3147 for (j = 0; j < XVECLEN (x, i); j++)
3148 elimination_effects (XVECEXP (x, i, j), mem_mode);
3149 }
3150 }
3151
3152 /* Descend through rtx X and verify that no references to eliminable registers
3153 remain. If any do remain, mark the involved register as not
3154 eliminable. */
3155
3156 static void
3157 check_eliminable_occurrences (rtx x)
3158 {
3159 const char *fmt;
3160 int i;
3161 enum rtx_code code;
3162
3163 if (x == 0)
3164 return;
3165
3166 code = GET_CODE (x);
3167
3168 if (code == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
3169 {
3170 struct elim_table *ep;
3171
3172 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3173 if (ep->from_rtx == x)
3174 ep->can_eliminate = 0;
3175 return;
3176 }
3177
3178 fmt = GET_RTX_FORMAT (code);
3179 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3180 {
3181 if (*fmt == 'e')
3182 check_eliminable_occurrences (XEXP (x, i));
3183 else if (*fmt == 'E')
3184 {
3185 int j;
3186 for (j = 0; j < XVECLEN (x, i); j++)
3187 check_eliminable_occurrences (XVECEXP (x, i, j));
3188 }
3189 }
3190 }
3191
3192 /* Scan INSN and eliminate all eliminable registers in it.
3193
3194 If REPLACE is nonzero, do the replacement destructively. Also
3195 delete the insn as dead it if it is setting an eliminable register.
3196
3197 If REPLACE is zero, do all our allocations in reload_obstack.
3198
3199 If no eliminations were done and this insn doesn't require any elimination
3200 processing (these are not identical conditions: it might be updating sp,
3201 but not referencing fp; this needs to be seen during reload_as_needed so
3202 that the offset between fp and sp can be taken into consideration), zero
3203 is returned. Otherwise, 1 is returned. */
3204
3205 static int
3206 eliminate_regs_in_insn (rtx insn, int replace)
3207 {
3208 int icode = recog_memoized (insn);
3209 rtx old_body = PATTERN (insn);
3210 int insn_is_asm = asm_noperands (old_body) >= 0;
3211 rtx old_set = single_set (insn);
3212 rtx new_body;
3213 int val = 0;
3214 int i;
3215 rtx substed_operand[MAX_RECOG_OPERANDS];
3216 rtx orig_operand[MAX_RECOG_OPERANDS];
3217 struct elim_table *ep;
3218 rtx plus_src, plus_cst_src;
3219
3220 if (! insn_is_asm && icode < 0)
3221 {
3222 gcc_assert (GET_CODE (PATTERN (insn)) == USE
3223 || GET_CODE (PATTERN (insn)) == CLOBBER
3224 || GET_CODE (PATTERN (insn)) == ADDR_VEC
3225 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
3226 || GET_CODE (PATTERN (insn)) == ASM_INPUT
3227 || DEBUG_INSN_P (insn));
3228 if (DEBUG_INSN_P (insn))
3229 INSN_VAR_LOCATION_LOC (insn)
3230 = eliminate_regs (INSN_VAR_LOCATION_LOC (insn), VOIDmode, insn);
3231 return 0;
3232 }
3233
3234 if (old_set != 0 && REG_P (SET_DEST (old_set))
3235 && REGNO (SET_DEST (old_set)) < FIRST_PSEUDO_REGISTER)
3236 {
3237 /* Check for setting an eliminable register. */
3238 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3239 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
3240 {
3241 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3242 /* If this is setting the frame pointer register to the
3243 hardware frame pointer register and this is an elimination
3244 that will be done (tested above), this insn is really
3245 adjusting the frame pointer downward to compensate for
3246 the adjustment done before a nonlocal goto. */
3247 if (ep->from == FRAME_POINTER_REGNUM
3248 && ep->to == HARD_FRAME_POINTER_REGNUM)
3249 {
3250 rtx base = SET_SRC (old_set);
3251 rtx base_insn = insn;
3252 HOST_WIDE_INT offset = 0;
3253
3254 while (base != ep->to_rtx)
3255 {
3256 rtx prev_insn, prev_set;
3257
3258 if (GET_CODE (base) == PLUS
3259 && CONST_INT_P (XEXP (base, 1)))
3260 {
3261 offset += INTVAL (XEXP (base, 1));
3262 base = XEXP (base, 0);
3263 }
3264 else if ((prev_insn = prev_nonnote_insn (base_insn)) != 0
3265 && (prev_set = single_set (prev_insn)) != 0
3266 && rtx_equal_p (SET_DEST (prev_set), base))
3267 {
3268 base = SET_SRC (prev_set);
3269 base_insn = prev_insn;
3270 }
3271 else
3272 break;
3273 }
3274
3275 if (base == ep->to_rtx)
3276 {
3277 rtx src
3278 = plus_constant (ep->to_rtx, offset - ep->offset);
3279
3280 new_body = old_body;
3281 if (! replace)
3282 {
3283 new_body = copy_insn (old_body);
3284 if (REG_NOTES (insn))
3285 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3286 }
3287 PATTERN (insn) = new_body;
3288 old_set = single_set (insn);
3289
3290 /* First see if this insn remains valid when we
3291 make the change. If not, keep the INSN_CODE
3292 the same and let reload fit it up. */
3293 validate_change (insn, &SET_SRC (old_set), src, 1);
3294 validate_change (insn, &SET_DEST (old_set),
3295 ep->to_rtx, 1);
3296 if (! apply_change_group ())
3297 {
3298 SET_SRC (old_set) = src;
3299 SET_DEST (old_set) = ep->to_rtx;
3300 }
3301
3302 val = 1;
3303 goto done;
3304 }
3305 }
3306 #endif
3307
3308 /* In this case this insn isn't serving a useful purpose. We
3309 will delete it in reload_as_needed once we know that this
3310 elimination is, in fact, being done.
3311
3312 If REPLACE isn't set, we can't delete this insn, but needn't
3313 process it since it won't be used unless something changes. */
3314 if (replace)
3315 {
3316 delete_dead_insn (insn);
3317 return 1;
3318 }
3319 val = 1;
3320 goto done;
3321 }
3322 }
3323
3324 /* We allow one special case which happens to work on all machines we
3325 currently support: a single set with the source or a REG_EQUAL
3326 note being a PLUS of an eliminable register and a constant. */
3327 plus_src = plus_cst_src = 0;
3328 if (old_set && REG_P (SET_DEST (old_set)))
3329 {
3330 if (GET_CODE (SET_SRC (old_set)) == PLUS)
3331 plus_src = SET_SRC (old_set);
3332 /* First see if the source is of the form (plus (...) CST). */
3333 if (plus_src
3334 && CONST_INT_P (XEXP (plus_src, 1)))
3335 plus_cst_src = plus_src;
3336 else if (REG_P (SET_SRC (old_set))
3337 || plus_src)
3338 {
3339 /* Otherwise, see if we have a REG_EQUAL note of the form
3340 (plus (...) CST). */
3341 rtx links;
3342 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
3343 {
3344 if ((REG_NOTE_KIND (links) == REG_EQUAL
3345 || REG_NOTE_KIND (links) == REG_EQUIV)
3346 && GET_CODE (XEXP (links, 0)) == PLUS
3347 && CONST_INT_P (XEXP (XEXP (links, 0), 1)))
3348 {
3349 plus_cst_src = XEXP (links, 0);
3350 break;
3351 }
3352 }
3353 }
3354
3355 /* Check that the first operand of the PLUS is a hard reg or
3356 the lowpart subreg of one. */
3357 if (plus_cst_src)
3358 {
3359 rtx reg = XEXP (plus_cst_src, 0);
3360 if (GET_CODE (reg) == SUBREG && subreg_lowpart_p (reg))
3361 reg = SUBREG_REG (reg);
3362
3363 if (!REG_P (reg) || REGNO (reg) >= FIRST_PSEUDO_REGISTER)
3364 plus_cst_src = 0;
3365 }
3366 }
3367 if (plus_cst_src)
3368 {
3369 rtx reg = XEXP (plus_cst_src, 0);
3370 HOST_WIDE_INT offset = INTVAL (XEXP (plus_cst_src, 1));
3371
3372 if (GET_CODE (reg) == SUBREG)
3373 reg = SUBREG_REG (reg);
3374
3375 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3376 if (ep->from_rtx == reg && ep->can_eliminate)
3377 {
3378 rtx to_rtx = ep->to_rtx;
3379 offset += ep->offset;
3380 offset = trunc_int_for_mode (offset, GET_MODE (plus_cst_src));
3381
3382 if (GET_CODE (XEXP (plus_cst_src, 0)) == SUBREG)
3383 to_rtx = gen_lowpart (GET_MODE (XEXP (plus_cst_src, 0)),
3384 to_rtx);
3385 /* If we have a nonzero offset, and the source is already
3386 a simple REG, the following transformation would
3387 increase the cost of the insn by replacing a simple REG
3388 with (plus (reg sp) CST). So try only when we already
3389 had a PLUS before. */
3390 if (offset == 0 || plus_src)
3391 {
3392 rtx new_src = plus_constant (to_rtx, offset);
3393
3394 new_body = old_body;
3395 if (! replace)
3396 {
3397 new_body = copy_insn (old_body);
3398 if (REG_NOTES (insn))
3399 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3400 }
3401 PATTERN (insn) = new_body;
3402 old_set = single_set (insn);
3403
3404 /* First see if this insn remains valid when we make the
3405 change. If not, try to replace the whole pattern with
3406 a simple set (this may help if the original insn was a
3407 PARALLEL that was only recognized as single_set due to
3408 REG_UNUSED notes). If this isn't valid either, keep
3409 the INSN_CODE the same and let reload fix it up. */
3410 if (!validate_change (insn, &SET_SRC (old_set), new_src, 0))
3411 {
3412 rtx new_pat = gen_rtx_SET (VOIDmode,
3413 SET_DEST (old_set), new_src);
3414
3415 if (!validate_change (insn, &PATTERN (insn), new_pat, 0))
3416 SET_SRC (old_set) = new_src;
3417 }
3418 }
3419 else
3420 break;
3421
3422 val = 1;
3423 /* This can't have an effect on elimination offsets, so skip right
3424 to the end. */
3425 goto done;
3426 }
3427 }
3428
3429 /* Determine the effects of this insn on elimination offsets. */
3430 elimination_effects (old_body, VOIDmode);
3431
3432 /* Eliminate all eliminable registers occurring in operands that
3433 can be handled by reload. */
3434 extract_insn (insn);
3435 for (i = 0; i < recog_data.n_operands; i++)
3436 {
3437 orig_operand[i] = recog_data.operand[i];
3438 substed_operand[i] = recog_data.operand[i];
3439
3440 /* For an asm statement, every operand is eliminable. */
3441 if (insn_is_asm || insn_data[icode].operand[i].eliminable)
3442 {
3443 bool is_set_src, in_plus;
3444
3445 /* Check for setting a register that we know about. */
3446 if (recog_data.operand_type[i] != OP_IN
3447 && REG_P (orig_operand[i]))
3448 {
3449 /* If we are assigning to a register that can be eliminated, it
3450 must be as part of a PARALLEL, since the code above handles
3451 single SETs. We must indicate that we can no longer
3452 eliminate this reg. */
3453 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3454 ep++)
3455 if (ep->from_rtx == orig_operand[i])
3456 ep->can_eliminate = 0;
3457 }
3458
3459 /* Companion to the above plus substitution, we can allow
3460 invariants as the source of a plain move. */
3461 is_set_src = false;
3462 if (old_set && recog_data.operand_loc[i] == &SET_SRC (old_set))
3463 is_set_src = true;
3464 in_plus = false;
3465 if (plus_src
3466 && (recog_data.operand_loc[i] == &XEXP (plus_src, 0)
3467 || recog_data.operand_loc[i] == &XEXP (plus_src, 1)))
3468 in_plus = true;
3469
3470 substed_operand[i]
3471 = eliminate_regs_1 (recog_data.operand[i], VOIDmode,
3472 replace ? insn : NULL_RTX,
3473 is_set_src || in_plus);
3474 if (substed_operand[i] != orig_operand[i])
3475 val = 1;
3476 /* Terminate the search in check_eliminable_occurrences at
3477 this point. */
3478 *recog_data.operand_loc[i] = 0;
3479
3480 /* If an output operand changed from a REG to a MEM and INSN is an
3481 insn, write a CLOBBER insn. */
3482 if (recog_data.operand_type[i] != OP_IN
3483 && REG_P (orig_operand[i])
3484 && MEM_P (substed_operand[i])
3485 && replace)
3486 emit_insn_after (gen_clobber (orig_operand[i]), insn);
3487 }
3488 }
3489
3490 for (i = 0; i < recog_data.n_dups; i++)
3491 *recog_data.dup_loc[i]
3492 = *recog_data.operand_loc[(int) recog_data.dup_num[i]];
3493
3494 /* If any eliminable remain, they aren't eliminable anymore. */
3495 check_eliminable_occurrences (old_body);
3496
3497 /* Substitute the operands; the new values are in the substed_operand
3498 array. */
3499 for (i = 0; i < recog_data.n_operands; i++)
3500 *recog_data.operand_loc[i] = substed_operand[i];
3501 for (i = 0; i < recog_data.n_dups; i++)
3502 *recog_data.dup_loc[i] = substed_operand[(int) recog_data.dup_num[i]];
3503
3504 /* If we are replacing a body that was a (set X (plus Y Z)), try to
3505 re-recognize the insn. We do this in case we had a simple addition
3506 but now can do this as a load-address. This saves an insn in this
3507 common case.
3508 If re-recognition fails, the old insn code number will still be used,
3509 and some register operands may have changed into PLUS expressions.
3510 These will be handled by find_reloads by loading them into a register
3511 again. */
3512
3513 if (val)
3514 {
3515 /* If we aren't replacing things permanently and we changed something,
3516 make another copy to ensure that all the RTL is new. Otherwise
3517 things can go wrong if find_reload swaps commutative operands
3518 and one is inside RTL that has been copied while the other is not. */
3519 new_body = old_body;
3520 if (! replace)
3521 {
3522 new_body = copy_insn (old_body);
3523 if (REG_NOTES (insn))
3524 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3525 }
3526 PATTERN (insn) = new_body;
3527
3528 /* If we had a move insn but now we don't, rerecognize it. This will
3529 cause spurious re-recognition if the old move had a PARALLEL since
3530 the new one still will, but we can't call single_set without
3531 having put NEW_BODY into the insn and the re-recognition won't
3532 hurt in this rare case. */
3533 /* ??? Why this huge if statement - why don't we just rerecognize the
3534 thing always? */
3535 if (! insn_is_asm
3536 && old_set != 0
3537 && ((REG_P (SET_SRC (old_set))
3538 && (GET_CODE (new_body) != SET
3539 || !REG_P (SET_SRC (new_body))))
3540 /* If this was a load from or store to memory, compare
3541 the MEM in recog_data.operand to the one in the insn.
3542 If they are not equal, then rerecognize the insn. */
3543 || (old_set != 0
3544 && ((MEM_P (SET_SRC (old_set))
3545 && SET_SRC (old_set) != recog_data.operand[1])
3546 || (MEM_P (SET_DEST (old_set))
3547 && SET_DEST (old_set) != recog_data.operand[0])))
3548 /* If this was an add insn before, rerecognize. */
3549 || GET_CODE (SET_SRC (old_set)) == PLUS))
3550 {
3551 int new_icode = recog (PATTERN (insn), insn, 0);
3552 if (new_icode >= 0)
3553 INSN_CODE (insn) = new_icode;
3554 }
3555 }
3556
3557 /* Restore the old body. If there were any changes to it, we made a copy
3558 of it while the changes were still in place, so we'll correctly return
3559 a modified insn below. */
3560 if (! replace)
3561 {
3562 /* Restore the old body. */
3563 for (i = 0; i < recog_data.n_operands; i++)
3564 /* Restoring a top-level match_parallel would clobber the new_body
3565 we installed in the insn. */
3566 if (recog_data.operand_loc[i] != &PATTERN (insn))
3567 *recog_data.operand_loc[i] = orig_operand[i];
3568 for (i = 0; i < recog_data.n_dups; i++)
3569 *recog_data.dup_loc[i] = orig_operand[(int) recog_data.dup_num[i]];
3570 }
3571
3572 /* Update all elimination pairs to reflect the status after the current
3573 insn. The changes we make were determined by the earlier call to
3574 elimination_effects.
3575
3576 We also detect cases where register elimination cannot be done,
3577 namely, if a register would be both changed and referenced outside a MEM
3578 in the resulting insn since such an insn is often undefined and, even if
3579 not, we cannot know what meaning will be given to it. Note that it is
3580 valid to have a register used in an address in an insn that changes it
3581 (presumably with a pre- or post-increment or decrement).
3582
3583 If anything changes, return nonzero. */
3584
3585 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3586 {
3587 if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
3588 ep->can_eliminate = 0;
3589
3590 ep->ref_outside_mem = 0;
3591
3592 if (ep->previous_offset != ep->offset)
3593 val = 1;
3594 }
3595
3596 done:
3597 /* If we changed something, perform elimination in REG_NOTES. This is
3598 needed even when REPLACE is zero because a REG_DEAD note might refer
3599 to a register that we eliminate and could cause a different number
3600 of spill registers to be needed in the final reload pass than in
3601 the pre-passes. */
3602 if (val && REG_NOTES (insn) != 0)
3603 REG_NOTES (insn)
3604 = eliminate_regs_1 (REG_NOTES (insn), VOIDmode, REG_NOTES (insn), true);
3605
3606 return val;
3607 }
3608
3609 /* Loop through all elimination pairs.
3610 Recalculate the number not at initial offset.
3611
3612 Compute the maximum offset (minimum offset if the stack does not
3613 grow downward) for each elimination pair. */
3614
3615 static void
3616 update_eliminable_offsets (void)
3617 {
3618 struct elim_table *ep;
3619
3620 num_not_at_initial_offset = 0;
3621 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3622 {
3623 ep->previous_offset = ep->offset;
3624 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3625 num_not_at_initial_offset++;
3626 }
3627 }
3628
3629 /* Given X, a SET or CLOBBER of DEST, if DEST is the target of a register
3630 replacement we currently believe is valid, mark it as not eliminable if X
3631 modifies DEST in any way other than by adding a constant integer to it.
3632
3633 If DEST is the frame pointer, we do nothing because we assume that
3634 all assignments to the hard frame pointer are nonlocal gotos and are being
3635 done at a time when they are valid and do not disturb anything else.
3636 Some machines want to eliminate a fake argument pointer with either the
3637 frame or stack pointer. Assignments to the hard frame pointer must not
3638 prevent this elimination.
3639
3640 Called via note_stores from reload before starting its passes to scan
3641 the insns of the function. */
3642
3643 static void
3644 mark_not_eliminable (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
3645 {
3646 unsigned int i;
3647
3648 /* A SUBREG of a hard register here is just changing its mode. We should
3649 not see a SUBREG of an eliminable hard register, but check just in
3650 case. */
3651 if (GET_CODE (dest) == SUBREG)
3652 dest = SUBREG_REG (dest);
3653
3654 if (dest == hard_frame_pointer_rtx)
3655 return;
3656
3657 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3658 if (reg_eliminate[i].can_eliminate && dest == reg_eliminate[i].to_rtx
3659 && (GET_CODE (x) != SET
3660 || GET_CODE (SET_SRC (x)) != PLUS
3661 || XEXP (SET_SRC (x), 0) != dest
3662 || !CONST_INT_P (XEXP (SET_SRC (x), 1))))
3663 {
3664 reg_eliminate[i].can_eliminate_previous
3665 = reg_eliminate[i].can_eliminate = 0;
3666 num_eliminable--;
3667 }
3668 }
3669
3670 /* Verify that the initial elimination offsets did not change since the
3671 last call to set_initial_elim_offsets. This is used to catch cases
3672 where something illegal happened during reload_as_needed that could
3673 cause incorrect code to be generated if we did not check for it. */
3674
3675 static bool
3676 verify_initial_elim_offsets (void)
3677 {
3678 HOST_WIDE_INT t;
3679
3680 if (!num_eliminable)
3681 return true;
3682
3683 #ifdef ELIMINABLE_REGS
3684 {
3685 struct elim_table *ep;
3686
3687 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3688 {
3689 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, t);
3690 if (t != ep->initial_offset)
3691 return false;
3692 }
3693 }
3694 #else
3695 INITIAL_FRAME_POINTER_OFFSET (t);
3696 if (t != reg_eliminate[0].initial_offset)
3697 return false;
3698 #endif
3699
3700 return true;
3701 }
3702
3703 /* Reset all offsets on eliminable registers to their initial values. */
3704
3705 static void
3706 set_initial_elim_offsets (void)
3707 {
3708 struct elim_table *ep = reg_eliminate;
3709
3710 #ifdef ELIMINABLE_REGS
3711 for (; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3712 {
3713 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->initial_offset);
3714 ep->previous_offset = ep->offset = ep->initial_offset;
3715 }
3716 #else
3717 INITIAL_FRAME_POINTER_OFFSET (ep->initial_offset);
3718 ep->previous_offset = ep->offset = ep->initial_offset;
3719 #endif
3720
3721 num_not_at_initial_offset = 0;
3722 }
3723
3724 /* Subroutine of set_initial_label_offsets called via for_each_eh_label. */
3725
3726 static void
3727 set_initial_eh_label_offset (rtx label)
3728 {
3729 set_label_offsets (label, NULL_RTX, 1);
3730 }
3731
3732 /* Initialize the known label offsets.
3733 Set a known offset for each forced label to be at the initial offset
3734 of each elimination. We do this because we assume that all
3735 computed jumps occur from a location where each elimination is
3736 at its initial offset.
3737 For all other labels, show that we don't know the offsets. */
3738
3739 static void
3740 set_initial_label_offsets (void)
3741 {
3742 rtx x;
3743 memset (offsets_known_at, 0, num_labels);
3744
3745 for (x = forced_labels; x; x = XEXP (x, 1))
3746 if (XEXP (x, 0))
3747 set_label_offsets (XEXP (x, 0), NULL_RTX, 1);
3748
3749 for_each_eh_label (set_initial_eh_label_offset);
3750 }
3751
3752 /* Set all elimination offsets to the known values for the code label given
3753 by INSN. */
3754
3755 static void
3756 set_offsets_for_label (rtx insn)
3757 {
3758 unsigned int i;
3759 int label_nr = CODE_LABEL_NUMBER (insn);
3760 struct elim_table *ep;
3761
3762 num_not_at_initial_offset = 0;
3763 for (i = 0, ep = reg_eliminate; i < NUM_ELIMINABLE_REGS; ep++, i++)
3764 {
3765 ep->offset = ep->previous_offset
3766 = offsets_at[label_nr - first_label_num][i];
3767 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3768 num_not_at_initial_offset++;
3769 }
3770 }
3771
3772 /* See if anything that happened changes which eliminations are valid.
3773 For example, on the SPARC, whether or not the frame pointer can
3774 be eliminated can depend on what registers have been used. We need
3775 not check some conditions again (such as flag_omit_frame_pointer)
3776 since they can't have changed. */
3777
3778 static void
3779 update_eliminables (HARD_REG_SET *pset)
3780 {
3781 int previous_frame_pointer_needed = frame_pointer_needed;
3782 struct elim_table *ep;
3783
3784 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3785 if ((ep->from == HARD_FRAME_POINTER_REGNUM
3786 && targetm.frame_pointer_required ())
3787 #ifdef ELIMINABLE_REGS
3788 || ! targetm.can_eliminate (ep->from, ep->to)
3789 #endif
3790 )
3791 ep->can_eliminate = 0;
3792
3793 /* Look for the case where we have discovered that we can't replace
3794 register A with register B and that means that we will now be
3795 trying to replace register A with register C. This means we can
3796 no longer replace register C with register B and we need to disable
3797 such an elimination, if it exists. This occurs often with A == ap,
3798 B == sp, and C == fp. */
3799
3800 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3801 {
3802 struct elim_table *op;
3803 int new_to = -1;
3804
3805 if (! ep->can_eliminate && ep->can_eliminate_previous)
3806 {
3807 /* Find the current elimination for ep->from, if there is a
3808 new one. */
3809 for (op = reg_eliminate;
3810 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3811 if (op->from == ep->from && op->can_eliminate)
3812 {
3813 new_to = op->to;
3814 break;
3815 }
3816
3817 /* See if there is an elimination of NEW_TO -> EP->TO. If so,
3818 disable it. */
3819 for (op = reg_eliminate;
3820 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3821 if (op->from == new_to && op->to == ep->to)
3822 op->can_eliminate = 0;
3823 }
3824 }
3825
3826 /* See if any registers that we thought we could eliminate the previous
3827 time are no longer eliminable. If so, something has changed and we
3828 must spill the register. Also, recompute the number of eliminable
3829 registers and see if the frame pointer is needed; it is if there is
3830 no elimination of the frame pointer that we can perform. */
3831
3832 frame_pointer_needed = 1;
3833 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3834 {
3835 if (ep->can_eliminate
3836 && ep->from == FRAME_POINTER_REGNUM
3837 && ep->to != HARD_FRAME_POINTER_REGNUM
3838 && (! SUPPORTS_STACK_ALIGNMENT
3839 || ! crtl->stack_realign_needed))
3840 frame_pointer_needed = 0;
3841
3842 if (! ep->can_eliminate && ep->can_eliminate_previous)
3843 {
3844 ep->can_eliminate_previous = 0;
3845 SET_HARD_REG_BIT (*pset, ep->from);
3846 num_eliminable--;
3847 }
3848 }
3849
3850 /* If we didn't need a frame pointer last time, but we do now, spill
3851 the hard frame pointer. */
3852 if (frame_pointer_needed && ! previous_frame_pointer_needed)
3853 SET_HARD_REG_BIT (*pset, HARD_FRAME_POINTER_REGNUM);
3854 }
3855
3856 /* Return true if X is used as the target register of an elimination. */
3857
3858 bool
3859 elimination_target_reg_p (rtx x)
3860 {
3861 struct elim_table *ep;
3862
3863 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3864 if (ep->to_rtx == x && ep->can_eliminate)
3865 return true;
3866
3867 return false;
3868 }
3869
3870 /* Initialize the table of registers to eliminate.
3871 Pre-condition: global flag frame_pointer_needed has been set before
3872 calling this function. */
3873
3874 static void
3875 init_elim_table (void)
3876 {
3877 struct elim_table *ep;
3878 #ifdef ELIMINABLE_REGS
3879 const struct elim_table_1 *ep1;
3880 #endif
3881
3882 if (!reg_eliminate)
3883 reg_eliminate = XCNEWVEC (struct elim_table, NUM_ELIMINABLE_REGS);
3884
3885 num_eliminable = 0;
3886
3887 #ifdef ELIMINABLE_REGS
3888 for (ep = reg_eliminate, ep1 = reg_eliminate_1;
3889 ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++, ep1++)
3890 {
3891 ep->from = ep1->from;
3892 ep->to = ep1->to;
3893 ep->can_eliminate = ep->can_eliminate_previous
3894 = (targetm.can_eliminate (ep->from, ep->to)
3895 && ! (ep->to == STACK_POINTER_REGNUM
3896 && frame_pointer_needed
3897 && (! SUPPORTS_STACK_ALIGNMENT
3898 || ! stack_realign_fp)));
3899 }
3900 #else
3901 reg_eliminate[0].from = reg_eliminate_1[0].from;
3902 reg_eliminate[0].to = reg_eliminate_1[0].to;
3903 reg_eliminate[0].can_eliminate = reg_eliminate[0].can_eliminate_previous
3904 = ! frame_pointer_needed;
3905 #endif
3906
3907 /* Count the number of eliminable registers and build the FROM and TO
3908 REG rtx's. Note that code in gen_rtx_REG will cause, e.g.,
3909 gen_rtx_REG (Pmode, STACK_POINTER_REGNUM) to equal stack_pointer_rtx.
3910 We depend on this. */
3911 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3912 {
3913 num_eliminable += ep->can_eliminate;
3914 ep->from_rtx = gen_rtx_REG (Pmode, ep->from);
3915 ep->to_rtx = gen_rtx_REG (Pmode, ep->to);
3916 }
3917 } 4232 }
3918 4233
3919 /* Kick all pseudos out of hard register REGNO. 4234 /* Kick all pseudos out of hard register REGNO.
3920 4235
3921 If CANT_ELIMINATE is nonzero, it means that we are doing this spill 4236 If CANT_ELIMINATE is nonzero, it means that we are doing this spill
4280 reusing reload regs from previous insns, or else output 4595 reusing reload regs from previous insns, or else output
4281 load insns to reload them. Maybe output store insns too. 4596 load insns to reload them. Maybe output store insns too.
4282 Record the choices of reload reg in reload_reg_rtx. */ 4597 Record the choices of reload reg in reload_reg_rtx. */
4283 choose_reload_regs (chain); 4598 choose_reload_regs (chain);
4284 4599
4285 /* Merge any reloads that we didn't combine for fear of
4286 increasing the number of spill registers needed but now
4287 discover can be safely merged. */
4288 if (targetm.small_register_classes_for_mode_p (VOIDmode))
4289 merge_assigned_reloads (insn);
4290
4291 /* Generate the insns to reload operands into or out of 4600 /* Generate the insns to reload operands into or out of
4292 their reload regs. */ 4601 their reload regs. */
4293 emit_reload_insns (chain); 4602 emit_reload_insns (chain);
4294 4603
4295 /* Substitute the chosen reload regs from reload_reg_rtx 4604 /* Substitute the chosen reload regs from reload_reg_rtx
4297 load and store insn that we just made for reloading 4606 load and store insn that we just made for reloading
4298 and that we moved the structure into). */ 4607 and that we moved the structure into). */
4299 subst_reloads (insn); 4608 subst_reloads (insn);
4300 4609
4301 /* Adjust the exception region notes for loads and stores. */ 4610 /* Adjust the exception region notes for loads and stores. */
4302 if (flag_non_call_exceptions && !CALL_P (insn)) 4611 if (cfun->can_throw_non_call_exceptions && !CALL_P (insn))
4303 fixup_eh_region_note (insn, prev, next); 4612 fixup_eh_region_note (insn, prev, next);
4304 4613
4305 /* If this was an ASM, make sure that all the reload insns 4614 /* If this was an ASM, make sure that all the reload insns
4306 we have generated are valid. If not, give an error 4615 we have generated are valid. If not, give an error
4307 and delete them. */ 4616 and delete them. */
5669 return 1; 5978 return 1;
5670 if (x == frame_pointer_rtx || x == arg_pointer_rtx) 5979 if (x == frame_pointer_rtx || x == arg_pointer_rtx)
5671 return 1; 5980 return 1;
5672 if (GET_CODE (x) == PLUS 5981 if (GET_CODE (x) == PLUS
5673 && (XEXP (x, 0) == frame_pointer_rtx || XEXP (x, 0) == arg_pointer_rtx) 5982 && (XEXP (x, 0) == frame_pointer_rtx || XEXP (x, 0) == arg_pointer_rtx)
5674 && CONSTANT_P (XEXP (x, 1))) 5983 && GET_CODE (XEXP (x, 1)) == CONST_INT)
5675 return 1; 5984 return 1;
5676 return 0; 5985 return 0;
5677 } 5986 }
5678 5987
5679 /* Determine whether the reload reg X overlaps any rtx'es used for 5988 /* Determine whether the reload reg X overlaps any rtx'es used for
5714 for reload R. If it's valid, get an rtx for it. Return nonzero if 6023 for reload R. If it's valid, get an rtx for it. Return nonzero if
5715 successful. */ 6024 successful. */
5716 static int 6025 static int
5717 set_reload_reg (int i, int r) 6026 set_reload_reg (int i, int r)
5718 { 6027 {
5719 int regno; 6028 /* regno is 'set but not used' if HARD_REGNO_MODE_OK doesn't use its first
6029 parameter. */
6030 int regno ATTRIBUTE_UNUSED;
5720 rtx reg = spill_reg_rtx[i]; 6031 rtx reg = spill_reg_rtx[i];
5721 6032
5722 if (reg == 0 || GET_MODE (reg) != rld[r].mode) 6033 if (reg == 0 || GET_MODE (reg) != rld[r].mode)
5723 spill_reg_rtx[i] = reg 6034 spill_reg_rtx[i] = reg
5724 = gen_rtx_REG (rld[r].mode, spill_regs[i]); 6035 = gen_rtx_REG (rld[r].mode, spill_regs[i]);
5791 6102
5792 /* If we want a single register and haven't yet found one, 6103 /* If we want a single register and haven't yet found one,
5793 take any reg in the right class and not in use. 6104 take any reg in the right class and not in use.
5794 If we want a consecutive group, here is where we look for it. 6105 If we want a consecutive group, here is where we look for it.
5795 6106
5796 We use two passes so we can first look for reload regs to 6107 We use three passes so we can first look for reload regs to
5797 reuse, which are already in use for other reloads in this insn, 6108 reuse, which are already in use for other reloads in this insn,
5798 and only then use additional registers. 6109 and only then use additional registers which are not "bad", then
6110 finally any register.
6111
5799 I think that maximizing reuse is needed to make sure we don't 6112 I think that maximizing reuse is needed to make sure we don't
5800 run out of reload regs. Suppose we have three reloads, and 6113 run out of reload regs. Suppose we have three reloads, and
5801 reloads A and B can share regs. These need two regs. 6114 reloads A and B can share regs. These need two regs.
5802 Suppose A and B are given different regs. 6115 Suppose A and B are given different regs.
5803 That leaves none for C. */ 6116 That leaves none for C. */
5804 for (pass = 0; pass < 2; pass++) 6117 for (pass = 0; pass < 3; pass++)
5805 { 6118 {
5806 /* I is the index in spill_regs. 6119 /* I is the index in spill_regs.
5807 We advance it round-robin between insns to use all spill regs 6120 We advance it round-robin between insns to use all spill regs
5808 equally, so that inherited reloads have a chance 6121 equally, so that inherited reloads have a chance
5809 of leapfrogging each other. */ 6122 of leapfrogging each other. */
5839 regnum) 6152 regnum)
5840 && ! TEST_HARD_REG_BIT (reload_reg_used_for_inherit, 6153 && ! TEST_HARD_REG_BIT (reload_reg_used_for_inherit,
5841 regnum)))) 6154 regnum))))
5842 { 6155 {
5843 int nr = hard_regno_nregs[regnum][rld[r].mode]; 6156 int nr = hard_regno_nregs[regnum][rld[r].mode];
6157
6158 /* During the second pass we want to avoid reload registers
6159 which are "bad" for this reload. */
6160 if (pass == 1
6161 && ira_bad_reload_regno (regnum, rld[r].in, rld[r].out))
6162 continue;
6163
5844 /* Avoid the problem where spilling a GENERAL_OR_FP_REG 6164 /* Avoid the problem where spilling a GENERAL_OR_FP_REG
5845 (on 68000) got us two FP regs. If NR is 1, 6165 (on 68000) got us two FP regs. If NR is 1,
5846 we would reject both of them. */ 6166 we would reject both of them. */
5847 if (force_group) 6167 if (force_group)
5848 nr = rld[r].nregs; 6168 nr = rld[r].nregs;
5869 if (nr == 1) 6189 if (nr == 1)
5870 break; 6190 break;
5871 } 6191 }
5872 } 6192 }
5873 6193
5874 /* If we found something on pass 1, omit pass 2. */ 6194 /* If we found something on the current pass, omit later passes. */
5875 if (count < n_spills) 6195 if (count < n_spills)
5876 break; 6196 break;
5877 } 6197 }
5878 6198
5879 /* We should have found a spill register by now. */ 6199 /* We should have found a spill register by now. */
6156 && (TEST_HARD_REG_BIT (reg_class_contents[(int) rclass], i) 6476 && (TEST_HARD_REG_BIT (reg_class_contents[(int) rclass], i)
6157 /* Even if we can't use this register as a reload 6477 /* Even if we can't use this register as a reload
6158 register, we might use it for reload_override_in, 6478 register, we might use it for reload_override_in,
6159 if copying it to the desired class is cheap 6479 if copying it to the desired class is cheap
6160 enough. */ 6480 enough. */
6161 || ((REGISTER_MOVE_COST (mode, last_class, rclass) 6481 || ((register_move_cost (mode, last_class, rclass)
6162 < MEMORY_MOVE_COST (mode, rclass, 1)) 6482 < memory_move_cost (mode, rclass, true))
6163 && (secondary_reload_class (1, rclass, mode, 6483 && (secondary_reload_class (1, rclass, mode,
6164 last_reg) 6484 last_reg)
6165 == NO_REGS) 6485 == NO_REGS)
6166 #ifdef SECONDARY_MEMORY_NEEDED 6486 #ifdef SECONDARY_MEMORY_NEEDED
6167 && ! SECONDARY_MEMORY_NEEDED (last_class, rclass, 6487 && ! SECONDARY_MEMORY_NEEDED (last_class, rclass,
6278 || REG_P (rld[r].in) 6598 || REG_P (rld[r].in)
6279 || MEM_P (rld[r].in)) 6599 || MEM_P (rld[r].in))
6280 && (rld[r].nregs == max_group_size 6600 && (rld[r].nregs == max_group_size
6281 || ! reg_classes_intersect_p (rld[r].rclass, group_class))) 6601 || ! reg_classes_intersect_p (rld[r].rclass, group_class)))
6282 search_equiv = rld[r].in; 6602 search_equiv = rld[r].in;
6283 /* If this is an output reload from a simple move insn, look
6284 if an equivalence for the input is available. */
6285 else if (inheritance && rld[r].in == 0 && rld[r].out != 0)
6286 {
6287 rtx set = single_set (insn);
6288
6289 if (set
6290 && rtx_equal_p (rld[r].out, SET_DEST (set))
6291 && CONSTANT_P (SET_SRC (set)))
6292 search_equiv = SET_SRC (set);
6293 }
6294 6603
6295 if (search_equiv) 6604 if (search_equiv)
6296 { 6605 {
6297 rtx equiv 6606 rtx equiv
6298 = find_equiv_reg (search_equiv, insn, rld[r].rclass, 6607 = find_equiv_reg (search_equiv, insn, rld[r].rclass,
6669 clear_reload_reg_in_use (regno, rld[r].opnum, rld[r].when_needed, 6978 clear_reload_reg_in_use (regno, rld[r].opnum, rld[r].when_needed,
6670 rld[r].mode); 6979 rld[r].mode);
6671 reload_spill_index[r] = -1; 6980 reload_spill_index[r] = -1;
6672 } 6981 }
6673 6982
6674 /* If the small_register_classes_for_mode_p target hook returns true for
6675 some machine modes, we may not have merged two reloads of the same item
6676 for fear that we might not have enough reload registers. However,
6677 normally they will get the same reload register and hence actually need
6678 not be loaded twice.
6679
6680 Here we check for the most common case of this phenomenon: when we have
6681 a number of reloads for the same object, each of which were allocated
6682 the same reload_reg_rtx, that reload_reg_rtx is not used for any other
6683 reload, and is not modified in the insn itself. If we find such,
6684 merge all the reloads and set the resulting reload to RELOAD_OTHER.
6685 This will not increase the number of spill registers needed and will
6686 prevent redundant code. */
6687
6688 static void
6689 merge_assigned_reloads (rtx insn)
6690 {
6691 int i, j;
6692
6693 /* Scan all the reloads looking for ones that only load values and
6694 are not already RELOAD_OTHER and ones whose reload_reg_rtx are
6695 assigned and not modified by INSN. */
6696
6697 for (i = 0; i < n_reloads; i++)
6698 {
6699 int conflicting_input = 0;
6700 int max_input_address_opnum = -1;
6701 int min_conflicting_input_opnum = MAX_RECOG_OPERANDS;
6702
6703 if (rld[i].in == 0 || rld[i].when_needed == RELOAD_OTHER
6704 || rld[i].out != 0 || rld[i].reg_rtx == 0
6705 || reg_set_p (rld[i].reg_rtx, insn))
6706 continue;
6707
6708 /* Look at all other reloads. Ensure that the only use of this
6709 reload_reg_rtx is in a reload that just loads the same value
6710 as we do. Note that any secondary reloads must be of the identical
6711 class since the values, modes, and result registers are the
6712 same, so we need not do anything with any secondary reloads. */
6713
6714 for (j = 0; j < n_reloads; j++)
6715 {
6716 if (i == j || rld[j].reg_rtx == 0
6717 || ! reg_overlap_mentioned_p (rld[j].reg_rtx,
6718 rld[i].reg_rtx))
6719 continue;
6720
6721 if (rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6722 && rld[j].opnum > max_input_address_opnum)
6723 max_input_address_opnum = rld[j].opnum;
6724
6725 /* If the reload regs aren't exactly the same (e.g, different modes)
6726 or if the values are different, we can't merge this reload.
6727 But if it is an input reload, we might still merge
6728 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_OTHER_ADDRESS reloads. */
6729
6730 if (! rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6731 || rld[j].out != 0 || rld[j].in == 0
6732 || ! rtx_equal_p (rld[i].in, rld[j].in))
6733 {
6734 if (rld[j].when_needed != RELOAD_FOR_INPUT
6735 || ((rld[i].when_needed != RELOAD_FOR_INPUT_ADDRESS
6736 || rld[i].opnum > rld[j].opnum)
6737 && rld[i].when_needed != RELOAD_FOR_OTHER_ADDRESS))
6738 break;
6739 conflicting_input = 1;
6740 if (min_conflicting_input_opnum > rld[j].opnum)
6741 min_conflicting_input_opnum = rld[j].opnum;
6742 }
6743 }
6744
6745 /* If all is OK, merge the reloads. Only set this to RELOAD_OTHER if
6746 we, in fact, found any matching reloads. */
6747
6748 if (j == n_reloads
6749 && max_input_address_opnum <= min_conflicting_input_opnum)
6750 {
6751 gcc_assert (rld[i].when_needed != RELOAD_FOR_OUTPUT);
6752
6753 for (j = 0; j < n_reloads; j++)
6754 if (i != j && rld[j].reg_rtx != 0
6755 && rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6756 && (! conflicting_input
6757 || rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6758 || rld[j].when_needed == RELOAD_FOR_OTHER_ADDRESS))
6759 {
6760 rld[i].when_needed = RELOAD_OTHER;
6761 rld[j].in = 0;
6762 reload_spill_index[j] = -1;
6763 transfer_replacements (i, j);
6764 }
6765
6766 /* If this is now RELOAD_OTHER, look for any reloads that
6767 load parts of this operand and set them to
6768 RELOAD_FOR_OTHER_ADDRESS if they were for inputs,
6769 RELOAD_OTHER for outputs. Note that this test is
6770 equivalent to looking for reloads for this operand
6771 number.
6772
6773 We must take special care with RELOAD_FOR_OUTPUT_ADDRESS;
6774 it may share registers with a RELOAD_FOR_INPUT, so we can
6775 not change it to RELOAD_FOR_OTHER_ADDRESS. We should
6776 never need to, since we do not modify RELOAD_FOR_OUTPUT.
6777
6778 It is possible that the RELOAD_FOR_OPERAND_ADDRESS
6779 instruction is assigned the same register as the earlier
6780 RELOAD_FOR_OTHER_ADDRESS instruction. Merging these two
6781 instructions will cause the RELOAD_FOR_OTHER_ADDRESS
6782 instruction to be deleted later on. */
6783
6784 if (rld[i].when_needed == RELOAD_OTHER)
6785 for (j = 0; j < n_reloads; j++)
6786 if (rld[j].in != 0
6787 && rld[j].when_needed != RELOAD_OTHER
6788 && rld[j].when_needed != RELOAD_FOR_OTHER_ADDRESS
6789 && rld[j].when_needed != RELOAD_FOR_OUTPUT_ADDRESS
6790 && rld[j].when_needed != RELOAD_FOR_OPERAND_ADDRESS
6791 && (! conflicting_input
6792 || rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6793 || rld[j].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
6794 && reg_overlap_mentioned_for_reload_p (rld[j].in,
6795 rld[i].in))
6796 {
6797 int k;
6798
6799 rld[j].when_needed
6800 = ((rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6801 || rld[j].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
6802 ? RELOAD_FOR_OTHER_ADDRESS : RELOAD_OTHER);
6803
6804 /* Check to see if we accidentally converted two
6805 reloads that use the same reload register with
6806 different inputs to the same type. If so, the
6807 resulting code won't work. */
6808 if (rld[j].reg_rtx)
6809 for (k = 0; k < j; k++)
6810 gcc_assert (rld[k].in == 0 || rld[k].reg_rtx == 0
6811 || rld[k].when_needed != rld[j].when_needed
6812 || !rtx_equal_p (rld[k].reg_rtx,
6813 rld[j].reg_rtx)
6814 || rtx_equal_p (rld[k].in,
6815 rld[j].in));
6816 }
6817 }
6818 }
6819 }
6820
6821 /* These arrays are filled by emit_reload_insns and its subroutines. */ 6983 /* These arrays are filled by emit_reload_insns and its subroutines. */
6822 static rtx input_reload_insns[MAX_RECOG_OPERANDS]; 6984 static rtx input_reload_insns[MAX_RECOG_OPERANDS];
6823 static rtx other_input_address_reload_insns = 0; 6985 static rtx other_input_address_reload_insns = 0;
6824 static rtx other_input_reload_insns = 0; 6986 static rtx other_input_reload_insns = 0;
6825 static rtx input_address_reload_insns[MAX_RECOG_OPERANDS]; 6987 static rtx input_address_reload_insns[MAX_RECOG_OPERANDS];
7167 secondary_reload_info sri, sri2; 7329 secondary_reload_info sri, sri2;
7168 enum reg_class new_class, new_t_class; 7330 enum reg_class new_class, new_t_class;
7169 7331
7170 sri.icode = CODE_FOR_nothing; 7332 sri.icode = CODE_FOR_nothing;
7171 sri.prev_sri = NULL; 7333 sri.prev_sri = NULL;
7172 new_class = targetm.secondary_reload (1, real_oldequiv, rl->rclass, 7334 new_class
7173 mode, &sri); 7335 = (enum reg_class) targetm.secondary_reload (1, real_oldequiv,
7336 rl->rclass, mode,
7337 &sri);
7174 7338
7175 if (new_class == NO_REGS && sri.icode == CODE_FOR_nothing) 7339 if (new_class == NO_REGS && sri.icode == CODE_FOR_nothing)
7176 second_reload_reg = 0; 7340 second_reload_reg = 0;
7177 else if (new_class == NO_REGS) 7341 else if (new_class == NO_REGS)
7178 { 7342 {
7194 gcc_unreachable (); 7358 gcc_unreachable ();
7195 else 7359 else
7196 { 7360 {
7197 sri2.icode = CODE_FOR_nothing; 7361 sri2.icode = CODE_FOR_nothing;
7198 sri2.prev_sri = &sri; 7362 sri2.prev_sri = &sri;
7199 new_t_class = targetm.secondary_reload (1, real_oldequiv, 7363 new_t_class
7200 new_class, mode, &sri); 7364 = (enum reg_class) targetm.secondary_reload (1, real_oldequiv,
7365 new_class, mode,
7366 &sri);
7201 if (new_t_class == NO_REGS && sri2.icode == CODE_FOR_nothing) 7367 if (new_t_class == NO_REGS && sri2.icode == CODE_FOR_nothing)
7202 { 7368 {
7203 if (reload_adjust_reg_for_temp (&second_reload_reg, 7369 if (reload_adjust_reg_for_temp (&second_reload_reg,
7204 third_reload_reg, 7370 third_reload_reg,
7205 new_class, mode)) 7371 new_class, mode))
7321 && ((reg_equiv_memory_loc 7487 && ((reg_equiv_memory_loc
7322 [REGNO (SUBREG_REG (oldequiv))] != 0) 7488 [REGNO (SUBREG_REG (oldequiv))] != 0)
7323 || (reg_equiv_constant 7489 || (reg_equiv_constant
7324 [REGNO (SUBREG_REG (oldequiv))] != 0))) 7490 [REGNO (SUBREG_REG (oldequiv))] != 0)))
7325 || (CONSTANT_P (oldequiv) 7491 || (CONSTANT_P (oldequiv)
7326 && (PREFERRED_RELOAD_CLASS (oldequiv, 7492 && (targetm.preferred_reload_class (oldequiv,
7327 REGNO_REG_CLASS (REGNO (reloadreg))) 7493 REGNO_REG_CLASS (REGNO (reloadreg)))
7328 == NO_REGS))) 7494 == NO_REGS)))
7329 real_oldequiv = rl->in; 7495 real_oldequiv = rl->in;
7330 gen_reload (reloadreg, real_oldequiv, rl->opnum, 7496 gen_reload (reloadreg, real_oldequiv, rl->opnum,
7331 rl->when_needed); 7497 rl->when_needed);
7332 } 7498 }
7333 7499
7334 if (flag_non_call_exceptions) 7500 if (cfun->can_throw_non_call_exceptions)
7335 copy_reg_eh_region_note_forward (insn, get_insns (), NULL); 7501 copy_reg_eh_region_note_forward (insn, get_insns (), NULL);
7336 7502
7337 /* End this sequence. */ 7503 /* End this sequence. */
7338 *where = get_insns (); 7504 *where = get_insns ();
7339 end_sequence (); 7505 end_sequence ();
7549 other_output_reload_insns[rl->opnum] = get_insns (); 7715 other_output_reload_insns[rl->opnum] = get_insns ();
7550 } 7716 }
7551 else 7717 else
7552 output_reload_insns[rl->opnum] = get_insns (); 7718 output_reload_insns[rl->opnum] = get_insns ();
7553 7719
7554 if (flag_non_call_exceptions) 7720 if (cfun->can_throw_non_call_exceptions)
7555 copy_reg_eh_region_note_forward (insn, get_insns (), NULL); 7721 copy_reg_eh_region_note_forward (insn, get_insns (), NULL);
7556 7722
7557 end_sequence (); 7723 end_sequence ();
7558 } 7724 }
7559 7725
7918 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k); 8084 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
7919 8085
7920 /* Maybe the spill reg contains a copy of reload_out. */ 8086 /* Maybe the spill reg contains a copy of reload_out. */
7921 if (rld[r].out != 0 8087 if (rld[r].out != 0
7922 && (REG_P (rld[r].out) 8088 && (REG_P (rld[r].out)
7923 #ifdef AUTO_INC_DEC 8089 || (rld[r].out_reg
7924 || ! rld[r].out_reg 8090 ? REG_P (rld[r].out_reg)
7925 #endif 8091 /* The reload value is an auto-modification of
7926 || REG_P (rld[r].out_reg))) 8092 some kind. For PRE_INC, POST_INC, PRE_DEC
8093 and POST_DEC, we record an equivalence
8094 between the reload register and the operand
8095 on the optimistic assumption that we can make
8096 the equivalence hold. reload_as_needed must
8097 then either make it hold or invalidate the
8098 equivalence.
8099
8100 PRE_MODIFY and POST_MODIFY addresses are reloaded
8101 somewhat differently, and allowing them here leads
8102 to problems. */
8103 : (GET_CODE (rld[r].out) != POST_MODIFY
8104 && GET_CODE (rld[r].out) != PRE_MODIFY))))
7927 { 8105 {
7928 rtx reg; 8106 rtx reg;
7929 enum machine_mode mode; 8107 enum machine_mode mode;
7930 int regno, nregs; 8108 int regno, nregs;
7931 8109
8337 8515
8338 If there is another way to do this for a specific machine, a 8516 If there is another way to do this for a specific machine, a
8339 DEFINE_PEEPHOLE should be specified that recognizes the sequence 8517 DEFINE_PEEPHOLE should be specified that recognizes the sequence
8340 we emit below. */ 8518 we emit below. */
8341 8519
8342 code = (int) optab_handler (add_optab, GET_MODE (out))->insn_code; 8520 code = (int) optab_handler (add_optab, GET_MODE (out));
8343 8521
8344 if (CONSTANT_P (op1) || MEM_P (op1) || GET_CODE (op1) == SUBREG 8522 if (CONSTANT_P (op1) || MEM_P (op1) || GET_CODE (op1) == SUBREG
8345 || (REG_P (op1) 8523 || (REG_P (op1)
8346 && REGNO (op1) >= FIRST_PSEUDO_REGISTER) 8524 && REGNO (op1) >= FIRST_PSEUDO_REGISTER)
8347 || (code != CODE_FOR_nothing 8525 || (code != CODE_FOR_nothing
8436 { 8614 {
8437 set_unique_reg_note (insn, REG_EQUIV, in); 8615 set_unique_reg_note (insn, REG_EQUIV, in);
8438 return insn; 8616 return insn;
8439 } 8617 }
8440 8618
8441 fatal_insn ("Failure trying to reload:", set); 8619 fatal_insn ("failure trying to reload:", set);
8442 } 8620 }
8443 /* If IN is a simple operand, use gen_move_insn. */ 8621 /* If IN is a simple operand, use gen_move_insn. */
8444 else if (OBJECT_P (in) || GET_CODE (in) == SUBREG) 8622 else if (OBJECT_P (in) || GET_CODE (in) == SUBREG)
8445 { 8623 {
8446 tem = emit_insn (gen_move_insn (out, in)); 8624 tem = emit_insn (gen_move_insn (out, in));
8484 int k; 8662 int k;
8485 int n_occurrences; 8663 int n_occurrences;
8486 int n_inherited = 0; 8664 int n_inherited = 0;
8487 rtx i1; 8665 rtx i1;
8488 rtx substed; 8666 rtx substed;
8667 unsigned regno;
8668 int nregs;
8489 8669
8490 /* It is possible that this reload has been only used to set another reload 8670 /* It is possible that this reload has been only used to set another reload
8491 we eliminated earlier and thus deleted this instruction too. */ 8671 we eliminated earlier and thus deleted this instruction too. */
8492 if (INSN_DELETED_P (output_reload_insn)) 8672 if (INSN_DELETED_P (output_reload_insn))
8493 return; 8673 return;
8535 n_occurrences += count_occurrences (PATTERN (insn), XEXP (i1, 0), 0); 8715 n_occurrences += count_occurrences (PATTERN (insn), XEXP (i1, 0), 0);
8536 } 8716 }
8537 if (n_occurrences > n_inherited) 8717 if (n_occurrences > n_inherited)
8538 return; 8718 return;
8539 8719
8720 regno = REGNO (reg);
8721 if (regno >= FIRST_PSEUDO_REGISTER)
8722 nregs = 1;
8723 else
8724 nregs = hard_regno_nregs[regno][GET_MODE (reg)];
8725
8540 /* If the pseudo-reg we are reloading is no longer referenced 8726 /* If the pseudo-reg we are reloading is no longer referenced
8541 anywhere between the store into it and here, 8727 anywhere between the store into it and here,
8542 and we're within the same basic block, then the value can only 8728 and we're within the same basic block, then the value can only
8543 pass through the reload reg and end up here. 8729 pass through the reload reg and end up here.
8544 Otherwise, give up--return. */ 8730 Otherwise, give up--return. */
8546 i1 != insn; i1 = NEXT_INSN (i1)) 8732 i1 != insn; i1 = NEXT_INSN (i1))
8547 { 8733 {
8548 if (NOTE_INSN_BASIC_BLOCK_P (i1)) 8734 if (NOTE_INSN_BASIC_BLOCK_P (i1))
8549 return; 8735 return;
8550 if ((NONJUMP_INSN_P (i1) || CALL_P (i1)) 8736 if ((NONJUMP_INSN_P (i1) || CALL_P (i1))
8551 && reg_mentioned_p (reg, PATTERN (i1))) 8737 && refers_to_regno_p (regno, regno + nregs, PATTERN (i1), NULL))
8552 { 8738 {
8553 /* If this is USE in front of INSN, we only have to check that 8739 /* If this is USE in front of INSN, we only have to check that
8554 there are no more references than accounted for by inheritance. */ 8740 there are no more references than accounted for by inheritance. */
8555 while (NONJUMP_INSN_P (i1) && GET_CODE (PATTERN (i1)) == USE) 8741 while (NONJUMP_INSN_P (i1) && GET_CODE (PATTERN (i1)) == USE)
8556 { 8742 {
8857 /* If this is a pre-increment and we have incremented the value 9043 /* If this is a pre-increment and we have incremented the value
8858 where it lives, copy the incremented value to RELOADREG to 9044 where it lives, copy the incremented value to RELOADREG to
8859 be used as an address. */ 9045 be used as an address. */
8860 9046
8861 if (! post) 9047 if (! post)
8862 emit_insn (gen_move_insn (reloadreg, incloc)); 9048 add_insn = emit_insn (gen_move_insn (reloadreg, incloc));
8863 9049
8864 return add_insn; 9050 return add_insn;
8865 } 9051 }
8866 } 9052 }
8867 delete_insns_since (last); 9053 delete_insns_since (last);
8974 9160
8975 stop = NEXT_INSN (BB_END (bb)); 9161 stop = NEXT_INSN (BB_END (bb));
8976 BB_END (bb) = insn; 9162 BB_END (bb) = insn;
8977 insn = NEXT_INSN (insn); 9163 insn = NEXT_INSN (insn);
8978 9164
8979 FOR_EACH_EDGE (e, ei, bb->succs) 9165 e = find_fallthru_edge (bb->succs);
8980 if (e->flags & EDGE_FALLTHRU)
8981 break;
8982 9166
8983 while (insn && insn != stop) 9167 while (insn && insn != stop)
8984 { 9168 {
8985 next = NEXT_INSN (insn); 9169 next = NEXT_INSN (insn);
8986 if (INSN_P (insn)) 9170 if (INSN_P (insn))
9017 purge_dead_edges (bb); 9201 purge_dead_edges (bb);
9018 } 9202 }
9019 } 9203 }
9020 9204
9021 /* We've possibly turned single trapping insn into multiple ones. */ 9205 /* We've possibly turned single trapping insn into multiple ones. */
9022 if (flag_non_call_exceptions) 9206 if (cfun->can_throw_non_call_exceptions)
9023 { 9207 {
9024 sbitmap blocks; 9208 sbitmap blocks;
9025 blocks = sbitmap_alloc (last_basic_block); 9209 blocks = sbitmap_alloc (last_basic_block);
9026 sbitmap_ones (blocks); 9210 sbitmap_ones (blocks);
9027 find_many_sub_basic_blocks (blocks); 9211 find_many_sub_basic_blocks (blocks);