gcc/ PR rtl-optimization/51447 * df-scan.c (df_get_entry_block_def_set): Add global regs to the set. * df-problems.c (df_lr_local_compute): Make global regs always live. * dce.c (deletable_insn_p): Make insns setting a global reg inherently necessary. testsuite/ PR rtl-optimization/51447 * gcc.c-torture/execute/pr51447.c: New test. Index: df-scan.c =================================================================== --- df-scan.c (revision 193411) +++ df-scan.c (working copy) @@ -3790,8 +3790,12 @@ df_get_entry_block_def_set (bitmap entry bitmap_clear (entry_block_defs); for (i = 0; i < FIRST_PSEUDO_REGISTER; i++) - if (FUNCTION_ARG_REGNO_P (i)) - bitmap_set_bit (entry_block_defs, INCOMING_REGNO (i)); + { + if (global_regs[i]) + bitmap_set_bit (entry_block_defs, i); + if (FUNCTION_ARG_REGNO_P (i)) + bitmap_set_bit (entry_block_defs, INCOMING_REGNO (i)); + } /* The always important stack pointer. */ bitmap_set_bit (entry_block_defs, STACK_POINTER_REGNUM); Index: df-problems.c =================================================================== --- df-problems.c (revision 193411) +++ df-problems.c (working copy) @@ -931,7 +931,7 @@ df_lr_bb_local_compute (unsigned int bb_ static void df_lr_local_compute (bitmap all_blocks ATTRIBUTE_UNUSED) { - unsigned int bb_index; + unsigned int bb_index, i; bitmap_iterator bi; bitmap_clear (&df->hardware_regs_used); @@ -939,6 +939,11 @@ df_lr_local_compute (bitmap all_blocks A /* The all-important stack pointer must always be live. */ bitmap_set_bit (&df->hardware_regs_used, STACK_POINTER_REGNUM); + /* Global regs are always live, too. */ + for (i = 0; i < FIRST_PSEUDO_REGISTER; i++) + if (global_regs[i]) + bitmap_set_bit (&df->hardware_regs_used, i); + /* Before reload, there are a few registers that must be forced live everywhere -- which might not already be the case for blocks within infinite loops. */ Index: dce.c =================================================================== --- dce.c (revision 193411) +++ dce.c (working copy) @@ -121,6 +121,12 @@ deletable_insn_p (rtx insn, bool fast, b && !insn_nothrow_p (insn)) return false; + /* If INSN sets a global_reg, leave it untouched. */ + for (df_ref *def_rec = DF_INSN_DEFS (insn); *def_rec; def_rec++) + if (HARD_REGISTER_NUM_P (DF_REF_REGNO (*def_rec)) + && global_regs[DF_REF_REGNO (*def_rec)]) + return false; + body = PATTERN (insn); switch (GET_CODE (body)) { Index: testsuite/gcc.c-torture/execute/pr51447.c =================================================================== --- testsuite/gcc.c-torture/execute/pr51447.c (revision 0) +++ testsuite/gcc.c-torture/execute/pr51447.c (revision 0) @@ -0,0 +1,27 @@ +/* PR rtl-optimization/51447 */ + +extern void abort (void); + +#ifdef __x86_64__ +register void *ptr asm ("rbx"); +#else +void *ptr; +#endif + +int +main (void) +{ + __label__ nonlocal_lab; + __attribute__((noinline, noclone)) void + bar (void *func) + { + ptr = func; + goto nonlocal_lab; + } + bar (&&nonlocal_lab); + return 1; +nonlocal_lab: + if (ptr != &&nonlocal_lab) + abort (); + return 0; +}