blob: 23c05d5c28179d4521e0f20c60e6a16a97800b68 [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:
Alexey Bataev005248a2016-02-25 05:25:57 +000090 break;
91 }
92
93 return nullptr;
94}
95
96OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(OMPClause *C) {
97 auto *Res = OMPClauseWithPostUpdate::get(const_cast<const OMPClause *>(C));
98 return Res ? const_cast<OMPClauseWithPostUpdate *>(Res) : nullptr;
99}
100
101const OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(const OMPClause *C) {
102 switch (C->getClauseKind()) {
Alexey Bataev3392d762016-02-16 11:18:12 +0000103 case OMPC_lastprivate:
Alexey Bataev005248a2016-02-25 05:25:57 +0000104 return static_cast<const OMPLastprivateClause *>(C);
Alexey Bataev61205072016-03-02 04:57:40 +0000105 case OMPC_reduction:
106 return static_cast<const OMPReductionClause *>(C);
Alexey Bataev78849fb2016-03-09 09:49:00 +0000107 case OMPC_linear:
108 return static_cast<const OMPLinearClause *>(C);
Alexey Bataev005248a2016-02-25 05:25:57 +0000109 case OMPC_schedule:
110 case OMPC_dist_schedule:
111 case OMPC_firstprivate:
112 case OMPC_default:
113 case OMPC_proc_bind:
114 case OMPC_if:
115 case OMPC_final:
116 case OMPC_num_threads:
117 case OMPC_safelen:
118 case OMPC_simdlen:
119 case OMPC_collapse:
120 case OMPC_private:
Alexey Bataev3392d762016-02-16 11:18:12 +0000121 case OMPC_shared:
Alexey Bataev3392d762016-02-16 11:18:12 +0000122 case OMPC_aligned:
123 case OMPC_copyin:
124 case OMPC_copyprivate:
125 case OMPC_ordered:
126 case OMPC_nowait:
127 case OMPC_untied:
128 case OMPC_mergeable:
129 case OMPC_threadprivate:
130 case OMPC_flush:
131 case OMPC_read:
132 case OMPC_write:
133 case OMPC_update:
134 case OMPC_capture:
135 case OMPC_seq_cst:
136 case OMPC_depend:
137 case OMPC_device:
138 case OMPC_threads:
139 case OMPC_simd:
140 case OMPC_map:
141 case OMPC_num_teams:
142 case OMPC_thread_limit:
143 case OMPC_priority:
144 case OMPC_grainsize:
145 case OMPC_nogroup:
146 case OMPC_num_tasks:
147 case OMPC_hint:
148 case OMPC_defaultmap:
149 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000150 case OMPC_uniform:
Alexey Bataev3392d762016-02-16 11:18:12 +0000151 break;
152 }
153
154 return nullptr;
155}
156
James Y Knightb8bfd962015-10-02 13:41:04 +0000157void OMPPrivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
158 assert(VL.size() == varlist_size() &&
159 "Number of private copies is not the same as the preallocated buffer");
160 std::copy(VL.begin(), VL.end(), varlist_end());
161}
162
163OMPPrivateClause *
164OMPPrivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
165 SourceLocation LParenLoc, SourceLocation EndLoc,
166 ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL) {
167 // Allocate space for private variables and initializer expressions.
James Y Knight374fd202016-01-01 00:38:24 +0000168 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000169 OMPPrivateClause *Clause =
170 new (Mem) OMPPrivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
171 Clause->setVarRefs(VL);
172 Clause->setPrivateCopies(PrivateVL);
173 return Clause;
174}
175
176OMPPrivateClause *OMPPrivateClause::CreateEmpty(const ASTContext &C,
177 unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000178 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000179 return new (Mem) OMPPrivateClause(N);
180}
181
182void OMPFirstprivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
183 assert(VL.size() == varlist_size() &&
184 "Number of private copies is not the same as the preallocated buffer");
185 std::copy(VL.begin(), VL.end(), varlist_end());
186}
187
188void OMPFirstprivateClause::setInits(ArrayRef<Expr *> VL) {
189 assert(VL.size() == varlist_size() &&
190 "Number of inits is not the same as the preallocated buffer");
191 std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
192}
193
194OMPFirstprivateClause *
195OMPFirstprivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
196 SourceLocation LParenLoc, SourceLocation EndLoc,
197 ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL,
Alexey Bataev417089f2016-02-17 13:19:37 +0000198 ArrayRef<Expr *> InitVL, Stmt *PreInit) {
James Y Knight374fd202016-01-01 00:38:24 +0000199 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000200 OMPFirstprivateClause *Clause =
201 new (Mem) OMPFirstprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
202 Clause->setVarRefs(VL);
203 Clause->setPrivateCopies(PrivateVL);
204 Clause->setInits(InitVL);
Alexey Bataev417089f2016-02-17 13:19:37 +0000205 Clause->setPreInitStmt(PreInit);
James Y Knightb8bfd962015-10-02 13:41:04 +0000206 return Clause;
207}
208
209OMPFirstprivateClause *OMPFirstprivateClause::CreateEmpty(const ASTContext &C,
210 unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000211 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000212 return new (Mem) OMPFirstprivateClause(N);
213}
214
215void OMPLastprivateClause::setPrivateCopies(ArrayRef<Expr *> PrivateCopies) {
216 assert(PrivateCopies.size() == varlist_size() &&
217 "Number of private copies is not the same as the preallocated buffer");
218 std::copy(PrivateCopies.begin(), PrivateCopies.end(), varlist_end());
219}
220
221void OMPLastprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
222 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
223 "not the same as the "
224 "preallocated buffer");
225 std::copy(SrcExprs.begin(), SrcExprs.end(), getPrivateCopies().end());
226}
227
228void OMPLastprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
229 assert(DstExprs.size() == varlist_size() && "Number of destination "
230 "expressions is not the same as "
231 "the preallocated buffer");
232 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
233}
234
235void OMPLastprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
236 assert(AssignmentOps.size() == varlist_size() &&
237 "Number of assignment expressions is not the same as the preallocated "
238 "buffer");
239 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
240 getDestinationExprs().end());
241}
242
243OMPLastprivateClause *OMPLastprivateClause::Create(
244 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
245 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
Alexey Bataev005248a2016-02-25 05:25:57 +0000246 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps, Stmt *PreInit,
247 Expr *PostUpdate) {
James Y Knight374fd202016-01-01 00:38:24 +0000248 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000249 OMPLastprivateClause *Clause =
250 new (Mem) OMPLastprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
251 Clause->setVarRefs(VL);
252 Clause->setSourceExprs(SrcExprs);
253 Clause->setDestinationExprs(DstExprs);
254 Clause->setAssignmentOps(AssignmentOps);
Alexey Bataev005248a2016-02-25 05:25:57 +0000255 Clause->setPreInitStmt(PreInit);
256 Clause->setPostUpdateExpr(PostUpdate);
James Y Knightb8bfd962015-10-02 13:41:04 +0000257 return Clause;
258}
259
260OMPLastprivateClause *OMPLastprivateClause::CreateEmpty(const ASTContext &C,
261 unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000262 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000263 return new (Mem) OMPLastprivateClause(N);
264}
265
266OMPSharedClause *OMPSharedClause::Create(const ASTContext &C,
267 SourceLocation StartLoc,
268 SourceLocation LParenLoc,
269 SourceLocation EndLoc,
270 ArrayRef<Expr *> VL) {
James Y Knight374fd202016-01-01 00:38:24 +0000271 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000272 OMPSharedClause *Clause =
273 new (Mem) OMPSharedClause(StartLoc, LParenLoc, EndLoc, VL.size());
274 Clause->setVarRefs(VL);
275 return Clause;
276}
277
278OMPSharedClause *OMPSharedClause::CreateEmpty(const ASTContext &C, unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000279 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000280 return new (Mem) OMPSharedClause(N);
281}
282
283void OMPLinearClause::setPrivates(ArrayRef<Expr *> PL) {
284 assert(PL.size() == varlist_size() &&
285 "Number of privates is not the same as the preallocated buffer");
286 std::copy(PL.begin(), PL.end(), varlist_end());
287}
288
289void OMPLinearClause::setInits(ArrayRef<Expr *> IL) {
290 assert(IL.size() == varlist_size() &&
291 "Number of inits is not the same as the preallocated buffer");
292 std::copy(IL.begin(), IL.end(), getPrivates().end());
293}
294
295void OMPLinearClause::setUpdates(ArrayRef<Expr *> UL) {
296 assert(UL.size() == varlist_size() &&
297 "Number of updates is not the same as the preallocated buffer");
298 std::copy(UL.begin(), UL.end(), getInits().end());
299}
300
301void OMPLinearClause::setFinals(ArrayRef<Expr *> FL) {
302 assert(FL.size() == varlist_size() &&
303 "Number of final updates is not the same as the preallocated buffer");
304 std::copy(FL.begin(), FL.end(), getUpdates().end());
305}
306
307OMPLinearClause *OMPLinearClause::Create(
308 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
309 OpenMPLinearClauseKind Modifier, SourceLocation ModifierLoc,
310 SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef<Expr *> VL,
Alexey Bataev78849fb2016-03-09 09:49:00 +0000311 ArrayRef<Expr *> PL, ArrayRef<Expr *> IL, Expr *Step, Expr *CalcStep,
312 Stmt *PreInit, Expr *PostUpdate) {
James Y Knightb8bfd962015-10-02 13:41:04 +0000313 // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
314 // (Step and CalcStep).
James Y Knight374fd202016-01-01 00:38:24 +0000315 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size() + 2));
James Y Knightb8bfd962015-10-02 13:41:04 +0000316 OMPLinearClause *Clause = new (Mem) OMPLinearClause(
317 StartLoc, LParenLoc, Modifier, ModifierLoc, ColonLoc, EndLoc, VL.size());
318 Clause->setVarRefs(VL);
319 Clause->setPrivates(PL);
320 Clause->setInits(IL);
321 // Fill update and final expressions with zeroes, they are provided later,
322 // after the directive construction.
323 std::fill(Clause->getInits().end(), Clause->getInits().end() + VL.size(),
324 nullptr);
325 std::fill(Clause->getUpdates().end(), Clause->getUpdates().end() + VL.size(),
326 nullptr);
327 Clause->setStep(Step);
328 Clause->setCalcStep(CalcStep);
Alexey Bataev78849fb2016-03-09 09:49:00 +0000329 Clause->setPreInitStmt(PreInit);
330 Clause->setPostUpdateExpr(PostUpdate);
James Y Knightb8bfd962015-10-02 13:41:04 +0000331 return Clause;
332}
333
334OMPLinearClause *OMPLinearClause::CreateEmpty(const ASTContext &C,
335 unsigned NumVars) {
336 // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
337 // (Step and CalcStep).
James Y Knight374fd202016-01-01 00:38:24 +0000338 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * NumVars + 2));
James Y Knightb8bfd962015-10-02 13:41:04 +0000339 return new (Mem) OMPLinearClause(NumVars);
340}
341
342OMPAlignedClause *
343OMPAlignedClause::Create(const ASTContext &C, SourceLocation StartLoc,
344 SourceLocation LParenLoc, SourceLocation ColonLoc,
345 SourceLocation EndLoc, ArrayRef<Expr *> VL, Expr *A) {
James Y Knight374fd202016-01-01 00:38:24 +0000346 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
James Y Knightb8bfd962015-10-02 13:41:04 +0000347 OMPAlignedClause *Clause = new (Mem)
348 OMPAlignedClause(StartLoc, LParenLoc, ColonLoc, EndLoc, VL.size());
349 Clause->setVarRefs(VL);
350 Clause->setAlignment(A);
351 return Clause;
352}
353
354OMPAlignedClause *OMPAlignedClause::CreateEmpty(const ASTContext &C,
355 unsigned NumVars) {
James Y Knight374fd202016-01-01 00:38:24 +0000356 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumVars + 1));
James Y Knightb8bfd962015-10-02 13:41:04 +0000357 return new (Mem) OMPAlignedClause(NumVars);
358}
359
360void OMPCopyinClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
361 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
362 "not the same as the "
363 "preallocated buffer");
364 std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
365}
366
367void OMPCopyinClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
368 assert(DstExprs.size() == varlist_size() && "Number of destination "
369 "expressions is not the same as "
370 "the preallocated buffer");
371 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
372}
373
374void OMPCopyinClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
375 assert(AssignmentOps.size() == varlist_size() &&
376 "Number of assignment expressions is not the same as the preallocated "
377 "buffer");
378 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
379 getDestinationExprs().end());
380}
381
382OMPCopyinClause *OMPCopyinClause::Create(
383 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
384 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
385 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
James Y Knight374fd202016-01-01 00:38:24 +0000386 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000387 OMPCopyinClause *Clause =
388 new (Mem) OMPCopyinClause(StartLoc, LParenLoc, EndLoc, VL.size());
389 Clause->setVarRefs(VL);
390 Clause->setSourceExprs(SrcExprs);
391 Clause->setDestinationExprs(DstExprs);
392 Clause->setAssignmentOps(AssignmentOps);
393 return Clause;
394}
395
396OMPCopyinClause *OMPCopyinClause::CreateEmpty(const ASTContext &C, unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000397 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000398 return new (Mem) OMPCopyinClause(N);
399}
400
401void OMPCopyprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
402 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
403 "not the same as the "
404 "preallocated buffer");
405 std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
406}
407
408void OMPCopyprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
409 assert(DstExprs.size() == varlist_size() && "Number of destination "
410 "expressions is not the same as "
411 "the preallocated buffer");
412 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
413}
414
415void OMPCopyprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
416 assert(AssignmentOps.size() == varlist_size() &&
417 "Number of assignment expressions is not the same as the preallocated "
418 "buffer");
419 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
420 getDestinationExprs().end());
421}
422
423OMPCopyprivateClause *OMPCopyprivateClause::Create(
424 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
425 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
426 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
James Y Knight374fd202016-01-01 00:38:24 +0000427 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000428 OMPCopyprivateClause *Clause =
429 new (Mem) OMPCopyprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
430 Clause->setVarRefs(VL);
431 Clause->setSourceExprs(SrcExprs);
432 Clause->setDestinationExprs(DstExprs);
433 Clause->setAssignmentOps(AssignmentOps);
434 return Clause;
435}
436
437OMPCopyprivateClause *OMPCopyprivateClause::CreateEmpty(const ASTContext &C,
438 unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000439 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000440 return new (Mem) OMPCopyprivateClause(N);
441}
442
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000443void OMPReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
444 assert(Privates.size() == varlist_size() &&
445 "Number of private copies is not the same as the preallocated buffer");
446 std::copy(Privates.begin(), Privates.end(), varlist_end());
447}
448
James Y Knightb8bfd962015-10-02 13:41:04 +0000449void OMPReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
450 assert(
451 LHSExprs.size() == varlist_size() &&
452 "Number of LHS expressions is not the same as the preallocated buffer");
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000453 std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
James Y Knightb8bfd962015-10-02 13:41:04 +0000454}
455
456void OMPReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
457 assert(
458 RHSExprs.size() == varlist_size() &&
459 "Number of RHS expressions is not the same as the preallocated buffer");
460 std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
461}
462
463void OMPReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
464 assert(ReductionOps.size() == varlist_size() && "Number of reduction "
465 "expressions is not the same "
466 "as the preallocated buffer");
467 std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
468}
469
470OMPReductionClause *OMPReductionClause::Create(
471 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
472 SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
473 NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000474 ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
Alexey Bataev61205072016-03-02 04:57:40 +0000475 ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps, Stmt *PreInit,
476 Expr *PostUpdate) {
James Y Knight374fd202016-01-01 00:38:24 +0000477 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000478 OMPReductionClause *Clause = new (Mem) OMPReductionClause(
479 StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
480 Clause->setVarRefs(VL);
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000481 Clause->setPrivates(Privates);
James Y Knightb8bfd962015-10-02 13:41:04 +0000482 Clause->setLHSExprs(LHSExprs);
483 Clause->setRHSExprs(RHSExprs);
484 Clause->setReductionOps(ReductionOps);
Alexey Bataev61205072016-03-02 04:57:40 +0000485 Clause->setPreInitStmt(PreInit);
486 Clause->setPostUpdateExpr(PostUpdate);
James Y Knightb8bfd962015-10-02 13:41:04 +0000487 return Clause;
488}
489
490OMPReductionClause *OMPReductionClause::CreateEmpty(const ASTContext &C,
491 unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000492 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000493 return new (Mem) OMPReductionClause(N);
494}
495
496OMPFlushClause *OMPFlushClause::Create(const ASTContext &C,
497 SourceLocation StartLoc,
498 SourceLocation LParenLoc,
499 SourceLocation EndLoc,
500 ArrayRef<Expr *> VL) {
Alexey Bataev8b427062016-05-25 12:36:08 +0000501 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
James Y Knightb8bfd962015-10-02 13:41:04 +0000502 OMPFlushClause *Clause =
503 new (Mem) OMPFlushClause(StartLoc, LParenLoc, EndLoc, VL.size());
504 Clause->setVarRefs(VL);
505 return Clause;
506}
507
508OMPFlushClause *OMPFlushClause::CreateEmpty(const ASTContext &C, unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000509 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000510 return new (Mem) OMPFlushClause(N);
511}
512
Alexey Bataev8b427062016-05-25 12:36:08 +0000513OMPDependClause *OMPDependClause::Create(
514 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
515 SourceLocation EndLoc, OpenMPDependClauseKind DepKind,
516 SourceLocation DepLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL) {
517 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
James Y Knightb8bfd962015-10-02 13:41:04 +0000518 OMPDependClause *Clause =
519 new (Mem) OMPDependClause(StartLoc, LParenLoc, EndLoc, VL.size());
520 Clause->setVarRefs(VL);
521 Clause->setDependencyKind(DepKind);
522 Clause->setDependencyLoc(DepLoc);
523 Clause->setColonLoc(ColonLoc);
Alexey Bataev8b427062016-05-25 12:36:08 +0000524 Clause->setCounterValue(nullptr);
James Y Knightb8bfd962015-10-02 13:41:04 +0000525 return Clause;
526}
527
528OMPDependClause *OMPDependClause::CreateEmpty(const ASTContext &C, unsigned N) {
Alexey Bataev2af820542016-05-25 12:51:24 +0000529 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N + 1));
James Y Knightb8bfd962015-10-02 13:41:04 +0000530 return new (Mem) OMPDependClause(N);
531}
Kelvin Li0bff7af2015-11-23 05:32:03 +0000532
Alexey Bataev8b427062016-05-25 12:36:08 +0000533void OMPDependClause::setCounterValue(Expr *V) {
534 assert(getDependencyKind() == OMPC_DEPEND_sink ||
535 getDependencyKind() == OMPC_DEPEND_source || V == nullptr);
536 *getVarRefs().end() = V;
537}
538
539const Expr *OMPDependClause::getCounterValue() const {
540 auto *V = *getVarRefs().end();
541 assert(getDependencyKind() == OMPC_DEPEND_sink ||
542 getDependencyKind() == OMPC_DEPEND_source || V == nullptr);
543 return V;
544}
545
546Expr *OMPDependClause::getCounterValue() {
547 auto *V = *getVarRefs().end();
548 assert(getDependencyKind() == OMPC_DEPEND_sink ||
549 getDependencyKind() == OMPC_DEPEND_source || V == nullptr);
550 return V;
551}
552
Samuel Antao90927002016-04-26 14:54:23 +0000553unsigned OMPClauseMappableExprCommon::getComponentsTotalNumber(
554 MappableExprComponentListsRef ComponentLists) {
555 unsigned TotalNum = 0u;
556 for (auto &C : ComponentLists)
557 TotalNum += C.size();
558 return TotalNum;
559}
560
561unsigned OMPClauseMappableExprCommon::getUniqueDeclarationsTotalNumber(
562 ArrayRef<ValueDecl *> Declarations) {
563 unsigned TotalNum = 0u;
564 llvm::SmallPtrSet<const ValueDecl *, 8> Cache;
565 for (auto *D : Declarations) {
566 const ValueDecl *VD = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr;
567 if (Cache.count(VD))
568 continue;
569 ++TotalNum;
570 Cache.insert(VD);
571 }
572 return TotalNum;
573}
574
575OMPMapClause *
576OMPMapClause::Create(const ASTContext &C, SourceLocation StartLoc,
577 SourceLocation LParenLoc, SourceLocation EndLoc,
578 ArrayRef<Expr *> Vars, ArrayRef<ValueDecl *> Declarations,
579 MappableExprComponentListsRef ComponentLists,
580 OpenMPMapClauseKind TypeModifier, OpenMPMapClauseKind Type,
581 bool TypeIsImplicit, SourceLocation TypeLoc) {
582
583 unsigned NumVars = Vars.size();
584 unsigned NumUniqueDeclarations =
585 getUniqueDeclarationsTotalNumber(Declarations);
586 unsigned NumComponentLists = ComponentLists.size();
587 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
588
589 // We need to allocate:
590 // NumVars x Expr* - we have an original list expression for each clause list
591 // entry.
592 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
593 // with each component list.
594 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
595 // number of lists for each unique declaration and the size of each component
596 // list.
597 // NumComponents x MappableComponent - the total of all the components in all
598 // the lists.
599 void *Mem = C.Allocate(
600 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
601 OMPClauseMappableExprCommon::MappableComponent>(
602 NumVars, NumUniqueDeclarations,
603 NumUniqueDeclarations + NumComponentLists, NumComponents));
604 OMPMapClause *Clause = new (Mem) OMPMapClause(
605 TypeModifier, Type, TypeIsImplicit, TypeLoc, StartLoc, LParenLoc, EndLoc,
606 NumVars, NumUniqueDeclarations, NumComponentLists, NumComponents);
607
608 Clause->setVarRefs(Vars);
609 Clause->setClauseInfo(Declarations, ComponentLists);
Kelvin Li0bff7af2015-11-23 05:32:03 +0000610 Clause->setMapTypeModifier(TypeModifier);
611 Clause->setMapType(Type);
612 Clause->setMapLoc(TypeLoc);
613 return Clause;
614}
615
Samuel Antao90927002016-04-26 14:54:23 +0000616OMPMapClause *OMPMapClause::CreateEmpty(const ASTContext &C, unsigned NumVars,
617 unsigned NumUniqueDeclarations,
618 unsigned NumComponentLists,
619 unsigned NumComponents) {
620 void *Mem = C.Allocate(
621 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
622 OMPClauseMappableExprCommon::MappableComponent>(
623 NumVars, NumUniqueDeclarations,
624 NumUniqueDeclarations + NumComponentLists, NumComponents));
625 return new (Mem) OMPMapClause(NumVars, NumUniqueDeclarations,
626 NumComponentLists, NumComponents);
Kelvin Li0bff7af2015-11-23 05:32:03 +0000627}