tree-dfa.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. /* Data flow functions for trees.
  2. Copyright (C) 2001-2015 Free Software Foundation, Inc.
  3. Contributed by Diego Novillo <dnovillo@redhat.com>
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GCC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "config.h"
  17. #include "system.h"
  18. #include "coretypes.h"
  19. #include "tm.h"
  20. #include "hashtab.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 "wide-int.h"
  29. #include "inchash.h"
  30. #include "tree.h"
  31. #include "fold-const.h"
  32. #include "stor-layout.h"
  33. #include "tm_p.h"
  34. #include "predict.h"
  35. #include "hard-reg-set.h"
  36. #include "function.h"
  37. #include "dominance.h"
  38. #include "cfg.h"
  39. #include "basic-block.h"
  40. #include "langhooks.h"
  41. #include "flags.h"
  42. #include "tree-pretty-print.h"
  43. #include "tree-ssa-alias.h"
  44. #include "internal-fn.h"
  45. #include "gimple-expr.h"
  46. #include "is-a.h"
  47. #include "gimple.h"
  48. #include "gimple-iterator.h"
  49. #include "gimple-walk.h"
  50. #include "gimple-ssa.h"
  51. #include "tree-phinodes.h"
  52. #include "ssa-iterators.h"
  53. #include "stringpool.h"
  54. #include "tree-ssanames.h"
  55. #include "rtl.h"
  56. #include "statistics.h"
  57. #include "real.h"
  58. #include "fixed-value.h"
  59. #include "insn-config.h"
  60. #include "expmed.h"
  61. #include "dojump.h"
  62. #include "explow.h"
  63. #include "calls.h"
  64. #include "emit-rtl.h"
  65. #include "varasm.h"
  66. #include "stmt.h"
  67. #include "expr.h"
  68. #include "tree-dfa.h"
  69. #include "tree-inline.h"
  70. #include "tree-pass.h"
  71. #include "params.h"
  72. /* Build and maintain data flow information for trees. */
  73. /* Counters used to display DFA and SSA statistics. */
  74. struct dfa_stats_d
  75. {
  76. long num_defs;
  77. long num_uses;
  78. long num_phis;
  79. long num_phi_args;
  80. size_t max_num_phi_args;
  81. long num_vdefs;
  82. long num_vuses;
  83. };
  84. /* Local functions. */
  85. static void collect_dfa_stats (struct dfa_stats_d *);
  86. /*---------------------------------------------------------------------------
  87. Dataflow analysis (DFA) routines
  88. ---------------------------------------------------------------------------*/
  89. /* Renumber all of the gimple stmt uids. */
  90. void
  91. renumber_gimple_stmt_uids (void)
  92. {
  93. basic_block bb;
  94. set_gimple_stmt_max_uid (cfun, 0);
  95. FOR_ALL_BB_FN (bb, cfun)
  96. {
  97. gimple_stmt_iterator bsi;
  98. for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
  99. {
  100. gimple stmt = gsi_stmt (bsi);
  101. gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
  102. }
  103. for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
  104. {
  105. gimple stmt = gsi_stmt (bsi);
  106. gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
  107. }
  108. }
  109. }
  110. /* Like renumber_gimple_stmt_uids, but only do work on the basic blocks
  111. in BLOCKS, of which there are N_BLOCKS. Also renumbers PHIs. */
  112. void
  113. renumber_gimple_stmt_uids_in_blocks (basic_block *blocks, int n_blocks)
  114. {
  115. int i;
  116. set_gimple_stmt_max_uid (cfun, 0);
  117. for (i = 0; i < n_blocks; i++)
  118. {
  119. basic_block bb = blocks[i];
  120. gimple_stmt_iterator bsi;
  121. for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
  122. {
  123. gimple stmt = gsi_stmt (bsi);
  124. gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
  125. }
  126. for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
  127. {
  128. gimple stmt = gsi_stmt (bsi);
  129. gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
  130. }
  131. }
  132. }
  133. /*---------------------------------------------------------------------------
  134. Debugging functions
  135. ---------------------------------------------------------------------------*/
  136. /* Dump variable VAR and its may-aliases to FILE. */
  137. void
  138. dump_variable (FILE *file, tree var)
  139. {
  140. if (TREE_CODE (var) == SSA_NAME)
  141. {
  142. if (POINTER_TYPE_P (TREE_TYPE (var)))
  143. dump_points_to_info_for (file, var);
  144. var = SSA_NAME_VAR (var);
  145. }
  146. if (var == NULL_TREE)
  147. {
  148. fprintf (file, "<nil>");
  149. return;
  150. }
  151. print_generic_expr (file, var, dump_flags);
  152. fprintf (file, ", UID D.%u", (unsigned) DECL_UID (var));
  153. if (DECL_PT_UID (var) != DECL_UID (var))
  154. fprintf (file, ", PT-UID D.%u", (unsigned) DECL_PT_UID (var));
  155. fprintf (file, ", ");
  156. print_generic_expr (file, TREE_TYPE (var), dump_flags);
  157. if (TREE_ADDRESSABLE (var))
  158. fprintf (file, ", is addressable");
  159. if (is_global_var (var))
  160. fprintf (file, ", is global");
  161. if (TREE_THIS_VOLATILE (var))
  162. fprintf (file, ", is volatile");
  163. if (cfun && ssa_default_def (cfun, var))
  164. {
  165. fprintf (file, ", default def: ");
  166. print_generic_expr (file, ssa_default_def (cfun, var), dump_flags);
  167. }
  168. if (DECL_INITIAL (var))
  169. {
  170. fprintf (file, ", initial: ");
  171. print_generic_expr (file, DECL_INITIAL (var), dump_flags);
  172. }
  173. fprintf (file, "\n");
  174. }
  175. /* Dump variable VAR and its may-aliases to stderr. */
  176. DEBUG_FUNCTION void
  177. debug_variable (tree var)
  178. {
  179. dump_variable (stderr, var);
  180. }
  181. /* Dump various DFA statistics to FILE. */
  182. void
  183. dump_dfa_stats (FILE *file)
  184. {
  185. struct dfa_stats_d dfa_stats;
  186. unsigned long size, total = 0;
  187. const char * const fmt_str = "%-30s%-13s%12s\n";
  188. const char * const fmt_str_1 = "%-30s%13lu%11lu%c\n";
  189. const char * const fmt_str_3 = "%-43s%11lu%c\n";
  190. const char *funcname
  191. = lang_hooks.decl_printable_name (current_function_decl, 2);
  192. collect_dfa_stats (&dfa_stats);
  193. fprintf (file, "\nDFA Statistics for %s\n\n", funcname);
  194. fprintf (file, "---------------------------------------------------------\n");
  195. fprintf (file, fmt_str, "", " Number of ", "Memory");
  196. fprintf (file, fmt_str, "", " instances ", "used ");
  197. fprintf (file, "---------------------------------------------------------\n");
  198. size = dfa_stats.num_uses * sizeof (tree *);
  199. total += size;
  200. fprintf (file, fmt_str_1, "USE operands", dfa_stats.num_uses,
  201. SCALE (size), LABEL (size));
  202. size = dfa_stats.num_defs * sizeof (tree *);
  203. total += size;
  204. fprintf (file, fmt_str_1, "DEF operands", dfa_stats.num_defs,
  205. SCALE (size), LABEL (size));
  206. size = dfa_stats.num_vuses * sizeof (tree *);
  207. total += size;
  208. fprintf (file, fmt_str_1, "VUSE operands", dfa_stats.num_vuses,
  209. SCALE (size), LABEL (size));
  210. size = dfa_stats.num_vdefs * sizeof (tree *);
  211. total += size;
  212. fprintf (file, fmt_str_1, "VDEF operands", dfa_stats.num_vdefs,
  213. SCALE (size), LABEL (size));
  214. size = dfa_stats.num_phis * sizeof (struct gphi);
  215. total += size;
  216. fprintf (file, fmt_str_1, "PHI nodes", dfa_stats.num_phis,
  217. SCALE (size), LABEL (size));
  218. size = dfa_stats.num_phi_args * sizeof (struct phi_arg_d);
  219. total += size;
  220. fprintf (file, fmt_str_1, "PHI arguments", dfa_stats.num_phi_args,
  221. SCALE (size), LABEL (size));
  222. fprintf (file, "---------------------------------------------------------\n");
  223. fprintf (file, fmt_str_3, "Total memory used by DFA/SSA data", SCALE (total),
  224. LABEL (total));
  225. fprintf (file, "---------------------------------------------------------\n");
  226. fprintf (file, "\n");
  227. if (dfa_stats.num_phis)
  228. fprintf (file, "Average number of arguments per PHI node: %.1f (max: %ld)\n",
  229. (float) dfa_stats.num_phi_args / (float) dfa_stats.num_phis,
  230. (long) dfa_stats.max_num_phi_args);
  231. fprintf (file, "\n");
  232. }
  233. /* Dump DFA statistics on stderr. */
  234. DEBUG_FUNCTION void
  235. debug_dfa_stats (void)
  236. {
  237. dump_dfa_stats (stderr);
  238. }
  239. /* Collect DFA statistics and store them in the structure pointed to by
  240. DFA_STATS_P. */
  241. static void
  242. collect_dfa_stats (struct dfa_stats_d *dfa_stats_p ATTRIBUTE_UNUSED)
  243. {
  244. basic_block bb;
  245. gcc_assert (dfa_stats_p);
  246. memset ((void *)dfa_stats_p, 0, sizeof (struct dfa_stats_d));
  247. /* Walk all the statements in the function counting references. */
  248. FOR_EACH_BB_FN (bb, cfun)
  249. {
  250. for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
  251. gsi_next (&si))
  252. {
  253. gphi *phi = si.phi ();
  254. dfa_stats_p->num_phis++;
  255. dfa_stats_p->num_phi_args += gimple_phi_num_args (phi);
  256. if (gimple_phi_num_args (phi) > dfa_stats_p->max_num_phi_args)
  257. dfa_stats_p->max_num_phi_args = gimple_phi_num_args (phi);
  258. }
  259. for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
  260. gsi_next (&si))
  261. {
  262. gimple stmt = gsi_stmt (si);
  263. dfa_stats_p->num_defs += NUM_SSA_OPERANDS (stmt, SSA_OP_DEF);
  264. dfa_stats_p->num_uses += NUM_SSA_OPERANDS (stmt, SSA_OP_USE);
  265. dfa_stats_p->num_vdefs += gimple_vdef (stmt) ? 1 : 0;
  266. dfa_stats_p->num_vuses += gimple_vuse (stmt) ? 1 : 0;
  267. }
  268. }
  269. }
  270. /*---------------------------------------------------------------------------
  271. Miscellaneous helpers
  272. ---------------------------------------------------------------------------*/
  273. /* Lookup VAR UID in the default_defs hashtable and return the associated
  274. variable. */
  275. tree
  276. ssa_default_def (struct function *fn, tree var)
  277. {
  278. struct tree_decl_minimal ind;
  279. struct tree_ssa_name in;
  280. gcc_assert (TREE_CODE (var) == VAR_DECL
  281. || TREE_CODE (var) == PARM_DECL
  282. || TREE_CODE (var) == RESULT_DECL);
  283. in.var = (tree)&ind;
  284. ind.uid = DECL_UID (var);
  285. return DEFAULT_DEFS (fn)->find_with_hash ((tree)&in, DECL_UID (var));
  286. }
  287. /* Insert the pair VAR's UID, DEF into the default_defs hashtable
  288. of function FN. */
  289. void
  290. set_ssa_default_def (struct function *fn, tree var, tree def)
  291. {
  292. struct tree_decl_minimal ind;
  293. struct tree_ssa_name in;
  294. gcc_assert (TREE_CODE (var) == VAR_DECL
  295. || TREE_CODE (var) == PARM_DECL
  296. || TREE_CODE (var) == RESULT_DECL);
  297. in.var = (tree)&ind;
  298. ind.uid = DECL_UID (var);
  299. if (!def)
  300. {
  301. tree *loc = DEFAULT_DEFS (fn)->find_slot_with_hash ((tree)&in,
  302. DECL_UID (var),
  303. NO_INSERT);
  304. if (loc)
  305. {
  306. SSA_NAME_IS_DEFAULT_DEF (*(tree *)loc) = false;
  307. DEFAULT_DEFS (fn)->clear_slot (loc);
  308. }
  309. return;
  310. }
  311. gcc_assert (TREE_CODE (def) == SSA_NAME && SSA_NAME_VAR (def) == var);
  312. tree *loc = DEFAULT_DEFS (fn)->find_slot_with_hash ((tree)&in,
  313. DECL_UID (var), INSERT);
  314. /* Default definition might be changed by tail call optimization. */
  315. if (*loc)
  316. SSA_NAME_IS_DEFAULT_DEF (*loc) = false;
  317. /* Mark DEF as the default definition for VAR. */
  318. *loc = def;
  319. SSA_NAME_IS_DEFAULT_DEF (def) = true;
  320. }
  321. /* Retrieve or create a default definition for VAR. */
  322. tree
  323. get_or_create_ssa_default_def (struct function *fn, tree var)
  324. {
  325. tree ddef = ssa_default_def (fn, var);
  326. if (ddef == NULL_TREE)
  327. {
  328. ddef = make_ssa_name_fn (fn, var, gimple_build_nop ());
  329. set_ssa_default_def (fn, var, ddef);
  330. }
  331. return ddef;
  332. }
  333. /* If EXP is a handled component reference for a structure, return the
  334. base variable. The access range is delimited by bit positions *POFFSET and
  335. *POFFSET + *PMAX_SIZE. The access size is *PSIZE bits. If either
  336. *PSIZE or *PMAX_SIZE is -1, they could not be determined. If *PSIZE
  337. and *PMAX_SIZE are equal, the access is non-variable. */
  338. tree
  339. get_ref_base_and_extent (tree exp, HOST_WIDE_INT *poffset,
  340. HOST_WIDE_INT *psize,
  341. HOST_WIDE_INT *pmax_size)
  342. {
  343. offset_int bitsize = -1;
  344. offset_int maxsize;
  345. tree size_tree = NULL_TREE;
  346. offset_int bit_offset = 0;
  347. bool seen_variable_array_ref = false;
  348. /* First get the final access size from just the outermost expression. */
  349. if (TREE_CODE (exp) == COMPONENT_REF)
  350. size_tree = DECL_SIZE (TREE_OPERAND (exp, 1));
  351. else if (TREE_CODE (exp) == BIT_FIELD_REF)
  352. size_tree = TREE_OPERAND (exp, 1);
  353. else if (!VOID_TYPE_P (TREE_TYPE (exp)))
  354. {
  355. machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
  356. if (mode == BLKmode)
  357. size_tree = TYPE_SIZE (TREE_TYPE (exp));
  358. else
  359. bitsize = int (GET_MODE_PRECISION (mode));
  360. }
  361. if (size_tree != NULL_TREE
  362. && TREE_CODE (size_tree) == INTEGER_CST)
  363. bitsize = wi::to_offset (size_tree);
  364. /* Initially, maxsize is the same as the accessed element size.
  365. In the following it will only grow (or become -1). */
  366. maxsize = bitsize;
  367. /* Compute cumulative bit-offset for nested component-refs and array-refs,
  368. and find the ultimate containing object. */
  369. while (1)
  370. {
  371. switch (TREE_CODE (exp))
  372. {
  373. case BIT_FIELD_REF:
  374. bit_offset += wi::to_offset (TREE_OPERAND (exp, 2));
  375. break;
  376. case COMPONENT_REF:
  377. {
  378. tree field = TREE_OPERAND (exp, 1);
  379. tree this_offset = component_ref_field_offset (exp);
  380. if (this_offset && TREE_CODE (this_offset) == INTEGER_CST)
  381. {
  382. offset_int woffset = wi::lshift (wi::to_offset (this_offset),
  383. LOG2_BITS_PER_UNIT);
  384. woffset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
  385. bit_offset += woffset;
  386. /* If we had seen a variable array ref already and we just
  387. referenced the last field of a struct or a union member
  388. then we have to adjust maxsize by the padding at the end
  389. of our field. */
  390. if (seen_variable_array_ref && maxsize != -1)
  391. {
  392. tree stype = TREE_TYPE (TREE_OPERAND (exp, 0));
  393. tree next = DECL_CHAIN (field);
  394. while (next && TREE_CODE (next) != FIELD_DECL)
  395. next = DECL_CHAIN (next);
  396. if (!next
  397. || TREE_CODE (stype) != RECORD_TYPE)
  398. {
  399. tree fsize = DECL_SIZE_UNIT (field);
  400. tree ssize = TYPE_SIZE_UNIT (stype);
  401. if (fsize == NULL
  402. || TREE_CODE (fsize) != INTEGER_CST
  403. || ssize == NULL
  404. || TREE_CODE (ssize) != INTEGER_CST)
  405. maxsize = -1;
  406. else
  407. {
  408. offset_int tem = (wi::to_offset (ssize)
  409. - wi::to_offset (fsize));
  410. tem = wi::lshift (tem, LOG2_BITS_PER_UNIT);
  411. tem -= woffset;
  412. maxsize += tem;
  413. }
  414. }
  415. }
  416. }
  417. else
  418. {
  419. tree csize = TYPE_SIZE (TREE_TYPE (TREE_OPERAND (exp, 0)));
  420. /* We need to adjust maxsize to the whole structure bitsize.
  421. But we can subtract any constant offset seen so far,
  422. because that would get us out of the structure otherwise. */
  423. if (maxsize != -1
  424. && csize
  425. && TREE_CODE (csize) == INTEGER_CST)
  426. maxsize = wi::to_offset (csize) - bit_offset;
  427. else
  428. maxsize = -1;
  429. }
  430. }
  431. break;
  432. case ARRAY_REF:
  433. case ARRAY_RANGE_REF:
  434. {
  435. tree index = TREE_OPERAND (exp, 1);
  436. tree low_bound, unit_size;
  437. /* If the resulting bit-offset is constant, track it. */
  438. if (TREE_CODE (index) == INTEGER_CST
  439. && (low_bound = array_ref_low_bound (exp),
  440. TREE_CODE (low_bound) == INTEGER_CST)
  441. && (unit_size = array_ref_element_size (exp),
  442. TREE_CODE (unit_size) == INTEGER_CST))
  443. {
  444. offset_int woffset
  445. = wi::sext (wi::to_offset (index) - wi::to_offset (low_bound),
  446. TYPE_PRECISION (TREE_TYPE (index)));
  447. woffset *= wi::to_offset (unit_size);
  448. woffset = wi::lshift (woffset, LOG2_BITS_PER_UNIT);
  449. bit_offset += woffset;
  450. /* An array ref with a constant index up in the structure
  451. hierarchy will constrain the size of any variable array ref
  452. lower in the access hierarchy. */
  453. seen_variable_array_ref = false;
  454. }
  455. else
  456. {
  457. tree asize = TYPE_SIZE (TREE_TYPE (TREE_OPERAND (exp, 0)));
  458. /* We need to adjust maxsize to the whole array bitsize.
  459. But we can subtract any constant offset seen so far,
  460. because that would get us outside of the array otherwise. */
  461. if (maxsize != -1
  462. && asize
  463. && TREE_CODE (asize) == INTEGER_CST)
  464. maxsize = wi::to_offset (asize) - bit_offset;
  465. else
  466. maxsize = -1;
  467. /* Remember that we have seen an array ref with a variable
  468. index. */
  469. seen_variable_array_ref = true;
  470. }
  471. }
  472. break;
  473. case REALPART_EXPR:
  474. break;
  475. case IMAGPART_EXPR:
  476. bit_offset += bitsize;
  477. break;
  478. case VIEW_CONVERT_EXPR:
  479. break;
  480. case TARGET_MEM_REF:
  481. /* Via the variable index or index2 we can reach the
  482. whole object. Still hand back the decl here. */
  483. if (TREE_CODE (TMR_BASE (exp)) == ADDR_EXPR
  484. && (TMR_INDEX (exp) || TMR_INDEX2 (exp)))
  485. {
  486. exp = TREE_OPERAND (TMR_BASE (exp), 0);
  487. bit_offset = 0;
  488. maxsize = -1;
  489. goto done;
  490. }
  491. /* Fallthru. */
  492. case MEM_REF:
  493. /* We need to deal with variable arrays ending structures such as
  494. struct { int length; int a[1]; } x; x.a[d]
  495. struct { struct { int a; int b; } a[1]; } x; x.a[d].a
  496. struct { struct { int a[1]; } a[1]; } x; x.a[0][d], x.a[d][0]
  497. struct { int len; union { int a[1]; struct X x; } u; } x; x.u.a[d]
  498. where we do not know maxsize for variable index accesses to
  499. the array. The simplest way to conservatively deal with this
  500. is to punt in the case that offset + maxsize reaches the
  501. base type boundary. This needs to include possible trailing
  502. padding that is there for alignment purposes. */
  503. if (seen_variable_array_ref
  504. && maxsize != -1
  505. && (TYPE_SIZE (TREE_TYPE (exp)) == NULL_TREE
  506. || TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
  507. || (bit_offset + maxsize
  508. == wi::to_offset (TYPE_SIZE (TREE_TYPE (exp))))))
  509. maxsize = -1;
  510. /* Hand back the decl for MEM[&decl, off]. */
  511. if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
  512. {
  513. if (integer_zerop (TREE_OPERAND (exp, 1)))
  514. exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
  515. else
  516. {
  517. offset_int off = mem_ref_offset (exp);
  518. off = wi::lshift (off, LOG2_BITS_PER_UNIT);
  519. off += bit_offset;
  520. if (wi::fits_shwi_p (off))
  521. {
  522. bit_offset = off;
  523. exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
  524. }
  525. }
  526. }
  527. goto done;
  528. default:
  529. goto done;
  530. }
  531. exp = TREE_OPERAND (exp, 0);
  532. }
  533. /* We need to deal with variable arrays ending structures. */
  534. if (seen_variable_array_ref
  535. && maxsize != -1
  536. && (TYPE_SIZE (TREE_TYPE (exp)) == NULL_TREE
  537. || TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
  538. || (bit_offset + maxsize
  539. == wi::to_offset (TYPE_SIZE (TREE_TYPE (exp))))))
  540. maxsize = -1;
  541. done:
  542. if (!wi::fits_shwi_p (bitsize) || wi::neg_p (bitsize))
  543. {
  544. *poffset = 0;
  545. *psize = -1;
  546. *pmax_size = -1;
  547. return exp;
  548. }
  549. *psize = bitsize.to_shwi ();
  550. if (!wi::fits_shwi_p (bit_offset))
  551. {
  552. *poffset = 0;
  553. *pmax_size = -1;
  554. return exp;
  555. }
  556. /* In case of a decl or constant base object we can do better. */
  557. if (DECL_P (exp))
  558. {
  559. /* If maxsize is unknown adjust it according to the size of the
  560. base decl. */
  561. if (maxsize == -1
  562. && DECL_SIZE (exp)
  563. && TREE_CODE (DECL_SIZE (exp)) == INTEGER_CST)
  564. maxsize = wi::to_offset (DECL_SIZE (exp)) - bit_offset;
  565. }
  566. else if (CONSTANT_CLASS_P (exp))
  567. {
  568. /* If maxsize is unknown adjust it according to the size of the
  569. base type constant. */
  570. if (maxsize == -1
  571. && TYPE_SIZE (TREE_TYPE (exp))
  572. && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST)
  573. maxsize = (wi::to_offset (TYPE_SIZE (TREE_TYPE (exp)))
  574. - bit_offset);
  575. }
  576. /* ??? Due to negative offsets in ARRAY_REF we can end up with
  577. negative bit_offset here. We might want to store a zero offset
  578. in this case. */
  579. *poffset = bit_offset.to_shwi ();
  580. if (!wi::fits_shwi_p (maxsize) || wi::neg_p (maxsize))
  581. *pmax_size = -1;
  582. else
  583. *pmax_size = maxsize.to_shwi ();
  584. return exp;
  585. }
  586. /* Returns the base object and a constant BITS_PER_UNIT offset in *POFFSET that
  587. denotes the starting address of the memory access EXP.
  588. Returns NULL_TREE if the offset is not constant or any component
  589. is not BITS_PER_UNIT-aligned.
  590. VALUEIZE if non-NULL is used to valueize SSA names. It should return
  591. its argument or a constant if the argument is known to be constant. */
  592. tree
  593. get_addr_base_and_unit_offset_1 (tree exp, HOST_WIDE_INT *poffset,
  594. tree (*valueize) (tree))
  595. {
  596. HOST_WIDE_INT byte_offset = 0;
  597. /* Compute cumulative byte-offset for nested component-refs and array-refs,
  598. and find the ultimate containing object. */
  599. while (1)
  600. {
  601. switch (TREE_CODE (exp))
  602. {
  603. case BIT_FIELD_REF:
  604. {
  605. HOST_WIDE_INT this_off = TREE_INT_CST_LOW (TREE_OPERAND (exp, 2));
  606. if (this_off % BITS_PER_UNIT)
  607. return NULL_TREE;
  608. byte_offset += this_off / BITS_PER_UNIT;
  609. }
  610. break;
  611. case COMPONENT_REF:
  612. {
  613. tree field = TREE_OPERAND (exp, 1);
  614. tree this_offset = component_ref_field_offset (exp);
  615. HOST_WIDE_INT hthis_offset;
  616. if (!this_offset
  617. || TREE_CODE (this_offset) != INTEGER_CST
  618. || (TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (field))
  619. % BITS_PER_UNIT))
  620. return NULL_TREE;
  621. hthis_offset = TREE_INT_CST_LOW (this_offset);
  622. hthis_offset += (TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (field))
  623. / BITS_PER_UNIT);
  624. byte_offset += hthis_offset;
  625. }
  626. break;
  627. case ARRAY_REF:
  628. case ARRAY_RANGE_REF:
  629. {
  630. tree index = TREE_OPERAND (exp, 1);
  631. tree low_bound, unit_size;
  632. if (valueize
  633. && TREE_CODE (index) == SSA_NAME)
  634. index = (*valueize) (index);
  635. /* If the resulting bit-offset is constant, track it. */
  636. if (TREE_CODE (index) == INTEGER_CST
  637. && (low_bound = array_ref_low_bound (exp),
  638. TREE_CODE (low_bound) == INTEGER_CST)
  639. && (unit_size = array_ref_element_size (exp),
  640. TREE_CODE (unit_size) == INTEGER_CST))
  641. {
  642. offset_int woffset
  643. = wi::sext (wi::to_offset (index) - wi::to_offset (low_bound),
  644. TYPE_PRECISION (TREE_TYPE (index)));
  645. woffset *= wi::to_offset (unit_size);
  646. byte_offset += woffset.to_shwi ();
  647. }
  648. else
  649. return NULL_TREE;
  650. }
  651. break;
  652. case REALPART_EXPR:
  653. break;
  654. case IMAGPART_EXPR:
  655. byte_offset += TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (exp)));
  656. break;
  657. case VIEW_CONVERT_EXPR:
  658. break;
  659. case MEM_REF:
  660. {
  661. tree base = TREE_OPERAND (exp, 0);
  662. if (valueize
  663. && TREE_CODE (base) == SSA_NAME)
  664. base = (*valueize) (base);
  665. /* Hand back the decl for MEM[&decl, off]. */
  666. if (TREE_CODE (base) == ADDR_EXPR)
  667. {
  668. if (!integer_zerop (TREE_OPERAND (exp, 1)))
  669. {
  670. offset_int off = mem_ref_offset (exp);
  671. byte_offset += off.to_short_addr ();
  672. }
  673. exp = TREE_OPERAND (base, 0);
  674. }
  675. goto done;
  676. }
  677. case TARGET_MEM_REF:
  678. {
  679. tree base = TREE_OPERAND (exp, 0);
  680. if (valueize
  681. && TREE_CODE (base) == SSA_NAME)
  682. base = (*valueize) (base);
  683. /* Hand back the decl for MEM[&decl, off]. */
  684. if (TREE_CODE (base) == ADDR_EXPR)
  685. {
  686. if (TMR_INDEX (exp) || TMR_INDEX2 (exp))
  687. return NULL_TREE;
  688. if (!integer_zerop (TMR_OFFSET (exp)))
  689. {
  690. offset_int off = mem_ref_offset (exp);
  691. byte_offset += off.to_short_addr ();
  692. }
  693. exp = TREE_OPERAND (base, 0);
  694. }
  695. goto done;
  696. }
  697. default:
  698. goto done;
  699. }
  700. exp = TREE_OPERAND (exp, 0);
  701. }
  702. done:
  703. *poffset = byte_offset;
  704. return exp;
  705. }
  706. /* Returns the base object and a constant BITS_PER_UNIT offset in *POFFSET that
  707. denotes the starting address of the memory access EXP.
  708. Returns NULL_TREE if the offset is not constant or any component
  709. is not BITS_PER_UNIT-aligned. */
  710. tree
  711. get_addr_base_and_unit_offset (tree exp, HOST_WIDE_INT *poffset)
  712. {
  713. return get_addr_base_and_unit_offset_1 (exp, poffset, NULL);
  714. }
  715. /* Returns true if STMT references an SSA_NAME that has
  716. SSA_NAME_OCCURS_IN_ABNORMAL_PHI set, otherwise false. */
  717. bool
  718. stmt_references_abnormal_ssa_name (gimple stmt)
  719. {
  720. ssa_op_iter oi;
  721. use_operand_p use_p;
  722. FOR_EACH_SSA_USE_OPERAND (use_p, stmt, oi, SSA_OP_USE)
  723. {
  724. if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (use_p)))
  725. return true;
  726. }
  727. return false;
  728. }
  729. /* Pair of tree and a sorting index, for dump_enumerated_decls. */
  730. struct GTY(()) numbered_tree_d
  731. {
  732. tree t;
  733. int num;
  734. };
  735. typedef struct numbered_tree_d numbered_tree;
  736. /* Compare two declarations references by their DECL_UID / sequence number.
  737. Called via qsort. */
  738. static int
  739. compare_decls_by_uid (const void *pa, const void *pb)
  740. {
  741. const numbered_tree *nt_a = ((const numbered_tree *)pa);
  742. const numbered_tree *nt_b = ((const numbered_tree *)pb);
  743. if (DECL_UID (nt_a->t) != DECL_UID (nt_b->t))
  744. return DECL_UID (nt_a->t) - DECL_UID (nt_b->t);
  745. return nt_a->num - nt_b->num;
  746. }
  747. /* Called via walk_gimple_stmt / walk_gimple_op by dump_enumerated_decls. */
  748. static tree
  749. dump_enumerated_decls_push (tree *tp, int *walk_subtrees, void *data)
  750. {
  751. struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
  752. vec<numbered_tree> *list = (vec<numbered_tree> *) wi->info;
  753. numbered_tree nt;
  754. if (!DECL_P (*tp))
  755. return NULL_TREE;
  756. nt.t = *tp;
  757. nt.num = list->length ();
  758. list->safe_push (nt);
  759. *walk_subtrees = 0;
  760. return NULL_TREE;
  761. }
  762. /* Find all the declarations used by the current function, sort them by uid,
  763. and emit the sorted list. Each declaration is tagged with a sequence
  764. number indicating when it was found during statement / tree walking,
  765. so that TDF_NOUID comparisons of anonymous declarations are still
  766. meaningful. Where a declaration was encountered more than once, we
  767. emit only the sequence number of the first encounter.
  768. FILE is the dump file where to output the list and FLAGS is as in
  769. print_generic_expr. */
  770. void
  771. dump_enumerated_decls (FILE *file, int flags)
  772. {
  773. basic_block bb;
  774. struct walk_stmt_info wi;
  775. auto_vec<numbered_tree, 40> decl_list;
  776. memset (&wi, '\0', sizeof (wi));
  777. wi.info = (void *) &decl_list;
  778. FOR_EACH_BB_FN (bb, cfun)
  779. {
  780. gimple_stmt_iterator gsi;
  781. for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  782. if (!is_gimple_debug (gsi_stmt (gsi)))
  783. walk_gimple_stmt (&gsi, NULL, dump_enumerated_decls_push, &wi);
  784. }
  785. decl_list.qsort (compare_decls_by_uid);
  786. if (decl_list.length ())
  787. {
  788. unsigned ix;
  789. numbered_tree *ntp;
  790. tree last = NULL_TREE;
  791. fprintf (file, "Declarations used by %s, sorted by DECL_UID:\n",
  792. current_function_name ());
  793. FOR_EACH_VEC_ELT (decl_list, ix, ntp)
  794. {
  795. if (ntp->t == last)
  796. continue;
  797. fprintf (file, "%d: ", ntp->num);
  798. print_generic_decl (file, ntp->t, flags);
  799. fprintf (file, "\n");
  800. last = ntp->t;
  801. }
  802. }
  803. }