blob: 6e9f4ee9f5f51e2ecb5dc0494f643c4156cb2d9b [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);
Alexey Bataev3392d762016-02-16 11:18:12 +000051 case OMPC_default:
52 case OMPC_proc_bind:
53 case OMPC_if:
54 case OMPC_final:
55 case OMPC_num_threads:
56 case OMPC_safelen:
57 case OMPC_simdlen:
58 case OMPC_collapse:
59 case OMPC_private:
Alexey Bataev005248a2016-02-25 05:25:57 +000060 case OMPC_shared:
Alexey Bataev005248a2016-02-25 05:25:57 +000061 case OMPC_aligned:
62 case OMPC_copyin:
63 case OMPC_copyprivate:
64 case OMPC_ordered:
65 case OMPC_nowait:
66 case OMPC_untied:
67 case OMPC_mergeable:
68 case OMPC_threadprivate:
69 case OMPC_flush:
70 case OMPC_read:
71 case OMPC_write:
72 case OMPC_update:
73 case OMPC_capture:
74 case OMPC_seq_cst:
75 case OMPC_depend:
76 case OMPC_device:
77 case OMPC_threads:
78 case OMPC_simd:
79 case OMPC_map:
80 case OMPC_num_teams:
81 case OMPC_thread_limit:
82 case OMPC_priority:
83 case OMPC_grainsize:
84 case OMPC_nogroup:
85 case OMPC_num_tasks:
86 case OMPC_hint:
87 case OMPC_defaultmap:
88 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +000089 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +000090 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +000091 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +000092 case OMPC_use_device_ptr:
Alexey Bataev005248a2016-02-25 05:25:57 +000093 break;
94 }
95
96 return nullptr;
97}
98
99OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(OMPClause *C) {
100 auto *Res = OMPClauseWithPostUpdate::get(const_cast<const OMPClause *>(C));
101 return Res ? const_cast<OMPClauseWithPostUpdate *>(Res) : nullptr;
102}
103
104const OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(const OMPClause *C) {
105 switch (C->getClauseKind()) {
Alexey Bataev3392d762016-02-16 11:18:12 +0000106 case OMPC_lastprivate:
Alexey Bataev005248a2016-02-25 05:25:57 +0000107 return static_cast<const OMPLastprivateClause *>(C);
Alexey Bataev61205072016-03-02 04:57:40 +0000108 case OMPC_reduction:
109 return static_cast<const OMPReductionClause *>(C);
Alexey Bataev78849fb2016-03-09 09:49:00 +0000110 case OMPC_linear:
111 return static_cast<const OMPLinearClause *>(C);
Alexey Bataev005248a2016-02-25 05:25:57 +0000112 case OMPC_schedule:
113 case OMPC_dist_schedule:
114 case OMPC_firstprivate:
115 case OMPC_default:
116 case OMPC_proc_bind:
117 case OMPC_if:
118 case OMPC_final:
119 case OMPC_num_threads:
120 case OMPC_safelen:
121 case OMPC_simdlen:
122 case OMPC_collapse:
123 case OMPC_private:
Alexey Bataev3392d762016-02-16 11:18:12 +0000124 case OMPC_shared:
Alexey Bataev3392d762016-02-16 11:18:12 +0000125 case OMPC_aligned:
126 case OMPC_copyin:
127 case OMPC_copyprivate:
128 case OMPC_ordered:
129 case OMPC_nowait:
130 case OMPC_untied:
131 case OMPC_mergeable:
132 case OMPC_threadprivate:
133 case OMPC_flush:
134 case OMPC_read:
135 case OMPC_write:
136 case OMPC_update:
137 case OMPC_capture:
138 case OMPC_seq_cst:
139 case OMPC_depend:
140 case OMPC_device:
141 case OMPC_threads:
142 case OMPC_simd:
143 case OMPC_map:
144 case OMPC_num_teams:
145 case OMPC_thread_limit:
146 case OMPC_priority:
147 case OMPC_grainsize:
148 case OMPC_nogroup:
149 case OMPC_num_tasks:
150 case OMPC_hint:
151 case OMPC_defaultmap:
152 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000153 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +0000154 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +0000155 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +0000156 case OMPC_use_device_ptr:
Alexey Bataev3392d762016-02-16 11:18:12 +0000157 break;
158 }
159
160 return nullptr;
161}
162
James Y Knightb8bfd962015-10-02 13:41:04 +0000163void OMPPrivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
164 assert(VL.size() == varlist_size() &&
165 "Number of private copies is not the same as the preallocated buffer");
166 std::copy(VL.begin(), VL.end(), varlist_end());
167}
168
169OMPPrivateClause *
170OMPPrivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
171 SourceLocation LParenLoc, SourceLocation EndLoc,
172 ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL) {
173 // Allocate space for private variables and initializer expressions.
James Y Knight374fd202016-01-01 00:38:24 +0000174 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000175 OMPPrivateClause *Clause =
176 new (Mem) OMPPrivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
177 Clause->setVarRefs(VL);
178 Clause->setPrivateCopies(PrivateVL);
179 return Clause;
180}
181
182OMPPrivateClause *OMPPrivateClause::CreateEmpty(const ASTContext &C,
183 unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000184 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000185 return new (Mem) OMPPrivateClause(N);
186}
187
188void OMPFirstprivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
189 assert(VL.size() == varlist_size() &&
190 "Number of private copies is not the same as the preallocated buffer");
191 std::copy(VL.begin(), VL.end(), varlist_end());
192}
193
194void OMPFirstprivateClause::setInits(ArrayRef<Expr *> VL) {
195 assert(VL.size() == varlist_size() &&
196 "Number of inits is not the same as the preallocated buffer");
197 std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
198}
199
200OMPFirstprivateClause *
201OMPFirstprivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
202 SourceLocation LParenLoc, SourceLocation EndLoc,
203 ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL,
Alexey Bataev417089f2016-02-17 13:19:37 +0000204 ArrayRef<Expr *> InitVL, Stmt *PreInit) {
James Y Knight374fd202016-01-01 00:38:24 +0000205 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000206 OMPFirstprivateClause *Clause =
207 new (Mem) OMPFirstprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
208 Clause->setVarRefs(VL);
209 Clause->setPrivateCopies(PrivateVL);
210 Clause->setInits(InitVL);
Alexey Bataev417089f2016-02-17 13:19:37 +0000211 Clause->setPreInitStmt(PreInit);
James Y Knightb8bfd962015-10-02 13:41:04 +0000212 return Clause;
213}
214
215OMPFirstprivateClause *OMPFirstprivateClause::CreateEmpty(const ASTContext &C,
216 unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000217 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000218 return new (Mem) OMPFirstprivateClause(N);
219}
220
221void OMPLastprivateClause::setPrivateCopies(ArrayRef<Expr *> PrivateCopies) {
222 assert(PrivateCopies.size() == varlist_size() &&
223 "Number of private copies is not the same as the preallocated buffer");
224 std::copy(PrivateCopies.begin(), PrivateCopies.end(), varlist_end());
225}
226
227void OMPLastprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
228 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
229 "not the same as the "
230 "preallocated buffer");
231 std::copy(SrcExprs.begin(), SrcExprs.end(), getPrivateCopies().end());
232}
233
234void OMPLastprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
235 assert(DstExprs.size() == varlist_size() && "Number of destination "
236 "expressions is not the same as "
237 "the preallocated buffer");
238 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
239}
240
241void OMPLastprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
242 assert(AssignmentOps.size() == varlist_size() &&
243 "Number of assignment expressions is not the same as the preallocated "
244 "buffer");
245 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
246 getDestinationExprs().end());
247}
248
249OMPLastprivateClause *OMPLastprivateClause::Create(
250 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
251 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
Alexey Bataev005248a2016-02-25 05:25:57 +0000252 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps, Stmt *PreInit,
253 Expr *PostUpdate) {
James Y Knight374fd202016-01-01 00:38:24 +0000254 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000255 OMPLastprivateClause *Clause =
256 new (Mem) OMPLastprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
257 Clause->setVarRefs(VL);
258 Clause->setSourceExprs(SrcExprs);
259 Clause->setDestinationExprs(DstExprs);
260 Clause->setAssignmentOps(AssignmentOps);
Alexey Bataev005248a2016-02-25 05:25:57 +0000261 Clause->setPreInitStmt(PreInit);
262 Clause->setPostUpdateExpr(PostUpdate);
James Y Knightb8bfd962015-10-02 13:41:04 +0000263 return Clause;
264}
265
266OMPLastprivateClause *OMPLastprivateClause::CreateEmpty(const ASTContext &C,
267 unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000268 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000269 return new (Mem) OMPLastprivateClause(N);
270}
271
272OMPSharedClause *OMPSharedClause::Create(const ASTContext &C,
273 SourceLocation StartLoc,
274 SourceLocation LParenLoc,
275 SourceLocation EndLoc,
276 ArrayRef<Expr *> VL) {
James Y Knight374fd202016-01-01 00:38:24 +0000277 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000278 OMPSharedClause *Clause =
279 new (Mem) OMPSharedClause(StartLoc, LParenLoc, EndLoc, VL.size());
280 Clause->setVarRefs(VL);
281 return Clause;
282}
283
284OMPSharedClause *OMPSharedClause::CreateEmpty(const ASTContext &C, unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000285 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000286 return new (Mem) OMPSharedClause(N);
287}
288
289void OMPLinearClause::setPrivates(ArrayRef<Expr *> PL) {
290 assert(PL.size() == varlist_size() &&
291 "Number of privates is not the same as the preallocated buffer");
292 std::copy(PL.begin(), PL.end(), varlist_end());
293}
294
295void OMPLinearClause::setInits(ArrayRef<Expr *> IL) {
296 assert(IL.size() == varlist_size() &&
297 "Number of inits is not the same as the preallocated buffer");
298 std::copy(IL.begin(), IL.end(), getPrivates().end());
299}
300
301void OMPLinearClause::setUpdates(ArrayRef<Expr *> UL) {
302 assert(UL.size() == varlist_size() &&
303 "Number of updates is not the same as the preallocated buffer");
304 std::copy(UL.begin(), UL.end(), getInits().end());
305}
306
307void OMPLinearClause::setFinals(ArrayRef<Expr *> FL) {
308 assert(FL.size() == varlist_size() &&
309 "Number of final updates is not the same as the preallocated buffer");
310 std::copy(FL.begin(), FL.end(), getUpdates().end());
311}
312
313OMPLinearClause *OMPLinearClause::Create(
314 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
315 OpenMPLinearClauseKind Modifier, SourceLocation ModifierLoc,
316 SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef<Expr *> VL,
Alexey Bataev78849fb2016-03-09 09:49:00 +0000317 ArrayRef<Expr *> PL, ArrayRef<Expr *> IL, Expr *Step, Expr *CalcStep,
318 Stmt *PreInit, Expr *PostUpdate) {
James Y Knightb8bfd962015-10-02 13:41:04 +0000319 // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
320 // (Step and CalcStep).
James Y Knight374fd202016-01-01 00:38:24 +0000321 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size() + 2));
James Y Knightb8bfd962015-10-02 13:41:04 +0000322 OMPLinearClause *Clause = new (Mem) OMPLinearClause(
323 StartLoc, LParenLoc, Modifier, ModifierLoc, ColonLoc, EndLoc, VL.size());
324 Clause->setVarRefs(VL);
325 Clause->setPrivates(PL);
326 Clause->setInits(IL);
327 // Fill update and final expressions with zeroes, they are provided later,
328 // after the directive construction.
329 std::fill(Clause->getInits().end(), Clause->getInits().end() + VL.size(),
330 nullptr);
331 std::fill(Clause->getUpdates().end(), Clause->getUpdates().end() + VL.size(),
332 nullptr);
333 Clause->setStep(Step);
334 Clause->setCalcStep(CalcStep);
Alexey Bataev78849fb2016-03-09 09:49:00 +0000335 Clause->setPreInitStmt(PreInit);
336 Clause->setPostUpdateExpr(PostUpdate);
James Y Knightb8bfd962015-10-02 13:41:04 +0000337 return Clause;
338}
339
340OMPLinearClause *OMPLinearClause::CreateEmpty(const ASTContext &C,
341 unsigned NumVars) {
342 // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
343 // (Step and CalcStep).
James Y Knight374fd202016-01-01 00:38:24 +0000344 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * NumVars + 2));
James Y Knightb8bfd962015-10-02 13:41:04 +0000345 return new (Mem) OMPLinearClause(NumVars);
346}
347
348OMPAlignedClause *
349OMPAlignedClause::Create(const ASTContext &C, SourceLocation StartLoc,
350 SourceLocation LParenLoc, SourceLocation ColonLoc,
351 SourceLocation EndLoc, ArrayRef<Expr *> VL, Expr *A) {
James Y Knight374fd202016-01-01 00:38:24 +0000352 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
James Y Knightb8bfd962015-10-02 13:41:04 +0000353 OMPAlignedClause *Clause = new (Mem)
354 OMPAlignedClause(StartLoc, LParenLoc, ColonLoc, EndLoc, VL.size());
355 Clause->setVarRefs(VL);
356 Clause->setAlignment(A);
357 return Clause;
358}
359
360OMPAlignedClause *OMPAlignedClause::CreateEmpty(const ASTContext &C,
361 unsigned NumVars) {
James Y Knight374fd202016-01-01 00:38:24 +0000362 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumVars + 1));
James Y Knightb8bfd962015-10-02 13:41:04 +0000363 return new (Mem) OMPAlignedClause(NumVars);
364}
365
366void OMPCopyinClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
367 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
368 "not the same as the "
369 "preallocated buffer");
370 std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
371}
372
373void OMPCopyinClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
374 assert(DstExprs.size() == varlist_size() && "Number of destination "
375 "expressions is not the same as "
376 "the preallocated buffer");
377 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
378}
379
380void OMPCopyinClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
381 assert(AssignmentOps.size() == varlist_size() &&
382 "Number of assignment expressions is not the same as the preallocated "
383 "buffer");
384 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
385 getDestinationExprs().end());
386}
387
388OMPCopyinClause *OMPCopyinClause::Create(
389 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
390 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
391 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
James Y Knight374fd202016-01-01 00:38:24 +0000392 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000393 OMPCopyinClause *Clause =
394 new (Mem) OMPCopyinClause(StartLoc, LParenLoc, EndLoc, VL.size());
395 Clause->setVarRefs(VL);
396 Clause->setSourceExprs(SrcExprs);
397 Clause->setDestinationExprs(DstExprs);
398 Clause->setAssignmentOps(AssignmentOps);
399 return Clause;
400}
401
402OMPCopyinClause *OMPCopyinClause::CreateEmpty(const ASTContext &C, unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000403 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000404 return new (Mem) OMPCopyinClause(N);
405}
406
407void OMPCopyprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
408 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
409 "not the same as the "
410 "preallocated buffer");
411 std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
412}
413
414void OMPCopyprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
415 assert(DstExprs.size() == varlist_size() && "Number of destination "
416 "expressions is not the same as "
417 "the preallocated buffer");
418 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
419}
420
421void OMPCopyprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
422 assert(AssignmentOps.size() == varlist_size() &&
423 "Number of assignment expressions is not the same as the preallocated "
424 "buffer");
425 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
426 getDestinationExprs().end());
427}
428
429OMPCopyprivateClause *OMPCopyprivateClause::Create(
430 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
431 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
432 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
James Y Knight374fd202016-01-01 00:38:24 +0000433 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000434 OMPCopyprivateClause *Clause =
435 new (Mem) OMPCopyprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
436 Clause->setVarRefs(VL);
437 Clause->setSourceExprs(SrcExprs);
438 Clause->setDestinationExprs(DstExprs);
439 Clause->setAssignmentOps(AssignmentOps);
440 return Clause;
441}
442
443OMPCopyprivateClause *OMPCopyprivateClause::CreateEmpty(const ASTContext &C,
444 unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000445 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000446 return new (Mem) OMPCopyprivateClause(N);
447}
448
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000449void OMPReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
450 assert(Privates.size() == varlist_size() &&
451 "Number of private copies is not the same as the preallocated buffer");
452 std::copy(Privates.begin(), Privates.end(), varlist_end());
453}
454
James Y Knightb8bfd962015-10-02 13:41:04 +0000455void OMPReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
456 assert(
457 LHSExprs.size() == varlist_size() &&
458 "Number of LHS expressions is not the same as the preallocated buffer");
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000459 std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
James Y Knightb8bfd962015-10-02 13:41:04 +0000460}
461
462void OMPReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
463 assert(
464 RHSExprs.size() == varlist_size() &&
465 "Number of RHS expressions is not the same as the preallocated buffer");
466 std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
467}
468
469void OMPReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
470 assert(ReductionOps.size() == varlist_size() && "Number of reduction "
471 "expressions is not the same "
472 "as the preallocated buffer");
473 std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
474}
475
476OMPReductionClause *OMPReductionClause::Create(
477 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
478 SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
479 NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000480 ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
Alexey Bataev61205072016-03-02 04:57:40 +0000481 ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps, Stmt *PreInit,
482 Expr *PostUpdate) {
James Y Knight374fd202016-01-01 00:38:24 +0000483 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000484 OMPReductionClause *Clause = new (Mem) OMPReductionClause(
485 StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
486 Clause->setVarRefs(VL);
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000487 Clause->setPrivates(Privates);
James Y Knightb8bfd962015-10-02 13:41:04 +0000488 Clause->setLHSExprs(LHSExprs);
489 Clause->setRHSExprs(RHSExprs);
490 Clause->setReductionOps(ReductionOps);
Alexey Bataev61205072016-03-02 04:57:40 +0000491 Clause->setPreInitStmt(PreInit);
492 Clause->setPostUpdateExpr(PostUpdate);
James Y Knightb8bfd962015-10-02 13:41:04 +0000493 return Clause;
494}
495
496OMPReductionClause *OMPReductionClause::CreateEmpty(const ASTContext &C,
497 unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000498 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000499 return new (Mem) OMPReductionClause(N);
500}
501
502OMPFlushClause *OMPFlushClause::Create(const ASTContext &C,
503 SourceLocation StartLoc,
504 SourceLocation LParenLoc,
505 SourceLocation EndLoc,
506 ArrayRef<Expr *> VL) {
Alexey Bataev8b427062016-05-25 12:36:08 +0000507 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
James Y Knightb8bfd962015-10-02 13:41:04 +0000508 OMPFlushClause *Clause =
509 new (Mem) OMPFlushClause(StartLoc, LParenLoc, EndLoc, VL.size());
510 Clause->setVarRefs(VL);
511 return Clause;
512}
513
514OMPFlushClause *OMPFlushClause::CreateEmpty(const ASTContext &C, unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000515 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000516 return new (Mem) OMPFlushClause(N);
517}
518
Alexey Bataev8b427062016-05-25 12:36:08 +0000519OMPDependClause *OMPDependClause::Create(
520 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
521 SourceLocation EndLoc, OpenMPDependClauseKind DepKind,
522 SourceLocation DepLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL) {
523 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
James Y Knightb8bfd962015-10-02 13:41:04 +0000524 OMPDependClause *Clause =
525 new (Mem) OMPDependClause(StartLoc, LParenLoc, EndLoc, VL.size());
526 Clause->setVarRefs(VL);
527 Clause->setDependencyKind(DepKind);
528 Clause->setDependencyLoc(DepLoc);
529 Clause->setColonLoc(ColonLoc);
Alexey Bataev8b427062016-05-25 12:36:08 +0000530 Clause->setCounterValue(nullptr);
James Y Knightb8bfd962015-10-02 13:41:04 +0000531 return Clause;
532}
533
534OMPDependClause *OMPDependClause::CreateEmpty(const ASTContext &C, unsigned N) {
Alexey Bataev2af820542016-05-25 12:51:24 +0000535 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N + 1));
James Y Knightb8bfd962015-10-02 13:41:04 +0000536 return new (Mem) OMPDependClause(N);
537}
Kelvin Li0bff7af2015-11-23 05:32:03 +0000538
Alexey Bataev8b427062016-05-25 12:36:08 +0000539void OMPDependClause::setCounterValue(Expr *V) {
540 assert(getDependencyKind() == OMPC_DEPEND_sink ||
541 getDependencyKind() == OMPC_DEPEND_source || V == nullptr);
542 *getVarRefs().end() = V;
543}
544
545const Expr *OMPDependClause::getCounterValue() const {
546 auto *V = *getVarRefs().end();
547 assert(getDependencyKind() == OMPC_DEPEND_sink ||
548 getDependencyKind() == OMPC_DEPEND_source || V == nullptr);
549 return V;
550}
551
552Expr *OMPDependClause::getCounterValue() {
553 auto *V = *getVarRefs().end();
554 assert(getDependencyKind() == OMPC_DEPEND_sink ||
555 getDependencyKind() == OMPC_DEPEND_source || V == nullptr);
556 return V;
557}
558
Samuel Antao90927002016-04-26 14:54:23 +0000559unsigned OMPClauseMappableExprCommon::getComponentsTotalNumber(
560 MappableExprComponentListsRef ComponentLists) {
561 unsigned TotalNum = 0u;
562 for (auto &C : ComponentLists)
563 TotalNum += C.size();
564 return TotalNum;
565}
566
567unsigned OMPClauseMappableExprCommon::getUniqueDeclarationsTotalNumber(
568 ArrayRef<ValueDecl *> Declarations) {
569 unsigned TotalNum = 0u;
570 llvm::SmallPtrSet<const ValueDecl *, 8> Cache;
571 for (auto *D : Declarations) {
572 const ValueDecl *VD = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr;
573 if (Cache.count(VD))
574 continue;
575 ++TotalNum;
576 Cache.insert(VD);
577 }
578 return TotalNum;
579}
580
581OMPMapClause *
582OMPMapClause::Create(const ASTContext &C, SourceLocation StartLoc,
583 SourceLocation LParenLoc, SourceLocation EndLoc,
584 ArrayRef<Expr *> Vars, ArrayRef<ValueDecl *> Declarations,
585 MappableExprComponentListsRef ComponentLists,
586 OpenMPMapClauseKind TypeModifier, OpenMPMapClauseKind Type,
587 bool TypeIsImplicit, SourceLocation TypeLoc) {
588
589 unsigned NumVars = Vars.size();
590 unsigned NumUniqueDeclarations =
591 getUniqueDeclarationsTotalNumber(Declarations);
592 unsigned NumComponentLists = ComponentLists.size();
593 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
594
595 // We need to allocate:
596 // NumVars x Expr* - we have an original list expression for each clause list
597 // entry.
598 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
599 // with each component list.
600 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
601 // number of lists for each unique declaration and the size of each component
602 // list.
603 // NumComponents x MappableComponent - the total of all the components in all
604 // the lists.
605 void *Mem = C.Allocate(
606 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
607 OMPClauseMappableExprCommon::MappableComponent>(
608 NumVars, NumUniqueDeclarations,
609 NumUniqueDeclarations + NumComponentLists, NumComponents));
610 OMPMapClause *Clause = new (Mem) OMPMapClause(
611 TypeModifier, Type, TypeIsImplicit, TypeLoc, StartLoc, LParenLoc, EndLoc,
612 NumVars, NumUniqueDeclarations, NumComponentLists, NumComponents);
613
614 Clause->setVarRefs(Vars);
615 Clause->setClauseInfo(Declarations, ComponentLists);
Kelvin Li0bff7af2015-11-23 05:32:03 +0000616 Clause->setMapTypeModifier(TypeModifier);
617 Clause->setMapType(Type);
618 Clause->setMapLoc(TypeLoc);
619 return Clause;
620}
621
Samuel Antao90927002016-04-26 14:54:23 +0000622OMPMapClause *OMPMapClause::CreateEmpty(const ASTContext &C, unsigned NumVars,
623 unsigned NumUniqueDeclarations,
624 unsigned NumComponentLists,
625 unsigned NumComponents) {
626 void *Mem = C.Allocate(
627 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
628 OMPClauseMappableExprCommon::MappableComponent>(
629 NumVars, NumUniqueDeclarations,
630 NumUniqueDeclarations + NumComponentLists, NumComponents));
631 return new (Mem) OMPMapClause(NumVars, NumUniqueDeclarations,
632 NumComponentLists, NumComponents);
Kelvin Li0bff7af2015-11-23 05:32:03 +0000633}
Samuel Antao661c0902016-05-26 17:39:58 +0000634
635OMPToClause *OMPToClause::Create(const ASTContext &C, SourceLocation StartLoc,
636 SourceLocation LParenLoc,
637 SourceLocation EndLoc, ArrayRef<Expr *> Vars,
638 ArrayRef<ValueDecl *> Declarations,
639 MappableExprComponentListsRef ComponentLists) {
640 unsigned NumVars = Vars.size();
641 unsigned NumUniqueDeclarations =
642 getUniqueDeclarationsTotalNumber(Declarations);
643 unsigned NumComponentLists = ComponentLists.size();
644 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
645
646 // We need to allocate:
647 // NumVars x Expr* - we have an original list expression for each clause list
648 // entry.
649 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
650 // with each component list.
651 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
652 // number of lists for each unique declaration and the size of each component
653 // list.
654 // NumComponents x MappableComponent - the total of all the components in all
655 // the lists.
656 void *Mem = C.Allocate(
657 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
658 OMPClauseMappableExprCommon::MappableComponent>(
659 NumVars, NumUniqueDeclarations,
660 NumUniqueDeclarations + NumComponentLists, NumComponents));
661
662 OMPToClause *Clause = new (Mem)
663 OMPToClause(StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
664 NumComponentLists, NumComponents);
665
666 Clause->setVarRefs(Vars);
667 Clause->setClauseInfo(Declarations, ComponentLists);
668 return Clause;
669}
670
671OMPToClause *OMPToClause::CreateEmpty(const ASTContext &C, unsigned NumVars,
672 unsigned NumUniqueDeclarations,
673 unsigned NumComponentLists,
674 unsigned NumComponents) {
675 void *Mem = C.Allocate(
676 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
677 OMPClauseMappableExprCommon::MappableComponent>(
678 NumVars, NumUniqueDeclarations,
679 NumUniqueDeclarations + NumComponentLists, NumComponents));
680 return new (Mem) OMPToClause(NumVars, NumUniqueDeclarations,
681 NumComponentLists, NumComponents);
682}
Samuel Antaoec172c62016-05-26 17:49:04 +0000683
684OMPFromClause *
685OMPFromClause::Create(const ASTContext &C, SourceLocation StartLoc,
686 SourceLocation LParenLoc, SourceLocation EndLoc,
687 ArrayRef<Expr *> Vars, ArrayRef<ValueDecl *> Declarations,
688 MappableExprComponentListsRef ComponentLists) {
689 unsigned NumVars = Vars.size();
690 unsigned NumUniqueDeclarations =
691 getUniqueDeclarationsTotalNumber(Declarations);
692 unsigned NumComponentLists = ComponentLists.size();
693 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
694
695 // We need to allocate:
696 // NumVars x Expr* - we have an original list expression for each clause list
697 // entry.
698 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
699 // with each component list.
700 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
701 // number of lists for each unique declaration and the size of each component
702 // list.
703 // NumComponents x MappableComponent - the total of all the components in all
704 // the lists.
705 void *Mem = C.Allocate(
706 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
707 OMPClauseMappableExprCommon::MappableComponent>(
708 NumVars, NumUniqueDeclarations,
709 NumUniqueDeclarations + NumComponentLists, NumComponents));
710
711 OMPFromClause *Clause = new (Mem)
712 OMPFromClause(StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
713 NumComponentLists, NumComponents);
714
715 Clause->setVarRefs(Vars);
716 Clause->setClauseInfo(Declarations, ComponentLists);
717 return Clause;
718}
719
720OMPFromClause *OMPFromClause::CreateEmpty(const ASTContext &C, unsigned NumVars,
721 unsigned NumUniqueDeclarations,
722 unsigned NumComponentLists,
723 unsigned NumComponents) {
724 void *Mem = C.Allocate(
725 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
726 OMPClauseMappableExprCommon::MappableComponent>(
727 NumVars, NumUniqueDeclarations,
728 NumUniqueDeclarations + NumComponentLists, NumComponents));
729 return new (Mem) OMPFromClause(NumVars, NumUniqueDeclarations,
730 NumComponentLists, NumComponents);
731}
Carlo Bertolli2404b172016-07-13 15:37:16 +0000732
733OMPUseDevicePtrClause *OMPUseDevicePtrClause::Create(const ASTContext &C,
734 SourceLocation StartLoc,
735 SourceLocation LParenLoc,
736 SourceLocation EndLoc,
737 ArrayRef<Expr *> VL) {
738 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size()));
739 OMPUseDevicePtrClause *Clause =
740 new (Mem) OMPUseDevicePtrClause(StartLoc, LParenLoc, EndLoc, VL.size());
741 Clause->setVarRefs(VL);
742 return Clause;
743}
744
745OMPUseDevicePtrClause *OMPUseDevicePtrClause::CreateEmpty(const ASTContext &C,
746 unsigned N) {
747 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
748 return new (Mem) OMPUseDevicePtrClause(N);
749}