tree-vectorizer.c 20 KB
Newer Older
1
/* Vectorizer
Jakub Jelinek committed
2
   Copyright (C) 2003-2015 Free Software Foundation, Inc.
H.J. Lu committed
3
   Contributed by Dorit Naishlos <dorit@il.ibm.com>
4 5 6 7 8

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 the Free
9
Software Foundation; either version 3, or (at your option) any later
10 11 12 13 14 15 16 17
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
/* Loop and basic block vectorizer.
22

H.J. Lu committed
23 24
  This file contains drivers for the three vectorizers:
  (1) loop vectorizer (inter-iteration parallelism),
25 26 27
  (2) loop-aware SLP (intra-iteration parallelism) (invoked by the loop
      vectorizer)
  (3) BB vectorizer (out-of-loops), aka SLP
H.J. Lu committed
28

29
  The rest of the vectorizer's code is organized as follows:
H.J. Lu committed
30 31 32 33 34
  - tree-vect-loop.c - loop specific parts such as reductions, etc. These are
    used by drivers (1) and (2).
  - tree-vect-loop-manip.c - vectorizer's loop control-flow utilities, used by
    drivers (1) and (2).
  - tree-vect-slp.c - BB vectorization specific analysis and transformation,
35 36
    used by drivers (2) and (3).
  - tree-vect-stmts.c - statements analysis and transformation (used by all).
H.J. Lu committed
37
  - tree-vect-data-refs.c - vectorizer specific data-refs analysis and
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    manipulations (used by all).
  - tree-vect-patterns.c - vectorizable code patterns detector (used by all)

  Here's a poor attempt at illustrating that:

     tree-vectorizer.c:
     loop_vect()  loop_aware_slp()  slp_vect()
          |        /           \          /
          |       /             \        /
          tree-vect-loop.c  tree-vect-slp.c
                | \      \  /      /   |
                |  \      \/      /    |
                |   \     /\     /     |
                |    \   /  \   /      |
         tree-vect-stmts.c  tree-vect-data-refs.c
                       \      /
                    tree-vect-patterns.c
*/
Dorit Nuzman committed
56

57 58 59
#include "config.h"
#include "system.h"
#include "coretypes.h"
60
#include "dumpfile.h"
61
#include "tm.h"
62 63 64 65 66 67 68 69 70
#include "hash-set.h"
#include "machmode.h"
#include "vec.h"
#include "double-int.h"
#include "input.h"
#include "alias.h"
#include "symtab.h"
#include "wide-int.h"
#include "inchash.h"
71
#include "tree.h"
72
#include "fold-const.h"
73
#include "stor-layout.h"
74
#include "tree-pretty-print.h"
75 76 77 78 79 80
#include "predict.h"
#include "hard-reg-set.h"
#include "input.h"
#include "function.h"
#include "dominance.h"
#include "cfg.h"
81 82 83 84 85
#include "basic-block.h"
#include "tree-ssa-alias.h"
#include "internal-fn.h"
#include "gimple-expr.h"
#include "is-a.h"
86
#include "gimple.h"
87 88
#include "gimple-iterator.h"
#include "gimple-walk.h"
89
#include "gimple-ssa.h"
Andrew MacLeod committed
90 91 92
#include "hash-map.h"
#include "plugin-api.h"
#include "ipa-ref.h"
93 94 95
#include "cgraph.h"
#include "tree-phinodes.h"
#include "ssa-iterators.h"
96
#include "tree-ssa-loop-manip.h"
97
#include "tree-cfg.h"
98 99 100
#include "cfgloop.h"
#include "tree-vectorizer.h"
#include "tree-pass.h"
101
#include "tree-ssa-propagate.h"
102
#include "dbgcnt.h"
103
#include "gimple-fold.h"
104 105
#include "tree-scalar-evolution.h"

Dorit Nuzman committed
106

107
/* Loop or bb location.  */
108
source_location vect_location;
109

110
/* Vector mapping GIMPLE stmt to stmt_vec_info. */
111
vec<vec_void_p> stmt_vec_info_vec;
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

/* For mapping simduid to vectorization factor.  */

struct simduid_to_vf : typed_free_remove<simduid_to_vf>
{
  unsigned int simduid;
  int vf;

  /* hash_table support.  */
  typedef simduid_to_vf value_type;
  typedef simduid_to_vf compare_type;
  static inline hashval_t hash (const value_type *);
  static inline int equal (const value_type *, const compare_type *);
};

inline hashval_t
simduid_to_vf::hash (const value_type *p)
{
  return p->simduid;
}

inline int
simduid_to_vf::equal (const value_type *p1, const value_type *p2)
{
  return p1->simduid == p2->simduid;
}

/* This hash maps the OMP simd array to the corresponding simduid used
   to index into it.  Like thus,

        _7 = GOMP_SIMD_LANE (simduid.0)
        ...
        ...
        D.1737[_7] = stuff;


Jakub Jelinek committed
148 149
   This hash maps from the OMP simd array (D.1737[]) to DECL_UID of
   simduid.0.  */
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

struct simd_array_to_simduid : typed_free_remove<simd_array_to_simduid>
{
  tree decl;
  unsigned int simduid;

  /* hash_table support.  */
  typedef simd_array_to_simduid value_type;
  typedef simd_array_to_simduid compare_type;
  static inline hashval_t hash (const value_type *);
  static inline int equal (const value_type *, const compare_type *);
};

inline hashval_t
simd_array_to_simduid::hash (const value_type *p)
{
  return DECL_UID (p->decl);
}

inline int
simd_array_to_simduid::equal (const value_type *p1, const value_type *p2)
{
  return p1->decl == p2->decl;
}

/* Fold IFN_GOMP_SIMD_LANE, IFN_GOMP_SIMD_VF and IFN_GOMP_SIMD_LAST_LANE
   into their corresponding constants.  */

static void
179
adjust_simduid_builtins (hash_table<simduid_to_vf> **htab)
180 181 182
{
  basic_block bb;

183
  FOR_EACH_BB_FN (bb, cfun)
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    {
      gimple_stmt_iterator i;

      for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
	{
	  unsigned int vf = 1;
	  enum internal_fn ifn;
	  gimple stmt = gsi_stmt (i);
	  tree t;
	  if (!is_gimple_call (stmt)
	      || !gimple_call_internal_p (stmt))
	    continue;
	  ifn = gimple_call_internal_fn (stmt);
	  switch (ifn)
	    {
	    case IFN_GOMP_SIMD_LANE:
	    case IFN_GOMP_SIMD_VF:
	    case IFN_GOMP_SIMD_LAST_LANE:
	      break;
	    default:
	      continue;
	    }
	  tree arg = gimple_call_arg (stmt, 0);
	  gcc_assert (arg != NULL_TREE);
	  gcc_assert (TREE_CODE (arg) == SSA_NAME);
	  simduid_to_vf *p = NULL, data;
	  data.simduid = DECL_UID (SSA_NAME_VAR (arg));
211 212
	  if (*htab)
	    p = (*htab)->find (&data);
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
	  if (p)
	    vf = p->vf;
	  switch (ifn)
	    {
	    case IFN_GOMP_SIMD_VF:
	      t = build_int_cst (unsigned_type_node, vf);
	      break;
	    case IFN_GOMP_SIMD_LANE:
	      t = build_int_cst (unsigned_type_node, 0);
	      break;
	    case IFN_GOMP_SIMD_LAST_LANE:
	      t = gimple_call_arg (stmt, 1);
	      break;
	    default:
	      gcc_unreachable ();
	    }
	  update_call_from_tree (&i, t);
	}
    }
}
Dorit Nuzman committed
233

234 235 236 237
/* Helper structure for note_simd_array_uses.  */

struct note_simd_array_uses_struct
{
238
  hash_table<simd_array_to_simduid> **htab;
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
  unsigned int simduid;
};

/* Callback for note_simd_array_uses, called through walk_gimple_op.  */

static tree
note_simd_array_uses_cb (tree *tp, int *walk_subtrees, void *data)
{
  struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
  struct note_simd_array_uses_struct *ns
    = (struct note_simd_array_uses_struct *) wi->info;

  if (TYPE_P (*tp))
    *walk_subtrees = 0;
  else if (VAR_P (*tp)
	   && lookup_attribute ("omp simd array", DECL_ATTRIBUTES (*tp))
	   && DECL_CONTEXT (*tp) == current_function_decl)
    {
      simd_array_to_simduid data;
258 259
      if (!*ns->htab)
	*ns->htab = new hash_table<simd_array_to_simduid> (15);
260 261
      data.decl = *tp;
      data.simduid = ns->simduid;
262
      simd_array_to_simduid **slot = (*ns->htab)->find_slot (&data, INSERT);
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
      if (*slot == NULL)
	{
	  simd_array_to_simduid *p = XNEW (simd_array_to_simduid);
	  *p = data;
	  *slot = p;
	}
      else if ((*slot)->simduid != ns->simduid)
	(*slot)->simduid = -1U;
      *walk_subtrees = 0;
    }
  return NULL_TREE;
}

/* Find "omp simd array" temporaries and map them to corresponding
   simduid.  */

static void
280
note_simd_array_uses (hash_table<simd_array_to_simduid> **htab)
281 282 283 284 285 286 287 288 289 290
{
  basic_block bb;
  gimple_stmt_iterator gsi;
  struct walk_stmt_info wi;
  struct note_simd_array_uses_struct ns;

  memset (&wi, 0, sizeof (wi));
  wi.info = &ns;
  ns.htab = htab;

291
  FOR_EACH_BB_FN (bb, cfun)
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
    for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
      {
	gimple stmt = gsi_stmt (gsi);
	if (!is_gimple_call (stmt) || !gimple_call_internal_p (stmt))
	  continue;
	switch (gimple_call_internal_fn (stmt))
	  {
	  case IFN_GOMP_SIMD_LANE:
	  case IFN_GOMP_SIMD_VF:
	  case IFN_GOMP_SIMD_LAST_LANE:
	    break;
	  default:
	    continue;
	  }
	tree lhs = gimple_call_lhs (stmt);
	if (lhs == NULL_TREE)
	  continue;
	imm_use_iterator use_iter;
	gimple use_stmt;
	ns.simduid = DECL_UID (SSA_NAME_VAR (gimple_call_arg (stmt, 0)));
	FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, lhs)
	  if (!is_gimple_debug (use_stmt))
	    walk_gimple_op (use_stmt, note_simd_array_uses_cb, &wi);
      }
}
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
/* A helper function to free data refs.  */

void
vect_destroy_datarefs (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo)
{
  vec<data_reference_p> datarefs;
  struct data_reference *dr;
  unsigned int i;

 if (loop_vinfo)
    datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
  else
    datarefs = BB_VINFO_DATAREFS (bb_vinfo);

  FOR_EACH_VEC_ELT (datarefs, i, dr)
    if (dr->aux)
      {
        free (dr->aux);
        dr->aux = NULL;
      }

  free_data_refs (datarefs);
}


343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
/* If LOOP has been versioned during ifcvt, return the internal call
   guarding it.  */

static gimple
vect_loop_vectorized_call (struct loop *loop)
{
  basic_block bb = loop_preheader_edge (loop)->src;
  gimple g;
  do
    {
      g = last_stmt (bb);
      if (g)
	break;
      if (!single_pred_p (bb))
	break;
      bb = single_pred (bb);
    }
  while (1);
  if (g && gimple_code (g) == GIMPLE_COND)
    {
      gimple_stmt_iterator gsi = gsi_for_stmt (g);
      gsi_prev (&gsi);
      if (!gsi_end_p (gsi))
	{
	  g = gsi_stmt (gsi);
	  if (is_gimple_call (g)
	      && gimple_call_internal_p (g)
	      && gimple_call_internal_fn (g) == IFN_LOOP_VECTORIZED
	      && (tree_to_shwi (gimple_call_arg (g, 0)) == loop->num
		  || tree_to_shwi (gimple_call_arg (g, 1)) == loop->num))
	    return g;
	}
    }
  return NULL;
}

/* Fold LOOP_VECTORIZED internal call G to VALUE and
   update any immediate uses of it's LHS.  */

static void
fold_loop_vectorized_call (gimple g, tree value)
{
  tree lhs = gimple_call_lhs (g);
  use_operand_p use_p;
  imm_use_iterator iter;
  gimple use_stmt;
  gimple_stmt_iterator gsi = gsi_for_stmt (g);

  update_call_from_tree (&gsi, value);
  FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
393 394 395 396 397
    {
      FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
	SET_USE (use_p, value);
      update_stmt (use_stmt);
    }
398 399
}

400
/* Function vectorize_loops.
H.J. Lu committed
401

402
   Entry point to loop vectorization phase.  */
403

404
unsigned
405
vectorize_loops (void)
406
{
407
  unsigned int i;
408
  unsigned int num_vectorized_loops = 0;
409 410
  unsigned int vect_loops_num;
  struct loop *loop;
411 412
  hash_table<simduid_to_vf> *simduid_to_vf_htab = NULL;
  hash_table<simd_array_to_simduid> *simd_array_to_simduid_htab = NULL;
413 414
  bool any_ifcvt_loops = false;
  unsigned ret = 0;
415

416
  vect_loops_num = number_of_loops (cfun);
417 418 419

  /* Bail out if there are no loops.  */
  if (vect_loops_num <= 1)
420 421
    {
      if (cfun->has_simduid_loops)
422
	adjust_simduid_builtins (&simduid_to_vf_htab);
423 424 425 426 427
      return 0;
    }

  if (cfun->has_simduid_loops)
    note_simd_array_uses (&simd_array_to_simduid_htab);
428

429 430
  init_stmt_vec_info_vec ();

431 432
  /*  ----------- Analyze loops. -----------  */

H.J. Lu committed
433
  /* If some loop was duplicated, it gets bigger number
434
     than all previously defined loops.  This fact allows us to run
435
     only over initial loops skipping newly generated ones.  */
436
  FOR_EACH_LOOP (loop, 0)
437 438 439 440
    if (loop->dont_vectorize)
      any_ifcvt_loops = true;
    else if ((flag_tree_loop_vectorize
	      && optimize_loop_nest_for_speed_p (loop))
441
	     || loop->force_vectorize)
442 443
      {
	loop_vec_info loop_vinfo;
444
	vect_location = find_loop_location (loop);
445
        if (LOCATION_LOCUS (vect_location) != UNKNOWN_LOCATION
446
	    && dump_enabled_p ())
447
	  dump_printf (MSG_NOTE, "\nAnalyzing loop at %s:%d\n",
448 449
                       LOCATION_FILE (vect_location),
		       LOCATION_LINE (vect_location));
450

451 452
	loop_vinfo = vect_analyze_loop (loop);
	loop->aux = loop_vinfo;
453

454 455
	if (!loop_vinfo || !LOOP_VINFO_VECTORIZABLE_P (loop_vinfo))
	  continue;
456

457 458 459
        if (!dbg_cnt (vect_loop))
	  break;

460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
	gimple loop_vectorized_call = vect_loop_vectorized_call (loop);
	if (loop_vectorized_call)
	  {
	    tree arg = gimple_call_arg (loop_vectorized_call, 1);
	    basic_block *bbs;
	    unsigned int i;
	    struct loop *scalar_loop = get_loop (cfun, tree_to_shwi (arg));

	    LOOP_VINFO_SCALAR_LOOP (loop_vinfo) = scalar_loop;
	    gcc_checking_assert (vect_loop_vectorized_call
					(LOOP_VINFO_SCALAR_LOOP (loop_vinfo))
				 == loop_vectorized_call);
	    bbs = get_loop_body (scalar_loop);
	    for (i = 0; i < scalar_loop->num_nodes; i++)
	      {
		basic_block bb = bbs[i];
		gimple_stmt_iterator gsi;
		for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
		     gsi_next (&gsi))
		  {
		    gimple phi = gsi_stmt (gsi);
		    gimple_set_uid (phi, 0);
		  }
		for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
		     gsi_next (&gsi))
		  {
		    gimple stmt = gsi_stmt (gsi);
		    gimple_set_uid (stmt, 0);
		  }
	      }
	    free (bbs);
	  }

493
        if (LOCATION_LOCUS (vect_location) != UNKNOWN_LOCATION
494
	    && dump_enabled_p ())
495
          dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
496
                           "loop vectorized\n");
497 498
	vect_transform_loop (loop_vinfo);
	num_vectorized_loops++;
499 500
	/* Now that the loop has been vectorized, allow it to be unrolled
	   etc.  */
501
	loop->force_vectorize = false;
502 503 504 505

	if (loop->simduid)
	  {
	    simduid_to_vf *simduid_to_vf_data = XNEW (simduid_to_vf);
506 507
	    if (!simduid_to_vf_htab)
	      simduid_to_vf_htab = new hash_table<simduid_to_vf> (15);
508 509
	    simduid_to_vf_data->simduid = DECL_UID (loop->simduid);
	    simduid_to_vf_data->vf = loop_vinfo->vectorization_factor;
510
	    *simduid_to_vf_htab->find_slot (simduid_to_vf_data, INSERT)
511 512
	      = simduid_to_vf_data;
	  }
513 514 515 516 517 518

	if (loop_vectorized_call)
	  {
	    fold_loop_vectorized_call (loop_vectorized_call, boolean_true_node);
	    ret |= TODO_cleanup_cfg;
	  }
519
      }
520

521
  vect_location = UNKNOWN_LOCATION;
522

523
  statistics_counter_event (cfun, "Vectorized loops", num_vectorized_loops);
524 525
  if (dump_enabled_p ()
      || (num_vectorized_loops > 0 && dump_enabled_p ()))
526
    dump_printf_loc (MSG_NOTE, vect_location,
527 528
                     "vectorized %u loops in function.\n",
                     num_vectorized_loops);
529 530 531

  /*  ----------- Finalize. -----------  */

532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
  if (any_ifcvt_loops)
    for (i = 1; i < vect_loops_num; i++)
      {
	loop = get_loop (cfun, i);
	if (loop && loop->dont_vectorize)
	  {
	    gimple g = vect_loop_vectorized_call (loop);
	    if (g)
	      {
		fold_loop_vectorized_call (g, boolean_false_node);
		ret |= TODO_cleanup_cfg;
	      }
	  }
      }

547
  for (i = 1; i < vect_loops_num; i++)
548
    {
549 550
      loop_vec_info loop_vinfo;

551
      loop = get_loop (cfun, i);
552
      if (!loop)
553
	continue;
554
      loop_vinfo = (loop_vec_info) loop->aux;
555
      destroy_loop_vec_info (loop_vinfo, true);
556 557
      loop->aux = NULL;
    }
558

559 560
  free_stmt_vec_info_vec ();

561 562
  /* Fold IFN_GOMP_SIMD_{VF,LANE,LAST_LANE} builtins.  */
  if (cfun->has_simduid_loops)
563
    adjust_simduid_builtins (&simduid_to_vf_htab);
564 565 566

  /* Shrink any "omp array simd" temporary arrays to the
     actual vectorization factors.  */
567
  if (simd_array_to_simduid_htab)
568
    {
569 570 571
      for (hash_table<simd_array_to_simduid>::iterator iter
	   = simd_array_to_simduid_htab->begin ();
	   iter != simd_array_to_simduid_htab->end (); ++iter)
572
	if ((*iter)->simduid != -1U)
573
	  {
574
	    tree decl = (*iter)->decl;
575
	    int vf = 1;
576
	    if (simduid_to_vf_htab)
577 578
	      {
		simduid_to_vf *p = NULL, data;
579
		data.simduid = (*iter)->simduid;
580
		p = simduid_to_vf_htab->find (&data);
581 582 583 584 585 586 587 588 589
		if (p)
		  vf = p->vf;
	      }
	    tree atype
	      = build_array_type_nelts (TREE_TYPE (TREE_TYPE (decl)), vf);
	    TREE_TYPE (decl) = atype;
	    relayout_decl (decl);
	  }

590
      delete simd_array_to_simduid_htab;
591
    }
592 593
    delete simduid_to_vf_htab;
    simduid_to_vf_htab = NULL;
594

595 596 597 598 599 600 601 602 603
  if (num_vectorized_loops > 0)
    {
      /* If we vectorized any loop only virtual SSA form needs to be updated.
	 ???  Also while we try hard to update loop-closed SSA form we fail
	 to properly do this in some corner-cases (see PR56286).  */
      rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa_only_virtuals);
      return TODO_cleanup_cfg;
    }

604
  return ret;
605
}
H.J. Lu committed
606

607

608 609
/*  Entry point to basic block SLP phase.  */

610 611 612
namespace {

const pass_data pass_data_slp_vectorize =
613
{
614 615 616 617 618 619 620 621
  GIMPLE_PASS, /* type */
  "slp", /* name */
  OPTGROUP_LOOP | OPTGROUP_VEC, /* optinfo_flags */
  TV_TREE_SLP_VECTORIZATION, /* tv_id */
  ( PROP_ssa | PROP_cfg ), /* properties_required */
  0, /* properties_provided */
  0, /* properties_destroyed */
  0, /* todo_flags_start */
622
  TODO_update_ssa, /* todo_flags_finish */
623 624
};

625 626 627
class pass_slp_vectorize : public gimple_opt_pass
{
public:
628 629
  pass_slp_vectorize (gcc::context *ctxt)
    : gimple_opt_pass (pass_data_slp_vectorize, ctxt)
630 631 632
  {}

  /* opt_pass methods: */
633
  opt_pass * clone () { return new pass_slp_vectorize (m_ctxt); }
634
  virtual bool gate (function *) { return flag_tree_slp_vectorize != 0; }
635
  virtual unsigned int execute (function *);
636 637 638

}; // class pass_slp_vectorize

639 640 641 642 643
unsigned int
pass_slp_vectorize::execute (function *fun)
{
  basic_block bb;

644 645 646 647 648 649 650
  bool in_loop_pipeline = scev_initialized_p ();
  if (!in_loop_pipeline)
    {
      loop_optimizer_init (LOOPS_NORMAL);
      scev_initialize ();
    }

651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
  init_stmt_vec_info_vec ();

  FOR_EACH_BB_FN (bb, fun)
    {
      vect_location = find_bb_location (bb);

      if (vect_slp_analyze_bb (bb))
        {
          if (!dbg_cnt (vect_slp))
            break;

          vect_slp_transform_bb (bb);
          if (dump_enabled_p ())
            dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
			     "basic block vectorized\n");
        }
    }

  free_stmt_vec_info_vec ();
670 671 672 673 674 675 676

  if (!in_loop_pipeline)
    {
      scev_finalize ();
      loop_optimizer_finalize ();
    }

677 678 679
  return 0;
}

680 681 682 683 684 685 686 687
} // anon namespace

gimple_opt_pass *
make_pass_slp_vectorize (gcc::context *ctxt)
{
  return new pass_slp_vectorize (ctxt);
}

688

689 690 691 692 693 694 695 696 697 698
/* Increase alignment of global arrays to improve vectorization potential.
   TODO:
   - Consider also structs that have an array field.
   - Use ipa analysis to prune arrays that can't be vectorized?
     This should involve global alignment analysis and in the future also
     array padding.  */

static unsigned int
increase_alignment (void)
{
699
  varpool_node *vnode;
700

701
  vect_location = UNKNOWN_LOCATION;
702

703
  /* Increase the alignment of all global arrays for vectorization.  */
704
  FOR_EACH_DEFINED_VARIABLE (vnode)
705
    {
706
      tree vectype, decl = vnode->decl;
707
      tree t;
708 709
      unsigned int alignment;

710
      t = TREE_TYPE (decl);
711
      if (TREE_CODE (t) != ARRAY_TYPE)
712
        continue;
713
      vectype = get_vectype_for_scalar_type (strip_array_types (t));
714
      if (!vectype)
715
        continue;
716 717
      alignment = TYPE_ALIGN (vectype);
      if (DECL_ALIGN (decl) >= alignment)
718
        continue;
719 720

      if (vect_can_force_dr_alignment_p (decl, alignment))
721 722 723
        {
          DECL_ALIGN (decl) = TYPE_ALIGN (vectype);
          DECL_USER_ALIGN (decl) = 1;
724 725
	  if (TREE_STATIC (decl))
	    {
Martin Liska committed
726
	      tree target = symtab_node::get (decl)->ultimate_alias_target ()->decl;
727 728 729
              DECL_ALIGN (target) = TYPE_ALIGN (vectype);
              DECL_USER_ALIGN (target) = 1;
	    }
730 731 732
          dump_printf (MSG_NOTE, "Increasing alignment of decl: ");
          dump_generic_expr (MSG_NOTE, TDF_SLIM, decl);
          dump_printf (MSG_NOTE, "\n");
733
        }
734 735 736 737
    }
  return 0;
}

738

739 740 741
namespace {

const pass_data pass_data_ipa_increase_alignment =
742
{
743 744 745 746 747 748 749 750 751
  SIMPLE_IPA_PASS, /* type */
  "increase_alignment", /* name */
  OPTGROUP_LOOP | OPTGROUP_VEC, /* optinfo_flags */
  TV_IPA_OPT, /* tv_id */
  0, /* properties_required */
  0, /* properties_provided */
  0, /* properties_destroyed */
  0, /* todo_flags_start */
  0, /* todo_flags_finish */
752
};
753 754 755 756

class pass_ipa_increase_alignment : public simple_ipa_opt_pass
{
public:
757 758
  pass_ipa_increase_alignment (gcc::context *ctxt)
    : simple_ipa_opt_pass (pass_data_ipa_increase_alignment, ctxt)
759 760 761
  {}

  /* opt_pass methods: */
762 763 764 765 766
  virtual bool gate (function *)
    {
      return flag_section_anchors && flag_tree_loop_vectorize;
    }

767
  virtual unsigned int execute (function *) { return increase_alignment (); }
768 769 770 771 772 773 774 775 776 777

}; // class pass_ipa_increase_alignment

} // anon namespace

simple_ipa_opt_pass *
make_pass_ipa_increase_alignment (gcc::context *ctxt)
{
  return new pass_ipa_increase_alignment (ctxt);
}