blob: 77470a9b76d07dc54fd4eaddcab43d29ddf31459 [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);
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000057 case OMPC_thread_limit:
58 return static_cast<const OMPThreadLimitClause *>(C);
Alexey Bataev3392d762016-02-16 11:18:12 +000059 case OMPC_default:
60 case OMPC_proc_bind:
Alexey Bataev3392d762016-02-16 11:18:12 +000061 case OMPC_final:
Alexey Bataev3392d762016-02-16 11:18:12 +000062 case OMPC_safelen:
63 case OMPC_simdlen:
64 case OMPC_collapse:
65 case OMPC_private:
Alexey Bataev005248a2016-02-25 05:25:57 +000066 case OMPC_shared:
Alexey Bataev005248a2016-02-25 05:25:57 +000067 case OMPC_aligned:
68 case OMPC_copyin:
69 case OMPC_copyprivate:
70 case OMPC_ordered:
71 case OMPC_nowait:
72 case OMPC_untied:
73 case OMPC_mergeable:
74 case OMPC_threadprivate:
75 case OMPC_flush:
76 case OMPC_read:
77 case OMPC_write:
78 case OMPC_update:
79 case OMPC_capture:
80 case OMPC_seq_cst:
81 case OMPC_depend:
82 case OMPC_device:
83 case OMPC_threads:
84 case OMPC_simd:
85 case OMPC_map:
Alexey Bataev005248a2016-02-25 05:25:57 +000086 case OMPC_priority:
87 case OMPC_grainsize:
88 case OMPC_nogroup:
89 case OMPC_num_tasks:
90 case OMPC_hint:
91 case OMPC_defaultmap:
92 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +000093 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +000094 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +000095 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +000096 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +000097 case OMPC_is_device_ptr:
Alexey Bataev005248a2016-02-25 05:25:57 +000098 break;
99 }
100
101 return nullptr;
102}
103
104OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(OMPClause *C) {
105 auto *Res = OMPClauseWithPostUpdate::get(const_cast<const OMPClause *>(C));
106 return Res ? const_cast<OMPClauseWithPostUpdate *>(Res) : nullptr;
107}
108
109const OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(const OMPClause *C) {
110 switch (C->getClauseKind()) {
Alexey Bataev3392d762016-02-16 11:18:12 +0000111 case OMPC_lastprivate:
Alexey Bataev005248a2016-02-25 05:25:57 +0000112 return static_cast<const OMPLastprivateClause *>(C);
Alexey Bataev61205072016-03-02 04:57:40 +0000113 case OMPC_reduction:
114 return static_cast<const OMPReductionClause *>(C);
Alexey Bataev78849fb2016-03-09 09:49:00 +0000115 case OMPC_linear:
116 return static_cast<const OMPLinearClause *>(C);
Alexey Bataev005248a2016-02-25 05:25:57 +0000117 case OMPC_schedule:
118 case OMPC_dist_schedule:
119 case OMPC_firstprivate:
120 case OMPC_default:
121 case OMPC_proc_bind:
122 case OMPC_if:
123 case OMPC_final:
124 case OMPC_num_threads:
125 case OMPC_safelen:
126 case OMPC_simdlen:
127 case OMPC_collapse:
128 case OMPC_private:
Alexey Bataev3392d762016-02-16 11:18:12 +0000129 case OMPC_shared:
Alexey Bataev3392d762016-02-16 11:18:12 +0000130 case OMPC_aligned:
131 case OMPC_copyin:
132 case OMPC_copyprivate:
133 case OMPC_ordered:
134 case OMPC_nowait:
135 case OMPC_untied:
136 case OMPC_mergeable:
137 case OMPC_threadprivate:
138 case OMPC_flush:
139 case OMPC_read:
140 case OMPC_write:
141 case OMPC_update:
142 case OMPC_capture:
143 case OMPC_seq_cst:
144 case OMPC_depend:
145 case OMPC_device:
146 case OMPC_threads:
147 case OMPC_simd:
148 case OMPC_map:
149 case OMPC_num_teams:
150 case OMPC_thread_limit:
151 case OMPC_priority:
152 case OMPC_grainsize:
153 case OMPC_nogroup:
154 case OMPC_num_tasks:
155 case OMPC_hint:
156 case OMPC_defaultmap:
157 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000158 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +0000159 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +0000160 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +0000161 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +0000162 case OMPC_is_device_ptr:
Alexey Bataev3392d762016-02-16 11:18:12 +0000163 break;
164 }
165
166 return nullptr;
167}
168
James Y Knightb8bfd962015-10-02 13:41:04 +0000169void OMPPrivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
170 assert(VL.size() == varlist_size() &&
171 "Number of private copies is not the same as the preallocated buffer");
172 std::copy(VL.begin(), VL.end(), varlist_end());
173}
174
175OMPPrivateClause *
176OMPPrivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
177 SourceLocation LParenLoc, SourceLocation EndLoc,
178 ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL) {
179 // Allocate space for private variables and initializer expressions.
James Y Knight374fd202016-01-01 00:38:24 +0000180 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000181 OMPPrivateClause *Clause =
182 new (Mem) OMPPrivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
183 Clause->setVarRefs(VL);
184 Clause->setPrivateCopies(PrivateVL);
185 return Clause;
186}
187
188OMPPrivateClause *OMPPrivateClause::CreateEmpty(const ASTContext &C,
189 unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000190 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000191 return new (Mem) OMPPrivateClause(N);
192}
193
194void OMPFirstprivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
195 assert(VL.size() == varlist_size() &&
196 "Number of private copies is not the same as the preallocated buffer");
197 std::copy(VL.begin(), VL.end(), varlist_end());
198}
199
200void OMPFirstprivateClause::setInits(ArrayRef<Expr *> VL) {
201 assert(VL.size() == varlist_size() &&
202 "Number of inits is not the same as the preallocated buffer");
203 std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
204}
205
206OMPFirstprivateClause *
207OMPFirstprivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
208 SourceLocation LParenLoc, SourceLocation EndLoc,
209 ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL,
Alexey Bataev417089f2016-02-17 13:19:37 +0000210 ArrayRef<Expr *> InitVL, Stmt *PreInit) {
James Y Knight374fd202016-01-01 00:38:24 +0000211 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000212 OMPFirstprivateClause *Clause =
213 new (Mem) OMPFirstprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
214 Clause->setVarRefs(VL);
215 Clause->setPrivateCopies(PrivateVL);
216 Clause->setInits(InitVL);
Alexey Bataev417089f2016-02-17 13:19:37 +0000217 Clause->setPreInitStmt(PreInit);
James Y Knightb8bfd962015-10-02 13:41:04 +0000218 return Clause;
219}
220
221OMPFirstprivateClause *OMPFirstprivateClause::CreateEmpty(const ASTContext &C,
222 unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000223 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000224 return new (Mem) OMPFirstprivateClause(N);
225}
226
227void OMPLastprivateClause::setPrivateCopies(ArrayRef<Expr *> PrivateCopies) {
228 assert(PrivateCopies.size() == varlist_size() &&
229 "Number of private copies is not the same as the preallocated buffer");
230 std::copy(PrivateCopies.begin(), PrivateCopies.end(), varlist_end());
231}
232
233void OMPLastprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
234 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
235 "not the same as the "
236 "preallocated buffer");
237 std::copy(SrcExprs.begin(), SrcExprs.end(), getPrivateCopies().end());
238}
239
240void OMPLastprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
241 assert(DstExprs.size() == varlist_size() && "Number of destination "
242 "expressions is not the same as "
243 "the preallocated buffer");
244 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
245}
246
247void OMPLastprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
248 assert(AssignmentOps.size() == varlist_size() &&
249 "Number of assignment expressions is not the same as the preallocated "
250 "buffer");
251 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
252 getDestinationExprs().end());
253}
254
255OMPLastprivateClause *OMPLastprivateClause::Create(
256 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
257 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
Alexey Bataev005248a2016-02-25 05:25:57 +0000258 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps, Stmt *PreInit,
259 Expr *PostUpdate) {
James Y Knight374fd202016-01-01 00:38:24 +0000260 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000261 OMPLastprivateClause *Clause =
262 new (Mem) OMPLastprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
263 Clause->setVarRefs(VL);
264 Clause->setSourceExprs(SrcExprs);
265 Clause->setDestinationExprs(DstExprs);
266 Clause->setAssignmentOps(AssignmentOps);
Alexey Bataev005248a2016-02-25 05:25:57 +0000267 Clause->setPreInitStmt(PreInit);
268 Clause->setPostUpdateExpr(PostUpdate);
James Y Knightb8bfd962015-10-02 13:41:04 +0000269 return Clause;
270}
271
272OMPLastprivateClause *OMPLastprivateClause::CreateEmpty(const ASTContext &C,
273 unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000274 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000275 return new (Mem) OMPLastprivateClause(N);
276}
277
278OMPSharedClause *OMPSharedClause::Create(const ASTContext &C,
279 SourceLocation StartLoc,
280 SourceLocation LParenLoc,
281 SourceLocation EndLoc,
282 ArrayRef<Expr *> VL) {
James Y Knight374fd202016-01-01 00:38:24 +0000283 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000284 OMPSharedClause *Clause =
285 new (Mem) OMPSharedClause(StartLoc, LParenLoc, EndLoc, VL.size());
286 Clause->setVarRefs(VL);
287 return Clause;
288}
289
290OMPSharedClause *OMPSharedClause::CreateEmpty(const ASTContext &C, unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000291 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000292 return new (Mem) OMPSharedClause(N);
293}
294
295void OMPLinearClause::setPrivates(ArrayRef<Expr *> PL) {
296 assert(PL.size() == varlist_size() &&
297 "Number of privates is not the same as the preallocated buffer");
298 std::copy(PL.begin(), PL.end(), varlist_end());
299}
300
301void OMPLinearClause::setInits(ArrayRef<Expr *> IL) {
302 assert(IL.size() == varlist_size() &&
303 "Number of inits is not the same as the preallocated buffer");
304 std::copy(IL.begin(), IL.end(), getPrivates().end());
305}
306
307void OMPLinearClause::setUpdates(ArrayRef<Expr *> UL) {
308 assert(UL.size() == varlist_size() &&
309 "Number of updates is not the same as the preallocated buffer");
310 std::copy(UL.begin(), UL.end(), getInits().end());
311}
312
313void OMPLinearClause::setFinals(ArrayRef<Expr *> FL) {
314 assert(FL.size() == varlist_size() &&
315 "Number of final updates is not the same as the preallocated buffer");
316 std::copy(FL.begin(), FL.end(), getUpdates().end());
317}
318
319OMPLinearClause *OMPLinearClause::Create(
320 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
321 OpenMPLinearClauseKind Modifier, SourceLocation ModifierLoc,
322 SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef<Expr *> VL,
Alexey Bataev78849fb2016-03-09 09:49:00 +0000323 ArrayRef<Expr *> PL, ArrayRef<Expr *> IL, Expr *Step, Expr *CalcStep,
324 Stmt *PreInit, Expr *PostUpdate) {
James Y Knightb8bfd962015-10-02 13:41:04 +0000325 // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
326 // (Step and CalcStep).
James Y Knight374fd202016-01-01 00:38:24 +0000327 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size() + 2));
James Y Knightb8bfd962015-10-02 13:41:04 +0000328 OMPLinearClause *Clause = new (Mem) OMPLinearClause(
329 StartLoc, LParenLoc, Modifier, ModifierLoc, ColonLoc, EndLoc, VL.size());
330 Clause->setVarRefs(VL);
331 Clause->setPrivates(PL);
332 Clause->setInits(IL);
333 // Fill update and final expressions with zeroes, they are provided later,
334 // after the directive construction.
335 std::fill(Clause->getInits().end(), Clause->getInits().end() + VL.size(),
336 nullptr);
337 std::fill(Clause->getUpdates().end(), Clause->getUpdates().end() + VL.size(),
338 nullptr);
339 Clause->setStep(Step);
340 Clause->setCalcStep(CalcStep);
Alexey Bataev78849fb2016-03-09 09:49:00 +0000341 Clause->setPreInitStmt(PreInit);
342 Clause->setPostUpdateExpr(PostUpdate);
James Y Knightb8bfd962015-10-02 13:41:04 +0000343 return Clause;
344}
345
346OMPLinearClause *OMPLinearClause::CreateEmpty(const ASTContext &C,
347 unsigned NumVars) {
348 // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
349 // (Step and CalcStep).
James Y Knight374fd202016-01-01 00:38:24 +0000350 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * NumVars + 2));
James Y Knightb8bfd962015-10-02 13:41:04 +0000351 return new (Mem) OMPLinearClause(NumVars);
352}
353
354OMPAlignedClause *
355OMPAlignedClause::Create(const ASTContext &C, SourceLocation StartLoc,
356 SourceLocation LParenLoc, SourceLocation ColonLoc,
357 SourceLocation EndLoc, ArrayRef<Expr *> VL, Expr *A) {
James Y Knight374fd202016-01-01 00:38:24 +0000358 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
James Y Knightb8bfd962015-10-02 13:41:04 +0000359 OMPAlignedClause *Clause = new (Mem)
360 OMPAlignedClause(StartLoc, LParenLoc, ColonLoc, EndLoc, VL.size());
361 Clause->setVarRefs(VL);
362 Clause->setAlignment(A);
363 return Clause;
364}
365
366OMPAlignedClause *OMPAlignedClause::CreateEmpty(const ASTContext &C,
367 unsigned NumVars) {
James Y Knight374fd202016-01-01 00:38:24 +0000368 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumVars + 1));
James Y Knightb8bfd962015-10-02 13:41:04 +0000369 return new (Mem) OMPAlignedClause(NumVars);
370}
371
372void OMPCopyinClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
373 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
374 "not the same as the "
375 "preallocated buffer");
376 std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
377}
378
379void OMPCopyinClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
380 assert(DstExprs.size() == varlist_size() && "Number of destination "
381 "expressions is not the same as "
382 "the preallocated buffer");
383 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
384}
385
386void OMPCopyinClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
387 assert(AssignmentOps.size() == varlist_size() &&
388 "Number of assignment expressions is not the same as the preallocated "
389 "buffer");
390 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
391 getDestinationExprs().end());
392}
393
394OMPCopyinClause *OMPCopyinClause::Create(
395 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
396 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
397 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
James Y Knight374fd202016-01-01 00:38:24 +0000398 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000399 OMPCopyinClause *Clause =
400 new (Mem) OMPCopyinClause(StartLoc, LParenLoc, EndLoc, VL.size());
401 Clause->setVarRefs(VL);
402 Clause->setSourceExprs(SrcExprs);
403 Clause->setDestinationExprs(DstExprs);
404 Clause->setAssignmentOps(AssignmentOps);
405 return Clause;
406}
407
408OMPCopyinClause *OMPCopyinClause::CreateEmpty(const ASTContext &C, unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000409 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000410 return new (Mem) OMPCopyinClause(N);
411}
412
413void OMPCopyprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
414 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
415 "not the same as the "
416 "preallocated buffer");
417 std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
418}
419
420void OMPCopyprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
421 assert(DstExprs.size() == varlist_size() && "Number of destination "
422 "expressions is not the same as "
423 "the preallocated buffer");
424 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
425}
426
427void OMPCopyprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
428 assert(AssignmentOps.size() == varlist_size() &&
429 "Number of assignment expressions is not the same as the preallocated "
430 "buffer");
431 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
432 getDestinationExprs().end());
433}
434
435OMPCopyprivateClause *OMPCopyprivateClause::Create(
436 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
437 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
438 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
James Y Knight374fd202016-01-01 00:38:24 +0000439 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000440 OMPCopyprivateClause *Clause =
441 new (Mem) OMPCopyprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
442 Clause->setVarRefs(VL);
443 Clause->setSourceExprs(SrcExprs);
444 Clause->setDestinationExprs(DstExprs);
445 Clause->setAssignmentOps(AssignmentOps);
446 return Clause;
447}
448
449OMPCopyprivateClause *OMPCopyprivateClause::CreateEmpty(const ASTContext &C,
450 unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000451 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000452 return new (Mem) OMPCopyprivateClause(N);
453}
454
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000455void OMPReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
456 assert(Privates.size() == varlist_size() &&
457 "Number of private copies is not the same as the preallocated buffer");
458 std::copy(Privates.begin(), Privates.end(), varlist_end());
459}
460
James Y Knightb8bfd962015-10-02 13:41:04 +0000461void OMPReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
462 assert(
463 LHSExprs.size() == varlist_size() &&
464 "Number of LHS expressions is not the same as the preallocated buffer");
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000465 std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
James Y Knightb8bfd962015-10-02 13:41:04 +0000466}
467
468void OMPReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
469 assert(
470 RHSExprs.size() == varlist_size() &&
471 "Number of RHS expressions is not the same as the preallocated buffer");
472 std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
473}
474
475void OMPReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
476 assert(ReductionOps.size() == varlist_size() && "Number of reduction "
477 "expressions is not the same "
478 "as the preallocated buffer");
479 std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
480}
481
482OMPReductionClause *OMPReductionClause::Create(
483 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
484 SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
485 NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000486 ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
Alexey Bataev61205072016-03-02 04:57:40 +0000487 ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps, Stmt *PreInit,
488 Expr *PostUpdate) {
James Y Knight374fd202016-01-01 00:38:24 +0000489 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000490 OMPReductionClause *Clause = new (Mem) OMPReductionClause(
491 StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
492 Clause->setVarRefs(VL);
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000493 Clause->setPrivates(Privates);
James Y Knightb8bfd962015-10-02 13:41:04 +0000494 Clause->setLHSExprs(LHSExprs);
495 Clause->setRHSExprs(RHSExprs);
496 Clause->setReductionOps(ReductionOps);
Alexey Bataev61205072016-03-02 04:57:40 +0000497 Clause->setPreInitStmt(PreInit);
498 Clause->setPostUpdateExpr(PostUpdate);
James Y Knightb8bfd962015-10-02 13:41:04 +0000499 return Clause;
500}
501
502OMPReductionClause *OMPReductionClause::CreateEmpty(const ASTContext &C,
503 unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000504 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000505 return new (Mem) OMPReductionClause(N);
506}
507
508OMPFlushClause *OMPFlushClause::Create(const ASTContext &C,
509 SourceLocation StartLoc,
510 SourceLocation LParenLoc,
511 SourceLocation EndLoc,
512 ArrayRef<Expr *> VL) {
Alexey Bataev8b427062016-05-25 12:36:08 +0000513 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
James Y Knightb8bfd962015-10-02 13:41:04 +0000514 OMPFlushClause *Clause =
515 new (Mem) OMPFlushClause(StartLoc, LParenLoc, EndLoc, VL.size());
516 Clause->setVarRefs(VL);
517 return Clause;
518}
519
520OMPFlushClause *OMPFlushClause::CreateEmpty(const ASTContext &C, unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000521 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000522 return new (Mem) OMPFlushClause(N);
523}
524
Alexey Bataev8b427062016-05-25 12:36:08 +0000525OMPDependClause *OMPDependClause::Create(
526 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
527 SourceLocation EndLoc, OpenMPDependClauseKind DepKind,
528 SourceLocation DepLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL) {
529 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
James Y Knightb8bfd962015-10-02 13:41:04 +0000530 OMPDependClause *Clause =
531 new (Mem) OMPDependClause(StartLoc, LParenLoc, EndLoc, VL.size());
532 Clause->setVarRefs(VL);
533 Clause->setDependencyKind(DepKind);
534 Clause->setDependencyLoc(DepLoc);
535 Clause->setColonLoc(ColonLoc);
Alexey Bataev8b427062016-05-25 12:36:08 +0000536 Clause->setCounterValue(nullptr);
James Y Knightb8bfd962015-10-02 13:41:04 +0000537 return Clause;
538}
539
540OMPDependClause *OMPDependClause::CreateEmpty(const ASTContext &C, unsigned N) {
Alexey Bataev2af820542016-05-25 12:51:24 +0000541 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N + 1));
James Y Knightb8bfd962015-10-02 13:41:04 +0000542 return new (Mem) OMPDependClause(N);
543}
Kelvin Li0bff7af2015-11-23 05:32:03 +0000544
Alexey Bataev8b427062016-05-25 12:36:08 +0000545void OMPDependClause::setCounterValue(Expr *V) {
546 assert(getDependencyKind() == OMPC_DEPEND_sink ||
547 getDependencyKind() == OMPC_DEPEND_source || V == nullptr);
548 *getVarRefs().end() = V;
549}
550
551const Expr *OMPDependClause::getCounterValue() const {
552 auto *V = *getVarRefs().end();
553 assert(getDependencyKind() == OMPC_DEPEND_sink ||
554 getDependencyKind() == OMPC_DEPEND_source || V == nullptr);
555 return V;
556}
557
558Expr *OMPDependClause::getCounterValue() {
559 auto *V = *getVarRefs().end();
560 assert(getDependencyKind() == OMPC_DEPEND_sink ||
561 getDependencyKind() == OMPC_DEPEND_source || V == nullptr);
562 return V;
563}
564
Samuel Antao90927002016-04-26 14:54:23 +0000565unsigned OMPClauseMappableExprCommon::getComponentsTotalNumber(
566 MappableExprComponentListsRef ComponentLists) {
567 unsigned TotalNum = 0u;
568 for (auto &C : ComponentLists)
569 TotalNum += C.size();
570 return TotalNum;
571}
572
573unsigned OMPClauseMappableExprCommon::getUniqueDeclarationsTotalNumber(
574 ArrayRef<ValueDecl *> Declarations) {
575 unsigned TotalNum = 0u;
576 llvm::SmallPtrSet<const ValueDecl *, 8> Cache;
577 for (auto *D : Declarations) {
578 const ValueDecl *VD = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr;
579 if (Cache.count(VD))
580 continue;
581 ++TotalNum;
582 Cache.insert(VD);
583 }
584 return TotalNum;
585}
586
587OMPMapClause *
588OMPMapClause::Create(const ASTContext &C, SourceLocation StartLoc,
589 SourceLocation LParenLoc, SourceLocation EndLoc,
590 ArrayRef<Expr *> Vars, ArrayRef<ValueDecl *> Declarations,
591 MappableExprComponentListsRef ComponentLists,
592 OpenMPMapClauseKind TypeModifier, OpenMPMapClauseKind Type,
593 bool TypeIsImplicit, SourceLocation TypeLoc) {
594
595 unsigned NumVars = Vars.size();
596 unsigned NumUniqueDeclarations =
597 getUniqueDeclarationsTotalNumber(Declarations);
598 unsigned NumComponentLists = ComponentLists.size();
599 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
600
601 // We need to allocate:
602 // NumVars x Expr* - we have an original list expression for each clause list
603 // entry.
604 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
605 // with each component list.
606 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
607 // number of lists for each unique declaration and the size of each component
608 // list.
609 // NumComponents x MappableComponent - the total of all the components in all
610 // the lists.
611 void *Mem = C.Allocate(
612 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
613 OMPClauseMappableExprCommon::MappableComponent>(
614 NumVars, NumUniqueDeclarations,
615 NumUniqueDeclarations + NumComponentLists, NumComponents));
616 OMPMapClause *Clause = new (Mem) OMPMapClause(
617 TypeModifier, Type, TypeIsImplicit, TypeLoc, StartLoc, LParenLoc, EndLoc,
618 NumVars, NumUniqueDeclarations, NumComponentLists, NumComponents);
619
620 Clause->setVarRefs(Vars);
621 Clause->setClauseInfo(Declarations, ComponentLists);
Kelvin Li0bff7af2015-11-23 05:32:03 +0000622 Clause->setMapTypeModifier(TypeModifier);
623 Clause->setMapType(Type);
624 Clause->setMapLoc(TypeLoc);
625 return Clause;
626}
627
Samuel Antao90927002016-04-26 14:54:23 +0000628OMPMapClause *OMPMapClause::CreateEmpty(const ASTContext &C, unsigned NumVars,
629 unsigned NumUniqueDeclarations,
630 unsigned NumComponentLists,
631 unsigned NumComponents) {
632 void *Mem = C.Allocate(
633 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
634 OMPClauseMappableExprCommon::MappableComponent>(
635 NumVars, NumUniqueDeclarations,
636 NumUniqueDeclarations + NumComponentLists, NumComponents));
637 return new (Mem) OMPMapClause(NumVars, NumUniqueDeclarations,
638 NumComponentLists, NumComponents);
Kelvin Li0bff7af2015-11-23 05:32:03 +0000639}
Samuel Antao661c0902016-05-26 17:39:58 +0000640
641OMPToClause *OMPToClause::Create(const ASTContext &C, SourceLocation StartLoc,
642 SourceLocation LParenLoc,
643 SourceLocation EndLoc, ArrayRef<Expr *> Vars,
644 ArrayRef<ValueDecl *> Declarations,
645 MappableExprComponentListsRef ComponentLists) {
646 unsigned NumVars = Vars.size();
647 unsigned NumUniqueDeclarations =
648 getUniqueDeclarationsTotalNumber(Declarations);
649 unsigned NumComponentLists = ComponentLists.size();
650 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
651
652 // We need to allocate:
653 // NumVars x Expr* - we have an original list expression for each clause list
654 // entry.
655 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
656 // with each component list.
657 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
658 // number of lists for each unique declaration and the size of each component
659 // list.
660 // NumComponents x MappableComponent - the total of all the components in all
661 // the lists.
662 void *Mem = C.Allocate(
663 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
664 OMPClauseMappableExprCommon::MappableComponent>(
665 NumVars, NumUniqueDeclarations,
666 NumUniqueDeclarations + NumComponentLists, NumComponents));
667
668 OMPToClause *Clause = new (Mem)
669 OMPToClause(StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
670 NumComponentLists, NumComponents);
671
672 Clause->setVarRefs(Vars);
673 Clause->setClauseInfo(Declarations, ComponentLists);
674 return Clause;
675}
676
677OMPToClause *OMPToClause::CreateEmpty(const ASTContext &C, unsigned NumVars,
678 unsigned NumUniqueDeclarations,
679 unsigned NumComponentLists,
680 unsigned NumComponents) {
681 void *Mem = C.Allocate(
682 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
683 OMPClauseMappableExprCommon::MappableComponent>(
684 NumVars, NumUniqueDeclarations,
685 NumUniqueDeclarations + NumComponentLists, NumComponents));
686 return new (Mem) OMPToClause(NumVars, NumUniqueDeclarations,
687 NumComponentLists, NumComponents);
688}
Samuel Antaoec172c62016-05-26 17:49:04 +0000689
690OMPFromClause *
691OMPFromClause::Create(const ASTContext &C, SourceLocation StartLoc,
692 SourceLocation LParenLoc, SourceLocation EndLoc,
693 ArrayRef<Expr *> Vars, ArrayRef<ValueDecl *> Declarations,
694 MappableExprComponentListsRef ComponentLists) {
695 unsigned NumVars = Vars.size();
696 unsigned NumUniqueDeclarations =
697 getUniqueDeclarationsTotalNumber(Declarations);
698 unsigned NumComponentLists = ComponentLists.size();
699 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
700
701 // We need to allocate:
702 // NumVars x Expr* - we have an original list expression for each clause list
703 // entry.
704 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
705 // with each component list.
706 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
707 // number of lists for each unique declaration and the size of each component
708 // list.
709 // NumComponents x MappableComponent - the total of all the components in all
710 // the lists.
711 void *Mem = C.Allocate(
712 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
713 OMPClauseMappableExprCommon::MappableComponent>(
714 NumVars, NumUniqueDeclarations,
715 NumUniqueDeclarations + NumComponentLists, NumComponents));
716
717 OMPFromClause *Clause = new (Mem)
718 OMPFromClause(StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
719 NumComponentLists, NumComponents);
720
721 Clause->setVarRefs(Vars);
722 Clause->setClauseInfo(Declarations, ComponentLists);
723 return Clause;
724}
725
726OMPFromClause *OMPFromClause::CreateEmpty(const ASTContext &C, unsigned NumVars,
727 unsigned NumUniqueDeclarations,
728 unsigned NumComponentLists,
729 unsigned NumComponents) {
730 void *Mem = C.Allocate(
731 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
732 OMPClauseMappableExprCommon::MappableComponent>(
733 NumVars, NumUniqueDeclarations,
734 NumUniqueDeclarations + NumComponentLists, NumComponents));
735 return new (Mem) OMPFromClause(NumVars, NumUniqueDeclarations,
736 NumComponentLists, NumComponents);
737}
Carlo Bertolli2404b172016-07-13 15:37:16 +0000738
Samuel Antaocc10b852016-07-28 14:23:26 +0000739void OMPUseDevicePtrClause::setPrivateCopies(ArrayRef<Expr *> VL) {
740 assert(VL.size() == varlist_size() &&
741 "Number of private copies is not the same as the preallocated buffer");
742 std::copy(VL.begin(), VL.end(), varlist_end());
743}
744
745void OMPUseDevicePtrClause::setInits(ArrayRef<Expr *> VL) {
746 assert(VL.size() == varlist_size() &&
747 "Number of inits is not the same as the preallocated buffer");
748 std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
749}
750
751OMPUseDevicePtrClause *OMPUseDevicePtrClause::Create(
752 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
753 SourceLocation EndLoc, ArrayRef<Expr *> Vars, ArrayRef<Expr *> PrivateVars,
754 ArrayRef<Expr *> Inits, ArrayRef<ValueDecl *> Declarations,
755 MappableExprComponentListsRef ComponentLists) {
756 unsigned NumVars = Vars.size();
757 unsigned NumUniqueDeclarations =
758 getUniqueDeclarationsTotalNumber(Declarations);
759 unsigned NumComponentLists = ComponentLists.size();
760 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
761
762 // We need to allocate:
763 // 3 x NumVars x Expr* - we have an original list expression for each clause
764 // list entry and an equal number of private copies and inits.
765 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
766 // with each component list.
767 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
768 // number of lists for each unique declaration and the size of each component
769 // list.
770 // NumComponents x MappableComponent - the total of all the components in all
771 // the lists.
772 void *Mem = C.Allocate(
773 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
774 OMPClauseMappableExprCommon::MappableComponent>(
775 3 * NumVars, NumUniqueDeclarations,
776 NumUniqueDeclarations + NumComponentLists, NumComponents));
777
778 OMPUseDevicePtrClause *Clause = new (Mem) OMPUseDevicePtrClause(
779 StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
780 NumComponentLists, NumComponents);
781
782 Clause->setVarRefs(Vars);
783 Clause->setPrivateCopies(PrivateVars);
784 Clause->setInits(Inits);
785 Clause->setClauseInfo(Declarations, ComponentLists);
Carlo Bertolli2404b172016-07-13 15:37:16 +0000786 return Clause;
787}
788
Samuel Antaocc10b852016-07-28 14:23:26 +0000789OMPUseDevicePtrClause *OMPUseDevicePtrClause::CreateEmpty(
790 const ASTContext &C, unsigned NumVars, unsigned NumUniqueDeclarations,
791 unsigned NumComponentLists, unsigned NumComponents) {
792 void *Mem = C.Allocate(
793 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
794 OMPClauseMappableExprCommon::MappableComponent>(
795 3 * NumVars, NumUniqueDeclarations,
796 NumUniqueDeclarations + NumComponentLists, NumComponents));
797 return new (Mem) OMPUseDevicePtrClause(NumVars, NumUniqueDeclarations,
798 NumComponentLists, NumComponents);
Carlo Bertolli2404b172016-07-13 15:37:16 +0000799}
Carlo Bertolli70594e92016-07-13 17:16:49 +0000800
Samuel Antao6890b092016-07-28 14:25:09 +0000801OMPIsDevicePtrClause *
802OMPIsDevicePtrClause::Create(const ASTContext &C, SourceLocation StartLoc,
803 SourceLocation LParenLoc, SourceLocation EndLoc,
804 ArrayRef<Expr *> Vars,
805 ArrayRef<ValueDecl *> Declarations,
806 MappableExprComponentListsRef ComponentLists) {
807 unsigned NumVars = Vars.size();
808 unsigned NumUniqueDeclarations =
809 getUniqueDeclarationsTotalNumber(Declarations);
810 unsigned NumComponentLists = ComponentLists.size();
811 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
812
813 // We need to allocate:
814 // NumVars x Expr* - we have an original list expression for each clause list
815 // entry.
816 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
817 // with each component list.
818 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
819 // number of lists for each unique declaration and the size of each component
820 // list.
821 // NumComponents x MappableComponent - the total of all the components in all
822 // the lists.
823 void *Mem = C.Allocate(
824 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
825 OMPClauseMappableExprCommon::MappableComponent>(
826 NumVars, NumUniqueDeclarations,
827 NumUniqueDeclarations + NumComponentLists, NumComponents));
828
829 OMPIsDevicePtrClause *Clause = new (Mem) OMPIsDevicePtrClause(
830 StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
831 NumComponentLists, NumComponents);
832
833 Clause->setVarRefs(Vars);
834 Clause->setClauseInfo(Declarations, ComponentLists);
Carlo Bertolli70594e92016-07-13 17:16:49 +0000835 return Clause;
836}
837
Samuel Antao6890b092016-07-28 14:25:09 +0000838OMPIsDevicePtrClause *OMPIsDevicePtrClause::CreateEmpty(
839 const ASTContext &C, unsigned NumVars, unsigned NumUniqueDeclarations,
840 unsigned NumComponentLists, unsigned NumComponents) {
841 void *Mem = C.Allocate(
842 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
843 OMPClauseMappableExprCommon::MappableComponent>(
844 NumVars, NumUniqueDeclarations,
845 NumUniqueDeclarations + NumComponentLists, NumComponents));
846 return new (Mem) OMPIsDevicePtrClause(NumVars, NumUniqueDeclarations,
847 NumComponentLists, NumComponents);
Carlo Bertolli70594e92016-07-13 17:16:49 +0000848}