ipa-inline.c 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862
  1. /* Inlining decision heuristics.
  2. Copyright (C) 2003-2015 Free Software Foundation, Inc.
  3. Contributed by Jan Hubicka
  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. /* Inlining decision heuristics
  17. The implementation of inliner is organized as follows:
  18. inlining heuristics limits
  19. can_inline_edge_p allow to check that particular inlining is allowed
  20. by the limits specified by user (allowed function growth, growth and so
  21. on).
  22. Functions are inlined when it is obvious the result is profitable (such
  23. as functions called once or when inlining reduce code size).
  24. In addition to that we perform inlining of small functions and recursive
  25. inlining.
  26. inlining heuristics
  27. The inliner itself is split into two passes:
  28. pass_early_inlining
  29. Simple local inlining pass inlining callees into current function.
  30. This pass makes no use of whole unit analysis and thus it can do only
  31. very simple decisions based on local properties.
  32. The strength of the pass is that it is run in topological order
  33. (reverse postorder) on the callgraph. Functions are converted into SSA
  34. form just before this pass and optimized subsequently. As a result, the
  35. callees of the function seen by the early inliner was already optimized
  36. and results of early inlining adds a lot of optimization opportunities
  37. for the local optimization.
  38. The pass handle the obvious inlining decisions within the compilation
  39. unit - inlining auto inline functions, inlining for size and
  40. flattening.
  41. main strength of the pass is the ability to eliminate abstraction
  42. penalty in C++ code (via combination of inlining and early
  43. optimization) and thus improve quality of analysis done by real IPA
  44. optimizers.
  45. Because of lack of whole unit knowledge, the pass can not really make
  46. good code size/performance tradeoffs. It however does very simple
  47. speculative inlining allowing code size to grow by
  48. EARLY_INLINING_INSNS when callee is leaf function. In this case the
  49. optimizations performed later are very likely to eliminate the cost.
  50. pass_ipa_inline
  51. This is the real inliner able to handle inlining with whole program
  52. knowledge. It performs following steps:
  53. 1) inlining of small functions. This is implemented by greedy
  54. algorithm ordering all inlinable cgraph edges by their badness and
  55. inlining them in this order as long as inline limits allows doing so.
  56. This heuristics is not very good on inlining recursive calls. Recursive
  57. calls can be inlined with results similar to loop unrolling. To do so,
  58. special purpose recursive inliner is executed on function when
  59. recursive edge is met as viable candidate.
  60. 2) Unreachable functions are removed from callgraph. Inlining leads
  61. to devirtualization and other modification of callgraph so functions
  62. may become unreachable during the process. Also functions declared as
  63. extern inline or virtual functions are removed, since after inlining
  64. we no longer need the offline bodies.
  65. 3) Functions called once and not exported from the unit are inlined.
  66. This should almost always lead to reduction of code size by eliminating
  67. the need for offline copy of the function. */
  68. #include "config.h"
  69. #include "system.h"
  70. #include "coretypes.h"
  71. #include "tm.h"
  72. #include "hash-set.h"
  73. #include "machmode.h"
  74. #include "vec.h"
  75. #include "double-int.h"
  76. #include "input.h"
  77. #include "alias.h"
  78. #include "symtab.h"
  79. #include "wide-int.h"
  80. #include "inchash.h"
  81. #include "tree.h"
  82. #include "fold-const.h"
  83. #include "trans-mem.h"
  84. #include "calls.h"
  85. #include "tree-inline.h"
  86. #include "langhooks.h"
  87. #include "flags.h"
  88. #include "diagnostic.h"
  89. #include "gimple-pretty-print.h"
  90. #include "params.h"
  91. #include "intl.h"
  92. #include "tree-pass.h"
  93. #include "coverage.h"
  94. #include "rtl.h"
  95. #include "bitmap.h"
  96. #include "profile.h"
  97. #include "predict.h"
  98. #include "hard-reg-set.h"
  99. #include "input.h"
  100. #include "function.h"
  101. #include "basic-block.h"
  102. #include "tree-ssa-alias.h"
  103. #include "internal-fn.h"
  104. #include "gimple-expr.h"
  105. #include "is-a.h"
  106. #include "gimple.h"
  107. #include "gimple-ssa.h"
  108. #include "hash-map.h"
  109. #include "plugin-api.h"
  110. #include "ipa-ref.h"
  111. #include "cgraph.h"
  112. #include "alloc-pool.h"
  113. #include "symbol-summary.h"
  114. #include "ipa-prop.h"
  115. #include "except.h"
  116. #include "target.h"
  117. #include "ipa-inline.h"
  118. #include "ipa-utils.h"
  119. #include "sreal.h"
  120. #include "auto-profile.h"
  121. #include "builtins.h"
  122. #include "fibonacci_heap.h"
  123. #include "lto-streamer.h"
  124. typedef fibonacci_heap <sreal, cgraph_edge> edge_heap_t;
  125. typedef fibonacci_node <sreal, cgraph_edge> edge_heap_node_t;
  126. /* Statistics we collect about inlining algorithm. */
  127. static int overall_size;
  128. static gcov_type max_count;
  129. static gcov_type spec_rem;
  130. /* Pre-computed constants 1/CGRAPH_FREQ_BASE and 1/100. */
  131. static sreal cgraph_freq_base_rec, percent_rec;
  132. /* Return false when inlining edge E would lead to violating
  133. limits on function unit growth or stack usage growth.
  134. The relative function body growth limit is present generally
  135. to avoid problems with non-linear behavior of the compiler.
  136. To allow inlining huge functions into tiny wrapper, the limit
  137. is always based on the bigger of the two functions considered.
  138. For stack growth limits we always base the growth in stack usage
  139. of the callers. We want to prevent applications from segfaulting
  140. on stack overflow when functions with huge stack frames gets
  141. inlined. */
  142. static bool
  143. caller_growth_limits (struct cgraph_edge *e)
  144. {
  145. struct cgraph_node *to = e->caller;
  146. struct cgraph_node *what = e->callee->ultimate_alias_target ();
  147. int newsize;
  148. int limit = 0;
  149. HOST_WIDE_INT stack_size_limit = 0, inlined_stack;
  150. inline_summary *info, *what_info, *outer_info = inline_summaries->get (to);
  151. /* Look for function e->caller is inlined to. While doing
  152. so work out the largest function body on the way. As
  153. described above, we want to base our function growth
  154. limits based on that. Not on the self size of the
  155. outer function, not on the self size of inline code
  156. we immediately inline to. This is the most relaxed
  157. interpretation of the rule "do not grow large functions
  158. too much in order to prevent compiler from exploding". */
  159. while (true)
  160. {
  161. info = inline_summaries->get (to);
  162. if (limit < info->self_size)
  163. limit = info->self_size;
  164. if (stack_size_limit < info->estimated_self_stack_size)
  165. stack_size_limit = info->estimated_self_stack_size;
  166. if (to->global.inlined_to)
  167. to = to->callers->caller;
  168. else
  169. break;
  170. }
  171. what_info = inline_summaries->get (what);
  172. if (limit < what_info->self_size)
  173. limit = what_info->self_size;
  174. limit += limit * PARAM_VALUE (PARAM_LARGE_FUNCTION_GROWTH) / 100;
  175. /* Check the size after inlining against the function limits. But allow
  176. the function to shrink if it went over the limits by forced inlining. */
  177. newsize = estimate_size_after_inlining (to, e);
  178. if (newsize >= info->size
  179. && newsize > PARAM_VALUE (PARAM_LARGE_FUNCTION_INSNS)
  180. && newsize > limit)
  181. {
  182. e->inline_failed = CIF_LARGE_FUNCTION_GROWTH_LIMIT;
  183. return false;
  184. }
  185. if (!what_info->estimated_stack_size)
  186. return true;
  187. /* FIXME: Stack size limit often prevents inlining in Fortran programs
  188. due to large i/o datastructures used by the Fortran front-end.
  189. We ought to ignore this limit when we know that the edge is executed
  190. on every invocation of the caller (i.e. its call statement dominates
  191. exit block). We do not track this information, yet. */
  192. stack_size_limit += ((gcov_type)stack_size_limit
  193. * PARAM_VALUE (PARAM_STACK_FRAME_GROWTH) / 100);
  194. inlined_stack = (outer_info->stack_frame_offset
  195. + outer_info->estimated_self_stack_size
  196. + what_info->estimated_stack_size);
  197. /* Check new stack consumption with stack consumption at the place
  198. stack is used. */
  199. if (inlined_stack > stack_size_limit
  200. /* If function already has large stack usage from sibling
  201. inline call, we can inline, too.
  202. This bit overoptimistically assume that we are good at stack
  203. packing. */
  204. && inlined_stack > info->estimated_stack_size
  205. && inlined_stack > PARAM_VALUE (PARAM_LARGE_STACK_FRAME))
  206. {
  207. e->inline_failed = CIF_LARGE_STACK_FRAME_GROWTH_LIMIT;
  208. return false;
  209. }
  210. return true;
  211. }
  212. /* Dump info about why inlining has failed. */
  213. static void
  214. report_inline_failed_reason (struct cgraph_edge *e)
  215. {
  216. if (dump_file)
  217. {
  218. fprintf (dump_file, " not inlinable: %s/%i -> %s/%i, %s\n",
  219. xstrdup_for_dump (e->caller->name ()), e->caller->order,
  220. xstrdup_for_dump (e->callee->name ()), e->callee->order,
  221. cgraph_inline_failed_string (e->inline_failed));
  222. if ((e->inline_failed == CIF_TARGET_OPTION_MISMATCH
  223. || e->inline_failed == CIF_OPTIMIZATION_MISMATCH)
  224. && e->caller->lto_file_data
  225. && e->callee->function_symbol ()->lto_file_data)
  226. {
  227. fprintf (dump_file, " LTO objects: %s, %s\n",
  228. e->caller->lto_file_data->file_name,
  229. e->callee->function_symbol ()->lto_file_data->file_name);
  230. }
  231. if (e->inline_failed == CIF_TARGET_OPTION_MISMATCH)
  232. cl_target_option_print_diff
  233. (dump_file, 2, target_opts_for_fn (e->caller->decl),
  234. target_opts_for_fn (e->callee->ultimate_alias_target ()->decl));
  235. if (e->inline_failed == CIF_OPTIMIZATION_MISMATCH)
  236. cl_optimization_print_diff
  237. (dump_file, 2, opts_for_fn (e->caller->decl),
  238. opts_for_fn (e->callee->ultimate_alias_target ()->decl));
  239. }
  240. }
  241. /* Decide whether sanitizer-related attributes allow inlining. */
  242. static bool
  243. sanitize_attrs_match_for_inline_p (const_tree caller, const_tree callee)
  244. {
  245. /* Don't care if sanitizer is disabled */
  246. if (!(flag_sanitize & SANITIZE_ADDRESS))
  247. return true;
  248. if (!caller || !callee)
  249. return true;
  250. return !!lookup_attribute ("no_sanitize_address",
  251. DECL_ATTRIBUTES (caller)) ==
  252. !!lookup_attribute ("no_sanitize_address",
  253. DECL_ATTRIBUTES (callee));
  254. }
  255. /* Used for flags where it is safe to inline when caller's value is
  256. grater than callee's. */
  257. #define check_maybe_up(flag) \
  258. (opts_for_fn (caller->decl)->x_##flag \
  259. != opts_for_fn (callee->decl)->x_##flag \
  260. && (!always_inline \
  261. || opts_for_fn (caller->decl)->x_##flag \
  262. < opts_for_fn (callee->decl)->x_##flag))
  263. /* Used for flags where it is safe to inline when caller's value is
  264. smaller than callee's. */
  265. #define check_maybe_down(flag) \
  266. (opts_for_fn (caller->decl)->x_##flag \
  267. != opts_for_fn (callee->decl)->x_##flag \
  268. && (!always_inline \
  269. || opts_for_fn (caller->decl)->x_##flag \
  270. > opts_for_fn (callee->decl)->x_##flag))
  271. /* Used for flags where exact match is needed for correctness. */
  272. #define check_match(flag) \
  273. (opts_for_fn (caller->decl)->x_##flag \
  274. != opts_for_fn (callee->decl)->x_##flag)
  275. /* Decide if we can inline the edge and possibly update
  276. inline_failed reason.
  277. We check whether inlining is possible at all and whether
  278. caller growth limits allow doing so.
  279. if REPORT is true, output reason to the dump file.
  280. if DISREGARD_LIMITS is true, ignore size limits.*/
  281. static bool
  282. can_inline_edge_p (struct cgraph_edge *e, bool report,
  283. bool disregard_limits = false, bool early = false)
  284. {
  285. gcc_checking_assert (e->inline_failed);
  286. if (cgraph_inline_failed_type (e->inline_failed) == CIF_FINAL_ERROR)
  287. {
  288. if (report)
  289. report_inline_failed_reason (e);
  290. return false;
  291. }
  292. bool inlinable = true;
  293. enum availability avail;
  294. cgraph_node *callee = e->callee->ultimate_alias_target (&avail);
  295. cgraph_node *caller = e->caller->global.inlined_to
  296. ? e->caller->global.inlined_to : e->caller;
  297. tree caller_tree = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (caller->decl);
  298. tree callee_tree
  299. = callee ? DECL_FUNCTION_SPECIFIC_OPTIMIZATION (callee->decl) : NULL;
  300. if (!callee->definition)
  301. {
  302. e->inline_failed = CIF_BODY_NOT_AVAILABLE;
  303. inlinable = false;
  304. }
  305. else if (callee->calls_comdat_local)
  306. {
  307. e->inline_failed = CIF_USES_COMDAT_LOCAL;
  308. inlinable = false;
  309. }
  310. else if (avail <= AVAIL_INTERPOSABLE)
  311. {
  312. e->inline_failed = CIF_OVERWRITABLE;
  313. inlinable = false;
  314. }
  315. else if (e->call_stmt_cannot_inline_p)
  316. {
  317. if (e->inline_failed != CIF_FUNCTION_NOT_OPTIMIZED)
  318. e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
  319. inlinable = false;
  320. }
  321. /* Don't inline if the functions have different EH personalities. */
  322. else if (DECL_FUNCTION_PERSONALITY (caller->decl)
  323. && DECL_FUNCTION_PERSONALITY (callee->decl)
  324. && (DECL_FUNCTION_PERSONALITY (caller->decl)
  325. != DECL_FUNCTION_PERSONALITY (callee->decl)))
  326. {
  327. e->inline_failed = CIF_EH_PERSONALITY;
  328. inlinable = false;
  329. }
  330. /* TM pure functions should not be inlined into non-TM_pure
  331. functions. */
  332. else if (is_tm_pure (callee->decl) && !is_tm_pure (caller->decl))
  333. {
  334. e->inline_failed = CIF_UNSPECIFIED;
  335. inlinable = false;
  336. }
  337. /* Check compatibility of target optimization options. */
  338. else if (!targetm.target_option.can_inline_p (caller->decl,
  339. callee->decl))
  340. {
  341. e->inline_failed = CIF_TARGET_OPTION_MISMATCH;
  342. inlinable = false;
  343. }
  344. else if (!inline_summaries->get (callee)->inlinable)
  345. {
  346. e->inline_failed = CIF_FUNCTION_NOT_INLINABLE;
  347. inlinable = false;
  348. }
  349. else if (inline_summaries->get (caller)->contains_cilk_spawn)
  350. {
  351. e->inline_failed = CIF_CILK_SPAWN;
  352. inlinable = false;
  353. }
  354. /* Don't inline a function with mismatched sanitization attributes. */
  355. else if (!sanitize_attrs_match_for_inline_p (caller->decl, callee->decl))
  356. {
  357. e->inline_failed = CIF_ATTRIBUTE_MISMATCH;
  358. inlinable = false;
  359. }
  360. /* Check if caller growth allows the inlining. */
  361. else if (!DECL_DISREGARD_INLINE_LIMITS (callee->decl)
  362. && !disregard_limits
  363. && !lookup_attribute ("flatten",
  364. DECL_ATTRIBUTES (caller->decl))
  365. && !caller_growth_limits (e))
  366. inlinable = false;
  367. /* Don't inline a function with a higher optimization level than the
  368. caller. FIXME: this is really just tip of iceberg of handling
  369. optimization attribute. */
  370. else if (caller_tree != callee_tree)
  371. {
  372. bool always_inline =
  373. (DECL_DISREGARD_INLINE_LIMITS (callee->decl)
  374. && lookup_attribute ("always_inline",
  375. DECL_ATTRIBUTES (callee->decl)));
  376. /* Until GCC 4.9 we did not check the semantics alterning flags
  377. bellow and inline across optimization boundry.
  378. Enabling checks bellow breaks several packages by refusing
  379. to inline library always_inline functions. See PR65873.
  380. Disable the check for early inlining for now until better solution
  381. is found. */
  382. if (always_inline && early)
  383. ;
  384. /* There are some options that change IL semantics which means
  385. we cannot inline in these cases for correctness reason.
  386. Not even for always_inline declared functions. */
  387. /* Strictly speaking only when the callee contains signed integer
  388. math where overflow is undefined. */
  389. else if ((check_maybe_up (flag_strict_overflow)
  390. /* this flag is set by optimize. Allow inlining across
  391. optimize boundary. */
  392. && (!opt_for_fn (caller->decl, optimize)
  393. == !opt_for_fn (callee->decl, optimize) || !always_inline))
  394. || check_match (flag_wrapv)
  395. || check_match (flag_trapv)
  396. /* Strictly speaking only when the callee uses FP math. */
  397. || check_maybe_up (flag_rounding_math)
  398. || check_maybe_up (flag_trapping_math)
  399. || check_maybe_down (flag_unsafe_math_optimizations)
  400. || check_maybe_down (flag_finite_math_only)
  401. || check_maybe_up (flag_signaling_nans)
  402. || check_maybe_down (flag_cx_limited_range)
  403. || check_maybe_up (flag_signed_zeros)
  404. || check_maybe_down (flag_associative_math)
  405. || check_maybe_down (flag_reciprocal_math)
  406. /* We do not want to make code compiled with exceptions to be
  407. brought into a non-EH function unless we know that the callee
  408. does not throw.
  409. This is tracked by DECL_FUNCTION_PERSONALITY. */
  410. || (check_match (flag_non_call_exceptions)
  411. /* TODO: We also may allow bringing !flag_non_call_exceptions
  412. to flag_non_call_exceptions function, but that may need
  413. extra work in tree-inline to add the extra EH edges. */
  414. && (!opt_for_fn (callee->decl, flag_non_call_exceptions)
  415. || DECL_FUNCTION_PERSONALITY (callee->decl)))
  416. || (check_maybe_up (flag_exceptions)
  417. && DECL_FUNCTION_PERSONALITY (callee->decl))
  418. /* Strictly speaking only when the callee contains function
  419. calls that may end up setting errno. */
  420. || check_maybe_up (flag_errno_math)
  421. /* When devirtualization is diabled for callee, it is not safe
  422. to inline it as we possibly mangled the type info.
  423. Allow early inlining of always inlines. */
  424. || (!early && check_maybe_down (flag_devirtualize)))
  425. {
  426. e->inline_failed = CIF_OPTIMIZATION_MISMATCH;
  427. inlinable = false;
  428. }
  429. /* gcc.dg/pr43564.c. Apply user-forced inline even at -O0. */
  430. else if (always_inline)
  431. ;
  432. /* When user added an attribute to the callee honor it. */
  433. else if (lookup_attribute ("optimize", DECL_ATTRIBUTES (callee->decl))
  434. && opts_for_fn (caller->decl) != opts_for_fn (callee->decl))
  435. {
  436. e->inline_failed = CIF_OPTIMIZATION_MISMATCH;
  437. inlinable = false;
  438. }
  439. /* If explicit optimize attribute are not used, the mismatch is caused
  440. by different command line options used to build different units.
  441. Do not care about COMDAT functions - those are intended to be
  442. optimized with the optimization flags of module they are used in.
  443. Also do not care about mixing up size/speed optimization when
  444. DECL_DISREGARD_INLINE_LIMITS is set. */
  445. else if ((callee->merged
  446. && !lookup_attribute ("optimize",
  447. DECL_ATTRIBUTES (caller->decl)))
  448. || DECL_DISREGARD_INLINE_LIMITS (callee->decl))
  449. ;
  450. /* If mismatch is caused by merging two LTO units with different
  451. optimizationflags we want to be bit nicer. However never inline
  452. if one of functions is not optimized at all. */
  453. else if (!opt_for_fn (callee->decl, optimize)
  454. || !opt_for_fn (caller->decl, optimize))
  455. {
  456. e->inline_failed = CIF_OPTIMIZATION_MISMATCH;
  457. inlinable = false;
  458. }
  459. /* If callee is optimized for size and caller is not, allow inlining if
  460. code shrinks or we are in MAX_INLINE_INSNS_SINGLE limit and callee
  461. is inline (and thus likely an unified comdat). This will allow caller
  462. to run faster. */
  463. else if (opt_for_fn (callee->decl, optimize_size)
  464. > opt_for_fn (caller->decl, optimize_size))
  465. {
  466. int growth = estimate_edge_growth (e);
  467. if (growth > 0
  468. && (!DECL_DECLARED_INLINE_P (callee->decl)
  469. && growth >= MAX (MAX_INLINE_INSNS_SINGLE,
  470. MAX_INLINE_INSNS_AUTO)))
  471. {
  472. e->inline_failed = CIF_OPTIMIZATION_MISMATCH;
  473. inlinable = false;
  474. }
  475. }
  476. /* If callee is more aggressively optimized for performance than caller,
  477. we generally want to inline only cheap (runtime wise) functions. */
  478. else if (opt_for_fn (callee->decl, optimize_size)
  479. < opt_for_fn (caller->decl, optimize_size)
  480. || (opt_for_fn (callee->decl, optimize)
  481. > opt_for_fn (caller->decl, optimize)))
  482. {
  483. if (estimate_edge_time (e)
  484. >= 20 + inline_edge_summary (e)->call_stmt_time)
  485. {
  486. e->inline_failed = CIF_OPTIMIZATION_MISMATCH;
  487. inlinable = false;
  488. }
  489. }
  490. }
  491. if (!inlinable && report)
  492. report_inline_failed_reason (e);
  493. return inlinable;
  494. }
  495. /* Return true if the edge E is inlinable during early inlining. */
  496. static bool
  497. can_early_inline_edge_p (struct cgraph_edge *e)
  498. {
  499. struct cgraph_node *callee = e->callee->ultimate_alias_target ();
  500. /* Early inliner might get called at WPA stage when IPA pass adds new
  501. function. In this case we can not really do any of early inlining
  502. because function bodies are missing. */
  503. if (!gimple_has_body_p (callee->decl))
  504. {
  505. e->inline_failed = CIF_BODY_NOT_AVAILABLE;
  506. return false;
  507. }
  508. /* In early inliner some of callees may not be in SSA form yet
  509. (i.e. the callgraph is cyclic and we did not process
  510. the callee by early inliner, yet). We don't have CIF code for this
  511. case; later we will re-do the decision in the real inliner. */
  512. if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (e->caller->decl))
  513. || !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (callee->decl)))
  514. {
  515. if (dump_file)
  516. fprintf (dump_file, " edge not inlinable: not in SSA form\n");
  517. return false;
  518. }
  519. if (!can_inline_edge_p (e, true, false, true))
  520. return false;
  521. return true;
  522. }
  523. /* Return number of calls in N. Ignore cheap builtins. */
  524. static int
  525. num_calls (struct cgraph_node *n)
  526. {
  527. struct cgraph_edge *e;
  528. int num = 0;
  529. for (e = n->callees; e; e = e->next_callee)
  530. if (!is_inexpensive_builtin (e->callee->decl))
  531. num++;
  532. return num;
  533. }
  534. /* Return true if we are interested in inlining small function. */
  535. static bool
  536. want_early_inline_function_p (struct cgraph_edge *e)
  537. {
  538. bool want_inline = true;
  539. struct cgraph_node *callee = e->callee->ultimate_alias_target ();
  540. if (DECL_DISREGARD_INLINE_LIMITS (callee->decl))
  541. ;
  542. /* For AutoFDO, we need to make sure that before profile summary, all
  543. hot paths' IR look exactly the same as profiled binary. As a result,
  544. in einliner, we will disregard size limit and inline those callsites
  545. that are:
  546. * inlined in the profiled binary, and
  547. * the cloned callee has enough samples to be considered "hot". */
  548. else if (flag_auto_profile && afdo_callsite_hot_enough_for_early_inline (e))
  549. ;
  550. else if (!DECL_DECLARED_INLINE_P (callee->decl)
  551. && !opt_for_fn (e->caller->decl, flag_inline_small_functions))
  552. {
  553. e->inline_failed = CIF_FUNCTION_NOT_INLINE_CANDIDATE;
  554. report_inline_failed_reason (e);
  555. want_inline = false;
  556. }
  557. else
  558. {
  559. int growth = estimate_edge_growth (e);
  560. int n;
  561. if (growth <= 0)
  562. ;
  563. else if (!e->maybe_hot_p ()
  564. && growth > 0)
  565. {
  566. if (dump_file)
  567. fprintf (dump_file, " will not early inline: %s/%i->%s/%i, "
  568. "call is cold and code would grow by %i\n",
  569. xstrdup_for_dump (e->caller->name ()),
  570. e->caller->order,
  571. xstrdup_for_dump (callee->name ()), callee->order,
  572. growth);
  573. want_inline = false;
  574. }
  575. else if (growth > PARAM_VALUE (PARAM_EARLY_INLINING_INSNS))
  576. {
  577. if (dump_file)
  578. fprintf (dump_file, " will not early inline: %s/%i->%s/%i, "
  579. "growth %i exceeds --param early-inlining-insns\n",
  580. xstrdup_for_dump (e->caller->name ()),
  581. e->caller->order,
  582. xstrdup_for_dump (callee->name ()), callee->order,
  583. growth);
  584. want_inline = false;
  585. }
  586. else if ((n = num_calls (callee)) != 0
  587. && growth * (n + 1) > PARAM_VALUE (PARAM_EARLY_INLINING_INSNS))
  588. {
  589. if (dump_file)
  590. fprintf (dump_file, " will not early inline: %s/%i->%s/%i, "
  591. "growth %i exceeds --param early-inlining-insns "
  592. "divided by number of calls\n",
  593. xstrdup_for_dump (e->caller->name ()),
  594. e->caller->order,
  595. xstrdup_for_dump (callee->name ()), callee->order,
  596. growth);
  597. want_inline = false;
  598. }
  599. }
  600. return want_inline;
  601. }
  602. /* Compute time of the edge->caller + edge->callee execution when inlining
  603. does not happen. */
  604. inline sreal
  605. compute_uninlined_call_time (struct inline_summary *callee_info,
  606. struct cgraph_edge *edge)
  607. {
  608. sreal uninlined_call_time = (sreal)callee_info->time;
  609. cgraph_node *caller = (edge->caller->global.inlined_to
  610. ? edge->caller->global.inlined_to
  611. : edge->caller);
  612. if (edge->count && caller->count)
  613. uninlined_call_time *= (sreal)edge->count / caller->count;
  614. if (edge->frequency)
  615. uninlined_call_time *= cgraph_freq_base_rec * edge->frequency;
  616. else
  617. uninlined_call_time = uninlined_call_time >> 11;
  618. int caller_time = inline_summaries->get (caller)->time;
  619. return uninlined_call_time + caller_time;
  620. }
  621. /* Same as compute_uinlined_call_time but compute time when inlining
  622. does happen. */
  623. inline sreal
  624. compute_inlined_call_time (struct cgraph_edge *edge,
  625. int edge_time)
  626. {
  627. cgraph_node *caller = (edge->caller->global.inlined_to
  628. ? edge->caller->global.inlined_to
  629. : edge->caller);
  630. int caller_time = inline_summaries->get (caller)->time;
  631. sreal time = edge_time;
  632. if (edge->count && caller->count)
  633. time *= (sreal)edge->count / caller->count;
  634. if (edge->frequency)
  635. time *= cgraph_freq_base_rec * edge->frequency;
  636. else
  637. time = time >> 11;
  638. /* This calculation should match one in ipa-inline-analysis.
  639. FIXME: Once ipa-inline-analysis is converted to sreal this can be
  640. simplified. */
  641. time -= (sreal) ((gcov_type) edge->frequency
  642. * inline_edge_summary (edge)->call_stmt_time
  643. * (INLINE_TIME_SCALE / CGRAPH_FREQ_BASE)) / INLINE_TIME_SCALE;
  644. time += caller_time;
  645. if (time <= 0)
  646. time = ((sreal) 1) >> 8;
  647. gcc_checking_assert (time >= 0);
  648. return time;
  649. }
  650. /* Return true if the speedup for inlining E is bigger than
  651. PARAM_MAX_INLINE_MIN_SPEEDUP. */
  652. static bool
  653. big_speedup_p (struct cgraph_edge *e)
  654. {
  655. sreal time = compute_uninlined_call_time (inline_summaries->get (e->callee),
  656. e);
  657. sreal inlined_time = compute_inlined_call_time (e, estimate_edge_time (e));
  658. if (time - inlined_time
  659. > (sreal) time * PARAM_VALUE (PARAM_INLINE_MIN_SPEEDUP)
  660. * percent_rec)
  661. return true;
  662. return false;
  663. }
  664. /* Return true if we are interested in inlining small function.
  665. When REPORT is true, report reason to dump file. */
  666. static bool
  667. want_inline_small_function_p (struct cgraph_edge *e, bool report)
  668. {
  669. bool want_inline = true;
  670. struct cgraph_node *callee = e->callee->ultimate_alias_target ();
  671. if (DECL_DISREGARD_INLINE_LIMITS (callee->decl))
  672. ;
  673. else if (!DECL_DECLARED_INLINE_P (callee->decl)
  674. && !opt_for_fn (e->caller->decl, flag_inline_small_functions))
  675. {
  676. e->inline_failed = CIF_FUNCTION_NOT_INLINE_CANDIDATE;
  677. want_inline = false;
  678. }
  679. /* Do fast and conservative check if the function can be good
  680. inline candidate. At the moment we allow inline hints to
  681. promote non-inline functions to inline and we increase
  682. MAX_INLINE_INSNS_SINGLE 16-fold for inline functions. */
  683. else if ((!DECL_DECLARED_INLINE_P (callee->decl)
  684. && (!e->count || !e->maybe_hot_p ()))
  685. && inline_summaries->get (callee)->min_size
  686. - inline_edge_summary (e)->call_stmt_size
  687. > MAX (MAX_INLINE_INSNS_SINGLE, MAX_INLINE_INSNS_AUTO))
  688. {
  689. e->inline_failed = CIF_MAX_INLINE_INSNS_AUTO_LIMIT;
  690. want_inline = false;
  691. }
  692. else if ((DECL_DECLARED_INLINE_P (callee->decl) || e->count)
  693. && inline_summaries->get (callee)->min_size
  694. - inline_edge_summary (e)->call_stmt_size
  695. > 16 * MAX_INLINE_INSNS_SINGLE)
  696. {
  697. e->inline_failed = (DECL_DECLARED_INLINE_P (callee->decl)
  698. ? CIF_MAX_INLINE_INSNS_SINGLE_LIMIT
  699. : CIF_MAX_INLINE_INSNS_AUTO_LIMIT);
  700. want_inline = false;
  701. }
  702. else
  703. {
  704. int growth = estimate_edge_growth (e);
  705. inline_hints hints = estimate_edge_hints (e);
  706. bool big_speedup = big_speedup_p (e);
  707. if (growth <= 0)
  708. ;
  709. /* Apply MAX_INLINE_INSNS_SINGLE limit. Do not do so when
  710. hints suggests that inlining given function is very profitable. */
  711. else if (DECL_DECLARED_INLINE_P (callee->decl)
  712. && growth >= MAX_INLINE_INSNS_SINGLE
  713. && ((!big_speedup
  714. && !(hints & (INLINE_HINT_indirect_call
  715. | INLINE_HINT_known_hot
  716. | INLINE_HINT_loop_iterations
  717. | INLINE_HINT_array_index
  718. | INLINE_HINT_loop_stride)))
  719. || growth >= MAX_INLINE_INSNS_SINGLE * 16))
  720. {
  721. e->inline_failed = CIF_MAX_INLINE_INSNS_SINGLE_LIMIT;
  722. want_inline = false;
  723. }
  724. else if (!DECL_DECLARED_INLINE_P (callee->decl)
  725. && !opt_for_fn (e->caller->decl, flag_inline_functions))
  726. {
  727. /* growth_likely_positive is expensive, always test it last. */
  728. if (growth >= MAX_INLINE_INSNS_SINGLE
  729. || growth_likely_positive (callee, growth))
  730. {
  731. e->inline_failed = CIF_NOT_DECLARED_INLINED;
  732. want_inline = false;
  733. }
  734. }
  735. /* Apply MAX_INLINE_INSNS_AUTO limit for functions not declared inline
  736. Upgrade it to MAX_INLINE_INSNS_SINGLE when hints suggests that
  737. inlining given function is very profitable. */
  738. else if (!DECL_DECLARED_INLINE_P (callee->decl)
  739. && !big_speedup
  740. && !(hints & INLINE_HINT_known_hot)
  741. && growth >= ((hints & (INLINE_HINT_indirect_call
  742. | INLINE_HINT_loop_iterations
  743. | INLINE_HINT_array_index
  744. | INLINE_HINT_loop_stride))
  745. ? MAX (MAX_INLINE_INSNS_AUTO,
  746. MAX_INLINE_INSNS_SINGLE)
  747. : MAX_INLINE_INSNS_AUTO))
  748. {
  749. /* growth_likely_positive is expensive, always test it last. */
  750. if (growth >= MAX_INLINE_INSNS_SINGLE
  751. || growth_likely_positive (callee, growth))
  752. {
  753. e->inline_failed = CIF_MAX_INLINE_INSNS_AUTO_LIMIT;
  754. want_inline = false;
  755. }
  756. }
  757. /* If call is cold, do not inline when function body would grow. */
  758. else if (!e->maybe_hot_p ()
  759. && (growth >= MAX_INLINE_INSNS_SINGLE
  760. || growth_likely_positive (callee, growth)))
  761. {
  762. e->inline_failed = CIF_UNLIKELY_CALL;
  763. want_inline = false;
  764. }
  765. }
  766. if (!want_inline && report)
  767. report_inline_failed_reason (e);
  768. return want_inline;
  769. }
  770. /* EDGE is self recursive edge.
  771. We hand two cases - when function A is inlining into itself
  772. or when function A is being inlined into another inliner copy of function
  773. A within function B.
  774. In first case OUTER_NODE points to the toplevel copy of A, while
  775. in the second case OUTER_NODE points to the outermost copy of A in B.
  776. In both cases we want to be extra selective since
  777. inlining the call will just introduce new recursive calls to appear. */
  778. static bool
  779. want_inline_self_recursive_call_p (struct cgraph_edge *edge,
  780. struct cgraph_node *outer_node,
  781. bool peeling,
  782. int depth)
  783. {
  784. char const *reason = NULL;
  785. bool want_inline = true;
  786. int caller_freq = CGRAPH_FREQ_BASE;
  787. int max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH_AUTO);
  788. if (DECL_DECLARED_INLINE_P (edge->caller->decl))
  789. max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH);
  790. if (!edge->maybe_hot_p ())
  791. {
  792. reason = "recursive call is cold";
  793. want_inline = false;
  794. }
  795. else if (max_count && !outer_node->count)
  796. {
  797. reason = "not executed in profile";
  798. want_inline = false;
  799. }
  800. else if (depth > max_depth)
  801. {
  802. reason = "--param max-inline-recursive-depth exceeded.";
  803. want_inline = false;
  804. }
  805. if (outer_node->global.inlined_to)
  806. caller_freq = outer_node->callers->frequency;
  807. if (!caller_freq)
  808. {
  809. reason = "function is inlined and unlikely";
  810. want_inline = false;
  811. }
  812. if (!want_inline)
  813. ;
  814. /* Inlining of self recursive function into copy of itself within other function
  815. is transformation similar to loop peeling.
  816. Peeling is profitable if we can inline enough copies to make probability
  817. of actual call to the self recursive function very small. Be sure that
  818. the probability of recursion is small.
  819. We ensure that the frequency of recursing is at most 1 - (1/max_depth).
  820. This way the expected number of recision is at most max_depth. */
  821. else if (peeling)
  822. {
  823. int max_prob = CGRAPH_FREQ_BASE - ((CGRAPH_FREQ_BASE + max_depth - 1)
  824. / max_depth);
  825. int i;
  826. for (i = 1; i < depth; i++)
  827. max_prob = max_prob * max_prob / CGRAPH_FREQ_BASE;
  828. if (max_count
  829. && (edge->count * CGRAPH_FREQ_BASE / outer_node->count
  830. >= max_prob))
  831. {
  832. reason = "profile of recursive call is too large";
  833. want_inline = false;
  834. }
  835. if (!max_count
  836. && (edge->frequency * CGRAPH_FREQ_BASE / caller_freq
  837. >= max_prob))
  838. {
  839. reason = "frequency of recursive call is too large";
  840. want_inline = false;
  841. }
  842. }
  843. /* Recursive inlining, i.e. equivalent of unrolling, is profitable if recursion
  844. depth is large. We reduce function call overhead and increase chances that
  845. things fit in hardware return predictor.
  846. Recursive inlining might however increase cost of stack frame setup
  847. actually slowing down functions whose recursion tree is wide rather than
  848. deep.
  849. Deciding reliably on when to do recursive inlining without profile feedback
  850. is tricky. For now we disable recursive inlining when probability of self
  851. recursion is low.
  852. Recursive inlining of self recursive call within loop also results in large loop
  853. depths that generally optimize badly. We may want to throttle down inlining
  854. in those cases. In particular this seems to happen in one of libstdc++ rb tree
  855. methods. */
  856. else
  857. {
  858. if (max_count
  859. && (edge->count * 100 / outer_node->count
  860. <= PARAM_VALUE (PARAM_MIN_INLINE_RECURSIVE_PROBABILITY)))
  861. {
  862. reason = "profile of recursive call is too small";
  863. want_inline = false;
  864. }
  865. else if (!max_count
  866. && (edge->frequency * 100 / caller_freq
  867. <= PARAM_VALUE (PARAM_MIN_INLINE_RECURSIVE_PROBABILITY)))
  868. {
  869. reason = "frequency of recursive call is too small";
  870. want_inline = false;
  871. }
  872. }
  873. if (!want_inline && dump_file)
  874. fprintf (dump_file, " not inlining recursively: %s\n", reason);
  875. return want_inline;
  876. }
  877. /* Return true when NODE has uninlinable caller;
  878. set HAS_HOT_CALL if it has hot call.
  879. Worker for cgraph_for_node_and_aliases. */
  880. static bool
  881. check_callers (struct cgraph_node *node, void *has_hot_call)
  882. {
  883. struct cgraph_edge *e;
  884. for (e = node->callers; e; e = e->next_caller)
  885. {
  886. if (!opt_for_fn (e->caller->decl, flag_inline_functions_called_once))
  887. return true;
  888. if (!can_inline_edge_p (e, true))
  889. return true;
  890. if (e->recursive_p ())
  891. return true;
  892. if (!(*(bool *)has_hot_call) && e->maybe_hot_p ())
  893. *(bool *)has_hot_call = true;
  894. }
  895. return false;
  896. }
  897. /* If NODE has a caller, return true. */
  898. static bool
  899. has_caller_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
  900. {
  901. if (node->callers)
  902. return true;
  903. return false;
  904. }
  905. /* Decide if inlining NODE would reduce unit size by eliminating
  906. the offline copy of function.
  907. When COLD is true the cold calls are considered, too. */
  908. static bool
  909. want_inline_function_to_all_callers_p (struct cgraph_node *node, bool cold)
  910. {
  911. bool has_hot_call = false;
  912. /* Aliases gets inlined along with the function they alias. */
  913. if (node->alias)
  914. return false;
  915. /* Already inlined? */
  916. if (node->global.inlined_to)
  917. return false;
  918. /* Does it have callers? */
  919. if (!node->call_for_symbol_and_aliases (has_caller_p, NULL, true))
  920. return false;
  921. /* Inlining into all callers would increase size? */
  922. if (estimate_growth (node) > 0)
  923. return false;
  924. /* All inlines must be possible. */
  925. if (node->call_for_symbol_and_aliases (check_callers, &has_hot_call,
  926. true))
  927. return false;
  928. if (!cold && !has_hot_call)
  929. return false;
  930. return true;
  931. }
  932. /* A cost model driving the inlining heuristics in a way so the edges with
  933. smallest badness are inlined first. After each inlining is performed
  934. the costs of all caller edges of nodes affected are recomputed so the
  935. metrics may accurately depend on values such as number of inlinable callers
  936. of the function or function body size. */
  937. static sreal
  938. edge_badness (struct cgraph_edge *edge, bool dump)
  939. {
  940. sreal badness;
  941. int growth, edge_time;
  942. struct cgraph_node *callee = edge->callee->ultimate_alias_target ();
  943. struct inline_summary *callee_info = inline_summaries->get (callee);
  944. inline_hints hints;
  945. cgraph_node *caller = (edge->caller->global.inlined_to
  946. ? edge->caller->global.inlined_to
  947. : edge->caller);
  948. growth = estimate_edge_growth (edge);
  949. edge_time = estimate_edge_time (edge);
  950. hints = estimate_edge_hints (edge);
  951. gcc_checking_assert (edge_time >= 0);
  952. gcc_checking_assert (edge_time <= callee_info->time);
  953. gcc_checking_assert (growth <= callee_info->size);
  954. if (dump)
  955. {
  956. fprintf (dump_file, " Badness calculation for %s/%i -> %s/%i\n",
  957. xstrdup_for_dump (edge->caller->name ()),
  958. edge->caller->order,
  959. xstrdup_for_dump (callee->name ()),
  960. edge->callee->order);
  961. fprintf (dump_file, " size growth %i, time %i ",
  962. growth,
  963. edge_time);
  964. dump_inline_hints (dump_file, hints);
  965. if (big_speedup_p (edge))
  966. fprintf (dump_file, " big_speedup");
  967. fprintf (dump_file, "\n");
  968. }
  969. /* Always prefer inlining saving code size. */
  970. if (growth <= 0)
  971. {
  972. badness = (sreal) (-SREAL_MIN_SIG + growth) << (SREAL_MAX_EXP / 256);
  973. if (dump)
  974. fprintf (dump_file, " %f: Growth %d <= 0\n", badness.to_double (),
  975. growth);
  976. }
  977. /* Inlining into EXTERNAL functions is not going to change anything unless
  978. they are themselves inlined. */
  979. else if (DECL_EXTERNAL (caller->decl))
  980. {
  981. if (dump)
  982. fprintf (dump_file, " max: function is external\n");
  983. return sreal::max ();
  984. }
  985. /* When profile is available. Compute badness as:
  986. time_saved * caller_count
  987. goodness = -------------------------------------------------
  988. growth_of_caller * overall_growth * combined_size
  989. badness = - goodness
  990. Again use negative value to make calls with profile appear hotter
  991. then calls without.
  992. */
  993. else if (opt_for_fn (caller->decl, flag_guess_branch_prob) || caller->count)
  994. {
  995. sreal numerator, denominator;
  996. int overall_growth;
  997. numerator = (compute_uninlined_call_time (callee_info, edge)
  998. - compute_inlined_call_time (edge, edge_time));
  999. if (numerator == 0)
  1000. numerator = ((sreal) 1 >> 8);
  1001. if (caller->count)
  1002. numerator *= caller->count;
  1003. else if (opt_for_fn (caller->decl, flag_branch_probabilities))
  1004. numerator = numerator >> 11;
  1005. denominator = growth;
  1006. overall_growth = callee_info->growth;
  1007. /* Look for inliner wrappers of the form:
  1008. inline_caller ()
  1009. {
  1010. do_fast_job...
  1011. if (need_more_work)
  1012. noninline_callee ();
  1013. }
  1014. Withhout panilizing this case, we usually inline noninline_callee
  1015. into the inline_caller because overall_growth is small preventing
  1016. further inlining of inline_caller.
  1017. Penalize only callgraph edges to functions with small overall
  1018. growth ...
  1019. */
  1020. if (growth > overall_growth
  1021. /* ... and having only one caller which is not inlined ... */
  1022. && callee_info->single_caller
  1023. && !edge->caller->global.inlined_to
  1024. /* ... and edges executed only conditionally ... */
  1025. && edge->frequency < CGRAPH_FREQ_BASE
  1026. /* ... consider case where callee is not inline but caller is ... */
  1027. && ((!DECL_DECLARED_INLINE_P (edge->callee->decl)
  1028. && DECL_DECLARED_INLINE_P (caller->decl))
  1029. /* ... or when early optimizers decided to split and edge
  1030. frequency still indicates splitting is a win ... */
  1031. || (callee->split_part && !caller->split_part
  1032. && edge->frequency
  1033. < CGRAPH_FREQ_BASE
  1034. * PARAM_VALUE
  1035. (PARAM_PARTIAL_INLINING_ENTRY_PROBABILITY) / 100
  1036. /* ... and do not overwrite user specified hints. */
  1037. && (!DECL_DECLARED_INLINE_P (edge->callee->decl)
  1038. || DECL_DECLARED_INLINE_P (caller->decl)))))
  1039. {
  1040. struct inline_summary *caller_info = inline_summaries->get (caller);
  1041. int caller_growth = caller_info->growth;
  1042. /* Only apply the penalty when caller looks like inline candidate,
  1043. and it is not called once and. */
  1044. if (!caller_info->single_caller && overall_growth < caller_growth
  1045. && caller_info->inlinable
  1046. && caller_info->size
  1047. < (DECL_DECLARED_INLINE_P (caller->decl)
  1048. ? MAX_INLINE_INSNS_SINGLE : MAX_INLINE_INSNS_AUTO))
  1049. {
  1050. if (dump)
  1051. fprintf (dump_file,
  1052. " Wrapper penalty. Increasing growth %i to %i\n",
  1053. overall_growth, caller_growth);
  1054. overall_growth = caller_growth;
  1055. }
  1056. }
  1057. if (overall_growth > 0)
  1058. {
  1059. /* Strongly preffer functions with few callers that can be inlined
  1060. fully. The square root here leads to smaller binaries at average.
  1061. Watch however for extreme cases and return to linear function
  1062. when growth is large. */
  1063. if (overall_growth < 256)
  1064. overall_growth *= overall_growth;
  1065. else
  1066. overall_growth += 256 * 256 - 256;
  1067. denominator *= overall_growth;
  1068. }
  1069. denominator *= inline_summaries->get (caller)->self_size + growth;
  1070. badness = - numerator / denominator;
  1071. if (dump)
  1072. {
  1073. fprintf (dump_file,
  1074. " %f: guessed profile. frequency %f, count %"PRId64
  1075. " caller count %"PRId64
  1076. " time w/o inlining %f, time w inlining %f"
  1077. " overall growth %i (current) %i (original)"
  1078. " %i (compensated)\n",
  1079. badness.to_double (),
  1080. (double)edge->frequency / CGRAPH_FREQ_BASE,
  1081. edge->count, caller->count,
  1082. compute_uninlined_call_time (callee_info, edge).to_double (),
  1083. compute_inlined_call_time (edge, edge_time).to_double (),
  1084. estimate_growth (callee),
  1085. callee_info->growth, overall_growth);
  1086. }
  1087. }
  1088. /* When function local profile is not available or it does not give
  1089. useful information (ie frequency is zero), base the cost on
  1090. loop nest and overall size growth, so we optimize for overall number
  1091. of functions fully inlined in program. */
  1092. else
  1093. {
  1094. int nest = MIN (inline_edge_summary (edge)->loop_depth, 8);
  1095. badness = growth;
  1096. /* Decrease badness if call is nested. */
  1097. if (badness > 0)
  1098. badness = badness >> nest;
  1099. else
  1100. badness = badness << nest;
  1101. if (dump)
  1102. fprintf (dump_file, " %f: no profile. nest %i\n",
  1103. badness.to_double (), nest);
  1104. }
  1105. gcc_checking_assert (badness != 0);
  1106. if (edge->recursive_p ())
  1107. badness = badness.shift (badness > 0 ? 4 : -4);
  1108. if ((hints & (INLINE_HINT_indirect_call
  1109. | INLINE_HINT_loop_iterations
  1110. | INLINE_HINT_array_index
  1111. | INLINE_HINT_loop_stride))
  1112. || callee_info->growth <= 0)
  1113. badness = badness.shift (badness > 0 ? -2 : 2);
  1114. if (hints & (INLINE_HINT_same_scc))
  1115. badness = badness.shift (badness > 0 ? 3 : -3);
  1116. else if (hints & (INLINE_HINT_in_scc))
  1117. badness = badness.shift (badness > 0 ? 2 : -2);
  1118. else if (hints & (INLINE_HINT_cross_module))
  1119. badness = badness.shift (badness > 0 ? 1 : -1);
  1120. if (DECL_DISREGARD_INLINE_LIMITS (callee->decl))
  1121. badness = badness.shift (badness > 0 ? -4 : 4);
  1122. else if ((hints & INLINE_HINT_declared_inline))
  1123. badness = badness.shift (badness > 0 ? -3 : 3);
  1124. if (dump)
  1125. fprintf (dump_file, " Adjusted by hints %f\n", badness.to_double ());
  1126. return badness;
  1127. }
  1128. /* Recompute badness of EDGE and update its key in HEAP if needed. */
  1129. static inline void
  1130. update_edge_key (edge_heap_t *heap, struct cgraph_edge *edge)
  1131. {
  1132. sreal badness = edge_badness (edge, false);
  1133. if (edge->aux)
  1134. {
  1135. edge_heap_node_t *n = (edge_heap_node_t *) edge->aux;
  1136. gcc_checking_assert (n->get_data () == edge);
  1137. /* fibonacci_heap::replace_key does busy updating of the
  1138. heap that is unnecesarily expensive.
  1139. We do lazy increases: after extracting minimum if the key
  1140. turns out to be out of date, it is re-inserted into heap
  1141. with correct value. */
  1142. if (badness < n->get_key ())
  1143. {
  1144. if (dump_file && (dump_flags & TDF_DETAILS))
  1145. {
  1146. fprintf (dump_file,
  1147. " decreasing badness %s/%i -> %s/%i, %f"
  1148. " to %f\n",
  1149. xstrdup_for_dump (edge->caller->name ()),
  1150. edge->caller->order,
  1151. xstrdup_for_dump (edge->callee->name ()),
  1152. edge->callee->order,
  1153. n->get_key ().to_double (),
  1154. badness.to_double ());
  1155. }
  1156. heap->decrease_key (n, badness);
  1157. }
  1158. }
  1159. else
  1160. {
  1161. if (dump_file && (dump_flags & TDF_DETAILS))
  1162. {
  1163. fprintf (dump_file,
  1164. " enqueuing call %s/%i -> %s/%i, badness %f\n",
  1165. xstrdup_for_dump (edge->caller->name ()),
  1166. edge->caller->order,
  1167. xstrdup_for_dump (edge->callee->name ()),
  1168. edge->callee->order,
  1169. badness.to_double ());
  1170. }
  1171. edge->aux = heap->insert (badness, edge);
  1172. }
  1173. }
  1174. /* NODE was inlined.
  1175. All caller edges needs to be resetted because
  1176. size estimates change. Similarly callees needs reset
  1177. because better context may be known. */
  1178. static void
  1179. reset_edge_caches (struct cgraph_node *node)
  1180. {
  1181. struct cgraph_edge *edge;
  1182. struct cgraph_edge *e = node->callees;
  1183. struct cgraph_node *where = node;
  1184. struct ipa_ref *ref;
  1185. if (where->global.inlined_to)
  1186. where = where->global.inlined_to;
  1187. for (edge = where->callers; edge; edge = edge->next_caller)
  1188. if (edge->inline_failed)
  1189. reset_edge_growth_cache (edge);
  1190. FOR_EACH_ALIAS (where, ref)
  1191. reset_edge_caches (dyn_cast <cgraph_node *> (ref->referring));
  1192. if (!e)
  1193. return;
  1194. while (true)
  1195. if (!e->inline_failed && e->callee->callees)
  1196. e = e->callee->callees;
  1197. else
  1198. {
  1199. if (e->inline_failed)
  1200. reset_edge_growth_cache (e);
  1201. if (e->next_callee)
  1202. e = e->next_callee;
  1203. else
  1204. {
  1205. do
  1206. {
  1207. if (e->caller == node)
  1208. return;
  1209. e = e->caller->callers;
  1210. }
  1211. while (!e->next_callee);
  1212. e = e->next_callee;
  1213. }
  1214. }
  1215. }
  1216. /* Recompute HEAP nodes for each of caller of NODE.
  1217. UPDATED_NODES track nodes we already visited, to avoid redundant work.
  1218. When CHECK_INLINABLITY_FOR is set, re-check for specified edge that
  1219. it is inlinable. Otherwise check all edges. */
  1220. static void
  1221. update_caller_keys (edge_heap_t *heap, struct cgraph_node *node,
  1222. bitmap updated_nodes,
  1223. struct cgraph_edge *check_inlinablity_for)
  1224. {
  1225. struct cgraph_edge *edge;
  1226. struct ipa_ref *ref;
  1227. if ((!node->alias && !inline_summaries->get (node)->inlinable)
  1228. || node->global.inlined_to)
  1229. return;
  1230. if (!bitmap_set_bit (updated_nodes, node->uid))
  1231. return;
  1232. FOR_EACH_ALIAS (node, ref)
  1233. {
  1234. struct cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
  1235. update_caller_keys (heap, alias, updated_nodes, check_inlinablity_for);
  1236. }
  1237. for (edge = node->callers; edge; edge = edge->next_caller)
  1238. if (edge->inline_failed)
  1239. {
  1240. if (!check_inlinablity_for
  1241. || check_inlinablity_for == edge)
  1242. {
  1243. if (can_inline_edge_p (edge, false)
  1244. && want_inline_small_function_p (edge, false))
  1245. update_edge_key (heap, edge);
  1246. else if (edge->aux)
  1247. {
  1248. report_inline_failed_reason (edge);
  1249. heap->delete_node ((edge_heap_node_t *) edge->aux);
  1250. edge->aux = NULL;
  1251. }
  1252. }
  1253. else if (edge->aux)
  1254. update_edge_key (heap, edge);
  1255. }
  1256. }
  1257. /* Recompute HEAP nodes for each uninlined call in NODE.
  1258. This is used when we know that edge badnesses are going only to increase
  1259. (we introduced new call site) and thus all we need is to insert newly
  1260. created edges into heap. */
  1261. static void
  1262. update_callee_keys (edge_heap_t *heap, struct cgraph_node *node,
  1263. bitmap updated_nodes)
  1264. {
  1265. struct cgraph_edge *e = node->callees;
  1266. if (!e)
  1267. return;
  1268. while (true)
  1269. if (!e->inline_failed && e->callee->callees)
  1270. e = e->callee->callees;
  1271. else
  1272. {
  1273. enum availability avail;
  1274. struct cgraph_node *callee;
  1275. /* We do not reset callee growth cache here. Since we added a new call,
  1276. growth chould have just increased and consequentely badness metric
  1277. don't need updating. */
  1278. if (e->inline_failed
  1279. && (callee = e->callee->ultimate_alias_target (&avail))
  1280. && inline_summaries->get (callee)->inlinable
  1281. && avail >= AVAIL_AVAILABLE
  1282. && !bitmap_bit_p (updated_nodes, callee->uid))
  1283. {
  1284. if (can_inline_edge_p (e, false)
  1285. && want_inline_small_function_p (e, false))
  1286. update_edge_key (heap, e);
  1287. else if (e->aux)
  1288. {
  1289. report_inline_failed_reason (e);
  1290. heap->delete_node ((edge_heap_node_t *) e->aux);
  1291. e->aux = NULL;
  1292. }
  1293. }
  1294. if (e->next_callee)
  1295. e = e->next_callee;
  1296. else
  1297. {
  1298. do
  1299. {
  1300. if (e->caller == node)
  1301. return;
  1302. e = e->caller->callers;
  1303. }
  1304. while (!e->next_callee);
  1305. e = e->next_callee;
  1306. }
  1307. }
  1308. }
  1309. /* Enqueue all recursive calls from NODE into priority queue depending on
  1310. how likely we want to recursively inline the call. */
  1311. static void
  1312. lookup_recursive_calls (struct cgraph_node *node, struct cgraph_node *where,
  1313. edge_heap_t *heap)
  1314. {
  1315. struct cgraph_edge *e;
  1316. enum availability avail;
  1317. for (e = where->callees; e; e = e->next_callee)
  1318. if (e->callee == node
  1319. || (e->callee->ultimate_alias_target (&avail) == node
  1320. && avail > AVAIL_INTERPOSABLE))
  1321. {
  1322. /* When profile feedback is available, prioritize by expected number
  1323. of calls. */
  1324. heap->insert (!max_count ? -e->frequency
  1325. : -(e->count / ((max_count + (1<<24) - 1) / (1<<24))),
  1326. e);
  1327. }
  1328. for (e = where->callees; e; e = e->next_callee)
  1329. if (!e->inline_failed)
  1330. lookup_recursive_calls (node, e->callee, heap);
  1331. }
  1332. /* Decide on recursive inlining: in the case function has recursive calls,
  1333. inline until body size reaches given argument. If any new indirect edges
  1334. are discovered in the process, add them to *NEW_EDGES, unless NEW_EDGES
  1335. is NULL. */
  1336. static bool
  1337. recursive_inlining (struct cgraph_edge *edge,
  1338. vec<cgraph_edge *> *new_edges)
  1339. {
  1340. int limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE_AUTO);
  1341. edge_heap_t heap (sreal::min ());
  1342. struct cgraph_node *node;
  1343. struct cgraph_edge *e;
  1344. struct cgraph_node *master_clone = NULL, *next;
  1345. int depth = 0;
  1346. int n = 0;
  1347. node = edge->caller;
  1348. if (node->global.inlined_to)
  1349. node = node->global.inlined_to;
  1350. if (DECL_DECLARED_INLINE_P (node->decl))
  1351. limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE);
  1352. /* Make sure that function is small enough to be considered for inlining. */
  1353. if (estimate_size_after_inlining (node, edge) >= limit)
  1354. return false;
  1355. lookup_recursive_calls (node, node, &heap);
  1356. if (heap.empty ())
  1357. return false;
  1358. if (dump_file)
  1359. fprintf (dump_file,
  1360. " Performing recursive inlining on %s\n",
  1361. node->name ());
  1362. /* Do the inlining and update list of recursive call during process. */
  1363. while (!heap.empty ())
  1364. {
  1365. struct cgraph_edge *curr = heap.extract_min ();
  1366. struct cgraph_node *cnode, *dest = curr->callee;
  1367. if (!can_inline_edge_p (curr, true))
  1368. continue;
  1369. /* MASTER_CLONE is produced in the case we already started modified
  1370. the function. Be sure to redirect edge to the original body before
  1371. estimating growths otherwise we will be seeing growths after inlining
  1372. the already modified body. */
  1373. if (master_clone)
  1374. {
  1375. curr->redirect_callee (master_clone);
  1376. reset_edge_growth_cache (curr);
  1377. }
  1378. if (estimate_size_after_inlining (node, curr) > limit)
  1379. {
  1380. curr->redirect_callee (dest);
  1381. reset_edge_growth_cache (curr);
  1382. break;
  1383. }
  1384. depth = 1;
  1385. for (cnode = curr->caller;
  1386. cnode->global.inlined_to; cnode = cnode->callers->caller)
  1387. if (node->decl
  1388. == curr->callee->ultimate_alias_target ()->decl)
  1389. depth++;
  1390. if (!want_inline_self_recursive_call_p (curr, node, false, depth))
  1391. {
  1392. curr->redirect_callee (dest);
  1393. reset_edge_growth_cache (curr);
  1394. continue;
  1395. }
  1396. if (dump_file)
  1397. {
  1398. fprintf (dump_file,
  1399. " Inlining call of depth %i", depth);
  1400. if (node->count)
  1401. {
  1402. fprintf (dump_file, " called approx. %.2f times per call",
  1403. (double)curr->count / node->count);
  1404. }
  1405. fprintf (dump_file, "\n");
  1406. }
  1407. if (!master_clone)
  1408. {
  1409. /* We need original clone to copy around. */
  1410. master_clone = node->create_clone (node->decl, node->count,
  1411. CGRAPH_FREQ_BASE, false, vNULL,
  1412. true, NULL, NULL);
  1413. for (e = master_clone->callees; e; e = e->next_callee)
  1414. if (!e->inline_failed)
  1415. clone_inlined_nodes (e, true, false, NULL, CGRAPH_FREQ_BASE);
  1416. curr->redirect_callee (master_clone);
  1417. reset_edge_growth_cache (curr);
  1418. }
  1419. inline_call (curr, false, new_edges, &overall_size, true);
  1420. lookup_recursive_calls (node, curr->callee, &heap);
  1421. n++;
  1422. }
  1423. if (!heap.empty () && dump_file)
  1424. fprintf (dump_file, " Recursive inlining growth limit met.\n");
  1425. if (!master_clone)
  1426. return false;
  1427. if (dump_file)
  1428. fprintf (dump_file,
  1429. "\n Inlined %i times, "
  1430. "body grown from size %i to %i, time %i to %i\n", n,
  1431. inline_summaries->get (master_clone)->size, inline_summaries->get (node)->size,
  1432. inline_summaries->get (master_clone)->time, inline_summaries->get (node)->time);
  1433. /* Remove master clone we used for inlining. We rely that clones inlined
  1434. into master clone gets queued just before master clone so we don't
  1435. need recursion. */
  1436. for (node = symtab->first_function (); node != master_clone;
  1437. node = next)
  1438. {
  1439. next = symtab->next_function (node);
  1440. if (node->global.inlined_to == master_clone)
  1441. node->remove ();
  1442. }
  1443. master_clone->remove ();
  1444. return true;
  1445. }
  1446. /* Given whole compilation unit estimate of INSNS, compute how large we can
  1447. allow the unit to grow. */
  1448. static int
  1449. compute_max_insns (int insns)
  1450. {
  1451. int max_insns = insns;
  1452. if (max_insns < PARAM_VALUE (PARAM_LARGE_UNIT_INSNS))
  1453. max_insns = PARAM_VALUE (PARAM_LARGE_UNIT_INSNS);
  1454. return ((int64_t) max_insns
  1455. * (100 + PARAM_VALUE (PARAM_INLINE_UNIT_GROWTH)) / 100);
  1456. }
  1457. /* Compute badness of all edges in NEW_EDGES and add them to the HEAP. */
  1458. static void
  1459. add_new_edges_to_heap (edge_heap_t *heap, vec<cgraph_edge *> new_edges)
  1460. {
  1461. while (new_edges.length () > 0)
  1462. {
  1463. struct cgraph_edge *edge = new_edges.pop ();
  1464. gcc_assert (!edge->aux);
  1465. if (edge->inline_failed
  1466. && can_inline_edge_p (edge, true)
  1467. && want_inline_small_function_p (edge, true))
  1468. edge->aux = heap->insert (edge_badness (edge, false), edge);
  1469. }
  1470. }
  1471. /* Remove EDGE from the fibheap. */
  1472. static void
  1473. heap_edge_removal_hook (struct cgraph_edge *e, void *data)
  1474. {
  1475. if (e->aux)
  1476. {
  1477. ((edge_heap_t *)data)->delete_node ((edge_heap_node_t *)e->aux);
  1478. e->aux = NULL;
  1479. }
  1480. }
  1481. /* Return true if speculation of edge E seems useful.
  1482. If ANTICIPATE_INLINING is true, be conservative and hope that E
  1483. may get inlined. */
  1484. bool
  1485. speculation_useful_p (struct cgraph_edge *e, bool anticipate_inlining)
  1486. {
  1487. enum availability avail;
  1488. struct cgraph_node *target = e->callee->ultimate_alias_target (&avail);
  1489. struct cgraph_edge *direct, *indirect;
  1490. struct ipa_ref *ref;
  1491. gcc_assert (e->speculative && !e->indirect_unknown_callee);
  1492. if (!e->maybe_hot_p ())
  1493. return false;
  1494. /* See if IP optimizations found something potentially useful about the
  1495. function. For now we look only for CONST/PURE flags. Almost everything
  1496. else we propagate is useless. */
  1497. if (avail >= AVAIL_AVAILABLE)
  1498. {
  1499. int ecf_flags = flags_from_decl_or_type (target->decl);
  1500. if (ecf_flags & ECF_CONST)
  1501. {
  1502. e->speculative_call_info (direct, indirect, ref);
  1503. if (!(indirect->indirect_info->ecf_flags & ECF_CONST))
  1504. return true;
  1505. }
  1506. else if (ecf_flags & ECF_PURE)
  1507. {
  1508. e->speculative_call_info (direct, indirect, ref);
  1509. if (!(indirect->indirect_info->ecf_flags & ECF_PURE))
  1510. return true;
  1511. }
  1512. }
  1513. /* If we did not managed to inline the function nor redirect
  1514. to an ipa-cp clone (that are seen by having local flag set),
  1515. it is probably pointless to inline it unless hardware is missing
  1516. indirect call predictor. */
  1517. if (!anticipate_inlining && e->inline_failed && !target->local.local)
  1518. return false;
  1519. /* For overwritable targets there is not much to do. */
  1520. if (e->inline_failed && !can_inline_edge_p (e, false, true))
  1521. return false;
  1522. /* OK, speculation seems interesting. */
  1523. return true;
  1524. }
  1525. /* We know that EDGE is not going to be inlined.
  1526. See if we can remove speculation. */
  1527. static void
  1528. resolve_noninline_speculation (edge_heap_t *edge_heap, struct cgraph_edge *edge)
  1529. {
  1530. if (edge->speculative && !speculation_useful_p (edge, false))
  1531. {
  1532. struct cgraph_node *node = edge->caller;
  1533. struct cgraph_node *where = node->global.inlined_to
  1534. ? node->global.inlined_to : node;
  1535. bitmap updated_nodes = BITMAP_ALLOC (NULL);
  1536. spec_rem += edge->count;
  1537. edge->resolve_speculation ();
  1538. reset_edge_caches (where);
  1539. inline_update_overall_summary (where);
  1540. update_caller_keys (edge_heap, where,
  1541. updated_nodes, NULL);
  1542. update_callee_keys (edge_heap, where,
  1543. updated_nodes);
  1544. BITMAP_FREE (updated_nodes);
  1545. }
  1546. }
  1547. /* Return true if NODE should be accounted for overall size estimate.
  1548. Skip all nodes optimized for size so we can measure the growth of hot
  1549. part of program no matter of the padding. */
  1550. bool
  1551. inline_account_function_p (struct cgraph_node *node)
  1552. {
  1553. return (!DECL_EXTERNAL (node->decl)
  1554. && !opt_for_fn (node->decl, optimize_size)
  1555. && node->frequency != NODE_FREQUENCY_UNLIKELY_EXECUTED);
  1556. }
  1557. /* Count number of callers of NODE and store it into DATA (that
  1558. points to int. Worker for cgraph_for_node_and_aliases. */
  1559. static bool
  1560. sum_callers (struct cgraph_node *node, void *data)
  1561. {
  1562. struct cgraph_edge *e;
  1563. int *num_calls = (int *)data;
  1564. for (e = node->callers; e; e = e->next_caller)
  1565. (*num_calls)++;
  1566. return false;
  1567. }
  1568. /* We use greedy algorithm for inlining of small functions:
  1569. All inline candidates are put into prioritized heap ordered in
  1570. increasing badness.
  1571. The inlining of small functions is bounded by unit growth parameters. */
  1572. static void
  1573. inline_small_functions (void)
  1574. {
  1575. struct cgraph_node *node;
  1576. struct cgraph_edge *edge;
  1577. edge_heap_t edge_heap (sreal::min ());
  1578. bitmap updated_nodes = BITMAP_ALLOC (NULL);
  1579. int min_size, max_size;
  1580. auto_vec<cgraph_edge *> new_indirect_edges;
  1581. int initial_size = 0;
  1582. struct cgraph_node **order = XCNEWVEC (cgraph_node *, symtab->cgraph_count);
  1583. struct cgraph_edge_hook_list *edge_removal_hook_holder;
  1584. new_indirect_edges.create (8);
  1585. edge_removal_hook_holder
  1586. = symtab->add_edge_removal_hook (&heap_edge_removal_hook, &edge_heap);
  1587. /* Compute overall unit size and other global parameters used by badness
  1588. metrics. */
  1589. max_count = 0;
  1590. ipa_reduced_postorder (order, true, true, NULL);
  1591. free (order);
  1592. FOR_EACH_DEFINED_FUNCTION (node)
  1593. if (!node->global.inlined_to)
  1594. {
  1595. if (!node->alias && node->analyzed
  1596. && (node->has_gimple_body_p () || node->thunk.thunk_p))
  1597. {
  1598. struct inline_summary *info = inline_summaries->get (node);
  1599. struct ipa_dfs_info *dfs = (struct ipa_dfs_info *) node->aux;
  1600. /* Do not account external functions, they will be optimized out
  1601. if not inlined. Also only count the non-cold portion of program. */
  1602. if (inline_account_function_p (node))
  1603. initial_size += info->size;
  1604. info->growth = estimate_growth (node);
  1605. int num_calls = 0;
  1606. node->call_for_symbol_and_aliases (sum_callers, &num_calls,
  1607. true);
  1608. if (num_calls == 1)
  1609. info->single_caller = true;
  1610. if (dfs && dfs->next_cycle)
  1611. {
  1612. struct cgraph_node *n2;
  1613. int id = dfs->scc_no + 1;
  1614. for (n2 = node; n2;
  1615. n2 = ((struct ipa_dfs_info *) node->aux)->next_cycle)
  1616. {
  1617. struct inline_summary *info2 = inline_summaries->get (n2);
  1618. if (info2->scc_no)
  1619. break;
  1620. info2->scc_no = id;
  1621. }
  1622. }
  1623. }
  1624. for (edge = node->callers; edge; edge = edge->next_caller)
  1625. if (max_count < edge->count)
  1626. max_count = edge->count;
  1627. }
  1628. ipa_free_postorder_info ();
  1629. initialize_growth_caches ();
  1630. if (dump_file)
  1631. fprintf (dump_file,
  1632. "\nDeciding on inlining of small functions. Starting with size %i.\n",
  1633. initial_size);
  1634. overall_size = initial_size;
  1635. max_size = compute_max_insns (overall_size);
  1636. min_size = overall_size;
  1637. /* Populate the heap with all edges we might inline. */
  1638. FOR_EACH_DEFINED_FUNCTION (node)
  1639. {
  1640. bool update = false;
  1641. struct cgraph_edge *next = NULL;
  1642. bool has_speculative = false;
  1643. if (dump_file)
  1644. fprintf (dump_file, "Enqueueing calls in %s/%i.\n",
  1645. node->name (), node->order);
  1646. for (edge = node->callees; edge; edge = next)
  1647. {
  1648. next = edge->next_callee;
  1649. if (edge->inline_failed
  1650. && !edge->aux
  1651. && can_inline_edge_p (edge, true)
  1652. && want_inline_small_function_p (edge, true)
  1653. && edge->inline_failed)
  1654. {
  1655. gcc_assert (!edge->aux);
  1656. update_edge_key (&edge_heap, edge);
  1657. }
  1658. if (edge->speculative)
  1659. has_speculative = true;
  1660. }
  1661. if (has_speculative)
  1662. for (edge = node->callees; edge; edge = next)
  1663. if (edge->speculative && !speculation_useful_p (edge,
  1664. edge->aux != NULL))
  1665. {
  1666. edge->resolve_speculation ();
  1667. update = true;
  1668. }
  1669. if (update)
  1670. {
  1671. struct cgraph_node *where = node->global.inlined_to
  1672. ? node->global.inlined_to : node;
  1673. inline_update_overall_summary (where);
  1674. reset_edge_caches (where);
  1675. update_caller_keys (&edge_heap, where,
  1676. updated_nodes, NULL);
  1677. update_callee_keys (&edge_heap, where,
  1678. updated_nodes);
  1679. bitmap_clear (updated_nodes);
  1680. }
  1681. }
  1682. gcc_assert (in_lto_p
  1683. || !max_count
  1684. || (profile_info && flag_branch_probabilities));
  1685. while (!edge_heap.empty ())
  1686. {
  1687. int old_size = overall_size;
  1688. struct cgraph_node *where, *callee;
  1689. sreal badness = edge_heap.min_key ();
  1690. sreal current_badness;
  1691. int growth;
  1692. edge = edge_heap.extract_min ();
  1693. gcc_assert (edge->aux);
  1694. edge->aux = NULL;
  1695. if (!edge->inline_failed || !edge->callee->analyzed)
  1696. continue;
  1697. #ifdef ENABLE_CHECKING
  1698. /* Be sure that caches are maintained consistent. */
  1699. sreal cached_badness = edge_badness (edge, false);
  1700. int old_size_est = estimate_edge_size (edge);
  1701. int old_time_est = estimate_edge_time (edge);
  1702. int old_hints_est = estimate_edge_hints (edge);
  1703. reset_edge_growth_cache (edge);
  1704. gcc_assert (old_size_est == estimate_edge_size (edge));
  1705. gcc_assert (old_time_est == estimate_edge_time (edge));
  1706. /* FIXME:
  1707. gcc_assert (old_hints_est == estimate_edge_hints (edge));
  1708. fails with profile feedback because some hints depends on
  1709. maybe_hot_edge_p predicate and because callee gets inlined to other
  1710. calls, the edge may become cold.
  1711. This ought to be fixed by computing relative probabilities
  1712. for given invocation but that will be better done once whole
  1713. code is converted to sreals. Disable for now and revert to "wrong"
  1714. value so enable/disable checking paths agree. */
  1715. edge_growth_cache[edge->uid].hints = old_hints_est + 1;
  1716. /* When updating the edge costs, we only decrease badness in the keys.
  1717. Increases of badness are handled lazilly; when we see key with out
  1718. of date value on it, we re-insert it now. */
  1719. current_badness = edge_badness (edge, false);
  1720. /* Disable checking for profile because roundoff errors may cause slight
  1721. deviations in the order. */
  1722. gcc_assert (max_count || cached_badness == current_badness);
  1723. gcc_assert (current_badness >= badness);
  1724. #else
  1725. current_badness = edge_badness (edge, false);
  1726. #endif
  1727. if (current_badness != badness)
  1728. {
  1729. if (edge_heap.min () && current_badness > edge_heap.min_key ())
  1730. {
  1731. edge->aux = edge_heap.insert (current_badness, edge);
  1732. continue;
  1733. }
  1734. else
  1735. badness = current_badness;
  1736. }
  1737. if (!can_inline_edge_p (edge, true))
  1738. {
  1739. resolve_noninline_speculation (&edge_heap, edge);
  1740. continue;
  1741. }
  1742. callee = edge->callee->ultimate_alias_target ();
  1743. growth = estimate_edge_growth (edge);
  1744. if (dump_file)
  1745. {
  1746. fprintf (dump_file,
  1747. "\nConsidering %s/%i with %i size\n",
  1748. callee->name (), callee->order,
  1749. inline_summaries->get (callee)->size);
  1750. fprintf (dump_file,
  1751. " to be inlined into %s/%i in %s:%i\n"
  1752. " Estimated badness is %f, frequency %.2f.\n",
  1753. edge->caller->name (), edge->caller->order,
  1754. edge->call_stmt
  1755. && (LOCATION_LOCUS (gimple_location ((const_gimple)
  1756. edge->call_stmt))
  1757. > BUILTINS_LOCATION)
  1758. ? gimple_filename ((const_gimple) edge->call_stmt)
  1759. : "unknown",
  1760. edge->call_stmt
  1761. ? gimple_lineno ((const_gimple) edge->call_stmt)
  1762. : -1,
  1763. badness.to_double (),
  1764. edge->frequency / (double)CGRAPH_FREQ_BASE);
  1765. if (edge->count)
  1766. fprintf (dump_file," Called %"PRId64"x\n",
  1767. edge->count);
  1768. if (dump_flags & TDF_DETAILS)
  1769. edge_badness (edge, true);
  1770. }
  1771. if (overall_size + growth > max_size
  1772. && !DECL_DISREGARD_INLINE_LIMITS (callee->decl))
  1773. {
  1774. edge->inline_failed = CIF_INLINE_UNIT_GROWTH_LIMIT;
  1775. report_inline_failed_reason (edge);
  1776. resolve_noninline_speculation (&edge_heap, edge);
  1777. continue;
  1778. }
  1779. if (!want_inline_small_function_p (edge, true))
  1780. {
  1781. resolve_noninline_speculation (&edge_heap, edge);
  1782. continue;
  1783. }
  1784. /* Heuristics for inlining small functions work poorly for
  1785. recursive calls where we do effects similar to loop unrolling.
  1786. When inlining such edge seems profitable, leave decision on
  1787. specific inliner. */
  1788. if (edge->recursive_p ())
  1789. {
  1790. where = edge->caller;
  1791. if (where->global.inlined_to)
  1792. where = where->global.inlined_to;
  1793. if (!recursive_inlining (edge,
  1794. opt_for_fn (edge->caller->decl,
  1795. flag_indirect_inlining)
  1796. ? &new_indirect_edges : NULL))
  1797. {
  1798. edge->inline_failed = CIF_RECURSIVE_INLINING;
  1799. resolve_noninline_speculation (&edge_heap, edge);
  1800. continue;
  1801. }
  1802. reset_edge_caches (where);
  1803. /* Recursive inliner inlines all recursive calls of the function
  1804. at once. Consequently we need to update all callee keys. */
  1805. if (opt_for_fn (edge->caller->decl, flag_indirect_inlining))
  1806. add_new_edges_to_heap (&edge_heap, new_indirect_edges);
  1807. update_callee_keys (&edge_heap, where, updated_nodes);
  1808. bitmap_clear (updated_nodes);
  1809. }
  1810. else
  1811. {
  1812. struct cgraph_node *outer_node = NULL;
  1813. int depth = 0;
  1814. /* Consider the case where self recursive function A is inlined
  1815. into B. This is desired optimization in some cases, since it
  1816. leads to effect similar of loop peeling and we might completely
  1817. optimize out the recursive call. However we must be extra
  1818. selective. */
  1819. where = edge->caller;
  1820. while (where->global.inlined_to)
  1821. {
  1822. if (where->decl == callee->decl)
  1823. outer_node = where, depth++;
  1824. where = where->callers->caller;
  1825. }
  1826. if (outer_node
  1827. && !want_inline_self_recursive_call_p (edge, outer_node,
  1828. true, depth))
  1829. {
  1830. edge->inline_failed
  1831. = (DECL_DISREGARD_INLINE_LIMITS (edge->callee->decl)
  1832. ? CIF_RECURSIVE_INLINING : CIF_UNSPECIFIED);
  1833. resolve_noninline_speculation (&edge_heap, edge);
  1834. continue;
  1835. }
  1836. else if (depth && dump_file)
  1837. fprintf (dump_file, " Peeling recursion with depth %i\n", depth);
  1838. gcc_checking_assert (!callee->global.inlined_to);
  1839. inline_call (edge, true, &new_indirect_edges, &overall_size, true);
  1840. add_new_edges_to_heap (&edge_heap, new_indirect_edges);
  1841. reset_edge_caches (edge->callee->function_symbol ());
  1842. update_callee_keys (&edge_heap, where, updated_nodes);
  1843. }
  1844. where = edge->caller;
  1845. if (where->global.inlined_to)
  1846. where = where->global.inlined_to;
  1847. /* Our profitability metric can depend on local properties
  1848. such as number of inlinable calls and size of the function body.
  1849. After inlining these properties might change for the function we
  1850. inlined into (since it's body size changed) and for the functions
  1851. called by function we inlined (since number of it inlinable callers
  1852. might change). */
  1853. update_caller_keys (&edge_heap, where, updated_nodes, NULL);
  1854. /* Offline copy count has possibly changed, recompute if profile is
  1855. available. */
  1856. if (max_count)
  1857. {
  1858. struct cgraph_node *n = cgraph_node::get (edge->callee->decl);
  1859. if (n != edge->callee && n->analyzed)
  1860. update_callee_keys (&edge_heap, n, updated_nodes);
  1861. }
  1862. bitmap_clear (updated_nodes);
  1863. if (dump_file)
  1864. {
  1865. fprintf (dump_file,
  1866. " Inlined into %s which now has time %i and size %i,"
  1867. "net change of %+i.\n",
  1868. edge->caller->name (),
  1869. inline_summaries->get (edge->caller)->time,
  1870. inline_summaries->get (edge->caller)->size,
  1871. overall_size - old_size);
  1872. }
  1873. if (min_size > overall_size)
  1874. {
  1875. min_size = overall_size;
  1876. max_size = compute_max_insns (min_size);
  1877. if (dump_file)
  1878. fprintf (dump_file, "New minimal size reached: %i\n", min_size);
  1879. }
  1880. }
  1881. free_growth_caches ();
  1882. if (dump_file)
  1883. fprintf (dump_file,
  1884. "Unit growth for small function inlining: %i->%i (%i%%)\n",
  1885. initial_size, overall_size,
  1886. initial_size ? overall_size * 100 / (initial_size) - 100: 0);
  1887. BITMAP_FREE (updated_nodes);
  1888. symtab->remove_edge_removal_hook (edge_removal_hook_holder);
  1889. }
  1890. /* Flatten NODE. Performed both during early inlining and
  1891. at IPA inlining time. */
  1892. static void
  1893. flatten_function (struct cgraph_node *node, bool early)
  1894. {
  1895. struct cgraph_edge *e;
  1896. /* We shouldn't be called recursively when we are being processed. */
  1897. gcc_assert (node->aux == NULL);
  1898. node->aux = (void *) node;
  1899. for (e = node->callees; e; e = e->next_callee)
  1900. {
  1901. struct cgraph_node *orig_callee;
  1902. struct cgraph_node *callee = e->callee->ultimate_alias_target ();
  1903. /* We've hit cycle? It is time to give up. */
  1904. if (callee->aux)
  1905. {
  1906. if (dump_file)
  1907. fprintf (dump_file,
  1908. "Not inlining %s into %s to avoid cycle.\n",
  1909. xstrdup_for_dump (callee->name ()),
  1910. xstrdup_for_dump (e->caller->name ()));
  1911. e->inline_failed = CIF_RECURSIVE_INLINING;
  1912. continue;
  1913. }
  1914. /* When the edge is already inlined, we just need to recurse into
  1915. it in order to fully flatten the leaves. */
  1916. if (!e->inline_failed)
  1917. {
  1918. flatten_function (callee, early);
  1919. continue;
  1920. }
  1921. /* Flatten attribute needs to be processed during late inlining. For
  1922. extra code quality we however do flattening during early optimization,
  1923. too. */
  1924. if (!early
  1925. ? !can_inline_edge_p (e, true)
  1926. : !can_early_inline_edge_p (e))
  1927. continue;
  1928. if (e->recursive_p ())
  1929. {
  1930. if (dump_file)
  1931. fprintf (dump_file, "Not inlining: recursive call.\n");
  1932. continue;
  1933. }
  1934. if (gimple_in_ssa_p (DECL_STRUCT_FUNCTION (node->decl))
  1935. != gimple_in_ssa_p (DECL_STRUCT_FUNCTION (callee->decl)))
  1936. {
  1937. if (dump_file)
  1938. fprintf (dump_file, "Not inlining: SSA form does not match.\n");
  1939. continue;
  1940. }
  1941. /* Inline the edge and flatten the inline clone. Avoid
  1942. recursing through the original node if the node was cloned. */
  1943. if (dump_file)
  1944. fprintf (dump_file, " Inlining %s into %s.\n",
  1945. xstrdup_for_dump (callee->name ()),
  1946. xstrdup_for_dump (e->caller->name ()));
  1947. orig_callee = callee;
  1948. inline_call (e, true, NULL, NULL, false);
  1949. if (e->callee != orig_callee)
  1950. orig_callee->aux = (void *) node;
  1951. flatten_function (e->callee, early);
  1952. if (e->callee != orig_callee)
  1953. orig_callee->aux = NULL;
  1954. }
  1955. node->aux = NULL;
  1956. if (!node->global.inlined_to)
  1957. inline_update_overall_summary (node);
  1958. }
  1959. /* Inline NODE to all callers. Worker for cgraph_for_node_and_aliases.
  1960. DATA points to number of calls originally found so we avoid infinite
  1961. recursion. */
  1962. static bool
  1963. inline_to_all_callers (struct cgraph_node *node, void *data)
  1964. {
  1965. int *num_calls = (int *)data;
  1966. bool callee_removed = false;
  1967. while (node->callers && !node->global.inlined_to)
  1968. {
  1969. struct cgraph_node *caller = node->callers->caller;
  1970. if (!can_inline_edge_p (node->callers, true)
  1971. || node->callers->recursive_p ())
  1972. {
  1973. if (dump_file)
  1974. fprintf (dump_file, "Uninlinable call found; giving up.\n");
  1975. *num_calls = 0;
  1976. return false;
  1977. }
  1978. if (dump_file)
  1979. {
  1980. fprintf (dump_file,
  1981. "\nInlining %s size %i.\n",
  1982. node->name (),
  1983. inline_summaries->get (node)->size);
  1984. fprintf (dump_file,
  1985. " Called once from %s %i insns.\n",
  1986. node->callers->caller->name (),
  1987. inline_summaries->get (node->callers->caller)->size);
  1988. }
  1989. inline_call (node->callers, true, NULL, NULL, true, &callee_removed);
  1990. if (dump_file)
  1991. fprintf (dump_file,
  1992. " Inlined into %s which now has %i size\n",
  1993. caller->name (),
  1994. inline_summaries->get (caller)->size);
  1995. if (!(*num_calls)--)
  1996. {
  1997. if (dump_file)
  1998. fprintf (dump_file, "New calls found; giving up.\n");
  1999. return callee_removed;
  2000. }
  2001. if (callee_removed)
  2002. return true;
  2003. }
  2004. return false;
  2005. }
  2006. /* Output overall time estimate. */
  2007. static void
  2008. dump_overall_stats (void)
  2009. {
  2010. int64_t sum_weighted = 0, sum = 0;
  2011. struct cgraph_node *node;
  2012. FOR_EACH_DEFINED_FUNCTION (node)
  2013. if (!node->global.inlined_to
  2014. && !node->alias)
  2015. {
  2016. int time = inline_summaries->get (node)->time;
  2017. sum += time;
  2018. sum_weighted += time * node->count;
  2019. }
  2020. fprintf (dump_file, "Overall time estimate: "
  2021. "%"PRId64" weighted by profile: "
  2022. "%"PRId64"\n", sum, sum_weighted);
  2023. }
  2024. /* Output some useful stats about inlining. */
  2025. static void
  2026. dump_inline_stats (void)
  2027. {
  2028. int64_t inlined_cnt = 0, inlined_indir_cnt = 0;
  2029. int64_t inlined_virt_cnt = 0, inlined_virt_indir_cnt = 0;
  2030. int64_t noninlined_cnt = 0, noninlined_indir_cnt = 0;
  2031. int64_t noninlined_virt_cnt = 0, noninlined_virt_indir_cnt = 0;
  2032. int64_t inlined_speculative = 0, inlined_speculative_ply = 0;
  2033. int64_t indirect_poly_cnt = 0, indirect_cnt = 0;
  2034. int64_t reason[CIF_N_REASONS][3];
  2035. int i;
  2036. struct cgraph_node *node;
  2037. memset (reason, 0, sizeof (reason));
  2038. FOR_EACH_DEFINED_FUNCTION (node)
  2039. {
  2040. struct cgraph_edge *e;
  2041. for (e = node->callees; e; e = e->next_callee)
  2042. {
  2043. if (e->inline_failed)
  2044. {
  2045. reason[(int) e->inline_failed][0] += e->count;
  2046. reason[(int) e->inline_failed][1] += e->frequency;
  2047. reason[(int) e->inline_failed][2] ++;
  2048. if (DECL_VIRTUAL_P (e->callee->decl))
  2049. {
  2050. if (e->indirect_inlining_edge)
  2051. noninlined_virt_indir_cnt += e->count;
  2052. else
  2053. noninlined_virt_cnt += e->count;
  2054. }
  2055. else
  2056. {
  2057. if (e->indirect_inlining_edge)
  2058. noninlined_indir_cnt += e->count;
  2059. else
  2060. noninlined_cnt += e->count;
  2061. }
  2062. }
  2063. else
  2064. {
  2065. if (e->speculative)
  2066. {
  2067. if (DECL_VIRTUAL_P (e->callee->decl))
  2068. inlined_speculative_ply += e->count;
  2069. else
  2070. inlined_speculative += e->count;
  2071. }
  2072. else if (DECL_VIRTUAL_P (e->callee->decl))
  2073. {
  2074. if (e->indirect_inlining_edge)
  2075. inlined_virt_indir_cnt += e->count;
  2076. else
  2077. inlined_virt_cnt += e->count;
  2078. }
  2079. else
  2080. {
  2081. if (e->indirect_inlining_edge)
  2082. inlined_indir_cnt += e->count;
  2083. else
  2084. inlined_cnt += e->count;
  2085. }
  2086. }
  2087. }
  2088. for (e = node->indirect_calls; e; e = e->next_callee)
  2089. if (e->indirect_info->polymorphic)
  2090. indirect_poly_cnt += e->count;
  2091. else
  2092. indirect_cnt += e->count;
  2093. }
  2094. if (max_count)
  2095. {
  2096. fprintf (dump_file,
  2097. "Inlined %"PRId64 " + speculative "
  2098. "%"PRId64 " + speculative polymorphic "
  2099. "%"PRId64 " + previously indirect "
  2100. "%"PRId64 " + virtual "
  2101. "%"PRId64 " + virtual and previously indirect "
  2102. "%"PRId64 "\n" "Not inlined "
  2103. "%"PRId64 " + previously indirect "
  2104. "%"PRId64 " + virtual "
  2105. "%"PRId64 " + virtual and previously indirect "
  2106. "%"PRId64 " + stil indirect "
  2107. "%"PRId64 " + still indirect polymorphic "
  2108. "%"PRId64 "\n", inlined_cnt,
  2109. inlined_speculative, inlined_speculative_ply,
  2110. inlined_indir_cnt, inlined_virt_cnt, inlined_virt_indir_cnt,
  2111. noninlined_cnt, noninlined_indir_cnt, noninlined_virt_cnt,
  2112. noninlined_virt_indir_cnt, indirect_cnt, indirect_poly_cnt);
  2113. fprintf (dump_file,
  2114. "Removed speculations %"PRId64 "\n",
  2115. spec_rem);
  2116. }
  2117. dump_overall_stats ();
  2118. fprintf (dump_file, "\nWhy inlining failed?\n");
  2119. for (i = 0; i < CIF_N_REASONS; i++)
  2120. if (reason[i][2])
  2121. fprintf (dump_file, "%-50s: %8i calls, %8i freq, %"PRId64" count\n",
  2122. cgraph_inline_failed_string ((cgraph_inline_failed_t) i),
  2123. (int) reason[i][2], (int) reason[i][1], reason[i][0]);
  2124. }
  2125. /* Decide on the inlining. We do so in the topological order to avoid
  2126. expenses on updating data structures. */
  2127. static unsigned int
  2128. ipa_inline (void)
  2129. {
  2130. struct cgraph_node *node;
  2131. int nnodes;
  2132. struct cgraph_node **order;
  2133. int i;
  2134. int cold;
  2135. bool remove_functions = false;
  2136. if (!optimize)
  2137. return 0;
  2138. cgraph_freq_base_rec = (sreal) 1 / (sreal) CGRAPH_FREQ_BASE;
  2139. percent_rec = (sreal) 1 / (sreal) 100;
  2140. order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
  2141. if (in_lto_p && optimize)
  2142. ipa_update_after_lto_read ();
  2143. if (dump_file)
  2144. dump_inline_summaries (dump_file);
  2145. nnodes = ipa_reverse_postorder (order);
  2146. FOR_EACH_FUNCTION (node)
  2147. {
  2148. node->aux = 0;
  2149. /* Recompute the default reasons for inlining because they may have
  2150. changed during merging. */
  2151. if (in_lto_p)
  2152. {
  2153. for (cgraph_edge *e = node->callees; e; e = e->next_callee)
  2154. {
  2155. gcc_assert (e->inline_failed);
  2156. initialize_inline_failed (e);
  2157. }
  2158. for (cgraph_edge *e = node->indirect_calls; e; e = e->next_callee)
  2159. initialize_inline_failed (e);
  2160. }
  2161. }
  2162. if (dump_file)
  2163. fprintf (dump_file, "\nFlattening functions:\n");
  2164. /* In the first pass handle functions to be flattened. Do this with
  2165. a priority so none of our later choices will make this impossible. */
  2166. for (i = nnodes - 1; i >= 0; i--)
  2167. {
  2168. node = order[i];
  2169. /* Handle nodes to be flattened.
  2170. Ideally when processing callees we stop inlining at the
  2171. entry of cycles, possibly cloning that entry point and
  2172. try to flatten itself turning it into a self-recursive
  2173. function. */
  2174. if (lookup_attribute ("flatten",
  2175. DECL_ATTRIBUTES (node->decl)) != NULL)
  2176. {
  2177. if (dump_file)
  2178. fprintf (dump_file,
  2179. "Flattening %s\n", node->name ());
  2180. flatten_function (node, false);
  2181. }
  2182. }
  2183. if (dump_file)
  2184. dump_overall_stats ();
  2185. inline_small_functions ();
  2186. gcc_assert (symtab->state == IPA_SSA);
  2187. symtab->state = IPA_SSA_AFTER_INLINING;
  2188. /* Do first after-inlining removal. We want to remove all "stale" extern
  2189. inline functions and virtual functions so we really know what is called
  2190. once. */
  2191. symtab->remove_unreachable_nodes (dump_file);
  2192. free (order);
  2193. /* Inline functions with a property that after inlining into all callers the
  2194. code size will shrink because the out-of-line copy is eliminated.
  2195. We do this regardless on the callee size as long as function growth limits
  2196. are met. */
  2197. if (dump_file)
  2198. fprintf (dump_file,
  2199. "\nDeciding on functions to be inlined into all callers and "
  2200. "removing useless speculations:\n");
  2201. /* Inlining one function called once has good chance of preventing
  2202. inlining other function into the same callee. Ideally we should
  2203. work in priority order, but probably inlining hot functions first
  2204. is good cut without the extra pain of maintaining the queue.
  2205. ??? this is not really fitting the bill perfectly: inlining function
  2206. into callee often leads to better optimization of callee due to
  2207. increased context for optimization.
  2208. For example if main() function calls a function that outputs help
  2209. and then function that does the main optmization, we should inline
  2210. the second with priority even if both calls are cold by themselves.
  2211. We probably want to implement new predicate replacing our use of
  2212. maybe_hot_edge interpreted as maybe_hot_edge || callee is known
  2213. to be hot. */
  2214. for (cold = 0; cold <= 1; cold ++)
  2215. {
  2216. FOR_EACH_DEFINED_FUNCTION (node)
  2217. {
  2218. struct cgraph_edge *edge, *next;
  2219. bool update=false;
  2220. for (edge = node->callees; edge; edge = next)
  2221. {
  2222. next = edge->next_callee;
  2223. if (edge->speculative && !speculation_useful_p (edge, false))
  2224. {
  2225. edge->resolve_speculation ();
  2226. spec_rem += edge->count;
  2227. update = true;
  2228. remove_functions = true;
  2229. }
  2230. }
  2231. if (update)
  2232. {
  2233. struct cgraph_node *where = node->global.inlined_to
  2234. ? node->global.inlined_to : node;
  2235. reset_edge_caches (where);
  2236. inline_update_overall_summary (where);
  2237. }
  2238. if (want_inline_function_to_all_callers_p (node, cold))
  2239. {
  2240. int num_calls = 0;
  2241. node->call_for_symbol_and_aliases (sum_callers, &num_calls,
  2242. true);
  2243. while (node->call_for_symbol_and_aliases
  2244. (inline_to_all_callers, &num_calls, true))
  2245. ;
  2246. remove_functions = true;
  2247. }
  2248. }
  2249. }
  2250. /* Free ipa-prop structures if they are no longer needed. */
  2251. if (optimize)
  2252. ipa_free_all_structures_after_iinln ();
  2253. if (dump_file)
  2254. {
  2255. fprintf (dump_file,
  2256. "\nInlined %i calls, eliminated %i functions\n\n",
  2257. ncalls_inlined, nfunctions_inlined);
  2258. dump_inline_stats ();
  2259. }
  2260. if (dump_file)
  2261. dump_inline_summaries (dump_file);
  2262. /* In WPA we use inline summaries for partitioning process. */
  2263. if (!flag_wpa)
  2264. inline_free_summary ();
  2265. return remove_functions ? TODO_remove_functions : 0;
  2266. }
  2267. /* Inline always-inline function calls in NODE. */
  2268. static bool
  2269. inline_always_inline_functions (struct cgraph_node *node)
  2270. {
  2271. struct cgraph_edge *e;
  2272. bool inlined = false;
  2273. for (e = node->callees; e; e = e->next_callee)
  2274. {
  2275. struct cgraph_node *callee = e->callee->ultimate_alias_target ();
  2276. if (!DECL_DISREGARD_INLINE_LIMITS (callee->decl))
  2277. continue;
  2278. if (e->recursive_p ())
  2279. {
  2280. if (dump_file)
  2281. fprintf (dump_file, " Not inlining recursive call to %s.\n",
  2282. e->callee->name ());
  2283. e->inline_failed = CIF_RECURSIVE_INLINING;
  2284. continue;
  2285. }
  2286. if (!can_early_inline_edge_p (e))
  2287. {
  2288. /* Set inlined to true if the callee is marked "always_inline" but
  2289. is not inlinable. This will allow flagging an error later in
  2290. expand_call_inline in tree-inline.c. */
  2291. if (lookup_attribute ("always_inline",
  2292. DECL_ATTRIBUTES (callee->decl)) != NULL)
  2293. inlined = true;
  2294. continue;
  2295. }
  2296. if (dump_file)
  2297. fprintf (dump_file, " Inlining %s into %s (always_inline).\n",
  2298. xstrdup_for_dump (e->callee->name ()),
  2299. xstrdup_for_dump (e->caller->name ()));
  2300. inline_call (e, true, NULL, NULL, false);
  2301. inlined = true;
  2302. }
  2303. if (inlined)
  2304. inline_update_overall_summary (node);
  2305. return inlined;
  2306. }
  2307. /* Decide on the inlining. We do so in the topological order to avoid
  2308. expenses on updating data structures. */
  2309. static bool
  2310. early_inline_small_functions (struct cgraph_node *node)
  2311. {
  2312. struct cgraph_edge *e;
  2313. bool inlined = false;
  2314. for (e = node->callees; e; e = e->next_callee)
  2315. {
  2316. struct cgraph_node *callee = e->callee->ultimate_alias_target ();
  2317. if (!inline_summaries->get (callee)->inlinable
  2318. || !e->inline_failed)
  2319. continue;
  2320. /* Do not consider functions not declared inline. */
  2321. if (!DECL_DECLARED_INLINE_P (callee->decl)
  2322. && !opt_for_fn (node->decl, flag_inline_small_functions)
  2323. && !opt_for_fn (node->decl, flag_inline_functions))
  2324. continue;
  2325. if (dump_file)
  2326. fprintf (dump_file, "Considering inline candidate %s.\n",
  2327. callee->name ());
  2328. if (!can_early_inline_edge_p (e))
  2329. continue;
  2330. if (e->recursive_p ())
  2331. {
  2332. if (dump_file)
  2333. fprintf (dump_file, " Not inlining: recursive call.\n");
  2334. continue;
  2335. }
  2336. if (!want_early_inline_function_p (e))
  2337. continue;
  2338. if (dump_file)
  2339. fprintf (dump_file, " Inlining %s into %s.\n",
  2340. xstrdup_for_dump (callee->name ()),
  2341. xstrdup_for_dump (e->caller->name ()));
  2342. inline_call (e, true, NULL, NULL, true);
  2343. inlined = true;
  2344. }
  2345. return inlined;
  2346. }
  2347. unsigned int
  2348. early_inliner (function *fun)
  2349. {
  2350. struct cgraph_node *node = cgraph_node::get (current_function_decl);
  2351. struct cgraph_edge *edge;
  2352. unsigned int todo = 0;
  2353. int iterations = 0;
  2354. bool inlined = false;
  2355. if (seen_error ())
  2356. return 0;
  2357. /* Do nothing if datastructures for ipa-inliner are already computed. This
  2358. happens when some pass decides to construct new function and
  2359. cgraph_add_new_function calls lowering passes and early optimization on
  2360. it. This may confuse ourself when early inliner decide to inline call to
  2361. function clone, because function clones don't have parameter list in
  2362. ipa-prop matching their signature. */
  2363. if (ipa_node_params_sum)
  2364. return 0;
  2365. #ifdef ENABLE_CHECKING
  2366. node->verify ();
  2367. #endif
  2368. node->remove_all_references ();
  2369. /* Rebuild this reference because it dosn't depend on
  2370. function's body and it's required to pass cgraph_node
  2371. verification. */
  2372. if (node->instrumented_version
  2373. && !node->instrumentation_clone)
  2374. node->create_reference (node->instrumented_version, IPA_REF_CHKP, NULL);
  2375. /* Even when not optimizing or not inlining inline always-inline
  2376. functions. */
  2377. inlined = inline_always_inline_functions (node);
  2378. if (!optimize
  2379. || flag_no_inline
  2380. || !flag_early_inlining
  2381. /* Never inline regular functions into always-inline functions
  2382. during incremental inlining. This sucks as functions calling
  2383. always inline functions will get less optimized, but at the
  2384. same time inlining of functions calling always inline
  2385. function into an always inline function might introduce
  2386. cycles of edges to be always inlined in the callgraph.
  2387. We might want to be smarter and just avoid this type of inlining. */
  2388. || (DECL_DISREGARD_INLINE_LIMITS (node->decl)
  2389. && lookup_attribute ("always_inline",
  2390. DECL_ATTRIBUTES (node->decl))))
  2391. ;
  2392. else if (lookup_attribute ("flatten",
  2393. DECL_ATTRIBUTES (node->decl)) != NULL)
  2394. {
  2395. /* When the function is marked to be flattened, recursively inline
  2396. all calls in it. */
  2397. if (dump_file)
  2398. fprintf (dump_file,
  2399. "Flattening %s\n", node->name ());
  2400. flatten_function (node, true);
  2401. inlined = true;
  2402. }
  2403. else
  2404. {
  2405. /* If some always_inline functions was inlined, apply the changes.
  2406. This way we will not account always inline into growth limits and
  2407. moreover we will inline calls from always inlines that we skipped
  2408. previously becuase of conditional above. */
  2409. if (inlined)
  2410. {
  2411. timevar_push (TV_INTEGRATION);
  2412. todo |= optimize_inline_calls (current_function_decl);
  2413. /* optimize_inline_calls call above might have introduced new
  2414. statements that don't have inline parameters computed. */
  2415. for (edge = node->callees; edge; edge = edge->next_callee)
  2416. {
  2417. if (inline_edge_summary_vec.length () > (unsigned) edge->uid)
  2418. {
  2419. struct inline_edge_summary *es = inline_edge_summary (edge);
  2420. es->call_stmt_size
  2421. = estimate_num_insns (edge->call_stmt, &eni_size_weights);
  2422. es->call_stmt_time
  2423. = estimate_num_insns (edge->call_stmt, &eni_time_weights);
  2424. }
  2425. }
  2426. inline_update_overall_summary (node);
  2427. inlined = false;
  2428. timevar_pop (TV_INTEGRATION);
  2429. }
  2430. /* We iterate incremental inlining to get trivial cases of indirect
  2431. inlining. */
  2432. while (iterations < PARAM_VALUE (PARAM_EARLY_INLINER_MAX_ITERATIONS)
  2433. && early_inline_small_functions (node))
  2434. {
  2435. timevar_push (TV_INTEGRATION);
  2436. todo |= optimize_inline_calls (current_function_decl);
  2437. /* Technically we ought to recompute inline parameters so the new
  2438. iteration of early inliner works as expected. We however have
  2439. values approximately right and thus we only need to update edge
  2440. info that might be cleared out for newly discovered edges. */
  2441. for (edge = node->callees; edge; edge = edge->next_callee)
  2442. {
  2443. /* We have no summary for new bound store calls yet. */
  2444. if (inline_edge_summary_vec.length () > (unsigned)edge->uid)
  2445. {
  2446. struct inline_edge_summary *es = inline_edge_summary (edge);
  2447. es->call_stmt_size
  2448. = estimate_num_insns (edge->call_stmt, &eni_size_weights);
  2449. es->call_stmt_time
  2450. = estimate_num_insns (edge->call_stmt, &eni_time_weights);
  2451. }
  2452. if (edge->callee->decl
  2453. && !gimple_check_call_matching_types (
  2454. edge->call_stmt, edge->callee->decl, false))
  2455. edge->call_stmt_cannot_inline_p = true;
  2456. }
  2457. if (iterations < PARAM_VALUE (PARAM_EARLY_INLINER_MAX_ITERATIONS) - 1)
  2458. inline_update_overall_summary (node);
  2459. timevar_pop (TV_INTEGRATION);
  2460. iterations++;
  2461. inlined = false;
  2462. }
  2463. if (dump_file)
  2464. fprintf (dump_file, "Iterations: %i\n", iterations);
  2465. }
  2466. if (inlined)
  2467. {
  2468. timevar_push (TV_INTEGRATION);
  2469. todo |= optimize_inline_calls (current_function_decl);
  2470. timevar_pop (TV_INTEGRATION);
  2471. }
  2472. fun->always_inline_functions_inlined = true;
  2473. return todo;
  2474. }
  2475. /* Do inlining of small functions. Doing so early helps profiling and other
  2476. passes to be somewhat more effective and avoids some code duplication in
  2477. later real inlining pass for testcases with very many function calls. */
  2478. namespace {
  2479. const pass_data pass_data_early_inline =
  2480. {
  2481. GIMPLE_PASS, /* type */
  2482. "einline", /* name */
  2483. OPTGROUP_INLINE, /* optinfo_flags */
  2484. TV_EARLY_INLINING, /* tv_id */
  2485. PROP_ssa, /* properties_required */
  2486. 0, /* properties_provided */
  2487. 0, /* properties_destroyed */
  2488. 0, /* todo_flags_start */
  2489. 0, /* todo_flags_finish */
  2490. };
  2491. class pass_early_inline : public gimple_opt_pass
  2492. {
  2493. public:
  2494. pass_early_inline (gcc::context *ctxt)
  2495. : gimple_opt_pass (pass_data_early_inline, ctxt)
  2496. {}
  2497. /* opt_pass methods: */
  2498. virtual unsigned int execute (function *);
  2499. }; // class pass_early_inline
  2500. unsigned int
  2501. pass_early_inline::execute (function *fun)
  2502. {
  2503. return early_inliner (fun);
  2504. }
  2505. } // anon namespace
  2506. gimple_opt_pass *
  2507. make_pass_early_inline (gcc::context *ctxt)
  2508. {
  2509. return new pass_early_inline (ctxt);
  2510. }
  2511. namespace {
  2512. const pass_data pass_data_ipa_inline =
  2513. {
  2514. IPA_PASS, /* type */
  2515. "inline", /* name */
  2516. OPTGROUP_INLINE, /* optinfo_flags */
  2517. TV_IPA_INLINING, /* tv_id */
  2518. 0, /* properties_required */
  2519. 0, /* properties_provided */
  2520. 0, /* properties_destroyed */
  2521. 0, /* todo_flags_start */
  2522. ( TODO_dump_symtab ), /* todo_flags_finish */
  2523. };
  2524. class pass_ipa_inline : public ipa_opt_pass_d
  2525. {
  2526. public:
  2527. pass_ipa_inline (gcc::context *ctxt)
  2528. : ipa_opt_pass_d (pass_data_ipa_inline, ctxt,
  2529. inline_generate_summary, /* generate_summary */
  2530. inline_write_summary, /* write_summary */
  2531. inline_read_summary, /* read_summary */
  2532. NULL, /* write_optimization_summary */
  2533. NULL, /* read_optimization_summary */
  2534. NULL, /* stmt_fixup */
  2535. 0, /* function_transform_todo_flags_start */
  2536. inline_transform, /* function_transform */
  2537. NULL) /* variable_transform */
  2538. {}
  2539. /* opt_pass methods: */
  2540. virtual unsigned int execute (function *) { return ipa_inline (); }
  2541. }; // class pass_ipa_inline
  2542. } // anon namespace
  2543. ipa_opt_pass_d *
  2544. make_pass_ipa_inline (gcc::context *ctxt)
  2545. {
  2546. return new pass_ipa_inline (ctxt);
  2547. }