tree-stdarg.c 26.7 KB
Newer Older
1
/* Pass computing data for optimizing stdarg functions.
2
   Copyright (C) 2004-2014 Free Software Foundation, Inc.
3 4 5 6 7 8
   Contributed by Jakub Jelinek <jakub@redhat.com>

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify
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 11 12 13 14 15 16 17
any later version.

GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
18 19
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */
20 21 22 23 24 25 26 27

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "tree.h"
#include "function.h"
#include "langhooks.h"
28
#include "gimple-pretty-print.h"
29
#include "target.h"
30
#include "bitmap.h"
31 32 33 34 35
#include "basic-block.h"
#include "tree-ssa-alias.h"
#include "internal-fn.h"
#include "gimple-expr.h"
#include "is-a.h"
36
#include "gimple.h"
37 38
#include "gimple-iterator.h"
#include "gimple-walk.h"
39 40 41
#include "gimple-ssa.h"
#include "tree-phinodes.h"
#include "ssa-iterators.h"
42
#include "stringpool.h"
43
#include "tree-ssanames.h"
44
#include "sbitmap.h"
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#include "tree-pass.h"
#include "tree-stdarg.h"

/* A simple pass that attempts to optimize stdarg functions on architectures
   that need to save register arguments to stack on entry to stdarg functions.
   If the function doesn't use any va_start macros, no registers need to
   be saved.  If va_start macros are used, the va_list variables don't escape
   the function, it is only necessary to save registers that will be used
   in va_arg macros.  E.g. if va_arg is only used with integral types
   in the function, floating point registers don't need to be saved, etc.  */


/* Return true if basic block VA_ARG_BB is dominated by VA_START_BB and
   is executed at most as many times as VA_START_BB.  */

static bool
reachable_at_most_once (basic_block va_arg_bb, basic_block va_start_bb)
{
63
  vec<edge> stack = vNULL;
64
  edge e;
65 66 67 68 69 70 71 72 73 74
  edge_iterator ei;
  sbitmap visited;
  bool ret;

  if (va_arg_bb == va_start_bb)
    return true;

  if (! dominated_by_p (CDI_DOMINATORS, va_arg_bb, va_start_bb))
    return false;

75
  visited = sbitmap_alloc (last_basic_block_for_fn (cfun));
76
  bitmap_clear (visited);
77 78 79
  ret = true;

  FOR_EACH_EDGE (e, ei, va_arg_bb->preds)
80
    stack.safe_push (e);
81

82
  while (! stack.is_empty ())
83 84 85
    {
      basic_block src;

86
      e = stack.pop ();
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
      src = e->src;

      if (e->flags & EDGE_COMPLEX)
	{
	  ret = false;
	  break;
	}

      if (src == va_start_bb)
	continue;

      /* va_arg_bb can be executed more times than va_start_bb.  */
      if (src == va_arg_bb)
	{
	  ret = false;
	  break;
	}

105
      gcc_assert (src != ENTRY_BLOCK_PTR_FOR_FN (cfun));
106

107
      if (! bitmap_bit_p (visited, src->index))
108
	{
109
	  bitmap_set_bit (visited, src->index);
110
	  FOR_EACH_EDGE (e, ei, src->preds)
111
	    stack.safe_push (e);
112 113 114
	}
    }

115
  stack.release ();
116 117 118 119 120 121
  sbitmap_free (visited);
  return ret;
}


/* For statement COUNTER = RHS, if RHS is COUNTER + constant,
122
   return constant, otherwise return HOST_WIDE_INT_M1U.
123 124 125 126 127 128
   GPR_P is true if this is GPR counter.  */

static unsigned HOST_WIDE_INT
va_list_counter_bump (struct stdarg_info *si, tree counter, tree rhs,
		      bool gpr_p)
{
129 130
  tree lhs, orig_lhs;
  gimple stmt;
131 132 133 134 135 136 137
  unsigned HOST_WIDE_INT ret = 0, val, counter_val;
  unsigned int max_size;

  if (si->offsets == NULL)
    {
      unsigned int i;

138
      si->offsets = XNEWVEC (int, num_ssa_names);
139 140 141 142 143 144 145 146 147
      for (i = 0; i < num_ssa_names; ++i)
	si->offsets[i] = -1;
    }

  counter_val = gpr_p ? cfun->va_list_gpr_size : cfun->va_list_fpr_size;
  max_size = gpr_p ? VA_LIST_MAX_GPR_SIZE : VA_LIST_MAX_FPR_SIZE;
  orig_lhs = lhs = rhs;
  while (lhs)
    {
148
      enum tree_code rhs_code;
149
      tree rhs1;
150

151 152 153 154 155 156 157 158 159 160 161 162 163 164
      if (si->offsets[SSA_NAME_VERSION (lhs)] != -1)
	{
	  if (counter_val >= max_size)
	    {
	      ret = max_size;
	      break;
	    }

	  ret -= counter_val - si->offsets[SSA_NAME_VERSION (lhs)];
	  break;
	}

      stmt = SSA_NAME_DEF_STMT (lhs);

165
      if (!is_gimple_assign (stmt) || gimple_assign_lhs (stmt) != lhs)
166
	return HOST_WIDE_INT_M1U;
167

168
      rhs_code = gimple_assign_rhs_code (stmt);
169
      rhs1 = gimple_assign_rhs1 (stmt);
170 171
      if ((get_gimple_rhs_class (rhs_code) == GIMPLE_SINGLE_RHS
	   || gimple_assign_cast_p (stmt))
172
	  && TREE_CODE (rhs1) == SSA_NAME)
173
	{
174
	  lhs = rhs1;
175 176 177
	  continue;
	}

178 179
      if ((rhs_code == POINTER_PLUS_EXPR
	   || rhs_code == PLUS_EXPR)
180
	  && TREE_CODE (rhs1) == SSA_NAME
181
	  && tree_fits_uhwi_p (gimple_assign_rhs2 (stmt)))
182
	{
183
	  ret += tree_to_uhwi (gimple_assign_rhs2 (stmt));
184 185 186 187 188 189 190
	  lhs = rhs1;
	  continue;
	}

      if (rhs_code == ADDR_EXPR 
	  && TREE_CODE (TREE_OPERAND (rhs1, 0)) == MEM_REF
	  && TREE_CODE (TREE_OPERAND (TREE_OPERAND (rhs1, 0), 0)) == SSA_NAME
191
	  && tree_fits_uhwi_p (TREE_OPERAND (TREE_OPERAND (rhs1, 0), 1)))
192
	{
193
	  ret += tree_to_uhwi (TREE_OPERAND (TREE_OPERAND (rhs1, 0), 1));
194
	  lhs = TREE_OPERAND (TREE_OPERAND (rhs1, 0), 0);
195 196 197
	  continue;
	}

198
      if (get_gimple_rhs_class (rhs_code) != GIMPLE_SINGLE_RHS)
199
	return HOST_WIDE_INT_M1U;
200

201
      rhs = gimple_assign_rhs1 (stmt);
202
      if (TREE_CODE (counter) != TREE_CODE (rhs))
203
	return HOST_WIDE_INT_M1U;
204 205 206 207 208 209

      if (TREE_CODE (counter) == COMPONENT_REF)
	{
	  if (get_base_address (counter) != get_base_address (rhs)
	      || TREE_CODE (TREE_OPERAND (rhs, 1)) != FIELD_DECL
	      || TREE_OPERAND (counter, 1) != TREE_OPERAND (rhs, 1))
210
	    return HOST_WIDE_INT_M1U;
211 212
	}
      else if (counter != rhs)
213
	return HOST_WIDE_INT_M1U;
214 215 216 217 218 219 220 221

      lhs = NULL;
    }

  lhs = orig_lhs;
  val = ret + counter_val;
  while (lhs)
    {
222
      enum tree_code rhs_code;
223
      tree rhs1;
224

225 226 227 228 229 230 231 232 233 234
      if (si->offsets[SSA_NAME_VERSION (lhs)] != -1)
	break;

      if (val >= max_size)
	si->offsets[SSA_NAME_VERSION (lhs)] = max_size;
      else
	si->offsets[SSA_NAME_VERSION (lhs)] = val;

      stmt = SSA_NAME_DEF_STMT (lhs);

235
      rhs_code = gimple_assign_rhs_code (stmt);
236
      rhs1 = gimple_assign_rhs1 (stmt);
237 238
      if ((get_gimple_rhs_class (rhs_code) == GIMPLE_SINGLE_RHS
	   || gimple_assign_cast_p (stmt))
239
	  && TREE_CODE (rhs1) == SSA_NAME)
240
	{
241
	  lhs = rhs1;
242 243 244
	  continue;
	}

245 246
      if ((rhs_code == POINTER_PLUS_EXPR
	   || rhs_code == PLUS_EXPR)
247
	  && TREE_CODE (rhs1) == SSA_NAME
248
	  && tree_fits_uhwi_p (gimple_assign_rhs2 (stmt)))
249
	{
250
	  val -= tree_to_uhwi (gimple_assign_rhs2 (stmt));
251 252 253 254 255 256 257
	  lhs = rhs1;
	  continue;
	}

      if (rhs_code == ADDR_EXPR 
	  && TREE_CODE (TREE_OPERAND (rhs1, 0)) == MEM_REF
	  && TREE_CODE (TREE_OPERAND (TREE_OPERAND (rhs1, 0), 0)) == SSA_NAME
258
	  && tree_fits_uhwi_p (TREE_OPERAND (TREE_OPERAND (rhs1, 0), 1)))
259
	{
260
	  val -= tree_to_uhwi (TREE_OPERAND (TREE_OPERAND (rhs1, 0), 1));
261
	  lhs = TREE_OPERAND (TREE_OPERAND (rhs1, 0), 0);
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
	  continue;
	}

      lhs = NULL;
    }

  return ret;
}


/* Called by walk_tree to look for references to va_list variables.  */

static tree
find_va_list_reference (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
			void *data)
{
278
  bitmap va_list_vars = (bitmap) ((struct walk_stmt_info *) data)->info;
279 280 281
  tree var = *tp;

  if (TREE_CODE (var) == SSA_NAME)
282 283 284 285 286 287 288 289 290
    {
      if (bitmap_bit_p (va_list_vars, SSA_NAME_VERSION (var)))
	return var;
    }
  else if (TREE_CODE (var) == VAR_DECL)
    {
      if (bitmap_bit_p (va_list_vars, DECL_UID (var) + num_ssa_names))
	return var;
    }
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366

  return NULL_TREE;
}


/* Helper function of va_list_counter_struct_op.  Compute
   cfun->va_list_{g,f}pr_size.  AP is a va_list GPR/FPR counter,
   if WRITE_P is true, seen in AP = VAR, otherwise seen in VAR = AP
   statement.  GPR_P is true if AP is a GPR counter, false if it is
   a FPR counter.  */

static void
va_list_counter_op (struct stdarg_info *si, tree ap, tree var, bool gpr_p,
		    bool write_p)
{
  unsigned HOST_WIDE_INT increment;

  if (si->compute_sizes < 0)
    {
      si->compute_sizes = 0;
      if (si->va_start_count == 1
	  && reachable_at_most_once (si->bb, si->va_start_bb))
	si->compute_sizes = 1;

      if (dump_file && (dump_flags & TDF_DETAILS))
	fprintf (dump_file,
		 "bb%d will %sbe executed at most once for each va_start "
		 "in bb%d\n", si->bb->index, si->compute_sizes ? "" : "not ",
		 si->va_start_bb->index);
    }

  if (write_p
      && si->compute_sizes
      && (increment = va_list_counter_bump (si, ap, var, gpr_p)) + 1 > 1)
    {
      if (gpr_p && cfun->va_list_gpr_size + increment < VA_LIST_MAX_GPR_SIZE)
	{
	  cfun->va_list_gpr_size += increment;
	  return;
	}

      if (!gpr_p && cfun->va_list_fpr_size + increment < VA_LIST_MAX_FPR_SIZE)
	{
	  cfun->va_list_fpr_size += increment;
	  return;
	}
    }

  if (write_p || !si->compute_sizes)
    {
      if (gpr_p)
	cfun->va_list_gpr_size = VA_LIST_MAX_GPR_SIZE;
      else
	cfun->va_list_fpr_size = VA_LIST_MAX_FPR_SIZE;
    }
}


/* If AP is a va_list GPR/FPR counter, compute cfun->va_list_{g,f}pr_size.
   If WRITE_P is true, AP has been seen in AP = VAR assignment, if WRITE_P
   is false, AP has been seen in VAR = AP assignment.
   Return true if the AP = VAR (resp. VAR = AP) statement is a recognized
   va_arg operation that doesn't cause the va_list variable to escape
   current function.  */

static bool
va_list_counter_struct_op (struct stdarg_info *si, tree ap, tree var,
			   bool write_p)
{
  tree base;

  if (TREE_CODE (ap) != COMPONENT_REF
      || TREE_CODE (TREE_OPERAND (ap, 1)) != FIELD_DECL)
    return false;

  if (TREE_CODE (var) != SSA_NAME
367
      || bitmap_bit_p (si->va_list_vars, SSA_NAME_VERSION (var)))
368 369 370 371
    return false;

  base = get_base_address (ap);
  if (TREE_CODE (base) != VAR_DECL
372
      || !bitmap_bit_p (si->va_list_vars, DECL_UID (base) + num_ssa_names))
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
    return false;

  if (TREE_OPERAND (ap, 1) == va_list_gpr_counter_field)
    va_list_counter_op (si, ap, var, true, write_p);
  else if (TREE_OPERAND (ap, 1) == va_list_fpr_counter_field)
    va_list_counter_op (si, ap, var, false, write_p);

  return true;
}


/* Check for TEM = AP.  Return true if found and the caller shouldn't
   search for va_list references in the statement.  */

static bool
va_list_ptr_read (struct stdarg_info *si, tree ap, tree tem)
{
  if (TREE_CODE (ap) != VAR_DECL
391
      || !bitmap_bit_p (si->va_list_vars, DECL_UID (ap) + num_ssa_names))
392 393 394
    return false;

  if (TREE_CODE (tem) != SSA_NAME
395
      || bitmap_bit_p (si->va_list_vars, SSA_NAME_VERSION (tem)))
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
    return false;

  if (si->compute_sizes < 0)
    {
      si->compute_sizes = 0;
      if (si->va_start_count == 1
	  && reachable_at_most_once (si->bb, si->va_start_bb))
	si->compute_sizes = 1;

      if (dump_file && (dump_flags & TDF_DETAILS))
	fprintf (dump_file,
		 "bb%d will %sbe executed at most once for each va_start "
		 "in bb%d\n", si->bb->index, si->compute_sizes ? "" : "not ",
		 si->va_start_bb->index);
    }

  /* For void * or char * va_list types, there is just one counter.
     If va_arg is used in a loop, we don't know how many registers need
     saving.  */
  if (! si->compute_sizes)
    return false;

418
  if (va_list_counter_bump (si, ap, tem, true) == HOST_WIDE_INT_M1U)
419 420 421 422
    return false;

  /* Note the temporary, as we need to track whether it doesn't escape
     the current function.  */
423 424
  bitmap_set_bit (si->va_list_escape_vars, SSA_NAME_VERSION (tem));

425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
  return true;
}


/* Check for:
     tem1 = AP;
     TEM2 = tem1 + CST;
     AP = TEM2;
   sequence and update cfun->va_list_gpr_size.  Return true if found.  */

static bool
va_list_ptr_write (struct stdarg_info *si, tree ap, tree tem2)
{
  unsigned HOST_WIDE_INT increment;

  if (TREE_CODE (ap) != VAR_DECL
441
      || !bitmap_bit_p (si->va_list_vars, DECL_UID (ap) + num_ssa_names))
442 443 444
    return false;

  if (TREE_CODE (tem2) != SSA_NAME
445
      || bitmap_bit_p (si->va_list_vars, SSA_NAME_VERSION (tem2)))
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
    return false;

  if (si->compute_sizes <= 0)
    return false;

  increment = va_list_counter_bump (si, ap, tem2, true);
  if (increment + 1 <= 1)
    return false;

  if (cfun->va_list_gpr_size + increment < VA_LIST_MAX_GPR_SIZE)
    cfun->va_list_gpr_size += increment;
  else
    cfun->va_list_gpr_size = VA_LIST_MAX_GPR_SIZE;

  return true;
}


/* If RHS is X, (some type *) X or X + CST for X a temporary variable
   containing value of some va_list variable plus optionally some constant,
   either set si->va_list_escapes or add LHS to si->va_list_escape_vars,
   depending whether LHS is a function local temporary.  */

static void
check_va_list_escapes (struct stdarg_info *si, tree lhs, tree rhs)
{
  if (! POINTER_TYPE_P (TREE_TYPE (rhs)))
    return;

475 476
  if (TREE_CODE (rhs) == SSA_NAME)
    {
477
      if (! bitmap_bit_p (si->va_list_escape_vars, SSA_NAME_VERSION (rhs)))
478 479 480 481 482 483
	return;
    }
  else if (TREE_CODE (rhs) == ADDR_EXPR
	   && TREE_CODE (TREE_OPERAND (rhs, 0)) == MEM_REF
	   && TREE_CODE (TREE_OPERAND (TREE_OPERAND (rhs, 0), 0)) == SSA_NAME)
    {
484 485
      tree ptr = TREE_OPERAND (TREE_OPERAND (rhs, 0), 0);
      if (! bitmap_bit_p (si->va_list_escape_vars, SSA_NAME_VERSION (ptr)))
486 487 488
	return;
    }
  else
489 490
    return;

491
  if (TREE_CODE (lhs) != SSA_NAME)
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
    {
      si->va_list_escapes = true;
      return;
    }

  if (si->compute_sizes < 0)
    {
      si->compute_sizes = 0;
      if (si->va_start_count == 1
	  && reachable_at_most_once (si->bb, si->va_start_bb))
	si->compute_sizes = 1;

      if (dump_file && (dump_flags & TDF_DETAILS))
	fprintf (dump_file,
		 "bb%d will %sbe executed at most once for each va_start "
		 "in bb%d\n", si->bb->index, si->compute_sizes ? "" : "not ",
		 si->va_start_bb->index);
    }

  /* For void * or char * va_list types, there is just one counter.
     If va_arg is used in a loop, we don't know how many registers need
     saving.  */
  if (! si->compute_sizes)
    {
      si->va_list_escapes = true;
      return;
    }

  if (va_list_counter_bump (si, si->va_start_ap, lhs, true)
521
      == HOST_WIDE_INT_M1U)
522 523 524 525 526
    {
      si->va_list_escapes = true;
      return;
    }

527
  bitmap_set_bit (si->va_list_escape_vars, SSA_NAME_VERSION (lhs));
528 529 530 531 532 533 534 535 536 537 538
}


/* Check all uses of temporaries from si->va_list_escape_vars bitmap.
   Return true if va_list might be escaping.  */

static bool
check_all_va_list_escapes (struct stdarg_info *si)
{
  basic_block bb;

539
  FOR_EACH_BB_FN (bb, cfun)
540
    {
541
      gimple_stmt_iterator i;
542

543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
      for (i = gsi_start_phis (bb); !gsi_end_p (i); gsi_next (&i))
	{
	  tree lhs;
	  use_operand_p uop;
	  ssa_op_iter soi;
	  gimple phi = gsi_stmt (i);

	  lhs = PHI_RESULT (phi);
	  if (virtual_operand_p (lhs)
	      || bitmap_bit_p (si->va_list_escape_vars,
			       SSA_NAME_VERSION (lhs)))
	    continue;

	  FOR_EACH_PHI_ARG (uop, phi, soi, SSA_OP_USE)
	    {
	      tree rhs = USE_FROM_PTR (uop);
	      if (TREE_CODE (rhs) == SSA_NAME
		  && bitmap_bit_p (si->va_list_escape_vars,
				SSA_NAME_VERSION (rhs)))
		{
		  if (dump_file && (dump_flags & TDF_DETAILS))
		    {
		      fputs ("va_list escapes in ", dump_file);
		      print_gimple_stmt (dump_file, phi, 0, dump_flags);
		      fputc ('\n', dump_file);
		    }
		  return true;
		}
	    }
	}

574
      for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
575
	{
576 577
	  gimple stmt = gsi_stmt (i);
	  tree use;
578 579
	  ssa_op_iter iter;

580 581 582
	  if (is_gimple_debug (stmt))
	    continue;

583 584 585
	  FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_ALL_USES)
	    {
	      if (! bitmap_bit_p (si->va_list_escape_vars,
586
				  SSA_NAME_VERSION (use)))
587 588
		continue;

589
	      if (is_gimple_assign (stmt))
590
		{
591 592
		  tree rhs = gimple_assign_rhs1 (stmt);
		  enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
593 594

		  /* x = *ap_temp;  */
595
		  if (rhs_code == MEM_REF
596 597
		      && TREE_OPERAND (rhs, 0) == use
		      && TYPE_SIZE_UNIT (TREE_TYPE (rhs))
598
		      && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (rhs)))
599 600 601 602 603 604
		      && si->offsets[SSA_NAME_VERSION (use)] != -1)
		    {
		      unsigned HOST_WIDE_INT gpr_size;
		      tree access_size = TYPE_SIZE_UNIT (TREE_TYPE (rhs));

		      gpr_size = si->offsets[SSA_NAME_VERSION (use)]
605
			  	 + tree_to_shwi (TREE_OPERAND (rhs, 1))
606
				 + tree_to_uhwi (access_size);
607 608 609 610 611 612 613 614 615 616 617 618 619
		      if (gpr_size >= VA_LIST_MAX_GPR_SIZE)
			cfun->va_list_gpr_size = VA_LIST_MAX_GPR_SIZE;
		      else if (gpr_size > cfun->va_list_gpr_size)
			cfun->va_list_gpr_size = gpr_size;
		      continue;
		    }

		  /* va_arg sequences may contain
		     other_ap_temp = ap_temp;
		     other_ap_temp = ap_temp + constant;
		     other_ap_temp = (some_type *) ap_temp;
		     ap = ap_temp;
		     statements.  */
620 621 622 623 624 625 626
		  if (rhs == use
		      && ((rhs_code == POINTER_PLUS_EXPR
			   && (TREE_CODE (gimple_assign_rhs2 (stmt))
			       == INTEGER_CST))
			  || gimple_assign_cast_p (stmt)
			  || (get_gimple_rhs_class (rhs_code)
			      == GIMPLE_SINGLE_RHS)))
627
		    {
628 629
		      tree lhs = gimple_assign_lhs (stmt);

630 631
		      if (TREE_CODE (lhs) == SSA_NAME
			  && bitmap_bit_p (si->va_list_escape_vars,
632
					   SSA_NAME_VERSION (lhs)))
633 634 635 636
			continue;

		      if (TREE_CODE (lhs) == VAR_DECL
			  && bitmap_bit_p (si->va_list_vars,
637
					   DECL_UID (lhs) + num_ssa_names))
638 639
			continue;
		    }
640 641 642 643 644 645 646
		  else if (rhs_code == ADDR_EXPR
			   && TREE_CODE (TREE_OPERAND (rhs, 0)) == MEM_REF
			   && TREE_OPERAND (TREE_OPERAND (rhs, 0), 0) == use)
		    {
		      tree lhs = gimple_assign_lhs (stmt);

		      if (bitmap_bit_p (si->va_list_escape_vars,
647
					SSA_NAME_VERSION (lhs)))
648 649
			continue;
		    }
650 651 652 653 654
		}

	      if (dump_file && (dump_flags & TDF_DETAILS))
		{
		  fputs ("va_list escapes in ", dump_file);
655
		  print_gimple_stmt (dump_file, stmt, 0, dump_flags);
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
		  fputc ('\n', dump_file);
		}
	      return true;
	    }
	}
    }

  return false;
}


/* Return true if this optimization pass should be done.
   It makes only sense for stdarg functions.  */

static bool
gate_optimize_stdarg (void)
{
673 674
  /* This optimization is only for stdarg functions.  */
  return cfun->stdarg != 0;
675 676 677 678 679
}


/* Entry point to the stdarg optimization pass.  */

680
static unsigned int
681 682 683 684 685 686
execute_optimize_stdarg (void)
{
  basic_block bb;
  bool va_list_escapes = false;
  bool va_list_simple_ptr;
  struct stdarg_info si;
687
  struct walk_stmt_info wi;
688
  const char *funcname = NULL;
689
  tree cfun_va_list;
690 691 692 693 694 695 696 697 698 699

  cfun->va_list_gpr_size = 0;
  cfun->va_list_fpr_size = 0;
  memset (&si, 0, sizeof (si));
  si.va_list_vars = BITMAP_ALLOC (NULL);
  si.va_list_escape_vars = BITMAP_ALLOC (NULL);

  if (dump_file)
    funcname = lang_hooks.decl_printable_name (current_function_decl, 2);

700 701 702 703 704
  cfun_va_list = targetm.fn_abi_va_list (cfun->decl);
  va_list_simple_ptr = POINTER_TYPE_P (cfun_va_list)
		       && (TREE_TYPE (cfun_va_list) == void_type_node
			   || TREE_TYPE (cfun_va_list) == char_type_node);
  gcc_assert (is_gimple_reg_type (cfun_va_list) == va_list_simple_ptr);
705

706
  FOR_EACH_BB_FN (bb, cfun)
707
    {
708
      gimple_stmt_iterator i;
709

710
      for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
711
	{
712 713
	  gimple stmt = gsi_stmt (i);
	  tree callee, ap;
714

715
	  if (!is_gimple_call (stmt))
716 717
	    continue;

718
	  callee = gimple_call_fndecl (stmt);
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
	  if (!callee
	      || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL)
	    continue;

	  switch (DECL_FUNCTION_CODE (callee))
	    {
	    case BUILT_IN_VA_START:
	      break;
	      /* If old style builtins are used, don't optimize anything.  */
	    case BUILT_IN_SAVEREGS:
	    case BUILT_IN_NEXT_ARG:
	      va_list_escapes = true;
	      continue;
	    default:
	      continue;
	    }

	  si.va_start_count++;
737
	  ap = gimple_call_arg (stmt, 0);
738 739

	  if (TREE_CODE (ap) != ADDR_EXPR)
740 741 742 743 744
	    {
	      va_list_escapes = true;
	      break;
	    }
	  ap = TREE_OPERAND (ap, 0);
745 746 747 748 749 750 751 752 753 754
	  if (TREE_CODE (ap) == ARRAY_REF)
	    {
	      if (! integer_zerop (TREE_OPERAND (ap, 1)))
	        {
	          va_list_escapes = true;
	          break;
		}
	      ap = TREE_OPERAND (ap, 0);
	    }
	  if (TYPE_MAIN_VARIANT (TREE_TYPE (ap))
755
	      != TYPE_MAIN_VARIANT (targetm.fn_abi_va_list (cfun->decl))
756 757 758 759 760 761
	      || TREE_CODE (ap) != VAR_DECL)
	    {
	      va_list_escapes = true;
	      break;
	    }

762 763 764 765 766 767
	  if (is_global_var (ap))
	    {
	      va_list_escapes = true;
	      break;
	    }

768
	  bitmap_set_bit (si.va_list_vars, DECL_UID (ap) + num_ssa_names);
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811

	  /* VA_START_BB and VA_START_AP will be only used if there is just
	     one va_start in the function.  */
	  si.va_start_bb = bb;
	  si.va_start_ap = ap;
	}

      if (va_list_escapes)
	break;
    }

  /* If there were no va_start uses in the function, there is no need to
     save anything.  */
  if (si.va_start_count == 0)
    goto finish;

  /* If some va_list arguments weren't local, we can't optimize.  */
  if (va_list_escapes)
    goto finish;

  /* For void * or char * va_list, something useful can be done only
     if there is just one va_start.  */
  if (va_list_simple_ptr && si.va_start_count > 1)
    {
      va_list_escapes = true;
      goto finish;
    }

  /* For struct * va_list, if the backend didn't tell us what the counter fields
     are, there is nothing more we can do.  */
  if (!va_list_simple_ptr
      && va_list_gpr_counter_field == NULL_TREE
      && va_list_fpr_counter_field == NULL_TREE)
    {
      va_list_escapes = true;
      goto finish;
    }

  /* For void * or char * va_list there is just one counter
     (va_list itself).  Use VA_LIST_GPR_SIZE for it.  */
  if (va_list_simple_ptr)
    cfun->va_list_fpr_size = VA_LIST_MAX_FPR_SIZE;

812
  calculate_dominance_info (CDI_DOMINATORS);
813 814
  memset (&wi, 0, sizeof (wi));
  wi.info = si.va_list_vars;
815

816
  FOR_EACH_BB_FN (bb, cfun)
817
    {
818
      gimple_stmt_iterator i;
819 820 821

      si.compute_sizes = -1;
      si.bb = bb;
822 823 824 825 826 827 828

      /* For va_list_simple_ptr, we have to check PHI nodes too.  We treat
	 them as assignments for the purpose of escape analysis.  This is
	 not needed for non-simple va_list because virtual phis don't perform
	 any real data movement.  */
      if (va_list_simple_ptr)
	{
829
	  tree lhs, rhs;
830 831 832
	  use_operand_p uop;
	  ssa_op_iter soi;

833
	  for (i = gsi_start_phis (bb); !gsi_end_p (i); gsi_next (&i))
834
	    {
835
	      gimple phi = gsi_stmt (i);
836 837
	      lhs = PHI_RESULT (phi);

838
	      if (virtual_operand_p (lhs))
839 840 841 842 843 844 845 846 847 848 849 850
		continue;

	      FOR_EACH_PHI_ARG (uop, phi, soi, SSA_OP_USE)
		{
		  rhs = USE_FROM_PTR (uop);
		  if (va_list_ptr_read (&si, rhs, lhs))
		    continue;
		  else if (va_list_ptr_write (&si, lhs, rhs))
		    continue;
		  else
		    check_va_list_escapes (&si, lhs, rhs);

851
		  if (si.va_list_escapes)
852 853 854 855
		    {
		      if (dump_file && (dump_flags & TDF_DETAILS))
			{
			  fputs ("va_list escapes in ", dump_file);
856
			  print_gimple_stmt (dump_file, phi, 0, dump_flags);
857 858 859 860 861 862 863 864
			  fputc ('\n', dump_file);
			}
		      va_list_escapes = true;
		    }
		}
	    }
	}

865 866 867
      for (i = gsi_start_bb (bb);
	   !gsi_end_p (i) && !va_list_escapes;
	   gsi_next (&i))
868
	{
869
	  gimple stmt = gsi_stmt (i);
870 871

	  /* Don't look at __builtin_va_{start,end}, they are ok.  */
872
	  if (is_gimple_call (stmt))
873
	    {
874
	      tree callee = gimple_call_fndecl (stmt);
875 876 877 878 879 880 881 882

	      if (callee
		  && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL
		  && (DECL_FUNCTION_CODE (callee) == BUILT_IN_VA_START
		      || DECL_FUNCTION_CODE (callee) == BUILT_IN_VA_END))
		continue;
	    }

883
	  if (is_gimple_assign (stmt))
884
	    {
885 886
	      tree lhs = gimple_assign_lhs (stmt);
	      tree rhs = gimple_assign_rhs1 (stmt);
887 888 889

	      if (va_list_simple_ptr)
		{
890 891 892
		  if (get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
		      == GIMPLE_SINGLE_RHS)
		    {
893 894 895 896
		      /* Check for ap ={v} {}.  */
		      if (TREE_CLOBBER_P (rhs))
			continue;

897
		      /* Check for tem = ap.  */
898
		      else if (va_list_ptr_read (&si, rhs, lhs))
899
			continue;
900

901 902 903 904 905 906 907 908
		      /* Check for the last insn in:
			 tem1 = ap;
			 tem2 = tem1 + CST;
			 ap = tem2;
			 sequence.  */
		      else if (va_list_ptr_write (&si, lhs, rhs))
			continue;
		    }
909

910 911
		  if ((gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
		       && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST)
912
		      || CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
913 914
		      || (get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
			  == GIMPLE_SINGLE_RHS))
915 916 917 918
		    check_va_list_escapes (&si, lhs, rhs);
		}
	      else
		{
919 920 921
		  if (get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
		      == GIMPLE_SINGLE_RHS)
		    {
922 923 924
		      /* Check for ap ={v} {}.  */
		      if (TREE_CLOBBER_P (rhs))
			continue;
925

926
		      /* Check for ap[0].field = temp.  */
927
		      else if (va_list_counter_struct_op (&si, lhs, rhs, true))
928
			continue;
929

930 931 932 933 934
		      /* Check for temp = ap[0].field.  */
		      else if (va_list_counter_struct_op (&si, rhs, lhs,
							  false))
			continue;
		    }
935 936

		  /* Do any architecture specific checking.  */
937 938
		  if (targetm.stdarg_optimize_hook
		      && targetm.stdarg_optimize_hook (&si, stmt))
939 940 941
		    continue;
		}
	    }
942 943
	  else if (is_gimple_debug (stmt))
	    continue;
944 945 946 947 948 949 950 951

	  /* All other uses of va_list are either va_copy (that is not handled
	     in this optimization), taking address of va_list variable or
	     passing va_list to other functions (in that case va_list might
	     escape the function and therefore va_start needs to set it up
	     fully), or some unexpected use of va_list.  None of these should
	     happen in a gimplified VA_ARG_EXPR.  */
	  if (si.va_list_escapes
952
	      || walk_gimple_op (stmt, find_va_list_reference, &wi))
953 954 955 956
	    {
	      if (dump_file && (dump_flags & TDF_DETAILS))
		{
		  fputs ("va_list escapes in ", dump_file);
957
		  print_gimple_stmt (dump_file, stmt, 0, dump_flags);
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
		  fputc ('\n', dump_file);
		}
	      va_list_escapes = true;
	    }
	}

      if (va_list_escapes)
	break;
    }

  if (! va_list_escapes
      && va_list_simple_ptr
      && ! bitmap_empty_p (si.va_list_escape_vars)
      && check_all_va_list_escapes (&si))
    va_list_escapes = true;

finish:
  if (va_list_escapes)
    {
      cfun->va_list_gpr_size = VA_LIST_MAX_GPR_SIZE;
      cfun->va_list_fpr_size = VA_LIST_MAX_FPR_SIZE;
    }
  BITMAP_FREE (si.va_list_vars);
  BITMAP_FREE (si.va_list_escape_vars);
  free (si.offsets);
  if (dump_file)
    {
      fprintf (dump_file, "%s: va_list escapes %d, needs to save ",
	       funcname, (int) va_list_escapes);
      if (cfun->va_list_gpr_size >= VA_LIST_MAX_GPR_SIZE)
	fputs ("all", dump_file);
      else
	fprintf (dump_file, "%d", cfun->va_list_gpr_size);
      fputs (" GPR units and ", dump_file);
      if (cfun->va_list_fpr_size >= VA_LIST_MAX_FPR_SIZE)
	fputs ("all", dump_file);
      else
	fprintf (dump_file, "%d", cfun->va_list_fpr_size);
      fputs (" FPR units.\n", dump_file);
    }
998
  return 0;
999 1000 1001
}


1002 1003 1004
namespace {

const pass_data pass_data_stdarg =
1005
{
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
  GIMPLE_PASS, /* type */
  "stdarg", /* name */
  OPTGROUP_NONE, /* optinfo_flags */
  true, /* has_gate */
  true, /* has_execute */
  TV_NONE, /* tv_id */
  ( PROP_cfg | PROP_ssa ), /* properties_required */
  0, /* properties_provided */
  0, /* properties_destroyed */
  0, /* todo_flags_start */
  0, /* todo_flags_finish */
1017
};
1018 1019 1020 1021

class pass_stdarg : public gimple_opt_pass
{
public:
1022 1023
  pass_stdarg (gcc::context *ctxt)
    : gimple_opt_pass (pass_data_stdarg, ctxt)
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
  {}

  /* opt_pass methods: */
  bool gate () { return gate_optimize_stdarg (); }
  unsigned int execute () { return execute_optimize_stdarg (); }

}; // class pass_stdarg

} // anon namespace

gimple_opt_pass *
make_pass_stdarg (gcc::context *ctxt)
{
  return new pass_stdarg (ctxt);
}