OSDN Git Service

* gcc.c-torture/compile/20001226-1.x: Only xfail for Xtensa
[pf3gnuchains/gcc-fork.git] / gcc / ifcvt.c
index 207ff5c..842eb54 100644 (file)
@@ -27,6 +27,7 @@
 #include "flags.h"
 #include "insn-config.h"
 #include "recog.h"
+#include "except.h"
 #include "hard-reg-set.h"
 #include "basic-block.h"
 #include "expr.h"
@@ -104,19 +105,12 @@ static int find_if_block          PARAMS ((basic_block, edge, edge));
 static int find_if_case_1              PARAMS ((basic_block, edge, edge));
 static int find_if_case_2              PARAMS ((basic_block, edge, edge));
 static int find_cond_trap              PARAMS ((basic_block, edge, edge));
+static rtx block_has_only_trap         PARAMS ((basic_block));
 static int find_memory                 PARAMS ((rtx *, void *));
 static int dead_or_predicable          PARAMS ((basic_block, basic_block,
                                                 basic_block, basic_block, int));
 static void noce_emit_move_insn                PARAMS ((rtx, rtx));
 \f
-/* Abuse the basic_block AUX field to store the original block index,
-   as well as a flag indicating that the block should be rescaned for
-   life analysis.  */
-
-#define SET_ORIG_INDEX(BB,I)   ((BB)->aux = (void *)((size_t)(I)))
-#define ORIG_INDEX(BB)         ((size_t)(BB)->aux)
-
-\f
 /* Count the number of non-jump active insns in BB.  */
 
 static int
@@ -618,7 +612,7 @@ noce_try_store_flag (if_info)
 
       seq = get_insns ();
       end_sequence ();
-      emit_insns_before (seq, if_info->cond_earliest);
+      emit_insns_before_scope (seq, if_info->jump, INSN_SCOPE (if_info->insn_a));
 
       return TRUE;
     }
@@ -753,7 +747,7 @@ noce_try_store_flag_constants (if_info)
       if (seq_contains_jump (seq))
        return FALSE;
 
-      emit_insns_before (seq, if_info->cond_earliest);
+      emit_insns_before_scope (seq, if_info->jump, INSN_SCOPE (if_info->insn_a));
 
       return TRUE;
     }
@@ -813,7 +807,8 @@ noce_try_store_flag_inc (if_info)
          if (seq_contains_jump (seq))
            return FALSE;
 
-         emit_insns_before (seq, if_info->cond_earliest);
+         emit_insns_before_scope (seq, if_info->jump,
+                                  INSN_SCOPE (if_info->insn_a));
 
          return TRUE;
        }
@@ -865,7 +860,8 @@ noce_try_store_flag_mask (if_info)
          if (seq_contains_jump (seq))
            return FALSE;
 
-         emit_insns_before (seq, if_info->cond_earliest);
+         emit_insns_before_scope (seq, if_info->jump,
+                                  INSN_SCOPE (if_info->insn_a));
 
          return TRUE;
        }
@@ -960,7 +956,8 @@ noce_try_cmove (if_info)
 
          seq = get_insns ();
          end_sequence ();
-         emit_insns_before (seq, if_info->cond_earliest);
+         emit_insns_before_scope (seq, if_info->jump,
+                                  INSN_SCOPE (if_info->insn_a));
          return TRUE;
        }
       else
@@ -1122,7 +1119,7 @@ noce_try_cmove_arith (if_info)
 
   tmp = get_insns ();
   end_sequence ();
-  emit_insns_before (tmp, if_info->cond_earliest);
+  emit_insns_before_scope (tmp, if_info->jump, INSN_SCOPE (if_info->insn_a));
   return TRUE;
 
  end_seq_and_fail:
@@ -1374,7 +1371,7 @@ noce_try_minmax (if_info)
   if (seq_contains_jump (seq))
     return FALSE;
 
-  emit_insns_before (seq, earliest);
+  emit_insns_before_scope (seq, if_info->jump, INSN_SCOPE (if_info->insn_a));
   if_info->cond = cond;
   if_info->cond_earliest = earliest;
 
@@ -1492,7 +1489,7 @@ noce_try_abs (if_info)
   if (seq_contains_jump (seq))
     return FALSE;
 
-  emit_insns_before (seq, earliest);
+  emit_insns_before_scope (seq, if_info->jump, INSN_SCOPE (if_info->insn_a));
   if_info->cond = cond;
   if_info->cond_earliest = earliest;
 
@@ -1751,7 +1748,7 @@ noce_process_if_block (test_bb, then_bb, else_bb, join_bb)
   if (insn_b && else_bb)
     delete_insn (insn_b);
 
-  /* The new insns will have been inserted before cond_earliest.  We should
+  /* The new insns will have been inserted just before the jump.  We should
      be able to remove the jump with impunity, but the condition itself may
      have been modified by gcse to be shared across basic blocks.  */
   delete_insn (jump);
@@ -1764,7 +1761,7 @@ noce_process_if_block (test_bb, then_bb, else_bb, join_bb)
       insn_b = gen_sequence ();
       end_sequence ();
 
-      emit_insn_after (insn_b, test_bb->end);
+      emit_insn_after_scope (insn_b, test_bb->end, INSN_SCOPE (insn_a));
     }
 
   /* Merge the blocks!  */
@@ -1812,11 +1809,14 @@ merge_if_block (test_bb, then_bb, else_bb, join_bb)
 
   /* First merge TEST block into THEN block.  This is a no-brainer since
      the THEN block did not have a code label to begin with.  */
-
-  if (combo_bb->global_live_at_end)
-    COPY_REG_SET (combo_bb->global_live_at_end, then_bb->global_live_at_end);
-  merge_blocks_nomove (combo_bb, then_bb);
-  num_removed_blocks++;
+  if (then_bb)
+    {
+      if (combo_bb->global_live_at_end)
+       COPY_REG_SET (combo_bb->global_live_at_end,
+                     then_bb->global_live_at_end);
+      merge_blocks_nomove (combo_bb, then_bb);
+      num_removed_blocks++;
+    }
 
   /* The ELSE block, if it existed, had a label.  That label count
      will almost always be zero, but odd things can happen when labels
@@ -1832,14 +1832,34 @@ merge_if_block (test_bb, then_bb, else_bb, join_bb)
 
   if (! join_bb)
     {
+      rtx last = combo_bb->end;
+
       /* The outgoing edge for the current COMBO block should already
         be correct.  Verify this.  */
       if (combo_bb->succ == NULL_EDGE)
-       abort ();
+       {
+         if (find_reg_note (last, REG_NORETURN, NULL))
+           ;
+         else if (GET_CODE (last) == INSN
+                  && GET_CODE (PATTERN (last)) == TRAP_IF
+                  && TRAP_CONDITION (PATTERN (last)) == const_true_rtx)
+           ;
+         else
+           abort ();
+       }
 
-      /* There should still be a branch at the end of the THEN or ELSE
+      /* There should still be something at the end of the THEN or ELSE
          blocks taking us to our final destination.  */
-      if (GET_CODE (combo_bb->end) != JUMP_INSN)
+      else if (GET_CODE (last) == JUMP_INSN)
+       ;
+      else if (combo_bb->succ->dest == EXIT_BLOCK_PTR
+              && GET_CODE (last) == CALL_INSN
+              && SIBLING_CALL_P (last))
+       ;
+      else if ((combo_bb->succ->flags & EDGE_EH)
+              && can_throw_internal (last))
+       ;
+      else
        abort ();
     }
 
@@ -1948,7 +1968,7 @@ find_if_block (test_bb, then_edge, else_edge)
   basic_block join_bb = NULL_BLOCK;
   edge then_succ = then_bb->succ;
   edge else_succ = else_bb->succ;
-  int next_index;
+  basic_block next;
 
   /* The THEN block of an IF-THEN combo must have exactly one predecessor.  */
   if (then_bb->pred->pred_next != NULL_EDGE)
@@ -2032,10 +2052,10 @@ find_if_block (test_bb, then_edge, else_edge)
   /* ??? As an enhancement, move the ELSE block.  Have to deal with
      BLOCK notes, if by no other means than aborting the merge if they
      exist.  Sticky enough I don't want to think about it now.  */
-  next_index = then_bb->index;
-  if (else_bb && ++next_index != else_bb->index)
+  next = then_bb;
+  if (else_bb && (next = next->next_bb) != else_bb)
     return FALSE;
-  if (++next_index != join_bb->index && join_bb->index != EXIT_BLOCK)
+  if ((next = next->next_bb) != join_bb && join_bb != EXIT_BLOCK_PTR)
     {
       if (else_bb)
        join_bb = NULL;
@@ -2055,68 +2075,27 @@ find_cond_trap (test_bb, then_edge, else_edge)
      basic_block test_bb;
      edge then_edge, else_edge;
 {
-  basic_block then_bb, else_bb, join_bb, trap_bb;
+  basic_block then_bb, else_bb, trap_bb, other_bb;
   rtx trap, jump, cond, cond_earliest, seq;
   enum rtx_code code;
 
   then_bb = then_edge->dest;
   else_bb = else_edge->dest;
-  join_bb = NULL;
 
   /* Locate the block with the trap instruction.  */
   /* ??? While we look for no successors, we really ought to allow
      EH successors.  Need to fix merge_if_block for that to work.  */
-  /* ??? We can't currently handle merging the blocks if they are not
-     already adjacent.  Prevent losage in merge_if_block by detecting
-     this now.  */
-  if (then_bb->succ == NULL)
-    {
-      trap_bb = then_bb;
-      if (else_bb->index != then_bb->index + 1)
-       return FALSE;
-      join_bb = else_bb;
-      else_bb = NULL;
-    }
-  else if (else_bb->succ == NULL)
-    {
-      trap_bb = else_bb;
-      if (else_bb->index != then_bb->index + 1)
-       else_bb = NULL;
-      else if (then_bb->succ
-         && ! then_bb->succ->succ_next
-         && ! (then_bb->succ->flags & EDGE_COMPLEX)
-         && then_bb->succ->dest->index == else_bb->index + 1)
-       join_bb = then_bb->succ->dest;
-    }
+  if ((trap = block_has_only_trap (then_bb)) != NULL)
+    trap_bb = then_bb, other_bb = else_bb;
+  else if ((trap = block_has_only_trap (else_bb)) != NULL)
+    trap_bb = else_bb, other_bb = then_bb;
   else
     return FALSE;
 
-  /* Don't confuse a conditional return with something we want to
-     optimize here.  */
-  if (trap_bb == EXIT_BLOCK_PTR)
-    return FALSE;
-
-  /* The only instruction in the THEN block must be the trap.  */
-  trap = first_active_insn (trap_bb);
-  if (! (trap == trap_bb->end
-        && GET_CODE (PATTERN (trap)) == TRAP_IF
-         && TRAP_CONDITION (PATTERN (trap)) == const_true_rtx))
-    return FALSE;
-
   if (rtl_dump_file)
     {
-      if (trap_bb == then_bb)
-       fprintf (rtl_dump_file,
-                "\nTRAP-IF block found, start %d, trap %d",
-                test_bb->index, then_bb->index);
-      else
-       fprintf (rtl_dump_file,
-                "\nTRAP-IF block found, start %d, then %d, trap %d",
-                test_bb->index, then_bb->index, trap_bb->index);
-      if (join_bb)
-       fprintf (rtl_dump_file, ", join %d\n", join_bb->index);
-      else
-       fputc ('\n', rtl_dump_file);
+      fprintf (rtl_dump_file, "\nTRAP-IF block found, start %d, trap %d\n",
+              test_bb->index, trap_bb->index);
     }
 
   /* If this is not a standard conditional jump, we can't parse it.  */
@@ -2149,26 +2128,67 @@ find_cond_trap (test_bb, then_edge, else_edge)
   if (seq == NULL)
     return FALSE;
 
-  /* Emit the new insns before cond_earliest; delete the old jump
-     and trap insns.  */
-
-  emit_insn_before (seq, cond_earliest);
-
-  delete_insn (jump);
-
-  delete_insn (trap);
+  /* Emit the new insns before cond_earliest.  */
+  emit_insn_before_scope (seq, cond_earliest, INSN_SCOPE (trap));
 
-  /* Merge the blocks!  */
-  if (trap_bb != then_bb && ! else_bb)
+  /* Delete the trap block if possible.  */
+  remove_edge (trap_bb == then_bb ? then_edge : else_edge);
+  if (trap_bb->pred == NULL)
     {
       flow_delete_block (trap_bb);
       num_removed_blocks++;
     }
-  merge_if_block (test_bb, then_bb, else_bb, join_bb);
+
+  /* If the non-trap block and the test are now adjacent, merge them.
+     Otherwise we must insert a direct branch.  */
+  if (test_bb->next_bb == other_bb)
+    {
+      delete_insn (jump);
+      merge_if_block (test_bb, NULL, NULL, other_bb);
+    }
+  else
+    {
+      rtx lab, newjump;
+
+      lab = JUMP_LABEL (jump);
+      newjump = emit_jump_insn_after (gen_jump (lab), jump);
+      LABEL_NUSES (lab) += 1;
+      JUMP_LABEL (newjump) = lab;
+      emit_barrier_after (newjump);
+
+      delete_insn (jump);
+    }
 
   return TRUE;
 }
 
+/* Subroutine of find_cond_trap: if BB contains only a trap insn, 
+   return it.  */
+
+static rtx
+block_has_only_trap (bb)
+     basic_block bb;
+{
+  rtx trap;
+
+  /* We're not the exit block.  */
+  if (bb == EXIT_BLOCK_PTR)
+    return NULL_RTX;
+
+  /* The block must have no successors.  */
+  if (bb->succ)
+    return NULL_RTX;
+
+  /* The only instruction in the THEN block must be the trap.  */
+  trap = first_active_insn (bb);
+  if (! (trap == bb->end
+        && GET_CODE (PATTERN (trap)) == TRAP_IF
+         && TRAP_CONDITION (PATTERN (trap)) == const_true_rtx))
+    return NULL_RTX;
+
+  return trap;
+}
+
 /* Look for IF-THEN-ELSE cases in which one of THEN or ELSE is
    transformable, but not necessarily the other.  There need be no
    JOIN block.
@@ -2254,6 +2274,7 @@ find_if_case_1 (test_bb, then_edge, else_edge)
   basic_block then_bb = then_edge->dest;
   basic_block else_bb = else_edge->dest, new_bb;
   edge then_succ = then_bb->succ;
+  int then_bb_index;
 
   /* THEN has one successor.  */
   if (!then_succ || then_succ->succ_next != NULL)
@@ -2294,11 +2315,15 @@ find_if_case_1 (test_bb, then_edge, else_edge)
                    then_bb->global_live_at_end, BITMAP_IOR);
   
   new_bb = redirect_edge_and_branch_force (FALLTHRU_EDGE (test_bb), else_bb);
+  then_bb_index = then_bb->index;
+  flow_delete_block (then_bb);
   /* Make rest of code believe that the newly created block is the THEN_BB
-     block we are going to remove.  */
+     block we removed.  */
   if (new_bb)
-    new_bb->aux = then_bb->aux;
-  flow_delete_block (then_bb);
+    {
+      new_bb->index = then_bb_index;
+      BASIC_BLOCK (then_bb_index) = new_bb;
+    }
   /* We've possibly created jump to next insn, cleanup_cfg will solve that
      later.  */
 
@@ -2341,8 +2366,8 @@ find_if_case_2 (test_bb, then_edge, else_edge)
   if (note && INTVAL (XEXP (note, 0)) >= REG_BR_PROB_BASE / 2)
     ;
   else if (else_succ->dest->index < 0
-          || TEST_BIT (post_dominators[ORIG_INDEX (then_bb)], 
-                       ORIG_INDEX (else_succ->dest)))
+          || TEST_BIT (post_dominators[then_bb->index], 
+                       else_succ->dest->index))
     ;
   else
     return FALSE;
@@ -2660,7 +2685,7 @@ void
 if_convert (x_life_data_ok)
      int x_life_data_ok;
 {
-  int block_num;
+  basic_block bb;
 
   num_possible_if_blocks = 0;
   num_updated_if_blocks = 0;
@@ -2675,25 +2700,16 @@ if_convert (x_life_data_ok)
   post_dominators = NULL;
   if (HAVE_conditional_execution || life_data_ok)
     {
-      post_dominators = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
+      post_dominators = sbitmap_vector_alloc (last_basic_block, last_basic_block);
       calculate_dominance_info (NULL, post_dominators, CDI_POST_DOMINATORS);
     }
   if (life_data_ok)
     clear_bb_flags ();
 
-  /* Record initial block numbers.  */
-  for (block_num = 0; block_num < n_basic_blocks; block_num++)
-    SET_ORIG_INDEX (BASIC_BLOCK (block_num), block_num);
-
   /* Go through each of the basic blocks looking for things to convert.  */
-  for (block_num = 0; block_num < n_basic_blocks; )
-    {
-      basic_block bb = BASIC_BLOCK (block_num);
-      if (find_if_header (bb))
-       block_num = bb->index;
-      else 
-       block_num++;
-    }
+  FOR_EACH_BB (bb)
+    while (find_if_header (bb))
+      continue;
 
   if (post_dominators)
     sbitmap_vector_free (post_dominators);