ipa-devirt.c 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778
  1. /* Basic IPA utilities for type inheritance graph construction and
  2. devirtualization.
  3. Copyright (C) 2013-2015 Free Software Foundation, Inc.
  4. Contributed by Jan Hubicka
  5. This file is part of GCC.
  6. GCC is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License as published by the Free
  8. Software Foundation; either version 3, or (at your option) any later
  9. version.
  10. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GCC; see the file COPYING3. If not see
  16. <http://www.gnu.org/licenses/>. */
  17. /* Brief vocabulary:
  18. ODR = One Definition Rule
  19. In short, the ODR states that:
  20. 1 In any translation unit, a template, type, function, or object can
  21. have no more than one definition. Some of these can have any number
  22. of declarations. A definition provides an instance.
  23. 2 In the entire program, an object or non-inline function cannot have
  24. more than one definition; if an object or function is used, it must
  25. have exactly one definition. You can declare an object or function
  26. that is never used, in which case you don't have to provide
  27. a definition. In no event can there be more than one definition.
  28. 3 Some things, like types, templates, and extern inline functions, can
  29. be defined in more than one translation unit. For a given entity,
  30. each definition must be the same. Non-extern objects and functions
  31. in different translation units are different entities, even if their
  32. names and types are the same.
  33. OTR = OBJ_TYPE_REF
  34. This is the Gimple representation of type information of a polymorphic call.
  35. It contains two parameters:
  36. otr_type is a type of class whose method is called.
  37. otr_token is the index into virtual table where address is taken.
  38. BINFO
  39. This is the type inheritance information attached to each tree
  40. RECORD_TYPE by the C++ frontend. It provides information about base
  41. types and virtual tables.
  42. BINFO is linked to the RECORD_TYPE by TYPE_BINFO.
  43. BINFO also links to its type by BINFO_TYPE and to the virtual table by
  44. BINFO_VTABLE.
  45. Base types of a given type are enumerated by BINFO_BASE_BINFO
  46. vector. Members of this vectors are not BINFOs associated
  47. with a base type. Rather they are new copies of BINFOs
  48. (base BINFOs). Their virtual tables may differ from
  49. virtual table of the base type. Also BINFO_OFFSET specifies
  50. offset of the base within the type.
  51. In the case of single inheritance, the virtual table is shared
  52. and BINFO_VTABLE of base BINFO is NULL. In the case of multiple
  53. inheritance the individual virtual tables are pointer to by
  54. BINFO_VTABLE of base binfos (that differs of BINFO_VTABLE of
  55. binfo associated to the base type).
  56. BINFO lookup for a given base type and offset can be done by
  57. get_binfo_at_offset. It returns proper BINFO whose virtual table
  58. can be used for lookup of virtual methods associated with the
  59. base type.
  60. token
  61. This is an index of virtual method in virtual table associated
  62. to the type defining it. Token can be looked up from OBJ_TYPE_REF
  63. or from DECL_VINDEX of a given virtual table.
  64. polymorphic (indirect) call
  65. This is callgraph representation of virtual method call. Every
  66. polymorphic call contains otr_type and otr_token taken from
  67. original OBJ_TYPE_REF at callgraph construction time.
  68. What we do here:
  69. build_type_inheritance_graph triggers a construction of the type inheritance
  70. graph.
  71. We reconstruct it based on types of methods we see in the unit.
  72. This means that the graph is not complete. Types with no methods are not
  73. inserted into the graph. Also types without virtual methods are not
  74. represented at all, though it may be easy to add this.
  75. The inheritance graph is represented as follows:
  76. Vertices are structures odr_type. Every odr_type may correspond
  77. to one or more tree type nodes that are equivalent by ODR rule.
  78. (the multiple type nodes appear only with linktime optimization)
  79. Edges are represented by odr_type->base and odr_type->derived_types.
  80. At the moment we do not track offsets of types for multiple inheritance.
  81. Adding this is easy.
  82. possible_polymorphic_call_targets returns, given an parameters found in
  83. indirect polymorphic edge all possible polymorphic call targets of the call.
  84. pass_ipa_devirt performs simple speculative devirtualization.
  85. */
  86. #include "config.h"
  87. #include "system.h"
  88. #include "coretypes.h"
  89. #include "tm.h"
  90. #include "hash-set.h"
  91. #include "machmode.h"
  92. #include "hash-map.h"
  93. #include "vec.h"
  94. #include "double-int.h"
  95. #include "input.h"
  96. #include "alias.h"
  97. #include "symtab.h"
  98. #include "wide-int.h"
  99. #include "inchash.h"
  100. #include "tree.h"
  101. #include "fold-const.h"
  102. #include "print-tree.h"
  103. #include "calls.h"
  104. #include "predict.h"
  105. #include "basic-block.h"
  106. #include "is-a.h"
  107. #include "plugin-api.h"
  108. #include "hard-reg-set.h"
  109. #include "function.h"
  110. #include "ipa-ref.h"
  111. #include "cgraph.h"
  112. #include "hashtab.h"
  113. #include "rtl.h"
  114. #include "flags.h"
  115. #include "statistics.h"
  116. #include "real.h"
  117. #include "fixed-value.h"
  118. #include "insn-config.h"
  119. #include "expmed.h"
  120. #include "dojump.h"
  121. #include "explow.h"
  122. #include "emit-rtl.h"
  123. #include "varasm.h"
  124. #include "stmt.h"
  125. #include "expr.h"
  126. #include "tree-pass.h"
  127. #include "target.h"
  128. #include "hash-table.h"
  129. #include "tree-pretty-print.h"
  130. #include "ipa-utils.h"
  131. #include "tree-ssa-alias.h"
  132. #include "internal-fn.h"
  133. #include "gimple-fold.h"
  134. #include "gimple-expr.h"
  135. #include "gimple.h"
  136. #include "alloc-pool.h"
  137. #include "symbol-summary.h"
  138. #include "ipa-prop.h"
  139. #include "ipa-inline.h"
  140. #include "diagnostic.h"
  141. #include "tree-dfa.h"
  142. #include "demangle.h"
  143. #include "dbgcnt.h"
  144. #include "gimple-pretty-print.h"
  145. #include "stor-layout.h"
  146. #include "intl.h"
  147. #include "streamer-hooks.h"
  148. #include "lto-streamer.h"
  149. /* Hash based set of pairs of types. */
  150. typedef struct
  151. {
  152. tree first;
  153. tree second;
  154. } type_pair;
  155. struct pair_traits : default_hashset_traits
  156. {
  157. static hashval_t
  158. hash (type_pair p)
  159. {
  160. return TYPE_UID (p.first) ^ TYPE_UID (p.second);
  161. }
  162. static bool
  163. is_empty (type_pair p)
  164. {
  165. return p.first == NULL;
  166. }
  167. static bool
  168. is_deleted (type_pair p ATTRIBUTE_UNUSED)
  169. {
  170. return false;
  171. }
  172. static bool
  173. equal (const type_pair &a, const type_pair &b)
  174. {
  175. return a.first==b.first && a.second == b.second;
  176. }
  177. static void
  178. mark_empty (type_pair &e)
  179. {
  180. e.first = NULL;
  181. }
  182. };
  183. static bool odr_types_equivalent_p (tree, tree, bool, bool *,
  184. hash_set<type_pair,pair_traits> *);
  185. static bool odr_violation_reported = false;
  186. /* Pointer set of all call targets appearing in the cache. */
  187. static hash_set<cgraph_node *> *cached_polymorphic_call_targets;
  188. /* The node of type inheritance graph. For each type unique in
  189. One Definition Rule (ODR) sense, we produce one node linking all
  190. main variants of types equivalent to it, bases and derived types. */
  191. struct GTY(()) odr_type_d
  192. {
  193. /* leader type. */
  194. tree type;
  195. /* All bases; built only for main variants of types. */
  196. vec<odr_type> GTY((skip)) bases;
  197. /* All derived types with virtual methods seen in unit;
  198. built only for main variants of types. */
  199. vec<odr_type> GTY((skip)) derived_types;
  200. /* All equivalent types, if more than one. */
  201. vec<tree, va_gc> *types;
  202. /* Set of all equivalent types, if NON-NULL. */
  203. hash_set<tree> * GTY((skip)) types_set;
  204. /* Unique ID indexing the type in odr_types array. */
  205. int id;
  206. /* Is it in anonymous namespace? */
  207. bool anonymous_namespace;
  208. /* Do we know about all derivations of given type? */
  209. bool all_derivations_known;
  210. /* Did we report ODR violation here? */
  211. bool odr_violated;
  212. /* Set when virtual table without RTTI previaled table with. */
  213. bool rtti_broken;
  214. };
  215. /* Return TRUE if all derived types of T are known and thus
  216. we may consider the walk of derived type complete.
  217. This is typically true only for final anonymous namespace types and types
  218. defined within functions (that may be COMDAT and thus shared across units,
  219. but with the same set of derived types). */
  220. bool
  221. type_all_derivations_known_p (const_tree t)
  222. {
  223. if (TYPE_FINAL_P (t))
  224. return true;
  225. if (flag_ltrans)
  226. return false;
  227. /* Non-C++ types may have IDENTIFIER_NODE here, do not crash. */
  228. if (!TYPE_NAME (t) || TREE_CODE (TYPE_NAME (t)) != TYPE_DECL)
  229. return true;
  230. if (type_in_anonymous_namespace_p (t))
  231. return true;
  232. return (decl_function_context (TYPE_NAME (t)) != NULL);
  233. }
  234. /* Return TRUE if type's constructors are all visible. */
  235. static bool
  236. type_all_ctors_visible_p (tree t)
  237. {
  238. return !flag_ltrans
  239. && symtab->state >= CONSTRUCTION
  240. /* We can not always use type_all_derivations_known_p.
  241. For function local types we must assume case where
  242. the function is COMDAT and shared in between units.
  243. TODO: These cases are quite easy to get, but we need
  244. to keep track of C++ privatizing via -Wno-weak
  245. as well as the IPA privatizing. */
  246. && type_in_anonymous_namespace_p (t);
  247. }
  248. /* Return TRUE if type may have instance. */
  249. static bool
  250. type_possibly_instantiated_p (tree t)
  251. {
  252. tree vtable;
  253. varpool_node *vnode;
  254. /* TODO: Add abstract types here. */
  255. if (!type_all_ctors_visible_p (t))
  256. return true;
  257. vtable = BINFO_VTABLE (TYPE_BINFO (t));
  258. if (TREE_CODE (vtable) == POINTER_PLUS_EXPR)
  259. vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0);
  260. vnode = varpool_node::get (vtable);
  261. return vnode && vnode->definition;
  262. }
  263. /* Hash used to unify ODR types based on their mangled name and for anonymous
  264. namespace types. */
  265. struct odr_name_hasher
  266. {
  267. typedef odr_type_d value_type;
  268. typedef union tree_node compare_type;
  269. static inline hashval_t hash (const value_type *);
  270. static inline bool equal (const value_type *, const compare_type *);
  271. static inline void remove (value_type *);
  272. };
  273. /* Has used to unify ODR types based on their associated virtual table.
  274. This hash is needed to keep -fno-lto-odr-type-merging to work and contains
  275. only polymorphic types. Types with mangled names are inserted to both. */
  276. struct odr_vtable_hasher:odr_name_hasher
  277. {
  278. static inline hashval_t hash (const value_type *);
  279. static inline bool equal (const value_type *, const compare_type *);
  280. };
  281. /* Return type that was declared with T's name so that T is an
  282. qualified variant of it. */
  283. static inline tree
  284. main_odr_variant (const_tree t)
  285. {
  286. if (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
  287. return TREE_TYPE (TYPE_NAME (t));
  288. /* Unnamed types and non-C++ produced types can be compared by variants. */
  289. else
  290. return TYPE_MAIN_VARIANT (t);
  291. }
  292. static bool
  293. can_be_name_hashed_p (tree t)
  294. {
  295. return (!in_lto_p || type_in_anonymous_namespace_p (t)
  296. || (TYPE_NAME (t) && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t))));
  297. }
  298. /* Hash type by its ODR name. */
  299. static hashval_t
  300. hash_odr_name (const_tree t)
  301. {
  302. gcc_checking_assert (main_odr_variant (t) == t);
  303. /* If not in LTO, all main variants are unique, so we can do
  304. pointer hash. */
  305. if (!in_lto_p)
  306. return htab_hash_pointer (t);
  307. /* Anonymous types are unique. */
  308. if (type_in_anonymous_namespace_p (t))
  309. return htab_hash_pointer (t);
  310. gcc_checking_assert (TYPE_NAME (t)
  311. && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t)));
  312. return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (TYPE_NAME (t)));
  313. }
  314. /* Return the computed hashcode for ODR_TYPE. */
  315. inline hashval_t
  316. odr_name_hasher::hash (const value_type *odr_type)
  317. {
  318. return hash_odr_name (odr_type->type);
  319. }
  320. static bool
  321. can_be_vtable_hashed_p (tree t)
  322. {
  323. /* vtable hashing can distinguish only main variants. */
  324. if (TYPE_MAIN_VARIANT (t) != t)
  325. return false;
  326. /* Anonymous namespace types are always handled by name hash. */
  327. if (type_in_anonymous_namespace_p (t))
  328. return false;
  329. return (TREE_CODE (t) == RECORD_TYPE
  330. && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)));
  331. }
  332. /* Hash type by assembler name of its vtable. */
  333. static hashval_t
  334. hash_odr_vtable (const_tree t)
  335. {
  336. tree v = BINFO_VTABLE (TYPE_BINFO (TYPE_MAIN_VARIANT (t)));
  337. inchash::hash hstate;
  338. gcc_checking_assert (in_lto_p);
  339. gcc_checking_assert (!type_in_anonymous_namespace_p (t));
  340. gcc_checking_assert (TREE_CODE (t) == RECORD_TYPE
  341. && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)));
  342. gcc_checking_assert (main_odr_variant (t) == t);
  343. if (TREE_CODE (v) == POINTER_PLUS_EXPR)
  344. {
  345. add_expr (TREE_OPERAND (v, 1), hstate);
  346. v = TREE_OPERAND (TREE_OPERAND (v, 0), 0);
  347. }
  348. hstate.add_wide_int (IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (v)));
  349. return hstate.end ();
  350. }
  351. /* Return the computed hashcode for ODR_TYPE. */
  352. inline hashval_t
  353. odr_vtable_hasher::hash (const value_type *odr_type)
  354. {
  355. return hash_odr_vtable (odr_type->type);
  356. }
  357. /* For languages with One Definition Rule, work out if
  358. types are the same based on their name.
  359. This is non-trivial for LTO where minor differences in
  360. the type representation may have prevented type merging
  361. to merge two copies of otherwise equivalent type.
  362. Until we start streaming mangled type names, this function works
  363. only for polymorphic types.
  364. When STRICT is true, we compare types by their names for purposes of
  365. ODR violation warnings. When strict is false, we consider variants
  366. equivalent, becuase it is all that matters for devirtualization machinery.
  367. */
  368. bool
  369. types_same_for_odr (const_tree type1, const_tree type2, bool strict)
  370. {
  371. gcc_checking_assert (TYPE_P (type1) && TYPE_P (type2));
  372. type1 = main_odr_variant (type1);
  373. type2 = main_odr_variant (type2);
  374. if (!strict)
  375. {
  376. type1 = TYPE_MAIN_VARIANT (type1);
  377. type2 = TYPE_MAIN_VARIANT (type2);
  378. }
  379. if (type1 == type2)
  380. return true;
  381. if (!in_lto_p)
  382. return false;
  383. /* Check for anonymous namespaces. Those have !TREE_PUBLIC
  384. on the corresponding TYPE_STUB_DECL. */
  385. if (type_in_anonymous_namespace_p (type1)
  386. || type_in_anonymous_namespace_p (type2))
  387. return false;
  388. /* ODR name of the type is set in DECL_ASSEMBLER_NAME of its TYPE_NAME.
  389. Ideally we should never need types without ODR names here. It can however
  390. happen in two cases:
  391. 1) for builtin types that are not streamed but rebuilt in lto/lto-lang.c
  392. Here testing for equivalence is safe, since their MAIN_VARIANTs are
  393. unique.
  394. 2) for units streamed with -fno-lto-odr-type-merging. Here we can't
  395. establish precise ODR equivalency, but for correctness we care only
  396. about equivalency on complete polymorphic types. For these we can
  397. compare assembler names of their virtual tables. */
  398. if ((!TYPE_NAME (type1) || !DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type1)))
  399. || (!TYPE_NAME (type2) || !DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type2))))
  400. {
  401. /* See if types are obviously different (i.e. different codes
  402. or polymorphic wrt non-polymorphic). This is not strictly correct
  403. for ODR violating programs, but we can't do better without streaming
  404. ODR names. */
  405. if (TREE_CODE (type1) != TREE_CODE (type2))
  406. return false;
  407. if (TREE_CODE (type1) == RECORD_TYPE
  408. && (TYPE_BINFO (type1) == NULL_TREE)
  409. != (TYPE_BINFO (type1) == NULL_TREE))
  410. return false;
  411. if (TREE_CODE (type1) == RECORD_TYPE && TYPE_BINFO (type1)
  412. && (BINFO_VTABLE (TYPE_BINFO (type1)) == NULL_TREE)
  413. != (BINFO_VTABLE (TYPE_BINFO (type2)) == NULL_TREE))
  414. return false;
  415. /* At the moment we have no way to establish ODR equivalence at LTO
  416. other than comparing virtual table pointers of polymorphic types.
  417. Eventually we should start saving mangled names in TYPE_NAME.
  418. Then this condition will become non-trivial. */
  419. if (TREE_CODE (type1) == RECORD_TYPE
  420. && TYPE_BINFO (type1) && TYPE_BINFO (type2)
  421. && BINFO_VTABLE (TYPE_BINFO (type1))
  422. && BINFO_VTABLE (TYPE_BINFO (type2)))
  423. {
  424. tree v1 = BINFO_VTABLE (TYPE_BINFO (type1));
  425. tree v2 = BINFO_VTABLE (TYPE_BINFO (type2));
  426. gcc_assert (TREE_CODE (v1) == POINTER_PLUS_EXPR
  427. && TREE_CODE (v2) == POINTER_PLUS_EXPR);
  428. return (operand_equal_p (TREE_OPERAND (v1, 1),
  429. TREE_OPERAND (v2, 1), 0)
  430. && DECL_ASSEMBLER_NAME
  431. (TREE_OPERAND (TREE_OPERAND (v1, 0), 0))
  432. == DECL_ASSEMBLER_NAME
  433. (TREE_OPERAND (TREE_OPERAND (v2, 0), 0)));
  434. }
  435. gcc_unreachable ();
  436. }
  437. return (DECL_ASSEMBLER_NAME (TYPE_NAME (type1))
  438. == DECL_ASSEMBLER_NAME (TYPE_NAME (type2)));
  439. }
  440. /* Return true if we can decide on ODR equivalency.
  441. In non-LTO it is always decide, in LTO however it depends in the type has
  442. ODR info attached.
  443. When STRICT is false, compare main variants. */
  444. bool
  445. types_odr_comparable (tree t1, tree t2, bool strict)
  446. {
  447. return (!in_lto_p
  448. || (strict ? main_odr_variant (t1) == main_odr_variant (t2)
  449. : TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
  450. || (odr_type_p (t1) && odr_type_p (t2))
  451. || (TREE_CODE (t1) == RECORD_TYPE && TREE_CODE (t2) == RECORD_TYPE
  452. && TYPE_BINFO (t1) && TYPE_BINFO (t2)
  453. && polymorphic_type_binfo_p (TYPE_BINFO (t1))
  454. && polymorphic_type_binfo_p (TYPE_BINFO (t2))));
  455. }
  456. /* Return true if T1 and T2 are ODR equivalent. If ODR equivalency is not
  457. known, be conservative and return false. */
  458. bool
  459. types_must_be_same_for_odr (tree t1, tree t2)
  460. {
  461. if (types_odr_comparable (t1, t2))
  462. return types_same_for_odr (t1, t2);
  463. else
  464. return TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2);
  465. }
  466. /* Compare types T1 and T2 and return true if they are
  467. equivalent. */
  468. inline bool
  469. odr_name_hasher::equal (const value_type *o1, const compare_type *t2)
  470. {
  471. tree t1 = o1->type;
  472. gcc_checking_assert (main_odr_variant (t2) == t2);
  473. gcc_checking_assert (main_odr_variant (t1) == t1);
  474. if (t1 == t2)
  475. return true;
  476. if (!in_lto_p)
  477. return false;
  478. /* Check for anonymous namespaces. Those have !TREE_PUBLIC
  479. on the corresponding TYPE_STUB_DECL. */
  480. if (type_in_anonymous_namespace_p (t1)
  481. || type_in_anonymous_namespace_p (t2))
  482. return false;
  483. gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t1)));
  484. gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t2)));
  485. return (DECL_ASSEMBLER_NAME (TYPE_NAME (t1))
  486. == DECL_ASSEMBLER_NAME (TYPE_NAME (t2)));
  487. }
  488. /* Compare types T1 and T2 and return true if they are
  489. equivalent. */
  490. inline bool
  491. odr_vtable_hasher::equal (const value_type *o1, const compare_type *t2)
  492. {
  493. tree t1 = o1->type;
  494. gcc_checking_assert (main_odr_variant (t2) == t2);
  495. gcc_checking_assert (main_odr_variant (t1) == t1);
  496. gcc_checking_assert (in_lto_p);
  497. t1 = TYPE_MAIN_VARIANT (t1);
  498. t2 = TYPE_MAIN_VARIANT (t2);
  499. if (t1 == t2)
  500. return true;
  501. tree v1 = BINFO_VTABLE (TYPE_BINFO (t1));
  502. tree v2 = BINFO_VTABLE (TYPE_BINFO (t2));
  503. return (operand_equal_p (TREE_OPERAND (v1, 1),
  504. TREE_OPERAND (v2, 1), 0)
  505. && DECL_ASSEMBLER_NAME
  506. (TREE_OPERAND (TREE_OPERAND (v1, 0), 0))
  507. == DECL_ASSEMBLER_NAME
  508. (TREE_OPERAND (TREE_OPERAND (v2, 0), 0)));
  509. }
  510. /* Free ODR type V. */
  511. inline void
  512. odr_name_hasher::remove (value_type *v)
  513. {
  514. v->bases.release ();
  515. v->derived_types.release ();
  516. if (v->types_set)
  517. delete v->types_set;
  518. ggc_free (v);
  519. }
  520. /* ODR type hash used to look up ODR type based on tree type node. */
  521. typedef hash_table<odr_name_hasher> odr_hash_type;
  522. static odr_hash_type *odr_hash;
  523. typedef hash_table<odr_vtable_hasher> odr_vtable_hash_type;
  524. static odr_vtable_hash_type *odr_vtable_hash;
  525. /* ODR types are also stored into ODR_TYPE vector to allow consistent
  526. walking. Bases appear before derived types. Vector is garbage collected
  527. so we won't end up visiting empty types. */
  528. static GTY(()) vec <odr_type, va_gc> *odr_types_ptr;
  529. #define odr_types (*odr_types_ptr)
  530. /* Set TYPE_BINFO of TYPE and its variants to BINFO. */
  531. void
  532. set_type_binfo (tree type, tree binfo)
  533. {
  534. for (; type; type = TYPE_NEXT_VARIANT (type))
  535. if (COMPLETE_TYPE_P (type))
  536. TYPE_BINFO (type) = binfo;
  537. else
  538. gcc_assert (!TYPE_BINFO (type));
  539. }
  540. /* Compare T2 and T2 based on name or structure. */
  541. static bool
  542. odr_subtypes_equivalent_p (tree t1, tree t2,
  543. hash_set<type_pair,pair_traits> *visited)
  544. {
  545. bool an1, an2;
  546. /* This can happen in incomplete types that should be handled earlier. */
  547. gcc_assert (t1 && t2);
  548. t1 = main_odr_variant (t1);
  549. t2 = main_odr_variant (t2);
  550. if (t1 == t2)
  551. return true;
  552. /* Anonymous namespace types must match exactly. */
  553. an1 = type_in_anonymous_namespace_p (t1);
  554. an2 = type_in_anonymous_namespace_p (t2);
  555. if (an1 != an2 || an1)
  556. return false;
  557. /* For ODR types be sure to compare their names.
  558. To support -wno-odr-type-merging we allow one type to be non-ODR
  559. and other ODR even though it is a violation. */
  560. if (types_odr_comparable (t1, t2, true))
  561. {
  562. if (!types_same_for_odr (t1, t2, true))
  563. return false;
  564. /* Limit recursion: If subtypes are ODR types and we know
  565. that they are same, be happy. */
  566. if (!get_odr_type (t1, true)->odr_violated)
  567. return true;
  568. }
  569. /* Component types, builtins and possibly violating ODR types
  570. have to be compared structurally. */
  571. if (TREE_CODE (t1) != TREE_CODE (t2))
  572. return false;
  573. if ((TYPE_NAME (t1) == NULL_TREE) != (TYPE_NAME (t2) == NULL_TREE))
  574. return false;
  575. type_pair pair={t1,t2};
  576. if (TYPE_UID (t1) > TYPE_UID (t2))
  577. {
  578. pair.first = t2;
  579. pair.second = t1;
  580. }
  581. if (visited->add (pair))
  582. return true;
  583. return odr_types_equivalent_p (t1, t2, false, NULL, visited);
  584. }
  585. /* Compare two virtual tables, PREVAILING and VTABLE and output ODR
  586. violation warnings. */
  587. void
  588. compare_virtual_tables (varpool_node *prevailing, varpool_node *vtable)
  589. {
  590. int n1, n2;
  591. if (DECL_VIRTUAL_P (prevailing->decl) != DECL_VIRTUAL_P (vtable->decl))
  592. {
  593. odr_violation_reported = true;
  594. if (DECL_VIRTUAL_P (prevailing->decl))
  595. {
  596. varpool_node *tmp = prevailing;
  597. prevailing = vtable;
  598. vtable = tmp;
  599. }
  600. if (warning_at (DECL_SOURCE_LOCATION
  601. (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
  602. OPT_Wodr,
  603. "virtual table of type %qD violates one definition rule",
  604. DECL_CONTEXT (vtable->decl)))
  605. inform (DECL_SOURCE_LOCATION (prevailing->decl),
  606. "variable of same assembler name as the virtual table is "
  607. "defined in another translation unit");
  608. return;
  609. }
  610. if (!prevailing->definition || !vtable->definition)
  611. return;
  612. /* If we do not stream ODR type info, do not bother to do useful compare. */
  613. if (!TYPE_BINFO (DECL_CONTEXT (vtable->decl))
  614. || !polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (vtable->decl))))
  615. return;
  616. odr_type class_type = get_odr_type (DECL_CONTEXT (vtable->decl), true);
  617. if (class_type->odr_violated)
  618. return;
  619. for (n1 = 0, n2 = 0; true; n1++, n2++)
  620. {
  621. struct ipa_ref *ref1, *ref2;
  622. bool end1, end2;
  623. end1 = !prevailing->iterate_reference (n1, ref1);
  624. end2 = !vtable->iterate_reference (n2, ref2);
  625. /* !DECL_VIRTUAL_P means RTTI entry;
  626. We warn when RTTI is lost because non-RTTI previals; we silently
  627. accept the other case. */
  628. while (!end2
  629. && (end1
  630. || (DECL_ASSEMBLER_NAME (ref1->referred->decl)
  631. != DECL_ASSEMBLER_NAME (ref2->referred->decl)
  632. && TREE_CODE (ref1->referred->decl) == FUNCTION_DECL))
  633. && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL)
  634. {
  635. if (!class_type->rtti_broken
  636. && warning_at (DECL_SOURCE_LOCATION
  637. (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
  638. OPT_Wodr,
  639. "virtual table of type %qD contains RTTI "
  640. "information",
  641. DECL_CONTEXT (vtable->decl)))
  642. {
  643. inform (DECL_SOURCE_LOCATION
  644. (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
  645. "but is prevailed by one without from other translation "
  646. "unit");
  647. inform (DECL_SOURCE_LOCATION
  648. (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
  649. "RTTI will not work on this type");
  650. class_type->rtti_broken = true;
  651. }
  652. n2++;
  653. end2 = !vtable->iterate_reference (n2, ref2);
  654. }
  655. while (!end1
  656. && (end2
  657. || (DECL_ASSEMBLER_NAME (ref2->referred->decl)
  658. != DECL_ASSEMBLER_NAME (ref1->referred->decl)
  659. && TREE_CODE (ref2->referred->decl) == FUNCTION_DECL))
  660. && TREE_CODE (ref1->referred->decl) != FUNCTION_DECL)
  661. {
  662. n1++;
  663. end1 = !prevailing->iterate_reference (n1, ref1);
  664. }
  665. /* Finished? */
  666. if (end1 && end2)
  667. {
  668. /* Extra paranoia; compare the sizes. We do not have information
  669. about virtual inheritance offsets, so just be sure that these
  670. match.
  671. Do this as very last check so the not very informative error
  672. is not output too often. */
  673. if (DECL_SIZE (prevailing->decl) != DECL_SIZE (vtable->decl))
  674. {
  675. class_type->odr_violated = true;
  676. if (warning_at (DECL_SOURCE_LOCATION
  677. (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
  678. OPT_Wodr,
  679. "virtual table of type %qD violates "
  680. "one definition rule ",
  681. DECL_CONTEXT (vtable->decl)))
  682. {
  683. inform (DECL_SOURCE_LOCATION
  684. (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
  685. "the conflicting type defined in another translation "
  686. "unit has virtual table of different size");
  687. }
  688. }
  689. return;
  690. }
  691. if (!end1 && !end2)
  692. {
  693. if (DECL_ASSEMBLER_NAME (ref1->referred->decl)
  694. == DECL_ASSEMBLER_NAME (ref2->referred->decl))
  695. continue;
  696. class_type->odr_violated = true;
  697. /* If the loops above stopped on non-virtual pointer, we have
  698. mismatch in RTTI information mangling. */
  699. if (TREE_CODE (ref1->referred->decl) != FUNCTION_DECL
  700. && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL)
  701. {
  702. if (warning_at (DECL_SOURCE_LOCATION
  703. (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
  704. OPT_Wodr,
  705. "virtual table of type %qD violates "
  706. "one definition rule ",
  707. DECL_CONTEXT (vtable->decl)))
  708. {
  709. inform (DECL_SOURCE_LOCATION
  710. (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
  711. "the conflicting type defined in another translation "
  712. "unit with different RTTI information");
  713. }
  714. return;
  715. }
  716. /* At this point both REF1 and REF2 points either to virtual table
  717. or virtual method. If one points to virtual table and other to
  718. method we can complain the same way as if one table was shorter
  719. than other pointing out the extra method. */
  720. if (TREE_CODE (ref1->referred->decl)
  721. != TREE_CODE (ref2->referred->decl))
  722. {
  723. if (TREE_CODE (ref1->referred->decl) == VAR_DECL)
  724. end1 = true;
  725. else if (TREE_CODE (ref2->referred->decl) == VAR_DECL)
  726. end2 = true;
  727. }
  728. }
  729. class_type->odr_violated = true;
  730. /* Complain about size mismatch. Either we have too many virutal
  731. functions or too many virtual table pointers. */
  732. if (end1 || end2)
  733. {
  734. if (end1)
  735. {
  736. varpool_node *tmp = prevailing;
  737. prevailing = vtable;
  738. vtable = tmp;
  739. ref1 = ref2;
  740. }
  741. if (warning_at (DECL_SOURCE_LOCATION
  742. (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
  743. OPT_Wodr,
  744. "virtual table of type %qD violates "
  745. "one definition rule",
  746. DECL_CONTEXT (vtable->decl)))
  747. {
  748. if (TREE_CODE (ref1->referring->decl) == FUNCTION_DECL)
  749. {
  750. inform (DECL_SOURCE_LOCATION
  751. (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
  752. "the conflicting type defined in another translation "
  753. "unit");
  754. inform (DECL_SOURCE_LOCATION
  755. (TYPE_NAME (DECL_CONTEXT (ref1->referring->decl))),
  756. "contains additional virtual method %qD",
  757. ref1->referred->decl);
  758. }
  759. else
  760. {
  761. inform (DECL_SOURCE_LOCATION
  762. (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
  763. "the conflicting type defined in another translation "
  764. "unit has virtual table table with more entries");
  765. }
  766. }
  767. return;
  768. }
  769. /* And in the last case we have either mistmatch in between two virtual
  770. methods or two virtual table pointers. */
  771. if (warning_at (DECL_SOURCE_LOCATION
  772. (TYPE_NAME (DECL_CONTEXT (vtable->decl))), OPT_Wodr,
  773. "virtual table of type %qD violates "
  774. "one definition rule ",
  775. DECL_CONTEXT (vtable->decl)))
  776. {
  777. if (TREE_CODE (ref1->referred->decl) == FUNCTION_DECL)
  778. {
  779. inform (DECL_SOURCE_LOCATION
  780. (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
  781. "the conflicting type defined in another translation "
  782. "unit");
  783. gcc_assert (TREE_CODE (ref2->referred->decl)
  784. == FUNCTION_DECL);
  785. inform (DECL_SOURCE_LOCATION (ref1->referred->decl),
  786. "virtual method %qD", ref1->referred->decl);
  787. inform (DECL_SOURCE_LOCATION (ref2->referred->decl),
  788. "ought to match virtual method %qD but does not",
  789. ref2->referred->decl);
  790. }
  791. else
  792. inform (DECL_SOURCE_LOCATION
  793. (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
  794. "the conflicting type defined in another translation "
  795. "unit has virtual table table with different contents");
  796. return;
  797. }
  798. }
  799. }
  800. /* Output ODR violation warning about T1 and T2 with REASON.
  801. Display location of ST1 and ST2 if REASON speaks about field or
  802. method of the type.
  803. If WARN is false, do nothing. Set WARNED if warning was indeed
  804. output. */
  805. void
  806. warn_odr (tree t1, tree t2, tree st1, tree st2,
  807. bool warn, bool *warned, const char *reason)
  808. {
  809. tree decl2 = TYPE_NAME (t2);
  810. if (warned)
  811. *warned = false;
  812. if (!warn || !TYPE_NAME(t1))
  813. return;
  814. /* ODR warnings are output druing LTO streaming; we must apply location
  815. cache for potential warnings to be output correctly. */
  816. if (lto_location_cache::current_cache)
  817. lto_location_cache::current_cache->apply_location_cache ();
  818. if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (t1)), OPT_Wodr,
  819. "type %qT violates one definition rule",
  820. t1))
  821. return;
  822. if (!st1 && !st2)
  823. ;
  824. /* For FIELD_DECL support also case where one of fields is
  825. NULL - this is used when the structures have mismatching number of
  826. elements. */
  827. else if (!st1 || TREE_CODE (st1) == FIELD_DECL)
  828. {
  829. inform (DECL_SOURCE_LOCATION (decl2),
  830. "a different type is defined in another translation unit");
  831. if (!st1)
  832. {
  833. st1 = st2;
  834. st2 = NULL;
  835. }
  836. inform (DECL_SOURCE_LOCATION (st1),
  837. "the first difference of corresponding definitions is field %qD",
  838. st1);
  839. if (st2)
  840. decl2 = st2;
  841. }
  842. else if (TREE_CODE (st1) == FUNCTION_DECL)
  843. {
  844. inform (DECL_SOURCE_LOCATION (decl2),
  845. "a different type is defined in another translation unit");
  846. inform (DECL_SOURCE_LOCATION (st1),
  847. "the first difference of corresponding definitions is method %qD",
  848. st1);
  849. decl2 = st2;
  850. }
  851. else
  852. return;
  853. inform (DECL_SOURCE_LOCATION (decl2), reason);
  854. if (warned)
  855. *warned = true;
  856. }
  857. /* We already warned about ODR mismatch. T1 and T2 ought to be equivalent
  858. because they are used on same place in ODR matching types.
  859. They are not; inform the user. */
  860. void
  861. warn_types_mismatch (tree t1, tree t2)
  862. {
  863. /* If types have names and they are different, it is most informative to
  864. output those. */
  865. if (TYPE_NAME (t1) && TYPE_NAME (t2)
  866. && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t1))
  867. && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t2))
  868. && DECL_ASSEMBLER_NAME (TYPE_NAME (t1))
  869. != DECL_ASSEMBLER_NAME (TYPE_NAME (t2)))
  870. {
  871. char *name1 = xstrdup (cplus_demangle
  872. (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (t1))),
  873. DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES));
  874. char *name2 = cplus_demangle
  875. (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (t2))),
  876. DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES);
  877. if (name1 && name2 && strcmp (name1, name2))
  878. {
  879. inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
  880. "type name %<%s%> should match type name %<%s%>",
  881. name1, name2);
  882. inform (DECL_SOURCE_LOCATION (TYPE_NAME (t2)),
  883. "the incompatible type is defined here");
  884. free (name1);
  885. return;
  886. }
  887. free (name1);
  888. }
  889. /* It is a quite common bug to reference anonymous namespace type in
  890. non-anonymous namespace class. */
  891. if (type_in_anonymous_namespace_p (t1)
  892. || type_in_anonymous_namespace_p (t2))
  893. {
  894. if (!type_in_anonymous_namespace_p (t1))
  895. {
  896. tree tmp = t1;;
  897. t1 = t2;
  898. t2 = tmp;
  899. }
  900. if (TYPE_NAME (t1) && TYPE_NAME (t2))
  901. {
  902. inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
  903. "type %qT defined in anonymous namespace can not match "
  904. "type %qT",
  905. t1, t2);
  906. inform (DECL_SOURCE_LOCATION (TYPE_NAME (t2)),
  907. "the incompatible type defined in anonymous namespace in "
  908. "another translation unit");
  909. }
  910. else
  911. inform (UNKNOWN_LOCATION,
  912. "types in anonymous namespace does not match across "
  913. "translation unit boundary");
  914. return;
  915. }
  916. /* A tricky case are component types. Often they appear the same in source
  917. code and the mismatch is dragged in by type they are build from.
  918. Look for those differences in subtypes and try to be informative. In other
  919. cases just output nothing because the source code is probably different
  920. and in this case we already output a all necessary info. */
  921. if (!TYPE_NAME (t1) || !TYPE_NAME (t2))
  922. {
  923. if (TREE_CODE (t1) == TREE_CODE (t2))
  924. {
  925. hash_set<type_pair,pair_traits> visited;
  926. if (TREE_CODE (t1) == ARRAY_TYPE
  927. && COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
  928. {
  929. tree i1 = TYPE_DOMAIN (t1);
  930. tree i2 = TYPE_DOMAIN (t2);
  931. if (i1 && i2
  932. && TYPE_MAX_VALUE (i1)
  933. && TYPE_MAX_VALUE (i2)
  934. && !operand_equal_p (TYPE_MAX_VALUE (i1),
  935. TYPE_MAX_VALUE (i2), 0))
  936. {
  937. inform (UNKNOWN_LOCATION,
  938. "array types have different bounds");
  939. return;
  940. }
  941. }
  942. if ((POINTER_TYPE_P (t1) || TREE_CODE (t1) == ARRAY_TYPE)
  943. && !odr_subtypes_equivalent_p (TREE_TYPE (t1),
  944. TREE_TYPE (t2),
  945. &visited))
  946. warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
  947. else if (TREE_CODE (t1) == METHOD_TYPE
  948. || TREE_CODE (t1) == FUNCTION_TYPE)
  949. {
  950. tree parms1, parms2;
  951. int count = 1;
  952. if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
  953. &visited))
  954. {
  955. inform (UNKNOWN_LOCATION, "return value type mismatch");
  956. warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
  957. return;
  958. }
  959. for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
  960. parms1 && parms2;
  961. parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2),
  962. count++)
  963. {
  964. if (!odr_subtypes_equivalent_p
  965. (TREE_VALUE (parms1), TREE_VALUE (parms2), &visited))
  966. {
  967. inform (UNKNOWN_LOCATION,
  968. "type mismatch in parameter %i", count);
  969. warn_types_mismatch (TREE_VALUE (parms1),
  970. TREE_VALUE (parms2));
  971. return;
  972. }
  973. }
  974. if (parms1 || parms2)
  975. {
  976. inform (UNKNOWN_LOCATION,
  977. "types have different parameter counts");
  978. return;
  979. }
  980. }
  981. }
  982. return;
  983. }
  984. /* This should not happen but if it does, the warning would not be helpful.
  985. TODO: turn it into assert next stage1. */
  986. if (TYPE_NAME (t1) == TYPE_NAME (t2))
  987. return;
  988. /* In Firefox it is a common bug to have same types but in
  989. different namespaces. Be a bit more informative on
  990. this. */
  991. if (TYPE_CONTEXT (t1) && TYPE_CONTEXT (t2)
  992. && (((TREE_CODE (TYPE_CONTEXT (t1)) == NAMESPACE_DECL)
  993. != (TREE_CODE (TYPE_CONTEXT (t2)) == NAMESPACE_DECL))
  994. || (TREE_CODE (TYPE_CONTEXT (t1)) == NAMESPACE_DECL
  995. && (DECL_NAME (TYPE_CONTEXT (t1)) !=
  996. DECL_NAME (TYPE_CONTEXT (t2))))))
  997. inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
  998. "type %qT should match type %qT but is defined "
  999. "in different namespace ",
  1000. t1, t2);
  1001. else if (types_odr_comparable (t1, t2, true)
  1002. && types_same_for_odr (t1, t2, true))
  1003. inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
  1004. "type %qT should match type %qT that itself violate "
  1005. "one definition rule",
  1006. t1, t2);
  1007. else
  1008. inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
  1009. "type %qT should match type %qT",
  1010. t1, t2);
  1011. if (DECL_SOURCE_LOCATION (TYPE_NAME (t2)) > BUILTINS_LOCATION)
  1012. inform (DECL_SOURCE_LOCATION (TYPE_NAME (t2)),
  1013. "the incompatible type is defined here");
  1014. }
  1015. /* Compare T1 and T2, report ODR violations if WARN is true and set
  1016. WARNED to true if anything is reported. Return true if types match.
  1017. If true is returned, the types are also compatible in the sense of
  1018. gimple_canonical_types_compatible_p. */
  1019. static bool
  1020. odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
  1021. hash_set<type_pair,pair_traits> *visited)
  1022. {
  1023. /* Check first for the obvious case of pointer identity. */
  1024. if (t1 == t2)
  1025. return true;
  1026. gcc_assert (!type_in_anonymous_namespace_p (t1));
  1027. gcc_assert (!type_in_anonymous_namespace_p (t2));
  1028. /* Can't be the same type if the types don't have the same code. */
  1029. if (TREE_CODE (t1) != TREE_CODE (t2))
  1030. {
  1031. warn_odr (t1, t2, NULL, NULL, warn, warned,
  1032. G_("a different type is defined in another translation unit"));
  1033. return false;
  1034. }
  1035. if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
  1036. {
  1037. warn_odr (t1, t2, NULL, NULL, warn, warned,
  1038. G_("a type with different qualifiers is defined in another "
  1039. "translation unit"));
  1040. return false;
  1041. }
  1042. if (comp_type_attributes (t1, t2) != 1)
  1043. {
  1044. warn_odr (t1, t2, NULL, NULL, warn, warned,
  1045. G_("a type with attributes "
  1046. "is defined in another translation unit"));
  1047. return false;
  1048. }
  1049. if (TREE_CODE (t1) == ENUMERAL_TYPE
  1050. && TYPE_VALUES (t1) && TYPE_VALUES (t2))
  1051. {
  1052. tree v1, v2;
  1053. for (v1 = TYPE_VALUES (t1), v2 = TYPE_VALUES (t2);
  1054. v1 && v2 ; v1 = TREE_CHAIN (v1), v2 = TREE_CHAIN (v2))
  1055. {
  1056. if (TREE_PURPOSE (v1) != TREE_PURPOSE (v2))
  1057. {
  1058. warn_odr (t1, t2, NULL, NULL, warn, warned,
  1059. G_("an enum with different value name"
  1060. " is defined in another translation unit"));
  1061. return false;
  1062. }
  1063. if (TREE_VALUE (v1) != TREE_VALUE (v2)
  1064. && !operand_equal_p (DECL_INITIAL (TREE_VALUE (v1)),
  1065. DECL_INITIAL (TREE_VALUE (v2)), 0))
  1066. {
  1067. warn_odr (t1, t2, NULL, NULL, warn, warned,
  1068. G_("an enum with different values is defined"
  1069. " in another translation unit"));
  1070. return false;
  1071. }
  1072. }
  1073. if (v1 || v2)
  1074. {
  1075. warn_odr (t1, t2, NULL, NULL, warn, warned,
  1076. G_("an enum with mismatching number of values "
  1077. "is defined in another translation unit"));
  1078. return false;
  1079. }
  1080. }
  1081. /* Non-aggregate types can be handled cheaply. */
  1082. if (INTEGRAL_TYPE_P (t1)
  1083. || SCALAR_FLOAT_TYPE_P (t1)
  1084. || FIXED_POINT_TYPE_P (t1)
  1085. || TREE_CODE (t1) == VECTOR_TYPE
  1086. || TREE_CODE (t1) == COMPLEX_TYPE
  1087. || TREE_CODE (t1) == OFFSET_TYPE
  1088. || POINTER_TYPE_P (t1))
  1089. {
  1090. if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2))
  1091. {
  1092. warn_odr (t1, t2, NULL, NULL, warn, warned,
  1093. G_("a type with different precision is defined "
  1094. "in another translation unit"));
  1095. return false;
  1096. }
  1097. if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
  1098. {
  1099. warn_odr (t1, t2, NULL, NULL, warn, warned,
  1100. G_("a type with different signedness is defined "
  1101. "in another translation unit"));
  1102. return false;
  1103. }
  1104. if (TREE_CODE (t1) == INTEGER_TYPE
  1105. && TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2))
  1106. {
  1107. /* char WRT uint_8? */
  1108. warn_odr (t1, t2, NULL, NULL, warn, warned,
  1109. G_("a different type is defined in another "
  1110. "translation unit"));
  1111. return false;
  1112. }
  1113. /* For canonical type comparisons we do not want to build SCCs
  1114. so we cannot compare pointed-to types. But we can, for now,
  1115. require the same pointed-to type kind and match what
  1116. useless_type_conversion_p would do. */
  1117. if (POINTER_TYPE_P (t1))
  1118. {
  1119. if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
  1120. != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
  1121. {
  1122. warn_odr (t1, t2, NULL, NULL, warn, warned,
  1123. G_("it is defined as a pointer in different address "
  1124. "space in another translation unit"));
  1125. return false;
  1126. }
  1127. if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
  1128. {
  1129. warn_odr (t1, t2, NULL, NULL, warn, warned,
  1130. G_("it is defined as a pointer to different type "
  1131. "in another translation unit"));
  1132. if (warn && warned)
  1133. warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
  1134. return false;
  1135. }
  1136. }
  1137. if ((TREE_CODE (t1) == VECTOR_TYPE || TREE_CODE (t1) == COMPLEX_TYPE)
  1138. && !odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
  1139. {
  1140. /* Probably specific enough. */
  1141. warn_odr (t1, t2, NULL, NULL, warn, warned,
  1142. G_("a different type is defined "
  1143. "in another translation unit"));
  1144. if (warn && warned)
  1145. warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
  1146. return false;
  1147. }
  1148. }
  1149. /* Do type-specific comparisons. */
  1150. else switch (TREE_CODE (t1))
  1151. {
  1152. case ARRAY_TYPE:
  1153. {
  1154. /* Array types are the same if the element types are the same and
  1155. the number of elements are the same. */
  1156. if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
  1157. {
  1158. warn_odr (t1, t2, NULL, NULL, warn, warned,
  1159. G_("a different type is defined in another "
  1160. "translation unit"));
  1161. if (warn && warned)
  1162. warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
  1163. }
  1164. gcc_assert (TYPE_STRING_FLAG (t1) == TYPE_STRING_FLAG (t2));
  1165. gcc_assert (TYPE_NONALIASED_COMPONENT (t1)
  1166. == TYPE_NONALIASED_COMPONENT (t2));
  1167. tree i1 = TYPE_DOMAIN (t1);
  1168. tree i2 = TYPE_DOMAIN (t2);
  1169. /* For an incomplete external array, the type domain can be
  1170. NULL_TREE. Check this condition also. */
  1171. if (i1 == NULL_TREE || i2 == NULL_TREE)
  1172. return true;
  1173. tree min1 = TYPE_MIN_VALUE (i1);
  1174. tree min2 = TYPE_MIN_VALUE (i2);
  1175. tree max1 = TYPE_MAX_VALUE (i1);
  1176. tree max2 = TYPE_MAX_VALUE (i2);
  1177. /* In C++, minimums should be always 0. */
  1178. gcc_assert (min1 == min2);
  1179. if (!operand_equal_p (max1, max2, 0))
  1180. {
  1181. warn_odr (t1, t2, NULL, NULL, warn, warned,
  1182. G_("an array of different size is defined "
  1183. "in another translation unit"));
  1184. return false;
  1185. }
  1186. }
  1187. break;
  1188. case METHOD_TYPE:
  1189. case FUNCTION_TYPE:
  1190. /* Function types are the same if the return type and arguments types
  1191. are the same. */
  1192. if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
  1193. {
  1194. warn_odr (t1, t2, NULL, NULL, warn, warned,
  1195. G_("has different return value "
  1196. "in another translation unit"));
  1197. if (warn && warned)
  1198. warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
  1199. return false;
  1200. }
  1201. if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2))
  1202. return true;
  1203. else
  1204. {
  1205. tree parms1, parms2;
  1206. for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
  1207. parms1 && parms2;
  1208. parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
  1209. {
  1210. if (!odr_subtypes_equivalent_p
  1211. (TREE_VALUE (parms1), TREE_VALUE (parms2), visited))
  1212. {
  1213. warn_odr (t1, t2, NULL, NULL, warn, warned,
  1214. G_("has different parameters in another "
  1215. "translation unit"));
  1216. if (warn && warned)
  1217. warn_types_mismatch (TREE_VALUE (parms1),
  1218. TREE_VALUE (parms2));
  1219. return false;
  1220. }
  1221. }
  1222. if (parms1 || parms2)
  1223. {
  1224. warn_odr (t1, t2, NULL, NULL, warn, warned,
  1225. G_("has different parameters "
  1226. "in another translation unit"));
  1227. return false;
  1228. }
  1229. return true;
  1230. }
  1231. case RECORD_TYPE:
  1232. case UNION_TYPE:
  1233. case QUAL_UNION_TYPE:
  1234. {
  1235. tree f1, f2;
  1236. /* For aggregate types, all the fields must be the same. */
  1237. if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
  1238. {
  1239. if (TYPE_BINFO (t1) && TYPE_BINFO (t2)
  1240. && polymorphic_type_binfo_p (TYPE_BINFO (t1))
  1241. != polymorphic_type_binfo_p (TYPE_BINFO (t2)))
  1242. {
  1243. if (polymorphic_type_binfo_p (TYPE_BINFO (t1)))
  1244. warn_odr (t1, t2, NULL, NULL, warn, warned,
  1245. G_("a type defined in another translation unit "
  1246. "is not polymorphic"));
  1247. else
  1248. warn_odr (t1, t2, NULL, NULL, warn, warned,
  1249. G_("a type defined in another translation unit "
  1250. "is polymorphic"));
  1251. return false;
  1252. }
  1253. for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
  1254. f1 || f2;
  1255. f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
  1256. {
  1257. /* Skip non-fields. */
  1258. while (f1 && TREE_CODE (f1) != FIELD_DECL)
  1259. f1 = TREE_CHAIN (f1);
  1260. while (f2 && TREE_CODE (f2) != FIELD_DECL)
  1261. f2 = TREE_CHAIN (f2);
  1262. if (!f1 || !f2)
  1263. break;
  1264. if (DECL_VIRTUAL_P (f1) != DECL_VIRTUAL_P (f2))
  1265. {
  1266. warn_odr (t1, t2, NULL, NULL, warn, warned,
  1267. G_("a type with different virtual table pointers"
  1268. " is defined in another translation unit"));
  1269. return false;
  1270. }
  1271. if (DECL_ARTIFICIAL (f1) != DECL_ARTIFICIAL (f2))
  1272. {
  1273. warn_odr (t1, t2, NULL, NULL, warn, warned,
  1274. G_("a type with different bases is defined "
  1275. "in another translation unit"));
  1276. return false;
  1277. }
  1278. if (DECL_NAME (f1) != DECL_NAME (f2)
  1279. && !DECL_ARTIFICIAL (f1))
  1280. {
  1281. warn_odr (t1, t2, f1, f2, warn, warned,
  1282. G_("a field with different name is defined "
  1283. "in another translation unit"));
  1284. return false;
  1285. }
  1286. if (!odr_subtypes_equivalent_p (TREE_TYPE (f1),
  1287. TREE_TYPE (f2), visited))
  1288. {
  1289. /* Do not warn about artificial fields and just go into
  1290. generic field mismatch warning. */
  1291. if (DECL_ARTIFICIAL (f1))
  1292. break;
  1293. warn_odr (t1, t2, f1, f2, warn, warned,
  1294. G_("a field of same name but different type "
  1295. "is defined in another translation unit"));
  1296. if (warn && warned)
  1297. warn_types_mismatch (TREE_TYPE (f1), TREE_TYPE (f2));
  1298. return false;
  1299. }
  1300. if (!gimple_compare_field_offset (f1, f2))
  1301. {
  1302. /* Do not warn about artificial fields and just go into
  1303. generic field mismatch warning. */
  1304. if (DECL_ARTIFICIAL (f1))
  1305. break;
  1306. warn_odr (t1, t2, f1, f2, warn, warned,
  1307. G_("fields has different layout "
  1308. "in another translation unit"));
  1309. return false;
  1310. }
  1311. gcc_assert (DECL_NONADDRESSABLE_P (f1)
  1312. == DECL_NONADDRESSABLE_P (f2));
  1313. }
  1314. /* If one aggregate has more fields than the other, they
  1315. are not the same. */
  1316. if (f1 || f2)
  1317. {
  1318. if ((f1 && DECL_VIRTUAL_P (f1)) || (f2 && DECL_VIRTUAL_P (f2)))
  1319. warn_odr (t1, t2, NULL, NULL, warn, warned,
  1320. G_("a type with different virtual table pointers"
  1321. " is defined in another translation unit"));
  1322. else if ((f1 && DECL_ARTIFICIAL (f1))
  1323. || (f2 && DECL_ARTIFICIAL (f2)))
  1324. warn_odr (t1, t2, NULL, NULL, warn, warned,
  1325. G_("a type with different bases is defined "
  1326. "in another translation unit"));
  1327. else
  1328. warn_odr (t1, t2, f1, f2, warn, warned,
  1329. G_("a type with different number of fields "
  1330. "is defined in another translation unit"));
  1331. return false;
  1332. }
  1333. if ((TYPE_MAIN_VARIANT (t1) == t1 || TYPE_MAIN_VARIANT (t2) == t2)
  1334. && (TYPE_METHODS (TYPE_MAIN_VARIANT (t1))
  1335. != TYPE_METHODS (TYPE_MAIN_VARIANT (t2))))
  1336. {
  1337. for (f1 = TYPE_METHODS (TYPE_MAIN_VARIANT (t1)),
  1338. f2 = TYPE_METHODS (TYPE_MAIN_VARIANT (t2));
  1339. f1 && f2 ; f1 = DECL_CHAIN (f1), f2 = DECL_CHAIN (f2))
  1340. {
  1341. if (DECL_ASSEMBLER_NAME (f1) != DECL_ASSEMBLER_NAME (f2))
  1342. {
  1343. warn_odr (t1, t2, f1, f2, warn, warned,
  1344. G_("a different method of same type "
  1345. "is defined in another translation unit"));
  1346. return false;
  1347. }
  1348. if (DECL_VIRTUAL_P (f1) != DECL_VIRTUAL_P (f2))
  1349. {
  1350. warn_odr (t1, t2, f1, f2, warn, warned,
  1351. G_("s definition that differs by virtual "
  1352. "keyword in another translation unit"));
  1353. return false;
  1354. }
  1355. if (DECL_VINDEX (f1) != DECL_VINDEX (f2))
  1356. {
  1357. warn_odr (t1, t2, f1, f2, warn, warned,
  1358. G_("virtual table layout differs in another "
  1359. "translation unit"));
  1360. return false;
  1361. }
  1362. if (odr_subtypes_equivalent_p (TREE_TYPE (f1), TREE_TYPE (f2), visited))
  1363. {
  1364. warn_odr (t1, t2, f1, f2, warn, warned,
  1365. G_("method with incompatible type is defined "
  1366. "in another translation unit"));
  1367. return false;
  1368. }
  1369. }
  1370. if (f1 || f2)
  1371. {
  1372. warn_odr (t1, t2, NULL, NULL, warn, warned,
  1373. G_("a type with different number of methods "
  1374. "is defined in another translation unit"));
  1375. return false;
  1376. }
  1377. }
  1378. }
  1379. break;
  1380. }
  1381. case VOID_TYPE:
  1382. break;
  1383. default:
  1384. debug_tree (t1);
  1385. gcc_unreachable ();
  1386. }
  1387. /* Those are better to come last as they are utterly uninformative. */
  1388. if (TYPE_SIZE (t1) && TYPE_SIZE (t2)
  1389. && !operand_equal_p (TYPE_SIZE (t1), TYPE_SIZE (t2), 0))
  1390. {
  1391. warn_odr (t1, t2, NULL, NULL, warn, warned,
  1392. G_("a type with different size "
  1393. "is defined in another translation unit"));
  1394. return false;
  1395. }
  1396. if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2)
  1397. && TYPE_ALIGN (t1) != TYPE_ALIGN (t2))
  1398. {
  1399. warn_odr (t1, t2, NULL, NULL, warn, warned,
  1400. G_("a type with different alignment "
  1401. "is defined in another translation unit"));
  1402. return false;
  1403. }
  1404. gcc_assert (!TYPE_SIZE_UNIT (t1) || !TYPE_SIZE_UNIT (t2)
  1405. || operand_equal_p (TYPE_SIZE_UNIT (t1),
  1406. TYPE_SIZE_UNIT (t2), 0));
  1407. return true;
  1408. }
  1409. /* TYPE is equivalent to VAL by ODR, but its tree representation differs
  1410. from VAL->type. This may happen in LTO where tree merging did not merge
  1411. all variants of the same type or due to ODR violation.
  1412. Analyze and report ODR violations and add type to duplicate list.
  1413. If TYPE is more specified than VAL->type, prevail VAL->type. Also if
  1414. this is first time we see definition of a class return true so the
  1415. base types are analyzed. */
  1416. static bool
  1417. add_type_duplicate (odr_type val, tree type)
  1418. {
  1419. bool build_bases = false;
  1420. bool prevail = false;
  1421. bool odr_must_violate = false;
  1422. if (!val->types_set)
  1423. val->types_set = new hash_set<tree>;
  1424. /* Chose polymorphic type as leader (this happens only in case of ODR
  1425. violations. */
  1426. if ((TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type)
  1427. && polymorphic_type_binfo_p (TYPE_BINFO (type)))
  1428. && (TREE_CODE (val->type) != RECORD_TYPE || !TYPE_BINFO (val->type)
  1429. || !polymorphic_type_binfo_p (TYPE_BINFO (val->type))))
  1430. {
  1431. prevail = true;
  1432. build_bases = true;
  1433. }
  1434. /* Always prefer complete type to be the leader. */
  1435. else if (!COMPLETE_TYPE_P (val->type) && COMPLETE_TYPE_P (type))
  1436. {
  1437. prevail = true;
  1438. build_bases = TYPE_BINFO (type);
  1439. }
  1440. else if (COMPLETE_TYPE_P (val->type) && !COMPLETE_TYPE_P (type))
  1441. ;
  1442. else if (TREE_CODE (val->type) == ENUMERAL_TYPE
  1443. && TREE_CODE (type) == ENUMERAL_TYPE
  1444. && !TYPE_VALUES (val->type) && TYPE_VALUES (type))
  1445. prevail = true;
  1446. else if (TREE_CODE (val->type) == RECORD_TYPE
  1447. && TREE_CODE (type) == RECORD_TYPE
  1448. && TYPE_BINFO (type) && !TYPE_BINFO (val->type))
  1449. {
  1450. gcc_assert (!val->bases.length ());
  1451. build_bases = true;
  1452. prevail = true;
  1453. }
  1454. if (prevail)
  1455. {
  1456. tree tmp = type;
  1457. type = val->type;
  1458. val->type = tmp;
  1459. }
  1460. val->types_set->add (type);
  1461. /* If we now have a mangled name, be sure to record it to val->type
  1462. so ODR hash can work. */
  1463. if (can_be_name_hashed_p (type) && !can_be_name_hashed_p (val->type))
  1464. SET_DECL_ASSEMBLER_NAME (TYPE_NAME (val->type),
  1465. DECL_ASSEMBLER_NAME (TYPE_NAME (type)));
  1466. bool merge = true;
  1467. bool base_mismatch = false;
  1468. unsigned int i;
  1469. bool warned = false;
  1470. hash_set<type_pair,pair_traits> visited;
  1471. gcc_assert (in_lto_p);
  1472. vec_safe_push (val->types, type);
  1473. /* If both are class types, compare the bases. */
  1474. if (COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (val->type)
  1475. && TREE_CODE (val->type) == RECORD_TYPE
  1476. && TREE_CODE (type) == RECORD_TYPE
  1477. && TYPE_BINFO (val->type) && TYPE_BINFO (type))
  1478. {
  1479. if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
  1480. != BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)))
  1481. {
  1482. if (!flag_ltrans && !warned && !val->odr_violated)
  1483. {
  1484. tree extra_base;
  1485. warn_odr (type, val->type, NULL, NULL, !warned, &warned,
  1486. "a type with the same name but different "
  1487. "number of polymorphic bases is "
  1488. "defined in another translation unit");
  1489. if (warned)
  1490. {
  1491. if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
  1492. > BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)))
  1493. extra_base = BINFO_BASE_BINFO
  1494. (TYPE_BINFO (type),
  1495. BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)));
  1496. else
  1497. extra_base = BINFO_BASE_BINFO
  1498. (TYPE_BINFO (val->type),
  1499. BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
  1500. tree extra_base_type = BINFO_TYPE (extra_base);
  1501. inform (DECL_SOURCE_LOCATION (TYPE_NAME (extra_base_type)),
  1502. "the extra base is defined here");
  1503. }
  1504. }
  1505. base_mismatch = true;
  1506. }
  1507. else
  1508. for (i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
  1509. {
  1510. tree base1 = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
  1511. tree base2 = BINFO_BASE_BINFO (TYPE_BINFO (val->type), i);
  1512. tree type1 = BINFO_TYPE (base1);
  1513. tree type2 = BINFO_TYPE (base2);
  1514. if (types_odr_comparable (type1, type2))
  1515. {
  1516. if (!types_same_for_odr (type1, type2))
  1517. base_mismatch = true;
  1518. }
  1519. else
  1520. {
  1521. hash_set<type_pair,pair_traits> visited;
  1522. if (!odr_types_equivalent_p (type1, type2, false, NULL,
  1523. &visited))
  1524. base_mismatch = true;
  1525. }
  1526. if (base_mismatch)
  1527. {
  1528. if (!warned && !val->odr_violated)
  1529. {
  1530. warn_odr (type, val->type, NULL, NULL,
  1531. !warned, &warned,
  1532. "a type with the same name but different base "
  1533. "type is defined in another translation unit");
  1534. if (warned)
  1535. warn_types_mismatch (type1, type2);
  1536. }
  1537. break;
  1538. }
  1539. if (BINFO_OFFSET (base1) != BINFO_OFFSET (base2))
  1540. {
  1541. base_mismatch = true;
  1542. if (!warned && !val->odr_violated)
  1543. warn_odr (type, val->type, NULL, NULL,
  1544. !warned, &warned,
  1545. "a type with the same name but different base "
  1546. "layout is defined in another translation unit");
  1547. break;
  1548. }
  1549. /* One of bases is not of complete type. */
  1550. if (!TYPE_BINFO (type1) != !TYPE_BINFO (type2))
  1551. {
  1552. /* If we have a polymorphic type info specified for TYPE1
  1553. but not for TYPE2 we possibly missed a base when recording
  1554. VAL->type earlier.
  1555. Be sure this does not happen. */
  1556. if (TYPE_BINFO (type1)
  1557. && polymorphic_type_binfo_p (TYPE_BINFO (type1))
  1558. && !build_bases)
  1559. odr_must_violate = true;
  1560. break;
  1561. }
  1562. /* One base is polymorphic and the other not.
  1563. This ought to be diagnosed earlier, but do not ICE in the
  1564. checking bellow. */
  1565. else if (TYPE_BINFO (type1)
  1566. && polymorphic_type_binfo_p (TYPE_BINFO (type1))
  1567. != polymorphic_type_binfo_p (TYPE_BINFO (type2)))
  1568. {
  1569. if (!warned && !val->odr_violated)
  1570. warn_odr (type, val->type, NULL, NULL,
  1571. !warned, &warned,
  1572. "a base of the type is polymorphic only in one "
  1573. "translation unit");
  1574. base_mismatch = true;
  1575. break;
  1576. }
  1577. }
  1578. if (base_mismatch)
  1579. {
  1580. merge = false;
  1581. odr_violation_reported = true;
  1582. val->odr_violated = true;
  1583. if (symtab->dump_file)
  1584. {
  1585. fprintf (symtab->dump_file, "ODR base violation\n");
  1586. print_node (symtab->dump_file, "", val->type, 0);
  1587. putc ('\n',symtab->dump_file);
  1588. print_node (symtab->dump_file, "", type, 0);
  1589. putc ('\n',symtab->dump_file);
  1590. }
  1591. }
  1592. }
  1593. /* Next compare memory layout. */
  1594. if (!odr_types_equivalent_p (val->type, type,
  1595. !flag_ltrans && !val->odr_violated && !warned,
  1596. &warned, &visited))
  1597. {
  1598. merge = false;
  1599. odr_violation_reported = true;
  1600. val->odr_violated = true;
  1601. if (symtab->dump_file)
  1602. {
  1603. fprintf (symtab->dump_file, "ODR violation\n");
  1604. print_node (symtab->dump_file, "", val->type, 0);
  1605. putc ('\n',symtab->dump_file);
  1606. print_node (symtab->dump_file, "", type, 0);
  1607. putc ('\n',symtab->dump_file);
  1608. }
  1609. }
  1610. gcc_assert (val->odr_violated || !odr_must_violate);
  1611. /* Sanity check that all bases will be build same way again. */
  1612. #ifdef ENABLE_CHECKING
  1613. if (COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (val->type)
  1614. && TREE_CODE (val->type) == RECORD_TYPE
  1615. && TREE_CODE (type) == RECORD_TYPE
  1616. && TYPE_BINFO (val->type) && TYPE_BINFO (type)
  1617. && !val->odr_violated
  1618. && !base_mismatch && val->bases.length ())
  1619. {
  1620. unsigned int num_poly_bases = 0;
  1621. unsigned int j;
  1622. for (i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
  1623. if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
  1624. (TYPE_BINFO (type), i)))
  1625. num_poly_bases++;
  1626. gcc_assert (num_poly_bases == val->bases.length ());
  1627. for (j = 0, i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type));
  1628. i++)
  1629. if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
  1630. (TYPE_BINFO (type), i)))
  1631. {
  1632. odr_type base = get_odr_type
  1633. (BINFO_TYPE
  1634. (BINFO_BASE_BINFO (TYPE_BINFO (type),
  1635. i)),
  1636. true);
  1637. gcc_assert (val->bases[j] == base);
  1638. j++;
  1639. }
  1640. }
  1641. #endif
  1642. /* Regularize things a little. During LTO same types may come with
  1643. different BINFOs. Either because their virtual table was
  1644. not merged by tree merging and only later at decl merging or
  1645. because one type comes with external vtable, while other
  1646. with internal. We want to merge equivalent binfos to conserve
  1647. memory and streaming overhead.
  1648. The external vtables are more harmful: they contain references
  1649. to external declarations of methods that may be defined in the
  1650. merged LTO unit. For this reason we absolutely need to remove
  1651. them and replace by internal variants. Not doing so will lead
  1652. to incomplete answers from possible_polymorphic_call_targets.
  1653. FIXME: disable for now; because ODR types are now build during
  1654. streaming in, the variants do not need to be linked to the type,
  1655. yet. We need to do the merging in cleanup pass to be implemented
  1656. soon. */
  1657. if (!flag_ltrans && merge
  1658. && 0
  1659. && TREE_CODE (val->type) == RECORD_TYPE
  1660. && TREE_CODE (type) == RECORD_TYPE
  1661. && TYPE_BINFO (val->type) && TYPE_BINFO (type)
  1662. && TYPE_MAIN_VARIANT (type) == type
  1663. && TYPE_MAIN_VARIANT (val->type) == val->type
  1664. && BINFO_VTABLE (TYPE_BINFO (val->type))
  1665. && BINFO_VTABLE (TYPE_BINFO (type)))
  1666. {
  1667. tree master_binfo = TYPE_BINFO (val->type);
  1668. tree v1 = BINFO_VTABLE (master_binfo);
  1669. tree v2 = BINFO_VTABLE (TYPE_BINFO (type));
  1670. if (TREE_CODE (v1) == POINTER_PLUS_EXPR)
  1671. {
  1672. gcc_assert (TREE_CODE (v2) == POINTER_PLUS_EXPR
  1673. && operand_equal_p (TREE_OPERAND (v1, 1),
  1674. TREE_OPERAND (v2, 1), 0));
  1675. v1 = TREE_OPERAND (TREE_OPERAND (v1, 0), 0);
  1676. v2 = TREE_OPERAND (TREE_OPERAND (v2, 0), 0);
  1677. }
  1678. gcc_assert (DECL_ASSEMBLER_NAME (v1)
  1679. == DECL_ASSEMBLER_NAME (v2));
  1680. if (DECL_EXTERNAL (v1) && !DECL_EXTERNAL (v2))
  1681. {
  1682. unsigned int i;
  1683. set_type_binfo (val->type, TYPE_BINFO (type));
  1684. for (i = 0; i < val->types->length (); i++)
  1685. {
  1686. if (TYPE_BINFO ((*val->types)[i])
  1687. == master_binfo)
  1688. set_type_binfo ((*val->types)[i], TYPE_BINFO (type));
  1689. }
  1690. BINFO_TYPE (TYPE_BINFO (type)) = val->type;
  1691. }
  1692. else
  1693. set_type_binfo (type, master_binfo);
  1694. }
  1695. return build_bases;
  1696. }
  1697. /* Get ODR type hash entry for TYPE. If INSERT is true, create
  1698. possibly new entry. */
  1699. odr_type
  1700. get_odr_type (tree type, bool insert)
  1701. {
  1702. odr_type_d **slot = NULL;
  1703. odr_type_d **vtable_slot = NULL;
  1704. odr_type val = NULL;
  1705. hashval_t hash;
  1706. bool build_bases = false;
  1707. bool insert_to_odr_array = false;
  1708. int base_id = -1;
  1709. type = main_odr_variant (type);
  1710. gcc_checking_assert (can_be_name_hashed_p (type)
  1711. || can_be_vtable_hashed_p (type));
  1712. /* Lookup entry, first try name hash, fallback to vtable hash. */
  1713. if (can_be_name_hashed_p (type))
  1714. {
  1715. hash = hash_odr_name (type);
  1716. slot = odr_hash->find_slot_with_hash (type, hash,
  1717. insert ? INSERT : NO_INSERT);
  1718. }
  1719. if ((!slot || !*slot) && in_lto_p && can_be_vtable_hashed_p (type))
  1720. {
  1721. hash = hash_odr_vtable (type);
  1722. vtable_slot = odr_vtable_hash->find_slot_with_hash (type, hash,
  1723. insert ? INSERT : NO_INSERT);
  1724. }
  1725. if (!slot && !vtable_slot)
  1726. return NULL;
  1727. /* See if we already have entry for type. */
  1728. if ((slot && *slot) || (vtable_slot && *vtable_slot))
  1729. {
  1730. if (slot && *slot)
  1731. {
  1732. val = *slot;
  1733. #ifdef ENABLE_CHECKING
  1734. if (in_lto_p && can_be_vtable_hashed_p (type))
  1735. {
  1736. hash = hash_odr_vtable (type);
  1737. vtable_slot = odr_vtable_hash->find_slot_with_hash (type, hash,
  1738. NO_INSERT);
  1739. gcc_assert (!vtable_slot || *vtable_slot == *slot);
  1740. vtable_slot = NULL;
  1741. }
  1742. #endif
  1743. }
  1744. else if (*vtable_slot)
  1745. val = *vtable_slot;
  1746. if (val->type != type
  1747. && (!val->types_set || !val->types_set->add (type)))
  1748. {
  1749. gcc_assert (insert);
  1750. /* We have type duplicate, but it may introduce vtable name or
  1751. mangled name; be sure to keep hashes in sync. */
  1752. if (in_lto_p && can_be_vtable_hashed_p (type)
  1753. && (!vtable_slot || !*vtable_slot))
  1754. {
  1755. if (!vtable_slot)
  1756. {
  1757. hash = hash_odr_vtable (type);
  1758. vtable_slot = odr_vtable_hash->find_slot_with_hash
  1759. (type, hash, INSERT);
  1760. gcc_checking_assert (!*vtable_slot || *vtable_slot == val);
  1761. }
  1762. *vtable_slot = val;
  1763. }
  1764. if (slot && !*slot)
  1765. *slot = val;
  1766. build_bases = add_type_duplicate (val, type);
  1767. }
  1768. }
  1769. else
  1770. {
  1771. val = ggc_cleared_alloc<odr_type_d> ();
  1772. val->type = type;
  1773. val->bases = vNULL;
  1774. val->derived_types = vNULL;
  1775. val->anonymous_namespace = type_in_anonymous_namespace_p (type);
  1776. build_bases = COMPLETE_TYPE_P (val->type);
  1777. insert_to_odr_array = true;
  1778. if (slot)
  1779. *slot = val;
  1780. if (vtable_slot)
  1781. *vtable_slot = val;
  1782. }
  1783. if (build_bases && TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type)
  1784. && type == TYPE_MAIN_VARIANT (type))
  1785. {
  1786. tree binfo = TYPE_BINFO (type);
  1787. unsigned int i;
  1788. gcc_assert (BINFO_TYPE (TYPE_BINFO (val->type)) == type);
  1789. val->all_derivations_known = type_all_derivations_known_p (type);
  1790. for (i = 0; i < BINFO_N_BASE_BINFOS (binfo); i++)
  1791. /* For now record only polymorphic types. other are
  1792. pointless for devirtualization and we can not precisely
  1793. determine ODR equivalency of these during LTO. */
  1794. if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (binfo, i)))
  1795. {
  1796. tree base_type= BINFO_TYPE (BINFO_BASE_BINFO (binfo, i));
  1797. odr_type base = get_odr_type (base_type, true);
  1798. gcc_assert (TYPE_MAIN_VARIANT (base_type) == base_type);
  1799. base->derived_types.safe_push (val);
  1800. val->bases.safe_push (base);
  1801. if (base->id > base_id)
  1802. base_id = base->id;
  1803. }
  1804. }
  1805. /* Ensure that type always appears after bases. */
  1806. if (insert_to_odr_array)
  1807. {
  1808. if (odr_types_ptr)
  1809. val->id = odr_types.length ();
  1810. vec_safe_push (odr_types_ptr, val);
  1811. }
  1812. else if (base_id > val->id)
  1813. {
  1814. odr_types[val->id] = 0;
  1815. /* Be sure we did not recorded any derived types; these may need
  1816. renumbering too. */
  1817. gcc_assert (val->derived_types.length() == 0);
  1818. if (odr_types_ptr)
  1819. val->id = odr_types.length ();
  1820. vec_safe_push (odr_types_ptr, val);
  1821. }
  1822. return val;
  1823. }
  1824. /* Add TYPE od ODR type hash. */
  1825. void
  1826. register_odr_type (tree type)
  1827. {
  1828. if (!odr_hash)
  1829. {
  1830. odr_hash = new odr_hash_type (23);
  1831. if (in_lto_p)
  1832. odr_vtable_hash = new odr_vtable_hash_type (23);
  1833. }
  1834. /* Arrange things to be nicer and insert main variants first. */
  1835. if (odr_type_p (TYPE_MAIN_VARIANT (type)))
  1836. get_odr_type (TYPE_MAIN_VARIANT (type), true);
  1837. if (TYPE_MAIN_VARIANT (type) != type)
  1838. get_odr_type (type, true);
  1839. }
  1840. /* Return true if type is known to have no derivations. */
  1841. bool
  1842. type_known_to_have_no_deriavations_p (tree t)
  1843. {
  1844. return (type_all_derivations_known_p (t)
  1845. && (TYPE_FINAL_P (t)
  1846. || (odr_hash
  1847. && !get_odr_type (t, true)->derived_types.length())));
  1848. }
  1849. /* Dump ODR type T and all its derived types. INDENT specifies indentation for
  1850. recursive printing. */
  1851. static void
  1852. dump_odr_type (FILE *f, odr_type t, int indent=0)
  1853. {
  1854. unsigned int i;
  1855. fprintf (f, "%*s type %i: ", indent * 2, "", t->id);
  1856. print_generic_expr (f, t->type, TDF_SLIM);
  1857. fprintf (f, "%s", t->anonymous_namespace ? " (anonymous namespace)":"");
  1858. fprintf (f, "%s\n", t->all_derivations_known ? " (derivations known)":"");
  1859. if (TYPE_NAME (t->type))
  1860. {
  1861. /*fprintf (f, "%*s defined at: %s:%i\n", indent * 2, "",
  1862. DECL_SOURCE_FILE (TYPE_NAME (t->type)),
  1863. DECL_SOURCE_LINE (TYPE_NAME (t->type)));*/
  1864. if (DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t->type)))
  1865. fprintf (f, "%*s mangled name: %s\n", indent * 2, "",
  1866. IDENTIFIER_POINTER
  1867. (DECL_ASSEMBLER_NAME (TYPE_NAME (t->type))));
  1868. }
  1869. if (t->bases.length ())
  1870. {
  1871. fprintf (f, "%*s base odr type ids: ", indent * 2, "");
  1872. for (i = 0; i < t->bases.length (); i++)
  1873. fprintf (f, " %i", t->bases[i]->id);
  1874. fprintf (f, "\n");
  1875. }
  1876. if (t->derived_types.length ())
  1877. {
  1878. fprintf (f, "%*s derived types:\n", indent * 2, "");
  1879. for (i = 0; i < t->derived_types.length (); i++)
  1880. dump_odr_type (f, t->derived_types[i], indent + 1);
  1881. }
  1882. fprintf (f, "\n");
  1883. }
  1884. /* Dump the type inheritance graph. */
  1885. static void
  1886. dump_type_inheritance_graph (FILE *f)
  1887. {
  1888. unsigned int i;
  1889. if (!odr_types_ptr)
  1890. return;
  1891. fprintf (f, "\n\nType inheritance graph:\n");
  1892. for (i = 0; i < odr_types.length (); i++)
  1893. {
  1894. if (odr_types[i] && odr_types[i]->bases.length () == 0)
  1895. dump_odr_type (f, odr_types[i]);
  1896. }
  1897. for (i = 0; i < odr_types.length (); i++)
  1898. {
  1899. if (odr_types[i] && odr_types[i]->types && odr_types[i]->types->length ())
  1900. {
  1901. unsigned int j;
  1902. fprintf (f, "Duplicate tree types for odr type %i\n", i);
  1903. print_node (f, "", odr_types[i]->type, 0);
  1904. for (j = 0; j < odr_types[i]->types->length (); j++)
  1905. {
  1906. tree t;
  1907. fprintf (f, "duplicate #%i\n", j);
  1908. print_node (f, "", (*odr_types[i]->types)[j], 0);
  1909. t = (*odr_types[i]->types)[j];
  1910. while (TYPE_P (t) && TYPE_CONTEXT (t))
  1911. {
  1912. t = TYPE_CONTEXT (t);
  1913. print_node (f, "", t, 0);
  1914. }
  1915. putc ('\n',f);
  1916. }
  1917. }
  1918. }
  1919. }
  1920. /* Given method type T, return type of class it belongs to.
  1921. Look up this pointer and get its type. */
  1922. tree
  1923. method_class_type (const_tree t)
  1924. {
  1925. tree first_parm_type = TREE_VALUE (TYPE_ARG_TYPES (t));
  1926. gcc_assert (TREE_CODE (t) == METHOD_TYPE);
  1927. return TREE_TYPE (first_parm_type);
  1928. }
  1929. /* Initialize IPA devirt and build inheritance tree graph. */
  1930. void
  1931. build_type_inheritance_graph (void)
  1932. {
  1933. struct symtab_node *n;
  1934. FILE *inheritance_dump_file;
  1935. int flags;
  1936. if (odr_hash)
  1937. return;
  1938. timevar_push (TV_IPA_INHERITANCE);
  1939. inheritance_dump_file = dump_begin (TDI_inheritance, &flags);
  1940. odr_hash = new odr_hash_type (23);
  1941. if (in_lto_p)
  1942. odr_vtable_hash = new odr_vtable_hash_type (23);
  1943. /* We reconstruct the graph starting of types of all methods seen in the
  1944. the unit. */
  1945. FOR_EACH_SYMBOL (n)
  1946. if (is_a <cgraph_node *> (n)
  1947. && DECL_VIRTUAL_P (n->decl)
  1948. && n->real_symbol_p ())
  1949. get_odr_type (TYPE_MAIN_VARIANT (method_class_type (TREE_TYPE (n->decl))),
  1950. true);
  1951. /* Look also for virtual tables of types that do not define any methods.
  1952. We need it in a case where class B has virtual base of class A
  1953. re-defining its virtual method and there is class C with no virtual
  1954. methods with B as virtual base.
  1955. Here we output B's virtual method in two variant - for non-virtual
  1956. and virtual inheritance. B's virtual table has non-virtual version,
  1957. while C's has virtual.
  1958. For this reason we need to know about C in order to include both
  1959. variants of B. More correctly, record_target_from_binfo should
  1960. add both variants of the method when walking B, but we have no
  1961. link in between them.
  1962. We rely on fact that either the method is exported and thus we
  1963. assume it is called externally or C is in anonymous namespace and
  1964. thus we will see the vtable. */
  1965. else if (is_a <varpool_node *> (n)
  1966. && DECL_VIRTUAL_P (n->decl)
  1967. && TREE_CODE (DECL_CONTEXT (n->decl)) == RECORD_TYPE
  1968. && TYPE_BINFO (DECL_CONTEXT (n->decl))
  1969. && polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (n->decl))))
  1970. get_odr_type (TYPE_MAIN_VARIANT (DECL_CONTEXT (n->decl)), true);
  1971. if (inheritance_dump_file)
  1972. {
  1973. dump_type_inheritance_graph (inheritance_dump_file);
  1974. dump_end (TDI_inheritance, inheritance_dump_file);
  1975. }
  1976. timevar_pop (TV_IPA_INHERITANCE);
  1977. }
  1978. /* Return true if N has reference from live virtual table
  1979. (and thus can be a destination of polymorphic call).
  1980. Be conservatively correct when callgraph is not built or
  1981. if the method may be referred externally. */
  1982. static bool
  1983. referenced_from_vtable_p (struct cgraph_node *node)
  1984. {
  1985. int i;
  1986. struct ipa_ref *ref;
  1987. bool found = false;
  1988. if (node->externally_visible
  1989. || DECL_EXTERNAL (node->decl)
  1990. || node->used_from_other_partition)
  1991. return true;
  1992. /* Keep this test constant time.
  1993. It is unlikely this can happen except for the case where speculative
  1994. devirtualization introduced many speculative edges to this node.
  1995. In this case the target is very likely alive anyway. */
  1996. if (node->ref_list.referring.length () > 100)
  1997. return true;
  1998. /* We need references built. */
  1999. if (symtab->state <= CONSTRUCTION)
  2000. return true;
  2001. for (i = 0; node->iterate_referring (i, ref); i++)
  2002. if ((ref->use == IPA_REF_ALIAS
  2003. && referenced_from_vtable_p (dyn_cast<cgraph_node *> (ref->referring)))
  2004. || (ref->use == IPA_REF_ADDR
  2005. && TREE_CODE (ref->referring->decl) == VAR_DECL
  2006. && DECL_VIRTUAL_P (ref->referring->decl)))
  2007. {
  2008. found = true;
  2009. break;
  2010. }
  2011. return found;
  2012. }
  2013. /* If TARGET has associated node, record it in the NODES array.
  2014. CAN_REFER specify if program can refer to the target directly.
  2015. if TARGET is unknown (NULL) or it can not be inserted (for example because
  2016. its body was already removed and there is no way to refer to it), clear
  2017. COMPLETEP. */
  2018. static void
  2019. maybe_record_node (vec <cgraph_node *> &nodes,
  2020. tree target, hash_set<tree> *inserted,
  2021. bool can_refer,
  2022. bool *completep)
  2023. {
  2024. struct cgraph_node *target_node, *alias_target;
  2025. enum availability avail;
  2026. /* cxa_pure_virtual and __builtin_unreachable do not need to be added into
  2027. list of targets; the runtime effect of calling them is undefined.
  2028. Only "real" virtual methods should be accounted. */
  2029. if (target && TREE_CODE (TREE_TYPE (target)) != METHOD_TYPE)
  2030. return;
  2031. if (!can_refer)
  2032. {
  2033. /* The only case when method of anonymous namespace becomes unreferable
  2034. is when we completely optimized it out. */
  2035. if (flag_ltrans
  2036. || !target
  2037. || !type_in_anonymous_namespace_p (DECL_CONTEXT (target)))
  2038. *completep = false;
  2039. return;
  2040. }
  2041. if (!target)
  2042. return;
  2043. target_node = cgraph_node::get (target);
  2044. /* Prefer alias target over aliases, so we do not get confused by
  2045. fake duplicates. */
  2046. if (target_node)
  2047. {
  2048. alias_target = target_node->ultimate_alias_target (&avail);
  2049. if (target_node != alias_target
  2050. && avail >= AVAIL_AVAILABLE
  2051. && target_node->get_availability ())
  2052. target_node = alias_target;
  2053. }
  2054. /* Method can only be called by polymorphic call if any
  2055. of vtables referring to it are alive.
  2056. While this holds for non-anonymous functions, too, there are
  2057. cases where we want to keep them in the list; for example
  2058. inline functions with -fno-weak are static, but we still
  2059. may devirtualize them when instance comes from other unit.
  2060. The same holds for LTO.
  2061. Currently we ignore these functions in speculative devirtualization.
  2062. ??? Maybe it would make sense to be more aggressive for LTO even
  2063. elsewhere. */
  2064. if (!flag_ltrans
  2065. && type_in_anonymous_namespace_p (DECL_CONTEXT (target))
  2066. && (!target_node
  2067. || !referenced_from_vtable_p (target_node)))
  2068. ;
  2069. /* See if TARGET is useful function we can deal with. */
  2070. else if (target_node != NULL
  2071. && (TREE_PUBLIC (target)
  2072. || DECL_EXTERNAL (target)
  2073. || target_node->definition)
  2074. && target_node->real_symbol_p ())
  2075. {
  2076. gcc_assert (!target_node->global.inlined_to);
  2077. gcc_assert (target_node->real_symbol_p ());
  2078. if (!inserted->add (target))
  2079. {
  2080. cached_polymorphic_call_targets->add (target_node);
  2081. nodes.safe_push (target_node);
  2082. }
  2083. }
  2084. else if (completep
  2085. && (!type_in_anonymous_namespace_p
  2086. (DECL_CONTEXT (target))
  2087. || flag_ltrans))
  2088. *completep = false;
  2089. }
  2090. /* See if BINFO's type matches OUTER_TYPE. If so, look up
  2091. BINFO of subtype of OTR_TYPE at OFFSET and in that BINFO find
  2092. method in vtable and insert method to NODES array
  2093. or BASES_TO_CONSIDER if this array is non-NULL.
  2094. Otherwise recurse to base BINFOs.
  2095. This matches what get_binfo_at_offset does, but with offset
  2096. being unknown.
  2097. TYPE_BINFOS is a stack of BINFOS of types with defined
  2098. virtual table seen on way from class type to BINFO.
  2099. MATCHED_VTABLES tracks virtual tables we already did lookup
  2100. for virtual function in. INSERTED tracks nodes we already
  2101. inserted.
  2102. ANONYMOUS is true if BINFO is part of anonymous namespace.
  2103. Clear COMPLETEP when we hit unreferable target.
  2104. */
  2105. static void
  2106. record_target_from_binfo (vec <cgraph_node *> &nodes,
  2107. vec <tree> *bases_to_consider,
  2108. tree binfo,
  2109. tree otr_type,
  2110. vec <tree> &type_binfos,
  2111. HOST_WIDE_INT otr_token,
  2112. tree outer_type,
  2113. HOST_WIDE_INT offset,
  2114. hash_set<tree> *inserted,
  2115. hash_set<tree> *matched_vtables,
  2116. bool anonymous,
  2117. bool *completep)
  2118. {
  2119. tree type = BINFO_TYPE (binfo);
  2120. int i;
  2121. tree base_binfo;
  2122. if (BINFO_VTABLE (binfo))
  2123. type_binfos.safe_push (binfo);
  2124. if (types_same_for_odr (type, outer_type))
  2125. {
  2126. int i;
  2127. tree type_binfo = NULL;
  2128. /* Look up BINFO with virtual table. For normal types it is always last
  2129. binfo on stack. */
  2130. for (i = type_binfos.length () - 1; i >= 0; i--)
  2131. if (BINFO_OFFSET (type_binfos[i]) == BINFO_OFFSET (binfo))
  2132. {
  2133. type_binfo = type_binfos[i];
  2134. break;
  2135. }
  2136. if (BINFO_VTABLE (binfo))
  2137. type_binfos.pop ();
  2138. /* If this is duplicated BINFO for base shared by virtual inheritance,
  2139. we may not have its associated vtable. This is not a problem, since
  2140. we will walk it on the other path. */
  2141. if (!type_binfo)
  2142. return;
  2143. tree inner_binfo = get_binfo_at_offset (type_binfo,
  2144. offset, otr_type);
  2145. if (!inner_binfo)
  2146. {
  2147. gcc_assert (odr_violation_reported);
  2148. return;
  2149. }
  2150. /* For types in anonymous namespace first check if the respective vtable
  2151. is alive. If not, we know the type can't be called. */
  2152. if (!flag_ltrans && anonymous)
  2153. {
  2154. tree vtable = BINFO_VTABLE (inner_binfo);
  2155. varpool_node *vnode;
  2156. if (TREE_CODE (vtable) == POINTER_PLUS_EXPR)
  2157. vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0);
  2158. vnode = varpool_node::get (vtable);
  2159. if (!vnode || !vnode->definition)
  2160. return;
  2161. }
  2162. gcc_assert (inner_binfo);
  2163. if (bases_to_consider
  2164. ? !matched_vtables->contains (BINFO_VTABLE (inner_binfo))
  2165. : !matched_vtables->add (BINFO_VTABLE (inner_binfo)))
  2166. {
  2167. bool can_refer;
  2168. tree target = gimple_get_virt_method_for_binfo (otr_token,
  2169. inner_binfo,
  2170. &can_refer);
  2171. if (!bases_to_consider)
  2172. maybe_record_node (nodes, target, inserted, can_refer, completep);
  2173. /* Destructors are never called via construction vtables. */
  2174. else if (!target || !DECL_CXX_DESTRUCTOR_P (target))
  2175. bases_to_consider->safe_push (target);
  2176. }
  2177. return;
  2178. }
  2179. /* Walk bases. */
  2180. for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
  2181. /* Walking bases that have no virtual method is pointless exercise. */
  2182. if (polymorphic_type_binfo_p (base_binfo))
  2183. record_target_from_binfo (nodes, bases_to_consider, base_binfo, otr_type,
  2184. type_binfos,
  2185. otr_token, outer_type, offset, inserted,
  2186. matched_vtables, anonymous, completep);
  2187. if (BINFO_VTABLE (binfo))
  2188. type_binfos.pop ();
  2189. }
  2190. /* Look up virtual methods matching OTR_TYPE (with OFFSET and OTR_TOKEN)
  2191. of TYPE, insert them to NODES, recurse into derived nodes.
  2192. INSERTED is used to avoid duplicate insertions of methods into NODES.
  2193. MATCHED_VTABLES are used to avoid duplicate walking vtables.
  2194. Clear COMPLETEP if unreferable target is found.
  2195. If CONSIDER_CONSTRUCTION is true, record to BASES_TO_CONSIDER
  2196. all cases where BASE_SKIPPED is true (because the base is abstract
  2197. class). */
  2198. static void
  2199. possible_polymorphic_call_targets_1 (vec <cgraph_node *> &nodes,
  2200. hash_set<tree> *inserted,
  2201. hash_set<tree> *matched_vtables,
  2202. tree otr_type,
  2203. odr_type type,
  2204. HOST_WIDE_INT otr_token,
  2205. tree outer_type,
  2206. HOST_WIDE_INT offset,
  2207. bool *completep,
  2208. vec <tree> &bases_to_consider,
  2209. bool consider_construction)
  2210. {
  2211. tree binfo = TYPE_BINFO (type->type);
  2212. unsigned int i;
  2213. auto_vec <tree, 8> type_binfos;
  2214. bool possibly_instantiated = type_possibly_instantiated_p (type->type);
  2215. /* We may need to consider types w/o instances because of possible derived
  2216. types using their methods either directly or via construction vtables.
  2217. We are safe to skip them when all derivations are known, since we will
  2218. handle them later.
  2219. This is done by recording them to BASES_TO_CONSIDER array. */
  2220. if (possibly_instantiated || consider_construction)
  2221. {
  2222. record_target_from_binfo (nodes,
  2223. (!possibly_instantiated
  2224. && type_all_derivations_known_p (type->type))
  2225. ? &bases_to_consider : NULL,
  2226. binfo, otr_type, type_binfos, otr_token,
  2227. outer_type, offset,
  2228. inserted, matched_vtables,
  2229. type->anonymous_namespace, completep);
  2230. }
  2231. for (i = 0; i < type->derived_types.length (); i++)
  2232. possible_polymorphic_call_targets_1 (nodes, inserted,
  2233. matched_vtables,
  2234. otr_type,
  2235. type->derived_types[i],
  2236. otr_token, outer_type, offset, completep,
  2237. bases_to_consider, consider_construction);
  2238. }
  2239. /* Cache of queries for polymorphic call targets.
  2240. Enumerating all call targets may get expensive when there are many
  2241. polymorphic calls in the program, so we memoize all the previous
  2242. queries and avoid duplicated work. */
  2243. struct polymorphic_call_target_d
  2244. {
  2245. HOST_WIDE_INT otr_token;
  2246. ipa_polymorphic_call_context context;
  2247. odr_type type;
  2248. vec <cgraph_node *> targets;
  2249. tree decl_warning;
  2250. int type_warning;
  2251. bool complete;
  2252. bool speculative;
  2253. };
  2254. /* Polymorphic call target cache helpers. */
  2255. struct polymorphic_call_target_hasher
  2256. {
  2257. typedef polymorphic_call_target_d value_type;
  2258. typedef polymorphic_call_target_d compare_type;
  2259. static inline hashval_t hash (const value_type *);
  2260. static inline bool equal (const value_type *, const compare_type *);
  2261. static inline void remove (value_type *);
  2262. };
  2263. /* Return the computed hashcode for ODR_QUERY. */
  2264. inline hashval_t
  2265. polymorphic_call_target_hasher::hash (const value_type *odr_query)
  2266. {
  2267. inchash::hash hstate (odr_query->otr_token);
  2268. hstate.add_wide_int (odr_query->type->id);
  2269. hstate.merge_hash (TYPE_UID (odr_query->context.outer_type));
  2270. hstate.add_wide_int (odr_query->context.offset);
  2271. if (odr_query->context.speculative_outer_type)
  2272. {
  2273. hstate.merge_hash (TYPE_UID (odr_query->context.speculative_outer_type));
  2274. hstate.add_wide_int (odr_query->context.speculative_offset);
  2275. }
  2276. hstate.add_flag (odr_query->speculative);
  2277. hstate.add_flag (odr_query->context.maybe_in_construction);
  2278. hstate.add_flag (odr_query->context.maybe_derived_type);
  2279. hstate.add_flag (odr_query->context.speculative_maybe_derived_type);
  2280. hstate.commit_flag ();
  2281. return hstate.end ();
  2282. }
  2283. /* Compare cache entries T1 and T2. */
  2284. inline bool
  2285. polymorphic_call_target_hasher::equal (const value_type *t1,
  2286. const compare_type *t2)
  2287. {
  2288. return (t1->type == t2->type && t1->otr_token == t2->otr_token
  2289. && t1->speculative == t2->speculative
  2290. && t1->context.offset == t2->context.offset
  2291. && t1->context.speculative_offset == t2->context.speculative_offset
  2292. && t1->context.outer_type == t2->context.outer_type
  2293. && t1->context.speculative_outer_type == t2->context.speculative_outer_type
  2294. && t1->context.maybe_in_construction
  2295. == t2->context.maybe_in_construction
  2296. && t1->context.maybe_derived_type == t2->context.maybe_derived_type
  2297. && (t1->context.speculative_maybe_derived_type
  2298. == t2->context.speculative_maybe_derived_type));
  2299. }
  2300. /* Remove entry in polymorphic call target cache hash. */
  2301. inline void
  2302. polymorphic_call_target_hasher::remove (value_type *v)
  2303. {
  2304. v->targets.release ();
  2305. free (v);
  2306. }
  2307. /* Polymorphic call target query cache. */
  2308. typedef hash_table<polymorphic_call_target_hasher>
  2309. polymorphic_call_target_hash_type;
  2310. static polymorphic_call_target_hash_type *polymorphic_call_target_hash;
  2311. /* Destroy polymorphic call target query cache. */
  2312. static void
  2313. free_polymorphic_call_targets_hash ()
  2314. {
  2315. if (cached_polymorphic_call_targets)
  2316. {
  2317. delete polymorphic_call_target_hash;
  2318. polymorphic_call_target_hash = NULL;
  2319. delete cached_polymorphic_call_targets;
  2320. cached_polymorphic_call_targets = NULL;
  2321. }
  2322. }
  2323. /* When virtual function is removed, we may need to flush the cache. */
  2324. static void
  2325. devirt_node_removal_hook (struct cgraph_node *n, void *d ATTRIBUTE_UNUSED)
  2326. {
  2327. if (cached_polymorphic_call_targets
  2328. && cached_polymorphic_call_targets->contains (n))
  2329. free_polymorphic_call_targets_hash ();
  2330. }
  2331. /* Look up base of BINFO that has virtual table VTABLE with OFFSET. */
  2332. tree
  2333. subbinfo_with_vtable_at_offset (tree binfo, unsigned HOST_WIDE_INT offset,
  2334. tree vtable)
  2335. {
  2336. tree v = BINFO_VTABLE (binfo);
  2337. int i;
  2338. tree base_binfo;
  2339. unsigned HOST_WIDE_INT this_offset;
  2340. if (v)
  2341. {
  2342. if (!vtable_pointer_value_to_vtable (v, &v, &this_offset))
  2343. gcc_unreachable ();
  2344. if (offset == this_offset
  2345. && DECL_ASSEMBLER_NAME (v) == DECL_ASSEMBLER_NAME (vtable))
  2346. return binfo;
  2347. }
  2348. for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
  2349. if (polymorphic_type_binfo_p (base_binfo))
  2350. {
  2351. base_binfo = subbinfo_with_vtable_at_offset (base_binfo, offset, vtable);
  2352. if (base_binfo)
  2353. return base_binfo;
  2354. }
  2355. return NULL;
  2356. }
  2357. /* T is known constant value of virtual table pointer.
  2358. Store virtual table to V and its offset to OFFSET.
  2359. Return false if T does not look like virtual table reference. */
  2360. bool
  2361. vtable_pointer_value_to_vtable (const_tree t, tree *v,
  2362. unsigned HOST_WIDE_INT *offset)
  2363. {
  2364. /* We expect &MEM[(void *)&virtual_table + 16B].
  2365. We obtain object's BINFO from the context of the virtual table.
  2366. This one contains pointer to virtual table represented via
  2367. POINTER_PLUS_EXPR. Verify that this pointer matches what
  2368. we propagated through.
  2369. In the case of virtual inheritance, the virtual tables may
  2370. be nested, i.e. the offset may be different from 16 and we may
  2371. need to dive into the type representation. */
  2372. if (TREE_CODE (t) == ADDR_EXPR
  2373. && TREE_CODE (TREE_OPERAND (t, 0)) == MEM_REF
  2374. && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == ADDR_EXPR
  2375. && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 1)) == INTEGER_CST
  2376. && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0))
  2377. == VAR_DECL)
  2378. && DECL_VIRTUAL_P (TREE_OPERAND (TREE_OPERAND
  2379. (TREE_OPERAND (t, 0), 0), 0)))
  2380. {
  2381. *v = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0);
  2382. *offset = tree_to_uhwi (TREE_OPERAND (TREE_OPERAND (t, 0), 1));
  2383. return true;
  2384. }
  2385. /* Alternative representation, used by C++ frontend is POINTER_PLUS_EXPR.
  2386. We need to handle it when T comes from static variable initializer or
  2387. BINFO. */
  2388. if (TREE_CODE (t) == POINTER_PLUS_EXPR)
  2389. {
  2390. *offset = tree_to_uhwi (TREE_OPERAND (t, 1));
  2391. t = TREE_OPERAND (t, 0);
  2392. }
  2393. else
  2394. *offset = 0;
  2395. if (TREE_CODE (t) != ADDR_EXPR)
  2396. return false;
  2397. *v = TREE_OPERAND (t, 0);
  2398. return true;
  2399. }
  2400. /* T is known constant value of virtual table pointer. Return BINFO of the
  2401. instance type. */
  2402. tree
  2403. vtable_pointer_value_to_binfo (const_tree t)
  2404. {
  2405. tree vtable;
  2406. unsigned HOST_WIDE_INT offset;
  2407. if (!vtable_pointer_value_to_vtable (t, &vtable, &offset))
  2408. return NULL_TREE;
  2409. /* FIXME: for stores of construction vtables we return NULL,
  2410. because we do not have BINFO for those. Eventually we should fix
  2411. our representation to allow this case to be handled, too.
  2412. In the case we see store of BINFO we however may assume
  2413. that standard folding will be able to cope with it. */
  2414. return subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable)),
  2415. offset, vtable);
  2416. }
  2417. /* Walk bases of OUTER_TYPE that contain OTR_TYPE at OFFSET.
  2418. Look up their respective virtual methods for OTR_TOKEN and OTR_TYPE
  2419. and insert them in NODES.
  2420. MATCHED_VTABLES and INSERTED is used to avoid duplicated work. */
  2421. static void
  2422. record_targets_from_bases (tree otr_type,
  2423. HOST_WIDE_INT otr_token,
  2424. tree outer_type,
  2425. HOST_WIDE_INT offset,
  2426. vec <cgraph_node *> &nodes,
  2427. hash_set<tree> *inserted,
  2428. hash_set<tree> *matched_vtables,
  2429. bool *completep)
  2430. {
  2431. while (true)
  2432. {
  2433. HOST_WIDE_INT pos, size;
  2434. tree base_binfo;
  2435. tree fld;
  2436. if (types_same_for_odr (outer_type, otr_type))
  2437. return;
  2438. for (fld = TYPE_FIELDS (outer_type); fld; fld = DECL_CHAIN (fld))
  2439. {
  2440. if (TREE_CODE (fld) != FIELD_DECL)
  2441. continue;
  2442. pos = int_bit_position (fld);
  2443. size = tree_to_shwi (DECL_SIZE (fld));
  2444. if (pos <= offset && (pos + size) > offset
  2445. /* Do not get confused by zero sized bases. */
  2446. && polymorphic_type_binfo_p (TYPE_BINFO (TREE_TYPE (fld))))
  2447. break;
  2448. }
  2449. /* Within a class type we should always find corresponding fields. */
  2450. gcc_assert (fld && TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE);
  2451. /* Nonbase types should have been stripped by outer_class_type. */
  2452. gcc_assert (DECL_ARTIFICIAL (fld));
  2453. outer_type = TREE_TYPE (fld);
  2454. offset -= pos;
  2455. base_binfo = get_binfo_at_offset (TYPE_BINFO (outer_type),
  2456. offset, otr_type);
  2457. if (!base_binfo)
  2458. {
  2459. gcc_assert (odr_violation_reported);
  2460. return;
  2461. }
  2462. gcc_assert (base_binfo);
  2463. if (!matched_vtables->add (BINFO_VTABLE (base_binfo)))
  2464. {
  2465. bool can_refer;
  2466. tree target = gimple_get_virt_method_for_binfo (otr_token,
  2467. base_binfo,
  2468. &can_refer);
  2469. if (!target || ! DECL_CXX_DESTRUCTOR_P (target))
  2470. maybe_record_node (nodes, target, inserted, can_refer, completep);
  2471. matched_vtables->add (BINFO_VTABLE (base_binfo));
  2472. }
  2473. }
  2474. }
  2475. /* When virtual table is removed, we may need to flush the cache. */
  2476. static void
  2477. devirt_variable_node_removal_hook (varpool_node *n,
  2478. void *d ATTRIBUTE_UNUSED)
  2479. {
  2480. if (cached_polymorphic_call_targets
  2481. && DECL_VIRTUAL_P (n->decl)
  2482. && type_in_anonymous_namespace_p (DECL_CONTEXT (n->decl)))
  2483. free_polymorphic_call_targets_hash ();
  2484. }
  2485. /* Record about how many calls would benefit from given type to be final. */
  2486. struct odr_type_warn_count
  2487. {
  2488. tree type;
  2489. int count;
  2490. gcov_type dyn_count;
  2491. };
  2492. /* Record about how many calls would benefit from given method to be final. */
  2493. struct decl_warn_count
  2494. {
  2495. tree decl;
  2496. int count;
  2497. gcov_type dyn_count;
  2498. };
  2499. /* Information about type and decl warnings. */
  2500. struct final_warning_record
  2501. {
  2502. gcov_type dyn_count;
  2503. vec<odr_type_warn_count> type_warnings;
  2504. hash_map<tree, decl_warn_count> decl_warnings;
  2505. };
  2506. struct final_warning_record *final_warning_records;
  2507. /* Return vector containing possible targets of polymorphic call of type
  2508. OTR_TYPE calling method OTR_TOKEN within type of OTR_OUTER_TYPE and OFFSET.
  2509. If INCLUDE_BASES is true, walk also base types of OUTER_TYPES containing
  2510. OTR_TYPE and include their virtual method. This is useful for types
  2511. possibly in construction or destruction where the virtual table may
  2512. temporarily change to one of base types. INCLUDE_DERIVER_TYPES make
  2513. us to walk the inheritance graph for all derivations.
  2514. If COMPLETEP is non-NULL, store true if the list is complete.
  2515. CACHE_TOKEN (if non-NULL) will get stored to an unique ID of entry
  2516. in the target cache. If user needs to visit every target list
  2517. just once, it can memoize them.
  2518. If SPECULATIVE is set, the list will not contain targets that
  2519. are not speculatively taken.
  2520. Returned vector is placed into cache. It is NOT caller's responsibility
  2521. to free it. The vector can be freed on cgraph_remove_node call if
  2522. the particular node is a virtual function present in the cache. */
  2523. vec <cgraph_node *>
  2524. possible_polymorphic_call_targets (tree otr_type,
  2525. HOST_WIDE_INT otr_token,
  2526. ipa_polymorphic_call_context context,
  2527. bool *completep,
  2528. void **cache_token,
  2529. bool speculative)
  2530. {
  2531. static struct cgraph_node_hook_list *node_removal_hook_holder;
  2532. vec <cgraph_node *> nodes = vNULL;
  2533. auto_vec <tree, 8> bases_to_consider;
  2534. odr_type type, outer_type;
  2535. polymorphic_call_target_d key;
  2536. polymorphic_call_target_d **slot;
  2537. unsigned int i;
  2538. tree binfo, target;
  2539. bool complete;
  2540. bool can_refer = false;
  2541. bool skipped = false;
  2542. otr_type = TYPE_MAIN_VARIANT (otr_type);
  2543. /* If ODR is not initialized or the context is invalid, return empty
  2544. incomplete list. */
  2545. if (!odr_hash || context.invalid || !TYPE_BINFO (otr_type))
  2546. {
  2547. if (completep)
  2548. *completep = context.invalid;
  2549. if (cache_token)
  2550. *cache_token = NULL;
  2551. return nodes;
  2552. }
  2553. /* Do not bother to compute speculative info when user do not asks for it. */
  2554. if (!speculative || !context.speculative_outer_type)
  2555. context.clear_speculation ();
  2556. type = get_odr_type (otr_type, true);
  2557. /* Recording type variants would waste results cache. */
  2558. gcc_assert (!context.outer_type
  2559. || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type);
  2560. /* Look up the outer class type we want to walk.
  2561. If we fail to do so, the context is invalid. */
  2562. if ((context.outer_type || context.speculative_outer_type)
  2563. && !context.restrict_to_inner_class (otr_type))
  2564. {
  2565. if (completep)
  2566. *completep = true;
  2567. if (cache_token)
  2568. *cache_token = NULL;
  2569. return nodes;
  2570. }
  2571. gcc_assert (!context.invalid);
  2572. /* Check that restrict_to_inner_class kept the main variant. */
  2573. gcc_assert (!context.outer_type
  2574. || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type);
  2575. /* We canonicalize our query, so we do not need extra hashtable entries. */
  2576. /* Without outer type, we have no use for offset. Just do the
  2577. basic search from inner type. */
  2578. if (!context.outer_type)
  2579. context.clear_outer_type (otr_type);
  2580. /* We need to update our hierarchy if the type does not exist. */
  2581. outer_type = get_odr_type (context.outer_type, true);
  2582. /* If the type is complete, there are no derivations. */
  2583. if (TYPE_FINAL_P (outer_type->type))
  2584. context.maybe_derived_type = false;
  2585. /* Initialize query cache. */
  2586. if (!cached_polymorphic_call_targets)
  2587. {
  2588. cached_polymorphic_call_targets = new hash_set<cgraph_node *>;
  2589. polymorphic_call_target_hash
  2590. = new polymorphic_call_target_hash_type (23);
  2591. if (!node_removal_hook_holder)
  2592. {
  2593. node_removal_hook_holder =
  2594. symtab->add_cgraph_removal_hook (&devirt_node_removal_hook, NULL);
  2595. symtab->add_varpool_removal_hook (&devirt_variable_node_removal_hook,
  2596. NULL);
  2597. }
  2598. }
  2599. if (in_lto_p)
  2600. {
  2601. if (context.outer_type != otr_type)
  2602. context.outer_type
  2603. = get_odr_type (context.outer_type, true)->type;
  2604. if (context.speculative_outer_type)
  2605. context.speculative_outer_type
  2606. = get_odr_type (context.speculative_outer_type, true)->type;
  2607. }
  2608. /* Look up cached answer. */
  2609. key.type = type;
  2610. key.otr_token = otr_token;
  2611. key.speculative = speculative;
  2612. key.context = context;
  2613. slot = polymorphic_call_target_hash->find_slot (&key, INSERT);
  2614. if (cache_token)
  2615. *cache_token = (void *)*slot;
  2616. if (*slot)
  2617. {
  2618. if (completep)
  2619. *completep = (*slot)->complete;
  2620. if ((*slot)->type_warning && final_warning_records)
  2621. {
  2622. final_warning_records->type_warnings[(*slot)->type_warning - 1].count++;
  2623. final_warning_records->type_warnings[(*slot)->type_warning - 1].dyn_count
  2624. += final_warning_records->dyn_count;
  2625. }
  2626. if (!speculative && (*slot)->decl_warning && final_warning_records)
  2627. {
  2628. struct decl_warn_count *c =
  2629. final_warning_records->decl_warnings.get ((*slot)->decl_warning);
  2630. c->count++;
  2631. c->dyn_count += final_warning_records->dyn_count;
  2632. }
  2633. return (*slot)->targets;
  2634. }
  2635. complete = true;
  2636. /* Do actual search. */
  2637. timevar_push (TV_IPA_VIRTUAL_CALL);
  2638. *slot = XCNEW (polymorphic_call_target_d);
  2639. if (cache_token)
  2640. *cache_token = (void *)*slot;
  2641. (*slot)->type = type;
  2642. (*slot)->otr_token = otr_token;
  2643. (*slot)->context = context;
  2644. (*slot)->speculative = speculative;
  2645. hash_set<tree> inserted;
  2646. hash_set<tree> matched_vtables;
  2647. /* First insert targets we speculatively identified as likely. */
  2648. if (context.speculative_outer_type)
  2649. {
  2650. odr_type speculative_outer_type;
  2651. bool speculation_complete = true;
  2652. /* First insert target from type itself and check if it may have
  2653. derived types. */
  2654. speculative_outer_type = get_odr_type (context.speculative_outer_type, true);
  2655. if (TYPE_FINAL_P (speculative_outer_type->type))
  2656. context.speculative_maybe_derived_type = false;
  2657. binfo = get_binfo_at_offset (TYPE_BINFO (speculative_outer_type->type),
  2658. context.speculative_offset, otr_type);
  2659. if (binfo)
  2660. target = gimple_get_virt_method_for_binfo (otr_token, binfo,
  2661. &can_refer);
  2662. else
  2663. target = NULL;
  2664. /* In the case we get complete method, we don't need
  2665. to walk derivations. */
  2666. if (target && DECL_FINAL_P (target))
  2667. context.speculative_maybe_derived_type = false;
  2668. if (type_possibly_instantiated_p (speculative_outer_type->type))
  2669. maybe_record_node (nodes, target, &inserted, can_refer, &speculation_complete);
  2670. if (binfo)
  2671. matched_vtables.add (BINFO_VTABLE (binfo));
  2672. /* Next walk recursively all derived types. */
  2673. if (context.speculative_maybe_derived_type)
  2674. for (i = 0; i < speculative_outer_type->derived_types.length(); i++)
  2675. possible_polymorphic_call_targets_1 (nodes, &inserted,
  2676. &matched_vtables,
  2677. otr_type,
  2678. speculative_outer_type->derived_types[i],
  2679. otr_token, speculative_outer_type->type,
  2680. context.speculative_offset,
  2681. &speculation_complete,
  2682. bases_to_consider,
  2683. false);
  2684. }
  2685. if (!speculative || !nodes.length ())
  2686. {
  2687. /* First see virtual method of type itself. */
  2688. binfo = get_binfo_at_offset (TYPE_BINFO (outer_type->type),
  2689. context.offset, otr_type);
  2690. if (binfo)
  2691. target = gimple_get_virt_method_for_binfo (otr_token, binfo,
  2692. &can_refer);
  2693. else
  2694. {
  2695. gcc_assert (odr_violation_reported);
  2696. target = NULL;
  2697. }
  2698. /* Destructors are never called through construction virtual tables,
  2699. because the type is always known. */
  2700. if (target && DECL_CXX_DESTRUCTOR_P (target))
  2701. context.maybe_in_construction = false;
  2702. if (target)
  2703. {
  2704. /* In the case we get complete method, we don't need
  2705. to walk derivations. */
  2706. if (DECL_FINAL_P (target))
  2707. context.maybe_derived_type = false;
  2708. }
  2709. /* If OUTER_TYPE is abstract, we know we are not seeing its instance. */
  2710. if (type_possibly_instantiated_p (outer_type->type))
  2711. maybe_record_node (nodes, target, &inserted, can_refer, &complete);
  2712. else
  2713. skipped = true;
  2714. if (binfo)
  2715. matched_vtables.add (BINFO_VTABLE (binfo));
  2716. /* Next walk recursively all derived types. */
  2717. if (context.maybe_derived_type)
  2718. {
  2719. for (i = 0; i < outer_type->derived_types.length(); i++)
  2720. possible_polymorphic_call_targets_1 (nodes, &inserted,
  2721. &matched_vtables,
  2722. otr_type,
  2723. outer_type->derived_types[i],
  2724. otr_token, outer_type->type,
  2725. context.offset, &complete,
  2726. bases_to_consider,
  2727. context.maybe_in_construction);
  2728. if (!outer_type->all_derivations_known)
  2729. {
  2730. if (!speculative && final_warning_records)
  2731. {
  2732. if (complete
  2733. && nodes.length () == 1
  2734. && warn_suggest_final_types
  2735. && !outer_type->derived_types.length ())
  2736. {
  2737. if (outer_type->id >= (int)final_warning_records->type_warnings.length ())
  2738. final_warning_records->type_warnings.safe_grow_cleared
  2739. (odr_types.length ());
  2740. final_warning_records->type_warnings[outer_type->id].count++;
  2741. final_warning_records->type_warnings[outer_type->id].dyn_count
  2742. += final_warning_records->dyn_count;
  2743. final_warning_records->type_warnings[outer_type->id].type
  2744. = outer_type->type;
  2745. (*slot)->type_warning = outer_type->id + 1;
  2746. }
  2747. if (complete
  2748. && warn_suggest_final_methods
  2749. && nodes.length () == 1
  2750. && types_same_for_odr (DECL_CONTEXT (nodes[0]->decl),
  2751. outer_type->type))
  2752. {
  2753. bool existed;
  2754. struct decl_warn_count &c =
  2755. final_warning_records->decl_warnings.get_or_insert
  2756. (nodes[0]->decl, &existed);
  2757. if (existed)
  2758. {
  2759. c.count++;
  2760. c.dyn_count += final_warning_records->dyn_count;
  2761. }
  2762. else
  2763. {
  2764. c.count = 1;
  2765. c.dyn_count = final_warning_records->dyn_count;
  2766. c.decl = nodes[0]->decl;
  2767. }
  2768. (*slot)->decl_warning = nodes[0]->decl;
  2769. }
  2770. }
  2771. complete = false;
  2772. }
  2773. }
  2774. if (!speculative)
  2775. {
  2776. /* Destructors are never called through construction virtual tables,
  2777. because the type is always known. One of entries may be
  2778. cxa_pure_virtual so look to at least two of them. */
  2779. if (context.maybe_in_construction)
  2780. for (i =0 ; i < MIN (nodes.length (), 2); i++)
  2781. if (DECL_CXX_DESTRUCTOR_P (nodes[i]->decl))
  2782. context.maybe_in_construction = false;
  2783. if (context.maybe_in_construction)
  2784. {
  2785. if (type != outer_type
  2786. && (!skipped
  2787. || (context.maybe_derived_type
  2788. && !type_all_derivations_known_p (outer_type->type))))
  2789. record_targets_from_bases (otr_type, otr_token, outer_type->type,
  2790. context.offset, nodes, &inserted,
  2791. &matched_vtables, &complete);
  2792. if (skipped)
  2793. maybe_record_node (nodes, target, &inserted, can_refer, &complete);
  2794. for (i = 0; i < bases_to_consider.length(); i++)
  2795. maybe_record_node (nodes, bases_to_consider[i], &inserted, can_refer, &complete);
  2796. }
  2797. }
  2798. }
  2799. (*slot)->targets = nodes;
  2800. (*slot)->complete = complete;
  2801. if (completep)
  2802. *completep = complete;
  2803. timevar_pop (TV_IPA_VIRTUAL_CALL);
  2804. return nodes;
  2805. }
  2806. bool
  2807. add_decl_warning (const tree &key ATTRIBUTE_UNUSED, const decl_warn_count &value,
  2808. vec<const decl_warn_count*> *vec)
  2809. {
  2810. vec->safe_push (&value);
  2811. return true;
  2812. }
  2813. /* Dump target list TARGETS into FILE. */
  2814. static void
  2815. dump_targets (FILE *f, vec <cgraph_node *> targets)
  2816. {
  2817. unsigned int i;
  2818. for (i = 0; i < targets.length (); i++)
  2819. {
  2820. char *name = NULL;
  2821. if (in_lto_p)
  2822. name = cplus_demangle_v3 (targets[i]->asm_name (), 0);
  2823. fprintf (f, " %s/%i", name ? name : targets[i]->name (), targets[i]->order);
  2824. if (in_lto_p)
  2825. free (name);
  2826. if (!targets[i]->definition)
  2827. fprintf (f, " (no definition%s)",
  2828. DECL_DECLARED_INLINE_P (targets[i]->decl)
  2829. ? " inline" : "");
  2830. }
  2831. fprintf (f, "\n");
  2832. }
  2833. /* Dump all possible targets of a polymorphic call. */
  2834. void
  2835. dump_possible_polymorphic_call_targets (FILE *f,
  2836. tree otr_type,
  2837. HOST_WIDE_INT otr_token,
  2838. const ipa_polymorphic_call_context &ctx)
  2839. {
  2840. vec <cgraph_node *> targets;
  2841. bool final;
  2842. odr_type type = get_odr_type (TYPE_MAIN_VARIANT (otr_type), false);
  2843. unsigned int len;
  2844. if (!type)
  2845. return;
  2846. targets = possible_polymorphic_call_targets (otr_type, otr_token,
  2847. ctx,
  2848. &final, NULL, false);
  2849. fprintf (f, " Targets of polymorphic call of type %i:", type->id);
  2850. print_generic_expr (f, type->type, TDF_SLIM);
  2851. fprintf (f, " token %i\n", (int)otr_token);
  2852. ctx.dump (f);
  2853. fprintf (f, " %s%s%s%s\n ",
  2854. final ? "This is a complete list." :
  2855. "This is partial list; extra targets may be defined in other units.",
  2856. ctx.maybe_in_construction ? " (base types included)" : "",
  2857. ctx.maybe_derived_type ? " (derived types included)" : "",
  2858. ctx.speculative_maybe_derived_type ? " (speculative derived types included)" : "");
  2859. len = targets.length ();
  2860. dump_targets (f, targets);
  2861. targets = possible_polymorphic_call_targets (otr_type, otr_token,
  2862. ctx,
  2863. &final, NULL, true);
  2864. if (targets.length () != len)
  2865. {
  2866. fprintf (f, " Speculative targets:");
  2867. dump_targets (f, targets);
  2868. }
  2869. gcc_assert (targets.length () <= len);
  2870. fprintf (f, "\n");
  2871. }
  2872. /* Return true if N can be possibly target of a polymorphic call of
  2873. OTR_TYPE/OTR_TOKEN. */
  2874. bool
  2875. possible_polymorphic_call_target_p (tree otr_type,
  2876. HOST_WIDE_INT otr_token,
  2877. const ipa_polymorphic_call_context &ctx,
  2878. struct cgraph_node *n)
  2879. {
  2880. vec <cgraph_node *> targets;
  2881. unsigned int i;
  2882. enum built_in_function fcode;
  2883. bool final;
  2884. if (TREE_CODE (TREE_TYPE (n->decl)) == FUNCTION_TYPE
  2885. && ((fcode = DECL_FUNCTION_CODE (n->decl))
  2886. == BUILT_IN_UNREACHABLE
  2887. || fcode == BUILT_IN_TRAP))
  2888. return true;
  2889. if (!odr_hash)
  2890. return true;
  2891. targets = possible_polymorphic_call_targets (otr_type, otr_token, ctx, &final);
  2892. for (i = 0; i < targets.length (); i++)
  2893. if (n->semantically_equivalent_p (targets[i]))
  2894. return true;
  2895. /* At a moment we allow middle end to dig out new external declarations
  2896. as a targets of polymorphic calls. */
  2897. if (!final && !n->definition)
  2898. return true;
  2899. return false;
  2900. }
  2901. /* Return true if N can be possibly target of a polymorphic call of
  2902. OBJ_TYPE_REF expression REF in STMT. */
  2903. bool
  2904. possible_polymorphic_call_target_p (tree ref,
  2905. gimple stmt,
  2906. struct cgraph_node *n)
  2907. {
  2908. ipa_polymorphic_call_context context (current_function_decl, ref, stmt);
  2909. tree call_fn = gimple_call_fn (stmt);
  2910. return possible_polymorphic_call_target_p (obj_type_ref_class (call_fn),
  2911. tree_to_uhwi
  2912. (OBJ_TYPE_REF_TOKEN (call_fn)),
  2913. context,
  2914. n);
  2915. }
  2916. /* After callgraph construction new external nodes may appear.
  2917. Add them into the graph. */
  2918. void
  2919. update_type_inheritance_graph (void)
  2920. {
  2921. struct cgraph_node *n;
  2922. if (!odr_hash)
  2923. return;
  2924. free_polymorphic_call_targets_hash ();
  2925. timevar_push (TV_IPA_INHERITANCE);
  2926. /* We reconstruct the graph starting from types of all methods seen in the
  2927. the unit. */
  2928. FOR_EACH_FUNCTION (n)
  2929. if (DECL_VIRTUAL_P (n->decl)
  2930. && !n->definition
  2931. && n->real_symbol_p ())
  2932. get_odr_type (method_class_type (TYPE_MAIN_VARIANT (TREE_TYPE (n->decl))),
  2933. true);
  2934. timevar_pop (TV_IPA_INHERITANCE);
  2935. }
  2936. /* Return true if N looks like likely target of a polymorphic call.
  2937. Rule out cxa_pure_virtual, noreturns, function declared cold and
  2938. other obvious cases. */
  2939. bool
  2940. likely_target_p (struct cgraph_node *n)
  2941. {
  2942. int flags;
  2943. /* cxa_pure_virtual and similar things are not likely. */
  2944. if (TREE_CODE (TREE_TYPE (n->decl)) != METHOD_TYPE)
  2945. return false;
  2946. flags = flags_from_decl_or_type (n->decl);
  2947. if (flags & ECF_NORETURN)
  2948. return false;
  2949. if (lookup_attribute ("cold",
  2950. DECL_ATTRIBUTES (n->decl)))
  2951. return false;
  2952. if (n->frequency < NODE_FREQUENCY_NORMAL)
  2953. return false;
  2954. /* If there are no live virtual tables referring the target,
  2955. the only way the target can be called is an instance coming from other
  2956. compilation unit; speculative devirtualization is built around an
  2957. assumption that won't happen. */
  2958. if (!referenced_from_vtable_p (n))
  2959. return false;
  2960. return true;
  2961. }
  2962. /* Compare type warning records P1 and P2 and choose one with larger count;
  2963. helper for qsort. */
  2964. int
  2965. type_warning_cmp (const void *p1, const void *p2)
  2966. {
  2967. const odr_type_warn_count *t1 = (const odr_type_warn_count *)p1;
  2968. const odr_type_warn_count *t2 = (const odr_type_warn_count *)p2;
  2969. if (t1->dyn_count < t2->dyn_count)
  2970. return 1;
  2971. if (t1->dyn_count > t2->dyn_count)
  2972. return -1;
  2973. return t2->count - t1->count;
  2974. }
  2975. /* Compare decl warning records P1 and P2 and choose one with larger count;
  2976. helper for qsort. */
  2977. int
  2978. decl_warning_cmp (const void *p1, const void *p2)
  2979. {
  2980. const decl_warn_count *t1 = *(const decl_warn_count * const *)p1;
  2981. const decl_warn_count *t2 = *(const decl_warn_count * const *)p2;
  2982. if (t1->dyn_count < t2->dyn_count)
  2983. return 1;
  2984. if (t1->dyn_count > t2->dyn_count)
  2985. return -1;
  2986. return t2->count - t1->count;
  2987. }
  2988. /* Try to speculatively devirtualize call to OTR_TYPE with OTR_TOKEN with
  2989. context CTX. */
  2990. struct cgraph_node *
  2991. try_speculative_devirtualization (tree otr_type, HOST_WIDE_INT otr_token,
  2992. ipa_polymorphic_call_context ctx)
  2993. {
  2994. vec <cgraph_node *>targets
  2995. = possible_polymorphic_call_targets
  2996. (otr_type, otr_token, ctx, NULL, NULL, true);
  2997. unsigned int i;
  2998. struct cgraph_node *likely_target = NULL;
  2999. for (i = 0; i < targets.length (); i++)
  3000. if (likely_target_p (targets[i]))
  3001. {
  3002. if (likely_target)
  3003. return NULL;
  3004. likely_target = targets[i];
  3005. }
  3006. if (!likely_target
  3007. ||!likely_target->definition
  3008. || DECL_EXTERNAL (likely_target->decl))
  3009. return NULL;
  3010. /* Don't use an implicitly-declared destructor (c++/58678). */
  3011. struct cgraph_node *non_thunk_target
  3012. = likely_target->function_symbol ();
  3013. if (DECL_ARTIFICIAL (non_thunk_target->decl))
  3014. return NULL;
  3015. if (likely_target->get_availability () <= AVAIL_INTERPOSABLE
  3016. && likely_target->can_be_discarded_p ())
  3017. return NULL;
  3018. return likely_target;
  3019. }
  3020. /* The ipa-devirt pass.
  3021. When polymorphic call has only one likely target in the unit,
  3022. turn it into a speculative call. */
  3023. static unsigned int
  3024. ipa_devirt (void)
  3025. {
  3026. struct cgraph_node *n;
  3027. hash_set<void *> bad_call_targets;
  3028. struct cgraph_edge *e;
  3029. int npolymorphic = 0, nspeculated = 0, nconverted = 0, ncold = 0;
  3030. int nmultiple = 0, noverwritable = 0, ndevirtualized = 0, nnotdefined = 0;
  3031. int nwrong = 0, nok = 0, nexternal = 0, nartificial = 0;
  3032. int ndropped = 0;
  3033. if (!odr_types_ptr)
  3034. return 0;
  3035. if (dump_file)
  3036. dump_type_inheritance_graph (dump_file);
  3037. /* We can output -Wsuggest-final-methods and -Wsuggest-final-types warnings.
  3038. This is implemented by setting up final_warning_records that are updated
  3039. by get_polymorphic_call_targets.
  3040. We need to clear cache in this case to trigger recomputation of all
  3041. entries. */
  3042. if (warn_suggest_final_methods || warn_suggest_final_types)
  3043. {
  3044. final_warning_records = new (final_warning_record);
  3045. final_warning_records->type_warnings = vNULL;
  3046. final_warning_records->type_warnings.safe_grow_cleared (odr_types.length ());
  3047. free_polymorphic_call_targets_hash ();
  3048. }
  3049. FOR_EACH_DEFINED_FUNCTION (n)
  3050. {
  3051. bool update = false;
  3052. if (!opt_for_fn (n->decl, flag_devirtualize))
  3053. continue;
  3054. if (dump_file && n->indirect_calls)
  3055. fprintf (dump_file, "\n\nProcesing function %s/%i\n",
  3056. n->name (), n->order);
  3057. for (e = n->indirect_calls; e; e = e->next_callee)
  3058. if (e->indirect_info->polymorphic)
  3059. {
  3060. struct cgraph_node *likely_target = NULL;
  3061. void *cache_token;
  3062. bool final;
  3063. if (final_warning_records)
  3064. final_warning_records->dyn_count = e->count;
  3065. vec <cgraph_node *>targets
  3066. = possible_polymorphic_call_targets
  3067. (e, &final, &cache_token, true);
  3068. unsigned int i;
  3069. /* Trigger warnings by calculating non-speculative targets. */
  3070. if (warn_suggest_final_methods || warn_suggest_final_types)
  3071. possible_polymorphic_call_targets (e);
  3072. if (dump_file)
  3073. dump_possible_polymorphic_call_targets
  3074. (dump_file, e);
  3075. npolymorphic++;
  3076. /* See if the call can be devirtualized by means of ipa-prop's
  3077. polymorphic call context propagation. If not, we can just
  3078. forget about this call being polymorphic and avoid some heavy
  3079. lifting in remove_unreachable_nodes that will otherwise try to
  3080. keep all possible targets alive until inlining and in the inliner
  3081. itself.
  3082. This may need to be revisited once we add further ways to use
  3083. the may edges, but it is a resonable thing to do right now. */
  3084. if ((e->indirect_info->param_index == -1
  3085. || (!opt_for_fn (n->decl, flag_devirtualize_speculatively)
  3086. && e->indirect_info->vptr_changed))
  3087. && !flag_ltrans_devirtualize)
  3088. {
  3089. e->indirect_info->polymorphic = false;
  3090. ndropped++;
  3091. if (dump_file)
  3092. fprintf (dump_file, "Dropping polymorphic call info;"
  3093. " it can not be used by ipa-prop\n");
  3094. }
  3095. if (!opt_for_fn (n->decl, flag_devirtualize_speculatively))
  3096. continue;
  3097. if (!e->maybe_hot_p ())
  3098. {
  3099. if (dump_file)
  3100. fprintf (dump_file, "Call is cold\n\n");
  3101. ncold++;
  3102. continue;
  3103. }
  3104. if (e->speculative)
  3105. {
  3106. if (dump_file)
  3107. fprintf (dump_file, "Call is already speculated\n\n");
  3108. nspeculated++;
  3109. /* When dumping see if we agree with speculation. */
  3110. if (!dump_file)
  3111. continue;
  3112. }
  3113. if (bad_call_targets.contains (cache_token))
  3114. {
  3115. if (dump_file)
  3116. fprintf (dump_file, "Target list is known to be useless\n\n");
  3117. nmultiple++;
  3118. continue;
  3119. }
  3120. for (i = 0; i < targets.length (); i++)
  3121. if (likely_target_p (targets[i]))
  3122. {
  3123. if (likely_target)
  3124. {
  3125. likely_target = NULL;
  3126. if (dump_file)
  3127. fprintf (dump_file, "More than one likely target\n\n");
  3128. nmultiple++;
  3129. break;
  3130. }
  3131. likely_target = targets[i];
  3132. }
  3133. if (!likely_target)
  3134. {
  3135. bad_call_targets.add (cache_token);
  3136. continue;
  3137. }
  3138. /* This is reached only when dumping; check if we agree or disagree
  3139. with the speculation. */
  3140. if (e->speculative)
  3141. {
  3142. struct cgraph_edge *e2;
  3143. struct ipa_ref *ref;
  3144. e->speculative_call_info (e2, e, ref);
  3145. if (e2->callee->ultimate_alias_target ()
  3146. == likely_target->ultimate_alias_target ())
  3147. {
  3148. fprintf (dump_file, "We agree with speculation\n\n");
  3149. nok++;
  3150. }
  3151. else
  3152. {
  3153. fprintf (dump_file, "We disagree with speculation\n\n");
  3154. nwrong++;
  3155. }
  3156. continue;
  3157. }
  3158. if (!likely_target->definition)
  3159. {
  3160. if (dump_file)
  3161. fprintf (dump_file, "Target is not a definition\n\n");
  3162. nnotdefined++;
  3163. continue;
  3164. }
  3165. /* Do not introduce new references to external symbols. While we
  3166. can handle these just well, it is common for programs to
  3167. incorrectly with headers defining methods they are linked
  3168. with. */
  3169. if (DECL_EXTERNAL (likely_target->decl))
  3170. {
  3171. if (dump_file)
  3172. fprintf (dump_file, "Target is external\n\n");
  3173. nexternal++;
  3174. continue;
  3175. }
  3176. /* Don't use an implicitly-declared destructor (c++/58678). */
  3177. struct cgraph_node *non_thunk_target
  3178. = likely_target->function_symbol ();
  3179. if (DECL_ARTIFICIAL (non_thunk_target->decl))
  3180. {
  3181. if (dump_file)
  3182. fprintf (dump_file, "Target is artificial\n\n");
  3183. nartificial++;
  3184. continue;
  3185. }
  3186. if (likely_target->get_availability () <= AVAIL_INTERPOSABLE
  3187. && likely_target->can_be_discarded_p ())
  3188. {
  3189. if (dump_file)
  3190. fprintf (dump_file, "Target is overwritable\n\n");
  3191. noverwritable++;
  3192. continue;
  3193. }
  3194. else if (dbg_cnt (devirt))
  3195. {
  3196. if (dump_enabled_p ())
  3197. {
  3198. location_t locus = gimple_location_safe (e->call_stmt);
  3199. dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
  3200. "speculatively devirtualizing call in %s/%i to %s/%i\n",
  3201. n->name (), n->order,
  3202. likely_target->name (),
  3203. likely_target->order);
  3204. }
  3205. if (!likely_target->can_be_discarded_p ())
  3206. {
  3207. cgraph_node *alias;
  3208. alias = dyn_cast<cgraph_node *> (likely_target->noninterposable_alias ());
  3209. if (alias)
  3210. likely_target = alias;
  3211. }
  3212. nconverted++;
  3213. update = true;
  3214. e->make_speculative
  3215. (likely_target, e->count * 8 / 10, e->frequency * 8 / 10);
  3216. }
  3217. }
  3218. if (update)
  3219. inline_update_overall_summary (n);
  3220. }
  3221. if (warn_suggest_final_methods || warn_suggest_final_types)
  3222. {
  3223. if (warn_suggest_final_types)
  3224. {
  3225. final_warning_records->type_warnings.qsort (type_warning_cmp);
  3226. for (unsigned int i = 0;
  3227. i < final_warning_records->type_warnings.length (); i++)
  3228. if (final_warning_records->type_warnings[i].count)
  3229. {
  3230. tree type = final_warning_records->type_warnings[i].type;
  3231. int count = final_warning_records->type_warnings[i].count;
  3232. long long dyn_count
  3233. = final_warning_records->type_warnings[i].dyn_count;
  3234. if (!dyn_count)
  3235. warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
  3236. OPT_Wsuggest_final_types, count,
  3237. "Declaring type %qD final "
  3238. "would enable devirtualization of %i call",
  3239. "Declaring type %qD final "
  3240. "would enable devirtualization of %i calls",
  3241. type,
  3242. count);
  3243. else
  3244. warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
  3245. OPT_Wsuggest_final_types, count,
  3246. "Declaring type %qD final "
  3247. "would enable devirtualization of %i call "
  3248. "executed %lli times",
  3249. "Declaring type %qD final "
  3250. "would enable devirtualization of %i calls "
  3251. "executed %lli times",
  3252. type,
  3253. count,
  3254. dyn_count);
  3255. }
  3256. }
  3257. if (warn_suggest_final_methods)
  3258. {
  3259. vec<const decl_warn_count*> decl_warnings_vec = vNULL;
  3260. final_warning_records->decl_warnings.traverse
  3261. <vec<const decl_warn_count *> *, add_decl_warning> (&decl_warnings_vec);
  3262. decl_warnings_vec.qsort (decl_warning_cmp);
  3263. for (unsigned int i = 0; i < decl_warnings_vec.length (); i++)
  3264. {
  3265. tree decl = decl_warnings_vec[i]->decl;
  3266. int count = decl_warnings_vec[i]->count;
  3267. long long dyn_count = decl_warnings_vec[i]->dyn_count;
  3268. if (!dyn_count)
  3269. if (DECL_CXX_DESTRUCTOR_P (decl))
  3270. warning_n (DECL_SOURCE_LOCATION (decl),
  3271. OPT_Wsuggest_final_methods, count,
  3272. "Declaring virtual destructor of %qD final "
  3273. "would enable devirtualization of %i call",
  3274. "Declaring virtual destructor of %qD final "
  3275. "would enable devirtualization of %i calls",
  3276. DECL_CONTEXT (decl), count);
  3277. else
  3278. warning_n (DECL_SOURCE_LOCATION (decl),
  3279. OPT_Wsuggest_final_methods, count,
  3280. "Declaring method %qD final "
  3281. "would enable devirtualization of %i call",
  3282. "Declaring method %qD final "
  3283. "would enable devirtualization of %i calls",
  3284. decl, count);
  3285. else if (DECL_CXX_DESTRUCTOR_P (decl))
  3286. warning_n (DECL_SOURCE_LOCATION (decl),
  3287. OPT_Wsuggest_final_methods, count,
  3288. "Declaring virtual destructor of %qD final "
  3289. "would enable devirtualization of %i call "
  3290. "executed %lli times",
  3291. "Declaring virtual destructor of %qD final "
  3292. "would enable devirtualization of %i calls "
  3293. "executed %lli times",
  3294. DECL_CONTEXT (decl), count, dyn_count);
  3295. else
  3296. warning_n (DECL_SOURCE_LOCATION (decl),
  3297. OPT_Wsuggest_final_methods, count,
  3298. "Declaring method %qD final "
  3299. "would enable devirtualization of %i call "
  3300. "executed %lli times",
  3301. "Declaring method %qD final "
  3302. "would enable devirtualization of %i calls "
  3303. "executed %lli times",
  3304. decl, count, dyn_count);
  3305. }
  3306. }
  3307. delete (final_warning_records);
  3308. final_warning_records = 0;
  3309. }
  3310. if (dump_file)
  3311. fprintf (dump_file,
  3312. "%i polymorphic calls, %i devirtualized,"
  3313. " %i speculatively devirtualized, %i cold\n"
  3314. "%i have multiple targets, %i overwritable,"
  3315. " %i already speculated (%i agree, %i disagree),"
  3316. " %i external, %i not defined, %i artificial, %i infos dropped\n",
  3317. npolymorphic, ndevirtualized, nconverted, ncold,
  3318. nmultiple, noverwritable, nspeculated, nok, nwrong,
  3319. nexternal, nnotdefined, nartificial, ndropped);
  3320. return ndevirtualized || ndropped ? TODO_remove_functions : 0;
  3321. }
  3322. namespace {
  3323. const pass_data pass_data_ipa_devirt =
  3324. {
  3325. IPA_PASS, /* type */
  3326. "devirt", /* name */
  3327. OPTGROUP_NONE, /* optinfo_flags */
  3328. TV_IPA_DEVIRT, /* tv_id */
  3329. 0, /* properties_required */
  3330. 0, /* properties_provided */
  3331. 0, /* properties_destroyed */
  3332. 0, /* todo_flags_start */
  3333. ( TODO_dump_symtab ), /* todo_flags_finish */
  3334. };
  3335. class pass_ipa_devirt : public ipa_opt_pass_d
  3336. {
  3337. public:
  3338. pass_ipa_devirt (gcc::context *ctxt)
  3339. : ipa_opt_pass_d (pass_data_ipa_devirt, ctxt,
  3340. NULL, /* generate_summary */
  3341. NULL, /* write_summary */
  3342. NULL, /* read_summary */
  3343. NULL, /* write_optimization_summary */
  3344. NULL, /* read_optimization_summary */
  3345. NULL, /* stmt_fixup */
  3346. 0, /* function_transform_todo_flags_start */
  3347. NULL, /* function_transform */
  3348. NULL) /* variable_transform */
  3349. {}
  3350. /* opt_pass methods: */
  3351. virtual bool gate (function *)
  3352. {
  3353. /* In LTO, always run the IPA passes and decide on function basis if the
  3354. pass is enabled. */
  3355. if (in_lto_p)
  3356. return true;
  3357. return (flag_devirtualize
  3358. && (flag_devirtualize_speculatively
  3359. || (warn_suggest_final_methods
  3360. || warn_suggest_final_types))
  3361. && optimize);
  3362. }
  3363. virtual unsigned int execute (function *) { return ipa_devirt (); }
  3364. }; // class pass_ipa_devirt
  3365. } // anon namespace
  3366. ipa_opt_pass_d *
  3367. make_pass_ipa_devirt (gcc::context *ctxt)
  3368. {
  3369. return new pass_ipa_devirt (ctxt);
  3370. }
  3371. #include "gt-ipa-devirt.h"