c-omp.c 14.5 KB
Newer Older
Diego Novillo committed
1 2 3
/* This file contains routines to construct GNU OpenMP constructs, 
   called from parsing in the C and C++ front ends.

4
   Copyright (C) 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
Diego Novillo committed
5 6 7 8 9 10 11
   Contributed by Richard Henderson <rth@redhat.com>,
		  Diego Novillo <dnovillo@redhat.com>.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
12
Software Foundation; either version 3, or (at your option) any later
Diego Novillo committed
13 14 15 16 17 18 19 20
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
21 22
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */
Diego Novillo committed
23 24 25 26 27 28 29 30 31

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "tree.h"
#include "function.h"
#include "c-common.h"
#include "toplev.h"
32
#include "gimple.h"
Diego Novillo committed
33 34 35 36 37
#include "bitmap.h"
#include "langhooks.h"


/* Complete a #pragma omp master construct.  STMT is the structured-block
38
   that follows the pragma.  LOC is the l*/
Diego Novillo committed
39 40

tree
41
c_finish_omp_master (location_t loc, tree stmt)
Diego Novillo committed
42
{
43 44 45
  tree t = add_stmt (build1 (OMP_MASTER, void_type_node, stmt));
  SET_EXPR_LOCATION (t, loc);
  return t;
Diego Novillo committed
46 47 48 49
}

/* Complete a #pragma omp critical construct.  STMT is the structured-block
   that follows the pragma, NAME is the identifier in the pragma, or null
50
   if it was omitted.  LOC is the location of the #pragma.  */
Diego Novillo committed
51 52

tree
53
c_finish_omp_critical (location_t loc, tree body, tree name)
Diego Novillo committed
54 55 56 57 58
{
  tree stmt = make_node (OMP_CRITICAL);
  TREE_TYPE (stmt) = void_type_node;
  OMP_CRITICAL_BODY (stmt) = body;
  OMP_CRITICAL_NAME (stmt) = name;
59
  SET_EXPR_LOCATION (stmt, loc);
Diego Novillo committed
60 61 62 63
  return add_stmt (stmt);
}

/* Complete a #pragma omp ordered construct.  STMT is the structured-block
64
   that follows the pragma.  LOC is the location of the #pragma.  */
Diego Novillo committed
65 66

tree
67
c_finish_omp_ordered (location_t loc, tree stmt)
Diego Novillo committed
68
{
69 70 71
  tree t = build1 (OMP_ORDERED, void_type_node, stmt);
  SET_EXPR_LOCATION (t, loc);
  return add_stmt (t);
Diego Novillo committed
72 73 74
}


75 76
/* Complete a #pragma omp barrier construct.  LOC is the location of
   the #pragma.  */
Diego Novillo committed
77 78

void
79
c_finish_omp_barrier (location_t loc)
Diego Novillo committed
80 81 82 83
{
  tree x;

  x = built_in_decls[BUILT_IN_GOMP_BARRIER];
84
  x = build_call_expr (x, 0);
85
  SET_EXPR_LOCATION (x, loc);
Diego Novillo committed
86 87 88 89
  add_stmt (x);
}


90 91
/* Complete a #pragma omp taskwait construct.  LOC is the location of the
   pragma.  */
92 93

void
94
c_finish_omp_taskwait (location_t loc)
95 96 97 98 99
{
  tree x;

  x = built_in_decls[BUILT_IN_GOMP_TASKWAIT];
  x = build_call_expr (x, 0);
100
  SET_EXPR_LOCATION (x, loc);
101 102 103 104
  add_stmt (x);
}


105 106 107 108 109
/* Complete a #pragma omp atomic construct.  The expression to be
   implemented atomically is LHS code= RHS.  LOC is the location of
   the atomic statement.  The value returned is either error_mark_node
   (if the construct was erroneous) or an OMP_ATOMIC node which should
   be added to the current statement tree with add_stmt.*/
Diego Novillo committed
110

111
tree
112
c_finish_omp_atomic (location_t loc, enum tree_code code, tree lhs, tree rhs)
Diego Novillo committed
113 114 115 116
{
  tree x, type, addr;

  if (lhs == error_mark_node || rhs == error_mark_node)
117
    return error_mark_node;
Diego Novillo committed
118 119 120 121 122 123 124 125 126 127

  /* ??? According to one reading of the OpenMP spec, complex type are
     supported, but there are no atomic stores for any architecture.
     But at least icc 9.0 doesn't support complex types here either.
     And lets not even talk about vector types...  */
  type = TREE_TYPE (lhs);
  if (!INTEGRAL_TYPE_P (type)
      && !POINTER_TYPE_P (type)
      && !SCALAR_FLOAT_TYPE_P (type))
    {
128
      error_at (loc, "invalid expression type for %<#pragma omp atomic%>");
129
      return error_mark_node;
Diego Novillo committed
130 131 132 133 134 135
    }

  /* ??? Validate that rhs does not overlap lhs.  */

  /* Take and save the address of the lhs.  From then on we'll reference it
     via indirection.  */
136
  addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
Diego Novillo committed
137
  if (addr == error_mark_node)
138
    return error_mark_node;
Diego Novillo committed
139
  addr = save_expr (addr);
140 141 142 143 144 145 146 147 148
  if (TREE_CODE (addr) != SAVE_EXPR
      && (TREE_CODE (addr) != ADDR_EXPR
	  || TREE_CODE (TREE_OPERAND (addr, 0)) != VAR_DECL))
    {
      /* Make sure LHS is simple enough so that goa_lhs_expr_p can recognize
	 it even after unsharing function body.  */
      tree var = create_tmp_var_raw (TREE_TYPE (addr), NULL);
      addr = build4 (TARGET_EXPR, TREE_TYPE (addr), var, addr, NULL, NULL);
    }
149
  lhs = build_indirect_ref (loc, addr, NULL);
Diego Novillo committed
150 151 152 153

  /* There are lots of warnings, errors, and conversions that need to happen
     in the course of interpreting a statement.  Use the normal mechanisms
     to do this, and then take it apart again.  */
154 155
  x = build_modify_expr (input_location, lhs, NULL_TREE, code,
      			 input_location, rhs, NULL_TREE);
Diego Novillo committed
156
  if (x == error_mark_node)
157
    return error_mark_node;
Diego Novillo committed
158 159 160 161
  gcc_assert (TREE_CODE (x) == MODIFY_EXPR);  
  rhs = TREE_OPERAND (x, 1);

  /* Punt the actual generation of atomic operations to common code.  */
162 163 164
  x = build2 (OMP_ATOMIC, void_type_node, addr, rhs);
  SET_EXPR_LOCATION (x, loc);
  return x;
Diego Novillo committed
165 166 167
}


168 169 170
/* Complete a #pragma omp flush construct.  We don't do anything with
   the variable list that the syntax allows.  LOC is the location of
   the #pragma.  */
Diego Novillo committed
171 172

void
173
c_finish_omp_flush (location_t loc)
Diego Novillo committed
174 175 176 177
{
  tree x;

  x = built_in_decls[BUILT_IN_SYNCHRONIZE];
178
  x = build_call_expr (x, 0);
179
  SET_EXPR_LOCATION (x, loc);
Diego Novillo committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
  add_stmt (x);
}


/* Check and canonicalize #pragma omp for increment expression.
   Helper function for c_finish_omp_for.  */

static tree
check_omp_for_incr_expr (tree exp, tree decl)
{
  tree t;

  if (!INTEGRAL_TYPE_P (TREE_TYPE (exp))
      || TYPE_PRECISION (TREE_TYPE (exp)) < TYPE_PRECISION (TREE_TYPE (decl)))
    return error_mark_node;

  if (exp == decl)
    return build_int_cst (TREE_TYPE (exp), 0);

  switch (TREE_CODE (exp))
    {
201
    CASE_CONVERT:
Diego Novillo committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
      t = check_omp_for_incr_expr (TREE_OPERAND (exp, 0), decl);
      if (t != error_mark_node)
        return fold_convert (TREE_TYPE (exp), t);
      break;
    case MINUS_EXPR:
      t = check_omp_for_incr_expr (TREE_OPERAND (exp, 0), decl);
      if (t != error_mark_node)
        return fold_build2 (MINUS_EXPR, TREE_TYPE (exp), t, TREE_OPERAND (exp, 1));
      break;
    case PLUS_EXPR:
      t = check_omp_for_incr_expr (TREE_OPERAND (exp, 0), decl);
      if (t != error_mark_node)
        return fold_build2 (PLUS_EXPR, TREE_TYPE (exp), t, TREE_OPERAND (exp, 1));
      t = check_omp_for_incr_expr (TREE_OPERAND (exp, 1), decl);
      if (t != error_mark_node)
        return fold_build2 (PLUS_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0), t);
      break;
    default:
      break;
    }

  return error_mark_node;
}

/* Validate and emit code for the OpenMP directive #pragma omp for.
227 228 229 230 231
   DECLV is a vector of iteration variables, for each collapsed loop.
   INITV, CONDV and INCRV are vectors containing initialization
   expressions, controlling predicates and increment expressions.
   BODY is the body of the loop and PRE_BODY statements that go before
   the loop.  */
Diego Novillo committed
232 233

tree
234 235
c_finish_omp_for (location_t locus, tree declv, tree initv, tree condv,
		  tree incrv, tree body, tree pre_body)
Diego Novillo committed
236
{
237
  location_t elocus;
Diego Novillo committed
238
  bool fail = false;
239
  int i;
Diego Novillo committed
240

241 242 243 244
  gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
  gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
  gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
  for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
Diego Novillo committed
245
    {
246 247 248 249 250 251 252 253 254 255 256 257 258
      tree decl = TREE_VEC_ELT (declv, i);
      tree init = TREE_VEC_ELT (initv, i);
      tree cond = TREE_VEC_ELT (condv, i);
      tree incr = TREE_VEC_ELT (incrv, i);

      elocus = locus;
      if (EXPR_HAS_LOCATION (init))
	elocus = EXPR_LOCATION (init);

      /* Validate the iteration variable.  */
      if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
	  && TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE)
	{
259
	  error_at (elocus, "invalid type for iteration variable %qE", decl);
260 261
	  fail = true;
	}
Diego Novillo committed
262

263 264 265 266 267 268 269 270 271
      /* In the case of "for (int i = 0...)", init will be a decl.  It should
	 have a DECL_INITIAL that we can turn into an assignment.  */
      if (init == decl)
	{
	  elocus = DECL_SOURCE_LOCATION (decl);

	  init = DECL_INITIAL (decl);
	  if (init == NULL)
	    {
272
	      error_at (elocus, "%qE is not initialized", decl);
273 274 275
	      init = integer_zero_node;
	      fail = true;
	    }
Diego Novillo committed
276

277 278 279 280 281
	  init = build_modify_expr (elocus, decl, NULL_TREE, NOP_EXPR, 
	      			    /* FIXME diagnostics: This should
				       be the location of the INIT.  */
	      			    elocus,
				    init,
282
				    NULL_TREE);
283 284 285 286 287
	}
      gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
      gcc_assert (TREE_OPERAND (init, 0) == decl);

      if (cond == NULL_TREE)
Diego Novillo committed
288
	{
289
	  error_at (elocus, "missing controlling predicate");
Diego Novillo committed
290 291
	  fail = true;
	}
292 293 294
      else
	{
	  bool cond_ok = false;
Diego Novillo committed
295

296 297
	  if (EXPR_HAS_LOCATION (cond))
	    elocus = EXPR_LOCATION (cond);
Diego Novillo committed
298

299 300 301
	  if (TREE_CODE (cond) == LT_EXPR
	      || TREE_CODE (cond) == LE_EXPR
	      || TREE_CODE (cond) == GT_EXPR
302 303
	      || TREE_CODE (cond) == GE_EXPR
	      || TREE_CODE (cond) == NE_EXPR)
304 305 306
	    {
	      tree op0 = TREE_OPERAND (cond, 0);
	      tree op1 = TREE_OPERAND (cond, 1);
Diego Novillo committed
307

308 309
	      /* 2.5.1.  The comparison in the condition is computed in
		 the type of DECL, otherwise the behavior is undefined.
Diego Novillo committed
310

311 312 313
		 For example:
		 long n; int i;
		 i < n;
Diego Novillo committed
314

315 316
		 according to ISO will be evaluated as:
		 (long)i < n;
Diego Novillo committed
317

318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
		 We want to force:
		 i < (int)n;  */
	      if (TREE_CODE (op0) == NOP_EXPR
		  && decl == TREE_OPERAND (op0, 0))
		{
		  TREE_OPERAND (cond, 0) = TREE_OPERAND (op0, 0);
		  TREE_OPERAND (cond, 1)
		    = fold_build1 (NOP_EXPR, TREE_TYPE (decl),
				   TREE_OPERAND (cond, 1));
		}
	      else if (TREE_CODE (op1) == NOP_EXPR
		       && decl == TREE_OPERAND (op1, 0))
		{
		  TREE_OPERAND (cond, 1) = TREE_OPERAND (op1, 0);
		  TREE_OPERAND (cond, 0)
		    = fold_build1 (NOP_EXPR, TREE_TYPE (decl),
				   TREE_OPERAND (cond, 0));
		}
Diego Novillo committed
336

337 338 339 340 341 342 343 344 345 346
	      if (decl == TREE_OPERAND (cond, 0))
		cond_ok = true;
	      else if (decl == TREE_OPERAND (cond, 1))
		{
		  TREE_SET_CODE (cond,
				 swap_tree_comparison (TREE_CODE (cond)));
		  TREE_OPERAND (cond, 1) = TREE_OPERAND (cond, 0);
		  TREE_OPERAND (cond, 0) = decl;
		  cond_ok = true;
		}
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362

	      if (TREE_CODE (cond) == NE_EXPR)
		{
		  if (!INTEGRAL_TYPE_P (TREE_TYPE (decl)))
		    cond_ok = false;
		  else if (operand_equal_p (TREE_OPERAND (cond, 1),
					    TYPE_MIN_VALUE (TREE_TYPE (decl)),
					    0))
		    TREE_SET_CODE (cond, GT_EXPR);
		  else if (operand_equal_p (TREE_OPERAND (cond, 1),
					    TYPE_MAX_VALUE (TREE_TYPE (decl)),
					    0))
		    TREE_SET_CODE (cond, LT_EXPR);
		  else
		    cond_ok = false;
		}
Diego Novillo committed
363 364
	    }

365
	  if (!cond_ok)
Diego Novillo committed
366
	    {
367
	      error_at (elocus, "invalid controlling predicate");
368
	      fail = true;
Diego Novillo committed
369 370 371
	    }
	}

372
      if (incr == NULL_TREE)
Diego Novillo committed
373
	{
374
	  error_at (elocus, "missing increment expression");
Diego Novillo committed
375 376
	  fail = true;
	}
377
      else
Diego Novillo committed
378
	{
379
	  bool incr_ok = false;
Diego Novillo committed
380

381 382 383 384 385 386
	  if (EXPR_HAS_LOCATION (incr))
	    elocus = EXPR_LOCATION (incr);

	  /* Check all the valid increment expressions: v++, v--, ++v, --v,
	     v = v + incr, v = incr + v and v = v - incr.  */
	  switch (TREE_CODE (incr))
Diego Novillo committed
387
	    {
388 389 390 391 392 393 394 395
	    case POSTINCREMENT_EXPR:
	    case PREINCREMENT_EXPR:
	    case POSTDECREMENT_EXPR:
	    case PREDECREMENT_EXPR:
	      if (TREE_OPERAND (incr, 0) != decl)
		break;

	      incr_ok = true;
396 397
	      if (POINTER_TYPE_P (TREE_TYPE (decl))
		  && TREE_OPERAND (incr, 1))
Diego Novillo committed
398
		{
399 400 401 402 403 404
		  tree t = fold_convert (sizetype, TREE_OPERAND (incr, 1));

		  if (TREE_CODE (incr) == POSTDECREMENT_EXPR
		      || TREE_CODE (incr) == PREDECREMENT_EXPR)
		    t = fold_build1 (NEGATE_EXPR, sizetype, t);
		  t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (decl), decl, t);
Diego Novillo committed
405 406
		  incr = build2 (MODIFY_EXPR, void_type_node, decl, t);
		}
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
	      break;

	    case MODIFY_EXPR:
	      if (TREE_OPERAND (incr, 0) != decl)
		break;
	      if (TREE_OPERAND (incr, 1) == decl)
		break;
	      if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
		  && (TREE_OPERAND (TREE_OPERAND (incr, 1), 0) == decl
		      || TREE_OPERAND (TREE_OPERAND (incr, 1), 1) == decl))
		incr_ok = true;
	      else if ((TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR
			|| (TREE_CODE (TREE_OPERAND (incr, 1))
			    == POINTER_PLUS_EXPR))
		       && TREE_OPERAND (TREE_OPERAND (incr, 1), 0) == decl)
		incr_ok = true;
	      else
		{
		  tree t = check_omp_for_incr_expr (TREE_OPERAND (incr, 1),
						    decl);
		  if (t != error_mark_node)
		    {
		      incr_ok = true;
		      t = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, t);
		      incr = build2 (MODIFY_EXPR, void_type_node, decl, t);
		    }
		}
	      break;
Diego Novillo committed
435

436 437 438 439 440
	    default:
	      break;
	    }
	  if (!incr_ok)
	    {
441
	      error_at (elocus, "invalid increment expression");
442 443
	      fail = true;
	    }
Diego Novillo committed
444
	}
445 446 447

      TREE_VEC_ELT (initv, i) = init;
      TREE_VEC_ELT (incrv, i) = incr;
Diego Novillo committed
448 449 450 451 452 453 454 455 456
    }

  if (fail)
    return NULL;
  else
    {
      tree t = make_node (OMP_FOR);

      TREE_TYPE (t) = void_type_node;
457 458 459
      OMP_FOR_INIT (t) = initv;
      OMP_FOR_COND (t) = condv;
      OMP_FOR_INCR (t) = incrv;
Diego Novillo committed
460 461 462 463 464 465 466 467 468
      OMP_FOR_BODY (t) = body;
      OMP_FOR_PRE_BODY (t) = pre_body;

      SET_EXPR_LOCATION (t, locus);
      return add_stmt (t);
    }
}


469 470 471 472 473
/* Divide CLAUSES into two lists: those that apply to a parallel
   construct, and those that apply to a work-sharing construct.  Place
   the results in *PAR_CLAUSES and *WS_CLAUSES respectively.  In
   addition, add a nowait clause to the work-sharing list.  LOC is the
   location of the OMP_PARALLEL*.  */
Diego Novillo committed
474 475

void
476 477
c_split_parallel_clauses (location_t loc, tree clauses,
			  tree *par_clauses, tree *ws_clauses)
Diego Novillo committed
478 479 480 481
{
  tree next;

  *par_clauses = NULL;
482
  *ws_clauses = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
Diego Novillo committed
483 484 485 486 487

  for (; clauses ; clauses = next)
    {
      next = OMP_CLAUSE_CHAIN (clauses);

488
      switch (OMP_CLAUSE_CODE (clauses))
Diego Novillo committed
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
	{
	case OMP_CLAUSE_PRIVATE:
	case OMP_CLAUSE_SHARED:
	case OMP_CLAUSE_FIRSTPRIVATE:
	case OMP_CLAUSE_LASTPRIVATE:
	case OMP_CLAUSE_REDUCTION:
	case OMP_CLAUSE_COPYIN:
	case OMP_CLAUSE_IF:
	case OMP_CLAUSE_NUM_THREADS:
	case OMP_CLAUSE_DEFAULT:
	  OMP_CLAUSE_CHAIN (clauses) = *par_clauses;
	  *par_clauses = clauses;
	  break;

	case OMP_CLAUSE_SCHEDULE:
	case OMP_CLAUSE_ORDERED:
505
	case OMP_CLAUSE_COLLAPSE:
Diego Novillo committed
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
	  OMP_CLAUSE_CHAIN (clauses) = *ws_clauses;
	  *ws_clauses = clauses;
	  break;

	default:
	  gcc_unreachable ();
	}
    }
}

/* True if OpenMP sharing attribute of DECL is predetermined.  */

enum omp_clause_default_kind
c_omp_predetermined_sharing (tree decl)
{
  /* Variables with const-qualified type having no mutable member
     are predetermined shared.  */
  if (TREE_READONLY (decl))
    return OMP_CLAUSE_DEFAULT_SHARED;

  return OMP_CLAUSE_DEFAULT_UNSPECIFIED;
}