1*e4b17023SJohn Marino /* Post-reload compare elimination.
2*e4b17023SJohn Marino Copyright (C) 2010, 2011
3*e4b17023SJohn Marino Free Software Foundation, Inc.
4*e4b17023SJohn Marino
5*e4b17023SJohn Marino This file is part of GCC.
6*e4b17023SJohn Marino
7*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it under
8*e4b17023SJohn Marino the terms of the GNU General Public License as published by the Free
9*e4b17023SJohn Marino Software Foundation; either version 3, or (at your option) any later
10*e4b17023SJohn Marino version.
11*e4b17023SJohn Marino
12*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13*e4b17023SJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or
14*e4b17023SJohn Marino FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15*e4b17023SJohn Marino for more details.
16*e4b17023SJohn Marino
17*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
18*e4b17023SJohn Marino along with GCC; see the file COPYING3. If not see
19*e4b17023SJohn Marino <http://www.gnu.org/licenses/>. */
20*e4b17023SJohn Marino
21*e4b17023SJohn Marino /* There is a set of targets whose general-purpose move or addition
22*e4b17023SJohn Marino instructions clobber the flags. These targets cannot split their
23*e4b17023SJohn Marino CBRANCH/CSTORE etc patterns before reload is complete, lest reload
24*e4b17023SJohn Marino itself insert these instructions in between the flags setter and user.
25*e4b17023SJohn Marino Because these targets cannot split the compare from the use, they
26*e4b17023SJohn Marino cannot make use of the comparison elimination offered by the combine pass.
27*e4b17023SJohn Marino
28*e4b17023SJohn Marino This is a small pass intended to provide comparison elimination similar to
29*e4b17023SJohn Marino what is available via NOTICE_UPDATE_CC for cc0 targets. This should help
30*e4b17023SJohn Marino encourage cc0 targets to convert to an explicit post-reload representation
31*e4b17023SJohn Marino of the flags.
32*e4b17023SJohn Marino
33*e4b17023SJohn Marino This pass assumes:
34*e4b17023SJohn Marino
35*e4b17023SJohn Marino (0) CBRANCH/CSTORE etc have been split in pass_split_after_reload.
36*e4b17023SJohn Marino
37*e4b17023SJohn Marino (1) All comparison patterns are represented as
38*e4b17023SJohn Marino
39*e4b17023SJohn Marino [(set (reg:CC) (compare:CC (reg) (immediate)))]
40*e4b17023SJohn Marino
41*e4b17023SJohn Marino (2) All insn patterns that modify the flags are represented as
42*e4b17023SJohn Marino
43*e4b17023SJohn Marino [(set (reg) (operation)
44*e4b17023SJohn Marino (clobber (reg:CC))]
45*e4b17023SJohn Marino
46*e4b17023SJohn Marino (3) If an insn of form (2) can usefully set the flags, there is
47*e4b17023SJohn Marino another pattern of the form
48*e4b17023SJohn Marino
49*e4b17023SJohn Marino [(set (reg) (operation)
50*e4b17023SJohn Marino (set (reg:CCM) (compare:CCM (operation) (immediate)))]
51*e4b17023SJohn Marino
52*e4b17023SJohn Marino The mode CCM will be chosen as if by SELECT_CC_MODE.
53*e4b17023SJohn Marino
54*e4b17023SJohn Marino Note that unlike NOTICE_UPDATE_CC, we do not handle memory operands.
55*e4b17023SJohn Marino This could be handled as a future enhancement.
56*e4b17023SJohn Marino */
57*e4b17023SJohn Marino
58*e4b17023SJohn Marino #include "config.h"
59*e4b17023SJohn Marino #include "system.h"
60*e4b17023SJohn Marino #include "coretypes.h"
61*e4b17023SJohn Marino #include "tm.h"
62*e4b17023SJohn Marino #include "rtl.h"
63*e4b17023SJohn Marino #include "tm_p.h"
64*e4b17023SJohn Marino #include "insn-config.h"
65*e4b17023SJohn Marino #include "recog.h"
66*e4b17023SJohn Marino #include "flags.h"
67*e4b17023SJohn Marino #include "basic-block.h"
68*e4b17023SJohn Marino #include "tree-pass.h"
69*e4b17023SJohn Marino #include "target.h"
70*e4b17023SJohn Marino #include "df.h"
71*e4b17023SJohn Marino #include "domwalk.h"
72*e4b17023SJohn Marino
73*e4b17023SJohn Marino
74*e4b17023SJohn Marino /* These structures describe a comparison and how it is used. */
75*e4b17023SJohn Marino
76*e4b17023SJohn Marino /* The choice of maximum 3 uses comes from wanting to eliminate the two
77*e4b17023SJohn Marino duplicate compares from a three-way branch on the sign of a value.
78*e4b17023SJohn Marino This is also sufficient to eliminate the duplicate compare against the
79*e4b17023SJohn Marino high-part of a double-word comparison. */
80*e4b17023SJohn Marino #define MAX_CMP_USE 3
81*e4b17023SJohn Marino
82*e4b17023SJohn Marino struct comparison_use
83*e4b17023SJohn Marino {
84*e4b17023SJohn Marino /* The instruction in which the result of the compare is used. */
85*e4b17023SJohn Marino rtx insn;
86*e4b17023SJohn Marino /* The location of the flags register within the use. */
87*e4b17023SJohn Marino rtx *loc;
88*e4b17023SJohn Marino /* The comparison code applied against the flags register. */
89*e4b17023SJohn Marino enum rtx_code code;
90*e4b17023SJohn Marino };
91*e4b17023SJohn Marino
92*e4b17023SJohn Marino struct comparison
93*e4b17023SJohn Marino {
94*e4b17023SJohn Marino /* The comparison instruction. */
95*e4b17023SJohn Marino rtx insn;
96*e4b17023SJohn Marino
97*e4b17023SJohn Marino /* The insn prior to the comparison insn that clobbers the flags. */
98*e4b17023SJohn Marino rtx prev_clobber;
99*e4b17023SJohn Marino
100*e4b17023SJohn Marino /* The two values being compared. These will be either REGs or
101*e4b17023SJohn Marino constants. */
102*e4b17023SJohn Marino rtx in_a, in_b;
103*e4b17023SJohn Marino
104*e4b17023SJohn Marino /* Information about how this comparison is used. */
105*e4b17023SJohn Marino struct comparison_use uses[MAX_CMP_USE];
106*e4b17023SJohn Marino
107*e4b17023SJohn Marino /* The original CC_MODE for this comparison. */
108*e4b17023SJohn Marino enum machine_mode orig_mode;
109*e4b17023SJohn Marino
110*e4b17023SJohn Marino /* The number of uses identified for this comparison. */
111*e4b17023SJohn Marino unsigned short n_uses;
112*e4b17023SJohn Marino
113*e4b17023SJohn Marino /* True if not all uses of this comparison have been identified.
114*e4b17023SJohn Marino This can happen either for overflowing the array above, or if
115*e4b17023SJohn Marino the flags register is used in some unusual context. */
116*e4b17023SJohn Marino bool missing_uses;
117*e4b17023SJohn Marino
118*e4b17023SJohn Marino /* True if its inputs are still valid at the end of the block. */
119*e4b17023SJohn Marino bool inputs_valid;
120*e4b17023SJohn Marino };
121*e4b17023SJohn Marino
122*e4b17023SJohn Marino typedef struct comparison *comparison_struct_p;
123*e4b17023SJohn Marino DEF_VEC_P(comparison_struct_p);
124*e4b17023SJohn Marino DEF_VEC_ALLOC_P(comparison_struct_p, heap);
125*e4b17023SJohn Marino
VEC(comparison_struct_p,heap)126*e4b17023SJohn Marino static VEC(comparison_struct_p, heap) *all_compares;
127*e4b17023SJohn Marino
128*e4b17023SJohn Marino /* Look for a "conforming" comparison, as defined above. If valid, return
129*e4b17023SJohn Marino the rtx for the COMPARE itself. */
130*e4b17023SJohn Marino
131*e4b17023SJohn Marino static rtx
132*e4b17023SJohn Marino conforming_compare (rtx insn)
133*e4b17023SJohn Marino {
134*e4b17023SJohn Marino rtx set, src, dest;
135*e4b17023SJohn Marino
136*e4b17023SJohn Marino set = single_set (insn);
137*e4b17023SJohn Marino if (set == NULL)
138*e4b17023SJohn Marino return NULL;
139*e4b17023SJohn Marino
140*e4b17023SJohn Marino src = SET_SRC (set);
141*e4b17023SJohn Marino if (GET_CODE (src) != COMPARE)
142*e4b17023SJohn Marino return NULL;
143*e4b17023SJohn Marino
144*e4b17023SJohn Marino dest = SET_DEST (set);
145*e4b17023SJohn Marino if (!REG_P (dest) || REGNO (dest) != targetm.flags_regnum)
146*e4b17023SJohn Marino return NULL;
147*e4b17023SJohn Marino
148*e4b17023SJohn Marino if (REG_P (XEXP (src, 0))
149*e4b17023SJohn Marino && REG_P (XEXP (src, 0))
150*e4b17023SJohn Marino && (REG_P (XEXP (src, 1)) || CONSTANT_P (XEXP (src, 1))))
151*e4b17023SJohn Marino return src;
152*e4b17023SJohn Marino
153*e4b17023SJohn Marino return NULL;
154*e4b17023SJohn Marino }
155*e4b17023SJohn Marino
156*e4b17023SJohn Marino /* Look for a pattern of the "correct" form for an insn with a flags clobber
157*e4b17023SJohn Marino for which we may be able to eliminate a compare later. We're not looking
158*e4b17023SJohn Marino to validate any inputs at this time, merely see that the basic shape is
159*e4b17023SJohn Marino correct. The term "arithmetic" may be somewhat misleading... */
160*e4b17023SJohn Marino
161*e4b17023SJohn Marino static bool
arithmetic_flags_clobber_p(rtx insn)162*e4b17023SJohn Marino arithmetic_flags_clobber_p (rtx insn)
163*e4b17023SJohn Marino {
164*e4b17023SJohn Marino rtx pat, x;
165*e4b17023SJohn Marino
166*e4b17023SJohn Marino if (!NONJUMP_INSN_P (insn))
167*e4b17023SJohn Marino return false;
168*e4b17023SJohn Marino pat = PATTERN (insn);
169*e4b17023SJohn Marino if (extract_asm_operands (pat))
170*e4b17023SJohn Marino return false;
171*e4b17023SJohn Marino
172*e4b17023SJohn Marino if (GET_CODE (pat) == PARALLEL && XVECLEN (pat, 0) == 2)
173*e4b17023SJohn Marino {
174*e4b17023SJohn Marino x = XVECEXP (pat, 0, 0);
175*e4b17023SJohn Marino if (GET_CODE (x) != SET)
176*e4b17023SJohn Marino return false;
177*e4b17023SJohn Marino x = SET_DEST (x);
178*e4b17023SJohn Marino if (!REG_P (x))
179*e4b17023SJohn Marino return false;
180*e4b17023SJohn Marino
181*e4b17023SJohn Marino x = XVECEXP (pat, 0, 1);
182*e4b17023SJohn Marino if (GET_CODE (x) == CLOBBER)
183*e4b17023SJohn Marino {
184*e4b17023SJohn Marino x = XEXP (x, 0);
185*e4b17023SJohn Marino if (REG_P (x) && REGNO (x) == targetm.flags_regnum)
186*e4b17023SJohn Marino return true;
187*e4b17023SJohn Marino }
188*e4b17023SJohn Marino }
189*e4b17023SJohn Marino
190*e4b17023SJohn Marino return false;
191*e4b17023SJohn Marino }
192*e4b17023SJohn Marino
193*e4b17023SJohn Marino /* Look for uses of FLAGS in INSN. If we find one we can analyze, record
194*e4b17023SJohn Marino it in CMP; otherwise indicate that we've missed a use. */
195*e4b17023SJohn Marino
196*e4b17023SJohn Marino static void
find_flags_uses_in_insn(struct comparison * cmp,rtx insn)197*e4b17023SJohn Marino find_flags_uses_in_insn (struct comparison *cmp, rtx insn)
198*e4b17023SJohn Marino {
199*e4b17023SJohn Marino df_ref *use_rec, use;
200*e4b17023SJohn Marino
201*e4b17023SJohn Marino /* If we've already lost track of uses, don't bother collecting more. */
202*e4b17023SJohn Marino if (cmp->missing_uses)
203*e4b17023SJohn Marino return;
204*e4b17023SJohn Marino
205*e4b17023SJohn Marino /* Find a USE of the flags register. */
206*e4b17023SJohn Marino for (use_rec = DF_INSN_USES (insn); (use = *use_rec) != NULL; use_rec++)
207*e4b17023SJohn Marino if (DF_REF_REGNO (use) == targetm.flags_regnum)
208*e4b17023SJohn Marino {
209*e4b17023SJohn Marino rtx x, *loc;
210*e4b17023SJohn Marino
211*e4b17023SJohn Marino /* If this is an unusual use, quit. */
212*e4b17023SJohn Marino if (DF_REF_TYPE (use) != DF_REF_REG_USE)
213*e4b17023SJohn Marino goto fail;
214*e4b17023SJohn Marino
215*e4b17023SJohn Marino /* If we've run out of slots to record uses, quit. */
216*e4b17023SJohn Marino if (cmp->n_uses == MAX_CMP_USE)
217*e4b17023SJohn Marino goto fail;
218*e4b17023SJohn Marino
219*e4b17023SJohn Marino /* Unfortunately the location of the flags register, while present
220*e4b17023SJohn Marino in the reference structure, doesn't help. We need to find the
221*e4b17023SJohn Marino comparison code that is outer to the actual flags use. */
222*e4b17023SJohn Marino loc = DF_REF_LOC (use);
223*e4b17023SJohn Marino x = PATTERN (insn);
224*e4b17023SJohn Marino if (GET_CODE (x) == PARALLEL)
225*e4b17023SJohn Marino x = XVECEXP (x, 0, 0);
226*e4b17023SJohn Marino x = SET_SRC (x);
227*e4b17023SJohn Marino if (GET_CODE (x) == IF_THEN_ELSE)
228*e4b17023SJohn Marino x = XEXP (x, 0);
229*e4b17023SJohn Marino if (COMPARISON_P (x)
230*e4b17023SJohn Marino && loc == &XEXP (x, 0)
231*e4b17023SJohn Marino && XEXP (x, 1) == const0_rtx)
232*e4b17023SJohn Marino {
233*e4b17023SJohn Marino /* We've found a use of the flags that we understand. */
234*e4b17023SJohn Marino struct comparison_use *cuse = &cmp->uses[cmp->n_uses++];
235*e4b17023SJohn Marino cuse->insn = insn;
236*e4b17023SJohn Marino cuse->loc = loc;
237*e4b17023SJohn Marino cuse->code = GET_CODE (x);
238*e4b17023SJohn Marino }
239*e4b17023SJohn Marino else
240*e4b17023SJohn Marino goto fail;
241*e4b17023SJohn Marino }
242*e4b17023SJohn Marino return;
243*e4b17023SJohn Marino
244*e4b17023SJohn Marino fail:
245*e4b17023SJohn Marino /* We failed to recognize this use of the flags register. */
246*e4b17023SJohn Marino cmp->missing_uses = true;
247*e4b17023SJohn Marino }
248*e4b17023SJohn Marino
249*e4b17023SJohn Marino /* Identify comparison instructions within BB. If the flags from the last
250*e4b17023SJohn Marino compare in the BB is live at the end of the block, install the compare
251*e4b17023SJohn Marino in BB->AUX. Called via walk_dominators_tree. */
252*e4b17023SJohn Marino
253*e4b17023SJohn Marino static void
find_comparisons_in_bb(struct dom_walk_data * data ATTRIBUTE_UNUSED,basic_block bb)254*e4b17023SJohn Marino find_comparisons_in_bb (struct dom_walk_data *data ATTRIBUTE_UNUSED,
255*e4b17023SJohn Marino basic_block bb)
256*e4b17023SJohn Marino {
257*e4b17023SJohn Marino struct comparison *last_cmp;
258*e4b17023SJohn Marino rtx insn, next, last_clobber;
259*e4b17023SJohn Marino bool last_cmp_valid;
260*e4b17023SJohn Marino bitmap killed;
261*e4b17023SJohn Marino
262*e4b17023SJohn Marino killed = BITMAP_ALLOC (NULL);
263*e4b17023SJohn Marino
264*e4b17023SJohn Marino /* The last comparison that was made. Will be reset to NULL
265*e4b17023SJohn Marino once the flags are clobbered. */
266*e4b17023SJohn Marino last_cmp = NULL;
267*e4b17023SJohn Marino
268*e4b17023SJohn Marino /* True iff the last comparison has not been clobbered, nor
269*e4b17023SJohn Marino have its inputs. Used to eliminate duplicate compares. */
270*e4b17023SJohn Marino last_cmp_valid = false;
271*e4b17023SJohn Marino
272*e4b17023SJohn Marino /* The last insn that clobbered the flags, if that insn is of
273*e4b17023SJohn Marino a form that may be valid for eliminating a following compare.
274*e4b17023SJohn Marino To be reset to NULL once the flags are set otherwise. */
275*e4b17023SJohn Marino last_clobber = NULL;
276*e4b17023SJohn Marino
277*e4b17023SJohn Marino /* Propagate the last live comparison throughout the extended basic block. */
278*e4b17023SJohn Marino if (single_pred_p (bb))
279*e4b17023SJohn Marino {
280*e4b17023SJohn Marino last_cmp = (struct comparison *) single_pred (bb)->aux;
281*e4b17023SJohn Marino if (last_cmp)
282*e4b17023SJohn Marino last_cmp_valid = last_cmp->inputs_valid;
283*e4b17023SJohn Marino }
284*e4b17023SJohn Marino
285*e4b17023SJohn Marino for (insn = BB_HEAD (bb); insn; insn = next)
286*e4b17023SJohn Marino {
287*e4b17023SJohn Marino rtx src;
288*e4b17023SJohn Marino
289*e4b17023SJohn Marino next = (insn == BB_END (bb) ? NULL_RTX : NEXT_INSN (insn));
290*e4b17023SJohn Marino if (!NONDEBUG_INSN_P (insn))
291*e4b17023SJohn Marino continue;
292*e4b17023SJohn Marino
293*e4b17023SJohn Marino /* Compute the set of registers modified by this instruction. */
294*e4b17023SJohn Marino bitmap_clear (killed);
295*e4b17023SJohn Marino df_simulate_find_defs (insn, killed);
296*e4b17023SJohn Marino
297*e4b17023SJohn Marino src = conforming_compare (insn);
298*e4b17023SJohn Marino if (src)
299*e4b17023SJohn Marino {
300*e4b17023SJohn Marino enum machine_mode src_mode = GET_MODE (src);
301*e4b17023SJohn Marino
302*e4b17023SJohn Marino /* Eliminate a compare that's redundant with the previous. */
303*e4b17023SJohn Marino if (last_cmp_valid
304*e4b17023SJohn Marino && src_mode == last_cmp->orig_mode
305*e4b17023SJohn Marino && rtx_equal_p (last_cmp->in_a, XEXP (src, 0))
306*e4b17023SJohn Marino && rtx_equal_p (last_cmp->in_b, XEXP (src, 1)))
307*e4b17023SJohn Marino {
308*e4b17023SJohn Marino delete_insn (insn);
309*e4b17023SJohn Marino continue;
310*e4b17023SJohn Marino }
311*e4b17023SJohn Marino
312*e4b17023SJohn Marino last_cmp = XCNEW (struct comparison);
313*e4b17023SJohn Marino last_cmp->insn = insn;
314*e4b17023SJohn Marino last_cmp->prev_clobber = last_clobber;
315*e4b17023SJohn Marino last_cmp->in_a = XEXP (src, 0);
316*e4b17023SJohn Marino last_cmp->in_b = XEXP (src, 1);
317*e4b17023SJohn Marino last_cmp->orig_mode = src_mode;
318*e4b17023SJohn Marino VEC_safe_push (comparison_struct_p, heap, all_compares, last_cmp);
319*e4b17023SJohn Marino
320*e4b17023SJohn Marino /* It's unusual, but be prepared for comparison patterns that
321*e4b17023SJohn Marino also clobber an input, or perhaps a scratch. */
322*e4b17023SJohn Marino last_clobber = NULL;
323*e4b17023SJohn Marino last_cmp_valid = true;
324*e4b17023SJohn Marino }
325*e4b17023SJohn Marino
326*e4b17023SJohn Marino /* Notice if this instruction kills the flags register. */
327*e4b17023SJohn Marino else if (bitmap_bit_p (killed, targetm.flags_regnum))
328*e4b17023SJohn Marino {
329*e4b17023SJohn Marino /* See if this insn could be the "clobber" that eliminates
330*e4b17023SJohn Marino a future comparison. */
331*e4b17023SJohn Marino last_clobber = (arithmetic_flags_clobber_p (insn) ? insn : NULL);
332*e4b17023SJohn Marino
333*e4b17023SJohn Marino /* In either case, the previous compare is no longer valid. */
334*e4b17023SJohn Marino last_cmp = NULL;
335*e4b17023SJohn Marino last_cmp_valid = false;
336*e4b17023SJohn Marino continue;
337*e4b17023SJohn Marino }
338*e4b17023SJohn Marino
339*e4b17023SJohn Marino /* Notice if this instruction uses the flags register. */
340*e4b17023SJohn Marino else if (last_cmp)
341*e4b17023SJohn Marino find_flags_uses_in_insn (last_cmp, insn);
342*e4b17023SJohn Marino
343*e4b17023SJohn Marino /* Notice if any of the inputs to the comparison have changed. */
344*e4b17023SJohn Marino if (last_cmp_valid
345*e4b17023SJohn Marino && (bitmap_bit_p (killed, REGNO (last_cmp->in_a))
346*e4b17023SJohn Marino || (REG_P (last_cmp->in_b)
347*e4b17023SJohn Marino && bitmap_bit_p (killed, REGNO (last_cmp->in_b)))))
348*e4b17023SJohn Marino last_cmp_valid = false;
349*e4b17023SJohn Marino }
350*e4b17023SJohn Marino
351*e4b17023SJohn Marino BITMAP_FREE (killed);
352*e4b17023SJohn Marino
353*e4b17023SJohn Marino /* Remember the live comparison for subsequent members of
354*e4b17023SJohn Marino the extended basic block. */
355*e4b17023SJohn Marino if (last_cmp)
356*e4b17023SJohn Marino {
357*e4b17023SJohn Marino bb->aux = last_cmp;
358*e4b17023SJohn Marino last_cmp->inputs_valid = last_cmp_valid;
359*e4b17023SJohn Marino
360*e4b17023SJohn Marino /* Look to see if the flags register is live outgoing here, and
361*e4b17023SJohn Marino incoming to any successor not part of the extended basic block. */
362*e4b17023SJohn Marino if (bitmap_bit_p (df_get_live_out (bb), targetm.flags_regnum))
363*e4b17023SJohn Marino {
364*e4b17023SJohn Marino edge e;
365*e4b17023SJohn Marino edge_iterator ei;
366*e4b17023SJohn Marino
367*e4b17023SJohn Marino FOR_EACH_EDGE (e, ei, bb->succs)
368*e4b17023SJohn Marino {
369*e4b17023SJohn Marino basic_block dest = e->dest;
370*e4b17023SJohn Marino if (bitmap_bit_p (df_get_live_in (bb),
371*e4b17023SJohn Marino targetm.flags_regnum)
372*e4b17023SJohn Marino && !single_pred_p (dest))
373*e4b17023SJohn Marino {
374*e4b17023SJohn Marino last_cmp->missing_uses = true;
375*e4b17023SJohn Marino break;
376*e4b17023SJohn Marino }
377*e4b17023SJohn Marino }
378*e4b17023SJohn Marino }
379*e4b17023SJohn Marino }
380*e4b17023SJohn Marino }
381*e4b17023SJohn Marino
382*e4b17023SJohn Marino /* Find all comparisons in the function. */
383*e4b17023SJohn Marino
384*e4b17023SJohn Marino static void
find_comparisons(void)385*e4b17023SJohn Marino find_comparisons (void)
386*e4b17023SJohn Marino {
387*e4b17023SJohn Marino struct dom_walk_data data;
388*e4b17023SJohn Marino
389*e4b17023SJohn Marino memset (&data, 0, sizeof(data));
390*e4b17023SJohn Marino data.dom_direction = CDI_DOMINATORS;
391*e4b17023SJohn Marino data.before_dom_children = find_comparisons_in_bb;
392*e4b17023SJohn Marino
393*e4b17023SJohn Marino calculate_dominance_info (CDI_DOMINATORS);
394*e4b17023SJohn Marino
395*e4b17023SJohn Marino init_walk_dominator_tree (&data);
396*e4b17023SJohn Marino walk_dominator_tree (&data, ENTRY_BLOCK_PTR);
397*e4b17023SJohn Marino fini_walk_dominator_tree (&data);
398*e4b17023SJohn Marino
399*e4b17023SJohn Marino clear_aux_for_blocks ();
400*e4b17023SJohn Marino free_dominance_info (CDI_DOMINATORS);
401*e4b17023SJohn Marino }
402*e4b17023SJohn Marino
403*e4b17023SJohn Marino /* Select an alternate CC_MODE for a comparison insn comparing A and B.
404*e4b17023SJohn Marino Note that inputs are almost certainly different than the IN_A and IN_B
405*e4b17023SJohn Marino stored in CMP -- we're called while attempting to eliminate the compare
406*e4b17023SJohn Marino after all. Return the new FLAGS rtx if successful, else return NULL.
407*e4b17023SJohn Marino Note that this function may start a change group. */
408*e4b17023SJohn Marino
409*e4b17023SJohn Marino static rtx
maybe_select_cc_mode(struct comparison * cmp,rtx a ATTRIBUTE_UNUSED,rtx b ATTRIBUTE_UNUSED)410*e4b17023SJohn Marino maybe_select_cc_mode (struct comparison *cmp, rtx a ATTRIBUTE_UNUSED,
411*e4b17023SJohn Marino rtx b ATTRIBUTE_UNUSED)
412*e4b17023SJohn Marino {
413*e4b17023SJohn Marino enum machine_mode sel_mode;
414*e4b17023SJohn Marino const int n = cmp->n_uses;
415*e4b17023SJohn Marino rtx flags = NULL;
416*e4b17023SJohn Marino
417*e4b17023SJohn Marino #ifndef SELECT_CC_MODE
418*e4b17023SJohn Marino /* Minimize code differences when this target macro is undefined. */
419*e4b17023SJohn Marino return NULL;
420*e4b17023SJohn Marino #define SELECT_CC_MODE(A,B,C) (gcc_unreachable (), VOIDmode)
421*e4b17023SJohn Marino #endif
422*e4b17023SJohn Marino
423*e4b17023SJohn Marino /* If we don't have access to all of the uses, we can't validate. */
424*e4b17023SJohn Marino if (cmp->missing_uses || n == 0)
425*e4b17023SJohn Marino return NULL;
426*e4b17023SJohn Marino
427*e4b17023SJohn Marino /* Find a new mode that works for all of the uses. Special case the
428*e4b17023SJohn Marino common case of exactly one use. */
429*e4b17023SJohn Marino if (n == 1)
430*e4b17023SJohn Marino {
431*e4b17023SJohn Marino sel_mode = SELECT_CC_MODE (cmp->uses[0].code, a, b);
432*e4b17023SJohn Marino if (sel_mode != cmp->orig_mode)
433*e4b17023SJohn Marino {
434*e4b17023SJohn Marino flags = gen_rtx_REG (sel_mode, targetm.flags_regnum);
435*e4b17023SJohn Marino validate_change (cmp->uses[0].insn, cmp->uses[0].loc, flags, true);
436*e4b17023SJohn Marino }
437*e4b17023SJohn Marino }
438*e4b17023SJohn Marino else
439*e4b17023SJohn Marino {
440*e4b17023SJohn Marino int i;
441*e4b17023SJohn Marino
442*e4b17023SJohn Marino sel_mode = SELECT_CC_MODE (cmp->uses[0].code, a, b);
443*e4b17023SJohn Marino for (i = 1; i < n; ++i)
444*e4b17023SJohn Marino {
445*e4b17023SJohn Marino enum machine_mode new_mode;
446*e4b17023SJohn Marino new_mode = SELECT_CC_MODE (cmp->uses[i].code, a, b);
447*e4b17023SJohn Marino if (new_mode != sel_mode)
448*e4b17023SJohn Marino {
449*e4b17023SJohn Marino sel_mode = targetm.cc_modes_compatible (sel_mode, new_mode);
450*e4b17023SJohn Marino if (sel_mode == VOIDmode)
451*e4b17023SJohn Marino return NULL;
452*e4b17023SJohn Marino }
453*e4b17023SJohn Marino }
454*e4b17023SJohn Marino
455*e4b17023SJohn Marino if (sel_mode != cmp->orig_mode)
456*e4b17023SJohn Marino {
457*e4b17023SJohn Marino flags = gen_rtx_REG (sel_mode, targetm.flags_regnum);
458*e4b17023SJohn Marino for (i = 0; i < n; ++i)
459*e4b17023SJohn Marino validate_change (cmp->uses[i].insn, cmp->uses[i].loc, flags, true);
460*e4b17023SJohn Marino }
461*e4b17023SJohn Marino }
462*e4b17023SJohn Marino
463*e4b17023SJohn Marino return flags;
464*e4b17023SJohn Marino }
465*e4b17023SJohn Marino
466*e4b17023SJohn Marino /* Attempt to replace a comparison with a prior arithmetic insn that can
467*e4b17023SJohn Marino compute the same flags value as the comparison itself. Return true if
468*e4b17023SJohn Marino successful, having made all rtl modifications necessary. */
469*e4b17023SJohn Marino
470*e4b17023SJohn Marino static bool
try_eliminate_compare(struct comparison * cmp)471*e4b17023SJohn Marino try_eliminate_compare (struct comparison *cmp)
472*e4b17023SJohn Marino {
473*e4b17023SJohn Marino rtx x, insn, bb_head, flags, in_a, cmp_src;
474*e4b17023SJohn Marino
475*e4b17023SJohn Marino /* We must have found an interesting "clobber" preceeding the compare. */
476*e4b17023SJohn Marino if (cmp->prev_clobber == NULL)
477*e4b17023SJohn Marino return false;
478*e4b17023SJohn Marino
479*e4b17023SJohn Marino /* ??? For the moment we don't handle comparisons for which IN_B
480*e4b17023SJohn Marino is a register. We accepted these during initial comparison
481*e4b17023SJohn Marino recognition in order to eliminate duplicate compares.
482*e4b17023SJohn Marino An improvement here would be to handle x = a - b; if (a cmp b). */
483*e4b17023SJohn Marino if (!CONSTANT_P (cmp->in_b))
484*e4b17023SJohn Marino return false;
485*e4b17023SJohn Marino
486*e4b17023SJohn Marino /* Verify that IN_A is not clobbered in between CMP and PREV_CLOBBER.
487*e4b17023SJohn Marino Given that this target requires this pass, we can assume that most
488*e4b17023SJohn Marino insns do clobber the flags, and so the distance between the compare
489*e4b17023SJohn Marino and the clobber is likely to be small. */
490*e4b17023SJohn Marino /* ??? This is one point at which one could argue that DF_REF_CHAIN would
491*e4b17023SJohn Marino be useful, but it is thought to be too heavy-weight a solution here. */
492*e4b17023SJohn Marino
493*e4b17023SJohn Marino in_a = cmp->in_a;
494*e4b17023SJohn Marino insn = cmp->insn;
495*e4b17023SJohn Marino bb_head = BB_HEAD (BLOCK_FOR_INSN (insn));
496*e4b17023SJohn Marino for (insn = PREV_INSN (insn);
497*e4b17023SJohn Marino insn != cmp->prev_clobber;
498*e4b17023SJohn Marino insn = PREV_INSN (insn))
499*e4b17023SJohn Marino {
500*e4b17023SJohn Marino const int abnormal_flags
501*e4b17023SJohn Marino = (DF_REF_CONDITIONAL | DF_REF_PARTIAL | DF_REF_MAY_CLOBBER
502*e4b17023SJohn Marino | DF_REF_MUST_CLOBBER | DF_REF_SIGN_EXTRACT
503*e4b17023SJohn Marino | DF_REF_ZERO_EXTRACT | DF_REF_STRICT_LOW_PART
504*e4b17023SJohn Marino | DF_REF_PRE_POST_MODIFY);
505*e4b17023SJohn Marino df_ref *def_rec, def;
506*e4b17023SJohn Marino
507*e4b17023SJohn Marino /* Note that the BB_HEAD is always either a note or a label, but in
508*e4b17023SJohn Marino any case it means that IN_A is defined outside the block. */
509*e4b17023SJohn Marino if (insn == bb_head)
510*e4b17023SJohn Marino return false;
511*e4b17023SJohn Marino if (NOTE_P (insn) || DEBUG_INSN_P (insn))
512*e4b17023SJohn Marino continue;
513*e4b17023SJohn Marino
514*e4b17023SJohn Marino /* Find a possible def of IN_A in INSN. */
515*e4b17023SJohn Marino for (def_rec = DF_INSN_DEFS (insn); (def = *def_rec) != NULL; def_rec++)
516*e4b17023SJohn Marino if (DF_REF_REGNO (def) == REGNO (in_a))
517*e4b17023SJohn Marino break;
518*e4b17023SJohn Marino
519*e4b17023SJohn Marino /* No definitions of IN_A; continue searching. */
520*e4b17023SJohn Marino if (def == NULL)
521*e4b17023SJohn Marino continue;
522*e4b17023SJohn Marino
523*e4b17023SJohn Marino /* Bail if this is not a totally normal set of IN_A. */
524*e4b17023SJohn Marino if (DF_REF_IS_ARTIFICIAL (def))
525*e4b17023SJohn Marino return false;
526*e4b17023SJohn Marino if (DF_REF_FLAGS (def) & abnormal_flags)
527*e4b17023SJohn Marino return false;
528*e4b17023SJohn Marino
529*e4b17023SJohn Marino /* We've found an insn between the compare and the clobber that sets
530*e4b17023SJohn Marino IN_A. Given that pass_cprop_hardreg has not yet run, we still find
531*e4b17023SJohn Marino situations in which we can usefully look through a copy insn. */
532*e4b17023SJohn Marino x = single_set (insn);
533*e4b17023SJohn Marino if (x == NULL)
534*e4b17023SJohn Marino return false;
535*e4b17023SJohn Marino in_a = SET_SRC (x);
536*e4b17023SJohn Marino if (!REG_P (in_a))
537*e4b17023SJohn Marino return false;
538*e4b17023SJohn Marino }
539*e4b17023SJohn Marino
540*e4b17023SJohn Marino /* We've reached PREV_CLOBBER without finding a modification of IN_A.
541*e4b17023SJohn Marino Validate that PREV_CLOBBER itself does in fact refer to IN_A. Do
542*e4b17023SJohn Marino recall that we've already validated the shape of PREV_CLOBBER. */
543*e4b17023SJohn Marino x = XVECEXP (PATTERN (insn), 0, 0);
544*e4b17023SJohn Marino if (!rtx_equal_p (SET_DEST (x), in_a))
545*e4b17023SJohn Marino return false;
546*e4b17023SJohn Marino cmp_src = SET_SRC (x);
547*e4b17023SJohn Marino
548*e4b17023SJohn Marino /* Determine if we ought to use a different CC_MODE here. */
549*e4b17023SJohn Marino flags = maybe_select_cc_mode (cmp, cmp_src, cmp->in_b);
550*e4b17023SJohn Marino if (flags == NULL)
551*e4b17023SJohn Marino flags = gen_rtx_REG (cmp->orig_mode, targetm.flags_regnum);
552*e4b17023SJohn Marino
553*e4b17023SJohn Marino /* Generate a new comparison for installation in the setter. */
554*e4b17023SJohn Marino x = copy_rtx (cmp_src);
555*e4b17023SJohn Marino x = gen_rtx_COMPARE (GET_MODE (flags), x, cmp->in_b);
556*e4b17023SJohn Marino x = gen_rtx_SET (VOIDmode, flags, x);
557*e4b17023SJohn Marino
558*e4b17023SJohn Marino /* Succeed if the new instruction is valid. Note that we may have started
559*e4b17023SJohn Marino a change group within maybe_select_cc_mode, therefore we must continue. */
560*e4b17023SJohn Marino validate_change (insn, &XVECEXP (PATTERN (insn), 0, 1), x, true);
561*e4b17023SJohn Marino if (!apply_change_group ())
562*e4b17023SJohn Marino return false;
563*e4b17023SJohn Marino
564*e4b17023SJohn Marino /* Success. Delete the compare insn... */
565*e4b17023SJohn Marino delete_insn (cmp->insn);
566*e4b17023SJohn Marino
567*e4b17023SJohn Marino /* ... and any notes that are now invalid due to multiple sets. */
568*e4b17023SJohn Marino x = find_regno_note (insn, REG_UNUSED, targetm.flags_regnum);
569*e4b17023SJohn Marino if (x)
570*e4b17023SJohn Marino remove_note (insn, x);
571*e4b17023SJohn Marino x = find_reg_note (insn, REG_EQUAL, NULL);
572*e4b17023SJohn Marino if (x)
573*e4b17023SJohn Marino remove_note (insn, x);
574*e4b17023SJohn Marino x = find_reg_note (insn, REG_EQUIV, NULL);
575*e4b17023SJohn Marino if (x)
576*e4b17023SJohn Marino remove_note (insn, x);
577*e4b17023SJohn Marino
578*e4b17023SJohn Marino return true;
579*e4b17023SJohn Marino }
580*e4b17023SJohn Marino
581*e4b17023SJohn Marino /* Main entry point to the pass. */
582*e4b17023SJohn Marino
583*e4b17023SJohn Marino static unsigned int
execute_compare_elim_after_reload(void)584*e4b17023SJohn Marino execute_compare_elim_after_reload (void)
585*e4b17023SJohn Marino {
586*e4b17023SJohn Marino df_analyze ();
587*e4b17023SJohn Marino
588*e4b17023SJohn Marino gcc_checking_assert (all_compares == NULL);
589*e4b17023SJohn Marino
590*e4b17023SJohn Marino /* Locate all comparisons and their uses, and eliminate duplicates. */
591*e4b17023SJohn Marino find_comparisons ();
592*e4b17023SJohn Marino if (all_compares)
593*e4b17023SJohn Marino {
594*e4b17023SJohn Marino struct comparison *cmp;
595*e4b17023SJohn Marino size_t i;
596*e4b17023SJohn Marino
597*e4b17023SJohn Marino /* Eliminate comparisons that are redundant with flags computation. */
598*e4b17023SJohn Marino FOR_EACH_VEC_ELT (comparison_struct_p, all_compares, i, cmp)
599*e4b17023SJohn Marino {
600*e4b17023SJohn Marino try_eliminate_compare (cmp);
601*e4b17023SJohn Marino XDELETE (cmp);
602*e4b17023SJohn Marino }
603*e4b17023SJohn Marino
604*e4b17023SJohn Marino VEC_free (comparison_struct_p, heap, all_compares);
605*e4b17023SJohn Marino all_compares = NULL;
606*e4b17023SJohn Marino }
607*e4b17023SJohn Marino
608*e4b17023SJohn Marino return 0;
609*e4b17023SJohn Marino }
610*e4b17023SJohn Marino
611*e4b17023SJohn Marino static bool
gate_compare_elim_after_reload(void)612*e4b17023SJohn Marino gate_compare_elim_after_reload (void)
613*e4b17023SJohn Marino {
614*e4b17023SJohn Marino /* Setting this target hook value is how a backend indicates the need. */
615*e4b17023SJohn Marino if (targetm.flags_regnum == INVALID_REGNUM)
616*e4b17023SJohn Marino return false;
617*e4b17023SJohn Marino return flag_compare_elim_after_reload;
618*e4b17023SJohn Marino }
619*e4b17023SJohn Marino
620*e4b17023SJohn Marino struct rtl_opt_pass pass_compare_elim_after_reload =
621*e4b17023SJohn Marino {
622*e4b17023SJohn Marino {
623*e4b17023SJohn Marino RTL_PASS,
624*e4b17023SJohn Marino "cmpelim", /* name */
625*e4b17023SJohn Marino gate_compare_elim_after_reload, /* gate */
626*e4b17023SJohn Marino execute_compare_elim_after_reload, /* execute */
627*e4b17023SJohn Marino NULL, /* sub */
628*e4b17023SJohn Marino NULL, /* next */
629*e4b17023SJohn Marino 0, /* static_pass_number */
630*e4b17023SJohn Marino TV_NONE, /* tv_id */
631*e4b17023SJohn Marino 0, /* properties_required */
632*e4b17023SJohn Marino 0, /* properties_provided */
633*e4b17023SJohn Marino 0, /* properties_destroyed */
634*e4b17023SJohn Marino 0, /* todo_flags_start */
635*e4b17023SJohn Marino TODO_df_finish
636*e4b17023SJohn Marino | TODO_df_verify
637*e4b17023SJohn Marino | TODO_verify_rtl_sharing
638*e4b17023SJohn Marino | TODO_ggc_collect /* todo_flags_finish */
639*e4b17023SJohn Marino }
640*e4b17023SJohn Marino };
641