blob: 55f5ca55d298c5b1944fe8785cc52853c20daff2 [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);
Alexey Bataev3392d762016-02-16 11:18:12 +000055 case OMPC_default:
56 case OMPC_proc_bind:
Alexey Bataev3392d762016-02-16 11:18:12 +000057 case OMPC_final:
Alexey Bataev3392d762016-02-16 11:18:12 +000058 case OMPC_safelen:
59 case OMPC_simdlen:
60 case OMPC_collapse:
61 case OMPC_private:
Alexey Bataev005248a2016-02-25 05:25:57 +000062 case OMPC_shared:
Alexey Bataev005248a2016-02-25 05:25:57 +000063 case OMPC_aligned:
64 case OMPC_copyin:
65 case OMPC_copyprivate:
66 case OMPC_ordered:
67 case OMPC_nowait:
68 case OMPC_untied:
69 case OMPC_mergeable:
70 case OMPC_threadprivate:
71 case OMPC_flush:
72 case OMPC_read:
73 case OMPC_write:
74 case OMPC_update:
75 case OMPC_capture:
76 case OMPC_seq_cst:
77 case OMPC_depend:
78 case OMPC_device:
79 case OMPC_threads:
80 case OMPC_simd:
81 case OMPC_map:
82 case OMPC_num_teams:
83 case OMPC_thread_limit:
84 case OMPC_priority:
85 case OMPC_grainsize:
86 case OMPC_nogroup:
87 case OMPC_num_tasks:
88 case OMPC_hint:
89 case OMPC_defaultmap:
90 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +000091 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +000092 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +000093 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +000094 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +000095 case OMPC_is_device_ptr:
Alexey Bataev005248a2016-02-25 05:25:57 +000096 break;
97 }
98
99 return nullptr;
100}
101
102OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(OMPClause *C) {
103 auto *Res = OMPClauseWithPostUpdate::get(const_cast<const OMPClause *>(C));
104 return Res ? const_cast<OMPClauseWithPostUpdate *>(Res) : nullptr;
105}
106
107const OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(const OMPClause *C) {
108 switch (C->getClauseKind()) {
Alexey Bataev3392d762016-02-16 11:18:12 +0000109 case OMPC_lastprivate:
Alexey Bataev005248a2016-02-25 05:25:57 +0000110 return static_cast<const OMPLastprivateClause *>(C);
Alexey Bataev61205072016-03-02 04:57:40 +0000111 case OMPC_reduction:
112 return static_cast<const OMPReductionClause *>(C);
Alexey Bataev78849fb2016-03-09 09:49:00 +0000113 case OMPC_linear:
114 return static_cast<const OMPLinearClause *>(C);
Alexey Bataev005248a2016-02-25 05:25:57 +0000115 case OMPC_schedule:
116 case OMPC_dist_schedule:
117 case OMPC_firstprivate:
118 case OMPC_default:
119 case OMPC_proc_bind:
120 case OMPC_if:
121 case OMPC_final:
122 case OMPC_num_threads:
123 case OMPC_safelen:
124 case OMPC_simdlen:
125 case OMPC_collapse:
126 case OMPC_private:
Alexey Bataev3392d762016-02-16 11:18:12 +0000127 case OMPC_shared:
Alexey Bataev3392d762016-02-16 11:18:12 +0000128 case OMPC_aligned:
129 case OMPC_copyin:
130 case OMPC_copyprivate:
131 case OMPC_ordered:
132 case OMPC_nowait:
133 case OMPC_untied:
134 case OMPC_mergeable:
135 case OMPC_threadprivate:
136 case OMPC_flush:
137 case OMPC_read:
138 case OMPC_write:
139 case OMPC_update:
140 case OMPC_capture:
141 case OMPC_seq_cst:
142 case OMPC_depend:
143 case OMPC_device:
144 case OMPC_threads:
145 case OMPC_simd:
146 case OMPC_map:
147 case OMPC_num_teams:
148 case OMPC_thread_limit:
149 case OMPC_priority:
150 case OMPC_grainsize:
151 case OMPC_nogroup:
152 case OMPC_num_tasks:
153 case OMPC_hint:
154 case OMPC_defaultmap:
155 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000156 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +0000157 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +0000158 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +0000159 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +0000160 case OMPC_is_device_ptr:
Alexey Bataev3392d762016-02-16 11:18:12 +0000161 break;
162 }
163
164 return nullptr;
165}
166
James Y Knightb8bfd962015-10-02 13:41:04 +0000167void OMPPrivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
168 assert(VL.size() == varlist_size() &&
169 "Number of private copies is not the same as the preallocated buffer");
170 std::copy(VL.begin(), VL.end(), varlist_end());
171}
172
173OMPPrivateClause *
174OMPPrivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
175 SourceLocation LParenLoc, SourceLocation EndLoc,
176 ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL) {
177 // Allocate space for private variables and initializer expressions.
James Y Knight374fd202016-01-01 00:38:24 +0000178 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000179 OMPPrivateClause *Clause =
180 new (Mem) OMPPrivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
181 Clause->setVarRefs(VL);
182 Clause->setPrivateCopies(PrivateVL);
183 return Clause;
184}
185
186OMPPrivateClause *OMPPrivateClause::CreateEmpty(const ASTContext &C,
187 unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000188 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000189 return new (Mem) OMPPrivateClause(N);
190}
191
192void OMPFirstprivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
193 assert(VL.size() == varlist_size() &&
194 "Number of private copies is not the same as the preallocated buffer");
195 std::copy(VL.begin(), VL.end(), varlist_end());
196}
197
198void OMPFirstprivateClause::setInits(ArrayRef<Expr *> VL) {
199 assert(VL.size() == varlist_size() &&
200 "Number of inits is not the same as the preallocated buffer");
201 std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
202}
203
204OMPFirstprivateClause *
205OMPFirstprivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
206 SourceLocation LParenLoc, SourceLocation EndLoc,
207 ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL,
Alexey Bataev417089f2016-02-17 13:19:37 +0000208 ArrayRef<Expr *> InitVL, Stmt *PreInit) {
James Y Knight374fd202016-01-01 00:38:24 +0000209 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000210 OMPFirstprivateClause *Clause =
211 new (Mem) OMPFirstprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
212 Clause->setVarRefs(VL);
213 Clause->setPrivateCopies(PrivateVL);
214 Clause->setInits(InitVL);
Alexey Bataev417089f2016-02-17 13:19:37 +0000215 Clause->setPreInitStmt(PreInit);
James Y Knightb8bfd962015-10-02 13:41:04 +0000216 return Clause;
217}
218
219OMPFirstprivateClause *OMPFirstprivateClause::CreateEmpty(const ASTContext &C,
220 unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000221 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000222 return new (Mem) OMPFirstprivateClause(N);
223}
224
225void OMPLastprivateClause::setPrivateCopies(ArrayRef<Expr *> PrivateCopies) {
226 assert(PrivateCopies.size() == varlist_size() &&
227 "Number of private copies is not the same as the preallocated buffer");
228 std::copy(PrivateCopies.begin(), PrivateCopies.end(), varlist_end());
229}
230
231void OMPLastprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
232 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
233 "not the same as the "
234 "preallocated buffer");
235 std::copy(SrcExprs.begin(), SrcExprs.end(), getPrivateCopies().end());
236}
237
238void OMPLastprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
239 assert(DstExprs.size() == varlist_size() && "Number of destination "
240 "expressions is not the same as "
241 "the preallocated buffer");
242 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
243}
244
245void OMPLastprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
246 assert(AssignmentOps.size() == varlist_size() &&
247 "Number of assignment expressions is not the same as the preallocated "
248 "buffer");
249 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
250 getDestinationExprs().end());
251}
252
253OMPLastprivateClause *OMPLastprivateClause::Create(
254 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
255 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
Alexey Bataev005248a2016-02-25 05:25:57 +0000256 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps, Stmt *PreInit,
257 Expr *PostUpdate) {
James Y Knight374fd202016-01-01 00:38:24 +0000258 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000259 OMPLastprivateClause *Clause =
260 new (Mem) OMPLastprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
261 Clause->setVarRefs(VL);
262 Clause->setSourceExprs(SrcExprs);
263 Clause->setDestinationExprs(DstExprs);
264 Clause->setAssignmentOps(AssignmentOps);
Alexey Bataev005248a2016-02-25 05:25:57 +0000265 Clause->setPreInitStmt(PreInit);
266 Clause->setPostUpdateExpr(PostUpdate);
James Y Knightb8bfd962015-10-02 13:41:04 +0000267 return Clause;
268}
269
270OMPLastprivateClause *OMPLastprivateClause::CreateEmpty(const ASTContext &C,
271 unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000272 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000273 return new (Mem) OMPLastprivateClause(N);
274}
275
276OMPSharedClause *OMPSharedClause::Create(const ASTContext &C,
277 SourceLocation StartLoc,
278 SourceLocation LParenLoc,
279 SourceLocation EndLoc,
280 ArrayRef<Expr *> VL) {
James Y Knight374fd202016-01-01 00:38:24 +0000281 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000282 OMPSharedClause *Clause =
283 new (Mem) OMPSharedClause(StartLoc, LParenLoc, EndLoc, VL.size());
284 Clause->setVarRefs(VL);
285 return Clause;
286}
287
288OMPSharedClause *OMPSharedClause::CreateEmpty(const ASTContext &C, unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000289 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000290 return new (Mem) OMPSharedClause(N);
291}
292
293void OMPLinearClause::setPrivates(ArrayRef<Expr *> PL) {
294 assert(PL.size() == varlist_size() &&
295 "Number of privates is not the same as the preallocated buffer");
296 std::copy(PL.begin(), PL.end(), varlist_end());
297}
298
299void OMPLinearClause::setInits(ArrayRef<Expr *> IL) {
300 assert(IL.size() == varlist_size() &&
301 "Number of inits is not the same as the preallocated buffer");
302 std::copy(IL.begin(), IL.end(), getPrivates().end());
303}
304
305void OMPLinearClause::setUpdates(ArrayRef<Expr *> UL) {
306 assert(UL.size() == varlist_size() &&
307 "Number of updates is not the same as the preallocated buffer");
308 std::copy(UL.begin(), UL.end(), getInits().end());
309}
310
311void OMPLinearClause::setFinals(ArrayRef<Expr *> FL) {
312 assert(FL.size() == varlist_size() &&
313 "Number of final updates is not the same as the preallocated buffer");
314 std::copy(FL.begin(), FL.end(), getUpdates().end());
315}
316
317OMPLinearClause *OMPLinearClause::Create(
318 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
319 OpenMPLinearClauseKind Modifier, SourceLocation ModifierLoc,
320 SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef<Expr *> VL,
Alexey Bataev78849fb2016-03-09 09:49:00 +0000321 ArrayRef<Expr *> PL, ArrayRef<Expr *> IL, Expr *Step, Expr *CalcStep,
322 Stmt *PreInit, Expr *PostUpdate) {
James Y Knightb8bfd962015-10-02 13:41:04 +0000323 // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
324 // (Step and CalcStep).
James Y Knight374fd202016-01-01 00:38:24 +0000325 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size() + 2));
James Y Knightb8bfd962015-10-02 13:41:04 +0000326 OMPLinearClause *Clause = new (Mem) OMPLinearClause(
327 StartLoc, LParenLoc, Modifier, ModifierLoc, ColonLoc, EndLoc, VL.size());
328 Clause->setVarRefs(VL);
329 Clause->setPrivates(PL);
330 Clause->setInits(IL);
331 // Fill update and final expressions with zeroes, they are provided later,
332 // after the directive construction.
333 std::fill(Clause->getInits().end(), Clause->getInits().end() + VL.size(),
334 nullptr);
335 std::fill(Clause->getUpdates().end(), Clause->getUpdates().end() + VL.size(),
336 nullptr);
337 Clause->setStep(Step);
338 Clause->setCalcStep(CalcStep);
Alexey Bataev78849fb2016-03-09 09:49:00 +0000339 Clause->setPreInitStmt(PreInit);
340 Clause->setPostUpdateExpr(PostUpdate);
James Y Knightb8bfd962015-10-02 13:41:04 +0000341 return Clause;
342}
343
344OMPLinearClause *OMPLinearClause::CreateEmpty(const ASTContext &C,
345 unsigned NumVars) {
346 // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
347 // (Step and CalcStep).
James Y Knight374fd202016-01-01 00:38:24 +0000348 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * NumVars + 2));
James Y Knightb8bfd962015-10-02 13:41:04 +0000349 return new (Mem) OMPLinearClause(NumVars);
350}
351
352OMPAlignedClause *
353OMPAlignedClause::Create(const ASTContext &C, SourceLocation StartLoc,
354 SourceLocation LParenLoc, SourceLocation ColonLoc,
355 SourceLocation EndLoc, ArrayRef<Expr *> VL, Expr *A) {
James Y Knight374fd202016-01-01 00:38:24 +0000356 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
James Y Knightb8bfd962015-10-02 13:41:04 +0000357 OMPAlignedClause *Clause = new (Mem)
358 OMPAlignedClause(StartLoc, LParenLoc, ColonLoc, EndLoc, VL.size());
359 Clause->setVarRefs(VL);
360 Clause->setAlignment(A);
361 return Clause;
362}
363
364OMPAlignedClause *OMPAlignedClause::CreateEmpty(const ASTContext &C,
365 unsigned NumVars) {
James Y Knight374fd202016-01-01 00:38:24 +0000366 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumVars + 1));
James Y Knightb8bfd962015-10-02 13:41:04 +0000367 return new (Mem) OMPAlignedClause(NumVars);
368}
369
370void OMPCopyinClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
371 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
372 "not the same as the "
373 "preallocated buffer");
374 std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
375}
376
377void OMPCopyinClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
378 assert(DstExprs.size() == varlist_size() && "Number of destination "
379 "expressions is not the same as "
380 "the preallocated buffer");
381 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
382}
383
384void OMPCopyinClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
385 assert(AssignmentOps.size() == varlist_size() &&
386 "Number of assignment expressions is not the same as the preallocated "
387 "buffer");
388 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
389 getDestinationExprs().end());
390}
391
392OMPCopyinClause *OMPCopyinClause::Create(
393 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
394 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
395 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
James Y Knight374fd202016-01-01 00:38:24 +0000396 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000397 OMPCopyinClause *Clause =
398 new (Mem) OMPCopyinClause(StartLoc, LParenLoc, EndLoc, VL.size());
399 Clause->setVarRefs(VL);
400 Clause->setSourceExprs(SrcExprs);
401 Clause->setDestinationExprs(DstExprs);
402 Clause->setAssignmentOps(AssignmentOps);
403 return Clause;
404}
405
406OMPCopyinClause *OMPCopyinClause::CreateEmpty(const ASTContext &C, unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000407 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000408 return new (Mem) OMPCopyinClause(N);
409}
410
411void OMPCopyprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
412 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
413 "not the same as the "
414 "preallocated buffer");
415 std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
416}
417
418void OMPCopyprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
419 assert(DstExprs.size() == varlist_size() && "Number of destination "
420 "expressions is not the same as "
421 "the preallocated buffer");
422 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
423}
424
425void OMPCopyprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
426 assert(AssignmentOps.size() == varlist_size() &&
427 "Number of assignment expressions is not the same as the preallocated "
428 "buffer");
429 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
430 getDestinationExprs().end());
431}
432
433OMPCopyprivateClause *OMPCopyprivateClause::Create(
434 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
435 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
436 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
James Y Knight374fd202016-01-01 00:38:24 +0000437 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000438 OMPCopyprivateClause *Clause =
439 new (Mem) OMPCopyprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
440 Clause->setVarRefs(VL);
441 Clause->setSourceExprs(SrcExprs);
442 Clause->setDestinationExprs(DstExprs);
443 Clause->setAssignmentOps(AssignmentOps);
444 return Clause;
445}
446
447OMPCopyprivateClause *OMPCopyprivateClause::CreateEmpty(const ASTContext &C,
448 unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000449 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000450 return new (Mem) OMPCopyprivateClause(N);
451}
452
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000453void OMPReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
454 assert(Privates.size() == varlist_size() &&
455 "Number of private copies is not the same as the preallocated buffer");
456 std::copy(Privates.begin(), Privates.end(), varlist_end());
457}
458
James Y Knightb8bfd962015-10-02 13:41:04 +0000459void OMPReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
460 assert(
461 LHSExprs.size() == varlist_size() &&
462 "Number of LHS expressions is not the same as the preallocated buffer");
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000463 std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
James Y Knightb8bfd962015-10-02 13:41:04 +0000464}
465
466void OMPReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
467 assert(
468 RHSExprs.size() == varlist_size() &&
469 "Number of RHS expressions is not the same as the preallocated buffer");
470 std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
471}
472
473void OMPReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
474 assert(ReductionOps.size() == varlist_size() && "Number of reduction "
475 "expressions is not the same "
476 "as the preallocated buffer");
477 std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
478}
479
480OMPReductionClause *OMPReductionClause::Create(
481 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
482 SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
483 NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000484 ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
Alexey Bataev61205072016-03-02 04:57:40 +0000485 ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps, Stmt *PreInit,
486 Expr *PostUpdate) {
James Y Knight374fd202016-01-01 00:38:24 +0000487 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000488 OMPReductionClause *Clause = new (Mem) OMPReductionClause(
489 StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
490 Clause->setVarRefs(VL);
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000491 Clause->setPrivates(Privates);
James Y Knightb8bfd962015-10-02 13:41:04 +0000492 Clause->setLHSExprs(LHSExprs);
493 Clause->setRHSExprs(RHSExprs);
494 Clause->setReductionOps(ReductionOps);
Alexey Bataev61205072016-03-02 04:57:40 +0000495 Clause->setPreInitStmt(PreInit);
496 Clause->setPostUpdateExpr(PostUpdate);
James Y Knightb8bfd962015-10-02 13:41:04 +0000497 return Clause;
498}
499
500OMPReductionClause *OMPReductionClause::CreateEmpty(const ASTContext &C,
501 unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000502 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000503 return new (Mem) OMPReductionClause(N);
504}
505
506OMPFlushClause *OMPFlushClause::Create(const ASTContext &C,
507 SourceLocation StartLoc,
508 SourceLocation LParenLoc,
509 SourceLocation EndLoc,
510 ArrayRef<Expr *> VL) {
Alexey Bataev8b427062016-05-25 12:36:08 +0000511 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
James Y Knightb8bfd962015-10-02 13:41:04 +0000512 OMPFlushClause *Clause =
513 new (Mem) OMPFlushClause(StartLoc, LParenLoc, EndLoc, VL.size());
514 Clause->setVarRefs(VL);
515 return Clause;
516}
517
518OMPFlushClause *OMPFlushClause::CreateEmpty(const ASTContext &C, unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000519 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000520 return new (Mem) OMPFlushClause(N);
521}
522
Alexey Bataev8b427062016-05-25 12:36:08 +0000523OMPDependClause *OMPDependClause::Create(
524 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
525 SourceLocation EndLoc, OpenMPDependClauseKind DepKind,
526 SourceLocation DepLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL) {
527 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
James Y Knightb8bfd962015-10-02 13:41:04 +0000528 OMPDependClause *Clause =
529 new (Mem) OMPDependClause(StartLoc, LParenLoc, EndLoc, VL.size());
530 Clause->setVarRefs(VL);
531 Clause->setDependencyKind(DepKind);
532 Clause->setDependencyLoc(DepLoc);
533 Clause->setColonLoc(ColonLoc);
Alexey Bataev8b427062016-05-25 12:36:08 +0000534 Clause->setCounterValue(nullptr);
James Y Knightb8bfd962015-10-02 13:41:04 +0000535 return Clause;
536}
537
538OMPDependClause *OMPDependClause::CreateEmpty(const ASTContext &C, unsigned N) {
Alexey Bataev2af820542016-05-25 12:51:24 +0000539 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N + 1));
James Y Knightb8bfd962015-10-02 13:41:04 +0000540 return new (Mem) OMPDependClause(N);
541}
Kelvin Li0bff7af2015-11-23 05:32:03 +0000542
Alexey Bataev8b427062016-05-25 12:36:08 +0000543void OMPDependClause::setCounterValue(Expr *V) {
544 assert(getDependencyKind() == OMPC_DEPEND_sink ||
545 getDependencyKind() == OMPC_DEPEND_source || V == nullptr);
546 *getVarRefs().end() = V;
547}
548
549const Expr *OMPDependClause::getCounterValue() const {
550 auto *V = *getVarRefs().end();
551 assert(getDependencyKind() == OMPC_DEPEND_sink ||
552 getDependencyKind() == OMPC_DEPEND_source || V == nullptr);
553 return V;
554}
555
556Expr *OMPDependClause::getCounterValue() {
557 auto *V = *getVarRefs().end();
558 assert(getDependencyKind() == OMPC_DEPEND_sink ||
559 getDependencyKind() == OMPC_DEPEND_source || V == nullptr);
560 return V;
561}
562
Samuel Antao90927002016-04-26 14:54:23 +0000563unsigned OMPClauseMappableExprCommon::getComponentsTotalNumber(
564 MappableExprComponentListsRef ComponentLists) {
565 unsigned TotalNum = 0u;
566 for (auto &C : ComponentLists)
567 TotalNum += C.size();
568 return TotalNum;
569}
570
571unsigned OMPClauseMappableExprCommon::getUniqueDeclarationsTotalNumber(
572 ArrayRef<ValueDecl *> Declarations) {
573 unsigned TotalNum = 0u;
574 llvm::SmallPtrSet<const ValueDecl *, 8> Cache;
575 for (auto *D : Declarations) {
576 const ValueDecl *VD = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr;
577 if (Cache.count(VD))
578 continue;
579 ++TotalNum;
580 Cache.insert(VD);
581 }
582 return TotalNum;
583}
584
585OMPMapClause *
586OMPMapClause::Create(const ASTContext &C, SourceLocation StartLoc,
587 SourceLocation LParenLoc, SourceLocation EndLoc,
588 ArrayRef<Expr *> Vars, ArrayRef<ValueDecl *> Declarations,
589 MappableExprComponentListsRef ComponentLists,
590 OpenMPMapClauseKind TypeModifier, OpenMPMapClauseKind Type,
591 bool TypeIsImplicit, SourceLocation TypeLoc) {
592
593 unsigned NumVars = Vars.size();
594 unsigned NumUniqueDeclarations =
595 getUniqueDeclarationsTotalNumber(Declarations);
596 unsigned NumComponentLists = ComponentLists.size();
597 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
598
599 // We need to allocate:
600 // NumVars x Expr* - we have an original list expression for each clause list
601 // entry.
602 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
603 // with each component list.
604 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
605 // number of lists for each unique declaration and the size of each component
606 // list.
607 // NumComponents x MappableComponent - the total of all the components in all
608 // the lists.
609 void *Mem = C.Allocate(
610 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
611 OMPClauseMappableExprCommon::MappableComponent>(
612 NumVars, NumUniqueDeclarations,
613 NumUniqueDeclarations + NumComponentLists, NumComponents));
614 OMPMapClause *Clause = new (Mem) OMPMapClause(
615 TypeModifier, Type, TypeIsImplicit, TypeLoc, StartLoc, LParenLoc, EndLoc,
616 NumVars, NumUniqueDeclarations, NumComponentLists, NumComponents);
617
618 Clause->setVarRefs(Vars);
619 Clause->setClauseInfo(Declarations, ComponentLists);
Kelvin Li0bff7af2015-11-23 05:32:03 +0000620 Clause->setMapTypeModifier(TypeModifier);
621 Clause->setMapType(Type);
622 Clause->setMapLoc(TypeLoc);
623 return Clause;
624}
625
Samuel Antao90927002016-04-26 14:54:23 +0000626OMPMapClause *OMPMapClause::CreateEmpty(const ASTContext &C, unsigned NumVars,
627 unsigned NumUniqueDeclarations,
628 unsigned NumComponentLists,
629 unsigned NumComponents) {
630 void *Mem = C.Allocate(
631 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
632 OMPClauseMappableExprCommon::MappableComponent>(
633 NumVars, NumUniqueDeclarations,
634 NumUniqueDeclarations + NumComponentLists, NumComponents));
635 return new (Mem) OMPMapClause(NumVars, NumUniqueDeclarations,
636 NumComponentLists, NumComponents);
Kelvin Li0bff7af2015-11-23 05:32:03 +0000637}
Samuel Antao661c0902016-05-26 17:39:58 +0000638
639OMPToClause *OMPToClause::Create(const ASTContext &C, SourceLocation StartLoc,
640 SourceLocation LParenLoc,
641 SourceLocation EndLoc, ArrayRef<Expr *> Vars,
642 ArrayRef<ValueDecl *> Declarations,
643 MappableExprComponentListsRef ComponentLists) {
644 unsigned NumVars = Vars.size();
645 unsigned NumUniqueDeclarations =
646 getUniqueDeclarationsTotalNumber(Declarations);
647 unsigned NumComponentLists = ComponentLists.size();
648 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
649
650 // We need to allocate:
651 // NumVars x Expr* - we have an original list expression for each clause list
652 // entry.
653 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
654 // with each component list.
655 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
656 // number of lists for each unique declaration and the size of each component
657 // list.
658 // NumComponents x MappableComponent - the total of all the components in all
659 // the lists.
660 void *Mem = C.Allocate(
661 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
662 OMPClauseMappableExprCommon::MappableComponent>(
663 NumVars, NumUniqueDeclarations,
664 NumUniqueDeclarations + NumComponentLists, NumComponents));
665
666 OMPToClause *Clause = new (Mem)
667 OMPToClause(StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
668 NumComponentLists, NumComponents);
669
670 Clause->setVarRefs(Vars);
671 Clause->setClauseInfo(Declarations, ComponentLists);
672 return Clause;
673}
674
675OMPToClause *OMPToClause::CreateEmpty(const ASTContext &C, unsigned NumVars,
676 unsigned NumUniqueDeclarations,
677 unsigned NumComponentLists,
678 unsigned NumComponents) {
679 void *Mem = C.Allocate(
680 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
681 OMPClauseMappableExprCommon::MappableComponent>(
682 NumVars, NumUniqueDeclarations,
683 NumUniqueDeclarations + NumComponentLists, NumComponents));
684 return new (Mem) OMPToClause(NumVars, NumUniqueDeclarations,
685 NumComponentLists, NumComponents);
686}
Samuel Antaoec172c62016-05-26 17:49:04 +0000687
688OMPFromClause *
689OMPFromClause::Create(const ASTContext &C, SourceLocation StartLoc,
690 SourceLocation LParenLoc, SourceLocation EndLoc,
691 ArrayRef<Expr *> Vars, ArrayRef<ValueDecl *> Declarations,
692 MappableExprComponentListsRef ComponentLists) {
693 unsigned NumVars = Vars.size();
694 unsigned NumUniqueDeclarations =
695 getUniqueDeclarationsTotalNumber(Declarations);
696 unsigned NumComponentLists = ComponentLists.size();
697 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
698
699 // We need to allocate:
700 // NumVars x Expr* - we have an original list expression for each clause list
701 // entry.
702 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
703 // with each component list.
704 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
705 // number of lists for each unique declaration and the size of each component
706 // list.
707 // NumComponents x MappableComponent - the total of all the components in all
708 // the lists.
709 void *Mem = C.Allocate(
710 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
711 OMPClauseMappableExprCommon::MappableComponent>(
712 NumVars, NumUniqueDeclarations,
713 NumUniqueDeclarations + NumComponentLists, NumComponents));
714
715 OMPFromClause *Clause = new (Mem)
716 OMPFromClause(StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
717 NumComponentLists, NumComponents);
718
719 Clause->setVarRefs(Vars);
720 Clause->setClauseInfo(Declarations, ComponentLists);
721 return Clause;
722}
723
724OMPFromClause *OMPFromClause::CreateEmpty(const ASTContext &C, unsigned NumVars,
725 unsigned NumUniqueDeclarations,
726 unsigned NumComponentLists,
727 unsigned NumComponents) {
728 void *Mem = C.Allocate(
729 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
730 OMPClauseMappableExprCommon::MappableComponent>(
731 NumVars, NumUniqueDeclarations,
732 NumUniqueDeclarations + NumComponentLists, NumComponents));
733 return new (Mem) OMPFromClause(NumVars, NumUniqueDeclarations,
734 NumComponentLists, NumComponents);
735}
Carlo Bertolli2404b172016-07-13 15:37:16 +0000736
Samuel Antaocc10b852016-07-28 14:23:26 +0000737void OMPUseDevicePtrClause::setPrivateCopies(ArrayRef<Expr *> VL) {
738 assert(VL.size() == varlist_size() &&
739 "Number of private copies is not the same as the preallocated buffer");
740 std::copy(VL.begin(), VL.end(), varlist_end());
741}
742
743void OMPUseDevicePtrClause::setInits(ArrayRef<Expr *> VL) {
744 assert(VL.size() == varlist_size() &&
745 "Number of inits is not the same as the preallocated buffer");
746 std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
747}
748
749OMPUseDevicePtrClause *OMPUseDevicePtrClause::Create(
750 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
751 SourceLocation EndLoc, ArrayRef<Expr *> Vars, ArrayRef<Expr *> PrivateVars,
752 ArrayRef<Expr *> Inits, ArrayRef<ValueDecl *> Declarations,
753 MappableExprComponentListsRef ComponentLists) {
754 unsigned NumVars = Vars.size();
755 unsigned NumUniqueDeclarations =
756 getUniqueDeclarationsTotalNumber(Declarations);
757 unsigned NumComponentLists = ComponentLists.size();
758 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
759
760 // We need to allocate:
761 // 3 x NumVars x Expr* - we have an original list expression for each clause
762 // list entry and an equal number of private copies and inits.
763 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
764 // with each component list.
765 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
766 // number of lists for each unique declaration and the size of each component
767 // list.
768 // NumComponents x MappableComponent - the total of all the components in all
769 // the lists.
770 void *Mem = C.Allocate(
771 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
772 OMPClauseMappableExprCommon::MappableComponent>(
773 3 * NumVars, NumUniqueDeclarations,
774 NumUniqueDeclarations + NumComponentLists, NumComponents));
775
776 OMPUseDevicePtrClause *Clause = new (Mem) OMPUseDevicePtrClause(
777 StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
778 NumComponentLists, NumComponents);
779
780 Clause->setVarRefs(Vars);
781 Clause->setPrivateCopies(PrivateVars);
782 Clause->setInits(Inits);
783 Clause->setClauseInfo(Declarations, ComponentLists);
Carlo Bertolli2404b172016-07-13 15:37:16 +0000784 return Clause;
785}
786
Samuel Antaocc10b852016-07-28 14:23:26 +0000787OMPUseDevicePtrClause *OMPUseDevicePtrClause::CreateEmpty(
788 const ASTContext &C, unsigned NumVars, unsigned NumUniqueDeclarations,
789 unsigned NumComponentLists, unsigned NumComponents) {
790 void *Mem = C.Allocate(
791 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
792 OMPClauseMappableExprCommon::MappableComponent>(
793 3 * NumVars, NumUniqueDeclarations,
794 NumUniqueDeclarations + NumComponentLists, NumComponents));
795 return new (Mem) OMPUseDevicePtrClause(NumVars, NumUniqueDeclarations,
796 NumComponentLists, NumComponents);
Carlo Bertolli2404b172016-07-13 15:37:16 +0000797}
Carlo Bertolli70594e92016-07-13 17:16:49 +0000798
Samuel Antao6890b092016-07-28 14:25:09 +0000799OMPIsDevicePtrClause *
800OMPIsDevicePtrClause::Create(const ASTContext &C, SourceLocation StartLoc,
801 SourceLocation LParenLoc, SourceLocation EndLoc,
802 ArrayRef<Expr *> Vars,
803 ArrayRef<ValueDecl *> Declarations,
804 MappableExprComponentListsRef ComponentLists) {
805 unsigned NumVars = Vars.size();
806 unsigned NumUniqueDeclarations =
807 getUniqueDeclarationsTotalNumber(Declarations);
808 unsigned NumComponentLists = ComponentLists.size();
809 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
810
811 // We need to allocate:
812 // NumVars x Expr* - we have an original list expression for each clause list
813 // entry.
814 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
815 // with each component list.
816 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
817 // number of lists for each unique declaration and the size of each component
818 // list.
819 // NumComponents x MappableComponent - the total of all the components in all
820 // the lists.
821 void *Mem = C.Allocate(
822 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
823 OMPClauseMappableExprCommon::MappableComponent>(
824 NumVars, NumUniqueDeclarations,
825 NumUniqueDeclarations + NumComponentLists, NumComponents));
826
827 OMPIsDevicePtrClause *Clause = new (Mem) OMPIsDevicePtrClause(
828 StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
829 NumComponentLists, NumComponents);
830
831 Clause->setVarRefs(Vars);
832 Clause->setClauseInfo(Declarations, ComponentLists);
Carlo Bertolli70594e92016-07-13 17:16:49 +0000833 return Clause;
834}
835
Samuel Antao6890b092016-07-28 14:25:09 +0000836OMPIsDevicePtrClause *OMPIsDevicePtrClause::CreateEmpty(
837 const ASTContext &C, unsigned NumVars, unsigned NumUniqueDeclarations,
838 unsigned NumComponentLists, unsigned NumComponents) {
839 void *Mem = C.Allocate(
840 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
841 OMPClauseMappableExprCommon::MappableComponent>(
842 NumVars, NumUniqueDeclarations,
843 NumUniqueDeclarations + NumComponentLists, NumComponents));
844 return new (Mem) OMPIsDevicePtrClause(NumVars, NumUniqueDeclarations,
845 NumComponentLists, NumComponents);
Carlo Bertolli70594e92016-07-13 17:16:49 +0000846}