]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/compare-elim.c
This patch rewrites the old VEC macro-based interface into a new one
[thirdparty/gcc.git] / gcc / compare-elim.c
CommitLineData
a50372fe 1/* Post-reload compare elimination.
2 Copyright (C) 2010, 2011
3 Free Software Foundation, Inc.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
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.
27
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
31 of the flags.
32
33 This pass assumes:
34
35 (0) CBRANCH/CSTORE etc have been split in pass_split_after_reload.
36
37 (1) All comparison patterns are represented as
38
39 [(set (reg:CC) (compare:CC (reg) (immediate)))]
40
41 (2) All insn patterns that modify the flags are represented as
42
43 [(set (reg) (operation)
44 (clobber (reg:CC))]
45
46 (3) If an insn of form (2) can usefully set the flags, there is
47 another pattern of the form
48
49 [(set (reg) (operation)
50 (set (reg:CCM) (compare:CCM (operation) (immediate)))]
51
52 The mode CCM will be chosen as if by SELECT_CC_MODE.
53
54 Note that unlike NOTICE_UPDATE_CC, we do not handle memory operands.
55 This could be handled as a future enhancement.
56*/
57
58#include "config.h"
59#include "system.h"
60#include "coretypes.h"
61#include "tm.h"
62#include "rtl.h"
63#include "tm_p.h"
64#include "insn-config.h"
65#include "recog.h"
66#include "flags.h"
67#include "basic-block.h"
68#include "tree-pass.h"
69#include "target.h"
70#include "df.h"
71#include "domwalk.h"
72
73\f
74/* These structures describe a comparison and how it is used. */
75
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. */
80#define MAX_CMP_USE 3
81
82struct comparison_use
83{
84 /* The instruction in which the result of the compare is used. */
85 rtx insn;
86 /* The location of the flags register within the use. */
87 rtx *loc;
88 /* The comparison code applied against the flags register. */
89 enum rtx_code code;
90};
91
92struct comparison
93{
94 /* The comparison instruction. */
95 rtx insn;
96
97 /* The insn prior to the comparison insn that clobbers the flags. */
98 rtx prev_clobber;
99
100 /* The two values being compared. These will be either REGs or
101 constants. */
102 rtx in_a, in_b;
103
104 /* Information about how this comparison is used. */
105 struct comparison_use uses[MAX_CMP_USE];
106
107 /* The original CC_MODE for this comparison. */
108 enum machine_mode orig_mode;
109
110 /* The number of uses identified for this comparison. */
111 unsigned short n_uses;
112
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. */
116 bool missing_uses;
117
118 /* True if its inputs are still valid at the end of the block. */
119 bool inputs_valid;
120};
121
122typedef struct comparison *comparison_struct_p;
a50372fe 123
f1f41a6c 124static vec<comparison_struct_p> all_compares;
a50372fe 125
126/* Look for a "conforming" comparison, as defined above. If valid, return
127 the rtx for the COMPARE itself. */
128
129static rtx
130conforming_compare (rtx insn)
131{
132 rtx set, src, dest;
133
134 set = single_set (insn);
135 if (set == NULL)
136 return NULL;
137
138 src = SET_SRC (set);
139 if (GET_CODE (src) != COMPARE)
140 return NULL;
141
142 dest = SET_DEST (set);
143 if (!REG_P (dest) || REGNO (dest) != targetm.flags_regnum)
144 return NULL;
145
146 if (REG_P (XEXP (src, 0))
147 && REG_P (XEXP (src, 0))
148 && (REG_P (XEXP (src, 1)) || CONSTANT_P (XEXP (src, 1))))
149 return src;
150
151 return NULL;
152}
153
154/* Look for a pattern of the "correct" form for an insn with a flags clobber
155 for which we may be able to eliminate a compare later. We're not looking
156 to validate any inputs at this time, merely see that the basic shape is
157 correct. The term "arithmetic" may be somewhat misleading... */
158
159static bool
160arithmetic_flags_clobber_p (rtx insn)
161{
162 rtx pat, x;
163
164 if (!NONJUMP_INSN_P (insn))
165 return false;
166 pat = PATTERN (insn);
167 if (extract_asm_operands (pat))
168 return false;
169
170 if (GET_CODE (pat) == PARALLEL && XVECLEN (pat, 0) == 2)
171 {
172 x = XVECEXP (pat, 0, 0);
173 if (GET_CODE (x) != SET)
174 return false;
175 x = SET_DEST (x);
176 if (!REG_P (x))
177 return false;
178
179 x = XVECEXP (pat, 0, 1);
180 if (GET_CODE (x) == CLOBBER)
181 {
182 x = XEXP (x, 0);
183 if (REG_P (x) && REGNO (x) == targetm.flags_regnum)
184 return true;
185 }
186 }
187
188 return false;
189}
190
191/* Look for uses of FLAGS in INSN. If we find one we can analyze, record
192 it in CMP; otherwise indicate that we've missed a use. */
193
194static void
195find_flags_uses_in_insn (struct comparison *cmp, rtx insn)
196{
197 df_ref *use_rec, use;
198
199 /* If we've already lost track of uses, don't bother collecting more. */
200 if (cmp->missing_uses)
201 return;
202
203 /* Find a USE of the flags register. */
204 for (use_rec = DF_INSN_USES (insn); (use = *use_rec) != NULL; use_rec++)
205 if (DF_REF_REGNO (use) == targetm.flags_regnum)
206 {
207 rtx x, *loc;
208
209 /* If this is an unusual use, quit. */
210 if (DF_REF_TYPE (use) != DF_REF_REG_USE)
211 goto fail;
212
213 /* If we've run out of slots to record uses, quit. */
214 if (cmp->n_uses == MAX_CMP_USE)
215 goto fail;
216
217 /* Unfortunately the location of the flags register, while present
218 in the reference structure, doesn't help. We need to find the
219 comparison code that is outer to the actual flags use. */
220 loc = DF_REF_LOC (use);
221 x = PATTERN (insn);
222 if (GET_CODE (x) == PARALLEL)
223 x = XVECEXP (x, 0, 0);
224 x = SET_SRC (x);
225 if (GET_CODE (x) == IF_THEN_ELSE)
226 x = XEXP (x, 0);
227 if (COMPARISON_P (x)
228 && loc == &XEXP (x, 0)
229 && XEXP (x, 1) == const0_rtx)
230 {
231 /* We've found a use of the flags that we understand. */
232 struct comparison_use *cuse = &cmp->uses[cmp->n_uses++];
233 cuse->insn = insn;
234 cuse->loc = loc;
235 cuse->code = GET_CODE (x);
236 }
237 else
238 goto fail;
239 }
240 return;
241
242 fail:
243 /* We failed to recognize this use of the flags register. */
244 cmp->missing_uses = true;
245}
246
247/* Identify comparison instructions within BB. If the flags from the last
248 compare in the BB is live at the end of the block, install the compare
249 in BB->AUX. Called via walk_dominators_tree. */
250
251static void
252find_comparisons_in_bb (struct dom_walk_data *data ATTRIBUTE_UNUSED,
253 basic_block bb)
254{
255 struct comparison *last_cmp;
256 rtx insn, next, last_clobber;
257 bool last_cmp_valid;
258 bitmap killed;
259
260 killed = BITMAP_ALLOC (NULL);
261
262 /* The last comparison that was made. Will be reset to NULL
263 once the flags are clobbered. */
264 last_cmp = NULL;
265
266 /* True iff the last comparison has not been clobbered, nor
267 have its inputs. Used to eliminate duplicate compares. */
268 last_cmp_valid = false;
269
270 /* The last insn that clobbered the flags, if that insn is of
271 a form that may be valid for eliminating a following compare.
272 To be reset to NULL once the flags are set otherwise. */
273 last_clobber = NULL;
274
275 /* Propagate the last live comparison throughout the extended basic block. */
276 if (single_pred_p (bb))
277 {
278 last_cmp = (struct comparison *) single_pred (bb)->aux;
279 if (last_cmp)
280 last_cmp_valid = last_cmp->inputs_valid;
281 }
282
283 for (insn = BB_HEAD (bb); insn; insn = next)
284 {
285 rtx src;
286
287 next = (insn == BB_END (bb) ? NULL_RTX : NEXT_INSN (insn));
288 if (!NONDEBUG_INSN_P (insn))
289 continue;
290
291 /* Compute the set of registers modified by this instruction. */
292 bitmap_clear (killed);
293 df_simulate_find_defs (insn, killed);
294
295 src = conforming_compare (insn);
296 if (src)
297 {
67755ff0 298 enum machine_mode src_mode = GET_MODE (src);
299
a50372fe 300 /* Eliminate a compare that's redundant with the previous. */
301 if (last_cmp_valid
302 && rtx_equal_p (last_cmp->in_a, XEXP (src, 0))
303 && rtx_equal_p (last_cmp->in_b, XEXP (src, 1)))
304 {
ea6ad4ae 305 rtx flags, x;
306 enum machine_mode new_mode
307 = targetm.cc_modes_compatible (last_cmp->orig_mode, src_mode);
308
309 /* New mode is incompatible with the previous compare mode. */
310 if (new_mode == VOIDmode)
311 continue;
312
313 if (new_mode != last_cmp->orig_mode)
314 {
315 flags = gen_rtx_REG (src_mode, targetm.flags_regnum);
316
317 /* Generate new comparison for substitution. */
318 x = gen_rtx_COMPARE (new_mode, XEXP (src, 0), XEXP (src, 1));
319 x = gen_rtx_SET (VOIDmode, flags, x);
320
321 if (!validate_change (last_cmp->insn,
322 &PATTERN (last_cmp->insn), x, false))
323 continue;
324
325 last_cmp->orig_mode = new_mode;
326 }
327
a50372fe 328 delete_insn (insn);
329 continue;
330 }
331
ea6ad4ae 332 last_cmp = XCNEW (struct comparison);
a50372fe 333 last_cmp->insn = insn;
334 last_cmp->prev_clobber = last_clobber;
335 last_cmp->in_a = XEXP (src, 0);
336 last_cmp->in_b = XEXP (src, 1);
67755ff0 337 last_cmp->orig_mode = src_mode;
f1f41a6c 338 all_compares.safe_push (last_cmp);
a50372fe 339
340 /* It's unusual, but be prepared for comparison patterns that
341 also clobber an input, or perhaps a scratch. */
342 last_clobber = NULL;
343 last_cmp_valid = true;
344 }
345
346 /* Notice if this instruction kills the flags register. */
347 else if (bitmap_bit_p (killed, targetm.flags_regnum))
348 {
349 /* See if this insn could be the "clobber" that eliminates
350 a future comparison. */
351 last_clobber = (arithmetic_flags_clobber_p (insn) ? insn : NULL);
352
353 /* In either case, the previous compare is no longer valid. */
354 last_cmp = NULL;
355 last_cmp_valid = false;
356 continue;
357 }
358
359 /* Notice if this instruction uses the flags register. */
360 else if (last_cmp)
361 find_flags_uses_in_insn (last_cmp, insn);
362
363 /* Notice if any of the inputs to the comparison have changed. */
364 if (last_cmp_valid
365 && (bitmap_bit_p (killed, REGNO (last_cmp->in_a))
366 || (REG_P (last_cmp->in_b)
367 && bitmap_bit_p (killed, REGNO (last_cmp->in_b)))))
368 last_cmp_valid = false;
369 }
370
371 BITMAP_FREE (killed);
372
373 /* Remember the live comparison for subsequent members of
374 the extended basic block. */
375 if (last_cmp)
376 {
377 bb->aux = last_cmp;
378 last_cmp->inputs_valid = last_cmp_valid;
379
380 /* Look to see if the flags register is live outgoing here, and
381 incoming to any successor not part of the extended basic block. */
7799dcb4 382 if (bitmap_bit_p (df_get_live_out (bb), targetm.flags_regnum))
a50372fe 383 {
384 edge e;
385 edge_iterator ei;
386
387 FOR_EACH_EDGE (e, ei, bb->succs)
388 {
389 basic_block dest = e->dest;
7799dcb4 390 if (bitmap_bit_p (df_get_live_in (bb),
a50372fe 391 targetm.flags_regnum)
392 && !single_pred_p (dest))
393 {
394 last_cmp->missing_uses = true;
395 break;
396 }
397 }
398 }
399 }
400}
401
402/* Find all comparisons in the function. */
403
404static void
405find_comparisons (void)
406{
407 struct dom_walk_data data;
408
409 memset (&data, 0, sizeof(data));
410 data.dom_direction = CDI_DOMINATORS;
411 data.before_dom_children = find_comparisons_in_bb;
412
413 calculate_dominance_info (CDI_DOMINATORS);
414
415 init_walk_dominator_tree (&data);
416 walk_dominator_tree (&data, ENTRY_BLOCK_PTR);
417 fini_walk_dominator_tree (&data);
418
419 clear_aux_for_blocks ();
420 free_dominance_info (CDI_DOMINATORS);
421}
422
423/* Select an alternate CC_MODE for a comparison insn comparing A and B.
424 Note that inputs are almost certainly different than the IN_A and IN_B
425 stored in CMP -- we're called while attempting to eliminate the compare
426 after all. Return the new FLAGS rtx if successful, else return NULL.
427 Note that this function may start a change group. */
428
429static rtx
97c6ec6a 430maybe_select_cc_mode (struct comparison *cmp, rtx a ATTRIBUTE_UNUSED,
431 rtx b ATTRIBUTE_UNUSED)
a50372fe 432{
433 enum machine_mode sel_mode;
434 const int n = cmp->n_uses;
435 rtx flags = NULL;
436
437#ifndef SELECT_CC_MODE
438 /* Minimize code differences when this target macro is undefined. */
439 return NULL;
440#define SELECT_CC_MODE(A,B,C) (gcc_unreachable (), VOIDmode)
441#endif
442
443 /* If we don't have access to all of the uses, we can't validate. */
444 if (cmp->missing_uses || n == 0)
445 return NULL;
446
447 /* Find a new mode that works for all of the uses. Special case the
448 common case of exactly one use. */
449 if (n == 1)
450 {
451 sel_mode = SELECT_CC_MODE (cmp->uses[0].code, a, b);
452 if (sel_mode != cmp->orig_mode)
453 {
454 flags = gen_rtx_REG (sel_mode, targetm.flags_regnum);
455 validate_change (cmp->uses[0].insn, cmp->uses[0].loc, flags, true);
456 }
457 }
458 else
459 {
460 int i;
461
462 sel_mode = SELECT_CC_MODE (cmp->uses[0].code, a, b);
463 for (i = 1; i < n; ++i)
464 {
465 enum machine_mode new_mode;
466 new_mode = SELECT_CC_MODE (cmp->uses[i].code, a, b);
467 if (new_mode != sel_mode)
468 {
469 sel_mode = targetm.cc_modes_compatible (sel_mode, new_mode);
470 if (sel_mode == VOIDmode)
471 return NULL;
472 }
473 }
474
475 if (sel_mode != cmp->orig_mode)
476 {
477 flags = gen_rtx_REG (sel_mode, targetm.flags_regnum);
478 for (i = 0; i < n; ++i)
479 validate_change (cmp->uses[i].insn, cmp->uses[i].loc, flags, true);
480 }
481 }
482
483 return flags;
484}
485
486/* Attempt to replace a comparison with a prior arithmetic insn that can
487 compute the same flags value as the comparison itself. Return true if
488 successful, having made all rtl modifications necessary. */
489
490static bool
491try_eliminate_compare (struct comparison *cmp)
492{
493 rtx x, insn, bb_head, flags, in_a, cmp_src;
494
9d75589a 495 /* We must have found an interesting "clobber" preceding the compare. */
a50372fe 496 if (cmp->prev_clobber == NULL)
497 return false;
498
499 /* ??? For the moment we don't handle comparisons for which IN_B
500 is a register. We accepted these during initial comparison
501 recognition in order to eliminate duplicate compares.
502 An improvement here would be to handle x = a - b; if (a cmp b). */
503 if (!CONSTANT_P (cmp->in_b))
504 return false;
505
506 /* Verify that IN_A is not clobbered in between CMP and PREV_CLOBBER.
507 Given that this target requires this pass, we can assume that most
508 insns do clobber the flags, and so the distance between the compare
509 and the clobber is likely to be small. */
510 /* ??? This is one point at which one could argue that DF_REF_CHAIN would
511 be useful, but it is thought to be too heavy-weight a solution here. */
512
513 in_a = cmp->in_a;
514 insn = cmp->insn;
515 bb_head = BB_HEAD (BLOCK_FOR_INSN (insn));
516 for (insn = PREV_INSN (insn);
517 insn != cmp->prev_clobber;
518 insn = PREV_INSN (insn))
519 {
520 const int abnormal_flags
521 = (DF_REF_CONDITIONAL | DF_REF_PARTIAL | DF_REF_MAY_CLOBBER
522 | DF_REF_MUST_CLOBBER | DF_REF_SIGN_EXTRACT
523 | DF_REF_ZERO_EXTRACT | DF_REF_STRICT_LOW_PART
524 | DF_REF_PRE_POST_MODIFY);
525 df_ref *def_rec, def;
526
527 /* Note that the BB_HEAD is always either a note or a label, but in
528 any case it means that IN_A is defined outside the block. */
529 if (insn == bb_head)
530 return false;
531 if (NOTE_P (insn) || DEBUG_INSN_P (insn))
532 continue;
533
534 /* Find a possible def of IN_A in INSN. */
535 for (def_rec = DF_INSN_DEFS (insn); (def = *def_rec) != NULL; def_rec++)
536 if (DF_REF_REGNO (def) == REGNO (in_a))
537 break;
538
539 /* No definitions of IN_A; continue searching. */
540 if (def == NULL)
541 continue;
542
543 /* Bail if this is not a totally normal set of IN_A. */
544 if (DF_REF_IS_ARTIFICIAL (def))
545 return false;
546 if (DF_REF_FLAGS (def) & abnormal_flags)
547 return false;
548
549 /* We've found an insn between the compare and the clobber that sets
550 IN_A. Given that pass_cprop_hardreg has not yet run, we still find
551 situations in which we can usefully look through a copy insn. */
552 x = single_set (insn);
553 if (x == NULL)
554 return false;
555 in_a = SET_SRC (x);
556 if (!REG_P (in_a))
557 return false;
558 }
559
560 /* We've reached PREV_CLOBBER without finding a modification of IN_A.
561 Validate that PREV_CLOBBER itself does in fact refer to IN_A. Do
562 recall that we've already validated the shape of PREV_CLOBBER. */
563 x = XVECEXP (PATTERN (insn), 0, 0);
56cc4397 564 if (rtx_equal_p (SET_DEST (x), in_a))
565 cmp_src = SET_SRC (x);
566
567 /* Also check operations with implicit extensions, e.g.:
568 [(set (reg:DI)
569 (zero_extend:DI (plus:SI (reg:SI)(reg:SI))))
570 (set (reg:CCZ flags)
571 (compare:CCZ
572 (plus:SI (reg:SI)(reg:SI))
573 (const_int 0)))] */
574 else if (REG_P (SET_DEST (x))
575 && REG_P (in_a)
576 && REGNO (SET_DEST (x)) == REGNO (in_a)
577 && (GET_CODE (SET_SRC (x)) == ZERO_EXTEND
578 || GET_CODE (SET_SRC (x)) == SIGN_EXTEND)
579 && GET_MODE (XEXP (SET_SRC (x), 0)) == GET_MODE (in_a))
580 cmp_src = XEXP (SET_SRC (x), 0);
581 else
a50372fe 582 return false;
56cc4397 583
a50372fe 584 /* Determine if we ought to use a different CC_MODE here. */
585 flags = maybe_select_cc_mode (cmp, cmp_src, cmp->in_b);
586 if (flags == NULL)
587 flags = gen_rtx_REG (cmp->orig_mode, targetm.flags_regnum);
588
589 /* Generate a new comparison for installation in the setter. */
590 x = copy_rtx (cmp_src);
591 x = gen_rtx_COMPARE (GET_MODE (flags), x, cmp->in_b);
592 x = gen_rtx_SET (VOIDmode, flags, x);
593
594 /* Succeed if the new instruction is valid. Note that we may have started
595 a change group within maybe_select_cc_mode, therefore we must continue. */
596 validate_change (insn, &XVECEXP (PATTERN (insn), 0, 1), x, true);
597 if (!apply_change_group ())
598 return false;
599
600 /* Success. Delete the compare insn... */
601 delete_insn (cmp->insn);
602
603 /* ... and any notes that are now invalid due to multiple sets. */
604 x = find_regno_note (insn, REG_UNUSED, targetm.flags_regnum);
605 if (x)
606 remove_note (insn, x);
607 x = find_reg_note (insn, REG_EQUAL, NULL);
608 if (x)
609 remove_note (insn, x);
610 x = find_reg_note (insn, REG_EQUIV, NULL);
611 if (x)
612 remove_note (insn, x);
613
614 return true;
615}
616
617/* Main entry point to the pass. */
618
619static unsigned int
620execute_compare_elim_after_reload (void)
621{
a50372fe 622 df_analyze ();
623
f1f41a6c 624 gcc_checking_assert (!all_compares.exists ());
a50372fe 625
626 /* Locate all comparisons and their uses, and eliminate duplicates. */
627 find_comparisons ();
f1f41a6c 628 if (all_compares.exists ())
a50372fe 629 {
630 struct comparison *cmp;
631 size_t i;
632
633 /* Eliminate comparisons that are redundant with flags computation. */
f1f41a6c 634 FOR_EACH_VEC_ELT (all_compares, i, cmp)
a50372fe 635 {
636 try_eliminate_compare (cmp);
637 XDELETE (cmp);
638 }
639
f1f41a6c 640 all_compares.release ();
a50372fe 641 }
642
643 return 0;
644}
645
646static bool
647gate_compare_elim_after_reload (void)
648{
649 /* Setting this target hook value is how a backend indicates the need. */
650 if (targetm.flags_regnum == INVALID_REGNUM)
651 return false;
652 return flag_compare_elim_after_reload;
653}
654
655struct rtl_opt_pass pass_compare_elim_after_reload =
656{
657 {
658 RTL_PASS,
659 "cmpelim", /* name */
c7875731 660 OPTGROUP_NONE, /* optinfo_flags */
a50372fe 661 gate_compare_elim_after_reload, /* gate */
662 execute_compare_elim_after_reload, /* execute */
663 NULL, /* sub */
664 NULL, /* next */
665 0, /* static_pass_number */
666 TV_NONE, /* tv_id */
667 0, /* properties_required */
668 0, /* properties_provided */
669 0, /* properties_destroyed */
670 0, /* todo_flags_start */
671 TODO_df_finish
672 | TODO_df_verify
673 | TODO_verify_rtl_sharing
a50372fe 674 | TODO_ggc_collect /* todo_flags_finish */
675 }
676};