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 "flags.h"
00030 #include "tm_p.h"
00031 #include "basic-block.h"
00032 #include "timevar.h"
00033 #include "diagnostic.h"
00034 #include "tree-flow.h"
00035 #include "tree-pass.h"
00036 #include "tree-dump.h"
00037 #include "langhooks.h"
00038
00039 static void tree_ssa_phiopt (void);
00040 static bool conditional_replacement (basic_block, tree, tree, tree);
00041 static bool value_replacement (basic_block, tree, tree, tree);
00042 static bool abs_replacement (basic_block, tree, tree, tree);
00043 static void replace_phi_with_stmt (block_stmt_iterator, basic_block,
00044 basic_block, tree, tree);
00045 static bool candidate_bb_for_phi_optimization (basic_block,
00046 basic_block *,
00047 basic_block *);
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 static void
00110 tree_ssa_phiopt (void)
00111 {
00112 basic_block bb;
00113 bool removed_phis = false;
00114
00115
00116 FOR_EACH_BB (bb)
00117 {
00118 tree arg0, arg1, phi;
00119
00120
00121
00122 phi = phi_nodes (bb);
00123 if (phi && PHI_CHAIN (phi) == NULL
00124 && PHI_NUM_ARGS (phi) == 2)
00125 {
00126 arg0 = PHI_ARG_DEF (phi, 0);
00127 arg1 = PHI_ARG_DEF (phi, 1);
00128
00129
00130 if (conditional_replacement (bb, phi, arg0, arg1)
00131 || value_replacement (bb, phi, arg0, arg1)
00132 || abs_replacement (bb, phi, arg0, arg1))
00133 {
00134
00135
00136 removed_phis = true;
00137 }
00138 }
00139 }
00140 }
00141
00142
00143
00144 bool
00145 empty_block_p (basic_block bb)
00146 {
00147 block_stmt_iterator bsi;
00148
00149
00150 bsi = bsi_start (bb);
00151 while (!bsi_end_p (bsi)
00152 && (TREE_CODE (bsi_stmt (bsi)) == LABEL_EXPR
00153 || IS_EMPTY_STMT (bsi_stmt (bsi))))
00154 bsi_next (&bsi);
00155
00156 if (!bsi_end_p (bsi))
00157 return false;
00158
00159 return true;
00160 }
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 static bool
00174 candidate_bb_for_phi_optimization (basic_block bb,
00175 basic_block *cond_block_p,
00176 basic_block *other_block_p)
00177 {
00178 tree last0, last1;
00179 basic_block cond_block, other_block;
00180
00181
00182
00183 last0 = last_stmt (EDGE_PRED (bb, 0)->src);
00184 last1 = last_stmt (EDGE_PRED (bb, 1)->src);
00185 if (last0 && TREE_CODE (last0) == COND_EXPR)
00186 {
00187 cond_block = EDGE_PRED (bb, 0)->src;
00188 other_block = EDGE_PRED (bb, 1)->src;
00189 }
00190 else if (last1 && TREE_CODE (last1) == COND_EXPR)
00191 {
00192 other_block = EDGE_PRED (bb, 0)->src;
00193 cond_block = EDGE_PRED (bb, 1)->src;
00194 }
00195 else
00196 return false;
00197
00198
00199
00200 if (EDGE_COUNT (cond_block->succs) != 2
00201 || (EDGE_SUCC (cond_block, 0)->flags & EDGE_ABNORMAL) != 0
00202 || (EDGE_SUCC (cond_block, 1)->flags & EDGE_ABNORMAL) != 0)
00203 return false;
00204
00205
00206
00207
00208 if (EDGE_COUNT (other_block->preds) != 1
00209 || EDGE_PRED (other_block, 0)->src != cond_block
00210 || EDGE_COUNT (other_block->succs) != 1
00211 || EDGE_SUCC (other_block, 0)->dest != bb
00212 || phi_nodes (other_block))
00213 return false;
00214
00215 *cond_block_p = cond_block;
00216 *other_block_p = other_block;
00217
00218 return true;
00219 }
00220
00221
00222
00223
00224
00225 static void
00226 replace_phi_with_stmt (block_stmt_iterator bsi, basic_block bb,
00227 basic_block cond_block, tree phi, tree new)
00228 {
00229 basic_block block_to_remove;
00230
00231
00232 bsi_insert_after (&bsi, new, BSI_NEW_STMT);
00233
00234
00235
00236 SSA_NAME_DEF_STMT (PHI_RESULT (phi)) = new;
00237
00238
00239
00240
00241
00242 release_phi_node (phi);
00243 bb_ann (bb)->phi_nodes = NULL;
00244
00245
00246 if (EDGE_SUCC (cond_block, 0)->dest == bb)
00247 {
00248 EDGE_SUCC (cond_block, 0)->flags |= EDGE_FALLTHRU;
00249 EDGE_SUCC (cond_block, 0)->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
00250
00251 block_to_remove = EDGE_SUCC (cond_block, 1)->dest;
00252 }
00253 else
00254 {
00255 EDGE_SUCC (cond_block, 1)->flags |= EDGE_FALLTHRU;
00256 EDGE_SUCC (cond_block, 1)->flags
00257 &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
00258
00259 block_to_remove = EDGE_SUCC (cond_block, 0)->dest;
00260 }
00261 delete_basic_block (block_to_remove);
00262
00263
00264 bsi = bsi_last (cond_block);
00265 bsi_remove (&bsi);
00266
00267 if (dump_file && (dump_flags & TDF_DETAILS))
00268 fprintf (dump_file,
00269 "COND_EXPR in block %d and PHI in block %d converted to straightline code.\n",
00270 cond_block->index,
00271 bb->index);
00272 }
00273
00274
00275
00276
00277
00278
00279
00280 static bool
00281 conditional_replacement (basic_block bb, tree phi, tree arg0, tree arg1)
00282 {
00283 tree result;
00284 tree old_result = NULL;
00285 basic_block other_block = NULL;
00286 basic_block cond_block = NULL;
00287 tree new, cond;
00288 block_stmt_iterator bsi;
00289 edge true_edge, false_edge;
00290 tree new_var = NULL;
00291
00292
00293
00294 if ((integer_zerop (arg0) && integer_onep (arg1))
00295 || (integer_zerop (arg1) && integer_onep (arg0)))
00296 ;
00297 else
00298 return false;
00299
00300 if (!candidate_bb_for_phi_optimization (bb, &cond_block, &other_block)
00301 || !empty_block_p (other_block))
00302 return false;
00303
00304
00305
00306
00307
00308
00309 cond = COND_EXPR_COND (last_stmt (cond_block));
00310 result = PHI_RESULT (phi);
00311 if (TREE_CODE (cond) != SSA_NAME
00312 && !lang_hooks.types_compatible_p (TREE_TYPE (cond), TREE_TYPE (result)))
00313 {
00314 new_var = make_rename_temp (TREE_TYPE (cond), NULL);
00315 old_result = cond;
00316 cond = new_var;
00317 }
00318
00319
00320
00321
00322 if (!lang_hooks.types_compatible_p (TREE_TYPE (cond), TREE_TYPE (result)))
00323 cond = fold_convert (TREE_TYPE (result), cond);
00324
00325
00326
00327 extract_true_false_edges_from_block (cond_block, &true_edge, &false_edge);
00328
00329
00330 bsi = bsi_after_labels (bb);
00331
00332 if (old_result)
00333 {
00334 tree new1;
00335 if (!COMPARISON_CLASS_P (old_result))
00336 return false;
00337
00338 new1 = build (TREE_CODE (old_result), TREE_TYPE (old_result),
00339 TREE_OPERAND (old_result, 0),
00340 TREE_OPERAND (old_result, 1));
00341
00342 new1 = build (MODIFY_EXPR, TREE_TYPE (old_result), new_var, new1);
00343 bsi_insert_after (&bsi, new1, BSI_NEW_STMT);
00344 }
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363 if ((PHI_ARG_EDGE (phi, 0) == true_edge && integer_onep (arg0))
00364 || (PHI_ARG_EDGE (phi, 0) == false_edge && integer_zerop (arg0))
00365 || (PHI_ARG_EDGE (phi, 1) == true_edge && integer_onep (arg1))
00366 || (PHI_ARG_EDGE (phi, 1) == false_edge && integer_zerop (arg1)))
00367 {
00368 new = build (MODIFY_EXPR, TREE_TYPE (PHI_RESULT (phi)),
00369 PHI_RESULT (phi), cond);
00370 }
00371 else
00372 {
00373 tree cond1 = invert_truthvalue (cond);
00374
00375 cond = cond1;
00376
00377
00378 if (TREE_CODE (cond) == COND_EXPR)
00379 return false;
00380
00381
00382
00383 if (is_gimple_cast (cond)
00384 && !is_gimple_val (TREE_OPERAND (cond, 0)))
00385 {
00386 tree temp = TREE_OPERAND (cond, 0);
00387 tree new_var_1 = make_rename_temp (TREE_TYPE (temp), NULL);
00388 new = build (MODIFY_EXPR, TREE_TYPE (new_var_1), new_var_1, temp);
00389 bsi_insert_after (&bsi, new, BSI_NEW_STMT);
00390 cond = fold_convert (TREE_TYPE (result), new_var_1);
00391 }
00392
00393 if (TREE_CODE (cond) == TRUTH_NOT_EXPR
00394 && !is_gimple_val (TREE_OPERAND (cond, 0)))
00395 return false;
00396
00397 new = build (MODIFY_EXPR, TREE_TYPE (PHI_RESULT (phi)),
00398 PHI_RESULT (phi), cond);
00399 }
00400
00401 replace_phi_with_stmt (bsi, bb, cond_block, phi, new);
00402
00403
00404 return true;
00405 }
00406
00407
00408
00409
00410
00411
00412
00413 static bool
00414 value_replacement (basic_block bb, tree phi, tree arg0, tree arg1)
00415 {
00416 tree result;
00417 basic_block other_block = NULL;
00418 basic_block cond_block = NULL;
00419 tree new, cond;
00420 edge true_edge, false_edge;
00421
00422
00423
00424 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
00425 return false;
00426
00427 if (!candidate_bb_for_phi_optimization (bb, &cond_block, &other_block)
00428 || !empty_block_p (other_block))
00429 return false;
00430
00431 cond = COND_EXPR_COND (last_stmt (cond_block));
00432 result = PHI_RESULT (phi);
00433
00434
00435 if (TREE_CODE (cond) != NE_EXPR && TREE_CODE (cond) != EQ_EXPR)
00436 return false;
00437
00438
00439
00440 extract_true_false_edges_from_block (cond_block, &true_edge, &false_edge);
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453 if ((operand_equal_for_phi_arg_p (arg0, TREE_OPERAND (cond, 0))
00454 && operand_equal_for_phi_arg_p (arg1, TREE_OPERAND (cond, 1)))
00455 || (operand_equal_for_phi_arg_p (arg1, TREE_OPERAND (cond, 0))
00456 && operand_equal_for_phi_arg_p (arg0, TREE_OPERAND (cond, 1))))
00457 {
00458 edge e;
00459 tree arg;
00460
00461
00462
00463
00464 e = (TREE_CODE (cond) == NE_EXPR ? true_edge : false_edge);
00465
00466
00467
00468
00469
00470 if (e->dest == other_block)
00471 e = EDGE_SUCC (e->dest, 0);
00472
00473
00474
00475 if (PHI_ARG_EDGE (phi, 0) == e)
00476 arg = arg0;
00477 else
00478 arg = arg1;
00479
00480
00481 new = build (MODIFY_EXPR, TREE_TYPE (result), result, arg);
00482
00483 replace_phi_with_stmt (bsi_after_labels (bb), bb, cond_block, phi, new);
00484
00485
00486 return true;
00487 }
00488 return false;
00489 }
00490
00491
00492
00493
00494
00495
00496 static bool
00497 abs_replacement (basic_block bb, tree phi, tree arg0, tree arg1)
00498 {
00499 tree result;
00500 basic_block other_block = NULL;
00501 basic_block cond_block = NULL;
00502 tree new, cond;
00503 block_stmt_iterator bsi;
00504 edge true_edge, false_edge;
00505 tree assign = NULL;
00506 edge e;
00507 tree rhs = NULL, lhs = NULL;
00508 bool negate;
00509 enum tree_code cond_code;
00510
00511
00512
00513 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
00514 return false;
00515
00516 if (!candidate_bb_for_phi_optimization (bb, &cond_block, &other_block))
00517 return false;
00518
00519
00520
00521 bsi = bsi_start (other_block);
00522 while (!bsi_end_p (bsi))
00523 {
00524 tree stmt = bsi_stmt (bsi);
00525
00526
00527 if (TREE_CODE (stmt) == LABEL_EXPR
00528 || IS_EMPTY_STMT (stmt))
00529 {
00530 bsi_next (&bsi);
00531 continue;
00532 }
00533
00534
00535
00536 if (assign)
00537 return false;
00538
00539
00540
00541
00542 if (TREE_CODE (stmt) == MODIFY_EXPR)
00543 {
00544 lhs = TREE_OPERAND (stmt, 0);
00545 rhs = TREE_OPERAND (stmt, 1);
00546
00547 if (TREE_CODE (rhs) == NEGATE_EXPR)
00548 {
00549 rhs = TREE_OPERAND (rhs, 0);
00550
00551
00552 if ((lhs == arg0 && rhs == arg1)
00553 || (lhs == arg1 && rhs == arg0))
00554 {
00555 assign = stmt;
00556 bsi_next (&bsi);
00557 }
00558 else
00559 return false;
00560 }
00561 else
00562 return false;
00563 }
00564 else
00565 return false;
00566 }
00567
00568
00569
00570 if (assign == NULL)
00571 return false;
00572
00573 cond = COND_EXPR_COND (last_stmt (cond_block));
00574 result = PHI_RESULT (phi);
00575
00576
00577 cond_code = TREE_CODE (cond);
00578 if (cond_code != GT_EXPR && cond_code != GE_EXPR
00579 && cond_code != LT_EXPR && cond_code != LE_EXPR)
00580 return false;
00581
00582
00583 if (TREE_OPERAND (cond, 0) != rhs)
00584 return false;
00585
00586 if (FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (cond, 1)))
00587 ? real_zerop (TREE_OPERAND (cond, 1))
00588 : integer_zerop (TREE_OPERAND (cond, 1)))
00589 ;
00590 else
00591 return false;
00592
00593
00594
00595 extract_true_false_edges_from_block (cond_block, &true_edge, &false_edge);
00596
00597
00598
00599
00600 if (cond_code == GT_EXPR || cond_code == GE_EXPR)
00601 e = true_edge;
00602 else
00603 e = false_edge;
00604
00605 if (e->dest == other_block)
00606 negate = true;
00607 else
00608 negate = false;
00609
00610 if (negate)
00611 lhs = make_rename_temp (TREE_TYPE (result), NULL);
00612 else
00613 lhs = result;
00614
00615
00616 new = build (MODIFY_EXPR, TREE_TYPE (lhs),
00617 lhs, build1 (ABS_EXPR, TREE_TYPE (lhs), rhs));
00618
00619 replace_phi_with_stmt (bsi_after_labels (bb), bb, cond_block, phi, new);
00620
00621 if (negate)
00622 {
00623
00624
00625
00626
00627 bsi = bsi_start (bb);
00628 bsi_next (&bsi);
00629 new = build (MODIFY_EXPR, TREE_TYPE (result),
00630 result, build1 (NEGATE_EXPR, TREE_TYPE (lhs), lhs));
00631
00632 bsi_insert_after (&bsi, new, BSI_NEW_STMT);
00633
00634
00635
00636
00637 SSA_NAME_DEF_STMT (result) = new;
00638 }
00639
00640
00641 return true;
00642 }
00643
00644
00645
00646
00647 static bool
00648 gate_phiopt (void)
00649 {
00650 return 1;
00651 }
00652
00653 struct tree_opt_pass pass_phiopt =
00654 {
00655 "phiopt",
00656 gate_phiopt,
00657 tree_ssa_phiopt,
00658 NULL,
00659 NULL,
00660 0,
00661 TV_TREE_PHIOPT,
00662 PROP_cfg | PROP_ssa | PROP_alias,
00663 0,
00664 0,
00665 0,
00666 TODO_cleanup_cfg | TODO_dump_func | TODO_ggc_collect
00667 | TODO_verify_ssa | TODO_rename_vars
00668 | TODO_verify_flow,
00669 0
00670 };
00671
00672