1 //===- OpenMPClause.cpp - Classes for OpenMP clauses ----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the subclesses of Stmt class declared in OpenMPClause.h
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "clang/AST/OpenMPClause.h"
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/Attr.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/DeclOpenMP.h"
18 #include "clang/Basic/LLVM.h"
19 #include "clang/Basic/OpenMPKinds.h"
20 #include "clang/Basic/TargetInfo.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/Support/Casting.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include <algorithm>
25 #include <cassert>
26 
27 using namespace clang;
28 using namespace llvm;
29 using namespace omp;
30 
31 OMPClause::child_range OMPClause::children() {
32   switch (getClauseKind()) {
33   default:
34     break;
35 #define GEN_CLANG_CLAUSE_CLASS
36 #define CLAUSE_CLASS(Enum, Str, Class)                                         \
37   case Enum:                                                                   \
38     return static_cast<Class *>(this)->children();
39 #include "llvm/Frontend/OpenMP/OMP.inc"
40   }
41   llvm_unreachable("unknown OMPClause");
42 }
43 
44 OMPClause::child_range OMPClause::used_children() {
45   switch (getClauseKind()) {
46 #define GEN_CLANG_CLAUSE_CLASS
47 #define CLAUSE_CLASS(Enum, Str, Class)                                         \
48   case Enum:                                                                   \
49     return static_cast<Class *>(this)->used_children();
50 #define CLAUSE_NO_CLASS(Enum, Str)                                             \
51   case Enum:                                                                   \
52     break;
53 #include "llvm/Frontend/OpenMP/OMP.inc"
54   }
55   llvm_unreachable("unknown OMPClause");
56 }
57 
58 OMPClauseWithPreInit *OMPClauseWithPreInit::get(OMPClause *C) {
59   auto *Res = OMPClauseWithPreInit::get(const_cast<const OMPClause *>(C));
60   return Res ? const_cast<OMPClauseWithPreInit *>(Res) : nullptr;
61 }
62 
63 const OMPClauseWithPreInit *OMPClauseWithPreInit::get(const OMPClause *C) {
64   switch (C->getClauseKind()) {
65   case OMPC_schedule:
66     return static_cast<const OMPScheduleClause *>(C);
67   case OMPC_dist_schedule:
68     return static_cast<const OMPDistScheduleClause *>(C);
69   case OMPC_firstprivate:
70     return static_cast<const OMPFirstprivateClause *>(C);
71   case OMPC_lastprivate:
72     return static_cast<const OMPLastprivateClause *>(C);
73   case OMPC_reduction:
74     return static_cast<const OMPReductionClause *>(C);
75   case OMPC_task_reduction:
76     return static_cast<const OMPTaskReductionClause *>(C);
77   case OMPC_in_reduction:
78     return static_cast<const OMPInReductionClause *>(C);
79   case OMPC_linear:
80     return static_cast<const OMPLinearClause *>(C);
81   case OMPC_if:
82     return static_cast<const OMPIfClause *>(C);
83   case OMPC_num_threads:
84     return static_cast<const OMPNumThreadsClause *>(C);
85   case OMPC_num_teams:
86     return static_cast<const OMPNumTeamsClause *>(C);
87   case OMPC_thread_limit:
88     return static_cast<const OMPThreadLimitClause *>(C);
89   case OMPC_device:
90     return static_cast<const OMPDeviceClause *>(C);
91   case OMPC_grainsize:
92     return static_cast<const OMPGrainsizeClause *>(C);
93   case OMPC_num_tasks:
94     return static_cast<const OMPNumTasksClause *>(C);
95   case OMPC_final:
96     return static_cast<const OMPFinalClause *>(C);
97   case OMPC_priority:
98     return static_cast<const OMPPriorityClause *>(C);
99   case OMPC_novariants:
100     return static_cast<const OMPNovariantsClause *>(C);
101   case OMPC_default:
102   case OMPC_proc_bind:
103   case OMPC_safelen:
104   case OMPC_simdlen:
105   case OMPC_sizes:
106   case OMPC_allocator:
107   case OMPC_allocate:
108   case OMPC_collapse:
109   case OMPC_private:
110   case OMPC_shared:
111   case OMPC_aligned:
112   case OMPC_copyin:
113   case OMPC_copyprivate:
114   case OMPC_ordered:
115   case OMPC_nowait:
116   case OMPC_untied:
117   case OMPC_mergeable:
118   case OMPC_threadprivate:
119   case OMPC_flush:
120   case OMPC_depobj:
121   case OMPC_read:
122   case OMPC_write:
123   case OMPC_update:
124   case OMPC_capture:
125   case OMPC_seq_cst:
126   case OMPC_acq_rel:
127   case OMPC_acquire:
128   case OMPC_release:
129   case OMPC_relaxed:
130   case OMPC_depend:
131   case OMPC_threads:
132   case OMPC_simd:
133   case OMPC_map:
134   case OMPC_nogroup:
135   case OMPC_hint:
136   case OMPC_defaultmap:
137   case OMPC_unknown:
138   case OMPC_uniform:
139   case OMPC_to:
140   case OMPC_from:
141   case OMPC_use_device_ptr:
142   case OMPC_use_device_addr:
143   case OMPC_is_device_ptr:
144   case OMPC_unified_address:
145   case OMPC_unified_shared_memory:
146   case OMPC_reverse_offload:
147   case OMPC_dynamic_allocators:
148   case OMPC_atomic_default_mem_order:
149   case OMPC_device_type:
150   case OMPC_match:
151   case OMPC_nontemporal:
152   case OMPC_order:
153   case OMPC_destroy:
154   case OMPC_detach:
155   case OMPC_inclusive:
156   case OMPC_exclusive:
157   case OMPC_uses_allocators:
158   case OMPC_affinity:
159     break;
160   default:
161     break;
162   }
163 
164   return nullptr;
165 }
166 
167 OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(OMPClause *C) {
168   auto *Res = OMPClauseWithPostUpdate::get(const_cast<const OMPClause *>(C));
169   return Res ? const_cast<OMPClauseWithPostUpdate *>(Res) : nullptr;
170 }
171 
172 const OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(const OMPClause *C) {
173   switch (C->getClauseKind()) {
174   case OMPC_lastprivate:
175     return static_cast<const OMPLastprivateClause *>(C);
176   case OMPC_reduction:
177     return static_cast<const OMPReductionClause *>(C);
178   case OMPC_task_reduction:
179     return static_cast<const OMPTaskReductionClause *>(C);
180   case OMPC_in_reduction:
181     return static_cast<const OMPInReductionClause *>(C);
182   case OMPC_linear:
183     return static_cast<const OMPLinearClause *>(C);
184   case OMPC_schedule:
185   case OMPC_dist_schedule:
186   case OMPC_firstprivate:
187   case OMPC_default:
188   case OMPC_proc_bind:
189   case OMPC_if:
190   case OMPC_final:
191   case OMPC_num_threads:
192   case OMPC_safelen:
193   case OMPC_simdlen:
194   case OMPC_sizes:
195   case OMPC_allocator:
196   case OMPC_allocate:
197   case OMPC_collapse:
198   case OMPC_private:
199   case OMPC_shared:
200   case OMPC_aligned:
201   case OMPC_copyin:
202   case OMPC_copyprivate:
203   case OMPC_ordered:
204   case OMPC_nowait:
205   case OMPC_untied:
206   case OMPC_mergeable:
207   case OMPC_threadprivate:
208   case OMPC_flush:
209   case OMPC_depobj:
210   case OMPC_read:
211   case OMPC_write:
212   case OMPC_update:
213   case OMPC_capture:
214   case OMPC_seq_cst:
215   case OMPC_acq_rel:
216   case OMPC_acquire:
217   case OMPC_release:
218   case OMPC_relaxed:
219   case OMPC_depend:
220   case OMPC_device:
221   case OMPC_threads:
222   case OMPC_simd:
223   case OMPC_map:
224   case OMPC_num_teams:
225   case OMPC_thread_limit:
226   case OMPC_priority:
227   case OMPC_grainsize:
228   case OMPC_nogroup:
229   case OMPC_num_tasks:
230   case OMPC_hint:
231   case OMPC_defaultmap:
232   case OMPC_unknown:
233   case OMPC_uniform:
234   case OMPC_to:
235   case OMPC_from:
236   case OMPC_use_device_ptr:
237   case OMPC_use_device_addr:
238   case OMPC_is_device_ptr:
239   case OMPC_unified_address:
240   case OMPC_unified_shared_memory:
241   case OMPC_reverse_offload:
242   case OMPC_dynamic_allocators:
243   case OMPC_atomic_default_mem_order:
244   case OMPC_device_type:
245   case OMPC_match:
246   case OMPC_nontemporal:
247   case OMPC_order:
248   case OMPC_destroy:
249   case OMPC_novariants:
250   case OMPC_detach:
251   case OMPC_inclusive:
252   case OMPC_exclusive:
253   case OMPC_uses_allocators:
254   case OMPC_affinity:
255     break;
256   default:
257     break;
258   }
259 
260   return nullptr;
261 }
262 
263 /// Gets the address of the original, non-captured, expression used in the
264 /// clause as the preinitializer.
265 static Stmt **getAddrOfExprAsWritten(Stmt *S) {
266   if (!S)
267     return nullptr;
268   if (auto *DS = dyn_cast<DeclStmt>(S)) {
269     assert(DS->isSingleDecl() && "Only single expression must be captured.");
270     if (auto *OED = dyn_cast<OMPCapturedExprDecl>(DS->getSingleDecl()))
271       return OED->getInitAddress();
272   }
273   return nullptr;
274 }
275 
276 OMPClause::child_range OMPIfClause::used_children() {
277   if (Stmt **C = getAddrOfExprAsWritten(getPreInitStmt()))
278     return child_range(C, C + 1);
279   return child_range(&Condition, &Condition + 1);
280 }
281 
282 OMPClause::child_range OMPGrainsizeClause::used_children() {
283   if (Stmt **C = getAddrOfExprAsWritten(getPreInitStmt()))
284     return child_range(C, C + 1);
285   return child_range(&Grainsize, &Grainsize + 1);
286 }
287 
288 OMPClause::child_range OMPNumTasksClause::used_children() {
289   if (Stmt **C = getAddrOfExprAsWritten(getPreInitStmt()))
290     return child_range(C, C + 1);
291   return child_range(&NumTasks, &NumTasks + 1);
292 }
293 
294 OMPClause::child_range OMPFinalClause::used_children() {
295   if (Stmt **C = getAddrOfExprAsWritten(getPreInitStmt()))
296     return child_range(C, C + 1);
297   return child_range(&Condition, &Condition + 1);
298 }
299 
300 OMPClause::child_range OMPPriorityClause::used_children() {
301   if (Stmt **C = getAddrOfExprAsWritten(getPreInitStmt()))
302     return child_range(C, C + 1);
303   return child_range(&Priority, &Priority + 1);
304 }
305 
306 OMPClause::child_range OMPNovariantsClause::used_children() {
307   if (Stmt **C = getAddrOfExprAsWritten(getPreInitStmt()))
308     return child_range(C, C + 1);
309   return child_range(&Condition, &Condition + 1);
310 }
311 
312 OMPOrderedClause *OMPOrderedClause::Create(const ASTContext &C, Expr *Num,
313                                            unsigned NumLoops,
314                                            SourceLocation StartLoc,
315                                            SourceLocation LParenLoc,
316                                            SourceLocation EndLoc) {
317   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * NumLoops));
318   auto *Clause =
319       new (Mem) OMPOrderedClause(Num, NumLoops, StartLoc, LParenLoc, EndLoc);
320   for (unsigned I = 0; I < NumLoops; ++I) {
321     Clause->setLoopNumIterations(I, nullptr);
322     Clause->setLoopCounter(I, nullptr);
323   }
324   return Clause;
325 }
326 
327 OMPOrderedClause *OMPOrderedClause::CreateEmpty(const ASTContext &C,
328                                                 unsigned NumLoops) {
329   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * NumLoops));
330   auto *Clause = new (Mem) OMPOrderedClause(NumLoops);
331   for (unsigned I = 0; I < NumLoops; ++I) {
332     Clause->setLoopNumIterations(I, nullptr);
333     Clause->setLoopCounter(I, nullptr);
334   }
335   return Clause;
336 }
337 
338 void OMPOrderedClause::setLoopNumIterations(unsigned NumLoop,
339                                             Expr *NumIterations) {
340   assert(NumLoop < NumberOfLoops && "out of loops number.");
341   getTrailingObjects<Expr *>()[NumLoop] = NumIterations;
342 }
343 
344 ArrayRef<Expr *> OMPOrderedClause::getLoopNumIterations() const {
345   return llvm::makeArrayRef(getTrailingObjects<Expr *>(), NumberOfLoops);
346 }
347 
348 void OMPOrderedClause::setLoopCounter(unsigned NumLoop, Expr *Counter) {
349   assert(NumLoop < NumberOfLoops && "out of loops number.");
350   getTrailingObjects<Expr *>()[NumberOfLoops + NumLoop] = Counter;
351 }
352 
353 Expr *OMPOrderedClause::getLoopCounter(unsigned NumLoop) {
354   assert(NumLoop < NumberOfLoops && "out of loops number.");
355   return getTrailingObjects<Expr *>()[NumberOfLoops + NumLoop];
356 }
357 
358 const Expr *OMPOrderedClause::getLoopCounter(unsigned NumLoop) const {
359   assert(NumLoop < NumberOfLoops && "out of loops number.");
360   return getTrailingObjects<Expr *>()[NumberOfLoops + NumLoop];
361 }
362 
363 OMPUpdateClause *OMPUpdateClause::Create(const ASTContext &C,
364                                          SourceLocation StartLoc,
365                                          SourceLocation EndLoc) {
366   return new (C) OMPUpdateClause(StartLoc, EndLoc, /*IsExtended=*/false);
367 }
368 
369 OMPUpdateClause *
370 OMPUpdateClause::Create(const ASTContext &C, SourceLocation StartLoc,
371                         SourceLocation LParenLoc, SourceLocation ArgumentLoc,
372                         OpenMPDependClauseKind DK, SourceLocation EndLoc) {
373   void *Mem =
374       C.Allocate(totalSizeToAlloc<SourceLocation, OpenMPDependClauseKind>(2, 1),
375                  alignof(OMPUpdateClause));
376   auto *Clause =
377       new (Mem) OMPUpdateClause(StartLoc, EndLoc, /*IsExtended=*/true);
378   Clause->setLParenLoc(LParenLoc);
379   Clause->setArgumentLoc(ArgumentLoc);
380   Clause->setDependencyKind(DK);
381   return Clause;
382 }
383 
384 OMPUpdateClause *OMPUpdateClause::CreateEmpty(const ASTContext &C,
385                                               bool IsExtended) {
386   if (!IsExtended)
387     return new (C) OMPUpdateClause(/*IsExtended=*/false);
388   void *Mem =
389       C.Allocate(totalSizeToAlloc<SourceLocation, OpenMPDependClauseKind>(2, 1),
390                  alignof(OMPUpdateClause));
391   auto *Clause = new (Mem) OMPUpdateClause(/*IsExtended=*/true);
392   Clause->IsExtended = true;
393   return Clause;
394 }
395 
396 void OMPPrivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
397   assert(VL.size() == varlist_size() &&
398          "Number of private copies is not the same as the preallocated buffer");
399   std::copy(VL.begin(), VL.end(), varlist_end());
400 }
401 
402 OMPPrivateClause *
403 OMPPrivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
404                          SourceLocation LParenLoc, SourceLocation EndLoc,
405                          ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL) {
406   // Allocate space for private variables and initializer expressions.
407   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * VL.size()));
408   OMPPrivateClause *Clause =
409       new (Mem) OMPPrivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
410   Clause->setVarRefs(VL);
411   Clause->setPrivateCopies(PrivateVL);
412   return Clause;
413 }
414 
415 OMPPrivateClause *OMPPrivateClause::CreateEmpty(const ASTContext &C,
416                                                 unsigned N) {
417   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * N));
418   return new (Mem) OMPPrivateClause(N);
419 }
420 
421 void OMPFirstprivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
422   assert(VL.size() == varlist_size() &&
423          "Number of private copies is not the same as the preallocated buffer");
424   std::copy(VL.begin(), VL.end(), varlist_end());
425 }
426 
427 void OMPFirstprivateClause::setInits(ArrayRef<Expr *> VL) {
428   assert(VL.size() == varlist_size() &&
429          "Number of inits is not the same as the preallocated buffer");
430   std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
431 }
432 
433 OMPFirstprivateClause *
434 OMPFirstprivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
435                               SourceLocation LParenLoc, SourceLocation EndLoc,
436                               ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL,
437                               ArrayRef<Expr *> InitVL, Stmt *PreInit) {
438   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * VL.size()));
439   OMPFirstprivateClause *Clause =
440       new (Mem) OMPFirstprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
441   Clause->setVarRefs(VL);
442   Clause->setPrivateCopies(PrivateVL);
443   Clause->setInits(InitVL);
444   Clause->setPreInitStmt(PreInit);
445   return Clause;
446 }
447 
448 OMPFirstprivateClause *OMPFirstprivateClause::CreateEmpty(const ASTContext &C,
449                                                           unsigned N) {
450   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * N));
451   return new (Mem) OMPFirstprivateClause(N);
452 }
453 
454 void OMPLastprivateClause::setPrivateCopies(ArrayRef<Expr *> PrivateCopies) {
455   assert(PrivateCopies.size() == varlist_size() &&
456          "Number of private copies is not the same as the preallocated buffer");
457   std::copy(PrivateCopies.begin(), PrivateCopies.end(), varlist_end());
458 }
459 
460 void OMPLastprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
461   assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
462                                               "not the same as the "
463                                               "preallocated buffer");
464   std::copy(SrcExprs.begin(), SrcExprs.end(), getPrivateCopies().end());
465 }
466 
467 void OMPLastprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
468   assert(DstExprs.size() == varlist_size() && "Number of destination "
469                                               "expressions is not the same as "
470                                               "the preallocated buffer");
471   std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
472 }
473 
474 void OMPLastprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
475   assert(AssignmentOps.size() == varlist_size() &&
476          "Number of assignment expressions is not the same as the preallocated "
477          "buffer");
478   std::copy(AssignmentOps.begin(), AssignmentOps.end(),
479             getDestinationExprs().end());
480 }
481 
482 OMPLastprivateClause *OMPLastprivateClause::Create(
483     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
484     SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
485     ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps,
486     OpenMPLastprivateModifier LPKind, SourceLocation LPKindLoc,
487     SourceLocation ColonLoc, Stmt *PreInit, Expr *PostUpdate) {
488   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
489   OMPLastprivateClause *Clause = new (Mem) OMPLastprivateClause(
490       StartLoc, LParenLoc, EndLoc, LPKind, LPKindLoc, ColonLoc, VL.size());
491   Clause->setVarRefs(VL);
492   Clause->setSourceExprs(SrcExprs);
493   Clause->setDestinationExprs(DstExprs);
494   Clause->setAssignmentOps(AssignmentOps);
495   Clause->setPreInitStmt(PreInit);
496   Clause->setPostUpdateExpr(PostUpdate);
497   return Clause;
498 }
499 
500 OMPLastprivateClause *OMPLastprivateClause::CreateEmpty(const ASTContext &C,
501                                                         unsigned N) {
502   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
503   return new (Mem) OMPLastprivateClause(N);
504 }
505 
506 OMPSharedClause *OMPSharedClause::Create(const ASTContext &C,
507                                          SourceLocation StartLoc,
508                                          SourceLocation LParenLoc,
509                                          SourceLocation EndLoc,
510                                          ArrayRef<Expr *> VL) {
511   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size()));
512   OMPSharedClause *Clause =
513       new (Mem) OMPSharedClause(StartLoc, LParenLoc, EndLoc, VL.size());
514   Clause->setVarRefs(VL);
515   return Clause;
516 }
517 
518 OMPSharedClause *OMPSharedClause::CreateEmpty(const ASTContext &C, unsigned N) {
519   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
520   return new (Mem) OMPSharedClause(N);
521 }
522 
523 void OMPLinearClause::setPrivates(ArrayRef<Expr *> PL) {
524   assert(PL.size() == varlist_size() &&
525          "Number of privates is not the same as the preallocated buffer");
526   std::copy(PL.begin(), PL.end(), varlist_end());
527 }
528 
529 void OMPLinearClause::setInits(ArrayRef<Expr *> IL) {
530   assert(IL.size() == varlist_size() &&
531          "Number of inits is not the same as the preallocated buffer");
532   std::copy(IL.begin(), IL.end(), getPrivates().end());
533 }
534 
535 void OMPLinearClause::setUpdates(ArrayRef<Expr *> UL) {
536   assert(UL.size() == varlist_size() &&
537          "Number of updates is not the same as the preallocated buffer");
538   std::copy(UL.begin(), UL.end(), getInits().end());
539 }
540 
541 void OMPLinearClause::setFinals(ArrayRef<Expr *> FL) {
542   assert(FL.size() == varlist_size() &&
543          "Number of final updates is not the same as the preallocated buffer");
544   std::copy(FL.begin(), FL.end(), getUpdates().end());
545 }
546 
547 void OMPLinearClause::setUsedExprs(ArrayRef<Expr *> UE) {
548   assert(
549       UE.size() == varlist_size() + 1 &&
550       "Number of used expressions is not the same as the preallocated buffer");
551   std::copy(UE.begin(), UE.end(), getFinals().end() + 2);
552 }
553 
554 OMPLinearClause *OMPLinearClause::Create(
555     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
556     OpenMPLinearClauseKind Modifier, SourceLocation ModifierLoc,
557     SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef<Expr *> VL,
558     ArrayRef<Expr *> PL, ArrayRef<Expr *> IL, Expr *Step, Expr *CalcStep,
559     Stmt *PreInit, Expr *PostUpdate) {
560   // Allocate space for 5 lists (Vars, Inits, Updates, Finals), 2 expressions
561   // (Step and CalcStep), list of used expression + step.
562   void *Mem =
563       C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size() + 2 + VL.size() + 1));
564   OMPLinearClause *Clause = new (Mem) OMPLinearClause(
565       StartLoc, LParenLoc, Modifier, ModifierLoc, ColonLoc, EndLoc, VL.size());
566   Clause->setVarRefs(VL);
567   Clause->setPrivates(PL);
568   Clause->setInits(IL);
569   // Fill update and final expressions with zeroes, they are provided later,
570   // after the directive construction.
571   std::fill(Clause->getInits().end(), Clause->getInits().end() + VL.size(),
572             nullptr);
573   std::fill(Clause->getUpdates().end(), Clause->getUpdates().end() + VL.size(),
574             nullptr);
575   std::fill(Clause->getUsedExprs().begin(), Clause->getUsedExprs().end(),
576             nullptr);
577   Clause->setStep(Step);
578   Clause->setCalcStep(CalcStep);
579   Clause->setPreInitStmt(PreInit);
580   Clause->setPostUpdateExpr(PostUpdate);
581   return Clause;
582 }
583 
584 OMPLinearClause *OMPLinearClause::CreateEmpty(const ASTContext &C,
585                                               unsigned NumVars) {
586   // Allocate space for 5 lists (Vars, Inits, Updates, Finals), 2 expressions
587   // (Step and CalcStep), list of used expression + step.
588   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * NumVars + 2 + NumVars  +1));
589   return new (Mem) OMPLinearClause(NumVars);
590 }
591 
592 OMPClause::child_range OMPLinearClause::used_children() {
593   // Range includes only non-nullptr elements.
594   return child_range(
595       reinterpret_cast<Stmt **>(getUsedExprs().begin()),
596       reinterpret_cast<Stmt **>(llvm::find(getUsedExprs(), nullptr)));
597 }
598 
599 OMPAlignedClause *
600 OMPAlignedClause::Create(const ASTContext &C, SourceLocation StartLoc,
601                          SourceLocation LParenLoc, SourceLocation ColonLoc,
602                          SourceLocation EndLoc, ArrayRef<Expr *> VL, Expr *A) {
603   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
604   OMPAlignedClause *Clause = new (Mem)
605       OMPAlignedClause(StartLoc, LParenLoc, ColonLoc, EndLoc, VL.size());
606   Clause->setVarRefs(VL);
607   Clause->setAlignment(A);
608   return Clause;
609 }
610 
611 OMPAlignedClause *OMPAlignedClause::CreateEmpty(const ASTContext &C,
612                                                 unsigned NumVars) {
613   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumVars + 1));
614   return new (Mem) OMPAlignedClause(NumVars);
615 }
616 
617 void OMPCopyinClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
618   assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
619                                               "not the same as the "
620                                               "preallocated buffer");
621   std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
622 }
623 
624 void OMPCopyinClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
625   assert(DstExprs.size() == varlist_size() && "Number of destination "
626                                               "expressions is not the same as "
627                                               "the preallocated buffer");
628   std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
629 }
630 
631 void OMPCopyinClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
632   assert(AssignmentOps.size() == varlist_size() &&
633          "Number of assignment expressions is not the same as the preallocated "
634          "buffer");
635   std::copy(AssignmentOps.begin(), AssignmentOps.end(),
636             getDestinationExprs().end());
637 }
638 
639 OMPCopyinClause *OMPCopyinClause::Create(
640     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
641     SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
642     ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
643   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
644   OMPCopyinClause *Clause =
645       new (Mem) OMPCopyinClause(StartLoc, LParenLoc, EndLoc, VL.size());
646   Clause->setVarRefs(VL);
647   Clause->setSourceExprs(SrcExprs);
648   Clause->setDestinationExprs(DstExprs);
649   Clause->setAssignmentOps(AssignmentOps);
650   return Clause;
651 }
652 
653 OMPCopyinClause *OMPCopyinClause::CreateEmpty(const ASTContext &C, unsigned N) {
654   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
655   return new (Mem) OMPCopyinClause(N);
656 }
657 
658 void OMPCopyprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
659   assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
660                                               "not the same as the "
661                                               "preallocated buffer");
662   std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
663 }
664 
665 void OMPCopyprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
666   assert(DstExprs.size() == varlist_size() && "Number of destination "
667                                               "expressions is not the same as "
668                                               "the preallocated buffer");
669   std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
670 }
671 
672 void OMPCopyprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
673   assert(AssignmentOps.size() == varlist_size() &&
674          "Number of assignment expressions is not the same as the preallocated "
675          "buffer");
676   std::copy(AssignmentOps.begin(), AssignmentOps.end(),
677             getDestinationExprs().end());
678 }
679 
680 OMPCopyprivateClause *OMPCopyprivateClause::Create(
681     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
682     SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
683     ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
684   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
685   OMPCopyprivateClause *Clause =
686       new (Mem) OMPCopyprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
687   Clause->setVarRefs(VL);
688   Clause->setSourceExprs(SrcExprs);
689   Clause->setDestinationExprs(DstExprs);
690   Clause->setAssignmentOps(AssignmentOps);
691   return Clause;
692 }
693 
694 OMPCopyprivateClause *OMPCopyprivateClause::CreateEmpty(const ASTContext &C,
695                                                         unsigned N) {
696   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
697   return new (Mem) OMPCopyprivateClause(N);
698 }
699 
700 void OMPReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
701   assert(Privates.size() == varlist_size() &&
702          "Number of private copies is not the same as the preallocated buffer");
703   std::copy(Privates.begin(), Privates.end(), varlist_end());
704 }
705 
706 void OMPReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
707   assert(
708       LHSExprs.size() == varlist_size() &&
709       "Number of LHS expressions is not the same as the preallocated buffer");
710   std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
711 }
712 
713 void OMPReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
714   assert(
715       RHSExprs.size() == varlist_size() &&
716       "Number of RHS expressions is not the same as the preallocated buffer");
717   std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
718 }
719 
720 void OMPReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
721   assert(ReductionOps.size() == varlist_size() && "Number of reduction "
722                                                   "expressions is not the same "
723                                                   "as the preallocated buffer");
724   std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
725 }
726 
727 void OMPReductionClause::setInscanCopyOps(ArrayRef<Expr *> Ops) {
728   assert(Modifier == OMPC_REDUCTION_inscan && "Expected inscan reduction.");
729   assert(Ops.size() == varlist_size() && "Number of copy "
730                                          "expressions is not the same "
731                                          "as the preallocated buffer");
732   llvm::copy(Ops, getReductionOps().end());
733 }
734 
735 void OMPReductionClause::setInscanCopyArrayTemps(
736     ArrayRef<Expr *> CopyArrayTemps) {
737   assert(Modifier == OMPC_REDUCTION_inscan && "Expected inscan reduction.");
738   assert(CopyArrayTemps.size() == varlist_size() &&
739          "Number of copy temp expressions is not the same as the preallocated "
740          "buffer");
741   llvm::copy(CopyArrayTemps, getInscanCopyOps().end());
742 }
743 
744 void OMPReductionClause::setInscanCopyArrayElems(
745     ArrayRef<Expr *> CopyArrayElems) {
746   assert(Modifier == OMPC_REDUCTION_inscan && "Expected inscan reduction.");
747   assert(CopyArrayElems.size() == varlist_size() &&
748          "Number of copy temp expressions is not the same as the preallocated "
749          "buffer");
750   llvm::copy(CopyArrayElems, getInscanCopyArrayTemps().end());
751 }
752 
753 OMPReductionClause *OMPReductionClause::Create(
754     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
755     SourceLocation ModifierLoc, SourceLocation EndLoc, SourceLocation ColonLoc,
756     OpenMPReductionClauseModifier Modifier, ArrayRef<Expr *> VL,
757     NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
758     ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
759     ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps,
760     ArrayRef<Expr *> CopyOps, ArrayRef<Expr *> CopyArrayTemps,
761     ArrayRef<Expr *> CopyArrayElems, Stmt *PreInit, Expr *PostUpdate) {
762   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(
763       (Modifier == OMPC_REDUCTION_inscan ? 8 : 5) * VL.size()));
764   auto *Clause = new (Mem)
765       OMPReductionClause(StartLoc, LParenLoc, ModifierLoc, EndLoc, ColonLoc,
766                          Modifier, VL.size(), QualifierLoc, NameInfo);
767   Clause->setVarRefs(VL);
768   Clause->setPrivates(Privates);
769   Clause->setLHSExprs(LHSExprs);
770   Clause->setRHSExprs(RHSExprs);
771   Clause->setReductionOps(ReductionOps);
772   Clause->setPreInitStmt(PreInit);
773   Clause->setPostUpdateExpr(PostUpdate);
774   if (Modifier == OMPC_REDUCTION_inscan) {
775     Clause->setInscanCopyOps(CopyOps);
776     Clause->setInscanCopyArrayTemps(CopyArrayTemps);
777     Clause->setInscanCopyArrayElems(CopyArrayElems);
778   } else {
779     assert(CopyOps.empty() &&
780            "copy operations are expected in inscan reductions only.");
781     assert(CopyArrayTemps.empty() &&
782            "copy array temps are expected in inscan reductions only.");
783     assert(CopyArrayElems.empty() &&
784            "copy array temps are expected in inscan reductions only.");
785   }
786   return Clause;
787 }
788 
789 OMPReductionClause *
790 OMPReductionClause::CreateEmpty(const ASTContext &C, unsigned N,
791                                 OpenMPReductionClauseModifier Modifier) {
792   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(
793       (Modifier == OMPC_REDUCTION_inscan ? 8 : 5) * N));
794   auto *Clause = new (Mem) OMPReductionClause(N);
795   Clause->setModifier(Modifier);
796   return Clause;
797 }
798 
799 void OMPTaskReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
800   assert(Privates.size() == varlist_size() &&
801          "Number of private copies is not the same as the preallocated buffer");
802   std::copy(Privates.begin(), Privates.end(), varlist_end());
803 }
804 
805 void OMPTaskReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
806   assert(
807       LHSExprs.size() == varlist_size() &&
808       "Number of LHS expressions is not the same as the preallocated buffer");
809   std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
810 }
811 
812 void OMPTaskReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
813   assert(
814       RHSExprs.size() == varlist_size() &&
815       "Number of RHS expressions is not the same as the preallocated buffer");
816   std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
817 }
818 
819 void OMPTaskReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
820   assert(ReductionOps.size() == varlist_size() && "Number of task reduction "
821                                                   "expressions is not the same "
822                                                   "as the preallocated buffer");
823   std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
824 }
825 
826 OMPTaskReductionClause *OMPTaskReductionClause::Create(
827     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
828     SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
829     NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
830     ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
831     ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps, Stmt *PreInit,
832     Expr *PostUpdate) {
833   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
834   OMPTaskReductionClause *Clause = new (Mem) OMPTaskReductionClause(
835       StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
836   Clause->setVarRefs(VL);
837   Clause->setPrivates(Privates);
838   Clause->setLHSExprs(LHSExprs);
839   Clause->setRHSExprs(RHSExprs);
840   Clause->setReductionOps(ReductionOps);
841   Clause->setPreInitStmt(PreInit);
842   Clause->setPostUpdateExpr(PostUpdate);
843   return Clause;
844 }
845 
846 OMPTaskReductionClause *OMPTaskReductionClause::CreateEmpty(const ASTContext &C,
847                                                             unsigned N) {
848   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
849   return new (Mem) OMPTaskReductionClause(N);
850 }
851 
852 void OMPInReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
853   assert(Privates.size() == varlist_size() &&
854          "Number of private copies is not the same as the preallocated buffer");
855   std::copy(Privates.begin(), Privates.end(), varlist_end());
856 }
857 
858 void OMPInReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
859   assert(
860       LHSExprs.size() == varlist_size() &&
861       "Number of LHS expressions is not the same as the preallocated buffer");
862   std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
863 }
864 
865 void OMPInReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
866   assert(
867       RHSExprs.size() == varlist_size() &&
868       "Number of RHS expressions is not the same as the preallocated buffer");
869   std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
870 }
871 
872 void OMPInReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
873   assert(ReductionOps.size() == varlist_size() && "Number of in reduction "
874                                                   "expressions is not the same "
875                                                   "as the preallocated buffer");
876   std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
877 }
878 
879 void OMPInReductionClause::setTaskgroupDescriptors(
880     ArrayRef<Expr *> TaskgroupDescriptors) {
881   assert(TaskgroupDescriptors.size() == varlist_size() &&
882          "Number of in reduction descriptors is not the same as the "
883          "preallocated buffer");
884   std::copy(TaskgroupDescriptors.begin(), TaskgroupDescriptors.end(),
885             getReductionOps().end());
886 }
887 
888 OMPInReductionClause *OMPInReductionClause::Create(
889     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
890     SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
891     NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
892     ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
893     ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps,
894     ArrayRef<Expr *> TaskgroupDescriptors, Stmt *PreInit, Expr *PostUpdate) {
895   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(6 * VL.size()));
896   OMPInReductionClause *Clause = new (Mem) OMPInReductionClause(
897       StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
898   Clause->setVarRefs(VL);
899   Clause->setPrivates(Privates);
900   Clause->setLHSExprs(LHSExprs);
901   Clause->setRHSExprs(RHSExprs);
902   Clause->setReductionOps(ReductionOps);
903   Clause->setTaskgroupDescriptors(TaskgroupDescriptors);
904   Clause->setPreInitStmt(PreInit);
905   Clause->setPostUpdateExpr(PostUpdate);
906   return Clause;
907 }
908 
909 OMPInReductionClause *OMPInReductionClause::CreateEmpty(const ASTContext &C,
910                                                         unsigned N) {
911   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(6 * N));
912   return new (Mem) OMPInReductionClause(N);
913 }
914 
915 OMPSizesClause *OMPSizesClause::Create(const ASTContext &C,
916                                        SourceLocation StartLoc,
917                                        SourceLocation LParenLoc,
918                                        SourceLocation EndLoc,
919                                        ArrayRef<Expr *> Sizes) {
920   OMPSizesClause *Clause = CreateEmpty(C, Sizes.size());
921   Clause->setLocStart(StartLoc);
922   Clause->setLParenLoc(LParenLoc);
923   Clause->setLocEnd(EndLoc);
924   Clause->setSizesRefs(Sizes);
925   return Clause;
926 }
927 
928 OMPSizesClause *OMPSizesClause::CreateEmpty(const ASTContext &C,
929                                             unsigned NumSizes) {
930   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumSizes));
931   return new (Mem) OMPSizesClause(NumSizes);
932 }
933 
934 OMPAllocateClause *
935 OMPAllocateClause::Create(const ASTContext &C, SourceLocation StartLoc,
936                           SourceLocation LParenLoc, Expr *Allocator,
937                           SourceLocation ColonLoc, SourceLocation EndLoc,
938                           ArrayRef<Expr *> VL) {
939   // Allocate space for private variables and initializer expressions.
940   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size()));
941   auto *Clause = new (Mem) OMPAllocateClause(StartLoc, LParenLoc, Allocator,
942                                              ColonLoc, EndLoc, VL.size());
943   Clause->setVarRefs(VL);
944   return Clause;
945 }
946 
947 OMPAllocateClause *OMPAllocateClause::CreateEmpty(const ASTContext &C,
948                                                   unsigned N) {
949   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
950   return new (Mem) OMPAllocateClause(N);
951 }
952 
953 OMPFlushClause *OMPFlushClause::Create(const ASTContext &C,
954                                        SourceLocation StartLoc,
955                                        SourceLocation LParenLoc,
956                                        SourceLocation EndLoc,
957                                        ArrayRef<Expr *> VL) {
958   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
959   OMPFlushClause *Clause =
960       new (Mem) OMPFlushClause(StartLoc, LParenLoc, EndLoc, VL.size());
961   Clause->setVarRefs(VL);
962   return Clause;
963 }
964 
965 OMPFlushClause *OMPFlushClause::CreateEmpty(const ASTContext &C, unsigned N) {
966   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
967   return new (Mem) OMPFlushClause(N);
968 }
969 
970 OMPDepobjClause *OMPDepobjClause::Create(const ASTContext &C,
971                                          SourceLocation StartLoc,
972                                          SourceLocation LParenLoc,
973                                          SourceLocation RParenLoc,
974                                          Expr *Depobj) {
975   auto *Clause = new (C) OMPDepobjClause(StartLoc, LParenLoc, RParenLoc);
976   Clause->setDepobj(Depobj);
977   return Clause;
978 }
979 
980 OMPDepobjClause *OMPDepobjClause::CreateEmpty(const ASTContext &C) {
981   return new (C) OMPDepobjClause();
982 }
983 
984 OMPDependClause *
985 OMPDependClause::Create(const ASTContext &C, SourceLocation StartLoc,
986                         SourceLocation LParenLoc, SourceLocation EndLoc,
987                         Expr *DepModifier, OpenMPDependClauseKind DepKind,
988                         SourceLocation DepLoc, SourceLocation ColonLoc,
989                         ArrayRef<Expr *> VL, unsigned NumLoops) {
990   void *Mem = C.Allocate(
991       totalSizeToAlloc<Expr *>(VL.size() + /*depend-modifier*/ 1 + NumLoops),
992       alignof(OMPDependClause));
993   OMPDependClause *Clause = new (Mem)
994       OMPDependClause(StartLoc, LParenLoc, EndLoc, VL.size(), NumLoops);
995   Clause->setVarRefs(VL);
996   Clause->setDependencyKind(DepKind);
997   Clause->setDependencyLoc(DepLoc);
998   Clause->setColonLoc(ColonLoc);
999   Clause->setModifier(DepModifier);
1000   for (unsigned I = 0 ; I < NumLoops; ++I)
1001     Clause->setLoopData(I, nullptr);
1002   return Clause;
1003 }
1004 
1005 OMPDependClause *OMPDependClause::CreateEmpty(const ASTContext &C, unsigned N,
1006                                               unsigned NumLoops) {
1007   void *Mem =
1008       C.Allocate(totalSizeToAlloc<Expr *>(N + /*depend-modifier*/ 1 + NumLoops),
1009                  alignof(OMPDependClause));
1010   return new (Mem) OMPDependClause(N, NumLoops);
1011 }
1012 
1013 void OMPDependClause::setLoopData(unsigned NumLoop, Expr *Cnt) {
1014   assert((getDependencyKind() == OMPC_DEPEND_sink ||
1015           getDependencyKind() == OMPC_DEPEND_source) &&
1016          NumLoop < NumLoops &&
1017          "Expected sink or source depend + loop index must be less number of "
1018          "loops.");
1019   auto *It = std::next(getVarRefs().end(), NumLoop + 1);
1020   *It = Cnt;
1021 }
1022 
1023 Expr *OMPDependClause::getLoopData(unsigned NumLoop) {
1024   assert((getDependencyKind() == OMPC_DEPEND_sink ||
1025           getDependencyKind() == OMPC_DEPEND_source) &&
1026          NumLoop < NumLoops &&
1027          "Expected sink or source depend + loop index must be less number of "
1028          "loops.");
1029   auto *It = std::next(getVarRefs().end(), NumLoop + 1);
1030   return *It;
1031 }
1032 
1033 const Expr *OMPDependClause::getLoopData(unsigned NumLoop) const {
1034   assert((getDependencyKind() == OMPC_DEPEND_sink ||
1035           getDependencyKind() == OMPC_DEPEND_source) &&
1036          NumLoop < NumLoops &&
1037          "Expected sink or source depend + loop index must be less number of "
1038          "loops.");
1039   const auto *It = std::next(getVarRefs().end(), NumLoop + 1);
1040   return *It;
1041 }
1042 
1043 void OMPDependClause::setModifier(Expr *DepModifier) {
1044   *getVarRefs().end() = DepModifier;
1045 }
1046 Expr *OMPDependClause::getModifier() { return *getVarRefs().end(); }
1047 
1048 unsigned OMPClauseMappableExprCommon::getComponentsTotalNumber(
1049     MappableExprComponentListsRef ComponentLists) {
1050   unsigned TotalNum = 0u;
1051   for (auto &C : ComponentLists)
1052     TotalNum += C.size();
1053   return TotalNum;
1054 }
1055 
1056 unsigned OMPClauseMappableExprCommon::getUniqueDeclarationsTotalNumber(
1057     ArrayRef<const ValueDecl *> Declarations) {
1058   unsigned TotalNum = 0u;
1059   llvm::SmallPtrSet<const ValueDecl *, 8> Cache;
1060   for (const ValueDecl *D : Declarations) {
1061     const ValueDecl *VD = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr;
1062     if (Cache.count(VD))
1063       continue;
1064     ++TotalNum;
1065     Cache.insert(VD);
1066   }
1067   return TotalNum;
1068 }
1069 
1070 OMPMapClause *OMPMapClause::Create(
1071     const ASTContext &C, const OMPVarListLocTy &Locs, ArrayRef<Expr *> Vars,
1072     ArrayRef<ValueDecl *> Declarations,
1073     MappableExprComponentListsRef ComponentLists, ArrayRef<Expr *> UDMapperRefs,
1074     ArrayRef<OpenMPMapModifierKind> MapModifiers,
1075     ArrayRef<SourceLocation> MapModifiersLoc,
1076     NestedNameSpecifierLoc UDMQualifierLoc, DeclarationNameInfo MapperId,
1077     OpenMPMapClauseKind Type, bool TypeIsImplicit, SourceLocation TypeLoc) {
1078   OMPMappableExprListSizeTy Sizes;
1079   Sizes.NumVars = Vars.size();
1080   Sizes.NumUniqueDeclarations = getUniqueDeclarationsTotalNumber(Declarations);
1081   Sizes.NumComponentLists = ComponentLists.size();
1082   Sizes.NumComponents = getComponentsTotalNumber(ComponentLists);
1083 
1084   // We need to allocate:
1085   // 2 x NumVars x Expr* - we have an original list expression and an associated
1086   // user-defined mapper for each clause list entry.
1087   // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
1088   // with each component list.
1089   // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
1090   // number of lists for each unique declaration and the size of each component
1091   // list.
1092   // NumComponents x MappableComponent - the total of all the components in all
1093   // the lists.
1094   void *Mem = C.Allocate(
1095       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1096                        OMPClauseMappableExprCommon::MappableComponent>(
1097           2 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
1098           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1099           Sizes.NumComponents));
1100   OMPMapClause *Clause = new (Mem)
1101       OMPMapClause(MapModifiers, MapModifiersLoc, UDMQualifierLoc, MapperId,
1102                    Type, TypeIsImplicit, TypeLoc, Locs, Sizes);
1103 
1104   Clause->setVarRefs(Vars);
1105   Clause->setUDMapperRefs(UDMapperRefs);
1106   Clause->setClauseInfo(Declarations, ComponentLists);
1107   Clause->setMapType(Type);
1108   Clause->setMapLoc(TypeLoc);
1109   return Clause;
1110 }
1111 
1112 OMPMapClause *
1113 OMPMapClause::CreateEmpty(const ASTContext &C,
1114                           const OMPMappableExprListSizeTy &Sizes) {
1115   void *Mem = C.Allocate(
1116       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1117                        OMPClauseMappableExprCommon::MappableComponent>(
1118           2 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
1119           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1120           Sizes.NumComponents));
1121   return new (Mem) OMPMapClause(Sizes);
1122 }
1123 
1124 OMPToClause *OMPToClause::Create(
1125     const ASTContext &C, const OMPVarListLocTy &Locs, ArrayRef<Expr *> Vars,
1126     ArrayRef<ValueDecl *> Declarations,
1127     MappableExprComponentListsRef ComponentLists, ArrayRef<Expr *> UDMapperRefs,
1128     ArrayRef<OpenMPMotionModifierKind> MotionModifiers,
1129     ArrayRef<SourceLocation> MotionModifiersLoc,
1130     NestedNameSpecifierLoc UDMQualifierLoc, DeclarationNameInfo MapperId) {
1131   OMPMappableExprListSizeTy Sizes;
1132   Sizes.NumVars = Vars.size();
1133   Sizes.NumUniqueDeclarations = getUniqueDeclarationsTotalNumber(Declarations);
1134   Sizes.NumComponentLists = ComponentLists.size();
1135   Sizes.NumComponents = getComponentsTotalNumber(ComponentLists);
1136 
1137   // We need to allocate:
1138   // 2 x NumVars x Expr* - we have an original list expression and an associated
1139   // user-defined mapper for each clause list entry.
1140   // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
1141   // with each component list.
1142   // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
1143   // number of lists for each unique declaration and the size of each component
1144   // list.
1145   // NumComponents x MappableComponent - the total of all the components in all
1146   // the lists.
1147   void *Mem = C.Allocate(
1148       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1149                        OMPClauseMappableExprCommon::MappableComponent>(
1150           2 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
1151           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1152           Sizes.NumComponents));
1153 
1154   auto *Clause = new (Mem) OMPToClause(MotionModifiers, MotionModifiersLoc,
1155                                        UDMQualifierLoc, MapperId, Locs, Sizes);
1156 
1157   Clause->setVarRefs(Vars);
1158   Clause->setUDMapperRefs(UDMapperRefs);
1159   Clause->setClauseInfo(Declarations, ComponentLists);
1160   return Clause;
1161 }
1162 
1163 OMPToClause *OMPToClause::CreateEmpty(const ASTContext &C,
1164                                       const OMPMappableExprListSizeTy &Sizes) {
1165   void *Mem = C.Allocate(
1166       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1167                        OMPClauseMappableExprCommon::MappableComponent>(
1168           2 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
1169           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1170           Sizes.NumComponents));
1171   return new (Mem) OMPToClause(Sizes);
1172 }
1173 
1174 OMPFromClause *OMPFromClause::Create(
1175     const ASTContext &C, const OMPVarListLocTy &Locs, ArrayRef<Expr *> Vars,
1176     ArrayRef<ValueDecl *> Declarations,
1177     MappableExprComponentListsRef ComponentLists, ArrayRef<Expr *> UDMapperRefs,
1178     ArrayRef<OpenMPMotionModifierKind> MotionModifiers,
1179     ArrayRef<SourceLocation> MotionModifiersLoc,
1180     NestedNameSpecifierLoc UDMQualifierLoc, DeclarationNameInfo MapperId) {
1181   OMPMappableExprListSizeTy Sizes;
1182   Sizes.NumVars = Vars.size();
1183   Sizes.NumUniqueDeclarations = getUniqueDeclarationsTotalNumber(Declarations);
1184   Sizes.NumComponentLists = ComponentLists.size();
1185   Sizes.NumComponents = getComponentsTotalNumber(ComponentLists);
1186 
1187   // We need to allocate:
1188   // 2 x NumVars x Expr* - we have an original list expression and an associated
1189   // user-defined mapper for each clause list entry.
1190   // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
1191   // with each component list.
1192   // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
1193   // number of lists for each unique declaration and the size of each component
1194   // list.
1195   // NumComponents x MappableComponent - the total of all the components in all
1196   // the lists.
1197   void *Mem = C.Allocate(
1198       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1199                        OMPClauseMappableExprCommon::MappableComponent>(
1200           2 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
1201           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1202           Sizes.NumComponents));
1203 
1204   auto *Clause =
1205       new (Mem) OMPFromClause(MotionModifiers, MotionModifiersLoc,
1206                               UDMQualifierLoc, MapperId, Locs, Sizes);
1207 
1208   Clause->setVarRefs(Vars);
1209   Clause->setUDMapperRefs(UDMapperRefs);
1210   Clause->setClauseInfo(Declarations, ComponentLists);
1211   return Clause;
1212 }
1213 
1214 OMPFromClause *
1215 OMPFromClause::CreateEmpty(const ASTContext &C,
1216                            const OMPMappableExprListSizeTy &Sizes) {
1217   void *Mem = C.Allocate(
1218       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1219                        OMPClauseMappableExprCommon::MappableComponent>(
1220           2 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
1221           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1222           Sizes.NumComponents));
1223   return new (Mem) OMPFromClause(Sizes);
1224 }
1225 
1226 void OMPUseDevicePtrClause::setPrivateCopies(ArrayRef<Expr *> VL) {
1227   assert(VL.size() == varlist_size() &&
1228          "Number of private copies is not the same as the preallocated buffer");
1229   std::copy(VL.begin(), VL.end(), varlist_end());
1230 }
1231 
1232 void OMPUseDevicePtrClause::setInits(ArrayRef<Expr *> VL) {
1233   assert(VL.size() == varlist_size() &&
1234          "Number of inits is not the same as the preallocated buffer");
1235   std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
1236 }
1237 
1238 OMPUseDevicePtrClause *OMPUseDevicePtrClause::Create(
1239     const ASTContext &C, const OMPVarListLocTy &Locs, ArrayRef<Expr *> Vars,
1240     ArrayRef<Expr *> PrivateVars, ArrayRef<Expr *> Inits,
1241     ArrayRef<ValueDecl *> Declarations,
1242     MappableExprComponentListsRef ComponentLists) {
1243   OMPMappableExprListSizeTy Sizes;
1244   Sizes.NumVars = Vars.size();
1245   Sizes.NumUniqueDeclarations = getUniqueDeclarationsTotalNumber(Declarations);
1246   Sizes.NumComponentLists = ComponentLists.size();
1247   Sizes.NumComponents = getComponentsTotalNumber(ComponentLists);
1248 
1249   // We need to allocate:
1250   // NumVars x Expr* - we have an original list expression for each clause
1251   // list entry.
1252   // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
1253   // with each component list.
1254   // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
1255   // number of lists for each unique declaration and the size of each component
1256   // list.
1257   // NumComponents x MappableComponent - the total of all the components in all
1258   // the lists.
1259   void *Mem = C.Allocate(
1260       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1261                        OMPClauseMappableExprCommon::MappableComponent>(
1262           3 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
1263           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1264           Sizes.NumComponents));
1265 
1266   OMPUseDevicePtrClause *Clause = new (Mem) OMPUseDevicePtrClause(Locs, Sizes);
1267 
1268   Clause->setVarRefs(Vars);
1269   Clause->setPrivateCopies(PrivateVars);
1270   Clause->setInits(Inits);
1271   Clause->setClauseInfo(Declarations, ComponentLists);
1272   return Clause;
1273 }
1274 
1275 OMPUseDevicePtrClause *
1276 OMPUseDevicePtrClause::CreateEmpty(const ASTContext &C,
1277                                    const OMPMappableExprListSizeTy &Sizes) {
1278   void *Mem = C.Allocate(
1279       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1280                        OMPClauseMappableExprCommon::MappableComponent>(
1281           3 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
1282           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1283           Sizes.NumComponents));
1284   return new (Mem) OMPUseDevicePtrClause(Sizes);
1285 }
1286 
1287 OMPUseDeviceAddrClause *
1288 OMPUseDeviceAddrClause::Create(const ASTContext &C, const OMPVarListLocTy &Locs,
1289                                ArrayRef<Expr *> Vars,
1290                                ArrayRef<ValueDecl *> Declarations,
1291                                MappableExprComponentListsRef ComponentLists) {
1292   OMPMappableExprListSizeTy Sizes;
1293   Sizes.NumVars = Vars.size();
1294   Sizes.NumUniqueDeclarations = getUniqueDeclarationsTotalNumber(Declarations);
1295   Sizes.NumComponentLists = ComponentLists.size();
1296   Sizes.NumComponents = getComponentsTotalNumber(ComponentLists);
1297 
1298   // We need to allocate:
1299   // 3 x NumVars x Expr* - we have an original list expression for each clause
1300   // list entry and an equal number of private copies and inits.
1301   // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
1302   // with each component list.
1303   // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
1304   // number of lists for each unique declaration and the size of each component
1305   // list.
1306   // NumComponents x MappableComponent - the total of all the components in all
1307   // the lists.
1308   void *Mem = C.Allocate(
1309       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1310                        OMPClauseMappableExprCommon::MappableComponent>(
1311           Sizes.NumVars, Sizes.NumUniqueDeclarations,
1312           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1313           Sizes.NumComponents));
1314 
1315   auto *Clause = new (Mem) OMPUseDeviceAddrClause(Locs, Sizes);
1316 
1317   Clause->setVarRefs(Vars);
1318   Clause->setClauseInfo(Declarations, ComponentLists);
1319   return Clause;
1320 }
1321 
1322 OMPUseDeviceAddrClause *
1323 OMPUseDeviceAddrClause::CreateEmpty(const ASTContext &C,
1324                                     const OMPMappableExprListSizeTy &Sizes) {
1325   void *Mem = C.Allocate(
1326       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1327                        OMPClauseMappableExprCommon::MappableComponent>(
1328           Sizes.NumVars, Sizes.NumUniqueDeclarations,
1329           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1330           Sizes.NumComponents));
1331   return new (Mem) OMPUseDeviceAddrClause(Sizes);
1332 }
1333 
1334 OMPIsDevicePtrClause *
1335 OMPIsDevicePtrClause::Create(const ASTContext &C, const OMPVarListLocTy &Locs,
1336                              ArrayRef<Expr *> Vars,
1337                              ArrayRef<ValueDecl *> Declarations,
1338                              MappableExprComponentListsRef ComponentLists) {
1339   OMPMappableExprListSizeTy Sizes;
1340   Sizes.NumVars = Vars.size();
1341   Sizes.NumUniqueDeclarations = getUniqueDeclarationsTotalNumber(Declarations);
1342   Sizes.NumComponentLists = ComponentLists.size();
1343   Sizes.NumComponents = getComponentsTotalNumber(ComponentLists);
1344 
1345   // We need to allocate:
1346   // NumVars x Expr* - we have an original list expression for each clause list
1347   // entry.
1348   // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
1349   // with each component list.
1350   // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
1351   // number of lists for each unique declaration and the size of each component
1352   // list.
1353   // NumComponents x MappableComponent - the total of all the components in all
1354   // the lists.
1355   void *Mem = C.Allocate(
1356       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1357                        OMPClauseMappableExprCommon::MappableComponent>(
1358           Sizes.NumVars, Sizes.NumUniqueDeclarations,
1359           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1360           Sizes.NumComponents));
1361 
1362   OMPIsDevicePtrClause *Clause = new (Mem) OMPIsDevicePtrClause(Locs, Sizes);
1363 
1364   Clause->setVarRefs(Vars);
1365   Clause->setClauseInfo(Declarations, ComponentLists);
1366   return Clause;
1367 }
1368 
1369 OMPIsDevicePtrClause *
1370 OMPIsDevicePtrClause::CreateEmpty(const ASTContext &C,
1371                                   const OMPMappableExprListSizeTy &Sizes) {
1372   void *Mem = C.Allocate(
1373       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1374                        OMPClauseMappableExprCommon::MappableComponent>(
1375           Sizes.NumVars, Sizes.NumUniqueDeclarations,
1376           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1377           Sizes.NumComponents));
1378   return new (Mem) OMPIsDevicePtrClause(Sizes);
1379 }
1380 
1381 OMPNontemporalClause *OMPNontemporalClause::Create(const ASTContext &C,
1382                                                    SourceLocation StartLoc,
1383                                                    SourceLocation LParenLoc,
1384                                                    SourceLocation EndLoc,
1385                                                    ArrayRef<Expr *> VL) {
1386   // Allocate space for nontemporal variables + private references.
1387   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * VL.size()));
1388   auto *Clause =
1389       new (Mem) OMPNontemporalClause(StartLoc, LParenLoc, EndLoc, VL.size());
1390   Clause->setVarRefs(VL);
1391   return Clause;
1392 }
1393 
1394 OMPNontemporalClause *OMPNontemporalClause::CreateEmpty(const ASTContext &C,
1395                                                         unsigned N) {
1396   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * N));
1397   return new (Mem) OMPNontemporalClause(N);
1398 }
1399 
1400 void OMPNontemporalClause::setPrivateRefs(ArrayRef<Expr *> VL) {
1401   assert(VL.size() == varlist_size() && "Number of private references is not "
1402                                         "the same as the preallocated buffer");
1403   std::copy(VL.begin(), VL.end(), varlist_end());
1404 }
1405 
1406 OMPInclusiveClause *OMPInclusiveClause::Create(const ASTContext &C,
1407                                                SourceLocation StartLoc,
1408                                                SourceLocation LParenLoc,
1409                                                SourceLocation EndLoc,
1410                                                ArrayRef<Expr *> VL) {
1411   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size()));
1412   auto *Clause =
1413       new (Mem) OMPInclusiveClause(StartLoc, LParenLoc, EndLoc, VL.size());
1414   Clause->setVarRefs(VL);
1415   return Clause;
1416 }
1417 
1418 OMPInclusiveClause *OMPInclusiveClause::CreateEmpty(const ASTContext &C,
1419                                                     unsigned N) {
1420   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
1421   return new (Mem) OMPInclusiveClause(N);
1422 }
1423 
1424 OMPExclusiveClause *OMPExclusiveClause::Create(const ASTContext &C,
1425                                                SourceLocation StartLoc,
1426                                                SourceLocation LParenLoc,
1427                                                SourceLocation EndLoc,
1428                                                ArrayRef<Expr *> VL) {
1429   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size()));
1430   auto *Clause =
1431       new (Mem) OMPExclusiveClause(StartLoc, LParenLoc, EndLoc, VL.size());
1432   Clause->setVarRefs(VL);
1433   return Clause;
1434 }
1435 
1436 OMPExclusiveClause *OMPExclusiveClause::CreateEmpty(const ASTContext &C,
1437                                                     unsigned N) {
1438   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
1439   return new (Mem) OMPExclusiveClause(N);
1440 }
1441 
1442 void OMPUsesAllocatorsClause::setAllocatorsData(
1443     ArrayRef<OMPUsesAllocatorsClause::Data> Data) {
1444   assert(Data.size() == NumOfAllocators &&
1445          "Size of allocators data is not the same as the preallocated buffer.");
1446   for (unsigned I = 0, E = Data.size(); I < E; ++I) {
1447     const OMPUsesAllocatorsClause::Data &D = Data[I];
1448     getTrailingObjects<Expr *>()[I * static_cast<int>(ExprOffsets::Total) +
1449                                  static_cast<int>(ExprOffsets::Allocator)] =
1450         D.Allocator;
1451     getTrailingObjects<Expr *>()[I * static_cast<int>(ExprOffsets::Total) +
1452                                  static_cast<int>(
1453                                      ExprOffsets::AllocatorTraits)] =
1454         D.AllocatorTraits;
1455     getTrailingObjects<
1456         SourceLocation>()[I * static_cast<int>(ParenLocsOffsets::Total) +
1457                           static_cast<int>(ParenLocsOffsets::LParen)] =
1458         D.LParenLoc;
1459     getTrailingObjects<
1460         SourceLocation>()[I * static_cast<int>(ParenLocsOffsets::Total) +
1461                           static_cast<int>(ParenLocsOffsets::RParen)] =
1462         D.RParenLoc;
1463   }
1464 }
1465 
1466 OMPUsesAllocatorsClause::Data
1467 OMPUsesAllocatorsClause::getAllocatorData(unsigned I) const {
1468   OMPUsesAllocatorsClause::Data Data;
1469   Data.Allocator =
1470       getTrailingObjects<Expr *>()[I * static_cast<int>(ExprOffsets::Total) +
1471                                    static_cast<int>(ExprOffsets::Allocator)];
1472   Data.AllocatorTraits =
1473       getTrailingObjects<Expr *>()[I * static_cast<int>(ExprOffsets::Total) +
1474                                    static_cast<int>(
1475                                        ExprOffsets::AllocatorTraits)];
1476   Data.LParenLoc = getTrailingObjects<
1477       SourceLocation>()[I * static_cast<int>(ParenLocsOffsets::Total) +
1478                         static_cast<int>(ParenLocsOffsets::LParen)];
1479   Data.RParenLoc = getTrailingObjects<
1480       SourceLocation>()[I * static_cast<int>(ParenLocsOffsets::Total) +
1481                         static_cast<int>(ParenLocsOffsets::RParen)];
1482   return Data;
1483 }
1484 
1485 OMPUsesAllocatorsClause *
1486 OMPUsesAllocatorsClause::Create(const ASTContext &C, SourceLocation StartLoc,
1487                                 SourceLocation LParenLoc, SourceLocation EndLoc,
1488                                 ArrayRef<OMPUsesAllocatorsClause::Data> Data) {
1489   void *Mem = C.Allocate(totalSizeToAlloc<Expr *, SourceLocation>(
1490       static_cast<int>(ExprOffsets::Total) * Data.size(),
1491       static_cast<int>(ParenLocsOffsets::Total) * Data.size()));
1492   auto *Clause = new (Mem)
1493       OMPUsesAllocatorsClause(StartLoc, LParenLoc, EndLoc, Data.size());
1494   Clause->setAllocatorsData(Data);
1495   return Clause;
1496 }
1497 
1498 OMPUsesAllocatorsClause *
1499 OMPUsesAllocatorsClause::CreateEmpty(const ASTContext &C, unsigned N) {
1500   void *Mem = C.Allocate(totalSizeToAlloc<Expr *, SourceLocation>(
1501       static_cast<int>(ExprOffsets::Total) * N,
1502       static_cast<int>(ParenLocsOffsets::Total) * N));
1503   return new (Mem) OMPUsesAllocatorsClause(N);
1504 }
1505 
1506 OMPAffinityClause *
1507 OMPAffinityClause::Create(const ASTContext &C, SourceLocation StartLoc,
1508                           SourceLocation LParenLoc, SourceLocation ColonLoc,
1509                           SourceLocation EndLoc, Expr *Modifier,
1510                           ArrayRef<Expr *> Locators) {
1511   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(Locators.size() + 1));
1512   auto *Clause = new (Mem)
1513       OMPAffinityClause(StartLoc, LParenLoc, ColonLoc, EndLoc, Locators.size());
1514   Clause->setModifier(Modifier);
1515   Clause->setVarRefs(Locators);
1516   return Clause;
1517 }
1518 
1519 OMPAffinityClause *OMPAffinityClause::CreateEmpty(const ASTContext &C,
1520                                                   unsigned N) {
1521   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N + 1));
1522   return new (Mem) OMPAffinityClause(N);
1523 }
1524 
1525 OMPInitClause *OMPInitClause::Create(const ASTContext &C, Expr *InteropVar,
1526                                      ArrayRef<Expr *> PrefExprs, bool IsTarget,
1527                                      bool IsTargetSync, SourceLocation StartLoc,
1528                                      SourceLocation LParenLoc,
1529                                      SourceLocation VarLoc,
1530                                      SourceLocation EndLoc) {
1531 
1532   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(PrefExprs.size() + 1));
1533   auto *Clause =
1534       new (Mem) OMPInitClause(IsTarget, IsTargetSync, StartLoc, LParenLoc,
1535                               VarLoc, EndLoc, PrefExprs.size() + 1);
1536   Clause->setInteropVar(InteropVar);
1537   llvm::copy(PrefExprs, Clause->getTrailingObjects<Expr *>() + 1);
1538   return Clause;
1539 }
1540 
1541 OMPInitClause *OMPInitClause::CreateEmpty(const ASTContext &C, unsigned N) {
1542   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
1543   return new (Mem) OMPInitClause(N);
1544 }
1545 
1546 //===----------------------------------------------------------------------===//
1547 //  OpenMP clauses printing methods
1548 //===----------------------------------------------------------------------===//
1549 
1550 void OMPClausePrinter::VisitOMPIfClause(OMPIfClause *Node) {
1551   OS << "if(";
1552   if (Node->getNameModifier() != OMPD_unknown)
1553     OS << getOpenMPDirectiveName(Node->getNameModifier()) << ": ";
1554   Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
1555   OS << ")";
1556 }
1557 
1558 void OMPClausePrinter::VisitOMPFinalClause(OMPFinalClause *Node) {
1559   OS << "final(";
1560   Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
1561   OS << ")";
1562 }
1563 
1564 void OMPClausePrinter::VisitOMPNumThreadsClause(OMPNumThreadsClause *Node) {
1565   OS << "num_threads(";
1566   Node->getNumThreads()->printPretty(OS, nullptr, Policy, 0);
1567   OS << ")";
1568 }
1569 
1570 void OMPClausePrinter::VisitOMPSafelenClause(OMPSafelenClause *Node) {
1571   OS << "safelen(";
1572   Node->getSafelen()->printPretty(OS, nullptr, Policy, 0);
1573   OS << ")";
1574 }
1575 
1576 void OMPClausePrinter::VisitOMPSimdlenClause(OMPSimdlenClause *Node) {
1577   OS << "simdlen(";
1578   Node->getSimdlen()->printPretty(OS, nullptr, Policy, 0);
1579   OS << ")";
1580 }
1581 
1582 void OMPClausePrinter::VisitOMPSizesClause(OMPSizesClause *Node) {
1583   OS << "sizes(";
1584   bool First = true;
1585   for (auto Size : Node->getSizesRefs()) {
1586     if (!First)
1587       OS << ", ";
1588     Size->printPretty(OS, nullptr, Policy, 0);
1589     First = false;
1590   }
1591   OS << ")";
1592 }
1593 
1594 void OMPClausePrinter::VisitOMPAllocatorClause(OMPAllocatorClause *Node) {
1595   OS << "allocator(";
1596   Node->getAllocator()->printPretty(OS, nullptr, Policy, 0);
1597   OS << ")";
1598 }
1599 
1600 void OMPClausePrinter::VisitOMPCollapseClause(OMPCollapseClause *Node) {
1601   OS << "collapse(";
1602   Node->getNumForLoops()->printPretty(OS, nullptr, Policy, 0);
1603   OS << ")";
1604 }
1605 
1606 void OMPClausePrinter::VisitOMPDetachClause(OMPDetachClause *Node) {
1607   OS << "detach(";
1608   Node->getEventHandler()->printPretty(OS, nullptr, Policy, 0);
1609   OS << ")";
1610 }
1611 
1612 void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) {
1613   OS << "default("
1614      << getOpenMPSimpleClauseTypeName(OMPC_default,
1615                                       unsigned(Node->getDefaultKind()))
1616      << ")";
1617 }
1618 
1619 void OMPClausePrinter::VisitOMPProcBindClause(OMPProcBindClause *Node) {
1620   OS << "proc_bind("
1621      << getOpenMPSimpleClauseTypeName(OMPC_proc_bind,
1622                                       unsigned(Node->getProcBindKind()))
1623      << ")";
1624 }
1625 
1626 void OMPClausePrinter::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {
1627   OS << "unified_address";
1628 }
1629 
1630 void OMPClausePrinter::VisitOMPUnifiedSharedMemoryClause(
1631     OMPUnifiedSharedMemoryClause *) {
1632   OS << "unified_shared_memory";
1633 }
1634 
1635 void OMPClausePrinter::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {
1636   OS << "reverse_offload";
1637 }
1638 
1639 void OMPClausePrinter::VisitOMPDynamicAllocatorsClause(
1640     OMPDynamicAllocatorsClause *) {
1641   OS << "dynamic_allocators";
1642 }
1643 
1644 void OMPClausePrinter::VisitOMPAtomicDefaultMemOrderClause(
1645     OMPAtomicDefaultMemOrderClause *Node) {
1646   OS << "atomic_default_mem_order("
1647      << getOpenMPSimpleClauseTypeName(OMPC_atomic_default_mem_order,
1648                                       Node->getAtomicDefaultMemOrderKind())
1649      << ")";
1650 }
1651 
1652 void OMPClausePrinter::VisitOMPScheduleClause(OMPScheduleClause *Node) {
1653   OS << "schedule(";
1654   if (Node->getFirstScheduleModifier() != OMPC_SCHEDULE_MODIFIER_unknown) {
1655     OS << getOpenMPSimpleClauseTypeName(OMPC_schedule,
1656                                         Node->getFirstScheduleModifier());
1657     if (Node->getSecondScheduleModifier() != OMPC_SCHEDULE_MODIFIER_unknown) {
1658       OS << ", ";
1659       OS << getOpenMPSimpleClauseTypeName(OMPC_schedule,
1660                                           Node->getSecondScheduleModifier());
1661     }
1662     OS << ": ";
1663   }
1664   OS << getOpenMPSimpleClauseTypeName(OMPC_schedule, Node->getScheduleKind());
1665   if (auto *E = Node->getChunkSize()) {
1666     OS << ", ";
1667     E->printPretty(OS, nullptr, Policy);
1668   }
1669   OS << ")";
1670 }
1671 
1672 void OMPClausePrinter::VisitOMPOrderedClause(OMPOrderedClause *Node) {
1673   OS << "ordered";
1674   if (auto *Num = Node->getNumForLoops()) {
1675     OS << "(";
1676     Num->printPretty(OS, nullptr, Policy, 0);
1677     OS << ")";
1678   }
1679 }
1680 
1681 void OMPClausePrinter::VisitOMPNowaitClause(OMPNowaitClause *) {
1682   OS << "nowait";
1683 }
1684 
1685 void OMPClausePrinter::VisitOMPUntiedClause(OMPUntiedClause *) {
1686   OS << "untied";
1687 }
1688 
1689 void OMPClausePrinter::VisitOMPNogroupClause(OMPNogroupClause *) {
1690   OS << "nogroup";
1691 }
1692 
1693 void OMPClausePrinter::VisitOMPMergeableClause(OMPMergeableClause *) {
1694   OS << "mergeable";
1695 }
1696 
1697 void OMPClausePrinter::VisitOMPReadClause(OMPReadClause *) { OS << "read"; }
1698 
1699 void OMPClausePrinter::VisitOMPWriteClause(OMPWriteClause *) { OS << "write"; }
1700 
1701 void OMPClausePrinter::VisitOMPUpdateClause(OMPUpdateClause *Node) {
1702   OS << "update";
1703   if (Node->isExtended()) {
1704     OS << "(";
1705     OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(),
1706                                         Node->getDependencyKind());
1707     OS << ")";
1708   }
1709 }
1710 
1711 void OMPClausePrinter::VisitOMPCaptureClause(OMPCaptureClause *) {
1712   OS << "capture";
1713 }
1714 
1715 void OMPClausePrinter::VisitOMPSeqCstClause(OMPSeqCstClause *) {
1716   OS << "seq_cst";
1717 }
1718 
1719 void OMPClausePrinter::VisitOMPAcqRelClause(OMPAcqRelClause *) {
1720   OS << "acq_rel";
1721 }
1722 
1723 void OMPClausePrinter::VisitOMPAcquireClause(OMPAcquireClause *) {
1724   OS << "acquire";
1725 }
1726 
1727 void OMPClausePrinter::VisitOMPReleaseClause(OMPReleaseClause *) {
1728   OS << "release";
1729 }
1730 
1731 void OMPClausePrinter::VisitOMPRelaxedClause(OMPRelaxedClause *) {
1732   OS << "relaxed";
1733 }
1734 
1735 void OMPClausePrinter::VisitOMPThreadsClause(OMPThreadsClause *) {
1736   OS << "threads";
1737 }
1738 
1739 void OMPClausePrinter::VisitOMPSIMDClause(OMPSIMDClause *) { OS << "simd"; }
1740 
1741 void OMPClausePrinter::VisitOMPDeviceClause(OMPDeviceClause *Node) {
1742   OS << "device(";
1743   OpenMPDeviceClauseModifier Modifier = Node->getModifier();
1744   if (Modifier != OMPC_DEVICE_unknown) {
1745     OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(), Modifier)
1746        << ": ";
1747   }
1748   Node->getDevice()->printPretty(OS, nullptr, Policy, 0);
1749   OS << ")";
1750 }
1751 
1752 void OMPClausePrinter::VisitOMPNumTeamsClause(OMPNumTeamsClause *Node) {
1753   OS << "num_teams(";
1754   Node->getNumTeams()->printPretty(OS, nullptr, Policy, 0);
1755   OS << ")";
1756 }
1757 
1758 void OMPClausePrinter::VisitOMPThreadLimitClause(OMPThreadLimitClause *Node) {
1759   OS << "thread_limit(";
1760   Node->getThreadLimit()->printPretty(OS, nullptr, Policy, 0);
1761   OS << ")";
1762 }
1763 
1764 void OMPClausePrinter::VisitOMPPriorityClause(OMPPriorityClause *Node) {
1765   OS << "priority(";
1766   Node->getPriority()->printPretty(OS, nullptr, Policy, 0);
1767   OS << ")";
1768 }
1769 
1770 void OMPClausePrinter::VisitOMPGrainsizeClause(OMPGrainsizeClause *Node) {
1771   OS << "grainsize(";
1772   Node->getGrainsize()->printPretty(OS, nullptr, Policy, 0);
1773   OS << ")";
1774 }
1775 
1776 void OMPClausePrinter::VisitOMPNumTasksClause(OMPNumTasksClause *Node) {
1777   OS << "num_tasks(";
1778   Node->getNumTasks()->printPretty(OS, nullptr, Policy, 0);
1779   OS << ")";
1780 }
1781 
1782 void OMPClausePrinter::VisitOMPHintClause(OMPHintClause *Node) {
1783   OS << "hint(";
1784   Node->getHint()->printPretty(OS, nullptr, Policy, 0);
1785   OS << ")";
1786 }
1787 
1788 void OMPClausePrinter::VisitOMPInitClause(OMPInitClause *Node) {
1789   OS << "init(";
1790   bool First = true;
1791   for (const Expr *E : Node->prefs()) {
1792     if (First)
1793       OS << "prefer_type(";
1794     else
1795       OS << ",";
1796     E->printPretty(OS, nullptr, Policy);
1797     First = false;
1798   }
1799   if (!First)
1800     OS << "), ";
1801   if (Node->getIsTarget())
1802     OS << "target";
1803   if (Node->getIsTargetSync()) {
1804     if (Node->getIsTarget())
1805       OS << ", ";
1806     OS << "targetsync";
1807   }
1808   OS << " : ";
1809   Node->getInteropVar()->printPretty(OS, nullptr, Policy);
1810   OS << ")";
1811 }
1812 
1813 void OMPClausePrinter::VisitOMPUseClause(OMPUseClause *Node) {
1814   OS << "use(";
1815   Node->getInteropVar()->printPretty(OS, nullptr, Policy);
1816   OS << ")";
1817 }
1818 
1819 void OMPClausePrinter::VisitOMPDestroyClause(OMPDestroyClause *Node) {
1820   OS << "destroy";
1821   if (Expr *E = Node->getInteropVar()) {
1822     OS << "(";
1823     E->printPretty(OS, nullptr, Policy);
1824     OS << ")";
1825   }
1826 }
1827 
1828 void OMPClausePrinter::VisitOMPNovariantsClause(OMPNovariantsClause *Node) {
1829   OS << "novariants";
1830   if (Expr *E = Node->getCondition()) {
1831     OS << "(";
1832     E->printPretty(OS, nullptr, Policy, 0);
1833     OS << ")";
1834   }
1835 }
1836 
1837 template<typename T>
1838 void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
1839   for (typename T::varlist_iterator I = Node->varlist_begin(),
1840                                     E = Node->varlist_end();
1841        I != E; ++I) {
1842     assert(*I && "Expected non-null Stmt");
1843     OS << (I == Node->varlist_begin() ? StartSym : ',');
1844     if (auto *DRE = dyn_cast<DeclRefExpr>(*I)) {
1845       if (isa<OMPCapturedExprDecl>(DRE->getDecl()))
1846         DRE->printPretty(OS, nullptr, Policy, 0);
1847       else
1848         DRE->getDecl()->printQualifiedName(OS);
1849     } else
1850       (*I)->printPretty(OS, nullptr, Policy, 0);
1851   }
1852 }
1853 
1854 void OMPClausePrinter::VisitOMPAllocateClause(OMPAllocateClause *Node) {
1855   if (Node->varlist_empty())
1856     return;
1857   OS << "allocate";
1858   if (Expr *Allocator = Node->getAllocator()) {
1859     OS << "(";
1860     Allocator->printPretty(OS, nullptr, Policy, 0);
1861     OS << ":";
1862     VisitOMPClauseList(Node, ' ');
1863   } else {
1864     VisitOMPClauseList(Node, '(');
1865   }
1866   OS << ")";
1867 }
1868 
1869 void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) {
1870   if (!Node->varlist_empty()) {
1871     OS << "private";
1872     VisitOMPClauseList(Node, '(');
1873     OS << ")";
1874   }
1875 }
1876 
1877 void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) {
1878   if (!Node->varlist_empty()) {
1879     OS << "firstprivate";
1880     VisitOMPClauseList(Node, '(');
1881     OS << ")";
1882   }
1883 }
1884 
1885 void OMPClausePrinter::VisitOMPLastprivateClause(OMPLastprivateClause *Node) {
1886   if (!Node->varlist_empty()) {
1887     OS << "lastprivate";
1888     OpenMPLastprivateModifier LPKind = Node->getKind();
1889     if (LPKind != OMPC_LASTPRIVATE_unknown) {
1890       OS << "("
1891          << getOpenMPSimpleClauseTypeName(OMPC_lastprivate, Node->getKind())
1892          << ":";
1893     }
1894     VisitOMPClauseList(Node, LPKind == OMPC_LASTPRIVATE_unknown ? '(' : ' ');
1895     OS << ")";
1896   }
1897 }
1898 
1899 void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) {
1900   if (!Node->varlist_empty()) {
1901     OS << "shared";
1902     VisitOMPClauseList(Node, '(');
1903     OS << ")";
1904   }
1905 }
1906 
1907 void OMPClausePrinter::VisitOMPReductionClause(OMPReductionClause *Node) {
1908   if (!Node->varlist_empty()) {
1909     OS << "reduction(";
1910     if (Node->getModifierLoc().isValid())
1911       OS << getOpenMPSimpleClauseTypeName(OMPC_reduction, Node->getModifier())
1912          << ", ";
1913     NestedNameSpecifier *QualifierLoc =
1914         Node->getQualifierLoc().getNestedNameSpecifier();
1915     OverloadedOperatorKind OOK =
1916         Node->getNameInfo().getName().getCXXOverloadedOperator();
1917     if (QualifierLoc == nullptr && OOK != OO_None) {
1918       // Print reduction identifier in C format
1919       OS << getOperatorSpelling(OOK);
1920     } else {
1921       // Use C++ format
1922       if (QualifierLoc != nullptr)
1923         QualifierLoc->print(OS, Policy);
1924       OS << Node->getNameInfo();
1925     }
1926     OS << ":";
1927     VisitOMPClauseList(Node, ' ');
1928     OS << ")";
1929   }
1930 }
1931 
1932 void OMPClausePrinter::VisitOMPTaskReductionClause(
1933     OMPTaskReductionClause *Node) {
1934   if (!Node->varlist_empty()) {
1935     OS << "task_reduction(";
1936     NestedNameSpecifier *QualifierLoc =
1937         Node->getQualifierLoc().getNestedNameSpecifier();
1938     OverloadedOperatorKind OOK =
1939         Node->getNameInfo().getName().getCXXOverloadedOperator();
1940     if (QualifierLoc == nullptr && OOK != OO_None) {
1941       // Print reduction identifier in C format
1942       OS << getOperatorSpelling(OOK);
1943     } else {
1944       // Use C++ format
1945       if (QualifierLoc != nullptr)
1946         QualifierLoc->print(OS, Policy);
1947       OS << Node->getNameInfo();
1948     }
1949     OS << ":";
1950     VisitOMPClauseList(Node, ' ');
1951     OS << ")";
1952   }
1953 }
1954 
1955 void OMPClausePrinter::VisitOMPInReductionClause(OMPInReductionClause *Node) {
1956   if (!Node->varlist_empty()) {
1957     OS << "in_reduction(";
1958     NestedNameSpecifier *QualifierLoc =
1959         Node->getQualifierLoc().getNestedNameSpecifier();
1960     OverloadedOperatorKind OOK =
1961         Node->getNameInfo().getName().getCXXOverloadedOperator();
1962     if (QualifierLoc == nullptr && OOK != OO_None) {
1963       // Print reduction identifier in C format
1964       OS << getOperatorSpelling(OOK);
1965     } else {
1966       // Use C++ format
1967       if (QualifierLoc != nullptr)
1968         QualifierLoc->print(OS, Policy);
1969       OS << Node->getNameInfo();
1970     }
1971     OS << ":";
1972     VisitOMPClauseList(Node, ' ');
1973     OS << ")";
1974   }
1975 }
1976 
1977 void OMPClausePrinter::VisitOMPLinearClause(OMPLinearClause *Node) {
1978   if (!Node->varlist_empty()) {
1979     OS << "linear";
1980     if (Node->getModifierLoc().isValid()) {
1981       OS << '('
1982          << getOpenMPSimpleClauseTypeName(OMPC_linear, Node->getModifier());
1983     }
1984     VisitOMPClauseList(Node, '(');
1985     if (Node->getModifierLoc().isValid())
1986       OS << ')';
1987     if (Node->getStep() != nullptr) {
1988       OS << ": ";
1989       Node->getStep()->printPretty(OS, nullptr, Policy, 0);
1990     }
1991     OS << ")";
1992   }
1993 }
1994 
1995 void OMPClausePrinter::VisitOMPAlignedClause(OMPAlignedClause *Node) {
1996   if (!Node->varlist_empty()) {
1997     OS << "aligned";
1998     VisitOMPClauseList(Node, '(');
1999     if (Node->getAlignment() != nullptr) {
2000       OS << ": ";
2001       Node->getAlignment()->printPretty(OS, nullptr, Policy, 0);
2002     }
2003     OS << ")";
2004   }
2005 }
2006 
2007 void OMPClausePrinter::VisitOMPCopyinClause(OMPCopyinClause *Node) {
2008   if (!Node->varlist_empty()) {
2009     OS << "copyin";
2010     VisitOMPClauseList(Node, '(');
2011     OS << ")";
2012   }
2013 }
2014 
2015 void OMPClausePrinter::VisitOMPCopyprivateClause(OMPCopyprivateClause *Node) {
2016   if (!Node->varlist_empty()) {
2017     OS << "copyprivate";
2018     VisitOMPClauseList(Node, '(');
2019     OS << ")";
2020   }
2021 }
2022 
2023 void OMPClausePrinter::VisitOMPFlushClause(OMPFlushClause *Node) {
2024   if (!Node->varlist_empty()) {
2025     VisitOMPClauseList(Node, '(');
2026     OS << ")";
2027   }
2028 }
2029 
2030 void OMPClausePrinter::VisitOMPDepobjClause(OMPDepobjClause *Node) {
2031   OS << "(";
2032   Node->getDepobj()->printPretty(OS, nullptr, Policy, 0);
2033   OS << ")";
2034 }
2035 
2036 void OMPClausePrinter::VisitOMPDependClause(OMPDependClause *Node) {
2037   OS << "depend(";
2038   if (Expr *DepModifier = Node->getModifier()) {
2039     DepModifier->printPretty(OS, nullptr, Policy);
2040     OS << ", ";
2041   }
2042   OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(),
2043                                       Node->getDependencyKind());
2044   if (!Node->varlist_empty()) {
2045     OS << " :";
2046     VisitOMPClauseList(Node, ' ');
2047   }
2048   OS << ")";
2049 }
2050 
2051 template <typename T>
2052 static void PrintMapper(raw_ostream &OS, T *Node,
2053                         const PrintingPolicy &Policy) {
2054   OS << '(';
2055   NestedNameSpecifier *MapperNNS =
2056       Node->getMapperQualifierLoc().getNestedNameSpecifier();
2057   if (MapperNNS)
2058     MapperNNS->print(OS, Policy);
2059   OS << Node->getMapperIdInfo() << ')';
2060 }
2061 
2062 void OMPClausePrinter::VisitOMPMapClause(OMPMapClause *Node) {
2063   if (!Node->varlist_empty()) {
2064     OS << "map(";
2065     if (Node->getMapType() != OMPC_MAP_unknown) {
2066       for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
2067         if (Node->getMapTypeModifier(I) != OMPC_MAP_MODIFIER_unknown) {
2068           OS << getOpenMPSimpleClauseTypeName(OMPC_map,
2069                                               Node->getMapTypeModifier(I));
2070           if (Node->getMapTypeModifier(I) == OMPC_MAP_MODIFIER_mapper)
2071             PrintMapper(OS, Node, Policy);
2072           OS << ',';
2073         }
2074       }
2075       OS << getOpenMPSimpleClauseTypeName(OMPC_map, Node->getMapType());
2076       OS << ':';
2077     }
2078     VisitOMPClauseList(Node, ' ');
2079     OS << ")";
2080   }
2081 }
2082 
2083 template <typename T> void OMPClausePrinter::VisitOMPMotionClause(T *Node) {
2084   if (Node->varlist_empty())
2085     return;
2086   OS << getOpenMPClauseName(Node->getClauseKind());
2087   unsigned ModifierCount = 0;
2088   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
2089     if (Node->getMotionModifier(I) != OMPC_MOTION_MODIFIER_unknown)
2090       ++ModifierCount;
2091   }
2092   if (ModifierCount) {
2093     OS << '(';
2094     for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
2095       if (Node->getMotionModifier(I) != OMPC_MOTION_MODIFIER_unknown) {
2096         OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(),
2097                                             Node->getMotionModifier(I));
2098         if (Node->getMotionModifier(I) == OMPC_MOTION_MODIFIER_mapper)
2099           PrintMapper(OS, Node, Policy);
2100         if (I < ModifierCount - 1)
2101           OS << ", ";
2102       }
2103     }
2104     OS << ':';
2105     VisitOMPClauseList(Node, ' ');
2106   } else {
2107     VisitOMPClauseList(Node, '(');
2108   }
2109   OS << ")";
2110 }
2111 
2112 void OMPClausePrinter::VisitOMPToClause(OMPToClause *Node) {
2113   VisitOMPMotionClause(Node);
2114 }
2115 
2116 void OMPClausePrinter::VisitOMPFromClause(OMPFromClause *Node) {
2117   VisitOMPMotionClause(Node);
2118 }
2119 
2120 void OMPClausePrinter::VisitOMPDistScheduleClause(OMPDistScheduleClause *Node) {
2121   OS << "dist_schedule(" << getOpenMPSimpleClauseTypeName(
2122                            OMPC_dist_schedule, Node->getDistScheduleKind());
2123   if (auto *E = Node->getChunkSize()) {
2124     OS << ", ";
2125     E->printPretty(OS, nullptr, Policy);
2126   }
2127   OS << ")";
2128 }
2129 
2130 void OMPClausePrinter::VisitOMPDefaultmapClause(OMPDefaultmapClause *Node) {
2131   OS << "defaultmap(";
2132   OS << getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
2133                                       Node->getDefaultmapModifier());
2134   if (Node->getDefaultmapKind() != OMPC_DEFAULTMAP_unknown) {
2135     OS << ": ";
2136     OS << getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
2137                                         Node->getDefaultmapKind());
2138   }
2139   OS << ")";
2140 }
2141 
2142 void OMPClausePrinter::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *Node) {
2143   if (!Node->varlist_empty()) {
2144     OS << "use_device_ptr";
2145     VisitOMPClauseList(Node, '(');
2146     OS << ")";
2147   }
2148 }
2149 
2150 void OMPClausePrinter::VisitOMPUseDeviceAddrClause(
2151     OMPUseDeviceAddrClause *Node) {
2152   if (!Node->varlist_empty()) {
2153     OS << "use_device_addr";
2154     VisitOMPClauseList(Node, '(');
2155     OS << ")";
2156   }
2157 }
2158 
2159 void OMPClausePrinter::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *Node) {
2160   if (!Node->varlist_empty()) {
2161     OS << "is_device_ptr";
2162     VisitOMPClauseList(Node, '(');
2163     OS << ")";
2164   }
2165 }
2166 
2167 void OMPClausePrinter::VisitOMPNontemporalClause(OMPNontemporalClause *Node) {
2168   if (!Node->varlist_empty()) {
2169     OS << "nontemporal";
2170     VisitOMPClauseList(Node, '(');
2171     OS << ")";
2172   }
2173 }
2174 
2175 void OMPClausePrinter::VisitOMPOrderClause(OMPOrderClause *Node) {
2176   OS << "order(" << getOpenMPSimpleClauseTypeName(OMPC_order, Node->getKind())
2177      << ")";
2178 }
2179 
2180 void OMPClausePrinter::VisitOMPInclusiveClause(OMPInclusiveClause *Node) {
2181   if (!Node->varlist_empty()) {
2182     OS << "inclusive";
2183     VisitOMPClauseList(Node, '(');
2184     OS << ")";
2185   }
2186 }
2187 
2188 void OMPClausePrinter::VisitOMPExclusiveClause(OMPExclusiveClause *Node) {
2189   if (!Node->varlist_empty()) {
2190     OS << "exclusive";
2191     VisitOMPClauseList(Node, '(');
2192     OS << ")";
2193   }
2194 }
2195 
2196 void OMPClausePrinter::VisitOMPUsesAllocatorsClause(
2197     OMPUsesAllocatorsClause *Node) {
2198   if (Node->getNumberOfAllocators() == 0)
2199     return;
2200   OS << "uses_allocators(";
2201   for (unsigned I = 0, E = Node->getNumberOfAllocators(); I < E; ++I) {
2202     OMPUsesAllocatorsClause::Data Data = Node->getAllocatorData(I);
2203     Data.Allocator->printPretty(OS, nullptr, Policy);
2204     if (Data.AllocatorTraits) {
2205       OS << "(";
2206       Data.AllocatorTraits->printPretty(OS, nullptr, Policy);
2207       OS << ")";
2208     }
2209     if (I < E - 1)
2210       OS << ",";
2211   }
2212   OS << ")";
2213 }
2214 
2215 void OMPClausePrinter::VisitOMPAffinityClause(OMPAffinityClause *Node) {
2216   if (Node->varlist_empty())
2217     return;
2218   OS << "affinity";
2219   char StartSym = '(';
2220   if (Expr *Modifier = Node->getModifier()) {
2221     OS << "(";
2222     Modifier->printPretty(OS, nullptr, Policy);
2223     OS << " :";
2224     StartSym = ' ';
2225   }
2226   VisitOMPClauseList(Node, StartSym);
2227   OS << ")";
2228 }
2229 
2230 void OMPTraitInfo::getAsVariantMatchInfo(ASTContext &ASTCtx,
2231                                          VariantMatchInfo &VMI) const {
2232   for (const OMPTraitSet &Set : Sets) {
2233     for (const OMPTraitSelector &Selector : Set.Selectors) {
2234 
2235       // User conditions are special as we evaluate the condition here.
2236       if (Selector.Kind == TraitSelector::user_condition) {
2237         assert(Selector.ScoreOrCondition &&
2238                "Ill-formed user condition, expected condition expression!");
2239         assert(Selector.Properties.size() == 1 &&
2240                Selector.Properties.front().Kind ==
2241                    TraitProperty::user_condition_unknown &&
2242                "Ill-formed user condition, expected unknown trait property!");
2243 
2244         if (Optional<APSInt> CondVal =
2245                 Selector.ScoreOrCondition->getIntegerConstantExpr(ASTCtx))
2246           VMI.addTrait(CondVal->isNullValue()
2247                            ? TraitProperty::user_condition_false
2248                            : TraitProperty::user_condition_true,
2249                        "<condition>");
2250         else
2251           VMI.addTrait(TraitProperty::user_condition_false, "<condition>");
2252         continue;
2253       }
2254 
2255       Optional<llvm::APSInt> Score;
2256       llvm::APInt *ScorePtr = nullptr;
2257       if (Selector.ScoreOrCondition) {
2258         if ((Score = Selector.ScoreOrCondition->getIntegerConstantExpr(ASTCtx)))
2259           ScorePtr = &*Score;
2260         else
2261           VMI.addTrait(TraitProperty::user_condition_false,
2262                        "<non-constant-score>");
2263       }
2264 
2265       for (const OMPTraitProperty &Property : Selector.Properties)
2266         VMI.addTrait(Set.Kind, Property.Kind, Property.RawString, ScorePtr);
2267 
2268       if (Set.Kind != TraitSet::construct)
2269         continue;
2270 
2271       // TODO: This might not hold once we implement SIMD properly.
2272       assert(Selector.Properties.size() == 1 &&
2273              Selector.Properties.front().Kind ==
2274                  getOpenMPContextTraitPropertyForSelector(
2275                      Selector.Kind) &&
2276              "Ill-formed construct selector!");
2277 
2278       VMI.ConstructTraits.push_back(Selector.Properties.front().Kind);
2279     }
2280   }
2281 }
2282 
2283 void OMPTraitInfo::print(llvm::raw_ostream &OS,
2284                          const PrintingPolicy &Policy) const {
2285   bool FirstSet = true;
2286   for (const OMPTraitSet &Set : Sets) {
2287     if (!FirstSet)
2288       OS << ", ";
2289     FirstSet = false;
2290     OS << getOpenMPContextTraitSetName(Set.Kind) << "={";
2291 
2292     bool FirstSelector = true;
2293     for (const OMPTraitSelector &Selector : Set.Selectors) {
2294       if (!FirstSelector)
2295         OS << ", ";
2296       FirstSelector = false;
2297       OS << getOpenMPContextTraitSelectorName(Selector.Kind);
2298 
2299       bool AllowsTraitScore = false;
2300       bool RequiresProperty = false;
2301       isValidTraitSelectorForTraitSet(
2302           Selector.Kind, Set.Kind, AllowsTraitScore, RequiresProperty);
2303 
2304       if (!RequiresProperty)
2305         continue;
2306 
2307       OS << "(";
2308       if (Selector.Kind == TraitSelector::user_condition) {
2309         if (Selector.ScoreOrCondition)
2310           Selector.ScoreOrCondition->printPretty(OS, nullptr, Policy);
2311         else
2312           OS << "...";
2313       } else {
2314 
2315         if (Selector.ScoreOrCondition) {
2316           OS << "score(";
2317           Selector.ScoreOrCondition->printPretty(OS, nullptr, Policy);
2318           OS << "): ";
2319         }
2320 
2321         bool FirstProperty = true;
2322         for (const OMPTraitProperty &Property : Selector.Properties) {
2323           if (!FirstProperty)
2324             OS << ", ";
2325           FirstProperty = false;
2326           OS << getOpenMPContextTraitPropertyName(Property.Kind,
2327                                                   Property.RawString);
2328         }
2329       }
2330       OS << ")";
2331     }
2332     OS << "}";
2333   }
2334 }
2335 
2336 std::string OMPTraitInfo::getMangledName() const {
2337   std::string MangledName;
2338   llvm::raw_string_ostream OS(MangledName);
2339   for (const OMPTraitSet &Set : Sets) {
2340     OS << '$' << 'S' << unsigned(Set.Kind);
2341     for (const OMPTraitSelector &Selector : Set.Selectors) {
2342 
2343       bool AllowsTraitScore = false;
2344       bool RequiresProperty = false;
2345       isValidTraitSelectorForTraitSet(
2346           Selector.Kind, Set.Kind, AllowsTraitScore, RequiresProperty);
2347       OS << '$' << 's' << unsigned(Selector.Kind);
2348 
2349       if (!RequiresProperty ||
2350           Selector.Kind == TraitSelector::user_condition)
2351         continue;
2352 
2353       for (const OMPTraitProperty &Property : Selector.Properties)
2354         OS << '$' << 'P'
2355            << getOpenMPContextTraitPropertyName(Property.Kind,
2356                                                 Property.RawString);
2357     }
2358   }
2359   return OS.str();
2360 }
2361 
2362 OMPTraitInfo::OMPTraitInfo(StringRef MangledName) {
2363   unsigned long U;
2364   do {
2365     if (!MangledName.consume_front("$S"))
2366       break;
2367     if (MangledName.consumeInteger(10, U))
2368       break;
2369     Sets.push_back(OMPTraitSet());
2370     OMPTraitSet &Set = Sets.back();
2371     Set.Kind = TraitSet(U);
2372     do {
2373       if (!MangledName.consume_front("$s"))
2374         break;
2375       if (MangledName.consumeInteger(10, U))
2376         break;
2377       Set.Selectors.push_back(OMPTraitSelector());
2378       OMPTraitSelector &Selector = Set.Selectors.back();
2379       Selector.Kind = TraitSelector(U);
2380       do {
2381         if (!MangledName.consume_front("$P"))
2382           break;
2383         Selector.Properties.push_back(OMPTraitProperty());
2384         OMPTraitProperty &Property = Selector.Properties.back();
2385         std::pair<StringRef, StringRef> PropRestPair = MangledName.split('$');
2386         Property.RawString = PropRestPair.first;
2387         Property.Kind = getOpenMPContextTraitPropertyKind(
2388             Set.Kind, Selector.Kind, PropRestPair.first);
2389         MangledName = MangledName.drop_front(PropRestPair.first.size());
2390       } while (true);
2391     } while (true);
2392   } while (true);
2393 }
2394 
2395 llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS,
2396                                      const OMPTraitInfo &TI) {
2397   LangOptions LO;
2398   PrintingPolicy Policy(LO);
2399   TI.print(OS, Policy);
2400   return OS;
2401 }
2402 llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS,
2403                                      const OMPTraitInfo *TI) {
2404   return TI ? OS << *TI : OS;
2405 }
2406 
2407 TargetOMPContext::TargetOMPContext(
2408     ASTContext &ASTCtx, std::function<void(StringRef)> &&DiagUnknownTrait,
2409     const FunctionDecl *CurrentFunctionDecl)
2410     : OMPContext(ASTCtx.getLangOpts().OpenMPIsDevice,
2411                  ASTCtx.getTargetInfo().getTriple()),
2412       FeatureValidityCheck([&](StringRef FeatureName) {
2413         return ASTCtx.getTargetInfo().isValidFeatureName(FeatureName);
2414       }),
2415       DiagUnknownTrait(std::move(DiagUnknownTrait)) {
2416   ASTCtx.getFunctionFeatureMap(FeatureMap, CurrentFunctionDecl);
2417 }
2418 
2419 bool TargetOMPContext::matchesISATrait(StringRef RawString) const {
2420   auto It = FeatureMap.find(RawString);
2421   if (It != FeatureMap.end())
2422     return It->second;
2423   if (!FeatureValidityCheck(RawString))
2424     DiagUnknownTrait(RawString);
2425   return false;
2426 }
2427