gimple-low.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. /* GIMPLE lowering pass. Converts High GIMPLE into Low GIMPLE.
  2. Copyright (C) 2003-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. 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 "tree-nested.h"
  31. #include "calls.h"
  32. #include "predict.h"
  33. #include "hard-reg-set.h"
  34. #include "input.h"
  35. #include "function.h"
  36. #include "basic-block.h"
  37. #include "tree-ssa-alias.h"
  38. #include "internal-fn.h"
  39. #include "gimple-expr.h"
  40. #include "is-a.h"
  41. #include "gimple.h"
  42. #include "gimple-iterator.h"
  43. #include "tree-iterator.h"
  44. #include "tree-inline.h"
  45. #include "flags.h"
  46. #include "diagnostic-core.h"
  47. #include "tree-pass.h"
  48. #include "langhooks.h"
  49. #include "gimple-low.h"
  50. #include "tree-nested.h"
  51. /* The differences between High GIMPLE and Low GIMPLE are the
  52. following:
  53. 1- Lexical scopes are removed (i.e., GIMPLE_BIND disappears).
  54. 2- GIMPLE_TRY and GIMPLE_CATCH are converted to abnormal control
  55. flow and exception regions are built as an on-the-side region
  56. hierarchy (See tree-eh.c:lower_eh_constructs).
  57. 3- Multiple identical return statements are grouped into a single
  58. return and gotos to the unique return site. */
  59. /* Match a return statement with a label. During lowering, we identify
  60. identical return statements and replace duplicates with a jump to
  61. the corresponding label. */
  62. struct return_statements_t
  63. {
  64. tree label;
  65. greturn *stmt;
  66. };
  67. typedef struct return_statements_t return_statements_t;
  68. struct lower_data
  69. {
  70. /* Block the current statement belongs to. */
  71. tree block;
  72. /* A vector of label and return statements to be moved to the end
  73. of the function. */
  74. vec<return_statements_t> return_statements;
  75. /* True if the current statement cannot fall through. */
  76. bool cannot_fallthru;
  77. };
  78. static void lower_stmt (gimple_stmt_iterator *, struct lower_data *);
  79. static void lower_gimple_bind (gimple_stmt_iterator *, struct lower_data *);
  80. static void lower_try_catch (gimple_stmt_iterator *, struct lower_data *);
  81. static void lower_gimple_return (gimple_stmt_iterator *, struct lower_data *);
  82. static void lower_builtin_setjmp (gimple_stmt_iterator *);
  83. static void lower_builtin_posix_memalign (gimple_stmt_iterator *);
  84. /* Lower the body of current_function_decl from High GIMPLE into Low
  85. GIMPLE. */
  86. static unsigned int
  87. lower_function_body (void)
  88. {
  89. struct lower_data data;
  90. gimple_seq body = gimple_body (current_function_decl);
  91. gimple_seq lowered_body;
  92. gimple_stmt_iterator i;
  93. gimple bind;
  94. gimple x;
  95. /* The gimplifier should've left a body of exactly one statement,
  96. namely a GIMPLE_BIND. */
  97. gcc_assert (gimple_seq_first (body) == gimple_seq_last (body)
  98. && gimple_code (gimple_seq_first_stmt (body)) == GIMPLE_BIND);
  99. memset (&data, 0, sizeof (data));
  100. data.block = DECL_INITIAL (current_function_decl);
  101. BLOCK_SUBBLOCKS (data.block) = NULL_TREE;
  102. BLOCK_CHAIN (data.block) = NULL_TREE;
  103. TREE_ASM_WRITTEN (data.block) = 1;
  104. data.return_statements.create (8);
  105. bind = gimple_seq_first_stmt (body);
  106. lowered_body = NULL;
  107. gimple_seq_add_stmt (&lowered_body, bind);
  108. i = gsi_start (lowered_body);
  109. lower_gimple_bind (&i, &data);
  110. i = gsi_last (lowered_body);
  111. /* If the function falls off the end, we need a null return statement.
  112. If we've already got one in the return_statements vector, we don't
  113. need to do anything special. Otherwise build one by hand. */
  114. bool may_fallthru = gimple_seq_may_fallthru (lowered_body);
  115. if (may_fallthru
  116. && (data.return_statements.is_empty ()
  117. || (gimple_return_retval (data.return_statements.last().stmt)
  118. != NULL)))
  119. {
  120. x = gimple_build_return (NULL);
  121. gimple_set_location (x, cfun->function_end_locus);
  122. gimple_set_block (x, DECL_INITIAL (current_function_decl));
  123. gsi_insert_after (&i, x, GSI_CONTINUE_LINKING);
  124. may_fallthru = false;
  125. }
  126. /* If we lowered any return statements, emit the representative
  127. at the end of the function. */
  128. while (!data.return_statements.is_empty ())
  129. {
  130. return_statements_t t = data.return_statements.pop ();
  131. x = gimple_build_label (t.label);
  132. gsi_insert_after (&i, x, GSI_CONTINUE_LINKING);
  133. gsi_insert_after (&i, t.stmt, GSI_CONTINUE_LINKING);
  134. if (may_fallthru)
  135. {
  136. /* Remove the line number from the representative return statement.
  137. It now fills in for the fallthru too. Failure to remove this
  138. will result in incorrect results for coverage analysis. */
  139. gimple_set_location (t.stmt, UNKNOWN_LOCATION);
  140. may_fallthru = false;
  141. }
  142. }
  143. /* Once the old body has been lowered, replace it with the new
  144. lowered sequence. */
  145. gimple_set_body (current_function_decl, lowered_body);
  146. gcc_assert (data.block == DECL_INITIAL (current_function_decl));
  147. BLOCK_SUBBLOCKS (data.block)
  148. = blocks_nreverse (BLOCK_SUBBLOCKS (data.block));
  149. clear_block_marks (data.block);
  150. data.return_statements.release ();
  151. return 0;
  152. }
  153. namespace {
  154. const pass_data pass_data_lower_cf =
  155. {
  156. GIMPLE_PASS, /* type */
  157. "lower", /* name */
  158. OPTGROUP_NONE, /* optinfo_flags */
  159. TV_NONE, /* tv_id */
  160. PROP_gimple_any, /* properties_required */
  161. PROP_gimple_lcf, /* properties_provided */
  162. 0, /* properties_destroyed */
  163. 0, /* todo_flags_start */
  164. 0, /* todo_flags_finish */
  165. };
  166. class pass_lower_cf : public gimple_opt_pass
  167. {
  168. public:
  169. pass_lower_cf (gcc::context *ctxt)
  170. : gimple_opt_pass (pass_data_lower_cf, ctxt)
  171. {}
  172. /* opt_pass methods: */
  173. virtual unsigned int execute (function *) { return lower_function_body (); }
  174. }; // class pass_lower_cf
  175. } // anon namespace
  176. gimple_opt_pass *
  177. make_pass_lower_cf (gcc::context *ctxt)
  178. {
  179. return new pass_lower_cf (ctxt);
  180. }
  181. /* Lower sequence SEQ. Unlike gimplification the statements are not relowered
  182. when they are changed -- if this has to be done, the lowering routine must
  183. do it explicitly. DATA is passed through the recursion. */
  184. static void
  185. lower_sequence (gimple_seq *seq, struct lower_data *data)
  186. {
  187. gimple_stmt_iterator gsi;
  188. for (gsi = gsi_start (*seq); !gsi_end_p (gsi); )
  189. lower_stmt (&gsi, data);
  190. }
  191. /* Lower the OpenMP directive statement pointed by GSI. DATA is
  192. passed through the recursion. */
  193. static void
  194. lower_omp_directive (gimple_stmt_iterator *gsi, struct lower_data *data)
  195. {
  196. gimple stmt;
  197. stmt = gsi_stmt (*gsi);
  198. lower_sequence (gimple_omp_body_ptr (stmt), data);
  199. gsi_insert_seq_after (gsi, gimple_omp_body (stmt), GSI_CONTINUE_LINKING);
  200. gimple_omp_set_body (stmt, NULL);
  201. gsi_next (gsi);
  202. }
  203. /* Lower statement GSI. DATA is passed through the recursion. We try to
  204. track the fallthruness of statements and get rid of unreachable return
  205. statements in order to prevent the EH lowering pass from adding useless
  206. edges that can cause bogus warnings to be issued later; this guess need
  207. not be 100% accurate, simply be conservative and reset cannot_fallthru
  208. to false if we don't know. */
  209. static void
  210. lower_stmt (gimple_stmt_iterator *gsi, struct lower_data *data)
  211. {
  212. gimple stmt = gsi_stmt (*gsi);
  213. gimple_set_block (stmt, data->block);
  214. switch (gimple_code (stmt))
  215. {
  216. case GIMPLE_BIND:
  217. lower_gimple_bind (gsi, data);
  218. /* Propagate fallthruness. */
  219. return;
  220. case GIMPLE_COND:
  221. case GIMPLE_GOTO:
  222. case GIMPLE_SWITCH:
  223. data->cannot_fallthru = true;
  224. gsi_next (gsi);
  225. return;
  226. case GIMPLE_RETURN:
  227. if (data->cannot_fallthru)
  228. {
  229. gsi_remove (gsi, false);
  230. /* Propagate fallthruness. */
  231. }
  232. else
  233. {
  234. lower_gimple_return (gsi, data);
  235. data->cannot_fallthru = true;
  236. }
  237. return;
  238. case GIMPLE_TRY:
  239. if (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH)
  240. lower_try_catch (gsi, data);
  241. else
  242. {
  243. /* It must be a GIMPLE_TRY_FINALLY. */
  244. bool cannot_fallthru;
  245. lower_sequence (gimple_try_eval_ptr (stmt), data);
  246. cannot_fallthru = data->cannot_fallthru;
  247. /* The finally clause is always executed after the try clause,
  248. so if it does not fall through, then the try-finally will not
  249. fall through. Otherwise, if the try clause does not fall
  250. through, then when the finally clause falls through it will
  251. resume execution wherever the try clause was going. So the
  252. whole try-finally will only fall through if both the try
  253. clause and the finally clause fall through. */
  254. data->cannot_fallthru = false;
  255. lower_sequence (gimple_try_cleanup_ptr (stmt), data);
  256. data->cannot_fallthru |= cannot_fallthru;
  257. gsi_next (gsi);
  258. }
  259. return;
  260. case GIMPLE_EH_ELSE:
  261. {
  262. geh_else *eh_else_stmt = as_a <geh_else *> (stmt);
  263. lower_sequence (gimple_eh_else_n_body_ptr (eh_else_stmt), data);
  264. lower_sequence (gimple_eh_else_e_body_ptr (eh_else_stmt), data);
  265. }
  266. break;
  267. case GIMPLE_NOP:
  268. case GIMPLE_ASM:
  269. case GIMPLE_ASSIGN:
  270. case GIMPLE_PREDICT:
  271. case GIMPLE_LABEL:
  272. case GIMPLE_EH_MUST_NOT_THROW:
  273. case GIMPLE_OMP_FOR:
  274. case GIMPLE_OMP_SECTIONS:
  275. case GIMPLE_OMP_SECTIONS_SWITCH:
  276. case GIMPLE_OMP_SECTION:
  277. case GIMPLE_OMP_SINGLE:
  278. case GIMPLE_OMP_MASTER:
  279. case GIMPLE_OMP_TASKGROUP:
  280. case GIMPLE_OMP_ORDERED:
  281. case GIMPLE_OMP_CRITICAL:
  282. case GIMPLE_OMP_RETURN:
  283. case GIMPLE_OMP_ATOMIC_LOAD:
  284. case GIMPLE_OMP_ATOMIC_STORE:
  285. case GIMPLE_OMP_CONTINUE:
  286. break;
  287. case GIMPLE_CALL:
  288. {
  289. tree decl = gimple_call_fndecl (stmt);
  290. unsigned i;
  291. for (i = 0; i < gimple_call_num_args (stmt); i++)
  292. {
  293. tree arg = gimple_call_arg (stmt, i);
  294. if (EXPR_P (arg))
  295. TREE_SET_BLOCK (arg, data->block);
  296. }
  297. if (decl
  298. && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
  299. {
  300. if (DECL_FUNCTION_CODE (decl) == BUILT_IN_SETJMP)
  301. {
  302. lower_builtin_setjmp (gsi);
  303. data->cannot_fallthru = false;
  304. return;
  305. }
  306. else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_POSIX_MEMALIGN
  307. && flag_tree_bit_ccp)
  308. {
  309. lower_builtin_posix_memalign (gsi);
  310. return;
  311. }
  312. }
  313. if (decl && (flags_from_decl_or_type (decl) & ECF_NORETURN))
  314. {
  315. data->cannot_fallthru = true;
  316. gsi_next (gsi);
  317. return;
  318. }
  319. }
  320. break;
  321. case GIMPLE_OMP_PARALLEL:
  322. case GIMPLE_OMP_TASK:
  323. case GIMPLE_OMP_TARGET:
  324. case GIMPLE_OMP_TEAMS:
  325. data->cannot_fallthru = false;
  326. lower_omp_directive (gsi, data);
  327. data->cannot_fallthru = false;
  328. return;
  329. case GIMPLE_TRANSACTION:
  330. lower_sequence (gimple_transaction_body_ptr (
  331. as_a <gtransaction *> (stmt)),
  332. data);
  333. break;
  334. default:
  335. gcc_unreachable ();
  336. }
  337. data->cannot_fallthru = false;
  338. gsi_next (gsi);
  339. }
  340. /* Lower a bind_expr TSI. DATA is passed through the recursion. */
  341. static void
  342. lower_gimple_bind (gimple_stmt_iterator *gsi, struct lower_data *data)
  343. {
  344. tree old_block = data->block;
  345. gbind *stmt = as_a <gbind *> (gsi_stmt (*gsi));
  346. tree new_block = gimple_bind_block (stmt);
  347. if (new_block)
  348. {
  349. if (new_block == old_block)
  350. {
  351. /* The outermost block of the original function may not be the
  352. outermost statement chain of the gimplified function. So we
  353. may see the outermost block just inside the function. */
  354. gcc_assert (new_block == DECL_INITIAL (current_function_decl));
  355. new_block = NULL;
  356. }
  357. else
  358. {
  359. /* We do not expect to handle duplicate blocks. */
  360. gcc_assert (!TREE_ASM_WRITTEN (new_block));
  361. TREE_ASM_WRITTEN (new_block) = 1;
  362. /* Block tree may get clobbered by inlining. Normally this would
  363. be fixed in rest_of_decl_compilation using block notes, but
  364. since we are not going to emit them, it is up to us. */
  365. BLOCK_CHAIN (new_block) = BLOCK_SUBBLOCKS (old_block);
  366. BLOCK_SUBBLOCKS (old_block) = new_block;
  367. BLOCK_SUBBLOCKS (new_block) = NULL_TREE;
  368. BLOCK_SUPERCONTEXT (new_block) = old_block;
  369. data->block = new_block;
  370. }
  371. }
  372. record_vars (gimple_bind_vars (stmt));
  373. lower_sequence (gimple_bind_body_ptr (stmt), data);
  374. if (new_block)
  375. {
  376. gcc_assert (data->block == new_block);
  377. BLOCK_SUBBLOCKS (new_block)
  378. = blocks_nreverse (BLOCK_SUBBLOCKS (new_block));
  379. data->block = old_block;
  380. }
  381. /* The GIMPLE_BIND no longer carries any useful information -- kill it. */
  382. gsi_insert_seq_before (gsi, gimple_bind_body (stmt), GSI_SAME_STMT);
  383. gsi_remove (gsi, false);
  384. }
  385. /* Same as above, but for a GIMPLE_TRY_CATCH. */
  386. static void
  387. lower_try_catch (gimple_stmt_iterator *gsi, struct lower_data *data)
  388. {
  389. bool cannot_fallthru;
  390. gimple stmt = gsi_stmt (*gsi);
  391. gimple_stmt_iterator i;
  392. /* We don't handle GIMPLE_TRY_FINALLY. */
  393. gcc_assert (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH);
  394. lower_sequence (gimple_try_eval_ptr (stmt), data);
  395. cannot_fallthru = data->cannot_fallthru;
  396. i = gsi_start (*gimple_try_cleanup_ptr (stmt));
  397. switch (gimple_code (gsi_stmt (i)))
  398. {
  399. case GIMPLE_CATCH:
  400. /* We expect to see a sequence of GIMPLE_CATCH stmts, each with a
  401. catch expression and a body. The whole try/catch may fall
  402. through iff any of the catch bodies falls through. */
  403. for (; !gsi_end_p (i); gsi_next (&i))
  404. {
  405. data->cannot_fallthru = false;
  406. lower_sequence (gimple_catch_handler_ptr (
  407. as_a <gcatch *> (gsi_stmt (i))),
  408. data);
  409. if (!data->cannot_fallthru)
  410. cannot_fallthru = false;
  411. }
  412. break;
  413. case GIMPLE_EH_FILTER:
  414. /* The exception filter expression only matters if there is an
  415. exception. If the exception does not match EH_FILTER_TYPES,
  416. we will execute EH_FILTER_FAILURE, and we will fall through
  417. if that falls through. If the exception does match
  418. EH_FILTER_TYPES, the stack unwinder will continue up the
  419. stack, so we will not fall through. We don't know whether we
  420. will throw an exception which matches EH_FILTER_TYPES or not,
  421. so we just ignore EH_FILTER_TYPES and assume that we might
  422. throw an exception which doesn't match. */
  423. data->cannot_fallthru = false;
  424. lower_sequence (gimple_eh_filter_failure_ptr (gsi_stmt (i)), data);
  425. if (!data->cannot_fallthru)
  426. cannot_fallthru = false;
  427. break;
  428. default:
  429. /* This case represents statements to be executed when an
  430. exception occurs. Those statements are implicitly followed
  431. by a GIMPLE_RESX to resume execution after the exception. So
  432. in this case the try/catch never falls through. */
  433. data->cannot_fallthru = false;
  434. lower_sequence (gimple_try_cleanup_ptr (stmt), data);
  435. break;
  436. }
  437. data->cannot_fallthru = cannot_fallthru;
  438. gsi_next (gsi);
  439. }
  440. /* Try to determine whether a TRY_CATCH expression can fall through.
  441. This is a subroutine of gimple_stmt_may_fallthru. */
  442. static bool
  443. gimple_try_catch_may_fallthru (gtry *stmt)
  444. {
  445. gimple_stmt_iterator i;
  446. /* We don't handle GIMPLE_TRY_FINALLY. */
  447. gcc_assert (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH);
  448. /* If the TRY block can fall through, the whole TRY_CATCH can
  449. fall through. */
  450. if (gimple_seq_may_fallthru (gimple_try_eval (stmt)))
  451. return true;
  452. i = gsi_start (*gimple_try_cleanup_ptr (stmt));
  453. switch (gimple_code (gsi_stmt (i)))
  454. {
  455. case GIMPLE_CATCH:
  456. /* We expect to see a sequence of GIMPLE_CATCH stmts, each with a
  457. catch expression and a body. The whole try/catch may fall
  458. through iff any of the catch bodies falls through. */
  459. for (; !gsi_end_p (i); gsi_next (&i))
  460. {
  461. if (gimple_seq_may_fallthru (gimple_catch_handler (
  462. as_a <gcatch *> (gsi_stmt (i)))))
  463. return true;
  464. }
  465. return false;
  466. case GIMPLE_EH_FILTER:
  467. /* The exception filter expression only matters if there is an
  468. exception. If the exception does not match EH_FILTER_TYPES,
  469. we will execute EH_FILTER_FAILURE, and we will fall through
  470. if that falls through. If the exception does match
  471. EH_FILTER_TYPES, the stack unwinder will continue up the
  472. stack, so we will not fall through. We don't know whether we
  473. will throw an exception which matches EH_FILTER_TYPES or not,
  474. so we just ignore EH_FILTER_TYPES and assume that we might
  475. throw an exception which doesn't match. */
  476. return gimple_seq_may_fallthru (gimple_eh_filter_failure (gsi_stmt (i)));
  477. default:
  478. /* This case represents statements to be executed when an
  479. exception occurs. Those statements are implicitly followed
  480. by a GIMPLE_RESX to resume execution after the exception. So
  481. in this case the try/catch never falls through. */
  482. return false;
  483. }
  484. }
  485. /* Try to determine if we can continue executing the statement
  486. immediately following STMT. This guess need not be 100% accurate;
  487. simply be conservative and return true if we don't know. This is
  488. used only to avoid stupidly generating extra code. If we're wrong,
  489. we'll just delete the extra code later. */
  490. bool
  491. gimple_stmt_may_fallthru (gimple stmt)
  492. {
  493. if (!stmt)
  494. return true;
  495. switch (gimple_code (stmt))
  496. {
  497. case GIMPLE_GOTO:
  498. case GIMPLE_RETURN:
  499. case GIMPLE_RESX:
  500. /* Easy cases. If the last statement of the seq implies
  501. control transfer, then we can't fall through. */
  502. return false;
  503. case GIMPLE_SWITCH:
  504. /* Switch has already been lowered and represents a branch
  505. to a selected label and hence can't fall through. */
  506. return false;
  507. case GIMPLE_COND:
  508. /* GIMPLE_COND's are already lowered into a two-way branch. They
  509. can't fall through. */
  510. return false;
  511. case GIMPLE_BIND:
  512. return gimple_seq_may_fallthru (
  513. gimple_bind_body (as_a <gbind *> (stmt)));
  514. case GIMPLE_TRY:
  515. if (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH)
  516. return gimple_try_catch_may_fallthru (as_a <gtry *> (stmt));
  517. /* It must be a GIMPLE_TRY_FINALLY. */
  518. /* The finally clause is always executed after the try clause,
  519. so if it does not fall through, then the try-finally will not
  520. fall through. Otherwise, if the try clause does not fall
  521. through, then when the finally clause falls through it will
  522. resume execution wherever the try clause was going. So the
  523. whole try-finally will only fall through if both the try
  524. clause and the finally clause fall through. */
  525. return (gimple_seq_may_fallthru (gimple_try_eval (stmt))
  526. && gimple_seq_may_fallthru (gimple_try_cleanup (stmt)));
  527. case GIMPLE_EH_ELSE:
  528. {
  529. geh_else *eh_else_stmt = as_a <geh_else *> (stmt);
  530. return (gimple_seq_may_fallthru (gimple_eh_else_n_body (eh_else_stmt))
  531. || gimple_seq_may_fallthru (gimple_eh_else_e_body (
  532. eh_else_stmt)));
  533. }
  534. case GIMPLE_CALL:
  535. /* Functions that do not return do not fall through. */
  536. return (gimple_call_flags (stmt) & ECF_NORETURN) == 0;
  537. default:
  538. return true;
  539. }
  540. }
  541. /* Same as gimple_stmt_may_fallthru, but for the gimple sequence SEQ. */
  542. bool
  543. gimple_seq_may_fallthru (gimple_seq seq)
  544. {
  545. return gimple_stmt_may_fallthru (gimple_seq_last_stmt (seq));
  546. }
  547. /* Lower a GIMPLE_RETURN GSI. DATA is passed through the recursion. */
  548. static void
  549. lower_gimple_return (gimple_stmt_iterator *gsi, struct lower_data *data)
  550. {
  551. greturn *stmt = as_a <greturn *> (gsi_stmt (*gsi));
  552. gimple t;
  553. int i;
  554. return_statements_t tmp_rs;
  555. /* Match this up with an existing return statement that's been created. */
  556. for (i = data->return_statements.length () - 1;
  557. i >= 0; i--)
  558. {
  559. tmp_rs = data->return_statements[i];
  560. if (gimple_return_retval (stmt) == gimple_return_retval (tmp_rs.stmt))
  561. {
  562. /* Remove the line number from the representative return statement.
  563. It now fills in for many such returns. Failure to remove this
  564. will result in incorrect results for coverage analysis. */
  565. gimple_set_location (tmp_rs.stmt, UNKNOWN_LOCATION);
  566. goto found;
  567. }
  568. }
  569. /* Not found. Create a new label and record the return statement. */
  570. tmp_rs.label = create_artificial_label (cfun->function_end_locus);
  571. tmp_rs.stmt = stmt;
  572. data->return_statements.safe_push (tmp_rs);
  573. /* Generate a goto statement and remove the return statement. */
  574. found:
  575. /* When not optimizing, make sure user returns are preserved. */
  576. if (!optimize && gimple_has_location (stmt))
  577. DECL_ARTIFICIAL (tmp_rs.label) = 0;
  578. t = gimple_build_goto (tmp_rs.label);
  579. gimple_set_location (t, gimple_location (stmt));
  580. gimple_set_block (t, gimple_block (stmt));
  581. gsi_insert_before (gsi, t, GSI_SAME_STMT);
  582. gsi_remove (gsi, false);
  583. }
  584. /* Lower a __builtin_setjmp GSI.
  585. __builtin_setjmp is passed a pointer to an array of five words (not
  586. all will be used on all machines). It operates similarly to the C
  587. library function of the same name, but is more efficient.
  588. It is lowered into 2 other builtins, namely __builtin_setjmp_setup,
  589. __builtin_setjmp_receiver.
  590. After full lowering, the body of the function should look like:
  591. {
  592. int D.1844;
  593. int D.2844;
  594. [...]
  595. __builtin_setjmp_setup (&buf, &<D1847>);
  596. D.1844 = 0;
  597. goto <D1846>;
  598. <D1847>:;
  599. __builtin_setjmp_receiver (&<D1847>);
  600. D.1844 = 1;
  601. <D1846>:;
  602. if (D.1844 == 0) goto <D1848>; else goto <D1849>;
  603. [...]
  604. __builtin_setjmp_setup (&buf, &<D2847>);
  605. D.2844 = 0;
  606. goto <D2846>;
  607. <D2847>:;
  608. __builtin_setjmp_receiver (&<D2847>);
  609. D.2844 = 1;
  610. <D2846>:;
  611. if (D.2844 == 0) goto <D2848>; else goto <D2849>;
  612. [...]
  613. <D3850>:;
  614. return;
  615. }
  616. During cfg creation an extra per-function (or per-OpenMP region)
  617. block with ABNORMAL_DISPATCHER internal call will be added, unique
  618. destination of all the abnormal call edges and the unique source of
  619. all the abnormal edges to the receivers, thus keeping the complexity
  620. explosion localized. */
  621. static void
  622. lower_builtin_setjmp (gimple_stmt_iterator *gsi)
  623. {
  624. gimple stmt = gsi_stmt (*gsi);
  625. location_t loc = gimple_location (stmt);
  626. tree cont_label = create_artificial_label (loc);
  627. tree next_label = create_artificial_label (loc);
  628. tree dest, t, arg;
  629. gimple g;
  630. /* __builtin_setjmp_{setup,receiver} aren't ECF_RETURNS_TWICE and for RTL
  631. these builtins are modelled as non-local label jumps to the label
  632. that is passed to these two builtins, so pretend we have a non-local
  633. label during GIMPLE passes too. See PR60003. */
  634. cfun->has_nonlocal_label = 1;
  635. /* NEXT_LABEL is the label __builtin_longjmp will jump to. Its address is
  636. passed to both __builtin_setjmp_setup and __builtin_setjmp_receiver. */
  637. FORCED_LABEL (next_label) = 1;
  638. dest = gimple_call_lhs (stmt);
  639. /* Build '__builtin_setjmp_setup (BUF, NEXT_LABEL)' and insert. */
  640. arg = build_addr (next_label, current_function_decl);
  641. t = builtin_decl_implicit (BUILT_IN_SETJMP_SETUP);
  642. g = gimple_build_call (t, 2, gimple_call_arg (stmt, 0), arg);
  643. gimple_set_location (g, loc);
  644. gimple_set_block (g, gimple_block (stmt));
  645. gsi_insert_before (gsi, g, GSI_SAME_STMT);
  646. /* Build 'DEST = 0' and insert. */
  647. if (dest)
  648. {
  649. g = gimple_build_assign (dest, build_zero_cst (TREE_TYPE (dest)));
  650. gimple_set_location (g, loc);
  651. gimple_set_block (g, gimple_block (stmt));
  652. gsi_insert_before (gsi, g, GSI_SAME_STMT);
  653. }
  654. /* Build 'goto CONT_LABEL' and insert. */
  655. g = gimple_build_goto (cont_label);
  656. gsi_insert_before (gsi, g, GSI_SAME_STMT);
  657. /* Build 'NEXT_LABEL:' and insert. */
  658. g = gimple_build_label (next_label);
  659. gsi_insert_before (gsi, g, GSI_SAME_STMT);
  660. /* Build '__builtin_setjmp_receiver (NEXT_LABEL)' and insert. */
  661. arg = build_addr (next_label, current_function_decl);
  662. t = builtin_decl_implicit (BUILT_IN_SETJMP_RECEIVER);
  663. g = gimple_build_call (t, 1, arg);
  664. gimple_set_location (g, loc);
  665. gimple_set_block (g, gimple_block (stmt));
  666. gsi_insert_before (gsi, g, GSI_SAME_STMT);
  667. /* Build 'DEST = 1' and insert. */
  668. if (dest)
  669. {
  670. g = gimple_build_assign (dest, fold_convert_loc (loc, TREE_TYPE (dest),
  671. integer_one_node));
  672. gimple_set_location (g, loc);
  673. gimple_set_block (g, gimple_block (stmt));
  674. gsi_insert_before (gsi, g, GSI_SAME_STMT);
  675. }
  676. /* Build 'CONT_LABEL:' and insert. */
  677. g = gimple_build_label (cont_label);
  678. gsi_insert_before (gsi, g, GSI_SAME_STMT);
  679. /* Remove the call to __builtin_setjmp. */
  680. gsi_remove (gsi, false);
  681. }
  682. /* Lower calls to posix_memalign to
  683. res = posix_memalign (ptr, align, size);
  684. if (res == 0)
  685. *ptr = __builtin_assume_aligned (*ptr, align);
  686. or to
  687. void *tem;
  688. res = posix_memalign (&tem, align, size);
  689. if (res == 0)
  690. ptr = __builtin_assume_aligned (tem, align);
  691. in case the first argument was &ptr. That way we can get at the
  692. alignment of the heap pointer in CCP. */
  693. static void
  694. lower_builtin_posix_memalign (gimple_stmt_iterator *gsi)
  695. {
  696. gimple stmt, call = gsi_stmt (*gsi);
  697. tree pptr = gimple_call_arg (call, 0);
  698. tree align = gimple_call_arg (call, 1);
  699. tree res = gimple_call_lhs (call);
  700. tree ptr = create_tmp_reg (ptr_type_node);
  701. if (TREE_CODE (pptr) == ADDR_EXPR)
  702. {
  703. tree tem = create_tmp_var (ptr_type_node);
  704. TREE_ADDRESSABLE (tem) = 1;
  705. gimple_call_set_arg (call, 0, build_fold_addr_expr (tem));
  706. stmt = gimple_build_assign (ptr, tem);
  707. }
  708. else
  709. stmt = gimple_build_assign (ptr,
  710. fold_build2 (MEM_REF, ptr_type_node, pptr,
  711. build_int_cst (ptr_type_node, 0)));
  712. if (res == NULL_TREE)
  713. {
  714. res = create_tmp_reg (integer_type_node);
  715. gimple_call_set_lhs (call, res);
  716. }
  717. tree align_label = create_artificial_label (UNKNOWN_LOCATION);
  718. tree noalign_label = create_artificial_label (UNKNOWN_LOCATION);
  719. gimple cond = gimple_build_cond (EQ_EXPR, res, integer_zero_node,
  720. align_label, noalign_label);
  721. gsi_insert_after (gsi, cond, GSI_NEW_STMT);
  722. gsi_insert_after (gsi, gimple_build_label (align_label), GSI_NEW_STMT);
  723. gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
  724. stmt = gimple_build_call (builtin_decl_implicit (BUILT_IN_ASSUME_ALIGNED),
  725. 2, ptr, align);
  726. gimple_call_set_lhs (stmt, ptr);
  727. gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
  728. stmt = gimple_build_assign (fold_build2 (MEM_REF, ptr_type_node, pptr,
  729. build_int_cst (ptr_type_node, 0)),
  730. ptr);
  731. gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
  732. gsi_insert_after (gsi, gimple_build_label (noalign_label), GSI_NEW_STMT);
  733. }
  734. /* Record the variables in VARS into function FN. */
  735. void
  736. record_vars_into (tree vars, tree fn)
  737. {
  738. for (; vars; vars = DECL_CHAIN (vars))
  739. {
  740. tree var = vars;
  741. /* BIND_EXPRs contains also function/type/constant declarations
  742. we don't need to care about. */
  743. if (TREE_CODE (var) != VAR_DECL)
  744. continue;
  745. /* Nothing to do in this case. */
  746. if (DECL_EXTERNAL (var))
  747. continue;
  748. /* Record the variable. */
  749. add_local_decl (DECL_STRUCT_FUNCTION (fn), var);
  750. }
  751. }
  752. /* Record the variables in VARS into current_function_decl. */
  753. void
  754. record_vars (tree vars)
  755. {
  756. record_vars_into (vars, current_function_decl);
  757. }