00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "config.h"
00022 #include "system.h"
00023 #include "coretypes.h"
00024 #include "tm.h"
00025 #include "errors.h"
00026 #include "ggc.h"
00027 #include "tree.h"
00028 #include "rtl.h"
00029 #include "tm_p.h"
00030 #include "basic-block.h"
00031 #include "timevar.h"
00032 #include "diagnostic.h"
00033 #include "tree-flow.h"
00034 #include "tree-pass.h"
00035 #include "tree-dump.h"
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114 static bitmap vars;
00115
00116 static bool need_imm_uses_for (tree);
00117 static void tree_ssa_forward_propagate_single_use_vars (void);
00118 static void record_single_argument_cond_exprs (varray_type,
00119 varray_type *,
00120 bitmap);
00121 static void substitute_single_use_vars (varray_type *, varray_type);
00122
00123
00124
00125
00126 static bool
00127 need_imm_uses_for (tree var)
00128 {
00129 return bitmap_bit_p (vars, SSA_NAME_VERSION (var));
00130 }
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143 static void
00144 record_single_argument_cond_exprs (varray_type cond_worklist,
00145 varray_type *vars_worklist,
00146 bitmap vars)
00147
00148 {
00149
00150
00151
00152
00153
00154 while (VARRAY_ACTIVE_SIZE (cond_worklist) > 0)
00155 {
00156 tree last = VARRAY_TOP_TREE (cond_worklist);
00157
00158 VARRAY_POP (cond_worklist);
00159
00160
00161 if (last && TREE_CODE (last) == COND_EXPR)
00162 {
00163 tree cond = COND_EXPR_COND (last);
00164 enum tree_code cond_code = TREE_CODE (cond);
00165
00166
00167
00168
00169
00170
00171
00172 if (cond_code == SSA_NAME
00173 || ((cond_code == EQ_EXPR || cond_code == NE_EXPR)
00174 && TREE_CODE (TREE_OPERAND (cond, 0)) == SSA_NAME
00175 && CONSTANT_CLASS_P (TREE_OPERAND (cond, 1))
00176 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (cond, 1)))))
00177 {
00178 tree def;
00179 tree test_var;
00180
00181
00182 if (cond_code == SSA_NAME)
00183 test_var = cond;
00184 else
00185 test_var = TREE_OPERAND (cond, 0);
00186
00187
00188
00189 if (bitmap_bit_p (vars, SSA_NAME_VERSION (test_var)))
00190 continue;
00191
00192
00193
00194 def = SSA_NAME_DEF_STMT (test_var);
00195 if (TREE_CODE (def) == MODIFY_EXPR)
00196 {
00197 tree def_rhs = TREE_OPERAND (def, 1);
00198
00199
00200
00201
00202
00203 if (TREE_CODE (def_rhs) == PLUS_EXPR
00204 || TREE_CODE (def_rhs) == MINUS_EXPR)
00205 {
00206 tree op0 = TREE_OPERAND (def_rhs, 0);
00207 tree op1 = TREE_OPERAND (def_rhs, 1);
00208
00209
00210
00211 if (TREE_CODE (op0) != SSA_NAME
00212 || !CONSTANT_CLASS_P (op1)
00213 || !INTEGRAL_TYPE_P (TREE_TYPE (op1)))
00214 continue;
00215
00216
00217
00218 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0))
00219 continue;
00220 }
00221
00222
00223
00224 else if (TREE_CODE (cond) == SSA_NAME
00225 || integer_zerop (TREE_OPERAND (cond, 1))
00226 || integer_onep (TREE_OPERAND (cond, 1)))
00227 {
00228
00229
00230
00231 if (COMPARISON_CLASS_P (def_rhs))
00232 {
00233 tree op0 = TREE_OPERAND (def_rhs, 0);
00234 tree op1 = TREE_OPERAND (def_rhs, 1);
00235
00236
00237
00238 if ((TREE_CODE (op0) != SSA_NAME
00239 && !is_gimple_min_invariant (op0))
00240 || (TREE_CODE (op1) != SSA_NAME
00241 && !is_gimple_min_invariant (op1)))
00242 continue;
00243
00244
00245
00246 if (TREE_CODE (op0) == SSA_NAME
00247 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0))
00248 continue;
00249
00250
00251
00252 if (TREE_CODE (op1) == SSA_NAME
00253 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op1))
00254 continue;
00255 }
00256
00257
00258
00259 else if (TREE_CODE (def_rhs) == TRUTH_NOT_EXPR)
00260 {
00261 def_rhs = TREE_OPERAND (def_rhs, 0);
00262
00263
00264 if (TREE_CODE (def_rhs) != SSA_NAME
00265 && !is_gimple_min_invariant (def_rhs))
00266 continue;
00267
00268
00269
00270 if (TREE_CODE (def_rhs) == SSA_NAME
00271 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def_rhs))
00272 continue;
00273 }
00274
00275
00276
00277
00278 else if (TREE_CODE (def_rhs) == NOP_EXPR
00279 || TREE_CODE (def_rhs) == CONVERT_EXPR)
00280 {
00281 tree outer_type;
00282 tree inner_type;
00283
00284 outer_type = TREE_TYPE (def_rhs);
00285 inner_type = TREE_TYPE (TREE_OPERAND (def_rhs, 0));
00286
00287 if ((TREE_CODE (outer_type) == BOOLEAN_TYPE
00288 && INTEGRAL_TYPE_P (inner_type))
00289 || (TREE_CODE (inner_type) == BOOLEAN_TYPE
00290 && INTEGRAL_TYPE_P (outer_type)))
00291 ;
00292 else
00293 continue;
00294
00295
00296
00297 if (TREE_CODE (TREE_OPERAND (def_rhs, 0)) == SSA_NAME
00298 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND
00299 (def_rhs, 0)))
00300 continue;
00301 }
00302 else
00303 continue;
00304 }
00305 else
00306 continue;
00307
00308
00309 VARRAY_PUSH_TREE (*vars_worklist, test_var);
00310 bitmap_set_bit (vars, SSA_NAME_VERSION (test_var));
00311 }
00312 }
00313 }
00314 }
00315 }
00316
00317
00318
00319
00320
00321
00322 static void
00323 substitute_single_use_vars (varray_type *cond_worklist,
00324 varray_type vars_worklist)
00325 {
00326 while (VARRAY_ACTIVE_SIZE (vars_worklist) > 0)
00327 {
00328 tree test_var = VARRAY_TOP_TREE (vars_worklist);
00329 tree def = SSA_NAME_DEF_STMT (test_var);
00330 dataflow_t df;
00331 int j, num_uses, propagated_uses;
00332
00333 VARRAY_POP (vars_worklist);
00334
00335
00336 df = get_immediate_uses (def);
00337 num_uses = num_immediate_uses (df);
00338 propagated_uses = 0;
00339
00340
00341
00342
00343 if (num_uses == 1
00344 || (TREE_CODE (TREE_TYPE (test_var)) == BOOLEAN_TYPE
00345 && TREE_CODE (TREE_OPERAND (def, 1)) == TRUTH_NOT_EXPR
00346 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (def, 1), 0))
00347 == SSA_NAME)))
00348 ;
00349 else
00350 continue;
00351
00352
00353
00354 for (j = 0; j < num_uses; j++)
00355 {
00356 tree cond_stmt;
00357 tree cond;
00358 enum tree_code cond_code;
00359 tree def_rhs;
00360 enum tree_code def_rhs_code;
00361 tree new_cond;
00362
00363 cond_stmt = immediate_use (df, j);
00364
00365
00366 if (TREE_CODE (cond_stmt) != COND_EXPR)
00367 continue;
00368
00369 cond = COND_EXPR_COND (cond_stmt);
00370 cond_code = TREE_CODE (cond);
00371 def_rhs = TREE_OPERAND (def, 1);
00372 def_rhs_code = TREE_CODE (def_rhs);
00373
00374
00375
00376
00377 if (def_rhs_code == PLUS_EXPR || def_rhs_code == MINUS_EXPR)
00378 {
00379 tree op0 = TREE_OPERAND (def_rhs, 0);
00380 tree op1 = TREE_OPERAND (def_rhs, 1);
00381 enum tree_code new_code;
00382 tree t;
00383
00384
00385
00386
00387
00388 new_code = def_rhs_code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR;
00389 t = int_const_binop (new_code, TREE_OPERAND (cond, 1), op1, 0);
00390 if (!is_gimple_val (t))
00391 continue;
00392
00393 new_cond = build (cond_code, boolean_type_node, op0, t);
00394 }
00395
00396 else if (TREE_CODE_CLASS (def_rhs_code) == tcc_comparison)
00397 {
00398
00399 tree op0 = TREE_OPERAND (def_rhs, 0);
00400 tree op1 = TREE_OPERAND (def_rhs, 1);
00401
00402 new_cond = build (def_rhs_code, boolean_type_node, op0, op1);
00403
00404
00405 if ((cond_code == EQ_EXPR
00406 && integer_zerop (TREE_OPERAND (cond, 1)))
00407 || (cond_code == NE_EXPR
00408 && integer_onep (TREE_OPERAND (cond, 1))))
00409 {
00410 new_cond = invert_truthvalue (new_cond);
00411
00412
00413
00414 if (!COMPARISON_CLASS_P (new_cond)
00415 && TREE_CODE (new_cond) != SSA_NAME)
00416 continue;
00417 }
00418 }
00419 else
00420 {
00421 bool invert = false;
00422 enum tree_code new_code;
00423 tree new_arg;
00424
00425
00426 if (def_rhs_code == TRUTH_NOT_EXPR)
00427 invert = true;
00428
00429
00430
00431 if ((cond_code == NE_EXPR || cond_code == EQ_EXPR)
00432 && TREE_CODE (TREE_OPERAND (cond, 1)) != INTEGER_CST)
00433 continue;
00434
00435 if (cond_code == SSA_NAME
00436 || (cond_code == NE_EXPR
00437 && integer_zerop (TREE_OPERAND (cond, 1)))
00438 || (cond_code == EQ_EXPR
00439 && integer_onep (TREE_OPERAND (cond, 1))))
00440 new_code = NE_EXPR;
00441 else
00442 new_code = EQ_EXPR;
00443
00444 if (invert)
00445 new_code = (new_code == EQ_EXPR ? NE_EXPR : EQ_EXPR);
00446
00447 new_arg = TREE_OPERAND (def_rhs, 0);
00448 new_cond = build2 (new_code, boolean_type_node, new_arg,
00449 fold_convert (TREE_TYPE (new_arg),
00450 integer_zero_node));
00451 }
00452
00453
00454 if (dump_file && (dump_flags & TDF_DETAILS))
00455 {
00456 fprintf (dump_file, " Replaced '");
00457 print_generic_expr (dump_file, cond, dump_flags);
00458 fprintf (dump_file, "' with '");
00459 print_generic_expr (dump_file, new_cond, dump_flags);
00460 fprintf (dump_file, "'\n");
00461 }
00462
00463
00464 COND_EXPR_COND (cond_stmt) = new_cond;
00465 modify_stmt (cond_stmt);
00466 propagated_uses++;
00467 VARRAY_PUSH_TREE (*cond_worklist, cond_stmt);
00468 }
00469
00470
00471
00472
00473 if (num_uses && num_uses == propagated_uses)
00474 {
00475 block_stmt_iterator bsi = bsi_for_stmt (def);
00476 bsi_remove (&bsi);
00477 }
00478 }
00479 }
00480
00481
00482
00483 static void
00484 tree_ssa_forward_propagate_single_use_vars (void)
00485 {
00486 basic_block bb;
00487 varray_type vars_worklist, cond_worklist;
00488
00489 vars = BITMAP_ALLOC (NULL);
00490 VARRAY_TREE_INIT (vars_worklist, 10, "VARS worklist");
00491 VARRAY_TREE_INIT (cond_worklist, 10, "COND worklist");
00492
00493
00494
00495 FOR_EACH_BB (bb)
00496 {
00497 tree last = last_stmt (bb);
00498 if (last && TREE_CODE (last) == COND_EXPR)
00499 VARRAY_PUSH_TREE (cond_worklist, last);
00500 }
00501
00502 while (VARRAY_ACTIVE_SIZE (cond_worklist) > 0)
00503 {
00504
00505
00506
00507 record_single_argument_cond_exprs (cond_worklist, &vars_worklist, vars);
00508
00509 if (VARRAY_ACTIVE_SIZE (vars_worklist) > 0)
00510 {
00511
00512 compute_immediate_uses (TDFA_USE_OPS, need_imm_uses_for);
00513
00514
00515
00516 bitmap_clear (vars);
00517
00518
00519
00520 substitute_single_use_vars (&cond_worklist, vars_worklist);
00521
00522
00523
00524
00525
00526 free_df ();
00527 }
00528 }
00529
00530
00531 BITMAP_FREE (vars);
00532 }
00533
00534
00535 static bool
00536 gate_forwprop (void)
00537 {
00538 return 1;
00539 }
00540
00541 struct tree_opt_pass pass_forwprop = {
00542 "forwprop",
00543 gate_forwprop,
00544 tree_ssa_forward_propagate_single_use_vars,
00545 NULL,
00546 NULL,
00547 0,
00548 TV_TREE_FORWPROP,
00549 PROP_cfg | PROP_ssa
00550 | PROP_alias,
00551 0,
00552 0,
00553 0,
00554 TODO_dump_func | TODO_ggc_collect
00555 | TODO_verify_ssa,
00556 0
00557 };