Line data Source code
1 : /* strub (stack scrubbing) support.
2 : Copyright (C) 2021-2026 Free Software Foundation, Inc.
3 : Contributed by Alexandre Oliva <oliva@adacore.com>.
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it under
8 : the terms of the GNU General Public License as published by the Free
9 : Software Foundation; either version 3, or (at your option) any later
10 : version.
11 :
12 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #include "config.h"
22 : #include "system.h"
23 : #include "coretypes.h"
24 : #include "backend.h"
25 : #include "tree.h"
26 : #include "gimple.h"
27 : #include "gimplify.h"
28 : #include "tree-pass.h"
29 : #include "ssa.h"
30 : #include "gimple-iterator.h"
31 : #include "gimplify-me.h"
32 : #include "tree-into-ssa.h"
33 : #include "tree-ssa.h"
34 : #include "tree-cfg.h"
35 : #include "cfghooks.h"
36 : #include "cfgloop.h"
37 : #include "cfgcleanup.h"
38 : #include "tree-eh.h"
39 : #include "except.h"
40 : #include "builtins.h"
41 : #include "attribs.h"
42 : #include "tree-inline.h"
43 : #include "cgraph.h"
44 : #include "alloc-pool.h"
45 : #include "symbol-summary.h"
46 : #include "sreal.h"
47 : #include "ipa-cp.h"
48 : #include "ipa-prop.h"
49 : #include "ipa-fnsummary.h"
50 : #include "gimple-fold.h"
51 : #include "fold-const.h"
52 : #include "gimple-walk.h"
53 : #include "tree-dfa.h"
54 : #include "langhooks.h"
55 : #include "calls.h"
56 : #include "vec.h"
57 : #include "stor-layout.h"
58 : #include "varasm.h"
59 : #include "alias.h"
60 : #include "diagnostic.h"
61 : #include "intl.h"
62 : #include "ipa-strub.h"
63 : #include "symtab-thunks.h"
64 : #include "attr-fnspec.h"
65 : #include "target.h"
66 : #include "gcc-urlifier.h"
67 :
68 : /* This file introduces two passes that, together, implement
69 : machine-independent stack scrubbing, strub for short. It arranges
70 : for stack frames that have strub enabled to be zeroed-out after
71 : relinquishing control to a caller, whether by returning or by
72 : propagating an exception. This admittedly unusual design decision
73 : was driven by exception support (one needs a stack frame to be
74 : active to propagate exceptions out of it), and it enabled an
75 : implementation that is entirely machine-independent (no custom
76 : epilogue code is required).
77 :
78 : Strub modes can be selected for stack frames by attaching attribute
79 : strub to functions or to variables (to their types, actually).
80 : Different strub modes, with different implementation details, are
81 : available, and they can be selected by an argument to the strub
82 : attribute. When enabled by strub-enabled variables, whether by
83 : accessing (as in reading from) statically-allocated ones, or by
84 : introducing (as in declaring) automatically-allocated ones, a
85 : suitable mode is selected automatically.
86 :
87 : At-calls mode modifies the interface of a function, adding a stack
88 : watermark argument, that callers use to clean up the stack frame of
89 : the called function. Because of the interface change, it can only
90 : be used when explicitly selected, or when a function is internal to
91 : a translation unit. Strub-at-calls function types are distinct
92 : from their original types (they're not modified in-place), and they
93 : are not interchangeable with other function types.
94 :
95 : Internal mode, in turn, does not modify the type or the interface
96 : of a function. It is currently implemented by turning the function
97 : into a wrapper, moving the function body to a separate wrapped
98 : function, and scrubbing the wrapped body's stack in the wrapper.
99 : Internal-strub function types are mostly interface-compatible with
100 : other strub modes, namely callable (from strub functions, though
101 : not strub-enabled) and disabled (not callable from strub
102 : functions).
103 :
104 : Always_inline functions can be strub functions, but they can only
105 : be called from other strub functions, because strub functions must
106 : never be inlined into non-strub functions. Internal and at-calls
107 : modes are indistinguishable when it comes to always_inline
108 : functions: they will necessarily be inlined into another strub
109 : function, and will thus be integrated into the caller's stack
110 : frame, whatever the mode. (Contrast with non-always_inline strub
111 : functions: an at-calls function can be called from other strub
112 : functions, ensuring no discontinuity in stack erasing, whereas an
113 : internal-strub function can only be called from other strub
114 : functions if it happens to be inlined, or if -fstrub=relaxed mode
115 : is in effect (that's the default). In -fstrub=strict mode,
116 : internal-strub functions are not callable from strub functions,
117 : because the wrapper itself is not strubbed.
118 :
119 : The implementation involves two simple-IPA passes. The earliest
120 : one, strub-mode, assigns strub modes to functions. It needs to run
121 : before any inlining, so that we can prevent inlining of strub
122 : functions into non-strub functions. It notes explicit strub mode
123 : requests, enables strub in response to strub variables and testing
124 : options, and flags unsatisfiable requests.
125 :
126 : Three possibilities of unsatisfiable requests come to mind: (a)
127 : when a strub mode is explicitly selected, but the function uses
128 : features that make it ineligible for that mode (e.g. at-calls rules
129 : out calling __builtin_apply_args, because of the interface changes,
130 : and internal mode rules out noclone or otherwise non-versionable
131 : functions, non-default varargs, non-local or forced labels, and
132 : functions with far too many arguments); (b) when some strub mode
133 : must be enabled because of a strub variable, but the function is
134 : not eligible or not viable for any mode; and (c) when
135 : -fstrub=strict is enabled, and calls are found in strub functions
136 : to functions that are not callable from strub contexts.
137 : compute_strub_mode implements (a) and (b), and verify_strub
138 : implements (c).
139 :
140 : The second IPA pass modifies interfaces of at-calls-strub functions
141 : and types, introduces strub calls in and around them. and splits
142 : internal-strub functions. It is placed after early inlining, so
143 : that even internal-strub functions get a chance of being inlined
144 : into other strub functions, but before non-early inlining, so that
145 : internal-strub wrapper functions still get a chance of inlining
146 : after splitting.
147 :
148 : Wrappers avoid duplicating the copying of large arguments again by
149 : passing them by reference to the wrapped bodies. This involves
150 : occasional SSA rewriting of address computations, because of the
151 : additional indirection. Besides these changes, and the
152 : introduction of the stack watermark parameter, wrappers and wrapped
153 : functions cooperate to handle variable argument lists (performing
154 : va_start in the wrapper, passing the list as an argument, and
155 : replacing va_start calls in the wrapped body with va_copy), and
156 : __builtin_apply_args (also called in the wrapper and passed to the
157 : wrapped body as an argument).
158 :
159 : Strub bodies (both internal-mode wrapped bodies, and at-calls
160 : functions) always start by adjusting the watermark parameter, by
161 : calling __builtin___strub_update. The compiler inserts them in the
162 : main strub pass. Allocations of additional stack space for the
163 : frame (__builtin_alloca) are also followed by watermark updates.
164 : Stack space temporarily allocated to pass arguments to other
165 : functions, released right after the call, is not regarded as part
166 : of the frame. Around calls to them, i.e., in internal-mode
167 : wrappers and at-calls callers (even calls through pointers), calls
168 : to __builtin___strub_enter and __builtin___strub_leave are
169 : inserted, the latter as a __finally block, so that it runs at
170 : regular and exceptional exit paths. strub_enter only initializes
171 : the stack watermark, and strub_leave is where the scrubbing takes
172 : place, overwriting with zeros the stack space from the top of the
173 : stack to the watermark.
174 :
175 : These calls can be optimized in various cases. In
176 : pass_ipa_strub::adjust_at_calls_call, for example, we enable
177 : tail-calling and other optimized calls from one strub body to
178 : another by passing on the watermark parameter. The builtins
179 : themselves may undergo inline substitution during expansion,
180 : dependign on optimization levels. This involves dealing with stack
181 : red zones (when the builtins are called out-of-line, the red zone
182 : cannot be used) and other ugly details related with inlining strub
183 : bodies into other strub bodies (see expand_builtin_strub_update).
184 : expand_builtin_strub_leave may even perform partial inline
185 : substitution. */
186 :
187 : /* Const and pure functions that gain a watermark parameter for strub purposes
188 : are still regarded as such, which may cause the inline expansions of the
189 : __strub builtins to malfunction. Ideally, attribute "fn spec" would enable
190 : us to inform the backend about requirements and side effects of the call, but
191 : call_fusage building in calls.c:expand_call does not even look at
192 : attr_fnspec, so we resort to asm loads and updates to attain an equivalent
193 : effect. Once expand_call gains the ability to issue extra memory uses and
194 : clobbers based on pure/const function's fnspec, we can define this to 1. */
195 : #define ATTR_FNSPEC_DECONST_WATERMARK 0
196 :
197 : enum strub_mode {
198 : /* This mode denotes a regular function, that does not require stack
199 : scrubbing (strubbing). It may call any other functions, but if
200 : it calls AT_CALLS (or WRAPPED) ones, strubbing logic is
201 : automatically introduced around those calls (the latter, by
202 : inlining INTERNAL wrappers). */
203 : STRUB_DISABLED = 0,
204 :
205 : /* This denotes a function whose signature is (to be) modified to
206 : take an extra parameter, for stack use annotation, and its
207 : callers must initialize and pass that argument, and perform the
208 : strubbing. Functions that are explicitly marked with attribute
209 : strub must have the mark visible wherever the function is,
210 : including aliases, and overriders and overriding methods.
211 : Functions that are implicitly marked for strubbing, for accessing
212 : variables explicitly marked as such, will only select this
213 : strubbing method if they are internal to a translation unit. It
214 : can only be inlined into other strubbing functions, i.e.,
215 : STRUB_AT_CALLS or STRUB_WRAPPED. */
216 : STRUB_AT_CALLS = 1,
217 :
218 : /* This denotes a function that is to perform strubbing internally,
219 : without any changes to its interface (the function is turned into
220 : a strubbing wrapper, and its original body is moved to a separate
221 : STRUB_WRAPPED function, with a modified interface). Functions
222 : may be explicitly marked with attribute strub(2), and the
223 : attribute must be visible at the point of definition. Functions
224 : that are explicitly marked for strubbing, for accessing variables
225 : explicitly marked as such, may select this strubbing mode if
226 : their interface cannot change, e.g. because its interface is
227 : visible to other translation units, directly, by indirection
228 : (having its address taken), inheritance, etc. Functions that use
229 : this method must not have the noclone attribute, nor the noipa
230 : one. Functions marked as always_inline may select this mode, but
231 : they are NOT wrapped, they remain unchanged, and are only inlined
232 : into strubbed contexts. Once non-always_inline functions are
233 : wrapped, the wrapper becomes STRUB_WRAPPER, and the wrapped becomes
234 : STRUB_WRAPPED. */
235 : STRUB_INTERNAL = 2,
236 :
237 : /* This denotes a function whose stack is not strubbed, but that is
238 : nevertheless explicitly or implicitly marked as callable from strubbing
239 : functions. Normally, only STRUB_AT_CALLS (and STRUB_INTERNAL ->
240 : STRUB_WRAPPED) functions can be called from strubbing contexts (bodies of
241 : STRUB_AT_CALLS, STRUB_INTERNAL and STRUB_WRAPPED functions), but attribute
242 : strub(3) enables other functions to be (indirectly) called from these
243 : contexts. Some builtins and internal functions may be implicitly marked as
244 : STRUB_CALLABLE. */
245 : STRUB_CALLABLE = 3,
246 :
247 : /* This denotes the function that took over the body of a
248 : STRUB_INTERNAL function. At first, it's only called by its
249 : wrapper, but the wrapper may be inlined. The wrapped function,
250 : in turn, can only be inlined into other functions whose stack
251 : frames are strubbed, i.e., that are STRUB_WRAPPED or
252 : STRUB_AT_CALLS. */
253 : STRUB_WRAPPED = -1,
254 :
255 : /* This denotes the wrapper function that replaced the STRUB_INTERNAL
256 : function. This mode overrides the STRUB_INTERNAL mode at the time the
257 : internal to-be-wrapped function becomes a wrapper, so that inlining logic
258 : can tell one from the other. */
259 : STRUB_WRAPPER = -2,
260 :
261 : /* This denotes an always_inline function that requires strubbing. It can
262 : only be called from, and inlined into, other strubbing contexts. */
263 : STRUB_INLINABLE = -3,
264 :
265 : /* This denotes a function that accesses strub variables, so it would call for
266 : internal strubbing (whether or not it's eligible for that), but since
267 : at-calls strubbing is viable, that's selected as an optimization. This
268 : mode addresses the inconvenience that such functions may have different
269 : modes selected depending on optimization flags, and get a different
270 : callable status depending on that choice: if we assigned them
271 : STRUB_AT_CALLS mode, they would be callable when optimizing, whereas
272 : STRUB_INTERNAL would not be callable. */
273 : STRUB_AT_CALLS_OPT = -4,
274 :
275 : };
276 :
277 : /* Look up a strub attribute in TYPE, and return it. */
278 :
279 : static tree
280 14672039 : get_strub_attr_from_type (tree type)
281 : {
282 14672039 : return lookup_attribute ("strub", TYPE_ATTRIBUTES (type));
283 : }
284 :
285 : /* Look up a strub attribute in DECL or in its type, and return it. */
286 :
287 : static tree
288 11477406 : get_strub_attr_from_decl (tree decl)
289 : {
290 11477406 : tree ret = lookup_attribute ("strub", DECL_ATTRIBUTES (decl));
291 11477406 : if (ret)
292 : return ret;
293 11461519 : return get_strub_attr_from_type (TREE_TYPE (decl));
294 : }
295 :
296 : #define STRUB_ID_COUNT 8
297 : #define STRUB_IDENT_COUNT 3
298 : #define STRUB_TYPE_COUNT 5
299 :
300 : #define STRUB_ID_BASE 0
301 : #define STRUB_IDENT_BASE (STRUB_ID_BASE + STRUB_ID_COUNT)
302 : #define STRUB_TYPE_BASE (STRUB_IDENT_BASE + STRUB_IDENT_COUNT)
303 : #define STRUB_CACHE_SIZE (STRUB_TYPE_BASE + STRUB_TYPE_COUNT)
304 :
305 : /* Keep the strub mode and temp identifiers and types from being GC'd. */
306 : static GTY((deletable)) tree strub_cache[STRUB_CACHE_SIZE];
307 :
308 : /* Define a function to cache identifier ID, to be used as a strub attribute
309 : parameter for a strub mode named after NAME. */
310 : #define DEF_STRUB_IDS(IDX, NAME, ID) \
311 : static inline tree get_strub_mode_id_ ## NAME () { \
312 : int idx = STRUB_ID_BASE + IDX; \
313 : tree identifier = strub_cache[idx]; \
314 : if (!identifier) \
315 : strub_cache[idx] = identifier = get_identifier (ID); \
316 : return identifier; \
317 : }
318 : /* Same as DEF_STRUB_IDS, but use the string expansion of NAME as ID. */
319 : #define DEF_STRUB_ID(IDX, NAME) \
320 : DEF_STRUB_IDS (IDX, NAME, #NAME)
321 :
322 : /* Define functions for each of the strub mode identifiers.
323 : Expose dashes rather than underscores. */
324 10163 : DEF_STRUB_ID (0, disabled)
325 18652 : DEF_STRUB_IDS (1, at_calls, "at-calls")
326 15474 : DEF_STRUB_ID (2, internal)
327 24234 : DEF_STRUB_ID (3, callable)
328 2229 : DEF_STRUB_ID (4, wrapped)
329 2394 : DEF_STRUB_ID (5, wrapper)
330 3507 : DEF_STRUB_ID (6, inlinable)
331 40 : DEF_STRUB_IDS (7, at_calls_opt, "at-calls-opt")
332 :
333 : /* Release the temporary macro names. */
334 : #undef DEF_STRUB_IDS
335 : #undef DEF_STRUB_ID
336 :
337 : /* Return the identifier corresponding to strub MODE. */
338 :
339 : static tree
340 76693 : get_strub_mode_attr_parm (enum strub_mode mode)
341 : {
342 76693 : switch (mode)
343 : {
344 10163 : case STRUB_DISABLED:
345 10163 : return get_strub_mode_id_disabled ();
346 :
347 18652 : case STRUB_AT_CALLS:
348 18652 : return get_strub_mode_id_at_calls ();
349 :
350 15474 : case STRUB_INTERNAL:
351 15474 : return get_strub_mode_id_internal ();
352 :
353 24234 : case STRUB_CALLABLE:
354 24234 : return get_strub_mode_id_callable ();
355 :
356 2229 : case STRUB_WRAPPED:
357 2229 : return get_strub_mode_id_wrapped ();
358 :
359 2394 : case STRUB_WRAPPER:
360 2394 : return get_strub_mode_id_wrapper ();
361 :
362 3507 : case STRUB_INLINABLE:
363 3507 : return get_strub_mode_id_inlinable ();
364 :
365 40 : case STRUB_AT_CALLS_OPT:
366 40 : return get_strub_mode_id_at_calls_opt ();
367 :
368 0 : default:
369 0 : gcc_unreachable ();
370 : }
371 : }
372 :
373 : /* Return the parameters (TREE_VALUE) for a strub attribute of MODE.
374 : We know we use a single parameter, so we bypass the creation of a
375 : tree list. */
376 :
377 : static tree
378 2707 : get_strub_mode_attr_value (enum strub_mode mode)
379 : {
380 0 : return get_strub_mode_attr_parm (mode);
381 : }
382 :
383 : /* Determine whether ID is a well-formed strub mode-specifying attribute
384 : parameter for a function (type). Only user-visible modes are accepted, and
385 : ID must be non-NULL.
386 :
387 : For unacceptable parms, return 0, otherwise a nonzero value as below.
388 :
389 : If the parm enables strub, return positive, otherwise negative.
390 :
391 : If the affected type must be a distinct, incompatible type,return an integer
392 : of absolute value 2, otherwise 1. */
393 :
394 : int
395 2195 : strub_validate_fn_attr_parm (tree id)
396 : {
397 2195 : int ret;
398 2195 : const char *s = NULL;
399 2195 : size_t len = 0;
400 :
401 : /* do NOT test for NULL. This is only to be called with non-NULL arguments.
402 : We assume that the strub parameter applies to a function, because only
403 : functions accept an explicit argument. If we accepted NULL, and we
404 : happened to be called to verify the argument for a variable, our return
405 : values would be wrong. */
406 2195 : if (TREE_CODE (id) == STRING_CST)
407 : {
408 2195 : s = TREE_STRING_POINTER (id);
409 2195 : len = TREE_STRING_LENGTH (id) - 1;
410 : }
411 0 : else if (TREE_CODE (id) == IDENTIFIER_NODE)
412 : {
413 0 : s = IDENTIFIER_POINTER (id);
414 0 : len = IDENTIFIER_LENGTH (id);
415 : }
416 : else
417 : return 0;
418 :
419 2195 : enum strub_mode mode;
420 :
421 2195 : if (len != 8)
422 : return 0;
423 :
424 2195 : switch (s[0])
425 : {
426 : case 'd':
427 : mode = STRUB_DISABLED;
428 : ret = -1;
429 : break;
430 :
431 590 : case 'a':
432 590 : mode = STRUB_AT_CALLS;
433 590 : ret = 2;
434 590 : break;
435 :
436 571 : case 'i':
437 571 : mode = STRUB_INTERNAL;
438 571 : ret = 1;
439 571 : break;
440 :
441 580 : case 'c':
442 580 : mode = STRUB_CALLABLE;
443 580 : ret = -2;
444 580 : break;
445 :
446 : default:
447 : /* Other parms are for internal use only. */
448 : return 0;
449 : }
450 :
451 2195 : tree mode_id = get_strub_mode_attr_parm (mode);
452 :
453 2195 : if (TREE_CODE (id) == IDENTIFIER_NODE
454 2195 : ? id != mode_id
455 2195 : : strncmp (s, IDENTIFIER_POINTER (mode_id), len) != 0)
456 0 : return 0;
457 :
458 : return ret;
459 : }
460 :
461 : /* Return the strub mode from STRUB_ATTR. VAR_P should be TRUE if the attribute
462 : is taken from a variable, rather than from a function, or a type thereof. */
463 :
464 : static enum strub_mode
465 11508723 : get_strub_mode_from_attr (tree strub_attr, bool var_p = false)
466 : {
467 11508723 : enum strub_mode mode = STRUB_DISABLED;
468 :
469 11508723 : if (strub_attr)
470 : {
471 75315 : if (!TREE_VALUE (strub_attr))
472 3548 : mode = !var_p ? STRUB_AT_CALLS : STRUB_INTERNAL;
473 : else
474 : {
475 71767 : gcc_checking_assert (!var_p);
476 71767 : tree id = TREE_VALUE (strub_attr);
477 71767 : if (TREE_CODE (id) == TREE_LIST)
478 55880 : id = TREE_VALUE (id);
479 71767 : const char *s = (TREE_CODE (id) == STRING_CST
480 71767 : ? TREE_STRING_POINTER (id)
481 71767 : : IDENTIFIER_POINTER (id));
482 71767 : size_t len = (TREE_CODE (id) == STRING_CST
483 71767 : ? TREE_STRING_LENGTH (id) - 1
484 71767 : : IDENTIFIER_LENGTH (id));
485 :
486 71767 : switch (len)
487 : {
488 3659 : case 7:
489 3659 : switch (s[6])
490 : {
491 : case 'r':
492 : mode = STRUB_WRAPPER;
493 : break;
494 :
495 1747 : case 'd':
496 1747 : mode = STRUB_WRAPPED;
497 1747 : break;
498 :
499 0 : default:
500 0 : gcc_unreachable ();
501 : }
502 : break;
503 :
504 64956 : case 8:
505 64956 : switch (s[0])
506 : {
507 : case 'd':
508 : mode = STRUB_DISABLED;
509 : break;
510 :
511 17981 : case 'a':
512 17981 : mode = STRUB_AT_CALLS;
513 17981 : break;
514 :
515 14389 : case 'i':
516 14389 : mode = STRUB_INTERNAL;
517 14389 : break;
518 :
519 22901 : case 'c':
520 22901 : mode = STRUB_CALLABLE;
521 22901 : break;
522 :
523 0 : default:
524 0 : gcc_unreachable ();
525 : }
526 : break;
527 :
528 : case 9:
529 : mode = STRUB_INLINABLE;
530 : break;
531 :
532 32 : case 12:
533 32 : mode = STRUB_AT_CALLS_OPT;
534 32 : break;
535 :
536 0 : default:
537 0 : gcc_unreachable ();
538 : }
539 :
540 71767 : gcc_checking_assert (TREE_CODE (id) == IDENTIFIER_NODE
541 : ? id == get_strub_mode_attr_parm (mode)
542 : : strncmp (IDENTIFIER_POINTER
543 : (get_strub_mode_attr_parm (mode)),
544 : s, len) == 0);
545 : }
546 : }
547 :
548 11508723 : return mode;
549 : }
550 :
551 : /* Look up, decode and return the strub mode associated with FNDECL. */
552 :
553 : static enum strub_mode
554 11466848 : get_strub_mode_from_fndecl (tree fndecl)
555 : {
556 11466848 : return get_strub_mode_from_attr (get_strub_attr_from_decl (fndecl));
557 : }
558 :
559 : /* Look up, decode and return the strub mode associated with NODE. */
560 :
561 : static enum strub_mode
562 11447072 : get_strub_mode (cgraph_node *node)
563 : {
564 0 : return get_strub_mode_from_fndecl (node->decl);
565 : }
566 :
567 : /* Look up, decode and return the strub mode associated with TYPE. */
568 :
569 : static enum strub_mode
570 3210520 : get_strub_mode_from_type (tree type)
571 : {
572 3210520 : bool var_p = !FUNC_OR_METHOD_TYPE_P (type);
573 3210520 : tree attr = get_strub_attr_from_type (type);
574 :
575 3210520 : if (attr)
576 32090 : return get_strub_mode_from_attr (attr, var_p);
577 :
578 3178430 : if (flag_strub >= -1 && !var_p)
579 8110 : return STRUB_CALLABLE;
580 :
581 : return STRUB_DISABLED;
582 : }
583 :
584 :
585 : /* Return TRUE iff NODE calls builtin va_start. */
586 :
587 : static bool
588 482 : calls_builtin_va_start_p (cgraph_node *node)
589 : {
590 482 : bool result = false;
591 :
592 3085 : for (cgraph_edge *e = node->callees; e; e = e->next_callee)
593 : {
594 2611 : tree cdecl = e->callee->decl;
595 2611 : if (fndecl_built_in_p (cdecl, BUILT_IN_VA_START))
596 : return true;
597 : }
598 :
599 : return result;
600 : }
601 :
602 : /* Return TRUE iff NODE calls builtin apply_args, and optionally REPORT it. */
603 :
604 : static bool
605 2899 : calls_builtin_apply_args_p (cgraph_node *node, bool report = false)
606 : {
607 2899 : bool result = false;
608 :
609 10209 : for (cgraph_edge *e = node->callees; e; e = e->next_callee)
610 : {
611 7338 : tree cdecl = e->callee->decl;
612 7338 : if (!fndecl_built_in_p (cdecl, BUILT_IN_APPLY_ARGS))
613 7306 : continue;
614 :
615 32 : result = true;
616 :
617 32 : if (!report)
618 : break;
619 :
620 8 : sorry_at (e->call_stmt
621 4 : ? gimple_location (e->call_stmt)
622 0 : : DECL_SOURCE_LOCATION (node->decl),
623 : "at-calls %<strub%> does not support call to %qD",
624 : cdecl);
625 : }
626 :
627 2899 : return result;
628 : }
629 :
630 : /* Return TRUE iff NODE carries the always_inline attribute. */
631 :
632 : static inline bool
633 7833 : strub_always_inline_p (cgraph_node *node)
634 : {
635 7833 : return lookup_attribute ("always_inline", DECL_ATTRIBUTES (node->decl));
636 : }
637 :
638 : /* Return TRUE iff the target has strub support for T, a function
639 : decl, or a type used in an indirect call, and optionally REPORT the
640 : reasons for ineligibility. If T is a type and error REPORTing is
641 : enabled, the LOCation (of the indirect call) should be provided. */
642 : static inline bool
643 4290 : strub_target_support_p (tree t, bool report = false,
644 : location_t loc = UNKNOWN_LOCATION)
645 : {
646 4290 : bool result = true;
647 :
648 4290 : if (!targetm.have_strub_support_for (t))
649 : {
650 0 : result = false;
651 :
652 0 : if (!report)
653 : return result;
654 :
655 0 : if (DECL_P (t))
656 0 : sorry_at (DECL_SOURCE_LOCATION (t),
657 : "%qD is not eligible for %<strub%>"
658 : " on the target system", t);
659 : else
660 0 : sorry_at (loc,
661 : "unsupported %<strub%> call"
662 : " on the target system");
663 : }
664 :
665 : return result;
666 : }
667 :
668 : /* Return TRUE iff NODE is potentially eligible for any strub-enabled mode, and
669 : optionally REPORT the reasons for ineligibility. */
670 :
671 : static inline bool
672 2362 : can_strub_p (cgraph_node *node, bool report = false)
673 : {
674 2362 : bool result = strub_target_support_p (node->decl, report);
675 :
676 2362 : if (!report && (!result || strub_always_inline_p (node)))
677 : return result;
678 :
679 2362 : auto_urlify_attributes sentinel;
680 :
681 2362 : if (flag_split_stack)
682 : {
683 16 : result = false;
684 :
685 16 : if (!report)
686 : return result;
687 :
688 8 : sorry_at (DECL_SOURCE_LOCATION (node->decl),
689 : "%qD is not eligible for %<strub%>"
690 : " because %<-fsplit-stack%> is enabled",
691 : node->decl);
692 : }
693 :
694 2354 : if (lookup_attribute ("noipa", DECL_ATTRIBUTES (node->decl)))
695 : {
696 1 : result = false;
697 :
698 1 : if (!report)
699 : return result;
700 :
701 0 : sorry_at (DECL_SOURCE_LOCATION (node->decl),
702 : "%qD is not eligible for %<strub%>"
703 : " because of attribute %<noipa%>",
704 : node->decl);
705 : }
706 :
707 : /* We can't, and don't want to vectorize the watermark and other
708 : strub-introduced parms. */
709 2353 : if (lookup_attribute ("simd", DECL_ATTRIBUTES (node->decl)))
710 : {
711 0 : result = false;
712 :
713 0 : if (!report)
714 : return result;
715 :
716 0 : sorry_at (DECL_SOURCE_LOCATION (node->decl),
717 : "%qD is not eligible for %<strub%>"
718 : " because of attribute %<simd%>",
719 : node->decl);
720 : }
721 :
722 : return result;
723 2362 : }
724 :
725 : /* Return TRUE iff NODE is eligible for at-calls strub, and optionally REPORT
726 : the reasons for ineligibility. Besides general non-eligibility for
727 : strub-enabled modes, at-calls rules out calling builtin apply_args. */
728 :
729 : static bool
730 2417 : can_strub_at_calls_p (cgraph_node *node, bool report = false)
731 : {
732 8 : bool result = !report || can_strub_p (node, report);
733 :
734 : if (!result && !report)
735 : return result;
736 :
737 2417 : return !calls_builtin_apply_args_p (node, report);
738 : }
739 :
740 : /* Return TRUE iff the called function (pointer or, if available,
741 : decl) undergoes a significant type conversion for the call. Strub
742 : mode changes between function types, and other non-useless type
743 : conversions, are regarded as significant. When the function type
744 : is overridden, the effective strub mode for the call is that of the
745 : call fntype, rather than that of the pointer or of the decl.
746 : Functions called with type overrides cannot undergo type changes;
747 : it's as if their address was taken, so they're considered
748 : non-viable for implicit at-calls strub mode. */
749 :
750 : static inline bool
751 19534 : strub_call_fntype_override_p (const gcall *gs)
752 : {
753 19534 : if (gimple_call_internal_p (gs))
754 : return false;
755 19534 : tree fn_type = TREE_TYPE (TREE_TYPE (gimple_call_fn (gs)));
756 19534 : if (tree decl = gimple_call_fndecl (gs))
757 19262 : fn_type = TREE_TYPE (decl);
758 :
759 : /* We do NOT want to take the mode from the decl here. This
760 : function is used to tell whether we can change the strub mode of
761 : a function, and whether the effective mode for the call is to be
762 : taken from the decl or from an overrider type. When the strub
763 : mode is explicitly declared, or overridden with a type cast, the
764 : difference will be noticed in function types. However, if the
765 : strub mode is implicit due to e.g. strub variables or -fstrub=*
766 : command-line flags, we will adjust call types along with function
767 : types. In either case, the presence of type or strub mode
768 : overriders in calls will prevent a function from having its strub
769 : modes changed in ways that would imply type changes, but taking
770 : strub modes from decls would defeat this, since we set strub
771 : modes and then call this function to tell whether the original
772 : type was overridden to decide whether to adjust the call. We
773 : need the answer to be about the type, not the decl. */
774 19534 : enum strub_mode mode = get_strub_mode_from_type (fn_type);
775 19534 : return (get_strub_mode_from_type (gs->u.fntype) != mode
776 19534 : || !useless_type_conversion_p (gs->u.fntype, fn_type));
777 : }
778 :
779 : /* Return TRUE iff NODE is called directly with a type override. */
780 :
781 : static bool
782 457 : called_directly_with_type_override_p (cgraph_node *node, void *)
783 : {
784 1290 : for (cgraph_edge *e = node->callers; e; e = e->next_caller)
785 833 : if (e->call_stmt && strub_call_fntype_override_p (e->call_stmt))
786 : return true;
787 :
788 : return false;
789 : }
790 :
791 : /* Return TRUE iff NODE or any other nodes aliased to it are called
792 : with type overrides. We can't safely change the type of such
793 : functions. */
794 :
795 : static bool
796 457 : called_with_type_override_p (cgraph_node *node)
797 : {
798 457 : return (node->call_for_symbol_thunks_and_aliases
799 457 : (called_directly_with_type_override_p, NULL, true, true));
800 : }
801 :
802 : /* Symbolic macro for the max number of arguments that internal strub may add to
803 : a function. */
804 :
805 : #define STRUB_INTERNAL_MAX_EXTRA_ARGS 3
806 :
807 : /* We can't perform internal strubbing if the function body involves certain
808 : features:
809 :
810 : - a non-default __builtin_va_start (e.g. x86's __builtin_ms_va_start) is
811 : currently unsupported because we can't discover the corresponding va_copy and
812 : va_end decls in the wrapper, and we don't convey the alternate variable
813 : arguments ABI to the modified wrapped function. The default
814 : __builtin_va_start is supported by calling va_start/va_end at the wrapper,
815 : that takes variable arguments, passing a pointer to the va_list object to the
816 : wrapped function, that runs va_copy from it where the original function ran
817 : va_start.
818 :
819 : __builtin_next_arg is currently unsupported because the wrapped function
820 : won't be a variable argument function. We could process it in the wrapper,
821 : that remains a variable argument function, and replace calls in the wrapped
822 : body, but we currently don't.
823 :
824 : __builtin_return_address is rejected because it's generally used when the
825 : actual caller matters, and introducing a wrapper breaks such uses as those in
826 : the unwinder. */
827 :
828 : static bool
829 1930 : can_strub_internally_p (cgraph_node *node, bool report = false)
830 : {
831 1930 : bool result = !report || can_strub_p (node, report);
832 :
833 : if (!result && !report)
834 : return result;
835 :
836 1930 : if (!report && strub_always_inline_p (node))
837 : return result;
838 :
839 : /* Since we're not changing the function identity proper, just
840 : moving its full implementation, we *could* disable
841 : fun->cannot_be_copied_reason and/or temporarily drop a noclone
842 : attribute, but we'd have to prevent remapping of the labels. */
843 1930 : if (lookup_attribute ("noclone", DECL_ATTRIBUTES (node->decl)))
844 : {
845 48 : result = false;
846 :
847 48 : if (!report)
848 : return result;
849 :
850 0 : sorry_at (DECL_SOURCE_LOCATION (node->decl),
851 : "%qD is not eligible for internal %<strub%>"
852 : " because of attribute %<noclone%>",
853 : node->decl);
854 : }
855 :
856 1882 : if (node->has_gimple_body_p ())
857 : {
858 3814 : for (cgraph_edge *e = node->callees; e; e = e->next_callee)
859 : {
860 2910 : tree cdecl = e->callee->decl;
861 5820 : if (!((fndecl_built_in_p (cdecl, BUILT_IN_VA_START)
862 8 : && cdecl != builtin_decl_explicit (BUILT_IN_VA_START))
863 2910 : || fndecl_built_in_p (cdecl, BUILT_IN_NEXT_ARG)
864 2910 : || fndecl_built_in_p (cdecl, BUILT_IN_RETURN_ADDRESS)))
865 2910 : continue;
866 :
867 0 : result = false;
868 :
869 0 : if (!report)
870 : return result;
871 :
872 0 : sorry_at (e->call_stmt
873 0 : ? gimple_location (e->call_stmt)
874 0 : : DECL_SOURCE_LOCATION (node->decl),
875 : "%qD is not eligible for internal %<strub%> "
876 : "because it calls %qD",
877 : node->decl, cdecl);
878 : }
879 :
880 904 : struct function *fun = DECL_STRUCT_FUNCTION (node->decl);
881 904 : if (fun->has_nonlocal_label)
882 : {
883 0 : result = false;
884 :
885 0 : if (!report)
886 : return result;
887 :
888 0 : sorry_at (DECL_SOURCE_LOCATION (node->decl),
889 : "%qD is not eligible for internal %<strub%> "
890 : "because it contains a non-local goto target",
891 : node->decl);
892 : }
893 :
894 904 : if (fun->has_forced_label_in_static)
895 : {
896 0 : result = false;
897 :
898 0 : if (!report)
899 : return result;
900 :
901 0 : sorry_at (DECL_SOURCE_LOCATION (node->decl),
902 : "%qD is not eligible for internal %<strub%> "
903 : "because the address of a local label escapes",
904 : node->decl);
905 : }
906 :
907 : /* Catch any other case that would prevent versioning/cloning
908 : so as to also have it covered above. */
909 904 : gcc_checking_assert (!result /* || !node->has_gimple_body_p () */
910 : || tree_versionable_function_p (node->decl));
911 :
912 :
913 : /* Label values references are not preserved when copying. If referenced
914 : in nested functions, as in 920415-1.c and 920721-4.c their decls get
915 : remapped independently. The exclusion below might be too broad, in
916 : that we might be able to support correctly cases in which the labels
917 : are only used internally in a function, but disconnecting forced labels
918 : from their original declarations is undesirable in general. */
919 904 : basic_block bb;
920 3187 : FOR_EACH_BB_FN (bb, DECL_STRUCT_FUNCTION (node->decl))
921 4566 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
922 2422 : !gsi_end_p (gsi); gsi_next (&gsi))
923 : {
924 2422 : glabel *label_stmt = dyn_cast <glabel *> (gsi_stmt (gsi));
925 139 : tree target;
926 :
927 139 : if (!label_stmt)
928 : break;
929 :
930 139 : target = gimple_label_label (label_stmt);
931 :
932 139 : if (!FORCED_LABEL (target))
933 139 : continue;
934 :
935 0 : result = false;
936 :
937 0 : if (!report)
938 1930 : return result;
939 :
940 0 : sorry_at (gimple_location (label_stmt),
941 : "internal %<strub%> does not support forced labels");
942 : }
943 : }
944 :
945 1882 : if (list_length (TYPE_ARG_TYPES (TREE_TYPE (node->decl)))
946 : >= ((HOST_WIDE_INT_1 << IPA_PARAM_MAX_INDEX_BITS)
947 : - STRUB_INTERNAL_MAX_EXTRA_ARGS))
948 : {
949 0 : result = false;
950 :
951 0 : if (!report)
952 : return result;
953 :
954 0 : sorry_at (DECL_SOURCE_LOCATION (node->decl),
955 : "%qD has too many arguments for internal %<strub%>",
956 : node->decl);
957 : }
958 :
959 : return result;
960 : }
961 :
962 : /* Return TRUE iff NODE has any strub-requiring local variable, or accesses (as
963 : in reading) any variable through a strub-requiring type. */
964 :
965 : static bool
966 1906 : strub_from_body_p (cgraph_node *node)
967 : {
968 1906 : if (!node->has_gimple_body_p ())
969 : return false;
970 :
971 : /* If any local variable is marked for strub... */
972 1154 : unsigned i;
973 1154 : tree var;
974 9607 : FOR_EACH_LOCAL_DECL (DECL_STRUCT_FUNCTION (node->decl),
975 : i, var)
976 7483 : if (get_strub_mode_from_type (TREE_TYPE (var))
977 : != STRUB_DISABLED)
978 : return true;
979 :
980 : /* Now scan the body for loads with strub-requiring types.
981 : ??? Compound types don't propagate the strub requirement to
982 : component types. */
983 1140 : basic_block bb;
984 4284 : FOR_EACH_BB_FN (bb, DECL_STRUCT_FUNCTION (node->decl))
985 6860 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
986 17279 : !gsi_end_p (gsi); gsi_next (&gsi))
987 : {
988 14135 : gimple *stmt = gsi_stmt (gsi);
989 :
990 14135 : if (!gimple_assign_load_p (stmt))
991 11576 : continue;
992 :
993 2559 : tree rhs = gimple_assign_rhs1 (stmt);
994 2559 : if (get_strub_mode_from_type (TREE_TYPE (rhs))
995 : != STRUB_DISABLED)
996 1906 : return true;
997 : }
998 :
999 : return false;
1000 : }
1001 :
1002 : /* Return TRUE iff node is associated with a builtin that should be callable
1003 : from strub contexts. */
1004 :
1005 : static inline bool
1006 1175 : strub_callable_builtin_p (cgraph_node *node)
1007 : {
1008 1175 : if (DECL_BUILT_IN_CLASS (node->decl) != BUILT_IN_NORMAL)
1009 : return false;
1010 :
1011 489 : enum built_in_function fcode = DECL_FUNCTION_CODE (node->decl);
1012 :
1013 489 : switch (fcode)
1014 : {
1015 0 : case BUILT_IN_NONE:
1016 0 : gcc_unreachable ();
1017 :
1018 : /* This temporarily allocates stack for the call, and we can't reasonably
1019 : update the watermark for that. Besides, we don't check the actual call
1020 : target, nor its signature, and it seems to be overkill to as much as
1021 : try to do so. */
1022 : case BUILT_IN_APPLY:
1023 : return false;
1024 :
1025 : /* Conversely, this shouldn't be called from within strub contexts, since
1026 : the caller may have had its signature modified. STRUB_INTERNAL is ok,
1027 : the call will remain in the STRUB_WRAPPER, and removed from the
1028 : STRUB_WRAPPED clone. */
1029 : case BUILT_IN_APPLY_ARGS:
1030 : return false;
1031 :
1032 : /* ??? Make all other builtins callable. We wish to make any builtin call
1033 : the compiler might introduce on its own callable. Anything that is
1034 : predictable enough as to be known not to allow stack data that should
1035 : be strubbed to unintentionally escape to non-strub contexts can be
1036 : allowed, and pretty much every builtin appears to fit this description.
1037 : The exceptions to this rule seem to be rare, and only available as
1038 : explicit __builtin calls, so let's keep it simple and allow all of
1039 : them... */
1040 : default:
1041 : return true;
1042 : }
1043 : }
1044 :
1045 : /* Compute the strub mode to be used for NODE. STRUB_ATTR should be the strub
1046 : attribute,found for NODE, if any. */
1047 :
1048 : static enum strub_mode
1049 3581 : compute_strub_mode (cgraph_node *node, tree strub_attr)
1050 : {
1051 3581 : if (node->assume_function)
1052 : return STRUB_CALLABLE;
1053 :
1054 3557 : enum strub_mode req_mode = get_strub_mode_from_attr (strub_attr);
1055 :
1056 3557 : gcc_checking_assert (flag_strub >= -2 && flag_strub <= 3);
1057 :
1058 : /* Symbolic encodings of the -fstrub-* flags. */
1059 : /* Enable strub when explicitly requested through attributes to functions or
1060 : variables, reporting errors if the requests cannot be satisfied. */
1061 3557 : const bool strub_flag_auto = flag_strub < 0;
1062 : /* strub_flag_auto with strub call verification; without this, functions are
1063 : implicitly callable. */
1064 3557 : const bool strub_flag_strict = flag_strub < -1;
1065 : /* Disable strub altogether, ignore attributes entirely. */
1066 3557 : const bool strub_flag_disabled = flag_strub == 0;
1067 : /* On top of _auto, also enable strub implicitly for functions that can
1068 : safely undergo at-calls strubbing. Internal mode will still be used in
1069 : functions that request it explicitly with attribute strub(2), or when the
1070 : function body requires strubbing and at-calls strubbing is not viable. */
1071 3557 : const bool strub_flag_at_calls = flag_strub == 1;
1072 : /* On top of default, also enable strub implicitly for functions that can
1073 : safely undergo internal strubbing. At-calls mode will still be used in
1074 : functions that request it explicitly with attribute strub() or strub(1),
1075 : or when the function body requires strubbing and internal strubbing is not
1076 : viable. */
1077 3557 : const bool strub_flag_internal = flag_strub == 2;
1078 : /* On top of default, also enable strub implicitly for functions that can
1079 : safely undergo strubbing in either mode. When both modes are viable,
1080 : at-calls is preferred. */
1081 3557 : const bool strub_flag_either = flag_strub == 3;
1082 : /* Besides the default behavior, enable strub implicitly for all viable
1083 : functions. */
1084 3557 : const bool strub_flag_viable = flag_strub > 0;
1085 :
1086 : /* The consider_* variables should be TRUE if selecting the corresponding
1087 : strub modes would be consistent with requests from attributes and command
1088 : line flags. Attributes associated with functions pretty much mandate a
1089 : selection, and should report an error if not satisfied; strub_flag_auto
1090 : implicitly enables some viable strub mode if that's required by references
1091 : to variables marked for strub; strub_flag_viable enables strub if viable
1092 : (even when favoring one mode, body-requested strub can still be satisfied
1093 : by either mode), and falls back to callable, silently unless variables
1094 : require strubbing. */
1095 :
1096 10671 : const bool consider_at_calls
1097 : = (!strub_flag_disabled
1098 3557 : && (strub_attr
1099 3557 : ? req_mode == STRUB_AT_CALLS
1100 : : true));
1101 10671 : const bool consider_internal
1102 : = (!strub_flag_disabled
1103 : && (strub_attr
1104 3557 : ? req_mode == STRUB_INTERNAL
1105 : : true));
1106 :
1107 3557 : const bool consider_callable
1108 : = (!strub_flag_disabled
1109 3557 : && (strub_attr
1110 1883 : ? req_mode == STRUB_CALLABLE
1111 : : (!strub_flag_strict
1112 1175 : || strub_callable_builtin_p (node))));
1113 :
1114 : /* This is a shorthand for either strub-enabled mode. */
1115 3557 : const bool consider_strub
1116 : = (consider_at_calls || consider_internal);
1117 :
1118 : /* We can cope with always_inline functions even with noipa and noclone,
1119 : because we just leave them alone. */
1120 3557 : const bool is_always_inline
1121 3557 : = strub_always_inline_p (node);
1122 :
1123 : /* Strubbing in general, and each specific strub mode, may have its own set of
1124 : requirements. We require noipa for strubbing, either because of cloning
1125 : required for internal strub, or because of caller enumeration required for
1126 : at-calls strub. We don't consider the at-calls mode eligible if it's not
1127 : even considered, it has no further requirements. Internal mode requires
1128 : cloning and the absence of certain features in the body and, like at-calls,
1129 : it's not eligible if it's not even under consideration.
1130 :
1131 : ??? Do we need target hooks for further constraints? E.g., x86's
1132 : "interrupt" attribute breaks internal strubbing because the wrapped clone
1133 : carries the attribute and thus isn't callable; in this case, we could use a
1134 : target hook to adjust the clone instead. */
1135 3557 : const bool strub_eligible
1136 : = (consider_strub
1137 3557 : && (is_always_inline || can_strub_p (node)));
1138 2890 : const bool at_calls_eligible
1139 : = (consider_at_calls && strub_eligible
1140 5299 : && can_strub_at_calls_p (node));
1141 3557 : const bool internal_eligible
1142 3557 : = (consider_internal && strub_eligible
1143 3557 : && (is_always_inline
1144 1926 : || can_strub_internally_p (node)));
1145 :
1146 : /* In addition to the strict eligibility requirements, some additional
1147 : constraints are placed on implicit selection of certain modes. These do
1148 : not prevent the selection of a mode if explicitly specified as part of a
1149 : function interface (the strub attribute), but they may prevent modes from
1150 : being selected by the command line or by function bodies. The only actual
1151 : constraint is on at-calls mode: since we change the function's exposed
1152 : signature, we won't do it implicitly if the function can possibly be used
1153 : in ways that do not expect the signature change, e.g., if the function is
1154 : available to or interposable by other units, if its address is taken,
1155 : etc. */
1156 3557 : const bool at_calls_viable
1157 : = (at_calls_eligible
1158 3557 : && (strub_attr
1159 1874 : || (node->has_gimple_body_p ()
1160 968 : && (!node->externally_visible
1161 475 : || (node->binds_to_current_def_p ()
1162 463 : && node->can_be_local_p ()))
1163 493 : && node->only_called_directly_p ()
1164 457 : && !called_with_type_override_p (node))));
1165 4708 : const bool internal_viable
1166 : = (internal_eligible);
1167 :
1168 : /* Shorthand. */
1169 4708 : const bool strub_viable
1170 : = (at_calls_viable || internal_viable);
1171 :
1172 : /* We wish to analyze the body, to look for implicit requests for strub, both
1173 : to implicitly enable it when the body calls for it, and to report errors if
1174 : the body calls for it but neither mode is viable (even if that follows from
1175 : non-eligibility because of the explicit specification of some non-strubbing
1176 : mode). We can refrain from scanning the body only in rare circumstances:
1177 : when strub is enabled by a function attribute (scanning might be redundant
1178 : in telling us to also enable it), and when we are enabling strub implicitly
1179 : but there are non-viable modes: we want to know whether strubbing is
1180 : required, to fallback to another mode, even if we're only enabling a
1181 : certain mode, or, when either mode would do, to report an error if neither
1182 : happens to be viable. */
1183 1674 : const bool analyze_body
1184 : = (strub_attr
1185 3034 : ? !consider_strub
1186 : : (strub_flag_auto
1187 636 : || (strub_flag_viable && (!at_calls_viable && !internal_viable))
1188 2518 : || (strub_flag_either && !strub_viable)));
1189 :
1190 : /* Cases in which strubbing is enabled or disabled by strub_flag_auto.
1191 : Unsatisfiable requests ought to be reported. */
1192 3557 : const bool strub_required
1193 3557 : = ((strub_attr && consider_strub)
1194 3557 : || (analyze_body && strub_from_body_p (node)));
1195 :
1196 : /* Besides the required cases, we want to abide by the requests to enabling on
1197 : an if-viable basis. */
1198 7143 : const bool strub_enable
1199 : = (strub_required
1200 2241 : || (strub_flag_at_calls && at_calls_viable)
1201 2177 : || (strub_flag_internal && internal_viable)
1202 1966 : || (strub_flag_either && strub_viable));
1203 :
1204 : /* And now we're finally ready to select a mode that abides by the viability
1205 : and eligibility constraints, and that satisfies the strubbing requirements
1206 : and requests, subject to the constraints. If both modes are viable and
1207 : strub is to be enabled, pick STRUB_AT_CALLS unless STRUB_INTERNAL was named
1208 : as preferred. */
1209 1832 : const enum strub_mode mode
1210 : = ((strub_enable && is_always_inline)
1211 1832 : ? (strub_required ? STRUB_INLINABLE : STRUB_CALLABLE)
1212 : : (strub_enable && internal_viable
1213 922 : && (strub_flag_internal || !at_calls_viable))
1214 3070 : ? STRUB_INTERNAL
1215 2229 : : (strub_enable && at_calls_viable)
1216 2229 : ? (strub_required && !strub_attr
1217 492 : ? STRUB_AT_CALLS_OPT
1218 : : STRUB_AT_CALLS)
1219 : : consider_callable
1220 1737 : ? STRUB_CALLABLE
1221 : : STRUB_DISABLED);
1222 :
1223 8 : switch (mode)
1224 : {
1225 1099 : case STRUB_CALLABLE:
1226 1099 : if (is_always_inline)
1227 : break;
1228 : /* Fall through. */
1229 :
1230 1549 : case STRUB_DISABLED:
1231 1549 : if (strub_enable && !strub_attr)
1232 : {
1233 0 : gcc_checking_assert (analyze_body);
1234 0 : error_at (DECL_SOURCE_LOCATION (node->decl),
1235 : "%qD requires %<strub%>,"
1236 : " but no viable %<strub%> mode was found",
1237 : node->decl);
1238 0 : break;
1239 : }
1240 : /* Fall through. */
1241 :
1242 : case STRUB_AT_CALLS:
1243 : case STRUB_INTERNAL:
1244 : case STRUB_INLINABLE:
1245 : /* Differences from an mode requested through a function attribute are
1246 : reported in set_strub_mode_to. */
1247 : break;
1248 :
1249 8 : case STRUB_AT_CALLS_OPT:
1250 : /* Functions that select this mode do so because of references to strub
1251 : variables. Even if we choose at-calls as an optimization, the
1252 : requirements for internal strub must still be satisfied. Optimization
1253 : options may render implicit at-calls strub not viable (-O0 sets
1254 : force_output for static non-inline functions), and it would not be good
1255 : if changing optimization options turned a well-formed into an
1256 : ill-formed one. */
1257 8 : if (!internal_viable)
1258 0 : can_strub_internally_p (node, true);
1259 : break;
1260 :
1261 : case STRUB_WRAPPED:
1262 : case STRUB_WRAPPER:
1263 : default:
1264 : gcc_unreachable ();
1265 : }
1266 :
1267 : return mode;
1268 : }
1269 :
1270 : /* Set FNDT's strub mode to MODE; FNDT may be a function decl or
1271 : function type. If OVERRIDE, do not check whether a mode is already
1272 : set. */
1273 :
1274 : static void
1275 2707 : strub_set_fndt_mode_to (tree fndt, enum strub_mode mode, bool override)
1276 : {
1277 2707 : gcc_checking_assert (override
1278 : || !(DECL_P (fndt)
1279 : ? get_strub_attr_from_decl (fndt)
1280 : : get_strub_attr_from_type (fndt)));
1281 :
1282 2707 : tree attr = tree_cons (get_identifier ("strub"),
1283 : get_strub_mode_attr_value (mode),
1284 : NULL_TREE);
1285 2707 : tree *attrp = NULL;
1286 2707 : if (DECL_P (fndt))
1287 : {
1288 2707 : gcc_checking_assert (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (fndt)));
1289 2707 : attrp = &DECL_ATTRIBUTES (fndt);
1290 : }
1291 0 : else if (FUNC_OR_METHOD_TYPE_P (fndt))
1292 0 : attrp = &TYPE_ATTRIBUTES (fndt);
1293 : else
1294 0 : gcc_unreachable ();
1295 :
1296 2707 : TREE_CHAIN (attr) = *attrp;
1297 2707 : *attrp = attr;
1298 2707 : }
1299 :
1300 : /* Set FNDT's strub mode to callable.
1301 : FNDT may be a function decl or a function type. */
1302 :
1303 : void
1304 0 : strub_make_callable (tree fndt)
1305 : {
1306 0 : strub_set_fndt_mode_to (fndt, STRUB_CALLABLE, false);
1307 0 : }
1308 :
1309 : /* Set NODE to strub MODE. Report incompatibilities between MODE and the mode
1310 : requested through explicit attributes, and cases of non-eligibility. */
1311 :
1312 : static void
1313 4554 : set_strub_mode_to (cgraph_node *node, enum strub_mode mode)
1314 : {
1315 4554 : tree attr = get_strub_attr_from_decl (node->decl);
1316 4554 : enum strub_mode req_mode = get_strub_mode_from_attr (attr);
1317 :
1318 4554 : if (attr)
1319 : {
1320 : /* Check for and report incompatible mode changes. */
1321 2638 : if (mode != req_mode
1322 1229 : && !(req_mode == STRUB_INTERNAL
1323 : && (mode == STRUB_WRAPPED
1324 1229 : || mode == STRUB_WRAPPER))
1325 265 : && !((req_mode == STRUB_INTERNAL
1326 : || req_mode == STRUB_AT_CALLS
1327 265 : || req_mode == STRUB_CALLABLE)
1328 : && mode == STRUB_INLINABLE))
1329 : {
1330 12 : error_at (DECL_SOURCE_LOCATION (node->decl),
1331 : "%<strub%> mode %qE selected for %qD, when %qE was requested",
1332 : get_strub_mode_attr_parm (mode),
1333 : node->decl,
1334 : get_strub_mode_attr_parm (req_mode));
1335 12 : if (node->alias)
1336 : {
1337 0 : cgraph_node *target = node->ultimate_alias_target ();
1338 0 : if (target != node)
1339 0 : error_at (DECL_SOURCE_LOCATION (target->decl),
1340 : "the incompatible selection was determined"
1341 : " by ultimate alias target %qD",
1342 : target->decl);
1343 : }
1344 :
1345 : /* Report any incompatibilities with explicitly-requested strub. */
1346 12 : switch (req_mode)
1347 : {
1348 8 : case STRUB_AT_CALLS:
1349 8 : can_strub_at_calls_p (node, true);
1350 8 : break;
1351 :
1352 4 : case STRUB_INTERNAL:
1353 4 : can_strub_internally_p (node, true);
1354 4 : break;
1355 :
1356 : default:
1357 : break;
1358 : }
1359 : }
1360 :
1361 : /* Drop any incompatible strub attributes leading the decl attribute
1362 : chain. Return if we find one with the mode we need. */
1363 2638 : for (;;)
1364 : {
1365 2638 : if (mode == req_mode)
1366 : return;
1367 :
1368 1229 : if (DECL_ATTRIBUTES (node->decl) != attr)
1369 : break;
1370 :
1371 468 : DECL_ATTRIBUTES (node->decl) = TREE_CHAIN (attr);
1372 468 : attr = get_strub_attr_from_decl (node->decl);
1373 468 : if (!attr)
1374 : break;
1375 :
1376 0 : req_mode = get_strub_mode_from_attr (attr);
1377 : }
1378 : }
1379 1916 : else if (mode == req_mode)
1380 : return;
1381 :
1382 2707 : strub_set_fndt_mode_to (node->decl, mode, attr);
1383 : }
1384 :
1385 : /* Compute and set NODE's strub mode. */
1386 :
1387 : static void
1388 3590 : set_strub_mode (cgraph_node *node)
1389 : {
1390 3590 : tree attr = get_strub_attr_from_decl (node->decl);
1391 :
1392 3590 : if (attr)
1393 1674 : switch (get_strub_mode_from_attr (attr))
1394 : {
1395 : /* These can't have been requested through user attributes, so we must
1396 : have already gone through them. */
1397 : case STRUB_WRAPPER:
1398 : case STRUB_WRAPPED:
1399 : case STRUB_INLINABLE:
1400 : case STRUB_AT_CALLS_OPT:
1401 : return;
1402 :
1403 : case STRUB_DISABLED:
1404 : case STRUB_AT_CALLS:
1405 : case STRUB_INTERNAL:
1406 : case STRUB_CALLABLE:
1407 : break;
1408 :
1409 0 : default:
1410 0 : gcc_unreachable ();
1411 : }
1412 :
1413 3590 : cgraph_node *xnode = node;
1414 3590 : if (node->alias)
1415 9 : xnode = node->ultimate_alias_target ();
1416 : /* Weakrefs may remain unresolved (the above will return node) if
1417 : their targets are not defined, so make sure we compute a strub
1418 : mode for them, instead of defaulting to STRUB_DISABLED and
1419 : rendering them uncallable. */
1420 9 : enum strub_mode mode = (xnode != node && !xnode->alias
1421 3599 : ? get_strub_mode (xnode)
1422 3590 : : compute_strub_mode (node, attr));
1423 :
1424 3590 : set_strub_mode_to (node, mode);
1425 : }
1426 :
1427 :
1428 : /* Non-strub functions shouldn't be called from within strub contexts,
1429 : except through callable ones. Always inline strub functions can
1430 : only be called from strub functions. */
1431 :
1432 : static bool
1433 9154 : strub_callable_from_p (strub_mode caller_mode, strub_mode callee_mode)
1434 : {
1435 9154 : switch (caller_mode)
1436 : {
1437 4839 : case STRUB_WRAPPED:
1438 4839 : case STRUB_AT_CALLS_OPT:
1439 4839 : case STRUB_AT_CALLS:
1440 4839 : case STRUB_INTERNAL:
1441 4839 : case STRUB_INLINABLE:
1442 4839 : break;
1443 :
1444 4315 : case STRUB_WRAPPER:
1445 4315 : case STRUB_DISABLED:
1446 4315 : case STRUB_CALLABLE:
1447 4315 : return callee_mode != STRUB_INLINABLE;
1448 :
1449 0 : default:
1450 0 : gcc_unreachable ();
1451 : }
1452 :
1453 4839 : switch (callee_mode)
1454 : {
1455 : case STRUB_WRAPPED:
1456 : case STRUB_AT_CALLS:
1457 : case STRUB_INLINABLE:
1458 : break;
1459 :
1460 862 : case STRUB_AT_CALLS_OPT:
1461 862 : case STRUB_INTERNAL:
1462 862 : case STRUB_WRAPPER:
1463 862 : return (flag_strub >= -1);
1464 :
1465 : case STRUB_DISABLED:
1466 : return false;
1467 :
1468 : case STRUB_CALLABLE:
1469 : break;
1470 :
1471 0 : default:
1472 0 : gcc_unreachable ();
1473 : }
1474 :
1475 3641 : return true;
1476 : }
1477 :
1478 : /* Return TRUE iff CALLEE can be inlined into CALLER. We wish to avoid inlining
1479 : WRAPPED functions back into their WRAPPERs. More generally, we wish to avoid
1480 : inlining strubbed functions into non-strubbed ones. CALLER doesn't have to
1481 : be an immediate caller of CALLEE: the immediate caller may have already been
1482 : cloned for inlining, and then CALLER may be further up the original call
1483 : chain. ??? It would be nice if our own caller would retry inlining callee
1484 : if caller gets inlined. */
1485 :
1486 : bool
1487 10124384 : strub_inlinable_to_p (cgraph_node *callee, cgraph_node *caller)
1488 : {
1489 10124384 : if (caller->assume_function)
1490 : return true;
1491 :
1492 10124264 : strub_mode callee_mode = get_strub_mode (callee);
1493 :
1494 10124264 : switch (callee_mode)
1495 : {
1496 3870 : case STRUB_WRAPPED:
1497 3870 : case STRUB_AT_CALLS:
1498 3870 : case STRUB_INTERNAL:
1499 3870 : case STRUB_INLINABLE:
1500 3870 : case STRUB_AT_CALLS_OPT:
1501 3870 : break;
1502 :
1503 : case STRUB_WRAPPER:
1504 : case STRUB_DISABLED:
1505 : case STRUB_CALLABLE:
1506 : /* When we consider inlining, we've already verified callability, so we
1507 : can even inline callable and then disabled into a strub context. That
1508 : will get strubbed along with the context, so it's hopefully not a
1509 : problem. */
1510 : return true;
1511 :
1512 : default:
1513 : gcc_unreachable ();
1514 : }
1515 :
1516 3870 : strub_mode caller_mode = get_strub_mode (caller);
1517 :
1518 3870 : switch (caller_mode)
1519 : {
1520 : case STRUB_WRAPPED:
1521 : case STRUB_AT_CALLS:
1522 : case STRUB_INTERNAL:
1523 : case STRUB_INLINABLE:
1524 : case STRUB_AT_CALLS_OPT:
1525 : return true;
1526 :
1527 : case STRUB_WRAPPER:
1528 : case STRUB_DISABLED:
1529 : case STRUB_CALLABLE:
1530 : break;
1531 :
1532 : default:
1533 : gcc_unreachable ();
1534 : }
1535 :
1536 : return false;
1537 : }
1538 :
1539 : /* Check that types T1 and T2 are strub-compatible. Return 1 if the strub modes
1540 : are the same, 2 if they are interchangeable, and 0 otherwise. */
1541 :
1542 : int
1543 1579178 : strub_comptypes (tree t1, tree t2)
1544 : {
1545 1579178 : if (TREE_CODE (t1) != TREE_CODE (t2))
1546 : return 0;
1547 :
1548 1579178 : enum strub_mode m1 = get_strub_mode_from_type (t1);
1549 1579178 : enum strub_mode m2 = get_strub_mode_from_type (t2);
1550 :
1551 1579178 : if (m1 == m2)
1552 : return 1;
1553 :
1554 : /* We're dealing with types, so only strub modes that can be selected by
1555 : attributes in the front end matter. If either mode is at-calls (for
1556 : functions) or internal (for variables), the conversion is not
1557 : compatible. */
1558 4384 : bool var_p = !FUNC_OR_METHOD_TYPE_P (t1);
1559 4384 : enum strub_mode mr = var_p ? STRUB_INTERNAL : STRUB_AT_CALLS;
1560 4384 : if (m1 == mr || m2 == mr)
1561 2469 : return 0;
1562 :
1563 : return 2;
1564 : }
1565 :
1566 : /* Return the effective strub mode used for CALL, and set *TYPEP to
1567 : the effective type used for the call. The effective type and mode
1568 : are those of the callee, unless the call involves a typecast. */
1569 :
1570 : static enum strub_mode
1571 18040 : effective_strub_mode_for_call (gcall *call, tree *typep)
1572 : {
1573 18040 : tree type;
1574 18040 : enum strub_mode mode;
1575 :
1576 18040 : if (strub_call_fntype_override_p (call))
1577 : {
1578 0 : type = gimple_call_fntype (call);
1579 0 : mode = get_strub_mode_from_type (type);
1580 : }
1581 : else
1582 : {
1583 18040 : type = TREE_TYPE (TREE_TYPE (gimple_call_fn (call)));
1584 18040 : tree decl = gimple_call_fndecl (call);
1585 18040 : if (decl)
1586 17768 : mode = get_strub_mode_from_fndecl (decl);
1587 : else
1588 272 : mode = get_strub_mode_from_type (type);
1589 : }
1590 :
1591 18040 : if (typep)
1592 17878 : *typep = type;
1593 :
1594 18040 : return mode;
1595 : }
1596 :
1597 : /* Create a distinct copy of the type of NODE's function, and change
1598 : the fntype of all calls to it with the same main type to the new
1599 : type. */
1600 :
1601 : static void
1602 73 : distinctify_node_type (cgraph_node *node)
1603 : {
1604 73 : tree old_type = TREE_TYPE (node->decl);
1605 73 : tree new_type = build_distinct_type_copy (old_type);
1606 73 : tree new_ptr_type = NULL_TREE;
1607 :
1608 : /* Remap any calls to node->decl that use old_type, or a variant
1609 : thereof, to new_type as well. We don't look for aliases, their
1610 : declarations will have their types changed independently, and
1611 : we'll adjust their fntypes then. */
1612 146 : for (cgraph_edge *e = node->callers; e; e = e->next_caller)
1613 : {
1614 73 : if (!e->call_stmt)
1615 0 : continue;
1616 73 : tree fnaddr = gimple_call_fn (e->call_stmt);
1617 73 : gcc_checking_assert (TREE_CODE (fnaddr) == ADDR_EXPR
1618 : && TREE_OPERAND (fnaddr, 0) == node->decl);
1619 73 : if (strub_call_fntype_override_p (e->call_stmt))
1620 0 : continue;
1621 73 : if (!new_ptr_type)
1622 73 : new_ptr_type = build_pointer_type (new_type);
1623 73 : TREE_TYPE (fnaddr) = new_ptr_type;
1624 73 : gimple_call_set_fntype (e->call_stmt, new_type);
1625 : }
1626 :
1627 73 : TREE_TYPE (node->decl) = new_type;
1628 73 : }
1629 :
1630 : /* Return TRUE iff TYPE and any variants have the same strub mode. */
1631 :
1632 : static bool
1633 2326 : same_strub_mode_in_variants_p (tree type)
1634 : {
1635 2326 : enum strub_mode mode = get_strub_mode_from_type (type);
1636 :
1637 2326 : for (tree other = TYPE_MAIN_VARIANT (type);
1638 4710 : other != NULL_TREE; other = TYPE_NEXT_VARIANT (other))
1639 2384 : if (type != other && mode != get_strub_mode_from_type (other))
1640 : return false;
1641 :
1642 : /* Check that the canonical type, if set, either is in the same
1643 : variant chain, or has the same strub mode as type. Also check
1644 : the variants of the canonical type. */
1645 2326 : if (TYPE_CANONICAL (type)
1646 2326 : && (TYPE_MAIN_VARIANT (TYPE_CANONICAL (type))
1647 146 : != TYPE_MAIN_VARIANT (type)))
1648 : {
1649 0 : if (mode != get_strub_mode_from_type (TYPE_CANONICAL (type)))
1650 : return false;
1651 : else
1652 0 : return same_strub_mode_in_variants_p (TYPE_CANONICAL (type));
1653 : }
1654 :
1655 : return true;
1656 : }
1657 :
1658 : /* Check that strub functions don't call non-strub functions, and that
1659 : always_inline strub functions are only called by strub
1660 : functions. */
1661 :
1662 : static void
1663 646 : verify_strub ()
1664 : {
1665 646 : cgraph_node *node;
1666 :
1667 : /* It's expected that check strub-wise pointer type compatibility of variables
1668 : and of functions is already taken care of by front-ends, on account of the
1669 : attribute's being marked as affecting type identity and of the creation of
1670 : distinct types. */
1671 :
1672 : /* Check that call targets in strub contexts have strub-callable types. */
1673 :
1674 2956 : FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
1675 : {
1676 2310 : if (node->assume_function)
1677 24 : continue;
1678 :
1679 2286 : enum strub_mode caller_mode = get_strub_mode (node);
1680 :
1681 2448 : for (cgraph_edge *e = node->indirect_calls; e; e = e->next_callee)
1682 : {
1683 162 : gcc_checking_assert (e->indirect_unknown_callee);
1684 :
1685 162 : if (!e->call_stmt)
1686 0 : continue;
1687 :
1688 162 : enum strub_mode callee_mode
1689 162 : = effective_strub_mode_for_call (e->call_stmt, NULL);
1690 :
1691 162 : if (!strub_callable_from_p (caller_mode, callee_mode))
1692 28 : error_at (gimple_location (e->call_stmt),
1693 : "indirect non-%<strub%> call in %<strub%> context %qD",
1694 : node->decl);
1695 : }
1696 :
1697 11278 : for (cgraph_edge *e = node->callees; e; e = e->next_callee)
1698 : {
1699 8992 : gcc_checking_assert (!e->indirect_unknown_callee);
1700 :
1701 8992 : if (!e->call_stmt)
1702 16 : continue;
1703 :
1704 8992 : tree callee_fntype;
1705 8992 : enum strub_mode callee_mode
1706 8992 : = effective_strub_mode_for_call (e->call_stmt, &callee_fntype);
1707 :
1708 8992 : if (!strub_callable_from_p (caller_mode, callee_mode))
1709 : {
1710 815 : if (callee_mode == STRUB_INLINABLE)
1711 211 : error_at (gimple_location (e->call_stmt),
1712 : "calling %<always_inline%> %<strub%> %qD"
1713 : " in non-%<strub%> context %qD",
1714 211 : e->callee->decl, node->decl);
1715 604 : else if (fndecl_built_in_p (e->callee->decl, BUILT_IN_APPLY_ARGS)
1716 604 : && caller_mode == STRUB_INTERNAL)
1717 : /* This is ok, it will be kept in the STRUB_WRAPPER, and removed
1718 : from the STRUB_WRAPPED's strub context. */
1719 16 : continue;
1720 588 : else if (!strub_call_fntype_override_p (e->call_stmt))
1721 588 : error_at (gimple_location (e->call_stmt),
1722 : "calling non-%<strub%> %qD in %<strub%> context %qD",
1723 588 : e->callee->decl, node->decl);
1724 : else
1725 0 : error_at (gimple_location (e->call_stmt),
1726 : "calling %qD using non-%<strub%> type %qT"
1727 : " in %<strub%> context %qD",
1728 0 : e->callee->decl, callee_fntype, node->decl);
1729 : }
1730 : }
1731 : }
1732 646 : }
1733 :
1734 : namespace {
1735 :
1736 : /* Define a pass to compute strub modes. */
1737 : const pass_data pass_data_ipa_strub_mode = {
1738 : SIMPLE_IPA_PASS,
1739 : "strubm",
1740 : OPTGROUP_NONE,
1741 : TV_NONE,
1742 : PROP_cfg, // properties_required
1743 : 0, // properties_provided
1744 : 0, // properties_destroyed
1745 : 0, // properties_start
1746 : 0, // properties_finish
1747 : };
1748 :
1749 : class pass_ipa_strub_mode : public simple_ipa_opt_pass
1750 : {
1751 : public:
1752 302985 : pass_ipa_strub_mode (gcc::context *ctxt)
1753 605970 : : simple_ipa_opt_pass (pass_data_ipa_strub_mode, ctxt)
1754 : {}
1755 0 : opt_pass *clone () { return new pass_ipa_strub_mode (m_ctxt); }
1756 243276 : virtual bool gate (function *) {
1757 : /* In relaxed (-3) and strict (-4) settings, that only enable strub at a
1758 : function or variable attribute's request, the attribute handler changes
1759 : flag_strub to -1 or -2, respectively, if any strub-enabling occurrence of
1760 : the attribute is found. Therefore, if it remains at -3 or -4, nothing
1761 : that would enable strub was found, so we can disable it and avoid the
1762 : overhead. */
1763 243276 : if (flag_strub < -2)
1764 242625 : flag_strub = 0;
1765 243276 : return flag_strub;
1766 : }
1767 : virtual unsigned int execute (function *);
1768 : };
1769 :
1770 : /* Define a pass to introduce strub transformations. */
1771 : const pass_data pass_data_ipa_strub = {
1772 : SIMPLE_IPA_PASS,
1773 : "strub",
1774 : OPTGROUP_NONE,
1775 : TV_NONE,
1776 : PROP_cfg | PROP_ssa, // properties_required
1777 : 0, // properties_provided
1778 : 0, // properties_destroyed
1779 : 0, // properties_start
1780 : TODO_update_ssa
1781 : | TODO_cleanup_cfg
1782 : | TODO_rebuild_cgraph_edges, // properties_finish
1783 : };
1784 :
1785 : class pass_ipa_strub : public simple_ipa_opt_pass
1786 : {
1787 : public:
1788 302985 : pass_ipa_strub (gcc::context *ctxt)
1789 605970 : : simple_ipa_opt_pass (pass_data_ipa_strub, ctxt)
1790 : {}
1791 0 : opt_pass *clone () { return new pass_ipa_strub (m_ctxt); }
1792 243276 : virtual bool gate (function *) { return flag_strub && !seen_error (); }
1793 : virtual unsigned int execute (function *);
1794 :
1795 : /* Define on demand and cache some types we use often. */
1796 : #define DEF_TYPE(IDX, NAME, INIT) \
1797 : static inline tree get_ ## NAME () { \
1798 : int idx = STRUB_TYPE_BASE + IDX; \
1799 : static tree type = strub_cache[idx]; \
1800 : if (!type) \
1801 : strub_cache[idx] = type = (INIT); \
1802 : return type; \
1803 : }
1804 :
1805 : /* Use a distinct ptr_type_node to denote the watermark, so that we can
1806 : recognize it in arg lists and avoid modifying types twice. */
1807 2555 : DEF_TYPE (0, wmt, build_variant_type_copy (ptr_type_node))
1808 :
1809 2569 : DEF_TYPE (1, pwmt, build_reference_type (get_wmt ()))
1810 :
1811 6375 : DEF_TYPE (2, qpwmt,
1812 : build_qualified_type (get_pwmt (),
1813 : TYPE_QUAL_RESTRICT
1814 : /* | TYPE_QUAL_CONST */))
1815 :
1816 16 : DEF_TYPE (3, qptr,
1817 : build_qualified_type (ptr_type_node,
1818 : TYPE_QUAL_RESTRICT
1819 : | TYPE_QUAL_CONST))
1820 :
1821 8 : DEF_TYPE (4, qpvalst,
1822 : build_qualified_type (build_reference_type
1823 : (va_list_type_node),
1824 : TYPE_QUAL_RESTRICT
1825 : /* | TYPE_QUAL_CONST */))
1826 :
1827 : #undef DEF_TYPE
1828 :
1829 : /* Define non-strub builtins on demand. */
1830 : #define DEF_NM_BUILTIN(NAME, CODE, FNTYPELIST) \
1831 : static tree get_ ## NAME () { \
1832 : tree decl = builtin_decl_explicit (CODE); \
1833 : if (!decl) \
1834 : { \
1835 : tree type = build_function_type_list FNTYPELIST; \
1836 : decl = add_builtin_function \
1837 : ("__builtin_" #NAME, \
1838 : type, CODE, BUILT_IN_NORMAL, \
1839 : NULL, NULL); \
1840 : TREE_NOTHROW (decl) = true; \
1841 : set_builtin_decl ((CODE), decl, true); \
1842 : } \
1843 : return decl; \
1844 : }
1845 :
1846 : DEF_NM_BUILTIN (stack_address,
1847 : BUILT_IN_STACK_ADDRESS,
1848 : (ptr_type_node, NULL))
1849 :
1850 : #undef DEF_NM_BUILTIN
1851 :
1852 : /* Define strub builtins on demand. */
1853 : #define DEF_SS_BUILTIN(NAME, FNSPEC, CODE, FNTYPELIST) \
1854 : static tree get_ ## NAME () { \
1855 : tree decl = builtin_decl_explicit (CODE); \
1856 : if (!decl) \
1857 : { \
1858 : tree type = build_function_type_list FNTYPELIST; \
1859 : tree attrs = NULL; \
1860 : if (FNSPEC) \
1861 : attrs = tree_cons (get_identifier ("fn spec"), \
1862 : build_tree_list \
1863 : (NULL_TREE, \
1864 : build_string (strlen (FNSPEC), \
1865 : (FNSPEC))), \
1866 : attrs); \
1867 : decl = add_builtin_function_ext_scope \
1868 : ("__builtin___strub_" #NAME, \
1869 : type, CODE, BUILT_IN_NORMAL, \
1870 : "__strub_" #NAME, attrs); \
1871 : TREE_NOTHROW (decl) = true; \
1872 : set_builtin_decl ((CODE), decl, true); \
1873 : } \
1874 : return decl; \
1875 : }
1876 :
1877 2006 : DEF_SS_BUILTIN (enter, ". Ot",
1878 : BUILT_IN___STRUB_ENTER,
1879 : (void_type_node, get_qpwmt (), NULL))
1880 1051 : DEF_SS_BUILTIN (update, ". Wt",
1881 : BUILT_IN___STRUB_UPDATE,
1882 : (void_type_node, get_qpwmt (), NULL))
1883 2006 : DEF_SS_BUILTIN (leave, ". w ",
1884 : BUILT_IN___STRUB_LEAVE,
1885 : (void_type_node, get_qpwmt (), NULL))
1886 :
1887 : #undef DEF_SS_BUILTIN
1888 :
1889 : /* Define strub identifiers on demand. */
1890 : #define DEF_IDENT(IDX, NAME) \
1891 : static inline tree get_ ## NAME () { \
1892 : int idx = STRUB_IDENT_BASE + IDX; \
1893 : tree identifier = strub_cache[idx]; \
1894 : if (!identifier) \
1895 : strub_cache[idx] = identifier = get_identifier (".strub." #NAME); \
1896 : return identifier; \
1897 : }
1898 :
1899 861 : DEF_IDENT (0, watermark_ptr)
1900 8 : DEF_IDENT (1, va_list_ptr)
1901 16 : DEF_IDENT (2, apply_args)
1902 :
1903 : #undef DEF_IDENT
1904 :
1905 : static inline int adjust_at_calls_type (tree);
1906 : static inline void adjust_at_calls_call (cgraph_edge *, int, tree);
1907 : static inline void adjust_at_calls_calls (cgraph_node *);
1908 :
1909 : /* Add to SEQ a call to the strub watermark update builtin, taking NODE's
1910 : location if given. Optionally add the corresponding edge from NODE, with
1911 : execution frequency COUNT. Return the modified SEQ. */
1912 :
1913 : static inline gimple_seq
1914 1051 : call_update_watermark (tree wmptr, cgraph_node *node, profile_count count,
1915 : gimple_seq seq = NULL)
1916 : {
1917 1051 : tree uwm = get_update ();
1918 1051 : gcall *update = gimple_build_call (uwm, 1, wmptr);
1919 1051 : if (node)
1920 813 : gimple_set_location (update, DECL_SOURCE_LOCATION (node->decl));
1921 1051 : gimple_seq_add_stmt (&seq, update);
1922 1051 : if (node)
1923 813 : node->create_edge (cgraph_node::get_create (uwm), update, count, false);
1924 1051 : return seq;
1925 : }
1926 :
1927 : };
1928 :
1929 : } // anon namespace
1930 :
1931 : /* Gather with this type a collection of parameters that we're turning into
1932 : explicit references. */
1933 :
1934 : typedef hash_set<tree> indirect_parms_t;
1935 :
1936 : /* Dereference OP's incoming turned-into-reference parm if it's an
1937 : INDIRECT_PARMS or an ADDR_EXPR thereof. Set *REC and return according to
1938 : gimple-walking expectations. */
1939 :
1940 : static tree
1941 170 : maybe_make_indirect (indirect_parms_t &indirect_parms, tree op, int *rec)
1942 : {
1943 170 : if (DECL_P (op))
1944 : {
1945 28 : *rec = 0;
1946 28 : if (indirect_parms.contains (op))
1947 : {
1948 6 : tree ret = gimple_fold_indirect_ref (op);
1949 6 : if (!ret)
1950 12 : ret = build2 (MEM_REF,
1951 6 : TREE_TYPE (TREE_TYPE (op)),
1952 : op,
1953 6 : build_int_cst (TREE_TYPE (op), 0));
1954 6 : if (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (op)))
1955 6 : && !TREE_THIS_VOLATILE (ret))
1956 6 : TREE_SIDE_EFFECTS (ret) = TREE_THIS_VOLATILE (ret) = 1;
1957 : return ret;
1958 : }
1959 : }
1960 142 : else if (TREE_CODE (op) == ADDR_EXPR
1961 142 : && DECL_P (TREE_OPERAND (op, 0)))
1962 : {
1963 14 : *rec = 0;
1964 14 : if (indirect_parms.contains (TREE_OPERAND (op, 0)))
1965 : {
1966 0 : op = TREE_OPERAND (op, 0);
1967 0 : return op;
1968 : }
1969 : }
1970 :
1971 : return NULL_TREE;
1972 : }
1973 :
1974 : /* A gimple-walking function that adds dereferencing to indirect parms. */
1975 :
1976 : static tree
1977 170 : walk_make_indirect (tree *op, int *rec, void *arg)
1978 : {
1979 170 : walk_stmt_info *wi = (walk_stmt_info *)arg;
1980 170 : indirect_parms_t &indirect_parms = *(indirect_parms_t *)wi->info;
1981 :
1982 170 : if (!*op || TYPE_P (*op))
1983 : {
1984 0 : *rec = 0;
1985 0 : return NULL_TREE;
1986 : }
1987 :
1988 170 : if (tree repl = maybe_make_indirect (indirect_parms, *op, rec))
1989 : {
1990 6 : *op = repl;
1991 6 : wi->changed = true;
1992 : }
1993 :
1994 : return NULL_TREE;
1995 : }
1996 :
1997 : /* A gimple-walking function that turns any non-gimple-val ADDR_EXPRs into a
1998 : separate SSA. Though addresses of e.g. parameters, and of members thereof,
1999 : are gimple vals, turning parameters into references, with an extra layer of
2000 : indirection and thus explicit dereferencing, need to be regimplified. */
2001 :
2002 : static tree
2003 12 : walk_regimplify_addr_expr (tree *op, int *rec, void *arg)
2004 : {
2005 12 : walk_stmt_info *wi = (walk_stmt_info *)arg;
2006 12 : gimple_stmt_iterator &gsi = *(gimple_stmt_iterator *)wi->info;
2007 :
2008 12 : *rec = 0;
2009 :
2010 12 : if (!*op || TREE_CODE (*op) != ADDR_EXPR)
2011 : return NULL_TREE;
2012 :
2013 0 : if (!is_gimple_val (*op))
2014 : {
2015 0 : tree ret = force_gimple_operand_gsi (&gsi, *op, true,
2016 : NULL_TREE, true, GSI_SAME_STMT);
2017 0 : gcc_assert (ret != *op);
2018 0 : *op = ret;
2019 0 : wi->changed = true;
2020 : }
2021 :
2022 : return NULL_TREE;
2023 : }
2024 :
2025 : /* Turn STMT's PHI arg defs into separate SSA defs if they've become
2026 : non-gimple_val. Return TRUE if any edge insertions need to be committed. */
2027 :
2028 : static bool
2029 0 : walk_regimplify_phi (gphi *stmt)
2030 : {
2031 0 : bool needs_commit = false;
2032 :
2033 0 : for (unsigned i = 0, n = gimple_phi_num_args (stmt); i < n; i++)
2034 : {
2035 0 : tree op = gimple_phi_arg_def (stmt, i);
2036 0 : if ((TREE_CODE (op) == ADDR_EXPR
2037 0 : && !is_gimple_val (op))
2038 : /* ??? A PARM_DECL that was addressable in the original function and
2039 : had its address in PHI nodes, but that became a reference in the
2040 : wrapped clone would NOT be updated by update_ssa in PHI nodes.
2041 : Alas, if we were to create a default def for it now, update_ssa
2042 : would complain that the symbol that needed rewriting already has
2043 : SSA names associated with it. OTOH, leaving the PARM_DECL alone,
2044 : it eventually causes errors because it remains unchanged in PHI
2045 : nodes, but it gets rewritten as expected if it appears in other
2046 : stmts. So we cheat a little here, and force the PARM_DECL out of
2047 : the PHI node and into an assignment. It's a little expensive,
2048 : because we insert it at the edge, which introduces a basic block
2049 : that's entirely unnecessary, but it works, and the block will be
2050 : removed as the default def gets propagated back into the PHI node,
2051 : so the final optimized code looks just as expected. */
2052 0 : || (TREE_CODE (op) == PARM_DECL
2053 0 : && !TREE_ADDRESSABLE (op)))
2054 : {
2055 0 : tree temp = make_ssa_name (TREE_TYPE (op), stmt);
2056 0 : if (TREE_CODE (op) == PARM_DECL)
2057 0 : SET_SSA_NAME_VAR_OR_IDENTIFIER (temp, DECL_NAME (op));
2058 0 : SET_PHI_ARG_DEF (stmt, i, temp);
2059 :
2060 0 : gimple *assign = gimple_build_assign (temp, op);
2061 0 : if (gimple_phi_arg_has_location (stmt, i))
2062 0 : gimple_set_location (assign, gimple_phi_arg_location (stmt, i));
2063 0 : gsi_insert_on_edge (gimple_phi_arg_edge (stmt, i), assign);
2064 0 : needs_commit = true;
2065 : }
2066 : }
2067 :
2068 0 : return needs_commit;
2069 : }
2070 :
2071 : /* Create a reference type to use for PARM when turning it into a
2072 : reference. */
2073 :
2074 : static tree
2075 14 : build_ref_type_for (tree parm)
2076 : {
2077 14 : gcc_checking_assert (TREE_CODE (parm) == PARM_DECL);
2078 :
2079 14 : tree ref_type = build_reference_type (TREE_TYPE (parm));
2080 :
2081 14 : return ref_type;
2082 : }
2083 :
2084 : /* Add cgraph edges from current_function_decl to callees in SEQ with frequency
2085 : COUNT, assuming all calls in SEQ are direct. */
2086 :
2087 : static void
2088 2874 : add_call_edges_for_seq (gimple_seq seq, profile_count count)
2089 : {
2090 2874 : cgraph_node *node = cgraph_node::get_create (current_function_decl);
2091 :
2092 2874 : for (gimple_stmt_iterator gsi = gsi_start (seq);
2093 9200 : !gsi_end_p (gsi); gsi_next (&gsi))
2094 : {
2095 6326 : gimple *stmt = gsi_stmt (gsi);
2096 :
2097 6326 : gcall *call = dyn_cast <gcall *> (stmt);
2098 6326 : if (!call)
2099 3444 : continue;
2100 :
2101 2882 : tree callee = gimple_call_fndecl (call);
2102 2882 : gcc_checking_assert (callee);
2103 2882 : node->create_edge (cgraph_node::get_create (callee), call, count, false);
2104 : }
2105 2874 : }
2106 :
2107 : /* Insert SEQ after the call at GSI, as if the call was in a try block with SEQ
2108 : as finally, i.e., SEQ will run after the call whether it returns or
2109 : propagates an exception. This handles block splitting, EH edge and block
2110 : creation, noreturn and nothrow optimizations, and even throwing calls without
2111 : preexisting local handlers. */
2112 :
2113 : static void
2114 2648 : gsi_insert_finally_seq_after_call (gimple_stmt_iterator gsi, gimple_seq seq)
2115 : {
2116 2648 : if (!seq)
2117 : return;
2118 :
2119 2244 : gimple *stmt = gsi_stmt (gsi);
2120 :
2121 2244 : if (gimple_has_location (stmt))
2122 1762 : annotate_all_with_location (seq, gimple_location (stmt));
2123 :
2124 2244 : gcall *call = dyn_cast <gcall *> (stmt);
2125 2244 : bool noreturn_p = call && gimple_call_noreturn_p (call);
2126 2244 : int eh_lp = lookup_stmt_eh_lp (stmt);
2127 2244 : bool must_not_throw_p = eh_lp < 0;
2128 2244 : bool nothrow_p = (must_not_throw_p
2129 4488 : || (call && gimple_call_nothrow_p (call))
2130 3494 : || (eh_lp <= 0
2131 1240 : && (TREE_NOTHROW (cfun->decl)
2132 903 : || !opt_for_fn (cfun->decl, flag_exceptions))));
2133 :
2134 1613 : if (noreturn_p && nothrow_p)
2135 : return;
2136 :
2137 : /* Don't expect an EH edge if we're not to throw, or if we're not in an EH
2138 : region yet. */
2139 2243 : bool no_eh_edge_p = (nothrow_p || !eh_lp);
2140 2243 : bool must_end_bb = stmt_ends_bb_p (stmt);
2141 :
2142 2243 : edge eft = NULL, eeh = NULL;
2143 2243 : if (must_end_bb && !(noreturn_p && no_eh_edge_p))
2144 : {
2145 10 : gcc_checking_assert (gsi_one_before_end_p (gsi));
2146 :
2147 10 : edge e;
2148 10 : edge_iterator ei;
2149 30 : FOR_EACH_EDGE (e, ei, gsi_bb (gsi)->succs)
2150 : {
2151 20 : if ((e->flags & EDGE_EH))
2152 : {
2153 10 : gcc_checking_assert (!eeh);
2154 : eeh = e;
2155 : #if !CHECKING_P
2156 : if (eft || noreturn_p)
2157 : break;
2158 : #endif
2159 : }
2160 20 : if ((e->flags & EDGE_FALLTHRU))
2161 : {
2162 10 : gcc_checking_assert (!eft);
2163 : eft = e;
2164 : #if !CHECKING_P
2165 : if (eeh || no_eh_edge_p)
2166 : break;
2167 : #endif
2168 : }
2169 : }
2170 :
2171 10 : gcc_checking_assert (!(eft && (eft->flags & EDGE_FALLTHRU))
2172 : == noreturn_p);
2173 10 : gcc_checking_assert (!(eeh && (eeh->flags & EDGE_EH))
2174 : == no_eh_edge_p);
2175 10 : gcc_checking_assert (eft != eeh);
2176 : }
2177 :
2178 2243 : if (!noreturn_p)
2179 : {
2180 2243 : gimple_seq nseq = nothrow_p ? seq : gimple_seq_copy (seq);
2181 :
2182 2243 : if (must_end_bb)
2183 : {
2184 10 : gcc_checking_assert (gsi_one_before_end_p (gsi));
2185 10 : add_call_edges_for_seq (nseq, eft->count ());
2186 10 : gsi_insert_seq_on_edge_immediate (eft, nseq);
2187 : }
2188 : else
2189 : {
2190 2233 : add_call_edges_for_seq (nseq, gsi_bb (gsi)->count);
2191 2233 : gsi_insert_seq_after (&gsi, nseq, GSI_SAME_STMT);
2192 : }
2193 : }
2194 :
2195 2243 : if (nothrow_p)
2196 : return;
2197 :
2198 631 : if (eh_lp)
2199 : {
2200 10 : add_call_edges_for_seq (seq, eeh->count ());
2201 10 : gsi_insert_seq_on_edge_immediate (eeh, seq);
2202 10 : return;
2203 : }
2204 :
2205 : /* A throwing call may appear within a basic block in a function that doesn't
2206 : have any EH regions. We're going to add a cleanup if so, therefore the
2207 : block will have to be split. */
2208 621 : basic_block bb = gsi_bb (gsi);
2209 621 : if (!gsi_one_before_end_p (gsi))
2210 621 : split_block (bb, stmt);
2211 :
2212 : /* Create a new block for the EH cleanup. */
2213 621 : basic_block bb_eh_cleanup = create_empty_bb (bb);
2214 621 : if (dom_info_available_p (CDI_DOMINATORS))
2215 0 : set_immediate_dominator (CDI_DOMINATORS, bb_eh_cleanup, bb);
2216 621 : if (current_loops)
2217 621 : add_bb_to_loop (bb_eh_cleanup, current_loops->tree_root);
2218 :
2219 : /* Make the new block an EH cleanup for the call. */
2220 621 : eh_region new_r = gen_eh_region_cleanup (NULL);
2221 621 : eh_landing_pad lp = gen_eh_landing_pad (new_r);
2222 621 : tree label = gimple_block_label (bb_eh_cleanup);
2223 621 : lp->post_landing_pad = label;
2224 621 : EH_LANDING_PAD_NR (label) = lp->index;
2225 621 : add_stmt_to_eh_lp (stmt, lp->index);
2226 :
2227 : /* Add the cleanup code to the EH cleanup block. */
2228 621 : gsi = gsi_after_labels (bb_eh_cleanup);
2229 621 : gsi_insert_seq_before (&gsi, seq, GSI_SAME_STMT);
2230 :
2231 : /* And then propagate the exception further. */
2232 621 : gresx *resx = gimple_build_resx (new_r->index);
2233 621 : if (gimple_has_location (stmt))
2234 511 : gimple_set_location (resx, gimple_location (stmt));
2235 621 : gsi_insert_before (&gsi, resx, GSI_SAME_STMT);
2236 :
2237 : /* Finally, wire the EH cleanup block into the CFG. */
2238 621 : edge neeh = make_eh_edge (stmt);
2239 621 : neeh->probability = profile_probability::never ();
2240 621 : gcc_checking_assert (neeh->dest == bb_eh_cleanup);
2241 621 : gcc_checking_assert (!neeh->dest->count.initialized_p ());
2242 621 : neeh->dest->count = neeh->count ();
2243 621 : add_call_edges_for_seq (seq, neeh->dest->count);
2244 : }
2245 :
2246 : /* Copy the attribute list at *ATTRS, minus any NAME attributes, leaving
2247 : shareable trailing nodes alone. */
2248 :
2249 : static inline void
2250 0 : remove_named_attribute_unsharing (const char *name, tree *attrs)
2251 : {
2252 0 : while (tree found = lookup_attribute (name, *attrs))
2253 : {
2254 : /* Copy nodes up to the next NAME attribute. */
2255 0 : while (*attrs != found)
2256 : {
2257 0 : *attrs = tree_cons (TREE_PURPOSE (*attrs),
2258 0 : TREE_VALUE (*attrs),
2259 0 : TREE_CHAIN (*attrs));
2260 0 : attrs = &TREE_CHAIN (*attrs);
2261 : }
2262 : /* Then drop it. */
2263 0 : gcc_checking_assert (*attrs == found);
2264 0 : *attrs = TREE_CHAIN (*attrs);
2265 0 : }
2266 0 : }
2267 :
2268 : /* Record the uid of the last cgraph entry whose mode we've already set, so
2269 : that we can perform mode setting incrementally without duplication. */
2270 : static int last_cgraph_uid;
2271 :
2272 : /* Set strub modes for functions introduced since the last call. */
2273 :
2274 : static void
2275 1223 : ipa_strub_set_mode_for_new_functions ()
2276 : {
2277 1223 : if (symtab->cgraph_max_uid == last_cgraph_uid)
2278 : return;
2279 :
2280 : cgraph_node *node;
2281 :
2282 : /* Go through the functions twice, once over non-aliases, and then over
2283 : aliases, so that aliases can reuse the mode computation of their ultimate
2284 : targets. */
2285 2244 : for (int aliases = 0; aliases <= 1; aliases++)
2286 10474 : FOR_EACH_FUNCTION (node)
2287 : {
2288 8978 : if (!node->alias != !aliases)
2289 4489 : continue;
2290 :
2291 : /* Already done. */
2292 4489 : if (node->get_uid () < last_cgraph_uid)
2293 899 : continue;
2294 :
2295 3590 : set_strub_mode (node);
2296 : }
2297 :
2298 748 : last_cgraph_uid = symtab->cgraph_max_uid;
2299 : }
2300 :
2301 : /* Return FALSE if NODE is a strub context, and TRUE otherwise. */
2302 :
2303 : bool
2304 1312979 : strub_splittable_p (cgraph_node *node)
2305 : {
2306 1312979 : switch (get_strub_mode (node))
2307 : {
2308 : case STRUB_WRAPPED:
2309 : case STRUB_AT_CALLS:
2310 : case STRUB_AT_CALLS_OPT:
2311 : case STRUB_INLINABLE:
2312 : case STRUB_INTERNAL:
2313 : case STRUB_WRAPPER:
2314 : return false;
2315 :
2316 1312843 : case STRUB_CALLABLE:
2317 1312843 : case STRUB_DISABLED:
2318 1312843 : break;
2319 :
2320 : default:
2321 : gcc_unreachable ();
2322 : }
2323 :
2324 1312843 : return true;
2325 : }
2326 :
2327 : /* Return the PARM_DECL of the incoming watermark pointer, if there is one. */
2328 :
2329 : tree
2330 2008 : strub_watermark_parm (tree fndecl)
2331 : {
2332 2008 : switch (get_strub_mode_from_fndecl (fndecl))
2333 : {
2334 1073 : case STRUB_WRAPPED:
2335 1073 : case STRUB_AT_CALLS:
2336 1073 : case STRUB_AT_CALLS_OPT:
2337 1073 : break;
2338 :
2339 : case STRUB_INTERNAL:
2340 : case STRUB_WRAPPER:
2341 : case STRUB_CALLABLE:
2342 : case STRUB_DISABLED:
2343 : case STRUB_INLINABLE:
2344 : return NULL_TREE;
2345 :
2346 0 : default:
2347 0 : gcc_unreachable ();
2348 : }
2349 :
2350 1077 : for (tree parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
2351 : /* The type (variant) compare finds the parameter even in a just-created
2352 : clone, before we set its name, but the type-based compare doesn't work
2353 : during builtin expansion within the lto compiler, because we'll have
2354 : created a separate variant in that run. */
2355 1077 : if (TREE_TYPE (parm) == pass_ipa_strub::get_qpwmt ()
2356 1077 : || DECL_NAME (parm) == pass_ipa_strub::get_watermark_ptr ())
2357 : return parm;
2358 :
2359 0 : gcc_unreachable ();
2360 : }
2361 :
2362 : /* Adjust a STRUB_AT_CALLS function TYPE, adding a watermark pointer if it
2363 : hasn't been added yet. Return the named argument count. */
2364 :
2365 : int
2366 2326 : pass_ipa_strub::adjust_at_calls_type (tree type)
2367 : {
2368 2326 : int named_args = 0;
2369 :
2370 2326 : gcc_checking_assert (same_strub_mode_in_variants_p (type));
2371 :
2372 2326 : if (!TYPE_ARG_TYPES (type))
2373 : return named_args;
2374 :
2375 2326 : tree *tlist = &TYPE_ARG_TYPES (type);
2376 2326 : tree qpwmptrt = get_qpwmt ();
2377 2450 : while (*tlist && TREE_VALUE (*tlist) != void_type_node)
2378 : {
2379 : /* The type has already been adjusted. */
2380 2078 : if (TREE_VALUE (*tlist) == qpwmptrt)
2381 : return named_args;
2382 124 : named_args++;
2383 372 : *tlist = tree_cons (TREE_PURPOSE (*tlist),
2384 124 : TREE_VALUE (*tlist),
2385 124 : TREE_CHAIN (*tlist));
2386 124 : tlist = &TREE_CHAIN (*tlist);
2387 : }
2388 :
2389 : /* Add the new argument after all named arguments, so as to not mess with
2390 : attributes that reference parameters. */
2391 372 : *tlist = tree_cons (NULL_TREE, get_qpwmt (), *tlist);
2392 :
2393 : #if ATTR_FNSPEC_DECONST_WATERMARK
2394 : if (!type_already_adjusted)
2395 : {
2396 : int flags = flags_from_decl_or_type (type);
2397 : tree fnspec = lookup_attribute ("fn spec", type);
2398 :
2399 : if ((flags & (ECF_CONST | ECF_PURE | ECF_NOVOPS)) || fnspec)
2400 : {
2401 : size_t xargs = 1;
2402 : size_t curlen = 0, tgtlen = 2 + 2 * (named_args + xargs);
2403 : auto_vec<char> nspecv (tgtlen);
2404 : char *nspec = &nspecv[0]; /* It will *not* be NUL-terminated! */
2405 : if (fnspec)
2406 : {
2407 : tree fnspecstr = TREE_VALUE (TREE_VALUE (fnspec));
2408 : curlen = TREE_STRING_LENGTH (fnspecstr);
2409 : memcpy (nspec, TREE_STRING_POINTER (fnspecstr), curlen);
2410 : }
2411 : if (!curlen)
2412 : {
2413 : nspec[curlen++] = '.';
2414 : nspec[curlen++] = ((flags & ECF_CONST)
2415 : ? 'c'
2416 : : (flags & ECF_PURE)
2417 : ? 'p'
2418 : : ' ');
2419 : }
2420 : while (curlen < tgtlen - 2 * xargs)
2421 : {
2422 : nspec[curlen++] = '.';
2423 : nspec[curlen++] = ' ';
2424 : }
2425 : nspec[curlen++] = 'W';
2426 : nspec[curlen++] = 't';
2427 :
2428 : /* The type has already been copied, if needed, before adding
2429 : parameters. */
2430 : TYPE_ATTRIBUTES (type)
2431 : = tree_cons (get_identifier ("fn spec"),
2432 : build_tree_list (NULL_TREE,
2433 : build_string (tgtlen, nspec)),
2434 : TYPE_ATTRIBUTES (type));
2435 : }
2436 : }
2437 : #endif
2438 :
2439 372 : return named_args;
2440 : }
2441 :
2442 : /* Adjust a call to an at-calls call target. Create a watermark local variable
2443 : if needed, initialize it before, pass it to the callee according to the
2444 : modified at-calls interface, and release the callee's stack space after the
2445 : call, if not deferred. If the call is const or pure, arrange for the
2446 : watermark to not be assumed unused or unchanged. */
2447 :
2448 : void
2449 1928 : pass_ipa_strub::adjust_at_calls_call (cgraph_edge *e, int named_args,
2450 : tree callee_fntype)
2451 : {
2452 1928 : gcc_checking_assert (e->call_stmt);
2453 1928 : gcall *ocall = e->call_stmt;
2454 1928 : gimple_stmt_iterator gsi = gsi_for_stmt (ocall);
2455 :
2456 : /* Make sure we haven't modified this call yet. */
2457 1928 : gcc_checking_assert (!(int (gimple_call_num_args (ocall)) > named_args
2458 : && (TREE_TYPE (gimple_call_arg (ocall, named_args))
2459 : == get_pwmt ())));
2460 :
2461 1928 : tree tsup;
2462 1928 : if (!(tsup = gimple_call_fndecl (ocall)))
2463 46 : tsup = TREE_TYPE (TREE_TYPE (gimple_call_fn (ocall)));
2464 1928 : if (!strub_target_support_p (tsup, true, gimple_location (ocall)))
2465 0 : return;
2466 :
2467 : /* If we're already within a strub context, pass on the incoming watermark
2468 : pointer, and omit the enter and leave calls around the modified call, as an
2469 : optimization, or as a means to satisfy a tail-call requirement. */
2470 1928 : tree swmp = ((opt_for_fn (e->caller->decl, optimize_size)
2471 1629 : || opt_for_fn (e->caller->decl, optimize) > 2
2472 1320 : || gimple_call_must_tail_p (ocall)
2473 1320 : || (opt_for_fn (e->caller->decl, optimize) == 2
2474 542 : && gimple_call_tail_p (ocall)))
2475 2237 : ? strub_watermark_parm (e->caller->decl)
2476 : : NULL_TREE);
2477 2132 : bool omit_own_watermark = swmp;
2478 2132 : tree swm = NULL_TREE;
2479 608 : if (!omit_own_watermark)
2480 : {
2481 1524 : swm = create_tmp_var (get_wmt (), ".strub.watermark");
2482 1524 : TREE_ADDRESSABLE (swm) = true;
2483 1524 : swmp = build1 (ADDR_EXPR, get_pwmt (), swm);
2484 :
2485 : /* Initialize the watermark before the call. */
2486 1524 : tree enter = get_enter ();
2487 1524 : gcall *stptr = gimple_build_call (enter, 1,
2488 : unshare_expr (swmp));
2489 1524 : if (gimple_has_location (ocall))
2490 1524 : gimple_set_location (stptr, gimple_location (ocall));
2491 1524 : gsi_insert_before (&gsi, stptr, GSI_SAME_STMT);
2492 1524 : e->caller->create_edge (cgraph_node::get_create (enter),
2493 1524 : stptr, gsi_bb (gsi)->count, false);
2494 : }
2495 :
2496 :
2497 : /* Replace the call with one that passes the swmp argument first. */
2498 1928 : gcall *wrcall;
2499 1928 : { gcall *stmt = ocall;
2500 : // Mostly copied from gimple_call_copy_skip_args.
2501 1928 : int i = 0;
2502 1928 : int nargs = gimple_call_num_args (stmt);
2503 1928 : auto_vec<tree> vargs (MAX (nargs, named_args) + 1);
2504 1928 : gcall *new_stmt;
2505 :
2506 : /* pr71109.c calls a prototypeless function, then defines it with
2507 : additional arguments. It's ill-formed, but after it's inlined,
2508 : it somehow works out. */
2509 3924 : for (; i < named_args && i < nargs; i++)
2510 68 : vargs.quick_push (gimple_call_arg (stmt, i));
2511 1928 : for (; i < named_args; i++)
2512 0 : vargs.quick_push (null_pointer_node);
2513 :
2514 1928 : vargs.quick_push (unshare_expr (swmp));
2515 :
2516 1956 : for (; i < nargs; i++)
2517 28 : vargs.quick_push (gimple_call_arg (stmt, i));
2518 :
2519 1928 : if (gimple_call_internal_p (stmt))
2520 0 : gcc_unreachable ();
2521 : else
2522 1928 : new_stmt = gimple_build_call_vec (gimple_call_fn (stmt), vargs);
2523 1928 : gimple_call_set_fntype (new_stmt, callee_fntype);
2524 :
2525 1928 : if (gimple_call_lhs (stmt))
2526 1865 : gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
2527 :
2528 1928 : gimple_move_vops (new_stmt, stmt);
2529 :
2530 1928 : if (gimple_has_location (stmt))
2531 1928 : gimple_set_location (new_stmt, gimple_location (stmt));
2532 1928 : gimple_call_copy_flags (new_stmt, stmt);
2533 1928 : gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
2534 :
2535 1928 : gimple_set_modified (new_stmt, true);
2536 :
2537 1928 : wrcall = new_stmt;
2538 1928 : }
2539 :
2540 1928 : update_stmt (wrcall);
2541 1928 : gsi_replace (&gsi, wrcall, true);
2542 1928 : cgraph_edge::set_call_stmt (e, wrcall, false);
2543 :
2544 : /* Insert the strub code after the call. */
2545 1928 : gimple_seq seq = NULL;
2546 :
2547 : #if !ATTR_FNSPEC_DECONST_WATERMARK
2548 : /* If the call will be assumed to not modify or even read the
2549 : watermark, make it read and modified ourselves. */
2550 1928 : if ((gimple_call_flags (wrcall)
2551 1928 : & (ECF_CONST | ECF_PURE | ECF_NOVOPS)))
2552 : {
2553 60 : if (!swm)
2554 0 : swm = build2 (MEM_REF,
2555 0 : TREE_TYPE (TREE_TYPE (swmp)),
2556 : swmp,
2557 0 : build_int_cst (TREE_TYPE (swmp), 0));
2558 :
2559 60 : vec<tree, va_gc> *inputs = NULL;
2560 60 : vec<tree, va_gc> *outputs = NULL;
2561 120 : vec_safe_push (outputs,
2562 : build_tree_list
2563 60 : (build_tree_list
2564 : (NULL_TREE, build_string (2, "=m")),
2565 : unshare_expr (swm)));
2566 120 : vec_safe_push (inputs,
2567 : build_tree_list
2568 60 : (build_tree_list
2569 : (NULL_TREE, build_string (1, "m")),
2570 : unshare_expr (swm)));
2571 60 : gasm *forcemod = gimple_build_asm_vec ("", inputs, outputs,
2572 : NULL, NULL);
2573 60 : gimple_seq_add_stmt (&seq, forcemod);
2574 :
2575 : /* If the call will be assumed to not even read the watermark,
2576 : make sure it is already in memory before the call. */
2577 60 : if ((gimple_call_flags (wrcall) & ECF_CONST))
2578 : {
2579 32 : vec<tree, va_gc> *inputs = NULL;
2580 64 : vec_safe_push (inputs,
2581 : build_tree_list
2582 32 : (build_tree_list
2583 : (NULL_TREE, build_string (1, "m")),
2584 : unshare_expr (swm)));
2585 32 : gasm *force_store = gimple_build_asm_vec ("", inputs, NULL,
2586 : NULL, NULL);
2587 32 : if (gimple_has_location (wrcall))
2588 32 : gimple_set_location (force_store, gimple_location (wrcall));
2589 32 : gsi_insert_before (&gsi, force_store, GSI_SAME_STMT);
2590 : }
2591 : }
2592 : #endif
2593 :
2594 1928 : if (!omit_own_watermark)
2595 : {
2596 1524 : gcall *sleave = gimple_build_call (get_leave (), 1,
2597 : unshare_expr (swmp));
2598 1524 : gimple_seq_add_stmt (&seq, sleave);
2599 :
2600 1524 : gassign *clobber = gimple_build_assign (swm,
2601 : build_clobber
2602 1524 : (TREE_TYPE (swm)));
2603 1524 : gimple_seq_add_stmt (&seq, clobber);
2604 : }
2605 :
2606 1928 : gsi_insert_finally_seq_after_call (gsi, seq);
2607 : }
2608 :
2609 : /* Adjust all at-calls calls in NODE. */
2610 :
2611 : void
2612 1290 : pass_ipa_strub::adjust_at_calls_calls (cgraph_node *node)
2613 : {
2614 : /* Adjust unknown-callee indirect calls with STRUB_AT_CALLS types within
2615 : onode. */
2616 1290 : if (node->indirect_calls)
2617 : {
2618 110 : push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2619 220 : for (cgraph_edge *e = node->indirect_calls; e; e = e->next_callee)
2620 : {
2621 110 : gcc_checking_assert (e->indirect_unknown_callee);
2622 :
2623 110 : if (!e->call_stmt)
2624 64 : continue;
2625 :
2626 110 : tree callee_fntype;
2627 110 : enum strub_mode callee_mode
2628 110 : = effective_strub_mode_for_call (e->call_stmt, &callee_fntype);
2629 :
2630 174 : if (callee_mode != STRUB_AT_CALLS
2631 110 : && callee_mode != STRUB_AT_CALLS_OPT)
2632 64 : continue;
2633 :
2634 46 : int named_args = adjust_at_calls_type (callee_fntype);
2635 :
2636 46 : adjust_at_calls_call (e, named_args, callee_fntype);
2637 : }
2638 110 : pop_cfun ();
2639 : }
2640 :
2641 1290 : if (node->callees)
2642 : {
2643 1051 : push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2644 9827 : for (cgraph_edge *e = node->callees; e; e = e->next_callee)
2645 : {
2646 8776 : gcc_checking_assert (!e->indirect_unknown_callee);
2647 :
2648 8776 : if (!e->call_stmt)
2649 6894 : continue;
2650 :
2651 8776 : tree callee_fntype;
2652 8776 : enum strub_mode callee_mode
2653 8776 : = effective_strub_mode_for_call (e->call_stmt, &callee_fntype);
2654 :
2655 15670 : if (callee_mode != STRUB_AT_CALLS
2656 8776 : && callee_mode != STRUB_AT_CALLS_OPT)
2657 6894 : continue;
2658 :
2659 1882 : int named_args = adjust_at_calls_type (callee_fntype);
2660 :
2661 1882 : adjust_at_calls_call (e, named_args, callee_fntype);
2662 : }
2663 1051 : pop_cfun ();
2664 : }
2665 1290 : }
2666 :
2667 : /* The strubm (strub mode) pass computes a strub mode for each function in the
2668 : call graph, and checks, before any inlining, that strub callability
2669 : requirements in effect are satisfied. */
2670 :
2671 : unsigned int
2672 646 : pass_ipa_strub_mode::execute (function *)
2673 : {
2674 646 : last_cgraph_uid = 0;
2675 646 : ipa_strub_set_mode_for_new_functions ();
2676 :
2677 : /* Verify before any inlining or other transformations. */
2678 646 : verify_strub ();
2679 :
2680 646 : return 0;
2681 : }
2682 :
2683 : /* Create a strub mode pass. */
2684 :
2685 : simple_ipa_opt_pass *
2686 302985 : make_pass_ipa_strub_mode (gcc::context *ctxt)
2687 : {
2688 302985 : return new pass_ipa_strub_mode (ctxt);
2689 : }
2690 :
2691 : /* The strub pass proper adjusts types, signatures, and at-calls calls, and
2692 : splits internal-strub functions. */
2693 :
2694 : unsigned int
2695 577 : pass_ipa_strub::execute (function *)
2696 : {
2697 577 : cgraph_node *onode;
2698 :
2699 577 : ipa_strub_set_mode_for_new_functions ();
2700 :
2701 : /* First, adjust the signature of at-calls functions. We adjust types of
2702 : at-calls functions first, so that we don't modify types in place unless
2703 : strub is explicitly requested. */
2704 2951 : FOR_EACH_FUNCTION (onode)
2705 : {
2706 2374 : enum strub_mode mode = get_strub_mode (onode);
2707 :
2708 2374 : if (mode == STRUB_AT_CALLS
2709 2374 : || mode == STRUB_AT_CALLS_OPT)
2710 : {
2711 : /* Create a type variant if strubbing was not explicitly requested in
2712 : the function type. */
2713 398 : if (get_strub_mode_from_type (TREE_TYPE (onode->decl)) != mode)
2714 73 : distinctify_node_type (onode);
2715 :
2716 398 : int named_args = adjust_at_calls_type (TREE_TYPE (onode->decl));
2717 :
2718 : /* An external function explicitly declared with strub won't have a
2719 : body. Even with implicit at-calls strub, a function may have had its
2720 : body removed after we selected the mode, and then we have nothing
2721 : further to do. */
2722 398 : if (!onode->has_gimple_body_p ())
2723 67 : continue;
2724 :
2725 331 : tree *pargs = &DECL_ARGUMENTS (onode->decl);
2726 :
2727 : /* A noninterposable_alias reuses the same parm decl chain, don't add
2728 : the parm twice. */
2729 0 : bool aliased_parms = (onode->alias && *pargs
2730 331 : && DECL_CONTEXT (*pargs) != onode->decl);
2731 :
2732 0 : if (aliased_parms)
2733 0 : continue;
2734 :
2735 383 : for (int i = 0; i < named_args; i++)
2736 52 : pargs = &DECL_CHAIN (*pargs);
2737 :
2738 331 : tree wmptr = build_decl (DECL_SOURCE_LOCATION (onode->decl),
2739 : PARM_DECL,
2740 : get_watermark_ptr (),
2741 : get_qpwmt ());
2742 331 : DECL_ARTIFICIAL (wmptr) = 1;
2743 331 : DECL_ARG_TYPE (wmptr) = get_qpwmt ();
2744 331 : DECL_CONTEXT (wmptr) = onode->decl;
2745 331 : TREE_USED (wmptr) = 1;
2746 331 : DECL_CHAIN (wmptr) = *pargs;
2747 331 : *pargs = wmptr;
2748 :
2749 331 : if (onode->alias)
2750 0 : continue;
2751 :
2752 331 : cgraph_node *nnode = onode;
2753 331 : push_cfun (DECL_STRUCT_FUNCTION (nnode->decl));
2754 :
2755 331 : {
2756 331 : edge e = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun));
2757 331 : gimple_seq seq = call_update_watermark (wmptr, nnode, e->src->count);
2758 331 : gsi_insert_seq_on_edge_immediate (e, seq);
2759 : }
2760 :
2761 331 : if (DECL_STRUCT_FUNCTION (nnode->decl)->calls_alloca)
2762 : {
2763 120 : basic_block bb;
2764 657 : FOR_EACH_BB_FN (bb, cfun)
2765 1074 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
2766 4057 : !gsi_end_p (gsi); gsi_next (&gsi))
2767 : {
2768 3520 : gimple *stmt = gsi_stmt (gsi);
2769 :
2770 3520 : gcall *call = dyn_cast <gcall *> (stmt);
2771 :
2772 3520 : if (!call)
2773 2222 : continue;
2774 :
2775 1298 : if (gimple_alloca_call_p (call))
2776 : {
2777 : /* Capture stack growth. */
2778 328 : gimple_seq seq = call_update_watermark (wmptr, NULL,
2779 164 : gsi_bb (gsi)
2780 : ->count);
2781 164 : gsi_insert_finally_seq_after_call (gsi, seq);
2782 : }
2783 : }
2784 : }
2785 :
2786 331 : pop_cfun ();
2787 : }
2788 : }
2789 :
2790 3214 : FOR_EACH_FUNCTION (onode)
2791 : {
2792 2637 : if (!onode->has_gimple_body_p ())
2793 2155 : continue;
2794 :
2795 1290 : enum strub_mode mode = get_strub_mode (onode);
2796 :
2797 1290 : if (mode != STRUB_INTERNAL)
2798 : {
2799 808 : adjust_at_calls_calls (onode);
2800 808 : continue;
2801 : }
2802 :
2803 482 : bool is_stdarg = calls_builtin_va_start_p (onode);;
2804 482 : bool apply_args = calls_builtin_apply_args_p (onode);
2805 :
2806 482 : vec<ipa_adjusted_param, va_gc> *nparms = NULL;
2807 482 : unsigned j = 0;
2808 482 : {
2809 : // The following loop copied from ipa-split.c:split_function.
2810 482 : for (tree parm = DECL_ARGUMENTS (onode->decl);
2811 635 : parm; parm = DECL_CHAIN (parm), j++)
2812 : {
2813 153 : ipa_adjusted_param adj = {};
2814 153 : adj.op = IPA_PARAM_OP_COPY;
2815 153 : adj.base_index = j;
2816 153 : adj.prev_clone_index = j;
2817 153 : vec_safe_push (nparms, adj);
2818 : }
2819 :
2820 482 : if (apply_args)
2821 : {
2822 16 : ipa_adjusted_param aaadj = {};
2823 16 : aaadj.op = IPA_PARAM_OP_NEW;
2824 16 : aaadj.type = get_qptr ();
2825 16 : vec_safe_push (nparms, aaadj);
2826 : }
2827 :
2828 482 : if (is_stdarg)
2829 : {
2830 8 : ipa_adjusted_param vladj = {};
2831 8 : vladj.op = IPA_PARAM_OP_NEW;
2832 8 : vladj.type = get_qpvalst ();
2833 8 : vec_safe_push (nparms, vladj);
2834 : }
2835 :
2836 482 : ipa_adjusted_param wmadj = {};
2837 482 : wmadj.op = IPA_PARAM_OP_NEW;
2838 482 : wmadj.type = get_qpwmt ();
2839 482 : vec_safe_push (nparms, wmadj);
2840 : }
2841 482 : ipa_param_adjustments adj (nparms, -1, false);
2842 :
2843 482 : cgraph_node *nnode = onode->create_version_clone_with_body
2844 482 : (auto_vec<cgraph_edge *> (0),
2845 : NULL, &adj, NULL, NULL, "strub", NULL);
2846 :
2847 482 : if (!nnode)
2848 : {
2849 0 : error_at (DECL_SOURCE_LOCATION (onode->decl),
2850 : "failed to split %qD for %<strub%>",
2851 : onode->decl);
2852 0 : continue;
2853 : }
2854 :
2855 482 : onode->split_part = true;
2856 482 : if (onode->calls_comdat_local)
2857 0 : nnode->add_to_same_comdat_group (onode);
2858 :
2859 482 : set_strub_mode_to (onode, STRUB_WRAPPER);
2860 482 : set_strub_mode_to (nnode, STRUB_WRAPPED);
2861 :
2862 482 : adjust_at_calls_calls (nnode);
2863 :
2864 : /* Decide which of the wrapped function's parms we want to turn into
2865 : references to the argument passed to the wrapper. In general, we want to
2866 : copy small arguments, and avoid copying large ones. Variable-sized array
2867 : lengths given by other arguments, as in 20020210-1.c, would lead to
2868 : problems if passed by value, after resetting the original function and
2869 : dropping the length computation; passing them by reference works.
2870 : DECL_BY_REFERENCE is *not* a substitute for this: it involves copying
2871 : anyway, but performed at the caller. */
2872 482 : indirect_parms_t indirect_nparms (3, false);
2873 482 : unsigned adjust_ftype = 0;
2874 482 : unsigned named_args = 0;
2875 482 : for (tree parm = DECL_ARGUMENTS (onode->decl),
2876 482 : nparm = DECL_ARGUMENTS (nnode->decl),
2877 482 : nparmt = TYPE_ARG_TYPES (TREE_TYPE (nnode->decl));
2878 635 : parm;
2879 : named_args++,
2880 153 : parm = DECL_CHAIN (parm),
2881 459 : nparm = DECL_CHAIN (nparm),
2882 153 : nparmt = nparmt ? TREE_CHAIN (nparmt) : NULL_TREE)
2883 153 : if (TREE_THIS_VOLATILE (parm)
2884 301 : || !(0 /* DECL_BY_REFERENCE (narg) */
2885 148 : || is_gimple_reg_type (TREE_TYPE (nparm))
2886 9 : || VECTOR_TYPE_P (TREE_TYPE (nparm))
2887 9 : || TREE_CODE (TREE_TYPE (nparm)) == COMPLEX_TYPE
2888 9 : || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (nparm)))
2889 16 : && (tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (nparm)))
2890 8 : <= 4 * UNITS_PER_WORD))))
2891 : {
2892 : /* No point in indirecting pointer types, unless they're
2893 : volatile. Presumably they won't ever pass the size-based
2894 : test above, but check the assumption here, because
2895 : getting this wrong would mess with attribute access and
2896 : possibly others. We deal with fn spec below. */
2897 14 : gcc_checking_assert (!POINTER_TYPE_P (TREE_TYPE (nparm))
2898 : || TREE_THIS_VOLATILE (parm));
2899 :
2900 14 : indirect_nparms.add (nparm);
2901 :
2902 : /* ??? Is there any case in which it is not safe to suggest the parms
2903 : turned indirect don't alias anything else? They are distinct,
2904 : unaliased memory in the wrapper, and the wrapped can't possibly
2905 : take pointers into them because none of the pointers passed to the
2906 : wrapper can alias other incoming parameters passed by value, even
2907 : if with transparent reference, and the wrapper doesn't take any
2908 : extra parms that could point into wrapper's parms. So we can
2909 : probably drop the TREE_ADDRESSABLE and keep the TRUE. */
2910 14 : tree ref_type = build_ref_type_for (nparm);
2911 :
2912 14 : if (TREE_THIS_VOLATILE (nparm)
2913 5 : && TYPE_VOLATILE (TREE_TYPE (nparm))
2914 19 : && !TYPE_VOLATILE (ref_type))
2915 5 : TREE_SIDE_EFFECTS (nparm) = TREE_THIS_VOLATILE (nparm) = 0;
2916 14 : DECL_ARG_TYPE (nparm) = TREE_TYPE (nparm) = ref_type;
2917 14 : relayout_decl (nparm);
2918 14 : TREE_ADDRESSABLE (nparm) = 0;
2919 14 : DECL_BY_REFERENCE (nparm) = 0;
2920 14 : DECL_NOT_GIMPLE_REG_P (nparm) = 0;
2921 : /* ??? This avoids mismatches in debug info bind stmts in
2922 : e.g. a-chahan . */
2923 14 : DECL_ABSTRACT_ORIGIN (nparm) = NULL;
2924 :
2925 14 : if (nparmt)
2926 14 : adjust_ftype++;
2927 : }
2928 :
2929 : /* Also adjust the wrapped function type, if needed. */
2930 482 : if (adjust_ftype)
2931 : {
2932 14 : tree nftype = TREE_TYPE (nnode->decl);
2933 :
2934 : /* We always add at least one argument at the end of the signature, when
2935 : cloning the function, so we don't expect to need to duplicate the
2936 : type here. */
2937 14 : gcc_checking_assert (TYPE_ARG_TYPES (nftype)
2938 : != TYPE_ARG_TYPES (TREE_TYPE (onode->decl)));
2939 :
2940 : /* Check that fnspec still works for the modified function signature,
2941 : and drop it otherwise. */
2942 14 : bool drop_fnspec = false;
2943 14 : tree fnspec = lookup_attribute ("fn spec", TYPE_ATTRIBUTES (nftype));
2944 14 : attr_fnspec spec = fnspec ? attr_fnspec (fnspec) : attr_fnspec ("");
2945 :
2946 14 : unsigned retcopy;
2947 14 : if (!(fnspec && spec.returns_arg (&retcopy)))
2948 : retcopy = (unsigned) -1;
2949 :
2950 14 : unsigned i = 0;
2951 14 : for (tree nparm = DECL_ARGUMENTS (nnode->decl),
2952 14 : nparmt = TYPE_ARG_TYPES (nftype);
2953 32 : adjust_ftype > 0;
2954 18 : i++, nparm = DECL_CHAIN (nparm), nparmt = TREE_CHAIN (nparmt))
2955 18 : if (indirect_nparms.contains (nparm))
2956 : {
2957 14 : TREE_VALUE (nparmt) = TREE_TYPE (nparm);
2958 14 : adjust_ftype--;
2959 :
2960 14 : if (fnspec && !drop_fnspec)
2961 : {
2962 0 : if (i == retcopy)
2963 : drop_fnspec = true;
2964 0 : else if (spec.arg_specified_p (i))
2965 : {
2966 : /* Properties that apply to pointers only must not be
2967 : present, because we don't make pointers further
2968 : indirect. */
2969 0 : gcc_checking_assert
2970 : (!spec.arg_max_access_size_given_by_arg_p (i, NULL));
2971 0 : gcc_checking_assert (!spec.arg_copied_to_arg_p (i, NULL));
2972 :
2973 : /* Any claim of direct access only is invalidated by
2974 : adding an indirection level. */
2975 0 : if (spec.arg_direct_p (i))
2976 : drop_fnspec = true;
2977 :
2978 : /* If there's a claim the argument is not read from, the
2979 : added indirection invalidates it: if the argument is
2980 : used at all, then the pointer will necessarily be
2981 : read. */
2982 0 : if (!spec.arg_maybe_read_p (i)
2983 0 : && spec.arg_used_p (i))
2984 : drop_fnspec = true;
2985 : }
2986 : }
2987 : }
2988 :
2989 : /* ??? Maybe we could adjust it instead. Note we don't need
2990 : to mess with attribute access: pointer-typed parameters are
2991 : not modified, so they can remain unchanged. */
2992 14 : if (drop_fnspec)
2993 0 : remove_named_attribute_unsharing ("fn spec",
2994 0 : &TYPE_ATTRIBUTES (nftype));
2995 :
2996 14 : TREE_TYPE (nnode->decl) = nftype;
2997 : }
2998 :
2999 : #if ATTR_FNSPEC_DECONST_WATERMARK
3000 : {
3001 : int flags = flags_from_decl_or_type (nnode->decl);
3002 : tree fnspec = lookup_attribute ("fn spec", TREE_TYPE (nnode->decl));
3003 :
3004 : if ((flags & (ECF_CONST | ECF_PURE | ECF_NOVOPS)) || fnspec)
3005 : {
3006 : size_t xargs = 1 + int (is_stdarg) + int (apply_args);
3007 : size_t curlen = 0, tgtlen = 2 + 2 * (named_args + xargs);
3008 : auto_vec<char> nspecv (tgtlen);
3009 : char *nspec = &nspecv[0]; /* It will *not* be NUL-terminated! */
3010 : bool no_writes_p = true;
3011 : if (fnspec)
3012 : {
3013 : tree fnspecstr = TREE_VALUE (TREE_VALUE (fnspec));
3014 : curlen = TREE_STRING_LENGTH (fnspecstr);
3015 : memcpy (nspec, TREE_STRING_POINTER (fnspecstr), curlen);
3016 : if (!(flags & (ECF_CONST | ECF_PURE | ECF_NOVOPS))
3017 : && curlen >= 2
3018 : && nspec[1] != 'c' && nspec[1] != 'C'
3019 : && nspec[1] != 'p' && nspec[1] != 'P')
3020 : no_writes_p = false;
3021 : }
3022 : if (!curlen)
3023 : {
3024 : nspec[curlen++] = '.';
3025 : nspec[curlen++] = ((flags & ECF_CONST)
3026 : ? 'c'
3027 : : (flags & ECF_PURE)
3028 : ? 'p'
3029 : : ' ');
3030 : }
3031 : while (curlen < tgtlen - 2 * xargs)
3032 : {
3033 : nspec[curlen++] = '.';
3034 : nspec[curlen++] = ' ';
3035 : }
3036 :
3037 : /* These extra args are unlikely to be present in const or pure
3038 : functions. It's conceivable that a function that takes variable
3039 : arguments, or that passes its arguments on to another function,
3040 : could be const or pure, but it would not modify the arguments, and,
3041 : being pure or const, it couldn't possibly modify or even access
3042 : memory referenced by them. But it can read from these internal
3043 : data structures created by the wrapper, and from any
3044 : argument-passing memory referenced by them, so we denote the
3045 : possibility of reading from multiple levels of indirection, but
3046 : only of reading because const/pure. */
3047 : if (apply_args)
3048 : {
3049 : nspec[curlen++] = 'r';
3050 : nspec[curlen++] = ' ';
3051 : }
3052 : if (is_stdarg)
3053 : {
3054 : nspec[curlen++] = (no_writes_p ? 'r' : '.');
3055 : nspec[curlen++] = (no_writes_p ? 't' : ' ');
3056 : }
3057 :
3058 : nspec[curlen++] = 'W';
3059 : nspec[curlen++] = 't';
3060 :
3061 : /* The type has already been copied before adding parameters. */
3062 : gcc_checking_assert (TYPE_ARG_TYPES (TREE_TYPE (nnode->decl))
3063 : != TYPE_ARG_TYPES (TREE_TYPE (onode->decl)));
3064 : TYPE_ATTRIBUTES (TREE_TYPE (nnode->decl))
3065 : = tree_cons (get_identifier ("fn spec"),
3066 : build_tree_list (NULL_TREE,
3067 : build_string (tgtlen, nspec)),
3068 : TYPE_ATTRIBUTES (TREE_TYPE (nnode->decl)));
3069 : }
3070 : }
3071 : #else
3072 482 : (void) named_args;
3073 : #endif
3074 :
3075 482 : {
3076 482 : tree decl = onode->decl;
3077 482 : cgraph_node *target = nnode;
3078 :
3079 482 : { // copied from create_wrapper
3080 :
3081 : /* Preserve DECL_RESULT so we get right by reference flag. */
3082 482 : tree decl_result = DECL_RESULT (decl);
3083 :
3084 : /* Remove the function's body but keep arguments to be reused
3085 : for thunk. */
3086 482 : onode->release_body (true);
3087 482 : onode->reset (/* unlike create_wrapper: preserve_comdat_group = */true);
3088 :
3089 482 : DECL_UNINLINABLE (decl) = false;
3090 482 : DECL_RESULT (decl) = decl_result;
3091 482 : DECL_INITIAL (decl) = NULL;
3092 482 : allocate_struct_function (decl, false);
3093 482 : set_cfun (NULL);
3094 :
3095 : /* Turn alias into thunk and expand it into GIMPLE representation. */
3096 482 : onode->definition = true;
3097 :
3098 482 : thunk_info::get_create (onode);
3099 482 : onode->thunk = true;
3100 482 : onode->create_edge (target, NULL, onode->count);
3101 482 : onode->callees->can_throw_external = !TREE_NOTHROW (target->decl);
3102 :
3103 482 : tree arguments = DECL_ARGUMENTS (decl);
3104 :
3105 635 : while (arguments)
3106 : {
3107 153 : TREE_ADDRESSABLE (arguments) = false;
3108 153 : arguments = TREE_CHAIN (arguments);
3109 : }
3110 :
3111 482 : {
3112 482 : tree alias = onode->callees->callee->decl;
3113 482 : tree thunk_fndecl = decl;
3114 482 : tree a;
3115 :
3116 482 : int nxargs = 1 + is_stdarg + apply_args;
3117 :
3118 482 : { // Simplified from expand_thunk.
3119 482 : tree restype;
3120 482 : basic_block bb, then_bb, else_bb, return_bb;
3121 482 : gimple_stmt_iterator bsi;
3122 482 : int nargs = 0;
3123 482 : tree arg;
3124 482 : int i;
3125 482 : tree resdecl;
3126 482 : tree restmp = NULL;
3127 :
3128 482 : gcall *call;
3129 482 : greturn *ret;
3130 482 : bool alias_is_noreturn = TREE_THIS_VOLATILE (alias);
3131 :
3132 482 : a = DECL_ARGUMENTS (thunk_fndecl);
3133 :
3134 482 : current_function_decl = thunk_fndecl;
3135 :
3136 482 : bitmap_obstack_initialize (NULL);
3137 :
3138 : /* Build the return declaration for the function. */
3139 482 : restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
3140 482 : if (DECL_RESULT (thunk_fndecl) == NULL_TREE)
3141 : {
3142 0 : resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
3143 0 : DECL_ARTIFICIAL (resdecl) = 1;
3144 0 : DECL_IGNORED_P (resdecl) = 1;
3145 0 : DECL_CONTEXT (resdecl) = thunk_fndecl;
3146 0 : DECL_RESULT (thunk_fndecl) = resdecl;
3147 : }
3148 : else
3149 : resdecl = DECL_RESULT (thunk_fndecl);
3150 :
3151 482 : profile_count cfg_count = onode->count;
3152 482 : if (!cfg_count.initialized_p ())
3153 161 : cfg_count = profile_count::from_gcov_type (BB_FREQ_MAX).guessed_local ();
3154 :
3155 964 : bb = then_bb = else_bb = return_bb
3156 482 : = init_lowered_empty_function (thunk_fndecl, true, cfg_count);
3157 :
3158 482 : bsi = gsi_start_bb (bb);
3159 :
3160 : /* Build call to the function being thunked. */
3161 482 : if (!VOID_TYPE_P (restype)
3162 482 : && (!alias_is_noreturn
3163 0 : || TREE_ADDRESSABLE (restype)
3164 0 : || TREE_CODE (TYPE_SIZE_UNIT (restype)) != INTEGER_CST))
3165 : {
3166 366 : if (DECL_BY_REFERENCE (resdecl))
3167 : {
3168 6 : restmp = gimple_fold_indirect_ref (resdecl);
3169 6 : if (!restmp)
3170 12 : restmp = build2 (MEM_REF,
3171 6 : TREE_TYPE (TREE_TYPE (resdecl)),
3172 : resdecl,
3173 6 : build_int_cst (TREE_TYPE (resdecl), 0));
3174 : }
3175 360 : else if (aggregate_value_p (resdecl, TREE_TYPE (thunk_fndecl)))
3176 : {
3177 1 : restmp = resdecl;
3178 :
3179 1 : if (VAR_P (restmp))
3180 : {
3181 0 : add_local_decl (cfun, restmp);
3182 0 : BLOCK_VARS (DECL_INITIAL (current_function_decl))
3183 0 : = restmp;
3184 : }
3185 : }
3186 : else
3187 359 : restmp = create_tmp_reg (restype, "retval");
3188 : }
3189 :
3190 635 : for (arg = a; arg; arg = DECL_CHAIN (arg))
3191 153 : nargs++;
3192 482 : auto_vec<tree> vargs (nargs + nxargs);
3193 482 : i = 0;
3194 482 : arg = a;
3195 :
3196 482 : if (nargs)
3197 74 : for (tree nparm = DECL_ARGUMENTS (nnode->decl);
3198 227 : i < nargs;
3199 153 : i++, arg = DECL_CHAIN (arg), nparm = DECL_CHAIN (nparm))
3200 : {
3201 153 : tree save_arg = arg;
3202 :
3203 : /* Arrange to pass indirectly the parms, if we decided to do
3204 : so, and revert its type in the wrapper. */
3205 153 : if (indirect_nparms.contains (nparm))
3206 : {
3207 14 : tree ref_type = TREE_TYPE (nparm);
3208 14 : TREE_ADDRESSABLE (arg) = true;
3209 14 : arg = build1 (ADDR_EXPR, ref_type, arg);
3210 : }
3211 139 : else if (!TREE_THIS_VOLATILE (arg))
3212 139 : DECL_NOT_GIMPLE_REG_P (arg) = 0;
3213 :
3214 : /* Convert the argument back to the type used by the calling
3215 : conventions, e.g. a non-prototyped float type is passed as
3216 : double, as in 930603-1.c, and needs to be converted back to
3217 : double to be passed on unchanged to the wrapped
3218 : function. */
3219 153 : if (TREE_TYPE (nparm) != DECL_ARG_TYPE (nparm))
3220 : {
3221 0 : tree tmp = arg;
3222 : /* If ARG is e.g. volatile, we must copy and
3223 : convert in separate statements. */
3224 0 : if (!is_gimple_val (arg))
3225 : {
3226 0 : tmp = create_tmp_reg (TYPE_MAIN_VARIANT
3227 : (TREE_TYPE (arg)), "arg");
3228 0 : gimple *stmt = gimple_build_assign (tmp, arg);
3229 0 : gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
3230 : }
3231 0 : arg = fold_convert (DECL_ARG_TYPE (nparm), tmp);
3232 : }
3233 :
3234 153 : if (!is_gimple_val (arg))
3235 : {
3236 0 : tree tmp = create_tmp_reg (TYPE_MAIN_VARIANT
3237 : (TREE_TYPE (arg)), "arg");
3238 0 : gimple *stmt = gimple_build_assign (tmp, arg);
3239 0 : gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
3240 0 : arg = tmp;
3241 : }
3242 153 : vargs.quick_push (arg);
3243 153 : arg = save_arg;
3244 : }
3245 : /* These strub arguments are adjusted later. */
3246 482 : if (apply_args)
3247 16 : vargs.quick_push (null_pointer_node);
3248 482 : if (is_stdarg)
3249 8 : vargs.quick_push (null_pointer_node);
3250 482 : vargs.quick_push (null_pointer_node);
3251 482 : call = gimple_build_call_vec (build_fold_addr_expr_loc (0, alias),
3252 : vargs);
3253 482 : onode->callees->call_stmt = call;
3254 : // gimple_call_set_from_thunk (call, true);
3255 482 : if (DECL_STATIC_CHAIN (alias))
3256 : {
3257 0 : tree p = DECL_STRUCT_FUNCTION (alias)->static_chain_decl;
3258 0 : tree type = TREE_TYPE (p);
3259 0 : tree decl = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl),
3260 : PARM_DECL, create_tmp_var_name ("CHAIN"),
3261 : type);
3262 0 : DECL_ARTIFICIAL (decl) = 1;
3263 0 : DECL_IGNORED_P (decl) = 1;
3264 0 : TREE_USED (decl) = 1;
3265 0 : DECL_CONTEXT (decl) = thunk_fndecl;
3266 0 : DECL_ARG_TYPE (decl) = type;
3267 0 : TREE_READONLY (decl) = 1;
3268 :
3269 0 : struct function *sf = DECL_STRUCT_FUNCTION (thunk_fndecl);
3270 0 : sf->static_chain_decl = decl;
3271 :
3272 0 : gimple_call_set_chain (call, decl);
3273 : }
3274 :
3275 : /* Return slot optimization is always possible and in fact required to
3276 : return values with DECL_BY_REFERENCE. */
3277 482 : if (aggregate_value_p (resdecl, TREE_TYPE (thunk_fndecl))
3278 482 : && (!is_gimple_reg_type (TREE_TYPE (resdecl))
3279 7 : || DECL_BY_REFERENCE (resdecl)))
3280 6 : gimple_call_set_return_slot_opt (call, true);
3281 :
3282 482 : if (restmp)
3283 : {
3284 366 : gimple_call_set_lhs (call, restmp);
3285 366 : gcc_assert (useless_type_conversion_p (TREE_TYPE (restmp),
3286 : TREE_TYPE (TREE_TYPE (alias))));
3287 : }
3288 482 : gsi_insert_after (&bsi, call, GSI_NEW_STMT);
3289 482 : if (!alias_is_noreturn)
3290 : {
3291 : /* Build return value. */
3292 481 : if (!DECL_BY_REFERENCE (resdecl))
3293 475 : ret = gimple_build_return (restmp);
3294 : else
3295 6 : ret = gimple_build_return (resdecl);
3296 :
3297 481 : gsi_insert_after (&bsi, ret, GSI_NEW_STMT);
3298 : }
3299 : else
3300 : {
3301 1 : remove_edge (single_succ_edge (bb));
3302 : }
3303 :
3304 482 : cfun->gimple_df->in_ssa_p = true;
3305 482 : update_max_bb_count ();
3306 482 : profile_status_for_fn (cfun)
3307 482 : = cfg_count.initialized_p () && cfg_count.ipa_p ()
3308 482 : ? PROFILE_READ : PROFILE_GUESSED;
3309 : /* FIXME: C++ FE should stop setting TREE_ASM_WRITTEN on thunks. */
3310 : // TREE_ASM_WRITTEN (thunk_fndecl) = false;
3311 482 : delete_unreachable_blocks ();
3312 482 : update_ssa (TODO_update_ssa);
3313 482 : checking_verify_flow_info ();
3314 482 : free_dominance_info (CDI_DOMINATORS);
3315 :
3316 : /* Since we want to emit the thunk, we explicitly mark its name as
3317 : referenced. */
3318 482 : onode->thunk = false;
3319 482 : onode->lowered = true;
3320 482 : bitmap_obstack_release (NULL);
3321 482 : }
3322 482 : current_function_decl = NULL;
3323 482 : set_cfun (NULL);
3324 : }
3325 :
3326 482 : thunk_info::remove (onode);
3327 :
3328 : // some more of create_wrapper at the end of the next block.
3329 : }
3330 : }
3331 :
3332 482 : {
3333 482 : tree aaval = NULL_TREE;
3334 482 : tree vaptr = NULL_TREE;
3335 482 : tree wmptr = NULL_TREE;
3336 1141 : for (tree arg = DECL_ARGUMENTS (nnode->decl); arg; arg = DECL_CHAIN (arg))
3337 : {
3338 659 : aaval = vaptr;
3339 659 : vaptr = wmptr;
3340 659 : wmptr = arg;
3341 : }
3342 :
3343 482 : if (!apply_args)
3344 : aaval = NULL_TREE;
3345 : /* The trailing args are [apply_args], [va_list_ptr], and
3346 : watermark. If we don't have a va_list_ptr, the penultimate
3347 : argument is apply_args.
3348 : */
3349 16 : else if (!is_stdarg)
3350 : aaval = vaptr;
3351 :
3352 466 : if (!is_stdarg)
3353 : vaptr = NULL_TREE;
3354 :
3355 482 : DECL_NAME (wmptr) = get_watermark_ptr ();
3356 482 : DECL_ARTIFICIAL (wmptr) = 1;
3357 482 : DECL_IGNORED_P (wmptr) = 1;
3358 482 : TREE_USED (wmptr) = 1;
3359 :
3360 482 : if (is_stdarg)
3361 : {
3362 8 : DECL_NAME (vaptr) = get_va_list_ptr ();
3363 8 : DECL_ARTIFICIAL (vaptr) = 1;
3364 8 : DECL_IGNORED_P (vaptr) = 1;
3365 8 : TREE_USED (vaptr) = 1;
3366 : }
3367 :
3368 482 : if (apply_args)
3369 : {
3370 16 : DECL_NAME (aaval) = get_apply_args ();
3371 16 : DECL_ARTIFICIAL (aaval) = 1;
3372 16 : DECL_IGNORED_P (aaval) = 1;
3373 16 : TREE_USED (aaval) = 1;
3374 : }
3375 :
3376 482 : push_cfun (DECL_STRUCT_FUNCTION (nnode->decl));
3377 :
3378 482 : {
3379 482 : edge e = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun));
3380 482 : gimple_seq seq = call_update_watermark (wmptr, nnode, e->src->count);
3381 482 : gsi_insert_seq_on_edge_immediate (e, seq);
3382 : }
3383 :
3384 482 : bool any_indirect = !indirect_nparms.is_empty ();
3385 :
3386 482 : if (any_indirect)
3387 : {
3388 14 : basic_block bb;
3389 14 : bool needs_commit = false;
3390 42 : FOR_EACH_BB_FN (bb, cfun)
3391 : {
3392 28 : for (gphi_iterator gsi = gsi_start_nonvirtual_phis (bb);
3393 28 : !gsi_end_p (gsi);
3394 0 : gsi_next_nonvirtual_phi (&gsi))
3395 : {
3396 0 : gphi *stmt = gsi.phi ();
3397 :
3398 0 : walk_stmt_info wi = {};
3399 0 : wi.info = &indirect_nparms;
3400 0 : walk_gimple_op (stmt, walk_make_indirect, &wi);
3401 0 : if (wi.changed && !is_gimple_debug (gsi_stmt (gsi)))
3402 0 : if (walk_regimplify_phi (stmt))
3403 0 : needs_commit = true;
3404 : }
3405 :
3406 56 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
3407 115 : !gsi_end_p (gsi); gsi_next (&gsi))
3408 : {
3409 87 : gimple *stmt = gsi_stmt (gsi);
3410 :
3411 87 : walk_stmt_info wi = {};
3412 87 : wi.info = &indirect_nparms;
3413 87 : walk_gimple_op (stmt, walk_make_indirect, &wi);
3414 87 : if (wi.changed)
3415 : {
3416 6 : if (!is_gimple_debug (stmt))
3417 : {
3418 6 : wi.info = &gsi;
3419 6 : walk_gimple_op (stmt, walk_regimplify_addr_expr,
3420 : &wi);
3421 : }
3422 6 : update_stmt (stmt);
3423 : }
3424 : }
3425 : }
3426 14 : if (needs_commit)
3427 0 : gsi_commit_edge_inserts ();
3428 : }
3429 :
3430 482 : if (DECL_STRUCT_FUNCTION (nnode->decl)->calls_alloca
3431 482 : || is_stdarg || apply_args)
3432 618 : for (cgraph_edge *e = nnode->callees, *enext; e; e = enext)
3433 : {
3434 534 : if (!e->call_stmt)
3435 0 : continue;
3436 :
3437 534 : gcall *call = e->call_stmt;
3438 534 : gimple_stmt_iterator gsi = gsi_for_stmt (call);
3439 534 : tree fndecl = e->callee->decl;
3440 :
3441 534 : enext = e->next_callee;
3442 :
3443 534 : if (gimple_alloca_call_p (call))
3444 : {
3445 74 : gimple_seq seq = call_update_watermark (wmptr, NULL,
3446 74 : gsi_bb (gsi)->count);
3447 74 : gsi_insert_finally_seq_after_call (gsi, seq);
3448 : }
3449 460 : else if (fndecl && is_stdarg
3450 460 : && fndecl_built_in_p (fndecl, BUILT_IN_VA_START))
3451 : {
3452 : /* Using a non-default stdarg ABI makes the function ineligible
3453 : for internal strub. */
3454 8 : gcc_checking_assert (builtin_decl_explicit (BUILT_IN_VA_START)
3455 : == fndecl);
3456 8 : tree bvacopy = builtin_decl_explicit (BUILT_IN_VA_COPY);
3457 8 : gimple_call_set_fndecl (call, bvacopy);
3458 8 : tree arg = vaptr;
3459 : /* The va_copy source must be dereferenced, unless it's an array
3460 : type, that would have decayed to a pointer. */
3461 8 : if (TREE_CODE (TREE_TYPE (TREE_TYPE (vaptr))) != ARRAY_TYPE)
3462 : {
3463 0 : arg = gimple_fold_indirect_ref (vaptr);
3464 0 : if (!arg)
3465 0 : arg = build2 (MEM_REF,
3466 0 : TREE_TYPE (TREE_TYPE (vaptr)),
3467 : vaptr,
3468 0 : build_int_cst (TREE_TYPE (vaptr), 0));
3469 0 : if (!is_gimple_val (arg))
3470 0 : arg = force_gimple_operand_gsi (&gsi, arg, true,
3471 : NULL_TREE, true, GSI_SAME_STMT);
3472 : }
3473 8 : gimple_call_set_arg (call, 1, arg);
3474 8 : update_stmt (call);
3475 8 : e->redirect_callee (cgraph_node::get_create (bvacopy));
3476 : }
3477 452 : else if (fndecl && apply_args
3478 452 : && fndecl_built_in_p (fndecl, BUILT_IN_APPLY_ARGS))
3479 : {
3480 16 : tree lhs = gimple_call_lhs (call);
3481 16 : gimple *assign = (lhs
3482 16 : ? gimple_build_assign (lhs, aaval)
3483 12 : : gimple_build_nop ());
3484 16 : gsi_replace (&gsi, assign, true);
3485 16 : cgraph_edge::remove (e);
3486 : }
3487 : }
3488 :
3489 482 : { // a little more copied from create_wrapper
3490 :
3491 : /* Inline summary set-up. */
3492 482 : nnode->analyze ();
3493 : // inline_analyze_function (nnode);
3494 : }
3495 :
3496 482 : pop_cfun ();
3497 : }
3498 :
3499 482 : {
3500 482 : push_cfun (DECL_STRUCT_FUNCTION (onode->decl));
3501 482 : gimple_stmt_iterator gsi
3502 482 : = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
3503 :
3504 482 : gcall *wrcall;
3505 482 : while (!(wrcall = dyn_cast <gcall *> (gsi_stmt (gsi))))
3506 0 : gsi_next (&gsi);
3507 :
3508 482 : tree swm = create_tmp_var (get_wmt (), ".strub.watermark");
3509 482 : TREE_ADDRESSABLE (swm) = true;
3510 482 : tree swmp = build1 (ADDR_EXPR, get_pwmt (), swm);
3511 :
3512 482 : tree enter = get_enter ();
3513 482 : gcall *stptr = gimple_build_call (enter, 1, unshare_expr (swmp));
3514 482 : gimple_set_location (stptr, gimple_location (wrcall));
3515 482 : gsi_insert_before (&gsi, stptr, GSI_SAME_STMT);
3516 482 : onode->create_edge (cgraph_node::get_create (enter),
3517 482 : stptr, gsi_bb (gsi)->count, false);
3518 :
3519 482 : int nargs = gimple_call_num_args (wrcall);
3520 :
3521 482 : gimple_seq seq = NULL;
3522 :
3523 482 : if (apply_args)
3524 : {
3525 16 : tree aalst = create_tmp_var (ptr_type_node, ".strub.apply_args");
3526 16 : tree bappargs = builtin_decl_explicit (BUILT_IN_APPLY_ARGS);
3527 16 : gcall *appargs = gimple_build_call (bappargs, 0);
3528 16 : gimple_call_set_lhs (appargs, aalst);
3529 16 : gimple_set_location (appargs, gimple_location (wrcall));
3530 16 : gsi_insert_before (&gsi, appargs, GSI_SAME_STMT);
3531 16 : gimple_call_set_arg (wrcall, nargs - 2 - is_stdarg, aalst);
3532 16 : onode->create_edge (cgraph_node::get_create (bappargs),
3533 16 : appargs, gsi_bb (gsi)->count, false);
3534 : }
3535 :
3536 482 : if (is_stdarg)
3537 : {
3538 8 : tree valst = create_tmp_var (va_list_type_node, ".strub.va_list");
3539 8 : TREE_ADDRESSABLE (valst) = true;
3540 8 : tree vaptr = build1 (ADDR_EXPR,
3541 : build_pointer_type (va_list_type_node),
3542 : valst);
3543 8 : gimple_call_set_arg (wrcall, nargs - 2, unshare_expr (vaptr));
3544 :
3545 8 : tree bvastart = builtin_decl_explicit (BUILT_IN_VA_START);
3546 8 : gcall *vastart = gimple_build_call (bvastart, 2,
3547 : unshare_expr (vaptr),
3548 : integer_zero_node);
3549 8 : gimple_set_location (vastart, gimple_location (wrcall));
3550 8 : gsi_insert_before (&gsi, vastart, GSI_SAME_STMT);
3551 8 : onode->create_edge (cgraph_node::get_create (bvastart),
3552 8 : vastart, gsi_bb (gsi)->count, false);
3553 :
3554 8 : tree bvaend = builtin_decl_explicit (BUILT_IN_VA_END);
3555 8 : gcall *vaend = gimple_build_call (bvaend, 1, unshare_expr (vaptr));
3556 8 : gimple_set_location (vaend, gimple_location (wrcall));
3557 8 : gimple_seq_add_stmt (&seq, vaend);
3558 : }
3559 :
3560 482 : gimple_call_set_arg (wrcall, nargs - 1, unshare_expr (swmp));
3561 : // gimple_call_set_tail (wrcall, false);
3562 482 : update_stmt (wrcall);
3563 :
3564 482 : {
3565 : #if !ATTR_FNSPEC_DECONST_WATERMARK
3566 : /* If the call will be assumed to not modify or even read the
3567 : watermark, make it read and modified ourselves. */
3568 482 : if ((gimple_call_flags (wrcall)
3569 482 : & (ECF_CONST | ECF_PURE | ECF_NOVOPS)))
3570 : {
3571 127 : vec<tree, va_gc> *inputs = NULL;
3572 127 : vec<tree, va_gc> *outputs = NULL;
3573 254 : vec_safe_push (outputs,
3574 : build_tree_list
3575 127 : (build_tree_list
3576 : (NULL_TREE, build_string (2, "=m")),
3577 : swm));
3578 254 : vec_safe_push (inputs,
3579 : build_tree_list
3580 127 : (build_tree_list
3581 : (NULL_TREE, build_string (1, "m")),
3582 : swm));
3583 127 : gasm *forcemod = gimple_build_asm_vec ("", inputs, outputs,
3584 : NULL, NULL);
3585 127 : gimple_seq_add_stmt (&seq, forcemod);
3586 :
3587 : /* If the call will be assumed to not even read the watermark,
3588 : make sure it is already in memory before the call. */
3589 127 : if ((gimple_call_flags (wrcall) & ECF_CONST))
3590 : {
3591 43 : vec<tree, va_gc> *inputs = NULL;
3592 86 : vec_safe_push (inputs,
3593 : build_tree_list
3594 43 : (build_tree_list
3595 : (NULL_TREE, build_string (1, "m")),
3596 : swm));
3597 43 : gasm *force_store = gimple_build_asm_vec ("", inputs, NULL,
3598 : NULL, NULL);
3599 43 : gimple_set_location (force_store, gimple_location (wrcall));
3600 43 : gsi_insert_before (&gsi, force_store, GSI_SAME_STMT);
3601 : }
3602 : }
3603 : #endif
3604 :
3605 482 : gcall *sleave = gimple_build_call (get_leave (), 1,
3606 : unshare_expr (swmp));
3607 482 : gimple_seq_add_stmt (&seq, sleave);
3608 :
3609 482 : gassign *clobber = gimple_build_assign (swm,
3610 : build_clobber
3611 482 : (TREE_TYPE (swm)));
3612 482 : gimple_seq_add_stmt (&seq, clobber);
3613 : }
3614 :
3615 482 : gsi_insert_finally_seq_after_call (gsi, seq);
3616 :
3617 : /* For nnode, we don't rebuild edges because we wish to retain
3618 : any redirections copied to it from earlier passes, so we add
3619 : call graph edges explicitly there, but for onode, we create a
3620 : fresh function, so we may as well just issue the calls and
3621 : then rebuild all cgraph edges. */
3622 : // cgraph_edge::rebuild_edges ();
3623 482 : onode->analyze ();
3624 : // inline_analyze_function (onode);
3625 :
3626 482 : pop_cfun ();
3627 : }
3628 482 : }
3629 :
3630 577 : return 0;
3631 : }
3632 :
3633 : simple_ipa_opt_pass *
3634 302985 : make_pass_ipa_strub (gcc::context *ctxt)
3635 : {
3636 302985 : return new pass_ipa_strub (ctxt);
3637 : }
3638 :
3639 : #include "gt-ipa-strub.h"
|