OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [gcc-4.5.1/] [gcc/] [graphite-scop-detection.c] - Diff between revs 816 and 826

Only display areas with differences | Details | Blame | View Log

Rev 816 Rev 826
/* Detection of Static Control Parts (SCoP) for Graphite.
/* Detection of Static Control Parts (SCoP) for Graphite.
   Copyright (C) 2009, 2010 Free Software Foundation, Inc.
   Copyright (C) 2009, 2010 Free Software Foundation, Inc.
   Contributed by Sebastian Pop <sebastian.pop@amd.com> and
   Contributed by Sebastian Pop <sebastian.pop@amd.com> and
   Tobias Grosser <grosser@fim.uni-passau.de>.
   Tobias Grosser <grosser@fim.uni-passau.de>.
 
 
This file is part of GCC.
This file is part of GCC.
 
 
GCC is free software; you can redistribute it and/or modify
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
the Free Software Foundation; either version 3, or (at your option)
any later version.
any later version.
 
 
GCC is distributed in the hope that it will be useful,
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
GNU General Public License for more details.
 
 
You should have received a copy of the GNU General Public License
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */
<http://www.gnu.org/licenses/>.  */
 
 
#include "config.h"
#include "config.h"
#include "system.h"
#include "system.h"
#include "coretypes.h"
#include "coretypes.h"
#include "tm.h"
#include "tm.h"
#include "ggc.h"
#include "ggc.h"
#include "tree.h"
#include "tree.h"
#include "rtl.h"
#include "rtl.h"
#include "basic-block.h"
#include "basic-block.h"
#include "diagnostic.h"
#include "diagnostic.h"
#include "tree-flow.h"
#include "tree-flow.h"
#include "toplev.h"
#include "toplev.h"
#include "tree-dump.h"
#include "tree-dump.h"
#include "timevar.h"
#include "timevar.h"
#include "cfgloop.h"
#include "cfgloop.h"
#include "tree-chrec.h"
#include "tree-chrec.h"
#include "tree-data-ref.h"
#include "tree-data-ref.h"
#include "tree-scalar-evolution.h"
#include "tree-scalar-evolution.h"
#include "tree-pass.h"
#include "tree-pass.h"
#include "domwalk.h"
#include "domwalk.h"
#include "value-prof.h"
#include "value-prof.h"
#include "pointer-set.h"
#include "pointer-set.h"
#include "gimple.h"
#include "gimple.h"
#include "sese.h"
#include "sese.h"
 
 
#ifdef HAVE_cloog
#ifdef HAVE_cloog
#include "cloog/cloog.h"
#include "cloog/cloog.h"
#include "ppl_c.h"
#include "ppl_c.h"
#include "graphite-ppl.h"
#include "graphite-ppl.h"
#include "graphite.h"
#include "graphite.h"
#include "graphite-poly.h"
#include "graphite-poly.h"
#include "graphite-scop-detection.h"
#include "graphite-scop-detection.h"
 
 
/* The type of the analyzed basic block.  */
/* The type of the analyzed basic block.  */
 
 
typedef enum gbb_type {
typedef enum gbb_type {
  GBB_UNKNOWN,
  GBB_UNKNOWN,
  GBB_LOOP_SING_EXIT_HEADER,
  GBB_LOOP_SING_EXIT_HEADER,
  GBB_LOOP_MULT_EXIT_HEADER,
  GBB_LOOP_MULT_EXIT_HEADER,
  GBB_LOOP_EXIT,
  GBB_LOOP_EXIT,
  GBB_COND_HEADER,
  GBB_COND_HEADER,
  GBB_SIMPLE,
  GBB_SIMPLE,
  GBB_LAST
  GBB_LAST
} gbb_type;
} gbb_type;
 
 
/* Detect the type of BB.  Loop headers are only marked, if they are
/* Detect the type of BB.  Loop headers are only marked, if they are
   new.  This means their loop_father is different to LAST_LOOP.
   new.  This means their loop_father is different to LAST_LOOP.
   Otherwise they are treated like any other bb and their type can be
   Otherwise they are treated like any other bb and their type can be
   any other type.  */
   any other type.  */
 
 
static gbb_type
static gbb_type
get_bb_type (basic_block bb, struct loop *last_loop)
get_bb_type (basic_block bb, struct loop *last_loop)
{
{
  VEC (basic_block, heap) *dom;
  VEC (basic_block, heap) *dom;
  int nb_dom, nb_suc;
  int nb_dom, nb_suc;
  struct loop *loop = bb->loop_father;
  struct loop *loop = bb->loop_father;
 
 
  /* Check, if we entry into a new loop. */
  /* Check, if we entry into a new loop. */
  if (loop != last_loop)
  if (loop != last_loop)
    {
    {
      if (single_exit (loop) != NULL)
      if (single_exit (loop) != NULL)
        return GBB_LOOP_SING_EXIT_HEADER;
        return GBB_LOOP_SING_EXIT_HEADER;
      else if (loop->num != 0)
      else if (loop->num != 0)
        return GBB_LOOP_MULT_EXIT_HEADER;
        return GBB_LOOP_MULT_EXIT_HEADER;
      else
      else
        return GBB_COND_HEADER;
        return GBB_COND_HEADER;
    }
    }
 
 
  dom = get_dominated_by (CDI_DOMINATORS, bb);
  dom = get_dominated_by (CDI_DOMINATORS, bb);
  nb_dom = VEC_length (basic_block, dom);
  nb_dom = VEC_length (basic_block, dom);
  VEC_free (basic_block, heap, dom);
  VEC_free (basic_block, heap, dom);
 
 
  if (nb_dom == 0)
  if (nb_dom == 0)
    return GBB_LAST;
    return GBB_LAST;
 
 
  nb_suc = VEC_length (edge, bb->succs);
  nb_suc = VEC_length (edge, bb->succs);
 
 
  if (nb_dom == 1 && nb_suc == 1)
  if (nb_dom == 1 && nb_suc == 1)
    return GBB_SIMPLE;
    return GBB_SIMPLE;
 
 
  return GBB_COND_HEADER;
  return GBB_COND_HEADER;
}
}
 
 
/* A SCoP detection region, defined using bbs as borders.
/* A SCoP detection region, defined using bbs as borders.
 
 
   All control flow touching this region, comes in passing basic_block
   All control flow touching this region, comes in passing basic_block
   ENTRY and leaves passing basic_block EXIT.  By using bbs instead of
   ENTRY and leaves passing basic_block EXIT.  By using bbs instead of
   edges for the borders we are able to represent also regions that do
   edges for the borders we are able to represent also regions that do
   not have a single entry or exit edge.
   not have a single entry or exit edge.
 
 
   But as they have a single entry basic_block and a single exit
   But as they have a single entry basic_block and a single exit
   basic_block, we are able to generate for every sd_region a single
   basic_block, we are able to generate for every sd_region a single
   entry and exit edge.
   entry and exit edge.
 
 
   1   2
   1   2
    \ /
    \ /
     3  <- entry
     3  <- entry
     |
     |
     4
     4
    / \                 This region contains: {3, 4, 5, 6, 7, 8}
    / \                 This region contains: {3, 4, 5, 6, 7, 8}
   5   6
   5   6
   |   |
   |   |
   7   8
   7   8
    \ /
    \ /
     9  <- exit  */
     9  <- exit  */
 
 
 
 
typedef struct sd_region_p
typedef struct sd_region_p
{
{
  /* The entry bb dominates all bbs in the sd_region.  It is part of
  /* The entry bb dominates all bbs in the sd_region.  It is part of
     the region.  */
     the region.  */
  basic_block entry;
  basic_block entry;
 
 
  /* The exit bb postdominates all bbs in the sd_region, but is not
  /* The exit bb postdominates all bbs in the sd_region, but is not
     part of the region.  */
     part of the region.  */
  basic_block exit;
  basic_block exit;
} sd_region;
} sd_region;
 
 
DEF_VEC_O(sd_region);
DEF_VEC_O(sd_region);
DEF_VEC_ALLOC_O(sd_region, heap);
DEF_VEC_ALLOC_O(sd_region, heap);
 
 
 
 
/* Moves the scops from SOURCE to TARGET and clean up SOURCE.  */
/* Moves the scops from SOURCE to TARGET and clean up SOURCE.  */
 
 
static void
static void
move_sd_regions (VEC (sd_region, heap) **source,
move_sd_regions (VEC (sd_region, heap) **source,
                 VEC (sd_region, heap) **target)
                 VEC (sd_region, heap) **target)
{
{
  sd_region *s;
  sd_region *s;
  int i;
  int i;
 
 
  for (i = 0; VEC_iterate (sd_region, *source, i, s); i++)
  for (i = 0; VEC_iterate (sd_region, *source, i, s); i++)
    VEC_safe_push (sd_region, heap, *target, s);
    VEC_safe_push (sd_region, heap, *target, s);
 
 
  VEC_free (sd_region, heap, *source);
  VEC_free (sd_region, heap, *source);
}
}
 
 
/* Something like "n * m" is not allowed.  */
/* Something like "n * m" is not allowed.  */
 
 
static bool
static bool
graphite_can_represent_init (tree e)
graphite_can_represent_init (tree e)
{
{
  switch (TREE_CODE (e))
  switch (TREE_CODE (e))
    {
    {
    case POLYNOMIAL_CHREC:
    case POLYNOMIAL_CHREC:
      return graphite_can_represent_init (CHREC_LEFT (e))
      return graphite_can_represent_init (CHREC_LEFT (e))
        && graphite_can_represent_init (CHREC_RIGHT (e));
        && graphite_can_represent_init (CHREC_RIGHT (e));
 
 
    case MULT_EXPR:
    case MULT_EXPR:
      if (chrec_contains_symbols (TREE_OPERAND (e, 0)))
      if (chrec_contains_symbols (TREE_OPERAND (e, 0)))
        return graphite_can_represent_init (TREE_OPERAND (e, 0))
        return graphite_can_represent_init (TREE_OPERAND (e, 0))
          && host_integerp (TREE_OPERAND (e, 1), 0);
          && host_integerp (TREE_OPERAND (e, 1), 0);
      else
      else
        return graphite_can_represent_init (TREE_OPERAND (e, 1))
        return graphite_can_represent_init (TREE_OPERAND (e, 1))
          && host_integerp (TREE_OPERAND (e, 0), 0);
          && host_integerp (TREE_OPERAND (e, 0), 0);
 
 
    case PLUS_EXPR:
    case PLUS_EXPR:
    case POINTER_PLUS_EXPR:
    case POINTER_PLUS_EXPR:
    case MINUS_EXPR:
    case MINUS_EXPR:
      return graphite_can_represent_init (TREE_OPERAND (e, 0))
      return graphite_can_represent_init (TREE_OPERAND (e, 0))
        && graphite_can_represent_init (TREE_OPERAND (e, 1));
        && graphite_can_represent_init (TREE_OPERAND (e, 1));
 
 
    case NEGATE_EXPR:
    case NEGATE_EXPR:
    case BIT_NOT_EXPR:
    case BIT_NOT_EXPR:
    CASE_CONVERT:
    CASE_CONVERT:
    case NON_LVALUE_EXPR:
    case NON_LVALUE_EXPR:
      return graphite_can_represent_init (TREE_OPERAND (e, 0));
      return graphite_can_represent_init (TREE_OPERAND (e, 0));
 
 
   default:
   default:
     break;
     break;
    }
    }
 
 
  return true;
  return true;
}
}
 
 
/* Return true when SCEV can be represented in the polyhedral model.
/* Return true when SCEV can be represented in the polyhedral model.
 
 
   An expression can be represented, if it can be expressed as an
   An expression can be represented, if it can be expressed as an
   affine expression.  For loops (i, j) and parameters (m, n) all
   affine expression.  For loops (i, j) and parameters (m, n) all
   affine expressions are of the form:
   affine expressions are of the form:
 
 
   x1 * i + x2 * j + x3 * m + x4 * n + x5 * 1 where x1..x5 element of Z
   x1 * i + x2 * j + x3 * m + x4 * n + x5 * 1 where x1..x5 element of Z
 
 
   1 i + 20 j + (-2) m + 25
   1 i + 20 j + (-2) m + 25
 
 
   Something like "i * n" or "n * m" is not allowed.
   Something like "i * n" or "n * m" is not allowed.
 
 
   OUTERMOST_LOOP defines the outermost loop that can variate.  */
   OUTERMOST_LOOP defines the outermost loop that can variate.  */
 
 
static bool
static bool
graphite_can_represent_scev (tree scev, int outermost_loop)
graphite_can_represent_scev (tree scev, int outermost_loop)
{
{
  if (chrec_contains_undetermined (scev))
  if (chrec_contains_undetermined (scev))
    return false;
    return false;
 
 
  switch (TREE_CODE (scev))
  switch (TREE_CODE (scev))
    {
    {
    case PLUS_EXPR:
    case PLUS_EXPR:
    case MINUS_EXPR:
    case MINUS_EXPR:
      return graphite_can_represent_scev (TREE_OPERAND (scev, 0), outermost_loop)
      return graphite_can_represent_scev (TREE_OPERAND (scev, 0), outermost_loop)
        && graphite_can_represent_scev (TREE_OPERAND (scev, 1), outermost_loop);
        && graphite_can_represent_scev (TREE_OPERAND (scev, 1), outermost_loop);
 
 
    case MULT_EXPR:
    case MULT_EXPR:
      return !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev, 0)))
      return !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev, 0)))
        && !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev, 1)))
        && !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev, 1)))
        && !(chrec_contains_symbols (TREE_OPERAND (scev, 0))
        && !(chrec_contains_symbols (TREE_OPERAND (scev, 0))
             && chrec_contains_symbols (TREE_OPERAND (scev, 1)))
             && chrec_contains_symbols (TREE_OPERAND (scev, 1)))
        && graphite_can_represent_init (scev)
        && graphite_can_represent_init (scev)
        && graphite_can_represent_scev (TREE_OPERAND (scev, 0), outermost_loop)
        && graphite_can_represent_scev (TREE_OPERAND (scev, 0), outermost_loop)
        && graphite_can_represent_scev (TREE_OPERAND (scev, 1), outermost_loop);
        && graphite_can_represent_scev (TREE_OPERAND (scev, 1), outermost_loop);
 
 
    case POLYNOMIAL_CHREC:
    case POLYNOMIAL_CHREC:
      /* Check for constant strides.  With a non constant stride of
      /* Check for constant strides.  With a non constant stride of
         'n' we would have a value of 'iv * n'.  Also check that the
         'n' we would have a value of 'iv * n'.  Also check that the
         initial value can represented: for example 'n * m' cannot be
         initial value can represented: for example 'n * m' cannot be
         represented.  */
         represented.  */
      if (!evolution_function_right_is_integer_cst (scev)
      if (!evolution_function_right_is_integer_cst (scev)
          || !graphite_can_represent_init (scev))
          || !graphite_can_represent_init (scev))
        return false;
        return false;
 
 
    default:
    default:
      break;
      break;
    }
    }
 
 
  /* Only affine functions can be represented.  */
  /* Only affine functions can be represented.  */
  if (!scev_is_linear_expression (scev))
  if (!scev_is_linear_expression (scev))
    return false;
    return false;
 
 
  return evolution_function_is_invariant_p (scev, outermost_loop)
  return evolution_function_is_invariant_p (scev, outermost_loop)
    || evolution_function_is_affine_multivariate_p (scev, outermost_loop);
    || evolution_function_is_affine_multivariate_p (scev, outermost_loop);
}
}
 
 
 
 
/* Return true when EXPR can be represented in the polyhedral model.
/* Return true when EXPR can be represented in the polyhedral model.
 
 
   This means an expression can be represented, if it is linear with
   This means an expression can be represented, if it is linear with
   respect to the loops and the strides are non parametric.
   respect to the loops and the strides are non parametric.
   LOOP is the place where the expr will be evaluated and OUTERMOST_LOOP
   LOOP is the place where the expr will be evaluated and OUTERMOST_LOOP
   defindes the outermost loop that can variate.  SCOP_ENTRY defines the
   defindes the outermost loop that can variate.  SCOP_ENTRY defines the
   entry of the region we analyse.  */
   entry of the region we analyse.  */
 
 
static bool
static bool
graphite_can_represent_expr (basic_block scop_entry, loop_p loop,
graphite_can_represent_expr (basic_block scop_entry, loop_p loop,
                             loop_p outermost_loop, tree expr)
                             loop_p outermost_loop, tree expr)
{
{
  tree scev = analyze_scalar_evolution (loop, expr);
  tree scev = analyze_scalar_evolution (loop, expr);
 
 
  scev = instantiate_scev (scop_entry, loop, scev);
  scev = instantiate_scev (scop_entry, loop, scev);
 
 
  return graphite_can_represent_scev (scev, outermost_loop->num);
  return graphite_can_represent_scev (scev, outermost_loop->num);
}
}
 
 
/* Return true if the data references of STMT can be represented by
/* Return true if the data references of STMT can be represented by
   Graphite.  */
   Graphite.  */
 
 
static bool
static bool
stmt_has_simple_data_refs_p (loop_p outermost_loop, gimple stmt)
stmt_has_simple_data_refs_p (loop_p outermost_loop, gimple stmt)
{
{
  data_reference_p dr;
  data_reference_p dr;
  unsigned i;
  unsigned i;
  int j;
  int j;
  bool res = true;
  bool res = true;
  int loop = outermost_loop->num;
  int loop = outermost_loop->num;
  VEC (data_reference_p, heap) *drs = VEC_alloc (data_reference_p, heap, 5);
  VEC (data_reference_p, heap) *drs = VEC_alloc (data_reference_p, heap, 5);
 
 
  graphite_find_data_references_in_stmt (outermost_loop, stmt, &drs);
  graphite_find_data_references_in_stmt (outermost_loop, stmt, &drs);
 
 
  for (j = 0; VEC_iterate (data_reference_p, drs, j, dr); j++)
  for (j = 0; VEC_iterate (data_reference_p, drs, j, dr); j++)
    for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
    for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
      if (!graphite_can_represent_scev (DR_ACCESS_FN (dr, i), loop))
      if (!graphite_can_represent_scev (DR_ACCESS_FN (dr, i), loop))
        {
        {
          res = false;
          res = false;
          goto done;
          goto done;
        }
        }
 
 
 done:
 done:
  free_data_refs (drs);
  free_data_refs (drs);
  return res;
  return res;
}
}
 
 
/* Return true only when STMT is simple enough for being handled by
/* Return true only when STMT is simple enough for being handled by
   Graphite.  This depends on SCOP_ENTRY, as the parameters are
   Graphite.  This depends on SCOP_ENTRY, as the parameters are
   initialized relatively to this basic block, the linear functions
   initialized relatively to this basic block, the linear functions
   are initialized to OUTERMOST_LOOP and BB is the place where we try
   are initialized to OUTERMOST_LOOP and BB is the place where we try
   to evaluate the STMT.  */
   to evaluate the STMT.  */
 
 
static bool
static bool
stmt_simple_for_scop_p (basic_block scop_entry, loop_p outermost_loop,
stmt_simple_for_scop_p (basic_block scop_entry, loop_p outermost_loop,
                        gimple stmt, basic_block bb)
                        gimple stmt, basic_block bb)
{
{
  loop_p loop = bb->loop_father;
  loop_p loop = bb->loop_father;
 
 
  gcc_assert (scop_entry);
  gcc_assert (scop_entry);
 
 
  /* GIMPLE_ASM and GIMPLE_CALL may embed arbitrary side effects.
  /* GIMPLE_ASM and GIMPLE_CALL may embed arbitrary side effects.
     Calls have side-effects, except those to const or pure
     Calls have side-effects, except those to const or pure
     functions.  */
     functions.  */
  if (gimple_has_volatile_ops (stmt)
  if (gimple_has_volatile_ops (stmt)
      || (gimple_code (stmt) == GIMPLE_CALL
      || (gimple_code (stmt) == GIMPLE_CALL
          && !(gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE)))
          && !(gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE)))
      || (gimple_code (stmt) == GIMPLE_ASM))
      || (gimple_code (stmt) == GIMPLE_ASM))
    return false;
    return false;
 
 
  if (is_gimple_debug (stmt))
  if (is_gimple_debug (stmt))
    return true;
    return true;
 
 
  if (!stmt_has_simple_data_refs_p (outermost_loop, stmt))
  if (!stmt_has_simple_data_refs_p (outermost_loop, stmt))
    return false;
    return false;
 
 
  switch (gimple_code (stmt))
  switch (gimple_code (stmt))
    {
    {
    case GIMPLE_RETURN:
    case GIMPLE_RETURN:
    case GIMPLE_LABEL:
    case GIMPLE_LABEL:
      return true;
      return true;
 
 
    case GIMPLE_COND:
    case GIMPLE_COND:
      {
      {
        tree op;
        tree op;
        ssa_op_iter op_iter;
        ssa_op_iter op_iter;
        enum tree_code code = gimple_cond_code (stmt);
        enum tree_code code = gimple_cond_code (stmt);
 
 
        /* We can handle all binary comparisons.  Inequalities are
        /* We can handle all binary comparisons.  Inequalities are
           also supported as they can be represented with union of
           also supported as they can be represented with union of
           polyhedra.  */
           polyhedra.  */
        if (!(code == LT_EXPR
        if (!(code == LT_EXPR
              || code == GT_EXPR
              || code == GT_EXPR
              || code == LE_EXPR
              || code == LE_EXPR
              || code == GE_EXPR
              || code == GE_EXPR
              || code == EQ_EXPR
              || code == EQ_EXPR
              || code == NE_EXPR))
              || code == NE_EXPR))
          return false;
          return false;
 
 
        FOR_EACH_SSA_TREE_OPERAND (op, stmt, op_iter, SSA_OP_ALL_USES)
        FOR_EACH_SSA_TREE_OPERAND (op, stmt, op_iter, SSA_OP_ALL_USES)
          if (!graphite_can_represent_expr (scop_entry, loop, outermost_loop,
          if (!graphite_can_represent_expr (scop_entry, loop, outermost_loop,
                                            op)
                                            op)
              /* We can not handle REAL_TYPE. Failed for pr39260.  */
              /* We can not handle REAL_TYPE. Failed for pr39260.  */
              || TREE_CODE (TREE_TYPE (op)) == REAL_TYPE)
              || TREE_CODE (TREE_TYPE (op)) == REAL_TYPE)
            return false;
            return false;
 
 
        return true;
        return true;
      }
      }
 
 
    case GIMPLE_ASSIGN:
    case GIMPLE_ASSIGN:
    case GIMPLE_CALL:
    case GIMPLE_CALL:
      return true;
      return true;
 
 
    default:
    default:
      /* These nodes cut a new scope.  */
      /* These nodes cut a new scope.  */
      return false;
      return false;
    }
    }
 
 
  return false;
  return false;
}
}
 
 
/* Returns the statement of BB that contains a harmful operation: that
/* Returns the statement of BB that contains a harmful operation: that
   can be a function call with side effects, the induction variables
   can be a function call with side effects, the induction variables
   are not linear with respect to SCOP_ENTRY, etc.  The current open
   are not linear with respect to SCOP_ENTRY, etc.  The current open
   scop should end before this statement.  The evaluation is limited using
   scop should end before this statement.  The evaluation is limited using
   OUTERMOST_LOOP as outermost loop that may change.  */
   OUTERMOST_LOOP as outermost loop that may change.  */
 
 
static gimple
static gimple
harmful_stmt_in_bb (basic_block scop_entry, loop_p outer_loop, basic_block bb)
harmful_stmt_in_bb (basic_block scop_entry, loop_p outer_loop, basic_block bb)
{
{
  gimple_stmt_iterator gsi;
  gimple_stmt_iterator gsi;
 
 
  for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    if (!stmt_simple_for_scop_p (scop_entry, outer_loop, gsi_stmt (gsi), bb))
    if (!stmt_simple_for_scop_p (scop_entry, outer_loop, gsi_stmt (gsi), bb))
      return gsi_stmt (gsi);
      return gsi_stmt (gsi);
 
 
  return NULL;
  return NULL;
}
}
 
 
/* Return true when it is not possible to represent LOOP in the
/* Return true when it is not possible to represent LOOP in the
   polyhedral representation.  This is evaluated taking SCOP_ENTRY and
   polyhedral representation.  This is evaluated taking SCOP_ENTRY and
   OUTERMOST_LOOP in mind.  */
   OUTERMOST_LOOP in mind.  */
 
 
static bool
static bool
graphite_can_represent_loop (basic_block scop_entry, loop_p outermost_loop,
graphite_can_represent_loop (basic_block scop_entry, loop_p outermost_loop,
                             loop_p loop)
                             loop_p loop)
{
{
  tree niter = number_of_latch_executions (loop);
  tree niter = number_of_latch_executions (loop);
 
 
  /* Number of iterations unknown.  */
  /* Number of iterations unknown.  */
  if (chrec_contains_undetermined (niter))
  if (chrec_contains_undetermined (niter))
    return false;
    return false;
 
 
  /* Number of iterations not affine.  */
  /* Number of iterations not affine.  */
  if (!graphite_can_represent_expr (scop_entry, loop, outermost_loop, niter))
  if (!graphite_can_represent_expr (scop_entry, loop, outermost_loop, niter))
    return false;
    return false;
 
 
  return true;
  return true;
}
}
 
 
/* Store information needed by scopdet_* functions.  */
/* Store information needed by scopdet_* functions.  */
 
 
struct scopdet_info
struct scopdet_info
{
{
  /* Exit of the open scop would stop if the current BB is harmful.  */
  /* Exit of the open scop would stop if the current BB is harmful.  */
  basic_block exit;
  basic_block exit;
 
 
  /* Where the next scop would start if the current BB is harmful.  */
  /* Where the next scop would start if the current BB is harmful.  */
  basic_block next;
  basic_block next;
 
 
  /* The bb or one of its children contains open loop exits.  That means
  /* The bb or one of its children contains open loop exits.  That means
     loop exit nodes that are not surrounded by a loop dominated by bb.  */
     loop exit nodes that are not surrounded by a loop dominated by bb.  */
  bool exits;
  bool exits;
 
 
  /* The bb or one of its children contains only structures we can handle.  */
  /* The bb or one of its children contains only structures we can handle.  */
  bool difficult;
  bool difficult;
};
};
 
 
static struct scopdet_info build_scops_1 (basic_block, loop_p,
static struct scopdet_info build_scops_1 (basic_block, loop_p,
                                          VEC (sd_region, heap) **, loop_p);
                                          VEC (sd_region, heap) **, loop_p);
 
 
/* Calculates BB infos. If bb is difficult we add valid SCoPs dominated by BB
/* Calculates BB infos. If bb is difficult we add valid SCoPs dominated by BB
   to SCOPS.  TYPE is the gbb_type of BB.  */
   to SCOPS.  TYPE is the gbb_type of BB.  */
 
 
static struct scopdet_info
static struct scopdet_info
scopdet_basic_block_info (basic_block bb, loop_p outermost_loop,
scopdet_basic_block_info (basic_block bb, loop_p outermost_loop,
                          VEC (sd_region, heap) **scops, gbb_type type)
                          VEC (sd_region, heap) **scops, gbb_type type)
{
{
  loop_p loop = bb->loop_father;
  loop_p loop = bb->loop_father;
  struct scopdet_info result;
  struct scopdet_info result;
  gimple stmt;
  gimple stmt;
 
 
  /* XXX: ENTRY_BLOCK_PTR could be optimized in later steps.  */
  /* XXX: ENTRY_BLOCK_PTR could be optimized in later steps.  */
  basic_block entry_block = ENTRY_BLOCK_PTR;
  basic_block entry_block = ENTRY_BLOCK_PTR;
  stmt = harmful_stmt_in_bb (entry_block, outermost_loop, bb);
  stmt = harmful_stmt_in_bb (entry_block, outermost_loop, bb);
  result.difficult = (stmt != NULL);
  result.difficult = (stmt != NULL);
  result.exit = NULL;
  result.exit = NULL;
 
 
  switch (type)
  switch (type)
    {
    {
    case GBB_LAST:
    case GBB_LAST:
      result.next = NULL;
      result.next = NULL;
      result.exits = false;
      result.exits = false;
 
 
      /* Mark bbs terminating a SESE region difficult, if they start
      /* Mark bbs terminating a SESE region difficult, if they start
         a condition.  */
         a condition.  */
      if (!single_succ_p (bb))
      if (!single_succ_p (bb))
        result.difficult = true;
        result.difficult = true;
      else
      else
        result.exit = single_succ (bb);
        result.exit = single_succ (bb);
 
 
      break;
      break;
 
 
    case GBB_SIMPLE:
    case GBB_SIMPLE:
      result.next = single_succ (bb);
      result.next = single_succ (bb);
      result.exits = false;
      result.exits = false;
      result.exit = single_succ (bb);
      result.exit = single_succ (bb);
      break;
      break;
 
 
    case GBB_LOOP_SING_EXIT_HEADER:
    case GBB_LOOP_SING_EXIT_HEADER:
      {
      {
        VEC (sd_region, heap) *regions = VEC_alloc (sd_region, heap, 3);
        VEC (sd_region, heap) *regions = VEC_alloc (sd_region, heap, 3);
        struct scopdet_info sinfo;
        struct scopdet_info sinfo;
        edge exit_e = single_exit (loop);
        edge exit_e = single_exit (loop);
 
 
        sinfo = build_scops_1 (bb, outermost_loop, &regions, loop);
        sinfo = build_scops_1 (bb, outermost_loop, &regions, loop);
 
 
        if (!graphite_can_represent_loop (entry_block, outermost_loop, loop))
        if (!graphite_can_represent_loop (entry_block, outermost_loop, loop))
          result.difficult = true;
          result.difficult = true;
 
 
        result.difficult |= sinfo.difficult;
        result.difficult |= sinfo.difficult;
 
 
        /* Try again with another loop level.  */
        /* Try again with another loop level.  */
        if (result.difficult
        if (result.difficult
            && loop_depth (outermost_loop) + 1 == loop_depth (loop))
            && loop_depth (outermost_loop) + 1 == loop_depth (loop))
          {
          {
            outermost_loop = loop;
            outermost_loop = loop;
 
 
            VEC_free (sd_region, heap, regions);
            VEC_free (sd_region, heap, regions);
            regions = VEC_alloc (sd_region, heap, 3);
            regions = VEC_alloc (sd_region, heap, 3);
 
 
            sinfo = scopdet_basic_block_info (bb, outermost_loop, scops, type);
            sinfo = scopdet_basic_block_info (bb, outermost_loop, scops, type);
 
 
            result = sinfo;
            result = sinfo;
            result.difficult = true;
            result.difficult = true;
 
 
            if (sinfo.difficult)
            if (sinfo.difficult)
              move_sd_regions (&regions, scops);
              move_sd_regions (&regions, scops);
            else
            else
              {
              {
                sd_region open_scop;
                sd_region open_scop;
                open_scop.entry = bb;
                open_scop.entry = bb;
                open_scop.exit = exit_e->dest;
                open_scop.exit = exit_e->dest;
                VEC_safe_push (sd_region, heap, *scops, &open_scop);
                VEC_safe_push (sd_region, heap, *scops, &open_scop);
                VEC_free (sd_region, heap, regions);
                VEC_free (sd_region, heap, regions);
              }
              }
          }
          }
        else
        else
          {
          {
            result.exit = exit_e->dest;
            result.exit = exit_e->dest;
            result.next = exit_e->dest;
            result.next = exit_e->dest;
 
 
            /* If we do not dominate result.next, remove it.  It's either
            /* If we do not dominate result.next, remove it.  It's either
               the EXIT_BLOCK_PTR, or another bb dominates it and will
               the EXIT_BLOCK_PTR, or another bb dominates it and will
               call the scop detection for this bb.  */
               call the scop detection for this bb.  */
            if (!dominated_by_p (CDI_DOMINATORS, result.next, bb))
            if (!dominated_by_p (CDI_DOMINATORS, result.next, bb))
              result.next = NULL;
              result.next = NULL;
 
 
            if (exit_e->src->loop_father != loop)
            if (exit_e->src->loop_father != loop)
              result.next = NULL;
              result.next = NULL;
 
 
            result.exits = false;
            result.exits = false;
 
 
            if (result.difficult)
            if (result.difficult)
              move_sd_regions (&regions, scops);
              move_sd_regions (&regions, scops);
            else
            else
              VEC_free (sd_region, heap, regions);
              VEC_free (sd_region, heap, regions);
          }
          }
 
 
        break;
        break;
      }
      }
 
 
    case GBB_LOOP_MULT_EXIT_HEADER:
    case GBB_LOOP_MULT_EXIT_HEADER:
      {
      {
        /* XXX: For now we just do not join loops with multiple exits.  If the
        /* XXX: For now we just do not join loops with multiple exits.  If the
           exits lead to the same bb it may be possible to join the loop.  */
           exits lead to the same bb it may be possible to join the loop.  */
        VEC (sd_region, heap) *regions = VEC_alloc (sd_region, heap, 3);
        VEC (sd_region, heap) *regions = VEC_alloc (sd_region, heap, 3);
        VEC (edge, heap) *exits = get_loop_exit_edges (loop);
        VEC (edge, heap) *exits = get_loop_exit_edges (loop);
        edge e;
        edge e;
        int i;
        int i;
        build_scops_1 (bb, loop, &regions, loop);
        build_scops_1 (bb, loop, &regions, loop);
 
 
        /* Scan the code dominated by this loop.  This means all bbs, that are
        /* Scan the code dominated by this loop.  This means all bbs, that are
           are dominated by a bb in this loop, but are not part of this loop.
           are dominated by a bb in this loop, but are not part of this loop.
 
 
           The easiest case:
           The easiest case:
             - The loop exit destination is dominated by the exit sources.
             - The loop exit destination is dominated by the exit sources.
 
 
           TODO: We miss here the more complex cases:
           TODO: We miss here the more complex cases:
                  - The exit destinations are dominated by another bb inside
                  - The exit destinations are dominated by another bb inside
                    the loop.
                    the loop.
                  - The loop dominates bbs, that are not exit destinations.  */
                  - The loop dominates bbs, that are not exit destinations.  */
        for (i = 0; VEC_iterate (edge, exits, i, e); i++)
        for (i = 0; VEC_iterate (edge, exits, i, e); i++)
          if (e->src->loop_father == loop
          if (e->src->loop_father == loop
              && dominated_by_p (CDI_DOMINATORS, e->dest, e->src))
              && dominated_by_p (CDI_DOMINATORS, e->dest, e->src))
            {
            {
              if (loop_outer (outermost_loop))
              if (loop_outer (outermost_loop))
                outermost_loop = loop_outer (outermost_loop);
                outermost_loop = loop_outer (outermost_loop);
 
 
              /* Pass loop_outer to recognize e->dest as loop header in
              /* Pass loop_outer to recognize e->dest as loop header in
                 build_scops_1.  */
                 build_scops_1.  */
              if (e->dest->loop_father->header == e->dest)
              if (e->dest->loop_father->header == e->dest)
                build_scops_1 (e->dest, outermost_loop, &regions,
                build_scops_1 (e->dest, outermost_loop, &regions,
                               loop_outer (e->dest->loop_father));
                               loop_outer (e->dest->loop_father));
              else
              else
                build_scops_1 (e->dest, outermost_loop, &regions,
                build_scops_1 (e->dest, outermost_loop, &regions,
                               e->dest->loop_father);
                               e->dest->loop_father);
            }
            }
 
 
        result.next = NULL;
        result.next = NULL;
        result.exit = NULL;
        result.exit = NULL;
        result.difficult = true;
        result.difficult = true;
        result.exits = false;
        result.exits = false;
        move_sd_regions (&regions, scops);
        move_sd_regions (&regions, scops);
        VEC_free (edge, heap, exits);
        VEC_free (edge, heap, exits);
        break;
        break;
      }
      }
    case GBB_COND_HEADER:
    case GBB_COND_HEADER:
      {
      {
        VEC (sd_region, heap) *regions = VEC_alloc (sd_region, heap, 3);
        VEC (sd_region, heap) *regions = VEC_alloc (sd_region, heap, 3);
        struct scopdet_info sinfo;
        struct scopdet_info sinfo;
        VEC (basic_block, heap) *dominated;
        VEC (basic_block, heap) *dominated;
        int i;
        int i;
        basic_block dom_bb;
        basic_block dom_bb;
        basic_block last_exit = NULL;
        basic_block last_exit = NULL;
        edge e;
        edge e;
        result.exits = false;
        result.exits = false;
 
 
        /* First check the successors of BB, and check if it is
        /* First check the successors of BB, and check if it is
           possible to join the different branches.  */
           possible to join the different branches.  */
        for (i = 0; VEC_iterate (edge, bb->succs, i, e); i++)
        for (i = 0; VEC_iterate (edge, bb->succs, i, e); i++)
          {
          {
            /* Ignore loop exits.  They will be handled after the loop
            /* Ignore loop exits.  They will be handled after the loop
               body.  */
               body.  */
            if (is_loop_exit (loop, e->dest))
            if (is_loop_exit (loop, e->dest))
              {
              {
                result.exits = true;
                result.exits = true;
                continue;
                continue;
              }
              }
 
 
            /* Do not follow edges that lead to the end of the
            /* Do not follow edges that lead to the end of the
               conditions block.  For example, in
               conditions block.  For example, in
 
 
               |   0
               |   0
               |  /|\
               |  /|\
               | 1 2 |
               | 1 2 |
               | | | |
               | | | |
               | 3 4 |
               | 3 4 |
               |  \|/
               |  \|/
               |   6
               |   6
 
 
               the edge from 0 => 6.  Only check if all paths lead to
               the edge from 0 => 6.  Only check if all paths lead to
               the same node 6.  */
               the same node 6.  */
 
 
            if (!single_pred_p (e->dest))
            if (!single_pred_p (e->dest))
              {
              {
                /* Check, if edge leads directly to the end of this
                /* Check, if edge leads directly to the end of this
                   condition.  */
                   condition.  */
                if (!last_exit)
                if (!last_exit)
                  last_exit = e->dest;
                  last_exit = e->dest;
 
 
                if (e->dest != last_exit)
                if (e->dest != last_exit)
                  result.difficult = true;
                  result.difficult = true;
 
 
                continue;
                continue;
              }
              }
 
 
            if (!dominated_by_p (CDI_DOMINATORS, e->dest, bb))
            if (!dominated_by_p (CDI_DOMINATORS, e->dest, bb))
              {
              {
                result.difficult = true;
                result.difficult = true;
                continue;
                continue;
              }
              }
 
 
            sinfo = build_scops_1 (e->dest, outermost_loop, &regions, loop);
            sinfo = build_scops_1 (e->dest, outermost_loop, &regions, loop);
 
 
            result.exits |= sinfo.exits;
            result.exits |= sinfo.exits;
            result.difficult |= sinfo.difficult;
            result.difficult |= sinfo.difficult;
 
 
            /* Checks, if all branches end at the same point.
            /* Checks, if all branches end at the same point.
               If that is true, the condition stays joinable.
               If that is true, the condition stays joinable.
               Have a look at the example above.  */
               Have a look at the example above.  */
            if (sinfo.exit)
            if (sinfo.exit)
              {
              {
                if (!last_exit)
                if (!last_exit)
                  last_exit = sinfo.exit;
                  last_exit = sinfo.exit;
 
 
                if (sinfo.exit != last_exit)
                if (sinfo.exit != last_exit)
                  result.difficult = true;
                  result.difficult = true;
              }
              }
            else
            else
              result.difficult = true;
              result.difficult = true;
          }
          }
 
 
        if (!last_exit)
        if (!last_exit)
          result.difficult = true;
          result.difficult = true;
 
 
        /* Join the branches of the condition if possible.  */
        /* Join the branches of the condition if possible.  */
        if (!result.exits && !result.difficult)
        if (!result.exits && !result.difficult)
          {
          {
            /* Only return a next pointer if we dominate this pointer.
            /* Only return a next pointer if we dominate this pointer.
               Otherwise it will be handled by the bb dominating it.  */
               Otherwise it will be handled by the bb dominating it.  */
            if (dominated_by_p (CDI_DOMINATORS, last_exit, bb)
            if (dominated_by_p (CDI_DOMINATORS, last_exit, bb)
                && last_exit != bb)
                && last_exit != bb)
              result.next = last_exit;
              result.next = last_exit;
            else
            else
              result.next = NULL;
              result.next = NULL;
 
 
            result.exit = last_exit;
            result.exit = last_exit;
 
 
            VEC_free (sd_region, heap, regions);
            VEC_free (sd_region, heap, regions);
            break;
            break;
          }
          }
 
 
        /* Scan remaining bbs dominated by BB.  */
        /* Scan remaining bbs dominated by BB.  */
        dominated = get_dominated_by (CDI_DOMINATORS, bb);
        dominated = get_dominated_by (CDI_DOMINATORS, bb);
 
 
        for (i = 0; VEC_iterate (basic_block, dominated, i, dom_bb); i++)
        for (i = 0; VEC_iterate (basic_block, dominated, i, dom_bb); i++)
          {
          {
            /* Ignore loop exits: they will be handled after the loop body.  */
            /* Ignore loop exits: they will be handled after the loop body.  */
            if (loop_depth (find_common_loop (loop, dom_bb->loop_father))
            if (loop_depth (find_common_loop (loop, dom_bb->loop_father))
                < loop_depth (loop))
                < loop_depth (loop))
              {
              {
                result.exits = true;
                result.exits = true;
                continue;
                continue;
              }
              }
 
 
            /* Ignore the bbs processed above.  */
            /* Ignore the bbs processed above.  */
            if (single_pred_p (dom_bb) && single_pred (dom_bb) == bb)
            if (single_pred_p (dom_bb) && single_pred (dom_bb) == bb)
              continue;
              continue;
 
 
            if (loop_depth (loop) > loop_depth (dom_bb->loop_father))
            if (loop_depth (loop) > loop_depth (dom_bb->loop_father))
              sinfo = build_scops_1 (dom_bb, outermost_loop, &regions,
              sinfo = build_scops_1 (dom_bb, outermost_loop, &regions,
                                     loop_outer (loop));
                                     loop_outer (loop));
            else
            else
              sinfo = build_scops_1 (dom_bb, outermost_loop, &regions, loop);
              sinfo = build_scops_1 (dom_bb, outermost_loop, &regions, loop);
 
 
            result.exits |= sinfo.exits;
            result.exits |= sinfo.exits;
            result.difficult = true;
            result.difficult = true;
            result.exit = NULL;
            result.exit = NULL;
          }
          }
 
 
        VEC_free (basic_block, heap, dominated);
        VEC_free (basic_block, heap, dominated);
 
 
        result.next = NULL;
        result.next = NULL;
        move_sd_regions (&regions, scops);
        move_sd_regions (&regions, scops);
 
 
        break;
        break;
      }
      }
 
 
    default:
    default:
      gcc_unreachable ();
      gcc_unreachable ();
    }
    }
 
 
  return result;
  return result;
}
}
 
 
/* Starting from CURRENT we walk the dominance tree and add new sd_regions to
/* Starting from CURRENT we walk the dominance tree and add new sd_regions to
   SCOPS. The analyse if a sd_region can be handled is based on the value
   SCOPS. The analyse if a sd_region can be handled is based on the value
   of OUTERMOST_LOOP. Only loops inside OUTERMOST loops may change.  LOOP
   of OUTERMOST_LOOP. Only loops inside OUTERMOST loops may change.  LOOP
   is the loop in which CURRENT is handled.
   is the loop in which CURRENT is handled.
 
 
   TODO: These functions got a little bit big. They definitely should be cleaned
   TODO: These functions got a little bit big. They definitely should be cleaned
         up.  */
         up.  */
 
 
static struct scopdet_info
static struct scopdet_info
build_scops_1 (basic_block current, loop_p outermost_loop,
build_scops_1 (basic_block current, loop_p outermost_loop,
               VEC (sd_region, heap) **scops, loop_p loop)
               VEC (sd_region, heap) **scops, loop_p loop)
{
{
  bool in_scop = false;
  bool in_scop = false;
  sd_region open_scop;
  sd_region open_scop;
  struct scopdet_info sinfo;
  struct scopdet_info sinfo;
 
 
  /* Initialize result.  */
  /* Initialize result.  */
  struct scopdet_info result;
  struct scopdet_info result;
  result.exits = false;
  result.exits = false;
  result.difficult = false;
  result.difficult = false;
  result.next = NULL;
  result.next = NULL;
  result.exit = NULL;
  result.exit = NULL;
  open_scop.entry = NULL;
  open_scop.entry = NULL;
  open_scop.exit = NULL;
  open_scop.exit = NULL;
  sinfo.exit = NULL;
  sinfo.exit = NULL;
 
 
  /* Loop over the dominance tree.  If we meet a difficult bb, close
  /* Loop over the dominance tree.  If we meet a difficult bb, close
     the current SCoP.  Loop and condition header start a new layer,
     the current SCoP.  Loop and condition header start a new layer,
     and can only be added if all bbs in deeper layers are simple.  */
     and can only be added if all bbs in deeper layers are simple.  */
  while (current != NULL)
  while (current != NULL)
    {
    {
      sinfo = scopdet_basic_block_info (current, outermost_loop, scops,
      sinfo = scopdet_basic_block_info (current, outermost_loop, scops,
                                        get_bb_type (current, loop));
                                        get_bb_type (current, loop));
 
 
      if (!in_scop && !(sinfo.exits || sinfo.difficult))
      if (!in_scop && !(sinfo.exits || sinfo.difficult))
        {
        {
          open_scop.entry = current;
          open_scop.entry = current;
          open_scop.exit = NULL;
          open_scop.exit = NULL;
          in_scop = true;
          in_scop = true;
        }
        }
      else if (in_scop && (sinfo.exits || sinfo.difficult))
      else if (in_scop && (sinfo.exits || sinfo.difficult))
        {
        {
          open_scop.exit = current;
          open_scop.exit = current;
          VEC_safe_push (sd_region, heap, *scops, &open_scop);
          VEC_safe_push (sd_region, heap, *scops, &open_scop);
          in_scop = false;
          in_scop = false;
        }
        }
 
 
      result.difficult |= sinfo.difficult;
      result.difficult |= sinfo.difficult;
      result.exits |= sinfo.exits;
      result.exits |= sinfo.exits;
 
 
      current = sinfo.next;
      current = sinfo.next;
    }
    }
 
 
  /* Try to close open_scop, if we are still in an open SCoP.  */
  /* Try to close open_scop, if we are still in an open SCoP.  */
  if (in_scop)
  if (in_scop)
    {
    {
      open_scop.exit = sinfo.exit;
      open_scop.exit = sinfo.exit;
      gcc_assert (open_scop.exit);
      gcc_assert (open_scop.exit);
      VEC_safe_push (sd_region, heap, *scops, &open_scop);
      VEC_safe_push (sd_region, heap, *scops, &open_scop);
    }
    }
 
 
  result.exit = sinfo.exit;
  result.exit = sinfo.exit;
  return result;
  return result;
}
}
 
 
/* Checks if a bb is contained in REGION.  */
/* Checks if a bb is contained in REGION.  */
 
 
static bool
static bool
bb_in_sd_region (basic_block bb, sd_region *region)
bb_in_sd_region (basic_block bb, sd_region *region)
{
{
  return bb_in_region (bb, region->entry, region->exit);
  return bb_in_region (bb, region->entry, region->exit);
}
}
 
 
/* Returns the single entry edge of REGION, if it does not exits NULL.  */
/* Returns the single entry edge of REGION, if it does not exits NULL.  */
 
 
static edge
static edge
find_single_entry_edge (sd_region *region)
find_single_entry_edge (sd_region *region)
{
{
  edge e;
  edge e;
  edge_iterator ei;
  edge_iterator ei;
  edge entry = NULL;
  edge entry = NULL;
 
 
  FOR_EACH_EDGE (e, ei, region->entry->preds)
  FOR_EACH_EDGE (e, ei, region->entry->preds)
    if (!bb_in_sd_region (e->src, region))
    if (!bb_in_sd_region (e->src, region))
      {
      {
        if (entry)
        if (entry)
          {
          {
            entry = NULL;
            entry = NULL;
            break;
            break;
          }
          }
 
 
        else
        else
          entry = e;
          entry = e;
      }
      }
 
 
  return entry;
  return entry;
}
}
 
 
/* Returns the single exit edge of REGION, if it does not exits NULL.  */
/* Returns the single exit edge of REGION, if it does not exits NULL.  */
 
 
static edge
static edge
find_single_exit_edge (sd_region *region)
find_single_exit_edge (sd_region *region)
{
{
  edge e;
  edge e;
  edge_iterator ei;
  edge_iterator ei;
  edge exit = NULL;
  edge exit = NULL;
 
 
  FOR_EACH_EDGE (e, ei, region->exit->preds)
  FOR_EACH_EDGE (e, ei, region->exit->preds)
    if (bb_in_sd_region (e->src, region))
    if (bb_in_sd_region (e->src, region))
      {
      {
        if (exit)
        if (exit)
          {
          {
            exit = NULL;
            exit = NULL;
            break;
            break;
          }
          }
 
 
        else
        else
          exit = e;
          exit = e;
      }
      }
 
 
  return exit;
  return exit;
}
}
 
 
/* Create a single entry edge for REGION.  */
/* Create a single entry edge for REGION.  */
 
 
static void
static void
create_single_entry_edge (sd_region *region)
create_single_entry_edge (sd_region *region)
{
{
  if (find_single_entry_edge (region))
  if (find_single_entry_edge (region))
    return;
    return;
 
 
  /* There are multiple predecessors for bb_3
  /* There are multiple predecessors for bb_3
 
 
  |  1  2
  |  1  2
  |  | /
  |  | /
  |  |/
  |  |/
  |  3  <- entry
  |  3  <- entry
  |  |\
  |  |\
  |  | |
  |  | |
  |  4 ^
  |  4 ^
  |  | |
  |  | |
  |  |/
  |  |/
  |  5
  |  5
 
 
  There are two edges (1->3, 2->3), that point from outside into the region,
  There are two edges (1->3, 2->3), that point from outside into the region,
  and another one (5->3), a loop latch, lead to bb_3.
  and another one (5->3), a loop latch, lead to bb_3.
 
 
  We split bb_3.
  We split bb_3.
 
 
  |  1  2
  |  1  2
  |  | /
  |  | /
  |  |/
  |  |/
  |3.0
  |3.0
  |  |\     (3.0 -> 3.1) = single entry edge
  |  |\     (3.0 -> 3.1) = single entry edge
  |3.1 |        <- entry
  |3.1 |        <- entry
  |  | |
  |  | |
  |  | |
  |  | |
  |  4 ^
  |  4 ^
  |  | |
  |  | |
  |  |/
  |  |/
  |  5
  |  5
 
 
  If the loop is part of the SCoP, we have to redirect the loop latches.
  If the loop is part of the SCoP, we have to redirect the loop latches.
 
 
  |  1  2
  |  1  2
  |  | /
  |  | /
  |  |/
  |  |/
  |3.0
  |3.0
  |  |      (3.0 -> 3.1) = entry edge
  |  |      (3.0 -> 3.1) = entry edge
  |3.1          <- entry
  |3.1          <- entry
  |  |\
  |  |\
  |  | |
  |  | |
  |  4 ^
  |  4 ^
  |  | |
  |  | |
  |  |/
  |  |/
  |  5  */
  |  5  */
 
 
  if (region->entry->loop_father->header != region->entry
  if (region->entry->loop_father->header != region->entry
      || dominated_by_p (CDI_DOMINATORS,
      || dominated_by_p (CDI_DOMINATORS,
                         loop_latch_edge (region->entry->loop_father)->src,
                         loop_latch_edge (region->entry->loop_father)->src,
                         region->exit))
                         region->exit))
    {
    {
      edge forwarder = split_block_after_labels (region->entry);
      edge forwarder = split_block_after_labels (region->entry);
      region->entry = forwarder->dest;
      region->entry = forwarder->dest;
    }
    }
  else
  else
    /* This case is never executed, as the loop headers seem always to have a
    /* This case is never executed, as the loop headers seem always to have a
       single edge pointing from outside into the loop.  */
       single edge pointing from outside into the loop.  */
    gcc_unreachable ();
    gcc_unreachable ();
 
 
#ifdef ENABLE_CHECKING
#ifdef ENABLE_CHECKING
  gcc_assert (find_single_entry_edge (region));
  gcc_assert (find_single_entry_edge (region));
#endif
#endif
}
}
 
 
/* Check if the sd_region, mentioned in EDGE, has no exit bb.  */
/* Check if the sd_region, mentioned in EDGE, has no exit bb.  */
 
 
static bool
static bool
sd_region_without_exit (edge e)
sd_region_without_exit (edge e)
{
{
  sd_region *r = (sd_region *) e->aux;
  sd_region *r = (sd_region *) e->aux;
 
 
  if (r)
  if (r)
    return r->exit == NULL;
    return r->exit == NULL;
  else
  else
    return false;
    return false;
}
}
 
 
/* Create a single exit edge for REGION.  */
/* Create a single exit edge for REGION.  */
 
 
static void
static void
create_single_exit_edge (sd_region *region)
create_single_exit_edge (sd_region *region)
{
{
  edge e;
  edge e;
  edge_iterator ei;
  edge_iterator ei;
  edge forwarder = NULL;
  edge forwarder = NULL;
  basic_block exit;
  basic_block exit;
 
 
  /* We create a forwarder bb (5) for all edges leaving this region
  /* We create a forwarder bb (5) for all edges leaving this region
     (3->5, 4->5).  All other edges leading to the same bb, are moved
     (3->5, 4->5).  All other edges leading to the same bb, are moved
     to a new bb (6).  If these edges where part of another region (2->5)
     to a new bb (6).  If these edges where part of another region (2->5)
     we update the region->exit pointer, of this region.
     we update the region->exit pointer, of this region.
 
 
     To identify which edge belongs to which region we depend on the e->aux
     To identify which edge belongs to which region we depend on the e->aux
     pointer in every edge.  It points to the region of the edge or to NULL,
     pointer in every edge.  It points to the region of the edge or to NULL,
     if the edge is not part of any region.
     if the edge is not part of any region.
 
 
     1 2 3 4    1->5 no region,                 2->5 region->exit = 5,
     1 2 3 4    1->5 no region,                 2->5 region->exit = 5,
      \| |/     3->5 region->exit = NULL,       4->5 region->exit = NULL
      \| |/     3->5 region->exit = NULL,       4->5 region->exit = NULL
        5       <- exit
        5       <- exit
 
 
     changes to
     changes to
 
 
     1 2 3 4    1->6 no region,                         2->6 region->exit = 6,
     1 2 3 4    1->6 no region,                         2->6 region->exit = 6,
     | | \/     3->5 no region,                         4->5 no region,
     | | \/     3->5 no region,                         4->5 no region,
     | |  5
     | |  5
      \| /      5->6 region->exit = 6
      \| /      5->6 region->exit = 6
        6
        6
 
 
     Now there is only a single exit edge (5->6).  */
     Now there is only a single exit edge (5->6).  */
  exit = region->exit;
  exit = region->exit;
  region->exit = NULL;
  region->exit = NULL;
  forwarder = make_forwarder_block (exit, &sd_region_without_exit, NULL);
  forwarder = make_forwarder_block (exit, &sd_region_without_exit, NULL);
 
 
  /* Unmark the edges, that are no longer exit edges.  */
  /* Unmark the edges, that are no longer exit edges.  */
  FOR_EACH_EDGE (e, ei, forwarder->src->preds)
  FOR_EACH_EDGE (e, ei, forwarder->src->preds)
    if (e->aux)
    if (e->aux)
      e->aux = NULL;
      e->aux = NULL;
 
 
  /* Mark the new exit edge.  */
  /* Mark the new exit edge.  */
  single_succ_edge (forwarder->src)->aux = region;
  single_succ_edge (forwarder->src)->aux = region;
 
 
  /* Update the exit bb of all regions, where exit edges lead to
  /* Update the exit bb of all regions, where exit edges lead to
     forwarder->dest.  */
     forwarder->dest.  */
  FOR_EACH_EDGE (e, ei, forwarder->dest->preds)
  FOR_EACH_EDGE (e, ei, forwarder->dest->preds)
    if (e->aux)
    if (e->aux)
      ((sd_region *) e->aux)->exit = forwarder->dest;
      ((sd_region *) e->aux)->exit = forwarder->dest;
 
 
#ifdef ENABLE_CHECKING
#ifdef ENABLE_CHECKING
  gcc_assert (find_single_exit_edge (region));
  gcc_assert (find_single_exit_edge (region));
#endif
#endif
}
}
 
 
/* Unmark the exit edges of all REGIONS.
/* Unmark the exit edges of all REGIONS.
   See comment in "create_single_exit_edge". */
   See comment in "create_single_exit_edge". */
 
 
static void
static void
unmark_exit_edges (VEC (sd_region, heap) *regions)
unmark_exit_edges (VEC (sd_region, heap) *regions)
{
{
  int i;
  int i;
  sd_region *s;
  sd_region *s;
  edge e;
  edge e;
  edge_iterator ei;
  edge_iterator ei;
 
 
  for (i = 0; VEC_iterate (sd_region, regions, i, s); i++)
  for (i = 0; VEC_iterate (sd_region, regions, i, s); i++)
    FOR_EACH_EDGE (e, ei, s->exit->preds)
    FOR_EACH_EDGE (e, ei, s->exit->preds)
      e->aux = NULL;
      e->aux = NULL;
}
}
 
 
 
 
/* Mark the exit edges of all REGIONS.
/* Mark the exit edges of all REGIONS.
   See comment in "create_single_exit_edge". */
   See comment in "create_single_exit_edge". */
 
 
static void
static void
mark_exit_edges (VEC (sd_region, heap) *regions)
mark_exit_edges (VEC (sd_region, heap) *regions)
{
{
  int i;
  int i;
  sd_region *s;
  sd_region *s;
  edge e;
  edge e;
  edge_iterator ei;
  edge_iterator ei;
 
 
  for (i = 0; VEC_iterate (sd_region, regions, i, s); i++)
  for (i = 0; VEC_iterate (sd_region, regions, i, s); i++)
    FOR_EACH_EDGE (e, ei, s->exit->preds)
    FOR_EACH_EDGE (e, ei, s->exit->preds)
      if (bb_in_sd_region (e->src, s))
      if (bb_in_sd_region (e->src, s))
        e->aux = s;
        e->aux = s;
}
}
 
 
/* Create for all scop regions a single entry and a single exit edge.  */
/* Create for all scop regions a single entry and a single exit edge.  */
 
 
static void
static void
create_sese_edges (VEC (sd_region, heap) *regions)
create_sese_edges (VEC (sd_region, heap) *regions)
{
{
  int i;
  int i;
  sd_region *s;
  sd_region *s;
 
 
  for (i = 0; VEC_iterate (sd_region, regions, i, s); i++)
  for (i = 0; VEC_iterate (sd_region, regions, i, s); i++)
    create_single_entry_edge (s);
    create_single_entry_edge (s);
 
 
  mark_exit_edges (regions);
  mark_exit_edges (regions);
 
 
  for (i = 0; VEC_iterate (sd_region, regions, i, s); i++)
  for (i = 0; VEC_iterate (sd_region, regions, i, s); i++)
    /* Don't handle multiple edges exiting the function.  */
    /* Don't handle multiple edges exiting the function.  */
    if (!find_single_exit_edge (s)
    if (!find_single_exit_edge (s)
        && s->exit != EXIT_BLOCK_PTR)
        && s->exit != EXIT_BLOCK_PTR)
      create_single_exit_edge (s);
      create_single_exit_edge (s);
 
 
  unmark_exit_edges (regions);
  unmark_exit_edges (regions);
 
 
  fix_loop_structure (NULL);
  fix_loop_structure (NULL);
 
 
#ifdef ENABLE_CHECKING
#ifdef ENABLE_CHECKING
  verify_loop_structure ();
  verify_loop_structure ();
  verify_dominators (CDI_DOMINATORS);
  verify_dominators (CDI_DOMINATORS);
  verify_ssa (false);
  verify_ssa (false);
#endif
#endif
}
}
 
 
/* Create graphite SCoPs from an array of scop detection REGIONS.  */
/* Create graphite SCoPs from an array of scop detection REGIONS.  */
 
 
static void
static void
build_graphite_scops (VEC (sd_region, heap) *regions,
build_graphite_scops (VEC (sd_region, heap) *regions,
                      VEC (scop_p, heap) **scops)
                      VEC (scop_p, heap) **scops)
{
{
  int i;
  int i;
  sd_region *s;
  sd_region *s;
 
 
  for (i = 0; VEC_iterate (sd_region, regions, i, s); i++)
  for (i = 0; VEC_iterate (sd_region, regions, i, s); i++)
    {
    {
      edge entry = find_single_entry_edge (s);
      edge entry = find_single_entry_edge (s);
      edge exit = find_single_exit_edge (s);
      edge exit = find_single_exit_edge (s);
      scop_p scop;
      scop_p scop;
 
 
      if (!exit)
      if (!exit)
        continue;
        continue;
 
 
      scop = new_scop (new_sese (entry, exit));
      scop = new_scop (new_sese (entry, exit));
      VEC_safe_push (scop_p, heap, *scops, scop);
      VEC_safe_push (scop_p, heap, *scops, scop);
 
 
      /* Are there overlapping SCoPs?  */
      /* Are there overlapping SCoPs?  */
#ifdef ENABLE_CHECKING
#ifdef ENABLE_CHECKING
        {
        {
          int j;
          int j;
          sd_region *s2;
          sd_region *s2;
 
 
          for (j = 0; VEC_iterate (sd_region, regions, j, s2); j++)
          for (j = 0; VEC_iterate (sd_region, regions, j, s2); j++)
            if (s != s2)
            if (s != s2)
              gcc_assert (!bb_in_sd_region (s->entry, s2));
              gcc_assert (!bb_in_sd_region (s->entry, s2));
        }
        }
#endif
#endif
    }
    }
}
}
 
 
/* Returns true when BB contains only close phi nodes.  */
/* Returns true when BB contains only close phi nodes.  */
 
 
static bool
static bool
contains_only_close_phi_nodes (basic_block bb)
contains_only_close_phi_nodes (basic_block bb)
{
{
  gimple_stmt_iterator gsi;
  gimple_stmt_iterator gsi;
 
 
  for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    if (gimple_code (gsi_stmt (gsi)) != GIMPLE_LABEL)
    if (gimple_code (gsi_stmt (gsi)) != GIMPLE_LABEL)
      return false;
      return false;
 
 
  return true;
  return true;
}
}
 
 
/* Print statistics for SCOP to FILE.  */
/* Print statistics for SCOP to FILE.  */
 
 
static void
static void
print_graphite_scop_statistics (FILE* file, scop_p scop)
print_graphite_scop_statistics (FILE* file, scop_p scop)
{
{
  long n_bbs = 0;
  long n_bbs = 0;
  long n_loops = 0;
  long n_loops = 0;
  long n_stmts = 0;
  long n_stmts = 0;
  long n_conditions = 0;
  long n_conditions = 0;
  long n_p_bbs = 0;
  long n_p_bbs = 0;
  long n_p_loops = 0;
  long n_p_loops = 0;
  long n_p_stmts = 0;
  long n_p_stmts = 0;
  long n_p_conditions = 0;
  long n_p_conditions = 0;
 
 
  basic_block bb;
  basic_block bb;
 
 
  FOR_ALL_BB (bb)
  FOR_ALL_BB (bb)
    {
    {
      gimple_stmt_iterator psi;
      gimple_stmt_iterator psi;
      loop_p loop = bb->loop_father;
      loop_p loop = bb->loop_father;
 
 
      if (!bb_in_sese_p (bb, SCOP_REGION (scop)))
      if (!bb_in_sese_p (bb, SCOP_REGION (scop)))
        continue;
        continue;
 
 
      n_bbs++;
      n_bbs++;
      n_p_bbs += bb->count;
      n_p_bbs += bb->count;
 
 
      if (VEC_length (edge, bb->succs) > 1)
      if (VEC_length (edge, bb->succs) > 1)
        {
        {
          n_conditions++;
          n_conditions++;
          n_p_conditions += bb->count;
          n_p_conditions += bb->count;
        }
        }
 
 
      for (psi = gsi_start_bb (bb); !gsi_end_p (psi); gsi_next (&psi))
      for (psi = gsi_start_bb (bb); !gsi_end_p (psi); gsi_next (&psi))
        {
        {
          n_stmts++;
          n_stmts++;
          n_p_stmts += bb->count;
          n_p_stmts += bb->count;
        }
        }
 
 
      if (loop->header == bb && loop_in_sese_p (loop, SCOP_REGION (scop)))
      if (loop->header == bb && loop_in_sese_p (loop, SCOP_REGION (scop)))
        {
        {
          n_loops++;
          n_loops++;
          n_p_loops += bb->count;
          n_p_loops += bb->count;
        }
        }
 
 
    }
    }
 
 
  fprintf (file, "\nBefore limit_scops SCoP statistics (");
  fprintf (file, "\nBefore limit_scops SCoP statistics (");
  fprintf (file, "BBS:%ld, ", n_bbs);
  fprintf (file, "BBS:%ld, ", n_bbs);
  fprintf (file, "LOOPS:%ld, ", n_loops);
  fprintf (file, "LOOPS:%ld, ", n_loops);
  fprintf (file, "CONDITIONS:%ld, ", n_conditions);
  fprintf (file, "CONDITIONS:%ld, ", n_conditions);
  fprintf (file, "STMTS:%ld)\n", n_stmts);
  fprintf (file, "STMTS:%ld)\n", n_stmts);
  fprintf (file, "\nBefore limit_scops SCoP profiling statistics (");
  fprintf (file, "\nBefore limit_scops SCoP profiling statistics (");
  fprintf (file, "BBS:%ld, ", n_p_bbs);
  fprintf (file, "BBS:%ld, ", n_p_bbs);
  fprintf (file, "LOOPS:%ld, ", n_p_loops);
  fprintf (file, "LOOPS:%ld, ", n_p_loops);
  fprintf (file, "CONDITIONS:%ld, ", n_p_conditions);
  fprintf (file, "CONDITIONS:%ld, ", n_p_conditions);
  fprintf (file, "STMTS:%ld)\n", n_p_stmts);
  fprintf (file, "STMTS:%ld)\n", n_p_stmts);
}
}
 
 
/* Print statistics for SCOPS to FILE.  */
/* Print statistics for SCOPS to FILE.  */
 
 
static void
static void
print_graphite_statistics (FILE* file, VEC (scop_p, heap) *scops)
print_graphite_statistics (FILE* file, VEC (scop_p, heap) *scops)
{
{
  int i;
  int i;
  scop_p scop;
  scop_p scop;
 
 
  for (i = 0; VEC_iterate (scop_p, scops, i, scop); i++)
  for (i = 0; VEC_iterate (scop_p, scops, i, scop); i++)
    print_graphite_scop_statistics (file, scop);
    print_graphite_scop_statistics (file, scop);
}
}
 
 
/* We limit all SCoPs to SCoPs, that are completely surrounded by a loop.
/* We limit all SCoPs to SCoPs, that are completely surrounded by a loop.
 
 
   Example:
   Example:
 
 
   for (i      |
   for (i      |
     {         |
     {         |
       for (j  |  SCoP 1
       for (j  |  SCoP 1
       for (k  |
       for (k  |
     }         |
     }         |
 
 
   * SCoP frontier, as this line is not surrounded by any loop. *
   * SCoP frontier, as this line is not surrounded by any loop. *
 
 
   for (l      |  SCoP 2
   for (l      |  SCoP 2
 
 
   This is necessary as scalar evolution and parameter detection need a
   This is necessary as scalar evolution and parameter detection need a
   outermost loop to initialize parameters correctly.
   outermost loop to initialize parameters correctly.
 
 
   TODO: FIX scalar evolution and parameter detection to allow more flexible
   TODO: FIX scalar evolution and parameter detection to allow more flexible
         SCoP frontiers.  */
         SCoP frontiers.  */
 
 
static void
static void
limit_scops (VEC (scop_p, heap) **scops)
limit_scops (VEC (scop_p, heap) **scops)
{
{
  VEC (sd_region, heap) *regions = VEC_alloc (sd_region, heap, 3);
  VEC (sd_region, heap) *regions = VEC_alloc (sd_region, heap, 3);
 
 
  int i;
  int i;
  scop_p scop;
  scop_p scop;
 
 
  for (i = 0; VEC_iterate (scop_p, *scops, i, scop); i++)
  for (i = 0; VEC_iterate (scop_p, *scops, i, scop); i++)
    {
    {
      int j;
      int j;
      loop_p loop;
      loop_p loop;
      sese region = SCOP_REGION (scop);
      sese region = SCOP_REGION (scop);
      build_sese_loop_nests (region);
      build_sese_loop_nests (region);
 
 
      for (j = 0; VEC_iterate (loop_p, SESE_LOOP_NEST (region), j, loop); j++)
      for (j = 0; VEC_iterate (loop_p, SESE_LOOP_NEST (region), j, loop); j++)
        if (!loop_in_sese_p (loop_outer (loop), region)
        if (!loop_in_sese_p (loop_outer (loop), region)
            && single_exit (loop))
            && single_exit (loop))
          {
          {
            sd_region open_scop;
            sd_region open_scop;
            open_scop.entry = loop->header;
            open_scop.entry = loop->header;
            open_scop.exit = single_exit (loop)->dest;
            open_scop.exit = single_exit (loop)->dest;
 
 
            /* This is a hack on top of the limit_scops hack.  The
            /* This is a hack on top of the limit_scops hack.  The
               limit_scops hack should disappear all together.  */
               limit_scops hack should disappear all together.  */
            if (single_succ_p (open_scop.exit)
            if (single_succ_p (open_scop.exit)
                && contains_only_close_phi_nodes (open_scop.exit))
                && contains_only_close_phi_nodes (open_scop.exit))
              open_scop.exit = single_succ_edge (open_scop.exit)->dest;
              open_scop.exit = single_succ_edge (open_scop.exit)->dest;
 
 
            VEC_safe_push (sd_region, heap, regions, &open_scop);
            VEC_safe_push (sd_region, heap, regions, &open_scop);
          }
          }
    }
    }
 
 
  free_scops (*scops);
  free_scops (*scops);
  *scops = VEC_alloc (scop_p, heap, 3);
  *scops = VEC_alloc (scop_p, heap, 3);
 
 
  create_sese_edges (regions);
  create_sese_edges (regions);
  build_graphite_scops (regions, scops);
  build_graphite_scops (regions, scops);
  VEC_free (sd_region, heap, regions);
  VEC_free (sd_region, heap, regions);
}
}
 
 
/* Transforms LOOP to the canonical loop closed SSA form.  */
/* Transforms LOOP to the canonical loop closed SSA form.  */
 
 
static void
static void
canonicalize_loop_closed_ssa (loop_p loop)
canonicalize_loop_closed_ssa (loop_p loop)
{
{
  edge e = single_exit (loop);
  edge e = single_exit (loop);
  basic_block bb;
  basic_block bb;
 
 
  if (!e || e->flags & EDGE_ABNORMAL)
  if (!e || e->flags & EDGE_ABNORMAL)
    return;
    return;
 
 
  bb = e->dest;
  bb = e->dest;
 
 
  if (VEC_length (edge, bb->preds) == 1)
  if (VEC_length (edge, bb->preds) == 1)
    split_block_after_labels (bb);
    split_block_after_labels (bb);
  else
  else
    {
    {
      gimple_stmt_iterator psi;
      gimple_stmt_iterator psi;
      basic_block close = split_edge (e);
      basic_block close = split_edge (e);
 
 
      e = single_succ_edge (close);
      e = single_succ_edge (close);
 
 
      for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
      for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
        {
        {
          gimple phi = gsi_stmt (psi);
          gimple phi = gsi_stmt (psi);
          unsigned i;
          unsigned i;
 
 
          for (i = 0; i < gimple_phi_num_args (phi); i++)
          for (i = 0; i < gimple_phi_num_args (phi); i++)
            if (gimple_phi_arg_edge (phi, i) == e)
            if (gimple_phi_arg_edge (phi, i) == e)
              {
              {
                tree res, arg = gimple_phi_arg_def (phi, i);
                tree res, arg = gimple_phi_arg_def (phi, i);
                use_operand_p use_p;
                use_operand_p use_p;
                gimple close_phi;
                gimple close_phi;
 
 
                if (TREE_CODE (arg) != SSA_NAME)
                if (TREE_CODE (arg) != SSA_NAME)
                  continue;
                  continue;
 
 
                close_phi = create_phi_node (arg, close);
                close_phi = create_phi_node (arg, close);
                res = create_new_def_for (gimple_phi_result (close_phi),
                res = create_new_def_for (gimple_phi_result (close_phi),
                                          close_phi,
                                          close_phi,
                                          gimple_phi_result_ptr (close_phi));
                                          gimple_phi_result_ptr (close_phi));
                add_phi_arg (close_phi, arg,
                add_phi_arg (close_phi, arg,
                             gimple_phi_arg_edge (close_phi, 0),
                             gimple_phi_arg_edge (close_phi, 0),
                             UNKNOWN_LOCATION);
                             UNKNOWN_LOCATION);
                use_p = gimple_phi_arg_imm_use_ptr (phi, i);
                use_p = gimple_phi_arg_imm_use_ptr (phi, i);
                replace_exp (use_p, res);
                replace_exp (use_p, res);
                update_stmt (phi);
                update_stmt (phi);
              }
              }
        }
        }
    }
    }
}
}
 
 
/* Converts the current loop closed SSA form to a canonical form
/* Converts the current loop closed SSA form to a canonical form
   expected by the Graphite code generation.
   expected by the Graphite code generation.
 
 
   The loop closed SSA form has the following invariant: a variable
   The loop closed SSA form has the following invariant: a variable
   defined in a loop that is used outside the loop appears only in the
   defined in a loop that is used outside the loop appears only in the
   phi nodes in the destination of the loop exit.  These phi nodes are
   phi nodes in the destination of the loop exit.  These phi nodes are
   called close phi nodes.
   called close phi nodes.
 
 
   The canonical loop closed SSA form contains the extra invariants:
   The canonical loop closed SSA form contains the extra invariants:
 
 
   - when the loop contains only one exit, the close phi nodes contain
   - when the loop contains only one exit, the close phi nodes contain
   only one argument.  That implies that the basic block that contains
   only one argument.  That implies that the basic block that contains
   the close phi nodes has only one predecessor, that is a basic block
   the close phi nodes has only one predecessor, that is a basic block
   in the loop.
   in the loop.
 
 
   - the basic block containing the close phi nodes does not contain
   - the basic block containing the close phi nodes does not contain
   other statements.
   other statements.
*/
*/
 
 
static void
static void
canonicalize_loop_closed_ssa_form (void)
canonicalize_loop_closed_ssa_form (void)
{
{
  loop_iterator li;
  loop_iterator li;
  loop_p loop;
  loop_p loop;
 
 
#ifdef ENABLE_CHECKING
#ifdef ENABLE_CHECKING
  verify_loop_closed_ssa ();
  verify_loop_closed_ssa ();
#endif
#endif
 
 
  FOR_EACH_LOOP (li, loop, 0)
  FOR_EACH_LOOP (li, loop, 0)
    canonicalize_loop_closed_ssa (loop);
    canonicalize_loop_closed_ssa (loop);
 
 
  rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
  rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
  update_ssa (TODO_update_ssa);
  update_ssa (TODO_update_ssa);
 
 
#ifdef ENABLE_CHECKING
#ifdef ENABLE_CHECKING
  verify_loop_closed_ssa ();
  verify_loop_closed_ssa ();
#endif
#endif
}
}
 
 
/* Find Static Control Parts (SCoP) in the current function and pushes
/* Find Static Control Parts (SCoP) in the current function and pushes
   them to SCOPS.  */
   them to SCOPS.  */
 
 
void
void
build_scops (VEC (scop_p, heap) **scops)
build_scops (VEC (scop_p, heap) **scops)
{
{
  struct loop *loop = current_loops->tree_root;
  struct loop *loop = current_loops->tree_root;
  VEC (sd_region, heap) *regions = VEC_alloc (sd_region, heap, 3);
  VEC (sd_region, heap) *regions = VEC_alloc (sd_region, heap, 3);
 
 
  canonicalize_loop_closed_ssa_form ();
  canonicalize_loop_closed_ssa_form ();
  build_scops_1 (single_succ (ENTRY_BLOCK_PTR), ENTRY_BLOCK_PTR->loop_father,
  build_scops_1 (single_succ (ENTRY_BLOCK_PTR), ENTRY_BLOCK_PTR->loop_father,
                 &regions, loop);
                 &regions, loop);
  create_sese_edges (regions);
  create_sese_edges (regions);
  build_graphite_scops (regions, scops);
  build_graphite_scops (regions, scops);
 
 
  if (dump_file && (dump_flags & TDF_DETAILS))
  if (dump_file && (dump_flags & TDF_DETAILS))
    print_graphite_statistics (dump_file, *scops);
    print_graphite_statistics (dump_file, *scops);
 
 
  limit_scops (scops);
  limit_scops (scops);
  VEC_free (sd_region, heap, regions);
  VEC_free (sd_region, heap, regions);
 
 
  if (dump_file && (dump_flags & TDF_DETAILS))
  if (dump_file && (dump_flags & TDF_DETAILS))
    fprintf (dump_file, "\nnumber of SCoPs: %d\n",
    fprintf (dump_file, "\nnumber of SCoPs: %d\n",
             VEC_length (scop_p, *scops));
             VEC_length (scop_p, *scops));
}
}
 
 
/* Pretty print to FILE all the SCoPs in DOT format and mark them with
/* Pretty print to FILE all the SCoPs in DOT format and mark them with
   different colors.  If there are not enough colors, paint the
   different colors.  If there are not enough colors, paint the
   remaining SCoPs in gray.
   remaining SCoPs in gray.
 
 
   Special nodes:
   Special nodes:
   - "*" after the node number denotes the entry of a SCoP,
   - "*" after the node number denotes the entry of a SCoP,
   - "#" after the node number denotes the exit of a SCoP,
   - "#" after the node number denotes the exit of a SCoP,
   - "()" around the node number denotes the entry or the
   - "()" around the node number denotes the entry or the
     exit nodes of the SCOP.  These are not part of SCoP.  */
     exit nodes of the SCOP.  These are not part of SCoP.  */
 
 
static void
static void
dot_all_scops_1 (FILE *file, VEC (scop_p, heap) *scops)
dot_all_scops_1 (FILE *file, VEC (scop_p, heap) *scops)
{
{
  basic_block bb;
  basic_block bb;
  edge e;
  edge e;
  edge_iterator ei;
  edge_iterator ei;
  scop_p scop;
  scop_p scop;
  const char* color;
  const char* color;
  int i;
  int i;
 
 
  /* Disable debugging while printing graph.  */
  /* Disable debugging while printing graph.  */
  int tmp_dump_flags = dump_flags;
  int tmp_dump_flags = dump_flags;
  dump_flags = 0;
  dump_flags = 0;
 
 
  fprintf (file, "digraph all {\n");
  fprintf (file, "digraph all {\n");
 
 
  FOR_ALL_BB (bb)
  FOR_ALL_BB (bb)
    {
    {
      int part_of_scop = false;
      int part_of_scop = false;
 
 
      /* Use HTML for every bb label.  So we are able to print bbs
      /* Use HTML for every bb label.  So we are able to print bbs
         which are part of two different SCoPs, with two different
         which are part of two different SCoPs, with two different
         background colors.  */
         background colors.  */
      fprintf (file, "%d [label=<\n  <TABLE BORDER=\"0\" CELLBORDER=\"1\" ",
      fprintf (file, "%d [label=<\n  <TABLE BORDER=\"0\" CELLBORDER=\"1\" ",
                     bb->index);
                     bb->index);
      fprintf (file, "CELLSPACING=\"0\">\n");
      fprintf (file, "CELLSPACING=\"0\">\n");
 
 
      /* Select color for SCoP.  */
      /* Select color for SCoP.  */
      for (i = 0; VEC_iterate (scop_p, scops, i, scop); i++)
      for (i = 0; VEC_iterate (scop_p, scops, i, scop); i++)
        {
        {
          sese region = SCOP_REGION (scop);
          sese region = SCOP_REGION (scop);
          if (bb_in_sese_p (bb, region)
          if (bb_in_sese_p (bb, region)
              || (SESE_EXIT_BB (region) == bb)
              || (SESE_EXIT_BB (region) == bb)
              || (SESE_ENTRY_BB (region) == bb))
              || (SESE_ENTRY_BB (region) == bb))
            {
            {
              switch (i % 17)
              switch (i % 17)
                {
                {
                case 0: /* red */
                case 0: /* red */
                  color = "#e41a1c";
                  color = "#e41a1c";
                  break;
                  break;
                case 1: /* blue */
                case 1: /* blue */
                  color = "#377eb8";
                  color = "#377eb8";
                  break;
                  break;
                case 2: /* green */
                case 2: /* green */
                  color = "#4daf4a";
                  color = "#4daf4a";
                  break;
                  break;
                case 3: /* purple */
                case 3: /* purple */
                  color = "#984ea3";
                  color = "#984ea3";
                  break;
                  break;
                case 4: /* orange */
                case 4: /* orange */
                  color = "#ff7f00";
                  color = "#ff7f00";
                  break;
                  break;
                case 5: /* yellow */
                case 5: /* yellow */
                  color = "#ffff33";
                  color = "#ffff33";
                  break;
                  break;
                case 6: /* brown */
                case 6: /* brown */
                  color = "#a65628";
                  color = "#a65628";
                  break;
                  break;
                case 7: /* rose */
                case 7: /* rose */
                  color = "#f781bf";
                  color = "#f781bf";
                  break;
                  break;
                case 8:
                case 8:
                  color = "#8dd3c7";
                  color = "#8dd3c7";
                  break;
                  break;
                case 9:
                case 9:
                  color = "#ffffb3";
                  color = "#ffffb3";
                  break;
                  break;
                case 10:
                case 10:
                  color = "#bebada";
                  color = "#bebada";
                  break;
                  break;
                case 11:
                case 11:
                  color = "#fb8072";
                  color = "#fb8072";
                  break;
                  break;
                case 12:
                case 12:
                  color = "#80b1d3";
                  color = "#80b1d3";
                  break;
                  break;
                case 13:
                case 13:
                  color = "#fdb462";
                  color = "#fdb462";
                  break;
                  break;
                case 14:
                case 14:
                  color = "#b3de69";
                  color = "#b3de69";
                  break;
                  break;
                case 15:
                case 15:
                  color = "#fccde5";
                  color = "#fccde5";
                  break;
                  break;
                case 16:
                case 16:
                  color = "#bc80bd";
                  color = "#bc80bd";
                  break;
                  break;
                default: /* gray */
                default: /* gray */
                  color = "#999999";
                  color = "#999999";
                }
                }
 
 
              fprintf (file, "    <TR><TD WIDTH=\"50\" BGCOLOR=\"%s\">", color);
              fprintf (file, "    <TR><TD WIDTH=\"50\" BGCOLOR=\"%s\">", color);
 
 
              if (!bb_in_sese_p (bb, region))
              if (!bb_in_sese_p (bb, region))
                fprintf (file, " (");
                fprintf (file, " (");
 
 
              if (bb == SESE_ENTRY_BB (region)
              if (bb == SESE_ENTRY_BB (region)
                  && bb == SESE_EXIT_BB (region))
                  && bb == SESE_EXIT_BB (region))
                fprintf (file, " %d*# ", bb->index);
                fprintf (file, " %d*# ", bb->index);
              else if (bb == SESE_ENTRY_BB (region))
              else if (bb == SESE_ENTRY_BB (region))
                fprintf (file, " %d* ", bb->index);
                fprintf (file, " %d* ", bb->index);
              else if (bb == SESE_EXIT_BB (region))
              else if (bb == SESE_EXIT_BB (region))
                fprintf (file, " %d# ", bb->index);
                fprintf (file, " %d# ", bb->index);
              else
              else
                fprintf (file, " %d ", bb->index);
                fprintf (file, " %d ", bb->index);
 
 
              if (!bb_in_sese_p (bb,region))
              if (!bb_in_sese_p (bb,region))
                fprintf (file, ")");
                fprintf (file, ")");
 
 
              fprintf (file, "</TD></TR>\n");
              fprintf (file, "</TD></TR>\n");
              part_of_scop  = true;
              part_of_scop  = true;
            }
            }
        }
        }
 
 
      if (!part_of_scop)
      if (!part_of_scop)
        {
        {
          fprintf (file, "    <TR><TD WIDTH=\"50\" BGCOLOR=\"#ffffff\">");
          fprintf (file, "    <TR><TD WIDTH=\"50\" BGCOLOR=\"#ffffff\">");
          fprintf (file, " %d </TD></TR>\n", bb->index);
          fprintf (file, " %d </TD></TR>\n", bb->index);
        }
        }
      fprintf (file, "  </TABLE>>, shape=box, style=\"setlinewidth(0)\"]\n");
      fprintf (file, "  </TABLE>>, shape=box, style=\"setlinewidth(0)\"]\n");
    }
    }
 
 
  FOR_ALL_BB (bb)
  FOR_ALL_BB (bb)
    {
    {
      FOR_EACH_EDGE (e, ei, bb->succs)
      FOR_EACH_EDGE (e, ei, bb->succs)
              fprintf (file, "%d -> %d;\n", bb->index, e->dest->index);
              fprintf (file, "%d -> %d;\n", bb->index, e->dest->index);
    }
    }
 
 
  fputs ("}\n\n", file);
  fputs ("}\n\n", file);
 
 
  /* Enable debugging again.  */
  /* Enable debugging again.  */
  dump_flags = tmp_dump_flags;
  dump_flags = tmp_dump_flags;
}
}
 
 
/* Display all SCoPs using dotty.  */
/* Display all SCoPs using dotty.  */
 
 
void
void
dot_all_scops (VEC (scop_p, heap) *scops)
dot_all_scops (VEC (scop_p, heap) *scops)
{
{
  /* When debugging, enable the following code.  This cannot be used
  /* When debugging, enable the following code.  This cannot be used
     in production compilers because it calls "system".  */
     in production compilers because it calls "system".  */
#if 0
#if 0
  int x;
  int x;
  FILE *stream = fopen ("/tmp/allscops.dot", "w");
  FILE *stream = fopen ("/tmp/allscops.dot", "w");
  gcc_assert (stream);
  gcc_assert (stream);
 
 
  dot_all_scops_1 (stream, scops);
  dot_all_scops_1 (stream, scops);
  fclose (stream);
  fclose (stream);
 
 
  x = system ("dotty /tmp/allscops.dot &");
  x = system ("dotty /tmp/allscops.dot &");
#else
#else
  dot_all_scops_1 (stderr, scops);
  dot_all_scops_1 (stderr, scops);
#endif
#endif
}
}
 
 
/* Display all SCoPs using dotty.  */
/* Display all SCoPs using dotty.  */
 
 
void
void
dot_scop (scop_p scop)
dot_scop (scop_p scop)
{
{
  VEC (scop_p, heap) *scops = NULL;
  VEC (scop_p, heap) *scops = NULL;
 
 
  if (scop)
  if (scop)
    VEC_safe_push (scop_p, heap, scops, scop);
    VEC_safe_push (scop_p, heap, scops, scop);
 
 
  /* When debugging, enable the following code.  This cannot be used
  /* When debugging, enable the following code.  This cannot be used
     in production compilers because it calls "system".  */
     in production compilers because it calls "system".  */
#if 0
#if 0
  {
  {
    int x;
    int x;
    FILE *stream = fopen ("/tmp/allscops.dot", "w");
    FILE *stream = fopen ("/tmp/allscops.dot", "w");
    gcc_assert (stream);
    gcc_assert (stream);
 
 
    dot_all_scops_1 (stream, scops);
    dot_all_scops_1 (stream, scops);
    fclose (stream);
    fclose (stream);
    x = system ("dotty /tmp/allscops.dot &");
    x = system ("dotty /tmp/allscops.dot &");
  }
  }
#else
#else
  dot_all_scops_1 (stderr, scops);
  dot_all_scops_1 (stderr, scops);
#endif
#endif
 
 
  VEC_free (scop_p, heap, scops);
  VEC_free (scop_p, heap, scops);
}
}
 
 
#endif
#endif
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.