auto-profile.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729
  1. /* Read and annotate call graph profile from the auto profile data file.
  2. Copyright (C) 2014-2015 Free Software Foundation, Inc.
  3. Contributed by Dehao Chen (dehao@google.com)
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. 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 <string.h>
  19. #include <map>
  20. #include <set>
  21. #include "coretypes.h"
  22. #include "hash-set.h"
  23. #include "machmode.h"
  24. #include "vec.h"
  25. #include "double-int.h"
  26. #include "input.h"
  27. #include "alias.h"
  28. #include "symtab.h"
  29. #include "options.h"
  30. #include "wide-int.h"
  31. #include "inchash.h"
  32. #include "tree.h"
  33. #include "fold-const.h"
  34. #include "tree-pass.h"
  35. #include "flags.h"
  36. #include "predict.h"
  37. #include "vec.h"
  38. #include "hashtab.h"
  39. #include "hash-set.h"
  40. #include "machmode.h"
  41. #include "tm.h"
  42. #include "hard-reg-set.h"
  43. #include "input.h"
  44. #include "function.h"
  45. #include "dominance.h"
  46. #include "cfg.h"
  47. #include "basic-block.h"
  48. #include "diagnostic-core.h"
  49. #include "gcov-io.h"
  50. #include "profile.h"
  51. #include "langhooks.h"
  52. #include "opts.h"
  53. #include "tree-pass.h"
  54. #include "cfgloop.h"
  55. #include "tree-ssa-alias.h"
  56. #include "tree-cfg.h"
  57. #include "tree-cfgcleanup.h"
  58. #include "tree-ssa-operands.h"
  59. #include "tree-into-ssa.h"
  60. #include "internal-fn.h"
  61. #include "is-a.h"
  62. #include "gimple-expr.h"
  63. #include "gimple.h"
  64. #include "gimple-iterator.h"
  65. #include "gimple-ssa.h"
  66. #include "hash-map.h"
  67. #include "plugin-api.h"
  68. #include "ipa-ref.h"
  69. #include "cgraph.h"
  70. #include "value-prof.h"
  71. #include "coverage.h"
  72. #include "params.h"
  73. #include "alloc-pool.h"
  74. #include "symbol-summary.h"
  75. #include "ipa-prop.h"
  76. #include "ipa-inline.h"
  77. #include "tree-inline.h"
  78. #include "stringpool.h"
  79. #include "auto-profile.h"
  80. /* The following routines implements AutoFDO optimization.
  81. This optimization uses sampling profiles to annotate basic block counts
  82. and uses heuristics to estimate branch probabilities.
  83. There are three phases in AutoFDO:
  84. Phase 1: Read profile from the profile data file.
  85. The following info is read from the profile datafile:
  86. * string_table: a map between function name and its index.
  87. * autofdo_source_profile: a map from function_instance name to
  88. function_instance. This is represented as a forest of
  89. function_instances.
  90. * WorkingSet: a histogram of how many instructions are covered for a
  91. given percentage of total cycles. This is describing the binary
  92. level information (not source level). This info is used to help
  93. decide if we want aggressive optimizations that could increase
  94. code footprint (e.g. loop unroll etc.)
  95. A function instance is an instance of function that could either be a
  96. standalone symbol, or a clone of a function that is inlined into another
  97. function.
  98. Phase 2: Early inline + value profile transformation.
  99. Early inline uses autofdo_source_profile to find if a callsite is:
  100. * inlined in the profiled binary.
  101. * callee body is hot in the profiling run.
  102. If both condition satisfies, early inline will inline the callsite
  103. regardless of the code growth.
  104. Phase 2 is an iterative process. During each iteration, we also check
  105. if an indirect callsite is promoted and inlined in the profiling run.
  106. If yes, vpt will happen to force promote it and in the next iteration,
  107. einline will inline the promoted callsite in the next iteration.
  108. Phase 3: Annotate control flow graph.
  109. AutoFDO uses a separate pass to:
  110. * Annotate basic block count
  111. * Estimate branch probability
  112. After the above 3 phases, all profile is readily annotated on the GCC IR.
  113. AutoFDO tries to reuse all FDO infrastructure as much as possible to make
  114. use of the profile. E.g. it uses existing mechanism to calculate the basic
  115. block/edge frequency, as well as the cgraph node/edge count.
  116. */
  117. #define DEFAULT_AUTO_PROFILE_FILE "fbdata.afdo"
  118. #define AUTO_PROFILE_VERSION 1
  119. namespace autofdo
  120. {
  121. /* Represent a source location: (function_decl, lineno). */
  122. typedef std::pair<tree, unsigned> decl_lineno;
  123. /* Represent an inline stack. vector[0] is the leaf node. */
  124. typedef auto_vec<decl_lineno> inline_stack;
  125. /* String array that stores function names. */
  126. typedef auto_vec<char *> string_vector;
  127. /* Map from function name's index in string_table to target's
  128. execution count. */
  129. typedef std::map<unsigned, gcov_type> icall_target_map;
  130. /* Set of gimple stmts. Used to track if the stmt has already been promoted
  131. to direct call. */
  132. typedef std::set<gimple> stmt_set;
  133. /* Represent count info of an inline stack. */
  134. struct count_info
  135. {
  136. /* Sampled count of the inline stack. */
  137. gcov_type count;
  138. /* Map from indirect call target to its sample count. */
  139. icall_target_map targets;
  140. /* Whether this inline stack is already used in annotation.
  141. Each inline stack should only be used to annotate IR once.
  142. This will be enforced when instruction-level discriminator
  143. is supported. */
  144. bool annotated;
  145. };
  146. /* operator< for "const char *". */
  147. struct string_compare
  148. {
  149. bool operator()(const char *a, const char *b) const
  150. {
  151. return strcmp (a, b) < 0;
  152. }
  153. };
  154. /* Store a string array, indexed by string position in the array. */
  155. class string_table
  156. {
  157. public:
  158. string_table ()
  159. {}
  160. ~string_table ();
  161. /* For a given string, returns its index. */
  162. int get_index (const char *name) const;
  163. /* For a given decl, returns the index of the decl name. */
  164. int get_index_by_decl (tree decl) const;
  165. /* For a given index, returns the string. */
  166. const char *get_name (int index) const;
  167. /* Read profile, return TRUE on success. */
  168. bool read ();
  169. private:
  170. typedef std::map<const char *, unsigned, string_compare> string_index_map;
  171. string_vector vector_;
  172. string_index_map map_;
  173. };
  174. /* Profile of a function instance:
  175. 1. total_count of the function.
  176. 2. head_count (entry basic block count) of the function (only valid when
  177. function is a top-level function_instance, i.e. it is the original copy
  178. instead of the inlined copy).
  179. 3. map from source location (decl_lineno) to profile (count_info).
  180. 4. map from callsite to callee function_instance. */
  181. class function_instance
  182. {
  183. public:
  184. typedef auto_vec<function_instance *> function_instance_stack;
  185. /* Read the profile and return a function_instance with head count as
  186. HEAD_COUNT. Recursively read callsites to create nested function_instances
  187. too. STACK is used to track the recursive creation process. */
  188. static function_instance *
  189. read_function_instance (function_instance_stack *stack,
  190. gcov_type head_count);
  191. /* Recursively deallocate all callsites (nested function_instances). */
  192. ~function_instance ();
  193. /* Accessors. */
  194. int
  195. name () const
  196. {
  197. return name_;
  198. }
  199. gcov_type
  200. total_count () const
  201. {
  202. return total_count_;
  203. }
  204. gcov_type
  205. head_count () const
  206. {
  207. return head_count_;
  208. }
  209. /* Traverse callsites of the current function_instance to find one at the
  210. location of LINENO and callee name represented in DECL. */
  211. function_instance *get_function_instance_by_decl (unsigned lineno,
  212. tree decl) const;
  213. /* Store the profile info for LOC in INFO. Return TRUE if profile info
  214. is found. */
  215. bool get_count_info (location_t loc, count_info *info) const;
  216. /* Read the inlined indirect call target profile for STMT and store it in
  217. MAP, return the total count for all inlined indirect calls. */
  218. gcov_type find_icall_target_map (gcall *stmt, icall_target_map *map) const;
  219. /* Sum of counts that is used during annotation. */
  220. gcov_type total_annotated_count () const;
  221. /* Mark LOC as annotated. */
  222. void mark_annotated (location_t loc);
  223. private:
  224. /* Callsite, represented as (decl_lineno, callee_function_name_index). */
  225. typedef std::pair<unsigned, unsigned> callsite;
  226. /* Map from callsite to callee function_instance. */
  227. typedef std::map<callsite, function_instance *> callsite_map;
  228. function_instance (unsigned name, gcov_type head_count)
  229. : name_ (name), total_count_ (0), head_count_ (head_count)
  230. {
  231. }
  232. /* Map from source location (decl_lineno) to profile (count_info). */
  233. typedef std::map<unsigned, count_info> position_count_map;
  234. /* function_instance name index in the string_table. */
  235. unsigned name_;
  236. /* Total sample count. */
  237. gcov_type total_count_;
  238. /* Entry BB's sample count. */
  239. gcov_type head_count_;
  240. /* Map from callsite location to callee function_instance. */
  241. callsite_map callsites;
  242. /* Map from source location to count_info. */
  243. position_count_map pos_counts;
  244. };
  245. /* Profile for all functions. */
  246. class autofdo_source_profile
  247. {
  248. public:
  249. static autofdo_source_profile *
  250. create ()
  251. {
  252. autofdo_source_profile *map = new autofdo_source_profile ();
  253. if (map->read ())
  254. return map;
  255. delete map;
  256. return NULL;
  257. }
  258. ~autofdo_source_profile ();
  259. /* For a given DECL, returns the top-level function_instance. */
  260. function_instance *get_function_instance_by_decl (tree decl) const;
  261. /* Find count_info for a given gimple STMT. If found, store the count_info
  262. in INFO and return true; otherwise return false. */
  263. bool get_count_info (gimple stmt, count_info *info) const;
  264. /* Find total count of the callee of EDGE. */
  265. gcov_type get_callsite_total_count (struct cgraph_edge *edge) const;
  266. /* Update value profile INFO for STMT from the inlined indirect callsite.
  267. Return true if INFO is updated. */
  268. bool update_inlined_ind_target (gcall *stmt, count_info *info);
  269. /* Mark LOC as annotated. */
  270. void mark_annotated (location_t loc);
  271. private:
  272. /* Map from function_instance name index (in string_table) to
  273. function_instance. */
  274. typedef std::map<unsigned, function_instance *> name_function_instance_map;
  275. autofdo_source_profile () {}
  276. /* Read AutoFDO profile and returns TRUE on success. */
  277. bool read ();
  278. /* Return the function_instance in the profile that correspond to the
  279. inline STACK. */
  280. function_instance *
  281. get_function_instance_by_inline_stack (const inline_stack &stack) const;
  282. name_function_instance_map map_;
  283. };
  284. /* Store the strings read from the profile data file. */
  285. static string_table *afdo_string_table;
  286. /* Store the AutoFDO source profile. */
  287. static autofdo_source_profile *afdo_source_profile;
  288. /* gcov_ctr_summary structure to store the profile_info. */
  289. static struct gcov_ctr_summary *afdo_profile_info;
  290. /* Helper functions. */
  291. /* Return the original name of NAME: strip the suffix that starts
  292. with '.' Caller is responsible for freeing RET. */
  293. static char *
  294. get_original_name (const char *name)
  295. {
  296. char *ret = xstrdup (name);
  297. char *find = strchr (ret, '.');
  298. if (find != NULL)
  299. *find = 0;
  300. return ret;
  301. }
  302. /* Return the combined location, which is a 32bit integer in which
  303. higher 16 bits stores the line offset of LOC to the start lineno
  304. of DECL, The lower 16 bits stores the discriminator. */
  305. static unsigned
  306. get_combined_location (location_t loc, tree decl)
  307. {
  308. /* TODO: allow more bits for line and less bits for discriminator. */
  309. if (LOCATION_LINE (loc) - DECL_SOURCE_LINE (decl) >= (1<<16))
  310. warning_at (loc, OPT_Woverflow, "Offset exceeds 16 bytes.");
  311. return ((LOCATION_LINE (loc) - DECL_SOURCE_LINE (decl)) << 16);
  312. }
  313. /* Return the function decl of a given lexical BLOCK. */
  314. static tree
  315. get_function_decl_from_block (tree block)
  316. {
  317. tree decl;
  318. if (LOCATION_LOCUS (BLOCK_SOURCE_LOCATION (block) == UNKNOWN_LOCATION))
  319. return NULL_TREE;
  320. for (decl = BLOCK_ABSTRACT_ORIGIN (block);
  321. decl && (TREE_CODE (decl) == BLOCK);
  322. decl = BLOCK_ABSTRACT_ORIGIN (decl))
  323. if (TREE_CODE (decl) == FUNCTION_DECL)
  324. break;
  325. return decl;
  326. }
  327. /* Store inline stack for STMT in STACK. */
  328. static void
  329. get_inline_stack (location_t locus, inline_stack *stack)
  330. {
  331. if (LOCATION_LOCUS (locus) == UNKNOWN_LOCATION)
  332. return;
  333. tree block = LOCATION_BLOCK (locus);
  334. if (block && TREE_CODE (block) == BLOCK)
  335. {
  336. int level = 0;
  337. for (block = BLOCK_SUPERCONTEXT (block);
  338. block && (TREE_CODE (block) == BLOCK);
  339. block = BLOCK_SUPERCONTEXT (block))
  340. {
  341. location_t tmp_locus = BLOCK_SOURCE_LOCATION (block);
  342. if (LOCATION_LOCUS (tmp_locus) == UNKNOWN_LOCATION)
  343. continue;
  344. tree decl = get_function_decl_from_block (block);
  345. stack->safe_push (
  346. std::make_pair (decl, get_combined_location (locus, decl)));
  347. locus = tmp_locus;
  348. level++;
  349. }
  350. }
  351. stack->safe_push (
  352. std::make_pair (current_function_decl,
  353. get_combined_location (locus, current_function_decl)));
  354. }
  355. /* Return STMT's combined location, which is a 32bit integer in which
  356. higher 16 bits stores the line offset of LOC to the start lineno
  357. of DECL, The lower 16 bits stores the discriminator. */
  358. static unsigned
  359. get_relative_location_for_stmt (gimple stmt)
  360. {
  361. location_t locus = gimple_location (stmt);
  362. if (LOCATION_LOCUS (locus) == UNKNOWN_LOCATION)
  363. return UNKNOWN_LOCATION;
  364. for (tree block = gimple_block (stmt); block && (TREE_CODE (block) == BLOCK);
  365. block = BLOCK_SUPERCONTEXT (block))
  366. if (LOCATION_LOCUS (BLOCK_SOURCE_LOCATION (block)) != UNKNOWN_LOCATION)
  367. return get_combined_location (locus,
  368. get_function_decl_from_block (block));
  369. return get_combined_location (locus, current_function_decl);
  370. }
  371. /* Return true if BB contains indirect call. */
  372. static bool
  373. has_indirect_call (basic_block bb)
  374. {
  375. gimple_stmt_iterator gsi;
  376. for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  377. {
  378. gimple stmt = gsi_stmt (gsi);
  379. if (gimple_code (stmt) == GIMPLE_CALL && !gimple_call_internal_p (stmt)
  380. && (gimple_call_fn (stmt) == NULL
  381. || TREE_CODE (gimple_call_fn (stmt)) != FUNCTION_DECL))
  382. return true;
  383. }
  384. return false;
  385. }
  386. /* Member functions for string_table. */
  387. /* Deconstructor. */
  388. string_table::~string_table ()
  389. {
  390. for (unsigned i = 0; i < vector_.length (); i++)
  391. free (vector_[i]);
  392. }
  393. /* Return the index of a given function NAME. Return -1 if NAME is not
  394. found in string table. */
  395. int
  396. string_table::get_index (const char *name) const
  397. {
  398. if (name == NULL)
  399. return -1;
  400. string_index_map::const_iterator iter = map_.find (name);
  401. if (iter == map_.end ())
  402. return -1;
  403. return iter->second;
  404. }
  405. /* Return the index of a given function DECL. Return -1 if DECL is not
  406. found in string table. */
  407. int
  408. string_table::get_index_by_decl (tree decl) const
  409. {
  410. char *name
  411. = get_original_name (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
  412. int ret = get_index (name);
  413. free (name);
  414. if (ret != -1)
  415. return ret;
  416. ret = get_index (lang_hooks.dwarf_name (decl, 0));
  417. if (ret != -1)
  418. return ret;
  419. if (DECL_ABSTRACT_ORIGIN (decl))
  420. return get_index_by_decl (DECL_ABSTRACT_ORIGIN (decl));
  421. return -1;
  422. }
  423. /* Return the function name of a given INDEX. */
  424. const char *
  425. string_table::get_name (int index) const
  426. {
  427. gcc_assert (index > 0 && index < (int)vector_.length ());
  428. return vector_[index];
  429. }
  430. /* Read the string table. Return TRUE if reading is successful. */
  431. bool
  432. string_table::read ()
  433. {
  434. if (gcov_read_unsigned () != GCOV_TAG_AFDO_FILE_NAMES)
  435. return false;
  436. /* Skip the length of the section. */
  437. gcov_read_unsigned ();
  438. /* Read in the file name table. */
  439. unsigned string_num = gcov_read_unsigned ();
  440. for (unsigned i = 0; i < string_num; i++)
  441. {
  442. vector_.safe_push (get_original_name (gcov_read_string ()));
  443. map_[vector_.last ()] = i;
  444. }
  445. return true;
  446. }
  447. /* Member functions for function_instance. */
  448. function_instance::~function_instance ()
  449. {
  450. for (callsite_map::iterator iter = callsites.begin ();
  451. iter != callsites.end (); ++iter)
  452. delete iter->second;
  453. }
  454. /* Traverse callsites of the current function_instance to find one at the
  455. location of LINENO and callee name represented in DECL. */
  456. function_instance *
  457. function_instance::get_function_instance_by_decl (unsigned lineno,
  458. tree decl) const
  459. {
  460. int func_name_idx = afdo_string_table->get_index_by_decl (decl);
  461. if (func_name_idx != -1)
  462. {
  463. callsite_map::const_iterator ret
  464. = callsites.find (std::make_pair (lineno, func_name_idx));
  465. if (ret != callsites.end ())
  466. return ret->second;
  467. }
  468. func_name_idx
  469. = afdo_string_table->get_index (lang_hooks.dwarf_name (decl, 0));
  470. if (func_name_idx != -1)
  471. {
  472. callsite_map::const_iterator ret
  473. = callsites.find (std::make_pair (lineno, func_name_idx));
  474. if (ret != callsites.end ())
  475. return ret->second;
  476. }
  477. if (DECL_ABSTRACT_ORIGIN (decl))
  478. return get_function_instance_by_decl (lineno, DECL_ABSTRACT_ORIGIN (decl));
  479. return NULL;
  480. }
  481. /* Store the profile info for LOC in INFO. Return TRUE if profile info
  482. is found. */
  483. bool
  484. function_instance::get_count_info (location_t loc, count_info *info) const
  485. {
  486. position_count_map::const_iterator iter = pos_counts.find (loc);
  487. if (iter == pos_counts.end ())
  488. return false;
  489. *info = iter->second;
  490. return true;
  491. }
  492. /* Mark LOC as annotated. */
  493. void
  494. function_instance::mark_annotated (location_t loc)
  495. {
  496. position_count_map::iterator iter = pos_counts.find (loc);
  497. if (iter == pos_counts.end ())
  498. return;
  499. iter->second.annotated = true;
  500. }
  501. /* Read the inlined indirect call target profile for STMT and store it in
  502. MAP, return the total count for all inlined indirect calls. */
  503. gcov_type
  504. function_instance::find_icall_target_map (gcall *stmt,
  505. icall_target_map *map) const
  506. {
  507. gcov_type ret = 0;
  508. unsigned stmt_offset = get_relative_location_for_stmt (stmt);
  509. for (callsite_map::const_iterator iter = callsites.begin ();
  510. iter != callsites.end (); ++iter)
  511. {
  512. unsigned callee = iter->second->name ();
  513. /* Check if callsite location match the stmt. */
  514. if (iter->first.first != stmt_offset)
  515. continue;
  516. struct cgraph_node *node = cgraph_node::get_for_asmname (
  517. get_identifier (afdo_string_table->get_name (callee)));
  518. if (node == NULL)
  519. continue;
  520. if (!check_ic_target (stmt, node))
  521. continue;
  522. (*map)[callee] = iter->second->total_count ();
  523. ret += iter->second->total_count ();
  524. }
  525. return ret;
  526. }
  527. /* Read the profile and create a function_instance with head count as
  528. HEAD_COUNT. Recursively read callsites to create nested function_instances
  529. too. STACK is used to track the recursive creation process. */
  530. /* function instance profile format:
  531. ENTRY_COUNT: 8 bytes
  532. NAME_INDEX: 4 bytes
  533. NUM_POS_COUNTS: 4 bytes
  534. NUM_CALLSITES: 4 byte
  535. POS_COUNT_1:
  536. POS_1_OFFSET: 4 bytes
  537. NUM_TARGETS: 4 bytes
  538. COUNT: 8 bytes
  539. TARGET_1:
  540. VALUE_PROFILE_TYPE: 4 bytes
  541. TARGET_IDX: 8 bytes
  542. COUNT: 8 bytes
  543. TARGET_2
  544. ...
  545. TARGET_n
  546. POS_COUNT_2
  547. ...
  548. POS_COUNT_N
  549. CALLSITE_1:
  550. CALLSITE_1_OFFSET: 4 bytes
  551. FUNCTION_INSTANCE_PROFILE (nested)
  552. CALLSITE_2
  553. ...
  554. CALLSITE_n. */
  555. function_instance *
  556. function_instance::read_function_instance (function_instance_stack *stack,
  557. gcov_type head_count)
  558. {
  559. unsigned name = gcov_read_unsigned ();
  560. unsigned num_pos_counts = gcov_read_unsigned ();
  561. unsigned num_callsites = gcov_read_unsigned ();
  562. function_instance *s = new function_instance (name, head_count);
  563. stack->safe_push (s);
  564. for (unsigned i = 0; i < num_pos_counts; i++)
  565. {
  566. unsigned offset = gcov_read_unsigned () & 0xffff0000;
  567. unsigned num_targets = gcov_read_unsigned ();
  568. gcov_type count = gcov_read_counter ();
  569. s->pos_counts[offset].count = count;
  570. for (unsigned j = 0; j < stack->length (); j++)
  571. (*stack)[j]->total_count_ += count;
  572. for (unsigned j = 0; j < num_targets; j++)
  573. {
  574. /* Only indirect call target histogram is supported now. */
  575. gcov_read_unsigned ();
  576. gcov_type target_idx = gcov_read_counter ();
  577. s->pos_counts[offset].targets[target_idx] = gcov_read_counter ();
  578. }
  579. }
  580. for (unsigned i = 0; i < num_callsites; i++)
  581. {
  582. unsigned offset = gcov_read_unsigned ();
  583. function_instance *callee_function_instance
  584. = read_function_instance (stack, 0);
  585. s->callsites[std::make_pair (offset, callee_function_instance->name ())]
  586. = callee_function_instance;
  587. }
  588. stack->pop ();
  589. return s;
  590. }
  591. /* Sum of counts that is used during annotation. */
  592. gcov_type
  593. function_instance::total_annotated_count () const
  594. {
  595. gcov_type ret = 0;
  596. for (callsite_map::const_iterator iter = callsites.begin ();
  597. iter != callsites.end (); ++iter)
  598. ret += iter->second->total_annotated_count ();
  599. for (position_count_map::const_iterator iter = pos_counts.begin ();
  600. iter != pos_counts.end (); ++iter)
  601. if (iter->second.annotated)
  602. ret += iter->second.count;
  603. return ret;
  604. }
  605. /* Member functions for autofdo_source_profile. */
  606. autofdo_source_profile::~autofdo_source_profile ()
  607. {
  608. for (name_function_instance_map::const_iterator iter = map_.begin ();
  609. iter != map_.end (); ++iter)
  610. delete iter->second;
  611. }
  612. /* For a given DECL, returns the top-level function_instance. */
  613. function_instance *
  614. autofdo_source_profile::get_function_instance_by_decl (tree decl) const
  615. {
  616. int index = afdo_string_table->get_index_by_decl (decl);
  617. if (index == -1)
  618. return NULL;
  619. name_function_instance_map::const_iterator ret = map_.find (index);
  620. return ret == map_.end () ? NULL : ret->second;
  621. }
  622. /* Find count_info for a given gimple STMT. If found, store the count_info
  623. in INFO and return true; otherwise return false. */
  624. bool
  625. autofdo_source_profile::get_count_info (gimple stmt, count_info *info) const
  626. {
  627. if (LOCATION_LOCUS (gimple_location (stmt)) == cfun->function_end_locus)
  628. return false;
  629. inline_stack stack;
  630. get_inline_stack (gimple_location (stmt), &stack);
  631. if (stack.length () == 0)
  632. return false;
  633. function_instance *s = get_function_instance_by_inline_stack (stack);
  634. if (s == NULL)
  635. return false;
  636. return s->get_count_info (stack[0].second, info);
  637. }
  638. /* Mark LOC as annotated. */
  639. void
  640. autofdo_source_profile::mark_annotated (location_t loc)
  641. {
  642. inline_stack stack;
  643. get_inline_stack (loc, &stack);
  644. if (stack.length () == 0)
  645. return;
  646. function_instance *s = get_function_instance_by_inline_stack (stack);
  647. if (s == NULL)
  648. return;
  649. s->mark_annotated (stack[0].second);
  650. }
  651. /* Update value profile INFO for STMT from the inlined indirect callsite.
  652. Return true if INFO is updated. */
  653. bool
  654. autofdo_source_profile::update_inlined_ind_target (gcall *stmt,
  655. count_info *info)
  656. {
  657. if (LOCATION_LOCUS (gimple_location (stmt)) == cfun->function_end_locus)
  658. return false;
  659. count_info old_info;
  660. get_count_info (stmt, &old_info);
  661. gcov_type total = 0;
  662. for (icall_target_map::const_iterator iter = old_info.targets.begin ();
  663. iter != old_info.targets.end (); ++iter)
  664. total += iter->second;
  665. /* Program behavior changed, original promoted (and inlined) target is not
  666. hot any more. Will avoid promote the original target.
  667. To check if original promoted target is still hot, we check the total
  668. count of the unpromoted targets (stored in old_info). If it is no less
  669. than half of the callsite count (stored in INFO), the original promoted
  670. target is considered not hot any more. */
  671. if (total >= info->count / 2)
  672. return false;
  673. inline_stack stack;
  674. get_inline_stack (gimple_location (stmt), &stack);
  675. if (stack.length () == 0)
  676. return false;
  677. function_instance *s = get_function_instance_by_inline_stack (stack);
  678. if (s == NULL)
  679. return false;
  680. icall_target_map map;
  681. if (s->find_icall_target_map (stmt, &map) == 0)
  682. return false;
  683. for (icall_target_map::const_iterator iter = map.begin ();
  684. iter != map.end (); ++iter)
  685. info->targets[iter->first] = iter->second;
  686. return true;
  687. }
  688. /* Find total count of the callee of EDGE. */
  689. gcov_type
  690. autofdo_source_profile::get_callsite_total_count (
  691. struct cgraph_edge *edge) const
  692. {
  693. inline_stack stack;
  694. stack.safe_push (std::make_pair (edge->callee->decl, 0));
  695. get_inline_stack (gimple_location (edge->call_stmt), &stack);
  696. function_instance *s = get_function_instance_by_inline_stack (stack);
  697. if (s == NULL
  698. || afdo_string_table->get_index (IDENTIFIER_POINTER (
  699. DECL_ASSEMBLER_NAME (edge->callee->decl))) != s->name ())
  700. return 0;
  701. return s->total_count ();
  702. }
  703. /* Read AutoFDO profile and returns TRUE on success. */
  704. /* source profile format:
  705. GCOV_TAG_AFDO_FUNCTION: 4 bytes
  706. LENGTH: 4 bytes
  707. NUM_FUNCTIONS: 4 bytes
  708. FUNCTION_INSTANCE_1
  709. FUNCTION_INSTANCE_2
  710. ...
  711. FUNCTION_INSTANCE_N. */
  712. bool
  713. autofdo_source_profile::read ()
  714. {
  715. if (gcov_read_unsigned () != GCOV_TAG_AFDO_FUNCTION)
  716. {
  717. inform (0, "Not expected TAG.");
  718. return false;
  719. }
  720. /* Skip the length of the section. */
  721. gcov_read_unsigned ();
  722. /* Read in the function/callsite profile, and store it in local
  723. data structure. */
  724. unsigned function_num = gcov_read_unsigned ();
  725. for (unsigned i = 0; i < function_num; i++)
  726. {
  727. function_instance::function_instance_stack stack;
  728. function_instance *s = function_instance::read_function_instance (
  729. &stack, gcov_read_counter ());
  730. afdo_profile_info->sum_all += s->total_count ();
  731. map_[s->name ()] = s;
  732. }
  733. return true;
  734. }
  735. /* Return the function_instance in the profile that correspond to the
  736. inline STACK. */
  737. function_instance *
  738. autofdo_source_profile::get_function_instance_by_inline_stack (
  739. const inline_stack &stack) const
  740. {
  741. name_function_instance_map::const_iterator iter = map_.find (
  742. afdo_string_table->get_index_by_decl (stack[stack.length () - 1].first));
  743. if (iter == map_.end())
  744. return NULL;
  745. function_instance *s = iter->second;
  746. for (unsigned i = stack.length() - 1; i > 0; i--)
  747. {
  748. s = s->get_function_instance_by_decl (
  749. stack[i].second, stack[i - 1].first);
  750. if (s == NULL)
  751. return NULL;
  752. }
  753. return s;
  754. }
  755. /* Module profile is only used by LIPO. Here we simply ignore it. */
  756. static void
  757. fake_read_autofdo_module_profile ()
  758. {
  759. /* Read in the module info. */
  760. gcov_read_unsigned ();
  761. /* Skip the length of the section. */
  762. gcov_read_unsigned ();
  763. /* Read in the file name table. */
  764. unsigned total_module_num = gcov_read_unsigned ();
  765. gcc_assert (total_module_num == 0);
  766. }
  767. /* Read data from profile data file. */
  768. static void
  769. read_profile (void)
  770. {
  771. if (gcov_open (auto_profile_file, 1) == 0)
  772. error ("Cannot open profile file %s.", auto_profile_file);
  773. if (gcov_read_unsigned () != GCOV_DATA_MAGIC)
  774. error ("AutoFDO profile magic number does not mathch.");
  775. /* Skip the version number. */
  776. unsigned version = gcov_read_unsigned ();
  777. if (version != AUTO_PROFILE_VERSION)
  778. error ("AutoFDO profile version %u does match %u.",
  779. version, AUTO_PROFILE_VERSION);
  780. /* Skip the empty integer. */
  781. gcov_read_unsigned ();
  782. /* string_table. */
  783. afdo_string_table = new string_table ();
  784. if (!afdo_string_table->read())
  785. error ("Cannot read string table from %s.", auto_profile_file);
  786. /* autofdo_source_profile. */
  787. afdo_source_profile = autofdo_source_profile::create ();
  788. if (afdo_source_profile == NULL)
  789. error ("Cannot read function profile from %s.", auto_profile_file);
  790. /* autofdo_module_profile. */
  791. fake_read_autofdo_module_profile ();
  792. /* Read in the working set. */
  793. if (gcov_read_unsigned () != GCOV_TAG_AFDO_WORKING_SET)
  794. error ("Cannot read working set from %s.", auto_profile_file);
  795. /* Skip the length of the section. */
  796. gcov_read_unsigned ();
  797. gcov_working_set_t set[128];
  798. for (unsigned i = 0; i < 128; i++)
  799. {
  800. set[i].num_counters = gcov_read_unsigned ();
  801. set[i].min_counter = gcov_read_counter ();
  802. }
  803. add_working_set (set);
  804. }
  805. /* From AutoFDO profiles, find values inside STMT for that we want to measure
  806. histograms for indirect-call optimization.
  807. This function is actually served for 2 purposes:
  808. * before annotation, we need to mark histogram, promote and inline
  809. * after annotation, we just need to mark, and let follow-up logic to
  810. decide if it needs to promote and inline. */
  811. static void
  812. afdo_indirect_call (gimple_stmt_iterator *gsi, const icall_target_map &map,
  813. bool transform)
  814. {
  815. gimple gs = gsi_stmt (*gsi);
  816. tree callee;
  817. if (map.size () == 0)
  818. return;
  819. gcall *stmt = dyn_cast <gcall *> (gs);
  820. if ((!stmt) || gimple_call_fndecl (stmt) != NULL_TREE)
  821. return;
  822. callee = gimple_call_fn (stmt);
  823. histogram_value hist = gimple_alloc_histogram_value (
  824. cfun, HIST_TYPE_INDIR_CALL, stmt, callee);
  825. hist->n_counters = 3;
  826. hist->hvalue.counters = XNEWVEC (gcov_type, hist->n_counters);
  827. gimple_add_histogram_value (cfun, stmt, hist);
  828. gcov_type total = 0;
  829. icall_target_map::const_iterator max_iter = map.end ();
  830. for (icall_target_map::const_iterator iter = map.begin ();
  831. iter != map.end (); ++iter)
  832. {
  833. total += iter->second;
  834. if (max_iter == map.end () || max_iter->second < iter->second)
  835. max_iter = iter;
  836. }
  837. hist->hvalue.counters[0]
  838. = (unsigned long long)afdo_string_table->get_name (max_iter->first);
  839. hist->hvalue.counters[1] = max_iter->second;
  840. hist->hvalue.counters[2] = total;
  841. if (!transform)
  842. return;
  843. struct cgraph_edge *indirect_edge
  844. = cgraph_node::get (current_function_decl)->get_edge (stmt);
  845. struct cgraph_node *direct_call = cgraph_node::get_for_asmname (
  846. get_identifier ((const char *) hist->hvalue.counters[0]));
  847. if (direct_call == NULL || !check_ic_target (stmt, direct_call))
  848. return;
  849. if (DECL_STRUCT_FUNCTION (direct_call->decl) == NULL)
  850. return;
  851. struct cgraph_edge *new_edge
  852. = indirect_edge->make_speculative (direct_call, 0, 0);
  853. new_edge->redirect_call_stmt_to_callee ();
  854. gimple_remove_histogram_value (cfun, stmt, hist);
  855. inline_call (new_edge, true, NULL, NULL, false);
  856. }
  857. /* From AutoFDO profiles, find values inside STMT for that we want to measure
  858. histograms and adds them to list VALUES. */
  859. static void
  860. afdo_vpt (gimple_stmt_iterator *gsi, const icall_target_map &map,
  861. bool transform)
  862. {
  863. afdo_indirect_call (gsi, map, transform);
  864. }
  865. typedef std::set<basic_block> bb_set;
  866. typedef std::set<edge> edge_set;
  867. static bool
  868. is_bb_annotated (const basic_block bb, const bb_set &annotated)
  869. {
  870. return annotated.find (bb) != annotated.end ();
  871. }
  872. static void
  873. set_bb_annotated (basic_block bb, bb_set *annotated)
  874. {
  875. annotated->insert (bb);
  876. }
  877. static bool
  878. is_edge_annotated (const edge e, const edge_set &annotated)
  879. {
  880. return annotated.find (e) != annotated.end ();
  881. }
  882. static void
  883. set_edge_annotated (edge e, edge_set *annotated)
  884. {
  885. annotated->insert (e);
  886. }
  887. /* For a given BB, set its execution count. Attach value profile if a stmt
  888. is not in PROMOTED, because we only want to promote an indirect call once.
  889. Return TRUE if BB is annotated. */
  890. static bool
  891. afdo_set_bb_count (basic_block bb, const stmt_set &promoted)
  892. {
  893. gimple_stmt_iterator gsi;
  894. edge e;
  895. edge_iterator ei;
  896. gcov_type max_count = 0;
  897. bool has_annotated = false;
  898. for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  899. {
  900. count_info info;
  901. gimple stmt = gsi_stmt (gsi);
  902. if (gimple_clobber_p (stmt) || is_gimple_debug (stmt))
  903. continue;
  904. if (afdo_source_profile->get_count_info (stmt, &info))
  905. {
  906. if (info.count > max_count)
  907. max_count = info.count;
  908. has_annotated = true;
  909. if (info.targets.size () > 0
  910. && promoted.find (stmt) == promoted.end ())
  911. afdo_vpt (&gsi, info.targets, false);
  912. }
  913. }
  914. if (!has_annotated)
  915. return false;
  916. for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  917. afdo_source_profile->mark_annotated (gimple_location (gsi_stmt (gsi)));
  918. for (gphi_iterator gpi = gsi_start_phis (bb);
  919. !gsi_end_p (gpi);
  920. gsi_next (&gpi))
  921. {
  922. gphi *phi = gpi.phi ();
  923. size_t i;
  924. for (i = 0; i < gimple_phi_num_args (phi); i++)
  925. afdo_source_profile->mark_annotated (gimple_phi_arg_location (phi, i));
  926. }
  927. FOR_EACH_EDGE (e, ei, bb->succs)
  928. afdo_source_profile->mark_annotated (e->goto_locus);
  929. bb->count = max_count;
  930. return true;
  931. }
  932. /* BB1 and BB2 are in an equivalent class iff:
  933. 1. BB1 dominates BB2.
  934. 2. BB2 post-dominates BB1.
  935. 3. BB1 and BB2 are in the same loop nest.
  936. This function finds the equivalent class for each basic block, and
  937. stores a pointer to the first BB in its equivalent class. Meanwhile,
  938. set bb counts for the same equivalent class to be idenical. Update
  939. ANNOTATED_BB for the first BB in its equivalent class. */
  940. static void
  941. afdo_find_equiv_class (bb_set *annotated_bb)
  942. {
  943. basic_block bb;
  944. FOR_ALL_BB_FN (bb, cfun)
  945. bb->aux = NULL;
  946. FOR_ALL_BB_FN (bb, cfun)
  947. {
  948. vec<basic_block> dom_bbs;
  949. basic_block bb1;
  950. int i;
  951. if (bb->aux != NULL)
  952. continue;
  953. bb->aux = bb;
  954. dom_bbs = get_dominated_by (CDI_DOMINATORS, bb);
  955. FOR_EACH_VEC_ELT (dom_bbs, i, bb1)
  956. if (bb1->aux == NULL && dominated_by_p (CDI_POST_DOMINATORS, bb, bb1)
  957. && bb1->loop_father == bb->loop_father)
  958. {
  959. bb1->aux = bb;
  960. if (bb1->count > bb->count && is_bb_annotated (bb1, *annotated_bb))
  961. {
  962. bb->count = bb1->count;
  963. set_bb_annotated (bb, annotated_bb);
  964. }
  965. }
  966. dom_bbs = get_dominated_by (CDI_POST_DOMINATORS, bb);
  967. FOR_EACH_VEC_ELT (dom_bbs, i, bb1)
  968. if (bb1->aux == NULL && dominated_by_p (CDI_DOMINATORS, bb, bb1)
  969. && bb1->loop_father == bb->loop_father)
  970. {
  971. bb1->aux = bb;
  972. if (bb1->count > bb->count && is_bb_annotated (bb1, *annotated_bb))
  973. {
  974. bb->count = bb1->count;
  975. set_bb_annotated (bb, annotated_bb);
  976. }
  977. }
  978. }
  979. }
  980. /* If a basic block's count is known, and only one of its in/out edges' count
  981. is unknown, its count can be calculated. Meanwhile, if all of the in/out
  982. edges' counts are known, then the basic block's unknown count can also be
  983. calculated.
  984. IS_SUCC is true if out edges of a basic blocks are examined.
  985. Update ANNOTATED_BB and ANNOTATED_EDGE accordingly.
  986. Return TRUE if any basic block/edge count is changed. */
  987. static bool
  988. afdo_propagate_edge (bool is_succ, bb_set *annotated_bb,
  989. edge_set *annotated_edge)
  990. {
  991. basic_block bb;
  992. bool changed = false;
  993. FOR_EACH_BB_FN (bb, cfun)
  994. {
  995. edge e, unknown_edge = NULL;
  996. edge_iterator ei;
  997. int num_unknown_edge = 0;
  998. gcov_type total_known_count = 0;
  999. FOR_EACH_EDGE (e, ei, is_succ ? bb->succs : bb->preds)
  1000. if (!is_edge_annotated (e, *annotated_edge))
  1001. num_unknown_edge++, unknown_edge = e;
  1002. else
  1003. total_known_count += e->count;
  1004. if (num_unknown_edge == 0)
  1005. {
  1006. if (total_known_count > bb->count)
  1007. {
  1008. bb->count = total_known_count;
  1009. changed = true;
  1010. }
  1011. if (!is_bb_annotated (bb, *annotated_bb))
  1012. {
  1013. set_bb_annotated (bb, annotated_bb);
  1014. changed = true;
  1015. }
  1016. }
  1017. else if (num_unknown_edge == 1 && is_bb_annotated (bb, *annotated_bb))
  1018. {
  1019. if (bb->count >= total_known_count)
  1020. unknown_edge->count = bb->count - total_known_count;
  1021. else
  1022. unknown_edge->count = 0;
  1023. set_edge_annotated (unknown_edge, annotated_edge);
  1024. changed = true;
  1025. }
  1026. }
  1027. return changed;
  1028. }
  1029. /* Special propagation for circuit expressions. Because GCC translates
  1030. control flow into data flow for circuit expressions. E.g.
  1031. BB1:
  1032. if (a && b)
  1033. BB2
  1034. else
  1035. BB3
  1036. will be translated into:
  1037. BB1:
  1038. if (a)
  1039. goto BB.t1
  1040. else
  1041. goto BB.t3
  1042. BB.t1:
  1043. if (b)
  1044. goto BB.t2
  1045. else
  1046. goto BB.t3
  1047. BB.t2:
  1048. goto BB.t3
  1049. BB.t3:
  1050. tmp = PHI (0 (BB1), 0 (BB.t1), 1 (BB.t2)
  1051. if (tmp)
  1052. goto BB2
  1053. else
  1054. goto BB3
  1055. In this case, we need to propagate through PHI to determine the edge
  1056. count of BB1->BB.t1, BB.t1->BB.t2.
  1057. Update ANNOTATED_EDGE accordingly. */
  1058. static void
  1059. afdo_propagate_circuit (const bb_set &annotated_bb, edge_set *annotated_edge)
  1060. {
  1061. basic_block bb;
  1062. FOR_ALL_BB_FN (bb, cfun)
  1063. {
  1064. gimple def_stmt;
  1065. tree cmp_rhs, cmp_lhs;
  1066. gimple cmp_stmt = last_stmt (bb);
  1067. edge e;
  1068. edge_iterator ei;
  1069. if (!cmp_stmt || gimple_code (cmp_stmt) != GIMPLE_COND)
  1070. continue;
  1071. cmp_rhs = gimple_cond_rhs (cmp_stmt);
  1072. cmp_lhs = gimple_cond_lhs (cmp_stmt);
  1073. if (!TREE_CONSTANT (cmp_rhs)
  1074. || !(integer_zerop (cmp_rhs) || integer_onep (cmp_rhs)))
  1075. continue;
  1076. if (TREE_CODE (cmp_lhs) != SSA_NAME)
  1077. continue;
  1078. if (!is_bb_annotated (bb, annotated_bb))
  1079. continue;
  1080. def_stmt = SSA_NAME_DEF_STMT (cmp_lhs);
  1081. while (def_stmt && gimple_code (def_stmt) == GIMPLE_ASSIGN
  1082. && gimple_assign_single_p (def_stmt)
  1083. && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME)
  1084. def_stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (def_stmt));
  1085. if (!def_stmt)
  1086. continue;
  1087. gphi *phi_stmt = dyn_cast <gphi *> (def_stmt);
  1088. if (!phi_stmt)
  1089. continue;
  1090. FOR_EACH_EDGE (e, ei, bb->succs)
  1091. {
  1092. unsigned i, total = 0;
  1093. edge only_one;
  1094. bool check_value_one = (((integer_onep (cmp_rhs))
  1095. ^ (gimple_cond_code (cmp_stmt) == EQ_EXPR))
  1096. ^ ((e->flags & EDGE_TRUE_VALUE) != 0));
  1097. if (!is_edge_annotated (e, *annotated_edge))
  1098. continue;
  1099. for (i = 0; i < gimple_phi_num_args (phi_stmt); i++)
  1100. {
  1101. tree val = gimple_phi_arg_def (phi_stmt, i);
  1102. edge ep = gimple_phi_arg_edge (phi_stmt, i);
  1103. if (!TREE_CONSTANT (val)
  1104. || !(integer_zerop (val) || integer_onep (val)))
  1105. continue;
  1106. if (check_value_one ^ integer_onep (val))
  1107. continue;
  1108. total++;
  1109. only_one = ep;
  1110. if (e->probability == 0 && !is_edge_annotated (ep, *annotated_edge))
  1111. {
  1112. ep->probability = 0;
  1113. ep->count = 0;
  1114. set_edge_annotated (ep, annotated_edge);
  1115. }
  1116. }
  1117. if (total == 1 && !is_edge_annotated (only_one, *annotated_edge))
  1118. {
  1119. only_one->probability = e->probability;
  1120. only_one->count = e->count;
  1121. set_edge_annotated (only_one, annotated_edge);
  1122. }
  1123. }
  1124. }
  1125. }
  1126. /* Propagate the basic block count and edge count on the control flow
  1127. graph. We do the propagation iteratively until stablize. */
  1128. static void
  1129. afdo_propagate (bb_set *annotated_bb, edge_set *annotated_edge)
  1130. {
  1131. basic_block bb;
  1132. bool changed = true;
  1133. int i = 0;
  1134. FOR_ALL_BB_FN (bb, cfun)
  1135. {
  1136. bb->count = ((basic_block)bb->aux)->count;
  1137. if (is_bb_annotated ((const basic_block)bb->aux, *annotated_bb))
  1138. set_bb_annotated (bb, annotated_bb);
  1139. }
  1140. while (changed && i++ < 10)
  1141. {
  1142. changed = false;
  1143. if (afdo_propagate_edge (true, annotated_bb, annotated_edge))
  1144. changed = true;
  1145. if (afdo_propagate_edge (false, annotated_bb, annotated_edge))
  1146. changed = true;
  1147. afdo_propagate_circuit (*annotated_bb, annotated_edge);
  1148. }
  1149. }
  1150. /* Propagate counts on control flow graph and calculate branch
  1151. probabilities. */
  1152. static void
  1153. afdo_calculate_branch_prob (bb_set *annotated_bb, edge_set *annotated_edge)
  1154. {
  1155. basic_block bb;
  1156. bool has_sample = false;
  1157. FOR_EACH_BB_FN (bb, cfun)
  1158. if (bb->count > 0)
  1159. has_sample = true;
  1160. if (!has_sample)
  1161. return;
  1162. calculate_dominance_info (CDI_POST_DOMINATORS);
  1163. calculate_dominance_info (CDI_DOMINATORS);
  1164. loop_optimizer_init (0);
  1165. afdo_find_equiv_class (annotated_bb);
  1166. afdo_propagate (annotated_bb, annotated_edge);
  1167. FOR_EACH_BB_FN (bb, cfun)
  1168. {
  1169. edge e;
  1170. edge_iterator ei;
  1171. int num_unknown_succ = 0;
  1172. gcov_type total_count = 0;
  1173. FOR_EACH_EDGE (e, ei, bb->succs)
  1174. {
  1175. if (!is_edge_annotated (e, *annotated_edge))
  1176. num_unknown_succ++;
  1177. else
  1178. total_count += e->count;
  1179. }
  1180. if (num_unknown_succ == 0 && total_count > 0)
  1181. {
  1182. FOR_EACH_EDGE (e, ei, bb->succs)
  1183. e->probability = (double)e->count * REG_BR_PROB_BASE / total_count;
  1184. }
  1185. }
  1186. FOR_ALL_BB_FN (bb, cfun)
  1187. {
  1188. edge e;
  1189. edge_iterator ei;
  1190. FOR_EACH_EDGE (e, ei, bb->succs)
  1191. e->count = (double)bb->count * e->probability / REG_BR_PROB_BASE;
  1192. bb->aux = NULL;
  1193. }
  1194. loop_optimizer_finalize ();
  1195. free_dominance_info (CDI_DOMINATORS);
  1196. free_dominance_info (CDI_POST_DOMINATORS);
  1197. }
  1198. /* Perform value profile transformation using AutoFDO profile. Add the
  1199. promoted stmts to PROMOTED_STMTS. Return TRUE if there is any
  1200. indirect call promoted. */
  1201. static bool
  1202. afdo_vpt_for_early_inline (stmt_set *promoted_stmts)
  1203. {
  1204. basic_block bb;
  1205. if (afdo_source_profile->get_function_instance_by_decl (
  1206. current_function_decl) == NULL)
  1207. return false;
  1208. compute_inline_parameters (cgraph_node::get (current_function_decl), true);
  1209. bool has_vpt = false;
  1210. FOR_EACH_BB_FN (bb, cfun)
  1211. {
  1212. if (!has_indirect_call (bb))
  1213. continue;
  1214. gimple_stmt_iterator gsi;
  1215. gcov_type bb_count = 0;
  1216. for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  1217. {
  1218. count_info info;
  1219. gimple stmt = gsi_stmt (gsi);
  1220. if (afdo_source_profile->get_count_info (stmt, &info))
  1221. bb_count = MAX (bb_count, info.count);
  1222. }
  1223. for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  1224. {
  1225. gcall *stmt = dyn_cast <gcall *> (gsi_stmt (gsi));
  1226. /* IC_promotion and early_inline_2 is done in multiple iterations.
  1227. No need to promoted the stmt if its in promoted_stmts (means
  1228. it is already been promoted in the previous iterations). */
  1229. if ((!stmt) || gimple_call_fn (stmt) == NULL
  1230. || TREE_CODE (gimple_call_fn (stmt)) == FUNCTION_DECL
  1231. || promoted_stmts->find (stmt) != promoted_stmts->end ())
  1232. continue;
  1233. count_info info;
  1234. afdo_source_profile->get_count_info (stmt, &info);
  1235. info.count = bb_count;
  1236. if (afdo_source_profile->update_inlined_ind_target (stmt, &info))
  1237. {
  1238. /* Promote the indirect call and update the promoted_stmts. */
  1239. promoted_stmts->insert (stmt);
  1240. afdo_vpt (&gsi, info.targets, true);
  1241. has_vpt = true;
  1242. }
  1243. }
  1244. }
  1245. if (has_vpt)
  1246. {
  1247. optimize_inline_calls (current_function_decl);
  1248. return true;
  1249. }
  1250. return false;
  1251. }
  1252. /* Annotate auto profile to the control flow graph. Do not annotate value
  1253. profile for stmts in PROMOTED_STMTS. */
  1254. static void
  1255. afdo_annotate_cfg (const stmt_set &promoted_stmts)
  1256. {
  1257. basic_block bb;
  1258. bb_set annotated_bb;
  1259. edge_set annotated_edge;
  1260. const function_instance *s
  1261. = afdo_source_profile->get_function_instance_by_decl (
  1262. current_function_decl);
  1263. if (s == NULL)
  1264. return;
  1265. cgraph_node::get (current_function_decl)->count = s->head_count ();
  1266. ENTRY_BLOCK_PTR_FOR_FN (cfun)->count = s->head_count ();
  1267. gcov_type max_count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
  1268. FOR_EACH_BB_FN (bb, cfun)
  1269. {
  1270. edge e;
  1271. edge_iterator ei;
  1272. bb->count = 0;
  1273. FOR_EACH_EDGE (e, ei, bb->succs)
  1274. e->count = 0;
  1275. if (afdo_set_bb_count (bb, promoted_stmts))
  1276. set_bb_annotated (bb, &annotated_bb);
  1277. if (bb->count > max_count)
  1278. max_count = bb->count;
  1279. }
  1280. if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count
  1281. > ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->count)
  1282. {
  1283. ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->count
  1284. = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
  1285. set_bb_annotated (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb, &annotated_bb);
  1286. }
  1287. if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count
  1288. > EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb->count)
  1289. {
  1290. EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb->count
  1291. = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
  1292. set_bb_annotated (EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb, &annotated_bb);
  1293. }
  1294. afdo_source_profile->mark_annotated (
  1295. DECL_SOURCE_LOCATION (current_function_decl));
  1296. afdo_source_profile->mark_annotated (cfun->function_start_locus);
  1297. afdo_source_profile->mark_annotated (cfun->function_end_locus);
  1298. if (max_count > 0)
  1299. {
  1300. afdo_calculate_branch_prob (&annotated_bb, &annotated_edge);
  1301. counts_to_freqs ();
  1302. profile_status_for_fn (cfun) = PROFILE_READ;
  1303. }
  1304. if (flag_value_profile_transformations)
  1305. {
  1306. gimple_value_profile_transformations ();
  1307. free_dominance_info (CDI_DOMINATORS);
  1308. free_dominance_info (CDI_POST_DOMINATORS);
  1309. update_ssa (TODO_update_ssa);
  1310. }
  1311. }
  1312. /* Wrapper function to invoke early inliner. */
  1313. static void
  1314. early_inline ()
  1315. {
  1316. compute_inline_parameters (cgraph_node::get (current_function_decl), true);
  1317. unsigned todo = early_inliner (cfun);
  1318. if (todo & TODO_update_ssa_any)
  1319. update_ssa (TODO_update_ssa);
  1320. }
  1321. /* Use AutoFDO profile to annoate the control flow graph.
  1322. Return the todo flag. */
  1323. static unsigned int
  1324. auto_profile (void)
  1325. {
  1326. struct cgraph_node *node;
  1327. if (symtab->state == FINISHED)
  1328. return 0;
  1329. init_node_map (true);
  1330. profile_info = autofdo::afdo_profile_info;
  1331. FOR_EACH_FUNCTION (node)
  1332. {
  1333. if (!gimple_has_body_p (node->decl))
  1334. continue;
  1335. /* Don't profile functions produced for builtin stuff. */
  1336. if (DECL_SOURCE_LOCATION (node->decl) == BUILTINS_LOCATION)
  1337. continue;
  1338. push_cfun (DECL_STRUCT_FUNCTION (node->decl));
  1339. /* First do indirect call promotion and early inline to make the
  1340. IR match the profiled binary before actual annotation.
  1341. This is needed because an indirect call might have been promoted
  1342. and inlined in the profiled binary. If we do not promote and
  1343. inline these indirect calls before annotation, the profile for
  1344. these promoted functions will be lost.
  1345. e.g. foo() --indirect_call--> bar()
  1346. In profiled binary, the callsite is promoted and inlined, making
  1347. the profile look like:
  1348. foo: {
  1349. loc_foo_1: count_1
  1350. bar@loc_foo_2: {
  1351. loc_bar_1: count_2
  1352. loc_bar_2: count_3
  1353. }
  1354. }
  1355. Before AutoFDO pass, loc_foo_2 is not promoted thus not inlined.
  1356. If we perform annotation on it, the profile inside bar@loc_foo2
  1357. will be wasted.
  1358. To avoid this, we promote loc_foo_2 and inline the promoted bar
  1359. function before annotation, so the profile inside bar@loc_foo2
  1360. will be useful. */
  1361. autofdo::stmt_set promoted_stmts;
  1362. for (int i = 0; i < PARAM_VALUE (PARAM_EARLY_INLINER_MAX_ITERATIONS); i++)
  1363. {
  1364. if (!flag_value_profile_transformations
  1365. || !autofdo::afdo_vpt_for_early_inline (&promoted_stmts))
  1366. break;
  1367. early_inline ();
  1368. }
  1369. early_inline ();
  1370. autofdo::afdo_annotate_cfg (promoted_stmts);
  1371. compute_function_frequency ();
  1372. /* Local pure-const may imply need to fixup the cfg. */
  1373. if (execute_fixup_cfg () & TODO_cleanup_cfg)
  1374. cleanup_tree_cfg ();
  1375. free_dominance_info (CDI_DOMINATORS);
  1376. free_dominance_info (CDI_POST_DOMINATORS);
  1377. cgraph_edge::rebuild_edges ();
  1378. compute_inline_parameters (cgraph_node::get (current_function_decl), true);
  1379. pop_cfun ();
  1380. }
  1381. return TODO_rebuild_cgraph_edges;
  1382. }
  1383. } /* namespace autofdo. */
  1384. /* Read the profile from the profile data file. */
  1385. void
  1386. read_autofdo_file (void)
  1387. {
  1388. if (auto_profile_file == NULL)
  1389. auto_profile_file = DEFAULT_AUTO_PROFILE_FILE;
  1390. autofdo::afdo_profile_info = (struct gcov_ctr_summary *)xcalloc (
  1391. 1, sizeof (struct gcov_ctr_summary));
  1392. autofdo::afdo_profile_info->runs = 1;
  1393. autofdo::afdo_profile_info->sum_max = 0;
  1394. autofdo::afdo_profile_info->sum_all = 0;
  1395. /* Read the profile from the profile file. */
  1396. autofdo::read_profile ();
  1397. }
  1398. /* Free the resources. */
  1399. void
  1400. end_auto_profile (void)
  1401. {
  1402. delete autofdo::afdo_source_profile;
  1403. delete autofdo::afdo_string_table;
  1404. profile_info = NULL;
  1405. }
  1406. /* Returns TRUE if EDGE is hot enough to be inlined early. */
  1407. bool
  1408. afdo_callsite_hot_enough_for_early_inline (struct cgraph_edge *edge)
  1409. {
  1410. gcov_type count
  1411. = autofdo::afdo_source_profile->get_callsite_total_count (edge);
  1412. if (count > 0)
  1413. {
  1414. bool is_hot;
  1415. const struct gcov_ctr_summary *saved_profile_info = profile_info;
  1416. /* At early inline stage, profile_info is not set yet. We need to
  1417. temporarily set it to afdo_profile_info to calculate hotness. */
  1418. profile_info = autofdo::afdo_profile_info;
  1419. is_hot = maybe_hot_count_p (NULL, count);
  1420. profile_info = saved_profile_info;
  1421. return is_hot;
  1422. }
  1423. return false;
  1424. }
  1425. namespace
  1426. {
  1427. const pass_data pass_data_ipa_auto_profile = {
  1428. SIMPLE_IPA_PASS, "afdo", /* name */
  1429. OPTGROUP_NONE, /* optinfo_flags */
  1430. TV_IPA_AUTOFDO, /* tv_id */
  1431. 0, /* properties_required */
  1432. 0, /* properties_provided */
  1433. 0, /* properties_destroyed */
  1434. 0, /* todo_flags_start */
  1435. 0, /* todo_flags_finish */
  1436. };
  1437. class pass_ipa_auto_profile : public simple_ipa_opt_pass
  1438. {
  1439. public:
  1440. pass_ipa_auto_profile (gcc::context *ctxt)
  1441. : simple_ipa_opt_pass (pass_data_ipa_auto_profile, ctxt)
  1442. {
  1443. }
  1444. /* opt_pass methods: */
  1445. virtual bool
  1446. gate (function *)
  1447. {
  1448. return flag_auto_profile;
  1449. }
  1450. virtual unsigned int
  1451. execute (function *)
  1452. {
  1453. return autofdo::auto_profile ();
  1454. }
  1455. }; // class pass_ipa_auto_profile
  1456. } // anon namespace
  1457. simple_ipa_opt_pass *
  1458. make_pass_ipa_auto_profile (gcc::context *ctxt)
  1459. {
  1460. return new pass_ipa_auto_profile (ctxt);
  1461. }