tree-ssa-strlen.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434
  1. /* String length optimization
  2. Copyright (C) 2011-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 "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 "options.h"
  27. #include "wide-int.h"
  28. #include "inchash.h"
  29. #include "tree.h"
  30. #include "fold-const.h"
  31. #include "stor-layout.h"
  32. #include "hash-table.h"
  33. #include "hash-map.h"
  34. #include "bitmap.h"
  35. #include "predict.h"
  36. #include "tm.h"
  37. #include "hard-reg-set.h"
  38. #include "function.h"
  39. #include "dominance.h"
  40. #include "cfg.h"
  41. #include "basic-block.h"
  42. #include "tree-ssa-alias.h"
  43. #include "internal-fn.h"
  44. #include "gimple-fold.h"
  45. #include "tree-eh.h"
  46. #include "gimple-expr.h"
  47. #include "is-a.h"
  48. #include "gimple.h"
  49. #include "gimplify.h"
  50. #include "gimple-iterator.h"
  51. #include "gimplify-me.h"
  52. #include "gimple-ssa.h"
  53. #include "tree-phinodes.h"
  54. #include "ssa-iterators.h"
  55. #include "stringpool.h"
  56. #include "tree-ssanames.h"
  57. #include "hashtab.h"
  58. #include "rtl.h"
  59. #include "flags.h"
  60. #include "statistics.h"
  61. #include "real.h"
  62. #include "fixed-value.h"
  63. #include "insn-config.h"
  64. #include "expmed.h"
  65. #include "dojump.h"
  66. #include "explow.h"
  67. #include "calls.h"
  68. #include "emit-rtl.h"
  69. #include "varasm.h"
  70. #include "stmt.h"
  71. #include "expr.h"
  72. #include "tree-dfa.h"
  73. #include "tree-pass.h"
  74. #include "domwalk.h"
  75. #include "alloc-pool.h"
  76. #include "tree-ssa-propagate.h"
  77. #include "gimple-pretty-print.h"
  78. #include "params.h"
  79. #include "plugin-api.h"
  80. #include "ipa-ref.h"
  81. #include "cgraph.h"
  82. #include "ipa-chkp.h"
  83. /* A vector indexed by SSA_NAME_VERSION. 0 means unknown, positive value
  84. is an index into strinfo vector, negative value stands for
  85. string length of a string literal (~strlen). */
  86. static vec<int> ssa_ver_to_stridx;
  87. /* Number of currently active string indexes plus one. */
  88. static int max_stridx;
  89. /* String information record. */
  90. typedef struct strinfo_struct
  91. {
  92. /* String length of this string. */
  93. tree length;
  94. /* Any of the corresponding pointers for querying alias oracle. */
  95. tree ptr;
  96. /* Statement for delayed length computation. */
  97. gimple stmt;
  98. /* Pointer to '\0' if known, if NULL, it can be computed as
  99. ptr + length. */
  100. tree endptr;
  101. /* Reference count. Any changes to strinfo entry possibly shared
  102. with dominating basic blocks need unshare_strinfo first, except
  103. for dont_invalidate which affects only the immediately next
  104. maybe_invalidate. */
  105. int refcount;
  106. /* Copy of index. get_strinfo (si->idx) should return si; */
  107. int idx;
  108. /* These 3 fields are for chaining related string pointers together.
  109. E.g. for
  110. bl = strlen (b); dl = strlen (d); strcpy (a, b); c = a + bl;
  111. strcpy (c, d); e = c + dl;
  112. strinfo(a) -> strinfo(c) -> strinfo(e)
  113. All have ->first field equal to strinfo(a)->idx and are doubly
  114. chained through prev/next fields. The later strinfos are required
  115. to point into the same string with zero or more bytes after
  116. the previous pointer and all bytes in between the two pointers
  117. must be non-zero. Functions like strcpy or memcpy are supposed
  118. to adjust all previous strinfo lengths, but not following strinfo
  119. lengths (those are uncertain, usually invalidated during
  120. maybe_invalidate, except when the alias oracle knows better).
  121. Functions like strcat on the other side adjust the whole
  122. related strinfo chain.
  123. They are updated lazily, so to use the chain the same first fields
  124. and si->prev->next == si->idx needs to be verified. */
  125. int first;
  126. int next;
  127. int prev;
  128. /* A flag whether the string is known to be written in the current
  129. function. */
  130. bool writable;
  131. /* A flag for the next maybe_invalidate that this strinfo shouldn't
  132. be invalidated. Always cleared by maybe_invalidate. */
  133. bool dont_invalidate;
  134. } *strinfo;
  135. /* Pool for allocating strinfo_struct entries. */
  136. static alloc_pool strinfo_pool;
  137. /* Vector mapping positive string indexes to strinfo, for the
  138. current basic block. The first pointer in the vector is special,
  139. it is either NULL, meaning the vector isn't shared, or it is
  140. a basic block pointer to the owner basic_block if shared.
  141. If some other bb wants to modify the vector, the vector needs
  142. to be unshared first, and only the owner bb is supposed to free it. */
  143. static vec<strinfo, va_heap, vl_embed> *stridx_to_strinfo;
  144. /* One OFFSET->IDX mapping. */
  145. struct stridxlist
  146. {
  147. struct stridxlist *next;
  148. HOST_WIDE_INT offset;
  149. int idx;
  150. };
  151. /* Hash table entry, mapping a DECL to a chain of OFFSET->IDX mappings. */
  152. struct decl_stridxlist_map
  153. {
  154. struct tree_map_base base;
  155. struct stridxlist list;
  156. };
  157. /* stridxlist hashtable helpers. */
  158. struct stridxlist_hash_traits : default_hashmap_traits
  159. {
  160. static inline hashval_t hash (tree);
  161. };
  162. /* Hash a from tree in a decl_stridxlist_map. */
  163. inline hashval_t
  164. stridxlist_hash_traits::hash (tree item)
  165. {
  166. return DECL_UID (item);
  167. }
  168. /* Hash table for mapping decls to a chained list of offset -> idx
  169. mappings. */
  170. static hash_map<tree, stridxlist, stridxlist_hash_traits>
  171. *decl_to_stridxlist_htab;
  172. /* Obstack for struct stridxlist and struct decl_stridxlist_map. */
  173. static struct obstack stridx_obstack;
  174. /* Last memcpy statement if it could be adjusted if the trailing
  175. '\0' written is immediately overwritten, or
  176. *x = '\0' store that could be removed if it is immediately overwritten. */
  177. struct laststmt_struct
  178. {
  179. gimple stmt;
  180. tree len;
  181. int stridx;
  182. } laststmt;
  183. static int get_stridx_plus_constant (strinfo, HOST_WIDE_INT, tree);
  184. /* Return strinfo vector entry IDX. */
  185. static inline strinfo
  186. get_strinfo (int idx)
  187. {
  188. if (vec_safe_length (stridx_to_strinfo) <= (unsigned int) idx)
  189. return NULL;
  190. return (*stridx_to_strinfo)[idx];
  191. }
  192. /* Helper function for get_stridx. */
  193. static int
  194. get_addr_stridx (tree exp)
  195. {
  196. HOST_WIDE_INT off;
  197. struct stridxlist *list;
  198. tree base;
  199. if (!decl_to_stridxlist_htab)
  200. return 0;
  201. base = get_addr_base_and_unit_offset (exp, &off);
  202. if (base == NULL || !DECL_P (base))
  203. return 0;
  204. list = decl_to_stridxlist_htab->get (base);
  205. if (list == NULL)
  206. return 0;
  207. do
  208. {
  209. if (list->offset == off)
  210. return list->idx;
  211. list = list->next;
  212. }
  213. while (list);
  214. return 0;
  215. }
  216. /* Return string index for EXP. */
  217. static int
  218. get_stridx (tree exp)
  219. {
  220. tree s, o;
  221. if (TREE_CODE (exp) == SSA_NAME)
  222. {
  223. if (ssa_ver_to_stridx[SSA_NAME_VERSION (exp)])
  224. return ssa_ver_to_stridx[SSA_NAME_VERSION (exp)];
  225. int i;
  226. tree e = exp;
  227. HOST_WIDE_INT off = 0;
  228. for (i = 0; i < 5; i++)
  229. {
  230. gimple def_stmt = SSA_NAME_DEF_STMT (e);
  231. if (!is_gimple_assign (def_stmt)
  232. || gimple_assign_rhs_code (def_stmt) != POINTER_PLUS_EXPR)
  233. return 0;
  234. tree rhs1 = gimple_assign_rhs1 (def_stmt);
  235. tree rhs2 = gimple_assign_rhs2 (def_stmt);
  236. if (TREE_CODE (rhs1) != SSA_NAME
  237. || !tree_fits_shwi_p (rhs2))
  238. return 0;
  239. HOST_WIDE_INT this_off = tree_to_shwi (rhs2);
  240. if (this_off < 0)
  241. return 0;
  242. off = (unsigned HOST_WIDE_INT) off + this_off;
  243. if (off < 0)
  244. return 0;
  245. if (ssa_ver_to_stridx[SSA_NAME_VERSION (rhs1)])
  246. {
  247. strinfo si
  248. = get_strinfo (ssa_ver_to_stridx[SSA_NAME_VERSION (rhs1)]);
  249. if (si
  250. && si->length
  251. && TREE_CODE (si->length) == INTEGER_CST
  252. && compare_tree_int (si->length, off) != -1)
  253. return get_stridx_plus_constant (si, off, exp);
  254. }
  255. e = rhs1;
  256. }
  257. return 0;
  258. }
  259. if (TREE_CODE (exp) == ADDR_EXPR)
  260. {
  261. int idx = get_addr_stridx (TREE_OPERAND (exp, 0));
  262. if (idx != 0)
  263. return idx;
  264. }
  265. s = string_constant (exp, &o);
  266. if (s != NULL_TREE
  267. && (o == NULL_TREE || tree_fits_shwi_p (o))
  268. && TREE_STRING_LENGTH (s) > 0)
  269. {
  270. HOST_WIDE_INT offset = o ? tree_to_shwi (o) : 0;
  271. const char *p = TREE_STRING_POINTER (s);
  272. int max = TREE_STRING_LENGTH (s) - 1;
  273. if (p[max] == '\0' && offset >= 0 && offset <= max)
  274. return ~(int) strlen (p + offset);
  275. }
  276. return 0;
  277. }
  278. /* Return true if strinfo vector is shared with the immediate dominator. */
  279. static inline bool
  280. strinfo_shared (void)
  281. {
  282. return vec_safe_length (stridx_to_strinfo)
  283. && (*stridx_to_strinfo)[0] != NULL;
  284. }
  285. /* Unshare strinfo vector that is shared with the immediate dominator. */
  286. static void
  287. unshare_strinfo_vec (void)
  288. {
  289. strinfo si;
  290. unsigned int i = 0;
  291. gcc_assert (strinfo_shared ());
  292. stridx_to_strinfo = vec_safe_copy (stridx_to_strinfo);
  293. for (i = 1; vec_safe_iterate (stridx_to_strinfo, i, &si); ++i)
  294. if (si != NULL)
  295. si->refcount++;
  296. (*stridx_to_strinfo)[0] = NULL;
  297. }
  298. /* Attempt to create a string index for exp, ADDR_EXPR's operand.
  299. Return a pointer to the location where the string index can
  300. be stored (if 0) or is stored, or NULL if this can't be tracked. */
  301. static int *
  302. addr_stridxptr (tree exp)
  303. {
  304. HOST_WIDE_INT off;
  305. tree base = get_addr_base_and_unit_offset (exp, &off);
  306. if (base == NULL_TREE || !DECL_P (base))
  307. return NULL;
  308. if (!decl_to_stridxlist_htab)
  309. {
  310. decl_to_stridxlist_htab
  311. = new hash_map<tree, stridxlist, stridxlist_hash_traits> (64);
  312. gcc_obstack_init (&stridx_obstack);
  313. }
  314. bool existed;
  315. stridxlist *list = &decl_to_stridxlist_htab->get_or_insert (base, &existed);
  316. if (existed)
  317. {
  318. int i;
  319. for (i = 0; i < 16; i++)
  320. {
  321. if (list->offset == off)
  322. return &list->idx;
  323. if (list->next == NULL)
  324. break;
  325. }
  326. if (i == 16)
  327. return NULL;
  328. list->next = XOBNEW (&stridx_obstack, struct stridxlist);
  329. list = list->next;
  330. }
  331. list->next = NULL;
  332. list->offset = off;
  333. list->idx = 0;
  334. return &list->idx;
  335. }
  336. /* Create a new string index, or return 0 if reached limit. */
  337. static int
  338. new_stridx (tree exp)
  339. {
  340. int idx;
  341. if (max_stridx >= PARAM_VALUE (PARAM_MAX_TRACKED_STRLENS))
  342. return 0;
  343. if (TREE_CODE (exp) == SSA_NAME)
  344. {
  345. if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (exp))
  346. return 0;
  347. idx = max_stridx++;
  348. ssa_ver_to_stridx[SSA_NAME_VERSION (exp)] = idx;
  349. return idx;
  350. }
  351. if (TREE_CODE (exp) == ADDR_EXPR)
  352. {
  353. int *pidx = addr_stridxptr (TREE_OPERAND (exp, 0));
  354. if (pidx != NULL)
  355. {
  356. gcc_assert (*pidx == 0);
  357. *pidx = max_stridx++;
  358. return *pidx;
  359. }
  360. }
  361. return 0;
  362. }
  363. /* Like new_stridx, but for ADDR_EXPR's operand instead. */
  364. static int
  365. new_addr_stridx (tree exp)
  366. {
  367. int *pidx;
  368. if (max_stridx >= PARAM_VALUE (PARAM_MAX_TRACKED_STRLENS))
  369. return 0;
  370. pidx = addr_stridxptr (exp);
  371. if (pidx != NULL)
  372. {
  373. gcc_assert (*pidx == 0);
  374. *pidx = max_stridx++;
  375. return *pidx;
  376. }
  377. return 0;
  378. }
  379. /* Create a new strinfo. */
  380. static strinfo
  381. new_strinfo (tree ptr, int idx, tree length)
  382. {
  383. strinfo si = (strinfo) pool_alloc (strinfo_pool);
  384. si->length = length;
  385. si->ptr = ptr;
  386. si->stmt = NULL;
  387. si->endptr = NULL_TREE;
  388. si->refcount = 1;
  389. si->idx = idx;
  390. si->first = 0;
  391. si->prev = 0;
  392. si->next = 0;
  393. si->writable = false;
  394. si->dont_invalidate = false;
  395. return si;
  396. }
  397. /* Decrease strinfo refcount and free it if not referenced anymore. */
  398. static inline void
  399. free_strinfo (strinfo si)
  400. {
  401. if (si && --si->refcount == 0)
  402. pool_free (strinfo_pool, si);
  403. }
  404. /* Set strinfo in the vector entry IDX to SI. */
  405. static inline void
  406. set_strinfo (int idx, strinfo si)
  407. {
  408. if (vec_safe_length (stridx_to_strinfo) && (*stridx_to_strinfo)[0])
  409. unshare_strinfo_vec ();
  410. if (vec_safe_length (stridx_to_strinfo) <= (unsigned int) idx)
  411. vec_safe_grow_cleared (stridx_to_strinfo, idx + 1);
  412. (*stridx_to_strinfo)[idx] = si;
  413. }
  414. /* Return string length, or NULL if it can't be computed. */
  415. static tree
  416. get_string_length (strinfo si)
  417. {
  418. if (si->length)
  419. return si->length;
  420. if (si->stmt)
  421. {
  422. gimple stmt = si->stmt, lenstmt;
  423. bool with_bounds = gimple_call_with_bounds_p (stmt);
  424. tree callee, lhs, fn, tem;
  425. location_t loc;
  426. gimple_stmt_iterator gsi;
  427. gcc_assert (is_gimple_call (stmt));
  428. callee = gimple_call_fndecl (stmt);
  429. gcc_assert (callee && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL);
  430. lhs = gimple_call_lhs (stmt);
  431. /* unshare_strinfo is intentionally not called here. The (delayed)
  432. transformation of strcpy or strcat into stpcpy is done at the place
  433. of the former strcpy/strcat call and so can affect all the strinfos
  434. with the same stmt. If they were unshared before and transformation
  435. has been already done, the handling of BUILT_IN_STPCPY{,_CHK} should
  436. just compute the right length. */
  437. switch (DECL_FUNCTION_CODE (callee))
  438. {
  439. case BUILT_IN_STRCAT:
  440. case BUILT_IN_STRCAT_CHK:
  441. case BUILT_IN_STRCAT_CHKP:
  442. case BUILT_IN_STRCAT_CHK_CHKP:
  443. gsi = gsi_for_stmt (stmt);
  444. fn = builtin_decl_implicit (BUILT_IN_STRLEN);
  445. gcc_assert (lhs == NULL_TREE);
  446. tem = unshare_expr (gimple_call_arg (stmt, 0));
  447. if (with_bounds)
  448. {
  449. lenstmt = gimple_build_call (chkp_maybe_create_clone (fn)->decl,
  450. 2, tem, gimple_call_arg (stmt, 1));
  451. gimple_call_set_with_bounds (lenstmt, true);
  452. }
  453. else
  454. lenstmt = gimple_build_call (fn, 1, tem);
  455. lhs = make_ssa_name (TREE_TYPE (TREE_TYPE (fn)), lenstmt);
  456. gimple_call_set_lhs (lenstmt, lhs);
  457. gimple_set_vuse (lenstmt, gimple_vuse (stmt));
  458. gsi_insert_before (&gsi, lenstmt, GSI_SAME_STMT);
  459. tem = gimple_call_arg (stmt, 0);
  460. if (!ptrofftype_p (TREE_TYPE (lhs)))
  461. {
  462. lhs = convert_to_ptrofftype (lhs);
  463. lhs = force_gimple_operand_gsi (&gsi, lhs, true, NULL_TREE,
  464. true, GSI_SAME_STMT);
  465. }
  466. lenstmt = gimple_build_assign
  467. (make_ssa_name (TREE_TYPE (gimple_call_arg (stmt, 0))),
  468. POINTER_PLUS_EXPR,tem, lhs);
  469. gsi_insert_before (&gsi, lenstmt, GSI_SAME_STMT);
  470. gimple_call_set_arg (stmt, 0, gimple_assign_lhs (lenstmt));
  471. lhs = NULL_TREE;
  472. /* FALLTHRU */
  473. case BUILT_IN_STRCPY:
  474. case BUILT_IN_STRCPY_CHK:
  475. case BUILT_IN_STRCPY_CHKP:
  476. case BUILT_IN_STRCPY_CHK_CHKP:
  477. gcc_assert (builtin_decl_implicit_p (BUILT_IN_STPCPY));
  478. if (gimple_call_num_args (stmt) == (with_bounds ? 4 : 2))
  479. fn = builtin_decl_implicit (BUILT_IN_STPCPY);
  480. else
  481. fn = builtin_decl_explicit (BUILT_IN_STPCPY_CHK);
  482. if (with_bounds)
  483. fn = chkp_maybe_create_clone (fn)->decl;
  484. gcc_assert (lhs == NULL_TREE);
  485. if (dump_file && (dump_flags & TDF_DETAILS) != 0)
  486. {
  487. fprintf (dump_file, "Optimizing: ");
  488. print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
  489. }
  490. gimple_call_set_fndecl (stmt, fn);
  491. lhs = make_ssa_name (TREE_TYPE (TREE_TYPE (fn)), stmt);
  492. gimple_call_set_lhs (stmt, lhs);
  493. update_stmt (stmt);
  494. if (dump_file && (dump_flags & TDF_DETAILS) != 0)
  495. {
  496. fprintf (dump_file, "into: ");
  497. print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
  498. }
  499. /* FALLTHRU */
  500. case BUILT_IN_STPCPY:
  501. case BUILT_IN_STPCPY_CHK:
  502. case BUILT_IN_STPCPY_CHKP:
  503. case BUILT_IN_STPCPY_CHK_CHKP:
  504. gcc_assert (lhs != NULL_TREE);
  505. loc = gimple_location (stmt);
  506. si->endptr = lhs;
  507. si->stmt = NULL;
  508. lhs = fold_convert_loc (loc, size_type_node, lhs);
  509. si->length = fold_convert_loc (loc, size_type_node, si->ptr);
  510. si->length = fold_build2_loc (loc, MINUS_EXPR, size_type_node,
  511. lhs, si->length);
  512. break;
  513. case BUILT_IN_MALLOC:
  514. break;
  515. /* BUILT_IN_CALLOC always has si->length set. */
  516. default:
  517. gcc_unreachable ();
  518. break;
  519. }
  520. }
  521. return si->length;
  522. }
  523. /* Invalidate string length information for strings whose length
  524. might change due to stores in stmt. */
  525. static bool
  526. maybe_invalidate (gimple stmt)
  527. {
  528. strinfo si;
  529. unsigned int i;
  530. bool nonempty = false;
  531. for (i = 1; vec_safe_iterate (stridx_to_strinfo, i, &si); ++i)
  532. if (si != NULL)
  533. {
  534. if (!si->dont_invalidate)
  535. {
  536. ao_ref r;
  537. /* Do not use si->length. */
  538. ao_ref_init_from_ptr_and_size (&r, si->ptr, NULL_TREE);
  539. if (stmt_may_clobber_ref_p_1 (stmt, &r))
  540. {
  541. set_strinfo (i, NULL);
  542. free_strinfo (si);
  543. continue;
  544. }
  545. }
  546. si->dont_invalidate = false;
  547. nonempty = true;
  548. }
  549. return nonempty;
  550. }
  551. /* Unshare strinfo record SI, if it has refcount > 1 or
  552. if stridx_to_strinfo vector is shared with some other
  553. bbs. */
  554. static strinfo
  555. unshare_strinfo (strinfo si)
  556. {
  557. strinfo nsi;
  558. if (si->refcount == 1 && !strinfo_shared ())
  559. return si;
  560. nsi = new_strinfo (si->ptr, si->idx, si->length);
  561. nsi->stmt = si->stmt;
  562. nsi->endptr = si->endptr;
  563. nsi->first = si->first;
  564. nsi->prev = si->prev;
  565. nsi->next = si->next;
  566. nsi->writable = si->writable;
  567. set_strinfo (si->idx, nsi);
  568. free_strinfo (si);
  569. return nsi;
  570. }
  571. /* Return first strinfo in the related strinfo chain
  572. if all strinfos in between belong to the chain, otherwise
  573. NULL. */
  574. static strinfo
  575. verify_related_strinfos (strinfo origsi)
  576. {
  577. strinfo si = origsi, psi;
  578. if (origsi->first == 0)
  579. return NULL;
  580. for (; si->prev; si = psi)
  581. {
  582. if (si->first != origsi->first)
  583. return NULL;
  584. psi = get_strinfo (si->prev);
  585. if (psi == NULL)
  586. return NULL;
  587. if (psi->next != si->idx)
  588. return NULL;
  589. }
  590. if (si->idx != si->first)
  591. return NULL;
  592. return si;
  593. }
  594. /* Attempt to create a new strinfo for BASESI + OFF, or find existing
  595. strinfo if there is any. Return it's idx, or 0 if no strinfo has
  596. been created. */
  597. static int
  598. get_stridx_plus_constant (strinfo basesi, HOST_WIDE_INT off, tree ptr)
  599. {
  600. gcc_checking_assert (TREE_CODE (ptr) == SSA_NAME);
  601. if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ptr))
  602. return 0;
  603. if (basesi->length == NULL_TREE
  604. || TREE_CODE (basesi->length) != INTEGER_CST
  605. || compare_tree_int (basesi->length, off) == -1
  606. || !tree_fits_shwi_p (basesi->length))
  607. return 0;
  608. HOST_WIDE_INT len = tree_to_shwi (basesi->length) - off;
  609. strinfo si = basesi, chainsi;
  610. if (si->first || si->prev || si->next)
  611. si = verify_related_strinfos (basesi);
  612. if (si == NULL
  613. || si->length == NULL_TREE
  614. || TREE_CODE (si->length) != INTEGER_CST)
  615. return 0;
  616. if (ssa_ver_to_stridx.length () <= SSA_NAME_VERSION (ptr))
  617. ssa_ver_to_stridx.safe_grow_cleared (num_ssa_names);
  618. gcc_checking_assert (compare_tree_int (si->length, off) != -1);
  619. for (chainsi = si; chainsi->next; chainsi = si)
  620. {
  621. si = get_strinfo (chainsi->next);
  622. if (si == NULL
  623. || si->first != chainsi->first
  624. || si->prev != chainsi->idx
  625. || si->length == NULL_TREE
  626. || TREE_CODE (si->length) != INTEGER_CST)
  627. break;
  628. int r = compare_tree_int (si->length, len);
  629. if (r != 1)
  630. {
  631. if (r == 0)
  632. {
  633. ssa_ver_to_stridx[SSA_NAME_VERSION (ptr)] = si->idx;
  634. return si->idx;
  635. }
  636. break;
  637. }
  638. }
  639. int idx = new_stridx (ptr);
  640. if (idx == 0)
  641. return 0;
  642. si = new_strinfo (ptr, idx, build_int_cst (size_type_node, len));
  643. set_strinfo (idx, si);
  644. if (chainsi->next)
  645. {
  646. strinfo nextsi = unshare_strinfo (get_strinfo (chainsi->next));
  647. si->next = nextsi->idx;
  648. nextsi->prev = idx;
  649. }
  650. chainsi = unshare_strinfo (chainsi);
  651. if (chainsi->first == 0)
  652. chainsi->first = chainsi->idx;
  653. chainsi->next = idx;
  654. if (chainsi->endptr == NULL_TREE && len == 0)
  655. chainsi->endptr = ptr;
  656. si->endptr = chainsi->endptr;
  657. si->prev = chainsi->idx;
  658. si->first = chainsi->first;
  659. si->writable = chainsi->writable;
  660. return si->idx;
  661. }
  662. /* Note that PTR, a pointer SSA_NAME initialized in the current stmt, points
  663. to a zero-length string and if possible chain it to a related strinfo
  664. chain whose part is or might be CHAINSI. */
  665. static strinfo
  666. zero_length_string (tree ptr, strinfo chainsi)
  667. {
  668. strinfo si;
  669. int idx;
  670. if (ssa_ver_to_stridx.length () <= SSA_NAME_VERSION (ptr))
  671. ssa_ver_to_stridx.safe_grow_cleared (num_ssa_names);
  672. gcc_checking_assert (TREE_CODE (ptr) == SSA_NAME
  673. && ssa_ver_to_stridx[SSA_NAME_VERSION (ptr)] == 0);
  674. if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ptr))
  675. return NULL;
  676. if (chainsi != NULL)
  677. {
  678. si = verify_related_strinfos (chainsi);
  679. if (si)
  680. {
  681. chainsi = si;
  682. for (; chainsi->next; chainsi = si)
  683. {
  684. if (chainsi->endptr == NULL_TREE)
  685. {
  686. chainsi = unshare_strinfo (chainsi);
  687. chainsi->endptr = ptr;
  688. }
  689. si = get_strinfo (chainsi->next);
  690. if (si == NULL
  691. || si->first != chainsi->first
  692. || si->prev != chainsi->idx)
  693. break;
  694. }
  695. gcc_assert (chainsi->length || chainsi->stmt);
  696. if (chainsi->endptr == NULL_TREE)
  697. {
  698. chainsi = unshare_strinfo (chainsi);
  699. chainsi->endptr = ptr;
  700. }
  701. if (chainsi->length && integer_zerop (chainsi->length))
  702. {
  703. if (chainsi->next)
  704. {
  705. chainsi = unshare_strinfo (chainsi);
  706. chainsi->next = 0;
  707. }
  708. ssa_ver_to_stridx[SSA_NAME_VERSION (ptr)] = chainsi->idx;
  709. return chainsi;
  710. }
  711. }
  712. else if (chainsi->first || chainsi->prev || chainsi->next)
  713. {
  714. chainsi = unshare_strinfo (chainsi);
  715. chainsi->first = 0;
  716. chainsi->prev = 0;
  717. chainsi->next = 0;
  718. }
  719. }
  720. idx = new_stridx (ptr);
  721. if (idx == 0)
  722. return NULL;
  723. si = new_strinfo (ptr, idx, build_int_cst (size_type_node, 0));
  724. set_strinfo (idx, si);
  725. si->endptr = ptr;
  726. if (chainsi != NULL)
  727. {
  728. chainsi = unshare_strinfo (chainsi);
  729. if (chainsi->first == 0)
  730. chainsi->first = chainsi->idx;
  731. chainsi->next = idx;
  732. if (chainsi->endptr == NULL_TREE)
  733. chainsi->endptr = ptr;
  734. si->prev = chainsi->idx;
  735. si->first = chainsi->first;
  736. si->writable = chainsi->writable;
  737. }
  738. return si;
  739. }
  740. /* For strinfo ORIGSI whose length has been just updated
  741. update also related strinfo lengths (add ADJ to each,
  742. but don't adjust ORIGSI). */
  743. static void
  744. adjust_related_strinfos (location_t loc, strinfo origsi, tree adj)
  745. {
  746. strinfo si = verify_related_strinfos (origsi);
  747. if (si == NULL)
  748. return;
  749. while (1)
  750. {
  751. strinfo nsi;
  752. if (si != origsi)
  753. {
  754. tree tem;
  755. si = unshare_strinfo (si);
  756. if (si->length)
  757. {
  758. tem = fold_convert_loc (loc, TREE_TYPE (si->length), adj);
  759. si->length = fold_build2_loc (loc, PLUS_EXPR,
  760. TREE_TYPE (si->length), si->length,
  761. tem);
  762. }
  763. else if (si->stmt != NULL)
  764. /* Delayed length computation is unaffected. */
  765. ;
  766. else
  767. gcc_unreachable ();
  768. si->endptr = NULL_TREE;
  769. si->dont_invalidate = true;
  770. }
  771. if (si->next == 0)
  772. return;
  773. nsi = get_strinfo (si->next);
  774. if (nsi == NULL
  775. || nsi->first != si->first
  776. || nsi->prev != si->idx)
  777. return;
  778. si = nsi;
  779. }
  780. }
  781. /* Find if there are other SSA_NAME pointers equal to PTR
  782. for which we don't track their string lengths yet. If so, use
  783. IDX for them. */
  784. static void
  785. find_equal_ptrs (tree ptr, int idx)
  786. {
  787. if (TREE_CODE (ptr) != SSA_NAME)
  788. return;
  789. while (1)
  790. {
  791. gimple stmt = SSA_NAME_DEF_STMT (ptr);
  792. if (!is_gimple_assign (stmt))
  793. return;
  794. ptr = gimple_assign_rhs1 (stmt);
  795. switch (gimple_assign_rhs_code (stmt))
  796. {
  797. case SSA_NAME:
  798. break;
  799. CASE_CONVERT:
  800. if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
  801. return;
  802. if (TREE_CODE (ptr) == SSA_NAME)
  803. break;
  804. if (TREE_CODE (ptr) != ADDR_EXPR)
  805. return;
  806. /* FALLTHRU */
  807. case ADDR_EXPR:
  808. {
  809. int *pidx = addr_stridxptr (TREE_OPERAND (ptr, 0));
  810. if (pidx != NULL && *pidx == 0)
  811. *pidx = idx;
  812. return;
  813. }
  814. default:
  815. return;
  816. }
  817. /* We might find an endptr created in this pass. Grow the
  818. vector in that case. */
  819. if (ssa_ver_to_stridx.length () <= SSA_NAME_VERSION (ptr))
  820. ssa_ver_to_stridx.safe_grow_cleared (num_ssa_names);
  821. if (ssa_ver_to_stridx[SSA_NAME_VERSION (ptr)] != 0)
  822. return;
  823. ssa_ver_to_stridx[SSA_NAME_VERSION (ptr)] = idx;
  824. }
  825. }
  826. /* If the last .MEM setter statement before STMT is
  827. memcpy (x, y, strlen (y) + 1), the only .MEM use of it is STMT
  828. and STMT is known to overwrite x[strlen (x)], adjust the last memcpy to
  829. just memcpy (x, y, strlen (y)). SI must be the zero length
  830. strinfo. */
  831. static void
  832. adjust_last_stmt (strinfo si, gimple stmt, bool is_strcat)
  833. {
  834. tree vuse, callee, len;
  835. struct laststmt_struct last = laststmt;
  836. strinfo lastsi, firstsi;
  837. unsigned len_arg_no = 2;
  838. laststmt.stmt = NULL;
  839. laststmt.len = NULL_TREE;
  840. laststmt.stridx = 0;
  841. if (last.stmt == NULL)
  842. return;
  843. vuse = gimple_vuse (stmt);
  844. if (vuse == NULL_TREE
  845. || SSA_NAME_DEF_STMT (vuse) != last.stmt
  846. || !has_single_use (vuse))
  847. return;
  848. gcc_assert (last.stridx > 0);
  849. lastsi = get_strinfo (last.stridx);
  850. if (lastsi == NULL)
  851. return;
  852. if (lastsi != si)
  853. {
  854. if (lastsi->first == 0 || lastsi->first != si->first)
  855. return;
  856. firstsi = verify_related_strinfos (si);
  857. if (firstsi == NULL)
  858. return;
  859. while (firstsi != lastsi)
  860. {
  861. strinfo nextsi;
  862. if (firstsi->next == 0)
  863. return;
  864. nextsi = get_strinfo (firstsi->next);
  865. if (nextsi == NULL
  866. || nextsi->prev != firstsi->idx
  867. || nextsi->first != si->first)
  868. return;
  869. firstsi = nextsi;
  870. }
  871. }
  872. if (!is_strcat)
  873. {
  874. if (si->length == NULL_TREE || !integer_zerop (si->length))
  875. return;
  876. }
  877. if (is_gimple_assign (last.stmt))
  878. {
  879. gimple_stmt_iterator gsi;
  880. if (!integer_zerop (gimple_assign_rhs1 (last.stmt)))
  881. return;
  882. if (stmt_could_throw_p (last.stmt))
  883. return;
  884. gsi = gsi_for_stmt (last.stmt);
  885. unlink_stmt_vdef (last.stmt);
  886. release_defs (last.stmt);
  887. gsi_remove (&gsi, true);
  888. return;
  889. }
  890. if (!gimple_call_builtin_p (last.stmt, BUILT_IN_NORMAL))
  891. return;
  892. callee = gimple_call_fndecl (last.stmt);
  893. switch (DECL_FUNCTION_CODE (callee))
  894. {
  895. case BUILT_IN_MEMCPY:
  896. case BUILT_IN_MEMCPY_CHK:
  897. break;
  898. case BUILT_IN_MEMCPY_CHKP:
  899. case BUILT_IN_MEMCPY_CHK_CHKP:
  900. len_arg_no = 4;
  901. break;
  902. default:
  903. return;
  904. }
  905. len = gimple_call_arg (last.stmt, len_arg_no);
  906. if (tree_fits_uhwi_p (len))
  907. {
  908. if (!tree_fits_uhwi_p (last.len)
  909. || integer_zerop (len)
  910. || tree_to_uhwi (len) != tree_to_uhwi (last.len) + 1)
  911. return;
  912. /* Don't adjust the length if it is divisible by 4, it is more efficient
  913. to store the extra '\0' in that case. */
  914. if ((tree_to_uhwi (len) & 3) == 0)
  915. return;
  916. }
  917. else if (TREE_CODE (len) == SSA_NAME)
  918. {
  919. gimple def_stmt = SSA_NAME_DEF_STMT (len);
  920. if (!is_gimple_assign (def_stmt)
  921. || gimple_assign_rhs_code (def_stmt) != PLUS_EXPR
  922. || gimple_assign_rhs1 (def_stmt) != last.len
  923. || !integer_onep (gimple_assign_rhs2 (def_stmt)))
  924. return;
  925. }
  926. else
  927. return;
  928. gimple_call_set_arg (last.stmt, len_arg_no, last.len);
  929. update_stmt (last.stmt);
  930. }
  931. /* Handle a strlen call. If strlen of the argument is known, replace
  932. the strlen call with the known value, otherwise remember that strlen
  933. of the argument is stored in the lhs SSA_NAME. */
  934. static void
  935. handle_builtin_strlen (gimple_stmt_iterator *gsi)
  936. {
  937. int idx;
  938. tree src;
  939. gimple stmt = gsi_stmt (*gsi);
  940. tree lhs = gimple_call_lhs (stmt);
  941. if (lhs == NULL_TREE)
  942. return;
  943. src = gimple_call_arg (stmt, 0);
  944. idx = get_stridx (src);
  945. if (idx)
  946. {
  947. strinfo si = NULL;
  948. tree rhs;
  949. if (idx < 0)
  950. rhs = build_int_cst (TREE_TYPE (lhs), ~idx);
  951. else
  952. {
  953. rhs = NULL_TREE;
  954. si = get_strinfo (idx);
  955. if (si != NULL)
  956. rhs = get_string_length (si);
  957. }
  958. if (rhs != NULL_TREE)
  959. {
  960. if (dump_file && (dump_flags & TDF_DETAILS) != 0)
  961. {
  962. fprintf (dump_file, "Optimizing: ");
  963. print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
  964. }
  965. rhs = unshare_expr (rhs);
  966. if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (rhs)))
  967. rhs = fold_convert_loc (gimple_location (stmt),
  968. TREE_TYPE (lhs), rhs);
  969. if (!update_call_from_tree (gsi, rhs))
  970. gimplify_and_update_call_from_tree (gsi, rhs);
  971. stmt = gsi_stmt (*gsi);
  972. update_stmt (stmt);
  973. if (dump_file && (dump_flags & TDF_DETAILS) != 0)
  974. {
  975. fprintf (dump_file, "into: ");
  976. print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
  977. }
  978. if (si != NULL
  979. && TREE_CODE (si->length) != SSA_NAME
  980. && TREE_CODE (si->length) != INTEGER_CST
  981. && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
  982. {
  983. si = unshare_strinfo (si);
  984. si->length = lhs;
  985. }
  986. return;
  987. }
  988. }
  989. if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
  990. return;
  991. if (idx == 0)
  992. idx = new_stridx (src);
  993. else if (get_strinfo (idx) != NULL)
  994. return;
  995. if (idx)
  996. {
  997. strinfo si = new_strinfo (src, idx, lhs);
  998. set_strinfo (idx, si);
  999. find_equal_ptrs (src, idx);
  1000. }
  1001. }
  1002. /* Handle a strchr call. If strlen of the first argument is known, replace
  1003. the strchr (x, 0) call with the endptr or x + strlen, otherwise remember
  1004. that lhs of the call is endptr and strlen of the argument is endptr - x. */
  1005. static void
  1006. handle_builtin_strchr (gimple_stmt_iterator *gsi)
  1007. {
  1008. int idx;
  1009. tree src;
  1010. gimple stmt = gsi_stmt (*gsi);
  1011. tree lhs = gimple_call_lhs (stmt);
  1012. bool with_bounds = gimple_call_with_bounds_p (stmt);
  1013. if (lhs == NULL_TREE)
  1014. return;
  1015. if (!integer_zerop (gimple_call_arg (stmt, with_bounds ? 2 : 1)))
  1016. return;
  1017. src = gimple_call_arg (stmt, 0);
  1018. idx = get_stridx (src);
  1019. if (idx)
  1020. {
  1021. strinfo si = NULL;
  1022. tree rhs;
  1023. if (idx < 0)
  1024. rhs = build_int_cst (size_type_node, ~idx);
  1025. else
  1026. {
  1027. rhs = NULL_TREE;
  1028. si = get_strinfo (idx);
  1029. if (si != NULL)
  1030. rhs = get_string_length (si);
  1031. }
  1032. if (rhs != NULL_TREE)
  1033. {
  1034. location_t loc = gimple_location (stmt);
  1035. if (dump_file && (dump_flags & TDF_DETAILS) != 0)
  1036. {
  1037. fprintf (dump_file, "Optimizing: ");
  1038. print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
  1039. }
  1040. if (si != NULL && si->endptr != NULL_TREE)
  1041. {
  1042. rhs = unshare_expr (si->endptr);
  1043. if (!useless_type_conversion_p (TREE_TYPE (lhs),
  1044. TREE_TYPE (rhs)))
  1045. rhs = fold_convert_loc (loc, TREE_TYPE (lhs), rhs);
  1046. }
  1047. else
  1048. {
  1049. rhs = fold_convert_loc (loc, sizetype, unshare_expr (rhs));
  1050. rhs = fold_build2_loc (loc, POINTER_PLUS_EXPR,
  1051. TREE_TYPE (src), src, rhs);
  1052. if (!useless_type_conversion_p (TREE_TYPE (lhs),
  1053. TREE_TYPE (rhs)))
  1054. rhs = fold_convert_loc (loc, TREE_TYPE (lhs), rhs);
  1055. }
  1056. if (!update_call_from_tree (gsi, rhs))
  1057. gimplify_and_update_call_from_tree (gsi, rhs);
  1058. stmt = gsi_stmt (*gsi);
  1059. update_stmt (stmt);
  1060. if (dump_file && (dump_flags & TDF_DETAILS) != 0)
  1061. {
  1062. fprintf (dump_file, "into: ");
  1063. print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
  1064. }
  1065. if (si != NULL
  1066. && si->endptr == NULL_TREE
  1067. && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
  1068. {
  1069. si = unshare_strinfo (si);
  1070. si->endptr = lhs;
  1071. }
  1072. zero_length_string (lhs, si);
  1073. return;
  1074. }
  1075. }
  1076. if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
  1077. return;
  1078. if (TREE_CODE (src) != SSA_NAME || !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (src))
  1079. {
  1080. if (idx == 0)
  1081. idx = new_stridx (src);
  1082. else if (get_strinfo (idx) != NULL)
  1083. {
  1084. zero_length_string (lhs, NULL);
  1085. return;
  1086. }
  1087. if (idx)
  1088. {
  1089. location_t loc = gimple_location (stmt);
  1090. tree lhsu = fold_convert_loc (loc, size_type_node, lhs);
  1091. tree srcu = fold_convert_loc (loc, size_type_node, src);
  1092. tree length = fold_build2_loc (loc, MINUS_EXPR,
  1093. size_type_node, lhsu, srcu);
  1094. strinfo si = new_strinfo (src, idx, length);
  1095. si->endptr = lhs;
  1096. set_strinfo (idx, si);
  1097. find_equal_ptrs (src, idx);
  1098. zero_length_string (lhs, si);
  1099. }
  1100. }
  1101. else
  1102. zero_length_string (lhs, NULL);
  1103. }
  1104. /* Handle a strcpy-like ({st{r,p}cpy,__st{r,p}cpy_chk}) call.
  1105. If strlen of the second argument is known, strlen of the first argument
  1106. is the same after this call. Furthermore, attempt to convert it to
  1107. memcpy. */
  1108. static void
  1109. handle_builtin_strcpy (enum built_in_function bcode, gimple_stmt_iterator *gsi)
  1110. {
  1111. int idx, didx;
  1112. tree src, dst, srclen, len, lhs, args, type, fn, oldlen;
  1113. bool success;
  1114. gimple stmt = gsi_stmt (*gsi);
  1115. strinfo si, dsi, olddsi, zsi;
  1116. location_t loc;
  1117. bool with_bounds = gimple_call_with_bounds_p (stmt);
  1118. src = gimple_call_arg (stmt, with_bounds ? 2 : 1);
  1119. dst = gimple_call_arg (stmt, 0);
  1120. lhs = gimple_call_lhs (stmt);
  1121. idx = get_stridx (src);
  1122. si = NULL;
  1123. if (idx > 0)
  1124. si = get_strinfo (idx);
  1125. didx = get_stridx (dst);
  1126. olddsi = NULL;
  1127. oldlen = NULL_TREE;
  1128. if (didx > 0)
  1129. olddsi = get_strinfo (didx);
  1130. else if (didx < 0)
  1131. return;
  1132. if (olddsi != NULL)
  1133. adjust_last_stmt (olddsi, stmt, false);
  1134. srclen = NULL_TREE;
  1135. if (si != NULL)
  1136. srclen = get_string_length (si);
  1137. else if (idx < 0)
  1138. srclen = build_int_cst (size_type_node, ~idx);
  1139. loc = gimple_location (stmt);
  1140. if (srclen == NULL_TREE)
  1141. switch (bcode)
  1142. {
  1143. case BUILT_IN_STRCPY:
  1144. case BUILT_IN_STRCPY_CHK:
  1145. case BUILT_IN_STRCPY_CHKP:
  1146. case BUILT_IN_STRCPY_CHK_CHKP:
  1147. if (lhs != NULL_TREE || !builtin_decl_implicit_p (BUILT_IN_STPCPY))
  1148. return;
  1149. break;
  1150. case BUILT_IN_STPCPY:
  1151. case BUILT_IN_STPCPY_CHK:
  1152. case BUILT_IN_STPCPY_CHKP:
  1153. case BUILT_IN_STPCPY_CHK_CHKP:
  1154. if (lhs == NULL_TREE)
  1155. return;
  1156. else
  1157. {
  1158. tree lhsuint = fold_convert_loc (loc, size_type_node, lhs);
  1159. srclen = fold_convert_loc (loc, size_type_node, dst);
  1160. srclen = fold_build2_loc (loc, MINUS_EXPR, size_type_node,
  1161. lhsuint, srclen);
  1162. }
  1163. break;
  1164. default:
  1165. gcc_unreachable ();
  1166. }
  1167. if (didx == 0)
  1168. {
  1169. didx = new_stridx (dst);
  1170. if (didx == 0)
  1171. return;
  1172. }
  1173. if (olddsi != NULL)
  1174. {
  1175. oldlen = olddsi->length;
  1176. dsi = unshare_strinfo (olddsi);
  1177. dsi->length = srclen;
  1178. /* Break the chain, so adjust_related_strinfo on later pointers in
  1179. the chain won't adjust this one anymore. */
  1180. dsi->next = 0;
  1181. dsi->stmt = NULL;
  1182. dsi->endptr = NULL_TREE;
  1183. }
  1184. else
  1185. {
  1186. dsi = new_strinfo (dst, didx, srclen);
  1187. set_strinfo (didx, dsi);
  1188. find_equal_ptrs (dst, didx);
  1189. }
  1190. dsi->writable = true;
  1191. dsi->dont_invalidate = true;
  1192. if (dsi->length == NULL_TREE)
  1193. {
  1194. strinfo chainsi;
  1195. /* If string length of src is unknown, use delayed length
  1196. computation. If string lenth of dst will be needed, it
  1197. can be computed by transforming this strcpy call into
  1198. stpcpy and subtracting dst from the return value. */
  1199. /* Look for earlier strings whose length could be determined if
  1200. this strcpy is turned into an stpcpy. */
  1201. if (dsi->prev != 0 && (chainsi = verify_related_strinfos (dsi)) != NULL)
  1202. {
  1203. for (; chainsi && chainsi != dsi; chainsi = get_strinfo (chainsi->next))
  1204. {
  1205. /* When setting a stmt for delayed length computation
  1206. prevent all strinfos through dsi from being
  1207. invalidated. */
  1208. chainsi = unshare_strinfo (chainsi);
  1209. chainsi->stmt = stmt;
  1210. chainsi->length = NULL_TREE;
  1211. chainsi->endptr = NULL_TREE;
  1212. chainsi->dont_invalidate = true;
  1213. }
  1214. }
  1215. dsi->stmt = stmt;
  1216. return;
  1217. }
  1218. if (olddsi != NULL)
  1219. {
  1220. tree adj = NULL_TREE;
  1221. if (oldlen == NULL_TREE)
  1222. ;
  1223. else if (integer_zerop (oldlen))
  1224. adj = srclen;
  1225. else if (TREE_CODE (oldlen) == INTEGER_CST
  1226. || TREE_CODE (srclen) == INTEGER_CST)
  1227. adj = fold_build2_loc (loc, MINUS_EXPR,
  1228. TREE_TYPE (srclen), srclen,
  1229. fold_convert_loc (loc, TREE_TYPE (srclen),
  1230. oldlen));
  1231. if (adj != NULL_TREE)
  1232. adjust_related_strinfos (loc, dsi, adj);
  1233. else
  1234. dsi->prev = 0;
  1235. }
  1236. /* strcpy src may not overlap dst, so src doesn't need to be
  1237. invalidated either. */
  1238. if (si != NULL)
  1239. si->dont_invalidate = true;
  1240. fn = NULL_TREE;
  1241. zsi = NULL;
  1242. switch (bcode)
  1243. {
  1244. case BUILT_IN_STRCPY:
  1245. case BUILT_IN_STRCPY_CHKP:
  1246. fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
  1247. if (lhs)
  1248. ssa_ver_to_stridx[SSA_NAME_VERSION (lhs)] = didx;
  1249. break;
  1250. case BUILT_IN_STRCPY_CHK:
  1251. case BUILT_IN_STRCPY_CHK_CHKP:
  1252. fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
  1253. if (lhs)
  1254. ssa_ver_to_stridx[SSA_NAME_VERSION (lhs)] = didx;
  1255. break;
  1256. case BUILT_IN_STPCPY:
  1257. case BUILT_IN_STPCPY_CHKP:
  1258. /* This would need adjustment of the lhs (subtract one),
  1259. or detection that the trailing '\0' doesn't need to be
  1260. written, if it will be immediately overwritten.
  1261. fn = builtin_decl_explicit (BUILT_IN_MEMPCPY); */
  1262. if (lhs)
  1263. {
  1264. dsi->endptr = lhs;
  1265. zsi = zero_length_string (lhs, dsi);
  1266. }
  1267. break;
  1268. case BUILT_IN_STPCPY_CHK:
  1269. case BUILT_IN_STPCPY_CHK_CHKP:
  1270. /* This would need adjustment of the lhs (subtract one),
  1271. or detection that the trailing '\0' doesn't need to be
  1272. written, if it will be immediately overwritten.
  1273. fn = builtin_decl_explicit (BUILT_IN_MEMPCPY_CHK); */
  1274. if (lhs)
  1275. {
  1276. dsi->endptr = lhs;
  1277. zsi = zero_length_string (lhs, dsi);
  1278. }
  1279. break;
  1280. default:
  1281. gcc_unreachable ();
  1282. }
  1283. if (zsi != NULL)
  1284. zsi->dont_invalidate = true;
  1285. if (fn == NULL_TREE)
  1286. return;
  1287. args = TYPE_ARG_TYPES (TREE_TYPE (fn));
  1288. type = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
  1289. len = fold_convert_loc (loc, type, unshare_expr (srclen));
  1290. len = fold_build2_loc (loc, PLUS_EXPR, type, len, build_int_cst (type, 1));
  1291. len = force_gimple_operand_gsi (gsi, len, true, NULL_TREE, true,
  1292. GSI_SAME_STMT);
  1293. if (dump_file && (dump_flags & TDF_DETAILS) != 0)
  1294. {
  1295. fprintf (dump_file, "Optimizing: ");
  1296. print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
  1297. }
  1298. if (with_bounds)
  1299. {
  1300. fn = chkp_maybe_create_clone (fn)->decl;
  1301. if (gimple_call_num_args (stmt) == 4)
  1302. success = update_gimple_call (gsi, fn, 5, dst,
  1303. gimple_call_arg (stmt, 1),
  1304. src,
  1305. gimple_call_arg (stmt, 3),
  1306. len);
  1307. else
  1308. success = update_gimple_call (gsi, fn, 6, dst,
  1309. gimple_call_arg (stmt, 1),
  1310. src,
  1311. gimple_call_arg (stmt, 3),
  1312. len,
  1313. gimple_call_arg (stmt, 4));
  1314. }
  1315. else
  1316. if (gimple_call_num_args (stmt) == 2)
  1317. success = update_gimple_call (gsi, fn, 3, dst, src, len);
  1318. else
  1319. success = update_gimple_call (gsi, fn, 4, dst, src, len,
  1320. gimple_call_arg (stmt, 2));
  1321. if (success)
  1322. {
  1323. stmt = gsi_stmt (*gsi);
  1324. gimple_call_set_with_bounds (stmt, with_bounds);
  1325. update_stmt (stmt);
  1326. if (dump_file && (dump_flags & TDF_DETAILS) != 0)
  1327. {
  1328. fprintf (dump_file, "into: ");
  1329. print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
  1330. }
  1331. /* Allow adjust_last_stmt to decrease this memcpy's size. */
  1332. laststmt.stmt = stmt;
  1333. laststmt.len = srclen;
  1334. laststmt.stridx = dsi->idx;
  1335. }
  1336. else if (dump_file && (dump_flags & TDF_DETAILS) != 0)
  1337. fprintf (dump_file, "not possible.\n");
  1338. }
  1339. /* Handle a memcpy-like ({mem{,p}cpy,__mem{,p}cpy_chk}) call.
  1340. If strlen of the second argument is known and length of the third argument
  1341. is that plus one, strlen of the first argument is the same after this
  1342. call. */
  1343. static void
  1344. handle_builtin_memcpy (enum built_in_function bcode, gimple_stmt_iterator *gsi)
  1345. {
  1346. int idx, didx;
  1347. tree src, dst, len, lhs, oldlen, newlen;
  1348. gimple stmt = gsi_stmt (*gsi);
  1349. strinfo si, dsi, olddsi;
  1350. bool with_bounds = gimple_call_with_bounds_p (stmt);
  1351. len = gimple_call_arg (stmt, with_bounds ? 4 : 2);
  1352. src = gimple_call_arg (stmt, with_bounds ? 2 : 1);
  1353. dst = gimple_call_arg (stmt, 0);
  1354. idx = get_stridx (src);
  1355. if (idx == 0)
  1356. return;
  1357. didx = get_stridx (dst);
  1358. olddsi = NULL;
  1359. if (didx > 0)
  1360. olddsi = get_strinfo (didx);
  1361. else if (didx < 0)
  1362. return;
  1363. if (olddsi != NULL
  1364. && tree_fits_uhwi_p (len)
  1365. && !integer_zerop (len))
  1366. adjust_last_stmt (olddsi, stmt, false);
  1367. if (idx > 0)
  1368. {
  1369. gimple def_stmt;
  1370. /* Handle memcpy (x, y, l) where l is strlen (y) + 1. */
  1371. si = get_strinfo (idx);
  1372. if (si == NULL || si->length == NULL_TREE)
  1373. return;
  1374. if (TREE_CODE (len) != SSA_NAME)
  1375. return;
  1376. def_stmt = SSA_NAME_DEF_STMT (len);
  1377. if (!is_gimple_assign (def_stmt)
  1378. || gimple_assign_rhs_code (def_stmt) != PLUS_EXPR
  1379. || gimple_assign_rhs1 (def_stmt) != si->length
  1380. || !integer_onep (gimple_assign_rhs2 (def_stmt)))
  1381. return;
  1382. }
  1383. else
  1384. {
  1385. si = NULL;
  1386. /* Handle memcpy (x, "abcd", 5) or
  1387. memcpy (x, "abc\0uvw", 7). */
  1388. if (!tree_fits_uhwi_p (len)
  1389. || tree_to_uhwi (len) <= (unsigned HOST_WIDE_INT) ~idx)
  1390. return;
  1391. }
  1392. if (olddsi != NULL && TREE_CODE (len) == SSA_NAME)
  1393. adjust_last_stmt (olddsi, stmt, false);
  1394. if (didx == 0)
  1395. {
  1396. didx = new_stridx (dst);
  1397. if (didx == 0)
  1398. return;
  1399. }
  1400. if (si != NULL)
  1401. newlen = si->length;
  1402. else
  1403. newlen = build_int_cst (size_type_node, ~idx);
  1404. oldlen = NULL_TREE;
  1405. if (olddsi != NULL)
  1406. {
  1407. dsi = unshare_strinfo (olddsi);
  1408. oldlen = olddsi->length;
  1409. dsi->length = newlen;
  1410. /* Break the chain, so adjust_related_strinfo on later pointers in
  1411. the chain won't adjust this one anymore. */
  1412. dsi->next = 0;
  1413. dsi->stmt = NULL;
  1414. dsi->endptr = NULL_TREE;
  1415. }
  1416. else
  1417. {
  1418. dsi = new_strinfo (dst, didx, newlen);
  1419. set_strinfo (didx, dsi);
  1420. find_equal_ptrs (dst, didx);
  1421. }
  1422. dsi->writable = true;
  1423. dsi->dont_invalidate = true;
  1424. if (olddsi != NULL)
  1425. {
  1426. tree adj = NULL_TREE;
  1427. location_t loc = gimple_location (stmt);
  1428. if (oldlen == NULL_TREE)
  1429. ;
  1430. else if (integer_zerop (oldlen))
  1431. adj = dsi->length;
  1432. else if (TREE_CODE (oldlen) == INTEGER_CST
  1433. || TREE_CODE (dsi->length) == INTEGER_CST)
  1434. adj = fold_build2_loc (loc, MINUS_EXPR,
  1435. TREE_TYPE (dsi->length), dsi->length,
  1436. fold_convert_loc (loc, TREE_TYPE (dsi->length),
  1437. oldlen));
  1438. if (adj != NULL_TREE)
  1439. adjust_related_strinfos (loc, dsi, adj);
  1440. else
  1441. dsi->prev = 0;
  1442. }
  1443. /* memcpy src may not overlap dst, so src doesn't need to be
  1444. invalidated either. */
  1445. if (si != NULL)
  1446. si->dont_invalidate = true;
  1447. lhs = gimple_call_lhs (stmt);
  1448. switch (bcode)
  1449. {
  1450. case BUILT_IN_MEMCPY:
  1451. case BUILT_IN_MEMCPY_CHK:
  1452. case BUILT_IN_MEMCPY_CHKP:
  1453. case BUILT_IN_MEMCPY_CHK_CHKP:
  1454. /* Allow adjust_last_stmt to decrease this memcpy's size. */
  1455. laststmt.stmt = stmt;
  1456. laststmt.len = dsi->length;
  1457. laststmt.stridx = dsi->idx;
  1458. if (lhs)
  1459. ssa_ver_to_stridx[SSA_NAME_VERSION (lhs)] = didx;
  1460. break;
  1461. case BUILT_IN_MEMPCPY:
  1462. case BUILT_IN_MEMPCPY_CHK:
  1463. case BUILT_IN_MEMPCPY_CHKP:
  1464. case BUILT_IN_MEMPCPY_CHK_CHKP:
  1465. break;
  1466. default:
  1467. gcc_unreachable ();
  1468. }
  1469. }
  1470. /* Handle a strcat-like ({strcat,__strcat_chk}) call.
  1471. If strlen of the second argument is known, strlen of the first argument
  1472. is increased by the length of the second argument. Furthermore, attempt
  1473. to convert it to memcpy/strcpy if the length of the first argument
  1474. is known. */
  1475. static void
  1476. handle_builtin_strcat (enum built_in_function bcode, gimple_stmt_iterator *gsi)
  1477. {
  1478. int idx, didx;
  1479. tree src, dst, srclen, dstlen, len, lhs, args, type, fn, objsz, endptr;
  1480. bool success;
  1481. gimple stmt = gsi_stmt (*gsi);
  1482. strinfo si, dsi;
  1483. location_t loc;
  1484. bool with_bounds = gimple_call_with_bounds_p (stmt);
  1485. src = gimple_call_arg (stmt, with_bounds ? 2 : 1);
  1486. dst = gimple_call_arg (stmt, 0);
  1487. lhs = gimple_call_lhs (stmt);
  1488. didx = get_stridx (dst);
  1489. if (didx < 0)
  1490. return;
  1491. dsi = NULL;
  1492. if (didx > 0)
  1493. dsi = get_strinfo (didx);
  1494. if (dsi == NULL || get_string_length (dsi) == NULL_TREE)
  1495. {
  1496. /* strcat (p, q) can be transformed into
  1497. tmp = p + strlen (p); endptr = strpcpy (tmp, q);
  1498. with length endptr - p if we need to compute the length
  1499. later on. Don't do this transformation if we don't need
  1500. it. */
  1501. if (builtin_decl_implicit_p (BUILT_IN_STPCPY) && lhs == NULL_TREE)
  1502. {
  1503. if (didx == 0)
  1504. {
  1505. didx = new_stridx (dst);
  1506. if (didx == 0)
  1507. return;
  1508. }
  1509. if (dsi == NULL)
  1510. {
  1511. dsi = new_strinfo (dst, didx, NULL_TREE);
  1512. set_strinfo (didx, dsi);
  1513. find_equal_ptrs (dst, didx);
  1514. }
  1515. else
  1516. {
  1517. dsi = unshare_strinfo (dsi);
  1518. dsi->length = NULL_TREE;
  1519. dsi->next = 0;
  1520. dsi->endptr = NULL_TREE;
  1521. }
  1522. dsi->writable = true;
  1523. dsi->stmt = stmt;
  1524. dsi->dont_invalidate = true;
  1525. }
  1526. return;
  1527. }
  1528. srclen = NULL_TREE;
  1529. si = NULL;
  1530. idx = get_stridx (src);
  1531. if (idx < 0)
  1532. srclen = build_int_cst (size_type_node, ~idx);
  1533. else if (idx > 0)
  1534. {
  1535. si = get_strinfo (idx);
  1536. if (si != NULL)
  1537. srclen = get_string_length (si);
  1538. }
  1539. loc = gimple_location (stmt);
  1540. dstlen = dsi->length;
  1541. endptr = dsi->endptr;
  1542. dsi = unshare_strinfo (dsi);
  1543. dsi->endptr = NULL_TREE;
  1544. dsi->stmt = NULL;
  1545. dsi->writable = true;
  1546. if (srclen != NULL_TREE)
  1547. {
  1548. dsi->length = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (dsi->length),
  1549. dsi->length, srclen);
  1550. adjust_related_strinfos (loc, dsi, srclen);
  1551. dsi->dont_invalidate = true;
  1552. }
  1553. else
  1554. {
  1555. dsi->length = NULL;
  1556. if (lhs == NULL_TREE && builtin_decl_implicit_p (BUILT_IN_STPCPY))
  1557. dsi->dont_invalidate = true;
  1558. }
  1559. if (si != NULL)
  1560. /* strcat src may not overlap dst, so src doesn't need to be
  1561. invalidated either. */
  1562. si->dont_invalidate = true;
  1563. /* For now. Could remove the lhs from the call and add
  1564. lhs = dst; afterwards. */
  1565. if (lhs)
  1566. return;
  1567. fn = NULL_TREE;
  1568. objsz = NULL_TREE;
  1569. switch (bcode)
  1570. {
  1571. case BUILT_IN_STRCAT:
  1572. case BUILT_IN_STRCAT_CHKP:
  1573. if (srclen != NULL_TREE)
  1574. fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
  1575. else
  1576. fn = builtin_decl_implicit (BUILT_IN_STRCPY);
  1577. break;
  1578. case BUILT_IN_STRCAT_CHK:
  1579. case BUILT_IN_STRCAT_CHK_CHKP:
  1580. if (srclen != NULL_TREE)
  1581. fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
  1582. else
  1583. fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK);
  1584. objsz = gimple_call_arg (stmt, with_bounds ? 4 : 2);
  1585. break;
  1586. default:
  1587. gcc_unreachable ();
  1588. }
  1589. if (fn == NULL_TREE)
  1590. return;
  1591. len = NULL_TREE;
  1592. if (srclen != NULL_TREE)
  1593. {
  1594. args = TYPE_ARG_TYPES (TREE_TYPE (fn));
  1595. type = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
  1596. len = fold_convert_loc (loc, type, unshare_expr (srclen));
  1597. len = fold_build2_loc (loc, PLUS_EXPR, type, len,
  1598. build_int_cst (type, 1));
  1599. len = force_gimple_operand_gsi (gsi, len, true, NULL_TREE, true,
  1600. GSI_SAME_STMT);
  1601. }
  1602. if (endptr)
  1603. dst = fold_convert_loc (loc, TREE_TYPE (dst), unshare_expr (endptr));
  1604. else
  1605. dst = fold_build2_loc (loc, POINTER_PLUS_EXPR,
  1606. TREE_TYPE (dst), unshare_expr (dst),
  1607. fold_convert_loc (loc, sizetype,
  1608. unshare_expr (dstlen)));
  1609. dst = force_gimple_operand_gsi (gsi, dst, true, NULL_TREE, true,
  1610. GSI_SAME_STMT);
  1611. if (dump_file && (dump_flags & TDF_DETAILS) != 0)
  1612. {
  1613. fprintf (dump_file, "Optimizing: ");
  1614. print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
  1615. }
  1616. if (with_bounds)
  1617. {
  1618. fn = chkp_maybe_create_clone (fn)->decl;
  1619. if (srclen != NULL_TREE)
  1620. success = update_gimple_call (gsi, fn, 5 + (objsz != NULL_TREE),
  1621. dst,
  1622. gimple_call_arg (stmt, 1),
  1623. src,
  1624. gimple_call_arg (stmt, 3),
  1625. len, objsz);
  1626. else
  1627. success = update_gimple_call (gsi, fn, 4 + (objsz != NULL_TREE),
  1628. dst,
  1629. gimple_call_arg (stmt, 1),
  1630. src,
  1631. gimple_call_arg (stmt, 3),
  1632. objsz);
  1633. }
  1634. else
  1635. if (srclen != NULL_TREE)
  1636. success = update_gimple_call (gsi, fn, 3 + (objsz != NULL_TREE),
  1637. dst, src, len, objsz);
  1638. else
  1639. success = update_gimple_call (gsi, fn, 2 + (objsz != NULL_TREE),
  1640. dst, src, objsz);
  1641. if (success)
  1642. {
  1643. stmt = gsi_stmt (*gsi);
  1644. gimple_call_set_with_bounds (stmt, with_bounds);
  1645. update_stmt (stmt);
  1646. if (dump_file && (dump_flags & TDF_DETAILS) != 0)
  1647. {
  1648. fprintf (dump_file, "into: ");
  1649. print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
  1650. }
  1651. /* If srclen == NULL, note that current string length can be
  1652. computed by transforming this strcpy into stpcpy. */
  1653. if (srclen == NULL_TREE && dsi->dont_invalidate)
  1654. dsi->stmt = stmt;
  1655. adjust_last_stmt (dsi, stmt, true);
  1656. if (srclen != NULL_TREE)
  1657. {
  1658. laststmt.stmt = stmt;
  1659. laststmt.len = srclen;
  1660. laststmt.stridx = dsi->idx;
  1661. }
  1662. }
  1663. else if (dump_file && (dump_flags & TDF_DETAILS) != 0)
  1664. fprintf (dump_file, "not possible.\n");
  1665. }
  1666. /* Handle a call to malloc or calloc. */
  1667. static void
  1668. handle_builtin_malloc (enum built_in_function bcode, gimple_stmt_iterator *gsi)
  1669. {
  1670. gimple stmt = gsi_stmt (*gsi);
  1671. tree lhs = gimple_call_lhs (stmt);
  1672. gcc_assert (get_stridx (lhs) == 0);
  1673. int idx = new_stridx (lhs);
  1674. tree length = NULL_TREE;
  1675. if (bcode == BUILT_IN_CALLOC)
  1676. length = build_int_cst (size_type_node, 0);
  1677. strinfo si = new_strinfo (lhs, idx, length);
  1678. if (bcode == BUILT_IN_CALLOC)
  1679. si->endptr = lhs;
  1680. set_strinfo (idx, si);
  1681. si->writable = true;
  1682. si->stmt = stmt;
  1683. si->dont_invalidate = true;
  1684. }
  1685. /* Handle a call to memset.
  1686. After a call to calloc, memset(,0,) is unnecessary.
  1687. memset(malloc(n),0,n) is calloc(n,1). */
  1688. static bool
  1689. handle_builtin_memset (gimple_stmt_iterator *gsi)
  1690. {
  1691. gimple stmt2 = gsi_stmt (*gsi);
  1692. if (!integer_zerop (gimple_call_arg (stmt2, 1)))
  1693. return true;
  1694. tree ptr = gimple_call_arg (stmt2, 0);
  1695. int idx1 = get_stridx (ptr);
  1696. if (idx1 <= 0)
  1697. return true;
  1698. strinfo si1 = get_strinfo (idx1);
  1699. if (!si1)
  1700. return true;
  1701. gimple stmt1 = si1->stmt;
  1702. if (!stmt1 || !is_gimple_call (stmt1))
  1703. return true;
  1704. tree callee1 = gimple_call_fndecl (stmt1);
  1705. if (!gimple_call_builtin_p (stmt1, BUILT_IN_NORMAL))
  1706. return true;
  1707. enum built_in_function code1 = DECL_FUNCTION_CODE (callee1);
  1708. tree size = gimple_call_arg (stmt2, 2);
  1709. if (code1 == BUILT_IN_CALLOC)
  1710. /* Not touching stmt1 */ ;
  1711. else if (code1 == BUILT_IN_MALLOC
  1712. && operand_equal_p (gimple_call_arg (stmt1, 0), size, 0))
  1713. {
  1714. gimple_stmt_iterator gsi1 = gsi_for_stmt (stmt1);
  1715. update_gimple_call (&gsi1, builtin_decl_implicit (BUILT_IN_CALLOC), 2,
  1716. size, build_one_cst (size_type_node));
  1717. si1->length = build_int_cst (size_type_node, 0);
  1718. si1->stmt = gsi_stmt (gsi1);
  1719. }
  1720. else
  1721. return true;
  1722. tree lhs = gimple_call_lhs (stmt2);
  1723. unlink_stmt_vdef (stmt2);
  1724. if (lhs)
  1725. {
  1726. gimple assign = gimple_build_assign (lhs, ptr);
  1727. gsi_replace (gsi, assign, false);
  1728. }
  1729. else
  1730. {
  1731. gsi_remove (gsi, true);
  1732. release_defs (stmt2);
  1733. }
  1734. return false;
  1735. }
  1736. /* Handle a POINTER_PLUS_EXPR statement.
  1737. For p = "abcd" + 2; compute associated length, or if
  1738. p = q + off is pointing to a '\0' character of a string, call
  1739. zero_length_string on it. */
  1740. static void
  1741. handle_pointer_plus (gimple_stmt_iterator *gsi)
  1742. {
  1743. gimple stmt = gsi_stmt (*gsi);
  1744. tree lhs = gimple_assign_lhs (stmt), off;
  1745. int idx = get_stridx (gimple_assign_rhs1 (stmt));
  1746. strinfo si, zsi;
  1747. if (idx == 0)
  1748. return;
  1749. if (idx < 0)
  1750. {
  1751. tree off = gimple_assign_rhs2 (stmt);
  1752. if (tree_fits_uhwi_p (off)
  1753. && tree_to_uhwi (off) <= (unsigned HOST_WIDE_INT) ~idx)
  1754. ssa_ver_to_stridx[SSA_NAME_VERSION (lhs)]
  1755. = ~(~idx - (int) tree_to_uhwi (off));
  1756. return;
  1757. }
  1758. si = get_strinfo (idx);
  1759. if (si == NULL || si->length == NULL_TREE)
  1760. return;
  1761. off = gimple_assign_rhs2 (stmt);
  1762. zsi = NULL;
  1763. if (operand_equal_p (si->length, off, 0))
  1764. zsi = zero_length_string (lhs, si);
  1765. else if (TREE_CODE (off) == SSA_NAME)
  1766. {
  1767. gimple def_stmt = SSA_NAME_DEF_STMT (off);
  1768. if (gimple_assign_single_p (def_stmt)
  1769. && operand_equal_p (si->length, gimple_assign_rhs1 (def_stmt), 0))
  1770. zsi = zero_length_string (lhs, si);
  1771. }
  1772. if (zsi != NULL
  1773. && si->endptr != NULL_TREE
  1774. && si->endptr != lhs
  1775. && TREE_CODE (si->endptr) == SSA_NAME)
  1776. {
  1777. enum tree_code rhs_code
  1778. = useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (si->endptr))
  1779. ? SSA_NAME : NOP_EXPR;
  1780. gimple_assign_set_rhs_with_ops (gsi, rhs_code, si->endptr);
  1781. gcc_assert (gsi_stmt (*gsi) == stmt);
  1782. update_stmt (stmt);
  1783. }
  1784. }
  1785. /* Handle a single character store. */
  1786. static bool
  1787. handle_char_store (gimple_stmt_iterator *gsi)
  1788. {
  1789. int idx = -1;
  1790. strinfo si = NULL;
  1791. gimple stmt = gsi_stmt (*gsi);
  1792. tree ssaname = NULL_TREE, lhs = gimple_assign_lhs (stmt);
  1793. if (TREE_CODE (lhs) == MEM_REF
  1794. && TREE_CODE (TREE_OPERAND (lhs, 0)) == SSA_NAME)
  1795. {
  1796. if (integer_zerop (TREE_OPERAND (lhs, 1)))
  1797. {
  1798. ssaname = TREE_OPERAND (lhs, 0);
  1799. idx = get_stridx (ssaname);
  1800. }
  1801. }
  1802. else
  1803. idx = get_addr_stridx (lhs);
  1804. if (idx > 0)
  1805. {
  1806. si = get_strinfo (idx);
  1807. if (si != NULL && si->length != NULL_TREE && integer_zerop (si->length))
  1808. {
  1809. if (initializer_zerop (gimple_assign_rhs1 (stmt)))
  1810. {
  1811. /* When storing '\0', the store can be removed
  1812. if we know it has been stored in the current function. */
  1813. if (!stmt_could_throw_p (stmt) && si->writable)
  1814. {
  1815. unlink_stmt_vdef (stmt);
  1816. release_defs (stmt);
  1817. gsi_remove (gsi, true);
  1818. return false;
  1819. }
  1820. else
  1821. {
  1822. si->writable = true;
  1823. gsi_next (gsi);
  1824. return false;
  1825. }
  1826. }
  1827. else
  1828. /* Otherwise this statement overwrites the '\0' with
  1829. something, if the previous stmt was a memcpy,
  1830. its length may be decreased. */
  1831. adjust_last_stmt (si, stmt, false);
  1832. }
  1833. else if (si != NULL && integer_zerop (gimple_assign_rhs1 (stmt)))
  1834. {
  1835. si = unshare_strinfo (si);
  1836. si->length = build_int_cst (size_type_node, 0);
  1837. si->endptr = NULL;
  1838. si->prev = 0;
  1839. si->next = 0;
  1840. si->stmt = NULL;
  1841. si->first = 0;
  1842. si->writable = true;
  1843. if (ssaname && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssaname))
  1844. si->endptr = ssaname;
  1845. si->dont_invalidate = true;
  1846. }
  1847. /* If si->length is non-zero constant, we aren't overwriting '\0',
  1848. and if we aren't storing '\0', we know that the length of the
  1849. string and any other zero terminated string in memory remains
  1850. the same. In that case we move to the next gimple statement and
  1851. return to signal the caller that it shouldn't invalidate anything.
  1852. This is benefical for cases like:
  1853. char p[20];
  1854. void foo (char *q)
  1855. {
  1856. strcpy (p, "foobar");
  1857. size_t len = strlen (p); // This can be optimized into 6
  1858. size_t len2 = strlen (q); // This has to be computed
  1859. p[0] = 'X';
  1860. size_t len3 = strlen (p); // This can be optimized into 6
  1861. size_t len4 = strlen (q); // This can be optimized into len2
  1862. bar (len, len2, len3, len4);
  1863. }
  1864. */
  1865. else if (si != NULL && si->length != NULL_TREE
  1866. && TREE_CODE (si->length) == INTEGER_CST
  1867. && integer_nonzerop (gimple_assign_rhs1 (stmt)))
  1868. {
  1869. gsi_next (gsi);
  1870. return false;
  1871. }
  1872. }
  1873. else if (idx == 0 && initializer_zerop (gimple_assign_rhs1 (stmt)))
  1874. {
  1875. if (ssaname)
  1876. {
  1877. si = zero_length_string (ssaname, NULL);
  1878. if (si != NULL)
  1879. si->dont_invalidate = true;
  1880. }
  1881. else
  1882. {
  1883. int idx = new_addr_stridx (lhs);
  1884. if (idx != 0)
  1885. {
  1886. si = new_strinfo (build_fold_addr_expr (lhs), idx,
  1887. build_int_cst (size_type_node, 0));
  1888. set_strinfo (idx, si);
  1889. si->dont_invalidate = true;
  1890. }
  1891. }
  1892. if (si != NULL)
  1893. si->writable = true;
  1894. }
  1895. else if (idx == 0
  1896. && TREE_CODE (gimple_assign_rhs1 (stmt)) == STRING_CST
  1897. && ssaname == NULL_TREE
  1898. && TREE_CODE (TREE_TYPE (lhs)) == ARRAY_TYPE)
  1899. {
  1900. size_t l = strlen (TREE_STRING_POINTER (gimple_assign_rhs1 (stmt)));
  1901. HOST_WIDE_INT a = int_size_in_bytes (TREE_TYPE (lhs));
  1902. if (a > 0 && (unsigned HOST_WIDE_INT) a > l)
  1903. {
  1904. int idx = new_addr_stridx (lhs);
  1905. if (idx != 0)
  1906. {
  1907. si = new_strinfo (build_fold_addr_expr (lhs), idx,
  1908. build_int_cst (size_type_node, l));
  1909. set_strinfo (idx, si);
  1910. si->dont_invalidate = true;
  1911. }
  1912. }
  1913. }
  1914. if (si != NULL && initializer_zerop (gimple_assign_rhs1 (stmt)))
  1915. {
  1916. /* Allow adjust_last_stmt to remove it if the stored '\0'
  1917. is immediately overwritten. */
  1918. laststmt.stmt = stmt;
  1919. laststmt.len = build_int_cst (size_type_node, 1);
  1920. laststmt.stridx = si->idx;
  1921. }
  1922. return true;
  1923. }
  1924. /* Attempt to optimize a single statement at *GSI using string length
  1925. knowledge. */
  1926. static bool
  1927. strlen_optimize_stmt (gimple_stmt_iterator *gsi)
  1928. {
  1929. gimple stmt = gsi_stmt (*gsi);
  1930. if (is_gimple_call (stmt))
  1931. {
  1932. tree callee = gimple_call_fndecl (stmt);
  1933. if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
  1934. switch (DECL_FUNCTION_CODE (callee))
  1935. {
  1936. case BUILT_IN_STRLEN:
  1937. case BUILT_IN_STRLEN_CHKP:
  1938. handle_builtin_strlen (gsi);
  1939. break;
  1940. case BUILT_IN_STRCHR:
  1941. case BUILT_IN_STRCHR_CHKP:
  1942. handle_builtin_strchr (gsi);
  1943. break;
  1944. case BUILT_IN_STRCPY:
  1945. case BUILT_IN_STRCPY_CHK:
  1946. case BUILT_IN_STPCPY:
  1947. case BUILT_IN_STPCPY_CHK:
  1948. case BUILT_IN_STRCPY_CHKP:
  1949. case BUILT_IN_STRCPY_CHK_CHKP:
  1950. case BUILT_IN_STPCPY_CHKP:
  1951. case BUILT_IN_STPCPY_CHK_CHKP:
  1952. handle_builtin_strcpy (DECL_FUNCTION_CODE (callee), gsi);
  1953. break;
  1954. case BUILT_IN_MEMCPY:
  1955. case BUILT_IN_MEMCPY_CHK:
  1956. case BUILT_IN_MEMPCPY:
  1957. case BUILT_IN_MEMPCPY_CHK:
  1958. case BUILT_IN_MEMCPY_CHKP:
  1959. case BUILT_IN_MEMCPY_CHK_CHKP:
  1960. case BUILT_IN_MEMPCPY_CHKP:
  1961. case BUILT_IN_MEMPCPY_CHK_CHKP:
  1962. handle_builtin_memcpy (DECL_FUNCTION_CODE (callee), gsi);
  1963. break;
  1964. case BUILT_IN_STRCAT:
  1965. case BUILT_IN_STRCAT_CHK:
  1966. case BUILT_IN_STRCAT_CHKP:
  1967. case BUILT_IN_STRCAT_CHK_CHKP:
  1968. handle_builtin_strcat (DECL_FUNCTION_CODE (callee), gsi);
  1969. break;
  1970. case BUILT_IN_MALLOC:
  1971. case BUILT_IN_CALLOC:
  1972. handle_builtin_malloc (DECL_FUNCTION_CODE (callee), gsi);
  1973. break;
  1974. case BUILT_IN_MEMSET:
  1975. if (!handle_builtin_memset (gsi))
  1976. return false;
  1977. break;
  1978. default:
  1979. break;
  1980. }
  1981. }
  1982. else if (is_gimple_assign (stmt) && !gimple_clobber_p (stmt))
  1983. {
  1984. tree lhs = gimple_assign_lhs (stmt);
  1985. if (TREE_CODE (lhs) == SSA_NAME && POINTER_TYPE_P (TREE_TYPE (lhs)))
  1986. {
  1987. if (gimple_assign_single_p (stmt)
  1988. || (gimple_assign_cast_p (stmt)
  1989. && POINTER_TYPE_P (TREE_TYPE (gimple_assign_rhs1 (stmt)))))
  1990. {
  1991. int idx = get_stridx (gimple_assign_rhs1 (stmt));
  1992. ssa_ver_to_stridx[SSA_NAME_VERSION (lhs)] = idx;
  1993. }
  1994. else if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
  1995. handle_pointer_plus (gsi);
  1996. }
  1997. else if (TREE_CODE (lhs) != SSA_NAME && !TREE_SIDE_EFFECTS (lhs))
  1998. {
  1999. tree type = TREE_TYPE (lhs);
  2000. if (TREE_CODE (type) == ARRAY_TYPE)
  2001. type = TREE_TYPE (type);
  2002. if (TREE_CODE (type) == INTEGER_TYPE
  2003. && TYPE_MODE (type) == TYPE_MODE (char_type_node)
  2004. && TYPE_PRECISION (type) == TYPE_PRECISION (char_type_node))
  2005. {
  2006. if (! handle_char_store (gsi))
  2007. return false;
  2008. }
  2009. }
  2010. }
  2011. if (gimple_vdef (stmt))
  2012. maybe_invalidate (stmt);
  2013. return true;
  2014. }
  2015. /* Recursively call maybe_invalidate on stmts that might be executed
  2016. in between dombb and current bb and that contain a vdef. Stop when
  2017. *count stmts are inspected, or if the whole strinfo vector has
  2018. been invalidated. */
  2019. static void
  2020. do_invalidate (basic_block dombb, gimple phi, bitmap visited, int *count)
  2021. {
  2022. unsigned int i, n = gimple_phi_num_args (phi);
  2023. for (i = 0; i < n; i++)
  2024. {
  2025. tree vuse = gimple_phi_arg_def (phi, i);
  2026. gimple stmt = SSA_NAME_DEF_STMT (vuse);
  2027. basic_block bb = gimple_bb (stmt);
  2028. if (bb == NULL
  2029. || bb == dombb
  2030. || !bitmap_set_bit (visited, bb->index)
  2031. || !dominated_by_p (CDI_DOMINATORS, bb, dombb))
  2032. continue;
  2033. while (1)
  2034. {
  2035. if (gimple_code (stmt) == GIMPLE_PHI)
  2036. {
  2037. do_invalidate (dombb, stmt, visited, count);
  2038. if (*count == 0)
  2039. return;
  2040. break;
  2041. }
  2042. if (--*count == 0)
  2043. return;
  2044. if (!maybe_invalidate (stmt))
  2045. {
  2046. *count = 0;
  2047. return;
  2048. }
  2049. vuse = gimple_vuse (stmt);
  2050. stmt = SSA_NAME_DEF_STMT (vuse);
  2051. if (gimple_bb (stmt) != bb)
  2052. {
  2053. bb = gimple_bb (stmt);
  2054. if (bb == NULL
  2055. || bb == dombb
  2056. || !bitmap_set_bit (visited, bb->index)
  2057. || !dominated_by_p (CDI_DOMINATORS, bb, dombb))
  2058. break;
  2059. }
  2060. }
  2061. }
  2062. }
  2063. class strlen_dom_walker : public dom_walker
  2064. {
  2065. public:
  2066. strlen_dom_walker (cdi_direction direction) : dom_walker (direction) {}
  2067. virtual void before_dom_children (basic_block);
  2068. virtual void after_dom_children (basic_block);
  2069. };
  2070. /* Callback for walk_dominator_tree. Attempt to optimize various
  2071. string ops by remembering string lenths pointed by pointer SSA_NAMEs. */
  2072. void
  2073. strlen_dom_walker::before_dom_children (basic_block bb)
  2074. {
  2075. basic_block dombb = get_immediate_dominator (CDI_DOMINATORS, bb);
  2076. if (dombb == NULL)
  2077. stridx_to_strinfo = NULL;
  2078. else
  2079. {
  2080. stridx_to_strinfo = ((vec<strinfo, va_heap, vl_embed> *) dombb->aux);
  2081. if (stridx_to_strinfo)
  2082. {
  2083. for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
  2084. gsi_next (&gsi))
  2085. {
  2086. gphi *phi = gsi.phi ();
  2087. if (virtual_operand_p (gimple_phi_result (phi)))
  2088. {
  2089. bitmap visited = BITMAP_ALLOC (NULL);
  2090. int count_vdef = 100;
  2091. do_invalidate (dombb, phi, visited, &count_vdef);
  2092. BITMAP_FREE (visited);
  2093. if (count_vdef == 0)
  2094. {
  2095. /* If there were too many vdefs in between immediate
  2096. dominator and current bb, invalidate everything.
  2097. If stridx_to_strinfo has been unshared, we need
  2098. to free it, otherwise just set it to NULL. */
  2099. if (!strinfo_shared ())
  2100. {
  2101. unsigned int i;
  2102. strinfo si;
  2103. for (i = 1;
  2104. vec_safe_iterate (stridx_to_strinfo, i, &si);
  2105. ++i)
  2106. {
  2107. free_strinfo (si);
  2108. (*stridx_to_strinfo)[i] = NULL;
  2109. }
  2110. }
  2111. else
  2112. stridx_to_strinfo = NULL;
  2113. }
  2114. break;
  2115. }
  2116. }
  2117. }
  2118. }
  2119. /* If all PHI arguments have the same string index, the PHI result
  2120. has it as well. */
  2121. for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
  2122. gsi_next (&gsi))
  2123. {
  2124. gphi *phi = gsi.phi ();
  2125. tree result = gimple_phi_result (phi);
  2126. if (!virtual_operand_p (result) && POINTER_TYPE_P (TREE_TYPE (result)))
  2127. {
  2128. int idx = get_stridx (gimple_phi_arg_def (phi, 0));
  2129. if (idx != 0)
  2130. {
  2131. unsigned int i, n = gimple_phi_num_args (phi);
  2132. for (i = 1; i < n; i++)
  2133. if (idx != get_stridx (gimple_phi_arg_def (phi, i)))
  2134. break;
  2135. if (i == n)
  2136. ssa_ver_to_stridx[SSA_NAME_VERSION (result)] = idx;
  2137. }
  2138. }
  2139. }
  2140. /* Attempt to optimize individual statements. */
  2141. for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
  2142. if (strlen_optimize_stmt (&gsi))
  2143. gsi_next (&gsi);
  2144. bb->aux = stridx_to_strinfo;
  2145. if (vec_safe_length (stridx_to_strinfo) && !strinfo_shared ())
  2146. (*stridx_to_strinfo)[0] = (strinfo) bb;
  2147. }
  2148. /* Callback for walk_dominator_tree. Free strinfo vector if it is
  2149. owned by the current bb, clear bb->aux. */
  2150. void
  2151. strlen_dom_walker::after_dom_children (basic_block bb)
  2152. {
  2153. if (bb->aux)
  2154. {
  2155. stridx_to_strinfo = ((vec<strinfo, va_heap, vl_embed> *) bb->aux);
  2156. if (vec_safe_length (stridx_to_strinfo)
  2157. && (*stridx_to_strinfo)[0] == (strinfo) bb)
  2158. {
  2159. unsigned int i;
  2160. strinfo si;
  2161. for (i = 1; vec_safe_iterate (stridx_to_strinfo, i, &si); ++i)
  2162. free_strinfo (si);
  2163. vec_free (stridx_to_strinfo);
  2164. }
  2165. bb->aux = NULL;
  2166. }
  2167. }
  2168. /* Main entry point. */
  2169. namespace {
  2170. const pass_data pass_data_strlen =
  2171. {
  2172. GIMPLE_PASS, /* type */
  2173. "strlen", /* name */
  2174. OPTGROUP_NONE, /* optinfo_flags */
  2175. TV_TREE_STRLEN, /* tv_id */
  2176. ( PROP_cfg | PROP_ssa ), /* properties_required */
  2177. 0, /* properties_provided */
  2178. 0, /* properties_destroyed */
  2179. 0, /* todo_flags_start */
  2180. 0, /* todo_flags_finish */
  2181. };
  2182. class pass_strlen : public gimple_opt_pass
  2183. {
  2184. public:
  2185. pass_strlen (gcc::context *ctxt)
  2186. : gimple_opt_pass (pass_data_strlen, ctxt)
  2187. {}
  2188. /* opt_pass methods: */
  2189. virtual bool gate (function *) { return flag_optimize_strlen != 0; }
  2190. virtual unsigned int execute (function *);
  2191. }; // class pass_strlen
  2192. unsigned int
  2193. pass_strlen::execute (function *fun)
  2194. {
  2195. ssa_ver_to_stridx.safe_grow_cleared (num_ssa_names);
  2196. max_stridx = 1;
  2197. strinfo_pool = create_alloc_pool ("strinfo_struct pool",
  2198. sizeof (struct strinfo_struct), 64);
  2199. calculate_dominance_info (CDI_DOMINATORS);
  2200. /* String length optimization is implemented as a walk of the dominator
  2201. tree and a forward walk of statements within each block. */
  2202. strlen_dom_walker (CDI_DOMINATORS).walk (fun->cfg->x_entry_block_ptr);
  2203. ssa_ver_to_stridx.release ();
  2204. free_alloc_pool (strinfo_pool);
  2205. if (decl_to_stridxlist_htab)
  2206. {
  2207. obstack_free (&stridx_obstack, NULL);
  2208. delete decl_to_stridxlist_htab;
  2209. decl_to_stridxlist_htab = NULL;
  2210. }
  2211. laststmt.stmt = NULL;
  2212. laststmt.len = NULL_TREE;
  2213. laststmt.stridx = 0;
  2214. return 0;
  2215. }
  2216. } // anon namespace
  2217. gimple_opt_pass *
  2218. make_pass_strlen (gcc::context *ctxt)
  2219. {
  2220. return new pass_strlen (ctxt);
  2221. }