blob: 6b092a80de99f13513cbafbc6613db12a95b325c [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 Bataevb08f89f2015-08-14 12:25:37 +000015#include "TreeTransform.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000016#include "clang/AST/ASTContext.h"
Alexey Bataev97720002014-11-11 04:05:39 +000017#include "clang/AST/ASTMutationListener.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000018#include "clang/AST/Decl.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000019#include "clang/AST/DeclCXX.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000020#include "clang/AST/DeclOpenMP.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000021#include "clang/AST/StmtCXX.h"
22#include "clang/AST/StmtOpenMP.h"
23#include "clang/AST/StmtVisitor.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000024#include "clang/Basic/OpenMPKinds.h"
Samuel Antaof8b50122015-07-13 22:54:53 +000025#include "clang/Basic/TargetInfo.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000026#include "clang/Lex/Preprocessor.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000027#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000028#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000029#include "clang/Sema/Scope.h"
30#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000031#include "clang/Sema/SemaInternal.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000032using namespace clang;
33
Alexey Bataev758e55e2013-09-06 18:03:48 +000034//===----------------------------------------------------------------------===//
35// Stack of data-sharing attributes for variables
36//===----------------------------------------------------------------------===//
37
38namespace {
39/// \brief Default data sharing attributes, which can be applied to directive.
40enum DefaultDataSharingAttributes {
Alexey Bataeved09d242014-05-28 05:53:51 +000041 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
42 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
43 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000044};
Alexey Bataev7ff55242014-06-19 09:13:45 +000045
Alexey Bataevf29276e2014-06-18 04:14:57 +000046template <class T> struct MatchesAny {
Alexey Bataev23b69422014-06-18 07:08:49 +000047 explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {}
Alexey Bataevf29276e2014-06-18 04:14:57 +000048 bool operator()(T Kind) {
49 for (auto KindEl : Arr)
50 if (KindEl == Kind)
51 return true;
52 return false;
53 }
54
55private:
56 ArrayRef<T> Arr;
57};
Alexey Bataev23b69422014-06-18 07:08:49 +000058struct MatchesAlways {
Alexey Bataevf29276e2014-06-18 04:14:57 +000059 MatchesAlways() {}
Alexey Bataev7ff55242014-06-19 09:13:45 +000060 template <class T> bool operator()(T) { return true; }
Alexey Bataevf29276e2014-06-18 04:14:57 +000061};
62
63typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause;
64typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective;
Alexey Bataev758e55e2013-09-06 18:03:48 +000065
66/// \brief Stack for tracking declarations used in OpenMP directives and
67/// clauses and their data-sharing attributes.
68class DSAStackTy {
69public:
70 struct DSAVarData {
71 OpenMPDirectiveKind DKind;
72 OpenMPClauseKind CKind;
73 DeclRefExpr *RefExpr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000074 SourceLocation ImplicitDSALoc;
75 DSAVarData()
76 : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr),
77 ImplicitDSALoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000078 };
Alexey Bataeved09d242014-05-28 05:53:51 +000079
Alexey Bataev758e55e2013-09-06 18:03:48 +000080private:
81 struct DSAInfo {
82 OpenMPClauseKind Attributes;
83 DeclRefExpr *RefExpr;
84 };
85 typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000086 typedef llvm::SmallDenseMap<VarDecl *, DeclRefExpr *, 64> AlignedMapTy;
Alexey Bataev9c821032015-04-30 04:23:23 +000087 typedef llvm::DenseSet<VarDecl *> LoopControlVariablesSetTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000088
89 struct SharingMapTy {
90 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000091 AlignedMapTy AlignedMap;
Alexey Bataev9c821032015-04-30 04:23:23 +000092 LoopControlVariablesSetTy LCVSet;
Alexey Bataev758e55e2013-09-06 18:03:48 +000093 DefaultDataSharingAttributes DefaultAttr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000094 SourceLocation DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +000095 OpenMPDirectiveKind Directive;
96 DeclarationNameInfo DirectiveName;
97 Scope *CurScope;
Alexey Bataevbae9a792014-06-27 10:37:06 +000098 SourceLocation ConstructLoc;
Alexey Bataev9fb6e642014-07-22 06:45:04 +000099 bool OrderedRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000100 bool NowaitRegion;
Alexey Bataev9c821032015-04-30 04:23:23 +0000101 unsigned CollapseNumber;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000102 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +0000103 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000104 Scope *CurScope, SourceLocation Loc)
Alexey Bataev9c821032015-04-30 04:23:23 +0000105 : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000106 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000107 ConstructLoc(Loc), OrderedRegion(false), NowaitRegion(false),
108 CollapseNumber(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000109 SharingMapTy()
Alexey Bataev9c821032015-04-30 04:23:23 +0000110 : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000111 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000112 ConstructLoc(), OrderedRegion(false), NowaitRegion(false),
113 CollapseNumber(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000114 };
115
116 typedef SmallVector<SharingMapTy, 64> StackTy;
117
118 /// \brief Stack of used declaration and their data-sharing attributes.
119 StackTy Stack;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000120 /// \brief true, if check for DSA must be from parent directive, false, if
121 /// from current directive.
Alexey Bataevaac108a2015-06-23 04:51:00 +0000122 OpenMPClauseKind ClauseKindMode;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000123 Sema &SemaRef;
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000124 bool ForceCapturing;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000125
126 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
127
128 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000129
130 /// \brief Checks if the variable is a local for OpenMP region.
131 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000132
Alexey Bataev758e55e2013-09-06 18:03:48 +0000133public:
Alexey Bataevaac108a2015-06-23 04:51:00 +0000134 explicit DSAStackTy(Sema &S)
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000135 : Stack(1), ClauseKindMode(OMPC_unknown), SemaRef(S),
136 ForceCapturing(false) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000137
Alexey Bataevaac108a2015-06-23 04:51:00 +0000138 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
139 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000140
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000141 bool isForceVarCapturing() const { return ForceCapturing; }
142 void setForceVarCapturing(bool V) { ForceCapturing = V; }
143
Alexey Bataev758e55e2013-09-06 18:03:48 +0000144 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000145 Scope *CurScope, SourceLocation Loc) {
146 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
147 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000148 }
149
150 void pop() {
151 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
152 Stack.pop_back();
153 }
154
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000155 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000156 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000157 /// for diagnostics.
158 DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE);
159
Alexey Bataev9c821032015-04-30 04:23:23 +0000160 /// \brief Register specified variable as loop control variable.
161 void addLoopControlVariable(VarDecl *D);
162 /// \brief Check if the specified variable is a loop control variable for
163 /// current region.
164 bool isLoopControlVariable(VarDecl *D);
165
Alexey Bataev758e55e2013-09-06 18:03:48 +0000166 /// \brief Adds explicit data sharing attribute to the specified declaration.
167 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
168
Alexey Bataev758e55e2013-09-06 18:03:48 +0000169 /// \brief Returns data sharing attributes from top of the stack for the
170 /// specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000171 DSAVarData getTopDSA(VarDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000172 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000173 DSAVarData getImplicitDSA(VarDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000174 /// \brief Checks if the specified variables has data-sharing attributes which
175 /// match specified \a CPred predicate in any directive which matches \a DPred
176 /// predicate.
177 template <class ClausesPredicate, class DirectivesPredicate>
178 DSAVarData hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000179 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000180 /// \brief Checks if the specified variables has data-sharing attributes which
181 /// match specified \a CPred predicate in any innermost directive which
182 /// matches \a DPred predicate.
183 template <class ClausesPredicate, class DirectivesPredicate>
184 DSAVarData hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000185 DirectivesPredicate DPred,
186 bool FromParent);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000187 /// \brief Checks if the specified variables has explicit data-sharing
188 /// attributes which match specified \a CPred predicate at the specified
189 /// OpenMP region.
190 bool hasExplicitDSA(VarDecl *D,
191 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
192 unsigned Level);
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000193 /// \brief Finds a directive which matches specified \a DPred predicate.
194 template <class NamedDirectivesPredicate>
195 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000196
Alexey Bataev758e55e2013-09-06 18:03:48 +0000197 /// \brief Returns currently analyzed directive.
198 OpenMPDirectiveKind getCurrentDirective() const {
199 return Stack.back().Directive;
200 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000201 /// \brief Returns parent directive.
202 OpenMPDirectiveKind getParentDirective() const {
203 if (Stack.size() > 2)
204 return Stack[Stack.size() - 2].Directive;
205 return OMPD_unknown;
206 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000207
208 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000209 void setDefaultDSANone(SourceLocation Loc) {
210 Stack.back().DefaultAttr = DSA_none;
211 Stack.back().DefaultAttrLoc = Loc;
212 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000213 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000214 void setDefaultDSAShared(SourceLocation Loc) {
215 Stack.back().DefaultAttr = DSA_shared;
216 Stack.back().DefaultAttrLoc = Loc;
217 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000218
219 DefaultDataSharingAttributes getDefaultDSA() const {
220 return Stack.back().DefaultAttr;
221 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000222 SourceLocation getDefaultDSALocation() const {
223 return Stack.back().DefaultAttrLoc;
224 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000225
Alexey Bataevf29276e2014-06-18 04:14:57 +0000226 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000227 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000228 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000229 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000230 }
231
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000232 /// \brief Marks current region as ordered (it has an 'ordered' clause).
233 void setOrderedRegion(bool IsOrdered = true) {
234 Stack.back().OrderedRegion = IsOrdered;
235 }
236 /// \brief Returns true, if parent region is ordered (has associated
237 /// 'ordered' clause), false - otherwise.
238 bool isParentOrderedRegion() const {
239 if (Stack.size() > 2)
240 return Stack[Stack.size() - 2].OrderedRegion;
241 return false;
242 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000243 /// \brief Marks current region as nowait (it has a 'nowait' clause).
244 void setNowaitRegion(bool IsNowait = true) {
245 Stack.back().NowaitRegion = IsNowait;
246 }
247 /// \brief Returns true, if parent region is nowait (has associated
248 /// 'nowait' clause), false - otherwise.
249 bool isParentNowaitRegion() const {
250 if (Stack.size() > 2)
251 return Stack[Stack.size() - 2].NowaitRegion;
252 return false;
253 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000254
Alexey Bataev9c821032015-04-30 04:23:23 +0000255 /// \brief Set collapse value for the region.
256 void setCollapseNumber(unsigned Val) { Stack.back().CollapseNumber = Val; }
257 /// \brief Return collapse value for region.
258 unsigned getCollapseNumber() const {
259 return Stack.back().CollapseNumber;
260 }
261
Alexey Bataev13314bf2014-10-09 04:18:56 +0000262 /// \brief Marks current target region as one with closely nested teams
263 /// region.
264 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
265 if (Stack.size() > 2)
266 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
267 }
268 /// \brief Returns true, if current region has closely nested teams region.
269 bool hasInnerTeamsRegion() const {
270 return getInnerTeamsRegionLoc().isValid();
271 }
272 /// \brief Returns location of the nested teams region (if any).
273 SourceLocation getInnerTeamsRegionLoc() const {
274 if (Stack.size() > 1)
275 return Stack.back().InnerTeamsRegionLoc;
276 return SourceLocation();
277 }
278
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000279 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000280 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000281 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000282};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000283bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
284 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
Alexey Bataev13314bf2014-10-09 04:18:56 +0000285 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000286}
Alexey Bataeved09d242014-05-28 05:53:51 +0000287} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000288
289DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
290 VarDecl *D) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000291 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000292 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000293 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000294 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
295 // in a region but not in construct]
296 // File-scope or namespace-scope variables referenced in called routines
297 // in the region are shared unless they appear in a threadprivate
298 // directive.
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000299 if (!D->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000300 DVar.CKind = OMPC_shared;
301
302 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
303 // in a region but not in construct]
304 // Variables with static storage duration that are declared in called
305 // routines in the region are shared.
306 if (D->hasGlobalStorage())
307 DVar.CKind = OMPC_shared;
308
Alexey Bataev758e55e2013-09-06 18:03:48 +0000309 return DVar;
310 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000311
Alexey Bataev758e55e2013-09-06 18:03:48 +0000312 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000313 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
314 // in a Construct, C/C++, predetermined, p.1]
315 // Variables with automatic storage duration that are declared in a scope
316 // inside the construct are private.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000317 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
318 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
319 DVar.CKind = OMPC_private;
320 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000321 }
322
Alexey Bataev758e55e2013-09-06 18:03:48 +0000323 // Explicitly specified attributes and local variables with predetermined
324 // attributes.
325 if (Iter->SharingMap.count(D)) {
326 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
327 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000328 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000329 return DVar;
330 }
331
332 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
333 // in a Construct, C/C++, implicitly determined, p.1]
334 // In a parallel or task construct, the data-sharing attributes of these
335 // variables are determined by the default clause, if present.
336 switch (Iter->DefaultAttr) {
337 case DSA_shared:
338 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000339 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000340 return DVar;
341 case DSA_none:
342 return DVar;
343 case DSA_unspecified:
344 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
345 // in a Construct, implicitly determined, p.2]
346 // In a parallel construct, if no default clause is present, these
347 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000348 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000349 if (isOpenMPParallelDirective(DVar.DKind) ||
350 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000351 DVar.CKind = OMPC_shared;
352 return DVar;
353 }
354
355 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
356 // in a Construct, implicitly determined, p.4]
357 // In a task construct, if no default clause is present, a variable that in
358 // the enclosing context is determined to be shared by all implicit tasks
359 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000360 if (DVar.DKind == OMPD_task) {
361 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000362 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000363 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000364 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
365 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000366 // in a Construct, implicitly determined, p.6]
367 // In a task construct, if no default clause is present, a variable
368 // whose data-sharing attribute is not determined by the rules above is
369 // firstprivate.
370 DVarTemp = getDSA(I, D);
371 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000372 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000373 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000374 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000375 return DVar;
376 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000377 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000378 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000379 }
380 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000381 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000382 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000383 return DVar;
384 }
385 }
386 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
387 // in a Construct, implicitly determined, p.3]
388 // For constructs other than task, if no default clause is present, these
389 // variables inherit their data-sharing attributes from the enclosing
390 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000391 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000392}
393
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000394DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
395 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000396 D = D->getCanonicalDecl();
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000397 auto It = Stack.back().AlignedMap.find(D);
398 if (It == Stack.back().AlignedMap.end()) {
399 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
400 Stack.back().AlignedMap[D] = NewDE;
401 return nullptr;
402 } else {
403 assert(It->second && "Unexpected nullptr expr in the aligned map");
404 return It->second;
405 }
406 return nullptr;
407}
408
Alexey Bataev9c821032015-04-30 04:23:23 +0000409void DSAStackTy::addLoopControlVariable(VarDecl *D) {
410 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
411 D = D->getCanonicalDecl();
412 Stack.back().LCVSet.insert(D);
413}
414
415bool DSAStackTy::isLoopControlVariable(VarDecl *D) {
416 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
417 D = D->getCanonicalDecl();
418 return Stack.back().LCVSet.count(D) > 0;
419}
420
Alexey Bataev758e55e2013-09-06 18:03:48 +0000421void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000422 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000423 if (A == OMPC_threadprivate) {
424 Stack[0].SharingMap[D].Attributes = A;
425 Stack[0].SharingMap[D].RefExpr = E;
426 } else {
427 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
428 Stack.back().SharingMap[D].Attributes = A;
429 Stack.back().SharingMap[D].RefExpr = E;
430 }
431}
432
Alexey Bataeved09d242014-05-28 05:53:51 +0000433bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000434 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000435 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000436 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000437 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000438 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000439 ++I;
440 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000441 if (I == E)
442 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000443 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000444 Scope *CurScope = getCurScope();
445 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000446 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000447 }
448 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000449 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000450 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000451}
452
Alexey Bataev39f915b82015-05-08 10:41:21 +0000453/// \brief Build a variable declaration for OpenMP loop iteration variable.
454static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
455 StringRef Name) {
456 DeclContext *DC = SemaRef.CurContext;
457 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
458 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
459 VarDecl *Decl =
460 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
461 Decl->setImplicit();
462 return Decl;
463}
464
465static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
466 SourceLocation Loc,
467 bool RefersToCapture = false) {
468 D->setReferenced();
469 D->markUsed(S.Context);
470 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
471 SourceLocation(), D, RefersToCapture, Loc, Ty,
472 VK_LValue);
473}
474
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000475DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000476 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000477 DSAVarData DVar;
478
479 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
480 // in a Construct, C/C++, predetermined, p.1]
481 // Variables appearing in threadprivate directives are threadprivate.
Samuel Antaof8b50122015-07-13 22:54:53 +0000482 if ((D->getTLSKind() != VarDecl::TLS_None &&
483 !(D->hasAttr<OMPThreadPrivateDeclAttr>() &&
484 SemaRef.getLangOpts().OpenMPUseTLS &&
485 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +0000486 (D->getStorageClass() == SC_Register && D->hasAttr<AsmLabelAttr>() &&
487 !D->isLocalVarDecl())) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000488 addDSA(D, buildDeclRefExpr(SemaRef, D, D->getType().getNonReferenceType(),
489 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000490 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000491 }
492 if (Stack[0].SharingMap.count(D)) {
493 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
494 DVar.CKind = OMPC_threadprivate;
495 return DVar;
496 }
497
498 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
499 // in a Construct, C/C++, predetermined, p.1]
500 // Variables with automatic storage duration that are declared in a scope
501 // inside the construct are private.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000502 OpenMPDirectiveKind Kind =
503 FromParent ? getParentDirective() : getCurrentDirective();
504 auto StartI = std::next(Stack.rbegin());
505 auto EndI = std::prev(Stack.rend());
506 if (FromParent && StartI != EndI) {
507 StartI = std::next(StartI);
508 }
509 if (!isParallelOrTaskRegion(Kind)) {
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000510 if (isOpenMPLocal(D, StartI) &&
511 ((D->isLocalVarDecl() && (D->getStorageClass() == SC_Auto ||
512 D->getStorageClass() == SC_None)) ||
513 isa<ParmVarDecl>(D))) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000514 DVar.CKind = OMPC_private;
515 return DVar;
Alexander Musman8dba6642014-04-22 13:09:42 +0000516 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000517
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000518 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
519 // in a Construct, C/C++, predetermined, p.4]
520 // Static data members are shared.
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000521 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
522 // in a Construct, C/C++, predetermined, p.7]
523 // Variables with static storage duration that are declared in a scope
524 // inside the construct are shared.
Alexey Bataev42971a32015-01-20 07:03:46 +0000525 if (D->isStaticDataMember() || D->isStaticLocal()) {
526 DSAVarData DVarTemp =
527 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
528 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
529 return DVar;
530
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000531 DVar.CKind = OMPC_shared;
532 return DVar;
533 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000534 }
535
536 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000537 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
538 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000539 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
540 // in a Construct, C/C++, predetermined, p.6]
541 // Variables with const qualified type having no mutable member are
542 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000543 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000544 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000545 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000546 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000547 // Variables with const-qualified type having no mutable member may be
548 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000549 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
550 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000551 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
552 return DVar;
553
Alexey Bataev758e55e2013-09-06 18:03:48 +0000554 DVar.CKind = OMPC_shared;
555 return DVar;
556 }
557
Alexey Bataev758e55e2013-09-06 18:03:48 +0000558 // Explicitly specified attributes and local variables with predetermined
559 // attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000560 auto I = std::prev(StartI);
561 if (I->SharingMap.count(D)) {
562 DVar.RefExpr = I->SharingMap[D].RefExpr;
563 DVar.CKind = I->SharingMap[D].Attributes;
564 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000565 }
566
567 return DVar;
568}
569
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000570DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000571 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000572 auto StartI = Stack.rbegin();
573 auto EndI = std::prev(Stack.rend());
574 if (FromParent && StartI != EndI) {
575 StartI = std::next(StartI);
576 }
577 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000578}
579
Alexey Bataevf29276e2014-06-18 04:14:57 +0000580template <class ClausesPredicate, class DirectivesPredicate>
581DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000582 DirectivesPredicate DPred,
583 bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000584 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000585 auto StartI = std::next(Stack.rbegin());
586 auto EndI = std::prev(Stack.rend());
587 if (FromParent && StartI != EndI) {
588 StartI = std::next(StartI);
589 }
590 for (auto I = StartI, EE = EndI; I != EE; ++I) {
591 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000592 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000593 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000594 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000595 return DVar;
596 }
597 return DSAVarData();
598}
599
Alexey Bataevf29276e2014-06-18 04:14:57 +0000600template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000601DSAStackTy::DSAVarData
602DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
603 DirectivesPredicate DPred, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000604 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000605 auto StartI = std::next(Stack.rbegin());
606 auto EndI = std::prev(Stack.rend());
607 if (FromParent && StartI != EndI) {
608 StartI = std::next(StartI);
609 }
610 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000611 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000612 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000613 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000614 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000615 return DVar;
616 return DSAVarData();
617 }
618 return DSAVarData();
619}
620
Alexey Bataevaac108a2015-06-23 04:51:00 +0000621bool DSAStackTy::hasExplicitDSA(
622 VarDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
623 unsigned Level) {
624 if (CPred(ClauseKindMode))
625 return true;
626 if (isClauseParsingMode())
627 ++Level;
628 D = D->getCanonicalDecl();
629 auto StartI = Stack.rbegin();
630 auto EndI = std::prev(Stack.rend());
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000631 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000632 return false;
633 std::advance(StartI, Level);
634 return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
635 CPred(StartI->SharingMap[D].Attributes);
636}
637
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000638template <class NamedDirectivesPredicate>
639bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
640 auto StartI = std::next(Stack.rbegin());
641 auto EndI = std::prev(Stack.rend());
642 if (FromParent && StartI != EndI) {
643 StartI = std::next(StartI);
644 }
645 for (auto I = StartI, EE = EndI; I != EE; ++I) {
646 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
647 return true;
648 }
649 return false;
650}
651
Alexey Bataev758e55e2013-09-06 18:03:48 +0000652void Sema::InitDataSharingAttributesStack() {
653 VarDataSharingAttributesStack = new DSAStackTy(*this);
654}
655
656#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
657
Alexey Bataevf841bd92014-12-16 07:00:22 +0000658bool Sema::IsOpenMPCapturedVar(VarDecl *VD) {
659 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000660 VD = VD->getCanonicalDecl();
Alexey Bataev48977c32015-08-04 08:10:48 +0000661 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
662 (!DSAStack->isClauseParsingMode() ||
663 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000664 if (DSAStack->isLoopControlVariable(VD) ||
665 (VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000666 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
667 DSAStack->isForceVarCapturing())
Alexey Bataev9c821032015-04-30 04:23:23 +0000668 return true;
Alexey Bataevaac108a2015-06-23 04:51:00 +0000669 auto DVarPrivate = DSAStack->getTopDSA(VD, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000670 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
671 return true;
672 DVarPrivate = DSAStack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(),
Alexey Bataevaac108a2015-06-23 04:51:00 +0000673 DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000674 return DVarPrivate.CKind != OMPC_unknown;
675 }
676 return false;
677}
678
Alexey Bataevaac108a2015-06-23 04:51:00 +0000679bool Sema::isOpenMPPrivateVar(VarDecl *VD, unsigned Level) {
680 assert(LangOpts.OpenMP && "OpenMP is not allowed");
681 return DSAStack->hasExplicitDSA(
682 VD, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
683}
684
Alexey Bataeved09d242014-05-28 05:53:51 +0000685void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000686
687void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
688 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000689 Scope *CurScope, SourceLocation Loc) {
690 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000691 PushExpressionEvaluationContext(PotentiallyEvaluated);
692}
693
Alexey Bataevaac108a2015-06-23 04:51:00 +0000694void Sema::StartOpenMPClause(OpenMPClauseKind K) {
695 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000696}
697
Alexey Bataevaac108a2015-06-23 04:51:00 +0000698void Sema::EndOpenMPClause() {
699 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000700}
701
Alexey Bataev758e55e2013-09-06 18:03:48 +0000702void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000703 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
704 // A variable of class type (or array thereof) that appears in a lastprivate
705 // clause requires an accessible, unambiguous default constructor for the
706 // class type, unless the list item is also specified in a firstprivate
707 // clause.
708 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000709 for (auto *C : D->clauses()) {
710 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
711 SmallVector<Expr *, 8> PrivateCopies;
712 for (auto *DE : Clause->varlists()) {
713 if (DE->isValueDependent() || DE->isTypeDependent()) {
714 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000715 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +0000716 }
717 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(DE)->getDecl());
Alexey Bataevbd9fec12015-08-18 06:47:21 +0000718 QualType Type = VD->getType().getNonReferenceType();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000719 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000720 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000721 // Generate helper private variable and initialize it with the
722 // default value. The address of the original variable is replaced
723 // by the address of the new private variable in CodeGen. This new
724 // variable is not added to IdResolver, so the code in the OpenMP
725 // region uses original variable for proper diagnostics.
Alexey Bataev39f915b82015-05-08 10:41:21 +0000726 auto *VDPrivate =
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000727 buildVarDecl(*this, DE->getExprLoc(), Type.getUnqualifiedType(),
728 VD->getName());
Alexey Bataev38e89532015-04-16 04:54:05 +0000729 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
730 if (VDPrivate->isInvalidDecl())
731 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000732 PrivateCopies.push_back(buildDeclRefExpr(
733 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +0000734 } else {
735 // The variable is also a firstprivate, so initialization sequence
736 // for private copy is generated already.
737 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000738 }
739 }
Alexey Bataev38e89532015-04-16 04:54:05 +0000740 // Set initializers to private copies if no errors were found.
741 if (PrivateCopies.size() == Clause->varlist_size()) {
742 Clause->setPrivateCopies(PrivateCopies);
743 }
Alexey Bataevf29276e2014-06-18 04:14:57 +0000744 }
745 }
746 }
747
Alexey Bataev758e55e2013-09-06 18:03:48 +0000748 DSAStack->pop();
749 DiscardCleanupsInEvaluationContext();
750 PopExpressionEvaluationContext();
751}
752
Alexander Musman3276a272015-03-21 10:12:56 +0000753static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
754 Expr *NumIterations, Sema &SemaRef,
755 Scope *S);
756
Alexey Bataeva769e072013-03-22 06:34:35 +0000757namespace {
758
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000759class VarDeclFilterCCC : public CorrectionCandidateCallback {
760private:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000761 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +0000762
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000763public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000764 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000765 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000766 NamedDecl *ND = Candidate.getCorrectionDecl();
767 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
768 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000769 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
770 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000771 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000772 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000773 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000774};
Alexey Bataeved09d242014-05-28 05:53:51 +0000775} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000776
777ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
778 CXXScopeSpec &ScopeSpec,
779 const DeclarationNameInfo &Id) {
780 LookupResult Lookup(*this, Id, LookupOrdinaryName);
781 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
782
783 if (Lookup.isAmbiguous())
784 return ExprError();
785
786 VarDecl *VD;
787 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000788 if (TypoCorrection Corrected = CorrectTypo(
789 Id, LookupOrdinaryName, CurScope, nullptr,
790 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000791 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000792 PDiag(Lookup.empty()
793 ? diag::err_undeclared_var_use_suggest
794 : diag::err_omp_expected_var_arg_suggest)
795 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000796 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000797 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000798 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
799 : diag::err_omp_expected_var_arg)
800 << Id.getName();
801 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000802 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000803 } else {
804 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000805 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000806 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
807 return ExprError();
808 }
809 }
810 Lookup.suppressDiagnostics();
811
812 // OpenMP [2.9.2, Syntax, C/C++]
813 // Variables must be file-scope, namespace-scope, or static block-scope.
814 if (!VD->hasGlobalStorage()) {
815 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000816 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
817 bool IsDecl =
818 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000819 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000820 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
821 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000822 return ExprError();
823 }
824
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000825 VarDecl *CanonicalVD = VD->getCanonicalDecl();
826 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000827 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
828 // A threadprivate directive for file-scope variables must appear outside
829 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000830 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
831 !getCurLexicalContext()->isTranslationUnit()) {
832 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000833 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
834 bool IsDecl =
835 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
836 Diag(VD->getLocation(),
837 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
838 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000839 return ExprError();
840 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000841 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
842 // A threadprivate directive for static class member variables must appear
843 // in the class definition, in the same scope in which the member
844 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000845 if (CanonicalVD->isStaticDataMember() &&
846 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
847 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000848 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
849 bool IsDecl =
850 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
851 Diag(VD->getLocation(),
852 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
853 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000854 return ExprError();
855 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000856 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
857 // A threadprivate directive for namespace-scope variables must appear
858 // outside any definition or declaration other than the namespace
859 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000860 if (CanonicalVD->getDeclContext()->isNamespace() &&
861 (!getCurLexicalContext()->isFileContext() ||
862 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
863 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000864 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
865 bool IsDecl =
866 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
867 Diag(VD->getLocation(),
868 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
869 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000870 return ExprError();
871 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000872 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
873 // A threadprivate directive for static block-scope variables must appear
874 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000875 if (CanonicalVD->isStaticLocal() && CurScope &&
876 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000877 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000878 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
879 bool IsDecl =
880 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
881 Diag(VD->getLocation(),
882 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
883 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000884 return ExprError();
885 }
886
887 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
888 // A threadprivate directive must lexically precede all references to any
889 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000890 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000891 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +0000892 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000893 return ExprError();
894 }
895
896 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +0000897 ExprResult DE = buildDeclRefExpr(*this, VD, ExprType, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000898 return DE;
899}
900
Alexey Bataeved09d242014-05-28 05:53:51 +0000901Sema::DeclGroupPtrTy
902Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
903 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000904 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000905 CurContext->addDecl(D);
906 return DeclGroupPtrTy::make(DeclGroupRef(D));
907 }
908 return DeclGroupPtrTy();
909}
910
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000911namespace {
912class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
913 Sema &SemaRef;
914
915public:
916 bool VisitDeclRefExpr(const DeclRefExpr *E) {
917 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
918 if (VD->hasLocalStorage()) {
919 SemaRef.Diag(E->getLocStart(),
920 diag::err_omp_local_var_in_threadprivate_init)
921 << E->getSourceRange();
922 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
923 << VD << VD->getSourceRange();
924 return true;
925 }
926 }
927 return false;
928 }
929 bool VisitStmt(const Stmt *S) {
930 for (auto Child : S->children()) {
931 if (Child && Visit(Child))
932 return true;
933 }
934 return false;
935 }
Alexey Bataev23b69422014-06-18 07:08:49 +0000936 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000937};
938} // namespace
939
Alexey Bataeved09d242014-05-28 05:53:51 +0000940OMPThreadPrivateDecl *
941Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000942 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +0000943 for (auto &RefExpr : VarList) {
944 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000945 VarDecl *VD = cast<VarDecl>(DE->getDecl());
946 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000947
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000948 QualType QType = VD->getType();
949 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
950 // It will be analyzed later.
951 Vars.push_back(DE);
952 continue;
953 }
954
Alexey Bataeva769e072013-03-22 06:34:35 +0000955 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
956 // A threadprivate variable must not have an incomplete type.
957 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000958 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000959 continue;
960 }
961
962 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
963 // A threadprivate variable must not have a reference type.
964 if (VD->getType()->isReferenceType()) {
965 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000966 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
967 bool IsDecl =
968 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
969 Diag(VD->getLocation(),
970 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
971 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000972 continue;
973 }
974
Samuel Antaof8b50122015-07-13 22:54:53 +0000975 // Check if this is a TLS variable. If TLS is not being supported, produce
976 // the corresponding diagnostic.
977 if ((VD->getTLSKind() != VarDecl::TLS_None &&
978 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
979 getLangOpts().OpenMPUseTLS &&
980 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +0000981 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
982 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +0000983 Diag(ILoc, diag::err_omp_var_thread_local)
984 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +0000985 bool IsDecl =
986 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
987 Diag(VD->getLocation(),
988 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
989 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000990 continue;
991 }
992
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000993 // Check if initial value of threadprivate variable reference variable with
994 // local storage (it is not supported by runtime).
995 if (auto Init = VD->getAnyInitializer()) {
996 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000997 if (Checker.Visit(Init))
998 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000999 }
1000
Alexey Bataeved09d242014-05-28 05:53:51 +00001001 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001002 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001003 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1004 Context, SourceRange(Loc, Loc)));
1005 if (auto *ML = Context.getASTMutationListener())
1006 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001007 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001008 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001009 if (!Vars.empty()) {
1010 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1011 Vars);
1012 D->setAccess(AS_public);
1013 }
1014 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001015}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001016
Alexey Bataev7ff55242014-06-19 09:13:45 +00001017static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
1018 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
1019 bool IsLoopIterVar = false) {
1020 if (DVar.RefExpr) {
1021 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1022 << getOpenMPClauseName(DVar.CKind);
1023 return;
1024 }
1025 enum {
1026 PDSA_StaticMemberShared,
1027 PDSA_StaticLocalVarShared,
1028 PDSA_LoopIterVarPrivate,
1029 PDSA_LoopIterVarLinear,
1030 PDSA_LoopIterVarLastprivate,
1031 PDSA_ConstVarShared,
1032 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001033 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001034 PDSA_LocalVarPrivate,
1035 PDSA_Implicit
1036 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001037 bool ReportHint = false;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001038 auto ReportLoc = VD->getLocation();
Alexey Bataev7ff55242014-06-19 09:13:45 +00001039 if (IsLoopIterVar) {
1040 if (DVar.CKind == OMPC_private)
1041 Reason = PDSA_LoopIterVarPrivate;
1042 else if (DVar.CKind == OMPC_lastprivate)
1043 Reason = PDSA_LoopIterVarLastprivate;
1044 else
1045 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001046 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1047 Reason = PDSA_TaskVarFirstprivate;
1048 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001049 } else if (VD->isStaticLocal())
1050 Reason = PDSA_StaticLocalVarShared;
1051 else if (VD->isStaticDataMember())
1052 Reason = PDSA_StaticMemberShared;
1053 else if (VD->isFileVarDecl())
1054 Reason = PDSA_GlobalVarShared;
1055 else if (VD->getType().isConstant(SemaRef.getASTContext()))
1056 Reason = PDSA_ConstVarShared;
Alexey Bataevbae9a792014-06-27 10:37:06 +00001057 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001058 ReportHint = true;
1059 Reason = PDSA_LocalVarPrivate;
1060 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001061 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001062 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001063 << Reason << ReportHint
1064 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1065 } else if (DVar.ImplicitDSALoc.isValid()) {
1066 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1067 << getOpenMPClauseName(DVar.CKind);
1068 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001069}
1070
Alexey Bataev758e55e2013-09-06 18:03:48 +00001071namespace {
1072class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1073 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001074 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001075 bool ErrorFound;
1076 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001077 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001078 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001079
Alexey Bataev758e55e2013-09-06 18:03:48 +00001080public:
1081 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001082 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001083 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001084 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1085 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001086
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001087 auto DVar = Stack->getTopDSA(VD, false);
1088 // Check if the variable has explicit DSA set and stop analysis if it so.
1089 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001090
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001091 auto ELoc = E->getExprLoc();
1092 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001093 // The default(none) clause requires that each variable that is referenced
1094 // in the construct, and does not have a predetermined data-sharing
1095 // attribute, must have its data-sharing attribute explicitly determined
1096 // by being listed in a data-sharing attribute clause.
1097 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001098 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001099 VarsWithInheritedDSA.count(VD) == 0) {
1100 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001101 return;
1102 }
1103
1104 // OpenMP [2.9.3.6, Restrictions, p.2]
1105 // A list item that appears in a reduction clause of the innermost
1106 // enclosing worksharing or parallel construct may not be accessed in an
1107 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001108 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001109 [](OpenMPDirectiveKind K) -> bool {
1110 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001111 isOpenMPWorksharingDirective(K) ||
1112 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001113 },
1114 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001115 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1116 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001117 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1118 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001119 return;
1120 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001121
1122 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001123 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001124 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001125 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001126 }
1127 }
1128 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001129 for (auto *C : S->clauses()) {
1130 // Skip analysis of arguments of implicitly defined firstprivate clause
1131 // for task directives.
1132 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1133 for (auto *CC : C->children()) {
1134 if (CC)
1135 Visit(CC);
1136 }
1137 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001138 }
1139 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001140 for (auto *C : S->children()) {
1141 if (C && !isa<OMPExecutableDirective>(C))
1142 Visit(C);
1143 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001144 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001145
1146 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001147 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001148 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
1149 return VarsWithInheritedDSA;
1150 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001151
Alexey Bataev7ff55242014-06-19 09:13:45 +00001152 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1153 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001154};
Alexey Bataeved09d242014-05-28 05:53:51 +00001155} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001156
Alexey Bataevbae9a792014-06-27 10:37:06 +00001157void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001158 switch (DKind) {
1159 case OMPD_parallel: {
1160 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1161 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001162 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001163 std::make_pair(".global_tid.", KmpInt32PtrTy),
1164 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1165 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001166 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001167 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1168 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001169 break;
1170 }
1171 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001172 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001173 std::make_pair(StringRef(), QualType()) // __context with shared vars
1174 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001175 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1176 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001177 break;
1178 }
1179 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001180 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001181 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001182 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001183 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1184 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001185 break;
1186 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001187 case OMPD_for_simd: {
1188 Sema::CapturedParamNameType Params[] = {
1189 std::make_pair(StringRef(), QualType()) // __context with shared vars
1190 };
1191 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1192 Params);
1193 break;
1194 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001195 case OMPD_sections: {
1196 Sema::CapturedParamNameType Params[] = {
1197 std::make_pair(StringRef(), QualType()) // __context with shared vars
1198 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001199 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1200 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001201 break;
1202 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001203 case OMPD_section: {
1204 Sema::CapturedParamNameType Params[] = {
1205 std::make_pair(StringRef(), QualType()) // __context with shared vars
1206 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001207 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1208 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001209 break;
1210 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001211 case OMPD_single: {
1212 Sema::CapturedParamNameType Params[] = {
1213 std::make_pair(StringRef(), QualType()) // __context with shared vars
1214 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001215 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1216 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001217 break;
1218 }
Alexander Musman80c22892014-07-17 08:54:58 +00001219 case OMPD_master: {
1220 Sema::CapturedParamNameType Params[] = {
1221 std::make_pair(StringRef(), QualType()) // __context with shared vars
1222 };
1223 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1224 Params);
1225 break;
1226 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001227 case OMPD_critical: {
1228 Sema::CapturedParamNameType Params[] = {
1229 std::make_pair(StringRef(), QualType()) // __context with shared vars
1230 };
1231 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1232 Params);
1233 break;
1234 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001235 case OMPD_parallel_for: {
1236 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1237 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1238 Sema::CapturedParamNameType Params[] = {
1239 std::make_pair(".global_tid.", KmpInt32PtrTy),
1240 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1241 std::make_pair(StringRef(), QualType()) // __context with shared vars
1242 };
1243 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1244 Params);
1245 break;
1246 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001247 case OMPD_parallel_for_simd: {
1248 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1249 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1250 Sema::CapturedParamNameType Params[] = {
1251 std::make_pair(".global_tid.", KmpInt32PtrTy),
1252 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1253 std::make_pair(StringRef(), QualType()) // __context with shared vars
1254 };
1255 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1256 Params);
1257 break;
1258 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001259 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001260 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1261 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001262 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001263 std::make_pair(".global_tid.", KmpInt32PtrTy),
1264 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001265 std::make_pair(StringRef(), QualType()) // __context with shared vars
1266 };
1267 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1268 Params);
1269 break;
1270 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001271 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001272 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001273 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1274 FunctionProtoType::ExtProtoInfo EPI;
1275 EPI.Variadic = true;
1276 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001277 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001278 std::make_pair(".global_tid.", KmpInt32Ty),
1279 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001280 std::make_pair(".privates.",
1281 Context.VoidPtrTy.withConst().withRestrict()),
1282 std::make_pair(
1283 ".copy_fn.",
1284 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001285 std::make_pair(StringRef(), QualType()) // __context with shared vars
1286 };
1287 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1288 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001289 // Mark this captured region as inlined, because we don't use outlined
1290 // function directly.
1291 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1292 AlwaysInlineAttr::CreateImplicit(
1293 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001294 break;
1295 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001296 case OMPD_ordered: {
1297 Sema::CapturedParamNameType Params[] = {
1298 std::make_pair(StringRef(), QualType()) // __context with shared vars
1299 };
1300 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1301 Params);
1302 break;
1303 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001304 case OMPD_atomic: {
1305 Sema::CapturedParamNameType Params[] = {
1306 std::make_pair(StringRef(), QualType()) // __context with shared vars
1307 };
1308 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1309 Params);
1310 break;
1311 }
Michael Wong65f367f2015-07-21 13:44:28 +00001312 case OMPD_target_data:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001313 case OMPD_target: {
1314 Sema::CapturedParamNameType Params[] = {
1315 std::make_pair(StringRef(), QualType()) // __context with shared vars
1316 };
1317 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1318 Params);
1319 break;
1320 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001321 case OMPD_teams: {
1322 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1323 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1324 Sema::CapturedParamNameType Params[] = {
1325 std::make_pair(".global_tid.", KmpInt32PtrTy),
1326 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1327 std::make_pair(StringRef(), QualType()) // __context with shared vars
1328 };
1329 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1330 Params);
1331 break;
1332 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001333 case OMPD_taskgroup: {
1334 Sema::CapturedParamNameType Params[] = {
1335 std::make_pair(StringRef(), QualType()) // __context with shared vars
1336 };
1337 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1338 Params);
1339 break;
1340 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001341 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001342 case OMPD_taskyield:
1343 case OMPD_barrier:
1344 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001345 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001346 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001347 case OMPD_flush:
Alexey Bataev9959db52014-05-06 10:08:46 +00001348 llvm_unreachable("OpenMP Directive is not allowed");
1349 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001350 llvm_unreachable("Unknown OpenMP directive");
1351 }
1352}
1353
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001354StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1355 ArrayRef<OMPClause *> Clauses) {
1356 if (!S.isUsable()) {
1357 ActOnCapturedRegionError();
1358 return StmtError();
1359 }
Alexey Bataev040d5402015-05-12 08:35:28 +00001360 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001361 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001362 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001363 Clause->getClauseKind() == OMPC_copyprivate ||
1364 (getLangOpts().OpenMPUseTLS &&
1365 getASTContext().getTargetInfo().isTLSSupported() &&
1366 Clause->getClauseKind() == OMPC_copyin)) {
1367 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001368 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001369 for (auto *VarRef : Clause->children()) {
1370 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001371 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001372 }
1373 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001374 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev040d5402015-05-12 08:35:28 +00001375 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
1376 Clause->getClauseKind() == OMPC_schedule) {
1377 // Mark all variables in private list clauses as used in inner region.
1378 // Required for proper codegen of combined directives.
1379 // TODO: add processing for other clauses.
1380 if (auto *E = cast_or_null<Expr>(
1381 cast<OMPScheduleClause>(Clause)->getHelperChunkSize())) {
1382 MarkDeclarationsReferencedInExpr(E);
1383 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001384 }
1385 }
1386 return ActOnCapturedRegionEnd(S.get());
1387}
1388
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001389static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1390 OpenMPDirectiveKind CurrentRegion,
1391 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001392 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001393 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001394 // Allowed nesting of constructs
1395 // +------------------+-----------------+------------------------------------+
1396 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1397 // +------------------+-----------------+------------------------------------+
1398 // | parallel | parallel | * |
1399 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001400 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001401 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001402 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001403 // | parallel | simd | * |
1404 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001405 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001406 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001407 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001408 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001409 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001410 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001411 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001412 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001413 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001414 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001415 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001416 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001417 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001418 // | parallel | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001419 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001420 // | parallel | cancellation | |
1421 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001422 // | parallel | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001423 // +------------------+-----------------+------------------------------------+
1424 // | for | parallel | * |
1425 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001426 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001427 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001428 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001429 // | for | simd | * |
1430 // | for | sections | + |
1431 // | for | section | + |
1432 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001433 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001434 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001435 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001436 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001437 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001438 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001439 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001440 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001441 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001442 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001443 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001444 // | for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001445 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001446 // | for | cancellation | |
1447 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001448 // | for | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001449 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001450 // | master | parallel | * |
1451 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001452 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001453 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001454 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001455 // | master | simd | * |
1456 // | master | sections | + |
1457 // | master | section | + |
1458 // | master | single | + |
1459 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001460 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001461 // | master |parallel sections| * |
1462 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001463 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001464 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001465 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001466 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001467 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001468 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001469 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001470 // | master | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001471 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001472 // | master | cancellation | |
1473 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001474 // | master | cancel | |
Alexander Musman80c22892014-07-17 08:54:58 +00001475 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001476 // | critical | parallel | * |
1477 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001478 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001479 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001480 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001481 // | critical | simd | * |
1482 // | critical | sections | + |
1483 // | critical | section | + |
1484 // | critical | single | + |
1485 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001486 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001487 // | critical |parallel sections| * |
1488 // | critical | task | * |
1489 // | critical | taskyield | * |
1490 // | critical | barrier | + |
1491 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001492 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001493 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001494 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001495 // | critical | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001496 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001497 // | critical | cancellation | |
1498 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001499 // | critical | cancel | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001500 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001501 // | simd | parallel | |
1502 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001503 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001504 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001505 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001506 // | simd | simd | |
1507 // | simd | sections | |
1508 // | simd | section | |
1509 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001510 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001511 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001512 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001513 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001514 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001515 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001516 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001517 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001518 // | simd | flush | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001519 // | simd | ordered | |
Alexey Bataev0162e452014-07-22 10:10:35 +00001520 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001521 // | simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001522 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001523 // | simd | cancellation | |
1524 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001525 // | simd | cancel | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001526 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001527 // | for simd | parallel | |
1528 // | for simd | for | |
1529 // | for simd | for simd | |
1530 // | for simd | master | |
1531 // | for simd | critical | |
1532 // | for simd | simd | |
1533 // | for simd | sections | |
1534 // | for simd | section | |
1535 // | for simd | single | |
1536 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001537 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001538 // | for simd |parallel sections| |
1539 // | for simd | task | |
1540 // | for simd | taskyield | |
1541 // | for simd | barrier | |
1542 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001543 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001544 // | for simd | flush | |
1545 // | for simd | ordered | |
1546 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001547 // | for simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001548 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001549 // | for simd | cancellation | |
1550 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001551 // | for simd | cancel | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001552 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001553 // | parallel for simd| parallel | |
1554 // | parallel for simd| for | |
1555 // | parallel for simd| for simd | |
1556 // | parallel for simd| master | |
1557 // | parallel for simd| critical | |
1558 // | parallel for simd| simd | |
1559 // | parallel for simd| sections | |
1560 // | parallel for simd| section | |
1561 // | parallel for simd| single | |
1562 // | parallel for simd| parallel for | |
1563 // | parallel for simd|parallel for simd| |
1564 // | parallel for simd|parallel sections| |
1565 // | parallel for simd| task | |
1566 // | parallel for simd| taskyield | |
1567 // | parallel for simd| barrier | |
1568 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001569 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001570 // | parallel for simd| flush | |
1571 // | parallel for simd| ordered | |
1572 // | parallel for simd| atomic | |
1573 // | parallel for simd| target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001574 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001575 // | parallel for simd| cancellation | |
1576 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001577 // | parallel for simd| cancel | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001578 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001579 // | sections | parallel | * |
1580 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001581 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001582 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001583 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001584 // | sections | simd | * |
1585 // | sections | sections | + |
1586 // | sections | section | * |
1587 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001588 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001589 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001590 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001591 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001592 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001593 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001594 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001595 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001596 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001597 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001598 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001599 // | sections | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001600 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001601 // | sections | cancellation | |
1602 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001603 // | sections | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001604 // +------------------+-----------------+------------------------------------+
1605 // | section | parallel | * |
1606 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001607 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001608 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001609 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001610 // | section | simd | * |
1611 // | section | sections | + |
1612 // | section | section | + |
1613 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001614 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001615 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001616 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001617 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001618 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001619 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001620 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001621 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001622 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001623 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001624 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001625 // | section | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001626 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001627 // | section | cancellation | |
1628 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001629 // | section | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001630 // +------------------+-----------------+------------------------------------+
1631 // | single | parallel | * |
1632 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001633 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001634 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001635 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001636 // | single | simd | * |
1637 // | single | sections | + |
1638 // | single | section | + |
1639 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001640 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001641 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001642 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001643 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001644 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001645 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001646 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001647 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001648 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001649 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001650 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001651 // | single | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001652 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001653 // | single | cancellation | |
1654 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001655 // | single | cancel | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001656 // +------------------+-----------------+------------------------------------+
1657 // | parallel for | parallel | * |
1658 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001659 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001660 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001661 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001662 // | parallel for | simd | * |
1663 // | parallel for | sections | + |
1664 // | parallel for | section | + |
1665 // | parallel for | single | + |
1666 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001667 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001668 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001669 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001670 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001671 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001672 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001673 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001674 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001675 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001676 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001677 // | parallel for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001678 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001679 // | parallel for | cancellation | |
1680 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001681 // | parallel for | cancel | ! |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001682 // +------------------+-----------------+------------------------------------+
1683 // | parallel sections| parallel | * |
1684 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001685 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001686 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001687 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001688 // | parallel sections| simd | * |
1689 // | parallel sections| sections | + |
1690 // | parallel sections| section | * |
1691 // | parallel sections| single | + |
1692 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001693 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001694 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001695 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001696 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001697 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001698 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001699 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001700 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001701 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001702 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001703 // | parallel sections| target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001704 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001705 // | parallel sections| cancellation | |
1706 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001707 // | parallel sections| cancel | ! |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001708 // +------------------+-----------------+------------------------------------+
1709 // | task | parallel | * |
1710 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001711 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001712 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001713 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001714 // | task | simd | * |
1715 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001716 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001717 // | task | single | + |
1718 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001719 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001720 // | task |parallel sections| * |
1721 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001722 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001723 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001724 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001725 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001726 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001727 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001728 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001729 // | task | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001730 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001731 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00001732 // | | point | ! |
1733 // | task | cancel | ! |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001734 // +------------------+-----------------+------------------------------------+
1735 // | ordered | parallel | * |
1736 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001737 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001738 // | ordered | master | * |
1739 // | ordered | critical | * |
1740 // | ordered | simd | * |
1741 // | ordered | sections | + |
1742 // | ordered | section | + |
1743 // | ordered | single | + |
1744 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001745 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001746 // | ordered |parallel sections| * |
1747 // | ordered | task | * |
1748 // | ordered | taskyield | * |
1749 // | ordered | barrier | + |
1750 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001751 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001752 // | ordered | flush | * |
1753 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001754 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001755 // | ordered | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001756 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001757 // | ordered | cancellation | |
1758 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001759 // | ordered | cancel | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001760 // +------------------+-----------------+------------------------------------+
1761 // | atomic | parallel | |
1762 // | atomic | for | |
1763 // | atomic | for simd | |
1764 // | atomic | master | |
1765 // | atomic | critical | |
1766 // | atomic | simd | |
1767 // | atomic | sections | |
1768 // | atomic | section | |
1769 // | atomic | single | |
1770 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001771 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001772 // | atomic |parallel sections| |
1773 // | atomic | task | |
1774 // | atomic | taskyield | |
1775 // | atomic | barrier | |
1776 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001777 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001778 // | atomic | flush | |
1779 // | atomic | ordered | |
1780 // | atomic | atomic | |
1781 // | atomic | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001782 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001783 // | atomic | cancellation | |
1784 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001785 // | atomic | cancel | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001786 // +------------------+-----------------+------------------------------------+
1787 // | target | parallel | * |
1788 // | target | for | * |
1789 // | target | for simd | * |
1790 // | target | master | * |
1791 // | target | critical | * |
1792 // | target | simd | * |
1793 // | target | sections | * |
1794 // | target | section | * |
1795 // | target | single | * |
1796 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001797 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001798 // | target |parallel sections| * |
1799 // | target | task | * |
1800 // | target | taskyield | * |
1801 // | target | barrier | * |
1802 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001803 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001804 // | target | flush | * |
1805 // | target | ordered | * |
1806 // | target | atomic | * |
1807 // | target | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001808 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001809 // | target | cancellation | |
1810 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001811 // | target | cancel | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001812 // +------------------+-----------------+------------------------------------+
1813 // | teams | parallel | * |
1814 // | teams | for | + |
1815 // | teams | for simd | + |
1816 // | teams | master | + |
1817 // | teams | critical | + |
1818 // | teams | simd | + |
1819 // | teams | sections | + |
1820 // | teams | section | + |
1821 // | teams | single | + |
1822 // | teams | parallel for | * |
1823 // | teams |parallel for simd| * |
1824 // | teams |parallel sections| * |
1825 // | teams | task | + |
1826 // | teams | taskyield | + |
1827 // | teams | barrier | + |
1828 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00001829 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001830 // | teams | flush | + |
1831 // | teams | ordered | + |
1832 // | teams | atomic | + |
1833 // | teams | target | + |
1834 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001835 // | teams | cancellation | |
1836 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001837 // | teams | cancel | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001838 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00001839 if (Stack->getCurScope()) {
1840 auto ParentRegion = Stack->getParentDirective();
1841 bool NestingProhibited = false;
1842 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001843 enum {
1844 NoRecommend,
1845 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00001846 ShouldBeInOrderedRegion,
1847 ShouldBeInTargetRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001848 } Recommend = NoRecommend;
Alexey Bataev549210e2014-06-24 04:39:47 +00001849 if (isOpenMPSimdDirective(ParentRegion)) {
1850 // OpenMP [2.16, Nesting of Regions]
1851 // OpenMP constructs may not be nested inside a simd region.
1852 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1853 return true;
1854 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001855 if (ParentRegion == OMPD_atomic) {
1856 // OpenMP [2.16, Nesting of Regions]
1857 // OpenMP constructs may not be nested inside an atomic region.
1858 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
1859 return true;
1860 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001861 if (CurrentRegion == OMPD_section) {
1862 // OpenMP [2.7.2, sections Construct, Restrictions]
1863 // Orphaned section directives are prohibited. That is, the section
1864 // directives must appear within the sections construct and must not be
1865 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001866 if (ParentRegion != OMPD_sections &&
1867 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001868 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1869 << (ParentRegion != OMPD_unknown)
1870 << getOpenMPDirectiveName(ParentRegion);
1871 return true;
1872 }
1873 return false;
1874 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001875 // Allow some constructs to be orphaned (they could be used in functions,
1876 // called from OpenMP regions with the required preconditions).
1877 if (ParentRegion == OMPD_unknown)
1878 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00001879 if (CurrentRegion == OMPD_cancellation_point ||
1880 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001881 // OpenMP [2.16, Nesting of Regions]
1882 // A cancellation point construct for which construct-type-clause is
1883 // taskgroup must be nested inside a task construct. A cancellation
1884 // point construct for which construct-type-clause is not taskgroup must
1885 // be closely nested inside an OpenMP construct that matches the type
1886 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00001887 // A cancel construct for which construct-type-clause is taskgroup must be
1888 // nested inside a task construct. A cancel construct for which
1889 // construct-type-clause is not taskgroup must be closely nested inside an
1890 // OpenMP construct that matches the type specified in
1891 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001892 NestingProhibited =
1893 !((CancelRegion == OMPD_parallel && ParentRegion == OMPD_parallel) ||
1894 (CancelRegion == OMPD_for && ParentRegion == OMPD_for) ||
1895 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
1896 (CancelRegion == OMPD_sections &&
1897 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections)));
1898 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00001899 // OpenMP [2.16, Nesting of Regions]
1900 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001901 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00001902 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
1903 ParentRegion == OMPD_task;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001904 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
1905 // OpenMP [2.16, Nesting of Regions]
1906 // A critical region may not be nested (closely or otherwise) inside a
1907 // critical region with the same name. Note that this restriction is not
1908 // sufficient to prevent deadlock.
1909 SourceLocation PreviousCriticalLoc;
1910 bool DeadLock =
1911 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
1912 OpenMPDirectiveKind K,
1913 const DeclarationNameInfo &DNI,
1914 SourceLocation Loc)
1915 ->bool {
1916 if (K == OMPD_critical &&
1917 DNI.getName() == CurrentName.getName()) {
1918 PreviousCriticalLoc = Loc;
1919 return true;
1920 } else
1921 return false;
1922 },
1923 false /* skip top directive */);
1924 if (DeadLock) {
1925 SemaRef.Diag(StartLoc,
1926 diag::err_omp_prohibited_region_critical_same_name)
1927 << CurrentName.getName();
1928 if (PreviousCriticalLoc.isValid())
1929 SemaRef.Diag(PreviousCriticalLoc,
1930 diag::note_omp_previous_critical_region);
1931 return true;
1932 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001933 } else if (CurrentRegion == OMPD_barrier) {
1934 // OpenMP [2.16, Nesting of Regions]
1935 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001936 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001937 NestingProhibited =
1938 isOpenMPWorksharingDirective(ParentRegion) ||
1939 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1940 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00001941 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00001942 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00001943 // OpenMP [2.16, Nesting of Regions]
1944 // A worksharing region may not be closely nested inside a worksharing,
1945 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001946 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00001947 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001948 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1949 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
1950 Recommend = ShouldBeInParallelRegion;
1951 } else if (CurrentRegion == OMPD_ordered) {
1952 // OpenMP [2.16, Nesting of Regions]
1953 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00001954 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001955 // An ordered region must be closely nested inside a loop region (or
1956 // parallel loop region) with an ordered clause.
1957 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00001958 ParentRegion == OMPD_task ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001959 !Stack->isParentOrderedRegion();
1960 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00001961 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
1962 // OpenMP [2.16, Nesting of Regions]
1963 // If specified, a teams construct must be contained within a target
1964 // construct.
1965 NestingProhibited = ParentRegion != OMPD_target;
1966 Recommend = ShouldBeInTargetRegion;
1967 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
1968 }
1969 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
1970 // OpenMP [2.16, Nesting of Regions]
1971 // distribute, parallel, parallel sections, parallel workshare, and the
1972 // parallel loop and parallel loop SIMD constructs are the only OpenMP
1973 // constructs that can be closely nested in the teams region.
1974 // TODO: add distribute directive.
1975 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion);
1976 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00001977 }
1978 if (NestingProhibited) {
1979 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001980 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
1981 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00001982 return true;
1983 }
1984 }
1985 return false;
1986}
1987
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001988StmtResult Sema::ActOnOpenMPExecutableDirective(
1989 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
1990 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
1991 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001992 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001993 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
1994 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00001995 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001996
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001997 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00001998 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001999 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002000 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002001 if (AStmt) {
2002 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2003
2004 // Check default data sharing attributes for referenced variables.
2005 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2006 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2007 if (DSAChecker.isErrorFound())
2008 return StmtError();
2009 // Generate list of implicitly defined firstprivate variables.
2010 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002011
2012 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2013 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2014 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2015 SourceLocation(), SourceLocation())) {
2016 ClausesWithImplicit.push_back(Implicit);
2017 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2018 DSAChecker.getImplicitFirstprivate().size();
2019 } else
2020 ErrorFound = true;
2021 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002022 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002023
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002024 switch (Kind) {
2025 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002026 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2027 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002028 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002029 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002030 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2031 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002032 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002033 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002034 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2035 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002036 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002037 case OMPD_for_simd:
2038 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2039 EndLoc, VarsWithInheritedDSA);
2040 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002041 case OMPD_sections:
2042 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2043 EndLoc);
2044 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002045 case OMPD_section:
2046 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002047 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002048 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2049 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002050 case OMPD_single:
2051 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2052 EndLoc);
2053 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002054 case OMPD_master:
2055 assert(ClausesWithImplicit.empty() &&
2056 "No clauses are allowed for 'omp master' directive");
2057 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2058 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002059 case OMPD_critical:
2060 assert(ClausesWithImplicit.empty() &&
2061 "No clauses are allowed for 'omp critical' directive");
2062 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
2063 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002064 case OMPD_parallel_for:
2065 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2066 EndLoc, VarsWithInheritedDSA);
2067 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002068 case OMPD_parallel_for_simd:
2069 Res = ActOnOpenMPParallelForSimdDirective(
2070 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2071 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002072 case OMPD_parallel_sections:
2073 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2074 StartLoc, EndLoc);
2075 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002076 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002077 Res =
2078 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2079 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002080 case OMPD_taskyield:
2081 assert(ClausesWithImplicit.empty() &&
2082 "No clauses are allowed for 'omp taskyield' directive");
2083 assert(AStmt == nullptr &&
2084 "No associated statement allowed for 'omp taskyield' directive");
2085 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2086 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002087 case OMPD_barrier:
2088 assert(ClausesWithImplicit.empty() &&
2089 "No clauses are allowed for 'omp barrier' directive");
2090 assert(AStmt == nullptr &&
2091 "No associated statement allowed for 'omp barrier' directive");
2092 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2093 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00002094 case OMPD_taskwait:
2095 assert(ClausesWithImplicit.empty() &&
2096 "No clauses are allowed for 'omp taskwait' directive");
2097 assert(AStmt == nullptr &&
2098 "No associated statement allowed for 'omp taskwait' directive");
2099 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2100 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002101 case OMPD_taskgroup:
2102 assert(ClausesWithImplicit.empty() &&
2103 "No clauses are allowed for 'omp taskgroup' directive");
2104 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
2105 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00002106 case OMPD_flush:
2107 assert(AStmt == nullptr &&
2108 "No associated statement allowed for 'omp flush' directive");
2109 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2110 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002111 case OMPD_ordered:
2112 assert(ClausesWithImplicit.empty() &&
2113 "No clauses are allowed for 'omp ordered' directive");
2114 Res = ActOnOpenMPOrderedDirective(AStmt, StartLoc, EndLoc);
2115 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00002116 case OMPD_atomic:
2117 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2118 EndLoc);
2119 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002120 case OMPD_teams:
2121 Res =
2122 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2123 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002124 case OMPD_target:
2125 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2126 EndLoc);
2127 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002128 case OMPD_cancellation_point:
2129 assert(ClausesWithImplicit.empty() &&
2130 "No clauses are allowed for 'omp cancellation point' directive");
2131 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
2132 "cancellation point' directive");
2133 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2134 break;
Alexey Bataev80909872015-07-02 11:25:17 +00002135 case OMPD_cancel:
2136 assert(ClausesWithImplicit.empty() &&
2137 "No clauses are allowed for 'omp cancel' directive");
2138 assert(AStmt == nullptr &&
2139 "No associated statement allowed for 'omp cancel' directive");
2140 Res = ActOnOpenMPCancelDirective(StartLoc, EndLoc, CancelRegion);
2141 break;
Michael Wong65f367f2015-07-21 13:44:28 +00002142 case OMPD_target_data:
2143 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
2144 EndLoc);
2145 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002146 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002147 llvm_unreachable("OpenMP Directive is not allowed");
2148 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002149 llvm_unreachable("Unknown OpenMP directive");
2150 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002151
Alexey Bataev4acb8592014-07-07 13:01:15 +00002152 for (auto P : VarsWithInheritedDSA) {
2153 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
2154 << P.first << P.second->getSourceRange();
2155 }
2156 if (!VarsWithInheritedDSA.empty())
2157 return StmtError();
2158
Alexey Bataeved09d242014-05-28 05:53:51 +00002159 if (ErrorFound)
2160 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002161 return Res;
2162}
2163
2164StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
2165 Stmt *AStmt,
2166 SourceLocation StartLoc,
2167 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00002168 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2169 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2170 // 1.2.2 OpenMP Language Terminology
2171 // Structured block - An executable statement with a single entry at the
2172 // top and a single exit at the bottom.
2173 // The point of exit cannot be a branch out of the structured block.
2174 // longjmp() and throw() must not violate the entry/exit criteria.
2175 CS->getCapturedDecl()->setNothrow();
2176
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002177 getCurFunction()->setHasBranchProtectedScope();
2178
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00002179 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
2180 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002181}
2182
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002183namespace {
2184/// \brief Helper class for checking canonical form of the OpenMP loops and
2185/// extracting iteration space of each loop in the loop nest, that will be used
2186/// for IR generation.
2187class OpenMPIterationSpaceChecker {
2188 /// \brief Reference to Sema.
2189 Sema &SemaRef;
2190 /// \brief A location for diagnostics (when there is no some better location).
2191 SourceLocation DefaultLoc;
2192 /// \brief A location for diagnostics (when increment is not compatible).
2193 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002194 /// \brief A source location for referring to loop init later.
2195 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002196 /// \brief A source location for referring to condition later.
2197 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002198 /// \brief A source location for referring to increment later.
2199 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002200 /// \brief Loop variable.
2201 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002202 /// \brief Reference to loop variable.
2203 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002204 /// \brief Lower bound (initializer for the var).
2205 Expr *LB;
2206 /// \brief Upper bound.
2207 Expr *UB;
2208 /// \brief Loop step (increment).
2209 Expr *Step;
2210 /// \brief This flag is true when condition is one of:
2211 /// Var < UB
2212 /// Var <= UB
2213 /// UB > Var
2214 /// UB >= Var
2215 bool TestIsLessOp;
2216 /// \brief This flag is true when condition is strict ( < or > ).
2217 bool TestIsStrictOp;
2218 /// \brief This flag is true when step is subtracted on each iteration.
2219 bool SubtractStep;
2220
2221public:
2222 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
2223 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00002224 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
2225 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002226 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
2227 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002228 /// \brief Check init-expr for canonical loop form and save loop counter
2229 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00002230 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002231 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
2232 /// for less/greater and for strict/non-strict comparison.
2233 bool CheckCond(Expr *S);
2234 /// \brief Check incr-expr for canonical loop form and return true if it
2235 /// does not conform, otherwise save loop step (#Step).
2236 bool CheckInc(Expr *S);
2237 /// \brief Return the loop counter variable.
2238 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002239 /// \brief Return the reference expression to loop counter variable.
2240 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002241 /// \brief Source range of the loop init.
2242 SourceRange GetInitSrcRange() const { return InitSrcRange; }
2243 /// \brief Source range of the loop condition.
2244 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
2245 /// \brief Source range of the loop increment.
2246 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
2247 /// \brief True if the step should be subtracted.
2248 bool ShouldSubtractStep() const { return SubtractStep; }
2249 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002250 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00002251 /// \brief Build the precondition expression for the loops.
2252 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002253 /// \brief Build reference expression to the counter be used for codegen.
2254 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00002255 /// \brief Build reference expression to the private counter be used for
2256 /// codegen.
2257 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002258 /// \brief Build initization of the counter be used for codegen.
2259 Expr *BuildCounterInit() const;
2260 /// \brief Build step of the counter be used for codegen.
2261 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002262 /// \brief Return true if any expression is dependent.
2263 bool Dependent() const;
2264
2265private:
2266 /// \brief Check the right-hand side of an assignment in the increment
2267 /// expression.
2268 bool CheckIncRHS(Expr *RHS);
2269 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002270 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002271 /// \brief Helper to set upper bound.
2272 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
2273 const SourceLocation &SL);
2274 /// \brief Helper to set loop increment.
2275 bool SetStep(Expr *NewStep, bool Subtract);
2276};
2277
2278bool OpenMPIterationSpaceChecker::Dependent() const {
2279 if (!Var) {
2280 assert(!LB && !UB && !Step);
2281 return false;
2282 }
2283 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
2284 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
2285}
2286
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002287template <typename T>
2288static T *getExprAsWritten(T *E) {
2289 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
2290 E = ExprTemp->getSubExpr();
2291
2292 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
2293 E = MTE->GetTemporaryExpr();
2294
2295 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
2296 E = Binder->getSubExpr();
2297
2298 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
2299 E = ICE->getSubExprAsWritten();
2300 return E->IgnoreParens();
2301}
2302
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002303bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
2304 DeclRefExpr *NewVarRefExpr,
2305 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002306 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002307 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
2308 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002309 if (!NewVar || !NewLB)
2310 return true;
2311 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002312 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002313 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
2314 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002315 if ((Ctor->isCopyOrMoveConstructor() ||
2316 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2317 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002318 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002319 LB = NewLB;
2320 return false;
2321}
2322
2323bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
2324 const SourceRange &SR,
2325 const SourceLocation &SL) {
2326 // State consistency checking to ensure correct usage.
2327 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
2328 !TestIsLessOp && !TestIsStrictOp);
2329 if (!NewUB)
2330 return true;
2331 UB = NewUB;
2332 TestIsLessOp = LessOp;
2333 TestIsStrictOp = StrictOp;
2334 ConditionSrcRange = SR;
2335 ConditionLoc = SL;
2336 return false;
2337}
2338
2339bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
2340 // State consistency checking to ensure correct usage.
2341 assert(Var != nullptr && LB != nullptr && Step == nullptr);
2342 if (!NewStep)
2343 return true;
2344 if (!NewStep->isValueDependent()) {
2345 // Check that the step is integer expression.
2346 SourceLocation StepLoc = NewStep->getLocStart();
2347 ExprResult Val =
2348 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
2349 if (Val.isInvalid())
2350 return true;
2351 NewStep = Val.get();
2352
2353 // OpenMP [2.6, Canonical Loop Form, Restrictions]
2354 // If test-expr is of form var relational-op b and relational-op is < or
2355 // <= then incr-expr must cause var to increase on each iteration of the
2356 // loop. If test-expr is of form var relational-op b and relational-op is
2357 // > or >= then incr-expr must cause var to decrease on each iteration of
2358 // the loop.
2359 // If test-expr is of form b relational-op var and relational-op is < or
2360 // <= then incr-expr must cause var to decrease on each iteration of the
2361 // loop. If test-expr is of form b relational-op var and relational-op is
2362 // > or >= then incr-expr must cause var to increase on each iteration of
2363 // the loop.
2364 llvm::APSInt Result;
2365 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
2366 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
2367 bool IsConstNeg =
2368 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002369 bool IsConstPos =
2370 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002371 bool IsConstZero = IsConstant && !Result.getBoolValue();
2372 if (UB && (IsConstZero ||
2373 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00002374 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002375 SemaRef.Diag(NewStep->getExprLoc(),
2376 diag::err_omp_loop_incr_not_compatible)
2377 << Var << TestIsLessOp << NewStep->getSourceRange();
2378 SemaRef.Diag(ConditionLoc,
2379 diag::note_omp_loop_cond_requres_compatible_incr)
2380 << TestIsLessOp << ConditionSrcRange;
2381 return true;
2382 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002383 if (TestIsLessOp == Subtract) {
2384 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
2385 NewStep).get();
2386 Subtract = !Subtract;
2387 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002388 }
2389
2390 Step = NewStep;
2391 SubtractStep = Subtract;
2392 return false;
2393}
2394
Alexey Bataev9c821032015-04-30 04:23:23 +00002395bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002396 // Check init-expr for canonical loop form and save loop counter
2397 // variable - #Var and its initialization value - #LB.
2398 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
2399 // var = lb
2400 // integer-type var = lb
2401 // random-access-iterator-type var = lb
2402 // pointer-type var = lb
2403 //
2404 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00002405 if (EmitDiags) {
2406 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
2407 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002408 return true;
2409 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002410 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002411 if (Expr *E = dyn_cast<Expr>(S))
2412 S = E->IgnoreParens();
2413 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2414 if (BO->getOpcode() == BO_Assign)
2415 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002416 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002417 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002418 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
2419 if (DS->isSingleDecl()) {
2420 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00002421 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002422 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00002423 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002424 SemaRef.Diag(S->getLocStart(),
2425 diag::ext_omp_loop_not_canonical_init)
2426 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002427 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002428 }
2429 }
2430 }
2431 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
2432 if (CE->getOperator() == OO_Equal)
2433 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002434 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2435 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002436
Alexey Bataev9c821032015-04-30 04:23:23 +00002437 if (EmitDiags) {
2438 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
2439 << S->getSourceRange();
2440 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002441 return true;
2442}
2443
Alexey Bataev23b69422014-06-18 07:08:49 +00002444/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002445/// variable (which may be the loop variable) if possible.
2446static const VarDecl *GetInitVarDecl(const Expr *E) {
2447 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00002448 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002449 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002450 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
2451 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002452 if ((Ctor->isCopyOrMoveConstructor() ||
2453 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2454 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002455 E = CE->getArg(0)->IgnoreParenImpCasts();
2456 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
2457 if (!DRE)
2458 return nullptr;
2459 return dyn_cast<VarDecl>(DRE->getDecl());
2460}
2461
2462bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
2463 // Check test-expr for canonical form, save upper-bound UB, flags for
2464 // less/greater and for strict/non-strict comparison.
2465 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2466 // var relational-op b
2467 // b relational-op var
2468 //
2469 if (!S) {
2470 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
2471 return true;
2472 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002473 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002474 SourceLocation CondLoc = S->getLocStart();
2475 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2476 if (BO->isRelationalOp()) {
2477 if (GetInitVarDecl(BO->getLHS()) == Var)
2478 return SetUB(BO->getRHS(),
2479 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
2480 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2481 BO->getSourceRange(), BO->getOperatorLoc());
2482 if (GetInitVarDecl(BO->getRHS()) == Var)
2483 return SetUB(BO->getLHS(),
2484 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
2485 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2486 BO->getSourceRange(), BO->getOperatorLoc());
2487 }
2488 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2489 if (CE->getNumArgs() == 2) {
2490 auto Op = CE->getOperator();
2491 switch (Op) {
2492 case OO_Greater:
2493 case OO_GreaterEqual:
2494 case OO_Less:
2495 case OO_LessEqual:
2496 if (GetInitVarDecl(CE->getArg(0)) == Var)
2497 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
2498 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2499 CE->getOperatorLoc());
2500 if (GetInitVarDecl(CE->getArg(1)) == Var)
2501 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
2502 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2503 CE->getOperatorLoc());
2504 break;
2505 default:
2506 break;
2507 }
2508 }
2509 }
2510 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
2511 << S->getSourceRange() << Var;
2512 return true;
2513}
2514
2515bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
2516 // RHS of canonical loop form increment can be:
2517 // var + incr
2518 // incr + var
2519 // var - incr
2520 //
2521 RHS = RHS->IgnoreParenImpCasts();
2522 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
2523 if (BO->isAdditiveOp()) {
2524 bool IsAdd = BO->getOpcode() == BO_Add;
2525 if (GetInitVarDecl(BO->getLHS()) == Var)
2526 return SetStep(BO->getRHS(), !IsAdd);
2527 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
2528 return SetStep(BO->getLHS(), false);
2529 }
2530 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
2531 bool IsAdd = CE->getOperator() == OO_Plus;
2532 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
2533 if (GetInitVarDecl(CE->getArg(0)) == Var)
2534 return SetStep(CE->getArg(1), !IsAdd);
2535 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
2536 return SetStep(CE->getArg(0), false);
2537 }
2538 }
2539 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2540 << RHS->getSourceRange() << Var;
2541 return true;
2542}
2543
2544bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
2545 // Check incr-expr for canonical loop form and return true if it
2546 // does not conform.
2547 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2548 // ++var
2549 // var++
2550 // --var
2551 // var--
2552 // var += incr
2553 // var -= incr
2554 // var = var + incr
2555 // var = incr + var
2556 // var = var - incr
2557 //
2558 if (!S) {
2559 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
2560 return true;
2561 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002562 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002563 S = S->IgnoreParens();
2564 if (auto UO = dyn_cast<UnaryOperator>(S)) {
2565 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
2566 return SetStep(
2567 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
2568 (UO->isDecrementOp() ? -1 : 1)).get(),
2569 false);
2570 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
2571 switch (BO->getOpcode()) {
2572 case BO_AddAssign:
2573 case BO_SubAssign:
2574 if (GetInitVarDecl(BO->getLHS()) == Var)
2575 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
2576 break;
2577 case BO_Assign:
2578 if (GetInitVarDecl(BO->getLHS()) == Var)
2579 return CheckIncRHS(BO->getRHS());
2580 break;
2581 default:
2582 break;
2583 }
2584 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2585 switch (CE->getOperator()) {
2586 case OO_PlusPlus:
2587 case OO_MinusMinus:
2588 if (GetInitVarDecl(CE->getArg(0)) == Var)
2589 return SetStep(
2590 SemaRef.ActOnIntegerConstant(
2591 CE->getLocStart(),
2592 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
2593 false);
2594 break;
2595 case OO_PlusEqual:
2596 case OO_MinusEqual:
2597 if (GetInitVarDecl(CE->getArg(0)) == Var)
2598 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
2599 break;
2600 case OO_Equal:
2601 if (GetInitVarDecl(CE->getArg(0)) == Var)
2602 return CheckIncRHS(CE->getArg(1));
2603 break;
2604 default:
2605 break;
2606 }
2607 }
2608 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2609 << S->getSourceRange() << Var;
2610 return true;
2611}
Alexander Musmana5f070a2014-10-01 06:03:56 +00002612
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002613namespace {
2614// Transform variables declared in GNU statement expressions to new ones to
2615// avoid crash on codegen.
2616class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
2617 typedef TreeTransform<TransformToNewDefs> BaseTransform;
2618
2619public:
2620 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
2621
2622 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
2623 if (auto *VD = cast<VarDecl>(D))
2624 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
2625 !isa<ImplicitParamDecl>(D)) {
2626 auto *NewVD = VarDecl::Create(
2627 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
2628 VD->getLocation(), VD->getIdentifier(), VD->getType(),
2629 VD->getTypeSourceInfo(), VD->getStorageClass());
2630 NewVD->setTSCSpec(VD->getTSCSpec());
2631 NewVD->setInit(VD->getInit());
2632 NewVD->setInitStyle(VD->getInitStyle());
2633 NewVD->setExceptionVariable(VD->isExceptionVariable());
2634 NewVD->setNRVOVariable(VD->isNRVOVariable());
2635 NewVD->setCXXForRangeDecl(VD->isInExternCXXContext());
2636 NewVD->setConstexpr(VD->isConstexpr());
2637 NewVD->setInitCapture(VD->isInitCapture());
2638 NewVD->setPreviousDeclInSameBlockScope(
2639 VD->isPreviousDeclInSameBlockScope());
2640 VD->getDeclContext()->addHiddenDecl(NewVD);
2641 transformedLocalDecl(VD, NewVD);
2642 return NewVD;
2643 }
2644 return BaseTransform::TransformDefinition(Loc, D);
2645 }
2646
2647 ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
2648 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
2649 if (E->getDecl() != NewD) {
2650 NewD->setReferenced();
2651 NewD->markUsed(SemaRef.Context);
2652 return DeclRefExpr::Create(
2653 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
2654 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
2655 E->getNameInfo(), E->getType(), E->getValueKind());
2656 }
2657 return BaseTransform::TransformDeclRefExpr(E);
2658 }
2659};
2660}
2661
Alexander Musmana5f070a2014-10-01 06:03:56 +00002662/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002663Expr *
2664OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
2665 const bool LimitedType) const {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002666 TransformToNewDefs Transform(SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00002667 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002668 auto VarType = Var->getType().getNonReferenceType();
2669 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00002670 SemaRef.getLangOpts().CPlusPlus) {
2671 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002672 auto *UBExpr = TestIsLessOp ? UB : LB;
2673 auto *LBExpr = TestIsLessOp ? LB : UB;
2674 Expr *Upper = Transform.TransformExpr(UBExpr).get();
2675 Expr *Lower = Transform.TransformExpr(LBExpr).get();
2676 if (!Upper || !Lower)
2677 return nullptr;
2678 Upper = SemaRef.PerformImplicitConversion(Upper, UBExpr->getType(),
2679 Sema::AA_Converting,
2680 /*AllowExplicit=*/true)
2681 .get();
2682 Lower = SemaRef.PerformImplicitConversion(Lower, LBExpr->getType(),
2683 Sema::AA_Converting,
2684 /*AllowExplicit=*/true)
2685 .get();
2686 if (!Upper || !Lower)
2687 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002688
2689 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
2690
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002691 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002692 // BuildBinOp already emitted error, this one is to point user to upper
2693 // and lower bound, and to tell what is passed to 'operator-'.
2694 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
2695 << Upper->getSourceRange() << Lower->getSourceRange();
2696 return nullptr;
2697 }
2698 }
2699
2700 if (!Diff.isUsable())
2701 return nullptr;
2702
2703 // Upper - Lower [- 1]
2704 if (TestIsStrictOp)
2705 Diff = SemaRef.BuildBinOp(
2706 S, DefaultLoc, BO_Sub, Diff.get(),
2707 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2708 if (!Diff.isUsable())
2709 return nullptr;
2710
2711 // Upper - Lower [- 1] + Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002712 auto NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
2713 if (NewStep.isInvalid())
2714 return nullptr;
2715 NewStep = SemaRef.PerformImplicitConversion(
2716 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
2717 /*AllowExplicit=*/true);
2718 if (NewStep.isInvalid())
2719 return nullptr;
2720 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002721 if (!Diff.isUsable())
2722 return nullptr;
2723
2724 // Parentheses (for dumping/debugging purposes only).
2725 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
2726 if (!Diff.isUsable())
2727 return nullptr;
2728
2729 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002730 NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
2731 if (NewStep.isInvalid())
2732 return nullptr;
2733 NewStep = SemaRef.PerformImplicitConversion(
2734 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
2735 /*AllowExplicit=*/true);
2736 if (NewStep.isInvalid())
2737 return nullptr;
2738 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002739 if (!Diff.isUsable())
2740 return nullptr;
2741
Alexander Musman174b3ca2014-10-06 11:16:29 +00002742 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002743 QualType Type = Diff.get()->getType();
2744 auto &C = SemaRef.Context;
2745 bool UseVarType = VarType->hasIntegerRepresentation() &&
2746 C.getTypeSize(Type) > C.getTypeSize(VarType);
2747 if (!Type->isIntegerType() || UseVarType) {
2748 unsigned NewSize =
2749 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
2750 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
2751 : Type->hasSignedIntegerRepresentation();
2752 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
2753 Diff = SemaRef.PerformImplicitConversion(
2754 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
2755 if (!Diff.isUsable())
2756 return nullptr;
2757 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00002758 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00002759 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
2760 if (NewSize != C.getTypeSize(Type)) {
2761 if (NewSize < C.getTypeSize(Type)) {
2762 assert(NewSize == 64 && "incorrect loop var size");
2763 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
2764 << InitSrcRange << ConditionSrcRange;
2765 }
2766 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002767 NewSize, Type->hasSignedIntegerRepresentation() ||
2768 C.getTypeSize(Type) < NewSize);
Alexander Musman174b3ca2014-10-06 11:16:29 +00002769 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
2770 Sema::AA_Converting, true);
2771 if (!Diff.isUsable())
2772 return nullptr;
2773 }
2774 }
2775
Alexander Musmana5f070a2014-10-01 06:03:56 +00002776 return Diff.get();
2777}
2778
Alexey Bataev62dbb972015-04-22 11:59:37 +00002779Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
2780 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
2781 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
2782 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002783 TransformToNewDefs Transform(SemaRef);
2784
2785 auto NewLB = Transform.TransformExpr(LB);
2786 auto NewUB = Transform.TransformExpr(UB);
2787 if (NewLB.isInvalid() || NewUB.isInvalid())
2788 return Cond;
2789 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
2790 Sema::AA_Converting,
2791 /*AllowExplicit=*/true);
2792 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
2793 Sema::AA_Converting,
2794 /*AllowExplicit=*/true);
2795 if (NewLB.isInvalid() || NewUB.isInvalid())
2796 return Cond;
Alexey Bataev62dbb972015-04-22 11:59:37 +00002797 auto CondExpr = SemaRef.BuildBinOp(
2798 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
2799 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002800 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002801 if (CondExpr.isUsable()) {
2802 CondExpr = SemaRef.PerformImplicitConversion(
2803 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
2804 /*AllowExplicit=*/true);
2805 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00002806 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
2807 // Otherwise use original loop conditon and evaluate it in runtime.
2808 return CondExpr.isUsable() ? CondExpr.get() : Cond;
2809}
2810
Alexander Musmana5f070a2014-10-01 06:03:56 +00002811/// \brief Build reference expression to the counter be used for codegen.
2812Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00002813 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
2814 DefaultLoc);
2815}
2816
2817Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
2818 if (Var && !Var->isInvalidDecl()) {
2819 auto Type = Var->getType().getNonReferenceType();
2820 auto *PrivateVar = buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName());
2821 if (PrivateVar->isInvalidDecl())
2822 return nullptr;
2823 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
2824 }
2825 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002826}
2827
2828/// \brief Build initization of the counter be used for codegen.
2829Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
2830
2831/// \brief Build step of the counter be used for codegen.
2832Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
2833
2834/// \brief Iteration space of a single for loop.
2835struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00002836 /// \brief Condition of the loop.
2837 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002838 /// \brief This expression calculates the number of iterations in the loop.
2839 /// It is always possible to calculate it before starting the loop.
2840 Expr *NumIterations;
2841 /// \brief The loop counter variable.
2842 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00002843 /// \brief Private loop counter variable.
2844 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002845 /// \brief This is initializer for the initial value of #CounterVar.
2846 Expr *CounterInit;
2847 /// \brief This is step for the #CounterVar used to generate its update:
2848 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
2849 Expr *CounterStep;
2850 /// \brief Should step be subtracted?
2851 bool Subtract;
2852 /// \brief Source range of the loop init.
2853 SourceRange InitSrcRange;
2854 /// \brief Source range of the loop condition.
2855 SourceRange CondSrcRange;
2856 /// \brief Source range of the loop increment.
2857 SourceRange IncSrcRange;
2858};
2859
Alexey Bataev23b69422014-06-18 07:08:49 +00002860} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002861
Alexey Bataev9c821032015-04-30 04:23:23 +00002862void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
2863 assert(getLangOpts().OpenMP && "OpenMP is not active.");
2864 assert(Init && "Expected loop in canonical form.");
2865 unsigned CollapseIteration = DSAStack->getCollapseNumber();
2866 if (CollapseIteration > 0 &&
2867 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
2868 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
2869 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
2870 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
2871 }
2872 DSAStack->setCollapseNumber(CollapseIteration - 1);
2873 }
2874}
2875
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002876/// \brief Called on a for stmt to check and extract its iteration space
2877/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00002878static bool CheckOpenMPIterationSpace(
2879 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
2880 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00002881 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002882 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
2883 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002884 // OpenMP [2.6, Canonical Loop Form]
2885 // for (init-expr; test-expr; incr-expr) structured-block
2886 auto For = dyn_cast_or_null<ForStmt>(S);
2887 if (!For) {
2888 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00002889 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
2890 << getOpenMPDirectiveName(DKind) << NestedLoopCount
2891 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
2892 if (NestedLoopCount > 1) {
2893 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
2894 SemaRef.Diag(DSA.getConstructLoc(),
2895 diag::note_omp_collapse_ordered_expr)
2896 << 2 << CollapseLoopCountExpr->getSourceRange()
2897 << OrderedLoopCountExpr->getSourceRange();
2898 else if (CollapseLoopCountExpr)
2899 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
2900 diag::note_omp_collapse_ordered_expr)
2901 << 0 << CollapseLoopCountExpr->getSourceRange();
2902 else
2903 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
2904 diag::note_omp_collapse_ordered_expr)
2905 << 1 << OrderedLoopCountExpr->getSourceRange();
2906 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002907 return true;
2908 }
2909 assert(For->getBody());
2910
2911 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
2912
2913 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002914 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002915 if (ISC.CheckInit(Init)) {
2916 return true;
2917 }
2918
2919 bool HasErrors = false;
2920
2921 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002922 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002923
2924 // OpenMP [2.6, Canonical Loop Form]
2925 // Var is one of the following:
2926 // A variable of signed or unsigned integer type.
2927 // For C++, a variable of a random access iterator type.
2928 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00002929 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002930 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
2931 !VarType->isPointerType() &&
2932 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
2933 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
2934 << SemaRef.getLangOpts().CPlusPlus;
2935 HasErrors = true;
2936 }
2937
Alexey Bataev4acb8592014-07-07 13:01:15 +00002938 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
2939 // Construct
2940 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2941 // parallel for construct is (are) private.
2942 // The loop iteration variable in the associated for-loop of a simd construct
2943 // with just one associated for-loop is linear with a constant-linear-step
2944 // that is the increment of the associated for-loop.
2945 // Exclude loop var from the list of variables with implicitly defined data
2946 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00002947 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002948
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002949 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
2950 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00002951 // The loop iteration variable in the associated for-loop of a simd construct
2952 // with just one associated for-loop may be listed in a linear clause with a
2953 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002954 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2955 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002956 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002957 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
2958 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
2959 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002960 auto PredeterminedCKind =
2961 isOpenMPSimdDirective(DKind)
2962 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
2963 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002964 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00002965 DVar.CKind != OMPC_threadprivate && DVar.CKind != PredeterminedCKind) ||
Alexander Musmanf82886e2014-09-18 05:12:34 +00002966 (isOpenMPWorksharingDirective(DKind) && !isOpenMPSimdDirective(DKind) &&
2967 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00002968 DVar.CKind != OMPC_lastprivate && DVar.CKind != OMPC_threadprivate)) &&
2969 ((DVar.CKind != OMPC_private && DVar.CKind != OMPC_threadprivate) ||
2970 DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002971 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00002972 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
2973 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00002974 if (DVar.RefExpr == nullptr)
2975 DVar.CKind = PredeterminedCKind;
2976 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002977 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002978 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002979 // Make the loop iteration variable private (for worksharing constructs),
2980 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00002981 // lastprivate (for simd directives with several collapsed or ordered
2982 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00002983 if (DVar.CKind == OMPC_unknown)
2984 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
2985 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00002986 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002987 }
2988
Alexey Bataev7ff55242014-06-19 09:13:45 +00002989 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00002990
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002991 // Check test-expr.
2992 HasErrors |= ISC.CheckCond(For->getCond());
2993
2994 // Check incr-expr.
2995 HasErrors |= ISC.CheckInc(For->getInc());
2996
Alexander Musmana5f070a2014-10-01 06:03:56 +00002997 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002998 return HasErrors;
2999
Alexander Musmana5f070a2014-10-01 06:03:56 +00003000 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003001 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00003002 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
3003 DSA.getCurScope(), /* LimitedType */ isOpenMPWorksharingDirective(DKind));
Alexander Musmana5f070a2014-10-01 06:03:56 +00003004 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00003005 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003006 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
3007 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
3008 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
3009 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
3010 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
3011 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
3012
Alexey Bataev62dbb972015-04-22 11:59:37 +00003013 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
3014 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003015 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00003016 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003017 ResultIterSpace.CounterInit == nullptr ||
3018 ResultIterSpace.CounterStep == nullptr);
3019
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003020 return HasErrors;
3021}
3022
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003023/// \brief Build 'VarRef = Start.
3024static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
3025 ExprResult VarRef, ExprResult Start) {
3026 TransformToNewDefs Transform(SemaRef);
3027 // Build 'VarRef = Start.
3028 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3029 if (NewStart.isInvalid())
3030 return ExprError();
3031 NewStart = SemaRef.PerformImplicitConversion(
3032 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3033 Sema::AA_Converting,
3034 /*AllowExplicit=*/true);
3035 if (NewStart.isInvalid())
3036 return ExprError();
3037 NewStart = SemaRef.PerformImplicitConversion(
3038 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
3039 /*AllowExplicit=*/true);
3040 if (!NewStart.isUsable())
3041 return ExprError();
3042
3043 auto Init =
3044 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
3045 return Init;
3046}
3047
Alexander Musmana5f070a2014-10-01 06:03:56 +00003048/// \brief Build 'VarRef = Start + Iter * Step'.
3049static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
3050 SourceLocation Loc, ExprResult VarRef,
3051 ExprResult Start, ExprResult Iter,
3052 ExprResult Step, bool Subtract) {
3053 // Add parentheses (for debugging purposes only).
3054 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
3055 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
3056 !Step.isUsable())
3057 return ExprError();
3058
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003059 TransformToNewDefs Transform(SemaRef);
3060 auto NewStep = Transform.TransformExpr(Step.get()->IgnoreImplicit());
3061 if (NewStep.isInvalid())
3062 return ExprError();
3063 NewStep = SemaRef.PerformImplicitConversion(
3064 NewStep.get(), Step.get()->IgnoreImplicit()->getType(),
3065 Sema::AA_Converting,
3066 /*AllowExplicit=*/true);
3067 if (NewStep.isInvalid())
3068 return ExprError();
3069 ExprResult Update =
3070 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003071 if (!Update.isUsable())
3072 return ExprError();
3073
3074 // Build 'VarRef = Start + Iter * Step'.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003075 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3076 if (NewStart.isInvalid())
3077 return ExprError();
3078 NewStart = SemaRef.PerformImplicitConversion(
3079 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3080 Sema::AA_Converting,
3081 /*AllowExplicit=*/true);
3082 if (NewStart.isInvalid())
3083 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003084 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003085 NewStart.get(), Update.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003086 if (!Update.isUsable())
3087 return ExprError();
3088
3089 Update = SemaRef.PerformImplicitConversion(
3090 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
3091 if (!Update.isUsable())
3092 return ExprError();
3093
3094 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
3095 return Update;
3096}
3097
3098/// \brief Convert integer expression \a E to make it have at least \a Bits
3099/// bits.
3100static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
3101 Sema &SemaRef) {
3102 if (E == nullptr)
3103 return ExprError();
3104 auto &C = SemaRef.Context;
3105 QualType OldType = E->getType();
3106 unsigned HasBits = C.getTypeSize(OldType);
3107 if (HasBits >= Bits)
3108 return ExprResult(E);
3109 // OK to convert to signed, because new type has more bits than old.
3110 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
3111 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
3112 true);
3113}
3114
3115/// \brief Check if the given expression \a E is a constant integer that fits
3116/// into \a Bits bits.
3117static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
3118 if (E == nullptr)
3119 return false;
3120 llvm::APSInt Result;
3121 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
3122 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
3123 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003124}
3125
3126/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00003127/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
3128/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003129static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00003130CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
3131 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
3132 DSAStackTy &DSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003133 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00003134 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003135 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003136 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003137 // Found 'collapse' clause - calculate collapse number.
3138 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003139 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
3140 NestedLoopCount += Result.getLimitedValue() - 1;
3141 }
3142 if (OrderedLoopCountExpr) {
3143 // Found 'ordered' clause - calculate collapse number.
3144 llvm::APSInt Result;
3145 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
3146 NestedLoopCount += Result.getLimitedValue() - 1;
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003147 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003148 // This is helper routine for loop directives (e.g., 'for', 'simd',
3149 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00003150 SmallVector<LoopIterationSpace, 4> IterSpaces;
3151 IterSpaces.resize(NestedLoopCount);
3152 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003153 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003154 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003155 NestedLoopCount, CollapseLoopCountExpr,
3156 OrderedLoopCountExpr, VarsWithImplicitDSA,
3157 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00003158 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003159 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003160 // OpenMP [2.8.1, simd construct, Restrictions]
3161 // All loops associated with the construct must be perfectly nested; that
3162 // is, there must be no intervening code nor any OpenMP directive between
3163 // any two loops.
3164 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003165 }
3166
Alexander Musmana5f070a2014-10-01 06:03:56 +00003167 Built.clear(/* size */ NestedLoopCount);
3168
3169 if (SemaRef.CurContext->isDependentContext())
3170 return NestedLoopCount;
3171
3172 // An example of what is generated for the following code:
3173 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00003174 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00003175 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003176 // for (k = 0; k < NK; ++k)
3177 // for (j = J0; j < NJ; j+=2) {
3178 // <loop body>
3179 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003180 //
3181 // We generate the code below.
3182 // Note: the loop body may be outlined in CodeGen.
3183 // Note: some counters may be C++ classes, operator- is used to find number of
3184 // iterations and operator+= to calculate counter value.
3185 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
3186 // or i64 is currently supported).
3187 //
3188 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
3189 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
3190 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
3191 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
3192 // // similar updates for vars in clauses (e.g. 'linear')
3193 // <loop body (using local i and j)>
3194 // }
3195 // i = NI; // assign final values of counters
3196 // j = NJ;
3197 //
3198
3199 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
3200 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003201 // Precondition tests if there is at least one iteration (all conditions are
3202 // true).
3203 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003204 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003205 ExprResult LastIteration32 = WidenIterationCount(
3206 32 /* Bits */, SemaRef.PerformImplicitConversion(
3207 N0->IgnoreImpCasts(), N0->getType(),
3208 Sema::AA_Converting, /*AllowExplicit=*/true)
3209 .get(),
3210 SemaRef);
3211 ExprResult LastIteration64 = WidenIterationCount(
3212 64 /* Bits */, SemaRef.PerformImplicitConversion(
3213 N0->IgnoreImpCasts(), N0->getType(),
3214 Sema::AA_Converting, /*AllowExplicit=*/true)
3215 .get(),
3216 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003217
3218 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
3219 return NestedLoopCount;
3220
3221 auto &C = SemaRef.Context;
3222 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
3223
3224 Scope *CurScope = DSA.getCurScope();
3225 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003226 if (PreCond.isUsable()) {
3227 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
3228 PreCond.get(), IterSpaces[Cnt].PreCond);
3229 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003230 auto N = IterSpaces[Cnt].NumIterations;
3231 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
3232 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003233 LastIteration32 = SemaRef.BuildBinOp(
3234 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
3235 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3236 Sema::AA_Converting,
3237 /*AllowExplicit=*/true)
3238 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003239 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003240 LastIteration64 = SemaRef.BuildBinOp(
3241 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
3242 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3243 Sema::AA_Converting,
3244 /*AllowExplicit=*/true)
3245 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003246 }
3247
3248 // Choose either the 32-bit or 64-bit version.
3249 ExprResult LastIteration = LastIteration64;
3250 if (LastIteration32.isUsable() &&
3251 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
3252 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
3253 FitsInto(
3254 32 /* Bits */,
3255 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
3256 LastIteration64.get(), SemaRef)))
3257 LastIteration = LastIteration32;
3258
3259 if (!LastIteration.isUsable())
3260 return 0;
3261
3262 // Save the number of iterations.
3263 ExprResult NumIterations = LastIteration;
3264 {
3265 LastIteration = SemaRef.BuildBinOp(
3266 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
3267 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3268 if (!LastIteration.isUsable())
3269 return 0;
3270 }
3271
3272 // Calculate the last iteration number beforehand instead of doing this on
3273 // each iteration. Do not do this if the number of iterations may be kfold-ed.
3274 llvm::APSInt Result;
3275 bool IsConstant =
3276 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
3277 ExprResult CalcLastIteration;
3278 if (!IsConstant) {
3279 SourceLocation SaveLoc;
3280 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003281 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003282 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00003283 ExprResult SaveRef = buildDeclRefExpr(
3284 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003285 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
3286 SaveRef.get(), LastIteration.get());
3287 LastIteration = SaveRef;
3288
3289 // Prepare SaveRef + 1.
3290 NumIterations = SemaRef.BuildBinOp(
3291 CurScope, SaveLoc, BO_Add, SaveRef.get(),
3292 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3293 if (!NumIterations.isUsable())
3294 return 0;
3295 }
3296
3297 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
3298
Alexander Musmanc6388682014-12-15 07:07:06 +00003299 QualType VType = LastIteration.get()->getType();
3300 // Build variables passed into runtime, nesessary for worksharing directives.
3301 ExprResult LB, UB, IL, ST, EUB;
3302 if (isOpenMPWorksharingDirective(DKind)) {
3303 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003304 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
3305 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003306 SemaRef.AddInitializerToDecl(
3307 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3308 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3309
3310 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003311 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
3312 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003313 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
3314 /*DirectInit*/ false,
3315 /*TypeMayContainAuto*/ false);
3316
3317 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
3318 // This will be used to implement clause 'lastprivate'.
3319 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00003320 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
3321 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003322 SemaRef.AddInitializerToDecl(
3323 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3324 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3325
3326 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00003327 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
3328 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003329 SemaRef.AddInitializerToDecl(
3330 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
3331 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3332
3333 // Build expression: UB = min(UB, LastIteration)
3334 // It is nesessary for CodeGen of directives with static scheduling.
3335 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
3336 UB.get(), LastIteration.get());
3337 ExprResult CondOp = SemaRef.ActOnConditionalOp(
3338 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
3339 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
3340 CondOp.get());
3341 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
3342 }
3343
3344 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003345 ExprResult IV;
3346 ExprResult Init;
3347 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00003348 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
3349 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003350 Expr *RHS = isOpenMPWorksharingDirective(DKind)
3351 ? LB.get()
3352 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
3353 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
3354 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003355 }
3356
Alexander Musmanc6388682014-12-15 07:07:06 +00003357 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003358 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00003359 ExprResult Cond =
3360 isOpenMPWorksharingDirective(DKind)
3361 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
3362 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
3363 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003364
3365 // Loop increment (IV = IV + 1)
3366 SourceLocation IncLoc;
3367 ExprResult Inc =
3368 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
3369 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
3370 if (!Inc.isUsable())
3371 return 0;
3372 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00003373 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
3374 if (!Inc.isUsable())
3375 return 0;
3376
3377 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
3378 // Used for directives with static scheduling.
3379 ExprResult NextLB, NextUB;
3380 if (isOpenMPWorksharingDirective(DKind)) {
3381 // LB + ST
3382 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
3383 if (!NextLB.isUsable())
3384 return 0;
3385 // LB = LB + ST
3386 NextLB =
3387 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
3388 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
3389 if (!NextLB.isUsable())
3390 return 0;
3391 // UB + ST
3392 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
3393 if (!NextUB.isUsable())
3394 return 0;
3395 // UB = UB + ST
3396 NextUB =
3397 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
3398 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
3399 if (!NextUB.isUsable())
3400 return 0;
3401 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003402
3403 // Build updates and final values of the loop counters.
3404 bool HasErrors = false;
3405 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003406 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003407 Built.Updates.resize(NestedLoopCount);
3408 Built.Finals.resize(NestedLoopCount);
3409 {
3410 ExprResult Div;
3411 // Go from inner nested loop to outer.
3412 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
3413 LoopIterationSpace &IS = IterSpaces[Cnt];
3414 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
3415 // Build: Iter = (IV / Div) % IS.NumIters
3416 // where Div is product of previous iterations' IS.NumIters.
3417 ExprResult Iter;
3418 if (Div.isUsable()) {
3419 Iter =
3420 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
3421 } else {
3422 Iter = IV;
3423 assert((Cnt == (int)NestedLoopCount - 1) &&
3424 "unusable div expected on first iteration only");
3425 }
3426
3427 if (Cnt != 0 && Iter.isUsable())
3428 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
3429 IS.NumIterations);
3430 if (!Iter.isUsable()) {
3431 HasErrors = true;
3432 break;
3433 }
3434
Alexey Bataev39f915b82015-05-08 10:41:21 +00003435 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
3436 auto *CounterVar = buildDeclRefExpr(
3437 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
3438 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
3439 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003440 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
3441 IS.CounterInit);
3442 if (!Init.isUsable()) {
3443 HasErrors = true;
3444 break;
3445 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003446 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003447 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003448 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
3449 if (!Update.isUsable()) {
3450 HasErrors = true;
3451 break;
3452 }
3453
3454 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
3455 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00003456 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003457 IS.NumIterations, IS.CounterStep, IS.Subtract);
3458 if (!Final.isUsable()) {
3459 HasErrors = true;
3460 break;
3461 }
3462
3463 // Build Div for the next iteration: Div <- Div * IS.NumIters
3464 if (Cnt != 0) {
3465 if (Div.isUnset())
3466 Div = IS.NumIterations;
3467 else
3468 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
3469 IS.NumIterations);
3470
3471 // Add parentheses (for debugging purposes only).
3472 if (Div.isUsable())
3473 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
3474 if (!Div.isUsable()) {
3475 HasErrors = true;
3476 break;
3477 }
3478 }
3479 if (!Update.isUsable() || !Final.isUsable()) {
3480 HasErrors = true;
3481 break;
3482 }
3483 // Save results
3484 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003485 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003486 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003487 Built.Updates[Cnt] = Update.get();
3488 Built.Finals[Cnt] = Final.get();
3489 }
3490 }
3491
3492 if (HasErrors)
3493 return 0;
3494
3495 // Save results
3496 Built.IterationVarRef = IV.get();
3497 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00003498 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003499 Built.CalcLastIteration =
3500 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003501 Built.PreCond = PreCond.get();
3502 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003503 Built.Init = Init.get();
3504 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00003505 Built.LB = LB.get();
3506 Built.UB = UB.get();
3507 Built.IL = IL.get();
3508 Built.ST = ST.get();
3509 Built.EUB = EUB.get();
3510 Built.NLB = NextLB.get();
3511 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003512
Alexey Bataevabfc0692014-06-25 06:52:00 +00003513 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003514}
3515
Alexey Bataev10e775f2015-07-30 11:36:16 +00003516static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevc925aa32015-04-27 08:00:32 +00003517 auto &&CollapseFilter = [](const OMPClause *C) -> bool {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003518 return C->getClauseKind() == OMPC_collapse;
3519 };
3520 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
Alexey Bataevc925aa32015-04-27 08:00:32 +00003521 Clauses, std::move(CollapseFilter));
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003522 if (I)
3523 return cast<OMPCollapseClause>(*I)->getNumForLoops();
3524 return nullptr;
3525}
3526
Alexey Bataev10e775f2015-07-30 11:36:16 +00003527static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
3528 auto &&OrderedFilter = [](const OMPClause *C) -> bool {
3529 return C->getClauseKind() == OMPC_ordered;
3530 };
3531 OMPExecutableDirective::filtered_clause_iterator<decltype(OrderedFilter)> I(
3532 Clauses, std::move(OrderedFilter));
3533 if (I)
3534 return cast<OMPOrderedClause>(*I)->getNumForLoops();
3535 return nullptr;
3536}
3537
Alexey Bataev66b15b52015-08-21 11:14:16 +00003538static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
3539 const Expr *Safelen) {
3540 llvm::APSInt SimdlenRes, SafelenRes;
3541 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
3542 Simdlen->isInstantiationDependent() ||
3543 Simdlen->containsUnexpandedParameterPack())
3544 return false;
3545 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
3546 Safelen->isInstantiationDependent() ||
3547 Safelen->containsUnexpandedParameterPack())
3548 return false;
3549 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
3550 Safelen->EvaluateAsInt(SafelenRes, S.Context);
3551 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
3552 // If both simdlen and safelen clauses are specified, the value of the simdlen
3553 // parameter must be less than or equal to the value of the safelen parameter.
3554 if (SimdlenRes > SafelenRes) {
3555 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
3556 << Simdlen->getSourceRange() << Safelen->getSourceRange();
3557 return true;
3558 }
3559 return false;
3560}
3561
Alexey Bataev4acb8592014-07-07 13:01:15 +00003562StmtResult Sema::ActOnOpenMPSimdDirective(
3563 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3564 SourceLocation EndLoc,
3565 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003566 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003567 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3568 // define the nested loops number.
3569 unsigned NestedLoopCount = CheckOpenMPLoop(
3570 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
3571 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00003572 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003573 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003574
Alexander Musmana5f070a2014-10-01 06:03:56 +00003575 assert((CurContext->isDependentContext() || B.builtAll()) &&
3576 "omp simd loop exprs were not built");
3577
Alexander Musman3276a272015-03-21 10:12:56 +00003578 if (!CurContext->isDependentContext()) {
3579 // Finalize the clauses that need pre-built expressions for CodeGen.
3580 for (auto C : Clauses) {
3581 if (auto LC = dyn_cast<OMPLinearClause>(C))
3582 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3583 B.NumIterations, *this, CurScope))
3584 return StmtError();
3585 }
3586 }
3587
Alexey Bataev66b15b52015-08-21 11:14:16 +00003588 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
3589 // If both simdlen and safelen clauses are specified, the value of the simdlen
3590 // parameter must be less than or equal to the value of the safelen parameter.
3591 OMPSafelenClause *Safelen = nullptr;
3592 OMPSimdlenClause *Simdlen = nullptr;
3593 for (auto *Clause : Clauses) {
3594 if (Clause->getClauseKind() == OMPC_safelen)
3595 Safelen = cast<OMPSafelenClause>(Clause);
3596 else if (Clause->getClauseKind() == OMPC_simdlen)
3597 Simdlen = cast<OMPSimdlenClause>(Clause);
3598 if (Safelen && Simdlen)
3599 break;
3600 }
3601 if (Simdlen && Safelen &&
3602 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
3603 Safelen->getSafelen()))
3604 return StmtError();
3605
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003606 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003607 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3608 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003609}
3610
Alexey Bataev4acb8592014-07-07 13:01:15 +00003611StmtResult Sema::ActOnOpenMPForDirective(
3612 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3613 SourceLocation EndLoc,
3614 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003615 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003616 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3617 // define the nested loops number.
3618 unsigned NestedLoopCount = CheckOpenMPLoop(
3619 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
3620 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00003621 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00003622 return StmtError();
3623
Alexander Musmana5f070a2014-10-01 06:03:56 +00003624 assert((CurContext->isDependentContext() || B.builtAll()) &&
3625 "omp for loop exprs were not built");
3626
Alexey Bataev54acd402015-08-04 11:18:19 +00003627 if (!CurContext->isDependentContext()) {
3628 // Finalize the clauses that need pre-built expressions for CodeGen.
3629 for (auto C : Clauses) {
3630 if (auto LC = dyn_cast<OMPLinearClause>(C))
3631 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3632 B.NumIterations, *this, CurScope))
3633 return StmtError();
3634 }
3635 }
3636
Alexey Bataevf29276e2014-06-18 04:14:57 +00003637 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003638 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3639 Clauses, AStmt, B);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003640}
3641
Alexander Musmanf82886e2014-09-18 05:12:34 +00003642StmtResult Sema::ActOnOpenMPForSimdDirective(
3643 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3644 SourceLocation EndLoc,
3645 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003646 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003647 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3648 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00003649 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00003650 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
3651 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
3652 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00003653 if (NestedLoopCount == 0)
3654 return StmtError();
3655
Alexander Musmanc6388682014-12-15 07:07:06 +00003656 assert((CurContext->isDependentContext() || B.builtAll()) &&
3657 "omp for simd loop exprs were not built");
3658
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00003659 if (!CurContext->isDependentContext()) {
3660 // Finalize the clauses that need pre-built expressions for CodeGen.
3661 for (auto C : Clauses) {
3662 if (auto LC = dyn_cast<OMPLinearClause>(C))
3663 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3664 B.NumIterations, *this, CurScope))
3665 return StmtError();
3666 }
3667 }
3668
Alexey Bataev66b15b52015-08-21 11:14:16 +00003669 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
3670 // If both simdlen and safelen clauses are specified, the value of the simdlen
3671 // parameter must be less than or equal to the value of the safelen parameter.
3672 OMPSafelenClause *Safelen = nullptr;
3673 OMPSimdlenClause *Simdlen = nullptr;
3674 for (auto *Clause : Clauses) {
3675 if (Clause->getClauseKind() == OMPC_safelen)
3676 Safelen = cast<OMPSafelenClause>(Clause);
3677 else if (Clause->getClauseKind() == OMPC_simdlen)
3678 Simdlen = cast<OMPSimdlenClause>(Clause);
3679 if (Safelen && Simdlen)
3680 break;
3681 }
3682 if (Simdlen && Safelen &&
3683 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
3684 Safelen->getSafelen()))
3685 return StmtError();
3686
Alexander Musmanf82886e2014-09-18 05:12:34 +00003687 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003688 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3689 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00003690}
3691
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003692StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
3693 Stmt *AStmt,
3694 SourceLocation StartLoc,
3695 SourceLocation EndLoc) {
3696 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3697 auto BaseStmt = AStmt;
3698 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3699 BaseStmt = CS->getCapturedStmt();
3700 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3701 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00003702 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003703 return StmtError();
3704 // All associated statements must be '#pragma omp section' except for
3705 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00003706 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003707 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3708 if (SectionStmt)
3709 Diag(SectionStmt->getLocStart(),
3710 diag::err_omp_sections_substmt_not_section);
3711 return StmtError();
3712 }
3713 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003714 } else {
3715 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
3716 return StmtError();
3717 }
3718
3719 getCurFunction()->setHasBranchProtectedScope();
3720
3721 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
3722 AStmt);
3723}
3724
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003725StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
3726 SourceLocation StartLoc,
3727 SourceLocation EndLoc) {
3728 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3729
3730 getCurFunction()->setHasBranchProtectedScope();
3731
3732 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
3733}
3734
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003735StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
3736 Stmt *AStmt,
3737 SourceLocation StartLoc,
3738 SourceLocation EndLoc) {
Alexey Bataev74a05c92014-07-15 02:55:09 +00003739 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3740
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003741 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00003742
Alexey Bataev3255bf32015-01-19 05:20:46 +00003743 // OpenMP [2.7.3, single Construct, Restrictions]
3744 // The copyprivate clause must not be used with the nowait clause.
3745 OMPClause *Nowait = nullptr;
3746 OMPClause *Copyprivate = nullptr;
3747 for (auto *Clause : Clauses) {
3748 if (Clause->getClauseKind() == OMPC_nowait)
3749 Nowait = Clause;
3750 else if (Clause->getClauseKind() == OMPC_copyprivate)
3751 Copyprivate = Clause;
3752 if (Copyprivate && Nowait) {
3753 Diag(Copyprivate->getLocStart(),
3754 diag::err_omp_single_copyprivate_with_nowait);
3755 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
3756 return StmtError();
3757 }
3758 }
3759
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003760 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3761}
3762
Alexander Musman80c22892014-07-17 08:54:58 +00003763StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
3764 SourceLocation StartLoc,
3765 SourceLocation EndLoc) {
3766 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3767
3768 getCurFunction()->setHasBranchProtectedScope();
3769
3770 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
3771}
3772
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003773StmtResult
3774Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
3775 Stmt *AStmt, SourceLocation StartLoc,
3776 SourceLocation EndLoc) {
3777 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3778
3779 getCurFunction()->setHasBranchProtectedScope();
3780
3781 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
3782 AStmt);
3783}
3784
Alexey Bataev4acb8592014-07-07 13:01:15 +00003785StmtResult Sema::ActOnOpenMPParallelForDirective(
3786 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3787 SourceLocation EndLoc,
3788 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3789 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3790 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3791 // 1.2.2 OpenMP Language Terminology
3792 // Structured block - An executable statement with a single entry at the
3793 // top and a single exit at the bottom.
3794 // The point of exit cannot be a branch out of the structured block.
3795 // longjmp() and throw() must not violate the entry/exit criteria.
3796 CS->getCapturedDecl()->setNothrow();
3797
Alexander Musmanc6388682014-12-15 07:07:06 +00003798 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003799 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3800 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003801 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00003802 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
3803 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
3804 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003805 if (NestedLoopCount == 0)
3806 return StmtError();
3807
Alexander Musmana5f070a2014-10-01 06:03:56 +00003808 assert((CurContext->isDependentContext() || B.builtAll()) &&
3809 "omp parallel for loop exprs were not built");
3810
Alexey Bataev54acd402015-08-04 11:18:19 +00003811 if (!CurContext->isDependentContext()) {
3812 // Finalize the clauses that need pre-built expressions for CodeGen.
3813 for (auto C : Clauses) {
3814 if (auto LC = dyn_cast<OMPLinearClause>(C))
3815 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3816 B.NumIterations, *this, CurScope))
3817 return StmtError();
3818 }
3819 }
3820
Alexey Bataev4acb8592014-07-07 13:01:15 +00003821 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003822 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
3823 NestedLoopCount, Clauses, AStmt, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003824}
3825
Alexander Musmane4e893b2014-09-23 09:33:00 +00003826StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
3827 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3828 SourceLocation EndLoc,
3829 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3830 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3831 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3832 // 1.2.2 OpenMP Language Terminology
3833 // Structured block - An executable statement with a single entry at the
3834 // top and a single exit at the bottom.
3835 // The point of exit cannot be a branch out of the structured block.
3836 // longjmp() and throw() must not violate the entry/exit criteria.
3837 CS->getCapturedDecl()->setNothrow();
3838
Alexander Musmanc6388682014-12-15 07:07:06 +00003839 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003840 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3841 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00003842 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00003843 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
3844 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
3845 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003846 if (NestedLoopCount == 0)
3847 return StmtError();
3848
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00003849 if (!CurContext->isDependentContext()) {
3850 // Finalize the clauses that need pre-built expressions for CodeGen.
3851 for (auto C : Clauses) {
3852 if (auto LC = dyn_cast<OMPLinearClause>(C))
3853 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3854 B.NumIterations, *this, CurScope))
3855 return StmtError();
3856 }
3857 }
3858
Alexey Bataev66b15b52015-08-21 11:14:16 +00003859 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
3860 // If both simdlen and safelen clauses are specified, the value of the simdlen
3861 // parameter must be less than or equal to the value of the safelen parameter.
3862 OMPSafelenClause *Safelen = nullptr;
3863 OMPSimdlenClause *Simdlen = nullptr;
3864 for (auto *Clause : Clauses) {
3865 if (Clause->getClauseKind() == OMPC_safelen)
3866 Safelen = cast<OMPSafelenClause>(Clause);
3867 else if (Clause->getClauseKind() == OMPC_simdlen)
3868 Simdlen = cast<OMPSimdlenClause>(Clause);
3869 if (Safelen && Simdlen)
3870 break;
3871 }
3872 if (Simdlen && Safelen &&
3873 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
3874 Safelen->getSafelen()))
3875 return StmtError();
3876
Alexander Musmane4e893b2014-09-23 09:33:00 +00003877 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003878 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00003879 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003880}
3881
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003882StmtResult
3883Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
3884 Stmt *AStmt, SourceLocation StartLoc,
3885 SourceLocation EndLoc) {
3886 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3887 auto BaseStmt = AStmt;
3888 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3889 BaseStmt = CS->getCapturedStmt();
3890 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3891 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00003892 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003893 return StmtError();
3894 // All associated statements must be '#pragma omp section' except for
3895 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00003896 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003897 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3898 if (SectionStmt)
3899 Diag(SectionStmt->getLocStart(),
3900 diag::err_omp_parallel_sections_substmt_not_section);
3901 return StmtError();
3902 }
3903 }
3904 } else {
3905 Diag(AStmt->getLocStart(),
3906 diag::err_omp_parallel_sections_not_compound_stmt);
3907 return StmtError();
3908 }
3909
3910 getCurFunction()->setHasBranchProtectedScope();
3911
3912 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
3913 Clauses, AStmt);
3914}
3915
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003916StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
3917 Stmt *AStmt, SourceLocation StartLoc,
3918 SourceLocation EndLoc) {
3919 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3920 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3921 // 1.2.2 OpenMP Language Terminology
3922 // Structured block - An executable statement with a single entry at the
3923 // top and a single exit at the bottom.
3924 // The point of exit cannot be a branch out of the structured block.
3925 // longjmp() and throw() must not violate the entry/exit criteria.
3926 CS->getCapturedDecl()->setNothrow();
3927
3928 getCurFunction()->setHasBranchProtectedScope();
3929
3930 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3931}
3932
Alexey Bataev68446b72014-07-18 07:47:19 +00003933StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
3934 SourceLocation EndLoc) {
3935 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
3936}
3937
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003938StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
3939 SourceLocation EndLoc) {
3940 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
3941}
3942
Alexey Bataev2df347a2014-07-18 10:17:07 +00003943StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
3944 SourceLocation EndLoc) {
3945 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
3946}
3947
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003948StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
3949 SourceLocation StartLoc,
3950 SourceLocation EndLoc) {
3951 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3952
3953 getCurFunction()->setHasBranchProtectedScope();
3954
3955 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
3956}
3957
Alexey Bataev6125da92014-07-21 11:26:11 +00003958StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
3959 SourceLocation StartLoc,
3960 SourceLocation EndLoc) {
3961 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
3962 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
3963}
3964
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003965StmtResult Sema::ActOnOpenMPOrderedDirective(Stmt *AStmt,
3966 SourceLocation StartLoc,
3967 SourceLocation EndLoc) {
3968 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3969
3970 getCurFunction()->setHasBranchProtectedScope();
3971
3972 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, AStmt);
3973}
3974
Alexey Bataev1d160b12015-03-13 12:27:31 +00003975namespace {
3976/// \brief Helper class for checking expression in 'omp atomic [update]'
3977/// construct.
3978class OpenMPAtomicUpdateChecker {
3979 /// \brief Error results for atomic update expressions.
3980 enum ExprAnalysisErrorCode {
3981 /// \brief A statement is not an expression statement.
3982 NotAnExpression,
3983 /// \brief Expression is not builtin binary or unary operation.
3984 NotABinaryOrUnaryExpression,
3985 /// \brief Unary operation is not post-/pre- increment/decrement operation.
3986 NotAnUnaryIncDecExpression,
3987 /// \brief An expression is not of scalar type.
3988 NotAScalarType,
3989 /// \brief A binary operation is not an assignment operation.
3990 NotAnAssignmentOp,
3991 /// \brief RHS part of the binary operation is not a binary expression.
3992 NotABinaryExpression,
3993 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
3994 /// expression.
3995 NotABinaryOperator,
3996 /// \brief RHS binary operation does not have reference to the updated LHS
3997 /// part.
3998 NotAnUpdateExpression,
3999 /// \brief No errors is found.
4000 NoError
4001 };
4002 /// \brief Reference to Sema.
4003 Sema &SemaRef;
4004 /// \brief A location for note diagnostics (when error is found).
4005 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004006 /// \brief 'x' lvalue part of the source atomic expression.
4007 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004008 /// \brief 'expr' rvalue part of the source atomic expression.
4009 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004010 /// \brief Helper expression of the form
4011 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4012 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4013 Expr *UpdateExpr;
4014 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
4015 /// important for non-associative operations.
4016 bool IsXLHSInRHSPart;
4017 BinaryOperatorKind Op;
4018 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004019 /// \brief true if the source expression is a postfix unary operation, false
4020 /// if it is a prefix unary operation.
4021 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004022
4023public:
4024 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00004025 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00004026 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00004027 /// \brief Check specified statement that it is suitable for 'atomic update'
4028 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00004029 /// expression. If DiagId and NoteId == 0, then only check is performed
4030 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00004031 /// \param DiagId Diagnostic which should be emitted if error is found.
4032 /// \param NoteId Diagnostic note for the main error message.
4033 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00004034 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004035 /// \brief Return the 'x' lvalue part of the source atomic expression.
4036 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00004037 /// \brief Return the 'expr' rvalue part of the source atomic expression.
4038 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00004039 /// \brief Return the update expression used in calculation of the updated
4040 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4041 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4042 Expr *getUpdateExpr() const { return UpdateExpr; }
4043 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
4044 /// false otherwise.
4045 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
4046
Alexey Bataevb78ca832015-04-01 03:33:17 +00004047 /// \brief true if the source expression is a postfix unary operation, false
4048 /// if it is a prefix unary operation.
4049 bool isPostfixUpdate() const { return IsPostfixUpdate; }
4050
Alexey Bataev1d160b12015-03-13 12:27:31 +00004051private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00004052 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
4053 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004054};
4055} // namespace
4056
4057bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
4058 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
4059 ExprAnalysisErrorCode ErrorFound = NoError;
4060 SourceLocation ErrorLoc, NoteLoc;
4061 SourceRange ErrorRange, NoteRange;
4062 // Allowed constructs are:
4063 // x = x binop expr;
4064 // x = expr binop x;
4065 if (AtomicBinOp->getOpcode() == BO_Assign) {
4066 X = AtomicBinOp->getLHS();
4067 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
4068 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
4069 if (AtomicInnerBinOp->isMultiplicativeOp() ||
4070 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
4071 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004072 Op = AtomicInnerBinOp->getOpcode();
4073 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004074 auto *LHS = AtomicInnerBinOp->getLHS();
4075 auto *RHS = AtomicInnerBinOp->getRHS();
4076 llvm::FoldingSetNodeID XId, LHSId, RHSId;
4077 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
4078 /*Canonical=*/true);
4079 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
4080 /*Canonical=*/true);
4081 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
4082 /*Canonical=*/true);
4083 if (XId == LHSId) {
4084 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004085 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004086 } else if (XId == RHSId) {
4087 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004088 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004089 } else {
4090 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4091 ErrorRange = AtomicInnerBinOp->getSourceRange();
4092 NoteLoc = X->getExprLoc();
4093 NoteRange = X->getSourceRange();
4094 ErrorFound = NotAnUpdateExpression;
4095 }
4096 } else {
4097 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4098 ErrorRange = AtomicInnerBinOp->getSourceRange();
4099 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
4100 NoteRange = SourceRange(NoteLoc, NoteLoc);
4101 ErrorFound = NotABinaryOperator;
4102 }
4103 } else {
4104 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
4105 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
4106 ErrorFound = NotABinaryExpression;
4107 }
4108 } else {
4109 ErrorLoc = AtomicBinOp->getExprLoc();
4110 ErrorRange = AtomicBinOp->getSourceRange();
4111 NoteLoc = AtomicBinOp->getOperatorLoc();
4112 NoteRange = SourceRange(NoteLoc, NoteLoc);
4113 ErrorFound = NotAnAssignmentOp;
4114 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004115 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004116 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
4117 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
4118 return true;
4119 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00004120 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00004121 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004122}
4123
4124bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
4125 unsigned NoteId) {
4126 ExprAnalysisErrorCode ErrorFound = NoError;
4127 SourceLocation ErrorLoc, NoteLoc;
4128 SourceRange ErrorRange, NoteRange;
4129 // Allowed constructs are:
4130 // x++;
4131 // x--;
4132 // ++x;
4133 // --x;
4134 // x binop= expr;
4135 // x = x binop expr;
4136 // x = expr binop x;
4137 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
4138 AtomicBody = AtomicBody->IgnoreParenImpCasts();
4139 if (AtomicBody->getType()->isScalarType() ||
4140 AtomicBody->isInstantiationDependent()) {
4141 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
4142 AtomicBody->IgnoreParenImpCasts())) {
4143 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00004144 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00004145 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00004146 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004147 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004148 X = AtomicCompAssignOp->getLHS();
4149 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004150 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
4151 AtomicBody->IgnoreParenImpCasts())) {
4152 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00004153 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
4154 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004155 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00004156 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
4157 // Check for Unary Operation
4158 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004159 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004160 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
4161 OpLoc = AtomicUnaryOp->getOperatorLoc();
4162 X = AtomicUnaryOp->getSubExpr();
4163 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
4164 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004165 } else {
4166 ErrorFound = NotAnUnaryIncDecExpression;
4167 ErrorLoc = AtomicUnaryOp->getExprLoc();
4168 ErrorRange = AtomicUnaryOp->getSourceRange();
4169 NoteLoc = AtomicUnaryOp->getOperatorLoc();
4170 NoteRange = SourceRange(NoteLoc, NoteLoc);
4171 }
4172 } else {
4173 ErrorFound = NotABinaryOrUnaryExpression;
4174 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
4175 NoteRange = ErrorRange = AtomicBody->getSourceRange();
4176 }
4177 } else {
4178 ErrorFound = NotAScalarType;
4179 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
4180 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4181 }
4182 } else {
4183 ErrorFound = NotAnExpression;
4184 NoteLoc = ErrorLoc = S->getLocStart();
4185 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4186 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004187 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004188 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
4189 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
4190 return true;
4191 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00004192 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00004193 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004194 // Build an update expression of form 'OpaqueValueExpr(x) binop
4195 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
4196 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
4197 auto *OVEX = new (SemaRef.getASTContext())
4198 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
4199 auto *OVEExpr = new (SemaRef.getASTContext())
4200 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
4201 auto Update =
4202 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
4203 IsXLHSInRHSPart ? OVEExpr : OVEX);
4204 if (Update.isInvalid())
4205 return true;
4206 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
4207 Sema::AA_Casting);
4208 if (Update.isInvalid())
4209 return true;
4210 UpdateExpr = Update.get();
4211 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00004212 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004213}
4214
Alexey Bataev0162e452014-07-22 10:10:35 +00004215StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
4216 Stmt *AStmt,
4217 SourceLocation StartLoc,
4218 SourceLocation EndLoc) {
4219 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004220 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00004221 // 1.2.2 OpenMP Language Terminology
4222 // Structured block - An executable statement with a single entry at the
4223 // top and a single exit at the bottom.
4224 // The point of exit cannot be a branch out of the structured block.
4225 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00004226 OpenMPClauseKind AtomicKind = OMPC_unknown;
4227 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004228 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00004229 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00004230 C->getClauseKind() == OMPC_update ||
4231 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00004232 if (AtomicKind != OMPC_unknown) {
4233 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
4234 << SourceRange(C->getLocStart(), C->getLocEnd());
4235 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
4236 << getOpenMPClauseName(AtomicKind);
4237 } else {
4238 AtomicKind = C->getClauseKind();
4239 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004240 }
4241 }
4242 }
Alexey Bataev62cec442014-11-18 10:14:22 +00004243
Alexey Bataev459dec02014-07-24 06:46:57 +00004244 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00004245 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
4246 Body = EWC->getSubExpr();
4247
Alexey Bataev62cec442014-11-18 10:14:22 +00004248 Expr *X = nullptr;
4249 Expr *V = nullptr;
4250 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004251 Expr *UE = nullptr;
4252 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004253 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00004254 // OpenMP [2.12.6, atomic Construct]
4255 // In the next expressions:
4256 // * x and v (as applicable) are both l-value expressions with scalar type.
4257 // * During the execution of an atomic region, multiple syntactic
4258 // occurrences of x must designate the same storage location.
4259 // * Neither of v and expr (as applicable) may access the storage location
4260 // designated by x.
4261 // * Neither of x and expr (as applicable) may access the storage location
4262 // designated by v.
4263 // * expr is an expression with scalar type.
4264 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
4265 // * binop, binop=, ++, and -- are not overloaded operators.
4266 // * The expression x binop expr must be numerically equivalent to x binop
4267 // (expr). This requirement is satisfied if the operators in expr have
4268 // precedence greater than binop, or by using parentheses around expr or
4269 // subexpressions of expr.
4270 // * The expression expr binop x must be numerically equivalent to (expr)
4271 // binop x. This requirement is satisfied if the operators in expr have
4272 // precedence equal to or greater than binop, or by using parentheses around
4273 // expr or subexpressions of expr.
4274 // * For forms that allow multiple occurrences of x, the number of times
4275 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00004276 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004277 enum {
4278 NotAnExpression,
4279 NotAnAssignmentOp,
4280 NotAScalarType,
4281 NotAnLValue,
4282 NoError
4283 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00004284 SourceLocation ErrorLoc, NoteLoc;
4285 SourceRange ErrorRange, NoteRange;
4286 // If clause is read:
4287 // v = x;
4288 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
4289 auto AtomicBinOp =
4290 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4291 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4292 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
4293 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
4294 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
4295 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
4296 if (!X->isLValue() || !V->isLValue()) {
4297 auto NotLValueExpr = X->isLValue() ? V : X;
4298 ErrorFound = NotAnLValue;
4299 ErrorLoc = AtomicBinOp->getExprLoc();
4300 ErrorRange = AtomicBinOp->getSourceRange();
4301 NoteLoc = NotLValueExpr->getExprLoc();
4302 NoteRange = NotLValueExpr->getSourceRange();
4303 }
4304 } else if (!X->isInstantiationDependent() ||
4305 !V->isInstantiationDependent()) {
4306 auto NotScalarExpr =
4307 (X->isInstantiationDependent() || X->getType()->isScalarType())
4308 ? V
4309 : X;
4310 ErrorFound = NotAScalarType;
4311 ErrorLoc = AtomicBinOp->getExprLoc();
4312 ErrorRange = AtomicBinOp->getSourceRange();
4313 NoteLoc = NotScalarExpr->getExprLoc();
4314 NoteRange = NotScalarExpr->getSourceRange();
4315 }
4316 } else {
4317 ErrorFound = NotAnAssignmentOp;
4318 ErrorLoc = AtomicBody->getExprLoc();
4319 ErrorRange = AtomicBody->getSourceRange();
4320 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4321 : AtomicBody->getExprLoc();
4322 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4323 : AtomicBody->getSourceRange();
4324 }
4325 } else {
4326 ErrorFound = NotAnExpression;
4327 NoteLoc = ErrorLoc = Body->getLocStart();
4328 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00004329 }
Alexey Bataev62cec442014-11-18 10:14:22 +00004330 if (ErrorFound != NoError) {
4331 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
4332 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00004333 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4334 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00004335 return StmtError();
4336 } else if (CurContext->isDependentContext())
4337 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00004338 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004339 enum {
4340 NotAnExpression,
4341 NotAnAssignmentOp,
4342 NotAScalarType,
4343 NotAnLValue,
4344 NoError
4345 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00004346 SourceLocation ErrorLoc, NoteLoc;
4347 SourceRange ErrorRange, NoteRange;
4348 // If clause is write:
4349 // x = expr;
4350 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
4351 auto AtomicBinOp =
4352 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4353 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00004354 X = AtomicBinOp->getLHS();
4355 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00004356 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
4357 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
4358 if (!X->isLValue()) {
4359 ErrorFound = NotAnLValue;
4360 ErrorLoc = AtomicBinOp->getExprLoc();
4361 ErrorRange = AtomicBinOp->getSourceRange();
4362 NoteLoc = X->getExprLoc();
4363 NoteRange = X->getSourceRange();
4364 }
4365 } else if (!X->isInstantiationDependent() ||
4366 !E->isInstantiationDependent()) {
4367 auto NotScalarExpr =
4368 (X->isInstantiationDependent() || X->getType()->isScalarType())
4369 ? E
4370 : X;
4371 ErrorFound = NotAScalarType;
4372 ErrorLoc = AtomicBinOp->getExprLoc();
4373 ErrorRange = AtomicBinOp->getSourceRange();
4374 NoteLoc = NotScalarExpr->getExprLoc();
4375 NoteRange = NotScalarExpr->getSourceRange();
4376 }
4377 } else {
4378 ErrorFound = NotAnAssignmentOp;
4379 ErrorLoc = AtomicBody->getExprLoc();
4380 ErrorRange = AtomicBody->getSourceRange();
4381 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4382 : AtomicBody->getExprLoc();
4383 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4384 : AtomicBody->getSourceRange();
4385 }
4386 } else {
4387 ErrorFound = NotAnExpression;
4388 NoteLoc = ErrorLoc = Body->getLocStart();
4389 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00004390 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00004391 if (ErrorFound != NoError) {
4392 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
4393 << ErrorRange;
4394 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4395 << NoteRange;
4396 return StmtError();
4397 } else if (CurContext->isDependentContext())
4398 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00004399 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004400 // If clause is update:
4401 // x++;
4402 // x--;
4403 // ++x;
4404 // --x;
4405 // x binop= expr;
4406 // x = x binop expr;
4407 // x = expr binop x;
4408 OpenMPAtomicUpdateChecker Checker(*this);
4409 if (Checker.checkStatement(
4410 Body, (AtomicKind == OMPC_update)
4411 ? diag::err_omp_atomic_update_not_expression_statement
4412 : diag::err_omp_atomic_not_expression_statement,
4413 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00004414 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004415 if (!CurContext->isDependentContext()) {
4416 E = Checker.getExpr();
4417 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004418 UE = Checker.getUpdateExpr();
4419 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00004420 }
Alexey Bataev459dec02014-07-24 06:46:57 +00004421 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004422 enum {
4423 NotAnAssignmentOp,
4424 NotACompoundStatement,
4425 NotTwoSubstatements,
4426 NotASpecificExpression,
4427 NoError
4428 } ErrorFound = NoError;
4429 SourceLocation ErrorLoc, NoteLoc;
4430 SourceRange ErrorRange, NoteRange;
4431 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
4432 // If clause is a capture:
4433 // v = x++;
4434 // v = x--;
4435 // v = ++x;
4436 // v = --x;
4437 // v = x binop= expr;
4438 // v = x = x binop expr;
4439 // v = x = expr binop x;
4440 auto *AtomicBinOp =
4441 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4442 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4443 V = AtomicBinOp->getLHS();
4444 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
4445 OpenMPAtomicUpdateChecker Checker(*this);
4446 if (Checker.checkStatement(
4447 Body, diag::err_omp_atomic_capture_not_expression_statement,
4448 diag::note_omp_atomic_update))
4449 return StmtError();
4450 E = Checker.getExpr();
4451 X = Checker.getX();
4452 UE = Checker.getUpdateExpr();
4453 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
4454 IsPostfixUpdate = Checker.isPostfixUpdate();
4455 } else {
4456 ErrorLoc = AtomicBody->getExprLoc();
4457 ErrorRange = AtomicBody->getSourceRange();
4458 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4459 : AtomicBody->getExprLoc();
4460 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4461 : AtomicBody->getSourceRange();
4462 ErrorFound = NotAnAssignmentOp;
4463 }
4464 if (ErrorFound != NoError) {
4465 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
4466 << ErrorRange;
4467 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
4468 return StmtError();
4469 } else if (CurContext->isDependentContext()) {
4470 UE = V = E = X = nullptr;
4471 }
4472 } else {
4473 // If clause is a capture:
4474 // { v = x; x = expr; }
4475 // { v = x; x++; }
4476 // { v = x; x--; }
4477 // { v = x; ++x; }
4478 // { v = x; --x; }
4479 // { v = x; x binop= expr; }
4480 // { v = x; x = x binop expr; }
4481 // { v = x; x = expr binop x; }
4482 // { x++; v = x; }
4483 // { x--; v = x; }
4484 // { ++x; v = x; }
4485 // { --x; v = x; }
4486 // { x binop= expr; v = x; }
4487 // { x = x binop expr; v = x; }
4488 // { x = expr binop x; v = x; }
4489 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
4490 // Check that this is { expr1; expr2; }
4491 if (CS->size() == 2) {
4492 auto *First = CS->body_front();
4493 auto *Second = CS->body_back();
4494 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
4495 First = EWC->getSubExpr()->IgnoreParenImpCasts();
4496 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
4497 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
4498 // Need to find what subexpression is 'v' and what is 'x'.
4499 OpenMPAtomicUpdateChecker Checker(*this);
4500 bool IsUpdateExprFound = !Checker.checkStatement(Second);
4501 BinaryOperator *BinOp = nullptr;
4502 if (IsUpdateExprFound) {
4503 BinOp = dyn_cast<BinaryOperator>(First);
4504 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
4505 }
4506 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
4507 // { v = x; x++; }
4508 // { v = x; x--; }
4509 // { v = x; ++x; }
4510 // { v = x; --x; }
4511 // { v = x; x binop= expr; }
4512 // { v = x; x = x binop expr; }
4513 // { v = x; x = expr binop x; }
4514 // Check that the first expression has form v = x.
4515 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
4516 llvm::FoldingSetNodeID XId, PossibleXId;
4517 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
4518 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
4519 IsUpdateExprFound = XId == PossibleXId;
4520 if (IsUpdateExprFound) {
4521 V = BinOp->getLHS();
4522 X = Checker.getX();
4523 E = Checker.getExpr();
4524 UE = Checker.getUpdateExpr();
4525 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00004526 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004527 }
4528 }
4529 if (!IsUpdateExprFound) {
4530 IsUpdateExprFound = !Checker.checkStatement(First);
4531 BinOp = nullptr;
4532 if (IsUpdateExprFound) {
4533 BinOp = dyn_cast<BinaryOperator>(Second);
4534 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
4535 }
4536 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
4537 // { x++; v = x; }
4538 // { x--; v = x; }
4539 // { ++x; v = x; }
4540 // { --x; v = x; }
4541 // { x binop= expr; v = x; }
4542 // { x = x binop expr; v = x; }
4543 // { x = expr binop x; v = x; }
4544 // Check that the second expression has form v = x.
4545 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
4546 llvm::FoldingSetNodeID XId, PossibleXId;
4547 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
4548 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
4549 IsUpdateExprFound = XId == PossibleXId;
4550 if (IsUpdateExprFound) {
4551 V = BinOp->getLHS();
4552 X = Checker.getX();
4553 E = Checker.getExpr();
4554 UE = Checker.getUpdateExpr();
4555 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00004556 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004557 }
4558 }
4559 }
4560 if (!IsUpdateExprFound) {
4561 // { v = x; x = expr; }
4562 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
4563 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
4564 ErrorFound = NotAnAssignmentOp;
4565 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
4566 : First->getLocStart();
4567 NoteRange = ErrorRange = FirstBinOp
4568 ? FirstBinOp->getSourceRange()
4569 : SourceRange(ErrorLoc, ErrorLoc);
4570 } else {
4571 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
4572 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
4573 ErrorFound = NotAnAssignmentOp;
4574 NoteLoc = ErrorLoc = SecondBinOp ? SecondBinOp->getOperatorLoc()
4575 : Second->getLocStart();
4576 NoteRange = ErrorRange = SecondBinOp
4577 ? SecondBinOp->getSourceRange()
4578 : SourceRange(ErrorLoc, ErrorLoc);
4579 } else {
4580 auto *PossibleXRHSInFirst =
4581 FirstBinOp->getRHS()->IgnoreParenImpCasts();
4582 auto *PossibleXLHSInSecond =
4583 SecondBinOp->getLHS()->IgnoreParenImpCasts();
4584 llvm::FoldingSetNodeID X1Id, X2Id;
4585 PossibleXRHSInFirst->Profile(X1Id, Context, /*Canonical=*/true);
4586 PossibleXLHSInSecond->Profile(X2Id, Context,
4587 /*Canonical=*/true);
4588 IsUpdateExprFound = X1Id == X2Id;
4589 if (IsUpdateExprFound) {
4590 V = FirstBinOp->getLHS();
4591 X = SecondBinOp->getLHS();
4592 E = SecondBinOp->getRHS();
4593 UE = nullptr;
4594 IsXLHSInRHSPart = false;
4595 IsPostfixUpdate = true;
4596 } else {
4597 ErrorFound = NotASpecificExpression;
4598 ErrorLoc = FirstBinOp->getExprLoc();
4599 ErrorRange = FirstBinOp->getSourceRange();
4600 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
4601 NoteRange = SecondBinOp->getRHS()->getSourceRange();
4602 }
4603 }
4604 }
4605 }
4606 } else {
4607 NoteLoc = ErrorLoc = Body->getLocStart();
4608 NoteRange = ErrorRange =
4609 SourceRange(Body->getLocStart(), Body->getLocStart());
4610 ErrorFound = NotTwoSubstatements;
4611 }
4612 } else {
4613 NoteLoc = ErrorLoc = Body->getLocStart();
4614 NoteRange = ErrorRange =
4615 SourceRange(Body->getLocStart(), Body->getLocStart());
4616 ErrorFound = NotACompoundStatement;
4617 }
4618 if (ErrorFound != NoError) {
4619 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
4620 << ErrorRange;
4621 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
4622 return StmtError();
4623 } else if (CurContext->isDependentContext()) {
4624 UE = V = E = X = nullptr;
4625 }
Alexey Bataev459dec02014-07-24 06:46:57 +00004626 }
Alexey Bataevdea47612014-07-23 07:46:59 +00004627 }
Alexey Bataev0162e452014-07-22 10:10:35 +00004628
4629 getCurFunction()->setHasBranchProtectedScope();
4630
Alexey Bataev62cec442014-11-18 10:14:22 +00004631 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00004632 X, V, E, UE, IsXLHSInRHSPart,
4633 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00004634}
4635
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004636StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
4637 Stmt *AStmt,
4638 SourceLocation StartLoc,
4639 SourceLocation EndLoc) {
4640 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
4641
Alexey Bataev13314bf2014-10-09 04:18:56 +00004642 // OpenMP [2.16, Nesting of Regions]
4643 // If specified, a teams construct must be contained within a target
4644 // construct. That target construct must contain no statements or directives
4645 // outside of the teams construct.
4646 if (DSAStack->hasInnerTeamsRegion()) {
4647 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
4648 bool OMPTeamsFound = true;
4649 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
4650 auto I = CS->body_begin();
4651 while (I != CS->body_end()) {
4652 auto OED = dyn_cast<OMPExecutableDirective>(*I);
4653 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
4654 OMPTeamsFound = false;
4655 break;
4656 }
4657 ++I;
4658 }
4659 assert(I != CS->body_end() && "Not found statement");
4660 S = *I;
4661 }
4662 if (!OMPTeamsFound) {
4663 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
4664 Diag(DSAStack->getInnerTeamsRegionLoc(),
4665 diag::note_omp_nested_teams_construct_here);
4666 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
4667 << isa<OMPExecutableDirective>(S);
4668 return StmtError();
4669 }
4670 }
4671
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004672 getCurFunction()->setHasBranchProtectedScope();
4673
4674 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4675}
4676
Michael Wong65f367f2015-07-21 13:44:28 +00004677StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
4678 Stmt *AStmt,
4679 SourceLocation StartLoc,
4680 SourceLocation EndLoc) {
4681 getCurFunction()->setHasBranchProtectedScope();
4682
4683 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
4684 AStmt);
4685}
4686
Alexey Bataev13314bf2014-10-09 04:18:56 +00004687StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
4688 Stmt *AStmt, SourceLocation StartLoc,
4689 SourceLocation EndLoc) {
4690 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
4691 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4692 // 1.2.2 OpenMP Language Terminology
4693 // Structured block - An executable statement with a single entry at the
4694 // top and a single exit at the bottom.
4695 // The point of exit cannot be a branch out of the structured block.
4696 // longjmp() and throw() must not violate the entry/exit criteria.
4697 CS->getCapturedDecl()->setNothrow();
4698
4699 getCurFunction()->setHasBranchProtectedScope();
4700
4701 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4702}
4703
Alexey Bataev6d4ed052015-07-01 06:57:41 +00004704StmtResult
4705Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
4706 SourceLocation EndLoc,
4707 OpenMPDirectiveKind CancelRegion) {
4708 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
4709 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
4710 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
4711 << getOpenMPDirectiveName(CancelRegion);
4712 return StmtError();
4713 }
4714 if (DSAStack->isParentNowaitRegion()) {
4715 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
4716 return StmtError();
4717 }
4718 if (DSAStack->isParentOrderedRegion()) {
4719 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
4720 return StmtError();
4721 }
4722 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
4723 CancelRegion);
4724}
4725
Alexey Bataev80909872015-07-02 11:25:17 +00004726StmtResult Sema::ActOnOpenMPCancelDirective(SourceLocation StartLoc,
4727 SourceLocation EndLoc,
4728 OpenMPDirectiveKind CancelRegion) {
4729 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
4730 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
4731 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
4732 << getOpenMPDirectiveName(CancelRegion);
4733 return StmtError();
4734 }
4735 if (DSAStack->isParentNowaitRegion()) {
4736 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
4737 return StmtError();
4738 }
4739 if (DSAStack->isParentOrderedRegion()) {
4740 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
4741 return StmtError();
4742 }
4743 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, CancelRegion);
4744}
4745
Alexey Bataeved09d242014-05-28 05:53:51 +00004746OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004747 SourceLocation StartLoc,
4748 SourceLocation LParenLoc,
4749 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004750 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004751 switch (Kind) {
4752 case OMPC_if:
4753 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
4754 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00004755 case OMPC_final:
4756 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
4757 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00004758 case OMPC_num_threads:
4759 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
4760 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00004761 case OMPC_safelen:
4762 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
4763 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00004764 case OMPC_simdlen:
4765 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
4766 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00004767 case OMPC_collapse:
4768 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
4769 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004770 case OMPC_ordered:
4771 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
4772 break;
Michael Wonge710d542015-08-07 16:16:36 +00004773 case OMPC_device:
4774 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
4775 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004776 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004777 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004778 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004779 case OMPC_private:
4780 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00004781 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004782 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00004783 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00004784 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004785 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004786 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004787 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00004788 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004789 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004790 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004791 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004792 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004793 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004794 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004795 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004796 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004797 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004798 case OMPC_depend:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004799 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004800 llvm_unreachable("Clause is not allowed.");
4801 }
4802 return Res;
4803}
4804
Alexey Bataeved09d242014-05-28 05:53:51 +00004805OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004806 SourceLocation LParenLoc,
4807 SourceLocation EndLoc) {
4808 Expr *ValExpr = Condition;
4809 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
4810 !Condition->isInstantiationDependent() &&
4811 !Condition->containsUnexpandedParameterPack()) {
4812 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00004813 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004814 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004815 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004816
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004817 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004818 }
4819
4820 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
4821}
4822
Alexey Bataev3778b602014-07-17 07:32:53 +00004823OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
4824 SourceLocation StartLoc,
4825 SourceLocation LParenLoc,
4826 SourceLocation EndLoc) {
4827 Expr *ValExpr = Condition;
4828 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
4829 !Condition->isInstantiationDependent() &&
4830 !Condition->containsUnexpandedParameterPack()) {
4831 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
4832 Condition->getExprLoc(), Condition);
4833 if (Val.isInvalid())
4834 return nullptr;
4835
4836 ValExpr = Val.get();
4837 }
4838
4839 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
4840}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004841ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
4842 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00004843 if (!Op)
4844 return ExprError();
4845
4846 class IntConvertDiagnoser : public ICEConvertDiagnoser {
4847 public:
4848 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00004849 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00004850 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
4851 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004852 return S.Diag(Loc, diag::err_omp_not_integral) << T;
4853 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004854 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
4855 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004856 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
4857 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004858 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
4859 QualType T,
4860 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004861 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
4862 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004863 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
4864 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004865 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00004866 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00004867 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004868 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
4869 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004870 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
4871 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004872 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
4873 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004874 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00004875 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00004876 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004877 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
4878 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004879 llvm_unreachable("conversion functions are permitted");
4880 }
4881 } ConvertDiagnoser;
4882 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
4883}
4884
4885OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
4886 SourceLocation StartLoc,
4887 SourceLocation LParenLoc,
4888 SourceLocation EndLoc) {
4889 Expr *ValExpr = NumThreads;
4890 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
Alexey Bataev568a8332014-03-06 06:15:19 +00004891 !NumThreads->containsUnexpandedParameterPack()) {
4892 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
4893 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004894 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00004895 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004896 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00004897
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004898 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00004899
4900 // OpenMP [2.5, Restrictions]
4901 // The num_threads expression must evaluate to a positive integer value.
4902 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00004903 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
4904 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00004905 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
4906 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004907 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00004908 }
4909 }
4910
Alexey Bataeved09d242014-05-28 05:53:51 +00004911 return new (Context)
4912 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00004913}
4914
Alexey Bataev62c87d22014-03-21 04:51:18 +00004915ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
4916 OpenMPClauseKind CKind) {
4917 if (!E)
4918 return ExprError();
4919 if (E->isValueDependent() || E->isTypeDependent() ||
4920 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00004921 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00004922 llvm::APSInt Result;
4923 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
4924 if (ICE.isInvalid())
4925 return ExprError();
4926 if (!Result.isStrictlyPositive()) {
4927 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
4928 << getOpenMPClauseName(CKind) << E->getSourceRange();
4929 return ExprError();
4930 }
Alexander Musman09184fe2014-09-30 05:29:28 +00004931 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
4932 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
4933 << E->getSourceRange();
4934 return ExprError();
4935 }
Alexey Bataev9c821032015-04-30 04:23:23 +00004936 if (CKind == OMPC_collapse) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00004937 DSAStack->setCollapseNumber(DSAStack->getCollapseNumber() - 1 +
4938 Result.getExtValue());
4939 } else if (CKind == OMPC_ordered) {
4940 DSAStack->setCollapseNumber(DSAStack->getCollapseNumber() - 1 +
4941 Result.getExtValue());
Alexey Bataev9c821032015-04-30 04:23:23 +00004942 }
Alexey Bataev62c87d22014-03-21 04:51:18 +00004943 return ICE;
4944}
4945
4946OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
4947 SourceLocation LParenLoc,
4948 SourceLocation EndLoc) {
4949 // OpenMP [2.8.1, simd construct, Description]
4950 // The parameter of the safelen clause must be a constant
4951 // positive integer expression.
4952 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
4953 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004954 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00004955 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004956 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00004957}
4958
Alexey Bataev66b15b52015-08-21 11:14:16 +00004959OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
4960 SourceLocation LParenLoc,
4961 SourceLocation EndLoc) {
4962 // OpenMP [2.8.1, simd construct, Description]
4963 // The parameter of the simdlen clause must be a constant
4964 // positive integer expression.
4965 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
4966 if (Simdlen.isInvalid())
4967 return nullptr;
4968 return new (Context)
4969 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
4970}
4971
Alexander Musman64d33f12014-06-04 07:53:32 +00004972OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
4973 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00004974 SourceLocation LParenLoc,
4975 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00004976 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00004977 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00004978 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00004979 // The parameter of the collapse clause must be a constant
4980 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00004981 ExprResult NumForLoopsResult =
4982 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
4983 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00004984 return nullptr;
4985 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00004986 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00004987}
4988
Alexey Bataev10e775f2015-07-30 11:36:16 +00004989OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
4990 SourceLocation EndLoc,
4991 SourceLocation LParenLoc,
4992 Expr *NumForLoops) {
4993 DSAStack->setOrderedRegion();
4994 // OpenMP [2.7.1, loop construct, Description]
4995 // OpenMP [2.8.1, simd construct, Description]
4996 // OpenMP [2.9.6, distribute construct, Description]
4997 // The parameter of the ordered clause must be a constant
4998 // positive integer expression if any.
4999 if (NumForLoops && LParenLoc.isValid()) {
5000 ExprResult NumForLoopsResult =
5001 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
5002 if (NumForLoopsResult.isInvalid())
5003 return nullptr;
5004 NumForLoops = NumForLoopsResult.get();
5005 }
5006 return new (Context)
5007 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
5008}
5009
Alexey Bataeved09d242014-05-28 05:53:51 +00005010OMPClause *Sema::ActOnOpenMPSimpleClause(
5011 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
5012 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005013 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005014 switch (Kind) {
5015 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00005016 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00005017 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
5018 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005019 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005020 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00005021 Res = ActOnOpenMPProcBindClause(
5022 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
5023 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005024 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005025 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005026 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00005027 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00005028 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005029 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00005030 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005031 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005032 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005033 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00005034 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00005035 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005036 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00005037 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005038 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005039 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005040 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005041 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005042 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005043 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005044 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005045 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005046 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005047 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005048 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005049 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005050 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005051 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005052 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005053 case OMPC_device:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005054 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005055 llvm_unreachable("Clause is not allowed.");
5056 }
5057 return Res;
5058}
5059
5060OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
5061 SourceLocation KindKwLoc,
5062 SourceLocation StartLoc,
5063 SourceLocation LParenLoc,
5064 SourceLocation EndLoc) {
5065 if (Kind == OMPC_DEFAULT_unknown) {
5066 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005067 static_assert(OMPC_DEFAULT_unknown > 0,
5068 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00005069 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005070 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005071 Values += "'";
5072 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
5073 Values += "'";
5074 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005075 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005076 Values += " or ";
5077 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005078 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005079 break;
5080 default:
5081 Values += Sep;
5082 break;
5083 }
5084 }
5085 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00005086 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005087 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005088 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00005089 switch (Kind) {
5090 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005091 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005092 break;
5093 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005094 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005095 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005096 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005097 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00005098 break;
5099 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005100 return new (Context)
5101 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005102}
5103
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005104OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
5105 SourceLocation KindKwLoc,
5106 SourceLocation StartLoc,
5107 SourceLocation LParenLoc,
5108 SourceLocation EndLoc) {
5109 if (Kind == OMPC_PROC_BIND_unknown) {
5110 std::string Values;
5111 std::string Sep(", ");
5112 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
5113 Values += "'";
5114 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
5115 Values += "'";
5116 switch (i) {
5117 case OMPC_PROC_BIND_unknown - 2:
5118 Values += " or ";
5119 break;
5120 case OMPC_PROC_BIND_unknown - 1:
5121 break;
5122 default:
5123 Values += Sep;
5124 break;
5125 }
5126 }
5127 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00005128 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005129 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005130 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005131 return new (Context)
5132 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005133}
5134
Alexey Bataev56dafe82014-06-20 07:16:17 +00005135OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
5136 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
5137 SourceLocation StartLoc, SourceLocation LParenLoc,
5138 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
5139 SourceLocation EndLoc) {
5140 OMPClause *Res = nullptr;
5141 switch (Kind) {
5142 case OMPC_schedule:
5143 Res = ActOnOpenMPScheduleClause(
5144 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
5145 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
5146 break;
5147 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005148 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005149 case OMPC_num_threads:
5150 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005151 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005152 case OMPC_collapse:
5153 case OMPC_default:
5154 case OMPC_proc_bind:
5155 case OMPC_private:
5156 case OMPC_firstprivate:
5157 case OMPC_lastprivate:
5158 case OMPC_shared:
5159 case OMPC_reduction:
5160 case OMPC_linear:
5161 case OMPC_aligned:
5162 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005163 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005164 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005165 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005166 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005167 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005168 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005169 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005170 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005171 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005172 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005173 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005174 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005175 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005176 case OMPC_device:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005177 case OMPC_unknown:
5178 llvm_unreachable("Clause is not allowed.");
5179 }
5180 return Res;
5181}
5182
5183OMPClause *Sema::ActOnOpenMPScheduleClause(
5184 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
5185 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
5186 SourceLocation EndLoc) {
5187 if (Kind == OMPC_SCHEDULE_unknown) {
5188 std::string Values;
5189 std::string Sep(", ");
5190 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
5191 Values += "'";
5192 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
5193 Values += "'";
5194 switch (i) {
5195 case OMPC_SCHEDULE_unknown - 2:
5196 Values += " or ";
5197 break;
5198 case OMPC_SCHEDULE_unknown - 1:
5199 break;
5200 default:
5201 Values += Sep;
5202 break;
5203 }
5204 }
5205 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
5206 << Values << getOpenMPClauseName(OMPC_schedule);
5207 return nullptr;
5208 }
5209 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00005210 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00005211 if (ChunkSize) {
5212 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
5213 !ChunkSize->isInstantiationDependent() &&
5214 !ChunkSize->containsUnexpandedParameterPack()) {
5215 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
5216 ExprResult Val =
5217 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
5218 if (Val.isInvalid())
5219 return nullptr;
5220
5221 ValExpr = Val.get();
5222
5223 // OpenMP [2.7.1, Restrictions]
5224 // chunk_size must be a loop invariant integer expression with a positive
5225 // value.
5226 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00005227 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
5228 if (Result.isSigned() && !Result.isStrictlyPositive()) {
5229 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
5230 << "schedule" << ChunkSize->getSourceRange();
5231 return nullptr;
5232 }
5233 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
5234 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
5235 ChunkSize->getType(), ".chunk.");
5236 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
5237 ChunkSize->getExprLoc(),
5238 /*RefersToCapture=*/true);
5239 HelperValExpr = ImpVarRef;
Alexey Bataev56dafe82014-06-20 07:16:17 +00005240 }
5241 }
5242 }
5243
5244 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
Alexey Bataev040d5402015-05-12 08:35:28 +00005245 EndLoc, Kind, ValExpr, HelperValExpr);
Alexey Bataev56dafe82014-06-20 07:16:17 +00005246}
5247
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005248OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
5249 SourceLocation StartLoc,
5250 SourceLocation EndLoc) {
5251 OMPClause *Res = nullptr;
5252 switch (Kind) {
5253 case OMPC_ordered:
5254 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
5255 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00005256 case OMPC_nowait:
5257 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
5258 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005259 case OMPC_untied:
5260 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
5261 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005262 case OMPC_mergeable:
5263 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
5264 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005265 case OMPC_read:
5266 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
5267 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00005268 case OMPC_write:
5269 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
5270 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005271 case OMPC_update:
5272 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
5273 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00005274 case OMPC_capture:
5275 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
5276 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005277 case OMPC_seq_cst:
5278 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
5279 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005280 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005281 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005282 case OMPC_num_threads:
5283 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005284 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005285 case OMPC_collapse:
5286 case OMPC_schedule:
5287 case OMPC_private:
5288 case OMPC_firstprivate:
5289 case OMPC_lastprivate:
5290 case OMPC_shared:
5291 case OMPC_reduction:
5292 case OMPC_linear:
5293 case OMPC_aligned:
5294 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005295 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005296 case OMPC_default:
5297 case OMPC_proc_bind:
5298 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005299 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005300 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005301 case OMPC_device:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005302 case OMPC_unknown:
5303 llvm_unreachable("Clause is not allowed.");
5304 }
5305 return Res;
5306}
5307
Alexey Bataev236070f2014-06-20 11:19:47 +00005308OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
5309 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00005310 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00005311 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
5312}
5313
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005314OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
5315 SourceLocation EndLoc) {
5316 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
5317}
5318
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005319OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
5320 SourceLocation EndLoc) {
5321 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
5322}
5323
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005324OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
5325 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005326 return new (Context) OMPReadClause(StartLoc, EndLoc);
5327}
5328
Alexey Bataevdea47612014-07-23 07:46:59 +00005329OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
5330 SourceLocation EndLoc) {
5331 return new (Context) OMPWriteClause(StartLoc, EndLoc);
5332}
5333
Alexey Bataev67a4f222014-07-23 10:25:33 +00005334OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
5335 SourceLocation EndLoc) {
5336 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
5337}
5338
Alexey Bataev459dec02014-07-24 06:46:57 +00005339OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
5340 SourceLocation EndLoc) {
5341 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
5342}
5343
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005344OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
5345 SourceLocation EndLoc) {
5346 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
5347}
5348
Alexey Bataevc5e02582014-06-16 07:08:35 +00005349OMPClause *Sema::ActOnOpenMPVarListClause(
5350 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
5351 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
5352 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005353 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Alexey Bataev182227b2015-08-20 10:54:39 +00005354 OpenMPLinearClauseKind LinKind, SourceLocation DepLinLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005355 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005356 switch (Kind) {
5357 case OMPC_private:
5358 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5359 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005360 case OMPC_firstprivate:
5361 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5362 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00005363 case OMPC_lastprivate:
5364 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5365 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00005366 case OMPC_shared:
5367 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
5368 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005369 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00005370 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
5371 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005372 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00005373 case OMPC_linear:
5374 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Alexey Bataev182227b2015-08-20 10:54:39 +00005375 LinKind, DepLinLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00005376 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005377 case OMPC_aligned:
5378 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
5379 ColonLoc, EndLoc);
5380 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005381 case OMPC_copyin:
5382 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
5383 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00005384 case OMPC_copyprivate:
5385 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5386 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00005387 case OMPC_flush:
5388 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
5389 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005390 case OMPC_depend:
Alexey Bataev182227b2015-08-20 10:54:39 +00005391 Res = ActOnOpenMPDependClause(DepKind, DepLinLoc, ColonLoc, VarList, StartLoc,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005392 LParenLoc, EndLoc);
5393 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005394 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005395 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00005396 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00005397 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005398 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00005399 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005400 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005401 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005402 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005403 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005404 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005405 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005406 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005407 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005408 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005409 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005410 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005411 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005412 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00005413 case OMPC_device:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005414 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005415 llvm_unreachable("Clause is not allowed.");
5416 }
5417 return Res;
5418}
5419
5420OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
5421 SourceLocation StartLoc,
5422 SourceLocation LParenLoc,
5423 SourceLocation EndLoc) {
5424 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00005425 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00005426 for (auto &RefExpr : VarList) {
5427 assert(RefExpr && "NULL expr in OpenMP private clause.");
5428 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005429 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005430 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005431 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005432 continue;
5433 }
5434
Alexey Bataeved09d242014-05-28 05:53:51 +00005435 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005436 // OpenMP [2.1, C/C++]
5437 // A list item is a variable name.
5438 // OpenMP [2.9.3.3, Restrictions, p.1]
5439 // A variable that is part of another variable (as an array or
5440 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00005441 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005442 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005443 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005444 continue;
5445 }
5446 Decl *D = DE->getDecl();
5447 VarDecl *VD = cast<VarDecl>(D);
5448
5449 QualType Type = VD->getType();
5450 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5451 // It will be analyzed later.
5452 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005453 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005454 continue;
5455 }
5456
5457 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5458 // A variable that appears in a private clause must not have an incomplete
5459 // type or a reference type.
5460 if (RequireCompleteType(ELoc, Type,
5461 diag::err_omp_private_incomplete_type)) {
5462 continue;
5463 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00005464 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005465
Alexey Bataev758e55e2013-09-06 18:03:48 +00005466 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5467 // in a Construct]
5468 // Variables with the predetermined data-sharing attributes may not be
5469 // listed in data-sharing attributes clauses, except for the cases
5470 // listed below. For these exceptions only, listing a predetermined
5471 // variable in a data-sharing attribute clause is allowed and overrides
5472 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005473 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005474 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005475 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
5476 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005477 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005478 continue;
5479 }
5480
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005481 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00005482 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005483 DSAStack->getCurrentDirective() == OMPD_task) {
5484 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
5485 << getOpenMPClauseName(OMPC_private) << Type
5486 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
5487 bool IsDecl =
5488 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5489 Diag(VD->getLocation(),
5490 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5491 << VD;
5492 continue;
5493 }
5494
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005495 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
5496 // A variable of class type (or array thereof) that appears in a private
5497 // clause requires an accessible, unambiguous default constructor for the
5498 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00005499 // Generate helper private variable and initialize it with the default
5500 // value. The address of the original variable is replaced by the address of
5501 // the new private variable in CodeGen. This new variable is not added to
5502 // IdResolver, so the code in the OpenMP region uses original variable for
5503 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005504 Type = Type.getUnqualifiedType();
5505 auto VDPrivate = buildVarDecl(*this, DE->getExprLoc(), Type, VD->getName());
Alexey Bataev39f915b82015-05-08 10:41:21 +00005506 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005507 if (VDPrivate->isInvalidDecl())
5508 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005509 auto VDPrivateRefExpr = buildDeclRefExpr(
5510 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev03b340a2014-10-21 03:16:40 +00005511
Alexey Bataev758e55e2013-09-06 18:03:48 +00005512 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005513 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005514 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005515 }
5516
Alexey Bataeved09d242014-05-28 05:53:51 +00005517 if (Vars.empty())
5518 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005519
Alexey Bataev03b340a2014-10-21 03:16:40 +00005520 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
5521 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005522}
5523
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005524namespace {
5525class DiagsUninitializedSeveretyRAII {
5526private:
5527 DiagnosticsEngine &Diags;
5528 SourceLocation SavedLoc;
5529 bool IsIgnored;
5530
5531public:
5532 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
5533 bool IsIgnored)
5534 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
5535 if (!IsIgnored) {
5536 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
5537 /*Map*/ diag::Severity::Ignored, Loc);
5538 }
5539 }
5540 ~DiagsUninitializedSeveretyRAII() {
5541 if (!IsIgnored)
5542 Diags.popMappings(SavedLoc);
5543 }
5544};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00005545}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005546
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005547OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
5548 SourceLocation StartLoc,
5549 SourceLocation LParenLoc,
5550 SourceLocation EndLoc) {
5551 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005552 SmallVector<Expr *, 8> PrivateCopies;
5553 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005554 bool IsImplicitClause =
5555 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
5556 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
5557
Alexey Bataeved09d242014-05-28 05:53:51 +00005558 for (auto &RefExpr : VarList) {
5559 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
5560 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005561 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005562 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005563 PrivateCopies.push_back(nullptr);
5564 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005565 continue;
5566 }
5567
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005568 SourceLocation ELoc =
5569 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005570 // OpenMP [2.1, C/C++]
5571 // A list item is a variable name.
5572 // OpenMP [2.9.3.3, Restrictions, p.1]
5573 // A variable that is part of another variable (as an array or
5574 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00005575 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005576 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005577 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005578 continue;
5579 }
5580 Decl *D = DE->getDecl();
5581 VarDecl *VD = cast<VarDecl>(D);
5582
5583 QualType Type = VD->getType();
5584 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5585 // It will be analyzed later.
5586 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005587 PrivateCopies.push_back(nullptr);
5588 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005589 continue;
5590 }
5591
5592 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5593 // A variable that appears in a private clause must not have an incomplete
5594 // type or a reference type.
5595 if (RequireCompleteType(ELoc, Type,
5596 diag::err_omp_firstprivate_incomplete_type)) {
5597 continue;
5598 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00005599 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005600
5601 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
5602 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00005603 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005604 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005605 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005606
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005607 // If an implicit firstprivate variable found it was checked already.
5608 if (!IsImplicitClause) {
5609 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005610 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005611 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
5612 // A list item that specifies a given variable may not appear in more
5613 // than one clause on the same directive, except that a variable may be
5614 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005615 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00005616 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005617 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00005618 << getOpenMPClauseName(DVar.CKind)
5619 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005620 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005621 continue;
5622 }
5623
5624 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5625 // in a Construct]
5626 // Variables with the predetermined data-sharing attributes may not be
5627 // listed in data-sharing attributes clauses, except for the cases
5628 // listed below. For these exceptions only, listing a predetermined
5629 // variable in a data-sharing attribute clause is allowed and overrides
5630 // the variable's predetermined data-sharing attributes.
5631 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5632 // in a Construct, C/C++, p.2]
5633 // Variables with const-qualified type having no mutable member may be
5634 // listed in a firstprivate clause, even if they are static data members.
5635 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
5636 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
5637 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00005638 << getOpenMPClauseName(DVar.CKind)
5639 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005640 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005641 continue;
5642 }
5643
Alexey Bataevf29276e2014-06-18 04:14:57 +00005644 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005645 // OpenMP [2.9.3.4, Restrictions, p.2]
5646 // A list item that is private within a parallel region must not appear
5647 // in a firstprivate clause on a worksharing construct if any of the
5648 // worksharing regions arising from the worksharing construct ever bind
5649 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00005650 if (isOpenMPWorksharingDirective(CurrDir) &&
5651 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005652 DVar = DSAStack->getImplicitDSA(VD, true);
5653 if (DVar.CKind != OMPC_shared &&
5654 (isOpenMPParallelDirective(DVar.DKind) ||
5655 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00005656 Diag(ELoc, diag::err_omp_required_access)
5657 << getOpenMPClauseName(OMPC_firstprivate)
5658 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005659 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005660 continue;
5661 }
5662 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005663 // OpenMP [2.9.3.4, Restrictions, p.3]
5664 // A list item that appears in a reduction clause of a parallel construct
5665 // must not appear in a firstprivate clause on a worksharing or task
5666 // construct if any of the worksharing or task regions arising from the
5667 // worksharing or task construct ever bind to any of the parallel regions
5668 // arising from the parallel construct.
5669 // OpenMP [2.9.3.4, Restrictions, p.4]
5670 // A list item that appears in a reduction clause in worksharing
5671 // construct must not appear in a firstprivate clause in a task construct
5672 // encountered during execution of any of the worksharing regions arising
5673 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005674 if (CurrDir == OMPD_task) {
5675 DVar =
5676 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
5677 [](OpenMPDirectiveKind K) -> bool {
5678 return isOpenMPParallelDirective(K) ||
5679 isOpenMPWorksharingDirective(K);
5680 },
5681 false);
5682 if (DVar.CKind == OMPC_reduction &&
5683 (isOpenMPParallelDirective(DVar.DKind) ||
5684 isOpenMPWorksharingDirective(DVar.DKind))) {
5685 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
5686 << getOpenMPDirectiveName(DVar.DKind);
5687 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5688 continue;
5689 }
5690 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005691 }
5692
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005693 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00005694 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005695 DSAStack->getCurrentDirective() == OMPD_task) {
5696 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
5697 << getOpenMPClauseName(OMPC_firstprivate) << Type
5698 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
5699 bool IsDecl =
5700 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5701 Diag(VD->getLocation(),
5702 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5703 << VD;
5704 continue;
5705 }
5706
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005707 Type = Type.getUnqualifiedType();
5708 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName());
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005709 // Generate helper private variable and initialize it with the value of the
5710 // original variable. The address of the original variable is replaced by
5711 // the address of the new private variable in the CodeGen. This new variable
5712 // is not added to IdResolver, so the code in the OpenMP region uses
5713 // original variable for proper diagnostics and variable capturing.
5714 Expr *VDInitRefExpr = nullptr;
5715 // For arrays generate initializer for single element and replace it by the
5716 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005717 if (Type->isArrayType()) {
5718 auto VDInit =
5719 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
5720 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005721 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005722 ElemType = ElemType.getUnqualifiedType();
5723 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
5724 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00005725 InitializedEntity Entity =
5726 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005727 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
5728
5729 InitializationSequence InitSeq(*this, Entity, Kind, Init);
5730 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
5731 if (Result.isInvalid())
5732 VDPrivate->setInvalidDecl();
5733 else
5734 VDPrivate->setInit(Result.getAs<Expr>());
5735 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00005736 auto *VDInit =
Alexey Bataev39f915b82015-05-08 10:41:21 +00005737 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005738 VDInitRefExpr =
5739 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00005740 AddInitializerToDecl(VDPrivate,
5741 DefaultLvalueConversion(VDInitRefExpr).get(),
5742 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005743 }
5744 if (VDPrivate->isInvalidDecl()) {
5745 if (IsImplicitClause) {
5746 Diag(DE->getExprLoc(),
5747 diag::note_omp_task_predetermined_firstprivate_here);
5748 }
5749 continue;
5750 }
5751 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005752 auto VDPrivateRefExpr = buildDeclRefExpr(
5753 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005754 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
5755 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005756 PrivateCopies.push_back(VDPrivateRefExpr);
5757 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005758 }
5759
Alexey Bataeved09d242014-05-28 05:53:51 +00005760 if (Vars.empty())
5761 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005762
5763 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005764 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005765}
5766
Alexander Musman1bb328c2014-06-04 13:06:39 +00005767OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
5768 SourceLocation StartLoc,
5769 SourceLocation LParenLoc,
5770 SourceLocation EndLoc) {
5771 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00005772 SmallVector<Expr *, 8> SrcExprs;
5773 SmallVector<Expr *, 8> DstExprs;
5774 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00005775 for (auto &RefExpr : VarList) {
5776 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
5777 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5778 // It will be analyzed later.
5779 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00005780 SrcExprs.push_back(nullptr);
5781 DstExprs.push_back(nullptr);
5782 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005783 continue;
5784 }
5785
5786 SourceLocation ELoc = RefExpr->getExprLoc();
5787 // OpenMP [2.1, C/C++]
5788 // A list item is a variable name.
5789 // OpenMP [2.14.3.5, Restrictions, p.1]
5790 // A variable that is part of another variable (as an array or structure
5791 // element) cannot appear in a lastprivate clause.
5792 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
5793 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5794 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5795 continue;
5796 }
5797 Decl *D = DE->getDecl();
5798 VarDecl *VD = cast<VarDecl>(D);
5799
5800 QualType Type = VD->getType();
5801 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5802 // It will be analyzed later.
5803 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00005804 SrcExprs.push_back(nullptr);
5805 DstExprs.push_back(nullptr);
5806 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005807 continue;
5808 }
5809
5810 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
5811 // A variable that appears in a lastprivate clause must not have an
5812 // incomplete type or a reference type.
5813 if (RequireCompleteType(ELoc, Type,
5814 diag::err_omp_lastprivate_incomplete_type)) {
5815 continue;
5816 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00005817 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00005818
5819 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
5820 // in a Construct]
5821 // Variables with the predetermined data-sharing attributes may not be
5822 // listed in data-sharing attributes clauses, except for the cases
5823 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005824 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005825 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
5826 DVar.CKind != OMPC_firstprivate &&
5827 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
5828 Diag(ELoc, diag::err_omp_wrong_dsa)
5829 << getOpenMPClauseName(DVar.CKind)
5830 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005831 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005832 continue;
5833 }
5834
Alexey Bataevf29276e2014-06-18 04:14:57 +00005835 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
5836 // OpenMP [2.14.3.5, Restrictions, p.2]
5837 // A list item that is private within a parallel region, or that appears in
5838 // the reduction clause of a parallel construct, must not appear in a
5839 // lastprivate clause on a worksharing construct if any of the corresponding
5840 // worksharing regions ever binds to any of the corresponding parallel
5841 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005842 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00005843 if (isOpenMPWorksharingDirective(CurrDir) &&
5844 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005845 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005846 if (DVar.CKind != OMPC_shared) {
5847 Diag(ELoc, diag::err_omp_required_access)
5848 << getOpenMPClauseName(OMPC_lastprivate)
5849 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005850 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005851 continue;
5852 }
5853 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00005854 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00005855 // A variable of class type (or array thereof) that appears in a
5856 // lastprivate clause requires an accessible, unambiguous default
5857 // constructor for the class type, unless the list item is also specified
5858 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00005859 // A variable of class type (or array thereof) that appears in a
5860 // lastprivate clause requires an accessible, unambiguous copy assignment
5861 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00005862 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00005863 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev38e89532015-04-16 04:54:05 +00005864 Type.getUnqualifiedType(), ".lastprivate.src");
Alexey Bataev39f915b82015-05-08 10:41:21 +00005865 auto *PseudoSrcExpr = buildDeclRefExpr(
5866 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00005867 auto *DstVD =
Alexey Bataev39f915b82015-05-08 10:41:21 +00005868 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst");
Alexey Bataev38e89532015-04-16 04:54:05 +00005869 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00005870 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00005871 // For arrays generate assignment operation for single element and replace
5872 // it by the original array element in CodeGen.
5873 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
5874 PseudoDstExpr, PseudoSrcExpr);
5875 if (AssignmentOp.isInvalid())
5876 continue;
5877 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
5878 /*DiscardedValue=*/true);
5879 if (AssignmentOp.isInvalid())
5880 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00005881
Alexey Bataev39f915b82015-05-08 10:41:21 +00005882 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005883 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005884 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00005885 SrcExprs.push_back(PseudoSrcExpr);
5886 DstExprs.push_back(PseudoDstExpr);
5887 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00005888 }
5889
5890 if (Vars.empty())
5891 return nullptr;
5892
5893 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00005894 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005895}
5896
Alexey Bataev758e55e2013-09-06 18:03:48 +00005897OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
5898 SourceLocation StartLoc,
5899 SourceLocation LParenLoc,
5900 SourceLocation EndLoc) {
5901 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00005902 for (auto &RefExpr : VarList) {
5903 assert(RefExpr && "NULL expr in OpenMP shared clause.");
5904 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00005905 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005906 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005907 continue;
5908 }
5909
Alexey Bataeved09d242014-05-28 05:53:51 +00005910 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00005911 // OpenMP [2.1, C/C++]
5912 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00005913 // OpenMP [2.14.3.2, Restrictions, p.1]
5914 // A variable that is part of another variable (as an array or structure
5915 // element) cannot appear in a shared unless it is a static data member
5916 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00005917 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005918 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005919 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00005920 continue;
5921 }
5922 Decl *D = DE->getDecl();
5923 VarDecl *VD = cast<VarDecl>(D);
5924
5925 QualType Type = VD->getType();
5926 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5927 // It will be analyzed later.
5928 Vars.push_back(DE);
5929 continue;
5930 }
5931
5932 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5933 // in a Construct]
5934 // Variables with the predetermined data-sharing attributes may not be
5935 // listed in data-sharing attributes clauses, except for the cases
5936 // listed below. For these exceptions only, listing a predetermined
5937 // variable in a data-sharing attribute clause is allowed and overrides
5938 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005939 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00005940 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
5941 DVar.RefExpr) {
5942 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
5943 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005944 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005945 continue;
5946 }
5947
5948 DSAStack->addDSA(VD, DE, OMPC_shared);
5949 Vars.push_back(DE);
5950 }
5951
Alexey Bataeved09d242014-05-28 05:53:51 +00005952 if (Vars.empty())
5953 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00005954
5955 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
5956}
5957
Alexey Bataevc5e02582014-06-16 07:08:35 +00005958namespace {
5959class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
5960 DSAStackTy *Stack;
5961
5962public:
5963 bool VisitDeclRefExpr(DeclRefExpr *E) {
5964 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005965 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005966 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
5967 return false;
5968 if (DVar.CKind != OMPC_unknown)
5969 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00005970 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005971 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005972 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00005973 return true;
5974 return false;
5975 }
5976 return false;
5977 }
5978 bool VisitStmt(Stmt *S) {
5979 for (auto Child : S->children()) {
5980 if (Child && Visit(Child))
5981 return true;
5982 }
5983 return false;
5984 }
Alexey Bataev23b69422014-06-18 07:08:49 +00005985 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00005986};
Alexey Bataev23b69422014-06-18 07:08:49 +00005987} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00005988
5989OMPClause *Sema::ActOnOpenMPReductionClause(
5990 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
5991 SourceLocation ColonLoc, SourceLocation EndLoc,
5992 CXXScopeSpec &ReductionIdScopeSpec,
5993 const DeclarationNameInfo &ReductionId) {
5994 // TODO: Allow scope specification search when 'declare reduction' is
5995 // supported.
5996 assert(ReductionIdScopeSpec.isEmpty() &&
5997 "No support for scoped reduction identifiers yet.");
5998
5999 auto DN = ReductionId.getName();
6000 auto OOK = DN.getCXXOverloadedOperator();
6001 BinaryOperatorKind BOK = BO_Comma;
6002
6003 // OpenMP [2.14.3.6, reduction clause]
6004 // C
6005 // reduction-identifier is either an identifier or one of the following
6006 // operators: +, -, *, &, |, ^, && and ||
6007 // C++
6008 // reduction-identifier is either an id-expression or one of the following
6009 // operators: +, -, *, &, |, ^, && and ||
6010 // FIXME: Only 'min' and 'max' identifiers are supported for now.
6011 switch (OOK) {
6012 case OO_Plus:
6013 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006014 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006015 break;
6016 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006017 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006018 break;
6019 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006020 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006021 break;
6022 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006023 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006024 break;
6025 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006026 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006027 break;
6028 case OO_AmpAmp:
6029 BOK = BO_LAnd;
6030 break;
6031 case OO_PipePipe:
6032 BOK = BO_LOr;
6033 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006034 case OO_New:
6035 case OO_Delete:
6036 case OO_Array_New:
6037 case OO_Array_Delete:
6038 case OO_Slash:
6039 case OO_Percent:
6040 case OO_Tilde:
6041 case OO_Exclaim:
6042 case OO_Equal:
6043 case OO_Less:
6044 case OO_Greater:
6045 case OO_LessEqual:
6046 case OO_GreaterEqual:
6047 case OO_PlusEqual:
6048 case OO_MinusEqual:
6049 case OO_StarEqual:
6050 case OO_SlashEqual:
6051 case OO_PercentEqual:
6052 case OO_CaretEqual:
6053 case OO_AmpEqual:
6054 case OO_PipeEqual:
6055 case OO_LessLess:
6056 case OO_GreaterGreater:
6057 case OO_LessLessEqual:
6058 case OO_GreaterGreaterEqual:
6059 case OO_EqualEqual:
6060 case OO_ExclaimEqual:
6061 case OO_PlusPlus:
6062 case OO_MinusMinus:
6063 case OO_Comma:
6064 case OO_ArrowStar:
6065 case OO_Arrow:
6066 case OO_Call:
6067 case OO_Subscript:
6068 case OO_Conditional:
6069 case NUM_OVERLOADED_OPERATORS:
6070 llvm_unreachable("Unexpected reduction identifier");
6071 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006072 if (auto II = DN.getAsIdentifierInfo()) {
6073 if (II->isStr("max"))
6074 BOK = BO_GT;
6075 else if (II->isStr("min"))
6076 BOK = BO_LT;
6077 }
6078 break;
6079 }
6080 SourceRange ReductionIdRange;
6081 if (ReductionIdScopeSpec.isValid()) {
6082 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
6083 }
6084 ReductionIdRange.setEnd(ReductionId.getEndLoc());
6085 if (BOK == BO_Comma) {
6086 // Not allowed reduction identifier is found.
6087 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
6088 << ReductionIdRange;
6089 return nullptr;
6090 }
6091
6092 SmallVector<Expr *, 8> Vars;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006093 SmallVector<Expr *, 8> LHSs;
6094 SmallVector<Expr *, 8> RHSs;
6095 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006096 for (auto RefExpr : VarList) {
6097 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
6098 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6099 // It will be analyzed later.
6100 Vars.push_back(RefExpr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006101 LHSs.push_back(nullptr);
6102 RHSs.push_back(nullptr);
6103 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006104 continue;
6105 }
6106
6107 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
6108 RefExpr->isInstantiationDependent() ||
6109 RefExpr->containsUnexpandedParameterPack()) {
6110 // It will be analyzed later.
6111 Vars.push_back(RefExpr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006112 LHSs.push_back(nullptr);
6113 RHSs.push_back(nullptr);
6114 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006115 continue;
6116 }
6117
6118 auto ELoc = RefExpr->getExprLoc();
6119 auto ERange = RefExpr->getSourceRange();
6120 // OpenMP [2.1, C/C++]
6121 // A list item is a variable or array section, subject to the restrictions
6122 // specified in Section 2.4 on page 42 and in each of the sections
6123 // describing clauses and directives for which a list appears.
6124 // OpenMP [2.14.3.3, Restrictions, p.1]
6125 // A variable that is part of another variable (as an array or
6126 // structure element) cannot appear in a private clause.
6127 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
6128 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6129 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
6130 continue;
6131 }
6132 auto D = DE->getDecl();
6133 auto VD = cast<VarDecl>(D);
6134 auto Type = VD->getType();
6135 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6136 // A variable that appears in a private clause must not have an incomplete
6137 // type or a reference type.
6138 if (RequireCompleteType(ELoc, Type,
6139 diag::err_omp_reduction_incomplete_type))
6140 continue;
6141 // OpenMP [2.14.3.6, reduction clause, Restrictions]
6142 // Arrays may not appear in a reduction clause.
6143 if (Type.getNonReferenceType()->isArrayType()) {
6144 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
6145 bool IsDecl =
6146 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6147 Diag(VD->getLocation(),
6148 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6149 << VD;
6150 continue;
6151 }
6152 // OpenMP [2.14.3.6, reduction clause, Restrictions]
6153 // A list item that appears in a reduction clause must not be
6154 // const-qualified.
6155 if (Type.getNonReferenceType().isConstant(Context)) {
6156 Diag(ELoc, diag::err_omp_const_variable)
6157 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
6158 bool IsDecl =
6159 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6160 Diag(VD->getLocation(),
6161 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6162 << VD;
6163 continue;
6164 }
6165 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
6166 // If a list-item is a reference type then it must bind to the same object
6167 // for all threads of the team.
6168 VarDecl *VDDef = VD->getDefinition();
6169 if (Type->isReferenceType() && VDDef) {
6170 DSARefChecker Check(DSAStack);
6171 if (Check.Visit(VDDef->getInit())) {
6172 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
6173 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
6174 continue;
6175 }
6176 }
6177 // OpenMP [2.14.3.6, reduction clause, Restrictions]
6178 // The type of a list item that appears in a reduction clause must be valid
6179 // for the reduction-identifier. For a max or min reduction in C, the type
6180 // of the list item must be an allowed arithmetic data type: char, int,
6181 // float, double, or _Bool, possibly modified with long, short, signed, or
6182 // unsigned. For a max or min reduction in C++, the type of the list item
6183 // must be an allowed arithmetic data type: char, wchar_t, int, float,
6184 // double, or bool, possibly modified with long, short, signed, or unsigned.
6185 if ((BOK == BO_GT || BOK == BO_LT) &&
6186 !(Type->isScalarType() ||
6187 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
6188 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
6189 << getLangOpts().CPlusPlus;
6190 bool IsDecl =
6191 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6192 Diag(VD->getLocation(),
6193 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6194 << VD;
6195 continue;
6196 }
6197 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
6198 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
6199 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
6200 bool IsDecl =
6201 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6202 Diag(VD->getLocation(),
6203 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6204 << VD;
6205 continue;
6206 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00006207 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
6208 // in a Construct]
6209 // Variables with the predetermined data-sharing attributes may not be
6210 // listed in data-sharing attributes clauses, except for the cases
6211 // listed below. For these exceptions only, listing a predetermined
6212 // variable in a data-sharing attribute clause is allowed and overrides
6213 // the variable's predetermined data-sharing attributes.
6214 // OpenMP [2.14.3.6, Restrictions, p.3]
6215 // Any number of reduction clauses can be specified on the directive,
6216 // but a list item can appear only once in the reduction clauses for that
6217 // directive.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006218 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006219 if (DVar.CKind == OMPC_reduction) {
6220 Diag(ELoc, diag::err_omp_once_referenced)
6221 << getOpenMPClauseName(OMPC_reduction);
6222 if (DVar.RefExpr) {
6223 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
6224 }
6225 } else if (DVar.CKind != OMPC_unknown) {
6226 Diag(ELoc, diag::err_omp_wrong_dsa)
6227 << getOpenMPClauseName(DVar.CKind)
6228 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006229 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006230 continue;
6231 }
6232
6233 // OpenMP [2.14.3.6, Restrictions, p.1]
6234 // A list item that appears in a reduction clause of a worksharing
6235 // construct must be shared in the parallel regions to which any of the
6236 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00006237 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00006238 if (isOpenMPWorksharingDirective(CurrDir) &&
6239 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006240 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006241 if (DVar.CKind != OMPC_shared) {
6242 Diag(ELoc, diag::err_omp_required_access)
6243 << getOpenMPClauseName(OMPC_reduction)
6244 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006245 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006246 continue;
6247 }
6248 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006249 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00006250 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs");
6251 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006252 // Add initializer for private variable.
6253 Expr *Init = nullptr;
6254 switch (BOK) {
6255 case BO_Add:
6256 case BO_Xor:
6257 case BO_Or:
6258 case BO_LOr:
6259 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
6260 if (Type->isScalarType() || Type->isAnyComplexType()) {
6261 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00006262 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006263 break;
6264 case BO_Mul:
6265 case BO_LAnd:
6266 if (Type->isScalarType() || Type->isAnyComplexType()) {
6267 // '*' and '&&' reduction ops - initializer is '1'.
6268 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
6269 }
6270 break;
6271 case BO_And: {
6272 // '&' reduction op - initializer is '~0'.
6273 QualType OrigType = Type;
6274 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
6275 Type = ComplexTy->getElementType();
6276 }
6277 if (Type->isRealFloatingType()) {
6278 llvm::APFloat InitValue =
6279 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
6280 /*isIEEE=*/true);
6281 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
6282 Type, ELoc);
6283 } else if (Type->isScalarType()) {
6284 auto Size = Context.getTypeSize(Type);
6285 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
6286 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
6287 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
6288 }
6289 if (Init && OrigType->isAnyComplexType()) {
6290 // Init = 0xFFFF + 0xFFFFi;
6291 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
6292 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
6293 }
6294 Type = OrigType;
6295 break;
6296 }
6297 case BO_LT:
6298 case BO_GT: {
6299 // 'min' reduction op - initializer is 'Largest representable number in
6300 // the reduction list item type'.
6301 // 'max' reduction op - initializer is 'Least representable number in
6302 // the reduction list item type'.
6303 if (Type->isIntegerType() || Type->isPointerType()) {
6304 bool IsSigned = Type->hasSignedIntegerRepresentation();
6305 auto Size = Context.getTypeSize(Type);
6306 QualType IntTy =
6307 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
6308 llvm::APInt InitValue =
6309 (BOK != BO_LT)
6310 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
6311 : llvm::APInt::getMinValue(Size)
6312 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
6313 : llvm::APInt::getMaxValue(Size);
6314 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
6315 if (Type->isPointerType()) {
6316 // Cast to pointer type.
6317 auto CastExpr = BuildCStyleCastExpr(
6318 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
6319 SourceLocation(), Init);
6320 if (CastExpr.isInvalid())
6321 continue;
6322 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00006323 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006324 } else if (Type->isRealFloatingType()) {
6325 llvm::APFloat InitValue = llvm::APFloat::getLargest(
6326 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
6327 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
6328 Type, ELoc);
6329 }
6330 break;
6331 }
6332 case BO_PtrMemD:
6333 case BO_PtrMemI:
6334 case BO_MulAssign:
6335 case BO_Div:
6336 case BO_Rem:
6337 case BO_Sub:
6338 case BO_Shl:
6339 case BO_Shr:
6340 case BO_LE:
6341 case BO_GE:
6342 case BO_EQ:
6343 case BO_NE:
6344 case BO_AndAssign:
6345 case BO_XorAssign:
6346 case BO_OrAssign:
6347 case BO_Assign:
6348 case BO_AddAssign:
6349 case BO_SubAssign:
6350 case BO_DivAssign:
6351 case BO_RemAssign:
6352 case BO_ShlAssign:
6353 case BO_ShrAssign:
6354 case BO_Comma:
6355 llvm_unreachable("Unexpected reduction operation");
6356 }
6357 if (Init) {
6358 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
6359 /*TypeMayContainAuto=*/false);
6360 } else {
6361 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
6362 }
6363 if (!RHSVD->hasInit()) {
6364 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
6365 << ReductionIdRange;
6366 bool IsDecl =
6367 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6368 Diag(VD->getLocation(),
6369 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6370 << VD;
6371 continue;
6372 }
Alexey Bataev39f915b82015-05-08 10:41:21 +00006373 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
6374 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006375 ExprResult ReductionOp =
6376 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
6377 LHSDRE, RHSDRE);
6378 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00006379 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006380 ReductionOp =
6381 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
6382 BO_Assign, LHSDRE, ReductionOp.get());
6383 } else {
6384 auto *ConditionalOp = new (Context) ConditionalOperator(
6385 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
6386 RHSDRE, Type, VK_LValue, OK_Ordinary);
6387 ReductionOp =
6388 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
6389 BO_Assign, LHSDRE, ConditionalOp);
6390 }
6391 if (ReductionOp.isUsable()) {
6392 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00006393 }
6394 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006395 if (ReductionOp.isInvalid())
6396 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006397
6398 DSAStack->addDSA(VD, DE, OMPC_reduction);
6399 Vars.push_back(DE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006400 LHSs.push_back(LHSDRE);
6401 RHSs.push_back(RHSDRE);
6402 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00006403 }
6404
6405 if (Vars.empty())
6406 return nullptr;
6407
6408 return OMPReductionClause::Create(
6409 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006410 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, LHSs,
6411 RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006412}
6413
Alexey Bataev182227b2015-08-20 10:54:39 +00006414OMPClause *Sema::ActOnOpenMPLinearClause(
6415 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
6416 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
6417 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00006418 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006419 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00006420 SmallVector<Expr *, 8> Inits;
Alexey Bataev182227b2015-08-20 10:54:39 +00006421 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
6422 LinKind == OMPC_LINEAR_unknown) {
6423 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
6424 LinKind = OMPC_LINEAR_val;
6425 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006426 for (auto &RefExpr : VarList) {
6427 assert(RefExpr && "NULL expr in OpenMP linear clause.");
6428 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00006429 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006430 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006431 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00006432 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006433 continue;
6434 }
6435
6436 // OpenMP [2.14.3.7, linear clause]
6437 // A list item that appears in a linear clause is subject to the private
6438 // clause semantics described in Section 2.14.3.3 on page 159 except as
6439 // noted. In addition, the value of the new list item on each iteration
6440 // of the associated loop(s) corresponds to the value of the original
6441 // list item before entering the construct plus the logical number of
6442 // the iteration times linear-step.
6443
Alexey Bataeved09d242014-05-28 05:53:51 +00006444 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00006445 // OpenMP [2.1, C/C++]
6446 // A list item is a variable name.
6447 // OpenMP [2.14.3.3, Restrictions, p.1]
6448 // A variable that is part of another variable (as an array or
6449 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006450 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006451 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006452 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00006453 continue;
6454 }
6455
6456 VarDecl *VD = cast<VarDecl>(DE->getDecl());
6457
6458 // OpenMP [2.14.3.7, linear clause]
6459 // A list-item cannot appear in more than one linear clause.
6460 // A list-item that appears in a linear clause cannot appear in any
6461 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006462 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00006463 if (DVar.RefExpr) {
6464 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6465 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006466 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00006467 continue;
6468 }
6469
6470 QualType QType = VD->getType();
6471 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
6472 // It will be analyzed later.
6473 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006474 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00006475 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006476 continue;
6477 }
6478
6479 // A variable must not have an incomplete type or a reference type.
6480 if (RequireCompleteType(ELoc, QType,
6481 diag::err_omp_linear_incomplete_type)) {
6482 continue;
6483 }
Alexey Bataev1185e192015-08-20 12:15:57 +00006484 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
6485 !QType->isReferenceType()) {
6486 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
6487 << QType << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
6488 continue;
6489 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006490 QType = QType.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00006491
6492 // A list item must not be const-qualified.
6493 if (QType.isConstant(Context)) {
6494 Diag(ELoc, diag::err_omp_const_variable)
6495 << getOpenMPClauseName(OMPC_linear);
6496 bool IsDecl =
6497 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6498 Diag(VD->getLocation(),
6499 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6500 << VD;
6501 continue;
6502 }
6503
6504 // A list item must be of integral or pointer type.
6505 QType = QType.getUnqualifiedType().getCanonicalType();
6506 const Type *Ty = QType.getTypePtrOrNull();
6507 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
6508 !Ty->isPointerType())) {
6509 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
6510 bool IsDecl =
6511 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6512 Diag(VD->getLocation(),
6513 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6514 << VD;
6515 continue;
6516 }
6517
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006518 // Build private copy of original var.
6519 auto *Private = buildVarDecl(*this, ELoc, QType, VD->getName());
6520 auto *PrivateRef = buildDeclRefExpr(
6521 *this, Private, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman3276a272015-03-21 10:12:56 +00006522 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006523 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00006524 Expr *InitExpr;
6525 if (LinKind == OMPC_LINEAR_uval)
6526 InitExpr = VD->getInit();
6527 else
6528 InitExpr = DE;
6529 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexander Musman3276a272015-03-21 10:12:56 +00006530 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006531 auto InitRef = buildDeclRefExpr(
6532 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00006533 DSAStack->addDSA(VD, DE, OMPC_linear);
6534 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006535 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00006536 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00006537 }
6538
6539 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006540 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00006541
6542 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00006543 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00006544 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
6545 !Step->isInstantiationDependent() &&
6546 !Step->containsUnexpandedParameterPack()) {
6547 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006548 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00006549 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006550 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006551 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00006552
Alexander Musman3276a272015-03-21 10:12:56 +00006553 // Build var to save the step value.
6554 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006555 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00006556 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006557 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00006558 ExprResult CalcStep =
6559 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
6560
Alexander Musman8dba6642014-04-22 13:09:42 +00006561 // Warn about zero linear step (it would be probably better specified as
6562 // making corresponding variables 'const').
6563 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00006564 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
6565 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00006566 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
6567 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00006568 if (!IsConstant && CalcStep.isUsable()) {
6569 // Calculate the step beforehand instead of doing this on each iteration.
6570 // (This is not used if the number of iterations may be kfold-ed).
6571 CalcStepExpr = CalcStep.get();
6572 }
Alexander Musman8dba6642014-04-22 13:09:42 +00006573 }
6574
Alexey Bataev182227b2015-08-20 10:54:39 +00006575 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
6576 ColonLoc, EndLoc, Vars, Privates, Inits,
6577 StepExpr, CalcStepExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00006578}
6579
6580static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
6581 Expr *NumIterations, Sema &SemaRef,
6582 Scope *S) {
6583 // Walk the vars and build update/final expressions for the CodeGen.
6584 SmallVector<Expr *, 8> Updates;
6585 SmallVector<Expr *, 8> Finals;
6586 Expr *Step = Clause.getStep();
6587 Expr *CalcStep = Clause.getCalcStep();
6588 // OpenMP [2.14.3.7, linear clause]
6589 // If linear-step is not specified it is assumed to be 1.
6590 if (Step == nullptr)
6591 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
6592 else if (CalcStep)
6593 Step = cast<BinaryOperator>(CalcStep)->getLHS();
6594 bool HasErrors = false;
6595 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006596 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00006597 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00006598 for (auto &RefExpr : Clause.varlists()) {
6599 Expr *InitExpr = *CurInit;
6600
6601 // Build privatized reference to the current linear var.
6602 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00006603 Expr *CapturedRef;
6604 if (LinKind == OMPC_LINEAR_uval)
6605 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
6606 else
6607 CapturedRef =
6608 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
6609 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
6610 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00006611
6612 // Build update: Var = InitExpr + IV * Step
6613 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006614 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00006615 InitExpr, IV, Step, /* Subtract */ false);
6616 Update = SemaRef.ActOnFinishFullExpr(Update.get());
6617
6618 // Build final: Var = InitExpr + NumIterations * Step
6619 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006620 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00006621 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexander Musman3276a272015-03-21 10:12:56 +00006622 Final = SemaRef.ActOnFinishFullExpr(Final.get());
6623 if (!Update.isUsable() || !Final.isUsable()) {
6624 Updates.push_back(nullptr);
6625 Finals.push_back(nullptr);
6626 HasErrors = true;
6627 } else {
6628 Updates.push_back(Update.get());
6629 Finals.push_back(Final.get());
6630 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006631 ++CurInit, ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00006632 }
6633 Clause.setUpdates(Updates);
6634 Clause.setFinals(Finals);
6635 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00006636}
6637
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006638OMPClause *Sema::ActOnOpenMPAlignedClause(
6639 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
6640 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
6641
6642 SmallVector<Expr *, 8> Vars;
6643 for (auto &RefExpr : VarList) {
6644 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
6645 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6646 // It will be analyzed later.
6647 Vars.push_back(RefExpr);
6648 continue;
6649 }
6650
6651 SourceLocation ELoc = RefExpr->getExprLoc();
6652 // OpenMP [2.1, C/C++]
6653 // A list item is a variable name.
6654 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
6655 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6656 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6657 continue;
6658 }
6659
6660 VarDecl *VD = cast<VarDecl>(DE->getDecl());
6661
6662 // OpenMP [2.8.1, simd construct, Restrictions]
6663 // The type of list items appearing in the aligned clause must be
6664 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006665 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006666 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006667 const Type *Ty = QType.getTypePtrOrNull();
6668 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
6669 !Ty->isPointerType())) {
6670 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
6671 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
6672 bool IsDecl =
6673 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6674 Diag(VD->getLocation(),
6675 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6676 << VD;
6677 continue;
6678 }
6679
6680 // OpenMP [2.8.1, simd construct, Restrictions]
6681 // A list-item cannot appear in more than one aligned clause.
6682 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
6683 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
6684 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
6685 << getOpenMPClauseName(OMPC_aligned);
6686 continue;
6687 }
6688
6689 Vars.push_back(DE);
6690 }
6691
6692 // OpenMP [2.8.1, simd construct, Description]
6693 // The parameter of the aligned clause, alignment, must be a constant
6694 // positive integer expression.
6695 // If no optional parameter is specified, implementation-defined default
6696 // alignments for SIMD instructions on the target platforms are assumed.
6697 if (Alignment != nullptr) {
6698 ExprResult AlignResult =
6699 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
6700 if (AlignResult.isInvalid())
6701 return nullptr;
6702 Alignment = AlignResult.get();
6703 }
6704 if (Vars.empty())
6705 return nullptr;
6706
6707 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
6708 EndLoc, Vars, Alignment);
6709}
6710
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006711OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
6712 SourceLocation StartLoc,
6713 SourceLocation LParenLoc,
6714 SourceLocation EndLoc) {
6715 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006716 SmallVector<Expr *, 8> SrcExprs;
6717 SmallVector<Expr *, 8> DstExprs;
6718 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00006719 for (auto &RefExpr : VarList) {
6720 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
6721 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006722 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006723 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006724 SrcExprs.push_back(nullptr);
6725 DstExprs.push_back(nullptr);
6726 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006727 continue;
6728 }
6729
Alexey Bataeved09d242014-05-28 05:53:51 +00006730 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006731 // OpenMP [2.1, C/C++]
6732 // A list item is a variable name.
6733 // OpenMP [2.14.4.1, Restrictions, p.1]
6734 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00006735 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006736 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006737 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006738 continue;
6739 }
6740
6741 Decl *D = DE->getDecl();
6742 VarDecl *VD = cast<VarDecl>(D);
6743
6744 QualType Type = VD->getType();
6745 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6746 // It will be analyzed later.
6747 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006748 SrcExprs.push_back(nullptr);
6749 DstExprs.push_back(nullptr);
6750 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006751 continue;
6752 }
6753
6754 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
6755 // A list item that appears in a copyin clause must be threadprivate.
6756 if (!DSAStack->isThreadPrivate(VD)) {
6757 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00006758 << getOpenMPClauseName(OMPC_copyin)
6759 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006760 continue;
6761 }
6762
6763 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
6764 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00006765 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006766 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006767 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00006768 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006769 ElemType.getUnqualifiedType(), ".copyin.src");
Alexey Bataev39f915b82015-05-08 10:41:21 +00006770 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006771 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
6772 auto *DstVD =
6773 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst");
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006774 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006775 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006776 // For arrays generate assignment operation for single element and replace
6777 // it by the original array element in CodeGen.
6778 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6779 PseudoDstExpr, PseudoSrcExpr);
6780 if (AssignmentOp.isInvalid())
6781 continue;
6782 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6783 /*DiscardedValue=*/true);
6784 if (AssignmentOp.isInvalid())
6785 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006786
6787 DSAStack->addDSA(VD, DE, OMPC_copyin);
6788 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006789 SrcExprs.push_back(PseudoSrcExpr);
6790 DstExprs.push_back(PseudoDstExpr);
6791 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006792 }
6793
Alexey Bataeved09d242014-05-28 05:53:51 +00006794 if (Vars.empty())
6795 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006796
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006797 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
6798 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006799}
6800
Alexey Bataevbae9a792014-06-27 10:37:06 +00006801OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
6802 SourceLocation StartLoc,
6803 SourceLocation LParenLoc,
6804 SourceLocation EndLoc) {
6805 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00006806 SmallVector<Expr *, 8> SrcExprs;
6807 SmallVector<Expr *, 8> DstExprs;
6808 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006809 for (auto &RefExpr : VarList) {
6810 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
6811 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6812 // It will be analyzed later.
6813 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006814 SrcExprs.push_back(nullptr);
6815 DstExprs.push_back(nullptr);
6816 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006817 continue;
6818 }
6819
6820 SourceLocation ELoc = RefExpr->getExprLoc();
6821 // OpenMP [2.1, C/C++]
6822 // A list item is a variable name.
6823 // OpenMP [2.14.4.1, Restrictions, p.1]
6824 // A list item that appears in a copyin clause must be threadprivate.
6825 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
6826 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6827 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6828 continue;
6829 }
6830
6831 Decl *D = DE->getDecl();
6832 VarDecl *VD = cast<VarDecl>(D);
6833
6834 QualType Type = VD->getType();
6835 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6836 // It will be analyzed later.
6837 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006838 SrcExprs.push_back(nullptr);
6839 DstExprs.push_back(nullptr);
6840 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006841 continue;
6842 }
6843
6844 // OpenMP [2.14.4.2, Restrictions, p.2]
6845 // A list item that appears in a copyprivate clause may not appear in a
6846 // private or firstprivate clause on the single construct.
6847 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006848 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006849 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
6850 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00006851 Diag(ELoc, diag::err_omp_wrong_dsa)
6852 << getOpenMPClauseName(DVar.CKind)
6853 << getOpenMPClauseName(OMPC_copyprivate);
6854 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6855 continue;
6856 }
6857
6858 // OpenMP [2.11.4.2, Restrictions, p.1]
6859 // All list items that appear in a copyprivate clause must be either
6860 // threadprivate or private in the enclosing context.
6861 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006862 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006863 if (DVar.CKind == OMPC_shared) {
6864 Diag(ELoc, diag::err_omp_required_access)
6865 << getOpenMPClauseName(OMPC_copyprivate)
6866 << "threadprivate or private in the enclosing context";
6867 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6868 continue;
6869 }
6870 }
6871 }
6872
Alexey Bataev7a3e5852015-05-19 08:19:24 +00006873 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006874 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00006875 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006876 << getOpenMPClauseName(OMPC_copyprivate) << Type
6877 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00006878 bool IsDecl =
6879 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6880 Diag(VD->getLocation(),
6881 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6882 << VD;
6883 continue;
6884 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006885
Alexey Bataevbae9a792014-06-27 10:37:06 +00006886 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
6887 // A variable of class type (or array thereof) that appears in a
6888 // copyin clause requires an accessible, unambiguous copy assignment
6889 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006890 Type = Context.getBaseElementType(Type.getNonReferenceType())
6891 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00006892 auto *SrcVD =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006893 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src");
Alexey Bataev420d45b2015-04-14 05:11:24 +00006894 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006895 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00006896 auto *DstVD =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006897 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst");
Alexey Bataev420d45b2015-04-14 05:11:24 +00006898 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006899 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00006900 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6901 PseudoDstExpr, PseudoSrcExpr);
6902 if (AssignmentOp.isInvalid())
6903 continue;
6904 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6905 /*DiscardedValue=*/true);
6906 if (AssignmentOp.isInvalid())
6907 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006908
6909 // No need to mark vars as copyprivate, they are already threadprivate or
6910 // implicitly private.
6911 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006912 SrcExprs.push_back(PseudoSrcExpr);
6913 DstExprs.push_back(PseudoDstExpr);
6914 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00006915 }
6916
6917 if (Vars.empty())
6918 return nullptr;
6919
Alexey Bataeva63048e2015-03-23 06:18:07 +00006920 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
6921 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006922}
6923
Alexey Bataev6125da92014-07-21 11:26:11 +00006924OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
6925 SourceLocation StartLoc,
6926 SourceLocation LParenLoc,
6927 SourceLocation EndLoc) {
6928 if (VarList.empty())
6929 return nullptr;
6930
6931 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
6932}
Alexey Bataevdea47612014-07-23 07:46:59 +00006933
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006934OMPClause *
6935Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
6936 SourceLocation DepLoc, SourceLocation ColonLoc,
6937 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
6938 SourceLocation LParenLoc, SourceLocation EndLoc) {
6939 if (DepKind == OMPC_DEPEND_unknown) {
6940 std::string Values;
6941 std::string Sep(", ");
6942 for (unsigned i = 0; i < OMPC_DEPEND_unknown; ++i) {
6943 Values += "'";
6944 Values += getOpenMPSimpleClauseTypeName(OMPC_depend, i);
6945 Values += "'";
6946 switch (i) {
6947 case OMPC_DEPEND_unknown - 2:
6948 Values += " or ";
6949 break;
6950 case OMPC_DEPEND_unknown - 1:
6951 break;
6952 default:
6953 Values += Sep;
6954 break;
6955 }
6956 }
6957 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
6958 << Values << getOpenMPClauseName(OMPC_depend);
6959 return nullptr;
6960 }
6961 SmallVector<Expr *, 8> Vars;
6962 for (auto &RefExpr : VarList) {
6963 assert(RefExpr && "NULL expr in OpenMP shared clause.");
6964 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6965 // It will be analyzed later.
6966 Vars.push_back(RefExpr);
6967 continue;
6968 }
6969
6970 SourceLocation ELoc = RefExpr->getExprLoc();
6971 // OpenMP [2.11.1.1, Restrictions, p.3]
6972 // A variable that is part of another variable (such as a field of a
6973 // structure) but is not an array element or an array section cannot appear
6974 // in a depend clause.
6975 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
Alexey Bataev1a3320e2015-08-25 14:24:04 +00006976 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
6977 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
6978 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
6979 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
6980 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006981 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
6982 !ASE->getBase()->getType()->isArrayType())) {
6983 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
6984 << RefExpr->getSourceRange();
6985 continue;
6986 }
6987
6988 Vars.push_back(RefExpr->IgnoreParenImpCasts());
6989 }
6990
6991 if (Vars.empty())
6992 return nullptr;
6993
6994 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
6995 DepLoc, ColonLoc, Vars);
6996}
Michael Wonge710d542015-08-07 16:16:36 +00006997
6998OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
6999 SourceLocation LParenLoc,
7000 SourceLocation EndLoc) {
7001 Expr *ValExpr = Device;
7002 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
7003 !ValExpr->isInstantiationDependent()) {
7004 SourceLocation Loc = ValExpr->getExprLoc();
7005 ExprResult Value = PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
7006 if (Value.isInvalid())
7007 return nullptr;
7008
7009 // OpenMP [2.9.1, Restrictions]
7010 // The device expression must evaluate to a non-negative integer value.
7011 llvm::APSInt Result;
7012 if (Value.get()->isIntegerConstantExpr(Result, Context) &&
7013 Result.isSigned() && !Result.isStrictlyPositive()) {
7014 Diag(Loc, diag::err_omp_negative_expression_in_clause)
7015 << "device" << ValExpr->getSourceRange();
7016 return nullptr;
7017 }
7018 }
7019
7020 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
7021}