comparison gcc/tree-ssa-loop-manip.c @ 55:77e2b8dfacca gcc-4.4.5

update it from 4.4.3 to 4.5.0
author ryoma <e075725@ie.u-ryukyu.ac.jp>
date Fri, 12 Feb 2010 23:39:51 +0900
parents a06113de4d67
children b7f97abdc517
comparison
equal deleted inserted replaced
52:c156f1bd5cd9 55:77e2b8dfacca
1 /* High-level loop manipulation functions. 1 /* High-level loop manipulation functions.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc. 2 Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
3 3
4 This file is part of GCC. 4 This file is part of GCC.
5 5
6 GCC is free software; you can redistribute it and/or modify it 6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the 7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any 8 Free Software Foundation; either version 3, or (at your option) any
9 later version. 9 later version.
10 10
11 GCC is distributed in the hope that it will be useful, but WITHOUT 11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details. 14 for more details.
15 15
16 You should have received a copy of the GNU General Public License 16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see 17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */ 18 <http://www.gnu.org/licenses/>. */
19 19
20 #include "config.h" 20 #include "config.h"
35 #include "tree-pass.h" 35 #include "tree-pass.h"
36 #include "cfglayout.h" 36 #include "cfglayout.h"
37 #include "tree-scalar-evolution.h" 37 #include "tree-scalar-evolution.h"
38 #include "params.h" 38 #include "params.h"
39 #include "tree-inline.h" 39 #include "tree-inline.h"
40 #include "langhooks.h"
40 41
41 /* Creates an induction variable with value BASE + STEP * iteration in LOOP. 42 /* Creates an induction variable with value BASE + STEP * iteration in LOOP.
42 It is expected that neither BASE nor STEP are shared with other expressions 43 It is expected that neither BASE nor STEP are shared with other expressions
43 (unless the sharing rules allow this). Use VAR as a base var_decl for it 44 (unless the sharing rules allow this). Use VAR as a base var_decl for it
44 (if NULL, a new temporary will be created). The increment will occur at 45 (if NULL, a new temporary will be created). The increment will occur at
45 INCR_POS (after it if AFTER is true, before it otherwise). INCR_POS and 46 INCR_POS (after it if AFTER is true, before it otherwise). INCR_POS and
46 AFTER can be computed using standard_iv_increment_position. The ssa versions 47 AFTER can be computed using standard_iv_increment_position. The ssa versions
47 of the variable before and after increment will be stored in VAR_BEFORE and 48 of the variable before and after increment will be stored in VAR_BEFORE and
48 VAR_AFTER (unless they are NULL). */ 49 VAR_AFTER (unless they are NULL). */
49 50
50 void 51 void
97 } 98 }
98 } 99 }
99 } 100 }
100 if (POINTER_TYPE_P (TREE_TYPE (base))) 101 if (POINTER_TYPE_P (TREE_TYPE (base)))
101 { 102 {
103 if (TREE_CODE (base) == ADDR_EXPR)
104 mark_addressable (TREE_OPERAND (base, 0));
102 step = fold_convert (sizetype, step); 105 step = fold_convert (sizetype, step);
103 if (incr_op == MINUS_EXPR) 106 if (incr_op == MINUS_EXPR)
104 step = fold_build1 (NEGATE_EXPR, sizetype, step); 107 step = fold_build1 (NEGATE_EXPR, sizetype, step);
105 incr_op = POINTER_PLUS_EXPR; 108 incr_op = POINTER_PLUS_EXPR;
106 } 109 }
120 if (stmts) 123 if (stmts)
121 gsi_insert_seq_on_edge_immediate (pe, stmts); 124 gsi_insert_seq_on_edge_immediate (pe, stmts);
122 125
123 stmt = create_phi_node (vb, loop->header); 126 stmt = create_phi_node (vb, loop->header);
124 SSA_NAME_DEF_STMT (vb) = stmt; 127 SSA_NAME_DEF_STMT (vb) = stmt;
125 add_phi_arg (stmt, initial, loop_preheader_edge (loop)); 128 add_phi_arg (stmt, initial, loop_preheader_edge (loop), UNKNOWN_LOCATION);
126 add_phi_arg (stmt, va, loop_latch_edge (loop)); 129 add_phi_arg (stmt, va, loop_latch_edge (loop), UNKNOWN_LOCATION);
127 } 130 }
128 131
129 /* Add exit phis for the USE on EXIT. */ 132 /* Add exit phis for the USE on EXIT. */
130 133
131 static void 134 static void
151 154
152 phi = create_phi_node (use, exit); 155 phi = create_phi_node (use, exit);
153 create_new_def_for (gimple_phi_result (phi), phi, 156 create_new_def_for (gimple_phi_result (phi), phi,
154 gimple_phi_result_ptr (phi)); 157 gimple_phi_result_ptr (phi));
155 FOR_EACH_EDGE (e, ei, exit->preds) 158 FOR_EACH_EDGE (e, ei, exit->preds)
156 add_phi_arg (phi, use, e); 159 add_phi_arg (phi, use, e, UNKNOWN_LOCATION);
157 } 160 }
158 161
159 /* Add exit phis for VAR that is used in LIVEIN. 162 /* Add exit phis for VAR that is used in LIVEIN.
160 Exits of the loops are stored in EXITS. */ 163 Exits of the loops are stored in EXITS. */
161 164
274 { 277 {
275 ssa_op_iter iter; 278 ssa_op_iter iter;
276 tree var; 279 tree var;
277 basic_block bb = gimple_bb (stmt); 280 basic_block bb = gimple_bb (stmt);
278 281
282 if (is_gimple_debug (stmt))
283 return;
284
279 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_ALL_USES) 285 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_ALL_USES)
280 find_uses_to_rename_use (bb, var, use_blocks, need_phis); 286 find_uses_to_rename_use (bb, var, use_blocks, need_phis);
281 } 287 }
282 288
283 /* Marks names that are used in BB and outside of the loop they are 289 /* Marks names that are used in BB and outside of the loop they are
294 300
295 FOR_EACH_EDGE (e, ei, bb->succs) 301 FOR_EACH_EDGE (e, ei, bb->succs)
296 for (bsi = gsi_start_phis (e->dest); !gsi_end_p (bsi); gsi_next (&bsi)) 302 for (bsi = gsi_start_phis (e->dest); !gsi_end_p (bsi); gsi_next (&bsi))
297 find_uses_to_rename_use (bb, PHI_ARG_DEF_FROM_EDGE (gsi_stmt (bsi), e), 303 find_uses_to_rename_use (bb, PHI_ARG_DEF_FROM_EDGE (gsi_stmt (bsi), e),
298 use_blocks, need_phis); 304 use_blocks, need_phis);
299 305
300 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi)) 306 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
301 find_uses_to_rename_stmt (gsi_stmt (bsi), use_blocks, need_phis); 307 find_uses_to_rename_stmt (gsi_stmt (bsi), use_blocks, need_phis);
302 } 308 }
303 309
304 /* Marks names that are used outside of the loop they are defined in 310 /* Marks names that are used outside of the loop they are defined in
305 for rewrite. Records the set of blocks in that the ssa 311 for rewrite. Records the set of blocks in that the ssa
306 names are defined to USE_BLOCKS. If CHANGED_BBS is not NULL, 312 names are defined to USE_BLOCKS. If CHANGED_BBS is not NULL,
307 scan only blocks in this set. */ 313 scan only blocks in this set. */
308 314
352 } 358 }
353 359
354 Looking from the outer loop with the normal SSA form, the first use of k 360 Looking from the outer loop with the normal SSA form, the first use of k
355 is not well-behaved, while the second one is an induction variable with 361 is not well-behaved, while the second one is an induction variable with
356 base 99 and step 1. 362 base 99 and step 1.
357 363
358 If CHANGED_BBS is not NULL, we look for uses outside loops only in 364 If CHANGED_BBS is not NULL, we look for uses outside loops only in
359 the basic blocks in this set. 365 the basic blocks in this set.
360 366
361 UPDATE_FLAG is used in the call to update_ssa. See 367 UPDATE_FLAG is used in the call to update_ssa. See
362 TODO_update_ssa* for documentation. */ 368 TODO_update_ssa* for documentation. */
406 static void 412 static void
407 check_loop_closed_ssa_use (basic_block bb, tree use) 413 check_loop_closed_ssa_use (basic_block bb, tree use)
408 { 414 {
409 gimple def; 415 gimple def;
410 basic_block def_bb; 416 basic_block def_bb;
411 417
412 if (TREE_CODE (use) != SSA_NAME || !is_gimple_reg (use)) 418 if (TREE_CODE (use) != SSA_NAME || !is_gimple_reg (use))
413 return; 419 return;
414 420
415 def = SSA_NAME_DEF_STMT (use); 421 def = SSA_NAME_DEF_STMT (use);
416 def_bb = gimple_bb (def); 422 def_bb = gimple_bb (def);
423 static void 429 static void
424 check_loop_closed_ssa_stmt (basic_block bb, gimple stmt) 430 check_loop_closed_ssa_stmt (basic_block bb, gimple stmt)
425 { 431 {
426 ssa_op_iter iter; 432 ssa_op_iter iter;
427 tree var; 433 tree var;
434
435 if (is_gimple_debug (stmt))
436 return;
428 437
429 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_ALL_USES) 438 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_ALL_USES)
430 check_loop_closed_ssa_use (bb, var); 439 check_loop_closed_ssa_use (bb, var);
431 } 440 }
432 441
471 basic_block bb = split_edge (exit); 480 basic_block bb = split_edge (exit);
472 gimple phi, new_phi; 481 gimple phi, new_phi;
473 tree new_name, name; 482 tree new_name, name;
474 use_operand_p op_p; 483 use_operand_p op_p;
475 gimple_stmt_iterator psi; 484 gimple_stmt_iterator psi;
485 source_location locus;
476 486
477 for (psi = gsi_start_phis (dest); !gsi_end_p (psi); gsi_next (&psi)) 487 for (psi = gsi_start_phis (dest); !gsi_end_p (psi); gsi_next (&psi))
478 { 488 {
479 phi = gsi_stmt (psi); 489 phi = gsi_stmt (psi);
480 op_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, single_succ_edge (bb)); 490 op_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, single_succ_edge (bb));
491 locus = gimple_phi_arg_location_from_edge (phi, single_succ_edge (bb));
481 492
482 name = USE_FROM_PTR (op_p); 493 name = USE_FROM_PTR (op_p);
483 494
484 /* If the argument of the PHI node is a constant, we do not need 495 /* If the argument of the PHI node is a constant, we do not need
485 to keep it inside loop. */ 496 to keep it inside loop. */
489 /* Otherwise create an auxiliary phi node that will copy the value 500 /* Otherwise create an auxiliary phi node that will copy the value
490 of the SSA name out of the loop. */ 501 of the SSA name out of the loop. */
491 new_name = duplicate_ssa_name (name, NULL); 502 new_name = duplicate_ssa_name (name, NULL);
492 new_phi = create_phi_node (new_name, bb); 503 new_phi = create_phi_node (new_name, bb);
493 SSA_NAME_DEF_STMT (new_name) = new_phi; 504 SSA_NAME_DEF_STMT (new_name) = new_phi;
494 add_phi_arg (new_phi, name, exit); 505 add_phi_arg (new_phi, name, exit, locus);
495 SET_USE (op_p, new_name); 506 SET_USE (op_p, new_name);
496 } 507 }
497 508
498 return bb; 509 return bb;
499 } 510 }
805 /* Unroll LOOP FACTOR times. DESC describes number of iterations of LOOP. 816 /* Unroll LOOP FACTOR times. DESC describes number of iterations of LOOP.
806 EXIT is the exit of the loop to that DESC corresponds. 817 EXIT is the exit of the loop to that DESC corresponds.
807 818
808 If N is number of iterations of the loop and MAY_BE_ZERO is the condition 819 If N is number of iterations of the loop and MAY_BE_ZERO is the condition
809 under that loop exits in the first iteration even if N != 0, 820 under that loop exits in the first iteration even if N != 0,
810 821
811 while (1) 822 while (1)
812 { 823 {
813 x = phi (init, next); 824 x = phi (init, next);
814 825
815 pre; 826 pre;
818 post; 829 post;
819 } 830 }
820 831
821 becomes (with possibly the exit conditions formulated a bit differently, 832 becomes (with possibly the exit conditions formulated a bit differently,
822 avoiding the need to create a new iv): 833 avoiding the need to create a new iv):
823 834
824 if (MAY_BE_ZERO || N < FACTOR) 835 if (MAY_BE_ZERO || N < FACTOR)
825 goto rest; 836 goto rest;
826 837
827 do 838 do
828 { 839 {
834 post; 845 post;
835 ... 846 ...
836 pre; 847 pre;
837 post; 848 post;
838 N -= FACTOR; 849 N -= FACTOR;
839 850
840 } while (N >= FACTOR); 851 } while (N >= FACTOR);
841 852
842 rest: 853 rest:
843 init' = phi (init, x); 854 init' = phi (init, x);
844 855
849 pre; 860 pre;
850 if (st) 861 if (st)
851 break; 862 break;
852 post; 863 post;
853 } 864 }
854 865
855 Before the loop is unrolled, TRANSFORM is called for it (only for the 866 Before the loop is unrolled, TRANSFORM is called for it (only for the
856 unrolled loop, but not for its versioned copy). DATA is passed to 867 unrolled loop, but not for its versioned copy). DATA is passed to
857 TRANSFORM. */ 868 TRANSFORM. */
858 869
859 /* Probability in % that the unrolled loop is entered. Just a guess. */ 870 /* Probability in % that the unrolled loop is entered. Just a guess. */
986 next = PHI_ARG_DEF_FROM_EDGE (phi_old_loop, old_latch); 997 next = PHI_ARG_DEF_FROM_EDGE (phi_old_loop, old_latch);
987 998
988 /* Prefer using original variable as a base for the new ssa name. 999 /* Prefer using original variable as a base for the new ssa name.
989 This is necessary for virtual ops, and useful in order to avoid 1000 This is necessary for virtual ops, and useful in order to avoid
990 losing debug info for real ops. */ 1001 losing debug info for real ops. */
991 if (TREE_CODE (next) == SSA_NAME) 1002 if (TREE_CODE (next) == SSA_NAME
1003 && useless_type_conversion_p (TREE_TYPE (next),
1004 TREE_TYPE (init)))
992 var = SSA_NAME_VAR (next); 1005 var = SSA_NAME_VAR (next);
993 else if (TREE_CODE (init) == SSA_NAME) 1006 else if (TREE_CODE (init) == SSA_NAME
1007 && useless_type_conversion_p (TREE_TYPE (init),
1008 TREE_TYPE (next)))
994 var = SSA_NAME_VAR (init); 1009 var = SSA_NAME_VAR (init);
1010 else if (useless_type_conversion_p (TREE_TYPE (next), TREE_TYPE (init)))
1011 {
1012 var = create_tmp_var (TREE_TYPE (next), "unrinittmp");
1013 add_referenced_var (var);
1014 }
995 else 1015 else
996 { 1016 {
997 var = create_tmp_var (TREE_TYPE (init), "unrinittmp"); 1017 var = create_tmp_var (TREE_TYPE (init), "unrinittmp");
998 add_referenced_var (var); 1018 add_referenced_var (var);
999 } 1019 }
1000 1020
1001 new_init = make_ssa_name (var, NULL); 1021 new_init = make_ssa_name (var, NULL);
1002 phi_rest = create_phi_node (new_init, rest); 1022 phi_rest = create_phi_node (new_init, rest);
1003 SSA_NAME_DEF_STMT (new_init) = phi_rest; 1023 SSA_NAME_DEF_STMT (new_init) = phi_rest;
1004 1024
1005 add_phi_arg (phi_rest, init, precond_edge); 1025 add_phi_arg (phi_rest, init, precond_edge, UNKNOWN_LOCATION);
1006 add_phi_arg (phi_rest, next, new_exit); 1026 add_phi_arg (phi_rest, next, new_exit, UNKNOWN_LOCATION);
1007 SET_USE (op, new_init); 1027 SET_USE (op, new_init);
1008 } 1028 }
1009 1029
1010 remove_path (exit); 1030 remove_path (exit);
1011 1031
1087 edge exit, struct tree_niter_desc *desc) 1107 edge exit, struct tree_niter_desc *desc)
1088 { 1108 {
1089 tree_transform_and_unroll_loop (loop, factor, exit, desc, 1109 tree_transform_and_unroll_loop (loop, factor, exit, desc,
1090 NULL, NULL); 1110 NULL, NULL);
1091 } 1111 }
1112
1113 /* Rewrite the phi node at position PSI in function of the main
1114 induction variable MAIN_IV and insert the generated code at GSI. */
1115
1116 static void
1117 rewrite_phi_with_iv (loop_p loop,
1118 gimple_stmt_iterator *psi,
1119 gimple_stmt_iterator *gsi,
1120 tree main_iv)
1121 {
1122 affine_iv iv;
1123 gimple stmt, phi = gsi_stmt (*psi);
1124 tree atype, mtype, val, res = PHI_RESULT (phi);
1125
1126 if (!is_gimple_reg (res) || res == main_iv)
1127 {
1128 gsi_next (psi);
1129 return;
1130 }
1131
1132 if (!simple_iv (loop, loop, res, &iv, true))
1133 {
1134 gsi_next (psi);
1135 return;
1136 }
1137
1138 remove_phi_node (psi, false);
1139
1140 atype = TREE_TYPE (res);
1141 mtype = POINTER_TYPE_P (atype) ? sizetype : atype;
1142 val = fold_build2 (MULT_EXPR, mtype, unshare_expr (iv.step),
1143 fold_convert (mtype, main_iv));
1144 val = fold_build2 (POINTER_TYPE_P (atype)
1145 ? POINTER_PLUS_EXPR : PLUS_EXPR,
1146 atype, unshare_expr (iv.base), val);
1147 val = force_gimple_operand_gsi (gsi, val, false, NULL_TREE, true,
1148 GSI_SAME_STMT);
1149 stmt = gimple_build_assign (res, val);
1150 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1151 SSA_NAME_DEF_STMT (res) = stmt;
1152 }
1153
1154 /* Rewrite all the phi nodes of LOOP in function of the main induction
1155 variable MAIN_IV. */
1156
1157 static void
1158 rewrite_all_phi_nodes_with_iv (loop_p loop, tree main_iv)
1159 {
1160 unsigned i;
1161 basic_block *bbs = get_loop_body_in_dom_order (loop);
1162 gimple_stmt_iterator psi;
1163
1164 for (i = 0; i < loop->num_nodes; i++)
1165 {
1166 basic_block bb = bbs[i];
1167 gimple_stmt_iterator gsi = gsi_after_labels (bb);
1168
1169 if (bb->loop_father != loop)
1170 continue;
1171
1172 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); )
1173 rewrite_phi_with_iv (loop, &psi, &gsi, main_iv);
1174 }
1175
1176 free (bbs);
1177 }
1178
1179 /* Bases all the induction variables in LOOP on a single induction
1180 variable (unsigned with base 0 and step 1), whose final value is
1181 compared with *NIT. When the IV type precision has to be larger
1182 than *NIT type precision, *NIT is converted to the larger type, the
1183 conversion code is inserted before the loop, and *NIT is updated to
1184 the new definition. The induction variable is incremented in the
1185 loop latch. Return the induction variable that was created. */
1186
1187 tree
1188 canonicalize_loop_ivs (struct loop *loop, tree *nit)
1189 {
1190 unsigned precision = TYPE_PRECISION (TREE_TYPE (*nit));
1191 unsigned original_precision = precision;
1192 tree type, var_before;
1193 gimple_stmt_iterator gsi, psi;
1194 gimple stmt;
1195 edge exit = single_dom_exit (loop);
1196 gimple_seq stmts;
1197
1198 for (psi = gsi_start_phis (loop->header);
1199 !gsi_end_p (psi); gsi_next (&psi))
1200 {
1201 gimple phi = gsi_stmt (psi);
1202 tree res = PHI_RESULT (phi);
1203
1204 if (is_gimple_reg (res) && TYPE_PRECISION (TREE_TYPE (res)) > precision)
1205 precision = TYPE_PRECISION (TREE_TYPE (res));
1206 }
1207
1208 type = lang_hooks.types.type_for_size (precision, 1);
1209
1210 if (original_precision != precision)
1211 {
1212 *nit = fold_convert (type, *nit);
1213 *nit = force_gimple_operand (*nit, &stmts, true, NULL_TREE);
1214 if (stmts)
1215 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
1216 }
1217
1218 gsi = gsi_last_bb (loop->latch);
1219 create_iv (build_int_cst_type (type, 0), build_int_cst (type, 1), NULL_TREE,
1220 loop, &gsi, true, &var_before, NULL);
1221
1222 rewrite_all_phi_nodes_with_iv (loop, var_before);
1223
1224 stmt = last_stmt (exit->src);
1225 /* Make the loop exit if the control condition is not satisfied. */
1226 if (exit->flags & EDGE_TRUE_VALUE)
1227 {
1228 edge te, fe;
1229
1230 extract_true_false_edges_from_block (exit->src, &te, &fe);
1231 te->flags = EDGE_FALSE_VALUE;
1232 fe->flags = EDGE_TRUE_VALUE;
1233 }
1234 gimple_cond_set_code (stmt, LT_EXPR);
1235 gimple_cond_set_lhs (stmt, var_before);
1236 gimple_cond_set_rhs (stmt, *nit);
1237 update_stmt (stmt);
1238
1239 return var_before;
1240 }