clang  6.0.0svn
SemaOpenMP.cpp
Go to the documentation of this file.
1 //===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 /// \file
10 /// \brief This file implements semantic analysis for OpenMP directives and
11 /// clauses.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #include "TreeTransform.h"
16 #include "clang/AST/ASTContext.h"
19 #include "clang/AST/Decl.h"
20 #include "clang/AST/DeclCXX.h"
21 #include "clang/AST/DeclOpenMP.h"
22 #include "clang/AST/StmtCXX.h"
23 #include "clang/AST/StmtOpenMP.h"
24 #include "clang/AST/StmtVisitor.h"
27 #include "clang/Sema/Lookup.h"
28 #include "clang/Sema/Scope.h"
29 #include "clang/Sema/ScopeInfo.h"
31 #include "llvm/ADT/PointerEmbeddedInt.h"
32 using namespace clang;
33 
34 //===----------------------------------------------------------------------===//
35 // Stack of data-sharing attributes for variables
36 //===----------------------------------------------------------------------===//
37 
39  Sema &SemaRef, Expr *E,
41  OpenMPClauseKind CKind);
42 
43 namespace {
44 /// \brief Default data sharing attributes, which can be applied to directive.
46  DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
47  DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
48  DSA_shared = 1 << 1, /// \brief Default data sharing attribute 'shared'.
49 };
50 
51 /// Attributes of the defaultmap clause.
53  DMA_unspecified, /// Default mapping is not specified.
54  DMA_tofrom_scalar, /// Default mapping is 'tofrom:scalar'.
55 };
56 
57 /// \brief Stack for tracking declarations used in OpenMP directives and
58 /// clauses and their data-sharing attributes.
59 class DSAStackTy final {
60 public:
61  struct DSAVarData final {
64  Expr *RefExpr = nullptr;
65  DeclRefExpr *PrivateCopy = nullptr;
66  SourceLocation ImplicitDSALoc;
67  DSAVarData() = default;
68  DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, Expr *RefExpr,
69  DeclRefExpr *PrivateCopy, SourceLocation ImplicitDSALoc)
70  : DKind(DKind), CKind(CKind), RefExpr(RefExpr),
71  PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc) {}
72  };
74  OperatorOffsetTy;
75 
76 private:
77  struct DSAInfo final {
78  OpenMPClauseKind Attributes = OMPC_unknown;
79  /// Pointer to a reference expression and a flag which shows that the
80  /// variable is marked as lastprivate(true) or not (false).
81  llvm::PointerIntPair<Expr *, 1, bool> RefExpr;
82  DeclRefExpr *PrivateCopy = nullptr;
83  };
84  typedef llvm::DenseMap<ValueDecl *, DSAInfo> DeclSAMapTy;
85  typedef llvm::DenseMap<ValueDecl *, Expr *> AlignedMapTy;
86  typedef std::pair<unsigned, VarDecl *> LCDeclInfo;
87  typedef llvm::DenseMap<ValueDecl *, LCDeclInfo> LoopControlVariablesMapTy;
88  /// Struct that associates a component with the clause kind where they are
89  /// found.
90  struct MappedExprComponentTy {
93  };
94  typedef llvm::DenseMap<ValueDecl *, MappedExprComponentTy>
95  MappedExprComponentsTy;
96  typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>>
97  CriticalsWithHintsTy;
98  typedef llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>
99  DoacrossDependMapTy;
100  struct ReductionData {
101  typedef llvm::PointerEmbeddedInt<BinaryOperatorKind, 16> BOKPtrType;
102  SourceRange ReductionRange;
103  llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp;
104  ReductionData() = default;
105  void set(BinaryOperatorKind BO, SourceRange RR) {
106  ReductionRange = RR;
107  ReductionOp = BO;
108  }
109  void set(const Expr *RefExpr, SourceRange RR) {
110  ReductionRange = RR;
111  ReductionOp = RefExpr;
112  }
113  };
114  typedef llvm::DenseMap<ValueDecl *, ReductionData> DeclReductionMapTy;
115 
116  struct SharingMapTy final {
117  DeclSAMapTy SharingMap;
118  DeclReductionMapTy ReductionMap;
119  AlignedMapTy AlignedMap;
120  MappedExprComponentsTy MappedExprComponents;
121  LoopControlVariablesMapTy LCVMap;
122  DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
123  SourceLocation DefaultAttrLoc;
124  DefaultMapAttributes DefaultMapAttr = DMA_unspecified;
125  SourceLocation DefaultMapAttrLoc;
127  DeclarationNameInfo DirectiveName;
128  Scope *CurScope = nullptr;
129  SourceLocation ConstructLoc;
130  /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to
131  /// get the data (loop counters etc.) about enclosing loop-based construct.
132  /// This data is required during codegen.
133  DoacrossDependMapTy DoacrossDepends;
134  /// \brief first argument (Expr *) contains optional argument of the
135  /// 'ordered' clause, the second one is true if the regions has 'ordered'
136  /// clause, false otherwise.
137  llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
138  bool NowaitRegion = false;
139  bool CancelRegion = false;
140  unsigned AssociatedLoops = 1;
141  SourceLocation InnerTeamsRegionLoc;
142  /// Reference to the taskgroup task_reduction reference expression.
143  Expr *TaskgroupReductionRef = nullptr;
144  SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
145  Scope *CurScope, SourceLocation Loc)
146  : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
147  ConstructLoc(Loc) {}
148  SharingMapTy() = default;
149  };
150 
151  typedef SmallVector<SharingMapTy, 4> StackTy;
152 
153  /// \brief Stack of used declaration and their data-sharing attributes.
154  DeclSAMapTy Threadprivates;
155  const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr;
157  /// \brief true, if check for DSA must be from parent directive, false, if
158  /// from current directive.
159  OpenMPClauseKind ClauseKindMode = OMPC_unknown;
160  Sema &SemaRef;
161  bool ForceCapturing = false;
162  CriticalsWithHintsTy Criticals;
163 
164  typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
165 
166  DSAVarData getDSA(StackTy::reverse_iterator &Iter, ValueDecl *D);
167 
168  /// \brief Checks if the variable is a local for OpenMP region.
169  bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
170 
171  bool isStackEmpty() const {
172  return Stack.empty() ||
173  Stack.back().second != CurrentNonCapturingFunctionScope ||
174  Stack.back().first.empty();
175  }
176 
177 public:
178  explicit DSAStackTy(Sema &S) : SemaRef(S) {}
179 
180  bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
181  void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
182 
183  bool isForceVarCapturing() const { return ForceCapturing; }
184  void setForceVarCapturing(bool V) { ForceCapturing = V; }
185 
186  void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
187  Scope *CurScope, SourceLocation Loc) {
188  if (Stack.empty() ||
189  Stack.back().second != CurrentNonCapturingFunctionScope)
190  Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope);
191  Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc);
192  Stack.back().first.back().DefaultAttrLoc = Loc;
193  }
194 
195  void pop() {
196  assert(!Stack.back().first.empty() &&
197  "Data-sharing attributes stack is empty!");
198  Stack.back().first.pop_back();
199  }
200 
201  /// Start new OpenMP region stack in new non-capturing function.
202  void pushFunction() {
203  const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction();
204  assert(!isa<CapturingScopeInfo>(CurFnScope));
205  CurrentNonCapturingFunctionScope = CurFnScope;
206  }
207  /// Pop region stack for non-capturing function.
208  void popFunction(const FunctionScopeInfo *OldFSI) {
209  if (!Stack.empty() && Stack.back().second == OldFSI) {
210  assert(Stack.back().first.empty());
211  Stack.pop_back();
212  }
213  CurrentNonCapturingFunctionScope = nullptr;
214  for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) {
215  if (!isa<CapturingScopeInfo>(FSI)) {
216  CurrentNonCapturingFunctionScope = FSI;
217  break;
218  }
219  }
220  }
221 
222  void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) {
223  Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint);
224  }
225  const std::pair<OMPCriticalDirective *, llvm::APSInt>
226  getCriticalWithHint(const DeclarationNameInfo &Name) const {
227  auto I = Criticals.find(Name.getAsString());
228  if (I != Criticals.end())
229  return I->second;
230  return std::make_pair(nullptr, llvm::APSInt());
231  }
232  /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
233  /// add it and return NULL; otherwise return previous occurrence's expression
234  /// for diagnostics.
235  Expr *addUniqueAligned(ValueDecl *D, Expr *NewDE);
236 
237  /// \brief Register specified variable as loop control variable.
238  void addLoopControlVariable(ValueDecl *D, VarDecl *Capture);
239  /// \brief Check if the specified variable is a loop control variable for
240  /// current region.
241  /// \return The index of the loop control variable in the list of associated
242  /// for-loops (from outer to inner).
243  LCDeclInfo isLoopControlVariable(ValueDecl *D);
244  /// \brief Check if the specified variable is a loop control variable for
245  /// parent region.
246  /// \return The index of the loop control variable in the list of associated
247  /// for-loops (from outer to inner).
248  LCDeclInfo isParentLoopControlVariable(ValueDecl *D);
249  /// \brief Get the loop control variable for the I-th loop (or nullptr) in
250  /// parent directive.
251  ValueDecl *getParentLoopControlVariable(unsigned I);
252 
253  /// \brief Adds explicit data sharing attribute to the specified declaration.
254  void addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
255  DeclRefExpr *PrivateCopy = nullptr);
256 
257  /// Adds additional information for the reduction items with the reduction id
258  /// represented as an operator.
259  void addTaskgroupReductionData(ValueDecl *D, SourceRange SR,
260  BinaryOperatorKind BOK);
261  /// Adds additional information for the reduction items with the reduction id
262  /// represented as reduction identifier.
263  void addTaskgroupReductionData(ValueDecl *D, SourceRange SR,
264  const Expr *ReductionRef);
265  /// Returns the location and reduction operation from the innermost parent
266  /// region for the given \p D.
267  DSAVarData getTopMostTaskgroupReductionData(ValueDecl *D, SourceRange &SR,
268  BinaryOperatorKind &BOK,
269  Expr *&TaskgroupDescriptor);
270  /// Returns the location and reduction operation from the innermost parent
271  /// region for the given \p D.
272  DSAVarData getTopMostTaskgroupReductionData(ValueDecl *D, SourceRange &SR,
273  const Expr *&ReductionRef,
274  Expr *&TaskgroupDescriptor);
275  /// Return reduction reference expression for the current taskgroup.
276  Expr *getTaskgroupReductionRef() const {
277  assert(Stack.back().first.back().Directive == OMPD_taskgroup &&
278  "taskgroup reference expression requested for non taskgroup "
279  "directive.");
280  return Stack.back().first.back().TaskgroupReductionRef;
281  }
282  /// Checks if the given \p VD declaration is actually a taskgroup reduction
283  /// descriptor variable at the \p Level of OpenMP regions.
284  bool isTaskgroupReductionRef(ValueDecl *VD, unsigned Level) const {
285  return Stack.back().first[Level].TaskgroupReductionRef &&
286  cast<DeclRefExpr>(Stack.back().first[Level].TaskgroupReductionRef)
287  ->getDecl() == VD;
288  }
289 
290  /// \brief Returns data sharing attributes from top of the stack for the
291  /// specified declaration.
292  DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
293  /// \brief Returns data-sharing attributes for the specified declaration.
294  DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent);
295  /// \brief Checks if the specified variables has data-sharing attributes which
296  /// match specified \a CPred predicate in any directive which matches \a DPred
297  /// predicate.
298  DSAVarData hasDSA(ValueDecl *D,
299  const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
300  const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
301  bool FromParent);
302  /// \brief Checks if the specified variables has data-sharing attributes which
303  /// match specified \a CPred predicate in any innermost directive which
304  /// matches \a DPred predicate.
305  DSAVarData
306  hasInnermostDSA(ValueDecl *D,
307  const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
308  const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
309  bool FromParent);
310  /// \brief Checks if the specified variables has explicit data-sharing
311  /// attributes which match specified \a CPred predicate at the specified
312  /// OpenMP region.
313  bool hasExplicitDSA(ValueDecl *D,
314  const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
315  unsigned Level, bool NotLastprivate = false);
316 
317  /// \brief Returns true if the directive at level \Level matches in the
318  /// specified \a DPred predicate.
319  bool hasExplicitDirective(
320  const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
321  unsigned Level);
322 
323  /// \brief Finds a directive which matches specified \a DPred predicate.
324  bool hasDirective(const llvm::function_ref<bool(OpenMPDirectiveKind,
325  const DeclarationNameInfo &,
326  SourceLocation)> &DPred,
327  bool FromParent);
328 
329  /// \brief Returns currently analyzed directive.
330  OpenMPDirectiveKind getCurrentDirective() const {
331  return isStackEmpty() ? OMPD_unknown : Stack.back().first.back().Directive;
332  }
333  /// \brief Returns parent directive.
334  OpenMPDirectiveKind getParentDirective() const {
335  if (isStackEmpty() || Stack.back().first.size() == 1)
336  return OMPD_unknown;
337  return std::next(Stack.back().first.rbegin())->Directive;
338  }
339 
340  /// \brief Set default data sharing attribute to none.
341  void setDefaultDSANone(SourceLocation Loc) {
342  assert(!isStackEmpty());
343  Stack.back().first.back().DefaultAttr = DSA_none;
344  Stack.back().first.back().DefaultAttrLoc = Loc;
345  }
346  /// \brief Set default data sharing attribute to shared.
347  void setDefaultDSAShared(SourceLocation Loc) {
348  assert(!isStackEmpty());
349  Stack.back().first.back().DefaultAttr = DSA_shared;
350  Stack.back().first.back().DefaultAttrLoc = Loc;
351  }
352  /// Set default data mapping attribute to 'tofrom:scalar'.
353  void setDefaultDMAToFromScalar(SourceLocation Loc) {
354  assert(!isStackEmpty());
355  Stack.back().first.back().DefaultMapAttr = DMA_tofrom_scalar;
356  Stack.back().first.back().DefaultMapAttrLoc = Loc;
357  }
358 
359  DefaultDataSharingAttributes getDefaultDSA() const {
360  return isStackEmpty() ? DSA_unspecified
361  : Stack.back().first.back().DefaultAttr;
362  }
363  SourceLocation getDefaultDSALocation() const {
364  return isStackEmpty() ? SourceLocation()
365  : Stack.back().first.back().DefaultAttrLoc;
366  }
367  DefaultMapAttributes getDefaultDMA() const {
368  return isStackEmpty() ? DMA_unspecified
369  : Stack.back().first.back().DefaultMapAttr;
370  }
371  DefaultMapAttributes getDefaultDMAAtLevel(unsigned Level) const {
372  return Stack.back().first[Level].DefaultMapAttr;
373  }
374  SourceLocation getDefaultDMALocation() const {
375  return isStackEmpty() ? SourceLocation()
376  : Stack.back().first.back().DefaultMapAttrLoc;
377  }
378 
379  /// \brief Checks if the specified variable is a threadprivate.
380  bool isThreadPrivate(VarDecl *D) {
381  DSAVarData DVar = getTopDSA(D, false);
382  return isOpenMPThreadPrivate(DVar.CKind);
383  }
384 
385  /// \brief Marks current region as ordered (it has an 'ordered' clause).
386  void setOrderedRegion(bool IsOrdered, Expr *Param) {
387  assert(!isStackEmpty());
388  Stack.back().first.back().OrderedRegion.setInt(IsOrdered);
389  Stack.back().first.back().OrderedRegion.setPointer(Param);
390  }
391  /// \brief Returns true, if parent region is ordered (has associated
392  /// 'ordered' clause), false - otherwise.
393  bool isParentOrderedRegion() const {
394  if (isStackEmpty() || Stack.back().first.size() == 1)
395  return false;
396  return std::next(Stack.back().first.rbegin())->OrderedRegion.getInt();
397  }
398  /// \brief Returns optional parameter for the ordered region.
399  Expr *getParentOrderedRegionParam() const {
400  if (isStackEmpty() || Stack.back().first.size() == 1)
401  return nullptr;
402  return std::next(Stack.back().first.rbegin())->OrderedRegion.getPointer();
403  }
404  /// \brief Marks current region as nowait (it has a 'nowait' clause).
405  void setNowaitRegion(bool IsNowait = true) {
406  assert(!isStackEmpty());
407  Stack.back().first.back().NowaitRegion = IsNowait;
408  }
409  /// \brief Returns true, if parent region is nowait (has associated
410  /// 'nowait' clause), false - otherwise.
411  bool isParentNowaitRegion() const {
412  if (isStackEmpty() || Stack.back().first.size() == 1)
413  return false;
414  return std::next(Stack.back().first.rbegin())->NowaitRegion;
415  }
416  /// \brief Marks parent region as cancel region.
417  void setParentCancelRegion(bool Cancel = true) {
418  if (!isStackEmpty() && Stack.back().first.size() > 1) {
419  auto &StackElemRef = *std::next(Stack.back().first.rbegin());
420  StackElemRef.CancelRegion |= StackElemRef.CancelRegion || Cancel;
421  }
422  }
423  /// \brief Return true if current region has inner cancel construct.
424  bool isCancelRegion() const {
425  return isStackEmpty() ? false : Stack.back().first.back().CancelRegion;
426  }
427 
428  /// \brief Set collapse value for the region.
429  void setAssociatedLoops(unsigned Val) {
430  assert(!isStackEmpty());
431  Stack.back().first.back().AssociatedLoops = Val;
432  }
433  /// \brief Return collapse value for region.
434  unsigned getAssociatedLoops() const {
435  return isStackEmpty() ? 0 : Stack.back().first.back().AssociatedLoops;
436  }
437 
438  /// \brief Marks current target region as one with closely nested teams
439  /// region.
440  void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
441  if (!isStackEmpty() && Stack.back().first.size() > 1) {
442  std::next(Stack.back().first.rbegin())->InnerTeamsRegionLoc =
443  TeamsRegionLoc;
444  }
445  }
446  /// \brief Returns true, if current region has closely nested teams region.
447  bool hasInnerTeamsRegion() const {
448  return getInnerTeamsRegionLoc().isValid();
449  }
450  /// \brief Returns location of the nested teams region (if any).
451  SourceLocation getInnerTeamsRegionLoc() const {
452  return isStackEmpty() ? SourceLocation()
453  : Stack.back().first.back().InnerTeamsRegionLoc;
454  }
455 
456  Scope *getCurScope() const {
457  return isStackEmpty() ? nullptr : Stack.back().first.back().CurScope;
458  }
459  Scope *getCurScope() {
460  return isStackEmpty() ? nullptr : Stack.back().first.back().CurScope;
461  }
462  SourceLocation getConstructLoc() {
463  return isStackEmpty() ? SourceLocation()
464  : Stack.back().first.back().ConstructLoc;
465  }
466 
467  /// Do the check specified in \a Check to all component lists and return true
468  /// if any issue is found.
469  bool checkMappableExprComponentListsForDecl(
470  ValueDecl *VD, bool CurrentRegionOnly,
471  const llvm::function_ref<
473  OpenMPClauseKind)> &Check) {
474  if (isStackEmpty())
475  return false;
476  auto SI = Stack.back().first.rbegin();
477  auto SE = Stack.back().first.rend();
478 
479  if (SI == SE)
480  return false;
481 
482  if (CurrentRegionOnly) {
483  SE = std::next(SI);
484  } else {
485  ++SI;
486  }
487 
488  for (; SI != SE; ++SI) {
489  auto MI = SI->MappedExprComponents.find(VD);
490  if (MI != SI->MappedExprComponents.end())
491  for (auto &L : MI->second.Components)
492  if (Check(L, MI->second.Kind))
493  return true;
494  }
495  return false;
496  }
497 
498  /// Do the check specified in \a Check to all component lists at a given level
499  /// and return true if any issue is found.
500  bool checkMappableExprComponentListsForDeclAtLevel(
501  ValueDecl *VD, unsigned Level,
502  const llvm::function_ref<
504  OpenMPClauseKind)> &Check) {
505  if (isStackEmpty())
506  return false;
507 
508  auto StartI = Stack.back().first.begin();
509  auto EndI = Stack.back().first.end();
510  if (std::distance(StartI, EndI) <= (int)Level)
511  return false;
512  std::advance(StartI, Level);
513 
514  auto MI = StartI->MappedExprComponents.find(VD);
515  if (MI != StartI->MappedExprComponents.end())
516  for (auto &L : MI->second.Components)
517  if (Check(L, MI->second.Kind))
518  return true;
519  return false;
520  }
521 
522  /// Create a new mappable expression component list associated with a given
523  /// declaration and initialize it with the provided list of components.
524  void addMappableExpressionComponents(
525  ValueDecl *VD,
527  OpenMPClauseKind WhereFoundClauseKind) {
528  assert(!isStackEmpty() &&
529  "Not expecting to retrieve components from a empty stack!");
530  auto &MEC = Stack.back().first.back().MappedExprComponents[VD];
531  // Create new entry and append the new components there.
532  MEC.Components.resize(MEC.Components.size() + 1);
533  MEC.Components.back().append(Components.begin(), Components.end());
534  MEC.Kind = WhereFoundClauseKind;
535  }
536 
537  unsigned getNestingLevel() const {
538  assert(!isStackEmpty());
539  return Stack.back().first.size() - 1;
540  }
541  void addDoacrossDependClause(OMPDependClause *C, OperatorOffsetTy &OpsOffs) {
542  assert(!isStackEmpty() && Stack.back().first.size() > 1);
543  auto &StackElem = *std::next(Stack.back().first.rbegin());
544  assert(isOpenMPWorksharingDirective(StackElem.Directive));
545  StackElem.DoacrossDepends.insert({C, OpsOffs});
546  }
547  llvm::iterator_range<DoacrossDependMapTy::const_iterator>
548  getDoacrossDependClauses() const {
549  assert(!isStackEmpty());
550  auto &StackElem = Stack.back().first.back();
551  if (isOpenMPWorksharingDirective(StackElem.Directive)) {
552  auto &Ref = StackElem.DoacrossDepends;
553  return llvm::make_range(Ref.begin(), Ref.end());
554  }
555  return llvm::make_range(StackElem.DoacrossDepends.end(),
556  StackElem.DoacrossDepends.end());
557  }
558 };
559 bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
560  return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) ||
561  isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
562 }
563 } // namespace
564 
566  if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
567  E = ExprTemp->getSubExpr();
568 
569  if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
570  E = MTE->GetTemporaryExpr();
571 
572  while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
573  E = Binder->getSubExpr();
574 
575  if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
576  E = ICE->getSubExprAsWritten();
577  return E->IgnoreParens();
578 }
579 
581  if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D))
582  if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
583  D = ME->getMemberDecl();
584  auto *VD = dyn_cast<VarDecl>(D);
585  auto *FD = dyn_cast<FieldDecl>(D);
586  if (VD != nullptr) {
587  VD = VD->getCanonicalDecl();
588  D = VD;
589  } else {
590  assert(FD);
591  FD = FD->getCanonicalDecl();
592  D = FD;
593  }
594  return D;
595 }
596 
597 DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator &Iter,
598  ValueDecl *D) {
599  D = getCanonicalDecl(D);
600  auto *VD = dyn_cast<VarDecl>(D);
601  auto *FD = dyn_cast<FieldDecl>(D);
602  DSAVarData DVar;
603  if (isStackEmpty() || Iter == Stack.back().first.rend()) {
604  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
605  // in a region but not in construct]
606  // File-scope or namespace-scope variables referenced in called routines
607  // in the region are shared unless they appear in a threadprivate
608  // directive.
609  if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
610  DVar.CKind = OMPC_shared;
611 
612  // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
613  // in a region but not in construct]
614  // Variables with static storage duration that are declared in called
615  // routines in the region are shared.
616  if (VD && VD->hasGlobalStorage())
617  DVar.CKind = OMPC_shared;
618 
619  // Non-static data members are shared by default.
620  if (FD)
621  DVar.CKind = OMPC_shared;
622 
623  return DVar;
624  }
625 
626  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
627  // in a Construct, C/C++, predetermined, p.1]
628  // Variables with automatic storage duration that are declared in a scope
629  // inside the construct are private.
630  if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
631  (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
632  DVar.CKind = OMPC_private;
633  return DVar;
634  }
635 
636  DVar.DKind = Iter->Directive;
637  // Explicitly specified attributes and local variables with predetermined
638  // attributes.
639  if (Iter->SharingMap.count(D)) {
640  DVar.RefExpr = Iter->SharingMap[D].RefExpr.getPointer();
641  DVar.PrivateCopy = Iter->SharingMap[D].PrivateCopy;
642  DVar.CKind = Iter->SharingMap[D].Attributes;
643  DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
644  return DVar;
645  }
646 
647  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
648  // in a Construct, C/C++, implicitly determined, p.1]
649  // In a parallel or task construct, the data-sharing attributes of these
650  // variables are determined by the default clause, if present.
651  switch (Iter->DefaultAttr) {
652  case DSA_shared:
653  DVar.CKind = OMPC_shared;
654  DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
655  return DVar;
656  case DSA_none:
657  return DVar;
658  case DSA_unspecified:
659  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
660  // in a Construct, implicitly determined, p.2]
661  // In a parallel construct, if no default clause is present, these
662  // variables are shared.
663  DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
664  if (isOpenMPParallelDirective(DVar.DKind) ||
665  isOpenMPTeamsDirective(DVar.DKind)) {
666  DVar.CKind = OMPC_shared;
667  return DVar;
668  }
669 
670  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
671  // in a Construct, implicitly determined, p.4]
672  // In a task construct, if no default clause is present, a variable that in
673  // the enclosing context is determined to be shared by all implicit tasks
674  // bound to the current team is shared.
675  if (isOpenMPTaskingDirective(DVar.DKind)) {
676  DSAVarData DVarTemp;
677  auto I = Iter, E = Stack.back().first.rend();
678  do {
679  ++I;
680  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
681  // Referenced in a Construct, implicitly determined, p.6]
682  // In a task construct, if no default clause is present, a variable
683  // whose data-sharing attribute is not determined by the rules above is
684  // firstprivate.
685  DVarTemp = getDSA(I, D);
686  if (DVarTemp.CKind != OMPC_shared) {
687  DVar.RefExpr = nullptr;
688  DVar.CKind = OMPC_firstprivate;
689  return DVar;
690  }
691  } while (I != E && !isParallelOrTaskRegion(I->Directive));
692  DVar.CKind =
693  (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
694  return DVar;
695  }
696  }
697  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
698  // in a Construct, implicitly determined, p.3]
699  // For constructs other than task, if no default clause is present, these
700  // variables inherit their data-sharing attributes from the enclosing
701  // context.
702  return getDSA(++Iter, D);
703 }
704 
705 Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) {
706  assert(!isStackEmpty() && "Data sharing attributes stack is empty");
707  D = getCanonicalDecl(D);
708  auto &StackElem = Stack.back().first.back();
709  auto It = StackElem.AlignedMap.find(D);
710  if (It == StackElem.AlignedMap.end()) {
711  assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
712  StackElem.AlignedMap[D] = NewDE;
713  return nullptr;
714  } else {
715  assert(It->second && "Unexpected nullptr expr in the aligned map");
716  return It->second;
717  }
718  return nullptr;
719 }
720 
721 void DSAStackTy::addLoopControlVariable(ValueDecl *D, VarDecl *Capture) {
722  assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
723  D = getCanonicalDecl(D);
724  auto &StackElem = Stack.back().first.back();
725  StackElem.LCVMap.insert(
726  {D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture)});
727 }
728 
729 DSAStackTy::LCDeclInfo DSAStackTy::isLoopControlVariable(ValueDecl *D) {
730  assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
731  D = getCanonicalDecl(D);
732  auto &StackElem = Stack.back().first.back();
733  auto It = StackElem.LCVMap.find(D);
734  if (It != StackElem.LCVMap.end())
735  return It->second;
736  return {0, nullptr};
737 }
738 
739 DSAStackTy::LCDeclInfo DSAStackTy::isParentLoopControlVariable(ValueDecl *D) {
740  assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
741  "Data-sharing attributes stack is empty");
742  D = getCanonicalDecl(D);
743  auto &StackElem = *std::next(Stack.back().first.rbegin());
744  auto It = StackElem.LCVMap.find(D);
745  if (It != StackElem.LCVMap.end())
746  return It->second;
747  return {0, nullptr};
748 }
749 
750 ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) {
751  assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
752  "Data-sharing attributes stack is empty");
753  auto &StackElem = *std::next(Stack.back().first.rbegin());
754  if (StackElem.LCVMap.size() < I)
755  return nullptr;
756  for (auto &Pair : StackElem.LCVMap)
757  if (Pair.second.first == I)
758  return Pair.first;
759  return nullptr;
760 }
761 
762 void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
763  DeclRefExpr *PrivateCopy) {
764  D = getCanonicalDecl(D);
765  if (A == OMPC_threadprivate) {
766  auto &Data = Threadprivates[D];
767  Data.Attributes = A;
768  Data.RefExpr.setPointer(E);
769  Data.PrivateCopy = nullptr;
770  } else {
771  assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
772  auto &Data = Stack.back().first.back().SharingMap[D];
773  assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
774  (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
775  (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
776  (isLoopControlVariable(D).first && A == OMPC_private));
777  if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
778  Data.RefExpr.setInt(/*IntVal=*/true);
779  return;
780  }
781  const bool IsLastprivate =
782  A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
783  Data.Attributes = A;
784  Data.RefExpr.setPointerAndInt(E, IsLastprivate);
785  Data.PrivateCopy = PrivateCopy;
786  if (PrivateCopy) {
787  auto &Data = Stack.back().first.back().SharingMap[PrivateCopy->getDecl()];
788  Data.Attributes = A;
789  Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
790  Data.PrivateCopy = nullptr;
791  }
792  }
793 }
794 
795 /// \brief Build a variable declaration for OpenMP loop iteration variable.
797  StringRef Name, const AttrVec *Attrs = nullptr) {
798  DeclContext *DC = SemaRef.CurContext;
799  IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
800  TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
801  VarDecl *Decl =
802  VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
803  if (Attrs) {
804  for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
805  I != E; ++I)
806  Decl->addAttr(*I);
807  }
808  Decl->setImplicit();
809  return Decl;
810 }
811 
813  SourceLocation Loc,
814  bool RefersToCapture = false) {
815  D->setReferenced();
816  D->markUsed(S.Context);
818  SourceLocation(), D, RefersToCapture, Loc, Ty,
819  VK_LValue);
820 }
821 
822 void DSAStackTy::addTaskgroupReductionData(ValueDecl *D, SourceRange SR,
823  BinaryOperatorKind BOK) {
824  D = getCanonicalDecl(D);
825  assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
826  assert(
827  Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
828  "Additional reduction info may be specified only for reduction items.");
829  auto &ReductionData = Stack.back().first.back().ReductionMap[D];
830  assert(ReductionData.ReductionRange.isInvalid() &&
831  Stack.back().first.back().Directive == OMPD_taskgroup &&
832  "Additional reduction info may be specified only once for reduction "
833  "items.");
834  ReductionData.set(BOK, SR);
835  Expr *&TaskgroupReductionRef =
836  Stack.back().first.back().TaskgroupReductionRef;
837  if (!TaskgroupReductionRef) {
838  auto *VD = buildVarDecl(SemaRef, SourceLocation(),
839  SemaRef.Context.VoidPtrTy, ".task_red.");
840  TaskgroupReductionRef = buildDeclRefExpr(
841  SemaRef, VD, SemaRef.Context.VoidPtrTy, SourceLocation());
842  }
843 }
844 
845 void DSAStackTy::addTaskgroupReductionData(ValueDecl *D, SourceRange SR,
846  const Expr *ReductionRef) {
847  D = getCanonicalDecl(D);
848  assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
849  assert(
850  Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
851  "Additional reduction info may be specified only for reduction items.");
852  auto &ReductionData = Stack.back().first.back().ReductionMap[D];
853  assert(ReductionData.ReductionRange.isInvalid() &&
854  Stack.back().first.back().Directive == OMPD_taskgroup &&
855  "Additional reduction info may be specified only once for reduction "
856  "items.");
857  ReductionData.set(ReductionRef, SR);
858  Expr *&TaskgroupReductionRef =
859  Stack.back().first.back().TaskgroupReductionRef;
860  if (!TaskgroupReductionRef) {
861  auto *VD = buildVarDecl(SemaRef, SourceLocation(),
862  SemaRef.Context.VoidPtrTy, ".task_red.");
863  TaskgroupReductionRef = buildDeclRefExpr(
864  SemaRef, VD, SemaRef.Context.VoidPtrTy, SourceLocation());
865  }
866 }
867 
868 DSAStackTy::DSAVarData
869 DSAStackTy::getTopMostTaskgroupReductionData(ValueDecl *D, SourceRange &SR,
870  BinaryOperatorKind &BOK,
871  Expr *&TaskgroupDescriptor) {
872  D = getCanonicalDecl(D);
873  assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
874  if (Stack.back().first.empty())
875  return DSAVarData();
876  for (auto I = std::next(Stack.back().first.rbegin(), 1),
877  E = Stack.back().first.rend();
878  I != E; std::advance(I, 1)) {
879  auto &Data = I->SharingMap[D];
880  if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
881  continue;
882  auto &ReductionData = I->ReductionMap[D];
883  if (!ReductionData.ReductionOp ||
884  ReductionData.ReductionOp.is<const Expr *>())
885  return DSAVarData();
886  SR = ReductionData.ReductionRange;
887  BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>();
888  assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
889  "expression for the descriptor is not "
890  "set.");
891  TaskgroupDescriptor = I->TaskgroupReductionRef;
892  return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
893  Data.PrivateCopy, I->DefaultAttrLoc);
894  }
895  return DSAVarData();
896 }
897 
898 DSAStackTy::DSAVarData
899 DSAStackTy::getTopMostTaskgroupReductionData(ValueDecl *D, SourceRange &SR,
900  const Expr *&ReductionRef,
901  Expr *&TaskgroupDescriptor) {
902  D = getCanonicalDecl(D);
903  assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
904  if (Stack.back().first.empty())
905  return DSAVarData();
906  for (auto I = std::next(Stack.back().first.rbegin(), 1),
907  E = Stack.back().first.rend();
908  I != E; std::advance(I, 1)) {
909  auto &Data = I->SharingMap[D];
910  if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
911  continue;
912  auto &ReductionData = I->ReductionMap[D];
913  if (!ReductionData.ReductionOp ||
914  !ReductionData.ReductionOp.is<const Expr *>())
915  return DSAVarData();
916  SR = ReductionData.ReductionRange;
917  ReductionRef = ReductionData.ReductionOp.get<const Expr *>();
918  assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
919  "expression for the descriptor is not "
920  "set.");
921  TaskgroupDescriptor = I->TaskgroupReductionRef;
922  return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
923  Data.PrivateCopy, I->DefaultAttrLoc);
924  }
925  return DSAVarData();
926 }
927 
928 bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
929  D = D->getCanonicalDecl();
930  if (!isStackEmpty() && Stack.back().first.size() > 1) {
931  reverse_iterator I = Iter, E = Stack.back().first.rend();
932  Scope *TopScope = nullptr;
933  while (I != E && !isParallelOrTaskRegion(I->Directive))
934  ++I;
935  if (I == E)
936  return false;
937  TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
938  Scope *CurScope = getCurScope();
939  while (CurScope != TopScope && !CurScope->isDeclScope(D))
940  CurScope = CurScope->getParent();
941  return CurScope != TopScope;
942  }
943  return false;
944 }
945 
946 DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) {
947  D = getCanonicalDecl(D);
948  DSAVarData DVar;
949 
950  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
951  // in a Construct, C/C++, predetermined, p.1]
952  // Variables appearing in threadprivate directives are threadprivate.
953  auto *VD = dyn_cast<VarDecl>(D);
954  if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
955  !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
956  SemaRef.getLangOpts().OpenMPUseTLS &&
957  SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
958  (VD && VD->getStorageClass() == SC_Register &&
959  VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
960  addDSA(D, buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
961  D->getLocation()),
963  }
964  auto TI = Threadprivates.find(D);
965  if (TI != Threadprivates.end()) {
966  DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
967  DVar.CKind = OMPC_threadprivate;
968  return DVar;
969  }
970 
971  if (isStackEmpty())
972  // Not in OpenMP execution region and top scope was already checked.
973  return DVar;
974 
975  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
976  // in a Construct, C/C++, predetermined, p.4]
977  // Static data members are shared.
978  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
979  // in a Construct, C/C++, predetermined, p.7]
980  // Variables with static storage duration that are declared in a scope
981  // inside the construct are shared.
982  auto &&MatchesAlways = [](OpenMPDirectiveKind) -> bool { return true; };
983  if (VD && VD->isStaticDataMember()) {
984  DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent);
985  if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
986  return DVar;
987 
988  DVar.CKind = OMPC_shared;
989  return DVar;
990  }
991 
993  bool IsConstant = Type.isConstant(SemaRef.getASTContext());
994  Type = SemaRef.getASTContext().getBaseElementType(Type);
995  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
996  // in a Construct, C/C++, predetermined, p.6]
997  // Variables with const qualified type having no mutable member are
998  // shared.
999  CXXRecordDecl *RD =
1000  SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
1001  if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
1002  if (auto *CTD = CTSD->getSpecializedTemplate())
1003  RD = CTD->getTemplatedDecl();
1004  if (IsConstant &&
1005  !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
1006  RD->hasMutableFields())) {
1007  // Variables with const-qualified type having no mutable member may be
1008  // listed in a firstprivate clause, even if they are static data members.
1009  DSAVarData DVarTemp = hasDSA(
1010  D, [](OpenMPClauseKind C) -> bool { return C == OMPC_firstprivate; },
1011  MatchesAlways, FromParent);
1012  if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
1013  return DVar;
1014 
1015  DVar.CKind = OMPC_shared;
1016  return DVar;
1017  }
1018 
1019  // Explicitly specified attributes and local variables with predetermined
1020  // attributes.
1021  auto I = Stack.back().first.rbegin();
1022  auto EndI = Stack.back().first.rend();
1023  if (FromParent && I != EndI)
1024  std::advance(I, 1);
1025  if (I->SharingMap.count(D)) {
1026  DVar.RefExpr = I->SharingMap[D].RefExpr.getPointer();
1027  DVar.PrivateCopy = I->SharingMap[D].PrivateCopy;
1028  DVar.CKind = I->SharingMap[D].Attributes;
1029  DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1030  DVar.DKind = I->Directive;
1031  }
1032 
1033  return DVar;
1034 }
1035 
1036 DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1037  bool FromParent) {
1038  if (isStackEmpty()) {
1039  StackTy::reverse_iterator I;
1040  return getDSA(I, D);
1041  }
1042  D = getCanonicalDecl(D);
1043  auto StartI = Stack.back().first.rbegin();
1044  auto EndI = Stack.back().first.rend();
1045  if (FromParent && StartI != EndI)
1046  std::advance(StartI, 1);
1047  return getDSA(StartI, D);
1048 }
1049 
1050 DSAStackTy::DSAVarData
1051 DSAStackTy::hasDSA(ValueDecl *D,
1052  const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
1053  const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
1054  bool FromParent) {
1055  if (isStackEmpty())
1056  return {};
1057  D = getCanonicalDecl(D);
1058  auto I = Stack.back().first.rbegin();
1059  auto EndI = Stack.back().first.rend();
1060  if (FromParent && I != EndI)
1061  std::advance(I, 1);
1062  for (; I != EndI; std::advance(I, 1)) {
1063  if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
1064  continue;
1065  auto NewI = I;
1066  DSAVarData DVar = getDSA(NewI, D);
1067  if (I == NewI && CPred(DVar.CKind))
1068  return DVar;
1069  }
1070  return {};
1071 }
1072 
1073 DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
1074  ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
1075  const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
1076  bool FromParent) {
1077  if (isStackEmpty())
1078  return {};
1079  D = getCanonicalDecl(D);
1080  auto StartI = Stack.back().first.rbegin();
1081  auto EndI = Stack.back().first.rend();
1082  if (FromParent && StartI != EndI)
1083  std::advance(StartI, 1);
1084  if (StartI == EndI || !DPred(StartI->Directive))
1085  return {};
1086  auto NewI = StartI;
1087  DSAVarData DVar = getDSA(NewI, D);
1088  return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData();
1089 }
1090 
1091 bool DSAStackTy::hasExplicitDSA(
1092  ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
1093  unsigned Level, bool NotLastprivate) {
1094  if (CPred(ClauseKindMode))
1095  return true;
1096  if (isStackEmpty())
1097  return false;
1098  D = getCanonicalDecl(D);
1099  auto StartI = Stack.back().first.begin();
1100  auto EndI = Stack.back().first.end();
1101  if (std::distance(StartI, EndI) <= (int)Level)
1102  return false;
1103  std::advance(StartI, Level);
1104  return (StartI->SharingMap.count(D) > 0) &&
1105  StartI->SharingMap[D].RefExpr.getPointer() &&
1106  CPred(StartI->SharingMap[D].Attributes) &&
1107  (!NotLastprivate || !StartI->SharingMap[D].RefExpr.getInt());
1108 }
1109 
1110 bool DSAStackTy::hasExplicitDirective(
1111  const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
1112  unsigned Level) {
1113  if (isStackEmpty())
1114  return false;
1115  auto StartI = Stack.back().first.begin();
1116  auto EndI = Stack.back().first.end();
1117  if (std::distance(StartI, EndI) <= (int)Level)
1118  return false;
1119  std::advance(StartI, Level);
1120  return DPred(StartI->Directive);
1121 }
1122 
1123 bool DSAStackTy::hasDirective(
1124  const llvm::function_ref<bool(OpenMPDirectiveKind,
1126  &DPred,
1127  bool FromParent) {
1128  // We look only in the enclosing region.
1129  if (isStackEmpty())
1130  return false;
1131  auto StartI = std::next(Stack.back().first.rbegin());
1132  auto EndI = Stack.back().first.rend();
1133  if (FromParent && StartI != EndI)
1134  StartI = std::next(StartI);
1135  for (auto I = StartI, EE = EndI; I != EE; ++I) {
1136  if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
1137  return true;
1138  }
1139  return false;
1140 }
1141 
1142 void Sema::InitDataSharingAttributesStack() {
1143  VarDataSharingAttributesStack = new DSAStackTy(*this);
1144 }
1145 
1146 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
1147 
1148 void Sema::pushOpenMPFunctionRegion() {
1149  DSAStack->pushFunction();
1150 }
1151 
1152 void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
1153  DSAStack->popFunction(OldFSI);
1154 }
1155 
1157  assert(LangOpts.OpenMP && "OpenMP is not allowed");
1158 
1159  auto &Ctx = getASTContext();
1160  bool IsByRef = true;
1161 
1162  // Find the directive that is associated with the provided scope.
1163  D = cast<ValueDecl>(D->getCanonicalDecl());
1164  auto Ty = D->getType();
1165 
1166  if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
1167  // This table summarizes how a given variable should be passed to the device
1168  // given its type and the clauses where it appears. This table is based on
1169  // the description in OpenMP 4.5 [2.10.4, target Construct] and
1170  // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
1171  //
1172  // =========================================================================
1173  // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
1174  // | |(tofrom:scalar)| | pvt | | | |
1175  // =========================================================================
1176  // | scl | | | | - | | bycopy|
1177  // | scl | | - | x | - | - | bycopy|
1178  // | scl | | x | - | - | - | null |
1179  // | scl | x | | | - | | byref |
1180  // | scl | x | - | x | - | - | bycopy|
1181  // | scl | x | x | - | - | - | null |
1182  // | scl | | - | - | - | x | byref |
1183  // | scl | x | - | - | - | x | byref |
1184  //
1185  // | agg | n.a. | | | - | | byref |
1186  // | agg | n.a. | - | x | - | - | byref |
1187  // | agg | n.a. | x | - | - | - | null |
1188  // | agg | n.a. | - | - | - | x | byref |
1189  // | agg | n.a. | - | - | - | x[] | byref |
1190  //
1191  // | ptr | n.a. | | | - | | bycopy|
1192  // | ptr | n.a. | - | x | - | - | bycopy|
1193  // | ptr | n.a. | x | - | - | - | null |
1194  // | ptr | n.a. | - | - | - | x | byref |
1195  // | ptr | n.a. | - | - | - | x[] | bycopy|
1196  // | ptr | n.a. | - | - | x | | bycopy|
1197  // | ptr | n.a. | - | - | x | x | bycopy|
1198  // | ptr | n.a. | - | - | x | x[] | bycopy|
1199  // =========================================================================
1200  // Legend:
1201  // scl - scalar
1202  // ptr - pointer
1203  // agg - aggregate
1204  // x - applies
1205  // - - invalid in this combination
1206  // [] - mapped with an array section
1207  // byref - should be mapped by reference
1208  // byval - should be mapped by value
1209  // null - initialize a local variable to null on the device
1210  //
1211  // Observations:
1212  // - All scalar declarations that show up in a map clause have to be passed
1213  // by reference, because they may have been mapped in the enclosing data
1214  // environment.
1215  // - If the scalar value does not fit the size of uintptr, it has to be
1216  // passed by reference, regardless the result in the table above.
1217  // - For pointers mapped by value that have either an implicit map or an
1218  // array section, the runtime library may pass the NULL value to the
1219  // device instead of the value passed to it by the compiler.
1220 
1221  if (Ty->isReferenceType())
1222  Ty = Ty->castAs<ReferenceType>()->getPointeeType();
1223 
1224  // Locate map clauses and see if the variable being captured is referred to
1225  // in any of those clauses. Here we only care about variables, not fields,
1226  // because fields are part of aggregates.
1227  bool IsVariableUsedInMapClause = false;
1228  bool IsVariableAssociatedWithSection = false;
1229 
1230  DSAStack->checkMappableExprComponentListsForDeclAtLevel(
1232  MapExprComponents,
1233  OpenMPClauseKind WhereFoundClauseKind) {
1234  // Only the map clause information influences how a variable is
1235  // captured. E.g. is_device_ptr does not require changing the default
1236  // behavior.
1237  if (WhereFoundClauseKind != OMPC_map)
1238  return false;
1239 
1240  auto EI = MapExprComponents.rbegin();
1241  auto EE = MapExprComponents.rend();
1242 
1243  assert(EI != EE && "Invalid map expression!");
1244 
1245  if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
1246  IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
1247 
1248  ++EI;
1249  if (EI == EE)
1250  return false;
1251 
1252  if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
1253  isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
1254  isa<MemberExpr>(EI->getAssociatedExpression())) {
1255  IsVariableAssociatedWithSection = true;
1256  // There is nothing more we need to know about this variable.
1257  return true;
1258  }
1259 
1260  // Keep looking for more map info.
1261  return false;
1262  });
1263 
1264  if (IsVariableUsedInMapClause) {
1265  // If variable is identified in a map clause it is always captured by
1266  // reference except if it is a pointer that is dereferenced somehow.
1267  IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
1268  } else {
1269  // By default, all the data that has a scalar type is mapped by copy.
1270  IsByRef = !Ty->isScalarType() ||
1271  DSAStack->getDefaultDMAAtLevel(Level) == DMA_tofrom_scalar;
1272  }
1273  }
1274 
1275  if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
1276  IsByRef = !DSAStack->hasExplicitDSA(
1277  D, [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
1278  Level, /*NotLastprivate=*/true);
1279  }
1280 
1281  // When passing data by copy, we need to make sure it fits the uintptr size
1282  // and alignment, because the runtime library only deals with uintptr types.
1283  // If it does not fit the uintptr size, we need to pass the data by reference
1284  // instead.
1285  if (!IsByRef &&
1286  (Ctx.getTypeSizeInChars(Ty) >
1287  Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
1288  Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
1289  IsByRef = true;
1290  }
1291 
1292  return IsByRef;
1293 }
1294 
1295 unsigned Sema::getOpenMPNestingLevel() const {
1296  assert(getLangOpts().OpenMP);
1297  return DSAStack->getNestingLevel();
1298 }
1299 
1301  assert(LangOpts.OpenMP && "OpenMP is not allowed");
1302  D = getCanonicalDecl(D);
1303 
1304  // If we are attempting to capture a global variable in a directive with
1305  // 'target' we return true so that this global is also mapped to the device.
1306  //
1307  // FIXME: If the declaration is enclosed in a 'declare target' directive,
1308  // then it should not be captured. Therefore, an extra check has to be
1309  // inserted here once support for 'declare target' is added.
1310  //
1311  auto *VD = dyn_cast<VarDecl>(D);
1312  if (VD && !VD->hasLocalStorage()) {
1313  if (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) &&
1314  !DSAStack->isClauseParsingMode())
1315  return VD;
1316  if (DSAStack->hasDirective(
1318  SourceLocation) -> bool {
1319  return isOpenMPTargetExecutionDirective(K);
1320  },
1321  false))
1322  return VD;
1323  }
1324 
1325  if (DSAStack->getCurrentDirective() != OMPD_unknown &&
1326  (!DSAStack->isClauseParsingMode() ||
1327  DSAStack->getParentDirective() != OMPD_unknown)) {
1328  auto &&Info = DSAStack->isLoopControlVariable(D);
1329  if (Info.first ||
1330  (VD && VD->hasLocalStorage() &&
1331  isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
1332  (VD && DSAStack->isForceVarCapturing()))
1333  return VD ? VD : Info.second;
1334  auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
1335  if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
1336  return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
1337  DVarPrivate = DSAStack->hasDSA(
1338  D, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
1339  DSAStack->isClauseParsingMode());
1340  if (DVarPrivate.CKind != OMPC_unknown)
1341  return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
1342  }
1343  return nullptr;
1344 }
1345 
1347  assert(LangOpts.OpenMP && "OpenMP is not allowed");
1348  return DSAStack->hasExplicitDSA(
1349  D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; },
1350  Level) ||
1351  // Consider taskgroup reduction descriptor variable a private to avoid
1352  // possible capture in the region.
1353  (DSAStack->hasExplicitDirective(
1354  [](OpenMPDirectiveKind K) { return K == OMPD_taskgroup; },
1355  Level) &&
1356  DSAStack->isTaskgroupReductionRef(D, Level));
1357 }
1358 
1360  assert(LangOpts.OpenMP && "OpenMP is not allowed");
1361  D = getCanonicalDecl(D);
1363  for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) {
1364  const unsigned NewLevel = I - 1;
1365  if (DSAStack->hasExplicitDSA(D,
1366  [&OMPC](const OpenMPClauseKind K) {
1367  if (isOpenMPPrivate(K)) {
1368  OMPC = K;
1369  return true;
1370  }
1371  return false;
1372  },
1373  NewLevel))
1374  break;
1375  if (DSAStack->checkMappableExprComponentListsForDeclAtLevel(
1376  D, NewLevel,
1378  OpenMPClauseKind) { return true; })) {
1379  OMPC = OMPC_map;
1380  break;
1381  }
1382  if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1383  NewLevel)) {
1384  OMPC = OMPC_firstprivate;
1385  break;
1386  }
1387  }
1388  if (OMPC != OMPC_unknown)
1389  FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, OMPC));
1390 }
1391 
1393  assert(LangOpts.OpenMP && "OpenMP is not allowed");
1394  // Return true if the current level is no longer enclosed in a target region.
1395 
1396  auto *VD = dyn_cast<VarDecl>(D);
1397  return VD && !VD->hasLocalStorage() &&
1398  DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1399  Level);
1400 }
1401 
1402 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
1403 
1405  const DeclarationNameInfo &DirName,
1406  Scope *CurScope, SourceLocation Loc) {
1407  DSAStack->push(DKind, DirName, CurScope, Loc);
1408  PushExpressionEvaluationContext(
1409  ExpressionEvaluationContext::PotentiallyEvaluated);
1410 }
1411 
1413  DSAStack->setClauseParsingMode(K);
1414 }
1415 
1417  DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
1418 }
1419 
1420 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
1421  // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1422  // A variable of class type (or array thereof) that appears in a lastprivate
1423  // clause requires an accessible, unambiguous default constructor for the
1424  // class type, unless the list item is also specified in a firstprivate
1425  // clause.
1426  if (auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
1427  for (auto *C : D->clauses()) {
1428  if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1429  SmallVector<Expr *, 8> PrivateCopies;
1430  for (auto *DE : Clause->varlists()) {
1431  if (DE->isValueDependent() || DE->isTypeDependent()) {
1432  PrivateCopies.push_back(nullptr);
1433  continue;
1434  }
1435  auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
1436  VarDecl *VD = cast<VarDecl>(DRE->getDecl());
1437  QualType Type = VD->getType().getNonReferenceType();
1438  auto DVar = DSAStack->getTopDSA(VD, false);
1439  if (DVar.CKind == OMPC_lastprivate) {
1440  // Generate helper private variable and initialize it with the
1441  // default value. The address of the original variable is replaced
1442  // by the address of the new private variable in CodeGen. This new
1443  // variable is not added to IdResolver, so the code in the OpenMP
1444  // region uses original variable for proper diagnostics.
1445  auto *VDPrivate = buildVarDecl(
1446  *this, DE->getExprLoc(), Type.getUnqualifiedType(),
1447  VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
1448  ActOnUninitializedDecl(VDPrivate);
1449  if (VDPrivate->isInvalidDecl())
1450  continue;
1451  PrivateCopies.push_back(buildDeclRefExpr(
1452  *this, VDPrivate, DE->getType(), DE->getExprLoc()));
1453  } else {
1454  // The variable is also a firstprivate, so initialization sequence
1455  // for private copy is generated already.
1456  PrivateCopies.push_back(nullptr);
1457  }
1458  }
1459  // Set initializers to private copies if no errors were found.
1460  if (PrivateCopies.size() == Clause->varlist_size())
1461  Clause->setPrivateCopies(PrivateCopies);
1462  }
1463  }
1464  }
1465 
1466  DSAStack->pop();
1467  DiscardCleanupsInEvaluationContext();
1468  PopExpressionEvaluationContext();
1469 }
1470 
1471 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1472  Expr *NumIterations, Sema &SemaRef,
1473  Scope *S, DSAStackTy *Stack);
1474 
1475 namespace {
1476 
1477 class VarDeclFilterCCC : public CorrectionCandidateCallback {
1478 private:
1479  Sema &SemaRef;
1480 
1481 public:
1482  explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
1483  bool ValidateCandidate(const TypoCorrection &Candidate) override {
1484  NamedDecl *ND = Candidate.getCorrectionDecl();
1485  if (auto *VD = dyn_cast_or_null<VarDecl>(ND)) {
1486  return VD->hasGlobalStorage() &&
1487  SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1488  SemaRef.getCurScope());
1489  }
1490  return false;
1491  }
1492 };
1493 
1494 class VarOrFuncDeclFilterCCC : public CorrectionCandidateCallback {
1495 private:
1496  Sema &SemaRef;
1497 
1498 public:
1499  explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
1500  bool ValidateCandidate(const TypoCorrection &Candidate) override {
1501  NamedDecl *ND = Candidate.getCorrectionDecl();
1502  if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
1503  return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1504  SemaRef.getCurScope());
1505  }
1506  return false;
1507  }
1508 };
1509 
1510 } // namespace
1511 
1513  CXXScopeSpec &ScopeSpec,
1514  const DeclarationNameInfo &Id) {
1515  LookupResult Lookup(*this, Id, LookupOrdinaryName);
1516  LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1517 
1518  if (Lookup.isAmbiguous())
1519  return ExprError();
1520 
1521  VarDecl *VD;
1522  if (!Lookup.isSingleResult()) {
1523  if (TypoCorrection Corrected = CorrectTypo(
1524  Id, LookupOrdinaryName, CurScope, nullptr,
1525  llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
1526  diagnoseTypo(Corrected,
1527  PDiag(Lookup.empty()
1528  ? diag::err_undeclared_var_use_suggest
1529  : diag::err_omp_expected_var_arg_suggest)
1530  << Id.getName());
1531  VD = Corrected.getCorrectionDeclAs<VarDecl>();
1532  } else {
1533  Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1534  : diag::err_omp_expected_var_arg)
1535  << Id.getName();
1536  return ExprError();
1537  }
1538  } else {
1539  if (!(VD = Lookup.getAsSingle<VarDecl>())) {
1540  Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
1541  Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1542  return ExprError();
1543  }
1544  }
1545  Lookup.suppressDiagnostics();
1546 
1547  // OpenMP [2.9.2, Syntax, C/C++]
1548  // Variables must be file-scope, namespace-scope, or static block-scope.
1549  if (!VD->hasGlobalStorage()) {
1550  Diag(Id.getLoc(), diag::err_omp_global_var_arg)
1551  << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1552  bool IsDecl =
1554  Diag(VD->getLocation(),
1555  IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1556  << VD;
1557  return ExprError();
1558  }
1559 
1560  VarDecl *CanonicalVD = VD->getCanonicalDecl();
1561  NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
1562  // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1563  // A threadprivate directive for file-scope variables must appear outside
1564  // any definition or declaration.
1565  if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1566  !getCurLexicalContext()->isTranslationUnit()) {
1567  Diag(Id.getLoc(), diag::err_omp_var_scope)
1568  << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1569  bool IsDecl =
1571  Diag(VD->getLocation(),
1572  IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1573  << VD;
1574  return ExprError();
1575  }
1576  // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1577  // A threadprivate directive for static class member variables must appear
1578  // in the class definition, in the same scope in which the member
1579  // variables are declared.
1580  if (CanonicalVD->isStaticDataMember() &&
1581  !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1582  Diag(Id.getLoc(), diag::err_omp_var_scope)
1583  << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1584  bool IsDecl =
1586  Diag(VD->getLocation(),
1587  IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1588  << VD;
1589  return ExprError();
1590  }
1591  // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1592  // A threadprivate directive for namespace-scope variables must appear
1593  // outside any definition or declaration other than the namespace
1594  // definition itself.
1595  if (CanonicalVD->getDeclContext()->isNamespace() &&
1596  (!getCurLexicalContext()->isFileContext() ||
1597  !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1598  Diag(Id.getLoc(), diag::err_omp_var_scope)
1599  << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1600  bool IsDecl =
1602  Diag(VD->getLocation(),
1603  IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1604  << VD;
1605  return ExprError();
1606  }
1607  // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1608  // A threadprivate directive for static block-scope variables must appear
1609  // in the scope of the variable and not in a nested scope.
1610  if (CanonicalVD->isStaticLocal() && CurScope &&
1611  !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
1612  Diag(Id.getLoc(), diag::err_omp_var_scope)
1613  << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1614  bool IsDecl =
1616  Diag(VD->getLocation(),
1617  IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1618  << VD;
1619  return ExprError();
1620  }
1621 
1622  // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1623  // A threadprivate directive must lexically precede all references to any
1624  // of the variables in its list.
1625  if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
1626  Diag(Id.getLoc(), diag::err_omp_var_used)
1627  << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1628  return ExprError();
1629  }
1630 
1631  QualType ExprType = VD->getType().getNonReferenceType();
1632  return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1633  SourceLocation(), VD,
1634  /*RefersToEnclosingVariableOrCapture=*/false,
1635  Id.getLoc(), ExprType, VK_LValue);
1636 }
1637 
1640  ArrayRef<Expr *> VarList) {
1641  if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
1642  CurContext->addDecl(D);
1643  return DeclGroupPtrTy::make(DeclGroupRef(D));
1644  }
1645  return nullptr;
1646 }
1647 
1648 namespace {
1649 class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1650  Sema &SemaRef;
1651 
1652 public:
1653  bool VisitDeclRefExpr(const DeclRefExpr *E) {
1654  if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
1655  if (VD->hasLocalStorage()) {
1656  SemaRef.Diag(E->getLocStart(),
1657  diag::err_omp_local_var_in_threadprivate_init)
1658  << E->getSourceRange();
1659  SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1660  << VD << VD->getSourceRange();
1661  return true;
1662  }
1663  }
1664  return false;
1665  }
1666  bool VisitStmt(const Stmt *S) {
1667  for (auto Child : S->children()) {
1668  if (Child && Visit(Child))
1669  return true;
1670  }
1671  return false;
1672  }
1673  explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
1674 };
1675 } // namespace
1676 
1680  for (auto &RefExpr : VarList) {
1681  DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
1682  VarDecl *VD = cast<VarDecl>(DE->getDecl());
1683  SourceLocation ILoc = DE->getExprLoc();
1684 
1685  // Mark variable as used.
1686  VD->setReferenced();
1687  VD->markUsed(Context);
1688 
1689  QualType QType = VD->getType();
1690  if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1691  // It will be analyzed later.
1692  Vars.push_back(DE);
1693  continue;
1694  }
1695 
1696  // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1697  // A threadprivate variable must not have an incomplete type.
1698  if (RequireCompleteType(ILoc, VD->getType(),
1699  diag::err_omp_threadprivate_incomplete_type)) {
1700  continue;
1701  }
1702 
1703  // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1704  // A threadprivate variable must not have a reference type.
1705  if (VD->getType()->isReferenceType()) {
1706  Diag(ILoc, diag::err_omp_ref_type_arg)
1707  << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1708  bool IsDecl =
1709  VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1710  Diag(VD->getLocation(),
1711  IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1712  << VD;
1713  continue;
1714  }
1715 
1716  // Check if this is a TLS variable. If TLS is not being supported, produce
1717  // the corresponding diagnostic.
1718  if ((VD->getTLSKind() != VarDecl::TLS_None &&
1719  !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1720  getLangOpts().OpenMPUseTLS &&
1721  getASTContext().getTargetInfo().isTLSSupported())) ||
1722  (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1723  !VD->isLocalVarDecl())) {
1724  Diag(ILoc, diag::err_omp_var_thread_local)
1725  << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
1726  bool IsDecl =
1727  VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1728  Diag(VD->getLocation(),
1729  IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1730  << VD;
1731  continue;
1732  }
1733 
1734  // Check if initial value of threadprivate variable reference variable with
1735  // local storage (it is not supported by runtime).
1736  if (auto Init = VD->getAnyInitializer()) {
1737  LocalVarRefChecker Checker(*this);
1738  if (Checker.Visit(Init))
1739  continue;
1740  }
1741 
1742  Vars.push_back(RefExpr);
1743  DSAStack->addDSA(VD, DE, OMPC_threadprivate);
1744  VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1745  Context, SourceRange(Loc, Loc)));
1746  if (auto *ML = Context.getASTMutationListener())
1747  ML->DeclarationMarkedOpenMPThreadPrivate(VD);
1748  }
1749  OMPThreadPrivateDecl *D = nullptr;
1750  if (!Vars.empty()) {
1751  D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1752  Vars);
1753  D->setAccess(AS_public);
1754  }
1755  return D;
1756 }
1757 
1758 static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
1759  const ValueDecl *D, DSAStackTy::DSAVarData DVar,
1760  bool IsLoopIterVar = false) {
1761  if (DVar.RefExpr) {
1762  SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1763  << getOpenMPClauseName(DVar.CKind);
1764  return;
1765  }
1766  enum {
1767  PDSA_StaticMemberShared,
1768  PDSA_StaticLocalVarShared,
1769  PDSA_LoopIterVarPrivate,
1770  PDSA_LoopIterVarLinear,
1771  PDSA_LoopIterVarLastprivate,
1772  PDSA_ConstVarShared,
1773  PDSA_GlobalVarShared,
1774  PDSA_TaskVarFirstprivate,
1775  PDSA_LocalVarPrivate,
1776  PDSA_Implicit
1777  } Reason = PDSA_Implicit;
1778  bool ReportHint = false;
1779  auto ReportLoc = D->getLocation();
1780  auto *VD = dyn_cast<VarDecl>(D);
1781  if (IsLoopIterVar) {
1782  if (DVar.CKind == OMPC_private)
1783  Reason = PDSA_LoopIterVarPrivate;
1784  else if (DVar.CKind == OMPC_lastprivate)
1785  Reason = PDSA_LoopIterVarLastprivate;
1786  else
1787  Reason = PDSA_LoopIterVarLinear;
1788  } else if (isOpenMPTaskingDirective(DVar.DKind) &&
1789  DVar.CKind == OMPC_firstprivate) {
1790  Reason = PDSA_TaskVarFirstprivate;
1791  ReportLoc = DVar.ImplicitDSALoc;
1792  } else if (VD && VD->isStaticLocal())
1793  Reason = PDSA_StaticLocalVarShared;
1794  else if (VD && VD->isStaticDataMember())
1795  Reason = PDSA_StaticMemberShared;
1796  else if (VD && VD->isFileVarDecl())
1797  Reason = PDSA_GlobalVarShared;
1798  else if (D->getType().isConstant(SemaRef.getASTContext()))
1799  Reason = PDSA_ConstVarShared;
1800  else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
1801  ReportHint = true;
1802  Reason = PDSA_LocalVarPrivate;
1803  }
1804  if (Reason != PDSA_Implicit) {
1805  SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
1806  << Reason << ReportHint
1807  << getOpenMPDirectiveName(Stack->getCurrentDirective());
1808  } else if (DVar.ImplicitDSALoc.isValid()) {
1809  SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1810  << getOpenMPClauseName(DVar.CKind);
1811  }
1812 }
1813 
1814 namespace {
1815 class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1816  DSAStackTy *Stack;
1817  Sema &SemaRef;
1818  bool ErrorFound;
1819  CapturedStmt *CS;
1820  llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
1821  llvm::SmallVector<Expr *, 8> ImplicitMap;
1822  llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
1823  llvm::DenseSet<ValueDecl *> ImplicitDeclarations;
1824 
1825 public:
1826  void VisitDeclRefExpr(DeclRefExpr *E) {
1827  if (E->isTypeDependent() || E->isValueDependent() ||
1829  return;
1830  if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
1831  VD = VD->getCanonicalDecl();
1832  // Skip internally declared variables.
1833  if (VD->hasLocalStorage() && !CS->capturesVariable(VD))
1834  return;
1835 
1836  auto DVar = Stack->getTopDSA(VD, false);
1837  // Check if the variable has explicit DSA set and stop analysis if it so.
1838  if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second)
1839  return;
1840 
1841  // Skip internally declared static variables.
1842  if (VD->hasGlobalStorage() && !CS->capturesVariable(VD))
1843  return;
1844 
1845  auto ELoc = E->getExprLoc();
1846  auto DKind = Stack->getCurrentDirective();
1847  // The default(none) clause requires that each variable that is referenced
1848  // in the construct, and does not have a predetermined data-sharing
1849  // attribute, must have its data-sharing attribute explicitly determined
1850  // by being listed in a data-sharing attribute clause.
1851  if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
1852  isParallelOrTaskRegion(DKind) &&
1853  VarsWithInheritedDSA.count(VD) == 0) {
1854  VarsWithInheritedDSA[VD] = E;
1855  return;
1856  }
1857 
1858  if (isOpenMPTargetExecutionDirective(DKind) &&
1859  !Stack->isLoopControlVariable(VD).first) {
1860  if (!Stack->checkMappableExprComponentListsForDecl(
1861  VD, /*CurrentRegionOnly=*/true,
1863  StackComponents,
1864  OpenMPClauseKind) {
1865  // Variable is used if it has been marked as an array, array
1866  // section or the variable iself.
1867  return StackComponents.size() == 1 ||
1868  std::all_of(
1869  std::next(StackComponents.rbegin()),
1870  StackComponents.rend(),
1871  [](const OMPClauseMappableExprCommon::
1872  MappableComponent &MC) {
1873  return MC.getAssociatedDeclaration() ==
1874  nullptr &&
1875  (isa<OMPArraySectionExpr>(
1876  MC.getAssociatedExpression()) ||
1877  isa<ArraySubscriptExpr>(
1878  MC.getAssociatedExpression()));
1879  });
1880  })) {
1881  bool IsFirstprivate = false;
1882  // By default lambdas are captured as firstprivates.
1883  if (const auto *RD =
1884  VD->getType().getNonReferenceType()->getAsCXXRecordDecl())
1885  IsFirstprivate = RD->isLambda();
1886  IsFirstprivate =
1887  IsFirstprivate ||
1888  (VD->getType().getNonReferenceType()->isScalarType() &&
1889  Stack->getDefaultDMA() != DMA_tofrom_scalar);
1890  if (IsFirstprivate)
1891  ImplicitFirstprivate.emplace_back(E);
1892  else
1893  ImplicitMap.emplace_back(E);
1894  return;
1895  }
1896  }
1897 
1898  // OpenMP [2.9.3.6, Restrictions, p.2]
1899  // A list item that appears in a reduction clause of the innermost
1900  // enclosing worksharing or parallel construct may not be accessed in an
1901  // explicit task.
1902  DVar = Stack->hasInnermostDSA(
1903  VD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
1904  [](OpenMPDirectiveKind K) -> bool {
1905  return isOpenMPParallelDirective(K) ||
1907  },
1908  /*FromParent=*/true);
1909  if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
1910  ErrorFound = true;
1911  SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1912  ReportOriginalDSA(SemaRef, Stack, VD, DVar);
1913  return;
1914  }
1915 
1916  // Define implicit data-sharing attributes for task.
1917  DVar = Stack->getImplicitDSA(VD, false);
1918  if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1919  !Stack->isLoopControlVariable(VD).first)
1920  ImplicitFirstprivate.push_back(E);
1921  }
1922  }
1923  void VisitMemberExpr(MemberExpr *E) {
1924  if (E->isTypeDependent() || E->isValueDependent() ||
1926  return;
1927  auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
1928  if (!FD)
1929  return;
1930  OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
1931  if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1932  auto DVar = Stack->getTopDSA(FD, false);
1933  // Check if the variable has explicit DSA set and stop analysis if it
1934  // so.
1935  if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second)
1936  return;
1937 
1938  if (isOpenMPTargetExecutionDirective(DKind) &&
1939  !Stack->isLoopControlVariable(FD).first &&
1940  !Stack->checkMappableExprComponentListsForDecl(
1941  FD, /*CurrentRegionOnly=*/true,
1943  StackComponents,
1944  OpenMPClauseKind) {
1945  return isa<CXXThisExpr>(
1946  cast<MemberExpr>(
1947  StackComponents.back().getAssociatedExpression())
1948  ->getBase()
1949  ->IgnoreParens());
1950  })) {
1951  // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
1952  // A bit-field cannot appear in a map clause.
1953  //
1954  if (FD->isBitField()) {
1955  SemaRef.Diag(E->getMemberLoc(),
1956  diag::err_omp_bit_fields_forbidden_in_clause)
1957  << E->getSourceRange() << getOpenMPClauseName(OMPC_map);
1958  return;
1959  }
1960  ImplicitMap.emplace_back(E);
1961  return;
1962  }
1963 
1964  auto ELoc = E->getExprLoc();
1965  // OpenMP [2.9.3.6, Restrictions, p.2]
1966  // A list item that appears in a reduction clause of the innermost
1967  // enclosing worksharing or parallel construct may not be accessed in
1968  // an explicit task.
1969  DVar = Stack->hasInnermostDSA(
1970  FD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
1971  [](OpenMPDirectiveKind K) -> bool {
1972  return isOpenMPParallelDirective(K) ||
1974  },
1975  /*FromParent=*/true);
1976  if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
1977  ErrorFound = true;
1978  SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1979  ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1980  return;
1981  }
1982 
1983  // Define implicit data-sharing attributes for task.
1984  DVar = Stack->getImplicitDSA(FD, false);
1985  if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1986  !Stack->isLoopControlVariable(FD).first)
1987  ImplicitFirstprivate.push_back(E);
1988  return;
1989  }
1990  if (isOpenMPTargetExecutionDirective(DKind) && !FD->isBitField()) {
1992  CheckMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map);
1993  auto *VD = cast<ValueDecl>(
1994  CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
1995  if (!Stack->checkMappableExprComponentListsForDecl(
1996  VD, /*CurrentRegionOnly=*/true,
1997  [&CurComponents](
1999  StackComponents,
2000  OpenMPClauseKind) {
2001  auto CCI = CurComponents.rbegin();
2002  auto CCE = CurComponents.rend();
2003  for (const auto &SC : llvm::reverse(StackComponents)) {
2004  // Do both expressions have the same kind?
2005  if (CCI->getAssociatedExpression()->getStmtClass() !=
2006  SC.getAssociatedExpression()->getStmtClass())
2007  if (!(isa<OMPArraySectionExpr>(
2008  SC.getAssociatedExpression()) &&
2009  isa<ArraySubscriptExpr>(
2010  CCI->getAssociatedExpression())))
2011  return false;
2012 
2013  Decl *CCD = CCI->getAssociatedDeclaration();
2014  Decl *SCD = SC.getAssociatedDeclaration();
2015  CCD = CCD ? CCD->getCanonicalDecl() : nullptr;
2016  SCD = SCD ? SCD->getCanonicalDecl() : nullptr;
2017  if (SCD != CCD)
2018  return false;
2019  std::advance(CCI, 1);
2020  if (CCI == CCE)
2021  break;
2022  }
2023  return true;
2024  })) {
2025  Visit(E->getBase());
2026  }
2027  } else
2028  Visit(E->getBase());
2029  }
2030  void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
2031  for (auto *C : S->clauses()) {
2032  // Skip analysis of arguments of implicitly defined firstprivate clause
2033  // for task|target directives.
2034  // Skip analysis of arguments of implicitly defined map clause for target
2035  // directives.
2036  if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) &&
2037  C->isImplicit())) {
2038  for (auto *CC : C->children()) {
2039  if (CC)
2040  Visit(CC);
2041  }
2042  }
2043  }
2044  }
2045  void VisitStmt(Stmt *S) {
2046  for (auto *C : S->children()) {
2047  if (C && !isa<OMPExecutableDirective>(C))
2048  Visit(C);
2049  }
2050  }
2051 
2052  bool isErrorFound() { return ErrorFound; }
2053  ArrayRef<Expr *> getImplicitFirstprivate() const {
2054  return ImplicitFirstprivate;
2055  }
2056  ArrayRef<Expr *> getImplicitMap() const { return ImplicitMap; }
2057  llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
2058  return VarsWithInheritedDSA;
2059  }
2060 
2061  DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
2062  : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
2063 };
2064 } // namespace
2065 
2067  switch (DKind) {
2068  case OMPD_parallel:
2069  case OMPD_parallel_for:
2070  case OMPD_parallel_for_simd:
2071  case OMPD_parallel_sections:
2072  case OMPD_teams:
2073  case OMPD_teams_distribute: {
2074  QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
2075  QualType KmpInt32PtrTy =
2076  Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2077  Sema::CapturedParamNameType Params[] = {
2078  std::make_pair(".global_tid.", KmpInt32PtrTy),
2079  std::make_pair(".bound_tid.", KmpInt32PtrTy),
2080  std::make_pair(StringRef(), QualType()) // __context with shared vars
2081  };
2082  ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2083  Params);
2084  break;
2085  }
2086  case OMPD_target_teams:
2087  case OMPD_target_parallel: {
2088  Sema::CapturedParamNameType ParamsTarget[] = {
2089  std::make_pair(StringRef(), QualType()) // __context with shared vars
2090  };
2091  // Start a captured region for 'target' with no implicit parameters.
2092  ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2093  ParamsTarget);
2094  QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
2095  QualType KmpInt32PtrTy =
2096  Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2097  Sema::CapturedParamNameType ParamsTeamsOrParallel[] = {
2098  std::make_pair(".global_tid.", KmpInt32PtrTy),
2099  std::make_pair(".bound_tid.", KmpInt32PtrTy),
2100  std::make_pair(StringRef(), QualType()) // __context with shared vars
2101  };
2102  // Start a captured region for 'teams' or 'parallel'. Both regions have
2103  // the same implicit parameters.
2104  ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2105  ParamsTeamsOrParallel);
2106  break;
2107  }
2108  case OMPD_simd:
2109  case OMPD_for:
2110  case OMPD_for_simd:
2111  case OMPD_sections:
2112  case OMPD_section:
2113  case OMPD_single:
2114  case OMPD_master:
2115  case OMPD_critical:
2116  case OMPD_taskgroup:
2117  case OMPD_distribute:
2118  case OMPD_ordered:
2119  case OMPD_atomic:
2120  case OMPD_target_data:
2121  case OMPD_target:
2122  case OMPD_target_parallel_for:
2123  case OMPD_target_parallel_for_simd:
2124  case OMPD_target_simd: {
2125  Sema::CapturedParamNameType Params[] = {
2126  std::make_pair(StringRef(), QualType()) // __context with shared vars
2127  };
2128  ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2129  Params);
2130  break;
2131  }
2132  case OMPD_task: {
2133  QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
2134  QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
2136  EPI.Variadic = true;
2137  QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2138  Sema::CapturedParamNameType Params[] = {
2139  std::make_pair(".global_tid.", KmpInt32Ty),
2140  std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
2141  std::make_pair(".privates.", Context.VoidPtrTy.withConst()),
2142  std::make_pair(".copy_fn.",
2143  Context.getPointerType(CopyFnType).withConst()),
2144  std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2145  std::make_pair(StringRef(), QualType()) // __context with shared vars
2146  };
2147  ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2148  Params);
2149  // Mark this captured region as inlined, because we don't use outlined
2150  // function directly.
2151  getCurCapturedRegion()->TheCapturedDecl->addAttr(
2152  AlwaysInlineAttr::CreateImplicit(
2153  Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
2154  break;
2155  }
2156  case OMPD_taskloop:
2157  case OMPD_taskloop_simd: {
2158  QualType KmpInt32Ty =
2159  Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
2160  QualType KmpUInt64Ty =
2161  Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
2162  QualType KmpInt64Ty =
2163  Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
2164  QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
2166  EPI.Variadic = true;
2167  QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2168  Sema::CapturedParamNameType Params[] = {
2169  std::make_pair(".global_tid.", KmpInt32Ty),
2170  std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
2171  std::make_pair(".privates.",
2172  Context.VoidPtrTy.withConst().withRestrict()),
2173  std::make_pair(
2174  ".copy_fn.",
2175  Context.getPointerType(CopyFnType).withConst().withRestrict()),
2176  std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2177  std::make_pair(".lb.", KmpUInt64Ty),
2178  std::make_pair(".ub.", KmpUInt64Ty), std::make_pair(".st.", KmpInt64Ty),
2179  std::make_pair(".liter.", KmpInt32Ty),
2180  std::make_pair(".reductions.",
2181  Context.VoidPtrTy.withConst().withRestrict()),
2182  std::make_pair(StringRef(), QualType()) // __context with shared vars
2183  };
2184  ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2185  Params);
2186  // Mark this captured region as inlined, because we don't use outlined
2187  // function directly.
2188  getCurCapturedRegion()->TheCapturedDecl->addAttr(
2189  AlwaysInlineAttr::CreateImplicit(
2190  Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
2191  break;
2192  }
2193  case OMPD_distribute_parallel_for_simd:
2194  case OMPD_distribute_simd:
2195  case OMPD_distribute_parallel_for:
2196  case OMPD_teams_distribute_simd:
2197  case OMPD_teams_distribute_parallel_for_simd:
2198  case OMPD_teams_distribute_parallel_for:
2199  case OMPD_target_teams_distribute:
2200  case OMPD_target_teams_distribute_parallel_for:
2201  case OMPD_target_teams_distribute_parallel_for_simd:
2202  case OMPD_target_teams_distribute_simd: {
2203  QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
2204  QualType KmpInt32PtrTy =
2205  Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2206  Sema::CapturedParamNameType Params[] = {
2207  std::make_pair(".global_tid.", KmpInt32PtrTy),
2208  std::make_pair(".bound_tid.", KmpInt32PtrTy),
2209  std::make_pair(".previous.lb.", Context.getSizeType()),
2210  std::make_pair(".previous.ub.", Context.getSizeType()),
2211  std::make_pair(StringRef(), QualType()) // __context with shared vars
2212  };
2213  ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2214  Params);
2215  break;
2216  }
2217  case OMPD_threadprivate:
2218  case OMPD_taskyield:
2219  case OMPD_barrier:
2220  case OMPD_taskwait:
2221  case OMPD_cancellation_point:
2222  case OMPD_cancel:
2223  case OMPD_flush:
2224  case OMPD_target_enter_data:
2225  case OMPD_target_exit_data:
2226  case OMPD_declare_reduction:
2227  case OMPD_declare_simd:
2228  case OMPD_declare_target:
2229  case OMPD_end_declare_target:
2230  case OMPD_target_update:
2231  llvm_unreachable("OpenMP Directive is not allowed");
2232  case OMPD_unknown:
2233  llvm_unreachable("Unknown OpenMP directive");
2234  }
2235 }
2236 
2238  SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2239  getOpenMPCaptureRegions(CaptureRegions, DKind);
2240  return CaptureRegions.size();
2241 }
2242 
2244  Expr *CaptureExpr, bool WithInit,
2245  bool AsExpression) {
2246  assert(CaptureExpr);
2247  ASTContext &C = S.getASTContext();
2248  Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
2249  QualType Ty = Init->getType();
2250  if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
2251  if (S.getLangOpts().CPlusPlus)
2252  Ty = C.getLValueReferenceType(Ty);
2253  else {
2254  Ty = C.getPointerType(Ty);
2255  ExprResult Res =
2256  S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
2257  if (!Res.isUsable())
2258  return nullptr;
2259  Init = Res.get();
2260  }
2261  WithInit = true;
2262  }
2263  auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
2264  CaptureExpr->getLocStart());
2265  if (!WithInit)
2266  CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
2267  S.CurContext->addHiddenDecl(CED);
2268  S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
2269  return CED;
2270 }
2271 
2272 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
2273  bool WithInit) {
2274  OMPCapturedExprDecl *CD;
2275  if (auto *VD = S.IsOpenMPCapturedDecl(D))
2276  CD = cast<OMPCapturedExprDecl>(VD);
2277  else
2278  CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
2279  /*AsExpression=*/false);
2280  return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
2281  CaptureExpr->getExprLoc());
2282 }
2283 
2284 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
2285  if (!Ref) {
2286  auto *CD =
2287  buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."),
2288  CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true);
2289  Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
2290  CaptureExpr->getExprLoc());
2291  }
2292  ExprResult Res = Ref;
2293  if (!S.getLangOpts().CPlusPlus &&
2294  CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
2295  Ref->getType()->isPointerType())
2296  Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
2297  if (!Res.isUsable())
2298  return ExprError();
2299  return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get());
2300 }
2301 
2302 namespace {
2303 // OpenMP directives parsed in this section are represented as a
2304 // CapturedStatement with an associated statement. If a syntax error
2305 // is detected during the parsing of the associated statement, the
2306 // compiler must abort processing and close the CapturedStatement.
2307 //
2308 // Combined directives such as 'target parallel' have more than one
2309 // nested CapturedStatements. This RAII ensures that we unwind out
2310 // of all the nested CapturedStatements when an error is found.
2311 class CaptureRegionUnwinderRAII {
2312 private:
2313  Sema &S;
2314  bool &ErrorFound;
2315  OpenMPDirectiveKind DKind;
2316 
2317 public:
2318  CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
2319  OpenMPDirectiveKind DKind)
2320  : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
2321  ~CaptureRegionUnwinderRAII() {
2322  if (ErrorFound) {
2323  int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind);
2324  while (--ThisCaptureLevel >= 0)
2326  }
2327  }
2328 };
2329 } // namespace
2330 
2332  ArrayRef<OMPClause *> Clauses) {
2333  bool ErrorFound = false;
2334  CaptureRegionUnwinderRAII CaptureRegionUnwinder(
2335  *this, ErrorFound, DSAStack->getCurrentDirective());
2336  if (!S.isUsable()) {
2337  ErrorFound = true;
2338  return StmtError();
2339  }
2340 
2341  OMPOrderedClause *OC = nullptr;
2342  OMPScheduleClause *SC = nullptr;
2345  // This is required for proper codegen.
2346  for (auto *Clause : Clauses) {
2347  if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
2348  Clause->getClauseKind() == OMPC_in_reduction) {
2349  // Capture taskgroup task_reduction descriptors inside the tasking regions
2350  // with the corresponding in_reduction items.
2351  auto *IRC = cast<OMPInReductionClause>(Clause);
2352  for (auto *E : IRC->taskgroup_descriptors())
2353  if (E)
2354  MarkDeclarationsReferencedInExpr(E);
2355  }
2356  if (isOpenMPPrivate(Clause->getClauseKind()) ||
2357  Clause->getClauseKind() == OMPC_copyprivate ||
2358  (getLangOpts().OpenMPUseTLS &&
2359  getASTContext().getTargetInfo().isTLSSupported() &&
2360  Clause->getClauseKind() == OMPC_copyin)) {
2361  DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
2362  // Mark all variables in private list clauses as used in inner region.
2363  for (auto *VarRef : Clause->children()) {
2364  if (auto *E = cast_or_null<Expr>(VarRef)) {
2365  MarkDeclarationsReferencedInExpr(E);
2366  }
2367  }
2368  DSAStack->setForceVarCapturing(/*V=*/false);
2369  } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
2370  if (auto *C = OMPClauseWithPreInit::get(Clause))
2371  PICs.push_back(C);
2372  if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
2373  if (auto *E = C->getPostUpdateExpr())
2374  MarkDeclarationsReferencedInExpr(E);
2375  }
2376  }
2377  if (Clause->getClauseKind() == OMPC_schedule)
2378  SC = cast<OMPScheduleClause>(Clause);
2379  else if (Clause->getClauseKind() == OMPC_ordered)
2380  OC = cast<OMPOrderedClause>(Clause);
2381  else if (Clause->getClauseKind() == OMPC_linear)
2382  LCs.push_back(cast<OMPLinearClause>(Clause));
2383  }
2384  // OpenMP, 2.7.1 Loop Construct, Restrictions
2385  // The nonmonotonic modifier cannot be specified if an ordered clause is
2386  // specified.
2387  if (SC &&
2388  (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
2389  SC->getSecondScheduleModifier() ==
2390  OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
2391  OC) {
2392  Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
2395  diag::err_omp_schedule_nonmonotonic_ordered)
2396  << SourceRange(OC->getLocStart(), OC->getLocEnd());
2397  ErrorFound = true;
2398  }
2399  if (!LCs.empty() && OC && OC->getNumForLoops()) {
2400  for (auto *C : LCs) {
2401  Diag(C->getLocStart(), diag::err_omp_linear_ordered)
2402  << SourceRange(OC->getLocStart(), OC->getLocEnd());
2403  }
2404  ErrorFound = true;
2405  }
2406  if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
2407  isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
2408  OC->getNumForLoops()) {
2409  Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
2410  << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
2411  ErrorFound = true;
2412  }
2413  if (ErrorFound) {
2414  return StmtError();
2415  }
2416  StmtResult SR = S;
2417  SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2418  getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
2419  for (auto ThisCaptureRegion : llvm::reverse(CaptureRegions)) {
2420  // Mark all variables in private list clauses as used in inner region.
2421  // Required for proper codegen of combined directives.
2422  // TODO: add processing for other clauses.
2423  if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
2424  for (auto *C : PICs) {
2425  OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
2426  // Find the particular capture region for the clause if the
2427  // directive is a combined one with multiple capture regions.
2428  // If the directive is not a combined one, the capture region
2429  // associated with the clause is OMPD_unknown and is generated
2430  // only once.
2431  if (CaptureRegion == ThisCaptureRegion ||
2432  CaptureRegion == OMPD_unknown) {
2433  if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
2434  for (auto *D : DS->decls())
2435  MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
2436  }
2437  }
2438  }
2439  }
2440  SR = ActOnCapturedRegionEnd(SR.get());
2441  }
2442  return SR;
2443 }
2444 
2445 static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
2446  OpenMPDirectiveKind CancelRegion,
2447  SourceLocation StartLoc) {
2448  // CancelRegion is only needed for cancel and cancellation_point.
2449  if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
2450  return false;
2451 
2452  if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
2453  CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
2454  return false;
2455 
2456  SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region)
2457  << getOpenMPDirectiveName(CancelRegion);
2458  return true;
2459 }
2460 
2461 static bool checkNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
2462  OpenMPDirectiveKind CurrentRegion,
2463  const DeclarationNameInfo &CurrentName,
2464  OpenMPDirectiveKind CancelRegion,
2465  SourceLocation StartLoc) {
2466  if (Stack->getCurScope()) {
2467  auto ParentRegion = Stack->getParentDirective();
2468  auto OffendingRegion = ParentRegion;
2469  bool NestingProhibited = false;
2470  bool CloseNesting = true;
2471  bool OrphanSeen = false;
2472  enum {
2473  NoRecommend,
2474  ShouldBeInParallelRegion,
2475  ShouldBeInOrderedRegion,
2476  ShouldBeInTargetRegion,
2477  ShouldBeInTeamsRegion
2478  } Recommend = NoRecommend;
2479  if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
2480  // OpenMP [2.16, Nesting of Regions]
2481  // OpenMP constructs may not be nested inside a simd region.
2482  // OpenMP [2.8.1,simd Construct, Restrictions]
2483  // An ordered construct with the simd clause is the only OpenMP
2484  // construct that can appear in the simd region.
2485  // Allowing a SIMD construct nested in another SIMD construct is an
2486  // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
2487  // message.
2488  SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
2489  ? diag::err_omp_prohibited_region_simd
2490  : diag::warn_omp_nesting_simd);
2491  return CurrentRegion != OMPD_simd;
2492  }
2493  if (ParentRegion == OMPD_atomic) {
2494  // OpenMP [2.16, Nesting of Regions]
2495  // OpenMP constructs may not be nested inside an atomic region.
2496  SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2497  return true;
2498  }
2499  if (CurrentRegion == OMPD_section) {
2500  // OpenMP [2.7.2, sections Construct, Restrictions]
2501  // Orphaned section directives are prohibited. That is, the section
2502  // directives must appear within the sections construct and must not be
2503  // encountered elsewhere in the sections region.
2504  if (ParentRegion != OMPD_sections &&
2505  ParentRegion != OMPD_parallel_sections) {
2506  SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2507  << (ParentRegion != OMPD_unknown)
2508  << getOpenMPDirectiveName(ParentRegion);
2509  return true;
2510  }
2511  return false;
2512  }
2513  // Allow some constructs (except teams) to be orphaned (they could be
2514  // used in functions, called from OpenMP regions with the required
2515  // preconditions).
2516  if (ParentRegion == OMPD_unknown &&
2517  !isOpenMPNestingTeamsDirective(CurrentRegion))
2518  return false;
2519  if (CurrentRegion == OMPD_cancellation_point ||
2520  CurrentRegion == OMPD_cancel) {
2521  // OpenMP [2.16, Nesting of Regions]
2522  // A cancellation point construct for which construct-type-clause is
2523  // taskgroup must be nested inside a task construct. A cancellation
2524  // point construct for which construct-type-clause is not taskgroup must
2525  // be closely nested inside an OpenMP construct that matches the type
2526  // specified in construct-type-clause.
2527  // A cancel construct for which construct-type-clause is taskgroup must be
2528  // nested inside a task construct. A cancel construct for which
2529  // construct-type-clause is not taskgroup must be closely nested inside an
2530  // OpenMP construct that matches the type specified in
2531  // construct-type-clause.
2532  NestingProhibited =
2533  !((CancelRegion == OMPD_parallel &&
2534  (ParentRegion == OMPD_parallel ||
2535  ParentRegion == OMPD_target_parallel)) ||
2536  (CancelRegion == OMPD_for &&
2537  (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
2538  ParentRegion == OMPD_target_parallel_for)) ||
2539  (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2540  (CancelRegion == OMPD_sections &&
2541  (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2542  ParentRegion == OMPD_parallel_sections)));
2543  } else if (CurrentRegion == OMPD_master) {
2544  // OpenMP [2.16, Nesting of Regions]
2545  // A master region may not be closely nested inside a worksharing,
2546  // atomic, or explicit task region.
2547  NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2548  isOpenMPTaskingDirective(ParentRegion);
2549  } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2550  // OpenMP [2.16, Nesting of Regions]
2551  // A critical region may not be nested (closely or otherwise) inside a
2552  // critical region with the same name. Note that this restriction is not
2553  // sufficient to prevent deadlock.
2554  SourceLocation PreviousCriticalLoc;
2555  bool DeadLock = Stack->hasDirective(
2556  [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
2557  const DeclarationNameInfo &DNI,
2558  SourceLocation Loc) -> bool {
2559  if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
2560  PreviousCriticalLoc = Loc;
2561  return true;
2562  } else
2563  return false;
2564  },
2565  false /* skip top directive */);
2566  if (DeadLock) {
2567  SemaRef.Diag(StartLoc,
2568  diag::err_omp_prohibited_region_critical_same_name)
2569  << CurrentName.getName();
2570  if (PreviousCriticalLoc.isValid())
2571  SemaRef.Diag(PreviousCriticalLoc,
2572  diag::note_omp_previous_critical_region);
2573  return true;
2574  }
2575  } else if (CurrentRegion == OMPD_barrier) {
2576  // OpenMP [2.16, Nesting of Regions]
2577  // A barrier region may not be closely nested inside a worksharing,
2578  // explicit task, critical, ordered, atomic, or master region.
2579  NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2580  isOpenMPTaskingDirective(ParentRegion) ||
2581  ParentRegion == OMPD_master ||
2582  ParentRegion == OMPD_critical ||
2583  ParentRegion == OMPD_ordered;
2584  } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
2585  !isOpenMPParallelDirective(CurrentRegion) &&
2586  !isOpenMPTeamsDirective(CurrentRegion)) {
2587  // OpenMP [2.16, Nesting of Regions]
2588  // A worksharing region may not be closely nested inside a worksharing,
2589  // explicit task, critical, ordered, atomic, or master region.
2590  NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2591  isOpenMPTaskingDirective(ParentRegion) ||
2592  ParentRegion == OMPD_master ||
2593  ParentRegion == OMPD_critical ||
2594  ParentRegion == OMPD_ordered;
2595  Recommend = ShouldBeInParallelRegion;
2596  } else if (CurrentRegion == OMPD_ordered) {
2597  // OpenMP [2.16, Nesting of Regions]
2598  // An ordered region may not be closely nested inside a critical,
2599  // atomic, or explicit task region.
2600  // An ordered region must be closely nested inside a loop region (or
2601  // parallel loop region) with an ordered clause.
2602  // OpenMP [2.8.1,simd Construct, Restrictions]
2603  // An ordered construct with the simd clause is the only OpenMP construct
2604  // that can appear in the simd region.
2605  NestingProhibited = ParentRegion == OMPD_critical ||
2606  isOpenMPTaskingDirective(ParentRegion) ||
2607  !(isOpenMPSimdDirective(ParentRegion) ||
2608  Stack->isParentOrderedRegion());
2609  Recommend = ShouldBeInOrderedRegion;
2610  } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) {
2611  // OpenMP [2.16, Nesting of Regions]
2612  // If specified, a teams construct must be contained within a target
2613  // construct.
2614  NestingProhibited = ParentRegion != OMPD_target;
2615  OrphanSeen = ParentRegion == OMPD_unknown;
2616  Recommend = ShouldBeInTargetRegion;
2617  Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2618  }
2619  if (!NestingProhibited &&
2620  !isOpenMPTargetExecutionDirective(CurrentRegion) &&
2621  !isOpenMPTargetDataManagementDirective(CurrentRegion) &&
2622  (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) {
2623  // OpenMP [2.16, Nesting of Regions]
2624  // distribute, parallel, parallel sections, parallel workshare, and the
2625  // parallel loop and parallel loop SIMD constructs are the only OpenMP
2626  // constructs that can be closely nested in the teams region.
2627  NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2628  !isOpenMPDistributeDirective(CurrentRegion);
2629  Recommend = ShouldBeInParallelRegion;
2630  }
2631  if (!NestingProhibited &&
2632  isOpenMPNestingDistributeDirective(CurrentRegion)) {
2633  // OpenMP 4.5 [2.17 Nesting of Regions]
2634  // The region associated with the distribute construct must be strictly
2635  // nested inside a teams region
2636  NestingProhibited =
2637  (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams);
2638  Recommend = ShouldBeInTeamsRegion;
2639  }
2640  if (!NestingProhibited &&
2641  (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2642  isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2643  // OpenMP 4.5 [2.17 Nesting of Regions]
2644  // If a target, target update, target data, target enter data, or
2645  // target exit data construct is encountered during execution of a
2646  // target region, the behavior is unspecified.
2647  NestingProhibited = Stack->hasDirective(
2648  [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
2649  SourceLocation) -> bool {
2651  OffendingRegion = K;
2652  return true;
2653  } else
2654  return false;
2655  },
2656  false /* don't skip top directive */);
2657  CloseNesting = false;
2658  }
2659  if (NestingProhibited) {
2660  if (OrphanSeen) {
2661  SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
2662  << getOpenMPDirectiveName(CurrentRegion) << Recommend;
2663  } else {
2664  SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
2665  << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2666  << Recommend << getOpenMPDirectiveName(CurrentRegion);
2667  }
2668  return true;
2669  }
2670  }
2671  return false;
2672 }
2673 
2675  ArrayRef<OMPClause *> Clauses,
2676  ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2677  bool ErrorFound = false;
2678  unsigned NamedModifiersNumber = 0;
2680  OMPD_unknown + 1);
2681  SmallVector<SourceLocation, 4> NameModifierLoc;
2682  for (const auto *C : Clauses) {
2683  if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2684  // At most one if clause without a directive-name-modifier can appear on
2685  // the directive.
2686  OpenMPDirectiveKind CurNM = IC->getNameModifier();
2687  if (FoundNameModifiers[CurNM]) {
2688  S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2689  << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2690  << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2691  ErrorFound = true;
2692  } else if (CurNM != OMPD_unknown) {
2693  NameModifierLoc.push_back(IC->getNameModifierLoc());
2694  ++NamedModifiersNumber;
2695  }
2696  FoundNameModifiers[CurNM] = IC;
2697  if (CurNM == OMPD_unknown)
2698  continue;
2699  // Check if the specified name modifier is allowed for the current
2700  // directive.
2701  // At most one if clause with the particular directive-name-modifier can
2702  // appear on the directive.
2703  bool MatchFound = false;
2704  for (auto NM : AllowedNameModifiers) {
2705  if (CurNM == NM) {
2706  MatchFound = true;
2707  break;
2708  }
2709  }
2710  if (!MatchFound) {
2711  S.Diag(IC->getNameModifierLoc(),
2712  diag::err_omp_wrong_if_directive_name_modifier)
2714  ErrorFound = true;
2715  }
2716  }
2717  }
2718  // If any if clause on the directive includes a directive-name-modifier then
2719  // all if clauses on the directive must include a directive-name-modifier.
2720  if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2721  if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2722  S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2723  diag::err_omp_no_more_if_clause);
2724  } else {
2725  std::string Values;
2726  std::string Sep(", ");
2727  unsigned AllowedCnt = 0;
2728  unsigned TotalAllowedNum =
2729  AllowedNameModifiers.size() - NamedModifiersNumber;
2730  for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2731  ++Cnt) {
2732  OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2733  if (!FoundNameModifiers[NM]) {
2734  Values += "'";
2735  Values += getOpenMPDirectiveName(NM);
2736  Values += "'";
2737  if (AllowedCnt + 2 == TotalAllowedNum)
2738  Values += " or ";
2739  else if (AllowedCnt + 1 != TotalAllowedNum)
2740  Values += Sep;
2741  ++AllowedCnt;
2742  }
2743  }
2744  S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2745  diag::err_omp_unnamed_if_clause)
2746  << (TotalAllowedNum > 1) << Values;
2747  }
2748  for (auto Loc : NameModifierLoc) {
2749  S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2750  }
2751  ErrorFound = true;
2752  }
2753  return ErrorFound;
2754 }
2755 
2758  OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2759  Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
2760  StmtResult Res = StmtError();
2761  // First check CancelRegion which is then used in checkNestingOfRegions.
2762  if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) ||
2763  checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2764  StartLoc))
2765  return StmtError();
2766 
2767  llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
2768  llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
2769  bool ErrorFound = false;
2770  ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
2771  if (AStmt && !CurContext->isDependentContext()) {
2772  assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2773 
2774  // Check default data sharing attributes for referenced variables.
2775  DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2776  int ThisCaptureLevel = getOpenMPCaptureLevels(Kind);
2777  Stmt *S = AStmt;
2778  while (--ThisCaptureLevel >= 0)
2779  S = cast<CapturedStmt>(S)->getCapturedStmt();
2780  DSAChecker.Visit(S);
2781  if (DSAChecker.isErrorFound())
2782  return StmtError();
2783  // Generate list of implicitly defined firstprivate variables.
2784  VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
2785 
2786  SmallVector<Expr *, 4> ImplicitFirstprivates(
2787  DSAChecker.getImplicitFirstprivate().begin(),
2788  DSAChecker.getImplicitFirstprivate().end());
2789  SmallVector<Expr *, 4> ImplicitMaps(DSAChecker.getImplicitMap().begin(),
2790  DSAChecker.getImplicitMap().end());
2791  // Mark taskgroup task_reduction descriptors as implicitly firstprivate.
2792  for (auto *C : Clauses) {
2793  if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) {
2794  for (auto *E : IRC->taskgroup_descriptors())
2795  if (E)
2796  ImplicitFirstprivates.emplace_back(E);
2797  }
2798  }
2799  if (!ImplicitFirstprivates.empty()) {
2800  if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2801  ImplicitFirstprivates, SourceLocation(), SourceLocation(),
2802  SourceLocation())) {
2803  ClausesWithImplicit.push_back(Implicit);
2804  ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2805  ImplicitFirstprivates.size();
2806  } else
2807  ErrorFound = true;
2808  }
2809  if (!ImplicitMaps.empty()) {
2810  if (OMPClause *Implicit = ActOnOpenMPMapClause(
2811  OMPC_MAP_unknown, OMPC_MAP_tofrom, /*IsMapTypeImplicit=*/true,
2812  SourceLocation(), SourceLocation(), ImplicitMaps,
2814  ClausesWithImplicit.emplace_back(Implicit);
2815  ErrorFound |=
2816  cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMaps.size();
2817  } else
2818  ErrorFound = true;
2819  }
2820  }
2821 
2822  llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
2823  switch (Kind) {
2824  case OMPD_parallel:
2825  Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2826  EndLoc);
2827  AllowedNameModifiers.push_back(OMPD_parallel);
2828  break;
2829  case OMPD_simd:
2830  Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2831  VarsWithInheritedDSA);
2832  break;
2833  case OMPD_for:
2834  Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2835  VarsWithInheritedDSA);
2836  break;
2837  case OMPD_for_simd:
2838  Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2839  EndLoc, VarsWithInheritedDSA);
2840  break;
2841  case OMPD_sections:
2842  Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2843  EndLoc);
2844  break;
2845  case OMPD_section:
2846  assert(ClausesWithImplicit.empty() &&
2847  "No clauses are allowed for 'omp section' directive");
2848  Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2849  break;
2850  case OMPD_single:
2851  Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2852  EndLoc);
2853  break;
2854  case OMPD_master:
2855  assert(ClausesWithImplicit.empty() &&
2856  "No clauses are allowed for 'omp master' directive");
2857  Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2858  break;
2859  case OMPD_critical:
2860  Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
2861  StartLoc, EndLoc);
2862  break;
2863  case OMPD_parallel_for:
2864  Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2865  EndLoc, VarsWithInheritedDSA);
2866  AllowedNameModifiers.push_back(OMPD_parallel);
2867  break;
2868  case OMPD_parallel_for_simd:
2869  Res = ActOnOpenMPParallelForSimdDirective(
2870  ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2871  AllowedNameModifiers.push_back(OMPD_parallel);
2872  break;
2873  case OMPD_parallel_sections:
2874  Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2875  StartLoc, EndLoc);
2876  AllowedNameModifiers.push_back(OMPD_parallel);
2877  break;
2878  case OMPD_task:
2879  Res =
2880  ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2881  AllowedNameModifiers.push_back(OMPD_task);
2882  break;
2883  case OMPD_taskyield:
2884  assert(ClausesWithImplicit.empty() &&
2885  "No clauses are allowed for 'omp taskyield' directive");
2886  assert(AStmt == nullptr &&
2887  "No associated statement allowed for 'omp taskyield' directive");
2888  Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2889  break;
2890  case OMPD_barrier:
2891  assert(ClausesWithImplicit.empty() &&
2892  "No clauses are allowed for 'omp barrier' directive");
2893  assert(AStmt == nullptr &&
2894  "No associated statement allowed for 'omp barrier' directive");
2895  Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2896  break;
2897  case OMPD_taskwait:
2898  assert(ClausesWithImplicit.empty() &&
2899  "No clauses are allowed for 'omp taskwait' directive");
2900  assert(AStmt == nullptr &&
2901  "No associated statement allowed for 'omp taskwait' directive");
2902  Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2903  break;
2904  case OMPD_taskgroup:
2905  Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc,
2906  EndLoc);
2907  break;
2908  case OMPD_flush:
2909  assert(AStmt == nullptr &&
2910  "No associated statement allowed for 'omp flush' directive");
2911  Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2912  break;
2913  case OMPD_ordered:
2914  Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
2915  EndLoc);
2916  break;
2917  case OMPD_atomic:
2918  Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2919  EndLoc);
2920  break;
2921  case OMPD_teams:
2922  Res =
2923  ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2924  break;
2925  case OMPD_target:
2926  Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2927  EndLoc);
2928  AllowedNameModifiers.push_back(OMPD_target);
2929  break;
2930  case OMPD_target_parallel:
2931  Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
2932  StartLoc, EndLoc);
2933  AllowedNameModifiers.push_back(OMPD_target);
2934  AllowedNameModifiers.push_back(OMPD_parallel);
2935  break;
2936  case OMPD_target_parallel_for:
2937  Res = ActOnOpenMPTargetParallelForDirective(
2938  ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2939  AllowedNameModifiers.push_back(OMPD_target);
2940  AllowedNameModifiers.push_back(OMPD_parallel);
2941  break;
2942  case OMPD_cancellation_point:
2943  assert(ClausesWithImplicit.empty() &&
2944  "No clauses are allowed for 'omp cancellation point' directive");
2945  assert(AStmt == nullptr && "No associated statement allowed for 'omp "
2946  "cancellation point' directive");
2947  Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2948  break;
2949  case OMPD_cancel:
2950  assert(AStmt == nullptr &&
2951  "No associated statement allowed for 'omp cancel' directive");
2952  Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
2953  CancelRegion);
2954  AllowedNameModifiers.push_back(OMPD_cancel);
2955  break;
2956  case OMPD_target_data:
2957  Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
2958  EndLoc);
2959  AllowedNameModifiers.push_back(OMPD_target_data);
2960  break;
2961  case OMPD_target_enter_data:
2962  Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
2963  EndLoc);
2964  AllowedNameModifiers.push_back(OMPD_target_enter_data);
2965  break;
2966  case OMPD_target_exit_data:
2967  Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
2968  EndLoc);
2969  AllowedNameModifiers.push_back(OMPD_target_exit_data);
2970  break;
2971  case OMPD_taskloop:
2972  Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
2973  EndLoc, VarsWithInheritedDSA);
2974  AllowedNameModifiers.push_back(OMPD_taskloop);
2975  break;
2976  case OMPD_taskloop_simd:
2977  Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2978  EndLoc, VarsWithInheritedDSA);
2979  AllowedNameModifiers.push_back(OMPD_taskloop);
2980  break;
2981  case OMPD_distribute:
2982  Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
2983  EndLoc, VarsWithInheritedDSA);
2984  break;
2985  case OMPD_target_update:
2986  assert(!AStmt && "Statement is not allowed for target update");
2987  Res =
2988  ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, EndLoc);
2989  AllowedNameModifiers.push_back(OMPD_target_update);
2990  break;
2991  case OMPD_distribute_parallel_for:
2992  Res = ActOnOpenMPDistributeParallelForDirective(
2993  ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2994  AllowedNameModifiers.push_back(OMPD_parallel);
2995  break;
2996  case OMPD_distribute_parallel_for_simd:
2997  Res = ActOnOpenMPDistributeParallelForSimdDirective(
2998  ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2999  AllowedNameModifiers.push_back(OMPD_parallel);
3000  break;
3001  case OMPD_distribute_simd:
3002  Res = ActOnOpenMPDistributeSimdDirective(
3003  ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3004  break;
3005  case OMPD_target_parallel_for_simd:
3006  Res = ActOnOpenMPTargetParallelForSimdDirective(
3007  ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3008  AllowedNameModifiers.push_back(OMPD_target);
3009  AllowedNameModifiers.push_back(OMPD_parallel);
3010  break;
3011  case OMPD_target_simd:
3012  Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3013  EndLoc, VarsWithInheritedDSA);
3014  AllowedNameModifiers.push_back(OMPD_target);
3015  break;
3016  case OMPD_teams_distribute:
3017  Res = ActOnOpenMPTeamsDistributeDirective(
3018  ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3019  break;
3020  case OMPD_teams_distribute_simd:
3021  Res = ActOnOpenMPTeamsDistributeSimdDirective(
3022  ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3023  break;
3024  case OMPD_teams_distribute_parallel_for_simd:
3025  Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
3026  ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3027  AllowedNameModifiers.push_back(OMPD_parallel);
3028  break;
3029  case OMPD_teams_distribute_parallel_for:
3030  Res = ActOnOpenMPTeamsDistributeParallelForDirective(
3031  ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3032  AllowedNameModifiers.push_back(OMPD_parallel);
3033  break;
3034  case OMPD_target_teams:
3035  Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc,
3036  EndLoc);
3037  AllowedNameModifiers.push_back(OMPD_target);
3038  break;
3039  case OMPD_target_teams_distribute:
3040  Res = ActOnOpenMPTargetTeamsDistributeDirective(
3041  ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3042  AllowedNameModifiers.push_back(OMPD_target);
3043  break;
3044  case OMPD_target_teams_distribute_parallel_for:
3045  Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
3046  ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3047  AllowedNameModifiers.push_back(OMPD_target);
3048  AllowedNameModifiers.push_back(OMPD_parallel);
3049  break;
3050  case OMPD_target_teams_distribute_parallel_for_simd:
3051  Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
3052  ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3053  AllowedNameModifiers.push_back(OMPD_target);
3054  AllowedNameModifiers.push_back(OMPD_parallel);
3055  break;
3056  case OMPD_target_teams_distribute_simd:
3057  Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
3058  ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3059  AllowedNameModifiers.push_back(OMPD_target);
3060  break;
3061  case OMPD_declare_target:
3062  case OMPD_end_declare_target:
3063  case OMPD_threadprivate:
3064  case OMPD_declare_reduction:
3065  case OMPD_declare_simd:
3066  llvm_unreachable("OpenMP Directive is not allowed");
3067  case OMPD_unknown:
3068  llvm_unreachable("Unknown OpenMP directive");
3069  }
3070 
3071  for (auto P : VarsWithInheritedDSA) {
3072  Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3073  << P.first << P.second->getSourceRange();
3074  }
3075  ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3076 
3077  if (!AllowedNameModifiers.empty())
3078  ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3079  ErrorFound;
3080 
3081  if (ErrorFound)
3082  return StmtError();
3083  return Res;
3084 }
3085 
3087  DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
3088  ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
3089  ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3090  ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
3091  assert(Aligneds.size() == Alignments.size());
3092  assert(Linears.size() == LinModifiers.size());
3093  assert(Linears.size() == Steps.size());
3094  if (!DG || DG.get().isNull())
3095  return DeclGroupPtrTy();
3096 
3097  if (!DG.get().isSingleDecl()) {
3098  Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
3099  return DG;
3100  }
3101  auto *ADecl = DG.get().getSingleDecl();
3102  if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3103  ADecl = FTD->getTemplatedDecl();
3104 
3105  auto *FD = dyn_cast<FunctionDecl>(ADecl);
3106  if (!FD) {
3107  Diag(ADecl->getLocation(), diag::err_omp_function_expected);
3108  return DeclGroupPtrTy();
3109  }
3110 
3111  // OpenMP [2.8.2, declare simd construct, Description]
3112  // The parameter of the simdlen clause must be a constant positive integer
3113  // expression.
3114  ExprResult SL;
3115  if (Simdlen)
3116  SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
3117  // OpenMP [2.8.2, declare simd construct, Description]
3118  // The special this pointer can be used as if was one of the arguments to the
3119  // function in any of the linear, aligned, or uniform clauses.
3120  // The uniform clause declares one or more arguments to have an invariant
3121  // value for all concurrent invocations of the function in the execution of a
3122  // single SIMD loop.
3123  llvm::DenseMap<Decl *, Expr *> UniformedArgs;
3124  Expr *UniformedLinearThis = nullptr;
3125  for (auto *E : Uniforms) {
3126  E = E->IgnoreParenImpCasts();
3127  if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3128  if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
3129  if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3130  FD->getParamDecl(PVD->getFunctionScopeIndex())
3131  ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
3132  UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E));
3133  continue;
3134  }
3135  if (isa<CXXThisExpr>(E)) {
3136  UniformedLinearThis = E;
3137  continue;
3138  }
3139  Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3140  << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3141  }
3142  // OpenMP [2.8.2, declare simd construct, Description]
3143  // The aligned clause declares that the object to which each list item points
3144  // is aligned to the number of bytes expressed in the optional parameter of
3145  // the aligned clause.
3146  // The special this pointer can be used as if was one of the arguments to the
3147  // function in any of the linear, aligned, or uniform clauses.
3148  // The type of list items appearing in the aligned clause must be array,
3149  // pointer, reference to array, or reference to pointer.
3150  llvm::DenseMap<Decl *, Expr *> AlignedArgs;
3151  Expr *AlignedThis = nullptr;
3152  for (auto *E : Aligneds) {
3153  E = E->IgnoreParenImpCasts();
3154  if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3155  if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3156  auto *CanonPVD = PVD->getCanonicalDecl();
3157  if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3158  FD->getParamDecl(PVD->getFunctionScopeIndex())
3159  ->getCanonicalDecl() == CanonPVD) {
3160  // OpenMP [2.8.1, simd construct, Restrictions]
3161  // A list-item cannot appear in more than one aligned clause.
3162  if (AlignedArgs.count(CanonPVD) > 0) {
3163  Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3164  << 1 << E->getSourceRange();
3165  Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3166  diag::note_omp_explicit_dsa)
3167  << getOpenMPClauseName(OMPC_aligned);
3168  continue;
3169  }
3170  AlignedArgs[CanonPVD] = E;
3171  QualType QTy = PVD->getType()
3172  .getNonReferenceType()
3173  .getUnqualifiedType()
3174  .getCanonicalType();
3175  const Type *Ty = QTy.getTypePtrOrNull();
3176  if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3177  Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3178  << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3179  Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3180  }
3181  continue;
3182  }
3183  }
3184  if (isa<CXXThisExpr>(E)) {
3185  if (AlignedThis) {
3186  Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3187  << 2 << E->getSourceRange();
3188  Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3189  << getOpenMPClauseName(OMPC_aligned);
3190  }
3191  AlignedThis = E;
3192  continue;
3193  }
3194  Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3195  << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3196  }
3197  // The optional parameter of the aligned clause, alignment, must be a constant
3198  // positive integer expression. If no optional parameter is specified,
3199  // implementation-defined default alignments for SIMD instructions on the
3200  // target platforms are assumed.
3201  SmallVector<Expr *, 4> NewAligns;
3202  for (auto *E : Alignments) {
3203  ExprResult Align;
3204  if (E)
3205  Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3206  NewAligns.push_back(Align.get());
3207  }
3208  // OpenMP [2.8.2, declare simd construct, Description]
3209  // The linear clause declares one or more list items to be private to a SIMD
3210  // lane and to have a linear relationship with respect to the iteration space
3211  // of a loop.
3212  // The special this pointer can be used as if was one of the arguments to the
3213  // function in any of the linear, aligned, or uniform clauses.
3214  // When a linear-step expression is specified in a linear clause it must be
3215  // either a constant integer expression or an integer-typed parameter that is
3216  // specified in a uniform clause on the directive.
3217  llvm::DenseMap<Decl *, Expr *> LinearArgs;
3218  const bool IsUniformedThis = UniformedLinearThis != nullptr;
3219  auto MI = LinModifiers.begin();
3220  for (auto *E : Linears) {
3221  auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3222  ++MI;
3223  E = E->IgnoreParenImpCasts();
3224  if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3225  if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3226  auto *CanonPVD = PVD->getCanonicalDecl();
3227  if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3228  FD->getParamDecl(PVD->getFunctionScopeIndex())
3229  ->getCanonicalDecl() == CanonPVD) {
3230  // OpenMP [2.15.3.7, linear Clause, Restrictions]
3231  // A list-item cannot appear in more than one linear clause.
3232  if (LinearArgs.count(CanonPVD) > 0) {
3233  Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3234  << getOpenMPClauseName(OMPC_linear)
3235  << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3236  Diag(LinearArgs[CanonPVD]->getExprLoc(),
3237  diag::note_omp_explicit_dsa)
3238  << getOpenMPClauseName(OMPC_linear);
3239  continue;
3240  }
3241  // Each argument can appear in at most one uniform or linear clause.
3242  if (UniformedArgs.count(CanonPVD) > 0) {
3243  Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3244  << getOpenMPClauseName(OMPC_linear)
3246  Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3247  diag::note_omp_explicit_dsa)
3249  continue;
3250  }
3251  LinearArgs[CanonPVD] = E;
3252  if (E->isValueDependent() || E->isTypeDependent() ||
3253  E->isInstantiationDependent() ||
3255  continue;
3256  (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3257  PVD->getOriginalType());
3258  continue;
3259  }
3260  }
3261  if (isa<CXXThisExpr>(E)) {
3262  if (UniformedLinearThis) {
3263  Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3264  << getOpenMPClauseName(OMPC_linear)
3265  << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3266  << E->getSourceRange();
3267  Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3268  << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3269  : OMPC_linear);
3270  continue;
3271  }
3272  UniformedLinearThis = E;
3273  if (E->isValueDependent() || E->isTypeDependent() ||
3275  continue;
3276  (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3277  E->getType());
3278  continue;
3279  }
3280  Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3281  << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3282  }
3283  Expr *Step = nullptr;
3284  Expr *NewStep = nullptr;
3285  SmallVector<Expr *, 4> NewSteps;
3286  for (auto *E : Steps) {
3287  // Skip the same step expression, it was checked already.
3288  if (Step == E || !E) {
3289  NewSteps.push_back(E ? NewStep : nullptr);
3290  continue;
3291  }
3292  Step = E;
3293  if (auto *DRE = dyn_cast<DeclRefExpr>(Step))
3294  if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3295  auto *CanonPVD = PVD->getCanonicalDecl();
3296  if (UniformedArgs.count(CanonPVD) == 0) {
3297  Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3298  << Step->getSourceRange();
3299  } else if (E->isValueDependent() || E->isTypeDependent() ||
3300  E->isInstantiationDependent() ||
3302  CanonPVD->getType()->hasIntegerRepresentation())
3303  NewSteps.push_back(Step);
3304  else {
3305  Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3306  << Step->getSourceRange();
3307  }
3308  continue;
3309  }
3310  NewStep = Step;
3311  if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3312  !Step->isInstantiationDependent() &&
3314  NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3315  .get();
3316  if (NewStep)
3317  NewStep = VerifyIntegerConstantExpression(NewStep).get();
3318  }
3319  NewSteps.push_back(NewStep);
3320  }
3321  auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3322  Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
3323  Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
3324  const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3325  const_cast<Expr **>(Linears.data()), Linears.size(),
3326  const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3327  NewSteps.data(), NewSteps.size(), SR);
3328  ADecl->addAttr(NewAttr);
3329  return ConvertDeclToDeclGroup(ADecl);
3330 }
3331 
3333  Stmt *AStmt,
3334  SourceLocation StartLoc,
3335  SourceLocation EndLoc) {
3336  if (!AStmt)
3337  return StmtError();
3338 
3339  CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3340  // 1.2.2 OpenMP Language Terminology
3341  // Structured block - An executable statement with a single entry at the
3342  // top and a single exit at the bottom.
3343  // The point of exit cannot be a branch out of the structured block.
3344  // longjmp() and throw() must not violate the entry/exit criteria.
3345  CS->getCapturedDecl()->setNothrow();
3346 
3347  getCurFunction()->setHasBranchProtectedScope();
3348 
3349  return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3350  DSAStack->isCancelRegion());
3351 }
3352 
3353 namespace {
3354 /// \brief Helper class for checking canonical form of the OpenMP loops and
3355 /// extracting iteration space of each loop in the loop nest, that will be used
3356 /// for IR generation.
3357 class OpenMPIterationSpaceChecker {
3358  /// \brief Reference to Sema.
3359  Sema &SemaRef;
3360  /// \brief A location for diagnostics (when there is no some better location).
3361  SourceLocation DefaultLoc;
3362  /// \brief A location for diagnostics (when increment is not compatible).
3363  SourceLocation ConditionLoc;
3364  /// \brief A source location for referring to loop init later.
3365  SourceRange InitSrcRange;
3366  /// \brief A source location for referring to condition later.
3367  SourceRange ConditionSrcRange;
3368  /// \brief A source location for referring to increment later.
3369  SourceRange IncrementSrcRange;
3370  /// \brief Loop variable.
3371  ValueDecl *LCDecl = nullptr;
3372  /// \brief Reference to loop variable.
3373  Expr *LCRef = nullptr;
3374  /// \brief Lower bound (initializer for the var).
3375  Expr *LB = nullptr;
3376  /// \brief Upper bound.
3377  Expr *UB = nullptr;
3378  /// \brief Loop step (increment).
3379  Expr *Step = nullptr;
3380  /// \brief This flag is true when condition is one of:
3381  /// Var < UB
3382  /// Var <= UB
3383  /// UB > Var
3384  /// UB >= Var
3385  bool TestIsLessOp = false;
3386  /// \brief This flag is true when condition is strict ( < or > ).
3387  bool TestIsStrictOp = false;
3388  /// \brief This flag is true when step is subtracted on each iteration.
3389  bool SubtractStep = false;
3390 
3391 public:
3392  OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
3393  : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
3394  /// \brief Check init-expr for canonical loop form and save loop counter
3395  /// variable - #Var and its initialization value - #LB.
3396  bool CheckInit(Stmt *S, bool EmitDiags = true);
3397  /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3398  /// for less/greater and for strict/non-strict comparison.
3399  bool CheckCond(Expr *S);
3400  /// \brief Check incr-expr for canonical loop form and return true if it
3401  /// does not conform, otherwise save loop step (#Step).
3402  bool CheckInc(Expr *S);
3403  /// \brief Return the loop counter variable.
3404  ValueDecl *GetLoopDecl() const { return LCDecl; }
3405  /// \brief Return the reference expression to loop counter variable.
3406  Expr *GetLoopDeclRefExpr() const { return LCRef; }
3407  /// \brief Source range of the loop init.
3408  SourceRange GetInitSrcRange() const { return InitSrcRange; }
3409  /// \brief Source range of the loop condition.
3410  SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3411  /// \brief Source range of the loop increment.
3412  SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3413  /// \brief True if the step should be subtracted.
3414  bool ShouldSubtractStep() const { return SubtractStep; }
3415  /// \brief Build the expression to calculate the number of iterations.
3416  Expr *
3417  BuildNumIterations(Scope *S, const bool LimitedType,
3418  llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
3419  /// \brief Build the precondition expression for the loops.
3420  Expr *BuildPreCond(Scope *S, Expr *Cond,
3421  llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
3422  /// \brief Build reference expression to the counter be used for codegen.
3423  DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures,
3424  DSAStackTy &DSA) const;
3425  /// \brief Build reference expression to the private counter be used for
3426  /// codegen.
3427  Expr *BuildPrivateCounterVar() const;
3428  /// \brief Build initialization of the counter be used for codegen.
3429  Expr *BuildCounterInit() const;
3430  /// \brief Build step of the counter be used for codegen.
3431  Expr *BuildCounterStep() const;
3432  /// \brief Return true if any expression is dependent.
3433  bool Dependent() const;
3434 
3435 private:
3436  /// \brief Check the right-hand side of an assignment in the increment
3437  /// expression.
3438  bool CheckIncRHS(Expr *RHS);
3439  /// \brief Helper to set loop counter variable and its initializer.
3440  bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
3441  /// \brief Helper to set upper bound.
3442  bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
3443  SourceLocation SL);
3444  /// \brief Helper to set loop increment.
3445  bool SetStep(Expr *NewStep, bool Subtract);
3446 };
3447 
3448 bool OpenMPIterationSpaceChecker::Dependent() const {
3449  if (!LCDecl) {
3450  assert(!LB && !UB && !Step);
3451  return false;
3452  }
3453  return LCDecl->getType()->isDependentType() ||
3454  (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
3455  (Step && Step->isValueDependent());
3456 }
3457 
3458 bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl,
3459  Expr *NewLCRefExpr,
3460  Expr *NewLB) {
3461  // State consistency checking to ensure correct usage.
3462  assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
3463  UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
3464  if (!NewLCDecl || !NewLB)
3465  return true;
3466  LCDecl = getCanonicalDecl(NewLCDecl);
3467  LCRef = NewLCRefExpr;
3468  if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3469  if (const CXXConstructorDecl *Ctor = CE->getConstructor())
3470  if ((Ctor->isCopyOrMoveConstructor() ||
3471  Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3472  CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
3473  NewLB = CE->getArg(0)->IgnoreParenImpCasts();
3474  LB = NewLB;
3475  return false;
3476 }
3477 
3478 bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
3479  SourceRange SR, SourceLocation SL) {
3480  // State consistency checking to ensure correct usage.
3481  assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
3482  Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
3483  if (!NewUB)
3484  return true;
3485  UB = NewUB;
3486  TestIsLessOp = LessOp;
3487  TestIsStrictOp = StrictOp;
3488  ConditionSrcRange = SR;
3489  ConditionLoc = SL;
3490  return false;
3491 }
3492 
3493 bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3494  // State consistency checking to ensure correct usage.
3495  assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
3496  if (!NewStep)
3497  return true;
3498  if (!NewStep->isValueDependent()) {
3499  // Check that the step is integer expression.
3500  SourceLocation StepLoc = NewStep->getLocStart();
3502  StepLoc, getExprAsWritten(NewStep));
3503  if (Val.isInvalid())
3504  return true;
3505  NewStep = Val.get();
3506 
3507  // OpenMP [2.6, Canonical Loop Form, Restrictions]
3508  // If test-expr is of form var relational-op b and relational-op is < or
3509  // <= then incr-expr must cause var to increase on each iteration of the
3510  // loop. If test-expr is of form var relational-op b and relational-op is
3511  // > or >= then incr-expr must cause var to decrease on each iteration of
3512  // the loop.
3513  // If test-expr is of form b relational-op var and relational-op is < or
3514  // <= then incr-expr must cause var to decrease on each iteration of the
3515  // loop. If test-expr is of form b relational-op var and relational-op is
3516  // > or >= then incr-expr must cause var to increase on each iteration of
3517  // the loop.
3518  llvm::APSInt Result;
3519  bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3520  bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3521  bool IsConstNeg =
3522  IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
3523  bool IsConstPos =
3524  IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
3525  bool IsConstZero = IsConstant && !Result.getBoolValue();
3526  if (UB && (IsConstZero ||
3527  (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
3528  : (IsConstPos || (IsUnsigned && !Subtract))))) {
3529  SemaRef.Diag(NewStep->getExprLoc(),
3530  diag::err_omp_loop_incr_not_compatible)
3531  << LCDecl << TestIsLessOp << NewStep->getSourceRange();
3532  SemaRef.Diag(ConditionLoc,
3533  diag::note_omp_loop_cond_requres_compatible_incr)
3534  << TestIsLessOp << ConditionSrcRange;
3535  return true;
3536  }
3537  if (TestIsLessOp == Subtract) {
3538  NewStep =
3539  SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
3540  .get();
3541  Subtract = !Subtract;
3542  }
3543  }
3544 
3545  Step = NewStep;
3546  SubtractStep = Subtract;
3547  return false;
3548 }
3549 
3550 bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
3551  // Check init-expr for canonical loop form and save loop counter
3552  // variable - #Var and its initialization value - #LB.
3553  // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3554  // var = lb
3555  // integer-type var = lb
3556  // random-access-iterator-type var = lb
3557  // pointer-type var = lb
3558  //
3559  if (!S) {
3560  if (EmitDiags) {
3561  SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3562  }
3563  return true;
3564  }
3565  if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
3566  if (!ExprTemp->cleanupsHaveSideEffects())
3567  S = ExprTemp->getSubExpr();
3568 
3569  InitSrcRange = S->getSourceRange();
3570  if (Expr *E = dyn_cast<Expr>(S))
3571  S = E->IgnoreParens();
3572  if (auto *BO = dyn_cast<BinaryOperator>(S)) {
3573  if (BO->getOpcode() == BO_Assign) {
3574  auto *LHS = BO->getLHS()->IgnoreParens();
3575  if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
3576  if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3577  if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3578  return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3579  return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
3580  }
3581  if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3582  if (ME->isArrow() &&
3583  isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3584  return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3585  }
3586  }
3587  } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
3588  if (DS->isSingleDecl()) {
3589  if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
3590  if (Var->hasInit() && !Var->getType()->isReferenceType()) {
3591  // Accept non-canonical init form here but emit ext. warning.
3592  if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
3593  SemaRef.Diag(S->getLocStart(),
3594  diag::ext_omp_loop_not_canonical_init)
3595  << S->getSourceRange();
3596  return SetLCDeclAndLB(Var, nullptr, Var->getInit());
3597  }
3598  }
3599  }
3600  } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3601  if (CE->getOperator() == OO_Equal) {
3602  auto *LHS = CE->getArg(0);
3603  if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
3604  if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3605  if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3606  return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3607  return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
3608  }
3609  if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3610  if (ME->isArrow() &&
3611  isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3612  return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3613  }
3614  }
3615  }
3616 
3617  if (Dependent() || SemaRef.CurContext->isDependentContext())
3618  return false;
3619  if (EmitDiags) {
3620  SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3621  << S->getSourceRange();
3622  }
3623  return true;
3624 }
3625 
3626 /// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
3627 /// variable (which may be the loop variable) if possible.
3628 static const ValueDecl *GetInitLCDecl(Expr *E) {
3629  if (!E)
3630  return nullptr;
3631  E = getExprAsWritten(E);
3632  if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3633  if (const CXXConstructorDecl *Ctor = CE->getConstructor())
3634  if ((Ctor->isCopyOrMoveConstructor() ||
3635  Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3636  CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
3637  E = CE->getArg(0)->IgnoreParenImpCasts();
3638  if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
3639  if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
3640  return getCanonicalDecl(VD);
3641  }
3642  if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
3643  if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3644  return getCanonicalDecl(ME->getMemberDecl());
3645  return nullptr;
3646 }
3647 
3648 bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3649  // Check test-expr for canonical form, save upper-bound UB, flags for
3650  // less/greater and for strict/non-strict comparison.
3651  // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3652  // var relational-op b
3653  // b relational-op var
3654  //
3655  if (!S) {
3656  SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
3657  return true;
3658  }
3659  S = getExprAsWritten(S);
3660  SourceLocation CondLoc = S->getLocStart();
3661  if (auto *BO = dyn_cast<BinaryOperator>(S)) {
3662  if (BO->isRelationalOp()) {
3663  if (GetInitLCDecl(BO->getLHS()) == LCDecl)
3664  return SetUB(BO->getRHS(),
3665  (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3666  (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3667  BO->getSourceRange(), BO->getOperatorLoc());
3668  if (GetInitLCDecl(BO->getRHS()) == LCDecl)
3669  return SetUB(BO->getLHS(),
3670  (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3671  (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3672  BO->getSourceRange(), BO->getOperatorLoc());
3673  }
3674  } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3675  if (CE->getNumArgs() == 2) {
3676  auto Op = CE->getOperator();
3677  switch (Op) {
3678  case OO_Greater:
3679  case OO_GreaterEqual:
3680  case OO_Less:
3681  case OO_LessEqual:
3682  if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
3683  return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3684  Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3685  CE->getOperatorLoc());
3686  if (GetInitLCDecl(CE->getArg(1)) == LCDecl)
3687  return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3688  Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3689  CE->getOperatorLoc());
3690  break;
3691  default:
3692  break;
3693  }
3694  }
3695  }
3696  if (Dependent() || SemaRef.CurContext->isDependentContext())
3697  return false;
3698  SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
3699  << S->getSourceRange() << LCDecl;
3700  return true;
3701 }
3702 
3703 bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3704  // RHS of canonical loop form increment can be:
3705  // var + incr
3706  // incr + var
3707  // var - incr
3708  //
3709  RHS = RHS->IgnoreParenImpCasts();
3710  if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
3711  if (BO->isAdditiveOp()) {
3712  bool IsAdd = BO->getOpcode() == BO_Add;
3713  if (GetInitLCDecl(BO->getLHS()) == LCDecl)
3714  return SetStep(BO->getRHS(), !IsAdd);
3715  if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl)
3716  return SetStep(BO->getLHS(), false);
3717  }
3718  } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3719  bool IsAdd = CE->getOperator() == OO_Plus;
3720  if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
3721  if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
3722  return SetStep(CE->getArg(1), !IsAdd);
3723  if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl)
3724  return SetStep(CE->getArg(0), false);
3725  }
3726  }
3727  if (Dependent() || SemaRef.CurContext->isDependentContext())
3728  return false;
3729  SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3730  << RHS->getSourceRange() << LCDecl;
3731  return true;
3732 }
3733 
3734 bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3735  // Check incr-expr for canonical loop form and return true if it
3736  // does not conform.
3737  // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3738  // ++var
3739  // var++
3740  // --var
3741  // var--
3742  // var += incr
3743  // var -= incr
3744  // var = var + incr
3745  // var = incr + var
3746  // var = var - incr
3747  //
3748  if (!S) {
3749  SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
3750  return true;
3751  }
3752  if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
3753  if (!ExprTemp->cleanupsHaveSideEffects())
3754  S = ExprTemp->getSubExpr();
3755 
3756  IncrementSrcRange = S->getSourceRange();
3757  S = S->IgnoreParens();
3758  if (auto *UO = dyn_cast<UnaryOperator>(S)) {
3759  if (UO->isIncrementDecrementOp() &&
3760  GetInitLCDecl(UO->getSubExpr()) == LCDecl)
3761  return SetStep(SemaRef
3762  .ActOnIntegerConstant(UO->getLocStart(),
3763  (UO->isDecrementOp() ? -1 : 1))
3764  .get(),
3765  false);
3766  } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
3767  switch (BO->getOpcode()) {
3768  case BO_AddAssign:
3769  case BO_SubAssign:
3770  if (GetInitLCDecl(BO->getLHS()) == LCDecl)
3771  return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3772  break;
3773  case BO_Assign:
3774  if (GetInitLCDecl(BO->getLHS()) == LCDecl)
3775  return CheckIncRHS(BO->getRHS());
3776  break;
3777  default:
3778  break;
3779  }
3780  } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3781  switch (CE->getOperator()) {
3782  case OO_PlusPlus:
3783  case OO_MinusMinus:
3784  if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
3785  return SetStep(SemaRef
3786  .ActOnIntegerConstant(
3787  CE->getLocStart(),
3788  ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
3789  .get(),
3790  false);
3791  break;
3792  case OO_PlusEqual:
3793  case OO_MinusEqual:
3794  if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
3795  return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3796  break;
3797  case OO_Equal:
3798  if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
3799  return CheckIncRHS(CE->getArg(1));
3800  break;
3801  default:
3802  break;
3803  }
3804  }
3805  if (Dependent() || SemaRef.CurContext->isDependentContext())
3806  return false;
3807  SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3808  << S->getSourceRange() << LCDecl;
3809  return true;
3810 }
3811 
3812 static ExprResult
3813 tryBuildCapture(Sema &SemaRef, Expr *Capture,
3814  llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
3815  if (SemaRef.CurContext->isDependentContext())
3816  return ExprResult(Capture);
3817  if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
3818  return SemaRef.PerformImplicitConversion(
3819  Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
3820  /*AllowExplicit=*/true);
3821  auto I = Captures.find(Capture);
3822  if (I != Captures.end())
3823  return buildCapture(SemaRef, Capture, I->second);
3824  DeclRefExpr *Ref = nullptr;
3825  ExprResult Res = buildCapture(SemaRef, Capture, Ref);
3826  Captures[Capture] = Ref;
3827  return Res;
3828 }
3829 
3830 /// \brief Build the expression to calculate the number of iterations.
3831 Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
3832  Scope *S, const bool LimitedType,
3833  llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
3834  ExprResult Diff;
3835  auto VarType = LCDecl->getType().getNonReferenceType();
3836  if (VarType->isIntegerType() || VarType->isPointerType() ||
3837  SemaRef.getLangOpts().CPlusPlus) {
3838  // Upper - Lower
3839  auto *UBExpr = TestIsLessOp ? UB : LB;
3840  auto *LBExpr = TestIsLessOp ? LB : UB;
3841  Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
3842  Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
3843  if (!Upper || !Lower)
3844  return nullptr;
3845 
3846  Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3847 
3848  if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
3849  // BuildBinOp already emitted error, this one is to point user to upper
3850  // and lower bound, and to tell what is passed to 'operator-'.
3851  SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3852  << Upper->getSourceRange() << Lower->getSourceRange();
3853  return nullptr;
3854  }
3855  }
3856 
3857  if (!Diff.isUsable())
3858  return nullptr;
3859 
3860  // Upper - Lower [- 1]
3861  if (TestIsStrictOp)
3862  Diff = SemaRef.BuildBinOp(
3863  S, DefaultLoc, BO_Sub, Diff.get(),
3864  SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3865  if (!Diff.isUsable())
3866  return nullptr;
3867 
3868  // Upper - Lower [- 1] + Step
3869  auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
3870  if (!NewStep.isUsable())
3871  return nullptr;
3872  Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
3873  if (!Diff.isUsable())
3874  return nullptr;
3875 
3876  // Parentheses (for dumping/debugging purposes only).
3877  Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3878  if (!Diff.isUsable())
3879  return nullptr;
3880 
3881  // (Upper - Lower [- 1] + Step) / Step
3882  Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
3883  if (!Diff.isUsable())
3884  return nullptr;
3885 
3886  // OpenMP runtime requires 32-bit or 64-bit loop variables.
3887  QualType Type = Diff.get()->getType();
3888  auto &C = SemaRef.Context;
3889  bool UseVarType = VarType->hasIntegerRepresentation() &&
3890  C.getTypeSize(Type) > C.getTypeSize(VarType);
3891  if (!Type->isIntegerType() || UseVarType) {
3892  unsigned NewSize =
3893  UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3894  bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3896  Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
3897  if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
3898  Diff = SemaRef.PerformImplicitConversion(
3899  Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3900  if (!Diff.isUsable())
3901  return nullptr;
3902  }
3903  }
3904  if (LimitedType) {
3905  unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3906  if (NewSize != C.getTypeSize(Type)) {
3907  if (NewSize < C.getTypeSize(Type)) {
3908  assert(NewSize == 64 && "incorrect loop var size");
3909  SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3910  << InitSrcRange << ConditionSrcRange;
3911  }
3912  QualType NewType = C.getIntTypeForBitwidth(
3913  NewSize, Type->hasSignedIntegerRepresentation() ||
3914  C.getTypeSize(Type) < NewSize);
3915  if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
3916  Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3917  Sema::AA_Converting, true);
3918  if (!Diff.isUsable())
3919  return nullptr;
3920  }
3921  }
3922  }
3923 
3924  return Diff.get();
3925 }
3926 
3927 Expr *OpenMPIterationSpaceChecker::BuildPreCond(
3928  Scope *S, Expr *Cond,
3929  llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
3930  // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3931  bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3932  SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
3933 
3934  auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
3935  auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
3936  if (!NewLB.isUsable() || !NewUB.isUsable())
3937  return nullptr;
3938 
3939  auto CondExpr = SemaRef.BuildBinOp(
3940  S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3941  : (TestIsStrictOp ? BO_GT : BO_GE),
3942  NewLB.get(), NewUB.get());
3943  if (CondExpr.isUsable()) {
3944  if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
3945  SemaRef.Context.BoolTy))
3946  CondExpr = SemaRef.PerformImplicitConversion(
3947  CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3948  /*AllowExplicit=*/true);
3949  }
3950  SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3951  // Otherwise use original loop conditon and evaluate it in runtime.
3952  return CondExpr.isUsable() ? CondExpr.get() : Cond;
3953 }
3954 
3955 /// \brief Build reference expression to the counter be used for codegen.
3956 DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar(
3957  llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const {
3958  auto *VD = dyn_cast<VarDecl>(LCDecl);
3959  if (!VD) {
3960  VD = SemaRef.IsOpenMPCapturedDecl(LCDecl);
3961  auto *Ref = buildDeclRefExpr(
3962  SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
3963  DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false);
3964  // If the loop control decl is explicitly marked as private, do not mark it
3965  // as captured again.
3966  if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
3967  Captures.insert(std::make_pair(LCRef, Ref));
3968  return Ref;
3969  }
3970  return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
3971  DefaultLoc);
3972 }
3973 
3974 Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
3975  if (LCDecl && !LCDecl->isInvalidDecl()) {
3976  auto Type = LCDecl->getType().getNonReferenceType();
3977  auto *PrivateVar =
3978  buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(),
3979  LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr);
3980  if (PrivateVar->isInvalidDecl())
3981  return nullptr;
3982  return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3983  }
3984  return nullptr;
3985 }
3986 
3987 /// \brief Build initialization of the counter to be used for codegen.
3989 
3990 /// \brief Build step of the counter be used for codegen.
3991 Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3992 
3993 /// \brief Iteration space of a single for loop.
3994 struct LoopIterationSpace final {
3995  /// \brief Condition of the loop.
3996  Expr *PreCond = nullptr;
3997  /// \brief This expression calculates the number of iterations in the loop.
3998  /// It is always possible to calculate it before starting the loop.
3999  Expr *NumIterations = nullptr;
4000  /// \brief The loop counter variable.
4001  Expr *CounterVar = nullptr;
4002  /// \brief Private loop counter variable.
4003  Expr *PrivateCounterVar = nullptr;
4004  /// \brief This is initializer for the initial value of #CounterVar.
4005  Expr *CounterInit = nullptr;
4006  /// \brief This is step for the #CounterVar used to generate its update:
4007  /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
4008  Expr *CounterStep = nullptr;
4009  /// \brief Should step be subtracted?
4010  bool Subtract = false;
4011  /// \brief Source range of the loop init.
4012  SourceRange InitSrcRange;
4013  /// \brief Source range of the loop condition.
4014  SourceRange CondSrcRange;
4015  /// \brief Source range of the loop increment.
4016  SourceRange IncSrcRange;
4017 };
4018 
4019 } // namespace
4020 
4022  assert(getLangOpts().OpenMP && "OpenMP is not active.");
4023  assert(Init && "Expected loop in canonical form.");
4024  unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4025  if (AssociatedLoops > 0 &&
4026  isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4027  OpenMPIterationSpaceChecker ISC(*this, ForLoc);
4028  if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
4029  if (auto *D = ISC.GetLoopDecl()) {
4030  auto *VD = dyn_cast<VarDecl>(D);
4031  if (!VD) {
4032  if (auto *Private = IsOpenMPCapturedDecl(D))
4033  VD = Private;
4034  else {
4035  auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(),
4036  /*WithInit=*/false);
4037  VD = cast<VarDecl>(Ref->getDecl());
4038  }
4039  }
4040  DSAStack->addLoopControlVariable(D, VD);
4041  }
4042  }
4043  DSAStack->setAssociatedLoops(AssociatedLoops - 1);
4044  }
4045 }
4046 
4047 /// \brief Called on a for stmt to check and extract its iteration space
4048 /// for further processing (such as collapsing).
4050  OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4051  unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
4052  Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
4053  llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
4054  LoopIterationSpace &ResultIterSpace,
4055  llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4056  // OpenMP [2.6, Canonical Loop Form]
4057  // for (init-expr; test-expr; incr-expr) structured-block
4058  auto *For = dyn_cast_or_null<ForStmt>(S);
4059  if (!For) {
4060  SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
4061  << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
4062  << getOpenMPDirectiveName(DKind) << NestedLoopCount
4063  << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
4064  if (NestedLoopCount > 1) {
4065  if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4066  SemaRef.Diag(DSA.getConstructLoc(),
4067  diag::note_omp_collapse_ordered_expr)
4068  << 2 << CollapseLoopCountExpr->getSourceRange()
4069  << OrderedLoopCountExpr->getSourceRange();
4070  else if (CollapseLoopCountExpr)
4071  SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4072  diag::note_omp_collapse_ordered_expr)
4073  << 0 << CollapseLoopCountExpr->getSourceRange();
4074  else
4075  SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4076  diag::note_omp_collapse_ordered_expr)
4077  << 1 << OrderedLoopCountExpr->getSourceRange();
4078  }
4079  return true;
4080  }
4081  assert(For->getBody());
4082 
4083  OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4084 
4085  // Check init.
4086  auto Init = For->getInit();
4087  if (ISC.CheckInit(Init))
4088  return true;
4089 
4090  bool HasErrors = false;
4091 
4092  // Check loop variable's type.
4093  if (auto *LCDecl = ISC.GetLoopDecl()) {
4094  auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr();
4095 
4096  // OpenMP [2.6, Canonical Loop Form]
4097  // Var is one of the following:
4098  // A variable of signed or unsigned integer type.
4099  // For C++, a variable of a random access iterator type.
4100  // For C, a variable of a pointer type.
4101  auto VarType = LCDecl->getType().getNonReferenceType();
4102  if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4103  !VarType->isPointerType() &&
4104  !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
4105  SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
4106  << SemaRef.getLangOpts().CPlusPlus;
4107  HasErrors = true;
4108  }
4109 
4110  // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4111  // a Construct
4112  // The loop iteration variable(s) in the associated for-loop(s) of a for or
4113  // parallel for construct is (are) private.
4114  // The loop iteration variable in the associated for-loop of a simd
4115  // construct with just one associated for-loop is linear with a
4116  // constant-linear-step that is the increment of the associated for-loop.
4117  // Exclude loop var from the list of variables with implicitly defined data
4118  // sharing attributes.
4119  VarsWithImplicitDSA.erase(LCDecl);
4120 
4121  // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4122  // in a Construct, C/C++].
4123  // The loop iteration variable in the associated for-loop of a simd
4124  // construct with just one associated for-loop may be listed in a linear
4125  // clause with a constant-linear-step that is the increment of the
4126  // associated for-loop.
4127  // The loop iteration variable(s) in the associated for-loop(s) of a for or
4128  // parallel for construct may be listed in a private or lastprivate clause.
4129  DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4130  // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4131  // declared in the loop and it is predetermined as a private.
4132  auto PredeterminedCKind =
4133  isOpenMPSimdDirective(DKind)
4134  ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4135  : OMPC_private;
4136  if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4137  DVar.CKind != PredeterminedCKind) ||
4138  ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4139  isOpenMPDistributeDirective(DKind)) &&
4140  !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4141  DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4142  (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4143  SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
4144  << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4145  << getOpenMPClauseName(PredeterminedCKind);
4146  if (DVar.RefExpr == nullptr)
4147  DVar.CKind = PredeterminedCKind;
4148  ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
4149  HasErrors = true;
4150  } else if (LoopDeclRefExpr != nullptr) {
4151  // Make the loop iteration variable private (for worksharing constructs),
4152  // linear (for simd directives with the only one associated loop) or
4153  // lastprivate (for simd directives with several collapsed or ordered
4154  // loops).
4155  if (DVar.CKind == OMPC_unknown)
4156  DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
4157  [](OpenMPDirectiveKind) -> bool { return true; },
4158  /*FromParent=*/false);
4159  DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4160  }
4161 
4162  assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4163 
4164  // Check test-expr.
4165  HasErrors |= ISC.CheckCond(For->getCond());
4166 
4167  // Check incr-expr.
4168  HasErrors |= ISC.CheckInc(For->getInc());
4169  }
4170 
4171  if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
4172  return HasErrors;
4173 
4174  // Build the loop's iteration space representation.
4175  ResultIterSpace.PreCond =
4176  ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
4177  ResultIterSpace.NumIterations = ISC.BuildNumIterations(
4178  DSA.getCurScope(),
4179  (isOpenMPWorksharingDirective(DKind) ||
4181  Captures);
4182  ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA);
4183  ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
4184  ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4185  ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4186  ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4187  ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4188  ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4189  ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4190 
4191  HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4192  ResultIterSpace.NumIterations == nullptr ||
4193  ResultIterSpace.CounterVar == nullptr ||
4194  ResultIterSpace.PrivateCounterVar == nullptr ||
4195  ResultIterSpace.CounterInit == nullptr ||
4196  ResultIterSpace.CounterStep == nullptr);
4197 
4198  return HasErrors;
4199 }
4200 
4201 /// \brief Build 'VarRef = Start.
4202 static ExprResult
4204  ExprResult Start,
4205  llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4206  // Build 'VarRef = Start.
4207  auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
4208  if (!NewStart.isUsable())
4209  return ExprError();
4210  if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
4211  VarRef.get()->getType())) {
4212  NewStart = SemaRef.PerformImplicitConversion(
4213  NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4214  /*AllowExplicit=*/true);
4215  if (!NewStart.isUsable())
4216  return ExprError();
4217  }
4218 
4219  auto Init =
4220  SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4221  return Init;
4222 }
4223 
4224 /// \brief Build 'VarRef = Start + Iter * Step'.
4225 static ExprResult
4227  ExprResult VarRef, ExprResult Start, ExprResult Iter,
4228  ExprResult Step, bool Subtract,
4229  llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
4230  // Add parentheses (for debugging purposes only).
4231  Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4232  if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4233  !Step.isUsable())
4234  return ExprError();
4235 
4236  ExprResult NewStep = Step;
4237  if (Captures)
4238  NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
4239  if (NewStep.isInvalid())
4240  return ExprError();
4241  ExprResult Update =
4242  SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
4243  if (!Update.isUsable())
4244  return ExprError();
4245 
4246  // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4247  // 'VarRef = Start (+|-) Iter * Step'.
4248  ExprResult NewStart = Start;
4249  if (Captures)
4250  NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
4251  if (NewStart.isInvalid())
4252  return ExprError();
4253 
4254  // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4255  ExprResult SavedUpdate = Update;
4256  ExprResult UpdateVal;
4257  if (VarRef.get()->getType()->isOverloadableType() ||
4258  NewStart.get()->getType()->isOverloadableType() ||
4259  Update.get()->getType()->isOverloadableType()) {
4260  bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4261  SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4262  Update =
4263  SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4264  if (Update.isUsable()) {
4265  UpdateVal =
4266  SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4267  VarRef.get(), SavedUpdate.get());
4268  if (UpdateVal.isUsable()) {
4269  Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4270  UpdateVal.get());
4271  }
4272  }
4273  SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4274  }
4275 
4276  // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4277  if (!Update.isUsable() || !UpdateVal.isUsable()) {
4278  Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4279  NewStart.get(), SavedUpdate.get());
4280  if (!Update.isUsable())
4281  return ExprError();
4282 
4283  if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4284  VarRef.get()->getType())) {
4285  Update = SemaRef.PerformImplicitConversion(
4286  Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4287  if (!Update.isUsable())
4288  return ExprError();
4289  }
4290 
4291  Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4292  }
4293  return Update;
4294 }
4295 
4296 /// \brief Convert integer expression \a E to make it have at least \a Bits
4297 /// bits.
4298 static ExprResult WidenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
4299  if (E == nullptr)
4300  return ExprError();
4301  auto &C = SemaRef.Context;
4302  QualType OldType = E->getType();
4303  unsigned HasBits = C.getTypeSize(OldType);
4304  if (HasBits >= Bits)
4305  return ExprResult(E);
4306  // OK to convert to signed, because new type has more bits than old.
4307  QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4308  return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4309  true);
4310 }
4311 
4312 /// \brief Check if the given expression \a E is a constant integer that fits
4313 /// into \a Bits bits.
4314 static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4315  if (E == nullptr)
4316  return false;
4317  llvm::APSInt Result;
4318  if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4319  return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4320  return false;
4321 }
4322 
4323 /// Build preinits statement for the given declarations.
4324 static Stmt *buildPreInits(ASTContext &Context,
4325  SmallVectorImpl<Decl *> &PreInits) {
4326  if (!PreInits.empty()) {
4327  return new (Context) DeclStmt(
4328  DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4330  }
4331  return nullptr;
4332 }
4333 
4334 /// Build preinits statement for the given declarations.
4335 static Stmt *buildPreInits(ASTContext &Context,
4336  llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4337  if (!Captures.empty()) {
4338  SmallVector<Decl *, 16> PreInits;
4339  for (auto &Pair : Captures)
4340  PreInits.push_back(Pair.second->getDecl());
4341  return buildPreInits(Context, PreInits);
4342  }
4343  return nullptr;
4344 }
4345 
4346 /// Build postupdate expression for the given list of postupdates expressions.
4347 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
4348  Expr *PostUpdate = nullptr;
4349  if (!PostUpdates.empty()) {
4350  for (auto *E : PostUpdates) {
4351  Expr *ConvE = S.BuildCStyleCastExpr(
4352  E->getExprLoc(),
4354  E->getExprLoc(), E)
4355  .get();
4356  PostUpdate = PostUpdate
4357  ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
4358  PostUpdate, ConvE)
4359  .get()
4360  : ConvE;
4361  }
4362  }
4363  return PostUpdate;
4364 }
4365 
4366 /// \brief Called on a for stmt to check itself and nested loops (if any).
4367 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4368 /// number of collapsed loops otherwise.
4369 static unsigned
4370 CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4371  Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4372  DSAStackTy &DSA,
4373  llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
4375  unsigned NestedLoopCount = 1;
4376  if (CollapseLoopCountExpr) {
4377  // Found 'collapse' clause - calculate collapse number.
4378  llvm::APSInt Result;
4379  if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
4380  NestedLoopCount = Result.getLimitedValue();
4381  }
4382  if (OrderedLoopCountExpr) {
4383  // Found 'ordered' clause - calculate collapse number.
4384  llvm::APSInt Result;
4385  if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4386  if (Result.getLimitedValue() < NestedLoopCount) {
4387  SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4388  diag::err_omp_wrong_ordered_loop_count)
4389  << OrderedLoopCountExpr->getSourceRange();
4390  SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4391  diag::note_collapse_loop_count)
4392  << CollapseLoopCountExpr->getSourceRange();
4393  }
4394  NestedLoopCount = Result.getLimitedValue();
4395  }
4396  }
4397  // This is helper routine for loop directives (e.g., 'for', 'simd',
4398  // 'for simd', etc.).
4399  llvm::MapVector<Expr *, DeclRefExpr *> Captures;
4401  IterSpaces.resize(NestedLoopCount);
4402  Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
4403  for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
4404  if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
4405  NestedLoopCount, CollapseLoopCountExpr,
4406  OrderedLoopCountExpr, VarsWithImplicitDSA,
4407  IterSpaces[Cnt], Captures))
4408  return 0;
4409  // Move on to the next nested for loop, or to the loop body.
4410  // OpenMP [2.8.1, simd construct, Restrictions]
4411  // All loops associated with the construct must be perfectly nested; that
4412  // is, there must be no intervening code nor any OpenMP directive between
4413  // any two loops.
4414  CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
4415  }
4416 
4417  Built.clear(/* size */ NestedLoopCount);
4418 
4419  if (SemaRef.CurContext->isDependentContext())
4420  return NestedLoopCount;
4421 
4422  // An example of what is generated for the following code:
4423  //
4424  // #pragma omp simd collapse(2) ordered(2)
4425  // for (i = 0; i < NI; ++i)
4426  // for (k = 0; k < NK; ++k)
4427  // for (j = J0; j < NJ; j+=2) {
4428  // <loop body>
4429  // }
4430  //
4431  // We generate the code below.
4432  // Note: the loop body may be outlined in CodeGen.
4433  // Note: some counters may be C++ classes, operator- is used to find number of
4434  // iterations and operator+= to calculate counter value.
4435  // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4436  // or i64 is currently supported).
4437  //
4438  // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4439  // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4440  // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4441  // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4442  // // similar updates for vars in clauses (e.g. 'linear')
4443  // <loop body (using local i and j)>
4444  // }
4445  // i = NI; // assign final values of counters
4446  // j = NJ;
4447  //
4448 
4449  // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4450  // the iteration counts of the collapsed for loops.
4451  // Precondition tests if there is at least one iteration (all conditions are
4452  // true).
4453  auto PreCond = ExprResult(IterSpaces[0].PreCond);
4454  auto N0 = IterSpaces[0].NumIterations;
4455  ExprResult LastIteration32 = WidenIterationCount(
4456  32 /* Bits */, SemaRef
4457  .PerformImplicitConversion(
4458  N0->IgnoreImpCasts(), N0->getType(),
4459  Sema::AA_Converting, /*AllowExplicit=*/true)
4460  .get(),
4461  SemaRef);
4462  ExprResult LastIteration64 = WidenIterationCount(
4463  64 /* Bits */, SemaRef
4464  .PerformImplicitConversion(
4465  N0->IgnoreImpCasts(), N0->getType(),
4466  Sema::AA_Converting, /*AllowExplicit=*/true)
4467  .get(),
4468  SemaRef);
4469 
4470  if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4471  return NestedLoopCount;
4472 
4473  auto &C = SemaRef.Context;
4474  bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4475 
4476  Scope *CurScope = DSA.getCurScope();
4477  for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
4478  if (PreCond.isUsable()) {
4479  PreCond =
4480  SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd,
4481  PreCond.get(), IterSpaces[Cnt].PreCond);
4482  }
4483  auto N = IterSpaces[Cnt].NumIterations;
4484  SourceLocation Loc = N->getExprLoc();
4485  AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4486  if (LastIteration32.isUsable())
4487  LastIteration32 = SemaRef.BuildBinOp(
4488  CurScope, Loc, BO_Mul, LastIteration32.get(),
4489  SemaRef
4490  .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4492  /*AllowExplicit=*/true)
4493  .get());
4494  if (LastIteration64.isUsable())
4495  LastIteration64 = SemaRef.BuildBinOp(
4496  CurScope, Loc, BO_Mul, LastIteration64.get(),
4497  SemaRef
4498  .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4500  /*AllowExplicit=*/true)
4501  .get());
4502  }
4503 
4504  // Choose either the 32-bit or 64-bit version.
4505  ExprResult LastIteration = LastIteration64;
4506  if (LastIteration32.isUsable() &&
4507  C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4508  (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4509  FitsInto(
4510  32 /* Bits */,
4511  LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4512  LastIteration64.get(), SemaRef)))
4513  LastIteration = LastIteration32;
4514  QualType VType = LastIteration.get()->getType();
4515  QualType RealVType = VType;
4516  QualType StrideVType = VType;
4517  if (isOpenMPTaskLoopDirective(DKind)) {
4518  VType =
4519  SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
4520  StrideVType =
4521  SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
4522  }
4523 
4524  if (!LastIteration.isUsable())
4525  return 0;
4526 
4527  // Save the number of iterations.
4528  ExprResult NumIterations = LastIteration;
4529  {
4530  LastIteration = SemaRef.BuildBinOp(
4531  CurScope, LastIteration.get()->getExprLoc(), BO_Sub,
4532  LastIteration.get(),
4533  SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4534  if (!LastIteration.isUsable())
4535  return 0;
4536  }
4537 
4538  // Calculate the last iteration number beforehand instead of doing this on
4539  // each iteration. Do not do this if the number of iterations may be kfold-ed.
4540  llvm::APSInt Result;
4541  bool IsConstant =
4542  LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4543  ExprResult CalcLastIteration;
4544  if (!IsConstant) {
4545  ExprResult SaveRef =
4546  tryBuildCapture(SemaRef, LastIteration.get(), Captures);
4547  LastIteration = SaveRef;
4548 
4549  // Prepare SaveRef + 1.
4550  NumIterations = SemaRef.BuildBinOp(
4551  CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(),
4552  SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4553  if (!NumIterations.isUsable())
4554  return 0;
4555  }
4556 
4557  SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4558 
4559  // Build variables passed into runtime, necessary for worksharing directives.
4560  ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
4562  isOpenMPDistributeDirective(DKind)) {
4563  // Lower bound variable, initialized with zero.
4564  VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4565  LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
4566  SemaRef.AddInitializerToDecl(LBDecl,
4567  SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4568  /*DirectInit*/ false);
4569 
4570  // Upper bound variable, initialized with last iteration number.
4571  VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4572  UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
4573  SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4574  /*DirectInit*/ false);
4575 
4576  // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4577  // This will be used to implement clause 'lastprivate'.
4578  QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
4579  VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4580  IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
4581  SemaRef.AddInitializerToDecl(ILDecl,
4582  SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4583  /*DirectInit*/ false);
4584 
4585  // Stride variable returned by runtime (we initialize it to 1 by default).
4586  VarDecl *STDecl =
4587  buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
4588  ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
4589  SemaRef.AddInitializerToDecl(STDecl,
4590  SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4591  /*DirectInit*/ false);
4592 
4593  // Build expression: UB = min(UB, LastIteration)
4594  // It is necessary for CodeGen of directives with static scheduling.
4595  ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4596  UB.get(), LastIteration.get());
4597  ExprResult CondOp = SemaRef.ActOnConditionalOp(
4598  InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4599  EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4600  CondOp.get());
4601  EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4602 
4603  // If we have a combined directive that combines 'distribute', 'for' or
4604  // 'simd' we need to be able to access the bounds of the schedule of the
4605  // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
4606  // by scheduling 'distribute' have to be passed to the schedule of 'for'.
4607  if (isOpenMPLoopBoundSharingDirective(DKind)) {
4608 
4609  // Lower bound variable, initialized with zero.
4610  VarDecl *CombLBDecl =
4611  buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb");
4612  CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc);
4613  SemaRef.AddInitializerToDecl(
4614  CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4615  /*DirectInit*/ false);
4616 
4617  // Upper bound variable, initialized with last iteration number.
4618  VarDecl *CombUBDecl =
4619  buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub");
4620  CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc);
4621  SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(),
4622  /*DirectInit*/ false);
4623 
4624  ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
4625  CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get());
4626  ExprResult CombCondOp =
4627  SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(),
4628  LastIteration.get(), CombUB.get());
4629  CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(),
4630  CombCondOp.get());
4631  CombEUB = SemaRef.ActOnFinishFullExpr(CombEUB.get());
4632 
4633  auto *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
4634  // We expect to have at least 2 more parameters than the 'parallel'
4635  // directive does - the lower and upper bounds of the previous schedule.
4636  assert(CD->getNumParams() >= 4 &&
4637  "Unexpected number of parameters in loop combined directive");
4638 
4639  // Set the proper type for the bounds given what we learned from the
4640  // enclosed loops.
4641  auto *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
4642  auto *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
4643 
4644  // Previous lower and upper bounds are obtained from the region
4645  // parameters.
4646  PrevLB =
4647  buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
4648  PrevUB =
4649  buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
4650  }
4651  }
4652 
4653  // Build the iteration variable and its initialization before loop.
4654  ExprResult IV;
4655  ExprResult Init, CombInit;
4656  {
4657  VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
4658  IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
4659  Expr *RHS =
4660  (isOpenMPWorksharingDirective(DKind) ||
4662  ? LB.get()
4663  : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4664  Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4665  Init = SemaRef.ActOnFinishFullExpr(Init.get());
4666 
4667  if (isOpenMPLoopBoundSharingDirective(DKind)) {
4668  Expr *CombRHS =
4669  (isOpenMPWorksharingDirective(DKind) ||
4670  isOpenMPTaskLoopDirective(DKind) ||
4672  ? CombLB.get()
4673  : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4674  CombInit =
4675  SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS);
4676  CombInit = SemaRef.ActOnFinishFullExpr(CombInit.get());
4677  }
4678  }
4679 
4680  // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
4681  SourceLocation CondLoc;
4682  ExprResult Cond =
4683  (isOpenMPWorksharingDirective(DKind) ||
4685  ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4686  : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4687  NumIterations.get());
4688  ExprResult CombCond;
4689  if (isOpenMPLoopBoundSharingDirective(DKind)) {
4690  CombCond =
4691  SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), CombUB.get());
4692  }
4693  // Loop increment (IV = IV + 1)
4694  SourceLocation IncLoc;
4695  ExprResult Inc =
4696  SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4697  SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4698  if (!Inc.isUsable())
4699  return 0;
4700  Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
4701  Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4702  if (!Inc.isUsable())
4703  return 0;
4704 
4705  // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4706  // Used for directives with static scheduling.
4707  // In combined construct, add combined version that use CombLB and CombUB
4708  // base variables for the update
4709  ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
4711  isOpenMPDistributeDirective(DKind)) {
4712  // LB + ST
4713  NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4714  if (!NextLB.isUsable())
4715  return 0;
4716  // LB = LB + ST
4717  NextLB =
4718  SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4719  NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4720  if (!NextLB.isUsable())
4721  return 0;
4722  // UB + ST
4723  NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4724  if (!NextUB.isUsable())
4725  return 0;
4726  // UB = UB + ST
4727  NextUB =
4728  SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4729  NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4730  if (!NextUB.isUsable())
4731  return 0;
4732  if (isOpenMPLoopBoundSharingDirective(DKind)) {
4733  CombNextLB =
4734  SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get());
4735  if (!NextLB.isUsable())
4736  return 0;
4737  // LB = LB + ST
4738  CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(),
4739  CombNextLB.get());
4740  CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get());
4741  if (!CombNextLB.isUsable())
4742  return 0;
4743  // UB + ST
4744  CombNextUB =
4745  SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get());
4746  if (!CombNextUB.isUsable())
4747  return 0;
4748  // UB = UB + ST
4749  CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(),
4750  CombNextUB.get());
4751  CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get());
4752  if (!CombNextUB.isUsable())
4753  return 0;
4754  }
4755  }
4756 
4757  // Create increment expression for distribute loop when combined in a same
4758  // directive with for as IV = IV + ST; ensure upper bound expression based
4759  // on PrevUB instead of NumIterations - used to implement 'for' when found
4760  // in combination with 'distribute', like in 'distribute parallel for'
4761  SourceLocation DistIncLoc;
4762  ExprResult DistCond, DistInc, PrevEUB;
4763  if (isOpenMPLoopBoundSharingDirective(DKind)) {
4764  DistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get());
4765  assert(DistCond.isUsable() && "distribute cond expr was not built");
4766 
4767  DistInc =
4768  SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get());
4769  assert(DistInc.isUsable() && "distribute inc expr was not built");
4770  DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(),
4771  DistInc.get());
4772  DistInc = SemaRef.ActOnFinishFullExpr(DistInc.get());
4773  assert(DistInc.isUsable() && "distribute inc expr was not built");
4774 
4775  // Build expression: UB = min(UB, prevUB) for #for in composite or combined
4776  // construct
4777  SourceLocation DistEUBLoc;
4778  ExprResult IsUBGreater =
4779  SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get());
4780  ExprResult CondOp = SemaRef.ActOnConditionalOp(
4781  DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get());
4782  PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(),
4783  CondOp.get());
4784  PrevEUB = SemaRef.ActOnFinishFullExpr(PrevEUB.get());
4785  }
4786 
4787  // Build updates and final values of the loop counters.
4788  bool HasErrors = false;
4789  Built.Counters.resize(NestedLoopCount);
4790  Built.Inits.resize(NestedLoopCount);
4791  Built.Updates.resize(NestedLoopCount);
4792  Built.Finals.resize(NestedLoopCount);
4793  SmallVector<Expr *, 4> LoopMultipliers;
4794  {
4795  ExprResult Div;
4796  // Go from inner nested loop to outer.
4797  for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4798  LoopIterationSpace &IS = IterSpaces[Cnt];
4799  SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4800  // Build: Iter = (IV / Div) % IS.NumIters
4801  // where Div is product of previous iterations' IS.NumIters.
4802  ExprResult Iter;
4803  if (Div.isUsable()) {
4804  Iter =
4805  SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4806  } else {
4807  Iter = IV;
4808  assert((Cnt == (int)NestedLoopCount - 1) &&
4809  "unusable div expected on first iteration only");
4810  }
4811 
4812  if (Cnt != 0 && Iter.isUsable())
4813  Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4814  IS.NumIterations);
4815  if (!Iter.isUsable()) {
4816  HasErrors = true;
4817  break;
4818  }
4819 
4820  // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
4821  auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
4822  auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(),
4823  IS.CounterVar->getExprLoc(),
4824  /*RefersToCapture=*/true);
4825  ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
4826  IS.CounterInit, Captures);
4827  if (!Init.isUsable()) {
4828  HasErrors = true;
4829  break;
4830  }
4832  SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
4833  IS.CounterStep, IS.Subtract, &Captures);
4834  if (!Update.isUsable()) {
4835  HasErrors = true;
4836  break;
4837  }
4838 
4839  // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4841  SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
4842  IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
4843  if (!Final.isUsable()) {
4844  HasErrors = true;
4845  break;
4846  }
4847 
4848  // Build Div for the next iteration: Div <- Div * IS.NumIters
4849  if (Cnt != 0) {
4850  if (Div.isUnset())
4851  Div = IS.NumIterations;
4852  else
4853  Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4854  IS.NumIterations);
4855 
4856  // Add parentheses (for debugging purposes only).
4857  if (Div.isUsable())
4858  Div = tryBuildCapture(SemaRef, Div.get(), Captures);
4859  if (!Div.isUsable()) {
4860  HasErrors = true;
4861  break;
4862  }
4863  LoopMultipliers.push_back(Div.get());
4864  }
4865  if (!Update.isUsable() || !Final.isUsable()) {
4866  HasErrors = true;
4867  break;
4868  }
4869  // Save results
4870  Built.Counters[Cnt] = IS.CounterVar;
4871  Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
4872  Built.Inits[Cnt] = Init.get();
4873  Built.Updates[Cnt] = Update.get();
4874  Built.Finals[Cnt] = Final.get();
4875  }
4876  }
4877 
4878  if (HasErrors)
4879  return 0;
4880 
4881  // Save results
4882  Built.IterationVarRef = IV.get();
4883  Built.LastIteration = LastIteration.get();
4884  Built.NumIterations = NumIterations.get();
4885  Built.CalcLastIteration =
4886  SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
4887  Built.PreCond = PreCond.get();
4888  Built.PreInits = buildPreInits(C, Captures);
4889  Built.Cond = Cond.get();
4890  Built.Init = Init.get();
4891  Built.Inc = Inc.get();
4892  Built.LB = LB.get();
4893  Built.UB = UB.get();
4894  Built.IL = IL.get();
4895  Built.ST = ST.get();
4896  Built.EUB = EUB.get();
4897  Built.NLB = NextLB.get();
4898  Built.NUB = NextUB.get();
4899  Built.PrevLB = PrevLB.get();
4900  Built.PrevUB = PrevUB.get();
4901  Built.DistInc = DistInc.get();
4902  Built.PrevEUB = PrevEUB.get();
4903  Built.DistCombinedFields.LB = CombLB.get();
4904  Built.DistCombinedFields.UB = CombUB.get();
4905  Built.DistCombinedFields.EUB = CombEUB.get();
4906  Built.DistCombinedFields.Init = CombInit.get();
4907  Built.DistCombinedFields.Cond = CombCond.get();
4908  Built.DistCombinedFields.NLB = CombNextLB.get();
4909  Built.DistCombinedFields.NUB = CombNextUB.get();
4910 
4911  Expr *CounterVal = SemaRef.DefaultLvalueConversion(IV.get()).get();
4912  // Fill data for doacross depend clauses.
4913  for (auto Pair : DSA.getDoacrossDependClauses()) {
4914  if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
4915  Pair.first->setCounterValue(CounterVal);
4916  else {
4917  if (NestedLoopCount != Pair.second.size() ||
4918  NestedLoopCount != LoopMultipliers.size() + 1) {
4919  // Erroneous case - clause has some problems.
4920  Pair.first->setCounterValue(CounterVal);
4921  continue;
4922  }
4923  assert(Pair.first->getDependencyKind() == OMPC_DEPEND_sink);
4924  auto I = Pair.second.rbegin();
4925  auto IS = IterSpaces.rbegin();
4926  auto ILM = LoopMultipliers.rbegin();
4927  Expr *UpCounterVal = CounterVal;
4928  Expr *Multiplier = nullptr;
4929  for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4930  if (I->first) {
4931  assert(IS->CounterStep);
4932  Expr *NormalizedOffset =
4933  SemaRef
4934  .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Div,
4935  I->first, IS->CounterStep)
4936  .get();
4937  if (Multiplier) {
4938  NormalizedOffset =
4939  SemaRef
4940  .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Mul,
4941  NormalizedOffset, Multiplier)
4942  .get();
4943  }
4944  assert(I->second == OO_Plus || I->second == OO_Minus);
4945  BinaryOperatorKind BOK = (I->second == OO_Plus) ? BO_Add : BO_Sub;
4946  UpCounterVal = SemaRef
4947  .BuildBinOp(CurScope, I->first->getExprLoc(), BOK,
4948  UpCounterVal, NormalizedOffset)
4949  .get();
4950  }
4951  Multiplier = *ILM;
4952  ++I;
4953  ++IS;
4954  ++ILM;
4955  }
4956  Pair.first->setCounterValue(UpCounterVal);
4957  }
4958  }
4959 
4960  return NestedLoopCount;
4961 }
4962 
4964  auto CollapseClauses =
4965  OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4966  if (CollapseClauses.begin() != CollapseClauses.end())
4967  return (*CollapseClauses.begin())->getNumForLoops();
4968  return nullptr;
4969 }
4970 
4972  auto OrderedClauses =
4973  OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4974  if (OrderedClauses.begin() != OrderedClauses.end())
4975  return (*OrderedClauses.begin())->getNumForLoops();
4976  return nullptr;
4977 }
4978 
4980  const ArrayRef<OMPClause *> Clauses) {
4981  OMPSafelenClause *Safelen = nullptr;
4982  OMPSimdlenClause *Simdlen = nullptr;
4983 
4984  for (auto *Clause : Clauses) {
4985  if (Clause->getClauseKind() == OMPC_safelen)
4986  Safelen = cast<OMPSafelenClause>(Clause);
4987  else if (Clause->getClauseKind() == OMPC_simdlen)
4988  Simdlen = cast<OMPSimdlenClause>(Clause);
4989  if (Safelen && Simdlen)
4990  break;
4991  }
4992 
4993  if (Simdlen && Safelen) {
4994  llvm::APSInt SimdlenRes, SafelenRes;
4995  auto SimdlenLength = Simdlen->getSimdlen();
4996  auto SafelenLength = Safelen->getSafelen();
4997  if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
4998  SimdlenLength->isInstantiationDependent() ||
4999  SimdlenLength->containsUnexpandedParameterPack())
5000  return false;
5001  if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
5002  SafelenLength->isInstantiationDependent() ||
5003  SafelenLength->containsUnexpandedParameterPack())
5004  return false;
5005  SimdlenLength->EvaluateAsInt(SimdlenRes, S.Context);
5006  SafelenLength->EvaluateAsInt(SafelenRes, S.Context);
5007  // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
5008  // If both simdlen and safelen clauses are specified, the value of the
5009  // simdlen parameter must be less than or equal to the value of the safelen
5010  // parameter.
5011  if (SimdlenRes > SafelenRes) {
5012  S.Diag(SimdlenLength->getExprLoc(),
5013  diag::err_omp_wrong_simdlen_safelen_values)
5014  << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
5015  return true;
5016  }
5017  }
5018  return false;
5019 }
5020 
5022  ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5023  SourceLocation EndLoc,
5024  llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5025  if (!AStmt)
5026  return StmtError();
5027 
5028  assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5030  // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5031  // define the nested loops number.
5032  unsigned NestedLoopCount = CheckOpenMPLoop(
5033  OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5034  AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
5035  if (NestedLoopCount == 0)
5036  return StmtError();
5037 
5038  assert((CurContext->isDependentContext() || B.builtAll()) &&
5039  "omp simd loop exprs were not built");
5040 
5041  if (!CurContext->isDependentContext()) {
5042  // Finalize the clauses that need pre-built expressions for CodeGen.
5043  for (auto C : Clauses) {
5044  if (auto *LC = dyn_cast<OMPLinearClause>(C))
5045  if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5046  B.NumIterations, *this, CurScope,
5047  DSAStack))
5048  return StmtError();
5049  }
5050  }
5051 
5052  if (checkSimdlenSafelenSpecified(*this, Clauses))
5053  return StmtError();
5054 
5055  getCurFunction()->setHasBranchProtectedScope();
5056  return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5057  Clauses, AStmt, B);
5058 }
5059 
5061  ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5062  SourceLocation EndLoc,
5063  llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5064  if (!AStmt)
5065  return StmtError();
5066 
5067  assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5069  // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5070  // define the nested loops number.
5071  unsigned NestedLoopCount = CheckOpenMPLoop(
5072  OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5073  AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
5074  if (NestedLoopCount == 0)
5075  return StmtError();
5076 
5077  assert((CurContext->isDependentContext() || B.builtAll()) &&
5078  "omp for loop exprs were not built");
5079 
5080  if (!CurContext->isDependentContext()) {
5081  // Finalize the clauses that need pre-built expressions for CodeGen.
5082  for (auto C : Clauses) {
5083  if (auto *LC = dyn_cast<OMPLinearClause>(C))
5084  if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5085  B.NumIterations, *this, CurScope,
5086  DSAStack))
5087  return StmtError();
5088  }
5089  }
5090 
5091  getCurFunction()->setHasBranchProtectedScope();
5092  return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5093  Clauses, AStmt, B, DSAStack->isCancelRegion());
5094 }
5095 
5097  ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5098  SourceLocation EndLoc,
5099  llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5100  if (!AStmt)
5101  return StmtError();
5102 
5103  assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5105  // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5106  // define the nested loops number.
5107  unsigned NestedLoopCount =
5108  CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
5109  getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5110  VarsWithImplicitDSA, B);
5111  if (NestedLoopCount == 0)
5112  return StmtError();
5113 
5114  assert((CurContext->isDependentContext() || B.builtAll()) &&
5115  "omp for simd loop exprs were not built");
5116 
5117  if (!CurContext->isDependentContext()) {
5118  // Finalize the clauses that need pre-built expressions for CodeGen.
5119  for (auto C : Clauses) {
5120  if (auto *LC = dyn_cast<OMPLinearClause>(C))
5121  if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5122  B.NumIterations, *this, CurScope,
5123  DSAStack))
5124  return StmtError();
5125  }
5126  }
5127 
5128  if (checkSimdlenSafelenSpecified(*this, Clauses))
5129  return StmtError();
5130 
5131  getCurFunction()->setHasBranchProtectedScope();
5132  return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5133  Clauses, AStmt, B);
5134 }
5135 
5137  Stmt *AStmt,
5138  SourceLocation StartLoc,
5139  SourceLocation EndLoc) {
5140  if (!AStmt)
5141  return StmtError();
5142 
5143  assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5144  auto BaseStmt = AStmt;
5145  while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5146  BaseStmt = CS->getCapturedStmt();
5147  if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5148  auto S = C->children();
5149  if (S.begin() == S.end())
5150  return StmtError();
5151  // All associated statements must be '#pragma omp section' except for
5152  // the first one.
5153  for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
5154  if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5155  if (SectionStmt)
5156  Diag(SectionStmt->getLocStart(),
5157  diag::err_omp_sections_substmt_not_section);
5158  return StmtError();
5159  }
5160  cast<OMPSectionDirective>(SectionStmt)
5161  ->setHasCancel(DSAStack->isCancelRegion());
5162  }
5163  } else {
5164  Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
5165  return StmtError();
5166  }
5167 
5168  getCurFunction()->setHasBranchProtectedScope();
5169 
5170  return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5171  DSAStack->isCancelRegion());
5172 }
5173 
5175  SourceLocation StartLoc,
5176  SourceLocation EndLoc) {
5177  if (!AStmt)
5178  return StmtError();
5179 
5180  assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5181 
5182  getCurFunction()->setHasBranchProtectedScope();
5183  DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
5184 
5185  return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5186  DSAStack->isCancelRegion());
5187 }
5188 
5190  Stmt *AStmt,
5191  SourceLocation StartLoc,
5192  SourceLocation EndLoc) {
5193  if (!AStmt)
5194  return StmtError();
5195 
5196  assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5197 
5198  getCurFunction()->setHasBranchProtectedScope();
5199 
5200  // OpenMP [2.7.3, single Construct, Restrictions]
5201  // The copyprivate clause must not be used with the nowait clause.
5202  OMPClause *Nowait = nullptr;
5203  OMPClause *Copyprivate = nullptr;
5204  for (auto *Clause : Clauses) {
5205  if (Clause->getClauseKind() == OMPC_nowait)
5206  Nowait = Clause;
5207  else if (Clause->getClauseKind() == OMPC_copyprivate)
5208  Copyprivate = Clause;
5209  if (Copyprivate && Nowait) {
5210  Diag(Copyprivate->getLocStart(),
5211  diag::err_omp_single_copyprivate_with_nowait);
5212  Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
5213  return StmtError();
5214  }
5215  }
5216 
5217  return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5218 }
5219 
5221  SourceLocation StartLoc,
5222  SourceLocation EndLoc) {
5223  if (!AStmt)
5224  return StmtError();
5225 
5226  assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5227 
5228  getCurFunction()->setHasBranchProtectedScope();
5229 
5230  return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5231 }
5232 
5234  const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5235  Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
5236  if (!AStmt)
5237  return StmtError();
5238 
5239  assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5240 
5241  bool ErrorFound = false;
5242  llvm::APSInt Hint;
5243  SourceLocation HintLoc;
5244  bool DependentHint = false;
5245  for (auto *C : Clauses) {
5246  if (C->getClauseKind() == OMPC_hint) {
5247  if (!DirName.getName()) {
5248  Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
5249  ErrorFound = true;
5250  }
5251  Expr *E = cast<OMPHintClause>(C)->getHint();
5252  if (E->isTypeDependent() || E->isValueDependent() ||
5254  DependentHint = true;
5255  else {
5256  Hint = E->EvaluateKnownConstInt(Context);
5257  HintLoc = C->getLocStart();
5258  }
5259  }
5260  }
5261  if (ErrorFound)
5262  return StmtError();
5263  auto Pair = DSAStack->getCriticalWithHint(DirName);
5264  if (Pair.first && DirName.getName() && !DependentHint) {
5265  if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5266  Diag(StartLoc, diag::err_omp_critical_with_hint);
5267  if (HintLoc.isValid()) {
5268  Diag(HintLoc, diag::note_omp_critical_hint_here)
5269  << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
5270  } else
5271  Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
5272  if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
5273  Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
5274  << 1
5275  << C->getHint()->EvaluateKnownConstInt(Context).toString(
5276  /*Radix=*/10, /*Signed=*/false);
5277  } else
5278  Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
5279  }
5280  }
5281 
5282  getCurFunction()->setHasBranchProtectedScope();
5283 
5284  auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5285  Clauses, AStmt);
5286  if (!Pair.first && DirName.getName() && !DependentHint)
5287  DSAStack->addCriticalWithHint(Dir, Hint);
5288  return Dir;
5289 }
5290 
5292  ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5293  SourceLocation EndLoc,
5294  llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5295  if (!AStmt)
5296  return StmtError();
5297 
5298  CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5299  // 1.2.2 OpenMP Language Terminology
5300  // Structured block - An executable statement with a single entry at the
5301  // top and a single exit at the bottom.
5302  // The point of exit cannot be a branch out of the structured block.
5303  // longjmp() and throw() must not violate the entry/exit criteria.
5304  CS->getCapturedDecl()->setNothrow();
5305 
5307  // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5308  // define the nested loops number.
5309  unsigned NestedLoopCount =
5310  CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
5311  getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5312  VarsWithImplicitDSA, B);
5313  if (NestedLoopCount == 0)
5314  return StmtError();
5315 
5316  assert((CurContext->isDependentContext() || B.builtAll()) &&
5317  "omp parallel for loop exprs were not built");
5318 
5319  if (!CurContext->isDependentContext()) {
5320  // Finalize the clauses that need pre-built expressions for CodeGen.
5321  for (auto C : Clauses) {
5322  if (auto *LC = dyn_cast<OMPLinearClause>(C))
5323  if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5324  B.NumIterations, *this, CurScope,
5325  DSAStack))
5326  return StmtError();
5327  }
5328  }
5329 
5330  getCurFunction()->setHasBranchProtectedScope();
5331  return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
5332  NestedLoopCount, Clauses, AStmt, B,
5333  DSAStack->isCancelRegion());
5334 }
5335 
5337  ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5338  SourceLocation EndLoc,
5339  llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5340  if (!AStmt)
5341  return StmtError();
5342 
5343  CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5344  // 1.2.2 OpenMP Language Terminology
5345  // Structured block - An executable statement with a single entry at the
5346  // top and a single exit at the bottom.
5347  // The point of exit cannot be a branch out of the structured block.
5348  // longjmp() and throw() must not violate the entry/exit criteria.
5349  CS->getCapturedDecl()->setNothrow();
5350 
5352  // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5353  // define the nested loops number.
5354  unsigned NestedLoopCount =
5355  CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
5356  getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5357  VarsWithImplicitDSA, B);
5358  if (NestedLoopCount == 0)
5359  return StmtError();
5360 
5361  if (!CurContext->isDependentContext()) {
5362  // Finalize the clauses that need pre-built expressions for CodeGen.
5363  for (auto C : Clauses) {
5364  if (auto *LC = dyn_cast<OMPLinearClause>(C))
5365  if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5366  B.NumIterations, *this, CurScope,
5367  DSAStack))
5368  return StmtError();
5369  }
5370  }
5371 
5372  if (checkSimdlenSafelenSpecified(*this, Clauses))
5373  return StmtError();
5374 
5375  getCurFunction()->setHasBranchProtectedScope();
5377  Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
5378 }
5379 
5380 StmtResult
5382  Stmt *AStmt, SourceLocation StartLoc,
5383  SourceLocation EndLoc) {
5384  if (!AStmt)
5385  return StmtError();
5386 
5387  assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5388  auto BaseStmt = AStmt;
5389  while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5390  BaseStmt = CS->getCapturedStmt();
5391  if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5392  auto S = C->children();
5393  if (S.begin() == S.end())
5394  return StmtError();
5395  // All associated statements must be '#pragma omp section' except for
5396  // the first one.
5397  for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
5398  if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5399  if (SectionStmt)
5400  Diag(SectionStmt->getLocStart(),
5401  diag::err_omp_parallel_sections_substmt_not_section);
5402  return StmtError();
5403  }
5404  cast<OMPSectionDirective>(SectionStmt)
5405  ->setHasCancel(DSAStack->isCancelRegion());
5406  }
5407  } else {
5408  Diag(AStmt->getLocStart(),
5409  diag::err_omp_parallel_sections_not_compound_stmt);
5410  return StmtError();
5411  }
5412 
5413  getCurFunction()->setHasBranchProtectedScope();
5414 
5416  Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
5417 }
5418 
5420  Stmt *AStmt, SourceLocation StartLoc,
5421  SourceLocation EndLoc) {
5422  if (!AStmt)
5423  return StmtError();
5424 
5425  auto *CS = cast<CapturedStmt>(AStmt);
5426  // 1.2.2 OpenMP Language Terminology
5427  // Structured block - An executable statement with a single entry at the
5428  // top and a single exit at the bottom.
5429  // The point of exit cannot be a branch out of the structured block.
5430  // longjmp() and throw() must not violate the entry/exit criteria.
5431  CS->getCapturedDecl()->setNothrow();
5432 
5433  getCurFunction()->setHasBranchProtectedScope();
5434 
5435  return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5436  DSAStack->isCancelRegion());
5437 }
5438 
5440  SourceLocation EndLoc) {
5441  return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5442 }
5443 
5445  SourceLocation EndLoc) {
5446  return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5447 }
5448 
5450  SourceLocation EndLoc) {
5451  return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5452 }
5453 
5455  Stmt *AStmt,
5456  SourceLocation StartLoc,
5457  SourceLocation EndLoc) {
5458  if (!AStmt)
5459  return StmtError();
5460 
5461  assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5462 
5463  getCurFunction()->setHasBranchProtectedScope();
5464 
5465  return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses,
5466  AStmt,
5467  DSAStack->getTaskgroupReductionRef());
5468 }
5469 
5471  SourceLocation StartLoc,
5472  SourceLocation EndLoc) {
5473  assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5474  return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5475 }
5476 
5478  Stmt *AStmt,
5479  SourceLocation StartLoc,
5480  SourceLocation EndLoc) {
5481  OMPClause *DependFound = nullptr;
5482  OMPClause *DependSourceClause = nullptr;
5483  OMPClause *DependSinkClause = nullptr;
5484  bool ErrorFound = false;
5485  OMPThreadsClause *TC = nullptr;
5486  OMPSIMDClause *SC = nullptr;
5487  for (auto *C : Clauses) {
5488  if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5489  DependFound = C;
5490  if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5491  if (DependSourceClause) {
5492  Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5493  << getOpenMPDirectiveName(OMPD_ordered)
5494  << getOpenMPClauseName(OMPC_depend) << 2;
5495  ErrorFound = true;
5496  } else
5497  DependSourceClause = C;
5498  if (DependSinkClause) {
5499  Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5500  << 0;
5501  ErrorFound = true;
5502  }
5503  } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5504  if (DependSourceClause) {
5505  Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5506  << 1;
5507  ErrorFound = true;
5508  }
5509  DependSinkClause = C;
5510  }
5511  } else if (C->getClauseKind() == OMPC_threads)
5512  TC = cast<OMPThreadsClause>(C);
5513  else if (C->getClauseKind() == OMPC_simd)
5514  SC = cast<OMPSIMDClause>(C);
5515  }
5516  if (!ErrorFound && !SC &&
5517  isOpenMPSimdDirective(DSAStack->getParentDirective())) {
5518  // OpenMP [2.8.1,simd Construct, Restrictions]
5519  // An ordered construct with the simd clause is the only OpenMP construct
5520  // that can appear in the simd region.
5521  Diag(StartLoc, diag::err_omp_prohibited_region_simd);
5522  ErrorFound = true;
5523  } else if (DependFound && (TC || SC)) {
5524  Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5525  << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5526  ErrorFound = true;
5527  } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
5528  Diag(DependFound->getLocStart(),
5529  diag::err_omp_ordered_directive_without_param);
5530  ErrorFound = true;
5531  } else if (TC || Clauses.empty()) {
5532  if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
5533  SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
5534  Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5535  << (TC != nullptr);
5536  Diag(Param->getLocStart(), diag::note_omp_ordered_param);
5537  ErrorFound = true;
5538  }
5539  }
5540  if ((!AStmt && !DependFound) || ErrorFound)
5541  return StmtError();
5542 
5543  if (AStmt) {
5544  assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5545 
5546  getCurFunction()->setHasBranchProtectedScope();
5547  }
5548 
5549  return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5550 }
5551 
5552 namespace {
5553 /// \brief Helper class for checking expression in 'omp atomic [update]'
5554 /// construct.
5555 class OpenMPAtomicUpdateChecker {
5556  /// \brief Error results for atomic update expressions.
5557  enum ExprAnalysisErrorCode {
5558  /// \brief A statement is not an expression statement.
5559  NotAnExpression,
5560  /// \brief Expression is not builtin binary or unary operation.
5561  NotABinaryOrUnaryExpression,
5562  /// \brief Unary operation is not post-/pre- increment/decrement operation.
5563  NotAnUnaryIncDecExpression,
5564  /// \brief An expression is not of scalar type.
5565  NotAScalarType,
5566  /// \brief A binary operation is not an assignment operation.
5567  NotAnAssignmentOp,
5568  /// \brief RHS part of the binary operation is not a binary expression.
5569  NotABinaryExpression,
5570  /// \brief RHS part is not additive/multiplicative/shift/biwise binary
5571  /// expression.
5572  NotABinaryOperator,
5573  /// \brief RHS binary operation does not have reference to the updated LHS
5574  /// part.
5575  NotAnUpdateExpression,
5576  /// \brief No errors is found.
5577  NoError
5578  };
5579  /// \brief Reference to Sema.
5580  Sema &SemaRef;
5581  /// \brief A location for note diagnostics (when error is found).
5582  SourceLocation NoteLoc;
5583  /// \brief 'x' lvalue part of the source atomic expression.
5584  Expr *X;
5585  /// \brief 'expr' rvalue part of the source atomic expression.
5586  Expr *E;
5587  /// \brief Helper expression of the form
5588  /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5589  /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5590  Expr *UpdateExpr;
5591  /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5592  /// important for non-associative operations.
5593  bool IsXLHSInRHSPart;
5594  BinaryOperatorKind Op;
5595  SourceLocation OpLoc;
5596  /// \brief true if the source expression is a postfix unary operation, false
5597  /// if it is a prefix unary operation.
5598  bool IsPostfixUpdate;
5599 
5600 public:
5601  OpenMPAtomicUpdateChecker(Sema &SemaRef)
5602  : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
5603  IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
5604  /// \brief Check specified statement that it is suitable for 'atomic update'
5605  /// constructs and extract 'x', 'expr' and Operation from the original
5606  /// expression. If DiagId and NoteId == 0, then only check is performed
5607  /// without error notification.
5608  /// \param DiagId Diagnostic which should be emitted if error is found.
5609  /// \param NoteId Diagnostic note for the main error message.
5610  /// \return true if statement is not an update expression, false otherwise.
5611  bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
5612  /// \brief Return the 'x' lvalue part of the source atomic expression.
5613  Expr *getX() const { return X; }
5614  /// \brief Return the 'expr' rvalue part of the source atomic expression.
5615  Expr *getExpr() const { return E; }
5616  /// \brief Return the update expression used in calculation of the updated
5617  /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5618  /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5619  Expr *getUpdateExpr() const { return UpdateExpr; }
5620  /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5621  /// false otherwise.
5622  bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5623 
5624  /// \brief true if the source expression is a postfix unary operation, false
5625  /// if it is a prefix unary operation.
5626  bool isPostfixUpdate() const { return IsPostfixUpdate; }
5627 
5628 private:
5629  bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5630  unsigned NoteId = 0);
5631 };
5632 } // namespace
5633 
5634 bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5635  BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5636  ExprAnalysisErrorCode ErrorFound = NoError;
5637  SourceLocation ErrorLoc, NoteLoc;
5638  SourceRange ErrorRange, NoteRange;
5639  // Allowed constructs are:
5640  // x = x binop expr;
5641  // x = expr binop x;
5642  if (AtomicBinOp->getOpcode() == BO_Assign) {
5643  X = AtomicBinOp->getLHS();
5644  if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5645  AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5646  if (AtomicInnerBinOp->isMultiplicativeOp() ||
5647  AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5648  AtomicInnerBinOp->isBitwiseOp()) {
5649  Op = AtomicInnerBinOp->getOpcode();
5650  OpLoc = AtomicInnerBinOp->getOperatorLoc();
5651  auto *LHS = AtomicInnerBinOp->getLHS();
5652  auto *RHS = AtomicInnerBinOp->getRHS();
5653  llvm::FoldingSetNodeID XId, LHSId, RHSId;
5654  X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5655  /*Canonical=*/true);
5656  LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5657  /*Canonical=*/true);
5658  RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5659  /*Canonical=*/true);
5660  if (XId == LHSId) {
5661  E = RHS;
5662  IsXLHSInRHSPart = true;
5663  } else if (XId == RHSId) {
5664  E = LHS;
5665  IsXLHSInRHSPart = false;
5666  } else {
5667  ErrorLoc = AtomicInnerBinOp->getExprLoc();
5668  ErrorRange = AtomicInnerBinOp->getSourceRange();
5669  NoteLoc = X->getExprLoc();
5670  NoteRange = X->getSourceRange();
5671  ErrorFound = NotAnUpdateExpression;
5672  }
5673  } else {
5674  ErrorLoc = AtomicInnerBinOp->getExprLoc();
5675  ErrorRange = AtomicInnerBinOp->getSourceRange();
5676  NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5677  NoteRange = SourceRange(NoteLoc, NoteLoc);
5678  ErrorFound = NotABinaryOperator;
5679  }
5680  } else {
5681  NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5682  NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5683  ErrorFound = NotABinaryExpression;
5684  }
5685  } else {
5686  ErrorLoc = AtomicBinOp->getExprLoc();
5687  ErrorRange = AtomicBinOp->getSourceRange();
5688  NoteLoc = AtomicBinOp->getOperatorLoc();
5689  NoteRange = SourceRange(NoteLoc, NoteLoc);
5690  ErrorFound = NotAnAssignmentOp;
5691  }
5692  if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
5693  SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5694  SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5695  return true;
5696  } else if (SemaRef.CurContext->isDependentContext())
5697  E = X = UpdateExpr = nullptr;
5698  return ErrorFound != NoError;
5699 }
5700 
5701 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5702  unsigned NoteId) {
5703  ExprAnalysisErrorCode ErrorFound = NoError;
5704  SourceLocation ErrorLoc, NoteLoc;
5705  SourceRange ErrorRange, NoteRange;
5706  // Allowed constructs are:
5707  // x++;
5708  // x--;
5709  // ++x;
5710  // --x;
5711  // x binop= expr;
5712  // x = x binop expr;
5713  // x = expr binop x;
5714  if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5715  AtomicBody = AtomicBody->IgnoreParenImpCasts();
5716  if (AtomicBody->getType()->isScalarType() ||
5717  AtomicBody->isInstantiationDependent()) {
5718  if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5719  AtomicBody->IgnoreParenImpCasts())) {
5720  // Check for Compound Assignment Operation
5722  AtomicCompAssignOp->getOpcode());
5723  OpLoc = AtomicCompAssignOp->getOperatorLoc();
5724  E = AtomicCompAssignOp->getRHS();
5725  X = AtomicCompAssignOp->getLHS()->IgnoreParens();
5726  IsXLHSInRHSPart = true;
5727  } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5728  AtomicBody->IgnoreParenImpCasts())) {
5729  // Check for Binary Operation
5730  if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
5731  return true;
5732  } else if (auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
5733  AtomicBody->IgnoreParenImpCasts())) {
5734  // Check for Unary Operation
5735  if (AtomicUnaryOp->isIncrementDecrementOp()) {
5736  IsPostfixUpdate = AtomicUnaryOp->isPostfix();
5737  Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5738  OpLoc = AtomicUnaryOp->getOperatorLoc();
5739  X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
5740  E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5741  IsXLHSInRHSPart = true;
5742  } else {
5743  ErrorFound = NotAnUnaryIncDecExpression;
5744  ErrorLoc = AtomicUnaryOp->getExprLoc();
5745  ErrorRange = AtomicUnaryOp->getSourceRange();
5746  NoteLoc = AtomicUnaryOp->getOperatorLoc();
5747  NoteRange = SourceRange(NoteLoc, NoteLoc);
5748  }
5749  } else if (!AtomicBody->isInstantiationDependent()) {
5750  ErrorFound = NotABinaryOrUnaryExpression;
5751  NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5752  NoteRange = ErrorRange = AtomicBody->getSourceRange();
5753  }
5754  }