OSDN Git Service

PR middle-end/43125
[pf3gnuchains/gcc-fork.git] / gcc / dce.c
index 75e148c..855bef3 100644 (file)
--- a/gcc/dce.c
+++ b/gcc/dce.c
@@ -27,6 +27,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "regs.h"
 #include "hard-reg-set.h"
 #include "flags.h"
+#include "except.h"
 #include "df.h"
 #include "cselib.h"
 #include "dce.h"
@@ -35,9 +36,6 @@ along with GCC; see the file COPYING3.  If not see
 #include "dbgcnt.h"
 #include "tm_p.h"
 
-DEF_VEC_I(int);
-DEF_VEC_ALLOC_I(int,heap);
-
 
 /* -------------------------------------------------------------------------
    Core mark/delete routines
@@ -81,13 +79,7 @@ deletable_insn_p_1 (rtx body)
       return false;
 
     default:
-      if (volatile_refs_p (body))
-       return false;
-
-      if (flag_non_call_exceptions && may_trap_p (body))
-       return false;
-
-      return true;
+      return !volatile_refs_p (body);
     }
 }
 
@@ -101,6 +93,14 @@ deletable_insn_p (rtx insn, bool fast, bitmap arg_stores)
   rtx body, x;
   int i;
 
+  /* Don't delete jumps, notes and the like.  */
+  if (!NONJUMP_INSN_P (insn))
+    return false;
+
+  /* Don't delete insns that can throw.  */
+  if (!insn_nothrow_p (insn))
+    return false;
+
   if (CALL_P (insn)
       /* We cannot delete calls inside of the recursive dce because
         this may cause basic blocks to be deleted and this messes up
@@ -115,13 +115,11 @@ deletable_insn_p (rtx insn, bool fast, bitmap arg_stores)
          && !RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)))
     return find_call_stack_args (insn, false, fast, arg_stores);
 
-  if (!NONJUMP_INSN_P (insn))
-    return false;
-
   body = PATTERN (insn);
   switch (GET_CODE (body))
     {
     case USE:
+    case VAR_LOCATION:
       return false;
 
     case CLOBBER:
@@ -352,13 +350,13 @@ find_call_stack_args (rtx call_insn, bool do_mark, bool fast,
          }
        for (byte = off; byte < off + INTVAL (MEM_SIZE (mem)); byte++)
          {
-           gcc_assert (!bitmap_bit_p (sp_bytes, byte - min_sp_off));
-           bitmap_set_bit (sp_bytes, byte - min_sp_off);
+           if (!bitmap_set_bit (sp_bytes, byte - min_sp_off))
+             gcc_unreachable ();
          }
       }
 
   /* Walk backwards, looking for argument stores.  The search stops
-     when seeting another call, sp adjustment or memory store other than
+     when seeing another call, sp adjustment or memory store other than
      argument store.  */
   ret = false;
   for (insn = PREV_INSN (call_insn); insn; insn = prev_insn)
@@ -440,9 +438,8 @@ find_call_stack_args (rtx call_insn, bool do_mark, bool fast,
        {
          if (byte < min_sp_off
              || byte >= max_sp_off
-             || !bitmap_bit_p (sp_bytes, byte - min_sp_off))
+             || !bitmap_clear_bit (sp_bytes, byte - min_sp_off))
            break;
-         bitmap_clear_bit (sp_bytes, byte - min_sp_off);
        }
 
       if (!deletable_insn_p (insn, fast, NULL))
@@ -510,8 +507,8 @@ delete_unmarked_insns (void)
   rtx insn, next;
   bool must_clean = false;
 
-  FOR_EACH_BB (bb)
-    FOR_BB_INSNS_SAFE (bb, insn, next)
+  FOR_EACH_BB_REVERSE (bb)
+    FOR_BB_INSNS_REVERSE_SAFE (bb, insn, next)
       if (INSN_P (insn))
        {
          /* Always delete no-op moves.  */
@@ -522,9 +519,24 @@ delete_unmarked_insns (void)
          else if (marked_insn_p (insn))
            continue;
 
-         /* Beware that reaching a dbg counter limit here can easily result
-            in miscompiled file, whenever some insn is eliminated, but
-            insn that depends on it is not.  */
+         /* Beware that reaching a dbg counter limit here can result
+            in miscompiled file.  This occurs when a group of insns
+            must be deleted together, typically because the kept insn
+            depends on the output from the deleted insn.  Deleting
+            this insns in reverse order (both at the bb level and
+            when looking at the blocks) minimizes this, but does not
+            eliminate it, since it is possible for the using insn to
+            be top of a block and the producer to be at the bottom of
+            the block.  However, in most cases this will only result
+            in an uninitialized use of an insn that is dead anyway.
+
+            However, there is one rare case that will cause a
+            miscompile: deletion of non-looping pure and constant
+            calls on a machine where ACCUMULATE_OUTGOING_ARGS is true.
+            In this case it is possible to remove the call, but leave
+            the argument pushes to the stack.  Because of the changes
+            to the stack pointer, this will almost always lead to a
+            miscompile.  */
          if (!dbg_cnt (dce))
            continue;
 
@@ -610,7 +622,7 @@ mark_artificial_uses (void)
 
   FOR_ALL_BB (bb)
     {
-      for (use_rec = df_get_artificial_uses (bb->index); 
+      for (use_rec = df_get_artificial_uses (bb->index);
           *use_rec; use_rec++)
        for (defs = DF_REF_CHAIN (*use_rec); defs; defs = defs->next)
          if (! DF_REF_IS_ARTIFICIAL (defs->ref))
@@ -627,6 +639,9 @@ mark_reg_dependencies (rtx insn)
   struct df_link *defs;
   df_ref *use_rec;
 
+  if (DEBUG_INSN_P (insn))
+    return;
+
   for (use_rec = DF_INSN_USES (insn); *use_rec; use_rec++)
     {
       df_ref use = *use_rec;
@@ -723,9 +738,9 @@ struct rtl_opt_pass pass_ud_rtl_dce =
 {
  {
   RTL_PASS,
-  "dce",                                /* name */
-  gate_ud_dce,                        /* gate */
-  rest_of_handle_ud_dce,              /* execute */
+  "ud dce",                             /* name */
+  gate_ud_dce,                          /* gate */
+  rest_of_handle_ud_dce,                /* execute */
   NULL,                                 /* sub */
   NULL,                                 /* next */
   0,                                    /* static_pass_number */
@@ -810,7 +825,7 @@ byte_dce_process_block (basic_block bb, bool redo_out, bitmap au)
                mark_insn (insn, true);
                goto quickexit;
              }
-           
+
            last = start + len;
            while (start < last)
              if (bitmap_bit_p (local_live, start++))
@@ -819,9 +834,9 @@ byte_dce_process_block (basic_block bb, bool redo_out, bitmap au)
                  goto quickexit;
                }
          }
-       
-      quickexit: 
-       
+
+      quickexit:
+
        /* No matter if the instruction is needed or not, we remove
           any regno in the defs from the live set.  */
        df_byte_lr_simulate_defs (insn, local_live);
@@ -833,12 +848,12 @@ byte_dce_process_block (basic_block bb, bool redo_out, bitmap au)
 
        if (dump_file)
          {
-           fprintf (dump_file, "finished processing insn %d live out = ", 
+           fprintf (dump_file, "finished processing insn %d live out = ",
                     INSN_UID (insn));
            df_print_byte_regset (dump_file, local_live);
          }
       }
-  
+
   df_byte_lr_simulate_artificial_refs_at_top (bb, local_live);
 
   block_changed = !bitmap_equal_p (local_live, DF_BYTE_LR_IN (bb));
@@ -898,10 +913,10 @@ dce_process_block (basic_block bb, bool redo_out, bitmap au)
              needed = true;
              break;
            }
-           
+
        if (needed)
          mark_insn (insn, true);
-       
+
        /* No matter if the instruction is needed or not, we remove
           any regno in the defs from the live set.  */
        df_simulate_defs (insn, local_live);
@@ -911,7 +926,7 @@ dce_process_block (basic_block bb, bool redo_out, bitmap au)
        if (marked_insn_p (insn))
          df_simulate_uses (insn, local_live);
       }
-  
+
   df_simulate_finalize_backwards (bb, local_live);
 
   block_changed = !bitmap_equal_p (local_live, DF_LR_IN (bb));
@@ -971,15 +986,15 @@ fast_dce (bool byte_level)
            }
 
          if (byte_level)
-           local_changed 
+           local_changed
              = byte_dce_process_block (bb, bitmap_bit_p (redo_out, index),
                                          bb_has_eh_pred (bb) ? au_eh : au);
          else
-           local_changed 
+           local_changed
              = dce_process_block (bb, bitmap_bit_p (redo_out, index),
                                   bb_has_eh_pred (bb) ? au_eh : au);
          bitmap_set_bit (processed, index);
-         
+
          if (local_changed)
            {
              edge e;
@@ -995,7 +1010,7 @@ fast_dce (bool byte_level)
                  bitmap_set_bit (redo_out, e->src->index);
            }
        }
-      
+
       if (global_changed)
        {
          /* Turn off the RUN_DCE flag to prevent recursive calls to
@@ -1008,11 +1023,11 @@ fast_dce (bool byte_level)
          sbitmap_zero (marked);
          bitmap_clear (processed);
          bitmap_clear (redo_out);
-         
+
          /* We do not need to rescan any instructions.  We only need
             to redo the dataflow equations for the blocks that had a
             change at the top of the block.  Then we need to redo the
-            iteration.  */ 
+            iteration.  */
          if (byte_level)
            df_analyze_problem (df_byte_lr, all_blocks, postorder, n_blocks);
          else
@@ -1075,9 +1090,9 @@ run_fast_df_dce (void)
       /* If dce is able to delete something, it has to happen
         immediately.  Otherwise there will be problems handling the
         eq_notes.  */
-      enum df_changeable_flags old_flags 
-       df_clear_flags (DF_DEFER_INSN_RESCAN + DF_NO_INSN_RESCAN);
-      
+      int old_flags =
+       df_clear_flags (DF_DEFER_INSN_RESCAN + DF_NO_INSN_RESCAN);
+
       df_in_progress = true;
       rest_of_handle_fast_dce ();
       df_in_progress = false;
@@ -1108,7 +1123,7 @@ struct rtl_opt_pass pass_fast_rtl_dce =
 {
  {
   RTL_PASS,
-  "dce",                                /* name */
+  "rtl dce",                            /* name */
   gate_fast_dce,                        /* gate */
   rest_of_handle_fast_dce,              /* execute */
   NULL,                                 /* sub */