value-prof.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155
  1. /* Transformations based on profile information for values.
  2. Copyright (C) 2003-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include "config.h"
  16. #include "system.h"
  17. #include "coretypes.h"
  18. #include "tm.h"
  19. #include "hash-set.h"
  20. #include "machmode.h"
  21. #include "vec.h"
  22. #include "double-int.h"
  23. #include "input.h"
  24. #include "alias.h"
  25. #include "symtab.h"
  26. #include "wide-int.h"
  27. #include "inchash.h"
  28. #include "tree.h"
  29. #include "fold-const.h"
  30. #include "tree-nested.h"
  31. #include "calls.h"
  32. #include "rtl.h"
  33. #include "hashtab.h"
  34. #include "hard-reg-set.h"
  35. #include "function.h"
  36. #include "flags.h"
  37. #include "statistics.h"
  38. #include "real.h"
  39. #include "fixed-value.h"
  40. #include "insn-config.h"
  41. #include "expmed.h"
  42. #include "dojump.h"
  43. #include "explow.h"
  44. #include "emit-rtl.h"
  45. #include "varasm.h"
  46. #include "stmt.h"
  47. #include "expr.h"
  48. #include "predict.h"
  49. #include "dominance.h"
  50. #include "cfg.h"
  51. #include "basic-block.h"
  52. #include "value-prof.h"
  53. #include "recog.h"
  54. #include "insn-codes.h"
  55. #include "optabs.h"
  56. #include "regs.h"
  57. #include "tree-ssa-alias.h"
  58. #include "internal-fn.h"
  59. #include "tree-eh.h"
  60. #include "gimple-expr.h"
  61. #include "is-a.h"
  62. #include "gimple.h"
  63. #include "gimplify.h"
  64. #include "gimple-iterator.h"
  65. #include "gimple-ssa.h"
  66. #include "tree-cfg.h"
  67. #include "tree-phinodes.h"
  68. #include "ssa-iterators.h"
  69. #include "stringpool.h"
  70. #include "tree-ssanames.h"
  71. #include "diagnostic.h"
  72. #include "gimple-pretty-print.h"
  73. #include "coverage.h"
  74. #include "gcov-io.h"
  75. #include "timevar.h"
  76. #include "dumpfile.h"
  77. #include "profile.h"
  78. #include "hash-map.h"
  79. #include "plugin-api.h"
  80. #include "ipa-ref.h"
  81. #include "cgraph.h"
  82. #include "data-streamer.h"
  83. #include "builtins.h"
  84. #include "params.h"
  85. #include "tree-chkp.h"
  86. /* In this file value profile based optimizations are placed. Currently the
  87. following optimizations are implemented (for more detailed descriptions
  88. see comments at value_profile_transformations):
  89. 1) Division/modulo specialization. Provided that we can determine that the
  90. operands of the division have some special properties, we may use it to
  91. produce more effective code.
  92. 2) Indirect/virtual call specialization. If we can determine most
  93. common function callee in indirect/virtual call. We can use this
  94. information to improve code effectiveness (especially info for
  95. the inliner).
  96. 3) Speculative prefetching. If we are able to determine that the difference
  97. between addresses accessed by a memory reference is usually constant, we
  98. may add the prefetch instructions.
  99. FIXME: This transformation was removed together with RTL based value
  100. profiling.
  101. Value profiling internals
  102. ==========================
  103. Every value profiling transformation starts with defining what values
  104. to profile. There are different histogram types (see HIST_TYPE_* in
  105. value-prof.h) and each transformation can request one or more histogram
  106. types per GIMPLE statement. The function gimple_find_values_to_profile()
  107. collects the values to profile in a vec, and adds the number of counters
  108. required for the different histogram types.
  109. For a -fprofile-generate run, the statements for which values should be
  110. recorded, are instrumented in instrument_values(). The instrumentation
  111. is done by helper functions that can be found in tree-profile.c, where
  112. new types of histograms can be added if necessary.
  113. After a -fprofile-use, the value profiling data is read back in by
  114. compute_value_histograms() that translates the collected data to
  115. histograms and attaches them to the profiled statements via
  116. gimple_add_histogram_value(). Histograms are stored in a hash table
  117. that is attached to every intrumented function, see VALUE_HISTOGRAMS
  118. in function.h.
  119. The value-profile transformations driver is the function
  120. gimple_value_profile_transformations(). It traverses all statements in
  121. the to-be-transformed function, and looks for statements with one or
  122. more histograms attached to it. If a statement has histograms, the
  123. transformation functions are called on the statement.
  124. Limitations / FIXME / TODO:
  125. * Only one histogram of each type can be associated with a statement.
  126. * Currently, HIST_TYPE_CONST_DELTA is not implemented.
  127. (This type of histogram was originally used to implement a form of
  128. stride profiling based speculative prefetching to improve SPEC2000
  129. scores for memory-bound benchmarks, mcf and equake. However, this
  130. was an RTL value-profiling transformation, and those have all been
  131. removed.)
  132. * Some value profile transformations are done in builtins.c (?!)
  133. * Updating of histograms needs some TLC.
  134. * The value profiling code could be used to record analysis results
  135. from non-profiling (e.g. VRP).
  136. * Adding new profilers should be simplified, starting with a cleanup
  137. of what-happens-where andwith making gimple_find_values_to_profile
  138. and gimple_value_profile_transformations table-driven, perhaps...
  139. */
  140. static tree gimple_divmod_fixed_value (gassign *, tree, int, gcov_type,
  141. gcov_type);
  142. static tree gimple_mod_pow2 (gassign *, int, gcov_type, gcov_type);
  143. static tree gimple_mod_subtract (gassign *, int, int, int, gcov_type,
  144. gcov_type, gcov_type);
  145. static bool gimple_divmod_fixed_value_transform (gimple_stmt_iterator *);
  146. static bool gimple_mod_pow2_value_transform (gimple_stmt_iterator *);
  147. static bool gimple_mod_subtract_transform (gimple_stmt_iterator *);
  148. static bool gimple_stringops_transform (gimple_stmt_iterator *);
  149. static bool gimple_ic_transform (gimple_stmt_iterator *);
  150. /* Allocate histogram value. */
  151. histogram_value
  152. gimple_alloc_histogram_value (struct function *fun ATTRIBUTE_UNUSED,
  153. enum hist_type type, gimple stmt, tree value)
  154. {
  155. histogram_value hist = (histogram_value) xcalloc (1, sizeof (*hist));
  156. hist->hvalue.value = value;
  157. hist->hvalue.stmt = stmt;
  158. hist->type = type;
  159. return hist;
  160. }
  161. /* Hash value for histogram. */
  162. static hashval_t
  163. histogram_hash (const void *x)
  164. {
  165. return htab_hash_pointer (((const_histogram_value)x)->hvalue.stmt);
  166. }
  167. /* Return nonzero if statement for histogram_value X is Y. */
  168. static int
  169. histogram_eq (const void *x, const void *y)
  170. {
  171. return ((const_histogram_value) x)->hvalue.stmt == (const_gimple) y;
  172. }
  173. /* Set histogram for STMT. */
  174. static void
  175. set_histogram_value (struct function *fun, gimple stmt, histogram_value hist)
  176. {
  177. void **loc;
  178. if (!hist && !VALUE_HISTOGRAMS (fun))
  179. return;
  180. if (!VALUE_HISTOGRAMS (fun))
  181. VALUE_HISTOGRAMS (fun) = htab_create (1, histogram_hash,
  182. histogram_eq, NULL);
  183. loc = htab_find_slot_with_hash (VALUE_HISTOGRAMS (fun), stmt,
  184. htab_hash_pointer (stmt),
  185. hist ? INSERT : NO_INSERT);
  186. if (!hist)
  187. {
  188. if (loc)
  189. htab_clear_slot (VALUE_HISTOGRAMS (fun), loc);
  190. return;
  191. }
  192. *loc = hist;
  193. }
  194. /* Get histogram list for STMT. */
  195. histogram_value
  196. gimple_histogram_value (struct function *fun, gimple stmt)
  197. {
  198. if (!VALUE_HISTOGRAMS (fun))
  199. return NULL;
  200. return (histogram_value) htab_find_with_hash (VALUE_HISTOGRAMS (fun), stmt,
  201. htab_hash_pointer (stmt));
  202. }
  203. /* Add histogram for STMT. */
  204. void
  205. gimple_add_histogram_value (struct function *fun, gimple stmt,
  206. histogram_value hist)
  207. {
  208. hist->hvalue.next = gimple_histogram_value (fun, stmt);
  209. set_histogram_value (fun, stmt, hist);
  210. hist->fun = fun;
  211. }
  212. /* Remove histogram HIST from STMT's histogram list. */
  213. void
  214. gimple_remove_histogram_value (struct function *fun, gimple stmt,
  215. histogram_value hist)
  216. {
  217. histogram_value hist2 = gimple_histogram_value (fun, stmt);
  218. if (hist == hist2)
  219. {
  220. set_histogram_value (fun, stmt, hist->hvalue.next);
  221. }
  222. else
  223. {
  224. while (hist2->hvalue.next != hist)
  225. hist2 = hist2->hvalue.next;
  226. hist2->hvalue.next = hist->hvalue.next;
  227. }
  228. free (hist->hvalue.counters);
  229. #ifdef ENABLE_CHECKING
  230. memset (hist, 0xab, sizeof (*hist));
  231. #endif
  232. free (hist);
  233. }
  234. /* Lookup histogram of type TYPE in the STMT. */
  235. histogram_value
  236. gimple_histogram_value_of_type (struct function *fun, gimple stmt,
  237. enum hist_type type)
  238. {
  239. histogram_value hist;
  240. for (hist = gimple_histogram_value (fun, stmt); hist;
  241. hist = hist->hvalue.next)
  242. if (hist->type == type)
  243. return hist;
  244. return NULL;
  245. }
  246. /* Dump information about HIST to DUMP_FILE. */
  247. static void
  248. dump_histogram_value (FILE *dump_file, histogram_value hist)
  249. {
  250. switch (hist->type)
  251. {
  252. case HIST_TYPE_INTERVAL:
  253. fprintf (dump_file, "Interval counter range %d -- %d",
  254. hist->hdata.intvl.int_start,
  255. (hist->hdata.intvl.int_start
  256. + hist->hdata.intvl.steps - 1));
  257. if (hist->hvalue.counters)
  258. {
  259. unsigned int i;
  260. fprintf (dump_file, " [");
  261. for (i = 0; i < hist->hdata.intvl.steps; i++)
  262. fprintf (dump_file, " %d:%"PRId64,
  263. hist->hdata.intvl.int_start + i,
  264. (int64_t) hist->hvalue.counters[i]);
  265. fprintf (dump_file, " ] outside range:%"PRId64,
  266. (int64_t) hist->hvalue.counters[i]);
  267. }
  268. fprintf (dump_file, ".\n");
  269. break;
  270. case HIST_TYPE_POW2:
  271. fprintf (dump_file, "Pow2 counter ");
  272. if (hist->hvalue.counters)
  273. {
  274. fprintf (dump_file, "pow2:%"PRId64
  275. " nonpow2:%"PRId64,
  276. (int64_t) hist->hvalue.counters[0],
  277. (int64_t) hist->hvalue.counters[1]);
  278. }
  279. fprintf (dump_file, ".\n");
  280. break;
  281. case HIST_TYPE_SINGLE_VALUE:
  282. fprintf (dump_file, "Single value ");
  283. if (hist->hvalue.counters)
  284. {
  285. fprintf (dump_file, "value:%"PRId64
  286. " match:%"PRId64
  287. " wrong:%"PRId64,
  288. (int64_t) hist->hvalue.counters[0],
  289. (int64_t) hist->hvalue.counters[1],
  290. (int64_t) hist->hvalue.counters[2]);
  291. }
  292. fprintf (dump_file, ".\n");
  293. break;
  294. case HIST_TYPE_AVERAGE:
  295. fprintf (dump_file, "Average value ");
  296. if (hist->hvalue.counters)
  297. {
  298. fprintf (dump_file, "sum:%"PRId64
  299. " times:%"PRId64,
  300. (int64_t) hist->hvalue.counters[0],
  301. (int64_t) hist->hvalue.counters[1]);
  302. }
  303. fprintf (dump_file, ".\n");
  304. break;
  305. case HIST_TYPE_IOR:
  306. fprintf (dump_file, "IOR value ");
  307. if (hist->hvalue.counters)
  308. {
  309. fprintf (dump_file, "ior:%"PRId64,
  310. (int64_t) hist->hvalue.counters[0]);
  311. }
  312. fprintf (dump_file, ".\n");
  313. break;
  314. case HIST_TYPE_CONST_DELTA:
  315. fprintf (dump_file, "Constant delta ");
  316. if (hist->hvalue.counters)
  317. {
  318. fprintf (dump_file, "value:%"PRId64
  319. " match:%"PRId64
  320. " wrong:%"PRId64,
  321. (int64_t) hist->hvalue.counters[0],
  322. (int64_t) hist->hvalue.counters[1],
  323. (int64_t) hist->hvalue.counters[2]);
  324. }
  325. fprintf (dump_file, ".\n");
  326. break;
  327. case HIST_TYPE_INDIR_CALL:
  328. fprintf (dump_file, "Indirect call ");
  329. if (hist->hvalue.counters)
  330. {
  331. fprintf (dump_file, "value:%"PRId64
  332. " match:%"PRId64
  333. " all:%"PRId64,
  334. (int64_t) hist->hvalue.counters[0],
  335. (int64_t) hist->hvalue.counters[1],
  336. (int64_t) hist->hvalue.counters[2]);
  337. }
  338. fprintf (dump_file, ".\n");
  339. break;
  340. case HIST_TYPE_TIME_PROFILE:
  341. fprintf (dump_file, "Time profile ");
  342. if (hist->hvalue.counters)
  343. {
  344. fprintf (dump_file, "time:%"PRId64,
  345. (int64_t) hist->hvalue.counters[0]);
  346. }
  347. fprintf (dump_file, ".\n");
  348. break;
  349. case HIST_TYPE_INDIR_CALL_TOPN:
  350. fprintf (dump_file, "Indirect call topn ");
  351. if (hist->hvalue.counters)
  352. {
  353. int i;
  354. fprintf (dump_file, "accu:%"PRId64, hist->hvalue.counters[0]);
  355. for (i = 1; i < (GCOV_ICALL_TOPN_VAL << 2); i += 2)
  356. {
  357. fprintf (dump_file, " target:%"PRId64 " value:%"PRId64,
  358. (int64_t) hist->hvalue.counters[i],
  359. (int64_t) hist->hvalue.counters[i+1]);
  360. }
  361. }
  362. fprintf (dump_file, ".\n");
  363. break;
  364. case HIST_TYPE_MAX:
  365. gcc_unreachable ();
  366. }
  367. }
  368. /* Dump information about HIST to DUMP_FILE. */
  369. void
  370. stream_out_histogram_value (struct output_block *ob, histogram_value hist)
  371. {
  372. struct bitpack_d bp;
  373. unsigned int i;
  374. bp = bitpack_create (ob->main_stream);
  375. bp_pack_enum (&bp, hist_type, HIST_TYPE_MAX, hist->type);
  376. bp_pack_value (&bp, hist->hvalue.next != NULL, 1);
  377. streamer_write_bitpack (&bp);
  378. switch (hist->type)
  379. {
  380. case HIST_TYPE_INTERVAL:
  381. streamer_write_hwi (ob, hist->hdata.intvl.int_start);
  382. streamer_write_uhwi (ob, hist->hdata.intvl.steps);
  383. break;
  384. default:
  385. break;
  386. }
  387. for (i = 0; i < hist->n_counters; i++)
  388. streamer_write_gcov_count (ob, hist->hvalue.counters[i]);
  389. if (hist->hvalue.next)
  390. stream_out_histogram_value (ob, hist->hvalue.next);
  391. }
  392. /* Dump information about HIST to DUMP_FILE. */
  393. void
  394. stream_in_histogram_value (struct lto_input_block *ib, gimple stmt)
  395. {
  396. enum hist_type type;
  397. unsigned int ncounters = 0;
  398. struct bitpack_d bp;
  399. unsigned int i;
  400. histogram_value new_val;
  401. bool next;
  402. histogram_value *next_p = NULL;
  403. do
  404. {
  405. bp = streamer_read_bitpack (ib);
  406. type = bp_unpack_enum (&bp, hist_type, HIST_TYPE_MAX);
  407. next = bp_unpack_value (&bp, 1);
  408. new_val = gimple_alloc_histogram_value (cfun, type, stmt, NULL);
  409. switch (type)
  410. {
  411. case HIST_TYPE_INTERVAL:
  412. new_val->hdata.intvl.int_start = streamer_read_hwi (ib);
  413. new_val->hdata.intvl.steps = streamer_read_uhwi (ib);
  414. ncounters = new_val->hdata.intvl.steps + 2;
  415. break;
  416. case HIST_TYPE_POW2:
  417. case HIST_TYPE_AVERAGE:
  418. ncounters = 2;
  419. break;
  420. case HIST_TYPE_SINGLE_VALUE:
  421. case HIST_TYPE_INDIR_CALL:
  422. ncounters = 3;
  423. break;
  424. case HIST_TYPE_CONST_DELTA:
  425. ncounters = 4;
  426. break;
  427. case HIST_TYPE_IOR:
  428. case HIST_TYPE_TIME_PROFILE:
  429. ncounters = 1;
  430. break;
  431. case HIST_TYPE_INDIR_CALL_TOPN:
  432. ncounters = (GCOV_ICALL_TOPN_VAL << 2) + 1;
  433. break;
  434. case HIST_TYPE_MAX:
  435. gcc_unreachable ();
  436. }
  437. new_val->hvalue.counters = XNEWVAR (gcov_type, sizeof (*new_val->hvalue.counters) * ncounters);
  438. new_val->n_counters = ncounters;
  439. for (i = 0; i < ncounters; i++)
  440. new_val->hvalue.counters[i] = streamer_read_gcov_count (ib);
  441. if (!next_p)
  442. gimple_add_histogram_value (cfun, stmt, new_val);
  443. else
  444. *next_p = new_val;
  445. next_p = &new_val->hvalue.next;
  446. }
  447. while (next);
  448. }
  449. /* Dump all histograms attached to STMT to DUMP_FILE. */
  450. void
  451. dump_histograms_for_stmt (struct function *fun, FILE *dump_file, gimple stmt)
  452. {
  453. histogram_value hist;
  454. for (hist = gimple_histogram_value (fun, stmt); hist; hist = hist->hvalue.next)
  455. dump_histogram_value (dump_file, hist);
  456. }
  457. /* Remove all histograms associated with STMT. */
  458. void
  459. gimple_remove_stmt_histograms (struct function *fun, gimple stmt)
  460. {
  461. histogram_value val;
  462. while ((val = gimple_histogram_value (fun, stmt)) != NULL)
  463. gimple_remove_histogram_value (fun, stmt, val);
  464. }
  465. /* Duplicate all histograms associates with OSTMT to STMT. */
  466. void
  467. gimple_duplicate_stmt_histograms (struct function *fun, gimple stmt,
  468. struct function *ofun, gimple ostmt)
  469. {
  470. histogram_value val;
  471. for (val = gimple_histogram_value (ofun, ostmt); val != NULL; val = val->hvalue.next)
  472. {
  473. histogram_value new_val = gimple_alloc_histogram_value (fun, val->type, NULL, NULL);
  474. memcpy (new_val, val, sizeof (*val));
  475. new_val->hvalue.stmt = stmt;
  476. new_val->hvalue.counters = XNEWVAR (gcov_type, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
  477. memcpy (new_val->hvalue.counters, val->hvalue.counters, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
  478. gimple_add_histogram_value (fun, stmt, new_val);
  479. }
  480. }
  481. /* Move all histograms associated with OSTMT to STMT. */
  482. void
  483. gimple_move_stmt_histograms (struct function *fun, gimple stmt, gimple ostmt)
  484. {
  485. histogram_value val = gimple_histogram_value (fun, ostmt);
  486. if (val)
  487. {
  488. /* The following three statements can't be reordered,
  489. because histogram hashtab relies on stmt field value
  490. for finding the exact slot. */
  491. set_histogram_value (fun, ostmt, NULL);
  492. for (; val != NULL; val = val->hvalue.next)
  493. val->hvalue.stmt = stmt;
  494. set_histogram_value (fun, stmt, val);
  495. }
  496. }
  497. static bool error_found = false;
  498. /* Helper function for verify_histograms. For each histogram reachable via htab
  499. walk verify that it was reached via statement walk. */
  500. static int
  501. visit_hist (void **slot, void *data)
  502. {
  503. hash_set<histogram_value> *visited = (hash_set<histogram_value> *) data;
  504. histogram_value hist = *(histogram_value *) slot;
  505. if (!visited->contains (hist)
  506. && hist->type != HIST_TYPE_TIME_PROFILE)
  507. {
  508. error ("dead histogram");
  509. dump_histogram_value (stderr, hist);
  510. debug_gimple_stmt (hist->hvalue.stmt);
  511. error_found = true;
  512. }
  513. return 1;
  514. }
  515. /* Verify sanity of the histograms. */
  516. DEBUG_FUNCTION void
  517. verify_histograms (void)
  518. {
  519. basic_block bb;
  520. gimple_stmt_iterator gsi;
  521. histogram_value hist;
  522. error_found = false;
  523. hash_set<histogram_value> visited_hists;
  524. FOR_EACH_BB_FN (bb, cfun)
  525. for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  526. {
  527. gimple stmt = gsi_stmt (gsi);
  528. for (hist = gimple_histogram_value (cfun, stmt); hist;
  529. hist = hist->hvalue.next)
  530. {
  531. if (hist->hvalue.stmt != stmt)
  532. {
  533. error ("Histogram value statement does not correspond to "
  534. "the statement it is associated with");
  535. debug_gimple_stmt (stmt);
  536. dump_histogram_value (stderr, hist);
  537. error_found = true;
  538. }
  539. visited_hists.add (hist);
  540. }
  541. }
  542. if (VALUE_HISTOGRAMS (cfun))
  543. htab_traverse (VALUE_HISTOGRAMS (cfun), visit_hist, &visited_hists);
  544. if (error_found)
  545. internal_error ("verify_histograms failed");
  546. }
  547. /* Helper function for verify_histograms. For each histogram reachable via htab
  548. walk verify that it was reached via statement walk. */
  549. static int
  550. free_hist (void **slot, void *data ATTRIBUTE_UNUSED)
  551. {
  552. histogram_value hist = *(histogram_value *) slot;
  553. free (hist->hvalue.counters);
  554. #ifdef ENABLE_CHECKING
  555. memset (hist, 0xab, sizeof (*hist));
  556. #endif
  557. free (hist);
  558. return 1;
  559. }
  560. void
  561. free_histograms (void)
  562. {
  563. if (VALUE_HISTOGRAMS (cfun))
  564. {
  565. htab_traverse (VALUE_HISTOGRAMS (cfun), free_hist, NULL);
  566. htab_delete (VALUE_HISTOGRAMS (cfun));
  567. VALUE_HISTOGRAMS (cfun) = NULL;
  568. }
  569. }
  570. /* The overall number of invocations of the counter should match
  571. execution count of basic block. Report it as error rather than
  572. internal error as it might mean that user has misused the profile
  573. somehow. */
  574. static bool
  575. check_counter (gimple stmt, const char * name,
  576. gcov_type *count, gcov_type *all, gcov_type bb_count)
  577. {
  578. if (*all != bb_count || *count > *all)
  579. {
  580. location_t locus;
  581. locus = (stmt != NULL)
  582. ? gimple_location (stmt)
  583. : DECL_SOURCE_LOCATION (current_function_decl);
  584. if (flag_profile_correction)
  585. {
  586. if (dump_enabled_p ())
  587. dump_printf_loc (MSG_MISSED_OPTIMIZATION, locus,
  588. "correcting inconsistent value profile: %s "
  589. "profiler overall count (%d) does not match BB "
  590. "count (%d)\n", name, (int)*all, (int)bb_count);
  591. *all = bb_count;
  592. if (*count > *all)
  593. *count = *all;
  594. return false;
  595. }
  596. else
  597. {
  598. error_at (locus, "corrupted value profile: %s "
  599. "profile counter (%d out of %d) inconsistent with "
  600. "basic-block count (%d)",
  601. name,
  602. (int) *count,
  603. (int) *all,
  604. (int) bb_count);
  605. return true;
  606. }
  607. }
  608. return false;
  609. }
  610. /* GIMPLE based transformations. */
  611. bool
  612. gimple_value_profile_transformations (void)
  613. {
  614. basic_block bb;
  615. gimple_stmt_iterator gsi;
  616. bool changed = false;
  617. FOR_EACH_BB_FN (bb, cfun)
  618. {
  619. for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  620. {
  621. gimple stmt = gsi_stmt (gsi);
  622. histogram_value th = gimple_histogram_value (cfun, stmt);
  623. if (!th)
  624. continue;
  625. if (dump_file)
  626. {
  627. fprintf (dump_file, "Trying transformations on stmt ");
  628. print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
  629. dump_histograms_for_stmt (cfun, dump_file, stmt);
  630. }
  631. /* Transformations: */
  632. /* The order of things in this conditional controls which
  633. transformation is used when more than one is applicable. */
  634. /* It is expected that any code added by the transformations
  635. will be added before the current statement, and that the
  636. current statement remain valid (although possibly
  637. modified) upon return. */
  638. if (gimple_mod_subtract_transform (&gsi)
  639. || gimple_divmod_fixed_value_transform (&gsi)
  640. || gimple_mod_pow2_value_transform (&gsi)
  641. || gimple_stringops_transform (&gsi)
  642. || gimple_ic_transform (&gsi))
  643. {
  644. stmt = gsi_stmt (gsi);
  645. changed = true;
  646. /* Original statement may no longer be in the same block. */
  647. if (bb != gimple_bb (stmt))
  648. {
  649. bb = gimple_bb (stmt);
  650. gsi = gsi_for_stmt (stmt);
  651. }
  652. }
  653. }
  654. }
  655. if (changed)
  656. {
  657. counts_to_freqs ();
  658. }
  659. return changed;
  660. }
  661. /* Generate code for transformation 1 (with parent gimple assignment
  662. STMT and probability of taking the optimal path PROB, which is
  663. equivalent to COUNT/ALL within roundoff error). This generates the
  664. result into a temp and returns the temp; it does not replace or
  665. alter the original STMT. */
  666. static tree
  667. gimple_divmod_fixed_value (gassign *stmt, tree value, int prob,
  668. gcov_type count, gcov_type all)
  669. {
  670. gassign *stmt1, *stmt2;
  671. gcond *stmt3;
  672. tree tmp0, tmp1, tmp2;
  673. gimple bb1end, bb2end, bb3end;
  674. basic_block bb, bb2, bb3, bb4;
  675. tree optype, op1, op2;
  676. edge e12, e13, e23, e24, e34;
  677. gimple_stmt_iterator gsi;
  678. gcc_assert (is_gimple_assign (stmt)
  679. && (gimple_assign_rhs_code (stmt) == TRUNC_DIV_EXPR
  680. || gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR));
  681. optype = TREE_TYPE (gimple_assign_lhs (stmt));
  682. op1 = gimple_assign_rhs1 (stmt);
  683. op2 = gimple_assign_rhs2 (stmt);
  684. bb = gimple_bb (stmt);
  685. gsi = gsi_for_stmt (stmt);
  686. tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
  687. tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
  688. stmt1 = gimple_build_assign (tmp0, fold_convert (optype, value));
  689. stmt2 = gimple_build_assign (tmp1, op2);
  690. stmt3 = gimple_build_cond (NE_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
  691. gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
  692. gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
  693. gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
  694. bb1end = stmt3;
  695. tmp2 = create_tmp_reg (optype, "PROF");
  696. stmt1 = gimple_build_assign (tmp2, gimple_assign_rhs_code (stmt), op1, tmp0);
  697. gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
  698. bb2end = stmt1;
  699. stmt1 = gimple_build_assign (tmp2, gimple_assign_rhs_code (stmt), op1, op2);
  700. gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
  701. bb3end = stmt1;
  702. /* Fix CFG. */
  703. /* Edge e23 connects bb2 to bb3, etc. */
  704. e12 = split_block (bb, bb1end);
  705. bb2 = e12->dest;
  706. bb2->count = count;
  707. e23 = split_block (bb2, bb2end);
  708. bb3 = e23->dest;
  709. bb3->count = all - count;
  710. e34 = split_block (bb3, bb3end);
  711. bb4 = e34->dest;
  712. bb4->count = all;
  713. e12->flags &= ~EDGE_FALLTHRU;
  714. e12->flags |= EDGE_FALSE_VALUE;
  715. e12->probability = prob;
  716. e12->count = count;
  717. e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
  718. e13->probability = REG_BR_PROB_BASE - prob;
  719. e13->count = all - count;
  720. remove_edge (e23);
  721. e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
  722. e24->probability = REG_BR_PROB_BASE;
  723. e24->count = count;
  724. e34->probability = REG_BR_PROB_BASE;
  725. e34->count = all - count;
  726. return tmp2;
  727. }
  728. /* Do transform 1) on INSN if applicable. */
  729. static bool
  730. gimple_divmod_fixed_value_transform (gimple_stmt_iterator *si)
  731. {
  732. histogram_value histogram;
  733. enum tree_code code;
  734. gcov_type val, count, all;
  735. tree result, value, tree_val;
  736. gcov_type prob;
  737. gassign *stmt;
  738. stmt = dyn_cast <gassign *> (gsi_stmt (*si));
  739. if (!stmt)
  740. return false;
  741. if (!INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_lhs (stmt))))
  742. return false;
  743. code = gimple_assign_rhs_code (stmt);
  744. if (code != TRUNC_DIV_EXPR && code != TRUNC_MOD_EXPR)
  745. return false;
  746. histogram = gimple_histogram_value_of_type (cfun, stmt,
  747. HIST_TYPE_SINGLE_VALUE);
  748. if (!histogram)
  749. return false;
  750. value = histogram->hvalue.value;
  751. val = histogram->hvalue.counters[0];
  752. count = histogram->hvalue.counters[1];
  753. all = histogram->hvalue.counters[2];
  754. gimple_remove_histogram_value (cfun, stmt, histogram);
  755. /* We require that count is at least half of all; this means
  756. that for the transformation to fire the value must be constant
  757. at least 50% of time (and 75% gives the guarantee of usage). */
  758. if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
  759. || 2 * count < all
  760. || optimize_bb_for_size_p (gimple_bb (stmt)))
  761. return false;
  762. if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
  763. return false;
  764. /* Compute probability of taking the optimal path. */
  765. if (all > 0)
  766. prob = GCOV_COMPUTE_SCALE (count, all);
  767. else
  768. prob = 0;
  769. if (sizeof (gcov_type) == sizeof (HOST_WIDE_INT))
  770. tree_val = build_int_cst (get_gcov_type (), val);
  771. else
  772. {
  773. HOST_WIDE_INT a[2];
  774. a[0] = (unsigned HOST_WIDE_INT) val;
  775. a[1] = val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1;
  776. tree_val = wide_int_to_tree (get_gcov_type (), wide_int::from_array (a, 2,
  777. TYPE_PRECISION (get_gcov_type ()), false));
  778. }
  779. result = gimple_divmod_fixed_value (stmt, tree_val, prob, count, all);
  780. if (dump_file)
  781. {
  782. fprintf (dump_file, "Div/mod by constant ");
  783. print_generic_expr (dump_file, value, TDF_SLIM);
  784. fprintf (dump_file, "=");
  785. print_generic_expr (dump_file, tree_val, TDF_SLIM);
  786. fprintf (dump_file, " transformation on insn ");
  787. print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
  788. }
  789. gimple_assign_set_rhs_from_tree (si, result);
  790. update_stmt (gsi_stmt (*si));
  791. return true;
  792. }
  793. /* Generate code for transformation 2 (with parent gimple assign STMT and
  794. probability of taking the optimal path PROB, which is equivalent to COUNT/ALL
  795. within roundoff error). This generates the result into a temp and returns
  796. the temp; it does not replace or alter the original STMT. */
  797. static tree
  798. gimple_mod_pow2 (gassign *stmt, int prob, gcov_type count, gcov_type all)
  799. {
  800. gassign *stmt1, *stmt2, *stmt3;
  801. gcond *stmt4;
  802. tree tmp2, tmp3;
  803. gimple bb1end, bb2end, bb3end;
  804. basic_block bb, bb2, bb3, bb4;
  805. tree optype, op1, op2;
  806. edge e12, e13, e23, e24, e34;
  807. gimple_stmt_iterator gsi;
  808. tree result;
  809. gcc_assert (is_gimple_assign (stmt)
  810. && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
  811. optype = TREE_TYPE (gimple_assign_lhs (stmt));
  812. op1 = gimple_assign_rhs1 (stmt);
  813. op2 = gimple_assign_rhs2 (stmt);
  814. bb = gimple_bb (stmt);
  815. gsi = gsi_for_stmt (stmt);
  816. result = create_tmp_reg (optype, "PROF");
  817. tmp2 = make_temp_ssa_name (optype, NULL, "PROF");
  818. tmp3 = make_temp_ssa_name (optype, NULL, "PROF");
  819. stmt2 = gimple_build_assign (tmp2, PLUS_EXPR, op2,
  820. build_int_cst (optype, -1));
  821. stmt3 = gimple_build_assign (tmp3, BIT_AND_EXPR, tmp2, op2);
  822. stmt4 = gimple_build_cond (NE_EXPR, tmp3, build_int_cst (optype, 0),
  823. NULL_TREE, NULL_TREE);
  824. gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
  825. gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
  826. gsi_insert_before (&gsi, stmt4, GSI_SAME_STMT);
  827. bb1end = stmt4;
  828. /* tmp2 == op2-1 inherited from previous block. */
  829. stmt1 = gimple_build_assign (result, BIT_AND_EXPR, op1, tmp2);
  830. gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
  831. bb2end = stmt1;
  832. stmt1 = gimple_build_assign (result, gimple_assign_rhs_code (stmt),
  833. op1, op2);
  834. gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
  835. bb3end = stmt1;
  836. /* Fix CFG. */
  837. /* Edge e23 connects bb2 to bb3, etc. */
  838. e12 = split_block (bb, bb1end);
  839. bb2 = e12->dest;
  840. bb2->count = count;
  841. e23 = split_block (bb2, bb2end);
  842. bb3 = e23->dest;
  843. bb3->count = all - count;
  844. e34 = split_block (bb3, bb3end);
  845. bb4 = e34->dest;
  846. bb4->count = all;
  847. e12->flags &= ~EDGE_FALLTHRU;
  848. e12->flags |= EDGE_FALSE_VALUE;
  849. e12->probability = prob;
  850. e12->count = count;
  851. e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
  852. e13->probability = REG_BR_PROB_BASE - prob;
  853. e13->count = all - count;
  854. remove_edge (e23);
  855. e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
  856. e24->probability = REG_BR_PROB_BASE;
  857. e24->count = count;
  858. e34->probability = REG_BR_PROB_BASE;
  859. e34->count = all - count;
  860. return result;
  861. }
  862. /* Do transform 2) on INSN if applicable. */
  863. static bool
  864. gimple_mod_pow2_value_transform (gimple_stmt_iterator *si)
  865. {
  866. histogram_value histogram;
  867. enum tree_code code;
  868. gcov_type count, wrong_values, all;
  869. tree lhs_type, result, value;
  870. gcov_type prob;
  871. gassign *stmt;
  872. stmt = dyn_cast <gassign *> (gsi_stmt (*si));
  873. if (!stmt)
  874. return false;
  875. lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
  876. if (!INTEGRAL_TYPE_P (lhs_type))
  877. return false;
  878. code = gimple_assign_rhs_code (stmt);
  879. if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
  880. return false;
  881. histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_POW2);
  882. if (!histogram)
  883. return false;
  884. value = histogram->hvalue.value;
  885. wrong_values = histogram->hvalue.counters[0];
  886. count = histogram->hvalue.counters[1];
  887. gimple_remove_histogram_value (cfun, stmt, histogram);
  888. /* We require that we hit a power of 2 at least half of all evaluations. */
  889. if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
  890. || count < wrong_values
  891. || optimize_bb_for_size_p (gimple_bb (stmt)))
  892. return false;
  893. if (dump_file)
  894. {
  895. fprintf (dump_file, "Mod power of 2 transformation on insn ");
  896. print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
  897. }
  898. /* Compute probability of taking the optimal path. */
  899. all = count + wrong_values;
  900. if (check_counter (stmt, "pow2", &count, &all, gimple_bb (stmt)->count))
  901. return false;
  902. if (all > 0)
  903. prob = GCOV_COMPUTE_SCALE (count, all);
  904. else
  905. prob = 0;
  906. result = gimple_mod_pow2 (stmt, prob, count, all);
  907. gimple_assign_set_rhs_from_tree (si, result);
  908. update_stmt (gsi_stmt (*si));
  909. return true;
  910. }
  911. /* Generate code for transformations 3 and 4 (with parent gimple assign STMT, and
  912. NCOUNTS the number of cases to support. Currently only NCOUNTS==0 or 1 is
  913. supported and this is built into this interface. The probabilities of taking
  914. the optimal paths are PROB1 and PROB2, which are equivalent to COUNT1/ALL and
  915. COUNT2/ALL respectively within roundoff error). This generates the
  916. result into a temp and returns the temp; it does not replace or alter
  917. the original STMT. */
  918. /* FIXME: Generalize the interface to handle NCOUNTS > 1. */
  919. static tree
  920. gimple_mod_subtract (gassign *stmt, int prob1, int prob2, int ncounts,
  921. gcov_type count1, gcov_type count2, gcov_type all)
  922. {
  923. gassign *stmt1;
  924. gimple stmt2;
  925. gcond *stmt3;
  926. tree tmp1;
  927. gimple bb1end, bb2end = NULL, bb3end;
  928. basic_block bb, bb2, bb3, bb4;
  929. tree optype, op1, op2;
  930. edge e12, e23 = 0, e24, e34, e14;
  931. gimple_stmt_iterator gsi;
  932. tree result;
  933. gcc_assert (is_gimple_assign (stmt)
  934. && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
  935. optype = TREE_TYPE (gimple_assign_lhs (stmt));
  936. op1 = gimple_assign_rhs1 (stmt);
  937. op2 = gimple_assign_rhs2 (stmt);
  938. bb = gimple_bb (stmt);
  939. gsi = gsi_for_stmt (stmt);
  940. result = create_tmp_reg (optype, "PROF");
  941. tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
  942. stmt1 = gimple_build_assign (result, op1);
  943. stmt2 = gimple_build_assign (tmp1, op2);
  944. stmt3 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
  945. gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
  946. gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
  947. gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
  948. bb1end = stmt3;
  949. if (ncounts) /* Assumed to be 0 or 1 */
  950. {
  951. stmt1 = gimple_build_assign (result, MINUS_EXPR, result, tmp1);
  952. stmt2 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
  953. gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
  954. gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
  955. bb2end = stmt2;
  956. }
  957. /* Fallback case. */
  958. stmt1 = gimple_build_assign (result, gimple_assign_rhs_code (stmt),
  959. result, tmp1);
  960. gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
  961. bb3end = stmt1;
  962. /* Fix CFG. */
  963. /* Edge e23 connects bb2 to bb3, etc. */
  964. /* However block 3 is optional; if it is not there, references
  965. to 3 really refer to block 2. */
  966. e12 = split_block (bb, bb1end);
  967. bb2 = e12->dest;
  968. bb2->count = all - count1;
  969. if (ncounts) /* Assumed to be 0 or 1. */
  970. {
  971. e23 = split_block (bb2, bb2end);
  972. bb3 = e23->dest;
  973. bb3->count = all - count1 - count2;
  974. }
  975. e34 = split_block (ncounts ? bb3 : bb2, bb3end);
  976. bb4 = e34->dest;
  977. bb4->count = all;
  978. e12->flags &= ~EDGE_FALLTHRU;
  979. e12->flags |= EDGE_FALSE_VALUE;
  980. e12->probability = REG_BR_PROB_BASE - prob1;
  981. e12->count = all - count1;
  982. e14 = make_edge (bb, bb4, EDGE_TRUE_VALUE);
  983. e14->probability = prob1;
  984. e14->count = count1;
  985. if (ncounts) /* Assumed to be 0 or 1. */
  986. {
  987. e23->flags &= ~EDGE_FALLTHRU;
  988. e23->flags |= EDGE_FALSE_VALUE;
  989. e23->count = all - count1 - count2;
  990. e23->probability = REG_BR_PROB_BASE - prob2;
  991. e24 = make_edge (bb2, bb4, EDGE_TRUE_VALUE);
  992. e24->probability = prob2;
  993. e24->count = count2;
  994. }
  995. e34->probability = REG_BR_PROB_BASE;
  996. e34->count = all - count1 - count2;
  997. return result;
  998. }
  999. /* Do transforms 3) and 4) on the statement pointed-to by SI if applicable. */
  1000. static bool
  1001. gimple_mod_subtract_transform (gimple_stmt_iterator *si)
  1002. {
  1003. histogram_value histogram;
  1004. enum tree_code code;
  1005. gcov_type count, wrong_values, all;
  1006. tree lhs_type, result;
  1007. gcov_type prob1, prob2;
  1008. unsigned int i, steps;
  1009. gcov_type count1, count2;
  1010. gassign *stmt;
  1011. stmt = dyn_cast <gassign *> (gsi_stmt (*si));
  1012. if (!stmt)
  1013. return false;
  1014. lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
  1015. if (!INTEGRAL_TYPE_P (lhs_type))
  1016. return false;
  1017. code = gimple_assign_rhs_code (stmt);
  1018. if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
  1019. return false;
  1020. histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INTERVAL);
  1021. if (!histogram)
  1022. return false;
  1023. all = 0;
  1024. wrong_values = 0;
  1025. for (i = 0; i < histogram->hdata.intvl.steps; i++)
  1026. all += histogram->hvalue.counters[i];
  1027. wrong_values += histogram->hvalue.counters[i];
  1028. wrong_values += histogram->hvalue.counters[i+1];
  1029. steps = histogram->hdata.intvl.steps;
  1030. all += wrong_values;
  1031. count1 = histogram->hvalue.counters[0];
  1032. count2 = histogram->hvalue.counters[1];
  1033. /* Compute probability of taking the optimal path. */
  1034. if (check_counter (stmt, "interval", &count1, &all, gimple_bb (stmt)->count))
  1035. {
  1036. gimple_remove_histogram_value (cfun, stmt, histogram);
  1037. return false;
  1038. }
  1039. if (flag_profile_correction && count1 + count2 > all)
  1040. all = count1 + count2;
  1041. gcc_assert (count1 + count2 <= all);
  1042. /* We require that we use just subtractions in at least 50% of all
  1043. evaluations. */
  1044. count = 0;
  1045. for (i = 0; i < histogram->hdata.intvl.steps; i++)
  1046. {
  1047. count += histogram->hvalue.counters[i];
  1048. if (count * 2 >= all)
  1049. break;
  1050. }
  1051. if (i == steps
  1052. || optimize_bb_for_size_p (gimple_bb (stmt)))
  1053. return false;
  1054. gimple_remove_histogram_value (cfun, stmt, histogram);
  1055. if (dump_file)
  1056. {
  1057. fprintf (dump_file, "Mod subtract transformation on insn ");
  1058. print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
  1059. }
  1060. /* Compute probability of taking the optimal path(s). */
  1061. if (all > 0)
  1062. {
  1063. prob1 = GCOV_COMPUTE_SCALE (count1, all);
  1064. prob2 = GCOV_COMPUTE_SCALE (count2, all);
  1065. }
  1066. else
  1067. {
  1068. prob1 = prob2 = 0;
  1069. }
  1070. /* In practice, "steps" is always 2. This interface reflects this,
  1071. and will need to be changed if "steps" can change. */
  1072. result = gimple_mod_subtract (stmt, prob1, prob2, i, count1, count2, all);
  1073. gimple_assign_set_rhs_from_tree (si, result);
  1074. update_stmt (gsi_stmt (*si));
  1075. return true;
  1076. }
  1077. struct profile_id_traits : default_hashmap_traits
  1078. {
  1079. template<typename T>
  1080. static bool
  1081. is_deleted (T &e)
  1082. {
  1083. return e.m_key == UINT_MAX;
  1084. }
  1085. template<typename T> static bool is_empty (T &e) { return e.m_key == 0; }
  1086. template<typename T> static void mark_deleted (T &e) { e.m_key = UINT_MAX; }
  1087. template<typename T> static void mark_empty (T &e) { e.m_key = 0; }
  1088. };
  1089. static hash_map<unsigned int, cgraph_node *, profile_id_traits> *
  1090. cgraph_node_map = 0;
  1091. /* Returns true if node graph is initialized. This
  1092. is used to test if profile_id has been created
  1093. for cgraph_nodes. */
  1094. bool
  1095. coverage_node_map_initialized_p (void)
  1096. {
  1097. return cgraph_node_map != 0;
  1098. }
  1099. /* Initialize map from PROFILE_ID to CGRAPH_NODE.
  1100. When LOCAL is true, the PROFILE_IDs are computed. when it is false we assume
  1101. that the PROFILE_IDs was already assigned. */
  1102. void
  1103. init_node_map (bool local)
  1104. {
  1105. struct cgraph_node *n;
  1106. cgraph_node_map
  1107. = new hash_map<unsigned int, cgraph_node *, profile_id_traits>;
  1108. FOR_EACH_DEFINED_FUNCTION (n)
  1109. if (n->has_gimple_body_p ())
  1110. {
  1111. cgraph_node **val;
  1112. if (local)
  1113. {
  1114. n->profile_id = coverage_compute_profile_id (n);
  1115. while ((val = cgraph_node_map->get (n->profile_id))
  1116. || !n->profile_id)
  1117. {
  1118. if (dump_file)
  1119. fprintf (dump_file, "Local profile-id %i conflict"
  1120. " with nodes %s/%i %s/%i\n",
  1121. n->profile_id,
  1122. n->name (),
  1123. n->order,
  1124. (*val)->name (),
  1125. (*val)->order);
  1126. n->profile_id = (n->profile_id + 1) & 0x7fffffff;
  1127. }
  1128. }
  1129. else if (!n->profile_id)
  1130. {
  1131. if (dump_file)
  1132. fprintf (dump_file,
  1133. "Node %s/%i has no profile-id"
  1134. " (profile feedback missing?)\n",
  1135. n->name (),
  1136. n->order);
  1137. continue;
  1138. }
  1139. else if ((val = cgraph_node_map->get (n->profile_id)))
  1140. {
  1141. if (dump_file)
  1142. fprintf (dump_file,
  1143. "Node %s/%i has IP profile-id %i conflict. "
  1144. "Giving up.\n",
  1145. n->name (),
  1146. n->order,
  1147. n->profile_id);
  1148. *val = NULL;
  1149. continue;
  1150. }
  1151. cgraph_node_map->put (n->profile_id, n);
  1152. }
  1153. }
  1154. /* Delete the CGRAPH_NODE_MAP. */
  1155. void
  1156. del_node_map (void)
  1157. {
  1158. delete cgraph_node_map;
  1159. }
  1160. /* Return cgraph node for function with pid */
  1161. struct cgraph_node*
  1162. find_func_by_profile_id (int profile_id)
  1163. {
  1164. cgraph_node **val = cgraph_node_map->get (profile_id);
  1165. if (val)
  1166. return *val;
  1167. else
  1168. return NULL;
  1169. }
  1170. /* Perform sanity check on the indirect call target. Due to race conditions,
  1171. false function target may be attributed to an indirect call site. If the
  1172. call expression type mismatches with the target function's type, expand_call
  1173. may ICE. Here we only do very minimal sanity check just to make compiler happy.
  1174. Returns true if TARGET is considered ok for call CALL_STMT. */
  1175. bool
  1176. check_ic_target (gcall *call_stmt, struct cgraph_node *target)
  1177. {
  1178. location_t locus;
  1179. if (gimple_check_call_matching_types (call_stmt, target->decl, true))
  1180. return true;
  1181. locus = gimple_location (call_stmt);
  1182. if (dump_enabled_p ())
  1183. dump_printf_loc (MSG_MISSED_OPTIMIZATION, locus,
  1184. "Skipping target %s with mismatching types for icall\n",
  1185. target->name ());
  1186. return false;
  1187. }
  1188. /* Do transformation
  1189. if (actual_callee_address == address_of_most_common_function/method)
  1190. do direct call
  1191. else
  1192. old call
  1193. */
  1194. gcall *
  1195. gimple_ic (gcall *icall_stmt, struct cgraph_node *direct_call,
  1196. int prob, gcov_type count, gcov_type all)
  1197. {
  1198. gcall *dcall_stmt;
  1199. gassign *load_stmt;
  1200. gcond *cond_stmt;
  1201. gcall *iretbnd_stmt = NULL;
  1202. tree tmp0, tmp1, tmp;
  1203. basic_block cond_bb, dcall_bb, icall_bb, join_bb = NULL;
  1204. tree optype = build_pointer_type (void_type_node);
  1205. edge e_cd, e_ci, e_di, e_dj = NULL, e_ij;
  1206. gimple_stmt_iterator gsi;
  1207. int lp_nr, dflags;
  1208. edge e_eh, e;
  1209. edge_iterator ei;
  1210. gimple_stmt_iterator psi;
  1211. cond_bb = gimple_bb (icall_stmt);
  1212. gsi = gsi_for_stmt (icall_stmt);
  1213. if (gimple_call_with_bounds_p (icall_stmt) && gimple_call_lhs (icall_stmt))
  1214. iretbnd_stmt = chkp_retbnd_call_by_val (gimple_call_lhs (icall_stmt));
  1215. tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
  1216. tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
  1217. tmp = unshare_expr (gimple_call_fn (icall_stmt));
  1218. load_stmt = gimple_build_assign (tmp0, tmp);
  1219. gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
  1220. tmp = fold_convert (optype, build_addr (direct_call->decl,
  1221. current_function_decl));
  1222. load_stmt = gimple_build_assign (tmp1, tmp);
  1223. gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
  1224. cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
  1225. gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
  1226. gimple_set_vdef (icall_stmt, NULL_TREE);
  1227. gimple_set_vuse (icall_stmt, NULL_TREE);
  1228. update_stmt (icall_stmt);
  1229. dcall_stmt = as_a <gcall *> (gimple_copy (icall_stmt));
  1230. gimple_call_set_fndecl (dcall_stmt, direct_call->decl);
  1231. dflags = flags_from_decl_or_type (direct_call->decl);
  1232. if ((dflags & ECF_NORETURN) != 0)
  1233. gimple_call_set_lhs (dcall_stmt, NULL_TREE);
  1234. gsi_insert_before (&gsi, dcall_stmt, GSI_SAME_STMT);
  1235. /* Fix CFG. */
  1236. /* Edge e_cd connects cond_bb to dcall_bb, etc; note the first letters. */
  1237. e_cd = split_block (cond_bb, cond_stmt);
  1238. dcall_bb = e_cd->dest;
  1239. dcall_bb->count = count;
  1240. e_di = split_block (dcall_bb, dcall_stmt);
  1241. icall_bb = e_di->dest;
  1242. icall_bb->count = all - count;
  1243. /* Do not disturb existing EH edges from the indirect call. */
  1244. if (!stmt_ends_bb_p (icall_stmt))
  1245. e_ij = split_block (icall_bb, icall_stmt);
  1246. else
  1247. {
  1248. e_ij = find_fallthru_edge (icall_bb->succs);
  1249. /* The indirect call might be noreturn. */
  1250. if (e_ij != NULL)
  1251. {
  1252. e_ij->probability = REG_BR_PROB_BASE;
  1253. e_ij->count = all - count;
  1254. e_ij = single_pred_edge (split_edge (e_ij));
  1255. }
  1256. }
  1257. if (e_ij != NULL)
  1258. {
  1259. join_bb = e_ij->dest;
  1260. join_bb->count = all;
  1261. }
  1262. e_cd->flags = (e_cd->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
  1263. e_cd->probability = prob;
  1264. e_cd->count = count;
  1265. e_ci = make_edge (cond_bb, icall_bb, EDGE_FALSE_VALUE);
  1266. e_ci->probability = REG_BR_PROB_BASE - prob;
  1267. e_ci->count = all - count;
  1268. remove_edge (e_di);
  1269. if (e_ij != NULL)
  1270. {
  1271. if ((dflags & ECF_NORETURN) != 0)
  1272. e_ij->count = all;
  1273. else
  1274. {
  1275. e_dj = make_edge (dcall_bb, join_bb, EDGE_FALLTHRU);
  1276. e_dj->probability = REG_BR_PROB_BASE;
  1277. e_dj->count = count;
  1278. e_ij->count = all - count;
  1279. }
  1280. e_ij->probability = REG_BR_PROB_BASE;
  1281. }
  1282. /* Insert PHI node for the call result if necessary. */
  1283. if (gimple_call_lhs (icall_stmt)
  1284. && TREE_CODE (gimple_call_lhs (icall_stmt)) == SSA_NAME
  1285. && (dflags & ECF_NORETURN) == 0)
  1286. {
  1287. tree result = gimple_call_lhs (icall_stmt);
  1288. gphi *phi = create_phi_node (result, join_bb);
  1289. gimple_call_set_lhs (icall_stmt,
  1290. duplicate_ssa_name (result, icall_stmt));
  1291. add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
  1292. gimple_call_set_lhs (dcall_stmt,
  1293. duplicate_ssa_name (result, dcall_stmt));
  1294. add_phi_arg (phi, gimple_call_lhs (dcall_stmt), e_dj, UNKNOWN_LOCATION);
  1295. /* If indirect call has following BUILT_IN_CHKP_BNDRET
  1296. call then we need to make it's copy for the direct
  1297. call. */
  1298. if (iretbnd_stmt)
  1299. {
  1300. if (gimple_call_lhs (iretbnd_stmt))
  1301. {
  1302. gimple copy;
  1303. gimple_set_vdef (iretbnd_stmt, NULL_TREE);
  1304. gimple_set_vuse (iretbnd_stmt, NULL_TREE);
  1305. update_stmt (iretbnd_stmt);
  1306. result = gimple_call_lhs (iretbnd_stmt);
  1307. phi = create_phi_node (result, join_bb);
  1308. copy = gimple_copy (iretbnd_stmt);
  1309. gimple_call_set_arg (copy, 0,
  1310. gimple_call_lhs (dcall_stmt));
  1311. gimple_call_set_lhs (copy, duplicate_ssa_name (result, copy));
  1312. gsi_insert_on_edge (e_dj, copy);
  1313. add_phi_arg (phi, gimple_call_lhs (copy),
  1314. e_dj, UNKNOWN_LOCATION);
  1315. gimple_call_set_arg (iretbnd_stmt, 0,
  1316. gimple_call_lhs (icall_stmt));
  1317. gimple_call_set_lhs (iretbnd_stmt,
  1318. duplicate_ssa_name (result, iretbnd_stmt));
  1319. psi = gsi_for_stmt (iretbnd_stmt);
  1320. gsi_remove (&psi, false);
  1321. gsi_insert_on_edge (e_ij, iretbnd_stmt);
  1322. add_phi_arg (phi, gimple_call_lhs (iretbnd_stmt),
  1323. e_ij, UNKNOWN_LOCATION);
  1324. gsi_commit_one_edge_insert (e_dj, NULL);
  1325. gsi_commit_one_edge_insert (e_ij, NULL);
  1326. }
  1327. else
  1328. {
  1329. psi = gsi_for_stmt (iretbnd_stmt);
  1330. gsi_remove (&psi, true);
  1331. }
  1332. }
  1333. }
  1334. /* Build an EH edge for the direct call if necessary. */
  1335. lp_nr = lookup_stmt_eh_lp (icall_stmt);
  1336. if (lp_nr > 0 && stmt_could_throw_p (dcall_stmt))
  1337. {
  1338. add_stmt_to_eh_lp (dcall_stmt, lp_nr);
  1339. }
  1340. FOR_EACH_EDGE (e_eh, ei, icall_bb->succs)
  1341. if (e_eh->flags & (EDGE_EH | EDGE_ABNORMAL))
  1342. {
  1343. e = make_edge (dcall_bb, e_eh->dest, e_eh->flags);
  1344. for (gphi_iterator psi = gsi_start_phis (e_eh->dest);
  1345. !gsi_end_p (psi); gsi_next (&psi))
  1346. {
  1347. gphi *phi = psi.phi ();
  1348. SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e),
  1349. PHI_ARG_DEF_FROM_EDGE (phi, e_eh));
  1350. }
  1351. }
  1352. if (!stmt_could_throw_p (dcall_stmt))
  1353. gimple_purge_dead_eh_edges (dcall_bb);
  1354. return dcall_stmt;
  1355. }
  1356. /*
  1357. For every checked indirect/virtual call determine if most common pid of
  1358. function/class method has probability more than 50%. If yes modify code of
  1359. this call to:
  1360. */
  1361. static bool
  1362. gimple_ic_transform (gimple_stmt_iterator *gsi)
  1363. {
  1364. gcall *stmt;
  1365. histogram_value histogram;
  1366. gcov_type val, count, all, bb_all;
  1367. struct cgraph_node *direct_call;
  1368. stmt = dyn_cast <gcall *> (gsi_stmt (*gsi));
  1369. if (!stmt)
  1370. return false;
  1371. if (gimple_call_fndecl (stmt) != NULL_TREE)
  1372. return false;
  1373. if (gimple_call_internal_p (stmt))
  1374. return false;
  1375. histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INDIR_CALL);
  1376. if (!histogram)
  1377. return false;
  1378. val = histogram->hvalue.counters [0];
  1379. count = histogram->hvalue.counters [1];
  1380. all = histogram->hvalue.counters [2];
  1381. bb_all = gimple_bb (stmt)->count;
  1382. /* The order of CHECK_COUNTER calls is important -
  1383. since check_counter can correct the third parameter
  1384. and we want to make count <= all <= bb_all. */
  1385. if ( check_counter (stmt, "ic", &all, &bb_all, bb_all)
  1386. || check_counter (stmt, "ic", &count, &all, all))
  1387. {
  1388. gimple_remove_histogram_value (cfun, stmt, histogram);
  1389. return false;
  1390. }
  1391. if (4 * count <= 3 * all)
  1392. return false;
  1393. direct_call = find_func_by_profile_id ((int)val);
  1394. if (direct_call == NULL)
  1395. {
  1396. if (val)
  1397. {
  1398. if (dump_file)
  1399. {
  1400. fprintf (dump_file, "Indirect call -> direct call from other module");
  1401. print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
  1402. fprintf (dump_file, "=> %i (will resolve only with LTO)\n", (int)val);
  1403. }
  1404. }
  1405. return false;
  1406. }
  1407. if (!check_ic_target (stmt, direct_call))
  1408. {
  1409. if (dump_file)
  1410. {
  1411. fprintf (dump_file, "Indirect call -> direct call ");
  1412. print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
  1413. fprintf (dump_file, "=> ");
  1414. print_generic_expr (dump_file, direct_call->decl, TDF_SLIM);
  1415. fprintf (dump_file, " transformation skipped because of type mismatch");
  1416. print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
  1417. }
  1418. gimple_remove_histogram_value (cfun, stmt, histogram);
  1419. return false;
  1420. }
  1421. if (dump_file)
  1422. {
  1423. fprintf (dump_file, "Indirect call -> direct call ");
  1424. print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
  1425. fprintf (dump_file, "=> ");
  1426. print_generic_expr (dump_file, direct_call->decl, TDF_SLIM);
  1427. fprintf (dump_file, " transformation on insn postponned to ipa-profile");
  1428. print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
  1429. fprintf (dump_file, "hist->count %"PRId64
  1430. " hist->all %"PRId64"\n", count, all);
  1431. }
  1432. return true;
  1433. }
  1434. /* Return true if the stringop CALL with FNDECL shall be profiled.
  1435. SIZE_ARG be set to the argument index for the size of the string
  1436. operation.
  1437. */
  1438. static bool
  1439. interesting_stringop_to_profile_p (tree fndecl, gcall *call, int *size_arg)
  1440. {
  1441. enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
  1442. if (fcode != BUILT_IN_MEMCPY && fcode != BUILT_IN_MEMPCPY
  1443. && fcode != BUILT_IN_MEMSET && fcode != BUILT_IN_BZERO)
  1444. return false;
  1445. switch (fcode)
  1446. {
  1447. case BUILT_IN_MEMCPY:
  1448. case BUILT_IN_MEMPCPY:
  1449. *size_arg = 2;
  1450. return validate_gimple_arglist (call, POINTER_TYPE, POINTER_TYPE,
  1451. INTEGER_TYPE, VOID_TYPE);
  1452. case BUILT_IN_MEMSET:
  1453. *size_arg = 2;
  1454. return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
  1455. INTEGER_TYPE, VOID_TYPE);
  1456. case BUILT_IN_BZERO:
  1457. *size_arg = 1;
  1458. return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
  1459. VOID_TYPE);
  1460. default:
  1461. gcc_unreachable ();
  1462. }
  1463. }
  1464. /* Convert stringop (..., vcall_size)
  1465. into
  1466. if (vcall_size == icall_size)
  1467. stringop (..., icall_size);
  1468. else
  1469. stringop (..., vcall_size);
  1470. assuming we'll propagate a true constant into ICALL_SIZE later. */
  1471. static void
  1472. gimple_stringop_fixed_value (gcall *vcall_stmt, tree icall_size, int prob,
  1473. gcov_type count, gcov_type all)
  1474. {
  1475. gassign *tmp_stmt;
  1476. gcond *cond_stmt;
  1477. gcall *icall_stmt;
  1478. tree tmp0, tmp1, vcall_size, optype;
  1479. basic_block cond_bb, icall_bb, vcall_bb, join_bb;
  1480. edge e_ci, e_cv, e_iv, e_ij, e_vj;
  1481. gimple_stmt_iterator gsi;
  1482. tree fndecl;
  1483. int size_arg;
  1484. fndecl = gimple_call_fndecl (vcall_stmt);
  1485. if (!interesting_stringop_to_profile_p (fndecl, vcall_stmt, &size_arg))
  1486. gcc_unreachable ();
  1487. cond_bb = gimple_bb (vcall_stmt);
  1488. gsi = gsi_for_stmt (vcall_stmt);
  1489. vcall_size = gimple_call_arg (vcall_stmt, size_arg);
  1490. optype = TREE_TYPE (vcall_size);
  1491. tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
  1492. tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
  1493. tmp_stmt = gimple_build_assign (tmp0, fold_convert (optype, icall_size));
  1494. gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
  1495. tmp_stmt = gimple_build_assign (tmp1, vcall_size);
  1496. gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
  1497. cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
  1498. gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
  1499. gimple_set_vdef (vcall_stmt, NULL);
  1500. gimple_set_vuse (vcall_stmt, NULL);
  1501. update_stmt (vcall_stmt);
  1502. icall_stmt = as_a <gcall *> (gimple_copy (vcall_stmt));
  1503. gimple_call_set_arg (icall_stmt, size_arg, icall_size);
  1504. gsi_insert_before (&gsi, icall_stmt, GSI_SAME_STMT);
  1505. /* Fix CFG. */
  1506. /* Edge e_ci connects cond_bb to icall_bb, etc. */
  1507. e_ci = split_block (cond_bb, cond_stmt);
  1508. icall_bb = e_ci->dest;
  1509. icall_bb->count = count;
  1510. e_iv = split_block (icall_bb, icall_stmt);
  1511. vcall_bb = e_iv->dest;
  1512. vcall_bb->count = all - count;
  1513. e_vj = split_block (vcall_bb, vcall_stmt);
  1514. join_bb = e_vj->dest;
  1515. join_bb->count = all;
  1516. e_ci->flags = (e_ci->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
  1517. e_ci->probability = prob;
  1518. e_ci->count = count;
  1519. e_cv = make_edge (cond_bb, vcall_bb, EDGE_FALSE_VALUE);
  1520. e_cv->probability = REG_BR_PROB_BASE - prob;
  1521. e_cv->count = all - count;
  1522. remove_edge (e_iv);
  1523. e_ij = make_edge (icall_bb, join_bb, EDGE_FALLTHRU);
  1524. e_ij->probability = REG_BR_PROB_BASE;
  1525. e_ij->count = count;
  1526. e_vj->probability = REG_BR_PROB_BASE;
  1527. e_vj->count = all - count;
  1528. /* Insert PHI node for the call result if necessary. */
  1529. if (gimple_call_lhs (vcall_stmt)
  1530. && TREE_CODE (gimple_call_lhs (vcall_stmt)) == SSA_NAME)
  1531. {
  1532. tree result = gimple_call_lhs (vcall_stmt);
  1533. gphi *phi = create_phi_node (result, join_bb);
  1534. gimple_call_set_lhs (vcall_stmt,
  1535. duplicate_ssa_name (result, vcall_stmt));
  1536. add_phi_arg (phi, gimple_call_lhs (vcall_stmt), e_vj, UNKNOWN_LOCATION);
  1537. gimple_call_set_lhs (icall_stmt,
  1538. duplicate_ssa_name (result, icall_stmt));
  1539. add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
  1540. }
  1541. /* Because these are all string op builtins, they're all nothrow. */
  1542. gcc_assert (!stmt_could_throw_p (vcall_stmt));
  1543. gcc_assert (!stmt_could_throw_p (icall_stmt));
  1544. }
  1545. /* Find values inside STMT for that we want to measure histograms for
  1546. division/modulo optimization. */
  1547. static bool
  1548. gimple_stringops_transform (gimple_stmt_iterator *gsi)
  1549. {
  1550. gcall *stmt;
  1551. tree fndecl;
  1552. tree blck_size;
  1553. enum built_in_function fcode;
  1554. histogram_value histogram;
  1555. gcov_type count, all, val;
  1556. tree dest, src;
  1557. unsigned int dest_align, src_align;
  1558. gcov_type prob;
  1559. tree tree_val;
  1560. int size_arg;
  1561. stmt = dyn_cast <gcall *> (gsi_stmt (*gsi));
  1562. if (!stmt)
  1563. return false;
  1564. fndecl = gimple_call_fndecl (stmt);
  1565. if (!fndecl)
  1566. return false;
  1567. fcode = DECL_FUNCTION_CODE (fndecl);
  1568. if (!interesting_stringop_to_profile_p (fndecl, stmt, &size_arg))
  1569. return false;
  1570. blck_size = gimple_call_arg (stmt, size_arg);
  1571. if (TREE_CODE (blck_size) == INTEGER_CST)
  1572. return false;
  1573. histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_SINGLE_VALUE);
  1574. if (!histogram)
  1575. return false;
  1576. val = histogram->hvalue.counters[0];
  1577. count = histogram->hvalue.counters[1];
  1578. all = histogram->hvalue.counters[2];
  1579. gimple_remove_histogram_value (cfun, stmt, histogram);
  1580. /* We require that count is at least half of all; this means
  1581. that for the transformation to fire the value must be constant
  1582. at least 80% of time. */
  1583. if ((6 * count / 5) < all || optimize_bb_for_size_p (gimple_bb (stmt)))
  1584. return false;
  1585. if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
  1586. return false;
  1587. if (all > 0)
  1588. prob = GCOV_COMPUTE_SCALE (count, all);
  1589. else
  1590. prob = 0;
  1591. dest = gimple_call_arg (stmt, 0);
  1592. dest_align = get_pointer_alignment (dest);
  1593. switch (fcode)
  1594. {
  1595. case BUILT_IN_MEMCPY:
  1596. case BUILT_IN_MEMPCPY:
  1597. src = gimple_call_arg (stmt, 1);
  1598. src_align = get_pointer_alignment (src);
  1599. if (!can_move_by_pieces (val, MIN (dest_align, src_align)))
  1600. return false;
  1601. break;
  1602. case BUILT_IN_MEMSET:
  1603. if (!can_store_by_pieces (val, builtin_memset_read_str,
  1604. gimple_call_arg (stmt, 1),
  1605. dest_align, true))
  1606. return false;
  1607. break;
  1608. case BUILT_IN_BZERO:
  1609. if (!can_store_by_pieces (val, builtin_memset_read_str,
  1610. integer_zero_node,
  1611. dest_align, true))
  1612. return false;
  1613. break;
  1614. default:
  1615. gcc_unreachable ();
  1616. }
  1617. if (sizeof (gcov_type) == sizeof (HOST_WIDE_INT))
  1618. tree_val = build_int_cst (get_gcov_type (), val);
  1619. else
  1620. {
  1621. HOST_WIDE_INT a[2];
  1622. a[0] = (unsigned HOST_WIDE_INT) val;
  1623. a[1] = val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1;
  1624. tree_val = wide_int_to_tree (get_gcov_type (), wide_int::from_array (a, 2,
  1625. TYPE_PRECISION (get_gcov_type ()), false));
  1626. }
  1627. if (dump_file)
  1628. {
  1629. fprintf (dump_file, "Single value %i stringop transformation on ",
  1630. (int)val);
  1631. print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
  1632. }
  1633. gimple_stringop_fixed_value (stmt, tree_val, prob, count, all);
  1634. return true;
  1635. }
  1636. void
  1637. stringop_block_profile (gimple stmt, unsigned int *expected_align,
  1638. HOST_WIDE_INT *expected_size)
  1639. {
  1640. histogram_value histogram;
  1641. histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_AVERAGE);
  1642. if (!histogram)
  1643. *expected_size = -1;
  1644. else if (!histogram->hvalue.counters[1])
  1645. {
  1646. *expected_size = -1;
  1647. gimple_remove_histogram_value (cfun, stmt, histogram);
  1648. }
  1649. else
  1650. {
  1651. gcov_type size;
  1652. size = ((histogram->hvalue.counters[0]
  1653. + histogram->hvalue.counters[1] / 2)
  1654. / histogram->hvalue.counters[1]);
  1655. /* Even if we can hold bigger value in SIZE, INT_MAX
  1656. is safe "infinity" for code generation strategies. */
  1657. if (size > INT_MAX)
  1658. size = INT_MAX;
  1659. *expected_size = size;
  1660. gimple_remove_histogram_value (cfun, stmt, histogram);
  1661. }
  1662. histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_IOR);
  1663. if (!histogram)
  1664. *expected_align = 0;
  1665. else if (!histogram->hvalue.counters[0])
  1666. {
  1667. gimple_remove_histogram_value (cfun, stmt, histogram);
  1668. *expected_align = 0;
  1669. }
  1670. else
  1671. {
  1672. gcov_type count;
  1673. int alignment;
  1674. count = histogram->hvalue.counters[0];
  1675. alignment = 1;
  1676. while (!(count & alignment)
  1677. && (alignment * 2 * BITS_PER_UNIT))
  1678. alignment <<= 1;
  1679. *expected_align = alignment * BITS_PER_UNIT;
  1680. gimple_remove_histogram_value (cfun, stmt, histogram);
  1681. }
  1682. }
  1683. /* Find values inside STMT for that we want to measure histograms for
  1684. division/modulo optimization. */
  1685. static void
  1686. gimple_divmod_values_to_profile (gimple stmt, histogram_values *values)
  1687. {
  1688. tree lhs, divisor, op0, type;
  1689. histogram_value hist;
  1690. if (gimple_code (stmt) != GIMPLE_ASSIGN)
  1691. return;
  1692. lhs = gimple_assign_lhs (stmt);
  1693. type = TREE_TYPE (lhs);
  1694. if (!INTEGRAL_TYPE_P (type))
  1695. return;
  1696. switch (gimple_assign_rhs_code (stmt))
  1697. {
  1698. case TRUNC_DIV_EXPR:
  1699. case TRUNC_MOD_EXPR:
  1700. divisor = gimple_assign_rhs2 (stmt);
  1701. op0 = gimple_assign_rhs1 (stmt);
  1702. values->reserve (3);
  1703. if (TREE_CODE (divisor) == SSA_NAME)
  1704. /* Check for the case where the divisor is the same value most
  1705. of the time. */
  1706. values->quick_push (gimple_alloc_histogram_value (cfun,
  1707. HIST_TYPE_SINGLE_VALUE,
  1708. stmt, divisor));
  1709. /* For mod, check whether it is not often a noop (or replaceable by
  1710. a few subtractions). */
  1711. if (gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR
  1712. && TYPE_UNSIGNED (type))
  1713. {
  1714. tree val;
  1715. /* Check for a special case where the divisor is power of 2. */
  1716. values->quick_push (gimple_alloc_histogram_value (cfun,
  1717. HIST_TYPE_POW2,
  1718. stmt, divisor));
  1719. val = build2 (TRUNC_DIV_EXPR, type, op0, divisor);
  1720. hist = gimple_alloc_histogram_value (cfun, HIST_TYPE_INTERVAL,
  1721. stmt, val);
  1722. hist->hdata.intvl.int_start = 0;
  1723. hist->hdata.intvl.steps = 2;
  1724. values->quick_push (hist);
  1725. }
  1726. return;
  1727. default:
  1728. return;
  1729. }
  1730. }
  1731. /* Find calls inside STMT for that we want to measure histograms for
  1732. indirect/virtual call optimization. */
  1733. static void
  1734. gimple_indirect_call_to_profile (gimple stmt, histogram_values *values)
  1735. {
  1736. tree callee;
  1737. if (gimple_code (stmt) != GIMPLE_CALL
  1738. || gimple_call_internal_p (stmt)
  1739. || gimple_call_fndecl (stmt) != NULL_TREE)
  1740. return;
  1741. callee = gimple_call_fn (stmt);
  1742. values->reserve (3);
  1743. values->quick_push (gimple_alloc_histogram_value (
  1744. cfun,
  1745. PARAM_VALUE (PARAM_INDIR_CALL_TOPN_PROFILE) ?
  1746. HIST_TYPE_INDIR_CALL_TOPN :
  1747. HIST_TYPE_INDIR_CALL,
  1748. stmt, callee));
  1749. return;
  1750. }
  1751. /* Find values inside STMT for that we want to measure histograms for
  1752. string operations. */
  1753. static void
  1754. gimple_stringops_values_to_profile (gimple gs, histogram_values *values)
  1755. {
  1756. gcall *stmt;
  1757. tree fndecl;
  1758. tree blck_size;
  1759. tree dest;
  1760. int size_arg;
  1761. stmt = dyn_cast <gcall *> (gs);
  1762. if (!stmt)
  1763. return;
  1764. fndecl = gimple_call_fndecl (stmt);
  1765. if (!fndecl)
  1766. return;
  1767. if (!interesting_stringop_to_profile_p (fndecl, stmt, &size_arg))
  1768. return;
  1769. dest = gimple_call_arg (stmt, 0);
  1770. blck_size = gimple_call_arg (stmt, size_arg);
  1771. if (TREE_CODE (blck_size) != INTEGER_CST)
  1772. {
  1773. values->safe_push (gimple_alloc_histogram_value (cfun,
  1774. HIST_TYPE_SINGLE_VALUE,
  1775. stmt, blck_size));
  1776. values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_AVERAGE,
  1777. stmt, blck_size));
  1778. }
  1779. if (TREE_CODE (blck_size) != INTEGER_CST)
  1780. values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_IOR,
  1781. stmt, dest));
  1782. }
  1783. /* Find values inside STMT for that we want to measure histograms and adds
  1784. them to list VALUES. */
  1785. static void
  1786. gimple_values_to_profile (gimple stmt, histogram_values *values)
  1787. {
  1788. gimple_divmod_values_to_profile (stmt, values);
  1789. gimple_stringops_values_to_profile (stmt, values);
  1790. gimple_indirect_call_to_profile (stmt, values);
  1791. }
  1792. void
  1793. gimple_find_values_to_profile (histogram_values *values)
  1794. {
  1795. basic_block bb;
  1796. gimple_stmt_iterator gsi;
  1797. unsigned i;
  1798. histogram_value hist = NULL;
  1799. values->create (0);
  1800. FOR_EACH_BB_FN (bb, cfun)
  1801. for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  1802. gimple_values_to_profile (gsi_stmt (gsi), values);
  1803. values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_TIME_PROFILE, 0, 0));
  1804. FOR_EACH_VEC_ELT (*values, i, hist)
  1805. {
  1806. switch (hist->type)
  1807. {
  1808. case HIST_TYPE_INTERVAL:
  1809. hist->n_counters = hist->hdata.intvl.steps + 2;
  1810. break;
  1811. case HIST_TYPE_POW2:
  1812. hist->n_counters = 2;
  1813. break;
  1814. case HIST_TYPE_SINGLE_VALUE:
  1815. hist->n_counters = 3;
  1816. break;
  1817. case HIST_TYPE_CONST_DELTA:
  1818. hist->n_counters = 4;
  1819. break;
  1820. case HIST_TYPE_INDIR_CALL:
  1821. hist->n_counters = 3;
  1822. break;
  1823. case HIST_TYPE_TIME_PROFILE:
  1824. hist->n_counters = 1;
  1825. break;
  1826. case HIST_TYPE_AVERAGE:
  1827. hist->n_counters = 2;
  1828. break;
  1829. case HIST_TYPE_IOR:
  1830. hist->n_counters = 1;
  1831. break;
  1832. case HIST_TYPE_INDIR_CALL_TOPN:
  1833. hist->n_counters = GCOV_ICALL_TOPN_NCOUNTS;
  1834. break;
  1835. default:
  1836. gcc_unreachable ();
  1837. }
  1838. if (dump_file)
  1839. {
  1840. fprintf (dump_file, "Stmt ");
  1841. print_gimple_stmt (dump_file, hist->hvalue.stmt, 0, TDF_SLIM);
  1842. dump_histogram_value (dump_file, hist);
  1843. }
  1844. }
  1845. }