xref: /dflybsd-src/contrib/gcc-8.0/gcc/cprop.c (revision 38fd149817dfbff97799f62fcb70be98c4e32523)
1*38fd1498Szrj /* Global constant/copy propagation for RTL.
2*38fd1498Szrj    Copyright (C) 1997-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 it under
7*38fd1498Szrj the terms of the GNU General Public License as published by the Free
8*38fd1498Szrj Software Foundation; either version 3, or (at your option) any later
9*38fd1498Szrj version.
10*38fd1498Szrj 
11*38fd1498Szrj GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12*38fd1498Szrj WARRANTY; without even the implied warranty of MERCHANTABILITY or
13*38fd1498Szrj FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14*38fd1498Szrj 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 "rtl.h"
25*38fd1498Szrj #include "cfghooks.h"
26*38fd1498Szrj #include "df.h"
27*38fd1498Szrj #include "insn-config.h"
28*38fd1498Szrj #include "memmodel.h"
29*38fd1498Szrj #include "emit-rtl.h"
30*38fd1498Szrj #include "recog.h"
31*38fd1498Szrj #include "diagnostic-core.h"
32*38fd1498Szrj #include "toplev.h"
33*38fd1498Szrj #include "cfgrtl.h"
34*38fd1498Szrj #include "cfganal.h"
35*38fd1498Szrj #include "lcm.h"
36*38fd1498Szrj #include "cfgcleanup.h"
37*38fd1498Szrj #include "params.h"
38*38fd1498Szrj #include "cselib.h"
39*38fd1498Szrj #include "intl.h"
40*38fd1498Szrj #include "tree-pass.h"
41*38fd1498Szrj #include "dbgcnt.h"
42*38fd1498Szrj #include "cfgloop.h"
43*38fd1498Szrj #include "gcse.h"
44*38fd1498Szrj 
45*38fd1498Szrj 
46*38fd1498Szrj /* An obstack for our working variables.  */
47*38fd1498Szrj static struct obstack cprop_obstack;
48*38fd1498Szrj 
49*38fd1498Szrj /* Occurrence of an expression.
50*38fd1498Szrj    There is one per basic block.  If a pattern appears more than once the
51*38fd1498Szrj    last appearance is used.  */
52*38fd1498Szrj 
53*38fd1498Szrj struct cprop_occr
54*38fd1498Szrj {
55*38fd1498Szrj   /* Next occurrence of this expression.  */
56*38fd1498Szrj   struct cprop_occr *next;
57*38fd1498Szrj   /* The insn that computes the expression.  */
58*38fd1498Szrj   rtx_insn *insn;
59*38fd1498Szrj };
60*38fd1498Szrj 
61*38fd1498Szrj /* Hash table entry for assignment expressions.  */
62*38fd1498Szrj 
63*38fd1498Szrj struct cprop_expr
64*38fd1498Szrj {
65*38fd1498Szrj   /* The expression (DEST := SRC).  */
66*38fd1498Szrj   rtx dest;
67*38fd1498Szrj   rtx src;
68*38fd1498Szrj 
69*38fd1498Szrj   /* Index in the available expression bitmaps.  */
70*38fd1498Szrj   int bitmap_index;
71*38fd1498Szrj   /* Next entry with the same hash.  */
72*38fd1498Szrj   struct cprop_expr *next_same_hash;
73*38fd1498Szrj   /* List of available occurrence in basic blocks in the function.
74*38fd1498Szrj      An "available occurrence" is one that is the last occurrence in the
75*38fd1498Szrj      basic block and whose operands are not modified by following statements
76*38fd1498Szrj      in the basic block [including this insn].  */
77*38fd1498Szrj   struct cprop_occr *avail_occr;
78*38fd1498Szrj };
79*38fd1498Szrj 
80*38fd1498Szrj /* Hash table for copy propagation expressions.
81*38fd1498Szrj    Each hash table is an array of buckets.
82*38fd1498Szrj    ??? It is known that if it were an array of entries, structure elements
83*38fd1498Szrj    `next_same_hash' and `bitmap_index' wouldn't be necessary.  However, it is
84*38fd1498Szrj    not clear whether in the final analysis a sufficient amount of memory would
85*38fd1498Szrj    be saved as the size of the available expression bitmaps would be larger
86*38fd1498Szrj    [one could build a mapping table without holes afterwards though].
87*38fd1498Szrj    Someday I'll perform the computation and figure it out.  */
88*38fd1498Szrj 
89*38fd1498Szrj struct hash_table_d
90*38fd1498Szrj {
91*38fd1498Szrj   /* The table itself.
92*38fd1498Szrj      This is an array of `set_hash_table_size' elements.  */
93*38fd1498Szrj   struct cprop_expr **table;
94*38fd1498Szrj 
95*38fd1498Szrj   /* Size of the hash table, in elements.  */
96*38fd1498Szrj   unsigned int size;
97*38fd1498Szrj 
98*38fd1498Szrj   /* Number of hash table elements.  */
99*38fd1498Szrj   unsigned int n_elems;
100*38fd1498Szrj };
101*38fd1498Szrj 
102*38fd1498Szrj /* Copy propagation hash table.  */
103*38fd1498Szrj static struct hash_table_d set_hash_table;
104*38fd1498Szrj 
105*38fd1498Szrj /* Array of implicit set patterns indexed by basic block index.  */
106*38fd1498Szrj static rtx *implicit_sets;
107*38fd1498Szrj 
108*38fd1498Szrj /* Array of indexes of expressions for implicit set patterns indexed by basic
109*38fd1498Szrj    block index.  In other words, implicit_set_indexes[i] is the bitmap_index
110*38fd1498Szrj    of the expression whose RTX is implicit_sets[i].  */
111*38fd1498Szrj static int *implicit_set_indexes;
112*38fd1498Szrj 
113*38fd1498Szrj /* Bitmap containing one bit for each register in the program.
114*38fd1498Szrj    Used when performing GCSE to track which registers have been set since
115*38fd1498Szrj    the start or end of the basic block while traversing that block.  */
116*38fd1498Szrj static regset reg_set_bitmap;
117*38fd1498Szrj 
118*38fd1498Szrj /* Various variables for statistics gathering.  */
119*38fd1498Szrj 
120*38fd1498Szrj /* Memory used in a pass.
121*38fd1498Szrj    This isn't intended to be absolutely precise.  Its intent is only
122*38fd1498Szrj    to keep an eye on memory usage.  */
123*38fd1498Szrj static int bytes_used;
124*38fd1498Szrj 
125*38fd1498Szrj /* Number of local constants propagated.  */
126*38fd1498Szrj static int local_const_prop_count;
127*38fd1498Szrj /* Number of local copies propagated.  */
128*38fd1498Szrj static int local_copy_prop_count;
129*38fd1498Szrj /* Number of global constants propagated.  */
130*38fd1498Szrj static int global_const_prop_count;
131*38fd1498Szrj /* Number of global copies propagated.  */
132*38fd1498Szrj static int global_copy_prop_count;
133*38fd1498Szrj 
134*38fd1498Szrj #define GOBNEW(T)		((T *) cprop_alloc (sizeof (T)))
135*38fd1498Szrj #define GOBNEWVAR(T, S)		((T *) cprop_alloc ((S)))
136*38fd1498Szrj 
137*38fd1498Szrj /* Cover function to obstack_alloc.  */
138*38fd1498Szrj 
139*38fd1498Szrj static void *
cprop_alloc(unsigned long size)140*38fd1498Szrj cprop_alloc (unsigned long size)
141*38fd1498Szrj {
142*38fd1498Szrj   bytes_used += size;
143*38fd1498Szrj   return obstack_alloc (&cprop_obstack, size);
144*38fd1498Szrj }
145*38fd1498Szrj 
146*38fd1498Szrj /* Return nonzero if register X is unchanged from INSN to the end
147*38fd1498Szrj    of INSN's basic block.  */
148*38fd1498Szrj 
149*38fd1498Szrj static int
reg_available_p(const_rtx x,const rtx_insn * insn ATTRIBUTE_UNUSED)150*38fd1498Szrj reg_available_p (const_rtx x, const rtx_insn *insn ATTRIBUTE_UNUSED)
151*38fd1498Szrj {
152*38fd1498Szrj   return ! REGNO_REG_SET_P (reg_set_bitmap, REGNO (x));
153*38fd1498Szrj }
154*38fd1498Szrj 
155*38fd1498Szrj /* Hash a set of register REGNO.
156*38fd1498Szrj 
157*38fd1498Szrj    Sets are hashed on the register that is set.  This simplifies the PRE copy
158*38fd1498Szrj    propagation code.
159*38fd1498Szrj 
160*38fd1498Szrj    ??? May need to make things more elaborate.  Later, as necessary.  */
161*38fd1498Szrj 
162*38fd1498Szrj static unsigned int
hash_mod(int regno,int hash_table_size)163*38fd1498Szrj hash_mod (int regno, int hash_table_size)
164*38fd1498Szrj {
165*38fd1498Szrj   return (unsigned) regno % hash_table_size;
166*38fd1498Szrj }
167*38fd1498Szrj 
168*38fd1498Szrj /* Insert assignment DEST:=SET from INSN in the hash table.
169*38fd1498Szrj    DEST is a register and SET is a register or a suitable constant.
170*38fd1498Szrj    If the assignment is already present in the table, record it as
171*38fd1498Szrj    the last occurrence in INSN's basic block.
172*38fd1498Szrj    IMPLICIT is true if it's an implicit set, false otherwise.  */
173*38fd1498Szrj 
174*38fd1498Szrj static void
insert_set_in_table(rtx dest,rtx src,rtx_insn * insn,struct hash_table_d * table,bool implicit)175*38fd1498Szrj insert_set_in_table (rtx dest, rtx src, rtx_insn *insn,
176*38fd1498Szrj 		     struct hash_table_d *table, bool implicit)
177*38fd1498Szrj {
178*38fd1498Szrj   bool found = false;
179*38fd1498Szrj   unsigned int hash;
180*38fd1498Szrj   struct cprop_expr *cur_expr, *last_expr = NULL;
181*38fd1498Szrj   struct cprop_occr *cur_occr;
182*38fd1498Szrj 
183*38fd1498Szrj   hash = hash_mod (REGNO (dest), table->size);
184*38fd1498Szrj 
185*38fd1498Szrj   for (cur_expr = table->table[hash]; cur_expr;
186*38fd1498Szrj        cur_expr = cur_expr->next_same_hash)
187*38fd1498Szrj     {
188*38fd1498Szrj       if (dest == cur_expr->dest
189*38fd1498Szrj 	  && src == cur_expr->src)
190*38fd1498Szrj 	{
191*38fd1498Szrj 	  found = true;
192*38fd1498Szrj 	  break;
193*38fd1498Szrj 	}
194*38fd1498Szrj       last_expr = cur_expr;
195*38fd1498Szrj     }
196*38fd1498Szrj 
197*38fd1498Szrj   if (! found)
198*38fd1498Szrj     {
199*38fd1498Szrj       cur_expr = GOBNEW (struct cprop_expr);
200*38fd1498Szrj       bytes_used += sizeof (struct cprop_expr);
201*38fd1498Szrj       if (table->table[hash] == NULL)
202*38fd1498Szrj 	/* This is the first pattern that hashed to this index.  */
203*38fd1498Szrj 	table->table[hash] = cur_expr;
204*38fd1498Szrj       else
205*38fd1498Szrj 	/* Add EXPR to end of this hash chain.  */
206*38fd1498Szrj 	last_expr->next_same_hash = cur_expr;
207*38fd1498Szrj 
208*38fd1498Szrj       /* Set the fields of the expr element.
209*38fd1498Szrj 	 We must copy X because it can be modified when copy propagation is
210*38fd1498Szrj 	 performed on its operands.  */
211*38fd1498Szrj       cur_expr->dest = copy_rtx (dest);
212*38fd1498Szrj       cur_expr->src = copy_rtx (src);
213*38fd1498Szrj       cur_expr->bitmap_index = table->n_elems++;
214*38fd1498Szrj       cur_expr->next_same_hash = NULL;
215*38fd1498Szrj       cur_expr->avail_occr = NULL;
216*38fd1498Szrj     }
217*38fd1498Szrj 
218*38fd1498Szrj   /* Now record the occurrence.  */
219*38fd1498Szrj   cur_occr = cur_expr->avail_occr;
220*38fd1498Szrj 
221*38fd1498Szrj   if (cur_occr
222*38fd1498Szrj       && BLOCK_FOR_INSN (cur_occr->insn) == BLOCK_FOR_INSN (insn))
223*38fd1498Szrj     {
224*38fd1498Szrj       /* Found another instance of the expression in the same basic block.
225*38fd1498Szrj 	 Prefer this occurrence to the currently recorded one.  We want
226*38fd1498Szrj 	 the last one in the block and the block is scanned from start
227*38fd1498Szrj 	 to end.  */
228*38fd1498Szrj       cur_occr->insn = insn;
229*38fd1498Szrj     }
230*38fd1498Szrj   else
231*38fd1498Szrj     {
232*38fd1498Szrj       /* First occurrence of this expression in this basic block.  */
233*38fd1498Szrj       cur_occr = GOBNEW (struct cprop_occr);
234*38fd1498Szrj       bytes_used += sizeof (struct cprop_occr);
235*38fd1498Szrj       cur_occr->insn = insn;
236*38fd1498Szrj       cur_occr->next = cur_expr->avail_occr;
237*38fd1498Szrj       cur_expr->avail_occr = cur_occr;
238*38fd1498Szrj     }
239*38fd1498Szrj 
240*38fd1498Szrj   /* Record bitmap_index of the implicit set in implicit_set_indexes.  */
241*38fd1498Szrj   if (implicit)
242*38fd1498Szrj     implicit_set_indexes[BLOCK_FOR_INSN (insn)->index]
243*38fd1498Szrj       = cur_expr->bitmap_index;
244*38fd1498Szrj }
245*38fd1498Szrj 
246*38fd1498Szrj /* Determine whether the rtx X should be treated as a constant for CPROP.
247*38fd1498Szrj    Since X might be inserted more than once we have to take care that it
248*38fd1498Szrj    is sharable.  */
249*38fd1498Szrj 
250*38fd1498Szrj static bool
cprop_constant_p(const_rtx x)251*38fd1498Szrj cprop_constant_p (const_rtx x)
252*38fd1498Szrj {
253*38fd1498Szrj   return CONSTANT_P (x) && (GET_CODE (x) != CONST || shared_const_p (x));
254*38fd1498Szrj }
255*38fd1498Szrj 
256*38fd1498Szrj /* Determine whether the rtx X should be treated as a register that can
257*38fd1498Szrj    be propagated.  Any pseudo-register is fine.  */
258*38fd1498Szrj 
259*38fd1498Szrj static bool
cprop_reg_p(const_rtx x)260*38fd1498Szrj cprop_reg_p (const_rtx x)
261*38fd1498Szrj {
262*38fd1498Szrj   return REG_P (x) && !HARD_REGISTER_P (x);
263*38fd1498Szrj }
264*38fd1498Szrj 
265*38fd1498Szrj /* Scan SET present in INSN and add an entry to the hash TABLE.
266*38fd1498Szrj    IMPLICIT is true if it's an implicit set, false otherwise.  */
267*38fd1498Szrj 
268*38fd1498Szrj static void
hash_scan_set(rtx set,rtx_insn * insn,struct hash_table_d * table,bool implicit)269*38fd1498Szrj hash_scan_set (rtx set, rtx_insn *insn, struct hash_table_d *table,
270*38fd1498Szrj 	       bool implicit)
271*38fd1498Szrj {
272*38fd1498Szrj   rtx src = SET_SRC (set);
273*38fd1498Szrj   rtx dest = SET_DEST (set);
274*38fd1498Szrj 
275*38fd1498Szrj   if (cprop_reg_p (dest)
276*38fd1498Szrj       && reg_available_p (dest, insn)
277*38fd1498Szrj       && can_copy_p (GET_MODE (dest)))
278*38fd1498Szrj     {
279*38fd1498Szrj       /* See if a REG_EQUAL note shows this equivalent to a simpler expression.
280*38fd1498Szrj 
281*38fd1498Szrj 	 This allows us to do a single CPROP pass and still eliminate
282*38fd1498Szrj 	 redundant constants, addresses or other expressions that are
283*38fd1498Szrj 	 constructed with multiple instructions.
284*38fd1498Szrj 
285*38fd1498Szrj 	 However, keep the original SRC if INSN is a simple reg-reg move.  In
286*38fd1498Szrj 	 In this case, there will almost always be a REG_EQUAL note on the
287*38fd1498Szrj 	 insn that sets SRC.  By recording the REG_EQUAL value here as SRC
288*38fd1498Szrj 	 for INSN, we miss copy propagation opportunities.
289*38fd1498Szrj 
290*38fd1498Szrj 	 Note that this does not impede profitable constant propagations.  We
291*38fd1498Szrj 	 "look through" reg-reg sets in lookup_set.  */
292*38fd1498Szrj       rtx note = find_reg_equal_equiv_note (insn);
293*38fd1498Szrj       if (note != 0
294*38fd1498Szrj 	  && REG_NOTE_KIND (note) == REG_EQUAL
295*38fd1498Szrj 	  && !REG_P (src)
296*38fd1498Szrj 	  && cprop_constant_p (XEXP (note, 0)))
297*38fd1498Szrj 	src = XEXP (note, 0), set = gen_rtx_SET (dest, src);
298*38fd1498Szrj 
299*38fd1498Szrj       /* Record sets for constant/copy propagation.  */
300*38fd1498Szrj       if ((cprop_reg_p (src)
301*38fd1498Szrj 	   && src != dest
302*38fd1498Szrj 	   && reg_available_p (src, insn))
303*38fd1498Szrj 	  || cprop_constant_p (src))
304*38fd1498Szrj 	insert_set_in_table (dest, src, insn, table, implicit);
305*38fd1498Szrj     }
306*38fd1498Szrj }
307*38fd1498Szrj 
308*38fd1498Szrj /* Process INSN and add hash table entries as appropriate.  */
309*38fd1498Szrj 
310*38fd1498Szrj static void
hash_scan_insn(rtx_insn * insn,struct hash_table_d * table)311*38fd1498Szrj hash_scan_insn (rtx_insn *insn, struct hash_table_d *table)
312*38fd1498Szrj {
313*38fd1498Szrj   rtx pat = PATTERN (insn);
314*38fd1498Szrj   int i;
315*38fd1498Szrj 
316*38fd1498Szrj   /* Pick out the sets of INSN and for other forms of instructions record
317*38fd1498Szrj      what's been modified.  */
318*38fd1498Szrj 
319*38fd1498Szrj   if (GET_CODE (pat) == SET)
320*38fd1498Szrj     hash_scan_set (pat, insn, table, false);
321*38fd1498Szrj   else if (GET_CODE (pat) == PARALLEL)
322*38fd1498Szrj     for (i = 0; i < XVECLEN (pat, 0); i++)
323*38fd1498Szrj       {
324*38fd1498Szrj 	rtx x = XVECEXP (pat, 0, i);
325*38fd1498Szrj 
326*38fd1498Szrj 	if (GET_CODE (x) == SET)
327*38fd1498Szrj 	  hash_scan_set (x, insn, table, false);
328*38fd1498Szrj       }
329*38fd1498Szrj }
330*38fd1498Szrj 
331*38fd1498Szrj /* Dump the hash table TABLE to file FILE under the name NAME.  */
332*38fd1498Szrj 
333*38fd1498Szrj static void
dump_hash_table(FILE * file,const char * name,struct hash_table_d * table)334*38fd1498Szrj dump_hash_table (FILE *file, const char *name, struct hash_table_d *table)
335*38fd1498Szrj {
336*38fd1498Szrj   int i;
337*38fd1498Szrj   /* Flattened out table, so it's printed in proper order.  */
338*38fd1498Szrj   struct cprop_expr **flat_table;
339*38fd1498Szrj   unsigned int *hash_val;
340*38fd1498Szrj   struct cprop_expr *expr;
341*38fd1498Szrj 
342*38fd1498Szrj   flat_table = XCNEWVEC (struct cprop_expr *, table->n_elems);
343*38fd1498Szrj   hash_val = XNEWVEC (unsigned int, table->n_elems);
344*38fd1498Szrj 
345*38fd1498Szrj   for (i = 0; i < (int) table->size; i++)
346*38fd1498Szrj     for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
347*38fd1498Szrj       {
348*38fd1498Szrj 	flat_table[expr->bitmap_index] = expr;
349*38fd1498Szrj 	hash_val[expr->bitmap_index] = i;
350*38fd1498Szrj       }
351*38fd1498Szrj 
352*38fd1498Szrj   fprintf (file, "%s hash table (%d buckets, %d entries)\n",
353*38fd1498Szrj 	   name, table->size, table->n_elems);
354*38fd1498Szrj 
355*38fd1498Szrj   for (i = 0; i < (int) table->n_elems; i++)
356*38fd1498Szrj     if (flat_table[i] != 0)
357*38fd1498Szrj       {
358*38fd1498Szrj 	expr = flat_table[i];
359*38fd1498Szrj 	fprintf (file, "Index %d (hash value %d)\n  ",
360*38fd1498Szrj 		 expr->bitmap_index, hash_val[i]);
361*38fd1498Szrj 	print_rtl (file, expr->dest);
362*38fd1498Szrj 	fprintf (file, " := ");
363*38fd1498Szrj 	print_rtl (file, expr->src);
364*38fd1498Szrj 	fprintf (file, "\n");
365*38fd1498Szrj       }
366*38fd1498Szrj 
367*38fd1498Szrj   fprintf (file, "\n");
368*38fd1498Szrj 
369*38fd1498Szrj   free (flat_table);
370*38fd1498Szrj   free (hash_val);
371*38fd1498Szrj }
372*38fd1498Szrj 
373*38fd1498Szrj /* Record as unavailable all registers that are DEF operands of INSN.  */
374*38fd1498Szrj 
375*38fd1498Szrj static void
make_set_regs_unavailable(rtx_insn * insn)376*38fd1498Szrj make_set_regs_unavailable (rtx_insn *insn)
377*38fd1498Szrj {
378*38fd1498Szrj   df_ref def;
379*38fd1498Szrj 
380*38fd1498Szrj   FOR_EACH_INSN_DEF (def, insn)
381*38fd1498Szrj     SET_REGNO_REG_SET (reg_set_bitmap, DF_REF_REGNO (def));
382*38fd1498Szrj }
383*38fd1498Szrj 
384*38fd1498Szrj /* Top level function to create an assignment hash table.
385*38fd1498Szrj 
386*38fd1498Szrj    Assignment entries are placed in the hash table if
387*38fd1498Szrj    - they are of the form (set (pseudo-reg) src),
388*38fd1498Szrj    - src is something we want to perform const/copy propagation on,
389*38fd1498Szrj    - none of the operands or target are subsequently modified in the block
390*38fd1498Szrj 
391*38fd1498Szrj    Currently src must be a pseudo-reg or a const_int.
392*38fd1498Szrj 
393*38fd1498Szrj    TABLE is the table computed.  */
394*38fd1498Szrj 
395*38fd1498Szrj static void
compute_hash_table_work(struct hash_table_d * table)396*38fd1498Szrj compute_hash_table_work (struct hash_table_d *table)
397*38fd1498Szrj {
398*38fd1498Szrj   basic_block bb;
399*38fd1498Szrj 
400*38fd1498Szrj   /* Allocate vars to track sets of regs.  */
401*38fd1498Szrj   reg_set_bitmap = ALLOC_REG_SET (NULL);
402*38fd1498Szrj 
403*38fd1498Szrj   FOR_EACH_BB_FN (bb, cfun)
404*38fd1498Szrj     {
405*38fd1498Szrj       rtx_insn *insn;
406*38fd1498Szrj 
407*38fd1498Szrj       /* Reset tables used to keep track of what's not yet invalid [since
408*38fd1498Szrj 	 the end of the block].  */
409*38fd1498Szrj       CLEAR_REG_SET (reg_set_bitmap);
410*38fd1498Szrj 
411*38fd1498Szrj       /* Go over all insns from the last to the first.  This is convenient
412*38fd1498Szrj 	 for tracking available registers, i.e. not set between INSN and
413*38fd1498Szrj 	 the end of the basic block BB.  */
414*38fd1498Szrj       FOR_BB_INSNS_REVERSE (bb, insn)
415*38fd1498Szrj         {
416*38fd1498Szrj 	  /* Only real insns are interesting.  */
417*38fd1498Szrj 	  if (!NONDEBUG_INSN_P (insn))
418*38fd1498Szrj 	    continue;
419*38fd1498Szrj 
420*38fd1498Szrj 	  /* Record interesting sets from INSN in the hash table.  */
421*38fd1498Szrj 	  hash_scan_insn (insn, table);
422*38fd1498Szrj 
423*38fd1498Szrj 	  /* Any registers set in INSN will make SETs above it not AVAIL.  */
424*38fd1498Szrj 	  make_set_regs_unavailable (insn);
425*38fd1498Szrj 	}
426*38fd1498Szrj 
427*38fd1498Szrj       /* Insert implicit sets in the hash table, pretending they appear as
428*38fd1498Szrj 	 insns at the head of the basic block.  */
429*38fd1498Szrj       if (implicit_sets[bb->index] != NULL_RTX)
430*38fd1498Szrj 	hash_scan_set (implicit_sets[bb->index], BB_HEAD (bb), table, true);
431*38fd1498Szrj     }
432*38fd1498Szrj 
433*38fd1498Szrj   FREE_REG_SET (reg_set_bitmap);
434*38fd1498Szrj }
435*38fd1498Szrj 
436*38fd1498Szrj /* Allocate space for the set/expr hash TABLE.
437*38fd1498Szrj    It is used to determine the number of buckets to use.  */
438*38fd1498Szrj 
439*38fd1498Szrj static void
alloc_hash_table(struct hash_table_d * table)440*38fd1498Szrj alloc_hash_table (struct hash_table_d *table)
441*38fd1498Szrj {
442*38fd1498Szrj   int n;
443*38fd1498Szrj 
444*38fd1498Szrj   n = get_max_insn_count ();
445*38fd1498Szrj 
446*38fd1498Szrj   table->size = n / 4;
447*38fd1498Szrj   if (table->size < 11)
448*38fd1498Szrj     table->size = 11;
449*38fd1498Szrj 
450*38fd1498Szrj   /* Attempt to maintain efficient use of hash table.
451*38fd1498Szrj      Making it an odd number is simplest for now.
452*38fd1498Szrj      ??? Later take some measurements.  */
453*38fd1498Szrj   table->size |= 1;
454*38fd1498Szrj   n = table->size * sizeof (struct cprop_expr *);
455*38fd1498Szrj   table->table = XNEWVAR (struct cprop_expr *, n);
456*38fd1498Szrj }
457*38fd1498Szrj 
458*38fd1498Szrj /* Free things allocated by alloc_hash_table.  */
459*38fd1498Szrj 
460*38fd1498Szrj static void
free_hash_table(struct hash_table_d * table)461*38fd1498Szrj free_hash_table (struct hash_table_d *table)
462*38fd1498Szrj {
463*38fd1498Szrj   free (table->table);
464*38fd1498Szrj }
465*38fd1498Szrj 
466*38fd1498Szrj /* Compute the hash TABLE for doing copy/const propagation or
467*38fd1498Szrj    expression hash table.  */
468*38fd1498Szrj 
469*38fd1498Szrj static void
compute_hash_table(struct hash_table_d * table)470*38fd1498Szrj compute_hash_table (struct hash_table_d *table)
471*38fd1498Szrj {
472*38fd1498Szrj   /* Initialize count of number of entries in hash table.  */
473*38fd1498Szrj   table->n_elems = 0;
474*38fd1498Szrj   memset (table->table, 0, table->size * sizeof (struct cprop_expr *));
475*38fd1498Szrj 
476*38fd1498Szrj   compute_hash_table_work (table);
477*38fd1498Szrj }
478*38fd1498Szrj 
479*38fd1498Szrj /* Expression tracking support.  */
480*38fd1498Szrj 
481*38fd1498Szrj /* Lookup REGNO in the set TABLE.  The result is a pointer to the
482*38fd1498Szrj    table entry, or NULL if not found.  */
483*38fd1498Szrj 
484*38fd1498Szrj static struct cprop_expr *
lookup_set(unsigned int regno,struct hash_table_d * table)485*38fd1498Szrj lookup_set (unsigned int regno, struct hash_table_d *table)
486*38fd1498Szrj {
487*38fd1498Szrj   unsigned int hash = hash_mod (regno, table->size);
488*38fd1498Szrj   struct cprop_expr *expr;
489*38fd1498Szrj 
490*38fd1498Szrj   expr = table->table[hash];
491*38fd1498Szrj 
492*38fd1498Szrj   while (expr && REGNO (expr->dest) != regno)
493*38fd1498Szrj     expr = expr->next_same_hash;
494*38fd1498Szrj 
495*38fd1498Szrj   return expr;
496*38fd1498Szrj }
497*38fd1498Szrj 
498*38fd1498Szrj /* Return the next entry for REGNO in list EXPR.  */
499*38fd1498Szrj 
500*38fd1498Szrj static struct cprop_expr *
next_set(unsigned int regno,struct cprop_expr * expr)501*38fd1498Szrj next_set (unsigned int regno, struct cprop_expr *expr)
502*38fd1498Szrj {
503*38fd1498Szrj   do
504*38fd1498Szrj     expr = expr->next_same_hash;
505*38fd1498Szrj   while (expr && REGNO (expr->dest) != regno);
506*38fd1498Szrj 
507*38fd1498Szrj   return expr;
508*38fd1498Szrj }
509*38fd1498Szrj 
510*38fd1498Szrj /* Reset tables used to keep track of what's still available [since the
511*38fd1498Szrj    start of the block].  */
512*38fd1498Szrj 
513*38fd1498Szrj static void
reset_opr_set_tables(void)514*38fd1498Szrj reset_opr_set_tables (void)
515*38fd1498Szrj {
516*38fd1498Szrj   /* Maintain a bitmap of which regs have been set since beginning of
517*38fd1498Szrj      the block.  */
518*38fd1498Szrj   CLEAR_REG_SET (reg_set_bitmap);
519*38fd1498Szrj }
520*38fd1498Szrj 
521*38fd1498Szrj /* Return nonzero if the register X has not been set yet [since the
522*38fd1498Szrj    start of the basic block containing INSN].  */
523*38fd1498Szrj 
524*38fd1498Szrj static int
reg_not_set_p(const_rtx x,const rtx_insn * insn ATTRIBUTE_UNUSED)525*38fd1498Szrj reg_not_set_p (const_rtx x, const rtx_insn *insn ATTRIBUTE_UNUSED)
526*38fd1498Szrj {
527*38fd1498Szrj   return ! REGNO_REG_SET_P (reg_set_bitmap, REGNO (x));
528*38fd1498Szrj }
529*38fd1498Szrj 
530*38fd1498Szrj /* Record things set by INSN.
531*38fd1498Szrj    This data is used by reg_not_set_p.  */
532*38fd1498Szrj 
533*38fd1498Szrj static void
mark_oprs_set(rtx_insn * insn)534*38fd1498Szrj mark_oprs_set (rtx_insn *insn)
535*38fd1498Szrj {
536*38fd1498Szrj   df_ref def;
537*38fd1498Szrj 
538*38fd1498Szrj   FOR_EACH_INSN_DEF (def, insn)
539*38fd1498Szrj     SET_REGNO_REG_SET (reg_set_bitmap, DF_REF_REGNO (def));
540*38fd1498Szrj }
541*38fd1498Szrj 
542*38fd1498Szrj /* Compute copy/constant propagation working variables.  */
543*38fd1498Szrj 
544*38fd1498Szrj /* Local properties of assignments.  */
545*38fd1498Szrj static sbitmap *cprop_avloc;
546*38fd1498Szrj static sbitmap *cprop_kill;
547*38fd1498Szrj 
548*38fd1498Szrj /* Global properties of assignments (computed from the local properties).  */
549*38fd1498Szrj static sbitmap *cprop_avin;
550*38fd1498Szrj static sbitmap *cprop_avout;
551*38fd1498Szrj 
552*38fd1498Szrj /* Allocate vars used for copy/const propagation.  N_BLOCKS is the number of
553*38fd1498Szrj    basic blocks.  N_SETS is the number of sets.  */
554*38fd1498Szrj 
555*38fd1498Szrj static void
alloc_cprop_mem(int n_blocks,int n_sets)556*38fd1498Szrj alloc_cprop_mem (int n_blocks, int n_sets)
557*38fd1498Szrj {
558*38fd1498Szrj   cprop_avloc = sbitmap_vector_alloc (n_blocks, n_sets);
559*38fd1498Szrj   cprop_kill = sbitmap_vector_alloc (n_blocks, n_sets);
560*38fd1498Szrj 
561*38fd1498Szrj   cprop_avin = sbitmap_vector_alloc (n_blocks, n_sets);
562*38fd1498Szrj   cprop_avout = sbitmap_vector_alloc (n_blocks, n_sets);
563*38fd1498Szrj }
564*38fd1498Szrj 
565*38fd1498Szrj /* Free vars used by copy/const propagation.  */
566*38fd1498Szrj 
567*38fd1498Szrj static void
free_cprop_mem(void)568*38fd1498Szrj free_cprop_mem (void)
569*38fd1498Szrj {
570*38fd1498Szrj   sbitmap_vector_free (cprop_avloc);
571*38fd1498Szrj   sbitmap_vector_free (cprop_kill);
572*38fd1498Szrj   sbitmap_vector_free (cprop_avin);
573*38fd1498Szrj   sbitmap_vector_free (cprop_avout);
574*38fd1498Szrj }
575*38fd1498Szrj 
576*38fd1498Szrj /* Compute the local properties of each recorded expression.
577*38fd1498Szrj 
578*38fd1498Szrj    Local properties are those that are defined by the block, irrespective of
579*38fd1498Szrj    other blocks.
580*38fd1498Szrj 
581*38fd1498Szrj    An expression is killed in a block if its operands, either DEST or SRC, are
582*38fd1498Szrj    modified in the block.
583*38fd1498Szrj 
584*38fd1498Szrj    An expression is computed (locally available) in a block if it is computed
585*38fd1498Szrj    at least once and expression would contain the same value if the
586*38fd1498Szrj    computation was moved to the end of the block.
587*38fd1498Szrj 
588*38fd1498Szrj    KILL and COMP are destination sbitmaps for recording local properties.  */
589*38fd1498Szrj 
590*38fd1498Szrj static void
compute_local_properties(sbitmap * kill,sbitmap * comp,struct hash_table_d * table)591*38fd1498Szrj compute_local_properties (sbitmap *kill, sbitmap *comp,
592*38fd1498Szrj 			  struct hash_table_d *table)
593*38fd1498Szrj {
594*38fd1498Szrj   unsigned int i;
595*38fd1498Szrj 
596*38fd1498Szrj   /* Initialize the bitmaps that were passed in.  */
597*38fd1498Szrj   bitmap_vector_clear (kill, last_basic_block_for_fn (cfun));
598*38fd1498Szrj   bitmap_vector_clear (comp, last_basic_block_for_fn (cfun));
599*38fd1498Szrj 
600*38fd1498Szrj   for (i = 0; i < table->size; i++)
601*38fd1498Szrj     {
602*38fd1498Szrj       struct cprop_expr *expr;
603*38fd1498Szrj 
604*38fd1498Szrj       for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
605*38fd1498Szrj 	{
606*38fd1498Szrj 	  int indx = expr->bitmap_index;
607*38fd1498Szrj 	  df_ref def;
608*38fd1498Szrj 	  struct cprop_occr *occr;
609*38fd1498Szrj 
610*38fd1498Szrj 	  /* For each definition of the destination pseudo-reg, the expression
611*38fd1498Szrj 	     is killed in the block where the definition is.  */
612*38fd1498Szrj 	  for (def = DF_REG_DEF_CHAIN (REGNO (expr->dest));
613*38fd1498Szrj 	       def; def = DF_REF_NEXT_REG (def))
614*38fd1498Szrj 	    bitmap_set_bit (kill[DF_REF_BB (def)->index], indx);
615*38fd1498Szrj 
616*38fd1498Szrj 	  /* If the source is a pseudo-reg, for each definition of the source,
617*38fd1498Szrj 	     the expression is killed in the block where the definition is.  */
618*38fd1498Szrj 	  if (REG_P (expr->src))
619*38fd1498Szrj 	    for (def = DF_REG_DEF_CHAIN (REGNO (expr->src));
620*38fd1498Szrj 		 def; def = DF_REF_NEXT_REG (def))
621*38fd1498Szrj 	      bitmap_set_bit (kill[DF_REF_BB (def)->index], indx);
622*38fd1498Szrj 
623*38fd1498Szrj 	  /* The occurrences recorded in avail_occr are exactly those that
624*38fd1498Szrj 	     are locally available in the block where they are.  */
625*38fd1498Szrj 	  for (occr = expr->avail_occr; occr != NULL; occr = occr->next)
626*38fd1498Szrj 	    {
627*38fd1498Szrj 	      bitmap_set_bit (comp[BLOCK_FOR_INSN (occr->insn)->index], indx);
628*38fd1498Szrj 	    }
629*38fd1498Szrj 	}
630*38fd1498Szrj     }
631*38fd1498Szrj }
632*38fd1498Szrj 
633*38fd1498Szrj /* Hash table support.  */
634*38fd1498Szrj 
635*38fd1498Szrj /* Top level routine to do the dataflow analysis needed by copy/const
636*38fd1498Szrj    propagation.  */
637*38fd1498Szrj 
638*38fd1498Szrj static void
compute_cprop_data(void)639*38fd1498Szrj compute_cprop_data (void)
640*38fd1498Szrj {
641*38fd1498Szrj   basic_block bb;
642*38fd1498Szrj 
643*38fd1498Szrj   compute_local_properties (cprop_kill, cprop_avloc, &set_hash_table);
644*38fd1498Szrj   compute_available (cprop_avloc, cprop_kill, cprop_avout, cprop_avin);
645*38fd1498Szrj 
646*38fd1498Szrj   /* Merge implicit sets into CPROP_AVIN.  They are always available at the
647*38fd1498Szrj      entry of their basic block.  We need to do this because 1) implicit sets
648*38fd1498Szrj      aren't recorded for the local pass so they cannot be propagated within
649*38fd1498Szrj      their basic block by this pass and 2) the global pass would otherwise
650*38fd1498Szrj      propagate them only in the successors of their basic block.  */
651*38fd1498Szrj   FOR_EACH_BB_FN (bb, cfun)
652*38fd1498Szrj     {
653*38fd1498Szrj       int index = implicit_set_indexes[bb->index];
654*38fd1498Szrj       if (index != -1)
655*38fd1498Szrj 	bitmap_set_bit (cprop_avin[bb->index], index);
656*38fd1498Szrj     }
657*38fd1498Szrj }
658*38fd1498Szrj 
659*38fd1498Szrj /* Copy/constant propagation.  */
660*38fd1498Szrj 
661*38fd1498Szrj /* Maximum number of register uses in an insn that we handle.  */
662*38fd1498Szrj #define MAX_USES 8
663*38fd1498Szrj 
664*38fd1498Szrj /* Table of uses (registers, both hard and pseudo) found in an insn.
665*38fd1498Szrj    Allocated statically to avoid alloc/free complexity and overhead.  */
666*38fd1498Szrj static rtx reg_use_table[MAX_USES];
667*38fd1498Szrj 
668*38fd1498Szrj /* Index into `reg_use_table' while building it.  */
669*38fd1498Szrj static unsigned reg_use_count;
670*38fd1498Szrj 
671*38fd1498Szrj /* Set up a list of register numbers used in INSN.  The found uses are stored
672*38fd1498Szrj    in `reg_use_table'.  `reg_use_count' is initialized to zero before entry,
673*38fd1498Szrj    and contains the number of uses in the table upon exit.
674*38fd1498Szrj 
675*38fd1498Szrj    ??? If a register appears multiple times we will record it multiple times.
676*38fd1498Szrj    This doesn't hurt anything but it will slow things down.  */
677*38fd1498Szrj 
678*38fd1498Szrj static void
find_used_regs(rtx * xptr,void * data ATTRIBUTE_UNUSED)679*38fd1498Szrj find_used_regs (rtx *xptr, void *data ATTRIBUTE_UNUSED)
680*38fd1498Szrj {
681*38fd1498Szrj   int i, j;
682*38fd1498Szrj   enum rtx_code code;
683*38fd1498Szrj   const char *fmt;
684*38fd1498Szrj   rtx x = *xptr;
685*38fd1498Szrj 
686*38fd1498Szrj   /* repeat is used to turn tail-recursion into iteration since GCC
687*38fd1498Szrj      can't do it when there's no return value.  */
688*38fd1498Szrj  repeat:
689*38fd1498Szrj   if (x == 0)
690*38fd1498Szrj     return;
691*38fd1498Szrj 
692*38fd1498Szrj   code = GET_CODE (x);
693*38fd1498Szrj   if (REG_P (x))
694*38fd1498Szrj     {
695*38fd1498Szrj       if (reg_use_count == MAX_USES)
696*38fd1498Szrj 	return;
697*38fd1498Szrj 
698*38fd1498Szrj       reg_use_table[reg_use_count] = x;
699*38fd1498Szrj       reg_use_count++;
700*38fd1498Szrj     }
701*38fd1498Szrj 
702*38fd1498Szrj   /* Recursively scan the operands of this expression.  */
703*38fd1498Szrj 
704*38fd1498Szrj   for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
705*38fd1498Szrj     {
706*38fd1498Szrj       if (fmt[i] == 'e')
707*38fd1498Szrj 	{
708*38fd1498Szrj 	  /* If we are about to do the last recursive call
709*38fd1498Szrj 	     needed at this level, change it into iteration.
710*38fd1498Szrj 	     This function is called enough to be worth it.  */
711*38fd1498Szrj 	  if (i == 0)
712*38fd1498Szrj 	    {
713*38fd1498Szrj 	      x = XEXP (x, 0);
714*38fd1498Szrj 	      goto repeat;
715*38fd1498Szrj 	    }
716*38fd1498Szrj 
717*38fd1498Szrj 	  find_used_regs (&XEXP (x, i), data);
718*38fd1498Szrj 	}
719*38fd1498Szrj       else if (fmt[i] == 'E')
720*38fd1498Szrj 	for (j = 0; j < XVECLEN (x, i); j++)
721*38fd1498Szrj 	  find_used_regs (&XVECEXP (x, i, j), data);
722*38fd1498Szrj     }
723*38fd1498Szrj }
724*38fd1498Szrj 
725*38fd1498Szrj /* Try to replace all uses of FROM in INSN with TO.
726*38fd1498Szrj    Return nonzero if successful.  */
727*38fd1498Szrj 
728*38fd1498Szrj static int
try_replace_reg(rtx from,rtx to,rtx_insn * insn)729*38fd1498Szrj try_replace_reg (rtx from, rtx to, rtx_insn *insn)
730*38fd1498Szrj {
731*38fd1498Szrj   rtx note = find_reg_equal_equiv_note (insn);
732*38fd1498Szrj   rtx src = 0;
733*38fd1498Szrj   int success = 0;
734*38fd1498Szrj   rtx set = single_set (insn);
735*38fd1498Szrj 
736*38fd1498Szrj   bool check_rtx_costs = true;
737*38fd1498Szrj   bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn));
738*38fd1498Szrj   int old_cost = set ? set_rtx_cost (set, speed) : 0;
739*38fd1498Szrj 
740*38fd1498Szrj   if (!set
741*38fd1498Szrj       || CONSTANT_P (SET_SRC (set))
742*38fd1498Szrj       || (note != 0
743*38fd1498Szrj 	  && REG_NOTE_KIND (note) == REG_EQUAL
744*38fd1498Szrj 	  && (GET_CODE (XEXP (note, 0)) == CONST
745*38fd1498Szrj 	      || CONSTANT_P (XEXP (note, 0)))))
746*38fd1498Szrj     check_rtx_costs = false;
747*38fd1498Szrj 
748*38fd1498Szrj   /* Usually we substitute easy stuff, so we won't copy everything.
749*38fd1498Szrj      We however need to take care to not duplicate non-trivial CONST
750*38fd1498Szrj      expressions.  */
751*38fd1498Szrj   to = copy_rtx (to);
752*38fd1498Szrj 
753*38fd1498Szrj   validate_replace_src_group (from, to, insn);
754*38fd1498Szrj 
755*38fd1498Szrj   /* If TO is a constant, check the cost of the set after propagation
756*38fd1498Szrj      to the cost of the set before the propagation.  If the cost is
757*38fd1498Szrj      higher, then do not replace FROM with TO.  */
758*38fd1498Szrj 
759*38fd1498Szrj   if (check_rtx_costs
760*38fd1498Szrj       && CONSTANT_P (to)
761*38fd1498Szrj       && set_rtx_cost (set, speed) > old_cost)
762*38fd1498Szrj     {
763*38fd1498Szrj       cancel_changes (0);
764*38fd1498Szrj       return false;
765*38fd1498Szrj     }
766*38fd1498Szrj 
767*38fd1498Szrj 
768*38fd1498Szrj   if (num_changes_pending () && apply_change_group ())
769*38fd1498Szrj     success = 1;
770*38fd1498Szrj 
771*38fd1498Szrj   /* Try to simplify SET_SRC if we have substituted a constant.  */
772*38fd1498Szrj   if (success && set && CONSTANT_P (to))
773*38fd1498Szrj     {
774*38fd1498Szrj       src = simplify_rtx (SET_SRC (set));
775*38fd1498Szrj 
776*38fd1498Szrj       if (src)
777*38fd1498Szrj 	validate_change (insn, &SET_SRC (set), src, 0);
778*38fd1498Szrj     }
779*38fd1498Szrj 
780*38fd1498Szrj   /* If there is already a REG_EQUAL note, update the expression in it
781*38fd1498Szrj      with our replacement.  */
782*38fd1498Szrj   if (note != 0 && REG_NOTE_KIND (note) == REG_EQUAL)
783*38fd1498Szrj     set_unique_reg_note (insn, REG_EQUAL,
784*38fd1498Szrj 			 simplify_replace_rtx (XEXP (note, 0), from, to));
785*38fd1498Szrj   if (!success && set && reg_mentioned_p (from, SET_SRC (set)))
786*38fd1498Szrj     {
787*38fd1498Szrj       /* If above failed and this is a single set, try to simplify the source
788*38fd1498Szrj 	 of the set given our substitution.  We could perhaps try this for
789*38fd1498Szrj 	 multiple SETs, but it probably won't buy us anything.  */
790*38fd1498Szrj       src = simplify_replace_rtx (SET_SRC (set), from, to);
791*38fd1498Szrj 
792*38fd1498Szrj       if (!rtx_equal_p (src, SET_SRC (set))
793*38fd1498Szrj 	  && validate_change (insn, &SET_SRC (set), src, 0))
794*38fd1498Szrj 	success = 1;
795*38fd1498Szrj 
796*38fd1498Szrj       /* If we've failed perform the replacement, have a single SET to
797*38fd1498Szrj 	 a REG destination and don't yet have a note, add a REG_EQUAL note
798*38fd1498Szrj 	 to not lose information.  */
799*38fd1498Szrj       if (!success && note == 0 && set != 0 && REG_P (SET_DEST (set)))
800*38fd1498Szrj 	note = set_unique_reg_note (insn, REG_EQUAL, copy_rtx (src));
801*38fd1498Szrj     }
802*38fd1498Szrj 
803*38fd1498Szrj   if (set && MEM_P (SET_DEST (set)) && reg_mentioned_p (from, SET_DEST (set)))
804*38fd1498Szrj     {
805*38fd1498Szrj       /* Registers can also appear as uses in SET_DEST if it is a MEM.
806*38fd1498Szrj          We could perhaps try this for multiple SETs, but it probably
807*38fd1498Szrj          won't buy us anything.  */
808*38fd1498Szrj       rtx dest = simplify_replace_rtx (SET_DEST (set), from, to);
809*38fd1498Szrj 
810*38fd1498Szrj       if (!rtx_equal_p (dest, SET_DEST (set))
811*38fd1498Szrj           && validate_change (insn, &SET_DEST (set), dest, 0))
812*38fd1498Szrj         success = 1;
813*38fd1498Szrj     }
814*38fd1498Szrj 
815*38fd1498Szrj   /* REG_EQUAL may get simplified into register.
816*38fd1498Szrj      We don't allow that. Remove that note. This code ought
817*38fd1498Szrj      not to happen, because previous code ought to synthesize
818*38fd1498Szrj      reg-reg move, but be on the safe side.  */
819*38fd1498Szrj   if (note && REG_NOTE_KIND (note) == REG_EQUAL && REG_P (XEXP (note, 0)))
820*38fd1498Szrj     remove_note (insn, note);
821*38fd1498Szrj 
822*38fd1498Szrj   return success;
823*38fd1498Szrj }
824*38fd1498Szrj 
825*38fd1498Szrj /* Find a set of REGNOs that are available on entry to INSN's block.  If found,
826*38fd1498Szrj    SET_RET[0] will be assigned a set with a register source and SET_RET[1] a
827*38fd1498Szrj    set with a constant source.  If not found the corresponding entry is set to
828*38fd1498Szrj    NULL.  */
829*38fd1498Szrj 
830*38fd1498Szrj static void
find_avail_set(int regno,rtx_insn * insn,struct cprop_expr * set_ret[2])831*38fd1498Szrj find_avail_set (int regno, rtx_insn *insn, struct cprop_expr *set_ret[2])
832*38fd1498Szrj {
833*38fd1498Szrj   set_ret[0] = set_ret[1] = NULL;
834*38fd1498Szrj 
835*38fd1498Szrj   /* Loops are not possible here.  To get a loop we would need two sets
836*38fd1498Szrj      available at the start of the block containing INSN.  i.e. we would
837*38fd1498Szrj      need two sets like this available at the start of the block:
838*38fd1498Szrj 
839*38fd1498Szrj        (set (reg X) (reg Y))
840*38fd1498Szrj        (set (reg Y) (reg X))
841*38fd1498Szrj 
842*38fd1498Szrj      This can not happen since the set of (reg Y) would have killed the
843*38fd1498Szrj      set of (reg X) making it unavailable at the start of this block.  */
844*38fd1498Szrj   while (1)
845*38fd1498Szrj     {
846*38fd1498Szrj       rtx src;
847*38fd1498Szrj       struct cprop_expr *set = lookup_set (regno, &set_hash_table);
848*38fd1498Szrj 
849*38fd1498Szrj       /* Find a set that is available at the start of the block
850*38fd1498Szrj 	 which contains INSN.  */
851*38fd1498Szrj       while (set)
852*38fd1498Szrj 	{
853*38fd1498Szrj 	  if (bitmap_bit_p (cprop_avin[BLOCK_FOR_INSN (insn)->index],
854*38fd1498Szrj 			set->bitmap_index))
855*38fd1498Szrj 	    break;
856*38fd1498Szrj 	  set = next_set (regno, set);
857*38fd1498Szrj 	}
858*38fd1498Szrj 
859*38fd1498Szrj       /* If no available set was found we've reached the end of the
860*38fd1498Szrj 	 (possibly empty) copy chain.  */
861*38fd1498Szrj       if (set == 0)
862*38fd1498Szrj 	break;
863*38fd1498Szrj 
864*38fd1498Szrj       src = set->src;
865*38fd1498Szrj 
866*38fd1498Szrj       /* We know the set is available.
867*38fd1498Szrj 	 Now check that SRC is locally anticipatable (i.e. none of the
868*38fd1498Szrj 	 source operands have changed since the start of the block).
869*38fd1498Szrj 
870*38fd1498Szrj          If the source operand changed, we may still use it for the next
871*38fd1498Szrj          iteration of this loop, but we may not use it for substitutions.  */
872*38fd1498Szrj 
873*38fd1498Szrj       if (cprop_constant_p (src))
874*38fd1498Szrj 	set_ret[1] = set;
875*38fd1498Szrj       else if (reg_not_set_p (src, insn))
876*38fd1498Szrj 	set_ret[0] = set;
877*38fd1498Szrj 
878*38fd1498Szrj       /* If the source of the set is anything except a register, then
879*38fd1498Szrj 	 we have reached the end of the copy chain.  */
880*38fd1498Szrj       if (! REG_P (src))
881*38fd1498Szrj 	break;
882*38fd1498Szrj 
883*38fd1498Szrj       /* Follow the copy chain, i.e. start another iteration of the loop
884*38fd1498Szrj 	 and see if we have an available copy into SRC.  */
885*38fd1498Szrj       regno = REGNO (src);
886*38fd1498Szrj     }
887*38fd1498Szrj }
888*38fd1498Szrj 
889*38fd1498Szrj /* Subroutine of cprop_insn that tries to propagate constants into
890*38fd1498Szrj    JUMP_INSNS.  JUMP must be a conditional jump.  If SETCC is non-NULL
891*38fd1498Szrj    it is the instruction that immediately precedes JUMP, and must be a
892*38fd1498Szrj    single SET of a register.  FROM is what we will try to replace,
893*38fd1498Szrj    SRC is the constant we will try to substitute for it.  Return nonzero
894*38fd1498Szrj    if a change was made.  */
895*38fd1498Szrj 
896*38fd1498Szrj static int
cprop_jump(basic_block bb,rtx_insn * setcc,rtx_insn * jump,rtx from,rtx src)897*38fd1498Szrj cprop_jump (basic_block bb, rtx_insn *setcc, rtx_insn *jump, rtx from, rtx src)
898*38fd1498Szrj {
899*38fd1498Szrj   rtx new_rtx, set_src, note_src;
900*38fd1498Szrj   rtx set = pc_set (jump);
901*38fd1498Szrj   rtx note = find_reg_equal_equiv_note (jump);
902*38fd1498Szrj 
903*38fd1498Szrj   if (note)
904*38fd1498Szrj     {
905*38fd1498Szrj       note_src = XEXP (note, 0);
906*38fd1498Szrj       if (GET_CODE (note_src) == EXPR_LIST)
907*38fd1498Szrj 	note_src = NULL_RTX;
908*38fd1498Szrj     }
909*38fd1498Szrj   else note_src = NULL_RTX;
910*38fd1498Szrj 
911*38fd1498Szrj   /* Prefer REG_EQUAL notes except those containing EXPR_LISTs.  */
912*38fd1498Szrj   set_src = note_src ? note_src : SET_SRC (set);
913*38fd1498Szrj 
914*38fd1498Szrj   /* First substitute the SETCC condition into the JUMP instruction,
915*38fd1498Szrj      then substitute that given values into this expanded JUMP.  */
916*38fd1498Szrj   if (setcc != NULL_RTX
917*38fd1498Szrj       && !modified_between_p (from, setcc, jump)
918*38fd1498Szrj       && !modified_between_p (src, setcc, jump))
919*38fd1498Szrj     {
920*38fd1498Szrj       rtx setcc_src;
921*38fd1498Szrj       rtx setcc_set = single_set (setcc);
922*38fd1498Szrj       rtx setcc_note = find_reg_equal_equiv_note (setcc);
923*38fd1498Szrj       setcc_src = (setcc_note && GET_CODE (XEXP (setcc_note, 0)) != EXPR_LIST)
924*38fd1498Szrj 		? XEXP (setcc_note, 0) : SET_SRC (setcc_set);
925*38fd1498Szrj       set_src = simplify_replace_rtx (set_src, SET_DEST (setcc_set),
926*38fd1498Szrj 				      setcc_src);
927*38fd1498Szrj     }
928*38fd1498Szrj   else
929*38fd1498Szrj     setcc = NULL;
930*38fd1498Szrj 
931*38fd1498Szrj   new_rtx = simplify_replace_rtx (set_src, from, src);
932*38fd1498Szrj 
933*38fd1498Szrj   /* If no simplification can be made, then try the next register.  */
934*38fd1498Szrj   if (rtx_equal_p (new_rtx, SET_SRC (set)))
935*38fd1498Szrj     return 0;
936*38fd1498Szrj 
937*38fd1498Szrj   /* If this is now a no-op delete it, otherwise this must be a valid insn.  */
938*38fd1498Szrj   if (new_rtx == pc_rtx)
939*38fd1498Szrj     delete_insn (jump);
940*38fd1498Szrj   else
941*38fd1498Szrj     {
942*38fd1498Szrj       /* Ensure the value computed inside the jump insn to be equivalent
943*38fd1498Szrj          to one computed by setcc.  */
944*38fd1498Szrj       if (setcc && modified_in_p (new_rtx, setcc))
945*38fd1498Szrj 	return 0;
946*38fd1498Szrj       if (! validate_unshare_change (jump, &SET_SRC (set), new_rtx, 0))
947*38fd1498Szrj 	{
948*38fd1498Szrj 	  /* When (some) constants are not valid in a comparison, and there
949*38fd1498Szrj 	     are two registers to be replaced by constants before the entire
950*38fd1498Szrj 	     comparison can be folded into a constant, we need to keep
951*38fd1498Szrj 	     intermediate information in REG_EQUAL notes.  For targets with
952*38fd1498Szrj 	     separate compare insns, such notes are added by try_replace_reg.
953*38fd1498Szrj 	     When we have a combined compare-and-branch instruction, however,
954*38fd1498Szrj 	     we need to attach a note to the branch itself to make this
955*38fd1498Szrj 	     optimization work.  */
956*38fd1498Szrj 
957*38fd1498Szrj 	  if (!rtx_equal_p (new_rtx, note_src))
958*38fd1498Szrj 	    set_unique_reg_note (jump, REG_EQUAL, copy_rtx (new_rtx));
959*38fd1498Szrj 	  return 0;
960*38fd1498Szrj 	}
961*38fd1498Szrj 
962*38fd1498Szrj       /* Remove REG_EQUAL note after simplification.  */
963*38fd1498Szrj       if (note_src)
964*38fd1498Szrj 	remove_note (jump, note);
965*38fd1498Szrj      }
966*38fd1498Szrj 
967*38fd1498Szrj   /* Delete the cc0 setter.  */
968*38fd1498Szrj   if (HAVE_cc0 && setcc != NULL && CC0_P (SET_DEST (single_set (setcc))))
969*38fd1498Szrj     delete_insn (setcc);
970*38fd1498Szrj 
971*38fd1498Szrj   global_const_prop_count++;
972*38fd1498Szrj   if (dump_file != NULL)
973*38fd1498Szrj     {
974*38fd1498Szrj       fprintf (dump_file,
975*38fd1498Szrj 	       "GLOBAL CONST-PROP: Replacing reg %d in jump_insn %d with "
976*38fd1498Szrj 	       "constant ", REGNO (from), INSN_UID (jump));
977*38fd1498Szrj       print_rtl (dump_file, src);
978*38fd1498Szrj       fprintf (dump_file, "\n");
979*38fd1498Szrj     }
980*38fd1498Szrj   purge_dead_edges (bb);
981*38fd1498Szrj 
982*38fd1498Szrj   /* If a conditional jump has been changed into unconditional jump, remove
983*38fd1498Szrj      the jump and make the edge fallthru - this is always called in
984*38fd1498Szrj      cfglayout mode.  */
985*38fd1498Szrj   if (new_rtx != pc_rtx && simplejump_p (jump))
986*38fd1498Szrj     {
987*38fd1498Szrj       edge e;
988*38fd1498Szrj       edge_iterator ei;
989*38fd1498Szrj 
990*38fd1498Szrj       FOR_EACH_EDGE (e, ei, bb->succs)
991*38fd1498Szrj 	if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
992*38fd1498Szrj 	    && BB_HEAD (e->dest) == JUMP_LABEL (jump))
993*38fd1498Szrj 	  {
994*38fd1498Szrj 	    e->flags |= EDGE_FALLTHRU;
995*38fd1498Szrj 	    break;
996*38fd1498Szrj 	  }
997*38fd1498Szrj       delete_insn (jump);
998*38fd1498Szrj     }
999*38fd1498Szrj 
1000*38fd1498Szrj   return 1;
1001*38fd1498Szrj }
1002*38fd1498Szrj 
1003*38fd1498Szrj /* Subroutine of cprop_insn that tries to propagate constants.  FROM is what
1004*38fd1498Szrj    we will try to replace, SRC is the constant we will try to substitute for
1005*38fd1498Szrj    it and INSN is the instruction where this will be happening.  */
1006*38fd1498Szrj 
1007*38fd1498Szrj static int
constprop_register(rtx from,rtx src,rtx_insn * insn)1008*38fd1498Szrj constprop_register (rtx from, rtx src, rtx_insn *insn)
1009*38fd1498Szrj {
1010*38fd1498Szrj   rtx sset;
1011*38fd1498Szrj 
1012*38fd1498Szrj   /* Check for reg or cc0 setting instructions followed by
1013*38fd1498Szrj      conditional branch instructions first.  */
1014*38fd1498Szrj   if ((sset = single_set (insn)) != NULL
1015*38fd1498Szrj       && NEXT_INSN (insn)
1016*38fd1498Szrj       && any_condjump_p (NEXT_INSN (insn)) && onlyjump_p (NEXT_INSN (insn)))
1017*38fd1498Szrj     {
1018*38fd1498Szrj       rtx dest = SET_DEST (sset);
1019*38fd1498Szrj       if ((REG_P (dest) || CC0_P (dest))
1020*38fd1498Szrj 	  && cprop_jump (BLOCK_FOR_INSN (insn), insn, NEXT_INSN (insn),
1021*38fd1498Szrj 			 from, src))
1022*38fd1498Szrj 	return 1;
1023*38fd1498Szrj     }
1024*38fd1498Szrj 
1025*38fd1498Szrj   /* Handle normal insns next.  */
1026*38fd1498Szrj   if (NONJUMP_INSN_P (insn) && try_replace_reg (from, src, insn))
1027*38fd1498Szrj     return 1;
1028*38fd1498Szrj 
1029*38fd1498Szrj   /* Try to propagate a CONST_INT into a conditional jump.
1030*38fd1498Szrj      We're pretty specific about what we will handle in this
1031*38fd1498Szrj      code, we can extend this as necessary over time.
1032*38fd1498Szrj 
1033*38fd1498Szrj      Right now the insn in question must look like
1034*38fd1498Szrj      (set (pc) (if_then_else ...))  */
1035*38fd1498Szrj   else if (any_condjump_p (insn) && onlyjump_p (insn))
1036*38fd1498Szrj     return cprop_jump (BLOCK_FOR_INSN (insn), NULL, insn, from, src);
1037*38fd1498Szrj   return 0;
1038*38fd1498Szrj }
1039*38fd1498Szrj 
1040*38fd1498Szrj /* Perform constant and copy propagation on INSN.
1041*38fd1498Szrj    Return nonzero if a change was made.  */
1042*38fd1498Szrj 
1043*38fd1498Szrj static int
cprop_insn(rtx_insn * insn)1044*38fd1498Szrj cprop_insn (rtx_insn *insn)
1045*38fd1498Szrj {
1046*38fd1498Szrj   unsigned i;
1047*38fd1498Szrj   int changed = 0, changed_this_round;
1048*38fd1498Szrj   rtx note;
1049*38fd1498Szrj 
1050*38fd1498Szrj   do
1051*38fd1498Szrj     {
1052*38fd1498Szrj       changed_this_round = 0;
1053*38fd1498Szrj       reg_use_count = 0;
1054*38fd1498Szrj       note_uses (&PATTERN (insn), find_used_regs, NULL);
1055*38fd1498Szrj 
1056*38fd1498Szrj       /* We may win even when propagating constants into notes.  */
1057*38fd1498Szrj       note = find_reg_equal_equiv_note (insn);
1058*38fd1498Szrj       if (note)
1059*38fd1498Szrj 	find_used_regs (&XEXP (note, 0), NULL);
1060*38fd1498Szrj 
1061*38fd1498Szrj       for (i = 0; i < reg_use_count; i++)
1062*38fd1498Szrj 	{
1063*38fd1498Szrj 	  rtx reg_used = reg_use_table[i];
1064*38fd1498Szrj 	  unsigned int regno = REGNO (reg_used);
1065*38fd1498Szrj 	  rtx src_cst = NULL, src_reg = NULL;
1066*38fd1498Szrj 	  struct cprop_expr *set[2];
1067*38fd1498Szrj 
1068*38fd1498Szrj 	  /* If the register has already been set in this block, there's
1069*38fd1498Szrj 	     nothing we can do.  */
1070*38fd1498Szrj 	  if (! reg_not_set_p (reg_used, insn))
1071*38fd1498Szrj 	    continue;
1072*38fd1498Szrj 
1073*38fd1498Szrj 	  /* Find an assignment that sets reg_used and is available
1074*38fd1498Szrj 	     at the start of the block.  */
1075*38fd1498Szrj 	  find_avail_set (regno, insn, set);
1076*38fd1498Szrj 	  if (set[0])
1077*38fd1498Szrj 	    src_reg = set[0]->src;
1078*38fd1498Szrj 	  if (set[1])
1079*38fd1498Szrj 	    src_cst = set[1]->src;
1080*38fd1498Szrj 
1081*38fd1498Szrj 	  /* Constant propagation.  */
1082*38fd1498Szrj 	  if (src_cst && cprop_constant_p (src_cst)
1083*38fd1498Szrj 	      && constprop_register (reg_used, src_cst, insn))
1084*38fd1498Szrj 	    {
1085*38fd1498Szrj 	      changed_this_round = changed = 1;
1086*38fd1498Szrj 	      global_const_prop_count++;
1087*38fd1498Szrj 	      if (dump_file != NULL)
1088*38fd1498Szrj 		{
1089*38fd1498Szrj 		  fprintf (dump_file,
1090*38fd1498Szrj 			   "GLOBAL CONST-PROP: Replacing reg %d in ", regno);
1091*38fd1498Szrj 		  fprintf (dump_file, "insn %d with constant ",
1092*38fd1498Szrj 			   INSN_UID (insn));
1093*38fd1498Szrj 		  print_rtl (dump_file, src_cst);
1094*38fd1498Szrj 		  fprintf (dump_file, "\n");
1095*38fd1498Szrj 		}
1096*38fd1498Szrj 	      if (insn->deleted ())
1097*38fd1498Szrj 		return 1;
1098*38fd1498Szrj 	    }
1099*38fd1498Szrj 	  /* Copy propagation.  */
1100*38fd1498Szrj 	  else if (src_reg && cprop_reg_p (src_reg)
1101*38fd1498Szrj 		   && REGNO (src_reg) != regno
1102*38fd1498Szrj 		   && try_replace_reg (reg_used, src_reg, insn))
1103*38fd1498Szrj 	    {
1104*38fd1498Szrj 	      changed_this_round = changed = 1;
1105*38fd1498Szrj 	      global_copy_prop_count++;
1106*38fd1498Szrj 	      if (dump_file != NULL)
1107*38fd1498Szrj 		{
1108*38fd1498Szrj 		  fprintf (dump_file,
1109*38fd1498Szrj 			   "GLOBAL COPY-PROP: Replacing reg %d in insn %d",
1110*38fd1498Szrj 			   regno, INSN_UID (insn));
1111*38fd1498Szrj 		  fprintf (dump_file, " with reg %d\n", REGNO (src_reg));
1112*38fd1498Szrj 		}
1113*38fd1498Szrj 
1114*38fd1498Szrj 	      /* The original insn setting reg_used may or may not now be
1115*38fd1498Szrj 		 deletable.  We leave the deletion to DCE.  */
1116*38fd1498Szrj 	      /* FIXME: If it turns out that the insn isn't deletable,
1117*38fd1498Szrj 		 then we may have unnecessarily extended register lifetimes
1118*38fd1498Szrj 		 and made things worse.  */
1119*38fd1498Szrj 	    }
1120*38fd1498Szrj 	}
1121*38fd1498Szrj     }
1122*38fd1498Szrj   /* If try_replace_reg simplified the insn, the regs found by find_used_regs
1123*38fd1498Szrj      may not be valid anymore.  Start over.  */
1124*38fd1498Szrj   while (changed_this_round);
1125*38fd1498Szrj 
1126*38fd1498Szrj   if (changed && DEBUG_INSN_P (insn))
1127*38fd1498Szrj     return 0;
1128*38fd1498Szrj 
1129*38fd1498Szrj   return changed;
1130*38fd1498Szrj }
1131*38fd1498Szrj 
1132*38fd1498Szrj /* Like find_used_regs, but avoid recording uses that appear in
1133*38fd1498Szrj    input-output contexts such as zero_extract or pre_dec.  This
1134*38fd1498Szrj    restricts the cases we consider to those for which local cprop
1135*38fd1498Szrj    can legitimately make replacements.  */
1136*38fd1498Szrj 
1137*38fd1498Szrj static void
local_cprop_find_used_regs(rtx * xptr,void * data)1138*38fd1498Szrj local_cprop_find_used_regs (rtx *xptr, void *data)
1139*38fd1498Szrj {
1140*38fd1498Szrj   rtx x = *xptr;
1141*38fd1498Szrj 
1142*38fd1498Szrj   if (x == 0)
1143*38fd1498Szrj     return;
1144*38fd1498Szrj 
1145*38fd1498Szrj   switch (GET_CODE (x))
1146*38fd1498Szrj     {
1147*38fd1498Szrj     case ZERO_EXTRACT:
1148*38fd1498Szrj     case SIGN_EXTRACT:
1149*38fd1498Szrj     case STRICT_LOW_PART:
1150*38fd1498Szrj       return;
1151*38fd1498Szrj 
1152*38fd1498Szrj     case PRE_DEC:
1153*38fd1498Szrj     case PRE_INC:
1154*38fd1498Szrj     case POST_DEC:
1155*38fd1498Szrj     case POST_INC:
1156*38fd1498Szrj     case PRE_MODIFY:
1157*38fd1498Szrj     case POST_MODIFY:
1158*38fd1498Szrj       /* Can only legitimately appear this early in the context of
1159*38fd1498Szrj 	 stack pushes for function arguments, but handle all of the
1160*38fd1498Szrj 	 codes nonetheless.  */
1161*38fd1498Szrj       return;
1162*38fd1498Szrj 
1163*38fd1498Szrj     case SUBREG:
1164*38fd1498Szrj       if (read_modify_subreg_p (x))
1165*38fd1498Szrj 	return;
1166*38fd1498Szrj       break;
1167*38fd1498Szrj 
1168*38fd1498Szrj     default:
1169*38fd1498Szrj       break;
1170*38fd1498Szrj     }
1171*38fd1498Szrj 
1172*38fd1498Szrj   find_used_regs (xptr, data);
1173*38fd1498Szrj }
1174*38fd1498Szrj 
1175*38fd1498Szrj /* Try to perform local const/copy propagation on X in INSN.  */
1176*38fd1498Szrj 
1177*38fd1498Szrj static bool
do_local_cprop(rtx x,rtx_insn * insn)1178*38fd1498Szrj do_local_cprop (rtx x, rtx_insn *insn)
1179*38fd1498Szrj {
1180*38fd1498Szrj   rtx newreg = NULL, newcnst = NULL;
1181*38fd1498Szrj 
1182*38fd1498Szrj   /* Rule out USE instructions and ASM statements as we don't want to
1183*38fd1498Szrj      change the hard registers mentioned.  */
1184*38fd1498Szrj   if (REG_P (x)
1185*38fd1498Szrj       && (cprop_reg_p (x)
1186*38fd1498Szrj           || (GET_CODE (PATTERN (insn)) != USE
1187*38fd1498Szrj 	      && asm_noperands (PATTERN (insn)) < 0)))
1188*38fd1498Szrj     {
1189*38fd1498Szrj       cselib_val *val = cselib_lookup (x, GET_MODE (x), 0, VOIDmode);
1190*38fd1498Szrj       struct elt_loc_list *l;
1191*38fd1498Szrj 
1192*38fd1498Szrj       if (!val)
1193*38fd1498Szrj 	return false;
1194*38fd1498Szrj       for (l = val->locs; l; l = l->next)
1195*38fd1498Szrj 	{
1196*38fd1498Szrj 	  rtx this_rtx = l->loc;
1197*38fd1498Szrj 	  rtx note;
1198*38fd1498Szrj 
1199*38fd1498Szrj 	  if (cprop_constant_p (this_rtx))
1200*38fd1498Szrj 	    newcnst = this_rtx;
1201*38fd1498Szrj 	  if (cprop_reg_p (this_rtx)
1202*38fd1498Szrj 	      /* Don't copy propagate if it has attached REG_EQUIV note.
1203*38fd1498Szrj 		 At this point this only function parameters should have
1204*38fd1498Szrj 		 REG_EQUIV notes and if the argument slot is used somewhere
1205*38fd1498Szrj 		 explicitly, it means address of parameter has been taken,
1206*38fd1498Szrj 		 so we should not extend the lifetime of the pseudo.  */
1207*38fd1498Szrj 	      && (!(note = find_reg_note (l->setting_insn, REG_EQUIV, NULL_RTX))
1208*38fd1498Szrj 		  || ! MEM_P (XEXP (note, 0))))
1209*38fd1498Szrj 	    newreg = this_rtx;
1210*38fd1498Szrj 	}
1211*38fd1498Szrj       if (newcnst && constprop_register (x, newcnst, insn))
1212*38fd1498Szrj 	{
1213*38fd1498Szrj 	  if (dump_file != NULL)
1214*38fd1498Szrj 	    {
1215*38fd1498Szrj 	      fprintf (dump_file, "LOCAL CONST-PROP: Replacing reg %d in ",
1216*38fd1498Szrj 		       REGNO (x));
1217*38fd1498Szrj 	      fprintf (dump_file, "insn %d with constant ",
1218*38fd1498Szrj 		       INSN_UID (insn));
1219*38fd1498Szrj 	      print_rtl (dump_file, newcnst);
1220*38fd1498Szrj 	      fprintf (dump_file, "\n");
1221*38fd1498Szrj 	    }
1222*38fd1498Szrj 	  local_const_prop_count++;
1223*38fd1498Szrj 	  return true;
1224*38fd1498Szrj 	}
1225*38fd1498Szrj       else if (newreg && newreg != x && try_replace_reg (x, newreg, insn))
1226*38fd1498Szrj 	{
1227*38fd1498Szrj 	  if (dump_file != NULL)
1228*38fd1498Szrj 	    {
1229*38fd1498Szrj 	      fprintf (dump_file,
1230*38fd1498Szrj 		       "LOCAL COPY-PROP: Replacing reg %d in insn %d",
1231*38fd1498Szrj 		       REGNO (x), INSN_UID (insn));
1232*38fd1498Szrj 	      fprintf (dump_file, " with reg %d\n", REGNO (newreg));
1233*38fd1498Szrj 	    }
1234*38fd1498Szrj 	  local_copy_prop_count++;
1235*38fd1498Szrj 	  return true;
1236*38fd1498Szrj 	}
1237*38fd1498Szrj     }
1238*38fd1498Szrj   return false;
1239*38fd1498Szrj }
1240*38fd1498Szrj 
1241*38fd1498Szrj /* Do local const/copy propagation (i.e. within each basic block).  */
1242*38fd1498Szrj 
1243*38fd1498Szrj static int
local_cprop_pass(void)1244*38fd1498Szrj local_cprop_pass (void)
1245*38fd1498Szrj {
1246*38fd1498Szrj   basic_block bb;
1247*38fd1498Szrj   rtx_insn *insn;
1248*38fd1498Szrj   bool changed = false;
1249*38fd1498Szrj   unsigned i;
1250*38fd1498Szrj 
1251*38fd1498Szrj   auto_vec<rtx_insn *> uncond_traps;
1252*38fd1498Szrj 
1253*38fd1498Szrj   cselib_init (0);
1254*38fd1498Szrj   FOR_EACH_BB_FN (bb, cfun)
1255*38fd1498Szrj     {
1256*38fd1498Szrj       FOR_BB_INSNS (bb, insn)
1257*38fd1498Szrj 	{
1258*38fd1498Szrj 	  if (INSN_P (insn))
1259*38fd1498Szrj 	    {
1260*38fd1498Szrj 	      bool was_uncond_trap
1261*38fd1498Szrj 		= (GET_CODE (PATTERN (insn)) == TRAP_IF
1262*38fd1498Szrj 		   && XEXP (PATTERN (insn), 0) == const1_rtx);
1263*38fd1498Szrj 	      rtx note = find_reg_equal_equiv_note (insn);
1264*38fd1498Szrj 	      do
1265*38fd1498Szrj 		{
1266*38fd1498Szrj 		  reg_use_count = 0;
1267*38fd1498Szrj 		  note_uses (&PATTERN (insn), local_cprop_find_used_regs,
1268*38fd1498Szrj 			     NULL);
1269*38fd1498Szrj 		  if (note)
1270*38fd1498Szrj 		    local_cprop_find_used_regs (&XEXP (note, 0), NULL);
1271*38fd1498Szrj 
1272*38fd1498Szrj 		  for (i = 0; i < reg_use_count; i++)
1273*38fd1498Szrj 		    {
1274*38fd1498Szrj 		      if (do_local_cprop (reg_use_table[i], insn))
1275*38fd1498Szrj 			{
1276*38fd1498Szrj 			  if (!DEBUG_INSN_P (insn))
1277*38fd1498Szrj 			    changed = true;
1278*38fd1498Szrj 			  break;
1279*38fd1498Szrj 			}
1280*38fd1498Szrj 		    }
1281*38fd1498Szrj 		  if (!was_uncond_trap
1282*38fd1498Szrj 		      && GET_CODE (PATTERN (insn)) == TRAP_IF
1283*38fd1498Szrj 		      && XEXP (PATTERN (insn), 0) == const1_rtx)
1284*38fd1498Szrj 		    {
1285*38fd1498Szrj 		      uncond_traps.safe_push (insn);
1286*38fd1498Szrj 		      break;
1287*38fd1498Szrj 		    }
1288*38fd1498Szrj 		  if (insn->deleted ())
1289*38fd1498Szrj 		    break;
1290*38fd1498Szrj 		}
1291*38fd1498Szrj 	      while (i < reg_use_count);
1292*38fd1498Szrj 	    }
1293*38fd1498Szrj 	  cselib_process_insn (insn);
1294*38fd1498Szrj 	}
1295*38fd1498Szrj 
1296*38fd1498Szrj       /* Forget everything at the end of a basic block.  */
1297*38fd1498Szrj       cselib_clear_table ();
1298*38fd1498Szrj     }
1299*38fd1498Szrj 
1300*38fd1498Szrj   cselib_finish ();
1301*38fd1498Szrj 
1302*38fd1498Szrj   while (!uncond_traps.is_empty ())
1303*38fd1498Szrj     {
1304*38fd1498Szrj       rtx_insn *insn = uncond_traps.pop ();
1305*38fd1498Szrj       basic_block to_split = BLOCK_FOR_INSN (insn);
1306*38fd1498Szrj       remove_edge (split_block (to_split, insn));
1307*38fd1498Szrj       emit_barrier_after_bb (to_split);
1308*38fd1498Szrj     }
1309*38fd1498Szrj 
1310*38fd1498Szrj   return changed;
1311*38fd1498Szrj }
1312*38fd1498Szrj 
1313*38fd1498Szrj /* Similar to get_condition, only the resulting condition must be
1314*38fd1498Szrj    valid at JUMP, instead of at EARLIEST.
1315*38fd1498Szrj 
1316*38fd1498Szrj    This differs from noce_get_condition in ifcvt.c in that we prefer not to
1317*38fd1498Szrj    settle for the condition variable in the jump instruction being integral.
1318*38fd1498Szrj    We prefer to be able to record the value of a user variable, rather than
1319*38fd1498Szrj    the value of a temporary used in a condition.  This could be solved by
1320*38fd1498Szrj    recording the value of *every* register scanned by canonicalize_condition,
1321*38fd1498Szrj    but this would require some code reorganization.  */
1322*38fd1498Szrj 
1323*38fd1498Szrj rtx
fis_get_condition(rtx_insn * jump)1324*38fd1498Szrj fis_get_condition (rtx_insn *jump)
1325*38fd1498Szrj {
1326*38fd1498Szrj   return get_condition (jump, NULL, false, true);
1327*38fd1498Szrj }
1328*38fd1498Szrj 
1329*38fd1498Szrj /* Check the comparison COND to see if we can safely form an implicit
1330*38fd1498Szrj    set from it.  */
1331*38fd1498Szrj 
1332*38fd1498Szrj static bool
implicit_set_cond_p(const_rtx cond)1333*38fd1498Szrj implicit_set_cond_p (const_rtx cond)
1334*38fd1498Szrj {
1335*38fd1498Szrj   machine_mode mode;
1336*38fd1498Szrj   rtx cst;
1337*38fd1498Szrj 
1338*38fd1498Szrj   /* COND must be either an EQ or NE comparison.  */
1339*38fd1498Szrj   if (GET_CODE (cond) != EQ && GET_CODE (cond) != NE)
1340*38fd1498Szrj     return false;
1341*38fd1498Szrj 
1342*38fd1498Szrj   /* The first operand of COND must be a register we can propagate.  */
1343*38fd1498Szrj   if (!cprop_reg_p (XEXP (cond, 0)))
1344*38fd1498Szrj     return false;
1345*38fd1498Szrj 
1346*38fd1498Szrj   /* The second operand of COND must be a suitable constant.  */
1347*38fd1498Szrj   mode = GET_MODE (XEXP (cond, 0));
1348*38fd1498Szrj   cst = XEXP (cond, 1);
1349*38fd1498Szrj 
1350*38fd1498Szrj   /* We can't perform this optimization if either operand might be or might
1351*38fd1498Szrj      contain a signed zero.  */
1352*38fd1498Szrj   if (HONOR_SIGNED_ZEROS (mode))
1353*38fd1498Szrj     {
1354*38fd1498Szrj       /* It is sufficient to check if CST is or contains a zero.  We must
1355*38fd1498Szrj 	 handle float, complex, and vector.  If any subpart is a zero, then
1356*38fd1498Szrj 	 the optimization can't be performed.  */
1357*38fd1498Szrj       /* ??? The complex and vector checks are not implemented yet.  We just
1358*38fd1498Szrj 	 always return zero for them.  */
1359*38fd1498Szrj       if (CONST_DOUBLE_AS_FLOAT_P (cst)
1360*38fd1498Szrj 	  && real_equal (CONST_DOUBLE_REAL_VALUE (cst), &dconst0))
1361*38fd1498Szrj 	return 0;
1362*38fd1498Szrj       else
1363*38fd1498Szrj 	return 0;
1364*38fd1498Szrj     }
1365*38fd1498Szrj 
1366*38fd1498Szrj   return cprop_constant_p (cst);
1367*38fd1498Szrj }
1368*38fd1498Szrj 
1369*38fd1498Szrj /* Find the implicit sets of a function.  An "implicit set" is a constraint
1370*38fd1498Szrj    on the value of a variable, implied by a conditional jump.  For example,
1371*38fd1498Szrj    following "if (x == 2)", the then branch may be optimized as though the
1372*38fd1498Szrj    conditional performed an "explicit set", in this example, "x = 2".  This
1373*38fd1498Szrj    function records the set patterns that are implicit at the start of each
1374*38fd1498Szrj    basic block.
1375*38fd1498Szrj 
1376*38fd1498Szrj    If an implicit set is found but the set is implicit on a critical edge,
1377*38fd1498Szrj    this critical edge is split.
1378*38fd1498Szrj 
1379*38fd1498Szrj    Return true if the CFG was modified, false otherwise.  */
1380*38fd1498Szrj 
1381*38fd1498Szrj static bool
find_implicit_sets(void)1382*38fd1498Szrj find_implicit_sets (void)
1383*38fd1498Szrj {
1384*38fd1498Szrj   basic_block bb, dest;
1385*38fd1498Szrj   rtx cond, new_rtx;
1386*38fd1498Szrj   unsigned int count = 0;
1387*38fd1498Szrj   bool edges_split = false;
1388*38fd1498Szrj   size_t implicit_sets_size = last_basic_block_for_fn (cfun) + 10;
1389*38fd1498Szrj 
1390*38fd1498Szrj   implicit_sets = XCNEWVEC (rtx, implicit_sets_size);
1391*38fd1498Szrj 
1392*38fd1498Szrj   FOR_EACH_BB_FN (bb, cfun)
1393*38fd1498Szrj     {
1394*38fd1498Szrj       /* Check for more than one successor.  */
1395*38fd1498Szrj       if (EDGE_COUNT (bb->succs) <= 1)
1396*38fd1498Szrj 	continue;
1397*38fd1498Szrj 
1398*38fd1498Szrj       cond = fis_get_condition (BB_END (bb));
1399*38fd1498Szrj 
1400*38fd1498Szrj       /* If no condition is found or if it isn't of a suitable form,
1401*38fd1498Szrj 	 ignore it.  */
1402*38fd1498Szrj       if (! cond || ! implicit_set_cond_p (cond))
1403*38fd1498Szrj 	continue;
1404*38fd1498Szrj 
1405*38fd1498Szrj       dest = GET_CODE (cond) == EQ
1406*38fd1498Szrj 	? BRANCH_EDGE (bb)->dest : FALLTHRU_EDGE (bb)->dest;
1407*38fd1498Szrj 
1408*38fd1498Szrj       /* If DEST doesn't go anywhere, ignore it.  */
1409*38fd1498Szrj       if (! dest || dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1410*38fd1498Szrj 	continue;
1411*38fd1498Szrj 
1412*38fd1498Szrj       /* We have found a suitable implicit set.  Try to record it now as
1413*38fd1498Szrj 	 a SET in DEST.  If DEST has more than one predecessor, the edge
1414*38fd1498Szrj 	 between BB and DEST is a critical edge and we must split it,
1415*38fd1498Szrj 	 because we can only record one implicit set per DEST basic block.  */
1416*38fd1498Szrj       if (! single_pred_p (dest))
1417*38fd1498Szrj         {
1418*38fd1498Szrj 	  dest = split_edge (find_edge (bb, dest));
1419*38fd1498Szrj 	  edges_split = true;
1420*38fd1498Szrj 	}
1421*38fd1498Szrj 
1422*38fd1498Szrj       if (implicit_sets_size <= (size_t) dest->index)
1423*38fd1498Szrj       {
1424*38fd1498Szrj         size_t old_implicit_sets_size = implicit_sets_size;
1425*38fd1498Szrj 	implicit_sets_size *= 2;
1426*38fd1498Szrj 	implicit_sets = XRESIZEVEC (rtx, implicit_sets, implicit_sets_size);
1427*38fd1498Szrj 	memset (implicit_sets + old_implicit_sets_size, 0,
1428*38fd1498Szrj 		(implicit_sets_size - old_implicit_sets_size) * sizeof (rtx));
1429*38fd1498Szrj       }
1430*38fd1498Szrj 
1431*38fd1498Szrj       new_rtx = gen_rtx_SET (XEXP (cond, 0), XEXP (cond, 1));
1432*38fd1498Szrj       implicit_sets[dest->index] = new_rtx;
1433*38fd1498Szrj       if (dump_file)
1434*38fd1498Szrj 	{
1435*38fd1498Szrj 	  fprintf (dump_file, "Implicit set of reg %d in ",
1436*38fd1498Szrj 		   REGNO (XEXP (cond, 0)));
1437*38fd1498Szrj 	  fprintf (dump_file, "basic block %d\n", dest->index);
1438*38fd1498Szrj 	}
1439*38fd1498Szrj       count++;
1440*38fd1498Szrj     }
1441*38fd1498Szrj 
1442*38fd1498Szrj   if (dump_file)
1443*38fd1498Szrj     fprintf (dump_file, "Found %d implicit sets\n", count);
1444*38fd1498Szrj 
1445*38fd1498Szrj   /* Confess our sins.  */
1446*38fd1498Szrj   return edges_split;
1447*38fd1498Szrj }
1448*38fd1498Szrj 
1449*38fd1498Szrj /* Bypass conditional jumps.  */
1450*38fd1498Szrj 
1451*38fd1498Szrj /* The value of last_basic_block at the beginning of the jump_bypass
1452*38fd1498Szrj    pass.  The use of redirect_edge_and_branch_force may introduce new
1453*38fd1498Szrj    basic blocks, but the data flow analysis is only valid for basic
1454*38fd1498Szrj    block indices less than bypass_last_basic_block.  */
1455*38fd1498Szrj 
1456*38fd1498Szrj static int bypass_last_basic_block;
1457*38fd1498Szrj 
1458*38fd1498Szrj /* Find a set of REGNO to a constant that is available at the end of basic
1459*38fd1498Szrj    block BB.  Return NULL if no such set is found.  Based heavily upon
1460*38fd1498Szrj    find_avail_set.  */
1461*38fd1498Szrj 
1462*38fd1498Szrj static struct cprop_expr *
find_bypass_set(int regno,int bb)1463*38fd1498Szrj find_bypass_set (int regno, int bb)
1464*38fd1498Szrj {
1465*38fd1498Szrj   struct cprop_expr *result = 0;
1466*38fd1498Szrj 
1467*38fd1498Szrj   for (;;)
1468*38fd1498Szrj     {
1469*38fd1498Szrj       rtx src;
1470*38fd1498Szrj       struct cprop_expr *set = lookup_set (regno, &set_hash_table);
1471*38fd1498Szrj 
1472*38fd1498Szrj       while (set)
1473*38fd1498Szrj 	{
1474*38fd1498Szrj 	  if (bitmap_bit_p (cprop_avout[bb], set->bitmap_index))
1475*38fd1498Szrj 	    break;
1476*38fd1498Szrj 	  set = next_set (regno, set);
1477*38fd1498Szrj 	}
1478*38fd1498Szrj 
1479*38fd1498Szrj       if (set == 0)
1480*38fd1498Szrj 	break;
1481*38fd1498Szrj 
1482*38fd1498Szrj       src = set->src;
1483*38fd1498Szrj       if (cprop_constant_p (src))
1484*38fd1498Szrj 	result = set;
1485*38fd1498Szrj 
1486*38fd1498Szrj       if (! REG_P (src))
1487*38fd1498Szrj 	break;
1488*38fd1498Szrj 
1489*38fd1498Szrj       regno = REGNO (src);
1490*38fd1498Szrj     }
1491*38fd1498Szrj   return result;
1492*38fd1498Szrj }
1493*38fd1498Szrj 
1494*38fd1498Szrj /* Subroutine of bypass_block that checks whether a pseudo is killed by
1495*38fd1498Szrj    any of the instructions inserted on an edge.  Jump bypassing places
1496*38fd1498Szrj    condition code setters on CFG edges using insert_insn_on_edge.  This
1497*38fd1498Szrj    function is required to check that our data flow analysis is still
1498*38fd1498Szrj    valid prior to commit_edge_insertions.  */
1499*38fd1498Szrj 
1500*38fd1498Szrj static bool
reg_killed_on_edge(const_rtx reg,const_edge e)1501*38fd1498Szrj reg_killed_on_edge (const_rtx reg, const_edge e)
1502*38fd1498Szrj {
1503*38fd1498Szrj   rtx_insn *insn;
1504*38fd1498Szrj 
1505*38fd1498Szrj   for (insn = e->insns.r; insn; insn = NEXT_INSN (insn))
1506*38fd1498Szrj     if (INSN_P (insn) && reg_set_p (reg, insn))
1507*38fd1498Szrj       return true;
1508*38fd1498Szrj 
1509*38fd1498Szrj   return false;
1510*38fd1498Szrj }
1511*38fd1498Szrj 
1512*38fd1498Szrj /* Subroutine of bypass_conditional_jumps that attempts to bypass the given
1513*38fd1498Szrj    basic block BB which has more than one predecessor.  If not NULL, SETCC
1514*38fd1498Szrj    is the first instruction of BB, which is immediately followed by JUMP_INSN
1515*38fd1498Szrj    JUMP.  Otherwise, SETCC is NULL, and JUMP is the first insn of BB.
1516*38fd1498Szrj    Returns nonzero if a change was made.
1517*38fd1498Szrj 
1518*38fd1498Szrj    During the jump bypassing pass, we may place copies of SETCC instructions
1519*38fd1498Szrj    on CFG edges.  The following routine must be careful to pay attention to
1520*38fd1498Szrj    these inserted insns when performing its transformations.  */
1521*38fd1498Szrj 
1522*38fd1498Szrj static int
bypass_block(basic_block bb,rtx_insn * setcc,rtx_insn * jump)1523*38fd1498Szrj bypass_block (basic_block bb, rtx_insn *setcc, rtx_insn *jump)
1524*38fd1498Szrj {
1525*38fd1498Szrj   rtx_insn *insn;
1526*38fd1498Szrj   rtx note;
1527*38fd1498Szrj   edge e, edest;
1528*38fd1498Szrj   int change;
1529*38fd1498Szrj   int may_be_loop_header = false;
1530*38fd1498Szrj   unsigned removed_p;
1531*38fd1498Szrj   unsigned i;
1532*38fd1498Szrj   edge_iterator ei;
1533*38fd1498Szrj 
1534*38fd1498Szrj   insn = (setcc != NULL) ? setcc : jump;
1535*38fd1498Szrj 
1536*38fd1498Szrj   /* Determine set of register uses in INSN.  */
1537*38fd1498Szrj   reg_use_count = 0;
1538*38fd1498Szrj   note_uses (&PATTERN (insn), find_used_regs, NULL);
1539*38fd1498Szrj   note = find_reg_equal_equiv_note (insn);
1540*38fd1498Szrj   if (note)
1541*38fd1498Szrj     find_used_regs (&XEXP (note, 0), NULL);
1542*38fd1498Szrj 
1543*38fd1498Szrj   if (current_loops)
1544*38fd1498Szrj     {
1545*38fd1498Szrj       /* If we are to preserve loop structure then do not bypass
1546*38fd1498Szrj          a loop header.  This will either rotate the loop, create
1547*38fd1498Szrj 	 multiple entry loops or even irreducible regions.  */
1548*38fd1498Szrj       if (bb == bb->loop_father->header)
1549*38fd1498Szrj 	return 0;
1550*38fd1498Szrj     }
1551*38fd1498Szrj   else
1552*38fd1498Szrj     {
1553*38fd1498Szrj       FOR_EACH_EDGE (e, ei, bb->preds)
1554*38fd1498Szrj 	if (e->flags & EDGE_DFS_BACK)
1555*38fd1498Szrj 	  {
1556*38fd1498Szrj 	    may_be_loop_header = true;
1557*38fd1498Szrj 	    break;
1558*38fd1498Szrj 	  }
1559*38fd1498Szrj     }
1560*38fd1498Szrj 
1561*38fd1498Szrj   change = 0;
1562*38fd1498Szrj   for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
1563*38fd1498Szrj     {
1564*38fd1498Szrj       removed_p = 0;
1565*38fd1498Szrj 
1566*38fd1498Szrj       if (e->flags & EDGE_COMPLEX)
1567*38fd1498Szrj 	{
1568*38fd1498Szrj 	  ei_next (&ei);
1569*38fd1498Szrj 	  continue;
1570*38fd1498Szrj 	}
1571*38fd1498Szrj 
1572*38fd1498Szrj       /* We can't redirect edges from new basic blocks.  */
1573*38fd1498Szrj       if (e->src->index >= bypass_last_basic_block)
1574*38fd1498Szrj 	{
1575*38fd1498Szrj 	  ei_next (&ei);
1576*38fd1498Szrj 	  continue;
1577*38fd1498Szrj 	}
1578*38fd1498Szrj 
1579*38fd1498Szrj       /* The irreducible loops created by redirecting of edges entering the
1580*38fd1498Szrj 	 loop from outside would decrease effectiveness of some of the
1581*38fd1498Szrj 	 following optimizations, so prevent this.  */
1582*38fd1498Szrj       if (may_be_loop_header
1583*38fd1498Szrj 	  && !(e->flags & EDGE_DFS_BACK))
1584*38fd1498Szrj 	{
1585*38fd1498Szrj 	  ei_next (&ei);
1586*38fd1498Szrj 	  continue;
1587*38fd1498Szrj 	}
1588*38fd1498Szrj 
1589*38fd1498Szrj       for (i = 0; i < reg_use_count; i++)
1590*38fd1498Szrj 	{
1591*38fd1498Szrj 	  rtx reg_used = reg_use_table[i];
1592*38fd1498Szrj 	  unsigned int regno = REGNO (reg_used);
1593*38fd1498Szrj 	  basic_block dest, old_dest;
1594*38fd1498Szrj 	  struct cprop_expr *set;
1595*38fd1498Szrj 	  rtx src, new_rtx;
1596*38fd1498Szrj 
1597*38fd1498Szrj 	  set = find_bypass_set (regno, e->src->index);
1598*38fd1498Szrj 
1599*38fd1498Szrj 	  if (! set)
1600*38fd1498Szrj 	    continue;
1601*38fd1498Szrj 
1602*38fd1498Szrj 	  /* Check the data flow is valid after edge insertions.  */
1603*38fd1498Szrj 	  if (e->insns.r && reg_killed_on_edge (reg_used, e))
1604*38fd1498Szrj 	    continue;
1605*38fd1498Szrj 
1606*38fd1498Szrj 	  src = SET_SRC (pc_set (jump));
1607*38fd1498Szrj 
1608*38fd1498Szrj 	  if (setcc != NULL)
1609*38fd1498Szrj 	    src = simplify_replace_rtx (src,
1610*38fd1498Szrj 					SET_DEST (PATTERN (setcc)),
1611*38fd1498Szrj 					SET_SRC (PATTERN (setcc)));
1612*38fd1498Szrj 
1613*38fd1498Szrj 	  new_rtx = simplify_replace_rtx (src, reg_used, set->src);
1614*38fd1498Szrj 
1615*38fd1498Szrj 	  /* Jump bypassing may have already placed instructions on
1616*38fd1498Szrj 	     edges of the CFG.  We can't bypass an outgoing edge that
1617*38fd1498Szrj 	     has instructions associated with it, as these insns won't
1618*38fd1498Szrj 	     get executed if the incoming edge is redirected.  */
1619*38fd1498Szrj 	  if (new_rtx == pc_rtx)
1620*38fd1498Szrj 	    {
1621*38fd1498Szrj 	      edest = FALLTHRU_EDGE (bb);
1622*38fd1498Szrj 	      dest = edest->insns.r ? NULL : edest->dest;
1623*38fd1498Szrj 	    }
1624*38fd1498Szrj 	  else if (GET_CODE (new_rtx) == LABEL_REF)
1625*38fd1498Szrj 	    {
1626*38fd1498Szrj 	      dest = BLOCK_FOR_INSN (XEXP (new_rtx, 0));
1627*38fd1498Szrj 	      /* Don't bypass edges containing instructions.  */
1628*38fd1498Szrj 	      edest = find_edge (bb, dest);
1629*38fd1498Szrj 	      if (edest && edest->insns.r)
1630*38fd1498Szrj 		dest = NULL;
1631*38fd1498Szrj 	    }
1632*38fd1498Szrj 	  else
1633*38fd1498Szrj 	    dest = NULL;
1634*38fd1498Szrj 
1635*38fd1498Szrj 	  /* Avoid unification of the edge with other edges from original
1636*38fd1498Szrj 	     branch.  We would end up emitting the instruction on "both"
1637*38fd1498Szrj 	     edges.  */
1638*38fd1498Szrj 	  if (dest && setcc && !CC0_P (SET_DEST (PATTERN (setcc)))
1639*38fd1498Szrj 	      && find_edge (e->src, dest))
1640*38fd1498Szrj 	    dest = NULL;
1641*38fd1498Szrj 
1642*38fd1498Szrj 	  old_dest = e->dest;
1643*38fd1498Szrj 	  if (dest != NULL
1644*38fd1498Szrj 	      && dest != old_dest
1645*38fd1498Szrj 	      && dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
1646*38fd1498Szrj             {
1647*38fd1498Szrj 	      redirect_edge_and_branch_force (e, dest);
1648*38fd1498Szrj 
1649*38fd1498Szrj 	      /* Copy the register setter to the redirected edge.
1650*38fd1498Szrj 		 Don't copy CC0 setters, as CC0 is dead after jump.  */
1651*38fd1498Szrj 	      if (setcc)
1652*38fd1498Szrj 		{
1653*38fd1498Szrj 		  rtx pat = PATTERN (setcc);
1654*38fd1498Szrj 		  if (!CC0_P (SET_DEST (pat)))
1655*38fd1498Szrj 		    insert_insn_on_edge (copy_insn (pat), e);
1656*38fd1498Szrj 		}
1657*38fd1498Szrj 
1658*38fd1498Szrj 	      if (dump_file != NULL)
1659*38fd1498Szrj 		{
1660*38fd1498Szrj 		  fprintf (dump_file, "JUMP-BYPASS: Proved reg %d "
1661*38fd1498Szrj 				      "in jump_insn %d equals constant ",
1662*38fd1498Szrj 			   regno, INSN_UID (jump));
1663*38fd1498Szrj 		  print_rtl (dump_file, set->src);
1664*38fd1498Szrj 		  fprintf (dump_file, "\n\t     when BB %d is entered from "
1665*38fd1498Szrj 				      "BB %d.  Redirect edge %d->%d to %d.\n",
1666*38fd1498Szrj 			   old_dest->index, e->src->index, e->src->index,
1667*38fd1498Szrj 			   old_dest->index, dest->index);
1668*38fd1498Szrj 		}
1669*38fd1498Szrj 	      change = 1;
1670*38fd1498Szrj 	      removed_p = 1;
1671*38fd1498Szrj 	      break;
1672*38fd1498Szrj 	    }
1673*38fd1498Szrj 	}
1674*38fd1498Szrj       if (!removed_p)
1675*38fd1498Szrj 	ei_next (&ei);
1676*38fd1498Szrj     }
1677*38fd1498Szrj   return change;
1678*38fd1498Szrj }
1679*38fd1498Szrj 
1680*38fd1498Szrj /* Find basic blocks with more than one predecessor that only contain a
1681*38fd1498Szrj    single conditional jump.  If the result of the comparison is known at
1682*38fd1498Szrj    compile-time from any incoming edge, redirect that edge to the
1683*38fd1498Szrj    appropriate target.  Return nonzero if a change was made.
1684*38fd1498Szrj 
1685*38fd1498Szrj    This function is now mis-named, because we also handle indirect jumps.  */
1686*38fd1498Szrj 
1687*38fd1498Szrj static int
bypass_conditional_jumps(void)1688*38fd1498Szrj bypass_conditional_jumps (void)
1689*38fd1498Szrj {
1690*38fd1498Szrj   basic_block bb;
1691*38fd1498Szrj   int changed;
1692*38fd1498Szrj   rtx_insn *setcc;
1693*38fd1498Szrj   rtx_insn *insn;
1694*38fd1498Szrj   rtx dest;
1695*38fd1498Szrj 
1696*38fd1498Szrj   /* Note we start at block 1.  */
1697*38fd1498Szrj   if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1698*38fd1498Szrj     return 0;
1699*38fd1498Szrj 
1700*38fd1498Szrj   mark_dfs_back_edges ();
1701*38fd1498Szrj 
1702*38fd1498Szrj   changed = 0;
1703*38fd1498Szrj   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->next_bb,
1704*38fd1498Szrj 		  EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
1705*38fd1498Szrj     {
1706*38fd1498Szrj       /* Check for more than one predecessor.  */
1707*38fd1498Szrj       if (!single_pred_p (bb))
1708*38fd1498Szrj 	{
1709*38fd1498Szrj 	  setcc = NULL;
1710*38fd1498Szrj 	  FOR_BB_INSNS (bb, insn)
1711*38fd1498Szrj 	    if (DEBUG_INSN_P (insn))
1712*38fd1498Szrj 	      continue;
1713*38fd1498Szrj 	    else if (NONJUMP_INSN_P (insn))
1714*38fd1498Szrj 	      {
1715*38fd1498Szrj 		if (setcc)
1716*38fd1498Szrj 		  break;
1717*38fd1498Szrj 		if (GET_CODE (PATTERN (insn)) != SET)
1718*38fd1498Szrj 		  break;
1719*38fd1498Szrj 
1720*38fd1498Szrj 		dest = SET_DEST (PATTERN (insn));
1721*38fd1498Szrj 		if (REG_P (dest) || CC0_P (dest))
1722*38fd1498Szrj 		  setcc = insn;
1723*38fd1498Szrj 		else
1724*38fd1498Szrj 		  break;
1725*38fd1498Szrj 	      }
1726*38fd1498Szrj 	    else if (JUMP_P (insn))
1727*38fd1498Szrj 	      {
1728*38fd1498Szrj 		if ((any_condjump_p (insn) || computed_jump_p (insn))
1729*38fd1498Szrj 		    && onlyjump_p (insn))
1730*38fd1498Szrj 		  changed |= bypass_block (bb, setcc, insn);
1731*38fd1498Szrj 		break;
1732*38fd1498Szrj 	      }
1733*38fd1498Szrj 	    else if (INSN_P (insn))
1734*38fd1498Szrj 	      break;
1735*38fd1498Szrj 	}
1736*38fd1498Szrj     }
1737*38fd1498Szrj 
1738*38fd1498Szrj   /* If we bypassed any register setting insns, we inserted a
1739*38fd1498Szrj      copy on the redirected edge.  These need to be committed.  */
1740*38fd1498Szrj   if (changed)
1741*38fd1498Szrj     commit_edge_insertions ();
1742*38fd1498Szrj 
1743*38fd1498Szrj   return changed;
1744*38fd1498Szrj }
1745*38fd1498Szrj 
1746*38fd1498Szrj /* Main function for the CPROP pass.  */
1747*38fd1498Szrj 
1748*38fd1498Szrj static int
one_cprop_pass(void)1749*38fd1498Szrj one_cprop_pass (void)
1750*38fd1498Szrj {
1751*38fd1498Szrj   int i;
1752*38fd1498Szrj   int changed = 0;
1753*38fd1498Szrj 
1754*38fd1498Szrj   /* Return if there's nothing to do, or it is too expensive.  */
1755*38fd1498Szrj   if (n_basic_blocks_for_fn (cfun) <= NUM_FIXED_BLOCKS + 1
1756*38fd1498Szrj       || gcse_or_cprop_is_too_expensive (_ ("const/copy propagation disabled")))
1757*38fd1498Szrj     return 0;
1758*38fd1498Szrj 
1759*38fd1498Szrj   global_const_prop_count = local_const_prop_count = 0;
1760*38fd1498Szrj   global_copy_prop_count = local_copy_prop_count = 0;
1761*38fd1498Szrj 
1762*38fd1498Szrj   bytes_used = 0;
1763*38fd1498Szrj   gcc_obstack_init (&cprop_obstack);
1764*38fd1498Szrj 
1765*38fd1498Szrj   /* Do a local const/copy propagation pass first.  The global pass
1766*38fd1498Szrj      only handles global opportunities.
1767*38fd1498Szrj      If the local pass changes something, remove any unreachable blocks
1768*38fd1498Szrj      because the CPROP global dataflow analysis may get into infinite
1769*38fd1498Szrj      loops for CFGs with unreachable blocks.
1770*38fd1498Szrj 
1771*38fd1498Szrj      FIXME: This local pass should not be necessary after CSE (but for
1772*38fd1498Szrj 	    some reason it still is).  It is also (proven) not necessary
1773*38fd1498Szrj 	    to run the local pass right after FWPWOP.
1774*38fd1498Szrj 
1775*38fd1498Szrj      FIXME: The global analysis would not get into infinite loops if it
1776*38fd1498Szrj 	    would use the DF solver (via df_simple_dataflow) instead of
1777*38fd1498Szrj 	    the solver implemented in this file.  */
1778*38fd1498Szrj   changed |= local_cprop_pass ();
1779*38fd1498Szrj   if (changed)
1780*38fd1498Szrj     delete_unreachable_blocks ();
1781*38fd1498Szrj 
1782*38fd1498Szrj   /* Determine implicit sets.  This may change the CFG (split critical
1783*38fd1498Szrj      edges if that exposes an implicit set).
1784*38fd1498Szrj      Note that find_implicit_sets() does not rely on up-to-date DF caches
1785*38fd1498Szrj      so that we do not have to re-run df_analyze() even if local CPROP
1786*38fd1498Szrj      changed something.
1787*38fd1498Szrj      ??? This could run earlier so that any uncovered implicit sets
1788*38fd1498Szrj 	 sets could be exploited in local_cprop_pass() also.  Later.  */
1789*38fd1498Szrj   changed |= find_implicit_sets ();
1790*38fd1498Szrj 
1791*38fd1498Szrj   /* If local_cprop_pass() or find_implicit_sets() changed something,
1792*38fd1498Szrj      run df_analyze() to bring all insn caches up-to-date, and to take
1793*38fd1498Szrj      new basic blocks from edge splitting on the DF radar.
1794*38fd1498Szrj      NB: This also runs the fast DCE pass, because execute_rtl_cprop
1795*38fd1498Szrj      sets DF_LR_RUN_DCE.  */
1796*38fd1498Szrj   if (changed)
1797*38fd1498Szrj     df_analyze ();
1798*38fd1498Szrj 
1799*38fd1498Szrj   /* Initialize implicit_set_indexes array.  */
1800*38fd1498Szrj   implicit_set_indexes = XNEWVEC (int, last_basic_block_for_fn (cfun));
1801*38fd1498Szrj   for (i = 0; i < last_basic_block_for_fn (cfun); i++)
1802*38fd1498Szrj     implicit_set_indexes[i] = -1;
1803*38fd1498Szrj 
1804*38fd1498Szrj   alloc_hash_table (&set_hash_table);
1805*38fd1498Szrj   compute_hash_table (&set_hash_table);
1806*38fd1498Szrj 
1807*38fd1498Szrj   /* Free implicit_sets before peak usage.  */
1808*38fd1498Szrj   free (implicit_sets);
1809*38fd1498Szrj   implicit_sets = NULL;
1810*38fd1498Szrj 
1811*38fd1498Szrj   if (dump_file)
1812*38fd1498Szrj     dump_hash_table (dump_file, "SET", &set_hash_table);
1813*38fd1498Szrj   if (set_hash_table.n_elems > 0)
1814*38fd1498Szrj     {
1815*38fd1498Szrj       basic_block bb;
1816*38fd1498Szrj       auto_vec<rtx_insn *> uncond_traps;
1817*38fd1498Szrj 
1818*38fd1498Szrj       alloc_cprop_mem (last_basic_block_for_fn (cfun),
1819*38fd1498Szrj 		       set_hash_table.n_elems);
1820*38fd1498Szrj       compute_cprop_data ();
1821*38fd1498Szrj 
1822*38fd1498Szrj       free (implicit_set_indexes);
1823*38fd1498Szrj       implicit_set_indexes = NULL;
1824*38fd1498Szrj 
1825*38fd1498Szrj       /* Allocate vars to track sets of regs.  */
1826*38fd1498Szrj       reg_set_bitmap = ALLOC_REG_SET (NULL);
1827*38fd1498Szrj 
1828*38fd1498Szrj       FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->next_bb,
1829*38fd1498Szrj 		      EXIT_BLOCK_PTR_FOR_FN (cfun),
1830*38fd1498Szrj 		      next_bb)
1831*38fd1498Szrj 	{
1832*38fd1498Szrj 	  bool seen_uncond_trap = false;
1833*38fd1498Szrj 	  rtx_insn *insn;
1834*38fd1498Szrj 
1835*38fd1498Szrj 	  /* Reset tables used to keep track of what's still valid [since
1836*38fd1498Szrj 	     the start of the block].  */
1837*38fd1498Szrj 	  reset_opr_set_tables ();
1838*38fd1498Szrj 
1839*38fd1498Szrj 	  FOR_BB_INSNS (bb, insn)
1840*38fd1498Szrj 	    if (INSN_P (insn))
1841*38fd1498Szrj 	      {
1842*38fd1498Szrj 		bool was_uncond_trap
1843*38fd1498Szrj 		  = (GET_CODE (PATTERN (insn)) == TRAP_IF
1844*38fd1498Szrj 		     && XEXP (PATTERN (insn), 0) == const1_rtx);
1845*38fd1498Szrj 
1846*38fd1498Szrj 		changed |= cprop_insn (insn);
1847*38fd1498Szrj 
1848*38fd1498Szrj 		/* Keep track of everything modified by this insn.  */
1849*38fd1498Szrj 		/* ??? Need to be careful w.r.t. mods done to INSN.
1850*38fd1498Szrj 		       Don't call mark_oprs_set if we turned the
1851*38fd1498Szrj 		       insn into a NOTE, or deleted the insn.  */
1852*38fd1498Szrj 		if (! NOTE_P (insn) && ! insn->deleted ())
1853*38fd1498Szrj 		  mark_oprs_set (insn);
1854*38fd1498Szrj 
1855*38fd1498Szrj 		if (!was_uncond_trap
1856*38fd1498Szrj 		    && GET_CODE (PATTERN (insn)) == TRAP_IF
1857*38fd1498Szrj 		    && XEXP (PATTERN (insn), 0) == const1_rtx)
1858*38fd1498Szrj 		  {
1859*38fd1498Szrj 		    /* If we have already seen an unconditional trap
1860*38fd1498Szrj 		       earlier, the rest of the bb is going to be removed
1861*38fd1498Szrj 		       as unreachable.  Just turn it into a note, so that
1862*38fd1498Szrj 		       RTL verification doesn't complain about it before
1863*38fd1498Szrj 		       it is finally removed.  */
1864*38fd1498Szrj 		    if (seen_uncond_trap)
1865*38fd1498Szrj 		      set_insn_deleted (insn);
1866*38fd1498Szrj 		    else
1867*38fd1498Szrj 		      {
1868*38fd1498Szrj 			seen_uncond_trap = true;
1869*38fd1498Szrj 			uncond_traps.safe_push (insn);
1870*38fd1498Szrj 		      }
1871*38fd1498Szrj 		  }
1872*38fd1498Szrj 	      }
1873*38fd1498Szrj 	}
1874*38fd1498Szrj 
1875*38fd1498Szrj       /* Make sure bypass_conditional_jumps will ignore not just its new
1876*38fd1498Szrj 	 basic blocks, but also the ones after unconditional traps (those are
1877*38fd1498Szrj 	 unreachable and will be eventually removed as such).  */
1878*38fd1498Szrj       bypass_last_basic_block = last_basic_block_for_fn (cfun);
1879*38fd1498Szrj 
1880*38fd1498Szrj       while (!uncond_traps.is_empty ())
1881*38fd1498Szrj 	{
1882*38fd1498Szrj 	  rtx_insn *insn = uncond_traps.pop ();
1883*38fd1498Szrj 	  basic_block to_split = BLOCK_FOR_INSN (insn);
1884*38fd1498Szrj 	  remove_edge (split_block (to_split, insn));
1885*38fd1498Szrj 	  emit_barrier_after_bb (to_split);
1886*38fd1498Szrj 	}
1887*38fd1498Szrj 
1888*38fd1498Szrj       changed |= bypass_conditional_jumps ();
1889*38fd1498Szrj 
1890*38fd1498Szrj       FREE_REG_SET (reg_set_bitmap);
1891*38fd1498Szrj       free_cprop_mem ();
1892*38fd1498Szrj     }
1893*38fd1498Szrj   else
1894*38fd1498Szrj     {
1895*38fd1498Szrj       free (implicit_set_indexes);
1896*38fd1498Szrj       implicit_set_indexes = NULL;
1897*38fd1498Szrj     }
1898*38fd1498Szrj 
1899*38fd1498Szrj   free_hash_table (&set_hash_table);
1900*38fd1498Szrj   obstack_free (&cprop_obstack, NULL);
1901*38fd1498Szrj 
1902*38fd1498Szrj   if (dump_file)
1903*38fd1498Szrj     {
1904*38fd1498Szrj       fprintf (dump_file, "CPROP of %s, %d basic blocks, %d bytes needed, ",
1905*38fd1498Szrj 	       current_function_name (), n_basic_blocks_for_fn (cfun),
1906*38fd1498Szrj 	       bytes_used);
1907*38fd1498Szrj       fprintf (dump_file, "%d local const props, %d local copy props, ",
1908*38fd1498Szrj 	       local_const_prop_count, local_copy_prop_count);
1909*38fd1498Szrj       fprintf (dump_file, "%d global const props, %d global copy props\n\n",
1910*38fd1498Szrj 	       global_const_prop_count, global_copy_prop_count);
1911*38fd1498Szrj     }
1912*38fd1498Szrj 
1913*38fd1498Szrj   return changed;
1914*38fd1498Szrj }
1915*38fd1498Szrj 
1916*38fd1498Szrj /* All the passes implemented in this file.  Each pass has its
1917*38fd1498Szrj    own gate and execute function, and at the end of the file a
1918*38fd1498Szrj    pass definition for passes.c.
1919*38fd1498Szrj 
1920*38fd1498Szrj    We do not construct an accurate cfg in functions which call
1921*38fd1498Szrj    setjmp, so none of these passes runs if the function calls
1922*38fd1498Szrj    setjmp.
1923*38fd1498Szrj    FIXME: Should just handle setjmp via REG_SETJMP notes.  */
1924*38fd1498Szrj 
1925*38fd1498Szrj static unsigned int
execute_rtl_cprop(void)1926*38fd1498Szrj execute_rtl_cprop (void)
1927*38fd1498Szrj {
1928*38fd1498Szrj   int changed;
1929*38fd1498Szrj   delete_unreachable_blocks ();
1930*38fd1498Szrj   df_set_flags (DF_LR_RUN_DCE);
1931*38fd1498Szrj   df_analyze ();
1932*38fd1498Szrj   changed = one_cprop_pass ();
1933*38fd1498Szrj   flag_rerun_cse_after_global_opts |= changed;
1934*38fd1498Szrj   if (changed)
1935*38fd1498Szrj     cleanup_cfg (CLEANUP_CFG_CHANGED);
1936*38fd1498Szrj   return 0;
1937*38fd1498Szrj }
1938*38fd1498Szrj 
1939*38fd1498Szrj namespace {
1940*38fd1498Szrj 
1941*38fd1498Szrj const pass_data pass_data_rtl_cprop =
1942*38fd1498Szrj {
1943*38fd1498Szrj   RTL_PASS, /* type */
1944*38fd1498Szrj   "cprop", /* name */
1945*38fd1498Szrj   OPTGROUP_NONE, /* optinfo_flags */
1946*38fd1498Szrj   TV_CPROP, /* tv_id */
1947*38fd1498Szrj   PROP_cfglayout, /* properties_required */
1948*38fd1498Szrj   0, /* properties_provided */
1949*38fd1498Szrj   0, /* properties_destroyed */
1950*38fd1498Szrj   0, /* todo_flags_start */
1951*38fd1498Szrj   TODO_df_finish, /* todo_flags_finish */
1952*38fd1498Szrj };
1953*38fd1498Szrj 
1954*38fd1498Szrj class pass_rtl_cprop : public rtl_opt_pass
1955*38fd1498Szrj {
1956*38fd1498Szrj public:
pass_rtl_cprop(gcc::context * ctxt)1957*38fd1498Szrj   pass_rtl_cprop (gcc::context *ctxt)
1958*38fd1498Szrj     : rtl_opt_pass (pass_data_rtl_cprop, ctxt)
1959*38fd1498Szrj   {}
1960*38fd1498Szrj 
1961*38fd1498Szrj   /* opt_pass methods: */
clone()1962*38fd1498Szrj   opt_pass * clone () { return new pass_rtl_cprop (m_ctxt); }
gate(function * fun)1963*38fd1498Szrj   virtual bool gate (function *fun)
1964*38fd1498Szrj     {
1965*38fd1498Szrj       return optimize > 0 && flag_gcse
1966*38fd1498Szrj 	&& !fun->calls_setjmp
1967*38fd1498Szrj 	&& dbg_cnt (cprop);
1968*38fd1498Szrj     }
1969*38fd1498Szrj 
execute(function *)1970*38fd1498Szrj   virtual unsigned int execute (function *) { return execute_rtl_cprop (); }
1971*38fd1498Szrj 
1972*38fd1498Szrj }; // class pass_rtl_cprop
1973*38fd1498Szrj 
1974*38fd1498Szrj } // anon namespace
1975*38fd1498Szrj 
1976*38fd1498Szrj rtl_opt_pass *
make_pass_rtl_cprop(gcc::context * ctxt)1977*38fd1498Szrj make_pass_rtl_cprop (gcc::context *ctxt)
1978*38fd1498Szrj {
1979*38fd1498Szrj   return new pass_rtl_cprop (ctxt);
1980*38fd1498Szrj }
1981