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"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/Basic/LLVM.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/Support/Casting.h"
20 #include "llvm/Support/ErrorHandling.h"
24 using namespace clang;
26 OMPClause::child_range OMPClause::children() {
27 switch (getClauseKind()) {
30 #define OPENMP_CLAUSE(Name, Class) \
32 return static_cast<Class *>(this)->children();
33 #include "clang/Basic/OpenMPKinds.def"
35 llvm_unreachable("unknown OMPClause");
38 OMPClauseWithPreInit *OMPClauseWithPreInit::get(OMPClause *C) {
39 auto *Res = OMPClauseWithPreInit::get(const_cast<const OMPClause *>(C));
40 return Res ? const_cast<OMPClauseWithPreInit *>(Res) : nullptr;
43 const OMPClauseWithPreInit *OMPClauseWithPreInit::get(const OMPClause *C) {
44 switch (C->getClauseKind()) {
46 return static_cast<const OMPScheduleClause *>(C);
47 case OMPC_dist_schedule:
48 return static_cast<const OMPDistScheduleClause *>(C);
49 case OMPC_firstprivate:
50 return static_cast<const OMPFirstprivateClause *>(C);
51 case OMPC_lastprivate:
52 return static_cast<const OMPLastprivateClause *>(C);
54 return static_cast<const OMPReductionClause *>(C);
55 case OMPC_task_reduction:
56 return static_cast<const OMPTaskReductionClause *>(C);
57 case OMPC_in_reduction:
58 return static_cast<const OMPInReductionClause *>(C);
60 return static_cast<const OMPLinearClause *>(C);
62 return static_cast<const OMPIfClause *>(C);
63 case OMPC_num_threads:
64 return static_cast<const OMPNumThreadsClause *>(C);
66 return static_cast<const OMPNumTeamsClause *>(C);
67 case OMPC_thread_limit:
68 return static_cast<const OMPThreadLimitClause *>(C);
70 return static_cast<const OMPDeviceClause *>(C);
81 case OMPC_copyprivate:
86 case OMPC_threadprivate:
102 case OMPC_defaultmap:
107 case OMPC_use_device_ptr:
108 case OMPC_is_device_ptr:
115 OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(OMPClause *C) {
116 auto *Res = OMPClauseWithPostUpdate::get(const_cast<const OMPClause *>(C));
117 return Res ? const_cast<OMPClauseWithPostUpdate *>(Res) : nullptr;
120 const OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(const OMPClause *C) {
121 switch (C->getClauseKind()) {
122 case OMPC_lastprivate:
123 return static_cast<const OMPLastprivateClause *>(C);
125 return static_cast<const OMPReductionClause *>(C);
126 case OMPC_task_reduction:
127 return static_cast<const OMPTaskReductionClause *>(C);
128 case OMPC_in_reduction:
129 return static_cast<const OMPInReductionClause *>(C);
131 return static_cast<const OMPLinearClause *>(C);
133 case OMPC_dist_schedule:
134 case OMPC_firstprivate:
139 case OMPC_num_threads:
147 case OMPC_copyprivate:
152 case OMPC_threadprivate:
165 case OMPC_thread_limit:
171 case OMPC_defaultmap:
176 case OMPC_use_device_ptr:
177 case OMPC_is_device_ptr:
184 OMPOrderedClause *OMPOrderedClause::Create(const ASTContext &C, Expr *Num,
186 SourceLocation StartLoc,
187 SourceLocation LParenLoc,
188 SourceLocation EndLoc) {
189 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * NumLoops));
191 new (Mem) OMPOrderedClause(Num, NumLoops, StartLoc, LParenLoc, EndLoc);
192 for (unsigned I = 0; I < NumLoops; ++I) {
193 Clause->setLoopNumIterations(I, nullptr);
194 Clause->setLoopCounter(I, nullptr);
199 OMPOrderedClause *OMPOrderedClause::CreateEmpty(const ASTContext &C,
201 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * NumLoops));
202 auto *Clause = new (Mem) OMPOrderedClause(NumLoops);
203 for (unsigned I = 0; I < NumLoops; ++I) {
204 Clause->setLoopNumIterations(I, nullptr);
205 Clause->setLoopCounter(I, nullptr);
210 void OMPOrderedClause::setLoopNumIterations(unsigned NumLoop,
211 Expr *NumIterations) {
212 assert(NumLoop < NumberOfLoops && "out of loops number.");
213 getTrailingObjects<Expr *>()[NumLoop] = NumIterations;
216 ArrayRef<Expr *> OMPOrderedClause::getLoopNumIterations() const {
217 return llvm::makeArrayRef(getTrailingObjects<Expr *>(), NumberOfLoops);
220 void OMPOrderedClause::setLoopCounter(unsigned NumLoop, Expr *Counter) {
221 assert(NumLoop < NumberOfLoops && "out of loops number.");
222 getTrailingObjects<Expr *>()[NumberOfLoops + NumLoop] = Counter;
225 Expr *OMPOrderedClause::getLoopCunter(unsigned NumLoop) {
226 assert(NumLoop < NumberOfLoops && "out of loops number.");
227 return getTrailingObjects<Expr *>()[NumberOfLoops + NumLoop];
230 const Expr *OMPOrderedClause::getLoopCunter(unsigned NumLoop) const {
231 assert(NumLoop < NumberOfLoops && "out of loops number.");
232 return getTrailingObjects<Expr *>()[NumberOfLoops + NumLoop];
235 void OMPPrivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
236 assert(VL.size() == varlist_size() &&
237 "Number of private copies is not the same as the preallocated buffer");
238 std::copy(VL.begin(), VL.end(), varlist_end());
242 OMPPrivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
243 SourceLocation LParenLoc, SourceLocation EndLoc,
244 ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL) {
245 // Allocate space for private variables and initializer expressions.
246 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * VL.size()));
247 OMPPrivateClause *Clause =
248 new (Mem) OMPPrivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
249 Clause->setVarRefs(VL);
250 Clause->setPrivateCopies(PrivateVL);
254 OMPPrivateClause *OMPPrivateClause::CreateEmpty(const ASTContext &C,
256 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * N));
257 return new (Mem) OMPPrivateClause(N);
260 void OMPFirstprivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
261 assert(VL.size() == varlist_size() &&
262 "Number of private copies is not the same as the preallocated buffer");
263 std::copy(VL.begin(), VL.end(), varlist_end());
266 void OMPFirstprivateClause::setInits(ArrayRef<Expr *> VL) {
267 assert(VL.size() == varlist_size() &&
268 "Number of inits is not the same as the preallocated buffer");
269 std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
272 OMPFirstprivateClause *
273 OMPFirstprivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
274 SourceLocation LParenLoc, SourceLocation EndLoc,
275 ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL,
276 ArrayRef<Expr *> InitVL, Stmt *PreInit) {
277 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * VL.size()));
278 OMPFirstprivateClause *Clause =
279 new (Mem) OMPFirstprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
280 Clause->setVarRefs(VL);
281 Clause->setPrivateCopies(PrivateVL);
282 Clause->setInits(InitVL);
283 Clause->setPreInitStmt(PreInit);
287 OMPFirstprivateClause *OMPFirstprivateClause::CreateEmpty(const ASTContext &C,
289 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * N));
290 return new (Mem) OMPFirstprivateClause(N);
293 void OMPLastprivateClause::setPrivateCopies(ArrayRef<Expr *> PrivateCopies) {
294 assert(PrivateCopies.size() == varlist_size() &&
295 "Number of private copies is not the same as the preallocated buffer");
296 std::copy(PrivateCopies.begin(), PrivateCopies.end(), varlist_end());
299 void OMPLastprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
300 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
301 "not the same as the "
302 "preallocated buffer");
303 std::copy(SrcExprs.begin(), SrcExprs.end(), getPrivateCopies().end());
306 void OMPLastprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
307 assert(DstExprs.size() == varlist_size() && "Number of destination "
308 "expressions is not the same as "
309 "the preallocated buffer");
310 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
313 void OMPLastprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
314 assert(AssignmentOps.size() == varlist_size() &&
315 "Number of assignment expressions is not the same as the preallocated "
317 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
318 getDestinationExprs().end());
321 OMPLastprivateClause *OMPLastprivateClause::Create(
322 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
323 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
324 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps, Stmt *PreInit,
326 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
327 OMPLastprivateClause *Clause =
328 new (Mem) OMPLastprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
329 Clause->setVarRefs(VL);
330 Clause->setSourceExprs(SrcExprs);
331 Clause->setDestinationExprs(DstExprs);
332 Clause->setAssignmentOps(AssignmentOps);
333 Clause->setPreInitStmt(PreInit);
334 Clause->setPostUpdateExpr(PostUpdate);
338 OMPLastprivateClause *OMPLastprivateClause::CreateEmpty(const ASTContext &C,
340 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
341 return new (Mem) OMPLastprivateClause(N);
344 OMPSharedClause *OMPSharedClause::Create(const ASTContext &C,
345 SourceLocation StartLoc,
346 SourceLocation LParenLoc,
347 SourceLocation EndLoc,
348 ArrayRef<Expr *> VL) {
349 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size()));
350 OMPSharedClause *Clause =
351 new (Mem) OMPSharedClause(StartLoc, LParenLoc, EndLoc, VL.size());
352 Clause->setVarRefs(VL);
356 OMPSharedClause *OMPSharedClause::CreateEmpty(const ASTContext &C, unsigned N) {
357 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
358 return new (Mem) OMPSharedClause(N);
361 void OMPLinearClause::setPrivates(ArrayRef<Expr *> PL) {
362 assert(PL.size() == varlist_size() &&
363 "Number of privates is not the same as the preallocated buffer");
364 std::copy(PL.begin(), PL.end(), varlist_end());
367 void OMPLinearClause::setInits(ArrayRef<Expr *> IL) {
368 assert(IL.size() == varlist_size() &&
369 "Number of inits is not the same as the preallocated buffer");
370 std::copy(IL.begin(), IL.end(), getPrivates().end());
373 void OMPLinearClause::setUpdates(ArrayRef<Expr *> UL) {
374 assert(UL.size() == varlist_size() &&
375 "Number of updates is not the same as the preallocated buffer");
376 std::copy(UL.begin(), UL.end(), getInits().end());
379 void OMPLinearClause::setFinals(ArrayRef<Expr *> FL) {
380 assert(FL.size() == varlist_size() &&
381 "Number of final updates is not the same as the preallocated buffer");
382 std::copy(FL.begin(), FL.end(), getUpdates().end());
385 OMPLinearClause *OMPLinearClause::Create(
386 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
387 OpenMPLinearClauseKind Modifier, SourceLocation ModifierLoc,
388 SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef<Expr *> VL,
389 ArrayRef<Expr *> PL, ArrayRef<Expr *> IL, Expr *Step, Expr *CalcStep,
390 Stmt *PreInit, Expr *PostUpdate) {
391 // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
392 // (Step and CalcStep).
393 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size() + 2));
394 OMPLinearClause *Clause = new (Mem) OMPLinearClause(
395 StartLoc, LParenLoc, Modifier, ModifierLoc, ColonLoc, EndLoc, VL.size());
396 Clause->setVarRefs(VL);
397 Clause->setPrivates(PL);
398 Clause->setInits(IL);
399 // Fill update and final expressions with zeroes, they are provided later,
400 // after the directive construction.
401 std::fill(Clause->getInits().end(), Clause->getInits().end() + VL.size(),
403 std::fill(Clause->getUpdates().end(), Clause->getUpdates().end() + VL.size(),
405 Clause->setStep(Step);
406 Clause->setCalcStep(CalcStep);
407 Clause->setPreInitStmt(PreInit);
408 Clause->setPostUpdateExpr(PostUpdate);
412 OMPLinearClause *OMPLinearClause::CreateEmpty(const ASTContext &C,
414 // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
415 // (Step and CalcStep).
416 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * NumVars + 2));
417 return new (Mem) OMPLinearClause(NumVars);
421 OMPAlignedClause::Create(const ASTContext &C, SourceLocation StartLoc,
422 SourceLocation LParenLoc, SourceLocation ColonLoc,
423 SourceLocation EndLoc, ArrayRef<Expr *> VL, Expr *A) {
424 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
425 OMPAlignedClause *Clause = new (Mem)
426 OMPAlignedClause(StartLoc, LParenLoc, ColonLoc, EndLoc, VL.size());
427 Clause->setVarRefs(VL);
428 Clause->setAlignment(A);
432 OMPAlignedClause *OMPAlignedClause::CreateEmpty(const ASTContext &C,
434 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumVars + 1));
435 return new (Mem) OMPAlignedClause(NumVars);
438 void OMPCopyinClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
439 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
440 "not the same as the "
441 "preallocated buffer");
442 std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
445 void OMPCopyinClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
446 assert(DstExprs.size() == varlist_size() && "Number of destination "
447 "expressions is not the same as "
448 "the preallocated buffer");
449 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
452 void OMPCopyinClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
453 assert(AssignmentOps.size() == varlist_size() &&
454 "Number of assignment expressions is not the same as the preallocated "
456 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
457 getDestinationExprs().end());
460 OMPCopyinClause *OMPCopyinClause::Create(
461 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
462 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
463 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
464 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
465 OMPCopyinClause *Clause =
466 new (Mem) OMPCopyinClause(StartLoc, LParenLoc, EndLoc, VL.size());
467 Clause->setVarRefs(VL);
468 Clause->setSourceExprs(SrcExprs);
469 Clause->setDestinationExprs(DstExprs);
470 Clause->setAssignmentOps(AssignmentOps);
474 OMPCopyinClause *OMPCopyinClause::CreateEmpty(const ASTContext &C, unsigned N) {
475 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
476 return new (Mem) OMPCopyinClause(N);
479 void OMPCopyprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
480 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
481 "not the same as the "
482 "preallocated buffer");
483 std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
486 void OMPCopyprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
487 assert(DstExprs.size() == varlist_size() && "Number of destination "
488 "expressions is not the same as "
489 "the preallocated buffer");
490 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
493 void OMPCopyprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
494 assert(AssignmentOps.size() == varlist_size() &&
495 "Number of assignment expressions is not the same as the preallocated "
497 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
498 getDestinationExprs().end());
501 OMPCopyprivateClause *OMPCopyprivateClause::Create(
502 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
503 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
504 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
505 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
506 OMPCopyprivateClause *Clause =
507 new (Mem) OMPCopyprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
508 Clause->setVarRefs(VL);
509 Clause->setSourceExprs(SrcExprs);
510 Clause->setDestinationExprs(DstExprs);
511 Clause->setAssignmentOps(AssignmentOps);
515 OMPCopyprivateClause *OMPCopyprivateClause::CreateEmpty(const ASTContext &C,
517 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
518 return new (Mem) OMPCopyprivateClause(N);
521 void OMPReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
522 assert(Privates.size() == varlist_size() &&
523 "Number of private copies is not the same as the preallocated buffer");
524 std::copy(Privates.begin(), Privates.end(), varlist_end());
527 void OMPReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
529 LHSExprs.size() == varlist_size() &&
530 "Number of LHS expressions is not the same as the preallocated buffer");
531 std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
534 void OMPReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
536 RHSExprs.size() == varlist_size() &&
537 "Number of RHS expressions is not the same as the preallocated buffer");
538 std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
541 void OMPReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
542 assert(ReductionOps.size() == varlist_size() && "Number of reduction "
543 "expressions is not the same "
544 "as the preallocated buffer");
545 std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
548 OMPReductionClause *OMPReductionClause::Create(
549 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
550 SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
551 NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
552 ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
553 ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps, Stmt *PreInit,
555 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
556 OMPReductionClause *Clause = new (Mem) OMPReductionClause(
557 StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
558 Clause->setVarRefs(VL);
559 Clause->setPrivates(Privates);
560 Clause->setLHSExprs(LHSExprs);
561 Clause->setRHSExprs(RHSExprs);
562 Clause->setReductionOps(ReductionOps);
563 Clause->setPreInitStmt(PreInit);
564 Clause->setPostUpdateExpr(PostUpdate);
568 OMPReductionClause *OMPReductionClause::CreateEmpty(const ASTContext &C,
570 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
571 return new (Mem) OMPReductionClause(N);
574 void OMPTaskReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
575 assert(Privates.size() == varlist_size() &&
576 "Number of private copies is not the same as the preallocated buffer");
577 std::copy(Privates.begin(), Privates.end(), varlist_end());
580 void OMPTaskReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
582 LHSExprs.size() == varlist_size() &&
583 "Number of LHS expressions is not the same as the preallocated buffer");
584 std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
587 void OMPTaskReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
589 RHSExprs.size() == varlist_size() &&
590 "Number of RHS expressions is not the same as the preallocated buffer");
591 std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
594 void OMPTaskReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
595 assert(ReductionOps.size() == varlist_size() && "Number of task reduction "
596 "expressions is not the same "
597 "as the preallocated buffer");
598 std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
601 OMPTaskReductionClause *OMPTaskReductionClause::Create(
602 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
603 SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
604 NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
605 ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
606 ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps, Stmt *PreInit,
608 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
609 OMPTaskReductionClause *Clause = new (Mem) OMPTaskReductionClause(
610 StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
611 Clause->setVarRefs(VL);
612 Clause->setPrivates(Privates);
613 Clause->setLHSExprs(LHSExprs);
614 Clause->setRHSExprs(RHSExprs);
615 Clause->setReductionOps(ReductionOps);
616 Clause->setPreInitStmt(PreInit);
617 Clause->setPostUpdateExpr(PostUpdate);
621 OMPTaskReductionClause *OMPTaskReductionClause::CreateEmpty(const ASTContext &C,
623 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
624 return new (Mem) OMPTaskReductionClause(N);
627 void OMPInReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
628 assert(Privates.size() == varlist_size() &&
629 "Number of private copies is not the same as the preallocated buffer");
630 std::copy(Privates.begin(), Privates.end(), varlist_end());
633 void OMPInReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
635 LHSExprs.size() == varlist_size() &&
636 "Number of LHS expressions is not the same as the preallocated buffer");
637 std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
640 void OMPInReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
642 RHSExprs.size() == varlist_size() &&
643 "Number of RHS expressions is not the same as the preallocated buffer");
644 std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
647 void OMPInReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
648 assert(ReductionOps.size() == varlist_size() && "Number of in reduction "
649 "expressions is not the same "
650 "as the preallocated buffer");
651 std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
654 void OMPInReductionClause::setTaskgroupDescriptors(
655 ArrayRef<Expr *> TaskgroupDescriptors) {
656 assert(TaskgroupDescriptors.size() == varlist_size() &&
657 "Number of in reduction descriptors is not the same as the "
658 "preallocated buffer");
659 std::copy(TaskgroupDescriptors.begin(), TaskgroupDescriptors.end(),
660 getReductionOps().end());
663 OMPInReductionClause *OMPInReductionClause::Create(
664 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
665 SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
666 NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
667 ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
668 ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps,
669 ArrayRef<Expr *> TaskgroupDescriptors, Stmt *PreInit, Expr *PostUpdate) {
670 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(6 * VL.size()));
671 OMPInReductionClause *Clause = new (Mem) OMPInReductionClause(
672 StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
673 Clause->setVarRefs(VL);
674 Clause->setPrivates(Privates);
675 Clause->setLHSExprs(LHSExprs);
676 Clause->setRHSExprs(RHSExprs);
677 Clause->setReductionOps(ReductionOps);
678 Clause->setTaskgroupDescriptors(TaskgroupDescriptors);
679 Clause->setPreInitStmt(PreInit);
680 Clause->setPostUpdateExpr(PostUpdate);
684 OMPInReductionClause *OMPInReductionClause::CreateEmpty(const ASTContext &C,
686 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(6 * N));
687 return new (Mem) OMPInReductionClause(N);
690 OMPFlushClause *OMPFlushClause::Create(const ASTContext &C,
691 SourceLocation StartLoc,
692 SourceLocation LParenLoc,
693 SourceLocation EndLoc,
694 ArrayRef<Expr *> VL) {
695 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
696 OMPFlushClause *Clause =
697 new (Mem) OMPFlushClause(StartLoc, LParenLoc, EndLoc, VL.size());
698 Clause->setVarRefs(VL);
702 OMPFlushClause *OMPFlushClause::CreateEmpty(const ASTContext &C, unsigned N) {
703 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
704 return new (Mem) OMPFlushClause(N);
708 OMPDependClause::Create(const ASTContext &C, SourceLocation StartLoc,
709 SourceLocation LParenLoc, SourceLocation EndLoc,
710 OpenMPDependClauseKind DepKind, SourceLocation DepLoc,
711 SourceLocation ColonLoc, ArrayRef<Expr *> VL,
713 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + NumLoops));
714 OMPDependClause *Clause = new (Mem)
715 OMPDependClause(StartLoc, LParenLoc, EndLoc, VL.size(), NumLoops);
716 Clause->setVarRefs(VL);
717 Clause->setDependencyKind(DepKind);
718 Clause->setDependencyLoc(DepLoc);
719 Clause->setColonLoc(ColonLoc);
720 for (unsigned I = 0 ; I < NumLoops; ++I)
721 Clause->setLoopData(I, nullptr);
725 OMPDependClause *OMPDependClause::CreateEmpty(const ASTContext &C, unsigned N,
727 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N + NumLoops));
728 return new (Mem) OMPDependClause(N, NumLoops);
731 void OMPDependClause::setLoopData(unsigned NumLoop, Expr *Cnt) {
732 assert((getDependencyKind() == OMPC_DEPEND_sink ||
733 getDependencyKind() == OMPC_DEPEND_source) &&
734 NumLoop < NumLoops &&
735 "Expected sink or source depend + loop index must be less number of "
737 auto It = std::next(getVarRefs().end(), NumLoop);
741 Expr *OMPDependClause::getLoopData(unsigned NumLoop) {
742 assert((getDependencyKind() == OMPC_DEPEND_sink ||
743 getDependencyKind() == OMPC_DEPEND_source) &&
744 NumLoop < NumLoops &&
745 "Expected sink or source depend + loop index must be less number of "
747 auto It = std::next(getVarRefs().end(), NumLoop);
751 const Expr *OMPDependClause::getLoopData(unsigned NumLoop) const {
752 assert((getDependencyKind() == OMPC_DEPEND_sink ||
753 getDependencyKind() == OMPC_DEPEND_source) &&
754 NumLoop < NumLoops &&
755 "Expected sink or source depend + loop index must be less number of "
757 auto It = std::next(getVarRefs().end(), NumLoop);
761 unsigned OMPClauseMappableExprCommon::getComponentsTotalNumber(
762 MappableExprComponentListsRef ComponentLists) {
763 unsigned TotalNum = 0u;
764 for (auto &C : ComponentLists)
765 TotalNum += C.size();
769 unsigned OMPClauseMappableExprCommon::getUniqueDeclarationsTotalNumber(
770 ArrayRef<const ValueDecl *> Declarations) {
771 unsigned TotalNum = 0u;
772 llvm::SmallPtrSet<const ValueDecl *, 8> Cache;
773 for (const ValueDecl *D : Declarations) {
774 const ValueDecl *VD = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr;
784 OMPMapClause::Create(const ASTContext &C, SourceLocation StartLoc,
785 SourceLocation LParenLoc, SourceLocation EndLoc,
786 ArrayRef<Expr *> Vars, ArrayRef<ValueDecl *> Declarations,
787 MappableExprComponentListsRef ComponentLists,
788 OpenMPMapClauseKind TypeModifier, OpenMPMapClauseKind Type,
789 bool TypeIsImplicit, SourceLocation TypeLoc) {
790 unsigned NumVars = Vars.size();
791 unsigned NumUniqueDeclarations =
792 getUniqueDeclarationsTotalNumber(Declarations);
793 unsigned NumComponentLists = ComponentLists.size();
794 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
796 // We need to allocate:
797 // NumVars x Expr* - we have an original list expression for each clause list
799 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
800 // with each component list.
801 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
802 // number of lists for each unique declaration and the size of each component
804 // NumComponents x MappableComponent - the total of all the components in all
806 void *Mem = C.Allocate(
807 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
808 OMPClauseMappableExprCommon::MappableComponent>(
809 NumVars, NumUniqueDeclarations,
810 NumUniqueDeclarations + NumComponentLists, NumComponents));
811 OMPMapClause *Clause = new (Mem) OMPMapClause(
812 TypeModifier, Type, TypeIsImplicit, TypeLoc, StartLoc, LParenLoc, EndLoc,
813 NumVars, NumUniqueDeclarations, NumComponentLists, NumComponents);
815 Clause->setVarRefs(Vars);
816 Clause->setClauseInfo(Declarations, ComponentLists);
817 Clause->setMapTypeModifier(TypeModifier);
818 Clause->setMapType(Type);
819 Clause->setMapLoc(TypeLoc);
823 OMPMapClause *OMPMapClause::CreateEmpty(const ASTContext &C, unsigned NumVars,
824 unsigned NumUniqueDeclarations,
825 unsigned NumComponentLists,
826 unsigned NumComponents) {
827 void *Mem = C.Allocate(
828 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
829 OMPClauseMappableExprCommon::MappableComponent>(
830 NumVars, NumUniqueDeclarations,
831 NumUniqueDeclarations + NumComponentLists, NumComponents));
832 return new (Mem) OMPMapClause(NumVars, NumUniqueDeclarations,
833 NumComponentLists, NumComponents);
836 OMPToClause *OMPToClause::Create(const ASTContext &C, SourceLocation StartLoc,
837 SourceLocation LParenLoc,
838 SourceLocation EndLoc, ArrayRef<Expr *> Vars,
839 ArrayRef<ValueDecl *> Declarations,
840 MappableExprComponentListsRef ComponentLists) {
841 unsigned NumVars = Vars.size();
842 unsigned NumUniqueDeclarations =
843 getUniqueDeclarationsTotalNumber(Declarations);
844 unsigned NumComponentLists = ComponentLists.size();
845 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
847 // We need to allocate:
848 // NumVars x Expr* - we have an original list expression for each clause list
850 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
851 // with each component list.
852 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
853 // number of lists for each unique declaration and the size of each component
855 // NumComponents x MappableComponent - the total of all the components in all
857 void *Mem = C.Allocate(
858 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
859 OMPClauseMappableExprCommon::MappableComponent>(
860 NumVars, NumUniqueDeclarations,
861 NumUniqueDeclarations + NumComponentLists, NumComponents));
863 OMPToClause *Clause = new (Mem)
864 OMPToClause(StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
865 NumComponentLists, NumComponents);
867 Clause->setVarRefs(Vars);
868 Clause->setClauseInfo(Declarations, ComponentLists);
872 OMPToClause *OMPToClause::CreateEmpty(const ASTContext &C, unsigned NumVars,
873 unsigned NumUniqueDeclarations,
874 unsigned NumComponentLists,
875 unsigned NumComponents) {
876 void *Mem = C.Allocate(
877 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
878 OMPClauseMappableExprCommon::MappableComponent>(
879 NumVars, NumUniqueDeclarations,
880 NumUniqueDeclarations + NumComponentLists, NumComponents));
881 return new (Mem) OMPToClause(NumVars, NumUniqueDeclarations,
882 NumComponentLists, NumComponents);
886 OMPFromClause::Create(const ASTContext &C, SourceLocation StartLoc,
887 SourceLocation LParenLoc, SourceLocation EndLoc,
888 ArrayRef<Expr *> Vars, ArrayRef<ValueDecl *> Declarations,
889 MappableExprComponentListsRef ComponentLists) {
890 unsigned NumVars = Vars.size();
891 unsigned NumUniqueDeclarations =
892 getUniqueDeclarationsTotalNumber(Declarations);
893 unsigned NumComponentLists = ComponentLists.size();
894 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
896 // We need to allocate:
897 // NumVars x Expr* - we have an original list expression for each clause list
899 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
900 // with each component list.
901 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
902 // number of lists for each unique declaration and the size of each component
904 // NumComponents x MappableComponent - the total of all the components in all
906 void *Mem = C.Allocate(
907 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
908 OMPClauseMappableExprCommon::MappableComponent>(
909 NumVars, NumUniqueDeclarations,
910 NumUniqueDeclarations + NumComponentLists, NumComponents));
912 OMPFromClause *Clause = new (Mem)
913 OMPFromClause(StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
914 NumComponentLists, NumComponents);
916 Clause->setVarRefs(Vars);
917 Clause->setClauseInfo(Declarations, ComponentLists);
921 OMPFromClause *OMPFromClause::CreateEmpty(const ASTContext &C, unsigned NumVars,
922 unsigned NumUniqueDeclarations,
923 unsigned NumComponentLists,
924 unsigned NumComponents) {
925 void *Mem = C.Allocate(
926 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
927 OMPClauseMappableExprCommon::MappableComponent>(
928 NumVars, NumUniqueDeclarations,
929 NumUniqueDeclarations + NumComponentLists, NumComponents));
930 return new (Mem) OMPFromClause(NumVars, NumUniqueDeclarations,
931 NumComponentLists, NumComponents);
934 void OMPUseDevicePtrClause::setPrivateCopies(ArrayRef<Expr *> VL) {
935 assert(VL.size() == varlist_size() &&
936 "Number of private copies is not the same as the preallocated buffer");
937 std::copy(VL.begin(), VL.end(), varlist_end());
940 void OMPUseDevicePtrClause::setInits(ArrayRef<Expr *> VL) {
941 assert(VL.size() == varlist_size() &&
942 "Number of inits is not the same as the preallocated buffer");
943 std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
946 OMPUseDevicePtrClause *OMPUseDevicePtrClause::Create(
947 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
948 SourceLocation EndLoc, ArrayRef<Expr *> Vars, ArrayRef<Expr *> PrivateVars,
949 ArrayRef<Expr *> Inits, ArrayRef<ValueDecl *> Declarations,
950 MappableExprComponentListsRef ComponentLists) {
951 unsigned NumVars = Vars.size();
952 unsigned NumUniqueDeclarations =
953 getUniqueDeclarationsTotalNumber(Declarations);
954 unsigned NumComponentLists = ComponentLists.size();
955 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
957 // We need to allocate:
958 // 3 x NumVars x Expr* - we have an original list expression for each clause
959 // list entry and an equal number of private copies and inits.
960 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
961 // with each component list.
962 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
963 // number of lists for each unique declaration and the size of each component
965 // NumComponents x MappableComponent - the total of all the components in all
967 void *Mem = C.Allocate(
968 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
969 OMPClauseMappableExprCommon::MappableComponent>(
970 3 * NumVars, NumUniqueDeclarations,
971 NumUniqueDeclarations + NumComponentLists, NumComponents));
973 OMPUseDevicePtrClause *Clause = new (Mem) OMPUseDevicePtrClause(
974 StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
975 NumComponentLists, NumComponents);
977 Clause->setVarRefs(Vars);
978 Clause->setPrivateCopies(PrivateVars);
979 Clause->setInits(Inits);
980 Clause->setClauseInfo(Declarations, ComponentLists);
984 OMPUseDevicePtrClause *OMPUseDevicePtrClause::CreateEmpty(
985 const ASTContext &C, unsigned NumVars, unsigned NumUniqueDeclarations,
986 unsigned NumComponentLists, unsigned NumComponents) {
987 void *Mem = C.Allocate(
988 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
989 OMPClauseMappableExprCommon::MappableComponent>(
990 3 * NumVars, NumUniqueDeclarations,
991 NumUniqueDeclarations + NumComponentLists, NumComponents));
992 return new (Mem) OMPUseDevicePtrClause(NumVars, NumUniqueDeclarations,
993 NumComponentLists, NumComponents);
996 OMPIsDevicePtrClause *
997 OMPIsDevicePtrClause::Create(const ASTContext &C, SourceLocation StartLoc,
998 SourceLocation LParenLoc, SourceLocation EndLoc,
999 ArrayRef<Expr *> Vars,
1000 ArrayRef<ValueDecl *> Declarations,
1001 MappableExprComponentListsRef ComponentLists) {
1002 unsigned NumVars = Vars.size();
1003 unsigned NumUniqueDeclarations =
1004 getUniqueDeclarationsTotalNumber(Declarations);
1005 unsigned NumComponentLists = ComponentLists.size();
1006 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
1008 // We need to allocate:
1009 // NumVars x Expr* - we have an original list expression for each clause list
1011 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
1012 // with each component list.
1013 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
1014 // number of lists for each unique declaration and the size of each component
1016 // NumComponents x MappableComponent - the total of all the components in all
1018 void *Mem = C.Allocate(
1019 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1020 OMPClauseMappableExprCommon::MappableComponent>(
1021 NumVars, NumUniqueDeclarations,
1022 NumUniqueDeclarations + NumComponentLists, NumComponents));
1024 OMPIsDevicePtrClause *Clause = new (Mem) OMPIsDevicePtrClause(
1025 StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
1026 NumComponentLists, NumComponents);
1028 Clause->setVarRefs(Vars);
1029 Clause->setClauseInfo(Declarations, ComponentLists);
1033 OMPIsDevicePtrClause *OMPIsDevicePtrClause::CreateEmpty(
1034 const ASTContext &C, unsigned NumVars, unsigned NumUniqueDeclarations,
1035 unsigned NumComponentLists, unsigned NumComponents) {
1036 void *Mem = C.Allocate(
1037 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1038 OMPClauseMappableExprCommon::MappableComponent>(
1039 NumVars, NumUniqueDeclarations,
1040 NumUniqueDeclarations + NumComponentLists, NumComponents));
1041 return new (Mem) OMPIsDevicePtrClause(NumVars, NumUniqueDeclarations,
1042 NumComponentLists, NumComponents);