ipa-polymorphic-call.c 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514
  1. /* Analysis of polymorphic call context.
  2. Copyright (C) 2013-2015 Free Software Foundation, Inc.
  3. Contributed by Jan Hubicka
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "config.h"
  17. #include "system.h"
  18. #include "coretypes.h"
  19. #include "tm.h"
  20. #include "hash-set.h"
  21. #include "machmode.h"
  22. #include "vec.h"
  23. #include "double-int.h"
  24. #include "input.h"
  25. #include "alias.h"
  26. #include "symtab.h"
  27. #include "wide-int.h"
  28. #include "inchash.h"
  29. #include "tree.h"
  30. #include "fold-const.h"
  31. #include "print-tree.h"
  32. #include "calls.h"
  33. #include "hashtab.h"
  34. #include "hard-reg-set.h"
  35. #include "function.h"
  36. #include "rtl.h"
  37. #include "flags.h"
  38. #include "statistics.h"
  39. #include "real.h"
  40. #include "fixed-value.h"
  41. #include "insn-config.h"
  42. #include "expmed.h"
  43. #include "dojump.h"
  44. #include "explow.h"
  45. #include "emit-rtl.h"
  46. #include "varasm.h"
  47. #include "stmt.h"
  48. #include "expr.h"
  49. #include "tree-pass.h"
  50. #include "target.h"
  51. #include "tree-pretty-print.h"
  52. #include "predict.h"
  53. #include "basic-block.h"
  54. #include "hash-map.h"
  55. #include "is-a.h"
  56. #include "plugin-api.h"
  57. #include "ipa-ref.h"
  58. #include "cgraph.h"
  59. #include "ipa-utils.h"
  60. #include "tree-ssa-alias.h"
  61. #include "internal-fn.h"
  62. #include "gimple-fold.h"
  63. #include "gimple-expr.h"
  64. #include "gimple.h"
  65. #include "alloc-pool.h"
  66. #include "symbol-summary.h"
  67. #include "ipa-prop.h"
  68. #include "ipa-inline.h"
  69. #include "diagnostic.h"
  70. #include "tree-dfa.h"
  71. #include "demangle.h"
  72. #include "dbgcnt.h"
  73. #include "gimple-pretty-print.h"
  74. #include "stor-layout.h"
  75. #include "intl.h"
  76. #include "data-streamer.h"
  77. #include "lto-streamer.h"
  78. #include "streamer-hooks.h"
  79. #include "tree-ssa-operands.h"
  80. #include "tree-into-ssa.h"
  81. /* Return true when TYPE contains an polymorphic type and thus is interesting
  82. for devirtualization machinery. */
  83. static bool contains_type_p (tree, HOST_WIDE_INT, tree,
  84. bool consider_placement_new = true,
  85. bool consider_bases = true);
  86. bool
  87. contains_polymorphic_type_p (const_tree type)
  88. {
  89. type = TYPE_MAIN_VARIANT (type);
  90. if (RECORD_OR_UNION_TYPE_P (type))
  91. {
  92. if (TYPE_BINFO (type)
  93. && polymorphic_type_binfo_p (TYPE_BINFO (type)))
  94. return true;
  95. for (tree fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
  96. if (TREE_CODE (fld) == FIELD_DECL
  97. && !DECL_ARTIFICIAL (fld)
  98. && contains_polymorphic_type_p (TREE_TYPE (fld)))
  99. return true;
  100. return false;
  101. }
  102. if (TREE_CODE (type) == ARRAY_TYPE)
  103. return contains_polymorphic_type_p (TREE_TYPE (type));
  104. return false;
  105. }
  106. /* Return true if it seems valid to use placement new to build EXPECTED_TYPE
  107. at possition CUR_OFFSET within TYPE.
  108. POD can be changed to an instance of a polymorphic type by
  109. placement new. Here we play safe and assume that any
  110. non-polymorphic type is POD. */
  111. bool
  112. possible_placement_new (tree type, tree expected_type,
  113. HOST_WIDE_INT cur_offset)
  114. {
  115. return ((TREE_CODE (type) != RECORD_TYPE
  116. || !TYPE_BINFO (type)
  117. || cur_offset >= POINTER_SIZE
  118. || !polymorphic_type_binfo_p (TYPE_BINFO (type)))
  119. && (!TYPE_SIZE (type)
  120. || !tree_fits_shwi_p (TYPE_SIZE (type))
  121. || (cur_offset
  122. + (expected_type ? tree_to_uhwi (TYPE_SIZE (expected_type))
  123. : POINTER_SIZE)
  124. <= tree_to_uhwi (TYPE_SIZE (type)))));
  125. }
  126. /* THIS->OUTER_TYPE is a type of memory object where object of OTR_TYPE
  127. is contained at THIS->OFFSET. Walk the memory representation of
  128. THIS->OUTER_TYPE and find the outermost class type that match
  129. OTR_TYPE or contain OTR_TYPE as a base. Update THIS
  130. to represent it.
  131. If OTR_TYPE is NULL, just find outermost polymorphic type with
  132. virtual table present at possition OFFSET.
  133. For example when THIS represents type
  134. class A
  135. {
  136. int a;
  137. class B b;
  138. }
  139. and we look for type at offset sizeof(int), we end up with B and offset 0.
  140. If the same is produced by multiple inheritance, we end up with A and offset
  141. sizeof(int).
  142. If we can not find corresponding class, give up by setting
  143. THIS->OUTER_TYPE to OTR_TYPE and THIS->OFFSET to NULL.
  144. Return true when lookup was sucesful.
  145. When CONSIDER_PLACEMENT_NEW is false, reject contexts that may be made
  146. valid only via alocation of new polymorphic type inside by means
  147. of placement new.
  148. When CONSIDER_BASES is false, only look for actual fields, not base types
  149. of TYPE. */
  150. bool
  151. ipa_polymorphic_call_context::restrict_to_inner_class (tree otr_type,
  152. bool consider_placement_new,
  153. bool consider_bases)
  154. {
  155. tree type = outer_type;
  156. HOST_WIDE_INT cur_offset = offset;
  157. bool speculative = false;
  158. bool size_unknown = false;
  159. unsigned HOST_WIDE_INT otr_type_size = POINTER_SIZE;
  160. /* Update OUTER_TYPE to match EXPECTED_TYPE if it is not set. */
  161. if (!outer_type)
  162. {
  163. clear_outer_type (otr_type);
  164. type = otr_type;
  165. cur_offset = 0;
  166. }
  167. /* See if OFFSET points inside OUTER_TYPE. If it does not, we know
  168. that the context is either invalid, or the instance type must be
  169. derived from OUTER_TYPE.
  170. Because the instance type may contain field whose type is of OUTER_TYPE,
  171. we can not derive any effective information about it.
  172. TODO: In the case we know all derrived types, we can definitely do better
  173. here. */
  174. else if (TYPE_SIZE (outer_type)
  175. && tree_fits_shwi_p (TYPE_SIZE (outer_type))
  176. && tree_to_shwi (TYPE_SIZE (outer_type)) >= 0
  177. && tree_to_shwi (TYPE_SIZE (outer_type)) <= offset)
  178. {
  179. clear_outer_type (otr_type);
  180. type = otr_type;
  181. cur_offset = 0;
  182. /* If derived type is not allowed, we know that the context is invalid.
  183. For dynamic types, we really do not have information about
  184. size of the memory location. It is possible that completely
  185. different type is stored after outer_type. */
  186. if (!maybe_derived_type && !dynamic)
  187. {
  188. clear_speculation ();
  189. invalid = true;
  190. return false;
  191. }
  192. }
  193. if (otr_type && TYPE_SIZE (otr_type)
  194. && tree_fits_shwi_p (TYPE_SIZE (otr_type)))
  195. otr_type_size = tree_to_uhwi (TYPE_SIZE (otr_type));
  196. if (!type || offset < 0)
  197. goto no_useful_type_info;
  198. /* Find the sub-object the constant actually refers to and mark whether it is
  199. an artificial one (as opposed to a user-defined one).
  200. This loop is performed twice; first time for outer_type and second time
  201. for speculative_outer_type. The second run has SPECULATIVE set. */
  202. while (true)
  203. {
  204. unsigned HOST_WIDE_INT pos, size;
  205. tree fld;
  206. /* If we do not know size of TYPE, we need to be more conservative
  207. about accepting cases where we can not find EXPECTED_TYPE.
  208. Generally the types that do matter here are of constant size.
  209. Size_unknown case should be very rare. */
  210. if (TYPE_SIZE (type)
  211. && tree_fits_shwi_p (TYPE_SIZE (type))
  212. && tree_to_shwi (TYPE_SIZE (type)) >= 0)
  213. size_unknown = false;
  214. else
  215. size_unknown = true;
  216. /* On a match, just return what we found. */
  217. if ((otr_type
  218. && types_odr_comparable (type, otr_type)
  219. && types_same_for_odr (type, otr_type))
  220. || (!otr_type
  221. && TREE_CODE (type) == RECORD_TYPE
  222. && TYPE_BINFO (type)
  223. && polymorphic_type_binfo_p (TYPE_BINFO (type))))
  224. {
  225. if (speculative)
  226. {
  227. /* If we did not match the offset, just give up on speculation. */
  228. if (cur_offset != 0
  229. /* Also check if speculation did not end up being same as
  230. non-speculation. */
  231. || (types_must_be_same_for_odr (speculative_outer_type,
  232. outer_type)
  233. && (maybe_derived_type
  234. == speculative_maybe_derived_type)))
  235. clear_speculation ();
  236. return true;
  237. }
  238. else
  239. {
  240. /* If type is known to be final, do not worry about derived
  241. types. Testing it here may help us to avoid speculation. */
  242. if (otr_type && TREE_CODE (outer_type) == RECORD_TYPE
  243. && (!in_lto_p || odr_type_p (outer_type))
  244. && type_known_to_have_no_deriavations_p (outer_type))
  245. maybe_derived_type = false;
  246. /* Type can not contain itself on an non-zero offset. In that case
  247. just give up. Still accept the case where size is now known.
  248. Either the second copy may appear past the end of type or within
  249. the non-POD buffer located inside the variably sized type
  250. itself. */
  251. if (cur_offset != 0)
  252. goto no_useful_type_info;
  253. /* If we determined type precisely or we have no clue on
  254. speuclation, we are done. */
  255. if (!maybe_derived_type || !speculative_outer_type
  256. || !speculation_consistent_p (speculative_outer_type,
  257. speculative_offset,
  258. speculative_maybe_derived_type,
  259. otr_type))
  260. {
  261. clear_speculation ();
  262. return true;
  263. }
  264. /* Otherwise look into speculation now. */
  265. else
  266. {
  267. speculative = true;
  268. type = speculative_outer_type;
  269. cur_offset = speculative_offset;
  270. continue;
  271. }
  272. }
  273. }
  274. /* Walk fields and find corresponding on at OFFSET. */
  275. if (TREE_CODE (type) == RECORD_TYPE)
  276. {
  277. for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
  278. {
  279. if (TREE_CODE (fld) != FIELD_DECL)
  280. continue;
  281. pos = int_bit_position (fld);
  282. if (pos > (unsigned HOST_WIDE_INT)cur_offset)
  283. continue;
  284. /* Do not consider vptr itself. Not even for placement new. */
  285. if (!pos && DECL_ARTIFICIAL (fld)
  286. && POINTER_TYPE_P (TREE_TYPE (fld))
  287. && TYPE_BINFO (type)
  288. && polymorphic_type_binfo_p (TYPE_BINFO (type)))
  289. continue;
  290. if (!DECL_SIZE (fld) || !tree_fits_uhwi_p (DECL_SIZE (fld)))
  291. goto no_useful_type_info;
  292. size = tree_to_uhwi (DECL_SIZE (fld));
  293. /* We can always skip types smaller than pointer size:
  294. those can not contain a virtual table pointer.
  295. Disqualifying fields that are too small to fit OTR_TYPE
  296. saves work needed to walk them for no benefit.
  297. Because of the way the bases are packed into a class, the
  298. field's size may be smaller than type size, so it needs
  299. to be done with a care. */
  300. if (pos <= (unsigned HOST_WIDE_INT)cur_offset
  301. && (pos + size) >= (unsigned HOST_WIDE_INT)cur_offset
  302. + POINTER_SIZE
  303. && (!otr_type
  304. || !TYPE_SIZE (TREE_TYPE (fld))
  305. || !tree_fits_shwi_p (TYPE_SIZE (TREE_TYPE (fld)))
  306. || (pos + tree_to_uhwi (TYPE_SIZE (TREE_TYPE (fld))))
  307. >= cur_offset + otr_type_size))
  308. break;
  309. }
  310. if (!fld)
  311. goto no_useful_type_info;
  312. type = TYPE_MAIN_VARIANT (TREE_TYPE (fld));
  313. cur_offset -= pos;
  314. /* DECL_ARTIFICIAL represents a basetype. */
  315. if (!DECL_ARTIFICIAL (fld))
  316. {
  317. if (!speculative)
  318. {
  319. outer_type = type;
  320. offset = cur_offset;
  321. /* As soon as we se an field containing the type,
  322. we know we are not looking for derivations. */
  323. maybe_derived_type = false;
  324. }
  325. else
  326. {
  327. speculative_outer_type = type;
  328. speculative_offset = cur_offset;
  329. speculative_maybe_derived_type = false;
  330. }
  331. }
  332. else if (!consider_bases)
  333. goto no_useful_type_info;
  334. }
  335. else if (TREE_CODE (type) == ARRAY_TYPE)
  336. {
  337. tree subtype = TYPE_MAIN_VARIANT (TREE_TYPE (type));
  338. /* Give up if we don't know array field size.
  339. Also give up on non-polymorphic types as they are used
  340. as buffers for placement new. */
  341. if (!TYPE_SIZE (subtype)
  342. || !tree_fits_shwi_p (TYPE_SIZE (subtype))
  343. || tree_to_shwi (TYPE_SIZE (subtype)) <= 0
  344. || !contains_polymorphic_type_p (subtype))
  345. goto no_useful_type_info;
  346. HOST_WIDE_INT new_offset = cur_offset % tree_to_shwi (TYPE_SIZE (subtype));
  347. /* We may see buffer for placement new. In this case the expected type
  348. can be bigger than the subtype. */
  349. if (TYPE_SIZE (subtype)
  350. && (cur_offset + otr_type_size
  351. > tree_to_uhwi (TYPE_SIZE (subtype))))
  352. goto no_useful_type_info;
  353. cur_offset = new_offset;
  354. type = subtype;
  355. if (!speculative)
  356. {
  357. outer_type = type;
  358. offset = cur_offset;
  359. maybe_derived_type = false;
  360. }
  361. else
  362. {
  363. speculative_outer_type = type;
  364. speculative_offset = cur_offset;
  365. speculative_maybe_derived_type = false;
  366. }
  367. }
  368. /* Give up on anything else. */
  369. else
  370. {
  371. no_useful_type_info:
  372. if (maybe_derived_type && !speculative
  373. && TREE_CODE (outer_type) == RECORD_TYPE
  374. && TREE_CODE (otr_type) == RECORD_TYPE
  375. && TYPE_BINFO (otr_type)
  376. && !offset
  377. && get_binfo_at_offset (TYPE_BINFO (otr_type), 0, outer_type))
  378. {
  379. clear_outer_type (otr_type);
  380. if (!speculative_outer_type
  381. || !speculation_consistent_p (speculative_outer_type,
  382. speculative_offset,
  383. speculative_maybe_derived_type,
  384. otr_type))
  385. clear_speculation ();
  386. if (speculative_outer_type)
  387. {
  388. speculative = true;
  389. type = speculative_outer_type;
  390. cur_offset = speculative_offset;
  391. }
  392. else
  393. return true;
  394. }
  395. /* We found no way to embedd EXPECTED_TYPE in TYPE.
  396. We still permit two special cases - placement new and
  397. the case of variadic types containing themselves. */
  398. if (!speculative
  399. && consider_placement_new
  400. && (size_unknown || !type || maybe_derived_type
  401. || possible_placement_new (type, otr_type, cur_offset)))
  402. {
  403. /* In these weird cases we want to accept the context.
  404. In non-speculative run we have no useful outer_type info
  405. (TODO: we may eventually want to record upper bound on the
  406. type size that can be used to prune the walk),
  407. but we still want to consider speculation that may
  408. give useful info. */
  409. if (!speculative)
  410. {
  411. clear_outer_type (otr_type);
  412. if (!speculative_outer_type
  413. || !speculation_consistent_p (speculative_outer_type,
  414. speculative_offset,
  415. speculative_maybe_derived_type,
  416. otr_type))
  417. clear_speculation ();
  418. if (speculative_outer_type)
  419. {
  420. speculative = true;
  421. type = speculative_outer_type;
  422. cur_offset = speculative_offset;
  423. }
  424. else
  425. return true;
  426. }
  427. else
  428. clear_speculation ();
  429. return true;
  430. }
  431. else
  432. {
  433. clear_speculation ();
  434. if (speculative)
  435. return true;
  436. clear_outer_type (otr_type);
  437. invalid = true;
  438. return false;
  439. }
  440. }
  441. }
  442. }
  443. /* Return true if OUTER_TYPE contains OTR_TYPE at OFFSET.
  444. CONSIDER_PLACEMENT_NEW makes function to accept cases where OTR_TYPE can
  445. be built within OUTER_TYPE by means of placement new. CONSIDER_BASES makes
  446. function to accept cases where OTR_TYPE appears as base of OUTER_TYPE or as
  447. base of one of fields of OUTER_TYPE. */
  448. static bool
  449. contains_type_p (tree outer_type, HOST_WIDE_INT offset,
  450. tree otr_type,
  451. bool consider_placement_new,
  452. bool consider_bases)
  453. {
  454. ipa_polymorphic_call_context context;
  455. /* Check that type is within range. */
  456. if (offset < 0)
  457. return false;
  458. if (TYPE_SIZE (outer_type) && TYPE_SIZE (otr_type)
  459. && TREE_CODE (outer_type) == INTEGER_CST
  460. && TREE_CODE (otr_type) == INTEGER_CST
  461. && wi::ltu_p (wi::to_offset (outer_type), (wi::to_offset (otr_type) + offset)))
  462. return false;
  463. context.offset = offset;
  464. context.outer_type = TYPE_MAIN_VARIANT (outer_type);
  465. context.maybe_derived_type = false;
  466. return context.restrict_to_inner_class (otr_type, consider_placement_new, consider_bases);
  467. }
  468. /* Return a FUNCTION_DECL if BLOCK represents a constructor or destructor.
  469. If CHECK_CLONES is true, also check for clones of ctor/dtors. */
  470. tree
  471. inlined_polymorphic_ctor_dtor_block_p (tree block, bool check_clones)
  472. {
  473. tree fn = BLOCK_ABSTRACT_ORIGIN (block);
  474. if (fn == NULL || TREE_CODE (fn) != FUNCTION_DECL)
  475. return NULL_TREE;
  476. if (TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
  477. || (!DECL_CXX_CONSTRUCTOR_P (fn) && !DECL_CXX_DESTRUCTOR_P (fn)))
  478. {
  479. if (!check_clones)
  480. return NULL_TREE;
  481. /* Watch for clones where we constant propagated the first
  482. argument (pointer to the instance). */
  483. fn = DECL_ABSTRACT_ORIGIN (fn);
  484. if (!fn
  485. || TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
  486. || (!DECL_CXX_CONSTRUCTOR_P (fn) && !DECL_CXX_DESTRUCTOR_P (fn)))
  487. return NULL_TREE;
  488. }
  489. if (flags_from_decl_or_type (fn) & (ECF_PURE | ECF_CONST))
  490. return NULL_TREE;
  491. return fn;
  492. }
  493. /* We know that the instance is stored in variable or parameter
  494. (not dynamically allocated) and we want to disprove the fact
  495. that it may be in construction at invocation of CALL.
  496. BASE represents memory location where instance is stored.
  497. If BASE is NULL, it is assumed to be global memory.
  498. OUTER_TYPE is known type of the instance or NULL if not
  499. known.
  500. For the variable to be in construction we actually need to
  501. be in constructor of corresponding global variable or
  502. the inline stack of CALL must contain the constructor.
  503. Check this condition. This check works safely only before
  504. IPA passes, because inline stacks may become out of date
  505. later. */
  506. bool
  507. decl_maybe_in_construction_p (tree base, tree outer_type,
  508. gimple call, tree function)
  509. {
  510. if (outer_type)
  511. outer_type = TYPE_MAIN_VARIANT (outer_type);
  512. gcc_assert (!base || DECL_P (base));
  513. /* After inlining the code unification optimizations may invalidate
  514. inline stacks. Also we need to give up on global variables after
  515. IPA, because addresses of these may have been propagated to their
  516. constructors. */
  517. if (DECL_STRUCT_FUNCTION (function)->after_inlining)
  518. return true;
  519. /* Pure functions can not do any changes on the dynamic type;
  520. that require writting to memory. */
  521. if ((!base || !auto_var_in_fn_p (base, function))
  522. && flags_from_decl_or_type (function) & (ECF_PURE | ECF_CONST))
  523. return false;
  524. bool check_clones = !base || is_global_var (base);
  525. for (tree block = gimple_block (call); block && TREE_CODE (block) == BLOCK;
  526. block = BLOCK_SUPERCONTEXT (block))
  527. if (tree fn = inlined_polymorphic_ctor_dtor_block_p (block, check_clones))
  528. {
  529. tree type = TYPE_MAIN_VARIANT (method_class_type (TREE_TYPE (fn)));
  530. if (!outer_type || !types_odr_comparable (type, outer_type))
  531. {
  532. if (TREE_CODE (type) == RECORD_TYPE
  533. && TYPE_BINFO (type)
  534. && polymorphic_type_binfo_p (TYPE_BINFO (type)))
  535. return true;
  536. }
  537. else if (types_same_for_odr (type, outer_type))
  538. return true;
  539. }
  540. if (!base || (TREE_CODE (base) == VAR_DECL && is_global_var (base)))
  541. {
  542. if (TREE_CODE (TREE_TYPE (function)) != METHOD_TYPE
  543. || (!DECL_CXX_CONSTRUCTOR_P (function)
  544. && !DECL_CXX_DESTRUCTOR_P (function)))
  545. {
  546. if (!DECL_ABSTRACT_ORIGIN (function))
  547. return false;
  548. /* Watch for clones where we constant propagated the first
  549. argument (pointer to the instance). */
  550. function = DECL_ABSTRACT_ORIGIN (function);
  551. if (!function
  552. || TREE_CODE (TREE_TYPE (function)) != METHOD_TYPE
  553. || (!DECL_CXX_CONSTRUCTOR_P (function)
  554. && !DECL_CXX_DESTRUCTOR_P (function)))
  555. return false;
  556. }
  557. tree type = TYPE_MAIN_VARIANT (method_class_type (TREE_TYPE (function)));
  558. if (!outer_type || !types_odr_comparable (type, outer_type))
  559. {
  560. if (TREE_CODE (type) == RECORD_TYPE
  561. && TYPE_BINFO (type)
  562. && polymorphic_type_binfo_p (TYPE_BINFO (type)))
  563. return true;
  564. }
  565. else if (types_same_for_odr (type, outer_type))
  566. return true;
  567. }
  568. return false;
  569. }
  570. /* Dump human readable context to F. If NEWLINE is true, it will be terminated
  571. by a newline. */
  572. void
  573. ipa_polymorphic_call_context::dump (FILE *f, bool newline) const
  574. {
  575. fprintf (f, " ");
  576. if (invalid)
  577. fprintf (f, "Call is known to be undefined");
  578. else
  579. {
  580. if (useless_p ())
  581. fprintf (f, "nothing known");
  582. if (outer_type || offset)
  583. {
  584. fprintf (f, "Outer type%s:", dynamic ? " (dynamic)":"");
  585. print_generic_expr (f, outer_type, TDF_SLIM);
  586. if (maybe_derived_type)
  587. fprintf (f, " (or a derived type)");
  588. if (maybe_in_construction)
  589. fprintf (f, " (maybe in construction)");
  590. fprintf (f, " offset "HOST_WIDE_INT_PRINT_DEC,
  591. offset);
  592. }
  593. if (speculative_outer_type)
  594. {
  595. if (outer_type || offset)
  596. fprintf (f, " ");
  597. fprintf (f, "Speculative outer type:");
  598. print_generic_expr (f, speculative_outer_type, TDF_SLIM);
  599. if (speculative_maybe_derived_type)
  600. fprintf (f, " (or a derived type)");
  601. fprintf (f, " at offset "HOST_WIDE_INT_PRINT_DEC,
  602. speculative_offset);
  603. }
  604. }
  605. if (newline)
  606. fprintf(f, "\n");
  607. }
  608. /* Print context to stderr. */
  609. void
  610. ipa_polymorphic_call_context::debug () const
  611. {
  612. dump (stderr);
  613. }
  614. /* Stream out the context to OB. */
  615. void
  616. ipa_polymorphic_call_context::stream_out (struct output_block *ob) const
  617. {
  618. struct bitpack_d bp = bitpack_create (ob->main_stream);
  619. bp_pack_value (&bp, invalid, 1);
  620. bp_pack_value (&bp, maybe_in_construction, 1);
  621. bp_pack_value (&bp, maybe_derived_type, 1);
  622. bp_pack_value (&bp, speculative_maybe_derived_type, 1);
  623. bp_pack_value (&bp, dynamic, 1);
  624. bp_pack_value (&bp, outer_type != NULL, 1);
  625. bp_pack_value (&bp, offset != 0, 1);
  626. bp_pack_value (&bp, speculative_outer_type != NULL, 1);
  627. streamer_write_bitpack (&bp);
  628. if (outer_type != NULL)
  629. stream_write_tree (ob, outer_type, true);
  630. if (offset)
  631. streamer_write_hwi (ob, offset);
  632. if (speculative_outer_type != NULL)
  633. {
  634. stream_write_tree (ob, speculative_outer_type, true);
  635. streamer_write_hwi (ob, speculative_offset);
  636. }
  637. else
  638. gcc_assert (!speculative_offset);
  639. }
  640. /* Stream in the context from IB and DATA_IN. */
  641. void
  642. ipa_polymorphic_call_context::stream_in (struct lto_input_block *ib,
  643. struct data_in *data_in)
  644. {
  645. struct bitpack_d bp = streamer_read_bitpack (ib);
  646. invalid = bp_unpack_value (&bp, 1);
  647. maybe_in_construction = bp_unpack_value (&bp, 1);
  648. maybe_derived_type = bp_unpack_value (&bp, 1);
  649. speculative_maybe_derived_type = bp_unpack_value (&bp, 1);
  650. dynamic = bp_unpack_value (&bp, 1);
  651. bool outer_type_p = bp_unpack_value (&bp, 1);
  652. bool offset_p = bp_unpack_value (&bp, 1);
  653. bool speculative_outer_type_p = bp_unpack_value (&bp, 1);
  654. if (outer_type_p)
  655. outer_type = stream_read_tree (ib, data_in);
  656. else
  657. outer_type = NULL;
  658. if (offset_p)
  659. offset = (HOST_WIDE_INT) streamer_read_hwi (ib);
  660. else
  661. offset = 0;
  662. if (speculative_outer_type_p)
  663. {
  664. speculative_outer_type = stream_read_tree (ib, data_in);
  665. speculative_offset = (HOST_WIDE_INT) streamer_read_hwi (ib);
  666. }
  667. else
  668. {
  669. speculative_outer_type = NULL;
  670. speculative_offset = 0;
  671. }
  672. }
  673. /* Proudce polymorphic call context for call method of instance
  674. that is located within BASE (that is assumed to be a decl) at offset OFF. */
  675. void
  676. ipa_polymorphic_call_context::set_by_decl (tree base, HOST_WIDE_INT off)
  677. {
  678. gcc_assert (DECL_P (base));
  679. clear_speculation ();
  680. if (!contains_polymorphic_type_p (TREE_TYPE (base)))
  681. {
  682. clear_outer_type ();
  683. offset = off;
  684. return;
  685. }
  686. outer_type = TYPE_MAIN_VARIANT (TREE_TYPE (base));
  687. offset = off;
  688. /* Make very conservative assumption that all objects
  689. may be in construction.
  690. It is up to caller to revisit this via
  691. get_dynamic_type or decl_maybe_in_construction_p. */
  692. maybe_in_construction = true;
  693. maybe_derived_type = false;
  694. dynamic = false;
  695. }
  696. /* CST is an invariant (address of decl), try to get meaningful
  697. polymorphic call context for polymorphic call of method
  698. if instance of OTR_TYPE that is located at offset OFF of this invariant.
  699. Return FALSE if nothing meaningful can be found. */
  700. bool
  701. ipa_polymorphic_call_context::set_by_invariant (tree cst,
  702. tree otr_type,
  703. HOST_WIDE_INT off)
  704. {
  705. HOST_WIDE_INT offset2, size, max_size;
  706. tree base;
  707. invalid = false;
  708. off = 0;
  709. clear_outer_type (otr_type);
  710. if (TREE_CODE (cst) != ADDR_EXPR)
  711. return false;
  712. cst = TREE_OPERAND (cst, 0);
  713. base = get_ref_base_and_extent (cst, &offset2, &size, &max_size);
  714. if (!DECL_P (base) || max_size == -1 || max_size != size)
  715. return false;
  716. /* Only type inconsistent programs can have otr_type that is
  717. not part of outer type. */
  718. if (otr_type && !contains_type_p (TREE_TYPE (base), off, otr_type))
  719. return false;
  720. set_by_decl (base, off);
  721. return true;
  722. }
  723. /* See if OP is SSA name initialized as a copy or by single assignment.
  724. If so, walk the SSA graph up. Because simple PHI conditional is considered
  725. copy, GLOBAL_VISITED may be used to avoid infinite loop walking the SSA
  726. graph. */
  727. static tree
  728. walk_ssa_copies (tree op, hash_set<tree> **global_visited = NULL)
  729. {
  730. hash_set <tree> *visited = NULL;
  731. STRIP_NOPS (op);
  732. while (TREE_CODE (op) == SSA_NAME
  733. && !SSA_NAME_IS_DEFAULT_DEF (op)
  734. /* We might be called via fold_stmt during cfgcleanup where
  735. SSA form need not be up-to-date. */
  736. && !name_registered_for_update_p (op)
  737. && (gimple_assign_single_p (SSA_NAME_DEF_STMT (op))
  738. || gimple_code (SSA_NAME_DEF_STMT (op)) == GIMPLE_PHI))
  739. {
  740. if (global_visited)
  741. {
  742. if (!*global_visited)
  743. *global_visited = new hash_set<tree>;
  744. if ((*global_visited)->add (op))
  745. goto done;
  746. }
  747. else
  748. {
  749. if (!visited)
  750. visited = new hash_set<tree>;
  751. if (visited->add (op))
  752. goto done;
  753. }
  754. /* Special case
  755. if (ptr == 0)
  756. ptr = 0;
  757. else
  758. ptr = ptr.foo;
  759. This pattern is implicitly produced for casts to non-primary
  760. bases. When doing context analysis, we do not really care
  761. about the case pointer is NULL, becuase the call will be
  762. undefined anyway. */
  763. if (gimple_code (SSA_NAME_DEF_STMT (op)) == GIMPLE_PHI)
  764. {
  765. gimple phi = SSA_NAME_DEF_STMT (op);
  766. if (gimple_phi_num_args (phi) > 2)
  767. goto done;
  768. if (gimple_phi_num_args (phi) == 1)
  769. op = gimple_phi_arg_def (phi, 0);
  770. else if (integer_zerop (gimple_phi_arg_def (phi, 0)))
  771. op = gimple_phi_arg_def (phi, 1);
  772. else if (integer_zerop (gimple_phi_arg_def (phi, 1)))
  773. op = gimple_phi_arg_def (phi, 0);
  774. else
  775. goto done;
  776. }
  777. else
  778. {
  779. if (gimple_assign_load_p (SSA_NAME_DEF_STMT (op)))
  780. goto done;
  781. op = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op));
  782. }
  783. STRIP_NOPS (op);
  784. }
  785. done:
  786. if (visited)
  787. delete (visited);
  788. return op;
  789. }
  790. /* Create polymorphic call context from IP invariant CST.
  791. This is typically &global_var.
  792. OTR_TYPE specify type of polymorphic call or NULL if unknown, OFF
  793. is offset of call. */
  794. ipa_polymorphic_call_context::ipa_polymorphic_call_context (tree cst,
  795. tree otr_type,
  796. HOST_WIDE_INT off)
  797. {
  798. clear_speculation ();
  799. set_by_invariant (cst, otr_type, off);
  800. }
  801. /* Build context for pointer REF contained in FNDECL at statement STMT.
  802. if INSTANCE is non-NULL, return pointer to the object described by
  803. the context or DECL where context is contained in. */
  804. ipa_polymorphic_call_context::ipa_polymorphic_call_context (tree fndecl,
  805. tree ref,
  806. gimple stmt,
  807. tree *instance)
  808. {
  809. tree otr_type = NULL;
  810. tree base_pointer;
  811. hash_set <tree> *visited = NULL;
  812. if (TREE_CODE (ref) == OBJ_TYPE_REF)
  813. {
  814. otr_type = obj_type_ref_class (ref);
  815. base_pointer = OBJ_TYPE_REF_OBJECT (ref);
  816. }
  817. else
  818. base_pointer = ref;
  819. /* Set up basic info in case we find nothing interesting in the analysis. */
  820. clear_speculation ();
  821. clear_outer_type (otr_type);
  822. invalid = false;
  823. /* Walk SSA for outer object. */
  824. while (true)
  825. {
  826. base_pointer = walk_ssa_copies (base_pointer, &visited);
  827. if (TREE_CODE (base_pointer) == ADDR_EXPR)
  828. {
  829. HOST_WIDE_INT size, max_size;
  830. HOST_WIDE_INT offset2;
  831. tree base = get_ref_base_and_extent (TREE_OPERAND (base_pointer, 0),
  832. &offset2, &size, &max_size);
  833. if (max_size != -1 && max_size == size)
  834. combine_speculation_with (TYPE_MAIN_VARIANT (TREE_TYPE (base)),
  835. offset + offset2,
  836. true,
  837. NULL /* Do not change outer type. */);
  838. /* If this is a varying address, punt. */
  839. if ((TREE_CODE (base) == MEM_REF || DECL_P (base))
  840. && max_size != -1
  841. && max_size == size)
  842. {
  843. /* We found dereference of a pointer. Type of the pointer
  844. and MEM_REF is meaningless, but we can look futher. */
  845. if (TREE_CODE (base) == MEM_REF)
  846. {
  847. base_pointer = TREE_OPERAND (base, 0);
  848. offset
  849. += offset2 + mem_ref_offset (base).to_short_addr () * BITS_PER_UNIT;
  850. outer_type = NULL;
  851. }
  852. /* We found base object. In this case the outer_type
  853. is known. */
  854. else if (DECL_P (base))
  855. {
  856. if (visited)
  857. delete (visited);
  858. /* Only type inconsistent programs can have otr_type that is
  859. not part of outer type. */
  860. if (otr_type
  861. && !contains_type_p (TREE_TYPE (base),
  862. offset + offset2, otr_type))
  863. {
  864. invalid = true;
  865. if (instance)
  866. *instance = base_pointer;
  867. return;
  868. }
  869. set_by_decl (base, offset + offset2);
  870. if (outer_type && maybe_in_construction && stmt)
  871. maybe_in_construction
  872. = decl_maybe_in_construction_p (base,
  873. outer_type,
  874. stmt,
  875. fndecl);
  876. if (instance)
  877. *instance = base;
  878. return;
  879. }
  880. else
  881. break;
  882. }
  883. else
  884. break;
  885. }
  886. else if (TREE_CODE (base_pointer) == POINTER_PLUS_EXPR
  887. && tree_fits_uhwi_p (TREE_OPERAND (base_pointer, 1)))
  888. {
  889. offset += tree_to_shwi (TREE_OPERAND (base_pointer, 1))
  890. * BITS_PER_UNIT;
  891. base_pointer = TREE_OPERAND (base_pointer, 0);
  892. }
  893. else
  894. break;
  895. }
  896. if (visited)
  897. delete (visited);
  898. /* Try to determine type of the outer object. */
  899. if (TREE_CODE (base_pointer) == SSA_NAME
  900. && SSA_NAME_IS_DEFAULT_DEF (base_pointer)
  901. && TREE_CODE (SSA_NAME_VAR (base_pointer)) == PARM_DECL)
  902. {
  903. /* See if parameter is THIS pointer of a method. */
  904. if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE
  905. && SSA_NAME_VAR (base_pointer) == DECL_ARGUMENTS (fndecl))
  906. {
  907. outer_type
  908. = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (base_pointer)));
  909. gcc_assert (TREE_CODE (outer_type) == RECORD_TYPE
  910. || TREE_CODE (outer_type) == UNION_TYPE);
  911. /* Dynamic casting has possibly upcasted the type
  912. in the hiearchy. In this case outer type is less
  913. informative than inner type and we should forget
  914. about it. */
  915. if ((otr_type
  916. && !contains_type_p (outer_type, offset,
  917. otr_type))
  918. || !contains_polymorphic_type_p (outer_type))
  919. {
  920. outer_type = NULL;
  921. if (instance)
  922. *instance = base_pointer;
  923. return;
  924. }
  925. dynamic = true;
  926. /* If the function is constructor or destructor, then
  927. the type is possibly in construction, but we know
  928. it is not derived type. */
  929. if (DECL_CXX_CONSTRUCTOR_P (fndecl)
  930. || DECL_CXX_DESTRUCTOR_P (fndecl))
  931. {
  932. maybe_in_construction = true;
  933. maybe_derived_type = false;
  934. }
  935. else
  936. {
  937. maybe_derived_type = true;
  938. maybe_in_construction = false;
  939. }
  940. if (instance)
  941. *instance = base_pointer;
  942. return;
  943. }
  944. /* Non-PODs passed by value are really passed by invisible
  945. reference. In this case we also know the type of the
  946. object. */
  947. if (DECL_BY_REFERENCE (SSA_NAME_VAR (base_pointer)))
  948. {
  949. outer_type
  950. = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (base_pointer)));
  951. /* Only type inconsistent programs can have otr_type that is
  952. not part of outer type. */
  953. if (otr_type && !contains_type_p (outer_type, offset,
  954. otr_type))
  955. {
  956. invalid = true;
  957. if (instance)
  958. *instance = base_pointer;
  959. return;
  960. }
  961. /* Non-polymorphic types have no interest for us. */
  962. else if (!otr_type && !contains_polymorphic_type_p (outer_type))
  963. {
  964. outer_type = NULL;
  965. if (instance)
  966. *instance = base_pointer;
  967. return;
  968. }
  969. maybe_derived_type = false;
  970. maybe_in_construction = false;
  971. if (instance)
  972. *instance = base_pointer;
  973. return;
  974. }
  975. }
  976. tree base_type = TREE_TYPE (base_pointer);
  977. if (TREE_CODE (base_pointer) == SSA_NAME
  978. && SSA_NAME_IS_DEFAULT_DEF (base_pointer)
  979. && !(TREE_CODE (SSA_NAME_VAR (base_pointer)) == PARM_DECL
  980. || TREE_CODE (SSA_NAME_VAR (base_pointer)) == RESULT_DECL))
  981. {
  982. invalid = true;
  983. if (instance)
  984. *instance = base_pointer;
  985. return;
  986. }
  987. if (TREE_CODE (base_pointer) == SSA_NAME
  988. && SSA_NAME_DEF_STMT (base_pointer)
  989. && gimple_assign_single_p (SSA_NAME_DEF_STMT (base_pointer)))
  990. base_type = TREE_TYPE (gimple_assign_rhs1
  991. (SSA_NAME_DEF_STMT (base_pointer)));
  992. if (base_type && POINTER_TYPE_P (base_type))
  993. combine_speculation_with (TYPE_MAIN_VARIANT (TREE_TYPE (base_type)),
  994. offset,
  995. true, NULL /* Do not change type here */);
  996. /* TODO: There are multiple ways to derive a type. For instance
  997. if BASE_POINTER is passed to an constructor call prior our refernece.
  998. We do not make this type of flow sensitive analysis yet. */
  999. if (instance)
  1000. *instance = base_pointer;
  1001. return;
  1002. }
  1003. /* Structure to be passed in between detect_type_change and
  1004. check_stmt_for_type_change. */
  1005. struct type_change_info
  1006. {
  1007. /* Offset into the object where there is the virtual method pointer we are
  1008. looking for. */
  1009. HOST_WIDE_INT offset;
  1010. /* The declaration or SSA_NAME pointer of the base that we are checking for
  1011. type change. */
  1012. tree instance;
  1013. /* The reference to virtual table pointer used. */
  1014. tree vtbl_ptr_ref;
  1015. tree otr_type;
  1016. /* If we actually can tell the type that the object has changed to, it is
  1017. stored in this field. Otherwise it remains NULL_TREE. */
  1018. tree known_current_type;
  1019. HOST_WIDE_INT known_current_offset;
  1020. /* Set to true if dynamic type change has been detected. */
  1021. bool type_maybe_changed;
  1022. /* Set to true if multiple types have been encountered. known_current_type
  1023. must be disregarded in that case. */
  1024. bool multiple_types_encountered;
  1025. /* Set to true if we possibly missed some dynamic type changes and we should
  1026. consider the set to be speculative. */
  1027. bool speculative;
  1028. bool seen_unanalyzed_store;
  1029. };
  1030. /* Return true if STMT is not call and can modify a virtual method table pointer.
  1031. We take advantage of fact that vtable stores must appear within constructor
  1032. and destructor functions. */
  1033. static bool
  1034. noncall_stmt_may_be_vtbl_ptr_store (gimple stmt)
  1035. {
  1036. if (is_gimple_assign (stmt))
  1037. {
  1038. tree lhs = gimple_assign_lhs (stmt);
  1039. if (gimple_clobber_p (stmt))
  1040. return false;
  1041. if (!AGGREGATE_TYPE_P (TREE_TYPE (lhs)))
  1042. {
  1043. if (flag_strict_aliasing
  1044. && !POINTER_TYPE_P (TREE_TYPE (lhs)))
  1045. return false;
  1046. if (TREE_CODE (lhs) == COMPONENT_REF
  1047. && !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
  1048. return false;
  1049. /* In the future we might want to use get_base_ref_and_offset to find
  1050. if there is a field corresponding to the offset and if so, proceed
  1051. almost like if it was a component ref. */
  1052. }
  1053. }
  1054. /* Code unification may mess with inline stacks. */
  1055. if (cfun->after_inlining)
  1056. return true;
  1057. /* Walk the inline stack and watch out for ctors/dtors.
  1058. TODO: Maybe we can require the store to appear in toplevel
  1059. block of CTOR/DTOR. */
  1060. for (tree block = gimple_block (stmt); block && TREE_CODE (block) == BLOCK;
  1061. block = BLOCK_SUPERCONTEXT (block))
  1062. if (BLOCK_ABSTRACT_ORIGIN (block)
  1063. && TREE_CODE (BLOCK_ABSTRACT_ORIGIN (block)) == FUNCTION_DECL)
  1064. return inlined_polymorphic_ctor_dtor_block_p (block, false);
  1065. return (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE
  1066. && (DECL_CXX_CONSTRUCTOR_P (current_function_decl)
  1067. || DECL_CXX_DESTRUCTOR_P (current_function_decl)));
  1068. }
  1069. /* If STMT can be proved to be an assignment to the virtual method table
  1070. pointer of ANALYZED_OBJ and the type associated with the new table
  1071. identified, return the type. Otherwise return NULL_TREE if type changes
  1072. in unknown way or ERROR_MARK_NODE if type is unchanged. */
  1073. static tree
  1074. extr_type_from_vtbl_ptr_store (gimple stmt, struct type_change_info *tci,
  1075. HOST_WIDE_INT *type_offset)
  1076. {
  1077. HOST_WIDE_INT offset, size, max_size;
  1078. tree lhs, rhs, base;
  1079. if (!gimple_assign_single_p (stmt))
  1080. return NULL_TREE;
  1081. lhs = gimple_assign_lhs (stmt);
  1082. rhs = gimple_assign_rhs1 (stmt);
  1083. if (TREE_CODE (lhs) != COMPONENT_REF
  1084. || !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
  1085. {
  1086. if (dump_file)
  1087. fprintf (dump_file, " LHS is not virtual table.\n");
  1088. return NULL_TREE;
  1089. }
  1090. if (tci->vtbl_ptr_ref && operand_equal_p (lhs, tci->vtbl_ptr_ref, 0))
  1091. ;
  1092. else
  1093. {
  1094. base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
  1095. if (DECL_P (tci->instance))
  1096. {
  1097. if (base != tci->instance)
  1098. {
  1099. if (dump_file)
  1100. {
  1101. fprintf (dump_file, " base:");
  1102. print_generic_expr (dump_file, base, TDF_SLIM);
  1103. fprintf (dump_file, " does not match instance:");
  1104. print_generic_expr (dump_file, tci->instance, TDF_SLIM);
  1105. fprintf (dump_file, "\n");
  1106. }
  1107. return NULL_TREE;
  1108. }
  1109. }
  1110. else if (TREE_CODE (base) == MEM_REF)
  1111. {
  1112. if (!operand_equal_p (tci->instance, TREE_OPERAND (base, 0), 0))
  1113. {
  1114. if (dump_file)
  1115. {
  1116. fprintf (dump_file, " base mem ref:");
  1117. print_generic_expr (dump_file, base, TDF_SLIM);
  1118. fprintf (dump_file, " does not match instance:");
  1119. print_generic_expr (dump_file, tci->instance, TDF_SLIM);
  1120. fprintf (dump_file, "\n");
  1121. }
  1122. return NULL_TREE;
  1123. }
  1124. if (!integer_zerop (TREE_OPERAND (base, 1)))
  1125. {
  1126. if (!tree_fits_shwi_p (TREE_OPERAND (base, 1)))
  1127. {
  1128. if (dump_file)
  1129. {
  1130. fprintf (dump_file, " base mem ref:");
  1131. print_generic_expr (dump_file, base, TDF_SLIM);
  1132. fprintf (dump_file, " has non-representable offset:");
  1133. print_generic_expr (dump_file, tci->instance, TDF_SLIM);
  1134. fprintf (dump_file, "\n");
  1135. }
  1136. return NULL_TREE;
  1137. }
  1138. else
  1139. offset += tree_to_shwi (TREE_OPERAND (base, 1)) * BITS_PER_UNIT;
  1140. }
  1141. }
  1142. else if (!operand_equal_p (tci->instance, base, 0)
  1143. || tci->offset)
  1144. {
  1145. if (dump_file)
  1146. {
  1147. fprintf (dump_file, " base:");
  1148. print_generic_expr (dump_file, base, TDF_SLIM);
  1149. fprintf (dump_file, " does not match instance:");
  1150. print_generic_expr (dump_file, tci->instance, TDF_SLIM);
  1151. fprintf (dump_file, " with offset %i\n", (int)tci->offset);
  1152. }
  1153. return tci->offset > POINTER_SIZE ? error_mark_node : NULL_TREE;
  1154. }
  1155. if (offset != tci->offset
  1156. || size != POINTER_SIZE
  1157. || max_size != POINTER_SIZE)
  1158. {
  1159. if (dump_file)
  1160. fprintf (dump_file, " wrong offset %i!=%i or size %i\n",
  1161. (int)offset, (int)tci->offset, (int)size);
  1162. return offset + POINTER_SIZE <= tci->offset
  1163. || (max_size != -1
  1164. && tci->offset + POINTER_SIZE > offset + max_size)
  1165. ? error_mark_node : NULL;
  1166. }
  1167. }
  1168. tree vtable;
  1169. unsigned HOST_WIDE_INT offset2;
  1170. if (!vtable_pointer_value_to_vtable (rhs, &vtable, &offset2))
  1171. {
  1172. if (dump_file)
  1173. fprintf (dump_file, " Failed to lookup binfo\n");
  1174. return NULL;
  1175. }
  1176. tree binfo = subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable)),
  1177. offset2, vtable);
  1178. if (!binfo)
  1179. {
  1180. if (dump_file)
  1181. fprintf (dump_file, " Construction vtable used\n");
  1182. /* FIXME: We should suport construction contexts. */
  1183. return NULL;
  1184. }
  1185. *type_offset = tree_to_shwi (BINFO_OFFSET (binfo)) * BITS_PER_UNIT;
  1186. return DECL_CONTEXT (vtable);
  1187. }
  1188. /* Record dynamic type change of TCI to TYPE. */
  1189. static void
  1190. record_known_type (struct type_change_info *tci, tree type, HOST_WIDE_INT offset)
  1191. {
  1192. if (dump_file)
  1193. {
  1194. if (type)
  1195. {
  1196. fprintf (dump_file, " Recording type: ");
  1197. print_generic_expr (dump_file, type, TDF_SLIM);
  1198. fprintf (dump_file, " at offset %i\n", (int)offset);
  1199. }
  1200. else
  1201. fprintf (dump_file, " Recording unknown type\n");
  1202. }
  1203. /* If we found a constructor of type that is not polymorphic or
  1204. that may contain the type in question as a field (not as base),
  1205. restrict to the inner class first to make type matching bellow
  1206. happier. */
  1207. if (type
  1208. && (offset
  1209. || (TREE_CODE (type) != RECORD_TYPE
  1210. || !TYPE_BINFO (type)
  1211. || !polymorphic_type_binfo_p (TYPE_BINFO (type)))))
  1212. {
  1213. ipa_polymorphic_call_context context;
  1214. context.offset = offset;
  1215. context.outer_type = type;
  1216. context.maybe_in_construction = false;
  1217. context.maybe_derived_type = false;
  1218. context.dynamic = true;
  1219. /* If we failed to find the inner type, we know that the call
  1220. would be undefined for type produced here. */
  1221. if (!context.restrict_to_inner_class (tci->otr_type))
  1222. {
  1223. if (dump_file)
  1224. fprintf (dump_file, " Ignoring; does not contain otr_type\n");
  1225. return;
  1226. }
  1227. /* Watch for case we reached an POD type and anticipate placement
  1228. new. */
  1229. if (!context.maybe_derived_type)
  1230. {
  1231. type = context.outer_type;
  1232. offset = context.offset;
  1233. }
  1234. }
  1235. if (tci->type_maybe_changed
  1236. && (!types_same_for_odr (type, tci->known_current_type)
  1237. || offset != tci->known_current_offset))
  1238. tci->multiple_types_encountered = true;
  1239. tci->known_current_type = TYPE_MAIN_VARIANT (type);
  1240. tci->known_current_offset = offset;
  1241. tci->type_maybe_changed = true;
  1242. }
  1243. /* Callback of walk_aliased_vdefs and a helper function for
  1244. detect_type_change to check whether a particular statement may modify
  1245. the virtual table pointer, and if possible also determine the new type of
  1246. the (sub-)object. It stores its result into DATA, which points to a
  1247. type_change_info structure. */
  1248. static bool
  1249. check_stmt_for_type_change (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
  1250. {
  1251. gimple stmt = SSA_NAME_DEF_STMT (vdef);
  1252. struct type_change_info *tci = (struct type_change_info *) data;
  1253. tree fn;
  1254. /* If we already gave up, just terminate the rest of walk. */
  1255. if (tci->multiple_types_encountered)
  1256. return true;
  1257. if (is_gimple_call (stmt))
  1258. {
  1259. if (gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE))
  1260. return false;
  1261. /* Check for a constructor call. */
  1262. if ((fn = gimple_call_fndecl (stmt)) != NULL_TREE
  1263. && DECL_CXX_CONSTRUCTOR_P (fn)
  1264. && TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
  1265. && gimple_call_num_args (stmt))
  1266. {
  1267. tree op = walk_ssa_copies (gimple_call_arg (stmt, 0));
  1268. tree type = method_class_type (TREE_TYPE (fn));
  1269. HOST_WIDE_INT offset = 0, size, max_size;
  1270. if (dump_file)
  1271. {
  1272. fprintf (dump_file, " Checking constructor call: ");
  1273. print_gimple_stmt (dump_file, stmt, 0, 0);
  1274. }
  1275. /* See if THIS parameter seems like instance pointer. */
  1276. if (TREE_CODE (op) == ADDR_EXPR)
  1277. {
  1278. op = get_ref_base_and_extent (TREE_OPERAND (op, 0),
  1279. &offset, &size, &max_size);
  1280. if (size != max_size || max_size == -1)
  1281. {
  1282. tci->speculative = true;
  1283. return false;
  1284. }
  1285. if (op && TREE_CODE (op) == MEM_REF)
  1286. {
  1287. if (!tree_fits_shwi_p (TREE_OPERAND (op, 1)))
  1288. {
  1289. tci->speculative = true;
  1290. return false;
  1291. }
  1292. offset += tree_to_shwi (TREE_OPERAND (op, 1))
  1293. * BITS_PER_UNIT;
  1294. op = TREE_OPERAND (op, 0);
  1295. }
  1296. else if (DECL_P (op))
  1297. ;
  1298. else
  1299. {
  1300. tci->speculative = true;
  1301. return false;
  1302. }
  1303. op = walk_ssa_copies (op);
  1304. }
  1305. if (operand_equal_p (op, tci->instance, 0)
  1306. && TYPE_SIZE (type)
  1307. && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
  1308. && tree_fits_shwi_p (TYPE_SIZE (type))
  1309. && tree_to_shwi (TYPE_SIZE (type)) + offset > tci->offset)
  1310. {
  1311. record_known_type (tci, type, tci->offset - offset);
  1312. return true;
  1313. }
  1314. }
  1315. /* Calls may possibly change dynamic type by placement new. Assume
  1316. it will not happen, but make result speculative only. */
  1317. if (dump_file)
  1318. {
  1319. fprintf (dump_file, " Function call may change dynamic type:");
  1320. print_gimple_stmt (dump_file, stmt, 0, 0);
  1321. }
  1322. tci->speculative = true;
  1323. return false;
  1324. }
  1325. /* Check for inlined virtual table store. */
  1326. else if (noncall_stmt_may_be_vtbl_ptr_store (stmt))
  1327. {
  1328. tree type;
  1329. HOST_WIDE_INT offset = 0;
  1330. if (dump_file)
  1331. {
  1332. fprintf (dump_file, " Checking vtbl store: ");
  1333. print_gimple_stmt (dump_file, stmt, 0, 0);
  1334. }
  1335. type = extr_type_from_vtbl_ptr_store (stmt, tci, &offset);
  1336. if (type == error_mark_node)
  1337. return false;
  1338. gcc_assert (!type || TYPE_MAIN_VARIANT (type) == type);
  1339. if (!type)
  1340. {
  1341. if (dump_file)
  1342. fprintf (dump_file, " Unanalyzed store may change type.\n");
  1343. tci->seen_unanalyzed_store = true;
  1344. tci->speculative = true;
  1345. }
  1346. else
  1347. record_known_type (tci, type, offset);
  1348. return true;
  1349. }
  1350. else
  1351. return false;
  1352. }
  1353. /* THIS is polymorphic call context obtained from get_polymorphic_context.
  1354. OTR_OBJECT is pointer to the instance returned by OBJ_TYPE_REF_OBJECT.
  1355. INSTANCE is pointer to the outer instance as returned by
  1356. get_polymorphic_context. To avoid creation of temporary expressions,
  1357. INSTANCE may also be an declaration of get_polymorphic_context found the
  1358. value to be in static storage.
  1359. If the type of instance is not fully determined
  1360. (either OUTER_TYPE is unknown or MAYBE_IN_CONSTRUCTION/INCLUDE_DERIVED_TYPES
  1361. is set), try to walk memory writes and find the actual construction of the
  1362. instance.
  1363. Return true if memory is unchanged from function entry.
  1364. We do not include this analysis in the context analysis itself, because
  1365. it needs memory SSA to be fully built and the walk may be expensive.
  1366. So it is not suitable for use withing fold_stmt and similar uses. */
  1367. bool
  1368. ipa_polymorphic_call_context::get_dynamic_type (tree instance,
  1369. tree otr_object,
  1370. tree otr_type,
  1371. gimple call)
  1372. {
  1373. struct type_change_info tci;
  1374. ao_ref ao;
  1375. bool function_entry_reached = false;
  1376. tree instance_ref = NULL;
  1377. gimple stmt = call;
  1378. /* Remember OFFSET before it is modified by restrict_to_inner_class.
  1379. This is because we do not update INSTANCE when walking inwards. */
  1380. HOST_WIDE_INT instance_offset = offset;
  1381. if (otr_type)
  1382. otr_type = TYPE_MAIN_VARIANT (otr_type);
  1383. /* Walk into inner type. This may clear maybe_derived_type and save us
  1384. from useless work. It also makes later comparsions with static type
  1385. easier. */
  1386. if (outer_type && otr_type)
  1387. {
  1388. if (!restrict_to_inner_class (otr_type))
  1389. return false;
  1390. }
  1391. if (!maybe_in_construction && !maybe_derived_type)
  1392. return false;
  1393. /* We need to obtain refernce to virtual table pointer. It is better
  1394. to look it up in the code rather than build our own. This require bit
  1395. of pattern matching, but we end up verifying that what we found is
  1396. correct.
  1397. What we pattern match is:
  1398. tmp = instance->_vptr.A; // vtbl ptr load
  1399. tmp2 = tmp[otr_token]; // vtable lookup
  1400. OBJ_TYPE_REF(tmp2;instance->0) (instance);
  1401. We want to start alias oracle walk from vtbl pointer load,
  1402. but we may not be able to identify it, for example, when PRE moved the
  1403. load around. */
  1404. if (gimple_code (call) == GIMPLE_CALL)
  1405. {
  1406. tree ref = gimple_call_fn (call);
  1407. HOST_WIDE_INT offset2, size, max_size;
  1408. if (TREE_CODE (ref) == OBJ_TYPE_REF)
  1409. {
  1410. ref = OBJ_TYPE_REF_EXPR (ref);
  1411. ref = walk_ssa_copies (ref);
  1412. /* Check if definition looks like vtable lookup. */
  1413. if (TREE_CODE (ref) == SSA_NAME
  1414. && !SSA_NAME_IS_DEFAULT_DEF (ref)
  1415. && gimple_assign_load_p (SSA_NAME_DEF_STMT (ref))
  1416. && TREE_CODE (gimple_assign_rhs1
  1417. (SSA_NAME_DEF_STMT (ref))) == MEM_REF)
  1418. {
  1419. ref = get_base_address
  1420. (TREE_OPERAND (gimple_assign_rhs1
  1421. (SSA_NAME_DEF_STMT (ref)), 0));
  1422. ref = walk_ssa_copies (ref);
  1423. /* Find base address of the lookup and see if it looks like
  1424. vptr load. */
  1425. if (TREE_CODE (ref) == SSA_NAME
  1426. && !SSA_NAME_IS_DEFAULT_DEF (ref)
  1427. && gimple_assign_load_p (SSA_NAME_DEF_STMT (ref)))
  1428. {
  1429. tree ref_exp = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (ref));
  1430. tree base_ref = get_ref_base_and_extent
  1431. (ref_exp, &offset2, &size, &max_size);
  1432. /* Finally verify that what we found looks like read from OTR_OBJECT
  1433. or from INSTANCE with offset OFFSET. */
  1434. if (base_ref
  1435. && ((TREE_CODE (base_ref) == MEM_REF
  1436. && ((offset2 == instance_offset
  1437. && TREE_OPERAND (base_ref, 0) == instance)
  1438. || (!offset2 && TREE_OPERAND (base_ref, 0) == otr_object)))
  1439. || (DECL_P (instance) && base_ref == instance
  1440. && offset2 == instance_offset)))
  1441. {
  1442. stmt = SSA_NAME_DEF_STMT (ref);
  1443. instance_ref = ref_exp;
  1444. }
  1445. }
  1446. }
  1447. }
  1448. }
  1449. /* If we failed to look up the refernece in code, build our own. */
  1450. if (!instance_ref)
  1451. {
  1452. /* If the statement in question does not use memory, we can't tell
  1453. anything. */
  1454. if (!gimple_vuse (stmt))
  1455. return false;
  1456. ao_ref_init_from_ptr_and_size (&ao, otr_object, NULL);
  1457. }
  1458. else
  1459. /* Otherwise use the real reference. */
  1460. ao_ref_init (&ao, instance_ref);
  1461. /* We look for vtbl pointer read. */
  1462. ao.size = POINTER_SIZE;
  1463. ao.max_size = ao.size;
  1464. if (otr_type)
  1465. ao.ref_alias_set
  1466. = get_deref_alias_set (TREE_TYPE (BINFO_VTABLE (TYPE_BINFO (otr_type))));
  1467. if (dump_file)
  1468. {
  1469. fprintf (dump_file, "Determining dynamic type for call: ");
  1470. print_gimple_stmt (dump_file, call, 0, 0);
  1471. fprintf (dump_file, " Starting walk at: ");
  1472. print_gimple_stmt (dump_file, stmt, 0, 0);
  1473. fprintf (dump_file, " instance pointer: ");
  1474. print_generic_expr (dump_file, otr_object, TDF_SLIM);
  1475. fprintf (dump_file, " Outer instance pointer: ");
  1476. print_generic_expr (dump_file, instance, TDF_SLIM);
  1477. fprintf (dump_file, " offset: %i (bits)", (int)offset);
  1478. fprintf (dump_file, " vtbl reference: ");
  1479. print_generic_expr (dump_file, instance_ref, TDF_SLIM);
  1480. fprintf (dump_file, "\n");
  1481. }
  1482. tci.offset = offset;
  1483. tci.instance = instance;
  1484. tci.vtbl_ptr_ref = instance_ref;
  1485. gcc_assert (TREE_CODE (instance) != MEM_REF);
  1486. tci.known_current_type = NULL_TREE;
  1487. tci.known_current_offset = 0;
  1488. tci.otr_type = otr_type;
  1489. tci.type_maybe_changed = false;
  1490. tci.multiple_types_encountered = false;
  1491. tci.speculative = false;
  1492. tci.seen_unanalyzed_store = false;
  1493. walk_aliased_vdefs (&ao, gimple_vuse (stmt), check_stmt_for_type_change,
  1494. &tci, NULL, &function_entry_reached);
  1495. /* If we did not find any type changing statements, we may still drop
  1496. maybe_in_construction flag if the context already have outer type.
  1497. Here we make special assumptions about both constructors and
  1498. destructors which are all the functions that are allowed to alter the
  1499. VMT pointers. It assumes that destructors begin with assignment into
  1500. all VMT pointers and that constructors essentially look in the
  1501. following way:
  1502. 1) The very first thing they do is that they call constructors of
  1503. ancestor sub-objects that have them.
  1504. 2) Then VMT pointers of this and all its ancestors is set to new
  1505. values corresponding to the type corresponding to the constructor.
  1506. 3) Only afterwards, other stuff such as constructor of member
  1507. sub-objects and the code written by the user is run. Only this may
  1508. include calling virtual functions, directly or indirectly.
  1509. 4) placement new can not be used to change type of non-POD statically
  1510. allocated variables.
  1511. There is no way to call a constructor of an ancestor sub-object in any
  1512. other way.
  1513. This means that we do not have to care whether constructors get the
  1514. correct type information because they will always change it (in fact,
  1515. if we define the type to be given by the VMT pointer, it is undefined).
  1516. The most important fact to derive from the above is that if, for some
  1517. statement in the section 3, we try to detect whether the dynamic type
  1518. has changed, we can safely ignore all calls as we examine the function
  1519. body backwards until we reach statements in section 2 because these
  1520. calls cannot be ancestor constructors or destructors (if the input is
  1521. not bogus) and so do not change the dynamic type (this holds true only
  1522. for automatically allocated objects but at the moment we devirtualize
  1523. only these). We then must detect that statements in section 2 change
  1524. the dynamic type and can try to derive the new type. That is enough
  1525. and we can stop, we will never see the calls into constructors of
  1526. sub-objects in this code.
  1527. Therefore if the static outer type was found (outer_type)
  1528. we can safely ignore tci.speculative that is set on calls and give up
  1529. only if there was dyanmic type store that may affect given variable
  1530. (seen_unanalyzed_store) */
  1531. if (!tci.type_maybe_changed
  1532. || (outer_type
  1533. && !dynamic
  1534. && !tci.seen_unanalyzed_store
  1535. && !tci.multiple_types_encountered
  1536. && offset == tci.offset
  1537. && types_same_for_odr (tci.known_current_type,
  1538. outer_type)))
  1539. {
  1540. if (!outer_type || tci.seen_unanalyzed_store)
  1541. return false;
  1542. if (maybe_in_construction)
  1543. maybe_in_construction = false;
  1544. if (dump_file)
  1545. fprintf (dump_file, " No dynamic type change found.\n");
  1546. return true;
  1547. }
  1548. if (tci.known_current_type
  1549. && !function_entry_reached
  1550. && !tci.multiple_types_encountered)
  1551. {
  1552. if (!tci.speculative)
  1553. {
  1554. outer_type = TYPE_MAIN_VARIANT (tci.known_current_type);
  1555. offset = tci.known_current_offset;
  1556. dynamic = true;
  1557. maybe_in_construction = false;
  1558. maybe_derived_type = false;
  1559. if (dump_file)
  1560. fprintf (dump_file, " Determined dynamic type.\n");
  1561. }
  1562. else if (!speculative_outer_type
  1563. || speculative_maybe_derived_type)
  1564. {
  1565. speculative_outer_type = TYPE_MAIN_VARIANT (tci.known_current_type);
  1566. speculative_offset = tci.known_current_offset;
  1567. speculative_maybe_derived_type = false;
  1568. if (dump_file)
  1569. fprintf (dump_file, " Determined speculative dynamic type.\n");
  1570. }
  1571. }
  1572. else if (dump_file)
  1573. {
  1574. fprintf (dump_file, " Found multiple types%s%s\n",
  1575. function_entry_reached ? " (function entry reached)" : "",
  1576. function_entry_reached ? " (multiple types encountered)" : "");
  1577. }
  1578. return false;
  1579. }
  1580. /* See if speculation given by SPEC_OUTER_TYPE, SPEC_OFFSET and SPEC_MAYBE_DERIVED_TYPE
  1581. seems consistent (and useful) with what we already have in the non-speculative context. */
  1582. bool
  1583. ipa_polymorphic_call_context::speculation_consistent_p (tree spec_outer_type,
  1584. HOST_WIDE_INT spec_offset,
  1585. bool spec_maybe_derived_type,
  1586. tree otr_type) const
  1587. {
  1588. if (!flag_devirtualize_speculatively)
  1589. return false;
  1590. /* Non-polymorphic types are useless for deriving likely polymorphic
  1591. call targets. */
  1592. if (!spec_outer_type || !contains_polymorphic_type_p (spec_outer_type))
  1593. return false;
  1594. /* If we know nothing, speculation is always good. */
  1595. if (!outer_type)
  1596. return true;
  1597. /* Speculation is only useful to avoid derived types.
  1598. This is not 100% true for placement new, where the outer context may
  1599. turn out to be useless, but ignore these for now. */
  1600. if (!maybe_derived_type)
  1601. return false;
  1602. /* If types agrees, speculation is consistent, but it makes sense only
  1603. when it says something new. */
  1604. if (types_must_be_same_for_odr (spec_outer_type, outer_type))
  1605. return maybe_derived_type && !spec_maybe_derived_type;
  1606. /* If speculation does not contain the type in question, ignore it. */
  1607. if (otr_type
  1608. && !contains_type_p (spec_outer_type, spec_offset, otr_type, false, true))
  1609. return false;
  1610. /* If outer type already contains speculation as a filed,
  1611. it is useless. We already know from OUTER_TYPE
  1612. SPEC_TYPE and that it is not in the construction. */
  1613. if (contains_type_p (outer_type, offset - spec_offset,
  1614. spec_outer_type, false, false))
  1615. return false;
  1616. /* If speculative outer type is not more specified than outer
  1617. type, just give up.
  1618. We can only decide this safely if we can compare types with OUTER_TYPE.
  1619. */
  1620. if ((!in_lto_p || odr_type_p (outer_type))
  1621. && !contains_type_p (spec_outer_type,
  1622. spec_offset - offset,
  1623. outer_type, false))
  1624. return false;
  1625. return true;
  1626. }
  1627. /* Improve THIS with speculation described by NEW_OUTER_TYPE, NEW_OFFSET,
  1628. NEW_MAYBE_DERIVED_TYPE
  1629. If OTR_TYPE is set, assume the context is used with OTR_TYPE. */
  1630. bool
  1631. ipa_polymorphic_call_context::combine_speculation_with
  1632. (tree new_outer_type, HOST_WIDE_INT new_offset, bool new_maybe_derived_type,
  1633. tree otr_type)
  1634. {
  1635. if (!new_outer_type)
  1636. return false;
  1637. /* restrict_to_inner_class may eliminate wrong speculation making our job
  1638. easeier. */
  1639. if (otr_type)
  1640. restrict_to_inner_class (otr_type);
  1641. if (!speculation_consistent_p (new_outer_type, new_offset,
  1642. new_maybe_derived_type, otr_type))
  1643. return false;
  1644. /* New speculation is a win in case we have no speculation or new
  1645. speculation does not consider derivations. */
  1646. if (!speculative_outer_type
  1647. || (speculative_maybe_derived_type
  1648. && !new_maybe_derived_type))
  1649. {
  1650. speculative_outer_type = new_outer_type;
  1651. speculative_offset = new_offset;
  1652. speculative_maybe_derived_type = new_maybe_derived_type;
  1653. return true;
  1654. }
  1655. else if (types_must_be_same_for_odr (speculative_outer_type,
  1656. new_outer_type))
  1657. {
  1658. if (speculative_offset != new_offset)
  1659. {
  1660. /* OK we have two contexts that seems valid but they disagree,
  1661. just give up.
  1662. This is not a lattice operation, so we may want to drop it later. */
  1663. if (dump_file && (dump_flags & TDF_DETAILS))
  1664. fprintf (dump_file,
  1665. "Speculative outer types match, "
  1666. "offset mismatch -> invalid speculation\n");
  1667. clear_speculation ();
  1668. return true;
  1669. }
  1670. else
  1671. {
  1672. if (speculative_maybe_derived_type && !new_maybe_derived_type)
  1673. {
  1674. speculative_maybe_derived_type = false;
  1675. return true;
  1676. }
  1677. else
  1678. return false;
  1679. }
  1680. }
  1681. /* Choose type that contains the other. This one either contains the outer
  1682. as a field (thus giving exactly one target) or is deeper in the type
  1683. hiearchy. */
  1684. else if (speculative_outer_type
  1685. && speculative_maybe_derived_type
  1686. && (new_offset > speculative_offset
  1687. || (new_offset == speculative_offset
  1688. && contains_type_p (new_outer_type,
  1689. 0, speculative_outer_type, false))))
  1690. {
  1691. tree old_outer_type = speculative_outer_type;
  1692. HOST_WIDE_INT old_offset = speculative_offset;
  1693. bool old_maybe_derived_type = speculative_maybe_derived_type;
  1694. speculative_outer_type = new_outer_type;
  1695. speculative_offset = new_offset;
  1696. speculative_maybe_derived_type = new_maybe_derived_type;
  1697. if (otr_type)
  1698. restrict_to_inner_class (otr_type);
  1699. /* If the speculation turned out to make no sense, revert to sensible
  1700. one. */
  1701. if (!speculative_outer_type)
  1702. {
  1703. speculative_outer_type = old_outer_type;
  1704. speculative_offset = old_offset;
  1705. speculative_maybe_derived_type = old_maybe_derived_type;
  1706. return false;
  1707. }
  1708. return (old_offset != speculative_offset
  1709. || old_maybe_derived_type != speculative_maybe_derived_type
  1710. || types_must_be_same_for_odr (speculative_outer_type,
  1711. new_outer_type));
  1712. }
  1713. return false;
  1714. }
  1715. /* Make speculation less specific so
  1716. NEW_OUTER_TYPE, NEW_OFFSET, NEW_MAYBE_DERIVED_TYPE is also included.
  1717. If OTR_TYPE is set, assume the context is used with OTR_TYPE. */
  1718. bool
  1719. ipa_polymorphic_call_context::meet_speculation_with
  1720. (tree new_outer_type, HOST_WIDE_INT new_offset, bool new_maybe_derived_type,
  1721. tree otr_type)
  1722. {
  1723. if (!new_outer_type && speculative_outer_type)
  1724. {
  1725. clear_speculation ();
  1726. return true;
  1727. }
  1728. /* restrict_to_inner_class may eliminate wrong speculation making our job
  1729. easeier. */
  1730. if (otr_type)
  1731. restrict_to_inner_class (otr_type);
  1732. if (!speculative_outer_type
  1733. || !speculation_consistent_p (speculative_outer_type,
  1734. speculative_offset,
  1735. speculative_maybe_derived_type,
  1736. otr_type))
  1737. return false;
  1738. if (!speculation_consistent_p (new_outer_type, new_offset,
  1739. new_maybe_derived_type, otr_type))
  1740. {
  1741. clear_speculation ();
  1742. return true;
  1743. }
  1744. else if (types_must_be_same_for_odr (speculative_outer_type,
  1745. new_outer_type))
  1746. {
  1747. if (speculative_offset != new_offset)
  1748. {
  1749. clear_speculation ();
  1750. return true;
  1751. }
  1752. else
  1753. {
  1754. if (!speculative_maybe_derived_type && new_maybe_derived_type)
  1755. {
  1756. speculative_maybe_derived_type = true;
  1757. return true;
  1758. }
  1759. else
  1760. return false;
  1761. }
  1762. }
  1763. /* See if one type contains the other as a field (not base). */
  1764. else if (contains_type_p (new_outer_type, new_offset - speculative_offset,
  1765. speculative_outer_type, false, false))
  1766. return false;
  1767. else if (contains_type_p (speculative_outer_type,
  1768. speculative_offset - new_offset,
  1769. new_outer_type, false, false))
  1770. {
  1771. speculative_outer_type = new_outer_type;
  1772. speculative_offset = new_offset;
  1773. speculative_maybe_derived_type = new_maybe_derived_type;
  1774. return true;
  1775. }
  1776. /* See if OUTER_TYPE is base of CTX.OUTER_TYPE. */
  1777. else if (contains_type_p (new_outer_type,
  1778. new_offset - speculative_offset,
  1779. speculative_outer_type, false, true))
  1780. {
  1781. if (!speculative_maybe_derived_type)
  1782. {
  1783. speculative_maybe_derived_type = true;
  1784. return true;
  1785. }
  1786. return false;
  1787. }
  1788. /* See if CTX.OUTER_TYPE is base of OUTER_TYPE. */
  1789. else if (contains_type_p (speculative_outer_type,
  1790. speculative_offset - new_offset, new_outer_type, false, true))
  1791. {
  1792. speculative_outer_type = new_outer_type;
  1793. speculative_offset = new_offset;
  1794. speculative_maybe_derived_type = true;
  1795. return true;
  1796. }
  1797. else
  1798. {
  1799. if (dump_file && (dump_flags & TDF_DETAILS))
  1800. fprintf (dump_file, "Giving up on speculative meet\n");
  1801. clear_speculation ();
  1802. return true;
  1803. }
  1804. }
  1805. /* Assume that both THIS and a given context is valid and strenghten THIS
  1806. if possible. Return true if any strenghtening was made.
  1807. If actual type the context is being used in is known, OTR_TYPE should be
  1808. set accordingly. This improves quality of combined result. */
  1809. bool
  1810. ipa_polymorphic_call_context::combine_with (ipa_polymorphic_call_context ctx,
  1811. tree otr_type)
  1812. {
  1813. bool updated = false;
  1814. if (ctx.useless_p () || invalid)
  1815. return false;
  1816. /* Restricting context to inner type makes merging easier, however do not
  1817. do that unless we know how the context is used (OTR_TYPE is non-NULL) */
  1818. if (otr_type && !invalid && !ctx.invalid)
  1819. {
  1820. restrict_to_inner_class (otr_type);
  1821. ctx.restrict_to_inner_class (otr_type);
  1822. if(invalid)
  1823. return false;
  1824. }
  1825. if (dump_file && (dump_flags & TDF_DETAILS))
  1826. {
  1827. fprintf (dump_file, "Polymorphic call context combine:");
  1828. dump (dump_file);
  1829. fprintf (dump_file, "With context: ");
  1830. ctx.dump (dump_file);
  1831. if (otr_type)
  1832. {
  1833. fprintf (dump_file, "To be used with type: ");
  1834. print_generic_expr (dump_file, otr_type, TDF_SLIM);
  1835. fprintf (dump_file, "\n");
  1836. }
  1837. }
  1838. /* If call is known to be invalid, we are done. */
  1839. if (ctx.invalid)
  1840. {
  1841. if (dump_file && (dump_flags & TDF_DETAILS))
  1842. fprintf (dump_file, "-> Invalid context\n");
  1843. goto invalidate;
  1844. }
  1845. if (!ctx.outer_type)
  1846. ;
  1847. else if (!outer_type)
  1848. {
  1849. outer_type = ctx.outer_type;
  1850. offset = ctx.offset;
  1851. dynamic = ctx.dynamic;
  1852. maybe_in_construction = ctx.maybe_in_construction;
  1853. maybe_derived_type = ctx.maybe_derived_type;
  1854. updated = true;
  1855. }
  1856. /* If types are known to be same, merging is quite easy. */
  1857. else if (types_must_be_same_for_odr (outer_type, ctx.outer_type))
  1858. {
  1859. if (offset != ctx.offset
  1860. && TYPE_SIZE (outer_type)
  1861. && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST)
  1862. {
  1863. if (dump_file && (dump_flags & TDF_DETAILS))
  1864. fprintf (dump_file, "Outer types match, offset mismatch -> invalid\n");
  1865. clear_speculation ();
  1866. clear_outer_type ();
  1867. invalid = true;
  1868. return true;
  1869. }
  1870. if (dump_file && (dump_flags & TDF_DETAILS))
  1871. fprintf (dump_file, "Outer types match, merging flags\n");
  1872. if (maybe_in_construction && !ctx.maybe_in_construction)
  1873. {
  1874. updated = true;
  1875. maybe_in_construction = false;
  1876. }
  1877. if (maybe_derived_type && !ctx.maybe_derived_type)
  1878. {
  1879. updated = true;
  1880. maybe_derived_type = false;
  1881. }
  1882. if (dynamic && !ctx.dynamic)
  1883. {
  1884. updated = true;
  1885. dynamic = false;
  1886. }
  1887. }
  1888. /* If we know the type precisely, there is not much to improve. */
  1889. else if (!maybe_derived_type && !maybe_in_construction
  1890. && !ctx.maybe_derived_type && !ctx.maybe_in_construction)
  1891. {
  1892. /* It may be easy to check if second context permits the first
  1893. and set INVALID otherwise. This is not easy to do in general;
  1894. contains_type_p may return false negatives for non-comparable
  1895. types.
  1896. If OTR_TYPE is known, we however can expect that
  1897. restrict_to_inner_class should have discovered the same base
  1898. type. */
  1899. if (otr_type && !ctx.maybe_in_construction && !ctx.maybe_derived_type)
  1900. {
  1901. if (dump_file && (dump_flags & TDF_DETAILS))
  1902. fprintf (dump_file, "Contextes disagree -> invalid\n");
  1903. goto invalidate;
  1904. }
  1905. }
  1906. /* See if one type contains the other as a field (not base).
  1907. In this case we want to choose the wider type, because it contains
  1908. more information. */
  1909. else if (contains_type_p (ctx.outer_type, ctx.offset - offset,
  1910. outer_type, false, false))
  1911. {
  1912. if (dump_file && (dump_flags & TDF_DETAILS))
  1913. fprintf (dump_file, "Second type contain the first as a field\n");
  1914. if (maybe_derived_type)
  1915. {
  1916. outer_type = ctx.outer_type;
  1917. maybe_derived_type = ctx.maybe_derived_type;
  1918. offset = ctx.offset;
  1919. dynamic = ctx.dynamic;
  1920. updated = true;
  1921. }
  1922. /* If we do not know how the context is being used, we can
  1923. not clear MAYBE_IN_CONSTRUCTION because it may be offseted
  1924. to other component of OUTER_TYPE later and we know nothing
  1925. about it. */
  1926. if (otr_type && maybe_in_construction
  1927. && !ctx.maybe_in_construction)
  1928. {
  1929. maybe_in_construction = false;
  1930. updated = true;
  1931. }
  1932. }
  1933. else if (contains_type_p (outer_type, offset - ctx.offset,
  1934. ctx.outer_type, false, false))
  1935. {
  1936. if (dump_file && (dump_flags & TDF_DETAILS))
  1937. fprintf (dump_file, "First type contain the second as a field\n");
  1938. if (otr_type && maybe_in_construction
  1939. && !ctx.maybe_in_construction)
  1940. {
  1941. maybe_in_construction = false;
  1942. updated = true;
  1943. }
  1944. }
  1945. /* See if OUTER_TYPE is base of CTX.OUTER_TYPE. */
  1946. else if (contains_type_p (ctx.outer_type,
  1947. ctx.offset - offset, outer_type, false, true))
  1948. {
  1949. if (dump_file && (dump_flags & TDF_DETAILS))
  1950. fprintf (dump_file, "First type is base of second\n");
  1951. if (!maybe_derived_type)
  1952. {
  1953. if (!ctx.maybe_in_construction
  1954. && types_odr_comparable (outer_type, ctx.outer_type))
  1955. {
  1956. if (dump_file && (dump_flags & TDF_DETAILS))
  1957. fprintf (dump_file, "Second context does not permit base -> invalid\n");
  1958. goto invalidate;
  1959. }
  1960. }
  1961. /* Pick variant deeper in the hiearchy. */
  1962. else
  1963. {
  1964. outer_type = ctx.outer_type;
  1965. maybe_in_construction = ctx.maybe_in_construction;
  1966. maybe_derived_type = ctx.maybe_derived_type;
  1967. offset = ctx.offset;
  1968. dynamic = ctx.dynamic;
  1969. updated = true;
  1970. }
  1971. }
  1972. /* See if CTX.OUTER_TYPE is base of OUTER_TYPE. */
  1973. else if (contains_type_p (outer_type,
  1974. offset - ctx.offset, ctx.outer_type, false, true))
  1975. {
  1976. if (dump_file && (dump_flags & TDF_DETAILS))
  1977. fprintf (dump_file, "Second type is base of first\n");
  1978. if (!ctx.maybe_derived_type)
  1979. {
  1980. if (!maybe_in_construction
  1981. && types_odr_comparable (outer_type, ctx.outer_type))
  1982. {
  1983. if (dump_file && (dump_flags & TDF_DETAILS))
  1984. fprintf (dump_file, "First context does not permit base -> invalid\n");
  1985. goto invalidate;
  1986. }
  1987. /* Pick the base type. */
  1988. else if (maybe_in_construction)
  1989. {
  1990. outer_type = ctx.outer_type;
  1991. maybe_in_construction = ctx.maybe_in_construction;
  1992. maybe_derived_type = ctx.maybe_derived_type;
  1993. offset = ctx.offset;
  1994. dynamic = ctx.dynamic;
  1995. updated = true;
  1996. }
  1997. }
  1998. }
  1999. /* TODO handle merging using hiearchy. */
  2000. else if (dump_file && (dump_flags & TDF_DETAILS))
  2001. fprintf (dump_file, "Giving up on merge\n");
  2002. updated |= combine_speculation_with (ctx.speculative_outer_type,
  2003. ctx.speculative_offset,
  2004. ctx.speculative_maybe_derived_type,
  2005. otr_type);
  2006. if (updated && dump_file && (dump_flags & TDF_DETAILS))
  2007. {
  2008. fprintf (dump_file, "Updated as: ");
  2009. dump (dump_file);
  2010. fprintf (dump_file, "\n");
  2011. }
  2012. return updated;
  2013. invalidate:
  2014. invalid = true;
  2015. clear_speculation ();
  2016. clear_outer_type ();
  2017. return true;
  2018. }
  2019. /* Take non-speculative info, merge it with speculative and clear speculation.
  2020. Used when we no longer manage to keep track of actual outer type, but we
  2021. think it is still there.
  2022. If OTR_TYPE is set, the transformation can be done more effectively assuming
  2023. that context is going to be used only that way. */
  2024. void
  2025. ipa_polymorphic_call_context::make_speculative (tree otr_type)
  2026. {
  2027. tree spec_outer_type = outer_type;
  2028. HOST_WIDE_INT spec_offset = offset;
  2029. bool spec_maybe_derived_type = maybe_derived_type;
  2030. if (invalid)
  2031. {
  2032. invalid = false;
  2033. clear_outer_type ();
  2034. clear_speculation ();
  2035. return;
  2036. }
  2037. if (!outer_type)
  2038. return;
  2039. clear_outer_type ();
  2040. combine_speculation_with (spec_outer_type, spec_offset,
  2041. spec_maybe_derived_type,
  2042. otr_type);
  2043. }
  2044. /* Use when we can not track dynamic type change. This speculatively assume
  2045. type change is not happening. */
  2046. void
  2047. ipa_polymorphic_call_context::possible_dynamic_type_change (bool in_poly_cdtor,
  2048. tree otr_type)
  2049. {
  2050. if (dynamic)
  2051. make_speculative (otr_type);
  2052. else if (in_poly_cdtor)
  2053. maybe_in_construction = true;
  2054. }
  2055. /* Return TRUE if this context conveys the same information as OTHER. */
  2056. bool
  2057. ipa_polymorphic_call_context::equal_to
  2058. (const ipa_polymorphic_call_context &x) const
  2059. {
  2060. if (useless_p ())
  2061. return x.useless_p ();
  2062. if (invalid)
  2063. return x.invalid;
  2064. if (x.useless_p () || x.invalid)
  2065. return false;
  2066. if (outer_type)
  2067. {
  2068. if (!x.outer_type
  2069. || !types_odr_comparable (outer_type, x.outer_type)
  2070. || !types_same_for_odr (outer_type, x.outer_type)
  2071. || offset != x.offset
  2072. || maybe_in_construction != x.maybe_in_construction
  2073. || maybe_derived_type != x.maybe_derived_type
  2074. || dynamic != x.dynamic)
  2075. return false;
  2076. }
  2077. else if (x.outer_type)
  2078. return false;
  2079. if (speculative_outer_type
  2080. && speculation_consistent_p (speculative_outer_type, speculative_offset,
  2081. speculative_maybe_derived_type, NULL_TREE))
  2082. {
  2083. if (!x.speculative_outer_type)
  2084. return false;
  2085. if (!types_odr_comparable (speculative_outer_type,
  2086. x.speculative_outer_type)
  2087. || !types_same_for_odr (speculative_outer_type,
  2088. x.speculative_outer_type)
  2089. || speculative_offset != x.speculative_offset
  2090. || speculative_maybe_derived_type != x.speculative_maybe_derived_type)
  2091. return false;
  2092. }
  2093. else if (x.speculative_outer_type
  2094. && x.speculation_consistent_p (x.speculative_outer_type,
  2095. x.speculative_offset,
  2096. x.speculative_maybe_derived_type,
  2097. NULL))
  2098. return false;
  2099. return true;
  2100. }
  2101. /* Modify context to be strictly less restrictive than CTX. */
  2102. bool
  2103. ipa_polymorphic_call_context::meet_with (ipa_polymorphic_call_context ctx,
  2104. tree otr_type)
  2105. {
  2106. bool updated = false;
  2107. if (useless_p () || ctx.invalid)
  2108. return false;
  2109. /* Restricting context to inner type makes merging easier, however do not
  2110. do that unless we know how the context is used (OTR_TYPE is non-NULL) */
  2111. if (otr_type && !useless_p () && !ctx.useless_p ())
  2112. {
  2113. restrict_to_inner_class (otr_type);
  2114. ctx.restrict_to_inner_class (otr_type);
  2115. if(invalid)
  2116. return false;
  2117. }
  2118. if (equal_to (ctx))
  2119. return false;
  2120. if (ctx.useless_p () || invalid)
  2121. {
  2122. *this = ctx;
  2123. return true;
  2124. }
  2125. if (dump_file && (dump_flags & TDF_DETAILS))
  2126. {
  2127. fprintf (dump_file, "Polymorphic call context meet:");
  2128. dump (dump_file);
  2129. fprintf (dump_file, "With context: ");
  2130. ctx.dump (dump_file);
  2131. if (otr_type)
  2132. {
  2133. fprintf (dump_file, "To be used with type: ");
  2134. print_generic_expr (dump_file, otr_type, TDF_SLIM);
  2135. fprintf (dump_file, "\n");
  2136. }
  2137. }
  2138. if (!dynamic && ctx.dynamic)
  2139. {
  2140. dynamic = true;
  2141. updated = true;
  2142. }
  2143. /* If call is known to be invalid, we are done. */
  2144. if (!outer_type)
  2145. ;
  2146. else if (!ctx.outer_type)
  2147. {
  2148. clear_outer_type ();
  2149. updated = true;
  2150. }
  2151. /* If types are known to be same, merging is quite easy. */
  2152. else if (types_must_be_same_for_odr (outer_type, ctx.outer_type))
  2153. {
  2154. if (offset != ctx.offset
  2155. && TYPE_SIZE (outer_type)
  2156. && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST)
  2157. {
  2158. if (dump_file && (dump_flags & TDF_DETAILS))
  2159. fprintf (dump_file, "Outer types match, offset mismatch -> clearing\n");
  2160. clear_outer_type ();
  2161. return true;
  2162. }
  2163. if (dump_file && (dump_flags & TDF_DETAILS))
  2164. fprintf (dump_file, "Outer types match, merging flags\n");
  2165. if (!maybe_in_construction && ctx.maybe_in_construction)
  2166. {
  2167. updated = true;
  2168. maybe_in_construction = true;
  2169. }
  2170. if (!maybe_derived_type && ctx.maybe_derived_type)
  2171. {
  2172. updated = true;
  2173. maybe_derived_type = true;
  2174. }
  2175. if (!dynamic && ctx.dynamic)
  2176. {
  2177. updated = true;
  2178. dynamic = true;
  2179. }
  2180. }
  2181. /* See if one type contains the other as a field (not base). */
  2182. else if (contains_type_p (ctx.outer_type, ctx.offset - offset,
  2183. outer_type, false, false))
  2184. {
  2185. if (dump_file && (dump_flags & TDF_DETAILS))
  2186. fprintf (dump_file, "Second type contain the first as a field\n");
  2187. /* The second type is more specified, so we keep the first.
  2188. We need to set DYNAMIC flag to avoid declaring context INVALID
  2189. of OFFSET ends up being out of range. */
  2190. if (!dynamic
  2191. && (ctx.dynamic
  2192. || (!otr_type
  2193. && (!TYPE_SIZE (ctx.outer_type)
  2194. || !TYPE_SIZE (outer_type)
  2195. || !operand_equal_p (TYPE_SIZE (ctx.outer_type),
  2196. TYPE_SIZE (outer_type), 0)))))
  2197. {
  2198. dynamic = true;
  2199. updated = true;
  2200. }
  2201. }
  2202. else if (contains_type_p (outer_type, offset - ctx.offset,
  2203. ctx.outer_type, false, false))
  2204. {
  2205. if (dump_file && (dump_flags & TDF_DETAILS))
  2206. fprintf (dump_file, "First type contain the second as a field\n");
  2207. if (!dynamic
  2208. && (ctx.dynamic
  2209. || (!otr_type
  2210. && (!TYPE_SIZE (ctx.outer_type)
  2211. || !TYPE_SIZE (outer_type)
  2212. || !operand_equal_p (TYPE_SIZE (ctx.outer_type),
  2213. TYPE_SIZE (outer_type), 0)))))
  2214. dynamic = true;
  2215. outer_type = ctx.outer_type;
  2216. offset = ctx.offset;
  2217. dynamic = ctx.dynamic;
  2218. maybe_in_construction = ctx.maybe_in_construction;
  2219. maybe_derived_type = ctx.maybe_derived_type;
  2220. updated = true;
  2221. }
  2222. /* See if OUTER_TYPE is base of CTX.OUTER_TYPE. */
  2223. else if (contains_type_p (ctx.outer_type,
  2224. ctx.offset - offset, outer_type, false, true))
  2225. {
  2226. if (dump_file && (dump_flags & TDF_DETAILS))
  2227. fprintf (dump_file, "First type is base of second\n");
  2228. if (!maybe_derived_type)
  2229. {
  2230. maybe_derived_type = true;
  2231. updated = true;
  2232. }
  2233. if (!maybe_in_construction && ctx.maybe_in_construction)
  2234. {
  2235. maybe_in_construction = true;
  2236. updated = true;
  2237. }
  2238. if (!dynamic && ctx.dynamic)
  2239. {
  2240. dynamic = true;
  2241. updated = true;
  2242. }
  2243. }
  2244. /* See if CTX.OUTER_TYPE is base of OUTER_TYPE. */
  2245. else if (contains_type_p (outer_type,
  2246. offset - ctx.offset, ctx.outer_type, false, true))
  2247. {
  2248. if (dump_file && (dump_flags & TDF_DETAILS))
  2249. fprintf (dump_file, "Second type is base of first\n");
  2250. outer_type = ctx.outer_type;
  2251. offset = ctx.offset;
  2252. updated = true;
  2253. if (!maybe_derived_type)
  2254. maybe_derived_type = true;
  2255. if (!maybe_in_construction && ctx.maybe_in_construction)
  2256. maybe_in_construction = true;
  2257. if (!dynamic && ctx.dynamic)
  2258. dynamic = true;
  2259. }
  2260. /* TODO handle merging using hiearchy. */
  2261. else
  2262. {
  2263. if (dump_file && (dump_flags & TDF_DETAILS))
  2264. fprintf (dump_file, "Giving up on meet\n");
  2265. clear_outer_type ();
  2266. updated = true;
  2267. }
  2268. updated |= meet_speculation_with (ctx.speculative_outer_type,
  2269. ctx.speculative_offset,
  2270. ctx.speculative_maybe_derived_type,
  2271. otr_type);
  2272. if (updated && dump_file && (dump_flags & TDF_DETAILS))
  2273. {
  2274. fprintf (dump_file, "Updated as: ");
  2275. dump (dump_file);
  2276. fprintf (dump_file, "\n");
  2277. }
  2278. return updated;
  2279. }