blob: 0dcf82d6bff9b9ce6474d81823243c1a4ef14090 [file] [log] [blame]
James Y Knightb8bfd962015-10-02 13:41:04 +00001//===--- OpenMPClause.cpp - Classes for OpenMP clauses --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the subclesses of Stmt class declared in OpenMPClause.h
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/OpenMPClause.h"
15
16#include "clang/AST/ASTContext.h"
17
18using namespace clang;
19
20OMPClause::child_range OMPClause::children() {
21 switch (getClauseKind()) {
22 default:
23 break;
24#define OPENMP_CLAUSE(Name, Class) \
25 case OMPC_##Name: \
26 return static_cast<Class *>(this)->children();
27#include "clang/Basic/OpenMPKinds.def"
28 }
29 llvm_unreachable("unknown OMPClause");
30}
31
Alexey Bataev3392d762016-02-16 11:18:12 +000032OMPClauseWithPreInit *OMPClauseWithPreInit::get(OMPClause *C) {
33 auto *Res = OMPClauseWithPreInit::get(const_cast<const OMPClause *>(C));
34 return Res ? const_cast<OMPClauseWithPreInit *>(Res) : nullptr;
35}
36
37const OMPClauseWithPreInit *OMPClauseWithPreInit::get(const OMPClause *C) {
38 switch (C->getClauseKind()) {
39 case OMPC_schedule:
40 return static_cast<const OMPScheduleClause *>(C);
41 case OMPC_dist_schedule:
42 return static_cast<const OMPDistScheduleClause *>(C);
Alexey Bataev417089f2016-02-17 13:19:37 +000043 case OMPC_firstprivate:
44 return static_cast<const OMPFirstprivateClause *>(C);
Alexey Bataev005248a2016-02-25 05:25:57 +000045 case OMPC_lastprivate:
46 return static_cast<const OMPLastprivateClause *>(C);
Alexey Bataev61205072016-03-02 04:57:40 +000047 case OMPC_reduction:
48 return static_cast<const OMPReductionClause *>(C);
Alexey Bataev78849fb2016-03-09 09:49:00 +000049 case OMPC_linear:
50 return static_cast<const OMPLinearClause *>(C);
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000051 case OMPC_if:
52 return static_cast<const OMPIfClause *>(C);
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +000053 case OMPC_num_threads:
54 return static_cast<const OMPNumThreadsClause *>(C);
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000055 case OMPC_num_teams:
56 return static_cast<const OMPNumTeamsClause *>(C);
Alexey Bataev3392d762016-02-16 11:18:12 +000057 case OMPC_default:
58 case OMPC_proc_bind:
Alexey Bataev3392d762016-02-16 11:18:12 +000059 case OMPC_final:
Alexey Bataev3392d762016-02-16 11:18:12 +000060 case OMPC_safelen:
61 case OMPC_simdlen:
62 case OMPC_collapse:
63 case OMPC_private:
Alexey Bataev005248a2016-02-25 05:25:57 +000064 case OMPC_shared:
Alexey Bataev005248a2016-02-25 05:25:57 +000065 case OMPC_aligned:
66 case OMPC_copyin:
67 case OMPC_copyprivate:
68 case OMPC_ordered:
69 case OMPC_nowait:
70 case OMPC_untied:
71 case OMPC_mergeable:
72 case OMPC_threadprivate:
73 case OMPC_flush:
74 case OMPC_read:
75 case OMPC_write:
76 case OMPC_update:
77 case OMPC_capture:
78 case OMPC_seq_cst:
79 case OMPC_depend:
80 case OMPC_device:
81 case OMPC_threads:
82 case OMPC_simd:
83 case OMPC_map:
Alexey Bataev005248a2016-02-25 05:25:57 +000084 case OMPC_thread_limit:
85 case OMPC_priority:
86 case OMPC_grainsize:
87 case OMPC_nogroup:
88 case OMPC_num_tasks:
89 case OMPC_hint:
90 case OMPC_defaultmap:
91 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +000092 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +000093 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +000094 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +000095 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +000096 case OMPC_is_device_ptr:
Alexey Bataev005248a2016-02-25 05:25:57 +000097 break;
98 }
99
100 return nullptr;
101}
102
103OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(OMPClause *C) {
104 auto *Res = OMPClauseWithPostUpdate::get(const_cast<const OMPClause *>(C));
105 return Res ? const_cast<OMPClauseWithPostUpdate *>(Res) : nullptr;
106}
107
108const OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(const OMPClause *C) {
109 switch (C->getClauseKind()) {
Alexey Bataev3392d762016-02-16 11:18:12 +0000110 case OMPC_lastprivate:
Alexey Bataev005248a2016-02-25 05:25:57 +0000111 return static_cast<const OMPLastprivateClause *>(C);
Alexey Bataev61205072016-03-02 04:57:40 +0000112 case OMPC_reduction:
113 return static_cast<const OMPReductionClause *>(C);
Alexey Bataev78849fb2016-03-09 09:49:00 +0000114 case OMPC_linear:
115 return static_cast<const OMPLinearClause *>(C);
Alexey Bataev005248a2016-02-25 05:25:57 +0000116 case OMPC_schedule:
117 case OMPC_dist_schedule:
118 case OMPC_firstprivate:
119 case OMPC_default:
120 case OMPC_proc_bind:
121 case OMPC_if:
122 case OMPC_final:
123 case OMPC_num_threads:
124 case OMPC_safelen:
125 case OMPC_simdlen:
126 case OMPC_collapse:
127 case OMPC_private:
Alexey Bataev3392d762016-02-16 11:18:12 +0000128 case OMPC_shared:
Alexey Bataev3392d762016-02-16 11:18:12 +0000129 case OMPC_aligned:
130 case OMPC_copyin:
131 case OMPC_copyprivate:
132 case OMPC_ordered:
133 case OMPC_nowait:
134 case OMPC_untied:
135 case OMPC_mergeable:
136 case OMPC_threadprivate:
137 case OMPC_flush:
138 case OMPC_read:
139 case OMPC_write:
140 case OMPC_update:
141 case OMPC_capture:
142 case OMPC_seq_cst:
143 case OMPC_depend:
144 case OMPC_device:
145 case OMPC_threads:
146 case OMPC_simd:
147 case OMPC_map:
148 case OMPC_num_teams:
149 case OMPC_thread_limit:
150 case OMPC_priority:
151 case OMPC_grainsize:
152 case OMPC_nogroup:
153 case OMPC_num_tasks:
154 case OMPC_hint:
155 case OMPC_defaultmap:
156 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000157 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +0000158 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +0000159 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +0000160 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +0000161 case OMPC_is_device_ptr:
Alexey Bataev3392d762016-02-16 11:18:12 +0000162 break;
163 }
164
165 return nullptr;
166}
167
James Y Knightb8bfd962015-10-02 13:41:04 +0000168void OMPPrivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
169 assert(VL.size() == varlist_size() &&
170 "Number of private copies is not the same as the preallocated buffer");
171 std::copy(VL.begin(), VL.end(), varlist_end());
172}
173
174OMPPrivateClause *
175OMPPrivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
176 SourceLocation LParenLoc, SourceLocation EndLoc,
177 ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL) {
178 // Allocate space for private variables and initializer expressions.
James Y Knight374fd202016-01-01 00:38:24 +0000179 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000180 OMPPrivateClause *Clause =
181 new (Mem) OMPPrivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
182 Clause->setVarRefs(VL);
183 Clause->setPrivateCopies(PrivateVL);
184 return Clause;
185}
186
187OMPPrivateClause *OMPPrivateClause::CreateEmpty(const ASTContext &C,
188 unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000189 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000190 return new (Mem) OMPPrivateClause(N);
191}
192
193void OMPFirstprivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
194 assert(VL.size() == varlist_size() &&
195 "Number of private copies is not the same as the preallocated buffer");
196 std::copy(VL.begin(), VL.end(), varlist_end());
197}
198
199void OMPFirstprivateClause::setInits(ArrayRef<Expr *> VL) {
200 assert(VL.size() == varlist_size() &&
201 "Number of inits is not the same as the preallocated buffer");
202 std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
203}
204
205OMPFirstprivateClause *
206OMPFirstprivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
207 SourceLocation LParenLoc, SourceLocation EndLoc,
208 ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL,
Alexey Bataev417089f2016-02-17 13:19:37 +0000209 ArrayRef<Expr *> InitVL, Stmt *PreInit) {
James Y Knight374fd202016-01-01 00:38:24 +0000210 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000211 OMPFirstprivateClause *Clause =
212 new (Mem) OMPFirstprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
213 Clause->setVarRefs(VL);
214 Clause->setPrivateCopies(PrivateVL);
215 Clause->setInits(InitVL);
Alexey Bataev417089f2016-02-17 13:19:37 +0000216 Clause->setPreInitStmt(PreInit);
James Y Knightb8bfd962015-10-02 13:41:04 +0000217 return Clause;
218}
219
220OMPFirstprivateClause *OMPFirstprivateClause::CreateEmpty(const ASTContext &C,
221 unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000222 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000223 return new (Mem) OMPFirstprivateClause(N);
224}
225
226void OMPLastprivateClause::setPrivateCopies(ArrayRef<Expr *> PrivateCopies) {
227 assert(PrivateCopies.size() == varlist_size() &&
228 "Number of private copies is not the same as the preallocated buffer");
229 std::copy(PrivateCopies.begin(), PrivateCopies.end(), varlist_end());
230}
231
232void OMPLastprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
233 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
234 "not the same as the "
235 "preallocated buffer");
236 std::copy(SrcExprs.begin(), SrcExprs.end(), getPrivateCopies().end());
237}
238
239void OMPLastprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
240 assert(DstExprs.size() == varlist_size() && "Number of destination "
241 "expressions is not the same as "
242 "the preallocated buffer");
243 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
244}
245
246void OMPLastprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
247 assert(AssignmentOps.size() == varlist_size() &&
248 "Number of assignment expressions is not the same as the preallocated "
249 "buffer");
250 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
251 getDestinationExprs().end());
252}
253
254OMPLastprivateClause *OMPLastprivateClause::Create(
255 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
256 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
Alexey Bataev005248a2016-02-25 05:25:57 +0000257 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps, Stmt *PreInit,
258 Expr *PostUpdate) {
James Y Knight374fd202016-01-01 00:38:24 +0000259 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000260 OMPLastprivateClause *Clause =
261 new (Mem) OMPLastprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
262 Clause->setVarRefs(VL);
263 Clause->setSourceExprs(SrcExprs);
264 Clause->setDestinationExprs(DstExprs);
265 Clause->setAssignmentOps(AssignmentOps);
Alexey Bataev005248a2016-02-25 05:25:57 +0000266 Clause->setPreInitStmt(PreInit);
267 Clause->setPostUpdateExpr(PostUpdate);
James Y Knightb8bfd962015-10-02 13:41:04 +0000268 return Clause;
269}
270
271OMPLastprivateClause *OMPLastprivateClause::CreateEmpty(const ASTContext &C,
272 unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000273 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000274 return new (Mem) OMPLastprivateClause(N);
275}
276
277OMPSharedClause *OMPSharedClause::Create(const ASTContext &C,
278 SourceLocation StartLoc,
279 SourceLocation LParenLoc,
280 SourceLocation EndLoc,
281 ArrayRef<Expr *> VL) {
James Y Knight374fd202016-01-01 00:38:24 +0000282 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000283 OMPSharedClause *Clause =
284 new (Mem) OMPSharedClause(StartLoc, LParenLoc, EndLoc, VL.size());
285 Clause->setVarRefs(VL);
286 return Clause;
287}
288
289OMPSharedClause *OMPSharedClause::CreateEmpty(const ASTContext &C, unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000290 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000291 return new (Mem) OMPSharedClause(N);
292}
293
294void OMPLinearClause::setPrivates(ArrayRef<Expr *> PL) {
295 assert(PL.size() == varlist_size() &&
296 "Number of privates is not the same as the preallocated buffer");
297 std::copy(PL.begin(), PL.end(), varlist_end());
298}
299
300void OMPLinearClause::setInits(ArrayRef<Expr *> IL) {
301 assert(IL.size() == varlist_size() &&
302 "Number of inits is not the same as the preallocated buffer");
303 std::copy(IL.begin(), IL.end(), getPrivates().end());
304}
305
306void OMPLinearClause::setUpdates(ArrayRef<Expr *> UL) {
307 assert(UL.size() == varlist_size() &&
308 "Number of updates is not the same as the preallocated buffer");
309 std::copy(UL.begin(), UL.end(), getInits().end());
310}
311
312void OMPLinearClause::setFinals(ArrayRef<Expr *> FL) {
313 assert(FL.size() == varlist_size() &&
314 "Number of final updates is not the same as the preallocated buffer");
315 std::copy(FL.begin(), FL.end(), getUpdates().end());
316}
317
318OMPLinearClause *OMPLinearClause::Create(
319 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
320 OpenMPLinearClauseKind Modifier, SourceLocation ModifierLoc,
321 SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef<Expr *> VL,
Alexey Bataev78849fb2016-03-09 09:49:00 +0000322 ArrayRef<Expr *> PL, ArrayRef<Expr *> IL, Expr *Step, Expr *CalcStep,
323 Stmt *PreInit, Expr *PostUpdate) {
James Y Knightb8bfd962015-10-02 13:41:04 +0000324 // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
325 // (Step and CalcStep).
James Y Knight374fd202016-01-01 00:38:24 +0000326 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size() + 2));
James Y Knightb8bfd962015-10-02 13:41:04 +0000327 OMPLinearClause *Clause = new (Mem) OMPLinearClause(
328 StartLoc, LParenLoc, Modifier, ModifierLoc, ColonLoc, EndLoc, VL.size());
329 Clause->setVarRefs(VL);
330 Clause->setPrivates(PL);
331 Clause->setInits(IL);
332 // Fill update and final expressions with zeroes, they are provided later,
333 // after the directive construction.
334 std::fill(Clause->getInits().end(), Clause->getInits().end() + VL.size(),
335 nullptr);
336 std::fill(Clause->getUpdates().end(), Clause->getUpdates().end() + VL.size(),
337 nullptr);
338 Clause->setStep(Step);
339 Clause->setCalcStep(CalcStep);
Alexey Bataev78849fb2016-03-09 09:49:00 +0000340 Clause->setPreInitStmt(PreInit);
341 Clause->setPostUpdateExpr(PostUpdate);
James Y Knightb8bfd962015-10-02 13:41:04 +0000342 return Clause;
343}
344
345OMPLinearClause *OMPLinearClause::CreateEmpty(const ASTContext &C,
346 unsigned NumVars) {
347 // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
348 // (Step and CalcStep).
James Y Knight374fd202016-01-01 00:38:24 +0000349 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * NumVars + 2));
James Y Knightb8bfd962015-10-02 13:41:04 +0000350 return new (Mem) OMPLinearClause(NumVars);
351}
352
353OMPAlignedClause *
354OMPAlignedClause::Create(const ASTContext &C, SourceLocation StartLoc,
355 SourceLocation LParenLoc, SourceLocation ColonLoc,
356 SourceLocation EndLoc, ArrayRef<Expr *> VL, Expr *A) {
James Y Knight374fd202016-01-01 00:38:24 +0000357 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
James Y Knightb8bfd962015-10-02 13:41:04 +0000358 OMPAlignedClause *Clause = new (Mem)
359 OMPAlignedClause(StartLoc, LParenLoc, ColonLoc, EndLoc, VL.size());
360 Clause->setVarRefs(VL);
361 Clause->setAlignment(A);
362 return Clause;
363}
364
365OMPAlignedClause *OMPAlignedClause::CreateEmpty(const ASTContext &C,
366 unsigned NumVars) {
James Y Knight374fd202016-01-01 00:38:24 +0000367 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumVars + 1));
James Y Knightb8bfd962015-10-02 13:41:04 +0000368 return new (Mem) OMPAlignedClause(NumVars);
369}
370
371void OMPCopyinClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
372 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
373 "not the same as the "
374 "preallocated buffer");
375 std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
376}
377
378void OMPCopyinClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
379 assert(DstExprs.size() == varlist_size() && "Number of destination "
380 "expressions is not the same as "
381 "the preallocated buffer");
382 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
383}
384
385void OMPCopyinClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
386 assert(AssignmentOps.size() == varlist_size() &&
387 "Number of assignment expressions is not the same as the preallocated "
388 "buffer");
389 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
390 getDestinationExprs().end());
391}
392
393OMPCopyinClause *OMPCopyinClause::Create(
394 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
395 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
396 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
James Y Knight374fd202016-01-01 00:38:24 +0000397 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000398 OMPCopyinClause *Clause =
399 new (Mem) OMPCopyinClause(StartLoc, LParenLoc, EndLoc, VL.size());
400 Clause->setVarRefs(VL);
401 Clause->setSourceExprs(SrcExprs);
402 Clause->setDestinationExprs(DstExprs);
403 Clause->setAssignmentOps(AssignmentOps);
404 return Clause;
405}
406
407OMPCopyinClause *OMPCopyinClause::CreateEmpty(const ASTContext &C, unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000408 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000409 return new (Mem) OMPCopyinClause(N);
410}
411
412void OMPCopyprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
413 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
414 "not the same as the "
415 "preallocated buffer");
416 std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
417}
418
419void OMPCopyprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
420 assert(DstExprs.size() == varlist_size() && "Number of destination "
421 "expressions is not the same as "
422 "the preallocated buffer");
423 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
424}
425
426void OMPCopyprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
427 assert(AssignmentOps.size() == varlist_size() &&
428 "Number of assignment expressions is not the same as the preallocated "
429 "buffer");
430 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
431 getDestinationExprs().end());
432}
433
434OMPCopyprivateClause *OMPCopyprivateClause::Create(
435 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
436 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
437 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
James Y Knight374fd202016-01-01 00:38:24 +0000438 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000439 OMPCopyprivateClause *Clause =
440 new (Mem) OMPCopyprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
441 Clause->setVarRefs(VL);
442 Clause->setSourceExprs(SrcExprs);
443 Clause->setDestinationExprs(DstExprs);
444 Clause->setAssignmentOps(AssignmentOps);
445 return Clause;
446}
447
448OMPCopyprivateClause *OMPCopyprivateClause::CreateEmpty(const ASTContext &C,
449 unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000450 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000451 return new (Mem) OMPCopyprivateClause(N);
452}
453
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000454void OMPReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
455 assert(Privates.size() == varlist_size() &&
456 "Number of private copies is not the same as the preallocated buffer");
457 std::copy(Privates.begin(), Privates.end(), varlist_end());
458}
459
James Y Knightb8bfd962015-10-02 13:41:04 +0000460void OMPReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
461 assert(
462 LHSExprs.size() == varlist_size() &&
463 "Number of LHS expressions is not the same as the preallocated buffer");
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000464 std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
James Y Knightb8bfd962015-10-02 13:41:04 +0000465}
466
467void OMPReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
468 assert(
469 RHSExprs.size() == varlist_size() &&
470 "Number of RHS expressions is not the same as the preallocated buffer");
471 std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
472}
473
474void OMPReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
475 assert(ReductionOps.size() == varlist_size() && "Number of reduction "
476 "expressions is not the same "
477 "as the preallocated buffer");
478 std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
479}
480
481OMPReductionClause *OMPReductionClause::Create(
482 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
483 SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
484 NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000485 ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
Alexey Bataev61205072016-03-02 04:57:40 +0000486 ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps, Stmt *PreInit,
487 Expr *PostUpdate) {
James Y Knight374fd202016-01-01 00:38:24 +0000488 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000489 OMPReductionClause *Clause = new (Mem) OMPReductionClause(
490 StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
491 Clause->setVarRefs(VL);
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000492 Clause->setPrivates(Privates);
James Y Knightb8bfd962015-10-02 13:41:04 +0000493 Clause->setLHSExprs(LHSExprs);
494 Clause->setRHSExprs(RHSExprs);
495 Clause->setReductionOps(ReductionOps);
Alexey Bataev61205072016-03-02 04:57:40 +0000496 Clause->setPreInitStmt(PreInit);
497 Clause->setPostUpdateExpr(PostUpdate);
James Y Knightb8bfd962015-10-02 13:41:04 +0000498 return Clause;
499}
500
501OMPReductionClause *OMPReductionClause::CreateEmpty(const ASTContext &C,
502 unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000503 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000504 return new (Mem) OMPReductionClause(N);
505}
506
507OMPFlushClause *OMPFlushClause::Create(const ASTContext &C,
508 SourceLocation StartLoc,
509 SourceLocation LParenLoc,
510 SourceLocation EndLoc,
511 ArrayRef<Expr *> VL) {
Alexey Bataev8b427062016-05-25 12:36:08 +0000512 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
James Y Knightb8bfd962015-10-02 13:41:04 +0000513 OMPFlushClause *Clause =
514 new (Mem) OMPFlushClause(StartLoc, LParenLoc, EndLoc, VL.size());
515 Clause->setVarRefs(VL);
516 return Clause;
517}
518
519OMPFlushClause *OMPFlushClause::CreateEmpty(const ASTContext &C, unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000520 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000521 return new (Mem) OMPFlushClause(N);
522}
523
Alexey Bataev8b427062016-05-25 12:36:08 +0000524OMPDependClause *OMPDependClause::Create(
525 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
526 SourceLocation EndLoc, OpenMPDependClauseKind DepKind,
527 SourceLocation DepLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL) {
528 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
James Y Knightb8bfd962015-10-02 13:41:04 +0000529 OMPDependClause *Clause =
530 new (Mem) OMPDependClause(StartLoc, LParenLoc, EndLoc, VL.size());
531 Clause->setVarRefs(VL);
532 Clause->setDependencyKind(DepKind);
533 Clause->setDependencyLoc(DepLoc);
534 Clause->setColonLoc(ColonLoc);
Alexey Bataev8b427062016-05-25 12:36:08 +0000535 Clause->setCounterValue(nullptr);
James Y Knightb8bfd962015-10-02 13:41:04 +0000536 return Clause;
537}
538
539OMPDependClause *OMPDependClause::CreateEmpty(const ASTContext &C, unsigned N) {
Alexey Bataev2af820542016-05-25 12:51:24 +0000540 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N + 1));
James Y Knightb8bfd962015-10-02 13:41:04 +0000541 return new (Mem) OMPDependClause(N);
542}
Kelvin Li0bff7af2015-11-23 05:32:03 +0000543
Alexey Bataev8b427062016-05-25 12:36:08 +0000544void OMPDependClause::setCounterValue(Expr *V) {
545 assert(getDependencyKind() == OMPC_DEPEND_sink ||
546 getDependencyKind() == OMPC_DEPEND_source || V == nullptr);
547 *getVarRefs().end() = V;
548}
549
550const Expr *OMPDependClause::getCounterValue() const {
551 auto *V = *getVarRefs().end();
552 assert(getDependencyKind() == OMPC_DEPEND_sink ||
553 getDependencyKind() == OMPC_DEPEND_source || V == nullptr);
554 return V;
555}
556
557Expr *OMPDependClause::getCounterValue() {
558 auto *V = *getVarRefs().end();
559 assert(getDependencyKind() == OMPC_DEPEND_sink ||
560 getDependencyKind() == OMPC_DEPEND_source || V == nullptr);
561 return V;
562}
563
Samuel Antao90927002016-04-26 14:54:23 +0000564unsigned OMPClauseMappableExprCommon::getComponentsTotalNumber(
565 MappableExprComponentListsRef ComponentLists) {
566 unsigned TotalNum = 0u;
567 for (auto &C : ComponentLists)
568 TotalNum += C.size();
569 return TotalNum;
570}
571
572unsigned OMPClauseMappableExprCommon::getUniqueDeclarationsTotalNumber(
573 ArrayRef<ValueDecl *> Declarations) {
574 unsigned TotalNum = 0u;
575 llvm::SmallPtrSet<const ValueDecl *, 8> Cache;
576 for (auto *D : Declarations) {
577 const ValueDecl *VD = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr;
578 if (Cache.count(VD))
579 continue;
580 ++TotalNum;
581 Cache.insert(VD);
582 }
583 return TotalNum;
584}
585
586OMPMapClause *
587OMPMapClause::Create(const ASTContext &C, SourceLocation StartLoc,
588 SourceLocation LParenLoc, SourceLocation EndLoc,
589 ArrayRef<Expr *> Vars, ArrayRef<ValueDecl *> Declarations,
590 MappableExprComponentListsRef ComponentLists,
591 OpenMPMapClauseKind TypeModifier, OpenMPMapClauseKind Type,
592 bool TypeIsImplicit, SourceLocation TypeLoc) {
593
594 unsigned NumVars = Vars.size();
595 unsigned NumUniqueDeclarations =
596 getUniqueDeclarationsTotalNumber(Declarations);
597 unsigned NumComponentLists = ComponentLists.size();
598 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
599
600 // We need to allocate:
601 // NumVars x Expr* - we have an original list expression for each clause list
602 // entry.
603 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
604 // with each component list.
605 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
606 // number of lists for each unique declaration and the size of each component
607 // list.
608 // NumComponents x MappableComponent - the total of all the components in all
609 // the lists.
610 void *Mem = C.Allocate(
611 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
612 OMPClauseMappableExprCommon::MappableComponent>(
613 NumVars, NumUniqueDeclarations,
614 NumUniqueDeclarations + NumComponentLists, NumComponents));
615 OMPMapClause *Clause = new (Mem) OMPMapClause(
616 TypeModifier, Type, TypeIsImplicit, TypeLoc, StartLoc, LParenLoc, EndLoc,
617 NumVars, NumUniqueDeclarations, NumComponentLists, NumComponents);
618
619 Clause->setVarRefs(Vars);
620 Clause->setClauseInfo(Declarations, ComponentLists);
Kelvin Li0bff7af2015-11-23 05:32:03 +0000621 Clause->setMapTypeModifier(TypeModifier);
622 Clause->setMapType(Type);
623 Clause->setMapLoc(TypeLoc);
624 return Clause;
625}
626
Samuel Antao90927002016-04-26 14:54:23 +0000627OMPMapClause *OMPMapClause::CreateEmpty(const ASTContext &C, unsigned NumVars,
628 unsigned NumUniqueDeclarations,
629 unsigned NumComponentLists,
630 unsigned NumComponents) {
631 void *Mem = C.Allocate(
632 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
633 OMPClauseMappableExprCommon::MappableComponent>(
634 NumVars, NumUniqueDeclarations,
635 NumUniqueDeclarations + NumComponentLists, NumComponents));
636 return new (Mem) OMPMapClause(NumVars, NumUniqueDeclarations,
637 NumComponentLists, NumComponents);
Kelvin Li0bff7af2015-11-23 05:32:03 +0000638}
Samuel Antao661c0902016-05-26 17:39:58 +0000639
640OMPToClause *OMPToClause::Create(const ASTContext &C, SourceLocation StartLoc,
641 SourceLocation LParenLoc,
642 SourceLocation EndLoc, ArrayRef<Expr *> Vars,
643 ArrayRef<ValueDecl *> Declarations,
644 MappableExprComponentListsRef ComponentLists) {
645 unsigned NumVars = Vars.size();
646 unsigned NumUniqueDeclarations =
647 getUniqueDeclarationsTotalNumber(Declarations);
648 unsigned NumComponentLists = ComponentLists.size();
649 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
650
651 // We need to allocate:
652 // NumVars x Expr* - we have an original list expression for each clause list
653 // entry.
654 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
655 // with each component list.
656 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
657 // number of lists for each unique declaration and the size of each component
658 // list.
659 // NumComponents x MappableComponent - the total of all the components in all
660 // the lists.
661 void *Mem = C.Allocate(
662 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
663 OMPClauseMappableExprCommon::MappableComponent>(
664 NumVars, NumUniqueDeclarations,
665 NumUniqueDeclarations + NumComponentLists, NumComponents));
666
667 OMPToClause *Clause = new (Mem)
668 OMPToClause(StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
669 NumComponentLists, NumComponents);
670
671 Clause->setVarRefs(Vars);
672 Clause->setClauseInfo(Declarations, ComponentLists);
673 return Clause;
674}
675
676OMPToClause *OMPToClause::CreateEmpty(const ASTContext &C, unsigned NumVars,
677 unsigned NumUniqueDeclarations,
678 unsigned NumComponentLists,
679 unsigned NumComponents) {
680 void *Mem = C.Allocate(
681 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
682 OMPClauseMappableExprCommon::MappableComponent>(
683 NumVars, NumUniqueDeclarations,
684 NumUniqueDeclarations + NumComponentLists, NumComponents));
685 return new (Mem) OMPToClause(NumVars, NumUniqueDeclarations,
686 NumComponentLists, NumComponents);
687}
Samuel Antaoec172c62016-05-26 17:49:04 +0000688
689OMPFromClause *
690OMPFromClause::Create(const ASTContext &C, SourceLocation StartLoc,
691 SourceLocation LParenLoc, SourceLocation EndLoc,
692 ArrayRef<Expr *> Vars, ArrayRef<ValueDecl *> Declarations,
693 MappableExprComponentListsRef ComponentLists) {
694 unsigned NumVars = Vars.size();
695 unsigned NumUniqueDeclarations =
696 getUniqueDeclarationsTotalNumber(Declarations);
697 unsigned NumComponentLists = ComponentLists.size();
698 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
699
700 // We need to allocate:
701 // NumVars x Expr* - we have an original list expression for each clause list
702 // entry.
703 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
704 // with each component list.
705 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
706 // number of lists for each unique declaration and the size of each component
707 // list.
708 // NumComponents x MappableComponent - the total of all the components in all
709 // the lists.
710 void *Mem = C.Allocate(
711 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
712 OMPClauseMappableExprCommon::MappableComponent>(
713 NumVars, NumUniqueDeclarations,
714 NumUniqueDeclarations + NumComponentLists, NumComponents));
715
716 OMPFromClause *Clause = new (Mem)
717 OMPFromClause(StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
718 NumComponentLists, NumComponents);
719
720 Clause->setVarRefs(Vars);
721 Clause->setClauseInfo(Declarations, ComponentLists);
722 return Clause;
723}
724
725OMPFromClause *OMPFromClause::CreateEmpty(const ASTContext &C, unsigned NumVars,
726 unsigned NumUniqueDeclarations,
727 unsigned NumComponentLists,
728 unsigned NumComponents) {
729 void *Mem = C.Allocate(
730 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
731 OMPClauseMappableExprCommon::MappableComponent>(
732 NumVars, NumUniqueDeclarations,
733 NumUniqueDeclarations + NumComponentLists, NumComponents));
734 return new (Mem) OMPFromClause(NumVars, NumUniqueDeclarations,
735 NumComponentLists, NumComponents);
736}
Carlo Bertolli2404b172016-07-13 15:37:16 +0000737
Samuel Antaocc10b852016-07-28 14:23:26 +0000738void OMPUseDevicePtrClause::setPrivateCopies(ArrayRef<Expr *> VL) {
739 assert(VL.size() == varlist_size() &&
740 "Number of private copies is not the same as the preallocated buffer");
741 std::copy(VL.begin(), VL.end(), varlist_end());
742}
743
744void OMPUseDevicePtrClause::setInits(ArrayRef<Expr *> VL) {
745 assert(VL.size() == varlist_size() &&
746 "Number of inits is not the same as the preallocated buffer");
747 std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
748}
749
750OMPUseDevicePtrClause *OMPUseDevicePtrClause::Create(
751 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
752 SourceLocation EndLoc, ArrayRef<Expr *> Vars, ArrayRef<Expr *> PrivateVars,
753 ArrayRef<Expr *> Inits, ArrayRef<ValueDecl *> Declarations,
754 MappableExprComponentListsRef ComponentLists) {
755 unsigned NumVars = Vars.size();
756 unsigned NumUniqueDeclarations =
757 getUniqueDeclarationsTotalNumber(Declarations);
758 unsigned NumComponentLists = ComponentLists.size();
759 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
760
761 // We need to allocate:
762 // 3 x NumVars x Expr* - we have an original list expression for each clause
763 // list entry and an equal number of private copies and inits.
764 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
765 // with each component list.
766 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
767 // number of lists for each unique declaration and the size of each component
768 // list.
769 // NumComponents x MappableComponent - the total of all the components in all
770 // the lists.
771 void *Mem = C.Allocate(
772 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
773 OMPClauseMappableExprCommon::MappableComponent>(
774 3 * NumVars, NumUniqueDeclarations,
775 NumUniqueDeclarations + NumComponentLists, NumComponents));
776
777 OMPUseDevicePtrClause *Clause = new (Mem) OMPUseDevicePtrClause(
778 StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
779 NumComponentLists, NumComponents);
780
781 Clause->setVarRefs(Vars);
782 Clause->setPrivateCopies(PrivateVars);
783 Clause->setInits(Inits);
784 Clause->setClauseInfo(Declarations, ComponentLists);
Carlo Bertolli2404b172016-07-13 15:37:16 +0000785 return Clause;
786}
787
Samuel Antaocc10b852016-07-28 14:23:26 +0000788OMPUseDevicePtrClause *OMPUseDevicePtrClause::CreateEmpty(
789 const ASTContext &C, unsigned NumVars, unsigned NumUniqueDeclarations,
790 unsigned NumComponentLists, unsigned NumComponents) {
791 void *Mem = C.Allocate(
792 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
793 OMPClauseMappableExprCommon::MappableComponent>(
794 3 * NumVars, NumUniqueDeclarations,
795 NumUniqueDeclarations + NumComponentLists, NumComponents));
796 return new (Mem) OMPUseDevicePtrClause(NumVars, NumUniqueDeclarations,
797 NumComponentLists, NumComponents);
Carlo Bertolli2404b172016-07-13 15:37:16 +0000798}
Carlo Bertolli70594e92016-07-13 17:16:49 +0000799
Samuel Antao6890b092016-07-28 14:25:09 +0000800OMPIsDevicePtrClause *
801OMPIsDevicePtrClause::Create(const ASTContext &C, SourceLocation StartLoc,
802 SourceLocation LParenLoc, SourceLocation EndLoc,
803 ArrayRef<Expr *> Vars,
804 ArrayRef<ValueDecl *> Declarations,
805 MappableExprComponentListsRef ComponentLists) {
806 unsigned NumVars = Vars.size();
807 unsigned NumUniqueDeclarations =
808 getUniqueDeclarationsTotalNumber(Declarations);
809 unsigned NumComponentLists = ComponentLists.size();
810 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
811
812 // We need to allocate:
813 // NumVars x Expr* - we have an original list expression for each clause list
814 // entry.
815 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
816 // with each component list.
817 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
818 // number of lists for each unique declaration and the size of each component
819 // list.
820 // NumComponents x MappableComponent - the total of all the components in all
821 // the lists.
822 void *Mem = C.Allocate(
823 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
824 OMPClauseMappableExprCommon::MappableComponent>(
825 NumVars, NumUniqueDeclarations,
826 NumUniqueDeclarations + NumComponentLists, NumComponents));
827
828 OMPIsDevicePtrClause *Clause = new (Mem) OMPIsDevicePtrClause(
829 StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
830 NumComponentLists, NumComponents);
831
832 Clause->setVarRefs(Vars);
833 Clause->setClauseInfo(Declarations, ComponentLists);
Carlo Bertolli70594e92016-07-13 17:16:49 +0000834 return Clause;
835}
836
Samuel Antao6890b092016-07-28 14:25:09 +0000837OMPIsDevicePtrClause *OMPIsDevicePtrClause::CreateEmpty(
838 const ASTContext &C, unsigned NumVars, unsigned NumUniqueDeclarations,
839 unsigned NumComponentLists, unsigned NumComponents) {
840 void *Mem = C.Allocate(
841 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
842 OMPClauseMappableExprCommon::MappableComponent>(
843 NumVars, NumUniqueDeclarations,
844 NumUniqueDeclarations + NumComponentLists, NumComponents));
845 return new (Mem) OMPIsDevicePtrClause(NumVars, NumUniqueDeclarations,
846 NumComponentLists, NumComponents);
Carlo Bertolli70594e92016-07-13 17:16:49 +0000847}