]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/tree-stdarg.c
c-common.c, [...]: Replace tree_low_cst (..., 0) with tree_to_shwi throughout.
[thirdparty/gcc.git] / gcc / tree-stdarg.c
1 /* Pass computing data for optimizing stdarg functions.
2 Copyright (C) 2004-2013 Free Software Foundation, Inc.
3 Contributed by Jakub Jelinek <jakub@redhat.com>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "function.h"
27 #include "langhooks.h"
28 #include "gimple-pretty-print.h"
29 #include "target.h"
30 #include "bitmap.h"
31 #include "gimple.h"
32 #include "gimple-iterator.h"
33 #include "gimple-walk.h"
34 #include "gimple-ssa.h"
35 #include "tree-phinodes.h"
36 #include "ssa-iterators.h"
37 #include "tree-ssanames.h"
38 #include "sbitmap.h"
39 #include "tree-pass.h"
40 #include "tree-stdarg.h"
41
42 /* A simple pass that attempts to optimize stdarg functions on architectures
43 that need to save register arguments to stack on entry to stdarg functions.
44 If the function doesn't use any va_start macros, no registers need to
45 be saved. If va_start macros are used, the va_list variables don't escape
46 the function, it is only necessary to save registers that will be used
47 in va_arg macros. E.g. if va_arg is only used with integral types
48 in the function, floating point registers don't need to be saved, etc. */
49
50
51 /* Return true if basic block VA_ARG_BB is dominated by VA_START_BB and
52 is executed at most as many times as VA_START_BB. */
53
54 static bool
55 reachable_at_most_once (basic_block va_arg_bb, basic_block va_start_bb)
56 {
57 vec<edge> stack = vNULL;
58 edge e;
59 edge_iterator ei;
60 sbitmap visited;
61 bool ret;
62
63 if (va_arg_bb == va_start_bb)
64 return true;
65
66 if (! dominated_by_p (CDI_DOMINATORS, va_arg_bb, va_start_bb))
67 return false;
68
69 visited = sbitmap_alloc (last_basic_block);
70 bitmap_clear (visited);
71 ret = true;
72
73 FOR_EACH_EDGE (e, ei, va_arg_bb->preds)
74 stack.safe_push (e);
75
76 while (! stack.is_empty ())
77 {
78 basic_block src;
79
80 e = stack.pop ();
81 src = e->src;
82
83 if (e->flags & EDGE_COMPLEX)
84 {
85 ret = false;
86 break;
87 }
88
89 if (src == va_start_bb)
90 continue;
91
92 /* va_arg_bb can be executed more times than va_start_bb. */
93 if (src == va_arg_bb)
94 {
95 ret = false;
96 break;
97 }
98
99 gcc_assert (src != ENTRY_BLOCK_PTR);
100
101 if (! bitmap_bit_p (visited, src->index))
102 {
103 bitmap_set_bit (visited, src->index);
104 FOR_EACH_EDGE (e, ei, src->preds)
105 stack.safe_push (e);
106 }
107 }
108
109 stack.release ();
110 sbitmap_free (visited);
111 return ret;
112 }
113
114
115 /* For statement COUNTER = RHS, if RHS is COUNTER + constant,
116 return constant, otherwise return HOST_WIDE_INT_M1U.
117 GPR_P is true if this is GPR counter. */
118
119 static unsigned HOST_WIDE_INT
120 va_list_counter_bump (struct stdarg_info *si, tree counter, tree rhs,
121 bool gpr_p)
122 {
123 tree lhs, orig_lhs;
124 gimple stmt;
125 unsigned HOST_WIDE_INT ret = 0, val, counter_val;
126 unsigned int max_size;
127
128 if (si->offsets == NULL)
129 {
130 unsigned int i;
131
132 si->offsets = XNEWVEC (int, num_ssa_names);
133 for (i = 0; i < num_ssa_names; ++i)
134 si->offsets[i] = -1;
135 }
136
137 counter_val = gpr_p ? cfun->va_list_gpr_size : cfun->va_list_fpr_size;
138 max_size = gpr_p ? VA_LIST_MAX_GPR_SIZE : VA_LIST_MAX_FPR_SIZE;
139 orig_lhs = lhs = rhs;
140 while (lhs)
141 {
142 enum tree_code rhs_code;
143 tree rhs1;
144
145 if (si->offsets[SSA_NAME_VERSION (lhs)] != -1)
146 {
147 if (counter_val >= max_size)
148 {
149 ret = max_size;
150 break;
151 }
152
153 ret -= counter_val - si->offsets[SSA_NAME_VERSION (lhs)];
154 break;
155 }
156
157 stmt = SSA_NAME_DEF_STMT (lhs);
158
159 if (!is_gimple_assign (stmt) || gimple_assign_lhs (stmt) != lhs)
160 return HOST_WIDE_INT_M1U;
161
162 rhs_code = gimple_assign_rhs_code (stmt);
163 rhs1 = gimple_assign_rhs1 (stmt);
164 if ((get_gimple_rhs_class (rhs_code) == GIMPLE_SINGLE_RHS
165 || gimple_assign_cast_p (stmt))
166 && TREE_CODE (rhs1) == SSA_NAME)
167 {
168 lhs = rhs1;
169 continue;
170 }
171
172 if ((rhs_code == POINTER_PLUS_EXPR
173 || rhs_code == PLUS_EXPR)
174 && TREE_CODE (rhs1) == SSA_NAME
175 && tree_fits_uhwi_p (gimple_assign_rhs2 (stmt)))
176 {
177 ret += tree_low_cst (gimple_assign_rhs2 (stmt), 1);
178 lhs = rhs1;
179 continue;
180 }
181
182 if (rhs_code == ADDR_EXPR
183 && TREE_CODE (TREE_OPERAND (rhs1, 0)) == MEM_REF
184 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (rhs1, 0), 0)) == SSA_NAME
185 && tree_fits_uhwi_p (TREE_OPERAND (TREE_OPERAND (rhs1, 0), 1)))
186 {
187 ret += tree_low_cst (TREE_OPERAND (TREE_OPERAND (rhs1, 0), 1), 1);
188 lhs = TREE_OPERAND (TREE_OPERAND (rhs1, 0), 0);
189 continue;
190 }
191
192 if (get_gimple_rhs_class (rhs_code) != GIMPLE_SINGLE_RHS)
193 return HOST_WIDE_INT_M1U;
194
195 rhs = gimple_assign_rhs1 (stmt);
196 if (TREE_CODE (counter) != TREE_CODE (rhs))
197 return HOST_WIDE_INT_M1U;
198
199 if (TREE_CODE (counter) == COMPONENT_REF)
200 {
201 if (get_base_address (counter) != get_base_address (rhs)
202 || TREE_CODE (TREE_OPERAND (rhs, 1)) != FIELD_DECL
203 || TREE_OPERAND (counter, 1) != TREE_OPERAND (rhs, 1))
204 return HOST_WIDE_INT_M1U;
205 }
206 else if (counter != rhs)
207 return HOST_WIDE_INT_M1U;
208
209 lhs = NULL;
210 }
211
212 lhs = orig_lhs;
213 val = ret + counter_val;
214 while (lhs)
215 {
216 enum tree_code rhs_code;
217 tree rhs1;
218
219 if (si->offsets[SSA_NAME_VERSION (lhs)] != -1)
220 break;
221
222 if (val >= max_size)
223 si->offsets[SSA_NAME_VERSION (lhs)] = max_size;
224 else
225 si->offsets[SSA_NAME_VERSION (lhs)] = val;
226
227 stmt = SSA_NAME_DEF_STMT (lhs);
228
229 rhs_code = gimple_assign_rhs_code (stmt);
230 rhs1 = gimple_assign_rhs1 (stmt);
231 if ((get_gimple_rhs_class (rhs_code) == GIMPLE_SINGLE_RHS
232 || gimple_assign_cast_p (stmt))
233 && TREE_CODE (rhs1) == SSA_NAME)
234 {
235 lhs = rhs1;
236 continue;
237 }
238
239 if ((rhs_code == POINTER_PLUS_EXPR
240 || rhs_code == PLUS_EXPR)
241 && TREE_CODE (rhs1) == SSA_NAME
242 && tree_fits_uhwi_p (gimple_assign_rhs2 (stmt)))
243 {
244 val -= tree_low_cst (gimple_assign_rhs2 (stmt), 1);
245 lhs = rhs1;
246 continue;
247 }
248
249 if (rhs_code == ADDR_EXPR
250 && TREE_CODE (TREE_OPERAND (rhs1, 0)) == MEM_REF
251 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (rhs1, 0), 0)) == SSA_NAME
252 && tree_fits_uhwi_p (TREE_OPERAND (TREE_OPERAND (rhs1, 0), 1)))
253 {
254 val -= tree_low_cst (TREE_OPERAND (TREE_OPERAND (rhs1, 0), 1), 1);
255 lhs = TREE_OPERAND (TREE_OPERAND (rhs1, 0), 0);
256 continue;
257 }
258
259 lhs = NULL;
260 }
261
262 return ret;
263 }
264
265
266 /* Called by walk_tree to look for references to va_list variables. */
267
268 static tree
269 find_va_list_reference (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
270 void *data)
271 {
272 bitmap va_list_vars = (bitmap) ((struct walk_stmt_info *) data)->info;
273 tree var = *tp;
274
275 if (TREE_CODE (var) == SSA_NAME)
276 {
277 if (bitmap_bit_p (va_list_vars, SSA_NAME_VERSION (var)))
278 return var;
279 }
280 else if (TREE_CODE (var) == VAR_DECL)
281 {
282 if (bitmap_bit_p (va_list_vars, DECL_UID (var) + num_ssa_names))
283 return var;
284 }
285
286 return NULL_TREE;
287 }
288
289
290 /* Helper function of va_list_counter_struct_op. Compute
291 cfun->va_list_{g,f}pr_size. AP is a va_list GPR/FPR counter,
292 if WRITE_P is true, seen in AP = VAR, otherwise seen in VAR = AP
293 statement. GPR_P is true if AP is a GPR counter, false if it is
294 a FPR counter. */
295
296 static void
297 va_list_counter_op (struct stdarg_info *si, tree ap, tree var, bool gpr_p,
298 bool write_p)
299 {
300 unsigned HOST_WIDE_INT increment;
301
302 if (si->compute_sizes < 0)
303 {
304 si->compute_sizes = 0;
305 if (si->va_start_count == 1
306 && reachable_at_most_once (si->bb, si->va_start_bb))
307 si->compute_sizes = 1;
308
309 if (dump_file && (dump_flags & TDF_DETAILS))
310 fprintf (dump_file,
311 "bb%d will %sbe executed at most once for each va_start "
312 "in bb%d\n", si->bb->index, si->compute_sizes ? "" : "not ",
313 si->va_start_bb->index);
314 }
315
316 if (write_p
317 && si->compute_sizes
318 && (increment = va_list_counter_bump (si, ap, var, gpr_p)) + 1 > 1)
319 {
320 if (gpr_p && cfun->va_list_gpr_size + increment < VA_LIST_MAX_GPR_SIZE)
321 {
322 cfun->va_list_gpr_size += increment;
323 return;
324 }
325
326 if (!gpr_p && cfun->va_list_fpr_size + increment < VA_LIST_MAX_FPR_SIZE)
327 {
328 cfun->va_list_fpr_size += increment;
329 return;
330 }
331 }
332
333 if (write_p || !si->compute_sizes)
334 {
335 if (gpr_p)
336 cfun->va_list_gpr_size = VA_LIST_MAX_GPR_SIZE;
337 else
338 cfun->va_list_fpr_size = VA_LIST_MAX_FPR_SIZE;
339 }
340 }
341
342
343 /* If AP is a va_list GPR/FPR counter, compute cfun->va_list_{g,f}pr_size.
344 If WRITE_P is true, AP has been seen in AP = VAR assignment, if WRITE_P
345 is false, AP has been seen in VAR = AP assignment.
346 Return true if the AP = VAR (resp. VAR = AP) statement is a recognized
347 va_arg operation that doesn't cause the va_list variable to escape
348 current function. */
349
350 static bool
351 va_list_counter_struct_op (struct stdarg_info *si, tree ap, tree var,
352 bool write_p)
353 {
354 tree base;
355
356 if (TREE_CODE (ap) != COMPONENT_REF
357 || TREE_CODE (TREE_OPERAND (ap, 1)) != FIELD_DECL)
358 return false;
359
360 if (TREE_CODE (var) != SSA_NAME
361 || bitmap_bit_p (si->va_list_vars, SSA_NAME_VERSION (var)))
362 return false;
363
364 base = get_base_address (ap);
365 if (TREE_CODE (base) != VAR_DECL
366 || !bitmap_bit_p (si->va_list_vars, DECL_UID (base) + num_ssa_names))
367 return false;
368
369 if (TREE_OPERAND (ap, 1) == va_list_gpr_counter_field)
370 va_list_counter_op (si, ap, var, true, write_p);
371 else if (TREE_OPERAND (ap, 1) == va_list_fpr_counter_field)
372 va_list_counter_op (si, ap, var, false, write_p);
373
374 return true;
375 }
376
377
378 /* Check for TEM = AP. Return true if found and the caller shouldn't
379 search for va_list references in the statement. */
380
381 static bool
382 va_list_ptr_read (struct stdarg_info *si, tree ap, tree tem)
383 {
384 if (TREE_CODE (ap) != VAR_DECL
385 || !bitmap_bit_p (si->va_list_vars, DECL_UID (ap) + num_ssa_names))
386 return false;
387
388 if (TREE_CODE (tem) != SSA_NAME
389 || bitmap_bit_p (si->va_list_vars, SSA_NAME_VERSION (tem)))
390 return false;
391
392 if (si->compute_sizes < 0)
393 {
394 si->compute_sizes = 0;
395 if (si->va_start_count == 1
396 && reachable_at_most_once (si->bb, si->va_start_bb))
397 si->compute_sizes = 1;
398
399 if (dump_file && (dump_flags & TDF_DETAILS))
400 fprintf (dump_file,
401 "bb%d will %sbe executed at most once for each va_start "
402 "in bb%d\n", si->bb->index, si->compute_sizes ? "" : "not ",
403 si->va_start_bb->index);
404 }
405
406 /* For void * or char * va_list types, there is just one counter.
407 If va_arg is used in a loop, we don't know how many registers need
408 saving. */
409 if (! si->compute_sizes)
410 return false;
411
412 if (va_list_counter_bump (si, ap, tem, true) == HOST_WIDE_INT_M1U)
413 return false;
414
415 /* Note the temporary, as we need to track whether it doesn't escape
416 the current function. */
417 bitmap_set_bit (si->va_list_escape_vars, SSA_NAME_VERSION (tem));
418
419 return true;
420 }
421
422
423 /* Check for:
424 tem1 = AP;
425 TEM2 = tem1 + CST;
426 AP = TEM2;
427 sequence and update cfun->va_list_gpr_size. Return true if found. */
428
429 static bool
430 va_list_ptr_write (struct stdarg_info *si, tree ap, tree tem2)
431 {
432 unsigned HOST_WIDE_INT increment;
433
434 if (TREE_CODE (ap) != VAR_DECL
435 || !bitmap_bit_p (si->va_list_vars, DECL_UID (ap) + num_ssa_names))
436 return false;
437
438 if (TREE_CODE (tem2) != SSA_NAME
439 || bitmap_bit_p (si->va_list_vars, SSA_NAME_VERSION (tem2)))
440 return false;
441
442 if (si->compute_sizes <= 0)
443 return false;
444
445 increment = va_list_counter_bump (si, ap, tem2, true);
446 if (increment + 1 <= 1)
447 return false;
448
449 if (cfun->va_list_gpr_size + increment < VA_LIST_MAX_GPR_SIZE)
450 cfun->va_list_gpr_size += increment;
451 else
452 cfun->va_list_gpr_size = VA_LIST_MAX_GPR_SIZE;
453
454 return true;
455 }
456
457
458 /* If RHS is X, (some type *) X or X + CST for X a temporary variable
459 containing value of some va_list variable plus optionally some constant,
460 either set si->va_list_escapes or add LHS to si->va_list_escape_vars,
461 depending whether LHS is a function local temporary. */
462
463 static void
464 check_va_list_escapes (struct stdarg_info *si, tree lhs, tree rhs)
465 {
466 if (! POINTER_TYPE_P (TREE_TYPE (rhs)))
467 return;
468
469 if (TREE_CODE (rhs) == SSA_NAME)
470 {
471 if (! bitmap_bit_p (si->va_list_escape_vars, SSA_NAME_VERSION (rhs)))
472 return;
473 }
474 else if (TREE_CODE (rhs) == ADDR_EXPR
475 && TREE_CODE (TREE_OPERAND (rhs, 0)) == MEM_REF
476 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (rhs, 0), 0)) == SSA_NAME)
477 {
478 tree ptr = TREE_OPERAND (TREE_OPERAND (rhs, 0), 0);
479 if (! bitmap_bit_p (si->va_list_escape_vars, SSA_NAME_VERSION (ptr)))
480 return;
481 }
482 else
483 return;
484
485 if (TREE_CODE (lhs) != SSA_NAME)
486 {
487 si->va_list_escapes = true;
488 return;
489 }
490
491 if (si->compute_sizes < 0)
492 {
493 si->compute_sizes = 0;
494 if (si->va_start_count == 1
495 && reachable_at_most_once (si->bb, si->va_start_bb))
496 si->compute_sizes = 1;
497
498 if (dump_file && (dump_flags & TDF_DETAILS))
499 fprintf (dump_file,
500 "bb%d will %sbe executed at most once for each va_start "
501 "in bb%d\n", si->bb->index, si->compute_sizes ? "" : "not ",
502 si->va_start_bb->index);
503 }
504
505 /* For void * or char * va_list types, there is just one counter.
506 If va_arg is used in a loop, we don't know how many registers need
507 saving. */
508 if (! si->compute_sizes)
509 {
510 si->va_list_escapes = true;
511 return;
512 }
513
514 if (va_list_counter_bump (si, si->va_start_ap, lhs, true)
515 == HOST_WIDE_INT_M1U)
516 {
517 si->va_list_escapes = true;
518 return;
519 }
520
521 bitmap_set_bit (si->va_list_escape_vars, SSA_NAME_VERSION (lhs));
522 }
523
524
525 /* Check all uses of temporaries from si->va_list_escape_vars bitmap.
526 Return true if va_list might be escaping. */
527
528 static bool
529 check_all_va_list_escapes (struct stdarg_info *si)
530 {
531 basic_block bb;
532
533 FOR_EACH_BB (bb)
534 {
535 gimple_stmt_iterator i;
536
537 for (i = gsi_start_phis (bb); !gsi_end_p (i); gsi_next (&i))
538 {
539 tree lhs;
540 use_operand_p uop;
541 ssa_op_iter soi;
542 gimple phi = gsi_stmt (i);
543
544 lhs = PHI_RESULT (phi);
545 if (virtual_operand_p (lhs)
546 || bitmap_bit_p (si->va_list_escape_vars,
547 SSA_NAME_VERSION (lhs)))
548 continue;
549
550 FOR_EACH_PHI_ARG (uop, phi, soi, SSA_OP_USE)
551 {
552 tree rhs = USE_FROM_PTR (uop);
553 if (TREE_CODE (rhs) == SSA_NAME
554 && bitmap_bit_p (si->va_list_escape_vars,
555 SSA_NAME_VERSION (rhs)))
556 {
557 if (dump_file && (dump_flags & TDF_DETAILS))
558 {
559 fputs ("va_list escapes in ", dump_file);
560 print_gimple_stmt (dump_file, phi, 0, dump_flags);
561 fputc ('\n', dump_file);
562 }
563 return true;
564 }
565 }
566 }
567
568 for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
569 {
570 gimple stmt = gsi_stmt (i);
571 tree use;
572 ssa_op_iter iter;
573
574 if (is_gimple_debug (stmt))
575 continue;
576
577 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_ALL_USES)
578 {
579 if (! bitmap_bit_p (si->va_list_escape_vars,
580 SSA_NAME_VERSION (use)))
581 continue;
582
583 if (is_gimple_assign (stmt))
584 {
585 tree rhs = gimple_assign_rhs1 (stmt);
586 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
587
588 /* x = *ap_temp; */
589 if (rhs_code == MEM_REF
590 && TREE_OPERAND (rhs, 0) == use
591 && TYPE_SIZE_UNIT (TREE_TYPE (rhs))
592 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (rhs)))
593 && si->offsets[SSA_NAME_VERSION (use)] != -1)
594 {
595 unsigned HOST_WIDE_INT gpr_size;
596 tree access_size = TYPE_SIZE_UNIT (TREE_TYPE (rhs));
597
598 gpr_size = si->offsets[SSA_NAME_VERSION (use)]
599 + tree_to_shwi (TREE_OPERAND (rhs, 1))
600 + tree_low_cst (access_size, 1);
601 if (gpr_size >= VA_LIST_MAX_GPR_SIZE)
602 cfun->va_list_gpr_size = VA_LIST_MAX_GPR_SIZE;
603 else if (gpr_size > cfun->va_list_gpr_size)
604 cfun->va_list_gpr_size = gpr_size;
605 continue;
606 }
607
608 /* va_arg sequences may contain
609 other_ap_temp = ap_temp;
610 other_ap_temp = ap_temp + constant;
611 other_ap_temp = (some_type *) ap_temp;
612 ap = ap_temp;
613 statements. */
614 if (rhs == use
615 && ((rhs_code == POINTER_PLUS_EXPR
616 && (TREE_CODE (gimple_assign_rhs2 (stmt))
617 == INTEGER_CST))
618 || gimple_assign_cast_p (stmt)
619 || (get_gimple_rhs_class (rhs_code)
620 == GIMPLE_SINGLE_RHS)))
621 {
622 tree lhs = gimple_assign_lhs (stmt);
623
624 if (TREE_CODE (lhs) == SSA_NAME
625 && bitmap_bit_p (si->va_list_escape_vars,
626 SSA_NAME_VERSION (lhs)))
627 continue;
628
629 if (TREE_CODE (lhs) == VAR_DECL
630 && bitmap_bit_p (si->va_list_vars,
631 DECL_UID (lhs) + num_ssa_names))
632 continue;
633 }
634 else if (rhs_code == ADDR_EXPR
635 && TREE_CODE (TREE_OPERAND (rhs, 0)) == MEM_REF
636 && TREE_OPERAND (TREE_OPERAND (rhs, 0), 0) == use)
637 {
638 tree lhs = gimple_assign_lhs (stmt);
639
640 if (bitmap_bit_p (si->va_list_escape_vars,
641 SSA_NAME_VERSION (lhs)))
642 continue;
643 }
644 }
645
646 if (dump_file && (dump_flags & TDF_DETAILS))
647 {
648 fputs ("va_list escapes in ", dump_file);
649 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
650 fputc ('\n', dump_file);
651 }
652 return true;
653 }
654 }
655 }
656
657 return false;
658 }
659
660
661 /* Return true if this optimization pass should be done.
662 It makes only sense for stdarg functions. */
663
664 static bool
665 gate_optimize_stdarg (void)
666 {
667 /* This optimization is only for stdarg functions. */
668 return cfun->stdarg != 0;
669 }
670
671
672 /* Entry point to the stdarg optimization pass. */
673
674 static unsigned int
675 execute_optimize_stdarg (void)
676 {
677 basic_block bb;
678 bool va_list_escapes = false;
679 bool va_list_simple_ptr;
680 struct stdarg_info si;
681 struct walk_stmt_info wi;
682 const char *funcname = NULL;
683 tree cfun_va_list;
684
685 cfun->va_list_gpr_size = 0;
686 cfun->va_list_fpr_size = 0;
687 memset (&si, 0, sizeof (si));
688 si.va_list_vars = BITMAP_ALLOC (NULL);
689 si.va_list_escape_vars = BITMAP_ALLOC (NULL);
690
691 if (dump_file)
692 funcname = lang_hooks.decl_printable_name (current_function_decl, 2);
693
694 cfun_va_list = targetm.fn_abi_va_list (cfun->decl);
695 va_list_simple_ptr = POINTER_TYPE_P (cfun_va_list)
696 && (TREE_TYPE (cfun_va_list) == void_type_node
697 || TREE_TYPE (cfun_va_list) == char_type_node);
698 gcc_assert (is_gimple_reg_type (cfun_va_list) == va_list_simple_ptr);
699
700 FOR_EACH_BB (bb)
701 {
702 gimple_stmt_iterator i;
703
704 for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
705 {
706 gimple stmt = gsi_stmt (i);
707 tree callee, ap;
708
709 if (!is_gimple_call (stmt))
710 continue;
711
712 callee = gimple_call_fndecl (stmt);
713 if (!callee
714 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL)
715 continue;
716
717 switch (DECL_FUNCTION_CODE (callee))
718 {
719 case BUILT_IN_VA_START:
720 break;
721 /* If old style builtins are used, don't optimize anything. */
722 case BUILT_IN_SAVEREGS:
723 case BUILT_IN_NEXT_ARG:
724 va_list_escapes = true;
725 continue;
726 default:
727 continue;
728 }
729
730 si.va_start_count++;
731 ap = gimple_call_arg (stmt, 0);
732
733 if (TREE_CODE (ap) != ADDR_EXPR)
734 {
735 va_list_escapes = true;
736 break;
737 }
738 ap = TREE_OPERAND (ap, 0);
739 if (TREE_CODE (ap) == ARRAY_REF)
740 {
741 if (! integer_zerop (TREE_OPERAND (ap, 1)))
742 {
743 va_list_escapes = true;
744 break;
745 }
746 ap = TREE_OPERAND (ap, 0);
747 }
748 if (TYPE_MAIN_VARIANT (TREE_TYPE (ap))
749 != TYPE_MAIN_VARIANT (targetm.fn_abi_va_list (cfun->decl))
750 || TREE_CODE (ap) != VAR_DECL)
751 {
752 va_list_escapes = true;
753 break;
754 }
755
756 if (is_global_var (ap))
757 {
758 va_list_escapes = true;
759 break;
760 }
761
762 bitmap_set_bit (si.va_list_vars, DECL_UID (ap) + num_ssa_names);
763
764 /* VA_START_BB and VA_START_AP will be only used if there is just
765 one va_start in the function. */
766 si.va_start_bb = bb;
767 si.va_start_ap = ap;
768 }
769
770 if (va_list_escapes)
771 break;
772 }
773
774 /* If there were no va_start uses in the function, there is no need to
775 save anything. */
776 if (si.va_start_count == 0)
777 goto finish;
778
779 /* If some va_list arguments weren't local, we can't optimize. */
780 if (va_list_escapes)
781 goto finish;
782
783 /* For void * or char * va_list, something useful can be done only
784 if there is just one va_start. */
785 if (va_list_simple_ptr && si.va_start_count > 1)
786 {
787 va_list_escapes = true;
788 goto finish;
789 }
790
791 /* For struct * va_list, if the backend didn't tell us what the counter fields
792 are, there is nothing more we can do. */
793 if (!va_list_simple_ptr
794 && va_list_gpr_counter_field == NULL_TREE
795 && va_list_fpr_counter_field == NULL_TREE)
796 {
797 va_list_escapes = true;
798 goto finish;
799 }
800
801 /* For void * or char * va_list there is just one counter
802 (va_list itself). Use VA_LIST_GPR_SIZE for it. */
803 if (va_list_simple_ptr)
804 cfun->va_list_fpr_size = VA_LIST_MAX_FPR_SIZE;
805
806 calculate_dominance_info (CDI_DOMINATORS);
807 memset (&wi, 0, sizeof (wi));
808 wi.info = si.va_list_vars;
809
810 FOR_EACH_BB (bb)
811 {
812 gimple_stmt_iterator i;
813
814 si.compute_sizes = -1;
815 si.bb = bb;
816
817 /* For va_list_simple_ptr, we have to check PHI nodes too. We treat
818 them as assignments for the purpose of escape analysis. This is
819 not needed for non-simple va_list because virtual phis don't perform
820 any real data movement. */
821 if (va_list_simple_ptr)
822 {
823 tree lhs, rhs;
824 use_operand_p uop;
825 ssa_op_iter soi;
826
827 for (i = gsi_start_phis (bb); !gsi_end_p (i); gsi_next (&i))
828 {
829 gimple phi = gsi_stmt (i);
830 lhs = PHI_RESULT (phi);
831
832 if (virtual_operand_p (lhs))
833 continue;
834
835 FOR_EACH_PHI_ARG (uop, phi, soi, SSA_OP_USE)
836 {
837 rhs = USE_FROM_PTR (uop);
838 if (va_list_ptr_read (&si, rhs, lhs))
839 continue;
840 else if (va_list_ptr_write (&si, lhs, rhs))
841 continue;
842 else
843 check_va_list_escapes (&si, lhs, rhs);
844
845 if (si.va_list_escapes)
846 {
847 if (dump_file && (dump_flags & TDF_DETAILS))
848 {
849 fputs ("va_list escapes in ", dump_file);
850 print_gimple_stmt (dump_file, phi, 0, dump_flags);
851 fputc ('\n', dump_file);
852 }
853 va_list_escapes = true;
854 }
855 }
856 }
857 }
858
859 for (i = gsi_start_bb (bb);
860 !gsi_end_p (i) && !va_list_escapes;
861 gsi_next (&i))
862 {
863 gimple stmt = gsi_stmt (i);
864
865 /* Don't look at __builtin_va_{start,end}, they are ok. */
866 if (is_gimple_call (stmt))
867 {
868 tree callee = gimple_call_fndecl (stmt);
869
870 if (callee
871 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL
872 && (DECL_FUNCTION_CODE (callee) == BUILT_IN_VA_START
873 || DECL_FUNCTION_CODE (callee) == BUILT_IN_VA_END))
874 continue;
875 }
876
877 if (is_gimple_assign (stmt))
878 {
879 tree lhs = gimple_assign_lhs (stmt);
880 tree rhs = gimple_assign_rhs1 (stmt);
881
882 if (va_list_simple_ptr)
883 {
884 if (get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
885 == GIMPLE_SINGLE_RHS)
886 {
887 /* Check for ap ={v} {}. */
888 if (TREE_CLOBBER_P (rhs))
889 continue;
890
891 /* Check for tem = ap. */
892 else if (va_list_ptr_read (&si, rhs, lhs))
893 continue;
894
895 /* Check for the last insn in:
896 tem1 = ap;
897 tem2 = tem1 + CST;
898 ap = tem2;
899 sequence. */
900 else if (va_list_ptr_write (&si, lhs, rhs))
901 continue;
902 }
903
904 if ((gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
905 && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST)
906 || CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
907 || (get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
908 == GIMPLE_SINGLE_RHS))
909 check_va_list_escapes (&si, lhs, rhs);
910 }
911 else
912 {
913 if (get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
914 == GIMPLE_SINGLE_RHS)
915 {
916 /* Check for ap ={v} {}. */
917 if (TREE_CLOBBER_P (rhs))
918 continue;
919
920 /* Check for ap[0].field = temp. */
921 else if (va_list_counter_struct_op (&si, lhs, rhs, true))
922 continue;
923
924 /* Check for temp = ap[0].field. */
925 else if (va_list_counter_struct_op (&si, rhs, lhs,
926 false))
927 continue;
928 }
929
930 /* Do any architecture specific checking. */
931 if (targetm.stdarg_optimize_hook
932 && targetm.stdarg_optimize_hook (&si, stmt))
933 continue;
934 }
935 }
936 else if (is_gimple_debug (stmt))
937 continue;
938
939 /* All other uses of va_list are either va_copy (that is not handled
940 in this optimization), taking address of va_list variable or
941 passing va_list to other functions (in that case va_list might
942 escape the function and therefore va_start needs to set it up
943 fully), or some unexpected use of va_list. None of these should
944 happen in a gimplified VA_ARG_EXPR. */
945 if (si.va_list_escapes
946 || walk_gimple_op (stmt, find_va_list_reference, &wi))
947 {
948 if (dump_file && (dump_flags & TDF_DETAILS))
949 {
950 fputs ("va_list escapes in ", dump_file);
951 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
952 fputc ('\n', dump_file);
953 }
954 va_list_escapes = true;
955 }
956 }
957
958 if (va_list_escapes)
959 break;
960 }
961
962 if (! va_list_escapes
963 && va_list_simple_ptr
964 && ! bitmap_empty_p (si.va_list_escape_vars)
965 && check_all_va_list_escapes (&si))
966 va_list_escapes = true;
967
968 finish:
969 if (va_list_escapes)
970 {
971 cfun->va_list_gpr_size = VA_LIST_MAX_GPR_SIZE;
972 cfun->va_list_fpr_size = VA_LIST_MAX_FPR_SIZE;
973 }
974 BITMAP_FREE (si.va_list_vars);
975 BITMAP_FREE (si.va_list_escape_vars);
976 free (si.offsets);
977 if (dump_file)
978 {
979 fprintf (dump_file, "%s: va_list escapes %d, needs to save ",
980 funcname, (int) va_list_escapes);
981 if (cfun->va_list_gpr_size >= VA_LIST_MAX_GPR_SIZE)
982 fputs ("all", dump_file);
983 else
984 fprintf (dump_file, "%d", cfun->va_list_gpr_size);
985 fputs (" GPR units and ", dump_file);
986 if (cfun->va_list_fpr_size >= VA_LIST_MAX_FPR_SIZE)
987 fputs ("all", dump_file);
988 else
989 fprintf (dump_file, "%d", cfun->va_list_fpr_size);
990 fputs (" FPR units.\n", dump_file);
991 }
992 return 0;
993 }
994
995
996 namespace {
997
998 const pass_data pass_data_stdarg =
999 {
1000 GIMPLE_PASS, /* type */
1001 "stdarg", /* name */
1002 OPTGROUP_NONE, /* optinfo_flags */
1003 true, /* has_gate */
1004 true, /* has_execute */
1005 TV_NONE, /* tv_id */
1006 ( PROP_cfg | PROP_ssa ), /* properties_required */
1007 0, /* properties_provided */
1008 0, /* properties_destroyed */
1009 0, /* todo_flags_start */
1010 0, /* todo_flags_finish */
1011 };
1012
1013 class pass_stdarg : public gimple_opt_pass
1014 {
1015 public:
1016 pass_stdarg (gcc::context *ctxt)
1017 : gimple_opt_pass (pass_data_stdarg, ctxt)
1018 {}
1019
1020 /* opt_pass methods: */
1021 bool gate () { return gate_optimize_stdarg (); }
1022 unsigned int execute () { return execute_optimize_stdarg (); }
1023
1024 }; // class pass_stdarg
1025
1026 } // anon namespace
1027
1028 gimple_opt_pass *
1029 make_pass_stdarg (gcc::context *ctxt)
1030 {
1031 return new pass_stdarg (ctxt);
1032 }