blob: 8d6d01e6eb021002fc2074cf149ef9ed0a7c82a9 [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
Alexey Bataeva769e072013-03-22 06:34:35 +00002//
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
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000011/// clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000012///
13//===----------------------------------------------------------------------===//
14
Alexey Bataev9959db52014-05-06 10:08:46 +000015#include "clang/AST/ASTContext.h"
Alexey Bataev97720002014-11-11 04:05:39 +000016#include "clang/AST/ASTMutationListener.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000017#include "clang/AST/Decl.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000018#include "clang/AST/DeclCXX.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000019#include "clang/AST/DeclOpenMP.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000020#include "clang/AST/StmtCXX.h"
21#include "clang/AST/StmtOpenMP.h"
22#include "clang/AST/StmtVisitor.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000023#include "clang/Basic/OpenMPKinds.h"
24#include "clang/Lex/Preprocessor.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000025#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000026#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000027#include "clang/Sema/Scope.h"
28#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000029#include "clang/Sema/SemaInternal.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000030using namespace clang;
31
Alexey Bataev758e55e2013-09-06 18:03:48 +000032//===----------------------------------------------------------------------===//
33// Stack of data-sharing attributes for variables
34//===----------------------------------------------------------------------===//
35
36namespace {
37/// \brief Default data sharing attributes, which can be applied to directive.
38enum DefaultDataSharingAttributes {
Alexey Bataeved09d242014-05-28 05:53:51 +000039 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
40 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
41 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000042};
Alexey Bataev7ff55242014-06-19 09:13:45 +000043
Alexey Bataevf29276e2014-06-18 04:14:57 +000044template <class T> struct MatchesAny {
Alexey Bataev23b69422014-06-18 07:08:49 +000045 explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {}
Alexey Bataevf29276e2014-06-18 04:14:57 +000046 bool operator()(T Kind) {
47 for (auto KindEl : Arr)
48 if (KindEl == Kind)
49 return true;
50 return false;
51 }
52
53private:
54 ArrayRef<T> Arr;
55};
Alexey Bataev23b69422014-06-18 07:08:49 +000056struct MatchesAlways {
Alexey Bataevf29276e2014-06-18 04:14:57 +000057 MatchesAlways() {}
Alexey Bataev7ff55242014-06-19 09:13:45 +000058 template <class T> bool operator()(T) { return true; }
Alexey Bataevf29276e2014-06-18 04:14:57 +000059};
60
61typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause;
62typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective;
Alexey Bataev758e55e2013-09-06 18:03:48 +000063
64/// \brief Stack for tracking declarations used in OpenMP directives and
65/// clauses and their data-sharing attributes.
66class DSAStackTy {
67public:
68 struct DSAVarData {
69 OpenMPDirectiveKind DKind;
70 OpenMPClauseKind CKind;
71 DeclRefExpr *RefExpr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000072 SourceLocation ImplicitDSALoc;
73 DSAVarData()
74 : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr),
75 ImplicitDSALoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000076 };
Alexey Bataeved09d242014-05-28 05:53:51 +000077
Alexey Bataev758e55e2013-09-06 18:03:48 +000078private:
79 struct DSAInfo {
80 OpenMPClauseKind Attributes;
81 DeclRefExpr *RefExpr;
82 };
83 typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000084 typedef llvm::SmallDenseMap<VarDecl *, DeclRefExpr *, 64> AlignedMapTy;
Alexey Bataev9c821032015-04-30 04:23:23 +000085 typedef llvm::DenseSet<VarDecl *> LoopControlVariablesSetTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000086
87 struct SharingMapTy {
88 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000089 AlignedMapTy AlignedMap;
Alexey Bataev9c821032015-04-30 04:23:23 +000090 LoopControlVariablesSetTy LCVSet;
Alexey Bataev758e55e2013-09-06 18:03:48 +000091 DefaultDataSharingAttributes DefaultAttr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000092 SourceLocation DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +000093 OpenMPDirectiveKind Directive;
94 DeclarationNameInfo DirectiveName;
95 Scope *CurScope;
Alexey Bataevbae9a792014-06-27 10:37:06 +000096 SourceLocation ConstructLoc;
Alexey Bataev9fb6e642014-07-22 06:45:04 +000097 bool OrderedRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +000098 bool NowaitRegion;
Alexey Bataev9c821032015-04-30 04:23:23 +000099 unsigned CollapseNumber;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000100 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +0000101 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000102 Scope *CurScope, SourceLocation Loc)
Alexey Bataev9c821032015-04-30 04:23:23 +0000103 : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000104 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000105 ConstructLoc(Loc), OrderedRegion(false), NowaitRegion(false),
106 CollapseNumber(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000107 SharingMapTy()
Alexey Bataev9c821032015-04-30 04:23:23 +0000108 : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000109 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000110 ConstructLoc(), OrderedRegion(false), NowaitRegion(false),
111 CollapseNumber(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000112 };
113
114 typedef SmallVector<SharingMapTy, 64> StackTy;
115
116 /// \brief Stack of used declaration and their data-sharing attributes.
117 StackTy Stack;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000118 /// \brief true, if check for DSA must be from parent directive, false, if
119 /// from current directive.
Alexey Bataevaac108a2015-06-23 04:51:00 +0000120 OpenMPClauseKind ClauseKindMode;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000121 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000122
123 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
124
125 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000126
127 /// \brief Checks if the variable is a local for OpenMP region.
128 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000129
Alexey Bataev758e55e2013-09-06 18:03:48 +0000130public:
Alexey Bataevaac108a2015-06-23 04:51:00 +0000131 explicit DSAStackTy(Sema &S)
132 : Stack(1), ClauseKindMode(OMPC_unknown), SemaRef(S) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000133
Alexey Bataevaac108a2015-06-23 04:51:00 +0000134 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
135 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000136
137 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000138 Scope *CurScope, SourceLocation Loc) {
139 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
140 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000141 }
142
143 void pop() {
144 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
145 Stack.pop_back();
146 }
147
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000148 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000149 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000150 /// for diagnostics.
151 DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE);
152
Alexey Bataev9c821032015-04-30 04:23:23 +0000153 /// \brief Register specified variable as loop control variable.
154 void addLoopControlVariable(VarDecl *D);
155 /// \brief Check if the specified variable is a loop control variable for
156 /// current region.
157 bool isLoopControlVariable(VarDecl *D);
158
Alexey Bataev758e55e2013-09-06 18:03:48 +0000159 /// \brief Adds explicit data sharing attribute to the specified declaration.
160 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
161
Alexey Bataev758e55e2013-09-06 18:03:48 +0000162 /// \brief Returns data sharing attributes from top of the stack for the
163 /// specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000164 DSAVarData getTopDSA(VarDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000165 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000166 DSAVarData getImplicitDSA(VarDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000167 /// \brief Checks if the specified variables has data-sharing attributes which
168 /// match specified \a CPred predicate in any directive which matches \a DPred
169 /// predicate.
170 template <class ClausesPredicate, class DirectivesPredicate>
171 DSAVarData hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000172 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000173 /// \brief Checks if the specified variables has data-sharing attributes which
174 /// match specified \a CPred predicate in any innermost directive which
175 /// matches \a DPred predicate.
176 template <class ClausesPredicate, class DirectivesPredicate>
177 DSAVarData hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000178 DirectivesPredicate DPred,
179 bool FromParent);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000180 /// \brief Checks if the specified variables has explicit data-sharing
181 /// attributes which match specified \a CPred predicate at the specified
182 /// OpenMP region.
183 bool hasExplicitDSA(VarDecl *D,
184 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
185 unsigned Level);
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000186 /// \brief Finds a directive which matches specified \a DPred predicate.
187 template <class NamedDirectivesPredicate>
188 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000189
Alexey Bataev758e55e2013-09-06 18:03:48 +0000190 /// \brief Returns currently analyzed directive.
191 OpenMPDirectiveKind getCurrentDirective() const {
192 return Stack.back().Directive;
193 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000194 /// \brief Returns parent directive.
195 OpenMPDirectiveKind getParentDirective() const {
196 if (Stack.size() > 2)
197 return Stack[Stack.size() - 2].Directive;
198 return OMPD_unknown;
199 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000200
201 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000202 void setDefaultDSANone(SourceLocation Loc) {
203 Stack.back().DefaultAttr = DSA_none;
204 Stack.back().DefaultAttrLoc = Loc;
205 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000206 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000207 void setDefaultDSAShared(SourceLocation Loc) {
208 Stack.back().DefaultAttr = DSA_shared;
209 Stack.back().DefaultAttrLoc = Loc;
210 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000211
212 DefaultDataSharingAttributes getDefaultDSA() const {
213 return Stack.back().DefaultAttr;
214 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000215 SourceLocation getDefaultDSALocation() const {
216 return Stack.back().DefaultAttrLoc;
217 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000218
Alexey Bataevf29276e2014-06-18 04:14:57 +0000219 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000220 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000221 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000222 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000223 }
224
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000225 /// \brief Marks current region as ordered (it has an 'ordered' clause).
226 void setOrderedRegion(bool IsOrdered = true) {
227 Stack.back().OrderedRegion = IsOrdered;
228 }
229 /// \brief Returns true, if parent region is ordered (has associated
230 /// 'ordered' clause), false - otherwise.
231 bool isParentOrderedRegion() const {
232 if (Stack.size() > 2)
233 return Stack[Stack.size() - 2].OrderedRegion;
234 return false;
235 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000236 /// \brief Marks current region as nowait (it has a 'nowait' clause).
237 void setNowaitRegion(bool IsNowait = true) {
238 Stack.back().NowaitRegion = IsNowait;
239 }
240 /// \brief Returns true, if parent region is nowait (has associated
241 /// 'nowait' clause), false - otherwise.
242 bool isParentNowaitRegion() const {
243 if (Stack.size() > 2)
244 return Stack[Stack.size() - 2].NowaitRegion;
245 return false;
246 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000247
Alexey Bataev9c821032015-04-30 04:23:23 +0000248 /// \brief Set collapse value for the region.
249 void setCollapseNumber(unsigned Val) { Stack.back().CollapseNumber = Val; }
250 /// \brief Return collapse value for region.
251 unsigned getCollapseNumber() const {
252 return Stack.back().CollapseNumber;
253 }
254
Alexey Bataev13314bf2014-10-09 04:18:56 +0000255 /// \brief Marks current target region as one with closely nested teams
256 /// region.
257 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
258 if (Stack.size() > 2)
259 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
260 }
261 /// \brief Returns true, if current region has closely nested teams region.
262 bool hasInnerTeamsRegion() const {
263 return getInnerTeamsRegionLoc().isValid();
264 }
265 /// \brief Returns location of the nested teams region (if any).
266 SourceLocation getInnerTeamsRegionLoc() const {
267 if (Stack.size() > 1)
268 return Stack.back().InnerTeamsRegionLoc;
269 return SourceLocation();
270 }
271
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000272 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000273 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000274 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000275};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000276bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
277 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
Alexey Bataev13314bf2014-10-09 04:18:56 +0000278 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000279}
Alexey Bataeved09d242014-05-28 05:53:51 +0000280} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000281
282DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
283 VarDecl *D) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000284 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000285 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000286 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000287 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
288 // in a region but not in construct]
289 // File-scope or namespace-scope variables referenced in called routines
290 // in the region are shared unless they appear in a threadprivate
291 // directive.
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000292 if (!D->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000293 DVar.CKind = OMPC_shared;
294
295 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
296 // in a region but not in construct]
297 // Variables with static storage duration that are declared in called
298 // routines in the region are shared.
299 if (D->hasGlobalStorage())
300 DVar.CKind = OMPC_shared;
301
Alexey Bataev758e55e2013-09-06 18:03:48 +0000302 return DVar;
303 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000304
Alexey Bataev758e55e2013-09-06 18:03:48 +0000305 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000306 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
307 // in a Construct, C/C++, predetermined, p.1]
308 // Variables with automatic storage duration that are declared in a scope
309 // inside the construct are private.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000310 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
311 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
312 DVar.CKind = OMPC_private;
313 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000314 }
315
Alexey Bataev758e55e2013-09-06 18:03:48 +0000316 // Explicitly specified attributes and local variables with predetermined
317 // attributes.
318 if (Iter->SharingMap.count(D)) {
319 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
320 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000321 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000322 return DVar;
323 }
324
325 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
326 // in a Construct, C/C++, implicitly determined, p.1]
327 // In a parallel or task construct, the data-sharing attributes of these
328 // variables are determined by the default clause, if present.
329 switch (Iter->DefaultAttr) {
330 case DSA_shared:
331 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000332 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000333 return DVar;
334 case DSA_none:
335 return DVar;
336 case DSA_unspecified:
337 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
338 // in a Construct, implicitly determined, p.2]
339 // In a parallel construct, if no default clause is present, these
340 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000341 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000342 if (isOpenMPParallelDirective(DVar.DKind) ||
343 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000344 DVar.CKind = OMPC_shared;
345 return DVar;
346 }
347
348 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
349 // in a Construct, implicitly determined, p.4]
350 // In a task construct, if no default clause is present, a variable that in
351 // the enclosing context is determined to be shared by all implicit tasks
352 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000353 if (DVar.DKind == OMPD_task) {
354 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000355 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000356 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000357 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
358 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000359 // in a Construct, implicitly determined, p.6]
360 // In a task construct, if no default clause is present, a variable
361 // whose data-sharing attribute is not determined by the rules above is
362 // firstprivate.
363 DVarTemp = getDSA(I, D);
364 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000365 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000366 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000367 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000368 return DVar;
369 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000370 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000371 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000372 }
373 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000374 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000375 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000376 return DVar;
377 }
378 }
379 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
380 // in a Construct, implicitly determined, p.3]
381 // For constructs other than task, if no default clause is present, these
382 // variables inherit their data-sharing attributes from the enclosing
383 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000384 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000385}
386
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000387DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
388 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000389 D = D->getCanonicalDecl();
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000390 auto It = Stack.back().AlignedMap.find(D);
391 if (It == Stack.back().AlignedMap.end()) {
392 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
393 Stack.back().AlignedMap[D] = NewDE;
394 return nullptr;
395 } else {
396 assert(It->second && "Unexpected nullptr expr in the aligned map");
397 return It->second;
398 }
399 return nullptr;
400}
401
Alexey Bataev9c821032015-04-30 04:23:23 +0000402void DSAStackTy::addLoopControlVariable(VarDecl *D) {
403 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
404 D = D->getCanonicalDecl();
405 Stack.back().LCVSet.insert(D);
406}
407
408bool DSAStackTy::isLoopControlVariable(VarDecl *D) {
409 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
410 D = D->getCanonicalDecl();
411 return Stack.back().LCVSet.count(D) > 0;
412}
413
Alexey Bataev758e55e2013-09-06 18:03:48 +0000414void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000415 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000416 if (A == OMPC_threadprivate) {
417 Stack[0].SharingMap[D].Attributes = A;
418 Stack[0].SharingMap[D].RefExpr = E;
419 } else {
420 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
421 Stack.back().SharingMap[D].Attributes = A;
422 Stack.back().SharingMap[D].RefExpr = E;
423 }
424}
425
Alexey Bataeved09d242014-05-28 05:53:51 +0000426bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000427 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000428 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000429 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000430 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000431 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000432 ++I;
433 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000434 if (I == E)
435 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000436 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000437 Scope *CurScope = getCurScope();
438 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000439 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000440 }
441 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000442 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000443 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000444}
445
Alexey Bataev39f915b82015-05-08 10:41:21 +0000446/// \brief Build a variable declaration for OpenMP loop iteration variable.
447static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
448 StringRef Name) {
449 DeclContext *DC = SemaRef.CurContext;
450 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
451 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
452 VarDecl *Decl =
453 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
454 Decl->setImplicit();
455 return Decl;
456}
457
458static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
459 SourceLocation Loc,
460 bool RefersToCapture = false) {
461 D->setReferenced();
462 D->markUsed(S.Context);
463 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
464 SourceLocation(), D, RefersToCapture, Loc, Ty,
465 VK_LValue);
466}
467
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000468DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000469 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000470 DSAVarData DVar;
471
472 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
473 // in a Construct, C/C++, predetermined, p.1]
474 // Variables appearing in threadprivate directives are threadprivate.
Alexey Bataev26a39242015-01-13 03:35:30 +0000475 if (D->getTLSKind() != VarDecl::TLS_None ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +0000476 (D->getStorageClass() == SC_Register && D->hasAttr<AsmLabelAttr>() &&
477 !D->isLocalVarDecl())) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000478 addDSA(D, buildDeclRefExpr(SemaRef, D, D->getType().getNonReferenceType(),
479 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000480 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000481 }
482 if (Stack[0].SharingMap.count(D)) {
483 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
484 DVar.CKind = OMPC_threadprivate;
485 return DVar;
486 }
487
488 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
489 // in a Construct, C/C++, predetermined, p.1]
490 // Variables with automatic storage duration that are declared in a scope
491 // inside the construct are private.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000492 OpenMPDirectiveKind Kind =
493 FromParent ? getParentDirective() : getCurrentDirective();
494 auto StartI = std::next(Stack.rbegin());
495 auto EndI = std::prev(Stack.rend());
496 if (FromParent && StartI != EndI) {
497 StartI = std::next(StartI);
498 }
499 if (!isParallelOrTaskRegion(Kind)) {
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000500 if (isOpenMPLocal(D, StartI) &&
501 ((D->isLocalVarDecl() && (D->getStorageClass() == SC_Auto ||
502 D->getStorageClass() == SC_None)) ||
503 isa<ParmVarDecl>(D))) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000504 DVar.CKind = OMPC_private;
505 return DVar;
Alexander Musman8dba6642014-04-22 13:09:42 +0000506 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000507
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000508 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
509 // in a Construct, C/C++, predetermined, p.4]
510 // Static data members are shared.
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000511 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
512 // in a Construct, C/C++, predetermined, p.7]
513 // Variables with static storage duration that are declared in a scope
514 // inside the construct are shared.
Alexey Bataev42971a32015-01-20 07:03:46 +0000515 if (D->isStaticDataMember() || D->isStaticLocal()) {
516 DSAVarData DVarTemp =
517 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
518 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
519 return DVar;
520
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000521 DVar.CKind = OMPC_shared;
522 return DVar;
523 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000524 }
525
526 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000527 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
528 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000529 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
530 // in a Construct, C/C++, predetermined, p.6]
531 // Variables with const qualified type having no mutable member are
532 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000533 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000534 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000535 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000536 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000537 // Variables with const-qualified type having no mutable member may be
538 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000539 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
540 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000541 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
542 return DVar;
543
Alexey Bataev758e55e2013-09-06 18:03:48 +0000544 DVar.CKind = OMPC_shared;
545 return DVar;
546 }
547
Alexey Bataev758e55e2013-09-06 18:03:48 +0000548 // Explicitly specified attributes and local variables with predetermined
549 // attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000550 auto I = std::prev(StartI);
551 if (I->SharingMap.count(D)) {
552 DVar.RefExpr = I->SharingMap[D].RefExpr;
553 DVar.CKind = I->SharingMap[D].Attributes;
554 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000555 }
556
557 return DVar;
558}
559
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000560DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000561 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000562 auto StartI = Stack.rbegin();
563 auto EndI = std::prev(Stack.rend());
564 if (FromParent && StartI != EndI) {
565 StartI = std::next(StartI);
566 }
567 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000568}
569
Alexey Bataevf29276e2014-06-18 04:14:57 +0000570template <class ClausesPredicate, class DirectivesPredicate>
571DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000572 DirectivesPredicate DPred,
573 bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000574 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000575 auto StartI = std::next(Stack.rbegin());
576 auto EndI = std::prev(Stack.rend());
577 if (FromParent && StartI != EndI) {
578 StartI = std::next(StartI);
579 }
580 for (auto I = StartI, EE = EndI; I != EE; ++I) {
581 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000582 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000583 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000584 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000585 return DVar;
586 }
587 return DSAVarData();
588}
589
Alexey Bataevf29276e2014-06-18 04:14:57 +0000590template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000591DSAStackTy::DSAVarData
592DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
593 DirectivesPredicate DPred, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000594 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000595 auto StartI = std::next(Stack.rbegin());
596 auto EndI = std::prev(Stack.rend());
597 if (FromParent && StartI != EndI) {
598 StartI = std::next(StartI);
599 }
600 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000601 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000602 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000603 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000604 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000605 return DVar;
606 return DSAVarData();
607 }
608 return DSAVarData();
609}
610
Alexey Bataevaac108a2015-06-23 04:51:00 +0000611bool DSAStackTy::hasExplicitDSA(
612 VarDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
613 unsigned Level) {
614 if (CPred(ClauseKindMode))
615 return true;
616 if (isClauseParsingMode())
617 ++Level;
618 D = D->getCanonicalDecl();
619 auto StartI = Stack.rbegin();
620 auto EndI = std::prev(Stack.rend());
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000621 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000622 return false;
623 std::advance(StartI, Level);
624 return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
625 CPred(StartI->SharingMap[D].Attributes);
626}
627
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000628template <class NamedDirectivesPredicate>
629bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
630 auto StartI = std::next(Stack.rbegin());
631 auto EndI = std::prev(Stack.rend());
632 if (FromParent && StartI != EndI) {
633 StartI = std::next(StartI);
634 }
635 for (auto I = StartI, EE = EndI; I != EE; ++I) {
636 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
637 return true;
638 }
639 return false;
640}
641
Alexey Bataev758e55e2013-09-06 18:03:48 +0000642void Sema::InitDataSharingAttributesStack() {
643 VarDataSharingAttributesStack = new DSAStackTy(*this);
644}
645
646#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
647
Alexey Bataevf841bd92014-12-16 07:00:22 +0000648bool Sema::IsOpenMPCapturedVar(VarDecl *VD) {
649 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000650 VD = VD->getCanonicalDecl();
Alexey Bataevf841bd92014-12-16 07:00:22 +0000651 if (DSAStack->getCurrentDirective() != OMPD_unknown) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000652 if (DSAStack->isLoopControlVariable(VD) ||
653 (VD->hasLocalStorage() &&
654 isParallelOrTaskRegion(DSAStack->getCurrentDirective())))
Alexey Bataev9c821032015-04-30 04:23:23 +0000655 return true;
Alexey Bataevaac108a2015-06-23 04:51:00 +0000656 auto DVarPrivate = DSAStack->getTopDSA(VD, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000657 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
658 return true;
659 DVarPrivate = DSAStack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(),
Alexey Bataevaac108a2015-06-23 04:51:00 +0000660 DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000661 return DVarPrivate.CKind != OMPC_unknown;
662 }
663 return false;
664}
665
Alexey Bataevaac108a2015-06-23 04:51:00 +0000666bool Sema::isOpenMPPrivateVar(VarDecl *VD, unsigned Level) {
667 assert(LangOpts.OpenMP && "OpenMP is not allowed");
668 return DSAStack->hasExplicitDSA(
669 VD, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
670}
671
Alexey Bataeved09d242014-05-28 05:53:51 +0000672void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000673
674void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
675 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000676 Scope *CurScope, SourceLocation Loc) {
677 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000678 PushExpressionEvaluationContext(PotentiallyEvaluated);
679}
680
Alexey Bataevaac108a2015-06-23 04:51:00 +0000681void Sema::StartOpenMPClause(OpenMPClauseKind K) {
682 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000683}
684
Alexey Bataevaac108a2015-06-23 04:51:00 +0000685void Sema::EndOpenMPClause() {
686 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000687}
688
Alexey Bataev758e55e2013-09-06 18:03:48 +0000689void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000690 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
691 // A variable of class type (or array thereof) that appears in a lastprivate
692 // clause requires an accessible, unambiguous default constructor for the
693 // class type, unless the list item is also specified in a firstprivate
694 // clause.
695 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000696 for (auto *C : D->clauses()) {
697 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
698 SmallVector<Expr *, 8> PrivateCopies;
699 for (auto *DE : Clause->varlists()) {
700 if (DE->isValueDependent() || DE->isTypeDependent()) {
701 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000702 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +0000703 }
704 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(DE)->getDecl());
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000705 QualType Type = VD->getType();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000706 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000707 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000708 // Generate helper private variable and initialize it with the
709 // default value. The address of the original variable is replaced
710 // by the address of the new private variable in CodeGen. This new
711 // variable is not added to IdResolver, so the code in the OpenMP
712 // region uses original variable for proper diagnostics.
Alexey Bataev39f915b82015-05-08 10:41:21 +0000713 auto *VDPrivate =
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000714 buildVarDecl(*this, DE->getExprLoc(), Type.getUnqualifiedType(),
715 VD->getName());
Alexey Bataev38e89532015-04-16 04:54:05 +0000716 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
717 if (VDPrivate->isInvalidDecl())
718 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000719 PrivateCopies.push_back(buildDeclRefExpr(
720 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +0000721 } else {
722 // The variable is also a firstprivate, so initialization sequence
723 // for private copy is generated already.
724 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000725 }
726 }
Alexey Bataev38e89532015-04-16 04:54:05 +0000727 // Set initializers to private copies if no errors were found.
728 if (PrivateCopies.size() == Clause->varlist_size()) {
729 Clause->setPrivateCopies(PrivateCopies);
730 }
Alexey Bataevf29276e2014-06-18 04:14:57 +0000731 }
732 }
733 }
734
Alexey Bataev758e55e2013-09-06 18:03:48 +0000735 DSAStack->pop();
736 DiscardCleanupsInEvaluationContext();
737 PopExpressionEvaluationContext();
738}
739
Alexander Musman3276a272015-03-21 10:12:56 +0000740static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
741 Expr *NumIterations, Sema &SemaRef,
742 Scope *S);
743
Alexey Bataeva769e072013-03-22 06:34:35 +0000744namespace {
745
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000746class VarDeclFilterCCC : public CorrectionCandidateCallback {
747private:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000748 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +0000749
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000750public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000751 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000752 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000753 NamedDecl *ND = Candidate.getCorrectionDecl();
754 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
755 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000756 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
757 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000758 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000759 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000760 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000761};
Alexey Bataeved09d242014-05-28 05:53:51 +0000762} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000763
764ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
765 CXXScopeSpec &ScopeSpec,
766 const DeclarationNameInfo &Id) {
767 LookupResult Lookup(*this, Id, LookupOrdinaryName);
768 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
769
770 if (Lookup.isAmbiguous())
771 return ExprError();
772
773 VarDecl *VD;
774 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000775 if (TypoCorrection Corrected = CorrectTypo(
776 Id, LookupOrdinaryName, CurScope, nullptr,
777 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000778 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000779 PDiag(Lookup.empty()
780 ? diag::err_undeclared_var_use_suggest
781 : diag::err_omp_expected_var_arg_suggest)
782 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000783 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000784 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000785 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
786 : diag::err_omp_expected_var_arg)
787 << Id.getName();
788 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000789 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000790 } else {
791 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000792 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000793 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
794 return ExprError();
795 }
796 }
797 Lookup.suppressDiagnostics();
798
799 // OpenMP [2.9.2, Syntax, C/C++]
800 // Variables must be file-scope, namespace-scope, or static block-scope.
801 if (!VD->hasGlobalStorage()) {
802 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000803 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
804 bool IsDecl =
805 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000806 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000807 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
808 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000809 return ExprError();
810 }
811
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000812 VarDecl *CanonicalVD = VD->getCanonicalDecl();
813 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000814 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
815 // A threadprivate directive for file-scope variables must appear outside
816 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000817 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
818 !getCurLexicalContext()->isTranslationUnit()) {
819 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000820 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
821 bool IsDecl =
822 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
823 Diag(VD->getLocation(),
824 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
825 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000826 return ExprError();
827 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000828 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
829 // A threadprivate directive for static class member variables must appear
830 // in the class definition, in the same scope in which the member
831 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000832 if (CanonicalVD->isStaticDataMember() &&
833 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
834 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000835 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
836 bool IsDecl =
837 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
838 Diag(VD->getLocation(),
839 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
840 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000841 return ExprError();
842 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000843 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
844 // A threadprivate directive for namespace-scope variables must appear
845 // outside any definition or declaration other than the namespace
846 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000847 if (CanonicalVD->getDeclContext()->isNamespace() &&
848 (!getCurLexicalContext()->isFileContext() ||
849 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
850 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000851 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
852 bool IsDecl =
853 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
854 Diag(VD->getLocation(),
855 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
856 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000857 return ExprError();
858 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000859 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
860 // A threadprivate directive for static block-scope variables must appear
861 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000862 if (CanonicalVD->isStaticLocal() && CurScope &&
863 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000864 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000865 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
866 bool IsDecl =
867 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
868 Diag(VD->getLocation(),
869 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
870 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000871 return ExprError();
872 }
873
874 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
875 // A threadprivate directive must lexically precede all references to any
876 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000877 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000878 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +0000879 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000880 return ExprError();
881 }
882
883 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +0000884 ExprResult DE = buildDeclRefExpr(*this, VD, ExprType, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000885 return DE;
886}
887
Alexey Bataeved09d242014-05-28 05:53:51 +0000888Sema::DeclGroupPtrTy
889Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
890 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000891 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000892 CurContext->addDecl(D);
893 return DeclGroupPtrTy::make(DeclGroupRef(D));
894 }
895 return DeclGroupPtrTy();
896}
897
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000898namespace {
899class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
900 Sema &SemaRef;
901
902public:
903 bool VisitDeclRefExpr(const DeclRefExpr *E) {
904 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
905 if (VD->hasLocalStorage()) {
906 SemaRef.Diag(E->getLocStart(),
907 diag::err_omp_local_var_in_threadprivate_init)
908 << E->getSourceRange();
909 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
910 << VD << VD->getSourceRange();
911 return true;
912 }
913 }
914 return false;
915 }
916 bool VisitStmt(const Stmt *S) {
917 for (auto Child : S->children()) {
918 if (Child && Visit(Child))
919 return true;
920 }
921 return false;
922 }
Alexey Bataev23b69422014-06-18 07:08:49 +0000923 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000924};
925} // namespace
926
Alexey Bataeved09d242014-05-28 05:53:51 +0000927OMPThreadPrivateDecl *
928Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000929 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +0000930 for (auto &RefExpr : VarList) {
931 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000932 VarDecl *VD = cast<VarDecl>(DE->getDecl());
933 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000934
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000935 QualType QType = VD->getType();
936 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
937 // It will be analyzed later.
938 Vars.push_back(DE);
939 continue;
940 }
941
Alexey Bataeva769e072013-03-22 06:34:35 +0000942 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
943 // A threadprivate variable must not have an incomplete type.
944 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000945 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000946 continue;
947 }
948
949 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
950 // A threadprivate variable must not have a reference type.
951 if (VD->getType()->isReferenceType()) {
952 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000953 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
954 bool IsDecl =
955 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
956 Diag(VD->getLocation(),
957 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
958 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000959 continue;
960 }
961
Richard Smithfd3834f2013-04-13 02:43:54 +0000962 // Check if this is a TLS variable.
Alexey Bataev26a39242015-01-13 03:35:30 +0000963 if (VD->getTLSKind() != VarDecl::TLS_None ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +0000964 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
965 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +0000966 Diag(ILoc, diag::err_omp_var_thread_local)
967 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +0000968 bool IsDecl =
969 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
970 Diag(VD->getLocation(),
971 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
972 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000973 continue;
974 }
975
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000976 // Check if initial value of threadprivate variable reference variable with
977 // local storage (it is not supported by runtime).
978 if (auto Init = VD->getAnyInitializer()) {
979 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000980 if (Checker.Visit(Init))
981 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000982 }
983
Alexey Bataeved09d242014-05-28 05:53:51 +0000984 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +0000985 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +0000986 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
987 Context, SourceRange(Loc, Loc)));
988 if (auto *ML = Context.getASTMutationListener())
989 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +0000990 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000991 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000992 if (!Vars.empty()) {
993 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
994 Vars);
995 D->setAccess(AS_public);
996 }
997 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +0000998}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000999
Alexey Bataev7ff55242014-06-19 09:13:45 +00001000static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
1001 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
1002 bool IsLoopIterVar = false) {
1003 if (DVar.RefExpr) {
1004 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1005 << getOpenMPClauseName(DVar.CKind);
1006 return;
1007 }
1008 enum {
1009 PDSA_StaticMemberShared,
1010 PDSA_StaticLocalVarShared,
1011 PDSA_LoopIterVarPrivate,
1012 PDSA_LoopIterVarLinear,
1013 PDSA_LoopIterVarLastprivate,
1014 PDSA_ConstVarShared,
1015 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001016 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001017 PDSA_LocalVarPrivate,
1018 PDSA_Implicit
1019 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001020 bool ReportHint = false;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001021 auto ReportLoc = VD->getLocation();
Alexey Bataev7ff55242014-06-19 09:13:45 +00001022 if (IsLoopIterVar) {
1023 if (DVar.CKind == OMPC_private)
1024 Reason = PDSA_LoopIterVarPrivate;
1025 else if (DVar.CKind == OMPC_lastprivate)
1026 Reason = PDSA_LoopIterVarLastprivate;
1027 else
1028 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001029 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1030 Reason = PDSA_TaskVarFirstprivate;
1031 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001032 } else if (VD->isStaticLocal())
1033 Reason = PDSA_StaticLocalVarShared;
1034 else if (VD->isStaticDataMember())
1035 Reason = PDSA_StaticMemberShared;
1036 else if (VD->isFileVarDecl())
1037 Reason = PDSA_GlobalVarShared;
1038 else if (VD->getType().isConstant(SemaRef.getASTContext()))
1039 Reason = PDSA_ConstVarShared;
Alexey Bataevbae9a792014-06-27 10:37:06 +00001040 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001041 ReportHint = true;
1042 Reason = PDSA_LocalVarPrivate;
1043 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001044 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001045 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001046 << Reason << ReportHint
1047 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1048 } else if (DVar.ImplicitDSALoc.isValid()) {
1049 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1050 << getOpenMPClauseName(DVar.CKind);
1051 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001052}
1053
Alexey Bataev758e55e2013-09-06 18:03:48 +00001054namespace {
1055class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1056 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001057 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001058 bool ErrorFound;
1059 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001060 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001061 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001062
Alexey Bataev758e55e2013-09-06 18:03:48 +00001063public:
1064 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001065 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001066 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001067 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1068 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001069
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001070 auto DVar = Stack->getTopDSA(VD, false);
1071 // Check if the variable has explicit DSA set and stop analysis if it so.
1072 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001073
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001074 auto ELoc = E->getExprLoc();
1075 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001076 // The default(none) clause requires that each variable that is referenced
1077 // in the construct, and does not have a predetermined data-sharing
1078 // attribute, must have its data-sharing attribute explicitly determined
1079 // by being listed in a data-sharing attribute clause.
1080 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001081 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001082 VarsWithInheritedDSA.count(VD) == 0) {
1083 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001084 return;
1085 }
1086
1087 // OpenMP [2.9.3.6, Restrictions, p.2]
1088 // A list item that appears in a reduction clause of the innermost
1089 // enclosing worksharing or parallel construct may not be accessed in an
1090 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001091 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001092 [](OpenMPDirectiveKind K) -> bool {
1093 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001094 isOpenMPWorksharingDirective(K) ||
1095 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001096 },
1097 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001098 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1099 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001100 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1101 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001102 return;
1103 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001104
1105 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001106 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001107 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001108 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001109 }
1110 }
1111 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001112 for (auto *C : S->clauses()) {
1113 // Skip analysis of arguments of implicitly defined firstprivate clause
1114 // for task directives.
1115 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1116 for (auto *CC : C->children()) {
1117 if (CC)
1118 Visit(CC);
1119 }
1120 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001121 }
1122 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001123 for (auto *C : S->children()) {
1124 if (C && !isa<OMPExecutableDirective>(C))
1125 Visit(C);
1126 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001127 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001128
1129 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001130 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001131 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
1132 return VarsWithInheritedDSA;
1133 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001134
Alexey Bataev7ff55242014-06-19 09:13:45 +00001135 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1136 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001137};
Alexey Bataeved09d242014-05-28 05:53:51 +00001138} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001139
Alexey Bataevbae9a792014-06-27 10:37:06 +00001140void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001141 switch (DKind) {
1142 case OMPD_parallel: {
1143 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1144 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001145 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001146 std::make_pair(".global_tid.", KmpInt32PtrTy),
1147 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1148 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001149 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001150 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1151 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001152 break;
1153 }
1154 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001155 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001156 std::make_pair(StringRef(), QualType()) // __context with shared vars
1157 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001158 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1159 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001160 break;
1161 }
1162 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001163 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001164 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001165 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001166 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1167 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001168 break;
1169 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001170 case OMPD_for_simd: {
1171 Sema::CapturedParamNameType Params[] = {
1172 std::make_pair(StringRef(), QualType()) // __context with shared vars
1173 };
1174 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1175 Params);
1176 break;
1177 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001178 case OMPD_sections: {
1179 Sema::CapturedParamNameType Params[] = {
1180 std::make_pair(StringRef(), QualType()) // __context with shared vars
1181 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001182 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1183 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001184 break;
1185 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001186 case OMPD_section: {
1187 Sema::CapturedParamNameType Params[] = {
1188 std::make_pair(StringRef(), QualType()) // __context with shared vars
1189 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001190 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1191 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001192 break;
1193 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001194 case OMPD_single: {
1195 Sema::CapturedParamNameType Params[] = {
1196 std::make_pair(StringRef(), QualType()) // __context with shared vars
1197 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001198 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1199 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001200 break;
1201 }
Alexander Musman80c22892014-07-17 08:54:58 +00001202 case OMPD_master: {
1203 Sema::CapturedParamNameType Params[] = {
1204 std::make_pair(StringRef(), QualType()) // __context with shared vars
1205 };
1206 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1207 Params);
1208 break;
1209 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001210 case OMPD_critical: {
1211 Sema::CapturedParamNameType Params[] = {
1212 std::make_pair(StringRef(), QualType()) // __context with shared vars
1213 };
1214 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1215 Params);
1216 break;
1217 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001218 case OMPD_parallel_for: {
1219 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1220 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1221 Sema::CapturedParamNameType Params[] = {
1222 std::make_pair(".global_tid.", KmpInt32PtrTy),
1223 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1224 std::make_pair(StringRef(), QualType()) // __context with shared vars
1225 };
1226 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1227 Params);
1228 break;
1229 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001230 case OMPD_parallel_for_simd: {
1231 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1232 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1233 Sema::CapturedParamNameType Params[] = {
1234 std::make_pair(".global_tid.", KmpInt32PtrTy),
1235 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1236 std::make_pair(StringRef(), QualType()) // __context with shared vars
1237 };
1238 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1239 Params);
1240 break;
1241 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001242 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001243 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1244 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001245 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001246 std::make_pair(".global_tid.", KmpInt32PtrTy),
1247 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001248 std::make_pair(StringRef(), QualType()) // __context with shared vars
1249 };
1250 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1251 Params);
1252 break;
1253 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001254 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001255 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001256 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1257 FunctionProtoType::ExtProtoInfo EPI;
1258 EPI.Variadic = true;
1259 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001260 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001261 std::make_pair(".global_tid.", KmpInt32Ty),
1262 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001263 std::make_pair(".privates.",
1264 Context.VoidPtrTy.withConst().withRestrict()),
1265 std::make_pair(
1266 ".copy_fn.",
1267 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001268 std::make_pair(StringRef(), QualType()) // __context with shared vars
1269 };
1270 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1271 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001272 // Mark this captured region as inlined, because we don't use outlined
1273 // function directly.
1274 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1275 AlwaysInlineAttr::CreateImplicit(
1276 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001277 break;
1278 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001279 case OMPD_ordered: {
1280 Sema::CapturedParamNameType Params[] = {
1281 std::make_pair(StringRef(), QualType()) // __context with shared vars
1282 };
1283 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1284 Params);
1285 break;
1286 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001287 case OMPD_atomic: {
1288 Sema::CapturedParamNameType Params[] = {
1289 std::make_pair(StringRef(), QualType()) // __context with shared vars
1290 };
1291 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1292 Params);
1293 break;
1294 }
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001295 case OMPD_target: {
1296 Sema::CapturedParamNameType Params[] = {
1297 std::make_pair(StringRef(), QualType()) // __context with shared vars
1298 };
1299 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1300 Params);
1301 break;
1302 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001303 case OMPD_teams: {
1304 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1305 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1306 Sema::CapturedParamNameType Params[] = {
1307 std::make_pair(".global_tid.", KmpInt32PtrTy),
1308 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1309 std::make_pair(StringRef(), QualType()) // __context with shared vars
1310 };
1311 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1312 Params);
1313 break;
1314 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001315 case OMPD_taskgroup: {
1316 Sema::CapturedParamNameType Params[] = {
1317 std::make_pair(StringRef(), QualType()) // __context with shared vars
1318 };
1319 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1320 Params);
1321 break;
1322 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001323 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001324 case OMPD_taskyield:
1325 case OMPD_barrier:
1326 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001327 case OMPD_cancellation_point:
Alexey Bataevee9af452014-11-21 11:33:46 +00001328 case OMPD_flush:
Alexey Bataev9959db52014-05-06 10:08:46 +00001329 llvm_unreachable("OpenMP Directive is not allowed");
1330 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001331 llvm_unreachable("Unknown OpenMP directive");
1332 }
1333}
1334
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001335StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1336 ArrayRef<OMPClause *> Clauses) {
1337 if (!S.isUsable()) {
1338 ActOnCapturedRegionError();
1339 return StmtError();
1340 }
Alexey Bataev040d5402015-05-12 08:35:28 +00001341 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001342 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001343 if (isOpenMPPrivate(Clause->getClauseKind()) ||
1344 Clause->getClauseKind() == OMPC_copyprivate) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001345 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001346 for (auto *VarRef : Clause->children()) {
1347 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001348 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001349 }
1350 }
Alexey Bataev040d5402015-05-12 08:35:28 +00001351 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
1352 Clause->getClauseKind() == OMPC_schedule) {
1353 // Mark all variables in private list clauses as used in inner region.
1354 // Required for proper codegen of combined directives.
1355 // TODO: add processing for other clauses.
1356 if (auto *E = cast_or_null<Expr>(
1357 cast<OMPScheduleClause>(Clause)->getHelperChunkSize())) {
1358 MarkDeclarationsReferencedInExpr(E);
1359 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001360 }
1361 }
1362 return ActOnCapturedRegionEnd(S.get());
1363}
1364
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001365static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1366 OpenMPDirectiveKind CurrentRegion,
1367 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001368 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001369 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001370 // Allowed nesting of constructs
1371 // +------------------+-----------------+------------------------------------+
1372 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1373 // +------------------+-----------------+------------------------------------+
1374 // | parallel | parallel | * |
1375 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001376 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001377 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001378 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001379 // | parallel | simd | * |
1380 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001381 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001382 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001383 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001384 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001385 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001386 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001387 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001388 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001389 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001390 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001391 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001392 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001393 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001394 // | parallel | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001395 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001396 // | parallel | cancellation | |
1397 // | | point | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001398 // +------------------+-----------------+------------------------------------+
1399 // | for | parallel | * |
1400 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001401 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001402 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001403 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001404 // | for | simd | * |
1405 // | for | sections | + |
1406 // | for | section | + |
1407 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001408 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001409 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001410 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001411 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001412 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001413 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001414 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001415 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001416 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001417 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001418 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001419 // | for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001420 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001421 // | for | cancellation | |
1422 // | | point | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001423 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001424 // | master | parallel | * |
1425 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001426 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001427 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001428 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001429 // | master | simd | * |
1430 // | master | sections | + |
1431 // | master | section | + |
1432 // | master | single | + |
1433 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001434 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001435 // | master |parallel sections| * |
1436 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001437 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001438 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001439 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001440 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001441 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001442 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001443 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001444 // | master | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001445 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001446 // | master | cancellation | |
1447 // | | point | |
Alexander Musman80c22892014-07-17 08:54:58 +00001448 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001449 // | critical | parallel | * |
1450 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001451 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001452 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001453 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001454 // | critical | simd | * |
1455 // | critical | sections | + |
1456 // | critical | section | + |
1457 // | critical | single | + |
1458 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001459 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001460 // | critical |parallel sections| * |
1461 // | critical | task | * |
1462 // | critical | taskyield | * |
1463 // | critical | barrier | + |
1464 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001465 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001466 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001467 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001468 // | critical | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001469 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001470 // | critical | cancellation | |
1471 // | | point | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001472 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001473 // | simd | parallel | |
1474 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001475 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001476 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001477 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001478 // | simd | simd | |
1479 // | simd | sections | |
1480 // | simd | section | |
1481 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001482 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001483 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001484 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001485 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001486 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001487 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001488 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001489 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001490 // | simd | flush | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001491 // | simd | ordered | |
Alexey Bataev0162e452014-07-22 10:10:35 +00001492 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001493 // | simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001494 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001495 // | simd | cancellation | |
1496 // | | point | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001497 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001498 // | for simd | parallel | |
1499 // | for simd | for | |
1500 // | for simd | for simd | |
1501 // | for simd | master | |
1502 // | for simd | critical | |
1503 // | for simd | simd | |
1504 // | for simd | sections | |
1505 // | for simd | section | |
1506 // | for simd | single | |
1507 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001508 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001509 // | for simd |parallel sections| |
1510 // | for simd | task | |
1511 // | for simd | taskyield | |
1512 // | for simd | barrier | |
1513 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001514 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001515 // | for simd | flush | |
1516 // | for simd | ordered | |
1517 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001518 // | for simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001519 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001520 // | for simd | cancellation | |
1521 // | | point | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001522 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001523 // | parallel for simd| parallel | |
1524 // | parallel for simd| for | |
1525 // | parallel for simd| for simd | |
1526 // | parallel for simd| master | |
1527 // | parallel for simd| critical | |
1528 // | parallel for simd| simd | |
1529 // | parallel for simd| sections | |
1530 // | parallel for simd| section | |
1531 // | parallel for simd| single | |
1532 // | parallel for simd| parallel for | |
1533 // | parallel for simd|parallel for simd| |
1534 // | parallel for simd|parallel sections| |
1535 // | parallel for simd| task | |
1536 // | parallel for simd| taskyield | |
1537 // | parallel for simd| barrier | |
1538 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001539 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001540 // | parallel for simd| flush | |
1541 // | parallel for simd| ordered | |
1542 // | parallel for simd| atomic | |
1543 // | parallel for simd| target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001544 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001545 // | parallel for simd| cancellation | |
1546 // | | point | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001547 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001548 // | sections | parallel | * |
1549 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001550 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001551 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001552 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001553 // | sections | simd | * |
1554 // | sections | sections | + |
1555 // | sections | section | * |
1556 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001557 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001558 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001559 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001560 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001561 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001562 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001563 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001564 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001565 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001566 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001567 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001568 // | sections | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001569 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001570 // | sections | cancellation | |
1571 // | | point | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001572 // +------------------+-----------------+------------------------------------+
1573 // | section | parallel | * |
1574 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001575 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001576 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001577 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001578 // | section | simd | * |
1579 // | section | sections | + |
1580 // | section | section | + |
1581 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001582 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001583 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001584 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001585 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001586 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001587 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001588 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001589 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001590 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001591 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001592 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001593 // | section | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001594 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001595 // | section | cancellation | |
1596 // | | point | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001597 // +------------------+-----------------+------------------------------------+
1598 // | single | parallel | * |
1599 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001600 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001601 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001602 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001603 // | single | simd | * |
1604 // | single | sections | + |
1605 // | single | section | + |
1606 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001607 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001608 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001609 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001610 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001611 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001612 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001613 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001614 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001615 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001616 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001617 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001618 // | single | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001619 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001620 // | single | cancellation | |
1621 // | | point | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001622 // +------------------+-----------------+------------------------------------+
1623 // | parallel for | parallel | * |
1624 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001625 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001626 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001627 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001628 // | parallel for | simd | * |
1629 // | parallel for | sections | + |
1630 // | parallel for | section | + |
1631 // | parallel for | single | + |
1632 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001633 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001634 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001635 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001636 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001637 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001638 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001639 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001640 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001641 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001642 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001643 // | parallel for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001644 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001645 // | parallel for | cancellation | |
1646 // | | point | ! |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001647 // +------------------+-----------------+------------------------------------+
1648 // | parallel sections| parallel | * |
1649 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001650 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001651 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001652 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001653 // | parallel sections| simd | * |
1654 // | parallel sections| sections | + |
1655 // | parallel sections| section | * |
1656 // | parallel sections| single | + |
1657 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001658 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001659 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001660 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001661 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001662 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001663 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001664 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001665 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001666 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001667 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001668 // | parallel sections| target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001669 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001670 // | parallel sections| cancellation | |
1671 // | | point | ! |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001672 // +------------------+-----------------+------------------------------------+
1673 // | task | parallel | * |
1674 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001675 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001676 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001677 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001678 // | task | simd | * |
1679 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001680 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001681 // | task | single | + |
1682 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001683 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001684 // | task |parallel sections| * |
1685 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001686 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001687 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001688 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001689 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001690 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001691 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001692 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001693 // | task | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001694 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001695 // | task | cancellation | |
1696 // | | point | ! |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001697 // +------------------+-----------------+------------------------------------+
1698 // | ordered | parallel | * |
1699 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001700 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001701 // | ordered | master | * |
1702 // | ordered | critical | * |
1703 // | ordered | simd | * |
1704 // | ordered | sections | + |
1705 // | ordered | section | + |
1706 // | ordered | single | + |
1707 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001708 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001709 // | ordered |parallel sections| * |
1710 // | ordered | task | * |
1711 // | ordered | taskyield | * |
1712 // | ordered | barrier | + |
1713 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001714 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001715 // | ordered | flush | * |
1716 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001717 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001718 // | ordered | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001719 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001720 // | ordered | cancellation | |
1721 // | | point | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001722 // +------------------+-----------------+------------------------------------+
1723 // | atomic | parallel | |
1724 // | atomic | for | |
1725 // | atomic | for simd | |
1726 // | atomic | master | |
1727 // | atomic | critical | |
1728 // | atomic | simd | |
1729 // | atomic | sections | |
1730 // | atomic | section | |
1731 // | atomic | single | |
1732 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001733 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001734 // | atomic |parallel sections| |
1735 // | atomic | task | |
1736 // | atomic | taskyield | |
1737 // | atomic | barrier | |
1738 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001739 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001740 // | atomic | flush | |
1741 // | atomic | ordered | |
1742 // | atomic | atomic | |
1743 // | atomic | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001744 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001745 // | atomic | cancellation | |
1746 // | | point | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001747 // +------------------+-----------------+------------------------------------+
1748 // | target | parallel | * |
1749 // | target | for | * |
1750 // | target | for simd | * |
1751 // | target | master | * |
1752 // | target | critical | * |
1753 // | target | simd | * |
1754 // | target | sections | * |
1755 // | target | section | * |
1756 // | target | single | * |
1757 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001758 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001759 // | target |parallel sections| * |
1760 // | target | task | * |
1761 // | target | taskyield | * |
1762 // | target | barrier | * |
1763 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001764 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001765 // | target | flush | * |
1766 // | target | ordered | * |
1767 // | target | atomic | * |
1768 // | target | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001769 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001770 // | target | cancellation | |
1771 // | | point | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001772 // +------------------+-----------------+------------------------------------+
1773 // | teams | parallel | * |
1774 // | teams | for | + |
1775 // | teams | for simd | + |
1776 // | teams | master | + |
1777 // | teams | critical | + |
1778 // | teams | simd | + |
1779 // | teams | sections | + |
1780 // | teams | section | + |
1781 // | teams | single | + |
1782 // | teams | parallel for | * |
1783 // | teams |parallel for simd| * |
1784 // | teams |parallel sections| * |
1785 // | teams | task | + |
1786 // | teams | taskyield | + |
1787 // | teams | barrier | + |
1788 // | teams | taskwait | + |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001789 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001790 // | teams | flush | + |
1791 // | teams | ordered | + |
1792 // | teams | atomic | + |
1793 // | teams | target | + |
1794 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001795 // | teams | cancellation | |
1796 // | | point | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001797 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00001798 if (Stack->getCurScope()) {
1799 auto ParentRegion = Stack->getParentDirective();
1800 bool NestingProhibited = false;
1801 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001802 enum {
1803 NoRecommend,
1804 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00001805 ShouldBeInOrderedRegion,
1806 ShouldBeInTargetRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001807 } Recommend = NoRecommend;
Alexey Bataev549210e2014-06-24 04:39:47 +00001808 if (isOpenMPSimdDirective(ParentRegion)) {
1809 // OpenMP [2.16, Nesting of Regions]
1810 // OpenMP constructs may not be nested inside a simd region.
1811 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1812 return true;
1813 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001814 if (ParentRegion == OMPD_atomic) {
1815 // OpenMP [2.16, Nesting of Regions]
1816 // OpenMP constructs may not be nested inside an atomic region.
1817 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
1818 return true;
1819 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001820 if (CurrentRegion == OMPD_section) {
1821 // OpenMP [2.7.2, sections Construct, Restrictions]
1822 // Orphaned section directives are prohibited. That is, the section
1823 // directives must appear within the sections construct and must not be
1824 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001825 if (ParentRegion != OMPD_sections &&
1826 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001827 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1828 << (ParentRegion != OMPD_unknown)
1829 << getOpenMPDirectiveName(ParentRegion);
1830 return true;
1831 }
1832 return false;
1833 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001834 // Allow some constructs to be orphaned (they could be used in functions,
1835 // called from OpenMP regions with the required preconditions).
1836 if (ParentRegion == OMPD_unknown)
1837 return false;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001838 if (CurrentRegion == OMPD_cancellation_point) {
1839 // OpenMP [2.16, Nesting of Regions]
1840 // A cancellation point construct for which construct-type-clause is
1841 // taskgroup must be nested inside a task construct. A cancellation
1842 // point construct for which construct-type-clause is not taskgroup must
1843 // be closely nested inside an OpenMP construct that matches the type
1844 // specified in construct-type-clause.
1845 NestingProhibited =
1846 !((CancelRegion == OMPD_parallel && ParentRegion == OMPD_parallel) ||
1847 (CancelRegion == OMPD_for && ParentRegion == OMPD_for) ||
1848 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
1849 (CancelRegion == OMPD_sections &&
1850 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections)));
1851 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00001852 // OpenMP [2.16, Nesting of Regions]
1853 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001854 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00001855 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
1856 ParentRegion == OMPD_task;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001857 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
1858 // OpenMP [2.16, Nesting of Regions]
1859 // A critical region may not be nested (closely or otherwise) inside a
1860 // critical region with the same name. Note that this restriction is not
1861 // sufficient to prevent deadlock.
1862 SourceLocation PreviousCriticalLoc;
1863 bool DeadLock =
1864 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
1865 OpenMPDirectiveKind K,
1866 const DeclarationNameInfo &DNI,
1867 SourceLocation Loc)
1868 ->bool {
1869 if (K == OMPD_critical &&
1870 DNI.getName() == CurrentName.getName()) {
1871 PreviousCriticalLoc = Loc;
1872 return true;
1873 } else
1874 return false;
1875 },
1876 false /* skip top directive */);
1877 if (DeadLock) {
1878 SemaRef.Diag(StartLoc,
1879 diag::err_omp_prohibited_region_critical_same_name)
1880 << CurrentName.getName();
1881 if (PreviousCriticalLoc.isValid())
1882 SemaRef.Diag(PreviousCriticalLoc,
1883 diag::note_omp_previous_critical_region);
1884 return true;
1885 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001886 } else if (CurrentRegion == OMPD_barrier) {
1887 // OpenMP [2.16, Nesting of Regions]
1888 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001889 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001890 NestingProhibited =
1891 isOpenMPWorksharingDirective(ParentRegion) ||
1892 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1893 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00001894 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00001895 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00001896 // OpenMP [2.16, Nesting of Regions]
1897 // A worksharing region may not be closely nested inside a worksharing,
1898 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001899 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00001900 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001901 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1902 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
1903 Recommend = ShouldBeInParallelRegion;
1904 } else if (CurrentRegion == OMPD_ordered) {
1905 // OpenMP [2.16, Nesting of Regions]
1906 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00001907 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001908 // An ordered region must be closely nested inside a loop region (or
1909 // parallel loop region) with an ordered clause.
1910 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00001911 ParentRegion == OMPD_task ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001912 !Stack->isParentOrderedRegion();
1913 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00001914 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
1915 // OpenMP [2.16, Nesting of Regions]
1916 // If specified, a teams construct must be contained within a target
1917 // construct.
1918 NestingProhibited = ParentRegion != OMPD_target;
1919 Recommend = ShouldBeInTargetRegion;
1920 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
1921 }
1922 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
1923 // OpenMP [2.16, Nesting of Regions]
1924 // distribute, parallel, parallel sections, parallel workshare, and the
1925 // parallel loop and parallel loop SIMD constructs are the only OpenMP
1926 // constructs that can be closely nested in the teams region.
1927 // TODO: add distribute directive.
1928 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion);
1929 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00001930 }
1931 if (NestingProhibited) {
1932 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001933 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
1934 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00001935 return true;
1936 }
1937 }
1938 return false;
1939}
1940
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001941StmtResult Sema::ActOnOpenMPExecutableDirective(
1942 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
1943 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
1944 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001945 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001946 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
1947 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00001948 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001949
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001950 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00001951 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001952 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001953 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00001954 if (AStmt) {
1955 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
1956
1957 // Check default data sharing attributes for referenced variables.
1958 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
1959 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
1960 if (DSAChecker.isErrorFound())
1961 return StmtError();
1962 // Generate list of implicitly defined firstprivate variables.
1963 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00001964
1965 if (!DSAChecker.getImplicitFirstprivate().empty()) {
1966 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
1967 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
1968 SourceLocation(), SourceLocation())) {
1969 ClausesWithImplicit.push_back(Implicit);
1970 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
1971 DSAChecker.getImplicitFirstprivate().size();
1972 } else
1973 ErrorFound = true;
1974 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001975 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001976
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001977 switch (Kind) {
1978 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00001979 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
1980 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001981 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001982 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001983 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1984 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001985 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00001986 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001987 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1988 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001989 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00001990 case OMPD_for_simd:
1991 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
1992 EndLoc, VarsWithInheritedDSA);
1993 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001994 case OMPD_sections:
1995 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
1996 EndLoc);
1997 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001998 case OMPD_section:
1999 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002000 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002001 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2002 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002003 case OMPD_single:
2004 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2005 EndLoc);
2006 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002007 case OMPD_master:
2008 assert(ClausesWithImplicit.empty() &&
2009 "No clauses are allowed for 'omp master' directive");
2010 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2011 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002012 case OMPD_critical:
2013 assert(ClausesWithImplicit.empty() &&
2014 "No clauses are allowed for 'omp critical' directive");
2015 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
2016 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002017 case OMPD_parallel_for:
2018 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2019 EndLoc, VarsWithInheritedDSA);
2020 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002021 case OMPD_parallel_for_simd:
2022 Res = ActOnOpenMPParallelForSimdDirective(
2023 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2024 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002025 case OMPD_parallel_sections:
2026 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2027 StartLoc, EndLoc);
2028 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002029 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002030 Res =
2031 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2032 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002033 case OMPD_taskyield:
2034 assert(ClausesWithImplicit.empty() &&
2035 "No clauses are allowed for 'omp taskyield' directive");
2036 assert(AStmt == nullptr &&
2037 "No associated statement allowed for 'omp taskyield' directive");
2038 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2039 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002040 case OMPD_barrier:
2041 assert(ClausesWithImplicit.empty() &&
2042 "No clauses are allowed for 'omp barrier' directive");
2043 assert(AStmt == nullptr &&
2044 "No associated statement allowed for 'omp barrier' directive");
2045 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2046 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00002047 case OMPD_taskwait:
2048 assert(ClausesWithImplicit.empty() &&
2049 "No clauses are allowed for 'omp taskwait' directive");
2050 assert(AStmt == nullptr &&
2051 "No associated statement allowed for 'omp taskwait' directive");
2052 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2053 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002054 case OMPD_taskgroup:
2055 assert(ClausesWithImplicit.empty() &&
2056 "No clauses are allowed for 'omp taskgroup' directive");
2057 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
2058 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00002059 case OMPD_flush:
2060 assert(AStmt == nullptr &&
2061 "No associated statement allowed for 'omp flush' directive");
2062 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2063 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002064 case OMPD_ordered:
2065 assert(ClausesWithImplicit.empty() &&
2066 "No clauses are allowed for 'omp ordered' directive");
2067 Res = ActOnOpenMPOrderedDirective(AStmt, StartLoc, EndLoc);
2068 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00002069 case OMPD_atomic:
2070 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2071 EndLoc);
2072 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002073 case OMPD_teams:
2074 Res =
2075 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2076 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002077 case OMPD_target:
2078 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2079 EndLoc);
2080 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002081 case OMPD_cancellation_point:
2082 assert(ClausesWithImplicit.empty() &&
2083 "No clauses are allowed for 'omp cancellation point' directive");
2084 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
2085 "cancellation point' directive");
2086 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2087 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002088 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002089 llvm_unreachable("OpenMP Directive is not allowed");
2090 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002091 llvm_unreachable("Unknown OpenMP directive");
2092 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002093
Alexey Bataev4acb8592014-07-07 13:01:15 +00002094 for (auto P : VarsWithInheritedDSA) {
2095 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
2096 << P.first << P.second->getSourceRange();
2097 }
2098 if (!VarsWithInheritedDSA.empty())
2099 return StmtError();
2100
Alexey Bataeved09d242014-05-28 05:53:51 +00002101 if (ErrorFound)
2102 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002103 return Res;
2104}
2105
2106StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
2107 Stmt *AStmt,
2108 SourceLocation StartLoc,
2109 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00002110 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2111 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2112 // 1.2.2 OpenMP Language Terminology
2113 // Structured block - An executable statement with a single entry at the
2114 // top and a single exit at the bottom.
2115 // The point of exit cannot be a branch out of the structured block.
2116 // longjmp() and throw() must not violate the entry/exit criteria.
2117 CS->getCapturedDecl()->setNothrow();
2118
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002119 getCurFunction()->setHasBranchProtectedScope();
2120
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00002121 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
2122 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002123}
2124
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002125namespace {
2126/// \brief Helper class for checking canonical form of the OpenMP loops and
2127/// extracting iteration space of each loop in the loop nest, that will be used
2128/// for IR generation.
2129class OpenMPIterationSpaceChecker {
2130 /// \brief Reference to Sema.
2131 Sema &SemaRef;
2132 /// \brief A location for diagnostics (when there is no some better location).
2133 SourceLocation DefaultLoc;
2134 /// \brief A location for diagnostics (when increment is not compatible).
2135 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002136 /// \brief A source location for referring to loop init later.
2137 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002138 /// \brief A source location for referring to condition later.
2139 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002140 /// \brief A source location for referring to increment later.
2141 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002142 /// \brief Loop variable.
2143 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002144 /// \brief Reference to loop variable.
2145 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002146 /// \brief Lower bound (initializer for the var).
2147 Expr *LB;
2148 /// \brief Upper bound.
2149 Expr *UB;
2150 /// \brief Loop step (increment).
2151 Expr *Step;
2152 /// \brief This flag is true when condition is one of:
2153 /// Var < UB
2154 /// Var <= UB
2155 /// UB > Var
2156 /// UB >= Var
2157 bool TestIsLessOp;
2158 /// \brief This flag is true when condition is strict ( < or > ).
2159 bool TestIsStrictOp;
2160 /// \brief This flag is true when step is subtracted on each iteration.
2161 bool SubtractStep;
2162
2163public:
2164 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
2165 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00002166 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
2167 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002168 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
2169 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002170 /// \brief Check init-expr for canonical loop form and save loop counter
2171 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00002172 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002173 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
2174 /// for less/greater and for strict/non-strict comparison.
2175 bool CheckCond(Expr *S);
2176 /// \brief Check incr-expr for canonical loop form and return true if it
2177 /// does not conform, otherwise save loop step (#Step).
2178 bool CheckInc(Expr *S);
2179 /// \brief Return the loop counter variable.
2180 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002181 /// \brief Return the reference expression to loop counter variable.
2182 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002183 /// \brief Source range of the loop init.
2184 SourceRange GetInitSrcRange() const { return InitSrcRange; }
2185 /// \brief Source range of the loop condition.
2186 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
2187 /// \brief Source range of the loop increment.
2188 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
2189 /// \brief True if the step should be subtracted.
2190 bool ShouldSubtractStep() const { return SubtractStep; }
2191 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002192 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00002193 /// \brief Build the precondition expression for the loops.
2194 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002195 /// \brief Build reference expression to the counter be used for codegen.
2196 Expr *BuildCounterVar() const;
2197 /// \brief Build initization of the counter be used for codegen.
2198 Expr *BuildCounterInit() const;
2199 /// \brief Build step of the counter be used for codegen.
2200 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002201 /// \brief Return true if any expression is dependent.
2202 bool Dependent() const;
2203
2204private:
2205 /// \brief Check the right-hand side of an assignment in the increment
2206 /// expression.
2207 bool CheckIncRHS(Expr *RHS);
2208 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002209 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002210 /// \brief Helper to set upper bound.
2211 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
2212 const SourceLocation &SL);
2213 /// \brief Helper to set loop increment.
2214 bool SetStep(Expr *NewStep, bool Subtract);
2215};
2216
2217bool OpenMPIterationSpaceChecker::Dependent() const {
2218 if (!Var) {
2219 assert(!LB && !UB && !Step);
2220 return false;
2221 }
2222 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
2223 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
2224}
2225
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002226bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
2227 DeclRefExpr *NewVarRefExpr,
2228 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002229 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002230 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
2231 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002232 if (!NewVar || !NewLB)
2233 return true;
2234 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002235 VarRef = NewVarRefExpr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002236 LB = NewLB;
2237 return false;
2238}
2239
2240bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
2241 const SourceRange &SR,
2242 const SourceLocation &SL) {
2243 // State consistency checking to ensure correct usage.
2244 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
2245 !TestIsLessOp && !TestIsStrictOp);
2246 if (!NewUB)
2247 return true;
2248 UB = NewUB;
2249 TestIsLessOp = LessOp;
2250 TestIsStrictOp = StrictOp;
2251 ConditionSrcRange = SR;
2252 ConditionLoc = SL;
2253 return false;
2254}
2255
2256bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
2257 // State consistency checking to ensure correct usage.
2258 assert(Var != nullptr && LB != nullptr && Step == nullptr);
2259 if (!NewStep)
2260 return true;
2261 if (!NewStep->isValueDependent()) {
2262 // Check that the step is integer expression.
2263 SourceLocation StepLoc = NewStep->getLocStart();
2264 ExprResult Val =
2265 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
2266 if (Val.isInvalid())
2267 return true;
2268 NewStep = Val.get();
2269
2270 // OpenMP [2.6, Canonical Loop Form, Restrictions]
2271 // If test-expr is of form var relational-op b and relational-op is < or
2272 // <= then incr-expr must cause var to increase on each iteration of the
2273 // loop. If test-expr is of form var relational-op b and relational-op is
2274 // > or >= then incr-expr must cause var to decrease on each iteration of
2275 // the loop.
2276 // If test-expr is of form b relational-op var and relational-op is < or
2277 // <= then incr-expr must cause var to decrease on each iteration of the
2278 // loop. If test-expr is of form b relational-op var and relational-op is
2279 // > or >= then incr-expr must cause var to increase on each iteration of
2280 // the loop.
2281 llvm::APSInt Result;
2282 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
2283 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
2284 bool IsConstNeg =
2285 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002286 bool IsConstPos =
2287 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002288 bool IsConstZero = IsConstant && !Result.getBoolValue();
2289 if (UB && (IsConstZero ||
2290 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00002291 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002292 SemaRef.Diag(NewStep->getExprLoc(),
2293 diag::err_omp_loop_incr_not_compatible)
2294 << Var << TestIsLessOp << NewStep->getSourceRange();
2295 SemaRef.Diag(ConditionLoc,
2296 diag::note_omp_loop_cond_requres_compatible_incr)
2297 << TestIsLessOp << ConditionSrcRange;
2298 return true;
2299 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002300 if (TestIsLessOp == Subtract) {
2301 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
2302 NewStep).get();
2303 Subtract = !Subtract;
2304 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002305 }
2306
2307 Step = NewStep;
2308 SubtractStep = Subtract;
2309 return false;
2310}
2311
Alexey Bataev9c821032015-04-30 04:23:23 +00002312bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002313 // Check init-expr for canonical loop form and save loop counter
2314 // variable - #Var and its initialization value - #LB.
2315 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
2316 // var = lb
2317 // integer-type var = lb
2318 // random-access-iterator-type var = lb
2319 // pointer-type var = lb
2320 //
2321 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00002322 if (EmitDiags) {
2323 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
2324 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002325 return true;
2326 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002327 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002328 if (Expr *E = dyn_cast<Expr>(S))
2329 S = E->IgnoreParens();
2330 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2331 if (BO->getOpcode() == BO_Assign)
2332 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002333 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002334 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002335 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
2336 if (DS->isSingleDecl()) {
2337 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
2338 if (Var->hasInit()) {
2339 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00002340 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002341 SemaRef.Diag(S->getLocStart(),
2342 diag::ext_omp_loop_not_canonical_init)
2343 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002344 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002345 }
2346 }
2347 }
2348 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
2349 if (CE->getOperator() == OO_Equal)
2350 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002351 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2352 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002353
Alexey Bataev9c821032015-04-30 04:23:23 +00002354 if (EmitDiags) {
2355 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
2356 << S->getSourceRange();
2357 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002358 return true;
2359}
2360
Alexey Bataev23b69422014-06-18 07:08:49 +00002361/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002362/// variable (which may be the loop variable) if possible.
2363static const VarDecl *GetInitVarDecl(const Expr *E) {
2364 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00002365 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002366 E = E->IgnoreParenImpCasts();
2367 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
2368 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
2369 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
2370 CE->getArg(0) != nullptr)
2371 E = CE->getArg(0)->IgnoreParenImpCasts();
2372 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
2373 if (!DRE)
2374 return nullptr;
2375 return dyn_cast<VarDecl>(DRE->getDecl());
2376}
2377
2378bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
2379 // Check test-expr for canonical form, save upper-bound UB, flags for
2380 // less/greater and for strict/non-strict comparison.
2381 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2382 // var relational-op b
2383 // b relational-op var
2384 //
2385 if (!S) {
2386 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
2387 return true;
2388 }
2389 S = S->IgnoreParenImpCasts();
2390 SourceLocation CondLoc = S->getLocStart();
2391 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2392 if (BO->isRelationalOp()) {
2393 if (GetInitVarDecl(BO->getLHS()) == Var)
2394 return SetUB(BO->getRHS(),
2395 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
2396 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2397 BO->getSourceRange(), BO->getOperatorLoc());
2398 if (GetInitVarDecl(BO->getRHS()) == Var)
2399 return SetUB(BO->getLHS(),
2400 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
2401 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2402 BO->getSourceRange(), BO->getOperatorLoc());
2403 }
2404 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2405 if (CE->getNumArgs() == 2) {
2406 auto Op = CE->getOperator();
2407 switch (Op) {
2408 case OO_Greater:
2409 case OO_GreaterEqual:
2410 case OO_Less:
2411 case OO_LessEqual:
2412 if (GetInitVarDecl(CE->getArg(0)) == Var)
2413 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
2414 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2415 CE->getOperatorLoc());
2416 if (GetInitVarDecl(CE->getArg(1)) == Var)
2417 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
2418 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2419 CE->getOperatorLoc());
2420 break;
2421 default:
2422 break;
2423 }
2424 }
2425 }
2426 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
2427 << S->getSourceRange() << Var;
2428 return true;
2429}
2430
2431bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
2432 // RHS of canonical loop form increment can be:
2433 // var + incr
2434 // incr + var
2435 // var - incr
2436 //
2437 RHS = RHS->IgnoreParenImpCasts();
2438 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
2439 if (BO->isAdditiveOp()) {
2440 bool IsAdd = BO->getOpcode() == BO_Add;
2441 if (GetInitVarDecl(BO->getLHS()) == Var)
2442 return SetStep(BO->getRHS(), !IsAdd);
2443 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
2444 return SetStep(BO->getLHS(), false);
2445 }
2446 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
2447 bool IsAdd = CE->getOperator() == OO_Plus;
2448 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
2449 if (GetInitVarDecl(CE->getArg(0)) == Var)
2450 return SetStep(CE->getArg(1), !IsAdd);
2451 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
2452 return SetStep(CE->getArg(0), false);
2453 }
2454 }
2455 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2456 << RHS->getSourceRange() << Var;
2457 return true;
2458}
2459
2460bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
2461 // Check incr-expr for canonical loop form and return true if it
2462 // does not conform.
2463 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2464 // ++var
2465 // var++
2466 // --var
2467 // var--
2468 // var += incr
2469 // var -= incr
2470 // var = var + incr
2471 // var = incr + var
2472 // var = var - incr
2473 //
2474 if (!S) {
2475 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
2476 return true;
2477 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002478 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002479 S = S->IgnoreParens();
2480 if (auto UO = dyn_cast<UnaryOperator>(S)) {
2481 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
2482 return SetStep(
2483 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
2484 (UO->isDecrementOp() ? -1 : 1)).get(),
2485 false);
2486 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
2487 switch (BO->getOpcode()) {
2488 case BO_AddAssign:
2489 case BO_SubAssign:
2490 if (GetInitVarDecl(BO->getLHS()) == Var)
2491 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
2492 break;
2493 case BO_Assign:
2494 if (GetInitVarDecl(BO->getLHS()) == Var)
2495 return CheckIncRHS(BO->getRHS());
2496 break;
2497 default:
2498 break;
2499 }
2500 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2501 switch (CE->getOperator()) {
2502 case OO_PlusPlus:
2503 case OO_MinusMinus:
2504 if (GetInitVarDecl(CE->getArg(0)) == Var)
2505 return SetStep(
2506 SemaRef.ActOnIntegerConstant(
2507 CE->getLocStart(),
2508 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
2509 false);
2510 break;
2511 case OO_PlusEqual:
2512 case OO_MinusEqual:
2513 if (GetInitVarDecl(CE->getArg(0)) == Var)
2514 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
2515 break;
2516 case OO_Equal:
2517 if (GetInitVarDecl(CE->getArg(0)) == Var)
2518 return CheckIncRHS(CE->getArg(1));
2519 break;
2520 default:
2521 break;
2522 }
2523 }
2524 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2525 << S->getSourceRange() << Var;
2526 return true;
2527}
Alexander Musmana5f070a2014-10-01 06:03:56 +00002528
2529/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002530Expr *
2531OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
2532 const bool LimitedType) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002533 ExprResult Diff;
2534 if (Var->getType()->isIntegerType() || Var->getType()->isPointerType() ||
2535 SemaRef.getLangOpts().CPlusPlus) {
2536 // Upper - Lower
2537 Expr *Upper = TestIsLessOp ? UB : LB;
2538 Expr *Lower = TestIsLessOp ? LB : UB;
2539
2540 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
2541
2542 if (!Diff.isUsable() && Var->getType()->getAsCXXRecordDecl()) {
2543 // BuildBinOp already emitted error, this one is to point user to upper
2544 // and lower bound, and to tell what is passed to 'operator-'.
2545 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
2546 << Upper->getSourceRange() << Lower->getSourceRange();
2547 return nullptr;
2548 }
2549 }
2550
2551 if (!Diff.isUsable())
2552 return nullptr;
2553
2554 // Upper - Lower [- 1]
2555 if (TestIsStrictOp)
2556 Diff = SemaRef.BuildBinOp(
2557 S, DefaultLoc, BO_Sub, Diff.get(),
2558 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2559 if (!Diff.isUsable())
2560 return nullptr;
2561
2562 // Upper - Lower [- 1] + Step
2563 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(),
2564 Step->IgnoreImplicit());
2565 if (!Diff.isUsable())
2566 return nullptr;
2567
2568 // Parentheses (for dumping/debugging purposes only).
2569 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
2570 if (!Diff.isUsable())
2571 return nullptr;
2572
2573 // (Upper - Lower [- 1] + Step) / Step
2574 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(),
2575 Step->IgnoreImplicit());
2576 if (!Diff.isUsable())
2577 return nullptr;
2578
Alexander Musman174b3ca2014-10-06 11:16:29 +00002579 // OpenMP runtime requires 32-bit or 64-bit loop variables.
2580 if (LimitedType) {
2581 auto &C = SemaRef.Context;
2582 QualType Type = Diff.get()->getType();
2583 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
2584 if (NewSize != C.getTypeSize(Type)) {
2585 if (NewSize < C.getTypeSize(Type)) {
2586 assert(NewSize == 64 && "incorrect loop var size");
2587 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
2588 << InitSrcRange << ConditionSrcRange;
2589 }
2590 QualType NewType = C.getIntTypeForBitwidth(
2591 NewSize, Type->hasSignedIntegerRepresentation());
2592 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
2593 Sema::AA_Converting, true);
2594 if (!Diff.isUsable())
2595 return nullptr;
2596 }
2597 }
2598
Alexander Musmana5f070a2014-10-01 06:03:56 +00002599 return Diff.get();
2600}
2601
Alexey Bataev62dbb972015-04-22 11:59:37 +00002602Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
2603 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
2604 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
2605 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
2606 auto CondExpr = SemaRef.BuildBinOp(
2607 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
2608 : (TestIsStrictOp ? BO_GT : BO_GE),
2609 LB, UB);
2610 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
2611 // Otherwise use original loop conditon and evaluate it in runtime.
2612 return CondExpr.isUsable() ? CondExpr.get() : Cond;
2613}
2614
Alexander Musmana5f070a2014-10-01 06:03:56 +00002615/// \brief Build reference expression to the counter be used for codegen.
2616Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataev39f915b82015-05-08 10:41:21 +00002617 return buildDeclRefExpr(SemaRef, Var, Var->getType(), DefaultLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00002618}
2619
2620/// \brief Build initization of the counter be used for codegen.
2621Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
2622
2623/// \brief Build step of the counter be used for codegen.
2624Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
2625
2626/// \brief Iteration space of a single for loop.
2627struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00002628 /// \brief Condition of the loop.
2629 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002630 /// \brief This expression calculates the number of iterations in the loop.
2631 /// It is always possible to calculate it before starting the loop.
2632 Expr *NumIterations;
2633 /// \brief The loop counter variable.
2634 Expr *CounterVar;
2635 /// \brief This is initializer for the initial value of #CounterVar.
2636 Expr *CounterInit;
2637 /// \brief This is step for the #CounterVar used to generate its update:
2638 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
2639 Expr *CounterStep;
2640 /// \brief Should step be subtracted?
2641 bool Subtract;
2642 /// \brief Source range of the loop init.
2643 SourceRange InitSrcRange;
2644 /// \brief Source range of the loop condition.
2645 SourceRange CondSrcRange;
2646 /// \brief Source range of the loop increment.
2647 SourceRange IncSrcRange;
2648};
2649
Alexey Bataev23b69422014-06-18 07:08:49 +00002650} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002651
Alexey Bataev9c821032015-04-30 04:23:23 +00002652void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
2653 assert(getLangOpts().OpenMP && "OpenMP is not active.");
2654 assert(Init && "Expected loop in canonical form.");
2655 unsigned CollapseIteration = DSAStack->getCollapseNumber();
2656 if (CollapseIteration > 0 &&
2657 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
2658 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
2659 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
2660 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
2661 }
2662 DSAStack->setCollapseNumber(CollapseIteration - 1);
2663 }
2664}
2665
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002666/// \brief Called on a for stmt to check and extract its iteration space
2667/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00002668static bool CheckOpenMPIterationSpace(
2669 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
2670 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
2671 Expr *NestedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002672 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
2673 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002674 // OpenMP [2.6, Canonical Loop Form]
2675 // for (init-expr; test-expr; incr-expr) structured-block
2676 auto For = dyn_cast_or_null<ForStmt>(S);
2677 if (!For) {
2678 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002679 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind)
2680 << NestedLoopCount << (CurrentNestedLoopCount > 0)
2681 << CurrentNestedLoopCount;
2682 if (NestedLoopCount > 1)
2683 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(),
2684 diag::note_omp_collapse_expr)
2685 << NestedLoopCountExpr->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002686 return true;
2687 }
2688 assert(For->getBody());
2689
2690 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
2691
2692 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002693 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002694 if (ISC.CheckInit(Init)) {
2695 return true;
2696 }
2697
2698 bool HasErrors = false;
2699
2700 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002701 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002702
2703 // OpenMP [2.6, Canonical Loop Form]
2704 // Var is one of the following:
2705 // A variable of signed or unsigned integer type.
2706 // For C++, a variable of a random access iterator type.
2707 // For C, a variable of a pointer type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002708 auto VarType = Var->getType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002709 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
2710 !VarType->isPointerType() &&
2711 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
2712 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
2713 << SemaRef.getLangOpts().CPlusPlus;
2714 HasErrors = true;
2715 }
2716
Alexey Bataev4acb8592014-07-07 13:01:15 +00002717 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
2718 // Construct
2719 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2720 // parallel for construct is (are) private.
2721 // The loop iteration variable in the associated for-loop of a simd construct
2722 // with just one associated for-loop is linear with a constant-linear-step
2723 // that is the increment of the associated for-loop.
2724 // Exclude loop var from the list of variables with implicitly defined data
2725 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00002726 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002727
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002728 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
2729 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00002730 // The loop iteration variable in the associated for-loop of a simd construct
2731 // with just one associated for-loop may be listed in a linear clause with a
2732 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002733 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2734 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002735 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002736 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
2737 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
2738 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002739 auto PredeterminedCKind =
2740 isOpenMPSimdDirective(DKind)
2741 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
2742 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002743 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00002744 DVar.CKind != OMPC_threadprivate && DVar.CKind != PredeterminedCKind) ||
Alexander Musmanf82886e2014-09-18 05:12:34 +00002745 (isOpenMPWorksharingDirective(DKind) && !isOpenMPSimdDirective(DKind) &&
2746 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00002747 DVar.CKind != OMPC_lastprivate && DVar.CKind != OMPC_threadprivate)) &&
2748 ((DVar.CKind != OMPC_private && DVar.CKind != OMPC_threadprivate) ||
2749 DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002750 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00002751 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
2752 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00002753 if (DVar.RefExpr == nullptr)
2754 DVar.CKind = PredeterminedCKind;
2755 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002756 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002757 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002758 // Make the loop iteration variable private (for worksharing constructs),
2759 // linear (for simd directives with the only one associated loop) or
2760 // lastprivate (for simd directives with several collapsed loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00002761 if (DVar.CKind == OMPC_unknown)
2762 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
2763 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00002764 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002765 }
2766
Alexey Bataev7ff55242014-06-19 09:13:45 +00002767 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00002768
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002769 // Check test-expr.
2770 HasErrors |= ISC.CheckCond(For->getCond());
2771
2772 // Check incr-expr.
2773 HasErrors |= ISC.CheckInc(For->getInc());
2774
Alexander Musmana5f070a2014-10-01 06:03:56 +00002775 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002776 return HasErrors;
2777
Alexander Musmana5f070a2014-10-01 06:03:56 +00002778 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00002779 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00002780 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
2781 DSA.getCurScope(), /* LimitedType */ isOpenMPWorksharingDirective(DKind));
Alexander Musmana5f070a2014-10-01 06:03:56 +00002782 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
2783 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
2784 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
2785 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
2786 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
2787 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
2788 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
2789
Alexey Bataev62dbb972015-04-22 11:59:37 +00002790 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
2791 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00002792 ResultIterSpace.CounterVar == nullptr ||
2793 ResultIterSpace.CounterInit == nullptr ||
2794 ResultIterSpace.CounterStep == nullptr);
2795
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002796 return HasErrors;
2797}
2798
Alexander Musmana5f070a2014-10-01 06:03:56 +00002799/// \brief Build 'VarRef = Start + Iter * Step'.
2800static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
2801 SourceLocation Loc, ExprResult VarRef,
2802 ExprResult Start, ExprResult Iter,
2803 ExprResult Step, bool Subtract) {
2804 // Add parentheses (for debugging purposes only).
2805 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
2806 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
2807 !Step.isUsable())
2808 return ExprError();
2809
2810 ExprResult Update = SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(),
2811 Step.get()->IgnoreImplicit());
2812 if (!Update.isUsable())
2813 return ExprError();
2814
2815 // Build 'VarRef = Start + Iter * Step'.
2816 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
2817 Start.get()->IgnoreImplicit(), Update.get());
2818 if (!Update.isUsable())
2819 return ExprError();
2820
2821 Update = SemaRef.PerformImplicitConversion(
2822 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
2823 if (!Update.isUsable())
2824 return ExprError();
2825
2826 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
2827 return Update;
2828}
2829
2830/// \brief Convert integer expression \a E to make it have at least \a Bits
2831/// bits.
2832static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
2833 Sema &SemaRef) {
2834 if (E == nullptr)
2835 return ExprError();
2836 auto &C = SemaRef.Context;
2837 QualType OldType = E->getType();
2838 unsigned HasBits = C.getTypeSize(OldType);
2839 if (HasBits >= Bits)
2840 return ExprResult(E);
2841 // OK to convert to signed, because new type has more bits than old.
2842 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
2843 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
2844 true);
2845}
2846
2847/// \brief Check if the given expression \a E is a constant integer that fits
2848/// into \a Bits bits.
2849static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
2850 if (E == nullptr)
2851 return false;
2852 llvm::APSInt Result;
2853 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
2854 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
2855 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002856}
2857
2858/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00002859/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
2860/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002861static unsigned
2862CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *NestedLoopCountExpr,
2863 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002864 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00002865 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002866 unsigned NestedLoopCount = 1;
2867 if (NestedLoopCountExpr) {
2868 // Found 'collapse' clause - calculate collapse number.
2869 llvm::APSInt Result;
2870 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
2871 NestedLoopCount = Result.getLimitedValue();
2872 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002873 // This is helper routine for loop directives (e.g., 'for', 'simd',
2874 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00002875 SmallVector<LoopIterationSpace, 4> IterSpaces;
2876 IterSpaces.resize(NestedLoopCount);
2877 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002878 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002879 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev4acb8592014-07-07 13:01:15 +00002880 NestedLoopCount, NestedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002881 VarsWithImplicitDSA, IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00002882 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002883 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00002884 // OpenMP [2.8.1, simd construct, Restrictions]
2885 // All loops associated with the construct must be perfectly nested; that
2886 // is, there must be no intervening code nor any OpenMP directive between
2887 // any two loops.
2888 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002889 }
2890
Alexander Musmana5f070a2014-10-01 06:03:56 +00002891 Built.clear(/* size */ NestedLoopCount);
2892
2893 if (SemaRef.CurContext->isDependentContext())
2894 return NestedLoopCount;
2895
2896 // An example of what is generated for the following code:
2897 //
2898 // #pragma omp simd collapse(2)
2899 // for (i = 0; i < NI; ++i)
2900 // for (j = J0; j < NJ; j+=2) {
2901 // <loop body>
2902 // }
2903 //
2904 // We generate the code below.
2905 // Note: the loop body may be outlined in CodeGen.
2906 // Note: some counters may be C++ classes, operator- is used to find number of
2907 // iterations and operator+= to calculate counter value.
2908 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
2909 // or i64 is currently supported).
2910 //
2911 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
2912 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
2913 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
2914 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
2915 // // similar updates for vars in clauses (e.g. 'linear')
2916 // <loop body (using local i and j)>
2917 // }
2918 // i = NI; // assign final values of counters
2919 // j = NJ;
2920 //
2921
2922 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
2923 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00002924 // Precondition tests if there is at least one iteration (all conditions are
2925 // true).
2926 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00002927 auto N0 = IterSpaces[0].NumIterations;
2928 ExprResult LastIteration32 = WidenIterationCount(32 /* Bits */, N0, SemaRef);
2929 ExprResult LastIteration64 = WidenIterationCount(64 /* Bits */, N0, SemaRef);
2930
2931 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
2932 return NestedLoopCount;
2933
2934 auto &C = SemaRef.Context;
2935 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
2936
2937 Scope *CurScope = DSA.getCurScope();
2938 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00002939 if (PreCond.isUsable()) {
2940 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
2941 PreCond.get(), IterSpaces[Cnt].PreCond);
2942 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002943 auto N = IterSpaces[Cnt].NumIterations;
2944 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
2945 if (LastIteration32.isUsable())
2946 LastIteration32 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
2947 LastIteration32.get(), N);
2948 if (LastIteration64.isUsable())
2949 LastIteration64 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
2950 LastIteration64.get(), N);
2951 }
2952
2953 // Choose either the 32-bit or 64-bit version.
2954 ExprResult LastIteration = LastIteration64;
2955 if (LastIteration32.isUsable() &&
2956 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
2957 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
2958 FitsInto(
2959 32 /* Bits */,
2960 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
2961 LastIteration64.get(), SemaRef)))
2962 LastIteration = LastIteration32;
2963
2964 if (!LastIteration.isUsable())
2965 return 0;
2966
2967 // Save the number of iterations.
2968 ExprResult NumIterations = LastIteration;
2969 {
2970 LastIteration = SemaRef.BuildBinOp(
2971 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
2972 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2973 if (!LastIteration.isUsable())
2974 return 0;
2975 }
2976
2977 // Calculate the last iteration number beforehand instead of doing this on
2978 // each iteration. Do not do this if the number of iterations may be kfold-ed.
2979 llvm::APSInt Result;
2980 bool IsConstant =
2981 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
2982 ExprResult CalcLastIteration;
2983 if (!IsConstant) {
2984 SourceLocation SaveLoc;
2985 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00002986 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00002987 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00002988 ExprResult SaveRef = buildDeclRefExpr(
2989 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00002990 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
2991 SaveRef.get(), LastIteration.get());
2992 LastIteration = SaveRef;
2993
2994 // Prepare SaveRef + 1.
2995 NumIterations = SemaRef.BuildBinOp(
2996 CurScope, SaveLoc, BO_Add, SaveRef.get(),
2997 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2998 if (!NumIterations.isUsable())
2999 return 0;
3000 }
3001
3002 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
3003
Alexander Musmanc6388682014-12-15 07:07:06 +00003004 QualType VType = LastIteration.get()->getType();
3005 // Build variables passed into runtime, nesessary for worksharing directives.
3006 ExprResult LB, UB, IL, ST, EUB;
3007 if (isOpenMPWorksharingDirective(DKind)) {
3008 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003009 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
3010 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003011 SemaRef.AddInitializerToDecl(
3012 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3013 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3014
3015 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003016 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
3017 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003018 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
3019 /*DirectInit*/ false,
3020 /*TypeMayContainAuto*/ false);
3021
3022 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
3023 // This will be used to implement clause 'lastprivate'.
3024 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00003025 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
3026 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003027 SemaRef.AddInitializerToDecl(
3028 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3029 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3030
3031 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00003032 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
3033 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003034 SemaRef.AddInitializerToDecl(
3035 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
3036 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3037
3038 // Build expression: UB = min(UB, LastIteration)
3039 // It is nesessary for CodeGen of directives with static scheduling.
3040 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
3041 UB.get(), LastIteration.get());
3042 ExprResult CondOp = SemaRef.ActOnConditionalOp(
3043 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
3044 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
3045 CondOp.get());
3046 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
3047 }
3048
3049 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003050 ExprResult IV;
3051 ExprResult Init;
3052 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00003053 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
3054 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003055 Expr *RHS = isOpenMPWorksharingDirective(DKind)
3056 ? LB.get()
3057 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
3058 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
3059 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003060 }
3061
Alexander Musmanc6388682014-12-15 07:07:06 +00003062 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003063 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00003064 ExprResult Cond =
3065 isOpenMPWorksharingDirective(DKind)
3066 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
3067 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
3068 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003069
3070 // Loop increment (IV = IV + 1)
3071 SourceLocation IncLoc;
3072 ExprResult Inc =
3073 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
3074 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
3075 if (!Inc.isUsable())
3076 return 0;
3077 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00003078 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
3079 if (!Inc.isUsable())
3080 return 0;
3081
3082 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
3083 // Used for directives with static scheduling.
3084 ExprResult NextLB, NextUB;
3085 if (isOpenMPWorksharingDirective(DKind)) {
3086 // LB + ST
3087 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
3088 if (!NextLB.isUsable())
3089 return 0;
3090 // LB = LB + ST
3091 NextLB =
3092 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
3093 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
3094 if (!NextLB.isUsable())
3095 return 0;
3096 // UB + ST
3097 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
3098 if (!NextUB.isUsable())
3099 return 0;
3100 // UB = UB + ST
3101 NextUB =
3102 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
3103 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
3104 if (!NextUB.isUsable())
3105 return 0;
3106 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003107
3108 // Build updates and final values of the loop counters.
3109 bool HasErrors = false;
3110 Built.Counters.resize(NestedLoopCount);
3111 Built.Updates.resize(NestedLoopCount);
3112 Built.Finals.resize(NestedLoopCount);
3113 {
3114 ExprResult Div;
3115 // Go from inner nested loop to outer.
3116 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
3117 LoopIterationSpace &IS = IterSpaces[Cnt];
3118 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
3119 // Build: Iter = (IV / Div) % IS.NumIters
3120 // where Div is product of previous iterations' IS.NumIters.
3121 ExprResult Iter;
3122 if (Div.isUsable()) {
3123 Iter =
3124 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
3125 } else {
3126 Iter = IV;
3127 assert((Cnt == (int)NestedLoopCount - 1) &&
3128 "unusable div expected on first iteration only");
3129 }
3130
3131 if (Cnt != 0 && Iter.isUsable())
3132 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
3133 IS.NumIterations);
3134 if (!Iter.isUsable()) {
3135 HasErrors = true;
3136 break;
3137 }
3138
Alexey Bataev39f915b82015-05-08 10:41:21 +00003139 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
3140 auto *CounterVar = buildDeclRefExpr(
3141 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
3142 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
3143 /*RefersToCapture=*/true);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003144 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003145 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003146 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
3147 if (!Update.isUsable()) {
3148 HasErrors = true;
3149 break;
3150 }
3151
3152 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
3153 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00003154 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003155 IS.NumIterations, IS.CounterStep, IS.Subtract);
3156 if (!Final.isUsable()) {
3157 HasErrors = true;
3158 break;
3159 }
3160
3161 // Build Div for the next iteration: Div <- Div * IS.NumIters
3162 if (Cnt != 0) {
3163 if (Div.isUnset())
3164 Div = IS.NumIterations;
3165 else
3166 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
3167 IS.NumIterations);
3168
3169 // Add parentheses (for debugging purposes only).
3170 if (Div.isUsable())
3171 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
3172 if (!Div.isUsable()) {
3173 HasErrors = true;
3174 break;
3175 }
3176 }
3177 if (!Update.isUsable() || !Final.isUsable()) {
3178 HasErrors = true;
3179 break;
3180 }
3181 // Save results
3182 Built.Counters[Cnt] = IS.CounterVar;
3183 Built.Updates[Cnt] = Update.get();
3184 Built.Finals[Cnt] = Final.get();
3185 }
3186 }
3187
3188 if (HasErrors)
3189 return 0;
3190
3191 // Save results
3192 Built.IterationVarRef = IV.get();
3193 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00003194 Built.NumIterations = NumIterations.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003195 Built.CalcLastIteration = CalcLastIteration.get();
3196 Built.PreCond = PreCond.get();
3197 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003198 Built.Init = Init.get();
3199 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00003200 Built.LB = LB.get();
3201 Built.UB = UB.get();
3202 Built.IL = IL.get();
3203 Built.ST = ST.get();
3204 Built.EUB = EUB.get();
3205 Built.NLB = NextLB.get();
3206 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003207
Alexey Bataevabfc0692014-06-25 06:52:00 +00003208 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003209}
3210
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003211static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevc925aa32015-04-27 08:00:32 +00003212 auto &&CollapseFilter = [](const OMPClause *C) -> bool {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003213 return C->getClauseKind() == OMPC_collapse;
3214 };
3215 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
Alexey Bataevc925aa32015-04-27 08:00:32 +00003216 Clauses, std::move(CollapseFilter));
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003217 if (I)
3218 return cast<OMPCollapseClause>(*I)->getNumForLoops();
3219 return nullptr;
3220}
3221
Alexey Bataev4acb8592014-07-07 13:01:15 +00003222StmtResult Sema::ActOnOpenMPSimdDirective(
3223 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3224 SourceLocation EndLoc,
3225 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003226 OMPLoopDirective::HelperExprs B;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003227 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003228 unsigned NestedLoopCount =
3229 CheckOpenMPLoop(OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003230 *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00003231 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003232 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003233
Alexander Musmana5f070a2014-10-01 06:03:56 +00003234 assert((CurContext->isDependentContext() || B.builtAll()) &&
3235 "omp simd loop exprs were not built");
3236
Alexander Musman3276a272015-03-21 10:12:56 +00003237 if (!CurContext->isDependentContext()) {
3238 // Finalize the clauses that need pre-built expressions for CodeGen.
3239 for (auto C : Clauses) {
3240 if (auto LC = dyn_cast<OMPLinearClause>(C))
3241 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3242 B.NumIterations, *this, CurScope))
3243 return StmtError();
3244 }
3245 }
3246
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003247 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003248 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3249 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003250}
3251
Alexey Bataev4acb8592014-07-07 13:01:15 +00003252StmtResult Sema::ActOnOpenMPForDirective(
3253 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3254 SourceLocation EndLoc,
3255 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003256 OMPLoopDirective::HelperExprs B;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003257 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003258 unsigned NestedLoopCount =
3259 CheckOpenMPLoop(OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003260 *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00003261 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00003262 return StmtError();
3263
Alexander Musmana5f070a2014-10-01 06:03:56 +00003264 assert((CurContext->isDependentContext() || B.builtAll()) &&
3265 "omp for loop exprs were not built");
3266
Alexey Bataevf29276e2014-06-18 04:14:57 +00003267 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003268 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3269 Clauses, AStmt, B);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003270}
3271
Alexander Musmanf82886e2014-09-18 05:12:34 +00003272StmtResult Sema::ActOnOpenMPForSimdDirective(
3273 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3274 SourceLocation EndLoc,
3275 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003276 OMPLoopDirective::HelperExprs B;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003277 // In presence of clause 'collapse', it will define the nested loops number.
3278 unsigned NestedLoopCount =
3279 CheckOpenMPLoop(OMPD_for_simd, GetCollapseNumberExpr(Clauses), AStmt,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003280 *this, *DSAStack, VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00003281 if (NestedLoopCount == 0)
3282 return StmtError();
3283
Alexander Musmanc6388682014-12-15 07:07:06 +00003284 assert((CurContext->isDependentContext() || B.builtAll()) &&
3285 "omp for simd loop exprs were not built");
3286
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00003287 if (!CurContext->isDependentContext()) {
3288 // Finalize the clauses that need pre-built expressions for CodeGen.
3289 for (auto C : Clauses) {
3290 if (auto LC = dyn_cast<OMPLinearClause>(C))
3291 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3292 B.NumIterations, *this, CurScope))
3293 return StmtError();
3294 }
3295 }
3296
Alexander Musmanf82886e2014-09-18 05:12:34 +00003297 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003298 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3299 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00003300}
3301
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003302StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
3303 Stmt *AStmt,
3304 SourceLocation StartLoc,
3305 SourceLocation EndLoc) {
3306 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3307 auto BaseStmt = AStmt;
3308 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3309 BaseStmt = CS->getCapturedStmt();
3310 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3311 auto S = C->children();
3312 if (!S)
3313 return StmtError();
3314 // All associated statements must be '#pragma omp section' except for
3315 // the first one.
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003316 for (++S; S; ++S) {
3317 auto SectionStmt = *S;
3318 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3319 if (SectionStmt)
3320 Diag(SectionStmt->getLocStart(),
3321 diag::err_omp_sections_substmt_not_section);
3322 return StmtError();
3323 }
3324 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003325 } else {
3326 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
3327 return StmtError();
3328 }
3329
3330 getCurFunction()->setHasBranchProtectedScope();
3331
3332 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
3333 AStmt);
3334}
3335
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003336StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
3337 SourceLocation StartLoc,
3338 SourceLocation EndLoc) {
3339 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3340
3341 getCurFunction()->setHasBranchProtectedScope();
3342
3343 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
3344}
3345
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003346StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
3347 Stmt *AStmt,
3348 SourceLocation StartLoc,
3349 SourceLocation EndLoc) {
Alexey Bataev74a05c92014-07-15 02:55:09 +00003350 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3351
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003352 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00003353
Alexey Bataev3255bf32015-01-19 05:20:46 +00003354 // OpenMP [2.7.3, single Construct, Restrictions]
3355 // The copyprivate clause must not be used with the nowait clause.
3356 OMPClause *Nowait = nullptr;
3357 OMPClause *Copyprivate = nullptr;
3358 for (auto *Clause : Clauses) {
3359 if (Clause->getClauseKind() == OMPC_nowait)
3360 Nowait = Clause;
3361 else if (Clause->getClauseKind() == OMPC_copyprivate)
3362 Copyprivate = Clause;
3363 if (Copyprivate && Nowait) {
3364 Diag(Copyprivate->getLocStart(),
3365 diag::err_omp_single_copyprivate_with_nowait);
3366 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
3367 return StmtError();
3368 }
3369 }
3370
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003371 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3372}
3373
Alexander Musman80c22892014-07-17 08:54:58 +00003374StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
3375 SourceLocation StartLoc,
3376 SourceLocation EndLoc) {
3377 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3378
3379 getCurFunction()->setHasBranchProtectedScope();
3380
3381 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
3382}
3383
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003384StmtResult
3385Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
3386 Stmt *AStmt, SourceLocation StartLoc,
3387 SourceLocation EndLoc) {
3388 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3389
3390 getCurFunction()->setHasBranchProtectedScope();
3391
3392 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
3393 AStmt);
3394}
3395
Alexey Bataev4acb8592014-07-07 13:01:15 +00003396StmtResult Sema::ActOnOpenMPParallelForDirective(
3397 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3398 SourceLocation EndLoc,
3399 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3400 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3401 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3402 // 1.2.2 OpenMP Language Terminology
3403 // Structured block - An executable statement with a single entry at the
3404 // top and a single exit at the bottom.
3405 // The point of exit cannot be a branch out of the structured block.
3406 // longjmp() and throw() must not violate the entry/exit criteria.
3407 CS->getCapturedDecl()->setNothrow();
3408
Alexander Musmanc6388682014-12-15 07:07:06 +00003409 OMPLoopDirective::HelperExprs B;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003410 // In presence of clause 'collapse', it will define the nested loops number.
3411 unsigned NestedLoopCount =
3412 CheckOpenMPLoop(OMPD_parallel_for, GetCollapseNumberExpr(Clauses), AStmt,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003413 *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003414 if (NestedLoopCount == 0)
3415 return StmtError();
3416
Alexander Musmana5f070a2014-10-01 06:03:56 +00003417 assert((CurContext->isDependentContext() || B.builtAll()) &&
3418 "omp parallel for loop exprs were not built");
3419
Alexey Bataev4acb8592014-07-07 13:01:15 +00003420 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003421 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
3422 NestedLoopCount, Clauses, AStmt, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003423}
3424
Alexander Musmane4e893b2014-09-23 09:33:00 +00003425StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
3426 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3427 SourceLocation EndLoc,
3428 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3429 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3430 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3431 // 1.2.2 OpenMP Language Terminology
3432 // Structured block - An executable statement with a single entry at the
3433 // top and a single exit at the bottom.
3434 // The point of exit cannot be a branch out of the structured block.
3435 // longjmp() and throw() must not violate the entry/exit criteria.
3436 CS->getCapturedDecl()->setNothrow();
3437
Alexander Musmanc6388682014-12-15 07:07:06 +00003438 OMPLoopDirective::HelperExprs B;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003439 // In presence of clause 'collapse', it will define the nested loops number.
3440 unsigned NestedLoopCount =
3441 CheckOpenMPLoop(OMPD_parallel_for_simd, GetCollapseNumberExpr(Clauses),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003442 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003443 if (NestedLoopCount == 0)
3444 return StmtError();
3445
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00003446 if (!CurContext->isDependentContext()) {
3447 // Finalize the clauses that need pre-built expressions for CodeGen.
3448 for (auto C : Clauses) {
3449 if (auto LC = dyn_cast<OMPLinearClause>(C))
3450 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3451 B.NumIterations, *this, CurScope))
3452 return StmtError();
3453 }
3454 }
3455
Alexander Musmane4e893b2014-09-23 09:33:00 +00003456 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003457 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00003458 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003459}
3460
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003461StmtResult
3462Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
3463 Stmt *AStmt, SourceLocation StartLoc,
3464 SourceLocation EndLoc) {
3465 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3466 auto BaseStmt = AStmt;
3467 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3468 BaseStmt = CS->getCapturedStmt();
3469 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3470 auto S = C->children();
3471 if (!S)
3472 return StmtError();
3473 // All associated statements must be '#pragma omp section' except for
3474 // the first one.
3475 for (++S; S; ++S) {
3476 auto SectionStmt = *S;
3477 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3478 if (SectionStmt)
3479 Diag(SectionStmt->getLocStart(),
3480 diag::err_omp_parallel_sections_substmt_not_section);
3481 return StmtError();
3482 }
3483 }
3484 } else {
3485 Diag(AStmt->getLocStart(),
3486 diag::err_omp_parallel_sections_not_compound_stmt);
3487 return StmtError();
3488 }
3489
3490 getCurFunction()->setHasBranchProtectedScope();
3491
3492 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
3493 Clauses, AStmt);
3494}
3495
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003496StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
3497 Stmt *AStmt, SourceLocation StartLoc,
3498 SourceLocation EndLoc) {
3499 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3500 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3501 // 1.2.2 OpenMP Language Terminology
3502 // Structured block - An executable statement with a single entry at the
3503 // top and a single exit at the bottom.
3504 // The point of exit cannot be a branch out of the structured block.
3505 // longjmp() and throw() must not violate the entry/exit criteria.
3506 CS->getCapturedDecl()->setNothrow();
3507
3508 getCurFunction()->setHasBranchProtectedScope();
3509
3510 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3511}
3512
Alexey Bataev68446b72014-07-18 07:47:19 +00003513StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
3514 SourceLocation EndLoc) {
3515 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
3516}
3517
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003518StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
3519 SourceLocation EndLoc) {
3520 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
3521}
3522
Alexey Bataev2df347a2014-07-18 10:17:07 +00003523StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
3524 SourceLocation EndLoc) {
3525 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
3526}
3527
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003528StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
3529 SourceLocation StartLoc,
3530 SourceLocation EndLoc) {
3531 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3532
3533 getCurFunction()->setHasBranchProtectedScope();
3534
3535 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
3536}
3537
Alexey Bataev6125da92014-07-21 11:26:11 +00003538StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
3539 SourceLocation StartLoc,
3540 SourceLocation EndLoc) {
3541 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
3542 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
3543}
3544
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003545StmtResult Sema::ActOnOpenMPOrderedDirective(Stmt *AStmt,
3546 SourceLocation StartLoc,
3547 SourceLocation EndLoc) {
3548 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3549
3550 getCurFunction()->setHasBranchProtectedScope();
3551
3552 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, AStmt);
3553}
3554
Alexey Bataev1d160b12015-03-13 12:27:31 +00003555namespace {
3556/// \brief Helper class for checking expression in 'omp atomic [update]'
3557/// construct.
3558class OpenMPAtomicUpdateChecker {
3559 /// \brief Error results for atomic update expressions.
3560 enum ExprAnalysisErrorCode {
3561 /// \brief A statement is not an expression statement.
3562 NotAnExpression,
3563 /// \brief Expression is not builtin binary or unary operation.
3564 NotABinaryOrUnaryExpression,
3565 /// \brief Unary operation is not post-/pre- increment/decrement operation.
3566 NotAnUnaryIncDecExpression,
3567 /// \brief An expression is not of scalar type.
3568 NotAScalarType,
3569 /// \brief A binary operation is not an assignment operation.
3570 NotAnAssignmentOp,
3571 /// \brief RHS part of the binary operation is not a binary expression.
3572 NotABinaryExpression,
3573 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
3574 /// expression.
3575 NotABinaryOperator,
3576 /// \brief RHS binary operation does not have reference to the updated LHS
3577 /// part.
3578 NotAnUpdateExpression,
3579 /// \brief No errors is found.
3580 NoError
3581 };
3582 /// \brief Reference to Sema.
3583 Sema &SemaRef;
3584 /// \brief A location for note diagnostics (when error is found).
3585 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003586 /// \brief 'x' lvalue part of the source atomic expression.
3587 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003588 /// \brief 'expr' rvalue part of the source atomic expression.
3589 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003590 /// \brief Helper expression of the form
3591 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
3592 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
3593 Expr *UpdateExpr;
3594 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
3595 /// important for non-associative operations.
3596 bool IsXLHSInRHSPart;
3597 BinaryOperatorKind Op;
3598 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00003599 /// \brief true if the source expression is a postfix unary operation, false
3600 /// if it is a prefix unary operation.
3601 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003602
3603public:
3604 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00003605 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00003606 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00003607 /// \brief Check specified statement that it is suitable for 'atomic update'
3608 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00003609 /// expression. If DiagId and NoteId == 0, then only check is performed
3610 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00003611 /// \param DiagId Diagnostic which should be emitted if error is found.
3612 /// \param NoteId Diagnostic note for the main error message.
3613 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00003614 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00003615 /// \brief Return the 'x' lvalue part of the source atomic expression.
3616 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00003617 /// \brief Return the 'expr' rvalue part of the source atomic expression.
3618 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00003619 /// \brief Return the update expression used in calculation of the updated
3620 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
3621 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
3622 Expr *getUpdateExpr() const { return UpdateExpr; }
3623 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
3624 /// false otherwise.
3625 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
3626
Alexey Bataevb78ca832015-04-01 03:33:17 +00003627 /// \brief true if the source expression is a postfix unary operation, false
3628 /// if it is a prefix unary operation.
3629 bool isPostfixUpdate() const { return IsPostfixUpdate; }
3630
Alexey Bataev1d160b12015-03-13 12:27:31 +00003631private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00003632 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
3633 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00003634};
3635} // namespace
3636
3637bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
3638 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
3639 ExprAnalysisErrorCode ErrorFound = NoError;
3640 SourceLocation ErrorLoc, NoteLoc;
3641 SourceRange ErrorRange, NoteRange;
3642 // Allowed constructs are:
3643 // x = x binop expr;
3644 // x = expr binop x;
3645 if (AtomicBinOp->getOpcode() == BO_Assign) {
3646 X = AtomicBinOp->getLHS();
3647 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
3648 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
3649 if (AtomicInnerBinOp->isMultiplicativeOp() ||
3650 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
3651 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00003652 Op = AtomicInnerBinOp->getOpcode();
3653 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00003654 auto *LHS = AtomicInnerBinOp->getLHS();
3655 auto *RHS = AtomicInnerBinOp->getRHS();
3656 llvm::FoldingSetNodeID XId, LHSId, RHSId;
3657 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
3658 /*Canonical=*/true);
3659 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
3660 /*Canonical=*/true);
3661 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
3662 /*Canonical=*/true);
3663 if (XId == LHSId) {
3664 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003665 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003666 } else if (XId == RHSId) {
3667 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003668 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003669 } else {
3670 ErrorLoc = AtomicInnerBinOp->getExprLoc();
3671 ErrorRange = AtomicInnerBinOp->getSourceRange();
3672 NoteLoc = X->getExprLoc();
3673 NoteRange = X->getSourceRange();
3674 ErrorFound = NotAnUpdateExpression;
3675 }
3676 } else {
3677 ErrorLoc = AtomicInnerBinOp->getExprLoc();
3678 ErrorRange = AtomicInnerBinOp->getSourceRange();
3679 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
3680 NoteRange = SourceRange(NoteLoc, NoteLoc);
3681 ErrorFound = NotABinaryOperator;
3682 }
3683 } else {
3684 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
3685 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
3686 ErrorFound = NotABinaryExpression;
3687 }
3688 } else {
3689 ErrorLoc = AtomicBinOp->getExprLoc();
3690 ErrorRange = AtomicBinOp->getSourceRange();
3691 NoteLoc = AtomicBinOp->getOperatorLoc();
3692 NoteRange = SourceRange(NoteLoc, NoteLoc);
3693 ErrorFound = NotAnAssignmentOp;
3694 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00003695 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00003696 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
3697 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
3698 return true;
3699 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00003700 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00003701 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003702}
3703
3704bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
3705 unsigned NoteId) {
3706 ExprAnalysisErrorCode ErrorFound = NoError;
3707 SourceLocation ErrorLoc, NoteLoc;
3708 SourceRange ErrorRange, NoteRange;
3709 // Allowed constructs are:
3710 // x++;
3711 // x--;
3712 // ++x;
3713 // --x;
3714 // x binop= expr;
3715 // x = x binop expr;
3716 // x = expr binop x;
3717 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
3718 AtomicBody = AtomicBody->IgnoreParenImpCasts();
3719 if (AtomicBody->getType()->isScalarType() ||
3720 AtomicBody->isInstantiationDependent()) {
3721 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
3722 AtomicBody->IgnoreParenImpCasts())) {
3723 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00003724 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00003725 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00003726 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00003727 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00003728 X = AtomicCompAssignOp->getLHS();
3729 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003730 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
3731 AtomicBody->IgnoreParenImpCasts())) {
3732 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00003733 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
3734 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003735 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00003736 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
3737 // Check for Unary Operation
3738 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00003739 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00003740 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
3741 OpLoc = AtomicUnaryOp->getOperatorLoc();
3742 X = AtomicUnaryOp->getSubExpr();
3743 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
3744 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003745 } else {
3746 ErrorFound = NotAnUnaryIncDecExpression;
3747 ErrorLoc = AtomicUnaryOp->getExprLoc();
3748 ErrorRange = AtomicUnaryOp->getSourceRange();
3749 NoteLoc = AtomicUnaryOp->getOperatorLoc();
3750 NoteRange = SourceRange(NoteLoc, NoteLoc);
3751 }
3752 } else {
3753 ErrorFound = NotABinaryOrUnaryExpression;
3754 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
3755 NoteRange = ErrorRange = AtomicBody->getSourceRange();
3756 }
3757 } else {
3758 ErrorFound = NotAScalarType;
3759 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
3760 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
3761 }
3762 } else {
3763 ErrorFound = NotAnExpression;
3764 NoteLoc = ErrorLoc = S->getLocStart();
3765 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
3766 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00003767 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00003768 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
3769 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
3770 return true;
3771 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00003772 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00003773 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00003774 // Build an update expression of form 'OpaqueValueExpr(x) binop
3775 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
3776 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
3777 auto *OVEX = new (SemaRef.getASTContext())
3778 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
3779 auto *OVEExpr = new (SemaRef.getASTContext())
3780 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
3781 auto Update =
3782 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
3783 IsXLHSInRHSPart ? OVEExpr : OVEX);
3784 if (Update.isInvalid())
3785 return true;
3786 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
3787 Sema::AA_Casting);
3788 if (Update.isInvalid())
3789 return true;
3790 UpdateExpr = Update.get();
3791 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00003792 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003793}
3794
Alexey Bataev0162e452014-07-22 10:10:35 +00003795StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
3796 Stmt *AStmt,
3797 SourceLocation StartLoc,
3798 SourceLocation EndLoc) {
3799 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003800 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00003801 // 1.2.2 OpenMP Language Terminology
3802 // Structured block - An executable statement with a single entry at the
3803 // top and a single exit at the bottom.
3804 // The point of exit cannot be a branch out of the structured block.
3805 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00003806 OpenMPClauseKind AtomicKind = OMPC_unknown;
3807 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003808 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00003809 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00003810 C->getClauseKind() == OMPC_update ||
3811 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00003812 if (AtomicKind != OMPC_unknown) {
3813 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
3814 << SourceRange(C->getLocStart(), C->getLocEnd());
3815 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
3816 << getOpenMPClauseName(AtomicKind);
3817 } else {
3818 AtomicKind = C->getClauseKind();
3819 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003820 }
3821 }
3822 }
Alexey Bataev62cec442014-11-18 10:14:22 +00003823
Alexey Bataev459dec02014-07-24 06:46:57 +00003824 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00003825 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
3826 Body = EWC->getSubExpr();
3827
Alexey Bataev62cec442014-11-18 10:14:22 +00003828 Expr *X = nullptr;
3829 Expr *V = nullptr;
3830 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003831 Expr *UE = nullptr;
3832 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00003833 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00003834 // OpenMP [2.12.6, atomic Construct]
3835 // In the next expressions:
3836 // * x and v (as applicable) are both l-value expressions with scalar type.
3837 // * During the execution of an atomic region, multiple syntactic
3838 // occurrences of x must designate the same storage location.
3839 // * Neither of v and expr (as applicable) may access the storage location
3840 // designated by x.
3841 // * Neither of x and expr (as applicable) may access the storage location
3842 // designated by v.
3843 // * expr is an expression with scalar type.
3844 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
3845 // * binop, binop=, ++, and -- are not overloaded operators.
3846 // * The expression x binop expr must be numerically equivalent to x binop
3847 // (expr). This requirement is satisfied if the operators in expr have
3848 // precedence greater than binop, or by using parentheses around expr or
3849 // subexpressions of expr.
3850 // * The expression expr binop x must be numerically equivalent to (expr)
3851 // binop x. This requirement is satisfied if the operators in expr have
3852 // precedence equal to or greater than binop, or by using parentheses around
3853 // expr or subexpressions of expr.
3854 // * For forms that allow multiple occurrences of x, the number of times
3855 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00003856 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00003857 enum {
3858 NotAnExpression,
3859 NotAnAssignmentOp,
3860 NotAScalarType,
3861 NotAnLValue,
3862 NoError
3863 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00003864 SourceLocation ErrorLoc, NoteLoc;
3865 SourceRange ErrorRange, NoteRange;
3866 // If clause is read:
3867 // v = x;
3868 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
3869 auto AtomicBinOp =
3870 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
3871 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
3872 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
3873 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
3874 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
3875 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
3876 if (!X->isLValue() || !V->isLValue()) {
3877 auto NotLValueExpr = X->isLValue() ? V : X;
3878 ErrorFound = NotAnLValue;
3879 ErrorLoc = AtomicBinOp->getExprLoc();
3880 ErrorRange = AtomicBinOp->getSourceRange();
3881 NoteLoc = NotLValueExpr->getExprLoc();
3882 NoteRange = NotLValueExpr->getSourceRange();
3883 }
3884 } else if (!X->isInstantiationDependent() ||
3885 !V->isInstantiationDependent()) {
3886 auto NotScalarExpr =
3887 (X->isInstantiationDependent() || X->getType()->isScalarType())
3888 ? V
3889 : X;
3890 ErrorFound = NotAScalarType;
3891 ErrorLoc = AtomicBinOp->getExprLoc();
3892 ErrorRange = AtomicBinOp->getSourceRange();
3893 NoteLoc = NotScalarExpr->getExprLoc();
3894 NoteRange = NotScalarExpr->getSourceRange();
3895 }
3896 } else {
3897 ErrorFound = NotAnAssignmentOp;
3898 ErrorLoc = AtomicBody->getExprLoc();
3899 ErrorRange = AtomicBody->getSourceRange();
3900 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
3901 : AtomicBody->getExprLoc();
3902 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
3903 : AtomicBody->getSourceRange();
3904 }
3905 } else {
3906 ErrorFound = NotAnExpression;
3907 NoteLoc = ErrorLoc = Body->getLocStart();
3908 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00003909 }
Alexey Bataev62cec442014-11-18 10:14:22 +00003910 if (ErrorFound != NoError) {
3911 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
3912 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00003913 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
3914 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00003915 return StmtError();
3916 } else if (CurContext->isDependentContext())
3917 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00003918 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00003919 enum {
3920 NotAnExpression,
3921 NotAnAssignmentOp,
3922 NotAScalarType,
3923 NotAnLValue,
3924 NoError
3925 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00003926 SourceLocation ErrorLoc, NoteLoc;
3927 SourceRange ErrorRange, NoteRange;
3928 // If clause is write:
3929 // x = expr;
3930 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
3931 auto AtomicBinOp =
3932 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
3933 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00003934 X = AtomicBinOp->getLHS();
3935 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00003936 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
3937 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
3938 if (!X->isLValue()) {
3939 ErrorFound = NotAnLValue;
3940 ErrorLoc = AtomicBinOp->getExprLoc();
3941 ErrorRange = AtomicBinOp->getSourceRange();
3942 NoteLoc = X->getExprLoc();
3943 NoteRange = X->getSourceRange();
3944 }
3945 } else if (!X->isInstantiationDependent() ||
3946 !E->isInstantiationDependent()) {
3947 auto NotScalarExpr =
3948 (X->isInstantiationDependent() || X->getType()->isScalarType())
3949 ? E
3950 : X;
3951 ErrorFound = NotAScalarType;
3952 ErrorLoc = AtomicBinOp->getExprLoc();
3953 ErrorRange = AtomicBinOp->getSourceRange();
3954 NoteLoc = NotScalarExpr->getExprLoc();
3955 NoteRange = NotScalarExpr->getSourceRange();
3956 }
3957 } else {
3958 ErrorFound = NotAnAssignmentOp;
3959 ErrorLoc = AtomicBody->getExprLoc();
3960 ErrorRange = AtomicBody->getSourceRange();
3961 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
3962 : AtomicBody->getExprLoc();
3963 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
3964 : AtomicBody->getSourceRange();
3965 }
3966 } else {
3967 ErrorFound = NotAnExpression;
3968 NoteLoc = ErrorLoc = Body->getLocStart();
3969 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00003970 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00003971 if (ErrorFound != NoError) {
3972 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
3973 << ErrorRange;
3974 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
3975 << NoteRange;
3976 return StmtError();
3977 } else if (CurContext->isDependentContext())
3978 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00003979 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00003980 // If clause is update:
3981 // x++;
3982 // x--;
3983 // ++x;
3984 // --x;
3985 // x binop= expr;
3986 // x = x binop expr;
3987 // x = expr binop x;
3988 OpenMPAtomicUpdateChecker Checker(*this);
3989 if (Checker.checkStatement(
3990 Body, (AtomicKind == OMPC_update)
3991 ? diag::err_omp_atomic_update_not_expression_statement
3992 : diag::err_omp_atomic_not_expression_statement,
3993 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00003994 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00003995 if (!CurContext->isDependentContext()) {
3996 E = Checker.getExpr();
3997 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00003998 UE = Checker.getUpdateExpr();
3999 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00004000 }
Alexey Bataev459dec02014-07-24 06:46:57 +00004001 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004002 enum {
4003 NotAnAssignmentOp,
4004 NotACompoundStatement,
4005 NotTwoSubstatements,
4006 NotASpecificExpression,
4007 NoError
4008 } ErrorFound = NoError;
4009 SourceLocation ErrorLoc, NoteLoc;
4010 SourceRange ErrorRange, NoteRange;
4011 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
4012 // If clause is a capture:
4013 // v = x++;
4014 // v = x--;
4015 // v = ++x;
4016 // v = --x;
4017 // v = x binop= expr;
4018 // v = x = x binop expr;
4019 // v = x = expr binop x;
4020 auto *AtomicBinOp =
4021 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4022 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4023 V = AtomicBinOp->getLHS();
4024 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
4025 OpenMPAtomicUpdateChecker Checker(*this);
4026 if (Checker.checkStatement(
4027 Body, diag::err_omp_atomic_capture_not_expression_statement,
4028 diag::note_omp_atomic_update))
4029 return StmtError();
4030 E = Checker.getExpr();
4031 X = Checker.getX();
4032 UE = Checker.getUpdateExpr();
4033 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
4034 IsPostfixUpdate = Checker.isPostfixUpdate();
4035 } else {
4036 ErrorLoc = AtomicBody->getExprLoc();
4037 ErrorRange = AtomicBody->getSourceRange();
4038 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4039 : AtomicBody->getExprLoc();
4040 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4041 : AtomicBody->getSourceRange();
4042 ErrorFound = NotAnAssignmentOp;
4043 }
4044 if (ErrorFound != NoError) {
4045 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
4046 << ErrorRange;
4047 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
4048 return StmtError();
4049 } else if (CurContext->isDependentContext()) {
4050 UE = V = E = X = nullptr;
4051 }
4052 } else {
4053 // If clause is a capture:
4054 // { v = x; x = expr; }
4055 // { v = x; x++; }
4056 // { v = x; x--; }
4057 // { v = x; ++x; }
4058 // { v = x; --x; }
4059 // { v = x; x binop= expr; }
4060 // { v = x; x = x binop expr; }
4061 // { v = x; x = expr binop x; }
4062 // { x++; v = x; }
4063 // { x--; v = x; }
4064 // { ++x; v = x; }
4065 // { --x; v = x; }
4066 // { x binop= expr; v = x; }
4067 // { x = x binop expr; v = x; }
4068 // { x = expr binop x; v = x; }
4069 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
4070 // Check that this is { expr1; expr2; }
4071 if (CS->size() == 2) {
4072 auto *First = CS->body_front();
4073 auto *Second = CS->body_back();
4074 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
4075 First = EWC->getSubExpr()->IgnoreParenImpCasts();
4076 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
4077 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
4078 // Need to find what subexpression is 'v' and what is 'x'.
4079 OpenMPAtomicUpdateChecker Checker(*this);
4080 bool IsUpdateExprFound = !Checker.checkStatement(Second);
4081 BinaryOperator *BinOp = nullptr;
4082 if (IsUpdateExprFound) {
4083 BinOp = dyn_cast<BinaryOperator>(First);
4084 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
4085 }
4086 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
4087 // { v = x; x++; }
4088 // { v = x; x--; }
4089 // { v = x; ++x; }
4090 // { v = x; --x; }
4091 // { v = x; x binop= expr; }
4092 // { v = x; x = x binop expr; }
4093 // { v = x; x = expr binop x; }
4094 // Check that the first expression has form v = x.
4095 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
4096 llvm::FoldingSetNodeID XId, PossibleXId;
4097 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
4098 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
4099 IsUpdateExprFound = XId == PossibleXId;
4100 if (IsUpdateExprFound) {
4101 V = BinOp->getLHS();
4102 X = Checker.getX();
4103 E = Checker.getExpr();
4104 UE = Checker.getUpdateExpr();
4105 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00004106 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004107 }
4108 }
4109 if (!IsUpdateExprFound) {
4110 IsUpdateExprFound = !Checker.checkStatement(First);
4111 BinOp = nullptr;
4112 if (IsUpdateExprFound) {
4113 BinOp = dyn_cast<BinaryOperator>(Second);
4114 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
4115 }
4116 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
4117 // { x++; v = x; }
4118 // { x--; v = x; }
4119 // { ++x; v = x; }
4120 // { --x; v = x; }
4121 // { x binop= expr; v = x; }
4122 // { x = x binop expr; v = x; }
4123 // { x = expr binop x; v = x; }
4124 // Check that the second expression has form v = x.
4125 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
4126 llvm::FoldingSetNodeID XId, PossibleXId;
4127 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
4128 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
4129 IsUpdateExprFound = XId == PossibleXId;
4130 if (IsUpdateExprFound) {
4131 V = BinOp->getLHS();
4132 X = Checker.getX();
4133 E = Checker.getExpr();
4134 UE = Checker.getUpdateExpr();
4135 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00004136 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004137 }
4138 }
4139 }
4140 if (!IsUpdateExprFound) {
4141 // { v = x; x = expr; }
4142 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
4143 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
4144 ErrorFound = NotAnAssignmentOp;
4145 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
4146 : First->getLocStart();
4147 NoteRange = ErrorRange = FirstBinOp
4148 ? FirstBinOp->getSourceRange()
4149 : SourceRange(ErrorLoc, ErrorLoc);
4150 } else {
4151 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
4152 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
4153 ErrorFound = NotAnAssignmentOp;
4154 NoteLoc = ErrorLoc = SecondBinOp ? SecondBinOp->getOperatorLoc()
4155 : Second->getLocStart();
4156 NoteRange = ErrorRange = SecondBinOp
4157 ? SecondBinOp->getSourceRange()
4158 : SourceRange(ErrorLoc, ErrorLoc);
4159 } else {
4160 auto *PossibleXRHSInFirst =
4161 FirstBinOp->getRHS()->IgnoreParenImpCasts();
4162 auto *PossibleXLHSInSecond =
4163 SecondBinOp->getLHS()->IgnoreParenImpCasts();
4164 llvm::FoldingSetNodeID X1Id, X2Id;
4165 PossibleXRHSInFirst->Profile(X1Id, Context, /*Canonical=*/true);
4166 PossibleXLHSInSecond->Profile(X2Id, Context,
4167 /*Canonical=*/true);
4168 IsUpdateExprFound = X1Id == X2Id;
4169 if (IsUpdateExprFound) {
4170 V = FirstBinOp->getLHS();
4171 X = SecondBinOp->getLHS();
4172 E = SecondBinOp->getRHS();
4173 UE = nullptr;
4174 IsXLHSInRHSPart = false;
4175 IsPostfixUpdate = true;
4176 } else {
4177 ErrorFound = NotASpecificExpression;
4178 ErrorLoc = FirstBinOp->getExprLoc();
4179 ErrorRange = FirstBinOp->getSourceRange();
4180 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
4181 NoteRange = SecondBinOp->getRHS()->getSourceRange();
4182 }
4183 }
4184 }
4185 }
4186 } else {
4187 NoteLoc = ErrorLoc = Body->getLocStart();
4188 NoteRange = ErrorRange =
4189 SourceRange(Body->getLocStart(), Body->getLocStart());
4190 ErrorFound = NotTwoSubstatements;
4191 }
4192 } else {
4193 NoteLoc = ErrorLoc = Body->getLocStart();
4194 NoteRange = ErrorRange =
4195 SourceRange(Body->getLocStart(), Body->getLocStart());
4196 ErrorFound = NotACompoundStatement;
4197 }
4198 if (ErrorFound != NoError) {
4199 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
4200 << ErrorRange;
4201 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
4202 return StmtError();
4203 } else if (CurContext->isDependentContext()) {
4204 UE = V = E = X = nullptr;
4205 }
Alexey Bataev459dec02014-07-24 06:46:57 +00004206 }
Alexey Bataevdea47612014-07-23 07:46:59 +00004207 }
Alexey Bataev0162e452014-07-22 10:10:35 +00004208
4209 getCurFunction()->setHasBranchProtectedScope();
4210
Alexey Bataev62cec442014-11-18 10:14:22 +00004211 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00004212 X, V, E, UE, IsXLHSInRHSPart,
4213 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00004214}
4215
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004216StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
4217 Stmt *AStmt,
4218 SourceLocation StartLoc,
4219 SourceLocation EndLoc) {
4220 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
4221
Alexey Bataev13314bf2014-10-09 04:18:56 +00004222 // OpenMP [2.16, Nesting of Regions]
4223 // If specified, a teams construct must be contained within a target
4224 // construct. That target construct must contain no statements or directives
4225 // outside of the teams construct.
4226 if (DSAStack->hasInnerTeamsRegion()) {
4227 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
4228 bool OMPTeamsFound = true;
4229 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
4230 auto I = CS->body_begin();
4231 while (I != CS->body_end()) {
4232 auto OED = dyn_cast<OMPExecutableDirective>(*I);
4233 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
4234 OMPTeamsFound = false;
4235 break;
4236 }
4237 ++I;
4238 }
4239 assert(I != CS->body_end() && "Not found statement");
4240 S = *I;
4241 }
4242 if (!OMPTeamsFound) {
4243 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
4244 Diag(DSAStack->getInnerTeamsRegionLoc(),
4245 diag::note_omp_nested_teams_construct_here);
4246 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
4247 << isa<OMPExecutableDirective>(S);
4248 return StmtError();
4249 }
4250 }
4251
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004252 getCurFunction()->setHasBranchProtectedScope();
4253
4254 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4255}
4256
Alexey Bataev13314bf2014-10-09 04:18:56 +00004257StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
4258 Stmt *AStmt, SourceLocation StartLoc,
4259 SourceLocation EndLoc) {
4260 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
4261 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4262 // 1.2.2 OpenMP Language Terminology
4263 // Structured block - An executable statement with a single entry at the
4264 // top and a single exit at the bottom.
4265 // The point of exit cannot be a branch out of the structured block.
4266 // longjmp() and throw() must not violate the entry/exit criteria.
4267 CS->getCapturedDecl()->setNothrow();
4268
4269 getCurFunction()->setHasBranchProtectedScope();
4270
4271 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4272}
4273
Alexey Bataev6d4ed052015-07-01 06:57:41 +00004274StmtResult
4275Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
4276 SourceLocation EndLoc,
4277 OpenMPDirectiveKind CancelRegion) {
4278 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
4279 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
4280 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
4281 << getOpenMPDirectiveName(CancelRegion);
4282 return StmtError();
4283 }
4284 if (DSAStack->isParentNowaitRegion()) {
4285 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
4286 return StmtError();
4287 }
4288 if (DSAStack->isParentOrderedRegion()) {
4289 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
4290 return StmtError();
4291 }
4292 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
4293 CancelRegion);
4294}
4295
Alexey Bataeved09d242014-05-28 05:53:51 +00004296OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004297 SourceLocation StartLoc,
4298 SourceLocation LParenLoc,
4299 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004300 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004301 switch (Kind) {
4302 case OMPC_if:
4303 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
4304 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00004305 case OMPC_final:
4306 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
4307 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00004308 case OMPC_num_threads:
4309 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
4310 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00004311 case OMPC_safelen:
4312 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
4313 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00004314 case OMPC_collapse:
4315 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
4316 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004317 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004318 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004319 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004320 case OMPC_private:
4321 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00004322 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004323 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00004324 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00004325 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004326 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004327 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004328 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004329 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00004330 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004331 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004332 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004333 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004334 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004335 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004336 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004337 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004338 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004339 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004340 case OMPC_depend:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004341 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004342 llvm_unreachable("Clause is not allowed.");
4343 }
4344 return Res;
4345}
4346
Alexey Bataeved09d242014-05-28 05:53:51 +00004347OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004348 SourceLocation LParenLoc,
4349 SourceLocation EndLoc) {
4350 Expr *ValExpr = Condition;
4351 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
4352 !Condition->isInstantiationDependent() &&
4353 !Condition->containsUnexpandedParameterPack()) {
4354 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00004355 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004356 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004357 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004358
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004359 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004360 }
4361
4362 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
4363}
4364
Alexey Bataev3778b602014-07-17 07:32:53 +00004365OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
4366 SourceLocation StartLoc,
4367 SourceLocation LParenLoc,
4368 SourceLocation EndLoc) {
4369 Expr *ValExpr = Condition;
4370 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
4371 !Condition->isInstantiationDependent() &&
4372 !Condition->containsUnexpandedParameterPack()) {
4373 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
4374 Condition->getExprLoc(), Condition);
4375 if (Val.isInvalid())
4376 return nullptr;
4377
4378 ValExpr = Val.get();
4379 }
4380
4381 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
4382}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004383ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
4384 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00004385 if (!Op)
4386 return ExprError();
4387
4388 class IntConvertDiagnoser : public ICEConvertDiagnoser {
4389 public:
4390 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00004391 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00004392 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
4393 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004394 return S.Diag(Loc, diag::err_omp_not_integral) << T;
4395 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004396 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
4397 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004398 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
4399 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004400 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
4401 QualType T,
4402 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004403 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
4404 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004405 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
4406 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004407 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00004408 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00004409 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004410 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
4411 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004412 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
4413 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004414 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
4415 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004416 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00004417 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00004418 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004419 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
4420 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004421 llvm_unreachable("conversion functions are permitted");
4422 }
4423 } ConvertDiagnoser;
4424 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
4425}
4426
4427OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
4428 SourceLocation StartLoc,
4429 SourceLocation LParenLoc,
4430 SourceLocation EndLoc) {
4431 Expr *ValExpr = NumThreads;
4432 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
Alexey Bataev568a8332014-03-06 06:15:19 +00004433 !NumThreads->containsUnexpandedParameterPack()) {
4434 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
4435 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004436 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00004437 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004438 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00004439
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004440 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00004441
4442 // OpenMP [2.5, Restrictions]
4443 // The num_threads expression must evaluate to a positive integer value.
4444 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00004445 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
4446 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00004447 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
4448 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004449 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00004450 }
4451 }
4452
Alexey Bataeved09d242014-05-28 05:53:51 +00004453 return new (Context)
4454 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00004455}
4456
Alexey Bataev62c87d22014-03-21 04:51:18 +00004457ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
4458 OpenMPClauseKind CKind) {
4459 if (!E)
4460 return ExprError();
4461 if (E->isValueDependent() || E->isTypeDependent() ||
4462 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00004463 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00004464 llvm::APSInt Result;
4465 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
4466 if (ICE.isInvalid())
4467 return ExprError();
4468 if (!Result.isStrictlyPositive()) {
4469 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
4470 << getOpenMPClauseName(CKind) << E->getSourceRange();
4471 return ExprError();
4472 }
Alexander Musman09184fe2014-09-30 05:29:28 +00004473 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
4474 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
4475 << E->getSourceRange();
4476 return ExprError();
4477 }
Alexey Bataev9c821032015-04-30 04:23:23 +00004478 if (CKind == OMPC_collapse) {
4479 DSAStack->setCollapseNumber(Result.getExtValue());
4480 }
Alexey Bataev62c87d22014-03-21 04:51:18 +00004481 return ICE;
4482}
4483
4484OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
4485 SourceLocation LParenLoc,
4486 SourceLocation EndLoc) {
4487 // OpenMP [2.8.1, simd construct, Description]
4488 // The parameter of the safelen clause must be a constant
4489 // positive integer expression.
4490 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
4491 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004492 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00004493 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004494 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00004495}
4496
Alexander Musman64d33f12014-06-04 07:53:32 +00004497OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
4498 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00004499 SourceLocation LParenLoc,
4500 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00004501 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00004502 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00004503 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00004504 // The parameter of the collapse clause must be a constant
4505 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00004506 ExprResult NumForLoopsResult =
4507 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
4508 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00004509 return nullptr;
4510 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00004511 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00004512}
4513
Alexey Bataeved09d242014-05-28 05:53:51 +00004514OMPClause *Sema::ActOnOpenMPSimpleClause(
4515 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
4516 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004517 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004518 switch (Kind) {
4519 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00004520 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00004521 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
4522 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004523 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004524 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00004525 Res = ActOnOpenMPProcBindClause(
4526 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
4527 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004528 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004529 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00004530 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00004531 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00004532 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00004533 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004534 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004535 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004536 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00004537 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00004538 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00004539 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00004540 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004541 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004542 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004543 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004544 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00004545 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004546 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004547 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004548 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004549 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004550 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004551 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004552 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004553 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004554 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004555 case OMPC_depend:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004556 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004557 llvm_unreachable("Clause is not allowed.");
4558 }
4559 return Res;
4560}
4561
4562OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
4563 SourceLocation KindKwLoc,
4564 SourceLocation StartLoc,
4565 SourceLocation LParenLoc,
4566 SourceLocation EndLoc) {
4567 if (Kind == OMPC_DEFAULT_unknown) {
4568 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00004569 static_assert(OMPC_DEFAULT_unknown > 0,
4570 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00004571 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00004572 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004573 Values += "'";
4574 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
4575 Values += "'";
4576 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00004577 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004578 Values += " or ";
4579 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00004580 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004581 break;
4582 default:
4583 Values += Sep;
4584 break;
4585 }
4586 }
4587 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00004588 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004589 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004590 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00004591 switch (Kind) {
4592 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004593 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004594 break;
4595 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004596 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004597 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004598 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004599 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00004600 break;
4601 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004602 return new (Context)
4603 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004604}
4605
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004606OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
4607 SourceLocation KindKwLoc,
4608 SourceLocation StartLoc,
4609 SourceLocation LParenLoc,
4610 SourceLocation EndLoc) {
4611 if (Kind == OMPC_PROC_BIND_unknown) {
4612 std::string Values;
4613 std::string Sep(", ");
4614 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
4615 Values += "'";
4616 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
4617 Values += "'";
4618 switch (i) {
4619 case OMPC_PROC_BIND_unknown - 2:
4620 Values += " or ";
4621 break;
4622 case OMPC_PROC_BIND_unknown - 1:
4623 break;
4624 default:
4625 Values += Sep;
4626 break;
4627 }
4628 }
4629 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00004630 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004631 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004632 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004633 return new (Context)
4634 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004635}
4636
Alexey Bataev56dafe82014-06-20 07:16:17 +00004637OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
4638 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
4639 SourceLocation StartLoc, SourceLocation LParenLoc,
4640 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
4641 SourceLocation EndLoc) {
4642 OMPClause *Res = nullptr;
4643 switch (Kind) {
4644 case OMPC_schedule:
4645 Res = ActOnOpenMPScheduleClause(
4646 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
4647 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
4648 break;
4649 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00004650 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004651 case OMPC_num_threads:
4652 case OMPC_safelen:
4653 case OMPC_collapse:
4654 case OMPC_default:
4655 case OMPC_proc_bind:
4656 case OMPC_private:
4657 case OMPC_firstprivate:
4658 case OMPC_lastprivate:
4659 case OMPC_shared:
4660 case OMPC_reduction:
4661 case OMPC_linear:
4662 case OMPC_aligned:
4663 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004664 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004665 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00004666 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004667 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004668 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004669 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004670 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004671 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004672 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004673 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004674 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004675 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004676 case OMPC_depend:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004677 case OMPC_unknown:
4678 llvm_unreachable("Clause is not allowed.");
4679 }
4680 return Res;
4681}
4682
4683OMPClause *Sema::ActOnOpenMPScheduleClause(
4684 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
4685 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
4686 SourceLocation EndLoc) {
4687 if (Kind == OMPC_SCHEDULE_unknown) {
4688 std::string Values;
4689 std::string Sep(", ");
4690 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
4691 Values += "'";
4692 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
4693 Values += "'";
4694 switch (i) {
4695 case OMPC_SCHEDULE_unknown - 2:
4696 Values += " or ";
4697 break;
4698 case OMPC_SCHEDULE_unknown - 1:
4699 break;
4700 default:
4701 Values += Sep;
4702 break;
4703 }
4704 }
4705 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
4706 << Values << getOpenMPClauseName(OMPC_schedule);
4707 return nullptr;
4708 }
4709 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00004710 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00004711 if (ChunkSize) {
4712 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
4713 !ChunkSize->isInstantiationDependent() &&
4714 !ChunkSize->containsUnexpandedParameterPack()) {
4715 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
4716 ExprResult Val =
4717 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
4718 if (Val.isInvalid())
4719 return nullptr;
4720
4721 ValExpr = Val.get();
4722
4723 // OpenMP [2.7.1, Restrictions]
4724 // chunk_size must be a loop invariant integer expression with a positive
4725 // value.
4726 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00004727 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
4728 if (Result.isSigned() && !Result.isStrictlyPositive()) {
4729 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
4730 << "schedule" << ChunkSize->getSourceRange();
4731 return nullptr;
4732 }
4733 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
4734 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
4735 ChunkSize->getType(), ".chunk.");
4736 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
4737 ChunkSize->getExprLoc(),
4738 /*RefersToCapture=*/true);
4739 HelperValExpr = ImpVarRef;
Alexey Bataev56dafe82014-06-20 07:16:17 +00004740 }
4741 }
4742 }
4743
4744 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
Alexey Bataev040d5402015-05-12 08:35:28 +00004745 EndLoc, Kind, ValExpr, HelperValExpr);
Alexey Bataev56dafe82014-06-20 07:16:17 +00004746}
4747
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004748OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
4749 SourceLocation StartLoc,
4750 SourceLocation EndLoc) {
4751 OMPClause *Res = nullptr;
4752 switch (Kind) {
4753 case OMPC_ordered:
4754 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
4755 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00004756 case OMPC_nowait:
4757 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
4758 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004759 case OMPC_untied:
4760 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
4761 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004762 case OMPC_mergeable:
4763 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
4764 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004765 case OMPC_read:
4766 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
4767 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00004768 case OMPC_write:
4769 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
4770 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00004771 case OMPC_update:
4772 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
4773 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00004774 case OMPC_capture:
4775 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
4776 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004777 case OMPC_seq_cst:
4778 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
4779 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004780 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00004781 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004782 case OMPC_num_threads:
4783 case OMPC_safelen:
4784 case OMPC_collapse:
4785 case OMPC_schedule:
4786 case OMPC_private:
4787 case OMPC_firstprivate:
4788 case OMPC_lastprivate:
4789 case OMPC_shared:
4790 case OMPC_reduction:
4791 case OMPC_linear:
4792 case OMPC_aligned:
4793 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004794 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004795 case OMPC_default:
4796 case OMPC_proc_bind:
4797 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004798 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004799 case OMPC_depend:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004800 case OMPC_unknown:
4801 llvm_unreachable("Clause is not allowed.");
4802 }
4803 return Res;
4804}
4805
4806OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
4807 SourceLocation EndLoc) {
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004808 DSAStack->setOrderedRegion();
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004809 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
4810}
4811
Alexey Bataev236070f2014-06-20 11:19:47 +00004812OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
4813 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00004814 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00004815 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
4816}
4817
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004818OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
4819 SourceLocation EndLoc) {
4820 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
4821}
4822
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004823OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
4824 SourceLocation EndLoc) {
4825 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
4826}
4827
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004828OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
4829 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004830 return new (Context) OMPReadClause(StartLoc, EndLoc);
4831}
4832
Alexey Bataevdea47612014-07-23 07:46:59 +00004833OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
4834 SourceLocation EndLoc) {
4835 return new (Context) OMPWriteClause(StartLoc, EndLoc);
4836}
4837
Alexey Bataev67a4f222014-07-23 10:25:33 +00004838OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
4839 SourceLocation EndLoc) {
4840 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
4841}
4842
Alexey Bataev459dec02014-07-24 06:46:57 +00004843OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
4844 SourceLocation EndLoc) {
4845 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
4846}
4847
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004848OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
4849 SourceLocation EndLoc) {
4850 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
4851}
4852
Alexey Bataevc5e02582014-06-16 07:08:35 +00004853OMPClause *Sema::ActOnOpenMPVarListClause(
4854 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
4855 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
4856 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004857 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
4858 SourceLocation DepLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004859 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004860 switch (Kind) {
4861 case OMPC_private:
4862 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4863 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004864 case OMPC_firstprivate:
4865 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4866 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00004867 case OMPC_lastprivate:
4868 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4869 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00004870 case OMPC_shared:
4871 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
4872 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00004873 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00004874 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
4875 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004876 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00004877 case OMPC_linear:
4878 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
4879 ColonLoc, EndLoc);
4880 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004881 case OMPC_aligned:
4882 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
4883 ColonLoc, EndLoc);
4884 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004885 case OMPC_copyin:
4886 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
4887 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00004888 case OMPC_copyprivate:
4889 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4890 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00004891 case OMPC_flush:
4892 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
4893 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004894 case OMPC_depend:
4895 Res = ActOnOpenMPDependClause(DepKind, DepLoc, ColonLoc, VarList, StartLoc,
4896 LParenLoc, EndLoc);
4897 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004898 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00004899 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00004900 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00004901 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00004902 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004903 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004904 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004905 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004906 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00004907 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004908 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004909 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004910 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004911 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004912 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004913 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004914 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004915 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004916 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004917 llvm_unreachable("Clause is not allowed.");
4918 }
4919 return Res;
4920}
4921
4922OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
4923 SourceLocation StartLoc,
4924 SourceLocation LParenLoc,
4925 SourceLocation EndLoc) {
4926 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00004927 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00004928 for (auto &RefExpr : VarList) {
4929 assert(RefExpr && "NULL expr in OpenMP private clause.");
4930 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004931 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004932 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00004933 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004934 continue;
4935 }
4936
Alexey Bataeved09d242014-05-28 05:53:51 +00004937 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004938 // OpenMP [2.1, C/C++]
4939 // A list item is a variable name.
4940 // OpenMP [2.9.3.3, Restrictions, p.1]
4941 // A variable that is part of another variable (as an array or
4942 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00004943 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004944 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004945 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004946 continue;
4947 }
4948 Decl *D = DE->getDecl();
4949 VarDecl *VD = cast<VarDecl>(D);
4950
4951 QualType Type = VD->getType();
4952 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4953 // It will be analyzed later.
4954 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00004955 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004956 continue;
4957 }
4958
4959 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
4960 // A variable that appears in a private clause must not have an incomplete
4961 // type or a reference type.
4962 if (RequireCompleteType(ELoc, Type,
4963 diag::err_omp_private_incomplete_type)) {
4964 continue;
4965 }
4966 if (Type->isReferenceType()) {
4967 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00004968 << getOpenMPClauseName(OMPC_private) << Type;
4969 bool IsDecl =
4970 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4971 Diag(VD->getLocation(),
4972 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4973 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004974 continue;
4975 }
4976
Alexey Bataev758e55e2013-09-06 18:03:48 +00004977 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4978 // in a Construct]
4979 // Variables with the predetermined data-sharing attributes may not be
4980 // listed in data-sharing attributes clauses, except for the cases
4981 // listed below. For these exceptions only, listing a predetermined
4982 // variable in a data-sharing attribute clause is allowed and overrides
4983 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004984 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004985 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004986 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
4987 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004988 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004989 continue;
4990 }
4991
Alexey Bataevccb59ec2015-05-19 08:44:56 +00004992 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00004993 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00004994 DSAStack->getCurrentDirective() == OMPD_task) {
4995 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
4996 << getOpenMPClauseName(OMPC_private) << Type
4997 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
4998 bool IsDecl =
4999 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5000 Diag(VD->getLocation(),
5001 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5002 << VD;
5003 continue;
5004 }
5005
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005006 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
5007 // A variable of class type (or array thereof) that appears in a private
5008 // clause requires an accessible, unambiguous default constructor for the
5009 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00005010 // Generate helper private variable and initialize it with the default
5011 // value. The address of the original variable is replaced by the address of
5012 // the new private variable in CodeGen. This new variable is not added to
5013 // IdResolver, so the code in the OpenMP region uses original variable for
5014 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005015 Type = Type.getUnqualifiedType();
5016 auto VDPrivate = buildVarDecl(*this, DE->getExprLoc(), Type, VD->getName());
Alexey Bataev39f915b82015-05-08 10:41:21 +00005017 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005018 if (VDPrivate->isInvalidDecl())
5019 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005020 auto VDPrivateRefExpr = buildDeclRefExpr(
5021 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev03b340a2014-10-21 03:16:40 +00005022
Alexey Bataev758e55e2013-09-06 18:03:48 +00005023 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005024 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005025 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005026 }
5027
Alexey Bataeved09d242014-05-28 05:53:51 +00005028 if (Vars.empty())
5029 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005030
Alexey Bataev03b340a2014-10-21 03:16:40 +00005031 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
5032 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005033}
5034
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005035namespace {
5036class DiagsUninitializedSeveretyRAII {
5037private:
5038 DiagnosticsEngine &Diags;
5039 SourceLocation SavedLoc;
5040 bool IsIgnored;
5041
5042public:
5043 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
5044 bool IsIgnored)
5045 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
5046 if (!IsIgnored) {
5047 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
5048 /*Map*/ diag::Severity::Ignored, Loc);
5049 }
5050 }
5051 ~DiagsUninitializedSeveretyRAII() {
5052 if (!IsIgnored)
5053 Diags.popMappings(SavedLoc);
5054 }
5055};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00005056}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005057
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005058OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
5059 SourceLocation StartLoc,
5060 SourceLocation LParenLoc,
5061 SourceLocation EndLoc) {
5062 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005063 SmallVector<Expr *, 8> PrivateCopies;
5064 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005065 bool IsImplicitClause =
5066 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
5067 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
5068
Alexey Bataeved09d242014-05-28 05:53:51 +00005069 for (auto &RefExpr : VarList) {
5070 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
5071 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005072 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005073 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005074 PrivateCopies.push_back(nullptr);
5075 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005076 continue;
5077 }
5078
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005079 SourceLocation ELoc =
5080 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005081 // OpenMP [2.1, C/C++]
5082 // A list item is a variable name.
5083 // OpenMP [2.9.3.3, Restrictions, p.1]
5084 // A variable that is part of another variable (as an array or
5085 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00005086 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005087 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005088 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005089 continue;
5090 }
5091 Decl *D = DE->getDecl();
5092 VarDecl *VD = cast<VarDecl>(D);
5093
5094 QualType Type = VD->getType();
5095 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5096 // It will be analyzed later.
5097 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005098 PrivateCopies.push_back(nullptr);
5099 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005100 continue;
5101 }
5102
5103 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5104 // A variable that appears in a private clause must not have an incomplete
5105 // type or a reference type.
5106 if (RequireCompleteType(ELoc, Type,
5107 diag::err_omp_firstprivate_incomplete_type)) {
5108 continue;
5109 }
5110 if (Type->isReferenceType()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005111 if (IsImplicitClause) {
5112 Diag(ImplicitClauseLoc,
5113 diag::err_omp_task_predetermined_firstprivate_ref_type_arg)
5114 << Type;
5115 Diag(RefExpr->getExprLoc(), diag::note_used_here);
5116 } else {
5117 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
5118 << getOpenMPClauseName(OMPC_firstprivate) << Type;
5119 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005120 bool IsDecl =
5121 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5122 Diag(VD->getLocation(),
5123 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5124 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005125 continue;
5126 }
5127
5128 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
5129 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00005130 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005131 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005132 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005133
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005134 // If an implicit firstprivate variable found it was checked already.
5135 if (!IsImplicitClause) {
5136 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005137 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005138 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
5139 // A list item that specifies a given variable may not appear in more
5140 // than one clause on the same directive, except that a variable may be
5141 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005142 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00005143 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005144 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00005145 << getOpenMPClauseName(DVar.CKind)
5146 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005147 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005148 continue;
5149 }
5150
5151 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5152 // in a Construct]
5153 // Variables with the predetermined data-sharing attributes may not be
5154 // listed in data-sharing attributes clauses, except for the cases
5155 // listed below. For these exceptions only, listing a predetermined
5156 // variable in a data-sharing attribute clause is allowed and overrides
5157 // the variable's predetermined data-sharing attributes.
5158 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5159 // in a Construct, C/C++, p.2]
5160 // Variables with const-qualified type having no mutable member may be
5161 // listed in a firstprivate clause, even if they are static data members.
5162 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
5163 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
5164 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00005165 << getOpenMPClauseName(DVar.CKind)
5166 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005167 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005168 continue;
5169 }
5170
Alexey Bataevf29276e2014-06-18 04:14:57 +00005171 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005172 // OpenMP [2.9.3.4, Restrictions, p.2]
5173 // A list item that is private within a parallel region must not appear
5174 // in a firstprivate clause on a worksharing construct if any of the
5175 // worksharing regions arising from the worksharing construct ever bind
5176 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00005177 if (isOpenMPWorksharingDirective(CurrDir) &&
5178 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005179 DVar = DSAStack->getImplicitDSA(VD, true);
5180 if (DVar.CKind != OMPC_shared &&
5181 (isOpenMPParallelDirective(DVar.DKind) ||
5182 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00005183 Diag(ELoc, diag::err_omp_required_access)
5184 << getOpenMPClauseName(OMPC_firstprivate)
5185 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005186 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005187 continue;
5188 }
5189 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005190 // OpenMP [2.9.3.4, Restrictions, p.3]
5191 // A list item that appears in a reduction clause of a parallel construct
5192 // must not appear in a firstprivate clause on a worksharing or task
5193 // construct if any of the worksharing or task regions arising from the
5194 // worksharing or task construct ever bind to any of the parallel regions
5195 // arising from the parallel construct.
5196 // OpenMP [2.9.3.4, Restrictions, p.4]
5197 // A list item that appears in a reduction clause in worksharing
5198 // construct must not appear in a firstprivate clause in a task construct
5199 // encountered during execution of any of the worksharing regions arising
5200 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005201 if (CurrDir == OMPD_task) {
5202 DVar =
5203 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
5204 [](OpenMPDirectiveKind K) -> bool {
5205 return isOpenMPParallelDirective(K) ||
5206 isOpenMPWorksharingDirective(K);
5207 },
5208 false);
5209 if (DVar.CKind == OMPC_reduction &&
5210 (isOpenMPParallelDirective(DVar.DKind) ||
5211 isOpenMPWorksharingDirective(DVar.DKind))) {
5212 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
5213 << getOpenMPDirectiveName(DVar.DKind);
5214 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5215 continue;
5216 }
5217 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005218 }
5219
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005220 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00005221 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005222 DSAStack->getCurrentDirective() == OMPD_task) {
5223 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
5224 << getOpenMPClauseName(OMPC_firstprivate) << Type
5225 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
5226 bool IsDecl =
5227 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5228 Diag(VD->getLocation(),
5229 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5230 << VD;
5231 continue;
5232 }
5233
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005234 Type = Type.getUnqualifiedType();
5235 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName());
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005236 // Generate helper private variable and initialize it with the value of the
5237 // original variable. The address of the original variable is replaced by
5238 // the address of the new private variable in the CodeGen. This new variable
5239 // is not added to IdResolver, so the code in the OpenMP region uses
5240 // original variable for proper diagnostics and variable capturing.
5241 Expr *VDInitRefExpr = nullptr;
5242 // For arrays generate initializer for single element and replace it by the
5243 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005244 if (Type->isArrayType()) {
5245 auto VDInit =
5246 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
5247 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005248 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005249 ElemType = ElemType.getUnqualifiedType();
5250 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
5251 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00005252 InitializedEntity Entity =
5253 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005254 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
5255
5256 InitializationSequence InitSeq(*this, Entity, Kind, Init);
5257 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
5258 if (Result.isInvalid())
5259 VDPrivate->setInvalidDecl();
5260 else
5261 VDPrivate->setInit(Result.getAs<Expr>());
5262 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00005263 auto *VDInit =
Alexey Bataev39f915b82015-05-08 10:41:21 +00005264 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005265 VDInitRefExpr =
5266 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00005267 AddInitializerToDecl(VDPrivate,
5268 DefaultLvalueConversion(VDInitRefExpr).get(),
5269 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005270 }
5271 if (VDPrivate->isInvalidDecl()) {
5272 if (IsImplicitClause) {
5273 Diag(DE->getExprLoc(),
5274 diag::note_omp_task_predetermined_firstprivate_here);
5275 }
5276 continue;
5277 }
5278 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005279 auto VDPrivateRefExpr = buildDeclRefExpr(
5280 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005281 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
5282 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005283 PrivateCopies.push_back(VDPrivateRefExpr);
5284 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005285 }
5286
Alexey Bataeved09d242014-05-28 05:53:51 +00005287 if (Vars.empty())
5288 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005289
5290 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005291 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005292}
5293
Alexander Musman1bb328c2014-06-04 13:06:39 +00005294OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
5295 SourceLocation StartLoc,
5296 SourceLocation LParenLoc,
5297 SourceLocation EndLoc) {
5298 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00005299 SmallVector<Expr *, 8> SrcExprs;
5300 SmallVector<Expr *, 8> DstExprs;
5301 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00005302 for (auto &RefExpr : VarList) {
5303 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
5304 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5305 // It will be analyzed later.
5306 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00005307 SrcExprs.push_back(nullptr);
5308 DstExprs.push_back(nullptr);
5309 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005310 continue;
5311 }
5312
5313 SourceLocation ELoc = RefExpr->getExprLoc();
5314 // OpenMP [2.1, C/C++]
5315 // A list item is a variable name.
5316 // OpenMP [2.14.3.5, Restrictions, p.1]
5317 // A variable that is part of another variable (as an array or structure
5318 // element) cannot appear in a lastprivate clause.
5319 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
5320 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5321 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5322 continue;
5323 }
5324 Decl *D = DE->getDecl();
5325 VarDecl *VD = cast<VarDecl>(D);
5326
5327 QualType Type = VD->getType();
5328 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5329 // It will be analyzed later.
5330 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00005331 SrcExprs.push_back(nullptr);
5332 DstExprs.push_back(nullptr);
5333 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005334 continue;
5335 }
5336
5337 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
5338 // A variable that appears in a lastprivate clause must not have an
5339 // incomplete type or a reference type.
5340 if (RequireCompleteType(ELoc, Type,
5341 diag::err_omp_lastprivate_incomplete_type)) {
5342 continue;
5343 }
5344 if (Type->isReferenceType()) {
5345 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
5346 << getOpenMPClauseName(OMPC_lastprivate) << Type;
5347 bool IsDecl =
5348 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5349 Diag(VD->getLocation(),
5350 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5351 << VD;
5352 continue;
5353 }
5354
5355 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
5356 // in a Construct]
5357 // Variables with the predetermined data-sharing attributes may not be
5358 // listed in data-sharing attributes clauses, except for the cases
5359 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005360 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005361 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
5362 DVar.CKind != OMPC_firstprivate &&
5363 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
5364 Diag(ELoc, diag::err_omp_wrong_dsa)
5365 << getOpenMPClauseName(DVar.CKind)
5366 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005367 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005368 continue;
5369 }
5370
Alexey Bataevf29276e2014-06-18 04:14:57 +00005371 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
5372 // OpenMP [2.14.3.5, Restrictions, p.2]
5373 // A list item that is private within a parallel region, or that appears in
5374 // the reduction clause of a parallel construct, must not appear in a
5375 // lastprivate clause on a worksharing construct if any of the corresponding
5376 // worksharing regions ever binds to any of the corresponding parallel
5377 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005378 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00005379 if (isOpenMPWorksharingDirective(CurrDir) &&
5380 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005381 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005382 if (DVar.CKind != OMPC_shared) {
5383 Diag(ELoc, diag::err_omp_required_access)
5384 << getOpenMPClauseName(OMPC_lastprivate)
5385 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005386 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005387 continue;
5388 }
5389 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00005390 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00005391 // A variable of class type (or array thereof) that appears in a
5392 // lastprivate clause requires an accessible, unambiguous default
5393 // constructor for the class type, unless the list item is also specified
5394 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00005395 // A variable of class type (or array thereof) that appears in a
5396 // lastprivate clause requires an accessible, unambiguous copy assignment
5397 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00005398 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00005399 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev38e89532015-04-16 04:54:05 +00005400 Type.getUnqualifiedType(), ".lastprivate.src");
Alexey Bataev39f915b82015-05-08 10:41:21 +00005401 auto *PseudoSrcExpr = buildDeclRefExpr(
5402 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00005403 auto *DstVD =
Alexey Bataev39f915b82015-05-08 10:41:21 +00005404 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst");
Alexey Bataev38e89532015-04-16 04:54:05 +00005405 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00005406 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00005407 // For arrays generate assignment operation for single element and replace
5408 // it by the original array element in CodeGen.
5409 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
5410 PseudoDstExpr, PseudoSrcExpr);
5411 if (AssignmentOp.isInvalid())
5412 continue;
5413 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
5414 /*DiscardedValue=*/true);
5415 if (AssignmentOp.isInvalid())
5416 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00005417
Alexey Bataev39f915b82015-05-08 10:41:21 +00005418 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005419 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005420 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00005421 SrcExprs.push_back(PseudoSrcExpr);
5422 DstExprs.push_back(PseudoDstExpr);
5423 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00005424 }
5425
5426 if (Vars.empty())
5427 return nullptr;
5428
5429 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00005430 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005431}
5432
Alexey Bataev758e55e2013-09-06 18:03:48 +00005433OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
5434 SourceLocation StartLoc,
5435 SourceLocation LParenLoc,
5436 SourceLocation EndLoc) {
5437 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00005438 for (auto &RefExpr : VarList) {
5439 assert(RefExpr && "NULL expr in OpenMP shared clause.");
5440 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00005441 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005442 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005443 continue;
5444 }
5445
Alexey Bataeved09d242014-05-28 05:53:51 +00005446 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00005447 // OpenMP [2.1, C/C++]
5448 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00005449 // OpenMP [2.14.3.2, Restrictions, p.1]
5450 // A variable that is part of another variable (as an array or structure
5451 // element) cannot appear in a shared unless it is a static data member
5452 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00005453 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005454 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005455 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00005456 continue;
5457 }
5458 Decl *D = DE->getDecl();
5459 VarDecl *VD = cast<VarDecl>(D);
5460
5461 QualType Type = VD->getType();
5462 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5463 // It will be analyzed later.
5464 Vars.push_back(DE);
5465 continue;
5466 }
5467
5468 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5469 // in a Construct]
5470 // Variables with the predetermined data-sharing attributes may not be
5471 // listed in data-sharing attributes clauses, except for the cases
5472 // listed below. For these exceptions only, listing a predetermined
5473 // variable in a data-sharing attribute clause is allowed and overrides
5474 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005475 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00005476 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
5477 DVar.RefExpr) {
5478 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
5479 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005480 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005481 continue;
5482 }
5483
5484 DSAStack->addDSA(VD, DE, OMPC_shared);
5485 Vars.push_back(DE);
5486 }
5487
Alexey Bataeved09d242014-05-28 05:53:51 +00005488 if (Vars.empty())
5489 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00005490
5491 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
5492}
5493
Alexey Bataevc5e02582014-06-16 07:08:35 +00005494namespace {
5495class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
5496 DSAStackTy *Stack;
5497
5498public:
5499 bool VisitDeclRefExpr(DeclRefExpr *E) {
5500 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005501 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005502 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
5503 return false;
5504 if (DVar.CKind != OMPC_unknown)
5505 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00005506 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005507 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005508 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00005509 return true;
5510 return false;
5511 }
5512 return false;
5513 }
5514 bool VisitStmt(Stmt *S) {
5515 for (auto Child : S->children()) {
5516 if (Child && Visit(Child))
5517 return true;
5518 }
5519 return false;
5520 }
Alexey Bataev23b69422014-06-18 07:08:49 +00005521 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00005522};
Alexey Bataev23b69422014-06-18 07:08:49 +00005523} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00005524
5525OMPClause *Sema::ActOnOpenMPReductionClause(
5526 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
5527 SourceLocation ColonLoc, SourceLocation EndLoc,
5528 CXXScopeSpec &ReductionIdScopeSpec,
5529 const DeclarationNameInfo &ReductionId) {
5530 // TODO: Allow scope specification search when 'declare reduction' is
5531 // supported.
5532 assert(ReductionIdScopeSpec.isEmpty() &&
5533 "No support for scoped reduction identifiers yet.");
5534
5535 auto DN = ReductionId.getName();
5536 auto OOK = DN.getCXXOverloadedOperator();
5537 BinaryOperatorKind BOK = BO_Comma;
5538
5539 // OpenMP [2.14.3.6, reduction clause]
5540 // C
5541 // reduction-identifier is either an identifier or one of the following
5542 // operators: +, -, *, &, |, ^, && and ||
5543 // C++
5544 // reduction-identifier is either an id-expression or one of the following
5545 // operators: +, -, *, &, |, ^, && and ||
5546 // FIXME: Only 'min' and 'max' identifiers are supported for now.
5547 switch (OOK) {
5548 case OO_Plus:
5549 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005550 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005551 break;
5552 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005553 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005554 break;
5555 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005556 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005557 break;
5558 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005559 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005560 break;
5561 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005562 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005563 break;
5564 case OO_AmpAmp:
5565 BOK = BO_LAnd;
5566 break;
5567 case OO_PipePipe:
5568 BOK = BO_LOr;
5569 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005570 case OO_New:
5571 case OO_Delete:
5572 case OO_Array_New:
5573 case OO_Array_Delete:
5574 case OO_Slash:
5575 case OO_Percent:
5576 case OO_Tilde:
5577 case OO_Exclaim:
5578 case OO_Equal:
5579 case OO_Less:
5580 case OO_Greater:
5581 case OO_LessEqual:
5582 case OO_GreaterEqual:
5583 case OO_PlusEqual:
5584 case OO_MinusEqual:
5585 case OO_StarEqual:
5586 case OO_SlashEqual:
5587 case OO_PercentEqual:
5588 case OO_CaretEqual:
5589 case OO_AmpEqual:
5590 case OO_PipeEqual:
5591 case OO_LessLess:
5592 case OO_GreaterGreater:
5593 case OO_LessLessEqual:
5594 case OO_GreaterGreaterEqual:
5595 case OO_EqualEqual:
5596 case OO_ExclaimEqual:
5597 case OO_PlusPlus:
5598 case OO_MinusMinus:
5599 case OO_Comma:
5600 case OO_ArrowStar:
5601 case OO_Arrow:
5602 case OO_Call:
5603 case OO_Subscript:
5604 case OO_Conditional:
5605 case NUM_OVERLOADED_OPERATORS:
5606 llvm_unreachable("Unexpected reduction identifier");
5607 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005608 if (auto II = DN.getAsIdentifierInfo()) {
5609 if (II->isStr("max"))
5610 BOK = BO_GT;
5611 else if (II->isStr("min"))
5612 BOK = BO_LT;
5613 }
5614 break;
5615 }
5616 SourceRange ReductionIdRange;
5617 if (ReductionIdScopeSpec.isValid()) {
5618 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
5619 }
5620 ReductionIdRange.setEnd(ReductionId.getEndLoc());
5621 if (BOK == BO_Comma) {
5622 // Not allowed reduction identifier is found.
5623 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
5624 << ReductionIdRange;
5625 return nullptr;
5626 }
5627
5628 SmallVector<Expr *, 8> Vars;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005629 SmallVector<Expr *, 8> LHSs;
5630 SmallVector<Expr *, 8> RHSs;
5631 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005632 for (auto RefExpr : VarList) {
5633 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
5634 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5635 // It will be analyzed later.
5636 Vars.push_back(RefExpr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005637 LHSs.push_back(nullptr);
5638 RHSs.push_back(nullptr);
5639 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005640 continue;
5641 }
5642
5643 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
5644 RefExpr->isInstantiationDependent() ||
5645 RefExpr->containsUnexpandedParameterPack()) {
5646 // It will be analyzed later.
5647 Vars.push_back(RefExpr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005648 LHSs.push_back(nullptr);
5649 RHSs.push_back(nullptr);
5650 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005651 continue;
5652 }
5653
5654 auto ELoc = RefExpr->getExprLoc();
5655 auto ERange = RefExpr->getSourceRange();
5656 // OpenMP [2.1, C/C++]
5657 // A list item is a variable or array section, subject to the restrictions
5658 // specified in Section 2.4 on page 42 and in each of the sections
5659 // describing clauses and directives for which a list appears.
5660 // OpenMP [2.14.3.3, Restrictions, p.1]
5661 // A variable that is part of another variable (as an array or
5662 // structure element) cannot appear in a private clause.
5663 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
5664 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5665 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
5666 continue;
5667 }
5668 auto D = DE->getDecl();
5669 auto VD = cast<VarDecl>(D);
5670 auto Type = VD->getType();
5671 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5672 // A variable that appears in a private clause must not have an incomplete
5673 // type or a reference type.
5674 if (RequireCompleteType(ELoc, Type,
5675 diag::err_omp_reduction_incomplete_type))
5676 continue;
5677 // OpenMP [2.14.3.6, reduction clause, Restrictions]
5678 // Arrays may not appear in a reduction clause.
5679 if (Type.getNonReferenceType()->isArrayType()) {
5680 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
5681 bool IsDecl =
5682 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5683 Diag(VD->getLocation(),
5684 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5685 << VD;
5686 continue;
5687 }
5688 // OpenMP [2.14.3.6, reduction clause, Restrictions]
5689 // A list item that appears in a reduction clause must not be
5690 // const-qualified.
5691 if (Type.getNonReferenceType().isConstant(Context)) {
5692 Diag(ELoc, diag::err_omp_const_variable)
5693 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
5694 bool IsDecl =
5695 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5696 Diag(VD->getLocation(),
5697 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5698 << VD;
5699 continue;
5700 }
5701 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
5702 // If a list-item is a reference type then it must bind to the same object
5703 // for all threads of the team.
5704 VarDecl *VDDef = VD->getDefinition();
5705 if (Type->isReferenceType() && VDDef) {
5706 DSARefChecker Check(DSAStack);
5707 if (Check.Visit(VDDef->getInit())) {
5708 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
5709 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
5710 continue;
5711 }
5712 }
5713 // OpenMP [2.14.3.6, reduction clause, Restrictions]
5714 // The type of a list item that appears in a reduction clause must be valid
5715 // for the reduction-identifier. For a max or min reduction in C, the type
5716 // of the list item must be an allowed arithmetic data type: char, int,
5717 // float, double, or _Bool, possibly modified with long, short, signed, or
5718 // unsigned. For a max or min reduction in C++, the type of the list item
5719 // must be an allowed arithmetic data type: char, wchar_t, int, float,
5720 // double, or bool, possibly modified with long, short, signed, or unsigned.
5721 if ((BOK == BO_GT || BOK == BO_LT) &&
5722 !(Type->isScalarType() ||
5723 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
5724 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
5725 << getLangOpts().CPlusPlus;
5726 bool IsDecl =
5727 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5728 Diag(VD->getLocation(),
5729 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5730 << VD;
5731 continue;
5732 }
5733 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
5734 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
5735 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
5736 bool IsDecl =
5737 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5738 Diag(VD->getLocation(),
5739 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5740 << VD;
5741 continue;
5742 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00005743 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
5744 // in a Construct]
5745 // Variables with the predetermined data-sharing attributes may not be
5746 // listed in data-sharing attributes clauses, except for the cases
5747 // listed below. For these exceptions only, listing a predetermined
5748 // variable in a data-sharing attribute clause is allowed and overrides
5749 // the variable's predetermined data-sharing attributes.
5750 // OpenMP [2.14.3.6, Restrictions, p.3]
5751 // Any number of reduction clauses can be specified on the directive,
5752 // but a list item can appear only once in the reduction clauses for that
5753 // directive.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005754 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005755 if (DVar.CKind == OMPC_reduction) {
5756 Diag(ELoc, diag::err_omp_once_referenced)
5757 << getOpenMPClauseName(OMPC_reduction);
5758 if (DVar.RefExpr) {
5759 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
5760 }
5761 } else if (DVar.CKind != OMPC_unknown) {
5762 Diag(ELoc, diag::err_omp_wrong_dsa)
5763 << getOpenMPClauseName(DVar.CKind)
5764 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005765 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005766 continue;
5767 }
5768
5769 // OpenMP [2.14.3.6, Restrictions, p.1]
5770 // A list item that appears in a reduction clause of a worksharing
5771 // construct must be shared in the parallel regions to which any of the
5772 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00005773 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00005774 if (isOpenMPWorksharingDirective(CurrDir) &&
5775 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005776 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005777 if (DVar.CKind != OMPC_shared) {
5778 Diag(ELoc, diag::err_omp_required_access)
5779 << getOpenMPClauseName(OMPC_reduction)
5780 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005781 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005782 continue;
5783 }
5784 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005785 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00005786 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs");
5787 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005788 // Add initializer for private variable.
5789 Expr *Init = nullptr;
5790 switch (BOK) {
5791 case BO_Add:
5792 case BO_Xor:
5793 case BO_Or:
5794 case BO_LOr:
5795 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
5796 if (Type->isScalarType() || Type->isAnyComplexType()) {
5797 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00005798 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005799 break;
5800 case BO_Mul:
5801 case BO_LAnd:
5802 if (Type->isScalarType() || Type->isAnyComplexType()) {
5803 // '*' and '&&' reduction ops - initializer is '1'.
5804 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
5805 }
5806 break;
5807 case BO_And: {
5808 // '&' reduction op - initializer is '~0'.
5809 QualType OrigType = Type;
5810 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
5811 Type = ComplexTy->getElementType();
5812 }
5813 if (Type->isRealFloatingType()) {
5814 llvm::APFloat InitValue =
5815 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
5816 /*isIEEE=*/true);
5817 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
5818 Type, ELoc);
5819 } else if (Type->isScalarType()) {
5820 auto Size = Context.getTypeSize(Type);
5821 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
5822 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
5823 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
5824 }
5825 if (Init && OrigType->isAnyComplexType()) {
5826 // Init = 0xFFFF + 0xFFFFi;
5827 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
5828 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
5829 }
5830 Type = OrigType;
5831 break;
5832 }
5833 case BO_LT:
5834 case BO_GT: {
5835 // 'min' reduction op - initializer is 'Largest representable number in
5836 // the reduction list item type'.
5837 // 'max' reduction op - initializer is 'Least representable number in
5838 // the reduction list item type'.
5839 if (Type->isIntegerType() || Type->isPointerType()) {
5840 bool IsSigned = Type->hasSignedIntegerRepresentation();
5841 auto Size = Context.getTypeSize(Type);
5842 QualType IntTy =
5843 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
5844 llvm::APInt InitValue =
5845 (BOK != BO_LT)
5846 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
5847 : llvm::APInt::getMinValue(Size)
5848 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
5849 : llvm::APInt::getMaxValue(Size);
5850 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
5851 if (Type->isPointerType()) {
5852 // Cast to pointer type.
5853 auto CastExpr = BuildCStyleCastExpr(
5854 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
5855 SourceLocation(), Init);
5856 if (CastExpr.isInvalid())
5857 continue;
5858 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00005859 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005860 } else if (Type->isRealFloatingType()) {
5861 llvm::APFloat InitValue = llvm::APFloat::getLargest(
5862 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
5863 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
5864 Type, ELoc);
5865 }
5866 break;
5867 }
5868 case BO_PtrMemD:
5869 case BO_PtrMemI:
5870 case BO_MulAssign:
5871 case BO_Div:
5872 case BO_Rem:
5873 case BO_Sub:
5874 case BO_Shl:
5875 case BO_Shr:
5876 case BO_LE:
5877 case BO_GE:
5878 case BO_EQ:
5879 case BO_NE:
5880 case BO_AndAssign:
5881 case BO_XorAssign:
5882 case BO_OrAssign:
5883 case BO_Assign:
5884 case BO_AddAssign:
5885 case BO_SubAssign:
5886 case BO_DivAssign:
5887 case BO_RemAssign:
5888 case BO_ShlAssign:
5889 case BO_ShrAssign:
5890 case BO_Comma:
5891 llvm_unreachable("Unexpected reduction operation");
5892 }
5893 if (Init) {
5894 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
5895 /*TypeMayContainAuto=*/false);
5896 } else {
5897 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
5898 }
5899 if (!RHSVD->hasInit()) {
5900 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
5901 << ReductionIdRange;
5902 bool IsDecl =
5903 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5904 Diag(VD->getLocation(),
5905 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5906 << VD;
5907 continue;
5908 }
Alexey Bataev39f915b82015-05-08 10:41:21 +00005909 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
5910 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005911 ExprResult ReductionOp =
5912 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
5913 LHSDRE, RHSDRE);
5914 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00005915 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005916 ReductionOp =
5917 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
5918 BO_Assign, LHSDRE, ReductionOp.get());
5919 } else {
5920 auto *ConditionalOp = new (Context) ConditionalOperator(
5921 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
5922 RHSDRE, Type, VK_LValue, OK_Ordinary);
5923 ReductionOp =
5924 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
5925 BO_Assign, LHSDRE, ConditionalOp);
5926 }
5927 if (ReductionOp.isUsable()) {
5928 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00005929 }
5930 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005931 if (ReductionOp.isInvalid())
5932 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005933
5934 DSAStack->addDSA(VD, DE, OMPC_reduction);
5935 Vars.push_back(DE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005936 LHSs.push_back(LHSDRE);
5937 RHSs.push_back(RHSDRE);
5938 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00005939 }
5940
5941 if (Vars.empty())
5942 return nullptr;
5943
5944 return OMPReductionClause::Create(
5945 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005946 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, LHSs,
5947 RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005948}
5949
Alexander Musman8dba6642014-04-22 13:09:42 +00005950OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
5951 SourceLocation StartLoc,
5952 SourceLocation LParenLoc,
5953 SourceLocation ColonLoc,
5954 SourceLocation EndLoc) {
5955 SmallVector<Expr *, 8> Vars;
Alexander Musman3276a272015-03-21 10:12:56 +00005956 SmallVector<Expr *, 8> Inits;
Alexey Bataeved09d242014-05-28 05:53:51 +00005957 for (auto &RefExpr : VarList) {
5958 assert(RefExpr && "NULL expr in OpenMP linear clause.");
5959 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00005960 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005961 Vars.push_back(RefExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00005962 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00005963 continue;
5964 }
5965
5966 // OpenMP [2.14.3.7, linear clause]
5967 // A list item that appears in a linear clause is subject to the private
5968 // clause semantics described in Section 2.14.3.3 on page 159 except as
5969 // noted. In addition, the value of the new list item on each iteration
5970 // of the associated loop(s) corresponds to the value of the original
5971 // list item before entering the construct plus the logical number of
5972 // the iteration times linear-step.
5973
Alexey Bataeved09d242014-05-28 05:53:51 +00005974 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00005975 // OpenMP [2.1, C/C++]
5976 // A list item is a variable name.
5977 // OpenMP [2.14.3.3, Restrictions, p.1]
5978 // A variable that is part of another variable (as an array or
5979 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00005980 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00005981 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005982 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00005983 continue;
5984 }
5985
5986 VarDecl *VD = cast<VarDecl>(DE->getDecl());
5987
5988 // OpenMP [2.14.3.7, linear clause]
5989 // A list-item cannot appear in more than one linear clause.
5990 // A list-item that appears in a linear clause cannot appear in any
5991 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005992 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00005993 if (DVar.RefExpr) {
5994 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
5995 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005996 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00005997 continue;
5998 }
5999
6000 QualType QType = VD->getType();
6001 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
6002 // It will be analyzed later.
6003 Vars.push_back(DE);
Alexander Musman3276a272015-03-21 10:12:56 +00006004 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006005 continue;
6006 }
6007
6008 // A variable must not have an incomplete type or a reference type.
6009 if (RequireCompleteType(ELoc, QType,
6010 diag::err_omp_linear_incomplete_type)) {
6011 continue;
6012 }
6013 if (QType->isReferenceType()) {
6014 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
6015 << getOpenMPClauseName(OMPC_linear) << QType;
6016 bool IsDecl =
6017 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6018 Diag(VD->getLocation(),
6019 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6020 << VD;
6021 continue;
6022 }
6023
6024 // A list item must not be const-qualified.
6025 if (QType.isConstant(Context)) {
6026 Diag(ELoc, diag::err_omp_const_variable)
6027 << getOpenMPClauseName(OMPC_linear);
6028 bool IsDecl =
6029 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6030 Diag(VD->getLocation(),
6031 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6032 << VD;
6033 continue;
6034 }
6035
6036 // A list item must be of integral or pointer type.
6037 QType = QType.getUnqualifiedType().getCanonicalType();
6038 const Type *Ty = QType.getTypePtrOrNull();
6039 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
6040 !Ty->isPointerType())) {
6041 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
6042 bool IsDecl =
6043 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6044 Diag(VD->getLocation(),
6045 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6046 << VD;
6047 continue;
6048 }
6049
Alexander Musman3276a272015-03-21 10:12:56 +00006050 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006051 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexander Musman3276a272015-03-21 10:12:56 +00006052 AddInitializerToDecl(Init, DefaultLvalueConversion(DE).get(),
6053 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006054 auto InitRef = buildDeclRefExpr(
6055 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00006056 DSAStack->addDSA(VD, DE, OMPC_linear);
6057 Vars.push_back(DE);
Alexander Musman3276a272015-03-21 10:12:56 +00006058 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00006059 }
6060
6061 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006062 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00006063
6064 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00006065 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00006066 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
6067 !Step->isInstantiationDependent() &&
6068 !Step->containsUnexpandedParameterPack()) {
6069 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006070 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00006071 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006072 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006073 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00006074
Alexander Musman3276a272015-03-21 10:12:56 +00006075 // Build var to save the step value.
6076 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006077 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00006078 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006079 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00006080 ExprResult CalcStep =
6081 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
6082
Alexander Musman8dba6642014-04-22 13:09:42 +00006083 // Warn about zero linear step (it would be probably better specified as
6084 // making corresponding variables 'const').
6085 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00006086 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
6087 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00006088 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
6089 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00006090 if (!IsConstant && CalcStep.isUsable()) {
6091 // Calculate the step beforehand instead of doing this on each iteration.
6092 // (This is not used if the number of iterations may be kfold-ed).
6093 CalcStepExpr = CalcStep.get();
6094 }
Alexander Musman8dba6642014-04-22 13:09:42 +00006095 }
6096
6097 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
Alexander Musman3276a272015-03-21 10:12:56 +00006098 Vars, Inits, StepExpr, CalcStepExpr);
6099}
6100
6101static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
6102 Expr *NumIterations, Sema &SemaRef,
6103 Scope *S) {
6104 // Walk the vars and build update/final expressions for the CodeGen.
6105 SmallVector<Expr *, 8> Updates;
6106 SmallVector<Expr *, 8> Finals;
6107 Expr *Step = Clause.getStep();
6108 Expr *CalcStep = Clause.getCalcStep();
6109 // OpenMP [2.14.3.7, linear clause]
6110 // If linear-step is not specified it is assumed to be 1.
6111 if (Step == nullptr)
6112 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
6113 else if (CalcStep)
6114 Step = cast<BinaryOperator>(CalcStep)->getLHS();
6115 bool HasErrors = false;
6116 auto CurInit = Clause.inits().begin();
6117 for (auto &RefExpr : Clause.varlists()) {
6118 Expr *InitExpr = *CurInit;
6119
6120 // Build privatized reference to the current linear var.
6121 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006122 auto PrivateRef =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006123 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
6124 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
6125 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00006126
6127 // Build update: Var = InitExpr + IV * Step
6128 ExprResult Update =
6129 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), PrivateRef,
6130 InitExpr, IV, Step, /* Subtract */ false);
6131 Update = SemaRef.ActOnFinishFullExpr(Update.get());
6132
6133 // Build final: Var = InitExpr + NumIterations * Step
6134 ExprResult Final =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006135 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), PrivateRef,
6136 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexander Musman3276a272015-03-21 10:12:56 +00006137 Final = SemaRef.ActOnFinishFullExpr(Final.get());
6138 if (!Update.isUsable() || !Final.isUsable()) {
6139 Updates.push_back(nullptr);
6140 Finals.push_back(nullptr);
6141 HasErrors = true;
6142 } else {
6143 Updates.push_back(Update.get());
6144 Finals.push_back(Final.get());
6145 }
6146 ++CurInit;
6147 }
6148 Clause.setUpdates(Updates);
6149 Clause.setFinals(Finals);
6150 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00006151}
6152
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006153OMPClause *Sema::ActOnOpenMPAlignedClause(
6154 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
6155 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
6156
6157 SmallVector<Expr *, 8> Vars;
6158 for (auto &RefExpr : VarList) {
6159 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
6160 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6161 // It will be analyzed later.
6162 Vars.push_back(RefExpr);
6163 continue;
6164 }
6165
6166 SourceLocation ELoc = RefExpr->getExprLoc();
6167 // OpenMP [2.1, C/C++]
6168 // A list item is a variable name.
6169 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
6170 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6171 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6172 continue;
6173 }
6174
6175 VarDecl *VD = cast<VarDecl>(DE->getDecl());
6176
6177 // OpenMP [2.8.1, simd construct, Restrictions]
6178 // The type of list items appearing in the aligned clause must be
6179 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006180 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006181 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006182 const Type *Ty = QType.getTypePtrOrNull();
6183 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
6184 !Ty->isPointerType())) {
6185 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
6186 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
6187 bool IsDecl =
6188 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6189 Diag(VD->getLocation(),
6190 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6191 << VD;
6192 continue;
6193 }
6194
6195 // OpenMP [2.8.1, simd construct, Restrictions]
6196 // A list-item cannot appear in more than one aligned clause.
6197 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
6198 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
6199 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
6200 << getOpenMPClauseName(OMPC_aligned);
6201 continue;
6202 }
6203
6204 Vars.push_back(DE);
6205 }
6206
6207 // OpenMP [2.8.1, simd construct, Description]
6208 // The parameter of the aligned clause, alignment, must be a constant
6209 // positive integer expression.
6210 // If no optional parameter is specified, implementation-defined default
6211 // alignments for SIMD instructions on the target platforms are assumed.
6212 if (Alignment != nullptr) {
6213 ExprResult AlignResult =
6214 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
6215 if (AlignResult.isInvalid())
6216 return nullptr;
6217 Alignment = AlignResult.get();
6218 }
6219 if (Vars.empty())
6220 return nullptr;
6221
6222 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
6223 EndLoc, Vars, Alignment);
6224}
6225
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006226OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
6227 SourceLocation StartLoc,
6228 SourceLocation LParenLoc,
6229 SourceLocation EndLoc) {
6230 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006231 SmallVector<Expr *, 8> SrcExprs;
6232 SmallVector<Expr *, 8> DstExprs;
6233 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00006234 for (auto &RefExpr : VarList) {
6235 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
6236 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006237 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006238 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006239 SrcExprs.push_back(nullptr);
6240 DstExprs.push_back(nullptr);
6241 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006242 continue;
6243 }
6244
Alexey Bataeved09d242014-05-28 05:53:51 +00006245 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006246 // OpenMP [2.1, C/C++]
6247 // A list item is a variable name.
6248 // OpenMP [2.14.4.1, Restrictions, p.1]
6249 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00006250 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006251 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006252 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006253 continue;
6254 }
6255
6256 Decl *D = DE->getDecl();
6257 VarDecl *VD = cast<VarDecl>(D);
6258
6259 QualType Type = VD->getType();
6260 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6261 // It will be analyzed later.
6262 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006263 SrcExprs.push_back(nullptr);
6264 DstExprs.push_back(nullptr);
6265 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006266 continue;
6267 }
6268
6269 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
6270 // A list item that appears in a copyin clause must be threadprivate.
6271 if (!DSAStack->isThreadPrivate(VD)) {
6272 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00006273 << getOpenMPClauseName(OMPC_copyin)
6274 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006275 continue;
6276 }
6277
6278 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
6279 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00006280 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006281 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006282 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00006283 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006284 ElemType.getUnqualifiedType(), ".copyin.src");
Alexey Bataev39f915b82015-05-08 10:41:21 +00006285 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006286 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
6287 auto *DstVD =
6288 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst");
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006289 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006290 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006291 // For arrays generate assignment operation for single element and replace
6292 // it by the original array element in CodeGen.
6293 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6294 PseudoDstExpr, PseudoSrcExpr);
6295 if (AssignmentOp.isInvalid())
6296 continue;
6297 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6298 /*DiscardedValue=*/true);
6299 if (AssignmentOp.isInvalid())
6300 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006301
6302 DSAStack->addDSA(VD, DE, OMPC_copyin);
6303 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006304 SrcExprs.push_back(PseudoSrcExpr);
6305 DstExprs.push_back(PseudoDstExpr);
6306 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006307 }
6308
Alexey Bataeved09d242014-05-28 05:53:51 +00006309 if (Vars.empty())
6310 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006311
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006312 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
6313 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006314}
6315
Alexey Bataevbae9a792014-06-27 10:37:06 +00006316OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
6317 SourceLocation StartLoc,
6318 SourceLocation LParenLoc,
6319 SourceLocation EndLoc) {
6320 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00006321 SmallVector<Expr *, 8> SrcExprs;
6322 SmallVector<Expr *, 8> DstExprs;
6323 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006324 for (auto &RefExpr : VarList) {
6325 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
6326 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6327 // It will be analyzed later.
6328 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006329 SrcExprs.push_back(nullptr);
6330 DstExprs.push_back(nullptr);
6331 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006332 continue;
6333 }
6334
6335 SourceLocation ELoc = RefExpr->getExprLoc();
6336 // OpenMP [2.1, C/C++]
6337 // A list item is a variable name.
6338 // OpenMP [2.14.4.1, Restrictions, p.1]
6339 // A list item that appears in a copyin clause must be threadprivate.
6340 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
6341 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6342 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6343 continue;
6344 }
6345
6346 Decl *D = DE->getDecl();
6347 VarDecl *VD = cast<VarDecl>(D);
6348
6349 QualType Type = VD->getType();
6350 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6351 // It will be analyzed later.
6352 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006353 SrcExprs.push_back(nullptr);
6354 DstExprs.push_back(nullptr);
6355 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006356 continue;
6357 }
6358
6359 // OpenMP [2.14.4.2, Restrictions, p.2]
6360 // A list item that appears in a copyprivate clause may not appear in a
6361 // private or firstprivate clause on the single construct.
6362 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006363 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006364 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
6365 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00006366 Diag(ELoc, diag::err_omp_wrong_dsa)
6367 << getOpenMPClauseName(DVar.CKind)
6368 << getOpenMPClauseName(OMPC_copyprivate);
6369 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6370 continue;
6371 }
6372
6373 // OpenMP [2.11.4.2, Restrictions, p.1]
6374 // All list items that appear in a copyprivate clause must be either
6375 // threadprivate or private in the enclosing context.
6376 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006377 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006378 if (DVar.CKind == OMPC_shared) {
6379 Diag(ELoc, diag::err_omp_required_access)
6380 << getOpenMPClauseName(OMPC_copyprivate)
6381 << "threadprivate or private in the enclosing context";
6382 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6383 continue;
6384 }
6385 }
6386 }
6387
Alexey Bataev7a3e5852015-05-19 08:19:24 +00006388 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006389 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00006390 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006391 << getOpenMPClauseName(OMPC_copyprivate) << Type
6392 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00006393 bool IsDecl =
6394 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6395 Diag(VD->getLocation(),
6396 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6397 << VD;
6398 continue;
6399 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006400
Alexey Bataevbae9a792014-06-27 10:37:06 +00006401 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
6402 // A variable of class type (or array thereof) that appears in a
6403 // copyin clause requires an accessible, unambiguous copy assignment
6404 // operator for the class type.
Alexey Bataev420d45b2015-04-14 05:11:24 +00006405 Type = Context.getBaseElementType(Type).getUnqualifiedType();
6406 auto *SrcVD =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006407 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src");
Alexey Bataev420d45b2015-04-14 05:11:24 +00006408 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006409 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00006410 auto *DstVD =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006411 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst");
Alexey Bataev420d45b2015-04-14 05:11:24 +00006412 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006413 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00006414 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6415 PseudoDstExpr, PseudoSrcExpr);
6416 if (AssignmentOp.isInvalid())
6417 continue;
6418 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6419 /*DiscardedValue=*/true);
6420 if (AssignmentOp.isInvalid())
6421 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006422
6423 // No need to mark vars as copyprivate, they are already threadprivate or
6424 // implicitly private.
6425 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006426 SrcExprs.push_back(PseudoSrcExpr);
6427 DstExprs.push_back(PseudoDstExpr);
6428 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00006429 }
6430
6431 if (Vars.empty())
6432 return nullptr;
6433
Alexey Bataeva63048e2015-03-23 06:18:07 +00006434 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
6435 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006436}
6437
Alexey Bataev6125da92014-07-21 11:26:11 +00006438OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
6439 SourceLocation StartLoc,
6440 SourceLocation LParenLoc,
6441 SourceLocation EndLoc) {
6442 if (VarList.empty())
6443 return nullptr;
6444
6445 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
6446}
Alexey Bataevdea47612014-07-23 07:46:59 +00006447
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006448OMPClause *
6449Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
6450 SourceLocation DepLoc, SourceLocation ColonLoc,
6451 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
6452 SourceLocation LParenLoc, SourceLocation EndLoc) {
6453 if (DepKind == OMPC_DEPEND_unknown) {
6454 std::string Values;
6455 std::string Sep(", ");
6456 for (unsigned i = 0; i < OMPC_DEPEND_unknown; ++i) {
6457 Values += "'";
6458 Values += getOpenMPSimpleClauseTypeName(OMPC_depend, i);
6459 Values += "'";
6460 switch (i) {
6461 case OMPC_DEPEND_unknown - 2:
6462 Values += " or ";
6463 break;
6464 case OMPC_DEPEND_unknown - 1:
6465 break;
6466 default:
6467 Values += Sep;
6468 break;
6469 }
6470 }
6471 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
6472 << Values << getOpenMPClauseName(OMPC_depend);
6473 return nullptr;
6474 }
6475 SmallVector<Expr *, 8> Vars;
6476 for (auto &RefExpr : VarList) {
6477 assert(RefExpr && "NULL expr in OpenMP shared clause.");
6478 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6479 // It will be analyzed later.
6480 Vars.push_back(RefExpr);
6481 continue;
6482 }
6483
6484 SourceLocation ELoc = RefExpr->getExprLoc();
6485 // OpenMP [2.11.1.1, Restrictions, p.3]
6486 // A variable that is part of another variable (such as a field of a
6487 // structure) but is not an array element or an array section cannot appear
6488 // in a depend clause.
6489 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
6490 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
6491 ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
6492 if (!RefExpr->IgnoreParenImpCasts()->isLValue() || (!ASE && !DE) ||
6493 (DE && !isa<VarDecl>(DE->getDecl())) ||
6494 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
6495 !ASE->getBase()->getType()->isArrayType())) {
6496 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
6497 << RefExpr->getSourceRange();
6498 continue;
6499 }
6500
6501 Vars.push_back(RefExpr->IgnoreParenImpCasts());
6502 }
6503
6504 if (Vars.empty())
6505 return nullptr;
6506
6507 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
6508 DepLoc, ColonLoc, Vars);
6509}
6510