blob: 1449e37ce6ec9830fcb3ae3bae78c6d2ae1481a7 [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:
Alexey Bataev005248a2016-02-25 05:25:57 +000091 break;
92 }
93
94 return nullptr;
95}
96
97OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(OMPClause *C) {
98 auto *Res = OMPClauseWithPostUpdate::get(const_cast<const OMPClause *>(C));
99 return Res ? const_cast<OMPClauseWithPostUpdate *>(Res) : nullptr;
100}
101
102const OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(const OMPClause *C) {
103 switch (C->getClauseKind()) {
Alexey Bataev3392d762016-02-16 11:18:12 +0000104 case OMPC_lastprivate:
Alexey Bataev005248a2016-02-25 05:25:57 +0000105 return static_cast<const OMPLastprivateClause *>(C);
Alexey Bataev61205072016-03-02 04:57:40 +0000106 case OMPC_reduction:
107 return static_cast<const OMPReductionClause *>(C);
Alexey Bataev78849fb2016-03-09 09:49:00 +0000108 case OMPC_linear:
109 return static_cast<const OMPLinearClause *>(C);
Alexey Bataev005248a2016-02-25 05:25:57 +0000110 case OMPC_schedule:
111 case OMPC_dist_schedule:
112 case OMPC_firstprivate:
113 case OMPC_default:
114 case OMPC_proc_bind:
115 case OMPC_if:
116 case OMPC_final:
117 case OMPC_num_threads:
118 case OMPC_safelen:
119 case OMPC_simdlen:
120 case OMPC_collapse:
121 case OMPC_private:
Alexey Bataev3392d762016-02-16 11:18:12 +0000122 case OMPC_shared:
Alexey Bataev3392d762016-02-16 11:18:12 +0000123 case OMPC_aligned:
124 case OMPC_copyin:
125 case OMPC_copyprivate:
126 case OMPC_ordered:
127 case OMPC_nowait:
128 case OMPC_untied:
129 case OMPC_mergeable:
130 case OMPC_threadprivate:
131 case OMPC_flush:
132 case OMPC_read:
133 case OMPC_write:
134 case OMPC_update:
135 case OMPC_capture:
136 case OMPC_seq_cst:
137 case OMPC_depend:
138 case OMPC_device:
139 case OMPC_threads:
140 case OMPC_simd:
141 case OMPC_map:
142 case OMPC_num_teams:
143 case OMPC_thread_limit:
144 case OMPC_priority:
145 case OMPC_grainsize:
146 case OMPC_nogroup:
147 case OMPC_num_tasks:
148 case OMPC_hint:
149 case OMPC_defaultmap:
150 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000151 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +0000152 case OMPC_to:
Alexey Bataev3392d762016-02-16 11:18:12 +0000153 break;
154 }
155
156 return nullptr;
157}
158
James Y Knightb8bfd962015-10-02 13:41:04 +0000159void OMPPrivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
160 assert(VL.size() == varlist_size() &&
161 "Number of private copies is not the same as the preallocated buffer");
162 std::copy(VL.begin(), VL.end(), varlist_end());
163}
164
165OMPPrivateClause *
166OMPPrivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
167 SourceLocation LParenLoc, SourceLocation EndLoc,
168 ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL) {
169 // Allocate space for private variables and initializer expressions.
James Y Knight374fd202016-01-01 00:38:24 +0000170 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000171 OMPPrivateClause *Clause =
172 new (Mem) OMPPrivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
173 Clause->setVarRefs(VL);
174 Clause->setPrivateCopies(PrivateVL);
175 return Clause;
176}
177
178OMPPrivateClause *OMPPrivateClause::CreateEmpty(const ASTContext &C,
179 unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000180 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000181 return new (Mem) OMPPrivateClause(N);
182}
183
184void OMPFirstprivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
185 assert(VL.size() == varlist_size() &&
186 "Number of private copies is not the same as the preallocated buffer");
187 std::copy(VL.begin(), VL.end(), varlist_end());
188}
189
190void OMPFirstprivateClause::setInits(ArrayRef<Expr *> VL) {
191 assert(VL.size() == varlist_size() &&
192 "Number of inits is not the same as the preallocated buffer");
193 std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
194}
195
196OMPFirstprivateClause *
197OMPFirstprivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
198 SourceLocation LParenLoc, SourceLocation EndLoc,
199 ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL,
Alexey Bataev417089f2016-02-17 13:19:37 +0000200 ArrayRef<Expr *> InitVL, Stmt *PreInit) {
James Y Knight374fd202016-01-01 00:38:24 +0000201 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000202 OMPFirstprivateClause *Clause =
203 new (Mem) OMPFirstprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
204 Clause->setVarRefs(VL);
205 Clause->setPrivateCopies(PrivateVL);
206 Clause->setInits(InitVL);
Alexey Bataev417089f2016-02-17 13:19:37 +0000207 Clause->setPreInitStmt(PreInit);
James Y Knightb8bfd962015-10-02 13:41:04 +0000208 return Clause;
209}
210
211OMPFirstprivateClause *OMPFirstprivateClause::CreateEmpty(const ASTContext &C,
212 unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000213 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000214 return new (Mem) OMPFirstprivateClause(N);
215}
216
217void OMPLastprivateClause::setPrivateCopies(ArrayRef<Expr *> PrivateCopies) {
218 assert(PrivateCopies.size() == varlist_size() &&
219 "Number of private copies is not the same as the preallocated buffer");
220 std::copy(PrivateCopies.begin(), PrivateCopies.end(), varlist_end());
221}
222
223void OMPLastprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
224 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
225 "not the same as the "
226 "preallocated buffer");
227 std::copy(SrcExprs.begin(), SrcExprs.end(), getPrivateCopies().end());
228}
229
230void OMPLastprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
231 assert(DstExprs.size() == varlist_size() && "Number of destination "
232 "expressions is not the same as "
233 "the preallocated buffer");
234 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
235}
236
237void OMPLastprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
238 assert(AssignmentOps.size() == varlist_size() &&
239 "Number of assignment expressions is not the same as the preallocated "
240 "buffer");
241 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
242 getDestinationExprs().end());
243}
244
245OMPLastprivateClause *OMPLastprivateClause::Create(
246 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
247 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
Alexey Bataev005248a2016-02-25 05:25:57 +0000248 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps, Stmt *PreInit,
249 Expr *PostUpdate) {
James Y Knight374fd202016-01-01 00:38:24 +0000250 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000251 OMPLastprivateClause *Clause =
252 new (Mem) OMPLastprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
253 Clause->setVarRefs(VL);
254 Clause->setSourceExprs(SrcExprs);
255 Clause->setDestinationExprs(DstExprs);
256 Clause->setAssignmentOps(AssignmentOps);
Alexey Bataev005248a2016-02-25 05:25:57 +0000257 Clause->setPreInitStmt(PreInit);
258 Clause->setPostUpdateExpr(PostUpdate);
James Y Knightb8bfd962015-10-02 13:41:04 +0000259 return Clause;
260}
261
262OMPLastprivateClause *OMPLastprivateClause::CreateEmpty(const ASTContext &C,
263 unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000264 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000265 return new (Mem) OMPLastprivateClause(N);
266}
267
268OMPSharedClause *OMPSharedClause::Create(const ASTContext &C,
269 SourceLocation StartLoc,
270 SourceLocation LParenLoc,
271 SourceLocation EndLoc,
272 ArrayRef<Expr *> VL) {
James Y Knight374fd202016-01-01 00:38:24 +0000273 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000274 OMPSharedClause *Clause =
275 new (Mem) OMPSharedClause(StartLoc, LParenLoc, EndLoc, VL.size());
276 Clause->setVarRefs(VL);
277 return Clause;
278}
279
280OMPSharedClause *OMPSharedClause::CreateEmpty(const ASTContext &C, unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000281 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000282 return new (Mem) OMPSharedClause(N);
283}
284
285void OMPLinearClause::setPrivates(ArrayRef<Expr *> PL) {
286 assert(PL.size() == varlist_size() &&
287 "Number of privates is not the same as the preallocated buffer");
288 std::copy(PL.begin(), PL.end(), varlist_end());
289}
290
291void OMPLinearClause::setInits(ArrayRef<Expr *> IL) {
292 assert(IL.size() == varlist_size() &&
293 "Number of inits is not the same as the preallocated buffer");
294 std::copy(IL.begin(), IL.end(), getPrivates().end());
295}
296
297void OMPLinearClause::setUpdates(ArrayRef<Expr *> UL) {
298 assert(UL.size() == varlist_size() &&
299 "Number of updates is not the same as the preallocated buffer");
300 std::copy(UL.begin(), UL.end(), getInits().end());
301}
302
303void OMPLinearClause::setFinals(ArrayRef<Expr *> FL) {
304 assert(FL.size() == varlist_size() &&
305 "Number of final updates is not the same as the preallocated buffer");
306 std::copy(FL.begin(), FL.end(), getUpdates().end());
307}
308
309OMPLinearClause *OMPLinearClause::Create(
310 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
311 OpenMPLinearClauseKind Modifier, SourceLocation ModifierLoc,
312 SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef<Expr *> VL,
Alexey Bataev78849fb2016-03-09 09:49:00 +0000313 ArrayRef<Expr *> PL, ArrayRef<Expr *> IL, Expr *Step, Expr *CalcStep,
314 Stmt *PreInit, Expr *PostUpdate) {
James Y Knightb8bfd962015-10-02 13:41:04 +0000315 // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
316 // (Step and CalcStep).
James Y Knight374fd202016-01-01 00:38:24 +0000317 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size() + 2));
James Y Knightb8bfd962015-10-02 13:41:04 +0000318 OMPLinearClause *Clause = new (Mem) OMPLinearClause(
319 StartLoc, LParenLoc, Modifier, ModifierLoc, ColonLoc, EndLoc, VL.size());
320 Clause->setVarRefs(VL);
321 Clause->setPrivates(PL);
322 Clause->setInits(IL);
323 // Fill update and final expressions with zeroes, they are provided later,
324 // after the directive construction.
325 std::fill(Clause->getInits().end(), Clause->getInits().end() + VL.size(),
326 nullptr);
327 std::fill(Clause->getUpdates().end(), Clause->getUpdates().end() + VL.size(),
328 nullptr);
329 Clause->setStep(Step);
330 Clause->setCalcStep(CalcStep);
Alexey Bataev78849fb2016-03-09 09:49:00 +0000331 Clause->setPreInitStmt(PreInit);
332 Clause->setPostUpdateExpr(PostUpdate);
James Y Knightb8bfd962015-10-02 13:41:04 +0000333 return Clause;
334}
335
336OMPLinearClause *OMPLinearClause::CreateEmpty(const ASTContext &C,
337 unsigned NumVars) {
338 // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
339 // (Step and CalcStep).
James Y Knight374fd202016-01-01 00:38:24 +0000340 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * NumVars + 2));
James Y Knightb8bfd962015-10-02 13:41:04 +0000341 return new (Mem) OMPLinearClause(NumVars);
342}
343
344OMPAlignedClause *
345OMPAlignedClause::Create(const ASTContext &C, SourceLocation StartLoc,
346 SourceLocation LParenLoc, SourceLocation ColonLoc,
347 SourceLocation EndLoc, ArrayRef<Expr *> VL, Expr *A) {
James Y Knight374fd202016-01-01 00:38:24 +0000348 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
James Y Knightb8bfd962015-10-02 13:41:04 +0000349 OMPAlignedClause *Clause = new (Mem)
350 OMPAlignedClause(StartLoc, LParenLoc, ColonLoc, EndLoc, VL.size());
351 Clause->setVarRefs(VL);
352 Clause->setAlignment(A);
353 return Clause;
354}
355
356OMPAlignedClause *OMPAlignedClause::CreateEmpty(const ASTContext &C,
357 unsigned NumVars) {
James Y Knight374fd202016-01-01 00:38:24 +0000358 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumVars + 1));
James Y Knightb8bfd962015-10-02 13:41:04 +0000359 return new (Mem) OMPAlignedClause(NumVars);
360}
361
362void OMPCopyinClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
363 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
364 "not the same as the "
365 "preallocated buffer");
366 std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
367}
368
369void OMPCopyinClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
370 assert(DstExprs.size() == varlist_size() && "Number of destination "
371 "expressions is not the same as "
372 "the preallocated buffer");
373 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
374}
375
376void OMPCopyinClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
377 assert(AssignmentOps.size() == varlist_size() &&
378 "Number of assignment expressions is not the same as the preallocated "
379 "buffer");
380 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
381 getDestinationExprs().end());
382}
383
384OMPCopyinClause *OMPCopyinClause::Create(
385 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
386 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
387 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
James Y Knight374fd202016-01-01 00:38:24 +0000388 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000389 OMPCopyinClause *Clause =
390 new (Mem) OMPCopyinClause(StartLoc, LParenLoc, EndLoc, VL.size());
391 Clause->setVarRefs(VL);
392 Clause->setSourceExprs(SrcExprs);
393 Clause->setDestinationExprs(DstExprs);
394 Clause->setAssignmentOps(AssignmentOps);
395 return Clause;
396}
397
398OMPCopyinClause *OMPCopyinClause::CreateEmpty(const ASTContext &C, unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000399 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000400 return new (Mem) OMPCopyinClause(N);
401}
402
403void OMPCopyprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
404 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
405 "not the same as the "
406 "preallocated buffer");
407 std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
408}
409
410void OMPCopyprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
411 assert(DstExprs.size() == varlist_size() && "Number of destination "
412 "expressions is not the same as "
413 "the preallocated buffer");
414 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
415}
416
417void OMPCopyprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
418 assert(AssignmentOps.size() == varlist_size() &&
419 "Number of assignment expressions is not the same as the preallocated "
420 "buffer");
421 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
422 getDestinationExprs().end());
423}
424
425OMPCopyprivateClause *OMPCopyprivateClause::Create(
426 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
427 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
428 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
James Y Knight374fd202016-01-01 00:38:24 +0000429 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000430 OMPCopyprivateClause *Clause =
431 new (Mem) OMPCopyprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
432 Clause->setVarRefs(VL);
433 Clause->setSourceExprs(SrcExprs);
434 Clause->setDestinationExprs(DstExprs);
435 Clause->setAssignmentOps(AssignmentOps);
436 return Clause;
437}
438
439OMPCopyprivateClause *OMPCopyprivateClause::CreateEmpty(const ASTContext &C,
440 unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000441 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000442 return new (Mem) OMPCopyprivateClause(N);
443}
444
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000445void OMPReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
446 assert(Privates.size() == varlist_size() &&
447 "Number of private copies is not the same as the preallocated buffer");
448 std::copy(Privates.begin(), Privates.end(), varlist_end());
449}
450
James Y Knightb8bfd962015-10-02 13:41:04 +0000451void OMPReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
452 assert(
453 LHSExprs.size() == varlist_size() &&
454 "Number of LHS expressions is not the same as the preallocated buffer");
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000455 std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
James Y Knightb8bfd962015-10-02 13:41:04 +0000456}
457
458void OMPReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
459 assert(
460 RHSExprs.size() == varlist_size() &&
461 "Number of RHS expressions is not the same as the preallocated buffer");
462 std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
463}
464
465void OMPReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
466 assert(ReductionOps.size() == varlist_size() && "Number of reduction "
467 "expressions is not the same "
468 "as the preallocated buffer");
469 std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
470}
471
472OMPReductionClause *OMPReductionClause::Create(
473 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
474 SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
475 NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000476 ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
Alexey Bataev61205072016-03-02 04:57:40 +0000477 ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps, Stmt *PreInit,
478 Expr *PostUpdate) {
James Y Knight374fd202016-01-01 00:38:24 +0000479 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
James Y Knightb8bfd962015-10-02 13:41:04 +0000480 OMPReductionClause *Clause = new (Mem) OMPReductionClause(
481 StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
482 Clause->setVarRefs(VL);
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000483 Clause->setPrivates(Privates);
James Y Knightb8bfd962015-10-02 13:41:04 +0000484 Clause->setLHSExprs(LHSExprs);
485 Clause->setRHSExprs(RHSExprs);
486 Clause->setReductionOps(ReductionOps);
Alexey Bataev61205072016-03-02 04:57:40 +0000487 Clause->setPreInitStmt(PreInit);
488 Clause->setPostUpdateExpr(PostUpdate);
James Y Knightb8bfd962015-10-02 13:41:04 +0000489 return Clause;
490}
491
492OMPReductionClause *OMPReductionClause::CreateEmpty(const ASTContext &C,
493 unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000494 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000495 return new (Mem) OMPReductionClause(N);
496}
497
498OMPFlushClause *OMPFlushClause::Create(const ASTContext &C,
499 SourceLocation StartLoc,
500 SourceLocation LParenLoc,
501 SourceLocation EndLoc,
502 ArrayRef<Expr *> VL) {
Alexey Bataev8b427062016-05-25 12:36:08 +0000503 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
James Y Knightb8bfd962015-10-02 13:41:04 +0000504 OMPFlushClause *Clause =
505 new (Mem) OMPFlushClause(StartLoc, LParenLoc, EndLoc, VL.size());
506 Clause->setVarRefs(VL);
507 return Clause;
508}
509
510OMPFlushClause *OMPFlushClause::CreateEmpty(const ASTContext &C, unsigned N) {
James Y Knight374fd202016-01-01 00:38:24 +0000511 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
James Y Knightb8bfd962015-10-02 13:41:04 +0000512 return new (Mem) OMPFlushClause(N);
513}
514
Alexey Bataev8b427062016-05-25 12:36:08 +0000515OMPDependClause *OMPDependClause::Create(
516 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
517 SourceLocation EndLoc, OpenMPDependClauseKind DepKind,
518 SourceLocation DepLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL) {
519 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
James Y Knightb8bfd962015-10-02 13:41:04 +0000520 OMPDependClause *Clause =
521 new (Mem) OMPDependClause(StartLoc, LParenLoc, EndLoc, VL.size());
522 Clause->setVarRefs(VL);
523 Clause->setDependencyKind(DepKind);
524 Clause->setDependencyLoc(DepLoc);
525 Clause->setColonLoc(ColonLoc);
Alexey Bataev8b427062016-05-25 12:36:08 +0000526 Clause->setCounterValue(nullptr);
James Y Knightb8bfd962015-10-02 13:41:04 +0000527 return Clause;
528}
529
530OMPDependClause *OMPDependClause::CreateEmpty(const ASTContext &C, unsigned N) {
Alexey Bataev2af820542016-05-25 12:51:24 +0000531 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N + 1));
James Y Knightb8bfd962015-10-02 13:41:04 +0000532 return new (Mem) OMPDependClause(N);
533}
Kelvin Li0bff7af2015-11-23 05:32:03 +0000534
Alexey Bataev8b427062016-05-25 12:36:08 +0000535void OMPDependClause::setCounterValue(Expr *V) {
536 assert(getDependencyKind() == OMPC_DEPEND_sink ||
537 getDependencyKind() == OMPC_DEPEND_source || V == nullptr);
538 *getVarRefs().end() = V;
539}
540
541const Expr *OMPDependClause::getCounterValue() const {
542 auto *V = *getVarRefs().end();
543 assert(getDependencyKind() == OMPC_DEPEND_sink ||
544 getDependencyKind() == OMPC_DEPEND_source || V == nullptr);
545 return V;
546}
547
548Expr *OMPDependClause::getCounterValue() {
549 auto *V = *getVarRefs().end();
550 assert(getDependencyKind() == OMPC_DEPEND_sink ||
551 getDependencyKind() == OMPC_DEPEND_source || V == nullptr);
552 return V;
553}
554
Samuel Antao90927002016-04-26 14:54:23 +0000555unsigned OMPClauseMappableExprCommon::getComponentsTotalNumber(
556 MappableExprComponentListsRef ComponentLists) {
557 unsigned TotalNum = 0u;
558 for (auto &C : ComponentLists)
559 TotalNum += C.size();
560 return TotalNum;
561}
562
563unsigned OMPClauseMappableExprCommon::getUniqueDeclarationsTotalNumber(
564 ArrayRef<ValueDecl *> Declarations) {
565 unsigned TotalNum = 0u;
566 llvm::SmallPtrSet<const ValueDecl *, 8> Cache;
567 for (auto *D : Declarations) {
568 const ValueDecl *VD = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr;
569 if (Cache.count(VD))
570 continue;
571 ++TotalNum;
572 Cache.insert(VD);
573 }
574 return TotalNum;
575}
576
577OMPMapClause *
578OMPMapClause::Create(const ASTContext &C, SourceLocation StartLoc,
579 SourceLocation LParenLoc, SourceLocation EndLoc,
580 ArrayRef<Expr *> Vars, ArrayRef<ValueDecl *> Declarations,
581 MappableExprComponentListsRef ComponentLists,
582 OpenMPMapClauseKind TypeModifier, OpenMPMapClauseKind Type,
583 bool TypeIsImplicit, SourceLocation TypeLoc) {
584
585 unsigned NumVars = Vars.size();
586 unsigned NumUniqueDeclarations =
587 getUniqueDeclarationsTotalNumber(Declarations);
588 unsigned NumComponentLists = ComponentLists.size();
589 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
590
591 // We need to allocate:
592 // NumVars x Expr* - we have an original list expression for each clause list
593 // entry.
594 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
595 // with each component list.
596 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
597 // number of lists for each unique declaration and the size of each component
598 // list.
599 // NumComponents x MappableComponent - the total of all the components in all
600 // the lists.
601 void *Mem = C.Allocate(
602 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
603 OMPClauseMappableExprCommon::MappableComponent>(
604 NumVars, NumUniqueDeclarations,
605 NumUniqueDeclarations + NumComponentLists, NumComponents));
606 OMPMapClause *Clause = new (Mem) OMPMapClause(
607 TypeModifier, Type, TypeIsImplicit, TypeLoc, StartLoc, LParenLoc, EndLoc,
608 NumVars, NumUniqueDeclarations, NumComponentLists, NumComponents);
609
610 Clause->setVarRefs(Vars);
611 Clause->setClauseInfo(Declarations, ComponentLists);
Kelvin Li0bff7af2015-11-23 05:32:03 +0000612 Clause->setMapTypeModifier(TypeModifier);
613 Clause->setMapType(Type);
614 Clause->setMapLoc(TypeLoc);
615 return Clause;
616}
617
Samuel Antao90927002016-04-26 14:54:23 +0000618OMPMapClause *OMPMapClause::CreateEmpty(const ASTContext &C, unsigned NumVars,
619 unsigned NumUniqueDeclarations,
620 unsigned NumComponentLists,
621 unsigned NumComponents) {
622 void *Mem = C.Allocate(
623 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
624 OMPClauseMappableExprCommon::MappableComponent>(
625 NumVars, NumUniqueDeclarations,
626 NumUniqueDeclarations + NumComponentLists, NumComponents));
627 return new (Mem) OMPMapClause(NumVars, NumUniqueDeclarations,
628 NumComponentLists, NumComponents);
Kelvin Li0bff7af2015-11-23 05:32:03 +0000629}
Samuel Antao661c0902016-05-26 17:39:58 +0000630
631OMPToClause *OMPToClause::Create(const ASTContext &C, SourceLocation StartLoc,
632 SourceLocation LParenLoc,
633 SourceLocation EndLoc, ArrayRef<Expr *> Vars,
634 ArrayRef<ValueDecl *> Declarations,
635 MappableExprComponentListsRef ComponentLists) {
636 unsigned NumVars = Vars.size();
637 unsigned NumUniqueDeclarations =
638 getUniqueDeclarationsTotalNumber(Declarations);
639 unsigned NumComponentLists = ComponentLists.size();
640 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
641
642 // We need to allocate:
643 // NumVars x Expr* - we have an original list expression for each clause list
644 // entry.
645 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
646 // with each component list.
647 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
648 // number of lists for each unique declaration and the size of each component
649 // list.
650 // NumComponents x MappableComponent - the total of all the components in all
651 // the lists.
652 void *Mem = C.Allocate(
653 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
654 OMPClauseMappableExprCommon::MappableComponent>(
655 NumVars, NumUniqueDeclarations,
656 NumUniqueDeclarations + NumComponentLists, NumComponents));
657
658 OMPToClause *Clause = new (Mem)
659 OMPToClause(StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
660 NumComponentLists, NumComponents);
661
662 Clause->setVarRefs(Vars);
663 Clause->setClauseInfo(Declarations, ComponentLists);
664 return Clause;
665}
666
667OMPToClause *OMPToClause::CreateEmpty(const ASTContext &C, unsigned NumVars,
668 unsigned NumUniqueDeclarations,
669 unsigned NumComponentLists,
670 unsigned NumComponents) {
671 void *Mem = C.Allocate(
672 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
673 OMPClauseMappableExprCommon::MappableComponent>(
674 NumVars, NumUniqueDeclarations,
675 NumUniqueDeclarations + NumComponentLists, NumComponents));
676 return new (Mem) OMPToClause(NumVars, NumUniqueDeclarations,
677 NumComponentLists, NumComponents);
678}