xref: /dflybsd-src/contrib/gcc-4.7/gcc/bt-load.c (revision 04febcfb30580676d3e95f58a16c5137ee478b32)
1*e4b17023SJohn Marino /* Perform branch target register load optimizations.
2*e4b17023SJohn Marino    Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3*e4b17023SJohn Marino    Free Software Foundation, Inc.
4*e4b17023SJohn Marino 
5*e4b17023SJohn Marino This file is part of GCC.
6*e4b17023SJohn Marino 
7*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it under
8*e4b17023SJohn Marino the terms of the GNU General Public License as published by the Free
9*e4b17023SJohn Marino Software Foundation; either version 3, or (at your option) any later
10*e4b17023SJohn Marino version.
11*e4b17023SJohn Marino 
12*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13*e4b17023SJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or
14*e4b17023SJohn Marino FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15*e4b17023SJohn Marino for more details.
16*e4b17023SJohn Marino 
17*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
18*e4b17023SJohn Marino along with GCC; see the file COPYING3.  If not see
19*e4b17023SJohn Marino <http://www.gnu.org/licenses/>.  */
20*e4b17023SJohn Marino 
21*e4b17023SJohn Marino #include "config.h"
22*e4b17023SJohn Marino #include "system.h"
23*e4b17023SJohn Marino #include "coretypes.h"
24*e4b17023SJohn Marino #include "tm.h"
25*e4b17023SJohn Marino #include "rtl.h"
26*e4b17023SJohn Marino #include "hard-reg-set.h"
27*e4b17023SJohn Marino #include "regs.h"
28*e4b17023SJohn Marino #include "fibheap.h"
29*e4b17023SJohn Marino #include "output.h"
30*e4b17023SJohn Marino #include "target.h"
31*e4b17023SJohn Marino #include "expr.h"
32*e4b17023SJohn Marino #include "flags.h"
33*e4b17023SJohn Marino #include "insn-attr.h"
34*e4b17023SJohn Marino #include "function.h"
35*e4b17023SJohn Marino #include "except.h"
36*e4b17023SJohn Marino #include "tm_p.h"
37*e4b17023SJohn Marino #include "diagnostic-core.h"
38*e4b17023SJohn Marino #include "tree-pass.h"
39*e4b17023SJohn Marino #include "recog.h"
40*e4b17023SJohn Marino #include "df.h"
41*e4b17023SJohn Marino 
42*e4b17023SJohn Marino /* Target register optimizations - these are performed after reload.  */
43*e4b17023SJohn Marino 
44*e4b17023SJohn Marino typedef struct btr_def_group_s
45*e4b17023SJohn Marino {
46*e4b17023SJohn Marino   struct btr_def_group_s *next;
47*e4b17023SJohn Marino   rtx src;
48*e4b17023SJohn Marino   struct btr_def_s *members;
49*e4b17023SJohn Marino } *btr_def_group;
50*e4b17023SJohn Marino 
51*e4b17023SJohn Marino typedef struct btr_user_s
52*e4b17023SJohn Marino {
53*e4b17023SJohn Marino   struct btr_user_s *next;
54*e4b17023SJohn Marino   basic_block bb;
55*e4b17023SJohn Marino   int luid;
56*e4b17023SJohn Marino   rtx insn;
57*e4b17023SJohn Marino   /* If INSN has a single use of a single branch register, then
58*e4b17023SJohn Marino      USE points to it within INSN.  If there is more than
59*e4b17023SJohn Marino      one branch register use, or the use is in some way ambiguous,
60*e4b17023SJohn Marino      then USE is NULL.  */
61*e4b17023SJohn Marino   rtx use;
62*e4b17023SJohn Marino   int n_reaching_defs;
63*e4b17023SJohn Marino   int first_reaching_def;
64*e4b17023SJohn Marino   char other_use_this_block;
65*e4b17023SJohn Marino } *btr_user;
66*e4b17023SJohn Marino 
67*e4b17023SJohn Marino /* btr_def structs appear on three lists:
68*e4b17023SJohn Marino      1. A list of all btr_def structures (head is
69*e4b17023SJohn Marino 	ALL_BTR_DEFS, linked by the NEXT field).
70*e4b17023SJohn Marino      2. A list of branch reg definitions per basic block (head is
71*e4b17023SJohn Marino 	BB_BTR_DEFS[i], linked by the NEXT_THIS_BB field).
72*e4b17023SJohn Marino      3. A list of all branch reg definitions belonging to the same
73*e4b17023SJohn Marino 	group (head is in a BTR_DEF_GROUP struct, linked by
74*e4b17023SJohn Marino 	NEXT_THIS_GROUP field).  */
75*e4b17023SJohn Marino 
76*e4b17023SJohn Marino typedef struct btr_def_s
77*e4b17023SJohn Marino {
78*e4b17023SJohn Marino   struct btr_def_s *next_this_bb;
79*e4b17023SJohn Marino   struct btr_def_s *next_this_group;
80*e4b17023SJohn Marino   basic_block bb;
81*e4b17023SJohn Marino   int luid;
82*e4b17023SJohn Marino   rtx insn;
83*e4b17023SJohn Marino   int btr;
84*e4b17023SJohn Marino   int cost;
85*e4b17023SJohn Marino   /* For a branch register setting insn that has a constant
86*e4b17023SJohn Marino      source (i.e. a label), group links together all the
87*e4b17023SJohn Marino      insns with the same source.  For other branch register
88*e4b17023SJohn Marino      setting insns, group is NULL.  */
89*e4b17023SJohn Marino   btr_def_group group;
90*e4b17023SJohn Marino   btr_user uses;
91*e4b17023SJohn Marino   /* If this def has a reaching use which is not a simple use
92*e4b17023SJohn Marino      in a branch instruction, then has_ambiguous_use will be true,
93*e4b17023SJohn Marino      and we will not attempt to migrate this definition.  */
94*e4b17023SJohn Marino   char has_ambiguous_use;
95*e4b17023SJohn Marino   /* live_range is an approximation to the true live range for this
96*e4b17023SJohn Marino      def/use web, because it records the set of blocks that contain
97*e4b17023SJohn Marino      the live range.  There could be other live ranges for the same
98*e4b17023SJohn Marino      branch register in that set of blocks, either in the block
99*e4b17023SJohn Marino      containing the def (before the def), or in a block containing
100*e4b17023SJohn Marino      a use (after the use).  If there are such other live ranges, then
101*e4b17023SJohn Marino      other_btr_uses_before_def or other_btr_uses_after_use must be set true
102*e4b17023SJohn Marino      as appropriate.  */
103*e4b17023SJohn Marino   char other_btr_uses_before_def;
104*e4b17023SJohn Marino   char other_btr_uses_after_use;
105*e4b17023SJohn Marino   /* We set own_end when we have moved a definition into a dominator.
106*e4b17023SJohn Marino      Thus, when a later combination removes this definition again, we know
107*e4b17023SJohn Marino      to clear out trs_live_at_end again.  */
108*e4b17023SJohn Marino   char own_end;
109*e4b17023SJohn Marino   bitmap live_range;
110*e4b17023SJohn Marino } *btr_def;
111*e4b17023SJohn Marino 
112*e4b17023SJohn Marino static int issue_rate;
113*e4b17023SJohn Marino 
114*e4b17023SJohn Marino static int basic_block_freq (const_basic_block);
115*e4b17023SJohn Marino static int insn_sets_btr_p (const_rtx, int, int *);
116*e4b17023SJohn Marino static rtx *find_btr_use (rtx);
117*e4b17023SJohn Marino static int btr_referenced_p (rtx, rtx *);
118*e4b17023SJohn Marino static int find_btr_reference (rtx *, void *);
119*e4b17023SJohn Marino static void find_btr_def_group (btr_def_group *, btr_def);
120*e4b17023SJohn Marino static btr_def add_btr_def (fibheap_t, basic_block, int, rtx,
121*e4b17023SJohn Marino 			    unsigned int, int, btr_def_group *);
122*e4b17023SJohn Marino static btr_user new_btr_user (basic_block, int, rtx);
123*e4b17023SJohn Marino static void dump_hard_reg_set (HARD_REG_SET);
124*e4b17023SJohn Marino static void dump_btrs_live (int);
125*e4b17023SJohn Marino static void note_other_use_this_block (unsigned int, btr_user);
126*e4b17023SJohn Marino static void compute_defs_uses_and_gen (fibheap_t, btr_def *,btr_user *,
127*e4b17023SJohn Marino 				       sbitmap *, sbitmap *, HARD_REG_SET *);
128*e4b17023SJohn Marino static void compute_kill (sbitmap *, sbitmap *, HARD_REG_SET *);
129*e4b17023SJohn Marino static void compute_out (sbitmap *bb_out, sbitmap *, sbitmap *, int);
130*e4b17023SJohn Marino static void link_btr_uses (btr_def *, btr_user *, sbitmap *, sbitmap *, int);
131*e4b17023SJohn Marino static void build_btr_def_use_webs (fibheap_t);
132*e4b17023SJohn Marino static int block_at_edge_of_live_range_p (int, btr_def);
133*e4b17023SJohn Marino static void clear_btr_from_live_range (btr_def def);
134*e4b17023SJohn Marino static void add_btr_to_live_range (btr_def, int);
135*e4b17023SJohn Marino static void augment_live_range (bitmap, HARD_REG_SET *, basic_block,
136*e4b17023SJohn Marino 				basic_block, int);
137*e4b17023SJohn Marino static int choose_btr (HARD_REG_SET);
138*e4b17023SJohn Marino static void combine_btr_defs (btr_def, HARD_REG_SET *);
139*e4b17023SJohn Marino static void btr_def_live_range (btr_def, HARD_REG_SET *);
140*e4b17023SJohn Marino static void move_btr_def (basic_block, int, btr_def, bitmap, HARD_REG_SET *);
141*e4b17023SJohn Marino static int migrate_btr_def (btr_def, int);
142*e4b17023SJohn Marino static void migrate_btr_defs (enum reg_class, int);
143*e4b17023SJohn Marino static int can_move_up (const_basic_block, const_rtx, int);
144*e4b17023SJohn Marino static void note_btr_set (rtx, const_rtx, void *);
145*e4b17023SJohn Marino 
146*e4b17023SJohn Marino /* The following code performs code motion of target load instructions
147*e4b17023SJohn Marino    (instructions that set branch target registers), to move them
148*e4b17023SJohn Marino    forward away from the branch instructions and out of loops (or,
149*e4b17023SJohn Marino    more generally, from a more frequently executed place to a less
150*e4b17023SJohn Marino    frequently executed place).
151*e4b17023SJohn Marino    Moving target load instructions further in front of the branch
152*e4b17023SJohn Marino    instruction that uses the target register value means that the hardware
153*e4b17023SJohn Marino    has a better chance of preloading the instructions at the branch
154*e4b17023SJohn Marino    target by the time the branch is reached.  This avoids bubbles
155*e4b17023SJohn Marino    when a taken branch needs to flush out the pipeline.
156*e4b17023SJohn Marino    Moving target load instructions out of loops means they are executed
157*e4b17023SJohn Marino    less frequently.  */
158*e4b17023SJohn Marino 
159*e4b17023SJohn Marino /* An obstack to hold the def-use web data structures built up for
160*e4b17023SJohn Marino    migrating branch target load instructions.  */
161*e4b17023SJohn Marino static struct obstack migrate_btrl_obstack;
162*e4b17023SJohn Marino 
163*e4b17023SJohn Marino /* Array indexed by basic block number, giving the set of registers
164*e4b17023SJohn Marino    live in that block.  */
165*e4b17023SJohn Marino static HARD_REG_SET *btrs_live;
166*e4b17023SJohn Marino 
167*e4b17023SJohn Marino /* Array indexed by basic block number, giving the set of registers live at
168*e4b17023SJohn Marino   the end of that block, including any uses by a final jump insn, if any.  */
169*e4b17023SJohn Marino static HARD_REG_SET *btrs_live_at_end;
170*e4b17023SJohn Marino 
171*e4b17023SJohn Marino /* Set of all target registers that we are willing to allocate.  */
172*e4b17023SJohn Marino static HARD_REG_SET all_btrs;
173*e4b17023SJohn Marino 
174*e4b17023SJohn Marino /* Provide lower and upper bounds for target register numbers, so that
175*e4b17023SJohn Marino    we don't need to search through all the hard registers all the time.  */
176*e4b17023SJohn Marino static int first_btr, last_btr;
177*e4b17023SJohn Marino 
178*e4b17023SJohn Marino 
179*e4b17023SJohn Marino 
180*e4b17023SJohn Marino /* Return an estimate of the frequency of execution of block bb.  */
181*e4b17023SJohn Marino static int
basic_block_freq(const_basic_block bb)182*e4b17023SJohn Marino basic_block_freq (const_basic_block bb)
183*e4b17023SJohn Marino {
184*e4b17023SJohn Marino   return bb->frequency;
185*e4b17023SJohn Marino }
186*e4b17023SJohn Marino 
187*e4b17023SJohn Marino static rtx *btr_reference_found;
188*e4b17023SJohn Marino 
189*e4b17023SJohn Marino /* A subroutine of btr_referenced_p, called through for_each_rtx.
190*e4b17023SJohn Marino    PREG is a pointer to an rtx that is to be excluded from the
191*e4b17023SJohn Marino    traversal.  If we find a reference to a target register anywhere
192*e4b17023SJohn Marino    else, return 1, and put a pointer to it into btr_reference_found.  */
193*e4b17023SJohn Marino static int
find_btr_reference(rtx * px,void * preg)194*e4b17023SJohn Marino find_btr_reference (rtx *px, void *preg)
195*e4b17023SJohn Marino {
196*e4b17023SJohn Marino   rtx x;
197*e4b17023SJohn Marino 
198*e4b17023SJohn Marino   if (px == preg)
199*e4b17023SJohn Marino     return -1;
200*e4b17023SJohn Marino   x = *px;
201*e4b17023SJohn Marino   if (!REG_P (x))
202*e4b17023SJohn Marino     return 0;
203*e4b17023SJohn Marino   if (overlaps_hard_reg_set_p (all_btrs, GET_MODE (x), REGNO (x)))
204*e4b17023SJohn Marino     {
205*e4b17023SJohn Marino       btr_reference_found = px;
206*e4b17023SJohn Marino       return 1;
207*e4b17023SJohn Marino     }
208*e4b17023SJohn Marino   return -1;
209*e4b17023SJohn Marino }
210*e4b17023SJohn Marino 
211*e4b17023SJohn Marino /* Return nonzero if X references (sets or reads) any branch target register.
212*e4b17023SJohn Marino    If EXCLUDEP is set, disregard any references within the rtx pointed to
213*e4b17023SJohn Marino    by it.  If returning nonzero, also set btr_reference_found as above.  */
214*e4b17023SJohn Marino static int
btr_referenced_p(rtx x,rtx * excludep)215*e4b17023SJohn Marino btr_referenced_p (rtx x, rtx *excludep)
216*e4b17023SJohn Marino {
217*e4b17023SJohn Marino   return for_each_rtx (&x, find_btr_reference, excludep);
218*e4b17023SJohn Marino }
219*e4b17023SJohn Marino 
220*e4b17023SJohn Marino /* Return true if insn is an instruction that sets a target register.
221*e4b17023SJohn Marino    if CHECK_CONST is true, only return true if the source is constant.
222*e4b17023SJohn Marino    If such a set is found and REGNO is nonzero, assign the register number
223*e4b17023SJohn Marino    of the destination register to *REGNO.  */
224*e4b17023SJohn Marino static int
insn_sets_btr_p(const_rtx insn,int check_const,int * regno)225*e4b17023SJohn Marino insn_sets_btr_p (const_rtx insn, int check_const, int *regno)
226*e4b17023SJohn Marino {
227*e4b17023SJohn Marino   rtx set;
228*e4b17023SJohn Marino 
229*e4b17023SJohn Marino   if (NONJUMP_INSN_P (insn)
230*e4b17023SJohn Marino       && (set = single_set (insn)))
231*e4b17023SJohn Marino     {
232*e4b17023SJohn Marino       rtx dest = SET_DEST (set);
233*e4b17023SJohn Marino       rtx src = SET_SRC (set);
234*e4b17023SJohn Marino 
235*e4b17023SJohn Marino       if (GET_CODE (dest) == SUBREG)
236*e4b17023SJohn Marino 	dest = XEXP (dest, 0);
237*e4b17023SJohn Marino 
238*e4b17023SJohn Marino       if (REG_P (dest)
239*e4b17023SJohn Marino 	  && TEST_HARD_REG_BIT (all_btrs, REGNO (dest)))
240*e4b17023SJohn Marino 	{
241*e4b17023SJohn Marino 	  gcc_assert (!btr_referenced_p (src, NULL));
242*e4b17023SJohn Marino 
243*e4b17023SJohn Marino 	  if (!check_const || CONSTANT_P (src))
244*e4b17023SJohn Marino 	    {
245*e4b17023SJohn Marino 	      if (regno)
246*e4b17023SJohn Marino 		*regno = REGNO (dest);
247*e4b17023SJohn Marino 	      return 1;
248*e4b17023SJohn Marino 	    }
249*e4b17023SJohn Marino 	}
250*e4b17023SJohn Marino     }
251*e4b17023SJohn Marino   return 0;
252*e4b17023SJohn Marino }
253*e4b17023SJohn Marino 
254*e4b17023SJohn Marino /* Find and return a use of a target register within an instruction INSN.  */
255*e4b17023SJohn Marino static rtx *
find_btr_use(rtx insn)256*e4b17023SJohn Marino find_btr_use (rtx insn)
257*e4b17023SJohn Marino {
258*e4b17023SJohn Marino   return btr_referenced_p (insn, NULL) ? btr_reference_found : NULL;
259*e4b17023SJohn Marino }
260*e4b17023SJohn Marino 
261*e4b17023SJohn Marino /* Find the group that the target register definition DEF belongs
262*e4b17023SJohn Marino    to in the list starting with *ALL_BTR_DEF_GROUPS.  If no such
263*e4b17023SJohn Marino    group exists, create one.  Add def to the group.  */
264*e4b17023SJohn Marino static void
find_btr_def_group(btr_def_group * all_btr_def_groups,btr_def def)265*e4b17023SJohn Marino find_btr_def_group (btr_def_group *all_btr_def_groups, btr_def def)
266*e4b17023SJohn Marino {
267*e4b17023SJohn Marino   if (insn_sets_btr_p (def->insn, 1, NULL))
268*e4b17023SJohn Marino     {
269*e4b17023SJohn Marino       btr_def_group this_group;
270*e4b17023SJohn Marino       rtx def_src = SET_SRC (single_set (def->insn));
271*e4b17023SJohn Marino 
272*e4b17023SJohn Marino       /* ?? This linear search is an efficiency concern, particularly
273*e4b17023SJohn Marino 	 as the search will almost always fail to find a match.  */
274*e4b17023SJohn Marino       for (this_group = *all_btr_def_groups;
275*e4b17023SJohn Marino 	   this_group != NULL;
276*e4b17023SJohn Marino 	   this_group = this_group->next)
277*e4b17023SJohn Marino 	if (rtx_equal_p (def_src, this_group->src))
278*e4b17023SJohn Marino 	  break;
279*e4b17023SJohn Marino 
280*e4b17023SJohn Marino       if (!this_group)
281*e4b17023SJohn Marino 	{
282*e4b17023SJohn Marino 	  this_group = XOBNEW (&migrate_btrl_obstack, struct btr_def_group_s);
283*e4b17023SJohn Marino 	  this_group->src = def_src;
284*e4b17023SJohn Marino 	  this_group->members = NULL;
285*e4b17023SJohn Marino 	  this_group->next = *all_btr_def_groups;
286*e4b17023SJohn Marino 	  *all_btr_def_groups = this_group;
287*e4b17023SJohn Marino 	}
288*e4b17023SJohn Marino       def->group = this_group;
289*e4b17023SJohn Marino       def->next_this_group = this_group->members;
290*e4b17023SJohn Marino       this_group->members = def;
291*e4b17023SJohn Marino     }
292*e4b17023SJohn Marino   else
293*e4b17023SJohn Marino     def->group = NULL;
294*e4b17023SJohn Marino }
295*e4b17023SJohn Marino 
296*e4b17023SJohn Marino /* Create a new target register definition structure, for a definition in
297*e4b17023SJohn Marino    block BB, instruction INSN, and insert it into ALL_BTR_DEFS.  Return
298*e4b17023SJohn Marino    the new definition.  */
299*e4b17023SJohn Marino static btr_def
add_btr_def(fibheap_t all_btr_defs,basic_block bb,int insn_luid,rtx insn,unsigned int dest_reg,int other_btr_uses_before_def,btr_def_group * all_btr_def_groups)300*e4b17023SJohn Marino add_btr_def (fibheap_t all_btr_defs, basic_block bb, int insn_luid, rtx insn,
301*e4b17023SJohn Marino 	     unsigned int dest_reg, int other_btr_uses_before_def,
302*e4b17023SJohn Marino 	     btr_def_group *all_btr_def_groups)
303*e4b17023SJohn Marino {
304*e4b17023SJohn Marino   btr_def this_def = XOBNEW (&migrate_btrl_obstack, struct btr_def_s);
305*e4b17023SJohn Marino   this_def->bb = bb;
306*e4b17023SJohn Marino   this_def->luid = insn_luid;
307*e4b17023SJohn Marino   this_def->insn = insn;
308*e4b17023SJohn Marino   this_def->btr = dest_reg;
309*e4b17023SJohn Marino   this_def->cost = basic_block_freq (bb);
310*e4b17023SJohn Marino   this_def->has_ambiguous_use = 0;
311*e4b17023SJohn Marino   this_def->other_btr_uses_before_def = other_btr_uses_before_def;
312*e4b17023SJohn Marino   this_def->other_btr_uses_after_use = 0;
313*e4b17023SJohn Marino   this_def->next_this_bb = NULL;
314*e4b17023SJohn Marino   this_def->next_this_group = NULL;
315*e4b17023SJohn Marino   this_def->uses = NULL;
316*e4b17023SJohn Marino   this_def->live_range = NULL;
317*e4b17023SJohn Marino   find_btr_def_group (all_btr_def_groups, this_def);
318*e4b17023SJohn Marino 
319*e4b17023SJohn Marino   fibheap_insert (all_btr_defs, -this_def->cost, this_def);
320*e4b17023SJohn Marino 
321*e4b17023SJohn Marino   if (dump_file)
322*e4b17023SJohn Marino     fprintf (dump_file,
323*e4b17023SJohn Marino       "Found target reg definition: sets %u { bb %d, insn %d }%s priority %d\n",
324*e4b17023SJohn Marino 	     dest_reg, bb->index, INSN_UID (insn),
325*e4b17023SJohn Marino 	     (this_def->group ? "" : ":not const"), this_def->cost);
326*e4b17023SJohn Marino 
327*e4b17023SJohn Marino   return this_def;
328*e4b17023SJohn Marino }
329*e4b17023SJohn Marino 
330*e4b17023SJohn Marino /* Create a new target register user structure, for a use in block BB,
331*e4b17023SJohn Marino    instruction INSN.  Return the new user.  */
332*e4b17023SJohn Marino static btr_user
new_btr_user(basic_block bb,int insn_luid,rtx insn)333*e4b17023SJohn Marino new_btr_user (basic_block bb, int insn_luid, rtx insn)
334*e4b17023SJohn Marino {
335*e4b17023SJohn Marino   /* This instruction reads target registers.  We need
336*e4b17023SJohn Marino      to decide whether we can replace all target register
337*e4b17023SJohn Marino      uses easily.
338*e4b17023SJohn Marino    */
339*e4b17023SJohn Marino   rtx *usep = find_btr_use (PATTERN (insn));
340*e4b17023SJohn Marino   rtx use;
341*e4b17023SJohn Marino   btr_user user = NULL;
342*e4b17023SJohn Marino 
343*e4b17023SJohn Marino   if (usep)
344*e4b17023SJohn Marino     {
345*e4b17023SJohn Marino       int unambiguous_single_use;
346*e4b17023SJohn Marino 
347*e4b17023SJohn Marino       /* We want to ensure that USE is the only use of a target
348*e4b17023SJohn Marino 	 register in INSN, so that we know that to rewrite INSN to use
349*e4b17023SJohn Marino 	 a different target register, all we have to do is replace USE.  */
350*e4b17023SJohn Marino       unambiguous_single_use = !btr_referenced_p (PATTERN (insn), usep);
351*e4b17023SJohn Marino       if (!unambiguous_single_use)
352*e4b17023SJohn Marino 	usep = NULL;
353*e4b17023SJohn Marino     }
354*e4b17023SJohn Marino   use = usep ? *usep : NULL_RTX;
355*e4b17023SJohn Marino   user = XOBNEW (&migrate_btrl_obstack, struct btr_user_s);
356*e4b17023SJohn Marino   user->bb = bb;
357*e4b17023SJohn Marino   user->luid = insn_luid;
358*e4b17023SJohn Marino   user->insn = insn;
359*e4b17023SJohn Marino   user->use = use;
360*e4b17023SJohn Marino   user->other_use_this_block = 0;
361*e4b17023SJohn Marino   user->next = NULL;
362*e4b17023SJohn Marino   user->n_reaching_defs = 0;
363*e4b17023SJohn Marino   user->first_reaching_def = -1;
364*e4b17023SJohn Marino 
365*e4b17023SJohn Marino   if (dump_file)
366*e4b17023SJohn Marino     {
367*e4b17023SJohn Marino       fprintf (dump_file, "Uses target reg: { bb %d, insn %d }",
368*e4b17023SJohn Marino 	       bb->index, INSN_UID (insn));
369*e4b17023SJohn Marino 
370*e4b17023SJohn Marino       if (user->use)
371*e4b17023SJohn Marino 	fprintf (dump_file, ": unambiguous use of reg %d\n",
372*e4b17023SJohn Marino 		 REGNO (user->use));
373*e4b17023SJohn Marino     }
374*e4b17023SJohn Marino 
375*e4b17023SJohn Marino   return user;
376*e4b17023SJohn Marino }
377*e4b17023SJohn Marino 
378*e4b17023SJohn Marino /* Write the contents of S to the dump file.  */
379*e4b17023SJohn Marino static void
dump_hard_reg_set(HARD_REG_SET s)380*e4b17023SJohn Marino dump_hard_reg_set (HARD_REG_SET s)
381*e4b17023SJohn Marino {
382*e4b17023SJohn Marino   int reg;
383*e4b17023SJohn Marino   for (reg = 0; reg < FIRST_PSEUDO_REGISTER; reg++)
384*e4b17023SJohn Marino     if (TEST_HARD_REG_BIT (s, reg))
385*e4b17023SJohn Marino       fprintf (dump_file, " %d", reg);
386*e4b17023SJohn Marino }
387*e4b17023SJohn Marino 
388*e4b17023SJohn Marino /* Write the set of target regs live in block BB to the dump file.  */
389*e4b17023SJohn Marino static void
dump_btrs_live(int bb)390*e4b17023SJohn Marino dump_btrs_live (int bb)
391*e4b17023SJohn Marino {
392*e4b17023SJohn Marino   fprintf (dump_file, "BB%d live:", bb);
393*e4b17023SJohn Marino   dump_hard_reg_set (btrs_live[bb]);
394*e4b17023SJohn Marino   fprintf (dump_file, "\n");
395*e4b17023SJohn Marino }
396*e4b17023SJohn Marino 
397*e4b17023SJohn Marino /* REGNO is the number of a branch target register that is being used or
398*e4b17023SJohn Marino    set.  USERS_THIS_BB is a list of preceding branch target register users;
399*e4b17023SJohn Marino    If any of them use the same register, set their other_use_this_block
400*e4b17023SJohn Marino    flag.  */
401*e4b17023SJohn Marino static void
note_other_use_this_block(unsigned int regno,btr_user users_this_bb)402*e4b17023SJohn Marino note_other_use_this_block (unsigned int regno, btr_user users_this_bb)
403*e4b17023SJohn Marino {
404*e4b17023SJohn Marino   btr_user user;
405*e4b17023SJohn Marino 
406*e4b17023SJohn Marino   for (user = users_this_bb; user != NULL; user = user->next)
407*e4b17023SJohn Marino     if (user->use && REGNO (user->use) == regno)
408*e4b17023SJohn Marino       user->other_use_this_block = 1;
409*e4b17023SJohn Marino }
410*e4b17023SJohn Marino 
411*e4b17023SJohn Marino typedef struct {
412*e4b17023SJohn Marino   btr_user users_this_bb;
413*e4b17023SJohn Marino   HARD_REG_SET btrs_written_in_block;
414*e4b17023SJohn Marino   HARD_REG_SET btrs_live_in_block;
415*e4b17023SJohn Marino   sbitmap bb_gen;
416*e4b17023SJohn Marino   sbitmap *btr_defset;
417*e4b17023SJohn Marino } defs_uses_info;
418*e4b17023SJohn Marino 
419*e4b17023SJohn Marino /* Called via note_stores or directly to register stores into /
420*e4b17023SJohn Marino    clobbers of a branch target register DEST that are not recognized as
421*e4b17023SJohn Marino    straightforward definitions.  DATA points to information about the
422*e4b17023SJohn Marino    current basic block that needs updating.  */
423*e4b17023SJohn Marino static void
note_btr_set(rtx dest,const_rtx set ATTRIBUTE_UNUSED,void * data)424*e4b17023SJohn Marino note_btr_set (rtx dest, const_rtx set ATTRIBUTE_UNUSED, void *data)
425*e4b17023SJohn Marino {
426*e4b17023SJohn Marino   defs_uses_info *info = (defs_uses_info *) data;
427*e4b17023SJohn Marino   int regno, end_regno;
428*e4b17023SJohn Marino 
429*e4b17023SJohn Marino   if (!REG_P (dest))
430*e4b17023SJohn Marino     return;
431*e4b17023SJohn Marino   regno = REGNO (dest);
432*e4b17023SJohn Marino   end_regno = END_HARD_REGNO (dest);
433*e4b17023SJohn Marino   for (; regno < end_regno; regno++)
434*e4b17023SJohn Marino     if (TEST_HARD_REG_BIT (all_btrs, regno))
435*e4b17023SJohn Marino       {
436*e4b17023SJohn Marino 	note_other_use_this_block (regno, info->users_this_bb);
437*e4b17023SJohn Marino 	SET_HARD_REG_BIT (info->btrs_written_in_block, regno);
438*e4b17023SJohn Marino 	SET_HARD_REG_BIT (info->btrs_live_in_block, regno);
439*e4b17023SJohn Marino 	sbitmap_difference (info->bb_gen, info->bb_gen,
440*e4b17023SJohn Marino 			    info->btr_defset[regno - first_btr]);
441*e4b17023SJohn Marino       }
442*e4b17023SJohn Marino }
443*e4b17023SJohn Marino 
444*e4b17023SJohn Marino static void
compute_defs_uses_and_gen(fibheap_t all_btr_defs,btr_def * def_array,btr_user * use_array,sbitmap * btr_defset,sbitmap * bb_gen,HARD_REG_SET * btrs_written)445*e4b17023SJohn Marino compute_defs_uses_and_gen (fibheap_t all_btr_defs, btr_def *def_array,
446*e4b17023SJohn Marino 			   btr_user *use_array, sbitmap *btr_defset,
447*e4b17023SJohn Marino 			   sbitmap *bb_gen, HARD_REG_SET *btrs_written)
448*e4b17023SJohn Marino {
449*e4b17023SJohn Marino   /* Scan the code building up the set of all defs and all uses.
450*e4b17023SJohn Marino      For each target register, build the set of defs of that register.
451*e4b17023SJohn Marino      For each block, calculate the set of target registers
452*e4b17023SJohn Marino      written in that block.
453*e4b17023SJohn Marino      Also calculate the set of btrs ever live in that block.
454*e4b17023SJohn Marino   */
455*e4b17023SJohn Marino   int i;
456*e4b17023SJohn Marino   int insn_luid = 0;
457*e4b17023SJohn Marino   btr_def_group all_btr_def_groups = NULL;
458*e4b17023SJohn Marino   defs_uses_info info;
459*e4b17023SJohn Marino 
460*e4b17023SJohn Marino   sbitmap_vector_zero (bb_gen, last_basic_block);
461*e4b17023SJohn Marino   for (i = NUM_FIXED_BLOCKS; i < last_basic_block; i++)
462*e4b17023SJohn Marino     {
463*e4b17023SJohn Marino       basic_block bb = BASIC_BLOCK (i);
464*e4b17023SJohn Marino       int reg;
465*e4b17023SJohn Marino       btr_def defs_this_bb = NULL;
466*e4b17023SJohn Marino       rtx insn;
467*e4b17023SJohn Marino       rtx last;
468*e4b17023SJohn Marino       int can_throw = 0;
469*e4b17023SJohn Marino 
470*e4b17023SJohn Marino       info.users_this_bb = NULL;
471*e4b17023SJohn Marino       info.bb_gen = bb_gen[i];
472*e4b17023SJohn Marino       info.btr_defset = btr_defset;
473*e4b17023SJohn Marino 
474*e4b17023SJohn Marino       CLEAR_HARD_REG_SET (info.btrs_live_in_block);
475*e4b17023SJohn Marino       CLEAR_HARD_REG_SET (info.btrs_written_in_block);
476*e4b17023SJohn Marino       for (reg = first_btr; reg <= last_btr; reg++)
477*e4b17023SJohn Marino 	if (TEST_HARD_REG_BIT (all_btrs, reg)
478*e4b17023SJohn Marino 	    && REGNO_REG_SET_P (df_get_live_in (bb), reg))
479*e4b17023SJohn Marino 	  SET_HARD_REG_BIT (info.btrs_live_in_block, reg);
480*e4b17023SJohn Marino 
481*e4b17023SJohn Marino       for (insn = BB_HEAD (bb), last = NEXT_INSN (BB_END (bb));
482*e4b17023SJohn Marino 	   insn != last;
483*e4b17023SJohn Marino 	   insn = NEXT_INSN (insn), insn_luid++)
484*e4b17023SJohn Marino 	{
485*e4b17023SJohn Marino 	  if (INSN_P (insn))
486*e4b17023SJohn Marino 	    {
487*e4b17023SJohn Marino 	      int regno;
488*e4b17023SJohn Marino 	      int insn_uid = INSN_UID (insn);
489*e4b17023SJohn Marino 
490*e4b17023SJohn Marino 	      if (insn_sets_btr_p (insn, 0, &regno))
491*e4b17023SJohn Marino 		{
492*e4b17023SJohn Marino 		  btr_def def = add_btr_def (
493*e4b17023SJohn Marino 		      all_btr_defs, bb, insn_luid, insn, regno,
494*e4b17023SJohn Marino 		      TEST_HARD_REG_BIT (info.btrs_live_in_block, regno),
495*e4b17023SJohn Marino 		      &all_btr_def_groups);
496*e4b17023SJohn Marino 
497*e4b17023SJohn Marino 		  def_array[insn_uid] = def;
498*e4b17023SJohn Marino 		  SET_HARD_REG_BIT (info.btrs_written_in_block, regno);
499*e4b17023SJohn Marino 		  SET_HARD_REG_BIT (info.btrs_live_in_block, regno);
500*e4b17023SJohn Marino 		  sbitmap_difference (bb_gen[i], bb_gen[i],
501*e4b17023SJohn Marino 				      btr_defset[regno - first_btr]);
502*e4b17023SJohn Marino 		  SET_BIT (bb_gen[i], insn_uid);
503*e4b17023SJohn Marino 		  def->next_this_bb = defs_this_bb;
504*e4b17023SJohn Marino 		  defs_this_bb = def;
505*e4b17023SJohn Marino 		  SET_BIT (btr_defset[regno - first_btr], insn_uid);
506*e4b17023SJohn Marino 		  note_other_use_this_block (regno, info.users_this_bb);
507*e4b17023SJohn Marino 		}
508*e4b17023SJohn Marino 	      /* Check for the blockage emitted by expand_nl_goto_receiver.  */
509*e4b17023SJohn Marino 	      else if (cfun->has_nonlocal_label
510*e4b17023SJohn Marino 		       && GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE)
511*e4b17023SJohn Marino 		{
512*e4b17023SJohn Marino 		  btr_user user;
513*e4b17023SJohn Marino 
514*e4b17023SJohn Marino 		  /* Do the equivalent of calling note_other_use_this_block
515*e4b17023SJohn Marino 		     for every target register.  */
516*e4b17023SJohn Marino 		  for (user = info.users_this_bb; user != NULL;
517*e4b17023SJohn Marino 		       user = user->next)
518*e4b17023SJohn Marino 		    if (user->use)
519*e4b17023SJohn Marino 		      user->other_use_this_block = 1;
520*e4b17023SJohn Marino 		  IOR_HARD_REG_SET (info.btrs_written_in_block, all_btrs);
521*e4b17023SJohn Marino 		  IOR_HARD_REG_SET (info.btrs_live_in_block, all_btrs);
522*e4b17023SJohn Marino 		  sbitmap_zero (info.bb_gen);
523*e4b17023SJohn Marino 		}
524*e4b17023SJohn Marino 	      else
525*e4b17023SJohn Marino 		{
526*e4b17023SJohn Marino 		  if (btr_referenced_p (PATTERN (insn), NULL))
527*e4b17023SJohn Marino 		    {
528*e4b17023SJohn Marino 		      btr_user user = new_btr_user (bb, insn_luid, insn);
529*e4b17023SJohn Marino 
530*e4b17023SJohn Marino 		      use_array[insn_uid] = user;
531*e4b17023SJohn Marino 		      if (user->use)
532*e4b17023SJohn Marino 			SET_HARD_REG_BIT (info.btrs_live_in_block,
533*e4b17023SJohn Marino 					  REGNO (user->use));
534*e4b17023SJohn Marino 		      else
535*e4b17023SJohn Marino 			{
536*e4b17023SJohn Marino 			  int reg;
537*e4b17023SJohn Marino 			  for (reg = first_btr; reg <= last_btr; reg++)
538*e4b17023SJohn Marino 			    if (TEST_HARD_REG_BIT (all_btrs, reg)
539*e4b17023SJohn Marino 				&& refers_to_regno_p (reg, reg + 1, user->insn,
540*e4b17023SJohn Marino 						      NULL))
541*e4b17023SJohn Marino 			      {
542*e4b17023SJohn Marino 				note_other_use_this_block (reg,
543*e4b17023SJohn Marino 							   info.users_this_bb);
544*e4b17023SJohn Marino 				SET_HARD_REG_BIT (info.btrs_live_in_block, reg);
545*e4b17023SJohn Marino 			      }
546*e4b17023SJohn Marino 			  note_stores (PATTERN (insn), note_btr_set, &info);
547*e4b17023SJohn Marino 			}
548*e4b17023SJohn Marino 		      user->next = info.users_this_bb;
549*e4b17023SJohn Marino 		      info.users_this_bb = user;
550*e4b17023SJohn Marino 		    }
551*e4b17023SJohn Marino 		  if (CALL_P (insn))
552*e4b17023SJohn Marino 		    {
553*e4b17023SJohn Marino 		      HARD_REG_SET *clobbered = &call_used_reg_set;
554*e4b17023SJohn Marino 		      HARD_REG_SET call_saved;
555*e4b17023SJohn Marino 		      rtx pat = PATTERN (insn);
556*e4b17023SJohn Marino 		      int i;
557*e4b17023SJohn Marino 
558*e4b17023SJohn Marino 		      /* Check for sibcall.  */
559*e4b17023SJohn Marino 		      if (GET_CODE (pat) == PARALLEL)
560*e4b17023SJohn Marino 			for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
561*e4b17023SJohn Marino 			  if (ANY_RETURN_P (XVECEXP (pat, 0, i)))
562*e4b17023SJohn Marino 			    {
563*e4b17023SJohn Marino 			      COMPL_HARD_REG_SET (call_saved,
564*e4b17023SJohn Marino 						  call_used_reg_set);
565*e4b17023SJohn Marino 			      clobbered = &call_saved;
566*e4b17023SJohn Marino 			    }
567*e4b17023SJohn Marino 
568*e4b17023SJohn Marino 		      for (regno = first_btr; regno <= last_btr; regno++)
569*e4b17023SJohn Marino 			if (TEST_HARD_REG_BIT (*clobbered, regno))
570*e4b17023SJohn Marino 			  note_btr_set (regno_reg_rtx[regno], NULL_RTX, &info);
571*e4b17023SJohn Marino 		    }
572*e4b17023SJohn Marino 		}
573*e4b17023SJohn Marino 	    }
574*e4b17023SJohn Marino 	}
575*e4b17023SJohn Marino 
576*e4b17023SJohn Marino       COPY_HARD_REG_SET (btrs_live[i], info.btrs_live_in_block);
577*e4b17023SJohn Marino       COPY_HARD_REG_SET (btrs_written[i], info.btrs_written_in_block);
578*e4b17023SJohn Marino 
579*e4b17023SJohn Marino       REG_SET_TO_HARD_REG_SET (btrs_live_at_end[i], df_get_live_out (bb));
580*e4b17023SJohn Marino       /* If this block ends in a jump insn, add any uses or even clobbers
581*e4b17023SJohn Marino 	 of branch target registers that it might have.  */
582*e4b17023SJohn Marino       for (insn = BB_END (bb); insn != BB_HEAD (bb) && ! INSN_P (insn); )
583*e4b17023SJohn Marino 	insn = PREV_INSN (insn);
584*e4b17023SJohn Marino       /* ??? for the fall-through edge, it would make sense to insert the
585*e4b17023SJohn Marino 	 btr set on the edge, but that would require to split the block
586*e4b17023SJohn Marino 	 early on so that we can distinguish between dominance from the fall
587*e4b17023SJohn Marino 	 through edge - which can use the call-clobbered registers - from
588*e4b17023SJohn Marino 	 dominance by the throw edge.  */
589*e4b17023SJohn Marino       if (can_throw_internal (insn))
590*e4b17023SJohn Marino 	{
591*e4b17023SJohn Marino 	  HARD_REG_SET tmp;
592*e4b17023SJohn Marino 
593*e4b17023SJohn Marino 	  COPY_HARD_REG_SET (tmp, call_used_reg_set);
594*e4b17023SJohn Marino 	  AND_HARD_REG_SET (tmp, all_btrs);
595*e4b17023SJohn Marino 	  IOR_HARD_REG_SET (btrs_live_at_end[i], tmp);
596*e4b17023SJohn Marino 	  can_throw = 1;
597*e4b17023SJohn Marino 	}
598*e4b17023SJohn Marino       if (can_throw || JUMP_P (insn))
599*e4b17023SJohn Marino 	{
600*e4b17023SJohn Marino 	  int regno;
601*e4b17023SJohn Marino 
602*e4b17023SJohn Marino 	  for (regno = first_btr; regno <= last_btr; regno++)
603*e4b17023SJohn Marino 	    if (refers_to_regno_p (regno, regno+1, insn, NULL))
604*e4b17023SJohn Marino 	      SET_HARD_REG_BIT (btrs_live_at_end[i], regno);
605*e4b17023SJohn Marino 	}
606*e4b17023SJohn Marino 
607*e4b17023SJohn Marino       if (dump_file)
608*e4b17023SJohn Marino 	dump_btrs_live(i);
609*e4b17023SJohn Marino     }
610*e4b17023SJohn Marino }
611*e4b17023SJohn Marino 
612*e4b17023SJohn Marino static void
compute_kill(sbitmap * bb_kill,sbitmap * btr_defset,HARD_REG_SET * btrs_written)613*e4b17023SJohn Marino compute_kill (sbitmap *bb_kill, sbitmap *btr_defset,
614*e4b17023SJohn Marino 	      HARD_REG_SET *btrs_written)
615*e4b17023SJohn Marino {
616*e4b17023SJohn Marino   int i;
617*e4b17023SJohn Marino   int regno;
618*e4b17023SJohn Marino 
619*e4b17023SJohn Marino   /* For each basic block, form the set BB_KILL - the set
620*e4b17023SJohn Marino      of definitions that the block kills.  */
621*e4b17023SJohn Marino   sbitmap_vector_zero (bb_kill, last_basic_block);
622*e4b17023SJohn Marino   for (i = NUM_FIXED_BLOCKS; i < last_basic_block; i++)
623*e4b17023SJohn Marino     {
624*e4b17023SJohn Marino       for (regno = first_btr; regno <= last_btr; regno++)
625*e4b17023SJohn Marino 	if (TEST_HARD_REG_BIT (all_btrs, regno)
626*e4b17023SJohn Marino 	    && TEST_HARD_REG_BIT (btrs_written[i], regno))
627*e4b17023SJohn Marino 	  sbitmap_a_or_b (bb_kill[i], bb_kill[i],
628*e4b17023SJohn Marino 			  btr_defset[regno - first_btr]);
629*e4b17023SJohn Marino     }
630*e4b17023SJohn Marino }
631*e4b17023SJohn Marino 
632*e4b17023SJohn Marino static void
compute_out(sbitmap * bb_out,sbitmap * bb_gen,sbitmap * bb_kill,int max_uid)633*e4b17023SJohn Marino compute_out (sbitmap *bb_out, sbitmap *bb_gen, sbitmap *bb_kill, int max_uid)
634*e4b17023SJohn Marino {
635*e4b17023SJohn Marino   /* Perform iterative dataflow:
636*e4b17023SJohn Marino       Initially, for all blocks, BB_OUT = BB_GEN.
637*e4b17023SJohn Marino       For each block,
638*e4b17023SJohn Marino 	BB_IN  = union over predecessors of BB_OUT(pred)
639*e4b17023SJohn Marino 	BB_OUT = (BB_IN - BB_KILL) + BB_GEN
640*e4b17023SJohn Marino      Iterate until the bb_out sets stop growing.  */
641*e4b17023SJohn Marino   int i;
642*e4b17023SJohn Marino   int changed;
643*e4b17023SJohn Marino   sbitmap bb_in = sbitmap_alloc (max_uid);
644*e4b17023SJohn Marino 
645*e4b17023SJohn Marino   for (i = NUM_FIXED_BLOCKS; i < last_basic_block; i++)
646*e4b17023SJohn Marino     sbitmap_copy (bb_out[i], bb_gen[i]);
647*e4b17023SJohn Marino 
648*e4b17023SJohn Marino   changed = 1;
649*e4b17023SJohn Marino   while (changed)
650*e4b17023SJohn Marino     {
651*e4b17023SJohn Marino       changed = 0;
652*e4b17023SJohn Marino       for (i = NUM_FIXED_BLOCKS; i < last_basic_block; i++)
653*e4b17023SJohn Marino 	{
654*e4b17023SJohn Marino 	  sbitmap_union_of_preds (bb_in, bb_out, i);
655*e4b17023SJohn Marino 	  changed |= sbitmap_union_of_diff_cg (bb_out[i], bb_gen[i],
656*e4b17023SJohn Marino 					       bb_in, bb_kill[i]);
657*e4b17023SJohn Marino 	}
658*e4b17023SJohn Marino     }
659*e4b17023SJohn Marino   sbitmap_free (bb_in);
660*e4b17023SJohn Marino }
661*e4b17023SJohn Marino 
662*e4b17023SJohn Marino static void
link_btr_uses(btr_def * def_array,btr_user * use_array,sbitmap * bb_out,sbitmap * btr_defset,int max_uid)663*e4b17023SJohn Marino link_btr_uses (btr_def *def_array, btr_user *use_array, sbitmap *bb_out,
664*e4b17023SJohn Marino 	       sbitmap *btr_defset, int max_uid)
665*e4b17023SJohn Marino {
666*e4b17023SJohn Marino   int i;
667*e4b17023SJohn Marino   sbitmap reaching_defs = sbitmap_alloc (max_uid);
668*e4b17023SJohn Marino 
669*e4b17023SJohn Marino   /* Link uses to the uses lists of all of their reaching defs.
670*e4b17023SJohn Marino      Count up the number of reaching defs of each use.  */
671*e4b17023SJohn Marino   for (i = NUM_FIXED_BLOCKS; i < last_basic_block; i++)
672*e4b17023SJohn Marino     {
673*e4b17023SJohn Marino       basic_block bb = BASIC_BLOCK (i);
674*e4b17023SJohn Marino       rtx insn;
675*e4b17023SJohn Marino       rtx last;
676*e4b17023SJohn Marino 
677*e4b17023SJohn Marino       sbitmap_union_of_preds (reaching_defs, bb_out, i);
678*e4b17023SJohn Marino       for (insn = BB_HEAD (bb), last = NEXT_INSN (BB_END (bb));
679*e4b17023SJohn Marino 	   insn != last;
680*e4b17023SJohn Marino 	   insn = NEXT_INSN (insn))
681*e4b17023SJohn Marino 	{
682*e4b17023SJohn Marino 	  if (INSN_P (insn))
683*e4b17023SJohn Marino 	    {
684*e4b17023SJohn Marino 	      int insn_uid = INSN_UID (insn);
685*e4b17023SJohn Marino 
686*e4b17023SJohn Marino 	      btr_def def   = def_array[insn_uid];
687*e4b17023SJohn Marino 	      btr_user user = use_array[insn_uid];
688*e4b17023SJohn Marino 	      if (def != NULL)
689*e4b17023SJohn Marino 		{
690*e4b17023SJohn Marino 		  /* Remove all reaching defs of regno except
691*e4b17023SJohn Marino 		     for this one.  */
692*e4b17023SJohn Marino 		  sbitmap_difference (reaching_defs, reaching_defs,
693*e4b17023SJohn Marino 				      btr_defset[def->btr - first_btr]);
694*e4b17023SJohn Marino 		  SET_BIT(reaching_defs, insn_uid);
695*e4b17023SJohn Marino 		}
696*e4b17023SJohn Marino 
697*e4b17023SJohn Marino 	      if (user != NULL)
698*e4b17023SJohn Marino 		{
699*e4b17023SJohn Marino 		  /* Find all the reaching defs for this use.  */
700*e4b17023SJohn Marino 		  sbitmap reaching_defs_of_reg = sbitmap_alloc(max_uid);
701*e4b17023SJohn Marino 		  unsigned int uid = 0;
702*e4b17023SJohn Marino 		  sbitmap_iterator sbi;
703*e4b17023SJohn Marino 
704*e4b17023SJohn Marino 		  if (user->use)
705*e4b17023SJohn Marino 		    sbitmap_a_and_b (
706*e4b17023SJohn Marino 		      reaching_defs_of_reg,
707*e4b17023SJohn Marino 		      reaching_defs,
708*e4b17023SJohn Marino 		      btr_defset[REGNO (user->use) - first_btr]);
709*e4b17023SJohn Marino 		  else
710*e4b17023SJohn Marino 		    {
711*e4b17023SJohn Marino 		      int reg;
712*e4b17023SJohn Marino 
713*e4b17023SJohn Marino 		      sbitmap_zero (reaching_defs_of_reg);
714*e4b17023SJohn Marino 		      for (reg = first_btr; reg <= last_btr; reg++)
715*e4b17023SJohn Marino 			if (TEST_HARD_REG_BIT (all_btrs, reg)
716*e4b17023SJohn Marino 			    && refers_to_regno_p (reg, reg + 1, user->insn,
717*e4b17023SJohn Marino 						  NULL))
718*e4b17023SJohn Marino 			  sbitmap_a_or_b_and_c (reaching_defs_of_reg,
719*e4b17023SJohn Marino 			    reaching_defs_of_reg,
720*e4b17023SJohn Marino 			    reaching_defs,
721*e4b17023SJohn Marino 			    btr_defset[reg - first_btr]);
722*e4b17023SJohn Marino 		    }
723*e4b17023SJohn Marino 		  EXECUTE_IF_SET_IN_SBITMAP (reaching_defs_of_reg, 0, uid, sbi)
724*e4b17023SJohn Marino 		    {
725*e4b17023SJohn Marino 		      btr_def def = def_array[uid];
726*e4b17023SJohn Marino 
727*e4b17023SJohn Marino 		      /* We now know that def reaches user.  */
728*e4b17023SJohn Marino 
729*e4b17023SJohn Marino 		      if (dump_file)
730*e4b17023SJohn Marino 			fprintf (dump_file,
731*e4b17023SJohn Marino 			  "Def in insn %d reaches use in insn %d\n",
732*e4b17023SJohn Marino 			  uid, insn_uid);
733*e4b17023SJohn Marino 
734*e4b17023SJohn Marino 		      user->n_reaching_defs++;
735*e4b17023SJohn Marino 		      if (!user->use)
736*e4b17023SJohn Marino 			def->has_ambiguous_use = 1;
737*e4b17023SJohn Marino 		      if (user->first_reaching_def != -1)
738*e4b17023SJohn Marino 			{ /* There is more than one reaching def.  This is
739*e4b17023SJohn Marino 			     a rare case, so just give up on this def/use
740*e4b17023SJohn Marino 			     web when it occurs.  */
741*e4b17023SJohn Marino 			  def->has_ambiguous_use = 1;
742*e4b17023SJohn Marino 			  def_array[user->first_reaching_def]
743*e4b17023SJohn Marino 			    ->has_ambiguous_use = 1;
744*e4b17023SJohn Marino 			  if (dump_file)
745*e4b17023SJohn Marino 			    fprintf (dump_file,
746*e4b17023SJohn Marino 				     "(use %d has multiple reaching defs)\n",
747*e4b17023SJohn Marino 				     insn_uid);
748*e4b17023SJohn Marino 			}
749*e4b17023SJohn Marino 		      else
750*e4b17023SJohn Marino 			user->first_reaching_def = uid;
751*e4b17023SJohn Marino 		      if (user->other_use_this_block)
752*e4b17023SJohn Marino 			def->other_btr_uses_after_use = 1;
753*e4b17023SJohn Marino 		      user->next = def->uses;
754*e4b17023SJohn Marino 		      def->uses = user;
755*e4b17023SJohn Marino 		    }
756*e4b17023SJohn Marino 		  sbitmap_free (reaching_defs_of_reg);
757*e4b17023SJohn Marino 		}
758*e4b17023SJohn Marino 
759*e4b17023SJohn Marino 	      if (CALL_P (insn))
760*e4b17023SJohn Marino 		{
761*e4b17023SJohn Marino 		  int regno;
762*e4b17023SJohn Marino 
763*e4b17023SJohn Marino 		  for (regno = first_btr; regno <= last_btr; regno++)
764*e4b17023SJohn Marino 		    if (TEST_HARD_REG_BIT (all_btrs, regno)
765*e4b17023SJohn Marino 			&& TEST_HARD_REG_BIT (call_used_reg_set, regno))
766*e4b17023SJohn Marino 		      sbitmap_difference (reaching_defs, reaching_defs,
767*e4b17023SJohn Marino 					  btr_defset[regno - first_btr]);
768*e4b17023SJohn Marino 		}
769*e4b17023SJohn Marino 	    }
770*e4b17023SJohn Marino 	}
771*e4b17023SJohn Marino     }
772*e4b17023SJohn Marino   sbitmap_free (reaching_defs);
773*e4b17023SJohn Marino }
774*e4b17023SJohn Marino 
775*e4b17023SJohn Marino static void
build_btr_def_use_webs(fibheap_t all_btr_defs)776*e4b17023SJohn Marino build_btr_def_use_webs (fibheap_t all_btr_defs)
777*e4b17023SJohn Marino {
778*e4b17023SJohn Marino   const int max_uid = get_max_uid ();
779*e4b17023SJohn Marino   btr_def  *def_array   = XCNEWVEC (btr_def, max_uid);
780*e4b17023SJohn Marino   btr_user *use_array   = XCNEWVEC (btr_user, max_uid);
781*e4b17023SJohn Marino   sbitmap *btr_defset   = sbitmap_vector_alloc (
782*e4b17023SJohn Marino 			   (last_btr - first_btr) + 1, max_uid);
783*e4b17023SJohn Marino   sbitmap *bb_gen      = sbitmap_vector_alloc (last_basic_block, max_uid);
784*e4b17023SJohn Marino   HARD_REG_SET *btrs_written = XCNEWVEC (HARD_REG_SET, last_basic_block);
785*e4b17023SJohn Marino   sbitmap *bb_kill;
786*e4b17023SJohn Marino   sbitmap *bb_out;
787*e4b17023SJohn Marino 
788*e4b17023SJohn Marino   sbitmap_vector_zero (btr_defset, (last_btr - first_btr) + 1);
789*e4b17023SJohn Marino 
790*e4b17023SJohn Marino   compute_defs_uses_and_gen (all_btr_defs, def_array, use_array, btr_defset,
791*e4b17023SJohn Marino 			     bb_gen, btrs_written);
792*e4b17023SJohn Marino 
793*e4b17023SJohn Marino   bb_kill = sbitmap_vector_alloc (last_basic_block, max_uid);
794*e4b17023SJohn Marino   compute_kill (bb_kill, btr_defset, btrs_written);
795*e4b17023SJohn Marino   free (btrs_written);
796*e4b17023SJohn Marino 
797*e4b17023SJohn Marino   bb_out = sbitmap_vector_alloc (last_basic_block, max_uid);
798*e4b17023SJohn Marino   compute_out (bb_out, bb_gen, bb_kill, max_uid);
799*e4b17023SJohn Marino 
800*e4b17023SJohn Marino   sbitmap_vector_free (bb_gen);
801*e4b17023SJohn Marino   sbitmap_vector_free (bb_kill);
802*e4b17023SJohn Marino 
803*e4b17023SJohn Marino   link_btr_uses (def_array, use_array, bb_out, btr_defset, max_uid);
804*e4b17023SJohn Marino 
805*e4b17023SJohn Marino   sbitmap_vector_free (bb_out);
806*e4b17023SJohn Marino   sbitmap_vector_free (btr_defset);
807*e4b17023SJohn Marino   free (use_array);
808*e4b17023SJohn Marino   free (def_array);
809*e4b17023SJohn Marino }
810*e4b17023SJohn Marino 
811*e4b17023SJohn Marino /* Return true if basic block BB contains the start or end of the
812*e4b17023SJohn Marino    live range of the definition DEF, AND there are other live
813*e4b17023SJohn Marino    ranges of the same target register that include BB.  */
814*e4b17023SJohn Marino static int
block_at_edge_of_live_range_p(int bb,btr_def def)815*e4b17023SJohn Marino block_at_edge_of_live_range_p (int bb, btr_def def)
816*e4b17023SJohn Marino {
817*e4b17023SJohn Marino   if (def->other_btr_uses_before_def && BASIC_BLOCK (bb) == def->bb)
818*e4b17023SJohn Marino     return 1;
819*e4b17023SJohn Marino   else if (def->other_btr_uses_after_use)
820*e4b17023SJohn Marino     {
821*e4b17023SJohn Marino       btr_user user;
822*e4b17023SJohn Marino       for (user = def->uses; user != NULL; user = user->next)
823*e4b17023SJohn Marino 	if (BASIC_BLOCK (bb) == user->bb)
824*e4b17023SJohn Marino 	  return 1;
825*e4b17023SJohn Marino     }
826*e4b17023SJohn Marino   return 0;
827*e4b17023SJohn Marino }
828*e4b17023SJohn Marino 
829*e4b17023SJohn Marino /* We are removing the def/use web DEF.  The target register
830*e4b17023SJohn Marino    used in this web is therefore no longer live in the live range
831*e4b17023SJohn Marino    of this web, so remove it from the live set of all basic blocks
832*e4b17023SJohn Marino    in the live range of the web.
833*e4b17023SJohn Marino    Blocks at the boundary of the live range may contain other live
834*e4b17023SJohn Marino    ranges for the same target register, so we have to be careful
835*e4b17023SJohn Marino    to remove the target register from the live set of these blocks
836*e4b17023SJohn Marino    only if they do not contain other live ranges for the same register.  */
837*e4b17023SJohn Marino static void
clear_btr_from_live_range(btr_def def)838*e4b17023SJohn Marino clear_btr_from_live_range (btr_def def)
839*e4b17023SJohn Marino {
840*e4b17023SJohn Marino   unsigned bb;
841*e4b17023SJohn Marino   bitmap_iterator bi;
842*e4b17023SJohn Marino 
843*e4b17023SJohn Marino   EXECUTE_IF_SET_IN_BITMAP (def->live_range, 0, bb, bi)
844*e4b17023SJohn Marino     {
845*e4b17023SJohn Marino       if ((!def->other_btr_uses_before_def
846*e4b17023SJohn Marino 	   && !def->other_btr_uses_after_use)
847*e4b17023SJohn Marino 	  || !block_at_edge_of_live_range_p (bb, def))
848*e4b17023SJohn Marino 	{
849*e4b17023SJohn Marino 	  CLEAR_HARD_REG_BIT (btrs_live[bb], def->btr);
850*e4b17023SJohn Marino 	  CLEAR_HARD_REG_BIT (btrs_live_at_end[bb], def->btr);
851*e4b17023SJohn Marino 	  if (dump_file)
852*e4b17023SJohn Marino 	    dump_btrs_live (bb);
853*e4b17023SJohn Marino 	}
854*e4b17023SJohn Marino     }
855*e4b17023SJohn Marino  if (def->own_end)
856*e4b17023SJohn Marino    CLEAR_HARD_REG_BIT (btrs_live_at_end[def->bb->index], def->btr);
857*e4b17023SJohn Marino }
858*e4b17023SJohn Marino 
859*e4b17023SJohn Marino 
860*e4b17023SJohn Marino /* We are adding the def/use web DEF.  Add the target register used
861*e4b17023SJohn Marino    in this web to the live set of all of the basic blocks that contain
862*e4b17023SJohn Marino    the live range of the web.
863*e4b17023SJohn Marino    If OWN_END is set, also show that the register is live from our
864*e4b17023SJohn Marino    definitions at the end of the basic block where it is defined.  */
865*e4b17023SJohn Marino static void
add_btr_to_live_range(btr_def def,int own_end)866*e4b17023SJohn Marino add_btr_to_live_range (btr_def def, int own_end)
867*e4b17023SJohn Marino {
868*e4b17023SJohn Marino   unsigned bb;
869*e4b17023SJohn Marino   bitmap_iterator bi;
870*e4b17023SJohn Marino 
871*e4b17023SJohn Marino   EXECUTE_IF_SET_IN_BITMAP (def->live_range, 0, bb, bi)
872*e4b17023SJohn Marino     {
873*e4b17023SJohn Marino       SET_HARD_REG_BIT (btrs_live[bb], def->btr);
874*e4b17023SJohn Marino       SET_HARD_REG_BIT (btrs_live_at_end[bb], def->btr);
875*e4b17023SJohn Marino       if (dump_file)
876*e4b17023SJohn Marino 	dump_btrs_live (bb);
877*e4b17023SJohn Marino     }
878*e4b17023SJohn Marino   if (own_end)
879*e4b17023SJohn Marino     {
880*e4b17023SJohn Marino       SET_HARD_REG_BIT (btrs_live_at_end[def->bb->index], def->btr);
881*e4b17023SJohn Marino       def->own_end = 1;
882*e4b17023SJohn Marino     }
883*e4b17023SJohn Marino }
884*e4b17023SJohn Marino 
885*e4b17023SJohn Marino /* Update a live range to contain the basic block NEW_BLOCK, and all
886*e4b17023SJohn Marino    blocks on paths between the existing live range and NEW_BLOCK.
887*e4b17023SJohn Marino    HEAD is a block contained in the existing live range that dominates
888*e4b17023SJohn Marino    all other blocks in the existing live range.
889*e4b17023SJohn Marino    Also add to the set BTRS_LIVE_IN_RANGE all target registers that
890*e4b17023SJohn Marino    are live in the blocks that we add to the live range.
891*e4b17023SJohn Marino    If FULL_RANGE is set, include the full live range of NEW_BB;
892*e4b17023SJohn Marino    otherwise, if NEW_BB dominates HEAD_BB, only add registers that
893*e4b17023SJohn Marino    are life at the end of NEW_BB for NEW_BB itself.
894*e4b17023SJohn Marino    It is a precondition that either NEW_BLOCK dominates HEAD,or
895*e4b17023SJohn Marino    HEAD dom NEW_BLOCK.  This is used to speed up the
896*e4b17023SJohn Marino    implementation of this function.  */
897*e4b17023SJohn Marino static void
augment_live_range(bitmap live_range,HARD_REG_SET * btrs_live_in_range,basic_block head_bb,basic_block new_bb,int full_range)898*e4b17023SJohn Marino augment_live_range (bitmap live_range, HARD_REG_SET *btrs_live_in_range,
899*e4b17023SJohn Marino 		    basic_block head_bb, basic_block new_bb, int full_range)
900*e4b17023SJohn Marino {
901*e4b17023SJohn Marino   basic_block *worklist, *tos;
902*e4b17023SJohn Marino 
903*e4b17023SJohn Marino   tos = worklist = XNEWVEC (basic_block, n_basic_blocks + 1);
904*e4b17023SJohn Marino 
905*e4b17023SJohn Marino   if (dominated_by_p (CDI_DOMINATORS, new_bb, head_bb))
906*e4b17023SJohn Marino     {
907*e4b17023SJohn Marino       if (new_bb == head_bb)
908*e4b17023SJohn Marino 	{
909*e4b17023SJohn Marino 	  if (full_range)
910*e4b17023SJohn Marino 	    IOR_HARD_REG_SET (*btrs_live_in_range, btrs_live[new_bb->index]);
911*e4b17023SJohn Marino 	  free (tos);
912*e4b17023SJohn Marino 	  return;
913*e4b17023SJohn Marino 	}
914*e4b17023SJohn Marino       *tos++ = new_bb;
915*e4b17023SJohn Marino     }
916*e4b17023SJohn Marino   else
917*e4b17023SJohn Marino     {
918*e4b17023SJohn Marino       edge e;
919*e4b17023SJohn Marino       edge_iterator ei;
920*e4b17023SJohn Marino       int new_block = new_bb->index;
921*e4b17023SJohn Marino 
922*e4b17023SJohn Marino       gcc_assert (dominated_by_p (CDI_DOMINATORS, head_bb, new_bb));
923*e4b17023SJohn Marino 
924*e4b17023SJohn Marino       IOR_HARD_REG_SET (*btrs_live_in_range, btrs_live[head_bb->index]);
925*e4b17023SJohn Marino       bitmap_set_bit (live_range, new_block);
926*e4b17023SJohn Marino       /* A previous btr migration could have caused a register to be
927*e4b17023SJohn Marino 	live just at the end of new_block which we need in full, so
928*e4b17023SJohn Marino 	use trs_live_at_end even if full_range is set.  */
929*e4b17023SJohn Marino       IOR_HARD_REG_SET (*btrs_live_in_range, btrs_live_at_end[new_block]);
930*e4b17023SJohn Marino       if (full_range)
931*e4b17023SJohn Marino 	IOR_HARD_REG_SET (*btrs_live_in_range, btrs_live[new_block]);
932*e4b17023SJohn Marino       if (dump_file)
933*e4b17023SJohn Marino 	{
934*e4b17023SJohn Marino 	  fprintf (dump_file,
935*e4b17023SJohn Marino 		   "Adding end of block %d and rest of %d to live range\n",
936*e4b17023SJohn Marino 		   new_block, head_bb->index);
937*e4b17023SJohn Marino 	  fprintf (dump_file,"Now live btrs are ");
938*e4b17023SJohn Marino 	  dump_hard_reg_set (*btrs_live_in_range);
939*e4b17023SJohn Marino 	  fprintf (dump_file, "\n");
940*e4b17023SJohn Marino 	}
941*e4b17023SJohn Marino       FOR_EACH_EDGE (e, ei, head_bb->preds)
942*e4b17023SJohn Marino 	*tos++ = e->src;
943*e4b17023SJohn Marino     }
944*e4b17023SJohn Marino 
945*e4b17023SJohn Marino   while (tos != worklist)
946*e4b17023SJohn Marino     {
947*e4b17023SJohn Marino       basic_block bb = *--tos;
948*e4b17023SJohn Marino       if (!bitmap_bit_p (live_range, bb->index))
949*e4b17023SJohn Marino 	{
950*e4b17023SJohn Marino 	  edge e;
951*e4b17023SJohn Marino 	  edge_iterator ei;
952*e4b17023SJohn Marino 
953*e4b17023SJohn Marino 	  bitmap_set_bit (live_range, bb->index);
954*e4b17023SJohn Marino 	  IOR_HARD_REG_SET (*btrs_live_in_range,
955*e4b17023SJohn Marino 	    btrs_live[bb->index]);
956*e4b17023SJohn Marino 	  /* A previous btr migration could have caused a register to be
957*e4b17023SJohn Marino 	     live just at the end of a block which we need in full.  */
958*e4b17023SJohn Marino 	  IOR_HARD_REG_SET (*btrs_live_in_range,
959*e4b17023SJohn Marino 	    btrs_live_at_end[bb->index]);
960*e4b17023SJohn Marino 	  if (dump_file)
961*e4b17023SJohn Marino 	    {
962*e4b17023SJohn Marino 	      fprintf (dump_file,
963*e4b17023SJohn Marino 		"Adding block %d to live range\n", bb->index);
964*e4b17023SJohn Marino 	      fprintf (dump_file,"Now live btrs are ");
965*e4b17023SJohn Marino 	      dump_hard_reg_set (*btrs_live_in_range);
966*e4b17023SJohn Marino 	      fprintf (dump_file, "\n");
967*e4b17023SJohn Marino 	    }
968*e4b17023SJohn Marino 
969*e4b17023SJohn Marino 	  FOR_EACH_EDGE (e, ei, bb->preds)
970*e4b17023SJohn Marino 	    {
971*e4b17023SJohn Marino 	      basic_block pred = e->src;
972*e4b17023SJohn Marino 	      if (!bitmap_bit_p (live_range, pred->index))
973*e4b17023SJohn Marino 		*tos++ = pred;
974*e4b17023SJohn Marino 	    }
975*e4b17023SJohn Marino 	}
976*e4b17023SJohn Marino     }
977*e4b17023SJohn Marino 
978*e4b17023SJohn Marino   free (worklist);
979*e4b17023SJohn Marino }
980*e4b17023SJohn Marino 
981*e4b17023SJohn Marino /*  Return the most desirable target register that is not in
982*e4b17023SJohn Marino     the set USED_BTRS.  */
983*e4b17023SJohn Marino static int
choose_btr(HARD_REG_SET used_btrs)984*e4b17023SJohn Marino choose_btr (HARD_REG_SET used_btrs)
985*e4b17023SJohn Marino {
986*e4b17023SJohn Marino   int i;
987*e4b17023SJohn Marino 
988*e4b17023SJohn Marino   if (!hard_reg_set_subset_p (all_btrs, used_btrs))
989*e4b17023SJohn Marino     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
990*e4b17023SJohn Marino       {
991*e4b17023SJohn Marino #ifdef REG_ALLOC_ORDER
992*e4b17023SJohn Marino 	int regno = reg_alloc_order[i];
993*e4b17023SJohn Marino #else
994*e4b17023SJohn Marino 	int regno = i;
995*e4b17023SJohn Marino #endif
996*e4b17023SJohn Marino 	if (TEST_HARD_REG_BIT (all_btrs, regno)
997*e4b17023SJohn Marino 	    && !TEST_HARD_REG_BIT (used_btrs, regno))
998*e4b17023SJohn Marino 	  return regno;
999*e4b17023SJohn Marino       }
1000*e4b17023SJohn Marino   return -1;
1001*e4b17023SJohn Marino }
1002*e4b17023SJohn Marino 
1003*e4b17023SJohn Marino /* Calculate the set of basic blocks that contain the live range of
1004*e4b17023SJohn Marino    the def/use web DEF.
1005*e4b17023SJohn Marino    Also calculate the set of target registers that are live at time
1006*e4b17023SJohn Marino    in this live range, but ignore the live range represented by DEF
1007*e4b17023SJohn Marino    when calculating this set.  */
1008*e4b17023SJohn Marino static void
btr_def_live_range(btr_def def,HARD_REG_SET * btrs_live_in_range)1009*e4b17023SJohn Marino btr_def_live_range (btr_def def, HARD_REG_SET *btrs_live_in_range)
1010*e4b17023SJohn Marino {
1011*e4b17023SJohn Marino   if (!def->live_range)
1012*e4b17023SJohn Marino     {
1013*e4b17023SJohn Marino       btr_user user;
1014*e4b17023SJohn Marino 
1015*e4b17023SJohn Marino       def->live_range = BITMAP_ALLOC (NULL);
1016*e4b17023SJohn Marino 
1017*e4b17023SJohn Marino       bitmap_set_bit (def->live_range, def->bb->index);
1018*e4b17023SJohn Marino       COPY_HARD_REG_SET (*btrs_live_in_range,
1019*e4b17023SJohn Marino 			 (flag_btr_bb_exclusive
1020*e4b17023SJohn Marino 			  ? btrs_live : btrs_live_at_end)[def->bb->index]);
1021*e4b17023SJohn Marino 
1022*e4b17023SJohn Marino       for (user = def->uses; user != NULL; user = user->next)
1023*e4b17023SJohn Marino 	augment_live_range (def->live_range, btrs_live_in_range,
1024*e4b17023SJohn Marino 			    def->bb, user->bb,
1025*e4b17023SJohn Marino 			    (flag_btr_bb_exclusive
1026*e4b17023SJohn Marino 			     || user->insn != BB_END (def->bb)
1027*e4b17023SJohn Marino 			     || !JUMP_P (user->insn)));
1028*e4b17023SJohn Marino     }
1029*e4b17023SJohn Marino   else
1030*e4b17023SJohn Marino     {
1031*e4b17023SJohn Marino       /* def->live_range is accurate, but we need to recompute
1032*e4b17023SJohn Marino 	 the set of target registers live over it, because migration
1033*e4b17023SJohn Marino 	 of other PT instructions may have affected it.
1034*e4b17023SJohn Marino       */
1035*e4b17023SJohn Marino       unsigned bb;
1036*e4b17023SJohn Marino       unsigned def_bb = flag_btr_bb_exclusive ? -1 : def->bb->index;
1037*e4b17023SJohn Marino       bitmap_iterator bi;
1038*e4b17023SJohn Marino 
1039*e4b17023SJohn Marino       CLEAR_HARD_REG_SET (*btrs_live_in_range);
1040*e4b17023SJohn Marino       EXECUTE_IF_SET_IN_BITMAP (def->live_range, 0, bb, bi)
1041*e4b17023SJohn Marino 	{
1042*e4b17023SJohn Marino 	  IOR_HARD_REG_SET (*btrs_live_in_range,
1043*e4b17023SJohn Marino 			    (def_bb == bb
1044*e4b17023SJohn Marino 			     ? btrs_live_at_end : btrs_live) [bb]);
1045*e4b17023SJohn Marino 	}
1046*e4b17023SJohn Marino     }
1047*e4b17023SJohn Marino   if (!def->other_btr_uses_before_def &&
1048*e4b17023SJohn Marino       !def->other_btr_uses_after_use)
1049*e4b17023SJohn Marino     CLEAR_HARD_REG_BIT (*btrs_live_in_range, def->btr);
1050*e4b17023SJohn Marino }
1051*e4b17023SJohn Marino 
1052*e4b17023SJohn Marino /* Merge into the def/use web DEF any other def/use webs in the same
1053*e4b17023SJohn Marino    group that are dominated by DEF, provided that there is a target
1054*e4b17023SJohn Marino    register available to allocate to the merged web.  */
1055*e4b17023SJohn Marino static void
combine_btr_defs(btr_def def,HARD_REG_SET * btrs_live_in_range)1056*e4b17023SJohn Marino combine_btr_defs (btr_def def, HARD_REG_SET *btrs_live_in_range)
1057*e4b17023SJohn Marino {
1058*e4b17023SJohn Marino   btr_def other_def;
1059*e4b17023SJohn Marino 
1060*e4b17023SJohn Marino   for (other_def = def->group->members;
1061*e4b17023SJohn Marino        other_def != NULL;
1062*e4b17023SJohn Marino        other_def = other_def->next_this_group)
1063*e4b17023SJohn Marino     {
1064*e4b17023SJohn Marino       if (other_def != def
1065*e4b17023SJohn Marino 	  && other_def->uses != NULL
1066*e4b17023SJohn Marino 	  && ! other_def->has_ambiguous_use
1067*e4b17023SJohn Marino 	  && dominated_by_p (CDI_DOMINATORS, other_def->bb, def->bb))
1068*e4b17023SJohn Marino 	{
1069*e4b17023SJohn Marino 	  /* def->bb dominates the other def, so def and other_def could
1070*e4b17023SJohn Marino 	     be combined.  */
1071*e4b17023SJohn Marino 	  /* Merge their live ranges, and get the set of
1072*e4b17023SJohn Marino 	     target registers live over the merged range.  */
1073*e4b17023SJohn Marino 	  int btr;
1074*e4b17023SJohn Marino 	  HARD_REG_SET combined_btrs_live;
1075*e4b17023SJohn Marino 	  bitmap combined_live_range = BITMAP_ALLOC (NULL);
1076*e4b17023SJohn Marino 	  btr_user user;
1077*e4b17023SJohn Marino 
1078*e4b17023SJohn Marino 	  if (other_def->live_range == NULL)
1079*e4b17023SJohn Marino 	    {
1080*e4b17023SJohn Marino 	      HARD_REG_SET dummy_btrs_live_in_range;
1081*e4b17023SJohn Marino 	      btr_def_live_range (other_def, &dummy_btrs_live_in_range);
1082*e4b17023SJohn Marino 	    }
1083*e4b17023SJohn Marino 	  COPY_HARD_REG_SET (combined_btrs_live, *btrs_live_in_range);
1084*e4b17023SJohn Marino 	  bitmap_copy (combined_live_range, def->live_range);
1085*e4b17023SJohn Marino 
1086*e4b17023SJohn Marino 	  for (user = other_def->uses; user != NULL; user = user->next)
1087*e4b17023SJohn Marino 	    augment_live_range (combined_live_range, &combined_btrs_live,
1088*e4b17023SJohn Marino 				def->bb, user->bb,
1089*e4b17023SJohn Marino 				(flag_btr_bb_exclusive
1090*e4b17023SJohn Marino 				 || user->insn != BB_END (def->bb)
1091*e4b17023SJohn Marino 				 || !JUMP_P (user->insn)));
1092*e4b17023SJohn Marino 
1093*e4b17023SJohn Marino 	  btr = choose_btr (combined_btrs_live);
1094*e4b17023SJohn Marino 	  if (btr != -1)
1095*e4b17023SJohn Marino 	    {
1096*e4b17023SJohn Marino 	      /* We can combine them.  */
1097*e4b17023SJohn Marino 	      if (dump_file)
1098*e4b17023SJohn Marino 		fprintf (dump_file,
1099*e4b17023SJohn Marino 			 "Combining def in insn %d with def in insn %d\n",
1100*e4b17023SJohn Marino 			 INSN_UID (other_def->insn), INSN_UID (def->insn));
1101*e4b17023SJohn Marino 
1102*e4b17023SJohn Marino 	      def->btr = btr;
1103*e4b17023SJohn Marino 	      user = other_def->uses;
1104*e4b17023SJohn Marino 	      while (user != NULL)
1105*e4b17023SJohn Marino 		{
1106*e4b17023SJohn Marino 		  btr_user next = user->next;
1107*e4b17023SJohn Marino 
1108*e4b17023SJohn Marino 		  user->next = def->uses;
1109*e4b17023SJohn Marino 		  def->uses = user;
1110*e4b17023SJohn Marino 		  user = next;
1111*e4b17023SJohn Marino 		}
1112*e4b17023SJohn Marino 	      /* Combining def/use webs can make target registers live
1113*e4b17023SJohn Marino 		 after uses where they previously were not.  This means
1114*e4b17023SJohn Marino 		 some REG_DEAD notes may no longer be correct.  We could
1115*e4b17023SJohn Marino 		 be more precise about this if we looked at the combined
1116*e4b17023SJohn Marino 		 live range, but here I just delete any REG_DEAD notes
1117*e4b17023SJohn Marino 		 in case they are no longer correct.  */
1118*e4b17023SJohn Marino 	      for (user = def->uses; user != NULL; user = user->next)
1119*e4b17023SJohn Marino 		remove_note (user->insn,
1120*e4b17023SJohn Marino 			     find_regno_note (user->insn, REG_DEAD,
1121*e4b17023SJohn Marino 					      REGNO (user->use)));
1122*e4b17023SJohn Marino 	      clear_btr_from_live_range (other_def);
1123*e4b17023SJohn Marino 	      other_def->uses = NULL;
1124*e4b17023SJohn Marino 	      bitmap_copy (def->live_range, combined_live_range);
1125*e4b17023SJohn Marino 	      if (other_def->btr == btr && other_def->other_btr_uses_after_use)
1126*e4b17023SJohn Marino 		def->other_btr_uses_after_use = 1;
1127*e4b17023SJohn Marino 	      COPY_HARD_REG_SET (*btrs_live_in_range, combined_btrs_live);
1128*e4b17023SJohn Marino 
1129*e4b17023SJohn Marino 	      /* Delete the old target register initialization.  */
1130*e4b17023SJohn Marino 	      delete_insn (other_def->insn);
1131*e4b17023SJohn Marino 
1132*e4b17023SJohn Marino 	    }
1133*e4b17023SJohn Marino 	  BITMAP_FREE (combined_live_range);
1134*e4b17023SJohn Marino 	}
1135*e4b17023SJohn Marino     }
1136*e4b17023SJohn Marino }
1137*e4b17023SJohn Marino 
1138*e4b17023SJohn Marino /* Move the definition DEF from its current position to basic
1139*e4b17023SJohn Marino    block NEW_DEF_BB, and modify it to use branch target register BTR.
1140*e4b17023SJohn Marino    Delete the old defining insn, and insert a new one in NEW_DEF_BB.
1141*e4b17023SJohn Marino    Update all reaching uses of DEF in the RTL to use BTR.
1142*e4b17023SJohn Marino    If this new position means that other defs in the
1143*e4b17023SJohn Marino    same group can be combined with DEF then combine them.  */
1144*e4b17023SJohn Marino static void
move_btr_def(basic_block new_def_bb,int btr,btr_def def,bitmap live_range,HARD_REG_SET * btrs_live_in_range)1145*e4b17023SJohn Marino move_btr_def (basic_block new_def_bb, int btr, btr_def def, bitmap live_range,
1146*e4b17023SJohn Marino 	     HARD_REG_SET *btrs_live_in_range)
1147*e4b17023SJohn Marino {
1148*e4b17023SJohn Marino   /* We can move the instruction.
1149*e4b17023SJohn Marino      Set a target register in block NEW_DEF_BB to the value
1150*e4b17023SJohn Marino      needed for this target register definition.
1151*e4b17023SJohn Marino      Replace all uses of the old target register definition by
1152*e4b17023SJohn Marino      uses of the new definition.  Delete the old definition.  */
1153*e4b17023SJohn Marino   basic_block b = new_def_bb;
1154*e4b17023SJohn Marino   rtx insp = BB_HEAD (b);
1155*e4b17023SJohn Marino   rtx old_insn = def->insn;
1156*e4b17023SJohn Marino   rtx src;
1157*e4b17023SJohn Marino   rtx btr_rtx;
1158*e4b17023SJohn Marino   rtx new_insn;
1159*e4b17023SJohn Marino   enum machine_mode btr_mode;
1160*e4b17023SJohn Marino   btr_user user;
1161*e4b17023SJohn Marino   rtx set;
1162*e4b17023SJohn Marino 
1163*e4b17023SJohn Marino   if (dump_file)
1164*e4b17023SJohn Marino     fprintf(dump_file, "migrating to basic block %d, using reg %d\n",
1165*e4b17023SJohn Marino 	    new_def_bb->index, btr);
1166*e4b17023SJohn Marino 
1167*e4b17023SJohn Marino   clear_btr_from_live_range (def);
1168*e4b17023SJohn Marino   def->btr = btr;
1169*e4b17023SJohn Marino   def->bb = new_def_bb;
1170*e4b17023SJohn Marino   def->luid = 0;
1171*e4b17023SJohn Marino   def->cost = basic_block_freq (new_def_bb);
1172*e4b17023SJohn Marino   bitmap_copy (def->live_range, live_range);
1173*e4b17023SJohn Marino   combine_btr_defs (def, btrs_live_in_range);
1174*e4b17023SJohn Marino   btr = def->btr;
1175*e4b17023SJohn Marino   def->other_btr_uses_before_def
1176*e4b17023SJohn Marino     = TEST_HARD_REG_BIT (btrs_live[b->index], btr) ? 1 : 0;
1177*e4b17023SJohn Marino   add_btr_to_live_range (def, 1);
1178*e4b17023SJohn Marino   if (LABEL_P (insp))
1179*e4b17023SJohn Marino     insp = NEXT_INSN (insp);
1180*e4b17023SJohn Marino   /* N.B.: insp is expected to be NOTE_INSN_BASIC_BLOCK now.  Some
1181*e4b17023SJohn Marino      optimizations can result in insp being both first and last insn of
1182*e4b17023SJohn Marino      its basic block.  */
1183*e4b17023SJohn Marino   /* ?? some assertions to check that insp is sensible? */
1184*e4b17023SJohn Marino 
1185*e4b17023SJohn Marino   if (def->other_btr_uses_before_def)
1186*e4b17023SJohn Marino     {
1187*e4b17023SJohn Marino       insp = BB_END (b);
1188*e4b17023SJohn Marino       for (insp = BB_END (b); ! INSN_P (insp); insp = PREV_INSN (insp))
1189*e4b17023SJohn Marino 	gcc_assert (insp != BB_HEAD (b));
1190*e4b17023SJohn Marino 
1191*e4b17023SJohn Marino       if (JUMP_P (insp) || can_throw_internal (insp))
1192*e4b17023SJohn Marino 	insp = PREV_INSN (insp);
1193*e4b17023SJohn Marino     }
1194*e4b17023SJohn Marino 
1195*e4b17023SJohn Marino   set = single_set (old_insn);
1196*e4b17023SJohn Marino   src = SET_SRC (set);
1197*e4b17023SJohn Marino   btr_mode = GET_MODE (SET_DEST (set));
1198*e4b17023SJohn Marino   btr_rtx = gen_rtx_REG (btr_mode, btr);
1199*e4b17023SJohn Marino 
1200*e4b17023SJohn Marino   new_insn = gen_move_insn (btr_rtx, src);
1201*e4b17023SJohn Marino 
1202*e4b17023SJohn Marino   /* Insert target register initialization at head of basic block.  */
1203*e4b17023SJohn Marino   def->insn = emit_insn_after (new_insn, insp);
1204*e4b17023SJohn Marino 
1205*e4b17023SJohn Marino   df_set_regs_ever_live (btr, true);
1206*e4b17023SJohn Marino 
1207*e4b17023SJohn Marino   if (dump_file)
1208*e4b17023SJohn Marino     fprintf (dump_file, "New pt is insn %d, inserted after insn %d\n",
1209*e4b17023SJohn Marino 	     INSN_UID (def->insn), INSN_UID (insp));
1210*e4b17023SJohn Marino 
1211*e4b17023SJohn Marino   /* Delete the old target register initialization.  */
1212*e4b17023SJohn Marino   delete_insn (old_insn);
1213*e4b17023SJohn Marino 
1214*e4b17023SJohn Marino   /* Replace each use of the old target register by a use of the new target
1215*e4b17023SJohn Marino      register.  */
1216*e4b17023SJohn Marino   for (user = def->uses; user != NULL; user = user->next)
1217*e4b17023SJohn Marino     {
1218*e4b17023SJohn Marino       /* Some extra work here to ensure consistent modes, because
1219*e4b17023SJohn Marino 	 it seems that a target register REG rtx can be given a different
1220*e4b17023SJohn Marino 	 mode depending on the context (surely that should not be
1221*e4b17023SJohn Marino 	 the case?).  */
1222*e4b17023SJohn Marino       rtx replacement_rtx;
1223*e4b17023SJohn Marino       if (GET_MODE (user->use) == GET_MODE (btr_rtx)
1224*e4b17023SJohn Marino 	  || GET_MODE (user->use) == VOIDmode)
1225*e4b17023SJohn Marino 	replacement_rtx = btr_rtx;
1226*e4b17023SJohn Marino       else
1227*e4b17023SJohn Marino 	replacement_rtx = gen_rtx_REG (GET_MODE (user->use), btr);
1228*e4b17023SJohn Marino       validate_replace_rtx (user->use, replacement_rtx, user->insn);
1229*e4b17023SJohn Marino       user->use = replacement_rtx;
1230*e4b17023SJohn Marino     }
1231*e4b17023SJohn Marino }
1232*e4b17023SJohn Marino 
1233*e4b17023SJohn Marino /* We anticipate intra-block scheduling to be done.  See if INSN could move
1234*e4b17023SJohn Marino    up within BB by N_INSNS.  */
1235*e4b17023SJohn Marino static int
can_move_up(const_basic_block bb,const_rtx insn,int n_insns)1236*e4b17023SJohn Marino can_move_up (const_basic_block bb, const_rtx insn, int n_insns)
1237*e4b17023SJohn Marino {
1238*e4b17023SJohn Marino   while (insn != BB_HEAD (bb) && n_insns > 0)
1239*e4b17023SJohn Marino     {
1240*e4b17023SJohn Marino       insn = PREV_INSN (insn);
1241*e4b17023SJohn Marino       /* ??? What if we have an anti-dependency that actually prevents the
1242*e4b17023SJohn Marino 	 scheduler from doing the move?  We'd like to re-allocate the register,
1243*e4b17023SJohn Marino 	 but not necessarily put the load into another basic block.  */
1244*e4b17023SJohn Marino       if (INSN_P (insn))
1245*e4b17023SJohn Marino 	n_insns--;
1246*e4b17023SJohn Marino     }
1247*e4b17023SJohn Marino   return n_insns <= 0;
1248*e4b17023SJohn Marino }
1249*e4b17023SJohn Marino 
1250*e4b17023SJohn Marino /* Attempt to migrate the target register definition DEF to an
1251*e4b17023SJohn Marino    earlier point in the flowgraph.
1252*e4b17023SJohn Marino 
1253*e4b17023SJohn Marino    It is a precondition of this function that DEF is migratable:
1254*e4b17023SJohn Marino    i.e. it has a constant source, and all uses are unambiguous.
1255*e4b17023SJohn Marino 
1256*e4b17023SJohn Marino    Only migrations that reduce the cost of DEF will be made.
1257*e4b17023SJohn Marino    MIN_COST is the lower bound on the cost of the DEF after migration.
1258*e4b17023SJohn Marino    If we migrate DEF so that its cost falls below MIN_COST,
1259*e4b17023SJohn Marino    then we do not attempt to migrate further.  The idea is that
1260*e4b17023SJohn Marino    we migrate definitions in a priority order based on their cost,
1261*e4b17023SJohn Marino    when the cost of this definition falls below MIN_COST, then
1262*e4b17023SJohn Marino    there is another definition with cost == MIN_COST which now
1263*e4b17023SJohn Marino    has a higher priority than this definition.
1264*e4b17023SJohn Marino 
1265*e4b17023SJohn Marino    Return nonzero if there may be benefit from attempting to
1266*e4b17023SJohn Marino    migrate this DEF further (i.e. we have reduced the cost below
1267*e4b17023SJohn Marino    MIN_COST, but we may be able to reduce it further).
1268*e4b17023SJohn Marino    Return zero if no further migration is possible.  */
1269*e4b17023SJohn Marino static int
migrate_btr_def(btr_def def,int min_cost)1270*e4b17023SJohn Marino migrate_btr_def (btr_def def, int min_cost)
1271*e4b17023SJohn Marino {
1272*e4b17023SJohn Marino   bitmap live_range;
1273*e4b17023SJohn Marino   HARD_REG_SET btrs_live_in_range;
1274*e4b17023SJohn Marino   int btr_used_near_def = 0;
1275*e4b17023SJohn Marino   int def_basic_block_freq;
1276*e4b17023SJohn Marino   basic_block attempt;
1277*e4b17023SJohn Marino   int give_up = 0;
1278*e4b17023SJohn Marino   int def_moved = 0;
1279*e4b17023SJohn Marino   btr_user user;
1280*e4b17023SJohn Marino   int def_latency;
1281*e4b17023SJohn Marino 
1282*e4b17023SJohn Marino   if (dump_file)
1283*e4b17023SJohn Marino     fprintf (dump_file,
1284*e4b17023SJohn Marino 	     "Attempting to migrate pt from insn %d (cost = %d, min_cost = %d) ... ",
1285*e4b17023SJohn Marino 	     INSN_UID (def->insn), def->cost, min_cost);
1286*e4b17023SJohn Marino 
1287*e4b17023SJohn Marino   if (!def->group || def->has_ambiguous_use)
1288*e4b17023SJohn Marino     /* These defs are not migratable.  */
1289*e4b17023SJohn Marino     {
1290*e4b17023SJohn Marino       if (dump_file)
1291*e4b17023SJohn Marino 	fprintf (dump_file, "it's not migratable\n");
1292*e4b17023SJohn Marino       return 0;
1293*e4b17023SJohn Marino     }
1294*e4b17023SJohn Marino 
1295*e4b17023SJohn Marino   if (!def->uses)
1296*e4b17023SJohn Marino     /* We have combined this def with another in the same group, so
1297*e4b17023SJohn Marino        no need to consider it further.
1298*e4b17023SJohn Marino     */
1299*e4b17023SJohn Marino     {
1300*e4b17023SJohn Marino       if (dump_file)
1301*e4b17023SJohn Marino 	fprintf (dump_file, "it's already combined with another pt\n");
1302*e4b17023SJohn Marino       return 0;
1303*e4b17023SJohn Marino     }
1304*e4b17023SJohn Marino 
1305*e4b17023SJohn Marino   btr_def_live_range (def, &btrs_live_in_range);
1306*e4b17023SJohn Marino   live_range = BITMAP_ALLOC (NULL);
1307*e4b17023SJohn Marino   bitmap_copy (live_range, def->live_range);
1308*e4b17023SJohn Marino 
1309*e4b17023SJohn Marino #ifdef INSN_SCHEDULING
1310*e4b17023SJohn Marino   def_latency = insn_default_latency (def->insn) * issue_rate;
1311*e4b17023SJohn Marino #else
1312*e4b17023SJohn Marino   def_latency = issue_rate;
1313*e4b17023SJohn Marino #endif
1314*e4b17023SJohn Marino 
1315*e4b17023SJohn Marino   for (user = def->uses; user != NULL; user = user->next)
1316*e4b17023SJohn Marino     {
1317*e4b17023SJohn Marino       if (user->bb == def->bb
1318*e4b17023SJohn Marino 	  && user->luid > def->luid
1319*e4b17023SJohn Marino 	  && (def->luid + def_latency) > user->luid
1320*e4b17023SJohn Marino 	  && ! can_move_up (def->bb, def->insn,
1321*e4b17023SJohn Marino 			    (def->luid + def_latency) - user->luid))
1322*e4b17023SJohn Marino 	{
1323*e4b17023SJohn Marino 	  btr_used_near_def = 1;
1324*e4b17023SJohn Marino 	  break;
1325*e4b17023SJohn Marino 	}
1326*e4b17023SJohn Marino     }
1327*e4b17023SJohn Marino 
1328*e4b17023SJohn Marino   def_basic_block_freq = basic_block_freq (def->bb);
1329*e4b17023SJohn Marino 
1330*e4b17023SJohn Marino   for (attempt = get_immediate_dominator (CDI_DOMINATORS, def->bb);
1331*e4b17023SJohn Marino        !give_up && attempt && attempt != ENTRY_BLOCK_PTR && def->cost >= min_cost;
1332*e4b17023SJohn Marino        attempt = get_immediate_dominator (CDI_DOMINATORS, attempt))
1333*e4b17023SJohn Marino     {
1334*e4b17023SJohn Marino       /* Try to move the instruction that sets the target register into
1335*e4b17023SJohn Marino 	 basic block ATTEMPT.  */
1336*e4b17023SJohn Marino       int try_freq = basic_block_freq (attempt);
1337*e4b17023SJohn Marino       edge_iterator ei;
1338*e4b17023SJohn Marino       edge e;
1339*e4b17023SJohn Marino 
1340*e4b17023SJohn Marino       /* If ATTEMPT has abnormal edges, skip it.  */
1341*e4b17023SJohn Marino       FOR_EACH_EDGE (e, ei, attempt->succs)
1342*e4b17023SJohn Marino 	if (e->flags & EDGE_COMPLEX)
1343*e4b17023SJohn Marino 	  break;
1344*e4b17023SJohn Marino       if (e)
1345*e4b17023SJohn Marino 	continue;
1346*e4b17023SJohn Marino 
1347*e4b17023SJohn Marino       if (dump_file)
1348*e4b17023SJohn Marino 	fprintf (dump_file, "trying block %d ...", attempt->index);
1349*e4b17023SJohn Marino 
1350*e4b17023SJohn Marino       if (try_freq < def_basic_block_freq
1351*e4b17023SJohn Marino 	  || (try_freq == def_basic_block_freq && btr_used_near_def))
1352*e4b17023SJohn Marino 	{
1353*e4b17023SJohn Marino 	  int btr;
1354*e4b17023SJohn Marino 	  augment_live_range (live_range, &btrs_live_in_range, def->bb, attempt,
1355*e4b17023SJohn Marino 			      flag_btr_bb_exclusive);
1356*e4b17023SJohn Marino 	  if (dump_file)
1357*e4b17023SJohn Marino 	    {
1358*e4b17023SJohn Marino 	      fprintf (dump_file, "Now btrs live in range are: ");
1359*e4b17023SJohn Marino 	      dump_hard_reg_set (btrs_live_in_range);
1360*e4b17023SJohn Marino 	      fprintf (dump_file, "\n");
1361*e4b17023SJohn Marino 	    }
1362*e4b17023SJohn Marino 	  btr = choose_btr (btrs_live_in_range);
1363*e4b17023SJohn Marino 	  if (btr != -1)
1364*e4b17023SJohn Marino 	    {
1365*e4b17023SJohn Marino 	      move_btr_def (attempt, btr, def, live_range, &btrs_live_in_range);
1366*e4b17023SJohn Marino 	      bitmap_copy(live_range, def->live_range);
1367*e4b17023SJohn Marino 	      btr_used_near_def = 0;
1368*e4b17023SJohn Marino 	      def_moved = 1;
1369*e4b17023SJohn Marino 	      def_basic_block_freq = basic_block_freq (def->bb);
1370*e4b17023SJohn Marino 	    }
1371*e4b17023SJohn Marino 	  else
1372*e4b17023SJohn Marino 	    {
1373*e4b17023SJohn Marino 	      /* There are no free target registers available to move
1374*e4b17023SJohn Marino 		 this far forward, so give up */
1375*e4b17023SJohn Marino 	      give_up = 1;
1376*e4b17023SJohn Marino 	      if (dump_file)
1377*e4b17023SJohn Marino 		fprintf (dump_file,
1378*e4b17023SJohn Marino 			 "giving up because there are no free target registers\n");
1379*e4b17023SJohn Marino 	    }
1380*e4b17023SJohn Marino 
1381*e4b17023SJohn Marino 	}
1382*e4b17023SJohn Marino     }
1383*e4b17023SJohn Marino   if (!def_moved)
1384*e4b17023SJohn Marino     {
1385*e4b17023SJohn Marino       give_up = 1;
1386*e4b17023SJohn Marino       if (dump_file)
1387*e4b17023SJohn Marino 	fprintf (dump_file, "failed to move\n");
1388*e4b17023SJohn Marino     }
1389*e4b17023SJohn Marino   BITMAP_FREE (live_range);
1390*e4b17023SJohn Marino   return !give_up;
1391*e4b17023SJohn Marino }
1392*e4b17023SJohn Marino 
1393*e4b17023SJohn Marino /* Attempt to move instructions that set target registers earlier
1394*e4b17023SJohn Marino    in the flowgraph, away from their corresponding uses.  */
1395*e4b17023SJohn Marino static void
migrate_btr_defs(enum reg_class btr_class,int allow_callee_save)1396*e4b17023SJohn Marino migrate_btr_defs (enum reg_class btr_class, int allow_callee_save)
1397*e4b17023SJohn Marino {
1398*e4b17023SJohn Marino   fibheap_t all_btr_defs = fibheap_new ();
1399*e4b17023SJohn Marino   int reg;
1400*e4b17023SJohn Marino 
1401*e4b17023SJohn Marino   gcc_obstack_init (&migrate_btrl_obstack);
1402*e4b17023SJohn Marino   if (dump_file)
1403*e4b17023SJohn Marino     {
1404*e4b17023SJohn Marino       int i;
1405*e4b17023SJohn Marino 
1406*e4b17023SJohn Marino       for (i = NUM_FIXED_BLOCKS; i < last_basic_block; i++)
1407*e4b17023SJohn Marino 	{
1408*e4b17023SJohn Marino 	  basic_block bb = BASIC_BLOCK (i);
1409*e4b17023SJohn Marino 	  fprintf(dump_file,
1410*e4b17023SJohn Marino 	    "Basic block %d: count = " HOST_WIDEST_INT_PRINT_DEC
1411*e4b17023SJohn Marino 	    " loop-depth = %d idom = %d\n",
1412*e4b17023SJohn Marino 	    i, (HOST_WIDEST_INT) bb->count, bb->loop_depth,
1413*e4b17023SJohn Marino 	    get_immediate_dominator (CDI_DOMINATORS, bb)->index);
1414*e4b17023SJohn Marino 	}
1415*e4b17023SJohn Marino     }
1416*e4b17023SJohn Marino 
1417*e4b17023SJohn Marino   CLEAR_HARD_REG_SET (all_btrs);
1418*e4b17023SJohn Marino   for (first_btr = -1, reg = 0; reg < FIRST_PSEUDO_REGISTER; reg++)
1419*e4b17023SJohn Marino     if (TEST_HARD_REG_BIT (reg_class_contents[(int) btr_class], reg)
1420*e4b17023SJohn Marino 	&& (allow_callee_save || call_used_regs[reg]
1421*e4b17023SJohn Marino 	    || df_regs_ever_live_p (reg)))
1422*e4b17023SJohn Marino       {
1423*e4b17023SJohn Marino 	SET_HARD_REG_BIT (all_btrs, reg);
1424*e4b17023SJohn Marino 	last_btr = reg;
1425*e4b17023SJohn Marino 	if (first_btr < 0)
1426*e4b17023SJohn Marino 	  first_btr = reg;
1427*e4b17023SJohn Marino       }
1428*e4b17023SJohn Marino 
1429*e4b17023SJohn Marino   btrs_live = XCNEWVEC (HARD_REG_SET, last_basic_block);
1430*e4b17023SJohn Marino   btrs_live_at_end = XCNEWVEC (HARD_REG_SET, last_basic_block);
1431*e4b17023SJohn Marino 
1432*e4b17023SJohn Marino   build_btr_def_use_webs (all_btr_defs);
1433*e4b17023SJohn Marino 
1434*e4b17023SJohn Marino   while (!fibheap_empty (all_btr_defs))
1435*e4b17023SJohn Marino     {
1436*e4b17023SJohn Marino       btr_def def = (btr_def) fibheap_extract_min (all_btr_defs);
1437*e4b17023SJohn Marino       int min_cost = -fibheap_min_key (all_btr_defs);
1438*e4b17023SJohn Marino       if (migrate_btr_def (def, min_cost))
1439*e4b17023SJohn Marino 	{
1440*e4b17023SJohn Marino 	  fibheap_insert (all_btr_defs, -def->cost, (void *) def);
1441*e4b17023SJohn Marino 	  if (dump_file)
1442*e4b17023SJohn Marino 	    {
1443*e4b17023SJohn Marino 	      fprintf (dump_file,
1444*e4b17023SJohn Marino 		"Putting insn %d back on queue with priority %d\n",
1445*e4b17023SJohn Marino 		INSN_UID (def->insn), def->cost);
1446*e4b17023SJohn Marino 	    }
1447*e4b17023SJohn Marino 	}
1448*e4b17023SJohn Marino       else
1449*e4b17023SJohn Marino 	BITMAP_FREE (def->live_range);
1450*e4b17023SJohn Marino     }
1451*e4b17023SJohn Marino 
1452*e4b17023SJohn Marino   free (btrs_live);
1453*e4b17023SJohn Marino   free (btrs_live_at_end);
1454*e4b17023SJohn Marino   obstack_free (&migrate_btrl_obstack, NULL);
1455*e4b17023SJohn Marino   fibheap_delete (all_btr_defs);
1456*e4b17023SJohn Marino }
1457*e4b17023SJohn Marino 
1458*e4b17023SJohn Marino static void
branch_target_load_optimize(bool after_prologue_epilogue_gen)1459*e4b17023SJohn Marino branch_target_load_optimize (bool after_prologue_epilogue_gen)
1460*e4b17023SJohn Marino {
1461*e4b17023SJohn Marino   enum reg_class klass
1462*e4b17023SJohn Marino     = (enum reg_class) targetm.branch_target_register_class ();
1463*e4b17023SJohn Marino   if (klass != NO_REGS)
1464*e4b17023SJohn Marino     {
1465*e4b17023SJohn Marino       /* Initialize issue_rate.  */
1466*e4b17023SJohn Marino       if (targetm.sched.issue_rate)
1467*e4b17023SJohn Marino 	issue_rate = targetm.sched.issue_rate ();
1468*e4b17023SJohn Marino       else
1469*e4b17023SJohn Marino 	issue_rate = 1;
1470*e4b17023SJohn Marino 
1471*e4b17023SJohn Marino       if (!after_prologue_epilogue_gen)
1472*e4b17023SJohn Marino 	{
1473*e4b17023SJohn Marino 	  /* Build the CFG for migrate_btr_defs.  */
1474*e4b17023SJohn Marino #if 1
1475*e4b17023SJohn Marino 	  /* This may or may not be needed, depending on where we
1476*e4b17023SJohn Marino 	     run this phase.  */
1477*e4b17023SJohn Marino 	  cleanup_cfg (optimize ? CLEANUP_EXPENSIVE : 0);
1478*e4b17023SJohn Marino #endif
1479*e4b17023SJohn Marino 	}
1480*e4b17023SJohn Marino       df_analyze ();
1481*e4b17023SJohn Marino 
1482*e4b17023SJohn Marino 
1483*e4b17023SJohn Marino       /* Dominator info is also needed for migrate_btr_def.  */
1484*e4b17023SJohn Marino       calculate_dominance_info (CDI_DOMINATORS);
1485*e4b17023SJohn Marino       migrate_btr_defs (klass,
1486*e4b17023SJohn Marino 		       (targetm.branch_target_register_callee_saved
1487*e4b17023SJohn Marino 			(after_prologue_epilogue_gen)));
1488*e4b17023SJohn Marino 
1489*e4b17023SJohn Marino       free_dominance_info (CDI_DOMINATORS);
1490*e4b17023SJohn Marino     }
1491*e4b17023SJohn Marino }
1492*e4b17023SJohn Marino 
1493*e4b17023SJohn Marino static bool
gate_handle_branch_target_load_optimize1(void)1494*e4b17023SJohn Marino gate_handle_branch_target_load_optimize1 (void)
1495*e4b17023SJohn Marino {
1496*e4b17023SJohn Marino   return flag_branch_target_load_optimize;
1497*e4b17023SJohn Marino }
1498*e4b17023SJohn Marino 
1499*e4b17023SJohn Marino 
1500*e4b17023SJohn Marino static unsigned int
rest_of_handle_branch_target_load_optimize1(void)1501*e4b17023SJohn Marino rest_of_handle_branch_target_load_optimize1 (void)
1502*e4b17023SJohn Marino {
1503*e4b17023SJohn Marino   branch_target_load_optimize (epilogue_completed);
1504*e4b17023SJohn Marino   return 0;
1505*e4b17023SJohn Marino }
1506*e4b17023SJohn Marino 
1507*e4b17023SJohn Marino struct rtl_opt_pass pass_branch_target_load_optimize1 =
1508*e4b17023SJohn Marino {
1509*e4b17023SJohn Marino  {
1510*e4b17023SJohn Marino   RTL_PASS,
1511*e4b17023SJohn Marino   "btl1",                               /* name */
1512*e4b17023SJohn Marino   gate_handle_branch_target_load_optimize1,      /* gate */
1513*e4b17023SJohn Marino   rest_of_handle_branch_target_load_optimize1,   /* execute */
1514*e4b17023SJohn Marino   NULL,                                 /* sub */
1515*e4b17023SJohn Marino   NULL,                                 /* next */
1516*e4b17023SJohn Marino   0,                                    /* static_pass_number */
1517*e4b17023SJohn Marino   TV_NONE,	                        /* tv_id */
1518*e4b17023SJohn Marino   0,                                    /* properties_required */
1519*e4b17023SJohn Marino   0,                                    /* properties_provided */
1520*e4b17023SJohn Marino   0,                                    /* properties_destroyed */
1521*e4b17023SJohn Marino   0,                                    /* todo_flags_start */
1522*e4b17023SJohn Marino   TODO_verify_rtl_sharing |
1523*e4b17023SJohn Marino   TODO_ggc_collect,                     /* todo_flags_finish */
1524*e4b17023SJohn Marino  }
1525*e4b17023SJohn Marino };
1526*e4b17023SJohn Marino 
1527*e4b17023SJohn Marino static bool
gate_handle_branch_target_load_optimize2(void)1528*e4b17023SJohn Marino gate_handle_branch_target_load_optimize2 (void)
1529*e4b17023SJohn Marino {
1530*e4b17023SJohn Marino   return (optimize > 0 && flag_branch_target_load_optimize2);
1531*e4b17023SJohn Marino }
1532*e4b17023SJohn Marino 
1533*e4b17023SJohn Marino 
1534*e4b17023SJohn Marino static unsigned int
rest_of_handle_branch_target_load_optimize2(void)1535*e4b17023SJohn Marino rest_of_handle_branch_target_load_optimize2 (void)
1536*e4b17023SJohn Marino {
1537*e4b17023SJohn Marino   static int warned = 0;
1538*e4b17023SJohn Marino 
1539*e4b17023SJohn Marino   /* Leave this a warning for now so that it is possible to experiment
1540*e4b17023SJohn Marino      with running this pass twice.  In 3.6, we should either make this
1541*e4b17023SJohn Marino      an error, or use separate dump files.  */
1542*e4b17023SJohn Marino   if (flag_branch_target_load_optimize
1543*e4b17023SJohn Marino       && flag_branch_target_load_optimize2
1544*e4b17023SJohn Marino       && !warned)
1545*e4b17023SJohn Marino     {
1546*e4b17023SJohn Marino       warning (0, "branch target register load optimization is not intended "
1547*e4b17023SJohn Marino 		  "to be run twice");
1548*e4b17023SJohn Marino 
1549*e4b17023SJohn Marino       warned = 1;
1550*e4b17023SJohn Marino     }
1551*e4b17023SJohn Marino 
1552*e4b17023SJohn Marino   branch_target_load_optimize (epilogue_completed);
1553*e4b17023SJohn Marino   return 0;
1554*e4b17023SJohn Marino }
1555*e4b17023SJohn Marino 
1556*e4b17023SJohn Marino struct rtl_opt_pass pass_branch_target_load_optimize2 =
1557*e4b17023SJohn Marino {
1558*e4b17023SJohn Marino  {
1559*e4b17023SJohn Marino   RTL_PASS,
1560*e4b17023SJohn Marino   "btl2",                               /* name */
1561*e4b17023SJohn Marino   gate_handle_branch_target_load_optimize2,      /* gate */
1562*e4b17023SJohn Marino   rest_of_handle_branch_target_load_optimize2,   /* execute */
1563*e4b17023SJohn Marino   NULL,                                 /* sub */
1564*e4b17023SJohn Marino   NULL,                                 /* next */
1565*e4b17023SJohn Marino   0,                                    /* static_pass_number */
1566*e4b17023SJohn Marino   TV_NONE,				/* tv_id */
1567*e4b17023SJohn Marino   0,                                    /* properties_required */
1568*e4b17023SJohn Marino   0,                                    /* properties_provided */
1569*e4b17023SJohn Marino   0,                                    /* properties_destroyed */
1570*e4b17023SJohn Marino   0,                                    /* todo_flags_start */
1571*e4b17023SJohn Marino   TODO_ggc_collect,                     /* todo_flags_finish */
1572*e4b17023SJohn Marino  }
1573*e4b17023SJohn Marino };
1574