tree-nested.c 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074
  1. /* Nested function decomposition for GIMPLE.
  2. Copyright (C) 2004-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GCC is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include "config.h"
  16. #include "system.h"
  17. #include "coretypes.h"
  18. #include "tm.h"
  19. #include "hash-set.h"
  20. #include "machmode.h"
  21. #include "vec.h"
  22. #include "double-int.h"
  23. #include "input.h"
  24. #include "alias.h"
  25. #include "symtab.h"
  26. #include "wide-int.h"
  27. #include "inchash.h"
  28. #include "tree.h"
  29. #include "fold-const.h"
  30. #include "stringpool.h"
  31. #include "stor-layout.h"
  32. #include "tm_p.h"
  33. #include "hard-reg-set.h"
  34. #include "function.h"
  35. #include "tree-dump.h"
  36. #include "tree-inline.h"
  37. #include "predict.h"
  38. #include "basic-block.h"
  39. #include "tree-ssa-alias.h"
  40. #include "internal-fn.h"
  41. #include "gimple-expr.h"
  42. #include "is-a.h"
  43. #include "gimple.h"
  44. #include "gimplify.h"
  45. #include "gimple-iterator.h"
  46. #include "gimple-walk.h"
  47. #include "tree-iterator.h"
  48. #include "bitmap.h"
  49. #include "hash-map.h"
  50. #include "plugin-api.h"
  51. #include "ipa-ref.h"
  52. #include "cgraph.h"
  53. #include "tree-cfg.h"
  54. #include "hashtab.h"
  55. #include "rtl.h"
  56. #include "flags.h"
  57. #include "statistics.h"
  58. #include "real.h"
  59. #include "fixed-value.h"
  60. #include "insn-config.h"
  61. #include "expmed.h"
  62. #include "dojump.h"
  63. #include "explow.h"
  64. #include "calls.h"
  65. #include "emit-rtl.h"
  66. #include "varasm.h"
  67. #include "stmt.h"
  68. #include "expr.h" /* FIXME: For STACK_SAVEAREA_MODE and SAVE_NONLOCAL. */
  69. #include "langhooks.h"
  70. #include "gimple-low.h"
  71. #include "gomp-constants.h"
  72. /* The object of this pass is to lower the representation of a set of nested
  73. functions in order to expose all of the gory details of the various
  74. nonlocal references. We want to do this sooner rather than later, in
  75. order to give us more freedom in emitting all of the functions in question.
  76. Back in olden times, when gcc was young, we developed an insanely
  77. complicated scheme whereby variables which were referenced nonlocally
  78. were forced to live in the stack of the declaring function, and then
  79. the nested functions magically discovered where these variables were
  80. placed. In order for this scheme to function properly, it required
  81. that the outer function be partially expanded, then we switch to
  82. compiling the inner function, and once done with those we switch back
  83. to compiling the outer function. Such delicate ordering requirements
  84. makes it difficult to do whole translation unit optimizations
  85. involving such functions.
  86. The implementation here is much more direct. Everything that can be
  87. referenced by an inner function is a member of an explicitly created
  88. structure herein called the "nonlocal frame struct". The incoming
  89. static chain for a nested function is a pointer to this struct in
  90. the parent. In this way, we settle on known offsets from a known
  91. base, and so are decoupled from the logic that places objects in the
  92. function's stack frame. More importantly, we don't have to wait for
  93. that to happen -- since the compilation of the inner function is no
  94. longer tied to a real stack frame, the nonlocal frame struct can be
  95. allocated anywhere. Which means that the outer function is now
  96. inlinable.
  97. Theory of operation here is very simple. Iterate over all the
  98. statements in all the functions (depth first) several times,
  99. allocating structures and fields on demand. In general we want to
  100. examine inner functions first, so that we can avoid making changes
  101. to outer functions which are unnecessary.
  102. The order of the passes matters a bit, in that later passes will be
  103. skipped if it is discovered that the functions don't actually interact
  104. at all. That is, they're nested in the lexical sense but could have
  105. been written as independent functions without change. */
  106. struct nesting_info
  107. {
  108. struct nesting_info *outer;
  109. struct nesting_info *inner;
  110. struct nesting_info *next;
  111. hash_map<tree, tree> *field_map;
  112. hash_map<tree, tree> *var_map;
  113. hash_set<tree *> *mem_refs;
  114. bitmap suppress_expansion;
  115. tree context;
  116. tree new_local_var_chain;
  117. tree debug_var_chain;
  118. tree frame_type;
  119. tree frame_decl;
  120. tree chain_field;
  121. tree chain_decl;
  122. tree nl_goto_field;
  123. bool any_parm_remapped;
  124. bool any_tramp_created;
  125. char static_chain_added;
  126. };
  127. /* Iterate over the nesting tree, starting with ROOT, depth first. */
  128. static inline struct nesting_info *
  129. iter_nestinfo_start (struct nesting_info *root)
  130. {
  131. while (root->inner)
  132. root = root->inner;
  133. return root;
  134. }
  135. static inline struct nesting_info *
  136. iter_nestinfo_next (struct nesting_info *node)
  137. {
  138. if (node->next)
  139. return iter_nestinfo_start (node->next);
  140. return node->outer;
  141. }
  142. #define FOR_EACH_NEST_INFO(I, ROOT) \
  143. for ((I) = iter_nestinfo_start (ROOT); (I); (I) = iter_nestinfo_next (I))
  144. /* Obstack used for the bitmaps in the struct above. */
  145. static struct bitmap_obstack nesting_info_bitmap_obstack;
  146. /* We're working in so many different function contexts simultaneously,
  147. that create_tmp_var is dangerous. Prevent mishap. */
  148. #define create_tmp_var cant_use_create_tmp_var_here_dummy
  149. /* Like create_tmp_var, except record the variable for registration at
  150. the given nesting level. */
  151. static tree
  152. create_tmp_var_for (struct nesting_info *info, tree type, const char *prefix)
  153. {
  154. tree tmp_var;
  155. /* If the type is of variable size or a type which must be created by the
  156. frontend, something is wrong. Note that we explicitly allow
  157. incomplete types here, since we create them ourselves here. */
  158. gcc_assert (!TREE_ADDRESSABLE (type));
  159. gcc_assert (!TYPE_SIZE_UNIT (type)
  160. || TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
  161. tmp_var = create_tmp_var_raw (type, prefix);
  162. DECL_CONTEXT (tmp_var) = info->context;
  163. DECL_CHAIN (tmp_var) = info->new_local_var_chain;
  164. DECL_SEEN_IN_BIND_EXPR_P (tmp_var) = 1;
  165. if (TREE_CODE (type) == COMPLEX_TYPE
  166. || TREE_CODE (type) == VECTOR_TYPE)
  167. DECL_GIMPLE_REG_P (tmp_var) = 1;
  168. info->new_local_var_chain = tmp_var;
  169. return tmp_var;
  170. }
  171. /* Take the address of EXP to be used within function CONTEXT.
  172. Mark it for addressability as necessary. */
  173. tree
  174. build_addr (tree exp, tree context)
  175. {
  176. tree base = exp;
  177. tree save_context;
  178. tree retval;
  179. while (handled_component_p (base))
  180. base = TREE_OPERAND (base, 0);
  181. if (DECL_P (base))
  182. TREE_ADDRESSABLE (base) = 1;
  183. /* Building the ADDR_EXPR will compute a set of properties for
  184. that ADDR_EXPR. Those properties are unfortunately context
  185. specific, i.e., they are dependent on CURRENT_FUNCTION_DECL.
  186. Temporarily set CURRENT_FUNCTION_DECL to the desired context,
  187. build the ADDR_EXPR, then restore CURRENT_FUNCTION_DECL. That
  188. way the properties are for the ADDR_EXPR are computed properly. */
  189. save_context = current_function_decl;
  190. current_function_decl = context;
  191. retval = build_fold_addr_expr (exp);
  192. current_function_decl = save_context;
  193. return retval;
  194. }
  195. /* Insert FIELD into TYPE, sorted by alignment requirements. */
  196. void
  197. insert_field_into_struct (tree type, tree field)
  198. {
  199. tree *p;
  200. DECL_CONTEXT (field) = type;
  201. for (p = &TYPE_FIELDS (type); *p ; p = &DECL_CHAIN (*p))
  202. if (DECL_ALIGN (field) >= DECL_ALIGN (*p))
  203. break;
  204. DECL_CHAIN (field) = *p;
  205. *p = field;
  206. /* Set correct alignment for frame struct type. */
  207. if (TYPE_ALIGN (type) < DECL_ALIGN (field))
  208. TYPE_ALIGN (type) = DECL_ALIGN (field);
  209. }
  210. /* Build or return the RECORD_TYPE that describes the frame state that is
  211. shared between INFO->CONTEXT and its nested functions. This record will
  212. not be complete until finalize_nesting_tree; up until that point we'll
  213. be adding fields as necessary.
  214. We also build the DECL that represents this frame in the function. */
  215. static tree
  216. get_frame_type (struct nesting_info *info)
  217. {
  218. tree type = info->frame_type;
  219. if (!type)
  220. {
  221. char *name;
  222. type = make_node (RECORD_TYPE);
  223. name = concat ("FRAME.",
  224. IDENTIFIER_POINTER (DECL_NAME (info->context)),
  225. NULL);
  226. TYPE_NAME (type) = get_identifier (name);
  227. free (name);
  228. info->frame_type = type;
  229. info->frame_decl = create_tmp_var_for (info, type, "FRAME");
  230. DECL_NONLOCAL_FRAME (info->frame_decl) = 1;
  231. /* ??? Always make it addressable for now, since it is meant to
  232. be pointed to by the static chain pointer. This pessimizes
  233. when it turns out that no static chains are needed because
  234. the nested functions referencing non-local variables are not
  235. reachable, but the true pessimization is to create the non-
  236. local frame structure in the first place. */
  237. TREE_ADDRESSABLE (info->frame_decl) = 1;
  238. }
  239. return type;
  240. }
  241. /* Return true if DECL should be referenced by pointer in the non-local
  242. frame structure. */
  243. static bool
  244. use_pointer_in_frame (tree decl)
  245. {
  246. if (TREE_CODE (decl) == PARM_DECL)
  247. {
  248. /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
  249. sized decls, and inefficient to copy large aggregates. Don't bother
  250. moving anything but scalar variables. */
  251. return AGGREGATE_TYPE_P (TREE_TYPE (decl));
  252. }
  253. else
  254. {
  255. /* Variable sized types make things "interesting" in the frame. */
  256. return DECL_SIZE (decl) == NULL || !TREE_CONSTANT (DECL_SIZE (decl));
  257. }
  258. }
  259. /* Given DECL, a non-locally accessed variable, find or create a field
  260. in the non-local frame structure for the given nesting context. */
  261. static tree
  262. lookup_field_for_decl (struct nesting_info *info, tree decl,
  263. enum insert_option insert)
  264. {
  265. if (insert == NO_INSERT)
  266. {
  267. tree *slot = info->field_map->get (decl);
  268. return slot ? *slot : NULL_TREE;
  269. }
  270. tree *slot = &info->field_map->get_or_insert (decl);
  271. if (!*slot)
  272. {
  273. tree field = make_node (FIELD_DECL);
  274. DECL_NAME (field) = DECL_NAME (decl);
  275. if (use_pointer_in_frame (decl))
  276. {
  277. TREE_TYPE (field) = build_pointer_type (TREE_TYPE (decl));
  278. DECL_ALIGN (field) = TYPE_ALIGN (TREE_TYPE (field));
  279. DECL_NONADDRESSABLE_P (field) = 1;
  280. }
  281. else
  282. {
  283. TREE_TYPE (field) = TREE_TYPE (decl);
  284. DECL_SOURCE_LOCATION (field) = DECL_SOURCE_LOCATION (decl);
  285. DECL_ALIGN (field) = DECL_ALIGN (decl);
  286. DECL_USER_ALIGN (field) = DECL_USER_ALIGN (decl);
  287. TREE_ADDRESSABLE (field) = TREE_ADDRESSABLE (decl);
  288. DECL_NONADDRESSABLE_P (field) = !TREE_ADDRESSABLE (decl);
  289. TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (decl);
  290. }
  291. insert_field_into_struct (get_frame_type (info), field);
  292. *slot = field;
  293. if (TREE_CODE (decl) == PARM_DECL)
  294. info->any_parm_remapped = true;
  295. }
  296. return *slot;
  297. }
  298. /* Build or return the variable that holds the static chain within
  299. INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
  300. static tree
  301. get_chain_decl (struct nesting_info *info)
  302. {
  303. tree decl = info->chain_decl;
  304. if (!decl)
  305. {
  306. tree type;
  307. type = get_frame_type (info->outer);
  308. type = build_pointer_type (type);
  309. /* Note that this variable is *not* entered into any BIND_EXPR;
  310. the construction of this variable is handled specially in
  311. expand_function_start and initialize_inlined_parameters.
  312. Note also that it's represented as a parameter. This is more
  313. close to the truth, since the initial value does come from
  314. the caller. */
  315. decl = build_decl (DECL_SOURCE_LOCATION (info->context),
  316. PARM_DECL, create_tmp_var_name ("CHAIN"), type);
  317. DECL_ARTIFICIAL (decl) = 1;
  318. DECL_IGNORED_P (decl) = 1;
  319. TREE_USED (decl) = 1;
  320. DECL_CONTEXT (decl) = info->context;
  321. DECL_ARG_TYPE (decl) = type;
  322. /* Tell tree-inline.c that we never write to this variable, so
  323. it can copy-prop the replacement value immediately. */
  324. TREE_READONLY (decl) = 1;
  325. info->chain_decl = decl;
  326. if (dump_file
  327. && (dump_flags & TDF_DETAILS)
  328. && !DECL_STATIC_CHAIN (info->context))
  329. fprintf (dump_file, "Setting static-chain for %s\n",
  330. lang_hooks.decl_printable_name (info->context, 2));
  331. DECL_STATIC_CHAIN (info->context) = 1;
  332. }
  333. return decl;
  334. }
  335. /* Build or return the field within the non-local frame state that holds
  336. the static chain for INFO->CONTEXT. This is the way to walk back up
  337. multiple nesting levels. */
  338. static tree
  339. get_chain_field (struct nesting_info *info)
  340. {
  341. tree field = info->chain_field;
  342. if (!field)
  343. {
  344. tree type = build_pointer_type (get_frame_type (info->outer));
  345. field = make_node (FIELD_DECL);
  346. DECL_NAME (field) = get_identifier ("__chain");
  347. TREE_TYPE (field) = type;
  348. DECL_ALIGN (field) = TYPE_ALIGN (type);
  349. DECL_NONADDRESSABLE_P (field) = 1;
  350. insert_field_into_struct (get_frame_type (info), field);
  351. info->chain_field = field;
  352. if (dump_file
  353. && (dump_flags & TDF_DETAILS)
  354. && !DECL_STATIC_CHAIN (info->context))
  355. fprintf (dump_file, "Setting static-chain for %s\n",
  356. lang_hooks.decl_printable_name (info->context, 2));
  357. DECL_STATIC_CHAIN (info->context) = 1;
  358. }
  359. return field;
  360. }
  361. /* Initialize a new temporary with the GIMPLE_CALL STMT. */
  362. static tree
  363. init_tmp_var_with_call (struct nesting_info *info, gimple_stmt_iterator *gsi,
  364. gcall *call)
  365. {
  366. tree t;
  367. t = create_tmp_var_for (info, gimple_call_return_type (call), NULL);
  368. gimple_call_set_lhs (call, t);
  369. if (! gsi_end_p (*gsi))
  370. gimple_set_location (call, gimple_location (gsi_stmt (*gsi)));
  371. gsi_insert_before (gsi, call, GSI_SAME_STMT);
  372. return t;
  373. }
  374. /* Copy EXP into a temporary. Allocate the temporary in the context of
  375. INFO and insert the initialization statement before GSI. */
  376. static tree
  377. init_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
  378. {
  379. tree t;
  380. gimple stmt;
  381. t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
  382. stmt = gimple_build_assign (t, exp);
  383. if (! gsi_end_p (*gsi))
  384. gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
  385. gsi_insert_before_without_update (gsi, stmt, GSI_SAME_STMT);
  386. return t;
  387. }
  388. /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
  389. static tree
  390. gsi_gimplify_val (struct nesting_info *info, tree exp,
  391. gimple_stmt_iterator *gsi)
  392. {
  393. if (is_gimple_val (exp))
  394. return exp;
  395. else
  396. return init_tmp_var (info, exp, gsi);
  397. }
  398. /* Similarly, but copy from the temporary and insert the statement
  399. after the iterator. */
  400. static tree
  401. save_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
  402. {
  403. tree t;
  404. gimple stmt;
  405. t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
  406. stmt = gimple_build_assign (exp, t);
  407. if (! gsi_end_p (*gsi))
  408. gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
  409. gsi_insert_after_without_update (gsi, stmt, GSI_SAME_STMT);
  410. return t;
  411. }
  412. /* Build or return the type used to represent a nested function trampoline. */
  413. static GTY(()) tree trampoline_type;
  414. static tree
  415. get_trampoline_type (struct nesting_info *info)
  416. {
  417. unsigned align, size;
  418. tree t;
  419. if (trampoline_type)
  420. return trampoline_type;
  421. align = TRAMPOLINE_ALIGNMENT;
  422. size = TRAMPOLINE_SIZE;
  423. /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
  424. then allocate extra space so that we can do dynamic alignment. */
  425. if (align > STACK_BOUNDARY)
  426. {
  427. size += ((align/BITS_PER_UNIT) - 1) & -(STACK_BOUNDARY/BITS_PER_UNIT);
  428. align = STACK_BOUNDARY;
  429. }
  430. t = build_index_type (size_int (size - 1));
  431. t = build_array_type (char_type_node, t);
  432. t = build_decl (DECL_SOURCE_LOCATION (info->context),
  433. FIELD_DECL, get_identifier ("__data"), t);
  434. DECL_ALIGN (t) = align;
  435. DECL_USER_ALIGN (t) = 1;
  436. trampoline_type = make_node (RECORD_TYPE);
  437. TYPE_NAME (trampoline_type) = get_identifier ("__builtin_trampoline");
  438. TYPE_FIELDS (trampoline_type) = t;
  439. layout_type (trampoline_type);
  440. DECL_CONTEXT (t) = trampoline_type;
  441. return trampoline_type;
  442. }
  443. /* Given DECL, a nested function, find or create a field in the non-local
  444. frame structure for a trampoline for this function. */
  445. static tree
  446. lookup_tramp_for_decl (struct nesting_info *info, tree decl,
  447. enum insert_option insert)
  448. {
  449. if (insert == NO_INSERT)
  450. {
  451. tree *slot = info->var_map->get (decl);
  452. return slot ? *slot : NULL_TREE;
  453. }
  454. tree *slot = &info->var_map->get_or_insert (decl);
  455. if (!*slot)
  456. {
  457. tree field = make_node (FIELD_DECL);
  458. DECL_NAME (field) = DECL_NAME (decl);
  459. TREE_TYPE (field) = get_trampoline_type (info);
  460. TREE_ADDRESSABLE (field) = 1;
  461. insert_field_into_struct (get_frame_type (info), field);
  462. *slot = field;
  463. info->any_tramp_created = true;
  464. }
  465. return *slot;
  466. }
  467. /* Build or return the field within the non-local frame state that holds
  468. the non-local goto "jmp_buf". The buffer itself is maintained by the
  469. rtl middle-end as dynamic stack space is allocated. */
  470. static tree
  471. get_nl_goto_field (struct nesting_info *info)
  472. {
  473. tree field = info->nl_goto_field;
  474. if (!field)
  475. {
  476. unsigned size;
  477. tree type;
  478. /* For __builtin_nonlocal_goto, we need N words. The first is the
  479. frame pointer, the rest is for the target's stack pointer save
  480. area. The number of words is controlled by STACK_SAVEAREA_MODE;
  481. not the best interface, but it'll do for now. */
  482. if (Pmode == ptr_mode)
  483. type = ptr_type_node;
  484. else
  485. type = lang_hooks.types.type_for_mode (Pmode, 1);
  486. size = GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL));
  487. size = size / GET_MODE_SIZE (Pmode);
  488. size = size + 1;
  489. type = build_array_type
  490. (type, build_index_type (size_int (size)));
  491. field = make_node (FIELD_DECL);
  492. DECL_NAME (field) = get_identifier ("__nl_goto_buf");
  493. TREE_TYPE (field) = type;
  494. DECL_ALIGN (field) = TYPE_ALIGN (type);
  495. TREE_ADDRESSABLE (field) = 1;
  496. insert_field_into_struct (get_frame_type (info), field);
  497. info->nl_goto_field = field;
  498. }
  499. return field;
  500. }
  501. /* Invoke CALLBACK on all statements of GIMPLE sequence *PSEQ. */
  502. static void
  503. walk_body (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
  504. struct nesting_info *info, gimple_seq *pseq)
  505. {
  506. struct walk_stmt_info wi;
  507. memset (&wi, 0, sizeof (wi));
  508. wi.info = info;
  509. wi.val_only = true;
  510. walk_gimple_seq_mod (pseq, callback_stmt, callback_op, &wi);
  511. }
  512. /* Invoke CALLBACK_STMT/CALLBACK_OP on all statements of INFO->CONTEXT. */
  513. static inline void
  514. walk_function (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
  515. struct nesting_info *info)
  516. {
  517. gimple_seq body = gimple_body (info->context);
  518. walk_body (callback_stmt, callback_op, info, &body);
  519. gimple_set_body (info->context, body);
  520. }
  521. /* Invoke CALLBACK on a GIMPLE_OMP_FOR's init, cond, incr and pre-body. */
  522. static void
  523. walk_gimple_omp_for (gomp_for *for_stmt,
  524. walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
  525. struct nesting_info *info)
  526. {
  527. struct walk_stmt_info wi;
  528. gimple_seq seq;
  529. tree t;
  530. size_t i;
  531. walk_body (callback_stmt, callback_op, info, gimple_omp_for_pre_body_ptr (for_stmt));
  532. seq = NULL;
  533. memset (&wi, 0, sizeof (wi));
  534. wi.info = info;
  535. wi.gsi = gsi_last (seq);
  536. for (i = 0; i < gimple_omp_for_collapse (for_stmt); i++)
  537. {
  538. wi.val_only = false;
  539. walk_tree (gimple_omp_for_index_ptr (for_stmt, i), callback_op,
  540. &wi, NULL);
  541. wi.val_only = true;
  542. wi.is_lhs = false;
  543. walk_tree (gimple_omp_for_initial_ptr (for_stmt, i), callback_op,
  544. &wi, NULL);
  545. wi.val_only = true;
  546. wi.is_lhs = false;
  547. walk_tree (gimple_omp_for_final_ptr (for_stmt, i), callback_op,
  548. &wi, NULL);
  549. t = gimple_omp_for_incr (for_stmt, i);
  550. gcc_assert (BINARY_CLASS_P (t));
  551. wi.val_only = false;
  552. walk_tree (&TREE_OPERAND (t, 0), callback_op, &wi, NULL);
  553. wi.val_only = true;
  554. wi.is_lhs = false;
  555. walk_tree (&TREE_OPERAND (t, 1), callback_op, &wi, NULL);
  556. }
  557. seq = gsi_seq (wi.gsi);
  558. if (!gimple_seq_empty_p (seq))
  559. {
  560. gimple_seq pre_body = gimple_omp_for_pre_body (for_stmt);
  561. annotate_all_with_location (seq, gimple_location (for_stmt));
  562. gimple_seq_add_seq (&pre_body, seq);
  563. gimple_omp_for_set_pre_body (for_stmt, pre_body);
  564. }
  565. }
  566. /* Similarly for ROOT and all functions nested underneath, depth first. */
  567. static void
  568. walk_all_functions (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
  569. struct nesting_info *root)
  570. {
  571. struct nesting_info *n;
  572. FOR_EACH_NEST_INFO (n, root)
  573. walk_function (callback_stmt, callback_op, n);
  574. }
  575. /* We have to check for a fairly pathological case. The operands of function
  576. nested function are to be interpreted in the context of the enclosing
  577. function. So if any are variably-sized, they will get remapped when the
  578. enclosing function is inlined. But that remapping would also have to be
  579. done in the types of the PARM_DECLs of the nested function, meaning the
  580. argument types of that function will disagree with the arguments in the
  581. calls to that function. So we'd either have to make a copy of the nested
  582. function corresponding to each time the enclosing function was inlined or
  583. add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
  584. function. The former is not practical. The latter would still require
  585. detecting this case to know when to add the conversions. So, for now at
  586. least, we don't inline such an enclosing function.
  587. We have to do that check recursively, so here return indicating whether
  588. FNDECL has such a nested function. ORIG_FN is the function we were
  589. trying to inline to use for checking whether any argument is variably
  590. modified by anything in it.
  591. It would be better to do this in tree-inline.c so that we could give
  592. the appropriate warning for why a function can't be inlined, but that's
  593. too late since the nesting structure has already been flattened and
  594. adding a flag just to record this fact seems a waste of a flag. */
  595. static bool
  596. check_for_nested_with_variably_modified (tree fndecl, tree orig_fndecl)
  597. {
  598. struct cgraph_node *cgn = cgraph_node::get (fndecl);
  599. tree arg;
  600. for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
  601. {
  602. for (arg = DECL_ARGUMENTS (cgn->decl); arg; arg = DECL_CHAIN (arg))
  603. if (variably_modified_type_p (TREE_TYPE (arg), orig_fndecl))
  604. return true;
  605. if (check_for_nested_with_variably_modified (cgn->decl,
  606. orig_fndecl))
  607. return true;
  608. }
  609. return false;
  610. }
  611. /* Construct our local datastructure describing the function nesting
  612. tree rooted by CGN. */
  613. static struct nesting_info *
  614. create_nesting_tree (struct cgraph_node *cgn)
  615. {
  616. struct nesting_info *info = XCNEW (struct nesting_info);
  617. info->field_map = new hash_map<tree, tree>;
  618. info->var_map = new hash_map<tree, tree>;
  619. info->mem_refs = new hash_set<tree *>;
  620. info->suppress_expansion = BITMAP_ALLOC (&nesting_info_bitmap_obstack);
  621. info->context = cgn->decl;
  622. for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
  623. {
  624. struct nesting_info *sub = create_nesting_tree (cgn);
  625. sub->outer = info;
  626. sub->next = info->inner;
  627. info->inner = sub;
  628. }
  629. /* See discussion at check_for_nested_with_variably_modified for a
  630. discussion of why this has to be here. */
  631. if (check_for_nested_with_variably_modified (info->context, info->context))
  632. DECL_UNINLINABLE (info->context) = true;
  633. return info;
  634. }
  635. /* Return an expression computing the static chain for TARGET_CONTEXT
  636. from INFO->CONTEXT. Insert any necessary computations before TSI. */
  637. static tree
  638. get_static_chain (struct nesting_info *info, tree target_context,
  639. gimple_stmt_iterator *gsi)
  640. {
  641. struct nesting_info *i;
  642. tree x;
  643. if (info->context == target_context)
  644. {
  645. x = build_addr (info->frame_decl, target_context);
  646. info->static_chain_added |= 1;
  647. }
  648. else
  649. {
  650. x = get_chain_decl (info);
  651. info->static_chain_added |= 2;
  652. for (i = info->outer; i->context != target_context; i = i->outer)
  653. {
  654. tree field = get_chain_field (i);
  655. x = build_simple_mem_ref (x);
  656. x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
  657. x = init_tmp_var (info, x, gsi);
  658. }
  659. }
  660. return x;
  661. }
  662. /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
  663. frame as seen from INFO->CONTEXT. Insert any necessary computations
  664. before GSI. */
  665. static tree
  666. get_frame_field (struct nesting_info *info, tree target_context,
  667. tree field, gimple_stmt_iterator *gsi)
  668. {
  669. struct nesting_info *i;
  670. tree x;
  671. if (info->context == target_context)
  672. {
  673. /* Make sure frame_decl gets created. */
  674. (void) get_frame_type (info);
  675. x = info->frame_decl;
  676. info->static_chain_added |= 1;
  677. }
  678. else
  679. {
  680. x = get_chain_decl (info);
  681. info->static_chain_added |= 2;
  682. for (i = info->outer; i->context != target_context; i = i->outer)
  683. {
  684. tree field = get_chain_field (i);
  685. x = build_simple_mem_ref (x);
  686. x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
  687. x = init_tmp_var (info, x, gsi);
  688. }
  689. x = build_simple_mem_ref (x);
  690. }
  691. x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
  692. return x;
  693. }
  694. static void note_nonlocal_vla_type (struct nesting_info *info, tree type);
  695. /* A subroutine of convert_nonlocal_reference_op. Create a local variable
  696. in the nested function with DECL_VALUE_EXPR set to reference the true
  697. variable in the parent function. This is used both for debug info
  698. and in OMP lowering. */
  699. static tree
  700. get_nonlocal_debug_decl (struct nesting_info *info, tree decl)
  701. {
  702. tree target_context;
  703. struct nesting_info *i;
  704. tree x, field, new_decl;
  705. tree *slot = &info->var_map->get_or_insert (decl);
  706. if (*slot)
  707. return *slot;
  708. target_context = decl_function_context (decl);
  709. /* A copy of the code in get_frame_field, but without the temporaries. */
  710. if (info->context == target_context)
  711. {
  712. /* Make sure frame_decl gets created. */
  713. (void) get_frame_type (info);
  714. x = info->frame_decl;
  715. i = info;
  716. info->static_chain_added |= 1;
  717. }
  718. else
  719. {
  720. x = get_chain_decl (info);
  721. info->static_chain_added |= 2;
  722. for (i = info->outer; i->context != target_context; i = i->outer)
  723. {
  724. field = get_chain_field (i);
  725. x = build_simple_mem_ref (x);
  726. x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
  727. }
  728. x = build_simple_mem_ref (x);
  729. }
  730. field = lookup_field_for_decl (i, decl, INSERT);
  731. x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
  732. if (use_pointer_in_frame (decl))
  733. x = build_simple_mem_ref (x);
  734. /* ??? We should be remapping types as well, surely. */
  735. new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
  736. VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
  737. DECL_CONTEXT (new_decl) = info->context;
  738. DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
  739. DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
  740. TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
  741. TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
  742. TREE_READONLY (new_decl) = TREE_READONLY (decl);
  743. TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
  744. DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
  745. if ((TREE_CODE (decl) == PARM_DECL
  746. || TREE_CODE (decl) == RESULT_DECL
  747. || TREE_CODE (decl) == VAR_DECL)
  748. && DECL_BY_REFERENCE (decl))
  749. DECL_BY_REFERENCE (new_decl) = 1;
  750. SET_DECL_VALUE_EXPR (new_decl, x);
  751. DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
  752. *slot = new_decl;
  753. DECL_CHAIN (new_decl) = info->debug_var_chain;
  754. info->debug_var_chain = new_decl;
  755. if (!optimize
  756. && info->context != target_context
  757. && variably_modified_type_p (TREE_TYPE (decl), NULL))
  758. note_nonlocal_vla_type (info, TREE_TYPE (decl));
  759. return new_decl;
  760. }
  761. /* Callback for walk_gimple_stmt, rewrite all references to VAR
  762. and PARM_DECLs that belong to outer functions.
  763. The rewrite will involve some number of structure accesses back up
  764. the static chain. E.g. for a variable FOO up one nesting level it'll
  765. be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
  766. indirections apply to decls for which use_pointer_in_frame is true. */
  767. static tree
  768. convert_nonlocal_reference_op (tree *tp, int *walk_subtrees, void *data)
  769. {
  770. struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
  771. struct nesting_info *const info = (struct nesting_info *) wi->info;
  772. tree t = *tp;
  773. *walk_subtrees = 0;
  774. switch (TREE_CODE (t))
  775. {
  776. case VAR_DECL:
  777. /* Non-automatic variables are never processed. */
  778. if (TREE_STATIC (t) || DECL_EXTERNAL (t))
  779. break;
  780. /* FALLTHRU */
  781. case PARM_DECL:
  782. if (decl_function_context (t) != info->context)
  783. {
  784. tree x;
  785. wi->changed = true;
  786. x = get_nonlocal_debug_decl (info, t);
  787. if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
  788. {
  789. tree target_context = decl_function_context (t);
  790. struct nesting_info *i;
  791. for (i = info->outer; i->context != target_context; i = i->outer)
  792. continue;
  793. x = lookup_field_for_decl (i, t, INSERT);
  794. x = get_frame_field (info, target_context, x, &wi->gsi);
  795. if (use_pointer_in_frame (t))
  796. {
  797. x = init_tmp_var (info, x, &wi->gsi);
  798. x = build_simple_mem_ref (x);
  799. }
  800. }
  801. if (wi->val_only)
  802. {
  803. if (wi->is_lhs)
  804. x = save_tmp_var (info, x, &wi->gsi);
  805. else
  806. x = init_tmp_var (info, x, &wi->gsi);
  807. }
  808. *tp = x;
  809. }
  810. break;
  811. case LABEL_DECL:
  812. /* We're taking the address of a label from a parent function, but
  813. this is not itself a non-local goto. Mark the label such that it
  814. will not be deleted, much as we would with a label address in
  815. static storage. */
  816. if (decl_function_context (t) != info->context)
  817. FORCED_LABEL (t) = 1;
  818. break;
  819. case ADDR_EXPR:
  820. {
  821. bool save_val_only = wi->val_only;
  822. wi->val_only = false;
  823. wi->is_lhs = false;
  824. wi->changed = false;
  825. walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference_op, wi, 0);
  826. wi->val_only = true;
  827. if (wi->changed)
  828. {
  829. tree save_context;
  830. /* If we changed anything, we might no longer be directly
  831. referencing a decl. */
  832. save_context = current_function_decl;
  833. current_function_decl = info->context;
  834. recompute_tree_invariant_for_addr_expr (t);
  835. current_function_decl = save_context;
  836. /* If the callback converted the address argument in a context
  837. where we only accept variables (and min_invariant, presumably),
  838. then compute the address into a temporary. */
  839. if (save_val_only)
  840. *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
  841. t, &wi->gsi);
  842. }
  843. }
  844. break;
  845. case REALPART_EXPR:
  846. case IMAGPART_EXPR:
  847. case COMPONENT_REF:
  848. case ARRAY_REF:
  849. case ARRAY_RANGE_REF:
  850. case BIT_FIELD_REF:
  851. /* Go down this entire nest and just look at the final prefix and
  852. anything that describes the references. Otherwise, we lose track
  853. of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
  854. wi->val_only = true;
  855. wi->is_lhs = false;
  856. for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
  857. {
  858. if (TREE_CODE (t) == COMPONENT_REF)
  859. walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op, wi,
  860. NULL);
  861. else if (TREE_CODE (t) == ARRAY_REF
  862. || TREE_CODE (t) == ARRAY_RANGE_REF)
  863. {
  864. walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference_op,
  865. wi, NULL);
  866. walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op,
  867. wi, NULL);
  868. walk_tree (&TREE_OPERAND (t, 3), convert_nonlocal_reference_op,
  869. wi, NULL);
  870. }
  871. }
  872. wi->val_only = false;
  873. walk_tree (tp, convert_nonlocal_reference_op, wi, NULL);
  874. break;
  875. case VIEW_CONVERT_EXPR:
  876. /* Just request to look at the subtrees, leaving val_only and lhs
  877. untouched. This might actually be for !val_only + lhs, in which
  878. case we don't want to force a replacement by a temporary. */
  879. *walk_subtrees = 1;
  880. break;
  881. default:
  882. if (!IS_TYPE_OR_DECL_P (t))
  883. {
  884. *walk_subtrees = 1;
  885. wi->val_only = true;
  886. wi->is_lhs = false;
  887. }
  888. break;
  889. }
  890. return NULL_TREE;
  891. }
  892. static tree convert_nonlocal_reference_stmt (gimple_stmt_iterator *, bool *,
  893. struct walk_stmt_info *);
  894. /* Helper for convert_nonlocal_references, rewrite all references to VAR
  895. and PARM_DECLs that belong to outer functions. */
  896. static bool
  897. convert_nonlocal_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
  898. {
  899. struct nesting_info *const info = (struct nesting_info *) wi->info;
  900. bool need_chain = false, need_stmts = false;
  901. tree clause, decl;
  902. int dummy;
  903. bitmap new_suppress;
  904. new_suppress = BITMAP_GGC_ALLOC ();
  905. bitmap_copy (new_suppress, info->suppress_expansion);
  906. for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
  907. {
  908. switch (OMP_CLAUSE_CODE (clause))
  909. {
  910. case OMP_CLAUSE_REDUCTION:
  911. if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
  912. need_stmts = true;
  913. goto do_decl_clause;
  914. case OMP_CLAUSE_LASTPRIVATE:
  915. if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
  916. need_stmts = true;
  917. goto do_decl_clause;
  918. case OMP_CLAUSE_LINEAR:
  919. if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
  920. need_stmts = true;
  921. wi->val_only = true;
  922. wi->is_lhs = false;
  923. convert_nonlocal_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause),
  924. &dummy, wi);
  925. goto do_decl_clause;
  926. case OMP_CLAUSE_PRIVATE:
  927. case OMP_CLAUSE_FIRSTPRIVATE:
  928. case OMP_CLAUSE_COPYPRIVATE:
  929. case OMP_CLAUSE_SHARED:
  930. do_decl_clause:
  931. decl = OMP_CLAUSE_DECL (clause);
  932. if (TREE_CODE (decl) == VAR_DECL
  933. && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
  934. break;
  935. if (decl_function_context (decl) != info->context)
  936. {
  937. bitmap_set_bit (new_suppress, DECL_UID (decl));
  938. OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
  939. if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_PRIVATE)
  940. need_chain = true;
  941. }
  942. break;
  943. case OMP_CLAUSE_SCHEDULE:
  944. if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
  945. break;
  946. /* FALLTHRU */
  947. case OMP_CLAUSE_FINAL:
  948. case OMP_CLAUSE_IF:
  949. case OMP_CLAUSE_NUM_THREADS:
  950. case OMP_CLAUSE_DEPEND:
  951. case OMP_CLAUSE_DEVICE:
  952. case OMP_CLAUSE_NUM_TEAMS:
  953. case OMP_CLAUSE_THREAD_LIMIT:
  954. case OMP_CLAUSE_SAFELEN:
  955. case OMP_CLAUSE__CILK_FOR_COUNT_:
  956. wi->val_only = true;
  957. wi->is_lhs = false;
  958. convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
  959. &dummy, wi);
  960. break;
  961. case OMP_CLAUSE_DIST_SCHEDULE:
  962. if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
  963. {
  964. wi->val_only = true;
  965. wi->is_lhs = false;
  966. convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
  967. &dummy, wi);
  968. }
  969. break;
  970. case OMP_CLAUSE_MAP:
  971. case OMP_CLAUSE_TO:
  972. case OMP_CLAUSE_FROM:
  973. if (OMP_CLAUSE_SIZE (clause))
  974. {
  975. wi->val_only = true;
  976. wi->is_lhs = false;
  977. convert_nonlocal_reference_op (&OMP_CLAUSE_SIZE (clause),
  978. &dummy, wi);
  979. }
  980. if (DECL_P (OMP_CLAUSE_DECL (clause)))
  981. goto do_decl_clause;
  982. wi->val_only = true;
  983. wi->is_lhs = false;
  984. walk_tree (&OMP_CLAUSE_DECL (clause), convert_nonlocal_reference_op,
  985. wi, NULL);
  986. break;
  987. case OMP_CLAUSE_ALIGNED:
  988. if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
  989. {
  990. wi->val_only = true;
  991. wi->is_lhs = false;
  992. convert_nonlocal_reference_op
  993. (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
  994. }
  995. /* Like do_decl_clause, but don't add any suppression. */
  996. decl = OMP_CLAUSE_DECL (clause);
  997. if (TREE_CODE (decl) == VAR_DECL
  998. && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
  999. break;
  1000. if (decl_function_context (decl) != info->context)
  1001. {
  1002. OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
  1003. if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_PRIVATE)
  1004. need_chain = true;
  1005. }
  1006. break;
  1007. case OMP_CLAUSE_NOWAIT:
  1008. case OMP_CLAUSE_ORDERED:
  1009. case OMP_CLAUSE_DEFAULT:
  1010. case OMP_CLAUSE_COPYIN:
  1011. case OMP_CLAUSE_COLLAPSE:
  1012. case OMP_CLAUSE_UNTIED:
  1013. case OMP_CLAUSE_MERGEABLE:
  1014. case OMP_CLAUSE_PROC_BIND:
  1015. break;
  1016. default:
  1017. gcc_unreachable ();
  1018. }
  1019. }
  1020. info->suppress_expansion = new_suppress;
  1021. if (need_stmts)
  1022. for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
  1023. switch (OMP_CLAUSE_CODE (clause))
  1024. {
  1025. case OMP_CLAUSE_REDUCTION:
  1026. if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
  1027. {
  1028. tree old_context
  1029. = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
  1030. DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
  1031. = info->context;
  1032. walk_body (convert_nonlocal_reference_stmt,
  1033. convert_nonlocal_reference_op, info,
  1034. &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
  1035. walk_body (convert_nonlocal_reference_stmt,
  1036. convert_nonlocal_reference_op, info,
  1037. &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
  1038. DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
  1039. = old_context;
  1040. }
  1041. break;
  1042. case OMP_CLAUSE_LASTPRIVATE:
  1043. walk_body (convert_nonlocal_reference_stmt,
  1044. convert_nonlocal_reference_op, info,
  1045. &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
  1046. break;
  1047. case OMP_CLAUSE_LINEAR:
  1048. walk_body (convert_nonlocal_reference_stmt,
  1049. convert_nonlocal_reference_op, info,
  1050. &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
  1051. break;
  1052. default:
  1053. break;
  1054. }
  1055. return need_chain;
  1056. }
  1057. /* Create nonlocal debug decls for nonlocal VLA array bounds. */
  1058. static void
  1059. note_nonlocal_vla_type (struct nesting_info *info, tree type)
  1060. {
  1061. while (POINTER_TYPE_P (type) && !TYPE_NAME (type))
  1062. type = TREE_TYPE (type);
  1063. if (TYPE_NAME (type)
  1064. && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
  1065. && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
  1066. type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
  1067. while (POINTER_TYPE_P (type)
  1068. || TREE_CODE (type) == VECTOR_TYPE
  1069. || TREE_CODE (type) == FUNCTION_TYPE
  1070. || TREE_CODE (type) == METHOD_TYPE)
  1071. type = TREE_TYPE (type);
  1072. if (TREE_CODE (type) == ARRAY_TYPE)
  1073. {
  1074. tree domain, t;
  1075. note_nonlocal_vla_type (info, TREE_TYPE (type));
  1076. domain = TYPE_DOMAIN (type);
  1077. if (domain)
  1078. {
  1079. t = TYPE_MIN_VALUE (domain);
  1080. if (t && (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
  1081. && decl_function_context (t) != info->context)
  1082. get_nonlocal_debug_decl (info, t);
  1083. t = TYPE_MAX_VALUE (domain);
  1084. if (t && (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
  1085. && decl_function_context (t) != info->context)
  1086. get_nonlocal_debug_decl (info, t);
  1087. }
  1088. }
  1089. }
  1090. /* Create nonlocal debug decls for nonlocal VLA array bounds for VLAs
  1091. in BLOCK. */
  1092. static void
  1093. note_nonlocal_block_vlas (struct nesting_info *info, tree block)
  1094. {
  1095. tree var;
  1096. for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
  1097. if (TREE_CODE (var) == VAR_DECL
  1098. && variably_modified_type_p (TREE_TYPE (var), NULL)
  1099. && DECL_HAS_VALUE_EXPR_P (var)
  1100. && decl_function_context (var) != info->context)
  1101. note_nonlocal_vla_type (info, TREE_TYPE (var));
  1102. }
  1103. /* Callback for walk_gimple_stmt. Rewrite all references to VAR and
  1104. PARM_DECLs that belong to outer functions. This handles statements
  1105. that are not handled via the standard recursion done in
  1106. walk_gimple_stmt. STMT is the statement to examine, DATA is as in
  1107. convert_nonlocal_reference_op. Set *HANDLED_OPS_P to true if all the
  1108. operands of STMT have been handled by this function. */
  1109. static tree
  1110. convert_nonlocal_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
  1111. struct walk_stmt_info *wi)
  1112. {
  1113. struct nesting_info *info = (struct nesting_info *) wi->info;
  1114. tree save_local_var_chain;
  1115. bitmap save_suppress;
  1116. gimple stmt = gsi_stmt (*gsi);
  1117. switch (gimple_code (stmt))
  1118. {
  1119. case GIMPLE_GOTO:
  1120. /* Don't walk non-local gotos for now. */
  1121. if (TREE_CODE (gimple_goto_dest (stmt)) != LABEL_DECL)
  1122. {
  1123. wi->val_only = true;
  1124. wi->is_lhs = false;
  1125. *handled_ops_p = true;
  1126. return NULL_TREE;
  1127. }
  1128. break;
  1129. case GIMPLE_OMP_PARALLEL:
  1130. case GIMPLE_OMP_TASK:
  1131. save_suppress = info->suppress_expansion;
  1132. if (convert_nonlocal_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
  1133. wi))
  1134. {
  1135. tree c, decl;
  1136. decl = get_chain_decl (info);
  1137. c = build_omp_clause (gimple_location (stmt),
  1138. OMP_CLAUSE_FIRSTPRIVATE);
  1139. OMP_CLAUSE_DECL (c) = decl;
  1140. OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
  1141. gimple_omp_taskreg_set_clauses (stmt, c);
  1142. }
  1143. save_local_var_chain = info->new_local_var_chain;
  1144. info->new_local_var_chain = NULL;
  1145. walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
  1146. info, gimple_omp_body_ptr (stmt));
  1147. if (info->new_local_var_chain)
  1148. declare_vars (info->new_local_var_chain,
  1149. gimple_seq_first_stmt (gimple_omp_body (stmt)),
  1150. false);
  1151. info->new_local_var_chain = save_local_var_chain;
  1152. info->suppress_expansion = save_suppress;
  1153. break;
  1154. case GIMPLE_OMP_FOR:
  1155. save_suppress = info->suppress_expansion;
  1156. convert_nonlocal_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
  1157. walk_gimple_omp_for (as_a <gomp_for *> (stmt),
  1158. convert_nonlocal_reference_stmt,
  1159. convert_nonlocal_reference_op, info);
  1160. walk_body (convert_nonlocal_reference_stmt,
  1161. convert_nonlocal_reference_op, info, gimple_omp_body_ptr (stmt));
  1162. info->suppress_expansion = save_suppress;
  1163. break;
  1164. case GIMPLE_OMP_SECTIONS:
  1165. save_suppress = info->suppress_expansion;
  1166. convert_nonlocal_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
  1167. walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
  1168. info, gimple_omp_body_ptr (stmt));
  1169. info->suppress_expansion = save_suppress;
  1170. break;
  1171. case GIMPLE_OMP_SINGLE:
  1172. save_suppress = info->suppress_expansion;
  1173. convert_nonlocal_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
  1174. walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
  1175. info, gimple_omp_body_ptr (stmt));
  1176. info->suppress_expansion = save_suppress;
  1177. break;
  1178. case GIMPLE_OMP_TARGET:
  1179. if (!is_gimple_omp_offloaded (stmt))
  1180. {
  1181. save_suppress = info->suppress_expansion;
  1182. convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
  1183. wi);
  1184. info->suppress_expansion = save_suppress;
  1185. walk_body (convert_nonlocal_reference_stmt,
  1186. convert_nonlocal_reference_op, info,
  1187. gimple_omp_body_ptr (stmt));
  1188. break;
  1189. }
  1190. save_suppress = info->suppress_expansion;
  1191. if (convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
  1192. wi))
  1193. {
  1194. tree c, decl;
  1195. decl = get_chain_decl (info);
  1196. c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
  1197. OMP_CLAUSE_DECL (c) = decl;
  1198. OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TO);
  1199. OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
  1200. OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
  1201. gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
  1202. }
  1203. save_local_var_chain = info->new_local_var_chain;
  1204. info->new_local_var_chain = NULL;
  1205. walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
  1206. info, gimple_omp_body_ptr (stmt));
  1207. if (info->new_local_var_chain)
  1208. declare_vars (info->new_local_var_chain,
  1209. gimple_seq_first_stmt (gimple_omp_body (stmt)),
  1210. false);
  1211. info->new_local_var_chain = save_local_var_chain;
  1212. info->suppress_expansion = save_suppress;
  1213. break;
  1214. case GIMPLE_OMP_TEAMS:
  1215. save_suppress = info->suppress_expansion;
  1216. convert_nonlocal_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
  1217. walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
  1218. info, gimple_omp_body_ptr (stmt));
  1219. info->suppress_expansion = save_suppress;
  1220. break;
  1221. case GIMPLE_OMP_SECTION:
  1222. case GIMPLE_OMP_MASTER:
  1223. case GIMPLE_OMP_TASKGROUP:
  1224. case GIMPLE_OMP_ORDERED:
  1225. walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
  1226. info, gimple_omp_body_ptr (stmt));
  1227. break;
  1228. case GIMPLE_BIND:
  1229. {
  1230. gbind *bind_stmt = as_a <gbind *> (stmt);
  1231. if (!optimize && gimple_bind_block (bind_stmt))
  1232. note_nonlocal_block_vlas (info, gimple_bind_block (bind_stmt));
  1233. for (tree var = gimple_bind_vars (bind_stmt); var; var = DECL_CHAIN (var))
  1234. if (TREE_CODE (var) == NAMELIST_DECL)
  1235. {
  1236. /* Adjust decls mentioned in NAMELIST_DECL. */
  1237. tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
  1238. tree decl;
  1239. unsigned int i;
  1240. FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
  1241. {
  1242. if (TREE_CODE (decl) == VAR_DECL
  1243. && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
  1244. continue;
  1245. if (decl_function_context (decl) != info->context)
  1246. CONSTRUCTOR_ELT (decls, i)->value
  1247. = get_nonlocal_debug_decl (info, decl);
  1248. }
  1249. }
  1250. *handled_ops_p = false;
  1251. return NULL_TREE;
  1252. }
  1253. case GIMPLE_COND:
  1254. wi->val_only = true;
  1255. wi->is_lhs = false;
  1256. *handled_ops_p = false;
  1257. return NULL_TREE;
  1258. default:
  1259. /* For every other statement that we are not interested in
  1260. handling here, let the walker traverse the operands. */
  1261. *handled_ops_p = false;
  1262. return NULL_TREE;
  1263. }
  1264. /* We have handled all of STMT operands, no need to traverse the operands. */
  1265. *handled_ops_p = true;
  1266. return NULL_TREE;
  1267. }
  1268. /* A subroutine of convert_local_reference. Create a local variable
  1269. in the parent function with DECL_VALUE_EXPR set to reference the
  1270. field in FRAME. This is used both for debug info and in OMP
  1271. lowering. */
  1272. static tree
  1273. get_local_debug_decl (struct nesting_info *info, tree decl, tree field)
  1274. {
  1275. tree x, new_decl;
  1276. tree *slot = &info->var_map->get_or_insert (decl);
  1277. if (*slot)
  1278. return *slot;
  1279. /* Make sure frame_decl gets created. */
  1280. (void) get_frame_type (info);
  1281. x = info->frame_decl;
  1282. x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
  1283. new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
  1284. VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
  1285. DECL_CONTEXT (new_decl) = info->context;
  1286. DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
  1287. DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
  1288. TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
  1289. TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
  1290. TREE_READONLY (new_decl) = TREE_READONLY (decl);
  1291. TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
  1292. DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
  1293. if ((TREE_CODE (decl) == PARM_DECL
  1294. || TREE_CODE (decl) == RESULT_DECL
  1295. || TREE_CODE (decl) == VAR_DECL)
  1296. && DECL_BY_REFERENCE (decl))
  1297. DECL_BY_REFERENCE (new_decl) = 1;
  1298. SET_DECL_VALUE_EXPR (new_decl, x);
  1299. DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
  1300. *slot = new_decl;
  1301. DECL_CHAIN (new_decl) = info->debug_var_chain;
  1302. info->debug_var_chain = new_decl;
  1303. /* Do not emit debug info twice. */
  1304. DECL_IGNORED_P (decl) = 1;
  1305. return new_decl;
  1306. }
  1307. /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
  1308. and PARM_DECLs that were referenced by inner nested functions.
  1309. The rewrite will be a structure reference to the local frame variable. */
  1310. static bool convert_local_omp_clauses (tree *, struct walk_stmt_info *);
  1311. static tree
  1312. convert_local_reference_op (tree *tp, int *walk_subtrees, void *data)
  1313. {
  1314. struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
  1315. struct nesting_info *const info = (struct nesting_info *) wi->info;
  1316. tree t = *tp, field, x;
  1317. bool save_val_only;
  1318. *walk_subtrees = 0;
  1319. switch (TREE_CODE (t))
  1320. {
  1321. case VAR_DECL:
  1322. /* Non-automatic variables are never processed. */
  1323. if (TREE_STATIC (t) || DECL_EXTERNAL (t))
  1324. break;
  1325. /* FALLTHRU */
  1326. case PARM_DECL:
  1327. if (decl_function_context (t) == info->context)
  1328. {
  1329. /* If we copied a pointer to the frame, then the original decl
  1330. is used unchanged in the parent function. */
  1331. if (use_pointer_in_frame (t))
  1332. break;
  1333. /* No need to transform anything if no child references the
  1334. variable. */
  1335. field = lookup_field_for_decl (info, t, NO_INSERT);
  1336. if (!field)
  1337. break;
  1338. wi->changed = true;
  1339. x = get_local_debug_decl (info, t, field);
  1340. if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
  1341. x = get_frame_field (info, info->context, field, &wi->gsi);
  1342. if (wi->val_only)
  1343. {
  1344. if (wi->is_lhs)
  1345. x = save_tmp_var (info, x, &wi->gsi);
  1346. else
  1347. x = init_tmp_var (info, x, &wi->gsi);
  1348. }
  1349. *tp = x;
  1350. }
  1351. break;
  1352. case ADDR_EXPR:
  1353. save_val_only = wi->val_only;
  1354. wi->val_only = false;
  1355. wi->is_lhs = false;
  1356. wi->changed = false;
  1357. walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op, wi, NULL);
  1358. wi->val_only = save_val_only;
  1359. /* If we converted anything ... */
  1360. if (wi->changed)
  1361. {
  1362. tree save_context;
  1363. /* Then the frame decl is now addressable. */
  1364. TREE_ADDRESSABLE (info->frame_decl) = 1;
  1365. save_context = current_function_decl;
  1366. current_function_decl = info->context;
  1367. recompute_tree_invariant_for_addr_expr (t);
  1368. current_function_decl = save_context;
  1369. /* If we are in a context where we only accept values, then
  1370. compute the address into a temporary. */
  1371. if (save_val_only)
  1372. *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
  1373. t, &wi->gsi);
  1374. }
  1375. break;
  1376. case REALPART_EXPR:
  1377. case IMAGPART_EXPR:
  1378. case COMPONENT_REF:
  1379. case ARRAY_REF:
  1380. case ARRAY_RANGE_REF:
  1381. case BIT_FIELD_REF:
  1382. /* Go down this entire nest and just look at the final prefix and
  1383. anything that describes the references. Otherwise, we lose track
  1384. of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
  1385. save_val_only = wi->val_only;
  1386. wi->val_only = true;
  1387. wi->is_lhs = false;
  1388. for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
  1389. {
  1390. if (TREE_CODE (t) == COMPONENT_REF)
  1391. walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
  1392. NULL);
  1393. else if (TREE_CODE (t) == ARRAY_REF
  1394. || TREE_CODE (t) == ARRAY_RANGE_REF)
  1395. {
  1396. walk_tree (&TREE_OPERAND (t, 1), convert_local_reference_op, wi,
  1397. NULL);
  1398. walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
  1399. NULL);
  1400. walk_tree (&TREE_OPERAND (t, 3), convert_local_reference_op, wi,
  1401. NULL);
  1402. }
  1403. }
  1404. wi->val_only = false;
  1405. walk_tree (tp, convert_local_reference_op, wi, NULL);
  1406. wi->val_only = save_val_only;
  1407. break;
  1408. case MEM_REF:
  1409. save_val_only = wi->val_only;
  1410. wi->val_only = true;
  1411. wi->is_lhs = false;
  1412. walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op,
  1413. wi, NULL);
  1414. /* We need to re-fold the MEM_REF as component references as
  1415. part of a ADDR_EXPR address are not allowed. But we cannot
  1416. fold here, as the chain record type is not yet finalized. */
  1417. if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
  1418. && !DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
  1419. info->mem_refs->add (tp);
  1420. wi->val_only = save_val_only;
  1421. break;
  1422. case VIEW_CONVERT_EXPR:
  1423. /* Just request to look at the subtrees, leaving val_only and lhs
  1424. untouched. This might actually be for !val_only + lhs, in which
  1425. case we don't want to force a replacement by a temporary. */
  1426. *walk_subtrees = 1;
  1427. break;
  1428. default:
  1429. if (!IS_TYPE_OR_DECL_P (t))
  1430. {
  1431. *walk_subtrees = 1;
  1432. wi->val_only = true;
  1433. wi->is_lhs = false;
  1434. }
  1435. break;
  1436. }
  1437. return NULL_TREE;
  1438. }
  1439. static tree convert_local_reference_stmt (gimple_stmt_iterator *, bool *,
  1440. struct walk_stmt_info *);
  1441. /* Helper for convert_local_reference. Convert all the references in
  1442. the chain of clauses at *PCLAUSES. WI is as in convert_local_reference. */
  1443. static bool
  1444. convert_local_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
  1445. {
  1446. struct nesting_info *const info = (struct nesting_info *) wi->info;
  1447. bool need_frame = false, need_stmts = false;
  1448. tree clause, decl;
  1449. int dummy;
  1450. bitmap new_suppress;
  1451. new_suppress = BITMAP_GGC_ALLOC ();
  1452. bitmap_copy (new_suppress, info->suppress_expansion);
  1453. for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
  1454. {
  1455. switch (OMP_CLAUSE_CODE (clause))
  1456. {
  1457. case OMP_CLAUSE_REDUCTION:
  1458. if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
  1459. need_stmts = true;
  1460. goto do_decl_clause;
  1461. case OMP_CLAUSE_LASTPRIVATE:
  1462. if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
  1463. need_stmts = true;
  1464. goto do_decl_clause;
  1465. case OMP_CLAUSE_LINEAR:
  1466. if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
  1467. need_stmts = true;
  1468. wi->val_only = true;
  1469. wi->is_lhs = false;
  1470. convert_local_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause), &dummy,
  1471. wi);
  1472. goto do_decl_clause;
  1473. case OMP_CLAUSE_PRIVATE:
  1474. case OMP_CLAUSE_FIRSTPRIVATE:
  1475. case OMP_CLAUSE_COPYPRIVATE:
  1476. case OMP_CLAUSE_SHARED:
  1477. do_decl_clause:
  1478. decl = OMP_CLAUSE_DECL (clause);
  1479. if (TREE_CODE (decl) == VAR_DECL
  1480. && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
  1481. break;
  1482. if (decl_function_context (decl) == info->context
  1483. && !use_pointer_in_frame (decl))
  1484. {
  1485. tree field = lookup_field_for_decl (info, decl, NO_INSERT);
  1486. if (field)
  1487. {
  1488. bitmap_set_bit (new_suppress, DECL_UID (decl));
  1489. OMP_CLAUSE_DECL (clause)
  1490. = get_local_debug_decl (info, decl, field);
  1491. need_frame = true;
  1492. }
  1493. }
  1494. break;
  1495. case OMP_CLAUSE_SCHEDULE:
  1496. if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
  1497. break;
  1498. /* FALLTHRU */
  1499. case OMP_CLAUSE_FINAL:
  1500. case OMP_CLAUSE_IF:
  1501. case OMP_CLAUSE_NUM_THREADS:
  1502. case OMP_CLAUSE_DEPEND:
  1503. case OMP_CLAUSE_DEVICE:
  1504. case OMP_CLAUSE_NUM_TEAMS:
  1505. case OMP_CLAUSE_THREAD_LIMIT:
  1506. case OMP_CLAUSE_SAFELEN:
  1507. case OMP_CLAUSE__CILK_FOR_COUNT_:
  1508. wi->val_only = true;
  1509. wi->is_lhs = false;
  1510. convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0), &dummy,
  1511. wi);
  1512. break;
  1513. case OMP_CLAUSE_DIST_SCHEDULE:
  1514. if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
  1515. {
  1516. wi->val_only = true;
  1517. wi->is_lhs = false;
  1518. convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
  1519. &dummy, wi);
  1520. }
  1521. break;
  1522. case OMP_CLAUSE_MAP:
  1523. case OMP_CLAUSE_TO:
  1524. case OMP_CLAUSE_FROM:
  1525. if (OMP_CLAUSE_SIZE (clause))
  1526. {
  1527. wi->val_only = true;
  1528. wi->is_lhs = false;
  1529. convert_local_reference_op (&OMP_CLAUSE_SIZE (clause),
  1530. &dummy, wi);
  1531. }
  1532. if (DECL_P (OMP_CLAUSE_DECL (clause)))
  1533. goto do_decl_clause;
  1534. wi->val_only = true;
  1535. wi->is_lhs = false;
  1536. walk_tree (&OMP_CLAUSE_DECL (clause), convert_local_reference_op,
  1537. wi, NULL);
  1538. break;
  1539. case OMP_CLAUSE_ALIGNED:
  1540. if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
  1541. {
  1542. wi->val_only = true;
  1543. wi->is_lhs = false;
  1544. convert_local_reference_op
  1545. (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
  1546. }
  1547. /* Like do_decl_clause, but don't add any suppression. */
  1548. decl = OMP_CLAUSE_DECL (clause);
  1549. if (TREE_CODE (decl) == VAR_DECL
  1550. && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
  1551. break;
  1552. if (decl_function_context (decl) == info->context
  1553. && !use_pointer_in_frame (decl))
  1554. {
  1555. tree field = lookup_field_for_decl (info, decl, NO_INSERT);
  1556. if (field)
  1557. {
  1558. OMP_CLAUSE_DECL (clause)
  1559. = get_local_debug_decl (info, decl, field);
  1560. need_frame = true;
  1561. }
  1562. }
  1563. break;
  1564. case OMP_CLAUSE_NOWAIT:
  1565. case OMP_CLAUSE_ORDERED:
  1566. case OMP_CLAUSE_DEFAULT:
  1567. case OMP_CLAUSE_COPYIN:
  1568. case OMP_CLAUSE_COLLAPSE:
  1569. case OMP_CLAUSE_UNTIED:
  1570. case OMP_CLAUSE_MERGEABLE:
  1571. case OMP_CLAUSE_PROC_BIND:
  1572. break;
  1573. default:
  1574. gcc_unreachable ();
  1575. }
  1576. }
  1577. info->suppress_expansion = new_suppress;
  1578. if (need_stmts)
  1579. for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
  1580. switch (OMP_CLAUSE_CODE (clause))
  1581. {
  1582. case OMP_CLAUSE_REDUCTION:
  1583. if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
  1584. {
  1585. tree old_context
  1586. = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
  1587. DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
  1588. = info->context;
  1589. walk_body (convert_local_reference_stmt,
  1590. convert_local_reference_op, info,
  1591. &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
  1592. walk_body (convert_local_reference_stmt,
  1593. convert_local_reference_op, info,
  1594. &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
  1595. DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
  1596. = old_context;
  1597. }
  1598. break;
  1599. case OMP_CLAUSE_LASTPRIVATE:
  1600. walk_body (convert_local_reference_stmt,
  1601. convert_local_reference_op, info,
  1602. &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
  1603. break;
  1604. case OMP_CLAUSE_LINEAR:
  1605. walk_body (convert_local_reference_stmt,
  1606. convert_local_reference_op, info,
  1607. &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
  1608. break;
  1609. default:
  1610. break;
  1611. }
  1612. return need_frame;
  1613. }
  1614. /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
  1615. and PARM_DECLs that were referenced by inner nested functions.
  1616. The rewrite will be a structure reference to the local frame variable. */
  1617. static tree
  1618. convert_local_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
  1619. struct walk_stmt_info *wi)
  1620. {
  1621. struct nesting_info *info = (struct nesting_info *) wi->info;
  1622. tree save_local_var_chain;
  1623. bitmap save_suppress;
  1624. gimple stmt = gsi_stmt (*gsi);
  1625. switch (gimple_code (stmt))
  1626. {
  1627. case GIMPLE_OMP_PARALLEL:
  1628. case GIMPLE_OMP_TASK:
  1629. save_suppress = info->suppress_expansion;
  1630. if (convert_local_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
  1631. wi))
  1632. {
  1633. tree c;
  1634. (void) get_frame_type (info);
  1635. c = build_omp_clause (gimple_location (stmt),
  1636. OMP_CLAUSE_SHARED);
  1637. OMP_CLAUSE_DECL (c) = info->frame_decl;
  1638. OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
  1639. gimple_omp_taskreg_set_clauses (stmt, c);
  1640. }
  1641. save_local_var_chain = info->new_local_var_chain;
  1642. info->new_local_var_chain = NULL;
  1643. walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
  1644. gimple_omp_body_ptr (stmt));
  1645. if (info->new_local_var_chain)
  1646. declare_vars (info->new_local_var_chain,
  1647. gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
  1648. info->new_local_var_chain = save_local_var_chain;
  1649. info->suppress_expansion = save_suppress;
  1650. break;
  1651. case GIMPLE_OMP_FOR:
  1652. save_suppress = info->suppress_expansion;
  1653. convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
  1654. walk_gimple_omp_for (as_a <gomp_for *> (stmt),
  1655. convert_local_reference_stmt,
  1656. convert_local_reference_op, info);
  1657. walk_body (convert_local_reference_stmt, convert_local_reference_op,
  1658. info, gimple_omp_body_ptr (stmt));
  1659. info->suppress_expansion = save_suppress;
  1660. break;
  1661. case GIMPLE_OMP_SECTIONS:
  1662. save_suppress = info->suppress_expansion;
  1663. convert_local_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
  1664. walk_body (convert_local_reference_stmt, convert_local_reference_op,
  1665. info, gimple_omp_body_ptr (stmt));
  1666. info->suppress_expansion = save_suppress;
  1667. break;
  1668. case GIMPLE_OMP_SINGLE:
  1669. save_suppress = info->suppress_expansion;
  1670. convert_local_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
  1671. walk_body (convert_local_reference_stmt, convert_local_reference_op,
  1672. info, gimple_omp_body_ptr (stmt));
  1673. info->suppress_expansion = save_suppress;
  1674. break;
  1675. case GIMPLE_OMP_TARGET:
  1676. if (!is_gimple_omp_offloaded (stmt))
  1677. {
  1678. save_suppress = info->suppress_expansion;
  1679. convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi);
  1680. info->suppress_expansion = save_suppress;
  1681. walk_body (convert_local_reference_stmt, convert_local_reference_op,
  1682. info, gimple_omp_body_ptr (stmt));
  1683. break;
  1684. }
  1685. save_suppress = info->suppress_expansion;
  1686. if (convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi))
  1687. {
  1688. tree c;
  1689. (void) get_frame_type (info);
  1690. c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
  1691. OMP_CLAUSE_DECL (c) = info->frame_decl;
  1692. OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
  1693. OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (info->frame_decl);
  1694. OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
  1695. gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
  1696. }
  1697. save_local_var_chain = info->new_local_var_chain;
  1698. info->new_local_var_chain = NULL;
  1699. walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
  1700. gimple_omp_body_ptr (stmt));
  1701. if (info->new_local_var_chain)
  1702. declare_vars (info->new_local_var_chain,
  1703. gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
  1704. info->new_local_var_chain = save_local_var_chain;
  1705. info->suppress_expansion = save_suppress;
  1706. break;
  1707. case GIMPLE_OMP_TEAMS:
  1708. save_suppress = info->suppress_expansion;
  1709. convert_local_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
  1710. walk_body (convert_local_reference_stmt, convert_local_reference_op,
  1711. info, gimple_omp_body_ptr (stmt));
  1712. info->suppress_expansion = save_suppress;
  1713. break;
  1714. case GIMPLE_OMP_SECTION:
  1715. case GIMPLE_OMP_MASTER:
  1716. case GIMPLE_OMP_TASKGROUP:
  1717. case GIMPLE_OMP_ORDERED:
  1718. walk_body (convert_local_reference_stmt, convert_local_reference_op,
  1719. info, gimple_omp_body_ptr (stmt));
  1720. break;
  1721. case GIMPLE_COND:
  1722. wi->val_only = true;
  1723. wi->is_lhs = false;
  1724. *handled_ops_p = false;
  1725. return NULL_TREE;
  1726. case GIMPLE_ASSIGN:
  1727. if (gimple_clobber_p (stmt))
  1728. {
  1729. tree lhs = gimple_assign_lhs (stmt);
  1730. if (!use_pointer_in_frame (lhs)
  1731. && lookup_field_for_decl (info, lhs, NO_INSERT))
  1732. {
  1733. gsi_replace (gsi, gimple_build_nop (), true);
  1734. break;
  1735. }
  1736. }
  1737. *handled_ops_p = false;
  1738. return NULL_TREE;
  1739. case GIMPLE_BIND:
  1740. for (tree var = gimple_bind_vars (as_a <gbind *> (stmt));
  1741. var;
  1742. var = DECL_CHAIN (var))
  1743. if (TREE_CODE (var) == NAMELIST_DECL)
  1744. {
  1745. /* Adjust decls mentioned in NAMELIST_DECL. */
  1746. tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
  1747. tree decl;
  1748. unsigned int i;
  1749. FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
  1750. {
  1751. if (TREE_CODE (decl) == VAR_DECL
  1752. && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
  1753. continue;
  1754. if (decl_function_context (decl) == info->context
  1755. && !use_pointer_in_frame (decl))
  1756. {
  1757. tree field = lookup_field_for_decl (info, decl, NO_INSERT);
  1758. if (field)
  1759. {
  1760. CONSTRUCTOR_ELT (decls, i)->value
  1761. = get_local_debug_decl (info, decl, field);
  1762. }
  1763. }
  1764. }
  1765. }
  1766. *handled_ops_p = false;
  1767. return NULL_TREE;
  1768. default:
  1769. /* For every other statement that we are not interested in
  1770. handling here, let the walker traverse the operands. */
  1771. *handled_ops_p = false;
  1772. return NULL_TREE;
  1773. }
  1774. /* Indicate that we have handled all the operands ourselves. */
  1775. *handled_ops_p = true;
  1776. return NULL_TREE;
  1777. }
  1778. /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_GOTOs
  1779. that reference labels from outer functions. The rewrite will be a
  1780. call to __builtin_nonlocal_goto. */
  1781. static tree
  1782. convert_nl_goto_reference (gimple_stmt_iterator *gsi, bool *handled_ops_p,
  1783. struct walk_stmt_info *wi)
  1784. {
  1785. struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
  1786. tree label, new_label, target_context, x, field;
  1787. gcall *call;
  1788. gimple stmt = gsi_stmt (*gsi);
  1789. if (gimple_code (stmt) != GIMPLE_GOTO)
  1790. {
  1791. *handled_ops_p = false;
  1792. return NULL_TREE;
  1793. }
  1794. label = gimple_goto_dest (stmt);
  1795. if (TREE_CODE (label) != LABEL_DECL)
  1796. {
  1797. *handled_ops_p = false;
  1798. return NULL_TREE;
  1799. }
  1800. target_context = decl_function_context (label);
  1801. if (target_context == info->context)
  1802. {
  1803. *handled_ops_p = false;
  1804. return NULL_TREE;
  1805. }
  1806. for (i = info->outer; target_context != i->context; i = i->outer)
  1807. continue;
  1808. /* The original user label may also be use for a normal goto, therefore
  1809. we must create a new label that will actually receive the abnormal
  1810. control transfer. This new label will be marked LABEL_NONLOCAL; this
  1811. mark will trigger proper behavior in the cfg, as well as cause the
  1812. (hairy target-specific) non-local goto receiver code to be generated
  1813. when we expand rtl. Enter this association into var_map so that we
  1814. can insert the new label into the IL during a second pass. */
  1815. tree *slot = &i->var_map->get_or_insert (label);
  1816. if (*slot == NULL)
  1817. {
  1818. new_label = create_artificial_label (UNKNOWN_LOCATION);
  1819. DECL_NONLOCAL (new_label) = 1;
  1820. *slot = new_label;
  1821. }
  1822. else
  1823. new_label = *slot;
  1824. /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
  1825. field = get_nl_goto_field (i);
  1826. x = get_frame_field (info, target_context, field, gsi);
  1827. x = build_addr (x, target_context);
  1828. x = gsi_gimplify_val (info, x, gsi);
  1829. call = gimple_build_call (builtin_decl_implicit (BUILT_IN_NONLOCAL_GOTO),
  1830. 2, build_addr (new_label, target_context), x);
  1831. gsi_replace (gsi, call, false);
  1832. /* We have handled all of STMT's operands, no need to keep going. */
  1833. *handled_ops_p = true;
  1834. return NULL_TREE;
  1835. }
  1836. /* Called via walk_function+walk_tree, rewrite all GIMPLE_LABELs whose labels
  1837. are referenced via nonlocal goto from a nested function. The rewrite
  1838. will involve installing a newly generated DECL_NONLOCAL label, and
  1839. (potentially) a branch around the rtl gunk that is assumed to be
  1840. attached to such a label. */
  1841. static tree
  1842. convert_nl_goto_receiver (gimple_stmt_iterator *gsi, bool *handled_ops_p,
  1843. struct walk_stmt_info *wi)
  1844. {
  1845. struct nesting_info *const info = (struct nesting_info *) wi->info;
  1846. tree label, new_label;
  1847. gimple_stmt_iterator tmp_gsi;
  1848. glabel *stmt = dyn_cast <glabel *> (gsi_stmt (*gsi));
  1849. if (!stmt)
  1850. {
  1851. *handled_ops_p = false;
  1852. return NULL_TREE;
  1853. }
  1854. label = gimple_label_label (stmt);
  1855. tree *slot = info->var_map->get (label);
  1856. if (!slot)
  1857. {
  1858. *handled_ops_p = false;
  1859. return NULL_TREE;
  1860. }
  1861. /* If there's any possibility that the previous statement falls through,
  1862. then we must branch around the new non-local label. */
  1863. tmp_gsi = wi->gsi;
  1864. gsi_prev (&tmp_gsi);
  1865. if (gsi_end_p (tmp_gsi) || gimple_stmt_may_fallthru (gsi_stmt (tmp_gsi)))
  1866. {
  1867. gimple stmt = gimple_build_goto (label);
  1868. gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
  1869. }
  1870. new_label = (tree) *slot;
  1871. stmt = gimple_build_label (new_label);
  1872. gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
  1873. *handled_ops_p = true;
  1874. return NULL_TREE;
  1875. }
  1876. /* Called via walk_function+walk_stmt, rewrite all references to addresses
  1877. of nested functions that require the use of trampolines. The rewrite
  1878. will involve a reference a trampoline generated for the occasion. */
  1879. static tree
  1880. convert_tramp_reference_op (tree *tp, int *walk_subtrees, void *data)
  1881. {
  1882. struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
  1883. struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
  1884. tree t = *tp, decl, target_context, x, builtin;
  1885. gcall *call;
  1886. *walk_subtrees = 0;
  1887. switch (TREE_CODE (t))
  1888. {
  1889. case ADDR_EXPR:
  1890. /* Build
  1891. T.1 = &CHAIN->tramp;
  1892. T.2 = __builtin_adjust_trampoline (T.1);
  1893. T.3 = (func_type)T.2;
  1894. */
  1895. decl = TREE_OPERAND (t, 0);
  1896. if (TREE_CODE (decl) != FUNCTION_DECL)
  1897. break;
  1898. /* Only need to process nested functions. */
  1899. target_context = decl_function_context (decl);
  1900. if (!target_context)
  1901. break;
  1902. /* If the nested function doesn't use a static chain, then
  1903. it doesn't need a trampoline. */
  1904. if (!DECL_STATIC_CHAIN (decl))
  1905. break;
  1906. /* If we don't want a trampoline, then don't build one. */
  1907. if (TREE_NO_TRAMPOLINE (t))
  1908. break;
  1909. /* Lookup the immediate parent of the callee, as that's where
  1910. we need to insert the trampoline. */
  1911. for (i = info; i->context != target_context; i = i->outer)
  1912. continue;
  1913. x = lookup_tramp_for_decl (i, decl, INSERT);
  1914. /* Compute the address of the field holding the trampoline. */
  1915. x = get_frame_field (info, target_context, x, &wi->gsi);
  1916. x = build_addr (x, target_context);
  1917. x = gsi_gimplify_val (info, x, &wi->gsi);
  1918. /* Do machine-specific ugliness. Normally this will involve
  1919. computing extra alignment, but it can really be anything. */
  1920. builtin = builtin_decl_implicit (BUILT_IN_ADJUST_TRAMPOLINE);
  1921. call = gimple_build_call (builtin, 1, x);
  1922. x = init_tmp_var_with_call (info, &wi->gsi, call);
  1923. /* Cast back to the proper function type. */
  1924. x = build1 (NOP_EXPR, TREE_TYPE (t), x);
  1925. x = init_tmp_var (info, x, &wi->gsi);
  1926. *tp = x;
  1927. break;
  1928. default:
  1929. if (!IS_TYPE_OR_DECL_P (t))
  1930. *walk_subtrees = 1;
  1931. break;
  1932. }
  1933. return NULL_TREE;
  1934. }
  1935. /* Called via walk_function+walk_gimple_stmt, rewrite all references
  1936. to addresses of nested functions that require the use of
  1937. trampolines. The rewrite will involve a reference a trampoline
  1938. generated for the occasion. */
  1939. static tree
  1940. convert_tramp_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
  1941. struct walk_stmt_info *wi)
  1942. {
  1943. struct nesting_info *info = (struct nesting_info *) wi->info;
  1944. gimple stmt = gsi_stmt (*gsi);
  1945. switch (gimple_code (stmt))
  1946. {
  1947. case GIMPLE_CALL:
  1948. {
  1949. /* Only walk call arguments, lest we generate trampolines for
  1950. direct calls. */
  1951. unsigned long i, nargs = gimple_call_num_args (stmt);
  1952. for (i = 0; i < nargs; i++)
  1953. walk_tree (gimple_call_arg_ptr (stmt, i), convert_tramp_reference_op,
  1954. wi, NULL);
  1955. break;
  1956. }
  1957. case GIMPLE_OMP_TARGET:
  1958. if (!is_gimple_omp_offloaded (stmt))
  1959. {
  1960. *handled_ops_p = false;
  1961. return NULL_TREE;
  1962. }
  1963. /* FALLTHRU */
  1964. case GIMPLE_OMP_PARALLEL:
  1965. case GIMPLE_OMP_TASK:
  1966. {
  1967. tree save_local_var_chain = info->new_local_var_chain;
  1968. walk_gimple_op (stmt, convert_tramp_reference_op, wi);
  1969. info->new_local_var_chain = NULL;
  1970. char save_static_chain_added = info->static_chain_added;
  1971. info->static_chain_added = 0;
  1972. walk_body (convert_tramp_reference_stmt, convert_tramp_reference_op,
  1973. info, gimple_omp_body_ptr (stmt));
  1974. if (info->new_local_var_chain)
  1975. declare_vars (info->new_local_var_chain,
  1976. gimple_seq_first_stmt (gimple_omp_body (stmt)),
  1977. false);
  1978. for (int i = 0; i < 2; i++)
  1979. {
  1980. tree c, decl;
  1981. if ((info->static_chain_added & (1 << i)) == 0)
  1982. continue;
  1983. decl = i ? get_chain_decl (info) : info->frame_decl;
  1984. /* Don't add CHAIN.* or FRAME.* twice. */
  1985. for (c = gimple_omp_taskreg_clauses (stmt);
  1986. c;
  1987. c = OMP_CLAUSE_CHAIN (c))
  1988. if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
  1989. || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
  1990. && OMP_CLAUSE_DECL (c) == decl)
  1991. break;
  1992. if (c == NULL && gimple_code (stmt) != GIMPLE_OMP_TARGET)
  1993. {
  1994. c = build_omp_clause (gimple_location (stmt),
  1995. i ? OMP_CLAUSE_FIRSTPRIVATE
  1996. : OMP_CLAUSE_SHARED);
  1997. OMP_CLAUSE_DECL (c) = decl;
  1998. OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
  1999. gimple_omp_taskreg_set_clauses (stmt, c);
  2000. }
  2001. else if (c == NULL)
  2002. {
  2003. c = build_omp_clause (gimple_location (stmt),
  2004. OMP_CLAUSE_MAP);
  2005. OMP_CLAUSE_DECL (c) = decl;
  2006. OMP_CLAUSE_SET_MAP_KIND (c,
  2007. i ? GOMP_MAP_TO : GOMP_MAP_TOFROM);
  2008. OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
  2009. OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
  2010. gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt),
  2011. c);
  2012. }
  2013. }
  2014. info->new_local_var_chain = save_local_var_chain;
  2015. info->static_chain_added |= save_static_chain_added;
  2016. }
  2017. break;
  2018. default:
  2019. *handled_ops_p = false;
  2020. return NULL_TREE;
  2021. }
  2022. *handled_ops_p = true;
  2023. return NULL_TREE;
  2024. }
  2025. /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_CALLs
  2026. that reference nested functions to make sure that the static chain
  2027. is set up properly for the call. */
  2028. static tree
  2029. convert_gimple_call (gimple_stmt_iterator *gsi, bool *handled_ops_p,
  2030. struct walk_stmt_info *wi)
  2031. {
  2032. struct nesting_info *const info = (struct nesting_info *) wi->info;
  2033. tree decl, target_context;
  2034. char save_static_chain_added;
  2035. int i;
  2036. gimple stmt = gsi_stmt (*gsi);
  2037. switch (gimple_code (stmt))
  2038. {
  2039. case GIMPLE_CALL:
  2040. if (gimple_call_chain (stmt))
  2041. break;
  2042. decl = gimple_call_fndecl (stmt);
  2043. if (!decl)
  2044. break;
  2045. target_context = decl_function_context (decl);
  2046. if (target_context && DECL_STATIC_CHAIN (decl))
  2047. {
  2048. gimple_call_set_chain (as_a <gcall *> (stmt),
  2049. get_static_chain (info, target_context,
  2050. &wi->gsi));
  2051. info->static_chain_added |= (1 << (info->context != target_context));
  2052. }
  2053. break;
  2054. case GIMPLE_OMP_PARALLEL:
  2055. case GIMPLE_OMP_TASK:
  2056. save_static_chain_added = info->static_chain_added;
  2057. info->static_chain_added = 0;
  2058. walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
  2059. for (i = 0; i < 2; i++)
  2060. {
  2061. tree c, decl;
  2062. if ((info->static_chain_added & (1 << i)) == 0)
  2063. continue;
  2064. decl = i ? get_chain_decl (info) : info->frame_decl;
  2065. /* Don't add CHAIN.* or FRAME.* twice. */
  2066. for (c = gimple_omp_taskreg_clauses (stmt);
  2067. c;
  2068. c = OMP_CLAUSE_CHAIN (c))
  2069. if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
  2070. || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
  2071. && OMP_CLAUSE_DECL (c) == decl)
  2072. break;
  2073. if (c == NULL)
  2074. {
  2075. c = build_omp_clause (gimple_location (stmt),
  2076. i ? OMP_CLAUSE_FIRSTPRIVATE
  2077. : OMP_CLAUSE_SHARED);
  2078. OMP_CLAUSE_DECL (c) = decl;
  2079. OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
  2080. gimple_omp_taskreg_set_clauses (stmt, c);
  2081. }
  2082. }
  2083. info->static_chain_added |= save_static_chain_added;
  2084. break;
  2085. case GIMPLE_OMP_TARGET:
  2086. if (!is_gimple_omp_offloaded (stmt))
  2087. {
  2088. walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
  2089. break;
  2090. }
  2091. save_static_chain_added = info->static_chain_added;
  2092. info->static_chain_added = 0;
  2093. walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
  2094. for (i = 0; i < 2; i++)
  2095. {
  2096. tree c, decl;
  2097. if ((info->static_chain_added & (1 << i)) == 0)
  2098. continue;
  2099. decl = i ? get_chain_decl (info) : info->frame_decl;
  2100. /* Don't add CHAIN.* or FRAME.* twice. */
  2101. for (c = gimple_omp_target_clauses (stmt);
  2102. c;
  2103. c = OMP_CLAUSE_CHAIN (c))
  2104. if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
  2105. && OMP_CLAUSE_DECL (c) == decl)
  2106. break;
  2107. if (c == NULL)
  2108. {
  2109. c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
  2110. OMP_CLAUSE_DECL (c) = decl;
  2111. OMP_CLAUSE_SET_MAP_KIND (c, i ? GOMP_MAP_TO : GOMP_MAP_TOFROM);
  2112. OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
  2113. OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
  2114. gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt),
  2115. c);
  2116. }
  2117. }
  2118. info->static_chain_added |= save_static_chain_added;
  2119. break;
  2120. case GIMPLE_OMP_FOR:
  2121. walk_body (convert_gimple_call, NULL, info,
  2122. gimple_omp_for_pre_body_ptr (stmt));
  2123. /* FALLTHRU */
  2124. case GIMPLE_OMP_SECTIONS:
  2125. case GIMPLE_OMP_SECTION:
  2126. case GIMPLE_OMP_SINGLE:
  2127. case GIMPLE_OMP_TEAMS:
  2128. case GIMPLE_OMP_MASTER:
  2129. case GIMPLE_OMP_TASKGROUP:
  2130. case GIMPLE_OMP_ORDERED:
  2131. case GIMPLE_OMP_CRITICAL:
  2132. walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
  2133. break;
  2134. default:
  2135. /* Keep looking for other operands. */
  2136. *handled_ops_p = false;
  2137. return NULL_TREE;
  2138. }
  2139. *handled_ops_p = true;
  2140. return NULL_TREE;
  2141. }
  2142. /* Walk the nesting tree starting with ROOT. Convert all trampolines and
  2143. call expressions. At the same time, determine if a nested function
  2144. actually uses its static chain; if not, remember that. */
  2145. static void
  2146. convert_all_function_calls (struct nesting_info *root)
  2147. {
  2148. unsigned int chain_count = 0, old_chain_count, iter_count;
  2149. struct nesting_info *n;
  2150. /* First, optimistically clear static_chain for all decls that haven't
  2151. used the static chain already for variable access. But always create
  2152. it if not optimizing. This makes it possible to reconstruct the static
  2153. nesting tree at run time and thus to resolve up-level references from
  2154. within the debugger. */
  2155. FOR_EACH_NEST_INFO (n, root)
  2156. {
  2157. tree decl = n->context;
  2158. if (!optimize)
  2159. {
  2160. if (n->inner)
  2161. (void) get_frame_type (n);
  2162. if (n->outer)
  2163. (void) get_chain_decl (n);
  2164. }
  2165. else if (!n->outer || (!n->chain_decl && !n->chain_field))
  2166. {
  2167. DECL_STATIC_CHAIN (decl) = 0;
  2168. if (dump_file && (dump_flags & TDF_DETAILS))
  2169. fprintf (dump_file, "Guessing no static-chain for %s\n",
  2170. lang_hooks.decl_printable_name (decl, 2));
  2171. }
  2172. else
  2173. DECL_STATIC_CHAIN (decl) = 1;
  2174. chain_count += DECL_STATIC_CHAIN (decl);
  2175. }
  2176. /* Walk the functions and perform transformations. Note that these
  2177. transformations can induce new uses of the static chain, which in turn
  2178. require re-examining all users of the decl. */
  2179. /* ??? It would make sense to try to use the call graph to speed this up,
  2180. but the call graph hasn't really been built yet. Even if it did, we
  2181. would still need to iterate in this loop since address-of references
  2182. wouldn't show up in the callgraph anyway. */
  2183. iter_count = 0;
  2184. do
  2185. {
  2186. old_chain_count = chain_count;
  2187. chain_count = 0;
  2188. iter_count++;
  2189. if (dump_file && (dump_flags & TDF_DETAILS))
  2190. fputc ('\n', dump_file);
  2191. FOR_EACH_NEST_INFO (n, root)
  2192. {
  2193. tree decl = n->context;
  2194. walk_function (convert_tramp_reference_stmt,
  2195. convert_tramp_reference_op, n);
  2196. walk_function (convert_gimple_call, NULL, n);
  2197. chain_count += DECL_STATIC_CHAIN (decl);
  2198. }
  2199. }
  2200. while (chain_count != old_chain_count);
  2201. if (dump_file && (dump_flags & TDF_DETAILS))
  2202. fprintf (dump_file, "convert_all_function_calls iterations: %u\n\n",
  2203. iter_count);
  2204. }
  2205. struct nesting_copy_body_data
  2206. {
  2207. copy_body_data cb;
  2208. struct nesting_info *root;
  2209. };
  2210. /* A helper subroutine for debug_var_chain type remapping. */
  2211. static tree
  2212. nesting_copy_decl (tree decl, copy_body_data *id)
  2213. {
  2214. struct nesting_copy_body_data *nid = (struct nesting_copy_body_data *) id;
  2215. tree *slot = nid->root->var_map->get (decl);
  2216. if (slot)
  2217. return (tree) *slot;
  2218. if (TREE_CODE (decl) == TYPE_DECL && DECL_ORIGINAL_TYPE (decl))
  2219. {
  2220. tree new_decl = copy_decl_no_change (decl, id);
  2221. DECL_ORIGINAL_TYPE (new_decl)
  2222. = remap_type (DECL_ORIGINAL_TYPE (decl), id);
  2223. return new_decl;
  2224. }
  2225. if (TREE_CODE (decl) == VAR_DECL
  2226. || TREE_CODE (decl) == PARM_DECL
  2227. || TREE_CODE (decl) == RESULT_DECL)
  2228. return decl;
  2229. return copy_decl_no_change (decl, id);
  2230. }
  2231. /* A helper function for remap_vla_decls. See if *TP contains
  2232. some remapped variables. */
  2233. static tree
  2234. contains_remapped_vars (tree *tp, int *walk_subtrees, void *data)
  2235. {
  2236. struct nesting_info *root = (struct nesting_info *) data;
  2237. tree t = *tp;
  2238. if (DECL_P (t))
  2239. {
  2240. *walk_subtrees = 0;
  2241. tree *slot = root->var_map->get (t);
  2242. if (slot)
  2243. return *slot;
  2244. }
  2245. return NULL;
  2246. }
  2247. /* Remap VLA decls in BLOCK and subblocks if remapped variables are
  2248. involved. */
  2249. static void
  2250. remap_vla_decls (tree block, struct nesting_info *root)
  2251. {
  2252. tree var, subblock, val, type;
  2253. struct nesting_copy_body_data id;
  2254. for (subblock = BLOCK_SUBBLOCKS (block);
  2255. subblock;
  2256. subblock = BLOCK_CHAIN (subblock))
  2257. remap_vla_decls (subblock, root);
  2258. for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
  2259. if (TREE_CODE (var) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (var))
  2260. {
  2261. val = DECL_VALUE_EXPR (var);
  2262. type = TREE_TYPE (var);
  2263. if (!(TREE_CODE (val) == INDIRECT_REF
  2264. && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
  2265. && variably_modified_type_p (type, NULL)))
  2266. continue;
  2267. if (root->var_map->get (TREE_OPERAND (val, 0))
  2268. || walk_tree (&type, contains_remapped_vars, root, NULL))
  2269. break;
  2270. }
  2271. if (var == NULL_TREE)
  2272. return;
  2273. memset (&id, 0, sizeof (id));
  2274. id.cb.copy_decl = nesting_copy_decl;
  2275. id.cb.decl_map = new hash_map<tree, tree>;
  2276. id.root = root;
  2277. for (; var; var = DECL_CHAIN (var))
  2278. if (TREE_CODE (var) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (var))
  2279. {
  2280. struct nesting_info *i;
  2281. tree newt, context;
  2282. val = DECL_VALUE_EXPR (var);
  2283. type = TREE_TYPE (var);
  2284. if (!(TREE_CODE (val) == INDIRECT_REF
  2285. && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
  2286. && variably_modified_type_p (type, NULL)))
  2287. continue;
  2288. tree *slot = root->var_map->get (TREE_OPERAND (val, 0));
  2289. if (!slot && !walk_tree (&type, contains_remapped_vars, root, NULL))
  2290. continue;
  2291. context = decl_function_context (var);
  2292. for (i = root; i; i = i->outer)
  2293. if (i->context == context)
  2294. break;
  2295. if (i == NULL)
  2296. continue;
  2297. /* Fully expand value expressions. This avoids having debug variables
  2298. only referenced from them and that can be swept during GC. */
  2299. if (slot)
  2300. {
  2301. tree t = (tree) *slot;
  2302. gcc_assert (DECL_P (t) && DECL_HAS_VALUE_EXPR_P (t));
  2303. val = build1 (INDIRECT_REF, TREE_TYPE (val), DECL_VALUE_EXPR (t));
  2304. }
  2305. id.cb.src_fn = i->context;
  2306. id.cb.dst_fn = i->context;
  2307. id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
  2308. TREE_TYPE (var) = newt = remap_type (type, &id.cb);
  2309. while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
  2310. {
  2311. newt = TREE_TYPE (newt);
  2312. type = TREE_TYPE (type);
  2313. }
  2314. if (TYPE_NAME (newt)
  2315. && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
  2316. && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
  2317. && newt != type
  2318. && TYPE_NAME (newt) == TYPE_NAME (type))
  2319. TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
  2320. walk_tree (&val, copy_tree_body_r, &id.cb, NULL);
  2321. if (val != DECL_VALUE_EXPR (var))
  2322. SET_DECL_VALUE_EXPR (var, val);
  2323. }
  2324. delete id.cb.decl_map;
  2325. }
  2326. /* Fold the MEM_REF *E. */
  2327. bool
  2328. fold_mem_refs (tree *const &e, void *data ATTRIBUTE_UNUSED)
  2329. {
  2330. tree *ref_p = CONST_CAST2 (tree *, const tree *, (const tree *)e);
  2331. *ref_p = fold (*ref_p);
  2332. return true;
  2333. }
  2334. /* Do "everything else" to clean up or complete state collected by the
  2335. various walking passes -- lay out the types and decls, generate code
  2336. to initialize the frame decl, store critical expressions in the
  2337. struct function for rtl to find. */
  2338. static void
  2339. finalize_nesting_tree_1 (struct nesting_info *root)
  2340. {
  2341. gimple_seq stmt_list;
  2342. gimple stmt;
  2343. tree context = root->context;
  2344. struct function *sf;
  2345. stmt_list = NULL;
  2346. /* If we created a non-local frame type or decl, we need to lay them
  2347. out at this time. */
  2348. if (root->frame_type)
  2349. {
  2350. /* In some cases the frame type will trigger the -Wpadded warning.
  2351. This is not helpful; suppress it. */
  2352. int save_warn_padded = warn_padded;
  2353. tree *adjust;
  2354. warn_padded = 0;
  2355. layout_type (root->frame_type);
  2356. warn_padded = save_warn_padded;
  2357. layout_decl (root->frame_decl, 0);
  2358. /* Remove root->frame_decl from root->new_local_var_chain, so
  2359. that we can declare it also in the lexical blocks, which
  2360. helps ensure virtual regs that end up appearing in its RTL
  2361. expression get substituted in instantiate_virtual_regs(). */
  2362. for (adjust = &root->new_local_var_chain;
  2363. *adjust != root->frame_decl;
  2364. adjust = &DECL_CHAIN (*adjust))
  2365. gcc_assert (DECL_CHAIN (*adjust));
  2366. *adjust = DECL_CHAIN (*adjust);
  2367. DECL_CHAIN (root->frame_decl) = NULL_TREE;
  2368. declare_vars (root->frame_decl,
  2369. gimple_seq_first_stmt (gimple_body (context)), true);
  2370. }
  2371. /* If any parameters were referenced non-locally, then we need to
  2372. insert a copy. Likewise, if any variables were referenced by
  2373. pointer, we need to initialize the address. */
  2374. if (root->any_parm_remapped)
  2375. {
  2376. tree p;
  2377. for (p = DECL_ARGUMENTS (context); p ; p = DECL_CHAIN (p))
  2378. {
  2379. tree field, x, y;
  2380. field = lookup_field_for_decl (root, p, NO_INSERT);
  2381. if (!field)
  2382. continue;
  2383. if (use_pointer_in_frame (p))
  2384. x = build_addr (p, context);
  2385. else
  2386. x = p;
  2387. /* If the assignment is from a non-register the stmt is
  2388. not valid gimple. Make it so by using a temporary instead. */
  2389. if (!is_gimple_reg (x)
  2390. && is_gimple_reg_type (TREE_TYPE (x)))
  2391. {
  2392. gimple_stmt_iterator gsi = gsi_last (stmt_list);
  2393. x = init_tmp_var (root, x, &gsi);
  2394. }
  2395. y = build3 (COMPONENT_REF, TREE_TYPE (field),
  2396. root->frame_decl, field, NULL_TREE);
  2397. stmt = gimple_build_assign (y, x);
  2398. gimple_seq_add_stmt (&stmt_list, stmt);
  2399. }
  2400. }
  2401. /* If a chain_field was created, then it needs to be initialized
  2402. from chain_decl. */
  2403. if (root->chain_field)
  2404. {
  2405. tree x = build3 (COMPONENT_REF, TREE_TYPE (root->chain_field),
  2406. root->frame_decl, root->chain_field, NULL_TREE);
  2407. stmt = gimple_build_assign (x, get_chain_decl (root));
  2408. gimple_seq_add_stmt (&stmt_list, stmt);
  2409. }
  2410. /* If trampolines were created, then we need to initialize them. */
  2411. if (root->any_tramp_created)
  2412. {
  2413. struct nesting_info *i;
  2414. for (i = root->inner; i ; i = i->next)
  2415. {
  2416. tree arg1, arg2, arg3, x, field;
  2417. field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
  2418. if (!field)
  2419. continue;
  2420. gcc_assert (DECL_STATIC_CHAIN (i->context));
  2421. arg3 = build_addr (root->frame_decl, context);
  2422. arg2 = build_addr (i->context, context);
  2423. x = build3 (COMPONENT_REF, TREE_TYPE (field),
  2424. root->frame_decl, field, NULL_TREE);
  2425. arg1 = build_addr (x, context);
  2426. x = builtin_decl_implicit (BUILT_IN_INIT_TRAMPOLINE);
  2427. stmt = gimple_build_call (x, 3, arg1, arg2, arg3);
  2428. gimple_seq_add_stmt (&stmt_list, stmt);
  2429. }
  2430. }
  2431. /* If we created initialization statements, insert them. */
  2432. if (stmt_list)
  2433. {
  2434. gbind *bind;
  2435. annotate_all_with_location (stmt_list, DECL_SOURCE_LOCATION (context));
  2436. bind = gimple_seq_first_stmt_as_a_bind (gimple_body (context));
  2437. gimple_seq_add_seq (&stmt_list, gimple_bind_body (bind));
  2438. gimple_bind_set_body (bind, stmt_list);
  2439. }
  2440. /* If a chain_decl was created, then it needs to be registered with
  2441. struct function so that it gets initialized from the static chain
  2442. register at the beginning of the function. */
  2443. sf = DECL_STRUCT_FUNCTION (root->context);
  2444. sf->static_chain_decl = root->chain_decl;
  2445. /* Similarly for the non-local goto save area. */
  2446. if (root->nl_goto_field)
  2447. {
  2448. sf->nonlocal_goto_save_area
  2449. = get_frame_field (root, context, root->nl_goto_field, NULL);
  2450. sf->has_nonlocal_label = 1;
  2451. }
  2452. /* Make sure all new local variables get inserted into the
  2453. proper BIND_EXPR. */
  2454. if (root->new_local_var_chain)
  2455. declare_vars (root->new_local_var_chain,
  2456. gimple_seq_first_stmt (gimple_body (root->context)),
  2457. false);
  2458. if (root->debug_var_chain)
  2459. {
  2460. tree debug_var;
  2461. gbind *scope;
  2462. remap_vla_decls (DECL_INITIAL (root->context), root);
  2463. for (debug_var = root->debug_var_chain; debug_var;
  2464. debug_var = DECL_CHAIN (debug_var))
  2465. if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
  2466. break;
  2467. /* If there are any debug decls with variable length types,
  2468. remap those types using other debug_var_chain variables. */
  2469. if (debug_var)
  2470. {
  2471. struct nesting_copy_body_data id;
  2472. memset (&id, 0, sizeof (id));
  2473. id.cb.copy_decl = nesting_copy_decl;
  2474. id.cb.decl_map = new hash_map<tree, tree>;
  2475. id.root = root;
  2476. for (; debug_var; debug_var = DECL_CHAIN (debug_var))
  2477. if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
  2478. {
  2479. tree type = TREE_TYPE (debug_var);
  2480. tree newt, t = type;
  2481. struct nesting_info *i;
  2482. for (i = root; i; i = i->outer)
  2483. if (variably_modified_type_p (type, i->context))
  2484. break;
  2485. if (i == NULL)
  2486. continue;
  2487. id.cb.src_fn = i->context;
  2488. id.cb.dst_fn = i->context;
  2489. id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
  2490. TREE_TYPE (debug_var) = newt = remap_type (type, &id.cb);
  2491. while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
  2492. {
  2493. newt = TREE_TYPE (newt);
  2494. t = TREE_TYPE (t);
  2495. }
  2496. if (TYPE_NAME (newt)
  2497. && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
  2498. && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
  2499. && newt != t
  2500. && TYPE_NAME (newt) == TYPE_NAME (t))
  2501. TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
  2502. }
  2503. delete id.cb.decl_map;
  2504. }
  2505. scope = gimple_seq_first_stmt_as_a_bind (gimple_body (root->context));
  2506. if (gimple_bind_block (scope))
  2507. declare_vars (root->debug_var_chain, scope, true);
  2508. else
  2509. BLOCK_VARS (DECL_INITIAL (root->context))
  2510. = chainon (BLOCK_VARS (DECL_INITIAL (root->context)),
  2511. root->debug_var_chain);
  2512. }
  2513. /* Fold the rewritten MEM_REF trees. */
  2514. root->mem_refs->traverse<void *, fold_mem_refs> (NULL);
  2515. /* Dump the translated tree function. */
  2516. if (dump_file)
  2517. {
  2518. fputs ("\n\n", dump_file);
  2519. dump_function_to_file (root->context, dump_file, dump_flags);
  2520. }
  2521. }
  2522. static void
  2523. finalize_nesting_tree (struct nesting_info *root)
  2524. {
  2525. struct nesting_info *n;
  2526. FOR_EACH_NEST_INFO (n, root)
  2527. finalize_nesting_tree_1 (n);
  2528. }
  2529. /* Unnest the nodes and pass them to cgraph. */
  2530. static void
  2531. unnest_nesting_tree_1 (struct nesting_info *root)
  2532. {
  2533. struct cgraph_node *node = cgraph_node::get (root->context);
  2534. /* For nested functions update the cgraph to reflect unnesting.
  2535. We also delay finalizing of these functions up to this point. */
  2536. if (node->origin)
  2537. {
  2538. node->unnest ();
  2539. cgraph_node::finalize_function (root->context, true);
  2540. }
  2541. }
  2542. static void
  2543. unnest_nesting_tree (struct nesting_info *root)
  2544. {
  2545. struct nesting_info *n;
  2546. FOR_EACH_NEST_INFO (n, root)
  2547. unnest_nesting_tree_1 (n);
  2548. }
  2549. /* Free the data structures allocated during this pass. */
  2550. static void
  2551. free_nesting_tree (struct nesting_info *root)
  2552. {
  2553. struct nesting_info *node, *next;
  2554. node = iter_nestinfo_start (root);
  2555. do
  2556. {
  2557. next = iter_nestinfo_next (node);
  2558. delete node->var_map;
  2559. delete node->field_map;
  2560. delete node->mem_refs;
  2561. free (node);
  2562. node = next;
  2563. }
  2564. while (node);
  2565. }
  2566. /* Gimplify a function and all its nested functions. */
  2567. static void
  2568. gimplify_all_functions (struct cgraph_node *root)
  2569. {
  2570. struct cgraph_node *iter;
  2571. if (!gimple_body (root->decl))
  2572. gimplify_function_tree (root->decl);
  2573. for (iter = root->nested; iter; iter = iter->next_nested)
  2574. gimplify_all_functions (iter);
  2575. }
  2576. /* Main entry point for this pass. Process FNDECL and all of its nested
  2577. subroutines and turn them into something less tightly bound. */
  2578. void
  2579. lower_nested_functions (tree fndecl)
  2580. {
  2581. struct cgraph_node *cgn;
  2582. struct nesting_info *root;
  2583. /* If there are no nested functions, there's nothing to do. */
  2584. cgn = cgraph_node::get (fndecl);
  2585. if (!cgn->nested)
  2586. return;
  2587. gimplify_all_functions (cgn);
  2588. dump_file = dump_begin (TDI_nested, &dump_flags);
  2589. if (dump_file)
  2590. fprintf (dump_file, "\n;; Function %s\n\n",
  2591. lang_hooks.decl_printable_name (fndecl, 2));
  2592. bitmap_obstack_initialize (&nesting_info_bitmap_obstack);
  2593. root = create_nesting_tree (cgn);
  2594. walk_all_functions (convert_nonlocal_reference_stmt,
  2595. convert_nonlocal_reference_op,
  2596. root);
  2597. walk_all_functions (convert_local_reference_stmt,
  2598. convert_local_reference_op,
  2599. root);
  2600. walk_all_functions (convert_nl_goto_reference, NULL, root);
  2601. walk_all_functions (convert_nl_goto_receiver, NULL, root);
  2602. convert_all_function_calls (root);
  2603. finalize_nesting_tree (root);
  2604. unnest_nesting_tree (root);
  2605. free_nesting_tree (root);
  2606. bitmap_obstack_release (&nesting_info_bitmap_obstack);
  2607. if (dump_file)
  2608. {
  2609. dump_end (TDI_nested, dump_file);
  2610. dump_file = NULL;
  2611. }
  2612. }
  2613. #include "gt-tree-nested.h"