2015-10-18 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / compare-elim.c
blob08e070cc7f17a7db9ea90c1ea729221bffedfbb0
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
9 version.
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
14 for more details.
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
30 of the flags.
32 This pass assumes:
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)
43 (clobber (reg:CC))]
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.
57 #include "config.h"
58 #include "system.h"
59 #include "coretypes.h"
60 #include "backend.h"
61 #include "rtl.h"
62 #include "df.h"
63 #include "tm_p.h"
64 #include "insn-config.h"
65 #include "recog.h"
66 #include "flags.h"
67 #include "cfgrtl.h"
68 #include "tree-pass.h"
69 #include "target.h"
70 #include "domwalk.h"
73 /* These structures describe a comparison and how it is used. */
75 /* The choice of maximum 3 uses comes from wanting to eliminate the two
76 duplicate compares from a three-way branch on the sign of a value.
77 This is also sufficient to eliminate the duplicate compare against the
78 high-part of a double-word comparison. */
79 #define MAX_CMP_USE 3
81 struct comparison_use
83 /* The instruction in which the result of the compare is used. */
84 rtx_insn *insn;
85 /* The location of the flags register within the use. */
86 rtx *loc;
87 /* The comparison code applied against the flags register. */
88 enum rtx_code code;
91 struct comparison
93 /* The comparison instruction. */
94 rtx_insn *insn;
96 /* The insn prior to the comparison insn that clobbers the flags. */
97 rtx_insn *prev_clobber;
99 /* The two values being compared. These will be either REGs or
100 constants. */
101 rtx in_a, in_b;
103 /* The REG_EH_REGION of the comparison. */
104 rtx eh_note;
106 /* Information about how this comparison is used. */
107 struct comparison_use uses[MAX_CMP_USE];
109 /* The original CC_MODE for this comparison. */
110 machine_mode orig_mode;
112 /* The number of uses identified for this comparison. */
113 unsigned short n_uses;
115 /* True if not all uses of this comparison have been identified.
116 This can happen either for overflowing the array above, or if
117 the flags register is used in some unusual context. */
118 bool missing_uses;
120 /* True if its inputs are still valid at the end of the block. */
121 bool inputs_valid;
124 static vec<comparison *> all_compares;
126 /* Look for a "conforming" comparison, as defined above. If valid, return
127 the rtx for the COMPARE itself. */
129 static rtx
130 conforming_compare (rtx_insn *insn)
132 rtx set, src, dest;
134 set = single_set (insn);
135 if (set == NULL)
136 return NULL;
138 src = SET_SRC (set);
139 if (GET_CODE (src) != COMPARE)
140 return NULL;
142 dest = SET_DEST (set);
143 if (!REG_P (dest) || REGNO (dest) != targetm.flags_regnum)
144 return NULL;
146 if (REG_P (XEXP (src, 0))
147 && (REG_P (XEXP (src, 1)) || CONSTANT_P (XEXP (src, 1))))
148 return src;
150 return NULL;
153 /* Look for a pattern of the "correct" form for an insn with a flags clobber
154 for which we may be able to eliminate a compare later. We're not looking
155 to validate any inputs at this time, merely see that the basic shape is
156 correct. The term "arithmetic" may be somewhat misleading... */
158 static bool
159 arithmetic_flags_clobber_p (rtx_insn *insn)
161 rtx pat, x;
163 if (!NONJUMP_INSN_P (insn))
164 return false;
165 pat = PATTERN (insn);
166 if (extract_asm_operands (pat))
167 return false;
169 if (GET_CODE (pat) == PARALLEL && XVECLEN (pat, 0) == 2)
171 x = XVECEXP (pat, 0, 0);
172 if (GET_CODE (x) != SET)
173 return false;
174 x = SET_DEST (x);
175 if (!REG_P (x))
176 return false;
178 x = XVECEXP (pat, 0, 1);
179 if (GET_CODE (x) == CLOBBER)
181 x = XEXP (x, 0);
182 if (REG_P (x) && REGNO (x) == targetm.flags_regnum)
183 return true;
187 return false;
190 /* Look for uses of FLAGS in INSN. If we find one we can analyze, record
191 it in CMP; otherwise indicate that we've missed a use. */
193 static void
194 find_flags_uses_in_insn (struct comparison *cmp, rtx_insn *insn)
196 df_ref use;
198 /* If we've already lost track of uses, don't bother collecting more. */
199 if (cmp->missing_uses)
200 return;
202 /* Find a USE of the flags register. */
203 FOR_EACH_INSN_USE (use, insn)
204 if (DF_REF_REGNO (use) == targetm.flags_regnum)
206 rtx x, *loc;
208 /* If this is an unusual use, quit. */
209 if (DF_REF_TYPE (use) != DF_REF_REG_USE)
210 goto fail;
212 /* If we've run out of slots to record uses, quit. */
213 if (cmp->n_uses == MAX_CMP_USE)
214 goto fail;
216 /* Unfortunately the location of the flags register, while present
217 in the reference structure, doesn't help. We need to find the
218 comparison code that is outer to the actual flags use. */
219 loc = DF_REF_LOC (use);
220 x = PATTERN (insn);
221 if (GET_CODE (x) == PARALLEL)
222 x = XVECEXP (x, 0, 0);
223 x = SET_SRC (x);
224 if (GET_CODE (x) == IF_THEN_ELSE)
225 x = XEXP (x, 0);
226 if (COMPARISON_P (x)
227 && loc == &XEXP (x, 0)
228 && XEXP (x, 1) == const0_rtx)
230 /* We've found a use of the flags that we understand. */
231 struct comparison_use *cuse = &cmp->uses[cmp->n_uses++];
232 cuse->insn = insn;
233 cuse->loc = loc;
234 cuse->code = GET_CODE (x);
236 else
237 goto fail;
239 return;
241 fail:
242 /* We failed to recognize this use of the flags register. */
243 cmp->missing_uses = true;
246 class find_comparison_dom_walker : public dom_walker
248 public:
249 find_comparison_dom_walker (cdi_direction direction)
250 : dom_walker (direction) {}
252 virtual void before_dom_children (basic_block);
255 /* Return true if conforming COMPARE with EH_NOTE is redundant with comparison
256 CMP and can thus be eliminated. */
258 static bool
259 can_eliminate_compare (rtx compare, rtx eh_note, struct comparison *cmp)
261 /* Take care that it's in the same EH region. */
262 if (cfun->can_throw_non_call_exceptions
263 && !rtx_equal_p (eh_note, cmp->eh_note))
264 return false;
266 /* Make sure the compare is redundant with the previous. */
267 if (!rtx_equal_p (XEXP (compare, 0), cmp->in_a)
268 || !rtx_equal_p (XEXP (compare, 1), cmp->in_b))
269 return false;
271 /* New mode must be compatible with the previous compare mode. */
272 enum machine_mode new_mode
273 = targetm.cc_modes_compatible (GET_MODE (compare), cmp->orig_mode);
275 if (new_mode == VOIDmode)
276 return false;
278 if (cmp->orig_mode != new_mode)
280 /* Generate new comparison for substitution. */
281 rtx flags = gen_rtx_REG (new_mode, targetm.flags_regnum);
282 rtx x = gen_rtx_COMPARE (new_mode, cmp->in_a, cmp->in_b);
283 x = gen_rtx_SET (flags, x);
285 if (!validate_change (cmp->insn, &PATTERN (cmp->insn), x, false))
286 return false;
288 cmp->orig_mode = new_mode;
291 return true;
294 /* Identify comparison instructions within BB. If the flags from the last
295 compare in the BB is live at the end of the block, install the compare
296 in BB->AUX. Called via dom_walker.walk (). */
298 void
299 find_comparison_dom_walker::before_dom_children (basic_block bb)
301 struct comparison *last_cmp;
302 rtx_insn *insn, *next, *last_clobber;
303 bool last_cmp_valid;
304 bool need_purge = false;
305 bitmap killed;
307 killed = BITMAP_ALLOC (NULL);
309 /* The last comparison that was made. Will be reset to NULL
310 once the flags are clobbered. */
311 last_cmp = NULL;
313 /* True iff the last comparison has not been clobbered, nor
314 have its inputs. Used to eliminate duplicate compares. */
315 last_cmp_valid = false;
317 /* The last insn that clobbered the flags, if that insn is of
318 a form that may be valid for eliminating a following compare.
319 To be reset to NULL once the flags are set otherwise. */
320 last_clobber = NULL;
322 /* Propagate the last live comparison throughout the extended basic block. */
323 if (single_pred_p (bb))
325 last_cmp = (struct comparison *) single_pred (bb)->aux;
326 if (last_cmp)
327 last_cmp_valid = last_cmp->inputs_valid;
330 for (insn = BB_HEAD (bb); insn; insn = next)
332 rtx src;
334 next = (insn == BB_END (bb) ? NULL : NEXT_INSN (insn));
335 if (!NONDEBUG_INSN_P (insn))
336 continue;
338 /* Compute the set of registers modified by this instruction. */
339 bitmap_clear (killed);
340 df_simulate_find_defs (insn, killed);
342 src = conforming_compare (insn);
343 if (src)
345 rtx eh_note = NULL;
347 if (cfun->can_throw_non_call_exceptions)
348 eh_note = find_reg_note (insn, REG_EH_REGION, NULL);
350 if (last_cmp_valid && can_eliminate_compare (src, eh_note, last_cmp))
352 if (eh_note)
353 need_purge = true;
354 delete_insn (insn);
355 continue;
358 last_cmp = XCNEW (struct comparison);
359 last_cmp->insn = insn;
360 last_cmp->prev_clobber = last_clobber;
361 last_cmp->in_a = XEXP (src, 0);
362 last_cmp->in_b = XEXP (src, 1);
363 last_cmp->eh_note = eh_note;
364 last_cmp->orig_mode = GET_MODE (src);
365 all_compares.safe_push (last_cmp);
367 /* It's unusual, but be prepared for comparison patterns that
368 also clobber an input, or perhaps a scratch. */
369 last_clobber = NULL;
370 last_cmp_valid = true;
373 /* Notice if this instruction kills the flags register. */
374 else if (bitmap_bit_p (killed, targetm.flags_regnum))
376 /* See if this insn could be the "clobber" that eliminates
377 a future comparison. */
378 last_clobber = (arithmetic_flags_clobber_p (insn) ? insn : NULL);
380 /* In either case, the previous compare is no longer valid. */
381 last_cmp = NULL;
382 last_cmp_valid = false;
385 /* Notice if this instruction uses the flags register. */
386 else if (last_cmp)
387 find_flags_uses_in_insn (last_cmp, insn);
389 /* Notice if any of the inputs to the comparison have changed. */
390 if (last_cmp_valid
391 && (bitmap_bit_p (killed, REGNO (last_cmp->in_a))
392 || (REG_P (last_cmp->in_b)
393 && bitmap_bit_p (killed, REGNO (last_cmp->in_b)))))
394 last_cmp_valid = false;
397 BITMAP_FREE (killed);
399 /* Remember the live comparison for subsequent members of
400 the extended basic block. */
401 if (last_cmp)
403 bb->aux = last_cmp;
404 last_cmp->inputs_valid = last_cmp_valid;
406 /* Look to see if the flags register is live outgoing here, and
407 incoming to any successor not part of the extended basic block. */
408 if (bitmap_bit_p (df_get_live_out (bb), targetm.flags_regnum))
410 edge e;
411 edge_iterator ei;
413 FOR_EACH_EDGE (e, ei, bb->succs)
415 basic_block dest = e->dest;
416 if (bitmap_bit_p (df_get_live_in (bb), targetm.flags_regnum)
417 && !single_pred_p (dest))
419 last_cmp->missing_uses = true;
420 break;
426 /* If we deleted a compare with a REG_EH_REGION note, we may need to
427 remove EH edges. */
428 if (need_purge)
429 purge_dead_edges (bb);
432 /* Find all comparisons in the function. */
434 static void
435 find_comparisons (void)
437 calculate_dominance_info (CDI_DOMINATORS);
439 find_comparison_dom_walker (CDI_DOMINATORS)
440 .walk (cfun->cfg->x_entry_block_ptr);
442 clear_aux_for_blocks ();
443 free_dominance_info (CDI_DOMINATORS);
446 /* Select an alternate CC_MODE for a comparison insn comparing A and B.
447 Note that inputs are almost certainly different than the IN_A and IN_B
448 stored in CMP -- we're called while attempting to eliminate the compare
449 after all. Return the new FLAGS rtx if successful, else return NULL.
450 Note that this function may start a change group. */
452 static rtx
453 maybe_select_cc_mode (struct comparison *cmp, rtx a ATTRIBUTE_UNUSED,
454 rtx b ATTRIBUTE_UNUSED)
456 machine_mode sel_mode;
457 const int n = cmp->n_uses;
458 rtx flags = NULL;
460 #ifndef SELECT_CC_MODE
461 /* Minimize code differences when this target macro is undefined. */
462 return NULL;
463 #define SELECT_CC_MODE(A,B,C) (gcc_unreachable (), VOIDmode)
464 #endif
466 /* If we don't have access to all of the uses, we can't validate. */
467 if (cmp->missing_uses || n == 0)
468 return NULL;
470 /* Find a new mode that works for all of the uses. Special case the
471 common case of exactly one use. */
472 if (n == 1)
474 sel_mode = SELECT_CC_MODE (cmp->uses[0].code, a, b);
475 if (sel_mode != cmp->orig_mode)
477 flags = gen_rtx_REG (sel_mode, targetm.flags_regnum);
478 validate_change (cmp->uses[0].insn, cmp->uses[0].loc, flags, true);
481 else
483 int i;
485 sel_mode = SELECT_CC_MODE (cmp->uses[0].code, a, b);
486 for (i = 1; i < n; ++i)
488 machine_mode new_mode = SELECT_CC_MODE (cmp->uses[i].code, a, b);
489 if (new_mode != sel_mode)
491 sel_mode = targetm.cc_modes_compatible (sel_mode, new_mode);
492 if (sel_mode == VOIDmode)
493 return NULL;
497 if (sel_mode != cmp->orig_mode)
499 flags = gen_rtx_REG (sel_mode, targetm.flags_regnum);
500 for (i = 0; i < n; ++i)
501 validate_change (cmp->uses[i].insn, cmp->uses[i].loc, flags, true);
505 return flags;
508 /* Attempt to replace a comparison with a prior arithmetic insn that can
509 compute the same flags value as the comparison itself. Return true if
510 successful, having made all rtl modifications necessary. */
512 static bool
513 try_eliminate_compare (struct comparison *cmp)
515 rtx_insn *insn, *bb_head;
516 rtx x, flags, in_a, cmp_src;
518 /* We must have found an interesting "clobber" preceding the compare. */
519 if (cmp->prev_clobber == NULL)
520 return false;
522 /* ??? For the moment we don't handle comparisons for which IN_B
523 is a register. We accepted these during initial comparison
524 recognition in order to eliminate duplicate compares.
525 An improvement here would be to handle x = a - b; if (a cmp b). */
526 if (!CONSTANT_P (cmp->in_b))
527 return false;
529 /* Verify that IN_A is not clobbered in between CMP and PREV_CLOBBER.
530 Given that this target requires this pass, we can assume that most
531 insns do clobber the flags, and so the distance between the compare
532 and the clobber is likely to be small. */
533 /* ??? This is one point at which one could argue that DF_REF_CHAIN would
534 be useful, but it is thought to be too heavy-weight a solution here. */
536 in_a = cmp->in_a;
537 insn = cmp->insn;
538 bb_head = BB_HEAD (BLOCK_FOR_INSN (insn));
539 for (insn = PREV_INSN (insn);
540 insn != cmp->prev_clobber;
541 insn = PREV_INSN (insn))
543 const int abnormal_flags
544 = (DF_REF_CONDITIONAL | DF_REF_PARTIAL | DF_REF_MAY_CLOBBER
545 | DF_REF_MUST_CLOBBER | DF_REF_SIGN_EXTRACT
546 | DF_REF_ZERO_EXTRACT | DF_REF_STRICT_LOW_PART
547 | DF_REF_PRE_POST_MODIFY);
548 df_ref def;
550 /* Note that the BB_HEAD is always either a note or a label, but in
551 any case it means that IN_A is defined outside the block. */
552 if (insn == bb_head)
553 return false;
554 if (NOTE_P (insn) || DEBUG_INSN_P (insn))
555 continue;
557 /* Find a possible def of IN_A in INSN. */
558 FOR_EACH_INSN_DEF (def, insn)
559 if (DF_REF_REGNO (def) == REGNO (in_a))
560 break;
562 /* No definitions of IN_A; continue searching. */
563 if (def == NULL)
564 continue;
566 /* Bail if this is not a totally normal set of IN_A. */
567 if (DF_REF_IS_ARTIFICIAL (def))
568 return false;
569 if (DF_REF_FLAGS (def) & abnormal_flags)
570 return false;
572 /* We've found an insn between the compare and the clobber that sets
573 IN_A. Given that pass_cprop_hardreg has not yet run, we still find
574 situations in which we can usefully look through a copy insn. */
575 x = single_set (insn);
576 if (x == NULL)
577 return false;
578 in_a = SET_SRC (x);
579 if (!REG_P (in_a))
580 return false;
583 /* We've reached PREV_CLOBBER without finding a modification of IN_A.
584 Validate that PREV_CLOBBER itself does in fact refer to IN_A. Do
585 recall that we've already validated the shape of PREV_CLOBBER. */
586 x = XVECEXP (PATTERN (insn), 0, 0);
587 if (rtx_equal_p (SET_DEST (x), in_a))
588 cmp_src = SET_SRC (x);
590 /* Also check operations with implicit extensions, e.g.:
591 [(set (reg:DI)
592 (zero_extend:DI (plus:SI (reg:SI)(reg:SI))))
593 (set (reg:CCZ flags)
594 (compare:CCZ
595 (plus:SI (reg:SI)(reg:SI))
596 (const_int 0)))] */
597 else if (REG_P (SET_DEST (x))
598 && REG_P (in_a)
599 && REGNO (SET_DEST (x)) == REGNO (in_a)
600 && (GET_CODE (SET_SRC (x)) == ZERO_EXTEND
601 || GET_CODE (SET_SRC (x)) == SIGN_EXTEND)
602 && GET_MODE (XEXP (SET_SRC (x), 0)) == GET_MODE (in_a))
603 cmp_src = XEXP (SET_SRC (x), 0);
604 else
605 return false;
607 /* Determine if we ought to use a different CC_MODE here. */
608 flags = maybe_select_cc_mode (cmp, cmp_src, cmp->in_b);
609 if (flags == NULL)
610 flags = gen_rtx_REG (cmp->orig_mode, targetm.flags_regnum);
612 /* Generate a new comparison for installation in the setter. */
613 x = copy_rtx (cmp_src);
614 x = gen_rtx_COMPARE (GET_MODE (flags), x, cmp->in_b);
615 x = gen_rtx_SET (flags, x);
617 /* Succeed if the new instruction is valid. Note that we may have started
618 a change group within maybe_select_cc_mode, therefore we must continue. */
619 validate_change (insn, &XVECEXP (PATTERN (insn), 0, 1), x, true);
620 if (!apply_change_group ())
621 return false;
623 /* Success. Delete the compare insn... */
624 delete_insn (cmp->insn);
626 /* ... and any notes that are now invalid due to multiple sets. */
627 x = find_regno_note (insn, REG_UNUSED, targetm.flags_regnum);
628 if (x)
629 remove_note (insn, x);
630 x = find_reg_note (insn, REG_EQUAL, NULL);
631 if (x)
632 remove_note (insn, x);
633 x = find_reg_note (insn, REG_EQUIV, NULL);
634 if (x)
635 remove_note (insn, x);
637 return true;
640 /* Main entry point to the pass. */
642 static unsigned int
643 execute_compare_elim_after_reload (void)
645 df_analyze ();
647 gcc_checking_assert (!all_compares.exists ());
649 /* Locate all comparisons and their uses, and eliminate duplicates. */
650 find_comparisons ();
651 if (all_compares.exists ())
653 struct comparison *cmp;
654 size_t i;
656 /* Eliminate comparisons that are redundant with flags computation. */
657 FOR_EACH_VEC_ELT (all_compares, i, cmp)
659 try_eliminate_compare (cmp);
660 XDELETE (cmp);
663 all_compares.release ();
666 return 0;
669 namespace {
671 const pass_data pass_data_compare_elim_after_reload =
673 RTL_PASS, /* type */
674 "cmpelim", /* name */
675 OPTGROUP_NONE, /* optinfo_flags */
676 TV_NONE, /* tv_id */
677 0, /* properties_required */
678 0, /* properties_provided */
679 0, /* properties_destroyed */
680 0, /* todo_flags_start */
681 ( TODO_df_finish | TODO_df_verify ), /* todo_flags_finish */
684 class pass_compare_elim_after_reload : public rtl_opt_pass
686 public:
687 pass_compare_elim_after_reload (gcc::context *ctxt)
688 : rtl_opt_pass (pass_data_compare_elim_after_reload, ctxt)
691 /* opt_pass methods: */
692 virtual bool gate (function *)
694 /* Setting this target hook value is how a backend indicates the need. */
695 if (targetm.flags_regnum == INVALID_REGNUM)
696 return false;
697 return flag_compare_elim_after_reload;
700 virtual unsigned int execute (function *)
702 return execute_compare_elim_after_reload ();
705 }; // class pass_compare_elim_after_reload
707 } // anon namespace
709 rtl_opt_pass *
710 make_pass_compare_elim_after_reload (gcc::context *ctxt)
712 return new pass_compare_elim_after_reload (ctxt);