sese.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. /* Single entry single exit control flow regions.
  2. Copyright (C) 2008-2015 Free Software Foundation, Inc.
  3. Contributed by Jan Sjodin <jan.sjodin@amd.com> and
  4. Sebastian Pop <sebastian.pop@amd.com>.
  5. This file is part of GCC.
  6. GCC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3, or (at your option)
  9. any later version.
  10. GCC is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GCC; see the file COPYING3. If not see
  16. <http://www.gnu.org/licenses/>. */
  17. #include "config.h"
  18. #include "system.h"
  19. #include "coretypes.h"
  20. #include "hash-map.h"
  21. #include "hash-set.h"
  22. #include "machmode.h"
  23. #include "vec.h"
  24. #include "double-int.h"
  25. #include "input.h"
  26. #include "alias.h"
  27. #include "symtab.h"
  28. #include "options.h"
  29. #include "wide-int.h"
  30. #include "inchash.h"
  31. #include "tree.h"
  32. #include "fold-const.h"
  33. #include "tree-pretty-print.h"
  34. #include "predict.h"
  35. #include "tm.h"
  36. #include "hard-reg-set.h"
  37. #include "input.h"
  38. #include "function.h"
  39. #include "dominance.h"
  40. #include "cfg.h"
  41. #include "basic-block.h"
  42. #include "tree-ssa-alias.h"
  43. #include "internal-fn.h"
  44. #include "gimple-fold.h"
  45. #include "tree-eh.h"
  46. #include "gimple-expr.h"
  47. #include "is-a.h"
  48. #include "gimple.h"
  49. #include "gimplify.h"
  50. #include "gimple-iterator.h"
  51. #include "gimplify-me.h"
  52. #include "gimple-ssa.h"
  53. #include "tree-cfg.h"
  54. #include "tree-phinodes.h"
  55. #include "ssa-iterators.h"
  56. #include "stringpool.h"
  57. #include "tree-ssanames.h"
  58. #include "tree-ssa-loop.h"
  59. #include "tree-into-ssa.h"
  60. #include "cfgloop.h"
  61. #include "tree-chrec.h"
  62. #include "tree-data-ref.h"
  63. #include "tree-scalar-evolution.h"
  64. #include "tree-pass.h"
  65. #include "value-prof.h"
  66. #include "sese.h"
  67. #include "tree-ssa-propagate.h"
  68. /* Helper function for debug_rename_map. */
  69. bool
  70. debug_rename_map_1 (tree_node *const &old_name, tree_node *const &expr,
  71. void *)
  72. {
  73. fprintf (stderr, "(");
  74. print_generic_expr (stderr, old_name, 0);
  75. fprintf (stderr, ", ");
  76. print_generic_expr (stderr, expr, 0);
  77. fprintf (stderr, ")\n");
  78. return true;
  79. }
  80. /* Hashtable helpers. */
  81. struct rename_map_hasher : default_hashmap_traits
  82. {
  83. static inline hashval_t hash (tree);
  84. };
  85. /* Computes a hash function for database element ELT. */
  86. inline hashval_t
  87. rename_map_hasher::hash (tree old_name)
  88. {
  89. return SSA_NAME_VERSION (old_name);
  90. }
  91. typedef hash_map<tree, tree, rename_map_hasher> rename_map_type;
  92. /* Print to stderr all the elements of RENAME_MAP. */
  93. DEBUG_FUNCTION void
  94. debug_rename_map (rename_map_type *rename_map)
  95. {
  96. rename_map->traverse <void *, debug_rename_map_1> (NULL);
  97. }
  98. /* Record LOOP as occurring in REGION. */
  99. static void
  100. sese_record_loop (sese region, loop_p loop)
  101. {
  102. if (sese_contains_loop (region, loop))
  103. return;
  104. bitmap_set_bit (SESE_LOOPS (region), loop->num);
  105. SESE_LOOP_NEST (region).safe_push (loop);
  106. }
  107. /* Build the loop nests contained in REGION. Returns true when the
  108. operation was successful. */
  109. void
  110. build_sese_loop_nests (sese region)
  111. {
  112. unsigned i;
  113. basic_block bb;
  114. struct loop *loop0, *loop1;
  115. FOR_EACH_BB_FN (bb, cfun)
  116. if (bb_in_sese_p (bb, region))
  117. {
  118. struct loop *loop = bb->loop_father;
  119. /* Only add loops if they are completely contained in the SCoP. */
  120. if (loop->header == bb
  121. && bb_in_sese_p (loop->latch, region))
  122. sese_record_loop (region, loop);
  123. }
  124. /* Make sure that the loops in the SESE_LOOP_NEST are ordered. It
  125. can be the case that an inner loop is inserted before an outer
  126. loop. To avoid this, semi-sort once. */
  127. FOR_EACH_VEC_ELT (SESE_LOOP_NEST (region), i, loop0)
  128. {
  129. if (SESE_LOOP_NEST (region).length () == i + 1)
  130. break;
  131. loop1 = SESE_LOOP_NEST (region)[i + 1];
  132. if (loop0->num > loop1->num)
  133. {
  134. SESE_LOOP_NEST (region)[i] = loop1;
  135. SESE_LOOP_NEST (region)[i + 1] = loop0;
  136. }
  137. }
  138. }
  139. /* For a USE in BB, if BB is outside REGION, mark the USE in the
  140. LIVEOUTS set. */
  141. static void
  142. sese_build_liveouts_use (sese region, bitmap liveouts, basic_block bb,
  143. tree use)
  144. {
  145. unsigned ver;
  146. basic_block def_bb;
  147. if (TREE_CODE (use) != SSA_NAME)
  148. return;
  149. ver = SSA_NAME_VERSION (use);
  150. def_bb = gimple_bb (SSA_NAME_DEF_STMT (use));
  151. if (!def_bb
  152. || !bb_in_sese_p (def_bb, region)
  153. || bb_in_sese_p (bb, region))
  154. return;
  155. bitmap_set_bit (liveouts, ver);
  156. }
  157. /* Marks for rewrite all the SSA_NAMES defined in REGION and that are
  158. used in BB that is outside of the REGION. */
  159. static void
  160. sese_build_liveouts_bb (sese region, bitmap liveouts, basic_block bb)
  161. {
  162. edge e;
  163. edge_iterator ei;
  164. ssa_op_iter iter;
  165. use_operand_p use_p;
  166. FOR_EACH_EDGE (e, ei, bb->succs)
  167. for (gphi_iterator bsi = gsi_start_phis (e->dest); !gsi_end_p (bsi);
  168. gsi_next (&bsi))
  169. sese_build_liveouts_use (region, liveouts, bb,
  170. PHI_ARG_DEF_FROM_EDGE (bsi.phi (), e));
  171. for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
  172. gsi_next (&bsi))
  173. {
  174. gimple stmt = gsi_stmt (bsi);
  175. if (is_gimple_debug (stmt))
  176. continue;
  177. FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
  178. sese_build_liveouts_use (region, liveouts, bb, USE_FROM_PTR (use_p));
  179. }
  180. }
  181. /* For a USE in BB, return true if BB is outside REGION and it's not
  182. in the LIVEOUTS set. */
  183. static bool
  184. sese_bad_liveouts_use (sese region, bitmap liveouts, basic_block bb,
  185. tree use)
  186. {
  187. unsigned ver;
  188. basic_block def_bb;
  189. if (TREE_CODE (use) != SSA_NAME)
  190. return false;
  191. ver = SSA_NAME_VERSION (use);
  192. /* If it's in liveouts, the variable will get a new PHI node, and
  193. the debug use will be properly adjusted. */
  194. if (bitmap_bit_p (liveouts, ver))
  195. return false;
  196. def_bb = gimple_bb (SSA_NAME_DEF_STMT (use));
  197. if (!def_bb
  198. || !bb_in_sese_p (def_bb, region)
  199. || bb_in_sese_p (bb, region))
  200. return false;
  201. return true;
  202. }
  203. /* Reset debug stmts that reference SSA_NAMES defined in REGION that
  204. are not marked as liveouts. */
  205. static void
  206. sese_reset_debug_liveouts_bb (sese region, bitmap liveouts, basic_block bb)
  207. {
  208. gimple_stmt_iterator bsi;
  209. ssa_op_iter iter;
  210. use_operand_p use_p;
  211. for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
  212. {
  213. gimple stmt = gsi_stmt (bsi);
  214. if (!is_gimple_debug (stmt))
  215. continue;
  216. FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
  217. if (sese_bad_liveouts_use (region, liveouts, bb,
  218. USE_FROM_PTR (use_p)))
  219. {
  220. gimple_debug_bind_reset_value (stmt);
  221. update_stmt (stmt);
  222. break;
  223. }
  224. }
  225. }
  226. /* Build the LIVEOUTS of REGION: the set of variables defined inside
  227. and used outside the REGION. */
  228. static void
  229. sese_build_liveouts (sese region, bitmap liveouts)
  230. {
  231. basic_block bb;
  232. FOR_EACH_BB_FN (bb, cfun)
  233. sese_build_liveouts_bb (region, liveouts, bb);
  234. if (MAY_HAVE_DEBUG_STMTS)
  235. FOR_EACH_BB_FN (bb, cfun)
  236. sese_reset_debug_liveouts_bb (region, liveouts, bb);
  237. }
  238. /* Builds a new SESE region from edges ENTRY and EXIT. */
  239. sese
  240. new_sese (edge entry, edge exit)
  241. {
  242. sese region = XNEW (struct sese_s);
  243. SESE_ENTRY (region) = entry;
  244. SESE_EXIT (region) = exit;
  245. SESE_LOOPS (region) = BITMAP_ALLOC (NULL);
  246. SESE_LOOP_NEST (region).create (3);
  247. SESE_ADD_PARAMS (region) = true;
  248. SESE_PARAMS (region).create (3);
  249. return region;
  250. }
  251. /* Deletes REGION. */
  252. void
  253. free_sese (sese region)
  254. {
  255. if (SESE_LOOPS (region))
  256. SESE_LOOPS (region) = BITMAP_ALLOC (NULL);
  257. SESE_PARAMS (region).release ();
  258. SESE_LOOP_NEST (region).release ();
  259. XDELETE (region);
  260. }
  261. /* Add exit phis for USE on EXIT. */
  262. static void
  263. sese_add_exit_phis_edge (basic_block exit, tree use, edge false_e, edge true_e)
  264. {
  265. gphi *phi = create_phi_node (NULL_TREE, exit);
  266. create_new_def_for (use, phi, gimple_phi_result_ptr (phi));
  267. add_phi_arg (phi, use, false_e, UNKNOWN_LOCATION);
  268. add_phi_arg (phi, use, true_e, UNKNOWN_LOCATION);
  269. }
  270. /* Insert in the block BB phi nodes for variables defined in REGION
  271. and used outside the REGION. The code generation moves REGION in
  272. the else clause of an "if (1)" and generates code in the then
  273. clause that is at this point empty:
  274. | if (1)
  275. | empty;
  276. | else
  277. | REGION;
  278. */
  279. void
  280. sese_insert_phis_for_liveouts (sese region, basic_block bb,
  281. edge false_e, edge true_e)
  282. {
  283. unsigned i;
  284. bitmap_iterator bi;
  285. bitmap liveouts = BITMAP_ALLOC (NULL);
  286. update_ssa (TODO_update_ssa);
  287. sese_build_liveouts (region, liveouts);
  288. EXECUTE_IF_SET_IN_BITMAP (liveouts, 0, i, bi)
  289. sese_add_exit_phis_edge (bb, ssa_name (i), false_e, true_e);
  290. BITMAP_FREE (liveouts);
  291. update_ssa (TODO_update_ssa);
  292. }
  293. /* Returns the first successor edge of BB with EDGE_TRUE_VALUE flag set. */
  294. edge
  295. get_true_edge_from_guard_bb (basic_block bb)
  296. {
  297. edge e;
  298. edge_iterator ei;
  299. FOR_EACH_EDGE (e, ei, bb->succs)
  300. if (e->flags & EDGE_TRUE_VALUE)
  301. return e;
  302. gcc_unreachable ();
  303. return NULL;
  304. }
  305. /* Returns the first successor edge of BB with EDGE_TRUE_VALUE flag cleared. */
  306. edge
  307. get_false_edge_from_guard_bb (basic_block bb)
  308. {
  309. edge e;
  310. edge_iterator ei;
  311. FOR_EACH_EDGE (e, ei, bb->succs)
  312. if (!(e->flags & EDGE_TRUE_VALUE))
  313. return e;
  314. gcc_unreachable ();
  315. return NULL;
  316. }
  317. /* Returns the expression associated to OLD_NAME in RENAME_MAP. */
  318. static tree
  319. get_rename (rename_map_type *rename_map, tree old_name)
  320. {
  321. gcc_assert (TREE_CODE (old_name) == SSA_NAME);
  322. tree *expr = rename_map->get (old_name);
  323. if (expr)
  324. return *expr;
  325. return NULL_TREE;
  326. }
  327. /* Register in RENAME_MAP the rename tuple (OLD_NAME, EXPR). */
  328. static void
  329. set_rename (rename_map_type *rename_map, tree old_name, tree expr)
  330. {
  331. if (old_name == expr)
  332. return;
  333. rename_map->put (old_name, expr);
  334. }
  335. /* Renames the scalar uses of the statement COPY, using the
  336. substitution map RENAME_MAP, inserting the gimplification code at
  337. GSI_TGT, for the translation REGION, with the original copied
  338. statement in LOOP, and using the induction variable renaming map
  339. IV_MAP. Returns true when something has been renamed. GLOOG_ERROR
  340. is set when the code generation cannot continue. */
  341. static bool
  342. rename_uses (gimple copy, rename_map_type *rename_map,
  343. gimple_stmt_iterator *gsi_tgt,
  344. sese region, loop_p loop, vec<tree> iv_map,
  345. bool *gloog_error)
  346. {
  347. use_operand_p use_p;
  348. ssa_op_iter op_iter;
  349. bool changed = false;
  350. if (is_gimple_debug (copy))
  351. {
  352. if (gimple_debug_bind_p (copy))
  353. gimple_debug_bind_reset_value (copy);
  354. else if (gimple_debug_source_bind_p (copy))
  355. return false;
  356. else
  357. gcc_unreachable ();
  358. return false;
  359. }
  360. FOR_EACH_SSA_USE_OPERAND (use_p, copy, op_iter, SSA_OP_USE)
  361. {
  362. tree old_name = USE_FROM_PTR (use_p);
  363. tree new_expr, scev;
  364. gimple_seq stmts;
  365. if (TREE_CODE (old_name) != SSA_NAME
  366. || SSA_NAME_IS_DEFAULT_DEF (old_name))
  367. continue;
  368. changed = true;
  369. new_expr = get_rename (rename_map, old_name);
  370. if (new_expr)
  371. {
  372. tree type_old_name = TREE_TYPE (old_name);
  373. tree type_new_expr = TREE_TYPE (new_expr);
  374. if (type_old_name != type_new_expr
  375. || TREE_CODE (new_expr) != SSA_NAME)
  376. {
  377. tree var = create_tmp_var (type_old_name, "var");
  378. if (!useless_type_conversion_p (type_old_name, type_new_expr))
  379. new_expr = fold_convert (type_old_name, new_expr);
  380. new_expr = force_gimple_operand (new_expr, &stmts, true, var);
  381. gsi_insert_seq_before (gsi_tgt, stmts, GSI_SAME_STMT);
  382. }
  383. replace_exp (use_p, new_expr);
  384. continue;
  385. }
  386. scev = scalar_evolution_in_region (region, loop, old_name);
  387. /* At this point we should know the exact scev for each
  388. scalar SSA_NAME used in the scop: all the other scalar
  389. SSA_NAMEs should have been translated out of SSA using
  390. arrays with one element. */
  391. if (chrec_contains_undetermined (scev))
  392. {
  393. *gloog_error = true;
  394. new_expr = build_zero_cst (TREE_TYPE (old_name));
  395. }
  396. else
  397. new_expr = chrec_apply_map (scev, iv_map);
  398. /* The apply should produce an expression tree containing
  399. the uses of the new induction variables. We should be
  400. able to use new_expr instead of the old_name in the newly
  401. generated loop nest. */
  402. if (chrec_contains_undetermined (new_expr)
  403. || tree_contains_chrecs (new_expr, NULL))
  404. {
  405. *gloog_error = true;
  406. new_expr = build_zero_cst (TREE_TYPE (old_name));
  407. }
  408. else
  409. /* Replace the old_name with the new_expr. */
  410. new_expr = force_gimple_operand (unshare_expr (new_expr), &stmts,
  411. true, NULL_TREE);
  412. gsi_insert_seq_before (gsi_tgt, stmts, GSI_SAME_STMT);
  413. replace_exp (use_p, new_expr);
  414. if (TREE_CODE (new_expr) == INTEGER_CST
  415. && is_gimple_assign (copy))
  416. {
  417. tree rhs = gimple_assign_rhs1 (copy);
  418. if (TREE_CODE (rhs) == ADDR_EXPR)
  419. recompute_tree_invariant_for_addr_expr (rhs);
  420. }
  421. set_rename (rename_map, old_name, new_expr);
  422. }
  423. return changed;
  424. }
  425. /* Duplicates the statements of basic block BB into basic block NEW_BB
  426. and compute the new induction variables according to the IV_MAP.
  427. GLOOG_ERROR is set when the code generation cannot continue. */
  428. static void
  429. graphite_copy_stmts_from_block (basic_block bb, basic_block new_bb,
  430. rename_map_type *rename_map,
  431. vec<tree> iv_map, sese region,
  432. bool *gloog_error)
  433. {
  434. gimple_stmt_iterator gsi, gsi_tgt;
  435. loop_p loop = bb->loop_father;
  436. gsi_tgt = gsi_start_bb (new_bb);
  437. for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  438. {
  439. def_operand_p def_p;
  440. ssa_op_iter op_iter;
  441. gimple stmt = gsi_stmt (gsi);
  442. gimple copy;
  443. tree lhs;
  444. /* Do not copy labels or conditions. */
  445. if (gimple_code (stmt) == GIMPLE_LABEL
  446. || gimple_code (stmt) == GIMPLE_COND)
  447. continue;
  448. /* Do not copy induction variables. */
  449. if (is_gimple_assign (stmt)
  450. && (lhs = gimple_assign_lhs (stmt))
  451. && TREE_CODE (lhs) == SSA_NAME
  452. && is_gimple_reg (lhs)
  453. && scev_analyzable_p (lhs, region))
  454. continue;
  455. /* Create a new copy of STMT and duplicate STMT's virtual
  456. operands. */
  457. copy = gimple_copy (stmt);
  458. gsi_insert_after (&gsi_tgt, copy, GSI_NEW_STMT);
  459. maybe_duplicate_eh_stmt (copy, stmt);
  460. gimple_duplicate_stmt_histograms (cfun, copy, cfun, stmt);
  461. /* Create new names for all the definitions created by COPY and
  462. add replacement mappings for each new name. */
  463. FOR_EACH_SSA_DEF_OPERAND (def_p, copy, op_iter, SSA_OP_ALL_DEFS)
  464. {
  465. tree old_name = DEF_FROM_PTR (def_p);
  466. tree new_name = create_new_def_for (old_name, copy, def_p);
  467. set_rename (rename_map, old_name, new_name);
  468. }
  469. if (rename_uses (copy, rename_map, &gsi_tgt, region, loop, iv_map,
  470. gloog_error))
  471. {
  472. gcc_assert (gsi_stmt (gsi_tgt) == copy);
  473. fold_stmt_inplace (&gsi_tgt);
  474. }
  475. update_stmt (copy);
  476. }
  477. }
  478. /* Copies BB and includes in the copied BB all the statements that can
  479. be reached following the use-def chains from the memory accesses,
  480. and returns the next edge following this new block. GLOOG_ERROR is
  481. set when the code generation cannot continue. */
  482. edge
  483. copy_bb_and_scalar_dependences (basic_block bb, sese region,
  484. edge next_e, vec<tree> iv_map,
  485. bool *gloog_error)
  486. {
  487. basic_block new_bb = split_edge (next_e);
  488. rename_map_type rename_map (10);
  489. next_e = single_succ_edge (new_bb);
  490. graphite_copy_stmts_from_block (bb, new_bb, &rename_map, iv_map, region,
  491. gloog_error);
  492. remove_phi_nodes (new_bb);
  493. return next_e;
  494. }
  495. /* Returns the outermost loop in SCOP that contains BB. */
  496. struct loop *
  497. outermost_loop_in_sese (sese region, basic_block bb)
  498. {
  499. struct loop *nest;
  500. nest = bb->loop_father;
  501. while (loop_outer (nest)
  502. && loop_in_sese_p (loop_outer (nest), region))
  503. nest = loop_outer (nest);
  504. return nest;
  505. }
  506. /* Sets the false region of an IF_REGION to REGION. */
  507. void
  508. if_region_set_false_region (ifsese if_region, sese region)
  509. {
  510. basic_block condition = if_region_get_condition_block (if_region);
  511. edge false_edge = get_false_edge_from_guard_bb (condition);
  512. basic_block dummy = false_edge->dest;
  513. edge entry_region = SESE_ENTRY (region);
  514. edge exit_region = SESE_EXIT (region);
  515. basic_block before_region = entry_region->src;
  516. basic_block last_in_region = exit_region->src;
  517. hashval_t hash = htab_hash_pointer (exit_region);
  518. loop_exit **slot
  519. = current_loops->exits->find_slot_with_hash (exit_region, hash, NO_INSERT);
  520. entry_region->flags = false_edge->flags;
  521. false_edge->flags = exit_region->flags;
  522. redirect_edge_pred (entry_region, condition);
  523. redirect_edge_pred (exit_region, before_region);
  524. redirect_edge_pred (false_edge, last_in_region);
  525. redirect_edge_succ (false_edge, single_succ (dummy));
  526. delete_basic_block (dummy);
  527. exit_region->flags = EDGE_FALLTHRU;
  528. recompute_all_dominators ();
  529. SESE_EXIT (region) = false_edge;
  530. free (if_region->false_region);
  531. if_region->false_region = region;
  532. if (slot)
  533. {
  534. struct loop_exit *loop_exit = ggc_cleared_alloc<struct loop_exit> ();
  535. memcpy (loop_exit, *((struct loop_exit **) slot), sizeof (struct loop_exit));
  536. current_loops->exits->clear_slot (slot);
  537. hashval_t hash = htab_hash_pointer (false_edge);
  538. slot = current_loops->exits->find_slot_with_hash (false_edge, hash,
  539. INSERT);
  540. loop_exit->e = false_edge;
  541. *slot = loop_exit;
  542. false_edge->src->loop_father->exits->next = loop_exit;
  543. }
  544. }
  545. /* Creates an IFSESE with CONDITION on edge ENTRY. */
  546. static ifsese
  547. create_if_region_on_edge (edge entry, tree condition)
  548. {
  549. edge e;
  550. edge_iterator ei;
  551. sese sese_region = XNEW (struct sese_s);
  552. sese true_region = XNEW (struct sese_s);
  553. sese false_region = XNEW (struct sese_s);
  554. ifsese if_region = XNEW (struct ifsese_s);
  555. edge exit = create_empty_if_region_on_edge (entry, condition);
  556. if_region->region = sese_region;
  557. if_region->region->entry = entry;
  558. if_region->region->exit = exit;
  559. FOR_EACH_EDGE (e, ei, entry->dest->succs)
  560. {
  561. if (e->flags & EDGE_TRUE_VALUE)
  562. {
  563. true_region->entry = e;
  564. true_region->exit = single_succ_edge (e->dest);
  565. if_region->true_region = true_region;
  566. }
  567. else if (e->flags & EDGE_FALSE_VALUE)
  568. {
  569. false_region->entry = e;
  570. false_region->exit = single_succ_edge (e->dest);
  571. if_region->false_region = false_region;
  572. }
  573. }
  574. return if_region;
  575. }
  576. /* Moves REGION in a condition expression:
  577. | if (1)
  578. | ;
  579. | else
  580. | REGION;
  581. */
  582. ifsese
  583. move_sese_in_condition (sese region)
  584. {
  585. basic_block pred_block = split_edge (SESE_ENTRY (region));
  586. ifsese if_region;
  587. SESE_ENTRY (region) = single_succ_edge (pred_block);
  588. if_region = create_if_region_on_edge (single_pred_edge (pred_block), integer_one_node);
  589. if_region_set_false_region (if_region, region);
  590. return if_region;
  591. }
  592. /* Replaces the condition of the IF_REGION with CONDITION:
  593. | if (CONDITION)
  594. | true_region;
  595. | else
  596. | false_region;
  597. */
  598. void
  599. set_ifsese_condition (ifsese if_region, tree condition)
  600. {
  601. sese region = if_region->region;
  602. edge entry = region->entry;
  603. basic_block bb = entry->dest;
  604. gimple last = last_stmt (bb);
  605. gimple_stmt_iterator gsi = gsi_last_bb (bb);
  606. gcond *cond_stmt;
  607. gcc_assert (gimple_code (last) == GIMPLE_COND);
  608. gsi_remove (&gsi, true);
  609. gsi = gsi_last_bb (bb);
  610. condition = force_gimple_operand_gsi (&gsi, condition, true, NULL,
  611. false, GSI_NEW_STMT);
  612. cond_stmt = gimple_build_cond_from_tree (condition, NULL_TREE, NULL_TREE);
  613. gsi = gsi_last_bb (bb);
  614. gsi_insert_after (&gsi, cond_stmt, GSI_NEW_STMT);
  615. }
  616. /* Returns the scalar evolution of T in REGION. Every variable that
  617. is not defined in the REGION is considered a parameter. */
  618. tree
  619. scalar_evolution_in_region (sese region, loop_p loop, tree t)
  620. {
  621. gimple def;
  622. struct loop *def_loop;
  623. basic_block before = block_before_sese (region);
  624. /* SCOP parameters. */
  625. if (TREE_CODE (t) == SSA_NAME
  626. && !defined_in_sese_p (t, region))
  627. return t;
  628. if (TREE_CODE (t) != SSA_NAME
  629. || loop_in_sese_p (loop, region))
  630. return instantiate_scev (before, loop,
  631. analyze_scalar_evolution (loop, t));
  632. def = SSA_NAME_DEF_STMT (t);
  633. def_loop = loop_containing_stmt (def);
  634. if (loop_in_sese_p (def_loop, region))
  635. {
  636. t = analyze_scalar_evolution (def_loop, t);
  637. def_loop = superloop_at_depth (def_loop, loop_depth (loop) + 1);
  638. t = compute_overall_effect_of_inner_loop (def_loop, t);
  639. return t;
  640. }
  641. else
  642. return instantiate_scev (before, loop, t);
  643. }