1 //===--- OpenMPClause.cpp - Classes for OpenMP clauses --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the subclesses of Stmt class declared in OpenMPClause.h
12 //===----------------------------------------------------------------------===//
14 #include "clang/AST/OpenMPClause.h"
16 #include "clang/AST/ASTContext.h"
18 using namespace clang;
20 OMPClause::child_range OMPClause::children() {
21 switch (getClauseKind()) {
24 #define OPENMP_CLAUSE(Name, Class) \
26 return static_cast<Class *>(this)->children();
27 #include "clang/Basic/OpenMPKinds.def"
29 llvm_unreachable("unknown OMPClause");
32 OMPClauseWithPreInit *OMPClauseWithPreInit::get(OMPClause *C) {
33 auto *Res = OMPClauseWithPreInit::get(const_cast<const OMPClause *>(C));
34 return Res ? const_cast<OMPClauseWithPreInit *>(Res) : nullptr;
37 const OMPClauseWithPreInit *OMPClauseWithPreInit::get(const OMPClause *C) {
38 switch (C->getClauseKind()) {
40 return static_cast<const OMPScheduleClause *>(C);
41 case OMPC_dist_schedule:
42 return static_cast<const OMPDistScheduleClause *>(C);
43 case OMPC_firstprivate:
44 return static_cast<const OMPFirstprivateClause *>(C);
45 case OMPC_lastprivate:
46 return static_cast<const OMPLastprivateClause *>(C);
48 return static_cast<const OMPReductionClause *>(C);
49 case OMPC_task_reduction:
50 return static_cast<const OMPTaskReductionClause *>(C);
52 return static_cast<const OMPLinearClause *>(C);
54 return static_cast<const OMPIfClause *>(C);
55 case OMPC_num_threads:
56 return static_cast<const OMPNumThreadsClause *>(C);
58 return static_cast<const OMPNumTeamsClause *>(C);
59 case OMPC_thread_limit:
60 return static_cast<const OMPThreadLimitClause *>(C);
71 case OMPC_copyprivate:
76 case OMPC_threadprivate:
98 case OMPC_use_device_ptr:
99 case OMPC_is_device_ptr:
106 OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(OMPClause *C) {
107 auto *Res = OMPClauseWithPostUpdate::get(const_cast<const OMPClause *>(C));
108 return Res ? const_cast<OMPClauseWithPostUpdate *>(Res) : nullptr;
111 const OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(const OMPClause *C) {
112 switch (C->getClauseKind()) {
113 case OMPC_lastprivate:
114 return static_cast<const OMPLastprivateClause *>(C);
116 return static_cast<const OMPReductionClause *>(C);
117 case OMPC_task_reduction:
118 return static_cast<const OMPTaskReductionClause *>(C);
120 return static_cast<const OMPLinearClause *>(C);
122 case OMPC_dist_schedule:
123 case OMPC_firstprivate:
128 case OMPC_num_threads:
136 case OMPC_copyprivate:
141 case OMPC_threadprivate:
154 case OMPC_thread_limit:
160 case OMPC_defaultmap:
165 case OMPC_use_device_ptr:
166 case OMPC_is_device_ptr:
173 void OMPPrivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
174 assert(VL.size() == varlist_size() &&
175 "Number of private copies is not the same as the preallocated buffer");
176 std::copy(VL.begin(), VL.end(), varlist_end());
180 OMPPrivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
181 SourceLocation LParenLoc, SourceLocation EndLoc,
182 ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL) {
183 // Allocate space for private variables and initializer expressions.
184 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * VL.size()));
185 OMPPrivateClause *Clause =
186 new (Mem) OMPPrivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
187 Clause->setVarRefs(VL);
188 Clause->setPrivateCopies(PrivateVL);
192 OMPPrivateClause *OMPPrivateClause::CreateEmpty(const ASTContext &C,
194 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * N));
195 return new (Mem) OMPPrivateClause(N);
198 void OMPFirstprivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
199 assert(VL.size() == varlist_size() &&
200 "Number of private copies is not the same as the preallocated buffer");
201 std::copy(VL.begin(), VL.end(), varlist_end());
204 void OMPFirstprivateClause::setInits(ArrayRef<Expr *> VL) {
205 assert(VL.size() == varlist_size() &&
206 "Number of inits is not the same as the preallocated buffer");
207 std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
210 OMPFirstprivateClause *
211 OMPFirstprivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
212 SourceLocation LParenLoc, SourceLocation EndLoc,
213 ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL,
214 ArrayRef<Expr *> InitVL, Stmt *PreInit) {
215 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * VL.size()));
216 OMPFirstprivateClause *Clause =
217 new (Mem) OMPFirstprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
218 Clause->setVarRefs(VL);
219 Clause->setPrivateCopies(PrivateVL);
220 Clause->setInits(InitVL);
221 Clause->setPreInitStmt(PreInit);
225 OMPFirstprivateClause *OMPFirstprivateClause::CreateEmpty(const ASTContext &C,
227 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * N));
228 return new (Mem) OMPFirstprivateClause(N);
231 void OMPLastprivateClause::setPrivateCopies(ArrayRef<Expr *> PrivateCopies) {
232 assert(PrivateCopies.size() == varlist_size() &&
233 "Number of private copies is not the same as the preallocated buffer");
234 std::copy(PrivateCopies.begin(), PrivateCopies.end(), varlist_end());
237 void OMPLastprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
238 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
239 "not the same as the "
240 "preallocated buffer");
241 std::copy(SrcExprs.begin(), SrcExprs.end(), getPrivateCopies().end());
244 void OMPLastprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
245 assert(DstExprs.size() == varlist_size() && "Number of destination "
246 "expressions is not the same as "
247 "the preallocated buffer");
248 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
251 void OMPLastprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
252 assert(AssignmentOps.size() == varlist_size() &&
253 "Number of assignment expressions is not the same as the preallocated "
255 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
256 getDestinationExprs().end());
259 OMPLastprivateClause *OMPLastprivateClause::Create(
260 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
261 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
262 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps, Stmt *PreInit,
264 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
265 OMPLastprivateClause *Clause =
266 new (Mem) OMPLastprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
267 Clause->setVarRefs(VL);
268 Clause->setSourceExprs(SrcExprs);
269 Clause->setDestinationExprs(DstExprs);
270 Clause->setAssignmentOps(AssignmentOps);
271 Clause->setPreInitStmt(PreInit);
272 Clause->setPostUpdateExpr(PostUpdate);
276 OMPLastprivateClause *OMPLastprivateClause::CreateEmpty(const ASTContext &C,
278 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
279 return new (Mem) OMPLastprivateClause(N);
282 OMPSharedClause *OMPSharedClause::Create(const ASTContext &C,
283 SourceLocation StartLoc,
284 SourceLocation LParenLoc,
285 SourceLocation EndLoc,
286 ArrayRef<Expr *> VL) {
287 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size()));
288 OMPSharedClause *Clause =
289 new (Mem) OMPSharedClause(StartLoc, LParenLoc, EndLoc, VL.size());
290 Clause->setVarRefs(VL);
294 OMPSharedClause *OMPSharedClause::CreateEmpty(const ASTContext &C, unsigned N) {
295 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
296 return new (Mem) OMPSharedClause(N);
299 void OMPLinearClause::setPrivates(ArrayRef<Expr *> PL) {
300 assert(PL.size() == varlist_size() &&
301 "Number of privates is not the same as the preallocated buffer");
302 std::copy(PL.begin(), PL.end(), varlist_end());
305 void OMPLinearClause::setInits(ArrayRef<Expr *> IL) {
306 assert(IL.size() == varlist_size() &&
307 "Number of inits is not the same as the preallocated buffer");
308 std::copy(IL.begin(), IL.end(), getPrivates().end());
311 void OMPLinearClause::setUpdates(ArrayRef<Expr *> UL) {
312 assert(UL.size() == varlist_size() &&
313 "Number of updates is not the same as the preallocated buffer");
314 std::copy(UL.begin(), UL.end(), getInits().end());
317 void OMPLinearClause::setFinals(ArrayRef<Expr *> FL) {
318 assert(FL.size() == varlist_size() &&
319 "Number of final updates is not the same as the preallocated buffer");
320 std::copy(FL.begin(), FL.end(), getUpdates().end());
323 OMPLinearClause *OMPLinearClause::Create(
324 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
325 OpenMPLinearClauseKind Modifier, SourceLocation ModifierLoc,
326 SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef<Expr *> VL,
327 ArrayRef<Expr *> PL, ArrayRef<Expr *> IL, Expr *Step, Expr *CalcStep,
328 Stmt *PreInit, Expr *PostUpdate) {
329 // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
330 // (Step and CalcStep).
331 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size() + 2));
332 OMPLinearClause *Clause = new (Mem) OMPLinearClause(
333 StartLoc, LParenLoc, Modifier, ModifierLoc, ColonLoc, EndLoc, VL.size());
334 Clause->setVarRefs(VL);
335 Clause->setPrivates(PL);
336 Clause->setInits(IL);
337 // Fill update and final expressions with zeroes, they are provided later,
338 // after the directive construction.
339 std::fill(Clause->getInits().end(), Clause->getInits().end() + VL.size(),
341 std::fill(Clause->getUpdates().end(), Clause->getUpdates().end() + VL.size(),
343 Clause->setStep(Step);
344 Clause->setCalcStep(CalcStep);
345 Clause->setPreInitStmt(PreInit);
346 Clause->setPostUpdateExpr(PostUpdate);
350 OMPLinearClause *OMPLinearClause::CreateEmpty(const ASTContext &C,
352 // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
353 // (Step and CalcStep).
354 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * NumVars + 2));
355 return new (Mem) OMPLinearClause(NumVars);
359 OMPAlignedClause::Create(const ASTContext &C, SourceLocation StartLoc,
360 SourceLocation LParenLoc, SourceLocation ColonLoc,
361 SourceLocation EndLoc, ArrayRef<Expr *> VL, Expr *A) {
362 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
363 OMPAlignedClause *Clause = new (Mem)
364 OMPAlignedClause(StartLoc, LParenLoc, ColonLoc, EndLoc, VL.size());
365 Clause->setVarRefs(VL);
366 Clause->setAlignment(A);
370 OMPAlignedClause *OMPAlignedClause::CreateEmpty(const ASTContext &C,
372 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumVars + 1));
373 return new (Mem) OMPAlignedClause(NumVars);
376 void OMPCopyinClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
377 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
378 "not the same as the "
379 "preallocated buffer");
380 std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
383 void OMPCopyinClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
384 assert(DstExprs.size() == varlist_size() && "Number of destination "
385 "expressions is not the same as "
386 "the preallocated buffer");
387 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
390 void OMPCopyinClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
391 assert(AssignmentOps.size() == varlist_size() &&
392 "Number of assignment expressions is not the same as the preallocated "
394 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
395 getDestinationExprs().end());
398 OMPCopyinClause *OMPCopyinClause::Create(
399 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
400 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
401 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
402 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
403 OMPCopyinClause *Clause =
404 new (Mem) OMPCopyinClause(StartLoc, LParenLoc, EndLoc, VL.size());
405 Clause->setVarRefs(VL);
406 Clause->setSourceExprs(SrcExprs);
407 Clause->setDestinationExprs(DstExprs);
408 Clause->setAssignmentOps(AssignmentOps);
412 OMPCopyinClause *OMPCopyinClause::CreateEmpty(const ASTContext &C, unsigned N) {
413 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
414 return new (Mem) OMPCopyinClause(N);
417 void OMPCopyprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
418 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
419 "not the same as the "
420 "preallocated buffer");
421 std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
424 void OMPCopyprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
425 assert(DstExprs.size() == varlist_size() && "Number of destination "
426 "expressions is not the same as "
427 "the preallocated buffer");
428 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
431 void OMPCopyprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
432 assert(AssignmentOps.size() == varlist_size() &&
433 "Number of assignment expressions is not the same as the preallocated "
435 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
436 getDestinationExprs().end());
439 OMPCopyprivateClause *OMPCopyprivateClause::Create(
440 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
441 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
442 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
443 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
444 OMPCopyprivateClause *Clause =
445 new (Mem) OMPCopyprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
446 Clause->setVarRefs(VL);
447 Clause->setSourceExprs(SrcExprs);
448 Clause->setDestinationExprs(DstExprs);
449 Clause->setAssignmentOps(AssignmentOps);
453 OMPCopyprivateClause *OMPCopyprivateClause::CreateEmpty(const ASTContext &C,
455 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
456 return new (Mem) OMPCopyprivateClause(N);
459 void OMPReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
460 assert(Privates.size() == varlist_size() &&
461 "Number of private copies is not the same as the preallocated buffer");
462 std::copy(Privates.begin(), Privates.end(), varlist_end());
465 void OMPReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
467 LHSExprs.size() == varlist_size() &&
468 "Number of LHS expressions is not the same as the preallocated buffer");
469 std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
472 void OMPReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
474 RHSExprs.size() == varlist_size() &&
475 "Number of RHS expressions is not the same as the preallocated buffer");
476 std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
479 void OMPReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
480 assert(ReductionOps.size() == varlist_size() && "Number of reduction "
481 "expressions is not the same "
482 "as the preallocated buffer");
483 std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
486 OMPReductionClause *OMPReductionClause::Create(
487 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
488 SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
489 NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
490 ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
491 ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps, Stmt *PreInit,
493 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
494 OMPReductionClause *Clause = new (Mem) OMPReductionClause(
495 StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
496 Clause->setVarRefs(VL);
497 Clause->setPrivates(Privates);
498 Clause->setLHSExprs(LHSExprs);
499 Clause->setRHSExprs(RHSExprs);
500 Clause->setReductionOps(ReductionOps);
501 Clause->setPreInitStmt(PreInit);
502 Clause->setPostUpdateExpr(PostUpdate);
506 OMPReductionClause *OMPReductionClause::CreateEmpty(const ASTContext &C,
508 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
509 return new (Mem) OMPReductionClause(N);
512 void OMPTaskReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
513 assert(Privates.size() == varlist_size() &&
514 "Number of private copies is not the same as the preallocated buffer");
515 std::copy(Privates.begin(), Privates.end(), varlist_end());
518 void OMPTaskReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
520 LHSExprs.size() == varlist_size() &&
521 "Number of LHS expressions is not the same as the preallocated buffer");
522 std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
525 void OMPTaskReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
527 RHSExprs.size() == varlist_size() &&
528 "Number of RHS expressions is not the same as the preallocated buffer");
529 std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
532 void OMPTaskReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
533 assert(ReductionOps.size() == varlist_size() && "Number of task reduction "
534 "expressions is not the same "
535 "as the preallocated buffer");
536 std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
539 OMPTaskReductionClause *OMPTaskReductionClause::Create(
540 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
541 SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
542 NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
543 ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
544 ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps, Stmt *PreInit,
546 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
547 OMPTaskReductionClause *Clause = new (Mem) OMPTaskReductionClause(
548 StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
549 Clause->setVarRefs(VL);
550 Clause->setPrivates(Privates);
551 Clause->setLHSExprs(LHSExprs);
552 Clause->setRHSExprs(RHSExprs);
553 Clause->setReductionOps(ReductionOps);
554 Clause->setPreInitStmt(PreInit);
555 Clause->setPostUpdateExpr(PostUpdate);
559 OMPTaskReductionClause *OMPTaskReductionClause::CreateEmpty(const ASTContext &C,
561 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
562 return new (Mem) OMPTaskReductionClause(N);
565 OMPFlushClause *OMPFlushClause::Create(const ASTContext &C,
566 SourceLocation StartLoc,
567 SourceLocation LParenLoc,
568 SourceLocation EndLoc,
569 ArrayRef<Expr *> VL) {
570 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
571 OMPFlushClause *Clause =
572 new (Mem) OMPFlushClause(StartLoc, LParenLoc, EndLoc, VL.size());
573 Clause->setVarRefs(VL);
577 OMPFlushClause *OMPFlushClause::CreateEmpty(const ASTContext &C, unsigned N) {
578 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
579 return new (Mem) OMPFlushClause(N);
582 OMPDependClause *OMPDependClause::Create(
583 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
584 SourceLocation EndLoc, OpenMPDependClauseKind DepKind,
585 SourceLocation DepLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL) {
586 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
587 OMPDependClause *Clause =
588 new (Mem) OMPDependClause(StartLoc, LParenLoc, EndLoc, VL.size());
589 Clause->setVarRefs(VL);
590 Clause->setDependencyKind(DepKind);
591 Clause->setDependencyLoc(DepLoc);
592 Clause->setColonLoc(ColonLoc);
593 Clause->setCounterValue(nullptr);
597 OMPDependClause *OMPDependClause::CreateEmpty(const ASTContext &C, unsigned N) {
598 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N + 1));
599 return new (Mem) OMPDependClause(N);
602 void OMPDependClause::setCounterValue(Expr *V) {
603 assert(getDependencyKind() == OMPC_DEPEND_sink ||
604 getDependencyKind() == OMPC_DEPEND_source || V == nullptr);
605 *getVarRefs().end() = V;
608 const Expr *OMPDependClause::getCounterValue() const {
609 auto *V = *getVarRefs().end();
610 assert(getDependencyKind() == OMPC_DEPEND_sink ||
611 getDependencyKind() == OMPC_DEPEND_source || V == nullptr);
615 Expr *OMPDependClause::getCounterValue() {
616 auto *V = *getVarRefs().end();
617 assert(getDependencyKind() == OMPC_DEPEND_sink ||
618 getDependencyKind() == OMPC_DEPEND_source || V == nullptr);
622 unsigned OMPClauseMappableExprCommon::getComponentsTotalNumber(
623 MappableExprComponentListsRef ComponentLists) {
624 unsigned TotalNum = 0u;
625 for (auto &C : ComponentLists)
626 TotalNum += C.size();
630 unsigned OMPClauseMappableExprCommon::getUniqueDeclarationsTotalNumber(
631 ArrayRef<ValueDecl *> Declarations) {
632 unsigned TotalNum = 0u;
633 llvm::SmallPtrSet<const ValueDecl *, 8> Cache;
634 for (auto *D : Declarations) {
635 const ValueDecl *VD = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr;
645 OMPMapClause::Create(const ASTContext &C, SourceLocation StartLoc,
646 SourceLocation LParenLoc, SourceLocation EndLoc,
647 ArrayRef<Expr *> Vars, ArrayRef<ValueDecl *> Declarations,
648 MappableExprComponentListsRef ComponentLists,
649 OpenMPMapClauseKind TypeModifier, OpenMPMapClauseKind Type,
650 bool TypeIsImplicit, SourceLocation TypeLoc) {
652 unsigned NumVars = Vars.size();
653 unsigned NumUniqueDeclarations =
654 getUniqueDeclarationsTotalNumber(Declarations);
655 unsigned NumComponentLists = ComponentLists.size();
656 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
658 // We need to allocate:
659 // NumVars x Expr* - we have an original list expression for each clause list
661 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
662 // with each component list.
663 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
664 // number of lists for each unique declaration and the size of each component
666 // NumComponents x MappableComponent - the total of all the components in all
668 void *Mem = C.Allocate(
669 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
670 OMPClauseMappableExprCommon::MappableComponent>(
671 NumVars, NumUniqueDeclarations,
672 NumUniqueDeclarations + NumComponentLists, NumComponents));
673 OMPMapClause *Clause = new (Mem) OMPMapClause(
674 TypeModifier, Type, TypeIsImplicit, TypeLoc, StartLoc, LParenLoc, EndLoc,
675 NumVars, NumUniqueDeclarations, NumComponentLists, NumComponents);
677 Clause->setVarRefs(Vars);
678 Clause->setClauseInfo(Declarations, ComponentLists);
679 Clause->setMapTypeModifier(TypeModifier);
680 Clause->setMapType(Type);
681 Clause->setMapLoc(TypeLoc);
685 OMPMapClause *OMPMapClause::CreateEmpty(const ASTContext &C, unsigned NumVars,
686 unsigned NumUniqueDeclarations,
687 unsigned NumComponentLists,
688 unsigned NumComponents) {
689 void *Mem = C.Allocate(
690 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
691 OMPClauseMappableExprCommon::MappableComponent>(
692 NumVars, NumUniqueDeclarations,
693 NumUniqueDeclarations + NumComponentLists, NumComponents));
694 return new (Mem) OMPMapClause(NumVars, NumUniqueDeclarations,
695 NumComponentLists, NumComponents);
698 OMPToClause *OMPToClause::Create(const ASTContext &C, SourceLocation StartLoc,
699 SourceLocation LParenLoc,
700 SourceLocation EndLoc, ArrayRef<Expr *> Vars,
701 ArrayRef<ValueDecl *> Declarations,
702 MappableExprComponentListsRef ComponentLists) {
703 unsigned NumVars = Vars.size();
704 unsigned NumUniqueDeclarations =
705 getUniqueDeclarationsTotalNumber(Declarations);
706 unsigned NumComponentLists = ComponentLists.size();
707 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
709 // We need to allocate:
710 // NumVars x Expr* - we have an original list expression for each clause list
712 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
713 // with each component list.
714 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
715 // number of lists for each unique declaration and the size of each component
717 // NumComponents x MappableComponent - the total of all the components in all
719 void *Mem = C.Allocate(
720 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
721 OMPClauseMappableExprCommon::MappableComponent>(
722 NumVars, NumUniqueDeclarations,
723 NumUniqueDeclarations + NumComponentLists, NumComponents));
725 OMPToClause *Clause = new (Mem)
726 OMPToClause(StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
727 NumComponentLists, NumComponents);
729 Clause->setVarRefs(Vars);
730 Clause->setClauseInfo(Declarations, ComponentLists);
734 OMPToClause *OMPToClause::CreateEmpty(const ASTContext &C, unsigned NumVars,
735 unsigned NumUniqueDeclarations,
736 unsigned NumComponentLists,
737 unsigned NumComponents) {
738 void *Mem = C.Allocate(
739 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
740 OMPClauseMappableExprCommon::MappableComponent>(
741 NumVars, NumUniqueDeclarations,
742 NumUniqueDeclarations + NumComponentLists, NumComponents));
743 return new (Mem) OMPToClause(NumVars, NumUniqueDeclarations,
744 NumComponentLists, NumComponents);
748 OMPFromClause::Create(const ASTContext &C, SourceLocation StartLoc,
749 SourceLocation LParenLoc, SourceLocation EndLoc,
750 ArrayRef<Expr *> Vars, ArrayRef<ValueDecl *> Declarations,
751 MappableExprComponentListsRef ComponentLists) {
752 unsigned NumVars = Vars.size();
753 unsigned NumUniqueDeclarations =
754 getUniqueDeclarationsTotalNumber(Declarations);
755 unsigned NumComponentLists = ComponentLists.size();
756 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
758 // We need to allocate:
759 // NumVars x Expr* - we have an original list expression for each clause list
761 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
762 // with each component list.
763 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
764 // number of lists for each unique declaration and the size of each component
766 // NumComponents x MappableComponent - the total of all the components in all
768 void *Mem = C.Allocate(
769 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
770 OMPClauseMappableExprCommon::MappableComponent>(
771 NumVars, NumUniqueDeclarations,
772 NumUniqueDeclarations + NumComponentLists, NumComponents));
774 OMPFromClause *Clause = new (Mem)
775 OMPFromClause(StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
776 NumComponentLists, NumComponents);
778 Clause->setVarRefs(Vars);
779 Clause->setClauseInfo(Declarations, ComponentLists);
783 OMPFromClause *OMPFromClause::CreateEmpty(const ASTContext &C, unsigned NumVars,
784 unsigned NumUniqueDeclarations,
785 unsigned NumComponentLists,
786 unsigned NumComponents) {
787 void *Mem = C.Allocate(
788 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
789 OMPClauseMappableExprCommon::MappableComponent>(
790 NumVars, NumUniqueDeclarations,
791 NumUniqueDeclarations + NumComponentLists, NumComponents));
792 return new (Mem) OMPFromClause(NumVars, NumUniqueDeclarations,
793 NumComponentLists, NumComponents);
796 void OMPUseDevicePtrClause::setPrivateCopies(ArrayRef<Expr *> VL) {
797 assert(VL.size() == varlist_size() &&
798 "Number of private copies is not the same as the preallocated buffer");
799 std::copy(VL.begin(), VL.end(), varlist_end());
802 void OMPUseDevicePtrClause::setInits(ArrayRef<Expr *> VL) {
803 assert(VL.size() == varlist_size() &&
804 "Number of inits is not the same as the preallocated buffer");
805 std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
808 OMPUseDevicePtrClause *OMPUseDevicePtrClause::Create(
809 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
810 SourceLocation EndLoc, ArrayRef<Expr *> Vars, ArrayRef<Expr *> PrivateVars,
811 ArrayRef<Expr *> Inits, ArrayRef<ValueDecl *> Declarations,
812 MappableExprComponentListsRef ComponentLists) {
813 unsigned NumVars = Vars.size();
814 unsigned NumUniqueDeclarations =
815 getUniqueDeclarationsTotalNumber(Declarations);
816 unsigned NumComponentLists = ComponentLists.size();
817 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
819 // We need to allocate:
820 // 3 x NumVars x Expr* - we have an original list expression for each clause
821 // list entry and an equal number of private copies and inits.
822 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
823 // with each component list.
824 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
825 // number of lists for each unique declaration and the size of each component
827 // NumComponents x MappableComponent - the total of all the components in all
829 void *Mem = C.Allocate(
830 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
831 OMPClauseMappableExprCommon::MappableComponent>(
832 3 * NumVars, NumUniqueDeclarations,
833 NumUniqueDeclarations + NumComponentLists, NumComponents));
835 OMPUseDevicePtrClause *Clause = new (Mem) OMPUseDevicePtrClause(
836 StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
837 NumComponentLists, NumComponents);
839 Clause->setVarRefs(Vars);
840 Clause->setPrivateCopies(PrivateVars);
841 Clause->setInits(Inits);
842 Clause->setClauseInfo(Declarations, ComponentLists);
846 OMPUseDevicePtrClause *OMPUseDevicePtrClause::CreateEmpty(
847 const ASTContext &C, unsigned NumVars, unsigned NumUniqueDeclarations,
848 unsigned NumComponentLists, unsigned NumComponents) {
849 void *Mem = C.Allocate(
850 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
851 OMPClauseMappableExprCommon::MappableComponent>(
852 3 * NumVars, NumUniqueDeclarations,
853 NumUniqueDeclarations + NumComponentLists, NumComponents));
854 return new (Mem) OMPUseDevicePtrClause(NumVars, NumUniqueDeclarations,
855 NumComponentLists, NumComponents);
858 OMPIsDevicePtrClause *
859 OMPIsDevicePtrClause::Create(const ASTContext &C, SourceLocation StartLoc,
860 SourceLocation LParenLoc, SourceLocation EndLoc,
861 ArrayRef<Expr *> Vars,
862 ArrayRef<ValueDecl *> Declarations,
863 MappableExprComponentListsRef ComponentLists) {
864 unsigned NumVars = Vars.size();
865 unsigned NumUniqueDeclarations =
866 getUniqueDeclarationsTotalNumber(Declarations);
867 unsigned NumComponentLists = ComponentLists.size();
868 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
870 // We need to allocate:
871 // NumVars x Expr* - we have an original list expression for each clause list
873 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
874 // with each component list.
875 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
876 // number of lists for each unique declaration and the size of each component
878 // NumComponents x MappableComponent - the total of all the components in all
880 void *Mem = C.Allocate(
881 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
882 OMPClauseMappableExprCommon::MappableComponent>(
883 NumVars, NumUniqueDeclarations,
884 NumUniqueDeclarations + NumComponentLists, NumComponents));
886 OMPIsDevicePtrClause *Clause = new (Mem) OMPIsDevicePtrClause(
887 StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
888 NumComponentLists, NumComponents);
890 Clause->setVarRefs(Vars);
891 Clause->setClauseInfo(Declarations, ComponentLists);
895 OMPIsDevicePtrClause *OMPIsDevicePtrClause::CreateEmpty(
896 const ASTContext &C, unsigned NumVars, unsigned NumUniqueDeclarations,
897 unsigned NumComponentLists, unsigned NumComponents) {
898 void *Mem = C.Allocate(
899 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
900 OMPClauseMappableExprCommon::MappableComponent>(
901 NumVars, NumUniqueDeclarations,
902 NumUniqueDeclarations + NumComponentLists, NumComponents));
903 return new (Mem) OMPIsDevicePtrClause(NumVars, NumUniqueDeclarations,
904 NumComponentLists, NumComponents);