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