tree-data-ref.c 118 KB
Newer Older
1
/* Data references and dependences detectors.
2
   Copyright (C) 2003-2016 Free Software Foundation, Inc.
3
   Contributed by Sebastian Pop <pop@cri.ensmp.fr>
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 22

/* This pass walks a given loop structure searching for array
   references.  The information about the array accesses is recorded
H.J. Lu committed
23 24 25 26 27
   in DATA_REFERENCE structures.

   The basic test for determining the dependences is:
   given two access functions chrec1 and chrec2 to a same array, and
   x and y two vectors from the iteration domain, the same element of
28 29
   the array is accessed twice at iterations x and y if and only if:
   |             chrec1 (x) == chrec2 (y).
H.J. Lu committed
30

31
   The goals of this analysis are:
H.J. Lu committed
32

33 34 35
   - to determine the independence: the relation between two
     independent accesses is qualified with the chrec_known (this
     information allows a loop parallelization),
H.J. Lu committed
36

37 38
   - when two data references access the same data, to qualify the
     dependence relation with classic dependence representations:
H.J. Lu committed
39

40 41 42 43 44
       - distance vectors
       - direction vectors
       - loop carried level dependence
       - polyhedron dependence
     or with the chains of recurrences based representation,
H.J. Lu committed
45 46

   - to define a knowledge base for storing the data dependence
47
     information,
H.J. Lu committed
48

49
   - to define an interface to access this data.
H.J. Lu committed
50 51


52
   Definitions:
H.J. Lu committed
53

54 55 56 57 58 59
   - subscript: given two array accesses a subscript is the tuple
   composed of the access functions for a given dimension.  Example:
   Given A[f1][f2][f3] and B[g1][g2][g3], there are three subscripts:
   (f1, g1), (f2, g2), (f3, g3).

   - Diophantine equation: an equation whose coefficients and
H.J. Lu committed
60
   solutions are integer constants, for example the equation
61 62
   |   3*x + 2*y = 1
   has an integer solution x = 1 and y = -1.
H.J. Lu committed
63

64
   References:
H.J. Lu committed
65

66 67
   - "Advanced Compilation for High Performance Computing" by Randy
   Allen and Ken Kennedy.
H.J. Lu committed
68 69 70
   http://citeseer.ist.psu.edu/goff91practical.html

   - "Loop Transformations for Restructuring Compilers - The Foundations"
71 72
   by Utpal Banerjee.

H.J. Lu committed
73

74 75 76 77 78
*/

#include "config.h"
#include "system.h"
#include "coretypes.h"
79
#include "backend.h"
80
#include "rtl.h"
81
#include "tree.h"
82
#include "gimple.h"
83 84
#include "gimple-pretty-print.h"
#include "alias.h"
85
#include "fold-const.h"
86
#include "expr.h"
87
#include "gimple-iterator.h"
88
#include "tree-ssa-loop-niter.h"
89
#include "tree-ssa-loop.h"
Andrew MacLeod committed
90
#include "tree-ssa.h"
91 92 93
#include "cfgloop.h"
#include "tree-data-ref.h"
#include "tree-scalar-evolution.h"
94
#include "dumpfile.h"
95
#include "tree-affine.h"
96
#include "params.h"
97

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
static struct datadep_stats
{
  int num_dependence_tests;
  int num_dependence_dependent;
  int num_dependence_independent;
  int num_dependence_undetermined;

  int num_subscript_tests;
  int num_subscript_undetermined;
  int num_same_subscript_function;

  int num_ziv;
  int num_ziv_independent;
  int num_ziv_dependent;
  int num_ziv_unimplemented;

  int num_siv;
  int num_siv_independent;
  int num_siv_dependent;
  int num_siv_unimplemented;

  int num_miv;
  int num_miv_independent;
  int num_miv_dependent;
  int num_miv_unimplemented;
} dependence_stats;

125 126
static bool subscript_dependence_tester_1 (struct data_dependence_relation *,
					   struct data_reference *,
127 128
					   struct data_reference *,
					   struct loop *);
129 130
/* Returns true iff A divides B.  */

H.J. Lu committed
131
static inline bool
132
tree_fold_divides_p (const_tree a, const_tree b)
133
{
134 135
  gcc_assert (TREE_CODE (a) == INTEGER_CST);
  gcc_assert (TREE_CODE (b) == INTEGER_CST);
136
  return integer_zerop (int_const_binop (TRUNC_MOD_EXPR, b, a));
137 138
}

139 140
/* Returns true iff A divides B.  */

H.J. Lu committed
141
static inline bool
142 143 144
int_divides_p (int a, int b)
{
  return ((b % a) == 0);
145 146 147 148
}



H.J. Lu committed
149
/* Dump into FILE all the data references from DATAREFS.  */
150

151
static void
152
dump_data_references (FILE *file, vec<data_reference_p> datarefs)
153 154
{
  unsigned int i;
155 156
  struct data_reference *dr;

157
  FOR_EACH_VEC_ELT (datarefs, i, dr)
158
    dump_data_reference (file, dr);
159 160
}

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
/* Unified dump into FILE all the data references from DATAREFS.  */

DEBUG_FUNCTION void
debug (vec<data_reference_p> &ref)
{
  dump_data_references (stderr, ref);
}

DEBUG_FUNCTION void
debug (vec<data_reference_p> *ptr)
{
  if (ptr)
    debug (*ptr);
  else
    fprintf (stderr, "<nil>\n");
}


H.J. Lu committed
179
/* Dump into STDERR all the data references from DATAREFS.  */
180

181
DEBUG_FUNCTION void
182
debug_data_references (vec<data_reference_p> datarefs)
183 184 185 186 187 188
{
  dump_data_references (stderr, datarefs);
}

/* Print to STDERR the data_reference DR.  */

189
DEBUG_FUNCTION void
190 191 192 193 194
debug_data_reference (struct data_reference *dr)
{
  dump_data_reference (stderr, dr);
}

195 196
/* Dump function for a DATA_REFERENCE structure.  */

H.J. Lu committed
197 198
void
dump_data_reference (FILE *outf,
199 200 201
		     struct data_reference *dr)
{
  unsigned int i;
H.J. Lu committed
202

203 204 205
  fprintf (outf, "#(Data Ref: \n");
  fprintf (outf, "#  bb: %d \n", gimple_bb (DR_STMT (dr))->index);
  fprintf (outf, "#  stmt: ");
206
  print_gimple_stmt (outf, DR_STMT (dr), 0, 0);
207
  fprintf (outf, "#  ref: ");
208
  print_generic_stmt (outf, DR_REF (dr), 0);
209
  fprintf (outf, "#  base_object: ");
210
  print_generic_stmt (outf, DR_BASE_OBJECT (dr), 0);
H.J. Lu committed
211

212 213
  for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
    {
214
      fprintf (outf, "#  Access function %d: ", i);
215 216
      print_generic_stmt (outf, DR_ACCESS_FN (dr, i), 0);
    }
217
  fprintf (outf, "#)\n");
218 219
}

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
/* Unified dump function for a DATA_REFERENCE structure.  */

DEBUG_FUNCTION void
debug (data_reference &ref)
{
  dump_data_reference (stderr, &ref);
}

DEBUG_FUNCTION void
debug (data_reference *ptr)
{
  if (ptr)
    debug (*ptr);
  else
    fprintf (stderr, "<nil>\n");
}


238 239
/* Dumps the affine function described by FN to the file OUTF.  */

240
DEBUG_FUNCTION void
241 242 243 244 245
dump_affine_function (FILE *outf, affine_fn fn)
{
  unsigned i;
  tree coef;

246 247
  print_generic_expr (outf, fn[0], TDF_SLIM);
  for (i = 1; fn.iterate (i, &coef); i++)
248 249 250 251 252 253 254 255 256
    {
      fprintf (outf, " + ");
      print_generic_expr (outf, coef, TDF_SLIM);
      fprintf (outf, " * x_%u", i);
    }
}

/* Dumps the conflict function CF to the file OUTF.  */

257
DEBUG_FUNCTION void
258 259 260 261 262
dump_conflict_function (FILE *outf, conflict_function *cf)
{
  unsigned i;

  if (cf->n == NO_DEPENDENCE)
263
    fprintf (outf, "no dependence");
264
  else if (cf->n == NOT_KNOWN)
265
    fprintf (outf, "not known");
266 267 268 269
  else
    {
      for (i = 0; i < cf->n; i++)
	{
270 271
	  if (i != 0)
	    fprintf (outf, " ");
272 273
	  fprintf (outf, "[");
	  dump_affine_function (outf, cf->fns[i]);
274
	  fprintf (outf, "]");
275 276 277 278
	}
    }
}

279 280
/* Dump function for a SUBSCRIPT structure.  */

281
DEBUG_FUNCTION void
282 283
dump_subscript (FILE *outf, struct subscript *subscript)
{
284
  conflict_function *cf = SUB_CONFLICTS_IN_A (subscript);
285 286 287

  fprintf (outf, "\n (subscript \n");
  fprintf (outf, "  iterations_that_access_an_element_twice_in_A: ");
288 289
  dump_conflict_function (outf, cf);
  if (CF_NONTRIVIAL_P (cf))
290 291
    {
      tree last_iteration = SUB_LAST_CONFLICT (subscript);
292 293
      fprintf (outf, "\n  last_conflict: ");
      print_generic_expr (outf, last_iteration, 0);
294
    }
H.J. Lu committed
295

296
  cf = SUB_CONFLICTS_IN_B (subscript);
297
  fprintf (outf, "\n  iterations_that_access_an_element_twice_in_B: ");
298 299
  dump_conflict_function (outf, cf);
  if (CF_NONTRIVIAL_P (cf))
300 301
    {
      tree last_iteration = SUB_LAST_CONFLICT (subscript);
302 303
      fprintf (outf, "\n  last_conflict: ");
      print_generic_expr (outf, last_iteration, 0);
304 305
    }

306 307 308
  fprintf (outf, "\n  (Subscript distance: ");
  print_generic_expr (outf, SUB_DISTANCE (subscript), 0);
  fprintf (outf, " ))\n");
309 310
}

311 312
/* Print the classic direction vector DIRV to OUTF.  */

313
DEBUG_FUNCTION void
314 315 316 317 318 319 320 321
print_direction_vector (FILE *outf,
			lambda_vector dirv,
			int length)
{
  int eq;

  for (eq = 0; eq < length; eq++)
    {
322 323
      enum data_dependence_direction dir = ((enum data_dependence_direction)
					    dirv[eq]);
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

      switch (dir)
	{
	case dir_positive:
	  fprintf (outf, "    +");
	  break;
	case dir_negative:
	  fprintf (outf, "    -");
	  break;
	case dir_equal:
	  fprintf (outf, "    =");
	  break;
	case dir_positive_or_equal:
	  fprintf (outf, "   +=");
	  break;
	case dir_positive_or_negative:
	  fprintf (outf, "   +-");
	  break;
	case dir_negative_or_equal:
	  fprintf (outf, "   -=");
	  break;
	case dir_star:
	  fprintf (outf, "    *");
	  break;
	default:
	  fprintf (outf, "indep");
	  break;
	}
    }
  fprintf (outf, "\n");
}

356 357
/* Print a vector of direction vectors.  */

358
DEBUG_FUNCTION void
359
print_dir_vectors (FILE *outf, vec<lambda_vector> dir_vects,
360 361 362 363 364
		   int length)
{
  unsigned j;
  lambda_vector v;

365
  FOR_EACH_VEC_ELT (dir_vects, j, v)
366 367 368
    print_direction_vector (outf, v, length);
}

369 370
/* Print out a vector VEC of length N to OUTFILE.  */

371
DEBUG_FUNCTION void
372 373 374 375 376 377 378 379 380
print_lambda_vector (FILE * outfile, lambda_vector vector, int n)
{
  int i;

  for (i = 0; i < n; i++)
    fprintf (outfile, "%3d ", vector[i]);
  fprintf (outfile, "\n");
}

381 382
/* Print a vector of distance vectors.  */

383
DEBUG_FUNCTION void
384
print_dist_vectors (FILE *outf, vec<lambda_vector> dist_vects,
385
		    int length)
386 387 388 389
{
  unsigned j;
  lambda_vector v;

390
  FOR_EACH_VEC_ELT (dist_vects, j, v)
391 392 393
    print_lambda_vector (outf, v, length);
}

394 395
/* Dump function for a DATA_DEPENDENCE_RELATION structure.  */

396
DEBUG_FUNCTION void
H.J. Lu committed
397
dump_data_dependence_relation (FILE *outf,
398 399 400
			       struct data_dependence_relation *ddr)
{
  struct data_reference *dra, *drb;
401 402

  fprintf (outf, "(Data Dep: \n");
403

404 405
  if (!ddr || DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
    {
406 407 408 409 410 411 412 413 414 415 416 417 418
      if (ddr)
	{
	  dra = DDR_A (ddr);
	  drb = DDR_B (ddr);
	  if (dra)
	    dump_data_reference (outf, dra);
	  else
	    fprintf (outf, "    (nil)\n");
	  if (drb)
	    dump_data_reference (outf, drb);
	  else
	    fprintf (outf, "    (nil)\n");
	}
419 420 421 422 423 424
      fprintf (outf, "    (don't know)\n)\n");
      return;
    }

  dra = DDR_A (ddr);
  drb = DDR_B (ddr);
425 426 427
  dump_data_reference (outf, dra);
  dump_data_reference (outf, drb);

428
  if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
429
    fprintf (outf, "    (no dependence)\n");
H.J. Lu committed
430

431
  else if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
432
    {
433
      unsigned int i;
434
      struct loop *loopi;
435

436 437 438 439 440 441
      for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
	{
	  fprintf (outf, "  access_fn_A: ");
	  print_generic_stmt (outf, DR_ACCESS_FN (dra, i), 0);
	  fprintf (outf, "  access_fn_B: ");
	  print_generic_stmt (outf, DR_ACCESS_FN (drb, i), 0);
442
	  dump_subscript (outf, DDR_SUBSCRIPT (ddr, i));
443
	}
444

445
      fprintf (outf, "  inner loop index: %d\n", DDR_INNER_LOOP (ddr));
446
      fprintf (outf, "  loop nest: (");
447
      FOR_EACH_VEC_ELT (DDR_LOOP_NEST (ddr), i, loopi)
448 449 450
	fprintf (outf, "%d ", loopi->num);
      fprintf (outf, ")\n");

451
      for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
452
	{
453 454
	  fprintf (outf, "  distance_vector: ");
	  print_lambda_vector (outf, DDR_DIST_VECT (ddr, i),
455
			       DDR_NB_LOOPS (ddr));
456
	}
457 458

      for (i = 0; i < DDR_NUM_DIR_VECTS (ddr); i++)
459
	{
460
	  fprintf (outf, "  direction_vector: ");
461
	  print_direction_vector (outf, DDR_DIR_VECT (ddr, i),
462
				  DDR_NB_LOOPS (ddr));
463 464 465 466 467 468
	}
    }

  fprintf (outf, ")\n");
}

469
/* Debug version.  */
470

471 472
DEBUG_FUNCTION void
debug_data_dependence_relation (struct data_dependence_relation *ddr)
473
{
474 475
  dump_data_dependence_relation (stderr, ddr);
}
H.J. Lu committed
476

477
/* Dump into FILE all the dependence relations from DDRS.  */
H.J. Lu committed
478

479
DEBUG_FUNCTION void
480
dump_data_dependence_relations (FILE *file,
481
				vec<ddr_p> ddrs)
482 483 484
{
  unsigned int i;
  struct data_dependence_relation *ddr;
H.J. Lu committed
485

486
  FOR_EACH_VEC_ELT (ddrs, i, ddr)
487 488
    dump_data_dependence_relation (file, ddr);
}
H.J. Lu committed
489

490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
DEBUG_FUNCTION void
debug (vec<ddr_p> &ref)
{
  dump_data_dependence_relations (stderr, ref);
}

DEBUG_FUNCTION void
debug (vec<ddr_p> *ptr)
{
  if (ptr)
    debug (*ptr);
  else
    fprintf (stderr, "<nil>\n");
}


506
/* Dump to STDERR all the dependence relations from DDRS.  */
H.J. Lu committed
507

508
DEBUG_FUNCTION void
509
debug_data_dependence_relations (vec<ddr_p> ddrs)
510 511
{
  dump_data_dependence_relations (stderr, ddrs);
512 513
}

514 515 516 517 518
/* Dumps the distance and direction vectors in FILE.  DDRS contains
   the dependence relations, and VECT_SIZE is the size of the
   dependence vectors, or in other words the number of loops in the
   considered nest.  */

519
DEBUG_FUNCTION void
520
dump_dist_dir_vectors (FILE *file, vec<ddr_p> ddrs)
521
{
522
  unsigned int i, j;
523 524
  struct data_dependence_relation *ddr;
  lambda_vector v;
525

526
  FOR_EACH_VEC_ELT (ddrs, i, ddr)
527 528
    if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE && DDR_AFFINE_P (ddr))
      {
529
	FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), j, v)
530 531 532 533 534 535
	  {
	    fprintf (file, "DISTANCE_V (");
	    print_lambda_vector (file, v, DDR_NB_LOOPS (ddr));
	    fprintf (file, ")\n");
	  }

536
	FOR_EACH_VEC_ELT (DDR_DIR_VECTS (ddr), j, v)
537 538 539 540 541 542
	  {
	    fprintf (file, "DIRECTION_V (");
	    print_direction_vector (file, v, DDR_NB_LOOPS (ddr));
	    fprintf (file, ")\n");
	  }
      }
543

544 545 546 547 548
  fprintf (file, "\n\n");
}

/* Dumps the data dependence relations DDRS in FILE.  */

549
DEBUG_FUNCTION void
550
dump_ddrs (FILE *file, vec<ddr_p> ddrs)
551 552
{
  unsigned int i;
553 554
  struct data_dependence_relation *ddr;

555
  FOR_EACH_VEC_ELT (ddrs, i, ddr)
556
    dump_data_dependence_relation (file, ddr);
557 558 559 560

  fprintf (file, "\n\n");
}

561
DEBUG_FUNCTION void
562
debug_ddrs (vec<ddr_p> ddrs)
563 564 565 566
{
  dump_ddrs (stderr, ddrs);
}

567 568 569 570 571
/* Helper function for split_constant_offset.  Expresses OP0 CODE OP1
   (the type of the result is TYPE) as VAR + OFF, where OFF is a nonzero
   constant of type ssizetype, and returns true.  If we cannot do this
   with OFF nonzero, OFF and VAR are set to NULL_TREE instead and false
   is returned.  */
572

573 574 575
static bool
split_constant_offset_1 (tree type, tree op0, enum tree_code code, tree op1,
			 tree *var, tree *off)
576
{
577 578
  tree var0, var1;
  tree off0, off1;
579
  enum tree_code ocode = code;
580

581 582
  *var = NULL_TREE;
  *off = NULL_TREE;
583

Andrew Pinski committed
584
  switch (code)
585
    {
586 587
    case INTEGER_CST:
      *var = build_int_cst (type, 0);
588 589
      *off = fold_convert (ssizetype, op0);
      return true;
590

Andrew Pinski committed
591
    case POINTER_PLUS_EXPR:
592
      ocode = PLUS_EXPR;
Andrew Pinski committed
593
      /* FALLTHROUGH */
594 595
    case PLUS_EXPR:
    case MINUS_EXPR:
596 597 598 599 600
      split_constant_offset (op0, &var0, &off0);
      split_constant_offset (op1, &var1, &off1);
      *var = fold_build2 (code, type, var0, var1);
      *off = size_binop (ocode, off0, off1);
      return true;
601 602

    case MULT_EXPR:
603 604
      if (TREE_CODE (op1) != INTEGER_CST)
	return false;
605

606 607 608 609
      split_constant_offset (op0, &var0, &off0);
      *var = fold_build2 (MULT_EXPR, type, var0, op1);
      *off = size_binop (MULT_EXPR, off0, fold_convert (ssizetype, op1));
      return true;
610

611 612
    case ADDR_EXPR:
      {
613
	tree base, poffset;
614
	HOST_WIDE_INT pbitsize, pbitpos;
615
	machine_mode pmode;
616
	int punsignedp, preversep, pvolatilep;
617

618
	op0 = TREE_OPERAND (op0, 0);
619 620
	base
	  = get_inner_reference (op0, &pbitsize, &pbitpos, &poffset, &pmode,
621
				 &punsignedp, &preversep, &pvolatilep);
622

623
	if (pbitpos % BITS_PER_UNIT != 0)
624
	  return false;
625 626
	base = build_fold_addr_expr (base);
	off0 = ssize_int (pbitpos / BITS_PER_UNIT);
627

628 629 630 631
	if (poffset)
	  {
	    split_constant_offset (poffset, &poffset, &off1);
	    off0 = size_binop (PLUS_EXPR, off0, off1);
632
	    if (POINTER_TYPE_P (TREE_TYPE (base)))
633
	      base = fold_build_pointer_plus (base, poffset);
634 635 636
	    else
	      base = fold_build2 (PLUS_EXPR, TREE_TYPE (base), base,
				  fold_convert (TREE_TYPE (base), poffset));
637 638
	  }

639 640 641 642 643 644 645
	var0 = fold_convert (type, base);

	/* If variable length types are involved, punt, otherwise casts
	   might be converted into ARRAY_REFs in gimplify_conversion.
	   To compute that ARRAY_REF's element size TYPE_SIZE_UNIT, which
	   possibly no longer appears in current GIMPLE, might resurface.
	   This perhaps could run
646
	   if (CONVERT_EXPR_P (var0))
647 648 649 650 651 652 653 654 655
	     {
	       gimplify_conversion (&var0);
	       // Attempt to fill in any within var0 found ARRAY_REF's
	       // element size from corresponding op embedded ARRAY_REF,
	       // if unsuccessful, just punt.
	     }  */
	while (POINTER_TYPE_P (type))
	  type = TREE_TYPE (type);
	if (int_size_in_bytes (type) < 0)
656
	  return false;
657 658

	*var = var0;
659
	*off = off0;
660
	return true;
661
      }
662

663 664
    case SSA_NAME:
      {
665 666 667
	if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0))
	  return false;

668
	gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
669
	enum tree_code subcode;
670

671 672 673 674 675 676 677 678
	if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
	  return false;

	var0 = gimple_assign_rhs1 (def_stmt);
	subcode = gimple_assign_rhs_code (def_stmt);
	var1 = gimple_assign_rhs2 (def_stmt);

	return split_constant_offset_1 (type, var0, subcode, var1, var, off);
679
      }
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
    CASE_CONVERT:
      {
	/* We must not introduce undefined overflow, and we must not change the value.
	   Hence we're okay if the inner type doesn't overflow to start with
	   (pointer or signed), the outer type also is an integer or pointer
	   and the outer precision is at least as large as the inner.  */
	tree itype = TREE_TYPE (op0);
	if ((POINTER_TYPE_P (itype)
	     || (INTEGRAL_TYPE_P (itype) && TYPE_OVERFLOW_UNDEFINED (itype)))
	    && TYPE_PRECISION (type) >= TYPE_PRECISION (itype)
	    && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)))
	  {
	    split_constant_offset (op0, &var0, off);
	    *var = fold_convert (type, var0);
	    return true;
	  }
	return false;
      }
698

699
    default:
700
      return false;
701
    }
702 703 704 705 706 707 708 709 710 711
}

/* Expresses EXP as VAR + OFF, where off is a constant.  The type of OFF
   will be ssizetype.  */

void
split_constant_offset (tree exp, tree *var, tree *off)
{
  tree type = TREE_TYPE (exp), otype, op0, op1, e, o;
  enum tree_code code;
712

713
  *var = exp;
714
  *off = ssize_int (0);
715 716
  STRIP_NOPS (exp);

717 718
  if (tree_is_chrec (exp)
      || get_gimple_rhs_class (TREE_CODE (exp)) == GIMPLE_TERNARY_RHS)
719 720 721 722 723 724 725 726 727 728
    return;

  otype = TREE_TYPE (exp);
  code = TREE_CODE (exp);
  extract_ops_from_tree (exp, &code, &op0, &op1);
  if (split_constant_offset_1 (otype, op0, code, op1, &e, &o))
    {
      *var = fold_convert (type, e);
      *off = o;
    }
729 730
}

731 732
/* Returns the address ADDR of an object in a canonical shape (without nop
   casts, and with type of pointer to the object).  */
733 734

static tree
735
canonicalize_base_object_address (tree addr)
736
{
737 738
  tree orig = addr;

739
  STRIP_NOPS (addr);
740

741 742 743 744 745
  /* The base address may be obtained by casting from integer, in that case
     keep the cast.  */
  if (!POINTER_TYPE_P (TREE_TYPE (addr)))
    return orig;

746 747
  if (TREE_CODE (addr) != ADDR_EXPR)
    return addr;
748

749
  return build_fold_addr_expr (TREE_OPERAND (addr, 0));
750 751
}

H.J. Lu committed
752
/* Analyzes the behavior of the memory reference DR in the innermost loop or
753
   basic block that contains it.  Returns true if analysis succeed or false
754
   otherwise.  */
755

756
bool
757
dr_analyze_innermost (struct data_reference *dr, struct loop *nest)
758
{
759
  gimple *stmt = DR_STMT (dr);
760 761
  struct loop *loop = loop_containing_stmt (stmt);
  tree ref = DR_REF (dr);
762
  HOST_WIDE_INT pbitsize, pbitpos;
763
  tree base, poffset;
764
  machine_mode pmode;
765
  int punsignedp, preversep, pvolatilep;
766 767
  affine_iv base_iv, offset_iv;
  tree init, dinit, step;
768
  bool in_loop = (loop && loop->num);
769 770 771

  if (dump_file && (dump_flags & TDF_DETAILS))
    fprintf (dump_file, "analyze_innermost: ");
772

773
  base = get_inner_reference (ref, &pbitsize, &pbitpos, &poffset, &pmode,
774
			      &punsignedp, &preversep, &pvolatilep);
775
  gcc_assert (base != NULL_TREE);
776

777
  if (pbitpos % BITS_PER_UNIT != 0)
778
    {
779 780
      if (dump_file && (dump_flags & TDF_DETAILS))
	fprintf (dump_file, "failed: bit offset alignment.\n");
781
      return false;
782
    }
783

784 785 786 787 788 789 790
  if (preversep)
    {
      if (dump_file && (dump_flags & TDF_DETAILS))
	fprintf (dump_file, "failed: reverse storage order.\n");
      return false;
    }

791 792 793 794
  if (TREE_CODE (base) == MEM_REF)
    {
      if (!integer_zerop (TREE_OPERAND (base, 1)))
	{
Kenneth Zadeck committed
795 796
	  offset_int moff = mem_ref_offset (base);
	  tree mofft = wide_int_to_tree (sizetype, moff);
797
	  if (!poffset)
798
	    poffset = mofft;
799
	  else
800
	    poffset = size_binop (PLUS_EXPR, poffset, mofft);
801 802 803 804 805
	}
      base = TREE_OPERAND (base, 0);
    }
  else
    base = build_fold_addr_expr (base);
806

807
  if (in_loop)
808
    {
H.J. Lu committed
809
      if (!simple_iv (loop, loop_containing_stmt (stmt), base, &base_iv,
810
                      nest ? true : false))
811
        {
812 813 814 815 816 817 818 819 820 821 822 823 824
          if (nest)
            {
              if (dump_file && (dump_flags & TDF_DETAILS))
                fprintf (dump_file, "failed: evolution of base is not"
                                    " affine.\n");
              return false;
            }
          else
            {
              base_iv.base = base;
              base_iv.step = ssize_int (0);
              base_iv.no_overflow = true;
            }
825 826 827 828 829 830 831
        }
    }
  else
    {
      base_iv.base = base;
      base_iv.step = ssize_int (0);
      base_iv.no_overflow = true;
832
    }
833

834
  if (!poffset)
835 836 837 838
    {
      offset_iv.base = ssize_int (0);
      offset_iv.step = ssize_int (0);
    }
839
  else
840
    {
841 842 843 844 845 846
      if (!in_loop)
        {
          offset_iv.base = poffset;
          offset_iv.step = ssize_int (0);
        }
      else if (!simple_iv (loop, loop_containing_stmt (stmt),
847 848
                           poffset, &offset_iv,
			   nest ? true : false))
849
        {
850 851 852 853 854 855 856 857 858 859 860 861
          if (nest)
            {
              if (dump_file && (dump_flags & TDF_DETAILS))
                fprintf (dump_file, "failed: evolution of offset is not"
                                    " affine.\n");
              return false;
            }
          else
            {
              offset_iv.base = poffset;
              offset_iv.step = ssize_int (0);
            }
862
        }
863
    }
864

865 866 867 868 869
  init = ssize_int (pbitpos / BITS_PER_UNIT);
  split_constant_offset (base_iv.base, &base_iv.base, &dinit);
  init =  size_binop (PLUS_EXPR, init, dinit);
  split_constant_offset (offset_iv.base, &offset_iv.base, &dinit);
  init =  size_binop (PLUS_EXPR, init, dinit);
870

871 872 873
  step = size_binop (PLUS_EXPR,
		     fold_convert (ssizetype, base_iv.step),
		     fold_convert (ssizetype, offset_iv.step));
874

875
  DR_BASE_ADDRESS (dr) = canonicalize_base_object_address (base_iv.base);
876

877 878 879
  DR_OFFSET (dr) = fold_convert (ssizetype, offset_iv.base);
  DR_INIT (dr) = init;
  DR_STEP (dr) = step;
880

881
  DR_ALIGNED_TO (dr) = size_int (highest_pow2_factor (offset_iv.base));
882

883 884
  if (dump_file && (dump_flags & TDF_DETAILS))
    fprintf (dump_file, "success.\n");
885 886

  return true;
887
}
888

889
/* Determines the base object and the list of indices of memory reference
890
   DR, analyzed in LOOP and instantiated in loop nest NEST.  */
891

892
static void
893
dr_analyze_indices (struct data_reference *dr, loop_p nest, loop_p loop)
894
{
895
  vec<tree> access_fns = vNULL;
896
  tree ref, op;
897 898
  tree base, off, access_fn;
  basic_block before_loop;
H.J. Lu committed
899

900 901
  /* If analyzing a basic-block there are no indices to analyze
     and thus no access functions.  */
902 903
  if (!nest)
    {
904
      DR_BASE_OBJECT (dr) = DR_REF (dr);
905
      DR_ACCESS_FNS (dr).create (0);
906 907 908
      return;
    }

909
  ref = DR_REF (dr);
910
  before_loop = block_before_loop (nest);
H.J. Lu committed
911

912 913 914 915 916 917
  /* REALPART_EXPR and IMAGPART_EXPR can be handled like accesses
     into a two element array with a constant index.  The base is
     then just the immediate underlying object.  */
  if (TREE_CODE (ref) == REALPART_EXPR)
    {
      ref = TREE_OPERAND (ref, 0);
918
      access_fns.safe_push (integer_zero_node);
919 920 921 922
    }
  else if (TREE_CODE (ref) == IMAGPART_EXPR)
    {
      ref = TREE_OPERAND (ref, 0);
923
      access_fns.safe_push (integer_one_node);
924 925
    }

926
  /* Analyze access functions of dimensions we know to be independent.  */
927
  while (handled_component_p (ref))
928
    {
929
      if (TREE_CODE (ref) == ARRAY_REF)
930
	{
931
	  op = TREE_OPERAND (ref, 1);
932 933
	  access_fn = analyze_scalar_evolution (loop, op);
	  access_fn = instantiate_scev (before_loop, loop, access_fn);
934
	  access_fns.safe_push (access_fn);
935
	}
936 937 938 939 940 941 942 943 944 945 946 947
      else if (TREE_CODE (ref) == COMPONENT_REF
	       && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
	{
	  /* For COMPONENT_REFs of records (but not unions!) use the
	     FIELD_DECL offset as constant access function so we can
	     disambiguate a[i].f1 and a[i].f2.  */
	  tree off = component_ref_field_offset (ref);
	  off = size_binop (PLUS_EXPR,
			    size_binop (MULT_EXPR,
					fold_convert (bitsizetype, off),
					bitsize_int (BITS_PER_UNIT)),
			    DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1)));
948
	  access_fns.safe_push (off);
949 950 951 952 953 954
	}
      else
	/* If we have an unhandled component we could not translate
	   to an access function stop analyzing.  We have determined
	   our base object in this case.  */
	break;
H.J. Lu committed
955

956
      ref = TREE_OPERAND (ref, 0);
957 958
    }

959 960
  /* If the address operand of a MEM_REF base has an evolution in the
     analyzed nest, add it as an additional independent access-function.  */
961
  if (TREE_CODE (ref) == MEM_REF)
962
    {
963
      op = TREE_OPERAND (ref, 0);
964
      access_fn = analyze_scalar_evolution (loop, op);
965
      access_fn = instantiate_scev (before_loop, loop, access_fn);
966
      if (TREE_CODE (access_fn) == POLYNOMIAL_CHREC)
967
	{
968
	  tree orig_type;
969
	  tree memoff = TREE_OPERAND (ref, 1);
970
	  base = initial_condition (access_fn);
971 972
	  orig_type = TREE_TYPE (base);
	  STRIP_USELESS_TYPE_CONVERSION (base);
973
	  split_constant_offset (base, &base, &off);
974
	  STRIP_USELESS_TYPE_CONVERSION (base);
975 976
	  /* Fold the MEM_REF offset into the evolutions initial
	     value to make more bases comparable.  */
977
	  if (!integer_zerop (memoff))
978 979
	    {
	      off = size_binop (PLUS_EXPR, off,
980 981
				fold_convert (ssizetype, memoff));
	      memoff = build_int_cst (TREE_TYPE (memoff), 0);
982
	    }
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
	  /* Adjust the offset so it is a multiple of the access type
	     size and thus we separate bases that can possibly be used
	     to produce partial overlaps (which the access_fn machinery
	     cannot handle).  */
	  wide_int rem;
	  if (TYPE_SIZE_UNIT (TREE_TYPE (ref))
	      && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (ref))) == INTEGER_CST
	      && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (ref))))
	    rem = wi::mod_trunc (off, TYPE_SIZE_UNIT (TREE_TYPE (ref)), SIGNED);
	  else
	    /* If we can't compute the remainder simply force the initial
	       condition to zero.  */
	    rem = off;
	  off = wide_int_to_tree (ssizetype, wi::sub (off, rem));
	  memoff = wide_int_to_tree (TREE_TYPE (memoff), rem);
	  /* And finally replace the initial condition.  */
999
	  access_fn = chrec_replace_initial_condition
1000
	      (access_fn, fold_convert (orig_type, off));
1001 1002 1003 1004 1005 1006 1007
	  /* ???  This is still not a suitable base object for
	     dr_may_alias_p - the base object needs to be an
	     access that covers the object as whole.  With
	     an evolution in the pointer this cannot be
	     guaranteed.
	     As a band-aid, mark the access so we can special-case
	     it in dr_may_alias_p.  */
1008
	  tree old = ref;
1009 1010 1011
	  ref = fold_build2_loc (EXPR_LOCATION (ref),
				 MEM_REF, TREE_TYPE (ref),
				 base, memoff);
1012 1013
	  MR_DEPENDENCE_CLIQUE (ref) = MR_DEPENDENCE_CLIQUE (old);
	  MR_DEPENDENCE_BASE (ref) = MR_DEPENDENCE_BASE (old);
1014
	  DR_UNCONSTRAINED_BASE (dr) = true;
1015
	  access_fns.safe_push (access_fn);
1016
	}
1017
    }
1018 1019 1020 1021 1022 1023 1024
  else if (DECL_P (ref))
    {
      /* Canonicalize DR_BASE_OBJECT to MEM_REF form.  */
      ref = build2 (MEM_REF, TREE_TYPE (ref),
		    build_fold_addr_expr (ref),
		    build_int_cst (reference_alias_ptr_type (ref), 0));
    }
1025

1026 1027
  DR_BASE_OBJECT (dr) = ref;
  DR_ACCESS_FNS (dr) = access_fns;
1028 1029
}

1030
/* Extracts the alias analysis information from the memory reference DR.  */
1031

1032 1033
static void
dr_analyze_alias (struct data_reference *dr)
1034
{
1035
  tree ref = DR_REF (dr);
1036 1037
  tree base = get_base_address (ref), addr;

1038 1039
  if (INDIRECT_REF_P (base)
      || TREE_CODE (base) == MEM_REF)
1040 1041 1042
    {
      addr = TREE_OPERAND (base, 0);
      if (TREE_CODE (addr) == SSA_NAME)
1043
	DR_PTR_INFO (dr) = SSA_NAME_PTR_INFO (addr);
1044 1045
    }
}
1046

1047
/* Frees data reference DR.  */
1048

1049
void
1050 1051
free_data_ref (data_reference_p dr)
{
1052
  DR_ACCESS_FNS (dr).release ();
1053 1054
  free (dr);
}
1055

1056 1057
/* Analyzes memory reference MEMREF accessed in STMT.  The reference
   is read if IS_READ is true, write otherwise.  Returns the
1058 1059 1060
   data_reference description of MEMREF.  NEST is the outermost loop
   in which the reference should be instantiated, LOOP is the loop in
   which the data reference should be analyzed.  */
1061

1062
struct data_reference *
1063
create_data_ref (loop_p nest, loop_p loop, tree memref, gimple *stmt,
1064
		 bool is_read)
1065
{
1066
  struct data_reference *dr;
1067

1068
  if (dump_file && (dump_flags & TDF_DETAILS))
1069
    {
1070 1071 1072
      fprintf (dump_file, "Creating dr for ");
      print_generic_expr (dump_file, memref, TDF_SLIM);
      fprintf (dump_file, "\n");
1073
    }
1074

1075 1076 1077 1078
  dr = XCNEW (struct data_reference);
  DR_STMT (dr) = stmt;
  DR_REF (dr) = memref;
  DR_IS_READ (dr) = is_read;
1079

1080
  dr_analyze_innermost (dr, nest);
1081
  dr_analyze_indices (dr, nest, loop);
1082
  dr_analyze_alias (dr);
1083 1084 1085

  if (dump_file && (dump_flags & TDF_DETAILS))
    {
1086
      unsigned i;
1087
      fprintf (dump_file, "\tbase_address: ");
1088 1089 1090 1091 1092 1093 1094
      print_generic_expr (dump_file, DR_BASE_ADDRESS (dr), TDF_SLIM);
      fprintf (dump_file, "\n\toffset from base address: ");
      print_generic_expr (dump_file, DR_OFFSET (dr), TDF_SLIM);
      fprintf (dump_file, "\n\tconstant offset from base address: ");
      print_generic_expr (dump_file, DR_INIT (dr), TDF_SLIM);
      fprintf (dump_file, "\n\tstep: ");
      print_generic_expr (dump_file, DR_STEP (dr), TDF_SLIM);
1095 1096 1097 1098
      fprintf (dump_file, "\n\taligned to: ");
      print_generic_expr (dump_file, DR_ALIGNED_TO (dr), TDF_SLIM);
      fprintf (dump_file, "\n\tbase_object: ");
      print_generic_expr (dump_file, DR_BASE_OBJECT (dr), TDF_SLIM);
1099
      fprintf (dump_file, "\n");
1100 1101 1102 1103 1104
      for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
	{
	  fprintf (dump_file, "\tAccess function %d: ", i);
	  print_generic_stmt (dump_file, DR_ACCESS_FN (dr, i), TDF_SLIM);
	}
1105 1106
    }

H.J. Lu committed
1107
  return dr;
1108 1109
}

1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
/* Check if OFFSET1 and OFFSET2 (DR_OFFSETs of some data-refs) are identical
   expressions.  */
static bool
dr_equal_offsets_p1 (tree offset1, tree offset2)
{
  bool res;

  STRIP_NOPS (offset1);
  STRIP_NOPS (offset2);

  if (offset1 == offset2)
    return true;

  if (TREE_CODE (offset1) != TREE_CODE (offset2)
      || (!BINARY_CLASS_P (offset1) && !UNARY_CLASS_P (offset1)))
    return false;

  res = dr_equal_offsets_p1 (TREE_OPERAND (offset1, 0),
                             TREE_OPERAND (offset2, 0));

  if (!res || !BINARY_CLASS_P (offset1))
    return res;

  res = dr_equal_offsets_p1 (TREE_OPERAND (offset1, 1),
                             TREE_OPERAND (offset2, 1));

  return res;
}

/* Check if DRA and DRB have equal offsets.  */
bool
dr_equal_offsets_p (struct data_reference *dra,
                    struct data_reference *drb)
{
  tree offset1, offset2;

  offset1 = DR_OFFSET (dra);
  offset2 = DR_OFFSET (drb);

  return dr_equal_offsets_p1 (offset1, offset2);
}

1152 1153 1154 1155 1156
/* Returns true if FNA == FNB.  */

static bool
affine_function_equal_p (affine_fn fna, affine_fn fnb)
{
1157
  unsigned i, n = fna.length ();
1158

1159
  if (n != fnb.length ())
1160
    return false;
1161

1162
  for (i = 0; i < n; i++)
1163
    if (!operand_equal_p (fna[i], fnb[i], 0))
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
      return false;

  return true;
}

/* If all the functions in CF are the same, returns one of them,
   otherwise returns NULL.  */

static affine_fn
common_affine_function (conflict_function *cf)
1174
{
1175 1176 1177 1178
  unsigned i;
  affine_fn comm;

  if (!CF_NONTRIVIAL_P (cf))
1179
    return affine_fn ();
1180 1181 1182 1183 1184

  comm = cf->fns[0];

  for (i = 1; i < cf->n; i++)
    if (!affine_function_equal_p (comm, cf->fns[i]))
1185
      return affine_fn ();
1186 1187 1188

  return comm;
}
1189

1190 1191 1192 1193 1194
/* Returns the base of the affine function FN.  */

static tree
affine_function_base (affine_fn fn)
{
1195
  return fn[0];
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
}

/* Returns true if FN is a constant.  */

static bool
affine_function_constant_p (affine_fn fn)
{
  unsigned i;
  tree coef;

1206
  for (i = 1; fn.iterate (i, &coef); i++)
1207
    if (!integer_zerop (coef))
1208 1209
      return false;

1210 1211 1212
  return true;
}

1213 1214 1215 1216 1217 1218 1219 1220 1221
/* Returns true if FN is the zero constant function.  */

static bool
affine_function_zero_p (affine_fn fn)
{
  return (integer_zerop (affine_function_base (fn))
	  && affine_function_constant_p (fn));
}

1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
/* Returns a signed integer type with the largest precision from TA
   and TB.  */

static tree
signed_type_for_types (tree ta, tree tb)
{
  if (TYPE_PRECISION (ta) > TYPE_PRECISION (tb))
    return signed_type_for (ta);
  else
    return signed_type_for (tb);
}

1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
/* Applies operation OP on affine functions FNA and FNB, and returns the
   result.  */

static affine_fn
affine_fn_op (enum tree_code op, affine_fn fna, affine_fn fnb)
{
  unsigned i, n, m;
  affine_fn ret;
  tree coef;

1244
  if (fnb.length () > fna.length ())
1245
    {
1246 1247
      n = fna.length ();
      m = fnb.length ();
1248 1249 1250
    }
  else
    {
1251 1252
      n = fnb.length ();
      m = fna.length ();
1253 1254
    }

1255
  ret.create (m);
1256
  for (i = 0; i < n; i++)
1257
    {
1258 1259 1260
      tree type = signed_type_for_types (TREE_TYPE (fna[i]),
					 TREE_TYPE (fnb[i]));
      ret.quick_push (fold_build2 (op, type, fna[i], fnb[i]));
1261
    }
1262

1263 1264
  for (; fna.iterate (i, &coef); i++)
    ret.quick_push (fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
1265
				 coef, integer_zero_node));
1266 1267
  for (; fnb.iterate (i, &coef); i++)
    ret.quick_push (fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
				 integer_zero_node, coef));

  return ret;
}

/* Returns the sum of affine functions FNA and FNB.  */

static affine_fn
affine_fn_plus (affine_fn fna, affine_fn fnb)
{
  return affine_fn_op (PLUS_EXPR, fna, fnb);
}

/* Returns the difference of affine functions FNA and FNB.  */

static affine_fn
affine_fn_minus (affine_fn fna, affine_fn fnb)
{
  return affine_fn_op (MINUS_EXPR, fna, fnb);
}

/* Frees affine function FN.  */

static void
affine_fn_free (affine_fn fn)
{
1294
  fn.release ();
1295 1296
}

1297 1298
/* Determine for each subscript in the data dependence relation DDR
   the distance.  */
1299

1300
static void
1301
compute_subscript_distance (struct data_dependence_relation *ddr)
1302
{
1303 1304 1305
  conflict_function *cf_a, *cf_b;
  affine_fn fn_a, fn_b, diff;

1306 1307 1308
  if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
    {
      unsigned int i;
H.J. Lu committed
1309

1310 1311 1312
      for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
 	{
 	  struct subscript *subscript;
H.J. Lu committed
1313

1314
 	  subscript = DDR_SUBSCRIPT (ddr, i);
1315 1316
 	  cf_a = SUB_CONFLICTS_IN_A (subscript);
 	  cf_b = SUB_CONFLICTS_IN_B (subscript);
1317

1318 1319
	  fn_a = common_affine_function (cf_a);
	  fn_b = common_affine_function (cf_b);
1320
	  if (!fn_a.exists () || !fn_b.exists ())
1321
	    {
1322 1323
	      SUB_DISTANCE (subscript) = chrec_dont_know;
	      return;
1324
	    }
1325
	  diff = affine_fn_minus (fn_a, fn_b);
H.J. Lu committed
1326

1327 1328
 	  if (affine_function_constant_p (diff))
 	    SUB_DISTANCE (subscript) = affine_function_base (diff);
1329 1330
 	  else
 	    SUB_DISTANCE (subscript) = chrec_dont_know;
1331 1332

	  affine_fn_free (diff);
1333 1334 1335 1336
 	}
    }
}

1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
/* Returns the conflict function for "unknown".  */

static conflict_function *
conflict_fn_not_known (void)
{
  conflict_function *fn = XCNEW (conflict_function);
  fn->n = NOT_KNOWN;

  return fn;
}

/* Returns the conflict function for "independent".  */

static conflict_function *
conflict_fn_no_dependence (void)
{
  conflict_function *fn = XCNEW (conflict_function);
  fn->n = NO_DEPENDENCE;

  return fn;
}

1359 1360 1361
/* Returns true if the address of OBJ is invariant in LOOP.  */

static bool
1362
object_address_invariant_in_loop_p (const struct loop *loop, const_tree obj)
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
{
  while (handled_component_p (obj))
    {
      if (TREE_CODE (obj) == ARRAY_REF)
	{
	  /* Index of the ARRAY_REF was zeroed in analyze_indices, thus we only
	     need to check the stride and the lower bound of the reference.  */
	  if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 2),
						      loop->num)
	      || chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 3),
							 loop->num))
	    return false;
	}
      else if (TREE_CODE (obj) == COMPONENT_REF)
	{
	  if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 2),
						      loop->num))
	    return false;
	}
      obj = TREE_OPERAND (obj, 0);
    }

1385 1386
  if (!INDIRECT_REF_P (obj)
      && TREE_CODE (obj) != MEM_REF)
1387 1388 1389 1390 1391 1392 1393
    return true;

  return !chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 0),
						  loop->num);
}

/* Returns false if we can prove that data references A and B do not alias,
1394 1395
   true otherwise.  If LOOP_NEST is false no cross-iteration aliases are
   considered.  */
1396

1397
bool
1398 1399
dr_may_alias_p (const struct data_reference *a, const struct data_reference *b,
		bool loop_nest)
1400
{
1401 1402
  tree addr_a = DR_BASE_OBJECT (a);
  tree addr_b = DR_BASE_OBJECT (b);
1403

1404 1405 1406 1407 1408 1409 1410 1411
  /* If we are not processing a loop nest but scalar code we
     do not need to care about possible cross-iteration dependences
     and thus can process the full original reference.  Do so,
     similar to how loop invariant motion applies extra offset-based
     disambiguation.  */
  if (!loop_nest)
    {
      aff_tree off1, off2;
Kenneth Zadeck committed
1412
      widest_int size1, size2;
1413 1414
      get_inner_reference_aff (DR_REF (a), &off1, &size1);
      get_inner_reference_aff (DR_REF (b), &off2, &size2);
Kenneth Zadeck committed
1415
      aff_combination_scale (&off1, -1);
1416 1417 1418 1419 1420
      aff_combination_add (&off2, &off1);
      if (aff_comb_cannot_overlap_p (&off2, size1, size2))
	return false;
    }

1421 1422 1423 1424 1425 1426
  if ((TREE_CODE (addr_a) == MEM_REF || TREE_CODE (addr_a) == TARGET_MEM_REF)
      && (TREE_CODE (addr_b) == MEM_REF || TREE_CODE (addr_b) == TARGET_MEM_REF)
      && MR_DEPENDENCE_CLIQUE (addr_a) == MR_DEPENDENCE_CLIQUE (addr_b)
      && MR_DEPENDENCE_BASE (addr_a) != MR_DEPENDENCE_BASE (addr_b))
    return false;

1427 1428 1429 1430
  /* If we had an evolution in a pointer-based MEM_REF BASE_OBJECT we
     do not know the size of the base-object.  So we cannot do any
     offset/overlap based analysis but have to rely on points-to
     information only.  */
1431
  if (TREE_CODE (addr_a) == MEM_REF
1432 1433
      && (DR_UNCONSTRAINED_BASE (a)
	  || TREE_CODE (TREE_OPERAND (addr_a, 0)) == SSA_NAME))
1434
    {
1435 1436 1437 1438 1439 1440 1441
      /* For true dependences we can apply TBAA.  */
      if (flag_strict_aliasing
	  && DR_IS_WRITE (a) && DR_IS_READ (b)
	  && !alias_sets_conflict_p (get_alias_set (DR_REF (a)),
				     get_alias_set (DR_REF (b))))
	return false;
      if (TREE_CODE (addr_b) == MEM_REF)
1442 1443 1444 1445 1446 1447 1448
	return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
				       TREE_OPERAND (addr_b, 0));
      else
	return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
				       build_fold_addr_expr (addr_b));
    }
  else if (TREE_CODE (addr_b) == MEM_REF
1449 1450
	   && (DR_UNCONSTRAINED_BASE (b)
	       || TREE_CODE (TREE_OPERAND (addr_b, 0)) == SSA_NAME))
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464
    {
      /* For true dependences we can apply TBAA.  */
      if (flag_strict_aliasing
	  && DR_IS_WRITE (a) && DR_IS_READ (b)
	  && !alias_sets_conflict_p (get_alias_set (DR_REF (a)),
				     get_alias_set (DR_REF (b))))
	return false;
      if (TREE_CODE (addr_a) == MEM_REF)
	return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
				       TREE_OPERAND (addr_b, 0));
      else
	return ptr_derefs_may_alias_p (build_fold_addr_expr (addr_a),
				       TREE_OPERAND (addr_b, 0));
    }
1465 1466 1467

  /* Otherwise DR_BASE_OBJECT is an access that covers the whole object
     that is being subsetted in the loop nest.  */
1468
  if (DR_IS_WRITE (a) && DR_IS_WRITE (b))
1469
    return refs_output_dependent_p (addr_a, addr_b);
1470
  else if (DR_IS_READ (a) && DR_IS_WRITE (b))
1471 1472
    return refs_anti_dependent_p (addr_a, addr_b);
  return refs_may_alias_p (addr_a, addr_b);
1473 1474
}

1475 1476 1477
/* Initialize a data dependence relation between data accesses A and
   B.  NB_LOOPS is the number of loops surrounding the references: the
   size of the classic distance/direction vectors.  */
1478

1479
struct data_dependence_relation *
H.J. Lu committed
1480
initialize_data_dependence_relation (struct data_reference *a,
1481
				     struct data_reference *b,
1482
 				     vec<loop_p> loop_nest)
1483 1484
{
  struct data_dependence_relation *res;
1485
  unsigned int i;
H.J. Lu committed
1486

1487
  res = XNEW (struct data_dependence_relation);
1488 1489
  DDR_A (res) = a;
  DDR_B (res) = b;
1490
  DDR_LOOP_NEST (res).create (0);
1491
  DDR_REVERSED_P (res) = false;
1492 1493 1494
  DDR_SUBSCRIPTS (res).create (0);
  DDR_DIR_VECTS (res).create (0);
  DDR_DIST_VECTS (res).create (0);
1495

1496 1497
  if (a == NULL || b == NULL)
    {
H.J. Lu committed
1498
      DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1499
      return res;
H.J. Lu committed
1500
    }
1501

1502
  /* If the data references do not alias, then they are independent.  */
1503
  if (!dr_may_alias_p (a, b, loop_nest.exists ()))
1504
    {
H.J. Lu committed
1505
      DDR_ARE_DEPENDENT (res) = chrec_known;
1506 1507
      return res;
    }
1508

1509
  /* The case where the references are exactly the same.  */
1510 1511
  if (operand_equal_p (DR_REF (a), DR_REF (b), 0))
    {
1512 1513 1514 1515 1516 1517 1518 1519
      if ((loop_nest.exists ()
	   && !object_address_invariant_in_loop_p (loop_nest[0],
						   DR_BASE_OBJECT (a)))
	  || DR_NUM_DIMENSIONS (a) == 0)
	{
	  DDR_ARE_DEPENDENT (res) = chrec_dont_know;
	  return res;
	}
1520 1521
      DDR_AFFINE_P (res) = true;
      DDR_ARE_DEPENDENT (res) = NULL_TREE;
1522
      DDR_SUBSCRIPTS (res).create (DR_NUM_DIMENSIONS (a));
1523 1524 1525
      DDR_LOOP_NEST (res) = loop_nest;
      DDR_INNER_LOOP (res) = 0;
      DDR_SELF_REFERENCE (res) = true;
1526 1527 1528 1529 1530 1531 1532 1533 1534
      for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
       {
         struct subscript *subscript;

         subscript = XNEW (struct subscript);
         SUB_CONFLICTS_IN_A (subscript) = conflict_fn_not_known ();
         SUB_CONFLICTS_IN_B (subscript) = conflict_fn_not_known ();
         SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
         SUB_DISTANCE (subscript) = chrec_dont_know;
1535
         DDR_SUBSCRIPTS (res).safe_push (subscript);
1536
       }
1537 1538 1539
      return res;
    }

1540
  /* If the references do not access the same object, we do not know
1541 1542 1543 1544 1545 1546 1547
     whether they alias or not.  We do not care about TBAA or alignment
     info so we can use OEP_ADDRESS_OF to avoid false negatives.
     But the accesses have to use compatible types as otherwise the
     built indices would not match.  */
  if (!operand_equal_p (DR_BASE_OBJECT (a), DR_BASE_OBJECT (b), OEP_ADDRESS_OF)
      || !types_compatible_p (TREE_TYPE (DR_BASE_OBJECT (a)),
			      TREE_TYPE (DR_BASE_OBJECT (b))))
1548
    {
H.J. Lu committed
1549
      DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1550 1551
      return res;
    }
1552

1553
  /* If the base of the object is not invariant in the loop nest, we cannot
1554
     analyze it.  TODO -- in fact, it would suffice to record that there may
1555
     be arbitrary dependences in the loops where the base object varies.  */
1556 1557 1558
  if ((loop_nest.exists ()
       && !object_address_invariant_in_loop_p (loop_nest[0], DR_BASE_OBJECT (a)))
      || DR_NUM_DIMENSIONS (a) == 0)
1559
    {
H.J. Lu committed
1560
      DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1561 1562
      return res;
    }
1563

1564 1565 1566 1567 1568 1569 1570 1571
  /* If the number of dimensions of the access to not agree we can have
     a pointer access to a component of the array element type and an
     array access while the base-objects are still the same.  Punt.  */
  if (DR_NUM_DIMENSIONS (a) != DR_NUM_DIMENSIONS (b))
    {
      DDR_ARE_DEPENDENT (res) = chrec_dont_know;
      return res;
    }
1572

1573 1574
  DDR_AFFINE_P (res) = true;
  DDR_ARE_DEPENDENT (res) = NULL_TREE;
1575
  DDR_SUBSCRIPTS (res).create (DR_NUM_DIMENSIONS (a));
1576
  DDR_LOOP_NEST (res) = loop_nest;
1577
  DDR_INNER_LOOP (res) = 0;
1578
  DDR_SELF_REFERENCE (res) = false;
1579

1580 1581 1582
  for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
    {
      struct subscript *subscript;
H.J. Lu committed
1583

1584
      subscript = XNEW (struct subscript);
1585 1586
      SUB_CONFLICTS_IN_A (subscript) = conflict_fn_not_known ();
      SUB_CONFLICTS_IN_B (subscript) = conflict_fn_not_known ();
1587 1588
      SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
      SUB_DISTANCE (subscript) = chrec_dont_know;
1589
      DDR_SUBSCRIPTS (res).safe_push (subscript);
1590
    }
1591

1592 1593 1594
  return res;
}

1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612
/* Frees memory used by the conflict function F.  */

static void
free_conflict_function (conflict_function *f)
{
  unsigned i;

  if (CF_NONTRIVIAL_P (f))
    {
      for (i = 0; i < f->n; i++)
	affine_fn_free (f->fns[i]);
    }
  free (f);
}

/* Frees memory used by SUBSCRIPTS.  */

static void
1613
free_subscripts (vec<subscript_p> subscripts)
1614 1615 1616 1617
{
  unsigned i;
  subscript_p s;

1618
  FOR_EACH_VEC_ELT (subscripts, i, s)
1619 1620 1621
    {
      free_conflict_function (s->conflicting_iterations_in_a);
      free_conflict_function (s->conflicting_iterations_in_b);
1622
      free (s);
1623
    }
1624
  subscripts.release ();
1625 1626
}

1627 1628 1629 1630
/* Set DDR_ARE_DEPENDENT to CHREC and finalize the subscript overlap
   description.  */

static inline void
H.J. Lu committed
1631
finalize_ddr_dependent (struct data_dependence_relation *ddr,
1632 1633
			tree chrec)
{
H.J. Lu committed
1634
  DDR_ARE_DEPENDENT (ddr) = chrec;
1635
  free_subscripts (DDR_SUBSCRIPTS (ddr));
1636
  DDR_SUBSCRIPTS (ddr).create (0);
1637 1638
}

1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650
/* The dependence relation DDR cannot be represented by a distance
   vector.  */

static inline void
non_affine_dependence_relation (struct data_dependence_relation *ddr)
{
  if (dump_file && (dump_flags & TDF_DETAILS))
    fprintf (dump_file, "(Dependence relation cannot be represented by distance vector.) \n");

  DDR_AFFINE_P (ddr) = false;
}

1651 1652 1653 1654 1655 1656 1657 1658


/* This section contains the classic Banerjee tests.  */

/* Returns true iff CHREC_A and CHREC_B are not dependent on any index
   variables, i.e., if the ZIV (Zero Index Variable) test is true.  */

static inline bool
1659
ziv_subscript_p (const_tree chrec_a, const_tree chrec_b)
1660 1661 1662 1663 1664 1665 1666 1667 1668
{
  return (evolution_function_is_constant_p (chrec_a)
	  && evolution_function_is_constant_p (chrec_b));
}

/* Returns true iff CHREC_A and CHREC_B are dependent on an index
   variable, i.e., if the SIV (Single Index Variable) test is true.  */

static bool
1669
siv_subscript_p (const_tree chrec_a, const_tree chrec_b)
1670 1671 1672 1673 1674 1675
{
  if ((evolution_function_is_constant_p (chrec_a)
       && evolution_function_is_univariate_p (chrec_b))
      || (evolution_function_is_constant_p (chrec_b)
	  && evolution_function_is_univariate_p (chrec_a)))
    return true;
H.J. Lu committed
1676

1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687
  if (evolution_function_is_univariate_p (chrec_a)
      && evolution_function_is_univariate_p (chrec_b))
    {
      switch (TREE_CODE (chrec_a))
	{
	case POLYNOMIAL_CHREC:
	  switch (TREE_CODE (chrec_b))
	    {
	    case POLYNOMIAL_CHREC:
	      if (CHREC_VARIABLE (chrec_a) != CHREC_VARIABLE (chrec_b))
		return false;
H.J. Lu committed
1688

1689 1690 1691
	    default:
	      return true;
	    }
H.J. Lu committed
1692

1693 1694 1695 1696
	default:
	  return true;
	}
    }
H.J. Lu committed
1697

1698 1699 1700
  return false;
}

1701 1702 1703 1704 1705 1706 1707 1708 1709 1710
/* Creates a conflict function with N dimensions.  The affine functions
   in each dimension follow.  */

static conflict_function *
conflict_fn (unsigned n, ...)
{
  unsigned i;
  conflict_function *ret = XCNEW (conflict_function);
  va_list ap;

1711
  gcc_assert (0 < n && n <= MAX_DIM);
1712
  va_start (ap, n);
H.J. Lu committed
1713

1714 1715 1716
  ret->n = n;
  for (i = 0; i < n; i++)
    ret->fns[i] = va_arg (ap, affine_fn);
1717
  va_end (ap);
1718 1719 1720 1721 1722 1723 1724 1725 1726

  return ret;
}

/* Returns constant affine function with value CST.  */

static affine_fn
affine_fn_cst (tree cst)
{
1727 1728 1729
  affine_fn fn;
  fn.create (1);
  fn.quick_push (cst);
1730 1731 1732 1733 1734 1735 1736 1737
  return fn;
}

/* Returns affine function with single variable, CST + COEF * x_DIM.  */

static affine_fn
affine_fn_univar (tree cst, unsigned dim, tree coef)
{
1738 1739
  affine_fn fn;
  fn.create (dim + 1);
1740 1741 1742
  unsigned i;

  gcc_assert (dim > 0);
1743
  fn.quick_push (cst);
1744
  for (i = 1; i < dim; i++)
1745 1746
    fn.quick_push (integer_zero_node);
  fn.quick_push (coef);
1747 1748 1749
  return fn;
}

1750 1751 1752 1753 1754 1755 1756
/* Analyze a ZIV (Zero Index Variable) subscript.  *OVERLAPS_A and
   *OVERLAPS_B are initialized to the functions that describe the
   relation between the elements accessed twice by CHREC_A and
   CHREC_B.  For k >= 0, the following property is verified:

   CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)).  */

H.J. Lu committed
1757 1758 1759
static void
analyze_ziv_subscript (tree chrec_a,
		       tree chrec_b,
1760
		       conflict_function **overlaps_a,
H.J. Lu committed
1761
		       conflict_function **overlaps_b,
1762
		       tree *last_conflicts)
1763
{
1764
  tree type, difference;
1765
  dependence_stats.num_ziv++;
H.J. Lu committed
1766

1767 1768
  if (dump_file && (dump_flags & TDF_DETAILS))
    fprintf (dump_file, "(analyze_ziv_subscript \n");
1769 1770

  type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
1771 1772
  chrec_a = chrec_convert (type, chrec_a, NULL);
  chrec_b = chrec_convert (type, chrec_b, NULL);
1773
  difference = chrec_fold_minus (type, chrec_a, chrec_b);
H.J. Lu committed
1774

1775 1776 1777 1778 1779 1780 1781
  switch (TREE_CODE (difference))
    {
    case INTEGER_CST:
      if (integer_zerop (difference))
	{
	  /* The difference is equal to zero: the accessed index
	     overlaps for each iteration in the loop.  */
1782 1783
	  *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
	  *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
1784
	  *last_conflicts = chrec_dont_know;
1785
	  dependence_stats.num_ziv_dependent++;
1786 1787 1788 1789
	}
      else
	{
	  /* The accesses do not overlap.  */
1790 1791
	  *overlaps_a = conflict_fn_no_dependence ();
	  *overlaps_b = conflict_fn_no_dependence ();
1792
	  *last_conflicts = integer_zero_node;
1793
	  dependence_stats.num_ziv_independent++;
1794 1795
	}
      break;
H.J. Lu committed
1796

1797
    default:
H.J. Lu committed
1798
      /* We're not sure whether the indexes overlap.  For the moment,
1799
	 conservatively answer "don't know".  */
1800 1801 1802
      if (dump_file && (dump_flags & TDF_DETAILS))
	fprintf (dump_file, "ziv test failed: difference is non-integer.\n");

1803 1804
      *overlaps_a = conflict_fn_not_known ();
      *overlaps_b = conflict_fn_not_known ();
1805
      *last_conflicts = chrec_dont_know;
1806
      dependence_stats.num_ziv_unimplemented++;
1807 1808
      break;
    }
H.J. Lu committed
1809

1810 1811 1812 1813
  if (dump_file && (dump_flags & TDF_DETAILS))
    fprintf (dump_file, ")\n");
}

1814
/* Similar to max_stmt_executions_int, but returns the bound as a tree,
1815
   and only if it fits to the int type.  If this is not the case, or the
1816
   bound  on the number of iterations of LOOP could not be derived, returns
1817 1818 1819
   chrec_dont_know.  */

static tree
1820
max_stmt_executions_tree (struct loop *loop)
1821
{
Kenneth Zadeck committed
1822
  widest_int nit;
1823

1824
  if (!max_stmt_executions (loop, &nit))
1825 1826
    return chrec_dont_know;

Kenneth Zadeck committed
1827
  if (!wi::fits_to_tree_p (nit, unsigned_type_node))
1828 1829
    return chrec_dont_know;

Kenneth Zadeck committed
1830
  return wide_int_to_tree (unsigned_type_node, nit);
1831 1832
}

1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902
/* Determine whether the CHREC is always positive/negative.  If the expression
   cannot be statically analyzed, return false, otherwise set the answer into
   VALUE.  */

static bool
chrec_is_positive (tree chrec, bool *value)
{
  bool value0, value1, value2;
  tree end_value, nb_iter;

  switch (TREE_CODE (chrec))
    {
    case POLYNOMIAL_CHREC:
      if (!chrec_is_positive (CHREC_LEFT (chrec), &value0)
	  || !chrec_is_positive (CHREC_RIGHT (chrec), &value1))
	return false;

      /* FIXME -- overflows.  */
      if (value0 == value1)
	{
	  *value = value0;
	  return true;
	}

      /* Otherwise the chrec is under the form: "{-197, +, 2}_1",
	 and the proof consists in showing that the sign never
	 changes during the execution of the loop, from 0 to
	 loop->nb_iterations.  */
      if (!evolution_function_is_affine_p (chrec))
	return false;

      nb_iter = number_of_latch_executions (get_chrec_loop (chrec));
      if (chrec_contains_undetermined (nb_iter))
	return false;

#if 0
      /* TODO -- If the test is after the exit, we may decrease the number of
	 iterations by one.  */
      if (after_exit)
	nb_iter = chrec_fold_minus (type, nb_iter, build_int_cst (type, 1));
#endif

      end_value = chrec_apply (CHREC_VARIABLE (chrec), chrec, nb_iter);

      if (!chrec_is_positive (end_value, &value2))
	return false;

      *value = value0;
      return value0 == value1;

    case INTEGER_CST:
      switch (tree_int_cst_sgn (chrec))
	{
	case -1:
	  *value = false;
	  break;
	case 1:
	  *value = true;
	  break;
	default:
	  return false;
	}
      return true;

    default:
      return false;
    }
}


1903 1904 1905 1906 1907 1908 1909 1910 1911
/* Analyze a SIV (Single Index Variable) subscript where CHREC_A is a
   constant, and CHREC_B is an affine function.  *OVERLAPS_A and
   *OVERLAPS_B are initialized to the functions that describe the
   relation between the elements accessed twice by CHREC_A and
   CHREC_B.  For k >= 0, the following property is verified:

   CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)).  */

static void
H.J. Lu committed
1912
analyze_siv_subscript_cst_affine (tree chrec_a,
1913
				  tree chrec_b,
H.J. Lu committed
1914 1915
				  conflict_function **overlaps_a,
				  conflict_function **overlaps_b,
1916
				  tree *last_conflicts)
1917 1918
{
  bool value0, value1, value2;
1919
  tree type, difference, tmp;
1920

1921
  type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
1922 1923
  chrec_a = chrec_convert (type, chrec_a, NULL);
  chrec_b = chrec_convert (type, chrec_b, NULL);
1924
  difference = chrec_fold_minus (type, initial_condition (chrec_b), chrec_a);
H.J. Lu committed
1925

1926 1927 1928 1929 1930 1931 1932 1933 1934
  /* Special case overlap in the first iteration.  */
  if (integer_zerop (difference))
    {
      *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
      *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
      *last_conflicts = integer_one_node;
      return;
    }

1935 1936
  if (!chrec_is_positive (initial_condition (difference), &value0))
    {
1937
      if (dump_file && (dump_flags & TDF_DETAILS))
H.J. Lu committed
1938
	fprintf (dump_file, "siv test failed: chrec is not positive.\n");
1939 1940

      dependence_stats.num_siv_unimplemented++;
1941 1942
      *overlaps_a = conflict_fn_not_known ();
      *overlaps_b = conflict_fn_not_known ();
1943
      *last_conflicts = chrec_dont_know;
1944 1945 1946 1947 1948 1949 1950 1951
      return;
    }
  else
    {
      if (value0 == false)
	{
	  if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value1))
	    {
1952 1953 1954
	      if (dump_file && (dump_flags & TDF_DETAILS))
		fprintf (dump_file, "siv test failed: chrec not positive.\n");

1955
	      *overlaps_a = conflict_fn_not_known ();
H.J. Lu committed
1956
	      *overlaps_b = conflict_fn_not_known ();
1957
	      *last_conflicts = chrec_dont_know;
1958
	      dependence_stats.num_siv_unimplemented++;
1959 1960 1961 1962 1963 1964
	      return;
	    }
	  else
	    {
	      if (value1 == true)
		{
H.J. Lu committed
1965
		  /* Example:
1966 1967 1968
		     chrec_a = 12
		     chrec_b = {10, +, 1}
		  */
H.J. Lu committed
1969

1970
		  if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
1971
		    {
1972 1973
		      HOST_WIDE_INT numiter;
		      struct loop *loop = get_chrec_loop (chrec_b);
1974

1975
		      *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1976 1977
		      tmp = fold_build2 (EXACT_DIV_EXPR, type,
					 fold_build1 (ABS_EXPR, type, difference),
1978 1979
					 CHREC_RIGHT (chrec_b));
		      *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
1980
		      *last_conflicts = integer_one_node;
H.J. Lu committed
1981

1982 1983 1984

		      /* Perform weak-zero siv test to see if overlap is
			 outside the loop bounds.  */
1985
		      numiter = max_stmt_executions_int (loop);
1986

1987 1988
		      if (numiter >= 0
			  && compare_tree_int (tmp, numiter) > 0)
1989
			{
1990 1991 1992 1993
			  free_conflict_function (*overlaps_a);
			  free_conflict_function (*overlaps_b);
			  *overlaps_a = conflict_fn_no_dependence ();
			  *overlaps_b = conflict_fn_no_dependence ();
1994
			  *last_conflicts = integer_zero_node;
1995
			  dependence_stats.num_siv_independent++;
1996
			  return;
H.J. Lu committed
1997
			}
1998
		      dependence_stats.num_siv_dependent++;
1999 2000
		      return;
		    }
H.J. Lu committed
2001

2002
		  /* When the step does not divide the difference, there are
2003 2004 2005
		     no overlaps.  */
		  else
		    {
2006
		      *overlaps_a = conflict_fn_no_dependence ();
H.J. Lu committed
2007
		      *overlaps_b = conflict_fn_no_dependence ();
2008
		      *last_conflicts = integer_zero_node;
2009
		      dependence_stats.num_siv_independent++;
2010 2011 2012
		      return;
		    }
		}
H.J. Lu committed
2013

2014 2015
	      else
		{
H.J. Lu committed
2016
		  /* Example:
2017 2018
		     chrec_a = 12
		     chrec_b = {10, +, -1}
H.J. Lu committed
2019

2020
		     In this case, chrec_a will not overlap with chrec_b.  */
2021 2022
		  *overlaps_a = conflict_fn_no_dependence ();
		  *overlaps_b = conflict_fn_no_dependence ();
2023
		  *last_conflicts = integer_zero_node;
2024
		  dependence_stats.num_siv_independent++;
2025 2026 2027 2028
		  return;
		}
	    }
	}
H.J. Lu committed
2029
      else
2030 2031 2032
	{
	  if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value2))
	    {
2033 2034 2035
	      if (dump_file && (dump_flags & TDF_DETAILS))
		fprintf (dump_file, "siv test failed: chrec not positive.\n");

2036
	      *overlaps_a = conflict_fn_not_known ();
H.J. Lu committed
2037
	      *overlaps_b = conflict_fn_not_known ();
2038
	      *last_conflicts = chrec_dont_know;
2039
	      dependence_stats.num_siv_unimplemented++;
2040 2041 2042 2043 2044 2045
	      return;
	    }
	  else
	    {
	      if (value2 == false)
		{
H.J. Lu committed
2046
		  /* Example:
2047 2048 2049
		     chrec_a = 3
		     chrec_b = {10, +, -1}
		  */
2050
		  if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
2051
		    {
2052 2053
		      HOST_WIDE_INT numiter;
		      struct loop *loop = get_chrec_loop (chrec_b);
2054

2055
		      *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2056
		      tmp = fold_build2 (EXACT_DIV_EXPR, type, difference,
2057 2058
					 CHREC_RIGHT (chrec_b));
		      *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
2059
		      *last_conflicts = integer_one_node;
2060 2061 2062

		      /* Perform weak-zero siv test to see if overlap is
			 outside the loop bounds.  */
2063
		      numiter = max_stmt_executions_int (loop);
2064

2065 2066
		      if (numiter >= 0
			  && compare_tree_int (tmp, numiter) > 0)
2067
			{
2068 2069 2070 2071
			  free_conflict_function (*overlaps_a);
			  free_conflict_function (*overlaps_b);
			  *overlaps_a = conflict_fn_no_dependence ();
			  *overlaps_b = conflict_fn_no_dependence ();
2072
			  *last_conflicts = integer_zero_node;
2073
			  dependence_stats.num_siv_independent++;
2074
			  return;
H.J. Lu committed
2075
			}
2076
		      dependence_stats.num_siv_dependent++;
2077 2078
		      return;
		    }
H.J. Lu committed
2079

2080
		  /* When the step does not divide the difference, there
2081 2082 2083
		     are no overlaps.  */
		  else
		    {
2084
		      *overlaps_a = conflict_fn_no_dependence ();
H.J. Lu committed
2085
		      *overlaps_b = conflict_fn_no_dependence ();
2086
		      *last_conflicts = integer_zero_node;
2087
		      dependence_stats.num_siv_independent++;
2088 2089 2090 2091 2092
		      return;
		    }
		}
	      else
		{
H.J. Lu committed
2093 2094
		  /* Example:
		     chrec_a = 3
2095
		     chrec_b = {4, +, 1}
H.J. Lu committed
2096

2097
		     In this case, chrec_a will not overlap with chrec_b.  */
2098 2099
		  *overlaps_a = conflict_fn_no_dependence ();
		  *overlaps_b = conflict_fn_no_dependence ();
2100
		  *last_conflicts = integer_zero_node;
2101
		  dependence_stats.num_siv_independent++;
2102 2103 2104 2105 2106 2107 2108
		  return;
		}
	    }
	}
    }
}

2109
/* Helper recursive function for initializing the matrix A.  Returns
2110
   the initial value of CHREC.  */
2111

2112
static tree
2113 2114 2115 2116
initialize_matrix_A (lambda_matrix A, tree chrec, unsigned index, int mult)
{
  gcc_assert (chrec);

2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134
  switch (TREE_CODE (chrec))
    {
    case POLYNOMIAL_CHREC:
      gcc_assert (TREE_CODE (CHREC_RIGHT (chrec)) == INTEGER_CST);

      A[index][0] = mult * int_cst_value (CHREC_RIGHT (chrec));
      return initialize_matrix_A (A, CHREC_LEFT (chrec), index + 1, mult);

    case PLUS_EXPR:
    case MULT_EXPR:
    case MINUS_EXPR:
      {
	tree op0 = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
	tree op1 = initialize_matrix_A (A, TREE_OPERAND (chrec, 1), index, mult);

	return chrec_fold_op (TREE_CODE (chrec), chrec_type (chrec), op0, op1);
      }

2135
    CASE_CONVERT:
2136 2137
      {
	tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2138
	return chrec_convert (chrec_type (chrec), op, NULL);
2139 2140
      }

2141 2142 2143 2144 2145 2146 2147 2148
    case BIT_NOT_EXPR:
      {
	/* Handle ~X as -1 - X.  */
	tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
	return chrec_fold_op (MINUS_EXPR, chrec_type (chrec),
			      build_int_cst (TREE_TYPE (chrec), -1), op);
      }

2149 2150
    case INTEGER_CST:
      return chrec;
2151

2152 2153 2154 2155
    default:
      gcc_unreachable ();
      return NULL_TREE;
    }
2156 2157 2158 2159
}

#define FLOOR_DIV(x,y) ((x) / (y))

H.J. Lu committed
2160
/* Solves the special case of the Diophantine equation:
2161 2162 2163 2164 2165
   | {0, +, STEP_A}_x (OVERLAPS_A) = {0, +, STEP_B}_y (OVERLAPS_B)

   Computes the descriptions OVERLAPS_A and OVERLAPS_B.  NITER is the
   number of iterations that loops X and Y run.  The overlaps will be
   constructed as evolutions in dimension DIM.  */
2166 2167

static void
H.J. Lu committed
2168
compute_overlap_steps_for_affine_univar (int niter, int step_a, int step_b,
2169
					 affine_fn *overlaps_a,
H.J. Lu committed
2170
					 affine_fn *overlaps_b,
2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182
					 tree *last_conflicts, int dim)
{
  if (((step_a > 0 && step_b > 0)
       || (step_a < 0 && step_b < 0)))
    {
      int step_overlaps_a, step_overlaps_b;
      int gcd_steps_a_b, last_conflict, tau2;

      gcd_steps_a_b = gcd (step_a, step_b);
      step_overlaps_a = step_b / gcd_steps_a_b;
      step_overlaps_b = step_a / gcd_steps_a_b;

2183 2184 2185 2186 2187 2188 2189 2190 2191
      if (niter > 0)
	{
	  tau2 = FLOOR_DIV (niter, step_overlaps_a);
	  tau2 = MIN (tau2, FLOOR_DIV (niter, step_overlaps_b));
	  last_conflict = tau2;
	  *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
	}
      else
	*last_conflicts = chrec_dont_know;
2192

H.J. Lu committed
2193
      *overlaps_a = affine_fn_univar (integer_zero_node, dim,
2194 2195
				      build_int_cst (NULL_TREE,
						     step_overlaps_a));
H.J. Lu committed
2196 2197
      *overlaps_b = affine_fn_univar (integer_zero_node, dim,
				      build_int_cst (NULL_TREE,
2198
						     step_overlaps_b));
2199 2200 2201 2202
    }

  else
    {
2203 2204
      *overlaps_a = affine_fn_cst (integer_zero_node);
      *overlaps_b = affine_fn_cst (integer_zero_node);
2205 2206 2207 2208 2209 2210
      *last_conflicts = integer_zero_node;
    }
}

/* Solves the special case of a Diophantine equation where CHREC_A is
   an affine bivariate function, and CHREC_B is an affine univariate
H.J. Lu committed
2211
   function.  For example,
2212 2213

   | {{0, +, 1}_x, +, 1335}_y = {0, +, 1336}_z
H.J. Lu committed
2214 2215

   has the following overlapping functions:
2216 2217 2218 2219 2220

   | x (t, u, v) = {{0, +, 1336}_t, +, 1}_v
   | y (t, u, v) = {{0, +, 1336}_u, +, 1}_v
   | z (t, u, v) = {{{0, +, 1}_t, +, 1335}_u, +, 1}_v

2221
   FORNOW: This is a specialized implementation for a case occurring in
2222 2223 2224
   a common benchmark.  Implement the general algorithm.  */

static void
H.J. Lu committed
2225
compute_overlap_steps_for_affine_1_2 (tree chrec_a, tree chrec_b,
2226
				      conflict_function **overlaps_a,
H.J. Lu committed
2227
				      conflict_function **overlaps_b,
2228
				      tree *last_conflicts)
2229
{
2230 2231
  bool xz_p, yz_p, xyz_p;
  int step_x, step_y, step_z;
2232
  HOST_WIDE_INT niter_x, niter_y, niter_z, niter;
2233 2234 2235 2236 2237
  affine_fn overlaps_a_xz, overlaps_b_xz;
  affine_fn overlaps_a_yz, overlaps_b_yz;
  affine_fn overlaps_a_xyz, overlaps_b_xyz;
  affine_fn ova1, ova2, ovb;
  tree last_conflicts_xz, last_conflicts_yz, last_conflicts_xyz;
2238

2239 2240 2241
  step_x = int_cst_value (CHREC_RIGHT (CHREC_LEFT (chrec_a)));
  step_y = int_cst_value (CHREC_RIGHT (chrec_a));
  step_z = int_cst_value (CHREC_RIGHT (chrec_b));
2242

2243 2244 2245
  niter_x = max_stmt_executions_int (get_chrec_loop (CHREC_LEFT (chrec_a)));
  niter_y = max_stmt_executions_int (get_chrec_loop (chrec_a));
  niter_z = max_stmt_executions_int (get_chrec_loop (chrec_b));
H.J. Lu committed
2246

2247
  if (niter_x < 0 || niter_y < 0 || niter_z < 0)
2248
    {
2249 2250
      if (dump_file && (dump_flags & TDF_DETAILS))
	fprintf (dump_file, "overlap steps test failed: no iteration counts.\n");
H.J. Lu committed
2251

2252 2253
      *overlaps_a = conflict_fn_not_known ();
      *overlaps_b = conflict_fn_not_known ();
2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280
      *last_conflicts = chrec_dont_know;
      return;
    }

  niter = MIN (niter_x, niter_z);
  compute_overlap_steps_for_affine_univar (niter, step_x, step_z,
					   &overlaps_a_xz,
					   &overlaps_b_xz,
					   &last_conflicts_xz, 1);
  niter = MIN (niter_y, niter_z);
  compute_overlap_steps_for_affine_univar (niter, step_y, step_z,
					   &overlaps_a_yz,
					   &overlaps_b_yz,
					   &last_conflicts_yz, 2);
  niter = MIN (niter_x, niter_z);
  niter = MIN (niter_y, niter);
  compute_overlap_steps_for_affine_univar (niter, step_x + step_y, step_z,
					   &overlaps_a_xyz,
					   &overlaps_b_xyz,
					   &last_conflicts_xyz, 3);

  xz_p = !integer_zerop (last_conflicts_xz);
  yz_p = !integer_zerop (last_conflicts_yz);
  xyz_p = !integer_zerop (last_conflicts_xyz);

  if (xz_p || yz_p || xyz_p)
    {
2281 2282 2283
      ova1 = affine_fn_cst (integer_zero_node);
      ova2 = affine_fn_cst (integer_zero_node);
      ovb = affine_fn_cst (integer_zero_node);
2284 2285
      if (xz_p)
	{
2286 2287 2288 2289 2290 2291 2292
	  affine_fn t0 = ova1;
	  affine_fn t2 = ovb;

	  ova1 = affine_fn_plus (ova1, overlaps_a_xz);
	  ovb = affine_fn_plus (ovb, overlaps_b_xz);
	  affine_fn_free (t0);
	  affine_fn_free (t2);
2293 2294 2295 2296
	  *last_conflicts = last_conflicts_xz;
	}
      if (yz_p)
	{
2297 2298 2299 2300 2301 2302 2303
	  affine_fn t0 = ova2;
	  affine_fn t2 = ovb;

	  ova2 = affine_fn_plus (ova2, overlaps_a_yz);
	  ovb = affine_fn_plus (ovb, overlaps_b_yz);
	  affine_fn_free (t0);
	  affine_fn_free (t2);
2304 2305 2306 2307
	  *last_conflicts = last_conflicts_yz;
	}
      if (xyz_p)
	{
2308 2309 2310 2311 2312 2313 2314 2315 2316 2317
	  affine_fn t0 = ova1;
	  affine_fn t2 = ova2;
	  affine_fn t4 = ovb;

	  ova1 = affine_fn_plus (ova1, overlaps_a_xyz);
	  ova2 = affine_fn_plus (ova2, overlaps_a_xyz);
	  ovb = affine_fn_plus (ovb, overlaps_b_xyz);
	  affine_fn_free (t0);
	  affine_fn_free (t2);
	  affine_fn_free (t4);
2318 2319
	  *last_conflicts = last_conflicts_xyz;
	}
2320 2321
      *overlaps_a = conflict_fn (2, ova1, ova2);
      *overlaps_b = conflict_fn (1, ovb);
2322 2323 2324
    }
  else
    {
2325 2326
      *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
      *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2327 2328
      *last_conflicts = integer_zero_node;
    }
2329 2330 2331 2332 2333 2334 2335

  affine_fn_free (overlaps_a_xz);
  affine_fn_free (overlaps_b_xz);
  affine_fn_free (overlaps_a_yz);
  affine_fn_free (overlaps_b_yz);
  affine_fn_free (overlaps_a_xyz);
  affine_fn_free (overlaps_b_xyz);
2336 2337
}

2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477
/* Copy the elements of vector VEC1 with length SIZE to VEC2.  */

static void
lambda_vector_copy (lambda_vector vec1, lambda_vector vec2,
		    int size)
{
  memcpy (vec2, vec1, size * sizeof (*vec1));
}

/* Copy the elements of M x N matrix MAT1 to MAT2.  */

static void
lambda_matrix_copy (lambda_matrix mat1, lambda_matrix mat2,
		    int m, int n)
{
  int i;

  for (i = 0; i < m; i++)
    lambda_vector_copy (mat1[i], mat2[i], n);
}

/* Store the N x N identity matrix in MAT.  */

static void
lambda_matrix_id (lambda_matrix mat, int size)
{
  int i, j;

  for (i = 0; i < size; i++)
    for (j = 0; j < size; j++)
      mat[i][j] = (i == j) ? 1 : 0;
}

/* Return the first nonzero element of vector VEC1 between START and N.
   We must have START <= N.   Returns N if VEC1 is the zero vector.  */

static int
lambda_vector_first_nz (lambda_vector vec1, int n, int start)
{
  int j = start;
  while (j < n && vec1[j] == 0)
    j++;
  return j;
}

/* Add a multiple of row R1 of matrix MAT with N columns to row R2:
   R2 = R2 + CONST1 * R1.  */

static void
lambda_matrix_row_add (lambda_matrix mat, int n, int r1, int r2, int const1)
{
  int i;

  if (const1 == 0)
    return;

  for (i = 0; i < n; i++)
    mat[r2][i] += const1 * mat[r1][i];
}

/* Multiply vector VEC1 of length SIZE by a constant CONST1,
   and store the result in VEC2.  */

static void
lambda_vector_mult_const (lambda_vector vec1, lambda_vector vec2,
			  int size, int const1)
{
  int i;

  if (const1 == 0)
    lambda_vector_clear (vec2, size);
  else
    for (i = 0; i < size; i++)
      vec2[i] = const1 * vec1[i];
}

/* Negate vector VEC1 with length SIZE and store it in VEC2.  */

static void
lambda_vector_negate (lambda_vector vec1, lambda_vector vec2,
		      int size)
{
  lambda_vector_mult_const (vec1, vec2, size, -1);
}

/* Negate row R1 of matrix MAT which has N columns.  */

static void
lambda_matrix_row_negate (lambda_matrix mat, int n, int r1)
{
  lambda_vector_negate (mat[r1], mat[r1], n);
}

/* Return true if two vectors are equal.  */

static bool
lambda_vector_equal (lambda_vector vec1, lambda_vector vec2, int size)
{
  int i;
  for (i = 0; i < size; i++)
    if (vec1[i] != vec2[i])
      return false;
  return true;
}

/* Given an M x N integer matrix A, this function determines an M x
   M unimodular matrix U, and an M x N echelon matrix S such that
   "U.A = S".  This decomposition is also known as "right Hermite".

   Ref: Algorithm 2.1 page 33 in "Loop Transformations for
   Restructuring Compilers" Utpal Banerjee.  */

static void
lambda_matrix_right_hermite (lambda_matrix A, int m, int n,
			     lambda_matrix S, lambda_matrix U)
{
  int i, j, i0 = 0;

  lambda_matrix_copy (A, S, m, n);
  lambda_matrix_id (U, m);

  for (j = 0; j < n; j++)
    {
      if (lambda_vector_first_nz (S[j], m, i0) < m)
	{
	  ++i0;
	  for (i = m - 1; i >= i0; i--)
	    {
	      while (S[i][j] != 0)
		{
		  int sigma, factor, a, b;

		  a = S[i-1][j];
		  b = S[i][j];
		  sigma = (a * b < 0) ? -1: 1;
		  a = abs (a);
		  b = abs (b);
		  factor = sigma * (a / b);

		  lambda_matrix_row_add (S, n, i, i-1, -factor);
2478
		  std::swap (S[i], S[i-1]);
2479 2480

		  lambda_matrix_row_add (U, m, i, i-1, -factor);
2481
		  std::swap (U[i], U[i-1]);
2482 2483 2484 2485 2486 2487
		}
	    }
	}
    }
}

2488
/* Determines the overlapping elements due to accesses CHREC_A and
2489 2490 2491
   CHREC_B, that are affine functions.  This function cannot handle
   symbolic evolution functions, ie. when initial conditions are
   parameters, because it uses lambda matrices of integers.  */
2492 2493

static void
H.J. Lu committed
2494
analyze_subscript_affine_affine (tree chrec_a,
2495
				 tree chrec_b,
H.J. Lu committed
2496 2497
				 conflict_function **overlaps_a,
				 conflict_function **overlaps_b,
2498
				 tree *last_conflicts)
2499
{
2500
  unsigned nb_vars_a, nb_vars_b, dim;
2501
  HOST_WIDE_INT init_a, init_b, gamma, gcd_alpha_beta;
2502
  lambda_matrix A, U, S;
2503
  struct obstack scratch_obstack;
2504

2505
  if (eq_evolutions_p (chrec_a, chrec_b))
2506
    {
2507 2508
      /* The accessed index overlaps for each iteration in the
	 loop.  */
2509 2510
      *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
      *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2511 2512 2513
      *last_conflicts = chrec_dont_know;
      return;
    }
2514 2515
  if (dump_file && (dump_flags & TDF_DETAILS))
    fprintf (dump_file, "(analyze_subscript_affine_affine \n");
H.J. Lu committed
2516

2517 2518
  /* For determining the initial intersection, we have to solve a
     Diophantine equation.  This is the most time consuming part.
H.J. Lu committed
2519

2520 2521 2522
     For answering to the question: "Is there a dependence?" we have
     to prove that there exists a solution to the Diophantine
     equation, and that the solution is in the iteration domain,
2523
     i.e. the solution is positive or zero, and that the solution
2524 2525 2526 2527
     happens before the upper bound loop.nb_iterations.  Otherwise
     there is no dependence.  This function outputs a description of
     the iterations that hold the intersections.  */

2528 2529 2530
  nb_vars_a = nb_vars_in_chrec (chrec_a);
  nb_vars_b = nb_vars_in_chrec (chrec_b);

2531 2532
  gcc_obstack_init (&scratch_obstack);

2533
  dim = nb_vars_a + nb_vars_b;
2534 2535 2536
  U = lambda_matrix_new (dim, dim, &scratch_obstack);
  A = lambda_matrix_new (dim, 1, &scratch_obstack);
  S = lambda_matrix_new (dim, 1, &scratch_obstack);
2537

2538 2539
  init_a = int_cst_value (initialize_matrix_A (A, chrec_a, 0, 1));
  init_b = int_cst_value (initialize_matrix_A (A, chrec_b, nb_vars_a, -1));
2540 2541 2542
  gamma = init_b - init_a;

  /* Don't do all the hard work of solving the Diophantine equation
H.J. Lu committed
2543
     when we already know the solution: for example,
2544 2545 2546
     | {3, +, 1}_1
     | {3, +, 4}_2
     | gamma = 3 - 3 = 0.
H.J. Lu committed
2547
     Then the first overlap occurs during the first iterations:
2548 2549 2550
     | {3, +, 1}_1 ({0, +, 4}_x) = {3, +, 4}_2 ({0, +, 1}_x)
  */
  if (gamma == 0)
2551
    {
2552
      if (nb_vars_a == 1 && nb_vars_b == 1)
2553
	{
2554
	  HOST_WIDE_INT step_a, step_b;
2555
	  HOST_WIDE_INT niter, niter_a, niter_b;
2556
	  affine_fn ova, ovb;
2557

2558 2559
	  niter_a = max_stmt_executions_int (get_chrec_loop (chrec_a));
	  niter_b = max_stmt_executions_int (get_chrec_loop (chrec_b));
2560
	  niter = MIN (niter_a, niter_b);
2561 2562
	  step_a = int_cst_value (CHREC_RIGHT (chrec_a));
	  step_b = int_cst_value (CHREC_RIGHT (chrec_b));
2563

H.J. Lu committed
2564 2565
	  compute_overlap_steps_for_affine_univar (niter, step_a, step_b,
						   &ova, &ovb,
2566
						   last_conflicts, 1);
2567 2568
	  *overlaps_a = conflict_fn (1, ova);
	  *overlaps_b = conflict_fn (1, ovb);
2569
	}
2570 2571 2572 2573 2574 2575 2576 2577 2578 2579

      else if (nb_vars_a == 2 && nb_vars_b == 1)
	compute_overlap_steps_for_affine_1_2
	  (chrec_a, chrec_b, overlaps_a, overlaps_b, last_conflicts);

      else if (nb_vars_a == 1 && nb_vars_b == 2)
	compute_overlap_steps_for_affine_1_2
	  (chrec_b, chrec_a, overlaps_b, overlaps_a, last_conflicts);

      else
2580
	{
2581 2582
	  if (dump_file && (dump_flags & TDF_DETAILS))
	    fprintf (dump_file, "affine-affine test failed: too many variables.\n");
2583 2584
	  *overlaps_a = conflict_fn_not_known ();
	  *overlaps_b = conflict_fn_not_known ();
2585
	  *last_conflicts = chrec_dont_know;
2586
	}
2587
      goto end_analyze_subs_aa;
2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599
    }

  /* U.A = S */
  lambda_matrix_right_hermite (A, dim, 1, S, U);

  if (S[0][0] < 0)
    {
      S[0][0] *= -1;
      lambda_matrix_row_negate (U, dim, 0);
    }
  gcd_alpha_beta = S[0][0];

2600 2601 2602 2603 2604
  /* Something went wrong: for example in {1, +, 0}_5 vs. {0, +, 0}_5,
     but that is a quite strange case.  Instead of ICEing, answer
     don't know.  */
  if (gcd_alpha_beta == 0)
    {
2605 2606
      *overlaps_a = conflict_fn_not_known ();
      *overlaps_b = conflict_fn_not_known ();
2607 2608 2609 2610
      *last_conflicts = chrec_dont_know;
      goto end_analyze_subs_aa;
    }

2611 2612 2613 2614 2615
  /* The classic "gcd-test".  */
  if (!int_divides_p (gcd_alpha_beta, gamma))
    {
      /* The "gcd-test" has determined that there is no integer
	 solution, i.e. there is no dependence.  */
2616 2617
      *overlaps_a = conflict_fn_no_dependence ();
      *overlaps_b = conflict_fn_no_dependence ();
2618 2619 2620 2621 2622 2623 2624 2625 2626
      *last_conflicts = integer_zero_node;
    }

  /* Both access functions are univariate.  This includes SIV and MIV cases.  */
  else if (nb_vars_a == 1 && nb_vars_b == 1)
    {
      /* Both functions should have the same evolution sign.  */
      if (((A[0][0] > 0 && -A[1][0] > 0)
	   || (A[0][0] < 0 && -A[1][0] < 0)))
2627 2628
	{
	  /* The solutions are given by:
H.J. Lu committed
2629
	     |
2630 2631
	     | [GAMMA/GCD_ALPHA_BETA  t].[u11 u12]  = [x0]
	     |                           [u21 u22]    [y0]
H.J. Lu committed
2632

2633
	     For a given integer t.  Using the following variables,
H.J. Lu committed
2634

2635 2636 2637 2638
	     | i0 = u11 * gamma / gcd_alpha_beta
	     | j0 = u12 * gamma / gcd_alpha_beta
	     | i1 = u21
	     | j1 = u22
H.J. Lu committed
2639

2640
	     the solutions are:
H.J. Lu committed
2641 2642

	     | x0 = i0 + i1 * t,
2643
	     | y0 = j0 + j1 * t.  */
2644
      	  HOST_WIDE_INT i0, j0, i1, j1;
2645 2646 2647 2648 2649 2650 2651 2652

	  i0 = U[0][0] * gamma / gcd_alpha_beta;
	  j0 = U[0][1] * gamma / gcd_alpha_beta;
	  i1 = U[1][0];
	  j1 = U[1][1];

	  if ((i1 == 0 && i0 < 0)
	      || (j1 == 0 && j0 < 0))
2653
	    {
H.J. Lu committed
2654 2655 2656
	      /* There is no solution.
		 FIXME: The case "i0 > nb_iterations, j0 > nb_iterations"
		 falls in here, but for the moment we don't look at the
2657
		 upper bound of the iteration domain.  */
2658 2659
	      *overlaps_a = conflict_fn_no_dependence ();
	      *overlaps_b = conflict_fn_no_dependence ();
2660
	      *last_conflicts = integer_zero_node;
2661
	      goto end_analyze_subs_aa;
2662 2663
	    }

2664
	  if (i1 > 0 && j1 > 0)
2665
	    {
2666 2667 2668 2669
	      HOST_WIDE_INT niter_a
		= max_stmt_executions_int (get_chrec_loop (chrec_a));
	      HOST_WIDE_INT niter_b
		= max_stmt_executions_int (get_chrec_loop (chrec_b));
2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686
	      HOST_WIDE_INT niter = MIN (niter_a, niter_b);

	      /* (X0, Y0) is a solution of the Diophantine equation:
		 "chrec_a (X0) = chrec_b (Y0)".  */
	      HOST_WIDE_INT tau1 = MAX (CEIL (-i0, i1),
					CEIL (-j0, j1));
	      HOST_WIDE_INT x0 = i1 * tau1 + i0;
	      HOST_WIDE_INT y0 = j1 * tau1 + j0;

	      /* (X1, Y1) is the smallest positive solution of the eq
		 "chrec_a (X1) = chrec_b (Y1)", i.e. this is where the
		 first conflict occurs.  */
	      HOST_WIDE_INT min_multiple = MIN (x0 / i1, y0 / j1);
	      HOST_WIDE_INT x1 = x0 - i1 * min_multiple;
	      HOST_WIDE_INT y1 = y0 - j1 * min_multiple;

	      if (niter > 0)
2687
		{
2688 2689 2690
		  HOST_WIDE_INT tau2 = MIN (FLOOR_DIV (niter - i0, i1),
					    FLOOR_DIV (niter - j0, j1));
		  HOST_WIDE_INT last_conflict = tau2 - (x1 - i0)/i1;
2691

2692 2693
		  /* If the overlap occurs outside of the bounds of the
		     loop, there is no dependence.  */
2694
		  if (x1 >= niter || y1 >= niter)
2695
		    {
2696 2697 2698 2699
		      *overlaps_a = conflict_fn_no_dependence ();
		      *overlaps_b = conflict_fn_no_dependence ();
		      *last_conflicts = integer_zero_node;
		      goto end_analyze_subs_aa;
2700 2701
		    }
		  else
2702
		    *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2703 2704
		}
	      else
2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726
		*last_conflicts = chrec_dont_know;

	      *overlaps_a
		= conflict_fn (1,
			       affine_fn_univar (build_int_cst (NULL_TREE, x1),
						 1,
						 build_int_cst (NULL_TREE, i1)));
	      *overlaps_b
		= conflict_fn (1,
			       affine_fn_univar (build_int_cst (NULL_TREE, y1),
						 1,
						 build_int_cst (NULL_TREE, j1)));
	    }
	  else
	    {
	      /* FIXME: For the moment, the upper bound of the
		 iteration domain for i and j is not checked.  */
	      if (dump_file && (dump_flags & TDF_DETAILS))
		fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
	      *overlaps_a = conflict_fn_not_known ();
	      *overlaps_b = conflict_fn_not_known ();
	      *last_conflicts = chrec_dont_know;
2727 2728
	    }
	}
2729 2730
      else
	{
2731 2732
	  if (dump_file && (dump_flags & TDF_DETAILS))
	    fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2733 2734
	  *overlaps_a = conflict_fn_not_known ();
	  *overlaps_b = conflict_fn_not_known ();
2735 2736
	  *last_conflicts = chrec_dont_know;
	}
2737 2738 2739
    }
  else
    {
2740 2741
      if (dump_file && (dump_flags & TDF_DETAILS))
	fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2742 2743
      *overlaps_a = conflict_fn_not_known ();
      *overlaps_b = conflict_fn_not_known ();
2744
      *last_conflicts = chrec_dont_know;
2745
    }
2746

H.J. Lu committed
2747
end_analyze_subs_aa:
2748
  obstack_free (&scratch_obstack, NULL);
2749 2750 2751
  if (dump_file && (dump_flags & TDF_DETAILS))
    {
      fprintf (dump_file, "  (overlaps_a = ");
2752
      dump_conflict_function (dump_file, *overlaps_a);
2753
      fprintf (dump_file, ")\n  (overlaps_b = ");
2754
      dump_conflict_function (dump_file, *overlaps_b);
2755
      fprintf (dump_file, "))\n");
2756
    }
2757 2758 2759 2760 2761 2762
}

/* Returns true when analyze_subscript_affine_affine can be used for
   determining the dependence relation between chrec_a and chrec_b,
   that contain symbols.  This function modifies chrec_a and chrec_b
   such that the analysis result is the same, and such that they don't
H.J. Lu committed
2763
   contain symbols, and then can safely be passed to the analyzer.
2764 2765 2766 2767

   Example: The analysis of the following tuples of evolutions produce
   the same results: {x+1, +, 1}_1 vs. {x+3, +, 1}_1, and {-2, +, 1}_1
   vs. {0, +, 1}_1
H.J. Lu committed
2768

2769 2770 2771 2772 2773 2774 2775
   {x+1, +, 1}_1 ({2, +, 1}_1) = {x+3, +, 1}_1 ({0, +, 1}_1)
   {-2, +, 1}_1 ({2, +, 1}_1) = {0, +, 1}_1 ({0, +, 1}_1)
*/

static bool
can_use_analyze_subscript_affine_affine (tree *chrec_a, tree *chrec_b)
{
2776
  tree diff, type, left_a, left_b, right_b;
2777 2778 2779 2780 2781 2782

  if (chrec_contains_symbols (CHREC_RIGHT (*chrec_a))
      || chrec_contains_symbols (CHREC_RIGHT (*chrec_b)))
    /* FIXME: For the moment not handled.  Might be refined later.  */
    return false;

2783 2784
  type = chrec_type (*chrec_a);
  left_a = CHREC_LEFT (*chrec_a);
2785
  left_b = chrec_convert (type, CHREC_LEFT (*chrec_b), NULL);
2786 2787
  diff = chrec_fold_minus (type, left_a, left_b);

2788 2789 2790
  if (!evolution_function_is_constant_p (diff))
    return false;

2791
  if (dump_file && (dump_flags & TDF_DETAILS))
2792 2793
    fprintf (dump_file, "can_use_subscript_aff_aff_for_symbolic \n");

H.J. Lu committed
2794
  *chrec_a = build_polynomial_chrec (CHREC_VARIABLE (*chrec_a),
2795
				     diff, CHREC_RIGHT (*chrec_a));
2796
  right_b = chrec_convert (type, CHREC_RIGHT (*chrec_b), NULL);
2797
  *chrec_b = build_polynomial_chrec (CHREC_VARIABLE (*chrec_b),
2798
				     build_int_cst (type, 0),
2799
				     right_b);
2800
  return true;
2801 2802 2803 2804 2805 2806 2807 2808 2809 2810
}

/* Analyze a SIV (Single Index Variable) subscript.  *OVERLAPS_A and
   *OVERLAPS_B are initialized to the functions that describe the
   relation between the elements accessed twice by CHREC_A and
   CHREC_B.  For k >= 0, the following property is verified:

   CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)).  */

static void
H.J. Lu committed
2811
analyze_siv_subscript (tree chrec_a,
2812
		       tree chrec_b,
H.J. Lu committed
2813 2814
		       conflict_function **overlaps_a,
		       conflict_function **overlaps_b,
2815 2816
		       tree *last_conflicts,
		       int loop_nest_num)
2817
{
2818
  dependence_stats.num_siv++;
H.J. Lu committed
2819

2820 2821
  if (dump_file && (dump_flags & TDF_DETAILS))
    fprintf (dump_file, "(analyze_siv_subscript \n");
H.J. Lu committed
2822

2823
  if (evolution_function_is_constant_p (chrec_a)
2824
      && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
H.J. Lu committed
2825
    analyze_siv_subscript_cst_affine (chrec_a, chrec_b,
2826
				      overlaps_a, overlaps_b, last_conflicts);
H.J. Lu committed
2827

2828
  else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
2829
	   && evolution_function_is_constant_p (chrec_b))
H.J. Lu committed
2830
    analyze_siv_subscript_cst_affine (chrec_b, chrec_a,
2831
				      overlaps_b, overlaps_a, last_conflicts);
H.J. Lu committed
2832

2833 2834
  else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
	   && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
2835 2836 2837 2838
    {
      if (!chrec_contains_symbols (chrec_a)
	  && !chrec_contains_symbols (chrec_b))
	{
H.J. Lu committed
2839 2840
	  analyze_subscript_affine_affine (chrec_a, chrec_b,
					   overlaps_a, overlaps_b,
2841 2842
					   last_conflicts);

2843 2844
	  if (CF_NOT_KNOWN_P (*overlaps_a)
	      || CF_NOT_KNOWN_P (*overlaps_b))
2845
	    dependence_stats.num_siv_unimplemented++;
2846 2847
	  else if (CF_NO_DEPENDENCE_P (*overlaps_a)
		   || CF_NO_DEPENDENCE_P (*overlaps_b))
2848 2849 2850 2851
	    dependence_stats.num_siv_independent++;
	  else
	    dependence_stats.num_siv_dependent++;
	}
H.J. Lu committed
2852
      else if (can_use_analyze_subscript_affine_affine (&chrec_a,
2853 2854
							&chrec_b))
	{
H.J. Lu committed
2855 2856
	  analyze_subscript_affine_affine (chrec_a, chrec_b,
					   overlaps_a, overlaps_b,
2857 2858
					   last_conflicts);

2859 2860
	  if (CF_NOT_KNOWN_P (*overlaps_a)
	      || CF_NOT_KNOWN_P (*overlaps_b))
2861
	    dependence_stats.num_siv_unimplemented++;
2862 2863
	  else if (CF_NO_DEPENDENCE_P (*overlaps_a)
		   || CF_NO_DEPENDENCE_P (*overlaps_b))
2864 2865 2866 2867 2868 2869 2870 2871
	    dependence_stats.num_siv_independent++;
	  else
	    dependence_stats.num_siv_dependent++;
	}
      else
	goto siv_subscript_dontknow;
    }

2872 2873
  else
    {
2874 2875
    siv_subscript_dontknow:;
      if (dump_file && (dump_flags & TDF_DETAILS))
2876
	fprintf (dump_file, "  siv test failed: unimplemented");
2877 2878
      *overlaps_a = conflict_fn_not_known ();
      *overlaps_b = conflict_fn_not_known ();
2879
      *last_conflicts = chrec_dont_know;
2880
      dependence_stats.num_siv_unimplemented++;
2881
    }
H.J. Lu committed
2882

2883 2884 2885 2886
  if (dump_file && (dump_flags & TDF_DETAILS))
    fprintf (dump_file, ")\n");
}

2887 2888
/* Returns false if we can prove that the greatest common divisor of the steps
   of CHREC does not divide CST, false otherwise.  */
2889 2890

static bool
2891
gcd_of_steps_may_divide_p (const_tree chrec, const_tree cst)
2892
{
2893 2894
  HOST_WIDE_INT cd = 0, val;
  tree step;
2895

2896
  if (!tree_fits_shwi_p (cst))
2897
    return true;
2898
  val = tree_to_shwi (cst);
2899 2900 2901 2902

  while (TREE_CODE (chrec) == POLYNOMIAL_CHREC)
    {
      step = CHREC_RIGHT (chrec);
2903
      if (!tree_fits_shwi_p (step))
2904
	return true;
2905
      cd = gcd (cd, tree_to_shwi (step));
2906
      chrec = CHREC_LEFT (chrec);
2907
    }
2908 2909

  return val % cd == 0;
2910 2911
}

2912 2913 2914 2915 2916
/* Analyze a MIV (Multiple Index Variable) subscript with respect to
   LOOP_NEST.  *OVERLAPS_A and *OVERLAPS_B are initialized to the
   functions that describe the relation between the elements accessed
   twice by CHREC_A and CHREC_B.  For k >= 0, the following property
   is verified:
2917 2918 2919 2920

   CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)).  */

static void
H.J. Lu committed
2921 2922 2923 2924
analyze_miv_subscript (tree chrec_a,
		       tree chrec_b,
		       conflict_function **overlaps_a,
		       conflict_function **overlaps_b,
2925 2926
		       tree *last_conflicts,
		       struct loop *loop_nest)
2927
{
2928 2929
  tree type, difference;

2930
  dependence_stats.num_miv++;
2931 2932
  if (dump_file && (dump_flags & TDF_DETAILS))
    fprintf (dump_file, "(analyze_miv_subscript \n");
2933

2934
  type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
2935 2936
  chrec_a = chrec_convert (type, chrec_a, NULL);
  chrec_b = chrec_convert (type, chrec_b, NULL);
2937
  difference = chrec_fold_minus (type, chrec_a, chrec_b);
H.J. Lu committed
2938

2939
  if (eq_evolutions_p (chrec_a, chrec_b))
2940 2941 2942
    {
      /* Access functions are the same: all the elements are accessed
	 in the same order.  */
2943 2944
      *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
      *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2945
      *last_conflicts = max_stmt_executions_tree (get_chrec_loop (chrec_a));
2946
      dependence_stats.num_miv_dependent++;
2947
    }
H.J. Lu committed
2948

2949 2950
  else if (evolution_function_is_constant_p (difference)
	   /* For the moment, the following is verified:
2951 2952
	      evolution_function_is_affine_multivariate_p (chrec_a,
	      loop_nest->num) */
2953
	   && !gcd_of_steps_may_divide_p (chrec_a, difference))
2954 2955
    {
      /* testsuite/.../ssa-chrec-33.c
H.J. Lu committed
2956 2957
	 {{21, +, 2}_1, +, -2}_2  vs.  {{20, +, 2}_1, +, -2}_2

2958 2959
	 The difference is 1, and all the evolution steps are multiples
	 of 2, consequently there are no overlapping elements.  */
2960 2961
      *overlaps_a = conflict_fn_no_dependence ();
      *overlaps_b = conflict_fn_no_dependence ();
2962
      *last_conflicts = integer_zero_node;
2963
      dependence_stats.num_miv_independent++;
2964
    }
H.J. Lu committed
2965

2966
  else if (evolution_function_is_affine_multivariate_p (chrec_a, loop_nest->num)
2967
	   && !chrec_contains_symbols (chrec_a)
2968
	   && evolution_function_is_affine_multivariate_p (chrec_b, loop_nest->num)
2969
	   && !chrec_contains_symbols (chrec_b))
2970 2971 2972 2973
    {
      /* testsuite/.../ssa-chrec-35.c
	 {0, +, 1}_2  vs.  {0, +, 1}_3
	 the overlapping elements are respectively located at iterations:
H.J. Lu committed
2974 2975
	 {0, +, 1}_x and {0, +, 1}_x,
	 in other words, we have the equality:
2976
	 {0, +, 1}_2 ({0, +, 1}_x) = {0, +, 1}_3 ({0, +, 1}_x)
H.J. Lu committed
2977 2978 2979

	 Other examples:
	 {{0, +, 1}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y) =
2980 2981
	 {0, +, 1}_1 ({{0, +, 1}_x, +, 2}_y)

H.J. Lu committed
2982
	 {{0, +, 2}_1, +, 3}_2 ({0, +, 1}_y, {0, +, 1}_x) =
2983
	 {{0, +, 3}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y)
2984
      */
H.J. Lu committed
2985
      analyze_subscript_affine_affine (chrec_a, chrec_b,
2986
				       overlaps_a, overlaps_b, last_conflicts);
2987

2988 2989
      if (CF_NOT_KNOWN_P (*overlaps_a)
 	  || CF_NOT_KNOWN_P (*overlaps_b))
2990
	dependence_stats.num_miv_unimplemented++;
2991 2992
      else if (CF_NO_DEPENDENCE_P (*overlaps_a)
	       || CF_NO_DEPENDENCE_P (*overlaps_b))
2993 2994 2995
	dependence_stats.num_miv_independent++;
      else
	dependence_stats.num_miv_dependent++;
2996
    }
H.J. Lu committed
2997

2998 2999 3000
  else
    {
      /* When the analysis is too difficult, answer "don't know".  */
3001 3002 3003
      if (dump_file && (dump_flags & TDF_DETAILS))
	fprintf (dump_file, "analyze_miv_subscript test failed: unimplemented.\n");

3004 3005
      *overlaps_a = conflict_fn_not_known ();
      *overlaps_b = conflict_fn_not_known ();
3006
      *last_conflicts = chrec_dont_know;
3007
      dependence_stats.num_miv_unimplemented++;
3008
    }
H.J. Lu committed
3009

3010 3011 3012 3013
  if (dump_file && (dump_flags & TDF_DETAILS))
    fprintf (dump_file, ")\n");
}

3014 3015 3016 3017
/* Determines the iterations for which CHREC_A is equal to CHREC_B in
   with respect to LOOP_NEST.  OVERLAP_ITERATIONS_A and
   OVERLAP_ITERATIONS_B are initialized with two functions that
   describe the iterations that contain conflicting elements.
H.J. Lu committed
3018

3019
   Remark: For an integer k >= 0, the following equality is true:
H.J. Lu committed
3020

3021 3022 3023
   CHREC_A (OVERLAP_ITERATIONS_A (k)) == CHREC_B (OVERLAP_ITERATIONS_B (k)).
*/

H.J. Lu committed
3024 3025 3026 3027 3028
static void
analyze_overlapping_iterations (tree chrec_a,
				tree chrec_b,
				conflict_function **overlap_iterations_a,
				conflict_function **overlap_iterations_b,
3029
				tree *last_conflicts, struct loop *loop_nest)
3030
{
3031 3032
  unsigned int lnn = loop_nest->num;

3033
  dependence_stats.num_subscript_tests++;
H.J. Lu committed
3034

3035 3036 3037 3038 3039
  if (dump_file && (dump_flags & TDF_DETAILS))
    {
      fprintf (dump_file, "(analyze_overlapping_iterations \n");
      fprintf (dump_file, "  (chrec_a = ");
      print_generic_expr (dump_file, chrec_a, 0);
3040
      fprintf (dump_file, ")\n  (chrec_b = ");
3041 3042 3043
      print_generic_expr (dump_file, chrec_b, 0);
      fprintf (dump_file, ")\n");
    }
3044

3045 3046 3047
  if (chrec_a == NULL_TREE
      || chrec_b == NULL_TREE
      || chrec_contains_undetermined (chrec_a)
3048
      || chrec_contains_undetermined (chrec_b))
3049
    {
3050
      dependence_stats.num_subscript_undetermined++;
H.J. Lu committed
3051

3052 3053
      *overlap_iterations_a = conflict_fn_not_known ();
      *overlap_iterations_b = conflict_fn_not_known ();
3054
    }
3055

H.J. Lu committed
3056
  /* If they are the same chrec, and are affine, they overlap
3057 3058
     on every iteration.  */
  else if (eq_evolutions_p (chrec_a, chrec_b)
3059 3060
	   && (evolution_function_is_affine_multivariate_p (chrec_a, lnn)
	       || operand_equal_p (chrec_a, chrec_b, 0)))
3061 3062
    {
      dependence_stats.num_same_subscript_function++;
3063 3064
      *overlap_iterations_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
      *overlap_iterations_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
3065 3066 3067 3068
      *last_conflicts = chrec_dont_know;
    }

  /* If they aren't the same, and aren't affine, we can't do anything
3069
     yet.  */
H.J. Lu committed
3070
  else if ((chrec_contains_symbols (chrec_a)
3071
	    || chrec_contains_symbols (chrec_b))
3072 3073
	   && (!evolution_function_is_affine_multivariate_p (chrec_a, lnn)
	       || !evolution_function_is_affine_multivariate_p (chrec_b, lnn)))
3074 3075
    {
      dependence_stats.num_subscript_undetermined++;
3076 3077
      *overlap_iterations_a = conflict_fn_not_known ();
      *overlap_iterations_b = conflict_fn_not_known ();
3078 3079
    }

3080
  else if (ziv_subscript_p (chrec_a, chrec_b))
H.J. Lu committed
3081
    analyze_ziv_subscript (chrec_a, chrec_b,
3082 3083
			   overlap_iterations_a, overlap_iterations_b,
			   last_conflicts);
H.J. Lu committed
3084

3085
  else if (siv_subscript_p (chrec_a, chrec_b))
H.J. Lu committed
3086 3087
    analyze_siv_subscript (chrec_a, chrec_b,
			   overlap_iterations_a, overlap_iterations_b,
3088
			   last_conflicts, lnn);
H.J. Lu committed
3089

3090
  else
H.J. Lu committed
3091
    analyze_miv_subscript (chrec_a, chrec_b,
3092
			   overlap_iterations_a, overlap_iterations_b,
3093
			   last_conflicts, loop_nest);
H.J. Lu committed
3094

3095 3096 3097
  if (dump_file && (dump_flags & TDF_DETAILS))
    {
      fprintf (dump_file, "  (overlap_iterations_a = ");
3098
      dump_conflict_function (dump_file, *overlap_iterations_a);
3099
      fprintf (dump_file, ")\n  (overlap_iterations_b = ");
3100
      dump_conflict_function (dump_file, *overlap_iterations_b);
3101
      fprintf (dump_file, "))\n");
3102 3103 3104
    }
}

3105
/* Helper function for uniquely inserting distance vectors.  */
3106

3107 3108 3109 3110 3111
static void
save_dist_v (struct data_dependence_relation *ddr, lambda_vector dist_v)
{
  unsigned i;
  lambda_vector v;
3112

3113
  FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, v)
3114 3115
    if (lambda_vector_equal (v, dist_v, DDR_NB_LOOPS (ddr)))
      return;
3116

3117
  DDR_DIST_VECTS (ddr).safe_push (dist_v);
3118
}
3119

3120 3121 3122 3123
/* Helper function for uniquely inserting direction vectors.  */

static void
save_dir_v (struct data_dependence_relation *ddr, lambda_vector dir_v)
3124 3125
{
  unsigned i;
3126
  lambda_vector v;
3127

3128
  FOR_EACH_VEC_ELT (DDR_DIR_VECTS (ddr), i, v)
3129 3130 3131
    if (lambda_vector_equal (v, dir_v, DDR_NB_LOOPS (ddr)))
      return;

3132
  DDR_DIR_VECTS (ddr).safe_push (dir_v);
3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177
}

/* Add a distance of 1 on all the loops outer than INDEX.  If we
   haven't yet determined a distance for this outer loop, push a new
   distance vector composed of the previous distance, and a distance
   of 1 for this outer loop.  Example:

   | loop_1
   |   loop_2
   |     A[10]
   |   endloop_2
   | endloop_1

   Saved vectors are of the form (dist_in_1, dist_in_2).  First, we
   save (0, 1), then we have to save (1, 0).  */

static void
add_outer_distances (struct data_dependence_relation *ddr,
		     lambda_vector dist_v, int index)
{
  /* For each outer loop where init_v is not set, the accesses are
     in dependence of distance 1 in the loop.  */
  while (--index >= 0)
    {
      lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
      lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
      save_v[index] = 1;
      save_dist_v (ddr, save_v);
    }
}

/* Return false when fail to represent the data dependence as a
   distance vector.  INIT_B is set to true when a component has been
   added to the distance vector DIST_V.  INDEX_CARRY is then set to
   the index in DIST_V that carries the dependence.  */

static bool
build_classic_dist_vector_1 (struct data_dependence_relation *ddr,
			     struct data_reference *ddr_a,
			     struct data_reference *ddr_b,
			     lambda_vector dist_v, bool *init_b,
			     int *index_carry)
{
  unsigned i;
  lambda_vector init_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3178

Daniel Berlin committed
3179
  for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3180
    {
3181
      tree access_fn_a, access_fn_b;
Daniel Berlin committed
3182
      struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
3183 3184

      if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3185 3186
	{
	  non_affine_dependence_relation (ddr);
3187
	  return false;
3188 3189
	}

3190 3191
      access_fn_a = DR_ACCESS_FN (ddr_a, i);
      access_fn_b = DR_ACCESS_FN (ddr_b, i);
3192

H.J. Lu committed
3193
      if (TREE_CODE (access_fn_a) == POLYNOMIAL_CHREC
3194
	  && TREE_CODE (access_fn_b) == POLYNOMIAL_CHREC)
3195
	{
3196
	  int dist, index;
3197 3198
	  int var_a = CHREC_VARIABLE (access_fn_a);
	  int var_b = CHREC_VARIABLE (access_fn_b);
3199

3200 3201
	  if (var_a != var_b
	      || chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3202 3203
	    {
	      non_affine_dependence_relation (ddr);
3204
	      return false;
3205
	    }
H.J. Lu committed
3206

3207
	  dist = int_cst_value (SUB_DISTANCE (subscript));
3208 3209
	  index = index_in_loop_nest (var_a, DDR_LOOP_NEST (ddr));
	  *index_carry = MIN (index, *index_carry);
3210

3211 3212 3213 3214 3215
	  /* This is the subscript coupling test.  If we have already
	     recorded a distance for this loop (a distance coming from
	     another subscript), it should be the same.  For example,
	     in the following code, there is no dependence:

3216 3217 3218 3219
	     | loop i = 0, N, 1
	     |   T[i+1][i] = ...
	     |   ... = T[i][i]
	     | endloop
3220 3221
	  */
	  if (init_v[index] != 0 && dist_v[index] != dist)
3222
	    {
Daniel Berlin committed
3223
	      finalize_ddr_dependent (ddr, chrec_known);
3224
	      return false;
3225 3226
	    }

3227 3228 3229 3230
	  dist_v[index] = dist;
	  init_v[index] = 1;
	  *init_b = true;
	}
3231
      else if (!operand_equal_p (access_fn_a, access_fn_b, 0))
3232 3233 3234 3235 3236 3237
	{
	  /* This can be for example an affine vs. constant dependence
	     (T[i] vs. T[3]) that is not an affine dependence and is
	     not representable as a distance vector.  */
	  non_affine_dependence_relation (ddr);
	  return false;
3238 3239
	}
    }
3240

3241 3242
  return true;
}
3243

3244 3245 3246
/* Return true when the DDR contains only constant access functions.  */

static bool
3247
constant_access_functions (const struct data_dependence_relation *ddr)
3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258
{
  unsigned i;

  for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
    if (!evolution_function_is_constant_p (DR_ACCESS_FN (DDR_A (ddr), i))
	|| !evolution_function_is_constant_p (DR_ACCESS_FN (DDR_B (ddr), i)))
      return false;

  return true;
}

3259
/* Helper function for the case where DDR_A and DDR_B are the same
3260 3261
   multivariate access function with a constant step.  For an example
   see pr34635-1.c.  */
3262

3263 3264 3265 3266 3267 3268 3269
static void
add_multivariate_self_dist (struct data_dependence_relation *ddr, tree c_2)
{
  int x_1, x_2;
  tree c_1 = CHREC_LEFT (c_2);
  tree c_0 = CHREC_LEFT (c_1);
  lambda_vector dist_v;
3270
  int v1, v2, cd;
3271

3272 3273 3274 3275 3276 3277 3278 3279
  /* Polynomials with more than 2 variables are not handled yet.  When
     the evolution steps are parameters, it is not possible to
     represent the dependence using classical distance vectors.  */
  if (TREE_CODE (c_0) != INTEGER_CST
      || TREE_CODE (CHREC_RIGHT (c_1)) != INTEGER_CST
      || TREE_CODE (CHREC_RIGHT (c_2)) != INTEGER_CST)
    {
      DDR_AFFINE_P (ddr) = false;
3280 3281
      return;
    }
3282

3283 3284
  x_2 = index_in_loop_nest (CHREC_VARIABLE (c_2), DDR_LOOP_NEST (ddr));
  x_1 = index_in_loop_nest (CHREC_VARIABLE (c_1), DDR_LOOP_NEST (ddr));
3285

3286 3287
  /* For "{{0, +, 2}_1, +, 3}_2" the distance vector is (3, -2).  */
  dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3288 3289
  v1 = int_cst_value (CHREC_RIGHT (c_1));
  v2 = int_cst_value (CHREC_RIGHT (c_2));
3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301
  cd = gcd (v1, v2);
  v1 /= cd;
  v2 /= cd;

  if (v2 < 0)
    {
      v2 = -v2;
      v1 = -v1;
    }

  dist_v[x_1] = v2;
  dist_v[x_2] = -v1;
3302
  save_dist_v (ddr, dist_v);
3303

3304 3305
  add_outer_distances (ddr, dist_v, x_1);
}
3306

3307 3308
/* Helper function for the case where DDR_A and DDR_B are the same
   access functions.  */
3309

3310 3311 3312 3313 3314 3315
static void
add_other_self_distances (struct data_dependence_relation *ddr)
{
  lambda_vector dist_v;
  unsigned i;
  int index_carry = DDR_NB_LOOPS (ddr);
3316

3317
  for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3318
    {
3319
      tree access_fun = DR_ACCESS_FN (DDR_A (ddr), i);
3320

3321
      if (TREE_CODE (access_fun) == POLYNOMIAL_CHREC)
3322
	{
3323 3324 3325 3326 3327 3328 3329 3330
	  if (!evolution_function_is_univariate_p (access_fun))
	    {
	      if (DDR_NUM_SUBSCRIPTS (ddr) != 1)
		{
		  DDR_ARE_DEPENDENT (ddr) = chrec_dont_know;
		  return;
		}

3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341
	      access_fun = DR_ACCESS_FN (DDR_A (ddr), 0);

	      if (TREE_CODE (CHREC_LEFT (access_fun)) == POLYNOMIAL_CHREC)
		add_multivariate_self_dist (ddr, access_fun);
	      else
		/* The evolution step is not constant: it varies in
		   the outer loop, so this cannot be represented by a
		   distance vector.  For example in pr34635.c the
		   evolution is {0, +, {0, +, 4}_1}_2.  */
		DDR_AFFINE_P (ddr) = false;

3342 3343 3344 3345 3346 3347
	      return;
	    }

	  index_carry = MIN (index_carry,
			     index_in_loop_nest (CHREC_VARIABLE (access_fun),
						 DDR_LOOP_NEST (ddr)));
3348
	}
3349 3350
    }

3351 3352
  dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
  add_outer_distances (ddr, dist_v, index_carry);
3353 3354
}

3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401
static void
insert_innermost_unit_dist_vector (struct data_dependence_relation *ddr)
{
  lambda_vector dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));

  dist_v[DDR_INNER_LOOP (ddr)] = 1;
  save_dist_v (ddr, dist_v);
}

/* Adds a unit distance vector to DDR when there is a 0 overlap.  This
   is the case for example when access functions are the same and
   equal to a constant, as in:

   | loop_1
   |   A[3] = ...
   |   ... = A[3]
   | endloop_1

   in which case the distance vectors are (0) and (1).  */

static void
add_distance_for_zero_overlaps (struct data_dependence_relation *ddr)
{
  unsigned i, j;

  for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
    {
      subscript_p sub = DDR_SUBSCRIPT (ddr, i);
      conflict_function *ca = SUB_CONFLICTS_IN_A (sub);
      conflict_function *cb = SUB_CONFLICTS_IN_B (sub);

      for (j = 0; j < ca->n; j++)
	if (affine_function_zero_p (ca->fns[j]))
	  {
	    insert_innermost_unit_dist_vector (ddr);
	    return;
	  }

      for (j = 0; j < cb->n; j++)
	if (affine_function_zero_p (cb->fns[j]))
	  {
	    insert_innermost_unit_dist_vector (ddr);
	    return;
	  }
    }
}

3402 3403 3404
/* Compute the classic per loop distance vector.  DDR is the data
   dependence relation to build a vector from.  Return false when fail
   to represent the data dependence as a distance vector.  */
3405

3406
static bool
3407 3408
build_classic_dist_vector (struct data_dependence_relation *ddr,
			   struct loop *loop_nest)
3409
{
3410
  bool init_b = false;
3411 3412
  int index_carry = DDR_NB_LOOPS (ddr);
  lambda_vector dist_v;
3413

Daniel Berlin committed
3414
  if (DDR_ARE_DEPENDENT (ddr) != NULL_TREE)
3415
    return false;
3416 3417

  if (same_access_functions (ddr))
3418
    {
3419 3420 3421
      /* Save the 0 vector.  */
      dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
      save_dist_v (ddr, dist_v);
3422

3423 3424 3425
      if (constant_access_functions (ddr))
	add_distance_for_zero_overlaps (ddr);

3426 3427
      if (DDR_NB_LOOPS (ddr) > 1)
	add_other_self_distances (ddr);
3428

3429 3430
      return true;
    }
3431

3432 3433 3434 3435
  dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
  if (!build_classic_dist_vector_1 (ddr, DDR_A (ddr), DDR_B (ddr),
				    dist_v, &init_b, &index_carry))
    return false;
3436

3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466
  /* Save the distance vector if we initialized one.  */
  if (init_b)
    {
      /* Verify a basic constraint: classic distance vectors should
	 always be lexicographically positive.

	 Data references are collected in the order of execution of
	 the program, thus for the following loop

	 | for (i = 1; i < 100; i++)
	 |   for (j = 1; j < 100; j++)
	 |     {
	 |       t = T[j+1][i-1];  // A
	 |       T[j][i] = t + 2;  // B
	 |     }

	 references are collected following the direction of the wind:
	 A then B.  The data dependence tests are performed also
	 following this order, such that we're looking at the distance
	 separating the elements accessed by A from the elements later
	 accessed by B.  But in this example, the distance returned by
	 test_dep (A, B) is lexicographically negative (-1, 1), that
	 means that the access A occurs later than B with respect to
	 the outer loop, ie. we're actually looking upwind.  In this
	 case we solve test_dep (B, A) looking downwind to the
	 lexicographically positive solution, that returns the
	 distance vector (1, -1).  */
      if (!lambda_vector_lexico_pos (dist_v, DDR_NB_LOOPS (ddr)))
	{
	  lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3467 3468 3469
	  if (!subscript_dependence_tester_1 (ddr, DDR_B (ddr), DDR_A (ddr),
					      loop_nest))
	    return false;
3470
	  compute_subscript_distance (ddr);
3471 3472 3473
	  if (!build_classic_dist_vector_1 (ddr, DDR_B (ddr), DDR_A (ddr),
					    save_v, &init_b, &index_carry))
	    return false;
3474
	  save_dist_v (ddr, save_v);
3475
	  DDR_REVERSED_P (ddr) = true;
3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487

	  /* In this case there is a dependence forward for all the
	     outer loops:

	     | for (k = 1; k < 100; k++)
	     |  for (i = 1; i < 100; i++)
	     |   for (j = 1; j < 100; j++)
	     |     {
	     |       t = T[j+1][i-1];  // A
	     |       T[j][i] = t + 2;  // B
	     |     }

H.J. Lu committed
3488
	     the vectors are:
3489 3490 3491 3492 3493 3494 3495 3496
	     (0,  1, -1)
	     (1,  1, -1)
	     (1, -1,  1)
	  */
	  if (DDR_NB_LOOPS (ddr) > 1)
	    {
 	      add_outer_distances (ddr, save_v, index_carry);
	      add_outer_distances (ddr, dist_v, index_carry);
3497
	    }
3498 3499 3500 3501 3502
	}
      else
	{
	  lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
	  lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
3503

3504
	  if (DDR_NB_LOOPS (ddr) > 1)
3505
	    {
3506
	      lambda_vector opposite_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3507

3508 3509 3510
	      if (!subscript_dependence_tester_1 (ddr, DDR_B (ddr),
						  DDR_A (ddr), loop_nest))
		return false;
3511
	      compute_subscript_distance (ddr);
3512 3513 3514 3515
	      if (!build_classic_dist_vector_1 (ddr, DDR_B (ddr), DDR_A (ddr),
						opposite_v, &init_b,
						&index_carry))
		return false;
3516

3517
	      save_dist_v (ddr, save_v);
3518 3519
	      add_outer_distances (ddr, dist_v, index_carry);
	      add_outer_distances (ddr, opposite_v, index_carry);
3520
	    }
3521 3522
	  else
	    save_dist_v (ddr, save_v);
3523 3524
	}
    }
3525 3526 3527 3528
  else
    {
      /* There is a distance of 1 on all the outer loops: Example:
	 there is a dependence of distance 1 on loop_1 for the array A.
3529

3530 3531 3532 3533 3534 3535 3536 3537 3538 3539
	 | loop_1
	 |   A[5] = ...
	 | endloop
      */
      add_outer_distances (ddr, dist_v,
			   lambda_vector_first_nz (dist_v,
						   DDR_NB_LOOPS (ddr), 0));
    }

  if (dump_file && (dump_flags & TDF_DETAILS))
3540
    {
3541
      unsigned i;
3542

3543 3544 3545 3546 3547 3548 3549 3550 3551
      fprintf (dump_file, "(build_classic_dist_vector\n");
      for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
	{
	  fprintf (dump_file, "  dist_vector = (");
	  print_lambda_vector (dump_file, DDR_DIST_VECT (ddr, i),
			       DDR_NB_LOOPS (ddr));
	  fprintf (dump_file, "  )\n");
	}
      fprintf (dump_file, ")\n");
3552 3553
    }

3554 3555
  return true;
}
3556

3557 3558 3559
/* Return the direction for a given distance.
   FIXME: Computing dir this way is suboptimal, since dir can catch
   cases that dist is unable to represent.  */
3560

3561 3562 3563 3564 3565 3566 3567 3568 3569 3570
static inline enum data_dependence_direction
dir_from_dist (int dist)
{
  if (dist > 0)
    return dir_positive;
  else if (dist < 0)
    return dir_negative;
  else
    return dir_equal;
}
3571

3572 3573
/* Compute the classic per loop direction vector.  DDR is the data
   dependence relation to build a vector from.  */
3574

3575 3576 3577 3578 3579
static void
build_classic_dir_vector (struct data_dependence_relation *ddr)
{
  unsigned i, j;
  lambda_vector dist_v;
3580

3581
  FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
3582 3583
    {
      lambda_vector dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3584

3585 3586
      for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
	dir_v[j] = dir_from_dist (dist_v[j]);
3587

3588 3589
      save_dir_v (ddr, dir_v);
    }
3590 3591
}

3592 3593
/* Helper function.  Returns true when there is a dependence between
   data references DRA and DRB.  */
3594

3595 3596 3597
static bool
subscript_dependence_tester_1 (struct data_dependence_relation *ddr,
			       struct data_reference *dra,
3598 3599
			       struct data_reference *drb,
			       struct loop *loop_nest)
3600 3601 3602
{
  unsigned int i;
  tree last_conflicts;
3603
  struct subscript *subscript;
3604
  tree res = NULL_TREE;
3605

3606
  for (i = 0; DDR_SUBSCRIPTS (ddr).iterate (i, &subscript); i++)
3607
    {
3608
      conflict_function *overlaps_a, *overlaps_b;
3609

H.J. Lu committed
3610
      analyze_overlapping_iterations (DR_ACCESS_FN (dra, i),
3611
				      DR_ACCESS_FN (drb, i),
H.J. Lu committed
3612
				      &overlaps_a, &overlaps_b,
3613
				      &last_conflicts, loop_nest);
3614

3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626
      if (SUB_CONFLICTS_IN_A (subscript))
	free_conflict_function (SUB_CONFLICTS_IN_A (subscript));
      if (SUB_CONFLICTS_IN_B (subscript))
	free_conflict_function (SUB_CONFLICTS_IN_B (subscript));

      SUB_CONFLICTS_IN_A (subscript) = overlaps_a;
      SUB_CONFLICTS_IN_B (subscript) = overlaps_b;
      SUB_LAST_CONFLICT (subscript) = last_conflicts;

      /* If there is any undetermined conflict function we have to
         give a conservative answer in case we cannot prove that
	 no dependence exists when analyzing another subscript.  */
3627 3628
      if (CF_NOT_KNOWN_P (overlaps_a)
 	  || CF_NOT_KNOWN_P (overlaps_b))
3629
 	{
3630 3631
	  res = chrec_dont_know;
	  continue;
3632
 	}
3633

3634
      /* When there is a subscript with no dependence we can stop.  */
3635 3636
      else if (CF_NO_DEPENDENCE_P (overlaps_a)
 	       || CF_NO_DEPENDENCE_P (overlaps_b))
3637
 	{
3638 3639
	  res = chrec_known;
	  break;
3640 3641 3642
 	}
    }

3643 3644 3645 3646 3647 3648 3649 3650 3651
  if (res == NULL_TREE)
    return true;

  if (res == chrec_known)
    dependence_stats.num_dependence_independent++;
  else
    dependence_stats.num_dependence_undetermined++;
  finalize_ddr_dependent (ddr, res);
  return false;
3652 3653
}

3654
/* Computes the conflicting iterations in LOOP_NEST, and initialize DDR.  */
3655 3656

static void
3657 3658
subscript_dependence_tester (struct data_dependence_relation *ddr,
			     struct loop *loop_nest)
3659
{
3660
  if (subscript_dependence_tester_1 (ddr, DDR_A (ddr), DDR_B (ddr), loop_nest))
3661
    dependence_stats.num_dependence_dependent++;
3662 3663

  compute_subscript_distance (ddr);
3664
  if (build_classic_dist_vector (ddr, loop_nest))
3665
    build_classic_dir_vector (ddr);
3666 3667
}

3668
/* Returns true when all the access functions of A are affine or
3669
   constant with respect to LOOP_NEST.  */
3670

H.J. Lu committed
3671
static bool
3672 3673
access_functions_are_affine_or_constant_p (const struct data_reference *a,
					   const struct loop *loop_nest)
3674 3675
{
  unsigned int i;
3676
  vec<tree> fns = DR_ACCESS_FNS (a);
3677
  tree t;
3678

3679
  FOR_EACH_VEC_ELT (fns, i, t)
3680 3681
    if (!evolution_function_is_invariant_p (t, loop_nest->num)
	&& !evolution_function_is_affine_multivariate_p (t, loop_nest->num))
3682
      return false;
H.J. Lu committed
3683

3684 3685 3686
  return true;
}

3687 3688 3689 3690
/* This computes the affine dependence relation between A and B with
   respect to LOOP_NEST.  CHREC_KNOWN is used for representing the
   independence between two accesses, while CHREC_DONT_KNOW is used
   for representing the unknown relation.
H.J. Lu committed
3691

3692 3693 3694 3695
   Note that it is possible to stop the computation of the dependence
   relation the first time we detect a CHREC_KNOWN element for a given
   subscript.  */

3696
void
3697 3698
compute_affine_dependence (struct data_dependence_relation *ddr,
			   struct loop *loop_nest)
3699 3700 3701
{
  struct data_reference *dra = DDR_A (ddr);
  struct data_reference *drb = DDR_B (ddr);
H.J. Lu committed
3702

3703 3704
  if (dump_file && (dump_flags & TDF_DETAILS))
    {
Daniel Berlin committed
3705
      fprintf (dump_file, "(compute_affine_dependence\n");
3706 3707 3708 3709
      fprintf (dump_file, "  stmt_a: ");
      print_gimple_stmt (dump_file, DR_STMT (dra), 0, TDF_SLIM);
      fprintf (dump_file, "  stmt_b: ");
      print_gimple_stmt (dump_file, DR_STMT (drb), 0, TDF_SLIM);
3710
    }
3711

3712
  /* Analyze only when the dependence relation is not yet known.  */
3713
  if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
3714
    {
3715 3716
      dependence_stats.num_dependence_tests++;

3717 3718
      if (access_functions_are_affine_or_constant_p (dra, loop_nest)
	  && access_functions_are_affine_or_constant_p (drb, loop_nest))
3719
	subscript_dependence_tester (ddr, loop_nest);
H.J. Lu committed
3720

3721 3722 3723 3724
      /* As a last case, if the dependence cannot be determined, or if
	 the dependence is considered too difficult to determine, answer
	 "don't know".  */
      else
3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737
	{
	  dependence_stats.num_dependence_undetermined++;

	  if (dump_file && (dump_flags & TDF_DETAILS))
	    {
	      fprintf (dump_file, "Data ref a:\n");
	      dump_data_reference (dump_file, dra);
	      fprintf (dump_file, "Data ref b:\n");
	      dump_data_reference (dump_file, drb);
	      fprintf (dump_file, "affine dependence test not usable: access function not affine or constant.\n");
	    }
	  finalize_ddr_dependent (ddr, chrec_dont_know);
	}
3738
    }
H.J. Lu committed
3739

3740
  if (dump_file && (dump_flags & TDF_DETAILS))
3741 3742 3743 3744 3745 3746 3747 3748
    {
      if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
	fprintf (dump_file, ") -> no dependence\n");
      else if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
	fprintf (dump_file, ") -> dependence analysis failed\n");
      else
	fprintf (dump_file, ")\n");
    }
3749 3750
}

3751 3752
/* Compute in DEPENDENCE_RELATIONS the data dependence graph for all
   the data references in DATAREFS, in the LOOP_NEST.  When
3753
   COMPUTE_SELF_AND_RR is FALSE, don't compute read-read and self
3754 3755
   relations.  Return true when successful, i.e. data references number
   is small enough to be handled.  */
3756

3757
bool
3758 3759 3760
compute_all_dependences (vec<data_reference_p> datarefs,
			 vec<ddr_p> *dependence_relations,
			 vec<loop_p> loop_nest,
3761
			 bool compute_self_and_rr)
3762
{
3763 3764 3765
  struct data_dependence_relation *ddr;
  struct data_reference *a, *b;
  unsigned int i, j;
3766

3767
  if ((int) datarefs.length ()
3768 3769 3770 3771 3772 3773 3774
      > PARAM_VALUE (PARAM_LOOP_MAX_DATAREFS_FOR_DATADEPS))
    {
      struct data_dependence_relation *ddr;

      /* Insert a single relation into dependence_relations:
	 chrec_dont_know.  */
      ddr = initialize_data_dependence_relation (NULL, NULL, loop_nest);
3775
      dependence_relations->safe_push (ddr);
3776 3777 3778
      return false;
    }

3779 3780
  FOR_EACH_VEC_ELT (datarefs, i, a)
    for (j = i + 1; datarefs.iterate (j, &b); j++)
3781
      if (DR_IS_WRITE (a) || DR_IS_WRITE (b) || compute_self_and_rr)
3782 3783
	{
	  ddr = initialize_data_dependence_relation (a, b, loop_nest);
3784 3785 3786
	  dependence_relations->safe_push (ddr);
          if (loop_nest.exists ())
   	    compute_affine_dependence (ddr, loop_nest[0]);
3787
	}
3788

3789
  if (compute_self_and_rr)
3790
    FOR_EACH_VEC_ELT (datarefs, i, a)
3791
      {
3792
	ddr = initialize_data_dependence_relation (a, a, loop_nest);
3793 3794 3795
	dependence_relations->safe_push (ddr);
        if (loop_nest.exists ())
   	  compute_affine_dependence (ddr, loop_nest[0]);
3796
      }
3797 3798

  return true;
3799 3800
}

3801 3802
/* Describes a location of a memory reference.  */

3803
struct data_ref_loc
3804
{
3805 3806
  /* The memory reference.  */
  tree ref;
3807

3808 3809
  /* True if the memory reference is read.  */
  bool is_read;
3810
};
3811 3812


3813 3814 3815
/* Stores the locations of memory references in STMT to REFERENCES.  Returns
   true if STMT clobbers memory, false otherwise.  */

3816
static bool
3817
get_references_in_stmt (gimple *stmt, vec<data_ref_loc, va_heap> *references)
3818 3819
{
  bool clobbers_memory = false;
3820
  data_ref_loc ref;
3821
  tree op0, op1;
3822
  enum gimple_code stmt_code = gimple_code (stmt);
3823 3824

  /* ASM_EXPR and CALL_EXPR may embed arbitrary side effects.
3825 3826
     As we cannot model data-references to not spelled out
     accesses give up if they may occur.  */
3827 3828 3829 3830
  if (stmt_code == GIMPLE_CALL
      && !(gimple_call_flags (stmt) & ECF_CONST))
    {
      /* Allow IFN_GOMP_SIMD_LANE in their own loops.  */
3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847
      if (gimple_call_internal_p (stmt))
	switch (gimple_call_internal_fn (stmt))
	  {
	  case IFN_GOMP_SIMD_LANE:
	    {
	      struct loop *loop = gimple_bb (stmt)->loop_father;
	      tree uid = gimple_call_arg (stmt, 0);
	      gcc_assert (TREE_CODE (uid) == SSA_NAME);
	      if (loop == NULL
		  || loop->simduid != SSA_NAME_VAR (uid))
		clobbers_memory = true;
	      break;
	    }
	  case IFN_MASK_LOAD:
	  case IFN_MASK_STORE:
	    break;
	  default:
3848
	    clobbers_memory = true;
3849 3850
	    break;
	  }
3851 3852 3853 3854
      else
	clobbers_memory = true;
    }
  else if (stmt_code == GIMPLE_ASM
3855 3856
	   && (gimple_asm_volatile_p (as_a <gasm *> (stmt))
	       || gimple_vuse (stmt)))
3857 3858
    clobbers_memory = true;

3859
  if (!gimple_vuse (stmt))
3860 3861
    return clobbers_memory;

3862
  if (stmt_code == GIMPLE_ASSIGN)
3863
    {
3864
      tree base;
3865 3866
      op0 = gimple_assign_lhs (stmt);
      op1 = gimple_assign_rhs1 (stmt);
H.J. Lu committed
3867

3868 3869 3870
      if (DECL_P (op1)
	  || (REFERENCE_CLASS_P (op1)
	      && (base = get_base_address (op1))
3871 3872
	      && TREE_CODE (base) != SSA_NAME
	      && !is_gimple_min_invariant (base)))
3873
	{
3874
	  ref.ref = op1;
3875
	  ref.is_read = true;
3876
	  references->safe_push (ref);
3877 3878
	}
    }
3879
  else if (stmt_code == GIMPLE_CALL)
3880
    {
3881
      unsigned i, n;
3882 3883
      tree ptr, type;
      unsigned int align;
3884

3885 3886 3887 3888 3889
      ref.is_read = false;
      if (gimple_call_internal_p (stmt))
	switch (gimple_call_internal_fn (stmt))
	  {
	  case IFN_MASK_LOAD:
3890 3891
	    if (gimple_call_lhs (stmt) == NULL_TREE)
	      break;
3892 3893
	    ref.is_read = true;
	  case IFN_MASK_STORE:
3894 3895 3896 3897 3898 3899 3900 3901 3902 3903
	    ptr = build_int_cst (TREE_TYPE (gimple_call_arg (stmt, 1)), 0);
	    align = tree_to_shwi (gimple_call_arg (stmt, 1));
	    if (ref.is_read)
	      type = TREE_TYPE (gimple_call_lhs (stmt));
	    else
	      type = TREE_TYPE (gimple_call_arg (stmt, 3));
	    if (TYPE_ALIGN (type) != align)
	      type = build_aligned_type (type, align);
	    ref.ref = fold_build2 (MEM_REF, type, gimple_call_arg (stmt, 0),
				   ptr);
3904 3905 3906 3907 3908 3909
	    references->safe_push (ref);
	    return false;
	  default:
	    break;
	  }

3910
      op0 = gimple_call_lhs (stmt);
3911
      n = gimple_call_num_args (stmt);
3912
      for (i = 0; i < n; i++)
3913
	{
3914
	  op1 = gimple_call_arg (stmt, i);
3915

3916 3917
	  if (DECL_P (op1)
	      || (REFERENCE_CLASS_P (op1) && get_base_address (op1)))
3918
	    {
3919
	      ref.ref = op1;
3920
	      ref.is_read = true;
3921
	      references->safe_push (ref);
3922 3923 3924
	    }
	}
    }
3925 3926
  else
    return clobbers_memory;
3927

3928 3929 3930
  if (op0
      && (DECL_P (op0)
	  || (REFERENCE_CLASS_P (op0) && get_base_address (op0))))
3931
    {
3932
      ref.ref = op0;
3933
      ref.is_read = false;
3934
      references->safe_push (ref);
3935
    }
3936 3937 3938
  return clobbers_memory;
}

Aditya Kumar committed
3939 3940 3941 3942 3943 3944 3945

/* Returns true if the loop-nest has any data reference.  */

bool
loop_nest_has_data_refs (loop_p loop)
{
  basic_block *bbs = get_loop_body (loop);
3946
  auto_vec<data_ref_loc, 3> references;
Aditya Kumar committed
3947 3948 3949 3950 3951 3952 3953 3954

  for (unsigned i = 0; i < loop->num_nodes; i++)
    {
      basic_block bb = bbs[i];
      gimple_stmt_iterator bsi;

      for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
	{
3955
	  gimple *stmt = gsi_stmt (bsi);
Aditya Kumar committed
3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978
	  get_references_in_stmt (stmt, &references);
	  if (references.length ())
	    {
	      free (bbs);
	      return true;
	    }
	}
    }
  free (bbs);

  if (loop->inner)
    {
      loop = loop->inner;
      while (loop)
	{
	  if (loop_nest_has_data_refs (loop))
	    return true;
	  loop = loop->next;
	}
    }
  return false;
}

3979
/* Stores the data references in STMT to DATAREFS.  If there is an unanalyzable
3980
   reference, returns false, otherwise returns true.  NEST is the outermost
3981
   loop of the loop nest in which the references should be analyzed.  */
3982

3983
bool
3984
find_data_references_in_stmt (struct loop *nest, gimple *stmt,
3985
			      vec<data_reference_p> *datarefs)
3986 3987
{
  unsigned i;
3988
  auto_vec<data_ref_loc, 2> references;
3989 3990 3991 3992 3993
  data_ref_loc *ref;
  bool ret = true;
  data_reference_p dr;

  if (get_references_in_stmt (stmt, &references))
3994
    return false;
3995

3996
  FOR_EACH_VEC_ELT (references, i, ref)
3997
    {
3998
      dr = create_data_ref (nest, loop_containing_stmt (stmt),
3999
			    ref->ref, stmt, ref->is_read);
4000
      gcc_assert (dr != NULL);
4001
      datarefs->safe_push (dr);
4002
    }
4003

4004 4005 4006
  return ret;
}

4007 4008 4009 4010 4011
/* Stores the data references in STMT to DATAREFS.  If there is an
   unanalyzable reference, returns false, otherwise returns true.
   NEST is the outermost loop of the loop nest in which the references
   should be instantiated, LOOP is the loop in which the references
   should be analyzed.  */
4012 4013

bool
4014
graphite_find_data_references_in_stmt (loop_p nest, loop_p loop, gimple *stmt,
4015
				       vec<data_reference_p> *datarefs)
4016 4017
{
  unsigned i;
4018
  auto_vec<data_ref_loc, 2> references;
4019 4020 4021 4022 4023
  data_ref_loc *ref;
  bool ret = true;
  data_reference_p dr;

  if (get_references_in_stmt (stmt, &references))
4024
    return false;
4025

4026
  FOR_EACH_VEC_ELT (references, i, ref)
4027
    {
4028
      dr = create_data_ref (nest, loop, ref->ref, stmt, ref->is_read);
4029
      gcc_assert (dr != NULL);
4030
      datarefs->safe_push (dr);
4031 4032 4033 4034 4035
    }

  return ret;
}

4036 4037
/* Search the data references in LOOP, and record the information into
   DATAREFS.  Returns chrec_dont_know when failing to analyze a
4038 4039
   difficult case, returns NULL_TREE otherwise.  */

4040
tree
4041
find_data_references_in_bb (struct loop *loop, basic_block bb,
4042
                            vec<data_reference_p> *datarefs)
4043 4044 4045 4046 4047
{
  gimple_stmt_iterator bsi;

  for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
    {
4048
      gimple *stmt = gsi_stmt (bsi);
4049 4050 4051 4052 4053

      if (!find_data_references_in_stmt (loop, stmt, datarefs))
        {
          struct data_reference *res;
          res = XCNEW (struct data_reference);
4054
          datarefs->safe_push (res);
4055 4056 4057 4058 4059 4060 4061 4062 4063 4064

          return chrec_dont_know;
        }
    }

  return NULL_TREE;
}

/* Search the data references in LOOP, and record the information into
   DATAREFS.  Returns chrec_dont_know when failing to analyze a
4065
   difficult case, returns NULL_TREE otherwise.
4066

4067 4068
   TODO: This function should be made smarter so that it can handle address
   arithmetic as if they were array accesses, etc.  */
4069

4070
tree
4071
find_data_references_in_loop (struct loop *loop,
4072
			      vec<data_reference_p> *datarefs)
4073
{
4074 4075
  basic_block bb, *bbs;
  unsigned int i;
4076

4077
  bbs = get_loop_body_in_dom_order (loop);
4078 4079

  for (i = 0; i < loop->num_nodes; i++)
4080
    {
4081 4082
      bb = bbs[i];

4083 4084 4085 4086 4087
      if (find_data_references_in_bb (loop, bb, datarefs) == chrec_dont_know)
        {
          free (bbs);
          return chrec_dont_know;
        }
4088
    }
4089 4090
  free (bbs);

4091
  return NULL_TREE;
4092 4093
}

4094 4095 4096
/* Recursive helper function.  */

static bool
4097
find_loop_nest_1 (struct loop *loop, vec<loop_p> *loop_nest)
4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114
{
  /* Inner loops of the nest should not contain siblings.  Example:
     when there are two consecutive loops,

     | loop_0
     |   loop_1
     |     A[{0, +, 1}_1]
     |   endloop_1
     |   loop_2
     |     A[{0, +, 1}_2]
     |   endloop_2
     | endloop_0

     the dependence relation cannot be captured by the distance
     abstraction.  */
  if (loop->next)
    return false;
4115

4116
  loop_nest->safe_push (loop);
4117 4118 4119 4120 4121 4122 4123 4124 4125 4126
  if (loop->inner)
    return find_loop_nest_1 (loop->inner, loop_nest);
  return true;
}

/* Return false when the LOOP is not well nested.  Otherwise return
   true and insert in LOOP_NEST the loops of the nest.  LOOP_NEST will
   contain the loops from the outermost to the innermost, as they will
   appear in the classic distance vector.  */

4127
bool
4128
find_loop_nest (struct loop *loop, vec<loop_p> *loop_nest)
4129
{
4130
  loop_nest->safe_push (loop);
4131 4132 4133 4134
  if (loop->inner)
    return find_loop_nest_1 (loop->inner, loop_nest);
  return true;
}
4135

4136 4137
/* Returns true when the data dependences have been computed, false otherwise.
   Given a loop nest LOOP, the following vectors are returned:
H.J. Lu committed
4138 4139 4140
   DATAREFS is initialized to all the array elements contained in this loop,
   DEPENDENCE_RELATIONS contains the relations between the data references.
   Compute read-read and self relations if
4141
   COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE.  */
4142

4143
bool
H.J. Lu committed
4144
compute_data_dependences_for_loop (struct loop *loop,
4145
				   bool compute_self_and_read_read_dependences,
4146 4147 4148
				   vec<loop_p> *loop_nest,
				   vec<data_reference_p> *datarefs,
				   vec<ddr_p> *dependence_relations)
4149
{
4150
  bool res = true;
4151

4152
  memset (&dependence_stats, 0, sizeof (dependence_stats));
4153

H.J. Lu committed
4154
  /* If the loop nest is not well formed, or one of the data references
4155 4156
     is not computable, give up without spending time to compute other
     dependences.  */
4157
  if (!loop
4158
      || !find_loop_nest (loop, loop_nest)
4159 4160 4161 4162
      || find_data_references_in_loop (loop, datarefs) == chrec_dont_know
      || !compute_all_dependences (*datarefs, dependence_relations, *loop_nest,
				   compute_self_and_read_read_dependences))
    res = false;
4163 4164

  if (dump_file && (dump_flags & TDF_STATS))
4165
    {
4166 4167
      fprintf (dump_file, "Dependence tester statistics:\n");

H.J. Lu committed
4168
      fprintf (dump_file, "Number of dependence tests: %d\n",
4169
	       dependence_stats.num_dependence_tests);
H.J. Lu committed
4170
      fprintf (dump_file, "Number of dependence tests classified dependent: %d\n",
4171
	       dependence_stats.num_dependence_dependent);
H.J. Lu committed
4172
      fprintf (dump_file, "Number of dependence tests classified independent: %d\n",
4173
	       dependence_stats.num_dependence_independent);
H.J. Lu committed
4174
      fprintf (dump_file, "Number of undetermined dependence tests: %d\n",
4175 4176
	       dependence_stats.num_dependence_undetermined);

H.J. Lu committed
4177
      fprintf (dump_file, "Number of subscript tests: %d\n",
4178
	       dependence_stats.num_subscript_tests);
H.J. Lu committed
4179
      fprintf (dump_file, "Number of undetermined subscript tests: %d\n",
4180
	       dependence_stats.num_subscript_undetermined);
H.J. Lu committed
4181
      fprintf (dump_file, "Number of same subscript function: %d\n",
4182 4183 4184 4185 4186 4187 4188 4189 4190
	       dependence_stats.num_same_subscript_function);

      fprintf (dump_file, "Number of ziv tests: %d\n",
	       dependence_stats.num_ziv);
      fprintf (dump_file, "Number of ziv tests returning dependent: %d\n",
	       dependence_stats.num_ziv_dependent);
      fprintf (dump_file, "Number of ziv tests returning independent: %d\n",
	       dependence_stats.num_ziv_independent);
      fprintf (dump_file, "Number of ziv tests unimplemented: %d\n",
H.J. Lu committed
4191
	       dependence_stats.num_ziv_unimplemented);
4192

H.J. Lu committed
4193
      fprintf (dump_file, "Number of siv tests: %d\n",
4194 4195 4196 4197 4198 4199 4200 4201
	       dependence_stats.num_siv);
      fprintf (dump_file, "Number of siv tests returning dependent: %d\n",
	       dependence_stats.num_siv_dependent);
      fprintf (dump_file, "Number of siv tests returning independent: %d\n",
	       dependence_stats.num_siv_independent);
      fprintf (dump_file, "Number of siv tests unimplemented: %d\n",
	       dependence_stats.num_siv_unimplemented);

H.J. Lu committed
4202
      fprintf (dump_file, "Number of miv tests: %d\n",
4203 4204 4205 4206 4207 4208 4209
	       dependence_stats.num_miv);
      fprintf (dump_file, "Number of miv tests returning dependent: %d\n",
	       dependence_stats.num_miv_dependent);
      fprintf (dump_file, "Number of miv tests returning independent: %d\n",
	       dependence_stats.num_miv_independent);
      fprintf (dump_file, "Number of miv tests unimplemented: %d\n",
	       dependence_stats.num_miv_unimplemented);
4210 4211 4212
    }

  return res;
4213 4214
}

Daniel Berlin committed
4215 4216 4217 4218 4219 4220 4221 4222
/* Free the memory used by a data dependence relation DDR.  */

void
free_dependence_relation (struct data_dependence_relation *ddr)
{
  if (ddr == NULL)
    return;

4223
  if (DDR_SUBSCRIPTS (ddr).exists ())
4224
    free_subscripts (DDR_SUBSCRIPTS (ddr));
4225 4226
  DDR_DIST_VECTS (ddr).release ();
  DDR_DIR_VECTS (ddr).release ();
4227

Daniel Berlin committed
4228 4229 4230 4231 4232 4233
  free (ddr);
}

/* Free the memory used by the data dependence relations from
   DEPENDENCE_RELATIONS.  */

H.J. Lu committed
4234
void
4235
free_dependence_relations (vec<ddr_p> dependence_relations)
Daniel Berlin committed
4236 4237
{
  unsigned int i;
4238
  struct data_dependence_relation *ddr;
Daniel Berlin committed
4239

4240
  FOR_EACH_VEC_ELT (dependence_relations, i, ddr)
4241
    if (ddr)
4242
      free_dependence_relation (ddr);
4243

4244
  dependence_relations.release ();
4245 4246
}

Daniel Berlin committed
4247 4248 4249
/* Free the memory used by the data references from DATAREFS.  */

void
4250
free_data_refs (vec<data_reference_p> datarefs)
Daniel Berlin committed
4251 4252
{
  unsigned int i;
4253
  struct data_reference *dr;
4254

4255
  FOR_EACH_VEC_ELT (datarefs, i, dr)
4256
    free_data_ref (dr);
4257
  datarefs.release ();
Daniel Berlin committed
4258
}