1 /* RTL dead code elimination. 2 Copyright (C) 2005-2017 Free Software Foundation, Inc. 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 #include "config.h" 21 #include "system.h" 22 #include "coretypes.h" 23 #include "backend.h" 24 #include "rtl.h" 25 #include "tree.h" 26 #include "predict.h" 27 #include "df.h" 28 #include "memmodel.h" 29 #include "tm_p.h" 30 #include "emit-rtl.h" /* FIXME: Can go away once crtl is moved to rtl.h. */ 31 #include "cfgrtl.h" 32 #include "cfgbuild.h" 33 #include "cfgcleanup.h" 34 #include "dce.h" 35 #include "valtrack.h" 36 #include "tree-pass.h" 37 #include "dbgcnt.h" 38 39 40 /* ------------------------------------------------------------------------- 41 Core mark/delete routines 42 ------------------------------------------------------------------------- */ 43 44 /* True if we are invoked while the df engine is running; in this case, 45 we don't want to reenter it. */ 46 static bool df_in_progress = false; 47 48 /* True if we are allowed to alter the CFG in this pass. */ 49 static bool can_alter_cfg = false; 50 51 /* Instructions that have been marked but whose dependencies have not 52 yet been processed. */ 53 static vec<rtx_insn *> worklist; 54 55 /* Bitmap of instructions marked as needed indexed by INSN_UID. */ 56 static sbitmap marked; 57 58 /* Bitmap obstacks used for block processing by the fast algorithm. */ 59 static bitmap_obstack dce_blocks_bitmap_obstack; 60 static bitmap_obstack dce_tmp_bitmap_obstack; 61 62 static bool find_call_stack_args (rtx_call_insn *, bool, bool, bitmap); 63 64 /* A subroutine for which BODY is part of the instruction being tested; 65 either the top-level pattern, or an element of a PARALLEL. The 66 instruction is known not to be a bare USE or CLOBBER. */ 67 68 static bool 69 deletable_insn_p_1 (rtx body) 70 { 71 switch (GET_CODE (body)) 72 { 73 case PREFETCH: 74 case TRAP_IF: 75 /* The UNSPEC case was added here because the ia-64 claims that 76 USEs do not work after reload and generates UNSPECS rather 77 than USEs. Since dce is run after reload we need to avoid 78 deleting these even if they are dead. If it turns out that 79 USEs really do work after reload, the ia-64 should be 80 changed, and the UNSPEC case can be removed. */ 81 case UNSPEC: 82 return false; 83 84 default: 85 return !volatile_refs_p (body); 86 } 87 } 88 89 /* Don't delete calls that may throw if we cannot do so. */ 90 91 static bool 92 can_delete_call (rtx_insn *insn) 93 { 94 if (cfun->can_delete_dead_exceptions && can_alter_cfg) 95 return true; 96 if (!insn_nothrow_p (insn)) 97 return false; 98 if (can_alter_cfg) 99 return true; 100 /* If we can't alter cfg, even when the call can't throw exceptions, it 101 might have EDGE_ABNORMAL_CALL edges and so we shouldn't delete such 102 calls. */ 103 gcc_assert (CALL_P (insn)); 104 if (BLOCK_FOR_INSN (insn) && BB_END (BLOCK_FOR_INSN (insn)) == insn) 105 { 106 edge e; 107 edge_iterator ei; 108 109 FOR_EACH_EDGE (e, ei, BLOCK_FOR_INSN (insn)->succs) 110 if ((e->flags & EDGE_ABNORMAL_CALL) != 0) 111 return false; 112 } 113 return true; 114 } 115 116 /* Return true if INSN is a normal instruction that can be deleted by 117 the DCE pass. */ 118 119 static bool 120 deletable_insn_p (rtx_insn *insn, bool fast, bitmap arg_stores) 121 { 122 rtx body, x; 123 int i; 124 df_ref def; 125 126 if (CALL_P (insn) 127 /* We cannot delete calls inside of the recursive dce because 128 this may cause basic blocks to be deleted and this messes up 129 the rest of the stack of optimization passes. */ 130 && (!df_in_progress) 131 /* We cannot delete pure or const sibling calls because it is 132 hard to see the result. */ 133 && (!SIBLING_CALL_P (insn)) 134 /* We can delete dead const or pure calls as long as they do not 135 infinite loop. */ 136 && (RTL_CONST_OR_PURE_CALL_P (insn) 137 && !RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)) 138 /* Don't delete calls that may throw if we cannot do so. */ 139 && can_delete_call (insn)) 140 return find_call_stack_args (as_a <rtx_call_insn *> (insn), false, 141 fast, arg_stores); 142 143 /* Don't delete jumps, notes and the like. */ 144 if (!NONJUMP_INSN_P (insn)) 145 return false; 146 147 /* Don't delete insns that may throw if we cannot do so. */ 148 if (!(cfun->can_delete_dead_exceptions && can_alter_cfg) 149 && !insn_nothrow_p (insn)) 150 return false; 151 152 /* If INSN sets a global_reg, leave it untouched. */ 153 FOR_EACH_INSN_DEF (def, insn) 154 if (HARD_REGISTER_NUM_P (DF_REF_REGNO (def)) 155 && global_regs[DF_REF_REGNO (def)]) 156 return false; 157 /* Initialization of pseudo PIC register should never be removed. */ 158 else if (DF_REF_REG (def) == pic_offset_table_rtx 159 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER) 160 return false; 161 162 /* Callee-save restores are needed. */ 163 if (RTX_FRAME_RELATED_P (insn) 164 && crtl->shrink_wrapped_separate 165 && find_reg_note (insn, REG_CFA_RESTORE, NULL)) 166 return false; 167 168 body = PATTERN (insn); 169 switch (GET_CODE (body)) 170 { 171 case USE: 172 case VAR_LOCATION: 173 return false; 174 175 case CLOBBER: 176 if (fast) 177 { 178 /* A CLOBBER of a dead pseudo register serves no purpose. 179 That is not necessarily true for hard registers until 180 after reload. */ 181 x = XEXP (body, 0); 182 return REG_P (x) && (!HARD_REGISTER_P (x) || reload_completed); 183 } 184 else 185 /* Because of the way that use-def chains are built, it is not 186 possible to tell if the clobber is dead because it can 187 never be the target of a use-def chain. */ 188 return false; 189 190 case PARALLEL: 191 for (i = XVECLEN (body, 0) - 1; i >= 0; i--) 192 if (!deletable_insn_p_1 (XVECEXP (body, 0, i))) 193 return false; 194 return true; 195 196 default: 197 return deletable_insn_p_1 (body); 198 } 199 } 200 201 202 /* Return true if INSN has been marked as needed. */ 203 204 static inline int 205 marked_insn_p (rtx_insn *insn) 206 { 207 /* Artificial defs are always needed and they do not have an insn. 208 We should never see them here. */ 209 gcc_assert (insn); 210 return bitmap_bit_p (marked, INSN_UID (insn)); 211 } 212 213 214 /* If INSN has not yet been marked as needed, mark it now, and add it to 215 the worklist. */ 216 217 static void 218 mark_insn (rtx_insn *insn, bool fast) 219 { 220 if (!marked_insn_p (insn)) 221 { 222 if (!fast) 223 worklist.safe_push (insn); 224 bitmap_set_bit (marked, INSN_UID (insn)); 225 if (dump_file) 226 fprintf (dump_file, " Adding insn %d to worklist\n", INSN_UID (insn)); 227 if (CALL_P (insn) 228 && !df_in_progress 229 && !SIBLING_CALL_P (insn) 230 && (RTL_CONST_OR_PURE_CALL_P (insn) 231 && !RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)) 232 && can_delete_call (insn)) 233 find_call_stack_args (as_a <rtx_call_insn *> (insn), true, fast, NULL); 234 } 235 } 236 237 238 /* A note_stores callback used by mark_nonreg_stores. DATA is the 239 instruction containing DEST. */ 240 241 static void 242 mark_nonreg_stores_1 (rtx dest, const_rtx pattern, void *data) 243 { 244 if (GET_CODE (pattern) != CLOBBER && !REG_P (dest)) 245 mark_insn ((rtx_insn *) data, true); 246 } 247 248 249 /* A note_stores callback used by mark_nonreg_stores. DATA is the 250 instruction containing DEST. */ 251 252 static void 253 mark_nonreg_stores_2 (rtx dest, const_rtx pattern, void *data) 254 { 255 if (GET_CODE (pattern) != CLOBBER && !REG_P (dest)) 256 mark_insn ((rtx_insn *) data, false); 257 } 258 259 260 /* Mark INSN if BODY stores to a non-register destination. */ 261 262 static void 263 mark_nonreg_stores (rtx body, rtx_insn *insn, bool fast) 264 { 265 if (fast) 266 note_stores (body, mark_nonreg_stores_1, insn); 267 else 268 note_stores (body, mark_nonreg_stores_2, insn); 269 } 270 271 272 /* Return true if a store to SIZE bytes, starting OFF bytes from stack pointer, 273 is a call argument store, and clear corresponding bits from SP_BYTES 274 bitmap if it is. */ 275 276 static bool 277 check_argument_store (HOST_WIDE_INT size, HOST_WIDE_INT off, 278 HOST_WIDE_INT min_sp_off, HOST_WIDE_INT max_sp_off, 279 bitmap sp_bytes) 280 { 281 HOST_WIDE_INT byte; 282 for (byte = off; byte < off + size; byte++) 283 { 284 if (byte < min_sp_off 285 || byte >= max_sp_off 286 || !bitmap_clear_bit (sp_bytes, byte - min_sp_off)) 287 return false; 288 } 289 return true; 290 } 291 292 293 /* Try to find all stack stores of CALL_INSN arguments if 294 ACCUMULATE_OUTGOING_ARGS. If all stack stores have been found 295 and it is therefore safe to eliminate the call, return true, 296 otherwise return false. This function should be first called 297 with DO_MARK false, and only when the CALL_INSN is actually 298 going to be marked called again with DO_MARK true. */ 299 300 static bool 301 find_call_stack_args (rtx_call_insn *call_insn, bool do_mark, bool fast, 302 bitmap arg_stores) 303 { 304 rtx p; 305 rtx_insn *insn, *prev_insn; 306 bool ret; 307 HOST_WIDE_INT min_sp_off, max_sp_off; 308 bitmap sp_bytes; 309 310 gcc_assert (CALL_P (call_insn)); 311 if (!ACCUMULATE_OUTGOING_ARGS) 312 return true; 313 314 if (!do_mark) 315 { 316 gcc_assert (arg_stores); 317 bitmap_clear (arg_stores); 318 } 319 320 min_sp_off = INTTYPE_MAXIMUM (HOST_WIDE_INT); 321 max_sp_off = 0; 322 323 /* First determine the minimum and maximum offset from sp for 324 stored arguments. */ 325 for (p = CALL_INSN_FUNCTION_USAGE (call_insn); p; p = XEXP (p, 1)) 326 if (GET_CODE (XEXP (p, 0)) == USE 327 && MEM_P (XEXP (XEXP (p, 0), 0))) 328 { 329 rtx mem = XEXP (XEXP (p, 0), 0), addr; 330 HOST_WIDE_INT off = 0, size; 331 if (!MEM_SIZE_KNOWN_P (mem)) 332 return false; 333 size = MEM_SIZE (mem); 334 addr = XEXP (mem, 0); 335 if (GET_CODE (addr) == PLUS 336 && REG_P (XEXP (addr, 0)) 337 && CONST_INT_P (XEXP (addr, 1))) 338 { 339 off = INTVAL (XEXP (addr, 1)); 340 addr = XEXP (addr, 0); 341 } 342 if (addr != stack_pointer_rtx) 343 { 344 if (!REG_P (addr)) 345 return false; 346 /* If not fast, use chains to see if addr wasn't set to 347 sp + offset. */ 348 if (!fast) 349 { 350 df_ref use; 351 struct df_link *defs; 352 rtx set; 353 354 FOR_EACH_INSN_USE (use, call_insn) 355 if (rtx_equal_p (addr, DF_REF_REG (use))) 356 break; 357 358 if (use == NULL) 359 return false; 360 361 for (defs = DF_REF_CHAIN (use); defs; defs = defs->next) 362 if (! DF_REF_IS_ARTIFICIAL (defs->ref)) 363 break; 364 365 if (defs == NULL) 366 return false; 367 368 set = single_set (DF_REF_INSN (defs->ref)); 369 if (!set) 370 return false; 371 372 if (GET_CODE (SET_SRC (set)) != PLUS 373 || XEXP (SET_SRC (set), 0) != stack_pointer_rtx 374 || !CONST_INT_P (XEXP (SET_SRC (set), 1))) 375 return false; 376 377 off += INTVAL (XEXP (SET_SRC (set), 1)); 378 } 379 else 380 return false; 381 } 382 min_sp_off = MIN (min_sp_off, off); 383 max_sp_off = MAX (max_sp_off, off + size); 384 } 385 386 if (min_sp_off >= max_sp_off) 387 return true; 388 sp_bytes = BITMAP_ALLOC (NULL); 389 390 /* Set bits in SP_BYTES bitmap for bytes relative to sp + min_sp_off 391 which contain arguments. Checking has been done in the previous 392 loop. */ 393 for (p = CALL_INSN_FUNCTION_USAGE (call_insn); p; p = XEXP (p, 1)) 394 if (GET_CODE (XEXP (p, 0)) == USE 395 && MEM_P (XEXP (XEXP (p, 0), 0))) 396 { 397 rtx mem = XEXP (XEXP (p, 0), 0), addr; 398 HOST_WIDE_INT off = 0, byte; 399 addr = XEXP (mem, 0); 400 if (GET_CODE (addr) == PLUS 401 && REG_P (XEXP (addr, 0)) 402 && CONST_INT_P (XEXP (addr, 1))) 403 { 404 off = INTVAL (XEXP (addr, 1)); 405 addr = XEXP (addr, 0); 406 } 407 if (addr != stack_pointer_rtx) 408 { 409 df_ref use; 410 struct df_link *defs; 411 rtx set; 412 413 FOR_EACH_INSN_USE (use, call_insn) 414 if (rtx_equal_p (addr, DF_REF_REG (use))) 415 break; 416 417 for (defs = DF_REF_CHAIN (use); defs; defs = defs->next) 418 if (! DF_REF_IS_ARTIFICIAL (defs->ref)) 419 break; 420 421 set = single_set (DF_REF_INSN (defs->ref)); 422 off += INTVAL (XEXP (SET_SRC (set), 1)); 423 } 424 for (byte = off; byte < off + MEM_SIZE (mem); byte++) 425 { 426 if (!bitmap_set_bit (sp_bytes, byte - min_sp_off)) 427 gcc_unreachable (); 428 } 429 } 430 431 /* Walk backwards, looking for argument stores. The search stops 432 when seeing another call, sp adjustment or memory store other than 433 argument store. */ 434 ret = false; 435 for (insn = PREV_INSN (call_insn); insn; insn = prev_insn) 436 { 437 rtx set, mem, addr; 438 HOST_WIDE_INT off; 439 440 if (insn == BB_HEAD (BLOCK_FOR_INSN (call_insn))) 441 prev_insn = NULL; 442 else 443 prev_insn = PREV_INSN (insn); 444 445 if (CALL_P (insn)) 446 break; 447 448 if (!NONDEBUG_INSN_P (insn)) 449 continue; 450 451 set = single_set (insn); 452 if (!set || SET_DEST (set) == stack_pointer_rtx) 453 break; 454 455 if (!MEM_P (SET_DEST (set))) 456 continue; 457 458 mem = SET_DEST (set); 459 addr = XEXP (mem, 0); 460 off = 0; 461 if (GET_CODE (addr) == PLUS 462 && REG_P (XEXP (addr, 0)) 463 && CONST_INT_P (XEXP (addr, 1))) 464 { 465 off = INTVAL (XEXP (addr, 1)); 466 addr = XEXP (addr, 0); 467 } 468 if (addr != stack_pointer_rtx) 469 { 470 if (!REG_P (addr)) 471 break; 472 if (!fast) 473 { 474 df_ref use; 475 struct df_link *defs; 476 rtx set; 477 478 FOR_EACH_INSN_USE (use, insn) 479 if (rtx_equal_p (addr, DF_REF_REG (use))) 480 break; 481 482 if (use == NULL) 483 break; 484 485 for (defs = DF_REF_CHAIN (use); defs; defs = defs->next) 486 if (! DF_REF_IS_ARTIFICIAL (defs->ref)) 487 break; 488 489 if (defs == NULL) 490 break; 491 492 set = single_set (DF_REF_INSN (defs->ref)); 493 if (!set) 494 break; 495 496 if (GET_CODE (SET_SRC (set)) != PLUS 497 || XEXP (SET_SRC (set), 0) != stack_pointer_rtx 498 || !CONST_INT_P (XEXP (SET_SRC (set), 1))) 499 break; 500 501 off += INTVAL (XEXP (SET_SRC (set), 1)); 502 } 503 else 504 break; 505 } 506 507 if (!MEM_SIZE_KNOWN_P (mem) 508 || !check_argument_store (MEM_SIZE (mem), off, min_sp_off, 509 max_sp_off, sp_bytes)) 510 break; 511 512 if (!deletable_insn_p (insn, fast, NULL)) 513 break; 514 515 if (do_mark) 516 mark_insn (insn, fast); 517 else 518 bitmap_set_bit (arg_stores, INSN_UID (insn)); 519 520 if (bitmap_empty_p (sp_bytes)) 521 { 522 ret = true; 523 break; 524 } 525 } 526 527 BITMAP_FREE (sp_bytes); 528 if (!ret && arg_stores) 529 bitmap_clear (arg_stores); 530 531 return ret; 532 } 533 534 535 /* Remove all REG_EQUAL and REG_EQUIV notes referring to the registers INSN 536 writes to. */ 537 538 static void 539 remove_reg_equal_equiv_notes_for_defs (rtx_insn *insn) 540 { 541 df_ref def; 542 543 FOR_EACH_INSN_DEF (def, insn) 544 remove_reg_equal_equiv_notes_for_regno (DF_REF_REGNO (def)); 545 } 546 547 /* Scan all BBs for debug insns and reset those that reference values 548 defined in unmarked insns. */ 549 550 static void 551 reset_unmarked_insns_debug_uses (void) 552 { 553 basic_block bb; 554 rtx_insn *insn, *next; 555 556 FOR_EACH_BB_REVERSE_FN (bb, cfun) 557 FOR_BB_INSNS_REVERSE_SAFE (bb, insn, next) 558 if (DEBUG_INSN_P (insn)) 559 { 560 df_ref use; 561 562 FOR_EACH_INSN_USE (use, insn) 563 { 564 struct df_link *defs; 565 for (defs = DF_REF_CHAIN (use); defs; defs = defs->next) 566 { 567 rtx_insn *ref_insn; 568 if (DF_REF_IS_ARTIFICIAL (defs->ref)) 569 continue; 570 ref_insn = DF_REF_INSN (defs->ref); 571 if (!marked_insn_p (ref_insn)) 572 break; 573 } 574 if (!defs) 575 continue; 576 /* ??? FIXME could we propagate the values assigned to 577 each of the DEFs? */ 578 INSN_VAR_LOCATION_LOC (insn) = gen_rtx_UNKNOWN_VAR_LOC (); 579 df_insn_rescan_debug_internal (insn); 580 break; 581 } 582 } 583 } 584 585 /* Delete every instruction that hasn't been marked. */ 586 587 static void 588 delete_unmarked_insns (void) 589 { 590 basic_block bb; 591 rtx_insn *insn, *next; 592 bool must_clean = false; 593 594 FOR_EACH_BB_REVERSE_FN (bb, cfun) 595 FOR_BB_INSNS_REVERSE_SAFE (bb, insn, next) 596 if (NONDEBUG_INSN_P (insn)) 597 { 598 rtx turn_into_use = NULL_RTX; 599 600 /* Always delete no-op moves. */ 601 if (noop_move_p (insn) 602 /* Unless the no-op move can throw and we are not allowed 603 to alter cfg. */ 604 && (!cfun->can_throw_non_call_exceptions 605 || (cfun->can_delete_dead_exceptions && can_alter_cfg) 606 || insn_nothrow_p (insn))) 607 { 608 if (RTX_FRAME_RELATED_P (insn)) 609 turn_into_use 610 = find_reg_note (insn, REG_CFA_RESTORE, NULL); 611 if (turn_into_use && REG_P (XEXP (turn_into_use, 0))) 612 turn_into_use = XEXP (turn_into_use, 0); 613 else 614 turn_into_use = NULL_RTX; 615 } 616 617 /* Otherwise rely only on the DCE algorithm. */ 618 else if (marked_insn_p (insn)) 619 continue; 620 621 /* Beware that reaching a dbg counter limit here can result 622 in miscompiled file. This occurs when a group of insns 623 must be deleted together, typically because the kept insn 624 depends on the output from the deleted insn. Deleting 625 this insns in reverse order (both at the bb level and 626 when looking at the blocks) minimizes this, but does not 627 eliminate it, since it is possible for the using insn to 628 be top of a block and the producer to be at the bottom of 629 the block. However, in most cases this will only result 630 in an uninitialized use of an insn that is dead anyway. 631 632 However, there is one rare case that will cause a 633 miscompile: deletion of non-looping pure and constant 634 calls on a machine where ACCUMULATE_OUTGOING_ARGS is true. 635 In this case it is possible to remove the call, but leave 636 the argument pushes to the stack. Because of the changes 637 to the stack pointer, this will almost always lead to a 638 miscompile. */ 639 if (!dbg_cnt (dce)) 640 continue; 641 642 if (dump_file) 643 fprintf (dump_file, "DCE: Deleting insn %d\n", INSN_UID (insn)); 644 645 /* Before we delete the insn we have to remove the REG_EQUAL notes 646 for the destination regs in order to avoid dangling notes. */ 647 remove_reg_equal_equiv_notes_for_defs (insn); 648 649 if (turn_into_use) 650 { 651 /* Don't remove frame related noop moves if they cary 652 REG_CFA_RESTORE note, while we don't need to emit any code, 653 we need it to emit the CFI restore note. */ 654 PATTERN (insn) 655 = gen_rtx_USE (GET_MODE (turn_into_use), turn_into_use); 656 INSN_CODE (insn) = -1; 657 df_insn_rescan (insn); 658 } 659 else 660 /* Now delete the insn. */ 661 must_clean |= delete_insn_and_edges (insn); 662 } 663 664 /* Deleted a pure or const call. */ 665 if (must_clean) 666 { 667 delete_unreachable_blocks (); 668 free_dominance_info (CDI_DOMINATORS); 669 } 670 } 671 672 673 /* Go through the instructions and mark those whose necessity is not 674 dependent on inter-instruction information. Make sure all other 675 instructions are not marked. */ 676 677 static void 678 prescan_insns_for_dce (bool fast) 679 { 680 basic_block bb; 681 rtx_insn *insn, *prev; 682 bitmap arg_stores = NULL; 683 684 if (dump_file) 685 fprintf (dump_file, "Finding needed instructions:\n"); 686 687 if (!df_in_progress && ACCUMULATE_OUTGOING_ARGS) 688 arg_stores = BITMAP_ALLOC (NULL); 689 690 FOR_EACH_BB_FN (bb, cfun) 691 { 692 FOR_BB_INSNS_REVERSE_SAFE (bb, insn, prev) 693 if (NONDEBUG_INSN_P (insn)) 694 { 695 /* Don't mark argument stores now. They will be marked 696 if needed when the associated CALL is marked. */ 697 if (arg_stores && bitmap_bit_p (arg_stores, INSN_UID (insn))) 698 continue; 699 if (deletable_insn_p (insn, fast, arg_stores)) 700 mark_nonreg_stores (PATTERN (insn), insn, fast); 701 else 702 mark_insn (insn, fast); 703 } 704 /* find_call_stack_args only looks at argument stores in the 705 same bb. */ 706 if (arg_stores) 707 bitmap_clear (arg_stores); 708 } 709 710 if (arg_stores) 711 BITMAP_FREE (arg_stores); 712 713 if (dump_file) 714 fprintf (dump_file, "Finished finding needed instructions:\n"); 715 } 716 717 718 /* UD-based DSE routines. */ 719 720 /* Mark instructions that define artificially-used registers, such as 721 the frame pointer and the stack pointer. */ 722 723 static void 724 mark_artificial_uses (void) 725 { 726 basic_block bb; 727 struct df_link *defs; 728 df_ref use; 729 730 FOR_ALL_BB_FN (bb, cfun) 731 FOR_EACH_ARTIFICIAL_USE (use, bb->index) 732 for (defs = DF_REF_CHAIN (use); defs; defs = defs->next) 733 if (!DF_REF_IS_ARTIFICIAL (defs->ref)) 734 mark_insn (DF_REF_INSN (defs->ref), false); 735 } 736 737 738 /* Mark every instruction that defines a register value that INSN uses. */ 739 740 static void 741 mark_reg_dependencies (rtx_insn *insn) 742 { 743 struct df_link *defs; 744 df_ref use; 745 746 if (DEBUG_INSN_P (insn)) 747 return; 748 749 FOR_EACH_INSN_USE (use, insn) 750 { 751 if (dump_file) 752 { 753 fprintf (dump_file, "Processing use of "); 754 print_simple_rtl (dump_file, DF_REF_REG (use)); 755 fprintf (dump_file, " in insn %d:\n", INSN_UID (insn)); 756 } 757 for (defs = DF_REF_CHAIN (use); defs; defs = defs->next) 758 if (! DF_REF_IS_ARTIFICIAL (defs->ref)) 759 mark_insn (DF_REF_INSN (defs->ref), false); 760 } 761 } 762 763 764 /* Initialize global variables for a new DCE pass. */ 765 766 static void 767 init_dce (bool fast) 768 { 769 if (!df_in_progress) 770 { 771 if (!fast) 772 { 773 df_set_flags (DF_RD_PRUNE_DEAD_DEFS); 774 df_chain_add_problem (DF_UD_CHAIN); 775 } 776 df_analyze (); 777 } 778 779 if (dump_file) 780 df_dump (dump_file); 781 782 if (fast) 783 { 784 bitmap_obstack_initialize (&dce_blocks_bitmap_obstack); 785 bitmap_obstack_initialize (&dce_tmp_bitmap_obstack); 786 can_alter_cfg = false; 787 } 788 else 789 can_alter_cfg = true; 790 791 marked = sbitmap_alloc (get_max_uid () + 1); 792 bitmap_clear (marked); 793 } 794 795 796 /* Free the data allocated by init_dce. */ 797 798 static void 799 fini_dce (bool fast) 800 { 801 sbitmap_free (marked); 802 803 if (fast) 804 { 805 bitmap_obstack_release (&dce_blocks_bitmap_obstack); 806 bitmap_obstack_release (&dce_tmp_bitmap_obstack); 807 } 808 } 809 810 811 /* UD-chain based DCE. */ 812 813 static unsigned int 814 rest_of_handle_ud_dce (void) 815 { 816 rtx_insn *insn; 817 818 init_dce (false); 819 820 prescan_insns_for_dce (false); 821 mark_artificial_uses (); 822 while (worklist.length () > 0) 823 { 824 insn = worklist.pop (); 825 mark_reg_dependencies (insn); 826 } 827 worklist.release (); 828 829 if (MAY_HAVE_DEBUG_INSNS) 830 reset_unmarked_insns_debug_uses (); 831 832 /* Before any insns are deleted, we must remove the chains since 833 they are not bidirectional. */ 834 df_remove_problem (df_chain); 835 delete_unmarked_insns (); 836 837 fini_dce (false); 838 return 0; 839 } 840 841 842 namespace { 843 844 const pass_data pass_data_ud_rtl_dce = 845 { 846 RTL_PASS, /* type */ 847 "ud_dce", /* name */ 848 OPTGROUP_NONE, /* optinfo_flags */ 849 TV_DCE, /* tv_id */ 850 0, /* properties_required */ 851 0, /* properties_provided */ 852 0, /* properties_destroyed */ 853 0, /* todo_flags_start */ 854 TODO_df_finish, /* todo_flags_finish */ 855 }; 856 857 class pass_ud_rtl_dce : public rtl_opt_pass 858 { 859 public: 860 pass_ud_rtl_dce (gcc::context *ctxt) 861 : rtl_opt_pass (pass_data_ud_rtl_dce, ctxt) 862 {} 863 864 /* opt_pass methods: */ 865 virtual bool gate (function *) 866 { 867 return optimize > 1 && flag_dce && dbg_cnt (dce_ud); 868 } 869 870 virtual unsigned int execute (function *) 871 { 872 return rest_of_handle_ud_dce (); 873 } 874 875 }; // class pass_ud_rtl_dce 876 877 } // anon namespace 878 879 rtl_opt_pass * 880 make_pass_ud_rtl_dce (gcc::context *ctxt) 881 { 882 return new pass_ud_rtl_dce (ctxt); 883 } 884 885 886 /* ------------------------------------------------------------------------- 887 Fast DCE functions 888 ------------------------------------------------------------------------- */ 889 890 /* Process basic block BB. Return true if the live_in set has 891 changed. REDO_OUT is true if the info at the bottom of the block 892 needs to be recalculated before starting. AU is the proper set of 893 artificial uses. Track global substitution of uses of dead pseudos 894 in debug insns using GLOBAL_DEBUG. */ 895 896 static bool 897 word_dce_process_block (basic_block bb, bool redo_out, 898 struct dead_debug_global *global_debug) 899 { 900 bitmap local_live = BITMAP_ALLOC (&dce_tmp_bitmap_obstack); 901 rtx_insn *insn; 902 bool block_changed; 903 struct dead_debug_local debug; 904 905 if (redo_out) 906 { 907 /* Need to redo the live_out set of this block if when one of 908 the succs of this block has had a change in it live in 909 set. */ 910 edge e; 911 edge_iterator ei; 912 df_confluence_function_n con_fun_n = df_word_lr->problem->con_fun_n; 913 bitmap_clear (DF_WORD_LR_OUT (bb)); 914 FOR_EACH_EDGE (e, ei, bb->succs) 915 (*con_fun_n) (e); 916 } 917 918 if (dump_file) 919 { 920 fprintf (dump_file, "processing block %d live out = ", bb->index); 921 df_print_word_regset (dump_file, DF_WORD_LR_OUT (bb)); 922 } 923 924 bitmap_copy (local_live, DF_WORD_LR_OUT (bb)); 925 dead_debug_local_init (&debug, NULL, global_debug); 926 927 FOR_BB_INSNS_REVERSE (bb, insn) 928 if (DEBUG_INSN_P (insn)) 929 { 930 df_ref use; 931 FOR_EACH_INSN_USE (use, insn) 932 if (DF_REF_REGNO (use) >= FIRST_PSEUDO_REGISTER 933 && (GET_MODE_SIZE (GET_MODE (DF_REF_REAL_REG (use))) 934 == 2 * UNITS_PER_WORD) 935 && !bitmap_bit_p (local_live, 2 * DF_REF_REGNO (use)) 936 && !bitmap_bit_p (local_live, 2 * DF_REF_REGNO (use) + 1)) 937 dead_debug_add (&debug, use, DF_REF_REGNO (use)); 938 } 939 else if (INSN_P (insn)) 940 { 941 bool any_changed; 942 943 /* No matter if the instruction is needed or not, we remove 944 any regno in the defs from the live set. */ 945 any_changed = df_word_lr_simulate_defs (insn, local_live); 946 if (any_changed) 947 mark_insn (insn, true); 948 949 /* On the other hand, we do not allow the dead uses to set 950 anything in local_live. */ 951 if (marked_insn_p (insn)) 952 df_word_lr_simulate_uses (insn, local_live); 953 954 /* Insert debug temps for dead REGs used in subsequent debug 955 insns. We may have to emit a debug temp even if the insn 956 was marked, in case the debug use was after the point of 957 death. */ 958 if (debug.used && !bitmap_empty_p (debug.used)) 959 { 960 df_ref def; 961 962 FOR_EACH_INSN_DEF (def, insn) 963 dead_debug_insert_temp (&debug, DF_REF_REGNO (def), insn, 964 marked_insn_p (insn) 965 && !control_flow_insn_p (insn) 966 ? DEBUG_TEMP_AFTER_WITH_REG_FORCE 967 : DEBUG_TEMP_BEFORE_WITH_VALUE); 968 } 969 970 if (dump_file) 971 { 972 fprintf (dump_file, "finished processing insn %d live out = ", 973 INSN_UID (insn)); 974 df_print_word_regset (dump_file, local_live); 975 } 976 } 977 978 block_changed = !bitmap_equal_p (local_live, DF_WORD_LR_IN (bb)); 979 if (block_changed) 980 bitmap_copy (DF_WORD_LR_IN (bb), local_live); 981 982 dead_debug_local_finish (&debug, NULL); 983 BITMAP_FREE (local_live); 984 return block_changed; 985 } 986 987 988 /* Process basic block BB. Return true if the live_in set has 989 changed. REDO_OUT is true if the info at the bottom of the block 990 needs to be recalculated before starting. AU is the proper set of 991 artificial uses. Track global substitution of uses of dead pseudos 992 in debug insns using GLOBAL_DEBUG. */ 993 994 static bool 995 dce_process_block (basic_block bb, bool redo_out, bitmap au, 996 struct dead_debug_global *global_debug) 997 { 998 bitmap local_live = BITMAP_ALLOC (&dce_tmp_bitmap_obstack); 999 rtx_insn *insn; 1000 bool block_changed; 1001 df_ref def; 1002 struct dead_debug_local debug; 1003 1004 if (redo_out) 1005 { 1006 /* Need to redo the live_out set of this block if when one of 1007 the succs of this block has had a change in it live in 1008 set. */ 1009 edge e; 1010 edge_iterator ei; 1011 df_confluence_function_n con_fun_n = df_lr->problem->con_fun_n; 1012 bitmap_clear (DF_LR_OUT (bb)); 1013 FOR_EACH_EDGE (e, ei, bb->succs) 1014 (*con_fun_n) (e); 1015 } 1016 1017 if (dump_file) 1018 { 1019 fprintf (dump_file, "processing block %d lr out = ", bb->index); 1020 df_print_regset (dump_file, DF_LR_OUT (bb)); 1021 } 1022 1023 bitmap_copy (local_live, DF_LR_OUT (bb)); 1024 1025 df_simulate_initialize_backwards (bb, local_live); 1026 dead_debug_local_init (&debug, NULL, global_debug); 1027 1028 FOR_BB_INSNS_REVERSE (bb, insn) 1029 if (DEBUG_INSN_P (insn)) 1030 { 1031 df_ref use; 1032 FOR_EACH_INSN_USE (use, insn) 1033 if (!bitmap_bit_p (local_live, DF_REF_REGNO (use)) 1034 && !bitmap_bit_p (au, DF_REF_REGNO (use))) 1035 dead_debug_add (&debug, use, DF_REF_REGNO (use)); 1036 } 1037 else if (INSN_P (insn)) 1038 { 1039 bool needed = marked_insn_p (insn); 1040 1041 /* The insn is needed if there is someone who uses the output. */ 1042 if (!needed) 1043 FOR_EACH_INSN_DEF (def, insn) 1044 if (bitmap_bit_p (local_live, DF_REF_REGNO (def)) 1045 || bitmap_bit_p (au, DF_REF_REGNO (def))) 1046 { 1047 needed = true; 1048 mark_insn (insn, true); 1049 break; 1050 } 1051 1052 /* No matter if the instruction is needed or not, we remove 1053 any regno in the defs from the live set. */ 1054 df_simulate_defs (insn, local_live); 1055 1056 /* On the other hand, we do not allow the dead uses to set 1057 anything in local_live. */ 1058 if (needed) 1059 df_simulate_uses (insn, local_live); 1060 1061 /* Insert debug temps for dead REGs used in subsequent debug 1062 insns. We may have to emit a debug temp even if the insn 1063 was marked, in case the debug use was after the point of 1064 death. */ 1065 if (debug.used && !bitmap_empty_p (debug.used)) 1066 FOR_EACH_INSN_DEF (def, insn) 1067 dead_debug_insert_temp (&debug, DF_REF_REGNO (def), insn, 1068 needed && !control_flow_insn_p (insn) 1069 ? DEBUG_TEMP_AFTER_WITH_REG_FORCE 1070 : DEBUG_TEMP_BEFORE_WITH_VALUE); 1071 } 1072 1073 dead_debug_local_finish (&debug, NULL); 1074 df_simulate_finalize_backwards (bb, local_live); 1075 1076 block_changed = !bitmap_equal_p (local_live, DF_LR_IN (bb)); 1077 if (block_changed) 1078 bitmap_copy (DF_LR_IN (bb), local_live); 1079 1080 BITMAP_FREE (local_live); 1081 return block_changed; 1082 } 1083 1084 1085 /* Perform fast DCE once initialization is done. If WORD_LEVEL is 1086 true, use the word level dce, otherwise do it at the pseudo 1087 level. */ 1088 1089 static void 1090 fast_dce (bool word_level) 1091 { 1092 int *postorder = df_get_postorder (DF_BACKWARD); 1093 int n_blocks = df_get_n_blocks (DF_BACKWARD); 1094 /* The set of blocks that have been seen on this iteration. */ 1095 bitmap processed = BITMAP_ALLOC (&dce_blocks_bitmap_obstack); 1096 /* The set of blocks that need to have the out vectors reset because 1097 the in of one of their successors has changed. */ 1098 bitmap redo_out = BITMAP_ALLOC (&dce_blocks_bitmap_obstack); 1099 bitmap all_blocks = BITMAP_ALLOC (&dce_blocks_bitmap_obstack); 1100 bool global_changed = true; 1101 1102 /* These regs are considered always live so if they end up dying 1103 because of some def, we need to bring the back again. Calling 1104 df_simulate_fixup_sets has the disadvantage of calling 1105 bb_has_eh_pred once per insn, so we cache the information 1106 here. */ 1107 bitmap au = &df->regular_block_artificial_uses; 1108 bitmap au_eh = &df->eh_block_artificial_uses; 1109 int i; 1110 struct dead_debug_global global_debug; 1111 1112 prescan_insns_for_dce (true); 1113 1114 for (i = 0; i < n_blocks; i++) 1115 bitmap_set_bit (all_blocks, postorder[i]); 1116 1117 dead_debug_global_init (&global_debug, NULL); 1118 1119 while (global_changed) 1120 { 1121 global_changed = false; 1122 1123 for (i = 0; i < n_blocks; i++) 1124 { 1125 int index = postorder[i]; 1126 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, index); 1127 bool local_changed; 1128 1129 if (index < NUM_FIXED_BLOCKS) 1130 { 1131 bitmap_set_bit (processed, index); 1132 continue; 1133 } 1134 1135 if (word_level) 1136 local_changed 1137 = word_dce_process_block (bb, bitmap_bit_p (redo_out, index), 1138 &global_debug); 1139 else 1140 local_changed 1141 = dce_process_block (bb, bitmap_bit_p (redo_out, index), 1142 bb_has_eh_pred (bb) ? au_eh : au, 1143 &global_debug); 1144 bitmap_set_bit (processed, index); 1145 1146 if (local_changed) 1147 { 1148 edge e; 1149 edge_iterator ei; 1150 FOR_EACH_EDGE (e, ei, bb->preds) 1151 if (bitmap_bit_p (processed, e->src->index)) 1152 /* Be tricky about when we need to iterate the 1153 analysis. We only have redo the analysis if the 1154 bitmaps change at the top of a block that is the 1155 entry to a loop. */ 1156 global_changed = true; 1157 else 1158 bitmap_set_bit (redo_out, e->src->index); 1159 } 1160 } 1161 1162 if (global_changed) 1163 { 1164 /* Turn off the RUN_DCE flag to prevent recursive calls to 1165 dce. */ 1166 int old_flag = df_clear_flags (DF_LR_RUN_DCE); 1167 1168 /* So something was deleted that requires a redo. Do it on 1169 the cheap. */ 1170 delete_unmarked_insns (); 1171 bitmap_clear (marked); 1172 bitmap_clear (processed); 1173 bitmap_clear (redo_out); 1174 1175 /* We do not need to rescan any instructions. We only need 1176 to redo the dataflow equations for the blocks that had a 1177 change at the top of the block. Then we need to redo the 1178 iteration. */ 1179 if (word_level) 1180 df_analyze_problem (df_word_lr, all_blocks, postorder, n_blocks); 1181 else 1182 df_analyze_problem (df_lr, all_blocks, postorder, n_blocks); 1183 1184 if (old_flag & DF_LR_RUN_DCE) 1185 df_set_flags (DF_LR_RUN_DCE); 1186 1187 prescan_insns_for_dce (true); 1188 } 1189 } 1190 1191 dead_debug_global_finish (&global_debug, NULL); 1192 1193 delete_unmarked_insns (); 1194 1195 BITMAP_FREE (processed); 1196 BITMAP_FREE (redo_out); 1197 BITMAP_FREE (all_blocks); 1198 } 1199 1200 1201 /* Fast register level DCE. */ 1202 1203 static unsigned int 1204 rest_of_handle_fast_dce (void) 1205 { 1206 init_dce (true); 1207 fast_dce (false); 1208 fini_dce (true); 1209 return 0; 1210 } 1211 1212 1213 /* Fast byte level DCE. */ 1214 1215 void 1216 run_word_dce (void) 1217 { 1218 int old_flags; 1219 1220 if (!flag_dce) 1221 return; 1222 1223 timevar_push (TV_DCE); 1224 old_flags = df_clear_flags (DF_DEFER_INSN_RESCAN + DF_NO_INSN_RESCAN); 1225 df_word_lr_add_problem (); 1226 init_dce (true); 1227 fast_dce (true); 1228 fini_dce (true); 1229 df_set_flags (old_flags); 1230 timevar_pop (TV_DCE); 1231 } 1232 1233 1234 /* This is an internal call that is used by the df live register 1235 problem to run fast dce as a side effect of creating the live 1236 information. The stack is organized so that the lr problem is run, 1237 this pass is run, which updates the live info and the df scanning 1238 info, and then returns to allow the rest of the problems to be run. 1239 1240 This can be called by elsewhere but it will not update the bit 1241 vectors for any other problems than LR. */ 1242 1243 void 1244 run_fast_df_dce (void) 1245 { 1246 if (flag_dce) 1247 { 1248 /* If dce is able to delete something, it has to happen 1249 immediately. Otherwise there will be problems handling the 1250 eq_notes. */ 1251 int old_flags = 1252 df_clear_flags (DF_DEFER_INSN_RESCAN + DF_NO_INSN_RESCAN); 1253 1254 df_in_progress = true; 1255 rest_of_handle_fast_dce (); 1256 df_in_progress = false; 1257 1258 df_set_flags (old_flags); 1259 } 1260 } 1261 1262 1263 /* Run a fast DCE pass. */ 1264 1265 void 1266 run_fast_dce (void) 1267 { 1268 if (flag_dce) 1269 rest_of_handle_fast_dce (); 1270 } 1271 1272 1273 namespace { 1274 1275 const pass_data pass_data_fast_rtl_dce = 1276 { 1277 RTL_PASS, /* type */ 1278 "rtl_dce", /* name */ 1279 OPTGROUP_NONE, /* optinfo_flags */ 1280 TV_DCE, /* tv_id */ 1281 0, /* properties_required */ 1282 0, /* properties_provided */ 1283 0, /* properties_destroyed */ 1284 0, /* todo_flags_start */ 1285 TODO_df_finish, /* todo_flags_finish */ 1286 }; 1287 1288 class pass_fast_rtl_dce : public rtl_opt_pass 1289 { 1290 public: 1291 pass_fast_rtl_dce (gcc::context *ctxt) 1292 : rtl_opt_pass (pass_data_fast_rtl_dce, ctxt) 1293 {} 1294 1295 /* opt_pass methods: */ 1296 virtual bool gate (function *) 1297 { 1298 return optimize > 0 && flag_dce && dbg_cnt (dce_fast); 1299 } 1300 1301 virtual unsigned int execute (function *) 1302 { 1303 return rest_of_handle_fast_dce (); 1304 } 1305 1306 }; // class pass_fast_rtl_dce 1307 1308 } // anon namespace 1309 1310 rtl_opt_pass * 1311 make_pass_fast_rtl_dce (gcc::context *ctxt) 1312 { 1313 return new pass_fast_rtl_dce (ctxt); 1314 } 1315