1*38fd1498Szrj /* Post-reload compare elimination.
2*38fd1498Szrj Copyright (C) 2010-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 /* There is a set of targets whose general-purpose move or addition
21*38fd1498Szrj instructions clobber the flags. These targets cannot split their
22*38fd1498Szrj CBRANCH/CSTORE etc patterns before reload is complete, lest reload
23*38fd1498Szrj itself insert these instructions in between the flags setter and user.
24*38fd1498Szrj Because these targets cannot split the compare from the use, they
25*38fd1498Szrj cannot make use of the comparison elimination offered by the combine pass.
26*38fd1498Szrj
27*38fd1498Szrj This is a small pass intended to provide comparison elimination similar to
28*38fd1498Szrj what is available via NOTICE_UPDATE_CC for cc0 targets. This should help
29*38fd1498Szrj encourage cc0 targets to convert to an explicit post-reload representation
30*38fd1498Szrj of the flags.
31*38fd1498Szrj
32*38fd1498Szrj This pass assumes:
33*38fd1498Szrj
34*38fd1498Szrj (0) CBRANCH/CSTORE etc have been split in pass_split_after_reload.
35*38fd1498Szrj
36*38fd1498Szrj (1) All comparison patterns are represented as
37*38fd1498Szrj
38*38fd1498Szrj [(set (reg:CC) (compare:CC (reg) (reg_or_immediate)))]
39*38fd1498Szrj
40*38fd1498Szrj (2) All insn patterns that modify the flags are represented as
41*38fd1498Szrj
42*38fd1498Szrj [(set (reg) (operation)
43*38fd1498Szrj (clobber (reg:CC))]
44*38fd1498Szrj
45*38fd1498Szrj (3) If an insn of form (2) can usefully set the flags, there is
46*38fd1498Szrj another pattern of the form
47*38fd1498Szrj
48*38fd1498Szrj [(set (reg:CCM) (compare:CCM (operation) (immediate)))
49*38fd1498Szrj (set (reg) (operation)]
50*38fd1498Szrj
51*38fd1498Szrj The mode CCM will be chosen as if by SELECT_CC_MODE.
52*38fd1498Szrj
53*38fd1498Szrj Note that unlike NOTICE_UPDATE_CC, we do not handle memory operands.
54*38fd1498Szrj This could be handled as a future enhancement.
55*38fd1498Szrj */
56*38fd1498Szrj
57*38fd1498Szrj #include "config.h"
58*38fd1498Szrj #include "system.h"
59*38fd1498Szrj #include "coretypes.h"
60*38fd1498Szrj #include "backend.h"
61*38fd1498Szrj #include "target.h"
62*38fd1498Szrj #include "rtl.h"
63*38fd1498Szrj #include "df.h"
64*38fd1498Szrj #include "memmodel.h"
65*38fd1498Szrj #include "tm_p.h"
66*38fd1498Szrj #include "insn-config.h"
67*38fd1498Szrj #include "recog.h"
68*38fd1498Szrj #include "emit-rtl.h"
69*38fd1498Szrj #include "cfgrtl.h"
70*38fd1498Szrj #include "tree-pass.h"
71*38fd1498Szrj #include "domwalk.h"
72*38fd1498Szrj
73*38fd1498Szrj
74*38fd1498Szrj /* These structures describe a comparison and how it is used. */
75*38fd1498Szrj
76*38fd1498Szrj /* The choice of maximum 3 uses comes from wanting to eliminate the two
77*38fd1498Szrj duplicate compares from a three-way branch on the sign of a value.
78*38fd1498Szrj This is also sufficient to eliminate the duplicate compare against the
79*38fd1498Szrj high-part of a double-word comparison. */
80*38fd1498Szrj #define MAX_CMP_USE 3
81*38fd1498Szrj
82*38fd1498Szrj struct comparison_use
83*38fd1498Szrj {
84*38fd1498Szrj /* The instruction in which the result of the compare is used. */
85*38fd1498Szrj rtx_insn *insn;
86*38fd1498Szrj /* The location of the flags register within the use. */
87*38fd1498Szrj rtx *loc;
88*38fd1498Szrj /* The comparison code applied against the flags register. */
89*38fd1498Szrj enum rtx_code code;
90*38fd1498Szrj };
91*38fd1498Szrj
92*38fd1498Szrj struct comparison
93*38fd1498Szrj {
94*38fd1498Szrj /* The comparison instruction. */
95*38fd1498Szrj rtx_insn *insn;
96*38fd1498Szrj
97*38fd1498Szrj /* The insn prior to the comparison insn that clobbers the flags. */
98*38fd1498Szrj rtx_insn *prev_clobber;
99*38fd1498Szrj
100*38fd1498Szrj /* The insn prior to the comparison insn that sets in_a REG. */
101*38fd1498Szrj rtx_insn *in_a_setter;
102*38fd1498Szrj
103*38fd1498Szrj /* The two values being compared. These will be either REGs or
104*38fd1498Szrj constants. */
105*38fd1498Szrj rtx in_a, in_b;
106*38fd1498Szrj
107*38fd1498Szrj /* The REG_EH_REGION of the comparison. */
108*38fd1498Szrj rtx eh_note;
109*38fd1498Szrj
110*38fd1498Szrj /* Information about how this comparison is used. */
111*38fd1498Szrj struct comparison_use uses[MAX_CMP_USE];
112*38fd1498Szrj
113*38fd1498Szrj /* The original CC_MODE for this comparison. */
114*38fd1498Szrj machine_mode orig_mode;
115*38fd1498Szrj
116*38fd1498Szrj /* The number of uses identified for this comparison. */
117*38fd1498Szrj unsigned short n_uses;
118*38fd1498Szrj
119*38fd1498Szrj /* True if not all uses of this comparison have been identified.
120*38fd1498Szrj This can happen either for overflowing the array above, or if
121*38fd1498Szrj the flags register is used in some unusual context. */
122*38fd1498Szrj bool missing_uses;
123*38fd1498Szrj
124*38fd1498Szrj /* True if its inputs are still valid at the end of the block. */
125*38fd1498Szrj bool inputs_valid;
126*38fd1498Szrj };
127*38fd1498Szrj
128*38fd1498Szrj static vec<comparison *> all_compares;
129*38fd1498Szrj
130*38fd1498Szrj /* Look for a "conforming" comparison, as defined above. If valid, return
131*38fd1498Szrj the rtx for the COMPARE itself. */
132*38fd1498Szrj
133*38fd1498Szrj static rtx
conforming_compare(rtx_insn * insn)134*38fd1498Szrj conforming_compare (rtx_insn *insn)
135*38fd1498Szrj {
136*38fd1498Szrj rtx set, src, dest;
137*38fd1498Szrj
138*38fd1498Szrj set = single_set (insn);
139*38fd1498Szrj if (set == NULL)
140*38fd1498Szrj return NULL;
141*38fd1498Szrj
142*38fd1498Szrj src = SET_SRC (set);
143*38fd1498Szrj if (GET_CODE (src) != COMPARE)
144*38fd1498Szrj return NULL;
145*38fd1498Szrj
146*38fd1498Szrj dest = SET_DEST (set);
147*38fd1498Szrj if (!REG_P (dest) || REGNO (dest) != targetm.flags_regnum)
148*38fd1498Szrj return NULL;
149*38fd1498Szrj
150*38fd1498Szrj if (!REG_P (XEXP (src, 0)))
151*38fd1498Szrj return NULL;
152*38fd1498Szrj
153*38fd1498Szrj if (CONSTANT_P (XEXP (src, 1)) || REG_P (XEXP (src, 1)))
154*38fd1498Szrj return src;
155*38fd1498Szrj
156*38fd1498Szrj if (GET_CODE (XEXP (src, 1)) == UNSPEC)
157*38fd1498Szrj {
158*38fd1498Szrj for (int i = 0; i < XVECLEN (XEXP (src, 1), 0); i++)
159*38fd1498Szrj if (!REG_P (XVECEXP (XEXP (src, 1), 0, i)))
160*38fd1498Szrj return NULL;
161*38fd1498Szrj return src;
162*38fd1498Szrj }
163*38fd1498Szrj
164*38fd1498Szrj return NULL;
165*38fd1498Szrj }
166*38fd1498Szrj
167*38fd1498Szrj /* Look for a pattern of the "correct" form for an insn with a flags clobber
168*38fd1498Szrj for which we may be able to eliminate a compare later. We're not looking
169*38fd1498Szrj to validate any inputs at this time, merely see that the basic shape is
170*38fd1498Szrj correct. The term "arithmetic" may be somewhat misleading... */
171*38fd1498Szrj
172*38fd1498Szrj static bool
arithmetic_flags_clobber_p(rtx_insn * insn)173*38fd1498Szrj arithmetic_flags_clobber_p (rtx_insn *insn)
174*38fd1498Szrj {
175*38fd1498Szrj rtx pat, x;
176*38fd1498Szrj
177*38fd1498Szrj if (!NONJUMP_INSN_P (insn))
178*38fd1498Szrj return false;
179*38fd1498Szrj pat = PATTERN (insn);
180*38fd1498Szrj if (asm_noperands (pat) >= 0)
181*38fd1498Szrj return false;
182*38fd1498Szrj
183*38fd1498Szrj if (GET_CODE (pat) == PARALLEL && XVECLEN (pat, 0) == 2)
184*38fd1498Szrj {
185*38fd1498Szrj x = XVECEXP (pat, 0, 0);
186*38fd1498Szrj if (GET_CODE (x) != SET)
187*38fd1498Szrj return false;
188*38fd1498Szrj x = SET_DEST (x);
189*38fd1498Szrj if (!REG_P (x))
190*38fd1498Szrj return false;
191*38fd1498Szrj
192*38fd1498Szrj x = XVECEXP (pat, 0, 1);
193*38fd1498Szrj if (GET_CODE (x) == CLOBBER)
194*38fd1498Szrj {
195*38fd1498Szrj x = XEXP (x, 0);
196*38fd1498Szrj if (REG_P (x) && REGNO (x) == targetm.flags_regnum)
197*38fd1498Szrj return true;
198*38fd1498Szrj }
199*38fd1498Szrj }
200*38fd1498Szrj
201*38fd1498Szrj return false;
202*38fd1498Szrj }
203*38fd1498Szrj
204*38fd1498Szrj /* Look for uses of FLAGS in INSN. If we find one we can analyze, record
205*38fd1498Szrj it in CMP; otherwise indicate that we've missed a use. */
206*38fd1498Szrj
207*38fd1498Szrj static void
find_flags_uses_in_insn(struct comparison * cmp,rtx_insn * insn)208*38fd1498Szrj find_flags_uses_in_insn (struct comparison *cmp, rtx_insn *insn)
209*38fd1498Szrj {
210*38fd1498Szrj df_ref use;
211*38fd1498Szrj
212*38fd1498Szrj /* If we've already lost track of uses, don't bother collecting more. */
213*38fd1498Szrj if (cmp->missing_uses)
214*38fd1498Szrj return;
215*38fd1498Szrj
216*38fd1498Szrj /* Find a USE of the flags register. */
217*38fd1498Szrj FOR_EACH_INSN_USE (use, insn)
218*38fd1498Szrj if (DF_REF_REGNO (use) == targetm.flags_regnum)
219*38fd1498Szrj {
220*38fd1498Szrj rtx x, *loc;
221*38fd1498Szrj
222*38fd1498Szrj /* If this is an unusual use, quit. */
223*38fd1498Szrj if (DF_REF_TYPE (use) != DF_REF_REG_USE)
224*38fd1498Szrj goto fail;
225*38fd1498Szrj
226*38fd1498Szrj /* If we've run out of slots to record uses, quit. */
227*38fd1498Szrj if (cmp->n_uses == MAX_CMP_USE)
228*38fd1498Szrj goto fail;
229*38fd1498Szrj
230*38fd1498Szrj /* Unfortunately the location of the flags register, while present
231*38fd1498Szrj in the reference structure, doesn't help. We need to find the
232*38fd1498Szrj comparison code that is outer to the actual flags use. */
233*38fd1498Szrj loc = DF_REF_LOC (use);
234*38fd1498Szrj x = PATTERN (insn);
235*38fd1498Szrj if (GET_CODE (x) == PARALLEL)
236*38fd1498Szrj x = XVECEXP (x, 0, 0);
237*38fd1498Szrj x = SET_SRC (x);
238*38fd1498Szrj if (GET_CODE (x) == IF_THEN_ELSE)
239*38fd1498Szrj x = XEXP (x, 0);
240*38fd1498Szrj if (COMPARISON_P (x)
241*38fd1498Szrj && loc == &XEXP (x, 0)
242*38fd1498Szrj && XEXP (x, 1) == const0_rtx)
243*38fd1498Szrj {
244*38fd1498Szrj /* We've found a use of the flags that we understand. */
245*38fd1498Szrj struct comparison_use *cuse = &cmp->uses[cmp->n_uses++];
246*38fd1498Szrj cuse->insn = insn;
247*38fd1498Szrj cuse->loc = loc;
248*38fd1498Szrj cuse->code = GET_CODE (x);
249*38fd1498Szrj }
250*38fd1498Szrj else
251*38fd1498Szrj goto fail;
252*38fd1498Szrj }
253*38fd1498Szrj return;
254*38fd1498Szrj
255*38fd1498Szrj fail:
256*38fd1498Szrj /* We failed to recognize this use of the flags register. */
257*38fd1498Szrj cmp->missing_uses = true;
258*38fd1498Szrj }
259*38fd1498Szrj
260*38fd1498Szrj class find_comparison_dom_walker : public dom_walker
261*38fd1498Szrj {
262*38fd1498Szrj public:
find_comparison_dom_walker(cdi_direction direction)263*38fd1498Szrj find_comparison_dom_walker (cdi_direction direction)
264*38fd1498Szrj : dom_walker (direction) {}
265*38fd1498Szrj
266*38fd1498Szrj virtual edge before_dom_children (basic_block);
267*38fd1498Szrj };
268*38fd1498Szrj
269*38fd1498Szrj /* Return true if conforming COMPARE with EH_NOTE is redundant with comparison
270*38fd1498Szrj CMP and can thus be eliminated. */
271*38fd1498Szrj
272*38fd1498Szrj static bool
can_eliminate_compare(rtx compare,rtx eh_note,struct comparison * cmp)273*38fd1498Szrj can_eliminate_compare (rtx compare, rtx eh_note, struct comparison *cmp)
274*38fd1498Szrj {
275*38fd1498Szrj /* Take care that it's in the same EH region. */
276*38fd1498Szrj if (cfun->can_throw_non_call_exceptions
277*38fd1498Szrj && !rtx_equal_p (eh_note, cmp->eh_note))
278*38fd1498Szrj return false;
279*38fd1498Szrj
280*38fd1498Szrj /* Make sure the compare is redundant with the previous. */
281*38fd1498Szrj if (!rtx_equal_p (XEXP (compare, 0), cmp->in_a)
282*38fd1498Szrj || !rtx_equal_p (XEXP (compare, 1), cmp->in_b))
283*38fd1498Szrj return false;
284*38fd1498Szrj
285*38fd1498Szrj /* New mode must be compatible with the previous compare mode. */
286*38fd1498Szrj machine_mode new_mode
287*38fd1498Szrj = targetm.cc_modes_compatible (GET_MODE (compare), cmp->orig_mode);
288*38fd1498Szrj
289*38fd1498Szrj if (new_mode == VOIDmode)
290*38fd1498Szrj return false;
291*38fd1498Szrj
292*38fd1498Szrj if (cmp->orig_mode != new_mode)
293*38fd1498Szrj {
294*38fd1498Szrj /* Generate new comparison for substitution. */
295*38fd1498Szrj rtx flags = gen_rtx_REG (new_mode, targetm.flags_regnum);
296*38fd1498Szrj rtx x = gen_rtx_COMPARE (new_mode, cmp->in_a, cmp->in_b);
297*38fd1498Szrj x = gen_rtx_SET (flags, x);
298*38fd1498Szrj
299*38fd1498Szrj if (!validate_change (cmp->insn, &PATTERN (cmp->insn), x, false))
300*38fd1498Szrj return false;
301*38fd1498Szrj
302*38fd1498Szrj cmp->orig_mode = new_mode;
303*38fd1498Szrj }
304*38fd1498Szrj
305*38fd1498Szrj return true;
306*38fd1498Szrj }
307*38fd1498Szrj
308*38fd1498Szrj /* Identify comparison instructions within BB. If the flags from the last
309*38fd1498Szrj compare in the BB is live at the end of the block, install the compare
310*38fd1498Szrj in BB->AUX. Called via dom_walker.walk (). */
311*38fd1498Szrj
312*38fd1498Szrj edge
before_dom_children(basic_block bb)313*38fd1498Szrj find_comparison_dom_walker::before_dom_children (basic_block bb)
314*38fd1498Szrj {
315*38fd1498Szrj rtx_insn *insn, *next;
316*38fd1498Szrj bool need_purge = false;
317*38fd1498Szrj rtx_insn *last_setter[FIRST_PSEUDO_REGISTER];
318*38fd1498Szrj
319*38fd1498Szrj /* The last comparison that was made. Will be reset to NULL
320*38fd1498Szrj once the flags are clobbered. */
321*38fd1498Szrj struct comparison *last_cmp = NULL;
322*38fd1498Szrj
323*38fd1498Szrj /* True iff the last comparison has not been clobbered, nor
324*38fd1498Szrj have its inputs. Used to eliminate duplicate compares. */
325*38fd1498Szrj bool last_cmp_valid = false;
326*38fd1498Szrj
327*38fd1498Szrj /* The last insn that clobbered the flags, if that insn is of
328*38fd1498Szrj a form that may be valid for eliminating a following compare.
329*38fd1498Szrj To be reset to NULL once the flags are set otherwise. */
330*38fd1498Szrj rtx_insn *last_clobber = NULL;
331*38fd1498Szrj
332*38fd1498Szrj /* Propagate the last live comparison throughout the extended basic block. */
333*38fd1498Szrj if (single_pred_p (bb))
334*38fd1498Szrj {
335*38fd1498Szrj last_cmp = (struct comparison *) single_pred (bb)->aux;
336*38fd1498Szrj if (last_cmp)
337*38fd1498Szrj last_cmp_valid = last_cmp->inputs_valid;
338*38fd1498Szrj }
339*38fd1498Szrj
340*38fd1498Szrj memset (last_setter, 0, sizeof (last_setter));
341*38fd1498Szrj for (insn = BB_HEAD (bb); insn; insn = next)
342*38fd1498Szrj {
343*38fd1498Szrj rtx src;
344*38fd1498Szrj
345*38fd1498Szrj next = (insn == BB_END (bb) ? NULL : NEXT_INSN (insn));
346*38fd1498Szrj if (!NONDEBUG_INSN_P (insn))
347*38fd1498Szrj continue;
348*38fd1498Szrj
349*38fd1498Szrj src = conforming_compare (insn);
350*38fd1498Szrj if (src)
351*38fd1498Szrj {
352*38fd1498Szrj rtx eh_note = NULL;
353*38fd1498Szrj
354*38fd1498Szrj if (cfun->can_throw_non_call_exceptions)
355*38fd1498Szrj eh_note = find_reg_note (insn, REG_EH_REGION, NULL);
356*38fd1498Szrj
357*38fd1498Szrj if (last_cmp_valid && can_eliminate_compare (src, eh_note, last_cmp))
358*38fd1498Szrj {
359*38fd1498Szrj if (eh_note)
360*38fd1498Szrj need_purge = true;
361*38fd1498Szrj delete_insn (insn);
362*38fd1498Szrj continue;
363*38fd1498Szrj }
364*38fd1498Szrj
365*38fd1498Szrj last_cmp = XCNEW (struct comparison);
366*38fd1498Szrj last_cmp->insn = insn;
367*38fd1498Szrj last_cmp->prev_clobber = last_clobber;
368*38fd1498Szrj last_cmp->in_a = XEXP (src, 0);
369*38fd1498Szrj last_cmp->in_b = XEXP (src, 1);
370*38fd1498Szrj last_cmp->eh_note = eh_note;
371*38fd1498Szrj last_cmp->orig_mode = GET_MODE (src);
372*38fd1498Szrj if (last_cmp->in_b == const0_rtx
373*38fd1498Szrj && last_setter[REGNO (last_cmp->in_a)])
374*38fd1498Szrj {
375*38fd1498Szrj rtx set = single_set (last_setter[REGNO (last_cmp->in_a)]);
376*38fd1498Szrj if (set && rtx_equal_p (SET_DEST (set), last_cmp->in_a))
377*38fd1498Szrj last_cmp->in_a_setter = last_setter[REGNO (last_cmp->in_a)];
378*38fd1498Szrj }
379*38fd1498Szrj all_compares.safe_push (last_cmp);
380*38fd1498Szrj
381*38fd1498Szrj /* It's unusual, but be prepared for comparison patterns that
382*38fd1498Szrj also clobber an input, or perhaps a scratch. */
383*38fd1498Szrj last_clobber = NULL;
384*38fd1498Szrj last_cmp_valid = true;
385*38fd1498Szrj }
386*38fd1498Szrj
387*38fd1498Szrj else
388*38fd1498Szrj {
389*38fd1498Szrj /* Notice if this instruction uses the flags register. */
390*38fd1498Szrj if (last_cmp)
391*38fd1498Szrj find_flags_uses_in_insn (last_cmp, insn);
392*38fd1498Szrj
393*38fd1498Szrj /* Notice if this instruction kills the flags register. */
394*38fd1498Szrj df_ref def;
395*38fd1498Szrj FOR_EACH_INSN_DEF (def, insn)
396*38fd1498Szrj if (DF_REF_REGNO (def) == targetm.flags_regnum)
397*38fd1498Szrj {
398*38fd1498Szrj /* See if this insn could be the "clobber" that eliminates
399*38fd1498Szrj a future comparison. */
400*38fd1498Szrj last_clobber = (arithmetic_flags_clobber_p (insn)
401*38fd1498Szrj ? insn : NULL);
402*38fd1498Szrj
403*38fd1498Szrj /* In either case, the previous compare is no longer valid. */
404*38fd1498Szrj last_cmp = NULL;
405*38fd1498Szrj last_cmp_valid = false;
406*38fd1498Szrj break;
407*38fd1498Szrj }
408*38fd1498Szrj }
409*38fd1498Szrj
410*38fd1498Szrj /* Notice if any of the inputs to the comparison have changed
411*38fd1498Szrj and remember last insn that sets each register. */
412*38fd1498Szrj df_ref def;
413*38fd1498Szrj FOR_EACH_INSN_DEF (def, insn)
414*38fd1498Szrj {
415*38fd1498Szrj if (last_cmp_valid
416*38fd1498Szrj && (DF_REF_REGNO (def) == REGNO (last_cmp->in_a)
417*38fd1498Szrj || (REG_P (last_cmp->in_b)
418*38fd1498Szrj && DF_REF_REGNO (def) == REGNO (last_cmp->in_b))))
419*38fd1498Szrj last_cmp_valid = false;
420*38fd1498Szrj last_setter[DF_REF_REGNO (def)] = insn;
421*38fd1498Szrj }
422*38fd1498Szrj }
423*38fd1498Szrj
424*38fd1498Szrj /* Remember the live comparison for subsequent members of
425*38fd1498Szrj the extended basic block. */
426*38fd1498Szrj if (last_cmp)
427*38fd1498Szrj {
428*38fd1498Szrj bb->aux = last_cmp;
429*38fd1498Szrj last_cmp->inputs_valid = last_cmp_valid;
430*38fd1498Szrj
431*38fd1498Szrj /* Look to see if the flags register is live outgoing here, and
432*38fd1498Szrj incoming to any successor not part of the extended basic block. */
433*38fd1498Szrj if (bitmap_bit_p (df_get_live_out (bb), targetm.flags_regnum))
434*38fd1498Szrj {
435*38fd1498Szrj edge e;
436*38fd1498Szrj edge_iterator ei;
437*38fd1498Szrj
438*38fd1498Szrj FOR_EACH_EDGE (e, ei, bb->succs)
439*38fd1498Szrj {
440*38fd1498Szrj basic_block dest = e->dest;
441*38fd1498Szrj if (bitmap_bit_p (df_get_live_in (bb), targetm.flags_regnum)
442*38fd1498Szrj && !single_pred_p (dest))
443*38fd1498Szrj {
444*38fd1498Szrj last_cmp->missing_uses = true;
445*38fd1498Szrj break;
446*38fd1498Szrj }
447*38fd1498Szrj }
448*38fd1498Szrj }
449*38fd1498Szrj }
450*38fd1498Szrj
451*38fd1498Szrj /* If we deleted a compare with a REG_EH_REGION note, we may need to
452*38fd1498Szrj remove EH edges. */
453*38fd1498Szrj if (need_purge)
454*38fd1498Szrj purge_dead_edges (bb);
455*38fd1498Szrj
456*38fd1498Szrj return NULL;
457*38fd1498Szrj }
458*38fd1498Szrj
459*38fd1498Szrj /* Find all comparisons in the function. */
460*38fd1498Szrj
461*38fd1498Szrj static void
find_comparisons(void)462*38fd1498Szrj find_comparisons (void)
463*38fd1498Szrj {
464*38fd1498Szrj calculate_dominance_info (CDI_DOMINATORS);
465*38fd1498Szrj
466*38fd1498Szrj find_comparison_dom_walker (CDI_DOMINATORS)
467*38fd1498Szrj .walk (cfun->cfg->x_entry_block_ptr);
468*38fd1498Szrj
469*38fd1498Szrj clear_aux_for_blocks ();
470*38fd1498Szrj free_dominance_info (CDI_DOMINATORS);
471*38fd1498Szrj }
472*38fd1498Szrj
473*38fd1498Szrj /* Select an alternate CC_MODE for a comparison insn comparing A and B.
474*38fd1498Szrj Note that inputs are almost certainly different than the IN_A and IN_B
475*38fd1498Szrj stored in CMP -- we're called while attempting to eliminate the compare
476*38fd1498Szrj after all. Return the new FLAGS rtx if successful, else return NULL.
477*38fd1498Szrj Note that this function may start a change group. */
478*38fd1498Szrj
479*38fd1498Szrj static rtx
maybe_select_cc_mode(struct comparison * cmp,rtx a ATTRIBUTE_UNUSED,rtx b ATTRIBUTE_UNUSED)480*38fd1498Szrj maybe_select_cc_mode (struct comparison *cmp, rtx a ATTRIBUTE_UNUSED,
481*38fd1498Szrj rtx b ATTRIBUTE_UNUSED)
482*38fd1498Szrj {
483*38fd1498Szrj machine_mode sel_mode;
484*38fd1498Szrj const int n = cmp->n_uses;
485*38fd1498Szrj rtx flags = NULL;
486*38fd1498Szrj
487*38fd1498Szrj #ifndef SELECT_CC_MODE
488*38fd1498Szrj /* Minimize code differences when this target macro is undefined. */
489*38fd1498Szrj return NULL;
490*38fd1498Szrj #define SELECT_CC_MODE(A,B,C) (gcc_unreachable (), VOIDmode)
491*38fd1498Szrj #endif
492*38fd1498Szrj
493*38fd1498Szrj /* If we don't have access to all of the uses, we can't validate. */
494*38fd1498Szrj if (cmp->missing_uses || n == 0)
495*38fd1498Szrj return NULL;
496*38fd1498Szrj
497*38fd1498Szrj /* Find a new mode that works for all of the uses. Special case the
498*38fd1498Szrj common case of exactly one use. */
499*38fd1498Szrj if (n == 1)
500*38fd1498Szrj {
501*38fd1498Szrj sel_mode = SELECT_CC_MODE (cmp->uses[0].code, a, b);
502*38fd1498Szrj if (sel_mode != cmp->orig_mode)
503*38fd1498Szrj {
504*38fd1498Szrj flags = gen_rtx_REG (sel_mode, targetm.flags_regnum);
505*38fd1498Szrj validate_change (cmp->uses[0].insn, cmp->uses[0].loc, flags, true);
506*38fd1498Szrj }
507*38fd1498Szrj }
508*38fd1498Szrj else
509*38fd1498Szrj {
510*38fd1498Szrj int i;
511*38fd1498Szrj
512*38fd1498Szrj sel_mode = SELECT_CC_MODE (cmp->uses[0].code, a, b);
513*38fd1498Szrj for (i = 1; i < n; ++i)
514*38fd1498Szrj {
515*38fd1498Szrj machine_mode new_mode = SELECT_CC_MODE (cmp->uses[i].code, a, b);
516*38fd1498Szrj if (new_mode != sel_mode)
517*38fd1498Szrj {
518*38fd1498Szrj sel_mode = targetm.cc_modes_compatible (sel_mode, new_mode);
519*38fd1498Szrj if (sel_mode == VOIDmode)
520*38fd1498Szrj return NULL;
521*38fd1498Szrj }
522*38fd1498Szrj }
523*38fd1498Szrj
524*38fd1498Szrj if (sel_mode != cmp->orig_mode)
525*38fd1498Szrj {
526*38fd1498Szrj flags = gen_rtx_REG (sel_mode, targetm.flags_regnum);
527*38fd1498Szrj for (i = 0; i < n; ++i)
528*38fd1498Szrj validate_change (cmp->uses[i].insn, cmp->uses[i].loc, flags, true);
529*38fd1498Szrj }
530*38fd1498Szrj }
531*38fd1498Szrj
532*38fd1498Szrj return flags;
533*38fd1498Szrj }
534*38fd1498Szrj
535*38fd1498Szrj /* Return a register RTX holding the same value at START as REG at END, or
536*38fd1498Szrj NULL_RTX if there is none. */
537*38fd1498Szrj
538*38fd1498Szrj static rtx
equivalent_reg_at_start(rtx reg,rtx_insn * end,rtx_insn * start)539*38fd1498Szrj equivalent_reg_at_start (rtx reg, rtx_insn *end, rtx_insn *start)
540*38fd1498Szrj {
541*38fd1498Szrj machine_mode orig_mode = GET_MODE (reg);
542*38fd1498Szrj rtx_insn *bb_head = BB_HEAD (BLOCK_FOR_INSN (end));
543*38fd1498Szrj
544*38fd1498Szrj for (rtx_insn *insn = PREV_INSN (end);
545*38fd1498Szrj insn != start;
546*38fd1498Szrj insn = PREV_INSN (insn))
547*38fd1498Szrj {
548*38fd1498Szrj const int abnormal_flags
549*38fd1498Szrj = (DF_REF_CONDITIONAL | DF_REF_PARTIAL | DF_REF_MAY_CLOBBER
550*38fd1498Szrj | DF_REF_MUST_CLOBBER | DF_REF_SIGN_EXTRACT
551*38fd1498Szrj | DF_REF_ZERO_EXTRACT | DF_REF_STRICT_LOW_PART
552*38fd1498Szrj | DF_REF_PRE_POST_MODIFY);
553*38fd1498Szrj df_ref def;
554*38fd1498Szrj
555*38fd1498Szrj /* Note that the BB_HEAD is always either a note or a label, but in
556*38fd1498Szrj any case it means that REG is defined outside the block. */
557*38fd1498Szrj if (insn == bb_head)
558*38fd1498Szrj return NULL_RTX;
559*38fd1498Szrj if (NOTE_P (insn) || DEBUG_INSN_P (insn))
560*38fd1498Szrj continue;
561*38fd1498Szrj
562*38fd1498Szrj /* Find a possible def of REG in INSN. */
563*38fd1498Szrj FOR_EACH_INSN_DEF (def, insn)
564*38fd1498Szrj if (DF_REF_REGNO (def) == REGNO (reg))
565*38fd1498Szrj break;
566*38fd1498Szrj
567*38fd1498Szrj /* No definitions of REG; continue searching. */
568*38fd1498Szrj if (def == NULL)
569*38fd1498Szrj continue;
570*38fd1498Szrj
571*38fd1498Szrj /* Bail if this is not a totally normal set of REG. */
572*38fd1498Szrj if (DF_REF_IS_ARTIFICIAL (def))
573*38fd1498Szrj return NULL_RTX;
574*38fd1498Szrj if (DF_REF_FLAGS (def) & abnormal_flags)
575*38fd1498Szrj return NULL_RTX;
576*38fd1498Szrj
577*38fd1498Szrj /* We've found an insn between the compare and the clobber that sets
578*38fd1498Szrj REG. Given that pass_cprop_hardreg has not yet run, we still find
579*38fd1498Szrj situations in which we can usefully look through a copy insn. */
580*38fd1498Szrj rtx x = single_set (insn);
581*38fd1498Szrj if (x == NULL_RTX)
582*38fd1498Szrj return NULL_RTX;
583*38fd1498Szrj reg = SET_SRC (x);
584*38fd1498Szrj if (!REG_P (reg))
585*38fd1498Szrj return NULL_RTX;
586*38fd1498Szrj }
587*38fd1498Szrj
588*38fd1498Szrj if (GET_MODE (reg) != orig_mode)
589*38fd1498Szrj return NULL_RTX;
590*38fd1498Szrj
591*38fd1498Szrj return reg;
592*38fd1498Szrj }
593*38fd1498Szrj
594*38fd1498Szrj /* Return true if it is okay to merge the comparison CMP_INSN with
595*38fd1498Szrj the instruction ARITH_INSN. Both instructions are assumed to be in the
596*38fd1498Szrj same basic block with ARITH_INSN appearing before CMP_INSN. This checks
597*38fd1498Szrj that there are no uses or defs of the condition flags or control flow
598*38fd1498Szrj changes between the two instructions. */
599*38fd1498Szrj
600*38fd1498Szrj static bool
can_merge_compare_into_arith(rtx_insn * cmp_insn,rtx_insn * arith_insn)601*38fd1498Szrj can_merge_compare_into_arith (rtx_insn *cmp_insn, rtx_insn *arith_insn)
602*38fd1498Szrj {
603*38fd1498Szrj for (rtx_insn *insn = PREV_INSN (cmp_insn);
604*38fd1498Szrj insn && insn != arith_insn;
605*38fd1498Szrj insn = PREV_INSN (insn))
606*38fd1498Szrj {
607*38fd1498Szrj if (!NONDEBUG_INSN_P (insn))
608*38fd1498Szrj continue;
609*38fd1498Szrj /* Bail if there are jumps or calls in between. */
610*38fd1498Szrj if (!NONJUMP_INSN_P (insn))
611*38fd1498Szrj return false;
612*38fd1498Szrj
613*38fd1498Szrj /* Bail on old-style asm statements because they lack
614*38fd1498Szrj data flow information. */
615*38fd1498Szrj if (GET_CODE (PATTERN (insn)) == ASM_INPUT)
616*38fd1498Szrj return false;
617*38fd1498Szrj
618*38fd1498Szrj df_ref ref;
619*38fd1498Szrj /* Find a USE of the flags register. */
620*38fd1498Szrj FOR_EACH_INSN_USE (ref, insn)
621*38fd1498Szrj if (DF_REF_REGNO (ref) == targetm.flags_regnum)
622*38fd1498Szrj return false;
623*38fd1498Szrj
624*38fd1498Szrj /* Find a DEF of the flags register. */
625*38fd1498Szrj FOR_EACH_INSN_DEF (ref, insn)
626*38fd1498Szrj if (DF_REF_REGNO (ref) == targetm.flags_regnum)
627*38fd1498Szrj return false;
628*38fd1498Szrj }
629*38fd1498Szrj return true;
630*38fd1498Szrj }
631*38fd1498Szrj
632*38fd1498Szrj /* Given two SET expressions, SET_A and SET_B determine whether they form
633*38fd1498Szrj a recognizable pattern when emitted in parallel. Return that parallel
634*38fd1498Szrj if so. Otherwise return NULL. */
635*38fd1498Szrj
636*38fd1498Szrj static rtx
try_validate_parallel(rtx set_a,rtx set_b)637*38fd1498Szrj try_validate_parallel (rtx set_a, rtx set_b)
638*38fd1498Szrj {
639*38fd1498Szrj rtx par = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set_a, set_b));
640*38fd1498Szrj rtx_insn *insn = make_insn_raw (par);
641*38fd1498Szrj
642*38fd1498Szrj if (insn_invalid_p (insn, false))
643*38fd1498Szrj {
644*38fd1498Szrj crtl->emit.x_cur_insn_uid--;
645*38fd1498Szrj return NULL_RTX;
646*38fd1498Szrj }
647*38fd1498Szrj
648*38fd1498Szrj SET_PREV_INSN (insn) = NULL_RTX;
649*38fd1498Szrj SET_NEXT_INSN (insn) = NULL_RTX;
650*38fd1498Szrj INSN_LOCATION (insn) = 0;
651*38fd1498Szrj return insn;
652*38fd1498Szrj }
653*38fd1498Szrj
654*38fd1498Szrj /* For a comparison instruction described by CMP check if it compares a
655*38fd1498Szrj register with zero i.e. it is of the form CC := CMP R1, 0.
656*38fd1498Szrj If it is, find the instruction defining R1 (say I1) and try to create a
657*38fd1498Szrj PARALLEL consisting of I1 and the comparison, representing a flag-setting
658*38fd1498Szrj arithmetic instruction. Example:
659*38fd1498Szrj I1: R1 := R2 + R3
660*38fd1498Szrj <instructions that don't read the condition register>
661*38fd1498Szrj I2: CC := CMP R1 0
662*38fd1498Szrj I2 can be merged with I1 into:
663*38fd1498Szrj I1: { CC := CMP (R2 + R3) 0 ; R1 := R2 + R3 }
664*38fd1498Szrj This catches cases where R1 is used between I1 and I2 and therefore
665*38fd1498Szrj combine and other RTL optimisations will not try to propagate it into
666*38fd1498Szrj I2. Return true if we succeeded in merging CMP. */
667*38fd1498Szrj
668*38fd1498Szrj static bool
try_merge_compare(struct comparison * cmp)669*38fd1498Szrj try_merge_compare (struct comparison *cmp)
670*38fd1498Szrj {
671*38fd1498Szrj rtx_insn *cmp_insn = cmp->insn;
672*38fd1498Szrj
673*38fd1498Szrj if (cmp->in_b != const0_rtx || cmp->in_a_setter == NULL)
674*38fd1498Szrj return false;
675*38fd1498Szrj rtx in_a = cmp->in_a;
676*38fd1498Szrj df_ref use;
677*38fd1498Szrj
678*38fd1498Szrj FOR_EACH_INSN_USE (use, cmp_insn)
679*38fd1498Szrj if (DF_REF_REGNO (use) == REGNO (in_a))
680*38fd1498Szrj break;
681*38fd1498Szrj if (!use)
682*38fd1498Szrj return false;
683*38fd1498Szrj
684*38fd1498Szrj rtx_insn *def_insn = cmp->in_a_setter;
685*38fd1498Szrj rtx set = single_set (def_insn);
686*38fd1498Szrj if (!set)
687*38fd1498Szrj return false;
688*38fd1498Szrj
689*38fd1498Szrj if (!can_merge_compare_into_arith (cmp_insn, def_insn))
690*38fd1498Szrj return false;
691*38fd1498Szrj
692*38fd1498Szrj rtx src = SET_SRC (set);
693*38fd1498Szrj rtx flags = maybe_select_cc_mode (cmp, src, CONST0_RTX (GET_MODE (src)));
694*38fd1498Szrj if (!flags)
695*38fd1498Szrj {
696*38fd1498Szrj /* We may already have a change group going through maybe_select_cc_mode.
697*38fd1498Szrj Discard it properly. */
698*38fd1498Szrj cancel_changes (0);
699*38fd1498Szrj return false;
700*38fd1498Szrj }
701*38fd1498Szrj
702*38fd1498Szrj rtx flag_set
703*38fd1498Szrj = gen_rtx_SET (flags, gen_rtx_COMPARE (GET_MODE (flags),
704*38fd1498Szrj copy_rtx (src),
705*38fd1498Szrj CONST0_RTX (GET_MODE (src))));
706*38fd1498Szrj rtx arith_set = copy_rtx (PATTERN (def_insn));
707*38fd1498Szrj rtx par = try_validate_parallel (flag_set, arith_set);
708*38fd1498Szrj if (!par)
709*38fd1498Szrj {
710*38fd1498Szrj /* We may already have a change group going through maybe_select_cc_mode.
711*38fd1498Szrj Discard it properly. */
712*38fd1498Szrj cancel_changes (0);
713*38fd1498Szrj return false;
714*38fd1498Szrj }
715*38fd1498Szrj if (!apply_change_group ())
716*38fd1498Szrj return false;
717*38fd1498Szrj emit_insn_after (par, def_insn);
718*38fd1498Szrj delete_insn (def_insn);
719*38fd1498Szrj delete_insn (cmp->insn);
720*38fd1498Szrj return true;
721*38fd1498Szrj }
722*38fd1498Szrj
723*38fd1498Szrj /* Attempt to replace a comparison with a prior arithmetic insn that can
724*38fd1498Szrj compute the same flags value as the comparison itself. Return true if
725*38fd1498Szrj successful, having made all rtl modifications necessary. */
726*38fd1498Szrj
727*38fd1498Szrj static bool
try_eliminate_compare(struct comparison * cmp)728*38fd1498Szrj try_eliminate_compare (struct comparison *cmp)
729*38fd1498Szrj {
730*38fd1498Szrj rtx flags, in_a, in_b, cmp_src;
731*38fd1498Szrj
732*38fd1498Szrj if (try_merge_compare (cmp))
733*38fd1498Szrj return true;
734*38fd1498Szrj
735*38fd1498Szrj /* We must have found an interesting "clobber" preceding the compare. */
736*38fd1498Szrj if (cmp->prev_clobber == NULL)
737*38fd1498Szrj return false;
738*38fd1498Szrj
739*38fd1498Szrj /* Verify that IN_A is not clobbered in between CMP and PREV_CLOBBER.
740*38fd1498Szrj Given that this target requires this pass, we can assume that most
741*38fd1498Szrj insns do clobber the flags, and so the distance between the compare
742*38fd1498Szrj and the clobber is likely to be small. */
743*38fd1498Szrj /* ??? This is one point at which one could argue that DF_REF_CHAIN would
744*38fd1498Szrj be useful, but it is thought to be too heavy-weight a solution here. */
745*38fd1498Szrj in_a = equivalent_reg_at_start (cmp->in_a, cmp->insn, cmp->prev_clobber);
746*38fd1498Szrj if (!in_a)
747*38fd1498Szrj return false;
748*38fd1498Szrj
749*38fd1498Szrj /* Likewise for IN_B if need be. */
750*38fd1498Szrj if (CONSTANT_P (cmp->in_b))
751*38fd1498Szrj in_b = cmp->in_b;
752*38fd1498Szrj else if (REG_P (cmp->in_b))
753*38fd1498Szrj {
754*38fd1498Szrj in_b = equivalent_reg_at_start (cmp->in_b, cmp->insn, cmp->prev_clobber);
755*38fd1498Szrj if (!in_b)
756*38fd1498Szrj return false;
757*38fd1498Szrj }
758*38fd1498Szrj else if (GET_CODE (cmp->in_b) == UNSPEC)
759*38fd1498Szrj {
760*38fd1498Szrj const int len = XVECLEN (cmp->in_b, 0);
761*38fd1498Szrj rtvec v = rtvec_alloc (len);
762*38fd1498Szrj for (int i = 0; i < len; i++)
763*38fd1498Szrj {
764*38fd1498Szrj rtx r = equivalent_reg_at_start (XVECEXP (cmp->in_b, 0, i),
765*38fd1498Szrj cmp->insn, cmp->prev_clobber);
766*38fd1498Szrj if (!r)
767*38fd1498Szrj return false;
768*38fd1498Szrj RTVEC_ELT (v, i) = r;
769*38fd1498Szrj }
770*38fd1498Szrj in_b = gen_rtx_UNSPEC (GET_MODE (cmp->in_b), v, XINT (cmp->in_b, 1));
771*38fd1498Szrj }
772*38fd1498Szrj else
773*38fd1498Szrj gcc_unreachable ();
774*38fd1498Szrj
775*38fd1498Szrj /* We've reached PREV_CLOBBER without finding a modification of IN_A.
776*38fd1498Szrj Validate that PREV_CLOBBER itself does in fact refer to IN_A. Do
777*38fd1498Szrj recall that we've already validated the shape of PREV_CLOBBER. */
778*38fd1498Szrj rtx_insn *insn = cmp->prev_clobber;
779*38fd1498Szrj
780*38fd1498Szrj rtx x = XVECEXP (PATTERN (insn), 0, 0);
781*38fd1498Szrj if (rtx_equal_p (SET_DEST (x), in_a))
782*38fd1498Szrj cmp_src = SET_SRC (x);
783*38fd1498Szrj
784*38fd1498Szrj /* Also check operations with implicit extensions, e.g.:
785*38fd1498Szrj [(set (reg:DI)
786*38fd1498Szrj (zero_extend:DI (plus:SI (reg:SI) (reg:SI))))
787*38fd1498Szrj (set (reg:CCZ flags)
788*38fd1498Szrj (compare:CCZ (plus:SI (reg:SI) (reg:SI))
789*38fd1498Szrj (const_int 0)))] */
790*38fd1498Szrj else if (REG_P (SET_DEST (x))
791*38fd1498Szrj && REG_P (in_a)
792*38fd1498Szrj && REGNO (SET_DEST (x)) == REGNO (in_a)
793*38fd1498Szrj && (GET_CODE (SET_SRC (x)) == ZERO_EXTEND
794*38fd1498Szrj || GET_CODE (SET_SRC (x)) == SIGN_EXTEND)
795*38fd1498Szrj && GET_MODE (XEXP (SET_SRC (x), 0)) == GET_MODE (in_a))
796*38fd1498Szrj cmp_src = XEXP (SET_SRC (x), 0);
797*38fd1498Szrj
798*38fd1498Szrj /* Also check fully redundant comparisons, e.g.:
799*38fd1498Szrj [(set (reg:SI)
800*38fd1498Szrj (minus:SI (reg:SI) (reg:SI))))
801*38fd1498Szrj (set (reg:CC flags)
802*38fd1498Szrj (compare:CC (reg:SI) (reg:SI)))] */
803*38fd1498Szrj else if (REG_P (in_b)
804*38fd1498Szrj && GET_CODE (SET_SRC (x)) == MINUS
805*38fd1498Szrj && rtx_equal_p (XEXP (SET_SRC (x), 0), in_a)
806*38fd1498Szrj && rtx_equal_p (XEXP (SET_SRC (x), 1), in_b))
807*38fd1498Szrj cmp_src = in_a;
808*38fd1498Szrj
809*38fd1498Szrj else
810*38fd1498Szrj return false;
811*38fd1498Szrj
812*38fd1498Szrj /* Determine if we ought to use a different CC_MODE here. */
813*38fd1498Szrj flags = maybe_select_cc_mode (cmp, cmp_src, in_b);
814*38fd1498Szrj if (flags == NULL)
815*38fd1498Szrj flags = gen_rtx_REG (cmp->orig_mode, targetm.flags_regnum);
816*38fd1498Szrj
817*38fd1498Szrj /* Generate a new comparison for installation in the setter. */
818*38fd1498Szrj rtx y = copy_rtx (cmp_src);
819*38fd1498Szrj y = gen_rtx_COMPARE (GET_MODE (flags), y, in_b);
820*38fd1498Szrj y = gen_rtx_SET (flags, y);
821*38fd1498Szrj
822*38fd1498Szrj /* Canonicalize instruction to:
823*38fd1498Szrj [(set (reg:CCM) (compare:CCM (operation) (immediate)))
824*38fd1498Szrj (set (reg) (operation)] */
825*38fd1498Szrj
826*38fd1498Szrj rtvec v = rtvec_alloc (2);
827*38fd1498Szrj RTVEC_ELT (v, 0) = y;
828*38fd1498Szrj RTVEC_ELT (v, 1) = x;
829*38fd1498Szrj
830*38fd1498Szrj rtx pat = gen_rtx_PARALLEL (VOIDmode, v);
831*38fd1498Szrj
832*38fd1498Szrj /* Succeed if the new instruction is valid. Note that we may have started
833*38fd1498Szrj a change group within maybe_select_cc_mode, therefore we must continue. */
834*38fd1498Szrj validate_change (insn, &PATTERN (insn), pat, true);
835*38fd1498Szrj
836*38fd1498Szrj if (!apply_change_group ())
837*38fd1498Szrj return false;
838*38fd1498Szrj
839*38fd1498Szrj /* Success. Delete the compare insn... */
840*38fd1498Szrj delete_insn (cmp->insn);
841*38fd1498Szrj
842*38fd1498Szrj /* ... and any notes that are now invalid due to multiple sets. */
843*38fd1498Szrj x = find_regno_note (insn, REG_UNUSED, targetm.flags_regnum);
844*38fd1498Szrj if (x)
845*38fd1498Szrj remove_note (insn, x);
846*38fd1498Szrj x = find_reg_note (insn, REG_EQUAL, NULL);
847*38fd1498Szrj if (x)
848*38fd1498Szrj remove_note (insn, x);
849*38fd1498Szrj x = find_reg_note (insn, REG_EQUIV, NULL);
850*38fd1498Szrj if (x)
851*38fd1498Szrj remove_note (insn, x);
852*38fd1498Szrj
853*38fd1498Szrj return true;
854*38fd1498Szrj }
855*38fd1498Szrj
856*38fd1498Szrj /* Main entry point to the pass. */
857*38fd1498Szrj
858*38fd1498Szrj static unsigned int
execute_compare_elim_after_reload(void)859*38fd1498Szrj execute_compare_elim_after_reload (void)
860*38fd1498Szrj {
861*38fd1498Szrj df_analyze ();
862*38fd1498Szrj
863*38fd1498Szrj gcc_checking_assert (!all_compares.exists ());
864*38fd1498Szrj
865*38fd1498Szrj /* Locate all comparisons and their uses, and eliminate duplicates. */
866*38fd1498Szrj find_comparisons ();
867*38fd1498Szrj if (all_compares.exists ())
868*38fd1498Szrj {
869*38fd1498Szrj struct comparison *cmp;
870*38fd1498Szrj size_t i;
871*38fd1498Szrj
872*38fd1498Szrj /* Eliminate comparisons that are redundant with flags computation. */
873*38fd1498Szrj FOR_EACH_VEC_ELT (all_compares, i, cmp)
874*38fd1498Szrj {
875*38fd1498Szrj try_eliminate_compare (cmp);
876*38fd1498Szrj XDELETE (cmp);
877*38fd1498Szrj }
878*38fd1498Szrj
879*38fd1498Szrj all_compares.release ();
880*38fd1498Szrj }
881*38fd1498Szrj
882*38fd1498Szrj return 0;
883*38fd1498Szrj }
884*38fd1498Szrj
885*38fd1498Szrj namespace {
886*38fd1498Szrj
887*38fd1498Szrj const pass_data pass_data_compare_elim_after_reload =
888*38fd1498Szrj {
889*38fd1498Szrj RTL_PASS, /* type */
890*38fd1498Szrj "cmpelim", /* name */
891*38fd1498Szrj OPTGROUP_NONE, /* optinfo_flags */
892*38fd1498Szrj TV_NONE, /* tv_id */
893*38fd1498Szrj 0, /* properties_required */
894*38fd1498Szrj 0, /* properties_provided */
895*38fd1498Szrj 0, /* properties_destroyed */
896*38fd1498Szrj 0, /* todo_flags_start */
897*38fd1498Szrj ( TODO_df_finish | TODO_df_verify ), /* todo_flags_finish */
898*38fd1498Szrj };
899*38fd1498Szrj
900*38fd1498Szrj class pass_compare_elim_after_reload : public rtl_opt_pass
901*38fd1498Szrj {
902*38fd1498Szrj public:
pass_compare_elim_after_reload(gcc::context * ctxt)903*38fd1498Szrj pass_compare_elim_after_reload (gcc::context *ctxt)
904*38fd1498Szrj : rtl_opt_pass (pass_data_compare_elim_after_reload, ctxt)
905*38fd1498Szrj {}
906*38fd1498Szrj
907*38fd1498Szrj /* opt_pass methods: */
gate(function *)908*38fd1498Szrj virtual bool gate (function *)
909*38fd1498Szrj {
910*38fd1498Szrj /* Setting this target hook value is how a backend indicates the need. */
911*38fd1498Szrj if (targetm.flags_regnum == INVALID_REGNUM)
912*38fd1498Szrj return false;
913*38fd1498Szrj return flag_compare_elim_after_reload;
914*38fd1498Szrj }
915*38fd1498Szrj
execute(function *)916*38fd1498Szrj virtual unsigned int execute (function *)
917*38fd1498Szrj {
918*38fd1498Szrj return execute_compare_elim_after_reload ();
919*38fd1498Szrj }
920*38fd1498Szrj
921*38fd1498Szrj }; // class pass_compare_elim_after_reload
922*38fd1498Szrj
923*38fd1498Szrj } // anon namespace
924*38fd1498Szrj
925*38fd1498Szrj rtl_opt_pass *
make_pass_compare_elim_after_reload(gcc::context * ctxt)926*38fd1498Szrj make_pass_compare_elim_after_reload (gcc::context *ctxt)
927*38fd1498Szrj {
928*38fd1498Szrj return new pass_compare_elim_after_reload (ctxt);
929*38fd1498Szrj }
930