xref: /dflybsd-src/contrib/gcc-8.0/gcc/tree-phinodes.c (revision 38fd149817dfbff97799f62fcb70be98c4e32523)
1*38fd1498Szrj /* Generic routines for manipulating PHIs
2*38fd1498Szrj    Copyright (C) 2003-2018 Free Software Foundation, Inc.
3*38fd1498Szrj 
4*38fd1498Szrj This file is part of GCC.
5*38fd1498Szrj 
6*38fd1498Szrj GCC is free software; you can redistribute it and/or modify
7*38fd1498Szrj it under the terms of the GNU General Public License as published by
8*38fd1498Szrj the Free Software Foundation; either version 3, or (at your option)
9*38fd1498Szrj any later version.
10*38fd1498Szrj 
11*38fd1498Szrj GCC is distributed in the hope that it will be useful,
12*38fd1498Szrj but WITHOUT ANY WARRANTY; without even the implied warranty of
13*38fd1498Szrj MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*38fd1498Szrj GNU General Public License for more details.
15*38fd1498Szrj 
16*38fd1498Szrj You should have received a copy of the GNU General Public License
17*38fd1498Szrj along with GCC; see the file COPYING3.  If not see
18*38fd1498Szrj <http://www.gnu.org/licenses/>.  */
19*38fd1498Szrj 
20*38fd1498Szrj #include "config.h"
21*38fd1498Szrj #include "system.h"
22*38fd1498Szrj #include "coretypes.h"
23*38fd1498Szrj #include "backend.h"
24*38fd1498Szrj #include "tree.h"
25*38fd1498Szrj #include "gimple.h"
26*38fd1498Szrj #include "ssa.h"
27*38fd1498Szrj #include "fold-const.h"
28*38fd1498Szrj #include "gimple-iterator.h"
29*38fd1498Szrj #include "tree-ssa.h"
30*38fd1498Szrj 
31*38fd1498Szrj /* Rewriting a function into SSA form can create a huge number of PHIs
32*38fd1498Szrj    many of which may be thrown away shortly after their creation if jumps
33*38fd1498Szrj    were threaded through PHI nodes.
34*38fd1498Szrj 
35*38fd1498Szrj    While our garbage collection mechanisms will handle this situation, it
36*38fd1498Szrj    is extremely wasteful to create nodes and throw them away, especially
37*38fd1498Szrj    when the nodes can be reused.
38*38fd1498Szrj 
39*38fd1498Szrj    For PR 8361, we can significantly reduce the number of nodes allocated
40*38fd1498Szrj    and thus the total amount of memory allocated by managing PHIs a
41*38fd1498Szrj    little.  This additionally helps reduce the amount of work done by the
42*38fd1498Szrj    garbage collector.  Similar results have been seen on a wider variety
43*38fd1498Szrj    of tests (such as the compiler itself).
44*38fd1498Szrj 
45*38fd1498Szrj    PHI nodes have different sizes, so we can't have a single list of all
46*38fd1498Szrj    the PHI nodes as it would be too expensive to walk down that list to
47*38fd1498Szrj    find a PHI of a suitable size.
48*38fd1498Szrj 
49*38fd1498Szrj    Instead we have an array of lists of free PHI nodes.  The array is
50*38fd1498Szrj    indexed by the number of PHI alternatives that PHI node can hold.
51*38fd1498Szrj    Except for the last array member, which holds all remaining PHI
52*38fd1498Szrj    nodes.
53*38fd1498Szrj 
54*38fd1498Szrj    So to find a free PHI node, we compute its index into the free PHI
55*38fd1498Szrj    node array and see if there are any elements with an exact match.
56*38fd1498Szrj    If so, then we are done.  Otherwise, we test the next larger size
57*38fd1498Szrj    up and continue until we are in the last array element.
58*38fd1498Szrj 
59*38fd1498Szrj    We do not actually walk members of the last array element.  While it
60*38fd1498Szrj    might allow us to pick up a few reusable PHI nodes, it could potentially
61*38fd1498Szrj    be very expensive if the program has released a bunch of large PHI nodes,
62*38fd1498Szrj    but keeps asking for even larger PHI nodes.  Experiments have shown that
63*38fd1498Szrj    walking the elements of the last array entry would result in finding less
64*38fd1498Szrj    than .1% additional reusable PHI nodes.
65*38fd1498Szrj 
66*38fd1498Szrj    Note that we can never have less than two PHI argument slots.  Thus,
67*38fd1498Szrj    the -2 on all the calculations below.  */
68*38fd1498Szrj 
69*38fd1498Szrj #define NUM_BUCKETS 10
70*38fd1498Szrj static GTY ((deletable (""))) vec<gimple *, va_gc> *free_phinodes[NUM_BUCKETS - 2];
71*38fd1498Szrj static unsigned long free_phinode_count;
72*38fd1498Szrj 
73*38fd1498Szrj static int ideal_phi_node_len (int);
74*38fd1498Szrj 
75*38fd1498Szrj unsigned int phi_nodes_reused;
76*38fd1498Szrj unsigned int phi_nodes_created;
77*38fd1498Szrj 
78*38fd1498Szrj /* Dump some simple statistics regarding the re-use of PHI nodes.  */
79*38fd1498Szrj 
80*38fd1498Szrj void
phinodes_print_statistics(void)81*38fd1498Szrj phinodes_print_statistics (void)
82*38fd1498Szrj {
83*38fd1498Szrj   fprintf (stderr, "PHI nodes allocated: %u\n", phi_nodes_created);
84*38fd1498Szrj   fprintf (stderr, "PHI nodes reused: %u\n", phi_nodes_reused);
85*38fd1498Szrj }
86*38fd1498Szrj 
87*38fd1498Szrj /* Allocate a PHI node with at least LEN arguments.  If the free list
88*38fd1498Szrj    happens to contain a PHI node with LEN arguments or more, return
89*38fd1498Szrj    that one.  */
90*38fd1498Szrj 
91*38fd1498Szrj static inline gphi *
allocate_phi_node(size_t len)92*38fd1498Szrj allocate_phi_node (size_t len)
93*38fd1498Szrj {
94*38fd1498Szrj   gphi *phi;
95*38fd1498Szrj   size_t bucket = NUM_BUCKETS - 2;
96*38fd1498Szrj   size_t size = sizeof (struct gphi)
97*38fd1498Szrj 	        + (len - 1) * sizeof (struct phi_arg_d);
98*38fd1498Szrj 
99*38fd1498Szrj   if (free_phinode_count)
100*38fd1498Szrj     for (bucket = len - 2; bucket < NUM_BUCKETS - 2; bucket++)
101*38fd1498Szrj       if (free_phinodes[bucket])
102*38fd1498Szrj 	break;
103*38fd1498Szrj 
104*38fd1498Szrj   /* If our free list has an element, then use it.  */
105*38fd1498Szrj   if (bucket < NUM_BUCKETS - 2
106*38fd1498Szrj       && gimple_phi_capacity ((*free_phinodes[bucket])[0]) >= len)
107*38fd1498Szrj     {
108*38fd1498Szrj       free_phinode_count--;
109*38fd1498Szrj       phi = as_a <gphi *> (free_phinodes[bucket]->pop ());
110*38fd1498Szrj       if (free_phinodes[bucket]->is_empty ())
111*38fd1498Szrj 	vec_free (free_phinodes[bucket]);
112*38fd1498Szrj       if (GATHER_STATISTICS)
113*38fd1498Szrj 	phi_nodes_reused++;
114*38fd1498Szrj     }
115*38fd1498Szrj   else
116*38fd1498Szrj     {
117*38fd1498Szrj       phi = static_cast <gphi *> (ggc_internal_alloc (size));
118*38fd1498Szrj       if (GATHER_STATISTICS)
119*38fd1498Szrj 	{
120*38fd1498Szrj 	  enum gimple_alloc_kind kind = gimple_alloc_kind (GIMPLE_PHI);
121*38fd1498Szrj 	  phi_nodes_created++;
122*38fd1498Szrj 	  gimple_alloc_counts[(int) kind]++;
123*38fd1498Szrj 	  gimple_alloc_sizes[(int) kind] += size;
124*38fd1498Szrj 	}
125*38fd1498Szrj     }
126*38fd1498Szrj 
127*38fd1498Szrj   return phi;
128*38fd1498Szrj }
129*38fd1498Szrj 
130*38fd1498Szrj /* Given LEN, the original number of requested PHI arguments, return
131*38fd1498Szrj    a new, "ideal" length for the PHI node.  The "ideal" length rounds
132*38fd1498Szrj    the total size of the PHI node up to the next power of two bytes.
133*38fd1498Szrj 
134*38fd1498Szrj    Rounding up will not result in wasting any memory since the size request
135*38fd1498Szrj    will be rounded up by the GC system anyway.  [ Note this is not entirely
136*38fd1498Szrj    true since the original length might have fit on one of the special
137*38fd1498Szrj    GC pages. ]  By rounding up, we may avoid the need to reallocate the
138*38fd1498Szrj    PHI node later if we increase the number of arguments for the PHI.  */
139*38fd1498Szrj 
140*38fd1498Szrj static int
ideal_phi_node_len(int len)141*38fd1498Szrj ideal_phi_node_len (int len)
142*38fd1498Szrj {
143*38fd1498Szrj   size_t size, new_size;
144*38fd1498Szrj   int log2, new_len;
145*38fd1498Szrj 
146*38fd1498Szrj   /* We do not support allocations of less than two PHI argument slots.  */
147*38fd1498Szrj   if (len < 2)
148*38fd1498Szrj     len = 2;
149*38fd1498Szrj 
150*38fd1498Szrj   /* Compute the number of bytes of the original request.  */
151*38fd1498Szrj   size = sizeof (struct gphi)
152*38fd1498Szrj 	 + (len - 1) * sizeof (struct phi_arg_d);
153*38fd1498Szrj 
154*38fd1498Szrj   /* Round it up to the next power of two.  */
155*38fd1498Szrj   log2 = ceil_log2 (size);
156*38fd1498Szrj   new_size = 1 << log2;
157*38fd1498Szrj 
158*38fd1498Szrj   /* Now compute and return the number of PHI argument slots given an
159*38fd1498Szrj      ideal size allocation.  */
160*38fd1498Szrj   new_len = len + (new_size - size) / sizeof (struct phi_arg_d);
161*38fd1498Szrj   return new_len;
162*38fd1498Szrj }
163*38fd1498Szrj 
164*38fd1498Szrj /* Return a PHI node with LEN argument slots for variable VAR.  */
165*38fd1498Szrj 
166*38fd1498Szrj static gphi *
make_phi_node(tree var,int len)167*38fd1498Szrj make_phi_node (tree var, int len)
168*38fd1498Szrj {
169*38fd1498Szrj   gphi *phi;
170*38fd1498Szrj   int capacity, i;
171*38fd1498Szrj 
172*38fd1498Szrj   capacity = ideal_phi_node_len (len);
173*38fd1498Szrj 
174*38fd1498Szrj   phi = allocate_phi_node (capacity);
175*38fd1498Szrj 
176*38fd1498Szrj   /* We need to clear the entire PHI node, including the argument
177*38fd1498Szrj      portion, because we represent a "missing PHI argument" by placing
178*38fd1498Szrj      NULL_TREE in PHI_ARG_DEF.  */
179*38fd1498Szrj   memset (phi, 0, (sizeof (struct gphi)
180*38fd1498Szrj 		   - sizeof (struct phi_arg_d)
181*38fd1498Szrj 		   + sizeof (struct phi_arg_d) * len));
182*38fd1498Szrj   phi->code = GIMPLE_PHI;
183*38fd1498Szrj   gimple_init_singleton (phi);
184*38fd1498Szrj   phi->nargs = len;
185*38fd1498Szrj   phi->capacity = capacity;
186*38fd1498Szrj   if (!var)
187*38fd1498Szrj     ;
188*38fd1498Szrj   else if (TREE_CODE (var) == SSA_NAME)
189*38fd1498Szrj     gimple_phi_set_result (phi, var);
190*38fd1498Szrj   else
191*38fd1498Szrj     gimple_phi_set_result (phi, make_ssa_name (var, phi));
192*38fd1498Szrj 
193*38fd1498Szrj   for (i = 0; i < len; i++)
194*38fd1498Szrj     {
195*38fd1498Szrj       use_operand_p  imm;
196*38fd1498Szrj 
197*38fd1498Szrj       gimple_phi_arg_set_location (phi, i, UNKNOWN_LOCATION);
198*38fd1498Szrj       imm = gimple_phi_arg_imm_use_ptr (phi, i);
199*38fd1498Szrj       imm->use = gimple_phi_arg_def_ptr (phi, i);
200*38fd1498Szrj       imm->prev = NULL;
201*38fd1498Szrj       imm->next = NULL;
202*38fd1498Szrj       imm->loc.stmt = phi;
203*38fd1498Szrj     }
204*38fd1498Szrj 
205*38fd1498Szrj   return phi;
206*38fd1498Szrj }
207*38fd1498Szrj 
208*38fd1498Szrj /* We no longer need PHI, release it so that it may be reused.  */
209*38fd1498Szrj 
210*38fd1498Szrj static void
release_phi_node(gimple * phi)211*38fd1498Szrj release_phi_node (gimple *phi)
212*38fd1498Szrj {
213*38fd1498Szrj   size_t bucket;
214*38fd1498Szrj   size_t len = gimple_phi_capacity (phi);
215*38fd1498Szrj   size_t x;
216*38fd1498Szrj 
217*38fd1498Szrj   for (x = 0; x < gimple_phi_num_args (phi); x++)
218*38fd1498Szrj     {
219*38fd1498Szrj       use_operand_p  imm;
220*38fd1498Szrj       imm = gimple_phi_arg_imm_use_ptr (phi, x);
221*38fd1498Szrj       delink_imm_use (imm);
222*38fd1498Szrj     }
223*38fd1498Szrj 
224*38fd1498Szrj   bucket = len > NUM_BUCKETS - 1 ? NUM_BUCKETS - 1 : len;
225*38fd1498Szrj   bucket -= 2;
226*38fd1498Szrj   vec_safe_push (free_phinodes[bucket], phi);
227*38fd1498Szrj   free_phinode_count++;
228*38fd1498Szrj }
229*38fd1498Szrj 
230*38fd1498Szrj 
231*38fd1498Szrj /* Resize an existing PHI node.  The only way is up.  Return the
232*38fd1498Szrj    possibly relocated phi.  */
233*38fd1498Szrj 
234*38fd1498Szrj static gphi *
resize_phi_node(gphi * phi,size_t len)235*38fd1498Szrj resize_phi_node (gphi *phi, size_t len)
236*38fd1498Szrj {
237*38fd1498Szrj   size_t old_size, i;
238*38fd1498Szrj   gphi *new_phi;
239*38fd1498Szrj 
240*38fd1498Szrj   gcc_assert (len > gimple_phi_capacity (phi));
241*38fd1498Szrj 
242*38fd1498Szrj   /* The garbage collector will not look at the PHI node beyond the
243*38fd1498Szrj      first PHI_NUM_ARGS elements.  Therefore, all we have to copy is a
244*38fd1498Szrj      portion of the PHI node currently in use.  */
245*38fd1498Szrj   old_size = sizeof (struct gphi)
246*38fd1498Szrj 	     + (gimple_phi_num_args (phi) - 1) * sizeof (struct phi_arg_d);
247*38fd1498Szrj 
248*38fd1498Szrj   new_phi = allocate_phi_node (len);
249*38fd1498Szrj 
250*38fd1498Szrj   memcpy (new_phi, phi, old_size);
251*38fd1498Szrj   memset ((char *)new_phi + old_size, 0,
252*38fd1498Szrj 	  (sizeof (struct gphi)
253*38fd1498Szrj 	   - sizeof (struct phi_arg_d)
254*38fd1498Szrj 	   + sizeof (struct phi_arg_d) * len) - old_size);
255*38fd1498Szrj 
256*38fd1498Szrj   for (i = 0; i < gimple_phi_num_args (new_phi); i++)
257*38fd1498Szrj     {
258*38fd1498Szrj       use_operand_p imm, old_imm;
259*38fd1498Szrj       imm = gimple_phi_arg_imm_use_ptr (new_phi, i);
260*38fd1498Szrj       old_imm = gimple_phi_arg_imm_use_ptr (phi, i);
261*38fd1498Szrj       imm->use = gimple_phi_arg_def_ptr (new_phi, i);
262*38fd1498Szrj       relink_imm_use_stmt (imm, old_imm, new_phi);
263*38fd1498Szrj     }
264*38fd1498Szrj 
265*38fd1498Szrj   new_phi->capacity = len;
266*38fd1498Szrj 
267*38fd1498Szrj   return new_phi;
268*38fd1498Szrj }
269*38fd1498Szrj 
270*38fd1498Szrj /* Reserve PHI arguments for a new edge to basic block BB.  */
271*38fd1498Szrj 
272*38fd1498Szrj void
reserve_phi_args_for_new_edge(basic_block bb)273*38fd1498Szrj reserve_phi_args_for_new_edge (basic_block bb)
274*38fd1498Szrj {
275*38fd1498Szrj   size_t len = EDGE_COUNT (bb->preds);
276*38fd1498Szrj   size_t cap = ideal_phi_node_len (len + 4);
277*38fd1498Szrj   gphi_iterator gsi;
278*38fd1498Szrj 
279*38fd1498Szrj   for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
280*38fd1498Szrj     {
281*38fd1498Szrj       gphi *stmt = gsi.phi ();
282*38fd1498Szrj 
283*38fd1498Szrj       if (len > gimple_phi_capacity (stmt))
284*38fd1498Szrj 	{
285*38fd1498Szrj 	  gphi *new_phi = resize_phi_node (stmt, cap);
286*38fd1498Szrj 
287*38fd1498Szrj 	  /* The result of the PHI is defined by this PHI node.  */
288*38fd1498Szrj 	  SSA_NAME_DEF_STMT (gimple_phi_result (new_phi)) = new_phi;
289*38fd1498Szrj 	  gsi_set_stmt (&gsi, new_phi);
290*38fd1498Szrj 
291*38fd1498Szrj 	  release_phi_node (stmt);
292*38fd1498Szrj 	  stmt = new_phi;
293*38fd1498Szrj 	}
294*38fd1498Szrj 
295*38fd1498Szrj       stmt->nargs++;
296*38fd1498Szrj 
297*38fd1498Szrj       /* We represent a "missing PHI argument" by placing NULL_TREE in
298*38fd1498Szrj 	 the corresponding slot.  If PHI arguments were added
299*38fd1498Szrj 	 immediately after an edge is created, this zeroing would not
300*38fd1498Szrj 	 be necessary, but unfortunately this is not the case.  For
301*38fd1498Szrj 	 example, the loop optimizer duplicates several basic blocks,
302*38fd1498Szrj 	 redirects edges, and then fixes up PHI arguments later in
303*38fd1498Szrj 	 batch.  */
304*38fd1498Szrj       use_operand_p imm = gimple_phi_arg_imm_use_ptr (stmt, len - 1);
305*38fd1498Szrj       imm->use = gimple_phi_arg_def_ptr (stmt, len - 1);
306*38fd1498Szrj       imm->prev = NULL;
307*38fd1498Szrj       imm->next = NULL;
308*38fd1498Szrj       imm->loc.stmt = stmt;
309*38fd1498Szrj       SET_PHI_ARG_DEF (stmt, len - 1, NULL_TREE);
310*38fd1498Szrj       gimple_phi_arg_set_location (stmt, len - 1, UNKNOWN_LOCATION);
311*38fd1498Szrj     }
312*38fd1498Szrj }
313*38fd1498Szrj 
314*38fd1498Szrj /* Adds PHI to BB.  */
315*38fd1498Szrj 
316*38fd1498Szrj void
add_phi_node_to_bb(gphi * phi,basic_block bb)317*38fd1498Szrj add_phi_node_to_bb (gphi *phi, basic_block bb)
318*38fd1498Szrj {
319*38fd1498Szrj   gimple_seq seq = phi_nodes (bb);
320*38fd1498Szrj   /* Add the new PHI node to the list of PHI nodes for block BB.  */
321*38fd1498Szrj   if (seq == NULL)
322*38fd1498Szrj     set_phi_nodes (bb, gimple_seq_alloc_with_stmt (phi));
323*38fd1498Szrj   else
324*38fd1498Szrj     {
325*38fd1498Szrj       gimple_seq_add_stmt (&seq, phi);
326*38fd1498Szrj       gcc_assert (seq == phi_nodes (bb));
327*38fd1498Szrj     }
328*38fd1498Szrj 
329*38fd1498Szrj   /* Associate BB to the PHI node.  */
330*38fd1498Szrj   gimple_set_bb (phi, bb);
331*38fd1498Szrj 
332*38fd1498Szrj }
333*38fd1498Szrj 
334*38fd1498Szrj /* Create a new PHI node for variable VAR at basic block BB.  */
335*38fd1498Szrj 
336*38fd1498Szrj gphi *
create_phi_node(tree var,basic_block bb)337*38fd1498Szrj create_phi_node (tree var, basic_block bb)
338*38fd1498Szrj {
339*38fd1498Szrj   gphi *phi = make_phi_node (var, EDGE_COUNT (bb->preds));
340*38fd1498Szrj 
341*38fd1498Szrj   add_phi_node_to_bb (phi, bb);
342*38fd1498Szrj   return phi;
343*38fd1498Szrj }
344*38fd1498Szrj 
345*38fd1498Szrj 
346*38fd1498Szrj /* Add a new argument to PHI node PHI.  DEF is the incoming reaching
347*38fd1498Szrj    definition and E is the edge through which DEF reaches PHI.  The new
348*38fd1498Szrj    argument is added at the end of the argument list.
349*38fd1498Szrj    If PHI has reached its maximum capacity, add a few slots.  In this case,
350*38fd1498Szrj    PHI points to the reallocated phi node when we return.  */
351*38fd1498Szrj 
352*38fd1498Szrj void
add_phi_arg(gphi * phi,tree def,edge e,source_location locus)353*38fd1498Szrj add_phi_arg (gphi *phi, tree def, edge e, source_location locus)
354*38fd1498Szrj {
355*38fd1498Szrj   basic_block bb = e->dest;
356*38fd1498Szrj 
357*38fd1498Szrj   gcc_assert (bb == gimple_bb (phi));
358*38fd1498Szrj 
359*38fd1498Szrj   /* We resize PHI nodes upon edge creation.  We should always have
360*38fd1498Szrj      enough room at this point.  */
361*38fd1498Szrj   gcc_assert (gimple_phi_num_args (phi) <= gimple_phi_capacity (phi));
362*38fd1498Szrj 
363*38fd1498Szrj   /* We resize PHI nodes upon edge creation.  We should always have
364*38fd1498Szrj      enough room at this point.  */
365*38fd1498Szrj   gcc_assert (e->dest_idx < gimple_phi_num_args (phi));
366*38fd1498Szrj 
367*38fd1498Szrj   /* Copy propagation needs to know what object occur in abnormal
368*38fd1498Szrj      PHI nodes.  This is a convenient place to record such information.  */
369*38fd1498Szrj   if (e->flags & EDGE_ABNORMAL)
370*38fd1498Szrj     {
371*38fd1498Szrj       SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def) = 1;
372*38fd1498Szrj       SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)) = 1;
373*38fd1498Szrj     }
374*38fd1498Szrj 
375*38fd1498Szrj   SET_PHI_ARG_DEF (phi, e->dest_idx, def);
376*38fd1498Szrj   gimple_phi_arg_set_location (phi, e->dest_idx, locus);
377*38fd1498Szrj }
378*38fd1498Szrj 
379*38fd1498Szrj 
380*38fd1498Szrj /* Remove the Ith argument from PHI's argument list.  This routine
381*38fd1498Szrj    implements removal by swapping the last alternative with the
382*38fd1498Szrj    alternative we want to delete and then shrinking the vector, which
383*38fd1498Szrj    is consistent with how we remove an edge from the edge vector.  */
384*38fd1498Szrj 
385*38fd1498Szrj static void
remove_phi_arg_num(gphi * phi,int i)386*38fd1498Szrj remove_phi_arg_num (gphi *phi, int i)
387*38fd1498Szrj {
388*38fd1498Szrj   int num_elem = gimple_phi_num_args (phi);
389*38fd1498Szrj 
390*38fd1498Szrj   gcc_assert (i < num_elem);
391*38fd1498Szrj 
392*38fd1498Szrj   /* Delink the item which is being removed.  */
393*38fd1498Szrj   delink_imm_use (gimple_phi_arg_imm_use_ptr (phi, i));
394*38fd1498Szrj 
395*38fd1498Szrj   /* If it is not the last element, move the last element
396*38fd1498Szrj      to the element we want to delete, resetting all the links. */
397*38fd1498Szrj   if (i != num_elem - 1)
398*38fd1498Szrj     {
399*38fd1498Szrj       use_operand_p old_p, new_p;
400*38fd1498Szrj       old_p = gimple_phi_arg_imm_use_ptr (phi, num_elem - 1);
401*38fd1498Szrj       new_p = gimple_phi_arg_imm_use_ptr (phi, i);
402*38fd1498Szrj       /* Set use on new node, and link into last element's place.  */
403*38fd1498Szrj       *(new_p->use) = *(old_p->use);
404*38fd1498Szrj       relink_imm_use (new_p, old_p);
405*38fd1498Szrj       /* Move the location as well.  */
406*38fd1498Szrj       gimple_phi_arg_set_location (phi, i,
407*38fd1498Szrj 				   gimple_phi_arg_location (phi, num_elem - 1));
408*38fd1498Szrj     }
409*38fd1498Szrj 
410*38fd1498Szrj   /* Shrink the vector and return.  Note that we do not have to clear
411*38fd1498Szrj      PHI_ARG_DEF because the garbage collector will not look at those
412*38fd1498Szrj      elements beyond the first PHI_NUM_ARGS elements of the array.  */
413*38fd1498Szrj   phi->nargs--;
414*38fd1498Szrj }
415*38fd1498Szrj 
416*38fd1498Szrj 
417*38fd1498Szrj /* Remove all PHI arguments associated with edge E.  */
418*38fd1498Szrj 
419*38fd1498Szrj void
remove_phi_args(edge e)420*38fd1498Szrj remove_phi_args (edge e)
421*38fd1498Szrj {
422*38fd1498Szrj   gphi_iterator gsi;
423*38fd1498Szrj 
424*38fd1498Szrj   for (gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
425*38fd1498Szrj     remove_phi_arg_num (gsi.phi (),
426*38fd1498Szrj 			e->dest_idx);
427*38fd1498Szrj }
428*38fd1498Szrj 
429*38fd1498Szrj 
430*38fd1498Szrj /* Remove the PHI node pointed-to by iterator GSI from basic block BB.  After
431*38fd1498Szrj    removal, iterator GSI is updated to point to the next PHI node in the
432*38fd1498Szrj    sequence. If RELEASE_LHS_P is true, the LHS of this PHI node is released
433*38fd1498Szrj    into the free pool of SSA names.  */
434*38fd1498Szrj 
435*38fd1498Szrj void
remove_phi_node(gimple_stmt_iterator * gsi,bool release_lhs_p)436*38fd1498Szrj remove_phi_node (gimple_stmt_iterator *gsi, bool release_lhs_p)
437*38fd1498Szrj {
438*38fd1498Szrj   gimple *phi = gsi_stmt (*gsi);
439*38fd1498Szrj 
440*38fd1498Szrj   if (release_lhs_p)
441*38fd1498Szrj     insert_debug_temps_for_defs (gsi);
442*38fd1498Szrj 
443*38fd1498Szrj   gsi_remove (gsi, false);
444*38fd1498Szrj 
445*38fd1498Szrj   /* If we are deleting the PHI node, then we should release the
446*38fd1498Szrj      SSA_NAME node so that it can be reused.  */
447*38fd1498Szrj   release_phi_node (phi);
448*38fd1498Szrj   if (release_lhs_p)
449*38fd1498Szrj     release_ssa_name (gimple_phi_result (phi));
450*38fd1498Szrj }
451*38fd1498Szrj 
452*38fd1498Szrj /* Remove all the phi nodes from BB.  */
453*38fd1498Szrj 
454*38fd1498Szrj void
remove_phi_nodes(basic_block bb)455*38fd1498Szrj remove_phi_nodes (basic_block bb)
456*38fd1498Szrj {
457*38fd1498Szrj   gphi_iterator gsi;
458*38fd1498Szrj 
459*38fd1498Szrj   for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); )
460*38fd1498Szrj     remove_phi_node (&gsi, true);
461*38fd1498Szrj 
462*38fd1498Szrj   set_phi_nodes (bb, NULL);
463*38fd1498Szrj }
464*38fd1498Szrj 
465*38fd1498Szrj /* Given PHI, return its RHS if the PHI is a degenerate, otherwise return
466*38fd1498Szrj    NULL.  */
467*38fd1498Szrj 
468*38fd1498Szrj tree
degenerate_phi_result(gphi * phi)469*38fd1498Szrj degenerate_phi_result (gphi *phi)
470*38fd1498Szrj {
471*38fd1498Szrj   tree lhs = gimple_phi_result (phi);
472*38fd1498Szrj   tree val = NULL;
473*38fd1498Szrj   size_t i;
474*38fd1498Szrj 
475*38fd1498Szrj   /* Ignoring arguments which are the same as LHS, if all the remaining
476*38fd1498Szrj      arguments are the same, then the PHI is a degenerate and has the
477*38fd1498Szrj      value of that common argument.  */
478*38fd1498Szrj   for (i = 0; i < gimple_phi_num_args (phi); i++)
479*38fd1498Szrj     {
480*38fd1498Szrj       tree arg = gimple_phi_arg_def (phi, i);
481*38fd1498Szrj 
482*38fd1498Szrj       if (arg == lhs)
483*38fd1498Szrj 	continue;
484*38fd1498Szrj       else if (!arg)
485*38fd1498Szrj 	break;
486*38fd1498Szrj       else if (!val)
487*38fd1498Szrj 	val = arg;
488*38fd1498Szrj       else if (arg == val)
489*38fd1498Szrj 	continue;
490*38fd1498Szrj       /* We bring in some of operand_equal_p not only to speed things
491*38fd1498Szrj 	 up, but also to avoid crashing when dereferencing the type of
492*38fd1498Szrj 	 a released SSA name.  */
493*38fd1498Szrj       else if (TREE_CODE (val) != TREE_CODE (arg)
494*38fd1498Szrj 	       || TREE_CODE (val) == SSA_NAME
495*38fd1498Szrj 	       || !operand_equal_p (arg, val, 0))
496*38fd1498Szrj 	break;
497*38fd1498Szrj     }
498*38fd1498Szrj   return (i == gimple_phi_num_args (phi) ? val : NULL);
499*38fd1498Szrj }
500*38fd1498Szrj 
501*38fd1498Szrj /* Set PHI nodes of a basic block BB to SEQ.  */
502*38fd1498Szrj 
503*38fd1498Szrj void
set_phi_nodes(basic_block bb,gimple_seq seq)504*38fd1498Szrj set_phi_nodes (basic_block bb, gimple_seq seq)
505*38fd1498Szrj {
506*38fd1498Szrj   gimple_stmt_iterator i;
507*38fd1498Szrj 
508*38fd1498Szrj   gcc_checking_assert (!(bb->flags & BB_RTL));
509*38fd1498Szrj   bb->il.gimple.phi_nodes = seq;
510*38fd1498Szrj   if (seq)
511*38fd1498Szrj     for (i = gsi_start (seq); !gsi_end_p (i); gsi_next (&i))
512*38fd1498Szrj       gimple_set_bb (gsi_stmt (i), bb);
513*38fd1498Szrj }
514*38fd1498Szrj 
515*38fd1498Szrj #include "gt-tree-phinodes.h"
516