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