OSDN Git Service

PR tree-optimization/16867
[pf3gnuchains/gcc-fork.git] / gcc / cfghooks.c
index fd361c5..bd67a21 100644 (file)
@@ -1,5 +1,5 @@
 /* Hooks for cfg representation specific functions.
-   Copyright (C) 2003 Free Software Foundation, Inc.
+   Copyright (C) 2003, 2004 Free Software Foundation, Inc.
    Contributed by Sebastian Pop <s.pop@laposte.net>
 
 This file is part of GCC.
@@ -26,6 +26,7 @@ Boston, MA 02111-1307, USA.  */
 #include "tree.h"
 #include "rtl.h"
 #include "basic-block.h"
+#include "tree-flow.h"
 #include "timevar.h"
 #include "toplev.h"
 
@@ -46,6 +47,22 @@ cfg_layout_rtl_register_cfg_hooks (void)
   cfg_hooks = &cfg_layout_rtl_cfg_hooks;
 }
 
+/* Initialization of functions specific to the tree IR.  */
+
+void
+tree_register_cfg_hooks (void)
+{
+  cfg_hooks = &tree_cfg_hooks;
+}
+
+/* Returns current ir type (rtl = 0, trees = 1).  */
+
+int
+ir_type (void)
+{
+  return cfg_hooks == &tree_cfg_hooks ? 1 : 0;
+}
+
 /* Verify the CFG consistency.
 
    Currently it does following: checks edge and basic block list correctness
@@ -206,8 +223,8 @@ dump_bb (basic_block bb, FILE *outf, int indent)
   edge e;
   char *s_indent;
  
-  s_indent = (char *) alloca ((size_t) indent + 1);
-  memset ((void *) s_indent, ' ', (size_t) indent);
+  s_indent = alloca ((size_t) indent + 1);
+  memset (s_indent, ' ', (size_t) indent);
   s_indent[indent] = '\0';
 
   fprintf (outf, ";;%s basic block %d, loop depth %d, count ",
@@ -246,10 +263,10 @@ dump_bb (basic_block bb, FILE *outf, int indent)
    be equivalent to E in the case of duplicate edges being removed) or NULL
    if edge is not easily redirectable for whatever reason.  */
 
-bool
+edge
 redirect_edge_and_branch (edge e, basic_block dest)
 {
-  bool ret;
+  edge ret;
 
   if (!cfg_hooks->redirect_edge_and_branch)
     internal_error ("%s does not support redirect_edge_and_branch.",
@@ -304,7 +321,7 @@ split_block (basic_block bb, void *i)
       set_immediate_dominator (CDI_DOMINATORS, new_bb, bb);
     }
 
-  return make_edge (bb, new_bb, EDGE_FALLTHRU);
+  return make_single_succ_edge (bb, new_bb, EDGE_FALLTHRU);
 }
 
 /* Splits block BB just after labels.  The newly created edge is returned.  */
@@ -315,7 +332,7 @@ split_block_after_labels (basic_block bb)
   return split_block (bb, NULL);
 }
 
-/* Moves block BB immediatelly after block AFTER.  Returns false if the
+/* Moves block BB immediately after block AFTER.  Returns false if the
    movement was impossible.  */
 
 bool
@@ -368,6 +385,7 @@ split_edge (edge e)
   basic_block ret;
   gcov_type count = e->count;
   int freq = EDGE_FREQUENCY (e);
+  edge f;
 
   if (!cfg_hooks->split_edge)
     internal_error ("%s does not support split_edge.", cfg_hooks->name);
@@ -382,9 +400,33 @@ split_edge (edge e)
     set_immediate_dominator (CDI_DOMINATORS, ret, ret->pred->src);
 
   if (dom_computed[CDI_DOMINATORS] >= DOM_NO_FAST_QUERY)
-    set_immediate_dominator (CDI_DOMINATORS, ret->succ->dest,
-                            recount_dominator (CDI_DOMINATORS,
-                                               ret->succ->dest));
+    {
+      /* There are two cases:
+
+        If the immediate dominator of e->dest is not e->src, it
+        remains unchanged.
+
+        If immediate dominator of e->dest is e->src, it may become
+        ret, provided that all other predecessors of e->dest are
+        dominated by e->dest.  */
+
+      if (get_immediate_dominator (CDI_DOMINATORS, ret->succ->dest)
+         == ret->pred->src)
+       {
+         for (f = ret->succ->dest->pred; f; f = f->pred_next)
+           {
+             if (f == ret->succ)
+               continue;
+
+             if (!dominated_by_p (CDI_DOMINATORS, f->src,
+                                  ret->succ->dest))
+               break;
+           }
+
+         if (!f)
+           set_immediate_dominator (CDI_DOMINATORS, ret->succ->dest, ret);
+       }
+    };
 
   return ret;
 }
@@ -434,6 +476,24 @@ can_merge_blocks_p (basic_block bb1, basic_block bb2)
   return ret;
 }
 
+void
+predict_edge (edge e, enum br_predictor predictor, int probability)
+{
+  if (!cfg_hooks->predict_edge)
+    internal_error ("%s does not support predict_edge.", cfg_hooks->name);
+
+  cfg_hooks->predict_edge (e, predictor, probability);
+}
+
+bool
+predicted_by_p (basic_block bb, enum br_predictor predictor)
+{
+  if (!cfg_hooks->predict_edge)
+    internal_error ("%s does not support predicted_by_p.", cfg_hooks->name);
+
+  return cfg_hooks->predicted_by_p (bb, predictor);
+}
+
 /* Merges basic block B into basic block A.  */
 
 void
@@ -506,6 +566,9 @@ make_forwarder_block (basic_block bb, bool (*redirect_edge_p) (edge),
        dummy->frequency = 0;
       if (dummy->count < 0)
        dummy->count = 0;
+      fallthru->count -= e->count;
+      if (fallthru->count < 0)
+       fallthru->count = 0;
 
       jump = redirect_edge_and_branch_force (e, bb);
       if (jump)
@@ -570,7 +633,145 @@ tidy_fallthru_edges (void)
       if ((s = b->succ) != NULL
          && ! (s->flags & EDGE_COMPLEX)
          && s->succ_next == NULL
-         && s->dest == c)
+         && s->dest == c
+         && !find_reg_note (BB_END (b), REG_CROSSING_JUMP, NULL_RTX))
        tidy_fallthru_edge (s);
     }
 }
+
+/* Returns true if we can duplicate basic block BB.  */
+
+bool
+can_duplicate_block_p (basic_block bb)
+{
+  edge e;
+
+  if (!cfg_hooks->can_duplicate_block_p)
+    internal_error ("%s does not support can_duplicate_block_p.",
+                   cfg_hooks->name);
+
+  if (bb == EXIT_BLOCK_PTR || bb == ENTRY_BLOCK_PTR)
+    return false;
+
+  /* Duplicating fallthru block to exit would require adding a jump
+     and splitting the real last BB.  */
+  for (e = bb->succ; e; e = e->succ_next)
+    if (e->dest == EXIT_BLOCK_PTR && e->flags & EDGE_FALLTHRU)
+       return false;
+
+  return cfg_hooks->can_duplicate_block_p (bb);
+}
+
+/* Duplicates basic block BB and redirects edge E to it.  Returns the
+   new basic block.  */
+
+basic_block
+duplicate_block (basic_block bb, edge e)
+{
+  edge s, n;
+  basic_block new_bb;
+  gcov_type new_count = e ? e->count : 0;
+
+  if (!cfg_hooks->duplicate_block)
+    internal_error ("%s does not support duplicate_block.",
+                   cfg_hooks->name);
+
+  if (bb->count < new_count)
+    new_count = bb->count;
+  if (!bb->pred)
+    abort ();
+#ifdef ENABLE_CHECKING
+  if (!can_duplicate_block_p (bb))
+    abort ();
+#endif
+
+  new_bb = cfg_hooks->duplicate_block (bb);
+
+  new_bb->loop_depth = bb->loop_depth;
+  new_bb->flags = bb->flags;
+  for (s = bb->succ; s; s = s->succ_next)
+    {
+      /* Since we are creating edges from a new block to successors
+        of another block (which therefore are known to be disjoint), there
+        is no need to actually check for duplicated edges.  */
+      n = unchecked_make_edge (new_bb, s->dest, s->flags);
+      n->probability = s->probability;
+      if (e && bb->count)
+       {
+         /* Take care for overflows!  */
+         n->count = s->count * (new_count * 10000 / bb->count) / 10000;
+         s->count -= n->count;
+       }
+      else
+       n->count = s->count;
+      n->aux = s->aux;
+    }
+
+  if (e)
+    {
+      new_bb->count = new_count;
+      bb->count -= new_count;
+
+      new_bb->frequency = EDGE_FREQUENCY (e);
+      bb->frequency -= EDGE_FREQUENCY (e);
+
+      redirect_edge_and_branch_force (e, new_bb);
+
+      if (bb->count < 0)
+       bb->count = 0;
+      if (bb->frequency < 0)
+       bb->frequency = 0;
+    }
+  else
+    {
+      new_bb->count = bb->count;
+      new_bb->frequency = bb->frequency;
+    }
+
+  new_bb->rbi->original = bb;
+  bb->rbi->copy = new_bb;
+
+  return new_bb;
+}
+
+/* Return 1 if BB ends with a call, possibly followed by some
+   instructions that must stay with the call, 0 otherwise.  */
+
+bool 
+block_ends_with_call_p (basic_block bb)
+{
+  if (!cfg_hooks->block_ends_with_call_p)
+    internal_error ("%s does not support block_ends_with_call_p", cfg_hooks->name);
+
+  return (cfg_hooks->block_ends_with_call_p) (bb);
+}
+
+/* Return 1 if BB ends with a conditional branch, 0 otherwise.  */
+
+bool 
+block_ends_with_condjump_p (basic_block bb)
+{
+  if (!cfg_hooks->block_ends_with_condjump_p)
+    internal_error ("%s does not support block_ends_with_condjump_p",
+                   cfg_hooks->name);
+
+  return (cfg_hooks->block_ends_with_condjump_p) (bb);
+}
+
+/* Add fake edges to the function exit for any non constant and non noreturn
+   calls, volatile inline assembly in the bitmap of blocks specified by
+   BLOCKS or to the whole CFG if BLOCKS is zero.  Return the number of blocks
+   that were split.
+
+   The goal is to expose cases in which entering a basic block does not imply
+   that all subsequent instructions must be executed.  */
+
+int
+flow_call_edges_add (sbitmap blocks)
+{
+  if (!cfg_hooks->flow_call_edges_add)
+    internal_error ("%s does not support flow_call_edges_add", 
+                   cfg_hooks->name);
+
+  return (cfg_hooks->flow_call_edges_add) (blocks);
+}