00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
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 #include "config.h"
00089 #include "system.h"
00090 #include "coretypes.h"
00091 #include "tm.h"
00092 #include "flags.h"
00093 #include "tree.h"
00094 #include "tree-flow.h"
00095 #include "real.h"
00096 #include "timevar.h"
00097 #include "tree-pass.h"
00098 #include "alloc-pool.h"
00099 #include "basic-block.h"
00100 #include "target.h"
00101
00102
00103
00104
00105
00106 struct occurrence {
00107
00108 basic_block bb;
00109
00110
00111
00112 tree recip_def;
00113
00114
00115
00116 tree recip_def_stmt;
00117
00118
00119
00120 struct occurrence *children;
00121
00122
00123
00124 struct occurrence *next;
00125
00126
00127
00128
00129 int num_divisions;
00130
00131
00132
00133
00134 bool bb_has_division;
00135 };
00136
00137
00138
00139
00140 static struct occurrence *occ_head;
00141
00142
00143 static alloc_pool occ_pool;
00144
00145
00146
00147
00148
00149 static struct occurrence *
00150 occ_new (basic_block bb, struct occurrence *children)
00151 {
00152 struct occurrence *occ;
00153
00154 occ = bb->aux = pool_alloc (occ_pool);
00155 memset (occ, 0, sizeof (struct occurrence));
00156
00157 occ->bb = bb;
00158 occ->children = children;
00159 return occ;
00160 }
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171 static void
00172 insert_bb (struct occurrence *new_occ, basic_block idom,
00173 struct occurrence **p_head)
00174 {
00175 struct occurrence *occ, **p_occ;
00176
00177 for (p_occ = p_head; (occ = *p_occ) != NULL; )
00178 {
00179 basic_block bb = new_occ->bb, occ_bb = occ->bb;
00180 basic_block dom = nearest_common_dominator (CDI_DOMINATORS, occ_bb, bb);
00181 if (dom == bb)
00182 {
00183
00184
00185 *p_occ = occ->next;
00186 occ->next = new_occ->children;
00187 new_occ->children = occ;
00188
00189
00190 }
00191
00192 else if (dom == occ_bb)
00193 {
00194
00195 insert_bb (new_occ, dom, &occ->children);
00196 return;
00197 }
00198
00199 else if (dom != idom)
00200 {
00201 gcc_assert (!dom->aux);
00202
00203
00204
00205
00206 *p_occ = occ->next;
00207 new_occ->next = occ;
00208 occ->next = NULL;
00209
00210
00211
00212
00213 new_occ = occ_new (dom, new_occ);
00214 }
00215
00216 else
00217 {
00218
00219 p_occ = &occ->next;
00220 }
00221 }
00222
00223
00224 new_occ->next = *p_head;
00225 *p_head = new_occ;
00226 }
00227
00228
00229
00230 static inline void
00231 register_division_in (basic_block bb)
00232 {
00233 struct occurrence *occ;
00234
00235 occ = (struct occurrence *) bb->aux;
00236 if (!occ)
00237 {
00238 occ = occ_new (bb, NULL);
00239 insert_bb (occ, ENTRY_BLOCK_PTR, &occ_head);
00240 }
00241
00242 occ->bb_has_division = true;
00243 occ->num_divisions++;
00244 }
00245
00246
00247
00248
00249
00250 static void
00251 compute_merit (struct occurrence *occ)
00252 {
00253 struct occurrence *occ_child;
00254 basic_block dom = occ->bb;
00255
00256 for (occ_child = occ->children; occ_child; occ_child = occ_child->next)
00257 {
00258 basic_block bb;
00259 if (occ_child->children)
00260 compute_merit (occ_child);
00261
00262 if (flag_exceptions)
00263 bb = single_noncomplex_succ (dom);
00264 else
00265 bb = dom;
00266
00267 if (dominated_by_p (CDI_POST_DOMINATORS, bb, occ_child->bb))
00268 occ->num_divisions += occ_child->num_divisions;
00269 }
00270 }
00271
00272
00273
00274 static inline bool
00275 is_division_by (tree use_stmt, tree def)
00276 {
00277 return TREE_CODE (use_stmt) == MODIFY_EXPR
00278 && TREE_CODE (TREE_OPERAND (use_stmt, 1)) == RDIV_EXPR
00279 && TREE_OPERAND (TREE_OPERAND (use_stmt, 1), 1) == def;
00280 }
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291 static void
00292 insert_reciprocals (block_stmt_iterator *def_bsi, struct occurrence *occ,
00293 tree def, tree recip_def, int threshold)
00294 {
00295 tree type, new_stmt;
00296 block_stmt_iterator bsi;
00297 struct occurrence *occ_child;
00298
00299 if (!recip_def
00300 && (occ->bb_has_division || !flag_trapping_math)
00301 && occ->num_divisions >= threshold)
00302 {
00303
00304 type = TREE_TYPE (def);
00305 recip_def = make_rename_temp (type, "reciptmp");
00306 new_stmt = build2 (MODIFY_EXPR, void_type_node, recip_def,
00307 fold_build2 (RDIV_EXPR, type, build_one_cst (type),
00308 def));
00309
00310
00311 if (occ->bb_has_division)
00312 {
00313
00314 bsi = bsi_after_labels (occ->bb);
00315 while (!bsi_end_p (bsi) && !is_division_by (bsi_stmt (bsi), def))
00316 bsi_next (&bsi);
00317
00318 bsi_insert_before (&bsi, new_stmt, BSI_SAME_STMT);
00319 }
00320 else if (def_bsi && occ->bb == def_bsi->bb)
00321 {
00322
00323
00324
00325
00326 bsi_insert_after (def_bsi, new_stmt, BSI_NEW_STMT);
00327 }
00328 else
00329 {
00330
00331 bsi = bsi_after_labels (occ->bb);
00332 bsi_insert_before (&bsi, new_stmt, BSI_SAME_STMT);
00333 }
00334
00335 occ->recip_def_stmt = new_stmt;
00336 }
00337
00338 occ->recip_def = recip_def;
00339 for (occ_child = occ->children; occ_child; occ_child = occ_child->next)
00340 insert_reciprocals (def_bsi, occ_child, def, recip_def, threshold);
00341 }
00342
00343
00344
00345
00346
00347 static inline void
00348 replace_reciprocal (use_operand_p use_p)
00349 {
00350 tree use_stmt = USE_STMT (use_p);
00351 basic_block bb = bb_for_stmt (use_stmt);
00352 struct occurrence *occ = (struct occurrence *) bb->aux;
00353
00354 if (occ->recip_def && use_stmt != occ->recip_def_stmt)
00355 {
00356 TREE_SET_CODE (TREE_OPERAND (use_stmt, 1), MULT_EXPR);
00357 SET_USE (use_p, occ->recip_def);
00358 fold_stmt_inplace (use_stmt);
00359 update_stmt (use_stmt);
00360 }
00361 }
00362
00363
00364
00365
00366 static struct occurrence *
00367 free_bb (struct occurrence *occ)
00368 {
00369 struct occurrence *child, *next;
00370
00371
00372 next = occ->next;
00373 child = occ->children;
00374 occ->bb->aux = NULL;
00375 pool_free (occ_pool, occ);
00376
00377
00378 if (!child)
00379 return next;
00380 else
00381 {
00382 while (next)
00383 next = free_bb (next);
00384
00385 return child;
00386 }
00387 }
00388
00389
00390
00391
00392
00393
00394
00395
00396 static void
00397 execute_cse_reciprocals_1 (block_stmt_iterator *def_bsi, tree def)
00398 {
00399 use_operand_p use_p;
00400 imm_use_iterator use_iter;
00401 struct occurrence *occ;
00402 int count = 0, threshold;
00403
00404 gcc_assert (FLOAT_TYPE_P (TREE_TYPE (def)) && is_gimple_reg (def));
00405
00406 FOR_EACH_IMM_USE_FAST (use_p, use_iter, def)
00407 {
00408 tree use_stmt = USE_STMT (use_p);
00409 if (is_division_by (use_stmt, def))
00410 {
00411 register_division_in (bb_for_stmt (use_stmt));
00412 count++;
00413 }
00414 }
00415
00416
00417 threshold = targetm.min_divisions_for_recip_mul (TYPE_MODE (TREE_TYPE (def)));
00418 if (count >= threshold)
00419 {
00420 tree use_stmt;
00421 for (occ = occ_head; occ; occ = occ->next)
00422 {
00423 compute_merit (occ);
00424 insert_reciprocals (def_bsi, occ, def, NULL, threshold);
00425 }
00426
00427 FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, def)
00428 {
00429 if (is_division_by (use_stmt, def))
00430 {
00431 FOR_EACH_IMM_USE_ON_STMT (use_p, use_iter)
00432 replace_reciprocal (use_p);
00433 }
00434 }
00435 }
00436
00437 for (occ = occ_head; occ; )
00438 occ = free_bb (occ);
00439
00440 occ_head = NULL;
00441 }
00442
00443
00444 static bool
00445 gate_cse_reciprocals (void)
00446 {
00447 return optimize && !optimize_size && flag_unsafe_math_optimizations;
00448 }
00449
00450
00451
00452
00453 static unsigned int
00454 execute_cse_reciprocals (void)
00455 {
00456 basic_block bb;
00457 tree arg;
00458
00459 occ_pool = create_alloc_pool ("dominators for recip",
00460 sizeof (struct occurrence),
00461 n_basic_blocks / 3 + 1);
00462
00463 calculate_dominance_info (CDI_DOMINATORS);
00464 calculate_dominance_info (CDI_POST_DOMINATORS);
00465
00466 #ifdef ENABLE_CHECKING
00467 FOR_EACH_BB (bb)
00468 gcc_assert (!bb->aux);
00469 #endif
00470
00471 for (arg = DECL_ARGUMENTS (cfun->decl); arg; arg = TREE_CHAIN (arg))
00472 if (default_def (arg)
00473 && FLOAT_TYPE_P (TREE_TYPE (arg))
00474 && is_gimple_reg (arg))
00475 execute_cse_reciprocals_1 (NULL, default_def (arg));
00476
00477 FOR_EACH_BB (bb)
00478 {
00479 block_stmt_iterator bsi;
00480 tree phi, def;
00481
00482 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
00483 {
00484 def = PHI_RESULT (phi);
00485 if (FLOAT_TYPE_P (TREE_TYPE (def))
00486 && is_gimple_reg (def))
00487 execute_cse_reciprocals_1 (NULL, def);
00488 }
00489
00490 for (bsi = bsi_after_labels (bb); !bsi_end_p (bsi); bsi_next (&bsi))
00491 {
00492 tree stmt = bsi_stmt (bsi);
00493 if (TREE_CODE (stmt) == MODIFY_EXPR
00494 && (def = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_DEF)) != NULL
00495 && FLOAT_TYPE_P (TREE_TYPE (def))
00496 && TREE_CODE (def) == SSA_NAME)
00497 execute_cse_reciprocals_1 (&bsi, def);
00498 }
00499 }
00500
00501 free_dominance_info (CDI_DOMINATORS);
00502 free_dominance_info (CDI_POST_DOMINATORS);
00503 free_alloc_pool (occ_pool);
00504 return 0;
00505 }
00506
00507 struct tree_opt_pass pass_cse_reciprocals =
00508 {
00509 "recip",
00510 gate_cse_reciprocals,
00511 execute_cse_reciprocals,
00512 NULL,
00513 NULL,
00514 0,
00515 0,
00516 PROP_ssa,
00517 0,
00518 0,
00519 0,
00520 TODO_dump_func | TODO_update_ssa | TODO_verify_ssa
00521 | TODO_verify_stmts,
00522 0
00523 };