Hi, [EMAIL PROTECTED] wrote on 16/01/2008 15:20:00:
> > When a loop is vectorized, some statements are removed from the basic > > blocks, but the vectorizer information attached to these BBs is never > > freed. > > Sebastian, thanks for bringing this to our attention. I'll look into this. > I hope that removing stmts from a BB can be easily localized. > -- Victor > The attached patch, mainly written by Victor, fixes memory leaks in the vectorizer, that were found with the help of valgrind and by examining the code. Bootstrapped with vectorization enabled and tested on vectorizer testsuite on ppc-linux. I still have to perform full regtesting. Is it O.K. for 4.3? Or will it wait for 4.4? Thanks,. Victor and Ira ChangeLog: * tree-vectorizer.c (free_stmt_vec_info): New function. (destroy_loop_vec_info): Move code to free_stmt_vec_info().): Call free_stmt_vec_info(). Free LOOP_VINFO_STRIDED_STORES. * tree-vectorizer.h (free_stmt_vec_info): Declare. * tree-vect-transform.c (vectorizable_conversion): Free vec_oprnds0 if it was allocated. (vect_permute_store_chain): Remove unused VECs. (vectorizable_store): Free VECs that are allocated in the function. (vect_transform_strided_load, vectorizable_load): Likewise. (vect_remove_stores): Simplify the code. (vect_transform_loop): Move code to vect_remove_stores(). Call vect_remove_stores() and free_stmt_vec_info(). (See attached file: memleaks.txt)
Index: tree-vectorizer.c =================================================================== --- tree-vectorizer.c (revision 131899) +++ tree-vectorizer.c (working copy) @@ -1558,6 +1558,22 @@ new_stmt_vec_info (tree stmt, loop_vec_i } +/* Free stmt vectorization related info. */ + +void +free_stmt_vec_info (tree stmt) +{ + stmt_vec_info stmt_info = vinfo_for_stmt (stmt); + + if (!stmt_info) + return; + + VEC_free (dr_p, heap, STMT_VINFO_SAME_ALIGN_REFS (stmt_info)); + free (stmt_info); + set_stmt_info (stmt_ann (stmt), NULL); +} + + /* Function bb_in_loop_p Used as predicate for dfs order traversal of the loop bbs. */ @@ -1714,21 +1730,13 @@ destroy_loop_vec_info (loop_vec_info loo { basic_block bb = bbs[j]; tree phi; - stmt_vec_info stmt_info; for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi)) - { - stmt_ann_t ann = stmt_ann (phi); - - stmt_info = vinfo_for_stmt (phi); - free (stmt_info); - set_stmt_info (ann, NULL); - } + free_stmt_vec_info (phi); for (si = bsi_start (bb); !bsi_end_p (si); ) { tree stmt = bsi_stmt (si); - stmt_ann_t ann = stmt_ann (stmt); stmt_vec_info stmt_info = vinfo_for_stmt (stmt); if (stmt_info) @@ -1746,9 +1754,7 @@ destroy_loop_vec_info (loop_vec_info loo } /* Free stmt_vec_info. */ - VEC_free (dr_p, heap, STMT_VINFO_SAME_ALIGN_REFS (stmt_info)); - free (stmt_info); - set_stmt_info (ann, NULL); + free_stmt_vec_info (stmt); /* Remove dead "pattern stmts". */ if (remove_stmt_p) @@ -1767,6 +1773,7 @@ destroy_loop_vec_info (loop_vec_info loo for (j = 0; VEC_iterate (slp_instance, slp_instances, j, instance); j++) vect_free_slp_tree (SLP_INSTANCE_TREE (instance)); VEC_free (slp_instance, heap, LOOP_VINFO_SLP_INSTANCES (loop_vinfo)); + VEC_free (tree, heap, LOOP_VINFO_STRIDED_STORES (loop_vinfo)); free (loop_vinfo); loop->aux = NULL; Index: tree-vectorizer.h =================================================================== --- tree-vectorizer.h (revision 131899) +++ tree-vectorizer.h (working copy) @@ -667,6 +667,7 @@ extern bool supportable_narrowing_operat extern loop_vec_info new_loop_vec_info (struct loop *loop); extern void destroy_loop_vec_info (loop_vec_info, bool); extern stmt_vec_info new_stmt_vec_info (tree stmt, loop_vec_info); +extern void free_stmt_vec_info (tree stmt); /** In tree-vect-analyze.c **/ Index: tree-vect-transform.c =================================================================== --- tree-vect-transform.c (revision 131899) +++ tree-vect-transform.c (working copy) @@ -3638,6 +3638,9 @@ vectorizable_conversion (tree stmt, bloc *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info); } + if (vec_oprnds0) + VEC_free (tree, heap, vec_oprnds0); + return true; } @@ -4589,11 +4592,8 @@ vect_permute_store_chain (VEC(tree,heap) tree scalar_dest, tmp; int i; unsigned int j; - VEC(tree,heap) *first, *second; scalar_dest = GIMPLE_STMT_OPERAND (stmt, 0); - first = VEC_alloc (tree, heap, length/2); - second = VEC_alloc (tree, heap, length/2); /* Check that the operation is supported. */ if (!vect_strided_store_supported (vectype)) @@ -4976,6 +4976,11 @@ vectorizable_store (tree stmt, block_stm } } + VEC_free (tree, heap, dr_chain); + VEC_free (tree, heap, oprnds); + if (result_chain) + VEC_free (tree, heap, result_chain); + return true; } @@ -5481,6 +5486,8 @@ vect_transform_strided_load (tree stmt, break; } } + + VEC_free (tree, heap, result_chain); return true; } @@ -5920,6 +5927,7 @@ vectorizable_load (tree stmt, block_stmt if (!vect_transform_strided_load (stmt, dr_chain, group_size, bsi)) return false; *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info); + VEC_free (tree, heap, dr_chain); dr_chain = VEC_alloc (tree, heap, group_size); } else @@ -5932,6 +5940,9 @@ vectorizable_load (tree stmt, block_stmt } } + if (dr_chain) + VEC_free (tree, heap, dr_chain); + return true; } @@ -7268,10 +7279,8 @@ vect_loop_versioning (loop_vec_info loop static void vect_remove_stores (tree first_stmt) { - stmt_ann_t ann; tree next = first_stmt; tree tmp; - stmt_vec_info next_stmt_info; block_stmt_iterator next_si; while (next) @@ -7279,11 +7288,8 @@ vect_remove_stores (tree first_stmt) /* Free the attached stmt_vec_info and remove the stmt. */ next_si = bsi_for_stmt (next); bsi_remove (&next_si, true); - next_stmt_info = vinfo_for_stmt (next); - ann = stmt_ann (next); - tmp = DR_GROUP_NEXT_DR (next_stmt_info); - free (next_stmt_info); - set_stmt_info (ann, NULL); + tmp = DR_GROUP_NEXT_DR (vinfo_for_stmt (next)); + free_stmt_vec_info (next); next = tmp; } } @@ -7382,7 +7388,7 @@ vect_transform_loop (loop_vec_info loop_ struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo); basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo); int nbbs = loop->num_nodes; - block_stmt_iterator si, next_si; + block_stmt_iterator si; int i; tree ratio = NULL; int vectorization_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo); @@ -7547,37 +7553,19 @@ vect_transform_loop (loop_vec_info loop_ is_store = vect_transform_stmt (stmt, &si, &strided_store, NULL); if (is_store) { - stmt_ann_t ann; if (STMT_VINFO_STRIDED_ACCESS (stmt_info)) { /* Interleaving. If IS_STORE is TRUE, the vectorization of the interleaving chain was completed - free all the stores in the chain. */ - tree next = DR_GROUP_FIRST_DR (stmt_info); - tree tmp; - stmt_vec_info next_stmt_info; - - while (next) - { - next_si = bsi_for_stmt (next); - next_stmt_info = vinfo_for_stmt (next); - /* Free the attached stmt_vec_info and remove the stmt. */ - ann = stmt_ann (next); - tmp = DR_GROUP_NEXT_DR (next_stmt_info); - free (next_stmt_info); - set_stmt_info (ann, NULL); - bsi_remove (&next_si, true); - next = tmp; - } + vect_remove_stores (DR_GROUP_FIRST_DR (stmt_info)); bsi_remove (&si, true); continue; } else { /* Free the attached stmt_vec_info and remove the stmt. */ - ann = stmt_ann (stmt); - free (stmt_info); - set_stmt_info (ann, NULL); + free_stmt_vec_info (stmt); bsi_remove (&si, true); continue; }