OSDN Git Service

gcc:
[pf3gnuchains/gcc-fork.git] / gcc / graphite-poly.h
index f059834..df04570 100644 (file)
@@ -1,5 +1,5 @@
 /* Graphite polyhedral representation.
-   Copyright (C) 2009 Free Software Foundation, Inc.
+   Copyright (C) 2009, 2010 Free Software Foundation, Inc.
    Contributed by Sebastian Pop <sebastian.pop@amd.com> and
    Tobias Grosser <grosser@fim.uni-passau.de>.
 
@@ -155,8 +155,8 @@ struct poly_dr
 void new_poly_dr (poly_bb_p, int, ppl_Pointset_Powerset_C_Polyhedron_t,
                  enum poly_dr_type, void *, graphite_dim_t);
 void free_poly_dr (poly_dr_p);
-void debug_pdr (poly_dr_p);
-void print_pdr (FILE *, poly_dr_p);
+void debug_pdr (poly_dr_p, int);
+void print_pdr (FILE *, poly_dr_p, int);
 static inline scop_p pdr_scop (poly_dr_p pdr);
 
 /* The dimension of the PDR_ACCESSES polyhedron of PDR.  */
@@ -263,7 +263,9 @@ typedef struct poly_scattering *poly_scattering_p;
 
 struct poly_scattering
 {
-  /* The scattering function containing the transformations.  */
+  /* The scattering function containing the transformations: the
+     layout of this polyhedron is: T|I|G with T the transform
+     scattering, I the iteration domain, G the context parameters.  */
   ppl_Polyhedron_t scattering;
 
   /* The number of local variables.  */
@@ -277,11 +279,15 @@ struct poly_scattering
 
 struct poly_bb
 {
+  /* Pointer to a basic block or a statement in the compiler.  */
   void *black_box;
 
+  /* Pointer to the SCOP containing this PBB.  */
   scop_p scop;
 
-  /* The iteration domain of this bb.
+  /* The iteration domain of this bb.  The layout of this polyhedron
+     is I|G with I the iteration domain, G the context parameters.
+
      Example:
 
      for (i = a - 7*b + 8; i <= 3*a + 13*b + 20; i++)
@@ -340,24 +346,27 @@ extern void new_poly_bb (scop_p, void *, bool);
 extern void free_poly_bb (poly_bb_p);
 extern void debug_loop_vec (poly_bb_p);
 extern void schedule_to_scattering (poly_bb_p, int);
-extern void print_pbb_domain (FILE *, poly_bb_p);
-extern void print_pbb (FILE *, poly_bb_p);
-extern void print_scop_context (FILE *, scop_p);
-extern void print_scop (FILE *, scop_p);
-extern void debug_pbb_domain (poly_bb_p);
-extern void debug_pbb (poly_bb_p);
-extern void print_pdrs (FILE *, poly_bb_p);
-extern void debug_pdrs (poly_bb_p);
-extern void debug_scop_context (scop_p);
-extern void debug_scop (scop_p);
-extern void print_scop_params (FILE *, scop_p);
-extern void debug_scop_params (scop_p);
-extern void print_iteration_domain (FILE *, poly_bb_p);
-extern void print_iteration_domains (FILE *, scop_p);
-extern void debug_iteration_domain (poly_bb_p);
-extern void debug_iteration_domains (scop_p);
+extern void print_pbb_domain (FILE *, poly_bb_p, int);
+extern void print_pbb (FILE *, poly_bb_p, int);
+extern void print_scop_context (FILE *, scop_p, int);
+extern void print_scop (FILE *, scop_p, int);
+extern void print_cloog (FILE *, scop_p, int);
+extern void debug_pbb_domain (poly_bb_p, int);
+extern void debug_pbb (poly_bb_p, int);
+extern void print_pdrs (FILE *, poly_bb_p, int);
+extern void debug_pdrs (poly_bb_p, int);
+extern void debug_scop_context (scop_p, int);
+extern void debug_scop (scop_p, int);
+extern void debug_cloog (scop_p, int);
+extern void print_scop_params (FILE *, scop_p, int);
+extern void debug_scop_params (scop_p, int);
+extern void print_iteration_domain (FILE *, poly_bb_p, int);
+extern void print_iteration_domains (FILE *, scop_p, int);
+extern void debug_iteration_domain (poly_bb_p, int);
+extern void debug_iteration_domains (scop_p, int);
 extern bool scop_do_interchange (scop_p);
 extern bool scop_do_strip_mine (scop_p);
+extern bool scop_do_block (scop_p);
 extern void pbb_number_of_iterations (poly_bb_p, graphite_dim_t, Value);
 extern void pbb_number_of_iterations_at_time (poly_bb_p, graphite_dim_t, Value);
 extern void pbb_remove_duplicate_pdrs (poly_bb_p);
@@ -378,12 +387,19 @@ number_of_write_pdrs (poly_bb_p pbb)
   return res;
 }
 
+/* The basic block of the PBB.  */
+static inline basic_block
+pbb_bb (poly_bb_p pbb)
+{
+  return GBB_BB (PBB_BLACK_BOX (pbb));
+}
+
 /* The index of the PBB.  */
 
 static inline int
 pbb_index (poly_bb_p pbb)
 {
-  return GBB_BB (PBB_BLACK_BOX (pbb))->index;
+  return pbb_bb (pbb)->index;
 }
 
 /* The loop of the PBB.  */
@@ -575,8 +591,19 @@ psct_parameter_dim (poly_bb_p pbb, graphite_dim_t param)
 static inline ppl_dimension_type
 psct_dynamic_dim (poly_bb_p pbb, graphite_dim_t level)
 {
-  graphite_dim_t result;
-  result = 1 + 2 * level;
+  graphite_dim_t result = 1 + 2 * level;
+
+  gcc_assert (result < pbb_nb_scattering_transform (pbb));
+  return result;
+}
+
+/* The scattering dimension of PBB corresponding to the static
+   sequence of the loop level LEVEL.  */
+
+static inline ppl_dimension_type
+psct_static_dim (poly_bb_p pbb, graphite_dim_t level)
+{
+  graphite_dim_t result = 2 * level;
 
   gcc_assert (result < pbb_nb_scattering_transform (pbb));
   return result;
@@ -620,6 +647,9 @@ struct lst {
   /* A pointer to the loop that contains this node.  */
   lst_p loop_father;
 
+  /* The sum of all the memory strides for an LST loop.  */
+  Value memory_strides;
+
   /* Loop nodes contain a sequence SEQ of LST nodes, statements
      contain a pointer to their polyhedral representation PBB.  */
   union {
@@ -632,10 +662,12 @@ struct lst {
 #define LST_LOOP_FATHER(LST) ((LST)->loop_father)
 #define LST_PBB(LST) ((LST)->node.pbb)
 #define LST_SEQ(LST) ((LST)->node.seq)
+#define LST_LOOP_MEMORY_STRIDES(LST) ((LST)->memory_strides)
 
 void scop_to_lst (scop_p);
 void print_lst (FILE *, lst_p, int);
 void debug_lst (lst_p);
+void dot_lst (lst_p);
 
 /* Creates a new LST loop with SEQ.  */
 
@@ -649,6 +681,8 @@ new_lst_loop (VEC (lst_p, heap) *seq)
   LST_LOOP_P (lst) = true;
   LST_SEQ (lst) = seq;
   LST_LOOP_FATHER (lst) = NULL;
+  value_init (LST_LOOP_MEMORY_STRIDES (lst));
+  value_set_si (LST_LOOP_MEMORY_STRIDES (lst), -1);
 
   for (i = 0; VEC_iterate (lst_p, seq, i, l); i++)
     LST_LOOP_FATHER (l) = lst;
@@ -669,6 +703,29 @@ new_lst_stmt (poly_bb_p pbb)
   return lst;
 }
 
+/* Frees the memory used by LST.  */
+
+static inline void
+free_lst (lst_p lst)
+{
+  if (!lst)
+    return;
+
+  if (LST_LOOP_P (lst))
+    {
+      int i;
+      lst_p l;
+
+      for (i = 0; VEC_iterate (lst_p, LST_SEQ (lst), i, l); i++)
+       free_lst (l);
+
+      value_clear (LST_LOOP_MEMORY_STRIDES (lst));
+      VEC_free (lst_p, heap, LST_SEQ (lst));
+    }
+
+  free (lst);
+}
+
 /* Returns a copy of LST.  */
 
 static inline lst_p
@@ -692,12 +749,33 @@ copy_lst (lst_p lst)
   return new_lst_stmt (LST_PBB (lst));
 }
 
+/* Adds a new loop under the loop LST.  */
+
+static inline void
+lst_add_loop_under_loop (lst_p lst)
+{
+  VEC (lst_p, heap) *seq = VEC_alloc (lst_p, heap, 1);
+  lst_p l = new_lst_loop (LST_SEQ (lst));
+
+  gcc_assert (LST_LOOP_P (lst));
+
+  LST_LOOP_FATHER (l) = lst;
+  VEC_quick_push (lst_p, seq, l);
+  LST_SEQ (lst) = seq;
+}
+
 /* Returns the loop depth of LST.  */
 
 static inline int
 lst_depth (lst_p lst)
 {
   if (!lst)
+    return -2;
+
+  /* The depth of the outermost "fake" loop is -1.  This outermost
+     loop does not have a loop father and it is just a container, as
+     in the loop representation of GCC.  */
+  if (!LST_LOOP_FATHER (lst))
     return -1;
 
   return lst_depth (LST_LOOP_FATHER (lst)) + 1;
@@ -724,6 +802,61 @@ lst_dewey_number (lst_p lst)
   return -1;
 }
 
+/* Returns the Dewey number of LST at depth DEPTH.  */
+
+static inline int
+lst_dewey_number_at_depth (lst_p lst, int depth)
+{
+  gcc_assert (lst && depth >= 0 && lst_depth (lst) <= depth);
+
+  if (lst_depth (lst) == depth)
+    return lst_dewey_number (lst);
+
+  return lst_dewey_number_at_depth (LST_LOOP_FATHER (lst), depth);
+}
+
+/* Returns the predecessor of LST in the sequence of its loop father.
+   Returns NULL if LST is the first statement in the sequence.  */
+
+static inline lst_p
+lst_pred (lst_p lst)
+{
+  int dewey;
+  lst_p father;
+
+  if (!lst || !LST_LOOP_FATHER (lst))
+    return NULL;
+
+  dewey = lst_dewey_number (lst);
+  if (dewey == 0)
+    return NULL;
+
+  father = LST_LOOP_FATHER (lst);
+  return VEC_index (lst_p, LST_SEQ (father), dewey - 1);
+}
+
+/* Returns the successor of LST in the sequence of its loop father.
+   Returns NULL if there is none.  */
+
+static inline lst_p
+lst_succ (lst_p lst)
+{
+  int dewey;
+  lst_p father;
+
+  if (!lst || !LST_LOOP_FATHER (lst))
+    return NULL;
+
+  dewey = lst_dewey_number (lst);
+  father = LST_LOOP_FATHER (lst);
+
+  if (VEC_length (lst_p, LST_SEQ (father)) == (unsigned) dewey + 1)
+    return NULL;
+
+  return VEC_index (lst_p, LST_SEQ (father), dewey + 1);
+}
+
+
 /* Return the LST node corresponding to PBB.  */
 
 static inline lst_p
@@ -735,15 +868,15 @@ lst_find_pbb (lst_p lst, poly_bb_p pbb)
   if (!lst)
     return NULL;
 
-  if (LST_LOOP_P (lst))
-    for (i = 0; VEC_iterate (lst_p, LST_SEQ (lst), i, l); i++)
-      {
-       lst_p res = lst_find_pbb (l, pbb);
-       if (res)
-         return res;
-      }
-  else if (pbb == LST_PBB (lst))
-    return lst;
+  if (!LST_LOOP_P (lst))
+    return (pbb == LST_PBB (lst)) ? lst : NULL;
+
+  for (i = 0; VEC_iterate (lst_p, LST_SEQ (lst), i, l); i++)
+    {
+      lst_p res = lst_find_pbb (l, pbb);
+      if (res)
+       return res;
+    }
 
   return NULL;
 }
@@ -764,6 +897,390 @@ find_lst_loop (lst_p stmt, int loop_depth)
   return loop;
 }
 
+/* Return the first lst representing a PBB statement in LST.  */
+
+static inline lst_p
+lst_find_first_pbb (lst_p lst)
+{
+  int i;
+  lst_p l;
+
+  if (!lst)
+    return NULL;
+
+  if (!LST_LOOP_P (lst))
+    return lst;
+
+  for (i = 0; VEC_iterate (lst_p, LST_SEQ (lst), i, l); i++)
+    {
+      lst_p res = lst_find_first_pbb (l);
+      if (res)
+       return res;
+    }
+
+  return NULL;
+}
+
+/* Returns true when LST is a loop that does not contains
+   statements.  */
+
+static inline bool
+lst_empty_p (lst_p lst)
+{
+  return !lst_find_first_pbb (lst);
+}
+
+/* Return the last lst representing a PBB statement in LST.  */
+
+static inline lst_p
+lst_find_last_pbb (lst_p lst)
+{
+  int i;
+  lst_p l, res = NULL;
+
+  if (!lst)
+    return NULL;
+
+  if (!LST_LOOP_P (lst))
+    return lst;
+
+  for (i = 0; VEC_iterate (lst_p, LST_SEQ (lst), i, l); i++)
+    {
+      lst_p last = lst_find_last_pbb (l);
+
+      if (last)
+       res = last;
+    }
+
+  gcc_assert (res);
+  return res;
+}
+
+/* Returns true if LOOP contains LST, in other words, if LST is nested
+   in LOOP.  */
+
+static inline bool
+lst_contains_p (lst_p loop, lst_p lst)
+{
+  if (!loop || !lst || !LST_LOOP_P (loop))
+    return false;
+
+  if (loop == lst)
+    return true;
+
+  return lst_contains_p (loop, LST_LOOP_FATHER (lst));
+}
+
+/* Returns true if LOOP contains PBB, in other words, if PBB is nested
+   in LOOP.  */
+
+static inline bool
+lst_contains_pbb (lst_p loop, poly_bb_p pbb)
+{
+  return lst_find_pbb (loop, pbb) ? true : false;
+}
+
+/* Creates a loop nest of depth NB_LOOPS containing LST.  */
+
+static inline lst_p
+lst_create_nest (int nb_loops, lst_p lst)
+{
+  lst_p res, loop;
+  VEC (lst_p, heap) *seq;
+
+  if (nb_loops == 0)
+    return lst;
+
+  seq = VEC_alloc (lst_p, heap, 1);
+  loop = lst_create_nest (nb_loops - 1, lst);
+  VEC_quick_push (lst_p, seq, loop);
+  res = new_lst_loop (seq);
+  LST_LOOP_FATHER (loop) = res;
+
+  return res;
+}
+
+/* Removes LST from the sequence of statements of its loop father.  */
+
+static inline void
+lst_remove_from_sequence (lst_p lst)
+{
+  lst_p father = LST_LOOP_FATHER (lst);
+  int dewey = lst_dewey_number (lst);
+
+  gcc_assert (lst && father && dewey >= 0);
+
+  VEC_ordered_remove (lst_p, LST_SEQ (father), dewey);
+  LST_LOOP_FATHER (lst) = NULL;
+}
+
+/* Updates the scattering of PBB to be at the DEWEY number in the loop
+   at depth LEVEL.  */
+
+static inline void
+pbb_update_scattering (poly_bb_p pbb, graphite_dim_t level, int dewey)
+{
+  ppl_Polyhedron_t ph = PBB_TRANSFORMED_SCATTERING (pbb);
+  ppl_dimension_type sched = psct_static_dim (pbb, level);
+  ppl_dimension_type ds[1];
+  ppl_Constraint_t new_cstr;
+  ppl_Linear_Expression_t expr;
+  ppl_dimension_type dim;
+
+  ppl_Polyhedron_space_dimension (ph, &dim);
+  ds[0] = sched;
+  ppl_Polyhedron_remove_space_dimensions (ph, ds, 1);
+  ppl_insert_dimensions (ph, sched, 1);
+
+  ppl_new_Linear_Expression_with_dimension (&expr, dim);
+  ppl_set_coef (expr, sched, -1);
+  ppl_set_inhomogeneous (expr, dewey);
+  ppl_new_Constraint (&new_cstr, expr, PPL_CONSTRAINT_TYPE_EQUAL);
+  ppl_delete_Linear_Expression (expr);
+  ppl_Polyhedron_add_constraint (ph, new_cstr);
+  ppl_delete_Constraint (new_cstr);
+}
+
+/* Updates the scattering of all the PBBs under LST to be at the DEWEY
+   number in the loop at depth LEVEL.  */
+
+static inline void
+lst_update_scattering_under (lst_p lst, int level, int dewey)
+{
+  int i;
+  lst_p l;
+
+  gcc_assert (lst && level >= 0 && dewey >= 0);
+
+  if (LST_LOOP_P (lst))
+    for (i = 0; VEC_iterate (lst_p, LST_SEQ (lst), i, l); i++)
+      lst_update_scattering_under (l, level, dewey);
+  else
+    pbb_update_scattering (LST_PBB (lst), level, dewey);
+}
+
+/* Updates the scattering of all the PBBs under LST and in sequence
+   with LST.  */
+
+static inline void
+lst_update_scattering_seq (lst_p lst)
+{
+  int i;
+  lst_p l;
+  lst_p father = LST_LOOP_FATHER (lst);
+  int dewey = lst_dewey_number (lst);
+  int level = lst_depth (lst);
+
+  gcc_assert (lst && father && dewey >= 0 && level >= 0);
+
+  for (i = dewey; VEC_iterate (lst_p, LST_SEQ (father), i, l); i++)
+    lst_update_scattering_under (l, level, i);
+}
+
+/* Updates the all the scattering levels of all the PBBs under
+   LST.  */
+
+static inline void
+lst_update_scattering (lst_p lst)
+{
+  int i;
+  lst_p l;
+
+  if (!lst || !LST_LOOP_P (lst))
+    return;
+
+  if (LST_LOOP_FATHER (lst))
+    lst_update_scattering_seq (lst);
+
+  for (i = 0; VEC_iterate (lst_p, LST_SEQ (lst), i, l); i++)
+    lst_update_scattering (l);
+}
+
+/* Inserts LST1 before LST2 if BEFORE is true; inserts LST1 after LST2
+   if BEFORE is false.  */
+
+static inline void
+lst_insert_in_sequence (lst_p lst1, lst_p lst2, bool before)
+{
+  lst_p father;
+  int dewey;
+
+  /* Do not insert empty loops.  */
+  if (!lst1 || lst_empty_p (lst1))
+    return;
+
+  father = LST_LOOP_FATHER (lst2);
+  dewey = lst_dewey_number (lst2);
+
+  gcc_assert (lst2 && father && dewey >= 0);
+
+  VEC_safe_insert (lst_p, heap, LST_SEQ (father), before ? dewey : dewey + 1,
+                  lst1);
+  LST_LOOP_FATHER (lst1) = father;
+}
+
+/* Replaces LST1 with LST2.  */
+
+static inline void
+lst_replace (lst_p lst1, lst_p lst2)
+{
+  lst_p father;
+  int dewey;
+
+  if (!lst2 || lst_empty_p (lst2))
+    return;
+
+  father = LST_LOOP_FATHER (lst1);
+  dewey = lst_dewey_number (lst1);
+  LST_LOOP_FATHER (lst2) = father;
+  VEC_replace (lst_p, LST_SEQ (father), dewey, lst2);
+}
+
+/* Returns a copy of ROOT where LST has been replaced by a copy of the
+   LSTs A B C in this sequence.  */
+
+static inline lst_p
+lst_substitute_3 (lst_p root, lst_p lst, lst_p a, lst_p b, lst_p c)
+{
+  int i;
+  lst_p l;
+  VEC (lst_p, heap) *seq;
+
+  if (!root)
+    return NULL;
+
+  gcc_assert (lst && root != lst);
+
+  if (!LST_LOOP_P (root))
+    return new_lst_stmt (LST_PBB (root));
+
+  seq = VEC_alloc (lst_p, heap, 5);
+
+  for (i = 0; VEC_iterate (lst_p, LST_SEQ (root), i, l); i++)
+    if (l != lst)
+      VEC_safe_push (lst_p, heap, seq, lst_substitute_3 (l, lst, a, b, c));
+    else
+      {
+       if (!lst_empty_p (a))
+         VEC_safe_push (lst_p, heap, seq, copy_lst (a));
+       if (!lst_empty_p (b))
+         VEC_safe_push (lst_p, heap, seq, copy_lst (b));
+       if (!lst_empty_p (c))
+         VEC_safe_push (lst_p, heap, seq, copy_lst (c));
+      }
+
+  return new_lst_loop (seq);
+}
+
+/* Moves LST before LOOP if BEFORE is true, and after the LOOP if
+   BEFORE is false.  */
+
+static inline void
+lst_distribute_lst (lst_p loop, lst_p lst, bool before)
+{
+  int loop_depth = lst_depth (loop);
+  int depth = lst_depth (lst);
+  int nb_loops = depth - loop_depth;
+
+  gcc_assert (lst && loop && LST_LOOP_P (loop) && nb_loops > 0);
+
+  lst_remove_from_sequence (lst);
+  lst_insert_in_sequence (lst_create_nest (nb_loops, lst), loop, before);
+}
+
+/* Removes from LOOP all the statements before/after and including PBB
+   if BEFORE is true/false.  Returns the negation of BEFORE when the
+   statement PBB has been found.  */
+
+static inline bool
+lst_remove_all_before_including_pbb (lst_p loop, poly_bb_p pbb, bool before)
+{
+  int i;
+  lst_p l;
+
+  if (!loop || !LST_LOOP_P (loop))
+    return before;
+
+  for (i = 0; VEC_iterate (lst_p, LST_SEQ (loop), i, l);)
+    if (LST_LOOP_P (l))
+      {
+       before = lst_remove_all_before_including_pbb (l, pbb, before);
+
+       if (VEC_length (lst_p, LST_SEQ (l)) == 0)
+         {
+           VEC_ordered_remove (lst_p, LST_SEQ (loop), i);
+           free_lst (l);
+         }
+       else
+         i++;
+      }
+    else
+      {
+       if (before)
+         {
+           if (LST_PBB (l) == pbb)
+             before = false;
+
+           VEC_ordered_remove (lst_p, LST_SEQ (loop), i);
+           free_lst (l);
+         }
+       else if (LST_PBB (l) == pbb)
+         {
+           before = true;
+           VEC_ordered_remove (lst_p, LST_SEQ (loop), i);
+           free_lst (l);
+         }
+       else
+         i++;
+      }
+
+  return before;
+}
+
+/* Removes from LOOP all the statements before/after and excluding PBB
+   if BEFORE is true/false; Returns the negation of BEFORE when the
+   statement PBB has been found.  */
+
+static inline bool
+lst_remove_all_before_excluding_pbb (lst_p loop, poly_bb_p pbb, bool before)
+{
+  int i;
+  lst_p l;
+
+  if (!loop || !LST_LOOP_P (loop))
+    return before;
+
+  for (i = 0; VEC_iterate (lst_p, LST_SEQ (loop), i, l);)
+    if (LST_LOOP_P (l))
+      {
+       before = lst_remove_all_before_excluding_pbb (l, pbb, before);
+
+       if (VEC_length (lst_p, LST_SEQ (l)) == 0)
+         {
+           VEC_ordered_remove (lst_p, LST_SEQ (loop), i);
+           free_lst (l);
+           continue;
+         }
+
+       i++;
+      }
+    else
+      {
+       if (before && LST_PBB (l) != pbb)
+         {
+           VEC_ordered_remove (lst_p, LST_SEQ (loop), i);
+           free_lst (l);
+           continue;
+         }
+
+       i++;
+
+       if (LST_PBB (l) == pbb)
+         before = before ? false : true;
+      }
+
+  return before;
+}
 
 /* A SCOP is a Static Control Part of the program, simple enough to be
    represented in polyhedral form.  */
@@ -780,8 +1297,8 @@ struct scop
      representation.  */
   VEC (poly_bb_p, heap) *bbs;
 
-  /* Original and transformed schedules.  */
-  lst_p original_schedule, transformed_schedule;
+  /* Original, transformed and saved schedules.  */
+  lst_p original_schedule, transformed_schedule, saved_schedule;
 
   /* The context describes known restrictions concerning the parameters
      and relations in between the parameters.
@@ -801,6 +1318,10 @@ struct scop
   /* A hashtable of the data dependence relations for the original
      scattering.  */
   htab_t original_pddrs;
+
+  /* True when the scop has been converted to its polyhedral
+     representation.  */
+  bool poly_scop_p;
 };
 
 #define SCOP_BBS(S) (S->bbs)
@@ -809,16 +1330,18 @@ struct scop
 #define SCOP_ORIGINAL_PDDRS(S) (S->original_pddrs)
 #define SCOP_ORIGINAL_SCHEDULE(S) (S->original_schedule)
 #define SCOP_TRANSFORMED_SCHEDULE(S) (S->transformed_schedule)
+#define SCOP_SAVED_SCHEDULE(S) (S->saved_schedule)
+#define POLY_SCOP_P(S) (S->poly_scop_p)
 
 extern scop_p new_scop (void *);
 extern void free_scop (scop_p);
 extern void free_scops (VEC (scop_p, heap) *);
 extern void print_generated_program (FILE *, scop_p);
 extern void debug_generated_program (scop_p);
-extern void print_scattering_function (FILE *, poly_bb_p);
-extern void print_scattering_functions (FILE *, scop_p);
-extern void debug_scattering_function (poly_bb_p);
-extern void debug_scattering_functions (scop_p);
+extern void print_scattering_function (FILE *, poly_bb_p, int);
+extern void print_scattering_functions (FILE *, scop_p, int);
+extern void debug_scattering_function (poly_bb_p, int);
+extern void debug_scattering_functions (scop_p, int);
 extern int scop_max_loop_depth (scop_p);
 extern int unify_scattering_dimensions (scop_p);
 extern bool apply_poly_transforms (scop_p);
@@ -896,6 +1419,28 @@ store_scattering_pbb (poly_bb_p pbb)
   PBB_SAVED (pbb) = poly_scattering_copy (PBB_TRANSFORMED (pbb));
 }
 
+/* Stores the SCOP_TRANSFORMED_SCHEDULE to SCOP_SAVED_SCHEDULE.  */
+
+static inline void
+store_lst_schedule (scop_p scop)
+{
+  if (SCOP_SAVED_SCHEDULE (scop))
+    free_lst (SCOP_SAVED_SCHEDULE (scop));
+
+  SCOP_SAVED_SCHEDULE (scop) = copy_lst (SCOP_TRANSFORMED_SCHEDULE (scop));
+}
+
+/* Restores the SCOP_TRANSFORMED_SCHEDULE from SCOP_SAVED_SCHEDULE.  */
+
+static inline void
+restore_lst_schedule (scop_p scop)
+{
+  if (SCOP_TRANSFORMED_SCHEDULE (scop))
+    free_lst (SCOP_TRANSFORMED_SCHEDULE (scop));
+
+  SCOP_TRANSFORMED_SCHEDULE (scop) = copy_lst (SCOP_SAVED_SCHEDULE (scop));
+}
+
 /* Saves the scattering for all the pbbs in the SCOP.  */
 
 static inline void
@@ -906,6 +1451,8 @@ store_scattering (scop_p scop)
 
   for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
     store_scattering_pbb (pbb);
+
+  store_lst_schedule (scop);
 }
 
 /* Restores the scattering of PBB.  */
@@ -929,6 +1476,53 @@ restore_scattering (scop_p scop)
 
   for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
     restore_scattering_pbb (pbb);
+
+  restore_lst_schedule (scop);
+}
+
+/* For a given PBB, add to RES the scop context, the iteration domain,
+   the original scattering when ORIGINAL_P is true, otherwise add the
+   transformed scattering.  */
+
+static inline void
+combine_context_id_scat (ppl_Pointset_Powerset_C_Polyhedron_t *res,
+                        poly_bb_p pbb, bool original_p)
+{
+  ppl_Pointset_Powerset_C_Polyhedron_t context;
+  ppl_Pointset_Powerset_C_Polyhedron_t id;
+
+  ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron
+    (res, original_p ?
+     PBB_ORIGINAL_SCATTERING (pbb) : PBB_TRANSFORMED_SCATTERING (pbb));
+
+  ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
+    (&context, SCOP_CONTEXT (PBB_SCOP (pbb)));
+
+  ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
+    (&id, PBB_DOMAIN (pbb));
+
+  /* Extend the context and the iteration domain to the dimension of
+     the scattering: T|I|G.  */
+  {
+    ppl_dimension_type gdim, tdim, idim;
+
+    ppl_Pointset_Powerset_C_Polyhedron_space_dimension (*res, &tdim);
+    ppl_Pointset_Powerset_C_Polyhedron_space_dimension (context, &gdim);
+    ppl_Pointset_Powerset_C_Polyhedron_space_dimension (id, &idim);
+
+    if (tdim > gdim)
+      ppl_insert_dimensions_pointset (context, 0, tdim - gdim);
+
+    if (tdim > idim)
+      ppl_insert_dimensions_pointset (id, 0, tdim - idim);
+  }
+
+  /* Add the context and the iteration domain to the result.  */
+  ppl_Pointset_Powerset_C_Polyhedron_intersection_assign (*res, context);
+  ppl_Pointset_Powerset_C_Polyhedron_intersection_assign (*res, id);
+
+  ppl_delete_Pointset_Powerset_C_Polyhedron (context);
+  ppl_delete_Pointset_Powerset_C_Polyhedron (id);
 }
 
 #endif