diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 944f806..9fcdd6c 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,21 @@
+2010-12-22  Sebastian Pop  <sebastian.pop@amd.com>
+
+	PR tree-optimization/47002
+	* tree-data-ref.c (compute_data_dependences_for_loop): Free vloops.
+	(free_dependence_relations): Do not free loop_nest.
+	(build_rdg): Pass in datarefs and dependence_relations.
+	(free_rdg): Also free the data on edges.
+	* tree-data-ref.h (build_rdg): Update declaration.
+	* tree-loop-distribution.c (rdg_flag_loop_exits): Free conds.
+	(rdg_build_partitions): Free comps.
+	(ldist_gen): Free components.
+	(distribute_loop): Update call to build_rdg.  Allocate and free
+	datarefs and dependence_relations.
+	* tree-vect-loop.c (destroy_loop_vec_info): Free
+	LOOP_VINFO_MAY_ALIAS_DDRS.
+	* tree-vect-slp.c (destroy_bb_vec_info): Call free_data_refs and
+	free_dependence_relations.
+
 2010-12-17  Paolo Bonzini  <bonzini@gnu.org>
 
 	PR c/20385
diff --git a/gcc/tree-data-ref.c b/gcc/tree-data-ref.c
index a6d0731..dc14989 100644
--- a/gcc/tree-data-ref.c
+++ b/gcc/tree-data-ref.c
@@ -4274,6 +4274,9 @@ compute_data_dependences_for_loop (struct loop *loop,
     compute_all_dependences (*datarefs, dependence_relations, vloops,
 			     compute_self_and_read_read_dependences);
 
+  if (vloops)
+    VEC_free (loop_p, heap, vloops);
+
   if (dump_file && (dump_flags & TDF_STATS))
     {
       fprintf (dump_file, "Dependence tester statistics:\n");
@@ -4454,22 +4457,11 @@ free_dependence_relations (VEC (ddr_p, heap) *dependence_relations)
 {
   unsigned int i;
   struct data_dependence_relation *ddr;
-  VEC (loop_p, heap) *loop_nest = NULL;
 
   FOR_EACH_VEC_ELT (ddr_p, dependence_relations, i, ddr)
-    {
-      if (ddr == NULL)
-	continue;
-      if (loop_nest == NULL)
-	loop_nest = DDR_LOOP_NEST (ddr);
-      else
-	gcc_assert (DDR_LOOP_NEST (ddr) == NULL
-		    || DDR_LOOP_NEST (ddr) == loop_nest);
+    if (ddr)
       free_dependence_relation (ddr);
-    }
 
-  if (loop_nest)
-    VEC_free (loop_p, heap, loop_nest);
   VEC_free (ddr_p, heap, dependence_relations);
 }
 
@@ -4905,37 +4897,23 @@ build_empty_rdg (int n_stmts)
    scalar dependence.  */
 
 struct graph *
-build_rdg (struct loop *loop)
+build_rdg (struct loop *loop,
+	   VEC (ddr_p, heap) **dependence_relations,
+	   VEC (data_reference_p, heap) **datarefs)
 {
-  int nb_data_refs = 10;
   struct graph *rdg = NULL;
-  VEC (ddr_p, heap) *dependence_relations;
-  VEC (data_reference_p, heap) *datarefs;
-  VEC (gimple, heap) *stmts = VEC_alloc (gimple, heap, nb_data_refs);
-
-  dependence_relations = VEC_alloc (ddr_p, heap, nb_data_refs * nb_data_refs) ;
-  datarefs = VEC_alloc (data_reference_p, heap, nb_data_refs);
-  compute_data_dependences_for_loop (loop,
-                                     false,
-                                     &datarefs,
-                                     &dependence_relations);
-
-  if (!known_dependences_p (dependence_relations))
-    {
-      free_dependence_relations (dependence_relations);
-      free_data_refs (datarefs);
-      VEC_free (gimple, heap, stmts);
-
-      return rdg;
-    }
+  VEC (gimple, heap) *stmts = VEC_alloc (gimple, heap, 10);
 
-  stmts_from_loop (loop, &stmts);
-  rdg = build_empty_rdg (VEC_length (gimple, stmts));
+  compute_data_dependences_for_loop (loop, false, datarefs,
+				     dependence_relations);
 
-  rdg->indices = htab_create (nb_data_refs, hash_stmt_vertex_info,
-			      eq_stmt_vertex_info, hash_stmt_vertex_del);
-  create_rdg_vertices (rdg, stmts);
-  create_rdg_edges (rdg, dependence_relations);
+  if (known_dependences_p (*dependence_relations))
+    {
+      stmts_from_loop (loop, &stmts);
+      rdg = build_empty_rdg (VEC_length (gimple, stmts));
+      create_rdg_vertices (rdg, stmts);
+      create_rdg_edges (rdg, *dependence_relations);
+    }
 
   VEC_free (gimple, heap, stmts);
   return rdg;
@@ -4949,7 +4927,19 @@ free_rdg (struct graph *rdg)
   int i;
 
   for (i = 0; i < rdg->n_vertices; i++)
-    free (rdg->vertices[i].data);
+    {
+      struct vertex *v = &(rdg->vertices[i]);
+      struct graph_edge *e;
+
+      for (e = v->succ; e; e = e->succ_next)
+	{
+	  free_dependence_relation (RDGE_RELATION (e));
+	  if (e->data)
+	    free (e->data);
+	}
+
+      free (v->data);
+    }
 
   htab_delete (rdg->indices);
   free_graph (rdg);
diff --git a/gcc/tree-data-ref.h b/gcc/tree-data-ref.h
index b4f317f..8585b2b 100644
--- a/gcc/tree-data-ref.h
+++ b/gcc/tree-data-ref.h
@@ -577,7 +577,8 @@ typedef struct rdg_edge
 #define RDGE_LEVEL(E)       ((struct rdg_edge *) ((E)->data))->level
 #define RDGE_RELATION(E)    ((struct rdg_edge *) ((E)->data))->relation
 
-struct graph *build_rdg (struct loop *);
+struct graph *build_rdg (struct loop *, VEC (ddr_p, heap) **,
+			 VEC (data_reference_p, heap) **);
 struct graph *build_empty_rdg (int);
 void free_rdg (struct graph *);
 
diff --git a/gcc/tree-loop-distribution.c b/gcc/tree-loop-distribution.c
index a9ee67f..d095e8a 100644
--- a/gcc/tree-loop-distribution.c
+++ b/gcc/tree-loop-distribution.c
@@ -653,6 +653,8 @@ rdg_flag_loop_exits (struct graph *rdg, bitmap loops, bitmap partition,
 
       BITMAP_FREE (new_loops);
     }
+
+  VEC_free (gimple, heap, conds);
 }
 
 /* Returns a bitmap in which all the statements needed for computing
@@ -965,6 +967,7 @@ rdg_build_partitions (struct graph *rdg, VEC (rdgc, heap) *components,
 
       VEC_free (int, heap, foo);
       free_rdg_components (comps);
+      VEC_free (rdgc, heap, comps);
     }
 
   add_scalar_computations_to_partition (rdg, *partitions, partition);
@@ -1129,6 +1132,7 @@ ldist_gen (struct loop *loop, struct graph *rdg,
   VEC_free (int, heap, other_stores);
   VEC_free (bitmap, heap, partitions);
   free_rdg_components (components);
+  VEC_free (rdgc, heap, components);
   return nbp;
 }
 
@@ -1146,6 +1150,8 @@ distribute_loop (struct loop *loop, VEC (gimple, heap) *stmts)
   gimple s;
   unsigned i;
   VEC (int, heap) *vertices;
+  VEC (ddr_p, heap) *dependence_relations;
+  VEC (data_reference_p, heap) *datarefs;
 
   if (loop->num_nodes > 2)
     {
@@ -1157,7 +1163,9 @@ distribute_loop (struct loop *loop, VEC (gimple, heap) *stmts)
       return res;
     }
 
-  rdg = build_rdg (loop);
+  datarefs = VEC_alloc (data_reference_p, heap, 10);
+  dependence_relations = VEC_alloc (ddr_p, heap, 100);
+  rdg = build_rdg (loop, &dependence_relations, &datarefs);
 
   if (!rdg)
     {
@@ -1166,6 +1174,8 @@ distribute_loop (struct loop *loop, VEC (gimple, heap) *stmts)
 		 "FIXME: Loop %d not distributed: failed to build the RDG.\n",
 		 loop->num);
 
+      free_dependence_relations (dependence_relations);
+      free_data_refs (datarefs);
       return res;
     }
 
@@ -1191,7 +1201,8 @@ distribute_loop (struct loop *loop, VEC (gimple, heap) *stmts)
   res = ldist_gen (loop, rdg, vertices);
   VEC_free (int, heap, vertices);
   free_rdg (rdg);
-
+  free_dependence_relations (dependence_relations);
+  free_data_refs (datarefs);
   return res;
 }
 
diff --git a/gcc/tree-vect-loop.c b/gcc/tree-vect-loop.c
index 2644c9e..3aa21c7 100644
--- a/gcc/tree-vect-loop.c
+++ b/gcc/tree-vect-loop.c
@@ -794,6 +794,7 @@ destroy_loop_vec_info (loop_vec_info loop_vinfo, bool clean_stmts)
       free_data_refs (LOOP_VINFO_DATAREFS (loop_vinfo));
       free_dependence_relations (LOOP_VINFO_DDRS (loop_vinfo));
       VEC_free (gimple, heap, LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo));
+      VEC_free (ddr_p, heap, LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo));
 
       free (loop_vinfo);
       loop->aux = NULL;
diff --git a/gcc/tree-vect-slp.c b/gcc/tree-vect-slp.c
index fdaaff2..00fb345 100644
--- a/gcc/tree-vect-slp.c
+++ b/gcc/tree-vect-slp.c
@@ -1457,6 +1457,8 @@ destroy_bb_vec_info (bb_vec_info bb_vinfo)
         free_stmt_vec_info (stmt);
     }
 
+  free_data_refs (BB_VINFO_DATAREFS (bb_vinfo));
+  free_dependence_relations (BB_VINFO_DDRS (bb_vinfo));
   VEC_free (gimple, heap, BB_VINFO_STRIDED_STORES (bb_vinfo));
   VEC_free (slp_instance, heap, BB_VINFO_SLP_INSTANCES (bb_vinfo));
   free (bb_vinfo);
