1 /* Post-reload compare elimination.
2 Copyright (C) 2010, 2011
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* There is a set of targets whose general-purpose move or addition
22 instructions clobber the flags. These targets cannot split their
23 CBRANCH/CSTORE etc patterns before reload is complete, lest reload
24 itself insert these instructions in between the flags setter and user.
25 Because these targets cannot split the compare from the use, they
26 cannot make use of the comparison elimination offered by the combine pass.
28 This is a small pass intended to provide comparison elimination similar to
29 what is available via NOTICE_UPDATE_CC for cc0 targets. This should help
30 encourage cc0 targets to convert to an explicit post-reload representation
35 (0) CBRANCH/CSTORE etc have been split in pass_split_after_reload.
37 (1) All comparison patterns are represented as
39 [(set (reg:CC) (compare:CC (reg) (immediate)))]
41 (2) All insn patterns that modify the flags are represented as
43 [(set (reg) (operation)
46 (3) If an insn of form (2) can usefully set the flags, there is
47 another pattern of the form
49 [(set (reg) (operation)
50 (set (reg:CCM) (compare:CCM (operation) (immediate)))]
52 The mode CCM will be chosen as if by SELECT_CC_MODE.
54 Note that unlike NOTICE_UPDATE_CC, we do not handle memory operands.
55 This could be handled as a future enhancement.
60 #include "coretypes.h"
64 #include "insn-config.h"
67 #include "basic-block.h"
68 #include "tree-pass.h"
74 /* These structures describe a comparison and how it is used. */
76 /* The choice of maximum 3 uses comes from wanting to eliminate the two
77 duplicate compares from a three-way branch on the sign of a value.
78 This is also sufficient to eliminate the duplicate compare against the
79 high-part of a double-word comparison. */
84 /* The instruction in which the result of the compare is used. */
86 /* The location of the flags register within the use. */
88 /* The comparison code applied against the flags register. */
94 /* The comparison instruction. */
97 /* The insn prior to the comparison insn that clobbers the flags. */
100 /* The two values being compared. These will be either REGs or
104 /* Information about how this comparison is used. */
105 struct comparison_use uses
[MAX_CMP_USE
];
107 /* The original CC_MODE for this comparison. */
108 enum machine_mode orig_mode
;
110 /* The number of uses identified for this comparison. */
111 unsigned short n_uses
;
113 /* True if not all uses of this comparison have been identified.
114 This can happen either for overflowing the array above, or if
115 the flags register is used in some unusual context. */
118 /* True if its inputs are still valid at the end of the block. */
122 typedef struct comparison
*comparison_struct_p
;
123 DEF_VEC_P(comparison_struct_p
);
124 DEF_VEC_ALLOC_P(comparison_struct_p
, heap
);
126 static VEC(comparison_struct_p
, heap
) *all_compares
;
128 /* Look for a "conforming" comparison, as defined above. If valid, return
129 the rtx for the COMPARE itself. */
132 conforming_compare (rtx insn
)
136 set
= single_set (insn
);
141 if (GET_CODE (src
) != COMPARE
)
144 dest
= SET_DEST (set
);
145 if (!REG_P (dest
) || REGNO (dest
) != targetm
.flags_regnum
)
148 if (REG_P (XEXP (src
, 0))
149 && REG_P (XEXP (src
, 0))
150 && (REG_P (XEXP (src
, 1)) || CONSTANT_P (XEXP (src
, 1))))
156 /* Look for a pattern of the "correct" form for an insn with a flags clobber
157 for which we may be able to eliminate a compare later. We're not looking
158 to validate any inputs at this time, merely see that the basic shape is
159 correct. The term "arithmetic" may be somewhat misleading... */
162 arithmetic_flags_clobber_p (rtx insn
)
166 if (!NONJUMP_INSN_P (insn
))
168 pat
= PATTERN (insn
);
169 if (extract_asm_operands (pat
))
172 if (GET_CODE (pat
) == PARALLEL
&& XVECLEN (pat
, 0) == 2)
174 x
= XVECEXP (pat
, 0, 0);
175 if (GET_CODE (x
) != SET
)
181 x
= XVECEXP (pat
, 0, 1);
182 if (GET_CODE (x
) == CLOBBER
)
185 if (REG_P (x
) && REGNO (x
) == targetm
.flags_regnum
)
193 /* Look for uses of FLAGS in INSN. If we find one we can analyze, record
194 it in CMP; otherwise indicate that we've missed a use. */
197 find_flags_uses_in_insn (struct comparison
*cmp
, rtx insn
)
199 df_ref
*use_rec
, use
;
201 /* If we've already lost track of uses, don't bother collecting more. */
202 if (cmp
->missing_uses
)
205 /* Find a USE of the flags register. */
206 for (use_rec
= DF_INSN_USES (insn
); (use
= *use_rec
) != NULL
; use_rec
++)
207 if (DF_REF_REGNO (use
) == targetm
.flags_regnum
)
211 /* If this is an unusual use, quit. */
212 if (DF_REF_TYPE (use
) != DF_REF_REG_USE
)
215 /* If we've run out of slots to record uses, quit. */
216 if (cmp
->n_uses
== MAX_CMP_USE
)
219 /* Unfortunately the location of the flags register, while present
220 in the reference structure, doesn't help. We need to find the
221 comparison code that is outer to the actual flags use. */
222 loc
= DF_REF_LOC (use
);
224 if (GET_CODE (x
) == PARALLEL
)
225 x
= XVECEXP (x
, 0, 0);
227 if (GET_CODE (x
) == IF_THEN_ELSE
)
230 && loc
== &XEXP (x
, 0)
231 && XEXP (x
, 1) == const0_rtx
)
233 /* We've found a use of the flags that we understand. */
234 struct comparison_use
*cuse
= &cmp
->uses
[cmp
->n_uses
++];
237 cuse
->code
= GET_CODE (x
);
245 /* We failed to recognize this use of the flags register. */
246 cmp
->missing_uses
= true;
249 /* Identify comparison instructions within BB. If the flags from the last
250 compare in the BB is live at the end of the block, install the compare
251 in BB->AUX. Called via walk_dominators_tree. */
254 find_comparisons_in_bb (struct dom_walk_data
*data ATTRIBUTE_UNUSED
,
257 struct comparison
*last_cmp
;
258 rtx insn
, next
, last_clobber
;
262 killed
= BITMAP_ALLOC (NULL
);
264 /* The last comparison that was made. Will be reset to NULL
265 once the flags are clobbered. */
268 /* True iff the last comparison has not been clobbered, nor
269 have its inputs. Used to eliminate duplicate compares. */
270 last_cmp_valid
= false;
272 /* The last insn that clobbered the flags, if that insn is of
273 a form that may be valid for eliminating a following compare.
274 To be reset to NULL once the flags are set otherwise. */
277 /* Propagate the last live comparison throughout the extended basic block. */
278 if (single_pred_p (bb
))
280 last_cmp
= (struct comparison
*) single_pred (bb
)->aux
;
282 last_cmp_valid
= last_cmp
->inputs_valid
;
285 for (insn
= BB_HEAD (bb
); insn
; insn
= next
)
289 next
= (insn
== BB_END (bb
) ? NULL_RTX
: NEXT_INSN (insn
));
290 if (!NONDEBUG_INSN_P (insn
))
293 /* Compute the set of registers modified by this instruction. */
294 bitmap_clear (killed
);
295 df_simulate_find_defs (insn
, killed
);
297 src
= conforming_compare (insn
);
300 /* Eliminate a compare that's redundant with the previous. */
302 && rtx_equal_p (last_cmp
->in_a
, XEXP (src
, 0))
303 && rtx_equal_p (last_cmp
->in_b
, XEXP (src
, 1)))
309 last_cmp
= XCNEW (struct comparison
);
310 last_cmp
->insn
= insn
;
311 last_cmp
->prev_clobber
= last_clobber
;
312 last_cmp
->in_a
= XEXP (src
, 0);
313 last_cmp
->in_b
= XEXP (src
, 1);
314 last_cmp
->orig_mode
= GET_MODE (SET_DEST (single_set (insn
)));
315 VEC_safe_push (comparison_struct_p
, heap
, all_compares
, last_cmp
);
317 /* It's unusual, but be prepared for comparison patterns that
318 also clobber an input, or perhaps a scratch. */
320 last_cmp_valid
= true;
323 /* Notice if this instruction kills the flags register. */
324 else if (bitmap_bit_p (killed
, targetm
.flags_regnum
))
326 /* See if this insn could be the "clobber" that eliminates
327 a future comparison. */
328 last_clobber
= (arithmetic_flags_clobber_p (insn
) ? insn
: NULL
);
330 /* In either case, the previous compare is no longer valid. */
332 last_cmp_valid
= false;
336 /* Notice if this instruction uses the flags register. */
338 find_flags_uses_in_insn (last_cmp
, insn
);
340 /* Notice if any of the inputs to the comparison have changed. */
342 && (bitmap_bit_p (killed
, REGNO (last_cmp
->in_a
))
343 || (REG_P (last_cmp
->in_b
)
344 && bitmap_bit_p (killed
, REGNO (last_cmp
->in_b
)))))
345 last_cmp_valid
= false;
348 BITMAP_FREE (killed
);
350 /* Remember the live comparison for subsequent members of
351 the extended basic block. */
355 last_cmp
->inputs_valid
= last_cmp_valid
;
357 /* Look to see if the flags register is live outgoing here, and
358 incoming to any successor not part of the extended basic block. */
359 if (bitmap_bit_p (&DF_LIVE_BB_INFO (bb
)->out
, targetm
.flags_regnum
))
364 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
366 basic_block dest
= e
->dest
;
367 if (bitmap_bit_p (&DF_LIVE_BB_INFO (dest
)->in
,
368 targetm
.flags_regnum
)
369 && !single_pred_p (dest
))
371 last_cmp
->missing_uses
= true;
379 /* Find all comparisons in the function. */
382 find_comparisons (void)
384 struct dom_walk_data data
;
386 memset (&data
, 0, sizeof(data
));
387 data
.dom_direction
= CDI_DOMINATORS
;
388 data
.before_dom_children
= find_comparisons_in_bb
;
390 calculate_dominance_info (CDI_DOMINATORS
);
392 init_walk_dominator_tree (&data
);
393 walk_dominator_tree (&data
, ENTRY_BLOCK_PTR
);
394 fini_walk_dominator_tree (&data
);
396 clear_aux_for_blocks ();
397 free_dominance_info (CDI_DOMINATORS
);
400 /* Select an alternate CC_MODE for a comparison insn comparing A and B.
401 Note that inputs are almost certainly different than the IN_A and IN_B
402 stored in CMP -- we're called while attempting to eliminate the compare
403 after all. Return the new FLAGS rtx if successful, else return NULL.
404 Note that this function may start a change group. */
407 maybe_select_cc_mode (struct comparison
*cmp
, rtx a ATTRIBUTE_UNUSED
,
408 rtx b ATTRIBUTE_UNUSED
)
410 enum machine_mode sel_mode
;
411 const int n
= cmp
->n_uses
;
414 #ifndef SELECT_CC_MODE
415 /* Minimize code differences when this target macro is undefined. */
417 #define SELECT_CC_MODE(A,B,C) (gcc_unreachable (), VOIDmode)
420 /* If we don't have access to all of the uses, we can't validate. */
421 if (cmp
->missing_uses
|| n
== 0)
424 /* Find a new mode that works for all of the uses. Special case the
425 common case of exactly one use. */
428 sel_mode
= SELECT_CC_MODE (cmp
->uses
[0].code
, a
, b
);
429 if (sel_mode
!= cmp
->orig_mode
)
431 flags
= gen_rtx_REG (sel_mode
, targetm
.flags_regnum
);
432 validate_change (cmp
->uses
[0].insn
, cmp
->uses
[0].loc
, flags
, true);
439 sel_mode
= SELECT_CC_MODE (cmp
->uses
[0].code
, a
, b
);
440 for (i
= 1; i
< n
; ++i
)
442 enum machine_mode new_mode
;
443 new_mode
= SELECT_CC_MODE (cmp
->uses
[i
].code
, a
, b
);
444 if (new_mode
!= sel_mode
)
446 sel_mode
= targetm
.cc_modes_compatible (sel_mode
, new_mode
);
447 if (sel_mode
== VOIDmode
)
452 if (sel_mode
!= cmp
->orig_mode
)
454 flags
= gen_rtx_REG (sel_mode
, targetm
.flags_regnum
);
455 for (i
= 0; i
< n
; ++i
)
456 validate_change (cmp
->uses
[i
].insn
, cmp
->uses
[i
].loc
, flags
, true);
463 /* Attempt to replace a comparison with a prior arithmetic insn that can
464 compute the same flags value as the comparison itself. Return true if
465 successful, having made all rtl modifications necessary. */
468 try_eliminate_compare (struct comparison
*cmp
)
470 rtx x
, insn
, bb_head
, flags
, in_a
, cmp_src
;
472 /* We must have found an interesting "clobber" preceeding the compare. */
473 if (cmp
->prev_clobber
== NULL
)
476 /* ??? For the moment we don't handle comparisons for which IN_B
477 is a register. We accepted these during initial comparison
478 recognition in order to eliminate duplicate compares.
479 An improvement here would be to handle x = a - b; if (a cmp b). */
480 if (!CONSTANT_P (cmp
->in_b
))
483 /* Verify that IN_A is not clobbered in between CMP and PREV_CLOBBER.
484 Given that this target requires this pass, we can assume that most
485 insns do clobber the flags, and so the distance between the compare
486 and the clobber is likely to be small. */
487 /* ??? This is one point at which one could argue that DF_REF_CHAIN would
488 be useful, but it is thought to be too heavy-weight a solution here. */
492 bb_head
= BB_HEAD (BLOCK_FOR_INSN (insn
));
493 for (insn
= PREV_INSN (insn
);
494 insn
!= cmp
->prev_clobber
;
495 insn
= PREV_INSN (insn
))
497 const int abnormal_flags
498 = (DF_REF_CONDITIONAL
| DF_REF_PARTIAL
| DF_REF_MAY_CLOBBER
499 | DF_REF_MUST_CLOBBER
| DF_REF_SIGN_EXTRACT
500 | DF_REF_ZERO_EXTRACT
| DF_REF_STRICT_LOW_PART
501 | DF_REF_PRE_POST_MODIFY
);
502 df_ref
*def_rec
, def
;
504 /* Note that the BB_HEAD is always either a note or a label, but in
505 any case it means that IN_A is defined outside the block. */
508 if (NOTE_P (insn
) || DEBUG_INSN_P (insn
))
511 /* Find a possible def of IN_A in INSN. */
512 for (def_rec
= DF_INSN_DEFS (insn
); (def
= *def_rec
) != NULL
; def_rec
++)
513 if (DF_REF_REGNO (def
) == REGNO (in_a
))
516 /* No definitions of IN_A; continue searching. */
520 /* Bail if this is not a totally normal set of IN_A. */
521 if (DF_REF_IS_ARTIFICIAL (def
))
523 if (DF_REF_FLAGS (def
) & abnormal_flags
)
526 /* We've found an insn between the compare and the clobber that sets
527 IN_A. Given that pass_cprop_hardreg has not yet run, we still find
528 situations in which we can usefully look through a copy insn. */
529 x
= single_set (insn
);
537 /* We've reached PREV_CLOBBER without finding a modification of IN_A.
538 Validate that PREV_CLOBBER itself does in fact refer to IN_A. Do
539 recall that we've already validated the shape of PREV_CLOBBER. */
540 x
= XVECEXP (PATTERN (insn
), 0, 0);
541 if (!rtx_equal_p (SET_DEST (x
), in_a
))
543 cmp_src
= SET_SRC (x
);
545 /* Determine if we ought to use a different CC_MODE here. */
546 flags
= maybe_select_cc_mode (cmp
, cmp_src
, cmp
->in_b
);
548 flags
= gen_rtx_REG (cmp
->orig_mode
, targetm
.flags_regnum
);
550 /* Generate a new comparison for installation in the setter. */
551 x
= copy_rtx (cmp_src
);
552 x
= gen_rtx_COMPARE (GET_MODE (flags
), x
, cmp
->in_b
);
553 x
= gen_rtx_SET (VOIDmode
, flags
, x
);
555 /* Succeed if the new instruction is valid. Note that we may have started
556 a change group within maybe_select_cc_mode, therefore we must continue. */
557 validate_change (insn
, &XVECEXP (PATTERN (insn
), 0, 1), x
, true);
558 if (!apply_change_group ())
561 /* Success. Delete the compare insn... */
562 delete_insn (cmp
->insn
);
564 /* ... and any notes that are now invalid due to multiple sets. */
565 x
= find_regno_note (insn
, REG_UNUSED
, targetm
.flags_regnum
);
567 remove_note (insn
, x
);
568 x
= find_reg_note (insn
, REG_EQUAL
, NULL
);
570 remove_note (insn
, x
);
571 x
= find_reg_note (insn
, REG_EQUIV
, NULL
);
573 remove_note (insn
, x
);
578 /* Main entry point to the pass. */
581 execute_compare_elim_after_reload (void)
583 df_set_flags (DF_DEFER_INSN_RESCAN
);
584 df_live_add_problem ();
587 gcc_checking_assert (all_compares
== NULL
);
589 /* Locate all comparisons and their uses, and eliminate duplicates. */
593 struct comparison
*cmp
;
596 /* Eliminate comparisons that are redundant with flags computation. */
597 FOR_EACH_VEC_ELT (comparison_struct_p
, all_compares
, i
, cmp
)
599 try_eliminate_compare (cmp
);
603 VEC_free (comparison_struct_p
, heap
, all_compares
);
613 gate_compare_elim_after_reload (void)
615 /* Setting this target hook value is how a backend indicates the need. */
616 if (targetm
.flags_regnum
== INVALID_REGNUM
)
618 return flag_compare_elim_after_reload
;
621 struct rtl_opt_pass pass_compare_elim_after_reload
=
625 "cmpelim", /* name */
626 gate_compare_elim_after_reload
, /* gate */
627 execute_compare_elim_after_reload
, /* execute */
630 0, /* static_pass_number */
632 0, /* properties_required */
633 0, /* properties_provided */
634 0, /* properties_destroyed */
635 0, /* todo_flags_start */
638 | TODO_verify_rtl_sharing
639 | TODO_ggc_collect
/* todo_flags_finish */