tree-cfgcleanup.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  1. /* CFG cleanup for trees.
  2. Copyright (C) 2001-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GCC is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include "config.h"
  16. #include "system.h"
  17. #include "coretypes.h"
  18. #include "tm.h"
  19. #include "hash-set.h"
  20. #include "machmode.h"
  21. #include "vec.h"
  22. #include "double-int.h"
  23. #include "input.h"
  24. #include "alias.h"
  25. #include "symtab.h"
  26. #include "wide-int.h"
  27. #include "inchash.h"
  28. #include "tree.h"
  29. #include "fold-const.h"
  30. #include "tm_p.h"
  31. #include "predict.h"
  32. #include "hard-reg-set.h"
  33. #include "function.h"
  34. #include "dominance.h"
  35. #include "cfg.h"
  36. #include "cfganal.h"
  37. #include "cfgcleanup.h"
  38. #include "basic-block.h"
  39. #include "diagnostic-core.h"
  40. #include "flags.h"
  41. #include "langhooks.h"
  42. #include "tree-ssa-alias.h"
  43. #include "internal-fn.h"
  44. #include "tree-eh.h"
  45. #include "gimple-expr.h"
  46. #include "is-a.h"
  47. #include "gimple.h"
  48. #include "gimplify.h"
  49. #include "gimple-iterator.h"
  50. #include "gimple-ssa.h"
  51. #include "tree-cfg.h"
  52. #include "tree-phinodes.h"
  53. #include "ssa-iterators.h"
  54. #include "stringpool.h"
  55. #include "tree-ssanames.h"
  56. #include "tree-ssa-loop-manip.h"
  57. #include "hashtab.h"
  58. #include "rtl.h"
  59. #include "statistics.h"
  60. #include "real.h"
  61. #include "fixed-value.h"
  62. #include "insn-config.h"
  63. #include "expmed.h"
  64. #include "dojump.h"
  65. #include "explow.h"
  66. #include "calls.h"
  67. #include "emit-rtl.h"
  68. #include "varasm.h"
  69. #include "stmt.h"
  70. #include "expr.h"
  71. #include "tree-dfa.h"
  72. #include "tree-ssa.h"
  73. #include "tree-pass.h"
  74. #include "except.h"
  75. #include "cfgloop.h"
  76. #include "tree-ssa-propagate.h"
  77. #include "tree-scalar-evolution.h"
  78. /* The set of blocks in that at least one of the following changes happened:
  79. -- the statement at the end of the block was changed
  80. -- the block was newly created
  81. -- the set of the predecessors of the block changed
  82. -- the set of the successors of the block changed
  83. ??? Maybe we could track these changes separately, since they determine
  84. what cleanups it makes sense to try on the block. */
  85. bitmap cfgcleanup_altered_bbs;
  86. /* Remove any fallthru edge from EV. Return true if an edge was removed. */
  87. static bool
  88. remove_fallthru_edge (vec<edge, va_gc> *ev)
  89. {
  90. edge_iterator ei;
  91. edge e;
  92. FOR_EACH_EDGE (e, ei, ev)
  93. if ((e->flags & EDGE_FALLTHRU) != 0)
  94. {
  95. if (e->flags & EDGE_COMPLEX)
  96. e->flags &= ~EDGE_FALLTHRU;
  97. else
  98. remove_edge_and_dominated_blocks (e);
  99. return true;
  100. }
  101. return false;
  102. }
  103. /* Disconnect an unreachable block in the control expression starting
  104. at block BB. */
  105. static bool
  106. cleanup_control_expr_graph (basic_block bb, gimple_stmt_iterator gsi)
  107. {
  108. edge taken_edge;
  109. bool retval = false;
  110. gimple stmt = gsi_stmt (gsi);
  111. tree val;
  112. if (!single_succ_p (bb))
  113. {
  114. edge e;
  115. edge_iterator ei;
  116. bool warned;
  117. location_t loc;
  118. fold_defer_overflow_warnings ();
  119. loc = gimple_location (stmt);
  120. switch (gimple_code (stmt))
  121. {
  122. case GIMPLE_COND:
  123. val = fold_binary_loc (loc, gimple_cond_code (stmt),
  124. boolean_type_node,
  125. gimple_cond_lhs (stmt),
  126. gimple_cond_rhs (stmt));
  127. break;
  128. case GIMPLE_SWITCH:
  129. val = gimple_switch_index (as_a <gswitch *> (stmt));
  130. break;
  131. default:
  132. val = NULL_TREE;
  133. }
  134. taken_edge = find_taken_edge (bb, val);
  135. if (!taken_edge)
  136. {
  137. fold_undefer_and_ignore_overflow_warnings ();
  138. return false;
  139. }
  140. /* Remove all the edges except the one that is always executed. */
  141. warned = false;
  142. for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
  143. {
  144. if (e != taken_edge)
  145. {
  146. if (!warned)
  147. {
  148. fold_undefer_overflow_warnings
  149. (true, stmt, WARN_STRICT_OVERFLOW_CONDITIONAL);
  150. warned = true;
  151. }
  152. taken_edge->probability += e->probability;
  153. taken_edge->count += e->count;
  154. remove_edge_and_dominated_blocks (e);
  155. retval = true;
  156. }
  157. else
  158. ei_next (&ei);
  159. }
  160. if (!warned)
  161. fold_undefer_and_ignore_overflow_warnings ();
  162. if (taken_edge->probability > REG_BR_PROB_BASE)
  163. taken_edge->probability = REG_BR_PROB_BASE;
  164. }
  165. else
  166. taken_edge = single_succ_edge (bb);
  167. bitmap_set_bit (cfgcleanup_altered_bbs, bb->index);
  168. gsi_remove (&gsi, true);
  169. taken_edge->flags = EDGE_FALLTHRU;
  170. return retval;
  171. }
  172. /* Cleanup the GF_CALL_CTRL_ALTERING flag according to
  173. to updated gimple_call_flags. */
  174. static void
  175. cleanup_call_ctrl_altering_flag (gimple bb_end)
  176. {
  177. if (!is_gimple_call (bb_end)
  178. || !gimple_call_ctrl_altering_p (bb_end))
  179. return;
  180. int flags = gimple_call_flags (bb_end);
  181. if (((flags & (ECF_CONST | ECF_PURE))
  182. && !(flags & ECF_LOOPING_CONST_OR_PURE))
  183. || (flags & ECF_LEAF))
  184. gimple_call_set_ctrl_altering (bb_end, false);
  185. }
  186. /* Try to remove superfluous control structures in basic block BB. Returns
  187. true if anything changes. */
  188. static bool
  189. cleanup_control_flow_bb (basic_block bb)
  190. {
  191. gimple_stmt_iterator gsi;
  192. bool retval = false;
  193. gimple stmt;
  194. /* If the last statement of the block could throw and now cannot,
  195. we need to prune cfg. */
  196. retval |= gimple_purge_dead_eh_edges (bb);
  197. gsi = gsi_last_bb (bb);
  198. if (gsi_end_p (gsi))
  199. return retval;
  200. stmt = gsi_stmt (gsi);
  201. /* Try to cleanup ctrl altering flag for call which ends bb. */
  202. cleanup_call_ctrl_altering_flag (stmt);
  203. if (gimple_code (stmt) == GIMPLE_COND
  204. || gimple_code (stmt) == GIMPLE_SWITCH)
  205. retval |= cleanup_control_expr_graph (bb, gsi);
  206. else if (gimple_code (stmt) == GIMPLE_GOTO
  207. && TREE_CODE (gimple_goto_dest (stmt)) == ADDR_EXPR
  208. && (TREE_CODE (TREE_OPERAND (gimple_goto_dest (stmt), 0))
  209. == LABEL_DECL))
  210. {
  211. /* If we had a computed goto which has a compile-time determinable
  212. destination, then we can eliminate the goto. */
  213. edge e;
  214. tree label;
  215. edge_iterator ei;
  216. basic_block target_block;
  217. /* First look at all the outgoing edges. Delete any outgoing
  218. edges which do not go to the right block. For the one
  219. edge which goes to the right block, fix up its flags. */
  220. label = TREE_OPERAND (gimple_goto_dest (stmt), 0);
  221. target_block = label_to_block (label);
  222. for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
  223. {
  224. if (e->dest != target_block)
  225. remove_edge_and_dominated_blocks (e);
  226. else
  227. {
  228. /* Turn off the EDGE_ABNORMAL flag. */
  229. e->flags &= ~EDGE_ABNORMAL;
  230. /* And set EDGE_FALLTHRU. */
  231. e->flags |= EDGE_FALLTHRU;
  232. ei_next (&ei);
  233. }
  234. }
  235. bitmap_set_bit (cfgcleanup_altered_bbs, bb->index);
  236. bitmap_set_bit (cfgcleanup_altered_bbs, target_block->index);
  237. /* Remove the GOTO_EXPR as it is not needed. The CFG has all the
  238. relevant information we need. */
  239. gsi_remove (&gsi, true);
  240. retval = true;
  241. }
  242. /* Check for indirect calls that have been turned into
  243. noreturn calls. */
  244. else if (is_gimple_call (stmt)
  245. && gimple_call_noreturn_p (stmt)
  246. && remove_fallthru_edge (bb->succs))
  247. retval = true;
  248. return retval;
  249. }
  250. /* Return true if basic block BB does nothing except pass control
  251. flow to another block and that we can safely insert a label at
  252. the start of the successor block.
  253. As a precondition, we require that BB be not equal to
  254. the entry block. */
  255. static bool
  256. tree_forwarder_block_p (basic_block bb, bool phi_wanted)
  257. {
  258. gimple_stmt_iterator gsi;
  259. location_t locus;
  260. /* BB must have a single outgoing edge. */
  261. if (single_succ_p (bb) != 1
  262. /* If PHI_WANTED is false, BB must not have any PHI nodes.
  263. Otherwise, BB must have PHI nodes. */
  264. || gimple_seq_empty_p (phi_nodes (bb)) == phi_wanted
  265. /* BB may not be a predecessor of the exit block. */
  266. || single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (cfun)
  267. /* Nor should this be an infinite loop. */
  268. || single_succ (bb) == bb
  269. /* BB may not have an abnormal outgoing edge. */
  270. || (single_succ_edge (bb)->flags & EDGE_ABNORMAL))
  271. return false;
  272. gcc_checking_assert (bb != ENTRY_BLOCK_PTR_FOR_FN (cfun));
  273. locus = single_succ_edge (bb)->goto_locus;
  274. /* There should not be an edge coming from entry, or an EH edge. */
  275. {
  276. edge_iterator ei;
  277. edge e;
  278. FOR_EACH_EDGE (e, ei, bb->preds)
  279. if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun) || (e->flags & EDGE_EH))
  280. return false;
  281. /* If goto_locus of any of the edges differs, prevent removing
  282. the forwarder block for -O0. */
  283. else if (optimize == 0 && e->goto_locus != locus)
  284. return false;
  285. }
  286. /* Now walk through the statements backward. We can ignore labels,
  287. anything else means this is not a forwarder block. */
  288. for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
  289. {
  290. gimple stmt = gsi_stmt (gsi);
  291. switch (gimple_code (stmt))
  292. {
  293. case GIMPLE_LABEL:
  294. if (DECL_NONLOCAL (gimple_label_label (as_a <glabel *> (stmt))))
  295. return false;
  296. if (optimize == 0 && gimple_location (stmt) != locus)
  297. return false;
  298. break;
  299. /* ??? For now, hope there's a corresponding debug
  300. assignment at the destination. */
  301. case GIMPLE_DEBUG:
  302. break;
  303. default:
  304. return false;
  305. }
  306. }
  307. if (current_loops)
  308. {
  309. basic_block dest;
  310. /* Protect loop headers. */
  311. if (bb->loop_father->header == bb)
  312. return false;
  313. dest = EDGE_SUCC (bb, 0)->dest;
  314. /* Protect loop preheaders and latches if requested. */
  315. if (dest->loop_father->header == dest)
  316. {
  317. if (bb->loop_father == dest->loop_father)
  318. {
  319. if (loops_state_satisfies_p (LOOPS_HAVE_SIMPLE_LATCHES))
  320. return false;
  321. /* If bb doesn't have a single predecessor we'd make this
  322. loop have multiple latches. Don't do that if that
  323. would in turn require disambiguating them. */
  324. return (single_pred_p (bb)
  325. || loops_state_satisfies_p
  326. (LOOPS_MAY_HAVE_MULTIPLE_LATCHES));
  327. }
  328. else if (bb->loop_father == loop_outer (dest->loop_father))
  329. return !loops_state_satisfies_p (LOOPS_HAVE_PREHEADERS);
  330. /* Always preserve other edges into loop headers that are
  331. not simple latches or preheaders. */
  332. return false;
  333. }
  334. }
  335. return true;
  336. }
  337. /* If all the PHI nodes in DEST have alternatives for E1 and E2 and
  338. those alternatives are equal in each of the PHI nodes, then return
  339. true, else return false. */
  340. static bool
  341. phi_alternatives_equal (basic_block dest, edge e1, edge e2)
  342. {
  343. int n1 = e1->dest_idx;
  344. int n2 = e2->dest_idx;
  345. gphi_iterator gsi;
  346. for (gsi = gsi_start_phis (dest); !gsi_end_p (gsi); gsi_next (&gsi))
  347. {
  348. gphi *phi = gsi.phi ();
  349. tree val1 = gimple_phi_arg_def (phi, n1);
  350. tree val2 = gimple_phi_arg_def (phi, n2);
  351. gcc_assert (val1 != NULL_TREE);
  352. gcc_assert (val2 != NULL_TREE);
  353. if (!operand_equal_for_phi_arg_p (val1, val2))
  354. return false;
  355. }
  356. return true;
  357. }
  358. /* Removes forwarder block BB. Returns false if this failed. */
  359. static bool
  360. remove_forwarder_block (basic_block bb)
  361. {
  362. edge succ = single_succ_edge (bb), e, s;
  363. basic_block dest = succ->dest;
  364. gimple label;
  365. edge_iterator ei;
  366. gimple_stmt_iterator gsi, gsi_to;
  367. bool can_move_debug_stmts;
  368. /* We check for infinite loops already in tree_forwarder_block_p.
  369. However it may happen that the infinite loop is created
  370. afterwards due to removal of forwarders. */
  371. if (dest == bb)
  372. return false;
  373. /* If the destination block consists of a nonlocal label or is a
  374. EH landing pad, do not merge it. */
  375. label = first_stmt (dest);
  376. if (label)
  377. if (glabel *label_stmt = dyn_cast <glabel *> (label))
  378. if (DECL_NONLOCAL (gimple_label_label (label_stmt))
  379. || EH_LANDING_PAD_NR (gimple_label_label (label_stmt)) != 0)
  380. return false;
  381. /* If there is an abnormal edge to basic block BB, but not into
  382. dest, problems might occur during removal of the phi node at out
  383. of ssa due to overlapping live ranges of registers.
  384. If there is an abnormal edge in DEST, the problems would occur
  385. anyway since cleanup_dead_labels would then merge the labels for
  386. two different eh regions, and rest of exception handling code
  387. does not like it.
  388. So if there is an abnormal edge to BB, proceed only if there is
  389. no abnormal edge to DEST and there are no phi nodes in DEST. */
  390. if (bb_has_abnormal_pred (bb)
  391. && (bb_has_abnormal_pred (dest)
  392. || !gimple_seq_empty_p (phi_nodes (dest))))
  393. return false;
  394. /* If there are phi nodes in DEST, and some of the blocks that are
  395. predecessors of BB are also predecessors of DEST, check that the
  396. phi node arguments match. */
  397. if (!gimple_seq_empty_p (phi_nodes (dest)))
  398. {
  399. FOR_EACH_EDGE (e, ei, bb->preds)
  400. {
  401. s = find_edge (e->src, dest);
  402. if (!s)
  403. continue;
  404. if (!phi_alternatives_equal (dest, succ, s))
  405. return false;
  406. }
  407. }
  408. can_move_debug_stmts = MAY_HAVE_DEBUG_STMTS && single_pred_p (dest);
  409. basic_block pred = NULL;
  410. if (single_pred_p (bb))
  411. pred = single_pred (bb);
  412. /* Redirect the edges. */
  413. for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
  414. {
  415. bitmap_set_bit (cfgcleanup_altered_bbs, e->src->index);
  416. if (e->flags & EDGE_ABNORMAL)
  417. {
  418. /* If there is an abnormal edge, redirect it anyway, and
  419. move the labels to the new block to make it legal. */
  420. s = redirect_edge_succ_nodup (e, dest);
  421. }
  422. else
  423. s = redirect_edge_and_branch (e, dest);
  424. if (s == e)
  425. {
  426. /* Create arguments for the phi nodes, since the edge was not
  427. here before. */
  428. for (gphi_iterator psi = gsi_start_phis (dest);
  429. !gsi_end_p (psi);
  430. gsi_next (&psi))
  431. {
  432. gphi *phi = psi.phi ();
  433. source_location l = gimple_phi_arg_location_from_edge (phi, succ);
  434. tree def = gimple_phi_arg_def (phi, succ->dest_idx);
  435. add_phi_arg (phi, unshare_expr (def), s, l);
  436. }
  437. }
  438. }
  439. /* Move nonlocal labels and computed goto targets as well as user
  440. defined labels and labels with an EH landing pad number to the
  441. new block, so that the redirection of the abnormal edges works,
  442. jump targets end up in a sane place and debug information for
  443. labels is retained. */
  444. gsi_to = gsi_start_bb (dest);
  445. for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
  446. {
  447. tree decl;
  448. label = gsi_stmt (gsi);
  449. if (is_gimple_debug (label))
  450. break;
  451. decl = gimple_label_label (as_a <glabel *> (label));
  452. if (EH_LANDING_PAD_NR (decl) != 0
  453. || DECL_NONLOCAL (decl)
  454. || FORCED_LABEL (decl)
  455. || !DECL_ARTIFICIAL (decl))
  456. {
  457. gsi_remove (&gsi, false);
  458. gsi_insert_before (&gsi_to, label, GSI_SAME_STMT);
  459. }
  460. else
  461. gsi_next (&gsi);
  462. }
  463. /* Move debug statements if the destination has a single predecessor. */
  464. if (can_move_debug_stmts)
  465. {
  466. gsi_to = gsi_after_labels (dest);
  467. for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); )
  468. {
  469. gimple debug = gsi_stmt (gsi);
  470. if (!is_gimple_debug (debug))
  471. break;
  472. gsi_remove (&gsi, false);
  473. gsi_insert_before (&gsi_to, debug, GSI_SAME_STMT);
  474. }
  475. }
  476. bitmap_set_bit (cfgcleanup_altered_bbs, dest->index);
  477. /* Update the dominators. */
  478. if (dom_info_available_p (CDI_DOMINATORS))
  479. {
  480. basic_block dom, dombb, domdest;
  481. dombb = get_immediate_dominator (CDI_DOMINATORS, bb);
  482. domdest = get_immediate_dominator (CDI_DOMINATORS, dest);
  483. if (domdest == bb)
  484. {
  485. /* Shortcut to avoid calling (relatively expensive)
  486. nearest_common_dominator unless necessary. */
  487. dom = dombb;
  488. }
  489. else
  490. dom = nearest_common_dominator (CDI_DOMINATORS, domdest, dombb);
  491. set_immediate_dominator (CDI_DOMINATORS, dest, dom);
  492. }
  493. /* Adjust latch infomation of BB's parent loop as otherwise
  494. the cfg hook has a hard time not to kill the loop. */
  495. if (current_loops && bb->loop_father->latch == bb)
  496. bb->loop_father->latch = pred;
  497. /* And kill the forwarder block. */
  498. delete_basic_block (bb);
  499. return true;
  500. }
  501. /* STMT is a call that has been discovered noreturn. Split the
  502. block to prepare fixing up the CFG and remove LHS.
  503. Return true if cleanup-cfg needs to run. */
  504. bool
  505. fixup_noreturn_call (gimple stmt)
  506. {
  507. basic_block bb = gimple_bb (stmt);
  508. bool changed = false;
  509. if (gimple_call_builtin_p (stmt, BUILT_IN_RETURN))
  510. return false;
  511. /* First split basic block if stmt is not last. */
  512. if (stmt != gsi_stmt (gsi_last_bb (bb)))
  513. {
  514. if (stmt == gsi_stmt (gsi_last_nondebug_bb (bb)))
  515. {
  516. /* Don't split if there are only debug stmts
  517. after stmt, that can result in -fcompare-debug
  518. failures. Remove the debug stmts instead,
  519. they should be all unreachable anyway. */
  520. gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
  521. for (gsi_next (&gsi); !gsi_end_p (gsi); )
  522. gsi_remove (&gsi, true);
  523. }
  524. else
  525. {
  526. split_block (bb, stmt);
  527. changed = true;
  528. }
  529. }
  530. /* If there is an LHS, remove it. */
  531. tree lhs = gimple_call_lhs (stmt);
  532. if (lhs)
  533. {
  534. gimple_call_set_lhs (stmt, NULL_TREE);
  535. /* We need to fix up the SSA name to avoid checking errors. */
  536. if (TREE_CODE (lhs) == SSA_NAME)
  537. {
  538. tree new_var = create_tmp_reg (TREE_TYPE (lhs));
  539. SET_SSA_NAME_VAR_OR_IDENTIFIER (lhs, new_var);
  540. SSA_NAME_DEF_STMT (lhs) = gimple_build_nop ();
  541. set_ssa_default_def (cfun, new_var, lhs);
  542. }
  543. update_stmt (stmt);
  544. }
  545. /* Mark the call as altering control flow. */
  546. if (!gimple_call_ctrl_altering_p (stmt))
  547. {
  548. gimple_call_set_ctrl_altering (stmt, true);
  549. changed = true;
  550. }
  551. return changed;
  552. }
  553. /* Tries to cleanup cfg in basic block BB. Returns true if anything
  554. changes. */
  555. static bool
  556. cleanup_tree_cfg_bb (basic_block bb)
  557. {
  558. bool retval = cleanup_control_flow_bb (bb);
  559. if (tree_forwarder_block_p (bb, false)
  560. && remove_forwarder_block (bb))
  561. return true;
  562. /* Merging the blocks may create new opportunities for folding
  563. conditional branches (due to the elimination of single-valued PHI
  564. nodes). */
  565. if (single_succ_p (bb)
  566. && can_merge_blocks_p (bb, single_succ (bb)))
  567. {
  568. /* If there is a merge opportunity with the predecessor
  569. do nothing now but wait until we process the predecessor.
  570. This happens when we visit BBs in a non-optimal order and
  571. avoids quadratic behavior with adjusting stmts BB pointer. */
  572. if (single_pred_p (bb)
  573. && can_merge_blocks_p (single_pred (bb), bb))
  574. ;
  575. else
  576. {
  577. merge_blocks (bb, single_succ (bb));
  578. return true;
  579. }
  580. }
  581. return retval;
  582. }
  583. /* Iterate the cfg cleanups, while anything changes. */
  584. static bool
  585. cleanup_tree_cfg_1 (void)
  586. {
  587. bool retval = false;
  588. basic_block bb;
  589. unsigned i, n;
  590. /* Prepare the worklists of altered blocks. */
  591. cfgcleanup_altered_bbs = BITMAP_ALLOC (NULL);
  592. /* During forwarder block cleanup, we may redirect edges out of
  593. SWITCH_EXPRs, which can get expensive. So we want to enable
  594. recording of edge to CASE_LABEL_EXPR. */
  595. start_recording_case_labels ();
  596. /* Start by iterating over all basic blocks. We cannot use FOR_EACH_BB_FN,
  597. since the basic blocks may get removed. */
  598. n = last_basic_block_for_fn (cfun);
  599. for (i = NUM_FIXED_BLOCKS; i < n; i++)
  600. {
  601. bb = BASIC_BLOCK_FOR_FN (cfun, i);
  602. if (bb)
  603. retval |= cleanup_tree_cfg_bb (bb);
  604. }
  605. /* Now process the altered blocks, as long as any are available. */
  606. while (!bitmap_empty_p (cfgcleanup_altered_bbs))
  607. {
  608. i = bitmap_first_set_bit (cfgcleanup_altered_bbs);
  609. bitmap_clear_bit (cfgcleanup_altered_bbs, i);
  610. if (i < NUM_FIXED_BLOCKS)
  611. continue;
  612. bb = BASIC_BLOCK_FOR_FN (cfun, i);
  613. if (!bb)
  614. continue;
  615. retval |= cleanup_tree_cfg_bb (bb);
  616. }
  617. end_recording_case_labels ();
  618. BITMAP_FREE (cfgcleanup_altered_bbs);
  619. return retval;
  620. }
  621. /* Remove unreachable blocks and other miscellaneous clean up work.
  622. Return true if the flowgraph was modified, false otherwise. */
  623. static bool
  624. cleanup_tree_cfg_noloop (void)
  625. {
  626. bool changed;
  627. timevar_push (TV_TREE_CLEANUP_CFG);
  628. /* Iterate until there are no more cleanups left to do. If any
  629. iteration changed the flowgraph, set CHANGED to true.
  630. If dominance information is available, there cannot be any unreachable
  631. blocks. */
  632. if (!dom_info_available_p (CDI_DOMINATORS))
  633. {
  634. changed = delete_unreachable_blocks ();
  635. calculate_dominance_info (CDI_DOMINATORS);
  636. }
  637. else
  638. {
  639. #ifdef ENABLE_CHECKING
  640. verify_dominators (CDI_DOMINATORS);
  641. #endif
  642. changed = false;
  643. }
  644. changed |= cleanup_tree_cfg_1 ();
  645. gcc_assert (dom_info_available_p (CDI_DOMINATORS));
  646. compact_blocks ();
  647. #ifdef ENABLE_CHECKING
  648. verify_flow_info ();
  649. #endif
  650. timevar_pop (TV_TREE_CLEANUP_CFG);
  651. if (changed && current_loops)
  652. loops_state_set (LOOPS_NEED_FIXUP);
  653. return changed;
  654. }
  655. /* Repairs loop structures. */
  656. static void
  657. repair_loop_structures (void)
  658. {
  659. bitmap changed_bbs;
  660. unsigned n_new_loops;
  661. calculate_dominance_info (CDI_DOMINATORS);
  662. timevar_push (TV_REPAIR_LOOPS);
  663. changed_bbs = BITMAP_ALLOC (NULL);
  664. n_new_loops = fix_loop_structure (changed_bbs);
  665. /* This usually does nothing. But sometimes parts of cfg that originally
  666. were inside a loop get out of it due to edge removal (since they
  667. become unreachable by back edges from latch). Also a former
  668. irreducible loop can become reducible - in this case force a full
  669. rewrite into loop-closed SSA form. */
  670. if (loops_state_satisfies_p (LOOP_CLOSED_SSA))
  671. rewrite_into_loop_closed_ssa (n_new_loops ? NULL : changed_bbs,
  672. TODO_update_ssa);
  673. BITMAP_FREE (changed_bbs);
  674. #ifdef ENABLE_CHECKING
  675. verify_loop_structure ();
  676. #endif
  677. scev_reset ();
  678. timevar_pop (TV_REPAIR_LOOPS);
  679. }
  680. /* Cleanup cfg and repair loop structures. */
  681. bool
  682. cleanup_tree_cfg (void)
  683. {
  684. bool changed = cleanup_tree_cfg_noloop ();
  685. if (current_loops != NULL
  686. && loops_state_satisfies_p (LOOPS_NEED_FIXUP))
  687. repair_loop_structures ();
  688. return changed;
  689. }
  690. /* Tries to merge the PHI nodes at BB into those at BB's sole successor.
  691. Returns true if successful. */
  692. static bool
  693. remove_forwarder_block_with_phi (basic_block bb)
  694. {
  695. edge succ = single_succ_edge (bb);
  696. basic_block dest = succ->dest;
  697. gimple label;
  698. basic_block dombb, domdest, dom;
  699. /* We check for infinite loops already in tree_forwarder_block_p.
  700. However it may happen that the infinite loop is created
  701. afterwards due to removal of forwarders. */
  702. if (dest == bb)
  703. return false;
  704. /* If the destination block consists of a nonlocal label, do not
  705. merge it. */
  706. label = first_stmt (dest);
  707. if (label)
  708. if (glabel *label_stmt = dyn_cast <glabel *> (label))
  709. if (DECL_NONLOCAL (gimple_label_label (label_stmt)))
  710. return false;
  711. /* Record BB's single pred in case we need to update the father
  712. loop's latch information later. */
  713. basic_block pred = NULL;
  714. if (single_pred_p (bb))
  715. pred = single_pred (bb);
  716. /* Redirect each incoming edge to BB to DEST. */
  717. while (EDGE_COUNT (bb->preds) > 0)
  718. {
  719. edge e = EDGE_PRED (bb, 0), s;
  720. gphi_iterator gsi;
  721. s = find_edge (e->src, dest);
  722. if (s)
  723. {
  724. /* We already have an edge S from E->src to DEST. If S and
  725. E->dest's sole successor edge have the same PHI arguments
  726. at DEST, redirect S to DEST. */
  727. if (phi_alternatives_equal (dest, s, succ))
  728. {
  729. e = redirect_edge_and_branch (e, dest);
  730. redirect_edge_var_map_clear (e);
  731. continue;
  732. }
  733. /* PHI arguments are different. Create a forwarder block by
  734. splitting E so that we can merge PHI arguments on E to
  735. DEST. */
  736. e = single_succ_edge (split_edge (e));
  737. }
  738. s = redirect_edge_and_branch (e, dest);
  739. /* redirect_edge_and_branch must not create a new edge. */
  740. gcc_assert (s == e);
  741. /* Add to the PHI nodes at DEST each PHI argument removed at the
  742. destination of E. */
  743. for (gsi = gsi_start_phis (dest);
  744. !gsi_end_p (gsi);
  745. gsi_next (&gsi))
  746. {
  747. gphi *phi = gsi.phi ();
  748. tree def = gimple_phi_arg_def (phi, succ->dest_idx);
  749. source_location locus = gimple_phi_arg_location_from_edge (phi, succ);
  750. if (TREE_CODE (def) == SSA_NAME)
  751. {
  752. /* If DEF is one of the results of PHI nodes removed during
  753. redirection, replace it with the PHI argument that used
  754. to be on E. */
  755. vec<edge_var_map> *head = redirect_edge_var_map_vector (e);
  756. size_t length = head ? head->length () : 0;
  757. for (size_t i = 0; i < length; i++)
  758. {
  759. edge_var_map *vm = &(*head)[i];
  760. tree old_arg = redirect_edge_var_map_result (vm);
  761. tree new_arg = redirect_edge_var_map_def (vm);
  762. if (def == old_arg)
  763. {
  764. def = new_arg;
  765. locus = redirect_edge_var_map_location (vm);
  766. break;
  767. }
  768. }
  769. }
  770. add_phi_arg (phi, def, s, locus);
  771. }
  772. redirect_edge_var_map_clear (e);
  773. }
  774. /* Update the dominators. */
  775. dombb = get_immediate_dominator (CDI_DOMINATORS, bb);
  776. domdest = get_immediate_dominator (CDI_DOMINATORS, dest);
  777. if (domdest == bb)
  778. {
  779. /* Shortcut to avoid calling (relatively expensive)
  780. nearest_common_dominator unless necessary. */
  781. dom = dombb;
  782. }
  783. else
  784. dom = nearest_common_dominator (CDI_DOMINATORS, domdest, dombb);
  785. set_immediate_dominator (CDI_DOMINATORS, dest, dom);
  786. /* Adjust latch infomation of BB's parent loop as otherwise
  787. the cfg hook has a hard time not to kill the loop. */
  788. if (current_loops && bb->loop_father->latch == bb)
  789. bb->loop_father->latch = pred;
  790. /* Remove BB since all of BB's incoming edges have been redirected
  791. to DEST. */
  792. delete_basic_block (bb);
  793. return true;
  794. }
  795. /* This pass merges PHI nodes if one feeds into another. For example,
  796. suppose we have the following:
  797. goto <bb 9> (<L9>);
  798. <L8>:;
  799. tem_17 = foo ();
  800. # tem_6 = PHI <tem_17(8), tem_23(7)>;
  801. <L9>:;
  802. # tem_3 = PHI <tem_6(9), tem_2(5)>;
  803. <L10>:;
  804. Then we merge the first PHI node into the second one like so:
  805. goto <bb 9> (<L10>);
  806. <L8>:;
  807. tem_17 = foo ();
  808. # tem_3 = PHI <tem_23(7), tem_2(5), tem_17(8)>;
  809. <L10>:;
  810. */
  811. namespace {
  812. const pass_data pass_data_merge_phi =
  813. {
  814. GIMPLE_PASS, /* type */
  815. "mergephi", /* name */
  816. OPTGROUP_NONE, /* optinfo_flags */
  817. TV_TREE_MERGE_PHI, /* tv_id */
  818. ( PROP_cfg | PROP_ssa ), /* properties_required */
  819. 0, /* properties_provided */
  820. 0, /* properties_destroyed */
  821. 0, /* todo_flags_start */
  822. 0, /* todo_flags_finish */
  823. };
  824. class pass_merge_phi : public gimple_opt_pass
  825. {
  826. public:
  827. pass_merge_phi (gcc::context *ctxt)
  828. : gimple_opt_pass (pass_data_merge_phi, ctxt)
  829. {}
  830. /* opt_pass methods: */
  831. opt_pass * clone () { return new pass_merge_phi (m_ctxt); }
  832. virtual unsigned int execute (function *);
  833. }; // class pass_merge_phi
  834. unsigned int
  835. pass_merge_phi::execute (function *fun)
  836. {
  837. basic_block *worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (fun));
  838. basic_block *current = worklist;
  839. basic_block bb;
  840. calculate_dominance_info (CDI_DOMINATORS);
  841. /* Find all PHI nodes that we may be able to merge. */
  842. FOR_EACH_BB_FN (bb, fun)
  843. {
  844. basic_block dest;
  845. /* Look for a forwarder block with PHI nodes. */
  846. if (!tree_forwarder_block_p (bb, true))
  847. continue;
  848. dest = single_succ (bb);
  849. /* We have to feed into another basic block with PHI
  850. nodes. */
  851. if (gimple_seq_empty_p (phi_nodes (dest))
  852. /* We don't want to deal with a basic block with
  853. abnormal edges. */
  854. || bb_has_abnormal_pred (bb))
  855. continue;
  856. if (!dominated_by_p (CDI_DOMINATORS, dest, bb))
  857. {
  858. /* If BB does not dominate DEST, then the PHI nodes at
  859. DEST must be the only users of the results of the PHI
  860. nodes at BB. */
  861. *current++ = bb;
  862. }
  863. else
  864. {
  865. gphi_iterator gsi;
  866. unsigned int dest_idx = single_succ_edge (bb)->dest_idx;
  867. /* BB dominates DEST. There may be many users of the PHI
  868. nodes in BB. However, there is still a trivial case we
  869. can handle. If the result of every PHI in BB is used
  870. only by a PHI in DEST, then we can trivially merge the
  871. PHI nodes from BB into DEST. */
  872. for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
  873. gsi_next (&gsi))
  874. {
  875. gphi *phi = gsi.phi ();
  876. tree result = gimple_phi_result (phi);
  877. use_operand_p imm_use;
  878. gimple use_stmt;
  879. /* If the PHI's result is never used, then we can just
  880. ignore it. */
  881. if (has_zero_uses (result))
  882. continue;
  883. /* Get the single use of the result of this PHI node. */
  884. if (!single_imm_use (result, &imm_use, &use_stmt)
  885. || gimple_code (use_stmt) != GIMPLE_PHI
  886. || gimple_bb (use_stmt) != dest
  887. || gimple_phi_arg_def (use_stmt, dest_idx) != result)
  888. break;
  889. }
  890. /* If the loop above iterated through all the PHI nodes
  891. in BB, then we can merge the PHIs from BB into DEST. */
  892. if (gsi_end_p (gsi))
  893. *current++ = bb;
  894. }
  895. }
  896. /* Now let's drain WORKLIST. */
  897. bool changed = false;
  898. while (current != worklist)
  899. {
  900. bb = *--current;
  901. changed |= remove_forwarder_block_with_phi (bb);
  902. }
  903. free (worklist);
  904. /* Removing forwarder blocks can cause formerly irreducible loops
  905. to become reducible if we merged two entry blocks. */
  906. if (changed
  907. && current_loops)
  908. loops_state_set (LOOPS_NEED_FIXUP);
  909. return 0;
  910. }
  911. } // anon namespace
  912. gimple_opt_pass *
  913. make_pass_merge_phi (gcc::context *ctxt)
  914. {
  915. return new pass_merge_phi (ctxt);
  916. }
  917. /* Pass: cleanup the CFG just before expanding trees to RTL.
  918. This is just a round of label cleanups and case node grouping
  919. because after the tree optimizers have run such cleanups may
  920. be necessary. */
  921. static unsigned int
  922. execute_cleanup_cfg_post_optimizing (void)
  923. {
  924. unsigned int todo = execute_fixup_cfg ();
  925. if (cleanup_tree_cfg ())
  926. {
  927. todo &= ~TODO_cleanup_cfg;
  928. todo |= TODO_update_ssa;
  929. }
  930. maybe_remove_unreachable_handlers ();
  931. cleanup_dead_labels ();
  932. group_case_labels ();
  933. if ((flag_compare_debug_opt || flag_compare_debug)
  934. && flag_dump_final_insns)
  935. {
  936. FILE *final_output = fopen (flag_dump_final_insns, "a");
  937. if (!final_output)
  938. {
  939. error ("could not open final insn dump file %qs: %m",
  940. flag_dump_final_insns);
  941. flag_dump_final_insns = NULL;
  942. }
  943. else
  944. {
  945. int save_unnumbered = flag_dump_unnumbered;
  946. int save_noaddr = flag_dump_noaddr;
  947. flag_dump_noaddr = flag_dump_unnumbered = 1;
  948. fprintf (final_output, "\n");
  949. dump_enumerated_decls (final_output, dump_flags | TDF_NOUID);
  950. flag_dump_noaddr = save_noaddr;
  951. flag_dump_unnumbered = save_unnumbered;
  952. if (fclose (final_output))
  953. {
  954. error ("could not close final insn dump file %qs: %m",
  955. flag_dump_final_insns);
  956. flag_dump_final_insns = NULL;
  957. }
  958. }
  959. }
  960. return todo;
  961. }
  962. namespace {
  963. const pass_data pass_data_cleanup_cfg_post_optimizing =
  964. {
  965. GIMPLE_PASS, /* type */
  966. "optimized", /* name */
  967. OPTGROUP_NONE, /* optinfo_flags */
  968. TV_TREE_CLEANUP_CFG, /* tv_id */
  969. PROP_cfg, /* properties_required */
  970. 0, /* properties_provided */
  971. 0, /* properties_destroyed */
  972. 0, /* todo_flags_start */
  973. TODO_remove_unused_locals, /* todo_flags_finish */
  974. };
  975. class pass_cleanup_cfg_post_optimizing : public gimple_opt_pass
  976. {
  977. public:
  978. pass_cleanup_cfg_post_optimizing (gcc::context *ctxt)
  979. : gimple_opt_pass (pass_data_cleanup_cfg_post_optimizing, ctxt)
  980. {}
  981. /* opt_pass methods: */
  982. virtual unsigned int execute (function *)
  983. {
  984. return execute_cleanup_cfg_post_optimizing ();
  985. }
  986. }; // class pass_cleanup_cfg_post_optimizing
  987. } // anon namespace
  988. gimple_opt_pass *
  989. make_pass_cleanup_cfg_post_optimizing (gcc::context *ctxt)
  990. {
  991. return new pass_cleanup_cfg_post_optimizing (ctxt);
  992. }