ipa-inline-analysis.c 129 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467
  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. /* Analysis used by the inliner and other passes limiting code size growth.
  17. We estimate for each function
  18. - function body size
  19. - average function execution time
  20. - inlining size benefit (that is how much of function body size
  21. and its call sequence is expected to disappear by inlining)
  22. - inlining time benefit
  23. - function frame size
  24. For each call
  25. - call statement size and time
  26. inlinie_summary datastructures store above information locally (i.e.
  27. parameters of the function itself) and globally (i.e. parameters of
  28. the function created by applying all the inline decisions already
  29. present in the callgraph).
  30. We provide accestor to the inline_summary datastructure and
  31. basic logic updating the parameters when inlining is performed.
  32. The summaries are context sensitive. Context means
  33. 1) partial assignment of known constant values of operands
  34. 2) whether function is inlined into the call or not.
  35. It is easy to add more variants. To represent function size and time
  36. that depends on context (i.e. it is known to be optimized away when
  37. context is known either by inlining or from IP-CP and clonning),
  38. we use predicates. Predicates are logical formulas in
  39. conjunctive-disjunctive form consisting of clauses. Clauses are bitmaps
  40. specifying what conditions must be true. Conditions are simple test
  41. of the form described above.
  42. In order to make predicate (possibly) true, all of its clauses must
  43. be (possibly) true. To make clause (possibly) true, one of conditions
  44. it mentions must be (possibly) true. There are fixed bounds on
  45. number of clauses and conditions and all the manipulation functions
  46. are conservative in positive direction. I.e. we may lose precision
  47. by thinking that predicate may be true even when it is not.
  48. estimate_edge_size and estimate_edge_growth can be used to query
  49. function size/time in the given context. inline_merge_summary merges
  50. properties of caller and callee after inlining.
  51. Finally pass_inline_parameters is exported. This is used to drive
  52. computation of function parameters used by the early inliner. IPA
  53. inlined performs analysis via its analyze_function method. */
  54. #include "config.h"
  55. #include "system.h"
  56. #include "coretypes.h"
  57. #include "tm.h"
  58. #include "hash-set.h"
  59. #include "machmode.h"
  60. #include "vec.h"
  61. #include "double-int.h"
  62. #include "input.h"
  63. #include "alias.h"
  64. #include "symtab.h"
  65. #include "wide-int.h"
  66. #include "inchash.h"
  67. #include "real.h"
  68. #include "tree.h"
  69. #include "fold-const.h"
  70. #include "stor-layout.h"
  71. #include "stringpool.h"
  72. #include "print-tree.h"
  73. #include "tree-inline.h"
  74. #include "langhooks.h"
  75. #include "flags.h"
  76. #include "diagnostic.h"
  77. #include "gimple-pretty-print.h"
  78. #include "params.h"
  79. #include "tree-pass.h"
  80. #include "coverage.h"
  81. #include "predict.h"
  82. #include "hard-reg-set.h"
  83. #include "input.h"
  84. #include "function.h"
  85. #include "dominance.h"
  86. #include "cfg.h"
  87. #include "cfganal.h"
  88. #include "basic-block.h"
  89. #include "tree-ssa-alias.h"
  90. #include "internal-fn.h"
  91. #include "gimple-expr.h"
  92. #include "is-a.h"
  93. #include "gimple.h"
  94. #include "gimple-iterator.h"
  95. #include "gimple-ssa.h"
  96. #include "tree-cfg.h"
  97. #include "tree-phinodes.h"
  98. #include "ssa-iterators.h"
  99. #include "tree-ssanames.h"
  100. #include "tree-ssa-loop-niter.h"
  101. #include "tree-ssa-loop.h"
  102. #include "hash-map.h"
  103. #include "plugin-api.h"
  104. #include "ipa-ref.h"
  105. #include "cgraph.h"
  106. #include "alloc-pool.h"
  107. #include "symbol-summary.h"
  108. #include "ipa-prop.h"
  109. #include "lto-streamer.h"
  110. #include "data-streamer.h"
  111. #include "tree-streamer.h"
  112. #include "ipa-inline.h"
  113. #include "cfgloop.h"
  114. #include "tree-scalar-evolution.h"
  115. #include "ipa-utils.h"
  116. #include "cilk.h"
  117. #include "cfgexpand.h"
  118. /* Estimate runtime of function can easilly run into huge numbers with many
  119. nested loops. Be sure we can compute time * INLINE_SIZE_SCALE * 2 in an
  120. integer. For anything larger we use gcov_type. */
  121. #define MAX_TIME 500000
  122. /* Number of bits in integer, but we really want to be stable across different
  123. hosts. */
  124. #define NUM_CONDITIONS 32
  125. enum predicate_conditions
  126. {
  127. predicate_false_condition = 0,
  128. predicate_not_inlined_condition = 1,
  129. predicate_first_dynamic_condition = 2
  130. };
  131. /* Special condition code we use to represent test that operand is compile time
  132. constant. */
  133. #define IS_NOT_CONSTANT ERROR_MARK
  134. /* Special condition code we use to represent test that operand is not changed
  135. across invocation of the function. When operand IS_NOT_CONSTANT it is always
  136. CHANGED, however i.e. loop invariants can be NOT_CHANGED given percentage
  137. of executions even when they are not compile time constants. */
  138. #define CHANGED IDENTIFIER_NODE
  139. /* Holders of ipa cgraph hooks: */
  140. static struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
  141. static struct cgraph_edge_hook_list *edge_removal_hook_holder;
  142. static void inline_edge_removal_hook (struct cgraph_edge *, void *);
  143. static void inline_edge_duplication_hook (struct cgraph_edge *,
  144. struct cgraph_edge *, void *);
  145. /* VECtor holding inline summaries.
  146. In GGC memory because conditions might point to constant trees. */
  147. function_summary <inline_summary *> *inline_summaries;
  148. vec<inline_edge_summary_t> inline_edge_summary_vec;
  149. /* Cached node/edge growths. */
  150. vec<edge_growth_cache_entry> edge_growth_cache;
  151. /* Edge predicates goes here. */
  152. static alloc_pool edge_predicate_pool;
  153. /* Return true predicate (tautology).
  154. We represent it by empty list of clauses. */
  155. static inline struct predicate
  156. true_predicate (void)
  157. {
  158. struct predicate p;
  159. p.clause[0] = 0;
  160. return p;
  161. }
  162. /* Return predicate testing single condition number COND. */
  163. static inline struct predicate
  164. single_cond_predicate (int cond)
  165. {
  166. struct predicate p;
  167. p.clause[0] = 1 << cond;
  168. p.clause[1] = 0;
  169. return p;
  170. }
  171. /* Return false predicate. First clause require false condition. */
  172. static inline struct predicate
  173. false_predicate (void)
  174. {
  175. return single_cond_predicate (predicate_false_condition);
  176. }
  177. /* Return true if P is (true). */
  178. static inline bool
  179. true_predicate_p (struct predicate *p)
  180. {
  181. return !p->clause[0];
  182. }
  183. /* Return true if P is (false). */
  184. static inline bool
  185. false_predicate_p (struct predicate *p)
  186. {
  187. if (p->clause[0] == (1 << predicate_false_condition))
  188. {
  189. gcc_checking_assert (!p->clause[1]
  190. && p->clause[0] == 1 << predicate_false_condition);
  191. return true;
  192. }
  193. return false;
  194. }
  195. /* Return predicate that is set true when function is not inlined. */
  196. static inline struct predicate
  197. not_inlined_predicate (void)
  198. {
  199. return single_cond_predicate (predicate_not_inlined_condition);
  200. }
  201. /* Simple description of whether a memory load or a condition refers to a load
  202. from an aggregate and if so, how and where from in the aggregate.
  203. Individual fields have the same meaning like fields with the same name in
  204. struct condition. */
  205. struct agg_position_info
  206. {
  207. HOST_WIDE_INT offset;
  208. bool agg_contents;
  209. bool by_ref;
  210. };
  211. /* Add condition to condition list CONDS. AGGPOS describes whether the used
  212. oprand is loaded from an aggregate and where in the aggregate it is. It can
  213. be NULL, which means this not a load from an aggregate. */
  214. static struct predicate
  215. add_condition (struct inline_summary *summary, int operand_num,
  216. struct agg_position_info *aggpos,
  217. enum tree_code code, tree val)
  218. {
  219. int i;
  220. struct condition *c;
  221. struct condition new_cond;
  222. HOST_WIDE_INT offset;
  223. bool agg_contents, by_ref;
  224. if (aggpos)
  225. {
  226. offset = aggpos->offset;
  227. agg_contents = aggpos->agg_contents;
  228. by_ref = aggpos->by_ref;
  229. }
  230. else
  231. {
  232. offset = 0;
  233. agg_contents = false;
  234. by_ref = false;
  235. }
  236. gcc_checking_assert (operand_num >= 0);
  237. for (i = 0; vec_safe_iterate (summary->conds, i, &c); i++)
  238. {
  239. if (c->operand_num == operand_num
  240. && c->code == code
  241. && c->val == val
  242. && c->agg_contents == agg_contents
  243. && (!agg_contents || (c->offset == offset && c->by_ref == by_ref)))
  244. return single_cond_predicate (i + predicate_first_dynamic_condition);
  245. }
  246. /* Too many conditions. Give up and return constant true. */
  247. if (i == NUM_CONDITIONS - predicate_first_dynamic_condition)
  248. return true_predicate ();
  249. new_cond.operand_num = operand_num;
  250. new_cond.code = code;
  251. new_cond.val = val;
  252. new_cond.agg_contents = agg_contents;
  253. new_cond.by_ref = by_ref;
  254. new_cond.offset = offset;
  255. vec_safe_push (summary->conds, new_cond);
  256. return single_cond_predicate (i + predicate_first_dynamic_condition);
  257. }
  258. /* Add clause CLAUSE into the predicate P. */
  259. static inline void
  260. add_clause (conditions conditions, struct predicate *p, clause_t clause)
  261. {
  262. int i;
  263. int i2;
  264. int insert_here = -1;
  265. int c1, c2;
  266. /* True clause. */
  267. if (!clause)
  268. return;
  269. /* False clause makes the whole predicate false. Kill the other variants. */
  270. if (clause == (1 << predicate_false_condition))
  271. {
  272. p->clause[0] = (1 << predicate_false_condition);
  273. p->clause[1] = 0;
  274. return;
  275. }
  276. if (false_predicate_p (p))
  277. return;
  278. /* No one should be silly enough to add false into nontrivial clauses. */
  279. gcc_checking_assert (!(clause & (1 << predicate_false_condition)));
  280. /* Look where to insert the clause. At the same time prune out
  281. clauses of P that are implied by the new clause and thus
  282. redundant. */
  283. for (i = 0, i2 = 0; i <= MAX_CLAUSES; i++)
  284. {
  285. p->clause[i2] = p->clause[i];
  286. if (!p->clause[i])
  287. break;
  288. /* If p->clause[i] implies clause, there is nothing to add. */
  289. if ((p->clause[i] & clause) == p->clause[i])
  290. {
  291. /* We had nothing to add, none of clauses should've become
  292. redundant. */
  293. gcc_checking_assert (i == i2);
  294. return;
  295. }
  296. if (p->clause[i] < clause && insert_here < 0)
  297. insert_here = i2;
  298. /* If clause implies p->clause[i], then p->clause[i] becomes redundant.
  299. Otherwise the p->clause[i] has to stay. */
  300. if ((p->clause[i] & clause) != clause)
  301. i2++;
  302. }
  303. /* Look for clauses that are obviously true. I.e.
  304. op0 == 5 || op0 != 5. */
  305. for (c1 = predicate_first_dynamic_condition; c1 < NUM_CONDITIONS; c1++)
  306. {
  307. condition *cc1;
  308. if (!(clause & (1 << c1)))
  309. continue;
  310. cc1 = &(*conditions)[c1 - predicate_first_dynamic_condition];
  311. /* We have no way to represent !CHANGED and !IS_NOT_CONSTANT
  312. and thus there is no point for looking for them. */
  313. if (cc1->code == CHANGED || cc1->code == IS_NOT_CONSTANT)
  314. continue;
  315. for (c2 = c1 + 1; c2 < NUM_CONDITIONS; c2++)
  316. if (clause & (1 << c2))
  317. {
  318. condition *cc1 =
  319. &(*conditions)[c1 - predicate_first_dynamic_condition];
  320. condition *cc2 =
  321. &(*conditions)[c2 - predicate_first_dynamic_condition];
  322. if (cc1->operand_num == cc2->operand_num
  323. && cc1->val == cc2->val
  324. && cc2->code != IS_NOT_CONSTANT
  325. && cc2->code != CHANGED
  326. && cc1->code == invert_tree_comparison (cc2->code,
  327. HONOR_NANS (cc1->val)))
  328. return;
  329. }
  330. }
  331. /* We run out of variants. Be conservative in positive direction. */
  332. if (i2 == MAX_CLAUSES)
  333. return;
  334. /* Keep clauses in decreasing order. This makes equivalence testing easy. */
  335. p->clause[i2 + 1] = 0;
  336. if (insert_here >= 0)
  337. for (; i2 > insert_here; i2--)
  338. p->clause[i2] = p->clause[i2 - 1];
  339. else
  340. insert_here = i2;
  341. p->clause[insert_here] = clause;
  342. }
  343. /* Return P & P2. */
  344. static struct predicate
  345. and_predicates (conditions conditions,
  346. struct predicate *p, struct predicate *p2)
  347. {
  348. struct predicate out = *p;
  349. int i;
  350. /* Avoid busy work. */
  351. if (false_predicate_p (p2) || true_predicate_p (p))
  352. return *p2;
  353. if (false_predicate_p (p) || true_predicate_p (p2))
  354. return *p;
  355. /* See how far predicates match. */
  356. for (i = 0; p->clause[i] && p->clause[i] == p2->clause[i]; i++)
  357. {
  358. gcc_checking_assert (i < MAX_CLAUSES);
  359. }
  360. /* Combine the predicates rest. */
  361. for (; p2->clause[i]; i++)
  362. {
  363. gcc_checking_assert (i < MAX_CLAUSES);
  364. add_clause (conditions, &out, p2->clause[i]);
  365. }
  366. return out;
  367. }
  368. /* Return true if predicates are obviously equal. */
  369. static inline bool
  370. predicates_equal_p (struct predicate *p, struct predicate *p2)
  371. {
  372. int i;
  373. for (i = 0; p->clause[i]; i++)
  374. {
  375. gcc_checking_assert (i < MAX_CLAUSES);
  376. gcc_checking_assert (p->clause[i] > p->clause[i + 1]);
  377. gcc_checking_assert (!p2->clause[i]
  378. || p2->clause[i] > p2->clause[i + 1]);
  379. if (p->clause[i] != p2->clause[i])
  380. return false;
  381. }
  382. return !p2->clause[i];
  383. }
  384. /* Return P | P2. */
  385. static struct predicate
  386. or_predicates (conditions conditions,
  387. struct predicate *p, struct predicate *p2)
  388. {
  389. struct predicate out = true_predicate ();
  390. int i, j;
  391. /* Avoid busy work. */
  392. if (false_predicate_p (p2) || true_predicate_p (p))
  393. return *p;
  394. if (false_predicate_p (p) || true_predicate_p (p2))
  395. return *p2;
  396. if (predicates_equal_p (p, p2))
  397. return *p;
  398. /* OK, combine the predicates. */
  399. for (i = 0; p->clause[i]; i++)
  400. for (j = 0; p2->clause[j]; j++)
  401. {
  402. gcc_checking_assert (i < MAX_CLAUSES && j < MAX_CLAUSES);
  403. add_clause (conditions, &out, p->clause[i] | p2->clause[j]);
  404. }
  405. return out;
  406. }
  407. /* Having partial truth assignment in POSSIBLE_TRUTHS, return false
  408. if predicate P is known to be false. */
  409. static bool
  410. evaluate_predicate (struct predicate *p, clause_t possible_truths)
  411. {
  412. int i;
  413. /* True remains true. */
  414. if (true_predicate_p (p))
  415. return true;
  416. gcc_assert (!(possible_truths & (1 << predicate_false_condition)));
  417. /* See if we can find clause we can disprove. */
  418. for (i = 0; p->clause[i]; i++)
  419. {
  420. gcc_checking_assert (i < MAX_CLAUSES);
  421. if (!(p->clause[i] & possible_truths))
  422. return false;
  423. }
  424. return true;
  425. }
  426. /* Return the probability in range 0...REG_BR_PROB_BASE that the predicated
  427. instruction will be recomputed per invocation of the inlined call. */
  428. static int
  429. predicate_probability (conditions conds,
  430. struct predicate *p, clause_t possible_truths,
  431. vec<inline_param_summary> inline_param_summary)
  432. {
  433. int i;
  434. int combined_prob = REG_BR_PROB_BASE;
  435. /* True remains true. */
  436. if (true_predicate_p (p))
  437. return REG_BR_PROB_BASE;
  438. if (false_predicate_p (p))
  439. return 0;
  440. gcc_assert (!(possible_truths & (1 << predicate_false_condition)));
  441. /* See if we can find clause we can disprove. */
  442. for (i = 0; p->clause[i]; i++)
  443. {
  444. gcc_checking_assert (i < MAX_CLAUSES);
  445. if (!(p->clause[i] & possible_truths))
  446. return 0;
  447. else
  448. {
  449. int this_prob = 0;
  450. int i2;
  451. if (!inline_param_summary.exists ())
  452. return REG_BR_PROB_BASE;
  453. for (i2 = 0; i2 < NUM_CONDITIONS; i2++)
  454. if ((p->clause[i] & possible_truths) & (1 << i2))
  455. {
  456. if (i2 >= predicate_first_dynamic_condition)
  457. {
  458. condition *c =
  459. &(*conds)[i2 - predicate_first_dynamic_condition];
  460. if (c->code == CHANGED
  461. && (c->operand_num <
  462. (int) inline_param_summary.length ()))
  463. {
  464. int iprob =
  465. inline_param_summary[c->operand_num].change_prob;
  466. this_prob = MAX (this_prob, iprob);
  467. }
  468. else
  469. this_prob = REG_BR_PROB_BASE;
  470. }
  471. else
  472. this_prob = REG_BR_PROB_BASE;
  473. }
  474. combined_prob = MIN (this_prob, combined_prob);
  475. if (!combined_prob)
  476. return 0;
  477. }
  478. }
  479. return combined_prob;
  480. }
  481. /* Dump conditional COND. */
  482. static void
  483. dump_condition (FILE *f, conditions conditions, int cond)
  484. {
  485. condition *c;
  486. if (cond == predicate_false_condition)
  487. fprintf (f, "false");
  488. else if (cond == predicate_not_inlined_condition)
  489. fprintf (f, "not inlined");
  490. else
  491. {
  492. c = &(*conditions)[cond - predicate_first_dynamic_condition];
  493. fprintf (f, "op%i", c->operand_num);
  494. if (c->agg_contents)
  495. fprintf (f, "[%soffset: " HOST_WIDE_INT_PRINT_DEC "]",
  496. c->by_ref ? "ref " : "", c->offset);
  497. if (c->code == IS_NOT_CONSTANT)
  498. {
  499. fprintf (f, " not constant");
  500. return;
  501. }
  502. if (c->code == CHANGED)
  503. {
  504. fprintf (f, " changed");
  505. return;
  506. }
  507. fprintf (f, " %s ", op_symbol_code (c->code));
  508. print_generic_expr (f, c->val, 1);
  509. }
  510. }
  511. /* Dump clause CLAUSE. */
  512. static void
  513. dump_clause (FILE *f, conditions conds, clause_t clause)
  514. {
  515. int i;
  516. bool found = false;
  517. fprintf (f, "(");
  518. if (!clause)
  519. fprintf (f, "true");
  520. for (i = 0; i < NUM_CONDITIONS; i++)
  521. if (clause & (1 << i))
  522. {
  523. if (found)
  524. fprintf (f, " || ");
  525. found = true;
  526. dump_condition (f, conds, i);
  527. }
  528. fprintf (f, ")");
  529. }
  530. /* Dump predicate PREDICATE. */
  531. static void
  532. dump_predicate (FILE *f, conditions conds, struct predicate *pred)
  533. {
  534. int i;
  535. if (true_predicate_p (pred))
  536. dump_clause (f, conds, 0);
  537. else
  538. for (i = 0; pred->clause[i]; i++)
  539. {
  540. if (i)
  541. fprintf (f, " && ");
  542. dump_clause (f, conds, pred->clause[i]);
  543. }
  544. fprintf (f, "\n");
  545. }
  546. /* Dump inline hints. */
  547. void
  548. dump_inline_hints (FILE *f, inline_hints hints)
  549. {
  550. if (!hints)
  551. return;
  552. fprintf (f, "inline hints:");
  553. if (hints & INLINE_HINT_indirect_call)
  554. {
  555. hints &= ~INLINE_HINT_indirect_call;
  556. fprintf (f, " indirect_call");
  557. }
  558. if (hints & INLINE_HINT_loop_iterations)
  559. {
  560. hints &= ~INLINE_HINT_loop_iterations;
  561. fprintf (f, " loop_iterations");
  562. }
  563. if (hints & INLINE_HINT_loop_stride)
  564. {
  565. hints &= ~INLINE_HINT_loop_stride;
  566. fprintf (f, " loop_stride");
  567. }
  568. if (hints & INLINE_HINT_same_scc)
  569. {
  570. hints &= ~INLINE_HINT_same_scc;
  571. fprintf (f, " same_scc");
  572. }
  573. if (hints & INLINE_HINT_in_scc)
  574. {
  575. hints &= ~INLINE_HINT_in_scc;
  576. fprintf (f, " in_scc");
  577. }
  578. if (hints & INLINE_HINT_cross_module)
  579. {
  580. hints &= ~INLINE_HINT_cross_module;
  581. fprintf (f, " cross_module");
  582. }
  583. if (hints & INLINE_HINT_declared_inline)
  584. {
  585. hints &= ~INLINE_HINT_declared_inline;
  586. fprintf (f, " declared_inline");
  587. }
  588. if (hints & INLINE_HINT_array_index)
  589. {
  590. hints &= ~INLINE_HINT_array_index;
  591. fprintf (f, " array_index");
  592. }
  593. if (hints & INLINE_HINT_known_hot)
  594. {
  595. hints &= ~INLINE_HINT_known_hot;
  596. fprintf (f, " known_hot");
  597. }
  598. gcc_assert (!hints);
  599. }
  600. /* Record SIZE and TIME under condition PRED into the inline summary. */
  601. static void
  602. account_size_time (struct inline_summary *summary, int size, int time,
  603. struct predicate *pred)
  604. {
  605. size_time_entry *e;
  606. bool found = false;
  607. int i;
  608. if (false_predicate_p (pred))
  609. return;
  610. /* We need to create initial empty unconitional clause, but otherwie
  611. we don't need to account empty times and sizes. */
  612. if (!size && !time && summary->entry)
  613. return;
  614. /* Watch overflow that might result from insane profiles. */
  615. if (time > MAX_TIME * INLINE_TIME_SCALE)
  616. time = MAX_TIME * INLINE_TIME_SCALE;
  617. gcc_assert (time >= 0);
  618. for (i = 0; vec_safe_iterate (summary->entry, i, &e); i++)
  619. if (predicates_equal_p (&e->predicate, pred))
  620. {
  621. found = true;
  622. break;
  623. }
  624. if (i == 256)
  625. {
  626. i = 0;
  627. found = true;
  628. e = &(*summary->entry)[0];
  629. gcc_assert (!e->predicate.clause[0]);
  630. if (dump_file && (dump_flags & TDF_DETAILS))
  631. fprintf (dump_file,
  632. "\t\tReached limit on number of entries, "
  633. "ignoring the predicate.");
  634. }
  635. if (dump_file && (dump_flags & TDF_DETAILS) && (time || size))
  636. {
  637. fprintf (dump_file,
  638. "\t\tAccounting size:%3.2f, time:%3.2f on %spredicate:",
  639. ((double) size) / INLINE_SIZE_SCALE,
  640. ((double) time) / INLINE_TIME_SCALE, found ? "" : "new ");
  641. dump_predicate (dump_file, summary->conds, pred);
  642. }
  643. if (!found)
  644. {
  645. struct size_time_entry new_entry;
  646. new_entry.size = size;
  647. new_entry.time = time;
  648. new_entry.predicate = *pred;
  649. vec_safe_push (summary->entry, new_entry);
  650. }
  651. else
  652. {
  653. e->size += size;
  654. e->time += time;
  655. if (e->time > MAX_TIME * INLINE_TIME_SCALE)
  656. e->time = MAX_TIME * INLINE_TIME_SCALE;
  657. }
  658. }
  659. /* We proved E to be unreachable, redirect it to __bultin_unreachable. */
  660. static struct cgraph_edge *
  661. redirect_to_unreachable (struct cgraph_edge *e)
  662. {
  663. struct cgraph_node *callee = !e->inline_failed ? e->callee : NULL;
  664. struct cgraph_node *target = cgraph_node::get_create
  665. (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
  666. if (e->speculative)
  667. e = e->resolve_speculation (target->decl);
  668. else if (!e->callee)
  669. e->make_direct (target);
  670. else
  671. e->redirect_callee (target);
  672. struct inline_edge_summary *es = inline_edge_summary (e);
  673. e->inline_failed = CIF_UNREACHABLE;
  674. e->frequency = 0;
  675. e->count = 0;
  676. es->call_stmt_size = 0;
  677. es->call_stmt_time = 0;
  678. if (callee)
  679. callee->remove_symbol_and_inline_clones ();
  680. return e;
  681. }
  682. /* Set predicate for edge E. */
  683. static void
  684. edge_set_predicate (struct cgraph_edge *e, struct predicate *predicate)
  685. {
  686. /* If the edge is determined to be never executed, redirect it
  687. to BUILTIN_UNREACHABLE to save inliner from inlining into it. */
  688. if (predicate && false_predicate_p (predicate)
  689. /* When handling speculative edges, we need to do the redirection
  690. just once. Do it always on the direct edge, so we do not
  691. attempt to resolve speculation while duplicating the edge. */
  692. && (!e->speculative || e->callee))
  693. e = redirect_to_unreachable (e);
  694. struct inline_edge_summary *es = inline_edge_summary (e);
  695. if (predicate && !true_predicate_p (predicate))
  696. {
  697. if (!es->predicate)
  698. es->predicate = (struct predicate *) pool_alloc (edge_predicate_pool);
  699. *es->predicate = *predicate;
  700. }
  701. else
  702. {
  703. if (es->predicate)
  704. pool_free (edge_predicate_pool, es->predicate);
  705. es->predicate = NULL;
  706. }
  707. }
  708. /* Set predicate for hint *P. */
  709. static void
  710. set_hint_predicate (struct predicate **p, struct predicate new_predicate)
  711. {
  712. if (false_predicate_p (&new_predicate) || true_predicate_p (&new_predicate))
  713. {
  714. if (*p)
  715. pool_free (edge_predicate_pool, *p);
  716. *p = NULL;
  717. }
  718. else
  719. {
  720. if (!*p)
  721. *p = (struct predicate *) pool_alloc (edge_predicate_pool);
  722. **p = new_predicate;
  723. }
  724. }
  725. /* KNOWN_VALS is partial mapping of parameters of NODE to constant values.
  726. KNOWN_AGGS is a vector of aggreggate jump functions for each parameter.
  727. Return clause of possible truths. When INLINE_P is true, assume that we are
  728. inlining.
  729. ERROR_MARK means compile time invariant. */
  730. static clause_t
  731. evaluate_conditions_for_known_args (struct cgraph_node *node,
  732. bool inline_p,
  733. vec<tree> known_vals,
  734. vec<ipa_agg_jump_function_p>
  735. known_aggs)
  736. {
  737. clause_t clause = inline_p ? 0 : 1 << predicate_not_inlined_condition;
  738. struct inline_summary *info = inline_summaries->get (node);
  739. int i;
  740. struct condition *c;
  741. for (i = 0; vec_safe_iterate (info->conds, i, &c); i++)
  742. {
  743. tree val;
  744. tree res;
  745. /* We allow call stmt to have fewer arguments than the callee function
  746. (especially for K&R style programs). So bound check here (we assume
  747. known_aggs vector, if non-NULL, has the same length as
  748. known_vals). */
  749. gcc_checking_assert (!known_aggs.exists ()
  750. || (known_vals.length () == known_aggs.length ()));
  751. if (c->operand_num >= (int) known_vals.length ())
  752. {
  753. clause |= 1 << (i + predicate_first_dynamic_condition);
  754. continue;
  755. }
  756. if (c->agg_contents)
  757. {
  758. struct ipa_agg_jump_function *agg;
  759. if (c->code == CHANGED
  760. && !c->by_ref
  761. && (known_vals[c->operand_num] == error_mark_node))
  762. continue;
  763. if (known_aggs.exists ())
  764. {
  765. agg = known_aggs[c->operand_num];
  766. val = ipa_find_agg_cst_for_param (agg, c->offset, c->by_ref);
  767. }
  768. else
  769. val = NULL_TREE;
  770. }
  771. else
  772. {
  773. val = known_vals[c->operand_num];
  774. if (val == error_mark_node && c->code != CHANGED)
  775. val = NULL_TREE;
  776. }
  777. if (!val)
  778. {
  779. clause |= 1 << (i + predicate_first_dynamic_condition);
  780. continue;
  781. }
  782. if (c->code == IS_NOT_CONSTANT || c->code == CHANGED)
  783. continue;
  784. if (operand_equal_p (TYPE_SIZE (TREE_TYPE (c->val)),
  785. TYPE_SIZE (TREE_TYPE (val)), 0))
  786. {
  787. val = fold_unary (VIEW_CONVERT_EXPR, TREE_TYPE (c->val), val);
  788. res = val
  789. ? fold_binary_to_constant (c->code, boolean_type_node, val, c->val)
  790. : NULL;
  791. if (res && integer_zerop (res))
  792. continue;
  793. }
  794. clause |= 1 << (i + predicate_first_dynamic_condition);
  795. }
  796. return clause;
  797. }
  798. /* Work out what conditions might be true at invocation of E. */
  799. static void
  800. evaluate_properties_for_edge (struct cgraph_edge *e, bool inline_p,
  801. clause_t *clause_ptr,
  802. vec<tree> *known_vals_ptr,
  803. vec<ipa_polymorphic_call_context>
  804. *known_contexts_ptr,
  805. vec<ipa_agg_jump_function_p> *known_aggs_ptr)
  806. {
  807. struct cgraph_node *callee = e->callee->ultimate_alias_target ();
  808. struct inline_summary *info = inline_summaries->get (callee);
  809. vec<tree> known_vals = vNULL;
  810. vec<ipa_agg_jump_function_p> known_aggs = vNULL;
  811. if (clause_ptr)
  812. *clause_ptr = inline_p ? 0 : 1 << predicate_not_inlined_condition;
  813. if (known_vals_ptr)
  814. known_vals_ptr->create (0);
  815. if (known_contexts_ptr)
  816. known_contexts_ptr->create (0);
  817. if (ipa_node_params_sum
  818. && !e->call_stmt_cannot_inline_p
  819. && ((clause_ptr && info->conds) || known_vals_ptr || known_contexts_ptr))
  820. {
  821. struct ipa_node_params *parms_info;
  822. struct ipa_edge_args *args = IPA_EDGE_REF (e);
  823. struct inline_edge_summary *es = inline_edge_summary (e);
  824. int i, count = ipa_get_cs_argument_count (args);
  825. if (e->caller->global.inlined_to)
  826. parms_info = IPA_NODE_REF (e->caller->global.inlined_to);
  827. else
  828. parms_info = IPA_NODE_REF (e->caller);
  829. if (count && (info->conds || known_vals_ptr))
  830. known_vals.safe_grow_cleared (count);
  831. if (count && (info->conds || known_aggs_ptr))
  832. known_aggs.safe_grow_cleared (count);
  833. if (count && known_contexts_ptr)
  834. known_contexts_ptr->safe_grow_cleared (count);
  835. for (i = 0; i < count; i++)
  836. {
  837. struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
  838. tree cst = ipa_value_from_jfunc (parms_info, jf);
  839. if (!cst && e->call_stmt
  840. && i < (int)gimple_call_num_args (e->call_stmt))
  841. {
  842. cst = gimple_call_arg (e->call_stmt, i);
  843. if (!is_gimple_min_invariant (cst))
  844. cst = NULL;
  845. }
  846. if (cst)
  847. {
  848. gcc_checking_assert (TREE_CODE (cst) != TREE_BINFO);
  849. if (known_vals.exists ())
  850. known_vals[i] = cst;
  851. }
  852. else if (inline_p && !es->param[i].change_prob)
  853. known_vals[i] = error_mark_node;
  854. if (known_contexts_ptr)
  855. (*known_contexts_ptr)[i] = ipa_context_from_jfunc (parms_info, e,
  856. i, jf);
  857. /* TODO: When IPA-CP starts propagating and merging aggregate jump
  858. functions, use its knowledge of the caller too, just like the
  859. scalar case above. */
  860. known_aggs[i] = &jf->agg;
  861. }
  862. }
  863. else if (e->call_stmt && !e->call_stmt_cannot_inline_p
  864. && ((clause_ptr && info->conds) || known_vals_ptr))
  865. {
  866. int i, count = (int)gimple_call_num_args (e->call_stmt);
  867. if (count && (info->conds || known_vals_ptr))
  868. known_vals.safe_grow_cleared (count);
  869. for (i = 0; i < count; i++)
  870. {
  871. tree cst = gimple_call_arg (e->call_stmt, i);
  872. if (!is_gimple_min_invariant (cst))
  873. cst = NULL;
  874. if (cst)
  875. known_vals[i] = cst;
  876. }
  877. }
  878. if (clause_ptr)
  879. *clause_ptr = evaluate_conditions_for_known_args (callee, inline_p,
  880. known_vals, known_aggs);
  881. if (known_vals_ptr)
  882. *known_vals_ptr = known_vals;
  883. else
  884. known_vals.release ();
  885. if (known_aggs_ptr)
  886. *known_aggs_ptr = known_aggs;
  887. else
  888. known_aggs.release ();
  889. }
  890. /* Allocate the inline summary vector or resize it to cover all cgraph nodes. */
  891. static void
  892. inline_summary_alloc (void)
  893. {
  894. if (!edge_removal_hook_holder)
  895. edge_removal_hook_holder =
  896. symtab->add_edge_removal_hook (&inline_edge_removal_hook, NULL);
  897. if (!edge_duplication_hook_holder)
  898. edge_duplication_hook_holder =
  899. symtab->add_edge_duplication_hook (&inline_edge_duplication_hook, NULL);
  900. if (!inline_summaries)
  901. inline_summaries = (inline_summary_t*) inline_summary_t::create_ggc (symtab);
  902. if (inline_edge_summary_vec.length () <= (unsigned) symtab->edges_max_uid)
  903. inline_edge_summary_vec.safe_grow_cleared (symtab->edges_max_uid + 1);
  904. if (!edge_predicate_pool)
  905. edge_predicate_pool = create_alloc_pool ("edge predicates",
  906. sizeof (struct predicate), 10);
  907. }
  908. /* We are called multiple time for given function; clear
  909. data from previous run so they are not cumulated. */
  910. static void
  911. reset_inline_edge_summary (struct cgraph_edge *e)
  912. {
  913. if (e->uid < (int) inline_edge_summary_vec.length ())
  914. {
  915. struct inline_edge_summary *es = inline_edge_summary (e);
  916. es->call_stmt_size = es->call_stmt_time = 0;
  917. if (es->predicate)
  918. pool_free (edge_predicate_pool, es->predicate);
  919. es->predicate = NULL;
  920. es->param.release ();
  921. }
  922. }
  923. /* We are called multiple time for given function; clear
  924. data from previous run so they are not cumulated. */
  925. static void
  926. reset_inline_summary (struct cgraph_node *node,
  927. inline_summary *info)
  928. {
  929. struct cgraph_edge *e;
  930. info->self_size = info->self_time = 0;
  931. info->estimated_stack_size = 0;
  932. info->estimated_self_stack_size = 0;
  933. info->stack_frame_offset = 0;
  934. info->size = 0;
  935. info->time = 0;
  936. info->growth = 0;
  937. info->scc_no = 0;
  938. if (info->loop_iterations)
  939. {
  940. pool_free (edge_predicate_pool, info->loop_iterations);
  941. info->loop_iterations = NULL;
  942. }
  943. if (info->loop_stride)
  944. {
  945. pool_free (edge_predicate_pool, info->loop_stride);
  946. info->loop_stride = NULL;
  947. }
  948. if (info->array_index)
  949. {
  950. pool_free (edge_predicate_pool, info->array_index);
  951. info->array_index = NULL;
  952. }
  953. vec_free (info->conds);
  954. vec_free (info->entry);
  955. for (e = node->callees; e; e = e->next_callee)
  956. reset_inline_edge_summary (e);
  957. for (e = node->indirect_calls; e; e = e->next_callee)
  958. reset_inline_edge_summary (e);
  959. }
  960. /* Hook that is called by cgraph.c when a node is removed. */
  961. void
  962. inline_summary_t::remove (cgraph_node *node, inline_summary *info)
  963. {
  964. reset_inline_summary (node, info);
  965. }
  966. /* Remap predicate P of former function to be predicate of duplicated function.
  967. POSSIBLE_TRUTHS is clause of possible truths in the duplicated node,
  968. INFO is inline summary of the duplicated node. */
  969. static struct predicate
  970. remap_predicate_after_duplication (struct predicate *p,
  971. clause_t possible_truths,
  972. struct inline_summary *info)
  973. {
  974. struct predicate new_predicate = true_predicate ();
  975. int j;
  976. for (j = 0; p->clause[j]; j++)
  977. if (!(possible_truths & p->clause[j]))
  978. {
  979. new_predicate = false_predicate ();
  980. break;
  981. }
  982. else
  983. add_clause (info->conds, &new_predicate,
  984. possible_truths & p->clause[j]);
  985. return new_predicate;
  986. }
  987. /* Same as remap_predicate_after_duplication but handle hint predicate *P.
  988. Additionally care about allocating new memory slot for updated predicate
  989. and set it to NULL when it becomes true or false (and thus uninteresting).
  990. */
  991. static void
  992. remap_hint_predicate_after_duplication (struct predicate **p,
  993. clause_t possible_truths,
  994. struct inline_summary *info)
  995. {
  996. struct predicate new_predicate;
  997. if (!*p)
  998. return;
  999. new_predicate = remap_predicate_after_duplication (*p,
  1000. possible_truths, info);
  1001. /* We do not want to free previous predicate; it is used by node origin. */
  1002. *p = NULL;
  1003. set_hint_predicate (p, new_predicate);
  1004. }
  1005. /* Hook that is called by cgraph.c when a node is duplicated. */
  1006. void
  1007. inline_summary_t::duplicate (cgraph_node *src,
  1008. cgraph_node *dst,
  1009. inline_summary *,
  1010. inline_summary *info)
  1011. {
  1012. inline_summary_alloc ();
  1013. memcpy (info, inline_summaries->get (src), sizeof (inline_summary));
  1014. /* TODO: as an optimization, we may avoid copying conditions
  1015. that are known to be false or true. */
  1016. info->conds = vec_safe_copy (info->conds);
  1017. /* When there are any replacements in the function body, see if we can figure
  1018. out that something was optimized out. */
  1019. if (ipa_node_params_sum && dst->clone.tree_map)
  1020. {
  1021. vec<size_time_entry, va_gc> *entry = info->entry;
  1022. /* Use SRC parm info since it may not be copied yet. */
  1023. struct ipa_node_params *parms_info = IPA_NODE_REF (src);
  1024. vec<tree> known_vals = vNULL;
  1025. int count = ipa_get_param_count (parms_info);
  1026. int i, j;
  1027. clause_t possible_truths;
  1028. struct predicate true_pred = true_predicate ();
  1029. size_time_entry *e;
  1030. int optimized_out_size = 0;
  1031. bool inlined_to_p = false;
  1032. struct cgraph_edge *edge, *next;
  1033. info->entry = 0;
  1034. known_vals.safe_grow_cleared (count);
  1035. for (i = 0; i < count; i++)
  1036. {
  1037. struct ipa_replace_map *r;
  1038. for (j = 0; vec_safe_iterate (dst->clone.tree_map, j, &r); j++)
  1039. {
  1040. if (((!r->old_tree && r->parm_num == i)
  1041. || (r->old_tree && r->old_tree == ipa_get_param (parms_info, i)))
  1042. && r->replace_p && !r->ref_p)
  1043. {
  1044. known_vals[i] = r->new_tree;
  1045. break;
  1046. }
  1047. }
  1048. }
  1049. possible_truths = evaluate_conditions_for_known_args (dst, false,
  1050. known_vals,
  1051. vNULL);
  1052. known_vals.release ();
  1053. account_size_time (info, 0, 0, &true_pred);
  1054. /* Remap size_time vectors.
  1055. Simplify the predicate by prunning out alternatives that are known
  1056. to be false.
  1057. TODO: as on optimization, we can also eliminate conditions known
  1058. to be true. */
  1059. for (i = 0; vec_safe_iterate (entry, i, &e); i++)
  1060. {
  1061. struct predicate new_predicate;
  1062. new_predicate = remap_predicate_after_duplication (&e->predicate,
  1063. possible_truths,
  1064. info);
  1065. if (false_predicate_p (&new_predicate))
  1066. optimized_out_size += e->size;
  1067. else
  1068. account_size_time (info, e->size, e->time, &new_predicate);
  1069. }
  1070. /* Remap edge predicates with the same simplification as above.
  1071. Also copy constantness arrays. */
  1072. for (edge = dst->callees; edge; edge = next)
  1073. {
  1074. struct predicate new_predicate;
  1075. struct inline_edge_summary *es = inline_edge_summary (edge);
  1076. next = edge->next_callee;
  1077. if (!edge->inline_failed)
  1078. inlined_to_p = true;
  1079. if (!es->predicate)
  1080. continue;
  1081. new_predicate = remap_predicate_after_duplication (es->predicate,
  1082. possible_truths,
  1083. info);
  1084. if (false_predicate_p (&new_predicate)
  1085. && !false_predicate_p (es->predicate))
  1086. optimized_out_size += es->call_stmt_size * INLINE_SIZE_SCALE;
  1087. edge_set_predicate (edge, &new_predicate);
  1088. }
  1089. /* Remap indirect edge predicates with the same simplificaiton as above.
  1090. Also copy constantness arrays. */
  1091. for (edge = dst->indirect_calls; edge; edge = next)
  1092. {
  1093. struct predicate new_predicate;
  1094. struct inline_edge_summary *es = inline_edge_summary (edge);
  1095. next = edge->next_callee;
  1096. gcc_checking_assert (edge->inline_failed);
  1097. if (!es->predicate)
  1098. continue;
  1099. new_predicate = remap_predicate_after_duplication (es->predicate,
  1100. possible_truths,
  1101. info);
  1102. if (false_predicate_p (&new_predicate)
  1103. && !false_predicate_p (es->predicate))
  1104. optimized_out_size += es->call_stmt_size * INLINE_SIZE_SCALE;
  1105. edge_set_predicate (edge, &new_predicate);
  1106. }
  1107. remap_hint_predicate_after_duplication (&info->loop_iterations,
  1108. possible_truths, info);
  1109. remap_hint_predicate_after_duplication (&info->loop_stride,
  1110. possible_truths, info);
  1111. remap_hint_predicate_after_duplication (&info->array_index,
  1112. possible_truths, info);
  1113. /* If inliner or someone after inliner will ever start producing
  1114. non-trivial clones, we will get trouble with lack of information
  1115. about updating self sizes, because size vectors already contains
  1116. sizes of the calees. */
  1117. gcc_assert (!inlined_to_p || !optimized_out_size);
  1118. }
  1119. else
  1120. {
  1121. info->entry = vec_safe_copy (info->entry);
  1122. if (info->loop_iterations)
  1123. {
  1124. predicate p = *info->loop_iterations;
  1125. info->loop_iterations = NULL;
  1126. set_hint_predicate (&info->loop_iterations, p);
  1127. }
  1128. if (info->loop_stride)
  1129. {
  1130. predicate p = *info->loop_stride;
  1131. info->loop_stride = NULL;
  1132. set_hint_predicate (&info->loop_stride, p);
  1133. }
  1134. if (info->array_index)
  1135. {
  1136. predicate p = *info->array_index;
  1137. info->array_index = NULL;
  1138. set_hint_predicate (&info->array_index, p);
  1139. }
  1140. }
  1141. if (!dst->global.inlined_to)
  1142. inline_update_overall_summary (dst);
  1143. }
  1144. /* Hook that is called by cgraph.c when a node is duplicated. */
  1145. static void
  1146. inline_edge_duplication_hook (struct cgraph_edge *src,
  1147. struct cgraph_edge *dst,
  1148. ATTRIBUTE_UNUSED void *data)
  1149. {
  1150. struct inline_edge_summary *info;
  1151. struct inline_edge_summary *srcinfo;
  1152. inline_summary_alloc ();
  1153. info = inline_edge_summary (dst);
  1154. srcinfo = inline_edge_summary (src);
  1155. memcpy (info, srcinfo, sizeof (struct inline_edge_summary));
  1156. info->predicate = NULL;
  1157. edge_set_predicate (dst, srcinfo->predicate);
  1158. info->param = srcinfo->param.copy ();
  1159. if (!dst->indirect_unknown_callee && src->indirect_unknown_callee)
  1160. {
  1161. info->call_stmt_size -= (eni_size_weights.indirect_call_cost
  1162. - eni_size_weights.call_cost);
  1163. info->call_stmt_time -= (eni_time_weights.indirect_call_cost
  1164. - eni_time_weights.call_cost);
  1165. }
  1166. }
  1167. /* Keep edge cache consistent across edge removal. */
  1168. static void
  1169. inline_edge_removal_hook (struct cgraph_edge *edge,
  1170. void *data ATTRIBUTE_UNUSED)
  1171. {
  1172. if (edge_growth_cache.exists ())
  1173. reset_edge_growth_cache (edge);
  1174. reset_inline_edge_summary (edge);
  1175. }
  1176. /* Initialize growth caches. */
  1177. void
  1178. initialize_growth_caches (void)
  1179. {
  1180. if (symtab->edges_max_uid)
  1181. edge_growth_cache.safe_grow_cleared (symtab->edges_max_uid);
  1182. }
  1183. /* Free growth caches. */
  1184. void
  1185. free_growth_caches (void)
  1186. {
  1187. edge_growth_cache.release ();
  1188. }
  1189. /* Dump edge summaries associated to NODE and recursively to all clones.
  1190. Indent by INDENT. */
  1191. static void
  1192. dump_inline_edge_summary (FILE *f, int indent, struct cgraph_node *node,
  1193. struct inline_summary *info)
  1194. {
  1195. struct cgraph_edge *edge;
  1196. for (edge = node->callees; edge; edge = edge->next_callee)
  1197. {
  1198. struct inline_edge_summary *es = inline_edge_summary (edge);
  1199. struct cgraph_node *callee = edge->callee->ultimate_alias_target ();
  1200. int i;
  1201. fprintf (f,
  1202. "%*s%s/%i %s\n%*s loop depth:%2i freq:%4i size:%2i"
  1203. " time: %2i callee size:%2i stack:%2i",
  1204. indent, "", callee->name (), callee->order,
  1205. !edge->inline_failed
  1206. ? "inlined" : cgraph_inline_failed_string (edge-> inline_failed),
  1207. indent, "", es->loop_depth, edge->frequency,
  1208. es->call_stmt_size, es->call_stmt_time,
  1209. (int) inline_summaries->get (callee)->size / INLINE_SIZE_SCALE,
  1210. (int) inline_summaries->get (callee)->estimated_stack_size);
  1211. if (es->predicate)
  1212. {
  1213. fprintf (f, " predicate: ");
  1214. dump_predicate (f, info->conds, es->predicate);
  1215. }
  1216. else
  1217. fprintf (f, "\n");
  1218. if (es->param.exists ())
  1219. for (i = 0; i < (int) es->param.length (); i++)
  1220. {
  1221. int prob = es->param[i].change_prob;
  1222. if (!prob)
  1223. fprintf (f, "%*s op%i is compile time invariant\n",
  1224. indent + 2, "", i);
  1225. else if (prob != REG_BR_PROB_BASE)
  1226. fprintf (f, "%*s op%i change %f%% of time\n", indent + 2, "", i,
  1227. prob * 100.0 / REG_BR_PROB_BASE);
  1228. }
  1229. if (!edge->inline_failed)
  1230. {
  1231. fprintf (f, "%*sStack frame offset %i, callee self size %i,"
  1232. " callee size %i\n",
  1233. indent + 2, "",
  1234. (int) inline_summaries->get (callee)->stack_frame_offset,
  1235. (int) inline_summaries->get (callee)->estimated_self_stack_size,
  1236. (int) inline_summaries->get (callee)->estimated_stack_size);
  1237. dump_inline_edge_summary (f, indent + 2, callee, info);
  1238. }
  1239. }
  1240. for (edge = node->indirect_calls; edge; edge = edge->next_callee)
  1241. {
  1242. struct inline_edge_summary *es = inline_edge_summary (edge);
  1243. fprintf (f, "%*sindirect call loop depth:%2i freq:%4i size:%2i"
  1244. " time: %2i",
  1245. indent, "",
  1246. es->loop_depth,
  1247. edge->frequency, es->call_stmt_size, es->call_stmt_time);
  1248. if (es->predicate)
  1249. {
  1250. fprintf (f, "predicate: ");
  1251. dump_predicate (f, info->conds, es->predicate);
  1252. }
  1253. else
  1254. fprintf (f, "\n");
  1255. }
  1256. }
  1257. void
  1258. dump_inline_summary (FILE *f, struct cgraph_node *node)
  1259. {
  1260. if (node->definition)
  1261. {
  1262. struct inline_summary *s = inline_summaries->get (node);
  1263. size_time_entry *e;
  1264. int i;
  1265. fprintf (f, "Inline summary for %s/%i", node->name (),
  1266. node->order);
  1267. if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
  1268. fprintf (f, " always_inline");
  1269. if (s->inlinable)
  1270. fprintf (f, " inlinable");
  1271. if (s->contains_cilk_spawn)
  1272. fprintf (f, " contains_cilk_spawn");
  1273. fprintf (f, "\n self time: %i\n", s->self_time);
  1274. fprintf (f, " global time: %i\n", s->time);
  1275. fprintf (f, " self size: %i\n", s->self_size);
  1276. fprintf (f, " global size: %i\n", s->size);
  1277. fprintf (f, " min size: %i\n", s->min_size);
  1278. fprintf (f, " self stack: %i\n",
  1279. (int) s->estimated_self_stack_size);
  1280. fprintf (f, " global stack: %i\n", (int) s->estimated_stack_size);
  1281. if (s->growth)
  1282. fprintf (f, " estimated growth:%i\n", (int) s->growth);
  1283. if (s->scc_no)
  1284. fprintf (f, " In SCC: %i\n", (int) s->scc_no);
  1285. for (i = 0; vec_safe_iterate (s->entry, i, &e); i++)
  1286. {
  1287. fprintf (f, " size:%f, time:%f, predicate:",
  1288. (double) e->size / INLINE_SIZE_SCALE,
  1289. (double) e->time / INLINE_TIME_SCALE);
  1290. dump_predicate (f, s->conds, &e->predicate);
  1291. }
  1292. if (s->loop_iterations)
  1293. {
  1294. fprintf (f, " loop iterations:");
  1295. dump_predicate (f, s->conds, s->loop_iterations);
  1296. }
  1297. if (s->loop_stride)
  1298. {
  1299. fprintf (f, " loop stride:");
  1300. dump_predicate (f, s->conds, s->loop_stride);
  1301. }
  1302. if (s->array_index)
  1303. {
  1304. fprintf (f, " array index:");
  1305. dump_predicate (f, s->conds, s->array_index);
  1306. }
  1307. fprintf (f, " calls:\n");
  1308. dump_inline_edge_summary (f, 4, node, s);
  1309. fprintf (f, "\n");
  1310. }
  1311. }
  1312. DEBUG_FUNCTION void
  1313. debug_inline_summary (struct cgraph_node *node)
  1314. {
  1315. dump_inline_summary (stderr, node);
  1316. }
  1317. void
  1318. dump_inline_summaries (FILE *f)
  1319. {
  1320. struct cgraph_node *node;
  1321. FOR_EACH_DEFINED_FUNCTION (node)
  1322. if (!node->global.inlined_to)
  1323. dump_inline_summary (f, node);
  1324. }
  1325. /* Give initial reasons why inlining would fail on EDGE. This gets either
  1326. nullified or usually overwritten by more precise reasons later. */
  1327. void
  1328. initialize_inline_failed (struct cgraph_edge *e)
  1329. {
  1330. struct cgraph_node *callee = e->callee;
  1331. if (e->indirect_unknown_callee)
  1332. e->inline_failed = CIF_INDIRECT_UNKNOWN_CALL;
  1333. else if (!callee->definition)
  1334. e->inline_failed = CIF_BODY_NOT_AVAILABLE;
  1335. else if (callee->local.redefined_extern_inline)
  1336. e->inline_failed = CIF_REDEFINED_EXTERN_INLINE;
  1337. else if (e->call_stmt_cannot_inline_p)
  1338. e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
  1339. else if (cfun && fn_contains_cilk_spawn_p (cfun))
  1340. /* We can't inline if the function is spawing a function. */
  1341. e->inline_failed = CIF_FUNCTION_NOT_INLINABLE;
  1342. else
  1343. e->inline_failed = CIF_FUNCTION_NOT_CONSIDERED;
  1344. }
  1345. /* Callback of walk_aliased_vdefs. Flags that it has been invoked to the
  1346. boolean variable pointed to by DATA. */
  1347. static bool
  1348. mark_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED,
  1349. void *data)
  1350. {
  1351. bool *b = (bool *) data;
  1352. *b = true;
  1353. return true;
  1354. }
  1355. /* If OP refers to value of function parameter, return the corresponding
  1356. parameter. */
  1357. static tree
  1358. unmodified_parm_1 (gimple stmt, tree op)
  1359. {
  1360. /* SSA_NAME referring to parm default def? */
  1361. if (TREE_CODE (op) == SSA_NAME
  1362. && SSA_NAME_IS_DEFAULT_DEF (op)
  1363. && TREE_CODE (SSA_NAME_VAR (op)) == PARM_DECL)
  1364. return SSA_NAME_VAR (op);
  1365. /* Non-SSA parm reference? */
  1366. if (TREE_CODE (op) == PARM_DECL)
  1367. {
  1368. bool modified = false;
  1369. ao_ref refd;
  1370. ao_ref_init (&refd, op);
  1371. walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified, &modified,
  1372. NULL);
  1373. if (!modified)
  1374. return op;
  1375. }
  1376. return NULL_TREE;
  1377. }
  1378. /* If OP refers to value of function parameter, return the corresponding
  1379. parameter. Also traverse chains of SSA register assignments. */
  1380. static tree
  1381. unmodified_parm (gimple stmt, tree op)
  1382. {
  1383. tree res = unmodified_parm_1 (stmt, op);
  1384. if (res)
  1385. return res;
  1386. if (TREE_CODE (op) == SSA_NAME
  1387. && !SSA_NAME_IS_DEFAULT_DEF (op)
  1388. && gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
  1389. return unmodified_parm (SSA_NAME_DEF_STMT (op),
  1390. gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op)));
  1391. return NULL_TREE;
  1392. }
  1393. /* If OP refers to a value of a function parameter or value loaded from an
  1394. aggregate passed to a parameter (either by value or reference), return TRUE
  1395. and store the number of the parameter to *INDEX_P and information whether
  1396. and how it has been loaded from an aggregate into *AGGPOS. INFO describes
  1397. the function parameters, STMT is the statement in which OP is used or
  1398. loaded. */
  1399. static bool
  1400. unmodified_parm_or_parm_agg_item (struct ipa_node_params *info,
  1401. gimple stmt, tree op, int *index_p,
  1402. struct agg_position_info *aggpos)
  1403. {
  1404. tree res = unmodified_parm_1 (stmt, op);
  1405. gcc_checking_assert (aggpos);
  1406. if (res)
  1407. {
  1408. *index_p = ipa_get_param_decl_index (info, res);
  1409. if (*index_p < 0)
  1410. return false;
  1411. aggpos->agg_contents = false;
  1412. aggpos->by_ref = false;
  1413. return true;
  1414. }
  1415. if (TREE_CODE (op) == SSA_NAME)
  1416. {
  1417. if (SSA_NAME_IS_DEFAULT_DEF (op)
  1418. || !gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
  1419. return false;
  1420. stmt = SSA_NAME_DEF_STMT (op);
  1421. op = gimple_assign_rhs1 (stmt);
  1422. if (!REFERENCE_CLASS_P (op))
  1423. return unmodified_parm_or_parm_agg_item (info, stmt, op, index_p,
  1424. aggpos);
  1425. }
  1426. aggpos->agg_contents = true;
  1427. return ipa_load_from_parm_agg (info, stmt, op, index_p, &aggpos->offset,
  1428. &aggpos->by_ref);
  1429. }
  1430. /* See if statement might disappear after inlining.
  1431. 0 - means not eliminated
  1432. 1 - half of statements goes away
  1433. 2 - for sure it is eliminated.
  1434. We are not terribly sophisticated, basically looking for simple abstraction
  1435. penalty wrappers. */
  1436. static int
  1437. eliminated_by_inlining_prob (gimple stmt)
  1438. {
  1439. enum gimple_code code = gimple_code (stmt);
  1440. enum tree_code rhs_code;
  1441. if (!optimize)
  1442. return 0;
  1443. switch (code)
  1444. {
  1445. case GIMPLE_RETURN:
  1446. return 2;
  1447. case GIMPLE_ASSIGN:
  1448. if (gimple_num_ops (stmt) != 2)
  1449. return 0;
  1450. rhs_code = gimple_assign_rhs_code (stmt);
  1451. /* Casts of parameters, loads from parameters passed by reference
  1452. and stores to return value or parameters are often free after
  1453. inlining dua to SRA and further combining.
  1454. Assume that half of statements goes away. */
  1455. if (CONVERT_EXPR_CODE_P (rhs_code)
  1456. || rhs_code == VIEW_CONVERT_EXPR
  1457. || rhs_code == ADDR_EXPR
  1458. || gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
  1459. {
  1460. tree rhs = gimple_assign_rhs1 (stmt);
  1461. tree lhs = gimple_assign_lhs (stmt);
  1462. tree inner_rhs = get_base_address (rhs);
  1463. tree inner_lhs = get_base_address (lhs);
  1464. bool rhs_free = false;
  1465. bool lhs_free = false;
  1466. if (!inner_rhs)
  1467. inner_rhs = rhs;
  1468. if (!inner_lhs)
  1469. inner_lhs = lhs;
  1470. /* Reads of parameter are expected to be free. */
  1471. if (unmodified_parm (stmt, inner_rhs))
  1472. rhs_free = true;
  1473. /* Match expressions of form &this->field. Those will most likely
  1474. combine with something upstream after inlining. */
  1475. else if (TREE_CODE (inner_rhs) == ADDR_EXPR)
  1476. {
  1477. tree op = get_base_address (TREE_OPERAND (inner_rhs, 0));
  1478. if (TREE_CODE (op) == PARM_DECL)
  1479. rhs_free = true;
  1480. else if (TREE_CODE (op) == MEM_REF
  1481. && unmodified_parm (stmt, TREE_OPERAND (op, 0)))
  1482. rhs_free = true;
  1483. }
  1484. /* When parameter is not SSA register because its address is taken
  1485. and it is just copied into one, the statement will be completely
  1486. free after inlining (we will copy propagate backward). */
  1487. if (rhs_free && is_gimple_reg (lhs))
  1488. return 2;
  1489. /* Reads of parameters passed by reference
  1490. expected to be free (i.e. optimized out after inlining). */
  1491. if (TREE_CODE (inner_rhs) == MEM_REF
  1492. && unmodified_parm (stmt, TREE_OPERAND (inner_rhs, 0)))
  1493. rhs_free = true;
  1494. /* Copying parameter passed by reference into gimple register is
  1495. probably also going to copy propagate, but we can't be quite
  1496. sure. */
  1497. if (rhs_free && is_gimple_reg (lhs))
  1498. lhs_free = true;
  1499. /* Writes to parameters, parameters passed by value and return value
  1500. (either dirrectly or passed via invisible reference) are free.
  1501. TODO: We ought to handle testcase like
  1502. struct a {int a,b;};
  1503. struct a
  1504. retrurnsturct (void)
  1505. {
  1506. struct a a ={1,2};
  1507. return a;
  1508. }
  1509. This translate into:
  1510. retrurnsturct ()
  1511. {
  1512. int a$b;
  1513. int a$a;
  1514. struct a a;
  1515. struct a D.2739;
  1516. <bb 2>:
  1517. D.2739.a = 1;
  1518. D.2739.b = 2;
  1519. return D.2739;
  1520. }
  1521. For that we either need to copy ipa-split logic detecting writes
  1522. to return value. */
  1523. if (TREE_CODE (inner_lhs) == PARM_DECL
  1524. || TREE_CODE (inner_lhs) == RESULT_DECL
  1525. || (TREE_CODE (inner_lhs) == MEM_REF
  1526. && (unmodified_parm (stmt, TREE_OPERAND (inner_lhs, 0))
  1527. || (TREE_CODE (TREE_OPERAND (inner_lhs, 0)) == SSA_NAME
  1528. && SSA_NAME_VAR (TREE_OPERAND (inner_lhs, 0))
  1529. && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND
  1530. (inner_lhs,
  1531. 0))) == RESULT_DECL))))
  1532. lhs_free = true;
  1533. if (lhs_free
  1534. && (is_gimple_reg (rhs) || is_gimple_min_invariant (rhs)))
  1535. rhs_free = true;
  1536. if (lhs_free && rhs_free)
  1537. return 1;
  1538. }
  1539. return 0;
  1540. default:
  1541. return 0;
  1542. }
  1543. }
  1544. /* If BB ends by a conditional we can turn into predicates, attach corresponding
  1545. predicates to the CFG edges. */
  1546. static void
  1547. set_cond_stmt_execution_predicate (struct ipa_node_params *info,
  1548. struct inline_summary *summary,
  1549. basic_block bb)
  1550. {
  1551. gimple last;
  1552. tree op;
  1553. int index;
  1554. struct agg_position_info aggpos;
  1555. enum tree_code code, inverted_code;
  1556. edge e;
  1557. edge_iterator ei;
  1558. gimple set_stmt;
  1559. tree op2;
  1560. last = last_stmt (bb);
  1561. if (!last || gimple_code (last) != GIMPLE_COND)
  1562. return;
  1563. if (!is_gimple_ip_invariant (gimple_cond_rhs (last)))
  1564. return;
  1565. op = gimple_cond_lhs (last);
  1566. /* TODO: handle conditionals like
  1567. var = op0 < 4;
  1568. if (var != 0). */
  1569. if (unmodified_parm_or_parm_agg_item (info, last, op, &index, &aggpos))
  1570. {
  1571. code = gimple_cond_code (last);
  1572. inverted_code = invert_tree_comparison (code, HONOR_NANS (op));
  1573. FOR_EACH_EDGE (e, ei, bb->succs)
  1574. {
  1575. enum tree_code this_code = (e->flags & EDGE_TRUE_VALUE
  1576. ? code : inverted_code);
  1577. /* invert_tree_comparison will return ERROR_MARK on FP
  1578. comparsions that are not EQ/NE instead of returning proper
  1579. unordered one. Be sure it is not confused with NON_CONSTANT. */
  1580. if (this_code != ERROR_MARK)
  1581. {
  1582. struct predicate p = add_condition (summary, index, &aggpos,
  1583. this_code,
  1584. gimple_cond_rhs (last));
  1585. e->aux = pool_alloc (edge_predicate_pool);
  1586. *(struct predicate *) e->aux = p;
  1587. }
  1588. }
  1589. }
  1590. if (TREE_CODE (op) != SSA_NAME)
  1591. return;
  1592. /* Special case
  1593. if (builtin_constant_p (op))
  1594. constant_code
  1595. else
  1596. nonconstant_code.
  1597. Here we can predicate nonconstant_code. We can't
  1598. really handle constant_code since we have no predicate
  1599. for this and also the constant code is not known to be
  1600. optimized away when inliner doen't see operand is constant.
  1601. Other optimizers might think otherwise. */
  1602. if (gimple_cond_code (last) != NE_EXPR
  1603. || !integer_zerop (gimple_cond_rhs (last)))
  1604. return;
  1605. set_stmt = SSA_NAME_DEF_STMT (op);
  1606. if (!gimple_call_builtin_p (set_stmt, BUILT_IN_CONSTANT_P)
  1607. || gimple_call_num_args (set_stmt) != 1)
  1608. return;
  1609. op2 = gimple_call_arg (set_stmt, 0);
  1610. if (!unmodified_parm_or_parm_agg_item
  1611. (info, set_stmt, op2, &index, &aggpos))
  1612. return;
  1613. FOR_EACH_EDGE (e, ei, bb->succs) if (e->flags & EDGE_FALSE_VALUE)
  1614. {
  1615. struct predicate p = add_condition (summary, index, &aggpos,
  1616. IS_NOT_CONSTANT, NULL_TREE);
  1617. e->aux = pool_alloc (edge_predicate_pool);
  1618. *(struct predicate *) e->aux = p;
  1619. }
  1620. }
  1621. /* If BB ends by a switch we can turn into predicates, attach corresponding
  1622. predicates to the CFG edges. */
  1623. static void
  1624. set_switch_stmt_execution_predicate (struct ipa_node_params *info,
  1625. struct inline_summary *summary,
  1626. basic_block bb)
  1627. {
  1628. gimple lastg;
  1629. tree op;
  1630. int index;
  1631. struct agg_position_info aggpos;
  1632. edge e;
  1633. edge_iterator ei;
  1634. size_t n;
  1635. size_t case_idx;
  1636. lastg = last_stmt (bb);
  1637. if (!lastg || gimple_code (lastg) != GIMPLE_SWITCH)
  1638. return;
  1639. gswitch *last = as_a <gswitch *> (lastg);
  1640. op = gimple_switch_index (last);
  1641. if (!unmodified_parm_or_parm_agg_item (info, last, op, &index, &aggpos))
  1642. return;
  1643. FOR_EACH_EDGE (e, ei, bb->succs)
  1644. {
  1645. e->aux = pool_alloc (edge_predicate_pool);
  1646. *(struct predicate *) e->aux = false_predicate ();
  1647. }
  1648. n = gimple_switch_num_labels (last);
  1649. for (case_idx = 0; case_idx < n; ++case_idx)
  1650. {
  1651. tree cl = gimple_switch_label (last, case_idx);
  1652. tree min, max;
  1653. struct predicate p;
  1654. e = find_edge (bb, label_to_block (CASE_LABEL (cl)));
  1655. min = CASE_LOW (cl);
  1656. max = CASE_HIGH (cl);
  1657. /* For default we might want to construct predicate that none
  1658. of cases is met, but it is bit hard to do not having negations
  1659. of conditionals handy. */
  1660. if (!min && !max)
  1661. p = true_predicate ();
  1662. else if (!max)
  1663. p = add_condition (summary, index, &aggpos, EQ_EXPR, min);
  1664. else
  1665. {
  1666. struct predicate p1, p2;
  1667. p1 = add_condition (summary, index, &aggpos, GE_EXPR, min);
  1668. p2 = add_condition (summary, index, &aggpos, LE_EXPR, max);
  1669. p = and_predicates (summary->conds, &p1, &p2);
  1670. }
  1671. *(struct predicate *) e->aux
  1672. = or_predicates (summary->conds, &p, (struct predicate *) e->aux);
  1673. }
  1674. }
  1675. /* For each BB in NODE attach to its AUX pointer predicate under
  1676. which it is executable. */
  1677. static void
  1678. compute_bb_predicates (struct cgraph_node *node,
  1679. struct ipa_node_params *parms_info,
  1680. struct inline_summary *summary)
  1681. {
  1682. struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
  1683. bool done = false;
  1684. basic_block bb;
  1685. FOR_EACH_BB_FN (bb, my_function)
  1686. {
  1687. set_cond_stmt_execution_predicate (parms_info, summary, bb);
  1688. set_switch_stmt_execution_predicate (parms_info, summary, bb);
  1689. }
  1690. /* Entry block is always executable. */
  1691. ENTRY_BLOCK_PTR_FOR_FN (my_function)->aux
  1692. = pool_alloc (edge_predicate_pool);
  1693. *(struct predicate *) ENTRY_BLOCK_PTR_FOR_FN (my_function)->aux
  1694. = true_predicate ();
  1695. /* A simple dataflow propagation of predicates forward in the CFG.
  1696. TODO: work in reverse postorder. */
  1697. while (!done)
  1698. {
  1699. done = true;
  1700. FOR_EACH_BB_FN (bb, my_function)
  1701. {
  1702. struct predicate p = false_predicate ();
  1703. edge e;
  1704. edge_iterator ei;
  1705. FOR_EACH_EDGE (e, ei, bb->preds)
  1706. {
  1707. if (e->src->aux)
  1708. {
  1709. struct predicate this_bb_predicate
  1710. = *(struct predicate *) e->src->aux;
  1711. if (e->aux)
  1712. this_bb_predicate
  1713. = and_predicates (summary->conds, &this_bb_predicate,
  1714. (struct predicate *) e->aux);
  1715. p = or_predicates (summary->conds, &p, &this_bb_predicate);
  1716. if (true_predicate_p (&p))
  1717. break;
  1718. }
  1719. }
  1720. if (false_predicate_p (&p))
  1721. gcc_assert (!bb->aux);
  1722. else
  1723. {
  1724. if (!bb->aux)
  1725. {
  1726. done = false;
  1727. bb->aux = pool_alloc (edge_predicate_pool);
  1728. *((struct predicate *) bb->aux) = p;
  1729. }
  1730. else if (!predicates_equal_p (&p, (struct predicate *) bb->aux))
  1731. {
  1732. /* This OR operation is needed to ensure monotonous data flow
  1733. in the case we hit the limit on number of clauses and the
  1734. and/or operations above give approximate answers. */
  1735. p = or_predicates (summary->conds, &p, (struct predicate *)bb->aux);
  1736. if (!predicates_equal_p (&p, (struct predicate *) bb->aux))
  1737. {
  1738. done = false;
  1739. *((struct predicate *) bb->aux) = p;
  1740. }
  1741. }
  1742. }
  1743. }
  1744. }
  1745. }
  1746. /* We keep info about constantness of SSA names. */
  1747. typedef struct predicate predicate_t;
  1748. /* Return predicate specifying when the STMT might have result that is not
  1749. a compile time constant. */
  1750. static struct predicate
  1751. will_be_nonconstant_expr_predicate (struct ipa_node_params *info,
  1752. struct inline_summary *summary,
  1753. tree expr,
  1754. vec<predicate_t> nonconstant_names)
  1755. {
  1756. tree parm;
  1757. int index;
  1758. while (UNARY_CLASS_P (expr))
  1759. expr = TREE_OPERAND (expr, 0);
  1760. parm = unmodified_parm (NULL, expr);
  1761. if (parm && (index = ipa_get_param_decl_index (info, parm)) >= 0)
  1762. return add_condition (summary, index, NULL, CHANGED, NULL_TREE);
  1763. if (is_gimple_min_invariant (expr))
  1764. return false_predicate ();
  1765. if (TREE_CODE (expr) == SSA_NAME)
  1766. return nonconstant_names[SSA_NAME_VERSION (expr)];
  1767. if (BINARY_CLASS_P (expr) || COMPARISON_CLASS_P (expr))
  1768. {
  1769. struct predicate p1 = will_be_nonconstant_expr_predicate
  1770. (info, summary, TREE_OPERAND (expr, 0),
  1771. nonconstant_names);
  1772. struct predicate p2;
  1773. if (true_predicate_p (&p1))
  1774. return p1;
  1775. p2 = will_be_nonconstant_expr_predicate (info, summary,
  1776. TREE_OPERAND (expr, 1),
  1777. nonconstant_names);
  1778. return or_predicates (summary->conds, &p1, &p2);
  1779. }
  1780. else if (TREE_CODE (expr) == COND_EXPR)
  1781. {
  1782. struct predicate p1 = will_be_nonconstant_expr_predicate
  1783. (info, summary, TREE_OPERAND (expr, 0),
  1784. nonconstant_names);
  1785. struct predicate p2;
  1786. if (true_predicate_p (&p1))
  1787. return p1;
  1788. p2 = will_be_nonconstant_expr_predicate (info, summary,
  1789. TREE_OPERAND (expr, 1),
  1790. nonconstant_names);
  1791. if (true_predicate_p (&p2))
  1792. return p2;
  1793. p1 = or_predicates (summary->conds, &p1, &p2);
  1794. p2 = will_be_nonconstant_expr_predicate (info, summary,
  1795. TREE_OPERAND (expr, 2),
  1796. nonconstant_names);
  1797. return or_predicates (summary->conds, &p1, &p2);
  1798. }
  1799. else
  1800. {
  1801. debug_tree (expr);
  1802. gcc_unreachable ();
  1803. }
  1804. return false_predicate ();
  1805. }
  1806. /* Return predicate specifying when the STMT might have result that is not
  1807. a compile time constant. */
  1808. static struct predicate
  1809. will_be_nonconstant_predicate (struct ipa_node_params *info,
  1810. struct inline_summary *summary,
  1811. gimple stmt,
  1812. vec<predicate_t> nonconstant_names)
  1813. {
  1814. struct predicate p = true_predicate ();
  1815. ssa_op_iter iter;
  1816. tree use;
  1817. struct predicate op_non_const;
  1818. bool is_load;
  1819. int base_index;
  1820. struct agg_position_info aggpos;
  1821. /* What statments might be optimized away
  1822. when their arguments are constant. */
  1823. if (gimple_code (stmt) != GIMPLE_ASSIGN
  1824. && gimple_code (stmt) != GIMPLE_COND
  1825. && gimple_code (stmt) != GIMPLE_SWITCH
  1826. && (gimple_code (stmt) != GIMPLE_CALL
  1827. || !(gimple_call_flags (stmt) & ECF_CONST)))
  1828. return p;
  1829. /* Stores will stay anyway. */
  1830. if (gimple_store_p (stmt))
  1831. return p;
  1832. is_load = gimple_assign_load_p (stmt);
  1833. /* Loads can be optimized when the value is known. */
  1834. if (is_load)
  1835. {
  1836. tree op;
  1837. gcc_assert (gimple_assign_single_p (stmt));
  1838. op = gimple_assign_rhs1 (stmt);
  1839. if (!unmodified_parm_or_parm_agg_item (info, stmt, op, &base_index,
  1840. &aggpos))
  1841. return p;
  1842. }
  1843. else
  1844. base_index = -1;
  1845. /* See if we understand all operands before we start
  1846. adding conditionals. */
  1847. FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
  1848. {
  1849. tree parm = unmodified_parm (stmt, use);
  1850. /* For arguments we can build a condition. */
  1851. if (parm && ipa_get_param_decl_index (info, parm) >= 0)
  1852. continue;
  1853. if (TREE_CODE (use) != SSA_NAME)
  1854. return p;
  1855. /* If we know when operand is constant,
  1856. we still can say something useful. */
  1857. if (!true_predicate_p (&nonconstant_names[SSA_NAME_VERSION (use)]))
  1858. continue;
  1859. return p;
  1860. }
  1861. if (is_load)
  1862. op_non_const =
  1863. add_condition (summary, base_index, &aggpos, CHANGED, NULL);
  1864. else
  1865. op_non_const = false_predicate ();
  1866. FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
  1867. {
  1868. tree parm = unmodified_parm (stmt, use);
  1869. int index;
  1870. if (parm && (index = ipa_get_param_decl_index (info, parm)) >= 0)
  1871. {
  1872. if (index != base_index)
  1873. p = add_condition (summary, index, NULL, CHANGED, NULL_TREE);
  1874. else
  1875. continue;
  1876. }
  1877. else
  1878. p = nonconstant_names[SSA_NAME_VERSION (use)];
  1879. op_non_const = or_predicates (summary->conds, &p, &op_non_const);
  1880. }
  1881. if ((gimple_code (stmt) == GIMPLE_ASSIGN || gimple_code (stmt) == GIMPLE_CALL)
  1882. && gimple_op (stmt, 0)
  1883. && TREE_CODE (gimple_op (stmt, 0)) == SSA_NAME)
  1884. nonconstant_names[SSA_NAME_VERSION (gimple_op (stmt, 0))]
  1885. = op_non_const;
  1886. return op_non_const;
  1887. }
  1888. struct record_modified_bb_info
  1889. {
  1890. bitmap bb_set;
  1891. gimple stmt;
  1892. };
  1893. /* Callback of walk_aliased_vdefs. Records basic blocks where the value may be
  1894. set except for info->stmt. */
  1895. static bool
  1896. record_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
  1897. {
  1898. struct record_modified_bb_info *info =
  1899. (struct record_modified_bb_info *) data;
  1900. if (SSA_NAME_DEF_STMT (vdef) == info->stmt)
  1901. return false;
  1902. bitmap_set_bit (info->bb_set,
  1903. SSA_NAME_IS_DEFAULT_DEF (vdef)
  1904. ? ENTRY_BLOCK_PTR_FOR_FN (cfun)->index
  1905. : gimple_bb (SSA_NAME_DEF_STMT (vdef))->index);
  1906. return false;
  1907. }
  1908. /* Return probability (based on REG_BR_PROB_BASE) that I-th parameter of STMT
  1909. will change since last invocation of STMT.
  1910. Value 0 is reserved for compile time invariants.
  1911. For common parameters it is REG_BR_PROB_BASE. For loop invariants it
  1912. ought to be REG_BR_PROB_BASE / estimated_iters. */
  1913. static int
  1914. param_change_prob (gimple stmt, int i)
  1915. {
  1916. tree op = gimple_call_arg (stmt, i);
  1917. basic_block bb = gimple_bb (stmt);
  1918. tree base;
  1919. /* Global invariants neve change. */
  1920. if (is_gimple_min_invariant (op))
  1921. return 0;
  1922. /* We would have to do non-trivial analysis to really work out what
  1923. is the probability of value to change (i.e. when init statement
  1924. is in a sibling loop of the call).
  1925. We do an conservative estimate: when call is executed N times more often
  1926. than the statement defining value, we take the frequency 1/N. */
  1927. if (TREE_CODE (op) == SSA_NAME)
  1928. {
  1929. int init_freq;
  1930. if (!bb->frequency)
  1931. return REG_BR_PROB_BASE;
  1932. if (SSA_NAME_IS_DEFAULT_DEF (op))
  1933. init_freq = ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency;
  1934. else
  1935. init_freq = gimple_bb (SSA_NAME_DEF_STMT (op))->frequency;
  1936. if (!init_freq)
  1937. init_freq = 1;
  1938. if (init_freq < bb->frequency)
  1939. return MAX (GCOV_COMPUTE_SCALE (init_freq, bb->frequency), 1);
  1940. else
  1941. return REG_BR_PROB_BASE;
  1942. }
  1943. base = get_base_address (op);
  1944. if (base)
  1945. {
  1946. ao_ref refd;
  1947. int max;
  1948. struct record_modified_bb_info info;
  1949. bitmap_iterator bi;
  1950. unsigned index;
  1951. tree init = ctor_for_folding (base);
  1952. if (init != error_mark_node)
  1953. return 0;
  1954. if (!bb->frequency)
  1955. return REG_BR_PROB_BASE;
  1956. ao_ref_init (&refd, op);
  1957. info.stmt = stmt;
  1958. info.bb_set = BITMAP_ALLOC (NULL);
  1959. walk_aliased_vdefs (&refd, gimple_vuse (stmt), record_modified, &info,
  1960. NULL);
  1961. if (bitmap_bit_p (info.bb_set, bb->index))
  1962. {
  1963. BITMAP_FREE (info.bb_set);
  1964. return REG_BR_PROB_BASE;
  1965. }
  1966. /* Assume that every memory is initialized at entry.
  1967. TODO: Can we easilly determine if value is always defined
  1968. and thus we may skip entry block? */
  1969. if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency)
  1970. max = ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency;
  1971. else
  1972. max = 1;
  1973. EXECUTE_IF_SET_IN_BITMAP (info.bb_set, 0, index, bi)
  1974. max = MIN (max, BASIC_BLOCK_FOR_FN (cfun, index)->frequency);
  1975. BITMAP_FREE (info.bb_set);
  1976. if (max < bb->frequency)
  1977. return MAX (GCOV_COMPUTE_SCALE (max, bb->frequency), 1);
  1978. else
  1979. return REG_BR_PROB_BASE;
  1980. }
  1981. return REG_BR_PROB_BASE;
  1982. }
  1983. /* Find whether a basic block BB is the final block of a (half) diamond CFG
  1984. sub-graph and if the predicate the condition depends on is known. If so,
  1985. return true and store the pointer the predicate in *P. */
  1986. static bool
  1987. phi_result_unknown_predicate (struct ipa_node_params *info,
  1988. inline_summary *summary, basic_block bb,
  1989. struct predicate *p,
  1990. vec<predicate_t> nonconstant_names)
  1991. {
  1992. edge e;
  1993. edge_iterator ei;
  1994. basic_block first_bb = NULL;
  1995. gimple stmt;
  1996. if (single_pred_p (bb))
  1997. {
  1998. *p = false_predicate ();
  1999. return true;
  2000. }
  2001. FOR_EACH_EDGE (e, ei, bb->preds)
  2002. {
  2003. if (single_succ_p (e->src))
  2004. {
  2005. if (!single_pred_p (e->src))
  2006. return false;
  2007. if (!first_bb)
  2008. first_bb = single_pred (e->src);
  2009. else if (single_pred (e->src) != first_bb)
  2010. return false;
  2011. }
  2012. else
  2013. {
  2014. if (!first_bb)
  2015. first_bb = e->src;
  2016. else if (e->src != first_bb)
  2017. return false;
  2018. }
  2019. }
  2020. if (!first_bb)
  2021. return false;
  2022. stmt = last_stmt (first_bb);
  2023. if (!stmt
  2024. || gimple_code (stmt) != GIMPLE_COND
  2025. || !is_gimple_ip_invariant (gimple_cond_rhs (stmt)))
  2026. return false;
  2027. *p = will_be_nonconstant_expr_predicate (info, summary,
  2028. gimple_cond_lhs (stmt),
  2029. nonconstant_names);
  2030. if (true_predicate_p (p))
  2031. return false;
  2032. else
  2033. return true;
  2034. }
  2035. /* Given a PHI statement in a function described by inline properties SUMMARY
  2036. and *P being the predicate describing whether the selected PHI argument is
  2037. known, store a predicate for the result of the PHI statement into
  2038. NONCONSTANT_NAMES, if possible. */
  2039. static void
  2040. predicate_for_phi_result (struct inline_summary *summary, gphi *phi,
  2041. struct predicate *p,
  2042. vec<predicate_t> nonconstant_names)
  2043. {
  2044. unsigned i;
  2045. for (i = 0; i < gimple_phi_num_args (phi); i++)
  2046. {
  2047. tree arg = gimple_phi_arg (phi, i)->def;
  2048. if (!is_gimple_min_invariant (arg))
  2049. {
  2050. gcc_assert (TREE_CODE (arg) == SSA_NAME);
  2051. *p = or_predicates (summary->conds, p,
  2052. &nonconstant_names[SSA_NAME_VERSION (arg)]);
  2053. if (true_predicate_p (p))
  2054. return;
  2055. }
  2056. }
  2057. if (dump_file && (dump_flags & TDF_DETAILS))
  2058. {
  2059. fprintf (dump_file, "\t\tphi predicate: ");
  2060. dump_predicate (dump_file, summary->conds, p);
  2061. }
  2062. nonconstant_names[SSA_NAME_VERSION (gimple_phi_result (phi))] = *p;
  2063. }
  2064. /* Return predicate specifying when array index in access OP becomes non-constant. */
  2065. static struct predicate
  2066. array_index_predicate (inline_summary *info,
  2067. vec< predicate_t> nonconstant_names, tree op)
  2068. {
  2069. struct predicate p = false_predicate ();
  2070. while (handled_component_p (op))
  2071. {
  2072. if (TREE_CODE (op) == ARRAY_REF || TREE_CODE (op) == ARRAY_RANGE_REF)
  2073. {
  2074. if (TREE_CODE (TREE_OPERAND (op, 1)) == SSA_NAME)
  2075. p = or_predicates (info->conds, &p,
  2076. &nonconstant_names[SSA_NAME_VERSION
  2077. (TREE_OPERAND (op, 1))]);
  2078. }
  2079. op = TREE_OPERAND (op, 0);
  2080. }
  2081. return p;
  2082. }
  2083. /* For a typical usage of __builtin_expect (a<b, 1), we
  2084. may introduce an extra relation stmt:
  2085. With the builtin, we have
  2086. t1 = a <= b;
  2087. t2 = (long int) t1;
  2088. t3 = __builtin_expect (t2, 1);
  2089. if (t3 != 0)
  2090. goto ...
  2091. Without the builtin, we have
  2092. if (a<=b)
  2093. goto...
  2094. This affects the size/time estimation and may have
  2095. an impact on the earlier inlining.
  2096. Here find this pattern and fix it up later. */
  2097. static gimple
  2098. find_foldable_builtin_expect (basic_block bb)
  2099. {
  2100. gimple_stmt_iterator bsi;
  2101. for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
  2102. {
  2103. gimple stmt = gsi_stmt (bsi);
  2104. if (gimple_call_builtin_p (stmt, BUILT_IN_EXPECT)
  2105. || (is_gimple_call (stmt)
  2106. && gimple_call_internal_p (stmt)
  2107. && gimple_call_internal_fn (stmt) == IFN_BUILTIN_EXPECT))
  2108. {
  2109. tree var = gimple_call_lhs (stmt);
  2110. tree arg = gimple_call_arg (stmt, 0);
  2111. use_operand_p use_p;
  2112. gimple use_stmt;
  2113. bool match = false;
  2114. bool done = false;
  2115. if (!var || !arg)
  2116. continue;
  2117. gcc_assert (TREE_CODE (var) == SSA_NAME);
  2118. while (TREE_CODE (arg) == SSA_NAME)
  2119. {
  2120. gimple stmt_tmp = SSA_NAME_DEF_STMT (arg);
  2121. if (!is_gimple_assign (stmt_tmp))
  2122. break;
  2123. switch (gimple_assign_rhs_code (stmt_tmp))
  2124. {
  2125. case LT_EXPR:
  2126. case LE_EXPR:
  2127. case GT_EXPR:
  2128. case GE_EXPR:
  2129. case EQ_EXPR:
  2130. case NE_EXPR:
  2131. match = true;
  2132. done = true;
  2133. break;
  2134. CASE_CONVERT:
  2135. break;
  2136. default:
  2137. done = true;
  2138. break;
  2139. }
  2140. if (done)
  2141. break;
  2142. arg = gimple_assign_rhs1 (stmt_tmp);
  2143. }
  2144. if (match && single_imm_use (var, &use_p, &use_stmt)
  2145. && gimple_code (use_stmt) == GIMPLE_COND)
  2146. return use_stmt;
  2147. }
  2148. }
  2149. return NULL;
  2150. }
  2151. /* Return true when the basic blocks contains only clobbers followed by RESX.
  2152. Such BBs are kept around to make removal of dead stores possible with
  2153. presence of EH and will be optimized out by optimize_clobbers later in the
  2154. game.
  2155. NEED_EH is used to recurse in case the clobber has non-EH predecestors
  2156. that can be clobber only, too.. When it is false, the RESX is not necessary
  2157. on the end of basic block. */
  2158. static bool
  2159. clobber_only_eh_bb_p (basic_block bb, bool need_eh = true)
  2160. {
  2161. gimple_stmt_iterator gsi = gsi_last_bb (bb);
  2162. edge_iterator ei;
  2163. edge e;
  2164. if (need_eh)
  2165. {
  2166. if (gsi_end_p (gsi))
  2167. return false;
  2168. if (gimple_code (gsi_stmt (gsi)) != GIMPLE_RESX)
  2169. return false;
  2170. gsi_prev (&gsi);
  2171. }
  2172. else if (!single_succ_p (bb))
  2173. return false;
  2174. for (; !gsi_end_p (gsi); gsi_prev (&gsi))
  2175. {
  2176. gimple stmt = gsi_stmt (gsi);
  2177. if (is_gimple_debug (stmt))
  2178. continue;
  2179. if (gimple_clobber_p (stmt))
  2180. continue;
  2181. if (gimple_code (stmt) == GIMPLE_LABEL)
  2182. break;
  2183. return false;
  2184. }
  2185. /* See if all predecestors are either throws or clobber only BBs. */
  2186. FOR_EACH_EDGE (e, ei, bb->preds)
  2187. if (!(e->flags & EDGE_EH)
  2188. && !clobber_only_eh_bb_p (e->src, false))
  2189. return false;
  2190. return true;
  2191. }
  2192. /* Compute function body size parameters for NODE.
  2193. When EARLY is true, we compute only simple summaries without
  2194. non-trivial predicates to drive the early inliner. */
  2195. static void
  2196. estimate_function_body_sizes (struct cgraph_node *node, bool early)
  2197. {
  2198. gcov_type time = 0;
  2199. /* Estimate static overhead for function prologue/epilogue and alignment. */
  2200. int size = 2;
  2201. /* Benefits are scaled by probability of elimination that is in range
  2202. <0,2>. */
  2203. basic_block bb;
  2204. struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
  2205. int freq;
  2206. struct inline_summary *info = inline_summaries->get (node);
  2207. struct predicate bb_predicate;
  2208. struct ipa_node_params *parms_info = NULL;
  2209. vec<predicate_t> nonconstant_names = vNULL;
  2210. int nblocks, n;
  2211. int *order;
  2212. predicate array_index = true_predicate ();
  2213. gimple fix_builtin_expect_stmt;
  2214. info->conds = NULL;
  2215. info->entry = NULL;
  2216. /* When optimizing and analyzing for IPA inliner, initialize loop optimizer
  2217. so we can produce proper inline hints.
  2218. When optimizing and analyzing for early inliner, initialize node params
  2219. so we can produce correct BB predicates. */
  2220. if (opt_for_fn (node->decl, optimize))
  2221. {
  2222. calculate_dominance_info (CDI_DOMINATORS);
  2223. if (!early)
  2224. loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
  2225. else
  2226. {
  2227. ipa_check_create_node_params ();
  2228. ipa_initialize_node_params (node);
  2229. }
  2230. if (ipa_node_params_sum)
  2231. {
  2232. parms_info = IPA_NODE_REF (node);
  2233. nonconstant_names.safe_grow_cleared
  2234. (SSANAMES (my_function)->length ());
  2235. }
  2236. }
  2237. if (dump_file)
  2238. fprintf (dump_file, "\nAnalyzing function body size: %s\n",
  2239. node->name ());
  2240. /* When we run into maximal number of entries, we assign everything to the
  2241. constant truth case. Be sure to have it in list. */
  2242. bb_predicate = true_predicate ();
  2243. account_size_time (info, 0, 0, &bb_predicate);
  2244. bb_predicate = not_inlined_predicate ();
  2245. account_size_time (info, 2 * INLINE_SIZE_SCALE, 0, &bb_predicate);
  2246. gcc_assert (my_function && my_function->cfg);
  2247. if (parms_info)
  2248. compute_bb_predicates (node, parms_info, info);
  2249. gcc_assert (cfun == my_function);
  2250. order = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
  2251. nblocks = pre_and_rev_post_order_compute (NULL, order, false);
  2252. for (n = 0; n < nblocks; n++)
  2253. {
  2254. bb = BASIC_BLOCK_FOR_FN (cfun, order[n]);
  2255. freq = compute_call_stmt_bb_frequency (node->decl, bb);
  2256. if (clobber_only_eh_bb_p (bb))
  2257. {
  2258. if (dump_file && (dump_flags & TDF_DETAILS))
  2259. fprintf (dump_file, "\n Ignoring BB %i;"
  2260. " it will be optimized away by cleanup_clobbers\n",
  2261. bb->index);
  2262. continue;
  2263. }
  2264. /* TODO: Obviously predicates can be propagated down across CFG. */
  2265. if (parms_info)
  2266. {
  2267. if (bb->aux)
  2268. bb_predicate = *(struct predicate *) bb->aux;
  2269. else
  2270. bb_predicate = false_predicate ();
  2271. }
  2272. else
  2273. bb_predicate = true_predicate ();
  2274. if (dump_file && (dump_flags & TDF_DETAILS))
  2275. {
  2276. fprintf (dump_file, "\n BB %i predicate:", bb->index);
  2277. dump_predicate (dump_file, info->conds, &bb_predicate);
  2278. }
  2279. if (parms_info && nonconstant_names.exists ())
  2280. {
  2281. struct predicate phi_predicate;
  2282. bool first_phi = true;
  2283. for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
  2284. gsi_next (&bsi))
  2285. {
  2286. if (first_phi
  2287. && !phi_result_unknown_predicate (parms_info, info, bb,
  2288. &phi_predicate,
  2289. nonconstant_names))
  2290. break;
  2291. first_phi = false;
  2292. if (dump_file && (dump_flags & TDF_DETAILS))
  2293. {
  2294. fprintf (dump_file, " ");
  2295. print_gimple_stmt (dump_file, gsi_stmt (bsi), 0, 0);
  2296. }
  2297. predicate_for_phi_result (info, bsi.phi (), &phi_predicate,
  2298. nonconstant_names);
  2299. }
  2300. }
  2301. fix_builtin_expect_stmt = find_foldable_builtin_expect (bb);
  2302. for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
  2303. gsi_next (&bsi))
  2304. {
  2305. gimple stmt = gsi_stmt (bsi);
  2306. int this_size = estimate_num_insns (stmt, &eni_size_weights);
  2307. int this_time = estimate_num_insns (stmt, &eni_time_weights);
  2308. int prob;
  2309. struct predicate will_be_nonconstant;
  2310. /* This relation stmt should be folded after we remove
  2311. buildin_expect call. Adjust the cost here. */
  2312. if (stmt == fix_builtin_expect_stmt)
  2313. {
  2314. this_size--;
  2315. this_time--;
  2316. }
  2317. if (dump_file && (dump_flags & TDF_DETAILS))
  2318. {
  2319. fprintf (dump_file, " ");
  2320. print_gimple_stmt (dump_file, stmt, 0, 0);
  2321. fprintf (dump_file, "\t\tfreq:%3.2f size:%3i time:%3i\n",
  2322. ((double) freq) / CGRAPH_FREQ_BASE, this_size,
  2323. this_time);
  2324. }
  2325. if (gimple_assign_load_p (stmt) && nonconstant_names.exists ())
  2326. {
  2327. struct predicate this_array_index;
  2328. this_array_index =
  2329. array_index_predicate (info, nonconstant_names,
  2330. gimple_assign_rhs1 (stmt));
  2331. if (!false_predicate_p (&this_array_index))
  2332. array_index =
  2333. and_predicates (info->conds, &array_index,
  2334. &this_array_index);
  2335. }
  2336. if (gimple_store_p (stmt) && nonconstant_names.exists ())
  2337. {
  2338. struct predicate this_array_index;
  2339. this_array_index =
  2340. array_index_predicate (info, nonconstant_names,
  2341. gimple_get_lhs (stmt));
  2342. if (!false_predicate_p (&this_array_index))
  2343. array_index =
  2344. and_predicates (info->conds, &array_index,
  2345. &this_array_index);
  2346. }
  2347. if (is_gimple_call (stmt)
  2348. && !gimple_call_internal_p (stmt))
  2349. {
  2350. struct cgraph_edge *edge = node->get_edge (stmt);
  2351. struct inline_edge_summary *es = inline_edge_summary (edge);
  2352. /* Special case: results of BUILT_IN_CONSTANT_P will be always
  2353. resolved as constant. We however don't want to optimize
  2354. out the cgraph edges. */
  2355. if (nonconstant_names.exists ()
  2356. && gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P)
  2357. && gimple_call_lhs (stmt)
  2358. && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
  2359. {
  2360. struct predicate false_p = false_predicate ();
  2361. nonconstant_names[SSA_NAME_VERSION (gimple_call_lhs (stmt))]
  2362. = false_p;
  2363. }
  2364. if (ipa_node_params_sum)
  2365. {
  2366. int count = gimple_call_num_args (stmt);
  2367. int i;
  2368. if (count)
  2369. es->param.safe_grow_cleared (count);
  2370. for (i = 0; i < count; i++)
  2371. {
  2372. int prob = param_change_prob (stmt, i);
  2373. gcc_assert (prob >= 0 && prob <= REG_BR_PROB_BASE);
  2374. es->param[i].change_prob = prob;
  2375. }
  2376. }
  2377. es->call_stmt_size = this_size;
  2378. es->call_stmt_time = this_time;
  2379. es->loop_depth = bb_loop_depth (bb);
  2380. edge_set_predicate (edge, &bb_predicate);
  2381. }
  2382. /* TODO: When conditional jump or swithc is known to be constant, but
  2383. we did not translate it into the predicates, we really can account
  2384. just maximum of the possible paths. */
  2385. if (parms_info)
  2386. will_be_nonconstant
  2387. = will_be_nonconstant_predicate (parms_info, info,
  2388. stmt, nonconstant_names);
  2389. if (this_time || this_size)
  2390. {
  2391. struct predicate p;
  2392. this_time *= freq;
  2393. prob = eliminated_by_inlining_prob (stmt);
  2394. if (prob == 1 && dump_file && (dump_flags & TDF_DETAILS))
  2395. fprintf (dump_file,
  2396. "\t\t50%% will be eliminated by inlining\n");
  2397. if (prob == 2 && dump_file && (dump_flags & TDF_DETAILS))
  2398. fprintf (dump_file, "\t\tWill be eliminated by inlining\n");
  2399. if (parms_info)
  2400. p = and_predicates (info->conds, &bb_predicate,
  2401. &will_be_nonconstant);
  2402. else
  2403. p = true_predicate ();
  2404. if (!false_predicate_p (&p)
  2405. || (is_gimple_call (stmt)
  2406. && !false_predicate_p (&bb_predicate)))
  2407. {
  2408. time += this_time;
  2409. size += this_size;
  2410. if (time > MAX_TIME * INLINE_TIME_SCALE)
  2411. time = MAX_TIME * INLINE_TIME_SCALE;
  2412. }
  2413. /* We account everything but the calls. Calls have their own
  2414. size/time info attached to cgraph edges. This is necessary
  2415. in order to make the cost disappear after inlining. */
  2416. if (!is_gimple_call (stmt))
  2417. {
  2418. if (prob)
  2419. {
  2420. struct predicate ip = not_inlined_predicate ();
  2421. ip = and_predicates (info->conds, &ip, &p);
  2422. account_size_time (info, this_size * prob,
  2423. this_time * prob, &ip);
  2424. }
  2425. if (prob != 2)
  2426. account_size_time (info, this_size * (2 - prob),
  2427. this_time * (2 - prob), &p);
  2428. }
  2429. gcc_assert (time >= 0);
  2430. gcc_assert (size >= 0);
  2431. }
  2432. }
  2433. }
  2434. set_hint_predicate (&inline_summaries->get (node)->array_index, array_index);
  2435. time = (time + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
  2436. if (time > MAX_TIME)
  2437. time = MAX_TIME;
  2438. free (order);
  2439. if (nonconstant_names.exists () && !early)
  2440. {
  2441. struct loop *loop;
  2442. predicate loop_iterations = true_predicate ();
  2443. predicate loop_stride = true_predicate ();
  2444. if (dump_file && (dump_flags & TDF_DETAILS))
  2445. flow_loops_dump (dump_file, NULL, 0);
  2446. scev_initialize ();
  2447. FOR_EACH_LOOP (loop, 0)
  2448. {
  2449. vec<edge> exits;
  2450. edge ex;
  2451. unsigned int j, i;
  2452. struct tree_niter_desc niter_desc;
  2453. basic_block *body = get_loop_body (loop);
  2454. bb_predicate = *(struct predicate *) loop->header->aux;
  2455. exits = get_loop_exit_edges (loop);
  2456. FOR_EACH_VEC_ELT (exits, j, ex)
  2457. if (number_of_iterations_exit (loop, ex, &niter_desc, false)
  2458. && !is_gimple_min_invariant (niter_desc.niter))
  2459. {
  2460. predicate will_be_nonconstant
  2461. = will_be_nonconstant_expr_predicate (parms_info, info,
  2462. niter_desc.niter,
  2463. nonconstant_names);
  2464. if (!true_predicate_p (&will_be_nonconstant))
  2465. will_be_nonconstant = and_predicates (info->conds,
  2466. &bb_predicate,
  2467. &will_be_nonconstant);
  2468. if (!true_predicate_p (&will_be_nonconstant)
  2469. && !false_predicate_p (&will_be_nonconstant))
  2470. /* This is slightly inprecise. We may want to represent each
  2471. loop with independent predicate. */
  2472. loop_iterations =
  2473. and_predicates (info->conds, &loop_iterations,
  2474. &will_be_nonconstant);
  2475. }
  2476. exits.release ();
  2477. for (i = 0; i < loop->num_nodes; i++)
  2478. {
  2479. gimple_stmt_iterator gsi;
  2480. bb_predicate = *(struct predicate *) body[i]->aux;
  2481. for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi);
  2482. gsi_next (&gsi))
  2483. {
  2484. gimple stmt = gsi_stmt (gsi);
  2485. affine_iv iv;
  2486. ssa_op_iter iter;
  2487. tree use;
  2488. FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
  2489. {
  2490. predicate will_be_nonconstant;
  2491. if (!simple_iv
  2492. (loop, loop_containing_stmt (stmt), use, &iv, true)
  2493. || is_gimple_min_invariant (iv.step))
  2494. continue;
  2495. will_be_nonconstant
  2496. = will_be_nonconstant_expr_predicate (parms_info, info,
  2497. iv.step,
  2498. nonconstant_names);
  2499. if (!true_predicate_p (&will_be_nonconstant))
  2500. will_be_nonconstant
  2501. = and_predicates (info->conds,
  2502. &bb_predicate,
  2503. &will_be_nonconstant);
  2504. if (!true_predicate_p (&will_be_nonconstant)
  2505. && !false_predicate_p (&will_be_nonconstant))
  2506. /* This is slightly inprecise. We may want to represent
  2507. each loop with independent predicate. */
  2508. loop_stride =
  2509. and_predicates (info->conds, &loop_stride,
  2510. &will_be_nonconstant);
  2511. }
  2512. }
  2513. }
  2514. free (body);
  2515. }
  2516. set_hint_predicate (&inline_summaries->get (node)->loop_iterations,
  2517. loop_iterations);
  2518. set_hint_predicate (&inline_summaries->get (node)->loop_stride, loop_stride);
  2519. scev_finalize ();
  2520. }
  2521. FOR_ALL_BB_FN (bb, my_function)
  2522. {
  2523. edge e;
  2524. edge_iterator ei;
  2525. if (bb->aux)
  2526. pool_free (edge_predicate_pool, bb->aux);
  2527. bb->aux = NULL;
  2528. FOR_EACH_EDGE (e, ei, bb->succs)
  2529. {
  2530. if (e->aux)
  2531. pool_free (edge_predicate_pool, e->aux);
  2532. e->aux = NULL;
  2533. }
  2534. }
  2535. inline_summaries->get (node)->self_time = time;
  2536. inline_summaries->get (node)->self_size = size;
  2537. nonconstant_names.release ();
  2538. if (opt_for_fn (node->decl, optimize))
  2539. {
  2540. if (!early)
  2541. loop_optimizer_finalize ();
  2542. else if (!ipa_edge_args_vector)
  2543. ipa_free_all_node_params ();
  2544. free_dominance_info (CDI_DOMINATORS);
  2545. }
  2546. if (dump_file)
  2547. {
  2548. fprintf (dump_file, "\n");
  2549. dump_inline_summary (dump_file, node);
  2550. }
  2551. }
  2552. /* Compute parameters of functions used by inliner.
  2553. EARLY is true when we compute parameters for the early inliner */
  2554. void
  2555. compute_inline_parameters (struct cgraph_node *node, bool early)
  2556. {
  2557. HOST_WIDE_INT self_stack_size;
  2558. struct cgraph_edge *e;
  2559. struct inline_summary *info;
  2560. gcc_assert (!node->global.inlined_to);
  2561. inline_summary_alloc ();
  2562. info = inline_summaries->get (node);
  2563. reset_inline_summary (node, info);
  2564. /* FIXME: Thunks are inlinable, but tree-inline don't know how to do that.
  2565. Once this happen, we will need to more curefully predict call
  2566. statement size. */
  2567. if (node->thunk.thunk_p)
  2568. {
  2569. struct inline_edge_summary *es = inline_edge_summary (node->callees);
  2570. struct predicate t = true_predicate ();
  2571. info->inlinable = 0;
  2572. node->callees->call_stmt_cannot_inline_p = true;
  2573. node->local.can_change_signature = false;
  2574. es->call_stmt_time = 1;
  2575. es->call_stmt_size = 1;
  2576. account_size_time (info, 0, 0, &t);
  2577. return;
  2578. }
  2579. /* Even is_gimple_min_invariant rely on current_function_decl. */
  2580. push_cfun (DECL_STRUCT_FUNCTION (node->decl));
  2581. /* Estimate the stack size for the function if we're optimizing. */
  2582. self_stack_size = optimize ? estimated_stack_frame_size (node) : 0;
  2583. info->estimated_self_stack_size = self_stack_size;
  2584. info->estimated_stack_size = self_stack_size;
  2585. info->stack_frame_offset = 0;
  2586. /* Can this function be inlined at all? */
  2587. if (!opt_for_fn (node->decl, optimize)
  2588. && !lookup_attribute ("always_inline",
  2589. DECL_ATTRIBUTES (node->decl)))
  2590. info->inlinable = false;
  2591. else
  2592. info->inlinable = tree_inlinable_function_p (node->decl);
  2593. info->contains_cilk_spawn = fn_contains_cilk_spawn_p (cfun);
  2594. /* Type attributes can use parameter indices to describe them. */
  2595. if (TYPE_ATTRIBUTES (TREE_TYPE (node->decl)))
  2596. node->local.can_change_signature = false;
  2597. else
  2598. {
  2599. /* Otherwise, inlinable functions always can change signature. */
  2600. if (info->inlinable)
  2601. node->local.can_change_signature = true;
  2602. else
  2603. {
  2604. /* Functions calling builtin_apply can not change signature. */
  2605. for (e = node->callees; e; e = e->next_callee)
  2606. {
  2607. tree cdecl = e->callee->decl;
  2608. if (DECL_BUILT_IN (cdecl)
  2609. && DECL_BUILT_IN_CLASS (cdecl) == BUILT_IN_NORMAL
  2610. && (DECL_FUNCTION_CODE (cdecl) == BUILT_IN_APPLY_ARGS
  2611. || DECL_FUNCTION_CODE (cdecl) == BUILT_IN_VA_START))
  2612. break;
  2613. }
  2614. node->local.can_change_signature = !e;
  2615. }
  2616. }
  2617. estimate_function_body_sizes (node, early);
  2618. for (e = node->callees; e; e = e->next_callee)
  2619. if (e->callee->comdat_local_p ())
  2620. break;
  2621. node->calls_comdat_local = (e != NULL);
  2622. /* Inlining characteristics are maintained by the cgraph_mark_inline. */
  2623. info->time = info->self_time;
  2624. info->size = info->self_size;
  2625. info->stack_frame_offset = 0;
  2626. info->estimated_stack_size = info->estimated_self_stack_size;
  2627. #ifdef ENABLE_CHECKING
  2628. inline_update_overall_summary (node);
  2629. gcc_assert (info->time == info->self_time && info->size == info->self_size);
  2630. #endif
  2631. pop_cfun ();
  2632. }
  2633. /* Compute parameters of functions used by inliner using
  2634. current_function_decl. */
  2635. static unsigned int
  2636. compute_inline_parameters_for_current (void)
  2637. {
  2638. compute_inline_parameters (cgraph_node::get (current_function_decl), true);
  2639. return 0;
  2640. }
  2641. namespace {
  2642. const pass_data pass_data_inline_parameters =
  2643. {
  2644. GIMPLE_PASS, /* type */
  2645. "inline_param", /* name */
  2646. OPTGROUP_INLINE, /* optinfo_flags */
  2647. TV_INLINE_PARAMETERS, /* tv_id */
  2648. 0, /* properties_required */
  2649. 0, /* properties_provided */
  2650. 0, /* properties_destroyed */
  2651. 0, /* todo_flags_start */
  2652. 0, /* todo_flags_finish */
  2653. };
  2654. class pass_inline_parameters : public gimple_opt_pass
  2655. {
  2656. public:
  2657. pass_inline_parameters (gcc::context *ctxt)
  2658. : gimple_opt_pass (pass_data_inline_parameters, ctxt)
  2659. {}
  2660. /* opt_pass methods: */
  2661. opt_pass * clone () { return new pass_inline_parameters (m_ctxt); }
  2662. virtual unsigned int execute (function *)
  2663. {
  2664. return compute_inline_parameters_for_current ();
  2665. }
  2666. }; // class pass_inline_parameters
  2667. } // anon namespace
  2668. gimple_opt_pass *
  2669. make_pass_inline_parameters (gcc::context *ctxt)
  2670. {
  2671. return new pass_inline_parameters (ctxt);
  2672. }
  2673. /* Estimate benefit devirtualizing indirect edge IE, provided KNOWN_VALS,
  2674. KNOWN_CONTEXTS and KNOWN_AGGS. */
  2675. static bool
  2676. estimate_edge_devirt_benefit (struct cgraph_edge *ie,
  2677. int *size, int *time,
  2678. vec<tree> known_vals,
  2679. vec<ipa_polymorphic_call_context> known_contexts,
  2680. vec<ipa_agg_jump_function_p> known_aggs)
  2681. {
  2682. tree target;
  2683. struct cgraph_node *callee;
  2684. struct inline_summary *isummary;
  2685. enum availability avail;
  2686. bool speculative;
  2687. if (!known_vals.exists () && !known_contexts.exists ())
  2688. return false;
  2689. if (!opt_for_fn (ie->caller->decl, flag_indirect_inlining))
  2690. return false;
  2691. target = ipa_get_indirect_edge_target (ie, known_vals, known_contexts,
  2692. known_aggs, &speculative);
  2693. if (!target || speculative)
  2694. return false;
  2695. /* Account for difference in cost between indirect and direct calls. */
  2696. *size -= (eni_size_weights.indirect_call_cost - eni_size_weights.call_cost);
  2697. *time -= (eni_time_weights.indirect_call_cost - eni_time_weights.call_cost);
  2698. gcc_checking_assert (*time >= 0);
  2699. gcc_checking_assert (*size >= 0);
  2700. callee = cgraph_node::get (target);
  2701. if (!callee || !callee->definition)
  2702. return false;
  2703. callee = callee->function_symbol (&avail);
  2704. if (avail < AVAIL_AVAILABLE)
  2705. return false;
  2706. isummary = inline_summaries->get (callee);
  2707. return isummary->inlinable;
  2708. }
  2709. /* Increase SIZE, MIN_SIZE (if non-NULL) and TIME for size and time needed to
  2710. handle edge E with probability PROB.
  2711. Set HINTS if edge may be devirtualized.
  2712. KNOWN_VALS, KNOWN_AGGS and KNOWN_CONTEXTS describe context of the call
  2713. site. */
  2714. static inline void
  2715. estimate_edge_size_and_time (struct cgraph_edge *e, int *size, int *min_size,
  2716. int *time,
  2717. int prob,
  2718. vec<tree> known_vals,
  2719. vec<ipa_polymorphic_call_context> known_contexts,
  2720. vec<ipa_agg_jump_function_p> known_aggs,
  2721. inline_hints *hints)
  2722. {
  2723. struct inline_edge_summary *es = inline_edge_summary (e);
  2724. int call_size = es->call_stmt_size;
  2725. int call_time = es->call_stmt_time;
  2726. int cur_size;
  2727. if (!e->callee
  2728. && estimate_edge_devirt_benefit (e, &call_size, &call_time,
  2729. known_vals, known_contexts, known_aggs)
  2730. && hints && e->maybe_hot_p ())
  2731. *hints |= INLINE_HINT_indirect_call;
  2732. cur_size = call_size * INLINE_SIZE_SCALE;
  2733. *size += cur_size;
  2734. if (min_size)
  2735. *min_size += cur_size;
  2736. *time += apply_probability ((gcov_type) call_time, prob)
  2737. * e->frequency * (INLINE_TIME_SCALE / CGRAPH_FREQ_BASE);
  2738. if (*time > MAX_TIME * INLINE_TIME_SCALE)
  2739. *time = MAX_TIME * INLINE_TIME_SCALE;
  2740. }
  2741. /* Increase SIZE, MIN_SIZE and TIME for size and time needed to handle all
  2742. calls in NODE. POSSIBLE_TRUTHS, KNOWN_VALS, KNOWN_AGGS and KNOWN_CONTEXTS
  2743. describe context of the call site. */
  2744. static void
  2745. estimate_calls_size_and_time (struct cgraph_node *node, int *size,
  2746. int *min_size, int *time,
  2747. inline_hints *hints,
  2748. clause_t possible_truths,
  2749. vec<tree> known_vals,
  2750. vec<ipa_polymorphic_call_context> known_contexts,
  2751. vec<ipa_agg_jump_function_p> known_aggs)
  2752. {
  2753. struct cgraph_edge *e;
  2754. for (e = node->callees; e; e = e->next_callee)
  2755. {
  2756. struct inline_edge_summary *es = inline_edge_summary (e);
  2757. /* Do not care about zero sized builtins. */
  2758. if (e->inline_failed && !es->call_stmt_size)
  2759. {
  2760. gcc_checking_assert (!es->call_stmt_time);
  2761. continue;
  2762. }
  2763. if (!es->predicate
  2764. || evaluate_predicate (es->predicate, possible_truths))
  2765. {
  2766. if (e->inline_failed)
  2767. {
  2768. /* Predicates of calls shall not use NOT_CHANGED codes,
  2769. sowe do not need to compute probabilities. */
  2770. estimate_edge_size_and_time (e, size,
  2771. es->predicate ? NULL : min_size,
  2772. time, REG_BR_PROB_BASE,
  2773. known_vals, known_contexts,
  2774. known_aggs, hints);
  2775. }
  2776. else
  2777. estimate_calls_size_and_time (e->callee, size, min_size, time,
  2778. hints,
  2779. possible_truths,
  2780. known_vals, known_contexts,
  2781. known_aggs);
  2782. }
  2783. }
  2784. for (e = node->indirect_calls; e; e = e->next_callee)
  2785. {
  2786. struct inline_edge_summary *es = inline_edge_summary (e);
  2787. if (!es->predicate
  2788. || evaluate_predicate (es->predicate, possible_truths))
  2789. estimate_edge_size_and_time (e, size,
  2790. es->predicate ? NULL : min_size,
  2791. time, REG_BR_PROB_BASE,
  2792. known_vals, known_contexts, known_aggs,
  2793. hints);
  2794. }
  2795. }
  2796. /* Estimate size and time needed to execute NODE assuming
  2797. POSSIBLE_TRUTHS clause, and KNOWN_VALS, KNOWN_AGGS and KNOWN_CONTEXTS
  2798. information about NODE's arguments. If non-NULL use also probability
  2799. information present in INLINE_PARAM_SUMMARY vector.
  2800. Additionally detemine hints determined by the context. Finally compute
  2801. minimal size needed for the call that is independent on the call context and
  2802. can be used for fast estimates. Return the values in RET_SIZE,
  2803. RET_MIN_SIZE, RET_TIME and RET_HINTS. */
  2804. static void
  2805. estimate_node_size_and_time (struct cgraph_node *node,
  2806. clause_t possible_truths,
  2807. vec<tree> known_vals,
  2808. vec<ipa_polymorphic_call_context> known_contexts,
  2809. vec<ipa_agg_jump_function_p> known_aggs,
  2810. int *ret_size, int *ret_min_size, int *ret_time,
  2811. inline_hints *ret_hints,
  2812. vec<inline_param_summary>
  2813. inline_param_summary)
  2814. {
  2815. struct inline_summary *info = inline_summaries->get (node);
  2816. size_time_entry *e;
  2817. int size = 0;
  2818. int time = 0;
  2819. int min_size = 0;
  2820. inline_hints hints = 0;
  2821. int i;
  2822. if (dump_file && (dump_flags & TDF_DETAILS))
  2823. {
  2824. bool found = false;
  2825. fprintf (dump_file, " Estimating body: %s/%i\n"
  2826. " Known to be false: ", node->name (),
  2827. node->order);
  2828. for (i = predicate_not_inlined_condition;
  2829. i < (predicate_first_dynamic_condition
  2830. + (int) vec_safe_length (info->conds)); i++)
  2831. if (!(possible_truths & (1 << i)))
  2832. {
  2833. if (found)
  2834. fprintf (dump_file, ", ");
  2835. found = true;
  2836. dump_condition (dump_file, info->conds, i);
  2837. }
  2838. }
  2839. for (i = 0; vec_safe_iterate (info->entry, i, &e); i++)
  2840. if (evaluate_predicate (&e->predicate, possible_truths))
  2841. {
  2842. size += e->size;
  2843. gcc_checking_assert (e->time >= 0);
  2844. gcc_checking_assert (time >= 0);
  2845. if (!inline_param_summary.exists ())
  2846. time += e->time;
  2847. else
  2848. {
  2849. int prob = predicate_probability (info->conds,
  2850. &e->predicate,
  2851. possible_truths,
  2852. inline_param_summary);
  2853. gcc_checking_assert (prob >= 0);
  2854. gcc_checking_assert (prob <= REG_BR_PROB_BASE);
  2855. time += apply_probability ((gcov_type) e->time, prob);
  2856. }
  2857. if (time > MAX_TIME * INLINE_TIME_SCALE)
  2858. time = MAX_TIME * INLINE_TIME_SCALE;
  2859. gcc_checking_assert (time >= 0);
  2860. }
  2861. gcc_checking_assert (true_predicate_p (&(*info->entry)[0].predicate));
  2862. min_size = (*info->entry)[0].size;
  2863. gcc_checking_assert (size >= 0);
  2864. gcc_checking_assert (time >= 0);
  2865. if (info->loop_iterations
  2866. && !evaluate_predicate (info->loop_iterations, possible_truths))
  2867. hints |= INLINE_HINT_loop_iterations;
  2868. if (info->loop_stride
  2869. && !evaluate_predicate (info->loop_stride, possible_truths))
  2870. hints |= INLINE_HINT_loop_stride;
  2871. if (info->array_index
  2872. && !evaluate_predicate (info->array_index, possible_truths))
  2873. hints |= INLINE_HINT_array_index;
  2874. if (info->scc_no)
  2875. hints |= INLINE_HINT_in_scc;
  2876. if (DECL_DECLARED_INLINE_P (node->decl))
  2877. hints |= INLINE_HINT_declared_inline;
  2878. estimate_calls_size_and_time (node, &size, &min_size, &time, &hints, possible_truths,
  2879. known_vals, known_contexts, known_aggs);
  2880. gcc_checking_assert (size >= 0);
  2881. gcc_checking_assert (time >= 0);
  2882. time = RDIV (time, INLINE_TIME_SCALE);
  2883. size = RDIV (size, INLINE_SIZE_SCALE);
  2884. min_size = RDIV (min_size, INLINE_SIZE_SCALE);
  2885. if (dump_file && (dump_flags & TDF_DETAILS))
  2886. fprintf (dump_file, "\n size:%i time:%i\n", (int) size, (int) time);
  2887. if (ret_time)
  2888. *ret_time = time;
  2889. if (ret_size)
  2890. *ret_size = size;
  2891. if (ret_min_size)
  2892. *ret_min_size = min_size;
  2893. if (ret_hints)
  2894. *ret_hints = hints;
  2895. return;
  2896. }
  2897. /* Estimate size and time needed to execute callee of EDGE assuming that
  2898. parameters known to be constant at caller of EDGE are propagated.
  2899. KNOWN_VALS and KNOWN_CONTEXTS are vectors of assumed known constant values
  2900. and types for parameters. */
  2901. void
  2902. estimate_ipcp_clone_size_and_time (struct cgraph_node *node,
  2903. vec<tree> known_vals,
  2904. vec<ipa_polymorphic_call_context>
  2905. known_contexts,
  2906. vec<ipa_agg_jump_function_p> known_aggs,
  2907. int *ret_size, int *ret_time,
  2908. inline_hints *hints)
  2909. {
  2910. clause_t clause;
  2911. clause = evaluate_conditions_for_known_args (node, false, known_vals,
  2912. known_aggs);
  2913. estimate_node_size_and_time (node, clause, known_vals, known_contexts,
  2914. known_aggs, ret_size, NULL, ret_time, hints, vNULL);
  2915. }
  2916. /* Translate all conditions from callee representation into caller
  2917. representation and symbolically evaluate predicate P into new predicate.
  2918. INFO is inline_summary of function we are adding predicate into, CALLEE_INFO
  2919. is summary of function predicate P is from. OPERAND_MAP is array giving
  2920. callee formal IDs the caller formal IDs. POSSSIBLE_TRUTHS is clausule of all
  2921. callee conditions that may be true in caller context. TOPLEV_PREDICATE is
  2922. predicate under which callee is executed. OFFSET_MAP is an array of of
  2923. offsets that need to be added to conditions, negative offset means that
  2924. conditions relying on values passed by reference have to be discarded
  2925. because they might not be preserved (and should be considered offset zero
  2926. for other purposes). */
  2927. static struct predicate
  2928. remap_predicate (struct inline_summary *info,
  2929. struct inline_summary *callee_info,
  2930. struct predicate *p,
  2931. vec<int> operand_map,
  2932. vec<int> offset_map,
  2933. clause_t possible_truths, struct predicate *toplev_predicate)
  2934. {
  2935. int i;
  2936. struct predicate out = true_predicate ();
  2937. /* True predicate is easy. */
  2938. if (true_predicate_p (p))
  2939. return *toplev_predicate;
  2940. for (i = 0; p->clause[i]; i++)
  2941. {
  2942. clause_t clause = p->clause[i];
  2943. int cond;
  2944. struct predicate clause_predicate = false_predicate ();
  2945. gcc_assert (i < MAX_CLAUSES);
  2946. for (cond = 0; cond < NUM_CONDITIONS; cond++)
  2947. /* Do we have condition we can't disprove? */
  2948. if (clause & possible_truths & (1 << cond))
  2949. {
  2950. struct predicate cond_predicate;
  2951. /* Work out if the condition can translate to predicate in the
  2952. inlined function. */
  2953. if (cond >= predicate_first_dynamic_condition)
  2954. {
  2955. struct condition *c;
  2956. c = &(*callee_info->conds)[cond
  2957. -
  2958. predicate_first_dynamic_condition];
  2959. /* See if we can remap condition operand to caller's operand.
  2960. Otherwise give up. */
  2961. if (!operand_map.exists ()
  2962. || (int) operand_map.length () <= c->operand_num
  2963. || operand_map[c->operand_num] == -1
  2964. /* TODO: For non-aggregate conditions, adding an offset is
  2965. basically an arithmetic jump function processing which
  2966. we should support in future. */
  2967. || ((!c->agg_contents || !c->by_ref)
  2968. && offset_map[c->operand_num] > 0)
  2969. || (c->agg_contents && c->by_ref
  2970. && offset_map[c->operand_num] < 0))
  2971. cond_predicate = true_predicate ();
  2972. else
  2973. {
  2974. struct agg_position_info ap;
  2975. HOST_WIDE_INT offset_delta = offset_map[c->operand_num];
  2976. if (offset_delta < 0)
  2977. {
  2978. gcc_checking_assert (!c->agg_contents || !c->by_ref);
  2979. offset_delta = 0;
  2980. }
  2981. gcc_assert (!c->agg_contents
  2982. || c->by_ref || offset_delta == 0);
  2983. ap.offset = c->offset + offset_delta;
  2984. ap.agg_contents = c->agg_contents;
  2985. ap.by_ref = c->by_ref;
  2986. cond_predicate = add_condition (info,
  2987. operand_map[c->operand_num],
  2988. &ap, c->code, c->val);
  2989. }
  2990. }
  2991. /* Fixed conditions remains same, construct single
  2992. condition predicate. */
  2993. else
  2994. {
  2995. cond_predicate.clause[0] = 1 << cond;
  2996. cond_predicate.clause[1] = 0;
  2997. }
  2998. clause_predicate = or_predicates (info->conds, &clause_predicate,
  2999. &cond_predicate);
  3000. }
  3001. out = and_predicates (info->conds, &out, &clause_predicate);
  3002. }
  3003. return and_predicates (info->conds, &out, toplev_predicate);
  3004. }
  3005. /* Update summary information of inline clones after inlining.
  3006. Compute peak stack usage. */
  3007. static void
  3008. inline_update_callee_summaries (struct cgraph_node *node, int depth)
  3009. {
  3010. struct cgraph_edge *e;
  3011. struct inline_summary *callee_info = inline_summaries->get (node);
  3012. struct inline_summary *caller_info = inline_summaries->get (node->callers->caller);
  3013. HOST_WIDE_INT peak;
  3014. callee_info->stack_frame_offset
  3015. = caller_info->stack_frame_offset
  3016. + caller_info->estimated_self_stack_size;
  3017. peak = callee_info->stack_frame_offset
  3018. + callee_info->estimated_self_stack_size;
  3019. if (inline_summaries->get (node->global.inlined_to)->estimated_stack_size < peak)
  3020. inline_summaries->get (node->global.inlined_to)->estimated_stack_size = peak;
  3021. ipa_propagate_frequency (node);
  3022. for (e = node->callees; e; e = e->next_callee)
  3023. {
  3024. if (!e->inline_failed)
  3025. inline_update_callee_summaries (e->callee, depth);
  3026. inline_edge_summary (e)->loop_depth += depth;
  3027. }
  3028. for (e = node->indirect_calls; e; e = e->next_callee)
  3029. inline_edge_summary (e)->loop_depth += depth;
  3030. }
  3031. /* Update change_prob of EDGE after INLINED_EDGE has been inlined.
  3032. When functoin A is inlined in B and A calls C with parameter that
  3033. changes with probability PROB1 and C is known to be passthroug
  3034. of argument if B that change with probability PROB2, the probability
  3035. of change is now PROB1*PROB2. */
  3036. static void
  3037. remap_edge_change_prob (struct cgraph_edge *inlined_edge,
  3038. struct cgraph_edge *edge)
  3039. {
  3040. if (ipa_node_params_sum)
  3041. {
  3042. int i;
  3043. struct ipa_edge_args *args = IPA_EDGE_REF (edge);
  3044. struct inline_edge_summary *es = inline_edge_summary (edge);
  3045. struct inline_edge_summary *inlined_es
  3046. = inline_edge_summary (inlined_edge);
  3047. for (i = 0; i < ipa_get_cs_argument_count (args); i++)
  3048. {
  3049. struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
  3050. if (jfunc->type == IPA_JF_PASS_THROUGH
  3051. && (ipa_get_jf_pass_through_formal_id (jfunc)
  3052. < (int) inlined_es->param.length ()))
  3053. {
  3054. int jf_formal_id = ipa_get_jf_pass_through_formal_id (jfunc);
  3055. int prob1 = es->param[i].change_prob;
  3056. int prob2 = inlined_es->param[jf_formal_id].change_prob;
  3057. int prob = combine_probabilities (prob1, prob2);
  3058. if (prob1 && prob2 && !prob)
  3059. prob = 1;
  3060. es->param[i].change_prob = prob;
  3061. }
  3062. }
  3063. }
  3064. }
  3065. /* Update edge summaries of NODE after INLINED_EDGE has been inlined.
  3066. Remap predicates of callees of NODE. Rest of arguments match
  3067. remap_predicate.
  3068. Also update change probabilities. */
  3069. static void
  3070. remap_edge_summaries (struct cgraph_edge *inlined_edge,
  3071. struct cgraph_node *node,
  3072. struct inline_summary *info,
  3073. struct inline_summary *callee_info,
  3074. vec<int> operand_map,
  3075. vec<int> offset_map,
  3076. clause_t possible_truths,
  3077. struct predicate *toplev_predicate)
  3078. {
  3079. struct cgraph_edge *e, *next;
  3080. for (e = node->callees; e; e = next)
  3081. {
  3082. struct inline_edge_summary *es = inline_edge_summary (e);
  3083. struct predicate p;
  3084. next = e->next_callee;
  3085. if (e->inline_failed)
  3086. {
  3087. remap_edge_change_prob (inlined_edge, e);
  3088. if (es->predicate)
  3089. {
  3090. p = remap_predicate (info, callee_info,
  3091. es->predicate, operand_map, offset_map,
  3092. possible_truths, toplev_predicate);
  3093. edge_set_predicate (e, &p);
  3094. }
  3095. else
  3096. edge_set_predicate (e, toplev_predicate);
  3097. }
  3098. else
  3099. remap_edge_summaries (inlined_edge, e->callee, info, callee_info,
  3100. operand_map, offset_map, possible_truths,
  3101. toplev_predicate);
  3102. }
  3103. for (e = node->indirect_calls; e; e = next)
  3104. {
  3105. struct inline_edge_summary *es = inline_edge_summary (e);
  3106. struct predicate p;
  3107. next = e->next_callee;
  3108. remap_edge_change_prob (inlined_edge, e);
  3109. if (es->predicate)
  3110. {
  3111. p = remap_predicate (info, callee_info,
  3112. es->predicate, operand_map, offset_map,
  3113. possible_truths, toplev_predicate);
  3114. edge_set_predicate (e, &p);
  3115. }
  3116. else
  3117. edge_set_predicate (e, toplev_predicate);
  3118. }
  3119. }
  3120. /* Same as remap_predicate, but set result into hint *HINT. */
  3121. static void
  3122. remap_hint_predicate (struct inline_summary *info,
  3123. struct inline_summary *callee_info,
  3124. struct predicate **hint,
  3125. vec<int> operand_map,
  3126. vec<int> offset_map,
  3127. clause_t possible_truths,
  3128. struct predicate *toplev_predicate)
  3129. {
  3130. predicate p;
  3131. if (!*hint)
  3132. return;
  3133. p = remap_predicate (info, callee_info,
  3134. *hint,
  3135. operand_map, offset_map,
  3136. possible_truths, toplev_predicate);
  3137. if (!false_predicate_p (&p) && !true_predicate_p (&p))
  3138. {
  3139. if (!*hint)
  3140. set_hint_predicate (hint, p);
  3141. else
  3142. **hint = and_predicates (info->conds, *hint, &p);
  3143. }
  3144. }
  3145. /* We inlined EDGE. Update summary of the function we inlined into. */
  3146. void
  3147. inline_merge_summary (struct cgraph_edge *edge)
  3148. {
  3149. struct inline_summary *callee_info = inline_summaries->get (edge->callee);
  3150. struct cgraph_node *to = (edge->caller->global.inlined_to
  3151. ? edge->caller->global.inlined_to : edge->caller);
  3152. struct inline_summary *info = inline_summaries->get (to);
  3153. clause_t clause = 0; /* not_inline is known to be false. */
  3154. size_time_entry *e;
  3155. vec<int> operand_map = vNULL;
  3156. vec<int> offset_map = vNULL;
  3157. int i;
  3158. struct predicate toplev_predicate;
  3159. struct predicate true_p = true_predicate ();
  3160. struct inline_edge_summary *es = inline_edge_summary (edge);
  3161. if (es->predicate)
  3162. toplev_predicate = *es->predicate;
  3163. else
  3164. toplev_predicate = true_predicate ();
  3165. if (callee_info->conds)
  3166. evaluate_properties_for_edge (edge, true, &clause, NULL, NULL, NULL);
  3167. if (ipa_node_params_sum && callee_info->conds)
  3168. {
  3169. struct ipa_edge_args *args = IPA_EDGE_REF (edge);
  3170. int count = ipa_get_cs_argument_count (args);
  3171. int i;
  3172. if (count)
  3173. {
  3174. operand_map.safe_grow_cleared (count);
  3175. offset_map.safe_grow_cleared (count);
  3176. }
  3177. for (i = 0; i < count; i++)
  3178. {
  3179. struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
  3180. int map = -1;
  3181. /* TODO: handle non-NOPs when merging. */
  3182. if (jfunc->type == IPA_JF_PASS_THROUGH)
  3183. {
  3184. if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
  3185. map = ipa_get_jf_pass_through_formal_id (jfunc);
  3186. if (!ipa_get_jf_pass_through_agg_preserved (jfunc))
  3187. offset_map[i] = -1;
  3188. }
  3189. else if (jfunc->type == IPA_JF_ANCESTOR)
  3190. {
  3191. HOST_WIDE_INT offset = ipa_get_jf_ancestor_offset (jfunc);
  3192. if (offset >= 0 && offset < INT_MAX)
  3193. {
  3194. map = ipa_get_jf_ancestor_formal_id (jfunc);
  3195. if (!ipa_get_jf_ancestor_agg_preserved (jfunc))
  3196. offset = -1;
  3197. offset_map[i] = offset;
  3198. }
  3199. }
  3200. operand_map[i] = map;
  3201. gcc_assert (map < ipa_get_param_count (IPA_NODE_REF (to)));
  3202. }
  3203. }
  3204. for (i = 0; vec_safe_iterate (callee_info->entry, i, &e); i++)
  3205. {
  3206. struct predicate p = remap_predicate (info, callee_info,
  3207. &e->predicate, operand_map,
  3208. offset_map, clause,
  3209. &toplev_predicate);
  3210. if (!false_predicate_p (&p))
  3211. {
  3212. gcov_type add_time = ((gcov_type) e->time * edge->frequency
  3213. + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
  3214. int prob = predicate_probability (callee_info->conds,
  3215. &e->predicate,
  3216. clause, es->param);
  3217. add_time = apply_probability ((gcov_type) add_time, prob);
  3218. if (add_time > MAX_TIME * INLINE_TIME_SCALE)
  3219. add_time = MAX_TIME * INLINE_TIME_SCALE;
  3220. if (prob != REG_BR_PROB_BASE
  3221. && dump_file && (dump_flags & TDF_DETAILS))
  3222. {
  3223. fprintf (dump_file, "\t\tScaling time by probability:%f\n",
  3224. (double) prob / REG_BR_PROB_BASE);
  3225. }
  3226. account_size_time (info, e->size, add_time, &p);
  3227. }
  3228. }
  3229. remap_edge_summaries (edge, edge->callee, info, callee_info, operand_map,
  3230. offset_map, clause, &toplev_predicate);
  3231. remap_hint_predicate (info, callee_info,
  3232. &callee_info->loop_iterations,
  3233. operand_map, offset_map, clause, &toplev_predicate);
  3234. remap_hint_predicate (info, callee_info,
  3235. &callee_info->loop_stride,
  3236. operand_map, offset_map, clause, &toplev_predicate);
  3237. remap_hint_predicate (info, callee_info,
  3238. &callee_info->array_index,
  3239. operand_map, offset_map, clause, &toplev_predicate);
  3240. inline_update_callee_summaries (edge->callee,
  3241. inline_edge_summary (edge)->loop_depth);
  3242. /* We do not maintain predicates of inlined edges, free it. */
  3243. edge_set_predicate (edge, &true_p);
  3244. /* Similarly remove param summaries. */
  3245. es->param.release ();
  3246. operand_map.release ();
  3247. offset_map.release ();
  3248. }
  3249. /* For performance reasons inline_merge_summary is not updating overall size
  3250. and time. Recompute it. */
  3251. void
  3252. inline_update_overall_summary (struct cgraph_node *node)
  3253. {
  3254. struct inline_summary *info = inline_summaries->get (node);
  3255. size_time_entry *e;
  3256. int i;
  3257. info->size = 0;
  3258. info->time = 0;
  3259. for (i = 0; vec_safe_iterate (info->entry, i, &e); i++)
  3260. {
  3261. info->size += e->size, info->time += e->time;
  3262. if (info->time > MAX_TIME * INLINE_TIME_SCALE)
  3263. info->time = MAX_TIME * INLINE_TIME_SCALE;
  3264. }
  3265. estimate_calls_size_and_time (node, &info->size, &info->min_size,
  3266. &info->time, NULL,
  3267. ~(clause_t) (1 << predicate_false_condition),
  3268. vNULL, vNULL, vNULL);
  3269. info->time = (info->time + INLINE_TIME_SCALE / 2) / INLINE_TIME_SCALE;
  3270. info->size = (info->size + INLINE_SIZE_SCALE / 2) / INLINE_SIZE_SCALE;
  3271. }
  3272. /* Return hints derrived from EDGE. */
  3273. int
  3274. simple_edge_hints (struct cgraph_edge *edge)
  3275. {
  3276. int hints = 0;
  3277. struct cgraph_node *to = (edge->caller->global.inlined_to
  3278. ? edge->caller->global.inlined_to : edge->caller);
  3279. struct cgraph_node *callee = edge->callee->ultimate_alias_target ();
  3280. if (inline_summaries->get (to)->scc_no
  3281. && inline_summaries->get (to)->scc_no
  3282. == inline_summaries->get (callee)->scc_no
  3283. && !edge->recursive_p ())
  3284. hints |= INLINE_HINT_same_scc;
  3285. if (callee->lto_file_data && edge->caller->lto_file_data
  3286. && edge->caller->lto_file_data != callee->lto_file_data
  3287. && !callee->merged)
  3288. hints |= INLINE_HINT_cross_module;
  3289. return hints;
  3290. }
  3291. /* Estimate the time cost for the caller when inlining EDGE.
  3292. Only to be called via estimate_edge_time, that handles the
  3293. caching mechanism.
  3294. When caching, also update the cache entry. Compute both time and
  3295. size, since we always need both metrics eventually. */
  3296. int
  3297. do_estimate_edge_time (struct cgraph_edge *edge)
  3298. {
  3299. int time;
  3300. int size;
  3301. inline_hints hints;
  3302. struct cgraph_node *callee;
  3303. clause_t clause;
  3304. vec<tree> known_vals;
  3305. vec<ipa_polymorphic_call_context> known_contexts;
  3306. vec<ipa_agg_jump_function_p> known_aggs;
  3307. struct inline_edge_summary *es = inline_edge_summary (edge);
  3308. int min_size;
  3309. callee = edge->callee->ultimate_alias_target ();
  3310. gcc_checking_assert (edge->inline_failed);
  3311. evaluate_properties_for_edge (edge, true,
  3312. &clause, &known_vals, &known_contexts,
  3313. &known_aggs);
  3314. estimate_node_size_and_time (callee, clause, known_vals, known_contexts,
  3315. known_aggs, &size, &min_size, &time, &hints, es->param);
  3316. /* When we have profile feedback, we can quite safely identify hot
  3317. edges and for those we disable size limits. Don't do that when
  3318. probability that caller will call the callee is low however, since it
  3319. may hurt optimization of the caller's hot path. */
  3320. if (edge->count && edge->maybe_hot_p ()
  3321. && (edge->count * 2
  3322. > (edge->caller->global.inlined_to
  3323. ? edge->caller->global.inlined_to->count : edge->caller->count)))
  3324. hints |= INLINE_HINT_known_hot;
  3325. known_vals.release ();
  3326. known_contexts.release ();
  3327. known_aggs.release ();
  3328. gcc_checking_assert (size >= 0);
  3329. gcc_checking_assert (time >= 0);
  3330. /* When caching, update the cache entry. */
  3331. if (edge_growth_cache.exists ())
  3332. {
  3333. inline_summaries->get (edge->callee)->min_size = min_size;
  3334. if ((int) edge_growth_cache.length () <= edge->uid)
  3335. edge_growth_cache.safe_grow_cleared (symtab->edges_max_uid);
  3336. edge_growth_cache[edge->uid].time = time + (time >= 0);
  3337. edge_growth_cache[edge->uid].size = size + (size >= 0);
  3338. hints |= simple_edge_hints (edge);
  3339. edge_growth_cache[edge->uid].hints = hints + 1;
  3340. }
  3341. return time;
  3342. }
  3343. /* Return estimated callee growth after inlining EDGE.
  3344. Only to be called via estimate_edge_size. */
  3345. int
  3346. do_estimate_edge_size (struct cgraph_edge *edge)
  3347. {
  3348. int size;
  3349. struct cgraph_node *callee;
  3350. clause_t clause;
  3351. vec<tree> known_vals;
  3352. vec<ipa_polymorphic_call_context> known_contexts;
  3353. vec<ipa_agg_jump_function_p> known_aggs;
  3354. /* When we do caching, use do_estimate_edge_time to populate the entry. */
  3355. if (edge_growth_cache.exists ())
  3356. {
  3357. do_estimate_edge_time (edge);
  3358. size = edge_growth_cache[edge->uid].size;
  3359. gcc_checking_assert (size);
  3360. return size - (size > 0);
  3361. }
  3362. callee = edge->callee->ultimate_alias_target ();
  3363. /* Early inliner runs without caching, go ahead and do the dirty work. */
  3364. gcc_checking_assert (edge->inline_failed);
  3365. evaluate_properties_for_edge (edge, true,
  3366. &clause, &known_vals, &known_contexts,
  3367. &known_aggs);
  3368. estimate_node_size_and_time (callee, clause, known_vals, known_contexts,
  3369. known_aggs, &size, NULL, NULL, NULL, vNULL);
  3370. known_vals.release ();
  3371. known_contexts.release ();
  3372. known_aggs.release ();
  3373. return size;
  3374. }
  3375. /* Estimate the growth of the caller when inlining EDGE.
  3376. Only to be called via estimate_edge_size. */
  3377. inline_hints
  3378. do_estimate_edge_hints (struct cgraph_edge *edge)
  3379. {
  3380. inline_hints hints;
  3381. struct cgraph_node *callee;
  3382. clause_t clause;
  3383. vec<tree> known_vals;
  3384. vec<ipa_polymorphic_call_context> known_contexts;
  3385. vec<ipa_agg_jump_function_p> known_aggs;
  3386. /* When we do caching, use do_estimate_edge_time to populate the entry. */
  3387. if (edge_growth_cache.exists ())
  3388. {
  3389. do_estimate_edge_time (edge);
  3390. hints = edge_growth_cache[edge->uid].hints;
  3391. gcc_checking_assert (hints);
  3392. return hints - 1;
  3393. }
  3394. callee = edge->callee->ultimate_alias_target ();
  3395. /* Early inliner runs without caching, go ahead and do the dirty work. */
  3396. gcc_checking_assert (edge->inline_failed);
  3397. evaluate_properties_for_edge (edge, true,
  3398. &clause, &known_vals, &known_contexts,
  3399. &known_aggs);
  3400. estimate_node_size_and_time (callee, clause, known_vals, known_contexts,
  3401. known_aggs, NULL, NULL, NULL, &hints, vNULL);
  3402. known_vals.release ();
  3403. known_contexts.release ();
  3404. known_aggs.release ();
  3405. hints |= simple_edge_hints (edge);
  3406. return hints;
  3407. }
  3408. /* Estimate self time of the function NODE after inlining EDGE. */
  3409. int
  3410. estimate_time_after_inlining (struct cgraph_node *node,
  3411. struct cgraph_edge *edge)
  3412. {
  3413. struct inline_edge_summary *es = inline_edge_summary (edge);
  3414. if (!es->predicate || !false_predicate_p (es->predicate))
  3415. {
  3416. gcov_type time =
  3417. inline_summaries->get (node)->time + estimate_edge_time (edge);
  3418. if (time < 0)
  3419. time = 0;
  3420. if (time > MAX_TIME)
  3421. time = MAX_TIME;
  3422. return time;
  3423. }
  3424. return inline_summaries->get (node)->time;
  3425. }
  3426. /* Estimate the size of NODE after inlining EDGE which should be an
  3427. edge to either NODE or a call inlined into NODE. */
  3428. int
  3429. estimate_size_after_inlining (struct cgraph_node *node,
  3430. struct cgraph_edge *edge)
  3431. {
  3432. struct inline_edge_summary *es = inline_edge_summary (edge);
  3433. if (!es->predicate || !false_predicate_p (es->predicate))
  3434. {
  3435. int size = inline_summaries->get (node)->size + estimate_edge_growth (edge);
  3436. gcc_assert (size >= 0);
  3437. return size;
  3438. }
  3439. return inline_summaries->get (node)->size;
  3440. }
  3441. struct growth_data
  3442. {
  3443. struct cgraph_node *node;
  3444. bool self_recursive;
  3445. bool uninlinable;
  3446. int growth;
  3447. };
  3448. /* Worker for do_estimate_growth. Collect growth for all callers. */
  3449. static bool
  3450. do_estimate_growth_1 (struct cgraph_node *node, void *data)
  3451. {
  3452. struct cgraph_edge *e;
  3453. struct growth_data *d = (struct growth_data *) data;
  3454. for (e = node->callers; e; e = e->next_caller)
  3455. {
  3456. gcc_checking_assert (e->inline_failed);
  3457. if (cgraph_inline_failed_type (e->inline_failed) == CIF_FINAL_ERROR)
  3458. {
  3459. d->uninlinable = true;
  3460. continue;
  3461. }
  3462. if (e->recursive_p ())
  3463. {
  3464. d->self_recursive = true;
  3465. continue;
  3466. }
  3467. d->growth += estimate_edge_growth (e);
  3468. }
  3469. return false;
  3470. }
  3471. /* Estimate the growth caused by inlining NODE into all callees. */
  3472. int
  3473. estimate_growth (struct cgraph_node *node)
  3474. {
  3475. struct growth_data d = { node, false, false, 0 };
  3476. struct inline_summary *info = inline_summaries->get (node);
  3477. node->call_for_symbol_and_aliases (do_estimate_growth_1, &d, true);
  3478. /* For self recursive functions the growth estimation really should be
  3479. infinity. We don't want to return very large values because the growth
  3480. plays various roles in badness computation fractions. Be sure to not
  3481. return zero or negative growths. */
  3482. if (d.self_recursive)
  3483. d.growth = d.growth < info->size ? info->size : d.growth;
  3484. else if (DECL_EXTERNAL (node->decl) || d.uninlinable)
  3485. ;
  3486. else
  3487. {
  3488. if (node->will_be_removed_from_program_if_no_direct_calls_p ())
  3489. d.growth -= info->size;
  3490. /* COMDAT functions are very often not shared across multiple units
  3491. since they come from various template instantiations.
  3492. Take this into account. */
  3493. else if (DECL_COMDAT (node->decl)
  3494. && node->can_remove_if_no_direct_calls_p ())
  3495. d.growth -= (info->size
  3496. * (100 - PARAM_VALUE (PARAM_COMDAT_SHARING_PROBABILITY))
  3497. + 50) / 100;
  3498. }
  3499. return d.growth;
  3500. }
  3501. /* Verify if there are fewer than MAX_CALLERS. */
  3502. static bool
  3503. check_callers (cgraph_node *node, int *max_callers)
  3504. {
  3505. ipa_ref *ref;
  3506. if (!node->can_remove_if_no_direct_calls_and_refs_p ())
  3507. return true;
  3508. for (cgraph_edge *e = node->callers; e; e = e->next_caller)
  3509. {
  3510. (*max_callers)--;
  3511. if (!*max_callers
  3512. || cgraph_inline_failed_type (e->inline_failed) == CIF_FINAL_ERROR)
  3513. return true;
  3514. }
  3515. FOR_EACH_ALIAS (node, ref)
  3516. if (check_callers (dyn_cast <cgraph_node *> (ref->referring), max_callers))
  3517. return true;
  3518. return false;
  3519. }
  3520. /* Make cheap estimation if growth of NODE is likely positive knowing
  3521. EDGE_GROWTH of one particular edge.
  3522. We assume that most of other edges will have similar growth
  3523. and skip computation if there are too many callers. */
  3524. bool
  3525. growth_likely_positive (struct cgraph_node *node,
  3526. int edge_growth)
  3527. {
  3528. int max_callers;
  3529. struct cgraph_edge *e;
  3530. gcc_checking_assert (edge_growth > 0);
  3531. /* First quickly check if NODE is removable at all. */
  3532. if (DECL_EXTERNAL (node->decl))
  3533. return true;
  3534. if (!node->can_remove_if_no_direct_calls_and_refs_p ()
  3535. || node->address_taken)
  3536. return true;
  3537. max_callers = inline_summaries->get (node)->size * 4 / edge_growth + 2;
  3538. for (e = node->callers; e; e = e->next_caller)
  3539. {
  3540. max_callers--;
  3541. if (!max_callers
  3542. || cgraph_inline_failed_type (e->inline_failed) == CIF_FINAL_ERROR)
  3543. return true;
  3544. }
  3545. ipa_ref *ref;
  3546. FOR_EACH_ALIAS (node, ref)
  3547. if (check_callers (dyn_cast <cgraph_node *> (ref->referring), &max_callers))
  3548. return true;
  3549. /* Unlike for functions called once, we play unsafe with
  3550. COMDATs. We can allow that since we know functions
  3551. in consideration are small (and thus risk is small) and
  3552. moreover grow estimates already accounts that COMDAT
  3553. functions may or may not disappear when eliminated from
  3554. current unit. With good probability making aggressive
  3555. choice in all units is going to make overall program
  3556. smaller. */
  3557. if (DECL_COMDAT (node->decl))
  3558. {
  3559. if (!node->can_remove_if_no_direct_calls_p ())
  3560. return true;
  3561. }
  3562. else if (!node->will_be_removed_from_program_if_no_direct_calls_p ())
  3563. return true;
  3564. return estimate_growth (node) > 0;
  3565. }
  3566. /* This function performs intraprocedural analysis in NODE that is required to
  3567. inline indirect calls. */
  3568. static void
  3569. inline_indirect_intraprocedural_analysis (struct cgraph_node *node)
  3570. {
  3571. ipa_analyze_node (node);
  3572. if (dump_file && (dump_flags & TDF_DETAILS))
  3573. {
  3574. ipa_print_node_params (dump_file, node);
  3575. ipa_print_node_jump_functions (dump_file, node);
  3576. }
  3577. }
  3578. /* Note function body size. */
  3579. void
  3580. inline_analyze_function (struct cgraph_node *node)
  3581. {
  3582. push_cfun (DECL_STRUCT_FUNCTION (node->decl));
  3583. if (dump_file)
  3584. fprintf (dump_file, "\nAnalyzing function: %s/%u\n",
  3585. node->name (), node->order);
  3586. if (opt_for_fn (node->decl, optimize) && !node->thunk.thunk_p)
  3587. inline_indirect_intraprocedural_analysis (node);
  3588. compute_inline_parameters (node, false);
  3589. if (!optimize)
  3590. {
  3591. struct cgraph_edge *e;
  3592. for (e = node->callees; e; e = e->next_callee)
  3593. {
  3594. if (e->inline_failed == CIF_FUNCTION_NOT_CONSIDERED)
  3595. e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED;
  3596. e->call_stmt_cannot_inline_p = true;
  3597. }
  3598. for (e = node->indirect_calls; e; e = e->next_callee)
  3599. {
  3600. if (e->inline_failed == CIF_FUNCTION_NOT_CONSIDERED)
  3601. e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED;
  3602. e->call_stmt_cannot_inline_p = true;
  3603. }
  3604. }
  3605. pop_cfun ();
  3606. }
  3607. /* Called when new function is inserted to callgraph late. */
  3608. void
  3609. inline_summary_t::insert (struct cgraph_node *node, inline_summary *)
  3610. {
  3611. inline_analyze_function (node);
  3612. }
  3613. /* Note function body size. */
  3614. void
  3615. inline_generate_summary (void)
  3616. {
  3617. struct cgraph_node *node;
  3618. /* When not optimizing, do not bother to analyze. Inlining is still done
  3619. because edge redirection needs to happen there. */
  3620. if (!optimize && !flag_generate_lto && !flag_generate_offload && !flag_wpa)
  3621. return;
  3622. if (!inline_summaries)
  3623. inline_summaries = (inline_summary_t*) inline_summary_t::create_ggc (symtab);
  3624. inline_summaries->enable_insertion_hook ();
  3625. ipa_register_cgraph_hooks ();
  3626. inline_free_summary ();
  3627. FOR_EACH_DEFINED_FUNCTION (node)
  3628. if (!node->alias)
  3629. inline_analyze_function (node);
  3630. }
  3631. /* Read predicate from IB. */
  3632. static struct predicate
  3633. read_predicate (struct lto_input_block *ib)
  3634. {
  3635. struct predicate out;
  3636. clause_t clause;
  3637. int k = 0;
  3638. do
  3639. {
  3640. gcc_assert (k <= MAX_CLAUSES);
  3641. clause = out.clause[k++] = streamer_read_uhwi (ib);
  3642. }
  3643. while (clause);
  3644. /* Zero-initialize the remaining clauses in OUT. */
  3645. while (k <= MAX_CLAUSES)
  3646. out.clause[k++] = 0;
  3647. return out;
  3648. }
  3649. /* Write inline summary for edge E to OB. */
  3650. static void
  3651. read_inline_edge_summary (struct lto_input_block *ib, struct cgraph_edge *e)
  3652. {
  3653. struct inline_edge_summary *es = inline_edge_summary (e);
  3654. struct predicate p;
  3655. int length, i;
  3656. es->call_stmt_size = streamer_read_uhwi (ib);
  3657. es->call_stmt_time = streamer_read_uhwi (ib);
  3658. es->loop_depth = streamer_read_uhwi (ib);
  3659. p = read_predicate (ib);
  3660. edge_set_predicate (e, &p);
  3661. length = streamer_read_uhwi (ib);
  3662. if (length)
  3663. {
  3664. es->param.safe_grow_cleared (length);
  3665. for (i = 0; i < length; i++)
  3666. es->param[i].change_prob = streamer_read_uhwi (ib);
  3667. }
  3668. }
  3669. /* Stream in inline summaries from the section. */
  3670. static void
  3671. inline_read_section (struct lto_file_decl_data *file_data, const char *data,
  3672. size_t len)
  3673. {
  3674. const struct lto_function_header *header =
  3675. (const struct lto_function_header *) data;
  3676. const int cfg_offset = sizeof (struct lto_function_header);
  3677. const int main_offset = cfg_offset + header->cfg_size;
  3678. const int string_offset = main_offset + header->main_size;
  3679. struct data_in *data_in;
  3680. unsigned int i, count2, j;
  3681. unsigned int f_count;
  3682. lto_input_block ib ((const char *) data + main_offset, header->main_size,
  3683. file_data->mode_table);
  3684. data_in =
  3685. lto_data_in_create (file_data, (const char *) data + string_offset,
  3686. header->string_size, vNULL);
  3687. f_count = streamer_read_uhwi (&ib);
  3688. for (i = 0; i < f_count; i++)
  3689. {
  3690. unsigned int index;
  3691. struct cgraph_node *node;
  3692. struct inline_summary *info;
  3693. lto_symtab_encoder_t encoder;
  3694. struct bitpack_d bp;
  3695. struct cgraph_edge *e;
  3696. predicate p;
  3697. index = streamer_read_uhwi (&ib);
  3698. encoder = file_data->symtab_node_encoder;
  3699. node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref (encoder,
  3700. index));
  3701. info = inline_summaries->get (node);
  3702. info->estimated_stack_size
  3703. = info->estimated_self_stack_size = streamer_read_uhwi (&ib);
  3704. info->size = info->self_size = streamer_read_uhwi (&ib);
  3705. info->time = info->self_time = streamer_read_uhwi (&ib);
  3706. bp = streamer_read_bitpack (&ib);
  3707. info->inlinable = bp_unpack_value (&bp, 1);
  3708. info->contains_cilk_spawn = bp_unpack_value (&bp, 1);
  3709. count2 = streamer_read_uhwi (&ib);
  3710. gcc_assert (!info->conds);
  3711. for (j = 0; j < count2; j++)
  3712. {
  3713. struct condition c;
  3714. c.operand_num = streamer_read_uhwi (&ib);
  3715. c.code = (enum tree_code) streamer_read_uhwi (&ib);
  3716. c.val = stream_read_tree (&ib, data_in);
  3717. bp = streamer_read_bitpack (&ib);
  3718. c.agg_contents = bp_unpack_value (&bp, 1);
  3719. c.by_ref = bp_unpack_value (&bp, 1);
  3720. if (c.agg_contents)
  3721. c.offset = streamer_read_uhwi (&ib);
  3722. vec_safe_push (info->conds, c);
  3723. }
  3724. count2 = streamer_read_uhwi (&ib);
  3725. gcc_assert (!info->entry);
  3726. for (j = 0; j < count2; j++)
  3727. {
  3728. struct size_time_entry e;
  3729. e.size = streamer_read_uhwi (&ib);
  3730. e.time = streamer_read_uhwi (&ib);
  3731. e.predicate = read_predicate (&ib);
  3732. vec_safe_push (info->entry, e);
  3733. }
  3734. p = read_predicate (&ib);
  3735. set_hint_predicate (&info->loop_iterations, p);
  3736. p = read_predicate (&ib);
  3737. set_hint_predicate (&info->loop_stride, p);
  3738. p = read_predicate (&ib);
  3739. set_hint_predicate (&info->array_index, p);
  3740. for (e = node->callees; e; e = e->next_callee)
  3741. read_inline_edge_summary (&ib, e);
  3742. for (e = node->indirect_calls; e; e = e->next_callee)
  3743. read_inline_edge_summary (&ib, e);
  3744. }
  3745. lto_free_section_data (file_data, LTO_section_inline_summary, NULL, data,
  3746. len);
  3747. lto_data_in_delete (data_in);
  3748. }
  3749. /* Read inline summary. Jump functions are shared among ipa-cp
  3750. and inliner, so when ipa-cp is active, we don't need to write them
  3751. twice. */
  3752. void
  3753. inline_read_summary (void)
  3754. {
  3755. struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
  3756. struct lto_file_decl_data *file_data;
  3757. unsigned int j = 0;
  3758. inline_summary_alloc ();
  3759. while ((file_data = file_data_vec[j++]))
  3760. {
  3761. size_t len;
  3762. const char *data = lto_get_section_data (file_data,
  3763. LTO_section_inline_summary,
  3764. NULL, &len);
  3765. if (data)
  3766. inline_read_section (file_data, data, len);
  3767. else
  3768. /* Fatal error here. We do not want to support compiling ltrans units
  3769. with different version of compiler or different flags than the WPA
  3770. unit, so this should never happen. */
  3771. fatal_error (input_location,
  3772. "ipa inline summary is missing in input file");
  3773. }
  3774. if (optimize)
  3775. {
  3776. ipa_register_cgraph_hooks ();
  3777. if (!flag_ipa_cp)
  3778. ipa_prop_read_jump_functions ();
  3779. }
  3780. gcc_assert (inline_summaries);
  3781. inline_summaries->enable_insertion_hook ();
  3782. }
  3783. /* Write predicate P to OB. */
  3784. static void
  3785. write_predicate (struct output_block *ob, struct predicate *p)
  3786. {
  3787. int j;
  3788. if (p)
  3789. for (j = 0; p->clause[j]; j++)
  3790. {
  3791. gcc_assert (j < MAX_CLAUSES);
  3792. streamer_write_uhwi (ob, p->clause[j]);
  3793. }
  3794. streamer_write_uhwi (ob, 0);
  3795. }
  3796. /* Write inline summary for edge E to OB. */
  3797. static void
  3798. write_inline_edge_summary (struct output_block *ob, struct cgraph_edge *e)
  3799. {
  3800. struct inline_edge_summary *es = inline_edge_summary (e);
  3801. int i;
  3802. streamer_write_uhwi (ob, es->call_stmt_size);
  3803. streamer_write_uhwi (ob, es->call_stmt_time);
  3804. streamer_write_uhwi (ob, es->loop_depth);
  3805. write_predicate (ob, es->predicate);
  3806. streamer_write_uhwi (ob, es->param.length ());
  3807. for (i = 0; i < (int) es->param.length (); i++)
  3808. streamer_write_uhwi (ob, es->param[i].change_prob);
  3809. }
  3810. /* Write inline summary for node in SET.
  3811. Jump functions are shared among ipa-cp and inliner, so when ipa-cp is
  3812. active, we don't need to write them twice. */
  3813. void
  3814. inline_write_summary (void)
  3815. {
  3816. struct cgraph_node *node;
  3817. struct output_block *ob = create_output_block (LTO_section_inline_summary);
  3818. lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
  3819. unsigned int count = 0;
  3820. int i;
  3821. for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
  3822. {
  3823. symtab_node *snode = lto_symtab_encoder_deref (encoder, i);
  3824. cgraph_node *cnode = dyn_cast <cgraph_node *> (snode);
  3825. if (cnode && cnode->definition && !cnode->alias)
  3826. count++;
  3827. }
  3828. streamer_write_uhwi (ob, count);
  3829. for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
  3830. {
  3831. symtab_node *snode = lto_symtab_encoder_deref (encoder, i);
  3832. cgraph_node *cnode = dyn_cast <cgraph_node *> (snode);
  3833. if (cnode && (node = cnode)->definition && !node->alias)
  3834. {
  3835. struct inline_summary *info = inline_summaries->get (node);
  3836. struct bitpack_d bp;
  3837. struct cgraph_edge *edge;
  3838. int i;
  3839. size_time_entry *e;
  3840. struct condition *c;
  3841. streamer_write_uhwi (ob,
  3842. lto_symtab_encoder_encode (encoder,
  3843. node));
  3844. streamer_write_hwi (ob, info->estimated_self_stack_size);
  3845. streamer_write_hwi (ob, info->self_size);
  3846. streamer_write_hwi (ob, info->self_time);
  3847. bp = bitpack_create (ob->main_stream);
  3848. bp_pack_value (&bp, info->inlinable, 1);
  3849. bp_pack_value (&bp, info->contains_cilk_spawn, 1);
  3850. streamer_write_bitpack (&bp);
  3851. streamer_write_uhwi (ob, vec_safe_length (info->conds));
  3852. for (i = 0; vec_safe_iterate (info->conds, i, &c); i++)
  3853. {
  3854. streamer_write_uhwi (ob, c->operand_num);
  3855. streamer_write_uhwi (ob, c->code);
  3856. stream_write_tree (ob, c->val, true);
  3857. bp = bitpack_create (ob->main_stream);
  3858. bp_pack_value (&bp, c->agg_contents, 1);
  3859. bp_pack_value (&bp, c->by_ref, 1);
  3860. streamer_write_bitpack (&bp);
  3861. if (c->agg_contents)
  3862. streamer_write_uhwi (ob, c->offset);
  3863. }
  3864. streamer_write_uhwi (ob, vec_safe_length (info->entry));
  3865. for (i = 0; vec_safe_iterate (info->entry, i, &e); i++)
  3866. {
  3867. streamer_write_uhwi (ob, e->size);
  3868. streamer_write_uhwi (ob, e->time);
  3869. write_predicate (ob, &e->predicate);
  3870. }
  3871. write_predicate (ob, info->loop_iterations);
  3872. write_predicate (ob, info->loop_stride);
  3873. write_predicate (ob, info->array_index);
  3874. for (edge = node->callees; edge; edge = edge->next_callee)
  3875. write_inline_edge_summary (ob, edge);
  3876. for (edge = node->indirect_calls; edge; edge = edge->next_callee)
  3877. write_inline_edge_summary (ob, edge);
  3878. }
  3879. }
  3880. streamer_write_char_stream (ob->main_stream, 0);
  3881. produce_asm (ob, NULL);
  3882. destroy_output_block (ob);
  3883. if (optimize && !flag_ipa_cp)
  3884. ipa_prop_write_jump_functions ();
  3885. }
  3886. /* Release inline summary. */
  3887. void
  3888. inline_free_summary (void)
  3889. {
  3890. struct cgraph_node *node;
  3891. if (edge_removal_hook_holder)
  3892. symtab->remove_edge_removal_hook (edge_removal_hook_holder);
  3893. edge_removal_hook_holder = NULL;
  3894. if (edge_duplication_hook_holder)
  3895. symtab->remove_edge_duplication_hook (edge_duplication_hook_holder);
  3896. edge_duplication_hook_holder = NULL;
  3897. if (!inline_edge_summary_vec.exists ())
  3898. return;
  3899. FOR_EACH_DEFINED_FUNCTION (node)
  3900. if (!node->alias)
  3901. reset_inline_summary (node, inline_summaries->get (node));
  3902. inline_summaries->release ();
  3903. inline_summaries = NULL;
  3904. inline_edge_summary_vec.release ();
  3905. if (edge_predicate_pool)
  3906. free_alloc_pool (edge_predicate_pool);
  3907. edge_predicate_pool = 0;
  3908. }