Branch data Line data Source code
1 : : /* A pass for lowering trees to RTL.
2 : : Copyright (C) 2004-2025 Free Software Foundation, Inc.
3 : :
4 : : This file is part of GCC.
5 : :
6 : : GCC is free software; you can redistribute it and/or modify
7 : : it under the terms of the GNU General Public License as published by
8 : : the Free Software Foundation; either version 3, or (at your option)
9 : : any later version.
10 : :
11 : : GCC is distributed in the hope that it will be useful,
12 : : but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : : GNU General Public License for more details.
15 : :
16 : : You should have received a copy of the GNU General Public License
17 : : along with GCC; see the file COPYING3. If not see
18 : : <http://www.gnu.org/licenses/>. */
19 : :
20 : : #include "config.h"
21 : : #include "system.h"
22 : : #include "coretypes.h"
23 : : #include "backend.h"
24 : : #include "target.h"
25 : : #include "rtl.h"
26 : : #include "tree.h"
27 : : #include "gimple.h"
28 : : #include "cfghooks.h"
29 : : #include "tree-pass.h"
30 : : #include "memmodel.h"
31 : : #include "tm_p.h"
32 : : #include "ssa.h"
33 : : #include "optabs.h"
34 : : #include "regs.h" /* For reg_renumber. */
35 : : #include "emit-rtl.h"
36 : : #include "recog.h"
37 : : #include "cgraph.h"
38 : : #include "diagnostic.h"
39 : : #include "fold-const.h"
40 : : #include "varasm.h"
41 : : #include "stor-layout.h"
42 : : #include "stmt.h"
43 : : #include "print-tree.h"
44 : : #include "cfgrtl.h"
45 : : #include "cfganal.h"
46 : : #include "cfgbuild.h"
47 : : #include "cfgcleanup.h"
48 : : #include "dojump.h"
49 : : #include "explow.h"
50 : : #include "calls.h"
51 : : #include "expr.h"
52 : : #include "internal-fn.h"
53 : : #include "tree-eh.h"
54 : : #include "gimple-iterator.h"
55 : : #include "gimple-expr.h"
56 : : #include "gimple-walk.h"
57 : : #include "tree-cfg.h"
58 : : #include "tree-dfa.h"
59 : : #include "tree-ssa.h"
60 : : #include "except.h"
61 : : #include "gimple-pretty-print.h"
62 : : #include "toplev.h"
63 : : #include "debug.h"
64 : : #include "tree-inline.h"
65 : : #include "value-prof.h"
66 : : #include "tree-ssa-live.h"
67 : : #include "tree-outof-ssa.h"
68 : : #include "cfgloop.h"
69 : : #include "insn-attr.h" /* For INSN_SCHEDULING. */
70 : : #include "stringpool.h"
71 : : #include "attribs.h"
72 : : #include "asan.h"
73 : : #include "tree-ssa-address.h"
74 : : #include "output.h"
75 : : #include "builtins.h"
76 : : #include "opts.h"
77 : :
78 : : /* Some systems use __main in a way incompatible with its use in gcc, in these
79 : : cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
80 : : give the same symbol without quotes for an alternative entry point. You
81 : : must define both, or neither. */
82 : : #ifndef NAME__MAIN
83 : : #define NAME__MAIN "__main"
84 : : #endif
85 : :
86 : : /* This variable holds information helping the rewriting of SSA trees
87 : : into RTL. */
88 : : struct ssaexpand SA;
89 : :
90 : : /* This variable holds the currently expanded gimple statement for purposes
91 : : of comminucating the profile info to the builtin expanders. */
92 : : gimple *currently_expanding_gimple_stmt;
93 : :
94 : : static rtx expand_debug_expr (tree);
95 : :
96 : : static bool defer_stack_allocation (tree, bool);
97 : :
98 : : static void record_alignment_for_reg_var (unsigned int);
99 : :
100 : : /* Return an expression tree corresponding to the RHS of GIMPLE
101 : : statement STMT. */
102 : :
103 : : tree
104 : 2833842 : gimple_assign_rhs_to_tree (gimple *stmt)
105 : : {
106 : 2833842 : tree t;
107 : 2833842 : switch (gimple_assign_rhs_class (stmt))
108 : : {
109 : 604 : case GIMPLE_TERNARY_RHS:
110 : 604 : t = build3 (gimple_assign_rhs_code (stmt),
111 : 604 : TREE_TYPE (gimple_assign_lhs (stmt)),
112 : : gimple_assign_rhs1 (stmt), gimple_assign_rhs2 (stmt),
113 : : gimple_assign_rhs3 (stmt));
114 : 604 : break;
115 : 975167 : case GIMPLE_BINARY_RHS:
116 : 975167 : t = build2 (gimple_assign_rhs_code (stmt),
117 : 975167 : TREE_TYPE (gimple_assign_lhs (stmt)),
118 : : gimple_assign_rhs1 (stmt), gimple_assign_rhs2 (stmt));
119 : 975167 : break;
120 : 316575 : case GIMPLE_UNARY_RHS:
121 : 316575 : t = build1 (gimple_assign_rhs_code (stmt),
122 : 316575 : TREE_TYPE (gimple_assign_lhs (stmt)),
123 : : gimple_assign_rhs1 (stmt));
124 : 316575 : break;
125 : 1541496 : case GIMPLE_SINGLE_RHS:
126 : 1541496 : {
127 : 1541496 : t = gimple_assign_rhs1 (stmt);
128 : : /* Avoid modifying this tree in place below. */
129 : 3014820 : if ((gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t)
130 : 1434235 : && gimple_location (stmt) != EXPR_LOCATION (t))
131 : 1682676 : || (gimple_block (stmt) && currently_expanding_to_rtl
132 : 40812 : && EXPR_P (t)))
133 : 1359220 : t = copy_node (t);
134 : : break;
135 : : }
136 : 0 : default:
137 : 0 : gcc_unreachable ();
138 : : }
139 : :
140 : 2833842 : if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t))
141 : 2655311 : SET_EXPR_LOCATION (t, gimple_location (stmt));
142 : :
143 : 2833842 : return t;
144 : : }
145 : :
146 : :
147 : : #ifndef STACK_ALIGNMENT_NEEDED
148 : : #define STACK_ALIGNMENT_NEEDED 1
149 : : #endif
150 : :
151 : : #define SSAVAR(x) (TREE_CODE (x) == SSA_NAME ? SSA_NAME_VAR (x) : x)
152 : :
153 : : /* Choose either CUR or NEXT as the leader DECL for a partition.
154 : : Prefer ignored decls, to simplify debug dumps and reduce ambiguity
155 : : out of the same user variable being in multiple partitions (this is
156 : : less likely for compiler-introduced temps). */
157 : :
158 : : static tree
159 : 57319278 : leader_merge (tree cur, tree next)
160 : : {
161 : 57319278 : if (cur == NULL || cur == next)
162 : : return next;
163 : :
164 : 3017355 : if (DECL_P (cur) && DECL_IGNORED_P (cur))
165 : : return cur;
166 : :
167 : 2228589 : if (DECL_P (next) && DECL_IGNORED_P (next))
168 : 153961 : return next;
169 : :
170 : : return cur;
171 : : }
172 : :
173 : : /* Associate declaration T with storage space X. If T is no
174 : : SSA name this is exactly SET_DECL_RTL, otherwise make the
175 : : partition of T associated with X. */
176 : : static inline void
177 : 67610518 : set_rtl (tree t, rtx x)
178 : : {
179 : 67610518 : gcc_checking_assert (!x
180 : : || !(TREE_CODE (t) == SSA_NAME || is_gimple_reg (t))
181 : : || (use_register_for_decl (t)
182 : : ? (REG_P (x)
183 : : || (GET_CODE (x) == CONCAT
184 : : && (REG_P (XEXP (x, 0))
185 : : || SUBREG_P (XEXP (x, 0)))
186 : : && (REG_P (XEXP (x, 1))
187 : : || SUBREG_P (XEXP (x, 1))))
188 : : /* We need to accept PARALLELs for RESUT_DECLs
189 : : because of vector types with BLKmode returned
190 : : in multiple registers, but they are supposed
191 : : to be uncoalesced. */
192 : : || (GET_CODE (x) == PARALLEL
193 : : && SSAVAR (t)
194 : : && TREE_CODE (SSAVAR (t)) == RESULT_DECL
195 : : && (GET_MODE (x) == BLKmode
196 : : || !flag_tree_coalesce_vars)))
197 : : : (MEM_P (x) || x == pc_rtx
198 : : || (GET_CODE (x) == CONCAT
199 : : && MEM_P (XEXP (x, 0))
200 : : && MEM_P (XEXP (x, 1))))));
201 : : /* Check that the RTL for SSA_NAMEs and gimple-reg PARM_DECLs and
202 : : RESULT_DECLs has the expected mode. For memory, we accept
203 : : unpromoted modes, since that's what we're likely to get. For
204 : : PARM_DECLs and RESULT_DECLs, we'll have been called by
205 : : set_parm_rtl, which will give us the default def, so we don't
206 : : have to compute it ourselves. For RESULT_DECLs, we accept mode
207 : : mismatches too, as long as we have BLKmode or are not coalescing
208 : : across variables, so that we don't reject BLKmode PARALLELs or
209 : : unpromoted REGs. */
210 : 62698917 : gcc_checking_assert (!x || x == pc_rtx || TREE_CODE (t) != SSA_NAME
211 : : || (SSAVAR (t)
212 : : && TREE_CODE (SSAVAR (t)) == RESULT_DECL
213 : : && (promote_ssa_mode (t, NULL) == BLKmode
214 : : || !flag_tree_coalesce_vars))
215 : : || !use_register_for_decl (t)
216 : : || GET_MODE (x) == promote_ssa_mode (t, NULL));
217 : :
218 : : if (x)
219 : : {
220 : 63086311 : bool skip = false;
221 : 63086311 : tree cur = NULL_TREE;
222 : : rtx xm = x;
223 : :
224 : 63086311 : retry:
225 : 63086311 : if (MEM_P (xm))
226 : 4647781 : cur = MEM_EXPR (xm);
227 : : else if (REG_P (xm))
228 : 52671497 : cur = REG_EXPR (xm);
229 : : else if (SUBREG_P (xm))
230 : : {
231 : 0 : gcc_assert (subreg_lowpart_p (xm));
232 : 0 : xm = SUBREG_REG (xm);
233 : 0 : goto retry;
234 : : }
235 : : else if (GET_CODE (xm) == CONCAT)
236 : : {
237 : 384290 : xm = XEXP (xm, 0);
238 : 384290 : goto retry;
239 : : }
240 : : else if (GET_CODE (xm) == PARALLEL)
241 : : {
242 : 3104 : xm = XVECEXP (xm, 0, 0);
243 : 3104 : gcc_assert (GET_CODE (xm) == EXPR_LIST);
244 : 3104 : xm = XEXP (xm, 0);
245 : 3104 : goto retry;
246 : : }
247 : 5379639 : else if (xm == pc_rtx)
248 : : skip = true;
249 : : else
250 : 0 : gcc_unreachable ();
251 : :
252 : 57319278 : tree next = skip ? cur : leader_merge (cur, SSAVAR (t) ? SSAVAR (t) : t);
253 : :
254 : 62698917 : if (cur != next)
255 : : {
256 : 26444816 : if (MEM_P (x))
257 : 3856876 : set_mem_attributes (x,
258 : 1928438 : next && TREE_CODE (next) == SSA_NAME
259 : 12527 : ? TREE_TYPE (next)
260 : : : next, true);
261 : : else
262 : 24516378 : set_reg_attrs_for_decl_rtl (next, x);
263 : : }
264 : : }
265 : :
266 : 67610518 : if (TREE_CODE (t) == SSA_NAME)
267 : : {
268 : 54903362 : int part = var_to_partition (SA.map, t);
269 : 54903362 : if (part != NO_PARTITION)
270 : : {
271 : 54903362 : if (SA.partition_to_pseudo[part])
272 : 30001728 : gcc_assert (SA.partition_to_pseudo[part] == x);
273 : 24901634 : else if (x != pc_rtx)
274 : 24898797 : SA.partition_to_pseudo[part] = x;
275 : : }
276 : : /* For the benefit of debug information at -O0 (where
277 : : vartracking doesn't run) record the place also in the base
278 : : DECL. For PARMs and RESULTs, do so only when setting the
279 : : default def. */
280 : 109803887 : if (x && x != pc_rtx && SSA_NAME_VAR (t)
281 : 72135135 : && (VAR_P (SSA_NAME_VAR (t))
282 : 7270004 : || SSA_NAME_IS_DEFAULT_DEF (t)))
283 : : {
284 : 17055833 : tree var = SSA_NAME_VAR (t);
285 : : /* If we don't yet have something recorded, just record it now. */
286 : 17055833 : if (!DECL_RTL_SET_P (var))
287 : 7243461 : SET_DECL_RTL (var, x);
288 : : /* If we have it set already to "multiple places" don't
289 : : change this. */
290 : 9812372 : else if (DECL_RTL (var) == pc_rtx)
291 : : ;
292 : : /* If we have something recorded and it's not the same place
293 : : as we want to record now, we have multiple partitions for the
294 : : same base variable, with different places. We can't just
295 : : randomly chose one, hence we have to say that we don't know.
296 : : This only happens with optimization, and there var-tracking
297 : : will figure out the right thing. */
298 : 8800989 : else if (DECL_RTL (var) != x)
299 : 206396 : SET_DECL_RTL (var, pc_rtx);
300 : : }
301 : : }
302 : : else
303 : 12707156 : SET_DECL_RTL (t, x);
304 : 67610518 : }
305 : :
306 : : /* This structure holds data relevant to one variable that will be
307 : : placed in a stack slot. */
308 : : class stack_var
309 : : {
310 : : public:
311 : : /* The Variable. */
312 : : tree decl;
313 : :
314 : : /* Initially, the size of the variable. Later, the size of the partition,
315 : : if this variable becomes it's partition's representative. */
316 : : poly_uint64 size;
317 : :
318 : : /* The *byte* alignment required for this variable. Or as, with the
319 : : size, the alignment for this partition. */
320 : : unsigned int alignb;
321 : :
322 : : /* The partition representative. */
323 : : unsigned representative;
324 : :
325 : : /* The next stack variable in the partition, or EOC. */
326 : : unsigned next;
327 : :
328 : : /* The numbers of conflicting stack variables. */
329 : : bitmap conflicts;
330 : : };
331 : :
332 : : #define EOC ((unsigned)-1)
333 : :
334 : : /* We have an array of such objects while deciding allocation. */
335 : : static class stack_var *stack_vars;
336 : : static unsigned stack_vars_alloc;
337 : : static unsigned stack_vars_num;
338 : : static hash_map<tree, unsigned> *decl_to_stack_part;
339 : :
340 : : #define INVALID_STACK_INDEX ((unsigned)-1)
341 : :
342 : : /* Conflict bitmaps go on this obstack. This allows us to destroy
343 : : all of them in one big sweep. */
344 : : static bitmap_obstack stack_var_bitmap_obstack;
345 : :
346 : : /* An array of indices such that stack_vars[stack_vars_sorted[i]].size
347 : : is non-decreasing. */
348 : : static unsigned *stack_vars_sorted;
349 : :
350 : : /* The phase of the stack frame. This is the known misalignment of
351 : : virtual_stack_vars_rtx from PREFERRED_STACK_BOUNDARY. That is,
352 : : (frame_offset+frame_phase) % PREFERRED_STACK_BOUNDARY == 0. */
353 : : static int frame_phase;
354 : :
355 : : /* Used during expand_used_vars to remember if we saw any decls for
356 : : which we'd like to enable stack smashing protection. */
357 : : static bool has_protected_decls;
358 : :
359 : : /* Used during expand_used_vars. Remember if we say a character buffer
360 : : smaller than our cutoff threshold. Used for -Wstack-protector. */
361 : : static bool has_short_buffer;
362 : :
363 : : /* Compute the byte alignment to use for DECL. Ignore alignment
364 : : we can't do with expected alignment of the stack boundary. */
365 : :
366 : : static unsigned int
367 : 6166070 : align_local_variable (tree decl, bool really_expand)
368 : : {
369 : 6166070 : unsigned int align;
370 : :
371 : 6166070 : if (TREE_CODE (decl) == SSA_NAME)
372 : : {
373 : 443659 : tree type = TREE_TYPE (decl);
374 : 443659 : machine_mode mode = TYPE_MODE (type);
375 : :
376 : 443659 : align = TYPE_ALIGN (type);
377 : 443659 : if (mode != BLKmode
378 : 443659 : && align < GET_MODE_ALIGNMENT (mode))
379 : 454 : align = GET_MODE_ALIGNMENT (mode);
380 : : }
381 : : else
382 : 5722411 : align = LOCAL_DECL_ALIGNMENT (decl);
383 : :
384 : 6166070 : if (hwasan_sanitize_stack_p ())
385 : 269 : align = MAX (align, (unsigned) HWASAN_TAG_GRANULE_SIZE * BITS_PER_UNIT);
386 : :
387 : 6166070 : if (TREE_CODE (decl) != SSA_NAME && really_expand)
388 : : /* Don't change DECL_ALIGN when called from estimated_stack_frame_size.
389 : : That is done before IPA and could bump alignment based on host
390 : : backend even for offloaded code which wants different
391 : : LOCAL_DECL_ALIGNMENT. */
392 : 1476344 : SET_DECL_ALIGN (decl, align);
393 : :
394 : 6166070 : return align / BITS_PER_UNIT;
395 : : }
396 : :
397 : : /* Align given offset BASE with ALIGN. Truncate up if ALIGN_UP is true,
398 : : down otherwise. Return truncated BASE value. */
399 : :
400 : : static inline unsigned HOST_WIDE_INT
401 : : align_base (HOST_WIDE_INT base, unsigned HOST_WIDE_INT align, bool align_up)
402 : : {
403 : : return align_up ? (base + align - 1) & -align : base & -align;
404 : : }
405 : :
406 : : /* Allocate SIZE bytes at byte alignment ALIGN from the stack frame.
407 : : Return the frame offset. */
408 : :
409 : : static poly_int64
410 : 1682168 : alloc_stack_frame_space (poly_int64 size, unsigned HOST_WIDE_INT align)
411 : : {
412 : 1682168 : poly_int64 offset, new_frame_offset;
413 : :
414 : 1682168 : if (FRAME_GROWS_DOWNWARD)
415 : : {
416 : 1682168 : new_frame_offset
417 : 1682168 : = aligned_lower_bound (frame_offset - frame_phase - size,
418 : 1682168 : align) + frame_phase;
419 : 1682168 : offset = new_frame_offset;
420 : : }
421 : : else
422 : : {
423 : : new_frame_offset
424 : : = aligned_upper_bound (frame_offset - frame_phase,
425 : : align) + frame_phase;
426 : : offset = new_frame_offset;
427 : : new_frame_offset += size;
428 : : }
429 : 1682168 : frame_offset = new_frame_offset;
430 : :
431 : 1682168 : if (frame_offset_overflow (frame_offset, cfun->decl))
432 : 0 : frame_offset = offset = 0;
433 : :
434 : 1682168 : return offset;
435 : : }
436 : :
437 : : /* Ensure that the stack is aligned to ALIGN bytes.
438 : : Return the new frame offset. */
439 : : static poly_int64
440 : 1614 : align_frame_offset (unsigned HOST_WIDE_INT align)
441 : : {
442 : 0 : return alloc_stack_frame_space (0, align);
443 : : }
444 : :
445 : : /* Accumulate DECL into STACK_VARS. */
446 : :
447 : : static void
448 : 5379639 : add_stack_var (tree decl, bool really_expand)
449 : : {
450 : 5379639 : class stack_var *v;
451 : :
452 : 5379639 : if (stack_vars_num >= stack_vars_alloc)
453 : : {
454 : 1254402 : if (stack_vars_alloc)
455 : 31557 : stack_vars_alloc = stack_vars_alloc * 3 / 2;
456 : : else
457 : 1222845 : stack_vars_alloc = 32;
458 : 1254402 : stack_vars
459 : 1254402 : = XRESIZEVEC (class stack_var, stack_vars, stack_vars_alloc);
460 : : }
461 : 5379639 : if (!decl_to_stack_part)
462 : 0 : decl_to_stack_part = new hash_map<tree, unsigned>;
463 : :
464 : 5379639 : v = &stack_vars[stack_vars_num];
465 : 5379639 : decl_to_stack_part->put (decl, stack_vars_num);
466 : :
467 : 5379639 : v->decl = decl;
468 : 5379639 : tree size = TREE_CODE (decl) == SSA_NAME
469 : 10756441 : ? TYPE_SIZE_UNIT (TREE_TYPE (decl))
470 : 5376802 : : DECL_SIZE_UNIT (decl);
471 : 5379639 : v->size = tree_to_poly_uint64 (size);
472 : : /* Ensure that all variables have size, so that &a != &b for any two
473 : : variables that are simultaneously live. */
474 : 5379639 : if (known_eq (v->size, 0U))
475 : 23398 : v->size = 1;
476 : 5379639 : v->alignb = align_local_variable (decl, really_expand);
477 : : /* An alignment of zero can mightily confuse us later. */
478 : 5379639 : gcc_assert (v->alignb != 0);
479 : :
480 : : /* All variables are initially in their own partition. */
481 : 5379639 : v->representative = stack_vars_num;
482 : 5379639 : v->next = EOC;
483 : :
484 : : /* All variables initially conflict with no other. */
485 : 5379639 : v->conflicts = NULL;
486 : :
487 : : /* Ensure that this decl doesn't get put onto the list twice. */
488 : 5379639 : set_rtl (decl, pc_rtx);
489 : :
490 : 5379639 : stack_vars_num++;
491 : 5379639 : }
492 : :
493 : : /* Make the decls associated with luid's X and Y conflict. */
494 : :
495 : : static void
496 : 10700924 : add_stack_var_conflict (unsigned x, unsigned y)
497 : : {
498 : 10700924 : class stack_var *a = &stack_vars[x];
499 : 10700924 : class stack_var *b = &stack_vars[y];
500 : 10700924 : if (x == y)
501 : : return;
502 : 9694890 : if (!a->conflicts)
503 : 608802 : a->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
504 : 9694890 : if (!b->conflicts)
505 : 106377 : b->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
506 : 9694890 : bitmap_set_bit (a->conflicts, y);
507 : 9694890 : bitmap_set_bit (b->conflicts, x);
508 : : }
509 : :
510 : : /* Check whether the decls associated with luid's X and Y conflict. */
511 : :
512 : : static bool
513 : 10935250 : stack_var_conflict_p (unsigned x, unsigned y)
514 : : {
515 : 10935250 : class stack_var *a = &stack_vars[x];
516 : 10935250 : class stack_var *b = &stack_vars[y];
517 : 10935250 : if (x == y)
518 : : return false;
519 : : /* Partitions containing an SSA name result from gimple registers
520 : : with things like unsupported modes. They are top-level and
521 : : hence conflict with everything else. */
522 : 10935250 : if (TREE_CODE (a->decl) == SSA_NAME || TREE_CODE (b->decl) == SSA_NAME)
523 : : return true;
524 : :
525 : 10901132 : if (!a->conflicts || !b->conflicts)
526 : : return false;
527 : 10846798 : return bitmap_bit_p (a->conflicts, y);
528 : : }
529 : :
530 : : /* Returns the DECL's index into the stack_vars array.
531 : : If the DECL does not exist return INVALID_STACK_INDEX. */
532 : : static unsigned
533 : 43114765 : decl_stack_index (tree decl)
534 : : {
535 : 43114765 : if (!decl)
536 : : return INVALID_STACK_INDEX;
537 : 43114765 : if (!DECL_P (decl))
538 : : return INVALID_STACK_INDEX;
539 : 33366528 : if (DECL_RTL_IF_SET (decl) != pc_rtx)
540 : : return INVALID_STACK_INDEX;
541 : 22176862 : unsigned *v = decl_to_stack_part->get (decl);
542 : 22176862 : if (!v)
543 : : return INVALID_STACK_INDEX;
544 : :
545 : 22176862 : unsigned indx = *v;
546 : 22176862 : gcc_checking_assert (indx != INVALID_STACK_INDEX);
547 : 22176862 : gcc_checking_assert (indx < stack_vars_num);
548 : : return indx;
549 : : }
550 : :
551 : : /* Callback for walk_stmt_ops. If OP is a decl touched by add_stack_var
552 : : enter its partition number into bitmap DATA. */
553 : :
554 : : static bool
555 : 27587941 : visit_op (gimple *, tree op, tree, void *data)
556 : : {
557 : 27587941 : bitmap active = (bitmap)data;
558 : 27587941 : op = get_base_address (op);
559 : 27587941 : unsigned idx = decl_stack_index (op);
560 : 27587941 : if (idx != INVALID_STACK_INDEX)
561 : 13726382 : bitmap_set_bit (active, idx);
562 : 27587941 : return false;
563 : : }
564 : :
565 : : /* Callback for walk_stmt_ops. If OP is a decl touched by add_stack_var
566 : : record conflicts between it and all currently active other partitions
567 : : from bitmap DATA. */
568 : :
569 : : static bool
570 : 12560053 : visit_conflict (gimple *, tree op, tree, void *data)
571 : : {
572 : 12560053 : bitmap active = (bitmap)data;
573 : 12560053 : op = get_base_address (op);
574 : 12560053 : unsigned num = decl_stack_index (op);
575 : 12560053 : if (num != INVALID_STACK_INDEX
576 : 12560053 : && bitmap_set_bit (active, num))
577 : : {
578 : 927866 : bitmap_iterator bi;
579 : 927866 : unsigned i;
580 : 927866 : gcc_assert (num < stack_vars_num);
581 : 10357868 : EXECUTE_IF_SET_IN_BITMAP (active, 0, i, bi)
582 : 9430002 : add_stack_var_conflict (num, i);
583 : : }
584 : 12560053 : return false;
585 : : }
586 : :
587 : : /* A cache for ssa name to address of stack variables.
588 : : When taking into account if a ssa name refers to an
589 : : address of a stack variable, we need to walk the
590 : : expressions backwards to find the addresses. This
591 : : cache is there so we don't need to walk the expressions
592 : : all the time. */
593 : : struct vars_ssa_cache
594 : : {
595 : : private:
596 : : /* Currently an entry is a bitmap of all of the known stack variables
597 : : addresses that are referenced by the ssa name.
598 : : When the bitmap is the nullptr, then there is no cache.
599 : : Currently only empty bitmaps are shared.
600 : : The reason for why empty cache is not just a null is so we know the
601 : : cache for an entry is filled in. */
602 : : struct entry
603 : : {
604 : : bitmap bmap = nullptr;
605 : : };
606 : : entry *vars_ssa_caches;
607 : : public:
608 : :
609 : : vars_ssa_cache();
610 : : ~vars_ssa_cache();
611 : : const_bitmap operator() (tree name);
612 : : void dump (FILE *file);
613 : :
614 : : private:
615 : : /* Can't copy. */
616 : : vars_ssa_cache(const vars_ssa_cache&) = delete;
617 : : vars_ssa_cache(vars_ssa_cache&&) = delete;
618 : :
619 : : /* The shared empty bitmap. */
620 : : bitmap empty;
621 : :
622 : : /* Unshare the index, currently only need
623 : : to unshare if the entry was empty. */
624 : 899839 : void unshare(int indx)
625 : : {
626 : 899839 : if (vars_ssa_caches[indx].bmap == empty)
627 : 400169 : vars_ssa_caches[indx].bmap = BITMAP_ALLOC (&stack_var_bitmap_obstack);
628 : 899839 : }
629 : : void create (tree);
630 : : bool exists (tree use);
631 : : void add_one (tree old_name, unsigned);
632 : : bool update (tree old_name, tree use);
633 : : };
634 : :
635 : : /* Constructor of the cache, create the cache array. */
636 : 140606 : vars_ssa_cache::vars_ssa_cache ()
637 : : {
638 : 29694174 : vars_ssa_caches = new entry[num_ssa_names]{};
639 : :
640 : : /* Create the shared empty bitmap too. */
641 : 140606 : empty = BITMAP_ALLOC (&stack_var_bitmap_obstack);
642 : 140606 : }
643 : :
644 : : /* Delete the array. The bitmaps will be freed
645 : : when stack_var_bitmap_obstack is freed. */
646 : 140606 : vars_ssa_cache::~vars_ssa_cache ()
647 : : {
648 : 140606 : delete []vars_ssa_caches;
649 : 140606 : }
650 : :
651 : : /* Create an empty entry for the USE ssa name. */
652 : : void
653 : 10150934 : vars_ssa_cache::create (tree use)
654 : : {
655 : 10150934 : int num = SSA_NAME_VERSION (use);
656 : 10150934 : if (vars_ssa_caches[num].bmap)
657 : : return;
658 : 10150934 : vars_ssa_caches[num].bmap = empty;
659 : : }
660 : :
661 : : /* Returns true if the cache for USE exists. */
662 : : bool
663 : 65919282 : vars_ssa_cache::exists (tree use)
664 : : {
665 : 65919282 : int num = SSA_NAME_VERSION (use);
666 : 65919282 : return vars_ssa_caches[num].bmap != nullptr;
667 : : }
668 : :
669 : : /* Add to USE's bitmap for stack variable IDX. */
670 : : void
671 : 139990 : vars_ssa_cache::add_one (tree use, unsigned idx)
672 : : {
673 : 139990 : gcc_assert (idx != INVALID_STACK_INDEX);
674 : 139990 : int num = SSA_NAME_VERSION (use);
675 : 139990 : gcc_assert (vars_ssa_caches[num].bmap);
676 : 139990 : unshare (num);
677 : 139990 : bitmap_set_bit (vars_ssa_caches[num].bmap, idx);
678 : 139990 : }
679 : :
680 : : /* Update cache of OLD_NAME from the USE's cache. */
681 : : bool
682 : 17496909 : vars_ssa_cache::update (tree old_name, tree use)
683 : : {
684 : 17496909 : if (old_name == use)
685 : : return false;
686 : 8835582 : int num = SSA_NAME_VERSION (use);
687 : 8835582 : int old_num = SSA_NAME_VERSION (old_name);
688 : :
689 : : /* If the old name was empty, then there is nothing to be updated. */
690 : 8835582 : if (vars_ssa_caches[num].bmap == empty)
691 : : return false;
692 : 759849 : unshare (old_num);
693 : 759849 : return bitmap_ior_into (vars_ssa_caches[old_num].bmap, vars_ssa_caches[num].bmap);
694 : : }
695 : :
696 : : /* Dump out the cache. Note empty and non-filled
697 : : in ssa names are not printed out. */
698 : : void
699 : 0 : vars_ssa_cache::dump (FILE *file)
700 : : {
701 : 0 : fprintf (file, "var ssa address cache\n");
702 : 0 : for (unsigned num = 0; num < num_ssa_names; num++)
703 : : {
704 : 0 : if (!vars_ssa_caches[num].bmap
705 : 0 : || vars_ssa_caches[num].bmap == empty)
706 : 0 : continue;
707 : 0 : fprintf (file, "_%d refers to:\n", num);
708 : 0 : bitmap_iterator bi;
709 : 0 : unsigned i;
710 : 0 : EXECUTE_IF_SET_IN_BITMAP (vars_ssa_caches[num].bmap, 0, i, bi)
711 : : {
712 : 0 : fputc ('\t', file);
713 : 0 : print_generic_expr (file, stack_vars[i].decl, dump_flags);
714 : : }
715 : 0 : fputc ('\n', file);
716 : : }
717 : 0 : fputc ('\n', file);
718 : 0 : }
719 : :
720 : : /* Returns the filled in cache for NAME.
721 : : This will fill in the cache if it does not exist already.
722 : : Returns an empty for ssa names that can't contain pointers
723 : : (only intergal types and pointer types will contain pointers). */
724 : :
725 : : const_bitmap
726 : 51597263 : vars_ssa_cache::operator() (tree name)
727 : : {
728 : 51597263 : gcc_assert (TREE_CODE (name) == SSA_NAME);
729 : :
730 : 85229205 : if (!POINTER_TYPE_P (TREE_TYPE (name))
731 : 84163831 : && !ANY_INTEGRAL_TYPE_P (TREE_TYPE (name)))
732 : 2398721 : return empty;
733 : :
734 : 49198542 : if (exists (name))
735 : 40763749 : return vars_ssa_caches[SSA_NAME_VERSION (name)].bmap;
736 : :
737 : 8434793 : auto_vec<std::pair<tree,tree>, 4> work_list;
738 : 8434793 : auto_vec<std::pair<tree,tree>, 4> update_cache_list;
739 : :
740 : 8434793 : work_list.safe_push (std::make_pair (name, name));
741 : :
742 : 31115671 : while (!work_list.is_empty ())
743 : : {
744 : 22680878 : auto item = work_list.pop();
745 : 22680878 : tree use = item.first;
746 : 22680878 : tree old_name = item.second;
747 : 22680878 : if (TREE_CODE (use) == ADDR_EXPR)
748 : : {
749 : 400678 : tree op = TREE_OPERAND (use, 0);
750 : 400678 : op = get_base_address (op);
751 : 400678 : unsigned idx = decl_stack_index (op);
752 : 400678 : if (idx != INVALID_STACK_INDEX)
753 : 139990 : add_one (old_name, idx);
754 : 12529944 : continue;
755 : 400678 : }
756 : :
757 : 22280200 : if (TREE_CODE (use) != SSA_NAME)
758 : 5483511 : continue;
759 : :
760 : 29312025 : if (!POINTER_TYPE_P (TREE_TYPE (use))
761 : 29199229 : && !ANY_INTEGRAL_TYPE_P (TREE_TYPE (use)))
762 : 75949 : continue;
763 : :
764 : : /* Mark the old ssa name needs to be update from the use. */
765 : 16720740 : update_cache_list.safe_push (item);
766 : :
767 : : /* If the cache exists for the use, don't try to recreate it. */
768 : 16720740 : if (exists (use))
769 : 6569806 : continue;
770 : :
771 : : /* Create the cache bitmap for the use and also
772 : : so we don't go into an infinite loop for some phi nodes with loops. */
773 : 10150934 : create (use);
774 : :
775 : 10150934 : gimple *g = SSA_NAME_DEF_STMT (use);
776 : :
777 : : /* CONSTRUCTOR here is always a vector initialization,
778 : : walk each element too. */
779 : 10150934 : if (gimple_assign_single_p (g)
780 : 10150934 : && TREE_CODE (gimple_assign_rhs1 (g)) == CONSTRUCTOR)
781 : : {
782 : : tree ctr = gimple_assign_rhs1 (g);
783 : : unsigned i;
784 : : tree elm;
785 : 326944 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctr), i, elm)
786 : 228561 : work_list.safe_push (std::make_pair (elm, use));
787 : : }
788 : : /* For assignments, walk each operand for possible addresses.
789 : : For PHI nodes, walk each argument. */
790 : 10052551 : else if (gassign *a = dyn_cast <gassign *> (g))
791 : : {
792 : : /* operand 0 is the lhs. */
793 : 17867598 : for (unsigned i = 1; i < gimple_num_ops (g); i++)
794 : 10299621 : work_list.safe_push (std::make_pair (gimple_op (a, i), use));
795 : : }
796 : 11633989 : else if (gphi *p = dyn_cast <gphi *> (g))
797 : 5200958 : for (unsigned i = 0; i < gimple_phi_num_args (p); ++i)
798 : 3717903 : work_list.safe_push (std::make_pair (gimple_phi_arg_def (p, i), use));
799 : : }
800 : :
801 : : /* Update the cache. Note a loop is needed as phi nodes could
802 : : cause a loop to form. The number of times through this loop
803 : : will be small though. */
804 : 8661312 : bool changed;
805 : 8661312 : do {
806 : 8661312 : changed = false;
807 : 43480845 : for (auto &e : update_cache_list)
808 : : {
809 : 17496909 : if (update (e.second, e.first))
810 : 280752 : changed = true;
811 : : }
812 : : } while (changed);
813 : :
814 : 8434793 : return vars_ssa_caches[SSA_NAME_VERSION (name)].bmap;
815 : 8434793 : }
816 : :
817 : : /* Helper function for add_scope_conflicts_1. For USE on
818 : : a stmt, if it is a SSA_NAME and in its defining statement
819 : : is known to be based on some ADDR_EXPR, invoke VISIT
820 : : on that ADDR_EXPR. */
821 : :
822 : : static inline void
823 : 51597263 : add_scope_conflicts_2 (vars_ssa_cache &cache, tree name,
824 : : bitmap work, walk_stmt_load_store_addr_fn visit)
825 : : {
826 : 51597263 : gcc_assert (TREE_CODE (name) == SSA_NAME);
827 : :
828 : : /* Querry the cache for the mapping of addresses that are referendd by
829 : : ssa name NAME. Querrying it will fill in it. */
830 : 51597263 : bitmap_iterator bi;
831 : 51597263 : unsigned i;
832 : 51597263 : const_bitmap bmap = cache (name);
833 : : /* Visit each stack variable that is referenced. */
834 : 53651878 : EXECUTE_IF_SET_IN_BITMAP (bmap, 0, i, bi)
835 : 2054615 : visit (nullptr, stack_vars[i].decl, nullptr, work);
836 : 51597263 : }
837 : :
838 : : /* Helper routine for add_scope_conflicts, calculating the active partitions
839 : : at the end of BB, leaving the result in WORK. We're called to generate
840 : : conflicts when FOR_CONFLICT is true, otherwise we're just tracking
841 : : liveness. */
842 : :
843 : : static void
844 : 15021780 : add_scope_conflicts_1 (vars_ssa_cache &cache, basic_block bb, bitmap work, bool for_conflict)
845 : : {
846 : 15021780 : edge e;
847 : 15021780 : edge_iterator ei;
848 : 15021780 : gimple_stmt_iterator gsi;
849 : 15021780 : walk_stmt_load_store_addr_fn visit;
850 : 15021780 : use_operand_p use_p;
851 : 15021780 : ssa_op_iter iter;
852 : 15021780 : bool had_non_clobbers = false;
853 : :
854 : 15021780 : bitmap_clear (work);
855 : : /* The copy what was alive out going from the edges. */
856 : 37916211 : FOR_EACH_EDGE (e, ei, bb->preds)
857 : 22894431 : bitmap_ior_into (work, (bitmap)e->src->aux);
858 : :
859 : 15021780 : visit = for_conflict ? visit_conflict : visit_op;
860 : :
861 : : /* Addresses coming into the bb via phis are alive at the entry point. */
862 : 19861180 : for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
863 : 4839400 : add_scope_conflicts_2 (cache, gimple_phi_result (gsi_stmt (gsi)), work, visit_op);
864 : :
865 : 154259868 : for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
866 : : {
867 : 139238088 : gimple *stmt = gsi_stmt (gsi);
868 : :
869 : : /* Debug statements are not considered for liveness. */
870 : 139238088 : if (is_gimple_debug (stmt))
871 : 80581858 : continue;
872 : :
873 : : /* If we had `var = {CLOBBER}`, then var is no longer
874 : : considered alive after this point but might become
875 : : alive later on. */
876 : 58656230 : if (gimple_clobber_p (stmt))
877 : : {
878 : 3149408 : tree lhs = gimple_assign_lhs (stmt);
879 : : /* Handle only plain var clobbers, not partial ones.
880 : : Nested functions lowering and C++ front-end inserts clobbers
881 : : which are partial clobbers. */
882 : 3149408 : if (!VAR_P (lhs))
883 : 583315 : continue;
884 : 2566093 : unsigned indx = decl_stack_index (lhs);
885 : 2566093 : if (indx != INVALID_STACK_INDEX)
886 : 2043817 : bitmap_clear_bit (work, indx);
887 : : }
888 : : else
889 : : {
890 : 55506822 : if (for_conflict && !had_non_clobbers)
891 : : {
892 : : /* When we are inheriting live variables from our predecessors
893 : : through a CFG merge we might not see an actual mention of
894 : : the variables to record the approprate conflict as defs/uses
895 : : might be through indirect stores/loads. For this reason
896 : : we have to make sure each live variable conflicts with
897 : : each other. When there's just a single predecessor the
898 : : set of conflicts is already up-to-date.
899 : : We perform this delayed at the first real instruction to
900 : : allow clobbers starting this block to remove variables from
901 : : the set of live variables. */
902 : 4515533 : bitmap_iterator bi;
903 : 4515533 : unsigned i;
904 : 4515533 : if (EDGE_COUNT (bb->preds) > 1)
905 : 11901237 : EXECUTE_IF_SET_IN_BITMAP (work, 0, i, bi)
906 : : {
907 : 10577666 : class stack_var *a = &stack_vars[i];
908 : 10577666 : if (!a->conflicts)
909 : 231425 : a->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
910 : 10577666 : bitmap_ior_into (a->conflicts, work);
911 : : }
912 : 4515533 : had_non_clobbers = true;
913 : : }
914 : 55506822 : walk_stmt_load_store_addr_ops (stmt, work, visit, visit, visit);
915 : 102264685 : FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
916 : 46757863 : add_scope_conflicts_2 (cache, USE_FROM_PTR (use_p), work, visit);
917 : : }
918 : : }
919 : :
920 : : /* When there was no real instruction but there's a CFG merge we need
921 : : to add the conflicts now. */
922 : 15021780 : if (for_conflict && !had_non_clobbers && EDGE_COUNT (bb->preds) > 1)
923 : : {
924 : 152771 : bitmap_iterator bi;
925 : 152771 : unsigned i;
926 : 1011198 : EXECUTE_IF_SET_IN_BITMAP (work, 0, i, bi)
927 : : {
928 : 858427 : class stack_var *a = &stack_vars[i];
929 : 858427 : if (!a->conflicts)
930 : 40862 : a->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
931 : 858427 : bitmap_ior_into (a->conflicts, work);
932 : : }
933 : : }
934 : 15021780 : }
935 : :
936 : : /* Generate stack partition conflicts between all partitions that are
937 : : simultaneously live. */
938 : :
939 : : static void
940 : 224785 : add_scope_conflicts (void)
941 : : {
942 : : /* If there is only one variable, there is nothing to be done as
943 : : there is only possible partition. */
944 : 224785 : if (stack_vars_num == 1)
945 : 84179 : return;
946 : :
947 : 140606 : basic_block bb;
948 : 140606 : bool changed;
949 : 140606 : bitmap work = BITMAP_ALLOC (NULL);
950 : 140606 : int *rpo;
951 : 140606 : int n_bbs;
952 : :
953 : 140606 : vars_ssa_cache cache;
954 : :
955 : : /* We approximate the live range of a stack variable by taking the first
956 : : mention of its name as starting point(s), and by the end-of-scope
957 : : death clobber added by gimplify as ending point(s) of the range.
958 : : This overapproximates in the case we for instance moved an address-taken
959 : : operation upward, without also moving a dereference to it upwards.
960 : : But it's conservatively correct as a variable never can hold values
961 : : before its name is mentioned at least once.
962 : :
963 : : We then do a mostly classical bitmap liveness algorithm. */
964 : :
965 : 5145734 : FOR_ALL_BB_FN (bb, cfun)
966 : 5005128 : bb->aux = BITMAP_ALLOC (&stack_var_bitmap_obstack);
967 : :
968 : 140606 : rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
969 : 140606 : n_bbs = pre_and_rev_post_order_compute (NULL, rpo, false);
970 : :
971 : 140606 : changed = true;
972 : 569862 : while (changed)
973 : : {
974 : : int i;
975 : : changed = false;
976 : 10586514 : for (i = 0; i < n_bbs; i++)
977 : : {
978 : 10297864 : bitmap active;
979 : 10297864 : bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
980 : 10297864 : active = (bitmap)bb->aux;
981 : 10297864 : add_scope_conflicts_1 (cache, bb, work, false);
982 : 10297864 : if (bitmap_ior_into (active, work))
983 : 4204071 : changed = true;
984 : : }
985 : : }
986 : :
987 : 4864522 : FOR_EACH_BB_FN (bb, cfun)
988 : 4723916 : add_scope_conflicts_1 (cache, bb, work, true);
989 : :
990 : 140606 : if (dump_file && (dump_flags & TDF_DETAILS))
991 : 0 : cache.dump (dump_file);
992 : :
993 : 140606 : free (rpo);
994 : 140606 : BITMAP_FREE (work);
995 : 5145734 : FOR_ALL_BB_FN (bb, cfun)
996 : 5005128 : BITMAP_FREE (bb->aux);
997 : 140606 : }
998 : :
999 : : /* A subroutine of partition_stack_vars. A comparison function for qsort,
1000 : : sorting an array of indices by the properties of the object. */
1001 : :
1002 : : static int
1003 : 18659151 : stack_var_cmp (const void *a, const void *b)
1004 : : {
1005 : 18659151 : unsigned ia = *(const unsigned *)a;
1006 : 18659151 : unsigned ib = *(const unsigned *)b;
1007 : 18659151 : unsigned int aligna = stack_vars[ia].alignb;
1008 : 18659151 : unsigned int alignb = stack_vars[ib].alignb;
1009 : 18659151 : poly_int64 sizea = stack_vars[ia].size;
1010 : 18659151 : poly_int64 sizeb = stack_vars[ib].size;
1011 : 18659151 : tree decla = stack_vars[ia].decl;
1012 : 18659151 : tree declb = stack_vars[ib].decl;
1013 : 18659151 : bool largea, largeb;
1014 : 18659151 : unsigned int uida, uidb;
1015 : :
1016 : : /* Primary compare on "large" alignment. Large comes first. */
1017 : 18659151 : largea = (aligna * BITS_PER_UNIT > MAX_SUPPORTED_STACK_ALIGNMENT);
1018 : 18659151 : largeb = (alignb * BITS_PER_UNIT > MAX_SUPPORTED_STACK_ALIGNMENT);
1019 : 18659151 : if (largea != largeb)
1020 : 0 : return (int)largeb - (int)largea;
1021 : :
1022 : : /* Secondary compare on size, decreasing */
1023 : 18659151 : int diff = compare_sizes_for_sort (sizeb, sizea);
1024 : 18659151 : if (diff != 0)
1025 : 8325037 : return diff;
1026 : :
1027 : : /* Tertiary compare on true alignment, decreasing. */
1028 : 10334114 : if (aligna < alignb)
1029 : : return -1;
1030 : 9997723 : if (aligna > alignb)
1031 : : return 1;
1032 : :
1033 : : /* Final compare on ID for sort stability, increasing.
1034 : : Two SSA names are compared by their version, SSA names come before
1035 : : non-SSA names, and two normal decls are compared by their DECL_UID. */
1036 : 9719616 : if (TREE_CODE (decla) == SSA_NAME)
1037 : : {
1038 : 36367 : if (TREE_CODE (declb) == SSA_NAME)
1039 : 31838 : uida = SSA_NAME_VERSION (decla), uidb = SSA_NAME_VERSION (declb);
1040 : : else
1041 : : return -1;
1042 : : }
1043 : 9683249 : else if (TREE_CODE (declb) == SSA_NAME)
1044 : : return 1;
1045 : : else
1046 : 9677820 : uida = DECL_UID (decla), uidb = DECL_UID (declb);
1047 : 9709658 : if (uida < uidb)
1048 : : return 1;
1049 : 4849139 : if (uida > uidb)
1050 : 4849139 : return -1;
1051 : : return 0;
1052 : : }
1053 : :
1054 : : struct part_traits : unbounded_int_hashmap_traits <unsigned , bitmap> {};
1055 : : typedef hash_map<unsigned, bitmap, part_traits> part_hashmap;
1056 : :
1057 : : /* If the points-to solution *PI points to variables that are in a partition
1058 : : together with other variables add all partition members to the pointed-to
1059 : : variables bitmap. */
1060 : :
1061 : : static void
1062 : 1518832 : add_partitioned_vars_to_ptset (struct pt_solution *pt,
1063 : : part_hashmap *decls_to_partitions,
1064 : : hash_set<bitmap> *visited, bitmap temp)
1065 : : {
1066 : 1518832 : bitmap_iterator bi;
1067 : 1518832 : unsigned i;
1068 : 1518832 : bitmap *part;
1069 : :
1070 : 1518832 : if (pt->anything
1071 : 1463748 : || pt->vars == NULL
1072 : : /* The pointed-to vars bitmap is shared, it is enough to
1073 : : visit it once. */
1074 : 2982580 : || visited->add (pt->vars))
1075 : 1244115 : return;
1076 : :
1077 : 274717 : bitmap_clear (temp);
1078 : :
1079 : : /* By using a temporary bitmap to store all members of the partitions
1080 : : we have to add we make sure to visit each of the partitions only
1081 : : once. */
1082 : 1233947 : EXECUTE_IF_SET_IN_BITMAP (pt->vars, 0, i, bi)
1083 : 959230 : if ((!temp
1084 : 959230 : || !bitmap_bit_p (temp, i))
1085 : 1546115 : && (part = decls_to_partitions->get (i)))
1086 : 179113 : bitmap_ior_into (temp, *part);
1087 : 274717 : if (!bitmap_empty_p (temp))
1088 : 143527 : bitmap_ior_into (pt->vars, temp);
1089 : : }
1090 : :
1091 : : /* Update points-to sets based on partition info, so we can use them on RTL.
1092 : : The bitmaps representing stack partitions will be saved until expand,
1093 : : where partitioned decls used as bases in memory expressions will be
1094 : : rewritten.
1095 : :
1096 : : It is not necessary to update TBAA info on accesses to the coalesced
1097 : : storage since our memory model doesn't allow TBAA to be used for
1098 : : WAW or WAR dependences. For RAW when the write is to an old object
1099 : : the new object would not have been initialized at the point of the
1100 : : read, invoking undefined behavior. */
1101 : :
1102 : : static void
1103 : 140606 : update_alias_info_with_stack_vars (void)
1104 : : {
1105 : 140606 : part_hashmap *decls_to_partitions = NULL;
1106 : 140606 : unsigned i, j;
1107 : 140606 : tree var = NULL_TREE;
1108 : :
1109 : 1189999 : for (i = 0; i < stack_vars_num; i++)
1110 : : {
1111 : 1049393 : bitmap part = NULL;
1112 : 1049393 : tree name;
1113 : 1049393 : struct ptr_info_def *pi;
1114 : :
1115 : : /* Not interested in partitions with single variable. */
1116 : 1049393 : if (stack_vars[i].representative != i
1117 : 808508 : || stack_vars[i].next == EOC)
1118 : 982674 : continue;
1119 : :
1120 : 66719 : if (!decls_to_partitions)
1121 : : {
1122 : 40551 : decls_to_partitions = new part_hashmap;
1123 : 40551 : cfun->gimple_df->decls_to_pointers = new hash_map<tree, tree>;
1124 : : }
1125 : :
1126 : : /* Create an SSA_NAME that points to the partition for use
1127 : : as base during alias-oracle queries on RTL for bases that
1128 : : have been partitioned. */
1129 : 66719 : if (var == NULL_TREE)
1130 : 40551 : var = create_tmp_var (ptr_type_node);
1131 : 66719 : name = make_ssa_name (var);
1132 : :
1133 : : /* Create bitmaps representing partitions. They will be used for
1134 : : points-to sets later, so use GGC alloc. */
1135 : 66719 : part = BITMAP_GGC_ALLOC ();
1136 : 374323 : for (j = i; j != EOC; j = stack_vars[j].next)
1137 : : {
1138 : 307604 : tree decl = stack_vars[j].decl;
1139 : 307604 : unsigned int uid = DECL_PT_UID (decl);
1140 : 307604 : bitmap_set_bit (part, uid);
1141 : 307604 : decls_to_partitions->put (uid, part);
1142 : 307604 : cfun->gimple_df->decls_to_pointers->put (decl, name);
1143 : 307604 : if (TREE_ADDRESSABLE (decl))
1144 : 273934 : TREE_ADDRESSABLE (name) = 1;
1145 : : }
1146 : :
1147 : : /* Make the SSA name point to all partition members. */
1148 : 66719 : pi = get_ptr_info (name);
1149 : 66719 : pt_solution_set (&pi->pt, part, false);
1150 : : }
1151 : :
1152 : : /* Make all points-to sets that contain one member of a partition
1153 : : contain all members of the partition. */
1154 : 140606 : if (decls_to_partitions)
1155 : : {
1156 : 40551 : unsigned i;
1157 : 40551 : tree name;
1158 : 40551 : hash_set<bitmap> visited;
1159 : 40551 : bitmap temp = BITMAP_ALLOC (&stack_var_bitmap_obstack);
1160 : :
1161 : 15225654 : FOR_EACH_SSA_NAME (i, name, cfun)
1162 : : {
1163 : 9756256 : struct ptr_info_def *pi;
1164 : :
1165 : 17901699 : if (POINTER_TYPE_P (TREE_TYPE (name))
1166 : 9835643 : && ((pi = SSA_NAME_PTR_INFO (name)) != NULL))
1167 : 1437730 : add_partitioned_vars_to_ptset (&pi->pt, decls_to_partitions,
1168 : : &visited, temp);
1169 : : }
1170 : :
1171 : 40551 : add_partitioned_vars_to_ptset (&cfun->gimple_df->escaped,
1172 : : decls_to_partitions, &visited, temp);
1173 : 40551 : add_partitioned_vars_to_ptset (&cfun->gimple_df->escaped_return,
1174 : : decls_to_partitions, &visited, temp);
1175 : 40551 : delete decls_to_partitions;
1176 : 40551 : BITMAP_FREE (temp);
1177 : 40551 : }
1178 : 140606 : }
1179 : :
1180 : : /* A subroutine of partition_stack_vars. The UNION portion of a UNION/FIND
1181 : : partitioning algorithm. Partitions A and B are known to be non-conflicting.
1182 : : Merge them into a single partition A. */
1183 : :
1184 : : static void
1185 : 240885 : union_stack_vars (unsigned a, unsigned b)
1186 : : {
1187 : 240885 : class stack_var *vb = &stack_vars[b];
1188 : 240885 : bitmap_iterator bi;
1189 : 240885 : unsigned u;
1190 : :
1191 : 240885 : gcc_assert (stack_vars[b].next == EOC);
1192 : : /* Add B to A's partition. */
1193 : 240885 : stack_vars[b].next = stack_vars[a].next;
1194 : 240885 : stack_vars[b].representative = a;
1195 : 240885 : stack_vars[a].next = b;
1196 : :
1197 : : /* Make sure A is big enough to hold B. */
1198 : 240885 : stack_vars[a].size = upper_bound (stack_vars[a].size, stack_vars[b].size);
1199 : :
1200 : : /* Update the required alignment of partition A to account for B. */
1201 : 240885 : if (stack_vars[a].alignb < stack_vars[b].alignb)
1202 : 2640 : stack_vars[a].alignb = stack_vars[b].alignb;
1203 : :
1204 : : /* Update the interference graph and merge the conflicts. */
1205 : 240885 : if (vb->conflicts)
1206 : : {
1207 : 1459443 : EXECUTE_IF_SET_IN_BITMAP (vb->conflicts, 0, u, bi)
1208 : 1270603 : add_stack_var_conflict (a, stack_vars[u].representative);
1209 : 188840 : BITMAP_FREE (vb->conflicts);
1210 : : }
1211 : 240885 : }
1212 : :
1213 : : /* A subroutine of expand_used_vars. Binpack the variables into
1214 : : partitions constrained by the interference graph. The overall
1215 : : algorithm used is as follows:
1216 : :
1217 : : Sort the objects by size in descending order.
1218 : : For each object A {
1219 : : S = size(A)
1220 : : O = 0
1221 : : loop {
1222 : : Look for the largest non-conflicting object B with size <= S.
1223 : : UNION (A, B)
1224 : : }
1225 : : }
1226 : : */
1227 : :
1228 : : static void
1229 : 224785 : partition_stack_vars (void)
1230 : : {
1231 : 224785 : unsigned si, sj, n = stack_vars_num;
1232 : :
1233 : 224785 : stack_vars_sorted = XNEWVEC (unsigned, stack_vars_num);
1234 : 1358357 : for (si = 0; si < n; ++si)
1235 : 1133572 : stack_vars_sorted[si] = si;
1236 : :
1237 : 224785 : if (n == 1)
1238 : : return;
1239 : :
1240 : 140606 : qsort (stack_vars_sorted, n, sizeof (unsigned), stack_var_cmp);
1241 : :
1242 : 1189999 : for (si = 0; si < n; ++si)
1243 : : {
1244 : 1049393 : unsigned i = stack_vars_sorted[si];
1245 : 1049393 : unsigned int ialign = stack_vars[i].alignb;
1246 : 1049393 : poly_int64 isize = stack_vars[i].size;
1247 : :
1248 : : /* Ignore objects that aren't partition representatives. If we
1249 : : see a var that is not a partition representative, it must
1250 : : have been merged earlier. */
1251 : 1049393 : if (stack_vars[i].representative != i)
1252 : 240885 : continue;
1253 : :
1254 : 12255176 : for (sj = si + 1; sj < n; ++sj)
1255 : : {
1256 : 11447605 : unsigned j = stack_vars_sorted[sj];
1257 : 11447605 : unsigned int jalign = stack_vars[j].alignb;
1258 : 11447605 : poly_int64 jsize = stack_vars[j].size;
1259 : :
1260 : : /* Ignore objects that aren't partition representatives. */
1261 : 11447605 : if (stack_vars[j].representative != j)
1262 : 11446668 : continue;
1263 : :
1264 : : /* Do not mix objects of "small" (supported) alignment
1265 : : and "large" (unsupported) alignment. */
1266 : 10936187 : if ((ialign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
1267 : 10936187 : != (jalign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT))
1268 : : break;
1269 : :
1270 : : /* For Address Sanitizer do not mix objects with different
1271 : : sizes, as the shorter vars wouldn't be adequately protected.
1272 : : Don't do that for "large" (unsupported) alignment objects,
1273 : : those aren't protected anyway. */
1274 : 10936187 : if (asan_sanitize_stack_p ()
1275 : 4973 : && maybe_ne (isize, jsize)
1276 : 10937124 : && ialign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
1277 : : break;
1278 : :
1279 : : /* Ignore conflicting objects. */
1280 : 10935250 : if (stack_var_conflict_p (i, j))
1281 : 10694365 : continue;
1282 : :
1283 : : /* UNION the objects, placing J at OFFSET. */
1284 : 240885 : union_stack_vars (i, j);
1285 : : }
1286 : : }
1287 : :
1288 : 140606 : update_alias_info_with_stack_vars ();
1289 : : }
1290 : :
1291 : : /* A debugging aid for expand_used_vars. Dump the generated partitions. */
1292 : :
1293 : : static void
1294 : 29 : dump_stack_var_partition (void)
1295 : : {
1296 : 29 : unsigned si, i, j, n = stack_vars_num;
1297 : :
1298 : 65 : for (si = 0; si < n; ++si)
1299 : : {
1300 : 36 : i = stack_vars_sorted[si];
1301 : :
1302 : : /* Skip variables that aren't partition representatives, for now. */
1303 : 36 : if (stack_vars[i].representative != i)
1304 : 3 : continue;
1305 : :
1306 : 33 : fprintf (dump_file, "Partition %u: size ", i);
1307 : 33 : print_dec (stack_vars[i].size, dump_file);
1308 : 33 : fprintf (dump_file, " align %u\n", stack_vars[i].alignb);
1309 : :
1310 : 102 : for (j = i; j != EOC; j = stack_vars[j].next)
1311 : : {
1312 : 36 : fputc ('\t', dump_file);
1313 : 36 : print_generic_expr (dump_file, stack_vars[j].decl, dump_flags);
1314 : : }
1315 : 33 : fputc ('\n', dump_file);
1316 : : }
1317 : 29 : }
1318 : :
1319 : : /* Assign rtl to DECL at BASE + OFFSET. */
1320 : :
1321 : : static void
1322 : 1920003 : expand_one_stack_var_at (tree decl, rtx base, unsigned base_align,
1323 : : poly_int64 offset)
1324 : : {
1325 : 1920003 : unsigned align;
1326 : 1920003 : rtx x;
1327 : :
1328 : : /* If this fails, we've overflowed the stack frame. Error nicely? */
1329 : 1920003 : gcc_assert (known_eq (offset, trunc_int_for_mode (offset, Pmode)));
1330 : :
1331 : 1920003 : if (hwasan_sanitize_stack_p ())
1332 : 89 : x = targetm.memtag.add_tag (base, offset,
1333 : 89 : hwasan_current_frame_tag ());
1334 : : else
1335 : 1919914 : x = plus_constant (Pmode, base, offset);
1336 : :
1337 : 3840006 : x = gen_rtx_MEM (TREE_CODE (decl) == SSA_NAME
1338 : 443659 : ? TYPE_MODE (TREE_TYPE (decl))
1339 : 1476344 : : DECL_MODE (decl), x);
1340 : :
1341 : : /* Set alignment we actually gave this decl if it isn't an SSA name.
1342 : : If it is we generate stack slots only accidentally so it isn't as
1343 : : important, we'll simply set the alignment directly on the MEM. */
1344 : :
1345 : 1920003 : if (stack_vars_base_reg_p (base))
1346 : 1917340 : offset -= frame_phase;
1347 : 1920003 : align = known_alignment (offset);
1348 : 1920003 : align *= BITS_PER_UNIT;
1349 : 1920003 : if (align == 0 || align > base_align)
1350 : 780300 : align = base_align;
1351 : :
1352 : 1920003 : if (TREE_CODE (decl) != SSA_NAME)
1353 : : {
1354 : : /* One would think that we could assert that we're not decreasing
1355 : : alignment here, but (at least) the i386 port does exactly this
1356 : : via the MINIMUM_ALIGNMENT hook. */
1357 : :
1358 : 1476344 : SET_DECL_ALIGN (decl, align);
1359 : 1476344 : DECL_USER_ALIGN (decl) = 0;
1360 : : }
1361 : :
1362 : 1920003 : set_rtl (decl, x);
1363 : :
1364 : 1920003 : set_mem_align (x, align);
1365 : 1920003 : }
1366 : :
1367 : 224785 : class stack_vars_data
1368 : : {
1369 : : public:
1370 : : /* Vector of offset pairs, always end of some padding followed
1371 : : by start of the padding that needs Address Sanitizer protection.
1372 : : The vector is in reversed, highest offset pairs come first. */
1373 : : auto_vec<HOST_WIDE_INT> asan_vec;
1374 : :
1375 : : /* Vector of partition representative decls in between the paddings. */
1376 : : auto_vec<tree> asan_decl_vec;
1377 : :
1378 : : /* Base pseudo register for Address Sanitizer protected automatic vars. */
1379 : : rtx asan_base;
1380 : :
1381 : : /* Alignment needed for the Address Sanitizer protected automatic vars. */
1382 : : unsigned int asan_alignb;
1383 : : };
1384 : :
1385 : : /* A subroutine of expand_used_vars. Give each partition representative
1386 : : a unique location within the stack frame. Update each partition member
1387 : : with that location. */
1388 : : static void
1389 : 226592 : expand_stack_vars (bool (*pred) (unsigned), class stack_vars_data *data)
1390 : : {
1391 : 226592 : unsigned si, i, j, n = stack_vars_num;
1392 : 226592 : poly_uint64 large_size = 0, large_alloc = 0;
1393 : 226592 : rtx large_base = NULL;
1394 : 226592 : rtx large_untagged_base = NULL;
1395 : 226592 : unsigned large_align = 0;
1396 : 226592 : bool large_allocation_done = false;
1397 : 226592 : tree decl;
1398 : :
1399 : : /* Determine if there are any variables requiring "large" alignment.
1400 : : Since these are dynamically allocated, we only process these if
1401 : : no predicate involved. */
1402 : 226592 : large_align = stack_vars[stack_vars_sorted[0]].alignb * BITS_PER_UNIT;
1403 : 226592 : if (pred == NULL && large_align > MAX_SUPPORTED_STACK_ALIGNMENT)
1404 : : {
1405 : : /* Find the total size of these variables. */
1406 : 0 : for (si = 0; si < n; ++si)
1407 : : {
1408 : 0 : unsigned alignb;
1409 : :
1410 : 0 : i = stack_vars_sorted[si];
1411 : 0 : alignb = stack_vars[i].alignb;
1412 : :
1413 : : /* All "large" alignment decls come before all "small" alignment
1414 : : decls, but "large" alignment decls are not sorted based on
1415 : : their alignment. Increase large_align to track the largest
1416 : : required alignment. */
1417 : 0 : if ((alignb * BITS_PER_UNIT) > large_align)
1418 : : large_align = alignb * BITS_PER_UNIT;
1419 : :
1420 : : /* Stop when we get to the first decl with "small" alignment. */
1421 : 0 : if (alignb * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
1422 : : break;
1423 : :
1424 : : /* Skip variables that aren't partition representatives. */
1425 : 0 : if (stack_vars[i].representative != i)
1426 : 0 : continue;
1427 : :
1428 : : /* Skip variables that have already had rtl assigned. See also
1429 : : add_stack_var where we perpetrate this pc_rtx hack. */
1430 : 0 : decl = stack_vars[i].decl;
1431 : 0 : if (TREE_CODE (decl) == SSA_NAME
1432 : 0 : ? SA.partition_to_pseudo[var_to_partition (SA.map, decl)] != NULL_RTX
1433 : 0 : : DECL_RTL (decl) != pc_rtx)
1434 : 0 : continue;
1435 : :
1436 : 0 : large_size = aligned_upper_bound (large_size, alignb);
1437 : 0 : large_size += stack_vars[i].size;
1438 : : }
1439 : : }
1440 : :
1441 : 1363981 : for (si = 0; si < n; ++si)
1442 : : {
1443 : 1137389 : rtx base;
1444 : 1137389 : unsigned base_align, alignb;
1445 : 1137389 : poly_int64 offset = 0;
1446 : :
1447 : 1137389 : i = stack_vars_sorted[si];
1448 : :
1449 : : /* Skip variables that aren't partition representatives, for now. */
1450 : 1137389 : if (stack_vars[i].representative != i)
1451 : 1378285 : continue;
1452 : :
1453 : : /* Skip variables that have already had rtl assigned. See also
1454 : : add_stack_var where we perpetrate this pc_rtx hack. */
1455 : 896493 : decl = stack_vars[i].decl;
1456 : 896493 : if (TREE_CODE (decl) == SSA_NAME
1457 : 896493 : ? SA.partition_to_pseudo[var_to_partition (SA.map, decl)] != NULL_RTX
1458 : 893084 : : DECL_RTL (decl) != pc_rtx)
1459 : 2904 : continue;
1460 : :
1461 : : /* Check the predicate to see whether this variable should be
1462 : : allocated in this pass. */
1463 : 893589 : if (pred && !pred (i))
1464 : 902 : continue;
1465 : :
1466 : 892687 : base = (hwasan_sanitize_stack_p ()
1467 : 892687 : ? hwasan_frame_base ()
1468 : 892631 : : virtual_stack_vars_rtx);
1469 : 892687 : alignb = stack_vars[i].alignb;
1470 : 892687 : if (alignb * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
1471 : : {
1472 : 892687 : poly_int64 hwasan_orig_offset;
1473 : 892687 : if (hwasan_sanitize_stack_p ())
1474 : : {
1475 : : /* There must be no tag granule "shared" between different
1476 : : objects. This means that no HWASAN_TAG_GRANULE_SIZE byte
1477 : : chunk can have more than one object in it.
1478 : :
1479 : : We ensure this by forcing the end of the last bit of data to
1480 : : be aligned to HWASAN_TAG_GRANULE_SIZE bytes here, and setting
1481 : : the start of each variable to be aligned to
1482 : : HWASAN_TAG_GRANULE_SIZE bytes in `align_local_variable`.
1483 : :
1484 : : We can't align just one of the start or end, since there are
1485 : : untagged things stored on the stack which we do not align to
1486 : : HWASAN_TAG_GRANULE_SIZE bytes. If we only aligned the start
1487 : : or the end of tagged objects then untagged objects could end
1488 : : up sharing the first granule of a tagged object or sharing the
1489 : : last granule of a tagged object respectively. */
1490 : 56 : hwasan_orig_offset = align_frame_offset (HWASAN_TAG_GRANULE_SIZE);
1491 : 56 : gcc_assert (stack_vars[i].alignb >= HWASAN_TAG_GRANULE_SIZE);
1492 : : }
1493 : : /* ASAN description strings don't yet have a syntax for expressing
1494 : : polynomial offsets. */
1495 : 892687 : HOST_WIDE_INT prev_offset;
1496 : 892687 : if (asan_sanitize_stack_p ()
1497 : 3280 : && pred
1498 : 2658 : && frame_offset.is_constant (&prev_offset)
1499 : 892687 : && stack_vars[i].size.is_constant ())
1500 : : {
1501 : 2658 : if (data->asan_vec.is_empty ())
1502 : : {
1503 : 1436 : align_frame_offset (ASAN_RED_ZONE_SIZE);
1504 : 1436 : prev_offset = frame_offset.to_constant ();
1505 : : }
1506 : 2658 : prev_offset = align_base (prev_offset,
1507 : : ASAN_MIN_RED_ZONE_SIZE,
1508 : : !FRAME_GROWS_DOWNWARD);
1509 : 2658 : tree repr_decl = NULL_TREE;
1510 : 2658 : unsigned HOST_WIDE_INT size
1511 : 2658 : = asan_var_and_redzone_size (stack_vars[i].size.to_constant ());
1512 : 2658 : if (data->asan_vec.is_empty ())
1513 : 1436 : size = MAX (size, ASAN_RED_ZONE_SIZE);
1514 : :
1515 : 2658 : unsigned HOST_WIDE_INT alignment = MAX (alignb,
1516 : : ASAN_MIN_RED_ZONE_SIZE);
1517 : 2658 : offset = alloc_stack_frame_space (size, alignment);
1518 : :
1519 : 2658 : data->asan_vec.safe_push (prev_offset);
1520 : : /* Allocating a constant amount of space from a constant
1521 : : starting offset must give a constant result. */
1522 : 2658 : data->asan_vec.safe_push ((offset + stack_vars[i].size)
1523 : 2658 : .to_constant ());
1524 : : /* Find best representative of the partition.
1525 : : Prefer those with DECL_NAME, even better
1526 : : satisfying asan_protect_stack_decl predicate. */
1527 : 3168 : for (j = i; j != EOC; j = stack_vars[j].next)
1528 : 2662 : if (asan_protect_stack_decl (stack_vars[j].decl)
1529 : 2662 : && DECL_NAME (stack_vars[j].decl))
1530 : : {
1531 : 2152 : repr_decl = stack_vars[j].decl;
1532 : 2152 : break;
1533 : : }
1534 : 510 : else if (repr_decl == NULL_TREE
1535 : 510 : && DECL_P (stack_vars[j].decl)
1536 : 1020 : && DECL_NAME (stack_vars[j].decl))
1537 : 0 : repr_decl = stack_vars[j].decl;
1538 : 2658 : if (repr_decl == NULL_TREE)
1539 : 506 : repr_decl = stack_vars[i].decl;
1540 : 2658 : data->asan_decl_vec.safe_push (repr_decl);
1541 : :
1542 : : /* Make sure a representative is unpoison if another
1543 : : variable in the partition is handled by
1544 : : use-after-scope sanitization. */
1545 : 2658 : if (asan_handled_variables != NULL
1546 : 2658 : && !asan_handled_variables->contains (repr_decl))
1547 : : {
1548 : 304 : for (j = i; j != EOC; j = stack_vars[j].next)
1549 : 154 : if (asan_handled_variables->contains (stack_vars[j].decl))
1550 : : break;
1551 : 152 : if (j != EOC)
1552 : 2 : asan_handled_variables->add (repr_decl);
1553 : : }
1554 : :
1555 : 2658 : data->asan_alignb = MAX (data->asan_alignb, alignb);
1556 : 2658 : if (data->asan_base == NULL)
1557 : 1436 : data->asan_base = gen_reg_rtx (Pmode);
1558 : 2658 : base = data->asan_base;
1559 : :
1560 : 2658 : if (!STRICT_ALIGNMENT)
1561 : 2658 : base_align = crtl->max_used_stack_slot_alignment;
1562 : : else
1563 : : base_align = MAX (crtl->max_used_stack_slot_alignment,
1564 : : GET_MODE_ALIGNMENT (SImode)
1565 : : << ASAN_SHADOW_SHIFT);
1566 : : }
1567 : : else
1568 : : {
1569 : 890029 : offset = alloc_stack_frame_space (stack_vars[i].size, alignb);
1570 : 890029 : base_align = crtl->max_used_stack_slot_alignment;
1571 : :
1572 : 890029 : if (hwasan_sanitize_stack_p ())
1573 : : {
1574 : : /* Align again since the point of this alignment is to handle
1575 : : the "end" of the object (i.e. smallest address after the
1576 : : stack object). For FRAME_GROWS_DOWNWARD that requires
1577 : : aligning the stack before allocating, but for a frame that
1578 : : grows upwards that requires aligning the stack after
1579 : : allocation.
1580 : :
1581 : : Use `frame_offset` to record the offset value rather than
1582 : : `offset` since the `frame_offset` describes the extent
1583 : : allocated for this particular variable while `offset`
1584 : : describes the address that this variable starts at. */
1585 : 56 : align_frame_offset (HWASAN_TAG_GRANULE_SIZE);
1586 : 56 : hwasan_record_stack_var (virtual_stack_vars_rtx, base,
1587 : : hwasan_orig_offset, frame_offset);
1588 : : }
1589 : : }
1590 : : }
1591 : : else
1592 : : {
1593 : : /* Large alignment is only processed in the last pass. */
1594 : 0 : if (pred)
1595 : 0 : continue;
1596 : :
1597 : : /* If there were any variables requiring "large" alignment, allocate
1598 : : space. */
1599 : 0 : if (maybe_ne (large_size, 0U) && ! large_allocation_done)
1600 : : {
1601 : 0 : poly_int64 loffset;
1602 : 0 : rtx large_allocsize;
1603 : :
1604 : 0 : large_allocsize = gen_int_mode (large_size, Pmode);
1605 : 0 : get_dynamic_stack_size (&large_allocsize, 0, large_align, NULL);
1606 : 0 : loffset = alloc_stack_frame_space
1607 : 0 : (rtx_to_poly_int64 (large_allocsize),
1608 : 0 : PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT);
1609 : 0 : large_base = get_dynamic_stack_base (loffset, large_align, base);
1610 : 0 : large_allocation_done = true;
1611 : : }
1612 : :
1613 : 0 : gcc_assert (large_base != NULL);
1614 : 0 : large_alloc = aligned_upper_bound (large_alloc, alignb);
1615 : 0 : offset = large_alloc;
1616 : 0 : large_alloc += stack_vars[i].size;
1617 : 0 : if (hwasan_sanitize_stack_p ())
1618 : : {
1619 : : /* An object with a large alignment requirement means that the
1620 : : alignment requirement is greater than the required alignment
1621 : : for tags. */
1622 : 0 : if (!large_untagged_base)
1623 : 0 : large_untagged_base
1624 : 0 : = targetm.memtag.untagged_pointer (large_base, NULL_RTX);
1625 : : /* Ensure the end of the variable is also aligned correctly. */
1626 : 0 : poly_int64 align_again
1627 : 0 : = aligned_upper_bound (large_alloc, HWASAN_TAG_GRANULE_SIZE);
1628 : : /* For large allocations we always allocate a chunk of space
1629 : : (which is addressed by large_untagged_base/large_base) and
1630 : : then use positive offsets from that. Hence the farthest
1631 : : offset is `align_again` and the nearest offset from the base
1632 : : is `offset`. */
1633 : 0 : hwasan_record_stack_var (large_untagged_base, large_base,
1634 : : offset, align_again);
1635 : : }
1636 : :
1637 : : base = large_base;
1638 : : base_align = large_align;
1639 : : }
1640 : :
1641 : : /* Create rtl for each variable based on their location within the
1642 : : partition. */
1643 : 2026259 : for (j = i; j != EOC; j = stack_vars[j].next)
1644 : : {
1645 : 1133572 : expand_one_stack_var_at (stack_vars[j].decl,
1646 : : base, base_align, offset);
1647 : : }
1648 : 892687 : if (hwasan_sanitize_stack_p ())
1649 : 56 : hwasan_increment_frame_tag ();
1650 : : }
1651 : :
1652 : 226592 : gcc_assert (known_eq (large_alloc, large_size));
1653 : 226592 : }
1654 : :
1655 : : /* Take into account all sizes of partitions and reset DECL_RTLs. */
1656 : : static poly_uint64
1657 : 998060 : account_stack_vars (void)
1658 : : {
1659 : 998060 : unsigned si, j, i, n = stack_vars_num;
1660 : 998060 : poly_uint64 size = 0;
1661 : :
1662 : 5244127 : for (si = 0; si < n; ++si)
1663 : : {
1664 : 4246067 : i = stack_vars_sorted[si];
1665 : :
1666 : : /* Skip variables that aren't partition representatives, for now. */
1667 : 4246067 : if (stack_vars[i].representative != i)
1668 : 0 : continue;
1669 : :
1670 : 8492134 : size += stack_vars[i].size;
1671 : 8492134 : for (j = i; j != EOC; j = stack_vars[j].next)
1672 : 4246067 : set_rtl (stack_vars[j].decl, NULL);
1673 : : }
1674 : 998060 : return size;
1675 : : }
1676 : :
1677 : : /* Record the RTL assignment X for the default def of PARM. */
1678 : :
1679 : : extern void
1680 : 4576297 : set_parm_rtl (tree parm, rtx x)
1681 : : {
1682 : 4576297 : gcc_assert (TREE_CODE (parm) == PARM_DECL
1683 : : || TREE_CODE (parm) == RESULT_DECL);
1684 : :
1685 : 4576297 : if (x && !MEM_P (x))
1686 : : {
1687 : 2957668 : unsigned int align = MINIMUM_ALIGNMENT (TREE_TYPE (parm),
1688 : : TYPE_MODE (TREE_TYPE (parm)),
1689 : : TYPE_ALIGN (TREE_TYPE (parm)));
1690 : :
1691 : : /* If the variable alignment is very large we'll dynamicaly
1692 : : allocate it, which means that in-frame portion is just a
1693 : : pointer. ??? We've got a pseudo for sure here, do we
1694 : : actually dynamically allocate its spilling area if needed?
1695 : : ??? Isn't it a problem when Pmode alignment also exceeds
1696 : : MAX_SUPPORTED_STACK_ALIGNMENT, as can happen on cris and lm32? */
1697 : 2957668 : if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
1698 : 0 : align = GET_MODE_ALIGNMENT (Pmode);
1699 : :
1700 : 2957668 : record_alignment_for_reg_var (align);
1701 : : }
1702 : :
1703 : 4576297 : tree ssa = ssa_default_def (cfun, parm);
1704 : 4576297 : if (!ssa)
1705 : 1029265 : return set_rtl (parm, x);
1706 : :
1707 : 3547032 : int part = var_to_partition (SA.map, ssa);
1708 : 3547032 : gcc_assert (part != NO_PARTITION);
1709 : :
1710 : 3547032 : bool changed = bitmap_bit_p (SA.partitions_for_parm_default_defs, part);
1711 : 3547032 : gcc_assert (changed);
1712 : :
1713 : 3547032 : set_rtl (ssa, x);
1714 : 3547032 : gcc_assert (DECL_RTL (parm) == x);
1715 : : }
1716 : :
1717 : : /* A subroutine of expand_one_var. Called to immediately assign rtl
1718 : : to a variable to be allocated in the stack frame. */
1719 : :
1720 : : static void
1721 : 786431 : expand_one_stack_var_1 (tree var)
1722 : : {
1723 : 786431 : poly_uint64 size;
1724 : 786431 : poly_int64 offset;
1725 : 786431 : unsigned byte_align;
1726 : :
1727 : 786431 : if (TREE_CODE (var) == SSA_NAME)
1728 : : {
1729 : 440822 : tree type = TREE_TYPE (var);
1730 : 440822 : size = tree_to_poly_uint64 (TYPE_SIZE_UNIT (type));
1731 : : }
1732 : : else
1733 : 345609 : size = tree_to_poly_uint64 (DECL_SIZE_UNIT (var));
1734 : :
1735 : 786431 : byte_align = align_local_variable (var, true);
1736 : :
1737 : : /* We handle highly aligned variables in expand_stack_vars. */
1738 : 786431 : gcc_assert (byte_align * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT);
1739 : :
1740 : 786431 : rtx base;
1741 : 786431 : if (hwasan_sanitize_stack_p ())
1742 : : {
1743 : : /* Allocate zero bytes to align the stack. */
1744 : 33 : poly_int64 hwasan_orig_offset
1745 : 33 : = align_frame_offset (HWASAN_TAG_GRANULE_SIZE);
1746 : 33 : offset = alloc_stack_frame_space (size, byte_align);
1747 : 33 : align_frame_offset (HWASAN_TAG_GRANULE_SIZE);
1748 : 33 : base = hwasan_frame_base ();
1749 : : /* Use `frame_offset` to automatically account for machines where the
1750 : : frame grows upwards.
1751 : :
1752 : : `offset` will always point to the "start" of the stack object, which
1753 : : will be the smallest address, for ! FRAME_GROWS_DOWNWARD this is *not*
1754 : : the "furthest" offset from the base delimiting the current stack
1755 : : object. `frame_offset` will always delimit the extent that the frame.
1756 : : */
1757 : 33 : hwasan_record_stack_var (virtual_stack_vars_rtx, base,
1758 : : hwasan_orig_offset, frame_offset);
1759 : : }
1760 : : else
1761 : : {
1762 : 786398 : offset = alloc_stack_frame_space (size, byte_align);
1763 : 786398 : base = virtual_stack_vars_rtx;
1764 : : }
1765 : :
1766 : 786431 : expand_one_stack_var_at (var, base,
1767 : : crtl->max_used_stack_slot_alignment, offset);
1768 : :
1769 : 786431 : if (hwasan_sanitize_stack_p ())
1770 : 33 : hwasan_increment_frame_tag ();
1771 : 786431 : }
1772 : :
1773 : : /* Wrapper for expand_one_stack_var_1 that checks SSA_NAMEs are
1774 : : already assigned some MEM. */
1775 : :
1776 : : static void
1777 : 345609 : expand_one_stack_var (tree var)
1778 : : {
1779 : 345609 : if (TREE_CODE (var) == SSA_NAME)
1780 : : {
1781 : 0 : int part = var_to_partition (SA.map, var);
1782 : 0 : if (part != NO_PARTITION)
1783 : : {
1784 : 0 : rtx x = SA.partition_to_pseudo[part];
1785 : 0 : gcc_assert (x);
1786 : 0 : gcc_assert (MEM_P (x));
1787 : : return;
1788 : : }
1789 : : }
1790 : :
1791 : 345609 : return expand_one_stack_var_1 (var);
1792 : : }
1793 : :
1794 : : /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
1795 : : that will reside in a hard register. */
1796 : :
1797 : : static void
1798 : 1085 : expand_one_hard_reg_var (tree var)
1799 : : {
1800 : 0 : rest_of_decl_compilation (var, 0, 0);
1801 : 0 : }
1802 : :
1803 : : /* Record the alignment requirements of some variable assigned to a
1804 : : pseudo. */
1805 : :
1806 : : static void
1807 : 38801594 : record_alignment_for_reg_var (unsigned int align)
1808 : : {
1809 : 38801594 : if (SUPPORTS_STACK_ALIGNMENT
1810 : 38801594 : && crtl->stack_alignment_estimated < align)
1811 : : {
1812 : : /* stack_alignment_estimated shouldn't change after stack
1813 : : realign decision made */
1814 : 1696241 : gcc_assert (!crtl->stack_realign_processed);
1815 : 1696241 : crtl->stack_alignment_estimated = align;
1816 : : }
1817 : :
1818 : : /* stack_alignment_needed > PREFERRED_STACK_BOUNDARY is permitted.
1819 : : So here we only make sure stack_alignment_needed >= align. */
1820 : 38801594 : if (crtl->stack_alignment_needed < align)
1821 : 544641 : crtl->stack_alignment_needed = align;
1822 : 38801594 : if (crtl->max_used_stack_slot_alignment < align)
1823 : 544641 : crtl->max_used_stack_slot_alignment = align;
1824 : 38801594 : }
1825 : :
1826 : : /* Create RTL for an SSA partition. */
1827 : :
1828 : : static void
1829 : 21351765 : expand_one_ssa_partition (tree var)
1830 : : {
1831 : 21351765 : int part = var_to_partition (SA.map, var);
1832 : 21351765 : gcc_assert (part != NO_PARTITION);
1833 : :
1834 : 21351765 : if (SA.partition_to_pseudo[part])
1835 : : return;
1836 : :
1837 : 21351765 : unsigned int align = MINIMUM_ALIGNMENT (TREE_TYPE (var),
1838 : : TYPE_MODE (TREE_TYPE (var)),
1839 : : TYPE_ALIGN (TREE_TYPE (var)));
1840 : :
1841 : : /* If the variable alignment is very large we'll dynamicaly allocate
1842 : : it, which means that in-frame portion is just a pointer. */
1843 : 21351765 : if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
1844 : 0 : align = GET_MODE_ALIGNMENT (Pmode);
1845 : :
1846 : 21351765 : record_alignment_for_reg_var (align);
1847 : :
1848 : 21351765 : if (!use_register_for_decl (var))
1849 : : {
1850 : 443659 : if (defer_stack_allocation (var, true))
1851 : 2837 : add_stack_var (var, true);
1852 : : else
1853 : 440822 : expand_one_stack_var_1 (var);
1854 : 443659 : return;
1855 : : }
1856 : :
1857 : 20908106 : machine_mode reg_mode = promote_ssa_mode (var, NULL);
1858 : 20908106 : rtx x = gen_reg_rtx (reg_mode);
1859 : :
1860 : 20908106 : set_rtl (var, x);
1861 : :
1862 : : /* For a promoted variable, X will not be used directly but wrapped in a
1863 : : SUBREG with SUBREG_PROMOTED_VAR_P set, which means that the RTL land
1864 : : will assume that its upper bits can be inferred from its lower bits.
1865 : : Therefore, if X isn't initialized on every path from the entry, then
1866 : : we must do it manually in order to fulfill the above assumption. */
1867 : 20908106 : if (reg_mode != TYPE_MODE (TREE_TYPE (var))
1868 : 20908106 : && bitmap_bit_p (SA.partitions_for_undefined_values, part))
1869 : 0 : emit_move_insn (x, CONST0_RTX (reg_mode));
1870 : : }
1871 : :
1872 : : /* Record the association between the RTL generated for partition PART
1873 : : and the underlying variable of the SSA_NAME VAR. */
1874 : :
1875 : : static void
1876 : 46516339 : adjust_one_expanded_partition_var (tree var)
1877 : : {
1878 : 46516339 : if (!var)
1879 : : return;
1880 : :
1881 : 46516339 : tree decl = SSA_NAME_VAR (var);
1882 : :
1883 : 46516339 : int part = var_to_partition (SA.map, var);
1884 : 46516339 : if (part == NO_PARTITION)
1885 : : return;
1886 : :
1887 : 30001728 : rtx x = SA.partition_to_pseudo[part];
1888 : :
1889 : 30001728 : gcc_assert (x);
1890 : :
1891 : 30001728 : set_rtl (var, x);
1892 : :
1893 : 30001728 : if (!REG_P (x))
1894 : : return;
1895 : :
1896 : : /* Note if the object is a user variable. */
1897 : 28033997 : if (decl && !DECL_ARTIFICIAL (decl))
1898 : 3981669 : mark_user_reg (x);
1899 : :
1900 : 28033997 : if (POINTER_TYPE_P (decl ? TREE_TYPE (decl) : TREE_TYPE (var)))
1901 : 7089298 : mark_reg_pointer (x, get_pointer_alignment (var));
1902 : : }
1903 : :
1904 : : /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
1905 : : that will reside in a pseudo register. */
1906 : :
1907 : : static void
1908 : 578678 : expand_one_register_var (tree var)
1909 : : {
1910 : 578678 : if (TREE_CODE (var) == SSA_NAME)
1911 : : {
1912 : 0 : int part = var_to_partition (SA.map, var);
1913 : 0 : if (part != NO_PARTITION)
1914 : : {
1915 : 0 : rtx x = SA.partition_to_pseudo[part];
1916 : 0 : gcc_assert (x);
1917 : 0 : gcc_assert (REG_P (x));
1918 : : return;
1919 : : }
1920 : 0 : gcc_unreachable ();
1921 : : }
1922 : :
1923 : 578678 : tree decl = var;
1924 : 578678 : tree type = TREE_TYPE (decl);
1925 : 578678 : machine_mode reg_mode = promote_decl_mode (decl, NULL);
1926 : 578678 : rtx x = gen_reg_rtx (reg_mode);
1927 : :
1928 : 578678 : set_rtl (var, x);
1929 : :
1930 : : /* Note if the object is a user variable. */
1931 : 578678 : if (!DECL_ARTIFICIAL (decl))
1932 : 105713 : mark_user_reg (x);
1933 : :
1934 : 578678 : if (POINTER_TYPE_P (type))
1935 : 169 : mark_reg_pointer (x, get_pointer_alignment (var));
1936 : : }
1937 : :
1938 : : /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL that
1939 : : has some associated error, e.g. its type is error-mark. We just need
1940 : : to pick something that won't crash the rest of the compiler. */
1941 : :
1942 : : static void
1943 : 37 : expand_one_error_var (tree var)
1944 : : {
1945 : 37 : machine_mode mode = DECL_MODE (var);
1946 : 37 : rtx x;
1947 : :
1948 : 37 : if (mode == BLKmode)
1949 : 1 : x = gen_rtx_MEM (BLKmode, const0_rtx);
1950 : 36 : else if (mode == VOIDmode)
1951 : 0 : x = const0_rtx;
1952 : : else
1953 : 36 : x = gen_reg_rtx (mode);
1954 : :
1955 : 37 : SET_DECL_RTL (var, x);
1956 : 37 : }
1957 : :
1958 : : /* A subroutine of expand_one_var. VAR is a variable that will be
1959 : : allocated to the local stack frame. Return true if we wish to
1960 : : add VAR to STACK_VARS so that it will be coalesced with other
1961 : : variables. Return false to allocate VAR immediately.
1962 : :
1963 : : This function is used to reduce the number of variables considered
1964 : : for coalescing, which reduces the size of the quadratic problem. */
1965 : :
1966 : : static bool
1967 : 6295950 : defer_stack_allocation (tree var, bool toplevel)
1968 : : {
1969 : 6295950 : tree size_unit = TREE_CODE (var) == SSA_NAME
1970 : 12148241 : ? TYPE_SIZE_UNIT (TREE_TYPE (var))
1971 : 5852291 : : DECL_SIZE_UNIT (var);
1972 : 6295950 : poly_uint64 size;
1973 : :
1974 : : /* Whether the variable is small enough for immediate allocation not to be
1975 : : a problem with regard to the frame size. */
1976 : 6295950 : bool smallish
1977 : 6295950 : = (poly_int_tree_p (size_unit, &size)
1978 : 6295950 : && (estimated_poly_value (size)
1979 : 6295950 : < param_min_size_for_stack_sharing));
1980 : :
1981 : : /* If stack protection is enabled, *all* stack variables must be deferred,
1982 : : so that we can re-order the strings to the top of the frame.
1983 : : Similarly for Address Sanitizer. */
1984 : 6295950 : if (flag_stack_protect || asan_sanitize_stack_p ())
1985 : 10948 : return true;
1986 : :
1987 : 6285002 : unsigned int align = TREE_CODE (var) == SSA_NAME
1988 : 6285002 : ? TYPE_ALIGN (TREE_TYPE (var))
1989 : 5841918 : : DECL_ALIGN (var);
1990 : :
1991 : : /* We handle "large" alignment via dynamic allocation. We want to handle
1992 : : this extra complication in only one place, so defer them. */
1993 : : if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
1994 : : return true;
1995 : :
1996 : 6285002 : bool ignored = TREE_CODE (var) == SSA_NAME
1997 : 6291321 : ? !SSAVAR (var) || DECL_IGNORED_P (SSA_NAME_VAR (var))
1998 : 5841918 : : DECL_IGNORED_P (var);
1999 : :
2000 : : /* When optimization is enabled, DECL_IGNORED_P variables originally scoped
2001 : : might be detached from their block and appear at toplevel when we reach
2002 : : here. We want to coalesce them with variables from other blocks when
2003 : : the immediate contribution to the frame size would be noticeable. */
2004 : 6285002 : if (toplevel && optimize > 0 && ignored && !smallish)
2005 : : return true;
2006 : :
2007 : : /* Variables declared in the outermost scope automatically conflict
2008 : : with every other variable. The only reason to want to defer them
2009 : : at all is that, after sorting, we can more efficiently pack
2010 : : small variables in the stack frame. Continue to defer at -O2. */
2011 : 4870839 : if (toplevel && optimize < 2)
2012 : : return false;
2013 : :
2014 : : /* Without optimization, *most* variables are allocated from the
2015 : : stack, which makes the quadratic problem large exactly when we
2016 : : want compilation to proceed as quickly as possible. On the
2017 : : other hand, we don't want the function's stack frame size to
2018 : : get completely out of hand. So we avoid adding scalars and
2019 : : "small" aggregates to the list at all. */
2020 : 4241578 : if (optimize == 0 && smallish)
2021 : : return false;
2022 : :
2023 : : return true;
2024 : : }
2025 : :
2026 : : /* A subroutine of expand_used_vars. Expand one variable according to
2027 : : its flavor. Variables to be placed on the stack are not actually
2028 : : expanded yet, merely recorded.
2029 : : When REALLY_EXPAND is false, only add stack values to be allocated.
2030 : : Return stack usage this variable is supposed to take.
2031 : : */
2032 : :
2033 : : static poly_uint64
2034 : 14784169 : expand_one_var (tree var, bool toplevel, bool really_expand,
2035 : : bitmap forced_stack_var = NULL)
2036 : : {
2037 : 14784169 : unsigned int align = BITS_PER_UNIT;
2038 : 14784169 : tree origvar = var;
2039 : :
2040 : 14784169 : var = SSAVAR (var);
2041 : :
2042 : 14784169 : if (TREE_TYPE (var) != error_mark_node && VAR_P (var))
2043 : : {
2044 : 14784169 : if (is_global_var (var))
2045 : 292008 : return 0;
2046 : :
2047 : : /* Because we don't know if VAR will be in register or on stack,
2048 : : we conservatively assume it will be on stack even if VAR is
2049 : : eventually put into register after RA pass. For non-automatic
2050 : : variables, which won't be on stack, we collect alignment of
2051 : : type and ignore user specified alignment. Similarly for
2052 : : SSA_NAMEs for which use_register_for_decl returns true. */
2053 : 14492161 : if (TREE_STATIC (var)
2054 : 14492161 : || DECL_EXTERNAL (var)
2055 : 28984322 : || (TREE_CODE (origvar) == SSA_NAME && use_register_for_decl (var)))
2056 : 0 : align = MINIMUM_ALIGNMENT (TREE_TYPE (var),
2057 : : TYPE_MODE (TREE_TYPE (var)),
2058 : : TYPE_ALIGN (TREE_TYPE (var)));
2059 : 14492161 : else if (DECL_HAS_VALUE_EXPR_P (var)
2060 : 14492161 : || (DECL_RTL_SET_P (var) && MEM_P (DECL_RTL (var))))
2061 : : /* Don't consider debug only variables with DECL_HAS_VALUE_EXPR_P set
2062 : : or variables which were assigned a stack slot already by
2063 : : expand_one_stack_var_at - in the latter case DECL_ALIGN has been
2064 : : changed from the offset chosen to it. */
2065 : 15711 : align = crtl->stack_alignment_estimated;
2066 : : else
2067 : 14476450 : align = MINIMUM_ALIGNMENT (var, DECL_MODE (var), DECL_ALIGN (var));
2068 : :
2069 : : /* If the variable alignment is very large we'll dynamicaly allocate
2070 : : it, which means that in-frame portion is just a pointer. */
2071 : 14492161 : if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
2072 : 0 : align = GET_MODE_ALIGNMENT (Pmode);
2073 : : }
2074 : :
2075 : 14492161 : record_alignment_for_reg_var (align);
2076 : :
2077 : 14492161 : poly_uint64 size;
2078 : 14492161 : if (TREE_CODE (origvar) == SSA_NAME)
2079 : : {
2080 : 0 : gcc_assert (!VAR_P (var)
2081 : : || (!DECL_EXTERNAL (var)
2082 : : && !DECL_HAS_VALUE_EXPR_P (var)
2083 : : && !TREE_STATIC (var)
2084 : : && TREE_TYPE (var) != error_mark_node
2085 : : && !DECL_HARD_REGISTER (var)
2086 : : && really_expand));
2087 : : }
2088 : 14492161 : if (!VAR_P (var) && TREE_CODE (origvar) != SSA_NAME)
2089 : : ;
2090 : 14492161 : else if (DECL_EXTERNAL (var))
2091 : : ;
2092 : 14492161 : else if (DECL_HAS_VALUE_EXPR_P (var))
2093 : : ;
2094 : 14476491 : else if (TREE_STATIC (var))
2095 : : ;
2096 : 14476491 : else if (TREE_CODE (origvar) != SSA_NAME && DECL_RTL_SET_P (var))
2097 : : ;
2098 : 14476408 : else if (TREE_TYPE (var) == error_mark_node)
2099 : : {
2100 : 0 : if (really_expand)
2101 : 0 : expand_one_error_var (var);
2102 : : }
2103 : 14476408 : else if (VAR_P (var) && DECL_HARD_REGISTER (var))
2104 : : {
2105 : 4108 : if (really_expand)
2106 : : {
2107 : 1085 : expand_one_hard_reg_var (var);
2108 : 1085 : if (!DECL_HARD_REGISTER (var))
2109 : : /* Invalid register specification. */
2110 : 37 : expand_one_error_var (var);
2111 : : }
2112 : : }
2113 : 14472300 : else if (use_register_for_decl (var)
2114 : 14472300 : && (!forced_stack_var
2115 : 588831 : || !bitmap_bit_p (forced_stack_var, DECL_UID (var))))
2116 : : {
2117 : 8620009 : if (really_expand)
2118 : 578678 : expand_one_register_var (origvar);
2119 : : }
2120 : 5852291 : else if (!poly_int_tree_p (DECL_SIZE_UNIT (var), &size)
2121 : 5852291 : || !valid_constant_size_p (DECL_SIZE_UNIT (var)))
2122 : : {
2123 : : /* Reject variables which cover more than half of the address-space. */
2124 : 0 : if (really_expand)
2125 : : {
2126 : 0 : if (DECL_NONLOCAL_FRAME (var))
2127 : 0 : error_at (DECL_SOURCE_LOCATION (current_function_decl),
2128 : : "total size of local objects is too large");
2129 : : else
2130 : 0 : error_at (DECL_SOURCE_LOCATION (var),
2131 : : "size of variable %q+D is too large", var);
2132 : 0 : expand_one_error_var (var);
2133 : : }
2134 : : }
2135 : 5852291 : else if (defer_stack_allocation (var, toplevel))
2136 : 5376802 : add_stack_var (origvar, really_expand);
2137 : : else
2138 : : {
2139 : 475489 : if (really_expand)
2140 : : {
2141 : 345382 : if (lookup_attribute ("naked",
2142 : 345382 : DECL_ATTRIBUTES (current_function_decl)))
2143 : 0 : error ("cannot allocate stack for variable %q+D, naked function",
2144 : : var);
2145 : :
2146 : 345382 : expand_one_stack_var (origvar);
2147 : : }
2148 : 475489 : return size;
2149 : : }
2150 : 14016672 : return 0;
2151 : : }
2152 : :
2153 : : /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
2154 : : expanding variables. Those variables that can be put into registers
2155 : : are allocated pseudos; those that can't are put on the stack.
2156 : :
2157 : : TOPLEVEL is true if this is the outermost BLOCK. */
2158 : :
2159 : : static void
2160 : 14761998 : expand_used_vars_for_block (tree block, bool toplevel, bitmap forced_stack_vars)
2161 : : {
2162 : 14761998 : tree t;
2163 : :
2164 : : /* Expand all variables at this level. */
2165 : 30657882 : for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
2166 : 15895884 : if (TREE_USED (t)
2167 : 15895884 : && ((!VAR_P (t) && TREE_CODE (t) != RESULT_DECL)
2168 : 756592 : || !DECL_NONSHAREABLE (t)))
2169 : 756592 : expand_one_var (t, toplevel, true, forced_stack_vars);
2170 : :
2171 : : /* Expand all variables at containing levels. */
2172 : 28091638 : for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
2173 : 13329640 : expand_used_vars_for_block (t, false, forced_stack_vars);
2174 : 14761998 : }
2175 : :
2176 : : /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
2177 : : and clear TREE_USED on all local variables. */
2178 : :
2179 : : static void
2180 : 14761998 : clear_tree_used (tree block)
2181 : : {
2182 : 14761998 : tree t;
2183 : :
2184 : 30657882 : for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
2185 : : /* if (!TREE_STATIC (t) && !DECL_EXTERNAL (t)) */
2186 : 877406 : if ((!VAR_P (t) && TREE_CODE (t) != RESULT_DECL)
2187 : 15895884 : || !DECL_NONSHAREABLE (t))
2188 : 15895884 : TREE_USED (t) = 0;
2189 : :
2190 : 28091638 : for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
2191 : 13329640 : clear_tree_used (t);
2192 : 14761998 : }
2193 : :
2194 : : /* Examine TYPE and determine a bit mask of the following features. */
2195 : :
2196 : : #define SPCT_HAS_LARGE_CHAR_ARRAY 1
2197 : : #define SPCT_HAS_SMALL_CHAR_ARRAY 2
2198 : : #define SPCT_HAS_ARRAY 4
2199 : : #define SPCT_HAS_AGGREGATE 8
2200 : :
2201 : : static unsigned int
2202 : 2582 : stack_protect_classify_type (tree type)
2203 : : {
2204 : 2582 : unsigned int ret = 0;
2205 : 2582 : tree t;
2206 : :
2207 : 2582 : switch (TREE_CODE (type))
2208 : : {
2209 : 586 : case ARRAY_TYPE:
2210 : 586 : t = TYPE_MAIN_VARIANT (TREE_TYPE (type));
2211 : 586 : if (t == char_type_node
2212 : 142 : || t == signed_char_type_node
2213 : 142 : || t == unsigned_char_type_node)
2214 : : {
2215 : 444 : unsigned HOST_WIDE_INT max = param_ssp_buffer_size;
2216 : 444 : unsigned HOST_WIDE_INT len;
2217 : :
2218 : 444 : if (!TYPE_SIZE_UNIT (type)
2219 : 444 : || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
2220 : : len = max;
2221 : : else
2222 : 444 : len = tree_to_uhwi (TYPE_SIZE_UNIT (type));
2223 : :
2224 : 444 : if (len < max)
2225 : : ret = SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_ARRAY;
2226 : : else
2227 : 420 : ret = SPCT_HAS_LARGE_CHAR_ARRAY | SPCT_HAS_ARRAY;
2228 : : }
2229 : : else
2230 : : ret = SPCT_HAS_ARRAY;
2231 : : break;
2232 : :
2233 : 1010 : case UNION_TYPE:
2234 : 1010 : case QUAL_UNION_TYPE:
2235 : 1010 : case RECORD_TYPE:
2236 : 1010 : ret = SPCT_HAS_AGGREGATE;
2237 : 10875 : for (t = TYPE_FIELDS (type); t ; t = TREE_CHAIN (t))
2238 : 9865 : if (TREE_CODE (t) == FIELD_DECL)
2239 : 1673 : ret |= stack_protect_classify_type (TREE_TYPE (t));
2240 : : break;
2241 : :
2242 : : default:
2243 : : break;
2244 : : }
2245 : :
2246 : 2582 : return ret;
2247 : : }
2248 : :
2249 : : /* Return nonzero if DECL should be segregated into the "vulnerable" upper
2250 : : part of the local stack frame. Remember if we ever return nonzero for
2251 : : any variable in this function. The return value is the phase number in
2252 : : which the variable should be allocated. */
2253 : :
2254 : : static int
2255 : 909 : stack_protect_decl_phase (tree decl)
2256 : : {
2257 : 909 : unsigned int bits = stack_protect_classify_type (TREE_TYPE (decl));
2258 : 909 : int ret = 0;
2259 : :
2260 : 909 : if (bits & SPCT_HAS_SMALL_CHAR_ARRAY)
2261 : 24 : has_short_buffer = true;
2262 : :
2263 : 909 : tree attribs = DECL_ATTRIBUTES (current_function_decl);
2264 : 909 : if (!lookup_attribute ("no_stack_protector", attribs)
2265 : 909 : && (flag_stack_protect == SPCT_FLAG_ALL
2266 : 909 : || flag_stack_protect == SPCT_FLAG_STRONG
2267 : 147 : || (flag_stack_protect == SPCT_FLAG_EXPLICIT
2268 : 5 : && lookup_attribute ("stack_protect", attribs))))
2269 : : {
2270 : 767 : if ((bits & (SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_LARGE_CHAR_ARRAY))
2271 : 344 : && !(bits & SPCT_HAS_AGGREGATE))
2272 : : ret = 1;
2273 : 711 : else if (bits & SPCT_HAS_ARRAY)
2274 : : ret = 2;
2275 : : }
2276 : : else
2277 : 142 : ret = (bits & SPCT_HAS_LARGE_CHAR_ARRAY) != 0;
2278 : :
2279 : 142 : if (ret)
2280 : 576 : has_protected_decls = true;
2281 : :
2282 : 909 : return ret;
2283 : : }
2284 : :
2285 : : /* Two helper routines that check for phase 1 and phase 2. These are used
2286 : : as callbacks for expand_stack_vars. */
2287 : :
2288 : : static bool
2289 : 294 : stack_protect_decl_phase_1 (unsigned i)
2290 : : {
2291 : 294 : return stack_protect_decl_phase (stack_vars[i].decl) == 1;
2292 : : }
2293 : :
2294 : : static bool
2295 : 204 : stack_protect_decl_phase_2 (unsigned i)
2296 : : {
2297 : 204 : return stack_protect_decl_phase (stack_vars[i].decl) == 2;
2298 : : }
2299 : :
2300 : : /* And helper function that checks for asan phase (with stack protector
2301 : : it is phase 3). This is used as callback for expand_stack_vars.
2302 : : Returns true if any of the vars in the partition need to be protected. */
2303 : :
2304 : : static bool
2305 : 3195 : asan_decl_phase_3 (unsigned i)
2306 : : {
2307 : 3817 : while (i != EOC)
2308 : : {
2309 : 3195 : if (asan_protect_stack_decl (stack_vars[i].decl))
2310 : : return true;
2311 : 622 : i = stack_vars[i].next;
2312 : : }
2313 : : return false;
2314 : : }
2315 : :
2316 : : /* Ensure that variables in different stack protection phases conflict
2317 : : so that they are not merged and share the same stack slot.
2318 : : Return true if there are any address taken variables. */
2319 : :
2320 : : static bool
2321 : 210 : add_stack_protection_conflicts (void)
2322 : : {
2323 : 210 : unsigned i, j, n = stack_vars_num;
2324 : 210 : unsigned char *phase;
2325 : 210 : bool ret = false;
2326 : :
2327 : 210 : phase = XNEWVEC (unsigned char, n);
2328 : 831 : for (i = 0; i < n; ++i)
2329 : : {
2330 : 411 : phase[i] = stack_protect_decl_phase (stack_vars[i].decl);
2331 : 411 : if (TREE_ADDRESSABLE (stack_vars[i].decl))
2332 : 338 : ret = true;
2333 : : }
2334 : :
2335 : 621 : for (i = 0; i < n; ++i)
2336 : : {
2337 : 411 : unsigned char ph_i = phase[i];
2338 : 1114 : for (j = i + 1; j < n; ++j)
2339 : 703 : if (ph_i != phase[j])
2340 : 319 : add_stack_var_conflict (i, j);
2341 : : }
2342 : :
2343 : 210 : XDELETEVEC (phase);
2344 : 210 : return ret;
2345 : : }
2346 : :
2347 : : /* Create a decl for the guard at the top of the stack frame. */
2348 : :
2349 : : static void
2350 : 227 : create_stack_guard (void)
2351 : : {
2352 : 227 : tree guard = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
2353 : : VAR_DECL, NULL, ptr_type_node);
2354 : 227 : TREE_THIS_VOLATILE (guard) = 1;
2355 : 227 : TREE_USED (guard) = 1;
2356 : 227 : expand_one_stack_var (guard);
2357 : 227 : crtl->stack_protect_guard = guard;
2358 : 227 : }
2359 : :
2360 : : /* Prepare for expanding variables. */
2361 : : static void
2362 : 7289863 : init_vars_expansion (void)
2363 : : {
2364 : : /* Conflict bitmaps, and a few related temporary bitmaps, go here. */
2365 : 7289863 : bitmap_obstack_initialize (&stack_var_bitmap_obstack);
2366 : :
2367 : : /* A map from decl to stack partition. */
2368 : 7289863 : decl_to_stack_part = new hash_map<tree, unsigned>;
2369 : :
2370 : : /* Initialize local stack smashing state. */
2371 : 7289863 : has_protected_decls = false;
2372 : 7289863 : has_short_buffer = false;
2373 : 7289863 : if (hwasan_sanitize_stack_p ())
2374 : 1206 : hwasan_record_frame_init ();
2375 : 7289863 : }
2376 : :
2377 : : /* Free up stack variable graph data. */
2378 : : static void
2379 : 7289863 : fini_vars_expansion (void)
2380 : : {
2381 : 7289863 : bitmap_obstack_release (&stack_var_bitmap_obstack);
2382 : 7289863 : if (stack_vars)
2383 : 1222845 : XDELETEVEC (stack_vars);
2384 : 7289863 : if (stack_vars_sorted)
2385 : 1222845 : XDELETEVEC (stack_vars_sorted);
2386 : 7289863 : stack_vars = NULL;
2387 : 7289863 : stack_vars_sorted = NULL;
2388 : 7289863 : stack_vars_alloc = stack_vars_num = 0;
2389 : 14579726 : delete decl_to_stack_part;
2390 : 7289863 : decl_to_stack_part = NULL;
2391 : 7289863 : }
2392 : :
2393 : : /* Make a fair guess for the size of the stack frame of the function
2394 : : in NODE. This doesn't have to be exact, the result is only used in
2395 : : the inline heuristics. So we don't want to run the full stack var
2396 : : packing algorithm (which is quadratic in the number of stack vars).
2397 : : Instead, we calculate the total size of all stack vars. This turns
2398 : : out to be a pretty fair estimate -- packing of stack vars doesn't
2399 : : happen very often. */
2400 : :
2401 : : HOST_WIDE_INT
2402 : 5857505 : estimated_stack_frame_size (struct cgraph_node *node)
2403 : : {
2404 : 5857505 : poly_int64 size = 0;
2405 : 5857505 : unsigned i;
2406 : 5857505 : tree var;
2407 : 5857505 : struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
2408 : :
2409 : 5857505 : push_cfun (fn);
2410 : :
2411 : 5857505 : init_vars_expansion ();
2412 : :
2413 : 23620882 : FOR_EACH_LOCAL_DECL (fn, i, var)
2414 : 12987853 : if (auto_var_in_fn_p (var, fn->decl))
2415 : 12420570 : size += expand_one_var (var, true, false);
2416 : :
2417 : 5857505 : if (stack_vars_num > 0)
2418 : : {
2419 : : /* Fake sorting the stack vars for account_stack_vars (). */
2420 : 998060 : stack_vars_sorted = XNEWVEC (unsigned , stack_vars_num);
2421 : 5244127 : for (i = 0; i < stack_vars_num; ++i)
2422 : 4246067 : stack_vars_sorted[i] = i;
2423 : 998060 : size += account_stack_vars ();
2424 : : }
2425 : :
2426 : 5857505 : fini_vars_expansion ();
2427 : 5857505 : pop_cfun ();
2428 : 5857505 : return estimated_poly_value (size);
2429 : : }
2430 : :
2431 : : /* Check if the current function has calls that use a return slot. */
2432 : :
2433 : : static bool
2434 : 399 : stack_protect_return_slot_p ()
2435 : : {
2436 : 399 : basic_block bb;
2437 : :
2438 : 1750 : FOR_ALL_BB_FN (bb, cfun)
2439 : 2758 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
2440 : 3626 : !gsi_end_p (gsi); gsi_next (&gsi))
2441 : : {
2442 : 2275 : gimple *stmt = gsi_stmt (gsi);
2443 : : /* This assumes that calls to internal-only functions never
2444 : : use a return slot. */
2445 : 2275 : if (is_gimple_call (stmt)
2446 : 460 : && !gimple_call_internal_p (stmt)
2447 : 2727 : && aggregate_value_p (TREE_TYPE (gimple_call_fntype (stmt)),
2448 : 452 : gimple_call_fndecl (stmt)))
2449 : 399 : return true;
2450 : : }
2451 : : return false;
2452 : : }
2453 : :
2454 : : /* Expand all variables used in the function. */
2455 : :
2456 : : static rtx_insn *
2457 : 1432358 : expand_used_vars (bitmap forced_stack_vars)
2458 : : {
2459 : 1432358 : tree var, outer_block = DECL_INITIAL (current_function_decl);
2460 : 1432358 : auto_vec<tree> maybe_local_decls;
2461 : 1432358 : rtx_insn *var_end_seq = NULL;
2462 : 1432358 : unsigned i;
2463 : 1432358 : unsigned len;
2464 : 1432358 : bool gen_stack_protect_signal = false;
2465 : :
2466 : : /* Compute the phase of the stack frame for this function. */
2467 : 1432358 : {
2468 : 1432358 : int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
2469 : 1432358 : int off = targetm.starting_frame_offset () % align;
2470 : 1432358 : frame_phase = off ? align - off : 0;
2471 : : }
2472 : :
2473 : : /* Set TREE_USED on all variables in the local_decls. */
2474 : 10152678 : FOR_EACH_LOCAL_DECL (cfun, i, var)
2475 : 7522756 : TREE_USED (var) = 1;
2476 : : /* Clear TREE_USED on all variables associated with a block scope. */
2477 : 1432358 : clear_tree_used (DECL_INITIAL (current_function_decl));
2478 : :
2479 : 1432358 : init_vars_expansion ();
2480 : :
2481 : 1432358 : if (targetm.use_pseudo_pic_reg ())
2482 : 39620 : pic_offset_table_rtx = gen_reg_rtx (Pmode);
2483 : :
2484 : 26331155 : for (i = 0; i < SA.map->num_partitions; i++)
2485 : : {
2486 : 24898797 : if (bitmap_bit_p (SA.partitions_for_parm_default_defs, i))
2487 : 3547032 : continue;
2488 : :
2489 : 21351765 : tree var = partition_to_var (SA.map, i);
2490 : :
2491 : 42703530 : gcc_assert (!virtual_operand_p (var));
2492 : :
2493 : 21351765 : expand_one_ssa_partition (var);
2494 : : }
2495 : :
2496 : 1432358 : if (flag_stack_protect == SPCT_FLAG_STRONG)
2497 : 399 : gen_stack_protect_signal = stack_protect_return_slot_p ();
2498 : :
2499 : : /* At this point all variables on the local_decls with TREE_USED
2500 : : set are not associated with any block scope. Lay them out. */
2501 : :
2502 : 1432358 : len = vec_safe_length (cfun->local_decls);
2503 : 8955114 : FOR_EACH_LOCAL_DECL (cfun, i, var)
2504 : : {
2505 : 7522756 : bool expand_now = false;
2506 : :
2507 : : /* Expanded above already. */
2508 : 7522756 : if (is_gimple_reg (var))
2509 : : {
2510 : 5242551 : TREE_USED (var) = 0;
2511 : 5242551 : goto next;
2512 : : }
2513 : : /* We didn't set a block for static or extern because it's hard
2514 : : to tell the difference between a global variable (re)declared
2515 : : in a local scope, and one that's really declared there to
2516 : : begin with. And it doesn't really matter much, since we're
2517 : : not giving them stack space. Expand them now. */
2518 : 2280205 : else if (TREE_STATIC (var) || DECL_EXTERNAL (var))
2519 : : expand_now = true;
2520 : :
2521 : : /* Expand variables not associated with any block now. Those created by
2522 : : the optimizers could be live anywhere in the function. Those that
2523 : : could possibly have been scoped originally and detached from their
2524 : : block will have their allocation deferred so we coalesce them with
2525 : : others when optimization is enabled. */
2526 : 2071591 : else if (TREE_USED (var))
2527 : 1607007 : expand_now = true;
2528 : :
2529 : : /* Finally, mark all variables on the list as used. We'll use
2530 : : this in a moment when we expand those associated with scopes. */
2531 : 2280205 : TREE_USED (var) = 1;
2532 : :
2533 : 2280205 : if (expand_now)
2534 : 1607007 : expand_one_var (var, true, true, forced_stack_vars);
2535 : :
2536 : 673198 : next:
2537 : 7522756 : if (DECL_ARTIFICIAL (var) && !DECL_IGNORED_P (var))
2538 : : {
2539 : 412875 : rtx rtl = DECL_RTL_IF_SET (var);
2540 : :
2541 : : /* Keep artificial non-ignored vars in cfun->local_decls
2542 : : chain until instantiate_decls. */
2543 : 311543 : if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
2544 : 19498 : add_local_decl (cfun, var);
2545 : 101332 : else if (rtl == NULL_RTX)
2546 : : /* If rtl isn't set yet, which can happen e.g. with
2547 : : -fstack-protector, retry before returning from this
2548 : : function. */
2549 : 101332 : maybe_local_decls.safe_push (var);
2550 : : }
2551 : : }
2552 : :
2553 : : /* We duplicated some of the decls in CFUN->LOCAL_DECLS.
2554 : :
2555 : : +-----------------+-----------------+
2556 : : | ...processed... | ...duplicates...|
2557 : : +-----------------+-----------------+
2558 : : ^
2559 : : +-- LEN points here.
2560 : :
2561 : : We just want the duplicates, as those are the artificial
2562 : : non-ignored vars that we want to keep until instantiate_decls.
2563 : : Move them down and truncate the array. */
2564 : 1432358 : if (!vec_safe_is_empty (cfun->local_decls))
2565 : 807750 : cfun->local_decls->block_remove (0, len);
2566 : :
2567 : : /* At this point, all variables within the block tree with TREE_USED
2568 : : set are actually used by the optimized function. Lay them out. */
2569 : 1432358 : expand_used_vars_for_block (outer_block, true, forced_stack_vars);
2570 : :
2571 : 1432358 : tree attribs = DECL_ATTRIBUTES (current_function_decl);
2572 : 1432358 : if (stack_vars_num > 0)
2573 : : {
2574 : 224785 : bool has_addressable_vars = false;
2575 : :
2576 : 224785 : add_scope_conflicts ();
2577 : :
2578 : : /* If stack protection is enabled, we don't share space between
2579 : : vulnerable data and non-vulnerable data. */
2580 : 224785 : if (flag_stack_protect != 0
2581 : 210 : && !lookup_attribute ("no_stack_protector", attribs)
2582 : 224995 : && (flag_stack_protect != SPCT_FLAG_EXPLICIT
2583 : : || (flag_stack_protect == SPCT_FLAG_EXPLICIT
2584 : 1 : && lookup_attribute ("stack_protect", attribs))))
2585 : 210 : has_addressable_vars = add_stack_protection_conflicts ();
2586 : :
2587 : 224785 : if (flag_stack_protect == SPCT_FLAG_STRONG && has_addressable_vars)
2588 : 224785 : gen_stack_protect_signal = true;
2589 : :
2590 : : /* Now that we have collected all stack variables, and have computed a
2591 : : minimal interference graph, attempt to save some stack space. */
2592 : 224785 : partition_stack_vars ();
2593 : 224785 : if (dump_file)
2594 : 29 : dump_stack_var_partition ();
2595 : : }
2596 : :
2597 : :
2598 : 1432358 : if (!lookup_attribute ("no_stack_protector", attribs))
2599 : 1432349 : switch (flag_stack_protect)
2600 : : {
2601 : 19 : case SPCT_FLAG_ALL:
2602 : 19 : create_stack_guard ();
2603 : 19 : break;
2604 : :
2605 : 399 : case SPCT_FLAG_STRONG:
2606 : 399 : if (gen_stack_protect_signal
2607 : 264 : || cfun->calls_alloca
2608 : 262 : || has_protected_decls
2609 : 653 : || lookup_attribute ("stack_protect", attribs))
2610 : 145 : create_stack_guard ();
2611 : : break;
2612 : :
2613 : 139 : case SPCT_FLAG_DEFAULT:
2614 : 139 : if (cfun->calls_alloca
2615 : 136 : || has_protected_decls
2616 : 225 : || lookup_attribute ("stack_protect", attribs))
2617 : 53 : create_stack_guard ();
2618 : : break;
2619 : :
2620 : 14 : case SPCT_FLAG_EXPLICIT:
2621 : 14 : if (lookup_attribute ("stack_protect", attribs))
2622 : 10 : create_stack_guard ();
2623 : : break;
2624 : :
2625 : : default:
2626 : : break;
2627 : : }
2628 : :
2629 : : /* Assign rtl to each variable based on these partitions. */
2630 : 1432358 : if (stack_vars_num > 0)
2631 : : {
2632 : 224785 : class stack_vars_data data;
2633 : :
2634 : 224785 : data.asan_base = NULL_RTX;
2635 : 224785 : data.asan_alignb = 0;
2636 : :
2637 : : /* Reorder decls to be protected by iterating over the variables
2638 : : array multiple times, and allocating out of each phase in turn. */
2639 : : /* ??? We could probably integrate this into the qsort we did
2640 : : earlier, such that we naturally see these variables first,
2641 : : and thus naturally allocate things in the right order. */
2642 : 224785 : if (has_protected_decls)
2643 : : {
2644 : : /* Phase 1 contains only character arrays. */
2645 : 117 : expand_stack_vars (stack_protect_decl_phase_1, &data);
2646 : :
2647 : : /* Phase 2 contains other kinds of arrays. */
2648 : 117 : if (!lookup_attribute ("no_stack_protector", attribs)
2649 : 117 : && (flag_stack_protect == SPCT_FLAG_ALL
2650 : 117 : || flag_stack_protect == SPCT_FLAG_STRONG
2651 : 51 : || (flag_stack_protect == SPCT_FLAG_EXPLICIT
2652 : 1 : && lookup_attribute ("stack_protect", attribs))))
2653 : 67 : expand_stack_vars (stack_protect_decl_phase_2, &data);
2654 : : }
2655 : :
2656 : 224785 : if (asan_sanitize_stack_p ())
2657 : : /* Phase 3, any partitions that need asan protection
2658 : : in addition to phase 1 and 2. */
2659 : 1623 : expand_stack_vars (asan_decl_phase_3, &data);
2660 : :
2661 : : /* ASAN description strings don't yet have a syntax for expressing
2662 : : polynomial offsets. */
2663 : 224785 : HOST_WIDE_INT prev_offset;
2664 : 226221 : if (!data.asan_vec.is_empty ()
2665 : 1436 : && frame_offset.is_constant (&prev_offset))
2666 : : {
2667 : 1436 : HOST_WIDE_INT offset, sz, redzonesz;
2668 : 1436 : redzonesz = ASAN_RED_ZONE_SIZE;
2669 : 1436 : sz = data.asan_vec[0] - prev_offset;
2670 : 1436 : if (data.asan_alignb > ASAN_RED_ZONE_SIZE
2671 : 1436 : && data.asan_alignb <= 4096
2672 : 45 : && sz + ASAN_RED_ZONE_SIZE >= (int) data.asan_alignb)
2673 : 45 : redzonesz = ((sz + ASAN_RED_ZONE_SIZE + data.asan_alignb - 1)
2674 : 45 : & ~(data.asan_alignb - HOST_WIDE_INT_1)) - sz;
2675 : : /* Allocating a constant amount of space from a constant
2676 : : starting offset must give a constant result. */
2677 : 1436 : offset = (alloc_stack_frame_space (redzonesz, ASAN_RED_ZONE_SIZE)
2678 : 1436 : .to_constant ());
2679 : 1436 : data.asan_vec.safe_push (prev_offset);
2680 : 1436 : data.asan_vec.safe_push (offset);
2681 : : /* Leave space for alignment if STRICT_ALIGNMENT. */
2682 : 1436 : if (STRICT_ALIGNMENT)
2683 : : alloc_stack_frame_space ((GET_MODE_ALIGNMENT (SImode)
2684 : : << ASAN_SHADOW_SHIFT)
2685 : : / BITS_PER_UNIT, 1);
2686 : :
2687 : 1436 : var_end_seq
2688 : 2872 : = asan_emit_stack_protection (virtual_stack_vars_rtx,
2689 : : data.asan_base,
2690 : : data.asan_alignb,
2691 : : data.asan_vec.address (),
2692 : : data.asan_decl_vec.address (),
2693 : 1436 : data.asan_vec.length ());
2694 : : }
2695 : :
2696 : 224785 : expand_stack_vars (NULL, &data);
2697 : 224785 : }
2698 : :
2699 : 1432358 : if (hwasan_sanitize_stack_p ())
2700 : 462 : hwasan_emit_prologue ();
2701 : 1432358 : if (asan_sanitize_allocas_p () && cfun->calls_alloca)
2702 : 178 : var_end_seq = asan_emit_allocas_unpoison (virtual_stack_dynamic_rtx,
2703 : : virtual_stack_vars_rtx,
2704 : : var_end_seq);
2705 : 1432180 : else if (hwasan_sanitize_allocas_p () && cfun->calls_alloca)
2706 : : /* When using out-of-line instrumentation we only want to emit one function
2707 : : call for clearing the tags in a region of shadow stack. When there are
2708 : : alloca calls in this frame we want to emit a call using the
2709 : : virtual_stack_dynamic_rtx, but when not we use the hwasan_frame_extent
2710 : : rtx we created in expand_stack_vars. */
2711 : 0 : var_end_seq = hwasan_emit_untag_frame (virtual_stack_dynamic_rtx,
2712 : : virtual_stack_vars_rtx);
2713 : 1432180 : else if (hwasan_sanitize_stack_p ())
2714 : : /* If no variables were stored on the stack, `hwasan_get_frame_extent`
2715 : : will return NULL_RTX and hence `hwasan_emit_untag_frame` will return
2716 : : NULL (i.e. an empty sequence). */
2717 : 462 : var_end_seq = hwasan_emit_untag_frame (hwasan_get_frame_extent (),
2718 : : virtual_stack_vars_rtx);
2719 : :
2720 : 1432358 : fini_vars_expansion ();
2721 : :
2722 : : /* If there were any artificial non-ignored vars without rtl
2723 : : found earlier, see if deferred stack allocation hasn't assigned
2724 : : rtl to them. */
2725 : 1575289 : FOR_EACH_VEC_ELT_REVERSE (maybe_local_decls, i, var)
2726 : : {
2727 : 105063 : rtx rtl = DECL_RTL_IF_SET (var);
2728 : :
2729 : : /* Keep artificial non-ignored vars in cfun->local_decls
2730 : : chain until instantiate_decls. */
2731 : 3731 : if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
2732 : 3585 : add_local_decl (cfun, var);
2733 : : }
2734 : :
2735 : : /* If the target requires that FRAME_OFFSET be aligned, do it. */
2736 : 1432358 : if (STACK_ALIGNMENT_NEEDED)
2737 : : {
2738 : 1432358 : HOST_WIDE_INT align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
2739 : 1432358 : if (FRAME_GROWS_DOWNWARD)
2740 : 1432358 : frame_offset = aligned_lower_bound (frame_offset, align);
2741 : : else
2742 : : frame_offset = aligned_upper_bound (frame_offset, align);
2743 : : }
2744 : :
2745 : 1432358 : return var_end_seq;
2746 : 1432358 : }
2747 : :
2748 : :
2749 : : /* If we need to produce a detailed dump, print the tree representation
2750 : : for STMT to the dump file. SINCE is the last RTX after which the RTL
2751 : : generated for STMT should have been appended. */
2752 : :
2753 : : static void
2754 : 39833200 : maybe_dump_rtl_for_gimple_stmt (gimple *stmt, rtx_insn *since)
2755 : : {
2756 : 39833200 : if (dump_file && (dump_flags & TDF_DETAILS))
2757 : : {
2758 : 318 : fprintf (dump_file, "\n;; ");
2759 : 318 : print_gimple_stmt (dump_file, stmt, 0,
2760 : : TDF_SLIM | (dump_flags & TDF_LINENO));
2761 : 318 : fprintf (dump_file, "\n");
2762 : :
2763 : 318 : print_rtl (dump_file, since ? NEXT_INSN (since) : since);
2764 : : }
2765 : 39833200 : }
2766 : :
2767 : : /* Maps the blocks that do not contain tree labels to rtx labels. */
2768 : :
2769 : : static hash_map<basic_block, rtx_code_label *> *lab_rtx_for_bb;
2770 : :
2771 : : /* Returns the label_rtx expression for a label starting basic block BB. */
2772 : :
2773 : : static rtx_code_label *
2774 : 7267350 : label_rtx_for_bb (basic_block bb ATTRIBUTE_UNUSED)
2775 : : {
2776 : 7267350 : if (bb->flags & BB_RTL)
2777 : 1583730 : return block_label (bb);
2778 : :
2779 : 5683620 : rtx_code_label **elt = lab_rtx_for_bb->get (bb);
2780 : 5683620 : if (elt)
2781 : 1121555 : return *elt;
2782 : :
2783 : : /* Find the tree label if it is present. */
2784 : 4562065 : gimple_stmt_iterator gsi = gsi_start_bb (bb);
2785 : 4562065 : glabel *lab_stmt;
2786 : 4562065 : if (!gsi_end_p (gsi)
2787 : 4435169 : && (lab_stmt = dyn_cast <glabel *> (gsi_stmt (gsi)))
2788 : 4771211 : && !DECL_NONLOCAL (gimple_label_label (lab_stmt)))
2789 : 209146 : return jump_target_rtx (gimple_label_label (lab_stmt));
2790 : :
2791 : 4352919 : rtx_code_label *l = gen_label_rtx ();
2792 : 4352919 : lab_rtx_for_bb->put (bb, l);
2793 : 4352919 : return l;
2794 : : }
2795 : :
2796 : :
2797 : : /* A subroutine of expand_gimple_cond. Given E, a fallthrough edge
2798 : : of a basic block where we just expanded the conditional at the end,
2799 : : possibly clean up the CFG and instruction sequence. LAST is the
2800 : : last instruction before the just emitted jump sequence. */
2801 : :
2802 : : static void
2803 : 4858893 : maybe_cleanup_end_of_block (edge e, rtx_insn *last)
2804 : : {
2805 : : /* Special case: when jumpif decides that the condition is
2806 : : trivial it emits an unconditional jump (and the necessary
2807 : : barrier). But we still have two edges, the fallthru one is
2808 : : wrong. purge_dead_edges would clean this up later. Unfortunately
2809 : : we have to insert insns (and split edges) before
2810 : : find_many_sub_basic_blocks and hence before purge_dead_edges.
2811 : : But splitting edges might create new blocks which depend on the
2812 : : fact that if there are two edges there's no barrier. So the
2813 : : barrier would get lost and verify_flow_info would ICE. Instead
2814 : : of auditing all edge splitters to care for the barrier (which
2815 : : normally isn't there in a cleaned CFG), fix it here. */
2816 : 4858893 : if (BARRIER_P (get_last_insn ()))
2817 : : {
2818 : 61 : rtx_insn *insn;
2819 : 61 : remove_edge (e);
2820 : : /* Now, we have a single successor block, if we have insns to
2821 : : insert on the remaining edge we potentially will insert
2822 : : it at the end of this block (if the dest block isn't feasible)
2823 : : in order to avoid splitting the edge. This insertion will take
2824 : : place in front of the last jump. But we might have emitted
2825 : : multiple jumps (conditional and one unconditional) to the
2826 : : same destination. Inserting in front of the last one then
2827 : : is a problem. See PR 40021. We fix this by deleting all
2828 : : jumps except the last unconditional one. */
2829 : 61 : insn = PREV_INSN (get_last_insn ());
2830 : : /* Make sure we have an unconditional jump. Otherwise we're
2831 : : confused. */
2832 : 61 : gcc_assert (JUMP_P (insn) && !any_condjump_p (insn));
2833 : 122 : for (insn = PREV_INSN (insn); insn != last;)
2834 : : {
2835 : 0 : insn = PREV_INSN (insn);
2836 : 0 : if (JUMP_P (NEXT_INSN (insn)))
2837 : : {
2838 : 0 : if (!any_condjump_p (NEXT_INSN (insn)))
2839 : : {
2840 : 0 : gcc_assert (BARRIER_P (NEXT_INSN (NEXT_INSN (insn))));
2841 : 0 : delete_insn (NEXT_INSN (NEXT_INSN (insn)));
2842 : : }
2843 : 0 : delete_insn (NEXT_INSN (insn));
2844 : : }
2845 : : }
2846 : : }
2847 : 4858893 : }
2848 : :
2849 : : /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_COND.
2850 : : Returns a new basic block if we've terminated the current basic
2851 : : block and created a new one. */
2852 : :
2853 : : static basic_block
2854 : 5309089 : expand_gimple_cond (basic_block bb, gcond *stmt)
2855 : : {
2856 : 5309089 : basic_block new_bb, dest;
2857 : 5309089 : edge true_edge;
2858 : 5309089 : edge false_edge;
2859 : 5309089 : rtx_insn *last2, *last;
2860 : 5309089 : enum tree_code code;
2861 : 5309089 : tree op0, op1;
2862 : :
2863 : 5309089 : code = gimple_cond_code (stmt);
2864 : 5309089 : op0 = gimple_cond_lhs (stmt);
2865 : 5309089 : op1 = gimple_cond_rhs (stmt);
2866 : : /* We're sometimes presented with such code:
2867 : : D.123_1 = x < y;
2868 : : if (D.123_1 != 0)
2869 : : ...
2870 : : This would expand to two comparisons which then later might
2871 : : be cleaned up by combine. But some pattern matchers like if-conversion
2872 : : work better when there's only one compare, so make up for this
2873 : : here as special exception if TER would have made the same change. */
2874 : 5309089 : if (SA.values
2875 : 3818700 : && TREE_CODE (op0) == SSA_NAME
2876 : 3747316 : && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
2877 : 360822 : && TREE_CODE (op1) == INTEGER_CST
2878 : 351362 : && ((gimple_cond_code (stmt) == NE_EXPR
2879 : 351345 : && integer_zerop (op1))
2880 : 17 : || (gimple_cond_code (stmt) == EQ_EXPR
2881 : 17 : && integer_onep (op1)))
2882 : 5660451 : && bitmap_bit_p (SA.values, SSA_NAME_VERSION (op0)))
2883 : : {
2884 : 192965 : gimple *second = SSA_NAME_DEF_STMT (op0);
2885 : 192965 : if (gimple_code (second) == GIMPLE_ASSIGN)
2886 : : {
2887 : 192965 : enum tree_code code2 = gimple_assign_rhs_code (second);
2888 : 192965 : if (TREE_CODE_CLASS (code2) == tcc_comparison)
2889 : : {
2890 : 641 : code = code2;
2891 : 641 : op0 = gimple_assign_rhs1 (second);
2892 : 641 : op1 = gimple_assign_rhs2 (second);
2893 : : }
2894 : : /* If jumps are cheap and the target does not support conditional
2895 : : compare, turn some more codes into jumpy sequences. */
2896 : 192324 : else if (BRANCH_COST (optimize_insn_for_speed_p (), false) < 4
2897 : 192324 : && !targetm.have_ccmp ())
2898 : : {
2899 : 192321 : if ((code2 == BIT_AND_EXPR
2900 : 55608 : && TYPE_PRECISION (TREE_TYPE (op0)) == 1
2901 : 55608 : && TREE_CODE (gimple_assign_rhs2 (second)) != INTEGER_CST)
2902 : 192321 : || code2 == TRUTH_AND_EXPR)
2903 : : {
2904 : 55608 : code = TRUTH_ANDIF_EXPR;
2905 : 55608 : op0 = gimple_assign_rhs1 (second);
2906 : 55608 : op1 = gimple_assign_rhs2 (second);
2907 : : }
2908 : 136713 : else if (code2 == BIT_IOR_EXPR || code2 == TRUTH_OR_EXPR)
2909 : : {
2910 : 62086 : code = TRUTH_ORIF_EXPR;
2911 : 62086 : op0 = gimple_assign_rhs1 (second);
2912 : 62086 : op1 = gimple_assign_rhs2 (second);
2913 : : }
2914 : : }
2915 : : }
2916 : : }
2917 : :
2918 : : /* Optimize (x % C1) == C2 or (x % C1) != C2 if it is beneficial
2919 : : into (x - C2) * C3 < C4. */
2920 : 5309089 : if ((code == EQ_EXPR || code == NE_EXPR)
2921 : 4167045 : && TREE_CODE (op0) == SSA_NAME
2922 : 4094558 : && TREE_CODE (op1) == INTEGER_CST)
2923 : 2925603 : code = maybe_optimize_mod_cmp (code, &op0, &op1);
2924 : :
2925 : : /* Optimize (x - y) < 0 into x < y if x - y has undefined overflow. */
2926 : 5309089 : if (!TYPE_UNSIGNED (TREE_TYPE (op0))
2927 : 2194104 : && (code == LT_EXPR || code == LE_EXPR
2928 : 2194104 : || code == GT_EXPR || code == GE_EXPR)
2929 : 660981 : && integer_zerop (op1)
2930 : 5499624 : && TREE_CODE (op0) == SSA_NAME)
2931 : 190535 : maybe_optimize_sub_cmp_0 (code, &op0, &op1);
2932 : :
2933 : 5309089 : last2 = last = get_last_insn ();
2934 : :
2935 : 5309089 : extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
2936 : 5309089 : set_curr_insn_location (gimple_location (stmt));
2937 : :
2938 : : /* These flags have no purpose in RTL land. */
2939 : 5309089 : true_edge->flags &= ~EDGE_TRUE_VALUE;
2940 : 5309089 : false_edge->flags &= ~EDGE_FALSE_VALUE;
2941 : :
2942 : : /* We can either have a pure conditional jump with one fallthru edge or
2943 : : two-way jump that needs to be decomposed into two basic blocks. */
2944 : 5309089 : if (false_edge->dest == bb->next_bb)
2945 : : {
2946 : 2089966 : jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
2947 : : true_edge->probability);
2948 : 2089966 : maybe_dump_rtl_for_gimple_stmt (stmt, last);
2949 : 2089966 : if (true_edge->goto_locus != UNKNOWN_LOCATION)
2950 : 1489521 : set_curr_insn_location (true_edge->goto_locus);
2951 : 2089966 : false_edge->flags |= EDGE_FALLTHRU;
2952 : 2089966 : maybe_cleanup_end_of_block (false_edge, last);
2953 : 2089966 : return NULL;
2954 : : }
2955 : 3219123 : if (true_edge->dest == bb->next_bb)
2956 : : {
2957 : 2768927 : jumpifnot_1 (code, op0, op1, label_rtx_for_bb (false_edge->dest),
2958 : : false_edge->probability);
2959 : 2768927 : maybe_dump_rtl_for_gimple_stmt (stmt, last);
2960 : 2768927 : if (false_edge->goto_locus != UNKNOWN_LOCATION)
2961 : 2465987 : set_curr_insn_location (false_edge->goto_locus);
2962 : 2768927 : true_edge->flags |= EDGE_FALLTHRU;
2963 : 2768927 : maybe_cleanup_end_of_block (true_edge, last);
2964 : 2768927 : return NULL;
2965 : : }
2966 : :
2967 : 450196 : jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
2968 : : true_edge->probability);
2969 : 450196 : last = get_last_insn ();
2970 : 450196 : if (false_edge->goto_locus != UNKNOWN_LOCATION)
2971 : 251522 : set_curr_insn_location (false_edge->goto_locus);
2972 : 450196 : emit_jump (label_rtx_for_bb (false_edge->dest));
2973 : :
2974 : 450196 : BB_END (bb) = last;
2975 : 450196 : if (BARRIER_P (BB_END (bb)))
2976 : 15 : BB_END (bb) = PREV_INSN (BB_END (bb));
2977 : 450196 : update_bb_for_insn (bb);
2978 : :
2979 : 450196 : new_bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
2980 : 450196 : dest = false_edge->dest;
2981 : 450196 : redirect_edge_succ (false_edge, new_bb);
2982 : 450196 : false_edge->flags |= EDGE_FALLTHRU;
2983 : 450196 : new_bb->count = false_edge->count ();
2984 : 450196 : loop_p loop = find_common_loop (bb->loop_father, dest->loop_father);
2985 : 450196 : add_bb_to_loop (new_bb, loop);
2986 : 450196 : if (loop->latch == bb
2987 : 28215 : && loop->header == dest)
2988 : 28214 : loop->latch = new_bb;
2989 : 450196 : make_single_succ_edge (new_bb, dest, 0);
2990 : 450196 : if (BARRIER_P (BB_END (new_bb)))
2991 : 450196 : BB_END (new_bb) = PREV_INSN (BB_END (new_bb));
2992 : 450196 : update_bb_for_insn (new_bb);
2993 : :
2994 : 450196 : maybe_dump_rtl_for_gimple_stmt (stmt, last2);
2995 : :
2996 : 450196 : if (true_edge->goto_locus != UNKNOWN_LOCATION)
2997 : : {
2998 : 251496 : set_curr_insn_location (true_edge->goto_locus);
2999 : 251496 : true_edge->goto_locus = curr_insn_location ();
3000 : : }
3001 : :
3002 : : return new_bb;
3003 : : }
3004 : :
3005 : : /* Mark all calls that can have a transaction restart. */
3006 : :
3007 : : static void
3008 : 6356278 : mark_transaction_restart_calls (gimple *stmt)
3009 : : {
3010 : 6356278 : struct tm_restart_node dummy;
3011 : 6356278 : tm_restart_node **slot;
3012 : :
3013 : 6356278 : if (!cfun->gimple_df->tm_restart)
3014 : 6353294 : return;
3015 : :
3016 : 2984 : dummy.stmt = stmt;
3017 : 2984 : slot = cfun->gimple_df->tm_restart->find_slot (&dummy, NO_INSERT);
3018 : 2984 : if (slot)
3019 : : {
3020 : 0 : struct tm_restart_node *n = *slot;
3021 : 0 : tree list = n->label_or_list;
3022 : 0 : rtx_insn *insn;
3023 : :
3024 : 0 : for (insn = next_real_insn (get_last_insn ());
3025 : 0 : !CALL_P (insn);
3026 : 0 : insn = next_real_insn (insn))
3027 : 0 : continue;
3028 : :
3029 : 0 : if (TREE_CODE (list) == LABEL_DECL)
3030 : 0 : add_reg_note (insn, REG_TM, label_rtx (list));
3031 : : else
3032 : 0 : for (; list ; list = TREE_CHAIN (list))
3033 : 0 : add_reg_note (insn, REG_TM, label_rtx (TREE_VALUE (list)));
3034 : 0 : }
3035 : : }
3036 : :
3037 : : /* A subroutine of expand_gimple_stmt_1, expanding one GIMPLE_CALL
3038 : : statement STMT. */
3039 : :
3040 : : static void
3041 : 6543383 : expand_call_stmt (gcall *stmt)
3042 : : {
3043 : 6543383 : tree exp, decl, lhs;
3044 : 6543383 : bool builtin_p;
3045 : 6543383 : size_t i;
3046 : :
3047 : 6543383 : if (gimple_call_internal_p (stmt))
3048 : : {
3049 : 152444 : expand_internal_call (stmt);
3050 : 152444 : return;
3051 : : }
3052 : :
3053 : : /* If this is a call to a built-in function and it has no effect other
3054 : : than setting the lhs, try to implement it using an internal function
3055 : : instead. */
3056 : 6390939 : decl = gimple_call_fndecl (stmt);
3057 : 6390939 : if (gimple_call_lhs (stmt)
3058 : 2273985 : && !gimple_has_side_effects (stmt)
3059 : 7079647 : && (optimize || (decl && called_as_built_in (decl))))
3060 : : {
3061 : 668963 : internal_fn ifn = replacement_internal_fn (stmt);
3062 : 668963 : if (ifn != IFN_LAST)
3063 : : {
3064 : 34661 : expand_internal_call (ifn, stmt);
3065 : 34661 : return;
3066 : : }
3067 : : }
3068 : :
3069 : 6356278 : exp = build_vl_exp (CALL_EXPR, gimple_call_num_args (stmt) + 3);
3070 : :
3071 : 6356278 : CALL_EXPR_FN (exp) = gimple_call_fn (stmt);
3072 : 6356278 : builtin_p = decl && fndecl_built_in_p (decl);
3073 : :
3074 : : /* If this is not a builtin function, the function type through which the
3075 : : call is made may be different from the type of the function. */
3076 : 4477782 : if (!builtin_p)
3077 : 8955564 : CALL_EXPR_FN (exp)
3078 : 13433346 : = fold_convert (build_pointer_type (gimple_call_fntype (stmt)),
3079 : : CALL_EXPR_FN (exp));
3080 : :
3081 : 6356278 : TREE_TYPE (exp) = gimple_call_return_type (stmt);
3082 : 6356278 : CALL_EXPR_STATIC_CHAIN (exp) = gimple_call_chain (stmt);
3083 : :
3084 : 19217825 : for (i = 0; i < gimple_call_num_args (stmt); i++)
3085 : : {
3086 : 12861547 : tree arg = gimple_call_arg (stmt, i);
3087 : 12861547 : gimple *def;
3088 : : /* TER addresses into arguments of builtin functions so we have a
3089 : : chance to infer more correct alignment information. See PR39954. */
3090 : 12861547 : if (builtin_p
3091 : 3818008 : && TREE_CODE (arg) == SSA_NAME
3092 : 1279029 : && (def = get_gimple_for_ssa_name (arg))
3093 : 329726 : && is_gimple_assign (def)
3094 : 13191112 : && gimple_assign_rhs_code (def) == ADDR_EXPR)
3095 : 14753 : arg = gimple_assign_rhs1 (def);
3096 : 12861547 : CALL_EXPR_ARG (exp, i) = arg;
3097 : : }
3098 : :
3099 : 6356278 : if (gimple_has_side_effects (stmt)
3100 : : /* ??? Downstream in expand_expr_real_1 we assume that expressions
3101 : : w/o side-effects do not throw so work around this here. */
3102 : 6356278 : || stmt_could_throw_p (cfun, stmt))
3103 : 5704449 : TREE_SIDE_EFFECTS (exp) = 1;
3104 : :
3105 : 6356278 : if (gimple_call_nothrow_p (stmt))
3106 : 2836284 : TREE_NOTHROW (exp) = 1;
3107 : :
3108 : 6356278 : CALL_EXPR_TAILCALL (exp) = gimple_call_tail_p (stmt);
3109 : 6356278 : CALL_EXPR_MUST_TAIL_CALL (exp) = gimple_call_must_tail_p (stmt);
3110 : 6356278 : CALL_EXPR_RETURN_SLOT_OPT (exp) = gimple_call_return_slot_opt_p (stmt);
3111 : 6356278 : if (decl
3112 : 6175676 : && fndecl_built_in_p (decl, BUILT_IN_NORMAL)
3113 : 8060732 : && ALLOCA_FUNCTION_CODE_P (DECL_FUNCTION_CODE (decl)))
3114 : 25274 : CALL_ALLOCA_FOR_VAR_P (exp) = gimple_call_alloca_for_var_p (stmt);
3115 : : else
3116 : 6331004 : CALL_FROM_THUNK_P (exp) = gimple_call_from_thunk_p (stmt);
3117 : 6356278 : CALL_EXPR_VA_ARG_PACK (exp) = gimple_call_va_arg_pack_p (stmt);
3118 : 6356278 : CALL_EXPR_BY_DESCRIPTOR (exp) = gimple_call_by_descriptor_p (stmt);
3119 : 6356278 : SET_EXPR_LOCATION (exp, gimple_location (stmt));
3120 : :
3121 : : /* Must come after copying location. */
3122 : 6356278 : copy_warning (exp, stmt);
3123 : :
3124 : : /* Ensure RTL is created for debug args. */
3125 : 12531954 : if (decl && DECL_HAS_DEBUG_ARGS_P (decl))
3126 : : {
3127 : 49375 : vec<tree, va_gc> **debug_args = decl_debug_args_lookup (decl);
3128 : 49375 : unsigned int ix;
3129 : 49375 : tree dtemp;
3130 : :
3131 : 49375 : if (debug_args)
3132 : 107017 : for (ix = 1; (*debug_args)->iterate (ix, &dtemp); ix += 2)
3133 : : {
3134 : 57642 : gcc_assert (TREE_CODE (dtemp) == DEBUG_EXPR_DECL);
3135 : 57642 : expand_debug_expr (dtemp);
3136 : : }
3137 : : }
3138 : :
3139 : 6356278 : rtx_insn *before_call = get_last_insn ();
3140 : 6356278 : lhs = gimple_call_lhs (stmt);
3141 : 6356278 : if (lhs)
3142 : 2239324 : expand_assignment (lhs, exp, false);
3143 : : else
3144 : 4116954 : expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
3145 : :
3146 : : /* If the gimple call is an indirect call and has 'nocf_check'
3147 : : attribute find a generated CALL insn to mark it as no
3148 : : control-flow verification is needed. */
3149 : 6356278 : if (gimple_call_nocf_check_p (stmt)
3150 : 6356299 : && !gimple_call_fndecl (stmt))
3151 : : {
3152 : 20 : rtx_insn *last = get_last_insn ();
3153 : 20 : while (!CALL_P (last)
3154 : 34 : && last != before_call)
3155 : 14 : last = PREV_INSN (last);
3156 : :
3157 : 20 : if (last != before_call)
3158 : 20 : add_reg_note (last, REG_CALL_NOCF_CHECK, const0_rtx);
3159 : : }
3160 : :
3161 : 6356278 : mark_transaction_restart_calls (stmt);
3162 : : }
3163 : :
3164 : :
3165 : : /* Generate RTL for an asm statement (explicit assembler code).
3166 : : STRING is a STRING_CST node containing the assembler code text,
3167 : : or an ADDR_EXPR containing a STRING_CST. VOL nonzero means the
3168 : : insn is volatile; don't optimize it. */
3169 : :
3170 : : static void
3171 : 2760 : expand_asm_loc (tree string, int vol, location_t locus)
3172 : : {
3173 : 2760 : rtx body;
3174 : :
3175 : 2760 : body = gen_rtx_ASM_INPUT_loc (VOIDmode,
3176 : : ggc_strdup (TREE_STRING_POINTER (string)),
3177 : : locus);
3178 : :
3179 : 2760 : MEM_VOLATILE_P (body) = vol;
3180 : :
3181 : : /* Non-empty basic ASM implicitly clobbers memory. */
3182 : 2760 : if (TREE_STRING_LENGTH (string) != 0)
3183 : : {
3184 : 895 : rtx asm_op, clob;
3185 : 895 : unsigned i, nclobbers;
3186 : 895 : auto_vec<rtx> input_rvec, output_rvec;
3187 : 895 : auto_vec<machine_mode> input_mode;
3188 : 895 : auto_vec<const char *> constraints;
3189 : 895 : auto_vec<rtx> use_rvec;
3190 : 895 : auto_vec<rtx> clobber_rvec;
3191 : 895 : HARD_REG_SET clobbered_regs;
3192 : 895 : CLEAR_HARD_REG_SET (clobbered_regs);
3193 : :
3194 : 895 : clob = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
3195 : 895 : clobber_rvec.safe_push (clob);
3196 : :
3197 : 895 : if (targetm.md_asm_adjust)
3198 : 895 : targetm.md_asm_adjust (output_rvec, input_rvec, input_mode,
3199 : : constraints, use_rvec, clobber_rvec,
3200 : : clobbered_regs, locus);
3201 : :
3202 : 895 : asm_op = body;
3203 : 895 : nclobbers = clobber_rvec.length ();
3204 : 895 : auto nuses = use_rvec.length ();
3205 : 895 : body = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (1 + nuses + nclobbers));
3206 : :
3207 : 895 : i = 0;
3208 : 895 : XVECEXP (body, 0, i++) = asm_op;
3209 : 895 : for (rtx use : use_rvec)
3210 : 0 : XVECEXP (body, 0, i++) = gen_rtx_USE (VOIDmode, use);
3211 : 4475 : for (rtx clobber : clobber_rvec)
3212 : 1790 : XVECEXP (body, 0, i++) = gen_rtx_CLOBBER (VOIDmode, clobber);
3213 : 895 : }
3214 : :
3215 : 2760 : emit_insn (body);
3216 : 2760 : }
3217 : :
3218 : : /* Return the number of times character C occurs in string S. */
3219 : : static int
3220 : 123427 : n_occurrences (int c, const char *s)
3221 : : {
3222 : 123427 : int n = 0;
3223 : 323202 : while (*s)
3224 : 199775 : n += (*s++ == c);
3225 : 123427 : return n;
3226 : : }
3227 : :
3228 : : /* A subroutine of expand_asm_operands. Check that all operands have
3229 : : the same number of alternatives. Return true if so. */
3230 : :
3231 : : static bool
3232 : 103616 : check_operand_nalternatives (const vec<const char *> &constraints)
3233 : : {
3234 : 103616 : unsigned len = constraints.length();
3235 : 103616 : if (len > 0)
3236 : : {
3237 : 39870 : int nalternatives = n_occurrences (',', constraints[0]);
3238 : :
3239 : 39870 : if (nalternatives + 1 > MAX_RECOG_ALTERNATIVES)
3240 : : {
3241 : 0 : error ("too many alternatives in %<asm%>");
3242 : 0 : return false;
3243 : : }
3244 : :
3245 : 123427 : for (unsigned i = 1; i < len; ++i)
3246 : 167114 : if (n_occurrences (',', constraints[i]) != nalternatives)
3247 : : {
3248 : 0 : error ("operand constraints for %<asm%> differ "
3249 : : "in number of alternatives");
3250 : 0 : return false;
3251 : : }
3252 : : }
3253 : : return true;
3254 : : }
3255 : :
3256 : : /* Check for overlap between registers marked in CLOBBERED_REGS and
3257 : : anything inappropriate in T. Emit error and return the register
3258 : : variable definition for error, NULL_TREE for ok. */
3259 : :
3260 : : static bool
3261 : 123418 : tree_conflicts_with_clobbers_p (tree t, HARD_REG_SET *clobbered_regs,
3262 : : location_t loc)
3263 : : {
3264 : : /* Conflicts between asm-declared register variables and the clobber
3265 : : list are not allowed. */
3266 : 123418 : tree overlap = tree_overlaps_hard_reg_set (t, clobbered_regs);
3267 : :
3268 : 123418 : if (overlap)
3269 : : {
3270 : 21 : error_at (loc, "%<asm%> specifier for variable %qE conflicts with "
3271 : 21 : "%<asm%> clobber list", DECL_NAME (overlap));
3272 : :
3273 : : /* Reset registerness to stop multiple errors emitted for a single
3274 : : variable. */
3275 : 21 : DECL_REGISTER (overlap) = 0;
3276 : 21 : return true;
3277 : : }
3278 : :
3279 : : return false;
3280 : : }
3281 : :
3282 : : /* Check that the given REGNO spanning NREGS is a valid
3283 : : asm clobber operand. Some HW registers cannot be
3284 : : saved/restored, hence they should not be clobbered by
3285 : : asm statements. */
3286 : : static bool
3287 : 41477 : asm_clobber_reg_is_valid (int regno, int nregs, const char *regname)
3288 : : {
3289 : 41477 : bool is_valid = true;
3290 : 41477 : HARD_REG_SET regset;
3291 : :
3292 : 41477 : CLEAR_HARD_REG_SET (regset);
3293 : :
3294 : 41477 : add_range_to_hard_reg_set (®set, regno, nregs);
3295 : :
3296 : : /* Clobbering the PIC register is an error. */
3297 : 41477 : if (PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
3298 : 0 : && overlaps_hard_reg_set_p (regset, Pmode, PIC_OFFSET_TABLE_REGNUM))
3299 : : {
3300 : : /* ??? Diagnose during gimplification? */
3301 : 0 : error ("PIC register clobbered by %qs in %<asm%>", regname);
3302 : 0 : is_valid = false;
3303 : : }
3304 : 41477 : else if (!in_hard_reg_set_p
3305 : 41477 : (accessible_reg_set, reg_raw_mode[regno], regno))
3306 : : {
3307 : : /* ??? Diagnose during gimplification? */
3308 : 0 : error ("the register %qs cannot be clobbered in %<asm%>"
3309 : : " for the current target", regname);
3310 : 0 : is_valid = false;
3311 : : }
3312 : :
3313 : : /* Clobbering the stack pointer register is deprecated. GCC expects
3314 : : the value of the stack pointer after an asm statement to be the same
3315 : : as it was before, so no asm can validly clobber the stack pointer in
3316 : : the usual sense. Adding the stack pointer to the clobber list has
3317 : : traditionally had some undocumented and somewhat obscure side-effects. */
3318 : 41477 : if (overlaps_hard_reg_set_p (regset, Pmode, STACK_POINTER_REGNUM))
3319 : : {
3320 : 1 : crtl->sp_is_clobbered_by_asm = true;
3321 : 1 : if (warning (OPT_Wdeprecated, "listing the stack pointer register"
3322 : : " %qs in a clobber list is deprecated", regname))
3323 : 1 : inform (input_location, "the value of the stack pointer after"
3324 : : " an %<asm%> statement must be the same as it was before"
3325 : : " the statement");
3326 : : }
3327 : :
3328 : 41477 : return is_valid;
3329 : : }
3330 : :
3331 : : /* Generate RTL for an asm statement with arguments.
3332 : : STRING is the instruction template.
3333 : : OUTPUTS is a list of output arguments (lvalues); INPUTS a list of inputs.
3334 : : Each output or input has an expression in the TREE_VALUE and
3335 : : a tree list in TREE_PURPOSE which in turn contains a constraint
3336 : : name in TREE_VALUE (or NULL_TREE) and a constraint string
3337 : : in TREE_PURPOSE.
3338 : : CLOBBERS is a list of STRING_CST nodes each naming a hard register
3339 : : that is clobbered by this insn.
3340 : :
3341 : : LABELS is a list of labels, and if LABELS is non-NULL, FALLTHRU_BB
3342 : : should be the fallthru basic block of the asm goto.
3343 : :
3344 : : Not all kinds of lvalue that may appear in OUTPUTS can be stored directly.
3345 : : Some elements of OUTPUTS may be replaced with trees representing temporary
3346 : : values. The caller should copy those temporary values to the originally
3347 : : specified lvalues.
3348 : :
3349 : : VOL nonzero means the insn is volatile; don't optimize it. */
3350 : :
3351 : : static void
3352 : 106381 : expand_asm_stmt (gasm *stmt)
3353 : : {
3354 : 106381 : class save_input_location
3355 : : {
3356 : : location_t old;
3357 : :
3358 : : public:
3359 : 103621 : explicit save_input_location(location_t where)
3360 : 103621 : {
3361 : 103621 : old = input_location;
3362 : 103621 : input_location = where;
3363 : : }
3364 : :
3365 : 103621 : ~save_input_location()
3366 : : {
3367 : 103621 : input_location = old;
3368 : 5 : }
3369 : : };
3370 : :
3371 : 106381 : location_t locus = gimple_location (stmt);
3372 : :
3373 : 106381 : if (gimple_asm_basic_p (stmt))
3374 : : {
3375 : 2760 : const char *s = gimple_asm_string (stmt);
3376 : 2760 : tree string = build_string (strlen (s), s);
3377 : 2760 : expand_asm_loc (string, gimple_asm_volatile_p (stmt), locus);
3378 : 5525 : return;
3379 : : }
3380 : :
3381 : : /* There are some legacy diagnostics in here. */
3382 : 103621 : save_input_location s_i_l(locus);
3383 : :
3384 : 103621 : unsigned noutputs = gimple_asm_noutputs (stmt);
3385 : 103621 : unsigned ninputs = gimple_asm_ninputs (stmt);
3386 : 103621 : unsigned nlabels = gimple_asm_nlabels (stmt);
3387 : 103621 : unsigned i;
3388 : 103621 : bool error_seen = false;
3389 : :
3390 : : /* ??? Diagnose during gimplification? */
3391 : 103621 : if (ninputs + noutputs + nlabels > MAX_RECOG_OPERANDS)
3392 : : {
3393 : 5 : error_at (locus, "more than %d operands in %<asm%>", MAX_RECOG_OPERANDS);
3394 : 5 : return;
3395 : : }
3396 : :
3397 : 103616 : auto_vec<tree, MAX_RECOG_OPERANDS> output_tvec;
3398 : 103616 : auto_vec<tree, MAX_RECOG_OPERANDS> input_tvec;
3399 : 103616 : auto_vec<const char *, MAX_RECOG_OPERANDS> constraints;
3400 : :
3401 : : /* Copy the gimple vectors into new vectors that we can manipulate. */
3402 : :
3403 : 103616 : output_tvec.safe_grow (noutputs, true);
3404 : 103616 : input_tvec.safe_grow (ninputs, true);
3405 : 103616 : constraints.safe_grow (noutputs + ninputs, true);
3406 : :
3407 : 175237 : for (i = 0; i < noutputs; ++i)
3408 : : {
3409 : 71621 : tree t = gimple_asm_output_op (stmt, i);
3410 : 71621 : output_tvec[i] = TREE_VALUE (t);
3411 : 71621 : constraints[i] = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
3412 : : }
3413 : 155422 : for (i = 0; i < ninputs; i++)
3414 : : {
3415 : 51806 : tree t = gimple_asm_input_op (stmt, i);
3416 : 51806 : input_tvec[i] = TREE_VALUE (t);
3417 : 51806 : constraints[i + noutputs]
3418 : 51806 : = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
3419 : : }
3420 : :
3421 : : /* ??? Diagnose during gimplification? */
3422 : 103616 : if (! check_operand_nalternatives (constraints))
3423 : : return;
3424 : :
3425 : : /* Count the number of meaningful clobbered registers, ignoring what
3426 : : we would ignore later. */
3427 : 103616 : auto_vec<rtx> clobber_rvec;
3428 : 103616 : HARD_REG_SET clobbered_regs;
3429 : 103616 : CLEAR_HARD_REG_SET (clobbered_regs);
3430 : :
3431 : 103616 : if (unsigned n = gimple_asm_nclobbers (stmt))
3432 : : {
3433 : 68395 : clobber_rvec.reserve (n);
3434 : 164311 : for (i = 0; i < n; i++)
3435 : : {
3436 : 95916 : tree t = gimple_asm_clobber_op (stmt, i);
3437 : 95916 : const char *regname = TREE_STRING_POINTER (TREE_VALUE (t));
3438 : 95916 : int nregs, j;
3439 : :
3440 : 95916 : j = decode_reg_name_and_count (regname, &nregs);
3441 : 95916 : if (j < 0)
3442 : : {
3443 : 54439 : if (j == -2)
3444 : : {
3445 : : /* ??? Diagnose during gimplification? */
3446 : 15 : error_at (locus, "unknown register name %qs in %<asm%>",
3447 : : regname);
3448 : 15 : error_seen = true;
3449 : : }
3450 : 54424 : else if (j == -4)
3451 : : {
3452 : 38699 : rtx x = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
3453 : 38699 : clobber_rvec.safe_push (x);
3454 : : }
3455 : 15725 : else if (j == -5)
3456 : : {
3457 : 2 : if (targetm.redzone_clobber)
3458 : 2 : if (rtx x = targetm.redzone_clobber ())
3459 : 2 : clobber_rvec.safe_push (x);
3460 : : }
3461 : : else
3462 : : {
3463 : : /* Otherwise we should have -1 == empty string
3464 : : or -3 == cc, which is not a register. */
3465 : 15723 : gcc_assert (j == -1 || j == -3);
3466 : : }
3467 : : }
3468 : : else
3469 : 82954 : for (int reg = j; reg < j + nregs; reg++)
3470 : : {
3471 : 41477 : if (!asm_clobber_reg_is_valid (reg, nregs, regname))
3472 : 0 : return;
3473 : :
3474 : 41477 : SET_HARD_REG_BIT (clobbered_regs, reg);
3475 : 41477 : rtx x = gen_rtx_REG (reg_raw_mode[reg], reg);
3476 : 41477 : clobber_rvec.safe_push (x);
3477 : : }
3478 : : }
3479 : : }
3480 : :
3481 : : /* First pass over inputs and outputs checks validity and sets
3482 : : mark_addressable if needed. */
3483 : : /* ??? Diagnose during gimplification? */
3484 : :
3485 : 175237 : for (i = 0; i < noutputs; ++i)
3486 : : {
3487 : 71621 : tree val = output_tvec[i];
3488 : 71621 : tree type = TREE_TYPE (val);
3489 : 71621 : const char *constraint;
3490 : 71621 : bool is_inout;
3491 : 71621 : bool allows_reg;
3492 : 71621 : bool allows_mem;
3493 : :
3494 : : /* Try to parse the output constraint. If that fails, there's
3495 : : no point in going further. */
3496 : 71621 : constraint = constraints[i];
3497 : 71621 : if (!parse_output_constraint (&constraint, i, ninputs, noutputs,
3498 : : &allows_mem, &allows_reg, &is_inout))
3499 : 0 : return;
3500 : :
3501 : : /* If the output is a hard register, verify it doesn't conflict with
3502 : : any other operand's possible hard register use. */
3503 : 71621 : if (DECL_P (val)
3504 : 7544 : && REG_P (DECL_RTL (val))
3505 : 72578 : && HARD_REGISTER_P (DECL_RTL (val)))
3506 : : {
3507 : 940 : unsigned j, output_hregno = REGNO (DECL_RTL (val));
3508 : 940 : bool early_clobber_p = strchr (constraints[i], '&') != NULL;
3509 : 940 : unsigned long match;
3510 : :
3511 : : /* Verify the other outputs do not use the same hard register. */
3512 : 1100 : for (j = i + 1; j < noutputs; ++j)
3513 : 160 : if (DECL_P (output_tvec[j])
3514 : 158 : && REG_P (DECL_RTL (output_tvec[j]))
3515 : 158 : && HARD_REGISTER_P (DECL_RTL (output_tvec[j]))
3516 : 318 : && output_hregno == REGNO (DECL_RTL (output_tvec[j])))
3517 : : {
3518 : 1 : error_at (locus, "invalid hard register usage between output "
3519 : : "operands");
3520 : 1 : error_seen = true;
3521 : : }
3522 : :
3523 : : /* Verify matching constraint operands use the same hard register
3524 : : and that the non-matching constraint operands do not use the same
3525 : : hard register if the output is an early clobber operand. */
3526 : 1851 : for (j = 0; j < ninputs; ++j)
3527 : 911 : if (DECL_P (input_tvec[j])
3528 : 772 : && REG_P (DECL_RTL (input_tvec[j]))
3529 : 1683 : && HARD_REGISTER_P (DECL_RTL (input_tvec[j])))
3530 : : {
3531 : 772 : unsigned input_hregno = REGNO (DECL_RTL (input_tvec[j]));
3532 : 772 : switch (*constraints[j + noutputs])
3533 : : {
3534 : 760 : case '0': case '1': case '2': case '3': case '4':
3535 : 760 : case '5': case '6': case '7': case '8': case '9':
3536 : 760 : match = strtoul (constraints[j + noutputs], NULL, 10);
3537 : 760 : break;
3538 : : default:
3539 : : match = ULONG_MAX;
3540 : : break;
3541 : : }
3542 : 760 : if (i == match
3543 : 661 : && output_hregno != input_hregno)
3544 : : {
3545 : 1 : error_at (locus, "invalid hard register usage between "
3546 : : "output operand and matching constraint operand");
3547 : 1 : error_seen = true;
3548 : : }
3549 : 771 : else if (early_clobber_p
3550 : 6 : && i != match
3551 : 4 : && output_hregno == input_hregno)
3552 : : {
3553 : 2 : error_at (locus, "invalid hard register usage between "
3554 : : "earlyclobber operand and input operand");
3555 : 2 : error_seen = true;
3556 : : }
3557 : : }
3558 : : }
3559 : :
3560 : 71621 : if (! allows_reg
3561 : 71621 : && (allows_mem
3562 : 0 : || is_inout
3563 : 0 : || (DECL_P (val)
3564 : 0 : && REG_P (DECL_RTL (val))
3565 : 0 : && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type))))
3566 : 6261 : mark_addressable (val);
3567 : : }
3568 : :
3569 : 155422 : for (i = 0; i < ninputs; ++i)
3570 : : {
3571 : 51806 : bool allows_reg, allows_mem;
3572 : 51806 : const char *constraint;
3573 : :
3574 : 51806 : constraint = constraints[i + noutputs];
3575 : 51806 : if (! parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
3576 : 51806 : constraints.address (),
3577 : : &allows_mem, &allows_reg))
3578 : 0 : return;
3579 : :
3580 : 51806 : if (! allows_reg && allows_mem)
3581 : 6932 : mark_addressable (input_tvec[i]);
3582 : : }
3583 : :
3584 : : /* Second pass evaluates arguments. */
3585 : :
3586 : : /* Make sure stack is consistent for asm goto. */
3587 : 103616 : if (nlabels > 0)
3588 : 445 : do_pending_stack_adjust ();
3589 : 103616 : int old_generating_concat_p = generating_concat_p;
3590 : :
3591 : : /* Vector of RTX's of evaluated output operands. */
3592 : 207232 : auto_vec<rtx, MAX_RECOG_OPERANDS> output_rvec;
3593 : 103616 : auto_vec<int, MAX_RECOG_OPERANDS> inout_opnum;
3594 : 103616 : rtx_insn *after_rtl_seq = NULL, *after_rtl_end = NULL;
3595 : :
3596 : 103616 : output_rvec.safe_grow (noutputs, true);
3597 : :
3598 : 175237 : for (i = 0; i < noutputs; ++i)
3599 : : {
3600 : 71621 : tree val = output_tvec[i];
3601 : 71621 : tree type = TREE_TYPE (val);
3602 : 71621 : bool is_inout, allows_reg, allows_mem, ok;
3603 : 71621 : rtx op;
3604 : :
3605 : 71621 : ok = parse_output_constraint (&constraints[i], i, ninputs,
3606 : : noutputs, &allows_mem, &allows_reg,
3607 : : &is_inout);
3608 : 71621 : gcc_assert (ok);
3609 : :
3610 : : /* If an output operand is not a decl or indirect ref and our constraint
3611 : : allows a register, make a temporary to act as an intermediate.
3612 : : Make the asm insn write into that, then we will copy it to
3613 : : the real output operand. Likewise for promoted variables. */
3614 : :
3615 : 71621 : generating_concat_p = 0;
3616 : :
3617 : 71621 : gcc_assert (TREE_CODE (val) != INDIRECT_REF);
3618 : 71621 : if (((TREE_CODE (val) == MEM_REF
3619 : 2635 : && TREE_CODE (TREE_OPERAND (val, 0)) != ADDR_EXPR)
3620 : 2534 : && allows_mem)
3621 : 71227 : || (DECL_P (val)
3622 : 7544 : && (allows_mem || REG_P (DECL_RTL (val)))
3623 : 7799 : && ! (REG_P (DECL_RTL (val))
3624 : 957 : && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type)))
3625 : 64385 : || ! allows_reg
3626 : 64195 : || is_inout
3627 : 64195 : || TREE_ADDRESSABLE (type)
3628 : 135816 : || (!tree_fits_poly_int64_p (TYPE_SIZE (type))
3629 : 0 : && !known_size_p (max_int_size_in_bytes (type))))
3630 : : {
3631 : 7426 : op = expand_expr (val, NULL_RTX, VOIDmode,
3632 : : !allows_reg ? EXPAND_MEMORY : EXPAND_WRITE);
3633 : 7426 : if (MEM_P (op))
3634 : 6469 : op = validize_mem (op);
3635 : :
3636 : 7426 : if (! allows_reg && !MEM_P (op))
3637 : : {
3638 : 0 : error_at (locus, "output number %d not directly addressable", i);
3639 : 0 : error_seen = true;
3640 : : }
3641 : 7426 : if ((! allows_mem && MEM_P (op) && GET_MODE (op) != BLKmode)
3642 : 7426 : || GET_CODE (op) == CONCAT)
3643 : : {
3644 : 0 : rtx old_op = op;
3645 : 0 : op = gen_reg_rtx (GET_MODE (op));
3646 : :
3647 : 0 : generating_concat_p = old_generating_concat_p;
3648 : :
3649 : 0 : if (is_inout)
3650 : 0 : emit_move_insn (op, old_op);
3651 : :
3652 : 0 : push_to_sequence2 (after_rtl_seq, after_rtl_end);
3653 : 0 : emit_move_insn (old_op, op);
3654 : 0 : after_rtl_seq = get_insns ();
3655 : 0 : after_rtl_end = get_last_insn ();
3656 : 0 : end_sequence ();
3657 : : }
3658 : : }
3659 : : else
3660 : : {
3661 : 64195 : op = assign_temp (type, 0, 1);
3662 : 64195 : op = validize_mem (op);
3663 : 64195 : if (!MEM_P (op) && TREE_CODE (val) == SSA_NAME)
3664 : 60588 : set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (val), op);
3665 : :
3666 : 64195 : generating_concat_p = old_generating_concat_p;
3667 : :
3668 : 64195 : push_to_sequence2 (after_rtl_seq, after_rtl_end);
3669 : 64195 : expand_assignment (val, make_tree (type, op), false);
3670 : 64195 : after_rtl_seq = get_insns ();
3671 : 64195 : after_rtl_end = get_last_insn ();
3672 : 64195 : end_sequence ();
3673 : : }
3674 : 71621 : output_rvec[i] = op;
3675 : :
3676 : 71621 : if (is_inout)
3677 : 0 : inout_opnum.safe_push (i);
3678 : : }
3679 : :
3680 : 103616 : const char *str = gimple_asm_string (stmt);
3681 : 103616 : if (error_seen)
3682 : : {
3683 : 19 : ninputs = 0;
3684 : 19 : noutputs = 0;
3685 : 19 : inout_opnum.truncate (0);
3686 : 19 : output_rvec.truncate (0);
3687 : 19 : clobber_rvec.truncate (0);
3688 : 19 : constraints.truncate (0);
3689 : 19 : CLEAR_HARD_REG_SET (clobbered_regs);
3690 : : str = "";
3691 : : }
3692 : :
3693 : 207232 : auto_vec<rtx, MAX_RECOG_OPERANDS> input_rvec;
3694 : 103616 : auto_vec<machine_mode, MAX_RECOG_OPERANDS> input_mode;
3695 : :
3696 : 103616 : input_rvec.safe_grow (ninputs, true);
3697 : 103616 : input_mode.safe_grow (ninputs, true);
3698 : :
3699 : 103616 : generating_concat_p = 0;
3700 : :
3701 : 155419 : for (i = 0; i < ninputs; ++i)
3702 : : {
3703 : 51803 : tree val = input_tvec[i];
3704 : 51803 : tree type = TREE_TYPE (val);
3705 : 51803 : bool allows_reg, allows_mem, ok;
3706 : 51803 : const char *constraint;
3707 : 51803 : rtx op;
3708 : :
3709 : 51803 : constraint = constraints[i + noutputs];
3710 : 51803 : ok = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
3711 : 51803 : constraints.address (),
3712 : : &allows_mem, &allows_reg);
3713 : 51803 : gcc_assert (ok);
3714 : :
3715 : : /* EXPAND_INITIALIZER will not generate code for valid initializer
3716 : : constants, but will still generate code for other types of operand.
3717 : : This is the behavior we want for constant constraints. */
3718 : 51803 : op = expand_expr (val, NULL_RTX, VOIDmode,
3719 : : allows_reg ? EXPAND_NORMAL
3720 : 8167 : : allows_mem ? EXPAND_MEMORY
3721 : : : EXPAND_INITIALIZER);
3722 : :
3723 : : /* Never pass a CONCAT to an ASM. */
3724 : 51803 : if (GET_CODE (op) == CONCAT)
3725 : 2 : op = force_reg (GET_MODE (op), op);
3726 : 51801 : else if (MEM_P (op))
3727 : 12116 : op = validize_mem (op);
3728 : :
3729 : 51803 : if (asm_operand_ok (op, constraint, NULL) <= 0)
3730 : : {
3731 : 24455 : if (allows_reg && TYPE_MODE (type) != BLKmode)
3732 : 24062 : op = force_reg (TYPE_MODE (type), op);
3733 : 393 : else if (!allows_mem)
3734 : 20 : warning_at (locus, 0, "%<asm%> operand %d probably does not match "
3735 : : "constraints", i + noutputs);
3736 : 373 : else if (MEM_P (op))
3737 : : {
3738 : : /* We won't recognize either volatile memory or memory
3739 : : with a queued address as available a memory_operand
3740 : : at this point. Ignore it: clearly this *is* a memory. */
3741 : : }
3742 : : else
3743 : 0 : gcc_unreachable ();
3744 : : }
3745 : 51803 : input_rvec[i] = op;
3746 : 103606 : input_mode[i] = TYPE_MODE (type);
3747 : : }
3748 : :
3749 : : /* For in-out operands, copy output rtx to input rtx. */
3750 : 103616 : unsigned ninout = inout_opnum.length ();
3751 : 103616 : for (i = 0; i < ninout; i++)
3752 : : {
3753 : 0 : int j = inout_opnum[i];
3754 : 0 : rtx o = output_rvec[j];
3755 : :
3756 : 0 : input_rvec.safe_push (o);
3757 : 0 : input_mode.safe_push (GET_MODE (o));
3758 : :
3759 : 0 : char buffer[16];
3760 : 0 : sprintf (buffer, "%d", j);
3761 : 0 : constraints.safe_push (ggc_strdup (buffer));
3762 : : }
3763 : 103616 : ninputs += ninout;
3764 : :
3765 : : /* Sometimes we wish to automatically clobber registers across an asm.
3766 : : Case in point is when the i386 backend moved from cc0 to a hard reg --
3767 : : maintaining source-level compatibility means automatically clobbering
3768 : : the flags register. */
3769 : 103616 : rtx_insn *after_md_seq = NULL;
3770 : 207232 : auto_vec<rtx> use_rvec;
3771 : 103616 : if (targetm.md_asm_adjust)
3772 : 103616 : after_md_seq
3773 : 103616 : = targetm.md_asm_adjust (output_rvec, input_rvec, input_mode,
3774 : : constraints, use_rvec, clobber_rvec,
3775 : : clobbered_regs, locus);
3776 : :
3777 : : /* Do not allow the hook to change the output and input count,
3778 : : lest it mess up the operand numbering. */
3779 : 207232 : gcc_assert (output_rvec.length() == noutputs);
3780 : 207232 : gcc_assert (input_rvec.length() == ninputs);
3781 : 207232 : gcc_assert (constraints.length() == noutputs + ninputs);
3782 : :
3783 : : /* But it certainly can adjust the uses and clobbers. */
3784 : 103616 : unsigned nuses = use_rvec.length ();
3785 : 103616 : unsigned nclobbers = clobber_rvec.length ();
3786 : :
3787 : : /* Third pass checks for easy conflicts. */
3788 : : /* ??? Why are we doing this on trees instead of rtx. */
3789 : :
3790 : 103616 : bool clobber_conflict_found = 0;
3791 : 175231 : for (i = 0; i < noutputs; ++i)
3792 : 71615 : if (tree_conflicts_with_clobbers_p (output_tvec[i], &clobbered_regs, locus))
3793 : 10 : clobber_conflict_found = 1;
3794 : 155419 : for (i = 0; i < ninputs - ninout; ++i)
3795 : 51803 : if (tree_conflicts_with_clobbers_p (input_tvec[i], &clobbered_regs, locus))
3796 : 11 : clobber_conflict_found = 1;
3797 : :
3798 : : /* Make vectors for the expression-rtx, constraint strings,
3799 : : and named operands. */
3800 : :
3801 : 103616 : rtvec argvec = rtvec_alloc (ninputs);
3802 : 103616 : rtvec constraintvec = rtvec_alloc (ninputs);
3803 : 103616 : rtvec labelvec = rtvec_alloc (nlabels);
3804 : :
3805 : 136379 : rtx body = gen_rtx_ASM_OPERANDS ((noutputs == 0 ? VOIDmode
3806 : : : GET_MODE (output_rvec[0])),
3807 : : ggc_strdup (str),
3808 : : "", 0, argvec, constraintvec,
3809 : : labelvec, locus);
3810 : 103616 : MEM_VOLATILE_P (body) = gimple_asm_volatile_p (stmt);
3811 : :
3812 : 155419 : for (i = 0; i < ninputs; ++i)
3813 : : {
3814 : 51803 : ASM_OPERANDS_INPUT (body, i) = input_rvec[i];
3815 : 103606 : ASM_OPERANDS_INPUT_CONSTRAINT_EXP (body, i)
3816 : 51803 : = gen_rtx_ASM_INPUT_loc (input_mode[i],
3817 : : constraints[i + noutputs],
3818 : : locus);
3819 : : }
3820 : :
3821 : : /* Copy labels to the vector. */
3822 : 103616 : rtx_code_label *fallthru_label = NULL;
3823 : 103616 : if (nlabels > 0)
3824 : : {
3825 : 445 : basic_block fallthru_bb = NULL;
3826 : 445 : edge fallthru = find_fallthru_edge (gimple_bb (stmt)->succs);
3827 : 445 : if (fallthru)
3828 : 445 : fallthru_bb = fallthru->dest;
3829 : :
3830 : 1133 : for (i = 0; i < nlabels; ++i)
3831 : : {
3832 : 688 : tree label = TREE_VALUE (gimple_asm_label_op (stmt, i));
3833 : 688 : rtx_insn *r;
3834 : : /* If asm goto has any labels in the fallthru basic block, use
3835 : : a label that we emit immediately after the asm goto. Expansion
3836 : : may insert further instructions into the same basic block after
3837 : : asm goto and if we don't do this, insertion of instructions on
3838 : : the fallthru edge might misbehave. See PR58670. */
3839 : 688 : if (fallthru_bb && label_to_block (cfun, label) == fallthru_bb)
3840 : : {
3841 : 240 : if (fallthru_label == NULL_RTX)
3842 : 235 : fallthru_label = gen_label_rtx ();
3843 : : r = fallthru_label;
3844 : : }
3845 : : else
3846 : 448 : r = label_rtx (label);
3847 : 688 : ASM_OPERANDS_LABEL (body, i) = gen_rtx_LABEL_REF (Pmode, r);
3848 : : }
3849 : : }
3850 : :
3851 : : /* Now, for each output, construct an rtx
3852 : : (set OUTPUT (asm_operands INSN OUTPUTCONSTRAINT OUTPUTNUMBER
3853 : : ARGVEC CONSTRAINTS OPNAMES))
3854 : : If there is more than one, put them inside a PARALLEL. */
3855 : :
3856 : 103616 : if (noutputs == 0 && nuses == 0 && nclobbers == 0)
3857 : : {
3858 : : /* No output operands: put in a raw ASM_OPERANDS rtx. */
3859 : 0 : if (nlabels > 0)
3860 : 0 : emit_jump_insn (body);
3861 : : else
3862 : 0 : emit_insn (body);
3863 : : }
3864 : 103616 : else if (noutputs == 1 && nuses == 0 && nclobbers == 0)
3865 : : {
3866 : 62 : ASM_OPERANDS_OUTPUT_CONSTRAINT (body) = constraints[0];
3867 : 62 : if (nlabels > 0)
3868 : 0 : emit_jump_insn (gen_rtx_SET (output_rvec[0], body));
3869 : : else
3870 : 62 : emit_insn (gen_rtx_SET (output_rvec[0], body));
3871 : : }
3872 : : else
3873 : : {
3874 : 103554 : rtx obody = body;
3875 : 103554 : int num = noutputs;
3876 : :
3877 : 103554 : if (num == 0)
3878 : 70853 : num = 1;
3879 : :
3880 : 103554 : body = gen_rtx_PARALLEL (VOIDmode,
3881 : : rtvec_alloc (num + nuses + nclobbers));
3882 : :
3883 : : /* For each output operand, store a SET. */
3884 : 175107 : for (i = 0; i < noutputs; ++i)
3885 : : {
3886 : 71553 : rtx src, o = output_rvec[i];
3887 : 71553 : if (i == 0)
3888 : : {
3889 : 32701 : ASM_OPERANDS_OUTPUT_CONSTRAINT (obody) = constraints[0];
3890 : 32701 : src = obody;
3891 : : }
3892 : : else
3893 : : {
3894 : 38852 : src = gen_rtx_ASM_OPERANDS (GET_MODE (o),
3895 : : ASM_OPERANDS_TEMPLATE (obody),
3896 : : constraints[i], i, argvec,
3897 : : constraintvec, labelvec, locus);
3898 : 38852 : MEM_VOLATILE_P (src) = gimple_asm_volatile_p (stmt);
3899 : : }
3900 : 71553 : XVECEXP (body, 0, i) = gen_rtx_SET (o, src);
3901 : : }
3902 : :
3903 : : /* If there are no outputs (but there are some clobbers)
3904 : : store the bare ASM_OPERANDS into the PARALLEL. */
3905 : 103554 : if (i == 0)
3906 : 70853 : XVECEXP (body, 0, i++) = obody;
3907 : :
3908 : : /* Add the uses specified by the target hook. No checking should
3909 : : be needed since this doesn't come directly from user code. */
3910 : 103554 : for (rtx use : use_rvec)
3911 : 0 : XVECEXP (body, 0, i++) = gen_rtx_USE (VOIDmode, use);
3912 : :
3913 : : /* Store (clobber REG) for each clobbered register specified. */
3914 : 287274 : for (unsigned j = 0; j < nclobbers; ++j)
3915 : : {
3916 : 183720 : rtx clobbered_reg = clobber_rvec[j];
3917 : :
3918 : : /* Do sanity check for overlap between clobbers and respectively
3919 : : input and outputs that hasn't been handled. Such overlap
3920 : : should have been detected and reported above. */
3921 : 183720 : if (!clobber_conflict_found && REG_P (clobbered_reg))
3922 : : {
3923 : : /* We test the old body (obody) contents to avoid
3924 : : tripping over the under-construction body. */
3925 : 217485 : for (unsigned k = 0; k < noutputs; ++k)
3926 : 72496 : if (reg_overlap_mentioned_p (clobbered_reg, output_rvec[k]))
3927 : 0 : internal_error ("%<asm%> clobber conflict with "
3928 : : "output operand");
3929 : :
3930 : 199563 : for (unsigned k = 0; k < ninputs - ninout; ++k)
3931 : 54574 : if (reg_overlap_mentioned_p (clobbered_reg, input_rvec[k]))
3932 : 0 : internal_error ("%<asm%> clobber conflict with "
3933 : : "input operand");
3934 : : }
3935 : :
3936 : 183720 : XVECEXP (body, 0, i++) = gen_rtx_CLOBBER (VOIDmode, clobbered_reg);
3937 : : }
3938 : :
3939 : 103554 : if (nlabels > 0)
3940 : 445 : emit_jump_insn (body);
3941 : : else
3942 : 103109 : emit_insn (body);
3943 : : }
3944 : :
3945 : 103616 : generating_concat_p = old_generating_concat_p;
3946 : :
3947 : 103616 : if (fallthru_label)
3948 : 235 : emit_label (fallthru_label);
3949 : :
3950 : 103616 : if (after_md_seq)
3951 : 71 : emit_insn (after_md_seq);
3952 : 103616 : if (after_rtl_seq)
3953 : : {
3954 : 26030 : if (nlabels == 0)
3955 : 25905 : emit_insn (after_rtl_seq);
3956 : : else
3957 : : {
3958 : 125 : edge e;
3959 : 125 : edge_iterator ei;
3960 : 125 : unsigned int cnt = EDGE_COUNT (gimple_bb (stmt)->succs);
3961 : :
3962 : 458 : FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
3963 : : {
3964 : 333 : rtx_insn *copy;
3965 : 333 : if (--cnt == 0)
3966 : : copy = after_rtl_seq;
3967 : : else
3968 : : {
3969 : 208 : start_sequence ();
3970 : 208 : duplicate_insn_chain (after_rtl_seq, after_rtl_end,
3971 : : NULL, NULL);
3972 : 208 : copy = get_insns ();
3973 : 208 : end_sequence ();
3974 : : }
3975 : 333 : prepend_insn_to_edge (copy, e);
3976 : : }
3977 : : }
3978 : : }
3979 : :
3980 : 103616 : free_temp_slots ();
3981 : 103616 : crtl->has_asm_statement = 1;
3982 : 103621 : }
3983 : :
3984 : : /* Emit code to jump to the address
3985 : : specified by the pointer expression EXP. */
3986 : :
3987 : : static void
3988 : 393 : expand_computed_goto (tree exp)
3989 : : {
3990 : 393 : rtx x = expand_normal (exp);
3991 : :
3992 : 393 : do_pending_stack_adjust ();
3993 : 393 : emit_indirect_jump (x);
3994 : 393 : }
3995 : :
3996 : : /* Generate RTL code for a `goto' statement with target label LABEL.
3997 : : LABEL should be a LABEL_DECL tree node that was or will later be
3998 : : defined with `expand_label'. */
3999 : :
4000 : : static void
4001 : 0 : expand_goto (tree label)
4002 : : {
4003 : 0 : if (flag_checking)
4004 : : {
4005 : : /* Check for a nonlocal goto to a containing function. Should have
4006 : : gotten translated to __builtin_nonlocal_goto. */
4007 : 0 : tree context = decl_function_context (label);
4008 : 0 : gcc_assert (!context || context == current_function_decl);
4009 : : }
4010 : :
4011 : 0 : emit_jump (jump_target_rtx (label));
4012 : 0 : }
4013 : :
4014 : : /* Output a return with no value. */
4015 : :
4016 : : static void
4017 : 803365 : expand_null_return_1 (void)
4018 : : {
4019 : 803365 : clear_pending_stack_adjust ();
4020 : 803365 : do_pending_stack_adjust ();
4021 : 803365 : emit_jump (return_label);
4022 : 803365 : }
4023 : :
4024 : : /* Generate RTL to return from the current function, with no value.
4025 : : (That is, we do not do anything about returning any value.) */
4026 : :
4027 : : void
4028 : 67875 : expand_null_return (void)
4029 : : {
4030 : : /* If this function was declared to return a value, but we
4031 : : didn't, clobber the return registers so that they are not
4032 : : propagated live to the rest of the function. */
4033 : 67875 : clobber_return_register ();
4034 : :
4035 : 67875 : expand_null_return_1 ();
4036 : 67875 : }
4037 : :
4038 : : /* Generate RTL to return from the current function, with value VAL. */
4039 : :
4040 : : static void
4041 : 735490 : expand_value_return (rtx val)
4042 : : {
4043 : : /* Copy the value to the return location unless it's already there. */
4044 : :
4045 : 735490 : tree decl = DECL_RESULT (current_function_decl);
4046 : 735490 : rtx return_reg = DECL_RTL (decl);
4047 : 735490 : if (return_reg != val)
4048 : : {
4049 : 422008 : tree funtype = TREE_TYPE (current_function_decl);
4050 : 422008 : tree type = TREE_TYPE (decl);
4051 : 422008 : int unsignedp = TYPE_UNSIGNED (type);
4052 : 422008 : machine_mode old_mode = DECL_MODE (decl);
4053 : 422008 : machine_mode mode;
4054 : 422008 : if (DECL_BY_REFERENCE (decl))
4055 : 0 : mode = promote_function_mode (type, old_mode, &unsignedp, funtype, 2);
4056 : : else
4057 : 422008 : mode = promote_function_mode (type, old_mode, &unsignedp, funtype, 1);
4058 : :
4059 : 422008 : if (mode != old_mode)
4060 : : {
4061 : : /* Some ABIs require scalar floating point modes to be returned
4062 : : in a wider scalar integer mode. We need to explicitly
4063 : : reinterpret to an integer mode of the correct precision
4064 : : before extending to the desired result. */
4065 : 8 : if (SCALAR_INT_MODE_P (mode)
4066 : 8 : && SCALAR_FLOAT_MODE_P (old_mode)
4067 : 8 : && known_gt (GET_MODE_SIZE (mode), GET_MODE_SIZE (old_mode)))
4068 : 0 : val = convert_float_to_wider_int (mode, old_mode, val);
4069 : : else
4070 : 8 : val = convert_modes (mode, old_mode, val, unsignedp);
4071 : : }
4072 : :
4073 : 422008 : if (GET_CODE (return_reg) == PARALLEL)
4074 : 2907 : emit_group_load (return_reg, val, type, int_size_in_bytes (type));
4075 : : else
4076 : 419101 : emit_move_insn (return_reg, val);
4077 : : }
4078 : :
4079 : 735490 : expand_null_return_1 ();
4080 : 735490 : }
4081 : :
4082 : : /* Generate RTL to evaluate the expression RETVAL and return it
4083 : : from the current function. */
4084 : :
4085 : : static void
4086 : 737495 : expand_return (tree retval)
4087 : : {
4088 : 737495 : rtx result_rtl;
4089 : 737495 : rtx val = 0;
4090 : 737495 : tree retval_rhs;
4091 : :
4092 : : /* If function wants no value, give it none. */
4093 : 737495 : if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
4094 : : {
4095 : 0 : expand_normal (retval);
4096 : 0 : expand_null_return ();
4097 : 0 : return;
4098 : : }
4099 : :
4100 : 737495 : if (retval == error_mark_node)
4101 : : {
4102 : : /* Treat this like a return of no value from a function that
4103 : : returns a value. */
4104 : 0 : expand_null_return ();
4105 : 0 : return;
4106 : : }
4107 : 737495 : else if ((TREE_CODE (retval) == MODIFY_EXPR
4108 : 737495 : || TREE_CODE (retval) == INIT_EXPR)
4109 : 737495 : && TREE_CODE (TREE_OPERAND (retval, 0)) == RESULT_DECL)
4110 : 675725 : retval_rhs = TREE_OPERAND (retval, 1);
4111 : : else
4112 : : retval_rhs = retval;
4113 : :
4114 : 737495 : result_rtl = DECL_RTL (DECL_RESULT (current_function_decl));
4115 : :
4116 : : /* If we are returning the RESULT_DECL, then the value has already
4117 : : been stored into it, so we don't have to do anything special. */
4118 : 737495 : if (TREE_CODE (retval_rhs) == RESULT_DECL)
4119 : 61770 : expand_value_return (result_rtl);
4120 : :
4121 : : /* If the result is an aggregate that is being returned in one (or more)
4122 : : registers, load the registers here. */
4123 : :
4124 : 675725 : else if (retval_rhs != 0
4125 : 675725 : && TYPE_MODE (TREE_TYPE (retval_rhs)) == BLKmode
4126 : 4649 : && REG_P (result_rtl))
4127 : : {
4128 : 3225 : val = copy_blkmode_to_reg (GET_MODE (result_rtl), retval_rhs);
4129 : 3225 : if (val)
4130 : : {
4131 : : /* Use the mode of the result value on the return register. */
4132 : 1220 : PUT_MODE (result_rtl, GET_MODE (val));
4133 : 1220 : expand_value_return (val);
4134 : : }
4135 : : else
4136 : 2005 : expand_null_return ();
4137 : : }
4138 : 672500 : else if (retval_rhs != 0
4139 : 672500 : && !VOID_TYPE_P (TREE_TYPE (retval_rhs))
4140 : 672500 : && (REG_P (result_rtl)
4141 : 5785 : || (GET_CODE (result_rtl) == PARALLEL)))
4142 : : {
4143 : : /* Compute the return value into a temporary (usually a pseudo reg). */
4144 : 669622 : val
4145 : 669622 : = assign_temp (TREE_TYPE (DECL_RESULT (current_function_decl)), 0, 1);
4146 : 669622 : val = expand_expr (retval_rhs, val, GET_MODE (val), EXPAND_NORMAL);
4147 : 669622 : val = force_not_mem (val);
4148 : 669622 : expand_value_return (val);
4149 : : }
4150 : : else
4151 : : {
4152 : : /* No hard reg used; calculate value into hard return reg. */
4153 : 2878 : expand_expr (retval, const0_rtx, VOIDmode, EXPAND_NORMAL);
4154 : 2878 : expand_value_return (result_rtl);
4155 : : }
4156 : : }
4157 : :
4158 : : /* Expand a clobber of LHS. If LHS is stored it in a multi-part
4159 : : register, tell the rtl optimizers that its value is no longer
4160 : : needed. */
4161 : :
4162 : : static void
4163 : 1370231 : expand_clobber (tree lhs)
4164 : : {
4165 : 1370231 : if (DECL_P (lhs))
4166 : : {
4167 : 1157712 : rtx decl_rtl = DECL_RTL_IF_SET (lhs);
4168 : 1157669 : if (decl_rtl && REG_P (decl_rtl))
4169 : : {
4170 : 172972 : machine_mode decl_mode = GET_MODE (decl_rtl);
4171 : 345944 : if (maybe_gt (GET_MODE_SIZE (decl_mode),
4172 : : REGMODE_NATURAL_SIZE (decl_mode)))
4173 : 133070 : emit_clobber (decl_rtl);
4174 : : }
4175 : : }
4176 : 1370231 : }
4177 : :
4178 : : /* A subroutine of expand_gimple_stmt, expanding one gimple statement
4179 : : STMT that doesn't require special handling for outgoing edges. That
4180 : : is no tailcalls and no GIMPLE_COND. */
4181 : :
4182 : : static void
4183 : 30171191 : expand_gimple_stmt_1 (gimple *stmt)
4184 : : {
4185 : 30171191 : tree op0;
4186 : :
4187 : 30171191 : set_curr_insn_location (gimple_location (stmt));
4188 : :
4189 : 30171191 : switch (gimple_code (stmt))
4190 : : {
4191 : 393 : case GIMPLE_GOTO:
4192 : 393 : op0 = gimple_goto_dest (stmt);
4193 : 393 : if (TREE_CODE (op0) == LABEL_DECL)
4194 : 0 : expand_goto (op0);
4195 : : else
4196 : 393 : expand_computed_goto (op0);
4197 : : break;
4198 : 602620 : case GIMPLE_LABEL:
4199 : 602620 : expand_label (gimple_label_label (as_a <glabel *> (stmt)));
4200 : 602620 : break;
4201 : : case GIMPLE_NOP:
4202 : : case GIMPLE_PREDICT:
4203 : : break;
4204 : 6527 : case GIMPLE_SWITCH:
4205 : 6527 : {
4206 : 6527 : gswitch *swtch = as_a <gswitch *> (stmt);
4207 : 6527 : if (gimple_switch_num_labels (swtch) == 1)
4208 : 0 : expand_goto (CASE_LABEL (gimple_switch_default_label (swtch)));
4209 : : else
4210 : 6527 : expand_case (swtch);
4211 : : }
4212 : : break;
4213 : 106381 : case GIMPLE_ASM:
4214 : 106381 : expand_asm_stmt (as_a <gasm *> (stmt));
4215 : 106381 : break;
4216 : 6543383 : case GIMPLE_CALL:
4217 : 6543383 : expand_call_stmt (as_a <gcall *> (stmt));
4218 : 6543383 : break;
4219 : :
4220 : 803365 : case GIMPLE_RETURN:
4221 : 803365 : {
4222 : 803365 : op0 = gimple_return_retval (as_a <greturn *> (stmt));
4223 : :
4224 : : /* If a return doesn't have a location, it very likely represents
4225 : : multiple user returns so we cannot let it inherit the location
4226 : : of the last statement of the previous basic block in RTL. */
4227 : 803365 : if (!gimple_has_location (stmt))
4228 : 212619 : set_curr_insn_location (cfun->function_end_locus);
4229 : :
4230 : 803365 : if (op0 && op0 != error_mark_node)
4231 : : {
4232 : 737495 : tree result = DECL_RESULT (current_function_decl);
4233 : :
4234 : : /* If we are not returning the current function's RESULT_DECL,
4235 : : build an assignment to it. */
4236 : 737495 : if (op0 != result)
4237 : : {
4238 : : /* I believe that a function's RESULT_DECL is unique. */
4239 : 675725 : gcc_assert (TREE_CODE (op0) != RESULT_DECL);
4240 : :
4241 : : /* ??? We'd like to use simply expand_assignment here,
4242 : : but this fails if the value is of BLKmode but the return
4243 : : decl is a register. expand_return has special handling
4244 : : for this combination, which eventually should move
4245 : : to common code. See comments there. Until then, let's
4246 : : build a modify expression :-/ */
4247 : 675725 : op0 = build2 (MODIFY_EXPR, TREE_TYPE (result),
4248 : : result, op0);
4249 : : }
4250 : : }
4251 : :
4252 : 737495 : if (!op0)
4253 : 65870 : expand_null_return ();
4254 : : else
4255 : 737495 : expand_return (op0);
4256 : : }
4257 : : break;
4258 : :
4259 : 22064464 : case GIMPLE_ASSIGN:
4260 : 22064464 : {
4261 : 22064464 : gassign *assign_stmt = as_a <gassign *> (stmt);
4262 : 22064464 : tree lhs = gimple_assign_lhs (assign_stmt);
4263 : :
4264 : : /* Tree expand used to fiddle with |= and &= of two bitfield
4265 : : COMPONENT_REFs here. This can't happen with gimple, the LHS
4266 : : of binary assigns must be a gimple reg. */
4267 : :
4268 : 22064464 : if (TREE_CODE (lhs) != SSA_NAME
4269 : 35174899 : || gimple_assign_rhs_class (assign_stmt) == GIMPLE_SINGLE_RHS)
4270 : : {
4271 : 16479070 : tree rhs = gimple_assign_rhs1 (assign_stmt);
4272 : 16479070 : gcc_assert (gimple_assign_rhs_class (assign_stmt)
4273 : : == GIMPLE_SINGLE_RHS);
4274 : 31409662 : if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (rhs)
4275 : : /* Do not put locations on possibly shared trees. */
4276 : 22188066 : && !is_gimple_min_invariant (rhs))
4277 : 4661717 : SET_EXPR_LOCATION (rhs, gimple_location (stmt));
4278 : 16479070 : if (TREE_CLOBBER_P (rhs))
4279 : : /* This is a clobber to mark the going out of scope for
4280 : : this LHS. */
4281 : 1370231 : expand_clobber (lhs);
4282 : : else
4283 : 15108839 : expand_assignment (lhs, rhs,
4284 : 15108839 : gimple_assign_nontemporal_move_p (
4285 : : assign_stmt));
4286 : : }
4287 : : else
4288 : : {
4289 : 5585394 : rtx target, temp;
4290 : 5585394 : gcc_assert (!gimple_assign_nontemporal_move_p (assign_stmt));
4291 : 5585394 : bool promoted = false;
4292 : :
4293 : 5585394 : target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
4294 : 5585394 : if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
4295 : : promoted = true;
4296 : :
4297 : : /* If we store into a promoted register, don't directly
4298 : : expand to target. */
4299 : 5585394 : temp = promoted ? NULL_RTX : target;
4300 : 11170788 : temp = expand_expr_real_gassign (assign_stmt, temp,
4301 : 5585394 : GET_MODE (target), EXPAND_NORMAL);
4302 : :
4303 : 5585394 : if (temp == target)
4304 : : ;
4305 : 753639 : else if (promoted)
4306 : : {
4307 : 3 : int unsignedp = SUBREG_PROMOTED_SIGN (target);
4308 : : /* If TEMP is a VOIDmode constant, use convert_modes to make
4309 : : sure that we properly convert it. */
4310 : 3 : if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
4311 : : {
4312 : 0 : temp = convert_modes (GET_MODE (target),
4313 : 0 : TYPE_MODE (TREE_TYPE (lhs)),
4314 : : temp, unsignedp);
4315 : 0 : temp = convert_modes (GET_MODE (SUBREG_REG (target)),
4316 : 0 : GET_MODE (target), temp, unsignedp);
4317 : : }
4318 : :
4319 : 3 : convert_move (SUBREG_REG (target), temp, unsignedp);
4320 : : }
4321 : : else
4322 : : {
4323 : 753636 : temp = force_operand (temp, target);
4324 : 753636 : if (temp == target)
4325 : : ;
4326 : 753636 : else if (GET_MODE (target) != BLKmode)
4327 : 753635 : emit_move_insn (target, temp);
4328 : : else
4329 : 1 : emit_block_move (target, temp, expr_size (lhs),
4330 : : BLOCK_OP_NORMAL);
4331 : : }
4332 : : }
4333 : : }
4334 : : break;
4335 : :
4336 : 0 : default:
4337 : 0 : gcc_unreachable ();
4338 : : }
4339 : 30171191 : }
4340 : :
4341 : : /* Expand one gimple statement STMT and return the last RTL instruction
4342 : : before any of the newly generated ones.
4343 : :
4344 : : In addition to generating the necessary RTL instructions this also
4345 : : sets REG_EH_REGION notes if necessary and sets the current source
4346 : : location for diagnostics. */
4347 : :
4348 : : static rtx_insn *
4349 : 30171191 : expand_gimple_stmt (gimple *stmt)
4350 : : {
4351 : 30171191 : location_t saved_location = input_location;
4352 : 30171191 : rtx_insn *last = get_last_insn ();
4353 : 30171191 : int lp_nr;
4354 : :
4355 : 30171191 : gcc_assert (cfun);
4356 : :
4357 : : /* We need to save and restore the current source location so that errors
4358 : : discovered during expansion are emitted with the right location. But
4359 : : it would be better if the diagnostic routines used the source location
4360 : : embedded in the tree nodes rather than globals. */
4361 : 30171191 : if (gimple_has_location (stmt))
4362 : 26426993 : input_location = gimple_location (stmt);
4363 : :
4364 : 30171191 : expand_gimple_stmt_1 (stmt);
4365 : :
4366 : : /* Free any temporaries used to evaluate this statement. */
4367 : 30171191 : free_temp_slots ();
4368 : :
4369 : 30171191 : input_location = saved_location;
4370 : :
4371 : : /* Mark all insns that may trap. */
4372 : 30171191 : lp_nr = lookup_stmt_eh_lp (stmt);
4373 : 30171191 : if (lp_nr)
4374 : : {
4375 : 680974 : rtx_insn *insn;
4376 : 3887988 : for (insn = next_real_insn (last); insn;
4377 : 3207014 : insn = next_real_insn (insn))
4378 : : {
4379 : 3207014 : if (! find_reg_note (insn, REG_EH_REGION, NULL_RTX)
4380 : : /* If we want exceptions for non-call insns, any
4381 : : may_trap_p instruction may throw. */
4382 : 3207007 : && GET_CODE (PATTERN (insn)) != CLOBBER
4383 : 3206941 : && GET_CODE (PATTERN (insn)) != USE
4384 : 6413955 : && insn_could_throw_p (insn))
4385 : 739949 : make_reg_eh_region_note (insn, 0, lp_nr);
4386 : : }
4387 : : }
4388 : :
4389 : 30171191 : return last;
4390 : : }
4391 : :
4392 : : /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_CALL
4393 : : that has CALL_EXPR_TAILCALL set. Returns non-null if we actually
4394 : : generated a tail call (something that might be denied by the ABI
4395 : : rules governing the call; see calls.cc).
4396 : :
4397 : : Sets CAN_FALLTHRU if we generated a *conditional* tail call, and
4398 : : can still reach the rest of BB. The case here is __builtin_sqrt,
4399 : : where the NaN result goes through the external function (with a
4400 : : tailcall) and the normal result happens via a sqrt instruction. */
4401 : :
4402 : : static basic_block
4403 : 168240 : expand_gimple_tailcall (basic_block bb, gcall *stmt, bool *can_fallthru)
4404 : : {
4405 : 168240 : rtx_insn *last2, *last;
4406 : 168240 : edge e;
4407 : 168240 : edge_iterator ei;
4408 : 168240 : profile_probability probability;
4409 : :
4410 : 168240 : last2 = last = expand_gimple_stmt (stmt);
4411 : :
4412 : 744692 : for (last = NEXT_INSN (last); last; last = NEXT_INSN (last))
4413 : 694016 : if (CALL_P (last) && SIBLING_CALL_P (last))
4414 : 117564 : goto found;
4415 : :
4416 : 50676 : maybe_dump_rtl_for_gimple_stmt (stmt, last2);
4417 : :
4418 : 50676 : *can_fallthru = true;
4419 : 50676 : return NULL;
4420 : :
4421 : 117564 : found:
4422 : : /* ??? Wouldn't it be better to just reset any pending stack adjust?
4423 : : Any instructions emitted here are about to be deleted. */
4424 : 117564 : do_pending_stack_adjust ();
4425 : :
4426 : : /* Remove any non-eh, non-abnormal edges that don't go to exit. */
4427 : : /* ??? I.e. the fallthrough edge. HOWEVER! If there were to be
4428 : : EH or abnormal edges, we shouldn't have created a tail call in
4429 : : the first place. So it seems to me we should just be removing
4430 : : all edges here, or redirecting the existing fallthru edge to
4431 : : the exit block. */
4432 : :
4433 : 117564 : probability = profile_probability::never ();
4434 : :
4435 : 235128 : for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
4436 : : {
4437 : 117564 : if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
4438 : : {
4439 : 117564 : if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
4440 : 55146 : e->dest->count -= e->count ();
4441 : 117564 : probability += e->probability;
4442 : 117564 : remove_edge (e);
4443 : : }
4444 : : else
4445 : 0 : ei_next (&ei);
4446 : : }
4447 : :
4448 : : /* This is somewhat ugly: the call_expr expander often emits instructions
4449 : : after the sibcall (to perform the function return). These confuse the
4450 : : find_many_sub_basic_blocks code, so we need to get rid of these. */
4451 : 117564 : last = NEXT_INSN (last);
4452 : 117564 : gcc_assert (BARRIER_P (last));
4453 : :
4454 : 117564 : *can_fallthru = false;
4455 : 143279 : while (NEXT_INSN (last))
4456 : : {
4457 : : /* For instance an sqrt builtin expander expands if with
4458 : : sibcall in the then and label for `else`. */
4459 : 25715 : if (LABEL_P (NEXT_INSN (last)))
4460 : : {
4461 : 0 : *can_fallthru = true;
4462 : 0 : break;
4463 : : }
4464 : 25715 : delete_insn (NEXT_INSN (last));
4465 : : }
4466 : :
4467 : 117564 : e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_ABNORMAL
4468 : : | EDGE_SIBCALL);
4469 : 117564 : e->probability = probability;
4470 : 117564 : BB_END (bb) = last;
4471 : 117564 : update_bb_for_insn (bb);
4472 : :
4473 : 117564 : if (NEXT_INSN (last))
4474 : : {
4475 : 0 : bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
4476 : :
4477 : 0 : last = BB_END (bb);
4478 : 0 : if (BARRIER_P (last))
4479 : 0 : BB_END (bb) = PREV_INSN (last);
4480 : : }
4481 : :
4482 : 117564 : maybe_dump_rtl_for_gimple_stmt (stmt, last2);
4483 : :
4484 : 117564 : return bb;
4485 : : }
4486 : :
4487 : : /* Return the difference between the floor and the truncated result of
4488 : : a signed division by OP1 with remainder MOD. */
4489 : : static rtx
4490 : 4 : floor_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
4491 : : {
4492 : : /* (mod != 0 ? (op1 / mod < 0 ? -1 : 0) : 0) */
4493 : 4 : return gen_rtx_IF_THEN_ELSE
4494 : : (mode, gen_rtx_NE (BImode, mod, const0_rtx),
4495 : : gen_rtx_IF_THEN_ELSE
4496 : : (mode, gen_rtx_LT (BImode,
4497 : : gen_rtx_DIV (mode, op1, mod),
4498 : : const0_rtx),
4499 : : constm1_rtx, const0_rtx),
4500 : : const0_rtx);
4501 : : }
4502 : :
4503 : : /* Return the difference between the ceil and the truncated result of
4504 : : a signed division by OP1 with remainder MOD. */
4505 : : static rtx
4506 : 3 : ceil_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
4507 : : {
4508 : : /* (mod != 0 ? (op1 / mod > 0 ? 1 : 0) : 0) */
4509 : 3 : return gen_rtx_IF_THEN_ELSE
4510 : : (mode, gen_rtx_NE (BImode, mod, const0_rtx),
4511 : : gen_rtx_IF_THEN_ELSE
4512 : : (mode, gen_rtx_GT (BImode,
4513 : : gen_rtx_DIV (mode, op1, mod),
4514 : : const0_rtx),
4515 : : const1_rtx, const0_rtx),
4516 : : const0_rtx);
4517 : : }
4518 : :
4519 : : /* Return the difference between the ceil and the truncated result of
4520 : : an unsigned division by OP1 with remainder MOD. */
4521 : : static rtx
4522 : 0 : ceil_udiv_adjust (machine_mode mode, rtx mod, rtx op1 ATTRIBUTE_UNUSED)
4523 : : {
4524 : : /* (mod != 0 ? 1 : 0) */
4525 : 0 : return gen_rtx_IF_THEN_ELSE
4526 : : (mode, gen_rtx_NE (BImode, mod, const0_rtx),
4527 : : const1_rtx, const0_rtx);
4528 : : }
4529 : :
4530 : : /* Return the difference between the rounded and the truncated result
4531 : : of a signed division by OP1 with remainder MOD. Halfway cases are
4532 : : rounded away from zero, rather than to the nearest even number. */
4533 : : static rtx
4534 : 0 : round_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
4535 : : {
4536 : : /* (abs (mod) >= abs (op1) - abs (mod)
4537 : : ? (op1 / mod > 0 ? 1 : -1)
4538 : : : 0) */
4539 : 0 : return gen_rtx_IF_THEN_ELSE
4540 : : (mode, gen_rtx_GE (BImode, gen_rtx_ABS (mode, mod),
4541 : : gen_rtx_MINUS (mode,
4542 : : gen_rtx_ABS (mode, op1),
4543 : : gen_rtx_ABS (mode, mod))),
4544 : : gen_rtx_IF_THEN_ELSE
4545 : : (mode, gen_rtx_GT (BImode,
4546 : : gen_rtx_DIV (mode, op1, mod),
4547 : : const0_rtx),
4548 : : const1_rtx, constm1_rtx),
4549 : : const0_rtx);
4550 : : }
4551 : :
4552 : : /* Return the difference between the rounded and the truncated result
4553 : : of a unsigned division by OP1 with remainder MOD. Halfway cases
4554 : : are rounded away from zero, rather than to the nearest even
4555 : : number. */
4556 : : static rtx
4557 : 0 : round_udiv_adjust (machine_mode mode, rtx mod, rtx op1)
4558 : : {
4559 : : /* (mod >= op1 - mod ? 1 : 0) */
4560 : 0 : return gen_rtx_IF_THEN_ELSE
4561 : : (mode, gen_rtx_GE (BImode, mod,
4562 : : gen_rtx_MINUS (mode, op1, mod)),
4563 : : const1_rtx, const0_rtx);
4564 : : }
4565 : :
4566 : : /* Convert X to MODE, that must be Pmode or ptr_mode, without emitting
4567 : : any rtl. */
4568 : :
4569 : : static rtx
4570 : 5754149 : convert_debug_memory_address (scalar_int_mode mode, rtx x,
4571 : : addr_space_t as)
4572 : : {
4573 : : #ifndef POINTERS_EXTEND_UNSIGNED
4574 : : gcc_assert (mode == Pmode
4575 : : || mode == targetm.addr_space.address_mode (as));
4576 : : gcc_assert (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode);
4577 : : #else
4578 : 5754149 : rtx temp;
4579 : :
4580 : 5754149 : gcc_assert (targetm.addr_space.valid_pointer_mode (mode, as));
4581 : :
4582 : 5754149 : if (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode)
4583 : : return x;
4584 : :
4585 : : /* X must have some form of address mode already. */
4586 : 0 : scalar_int_mode xmode = as_a <scalar_int_mode> (GET_MODE (x));
4587 : 0 : if (GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (xmode))
4588 : 0 : x = lowpart_subreg (mode, x, xmode);
4589 : 0 : else if (POINTERS_EXTEND_UNSIGNED > 0)
4590 : 0 : x = gen_rtx_ZERO_EXTEND (mode, x);
4591 : : else if (!POINTERS_EXTEND_UNSIGNED)
4592 : : x = gen_rtx_SIGN_EXTEND (mode, x);
4593 : : else
4594 : : {
4595 : : switch (GET_CODE (x))
4596 : : {
4597 : : case SUBREG:
4598 : : if ((SUBREG_PROMOTED_VAR_P (x)
4599 : : || (REG_P (SUBREG_REG (x)) && REG_POINTER (SUBREG_REG (x)))
4600 : : || (GET_CODE (SUBREG_REG (x)) == PLUS
4601 : : && REG_P (XEXP (SUBREG_REG (x), 0))
4602 : : && REG_POINTER (XEXP (SUBREG_REG (x), 0))
4603 : : && CONST_INT_P (XEXP (SUBREG_REG (x), 1))))
4604 : : && GET_MODE (SUBREG_REG (x)) == mode)
4605 : : return SUBREG_REG (x);
4606 : : break;
4607 : : case LABEL_REF:
4608 : : temp = gen_rtx_LABEL_REF (mode, label_ref_label (x));
4609 : : LABEL_REF_NONLOCAL_P (temp) = LABEL_REF_NONLOCAL_P (x);
4610 : : return temp;
4611 : : case SYMBOL_REF:
4612 : : temp = shallow_copy_rtx (x);
4613 : : PUT_MODE (temp, mode);
4614 : : return temp;
4615 : : case CONST:
4616 : : temp = convert_debug_memory_address (mode, XEXP (x, 0), as);
4617 : : if (temp)
4618 : : temp = gen_rtx_CONST (mode, temp);
4619 : : return temp;
4620 : : case PLUS:
4621 : : case MINUS:
4622 : : if (CONST_INT_P (XEXP (x, 1)))
4623 : : {
4624 : : temp = convert_debug_memory_address (mode, XEXP (x, 0), as);
4625 : : if (temp)
4626 : : return gen_rtx_fmt_ee (GET_CODE (x), mode, temp, XEXP (x, 1));
4627 : : }
4628 : : break;
4629 : : default:
4630 : : break;
4631 : : }
4632 : : /* Don't know how to express ptr_extend as operation in debug info. */
4633 : : return NULL;
4634 : : }
4635 : : #endif /* POINTERS_EXTEND_UNSIGNED */
4636 : :
4637 : : return x;
4638 : : }
4639 : :
4640 : : /* Map from SSA_NAMEs to corresponding DEBUG_EXPR_DECLs created
4641 : : by avoid_deep_ter_for_debug. */
4642 : :
4643 : : static hash_map<tree, tree> *deep_ter_debug_map;
4644 : :
4645 : : /* Split too deep TER chains for debug stmts using debug temporaries. */
4646 : :
4647 : : static void
4648 : 31510124 : avoid_deep_ter_for_debug (gimple *stmt, int depth)
4649 : : {
4650 : 31510124 : use_operand_p use_p;
4651 : 31510124 : ssa_op_iter iter;
4652 : 39481272 : FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
4653 : : {
4654 : 7971148 : tree use = USE_FROM_PTR (use_p);
4655 : 7971148 : if (TREE_CODE (use) != SSA_NAME || SSA_NAME_IS_DEFAULT_DEF (use))
4656 : 6752166 : continue;
4657 : 6844766 : gimple *g = get_gimple_for_ssa_name (use);
4658 : 6844766 : if (g == NULL)
4659 : 5625420 : continue;
4660 : 1219346 : if ((depth > 6 || !is_gimple_assign (g)) && !stmt_ends_bb_p (g))
4661 : : {
4662 : 2694 : if (deep_ter_debug_map == NULL)
4663 : 528 : deep_ter_debug_map = new hash_map<tree, tree>;
4664 : :
4665 : 2694 : tree &vexpr = deep_ter_debug_map->get_or_insert (use);
4666 : 2694 : if (vexpr != NULL)
4667 : 364 : continue;
4668 : 2330 : vexpr = build_debug_expr_decl (TREE_TYPE (use));
4669 : 2330 : gimple *def_temp = gimple_build_debug_bind (vexpr, use, g);
4670 : 2330 : gimple_stmt_iterator gsi = gsi_for_stmt (g);
4671 : 2330 : gsi_insert_after (&gsi, def_temp, GSI_NEW_STMT);
4672 : 2330 : avoid_deep_ter_for_debug (def_temp, 0);
4673 : : }
4674 : : else
4675 : 1216652 : avoid_deep_ter_for_debug (g, depth + 1);
4676 : : }
4677 : 31510124 : }
4678 : :
4679 : : /* Return an RTX equivalent to the value of the parameter DECL. */
4680 : :
4681 : : static rtx
4682 : 136126 : expand_debug_parm_decl (tree decl)
4683 : : {
4684 : 136126 : rtx incoming = DECL_INCOMING_RTL (decl);
4685 : :
4686 : 136126 : if (incoming
4687 : 17625 : && GET_MODE (incoming) != BLKmode
4688 : 153747 : && ((REG_P (incoming) && HARD_REGISTER_P (incoming))
4689 : 1768 : || (MEM_P (incoming)
4690 : 1682 : && REG_P (XEXP (incoming, 0))
4691 : 757 : && HARD_REGISTER_P (XEXP (incoming, 0)))))
4692 : : {
4693 : 15881 : rtx rtl = gen_rtx_ENTRY_VALUE (GET_MODE (incoming));
4694 : :
4695 : : #ifdef HAVE_window_save
4696 : : /* DECL_INCOMING_RTL uses the INCOMING_REGNO of parameter registers.
4697 : : If the target machine has an explicit window save instruction, the
4698 : : actual entry value is the corresponding OUTGOING_REGNO instead. */
4699 : : if (REG_P (incoming)
4700 : : && OUTGOING_REGNO (REGNO (incoming)) != REGNO (incoming))
4701 : : incoming
4702 : : = gen_rtx_REG_offset (incoming, GET_MODE (incoming),
4703 : : OUTGOING_REGNO (REGNO (incoming)), 0);
4704 : : else if (MEM_P (incoming))
4705 : : {
4706 : : rtx reg = XEXP (incoming, 0);
4707 : : if (OUTGOING_REGNO (REGNO (reg)) != REGNO (reg))
4708 : : {
4709 : : reg = gen_raw_REG (GET_MODE (reg), OUTGOING_REGNO (REGNO (reg)));
4710 : : incoming = replace_equiv_address_nv (incoming, reg);
4711 : : }
4712 : : else
4713 : : incoming = copy_rtx (incoming);
4714 : : }
4715 : : #endif
4716 : :
4717 : 15881 : ENTRY_VALUE_EXP (rtl) = incoming;
4718 : 15881 : return rtl;
4719 : : }
4720 : :
4721 : 120245 : if (incoming
4722 : 1744 : && GET_MODE (incoming) != BLKmode
4723 : 1740 : && !TREE_ADDRESSABLE (decl)
4724 : 1740 : && MEM_P (incoming)
4725 : 1654 : && (XEXP (incoming, 0) == virtual_incoming_args_rtx
4726 : 925 : || (GET_CODE (XEXP (incoming, 0)) == PLUS
4727 : 925 : && XEXP (XEXP (incoming, 0), 0) == virtual_incoming_args_rtx
4728 : 911 : && CONST_INT_P (XEXP (XEXP (incoming, 0), 1)))))
4729 : 1640 : return copy_rtx (incoming);
4730 : :
4731 : : return NULL_RTX;
4732 : : }
4733 : :
4734 : : /* Return an RTX equivalent to the value of the tree expression EXP. */
4735 : :
4736 : : static rtx
4737 : 32722478 : expand_debug_expr (tree exp)
4738 : : {
4739 : 32722478 : rtx op0 = NULL_RTX, op1 = NULL_RTX, op2 = NULL_RTX;
4740 : 32722478 : machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
4741 : 32722478 : machine_mode inner_mode = VOIDmode;
4742 : 32722478 : int unsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
4743 : 32722478 : addr_space_t as;
4744 : 32722478 : scalar_int_mode op0_mode, op1_mode, addr_mode;
4745 : :
4746 : 32722478 : switch (TREE_CODE_CLASS (TREE_CODE (exp)))
4747 : : {
4748 : 5322755 : case tcc_expression:
4749 : 5322755 : switch (TREE_CODE (exp))
4750 : : {
4751 : 9769 : case COND_EXPR:
4752 : 9769 : case DOT_PROD_EXPR:
4753 : 9769 : case SAD_EXPR:
4754 : 9769 : case WIDEN_MULT_PLUS_EXPR:
4755 : 9769 : case WIDEN_MULT_MINUS_EXPR:
4756 : 9769 : goto ternary;
4757 : :
4758 : 0 : case TRUTH_ANDIF_EXPR:
4759 : 0 : case TRUTH_ORIF_EXPR:
4760 : 0 : case TRUTH_AND_EXPR:
4761 : 0 : case TRUTH_OR_EXPR:
4762 : 0 : case TRUTH_XOR_EXPR:
4763 : 0 : goto binary;
4764 : :
4765 : 0 : case TRUTH_NOT_EXPR:
4766 : 0 : goto unary;
4767 : :
4768 : : default:
4769 : : break;
4770 : : }
4771 : : break;
4772 : :
4773 : 9769 : ternary:
4774 : 9769 : op2 = expand_debug_expr (TREE_OPERAND (exp, 2));
4775 : 9769 : if (!op2)
4776 : : return NULL_RTX;
4777 : : /* Fall through. */
4778 : :
4779 : 9769 : binary:
4780 : 1083890 : case tcc_binary:
4781 : 1083890 : if (mode == BLKmode)
4782 : : return NULL_RTX;
4783 : 1083880 : op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
4784 : 1083880 : if (!op1)
4785 : : return NULL_RTX;
4786 : 1083172 : switch (TREE_CODE (exp))
4787 : : {
4788 : 59745 : case LSHIFT_EXPR:
4789 : 59745 : case RSHIFT_EXPR:
4790 : 59745 : case LROTATE_EXPR:
4791 : 59745 : case RROTATE_EXPR:
4792 : 59745 : case WIDEN_LSHIFT_EXPR:
4793 : : /* Ensure second operand isn't wider than the first one. */
4794 : 59745 : inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 1)));
4795 : 59745 : if (is_a <scalar_int_mode> (inner_mode, &op1_mode)
4796 : 59737 : && (GET_MODE_UNIT_PRECISION (mode)
4797 : 59737 : < GET_MODE_PRECISION (op1_mode)))
4798 : 5028 : op1 = lowpart_subreg (GET_MODE_INNER (mode), op1, op1_mode);
4799 : : break;
4800 : : default:
4801 : : break;
4802 : : }
4803 : : /* Fall through. */
4804 : :
4805 : 1573238 : unary:
4806 : 1573238 : case tcc_unary:
4807 : 1573238 : if (mode == BLKmode)
4808 : : return NULL_RTX;
4809 : 1573236 : inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
4810 : 1573236 : op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
4811 : 1573236 : if (!op0)
4812 : : return NULL_RTX;
4813 : : break;
4814 : :
4815 : 57863 : case tcc_comparison:
4816 : 57863 : unsignedp = TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0)));
4817 : 57863 : goto binary;
4818 : :
4819 : 0 : case tcc_type:
4820 : 0 : case tcc_statement:
4821 : 0 : gcc_unreachable ();
4822 : :
4823 : : case tcc_constant:
4824 : : case tcc_exceptional:
4825 : : case tcc_declaration:
4826 : : case tcc_reference:
4827 : : case tcc_vl_exp:
4828 : : break;
4829 : : }
4830 : :
4831 : 32720030 : switch (TREE_CODE (exp))
4832 : : {
4833 : 222475 : case STRING_CST:
4834 : 222475 : if (!lookup_constant_def (exp))
4835 : : {
4836 : 86629 : if (strlen (TREE_STRING_POINTER (exp)) + 1
4837 : 86629 : != (size_t) TREE_STRING_LENGTH (exp))
4838 : : return NULL_RTX;
4839 : 85732 : op0 = gen_rtx_CONST_STRING (Pmode, TREE_STRING_POINTER (exp));
4840 : 85732 : op0 = gen_rtx_MEM (BLKmode, op0);
4841 : 85732 : set_mem_attributes (op0, exp, 0);
4842 : 85732 : return op0;
4843 : : }
4844 : : /* Fall through. */
4845 : :
4846 : 4522027 : case INTEGER_CST:
4847 : 4522027 : if (TREE_CODE (TREE_TYPE (exp)) == BITINT_TYPE
4848 : 4522027 : && TYPE_MODE (TREE_TYPE (exp)) == BLKmode)
4849 : : return NULL;
4850 : : /* FALLTHRU */
4851 : 4580380 : case REAL_CST:
4852 : 4580380 : case FIXED_CST:
4853 : 4580380 : op0 = expand_expr (exp, NULL_RTX, mode, EXPAND_INITIALIZER);
4854 : 4580380 : return op0;
4855 : :
4856 : 0 : case POLY_INT_CST:
4857 : 0 : return immed_wide_int_const (poly_int_cst_value (exp), mode);
4858 : :
4859 : 4051 : case COMPLEX_CST:
4860 : 4051 : gcc_assert (COMPLEX_MODE_P (mode));
4861 : 4051 : op0 = expand_debug_expr (TREE_REALPART (exp));
4862 : 4051 : op1 = expand_debug_expr (TREE_IMAGPART (exp));
4863 : 4051 : return gen_rtx_CONCAT (mode, op0, op1);
4864 : :
4865 : 4385311 : case DEBUG_EXPR_DECL:
4866 : 4385311 : op0 = DECL_RTL_IF_SET (exp);
4867 : :
4868 : 3366699 : if (op0)
4869 : : {
4870 : 3366699 : if (GET_MODE (op0) != mode)
4871 : 0 : gcc_assert (VECTOR_TYPE_P (TREE_TYPE (exp)));
4872 : : else
4873 : : return op0;
4874 : : }
4875 : :
4876 : 1018612 : op0 = gen_rtx_DEBUG_EXPR (mode);
4877 : 1018612 : DEBUG_EXPR_TREE_DECL (op0) = exp;
4878 : 1018612 : SET_DECL_RTL (exp, op0);
4879 : :
4880 : 1018612 : return op0;
4881 : :
4882 : 4221218 : case VAR_DECL:
4883 : 4221218 : case PARM_DECL:
4884 : 4221218 : case FUNCTION_DECL:
4885 : 4221218 : case LABEL_DECL:
4886 : 4221218 : case CONST_DECL:
4887 : 4221218 : case RESULT_DECL:
4888 : 4221218 : op0 = DECL_RTL_IF_SET (exp);
4889 : :
4890 : : /* This decl was probably optimized away. */
4891 : 3060951 : if (!op0
4892 : : /* At least label RTXen are sometimes replaced by
4893 : : NOTE_INSN_DELETED_LABEL. Any notes here are not
4894 : : handled by copy_rtx. */
4895 : 3060951 : || NOTE_P (op0))
4896 : : {
4897 : 1160268 : if (!VAR_P (exp)
4898 : 1153363 : || DECL_EXTERNAL (exp)
4899 : 1152794 : || !TREE_STATIC (exp)
4900 : 48359 : || !DECL_NAME (exp)
4901 : 48359 : || DECL_HARD_REGISTER (exp)
4902 : 48359 : || DECL_IN_CONSTANT_POOL (exp)
4903 : 48359 : || mode == VOIDmode
4904 : 1208627 : || symtab_node::get (exp) == NULL)
4905 : : return NULL;
4906 : :
4907 : 11295 : op0 = make_decl_rtl_for_debug (exp);
4908 : 11295 : if (!MEM_P (op0)
4909 : 11295 : || GET_CODE (XEXP (op0, 0)) != SYMBOL_REF
4910 : 22590 : || SYMBOL_REF_DECL (XEXP (op0, 0)) != exp)
4911 : : return NULL;
4912 : : }
4913 : 3060950 : else if (VAR_P (exp)
4914 : 2961361 : && is_global_var (exp)
4915 : 3218058 : && symtab_node::get (exp) == NULL)
4916 : : return NULL;
4917 : : else
4918 : 3060950 : op0 = copy_rtx (op0);
4919 : :
4920 : 3072245 : if (GET_MODE (op0) == BLKmode
4921 : : /* If op0 is not BLKmode, but mode is, adjust_mode
4922 : : below would ICE. While it is likely a FE bug,
4923 : : try to be robust here. See PR43166. */
4924 : 398035 : || mode == BLKmode
4925 : 398035 : || (mode == VOIDmode && GET_MODE (op0) != VOIDmode))
4926 : : {
4927 : 2674210 : gcc_assert (MEM_P (op0));
4928 : 2674210 : op0 = adjust_address_nv (op0, mode, 0);
4929 : 2674210 : return op0;
4930 : : }
4931 : :
4932 : : /* Fall through. */
4933 : :
4934 : 398035 : adjust_mode:
4935 : 8696452 : case PAREN_EXPR:
4936 : 8696452 : CASE_CONVERT:
4937 : 8696452 : {
4938 : 8696452 : inner_mode = GET_MODE (op0);
4939 : :
4940 : 8696452 : if (mode == inner_mode)
4941 : : return op0;
4942 : :
4943 : 134483 : if (inner_mode == VOIDmode)
4944 : : {
4945 : 4633 : if (TREE_CODE (exp) == SSA_NAME)
4946 : 19 : inner_mode = TYPE_MODE (TREE_TYPE (exp));
4947 : : else
4948 : 4614 : inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
4949 : 4633 : if (mode == inner_mode)
4950 : : return op0;
4951 : : }
4952 : :
4953 : 132222 : if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
4954 : : {
4955 : 6185 : if (GET_MODE_UNIT_BITSIZE (mode)
4956 : 12370 : == GET_MODE_UNIT_BITSIZE (inner_mode))
4957 : 4 : op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
4958 : 6181 : else if (GET_MODE_UNIT_BITSIZE (mode)
4959 : 12362 : < GET_MODE_UNIT_BITSIZE (inner_mode))
4960 : 3538 : op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
4961 : : else
4962 : 2643 : op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
4963 : : }
4964 : 126037 : else if (FLOAT_MODE_P (mode))
4965 : : {
4966 : 0 : gcc_assert (TREE_CODE (exp) != SSA_NAME);
4967 : 0 : if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
4968 : 0 : op0 = simplify_gen_unary (UNSIGNED_FLOAT, mode, op0, inner_mode);
4969 : : else
4970 : 0 : op0 = simplify_gen_unary (FLOAT, mode, op0, inner_mode);
4971 : : }
4972 : 126037 : else if (FLOAT_MODE_P (inner_mode))
4973 : : {
4974 : 0 : if (unsignedp)
4975 : 0 : op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
4976 : : else
4977 : 0 : op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
4978 : : }
4979 : 252074 : else if (GET_MODE_UNIT_PRECISION (mode)
4980 : 126037 : == GET_MODE_UNIT_PRECISION (inner_mode))
4981 : 0 : op0 = lowpart_subreg (mode, op0, inner_mode);
4982 : 252074 : else if (GET_MODE_UNIT_PRECISION (mode)
4983 : 126037 : < GET_MODE_UNIT_PRECISION (inner_mode))
4984 : 56619 : op0 = simplify_gen_unary (TRUNCATE, mode, op0, inner_mode);
4985 : 69418 : else if (UNARY_CLASS_P (exp)
4986 : 69418 : ? TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0)))
4987 : 0 : : unsignedp)
4988 : 51345 : op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
4989 : : else
4990 : 18073 : op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
4991 : :
4992 : : return op0;
4993 : : }
4994 : :
4995 : 1708869 : case MEM_REF:
4996 : 1708869 : if (!is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
4997 : : {
4998 : 659449 : tree newexp = fold_binary (MEM_REF, TREE_TYPE (exp),
4999 : : TREE_OPERAND (exp, 0),
5000 : : TREE_OPERAND (exp, 1));
5001 : 659449 : if (newexp)
5002 : 0 : return expand_debug_expr (newexp);
5003 : : }
5004 : : /* FALLTHROUGH */
5005 : 1708869 : case INDIRECT_REF:
5006 : 1708869 : inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
5007 : 1708869 : op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
5008 : 1708869 : if (!op0)
5009 : : return NULL;
5010 : :
5011 : 1705662 : if (TREE_CODE (exp) == MEM_REF)
5012 : : {
5013 : 1705662 : if (GET_CODE (op0) == DEBUG_IMPLICIT_PTR
5014 : 1675992 : || (GET_CODE (op0) == PLUS
5015 : 203419 : && GET_CODE (XEXP (op0, 0)) == DEBUG_IMPLICIT_PTR))
5016 : : /* (mem (debug_implicit_ptr)) might confuse aliasing.
5017 : : Instead just use get_inner_reference. */
5018 : 29670 : goto component_ref;
5019 : :
5020 : 1675992 : op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
5021 : 1675992 : poly_int64 offset;
5022 : 1675992 : if (!op1 || !poly_int_rtx_p (op1, &offset))
5023 : 0 : return NULL;
5024 : :
5025 : 1675992 : op0 = plus_constant (inner_mode, op0, offset);
5026 : : }
5027 : :
5028 : 1675992 : as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
5029 : :
5030 : 1675992 : op0 = convert_debug_memory_address (targetm.addr_space.address_mode (as),
5031 : : op0, as);
5032 : 1675992 : if (op0 == NULL_RTX)
5033 : : return NULL;
5034 : :
5035 : 1675992 : op0 = gen_rtx_MEM (mode, op0);
5036 : 1675992 : set_mem_attributes (op0, exp, 0);
5037 : 1675992 : if (TREE_CODE (exp) == MEM_REF
5038 : 1675992 : && !is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
5039 : 659449 : set_mem_expr (op0, NULL_TREE);
5040 : 1675992 : set_mem_addr_space (op0, as);
5041 : :
5042 : 1675992 : return op0;
5043 : :
5044 : 42255 : case TARGET_MEM_REF:
5045 : 42255 : if (TREE_CODE (TMR_BASE (exp)) == ADDR_EXPR
5046 : 42255 : && !DECL_RTL_SET_P (TREE_OPERAND (TMR_BASE (exp), 0)))
5047 : : return NULL;
5048 : :
5049 : 42245 : op0 = expand_debug_expr
5050 : 42245 : (tree_mem_ref_addr (build_pointer_type (TREE_TYPE (exp)), exp));
5051 : 42245 : if (!op0)
5052 : : return NULL;
5053 : :
5054 : 42245 : as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
5055 : 42245 : op0 = convert_debug_memory_address (targetm.addr_space.address_mode (as),
5056 : : op0, as);
5057 : 42245 : if (op0 == NULL_RTX)
5058 : : return NULL;
5059 : :
5060 : 42245 : op0 = gen_rtx_MEM (mode, op0);
5061 : :
5062 : 42245 : set_mem_attributes (op0, exp, 0);
5063 : 42245 : set_mem_addr_space (op0, as);
5064 : :
5065 : 42245 : return op0;
5066 : :
5067 : 2983105 : component_ref:
5068 : 2983105 : case ARRAY_REF:
5069 : 2983105 : case ARRAY_RANGE_REF:
5070 : 2983105 : case COMPONENT_REF:
5071 : 2983105 : case BIT_FIELD_REF:
5072 : 2983105 : case REALPART_EXPR:
5073 : 2983105 : case IMAGPART_EXPR:
5074 : 2983105 : case VIEW_CONVERT_EXPR:
5075 : 2983105 : {
5076 : 2983105 : machine_mode mode1;
5077 : 2983105 : poly_int64 bitsize, bitpos;
5078 : 2983105 : tree offset;
5079 : 2983105 : int reversep, volatilep = 0;
5080 : 2983105 : tree tem
5081 : 2983105 : = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
5082 : : &unsignedp, &reversep, &volatilep);
5083 : 2983105 : rtx orig_op0;
5084 : :
5085 : 2983105 : if (known_eq (bitsize, 0))
5086 : : return NULL;
5087 : :
5088 : 2951213 : orig_op0 = op0 = expand_debug_expr (tem);
5089 : :
5090 : 2951213 : if (!op0)
5091 : : return NULL;
5092 : :
5093 : 2726586 : if (offset)
5094 : : {
5095 : 12852 : machine_mode addrmode, offmode;
5096 : :
5097 : 12852 : if (!MEM_P (op0))
5098 : : return NULL;
5099 : :
5100 : 12852 : op0 = XEXP (op0, 0);
5101 : 12852 : addrmode = GET_MODE (op0);
5102 : 12852 : if (addrmode == VOIDmode)
5103 : 0 : addrmode = Pmode;
5104 : :
5105 : 12852 : op1 = expand_debug_expr (offset);
5106 : 12852 : if (!op1)
5107 : : return NULL;
5108 : :
5109 : 12850 : offmode = GET_MODE (op1);
5110 : 12850 : if (offmode == VOIDmode)
5111 : 0 : offmode = TYPE_MODE (TREE_TYPE (offset));
5112 : :
5113 : 12850 : if (addrmode != offmode)
5114 : 0 : op1 = lowpart_subreg (addrmode, op1, offmode);
5115 : :
5116 : : /* Don't use offset_address here, we don't need a
5117 : : recognizable address, and we don't want to generate
5118 : : code. */
5119 : 12850 : op0 = gen_rtx_MEM (mode, simplify_gen_binary (PLUS, addrmode,
5120 : : op0, op1));
5121 : : }
5122 : :
5123 : 2726584 : if (MEM_P (op0))
5124 : : {
5125 : 2624464 : if (mode1 == VOIDmode)
5126 : : {
5127 : 2688 : if (maybe_gt (bitsize, MAX_BITSIZE_MODE_ANY_INT))
5128 : 256555 : return NULL;
5129 : : /* Bitfield. */
5130 : 2688 : mode1 = smallest_int_mode_for_size (bitsize).require ();
5131 : : }
5132 : 2624464 : poly_int64 bytepos = bits_to_bytes_round_down (bitpos);
5133 : 2624464 : if (maybe_ne (bytepos, 0))
5134 : : {
5135 : 1179428 : op0 = adjust_address_nv (op0, mode1, bytepos);
5136 : 1179428 : bitpos = num_trailing_bits (bitpos);
5137 : : }
5138 : 1445036 : else if (known_eq (bitpos, 0)
5139 : 2888289 : && known_eq (bitsize, GET_MODE_BITSIZE (mode)))
5140 : 395865 : op0 = adjust_address_nv (op0, mode, 0);
5141 : 1049171 : else if (GET_MODE (op0) != mode1)
5142 : 4616 : op0 = adjust_address_nv (op0, mode1, 0);
5143 : : else
5144 : 1044555 : op0 = copy_rtx (op0);
5145 : 2624464 : if (op0 == orig_op0)
5146 : 89430 : op0 = shallow_copy_rtx (op0);
5147 : 2624464 : if (TREE_CODE (tem) != SSA_NAME)
5148 : 2621307 : set_mem_attributes (op0, exp, 0);
5149 : : }
5150 : :
5151 : 2726584 : if (known_eq (bitpos, 0) && mode == GET_MODE (op0))
5152 : : return op0;
5153 : :
5154 : 78833 : if (maybe_lt (bitpos, 0))
5155 : : return NULL;
5156 : :
5157 : 78831 : if (GET_MODE (op0) == BLKmode || mode == BLKmode)
5158 : : return NULL;
5159 : :
5160 : 78820 : poly_int64 bytepos;
5161 : 79359 : if (multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
5162 : 153580 : && known_eq (bitsize, GET_MODE_BITSIZE (mode1)))
5163 : : {
5164 : 76267 : machine_mode opmode = GET_MODE (op0);
5165 : :
5166 : 76267 : if (opmode == VOIDmode)
5167 : 0 : opmode = TYPE_MODE (TREE_TYPE (tem));
5168 : :
5169 : : /* This condition may hold if we're expanding the address
5170 : : right past the end of an array that turned out not to
5171 : : be addressable (i.e., the address was only computed in
5172 : : debug stmts). The gen_subreg below would rightfully
5173 : : crash, and the address doesn't really exist, so just
5174 : : drop it. */
5175 : 152534 : if (known_ge (bitpos, GET_MODE_BITSIZE (opmode)))
5176 : : return NULL;
5177 : :
5178 : 152492 : if (multiple_p (bitpos, GET_MODE_BITSIZE (mode)))
5179 : 76230 : return simplify_gen_subreg (mode, op0, opmode, bytepos);
5180 : : }
5181 : :
5182 : 5138 : return simplify_gen_ternary (SCALAR_INT_MODE_P (GET_MODE (op0))
5183 : 2168 : && TYPE_UNSIGNED (TREE_TYPE (exp))
5184 : : ? SIGN_EXTRACT
5185 : : : ZERO_EXTRACT, mode,
5186 : 2569 : GET_MODE (op0) != VOIDmode
5187 : : ? GET_MODE (op0)
5188 : 0 : : TYPE_MODE (TREE_TYPE (tem)),
5189 : : op0, gen_int_mode (bitsize, word_mode),
5190 : 2569 : gen_int_mode (bitpos, word_mode));
5191 : : }
5192 : :
5193 : 2284 : case ABS_EXPR:
5194 : 2284 : case ABSU_EXPR:
5195 : 2284 : return simplify_gen_unary (ABS, mode, op0, mode);
5196 : :
5197 : 13601 : case NEGATE_EXPR:
5198 : 13601 : return simplify_gen_unary (NEG, mode, op0, mode);
5199 : :
5200 : 15540 : case BIT_NOT_EXPR:
5201 : 15540 : return simplify_gen_unary (NOT, mode, op0, mode);
5202 : :
5203 : 2254 : case FLOAT_EXPR:
5204 : 2254 : return simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
5205 : : 0)))
5206 : : ? UNSIGNED_FLOAT : FLOAT, mode, op0,
5207 : 2254 : inner_mode);
5208 : :
5209 : 468 : case FIX_TRUNC_EXPR:
5210 : 468 : return simplify_gen_unary (unsignedp ? UNSIGNED_FIX : FIX, mode, op0,
5211 : 468 : inner_mode);
5212 : :
5213 : 175232 : case POINTER_PLUS_EXPR:
5214 : : /* For the rare target where pointers are not the same size as
5215 : : size_t, we need to check for mis-matched modes and correct
5216 : : the addend. */
5217 : 175232 : if (op0 && op1
5218 : 175232 : && is_a <scalar_int_mode> (GET_MODE (op0), &op0_mode)
5219 : 175140 : && is_a <scalar_int_mode> (GET_MODE (op1), &op1_mode)
5220 : 278448 : && op0_mode != op1_mode)
5221 : : {
5222 : 0 : if (GET_MODE_BITSIZE (op0_mode) < GET_MODE_BITSIZE (op1_mode)
5223 : : /* If OP0 is a partial mode, then we must truncate, even
5224 : : if it has the same bitsize as OP1 as GCC's
5225 : : representation of partial modes is opaque. */
5226 : 0 : || (GET_MODE_CLASS (op0_mode) == MODE_PARTIAL_INT
5227 : 0 : && (GET_MODE_BITSIZE (op0_mode)
5228 : 0 : == GET_MODE_BITSIZE (op1_mode))))
5229 : 0 : op1 = simplify_gen_unary (TRUNCATE, op0_mode, op1, op1_mode);
5230 : : else
5231 : : /* We always sign-extend, regardless of the signedness of
5232 : : the operand, because the operand is always unsigned
5233 : : here even if the original C expression is signed. */
5234 : 0 : op1 = simplify_gen_unary (SIGN_EXTEND, op0_mode, op1, op1_mode);
5235 : : }
5236 : : /* Fall through. */
5237 : 522578 : case PLUS_EXPR:
5238 : 522578 : return simplify_gen_binary (PLUS, mode, op0, op1);
5239 : :
5240 : 96562 : case MINUS_EXPR:
5241 : 96562 : case POINTER_DIFF_EXPR:
5242 : 96562 : return simplify_gen_binary (MINUS, mode, op0, op1);
5243 : :
5244 : 108756 : case MULT_EXPR:
5245 : 108756 : return simplify_gen_binary (MULT, mode, op0, op1);
5246 : :
5247 : 59818 : case RDIV_EXPR:
5248 : 59818 : case TRUNC_DIV_EXPR:
5249 : 59818 : case EXACT_DIV_EXPR:
5250 : 59818 : if (unsignedp)
5251 : 8708 : return simplify_gen_binary (UDIV, mode, op0, op1);
5252 : : else
5253 : 51110 : return simplify_gen_binary (DIV, mode, op0, op1);
5254 : :
5255 : 6304 : case TRUNC_MOD_EXPR:
5256 : 12608 : return simplify_gen_binary (unsignedp ? UMOD : MOD, mode, op0, op1);
5257 : :
5258 : 3 : case FLOOR_DIV_EXPR:
5259 : 3 : if (unsignedp)
5260 : 0 : return simplify_gen_binary (UDIV, mode, op0, op1);
5261 : : else
5262 : : {
5263 : 3 : rtx div = simplify_gen_binary (DIV, mode, op0, op1);
5264 : 3 : rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
5265 : 3 : rtx adj = floor_sdiv_adjust (mode, mod, op1);
5266 : 3 : return simplify_gen_binary (PLUS, mode, div, adj);
5267 : : }
5268 : :
5269 : 1 : case FLOOR_MOD_EXPR:
5270 : 1 : if (unsignedp)
5271 : 0 : return simplify_gen_binary (UMOD, mode, op0, op1);
5272 : : else
5273 : : {
5274 : 1 : rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
5275 : 1 : rtx adj = floor_sdiv_adjust (mode, mod, op1);
5276 : 1 : adj = simplify_gen_unary (NEG, mode,
5277 : : simplify_gen_binary (MULT, mode, adj, op1),
5278 : : mode);
5279 : 1 : return simplify_gen_binary (PLUS, mode, mod, adj);
5280 : : }
5281 : :
5282 : 3 : case CEIL_DIV_EXPR:
5283 : 3 : if (unsignedp)
5284 : : {
5285 : 0 : rtx div = simplify_gen_binary (UDIV, mode, op0, op1);
5286 : 0 : rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
5287 : 0 : rtx adj = ceil_udiv_adjust (mode, mod, op1);
5288 : 0 : return simplify_gen_binary (PLUS, mode, div, adj);
5289 : : }
5290 : : else
5291 : : {
5292 : 3 : rtx div = simplify_gen_binary (DIV, mode, op0, op1);
5293 : 3 : rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
5294 : 3 : rtx adj = ceil_sdiv_adjust (mode, mod, op1);
5295 : 3 : return simplify_gen_binary (PLUS, mode, div, adj);
5296 : : }
5297 : :
5298 : 0 : case CEIL_MOD_EXPR:
5299 : 0 : if (unsignedp)
5300 : : {
5301 : 0 : rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
5302 : 0 : rtx adj = ceil_udiv_adjust (mode, mod, op1);
5303 : 0 : adj = simplify_gen_unary (NEG, mode,
5304 : : simplify_gen_binary (MULT, mode, adj, op1),
5305 : : mode);
5306 : 0 : return simplify_gen_binary (PLUS, mode, mod, adj);
5307 : : }
5308 : : else
5309 : : {
5310 : 0 : rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
5311 : 0 : rtx adj = ceil_sdiv_adjust (mode, mod, op1);
5312 : 0 : adj = simplify_gen_unary (NEG, mode,
5313 : : simplify_gen_binary (MULT, mode, adj, op1),
5314 : : mode);
5315 : 0 : return simplify_gen_binary (PLUS, mode, mod, adj);
5316 : : }
5317 : :
5318 : 0 : case ROUND_DIV_EXPR:
5319 : 0 : if (unsignedp)
5320 : : {
5321 : 0 : rtx div = simplify_gen_binary (UDIV, mode, op0, op1);
5322 : 0 : rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
5323 : 0 : rtx adj = round_udiv_adjust (mode, mod, op1);
5324 : 0 : return simplify_gen_binary (PLUS, mode, div, adj);
5325 : : }
5326 : : else
5327 : : {
5328 : 0 : rtx div = simplify_gen_binary (DIV, mode, op0, op1);
5329 : 0 : rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
5330 : 0 : rtx adj = round_sdiv_adjust (mode, mod, op1);
5331 : 0 : return simplify_gen_binary (PLUS, mode, div, adj);
5332 : : }
5333 : :
5334 : 0 : case ROUND_MOD_EXPR:
5335 : 0 : if (unsignedp)
5336 : : {
5337 : 0 : rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
5338 : 0 : rtx adj = round_udiv_adjust (mode, mod, op1);
5339 : 0 : adj = simplify_gen_unary (NEG, mode,
5340 : : simplify_gen_binary (MULT, mode, adj, op1),
5341 : : mode);
5342 : 0 : return simplify_gen_binary (PLUS, mode, mod, adj);
5343 : : }
5344 : : else
5345 : : {
5346 : 0 : rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
5347 : 0 : rtx adj = round_sdiv_adjust (mode, mod, op1);
5348 : 0 : adj = simplify_gen_unary (NEG, mode,
5349 : : simplify_gen_binary (MULT, mode, adj, op1),
5350 : : mode);
5351 : 0 : return simplify_gen_binary (PLUS, mode, mod, adj);
5352 : : }
5353 : :
5354 : 14109 : case LSHIFT_EXPR:
5355 : 14109 : return simplify_gen_binary (ASHIFT, mode, op0, op1);
5356 : :
5357 : 44897 : case RSHIFT_EXPR:
5358 : 44897 : if (unsignedp)
5359 : 42189 : return simplify_gen_binary (LSHIFTRT, mode, op0, op1);
5360 : : else
5361 : 2708 : return simplify_gen_binary (ASHIFTRT, mode, op0, op1);
5362 : :
5363 : 2 : case LROTATE_EXPR:
5364 : 2 : return simplify_gen_binary (ROTATE, mode, op0, op1);
5365 : :
5366 : 479 : case RROTATE_EXPR:
5367 : 479 : return simplify_gen_binary (ROTATERT, mode, op0, op1);
5368 : :
5369 : 12282 : case MIN_EXPR:
5370 : 24564 : return simplify_gen_binary (unsignedp ? UMIN : SMIN, mode, op0, op1);
5371 : :
5372 : 13678 : case MAX_EXPR:
5373 : 27356 : return simplify_gen_binary (unsignedp ? UMAX : SMAX, mode, op0, op1);
5374 : :
5375 : 73541 : case BIT_AND_EXPR:
5376 : 73541 : case TRUTH_AND_EXPR:
5377 : 73541 : return simplify_gen_binary (AND, mode, op0, op1);
5378 : :
5379 : 40586 : case BIT_IOR_EXPR:
5380 : 40586 : case TRUTH_OR_EXPR:
5381 : 40586 : return simplify_gen_binary (IOR, mode, op0, op1);
5382 : :
5383 : 7473 : case BIT_XOR_EXPR:
5384 : 7473 : case TRUTH_XOR_EXPR:
5385 : 7473 : return simplify_gen_binary (XOR, mode, op0, op1);
5386 : :
5387 : 0 : case TRUTH_ANDIF_EXPR:
5388 : 0 : return gen_rtx_IF_THEN_ELSE (mode, op0, op1, const0_rtx);
5389 : :
5390 : 0 : case TRUTH_ORIF_EXPR:
5391 : 0 : return gen_rtx_IF_THEN_ELSE (mode, op0, const_true_rtx, op1);
5392 : :
5393 : 0 : case TRUTH_NOT_EXPR:
5394 : 0 : return simplify_gen_relational (EQ, mode, inner_mode, op0, const0_rtx);
5395 : :
5396 : 3677 : case LT_EXPR:
5397 : 3677 : return simplify_gen_relational (unsignedp ? LTU : LT, mode, inner_mode,
5398 : 3677 : op0, op1);
5399 : :
5400 : 3765 : case LE_EXPR:
5401 : 3765 : return simplify_gen_relational (unsignedp ? LEU : LE, mode, inner_mode,
5402 : 3765 : op0, op1);
5403 : :
5404 : 10772 : case GT_EXPR:
5405 : 10772 : return simplify_gen_relational (unsignedp ? GTU : GT, mode, inner_mode,
5406 : 10772 : op0, op1);
5407 : :
5408 : 1224 : case GE_EXPR:
5409 : 1224 : return simplify_gen_relational (unsignedp ? GEU : GE, mode, inner_mode,
5410 : 1224 : op0, op1);
5411 : :
5412 : 23397 : case EQ_EXPR:
5413 : 23397 : return simplify_gen_relational (EQ, mode, inner_mode, op0, op1);
5414 : :
5415 : 15005 : case NE_EXPR:
5416 : 15005 : return simplify_gen_relational (NE, mode, inner_mode, op0, op1);
5417 : :
5418 : 0 : case UNORDERED_EXPR:
5419 : 0 : return simplify_gen_relational (UNORDERED, mode, inner_mode, op0, op1);
5420 : :
5421 : 0 : case ORDERED_EXPR:
5422 : 0 : return simplify_gen_relational (ORDERED, mode, inner_mode, op0, op1);
5423 : :
5424 : 0 : case UNLT_EXPR:
5425 : 0 : return simplify_gen_relational (UNLT, mode, inner_mode, op0, op1);
5426 : :
5427 : 0 : case UNLE_EXPR:
5428 : 0 : return simplify_gen_relational (UNLE, mode, inner_mode, op0, op1);
5429 : :
5430 : 0 : case UNGT_EXPR:
5431 : 0 : return simplify_gen_relational (UNGT, mode, inner_mode, op0, op1);
5432 : :
5433 : 0 : case UNGE_EXPR:
5434 : 0 : return simplify_gen_relational (UNGE, mode, inner_mode, op0, op1);
5435 : :
5436 : 0 : case UNEQ_EXPR:
5437 : 0 : return simplify_gen_relational (UNEQ, mode, inner_mode, op0, op1);
5438 : :
5439 : 0 : case LTGT_EXPR:
5440 : 0 : return simplify_gen_relational (LTGT, mode, inner_mode, op0, op1);
5441 : :
5442 : 9769 : case COND_EXPR:
5443 : 9769 : return gen_rtx_IF_THEN_ELSE (mode, op0, op1, op2);
5444 : :
5445 : 9026 : case COMPLEX_EXPR:
5446 : 9026 : gcc_assert (COMPLEX_MODE_P (mode));
5447 : 9026 : if (GET_MODE (op0) == VOIDmode)
5448 : 0 : op0 = gen_rtx_CONST (GET_MODE_INNER (mode), op0);
5449 : 9026 : if (GET_MODE (op1) == VOIDmode)
5450 : 0 : op1 = gen_rtx_CONST (GET_MODE_INNER (mode), op1);
5451 : 9026 : return gen_rtx_CONCAT (mode, op0, op1);
5452 : :
5453 : 0 : case CONJ_EXPR:
5454 : 0 : if (GET_CODE (op0) == CONCAT)
5455 : 0 : return gen_rtx_CONCAT (mode, XEXP (op0, 0),
5456 : : simplify_gen_unary (NEG, GET_MODE_INNER (mode),
5457 : : XEXP (op0, 1),
5458 : : GET_MODE_INNER (mode)));
5459 : : else
5460 : : {
5461 : 0 : scalar_mode imode = GET_MODE_INNER (mode);
5462 : 0 : rtx re, im;
5463 : :
5464 : 0 : if (MEM_P (op0))
5465 : : {
5466 : 0 : re = adjust_address_nv (op0, imode, 0);
5467 : 0 : im = adjust_address_nv (op0, imode, GET_MODE_SIZE (imode));
5468 : : }
5469 : : else
5470 : : {
5471 : 0 : scalar_int_mode ifmode;
5472 : 0 : scalar_int_mode ihmode;
5473 : 0 : rtx halfsize;
5474 : 0 : if (!int_mode_for_mode (mode).exists (&ifmode)
5475 : 0 : || !int_mode_for_mode (imode).exists (&ihmode))
5476 : 0 : return NULL;
5477 : 0 : halfsize = GEN_INT (GET_MODE_BITSIZE (ihmode));
5478 : 0 : re = op0;
5479 : 0 : if (mode != ifmode)
5480 : 0 : re = gen_rtx_SUBREG (ifmode, re, 0);
5481 : 0 : re = gen_rtx_ZERO_EXTRACT (ihmode, re, halfsize, const0_rtx);
5482 : 0 : if (imode != ihmode)
5483 : 0 : re = gen_rtx_SUBREG (imode, re, 0);
5484 : 0 : im = copy_rtx (op0);
5485 : 0 : if (mode != ifmode)
5486 : 0 : im = gen_rtx_SUBREG (ifmode, im, 0);
5487 : 0 : im = gen_rtx_ZERO_EXTRACT (ihmode, im, halfsize, halfsize);
5488 : 0 : if (imode != ihmode)
5489 : 0 : im = gen_rtx_SUBREG (imode, im, 0);
5490 : : }
5491 : 0 : im = gen_rtx_NEG (imode, im);
5492 : 0 : return gen_rtx_CONCAT (mode, re, im);
5493 : : }
5494 : :
5495 : 5312880 : case ADDR_EXPR:
5496 : 5312880 : op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
5497 : 5312880 : if (!op0 || !MEM_P (op0))
5498 : : {
5499 : 1276968 : if ((TREE_CODE (TREE_OPERAND (exp, 0)) == VAR_DECL
5500 : 286155 : || TREE_CODE (TREE_OPERAND (exp, 0)) == PARM_DECL
5501 : 244996 : || TREE_CODE (TREE_OPERAND (exp, 0)) == RESULT_DECL)
5502 : 1318127 : && (!TREE_ADDRESSABLE (TREE_OPERAND (exp, 0))
5503 : 78190 : || target_for_debug_bind (TREE_OPERAND (exp, 0))))
5504 : 959393 : return gen_rtx_DEBUG_IMPLICIT_PTR (mode, TREE_OPERAND (exp, 0));
5505 : :
5506 : 317575 : if (handled_component_p (TREE_OPERAND (exp, 0)))
5507 : : {
5508 : 229429 : poly_int64 bitoffset, bitsize, maxsize, byteoffset;
5509 : 229429 : bool reverse;
5510 : 229429 : tree decl
5511 : 229429 : = get_ref_base_and_extent (TREE_OPERAND (exp, 0), &bitoffset,
5512 : : &bitsize, &maxsize, &reverse);
5513 : 229429 : if ((VAR_P (decl)
5514 : : || TREE_CODE (decl) == PARM_DECL
5515 : : || TREE_CODE (decl) == RESULT_DECL)
5516 : 204797 : && (!TREE_ADDRESSABLE (decl)
5517 : 18488 : || target_for_debug_bind (decl))
5518 : 44688 : && multiple_p (bitoffset, BITS_PER_UNIT, &byteoffset)
5519 : 186309 : && known_gt (bitsize, 0)
5520 : 184743 : && known_eq (bitsize, maxsize))
5521 : : {
5522 : 184741 : rtx base = gen_rtx_DEBUG_IMPLICIT_PTR (mode, decl);
5523 : 184741 : return plus_constant (mode, base, byteoffset);
5524 : : }
5525 : : }
5526 : :
5527 : 132834 : if (TREE_CODE (TREE_OPERAND (exp, 0)) == MEM_REF
5528 : 132834 : && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5529 : : == ADDR_EXPR)
5530 : : {
5531 : 7914 : op0 = expand_debug_expr (TREE_OPERAND (TREE_OPERAND (exp, 0),
5532 : : 0));
5533 : 7914 : if (op0 != NULL
5534 : 4773 : && (GET_CODE (op0) == DEBUG_IMPLICIT_PTR
5535 : 0 : || (GET_CODE (op0) == PLUS
5536 : 0 : && GET_CODE (XEXP (op0, 0)) == DEBUG_IMPLICIT_PTR
5537 : 0 : && CONST_INT_P (XEXP (op0, 1)))))
5538 : : {
5539 : 4773 : op1 = expand_debug_expr (TREE_OPERAND (TREE_OPERAND (exp, 0),
5540 : : 1));
5541 : 4773 : poly_int64 offset;
5542 : 9546 : if (!op1 || !poly_int_rtx_p (op1, &offset))
5543 : : return NULL;
5544 : :
5545 : 4773 : return plus_constant (mode, op0, offset);
5546 : : }
5547 : : }
5548 : :
5549 : 128061 : return NULL;
5550 : : }
5551 : :
5552 : 4035912 : as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
5553 : 4035912 : addr_mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (exp));
5554 : 4035912 : op0 = convert_debug_memory_address (addr_mode, XEXP (op0, 0), as);
5555 : :
5556 : 4035912 : return op0;
5557 : :
5558 : 961 : case VECTOR_CST:
5559 : 961 : {
5560 : 961 : unsigned HOST_WIDE_INT i, nelts;
5561 : :
5562 : 961 : if (!VECTOR_CST_NELTS (exp).is_constant (&nelts))
5563 : : return NULL;
5564 : :
5565 : 961 : op0 = gen_rtx_CONCATN (mode, rtvec_alloc (nelts));
5566 : :
5567 : 5056 : for (i = 0; i < nelts; ++i)
5568 : : {
5569 : 4095 : op1 = expand_debug_expr (VECTOR_CST_ELT (exp, i));
5570 : 4095 : if (!op1)
5571 : : return NULL;
5572 : 4095 : XVECEXP (op0, 0, i) = op1;
5573 : : }
5574 : :
5575 : : return op0;
5576 : : }
5577 : :
5578 : 276 : case CONSTRUCTOR:
5579 : 276 : if (TREE_CLOBBER_P (exp))
5580 : : return NULL;
5581 : 276 : else if (TREE_CODE (TREE_TYPE (exp)) == VECTOR_TYPE)
5582 : : {
5583 : 276 : unsigned i;
5584 : 276 : unsigned HOST_WIDE_INT nelts;
5585 : 276 : tree val;
5586 : :
5587 : 276 : if (!TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)).is_constant (&nelts))
5588 : : goto flag_unsupported;
5589 : :
5590 : 276 : op0 = gen_rtx_CONCATN (mode, rtvec_alloc (nelts));
5591 : :
5592 : 1428 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), i, val)
5593 : : {
5594 : 1162 : op1 = expand_debug_expr (val);
5595 : 1162 : if (!op1)
5596 : 32722478 : return NULL;
5597 : 1152 : XVECEXP (op0, 0, i) = op1;
5598 : : }
5599 : :
5600 : 266 : if (i < nelts)
5601 : : {
5602 : 23 : op1 = expand_debug_expr
5603 : 23 : (build_zero_cst (TREE_TYPE (TREE_TYPE (exp))));
5604 : :
5605 : 23 : if (!op1)
5606 : : return NULL;
5607 : :
5608 : 166 : for (; i < nelts; i++)
5609 : 143 : XVECEXP (op0, 0, i) = op1;
5610 : : }
5611 : :
5612 : 266 : return op0;
5613 : : }
5614 : : else
5615 : 0 : goto flag_unsupported;
5616 : :
5617 : : case CALL_EXPR:
5618 : : /* ??? Maybe handle some builtins? */
5619 : : return NULL;
5620 : :
5621 : 7852115 : case SSA_NAME:
5622 : 7852115 : {
5623 : 7852115 : gimple *g = get_gimple_for_ssa_name (exp);
5624 : 7852115 : if (g)
5625 : : {
5626 : 1152171 : tree t = NULL_TREE;
5627 : 1152171 : if (deep_ter_debug_map)
5628 : : {
5629 : 31852 : tree *slot = deep_ter_debug_map->get (exp);
5630 : 31852 : if (slot)
5631 : 5959 : t = *slot;
5632 : : }
5633 : 5959 : if (t == NULL_TREE)
5634 : : {
5635 : 1148542 : if (is_gimple_assign (g))
5636 : 1148345 : t = gimple_assign_rhs_to_tree (g);
5637 : : else
5638 : : /* expand_debug_expr doesn't handle CALL_EXPR right now. */
5639 : : return NULL;
5640 : : }
5641 : 1151974 : op0 = expand_debug_expr (t);
5642 : 1151974 : if (!op0)
5643 : : return NULL;
5644 : : }
5645 : : else
5646 : : {
5647 : : /* If this is a reference to an incoming value of
5648 : : parameter that is never used in the code or where the
5649 : : incoming value is never used in the code, use
5650 : : PARM_DECL's DECL_RTL if set. */
5651 : 6699944 : if (SSA_NAME_IS_DEFAULT_DEF (exp)
5652 : 1109377 : && SSA_NAME_VAR (exp)
5653 : 1109377 : && TREE_CODE (SSA_NAME_VAR (exp)) == PARM_DECL
5654 : 7758420 : && has_zero_uses (exp))
5655 : : {
5656 : 17155 : op0 = expand_debug_parm_decl (SSA_NAME_VAR (exp));
5657 : 17155 : if (op0)
5658 : 17065 : goto adjust_mode;
5659 : 90 : op0 = expand_debug_expr (SSA_NAME_VAR (exp));
5660 : 90 : if (op0)
5661 : 90 : goto adjust_mode;
5662 : : }
5663 : :
5664 : 6682789 : int part = var_to_partition (SA.map, exp);
5665 : :
5666 : 6682789 : if (part == NO_PARTITION)
5667 : : return NULL;
5668 : :
5669 : 6677849 : gcc_assert (part >= 0 && (unsigned)part < SA.map->num_partitions);
5670 : :
5671 : 6677849 : op0 = copy_rtx (SA.partition_to_pseudo[part]);
5672 : : }
5673 : 7826283 : goto adjust_mode;
5674 : : }
5675 : :
5676 : : case ERROR_MARK:
5677 : : return NULL;
5678 : :
5679 : : /* Vector stuff. For most of the codes we don't have rtl codes. */
5680 : : case REALIGN_LOAD_EXPR:
5681 : : case VEC_COND_EXPR:
5682 : : case VEC_PACK_FIX_TRUNC_EXPR:
5683 : : case VEC_PACK_FLOAT_EXPR:
5684 : : case VEC_PACK_SAT_EXPR:
5685 : : case VEC_PACK_TRUNC_EXPR:
5686 : : case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
5687 : : case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
5688 : : case VEC_UNPACK_FLOAT_HI_EXPR:
5689 : : case VEC_UNPACK_FLOAT_LO_EXPR:
5690 : : case VEC_UNPACK_HI_EXPR:
5691 : : case VEC_UNPACK_LO_EXPR:
5692 : : case VEC_WIDEN_MULT_HI_EXPR:
5693 : : case VEC_WIDEN_MULT_LO_EXPR:
5694 : : case VEC_WIDEN_MULT_EVEN_EXPR:
5695 : : case VEC_WIDEN_MULT_ODD_EXPR:
5696 : : case VEC_WIDEN_LSHIFT_HI_EXPR:
5697 : : case VEC_WIDEN_LSHIFT_LO_EXPR:
5698 : : case VEC_PERM_EXPR:
5699 : : case VEC_DUPLICATE_EXPR:
5700 : : case VEC_SERIES_EXPR:
5701 : : case SAD_EXPR:
5702 : : return NULL;
5703 : :
5704 : : /* Misc codes. */
5705 : : case ADDR_SPACE_CONVERT_EXPR:
5706 : : case FIXED_CONVERT_EXPR:
5707 : : case OBJ_TYPE_REF:
5708 : : case WITH_SIZE_EXPR:
5709 : : case BIT_INSERT_EXPR:
5710 : : return NULL;
5711 : :
5712 : 0 : case DOT_PROD_EXPR:
5713 : 0 : if (SCALAR_INT_MODE_P (GET_MODE (op0))
5714 : 0 : && SCALAR_INT_MODE_P (mode))
5715 : : {
5716 : 0 : op0
5717 : 0 : = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
5718 : : 0)))
5719 : : ? ZERO_EXTEND : SIGN_EXTEND, mode, op0,
5720 : : inner_mode);
5721 : 0 : op1
5722 : 0 : = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
5723 : : 1)))
5724 : : ? ZERO_EXTEND : SIGN_EXTEND, mode, op1,
5725 : : inner_mode);
5726 : 0 : op0 = simplify_gen_binary (MULT, mode, op0, op1);
5727 : 0 : return simplify_gen_binary (PLUS, mode, op0, op2);
5728 : : }
5729 : : return NULL;
5730 : :
5731 : 4083 : case WIDEN_MULT_EXPR:
5732 : 4083 : case WIDEN_MULT_PLUS_EXPR:
5733 : 4083 : case WIDEN_MULT_MINUS_EXPR:
5734 : 4083 : if (SCALAR_INT_MODE_P (GET_MODE (op0))
5735 : 4083 : && SCALAR_INT_MODE_P (mode))
5736 : : {
5737 : 4083 : inner_mode = GET_MODE (op0);
5738 : 4083 : if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
5739 : 3464 : op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
5740 : : else
5741 : 619 : op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
5742 : 4083 : if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 1))))
5743 : 3464 : op1 = simplify_gen_unary (ZERO_EXTEND, mode, op1, inner_mode);
5744 : : else
5745 : 619 : op1 = simplify_gen_unary (SIGN_EXTEND, mode, op1, inner_mode);
5746 : 4083 : op0 = simplify_gen_binary (MULT, mode, op0, op1);
5747 : 4083 : if (TREE_CODE (exp) == WIDEN_MULT_EXPR)
5748 : : return op0;
5749 : 0 : else if (TREE_CODE (exp) == WIDEN_MULT_PLUS_EXPR)
5750 : 0 : return simplify_gen_binary (PLUS, mode, op0, op2);
5751 : : else
5752 : 0 : return simplify_gen_binary (MINUS, mode, op2, op0);
5753 : : }
5754 : : return NULL;
5755 : :
5756 : : case MULT_HIGHPART_EXPR:
5757 : : /* ??? Similar to the above. */
5758 : : return NULL;
5759 : :
5760 : 0 : case WIDEN_SUM_EXPR:
5761 : 0 : case WIDEN_LSHIFT_EXPR:
5762 : 0 : if (SCALAR_INT_MODE_P (GET_MODE (op0))
5763 : 0 : && SCALAR_INT_MODE_P (mode))
5764 : : {
5765 : 0 : op0
5766 : 0 : = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
5767 : : 0)))
5768 : : ? ZERO_EXTEND : SIGN_EXTEND, mode, op0,
5769 : : inner_mode);
5770 : 0 : return simplify_gen_binary (TREE_CODE (exp) == WIDEN_LSHIFT_EXPR
5771 : 0 : ? ASHIFT : PLUS, mode, op0, op1);
5772 : : }
5773 : : return NULL;
5774 : :
5775 : 0 : default:
5776 : 0 : flag_unsupported:
5777 : 0 : if (flag_checking)
5778 : : {
5779 : 0 : debug_tree (exp);
5780 : 0 : gcc_unreachable ();
5781 : : }
5782 : : return NULL;
5783 : : }
5784 : : }
5785 : :
5786 : : /* Return an RTX equivalent to the source bind value of the tree expression
5787 : : EXP. */
5788 : :
5789 : : static rtx
5790 : 119706 : expand_debug_source_expr (tree exp)
5791 : : {
5792 : 226968 : rtx op0 = NULL_RTX;
5793 : 226968 : machine_mode mode = VOIDmode, inner_mode;
5794 : :
5795 : 226968 : switch (TREE_CODE (exp))
5796 : : {
5797 : 107262 : case VAR_DECL:
5798 : 107262 : if (DECL_ABSTRACT_ORIGIN (exp))
5799 : 107262 : return expand_debug_source_expr (DECL_ABSTRACT_ORIGIN (exp));
5800 : : break;
5801 : 118971 : case PARM_DECL:
5802 : 118971 : {
5803 : 118971 : mode = DECL_MODE (exp);
5804 : 118971 : op0 = expand_debug_parm_decl (exp);
5805 : 118971 : if (op0)
5806 : : break;
5807 : : /* See if this isn't an argument that has been completely
5808 : : optimized out. */
5809 : 118515 : if (!DECL_RTL_SET_P (exp)
5810 : 118501 : && !DECL_INCOMING_RTL (exp)
5811 : 237016 : && DECL_ABSTRACT_ORIGIN (current_function_decl))
5812 : : {
5813 : 34123 : tree aexp = DECL_ORIGIN (exp);
5814 : 34123 : if (DECL_CONTEXT (aexp)
5815 : 34123 : == DECL_ABSTRACT_ORIGIN (current_function_decl))
5816 : : {
5817 : 14404 : vec<tree, va_gc> **debug_args;
5818 : 14404 : unsigned int ix;
5819 : 14404 : tree ddecl;
5820 : 14404 : debug_args = decl_debug_args_lookup (current_function_decl);
5821 : 14404 : if (debug_args != NULL)
5822 : : {
5823 : 18111 : for (ix = 0; vec_safe_iterate (*debug_args, ix, &ddecl);
5824 : 3711 : ix += 2)
5825 : 18095 : if (ddecl == aexp)
5826 : 14384 : return gen_rtx_DEBUG_PARAMETER_REF (mode, aexp);
5827 : : }
5828 : : }
5829 : : }
5830 : : break;
5831 : : }
5832 : : default:
5833 : : break;
5834 : : }
5835 : :
5836 : 456 : if (op0 == NULL_RTX)
5837 : 104866 : return NULL_RTX;
5838 : :
5839 : 456 : inner_mode = GET_MODE (op0);
5840 : 456 : if (mode == inner_mode)
5841 : : return op0;
5842 : :
5843 : 21 : if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
5844 : : {
5845 : 7 : if (GET_MODE_UNIT_BITSIZE (mode)
5846 : 14 : == GET_MODE_UNIT_BITSIZE (inner_mode))
5847 : 0 : op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
5848 : 7 : else if (GET_MODE_UNIT_BITSIZE (mode)
5849 : 14 : < GET_MODE_UNIT_BITSIZE (inner_mode))
5850 : 7 : op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
5851 : : else
5852 : 0 : op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
5853 : : }
5854 : 14 : else if (FLOAT_MODE_P (mode))
5855 : 0 : gcc_unreachable ();
5856 : 14 : else if (FLOAT_MODE_P (inner_mode))
5857 : : {
5858 : 0 : if (TYPE_UNSIGNED (TREE_TYPE (exp)))
5859 : 0 : op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
5860 : : else
5861 : 0 : op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
5862 : : }
5863 : 28 : else if (GET_MODE_UNIT_PRECISION (mode)
5864 : 14 : == GET_MODE_UNIT_PRECISION (inner_mode))
5865 : 0 : op0 = lowpart_subreg (mode, op0, inner_mode);
5866 : 28 : else if (GET_MODE_UNIT_PRECISION (mode)
5867 : 14 : < GET_MODE_UNIT_PRECISION (inner_mode))
5868 : 14 : op0 = simplify_gen_unary (TRUNCATE, mode, op0, inner_mode);
5869 : 0 : else if (TYPE_UNSIGNED (TREE_TYPE (exp)))
5870 : 0 : op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
5871 : : else
5872 : 0 : op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
5873 : :
5874 : : return op0;
5875 : : }
5876 : :
5877 : : /* Ensure INSN_VAR_LOCATION_LOC (insn) doesn't have unbound complexity.
5878 : : Allow 4 levels of rtl nesting for most rtl codes, and if we see anything
5879 : : deeper than that, create DEBUG_EXPRs and emit DEBUG_INSNs before INSN. */
5880 : :
5881 : : static void
5882 : 40100316 : avoid_complex_debug_insns (rtx_insn *insn, rtx *exp_p, int depth)
5883 : : {
5884 : 40100316 : rtx exp = *exp_p;
5885 : :
5886 : 40100316 : if (exp == NULL_RTX)
5887 : : return;
5888 : :
5889 : 40100316 : if ((OBJECT_P (exp) && !MEM_P (exp)) || GET_CODE (exp) == CLOBBER)
5890 : : return;
5891 : :
5892 : 5436719 : if (depth == 4)
5893 : : {
5894 : : /* Create DEBUG_EXPR (and DEBUG_EXPR_DECL). */
5895 : 18555 : rtx dval = make_debug_expr_from_rtl (exp);
5896 : :
5897 : : /* Emit a debug bind insn before INSN. */
5898 : 18555 : rtx bind = gen_rtx_VAR_LOCATION (GET_MODE (exp),
5899 : : DEBUG_EXPR_TREE_DECL (dval), exp,
5900 : : VAR_INIT_STATUS_INITIALIZED);
5901 : :
5902 : 18555 : emit_debug_insn_before (bind, insn);
5903 : 18555 : *exp_p = dval;
5904 : 18555 : return;
5905 : : }
5906 : :
5907 : 5418164 : const char *format_ptr = GET_RTX_FORMAT (GET_CODE (exp));
5908 : 5418164 : int i, j;
5909 : 16155159 : for (i = 0; i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
5910 : 10736995 : switch (*format_ptr++)
5911 : : {
5912 : 9834677 : case 'e':
5913 : 9834677 : avoid_complex_debug_insns (insn, &XEXP (exp, i), depth + 1);
5914 : 9834677 : break;
5915 : :
5916 : : case 'E':
5917 : : case 'V':
5918 : 0 : for (j = 0; j < XVECLEN (exp, i); j++)
5919 : 0 : avoid_complex_debug_insns (insn, &XVECEXP (exp, i, j), depth + 1);
5920 : : break;
5921 : :
5922 : : default:
5923 : : break;
5924 : : }
5925 : : }
5926 : :
5927 : : /* Expand the _LOCs in debug insns. We run this after expanding all
5928 : : regular insns, so that any variables referenced in the function
5929 : : will have their DECL_RTLs set. */
5930 : :
5931 : : static void
5932 : 477609 : expand_debug_locations (void)
5933 : : {
5934 : 477609 : rtx_insn *insn;
5935 : 477609 : rtx_insn *last = get_last_insn ();
5936 : 477609 : int save_strict_alias = flag_strict_aliasing;
5937 : :
5938 : : /* New alias sets while setting up memory attributes cause
5939 : : -fcompare-debug failures, even though it doesn't bring about any
5940 : : codegen changes. */
5941 : 477609 : flag_strict_aliasing = 0;
5942 : :
5943 : 91774181 : for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5944 : 91296572 : if (DEBUG_BIND_INSN_P (insn))
5945 : : {
5946 : 30247084 : tree value = (tree)INSN_VAR_LOCATION_LOC (insn);
5947 : 30247084 : rtx val;
5948 : 30247084 : rtx_insn *prev_insn, *insn2;
5949 : 30247084 : machine_mode mode;
5950 : :
5951 : 30247084 : if (value == NULL_TREE)
5952 : : val = NULL_RTX;
5953 : : else
5954 : : {
5955 : 17235473 : if (INSN_VAR_LOCATION_STATUS (insn)
5956 : : == VAR_INIT_STATUS_UNINITIALIZED)
5957 : 119706 : val = expand_debug_source_expr (value);
5958 : : /* The avoid_deep_ter_for_debug function inserts
5959 : : debug bind stmts after SSA_NAME definition, with the
5960 : : SSA_NAME as the whole bind location. Disable temporarily
5961 : : expansion of that SSA_NAME into the DEBUG_EXPR_DECL
5962 : : being defined in this DEBUG_INSN. */
5963 : 17115767 : else if (deep_ter_debug_map && TREE_CODE (value) == SSA_NAME)
5964 : : {
5965 : 33029 : tree *slot = deep_ter_debug_map->get (value);
5966 : 33029 : if (slot)
5967 : : {
5968 : 2626 : if (*slot == INSN_VAR_LOCATION_DECL (insn))
5969 : 2330 : *slot = NULL_TREE;
5970 : : else
5971 : : slot = NULL;
5972 : : }
5973 : 33029 : val = expand_debug_expr (value);
5974 : 33029 : if (slot)
5975 : 2330 : *slot = INSN_VAR_LOCATION_DECL (insn);
5976 : : }
5977 : : else
5978 : 17082738 : val = expand_debug_expr (value);
5979 : 17235473 : gcc_assert (last == get_last_insn ());
5980 : : }
5981 : :
5982 : 17235473 : if (!val)
5983 : 13279561 : val = gen_rtx_UNKNOWN_VAR_LOC ();
5984 : : else
5985 : : {
5986 : 16967523 : mode = GET_MODE (INSN_VAR_LOCATION (insn));
5987 : :
5988 : 16967523 : gcc_assert (mode == GET_MODE (val)
5989 : : || (GET_MODE (val) == VOIDmode
5990 : : && (CONST_SCALAR_INT_P (val)
5991 : : || GET_CODE (val) == CONST_FIXED
5992 : : || GET_CODE (val) == LABEL_REF)));
5993 : : }
5994 : :
5995 : 30247084 : INSN_VAR_LOCATION_LOC (insn) = val;
5996 : 30247084 : prev_insn = PREV_INSN (insn);
5997 : 60512723 : for (insn2 = insn; insn2 != prev_insn; insn2 = PREV_INSN (insn2))
5998 : 30265639 : avoid_complex_debug_insns (insn2, &INSN_VAR_LOCATION_LOC (insn2), 0);
5999 : : }
6000 : :
6001 : 477609 : flag_strict_aliasing = save_strict_alias;
6002 : 477609 : }
6003 : :
6004 : : /* Performs swapping operands of commutative operations to expand
6005 : : the expensive one first. */
6006 : :
6007 : : static void
6008 : 8691115 : reorder_operands (basic_block bb)
6009 : : {
6010 : 8691115 : unsigned int *lattice; /* Hold cost of each statement. */
6011 : 8691115 : unsigned int i = 0, n = 0;
6012 : 8691115 : gimple_stmt_iterator gsi;
6013 : 8691115 : gimple_seq stmts;
6014 : 8691115 : gimple *stmt;
6015 : 8691115 : bool swap;
6016 : 8691115 : tree op0, op1;
6017 : 8691115 : ssa_op_iter iter;
6018 : 8691115 : use_operand_p use_p;
6019 : 8691115 : gimple *def0, *def1;
6020 : :
6021 : : /* Compute cost of each statement using estimate_num_insns. */
6022 : 8691115 : stmts = bb_seq (bb);
6023 : 80901135 : for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
6024 : : {
6025 : 72210020 : stmt = gsi_stmt (gsi);
6026 : 72210020 : if (!is_gimple_debug (stmt))
6027 : 31939177 : gimple_set_uid (stmt, n++);
6028 : : }
6029 : 8691115 : lattice = XNEWVEC (unsigned int, n);
6030 : 80901135 : for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
6031 : : {
6032 : 72210020 : unsigned cost;
6033 : 72210020 : stmt = gsi_stmt (gsi);
6034 : 72210020 : if (is_gimple_debug (stmt))
6035 : 40270843 : continue;
6036 : 31939177 : cost = estimate_num_insns (stmt, &eni_size_weights);
6037 : 31939177 : lattice[i] = cost;
6038 : 60411950 : FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
6039 : : {
6040 : 28472773 : tree use = USE_FROM_PTR (use_p);
6041 : 28472773 : gimple *def_stmt;
6042 : 28472773 : if (TREE_CODE (use) != SSA_NAME)
6043 : 0 : continue;
6044 : 28472773 : def_stmt = get_gimple_for_ssa_name (use);
6045 : 28472773 : if (!def_stmt)
6046 : 20130478 : continue;
6047 : 8342295 : lattice[i] += lattice[gimple_uid (def_stmt)];
6048 : : }
6049 : 31939177 : i++;
6050 : 31939177 : if (!is_gimple_assign (stmt)
6051 : 31939177 : || !commutative_tree_code (gimple_assign_rhs_code (stmt)))
6052 : 28085211 : continue;
6053 : 3853966 : op0 = gimple_op (stmt, 1);
6054 : 3853966 : op1 = gimple_op (stmt, 2);
6055 : 3853966 : if (TREE_CODE (op0) != SSA_NAME
6056 : 3851900 : || TREE_CODE (op1) != SSA_NAME)
6057 : 2434500 : continue;
6058 : : /* Swap operands if the second one is more expensive. */
6059 : 1419466 : def0 = get_gimple_for_ssa_name (op0);
6060 : 1419466 : def1 = get_gimple_for_ssa_name (op1);
6061 : 1419466 : if (!def1)
6062 : 793903 : continue;
6063 : 625563 : swap = false;
6064 : 625563 : if (!def0 || lattice[gimple_uid (def1)] > lattice[gimple_uid (def0)])
6065 : 290362 : swap = true;
6066 : 290362 : if (swap)
6067 : : {
6068 : 290362 : if (dump_file && (dump_flags & TDF_DETAILS))
6069 : : {
6070 : 2 : fprintf (dump_file, "Swap operands in stmt:\n");
6071 : 2 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
6072 : 3 : fprintf (dump_file, "Cost left opnd=%d, right opnd=%d\n",
6073 : 1 : def0 ? lattice[gimple_uid (def0)] : 0,
6074 : 2 : lattice[gimple_uid (def1)]);
6075 : : }
6076 : 290362 : swap_ssa_operands (stmt, gimple_assign_rhs1_ptr (stmt),
6077 : : gimple_assign_rhs2_ptr (stmt));
6078 : : }
6079 : : }
6080 : 8691115 : XDELETE (lattice);
6081 : 8691115 : }
6082 : :
6083 : : /* Expand basic block BB from GIMPLE trees to RTL. */
6084 : :
6085 : : static basic_block
6086 : 11971092 : expand_gimple_basic_block (basic_block bb, bool disable_tail_calls)
6087 : : {
6088 : 11971092 : gimple_stmt_iterator gsi;
6089 : 11971092 : gimple_seq stmts;
6090 : 11971092 : gimple *stmt = NULL;
6091 : 11971092 : rtx_note *note = NULL;
6092 : 11971092 : rtx_insn *last;
6093 : 11971092 : edge e;
6094 : 11971092 : edge_iterator ei;
6095 : 11971092 : bool nondebug_stmt_seen = false;
6096 : :
6097 : 11971092 : if (dump_file)
6098 : 718 : fprintf (dump_file, "\n;; Generating RTL for gimple basic block %d\n",
6099 : : bb->index);
6100 : :
6101 : : /* Note that since we are now transitioning from GIMPLE to RTL, we
6102 : : cannot use the gsi_*_bb() routines because they expect the basic
6103 : : block to be in GIMPLE, instead of RTL. Therefore, we need to
6104 : : access the BB sequence directly. */
6105 : 11971092 : if (optimize)
6106 : 8691115 : reorder_operands (bb);
6107 : 11971092 : stmts = bb_seq (bb);
6108 : 11971092 : bb->il.gimple.seq = NULL;
6109 : 11971092 : bb->il.gimple.phi_nodes = NULL;
6110 : 11971092 : rtl_profile_for_bb (bb);
6111 : 11971092 : init_rtl_bb_info (bb);
6112 : 11971092 : bb->flags |= BB_RTL;
6113 : :
6114 : : /* Remove the RETURN_EXPR if we may fall though to the exit
6115 : : instead. */
6116 : 11971092 : gsi = gsi_last (stmts);
6117 : 11971092 : if (!gsi_end_p (gsi)
6118 : 11971092 : && gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
6119 : : {
6120 : 1403328 : greturn *ret_stmt = as_a <greturn *> (gsi_stmt (gsi));
6121 : :
6122 : 1403328 : gcc_assert (single_succ_p (bb));
6123 : 1403328 : gcc_assert (single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (cfun));
6124 : :
6125 : 1403328 : if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
6126 : 1403328 : && !gimple_return_retval (ret_stmt))
6127 : : {
6128 : 584890 : gsi_remove (&gsi, false);
6129 : 584890 : single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
6130 : : }
6131 : : }
6132 : :
6133 : 11971092 : gsi = gsi_start (stmts);
6134 : 11971092 : if (!gsi_end_p (gsi))
6135 : : {
6136 : 11452310 : stmt = gsi_stmt (gsi);
6137 : 11452310 : if (gimple_code (stmt) != GIMPLE_LABEL)
6138 : 11379218 : stmt = NULL;
6139 : : }
6140 : :
6141 : 11971092 : rtx_code_label **elt = lab_rtx_for_bb->get (bb);
6142 : :
6143 : 11971092 : if (stmt || elt)
6144 : : {
6145 : 4944793 : gcc_checking_assert (!note);
6146 : 4944793 : last = get_last_insn ();
6147 : :
6148 : 4944793 : if (stmt)
6149 : : {
6150 : 591874 : expand_gimple_stmt (stmt);
6151 : 591874 : gsi_next (&gsi);
6152 : : }
6153 : :
6154 : 4944793 : if (elt)
6155 : 4352919 : emit_label (*elt);
6156 : :
6157 : 4944793 : BB_HEAD (bb) = NEXT_INSN (last);
6158 : 4944793 : if (NOTE_P (BB_HEAD (bb)))
6159 : 0 : BB_HEAD (bb) = NEXT_INSN (BB_HEAD (bb));
6160 : 4944793 : gcc_assert (LABEL_P (BB_HEAD (bb)));
6161 : 4944793 : note = emit_note_after (NOTE_INSN_BASIC_BLOCK, BB_HEAD (bb));
6162 : :
6163 : 4944793 : maybe_dump_rtl_for_gimple_stmt (stmt, last);
6164 : : }
6165 : : else
6166 : 7026299 : BB_HEAD (bb) = note = emit_note (NOTE_INSN_BASIC_BLOCK);
6167 : :
6168 : 11971092 : if (note)
6169 : 11971092 : NOTE_BASIC_BLOCK (note) = bb;
6170 : :
6171 : 62131405 : for (; !gsi_end_p (gsi); gsi_next (&gsi))
6172 : : {
6173 : 50728073 : basic_block new_bb;
6174 : :
6175 : 50728073 : stmt = gsi_stmt (gsi);
6176 : 50728073 : if (!is_gimple_debug (stmt))
6177 : 43230590 : nondebug_stmt_seen = true;
6178 : :
6179 : : /* If this statement is a non-debug one, and we generate debug
6180 : : insns, then this one might be the last real use of a TERed
6181 : : SSA_NAME, but where there are still some debug uses further
6182 : : down. Expanding the current SSA name in such further debug
6183 : : uses by their RHS might lead to wrong debug info, as coalescing
6184 : : might make the operands of such RHS be placed into the same
6185 : : pseudo as something else. Like so:
6186 : : a_1 = a_0 + 1; // Assume a_1 is TERed and a_0 is dead
6187 : : use(a_1);
6188 : : a_2 = ...
6189 : : #DEBUG ... => a_1
6190 : : As a_0 and a_2 don't overlap in lifetime, assume they are coalesced.
6191 : : If we now would expand a_1 by it's RHS (a_0 + 1) in the debug use,
6192 : : the write to a_2 would actually have clobbered the place which
6193 : : formerly held a_0.
6194 : :
6195 : : So, instead of that, we recognize the situation, and generate
6196 : : debug temporaries at the last real use of TERed SSA names:
6197 : : a_1 = a_0 + 1;
6198 : : #DEBUG #D1 => a_1
6199 : : use(a_1);
6200 : : a_2 = ...
6201 : : #DEBUG ... => #D1
6202 : : */
6203 : 50728073 : if (MAY_HAVE_DEBUG_BIND_INSNS
6204 : 27040332 : && SA.values
6205 : 76645912 : && !is_gimple_debug (stmt))
6206 : : {
6207 : 18789665 : ssa_op_iter iter;
6208 : 18789665 : tree op;
6209 : 18789665 : gimple *def;
6210 : :
6211 : 18789665 : location_t sloc = curr_insn_location ();
6212 : :
6213 : : /* Look for SSA names that have their last use here (TERed
6214 : : names always have only one real use). */
6215 : 35784640 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
6216 : 16994975 : if ((def = get_gimple_for_ssa_name (op))
6217 : 16994975 : && is_gimple_assign (def))
6218 : : {
6219 : 4703711 : imm_use_iterator imm_iter;
6220 : 4703711 : use_operand_p use_p;
6221 : 4703711 : bool have_debug_uses = false;
6222 : :
6223 : 9388042 : FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
6224 : : {
6225 : 4718869 : if (gimple_debug_bind_p (USE_STMT (use_p)))
6226 : : {
6227 : : have_debug_uses = true;
6228 : : break;
6229 : : }
6230 : : }
6231 : :
6232 : 4703711 : if (have_debug_uses)
6233 : : {
6234 : : /* OP is a TERed SSA name, with DEF its defining
6235 : : statement, and where OP is used in further debug
6236 : : instructions. Generate a debug temporary, and
6237 : : replace all uses of OP in debug insns with that
6238 : : temporary. */
6239 : 34538 : gimple *debugstmt;
6240 : 34538 : tree value = gimple_assign_rhs_to_tree (def);
6241 : 34538 : tree vexpr = build_debug_expr_decl (TREE_TYPE (value));
6242 : 34538 : rtx val;
6243 : 34538 : machine_mode mode;
6244 : :
6245 : 34538 : set_curr_insn_location (gimple_location (def));
6246 : :
6247 : 34538 : if (DECL_P (value))
6248 : 364 : mode = DECL_MODE (value);
6249 : : else
6250 : 34174 : mode = TYPE_MODE (TREE_TYPE (value));
6251 : : /* FIXME: Is setting the mode really necessary? */
6252 : 34538 : SET_DECL_MODE (vexpr, mode);
6253 : :
6254 : 34538 : val = gen_rtx_VAR_LOCATION
6255 : 34538 : (mode, vexpr, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
6256 : :
6257 : 34538 : emit_debug_insn (val);
6258 : :
6259 : 120279 : FOR_EACH_IMM_USE_STMT (debugstmt, imm_iter, op)
6260 : : {
6261 : 85741 : if (!gimple_debug_bind_p (debugstmt))
6262 : 34538 : continue;
6263 : :
6264 : 156391 : FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
6265 : 52594 : SET_USE (use_p, vexpr);
6266 : :
6267 : 51203 : update_stmt (debugstmt);
6268 : 34538 : }
6269 : : }
6270 : : }
6271 : 18789665 : set_curr_insn_location (sloc);
6272 : : }
6273 : :
6274 : 50728073 : currently_expanding_gimple_stmt = stmt;
6275 : :
6276 : : /* Expand this statement, then evaluate the resulting RTL and
6277 : : fixup the CFG accordingly. */
6278 : 50728073 : if (gimple_code (stmt) == GIMPLE_COND)
6279 : : {
6280 : 5309089 : new_bb = expand_gimple_cond (bb, as_a <gcond *> (stmt));
6281 : 5309089 : if (new_bb)
6282 : : {
6283 : 450196 : currently_expanding_gimple_stmt = NULL;
6284 : 450196 : return new_bb;
6285 : : }
6286 : : }
6287 : 45418984 : else if (is_gimple_debug (stmt))
6288 : : {
6289 : 7497483 : location_t sloc = curr_insn_location ();
6290 : 7497483 : gimple_stmt_iterator nsi = gsi;
6291 : :
6292 : 40166248 : for (;;)
6293 : : {
6294 : 40166248 : tree var;
6295 : 40166248 : tree value = NULL_TREE;
6296 : 40166248 : rtx val = NULL_RTX;
6297 : 40166248 : machine_mode mode;
6298 : :
6299 : 40166248 : if (!gimple_debug_nonbind_marker_p (stmt))
6300 : : {
6301 : 30321389 : if (gimple_debug_bind_p (stmt))
6302 : : {
6303 : 30201683 : var = gimple_debug_bind_get_var (stmt);
6304 : :
6305 : 30201683 : if (TREE_CODE (var) != DEBUG_EXPR_DECL
6306 : 28040856 : && TREE_CODE (var) != LABEL_DECL
6307 : 58233830 : && !target_for_debug_bind (var))
6308 : 108843 : goto delink_debug_stmt;
6309 : :
6310 : 30092840 : if (DECL_P (var) && !VECTOR_TYPE_P (TREE_TYPE (var)))
6311 : 30083914 : mode = DECL_MODE (var);
6312 : : else
6313 : 8926 : mode = TYPE_MODE (TREE_TYPE (var));
6314 : :
6315 : 30092840 : if (gimple_debug_bind_has_value_p (stmt))
6316 : 17081229 : value = gimple_debug_bind_get_value (stmt);
6317 : :
6318 : 30092840 : val = gen_rtx_VAR_LOCATION
6319 : 30092840 : (mode, var, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
6320 : : }
6321 : 119706 : else if (gimple_debug_source_bind_p (stmt))
6322 : : {
6323 : 119706 : var = gimple_debug_source_bind_get_var (stmt);
6324 : :
6325 : 119706 : value = gimple_debug_source_bind_get_value (stmt);
6326 : :
6327 : 119706 : if (!VECTOR_TYPE_P (TREE_TYPE (var)))
6328 : 119706 : mode = DECL_MODE (var);
6329 : : else
6330 : 0 : mode = TYPE_MODE (TREE_TYPE (var));
6331 : :
6332 : 119706 : val = gen_rtx_VAR_LOCATION (mode, var, (rtx)value,
6333 : : VAR_INIT_STATUS_UNINITIALIZED);
6334 : : }
6335 : : else
6336 : 0 : gcc_unreachable ();
6337 : : }
6338 : : /* If this function was first compiled with markers
6339 : : enabled, but they're now disable (e.g. LTO), drop
6340 : : them on the floor. */
6341 : 9844859 : else if (gimple_debug_nonbind_marker_p (stmt)
6342 : 9844859 : && !MAY_HAVE_DEBUG_MARKER_INSNS)
6343 : 0 : goto delink_debug_stmt;
6344 : 9844859 : else if (gimple_debug_begin_stmt_p (stmt))
6345 : 3771210 : val = GEN_RTX_DEBUG_MARKER_BEGIN_STMT_PAT ();
6346 : 6073649 : else if (gimple_debug_inline_entry_p (stmt))
6347 : 6073649 : val = GEN_RTX_DEBUG_MARKER_INLINE_ENTRY_PAT ();
6348 : : else
6349 : : gcc_unreachable ();
6350 : :
6351 : 40057405 : last = get_last_insn ();
6352 : :
6353 : 40057405 : set_curr_insn_location (gimple_location (stmt));
6354 : :
6355 : 40057405 : emit_debug_insn (val);
6356 : :
6357 : 40057405 : if (dump_file && (dump_flags & TDF_DETAILS))
6358 : : {
6359 : : /* We can't dump the insn with a TREE where an RTX
6360 : : is expected. */
6361 : 1 : if (GET_CODE (val) == VAR_LOCATION)
6362 : : {
6363 : 0 : gcc_checking_assert (PAT_VAR_LOCATION_LOC (val) == (rtx)value);
6364 : 0 : PAT_VAR_LOCATION_LOC (val) = const0_rtx;
6365 : : }
6366 : 1 : maybe_dump_rtl_for_gimple_stmt (stmt, last);
6367 : 1 : if (GET_CODE (val) == VAR_LOCATION)
6368 : 0 : PAT_VAR_LOCATION_LOC (val) = (rtx)value;
6369 : : }
6370 : :
6371 : 40166248 : delink_debug_stmt:
6372 : : /* In order not to generate too many debug temporaries,
6373 : : we delink all uses of debug statements we already expanded.
6374 : : Therefore debug statements between definition and real
6375 : : use of TERed SSA names will continue to use the SSA name,
6376 : : and not be replaced with debug temps. */
6377 : 40166248 : delink_stmt_imm_use (stmt);
6378 : :
6379 : 40166248 : gsi = nsi;
6380 : 40166248 : gsi_next (&nsi);
6381 : 40166248 : if (gsi_end_p (nsi))
6382 : : break;
6383 : 39534371 : stmt = gsi_stmt (nsi);
6384 : 39534371 : if (!is_gimple_debug (stmt))
6385 : : break;
6386 : : }
6387 : :
6388 : 7497483 : set_curr_insn_location (sloc);
6389 : : }
6390 : : else
6391 : : {
6392 : 37921501 : gcall *call_stmt = dyn_cast <gcall *> (stmt);
6393 : 6575940 : if (call_stmt
6394 : 6575940 : && gimple_call_tail_p (call_stmt)
6395 : 168252 : && disable_tail_calls)
6396 : 12 : gimple_call_set_tail (call_stmt, false);
6397 : :
6398 : 37921501 : if (call_stmt && gimple_call_tail_p (call_stmt))
6399 : : {
6400 : 168240 : bool can_fallthru;
6401 : 168240 : new_bb = expand_gimple_tailcall (bb, call_stmt, &can_fallthru);
6402 : 168240 : if (new_bb)
6403 : : {
6404 : 117564 : if (can_fallthru)
6405 : 0 : bb = new_bb;
6406 : : else
6407 : : {
6408 : 117564 : currently_expanding_gimple_stmt = NULL;
6409 : 117564 : return new_bb;
6410 : : }
6411 : : }
6412 : : }
6413 : : else
6414 : : {
6415 : 37753261 : def_operand_p def_p;
6416 : 37753261 : def_p = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF);
6417 : :
6418 : 37753261 : if (def_p != NULL)
6419 : : {
6420 : : /* Ignore this stmt if it is in the list of
6421 : : replaceable expressions. */
6422 : 31842725 : if (SA.values
6423 : 39390770 : && bitmap_bit_p (SA.values,
6424 : 15890229 : SSA_NAME_VERSION (DEF_FROM_PTR (def_p))))
6425 : 8342184 : continue;
6426 : : }
6427 : 29411077 : last = expand_gimple_stmt (stmt);
6428 : 29411077 : maybe_dump_rtl_for_gimple_stmt (stmt, last);
6429 : : }
6430 : : }
6431 : : }
6432 : :
6433 : 11403332 : currently_expanding_gimple_stmt = NULL;
6434 : :
6435 : : /* Expand implicit goto and convert goto_locus. */
6436 : 27608105 : FOR_EACH_EDGE (e, ei, bb->succs)
6437 : : {
6438 : 16204773 : if (e->goto_locus != UNKNOWN_LOCATION || !nondebug_stmt_seen)
6439 : 11186015 : set_curr_insn_location (e->goto_locus);
6440 : 16204773 : if ((e->flags & EDGE_FALLTHRU) && e->dest != bb->next_bb)
6441 : : {
6442 : 1508065 : emit_jump (label_rtx_for_bb (e->dest));
6443 : 1508065 : e->flags &= ~EDGE_FALLTHRU;
6444 : : }
6445 : : }
6446 : :
6447 : : /* Expanded RTL can create a jump in the last instruction of block.
6448 : : This later might be assumed to be a jump to successor and break edge insertion.
6449 : : We need to insert dummy move to prevent this. PR41440. */
6450 : 11403332 : if (single_succ_p (bb)
6451 : 5238937 : && (single_succ_edge (bb)->flags & EDGE_FALLTHRU)
6452 : 2997164 : && (last = get_last_insn ())
6453 : 14233323 : && (JUMP_P (last)
6454 : 2997116 : || (DEBUG_INSN_P (last)
6455 : 349376 : && JUMP_P (prev_nondebug_insn (last)))))
6456 : : {
6457 : 53 : rtx dummy = gen_reg_rtx (SImode);
6458 : 53 : emit_insn_after_noloc (gen_move_insn (dummy, dummy), last, NULL);
6459 : : }
6460 : :
6461 : : /* A __builtin_unreachable () will insert a barrier that should end
6462 : : the basic block. In gimple, any code after it will have already
6463 : : deleted, even without optimization. If we emit additional code
6464 : : here, as we would to adjust the stack after a call, it should be
6465 : : eventually deleted, but it confuses internal checkers (PR118006)
6466 : : and optimizers before it does, because we don't expect to find
6467 : : barriers inside basic blocks. */
6468 : 11403332 : if (!BARRIER_P (get_last_insn ()))
6469 : 8318600 : do_pending_stack_adjust ();
6470 : : else
6471 : 3084732 : discard_pending_stack_adjust ();
6472 : :
6473 : : /* Find the block tail. The last insn in the block is the insn
6474 : : before a barrier and/or table jump insn. */
6475 : 11403332 : last = get_last_insn ();
6476 : 11403332 : if (BARRIER_P (last))
6477 : 3084732 : last = PREV_INSN (last);
6478 : 11403332 : if (JUMP_TABLE_DATA_P (last))
6479 : 6527 : last = PREV_INSN (PREV_INSN (last));
6480 : 11403332 : if (BARRIER_P (last))
6481 : 5492 : last = PREV_INSN (last);
6482 : 11403332 : BB_END (bb) = last;
6483 : :
6484 : 11403332 : update_bb_for_insn (bb);
6485 : :
6486 : 11403332 : return bb;
6487 : : }
6488 : :
6489 : :
6490 : : /* Create a basic block for initialization code. */
6491 : :
6492 : : static basic_block
6493 : 1432358 : construct_init_block (void)
6494 : : {
6495 : 1432358 : basic_block init_block, first_block;
6496 : 1432358 : edge e = NULL;
6497 : 1432358 : int flags;
6498 : :
6499 : : /* Multiple entry points not supported yet. */
6500 : 1432358 : gcc_assert (EDGE_COUNT (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs) == 1);
6501 : 1432358 : init_rtl_bb_info (ENTRY_BLOCK_PTR_FOR_FN (cfun));
6502 : 1432358 : init_rtl_bb_info (EXIT_BLOCK_PTR_FOR_FN (cfun));
6503 : 1432358 : ENTRY_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_RTL;
6504 : 1432358 : EXIT_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_RTL;
6505 : :
6506 : 1432358 : e = EDGE_SUCC (ENTRY_BLOCK_PTR_FOR_FN (cfun), 0);
6507 : :
6508 : : /* When entry edge points to first basic block, we don't need jump,
6509 : : otherwise we have to jump into proper target. */
6510 : 1432358 : if (e && e->dest != ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
6511 : : {
6512 : 0 : tree label = gimple_block_label (e->dest);
6513 : :
6514 : 0 : emit_jump (jump_target_rtx (label));
6515 : 0 : flags = 0;
6516 : 0 : }
6517 : : else
6518 : : flags = EDGE_FALLTHRU;
6519 : :
6520 : 1432358 : init_block = create_basic_block (NEXT_INSN (get_insns ()),
6521 : : get_last_insn (),
6522 : 1432358 : ENTRY_BLOCK_PTR_FOR_FN (cfun));
6523 : 1432358 : init_block->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
6524 : 1432358 : add_bb_to_loop (init_block, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
6525 : 1432358 : if (e)
6526 : : {
6527 : 1432358 : first_block = e->dest;
6528 : 1432358 : redirect_edge_succ (e, init_block);
6529 : 1432358 : make_single_succ_edge (init_block, first_block, flags);
6530 : : }
6531 : : else
6532 : 0 : make_single_succ_edge (init_block, EXIT_BLOCK_PTR_FOR_FN (cfun),
6533 : : EDGE_FALLTHRU);
6534 : :
6535 : 1432358 : update_bb_for_insn (init_block);
6536 : 1432358 : return init_block;
6537 : : }
6538 : :
6539 : : /* For each lexical block, set BLOCK_NUMBER to the depth at which it is
6540 : : found in the block tree. */
6541 : :
6542 : : static void
6543 : 16194356 : set_block_levels (tree block, int level)
6544 : : {
6545 : 30956354 : while (block)
6546 : : {
6547 : 14761998 : BLOCK_NUMBER (block) = level;
6548 : 14761998 : set_block_levels (BLOCK_SUBBLOCKS (block), level + 1);
6549 : 14761998 : block = BLOCK_CHAIN (block);
6550 : : }
6551 : 16194356 : }
6552 : :
6553 : : /* Create a block containing landing pads and similar stuff. */
6554 : :
6555 : : static void
6556 : 1432358 : construct_exit_block (void)
6557 : : {
6558 : 1432358 : rtx_insn *head = get_last_insn ();
6559 : 1432358 : rtx_insn *end;
6560 : 1432358 : basic_block exit_block;
6561 : 1432358 : edge e, e2;
6562 : 1432358 : unsigned ix;
6563 : 1432358 : edge_iterator ei;
6564 : 1432358 : basic_block prev_bb = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
6565 : 1432358 : rtx_insn *orig_end = BB_END (prev_bb);
6566 : :
6567 : 1432358 : rtl_profile_for_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
6568 : :
6569 : : /* Make sure the locus is set to the end of the function, so that
6570 : : epilogue line numbers and warnings are set properly. */
6571 : 1432358 : if (LOCATION_LOCUS (cfun->function_end_locus) != UNKNOWN_LOCATION)
6572 : 1119365 : input_location = cfun->function_end_locus;
6573 : :
6574 : : /* Generate rtl for function exit. */
6575 : 1432358 : expand_function_end ();
6576 : :
6577 : 1432358 : end = get_last_insn ();
6578 : 1432358 : if (head == end)
6579 : 0 : return;
6580 : : /* While emitting the function end we could move end of the last basic
6581 : : block. */
6582 : 1432358 : BB_END (prev_bb) = orig_end;
6583 : 1432358 : while (NEXT_INSN (head) && NOTE_P (NEXT_INSN (head)))
6584 : : head = NEXT_INSN (head);
6585 : : /* But make sure exit_block starts with RETURN_LABEL, otherwise the
6586 : : bb count counting will be confused. Any instructions before that
6587 : : label are emitted for the case where PREV_BB falls through into the
6588 : : exit block, so append those instructions to prev_bb in that case. */
6589 : 1432358 : if (NEXT_INSN (head) != return_label)
6590 : : {
6591 : 14559 : while (NEXT_INSN (head) != return_label)
6592 : : {
6593 : 9680 : if (!NOTE_P (NEXT_INSN (head)))
6594 : 9680 : BB_END (prev_bb) = NEXT_INSN (head);
6595 : 9680 : head = NEXT_INSN (head);
6596 : : }
6597 : : }
6598 : 1432358 : exit_block = create_basic_block (NEXT_INSN (head), end, prev_bb);
6599 : 1432358 : exit_block->count = EXIT_BLOCK_PTR_FOR_FN (cfun)->count;
6600 : 1432358 : add_bb_to_loop (exit_block, EXIT_BLOCK_PTR_FOR_FN (cfun)->loop_father);
6601 : :
6602 : 1432358 : ix = 0;
6603 : 4323568 : while (ix < EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds))
6604 : : {
6605 : 1458852 : e = EDGE_PRED (EXIT_BLOCK_PTR_FOR_FN (cfun), ix);
6606 : 1458852 : if (!(e->flags & EDGE_ABNORMAL))
6607 : 1341288 : redirect_edge_succ (e, exit_block);
6608 : : else
6609 : 117564 : ix++;
6610 : : }
6611 : :
6612 : 1432358 : e = make_single_succ_edge (exit_block, EXIT_BLOCK_PTR_FOR_FN (cfun),
6613 : : EDGE_FALLTHRU);
6614 : 2982280 : FOR_EACH_EDGE (e2, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6615 : 1549922 : if (e2 != e)
6616 : : {
6617 : 117564 : exit_block->count -= e2->count ();
6618 : : }
6619 : 1432358 : update_bb_for_insn (exit_block);
6620 : : }
6621 : :
6622 : : /* Helper function for discover_nonconstant_array_refs.
6623 : : Look for ARRAY_REF nodes with non-constant indexes and mark them
6624 : : addressable. */
6625 : :
6626 : : static tree
6627 : 155360945 : discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees,
6628 : : void *data)
6629 : : {
6630 : 155360945 : tree t = *tp;
6631 : 155360945 : bitmap forced_stack_vars = (bitmap)((walk_stmt_info *)data)->info;
6632 : :
6633 : 155360945 : if (IS_TYPE_OR_DECL_P (t))
6634 : 32610903 : *walk_subtrees = 0;
6635 : 122750042 : else if (REFERENCE_CLASS_P (t) && TREE_THIS_VOLATILE (t))
6636 : : {
6637 : 66089 : t = get_base_address (t);
6638 : 66089 : if (t && DECL_P (t)
6639 : 53726 : && DECL_MODE (t) != BLKmode
6640 : 102574 : && !TREE_ADDRESSABLE (t))
6641 : 32638 : bitmap_set_bit (forced_stack_vars, DECL_UID (t));
6642 : 66089 : *walk_subtrees = 0;
6643 : : }
6644 : 122683953 : else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
6645 : : {
6646 : 6001596 : while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
6647 : 2872121 : && is_gimple_min_invariant (TREE_OPERAND (t, 1))
6648 : 2415492 : && (!TREE_OPERAND (t, 2)
6649 : 28722 : || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
6650 : 3613719 : || (TREE_CODE (t) == COMPONENT_REF
6651 : 900148 : && (!TREE_OPERAND (t,2)
6652 : 0 : || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
6653 : 2713571 : || TREE_CODE (t) == BIT_FIELD_REF
6654 : : || TREE_CODE (t) == REALPART_EXPR
6655 : : || TREE_CODE (t) == IMAGPART_EXPR
6656 : : || TREE_CODE (t) == VIEW_CONVERT_EXPR
6657 : 6001596 : || CONVERT_EXPR_P (t))
6658 : 3297084 : t = TREE_OPERAND (t, 0);
6659 : :
6660 : 2704512 : if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
6661 : : {
6662 : 484244 : t = get_base_address (t);
6663 : 484244 : if (t && DECL_P (t)
6664 : 395557 : && DECL_MODE (t) != BLKmode
6665 : 507388 : && !TREE_ADDRESSABLE (t))
6666 : 14891 : bitmap_set_bit (forced_stack_vars, DECL_UID (t));
6667 : : }
6668 : :
6669 : 2704512 : *walk_subtrees = 0;
6670 : : }
6671 : : /* References of size POLY_INT_CST to a fixed-size object must go
6672 : : through memory. It's more efficient to force that here than
6673 : : to create temporary slots on the fly.
6674 : : RTL expansion expectes TARGET_MEM_REF to always address actual memory.
6675 : : Also, force to stack non-BLKmode vars accessed through VIEW_CONVERT_EXPR
6676 : : to BLKmode type. */
6677 : 119979441 : else if (TREE_CODE (t) == TARGET_MEM_REF
6678 : : || (TREE_CODE (t) == MEM_REF
6679 : 119208162 : && TYPE_SIZE (TREE_TYPE (t))
6680 : : && POLY_INT_CST_P (TYPE_SIZE (TREE_TYPE (t))))
6681 : 239187603 : || (TREE_CODE (t) == VIEW_CONVERT_EXPR
6682 : 311930 : && TYPE_MODE (TREE_TYPE (t)) == BLKmode))
6683 : : {
6684 : 848995 : tree base = get_base_address (t);
6685 : 848995 : if (base
6686 : 848995 : && DECL_P (base)
6687 : 258996 : && !TREE_ADDRESSABLE (base)
6688 : 122172 : && DECL_MODE (base) != BLKmode
6689 : 854617 : && GET_MODE_SIZE (DECL_MODE (base)).is_constant ())
6690 : 5622 : bitmap_set_bit (forced_stack_vars, DECL_UID (base));
6691 : 848995 : *walk_subtrees = 0;
6692 : : }
6693 : :
6694 : 155360945 : return NULL_TREE;
6695 : : }
6696 : :
6697 : : /* If there's a chance to get a pseudo for t then if it would be of float mode
6698 : : and the actual access is via an integer mode (lowered memcpy or similar
6699 : : access) then avoid the register expansion if the mode likely is not storage
6700 : : suitable for raw bits processing (like XFmode on i?86). */
6701 : :
6702 : : static void
6703 : 6316958 : avoid_type_punning_on_regs (tree t, bitmap forced_stack_vars)
6704 : : {
6705 : 6316958 : machine_mode access_mode = TYPE_MODE (TREE_TYPE (t));
6706 : 6316958 : if (access_mode != BLKmode
6707 : 5989903 : && !SCALAR_INT_MODE_P (access_mode))
6708 : : return;
6709 : 5382531 : tree base = get_base_address (t);
6710 : 5382531 : if (DECL_P (base)
6711 : 4081431 : && !TREE_ADDRESSABLE (base)
6712 : 1194764 : && FLOAT_MODE_P (DECL_MODE (base))
6713 : 62 : && maybe_lt (GET_MODE_PRECISION (DECL_MODE (base)),
6714 : 186 : GET_MODE_BITSIZE (GET_MODE_INNER (DECL_MODE (base))))
6715 : : /* Double check in the expensive way we really would get a pseudo. */
6716 : 5382536 : && use_register_for_decl (base))
6717 : 5 : bitmap_set_bit (forced_stack_vars, DECL_UID (base));
6718 : : }
6719 : :
6720 : : /* RTL expansion is not able to compile array references with variable
6721 : : offsets for arrays stored in single register. Discover such
6722 : : expressions and mark variables as addressable to avoid this
6723 : : scenario. */
6724 : :
6725 : : static void
6726 : 1432358 : discover_nonconstant_array_refs (bitmap forced_stack_vars)
6727 : : {
6728 : 1432358 : basic_block bb;
6729 : 1432358 : gimple_stmt_iterator gsi;
6730 : :
6731 : 1432358 : walk_stmt_info wi = {};
6732 : 1432358 : wi.info = forced_stack_vars;
6733 : 13386825 : FOR_EACH_BB_FN (bb, cfun)
6734 : 108591200 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6735 : : {
6736 : 84682266 : gimple *stmt = gsi_stmt (gsi);
6737 : 84682266 : if (!is_gimple_debug (stmt))
6738 : : {
6739 : 44411407 : walk_gimple_op (stmt, discover_nonconstant_array_refs_r, &wi);
6740 : 44411407 : gcall *call = dyn_cast <gcall *> (stmt);
6741 : 6575940 : if (call && gimple_call_internal_p (call))
6742 : : {
6743 : 185001 : tree cand = NULL_TREE;
6744 : 185001 : switch (gimple_call_internal_fn (call))
6745 : : {
6746 : 0 : case IFN_LOAD_LANES:
6747 : : /* The source must be a MEM. */
6748 : 0 : cand = gimple_call_arg (call, 0);
6749 : 0 : break;
6750 : 0 : case IFN_STORE_LANES:
6751 : : /* The destination must be a MEM. */
6752 : 0 : cand = gimple_call_lhs (call);
6753 : 0 : break;
6754 : : default:
6755 : : break;
6756 : : }
6757 : 0 : if (cand)
6758 : 0 : cand = get_base_address (cand);
6759 : 0 : if (cand
6760 : 0 : && DECL_P (cand)
6761 : 0 : && use_register_for_decl (cand))
6762 : 0 : bitmap_set_bit (forced_stack_vars, DECL_UID (cand));
6763 : : }
6764 : 137886119 : if (gimple_vdef (stmt))
6765 : : {
6766 : 14694450 : tree t = gimple_get_lhs (stmt);
6767 : 14694450 : if (t && REFERENCE_CLASS_P (t))
6768 : 6316958 : avoid_type_punning_on_regs (t, forced_stack_vars);
6769 : : }
6770 : : }
6771 : : }
6772 : 1432358 : }
6773 : :
6774 : : /* This function sets crtl->args.internal_arg_pointer to a virtual
6775 : : register if DRAP is needed. Local register allocator will replace
6776 : : virtual_incoming_args_rtx with the virtual register. */
6777 : :
6778 : : static void
6779 : 1432358 : expand_stack_alignment (void)
6780 : : {
6781 : 1432358 : rtx drap_rtx;
6782 : 1432358 : unsigned int preferred_stack_boundary;
6783 : :
6784 : 1432358 : if (! SUPPORTS_STACK_ALIGNMENT)
6785 : : return;
6786 : :
6787 : 1432358 : if (cfun->calls_alloca
6788 : 1432358 : || cfun->has_nonlocal_label
6789 : 1416839 : || crtl->has_nonlocal_goto)
6790 : 15920 : crtl->need_drap = true;
6791 : :
6792 : : /* Call update_stack_boundary here again to update incoming stack
6793 : : boundary. It may set incoming stack alignment to a different
6794 : : value after RTL expansion. TARGET_FUNCTION_OK_FOR_SIBCALL may
6795 : : use the minimum incoming stack alignment to check if it is OK
6796 : : to perform sibcall optimization since sibcall optimization will
6797 : : only align the outgoing stack to incoming stack boundary. */
6798 : 1432358 : if (targetm.calls.update_stack_boundary)
6799 : 1432358 : targetm.calls.update_stack_boundary ();
6800 : :
6801 : : /* The incoming stack frame has to be aligned at least at
6802 : : parm_stack_boundary. */
6803 : 1432358 : gcc_assert (crtl->parm_stack_boundary <= INCOMING_STACK_BOUNDARY);
6804 : :
6805 : : /* Update crtl->stack_alignment_estimated and use it later to align
6806 : : stack. We check PREFERRED_STACK_BOUNDARY if there may be non-call
6807 : : exceptions since callgraph doesn't collect incoming stack alignment
6808 : : in this case. */
6809 : 1432358 : if (cfun->can_throw_non_call_exceptions
6810 : 262743 : && PREFERRED_STACK_BOUNDARY > crtl->preferred_stack_boundary)
6811 : : preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
6812 : : else
6813 : 1404393 : preferred_stack_boundary = crtl->preferred_stack_boundary;
6814 : 1432358 : if (preferred_stack_boundary > crtl->stack_alignment_estimated)
6815 : 693534 : crtl->stack_alignment_estimated = preferred_stack_boundary;
6816 : 1432358 : if (preferred_stack_boundary > crtl->stack_alignment_needed)
6817 : 643776 : crtl->stack_alignment_needed = preferred_stack_boundary;
6818 : :
6819 : 1432358 : gcc_assert (crtl->stack_alignment_needed
6820 : : <= crtl->stack_alignment_estimated);
6821 : :
6822 : 1432358 : crtl->stack_realign_needed
6823 : 1432358 : = INCOMING_STACK_BOUNDARY < crtl->stack_alignment_estimated;
6824 : 1432358 : crtl->stack_realign_tried = crtl->stack_realign_needed;
6825 : :
6826 : 1432358 : crtl->stack_realign_processed = true;
6827 : :
6828 : : /* Target has to redefine TARGET_GET_DRAP_RTX to support stack
6829 : : alignment. */
6830 : 1432358 : gcc_assert (targetm.calls.get_drap_rtx != NULL);
6831 : 1432358 : drap_rtx = targetm.calls.get_drap_rtx ();
6832 : :
6833 : : /* stack_realign_drap and drap_rtx must match. */
6834 : 1432358 : gcc_assert ((stack_realign_drap != 0) == (drap_rtx != NULL));
6835 : :
6836 : : /* Do nothing if NULL is returned, which means DRAP is not needed. */
6837 : 1432358 : if (drap_rtx != NULL)
6838 : : {
6839 : 7117 : crtl->args.internal_arg_pointer = drap_rtx;
6840 : :
6841 : : /* Call fixup_tail_calls to clean up REG_EQUIV note if DRAP is
6842 : : needed. */
6843 : 7117 : fixup_tail_calls ();
6844 : : }
6845 : : }
6846 : :
6847 : :
6848 : : static void
6849 : 0 : expand_main_function (void)
6850 : : {
6851 : : #if (defined(INVOKE__main) \
6852 : : || (!defined(HAS_INIT_SECTION) \
6853 : : && !defined(INIT_SECTION_ASM_OP) \
6854 : : && !defined(INIT_ARRAY_SECTION_ASM_OP)))
6855 : : emit_library_call (init_one_libfunc (NAME__MAIN), LCT_NORMAL, VOIDmode);
6856 : : #endif
6857 : 0 : }
6858 : :
6859 : :
6860 : : /* Expand code to initialize the stack_protect_guard. This is invoked at
6861 : : the beginning of a function to be protected. */
6862 : :
6863 : : static void
6864 : 226 : stack_protect_prologue (void)
6865 : : {
6866 : 226 : tree guard_decl = targetm.stack_protect_guard ();
6867 : 226 : rtx x, y;
6868 : :
6869 : 226 : crtl->stack_protect_guard_decl = guard_decl;
6870 : 226 : x = expand_normal (crtl->stack_protect_guard);
6871 : :
6872 : 226 : if (targetm.have_stack_protect_combined_set () && guard_decl)
6873 : : {
6874 : 0 : gcc_assert (DECL_P (guard_decl));
6875 : 0 : y = DECL_RTL (guard_decl);
6876 : :
6877 : : /* Allow the target to compute address of Y and copy it to X without
6878 : : leaking Y into a register. This combined address + copy pattern
6879 : : allows the target to prevent spilling of any intermediate results by
6880 : : splitting it after register allocator. */
6881 : 0 : if (rtx_insn *insn = targetm.gen_stack_protect_combined_set (x, y))
6882 : : {
6883 : 0 : emit_insn (insn);
6884 : 0 : return;
6885 : : }
6886 : : }
6887 : :
6888 : 226 : if (guard_decl)
6889 : 226 : y = expand_normal (guard_decl);
6890 : : else
6891 : 0 : y = const0_rtx;
6892 : :
6893 : : /* Allow the target to copy from Y to X without leaking Y into a
6894 : : register. */
6895 : 226 : if (targetm.have_stack_protect_set ())
6896 : 226 : if (rtx_insn *insn = targetm.gen_stack_protect_set (x, y))
6897 : : {
6898 : 226 : emit_insn (insn);
6899 : 226 : return;
6900 : : }
6901 : :
6902 : : /* Otherwise do a straight move. */
6903 : 0 : emit_move_insn (x, y);
6904 : : }
6905 : :
6906 : : /* Translate the intermediate representation contained in the CFG
6907 : : from GIMPLE trees to RTL.
6908 : :
6909 : : We do conversion per basic block and preserve/update the tree CFG.
6910 : : This implies we have to do some magic as the CFG can simultaneously
6911 : : consist of basic blocks containing RTL and GIMPLE trees. This can
6912 : : confuse the CFG hooks, so be careful to not manipulate CFG during
6913 : : the expansion. */
6914 : :
6915 : : namespace {
6916 : :
6917 : : const pass_data pass_data_expand =
6918 : : {
6919 : : RTL_PASS, /* type */
6920 : : "expand", /* name */
6921 : : OPTGROUP_NONE, /* optinfo_flags */
6922 : : TV_EXPAND, /* tv_id */
6923 : : ( PROP_ssa | PROP_gimple_leh | PROP_cfg
6924 : : | PROP_gimple_lcx
6925 : : | PROP_gimple_lvec
6926 : : | PROP_gimple_lva), /* properties_required */
6927 : : PROP_rtl, /* properties_provided */
6928 : : ( PROP_ssa | PROP_gimple ), /* properties_destroyed */
6929 : : 0, /* todo_flags_start */
6930 : : 0, /* todo_flags_finish */
6931 : : };
6932 : :
6933 : : class pass_expand : public rtl_opt_pass
6934 : : {
6935 : : public:
6936 : 282870 : pass_expand (gcc::context *ctxt)
6937 : 565740 : : rtl_opt_pass (pass_data_expand, ctxt)
6938 : : {}
6939 : :
6940 : : /* opt_pass methods: */
6941 : : unsigned int execute (function *) final override;
6942 : :
6943 : : }; // class pass_expand
6944 : :
6945 : : unsigned int
6946 : 1432358 : pass_expand::execute (function *fun)
6947 : : {
6948 : 1432358 : basic_block bb, init_block;
6949 : 1432358 : edge_iterator ei;
6950 : 1432358 : edge e;
6951 : 1432358 : rtx_insn *var_seq, *var_ret_seq;
6952 : 1432358 : unsigned i;
6953 : :
6954 : 1432358 : timevar_push (TV_OUT_OF_SSA);
6955 : 1432358 : rewrite_out_of_ssa (&SA);
6956 : 1432358 : timevar_pop (TV_OUT_OF_SSA);
6957 : 1432358 : SA.partition_to_pseudo = XCNEWVEC (rtx, SA.map->num_partitions);
6958 : :
6959 : 1432358 : if (MAY_HAVE_DEBUG_BIND_STMTS && flag_tree_ter)
6960 : : {
6961 : 477584 : gimple_stmt_iterator gsi;
6962 : 6276079 : FOR_EACH_BB_FN (bb, cfun)
6963 : 71813578 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6964 : 90507730 : if (gimple_debug_bind_p (gsi_stmt (gsi)))
6965 : 30291142 : avoid_deep_ter_for_debug (gsi_stmt (gsi), 0);
6966 : : }
6967 : :
6968 : : /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE. */
6969 : 1432358 : auto_bitmap forced_stack_vars;
6970 : 1432358 : discover_nonconstant_array_refs (forced_stack_vars);
6971 : :
6972 : : /* Make sure all values used by the optimization passes have sane
6973 : : defaults. */
6974 : 1432358 : reg_renumber = 0;
6975 : :
6976 : : /* Some backends want to know that we are expanding to RTL. */
6977 : 1432358 : currently_expanding_to_rtl = 1;
6978 : : /* Dominators are not kept up-to-date as we may create new basic-blocks. */
6979 : 1432358 : free_dominance_info (CDI_DOMINATORS);
6980 : :
6981 : 1432358 : rtl_profile_for_bb (ENTRY_BLOCK_PTR_FOR_FN (fun));
6982 : :
6983 : 1432358 : insn_locations_init ();
6984 : 1432358 : if (!DECL_IS_UNDECLARED_BUILTIN (current_function_decl))
6985 : : {
6986 : : /* Eventually, all FEs should explicitly set function_start_locus. */
6987 : 1311812 : if (LOCATION_LOCUS (fun->function_start_locus) == UNKNOWN_LOCATION)
6988 : 600392 : set_curr_insn_location
6989 : 600392 : (DECL_SOURCE_LOCATION (current_function_decl));
6990 : : else
6991 : 711420 : set_curr_insn_location (fun->function_start_locus);
6992 : : }
6993 : : else
6994 : 120546 : set_curr_insn_location (UNKNOWN_LOCATION);
6995 : 1432358 : prologue_location = curr_insn_location ();
6996 : :
6997 : : #ifdef INSN_SCHEDULING
6998 : 1432358 : init_sched_attrs ();
6999 : : #endif
7000 : :
7001 : : /* Make sure first insn is a note even if we don't want linenums.
7002 : : This makes sure the first insn will never be deleted.
7003 : : Also, final expects a note to appear there. */
7004 : 1432358 : emit_note (NOTE_INSN_DELETED);
7005 : :
7006 : 1432358 : targetm.expand_to_rtl_hook ();
7007 : 1432358 : crtl->init_stack_alignment ();
7008 : 1432358 : fun->cfg->max_jumptable_ents = 0;
7009 : :
7010 : : /* Resovle the function section. Some targets, like ARM EABI rely on knowledge
7011 : : of the function section at exapnsion time to predict distance of calls. */
7012 : 1432358 : resolve_unique_section (current_function_decl, 0, flag_function_sections);
7013 : :
7014 : : /* Expand the variables recorded during gimple lowering. */
7015 : 1432358 : timevar_push (TV_VAR_EXPAND);
7016 : 1432358 : start_sequence ();
7017 : :
7018 : 1432358 : var_ret_seq = expand_used_vars (forced_stack_vars);
7019 : :
7020 : 1432358 : var_seq = get_insns ();
7021 : 1432358 : end_sequence ();
7022 : 1432358 : timevar_pop (TV_VAR_EXPAND);
7023 : :
7024 : : /* Honor stack protection warnings. */
7025 : 1432358 : if (warn_stack_protect)
7026 : : {
7027 : 0 : if (fun->calls_alloca)
7028 : 0 : warning (OPT_Wstack_protector,
7029 : : "stack protector not protecting local variables: "
7030 : : "variable length buffer");
7031 : 0 : if (has_short_buffer && !crtl->stack_protect_guard)
7032 : 0 : warning (OPT_Wstack_protector,
7033 : : "stack protector not protecting function: "
7034 : : "all local arrays are less than %d bytes long",
7035 : 0 : (int) param_ssp_buffer_size);
7036 : : }
7037 : :
7038 : : /* Temporarily mark PARM_DECLs and RESULT_DECLs we need to expand to
7039 : : memory addressable so expand_function_start can emit the required
7040 : : copies. */
7041 : 1432358 : auto_vec<tree, 16> marked_parms;
7042 : 4488081 : for (tree parm = DECL_ARGUMENTS (current_function_decl); parm;
7043 : 3055723 : parm = DECL_CHAIN (parm))
7044 : 3055723 : if (!TREE_ADDRESSABLE (parm)
7045 : 3055723 : && bitmap_bit_p (forced_stack_vars, DECL_UID (parm)))
7046 : : {
7047 : 135 : TREE_ADDRESSABLE (parm) = 1;
7048 : 135 : marked_parms.safe_push (parm);
7049 : : }
7050 : 1432358 : if (DECL_RESULT (current_function_decl)
7051 : 1432358 : && !TREE_ADDRESSABLE (DECL_RESULT (current_function_decl))
7052 : 2858108 : && bitmap_bit_p (forced_stack_vars,
7053 : 1425750 : DECL_UID (DECL_RESULT (current_function_decl))))
7054 : : {
7055 : 0 : TREE_ADDRESSABLE (DECL_RESULT (current_function_decl)) = 1;
7056 : 0 : marked_parms.safe_push (DECL_RESULT (current_function_decl));
7057 : : }
7058 : :
7059 : : /* Set up parameters and prepare for return, for the function. */
7060 : 1432358 : expand_function_start (current_function_decl);
7061 : :
7062 : : /* Clear TREE_ADDRESSABLE again. */
7063 : 2864851 : while (!marked_parms.is_empty ())
7064 : 135 : TREE_ADDRESSABLE (marked_parms.pop ()) = 0;
7065 : :
7066 : : /* If we emitted any instructions for setting up the variables,
7067 : : emit them before the FUNCTION_START note. */
7068 : 1432358 : if (var_seq)
7069 : : {
7070 : 1499 : emit_insn_before (var_seq, parm_birth_insn);
7071 : :
7072 : : /* In expand_function_end we'll insert the alloca save/restore
7073 : : before parm_birth_insn. We've just insertted an alloca call.
7074 : : Adjust the pointer to match. */
7075 : 1499 : parm_birth_insn = var_seq;
7076 : : }
7077 : :
7078 : : /* Now propagate the RTL assignment of each partition to the
7079 : : underlying var of each SSA_NAME. */
7080 : : tree name;
7081 : :
7082 : 68179960 : FOR_EACH_SSA_NAME (i, name, cfun)
7083 : : {
7084 : : /* We might have generated new SSA names in
7085 : : update_alias_info_with_stack_vars. They will have a NULL
7086 : : defining statements, and won't be part of the partitioning,
7087 : : so ignore those. */
7088 : 46583058 : if (!SSA_NAME_DEF_STMT (name))
7089 : 66719 : continue;
7090 : :
7091 : 46516339 : adjust_one_expanded_partition_var (name);
7092 : : }
7093 : :
7094 : : /* Clean up RTL of variables that straddle across multiple
7095 : : partitions, and check that the rtl of any PARM_DECLs that are not
7096 : : cleaned up is that of their default defs. */
7097 : 68179960 : FOR_EACH_SSA_NAME (i, name, cfun)
7098 : : {
7099 : 46583058 : int part;
7100 : :
7101 : : /* We might have generated new SSA names in
7102 : : update_alias_info_with_stack_vars. They will have a NULL
7103 : : defining statements, and won't be part of the partitioning,
7104 : : so ignore those. */
7105 : 46583058 : if (!SSA_NAME_DEF_STMT (name))
7106 : 66719 : continue;
7107 : 46516339 : part = var_to_partition (SA.map, name);
7108 : 46516339 : if (part == NO_PARTITION)
7109 : 16514611 : continue;
7110 : :
7111 : : /* If this decl was marked as living in multiple places, reset
7112 : : this now to NULL. */
7113 : 30001728 : tree var = SSA_NAME_VAR (name);
7114 : 10047238 : if (var && DECL_RTL_IF_SET (var) == pc_rtx)
7115 : 206396 : SET_DECL_RTL (var, NULL);
7116 : : /* Check that the pseudos chosen by assign_parms are those of
7117 : : the corresponding default defs. */
7118 : 29795332 : else if (SSA_NAME_IS_DEFAULT_DEF (name)
7119 : 29795332 : && (TREE_CODE (var) == PARM_DECL
7120 : 3600249 : || TREE_CODE (var) == RESULT_DECL))
7121 : : {
7122 : 3547032 : rtx in = DECL_RTL_IF_SET (var);
7123 : 3547032 : gcc_assert (in);
7124 : 3547032 : rtx out = SA.partition_to_pseudo[part];
7125 : 3547032 : gcc_assert (in == out);
7126 : :
7127 : : /* Now reset VAR's RTL to IN, so that the _EXPR attrs match
7128 : : those expected by debug backends for each parm and for
7129 : : the result. This is particularly important for stabs,
7130 : : whose register elimination from parm's DECL_RTL may cause
7131 : : -fcompare-debug differences as SET_DECL_RTL changes reg's
7132 : : attrs. So, make sure the RTL already has the parm as the
7133 : : EXPR, so that it won't change. */
7134 : 3547032 : SET_DECL_RTL (var, NULL_RTX);
7135 : 3547032 : if (MEM_P (in))
7136 : 763184 : set_mem_attributes (in, var, true);
7137 : 3547032 : SET_DECL_RTL (var, in);
7138 : : }
7139 : : }
7140 : :
7141 : : /* If this function is `main', emit a call to `__main'
7142 : : to run global initializers, etc. */
7143 : 1432358 : if (DECL_NAME (current_function_decl)
7144 : 1432358 : && MAIN_NAME_P (DECL_NAME (current_function_decl))
7145 : 1542230 : && DECL_FILE_SCOPE_P (current_function_decl))
7146 : 1432358 : expand_main_function ();
7147 : :
7148 : : /* Initialize the stack_protect_guard field. This must happen after the
7149 : : call to __main (if any) so that the external decl is initialized. */
7150 : 1432358 : if (crtl->stack_protect_guard && targetm.stack_protect_runtime_enabled_p ())
7151 : 226 : stack_protect_prologue ();
7152 : :
7153 : 1432358 : expand_phi_nodes (&SA);
7154 : :
7155 : : /* Release any stale SSA redirection data. */
7156 : 1432358 : redirect_edge_var_map_empty ();
7157 : :
7158 : : /* Register rtl specific functions for cfg. */
7159 : 1432358 : rtl_register_cfg_hooks ();
7160 : :
7161 : 1432358 : init_block = construct_init_block ();
7162 : :
7163 : : /* Clear EDGE_EXECUTABLE on the entry edge(s). It is cleaned from the
7164 : : remaining edges later. */
7165 : 2864716 : FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (fun)->succs)
7166 : 1432358 : e->flags &= ~EDGE_EXECUTABLE;
7167 : :
7168 : : /* If the function has too many markers, drop them while expanding. */
7169 : 1432358 : if (cfun->debug_marker_count
7170 : 1432358 : >= param_max_debug_marker_count)
7171 : 1 : cfun->debug_nonbind_markers = false;
7172 : :
7173 : 1432358 : lab_rtx_for_bb = new hash_map<basic_block, rtx_code_label *>;
7174 : 13403450 : FOR_BB_BETWEEN (bb, init_block->next_bb, EXIT_BLOCK_PTR_FOR_FN (fun),
7175 : : next_bb)
7176 : 11971092 : bb = expand_gimple_basic_block (bb, var_ret_seq != NULL_RTX);
7177 : :
7178 : 1432358 : if (MAY_HAVE_DEBUG_BIND_INSNS)
7179 : 477609 : expand_debug_locations ();
7180 : :
7181 : 1432358 : if (deep_ter_debug_map)
7182 : : {
7183 : 528 : delete deep_ter_debug_map;
7184 : 528 : deep_ter_debug_map = NULL;
7185 : : }
7186 : :
7187 : : /* Free stuff we no longer need after GIMPLE optimizations. */
7188 : 1432358 : free_dominance_info (CDI_DOMINATORS);
7189 : 1432358 : free_dominance_info (CDI_POST_DOMINATORS);
7190 : 1432358 : delete_tree_cfg_annotations (fun);
7191 : :
7192 : 1432358 : timevar_push (TV_OUT_OF_SSA);
7193 : 1432358 : finish_out_of_ssa (&SA);
7194 : 1432358 : timevar_pop (TV_OUT_OF_SSA);
7195 : :
7196 : 1432358 : timevar_push (TV_POST_EXPAND);
7197 : : /* We are no longer in SSA form. */
7198 : 1432358 : fun->gimple_df->in_ssa_p = false;
7199 : 1432358 : loops_state_clear (LOOP_CLOSED_SSA);
7200 : :
7201 : : /* Expansion is used by optimization passes too, set maybe_hot_insn_p
7202 : : conservatively to true until they are all profile aware. */
7203 : 2864716 : delete lab_rtx_for_bb;
7204 : 1432358 : free_histograms (fun);
7205 : :
7206 : 1432358 : construct_exit_block ();
7207 : 1432358 : insn_locations_finalize ();
7208 : :
7209 : 1432358 : if (var_ret_seq)
7210 : : {
7211 : 1639 : rtx_insn *after = return_label;
7212 : 1639 : rtx_insn *next = NEXT_INSN (after);
7213 : 1639 : if (next && NOTE_INSN_BASIC_BLOCK_P (next))
7214 : 1639 : after = next;
7215 : 1639 : emit_insn_after (var_ret_seq, after);
7216 : : }
7217 : :
7218 : 1432358 : if (hwasan_sanitize_stack_p ())
7219 : 462 : hwasan_maybe_emit_frame_base_init ();
7220 : :
7221 : : /* Zap the tree EH table. */
7222 : 1432358 : set_eh_throw_stmt_table (fun, NULL);
7223 : :
7224 : : /* We need JUMP_LABEL be set in order to redirect jumps, and hence
7225 : : split edges which edge insertions might do. */
7226 : 1432358 : rebuild_jump_labels (get_insns ());
7227 : :
7228 : : /* If we have a single successor to the entry block, put the pending insns
7229 : : after parm birth, but before NOTE_INSNS_FUNCTION_BEG. */
7230 : 1432358 : if (single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (fun)))
7231 : : {
7232 : 1432358 : edge e = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (fun));
7233 : 1432358 : if (e->insns.r)
7234 : : {
7235 : 5 : rtx_insn *insns = e->insns.r;
7236 : 5 : e->insns.r = NULL;
7237 : 5 : rebuild_jump_labels_chain (insns);
7238 : 5 : if (NOTE_P (parm_birth_insn)
7239 : 5 : && NOTE_KIND (parm_birth_insn) == NOTE_INSN_FUNCTION_BEG)
7240 : 5 : emit_insn_before_noloc (insns, parm_birth_insn, e->dest);
7241 : : else
7242 : 0 : emit_insn_after_noloc (insns, parm_birth_insn, e->dest);
7243 : : }
7244 : : }
7245 : :
7246 : : /* Otherwise, as well as for other edges, take the usual way. */
7247 : 1432358 : commit_edge_insertions ();
7248 : :
7249 : : /* We're done expanding trees to RTL. */
7250 : 1432358 : currently_expanding_to_rtl = 0;
7251 : :
7252 : 1432358 : flush_mark_addressable_queue ();
7253 : :
7254 : 17402168 : FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (fun)->next_bb,
7255 : : EXIT_BLOCK_PTR_FOR_FN (fun), next_bb)
7256 : : {
7257 : 15969810 : edge e;
7258 : 15969810 : edge_iterator ei;
7259 : 37191257 : for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
7260 : : {
7261 : : /* Clear EDGE_EXECUTABLE. This flag is never used in the backend. */
7262 : 21221447 : e->flags &= ~EDGE_EXECUTABLE;
7263 : :
7264 : : /* At the moment not all abnormal edges match the RTL
7265 : : representation. It is safe to remove them here as
7266 : : find_many_sub_basic_blocks will rediscover them.
7267 : : In the future we should get this fixed properly. */
7268 : 21221447 : if ((e->flags & EDGE_ABNORMAL)
7269 : : && !(e->flags & EDGE_SIBCALL))
7270 : 9751 : remove_edge (e);
7271 : : else
7272 : 21211696 : ei_next (&ei);
7273 : : }
7274 : : }
7275 : :
7276 : 1432358 : auto_sbitmap blocks (last_basic_block_for_fn (fun));
7277 : 1432358 : bitmap_ones (blocks);
7278 : 1432358 : find_many_sub_basic_blocks (blocks);
7279 : 1432358 : purge_all_dead_edges ();
7280 : :
7281 : : /* After initial rtl generation, call back to finish generating
7282 : : exception support code. We need to do this before cleaning up
7283 : : the CFG as the code does not expect dead landing pads. */
7284 : 1432358 : if (fun->eh->region_tree != NULL)
7285 : 58214 : finish_eh_generation ();
7286 : :
7287 : : /* Call expand_stack_alignment after finishing all
7288 : : updates to crtl->preferred_stack_boundary. */
7289 : 1432358 : expand_stack_alignment ();
7290 : :
7291 : : /* Fixup REG_EQUIV notes in the prologue if there are tailcalls in this
7292 : : function. */
7293 : 1432358 : if (crtl->tail_call_emit)
7294 : 107232 : fixup_tail_calls ();
7295 : :
7296 : 1432358 : HOST_WIDE_INT patch_area_size, patch_area_entry;
7297 : 1432358 : parse_and_check_patch_area (flag_patchable_function_entry, false,
7298 : : &patch_area_size, &patch_area_entry);
7299 : :
7300 : 1432358 : tree patchable_function_entry_attr
7301 : 1432358 : = lookup_attribute ("patchable_function_entry",
7302 : 1432358 : DECL_ATTRIBUTES (cfun->decl));
7303 : 1432358 : if (patchable_function_entry_attr)
7304 : : {
7305 : 16 : tree pp_val = TREE_VALUE (patchable_function_entry_attr);
7306 : 16 : tree patchable_function_entry_value1 = TREE_VALUE (pp_val);
7307 : :
7308 : 16 : patch_area_size = tree_to_uhwi (patchable_function_entry_value1);
7309 : 16 : patch_area_entry = 0;
7310 : 16 : if (TREE_CHAIN (pp_val) != NULL_TREE)
7311 : : {
7312 : 8 : tree patchable_function_entry_value2
7313 : 8 : = TREE_VALUE (TREE_CHAIN (pp_val));
7314 : 8 : patch_area_entry = tree_to_uhwi (patchable_function_entry_value2);
7315 : : }
7316 : : }
7317 : :
7318 : 1432358 : if (patch_area_entry > patch_area_size)
7319 : : {
7320 : 0 : if (patch_area_size > 0)
7321 : 0 : warning (OPT_Wattributes,
7322 : : "patchable function entry %wu exceeds size %wu",
7323 : : patch_area_entry, patch_area_size);
7324 : 0 : patch_area_entry = 0;
7325 : : }
7326 : :
7327 : 1432358 : crtl->patch_area_size = patch_area_size;
7328 : 1432358 : crtl->patch_area_entry = patch_area_entry;
7329 : :
7330 : : /* BB subdivision may have created basic blocks that are only reachable
7331 : : from unlikely bbs but not marked as such in the profile. */
7332 : 1432358 : if (optimize)
7333 : 1003516 : propagate_unlikely_bbs_forward ();
7334 : :
7335 : : /* Remove unreachable blocks, otherwise we cannot compute dominators
7336 : : which are needed for loop state verification. As a side-effect
7337 : : this also compacts blocks.
7338 : : ??? We cannot remove trivially dead insns here as for example
7339 : : the DRAP reg on i?86 is not magically live at this point.
7340 : : gcc.c-torture/execute/ipa-sra-2.c execution, -Os -m32 fails otherwise. */
7341 : 1432358 : cleanup_cfg (CLEANUP_NO_INSN_DEL);
7342 : :
7343 : 1432358 : checking_verify_flow_info ();
7344 : :
7345 : : /* Initialize pseudos allocated for hard registers. */
7346 : 1432358 : emit_initial_value_sets ();
7347 : :
7348 : : /* And finally unshare all RTL. */
7349 : 1432358 : unshare_all_rtl ();
7350 : :
7351 : : /* There's no need to defer outputting this function any more; we
7352 : : know we want to output it. */
7353 : 1432358 : DECL_DEFER_OUTPUT (current_function_decl) = 0;
7354 : :
7355 : : /* Now that we're done expanding trees to RTL, we shouldn't have any
7356 : : more CONCATs anywhere. */
7357 : 1432358 : generating_concat_p = 0;
7358 : :
7359 : 1432358 : if (dump_file)
7360 : : {
7361 : 332 : fprintf (dump_file,
7362 : : "\n\n;;\n;; Full RTL generated for this function:\n;;\n");
7363 : : /* And the pass manager will dump RTL for us. */
7364 : : }
7365 : :
7366 : : /* If we're emitting a nested function, make sure its parent gets
7367 : : emitted as well. Doing otherwise confuses debug info. */
7368 : 1432358 : {
7369 : 1432358 : tree parent;
7370 : 1432358 : for (parent = DECL_CONTEXT (current_function_decl);
7371 : 3042797 : parent != NULL_TREE;
7372 : 1610439 : parent = get_containing_scope (parent))
7373 : 1610439 : if (TREE_CODE (parent) == FUNCTION_DECL)
7374 : 69686 : TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1;
7375 : : }
7376 : :
7377 : 1432358 : TREE_ASM_WRITTEN (current_function_decl) = 1;
7378 : :
7379 : : /* After expanding, the return labels are no longer needed. */
7380 : 1432358 : return_label = NULL;
7381 : 1432358 : naked_return_label = NULL;
7382 : :
7383 : : /* After expanding, the tm_restart map is no longer needed. */
7384 : 1432358 : if (fun->gimple_df->tm_restart)
7385 : 265 : fun->gimple_df->tm_restart = NULL;
7386 : :
7387 : : /* Tag the blocks with a depth number so that change_scope can find
7388 : : the common parent easily. */
7389 : 1432358 : set_block_levels (DECL_INITIAL (fun->decl), 0);
7390 : 1432358 : default_rtl_profile ();
7391 : :
7392 : : /* For -dx discard loops now, otherwise IL verify in clean_state will
7393 : : ICE. */
7394 : 1432358 : if (rtl_dump_and_exit)
7395 : : {
7396 : 79 : cfun->curr_properties &= ~PROP_loops;
7397 : 79 : loop_optimizer_finalize ();
7398 : : }
7399 : :
7400 : 1432358 : timevar_pop (TV_POST_EXPAND);
7401 : :
7402 : 1432358 : return 0;
7403 : 1432358 : }
7404 : :
7405 : : } // anon namespace
7406 : :
7407 : : rtl_opt_pass *
7408 : 282870 : make_pass_expand (gcc::context *ctxt)
7409 : : {
7410 : 282870 : return new pass_expand (ctxt);
7411 : : }
|