Mercurial > hg > CbC > GCC_original
diff gcc/compare-elim.c @ 16:04ced10e8804
gcc 7
author | kono |
---|---|
date | Fri, 27 Oct 2017 22:46:09 +0900 |
parents | 561a7518be6b |
children | 84e7813d76e9 |
line wrap: on
line diff
--- a/gcc/compare-elim.c Sun Aug 21 07:07:55 2011 +0900 +++ b/gcc/compare-elim.c Fri Oct 27 22:46:09 2017 +0900 @@ -1,6 +1,5 @@ /* Post-reload compare elimination. - Copyright (C) 2010, 2011 - Free Software Foundation, Inc. + Copyright (C) 2010-2017 Free Software Foundation, Inc. This file is part of GCC. @@ -36,7 +35,7 @@ (1) All comparison patterns are represented as - [(set (reg:CC) (compare:CC (reg) (immediate)))] + [(set (reg:CC) (compare:CC (reg) (reg_or_immediate)))] (2) All insn patterns that modify the flags are represented as @@ -46,9 +45,9 @@ (3) If an insn of form (2) can usefully set the flags, there is another pattern of the form - [(set (reg) (operation) - (set (reg:CCM) (compare:CCM (operation) (immediate)))] - + [(set (reg:CCM) (compare:CCM (operation) (immediate))) + (set (reg) (operation)] + The mode CCM will be chosen as if by SELECT_CC_MODE. Note that unlike NOTICE_UPDATE_CC, we do not handle memory operands. @@ -58,16 +57,17 @@ #include "config.h" #include "system.h" #include "coretypes.h" -#include "tm.h" +#include "backend.h" +#include "target.h" #include "rtl.h" +#include "df.h" +#include "memmodel.h" #include "tm_p.h" #include "insn-config.h" #include "recog.h" -#include "flags.h" -#include "basic-block.h" +#include "emit-rtl.h" +#include "cfgrtl.h" #include "tree-pass.h" -#include "target.h" -#include "df.h" #include "domwalk.h" @@ -82,7 +82,7 @@ struct comparison_use { /* The instruction in which the result of the compare is used. */ - rtx insn; + rtx_insn *insn; /* The location of the flags register within the use. */ rtx *loc; /* The comparison code applied against the flags register. */ @@ -92,20 +92,23 @@ struct comparison { /* The comparison instruction. */ - rtx insn; + rtx_insn *insn; /* The insn prior to the comparison insn that clobbers the flags. */ - rtx prev_clobber; + rtx_insn *prev_clobber; /* The two values being compared. These will be either REGs or constants. */ rtx in_a, in_b; + /* The REG_EH_REGION of the comparison. */ + rtx eh_note; + /* Information about how this comparison is used. */ struct comparison_use uses[MAX_CMP_USE]; /* The original CC_MODE for this comparison. */ - enum machine_mode orig_mode; + machine_mode orig_mode; /* The number of uses identified for this comparison. */ unsigned short n_uses; @@ -119,17 +122,13 @@ bool inputs_valid; }; -typedef struct comparison *comparison_struct_p; -DEF_VEC_P(comparison_struct_p); -DEF_VEC_ALLOC_P(comparison_struct_p, heap); - -static VEC(comparison_struct_p, heap) *all_compares; +static vec<comparison *> all_compares; /* Look for a "conforming" comparison, as defined above. If valid, return the rtx for the COMPARE itself. */ static rtx -conforming_compare (rtx insn) +conforming_compare (rtx_insn *insn) { rtx set, src, dest; @@ -145,11 +144,20 @@ if (!REG_P (dest) || REGNO (dest) != targetm.flags_regnum) return NULL; - if (REG_P (XEXP (src, 0)) - && REG_P (XEXP (src, 0)) - && (REG_P (XEXP (src, 1)) || CONSTANT_P (XEXP (src, 1)))) + if (!REG_P (XEXP (src, 0))) + return NULL; + + if (CONSTANT_P (XEXP (src, 1)) || REG_P (XEXP (src, 1))) return src; + if (GET_CODE (XEXP (src, 1)) == UNSPEC) + { + for (int i = 0; i < XVECLEN (XEXP (src, 1), 0); i++) + if (!REG_P (XVECEXP (XEXP (src, 1), 0, i))) + return NULL; + return src; + } + return NULL; } @@ -159,14 +167,14 @@ correct. The term "arithmetic" may be somewhat misleading... */ static bool -arithmetic_flags_clobber_p (rtx insn) +arithmetic_flags_clobber_p (rtx_insn *insn) { rtx pat, x; if (!NONJUMP_INSN_P (insn)) return false; pat = PATTERN (insn); - if (extract_asm_operands (pat)) + if (asm_noperands (pat) >= 0) return false; if (GET_CODE (pat) == PARALLEL && XVECLEN (pat, 0) == 2) @@ -194,16 +202,16 @@ it in CMP; otherwise indicate that we've missed a use. */ static void -find_flags_uses_in_insn (struct comparison *cmp, rtx insn) +find_flags_uses_in_insn (struct comparison *cmp, rtx_insn *insn) { - df_ref *use_rec, use; + df_ref use; /* If we've already lost track of uses, don't bother collecting more. */ if (cmp->missing_uses) return; /* Find a USE of the flags register. */ - for (use_rec = DF_INSN_USES (insn); (use = *use_rec) != NULL; use_rec++) + FOR_EACH_INSN_USE (use, insn) if (DF_REF_REGNO (use) == targetm.flags_regnum) { rtx x, *loc; @@ -246,17 +254,65 @@ cmp->missing_uses = true; } +class find_comparison_dom_walker : public dom_walker +{ +public: + find_comparison_dom_walker (cdi_direction direction) + : dom_walker (direction) {} + + virtual edge before_dom_children (basic_block); +}; + +/* Return true if conforming COMPARE with EH_NOTE is redundant with comparison + CMP and can thus be eliminated. */ + +static bool +can_eliminate_compare (rtx compare, rtx eh_note, struct comparison *cmp) +{ + /* Take care that it's in the same EH region. */ + if (cfun->can_throw_non_call_exceptions + && !rtx_equal_p (eh_note, cmp->eh_note)) + return false; + + /* Make sure the compare is redundant with the previous. */ + if (!rtx_equal_p (XEXP (compare, 0), cmp->in_a) + || !rtx_equal_p (XEXP (compare, 1), cmp->in_b)) + return false; + + /* New mode must be compatible with the previous compare mode. */ + machine_mode new_mode + = targetm.cc_modes_compatible (GET_MODE (compare), cmp->orig_mode); + + if (new_mode == VOIDmode) + return false; + + if (cmp->orig_mode != new_mode) + { + /* Generate new comparison for substitution. */ + rtx flags = gen_rtx_REG (new_mode, targetm.flags_regnum); + rtx x = gen_rtx_COMPARE (new_mode, cmp->in_a, cmp->in_b); + x = gen_rtx_SET (flags, x); + + if (!validate_change (cmp->insn, &PATTERN (cmp->insn), x, false)) + return false; + + cmp->orig_mode = new_mode; + } + + return true; +} + /* Identify comparison instructions within BB. If the flags from the last compare in the BB is live at the end of the block, install the compare - in BB->AUX. Called via walk_dominators_tree. */ + in BB->AUX. Called via dom_walker.walk (). */ -static void -find_comparisons_in_bb (struct dom_walk_data *data ATTRIBUTE_UNUSED, - basic_block bb) +edge +find_comparison_dom_walker::before_dom_children (basic_block bb) { struct comparison *last_cmp; - rtx insn, next, last_clobber; + rtx_insn *insn, *next, *last_clobber; bool last_cmp_valid; + bool need_purge = false; bitmap killed; killed = BITMAP_ALLOC (NULL); @@ -286,7 +342,7 @@ { rtx src; - next = (insn == BB_END (bb) ? NULL_RTX : NEXT_INSN (insn)); + next = (insn == BB_END (bb) ? NULL : NEXT_INSN (insn)); if (!NONDEBUG_INSN_P (insn)) continue; @@ -297,22 +353,27 @@ src = conforming_compare (insn); if (src) { - /* Eliminate a compare that's redundant with the previous. */ - if (last_cmp_valid - && rtx_equal_p (last_cmp->in_a, XEXP (src, 0)) - && rtx_equal_p (last_cmp->in_b, XEXP (src, 1))) + rtx eh_note = NULL; + + if (cfun->can_throw_non_call_exceptions) + eh_note = find_reg_note (insn, REG_EH_REGION, NULL); + + if (last_cmp_valid && can_eliminate_compare (src, eh_note, last_cmp)) { + if (eh_note) + need_purge = true; delete_insn (insn); continue; } - last_cmp = XCNEW (struct comparison); + last_cmp = XCNEW (struct comparison); last_cmp->insn = insn; last_cmp->prev_clobber = last_clobber; last_cmp->in_a = XEXP (src, 0); last_cmp->in_b = XEXP (src, 1); - last_cmp->orig_mode = GET_MODE (SET_DEST (single_set (insn))); - VEC_safe_push (comparison_struct_p, heap, all_compares, last_cmp); + last_cmp->eh_note = eh_note; + last_cmp->orig_mode = GET_MODE (src); + all_compares.safe_push (last_cmp); /* It's unusual, but be prepared for comparison patterns that also clobber an input, or perhaps a scratch. */ @@ -320,23 +381,25 @@ last_cmp_valid = true; } - /* Notice if this instruction kills the flags register. */ - else if (bitmap_bit_p (killed, targetm.flags_regnum)) + else { - /* See if this insn could be the "clobber" that eliminates - a future comparison. */ - last_clobber = (arithmetic_flags_clobber_p (insn) ? insn : NULL); + /* Notice if this instruction uses the flags register. */ + if (last_cmp) + find_flags_uses_in_insn (last_cmp, insn); - /* In either case, the previous compare is no longer valid. */ - last_cmp = NULL; - last_cmp_valid = false; - continue; + /* Notice if this instruction kills the flags register. */ + if (bitmap_bit_p (killed, targetm.flags_regnum)) + { + /* See if this insn could be the "clobber" that eliminates + a future comparison. */ + last_clobber = (arithmetic_flags_clobber_p (insn) ? insn : NULL); + + /* In either case, the previous compare is no longer valid. */ + last_cmp = NULL; + last_cmp_valid = false; + } } - /* Notice if this instruction uses the flags register. */ - else if (last_cmp) - find_flags_uses_in_insn (last_cmp, insn); - /* Notice if any of the inputs to the comparison have changed. */ if (last_cmp_valid && (bitmap_bit_p (killed, REGNO (last_cmp->in_a)) @@ -356,7 +419,7 @@ /* Look to see if the flags register is live outgoing here, and incoming to any successor not part of the extended basic block. */ - if (bitmap_bit_p (&DF_LIVE_BB_INFO (bb)->out, targetm.flags_regnum)) + if (bitmap_bit_p (df_get_live_out (bb), targetm.flags_regnum)) { edge e; edge_iterator ei; @@ -364,8 +427,7 @@ FOR_EACH_EDGE (e, ei, bb->succs) { basic_block dest = e->dest; - if (bitmap_bit_p (&DF_LIVE_BB_INFO (dest)->in, - targetm.flags_regnum) + if (bitmap_bit_p (df_get_live_in (bb), targetm.flags_regnum) && !single_pred_p (dest)) { last_cmp->missing_uses = true; @@ -374,6 +436,13 @@ } } } + + /* If we deleted a compare with a REG_EH_REGION note, we may need to + remove EH edges. */ + if (need_purge) + purge_dead_edges (bb); + + return NULL; } /* Find all comparisons in the function. */ @@ -381,17 +450,10 @@ static void find_comparisons (void) { - struct dom_walk_data data; - - memset (&data, 0, sizeof(data)); - data.dom_direction = CDI_DOMINATORS; - data.before_dom_children = find_comparisons_in_bb; - calculate_dominance_info (CDI_DOMINATORS); - init_walk_dominator_tree (&data); - walk_dominator_tree (&data, ENTRY_BLOCK_PTR); - fini_walk_dominator_tree (&data); + find_comparison_dom_walker (CDI_DOMINATORS) + .walk (cfun->cfg->x_entry_block_ptr); clear_aux_for_blocks (); free_dominance_info (CDI_DOMINATORS); @@ -407,7 +469,7 @@ maybe_select_cc_mode (struct comparison *cmp, rtx a ATTRIBUTE_UNUSED, rtx b ATTRIBUTE_UNUSED) { - enum machine_mode sel_mode; + machine_mode sel_mode; const int n = cmp->n_uses; rtx flags = NULL; @@ -439,8 +501,7 @@ sel_mode = SELECT_CC_MODE (cmp->uses[0].code, a, b); for (i = 1; i < n; ++i) { - enum machine_mode new_mode; - new_mode = SELECT_CC_MODE (cmp->uses[i].code, a, b); + machine_mode new_mode = SELECT_CC_MODE (cmp->uses[i].code, a, b); if (new_mode != sel_mode) { sel_mode = targetm.cc_modes_compatible (sel_mode, new_mode); @@ -448,7 +509,7 @@ return NULL; } } - + if (sel_mode != cmp->orig_mode) { flags = gen_rtx_REG (sel_mode, targetm.flags_regnum); @@ -460,6 +521,202 @@ return flags; } +/* Return a register RTX holding the same value at START as REG at END, or + NULL_RTX if there is none. */ + +static rtx +equivalent_reg_at_start (rtx reg, rtx_insn *end, rtx_insn *start) +{ + machine_mode orig_mode = GET_MODE (reg); + rtx_insn *bb_head = BB_HEAD (BLOCK_FOR_INSN (end)); + + for (rtx_insn *insn = PREV_INSN (end); + insn != start; + insn = PREV_INSN (insn)) + { + const int abnormal_flags + = (DF_REF_CONDITIONAL | DF_REF_PARTIAL | DF_REF_MAY_CLOBBER + | DF_REF_MUST_CLOBBER | DF_REF_SIGN_EXTRACT + | DF_REF_ZERO_EXTRACT | DF_REF_STRICT_LOW_PART + | DF_REF_PRE_POST_MODIFY); + df_ref def; + + /* Note that the BB_HEAD is always either a note or a label, but in + any case it means that REG is defined outside the block. */ + if (insn == bb_head) + return NULL_RTX; + if (NOTE_P (insn) || DEBUG_INSN_P (insn)) + continue; + + /* Find a possible def of REG in INSN. */ + FOR_EACH_INSN_DEF (def, insn) + if (DF_REF_REGNO (def) == REGNO (reg)) + break; + + /* No definitions of REG; continue searching. */ + if (def == NULL) + continue; + + /* Bail if this is not a totally normal set of REG. */ + if (DF_REF_IS_ARTIFICIAL (def)) + return NULL_RTX; + if (DF_REF_FLAGS (def) & abnormal_flags) + return NULL_RTX; + + /* We've found an insn between the compare and the clobber that sets + REG. Given that pass_cprop_hardreg has not yet run, we still find + situations in which we can usefully look through a copy insn. */ + rtx x = single_set (insn); + if (x == NULL_RTX) + return NULL_RTX; + reg = SET_SRC (x); + if (!REG_P (reg)) + return NULL_RTX; + } + + if (GET_MODE (reg) != orig_mode) + return NULL_RTX; + + return reg; +} + +/* Return true if it is okay to merge the comparison CMP_INSN with + the instruction ARITH_INSN. Both instructions are assumed to be in the + same basic block with ARITH_INSN appearing before CMP_INSN. This checks + that there are no uses or defs of the condition flags or control flow + changes between the two instructions. */ + +static bool +can_merge_compare_into_arith (rtx_insn *cmp_insn, rtx_insn *arith_insn) +{ + for (rtx_insn *insn = PREV_INSN (cmp_insn); + insn && insn != arith_insn; + insn = PREV_INSN (insn)) + { + if (!NONDEBUG_INSN_P (insn)) + continue; + /* Bail if there are jumps or calls in between. */ + if (!NONJUMP_INSN_P (insn)) + return false; + + /* Bail on old-style asm statements because they lack + data flow information. */ + if (GET_CODE (PATTERN (insn)) == ASM_INPUT) + return false; + + df_ref ref; + /* Find a USE of the flags register. */ + FOR_EACH_INSN_USE (ref, insn) + if (DF_REF_REGNO (ref) == targetm.flags_regnum) + return false; + + /* Find a DEF of the flags register. */ + FOR_EACH_INSN_DEF (ref, insn) + if (DF_REF_REGNO (ref) == targetm.flags_regnum) + return false; + } + return true; +} + +/* Given two SET expressions, SET_A and SET_B determine whether they form + a recognizable pattern when emitted in parallel. Return that parallel + if so. Otherwise return NULL. */ + +static rtx +try_validate_parallel (rtx set_a, rtx set_b) +{ + rtx par + = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set_a, set_b)); + + rtx_insn *insn; + insn = gen_rtx_INSN (VOIDmode, 0, 0, 0, par, 0, -1, 0); + + return recog_memoized (insn) > 0 ? par : NULL_RTX; +} + +/* For a comparison instruction described by CMP check if it compares a + register with zero i.e. it is of the form CC := CMP R1, 0. + If it is, find the instruction defining R1 (say I1) and try to create a + PARALLEL consisting of I1 and the comparison, representing a flag-setting + arithmetic instruction. Example: + I1: R1 := R2 + R3 + <instructions that don't read the condition register> + I2: CC := CMP R1 0 + I2 can be merged with I1 into: + I1: { R1 := R2 + R3 ; CC := CMP (R2 + R3) 0 } + This catches cases where R1 is used between I1 and I2 and therefore + combine and other RTL optimisations will not try to propagate it into + I2. Return true if we succeeded in merging CMP. */ + +static bool +try_merge_compare (struct comparison *cmp) +{ + rtx_insn *cmp_insn = cmp->insn; + + if (!REG_P (cmp->in_a) || cmp->in_b != const0_rtx) + return false; + rtx in_a = cmp->in_a; + df_ref use; + + FOR_EACH_INSN_USE (use, cmp_insn) + if (DF_REF_REGNO (use) == REGNO (in_a)) + break; + if (!use) + return false; + + /* Validate the data flow information before attempting to + find the instruction that defines in_a. */ + + struct df_link *ref_chain; + ref_chain = DF_REF_CHAIN (use); + if (!ref_chain || !ref_chain->ref + || !DF_REF_INSN_INFO (ref_chain->ref) || ref_chain->next != NULL) + return false; + + rtx_insn *def_insn = DF_REF_INSN (ref_chain->ref); + /* We found the insn that defines in_a. Only consider the cases where + it is in the same block as the comparison. */ + if (BLOCK_FOR_INSN (cmp_insn) != BLOCK_FOR_INSN (def_insn)) + return false; + + rtx set = single_set (def_insn); + if (!set) + return false; + + if (!can_merge_compare_into_arith (cmp_insn, def_insn)) + return false; + + rtx src = SET_SRC (set); + rtx flags = maybe_select_cc_mode (cmp, src, CONST0_RTX (GET_MODE (src))); + if (!flags) + { + /* We may already have a change group going through maybe_select_cc_mode. + Discard it properly. */ + cancel_changes (0); + return false; + } + + rtx flag_set + = gen_rtx_SET (flags, gen_rtx_COMPARE (GET_MODE (flags), + copy_rtx (src), + CONST0_RTX (GET_MODE (src)))); + rtx arith_set = copy_rtx (PATTERN (def_insn)); + rtx par = try_validate_parallel (flag_set, arith_set); + if (!par) + { + /* We may already have a change group going through maybe_select_cc_mode. + Discard it properly. */ + cancel_changes (0); + return false; + } + if (!apply_change_group ()) + return false; + emit_insn_after (par, def_insn); + delete_insn (def_insn); + delete_insn (cmp->insn); + return true; +} + /* Attempt to replace a comparison with a prior arithmetic insn that can compute the same flags value as the comparison itself. Return true if successful, having made all rtl modifications necessary. */ @@ -467,17 +724,13 @@ static bool try_eliminate_compare (struct comparison *cmp) { - rtx x, insn, bb_head, flags, in_a, cmp_src; - - /* We must have found an interesting "clobber" preceeding the compare. */ - if (cmp->prev_clobber == NULL) - return false; + rtx flags, in_a, in_b, cmp_src; - /* ??? For the moment we don't handle comparisons for which IN_B - is a register. We accepted these during initial comparison - recognition in order to eliminate duplicate compares. - An improvement here would be to handle x = a - b; if (a cmp b). */ - if (!CONSTANT_P (cmp->in_b)) + if (try_merge_compare (cmp)) + return true; + + /* We must have found an interesting "clobber" preceding the compare. */ + if (cmp->prev_clobber == NULL) return false; /* Verify that IN_A is not clobbered in between CMP and PREV_CLOBBER. @@ -486,78 +739,100 @@ and the clobber is likely to be small. */ /* ??? This is one point at which one could argue that DF_REF_CHAIN would be useful, but it is thought to be too heavy-weight a solution here. */ - - in_a = cmp->in_a; - insn = cmp->insn; - bb_head = BB_HEAD (BLOCK_FOR_INSN (insn)); - for (insn = PREV_INSN (insn); - insn != cmp->prev_clobber; - insn = PREV_INSN (insn)) - { - const int abnormal_flags - = (DF_REF_CONDITIONAL | DF_REF_PARTIAL | DF_REF_MAY_CLOBBER - | DF_REF_MUST_CLOBBER | DF_REF_SIGN_EXTRACT - | DF_REF_ZERO_EXTRACT | DF_REF_STRICT_LOW_PART - | DF_REF_PRE_POST_MODIFY); - df_ref *def_rec, def; - - /* Note that the BB_HEAD is always either a note or a label, but in - any case it means that IN_A is defined outside the block. */ - if (insn == bb_head) - return false; - if (NOTE_P (insn) || DEBUG_INSN_P (insn)) - continue; + in_a = equivalent_reg_at_start (cmp->in_a, cmp->insn, cmp->prev_clobber); + if (!in_a) + return false; - /* Find a possible def of IN_A in INSN. */ - for (def_rec = DF_INSN_DEFS (insn); (def = *def_rec) != NULL; def_rec++) - if (DF_REF_REGNO (def) == REGNO (in_a)) - break; - - /* No definitions of IN_A; continue searching. */ - if (def == NULL) - continue; - - /* Bail if this is not a totally normal set of IN_A. */ - if (DF_REF_IS_ARTIFICIAL (def)) - return false; - if (DF_REF_FLAGS (def) & abnormal_flags) - return false; - - /* We've found an insn between the compare and the clobber that sets - IN_A. Given that pass_cprop_hardreg has not yet run, we still find - situations in which we can usefully look through a copy insn. */ - x = single_set (insn); - if (x == NULL) - return false; - in_a = SET_SRC (x); - if (!REG_P (in_a)) + /* Likewise for IN_B if need be. */ + if (CONSTANT_P (cmp->in_b)) + in_b = cmp->in_b; + else if (REG_P (cmp->in_b)) + { + in_b = equivalent_reg_at_start (cmp->in_b, cmp->insn, cmp->prev_clobber); + if (!in_b) return false; } + else if (GET_CODE (cmp->in_b) == UNSPEC) + { + const int len = XVECLEN (cmp->in_b, 0); + rtvec v = rtvec_alloc (len); + for (int i = 0; i < len; i++) + { + rtx r = equivalent_reg_at_start (XVECEXP (cmp->in_b, 0, i), + cmp->insn, cmp->prev_clobber); + if (!r) + return false; + RTVEC_ELT (v, i) = r; + } + in_b = gen_rtx_UNSPEC (GET_MODE (cmp->in_b), v, XINT (cmp->in_b, 1)); + } + else + gcc_unreachable (); /* We've reached PREV_CLOBBER without finding a modification of IN_A. Validate that PREV_CLOBBER itself does in fact refer to IN_A. Do recall that we've already validated the shape of PREV_CLOBBER. */ - x = XVECEXP (PATTERN (insn), 0, 0); - if (!rtx_equal_p (SET_DEST (x), in_a)) + rtx_insn *insn = cmp->prev_clobber; + + rtx x = XVECEXP (PATTERN (insn), 0, 0); + if (rtx_equal_p (SET_DEST (x), in_a)) + cmp_src = SET_SRC (x); + + /* Also check operations with implicit extensions, e.g.: + [(set (reg:DI) + (zero_extend:DI (plus:SI (reg:SI) (reg:SI)))) + (set (reg:CCZ flags) + (compare:CCZ (plus:SI (reg:SI) (reg:SI)) + (const_int 0)))] */ + else if (REG_P (SET_DEST (x)) + && REG_P (in_a) + && REGNO (SET_DEST (x)) == REGNO (in_a) + && (GET_CODE (SET_SRC (x)) == ZERO_EXTEND + || GET_CODE (SET_SRC (x)) == SIGN_EXTEND) + && GET_MODE (XEXP (SET_SRC (x), 0)) == GET_MODE (in_a)) + cmp_src = XEXP (SET_SRC (x), 0); + + /* Also check fully redundant comparisons, e.g.: + [(set (reg:SI) + (minus:SI (reg:SI) (reg:SI)))) + (set (reg:CC flags) + (compare:CC (reg:SI) (reg:SI)))] */ + else if (REG_P (in_b) + && GET_CODE (SET_SRC (x)) == MINUS + && rtx_equal_p (XEXP (SET_SRC (x), 0), in_a) + && rtx_equal_p (XEXP (SET_SRC (x), 1), in_b)) + cmp_src = in_a; + + else return false; - cmp_src = SET_SRC (x); - + /* Determine if we ought to use a different CC_MODE here. */ - flags = maybe_select_cc_mode (cmp, cmp_src, cmp->in_b); + flags = maybe_select_cc_mode (cmp, cmp_src, in_b); if (flags == NULL) flags = gen_rtx_REG (cmp->orig_mode, targetm.flags_regnum); /* Generate a new comparison for installation in the setter. */ - x = copy_rtx (cmp_src); - x = gen_rtx_COMPARE (GET_MODE (flags), x, cmp->in_b); - x = gen_rtx_SET (VOIDmode, flags, x); + rtx y = copy_rtx (cmp_src); + y = gen_rtx_COMPARE (GET_MODE (flags), y, in_b); + y = gen_rtx_SET (flags, y); + /* Canonicalize instruction to: + [(set (reg:CCM) (compare:CCM (operation) (immediate))) + (set (reg) (operation)] */ + + rtvec v = rtvec_alloc (2); + RTVEC_ELT (v, 0) = y; + RTVEC_ELT (v, 1) = x; + + rtx pat = gen_rtx_PARALLEL (VOIDmode, v); + /* Succeed if the new instruction is valid. Note that we may have started a change group within maybe_select_cc_mode, therefore we must continue. */ - validate_change (insn, &XVECEXP (PATTERN (insn), 0, 1), x, true); + validate_change (insn, &PATTERN (insn), pat, true); + if (!apply_change_group ()) return false; - + /* Success. Delete the compare insn... */ delete_insn (cmp->insn); @@ -580,63 +855,73 @@ static unsigned int execute_compare_elim_after_reload (void) { - df_set_flags (DF_DEFER_INSN_RESCAN); - df_live_add_problem (); + df_chain_add_problem (DF_UD_CHAIN + DF_DU_CHAIN); df_analyze (); - gcc_checking_assert (all_compares == NULL); + gcc_checking_assert (!all_compares.exists ()); /* Locate all comparisons and their uses, and eliminate duplicates. */ find_comparisons (); - if (all_compares) + if (all_compares.exists ()) { struct comparison *cmp; size_t i; /* Eliminate comparisons that are redundant with flags computation. */ - FOR_EACH_VEC_ELT (comparison_struct_p, all_compares, i, cmp) + FOR_EACH_VEC_ELT (all_compares, i, cmp) { try_eliminate_compare (cmp); XDELETE (cmp); } - VEC_free (comparison_struct_p, heap, all_compares); - all_compares = NULL; - - df_analyze (); + all_compares.release (); } return 0; } -static bool -gate_compare_elim_after_reload (void) +namespace { + +const pass_data pass_data_compare_elim_after_reload = { - /* Setting this target hook value is how a backend indicates the need. */ - if (targetm.flags_regnum == INVALID_REGNUM) - return false; - return flag_compare_elim_after_reload; -} + RTL_PASS, /* type */ + "cmpelim", /* name */ + OPTGROUP_NONE, /* optinfo_flags */ + TV_NONE, /* tv_id */ + 0, /* properties_required */ + 0, /* properties_provided */ + 0, /* properties_destroyed */ + 0, /* todo_flags_start */ + ( TODO_df_finish | TODO_df_verify ), /* todo_flags_finish */ +}; -struct rtl_opt_pass pass_compare_elim_after_reload = +class pass_compare_elim_after_reload : public rtl_opt_pass { - { - RTL_PASS, - "cmpelim", /* name */ - gate_compare_elim_after_reload, /* gate */ - execute_compare_elim_after_reload, /* execute */ - NULL, /* sub */ - NULL, /* next */ - 0, /* static_pass_number */ - TV_NONE, /* tv_id */ - 0, /* properties_required */ - 0, /* properties_provided */ - 0, /* properties_destroyed */ - 0, /* todo_flags_start */ - TODO_df_finish - | TODO_df_verify - | TODO_verify_rtl_sharing - | TODO_dump_func - | TODO_ggc_collect /* todo_flags_finish */ - } -}; +public: + pass_compare_elim_after_reload (gcc::context *ctxt) + : rtl_opt_pass (pass_data_compare_elim_after_reload, ctxt) + {} + + /* opt_pass methods: */ + virtual bool gate (function *) + { + /* Setting this target hook value is how a backend indicates the need. */ + if (targetm.flags_regnum == INVALID_REGNUM) + return false; + return flag_compare_elim_after_reload; + } + + virtual unsigned int execute (function *) + { + return execute_compare_elim_after_reload (); + } + +}; // class pass_compare_elim_after_reload + +} // anon namespace + +rtl_opt_pass * +make_pass_compare_elim_after_reload (gcc::context *ctxt) +{ + return new pass_compare_elim_after_reload (ctxt); +}