tree-object-size.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  1. /* __builtin_object_size (ptr, object_size_type) computation
  2. Copyright (C) 2004-2015 Free Software Foundation, Inc.
  3. Contributed by Jakub Jelinek <jakub@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 "hash-set.h"
  21. #include "machmode.h"
  22. #include "vec.h"
  23. #include "double-int.h"
  24. #include "input.h"
  25. #include "alias.h"
  26. #include "symtab.h"
  27. #include "wide-int.h"
  28. #include "inchash.h"
  29. #include "tree.h"
  30. #include "fold-const.h"
  31. #include "tree-object-size.h"
  32. #include "diagnostic-core.h"
  33. #include "gimple-pretty-print.h"
  34. #include "bitmap.h"
  35. #include "predict.h"
  36. #include "hard-reg-set.h"
  37. #include "input.h"
  38. #include "function.h"
  39. #include "dominance.h"
  40. #include "cfg.h"
  41. #include "basic-block.h"
  42. #include "tree-ssa-alias.h"
  43. #include "internal-fn.h"
  44. #include "gimple-fold.h"
  45. #include "gimple-expr.h"
  46. #include "is-a.h"
  47. #include "gimple.h"
  48. #include "gimple-iterator.h"
  49. #include "gimple-ssa.h"
  50. #include "stringpool.h"
  51. #include "tree-ssanames.h"
  52. #include "tree-pass.h"
  53. #include "tree-ssa-propagate.h"
  54. #include "tree-phinodes.h"
  55. #include "ssa-iterators.h"
  56. #include "builtins.h"
  57. struct object_size_info
  58. {
  59. int object_size_type;
  60. bitmap visited, reexamine;
  61. int pass;
  62. bool changed;
  63. unsigned int *depths;
  64. unsigned int *stack, *tos;
  65. };
  66. static const unsigned HOST_WIDE_INT unknown[4] = { -1, -1, 0, 0 };
  67. static tree compute_object_offset (const_tree, const_tree);
  68. static unsigned HOST_WIDE_INT addr_object_size (struct object_size_info *,
  69. const_tree, int);
  70. static unsigned HOST_WIDE_INT alloc_object_size (const gcall *, int);
  71. static tree pass_through_call (const gcall *);
  72. static void collect_object_sizes_for (struct object_size_info *, tree);
  73. static void expr_object_size (struct object_size_info *, tree, tree);
  74. static bool merge_object_sizes (struct object_size_info *, tree, tree,
  75. unsigned HOST_WIDE_INT);
  76. static bool plus_stmt_object_size (struct object_size_info *, tree, gimple);
  77. static bool cond_expr_object_size (struct object_size_info *, tree, gimple);
  78. static void init_offset_limit (void);
  79. static void check_for_plus_in_loops (struct object_size_info *, tree);
  80. static void check_for_plus_in_loops_1 (struct object_size_info *, tree,
  81. unsigned int);
  82. /* object_sizes[0] is upper bound for number of bytes till the end of
  83. the object.
  84. object_sizes[1] is upper bound for number of bytes till the end of
  85. the subobject (innermost array or field with address taken).
  86. object_sizes[2] is lower bound for number of bytes till the end of
  87. the object and object_sizes[3] lower bound for subobject. */
  88. static vec<unsigned HOST_WIDE_INT> object_sizes[4];
  89. /* Bitmaps what object sizes have been computed already. */
  90. static bitmap computed[4];
  91. /* Maximum value of offset we consider to be addition. */
  92. static unsigned HOST_WIDE_INT offset_limit;
  93. /* Initialize OFFSET_LIMIT variable. */
  94. static void
  95. init_offset_limit (void)
  96. {
  97. if (tree_fits_uhwi_p (TYPE_MAX_VALUE (sizetype)))
  98. offset_limit = tree_to_uhwi (TYPE_MAX_VALUE (sizetype));
  99. else
  100. offset_limit = -1;
  101. offset_limit /= 2;
  102. }
  103. /* Compute offset of EXPR within VAR. Return error_mark_node
  104. if unknown. */
  105. static tree
  106. compute_object_offset (const_tree expr, const_tree var)
  107. {
  108. enum tree_code code = PLUS_EXPR;
  109. tree base, off, t;
  110. if (expr == var)
  111. return size_zero_node;
  112. switch (TREE_CODE (expr))
  113. {
  114. case COMPONENT_REF:
  115. base = compute_object_offset (TREE_OPERAND (expr, 0), var);
  116. if (base == error_mark_node)
  117. return base;
  118. t = TREE_OPERAND (expr, 1);
  119. off = size_binop (PLUS_EXPR, DECL_FIELD_OFFSET (t),
  120. size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
  121. / BITS_PER_UNIT));
  122. break;
  123. case REALPART_EXPR:
  124. CASE_CONVERT:
  125. case VIEW_CONVERT_EXPR:
  126. case NON_LVALUE_EXPR:
  127. return compute_object_offset (TREE_OPERAND (expr, 0), var);
  128. case IMAGPART_EXPR:
  129. base = compute_object_offset (TREE_OPERAND (expr, 0), var);
  130. if (base == error_mark_node)
  131. return base;
  132. off = TYPE_SIZE_UNIT (TREE_TYPE (expr));
  133. break;
  134. case ARRAY_REF:
  135. base = compute_object_offset (TREE_OPERAND (expr, 0), var);
  136. if (base == error_mark_node)
  137. return base;
  138. t = TREE_OPERAND (expr, 1);
  139. if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) < 0)
  140. {
  141. code = MINUS_EXPR;
  142. t = fold_build1 (NEGATE_EXPR, TREE_TYPE (t), t);
  143. }
  144. t = fold_convert (sizetype, t);
  145. off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
  146. break;
  147. case MEM_REF:
  148. gcc_assert (TREE_CODE (TREE_OPERAND (expr, 0)) == ADDR_EXPR);
  149. return wide_int_to_tree (sizetype, mem_ref_offset (expr));
  150. default:
  151. return error_mark_node;
  152. }
  153. return size_binop (code, base, off);
  154. }
  155. /* Compute __builtin_object_size for PTR, which is a ADDR_EXPR.
  156. OBJECT_SIZE_TYPE is the second argument from __builtin_object_size.
  157. If unknown, return unknown[object_size_type]. */
  158. static unsigned HOST_WIDE_INT
  159. addr_object_size (struct object_size_info *osi, const_tree ptr,
  160. int object_size_type)
  161. {
  162. tree pt_var, pt_var_size = NULL_TREE, var_size, bytes;
  163. gcc_assert (TREE_CODE (ptr) == ADDR_EXPR);
  164. pt_var = TREE_OPERAND (ptr, 0);
  165. while (handled_component_p (pt_var))
  166. pt_var = TREE_OPERAND (pt_var, 0);
  167. if (pt_var
  168. && TREE_CODE (pt_var) == MEM_REF)
  169. {
  170. unsigned HOST_WIDE_INT sz;
  171. if (!osi || (object_size_type & 1) != 0
  172. || TREE_CODE (TREE_OPERAND (pt_var, 0)) != SSA_NAME)
  173. {
  174. sz = compute_builtin_object_size (TREE_OPERAND (pt_var, 0),
  175. object_size_type & ~1);
  176. }
  177. else
  178. {
  179. tree var = TREE_OPERAND (pt_var, 0);
  180. if (osi->pass == 0)
  181. collect_object_sizes_for (osi, var);
  182. if (bitmap_bit_p (computed[object_size_type],
  183. SSA_NAME_VERSION (var)))
  184. sz = object_sizes[object_size_type][SSA_NAME_VERSION (var)];
  185. else
  186. sz = unknown[object_size_type];
  187. }
  188. if (sz != unknown[object_size_type])
  189. {
  190. offset_int dsz = wi::sub (sz, mem_ref_offset (pt_var));
  191. if (wi::neg_p (dsz))
  192. sz = 0;
  193. else if (wi::fits_uhwi_p (dsz))
  194. sz = dsz.to_uhwi ();
  195. else
  196. sz = unknown[object_size_type];
  197. }
  198. if (sz != unknown[object_size_type] && sz < offset_limit)
  199. pt_var_size = size_int (sz);
  200. }
  201. else if (pt_var
  202. && DECL_P (pt_var)
  203. && tree_fits_uhwi_p (DECL_SIZE_UNIT (pt_var))
  204. && tree_to_uhwi (DECL_SIZE_UNIT (pt_var)) < offset_limit)
  205. pt_var_size = DECL_SIZE_UNIT (pt_var);
  206. else if (pt_var
  207. && TREE_CODE (pt_var) == STRING_CST
  208. && TYPE_SIZE_UNIT (TREE_TYPE (pt_var))
  209. && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (pt_var)))
  210. && tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (pt_var)))
  211. < offset_limit)
  212. pt_var_size = TYPE_SIZE_UNIT (TREE_TYPE (pt_var));
  213. else
  214. return unknown[object_size_type];
  215. if (pt_var != TREE_OPERAND (ptr, 0))
  216. {
  217. tree var;
  218. if (object_size_type & 1)
  219. {
  220. var = TREE_OPERAND (ptr, 0);
  221. while (var != pt_var
  222. && TREE_CODE (var) != BIT_FIELD_REF
  223. && TREE_CODE (var) != COMPONENT_REF
  224. && TREE_CODE (var) != ARRAY_REF
  225. && TREE_CODE (var) != ARRAY_RANGE_REF
  226. && TREE_CODE (var) != REALPART_EXPR
  227. && TREE_CODE (var) != IMAGPART_EXPR)
  228. var = TREE_OPERAND (var, 0);
  229. if (var != pt_var && TREE_CODE (var) == ARRAY_REF)
  230. var = TREE_OPERAND (var, 0);
  231. if (! TYPE_SIZE_UNIT (TREE_TYPE (var))
  232. || ! tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (var)))
  233. || (pt_var_size
  234. && tree_int_cst_lt (pt_var_size,
  235. TYPE_SIZE_UNIT (TREE_TYPE (var)))))
  236. var = pt_var;
  237. else if (var != pt_var && TREE_CODE (pt_var) == MEM_REF)
  238. {
  239. tree v = var;
  240. /* For &X->fld, compute object size only if fld isn't the last
  241. field, as struct { int i; char c[1]; } is often used instead
  242. of flexible array member. */
  243. while (v && v != pt_var)
  244. switch (TREE_CODE (v))
  245. {
  246. case ARRAY_REF:
  247. if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_OPERAND (v, 0)))
  248. && TREE_CODE (TREE_OPERAND (v, 1)) == INTEGER_CST)
  249. {
  250. tree domain
  251. = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (v, 0)));
  252. if (domain
  253. && TYPE_MAX_VALUE (domain)
  254. && TREE_CODE (TYPE_MAX_VALUE (domain))
  255. == INTEGER_CST
  256. && tree_int_cst_lt (TREE_OPERAND (v, 1),
  257. TYPE_MAX_VALUE (domain)))
  258. {
  259. v = NULL_TREE;
  260. break;
  261. }
  262. }
  263. v = TREE_OPERAND (v, 0);
  264. break;
  265. case REALPART_EXPR:
  266. case IMAGPART_EXPR:
  267. v = NULL_TREE;
  268. break;
  269. case COMPONENT_REF:
  270. if (TREE_CODE (TREE_TYPE (v)) != ARRAY_TYPE)
  271. {
  272. v = NULL_TREE;
  273. break;
  274. }
  275. while (v != pt_var && TREE_CODE (v) == COMPONENT_REF)
  276. if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
  277. != UNION_TYPE
  278. && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
  279. != QUAL_UNION_TYPE)
  280. break;
  281. else
  282. v = TREE_OPERAND (v, 0);
  283. if (TREE_CODE (v) == COMPONENT_REF
  284. && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
  285. == RECORD_TYPE)
  286. {
  287. tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
  288. for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
  289. if (TREE_CODE (fld_chain) == FIELD_DECL)
  290. break;
  291. if (fld_chain)
  292. {
  293. v = NULL_TREE;
  294. break;
  295. }
  296. v = TREE_OPERAND (v, 0);
  297. }
  298. while (v != pt_var && TREE_CODE (v) == COMPONENT_REF)
  299. if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
  300. != UNION_TYPE
  301. && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
  302. != QUAL_UNION_TYPE)
  303. break;
  304. else
  305. v = TREE_OPERAND (v, 0);
  306. if (v != pt_var)
  307. v = NULL_TREE;
  308. else
  309. v = pt_var;
  310. break;
  311. default:
  312. v = pt_var;
  313. break;
  314. }
  315. if (v == pt_var)
  316. var = pt_var;
  317. }
  318. }
  319. else
  320. var = pt_var;
  321. if (var != pt_var)
  322. var_size = TYPE_SIZE_UNIT (TREE_TYPE (var));
  323. else if (!pt_var_size)
  324. return unknown[object_size_type];
  325. else
  326. var_size = pt_var_size;
  327. bytes = compute_object_offset (TREE_OPERAND (ptr, 0), var);
  328. if (bytes != error_mark_node)
  329. {
  330. if (TREE_CODE (bytes) == INTEGER_CST
  331. && tree_int_cst_lt (var_size, bytes))
  332. bytes = size_zero_node;
  333. else
  334. bytes = size_binop (MINUS_EXPR, var_size, bytes);
  335. }
  336. if (var != pt_var
  337. && pt_var_size
  338. && TREE_CODE (pt_var) == MEM_REF
  339. && bytes != error_mark_node)
  340. {
  341. tree bytes2 = compute_object_offset (TREE_OPERAND (ptr, 0), pt_var);
  342. if (bytes2 != error_mark_node)
  343. {
  344. if (TREE_CODE (bytes2) == INTEGER_CST
  345. && tree_int_cst_lt (pt_var_size, bytes2))
  346. bytes2 = size_zero_node;
  347. else
  348. bytes2 = size_binop (MINUS_EXPR, pt_var_size, bytes2);
  349. bytes = size_binop (MIN_EXPR, bytes, bytes2);
  350. }
  351. }
  352. }
  353. else if (!pt_var_size)
  354. return unknown[object_size_type];
  355. else
  356. bytes = pt_var_size;
  357. if (tree_fits_uhwi_p (bytes))
  358. return tree_to_uhwi (bytes);
  359. return unknown[object_size_type];
  360. }
  361. /* Compute __builtin_object_size for CALL, which is a GIMPLE_CALL.
  362. Handles various allocation calls. OBJECT_SIZE_TYPE is the second
  363. argument from __builtin_object_size. If unknown, return
  364. unknown[object_size_type]. */
  365. static unsigned HOST_WIDE_INT
  366. alloc_object_size (const gcall *call, int object_size_type)
  367. {
  368. tree callee, bytes = NULL_TREE;
  369. tree alloc_size;
  370. int arg1 = -1, arg2 = -1;
  371. gcc_assert (is_gimple_call (call));
  372. callee = gimple_call_fndecl (call);
  373. if (!callee)
  374. return unknown[object_size_type];
  375. alloc_size = lookup_attribute ("alloc_size",
  376. TYPE_ATTRIBUTES (TREE_TYPE (callee)));
  377. if (alloc_size && TREE_VALUE (alloc_size))
  378. {
  379. tree p = TREE_VALUE (alloc_size);
  380. arg1 = TREE_INT_CST_LOW (TREE_VALUE (p))-1;
  381. if (TREE_CHAIN (p))
  382. arg2 = TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (p)))-1;
  383. }
  384. if (DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
  385. switch (DECL_FUNCTION_CODE (callee))
  386. {
  387. case BUILT_IN_CALLOC:
  388. arg2 = 1;
  389. /* fall through */
  390. case BUILT_IN_MALLOC:
  391. case BUILT_IN_ALLOCA:
  392. case BUILT_IN_ALLOCA_WITH_ALIGN:
  393. arg1 = 0;
  394. default:
  395. break;
  396. }
  397. if (arg1 < 0 || arg1 >= (int)gimple_call_num_args (call)
  398. || TREE_CODE (gimple_call_arg (call, arg1)) != INTEGER_CST
  399. || (arg2 >= 0
  400. && (arg2 >= (int)gimple_call_num_args (call)
  401. || TREE_CODE (gimple_call_arg (call, arg2)) != INTEGER_CST)))
  402. return unknown[object_size_type];
  403. if (arg2 >= 0)
  404. bytes = size_binop (MULT_EXPR,
  405. fold_convert (sizetype, gimple_call_arg (call, arg1)),
  406. fold_convert (sizetype, gimple_call_arg (call, arg2)));
  407. else if (arg1 >= 0)
  408. bytes = fold_convert (sizetype, gimple_call_arg (call, arg1));
  409. if (bytes && tree_fits_uhwi_p (bytes))
  410. return tree_to_uhwi (bytes);
  411. return unknown[object_size_type];
  412. }
  413. /* If object size is propagated from one of function's arguments directly
  414. to its return value, return that argument for GIMPLE_CALL statement CALL.
  415. Otherwise return NULL. */
  416. static tree
  417. pass_through_call (const gcall *call)
  418. {
  419. tree callee = gimple_call_fndecl (call);
  420. if (callee
  421. && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
  422. switch (DECL_FUNCTION_CODE (callee))
  423. {
  424. case BUILT_IN_MEMCPY:
  425. case BUILT_IN_MEMMOVE:
  426. case BUILT_IN_MEMSET:
  427. case BUILT_IN_STRCPY:
  428. case BUILT_IN_STRNCPY:
  429. case BUILT_IN_STRCAT:
  430. case BUILT_IN_STRNCAT:
  431. case BUILT_IN_MEMCPY_CHK:
  432. case BUILT_IN_MEMMOVE_CHK:
  433. case BUILT_IN_MEMSET_CHK:
  434. case BUILT_IN_STRCPY_CHK:
  435. case BUILT_IN_STRNCPY_CHK:
  436. case BUILT_IN_STPNCPY_CHK:
  437. case BUILT_IN_STRCAT_CHK:
  438. case BUILT_IN_STRNCAT_CHK:
  439. case BUILT_IN_ASSUME_ALIGNED:
  440. if (gimple_call_num_args (call) >= 1)
  441. return gimple_call_arg (call, 0);
  442. break;
  443. default:
  444. break;
  445. }
  446. return NULL_TREE;
  447. }
  448. /* Compute __builtin_object_size value for PTR. OBJECT_SIZE_TYPE is the
  449. second argument from __builtin_object_size. */
  450. unsigned HOST_WIDE_INT
  451. compute_builtin_object_size (tree ptr, int object_size_type)
  452. {
  453. gcc_assert (object_size_type >= 0 && object_size_type <= 3);
  454. if (! offset_limit)
  455. init_offset_limit ();
  456. if (TREE_CODE (ptr) == ADDR_EXPR)
  457. return addr_object_size (NULL, ptr, object_size_type);
  458. if (TREE_CODE (ptr) == SSA_NAME
  459. && POINTER_TYPE_P (TREE_TYPE (ptr))
  460. && computed[object_size_type] != NULL)
  461. {
  462. if (!bitmap_bit_p (computed[object_size_type], SSA_NAME_VERSION (ptr)))
  463. {
  464. struct object_size_info osi;
  465. bitmap_iterator bi;
  466. unsigned int i;
  467. if (num_ssa_names > object_sizes[object_size_type].length ())
  468. object_sizes[object_size_type].safe_grow (num_ssa_names);
  469. if (dump_file)
  470. {
  471. fprintf (dump_file, "Computing %s %sobject size for ",
  472. (object_size_type & 2) ? "minimum" : "maximum",
  473. (object_size_type & 1) ? "sub" : "");
  474. print_generic_expr (dump_file, ptr, dump_flags);
  475. fprintf (dump_file, ":\n");
  476. }
  477. osi.visited = BITMAP_ALLOC (NULL);
  478. osi.reexamine = BITMAP_ALLOC (NULL);
  479. osi.object_size_type = object_size_type;
  480. osi.depths = NULL;
  481. osi.stack = NULL;
  482. osi.tos = NULL;
  483. /* First pass: walk UD chains, compute object sizes that
  484. can be computed. osi.reexamine bitmap at the end will
  485. contain what variables were found in dependency cycles
  486. and therefore need to be reexamined. */
  487. osi.pass = 0;
  488. osi.changed = false;
  489. collect_object_sizes_for (&osi, ptr);
  490. /* Second pass: keep recomputing object sizes of variables
  491. that need reexamination, until no object sizes are
  492. increased or all object sizes are computed. */
  493. if (! bitmap_empty_p (osi.reexamine))
  494. {
  495. bitmap reexamine = BITMAP_ALLOC (NULL);
  496. /* If looking for minimum instead of maximum object size,
  497. detect cases where a pointer is increased in a loop.
  498. Although even without this detection pass 2 would eventually
  499. terminate, it could take a long time. If a pointer is
  500. increasing this way, we need to assume 0 object size.
  501. E.g. p = &buf[0]; while (cond) p = p + 4; */
  502. if (object_size_type & 2)
  503. {
  504. osi.depths = XCNEWVEC (unsigned int, num_ssa_names);
  505. osi.stack = XNEWVEC (unsigned int, num_ssa_names);
  506. osi.tos = osi.stack;
  507. osi.pass = 1;
  508. /* collect_object_sizes_for is changing
  509. osi.reexamine bitmap, so iterate over a copy. */
  510. bitmap_copy (reexamine, osi.reexamine);
  511. EXECUTE_IF_SET_IN_BITMAP (reexamine, 0, i, bi)
  512. if (bitmap_bit_p (osi.reexamine, i))
  513. check_for_plus_in_loops (&osi, ssa_name (i));
  514. free (osi.depths);
  515. osi.depths = NULL;
  516. free (osi.stack);
  517. osi.stack = NULL;
  518. osi.tos = NULL;
  519. }
  520. do
  521. {
  522. osi.pass = 2;
  523. osi.changed = false;
  524. /* collect_object_sizes_for is changing
  525. osi.reexamine bitmap, so iterate over a copy. */
  526. bitmap_copy (reexamine, osi.reexamine);
  527. EXECUTE_IF_SET_IN_BITMAP (reexamine, 0, i, bi)
  528. if (bitmap_bit_p (osi.reexamine, i))
  529. {
  530. collect_object_sizes_for (&osi, ssa_name (i));
  531. if (dump_file && (dump_flags & TDF_DETAILS))
  532. {
  533. fprintf (dump_file, "Reexamining ");
  534. print_generic_expr (dump_file, ssa_name (i),
  535. dump_flags);
  536. fprintf (dump_file, "\n");
  537. }
  538. }
  539. }
  540. while (osi.changed);
  541. BITMAP_FREE (reexamine);
  542. }
  543. EXECUTE_IF_SET_IN_BITMAP (osi.reexamine, 0, i, bi)
  544. bitmap_set_bit (computed[object_size_type], i);
  545. /* Debugging dumps. */
  546. if (dump_file)
  547. {
  548. EXECUTE_IF_SET_IN_BITMAP (osi.visited, 0, i, bi)
  549. if (object_sizes[object_size_type][i]
  550. != unknown[object_size_type])
  551. {
  552. print_generic_expr (dump_file, ssa_name (i),
  553. dump_flags);
  554. fprintf (dump_file,
  555. ": %s %sobject size "
  556. HOST_WIDE_INT_PRINT_UNSIGNED "\n",
  557. (object_size_type & 2) ? "minimum" : "maximum",
  558. (object_size_type & 1) ? "sub" : "",
  559. object_sizes[object_size_type][i]);
  560. }
  561. }
  562. BITMAP_FREE (osi.reexamine);
  563. BITMAP_FREE (osi.visited);
  564. }
  565. return object_sizes[object_size_type][SSA_NAME_VERSION (ptr)];
  566. }
  567. return unknown[object_size_type];
  568. }
  569. /* Compute object_sizes for PTR, defined to VALUE, which is not an SSA_NAME. */
  570. static void
  571. expr_object_size (struct object_size_info *osi, tree ptr, tree value)
  572. {
  573. int object_size_type = osi->object_size_type;
  574. unsigned int varno = SSA_NAME_VERSION (ptr);
  575. unsigned HOST_WIDE_INT bytes;
  576. gcc_assert (object_sizes[object_size_type][varno]
  577. != unknown[object_size_type]);
  578. gcc_assert (osi->pass == 0);
  579. if (TREE_CODE (value) == WITH_SIZE_EXPR)
  580. value = TREE_OPERAND (value, 0);
  581. /* Pointer variables should have been handled by merge_object_sizes. */
  582. gcc_assert (TREE_CODE (value) != SSA_NAME
  583. || !POINTER_TYPE_P (TREE_TYPE (value)));
  584. if (TREE_CODE (value) == ADDR_EXPR)
  585. bytes = addr_object_size (osi, value, object_size_type);
  586. else
  587. bytes = unknown[object_size_type];
  588. if ((object_size_type & 2) == 0)
  589. {
  590. if (object_sizes[object_size_type][varno] < bytes)
  591. object_sizes[object_size_type][varno] = bytes;
  592. }
  593. else
  594. {
  595. if (object_sizes[object_size_type][varno] > bytes)
  596. object_sizes[object_size_type][varno] = bytes;
  597. }
  598. }
  599. /* Compute object_sizes for PTR, defined to the result of a call. */
  600. static void
  601. call_object_size (struct object_size_info *osi, tree ptr, gcall *call)
  602. {
  603. int object_size_type = osi->object_size_type;
  604. unsigned int varno = SSA_NAME_VERSION (ptr);
  605. unsigned HOST_WIDE_INT bytes;
  606. gcc_assert (is_gimple_call (call));
  607. gcc_assert (object_sizes[object_size_type][varno]
  608. != unknown[object_size_type]);
  609. gcc_assert (osi->pass == 0);
  610. bytes = alloc_object_size (call, object_size_type);
  611. if ((object_size_type & 2) == 0)
  612. {
  613. if (object_sizes[object_size_type][varno] < bytes)
  614. object_sizes[object_size_type][varno] = bytes;
  615. }
  616. else
  617. {
  618. if (object_sizes[object_size_type][varno] > bytes)
  619. object_sizes[object_size_type][varno] = bytes;
  620. }
  621. }
  622. /* Compute object_sizes for PTR, defined to an unknown value. */
  623. static void
  624. unknown_object_size (struct object_size_info *osi, tree ptr)
  625. {
  626. int object_size_type = osi->object_size_type;
  627. unsigned int varno = SSA_NAME_VERSION (ptr);
  628. unsigned HOST_WIDE_INT bytes;
  629. gcc_assert (object_sizes[object_size_type][varno]
  630. != unknown[object_size_type]);
  631. gcc_assert (osi->pass == 0);
  632. bytes = unknown[object_size_type];
  633. if ((object_size_type & 2) == 0)
  634. {
  635. if (object_sizes[object_size_type][varno] < bytes)
  636. object_sizes[object_size_type][varno] = bytes;
  637. }
  638. else
  639. {
  640. if (object_sizes[object_size_type][varno] > bytes)
  641. object_sizes[object_size_type][varno] = bytes;
  642. }
  643. }
  644. /* Merge object sizes of ORIG + OFFSET into DEST. Return true if
  645. the object size might need reexamination later. */
  646. static bool
  647. merge_object_sizes (struct object_size_info *osi, tree dest, tree orig,
  648. unsigned HOST_WIDE_INT offset)
  649. {
  650. int object_size_type = osi->object_size_type;
  651. unsigned int varno = SSA_NAME_VERSION (dest);
  652. unsigned HOST_WIDE_INT orig_bytes;
  653. if (object_sizes[object_size_type][varno] == unknown[object_size_type])
  654. return false;
  655. if (offset >= offset_limit)
  656. {
  657. object_sizes[object_size_type][varno] = unknown[object_size_type];
  658. return false;
  659. }
  660. if (osi->pass == 0)
  661. collect_object_sizes_for (osi, orig);
  662. orig_bytes = object_sizes[object_size_type][SSA_NAME_VERSION (orig)];
  663. if (orig_bytes != unknown[object_size_type])
  664. orig_bytes = (offset > orig_bytes)
  665. ? (unsigned HOST_WIDE_INT) 0 : orig_bytes - offset;
  666. if ((object_size_type & 2) == 0)
  667. {
  668. if (object_sizes[object_size_type][varno] < orig_bytes)
  669. {
  670. object_sizes[object_size_type][varno] = orig_bytes;
  671. osi->changed = true;
  672. }
  673. }
  674. else
  675. {
  676. if (object_sizes[object_size_type][varno] > orig_bytes)
  677. {
  678. object_sizes[object_size_type][varno] = orig_bytes;
  679. osi->changed = true;
  680. }
  681. }
  682. return bitmap_bit_p (osi->reexamine, SSA_NAME_VERSION (orig));
  683. }
  684. /* Compute object_sizes for VAR, defined to the result of an assignment
  685. with operator POINTER_PLUS_EXPR. Return true if the object size might
  686. need reexamination later. */
  687. static bool
  688. plus_stmt_object_size (struct object_size_info *osi, tree var, gimple stmt)
  689. {
  690. int object_size_type = osi->object_size_type;
  691. unsigned int varno = SSA_NAME_VERSION (var);
  692. unsigned HOST_WIDE_INT bytes;
  693. tree op0, op1;
  694. if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
  695. {
  696. op0 = gimple_assign_rhs1 (stmt);
  697. op1 = gimple_assign_rhs2 (stmt);
  698. }
  699. else if (gimple_assign_rhs_code (stmt) == ADDR_EXPR)
  700. {
  701. tree rhs = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
  702. gcc_assert (TREE_CODE (rhs) == MEM_REF);
  703. op0 = TREE_OPERAND (rhs, 0);
  704. op1 = TREE_OPERAND (rhs, 1);
  705. }
  706. else
  707. gcc_unreachable ();
  708. if (object_sizes[object_size_type][varno] == unknown[object_size_type])
  709. return false;
  710. /* Handle PTR + OFFSET here. */
  711. if (TREE_CODE (op1) == INTEGER_CST
  712. && (TREE_CODE (op0) == SSA_NAME
  713. || TREE_CODE (op0) == ADDR_EXPR))
  714. {
  715. if (! tree_fits_uhwi_p (op1))
  716. bytes = unknown[object_size_type];
  717. else if (TREE_CODE (op0) == SSA_NAME)
  718. return merge_object_sizes (osi, var, op0, tree_to_uhwi (op1));
  719. else
  720. {
  721. unsigned HOST_WIDE_INT off = tree_to_uhwi (op1);
  722. /* op0 will be ADDR_EXPR here. */
  723. bytes = addr_object_size (osi, op0, object_size_type);
  724. if (bytes == unknown[object_size_type])
  725. ;
  726. else if (off > offset_limit)
  727. bytes = unknown[object_size_type];
  728. else if (off > bytes)
  729. bytes = 0;
  730. else
  731. bytes -= off;
  732. }
  733. }
  734. else
  735. bytes = unknown[object_size_type];
  736. if ((object_size_type & 2) == 0)
  737. {
  738. if (object_sizes[object_size_type][varno] < bytes)
  739. object_sizes[object_size_type][varno] = bytes;
  740. }
  741. else
  742. {
  743. if (object_sizes[object_size_type][varno] > bytes)
  744. object_sizes[object_size_type][varno] = bytes;
  745. }
  746. return false;
  747. }
  748. /* Compute object_sizes for VAR, defined at STMT, which is
  749. a COND_EXPR. Return true if the object size might need reexamination
  750. later. */
  751. static bool
  752. cond_expr_object_size (struct object_size_info *osi, tree var, gimple stmt)
  753. {
  754. tree then_, else_;
  755. int object_size_type = osi->object_size_type;
  756. unsigned int varno = SSA_NAME_VERSION (var);
  757. bool reexamine = false;
  758. gcc_assert (gimple_assign_rhs_code (stmt) == COND_EXPR);
  759. if (object_sizes[object_size_type][varno] == unknown[object_size_type])
  760. return false;
  761. then_ = gimple_assign_rhs2 (stmt);
  762. else_ = gimple_assign_rhs3 (stmt);
  763. if (TREE_CODE (then_) == SSA_NAME)
  764. reexamine |= merge_object_sizes (osi, var, then_, 0);
  765. else
  766. expr_object_size (osi, var, then_);
  767. if (TREE_CODE (else_) == SSA_NAME)
  768. reexamine |= merge_object_sizes (osi, var, else_, 0);
  769. else
  770. expr_object_size (osi, var, else_);
  771. return reexamine;
  772. }
  773. /* Compute object sizes for VAR.
  774. For ADDR_EXPR an object size is the number of remaining bytes
  775. to the end of the object (where what is considered an object depends on
  776. OSI->object_size_type).
  777. For allocation GIMPLE_CALL like malloc or calloc object size is the size
  778. of the allocation.
  779. For POINTER_PLUS_EXPR where second operand is a constant integer,
  780. object size is object size of the first operand minus the constant.
  781. If the constant is bigger than the number of remaining bytes until the
  782. end of the object, object size is 0, but if it is instead a pointer
  783. subtraction, object size is unknown[object_size_type].
  784. To differentiate addition from subtraction, ADDR_EXPR returns
  785. unknown[object_size_type] for all objects bigger than half of the address
  786. space, and constants less than half of the address space are considered
  787. addition, while bigger constants subtraction.
  788. For a memcpy like GIMPLE_CALL that always returns one of its arguments, the
  789. object size is object size of that argument.
  790. Otherwise, object size is the maximum of object sizes of variables
  791. that it might be set to. */
  792. static void
  793. collect_object_sizes_for (struct object_size_info *osi, tree var)
  794. {
  795. int object_size_type = osi->object_size_type;
  796. unsigned int varno = SSA_NAME_VERSION (var);
  797. gimple stmt;
  798. bool reexamine;
  799. if (bitmap_bit_p (computed[object_size_type], varno))
  800. return;
  801. if (osi->pass == 0)
  802. {
  803. if (bitmap_set_bit (osi->visited, varno))
  804. {
  805. object_sizes[object_size_type][varno]
  806. = (object_size_type & 2) ? -1 : 0;
  807. }
  808. else
  809. {
  810. /* Found a dependency loop. Mark the variable for later
  811. re-examination. */
  812. bitmap_set_bit (osi->reexamine, varno);
  813. if (dump_file && (dump_flags & TDF_DETAILS))
  814. {
  815. fprintf (dump_file, "Found a dependency loop at ");
  816. print_generic_expr (dump_file, var, dump_flags);
  817. fprintf (dump_file, "\n");
  818. }
  819. return;
  820. }
  821. }
  822. if (dump_file && (dump_flags & TDF_DETAILS))
  823. {
  824. fprintf (dump_file, "Visiting use-def links for ");
  825. print_generic_expr (dump_file, var, dump_flags);
  826. fprintf (dump_file, "\n");
  827. }
  828. stmt = SSA_NAME_DEF_STMT (var);
  829. reexamine = false;
  830. switch (gimple_code (stmt))
  831. {
  832. case GIMPLE_ASSIGN:
  833. {
  834. tree rhs = gimple_assign_rhs1 (stmt);
  835. if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
  836. || (gimple_assign_rhs_code (stmt) == ADDR_EXPR
  837. && TREE_CODE (TREE_OPERAND (rhs, 0)) == MEM_REF))
  838. reexamine = plus_stmt_object_size (osi, var, stmt);
  839. else if (gimple_assign_rhs_code (stmt) == COND_EXPR)
  840. reexamine = cond_expr_object_size (osi, var, stmt);
  841. else if (gimple_assign_single_p (stmt)
  842. || gimple_assign_unary_nop_p (stmt))
  843. {
  844. if (TREE_CODE (rhs) == SSA_NAME
  845. && POINTER_TYPE_P (TREE_TYPE (rhs)))
  846. reexamine = merge_object_sizes (osi, var, rhs, 0);
  847. else
  848. expr_object_size (osi, var, rhs);
  849. }
  850. else
  851. unknown_object_size (osi, var);
  852. break;
  853. }
  854. case GIMPLE_CALL:
  855. {
  856. gcall *call_stmt = as_a <gcall *> (stmt);
  857. tree arg = pass_through_call (call_stmt);
  858. if (arg)
  859. {
  860. if (TREE_CODE (arg) == SSA_NAME
  861. && POINTER_TYPE_P (TREE_TYPE (arg)))
  862. reexamine = merge_object_sizes (osi, var, arg, 0);
  863. else
  864. expr_object_size (osi, var, arg);
  865. }
  866. else
  867. call_object_size (osi, var, call_stmt);
  868. break;
  869. }
  870. case GIMPLE_ASM:
  871. /* Pointers defined by __asm__ statements can point anywhere. */
  872. object_sizes[object_size_type][varno] = unknown[object_size_type];
  873. break;
  874. case GIMPLE_NOP:
  875. if (SSA_NAME_VAR (var)
  876. && TREE_CODE (SSA_NAME_VAR (var)) == PARM_DECL)
  877. expr_object_size (osi, var, SSA_NAME_VAR (var));
  878. else
  879. /* Uninitialized SSA names point nowhere. */
  880. object_sizes[object_size_type][varno] = unknown[object_size_type];
  881. break;
  882. case GIMPLE_PHI:
  883. {
  884. unsigned i;
  885. for (i = 0; i < gimple_phi_num_args (stmt); i++)
  886. {
  887. tree rhs = gimple_phi_arg (stmt, i)->def;
  888. if (object_sizes[object_size_type][varno]
  889. == unknown[object_size_type])
  890. break;
  891. if (TREE_CODE (rhs) == SSA_NAME)
  892. reexamine |= merge_object_sizes (osi, var, rhs, 0);
  893. else if (osi->pass == 0)
  894. expr_object_size (osi, var, rhs);
  895. }
  896. break;
  897. }
  898. default:
  899. gcc_unreachable ();
  900. }
  901. if (! reexamine
  902. || object_sizes[object_size_type][varno] == unknown[object_size_type])
  903. {
  904. bitmap_set_bit (computed[object_size_type], varno);
  905. bitmap_clear_bit (osi->reexamine, varno);
  906. }
  907. else
  908. {
  909. bitmap_set_bit (osi->reexamine, varno);
  910. if (dump_file && (dump_flags & TDF_DETAILS))
  911. {
  912. fprintf (dump_file, "Need to reexamine ");
  913. print_generic_expr (dump_file, var, dump_flags);
  914. fprintf (dump_file, "\n");
  915. }
  916. }
  917. }
  918. /* Helper function for check_for_plus_in_loops. Called recursively
  919. to detect loops. */
  920. static void
  921. check_for_plus_in_loops_1 (struct object_size_info *osi, tree var,
  922. unsigned int depth)
  923. {
  924. gimple stmt = SSA_NAME_DEF_STMT (var);
  925. unsigned int varno = SSA_NAME_VERSION (var);
  926. if (osi->depths[varno])
  927. {
  928. if (osi->depths[varno] != depth)
  929. {
  930. unsigned int *sp;
  931. /* Found a loop involving pointer addition. */
  932. for (sp = osi->tos; sp > osi->stack; )
  933. {
  934. --sp;
  935. bitmap_clear_bit (osi->reexamine, *sp);
  936. bitmap_set_bit (computed[osi->object_size_type], *sp);
  937. object_sizes[osi->object_size_type][*sp] = 0;
  938. if (*sp == varno)
  939. break;
  940. }
  941. }
  942. return;
  943. }
  944. else if (! bitmap_bit_p (osi->reexamine, varno))
  945. return;
  946. osi->depths[varno] = depth;
  947. *osi->tos++ = varno;
  948. switch (gimple_code (stmt))
  949. {
  950. case GIMPLE_ASSIGN:
  951. {
  952. if ((gimple_assign_single_p (stmt)
  953. || gimple_assign_unary_nop_p (stmt))
  954. && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
  955. {
  956. tree rhs = gimple_assign_rhs1 (stmt);
  957. check_for_plus_in_loops_1 (osi, rhs, depth);
  958. }
  959. else if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
  960. {
  961. tree basevar = gimple_assign_rhs1 (stmt);
  962. tree cst = gimple_assign_rhs2 (stmt);
  963. gcc_assert (TREE_CODE (cst) == INTEGER_CST);
  964. check_for_plus_in_loops_1 (osi, basevar,
  965. depth + !integer_zerop (cst));
  966. }
  967. else
  968. gcc_unreachable ();
  969. break;
  970. }
  971. case GIMPLE_CALL:
  972. {
  973. gcall *call_stmt = as_a <gcall *> (stmt);
  974. tree arg = pass_through_call (call_stmt);
  975. if (arg)
  976. {
  977. if (TREE_CODE (arg) == SSA_NAME)
  978. check_for_plus_in_loops_1 (osi, arg, depth);
  979. else
  980. gcc_unreachable ();
  981. }
  982. break;
  983. }
  984. case GIMPLE_PHI:
  985. {
  986. unsigned i;
  987. for (i = 0; i < gimple_phi_num_args (stmt); i++)
  988. {
  989. tree rhs = gimple_phi_arg (stmt, i)->def;
  990. if (TREE_CODE (rhs) == SSA_NAME)
  991. check_for_plus_in_loops_1 (osi, rhs, depth);
  992. }
  993. break;
  994. }
  995. default:
  996. gcc_unreachable ();
  997. }
  998. osi->depths[varno] = 0;
  999. osi->tos--;
  1000. }
  1001. /* Check if some pointer we are computing object size of is being increased
  1002. within a loop. If yes, assume all the SSA variables participating in
  1003. that loop have minimum object sizes 0. */
  1004. static void
  1005. check_for_plus_in_loops (struct object_size_info *osi, tree var)
  1006. {
  1007. gimple stmt = SSA_NAME_DEF_STMT (var);
  1008. /* NOTE: In the pre-tuples code, we handled a CALL_EXPR here,
  1009. and looked for a POINTER_PLUS_EXPR in the pass-through
  1010. argument, if any. In GIMPLE, however, such an expression
  1011. is not a valid call operand. */
  1012. if (is_gimple_assign (stmt)
  1013. && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
  1014. {
  1015. tree basevar = gimple_assign_rhs1 (stmt);
  1016. tree cst = gimple_assign_rhs2 (stmt);
  1017. gcc_assert (TREE_CODE (cst) == INTEGER_CST);
  1018. if (integer_zerop (cst))
  1019. return;
  1020. osi->depths[SSA_NAME_VERSION (basevar)] = 1;
  1021. *osi->tos++ = SSA_NAME_VERSION (basevar);
  1022. check_for_plus_in_loops_1 (osi, var, 2);
  1023. osi->depths[SSA_NAME_VERSION (basevar)] = 0;
  1024. osi->tos--;
  1025. }
  1026. }
  1027. /* Initialize data structures for the object size computation. */
  1028. void
  1029. init_object_sizes (void)
  1030. {
  1031. int object_size_type;
  1032. if (computed[0])
  1033. return;
  1034. for (object_size_type = 0; object_size_type <= 3; object_size_type++)
  1035. {
  1036. object_sizes[object_size_type].safe_grow (num_ssa_names);
  1037. computed[object_size_type] = BITMAP_ALLOC (NULL);
  1038. }
  1039. init_offset_limit ();
  1040. }
  1041. /* Destroy data structures after the object size computation. */
  1042. static void
  1043. fini_object_sizes (void)
  1044. {
  1045. int object_size_type;
  1046. for (object_size_type = 0; object_size_type <= 3; object_size_type++)
  1047. {
  1048. object_sizes[object_size_type].release ();
  1049. BITMAP_FREE (computed[object_size_type]);
  1050. }
  1051. }
  1052. /* Simple pass to optimize all __builtin_object_size () builtins. */
  1053. namespace {
  1054. const pass_data pass_data_object_sizes =
  1055. {
  1056. GIMPLE_PASS, /* type */
  1057. "objsz", /* name */
  1058. OPTGROUP_NONE, /* optinfo_flags */
  1059. TV_NONE, /* tv_id */
  1060. ( PROP_cfg | PROP_ssa ), /* properties_required */
  1061. 0, /* properties_provided */
  1062. 0, /* properties_destroyed */
  1063. 0, /* todo_flags_start */
  1064. 0, /* todo_flags_finish */
  1065. };
  1066. class pass_object_sizes : public gimple_opt_pass
  1067. {
  1068. public:
  1069. pass_object_sizes (gcc::context *ctxt)
  1070. : gimple_opt_pass (pass_data_object_sizes, ctxt)
  1071. {}
  1072. /* opt_pass methods: */
  1073. opt_pass * clone () { return new pass_object_sizes (m_ctxt); }
  1074. virtual unsigned int execute (function *);
  1075. }; // class pass_object_sizes
  1076. unsigned int
  1077. pass_object_sizes::execute (function *fun)
  1078. {
  1079. basic_block bb;
  1080. FOR_EACH_BB_FN (bb, fun)
  1081. {
  1082. gimple_stmt_iterator i;
  1083. for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
  1084. {
  1085. tree result;
  1086. gimple call = gsi_stmt (i);
  1087. if (!gimple_call_builtin_p (call, BUILT_IN_OBJECT_SIZE))
  1088. continue;
  1089. init_object_sizes ();
  1090. /* In the first pass instance, only attempt to fold
  1091. __builtin_object_size (x, 1) and __builtin_object_size (x, 3),
  1092. and rather than folding the builtin to the constant if any,
  1093. create a MIN_EXPR or MAX_EXPR of the __builtin_object_size
  1094. call result and the computed constant. */
  1095. if (first_pass_instance)
  1096. {
  1097. tree ost = gimple_call_arg (call, 1);
  1098. if (tree_fits_uhwi_p (ost))
  1099. {
  1100. unsigned HOST_WIDE_INT object_size_type = tree_to_uhwi (ost);
  1101. tree ptr = gimple_call_arg (call, 0);
  1102. tree lhs = gimple_call_lhs (call);
  1103. if ((object_size_type == 1 || object_size_type == 3)
  1104. && (TREE_CODE (ptr) == ADDR_EXPR
  1105. || TREE_CODE (ptr) == SSA_NAME)
  1106. && lhs)
  1107. {
  1108. tree type = TREE_TYPE (lhs);
  1109. unsigned HOST_WIDE_INT bytes
  1110. = compute_builtin_object_size (ptr, object_size_type);
  1111. if (bytes != (unsigned HOST_WIDE_INT) (object_size_type == 1
  1112. ? -1 : 0)
  1113. && wi::fits_to_tree_p (bytes, type))
  1114. {
  1115. tree tem = make_ssa_name (type);
  1116. gimple_call_set_lhs (call, tem);
  1117. enum tree_code code
  1118. = object_size_type == 1 ? MIN_EXPR : MAX_EXPR;
  1119. tree cst = build_int_cstu (type, bytes);
  1120. gimple g = gimple_build_assign (lhs, code, tem, cst);
  1121. gsi_insert_after (&i, g, GSI_NEW_STMT);
  1122. update_stmt (call);
  1123. }
  1124. }
  1125. }
  1126. continue;
  1127. }
  1128. result = fold_call_stmt (as_a <gcall *> (call), false);
  1129. if (!result)
  1130. {
  1131. tree ost = gimple_call_arg (call, 1);
  1132. if (tree_fits_uhwi_p (ost))
  1133. {
  1134. unsigned HOST_WIDE_INT object_size_type = tree_to_uhwi (ost);
  1135. if (object_size_type < 2)
  1136. result = fold_convert (size_type_node,
  1137. integer_minus_one_node);
  1138. else if (object_size_type < 4)
  1139. result = build_zero_cst (size_type_node);
  1140. }
  1141. if (!result)
  1142. continue;
  1143. }
  1144. gcc_assert (TREE_CODE (result) == INTEGER_CST);
  1145. if (dump_file && (dump_flags & TDF_DETAILS))
  1146. {
  1147. fprintf (dump_file, "Simplified\n ");
  1148. print_gimple_stmt (dump_file, call, 0, dump_flags);
  1149. fprintf (dump_file, " to ");
  1150. print_generic_expr (dump_file, result, 0);
  1151. fprintf (dump_file, "\n");
  1152. }
  1153. tree lhs = gimple_call_lhs (call);
  1154. if (!lhs)
  1155. continue;
  1156. /* Propagate into all uses and fold those stmts. */
  1157. gimple use_stmt;
  1158. imm_use_iterator iter;
  1159. FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
  1160. {
  1161. use_operand_p use_p;
  1162. FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
  1163. SET_USE (use_p, result);
  1164. gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
  1165. fold_stmt (&gsi);
  1166. update_stmt (gsi_stmt (gsi));
  1167. }
  1168. }
  1169. }
  1170. fini_object_sizes ();
  1171. return 0;
  1172. }
  1173. } // anon namespace
  1174. gimple_opt_pass *
  1175. make_pass_object_sizes (gcc::context *ctxt)
  1176. {
  1177. return new pass_object_sizes (ctxt);
  1178. }