1 /* Post-reload compare elimination.
2 Copyright (C) 2010-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* There is a set of targets whose general-purpose move or addition
21 instructions clobber the flags. These targets cannot split their
22 CBRANCH/CSTORE etc patterns before reload is complete, lest reload
23 itself insert these instructions in between the flags setter and user.
24 Because these targets cannot split the compare from the use, they
25 cannot make use of the comparison elimination offered by the combine pass.
27 This is a small pass intended to provide comparison elimination similar to
28 what is available via NOTICE_UPDATE_CC for cc0 targets. This should help
29 encourage cc0 targets to convert to an explicit post-reload representation
34 (0) CBRANCH/CSTORE etc have been split in pass_split_after_reload.
36 (1) All comparison patterns are represented as
38 [(set (reg:CC) (compare:CC (reg) (reg_or_immediate)))]
40 (2) All insn patterns that modify the flags are represented as
42 [(set (reg) (operation)
45 (3) If an insn of form (2) can usefully set the flags, there is
46 another pattern of the form
48 [(set (reg) (operation)
49 (set (reg:CCM) (compare:CCM (operation) (immediate)))]
51 The mode CCM will be chosen as if by SELECT_CC_MODE.
53 Note that unlike NOTICE_UPDATE_CC, we do not handle memory operands.
54 This could be handled as a future enhancement.
59 #include "coretypes.h"
63 #include "insn-config.h"
67 #include "hard-reg-set.h"
70 #include "dominance.h"
73 #include "basic-block.h"
74 #include "tree-pass.h"
80 /* These structures describe a comparison and how it is used. */
82 /* The choice of maximum 3 uses comes from wanting to eliminate the two
83 duplicate compares from a three-way branch on the sign of a value.
84 This is also sufficient to eliminate the duplicate compare against the
85 high-part of a double-word comparison. */
90 /* The instruction in which the result of the compare is used. */
92 /* The location of the flags register within the use. */
94 /* The comparison code applied against the flags register. */
100 /* The comparison instruction. */
103 /* The insn prior to the comparison insn that clobbers the flags. */
104 rtx_insn
*prev_clobber
;
106 /* The two values being compared. These will be either REGs or
110 /* The REG_EH_REGION of the comparison. */
113 /* Information about how this comparison is used. */
114 struct comparison_use uses
[MAX_CMP_USE
];
116 /* The original CC_MODE for this comparison. */
117 machine_mode orig_mode
;
119 /* The number of uses identified for this comparison. */
120 unsigned short n_uses
;
122 /* True if not all uses of this comparison have been identified.
123 This can happen either for overflowing the array above, or if
124 the flags register is used in some unusual context. */
127 /* True if its inputs are still valid at the end of the block. */
131 typedef struct comparison
*comparison_struct_p
;
133 static vec
<comparison_struct_p
> all_compares
;
135 /* Look for a "conforming" comparison, as defined above. If valid, return
136 the rtx for the COMPARE itself. */
139 conforming_compare (rtx_insn
*insn
)
143 set
= single_set (insn
);
148 if (GET_CODE (src
) != COMPARE
)
151 dest
= SET_DEST (set
);
152 if (!REG_P (dest
) || REGNO (dest
) != targetm
.flags_regnum
)
155 if (REG_P (XEXP (src
, 0))
156 && (REG_P (XEXP (src
, 1)) || CONSTANT_P (XEXP (src
, 1))))
162 /* Look for a pattern of the "correct" form for an insn with a flags clobber
163 for which we may be able to eliminate a compare later. We're not looking
164 to validate any inputs at this time, merely see that the basic shape is
165 correct. The term "arithmetic" may be somewhat misleading... */
168 arithmetic_flags_clobber_p (rtx_insn
*insn
)
172 if (!NONJUMP_INSN_P (insn
))
174 pat
= PATTERN (insn
);
175 if (extract_asm_operands (pat
))
178 if (GET_CODE (pat
) == PARALLEL
&& XVECLEN (pat
, 0) == 2)
180 x
= XVECEXP (pat
, 0, 0);
181 if (GET_CODE (x
) != SET
)
187 x
= XVECEXP (pat
, 0, 1);
188 if (GET_CODE (x
) == CLOBBER
)
191 if (REG_P (x
) && REGNO (x
) == targetm
.flags_regnum
)
199 /* Look for uses of FLAGS in INSN. If we find one we can analyze, record
200 it in CMP; otherwise indicate that we've missed a use. */
203 find_flags_uses_in_insn (struct comparison
*cmp
, rtx_insn
*insn
)
207 /* If we've already lost track of uses, don't bother collecting more. */
208 if (cmp
->missing_uses
)
211 /* Find a USE of the flags register. */
212 FOR_EACH_INSN_USE (use
, insn
)
213 if (DF_REF_REGNO (use
) == targetm
.flags_regnum
)
217 /* If this is an unusual use, quit. */
218 if (DF_REF_TYPE (use
) != DF_REF_REG_USE
)
221 /* If we've run out of slots to record uses, quit. */
222 if (cmp
->n_uses
== MAX_CMP_USE
)
225 /* Unfortunately the location of the flags register, while present
226 in the reference structure, doesn't help. We need to find the
227 comparison code that is outer to the actual flags use. */
228 loc
= DF_REF_LOC (use
);
230 if (GET_CODE (x
) == PARALLEL
)
231 x
= XVECEXP (x
, 0, 0);
233 if (GET_CODE (x
) == IF_THEN_ELSE
)
236 && loc
== &XEXP (x
, 0)
237 && XEXP (x
, 1) == const0_rtx
)
239 /* We've found a use of the flags that we understand. */
240 struct comparison_use
*cuse
= &cmp
->uses
[cmp
->n_uses
++];
243 cuse
->code
= GET_CODE (x
);
251 /* We failed to recognize this use of the flags register. */
252 cmp
->missing_uses
= true;
255 class find_comparison_dom_walker
: public dom_walker
258 find_comparison_dom_walker (cdi_direction direction
)
259 : dom_walker (direction
) {}
261 virtual void before_dom_children (basic_block
);
264 /* Return true if conforming COMPARE with EH_NOTE is redundant with comparison
265 CMP and can thus be eliminated. */
268 can_eliminate_compare (rtx compare
, rtx eh_note
, struct comparison
*cmp
)
270 /* Take care that it's in the same EH region. */
271 if (cfun
->can_throw_non_call_exceptions
272 && !rtx_equal_p (eh_note
, cmp
->eh_note
))
275 /* Make sure the compare is redundant with the previous. */
276 if (!rtx_equal_p (XEXP (compare
, 0), cmp
->in_a
)
277 || !rtx_equal_p (XEXP (compare
, 1), cmp
->in_b
))
280 /* New mode must be compatible with the previous compare mode. */
281 enum machine_mode new_mode
282 = targetm
.cc_modes_compatible (GET_MODE (compare
), cmp
->orig_mode
);
284 if (new_mode
== VOIDmode
)
287 if (cmp
->orig_mode
!= new_mode
)
289 /* Generate new comparison for substitution. */
290 rtx flags
= gen_rtx_REG (new_mode
, targetm
.flags_regnum
);
291 rtx x
= gen_rtx_COMPARE (new_mode
, cmp
->in_a
, cmp
->in_b
);
292 x
= gen_rtx_SET (flags
, x
);
294 if (!validate_change (cmp
->insn
, &PATTERN (cmp
->insn
), x
, false))
297 cmp
->orig_mode
= new_mode
;
303 /* Identify comparison instructions within BB. If the flags from the last
304 compare in the BB is live at the end of the block, install the compare
305 in BB->AUX. Called via dom_walker.walk (). */
308 find_comparison_dom_walker::before_dom_children (basic_block bb
)
310 struct comparison
*last_cmp
;
311 rtx_insn
*insn
, *next
, *last_clobber
;
313 bool need_purge
= false;
316 killed
= BITMAP_ALLOC (NULL
);
318 /* The last comparison that was made. Will be reset to NULL
319 once the flags are clobbered. */
322 /* True iff the last comparison has not been clobbered, nor
323 have its inputs. Used to eliminate duplicate compares. */
324 last_cmp_valid
= false;
326 /* The last insn that clobbered the flags, if that insn is of
327 a form that may be valid for eliminating a following compare.
328 To be reset to NULL once the flags are set otherwise. */
331 /* Propagate the last live comparison throughout the extended basic block. */
332 if (single_pred_p (bb
))
334 last_cmp
= (struct comparison
*) single_pred (bb
)->aux
;
336 last_cmp_valid
= last_cmp
->inputs_valid
;
339 for (insn
= BB_HEAD (bb
); insn
; insn
= next
)
343 next
= (insn
== BB_END (bb
) ? NULL
: NEXT_INSN (insn
));
344 if (!NONDEBUG_INSN_P (insn
))
347 /* Compute the set of registers modified by this instruction. */
348 bitmap_clear (killed
);
349 df_simulate_find_defs (insn
, killed
);
351 src
= conforming_compare (insn
);
356 if (cfun
->can_throw_non_call_exceptions
)
357 eh_note
= find_reg_note (insn
, REG_EH_REGION
, NULL
);
359 if (last_cmp_valid
&& can_eliminate_compare (src
, eh_note
, last_cmp
))
367 last_cmp
= XCNEW (struct comparison
);
368 last_cmp
->insn
= insn
;
369 last_cmp
->prev_clobber
= last_clobber
;
370 last_cmp
->in_a
= XEXP (src
, 0);
371 last_cmp
->in_b
= XEXP (src
, 1);
372 last_cmp
->eh_note
= eh_note
;
373 last_cmp
->orig_mode
= GET_MODE (src
);
374 all_compares
.safe_push (last_cmp
);
376 /* It's unusual, but be prepared for comparison patterns that
377 also clobber an input, or perhaps a scratch. */
379 last_cmp_valid
= true;
382 /* Notice if this instruction kills the flags register. */
383 else if (bitmap_bit_p (killed
, targetm
.flags_regnum
))
385 /* See if this insn could be the "clobber" that eliminates
386 a future comparison. */
387 last_clobber
= (arithmetic_flags_clobber_p (insn
) ? insn
: NULL
);
389 /* In either case, the previous compare is no longer valid. */
391 last_cmp_valid
= false;
394 /* Notice if this instruction uses the flags register. */
396 find_flags_uses_in_insn (last_cmp
, insn
);
398 /* Notice if any of the inputs to the comparison have changed. */
400 && (bitmap_bit_p (killed
, REGNO (last_cmp
->in_a
))
401 || (REG_P (last_cmp
->in_b
)
402 && bitmap_bit_p (killed
, REGNO (last_cmp
->in_b
)))))
403 last_cmp_valid
= false;
406 BITMAP_FREE (killed
);
408 /* Remember the live comparison for subsequent members of
409 the extended basic block. */
413 last_cmp
->inputs_valid
= last_cmp_valid
;
415 /* Look to see if the flags register is live outgoing here, and
416 incoming to any successor not part of the extended basic block. */
417 if (bitmap_bit_p (df_get_live_out (bb
), targetm
.flags_regnum
))
422 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
424 basic_block dest
= e
->dest
;
425 if (bitmap_bit_p (df_get_live_in (bb
), targetm
.flags_regnum
)
426 && !single_pred_p (dest
))
428 last_cmp
->missing_uses
= true;
435 /* If we deleted a compare with a REG_EH_REGION note, we may need to
438 purge_dead_edges (bb
);
441 /* Find all comparisons in the function. */
444 find_comparisons (void)
446 calculate_dominance_info (CDI_DOMINATORS
);
448 find_comparison_dom_walker (CDI_DOMINATORS
)
449 .walk (cfun
->cfg
->x_entry_block_ptr
);
451 clear_aux_for_blocks ();
452 free_dominance_info (CDI_DOMINATORS
);
455 /* Select an alternate CC_MODE for a comparison insn comparing A and B.
456 Note that inputs are almost certainly different than the IN_A and IN_B
457 stored in CMP -- we're called while attempting to eliminate the compare
458 after all. Return the new FLAGS rtx if successful, else return NULL.
459 Note that this function may start a change group. */
462 maybe_select_cc_mode (struct comparison
*cmp
, rtx a ATTRIBUTE_UNUSED
,
463 rtx b ATTRIBUTE_UNUSED
)
465 machine_mode sel_mode
;
466 const int n
= cmp
->n_uses
;
469 #ifndef SELECT_CC_MODE
470 /* Minimize code differences when this target macro is undefined. */
472 #define SELECT_CC_MODE(A,B,C) (gcc_unreachable (), VOIDmode)
475 /* If we don't have access to all of the uses, we can't validate. */
476 if (cmp
->missing_uses
|| n
== 0)
479 /* Find a new mode that works for all of the uses. Special case the
480 common case of exactly one use. */
483 sel_mode
= SELECT_CC_MODE (cmp
->uses
[0].code
, a
, b
);
484 if (sel_mode
!= cmp
->orig_mode
)
486 flags
= gen_rtx_REG (sel_mode
, targetm
.flags_regnum
);
487 validate_change (cmp
->uses
[0].insn
, cmp
->uses
[0].loc
, flags
, true);
494 sel_mode
= SELECT_CC_MODE (cmp
->uses
[0].code
, a
, b
);
495 for (i
= 1; i
< n
; ++i
)
497 machine_mode new_mode
= SELECT_CC_MODE (cmp
->uses
[i
].code
, a
, b
);
498 if (new_mode
!= sel_mode
)
500 sel_mode
= targetm
.cc_modes_compatible (sel_mode
, new_mode
);
501 if (sel_mode
== VOIDmode
)
506 if (sel_mode
!= cmp
->orig_mode
)
508 flags
= gen_rtx_REG (sel_mode
, targetm
.flags_regnum
);
509 for (i
= 0; i
< n
; ++i
)
510 validate_change (cmp
->uses
[i
].insn
, cmp
->uses
[i
].loc
, flags
, true);
517 /* Attempt to replace a comparison with a prior arithmetic insn that can
518 compute the same flags value as the comparison itself. Return true if
519 successful, having made all rtl modifications necessary. */
522 try_eliminate_compare (struct comparison
*cmp
)
524 rtx_insn
*insn
, *bb_head
;
525 rtx x
, flags
, in_a
, cmp_src
;
527 /* We must have found an interesting "clobber" preceding the compare. */
528 if (cmp
->prev_clobber
== NULL
)
531 /* ??? For the moment we don't handle comparisons for which IN_B
532 is a register. We accepted these during initial comparison
533 recognition in order to eliminate duplicate compares.
534 An improvement here would be to handle x = a - b; if (a cmp b). */
535 if (!CONSTANT_P (cmp
->in_b
))
538 /* Verify that IN_A is not clobbered in between CMP and PREV_CLOBBER.
539 Given that this target requires this pass, we can assume that most
540 insns do clobber the flags, and so the distance between the compare
541 and the clobber is likely to be small. */
542 /* ??? This is one point at which one could argue that DF_REF_CHAIN would
543 be useful, but it is thought to be too heavy-weight a solution here. */
547 bb_head
= BB_HEAD (BLOCK_FOR_INSN (insn
));
548 for (insn
= PREV_INSN (insn
);
549 insn
!= cmp
->prev_clobber
;
550 insn
= PREV_INSN (insn
))
552 const int abnormal_flags
553 = (DF_REF_CONDITIONAL
| DF_REF_PARTIAL
| DF_REF_MAY_CLOBBER
554 | DF_REF_MUST_CLOBBER
| DF_REF_SIGN_EXTRACT
555 | DF_REF_ZERO_EXTRACT
| DF_REF_STRICT_LOW_PART
556 | DF_REF_PRE_POST_MODIFY
);
559 /* Note that the BB_HEAD is always either a note or a label, but in
560 any case it means that IN_A is defined outside the block. */
563 if (NOTE_P (insn
) || DEBUG_INSN_P (insn
))
566 /* Find a possible def of IN_A in INSN. */
567 FOR_EACH_INSN_DEF (def
, insn
)
568 if (DF_REF_REGNO (def
) == REGNO (in_a
))
571 /* No definitions of IN_A; continue searching. */
575 /* Bail if this is not a totally normal set of IN_A. */
576 if (DF_REF_IS_ARTIFICIAL (def
))
578 if (DF_REF_FLAGS (def
) & abnormal_flags
)
581 /* We've found an insn between the compare and the clobber that sets
582 IN_A. Given that pass_cprop_hardreg has not yet run, we still find
583 situations in which we can usefully look through a copy insn. */
584 x
= single_set (insn
);
592 /* We've reached PREV_CLOBBER without finding a modification of IN_A.
593 Validate that PREV_CLOBBER itself does in fact refer to IN_A. Do
594 recall that we've already validated the shape of PREV_CLOBBER. */
595 x
= XVECEXP (PATTERN (insn
), 0, 0);
596 if (rtx_equal_p (SET_DEST (x
), in_a
))
597 cmp_src
= SET_SRC (x
);
599 /* Also check operations with implicit extensions, e.g.:
601 (zero_extend:DI (plus:SI (reg:SI)(reg:SI))))
604 (plus:SI (reg:SI)(reg:SI))
606 else if (REG_P (SET_DEST (x
))
608 && REGNO (SET_DEST (x
)) == REGNO (in_a
)
609 && (GET_CODE (SET_SRC (x
)) == ZERO_EXTEND
610 || GET_CODE (SET_SRC (x
)) == SIGN_EXTEND
)
611 && GET_MODE (XEXP (SET_SRC (x
), 0)) == GET_MODE (in_a
))
612 cmp_src
= XEXP (SET_SRC (x
), 0);
616 /* Determine if we ought to use a different CC_MODE here. */
617 flags
= maybe_select_cc_mode (cmp
, cmp_src
, cmp
->in_b
);
619 flags
= gen_rtx_REG (cmp
->orig_mode
, targetm
.flags_regnum
);
621 /* Generate a new comparison for installation in the setter. */
622 x
= copy_rtx (cmp_src
);
623 x
= gen_rtx_COMPARE (GET_MODE (flags
), x
, cmp
->in_b
);
624 x
= gen_rtx_SET (flags
, x
);
626 /* Succeed if the new instruction is valid. Note that we may have started
627 a change group within maybe_select_cc_mode, therefore we must continue. */
628 validate_change (insn
, &XVECEXP (PATTERN (insn
), 0, 1), x
, true);
629 if (!apply_change_group ())
632 /* Success. Delete the compare insn... */
633 delete_insn (cmp
->insn
);
635 /* ... and any notes that are now invalid due to multiple sets. */
636 x
= find_regno_note (insn
, REG_UNUSED
, targetm
.flags_regnum
);
638 remove_note (insn
, x
);
639 x
= find_reg_note (insn
, REG_EQUAL
, NULL
);
641 remove_note (insn
, x
);
642 x
= find_reg_note (insn
, REG_EQUIV
, NULL
);
644 remove_note (insn
, x
);
649 /* Main entry point to the pass. */
652 execute_compare_elim_after_reload (void)
656 gcc_checking_assert (!all_compares
.exists ());
658 /* Locate all comparisons and their uses, and eliminate duplicates. */
660 if (all_compares
.exists ())
662 struct comparison
*cmp
;
665 /* Eliminate comparisons that are redundant with flags computation. */
666 FOR_EACH_VEC_ELT (all_compares
, i
, cmp
)
668 try_eliminate_compare (cmp
);
672 all_compares
.release ();
680 const pass_data pass_data_compare_elim_after_reload
=
683 "cmpelim", /* name */
684 OPTGROUP_NONE
, /* optinfo_flags */
686 0, /* properties_required */
687 0, /* properties_provided */
688 0, /* properties_destroyed */
689 0, /* todo_flags_start */
690 ( TODO_df_finish
| TODO_df_verify
), /* todo_flags_finish */
693 class pass_compare_elim_after_reload
: public rtl_opt_pass
696 pass_compare_elim_after_reload (gcc::context
*ctxt
)
697 : rtl_opt_pass (pass_data_compare_elim_after_reload
, ctxt
)
700 /* opt_pass methods: */
701 virtual bool gate (function
*)
703 /* Setting this target hook value is how a backend indicates the need. */
704 if (targetm
.flags_regnum
== INVALID_REGNUM
)
706 return flag_compare_elim_after_reload
;
709 virtual unsigned int execute (function
*)
711 return execute_compare_elim_after_reload ();
714 }; // class pass_compare_elim_after_reload
719 make_pass_compare_elim_after_reload (gcc::context
*ctxt
)
721 return new pass_compare_elim_after_reload (ctxt
);