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 enum machine_mode src_mode
= GET_MODE (src
);
302 /* Eliminate a compare that's redundant with the previous. */
304 && rtx_equal_p (last_cmp
->in_a
, XEXP (src
, 0))
305 && rtx_equal_p (last_cmp
->in_b
, XEXP (src
, 1)))
308 enum machine_mode new_mode
309 = targetm
.cc_modes_compatible (last_cmp
->orig_mode
, src_mode
);
311 /* New mode is incompatible with the previous compare mode. */
312 if (new_mode
== VOIDmode
)
315 if (new_mode
!= last_cmp
->orig_mode
)
317 flags
= gen_rtx_REG (src_mode
, targetm
.flags_regnum
);
319 /* Generate new comparison for substitution. */
320 x
= gen_rtx_COMPARE (new_mode
, XEXP (src
, 0), XEXP (src
, 1));
321 x
= gen_rtx_SET (VOIDmode
, flags
, x
);
323 if (!validate_change (last_cmp
->insn
,
324 &PATTERN (last_cmp
->insn
), x
, false))
327 last_cmp
->orig_mode
= new_mode
;
334 last_cmp
= XCNEW (struct comparison
);
335 last_cmp
->insn
= insn
;
336 last_cmp
->prev_clobber
= last_clobber
;
337 last_cmp
->in_a
= XEXP (src
, 0);
338 last_cmp
->in_b
= XEXP (src
, 1);
339 last_cmp
->orig_mode
= src_mode
;
340 VEC_safe_push (comparison_struct_p
, heap
, all_compares
, last_cmp
);
342 /* It's unusual, but be prepared for comparison patterns that
343 also clobber an input, or perhaps a scratch. */
345 last_cmp_valid
= true;
348 /* Notice if this instruction kills the flags register. */
349 else if (bitmap_bit_p (killed
, targetm
.flags_regnum
))
351 /* See if this insn could be the "clobber" that eliminates
352 a future comparison. */
353 last_clobber
= (arithmetic_flags_clobber_p (insn
) ? insn
: NULL
);
355 /* In either case, the previous compare is no longer valid. */
357 last_cmp_valid
= false;
361 /* Notice if this instruction uses the flags register. */
363 find_flags_uses_in_insn (last_cmp
, insn
);
365 /* Notice if any of the inputs to the comparison have changed. */
367 && (bitmap_bit_p (killed
, REGNO (last_cmp
->in_a
))
368 || (REG_P (last_cmp
->in_b
)
369 && bitmap_bit_p (killed
, REGNO (last_cmp
->in_b
)))))
370 last_cmp_valid
= false;
373 BITMAP_FREE (killed
);
375 /* Remember the live comparison for subsequent members of
376 the extended basic block. */
380 last_cmp
->inputs_valid
= last_cmp_valid
;
382 /* Look to see if the flags register is live outgoing here, and
383 incoming to any successor not part of the extended basic block. */
384 if (bitmap_bit_p (df_get_live_out (bb
), targetm
.flags_regnum
))
389 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
391 basic_block dest
= e
->dest
;
392 if (bitmap_bit_p (df_get_live_in (bb
),
393 targetm
.flags_regnum
)
394 && !single_pred_p (dest
))
396 last_cmp
->missing_uses
= true;
404 /* Find all comparisons in the function. */
407 find_comparisons (void)
409 struct dom_walk_data data
;
411 memset (&data
, 0, sizeof(data
));
412 data
.dom_direction
= CDI_DOMINATORS
;
413 data
.before_dom_children
= find_comparisons_in_bb
;
415 calculate_dominance_info (CDI_DOMINATORS
);
417 init_walk_dominator_tree (&data
);
418 walk_dominator_tree (&data
, ENTRY_BLOCK_PTR
);
419 fini_walk_dominator_tree (&data
);
421 clear_aux_for_blocks ();
422 free_dominance_info (CDI_DOMINATORS
);
425 /* Select an alternate CC_MODE for a comparison insn comparing A and B.
426 Note that inputs are almost certainly different than the IN_A and IN_B
427 stored in CMP -- we're called while attempting to eliminate the compare
428 after all. Return the new FLAGS rtx if successful, else return NULL.
429 Note that this function may start a change group. */
432 maybe_select_cc_mode (struct comparison
*cmp
, rtx a ATTRIBUTE_UNUSED
,
433 rtx b ATTRIBUTE_UNUSED
)
435 enum machine_mode sel_mode
;
436 const int n
= cmp
->n_uses
;
439 #ifndef SELECT_CC_MODE
440 /* Minimize code differences when this target macro is undefined. */
442 #define SELECT_CC_MODE(A,B,C) (gcc_unreachable (), VOIDmode)
445 /* If we don't have access to all of the uses, we can't validate. */
446 if (cmp
->missing_uses
|| n
== 0)
449 /* Find a new mode that works for all of the uses. Special case the
450 common case of exactly one use. */
453 sel_mode
= SELECT_CC_MODE (cmp
->uses
[0].code
, a
, b
);
454 if (sel_mode
!= cmp
->orig_mode
)
456 flags
= gen_rtx_REG (sel_mode
, targetm
.flags_regnum
);
457 validate_change (cmp
->uses
[0].insn
, cmp
->uses
[0].loc
, flags
, true);
464 sel_mode
= SELECT_CC_MODE (cmp
->uses
[0].code
, a
, b
);
465 for (i
= 1; i
< n
; ++i
)
467 enum machine_mode new_mode
;
468 new_mode
= SELECT_CC_MODE (cmp
->uses
[i
].code
, a
, b
);
469 if (new_mode
!= sel_mode
)
471 sel_mode
= targetm
.cc_modes_compatible (sel_mode
, new_mode
);
472 if (sel_mode
== VOIDmode
)
477 if (sel_mode
!= cmp
->orig_mode
)
479 flags
= gen_rtx_REG (sel_mode
, targetm
.flags_regnum
);
480 for (i
= 0; i
< n
; ++i
)
481 validate_change (cmp
->uses
[i
].insn
, cmp
->uses
[i
].loc
, flags
, true);
488 /* Attempt to replace a comparison with a prior arithmetic insn that can
489 compute the same flags value as the comparison itself. Return true if
490 successful, having made all rtl modifications necessary. */
493 try_eliminate_compare (struct comparison
*cmp
)
495 rtx x
, insn
, bb_head
, flags
, in_a
, cmp_src
;
497 /* We must have found an interesting "clobber" preceding the compare. */
498 if (cmp
->prev_clobber
== NULL
)
501 /* ??? For the moment we don't handle comparisons for which IN_B
502 is a register. We accepted these during initial comparison
503 recognition in order to eliminate duplicate compares.
504 An improvement here would be to handle x = a - b; if (a cmp b). */
505 if (!CONSTANT_P (cmp
->in_b
))
508 /* Verify that IN_A is not clobbered in between CMP and PREV_CLOBBER.
509 Given that this target requires this pass, we can assume that most
510 insns do clobber the flags, and so the distance between the compare
511 and the clobber is likely to be small. */
512 /* ??? This is one point at which one could argue that DF_REF_CHAIN would
513 be useful, but it is thought to be too heavy-weight a solution here. */
517 bb_head
= BB_HEAD (BLOCK_FOR_INSN (insn
));
518 for (insn
= PREV_INSN (insn
);
519 insn
!= cmp
->prev_clobber
;
520 insn
= PREV_INSN (insn
))
522 const int abnormal_flags
523 = (DF_REF_CONDITIONAL
| DF_REF_PARTIAL
| DF_REF_MAY_CLOBBER
524 | DF_REF_MUST_CLOBBER
| DF_REF_SIGN_EXTRACT
525 | DF_REF_ZERO_EXTRACT
| DF_REF_STRICT_LOW_PART
526 | DF_REF_PRE_POST_MODIFY
);
527 df_ref
*def_rec
, def
;
529 /* Note that the BB_HEAD is always either a note or a label, but in
530 any case it means that IN_A is defined outside the block. */
533 if (NOTE_P (insn
) || DEBUG_INSN_P (insn
))
536 /* Find a possible def of IN_A in INSN. */
537 for (def_rec
= DF_INSN_DEFS (insn
); (def
= *def_rec
) != NULL
; def_rec
++)
538 if (DF_REF_REGNO (def
) == REGNO (in_a
))
541 /* No definitions of IN_A; continue searching. */
545 /* Bail if this is not a totally normal set of IN_A. */
546 if (DF_REF_IS_ARTIFICIAL (def
))
548 if (DF_REF_FLAGS (def
) & abnormal_flags
)
551 /* We've found an insn between the compare and the clobber that sets
552 IN_A. Given that pass_cprop_hardreg has not yet run, we still find
553 situations in which we can usefully look through a copy insn. */
554 x
= single_set (insn
);
562 /* We've reached PREV_CLOBBER without finding a modification of IN_A.
563 Validate that PREV_CLOBBER itself does in fact refer to IN_A. Do
564 recall that we've already validated the shape of PREV_CLOBBER. */
565 x
= XVECEXP (PATTERN (insn
), 0, 0);
566 if (rtx_equal_p (SET_DEST (x
), in_a
))
567 cmp_src
= SET_SRC (x
);
569 /* Also check operations with implicit extensions, e.g.:
571 (zero_extend:DI (plus:SI (reg:SI)(reg:SI))))
574 (plus:SI (reg:SI)(reg:SI))
576 else if (REG_P (SET_DEST (x
))
578 && REGNO (SET_DEST (x
)) == REGNO (in_a
)
579 && (GET_CODE (SET_SRC (x
)) == ZERO_EXTEND
580 || GET_CODE (SET_SRC (x
)) == SIGN_EXTEND
)
581 && GET_MODE (XEXP (SET_SRC (x
), 0)) == GET_MODE (in_a
))
582 cmp_src
= XEXP (SET_SRC (x
), 0);
586 /* Determine if we ought to use a different CC_MODE here. */
587 flags
= maybe_select_cc_mode (cmp
, cmp_src
, cmp
->in_b
);
589 flags
= gen_rtx_REG (cmp
->orig_mode
, targetm
.flags_regnum
);
591 /* Generate a new comparison for installation in the setter. */
592 x
= copy_rtx (cmp_src
);
593 x
= gen_rtx_COMPARE (GET_MODE (flags
), x
, cmp
->in_b
);
594 x
= gen_rtx_SET (VOIDmode
, flags
, x
);
596 /* Succeed if the new instruction is valid. Note that we may have started
597 a change group within maybe_select_cc_mode, therefore we must continue. */
598 validate_change (insn
, &XVECEXP (PATTERN (insn
), 0, 1), x
, true);
599 if (!apply_change_group ())
602 /* Success. Delete the compare insn... */
603 delete_insn (cmp
->insn
);
605 /* ... and any notes that are now invalid due to multiple sets. */
606 x
= find_regno_note (insn
, REG_UNUSED
, targetm
.flags_regnum
);
608 remove_note (insn
, x
);
609 x
= find_reg_note (insn
, REG_EQUAL
, NULL
);
611 remove_note (insn
, x
);
612 x
= find_reg_note (insn
, REG_EQUIV
, NULL
);
614 remove_note (insn
, x
);
619 /* Main entry point to the pass. */
622 execute_compare_elim_after_reload (void)
626 gcc_checking_assert (all_compares
== NULL
);
628 /* Locate all comparisons and their uses, and eliminate duplicates. */
632 struct comparison
*cmp
;
635 /* Eliminate comparisons that are redundant with flags computation. */
636 FOR_EACH_VEC_ELT (comparison_struct_p
, all_compares
, i
, cmp
)
638 try_eliminate_compare (cmp
);
642 VEC_free (comparison_struct_p
, heap
, all_compares
);
650 gate_compare_elim_after_reload (void)
652 /* Setting this target hook value is how a backend indicates the need. */
653 if (targetm
.flags_regnum
== INVALID_REGNUM
)
655 return flag_compare_elim_after_reload
;
658 struct rtl_opt_pass pass_compare_elim_after_reload
=
662 "cmpelim", /* name */
663 gate_compare_elim_after_reload
, /* gate */
664 execute_compare_elim_after_reload
, /* execute */
667 0, /* static_pass_number */
669 0, /* properties_required */
670 0, /* properties_provided */
671 0, /* properties_destroyed */
672 0, /* todo_flags_start */
675 | TODO_verify_rtl_sharing
676 | TODO_ggc_collect
/* todo_flags_finish */