blob: ebfb136598ed536bda7418b5324b5f83230ba5a4 [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,
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000455 StringRef Name, const AttrVec *Attrs = nullptr) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000456 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);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000461 if (Attrs) {
462 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
463 I != E; ++I)
464 Decl->addAttr(*I);
465 }
Alexey Bataev39f915b82015-05-08 10:41:21 +0000466 Decl->setImplicit();
467 return Decl;
468}
469
470static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
471 SourceLocation Loc,
472 bool RefersToCapture = false) {
473 D->setReferenced();
474 D->markUsed(S.Context);
475 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
476 SourceLocation(), D, RefersToCapture, Loc, Ty,
477 VK_LValue);
478}
479
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000480DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000481 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000482 DSAVarData DVar;
483
484 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
485 // in a Construct, C/C++, predetermined, p.1]
486 // Variables appearing in threadprivate directives are threadprivate.
Samuel Antaof8b50122015-07-13 22:54:53 +0000487 if ((D->getTLSKind() != VarDecl::TLS_None &&
488 !(D->hasAttr<OMPThreadPrivateDeclAttr>() &&
489 SemaRef.getLangOpts().OpenMPUseTLS &&
490 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +0000491 (D->getStorageClass() == SC_Register && D->hasAttr<AsmLabelAttr>() &&
492 !D->isLocalVarDecl())) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000493 addDSA(D, buildDeclRefExpr(SemaRef, D, D->getType().getNonReferenceType(),
494 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000495 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000496 }
497 if (Stack[0].SharingMap.count(D)) {
498 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
499 DVar.CKind = OMPC_threadprivate;
500 return DVar;
501 }
502
503 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
504 // in a Construct, C/C++, predetermined, p.1]
505 // Variables with automatic storage duration that are declared in a scope
506 // inside the construct are private.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000507 OpenMPDirectiveKind Kind =
508 FromParent ? getParentDirective() : getCurrentDirective();
509 auto StartI = std::next(Stack.rbegin());
510 auto EndI = std::prev(Stack.rend());
511 if (FromParent && StartI != EndI) {
512 StartI = std::next(StartI);
513 }
514 if (!isParallelOrTaskRegion(Kind)) {
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000515 if (isOpenMPLocal(D, StartI) &&
516 ((D->isLocalVarDecl() && (D->getStorageClass() == SC_Auto ||
517 D->getStorageClass() == SC_None)) ||
518 isa<ParmVarDecl>(D))) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000519 DVar.CKind = OMPC_private;
520 return DVar;
Alexander Musman8dba6642014-04-22 13:09:42 +0000521 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000522
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000523 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
524 // in a Construct, C/C++, predetermined, p.4]
525 // Static data members are shared.
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000526 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
527 // in a Construct, C/C++, predetermined, p.7]
528 // Variables with static storage duration that are declared in a scope
529 // inside the construct are shared.
Alexey Bataev42971a32015-01-20 07:03:46 +0000530 if (D->isStaticDataMember() || D->isStaticLocal()) {
531 DSAVarData DVarTemp =
532 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
533 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
534 return DVar;
535
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000536 DVar.CKind = OMPC_shared;
537 return DVar;
538 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000539 }
540
541 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000542 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
543 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000544 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
545 // in a Construct, C/C++, predetermined, p.6]
546 // Variables with const qualified type having no mutable member are
547 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000548 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000549 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000550 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000551 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000552 // Variables with const-qualified type having no mutable member may be
553 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000554 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
555 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000556 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
557 return DVar;
558
Alexey Bataev758e55e2013-09-06 18:03:48 +0000559 DVar.CKind = OMPC_shared;
560 return DVar;
561 }
562
Alexey Bataev758e55e2013-09-06 18:03:48 +0000563 // Explicitly specified attributes and local variables with predetermined
564 // attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000565 auto I = std::prev(StartI);
566 if (I->SharingMap.count(D)) {
567 DVar.RefExpr = I->SharingMap[D].RefExpr;
568 DVar.CKind = I->SharingMap[D].Attributes;
569 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000570 }
571
572 return DVar;
573}
574
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000575DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000576 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000577 auto StartI = Stack.rbegin();
578 auto EndI = std::prev(Stack.rend());
579 if (FromParent && StartI != EndI) {
580 StartI = std::next(StartI);
581 }
582 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000583}
584
Alexey Bataevf29276e2014-06-18 04:14:57 +0000585template <class ClausesPredicate, class DirectivesPredicate>
586DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000587 DirectivesPredicate DPred,
588 bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000589 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000590 auto StartI = std::next(Stack.rbegin());
591 auto EndI = std::prev(Stack.rend());
592 if (FromParent && StartI != EndI) {
593 StartI = std::next(StartI);
594 }
595 for (auto I = StartI, EE = EndI; I != EE; ++I) {
596 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000597 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000598 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000599 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000600 return DVar;
601 }
602 return DSAVarData();
603}
604
Alexey Bataevf29276e2014-06-18 04:14:57 +0000605template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000606DSAStackTy::DSAVarData
607DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
608 DirectivesPredicate DPred, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000609 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000610 auto StartI = std::next(Stack.rbegin());
611 auto EndI = std::prev(Stack.rend());
612 if (FromParent && StartI != EndI) {
613 StartI = std::next(StartI);
614 }
615 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000616 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000617 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000618 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000619 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000620 return DVar;
621 return DSAVarData();
622 }
623 return DSAVarData();
624}
625
Alexey Bataevaac108a2015-06-23 04:51:00 +0000626bool DSAStackTy::hasExplicitDSA(
627 VarDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
628 unsigned Level) {
629 if (CPred(ClauseKindMode))
630 return true;
631 if (isClauseParsingMode())
632 ++Level;
633 D = D->getCanonicalDecl();
634 auto StartI = Stack.rbegin();
635 auto EndI = std::prev(Stack.rend());
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000636 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000637 return false;
638 std::advance(StartI, Level);
639 return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
640 CPred(StartI->SharingMap[D].Attributes);
641}
642
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000643template <class NamedDirectivesPredicate>
644bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
645 auto StartI = std::next(Stack.rbegin());
646 auto EndI = std::prev(Stack.rend());
647 if (FromParent && StartI != EndI) {
648 StartI = std::next(StartI);
649 }
650 for (auto I = StartI, EE = EndI; I != EE; ++I) {
651 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
652 return true;
653 }
654 return false;
655}
656
Alexey Bataev758e55e2013-09-06 18:03:48 +0000657void Sema::InitDataSharingAttributesStack() {
658 VarDataSharingAttributesStack = new DSAStackTy(*this);
659}
660
661#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
662
Alexey Bataevf841bd92014-12-16 07:00:22 +0000663bool Sema::IsOpenMPCapturedVar(VarDecl *VD) {
664 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000665 VD = VD->getCanonicalDecl();
Alexey Bataev48977c32015-08-04 08:10:48 +0000666 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
667 (!DSAStack->isClauseParsingMode() ||
668 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000669 if (DSAStack->isLoopControlVariable(VD) ||
670 (VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000671 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
672 DSAStack->isForceVarCapturing())
Alexey Bataev9c821032015-04-30 04:23:23 +0000673 return true;
Alexey Bataevaac108a2015-06-23 04:51:00 +0000674 auto DVarPrivate = DSAStack->getTopDSA(VD, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000675 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
676 return true;
677 DVarPrivate = DSAStack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(),
Alexey Bataevaac108a2015-06-23 04:51:00 +0000678 DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000679 return DVarPrivate.CKind != OMPC_unknown;
680 }
681 return false;
682}
683
Alexey Bataevaac108a2015-06-23 04:51:00 +0000684bool Sema::isOpenMPPrivateVar(VarDecl *VD, unsigned Level) {
685 assert(LangOpts.OpenMP && "OpenMP is not allowed");
686 return DSAStack->hasExplicitDSA(
687 VD, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
688}
689
Alexey Bataeved09d242014-05-28 05:53:51 +0000690void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000691
692void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
693 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000694 Scope *CurScope, SourceLocation Loc) {
695 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000696 PushExpressionEvaluationContext(PotentiallyEvaluated);
697}
698
Alexey Bataevaac108a2015-06-23 04:51:00 +0000699void Sema::StartOpenMPClause(OpenMPClauseKind K) {
700 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000701}
702
Alexey Bataevaac108a2015-06-23 04:51:00 +0000703void Sema::EndOpenMPClause() {
704 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000705}
706
Alexey Bataev758e55e2013-09-06 18:03:48 +0000707void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000708 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
709 // A variable of class type (or array thereof) that appears in a lastprivate
710 // clause requires an accessible, unambiguous default constructor for the
711 // class type, unless the list item is also specified in a firstprivate
712 // clause.
713 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000714 for (auto *C : D->clauses()) {
715 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
716 SmallVector<Expr *, 8> PrivateCopies;
717 for (auto *DE : Clause->varlists()) {
718 if (DE->isValueDependent() || DE->isTypeDependent()) {
719 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000720 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +0000721 }
722 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(DE)->getDecl());
Alexey Bataevbd9fec12015-08-18 06:47:21 +0000723 QualType Type = VD->getType().getNonReferenceType();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000724 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000725 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000726 // Generate helper private variable and initialize it with the
727 // default value. The address of the original variable is replaced
728 // by the address of the new private variable in CodeGen. This new
729 // variable is not added to IdResolver, so the code in the OpenMP
730 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000731 auto *VDPrivate = buildVarDecl(
732 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
733 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +0000734 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
735 if (VDPrivate->isInvalidDecl())
736 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000737 PrivateCopies.push_back(buildDeclRefExpr(
738 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +0000739 } else {
740 // The variable is also a firstprivate, so initialization sequence
741 // for private copy is generated already.
742 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000743 }
744 }
Alexey Bataev38e89532015-04-16 04:54:05 +0000745 // Set initializers to private copies if no errors were found.
746 if (PrivateCopies.size() == Clause->varlist_size()) {
747 Clause->setPrivateCopies(PrivateCopies);
748 }
Alexey Bataevf29276e2014-06-18 04:14:57 +0000749 }
750 }
751 }
752
Alexey Bataev758e55e2013-09-06 18:03:48 +0000753 DSAStack->pop();
754 DiscardCleanupsInEvaluationContext();
755 PopExpressionEvaluationContext();
756}
757
Alexander Musman3276a272015-03-21 10:12:56 +0000758static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
759 Expr *NumIterations, Sema &SemaRef,
760 Scope *S);
761
Alexey Bataeva769e072013-03-22 06:34:35 +0000762namespace {
763
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000764class VarDeclFilterCCC : public CorrectionCandidateCallback {
765private:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000766 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +0000767
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000768public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000769 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000770 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000771 NamedDecl *ND = Candidate.getCorrectionDecl();
772 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
773 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000774 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
775 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000776 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000777 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000778 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000779};
Alexey Bataeved09d242014-05-28 05:53:51 +0000780} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000781
782ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
783 CXXScopeSpec &ScopeSpec,
784 const DeclarationNameInfo &Id) {
785 LookupResult Lookup(*this, Id, LookupOrdinaryName);
786 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
787
788 if (Lookup.isAmbiguous())
789 return ExprError();
790
791 VarDecl *VD;
792 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000793 if (TypoCorrection Corrected = CorrectTypo(
794 Id, LookupOrdinaryName, CurScope, nullptr,
795 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000796 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000797 PDiag(Lookup.empty()
798 ? diag::err_undeclared_var_use_suggest
799 : diag::err_omp_expected_var_arg_suggest)
800 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000801 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000802 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000803 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
804 : diag::err_omp_expected_var_arg)
805 << Id.getName();
806 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000807 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000808 } else {
809 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000810 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000811 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
812 return ExprError();
813 }
814 }
815 Lookup.suppressDiagnostics();
816
817 // OpenMP [2.9.2, Syntax, C/C++]
818 // Variables must be file-scope, namespace-scope, or static block-scope.
819 if (!VD->hasGlobalStorage()) {
820 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000821 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
822 bool IsDecl =
823 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000824 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000825 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
826 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000827 return ExprError();
828 }
829
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000830 VarDecl *CanonicalVD = VD->getCanonicalDecl();
831 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000832 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
833 // A threadprivate directive for file-scope variables must appear outside
834 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000835 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
836 !getCurLexicalContext()->isTranslationUnit()) {
837 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000838 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
839 bool IsDecl =
840 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
841 Diag(VD->getLocation(),
842 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
843 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000844 return ExprError();
845 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000846 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
847 // A threadprivate directive for static class member variables must appear
848 // in the class definition, in the same scope in which the member
849 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000850 if (CanonicalVD->isStaticDataMember() &&
851 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
852 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000853 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
854 bool IsDecl =
855 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
856 Diag(VD->getLocation(),
857 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
858 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000859 return ExprError();
860 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000861 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
862 // A threadprivate directive for namespace-scope variables must appear
863 // outside any definition or declaration other than the namespace
864 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000865 if (CanonicalVD->getDeclContext()->isNamespace() &&
866 (!getCurLexicalContext()->isFileContext() ||
867 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
868 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000869 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
870 bool IsDecl =
871 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
872 Diag(VD->getLocation(),
873 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
874 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000875 return ExprError();
876 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000877 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
878 // A threadprivate directive for static block-scope variables must appear
879 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000880 if (CanonicalVD->isStaticLocal() && CurScope &&
881 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000882 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000883 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
884 bool IsDecl =
885 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
886 Diag(VD->getLocation(),
887 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
888 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000889 return ExprError();
890 }
891
892 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
893 // A threadprivate directive must lexically precede all references to any
894 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000895 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000896 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +0000897 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000898 return ExprError();
899 }
900
901 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +0000902 ExprResult DE = buildDeclRefExpr(*this, VD, ExprType, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000903 return DE;
904}
905
Alexey Bataeved09d242014-05-28 05:53:51 +0000906Sema::DeclGroupPtrTy
907Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
908 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000909 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000910 CurContext->addDecl(D);
911 return DeclGroupPtrTy::make(DeclGroupRef(D));
912 }
913 return DeclGroupPtrTy();
914}
915
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000916namespace {
917class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
918 Sema &SemaRef;
919
920public:
921 bool VisitDeclRefExpr(const DeclRefExpr *E) {
922 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
923 if (VD->hasLocalStorage()) {
924 SemaRef.Diag(E->getLocStart(),
925 diag::err_omp_local_var_in_threadprivate_init)
926 << E->getSourceRange();
927 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
928 << VD << VD->getSourceRange();
929 return true;
930 }
931 }
932 return false;
933 }
934 bool VisitStmt(const Stmt *S) {
935 for (auto Child : S->children()) {
936 if (Child && Visit(Child))
937 return true;
938 }
939 return false;
940 }
Alexey Bataev23b69422014-06-18 07:08:49 +0000941 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000942};
943} // namespace
944
Alexey Bataeved09d242014-05-28 05:53:51 +0000945OMPThreadPrivateDecl *
946Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000947 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +0000948 for (auto &RefExpr : VarList) {
949 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000950 VarDecl *VD = cast<VarDecl>(DE->getDecl());
951 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000952
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000953 QualType QType = VD->getType();
954 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
955 // It will be analyzed later.
956 Vars.push_back(DE);
957 continue;
958 }
959
Alexey Bataeva769e072013-03-22 06:34:35 +0000960 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
961 // A threadprivate variable must not have an incomplete type.
962 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000963 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000964 continue;
965 }
966
967 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
968 // A threadprivate variable must not have a reference type.
969 if (VD->getType()->isReferenceType()) {
970 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000971 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
972 bool IsDecl =
973 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
974 Diag(VD->getLocation(),
975 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
976 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000977 continue;
978 }
979
Samuel Antaof8b50122015-07-13 22:54:53 +0000980 // Check if this is a TLS variable. If TLS is not being supported, produce
981 // the corresponding diagnostic.
982 if ((VD->getTLSKind() != VarDecl::TLS_None &&
983 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
984 getLangOpts().OpenMPUseTLS &&
985 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +0000986 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
987 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +0000988 Diag(ILoc, diag::err_omp_var_thread_local)
989 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +0000990 bool IsDecl =
991 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
992 Diag(VD->getLocation(),
993 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
994 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000995 continue;
996 }
997
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000998 // Check if initial value of threadprivate variable reference variable with
999 // local storage (it is not supported by runtime).
1000 if (auto Init = VD->getAnyInitializer()) {
1001 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001002 if (Checker.Visit(Init))
1003 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001004 }
1005
Alexey Bataeved09d242014-05-28 05:53:51 +00001006 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001007 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001008 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1009 Context, SourceRange(Loc, Loc)));
1010 if (auto *ML = Context.getASTMutationListener())
1011 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001012 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001013 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001014 if (!Vars.empty()) {
1015 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1016 Vars);
1017 D->setAccess(AS_public);
1018 }
1019 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001020}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001021
Alexey Bataev7ff55242014-06-19 09:13:45 +00001022static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
1023 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
1024 bool IsLoopIterVar = false) {
1025 if (DVar.RefExpr) {
1026 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1027 << getOpenMPClauseName(DVar.CKind);
1028 return;
1029 }
1030 enum {
1031 PDSA_StaticMemberShared,
1032 PDSA_StaticLocalVarShared,
1033 PDSA_LoopIterVarPrivate,
1034 PDSA_LoopIterVarLinear,
1035 PDSA_LoopIterVarLastprivate,
1036 PDSA_ConstVarShared,
1037 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001038 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001039 PDSA_LocalVarPrivate,
1040 PDSA_Implicit
1041 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001042 bool ReportHint = false;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001043 auto ReportLoc = VD->getLocation();
Alexey Bataev7ff55242014-06-19 09:13:45 +00001044 if (IsLoopIterVar) {
1045 if (DVar.CKind == OMPC_private)
1046 Reason = PDSA_LoopIterVarPrivate;
1047 else if (DVar.CKind == OMPC_lastprivate)
1048 Reason = PDSA_LoopIterVarLastprivate;
1049 else
1050 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001051 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1052 Reason = PDSA_TaskVarFirstprivate;
1053 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001054 } else if (VD->isStaticLocal())
1055 Reason = PDSA_StaticLocalVarShared;
1056 else if (VD->isStaticDataMember())
1057 Reason = PDSA_StaticMemberShared;
1058 else if (VD->isFileVarDecl())
1059 Reason = PDSA_GlobalVarShared;
1060 else if (VD->getType().isConstant(SemaRef.getASTContext()))
1061 Reason = PDSA_ConstVarShared;
Alexey Bataevbae9a792014-06-27 10:37:06 +00001062 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001063 ReportHint = true;
1064 Reason = PDSA_LocalVarPrivate;
1065 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001066 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001067 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001068 << Reason << ReportHint
1069 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1070 } else if (DVar.ImplicitDSALoc.isValid()) {
1071 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1072 << getOpenMPClauseName(DVar.CKind);
1073 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001074}
1075
Alexey Bataev758e55e2013-09-06 18:03:48 +00001076namespace {
1077class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1078 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001079 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001080 bool ErrorFound;
1081 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001082 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001083 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001084
Alexey Bataev758e55e2013-09-06 18:03:48 +00001085public:
1086 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001087 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001088 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001089 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1090 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001091
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001092 auto DVar = Stack->getTopDSA(VD, false);
1093 // Check if the variable has explicit DSA set and stop analysis if it so.
1094 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001095
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001096 auto ELoc = E->getExprLoc();
1097 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001098 // The default(none) clause requires that each variable that is referenced
1099 // in the construct, and does not have a predetermined data-sharing
1100 // attribute, must have its data-sharing attribute explicitly determined
1101 // by being listed in a data-sharing attribute clause.
1102 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001103 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001104 VarsWithInheritedDSA.count(VD) == 0) {
1105 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001106 return;
1107 }
1108
1109 // OpenMP [2.9.3.6, Restrictions, p.2]
1110 // A list item that appears in a reduction clause of the innermost
1111 // enclosing worksharing or parallel construct may not be accessed in an
1112 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001113 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001114 [](OpenMPDirectiveKind K) -> bool {
1115 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001116 isOpenMPWorksharingDirective(K) ||
1117 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001118 },
1119 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001120 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1121 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001122 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1123 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001124 return;
1125 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001126
1127 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001128 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001129 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001130 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001131 }
1132 }
1133 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001134 for (auto *C : S->clauses()) {
1135 // Skip analysis of arguments of implicitly defined firstprivate clause
1136 // for task directives.
1137 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1138 for (auto *CC : C->children()) {
1139 if (CC)
1140 Visit(CC);
1141 }
1142 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001143 }
1144 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001145 for (auto *C : S->children()) {
1146 if (C && !isa<OMPExecutableDirective>(C))
1147 Visit(C);
1148 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001149 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001150
1151 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001152 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001153 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
1154 return VarsWithInheritedDSA;
1155 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001156
Alexey Bataev7ff55242014-06-19 09:13:45 +00001157 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1158 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001159};
Alexey Bataeved09d242014-05-28 05:53:51 +00001160} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001161
Alexey Bataevbae9a792014-06-27 10:37:06 +00001162void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001163 switch (DKind) {
1164 case OMPD_parallel: {
1165 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001166 QualType KmpInt32PtrTy =
1167 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001168 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001169 std::make_pair(".global_tid.", KmpInt32PtrTy),
1170 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1171 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001172 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001173 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1174 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001175 break;
1176 }
1177 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001178 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001179 std::make_pair(StringRef(), QualType()) // __context with shared vars
1180 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001181 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1182 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001183 break;
1184 }
1185 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001186 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001187 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001188 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001189 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1190 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001191 break;
1192 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001193 case OMPD_for_simd: {
1194 Sema::CapturedParamNameType Params[] = {
1195 std::make_pair(StringRef(), QualType()) // __context with shared vars
1196 };
1197 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1198 Params);
1199 break;
1200 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001201 case OMPD_sections: {
1202 Sema::CapturedParamNameType Params[] = {
1203 std::make_pair(StringRef(), QualType()) // __context with shared vars
1204 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001205 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1206 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001207 break;
1208 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001209 case OMPD_section: {
1210 Sema::CapturedParamNameType Params[] = {
1211 std::make_pair(StringRef(), QualType()) // __context with shared vars
1212 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001213 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1214 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001215 break;
1216 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001217 case OMPD_single: {
1218 Sema::CapturedParamNameType Params[] = {
1219 std::make_pair(StringRef(), QualType()) // __context with shared vars
1220 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001221 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1222 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001223 break;
1224 }
Alexander Musman80c22892014-07-17 08:54:58 +00001225 case OMPD_master: {
1226 Sema::CapturedParamNameType Params[] = {
1227 std::make_pair(StringRef(), QualType()) // __context with shared vars
1228 };
1229 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1230 Params);
1231 break;
1232 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001233 case OMPD_critical: {
1234 Sema::CapturedParamNameType Params[] = {
1235 std::make_pair(StringRef(), QualType()) // __context with shared vars
1236 };
1237 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1238 Params);
1239 break;
1240 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001241 case OMPD_parallel_for: {
1242 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001243 QualType KmpInt32PtrTy =
1244 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001245 Sema::CapturedParamNameType Params[] = {
1246 std::make_pair(".global_tid.", KmpInt32PtrTy),
1247 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1248 std::make_pair(StringRef(), QualType()) // __context with shared vars
1249 };
1250 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1251 Params);
1252 break;
1253 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001254 case OMPD_parallel_for_simd: {
1255 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001256 QualType KmpInt32PtrTy =
1257 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001258 Sema::CapturedParamNameType Params[] = {
1259 std::make_pair(".global_tid.", KmpInt32PtrTy),
1260 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1261 std::make_pair(StringRef(), QualType()) // __context with shared vars
1262 };
1263 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1264 Params);
1265 break;
1266 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001267 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001268 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001269 QualType KmpInt32PtrTy =
1270 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001271 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001272 std::make_pair(".global_tid.", KmpInt32PtrTy),
1273 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001274 std::make_pair(StringRef(), QualType()) // __context with shared vars
1275 };
1276 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1277 Params);
1278 break;
1279 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001280 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001281 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001282 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1283 FunctionProtoType::ExtProtoInfo EPI;
1284 EPI.Variadic = true;
1285 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001286 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001287 std::make_pair(".global_tid.", KmpInt32Ty),
1288 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001289 std::make_pair(".privates.",
1290 Context.VoidPtrTy.withConst().withRestrict()),
1291 std::make_pair(
1292 ".copy_fn.",
1293 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001294 std::make_pair(StringRef(), QualType()) // __context with shared vars
1295 };
1296 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1297 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001298 // Mark this captured region as inlined, because we don't use outlined
1299 // function directly.
1300 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1301 AlwaysInlineAttr::CreateImplicit(
1302 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001303 break;
1304 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001305 case OMPD_ordered: {
1306 Sema::CapturedParamNameType Params[] = {
1307 std::make_pair(StringRef(), QualType()) // __context with shared vars
1308 };
1309 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1310 Params);
1311 break;
1312 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001313 case OMPD_atomic: {
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 }
Michael Wong65f367f2015-07-21 13:44:28 +00001321 case OMPD_target_data:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001322 case OMPD_target: {
1323 Sema::CapturedParamNameType Params[] = {
1324 std::make_pair(StringRef(), QualType()) // __context with shared vars
1325 };
1326 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1327 Params);
1328 break;
1329 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001330 case OMPD_teams: {
1331 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001332 QualType KmpInt32PtrTy =
1333 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001334 Sema::CapturedParamNameType Params[] = {
1335 std::make_pair(".global_tid.", KmpInt32PtrTy),
1336 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1337 std::make_pair(StringRef(), QualType()) // __context with shared vars
1338 };
1339 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1340 Params);
1341 break;
1342 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001343 case OMPD_taskgroup: {
1344 Sema::CapturedParamNameType Params[] = {
1345 std::make_pair(StringRef(), QualType()) // __context with shared vars
1346 };
1347 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1348 Params);
1349 break;
1350 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001351 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001352 case OMPD_taskyield:
1353 case OMPD_barrier:
1354 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001355 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001356 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001357 case OMPD_flush:
Alexey Bataev9959db52014-05-06 10:08:46 +00001358 llvm_unreachable("OpenMP Directive is not allowed");
1359 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001360 llvm_unreachable("Unknown OpenMP directive");
1361 }
1362}
1363
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001364StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1365 ArrayRef<OMPClause *> Clauses) {
1366 if (!S.isUsable()) {
1367 ActOnCapturedRegionError();
1368 return StmtError();
1369 }
Alexey Bataev040d5402015-05-12 08:35:28 +00001370 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001371 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001372 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001373 Clause->getClauseKind() == OMPC_copyprivate ||
1374 (getLangOpts().OpenMPUseTLS &&
1375 getASTContext().getTargetInfo().isTLSSupported() &&
1376 Clause->getClauseKind() == OMPC_copyin)) {
1377 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001378 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001379 for (auto *VarRef : Clause->children()) {
1380 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001381 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001382 }
1383 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001384 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev040d5402015-05-12 08:35:28 +00001385 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
1386 Clause->getClauseKind() == OMPC_schedule) {
1387 // Mark all variables in private list clauses as used in inner region.
1388 // Required for proper codegen of combined directives.
1389 // TODO: add processing for other clauses.
1390 if (auto *E = cast_or_null<Expr>(
1391 cast<OMPScheduleClause>(Clause)->getHelperChunkSize())) {
1392 MarkDeclarationsReferencedInExpr(E);
1393 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001394 }
1395 }
1396 return ActOnCapturedRegionEnd(S.get());
1397}
1398
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001399static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1400 OpenMPDirectiveKind CurrentRegion,
1401 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001402 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001403 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001404 // Allowed nesting of constructs
1405 // +------------------+-----------------+------------------------------------+
1406 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1407 // +------------------+-----------------+------------------------------------+
1408 // | parallel | parallel | * |
1409 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001410 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001411 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001412 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001413 // | parallel | simd | * |
1414 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001415 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001416 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001417 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001418 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001419 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001420 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001421 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001422 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001423 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001424 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001425 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001426 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001427 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001428 // | parallel | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001429 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001430 // | parallel | cancellation | |
1431 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001432 // | parallel | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001433 // +------------------+-----------------+------------------------------------+
1434 // | for | parallel | * |
1435 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001436 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001437 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001438 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001439 // | for | simd | * |
1440 // | for | sections | + |
1441 // | for | section | + |
1442 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001443 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001444 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001445 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001446 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001447 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001448 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001449 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001450 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001451 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001452 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001453 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001454 // | for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001455 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001456 // | for | cancellation | |
1457 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001458 // | for | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001459 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001460 // | master | parallel | * |
1461 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001462 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001463 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001464 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001465 // | master | simd | * |
1466 // | master | sections | + |
1467 // | master | section | + |
1468 // | master | single | + |
1469 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001470 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001471 // | master |parallel sections| * |
1472 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001473 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001474 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001475 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001476 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001477 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001478 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001479 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001480 // | master | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001481 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001482 // | master | cancellation | |
1483 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001484 // | master | cancel | |
Alexander Musman80c22892014-07-17 08:54:58 +00001485 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001486 // | critical | parallel | * |
1487 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001488 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001489 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001490 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001491 // | critical | simd | * |
1492 // | critical | sections | + |
1493 // | critical | section | + |
1494 // | critical | single | + |
1495 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001496 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001497 // | critical |parallel sections| * |
1498 // | critical | task | * |
1499 // | critical | taskyield | * |
1500 // | critical | barrier | + |
1501 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001502 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001503 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001504 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001505 // | critical | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001506 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001507 // | critical | cancellation | |
1508 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001509 // | critical | cancel | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001510 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001511 // | simd | parallel | |
1512 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001513 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001514 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001515 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001516 // | simd | simd | |
1517 // | simd | sections | |
1518 // | simd | section | |
1519 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001520 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001521 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001522 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001523 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001524 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001525 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001526 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001527 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001528 // | simd | flush | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001529 // | simd | ordered | |
Alexey Bataev0162e452014-07-22 10:10:35 +00001530 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001531 // | simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001532 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001533 // | simd | cancellation | |
1534 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001535 // | simd | cancel | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001536 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001537 // | for simd | parallel | |
1538 // | for simd | for | |
1539 // | for simd | for simd | |
1540 // | for simd | master | |
1541 // | for simd | critical | |
1542 // | for simd | simd | |
1543 // | for simd | sections | |
1544 // | for simd | section | |
1545 // | for simd | single | |
1546 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001547 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001548 // | for simd |parallel sections| |
1549 // | for simd | task | |
1550 // | for simd | taskyield | |
1551 // | for simd | barrier | |
1552 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001553 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001554 // | for simd | flush | |
1555 // | for simd | ordered | |
1556 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001557 // | for simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001558 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001559 // | for simd | cancellation | |
1560 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001561 // | for simd | cancel | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001562 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001563 // | parallel for simd| parallel | |
1564 // | parallel for simd| for | |
1565 // | parallel for simd| for simd | |
1566 // | parallel for simd| master | |
1567 // | parallel for simd| critical | |
1568 // | parallel for simd| simd | |
1569 // | parallel for simd| sections | |
1570 // | parallel for simd| section | |
1571 // | parallel for simd| single | |
1572 // | parallel for simd| parallel for | |
1573 // | parallel for simd|parallel for simd| |
1574 // | parallel for simd|parallel sections| |
1575 // | parallel for simd| task | |
1576 // | parallel for simd| taskyield | |
1577 // | parallel for simd| barrier | |
1578 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001579 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001580 // | parallel for simd| flush | |
1581 // | parallel for simd| ordered | |
1582 // | parallel for simd| atomic | |
1583 // | parallel for simd| target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001584 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001585 // | parallel for simd| cancellation | |
1586 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001587 // | parallel for simd| cancel | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001588 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001589 // | sections | parallel | * |
1590 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001591 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001592 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001593 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001594 // | sections | simd | * |
1595 // | sections | sections | + |
1596 // | sections | section | * |
1597 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001598 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001599 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001600 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001601 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001602 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001603 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001604 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001605 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001606 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001607 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001608 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001609 // | sections | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001610 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001611 // | sections | cancellation | |
1612 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001613 // | sections | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001614 // +------------------+-----------------+------------------------------------+
1615 // | section | parallel | * |
1616 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001617 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001618 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001619 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001620 // | section | simd | * |
1621 // | section | sections | + |
1622 // | section | section | + |
1623 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001624 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001625 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001626 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001627 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001628 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001629 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001630 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001631 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001632 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001633 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001634 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001635 // | section | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001636 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001637 // | section | cancellation | |
1638 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001639 // | section | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001640 // +------------------+-----------------+------------------------------------+
1641 // | single | parallel | * |
1642 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001643 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001644 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001645 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001646 // | single | simd | * |
1647 // | single | sections | + |
1648 // | single | section | + |
1649 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001650 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001651 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001652 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001653 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001654 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001655 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001656 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001657 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001658 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001659 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001660 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001661 // | single | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001662 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001663 // | single | cancellation | |
1664 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001665 // | single | cancel | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001666 // +------------------+-----------------+------------------------------------+
1667 // | parallel for | parallel | * |
1668 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001669 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001670 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001671 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001672 // | parallel for | simd | * |
1673 // | parallel for | sections | + |
1674 // | parallel for | section | + |
1675 // | parallel for | single | + |
1676 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001677 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001678 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001679 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001680 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001681 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001682 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001683 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001684 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001685 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001686 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001687 // | parallel for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001688 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001689 // | parallel for | cancellation | |
1690 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001691 // | parallel for | cancel | ! |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001692 // +------------------+-----------------+------------------------------------+
1693 // | parallel sections| parallel | * |
1694 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001695 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001696 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001697 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001698 // | parallel sections| simd | * |
1699 // | parallel sections| sections | + |
1700 // | parallel sections| section | * |
1701 // | parallel sections| single | + |
1702 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001703 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001704 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001705 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001706 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001707 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001708 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001709 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001710 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001711 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001712 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001713 // | parallel sections| target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001714 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001715 // | parallel sections| cancellation | |
1716 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001717 // | parallel sections| cancel | ! |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001718 // +------------------+-----------------+------------------------------------+
1719 // | task | parallel | * |
1720 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001721 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001722 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001723 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001724 // | task | simd | * |
1725 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001726 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001727 // | task | single | + |
1728 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001729 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001730 // | task |parallel sections| * |
1731 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001732 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001733 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001734 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001735 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001736 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001737 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001738 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001739 // | task | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001740 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001741 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00001742 // | | point | ! |
1743 // | task | cancel | ! |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001744 // +------------------+-----------------+------------------------------------+
1745 // | ordered | parallel | * |
1746 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001747 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001748 // | ordered | master | * |
1749 // | ordered | critical | * |
1750 // | ordered | simd | * |
1751 // | ordered | sections | + |
1752 // | ordered | section | + |
1753 // | ordered | single | + |
1754 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001755 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001756 // | ordered |parallel sections| * |
1757 // | ordered | task | * |
1758 // | ordered | taskyield | * |
1759 // | ordered | barrier | + |
1760 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001761 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001762 // | ordered | flush | * |
1763 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001764 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001765 // | ordered | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001766 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001767 // | ordered | cancellation | |
1768 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001769 // | ordered | cancel | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001770 // +------------------+-----------------+------------------------------------+
1771 // | atomic | parallel | |
1772 // | atomic | for | |
1773 // | atomic | for simd | |
1774 // | atomic | master | |
1775 // | atomic | critical | |
1776 // | atomic | simd | |
1777 // | atomic | sections | |
1778 // | atomic | section | |
1779 // | atomic | single | |
1780 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001781 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001782 // | atomic |parallel sections| |
1783 // | atomic | task | |
1784 // | atomic | taskyield | |
1785 // | atomic | barrier | |
1786 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001787 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001788 // | atomic | flush | |
1789 // | atomic | ordered | |
1790 // | atomic | atomic | |
1791 // | atomic | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001792 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001793 // | atomic | cancellation | |
1794 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001795 // | atomic | cancel | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001796 // +------------------+-----------------+------------------------------------+
1797 // | target | parallel | * |
1798 // | target | for | * |
1799 // | target | for simd | * |
1800 // | target | master | * |
1801 // | target | critical | * |
1802 // | target | simd | * |
1803 // | target | sections | * |
1804 // | target | section | * |
1805 // | target | single | * |
1806 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001807 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001808 // | target |parallel sections| * |
1809 // | target | task | * |
1810 // | target | taskyield | * |
1811 // | target | barrier | * |
1812 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001813 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001814 // | target | flush | * |
1815 // | target | ordered | * |
1816 // | target | atomic | * |
1817 // | target | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001818 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001819 // | target | cancellation | |
1820 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001821 // | target | cancel | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001822 // +------------------+-----------------+------------------------------------+
1823 // | teams | parallel | * |
1824 // | teams | for | + |
1825 // | teams | for simd | + |
1826 // | teams | master | + |
1827 // | teams | critical | + |
1828 // | teams | simd | + |
1829 // | teams | sections | + |
1830 // | teams | section | + |
1831 // | teams | single | + |
1832 // | teams | parallel for | * |
1833 // | teams |parallel for simd| * |
1834 // | teams |parallel sections| * |
1835 // | teams | task | + |
1836 // | teams | taskyield | + |
1837 // | teams | barrier | + |
1838 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00001839 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001840 // | teams | flush | + |
1841 // | teams | ordered | + |
1842 // | teams | atomic | + |
1843 // | teams | target | + |
1844 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001845 // | teams | cancellation | |
1846 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001847 // | teams | cancel | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001848 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00001849 if (Stack->getCurScope()) {
1850 auto ParentRegion = Stack->getParentDirective();
1851 bool NestingProhibited = false;
1852 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001853 enum {
1854 NoRecommend,
1855 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00001856 ShouldBeInOrderedRegion,
1857 ShouldBeInTargetRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001858 } Recommend = NoRecommend;
Alexey Bataev549210e2014-06-24 04:39:47 +00001859 if (isOpenMPSimdDirective(ParentRegion)) {
1860 // OpenMP [2.16, Nesting of Regions]
1861 // OpenMP constructs may not be nested inside a simd region.
1862 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1863 return true;
1864 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001865 if (ParentRegion == OMPD_atomic) {
1866 // OpenMP [2.16, Nesting of Regions]
1867 // OpenMP constructs may not be nested inside an atomic region.
1868 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
1869 return true;
1870 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001871 if (CurrentRegion == OMPD_section) {
1872 // OpenMP [2.7.2, sections Construct, Restrictions]
1873 // Orphaned section directives are prohibited. That is, the section
1874 // directives must appear within the sections construct and must not be
1875 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001876 if (ParentRegion != OMPD_sections &&
1877 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001878 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1879 << (ParentRegion != OMPD_unknown)
1880 << getOpenMPDirectiveName(ParentRegion);
1881 return true;
1882 }
1883 return false;
1884 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001885 // Allow some constructs to be orphaned (they could be used in functions,
1886 // called from OpenMP regions with the required preconditions).
1887 if (ParentRegion == OMPD_unknown)
1888 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00001889 if (CurrentRegion == OMPD_cancellation_point ||
1890 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001891 // OpenMP [2.16, Nesting of Regions]
1892 // A cancellation point construct for which construct-type-clause is
1893 // taskgroup must be nested inside a task construct. A cancellation
1894 // point construct for which construct-type-clause is not taskgroup must
1895 // be closely nested inside an OpenMP construct that matches the type
1896 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00001897 // A cancel construct for which construct-type-clause is taskgroup must be
1898 // nested inside a task construct. A cancel construct for which
1899 // construct-type-clause is not taskgroup must be closely nested inside an
1900 // OpenMP construct that matches the type specified in
1901 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001902 NestingProhibited =
1903 !((CancelRegion == OMPD_parallel && ParentRegion == OMPD_parallel) ||
1904 (CancelRegion == OMPD_for && ParentRegion == OMPD_for) ||
1905 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
1906 (CancelRegion == OMPD_sections &&
1907 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections)));
1908 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00001909 // OpenMP [2.16, Nesting of Regions]
1910 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001911 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00001912 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
1913 ParentRegion == OMPD_task;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001914 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
1915 // OpenMP [2.16, Nesting of Regions]
1916 // A critical region may not be nested (closely or otherwise) inside a
1917 // critical region with the same name. Note that this restriction is not
1918 // sufficient to prevent deadlock.
1919 SourceLocation PreviousCriticalLoc;
1920 bool DeadLock =
1921 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
1922 OpenMPDirectiveKind K,
1923 const DeclarationNameInfo &DNI,
1924 SourceLocation Loc)
1925 ->bool {
1926 if (K == OMPD_critical &&
1927 DNI.getName() == CurrentName.getName()) {
1928 PreviousCriticalLoc = Loc;
1929 return true;
1930 } else
1931 return false;
1932 },
1933 false /* skip top directive */);
1934 if (DeadLock) {
1935 SemaRef.Diag(StartLoc,
1936 diag::err_omp_prohibited_region_critical_same_name)
1937 << CurrentName.getName();
1938 if (PreviousCriticalLoc.isValid())
1939 SemaRef.Diag(PreviousCriticalLoc,
1940 diag::note_omp_previous_critical_region);
1941 return true;
1942 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001943 } else if (CurrentRegion == OMPD_barrier) {
1944 // OpenMP [2.16, Nesting of Regions]
1945 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001946 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001947 NestingProhibited =
1948 isOpenMPWorksharingDirective(ParentRegion) ||
1949 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1950 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00001951 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00001952 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00001953 // OpenMP [2.16, Nesting of Regions]
1954 // A worksharing region may not be closely nested inside a worksharing,
1955 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001956 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00001957 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001958 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1959 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
1960 Recommend = ShouldBeInParallelRegion;
1961 } else if (CurrentRegion == OMPD_ordered) {
1962 // OpenMP [2.16, Nesting of Regions]
1963 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00001964 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001965 // An ordered region must be closely nested inside a loop region (or
1966 // parallel loop region) with an ordered clause.
1967 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00001968 ParentRegion == OMPD_task ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001969 !Stack->isParentOrderedRegion();
1970 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00001971 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
1972 // OpenMP [2.16, Nesting of Regions]
1973 // If specified, a teams construct must be contained within a target
1974 // construct.
1975 NestingProhibited = ParentRegion != OMPD_target;
1976 Recommend = ShouldBeInTargetRegion;
1977 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
1978 }
1979 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
1980 // OpenMP [2.16, Nesting of Regions]
1981 // distribute, parallel, parallel sections, parallel workshare, and the
1982 // parallel loop and parallel loop SIMD constructs are the only OpenMP
1983 // constructs that can be closely nested in the teams region.
1984 // TODO: add distribute directive.
1985 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion);
1986 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00001987 }
1988 if (NestingProhibited) {
1989 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001990 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
1991 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00001992 return true;
1993 }
1994 }
1995 return false;
1996}
1997
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001998static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
1999 ArrayRef<OMPClause *> Clauses,
2000 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2001 bool ErrorFound = false;
2002 unsigned NamedModifiersNumber = 0;
2003 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2004 OMPD_unknown + 1);
2005 for (const auto *C : Clauses) {
2006 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2007 // At most one if clause without a directive-name-modifier can appear on
2008 // the directive.
2009 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2010 if (FoundNameModifiers[CurNM]) {
2011 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2012 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2013 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2014 ErrorFound = true;
2015 } else if (CurNM != OMPD_unknown)
2016 ++NamedModifiersNumber;
2017 FoundNameModifiers[CurNM] = IC;
2018 if (CurNM == OMPD_unknown)
2019 continue;
2020 // Check if the specified name modifier is allowed for the current
2021 // directive.
2022 // At most one if clause with the particular directive-name-modifier can
2023 // appear on the directive.
2024 bool MatchFound = false;
2025 for (auto NM : AllowedNameModifiers) {
2026 if (CurNM == NM) {
2027 MatchFound = true;
2028 break;
2029 }
2030 }
2031 if (!MatchFound) {
2032 S.Diag(IC->getNameModifierLoc(),
2033 diag::err_omp_wrong_if_directive_name_modifier)
2034 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2035 ErrorFound = true;
2036 }
2037 }
2038 }
2039 // If any if clause on the directive includes a directive-name-modifier then
2040 // all if clauses on the directive must include a directive-name-modifier.
2041 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2042 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2043 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2044 diag::err_omp_no_more_if_clause);
2045 } else {
2046 std::string Values;
2047 std::string Sep(", ");
2048 unsigned AllowedCnt = 0;
2049 unsigned TotalAllowedNum =
2050 AllowedNameModifiers.size() - NamedModifiersNumber;
2051 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2052 ++Cnt) {
2053 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2054 if (!FoundNameModifiers[NM]) {
2055 Values += "'";
2056 Values += getOpenMPDirectiveName(NM);
2057 Values += "'";
2058 if (AllowedCnt + 2 == TotalAllowedNum)
2059 Values += " or ";
2060 else if (AllowedCnt + 1 != TotalAllowedNum)
2061 Values += Sep;
2062 ++AllowedCnt;
2063 }
2064 }
2065 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2066 diag::err_omp_unnamed_if_clause)
2067 << (TotalAllowedNum > 1) << Values;
2068 }
2069 ErrorFound = true;
2070 }
2071 return ErrorFound;
2072}
2073
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002074StmtResult Sema::ActOnOpenMPExecutableDirective(
2075 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2076 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2077 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002078 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002079 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2080 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002081 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002082
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002083 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00002084 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002085 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002086 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002087 if (AStmt) {
2088 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2089
2090 // Check default data sharing attributes for referenced variables.
2091 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2092 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2093 if (DSAChecker.isErrorFound())
2094 return StmtError();
2095 // Generate list of implicitly defined firstprivate variables.
2096 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002097
2098 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2099 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2100 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2101 SourceLocation(), SourceLocation())) {
2102 ClausesWithImplicit.push_back(Implicit);
2103 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2104 DSAChecker.getImplicitFirstprivate().size();
2105 } else
2106 ErrorFound = true;
2107 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002108 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002109
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002110 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002111 switch (Kind) {
2112 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002113 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2114 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002115 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002116 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002117 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002118 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2119 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002120 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002121 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002122 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2123 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002124 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002125 case OMPD_for_simd:
2126 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2127 EndLoc, VarsWithInheritedDSA);
2128 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002129 case OMPD_sections:
2130 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2131 EndLoc);
2132 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002133 case OMPD_section:
2134 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002135 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002136 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2137 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002138 case OMPD_single:
2139 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2140 EndLoc);
2141 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002142 case OMPD_master:
2143 assert(ClausesWithImplicit.empty() &&
2144 "No clauses are allowed for 'omp master' directive");
2145 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2146 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002147 case OMPD_critical:
2148 assert(ClausesWithImplicit.empty() &&
2149 "No clauses are allowed for 'omp critical' directive");
2150 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
2151 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002152 case OMPD_parallel_for:
2153 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2154 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002155 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002156 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002157 case OMPD_parallel_for_simd:
2158 Res = ActOnOpenMPParallelForSimdDirective(
2159 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002160 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002161 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002162 case OMPD_parallel_sections:
2163 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2164 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002165 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002166 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002167 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002168 Res =
2169 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002170 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002171 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002172 case OMPD_taskyield:
2173 assert(ClausesWithImplicit.empty() &&
2174 "No clauses are allowed for 'omp taskyield' directive");
2175 assert(AStmt == nullptr &&
2176 "No associated statement allowed for 'omp taskyield' directive");
2177 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2178 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002179 case OMPD_barrier:
2180 assert(ClausesWithImplicit.empty() &&
2181 "No clauses are allowed for 'omp barrier' directive");
2182 assert(AStmt == nullptr &&
2183 "No associated statement allowed for 'omp barrier' directive");
2184 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2185 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00002186 case OMPD_taskwait:
2187 assert(ClausesWithImplicit.empty() &&
2188 "No clauses are allowed for 'omp taskwait' directive");
2189 assert(AStmt == nullptr &&
2190 "No associated statement allowed for 'omp taskwait' directive");
2191 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2192 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002193 case OMPD_taskgroup:
2194 assert(ClausesWithImplicit.empty() &&
2195 "No clauses are allowed for 'omp taskgroup' directive");
2196 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
2197 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00002198 case OMPD_flush:
2199 assert(AStmt == nullptr &&
2200 "No associated statement allowed for 'omp flush' directive");
2201 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2202 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002203 case OMPD_ordered:
2204 assert(ClausesWithImplicit.empty() &&
2205 "No clauses are allowed for 'omp ordered' directive");
2206 Res = ActOnOpenMPOrderedDirective(AStmt, StartLoc, EndLoc);
2207 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00002208 case OMPD_atomic:
2209 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2210 EndLoc);
2211 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002212 case OMPD_teams:
2213 Res =
2214 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2215 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002216 case OMPD_target:
2217 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2218 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002219 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002220 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002221 case OMPD_cancellation_point:
2222 assert(ClausesWithImplicit.empty() &&
2223 "No clauses are allowed for 'omp cancellation point' directive");
2224 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
2225 "cancellation point' directive");
2226 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2227 break;
Alexey Bataev80909872015-07-02 11:25:17 +00002228 case OMPD_cancel:
2229 assert(ClausesWithImplicit.empty() &&
2230 "No clauses are allowed for 'omp cancel' directive");
2231 assert(AStmt == nullptr &&
2232 "No associated statement allowed for 'omp cancel' directive");
2233 Res = ActOnOpenMPCancelDirective(StartLoc, EndLoc, CancelRegion);
2234 break;
Michael Wong65f367f2015-07-21 13:44:28 +00002235 case OMPD_target_data:
2236 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
2237 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002238 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00002239 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002240 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002241 llvm_unreachable("OpenMP Directive is not allowed");
2242 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002243 llvm_unreachable("Unknown OpenMP directive");
2244 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002245
Alexey Bataev4acb8592014-07-07 13:01:15 +00002246 for (auto P : VarsWithInheritedDSA) {
2247 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
2248 << P.first << P.second->getSourceRange();
2249 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002250 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
2251
2252 if (!AllowedNameModifiers.empty())
2253 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
2254 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002255
Alexey Bataeved09d242014-05-28 05:53:51 +00002256 if (ErrorFound)
2257 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002258 return Res;
2259}
2260
2261StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
2262 Stmt *AStmt,
2263 SourceLocation StartLoc,
2264 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002265 if (!AStmt)
2266 return StmtError();
2267
Alexey Bataev9959db52014-05-06 10:08:46 +00002268 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2269 // 1.2.2 OpenMP Language Terminology
2270 // Structured block - An executable statement with a single entry at the
2271 // top and a single exit at the bottom.
2272 // The point of exit cannot be a branch out of the structured block.
2273 // longjmp() and throw() must not violate the entry/exit criteria.
2274 CS->getCapturedDecl()->setNothrow();
2275
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002276 getCurFunction()->setHasBranchProtectedScope();
2277
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00002278 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
2279 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002280}
2281
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002282namespace {
2283/// \brief Helper class for checking canonical form of the OpenMP loops and
2284/// extracting iteration space of each loop in the loop nest, that will be used
2285/// for IR generation.
2286class OpenMPIterationSpaceChecker {
2287 /// \brief Reference to Sema.
2288 Sema &SemaRef;
2289 /// \brief A location for diagnostics (when there is no some better location).
2290 SourceLocation DefaultLoc;
2291 /// \brief A location for diagnostics (when increment is not compatible).
2292 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002293 /// \brief A source location for referring to loop init later.
2294 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002295 /// \brief A source location for referring to condition later.
2296 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002297 /// \brief A source location for referring to increment later.
2298 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002299 /// \brief Loop variable.
2300 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002301 /// \brief Reference to loop variable.
2302 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002303 /// \brief Lower bound (initializer for the var).
2304 Expr *LB;
2305 /// \brief Upper bound.
2306 Expr *UB;
2307 /// \brief Loop step (increment).
2308 Expr *Step;
2309 /// \brief This flag is true when condition is one of:
2310 /// Var < UB
2311 /// Var <= UB
2312 /// UB > Var
2313 /// UB >= Var
2314 bool TestIsLessOp;
2315 /// \brief This flag is true when condition is strict ( < or > ).
2316 bool TestIsStrictOp;
2317 /// \brief This flag is true when step is subtracted on each iteration.
2318 bool SubtractStep;
2319
2320public:
2321 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
2322 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00002323 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
2324 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002325 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
2326 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002327 /// \brief Check init-expr for canonical loop form and save loop counter
2328 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00002329 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002330 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
2331 /// for less/greater and for strict/non-strict comparison.
2332 bool CheckCond(Expr *S);
2333 /// \brief Check incr-expr for canonical loop form and return true if it
2334 /// does not conform, otherwise save loop step (#Step).
2335 bool CheckInc(Expr *S);
2336 /// \brief Return the loop counter variable.
2337 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002338 /// \brief Return the reference expression to loop counter variable.
2339 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002340 /// \brief Source range of the loop init.
2341 SourceRange GetInitSrcRange() const { return InitSrcRange; }
2342 /// \brief Source range of the loop condition.
2343 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
2344 /// \brief Source range of the loop increment.
2345 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
2346 /// \brief True if the step should be subtracted.
2347 bool ShouldSubtractStep() const { return SubtractStep; }
2348 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002349 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00002350 /// \brief Build the precondition expression for the loops.
2351 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002352 /// \brief Build reference expression to the counter be used for codegen.
2353 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00002354 /// \brief Build reference expression to the private counter be used for
2355 /// codegen.
2356 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002357 /// \brief Build initization of the counter be used for codegen.
2358 Expr *BuildCounterInit() const;
2359 /// \brief Build step of the counter be used for codegen.
2360 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002361 /// \brief Return true if any expression is dependent.
2362 bool Dependent() const;
2363
2364private:
2365 /// \brief Check the right-hand side of an assignment in the increment
2366 /// expression.
2367 bool CheckIncRHS(Expr *RHS);
2368 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002369 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002370 /// \brief Helper to set upper bound.
2371 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
2372 const SourceLocation &SL);
2373 /// \brief Helper to set loop increment.
2374 bool SetStep(Expr *NewStep, bool Subtract);
2375};
2376
2377bool OpenMPIterationSpaceChecker::Dependent() const {
2378 if (!Var) {
2379 assert(!LB && !UB && !Step);
2380 return false;
2381 }
2382 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
2383 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
2384}
2385
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002386template <typename T>
2387static T *getExprAsWritten(T *E) {
2388 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
2389 E = ExprTemp->getSubExpr();
2390
2391 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
2392 E = MTE->GetTemporaryExpr();
2393
2394 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
2395 E = Binder->getSubExpr();
2396
2397 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
2398 E = ICE->getSubExprAsWritten();
2399 return E->IgnoreParens();
2400}
2401
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002402bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
2403 DeclRefExpr *NewVarRefExpr,
2404 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002405 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002406 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
2407 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002408 if (!NewVar || !NewLB)
2409 return true;
2410 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002411 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002412 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
2413 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002414 if ((Ctor->isCopyOrMoveConstructor() ||
2415 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2416 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002417 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002418 LB = NewLB;
2419 return false;
2420}
2421
2422bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
2423 const SourceRange &SR,
2424 const SourceLocation &SL) {
2425 // State consistency checking to ensure correct usage.
2426 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
2427 !TestIsLessOp && !TestIsStrictOp);
2428 if (!NewUB)
2429 return true;
2430 UB = NewUB;
2431 TestIsLessOp = LessOp;
2432 TestIsStrictOp = StrictOp;
2433 ConditionSrcRange = SR;
2434 ConditionLoc = SL;
2435 return false;
2436}
2437
2438bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
2439 // State consistency checking to ensure correct usage.
2440 assert(Var != nullptr && LB != nullptr && Step == nullptr);
2441 if (!NewStep)
2442 return true;
2443 if (!NewStep->isValueDependent()) {
2444 // Check that the step is integer expression.
2445 SourceLocation StepLoc = NewStep->getLocStart();
2446 ExprResult Val =
2447 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
2448 if (Val.isInvalid())
2449 return true;
2450 NewStep = Val.get();
2451
2452 // OpenMP [2.6, Canonical Loop Form, Restrictions]
2453 // If test-expr is of form var relational-op b and relational-op is < or
2454 // <= then incr-expr must cause var to increase on each iteration of the
2455 // loop. If test-expr is of form var relational-op b and relational-op is
2456 // > or >= then incr-expr must cause var to decrease on each iteration of
2457 // the loop.
2458 // If test-expr is of form b relational-op var and relational-op is < or
2459 // <= then incr-expr must cause var to decrease on each iteration of the
2460 // loop. If test-expr is of form b relational-op var and relational-op is
2461 // > or >= then incr-expr must cause var to increase on each iteration of
2462 // the loop.
2463 llvm::APSInt Result;
2464 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
2465 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
2466 bool IsConstNeg =
2467 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002468 bool IsConstPos =
2469 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002470 bool IsConstZero = IsConstant && !Result.getBoolValue();
2471 if (UB && (IsConstZero ||
2472 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00002473 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002474 SemaRef.Diag(NewStep->getExprLoc(),
2475 diag::err_omp_loop_incr_not_compatible)
2476 << Var << TestIsLessOp << NewStep->getSourceRange();
2477 SemaRef.Diag(ConditionLoc,
2478 diag::note_omp_loop_cond_requres_compatible_incr)
2479 << TestIsLessOp << ConditionSrcRange;
2480 return true;
2481 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002482 if (TestIsLessOp == Subtract) {
2483 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
2484 NewStep).get();
2485 Subtract = !Subtract;
2486 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002487 }
2488
2489 Step = NewStep;
2490 SubtractStep = Subtract;
2491 return false;
2492}
2493
Alexey Bataev9c821032015-04-30 04:23:23 +00002494bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002495 // Check init-expr for canonical loop form and save loop counter
2496 // variable - #Var and its initialization value - #LB.
2497 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
2498 // var = lb
2499 // integer-type var = lb
2500 // random-access-iterator-type var = lb
2501 // pointer-type var = lb
2502 //
2503 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00002504 if (EmitDiags) {
2505 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
2506 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002507 return true;
2508 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002509 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002510 if (Expr *E = dyn_cast<Expr>(S))
2511 S = E->IgnoreParens();
2512 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2513 if (BO->getOpcode() == BO_Assign)
2514 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002515 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002516 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002517 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
2518 if (DS->isSingleDecl()) {
2519 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00002520 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002521 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00002522 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002523 SemaRef.Diag(S->getLocStart(),
2524 diag::ext_omp_loop_not_canonical_init)
2525 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002526 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002527 }
2528 }
2529 }
2530 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
2531 if (CE->getOperator() == OO_Equal)
2532 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002533 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2534 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002535
Alexey Bataev9c821032015-04-30 04:23:23 +00002536 if (EmitDiags) {
2537 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
2538 << S->getSourceRange();
2539 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002540 return true;
2541}
2542
Alexey Bataev23b69422014-06-18 07:08:49 +00002543/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002544/// variable (which may be the loop variable) if possible.
2545static const VarDecl *GetInitVarDecl(const Expr *E) {
2546 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00002547 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002548 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002549 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
2550 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002551 if ((Ctor->isCopyOrMoveConstructor() ||
2552 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2553 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002554 E = CE->getArg(0)->IgnoreParenImpCasts();
2555 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
2556 if (!DRE)
2557 return nullptr;
2558 return dyn_cast<VarDecl>(DRE->getDecl());
2559}
2560
2561bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
2562 // Check test-expr for canonical form, save upper-bound UB, flags for
2563 // less/greater and for strict/non-strict comparison.
2564 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2565 // var relational-op b
2566 // b relational-op var
2567 //
2568 if (!S) {
2569 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
2570 return true;
2571 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002572 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002573 SourceLocation CondLoc = S->getLocStart();
2574 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2575 if (BO->isRelationalOp()) {
2576 if (GetInitVarDecl(BO->getLHS()) == Var)
2577 return SetUB(BO->getRHS(),
2578 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
2579 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2580 BO->getSourceRange(), BO->getOperatorLoc());
2581 if (GetInitVarDecl(BO->getRHS()) == Var)
2582 return SetUB(BO->getLHS(),
2583 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
2584 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2585 BO->getSourceRange(), BO->getOperatorLoc());
2586 }
2587 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2588 if (CE->getNumArgs() == 2) {
2589 auto Op = CE->getOperator();
2590 switch (Op) {
2591 case OO_Greater:
2592 case OO_GreaterEqual:
2593 case OO_Less:
2594 case OO_LessEqual:
2595 if (GetInitVarDecl(CE->getArg(0)) == Var)
2596 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
2597 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2598 CE->getOperatorLoc());
2599 if (GetInitVarDecl(CE->getArg(1)) == Var)
2600 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
2601 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2602 CE->getOperatorLoc());
2603 break;
2604 default:
2605 break;
2606 }
2607 }
2608 }
2609 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
2610 << S->getSourceRange() << Var;
2611 return true;
2612}
2613
2614bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
2615 // RHS of canonical loop form increment can be:
2616 // var + incr
2617 // incr + var
2618 // var - incr
2619 //
2620 RHS = RHS->IgnoreParenImpCasts();
2621 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
2622 if (BO->isAdditiveOp()) {
2623 bool IsAdd = BO->getOpcode() == BO_Add;
2624 if (GetInitVarDecl(BO->getLHS()) == Var)
2625 return SetStep(BO->getRHS(), !IsAdd);
2626 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
2627 return SetStep(BO->getLHS(), false);
2628 }
2629 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
2630 bool IsAdd = CE->getOperator() == OO_Plus;
2631 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
2632 if (GetInitVarDecl(CE->getArg(0)) == Var)
2633 return SetStep(CE->getArg(1), !IsAdd);
2634 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
2635 return SetStep(CE->getArg(0), false);
2636 }
2637 }
2638 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2639 << RHS->getSourceRange() << Var;
2640 return true;
2641}
2642
2643bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
2644 // Check incr-expr for canonical loop form and return true if it
2645 // does not conform.
2646 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2647 // ++var
2648 // var++
2649 // --var
2650 // var--
2651 // var += incr
2652 // var -= incr
2653 // var = var + incr
2654 // var = incr + var
2655 // var = var - incr
2656 //
2657 if (!S) {
2658 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
2659 return true;
2660 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002661 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002662 S = S->IgnoreParens();
2663 if (auto UO = dyn_cast<UnaryOperator>(S)) {
2664 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
2665 return SetStep(
2666 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
2667 (UO->isDecrementOp() ? -1 : 1)).get(),
2668 false);
2669 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
2670 switch (BO->getOpcode()) {
2671 case BO_AddAssign:
2672 case BO_SubAssign:
2673 if (GetInitVarDecl(BO->getLHS()) == Var)
2674 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
2675 break;
2676 case BO_Assign:
2677 if (GetInitVarDecl(BO->getLHS()) == Var)
2678 return CheckIncRHS(BO->getRHS());
2679 break;
2680 default:
2681 break;
2682 }
2683 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2684 switch (CE->getOperator()) {
2685 case OO_PlusPlus:
2686 case OO_MinusMinus:
2687 if (GetInitVarDecl(CE->getArg(0)) == Var)
2688 return SetStep(
2689 SemaRef.ActOnIntegerConstant(
2690 CE->getLocStart(),
2691 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
2692 false);
2693 break;
2694 case OO_PlusEqual:
2695 case OO_MinusEqual:
2696 if (GetInitVarDecl(CE->getArg(0)) == Var)
2697 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
2698 break;
2699 case OO_Equal:
2700 if (GetInitVarDecl(CE->getArg(0)) == Var)
2701 return CheckIncRHS(CE->getArg(1));
2702 break;
2703 default:
2704 break;
2705 }
2706 }
2707 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2708 << S->getSourceRange() << Var;
2709 return true;
2710}
Alexander Musmana5f070a2014-10-01 06:03:56 +00002711
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002712namespace {
2713// Transform variables declared in GNU statement expressions to new ones to
2714// avoid crash on codegen.
2715class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
2716 typedef TreeTransform<TransformToNewDefs> BaseTransform;
2717
2718public:
2719 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
2720
2721 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
2722 if (auto *VD = cast<VarDecl>(D))
2723 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
2724 !isa<ImplicitParamDecl>(D)) {
2725 auto *NewVD = VarDecl::Create(
2726 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
2727 VD->getLocation(), VD->getIdentifier(), VD->getType(),
2728 VD->getTypeSourceInfo(), VD->getStorageClass());
2729 NewVD->setTSCSpec(VD->getTSCSpec());
2730 NewVD->setInit(VD->getInit());
2731 NewVD->setInitStyle(VD->getInitStyle());
2732 NewVD->setExceptionVariable(VD->isExceptionVariable());
2733 NewVD->setNRVOVariable(VD->isNRVOVariable());
2734 NewVD->setCXXForRangeDecl(VD->isInExternCXXContext());
2735 NewVD->setConstexpr(VD->isConstexpr());
2736 NewVD->setInitCapture(VD->isInitCapture());
2737 NewVD->setPreviousDeclInSameBlockScope(
2738 VD->isPreviousDeclInSameBlockScope());
2739 VD->getDeclContext()->addHiddenDecl(NewVD);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00002740 if (VD->hasAttrs())
2741 NewVD->setAttrs(VD->getAttrs());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002742 transformedLocalDecl(VD, NewVD);
2743 return NewVD;
2744 }
2745 return BaseTransform::TransformDefinition(Loc, D);
2746 }
2747
2748 ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
2749 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
2750 if (E->getDecl() != NewD) {
2751 NewD->setReferenced();
2752 NewD->markUsed(SemaRef.Context);
2753 return DeclRefExpr::Create(
2754 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
2755 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
2756 E->getNameInfo(), E->getType(), E->getValueKind());
2757 }
2758 return BaseTransform::TransformDeclRefExpr(E);
2759 }
2760};
2761}
2762
Alexander Musmana5f070a2014-10-01 06:03:56 +00002763/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002764Expr *
2765OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
2766 const bool LimitedType) const {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002767 TransformToNewDefs Transform(SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00002768 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002769 auto VarType = Var->getType().getNonReferenceType();
2770 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00002771 SemaRef.getLangOpts().CPlusPlus) {
2772 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002773 auto *UBExpr = TestIsLessOp ? UB : LB;
2774 auto *LBExpr = TestIsLessOp ? LB : UB;
2775 Expr *Upper = Transform.TransformExpr(UBExpr).get();
2776 Expr *Lower = Transform.TransformExpr(LBExpr).get();
2777 if (!Upper || !Lower)
2778 return nullptr;
2779 Upper = SemaRef.PerformImplicitConversion(Upper, UBExpr->getType(),
2780 Sema::AA_Converting,
2781 /*AllowExplicit=*/true)
2782 .get();
2783 Lower = SemaRef.PerformImplicitConversion(Lower, LBExpr->getType(),
2784 Sema::AA_Converting,
2785 /*AllowExplicit=*/true)
2786 .get();
2787 if (!Upper || !Lower)
2788 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002789
2790 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
2791
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002792 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002793 // BuildBinOp already emitted error, this one is to point user to upper
2794 // and lower bound, and to tell what is passed to 'operator-'.
2795 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
2796 << Upper->getSourceRange() << Lower->getSourceRange();
2797 return nullptr;
2798 }
2799 }
2800
2801 if (!Diff.isUsable())
2802 return nullptr;
2803
2804 // Upper - Lower [- 1]
2805 if (TestIsStrictOp)
2806 Diff = SemaRef.BuildBinOp(
2807 S, DefaultLoc, BO_Sub, Diff.get(),
2808 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2809 if (!Diff.isUsable())
2810 return nullptr;
2811
2812 // Upper - Lower [- 1] + Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002813 auto NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
2814 if (NewStep.isInvalid())
2815 return nullptr;
2816 NewStep = SemaRef.PerformImplicitConversion(
2817 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
2818 /*AllowExplicit=*/true);
2819 if (NewStep.isInvalid())
2820 return nullptr;
2821 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002822 if (!Diff.isUsable())
2823 return nullptr;
2824
2825 // Parentheses (for dumping/debugging purposes only).
2826 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
2827 if (!Diff.isUsable())
2828 return nullptr;
2829
2830 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002831 NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
2832 if (NewStep.isInvalid())
2833 return nullptr;
2834 NewStep = SemaRef.PerformImplicitConversion(
2835 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
2836 /*AllowExplicit=*/true);
2837 if (NewStep.isInvalid())
2838 return nullptr;
2839 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002840 if (!Diff.isUsable())
2841 return nullptr;
2842
Alexander Musman174b3ca2014-10-06 11:16:29 +00002843 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002844 QualType Type = Diff.get()->getType();
2845 auto &C = SemaRef.Context;
2846 bool UseVarType = VarType->hasIntegerRepresentation() &&
2847 C.getTypeSize(Type) > C.getTypeSize(VarType);
2848 if (!Type->isIntegerType() || UseVarType) {
2849 unsigned NewSize =
2850 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
2851 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
2852 : Type->hasSignedIntegerRepresentation();
2853 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
2854 Diff = SemaRef.PerformImplicitConversion(
2855 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
2856 if (!Diff.isUsable())
2857 return nullptr;
2858 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00002859 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00002860 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
2861 if (NewSize != C.getTypeSize(Type)) {
2862 if (NewSize < C.getTypeSize(Type)) {
2863 assert(NewSize == 64 && "incorrect loop var size");
2864 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
2865 << InitSrcRange << ConditionSrcRange;
2866 }
2867 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002868 NewSize, Type->hasSignedIntegerRepresentation() ||
2869 C.getTypeSize(Type) < NewSize);
Alexander Musman174b3ca2014-10-06 11:16:29 +00002870 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
2871 Sema::AA_Converting, true);
2872 if (!Diff.isUsable())
2873 return nullptr;
2874 }
2875 }
2876
Alexander Musmana5f070a2014-10-01 06:03:56 +00002877 return Diff.get();
2878}
2879
Alexey Bataev62dbb972015-04-22 11:59:37 +00002880Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
2881 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
2882 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
2883 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002884 TransformToNewDefs Transform(SemaRef);
2885
2886 auto NewLB = Transform.TransformExpr(LB);
2887 auto NewUB = Transform.TransformExpr(UB);
2888 if (NewLB.isInvalid() || NewUB.isInvalid())
2889 return Cond;
2890 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
2891 Sema::AA_Converting,
2892 /*AllowExplicit=*/true);
2893 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
2894 Sema::AA_Converting,
2895 /*AllowExplicit=*/true);
2896 if (NewLB.isInvalid() || NewUB.isInvalid())
2897 return Cond;
Alexey Bataev62dbb972015-04-22 11:59:37 +00002898 auto CondExpr = SemaRef.BuildBinOp(
2899 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
2900 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002901 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002902 if (CondExpr.isUsable()) {
2903 CondExpr = SemaRef.PerformImplicitConversion(
2904 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
2905 /*AllowExplicit=*/true);
2906 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00002907 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
2908 // Otherwise use original loop conditon and evaluate it in runtime.
2909 return CondExpr.isUsable() ? CondExpr.get() : Cond;
2910}
2911
Alexander Musmana5f070a2014-10-01 06:03:56 +00002912/// \brief Build reference expression to the counter be used for codegen.
2913Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00002914 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
2915 DefaultLoc);
2916}
2917
2918Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
2919 if (Var && !Var->isInvalidDecl()) {
2920 auto Type = Var->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00002921 auto *PrivateVar =
2922 buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName(),
2923 Var->hasAttrs() ? &Var->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00002924 if (PrivateVar->isInvalidDecl())
2925 return nullptr;
2926 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
2927 }
2928 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002929}
2930
2931/// \brief Build initization of the counter be used for codegen.
2932Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
2933
2934/// \brief Build step of the counter be used for codegen.
2935Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
2936
2937/// \brief Iteration space of a single for loop.
2938struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00002939 /// \brief Condition of the loop.
2940 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002941 /// \brief This expression calculates the number of iterations in the loop.
2942 /// It is always possible to calculate it before starting the loop.
2943 Expr *NumIterations;
2944 /// \brief The loop counter variable.
2945 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00002946 /// \brief Private loop counter variable.
2947 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002948 /// \brief This is initializer for the initial value of #CounterVar.
2949 Expr *CounterInit;
2950 /// \brief This is step for the #CounterVar used to generate its update:
2951 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
2952 Expr *CounterStep;
2953 /// \brief Should step be subtracted?
2954 bool Subtract;
2955 /// \brief Source range of the loop init.
2956 SourceRange InitSrcRange;
2957 /// \brief Source range of the loop condition.
2958 SourceRange CondSrcRange;
2959 /// \brief Source range of the loop increment.
2960 SourceRange IncSrcRange;
2961};
2962
Alexey Bataev23b69422014-06-18 07:08:49 +00002963} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002964
Alexey Bataev9c821032015-04-30 04:23:23 +00002965void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
2966 assert(getLangOpts().OpenMP && "OpenMP is not active.");
2967 assert(Init && "Expected loop in canonical form.");
2968 unsigned CollapseIteration = DSAStack->getCollapseNumber();
2969 if (CollapseIteration > 0 &&
2970 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
2971 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
2972 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
2973 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
2974 }
2975 DSAStack->setCollapseNumber(CollapseIteration - 1);
2976 }
2977}
2978
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002979/// \brief Called on a for stmt to check and extract its iteration space
2980/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00002981static bool CheckOpenMPIterationSpace(
2982 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
2983 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00002984 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002985 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
2986 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002987 // OpenMP [2.6, Canonical Loop Form]
2988 // for (init-expr; test-expr; incr-expr) structured-block
2989 auto For = dyn_cast_or_null<ForStmt>(S);
2990 if (!For) {
2991 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00002992 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
2993 << getOpenMPDirectiveName(DKind) << NestedLoopCount
2994 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
2995 if (NestedLoopCount > 1) {
2996 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
2997 SemaRef.Diag(DSA.getConstructLoc(),
2998 diag::note_omp_collapse_ordered_expr)
2999 << 2 << CollapseLoopCountExpr->getSourceRange()
3000 << OrderedLoopCountExpr->getSourceRange();
3001 else if (CollapseLoopCountExpr)
3002 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3003 diag::note_omp_collapse_ordered_expr)
3004 << 0 << CollapseLoopCountExpr->getSourceRange();
3005 else
3006 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3007 diag::note_omp_collapse_ordered_expr)
3008 << 1 << OrderedLoopCountExpr->getSourceRange();
3009 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003010 return true;
3011 }
3012 assert(For->getBody());
3013
3014 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3015
3016 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003017 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003018 if (ISC.CheckInit(Init)) {
3019 return true;
3020 }
3021
3022 bool HasErrors = false;
3023
3024 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003025 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003026
3027 // OpenMP [2.6, Canonical Loop Form]
3028 // Var is one of the following:
3029 // A variable of signed or unsigned integer type.
3030 // For C++, a variable of a random access iterator type.
3031 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00003032 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003033 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3034 !VarType->isPointerType() &&
3035 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3036 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3037 << SemaRef.getLangOpts().CPlusPlus;
3038 HasErrors = true;
3039 }
3040
Alexey Bataev4acb8592014-07-07 13:01:15 +00003041 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
3042 // Construct
3043 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3044 // parallel for construct is (are) private.
3045 // The loop iteration variable in the associated for-loop of a simd construct
3046 // with just one associated for-loop is linear with a constant-linear-step
3047 // that is the increment of the associated for-loop.
3048 // Exclude loop var from the list of variables with implicitly defined data
3049 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00003050 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003051
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003052 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
3053 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00003054 // The loop iteration variable in the associated for-loop of a simd construct
3055 // with just one associated for-loop may be listed in a linear clause with a
3056 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003057 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3058 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003059 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003060 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
3061 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3062 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003063 auto PredeterminedCKind =
3064 isOpenMPSimdDirective(DKind)
3065 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3066 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003067 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00003068 DVar.CKind != OMPC_threadprivate && DVar.CKind != PredeterminedCKind) ||
Alexander Musmanf82886e2014-09-18 05:12:34 +00003069 (isOpenMPWorksharingDirective(DKind) && !isOpenMPSimdDirective(DKind) &&
3070 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00003071 DVar.CKind != OMPC_lastprivate && DVar.CKind != OMPC_threadprivate)) &&
3072 ((DVar.CKind != OMPC_private && DVar.CKind != OMPC_threadprivate) ||
3073 DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003074 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00003075 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3076 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00003077 if (DVar.RefExpr == nullptr)
3078 DVar.CKind = PredeterminedCKind;
3079 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003080 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003081 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003082 // Make the loop iteration variable private (for worksharing constructs),
3083 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00003084 // lastprivate (for simd directives with several collapsed or ordered
3085 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00003086 if (DVar.CKind == OMPC_unknown)
3087 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
3088 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00003089 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003090 }
3091
Alexey Bataev7ff55242014-06-19 09:13:45 +00003092 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00003093
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003094 // Check test-expr.
3095 HasErrors |= ISC.CheckCond(For->getCond());
3096
3097 // Check incr-expr.
3098 HasErrors |= ISC.CheckInc(For->getInc());
3099
Alexander Musmana5f070a2014-10-01 06:03:56 +00003100 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003101 return HasErrors;
3102
Alexander Musmana5f070a2014-10-01 06:03:56 +00003103 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003104 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00003105 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
3106 DSA.getCurScope(), /* LimitedType */ isOpenMPWorksharingDirective(DKind));
Alexander Musmana5f070a2014-10-01 06:03:56 +00003107 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00003108 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003109 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
3110 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
3111 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
3112 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
3113 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
3114 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
3115
Alexey Bataev62dbb972015-04-22 11:59:37 +00003116 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
3117 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003118 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00003119 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003120 ResultIterSpace.CounterInit == nullptr ||
3121 ResultIterSpace.CounterStep == nullptr);
3122
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003123 return HasErrors;
3124}
3125
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003126/// \brief Build 'VarRef = Start.
3127static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
3128 ExprResult VarRef, ExprResult Start) {
3129 TransformToNewDefs Transform(SemaRef);
3130 // Build 'VarRef = Start.
3131 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3132 if (NewStart.isInvalid())
3133 return ExprError();
3134 NewStart = SemaRef.PerformImplicitConversion(
3135 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3136 Sema::AA_Converting,
3137 /*AllowExplicit=*/true);
3138 if (NewStart.isInvalid())
3139 return ExprError();
3140 NewStart = SemaRef.PerformImplicitConversion(
3141 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
3142 /*AllowExplicit=*/true);
3143 if (!NewStart.isUsable())
3144 return ExprError();
3145
3146 auto Init =
3147 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
3148 return Init;
3149}
3150
Alexander Musmana5f070a2014-10-01 06:03:56 +00003151/// \brief Build 'VarRef = Start + Iter * Step'.
3152static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
3153 SourceLocation Loc, ExprResult VarRef,
3154 ExprResult Start, ExprResult Iter,
3155 ExprResult Step, bool Subtract) {
3156 // Add parentheses (for debugging purposes only).
3157 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
3158 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
3159 !Step.isUsable())
3160 return ExprError();
3161
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003162 TransformToNewDefs Transform(SemaRef);
3163 auto NewStep = Transform.TransformExpr(Step.get()->IgnoreImplicit());
3164 if (NewStep.isInvalid())
3165 return ExprError();
3166 NewStep = SemaRef.PerformImplicitConversion(
3167 NewStep.get(), Step.get()->IgnoreImplicit()->getType(),
3168 Sema::AA_Converting,
3169 /*AllowExplicit=*/true);
3170 if (NewStep.isInvalid())
3171 return ExprError();
3172 ExprResult Update =
3173 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003174 if (!Update.isUsable())
3175 return ExprError();
3176
3177 // Build 'VarRef = Start + Iter * Step'.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003178 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3179 if (NewStart.isInvalid())
3180 return ExprError();
3181 NewStart = SemaRef.PerformImplicitConversion(
3182 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3183 Sema::AA_Converting,
3184 /*AllowExplicit=*/true);
3185 if (NewStart.isInvalid())
3186 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003187 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003188 NewStart.get(), Update.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003189 if (!Update.isUsable())
3190 return ExprError();
3191
3192 Update = SemaRef.PerformImplicitConversion(
3193 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
3194 if (!Update.isUsable())
3195 return ExprError();
3196
3197 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
3198 return Update;
3199}
3200
3201/// \brief Convert integer expression \a E to make it have at least \a Bits
3202/// bits.
3203static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
3204 Sema &SemaRef) {
3205 if (E == nullptr)
3206 return ExprError();
3207 auto &C = SemaRef.Context;
3208 QualType OldType = E->getType();
3209 unsigned HasBits = C.getTypeSize(OldType);
3210 if (HasBits >= Bits)
3211 return ExprResult(E);
3212 // OK to convert to signed, because new type has more bits than old.
3213 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
3214 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
3215 true);
3216}
3217
3218/// \brief Check if the given expression \a E is a constant integer that fits
3219/// into \a Bits bits.
3220static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
3221 if (E == nullptr)
3222 return false;
3223 llvm::APSInt Result;
3224 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
3225 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
3226 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003227}
3228
3229/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00003230/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
3231/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003232static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00003233CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
3234 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
3235 DSAStackTy &DSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003236 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00003237 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003238 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003239 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003240 // Found 'collapse' clause - calculate collapse number.
3241 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003242 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
3243 NestedLoopCount += Result.getLimitedValue() - 1;
3244 }
3245 if (OrderedLoopCountExpr) {
3246 // Found 'ordered' clause - calculate collapse number.
3247 llvm::APSInt Result;
3248 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
3249 NestedLoopCount += Result.getLimitedValue() - 1;
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003250 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003251 // This is helper routine for loop directives (e.g., 'for', 'simd',
3252 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00003253 SmallVector<LoopIterationSpace, 4> IterSpaces;
3254 IterSpaces.resize(NestedLoopCount);
3255 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003256 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003257 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003258 NestedLoopCount, CollapseLoopCountExpr,
3259 OrderedLoopCountExpr, VarsWithImplicitDSA,
3260 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00003261 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003262 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003263 // OpenMP [2.8.1, simd construct, Restrictions]
3264 // All loops associated with the construct must be perfectly nested; that
3265 // is, there must be no intervening code nor any OpenMP directive between
3266 // any two loops.
3267 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003268 }
3269
Alexander Musmana5f070a2014-10-01 06:03:56 +00003270 Built.clear(/* size */ NestedLoopCount);
3271
3272 if (SemaRef.CurContext->isDependentContext())
3273 return NestedLoopCount;
3274
3275 // An example of what is generated for the following code:
3276 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00003277 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00003278 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003279 // for (k = 0; k < NK; ++k)
3280 // for (j = J0; j < NJ; j+=2) {
3281 // <loop body>
3282 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003283 //
3284 // We generate the code below.
3285 // Note: the loop body may be outlined in CodeGen.
3286 // Note: some counters may be C++ classes, operator- is used to find number of
3287 // iterations and operator+= to calculate counter value.
3288 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
3289 // or i64 is currently supported).
3290 //
3291 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
3292 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
3293 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
3294 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
3295 // // similar updates for vars in clauses (e.g. 'linear')
3296 // <loop body (using local i and j)>
3297 // }
3298 // i = NI; // assign final values of counters
3299 // j = NJ;
3300 //
3301
3302 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
3303 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003304 // Precondition tests if there is at least one iteration (all conditions are
3305 // true).
3306 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003307 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003308 ExprResult LastIteration32 = WidenIterationCount(
3309 32 /* Bits */, SemaRef.PerformImplicitConversion(
3310 N0->IgnoreImpCasts(), N0->getType(),
3311 Sema::AA_Converting, /*AllowExplicit=*/true)
3312 .get(),
3313 SemaRef);
3314 ExprResult LastIteration64 = WidenIterationCount(
3315 64 /* Bits */, SemaRef.PerformImplicitConversion(
3316 N0->IgnoreImpCasts(), N0->getType(),
3317 Sema::AA_Converting, /*AllowExplicit=*/true)
3318 .get(),
3319 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003320
3321 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
3322 return NestedLoopCount;
3323
3324 auto &C = SemaRef.Context;
3325 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
3326
3327 Scope *CurScope = DSA.getCurScope();
3328 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003329 if (PreCond.isUsable()) {
3330 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
3331 PreCond.get(), IterSpaces[Cnt].PreCond);
3332 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003333 auto N = IterSpaces[Cnt].NumIterations;
3334 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
3335 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003336 LastIteration32 = SemaRef.BuildBinOp(
3337 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
3338 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3339 Sema::AA_Converting,
3340 /*AllowExplicit=*/true)
3341 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003342 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003343 LastIteration64 = SemaRef.BuildBinOp(
3344 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
3345 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3346 Sema::AA_Converting,
3347 /*AllowExplicit=*/true)
3348 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003349 }
3350
3351 // Choose either the 32-bit or 64-bit version.
3352 ExprResult LastIteration = LastIteration64;
3353 if (LastIteration32.isUsable() &&
3354 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
3355 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
3356 FitsInto(
3357 32 /* Bits */,
3358 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
3359 LastIteration64.get(), SemaRef)))
3360 LastIteration = LastIteration32;
3361
3362 if (!LastIteration.isUsable())
3363 return 0;
3364
3365 // Save the number of iterations.
3366 ExprResult NumIterations = LastIteration;
3367 {
3368 LastIteration = SemaRef.BuildBinOp(
3369 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
3370 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3371 if (!LastIteration.isUsable())
3372 return 0;
3373 }
3374
3375 // Calculate the last iteration number beforehand instead of doing this on
3376 // each iteration. Do not do this if the number of iterations may be kfold-ed.
3377 llvm::APSInt Result;
3378 bool IsConstant =
3379 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
3380 ExprResult CalcLastIteration;
3381 if (!IsConstant) {
3382 SourceLocation SaveLoc;
3383 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003384 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003385 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00003386 ExprResult SaveRef = buildDeclRefExpr(
3387 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003388 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
3389 SaveRef.get(), LastIteration.get());
3390 LastIteration = SaveRef;
3391
3392 // Prepare SaveRef + 1.
3393 NumIterations = SemaRef.BuildBinOp(
3394 CurScope, SaveLoc, BO_Add, SaveRef.get(),
3395 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3396 if (!NumIterations.isUsable())
3397 return 0;
3398 }
3399
3400 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
3401
Alexander Musmanc6388682014-12-15 07:07:06 +00003402 QualType VType = LastIteration.get()->getType();
3403 // Build variables passed into runtime, nesessary for worksharing directives.
3404 ExprResult LB, UB, IL, ST, EUB;
3405 if (isOpenMPWorksharingDirective(DKind)) {
3406 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003407 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
3408 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003409 SemaRef.AddInitializerToDecl(
3410 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3411 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3412
3413 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003414 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
3415 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003416 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
3417 /*DirectInit*/ false,
3418 /*TypeMayContainAuto*/ false);
3419
3420 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
3421 // This will be used to implement clause 'lastprivate'.
3422 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00003423 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
3424 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003425 SemaRef.AddInitializerToDecl(
3426 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3427 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3428
3429 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00003430 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
3431 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003432 SemaRef.AddInitializerToDecl(
3433 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
3434 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3435
3436 // Build expression: UB = min(UB, LastIteration)
3437 // It is nesessary for CodeGen of directives with static scheduling.
3438 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
3439 UB.get(), LastIteration.get());
3440 ExprResult CondOp = SemaRef.ActOnConditionalOp(
3441 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
3442 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
3443 CondOp.get());
3444 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
3445 }
3446
3447 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003448 ExprResult IV;
3449 ExprResult Init;
3450 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00003451 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
3452 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003453 Expr *RHS = isOpenMPWorksharingDirective(DKind)
3454 ? LB.get()
3455 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
3456 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
3457 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003458 }
3459
Alexander Musmanc6388682014-12-15 07:07:06 +00003460 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003461 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00003462 ExprResult Cond =
3463 isOpenMPWorksharingDirective(DKind)
3464 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
3465 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
3466 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003467
3468 // Loop increment (IV = IV + 1)
3469 SourceLocation IncLoc;
3470 ExprResult Inc =
3471 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
3472 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
3473 if (!Inc.isUsable())
3474 return 0;
3475 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00003476 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
3477 if (!Inc.isUsable())
3478 return 0;
3479
3480 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
3481 // Used for directives with static scheduling.
3482 ExprResult NextLB, NextUB;
3483 if (isOpenMPWorksharingDirective(DKind)) {
3484 // LB + ST
3485 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
3486 if (!NextLB.isUsable())
3487 return 0;
3488 // LB = LB + ST
3489 NextLB =
3490 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
3491 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
3492 if (!NextLB.isUsable())
3493 return 0;
3494 // UB + ST
3495 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
3496 if (!NextUB.isUsable())
3497 return 0;
3498 // UB = UB + ST
3499 NextUB =
3500 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
3501 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
3502 if (!NextUB.isUsable())
3503 return 0;
3504 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003505
3506 // Build updates and final values of the loop counters.
3507 bool HasErrors = false;
3508 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003509 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003510 Built.Updates.resize(NestedLoopCount);
3511 Built.Finals.resize(NestedLoopCount);
3512 {
3513 ExprResult Div;
3514 // Go from inner nested loop to outer.
3515 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
3516 LoopIterationSpace &IS = IterSpaces[Cnt];
3517 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
3518 // Build: Iter = (IV / Div) % IS.NumIters
3519 // where Div is product of previous iterations' IS.NumIters.
3520 ExprResult Iter;
3521 if (Div.isUsable()) {
3522 Iter =
3523 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
3524 } else {
3525 Iter = IV;
3526 assert((Cnt == (int)NestedLoopCount - 1) &&
3527 "unusable div expected on first iteration only");
3528 }
3529
3530 if (Cnt != 0 && Iter.isUsable())
3531 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
3532 IS.NumIterations);
3533 if (!Iter.isUsable()) {
3534 HasErrors = true;
3535 break;
3536 }
3537
Alexey Bataev39f915b82015-05-08 10:41:21 +00003538 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
3539 auto *CounterVar = buildDeclRefExpr(
3540 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
3541 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
3542 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003543 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
3544 IS.CounterInit);
3545 if (!Init.isUsable()) {
3546 HasErrors = true;
3547 break;
3548 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003549 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003550 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003551 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
3552 if (!Update.isUsable()) {
3553 HasErrors = true;
3554 break;
3555 }
3556
3557 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
3558 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00003559 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003560 IS.NumIterations, IS.CounterStep, IS.Subtract);
3561 if (!Final.isUsable()) {
3562 HasErrors = true;
3563 break;
3564 }
3565
3566 // Build Div for the next iteration: Div <- Div * IS.NumIters
3567 if (Cnt != 0) {
3568 if (Div.isUnset())
3569 Div = IS.NumIterations;
3570 else
3571 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
3572 IS.NumIterations);
3573
3574 // Add parentheses (for debugging purposes only).
3575 if (Div.isUsable())
3576 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
3577 if (!Div.isUsable()) {
3578 HasErrors = true;
3579 break;
3580 }
3581 }
3582 if (!Update.isUsable() || !Final.isUsable()) {
3583 HasErrors = true;
3584 break;
3585 }
3586 // Save results
3587 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003588 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003589 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003590 Built.Updates[Cnt] = Update.get();
3591 Built.Finals[Cnt] = Final.get();
3592 }
3593 }
3594
3595 if (HasErrors)
3596 return 0;
3597
3598 // Save results
3599 Built.IterationVarRef = IV.get();
3600 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00003601 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003602 Built.CalcLastIteration =
3603 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003604 Built.PreCond = PreCond.get();
3605 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003606 Built.Init = Init.get();
3607 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00003608 Built.LB = LB.get();
3609 Built.UB = UB.get();
3610 Built.IL = IL.get();
3611 Built.ST = ST.get();
3612 Built.EUB = EUB.get();
3613 Built.NLB = NextLB.get();
3614 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003615
Alexey Bataevabfc0692014-06-25 06:52:00 +00003616 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003617}
3618
Alexey Bataev10e775f2015-07-30 11:36:16 +00003619static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00003620 auto CollapseClauses =
3621 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
3622 if (CollapseClauses.begin() != CollapseClauses.end())
3623 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003624 return nullptr;
3625}
3626
Alexey Bataev10e775f2015-07-30 11:36:16 +00003627static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00003628 auto OrderedClauses =
3629 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
3630 if (OrderedClauses.begin() != OrderedClauses.end())
3631 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00003632 return nullptr;
3633}
3634
Alexey Bataev66b15b52015-08-21 11:14:16 +00003635static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
3636 const Expr *Safelen) {
3637 llvm::APSInt SimdlenRes, SafelenRes;
3638 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
3639 Simdlen->isInstantiationDependent() ||
3640 Simdlen->containsUnexpandedParameterPack())
3641 return false;
3642 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
3643 Safelen->isInstantiationDependent() ||
3644 Safelen->containsUnexpandedParameterPack())
3645 return false;
3646 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
3647 Safelen->EvaluateAsInt(SafelenRes, S.Context);
3648 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
3649 // If both simdlen and safelen clauses are specified, the value of the simdlen
3650 // parameter must be less than or equal to the value of the safelen parameter.
3651 if (SimdlenRes > SafelenRes) {
3652 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
3653 << Simdlen->getSourceRange() << Safelen->getSourceRange();
3654 return true;
3655 }
3656 return false;
3657}
3658
Alexey Bataev4acb8592014-07-07 13:01:15 +00003659StmtResult Sema::ActOnOpenMPSimdDirective(
3660 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3661 SourceLocation EndLoc,
3662 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003663 if (!AStmt)
3664 return StmtError();
3665
3666 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00003667 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003668 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3669 // define the nested loops number.
3670 unsigned NestedLoopCount = CheckOpenMPLoop(
3671 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
3672 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00003673 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003674 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003675
Alexander Musmana5f070a2014-10-01 06:03:56 +00003676 assert((CurContext->isDependentContext() || B.builtAll()) &&
3677 "omp simd loop exprs were not built");
3678
Alexander Musman3276a272015-03-21 10:12:56 +00003679 if (!CurContext->isDependentContext()) {
3680 // Finalize the clauses that need pre-built expressions for CodeGen.
3681 for (auto C : Clauses) {
3682 if (auto LC = dyn_cast<OMPLinearClause>(C))
3683 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3684 B.NumIterations, *this, CurScope))
3685 return StmtError();
3686 }
3687 }
3688
Alexey Bataev66b15b52015-08-21 11:14:16 +00003689 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
3690 // If both simdlen and safelen clauses are specified, the value of the simdlen
3691 // parameter must be less than or equal to the value of the safelen parameter.
3692 OMPSafelenClause *Safelen = nullptr;
3693 OMPSimdlenClause *Simdlen = nullptr;
3694 for (auto *Clause : Clauses) {
3695 if (Clause->getClauseKind() == OMPC_safelen)
3696 Safelen = cast<OMPSafelenClause>(Clause);
3697 else if (Clause->getClauseKind() == OMPC_simdlen)
3698 Simdlen = cast<OMPSimdlenClause>(Clause);
3699 if (Safelen && Simdlen)
3700 break;
3701 }
3702 if (Simdlen && Safelen &&
3703 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
3704 Safelen->getSafelen()))
3705 return StmtError();
3706
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003707 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003708 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3709 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003710}
3711
Alexey Bataev4acb8592014-07-07 13:01:15 +00003712StmtResult Sema::ActOnOpenMPForDirective(
3713 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3714 SourceLocation EndLoc,
3715 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003716 if (!AStmt)
3717 return StmtError();
3718
3719 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00003720 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003721 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3722 // define the nested loops number.
3723 unsigned NestedLoopCount = CheckOpenMPLoop(
3724 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
3725 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00003726 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00003727 return StmtError();
3728
Alexander Musmana5f070a2014-10-01 06:03:56 +00003729 assert((CurContext->isDependentContext() || B.builtAll()) &&
3730 "omp for loop exprs were not built");
3731
Alexey Bataev54acd402015-08-04 11:18:19 +00003732 if (!CurContext->isDependentContext()) {
3733 // Finalize the clauses that need pre-built expressions for CodeGen.
3734 for (auto C : Clauses) {
3735 if (auto LC = dyn_cast<OMPLinearClause>(C))
3736 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3737 B.NumIterations, *this, CurScope))
3738 return StmtError();
3739 }
3740 }
3741
Alexey Bataevf29276e2014-06-18 04:14:57 +00003742 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003743 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3744 Clauses, AStmt, B);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003745}
3746
Alexander Musmanf82886e2014-09-18 05:12:34 +00003747StmtResult Sema::ActOnOpenMPForSimdDirective(
3748 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3749 SourceLocation EndLoc,
3750 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003751 if (!AStmt)
3752 return StmtError();
3753
3754 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00003755 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003756 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3757 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00003758 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00003759 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
3760 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
3761 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00003762 if (NestedLoopCount == 0)
3763 return StmtError();
3764
Alexander Musmanc6388682014-12-15 07:07:06 +00003765 assert((CurContext->isDependentContext() || B.builtAll()) &&
3766 "omp for simd loop exprs were not built");
3767
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00003768 if (!CurContext->isDependentContext()) {
3769 // Finalize the clauses that need pre-built expressions for CodeGen.
3770 for (auto C : Clauses) {
3771 if (auto LC = dyn_cast<OMPLinearClause>(C))
3772 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3773 B.NumIterations, *this, CurScope))
3774 return StmtError();
3775 }
3776 }
3777
Alexey Bataev66b15b52015-08-21 11:14:16 +00003778 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
3779 // If both simdlen and safelen clauses are specified, the value of the simdlen
3780 // parameter must be less than or equal to the value of the safelen parameter.
3781 OMPSafelenClause *Safelen = nullptr;
3782 OMPSimdlenClause *Simdlen = nullptr;
3783 for (auto *Clause : Clauses) {
3784 if (Clause->getClauseKind() == OMPC_safelen)
3785 Safelen = cast<OMPSafelenClause>(Clause);
3786 else if (Clause->getClauseKind() == OMPC_simdlen)
3787 Simdlen = cast<OMPSimdlenClause>(Clause);
3788 if (Safelen && Simdlen)
3789 break;
3790 }
3791 if (Simdlen && Safelen &&
3792 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
3793 Safelen->getSafelen()))
3794 return StmtError();
3795
Alexander Musmanf82886e2014-09-18 05:12:34 +00003796 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003797 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3798 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00003799}
3800
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003801StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
3802 Stmt *AStmt,
3803 SourceLocation StartLoc,
3804 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003805 if (!AStmt)
3806 return StmtError();
3807
3808 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003809 auto BaseStmt = AStmt;
3810 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3811 BaseStmt = CS->getCapturedStmt();
3812 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3813 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00003814 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003815 return StmtError();
3816 // All associated statements must be '#pragma omp section' except for
3817 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00003818 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003819 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3820 if (SectionStmt)
3821 Diag(SectionStmt->getLocStart(),
3822 diag::err_omp_sections_substmt_not_section);
3823 return StmtError();
3824 }
3825 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003826 } else {
3827 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
3828 return StmtError();
3829 }
3830
3831 getCurFunction()->setHasBranchProtectedScope();
3832
3833 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
3834 AStmt);
3835}
3836
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003837StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
3838 SourceLocation StartLoc,
3839 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003840 if (!AStmt)
3841 return StmtError();
3842
3843 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003844
3845 getCurFunction()->setHasBranchProtectedScope();
3846
3847 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
3848}
3849
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003850StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
3851 Stmt *AStmt,
3852 SourceLocation StartLoc,
3853 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003854 if (!AStmt)
3855 return StmtError();
3856
3857 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00003858
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003859 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00003860
Alexey Bataev3255bf32015-01-19 05:20:46 +00003861 // OpenMP [2.7.3, single Construct, Restrictions]
3862 // The copyprivate clause must not be used with the nowait clause.
3863 OMPClause *Nowait = nullptr;
3864 OMPClause *Copyprivate = nullptr;
3865 for (auto *Clause : Clauses) {
3866 if (Clause->getClauseKind() == OMPC_nowait)
3867 Nowait = Clause;
3868 else if (Clause->getClauseKind() == OMPC_copyprivate)
3869 Copyprivate = Clause;
3870 if (Copyprivate && Nowait) {
3871 Diag(Copyprivate->getLocStart(),
3872 diag::err_omp_single_copyprivate_with_nowait);
3873 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
3874 return StmtError();
3875 }
3876 }
3877
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003878 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3879}
3880
Alexander Musman80c22892014-07-17 08:54:58 +00003881StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
3882 SourceLocation StartLoc,
3883 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003884 if (!AStmt)
3885 return StmtError();
3886
3887 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00003888
3889 getCurFunction()->setHasBranchProtectedScope();
3890
3891 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
3892}
3893
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003894StmtResult
3895Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
3896 Stmt *AStmt, SourceLocation StartLoc,
3897 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003898 if (!AStmt)
3899 return StmtError();
3900
3901 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003902
3903 getCurFunction()->setHasBranchProtectedScope();
3904
3905 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
3906 AStmt);
3907}
3908
Alexey Bataev4acb8592014-07-07 13:01:15 +00003909StmtResult Sema::ActOnOpenMPParallelForDirective(
3910 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3911 SourceLocation EndLoc,
3912 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003913 if (!AStmt)
3914 return StmtError();
3915
Alexey Bataev4acb8592014-07-07 13:01:15 +00003916 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3917 // 1.2.2 OpenMP Language Terminology
3918 // Structured block - An executable statement with a single entry at the
3919 // top and a single exit at the bottom.
3920 // The point of exit cannot be a branch out of the structured block.
3921 // longjmp() and throw() must not violate the entry/exit criteria.
3922 CS->getCapturedDecl()->setNothrow();
3923
Alexander Musmanc6388682014-12-15 07:07:06 +00003924 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003925 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3926 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003927 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00003928 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
3929 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
3930 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003931 if (NestedLoopCount == 0)
3932 return StmtError();
3933
Alexander Musmana5f070a2014-10-01 06:03:56 +00003934 assert((CurContext->isDependentContext() || B.builtAll()) &&
3935 "omp parallel for loop exprs were not built");
3936
Alexey Bataev54acd402015-08-04 11:18:19 +00003937 if (!CurContext->isDependentContext()) {
3938 // Finalize the clauses that need pre-built expressions for CodeGen.
3939 for (auto C : Clauses) {
3940 if (auto LC = dyn_cast<OMPLinearClause>(C))
3941 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3942 B.NumIterations, *this, CurScope))
3943 return StmtError();
3944 }
3945 }
3946
Alexey Bataev4acb8592014-07-07 13:01:15 +00003947 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003948 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
3949 NestedLoopCount, Clauses, AStmt, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003950}
3951
Alexander Musmane4e893b2014-09-23 09:33:00 +00003952StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
3953 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3954 SourceLocation EndLoc,
3955 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003956 if (!AStmt)
3957 return StmtError();
3958
Alexander Musmane4e893b2014-09-23 09:33:00 +00003959 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3960 // 1.2.2 OpenMP Language Terminology
3961 // Structured block - An executable statement with a single entry at the
3962 // top and a single exit at the bottom.
3963 // The point of exit cannot be a branch out of the structured block.
3964 // longjmp() and throw() must not violate the entry/exit criteria.
3965 CS->getCapturedDecl()->setNothrow();
3966
Alexander Musmanc6388682014-12-15 07:07:06 +00003967 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003968 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3969 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00003970 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00003971 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
3972 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
3973 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003974 if (NestedLoopCount == 0)
3975 return StmtError();
3976
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00003977 if (!CurContext->isDependentContext()) {
3978 // Finalize the clauses that need pre-built expressions for CodeGen.
3979 for (auto C : Clauses) {
3980 if (auto LC = dyn_cast<OMPLinearClause>(C))
3981 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3982 B.NumIterations, *this, CurScope))
3983 return StmtError();
3984 }
3985 }
3986
Alexey Bataev66b15b52015-08-21 11:14:16 +00003987 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
3988 // If both simdlen and safelen clauses are specified, the value of the simdlen
3989 // parameter must be less than or equal to the value of the safelen parameter.
3990 OMPSafelenClause *Safelen = nullptr;
3991 OMPSimdlenClause *Simdlen = nullptr;
3992 for (auto *Clause : Clauses) {
3993 if (Clause->getClauseKind() == OMPC_safelen)
3994 Safelen = cast<OMPSafelenClause>(Clause);
3995 else if (Clause->getClauseKind() == OMPC_simdlen)
3996 Simdlen = cast<OMPSimdlenClause>(Clause);
3997 if (Safelen && Simdlen)
3998 break;
3999 }
4000 if (Simdlen && Safelen &&
4001 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4002 Safelen->getSafelen()))
4003 return StmtError();
4004
Alexander Musmane4e893b2014-09-23 09:33:00 +00004005 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004006 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00004007 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004008}
4009
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004010StmtResult
4011Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
4012 Stmt *AStmt, SourceLocation StartLoc,
4013 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004014 if (!AStmt)
4015 return StmtError();
4016
4017 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004018 auto BaseStmt = AStmt;
4019 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4020 BaseStmt = CS->getCapturedStmt();
4021 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4022 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004023 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004024 return StmtError();
4025 // All associated statements must be '#pragma omp section' except for
4026 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004027 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004028 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4029 if (SectionStmt)
4030 Diag(SectionStmt->getLocStart(),
4031 diag::err_omp_parallel_sections_substmt_not_section);
4032 return StmtError();
4033 }
4034 }
4035 } else {
4036 Diag(AStmt->getLocStart(),
4037 diag::err_omp_parallel_sections_not_compound_stmt);
4038 return StmtError();
4039 }
4040
4041 getCurFunction()->setHasBranchProtectedScope();
4042
4043 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
4044 Clauses, AStmt);
4045}
4046
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004047StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
4048 Stmt *AStmt, SourceLocation StartLoc,
4049 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004050 if (!AStmt)
4051 return StmtError();
4052
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004053 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4054 // 1.2.2 OpenMP Language Terminology
4055 // Structured block - An executable statement with a single entry at the
4056 // top and a single exit at the bottom.
4057 // The point of exit cannot be a branch out of the structured block.
4058 // longjmp() and throw() must not violate the entry/exit criteria.
4059 CS->getCapturedDecl()->setNothrow();
4060
4061 getCurFunction()->setHasBranchProtectedScope();
4062
4063 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4064}
4065
Alexey Bataev68446b72014-07-18 07:47:19 +00004066StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
4067 SourceLocation EndLoc) {
4068 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
4069}
4070
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00004071StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
4072 SourceLocation EndLoc) {
4073 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
4074}
4075
Alexey Bataev2df347a2014-07-18 10:17:07 +00004076StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
4077 SourceLocation EndLoc) {
4078 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
4079}
4080
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004081StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
4082 SourceLocation StartLoc,
4083 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004084 if (!AStmt)
4085 return StmtError();
4086
4087 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004088
4089 getCurFunction()->setHasBranchProtectedScope();
4090
4091 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
4092}
4093
Alexey Bataev6125da92014-07-21 11:26:11 +00004094StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
4095 SourceLocation StartLoc,
4096 SourceLocation EndLoc) {
4097 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
4098 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
4099}
4100
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004101StmtResult Sema::ActOnOpenMPOrderedDirective(Stmt *AStmt,
4102 SourceLocation StartLoc,
4103 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004104 if (!AStmt)
4105 return StmtError();
4106
4107 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004108
4109 getCurFunction()->setHasBranchProtectedScope();
4110
4111 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, AStmt);
4112}
4113
Alexey Bataev1d160b12015-03-13 12:27:31 +00004114namespace {
4115/// \brief Helper class for checking expression in 'omp atomic [update]'
4116/// construct.
4117class OpenMPAtomicUpdateChecker {
4118 /// \brief Error results for atomic update expressions.
4119 enum ExprAnalysisErrorCode {
4120 /// \brief A statement is not an expression statement.
4121 NotAnExpression,
4122 /// \brief Expression is not builtin binary or unary operation.
4123 NotABinaryOrUnaryExpression,
4124 /// \brief Unary operation is not post-/pre- increment/decrement operation.
4125 NotAnUnaryIncDecExpression,
4126 /// \brief An expression is not of scalar type.
4127 NotAScalarType,
4128 /// \brief A binary operation is not an assignment operation.
4129 NotAnAssignmentOp,
4130 /// \brief RHS part of the binary operation is not a binary expression.
4131 NotABinaryExpression,
4132 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
4133 /// expression.
4134 NotABinaryOperator,
4135 /// \brief RHS binary operation does not have reference to the updated LHS
4136 /// part.
4137 NotAnUpdateExpression,
4138 /// \brief No errors is found.
4139 NoError
4140 };
4141 /// \brief Reference to Sema.
4142 Sema &SemaRef;
4143 /// \brief A location for note diagnostics (when error is found).
4144 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004145 /// \brief 'x' lvalue part of the source atomic expression.
4146 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004147 /// \brief 'expr' rvalue part of the source atomic expression.
4148 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004149 /// \brief Helper expression of the form
4150 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4151 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4152 Expr *UpdateExpr;
4153 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
4154 /// important for non-associative operations.
4155 bool IsXLHSInRHSPart;
4156 BinaryOperatorKind Op;
4157 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004158 /// \brief true if the source expression is a postfix unary operation, false
4159 /// if it is a prefix unary operation.
4160 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004161
4162public:
4163 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00004164 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00004165 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00004166 /// \brief Check specified statement that it is suitable for 'atomic update'
4167 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00004168 /// expression. If DiagId and NoteId == 0, then only check is performed
4169 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00004170 /// \param DiagId Diagnostic which should be emitted if error is found.
4171 /// \param NoteId Diagnostic note for the main error message.
4172 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00004173 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004174 /// \brief Return the 'x' lvalue part of the source atomic expression.
4175 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00004176 /// \brief Return the 'expr' rvalue part of the source atomic expression.
4177 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00004178 /// \brief Return the update expression used in calculation of the updated
4179 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4180 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4181 Expr *getUpdateExpr() const { return UpdateExpr; }
4182 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
4183 /// false otherwise.
4184 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
4185
Alexey Bataevb78ca832015-04-01 03:33:17 +00004186 /// \brief true if the source expression is a postfix unary operation, false
4187 /// if it is a prefix unary operation.
4188 bool isPostfixUpdate() const { return IsPostfixUpdate; }
4189
Alexey Bataev1d160b12015-03-13 12:27:31 +00004190private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00004191 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
4192 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004193};
4194} // namespace
4195
4196bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
4197 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
4198 ExprAnalysisErrorCode ErrorFound = NoError;
4199 SourceLocation ErrorLoc, NoteLoc;
4200 SourceRange ErrorRange, NoteRange;
4201 // Allowed constructs are:
4202 // x = x binop expr;
4203 // x = expr binop x;
4204 if (AtomicBinOp->getOpcode() == BO_Assign) {
4205 X = AtomicBinOp->getLHS();
4206 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
4207 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
4208 if (AtomicInnerBinOp->isMultiplicativeOp() ||
4209 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
4210 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004211 Op = AtomicInnerBinOp->getOpcode();
4212 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004213 auto *LHS = AtomicInnerBinOp->getLHS();
4214 auto *RHS = AtomicInnerBinOp->getRHS();
4215 llvm::FoldingSetNodeID XId, LHSId, RHSId;
4216 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
4217 /*Canonical=*/true);
4218 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
4219 /*Canonical=*/true);
4220 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
4221 /*Canonical=*/true);
4222 if (XId == LHSId) {
4223 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004224 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004225 } else if (XId == RHSId) {
4226 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004227 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004228 } else {
4229 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4230 ErrorRange = AtomicInnerBinOp->getSourceRange();
4231 NoteLoc = X->getExprLoc();
4232 NoteRange = X->getSourceRange();
4233 ErrorFound = NotAnUpdateExpression;
4234 }
4235 } else {
4236 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4237 ErrorRange = AtomicInnerBinOp->getSourceRange();
4238 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
4239 NoteRange = SourceRange(NoteLoc, NoteLoc);
4240 ErrorFound = NotABinaryOperator;
4241 }
4242 } else {
4243 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
4244 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
4245 ErrorFound = NotABinaryExpression;
4246 }
4247 } else {
4248 ErrorLoc = AtomicBinOp->getExprLoc();
4249 ErrorRange = AtomicBinOp->getSourceRange();
4250 NoteLoc = AtomicBinOp->getOperatorLoc();
4251 NoteRange = SourceRange(NoteLoc, NoteLoc);
4252 ErrorFound = NotAnAssignmentOp;
4253 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004254 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004255 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
4256 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
4257 return true;
4258 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00004259 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00004260 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004261}
4262
4263bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
4264 unsigned NoteId) {
4265 ExprAnalysisErrorCode ErrorFound = NoError;
4266 SourceLocation ErrorLoc, NoteLoc;
4267 SourceRange ErrorRange, NoteRange;
4268 // Allowed constructs are:
4269 // x++;
4270 // x--;
4271 // ++x;
4272 // --x;
4273 // x binop= expr;
4274 // x = x binop expr;
4275 // x = expr binop x;
4276 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
4277 AtomicBody = AtomicBody->IgnoreParenImpCasts();
4278 if (AtomicBody->getType()->isScalarType() ||
4279 AtomicBody->isInstantiationDependent()) {
4280 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
4281 AtomicBody->IgnoreParenImpCasts())) {
4282 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00004283 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00004284 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00004285 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004286 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004287 X = AtomicCompAssignOp->getLHS();
4288 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004289 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
4290 AtomicBody->IgnoreParenImpCasts())) {
4291 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00004292 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
4293 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004294 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00004295 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
4296 // Check for Unary Operation
4297 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004298 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004299 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
4300 OpLoc = AtomicUnaryOp->getOperatorLoc();
4301 X = AtomicUnaryOp->getSubExpr();
4302 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
4303 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004304 } else {
4305 ErrorFound = NotAnUnaryIncDecExpression;
4306 ErrorLoc = AtomicUnaryOp->getExprLoc();
4307 ErrorRange = AtomicUnaryOp->getSourceRange();
4308 NoteLoc = AtomicUnaryOp->getOperatorLoc();
4309 NoteRange = SourceRange(NoteLoc, NoteLoc);
4310 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004311 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004312 ErrorFound = NotABinaryOrUnaryExpression;
4313 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
4314 NoteRange = ErrorRange = AtomicBody->getSourceRange();
4315 }
4316 } else {
4317 ErrorFound = NotAScalarType;
4318 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
4319 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4320 }
4321 } else {
4322 ErrorFound = NotAnExpression;
4323 NoteLoc = ErrorLoc = S->getLocStart();
4324 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4325 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004326 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004327 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
4328 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
4329 return true;
4330 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00004331 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00004332 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004333 // Build an update expression of form 'OpaqueValueExpr(x) binop
4334 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
4335 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
4336 auto *OVEX = new (SemaRef.getASTContext())
4337 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
4338 auto *OVEExpr = new (SemaRef.getASTContext())
4339 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
4340 auto Update =
4341 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
4342 IsXLHSInRHSPart ? OVEExpr : OVEX);
4343 if (Update.isInvalid())
4344 return true;
4345 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
4346 Sema::AA_Casting);
4347 if (Update.isInvalid())
4348 return true;
4349 UpdateExpr = Update.get();
4350 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00004351 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004352}
4353
Alexey Bataev0162e452014-07-22 10:10:35 +00004354StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
4355 Stmt *AStmt,
4356 SourceLocation StartLoc,
4357 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004358 if (!AStmt)
4359 return StmtError();
4360
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004361 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00004362 // 1.2.2 OpenMP Language Terminology
4363 // Structured block - An executable statement with a single entry at the
4364 // top and a single exit at the bottom.
4365 // The point of exit cannot be a branch out of the structured block.
4366 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00004367 OpenMPClauseKind AtomicKind = OMPC_unknown;
4368 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004369 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00004370 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00004371 C->getClauseKind() == OMPC_update ||
4372 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00004373 if (AtomicKind != OMPC_unknown) {
4374 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
4375 << SourceRange(C->getLocStart(), C->getLocEnd());
4376 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
4377 << getOpenMPClauseName(AtomicKind);
4378 } else {
4379 AtomicKind = C->getClauseKind();
4380 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004381 }
4382 }
4383 }
Alexey Bataev62cec442014-11-18 10:14:22 +00004384
Alexey Bataev459dec02014-07-24 06:46:57 +00004385 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00004386 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
4387 Body = EWC->getSubExpr();
4388
Alexey Bataev62cec442014-11-18 10:14:22 +00004389 Expr *X = nullptr;
4390 Expr *V = nullptr;
4391 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004392 Expr *UE = nullptr;
4393 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004394 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00004395 // OpenMP [2.12.6, atomic Construct]
4396 // In the next expressions:
4397 // * x and v (as applicable) are both l-value expressions with scalar type.
4398 // * During the execution of an atomic region, multiple syntactic
4399 // occurrences of x must designate the same storage location.
4400 // * Neither of v and expr (as applicable) may access the storage location
4401 // designated by x.
4402 // * Neither of x and expr (as applicable) may access the storage location
4403 // designated by v.
4404 // * expr is an expression with scalar type.
4405 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
4406 // * binop, binop=, ++, and -- are not overloaded operators.
4407 // * The expression x binop expr must be numerically equivalent to x binop
4408 // (expr). This requirement is satisfied if the operators in expr have
4409 // precedence greater than binop, or by using parentheses around expr or
4410 // subexpressions of expr.
4411 // * The expression expr binop x must be numerically equivalent to (expr)
4412 // binop x. This requirement is satisfied if the operators in expr have
4413 // precedence equal to or greater than binop, or by using parentheses around
4414 // expr or subexpressions of expr.
4415 // * For forms that allow multiple occurrences of x, the number of times
4416 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00004417 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004418 enum {
4419 NotAnExpression,
4420 NotAnAssignmentOp,
4421 NotAScalarType,
4422 NotAnLValue,
4423 NoError
4424 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00004425 SourceLocation ErrorLoc, NoteLoc;
4426 SourceRange ErrorRange, NoteRange;
4427 // If clause is read:
4428 // v = x;
4429 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
4430 auto AtomicBinOp =
4431 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4432 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4433 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
4434 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
4435 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
4436 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
4437 if (!X->isLValue() || !V->isLValue()) {
4438 auto NotLValueExpr = X->isLValue() ? V : X;
4439 ErrorFound = NotAnLValue;
4440 ErrorLoc = AtomicBinOp->getExprLoc();
4441 ErrorRange = AtomicBinOp->getSourceRange();
4442 NoteLoc = NotLValueExpr->getExprLoc();
4443 NoteRange = NotLValueExpr->getSourceRange();
4444 }
4445 } else if (!X->isInstantiationDependent() ||
4446 !V->isInstantiationDependent()) {
4447 auto NotScalarExpr =
4448 (X->isInstantiationDependent() || X->getType()->isScalarType())
4449 ? V
4450 : X;
4451 ErrorFound = NotAScalarType;
4452 ErrorLoc = AtomicBinOp->getExprLoc();
4453 ErrorRange = AtomicBinOp->getSourceRange();
4454 NoteLoc = NotScalarExpr->getExprLoc();
4455 NoteRange = NotScalarExpr->getSourceRange();
4456 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004457 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00004458 ErrorFound = NotAnAssignmentOp;
4459 ErrorLoc = AtomicBody->getExprLoc();
4460 ErrorRange = AtomicBody->getSourceRange();
4461 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4462 : AtomicBody->getExprLoc();
4463 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4464 : AtomicBody->getSourceRange();
4465 }
4466 } else {
4467 ErrorFound = NotAnExpression;
4468 NoteLoc = ErrorLoc = Body->getLocStart();
4469 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00004470 }
Alexey Bataev62cec442014-11-18 10:14:22 +00004471 if (ErrorFound != NoError) {
4472 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
4473 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00004474 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4475 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00004476 return StmtError();
4477 } else if (CurContext->isDependentContext())
4478 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00004479 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004480 enum {
4481 NotAnExpression,
4482 NotAnAssignmentOp,
4483 NotAScalarType,
4484 NotAnLValue,
4485 NoError
4486 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00004487 SourceLocation ErrorLoc, NoteLoc;
4488 SourceRange ErrorRange, NoteRange;
4489 // If clause is write:
4490 // x = expr;
4491 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
4492 auto AtomicBinOp =
4493 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4494 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00004495 X = AtomicBinOp->getLHS();
4496 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00004497 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
4498 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
4499 if (!X->isLValue()) {
4500 ErrorFound = NotAnLValue;
4501 ErrorLoc = AtomicBinOp->getExprLoc();
4502 ErrorRange = AtomicBinOp->getSourceRange();
4503 NoteLoc = X->getExprLoc();
4504 NoteRange = X->getSourceRange();
4505 }
4506 } else if (!X->isInstantiationDependent() ||
4507 !E->isInstantiationDependent()) {
4508 auto NotScalarExpr =
4509 (X->isInstantiationDependent() || X->getType()->isScalarType())
4510 ? E
4511 : X;
4512 ErrorFound = NotAScalarType;
4513 ErrorLoc = AtomicBinOp->getExprLoc();
4514 ErrorRange = AtomicBinOp->getSourceRange();
4515 NoteLoc = NotScalarExpr->getExprLoc();
4516 NoteRange = NotScalarExpr->getSourceRange();
4517 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004518 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00004519 ErrorFound = NotAnAssignmentOp;
4520 ErrorLoc = AtomicBody->getExprLoc();
4521 ErrorRange = AtomicBody->getSourceRange();
4522 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4523 : AtomicBody->getExprLoc();
4524 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4525 : AtomicBody->getSourceRange();
4526 }
4527 } else {
4528 ErrorFound = NotAnExpression;
4529 NoteLoc = ErrorLoc = Body->getLocStart();
4530 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00004531 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00004532 if (ErrorFound != NoError) {
4533 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
4534 << ErrorRange;
4535 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4536 << NoteRange;
4537 return StmtError();
4538 } else if (CurContext->isDependentContext())
4539 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00004540 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004541 // If clause is update:
4542 // x++;
4543 // x--;
4544 // ++x;
4545 // --x;
4546 // x binop= expr;
4547 // x = x binop expr;
4548 // x = expr binop x;
4549 OpenMPAtomicUpdateChecker Checker(*this);
4550 if (Checker.checkStatement(
4551 Body, (AtomicKind == OMPC_update)
4552 ? diag::err_omp_atomic_update_not_expression_statement
4553 : diag::err_omp_atomic_not_expression_statement,
4554 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00004555 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004556 if (!CurContext->isDependentContext()) {
4557 E = Checker.getExpr();
4558 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004559 UE = Checker.getUpdateExpr();
4560 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00004561 }
Alexey Bataev459dec02014-07-24 06:46:57 +00004562 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004563 enum {
4564 NotAnAssignmentOp,
4565 NotACompoundStatement,
4566 NotTwoSubstatements,
4567 NotASpecificExpression,
4568 NoError
4569 } ErrorFound = NoError;
4570 SourceLocation ErrorLoc, NoteLoc;
4571 SourceRange ErrorRange, NoteRange;
4572 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
4573 // If clause is a capture:
4574 // v = x++;
4575 // v = x--;
4576 // v = ++x;
4577 // v = --x;
4578 // v = x binop= expr;
4579 // v = x = x binop expr;
4580 // v = x = expr binop x;
4581 auto *AtomicBinOp =
4582 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4583 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4584 V = AtomicBinOp->getLHS();
4585 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
4586 OpenMPAtomicUpdateChecker Checker(*this);
4587 if (Checker.checkStatement(
4588 Body, diag::err_omp_atomic_capture_not_expression_statement,
4589 diag::note_omp_atomic_update))
4590 return StmtError();
4591 E = Checker.getExpr();
4592 X = Checker.getX();
4593 UE = Checker.getUpdateExpr();
4594 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
4595 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00004596 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004597 ErrorLoc = AtomicBody->getExprLoc();
4598 ErrorRange = AtomicBody->getSourceRange();
4599 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4600 : AtomicBody->getExprLoc();
4601 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4602 : AtomicBody->getSourceRange();
4603 ErrorFound = NotAnAssignmentOp;
4604 }
4605 if (ErrorFound != NoError) {
4606 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
4607 << ErrorRange;
4608 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
4609 return StmtError();
4610 } else if (CurContext->isDependentContext()) {
4611 UE = V = E = X = nullptr;
4612 }
4613 } else {
4614 // If clause is a capture:
4615 // { v = x; x = expr; }
4616 // { v = x; x++; }
4617 // { v = x; x--; }
4618 // { v = x; ++x; }
4619 // { v = x; --x; }
4620 // { v = x; x binop= expr; }
4621 // { v = x; x = x binop expr; }
4622 // { v = x; x = expr binop x; }
4623 // { x++; v = x; }
4624 // { x--; v = x; }
4625 // { ++x; v = x; }
4626 // { --x; v = x; }
4627 // { x binop= expr; v = x; }
4628 // { x = x binop expr; v = x; }
4629 // { x = expr binop x; v = x; }
4630 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
4631 // Check that this is { expr1; expr2; }
4632 if (CS->size() == 2) {
4633 auto *First = CS->body_front();
4634 auto *Second = CS->body_back();
4635 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
4636 First = EWC->getSubExpr()->IgnoreParenImpCasts();
4637 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
4638 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
4639 // Need to find what subexpression is 'v' and what is 'x'.
4640 OpenMPAtomicUpdateChecker Checker(*this);
4641 bool IsUpdateExprFound = !Checker.checkStatement(Second);
4642 BinaryOperator *BinOp = nullptr;
4643 if (IsUpdateExprFound) {
4644 BinOp = dyn_cast<BinaryOperator>(First);
4645 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
4646 }
4647 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
4648 // { v = x; x++; }
4649 // { v = x; x--; }
4650 // { v = x; ++x; }
4651 // { v = x; --x; }
4652 // { v = x; x binop= expr; }
4653 // { v = x; x = x binop expr; }
4654 // { v = x; x = expr binop x; }
4655 // Check that the first expression has form v = x.
4656 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
4657 llvm::FoldingSetNodeID XId, PossibleXId;
4658 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
4659 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
4660 IsUpdateExprFound = XId == PossibleXId;
4661 if (IsUpdateExprFound) {
4662 V = BinOp->getLHS();
4663 X = Checker.getX();
4664 E = Checker.getExpr();
4665 UE = Checker.getUpdateExpr();
4666 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00004667 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004668 }
4669 }
4670 if (!IsUpdateExprFound) {
4671 IsUpdateExprFound = !Checker.checkStatement(First);
4672 BinOp = nullptr;
4673 if (IsUpdateExprFound) {
4674 BinOp = dyn_cast<BinaryOperator>(Second);
4675 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
4676 }
4677 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
4678 // { x++; v = x; }
4679 // { x--; v = x; }
4680 // { ++x; v = x; }
4681 // { --x; v = x; }
4682 // { x binop= expr; v = x; }
4683 // { x = x binop expr; v = x; }
4684 // { x = expr binop x; v = x; }
4685 // Check that the second expression has form v = x.
4686 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
4687 llvm::FoldingSetNodeID XId, PossibleXId;
4688 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
4689 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
4690 IsUpdateExprFound = XId == PossibleXId;
4691 if (IsUpdateExprFound) {
4692 V = BinOp->getLHS();
4693 X = Checker.getX();
4694 E = Checker.getExpr();
4695 UE = Checker.getUpdateExpr();
4696 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00004697 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004698 }
4699 }
4700 }
4701 if (!IsUpdateExprFound) {
4702 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00004703 auto *FirstExpr = dyn_cast<Expr>(First);
4704 auto *SecondExpr = dyn_cast<Expr>(Second);
4705 if (!FirstExpr || !SecondExpr ||
4706 !(FirstExpr->isInstantiationDependent() ||
4707 SecondExpr->isInstantiationDependent())) {
4708 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
4709 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004710 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00004711 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
4712 : First->getLocStart();
4713 NoteRange = ErrorRange = FirstBinOp
4714 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00004715 : SourceRange(ErrorLoc, ErrorLoc);
4716 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00004717 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
4718 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
4719 ErrorFound = NotAnAssignmentOp;
4720 NoteLoc = ErrorLoc = SecondBinOp
4721 ? SecondBinOp->getOperatorLoc()
4722 : Second->getLocStart();
4723 NoteRange = ErrorRange =
4724 SecondBinOp ? SecondBinOp->getSourceRange()
4725 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00004726 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00004727 auto *PossibleXRHSInFirst =
4728 FirstBinOp->getRHS()->IgnoreParenImpCasts();
4729 auto *PossibleXLHSInSecond =
4730 SecondBinOp->getLHS()->IgnoreParenImpCasts();
4731 llvm::FoldingSetNodeID X1Id, X2Id;
4732 PossibleXRHSInFirst->Profile(X1Id, Context,
4733 /*Canonical=*/true);
4734 PossibleXLHSInSecond->Profile(X2Id, Context,
4735 /*Canonical=*/true);
4736 IsUpdateExprFound = X1Id == X2Id;
4737 if (IsUpdateExprFound) {
4738 V = FirstBinOp->getLHS();
4739 X = SecondBinOp->getLHS();
4740 E = SecondBinOp->getRHS();
4741 UE = nullptr;
4742 IsXLHSInRHSPart = false;
4743 IsPostfixUpdate = true;
4744 } else {
4745 ErrorFound = NotASpecificExpression;
4746 ErrorLoc = FirstBinOp->getExprLoc();
4747 ErrorRange = FirstBinOp->getSourceRange();
4748 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
4749 NoteRange = SecondBinOp->getRHS()->getSourceRange();
4750 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004751 }
4752 }
4753 }
4754 }
4755 } else {
4756 NoteLoc = ErrorLoc = Body->getLocStart();
4757 NoteRange = ErrorRange =
4758 SourceRange(Body->getLocStart(), Body->getLocStart());
4759 ErrorFound = NotTwoSubstatements;
4760 }
4761 } else {
4762 NoteLoc = ErrorLoc = Body->getLocStart();
4763 NoteRange = ErrorRange =
4764 SourceRange(Body->getLocStart(), Body->getLocStart());
4765 ErrorFound = NotACompoundStatement;
4766 }
4767 if (ErrorFound != NoError) {
4768 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
4769 << ErrorRange;
4770 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
4771 return StmtError();
4772 } else if (CurContext->isDependentContext()) {
4773 UE = V = E = X = nullptr;
4774 }
Alexey Bataev459dec02014-07-24 06:46:57 +00004775 }
Alexey Bataevdea47612014-07-23 07:46:59 +00004776 }
Alexey Bataev0162e452014-07-22 10:10:35 +00004777
4778 getCurFunction()->setHasBranchProtectedScope();
4779
Alexey Bataev62cec442014-11-18 10:14:22 +00004780 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00004781 X, V, E, UE, IsXLHSInRHSPart,
4782 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00004783}
4784
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004785StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
4786 Stmt *AStmt,
4787 SourceLocation StartLoc,
4788 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004789 if (!AStmt)
4790 return StmtError();
4791
4792 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004793
Alexey Bataev13314bf2014-10-09 04:18:56 +00004794 // OpenMP [2.16, Nesting of Regions]
4795 // If specified, a teams construct must be contained within a target
4796 // construct. That target construct must contain no statements or directives
4797 // outside of the teams construct.
4798 if (DSAStack->hasInnerTeamsRegion()) {
4799 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
4800 bool OMPTeamsFound = true;
4801 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
4802 auto I = CS->body_begin();
4803 while (I != CS->body_end()) {
4804 auto OED = dyn_cast<OMPExecutableDirective>(*I);
4805 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
4806 OMPTeamsFound = false;
4807 break;
4808 }
4809 ++I;
4810 }
4811 assert(I != CS->body_end() && "Not found statement");
4812 S = *I;
4813 }
4814 if (!OMPTeamsFound) {
4815 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
4816 Diag(DSAStack->getInnerTeamsRegionLoc(),
4817 diag::note_omp_nested_teams_construct_here);
4818 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
4819 << isa<OMPExecutableDirective>(S);
4820 return StmtError();
4821 }
4822 }
4823
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004824 getCurFunction()->setHasBranchProtectedScope();
4825
4826 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4827}
4828
Michael Wong65f367f2015-07-21 13:44:28 +00004829StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
4830 Stmt *AStmt,
4831 SourceLocation StartLoc,
4832 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004833 if (!AStmt)
4834 return StmtError();
4835
4836 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
4837
Michael Wong65f367f2015-07-21 13:44:28 +00004838 getCurFunction()->setHasBranchProtectedScope();
4839
4840 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
4841 AStmt);
4842}
4843
Alexey Bataev13314bf2014-10-09 04:18:56 +00004844StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
4845 Stmt *AStmt, SourceLocation StartLoc,
4846 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004847 if (!AStmt)
4848 return StmtError();
4849
Alexey Bataev13314bf2014-10-09 04:18:56 +00004850 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4851 // 1.2.2 OpenMP Language Terminology
4852 // Structured block - An executable statement with a single entry at the
4853 // top and a single exit at the bottom.
4854 // The point of exit cannot be a branch out of the structured block.
4855 // longjmp() and throw() must not violate the entry/exit criteria.
4856 CS->getCapturedDecl()->setNothrow();
4857
4858 getCurFunction()->setHasBranchProtectedScope();
4859
4860 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4861}
4862
Alexey Bataev6d4ed052015-07-01 06:57:41 +00004863StmtResult
4864Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
4865 SourceLocation EndLoc,
4866 OpenMPDirectiveKind CancelRegion) {
4867 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
4868 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
4869 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
4870 << getOpenMPDirectiveName(CancelRegion);
4871 return StmtError();
4872 }
4873 if (DSAStack->isParentNowaitRegion()) {
4874 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
4875 return StmtError();
4876 }
4877 if (DSAStack->isParentOrderedRegion()) {
4878 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
4879 return StmtError();
4880 }
4881 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
4882 CancelRegion);
4883}
4884
Alexey Bataev80909872015-07-02 11:25:17 +00004885StmtResult Sema::ActOnOpenMPCancelDirective(SourceLocation StartLoc,
4886 SourceLocation EndLoc,
4887 OpenMPDirectiveKind CancelRegion) {
4888 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
4889 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
4890 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
4891 << getOpenMPDirectiveName(CancelRegion);
4892 return StmtError();
4893 }
4894 if (DSAStack->isParentNowaitRegion()) {
4895 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
4896 return StmtError();
4897 }
4898 if (DSAStack->isParentOrderedRegion()) {
4899 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
4900 return StmtError();
4901 }
4902 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, CancelRegion);
4903}
4904
Alexey Bataeved09d242014-05-28 05:53:51 +00004905OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004906 SourceLocation StartLoc,
4907 SourceLocation LParenLoc,
4908 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004909 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004910 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00004911 case OMPC_final:
4912 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
4913 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00004914 case OMPC_num_threads:
4915 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
4916 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00004917 case OMPC_safelen:
4918 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
4919 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00004920 case OMPC_simdlen:
4921 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
4922 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00004923 case OMPC_collapse:
4924 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
4925 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004926 case OMPC_ordered:
4927 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
4928 break;
Michael Wonge710d542015-08-07 16:16:36 +00004929 case OMPC_device:
4930 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
4931 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004932 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004933 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004934 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004935 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004936 case OMPC_private:
4937 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00004938 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004939 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00004940 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00004941 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004942 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004943 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004944 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00004945 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004946 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004947 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004948 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004949 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004950 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004951 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004952 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004953 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004954 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004955 case OMPC_depend:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004956 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004957 llvm_unreachable("Clause is not allowed.");
4958 }
4959 return Res;
4960}
4961
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004962OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
4963 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004964 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004965 SourceLocation NameModifierLoc,
4966 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004967 SourceLocation EndLoc) {
4968 Expr *ValExpr = Condition;
4969 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
4970 !Condition->isInstantiationDependent() &&
4971 !Condition->containsUnexpandedParameterPack()) {
4972 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00004973 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004974 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004975 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004976
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004977 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004978 }
4979
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004980 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
4981 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004982}
4983
Alexey Bataev3778b602014-07-17 07:32:53 +00004984OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
4985 SourceLocation StartLoc,
4986 SourceLocation LParenLoc,
4987 SourceLocation EndLoc) {
4988 Expr *ValExpr = Condition;
4989 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
4990 !Condition->isInstantiationDependent() &&
4991 !Condition->containsUnexpandedParameterPack()) {
4992 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
4993 Condition->getExprLoc(), Condition);
4994 if (Val.isInvalid())
4995 return nullptr;
4996
4997 ValExpr = Val.get();
4998 }
4999
5000 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
5001}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005002ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
5003 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00005004 if (!Op)
5005 return ExprError();
5006
5007 class IntConvertDiagnoser : public ICEConvertDiagnoser {
5008 public:
5009 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00005010 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00005011 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
5012 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005013 return S.Diag(Loc, diag::err_omp_not_integral) << T;
5014 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005015 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
5016 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005017 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
5018 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005019 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
5020 QualType T,
5021 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005022 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
5023 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005024 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
5025 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005026 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005027 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005028 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005029 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
5030 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005031 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
5032 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005033 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
5034 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005035 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005036 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005037 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005038 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
5039 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005040 llvm_unreachable("conversion functions are permitted");
5041 }
5042 } ConvertDiagnoser;
5043 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
5044}
5045
5046OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
5047 SourceLocation StartLoc,
5048 SourceLocation LParenLoc,
5049 SourceLocation EndLoc) {
5050 Expr *ValExpr = NumThreads;
5051 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
Alexey Bataev568a8332014-03-06 06:15:19 +00005052 !NumThreads->containsUnexpandedParameterPack()) {
5053 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
5054 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005055 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00005056 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005057 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00005058
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005059 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00005060
5061 // OpenMP [2.5, Restrictions]
5062 // The num_threads expression must evaluate to a positive integer value.
5063 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00005064 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
5065 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00005066 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
5067 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005068 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00005069 }
5070 }
5071
Alexey Bataeved09d242014-05-28 05:53:51 +00005072 return new (Context)
5073 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00005074}
5075
Alexey Bataev62c87d22014-03-21 04:51:18 +00005076ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
5077 OpenMPClauseKind CKind) {
5078 if (!E)
5079 return ExprError();
5080 if (E->isValueDependent() || E->isTypeDependent() ||
5081 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00005082 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005083 llvm::APSInt Result;
5084 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
5085 if (ICE.isInvalid())
5086 return ExprError();
5087 if (!Result.isStrictlyPositive()) {
5088 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
5089 << getOpenMPClauseName(CKind) << E->getSourceRange();
5090 return ExprError();
5091 }
Alexander Musman09184fe2014-09-30 05:29:28 +00005092 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
5093 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
5094 << E->getSourceRange();
5095 return ExprError();
5096 }
Alexey Bataev9c821032015-04-30 04:23:23 +00005097 if (CKind == OMPC_collapse) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00005098 DSAStack->setCollapseNumber(DSAStack->getCollapseNumber() - 1 +
5099 Result.getExtValue());
5100 } else if (CKind == OMPC_ordered) {
5101 DSAStack->setCollapseNumber(DSAStack->getCollapseNumber() - 1 +
5102 Result.getExtValue());
Alexey Bataev9c821032015-04-30 04:23:23 +00005103 }
Alexey Bataev62c87d22014-03-21 04:51:18 +00005104 return ICE;
5105}
5106
5107OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
5108 SourceLocation LParenLoc,
5109 SourceLocation EndLoc) {
5110 // OpenMP [2.8.1, simd construct, Description]
5111 // The parameter of the safelen clause must be a constant
5112 // positive integer expression.
5113 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
5114 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005115 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005116 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005117 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00005118}
5119
Alexey Bataev66b15b52015-08-21 11:14:16 +00005120OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
5121 SourceLocation LParenLoc,
5122 SourceLocation EndLoc) {
5123 // OpenMP [2.8.1, simd construct, Description]
5124 // The parameter of the simdlen clause must be a constant
5125 // positive integer expression.
5126 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
5127 if (Simdlen.isInvalid())
5128 return nullptr;
5129 return new (Context)
5130 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
5131}
5132
Alexander Musman64d33f12014-06-04 07:53:32 +00005133OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
5134 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00005135 SourceLocation LParenLoc,
5136 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00005137 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00005138 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00005139 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00005140 // The parameter of the collapse clause must be a constant
5141 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00005142 ExprResult NumForLoopsResult =
5143 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
5144 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00005145 return nullptr;
5146 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00005147 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00005148}
5149
Alexey Bataev10e775f2015-07-30 11:36:16 +00005150OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
5151 SourceLocation EndLoc,
5152 SourceLocation LParenLoc,
5153 Expr *NumForLoops) {
5154 DSAStack->setOrderedRegion();
5155 // OpenMP [2.7.1, loop construct, Description]
5156 // OpenMP [2.8.1, simd construct, Description]
5157 // OpenMP [2.9.6, distribute construct, Description]
5158 // The parameter of the ordered clause must be a constant
5159 // positive integer expression if any.
5160 if (NumForLoops && LParenLoc.isValid()) {
5161 ExprResult NumForLoopsResult =
5162 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
5163 if (NumForLoopsResult.isInvalid())
5164 return nullptr;
5165 NumForLoops = NumForLoopsResult.get();
5166 }
5167 return new (Context)
5168 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
5169}
5170
Alexey Bataeved09d242014-05-28 05:53:51 +00005171OMPClause *Sema::ActOnOpenMPSimpleClause(
5172 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
5173 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005174 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005175 switch (Kind) {
5176 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00005177 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00005178 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
5179 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005180 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005181 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00005182 Res = ActOnOpenMPProcBindClause(
5183 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
5184 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005185 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005186 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005187 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00005188 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00005189 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005190 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00005191 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005192 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005193 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005194 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00005195 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00005196 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005197 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00005198 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005199 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005200 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005201 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005202 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005203 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005204 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005205 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005206 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005207 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005208 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005209 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005210 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005211 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005212 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005213 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005214 case OMPC_device:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005215 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005216 llvm_unreachable("Clause is not allowed.");
5217 }
5218 return Res;
5219}
5220
5221OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
5222 SourceLocation KindKwLoc,
5223 SourceLocation StartLoc,
5224 SourceLocation LParenLoc,
5225 SourceLocation EndLoc) {
5226 if (Kind == OMPC_DEFAULT_unknown) {
5227 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005228 static_assert(OMPC_DEFAULT_unknown > 0,
5229 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00005230 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005231 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005232 Values += "'";
5233 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
5234 Values += "'";
5235 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005236 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005237 Values += " or ";
5238 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005239 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005240 break;
5241 default:
5242 Values += Sep;
5243 break;
5244 }
5245 }
5246 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00005247 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005248 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005249 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00005250 switch (Kind) {
5251 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005252 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005253 break;
5254 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005255 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005256 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005257 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005258 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00005259 break;
5260 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005261 return new (Context)
5262 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005263}
5264
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005265OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
5266 SourceLocation KindKwLoc,
5267 SourceLocation StartLoc,
5268 SourceLocation LParenLoc,
5269 SourceLocation EndLoc) {
5270 if (Kind == OMPC_PROC_BIND_unknown) {
5271 std::string Values;
5272 std::string Sep(", ");
5273 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
5274 Values += "'";
5275 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
5276 Values += "'";
5277 switch (i) {
5278 case OMPC_PROC_BIND_unknown - 2:
5279 Values += " or ";
5280 break;
5281 case OMPC_PROC_BIND_unknown - 1:
5282 break;
5283 default:
5284 Values += Sep;
5285 break;
5286 }
5287 }
5288 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00005289 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005290 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005291 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005292 return new (Context)
5293 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005294}
5295
Alexey Bataev56dafe82014-06-20 07:16:17 +00005296OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
5297 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
5298 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005299 SourceLocation ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00005300 SourceLocation EndLoc) {
5301 OMPClause *Res = nullptr;
5302 switch (Kind) {
5303 case OMPC_schedule:
5304 Res = ActOnOpenMPScheduleClause(
5305 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005306 LParenLoc, ArgumentLoc, DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00005307 break;
5308 case OMPC_if:
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005309 Res =
5310 ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument), Expr,
5311 StartLoc, LParenLoc, ArgumentLoc, DelimLoc, EndLoc);
5312 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00005313 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005314 case OMPC_num_threads:
5315 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005316 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005317 case OMPC_collapse:
5318 case OMPC_default:
5319 case OMPC_proc_bind:
5320 case OMPC_private:
5321 case OMPC_firstprivate:
5322 case OMPC_lastprivate:
5323 case OMPC_shared:
5324 case OMPC_reduction:
5325 case OMPC_linear:
5326 case OMPC_aligned:
5327 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005328 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005329 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005330 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005331 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005332 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005333 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005334 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005335 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005336 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005337 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005338 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005339 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005340 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005341 case OMPC_device:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005342 case OMPC_unknown:
5343 llvm_unreachable("Clause is not allowed.");
5344 }
5345 return Res;
5346}
5347
5348OMPClause *Sema::ActOnOpenMPScheduleClause(
5349 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
5350 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
5351 SourceLocation EndLoc) {
5352 if (Kind == OMPC_SCHEDULE_unknown) {
5353 std::string Values;
5354 std::string Sep(", ");
5355 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
5356 Values += "'";
5357 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
5358 Values += "'";
5359 switch (i) {
5360 case OMPC_SCHEDULE_unknown - 2:
5361 Values += " or ";
5362 break;
5363 case OMPC_SCHEDULE_unknown - 1:
5364 break;
5365 default:
5366 Values += Sep;
5367 break;
5368 }
5369 }
5370 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
5371 << Values << getOpenMPClauseName(OMPC_schedule);
5372 return nullptr;
5373 }
5374 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00005375 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00005376 if (ChunkSize) {
5377 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
5378 !ChunkSize->isInstantiationDependent() &&
5379 !ChunkSize->containsUnexpandedParameterPack()) {
5380 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
5381 ExprResult Val =
5382 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
5383 if (Val.isInvalid())
5384 return nullptr;
5385
5386 ValExpr = Val.get();
5387
5388 // OpenMP [2.7.1, Restrictions]
5389 // chunk_size must be a loop invariant integer expression with a positive
5390 // value.
5391 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00005392 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
5393 if (Result.isSigned() && !Result.isStrictlyPositive()) {
5394 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
5395 << "schedule" << ChunkSize->getSourceRange();
5396 return nullptr;
5397 }
5398 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
5399 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
5400 ChunkSize->getType(), ".chunk.");
5401 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
5402 ChunkSize->getExprLoc(),
5403 /*RefersToCapture=*/true);
5404 HelperValExpr = ImpVarRef;
Alexey Bataev56dafe82014-06-20 07:16:17 +00005405 }
5406 }
5407 }
5408
5409 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
Alexey Bataev040d5402015-05-12 08:35:28 +00005410 EndLoc, Kind, ValExpr, HelperValExpr);
Alexey Bataev56dafe82014-06-20 07:16:17 +00005411}
5412
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005413OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
5414 SourceLocation StartLoc,
5415 SourceLocation EndLoc) {
5416 OMPClause *Res = nullptr;
5417 switch (Kind) {
5418 case OMPC_ordered:
5419 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
5420 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00005421 case OMPC_nowait:
5422 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
5423 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005424 case OMPC_untied:
5425 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
5426 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005427 case OMPC_mergeable:
5428 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
5429 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005430 case OMPC_read:
5431 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
5432 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00005433 case OMPC_write:
5434 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
5435 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005436 case OMPC_update:
5437 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
5438 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00005439 case OMPC_capture:
5440 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
5441 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005442 case OMPC_seq_cst:
5443 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
5444 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005445 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005446 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005447 case OMPC_num_threads:
5448 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005449 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005450 case OMPC_collapse:
5451 case OMPC_schedule:
5452 case OMPC_private:
5453 case OMPC_firstprivate:
5454 case OMPC_lastprivate:
5455 case OMPC_shared:
5456 case OMPC_reduction:
5457 case OMPC_linear:
5458 case OMPC_aligned:
5459 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005460 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005461 case OMPC_default:
5462 case OMPC_proc_bind:
5463 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005464 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005465 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005466 case OMPC_device:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005467 case OMPC_unknown:
5468 llvm_unreachable("Clause is not allowed.");
5469 }
5470 return Res;
5471}
5472
Alexey Bataev236070f2014-06-20 11:19:47 +00005473OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
5474 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00005475 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00005476 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
5477}
5478
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005479OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
5480 SourceLocation EndLoc) {
5481 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
5482}
5483
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005484OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
5485 SourceLocation EndLoc) {
5486 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
5487}
5488
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005489OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
5490 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005491 return new (Context) OMPReadClause(StartLoc, EndLoc);
5492}
5493
Alexey Bataevdea47612014-07-23 07:46:59 +00005494OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
5495 SourceLocation EndLoc) {
5496 return new (Context) OMPWriteClause(StartLoc, EndLoc);
5497}
5498
Alexey Bataev67a4f222014-07-23 10:25:33 +00005499OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
5500 SourceLocation EndLoc) {
5501 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
5502}
5503
Alexey Bataev459dec02014-07-24 06:46:57 +00005504OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
5505 SourceLocation EndLoc) {
5506 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
5507}
5508
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005509OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
5510 SourceLocation EndLoc) {
5511 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
5512}
5513
Alexey Bataevc5e02582014-06-16 07:08:35 +00005514OMPClause *Sema::ActOnOpenMPVarListClause(
5515 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
5516 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
5517 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005518 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Alexey Bataev182227b2015-08-20 10:54:39 +00005519 OpenMPLinearClauseKind LinKind, SourceLocation DepLinLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005520 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005521 switch (Kind) {
5522 case OMPC_private:
5523 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5524 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005525 case OMPC_firstprivate:
5526 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5527 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00005528 case OMPC_lastprivate:
5529 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5530 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00005531 case OMPC_shared:
5532 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
5533 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005534 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00005535 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
5536 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005537 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00005538 case OMPC_linear:
5539 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Alexey Bataev182227b2015-08-20 10:54:39 +00005540 LinKind, DepLinLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00005541 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005542 case OMPC_aligned:
5543 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
5544 ColonLoc, EndLoc);
5545 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005546 case OMPC_copyin:
5547 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
5548 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00005549 case OMPC_copyprivate:
5550 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5551 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00005552 case OMPC_flush:
5553 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
5554 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005555 case OMPC_depend:
Alexey Bataev182227b2015-08-20 10:54:39 +00005556 Res = ActOnOpenMPDependClause(DepKind, DepLinLoc, ColonLoc, VarList, StartLoc,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005557 LParenLoc, EndLoc);
5558 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005559 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005560 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00005561 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00005562 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005563 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00005564 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005565 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005566 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005567 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005568 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005569 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005570 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005571 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005572 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005573 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005574 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005575 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005576 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005577 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00005578 case OMPC_device:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005579 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005580 llvm_unreachable("Clause is not allowed.");
5581 }
5582 return Res;
5583}
5584
5585OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
5586 SourceLocation StartLoc,
5587 SourceLocation LParenLoc,
5588 SourceLocation EndLoc) {
5589 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00005590 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00005591 for (auto &RefExpr : VarList) {
5592 assert(RefExpr && "NULL expr in OpenMP private clause.");
5593 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005594 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005595 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005596 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005597 continue;
5598 }
5599
Alexey Bataeved09d242014-05-28 05:53:51 +00005600 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005601 // OpenMP [2.1, C/C++]
5602 // A list item is a variable name.
5603 // OpenMP [2.9.3.3, Restrictions, p.1]
5604 // A variable that is part of another variable (as an array or
5605 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00005606 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005607 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005608 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005609 continue;
5610 }
5611 Decl *D = DE->getDecl();
5612 VarDecl *VD = cast<VarDecl>(D);
5613
5614 QualType Type = VD->getType();
5615 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5616 // It will be analyzed later.
5617 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005618 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005619 continue;
5620 }
5621
5622 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5623 // A variable that appears in a private clause must not have an incomplete
5624 // type or a reference type.
5625 if (RequireCompleteType(ELoc, Type,
5626 diag::err_omp_private_incomplete_type)) {
5627 continue;
5628 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00005629 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005630
Alexey Bataev758e55e2013-09-06 18:03:48 +00005631 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5632 // in a Construct]
5633 // Variables with the predetermined data-sharing attributes may not be
5634 // listed in data-sharing attributes clauses, except for the cases
5635 // listed below. For these exceptions only, listing a predetermined
5636 // variable in a data-sharing attribute clause is allowed and overrides
5637 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005638 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005639 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005640 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
5641 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005642 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005643 continue;
5644 }
5645
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005646 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00005647 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005648 DSAStack->getCurrentDirective() == OMPD_task) {
5649 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
5650 << getOpenMPClauseName(OMPC_private) << Type
5651 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
5652 bool IsDecl =
5653 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5654 Diag(VD->getLocation(),
5655 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5656 << VD;
5657 continue;
5658 }
5659
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005660 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
5661 // A variable of class type (or array thereof) that appears in a private
5662 // clause requires an accessible, unambiguous default constructor for the
5663 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00005664 // Generate helper private variable and initialize it with the default
5665 // value. The address of the original variable is replaced by the address of
5666 // the new private variable in CodeGen. This new variable is not added to
5667 // IdResolver, so the code in the OpenMP region uses original variable for
5668 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005669 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00005670 auto VDPrivate = buildVarDecl(*this, DE->getExprLoc(), Type, VD->getName(),
5671 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005672 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005673 if (VDPrivate->isInvalidDecl())
5674 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005675 auto VDPrivateRefExpr = buildDeclRefExpr(
5676 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev03b340a2014-10-21 03:16:40 +00005677
Alexey Bataev758e55e2013-09-06 18:03:48 +00005678 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005679 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005680 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005681 }
5682
Alexey Bataeved09d242014-05-28 05:53:51 +00005683 if (Vars.empty())
5684 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005685
Alexey Bataev03b340a2014-10-21 03:16:40 +00005686 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
5687 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005688}
5689
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005690namespace {
5691class DiagsUninitializedSeveretyRAII {
5692private:
5693 DiagnosticsEngine &Diags;
5694 SourceLocation SavedLoc;
5695 bool IsIgnored;
5696
5697public:
5698 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
5699 bool IsIgnored)
5700 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
5701 if (!IsIgnored) {
5702 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
5703 /*Map*/ diag::Severity::Ignored, Loc);
5704 }
5705 }
5706 ~DiagsUninitializedSeveretyRAII() {
5707 if (!IsIgnored)
5708 Diags.popMappings(SavedLoc);
5709 }
5710};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00005711}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005712
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005713OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
5714 SourceLocation StartLoc,
5715 SourceLocation LParenLoc,
5716 SourceLocation EndLoc) {
5717 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005718 SmallVector<Expr *, 8> PrivateCopies;
5719 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005720 bool IsImplicitClause =
5721 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
5722 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
5723
Alexey Bataeved09d242014-05-28 05:53:51 +00005724 for (auto &RefExpr : VarList) {
5725 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
5726 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005727 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005728 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005729 PrivateCopies.push_back(nullptr);
5730 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005731 continue;
5732 }
5733
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005734 SourceLocation ELoc =
5735 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005736 // OpenMP [2.1, C/C++]
5737 // A list item is a variable name.
5738 // OpenMP [2.9.3.3, Restrictions, p.1]
5739 // A variable that is part of another variable (as an array or
5740 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00005741 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005742 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005743 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005744 continue;
5745 }
5746 Decl *D = DE->getDecl();
5747 VarDecl *VD = cast<VarDecl>(D);
5748
5749 QualType Type = VD->getType();
5750 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5751 // It will be analyzed later.
5752 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005753 PrivateCopies.push_back(nullptr);
5754 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005755 continue;
5756 }
5757
5758 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5759 // A variable that appears in a private clause must not have an incomplete
5760 // type or a reference type.
5761 if (RequireCompleteType(ELoc, Type,
5762 diag::err_omp_firstprivate_incomplete_type)) {
5763 continue;
5764 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00005765 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005766
5767 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
5768 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00005769 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005770 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005771 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005772
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005773 // If an implicit firstprivate variable found it was checked already.
5774 if (!IsImplicitClause) {
5775 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005776 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005777 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
5778 // A list item that specifies a given variable may not appear in more
5779 // than one clause on the same directive, except that a variable may be
5780 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005781 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00005782 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005783 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00005784 << getOpenMPClauseName(DVar.CKind)
5785 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005786 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005787 continue;
5788 }
5789
5790 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5791 // in a Construct]
5792 // Variables with the predetermined data-sharing attributes may not be
5793 // listed in data-sharing attributes clauses, except for the cases
5794 // listed below. For these exceptions only, listing a predetermined
5795 // variable in a data-sharing attribute clause is allowed and overrides
5796 // the variable's predetermined data-sharing attributes.
5797 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5798 // in a Construct, C/C++, p.2]
5799 // Variables with const-qualified type having no mutable member may be
5800 // listed in a firstprivate clause, even if they are static data members.
5801 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
5802 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
5803 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00005804 << getOpenMPClauseName(DVar.CKind)
5805 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005806 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005807 continue;
5808 }
5809
Alexey Bataevf29276e2014-06-18 04:14:57 +00005810 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005811 // OpenMP [2.9.3.4, Restrictions, p.2]
5812 // A list item that is private within a parallel region must not appear
5813 // in a firstprivate clause on a worksharing construct if any of the
5814 // worksharing regions arising from the worksharing construct ever bind
5815 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00005816 if (isOpenMPWorksharingDirective(CurrDir) &&
5817 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005818 DVar = DSAStack->getImplicitDSA(VD, true);
5819 if (DVar.CKind != OMPC_shared &&
5820 (isOpenMPParallelDirective(DVar.DKind) ||
5821 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00005822 Diag(ELoc, diag::err_omp_required_access)
5823 << getOpenMPClauseName(OMPC_firstprivate)
5824 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005825 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005826 continue;
5827 }
5828 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005829 // OpenMP [2.9.3.4, Restrictions, p.3]
5830 // A list item that appears in a reduction clause of a parallel construct
5831 // must not appear in a firstprivate clause on a worksharing or task
5832 // construct if any of the worksharing or task regions arising from the
5833 // worksharing or task construct ever bind to any of the parallel regions
5834 // arising from the parallel construct.
5835 // OpenMP [2.9.3.4, Restrictions, p.4]
5836 // A list item that appears in a reduction clause in worksharing
5837 // construct must not appear in a firstprivate clause in a task construct
5838 // encountered during execution of any of the worksharing regions arising
5839 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005840 if (CurrDir == OMPD_task) {
5841 DVar =
5842 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
5843 [](OpenMPDirectiveKind K) -> bool {
5844 return isOpenMPParallelDirective(K) ||
5845 isOpenMPWorksharingDirective(K);
5846 },
5847 false);
5848 if (DVar.CKind == OMPC_reduction &&
5849 (isOpenMPParallelDirective(DVar.DKind) ||
5850 isOpenMPWorksharingDirective(DVar.DKind))) {
5851 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
5852 << getOpenMPDirectiveName(DVar.DKind);
5853 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5854 continue;
5855 }
5856 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005857 }
5858
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005859 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00005860 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005861 DSAStack->getCurrentDirective() == OMPD_task) {
5862 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
5863 << getOpenMPClauseName(OMPC_firstprivate) << Type
5864 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
5865 bool IsDecl =
5866 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5867 Diag(VD->getLocation(),
5868 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5869 << VD;
5870 continue;
5871 }
5872
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005873 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00005874 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName(),
5875 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005876 // Generate helper private variable and initialize it with the value of the
5877 // original variable. The address of the original variable is replaced by
5878 // the address of the new private variable in the CodeGen. This new variable
5879 // is not added to IdResolver, so the code in the OpenMP region uses
5880 // original variable for proper diagnostics and variable capturing.
5881 Expr *VDInitRefExpr = nullptr;
5882 // For arrays generate initializer for single element and replace it by the
5883 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005884 if (Type->isArrayType()) {
5885 auto VDInit =
5886 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
5887 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005888 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005889 ElemType = ElemType.getUnqualifiedType();
5890 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
5891 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00005892 InitializedEntity Entity =
5893 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005894 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
5895
5896 InitializationSequence InitSeq(*this, Entity, Kind, Init);
5897 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
5898 if (Result.isInvalid())
5899 VDPrivate->setInvalidDecl();
5900 else
5901 VDPrivate->setInit(Result.getAs<Expr>());
5902 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00005903 auto *VDInit =
Alexey Bataev39f915b82015-05-08 10:41:21 +00005904 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005905 VDInitRefExpr =
5906 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00005907 AddInitializerToDecl(VDPrivate,
5908 DefaultLvalueConversion(VDInitRefExpr).get(),
5909 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005910 }
5911 if (VDPrivate->isInvalidDecl()) {
5912 if (IsImplicitClause) {
5913 Diag(DE->getExprLoc(),
5914 diag::note_omp_task_predetermined_firstprivate_here);
5915 }
5916 continue;
5917 }
5918 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005919 auto VDPrivateRefExpr = buildDeclRefExpr(
5920 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005921 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
5922 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005923 PrivateCopies.push_back(VDPrivateRefExpr);
5924 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005925 }
5926
Alexey Bataeved09d242014-05-28 05:53:51 +00005927 if (Vars.empty())
5928 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005929
5930 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005931 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005932}
5933
Alexander Musman1bb328c2014-06-04 13:06:39 +00005934OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
5935 SourceLocation StartLoc,
5936 SourceLocation LParenLoc,
5937 SourceLocation EndLoc) {
5938 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00005939 SmallVector<Expr *, 8> SrcExprs;
5940 SmallVector<Expr *, 8> DstExprs;
5941 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00005942 for (auto &RefExpr : VarList) {
5943 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
5944 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5945 // It will be analyzed later.
5946 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00005947 SrcExprs.push_back(nullptr);
5948 DstExprs.push_back(nullptr);
5949 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005950 continue;
5951 }
5952
5953 SourceLocation ELoc = RefExpr->getExprLoc();
5954 // OpenMP [2.1, C/C++]
5955 // A list item is a variable name.
5956 // OpenMP [2.14.3.5, Restrictions, p.1]
5957 // A variable that is part of another variable (as an array or structure
5958 // element) cannot appear in a lastprivate clause.
5959 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
5960 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5961 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5962 continue;
5963 }
5964 Decl *D = DE->getDecl();
5965 VarDecl *VD = cast<VarDecl>(D);
5966
5967 QualType Type = VD->getType();
5968 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5969 // It will be analyzed later.
5970 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00005971 SrcExprs.push_back(nullptr);
5972 DstExprs.push_back(nullptr);
5973 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005974 continue;
5975 }
5976
5977 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
5978 // A variable that appears in a lastprivate clause must not have an
5979 // incomplete type or a reference type.
5980 if (RequireCompleteType(ELoc, Type,
5981 diag::err_omp_lastprivate_incomplete_type)) {
5982 continue;
5983 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00005984 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00005985
5986 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
5987 // in a Construct]
5988 // Variables with the predetermined data-sharing attributes may not be
5989 // listed in data-sharing attributes clauses, except for the cases
5990 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005991 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005992 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
5993 DVar.CKind != OMPC_firstprivate &&
5994 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
5995 Diag(ELoc, diag::err_omp_wrong_dsa)
5996 << getOpenMPClauseName(DVar.CKind)
5997 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005998 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005999 continue;
6000 }
6001
Alexey Bataevf29276e2014-06-18 04:14:57 +00006002 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
6003 // OpenMP [2.14.3.5, Restrictions, p.2]
6004 // A list item that is private within a parallel region, or that appears in
6005 // the reduction clause of a parallel construct, must not appear in a
6006 // lastprivate clause on a worksharing construct if any of the corresponding
6007 // worksharing regions ever binds to any of the corresponding parallel
6008 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00006009 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00006010 if (isOpenMPWorksharingDirective(CurrDir) &&
6011 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006012 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006013 if (DVar.CKind != OMPC_shared) {
6014 Diag(ELoc, diag::err_omp_required_access)
6015 << getOpenMPClauseName(OMPC_lastprivate)
6016 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006017 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006018 continue;
6019 }
6020 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00006021 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00006022 // A variable of class type (or array thereof) that appears in a
6023 // lastprivate clause requires an accessible, unambiguous default
6024 // constructor for the class type, unless the list item is also specified
6025 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00006026 // A variable of class type (or array thereof) that appears in a
6027 // lastprivate clause requires an accessible, unambiguous copy assignment
6028 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00006029 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00006030 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006031 Type.getUnqualifiedType(), ".lastprivate.src",
6032 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006033 auto *PseudoSrcExpr = buildDeclRefExpr(
6034 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00006035 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006036 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst",
6037 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00006038 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006039 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00006040 // For arrays generate assignment operation for single element and replace
6041 // it by the original array element in CodeGen.
6042 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6043 PseudoDstExpr, PseudoSrcExpr);
6044 if (AssignmentOp.isInvalid())
6045 continue;
6046 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6047 /*DiscardedValue=*/true);
6048 if (AssignmentOp.isInvalid())
6049 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006050
Alexey Bataev39f915b82015-05-08 10:41:21 +00006051 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00006052 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006053 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00006054 SrcExprs.push_back(PseudoSrcExpr);
6055 DstExprs.push_back(PseudoDstExpr);
6056 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00006057 }
6058
6059 if (Vars.empty())
6060 return nullptr;
6061
6062 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00006063 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006064}
6065
Alexey Bataev758e55e2013-09-06 18:03:48 +00006066OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
6067 SourceLocation StartLoc,
6068 SourceLocation LParenLoc,
6069 SourceLocation EndLoc) {
6070 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00006071 for (auto &RefExpr : VarList) {
6072 assert(RefExpr && "NULL expr in OpenMP shared clause.");
6073 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00006074 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006075 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006076 continue;
6077 }
6078
Alexey Bataeved09d242014-05-28 05:53:51 +00006079 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00006080 // OpenMP [2.1, C/C++]
6081 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00006082 // OpenMP [2.14.3.2, Restrictions, p.1]
6083 // A variable that is part of another variable (as an array or structure
6084 // element) cannot appear in a shared unless it is a static data member
6085 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00006086 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006087 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006088 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00006089 continue;
6090 }
6091 Decl *D = DE->getDecl();
6092 VarDecl *VD = cast<VarDecl>(D);
6093
6094 QualType Type = VD->getType();
6095 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6096 // It will be analyzed later.
6097 Vars.push_back(DE);
6098 continue;
6099 }
6100
6101 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6102 // in a Construct]
6103 // Variables with the predetermined data-sharing attributes may not be
6104 // listed in data-sharing attributes clauses, except for the cases
6105 // listed below. For these exceptions only, listing a predetermined
6106 // variable in a data-sharing attribute clause is allowed and overrides
6107 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006108 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00006109 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
6110 DVar.RefExpr) {
6111 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6112 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006113 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006114 continue;
6115 }
6116
6117 DSAStack->addDSA(VD, DE, OMPC_shared);
6118 Vars.push_back(DE);
6119 }
6120
Alexey Bataeved09d242014-05-28 05:53:51 +00006121 if (Vars.empty())
6122 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006123
6124 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
6125}
6126
Alexey Bataevc5e02582014-06-16 07:08:35 +00006127namespace {
6128class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
6129 DSAStackTy *Stack;
6130
6131public:
6132 bool VisitDeclRefExpr(DeclRefExpr *E) {
6133 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006134 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006135 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
6136 return false;
6137 if (DVar.CKind != OMPC_unknown)
6138 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00006139 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006140 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006141 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00006142 return true;
6143 return false;
6144 }
6145 return false;
6146 }
6147 bool VisitStmt(Stmt *S) {
6148 for (auto Child : S->children()) {
6149 if (Child && Visit(Child))
6150 return true;
6151 }
6152 return false;
6153 }
Alexey Bataev23b69422014-06-18 07:08:49 +00006154 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00006155};
Alexey Bataev23b69422014-06-18 07:08:49 +00006156} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00006157
6158OMPClause *Sema::ActOnOpenMPReductionClause(
6159 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
6160 SourceLocation ColonLoc, SourceLocation EndLoc,
6161 CXXScopeSpec &ReductionIdScopeSpec,
6162 const DeclarationNameInfo &ReductionId) {
6163 // TODO: Allow scope specification search when 'declare reduction' is
6164 // supported.
6165 assert(ReductionIdScopeSpec.isEmpty() &&
6166 "No support for scoped reduction identifiers yet.");
6167
6168 auto DN = ReductionId.getName();
6169 auto OOK = DN.getCXXOverloadedOperator();
6170 BinaryOperatorKind BOK = BO_Comma;
6171
6172 // OpenMP [2.14.3.6, reduction clause]
6173 // C
6174 // reduction-identifier is either an identifier or one of the following
6175 // operators: +, -, *, &, |, ^, && and ||
6176 // C++
6177 // reduction-identifier is either an id-expression or one of the following
6178 // operators: +, -, *, &, |, ^, && and ||
6179 // FIXME: Only 'min' and 'max' identifiers are supported for now.
6180 switch (OOK) {
6181 case OO_Plus:
6182 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006183 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006184 break;
6185 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006186 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006187 break;
6188 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006189 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006190 break;
6191 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006192 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006193 break;
6194 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006195 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006196 break;
6197 case OO_AmpAmp:
6198 BOK = BO_LAnd;
6199 break;
6200 case OO_PipePipe:
6201 BOK = BO_LOr;
6202 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006203 case OO_New:
6204 case OO_Delete:
6205 case OO_Array_New:
6206 case OO_Array_Delete:
6207 case OO_Slash:
6208 case OO_Percent:
6209 case OO_Tilde:
6210 case OO_Exclaim:
6211 case OO_Equal:
6212 case OO_Less:
6213 case OO_Greater:
6214 case OO_LessEqual:
6215 case OO_GreaterEqual:
6216 case OO_PlusEqual:
6217 case OO_MinusEqual:
6218 case OO_StarEqual:
6219 case OO_SlashEqual:
6220 case OO_PercentEqual:
6221 case OO_CaretEqual:
6222 case OO_AmpEqual:
6223 case OO_PipeEqual:
6224 case OO_LessLess:
6225 case OO_GreaterGreater:
6226 case OO_LessLessEqual:
6227 case OO_GreaterGreaterEqual:
6228 case OO_EqualEqual:
6229 case OO_ExclaimEqual:
6230 case OO_PlusPlus:
6231 case OO_MinusMinus:
6232 case OO_Comma:
6233 case OO_ArrowStar:
6234 case OO_Arrow:
6235 case OO_Call:
6236 case OO_Subscript:
6237 case OO_Conditional:
6238 case NUM_OVERLOADED_OPERATORS:
6239 llvm_unreachable("Unexpected reduction identifier");
6240 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006241 if (auto II = DN.getAsIdentifierInfo()) {
6242 if (II->isStr("max"))
6243 BOK = BO_GT;
6244 else if (II->isStr("min"))
6245 BOK = BO_LT;
6246 }
6247 break;
6248 }
6249 SourceRange ReductionIdRange;
6250 if (ReductionIdScopeSpec.isValid()) {
6251 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
6252 }
6253 ReductionIdRange.setEnd(ReductionId.getEndLoc());
6254 if (BOK == BO_Comma) {
6255 // Not allowed reduction identifier is found.
6256 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
6257 << ReductionIdRange;
6258 return nullptr;
6259 }
6260
6261 SmallVector<Expr *, 8> Vars;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006262 SmallVector<Expr *, 8> LHSs;
6263 SmallVector<Expr *, 8> RHSs;
6264 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006265 for (auto RefExpr : VarList) {
6266 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
6267 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6268 // It will be analyzed later.
6269 Vars.push_back(RefExpr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006270 LHSs.push_back(nullptr);
6271 RHSs.push_back(nullptr);
6272 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006273 continue;
6274 }
6275
6276 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
6277 RefExpr->isInstantiationDependent() ||
6278 RefExpr->containsUnexpandedParameterPack()) {
6279 // It will be analyzed later.
6280 Vars.push_back(RefExpr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006281 LHSs.push_back(nullptr);
6282 RHSs.push_back(nullptr);
6283 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006284 continue;
6285 }
6286
6287 auto ELoc = RefExpr->getExprLoc();
6288 auto ERange = RefExpr->getSourceRange();
6289 // OpenMP [2.1, C/C++]
6290 // A list item is a variable or array section, subject to the restrictions
6291 // specified in Section 2.4 on page 42 and in each of the sections
6292 // describing clauses and directives for which a list appears.
6293 // OpenMP [2.14.3.3, Restrictions, p.1]
6294 // A variable that is part of another variable (as an array or
6295 // structure element) cannot appear in a private clause.
6296 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
6297 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6298 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
6299 continue;
6300 }
6301 auto D = DE->getDecl();
6302 auto VD = cast<VarDecl>(D);
6303 auto Type = VD->getType();
6304 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6305 // A variable that appears in a private clause must not have an incomplete
6306 // type or a reference type.
6307 if (RequireCompleteType(ELoc, Type,
6308 diag::err_omp_reduction_incomplete_type))
6309 continue;
6310 // OpenMP [2.14.3.6, reduction clause, Restrictions]
6311 // Arrays may not appear in a reduction clause.
6312 if (Type.getNonReferenceType()->isArrayType()) {
6313 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
6314 bool IsDecl =
6315 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6316 Diag(VD->getLocation(),
6317 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6318 << VD;
6319 continue;
6320 }
6321 // OpenMP [2.14.3.6, reduction clause, Restrictions]
6322 // A list item that appears in a reduction clause must not be
6323 // const-qualified.
6324 if (Type.getNonReferenceType().isConstant(Context)) {
6325 Diag(ELoc, diag::err_omp_const_variable)
6326 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
6327 bool IsDecl =
6328 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6329 Diag(VD->getLocation(),
6330 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6331 << VD;
6332 continue;
6333 }
6334 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
6335 // If a list-item is a reference type then it must bind to the same object
6336 // for all threads of the team.
6337 VarDecl *VDDef = VD->getDefinition();
6338 if (Type->isReferenceType() && VDDef) {
6339 DSARefChecker Check(DSAStack);
6340 if (Check.Visit(VDDef->getInit())) {
6341 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
6342 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
6343 continue;
6344 }
6345 }
6346 // OpenMP [2.14.3.6, reduction clause, Restrictions]
6347 // The type of a list item that appears in a reduction clause must be valid
6348 // for the reduction-identifier. For a max or min reduction in C, the type
6349 // of the list item must be an allowed arithmetic data type: char, int,
6350 // float, double, or _Bool, possibly modified with long, short, signed, or
6351 // unsigned. For a max or min reduction in C++, the type of the list item
6352 // must be an allowed arithmetic data type: char, wchar_t, int, float,
6353 // double, or bool, possibly modified with long, short, signed, or unsigned.
6354 if ((BOK == BO_GT || BOK == BO_LT) &&
6355 !(Type->isScalarType() ||
6356 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
6357 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
6358 << getLangOpts().CPlusPlus;
6359 bool IsDecl =
6360 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6361 Diag(VD->getLocation(),
6362 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6363 << VD;
6364 continue;
6365 }
6366 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
6367 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
6368 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
6369 bool IsDecl =
6370 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6371 Diag(VD->getLocation(),
6372 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6373 << VD;
6374 continue;
6375 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00006376 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
6377 // in a Construct]
6378 // Variables with the predetermined data-sharing attributes may not be
6379 // listed in data-sharing attributes clauses, except for the cases
6380 // listed below. For these exceptions only, listing a predetermined
6381 // variable in a data-sharing attribute clause is allowed and overrides
6382 // the variable's predetermined data-sharing attributes.
6383 // OpenMP [2.14.3.6, Restrictions, p.3]
6384 // Any number of reduction clauses can be specified on the directive,
6385 // but a list item can appear only once in the reduction clauses for that
6386 // directive.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006387 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006388 if (DVar.CKind == OMPC_reduction) {
6389 Diag(ELoc, diag::err_omp_once_referenced)
6390 << getOpenMPClauseName(OMPC_reduction);
6391 if (DVar.RefExpr) {
6392 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
6393 }
6394 } else if (DVar.CKind != OMPC_unknown) {
6395 Diag(ELoc, diag::err_omp_wrong_dsa)
6396 << getOpenMPClauseName(DVar.CKind)
6397 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006398 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006399 continue;
6400 }
6401
6402 // OpenMP [2.14.3.6, Restrictions, p.1]
6403 // A list item that appears in a reduction clause of a worksharing
6404 // construct must be shared in the parallel regions to which any of the
6405 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00006406 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00006407 if (isOpenMPWorksharingDirective(CurrDir) &&
6408 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006409 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006410 if (DVar.CKind != OMPC_shared) {
6411 Diag(ELoc, diag::err_omp_required_access)
6412 << getOpenMPClauseName(OMPC_reduction)
6413 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006414 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006415 continue;
6416 }
6417 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006418 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006419 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
6420 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
6421 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName(),
6422 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006423 // Add initializer for private variable.
6424 Expr *Init = nullptr;
6425 switch (BOK) {
6426 case BO_Add:
6427 case BO_Xor:
6428 case BO_Or:
6429 case BO_LOr:
6430 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
6431 if (Type->isScalarType() || Type->isAnyComplexType()) {
6432 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00006433 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006434 break;
6435 case BO_Mul:
6436 case BO_LAnd:
6437 if (Type->isScalarType() || Type->isAnyComplexType()) {
6438 // '*' and '&&' reduction ops - initializer is '1'.
6439 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
6440 }
6441 break;
6442 case BO_And: {
6443 // '&' reduction op - initializer is '~0'.
6444 QualType OrigType = Type;
6445 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
6446 Type = ComplexTy->getElementType();
6447 }
6448 if (Type->isRealFloatingType()) {
6449 llvm::APFloat InitValue =
6450 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
6451 /*isIEEE=*/true);
6452 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
6453 Type, ELoc);
6454 } else if (Type->isScalarType()) {
6455 auto Size = Context.getTypeSize(Type);
6456 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
6457 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
6458 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
6459 }
6460 if (Init && OrigType->isAnyComplexType()) {
6461 // Init = 0xFFFF + 0xFFFFi;
6462 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
6463 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
6464 }
6465 Type = OrigType;
6466 break;
6467 }
6468 case BO_LT:
6469 case BO_GT: {
6470 // 'min' reduction op - initializer is 'Largest representable number in
6471 // the reduction list item type'.
6472 // 'max' reduction op - initializer is 'Least representable number in
6473 // the reduction list item type'.
6474 if (Type->isIntegerType() || Type->isPointerType()) {
6475 bool IsSigned = Type->hasSignedIntegerRepresentation();
6476 auto Size = Context.getTypeSize(Type);
6477 QualType IntTy =
6478 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
6479 llvm::APInt InitValue =
6480 (BOK != BO_LT)
6481 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
6482 : llvm::APInt::getMinValue(Size)
6483 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
6484 : llvm::APInt::getMaxValue(Size);
6485 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
6486 if (Type->isPointerType()) {
6487 // Cast to pointer type.
6488 auto CastExpr = BuildCStyleCastExpr(
6489 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
6490 SourceLocation(), Init);
6491 if (CastExpr.isInvalid())
6492 continue;
6493 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00006494 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006495 } else if (Type->isRealFloatingType()) {
6496 llvm::APFloat InitValue = llvm::APFloat::getLargest(
6497 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
6498 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
6499 Type, ELoc);
6500 }
6501 break;
6502 }
6503 case BO_PtrMemD:
6504 case BO_PtrMemI:
6505 case BO_MulAssign:
6506 case BO_Div:
6507 case BO_Rem:
6508 case BO_Sub:
6509 case BO_Shl:
6510 case BO_Shr:
6511 case BO_LE:
6512 case BO_GE:
6513 case BO_EQ:
6514 case BO_NE:
6515 case BO_AndAssign:
6516 case BO_XorAssign:
6517 case BO_OrAssign:
6518 case BO_Assign:
6519 case BO_AddAssign:
6520 case BO_SubAssign:
6521 case BO_DivAssign:
6522 case BO_RemAssign:
6523 case BO_ShlAssign:
6524 case BO_ShrAssign:
6525 case BO_Comma:
6526 llvm_unreachable("Unexpected reduction operation");
6527 }
6528 if (Init) {
6529 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
6530 /*TypeMayContainAuto=*/false);
6531 } else {
6532 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
6533 }
6534 if (!RHSVD->hasInit()) {
6535 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
6536 << ReductionIdRange;
6537 bool IsDecl =
6538 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6539 Diag(VD->getLocation(),
6540 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6541 << VD;
6542 continue;
6543 }
Alexey Bataev39f915b82015-05-08 10:41:21 +00006544 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
6545 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006546 ExprResult ReductionOp =
6547 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
6548 LHSDRE, RHSDRE);
6549 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00006550 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006551 ReductionOp =
6552 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
6553 BO_Assign, LHSDRE, ReductionOp.get());
6554 } else {
6555 auto *ConditionalOp = new (Context) ConditionalOperator(
6556 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
6557 RHSDRE, Type, VK_LValue, OK_Ordinary);
6558 ReductionOp =
6559 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
6560 BO_Assign, LHSDRE, ConditionalOp);
6561 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006562 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00006563 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006564 if (ReductionOp.isInvalid())
6565 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006566
6567 DSAStack->addDSA(VD, DE, OMPC_reduction);
6568 Vars.push_back(DE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006569 LHSs.push_back(LHSDRE);
6570 RHSs.push_back(RHSDRE);
6571 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00006572 }
6573
6574 if (Vars.empty())
6575 return nullptr;
6576
6577 return OMPReductionClause::Create(
6578 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006579 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, LHSs,
6580 RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006581}
6582
Alexey Bataev182227b2015-08-20 10:54:39 +00006583OMPClause *Sema::ActOnOpenMPLinearClause(
6584 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
6585 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
6586 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00006587 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006588 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00006589 SmallVector<Expr *, 8> Inits;
Alexey Bataev182227b2015-08-20 10:54:39 +00006590 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
6591 LinKind == OMPC_LINEAR_unknown) {
6592 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
6593 LinKind = OMPC_LINEAR_val;
6594 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006595 for (auto &RefExpr : VarList) {
6596 assert(RefExpr && "NULL expr in OpenMP linear clause.");
6597 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00006598 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006599 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006600 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00006601 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006602 continue;
6603 }
6604
6605 // OpenMP [2.14.3.7, linear clause]
6606 // A list item that appears in a linear clause is subject to the private
6607 // clause semantics described in Section 2.14.3.3 on page 159 except as
6608 // noted. In addition, the value of the new list item on each iteration
6609 // of the associated loop(s) corresponds to the value of the original
6610 // list item before entering the construct plus the logical number of
6611 // the iteration times linear-step.
6612
Alexey Bataeved09d242014-05-28 05:53:51 +00006613 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00006614 // OpenMP [2.1, C/C++]
6615 // A list item is a variable name.
6616 // OpenMP [2.14.3.3, Restrictions, p.1]
6617 // A variable that is part of another variable (as an array or
6618 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006619 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006620 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006621 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00006622 continue;
6623 }
6624
6625 VarDecl *VD = cast<VarDecl>(DE->getDecl());
6626
6627 // OpenMP [2.14.3.7, linear clause]
6628 // A list-item cannot appear in more than one linear clause.
6629 // A list-item that appears in a linear clause cannot appear in any
6630 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006631 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00006632 if (DVar.RefExpr) {
6633 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6634 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006635 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00006636 continue;
6637 }
6638
6639 QualType QType = VD->getType();
6640 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
6641 // It will be analyzed later.
6642 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006643 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00006644 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006645 continue;
6646 }
6647
6648 // A variable must not have an incomplete type or a reference type.
6649 if (RequireCompleteType(ELoc, QType,
6650 diag::err_omp_linear_incomplete_type)) {
6651 continue;
6652 }
Alexey Bataev1185e192015-08-20 12:15:57 +00006653 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
6654 !QType->isReferenceType()) {
6655 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
6656 << QType << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
6657 continue;
6658 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006659 QType = QType.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00006660
6661 // A list item must not be const-qualified.
6662 if (QType.isConstant(Context)) {
6663 Diag(ELoc, diag::err_omp_const_variable)
6664 << getOpenMPClauseName(OMPC_linear);
6665 bool IsDecl =
6666 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6667 Diag(VD->getLocation(),
6668 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6669 << VD;
6670 continue;
6671 }
6672
6673 // A list item must be of integral or pointer type.
6674 QType = QType.getUnqualifiedType().getCanonicalType();
6675 const Type *Ty = QType.getTypePtrOrNull();
6676 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
6677 !Ty->isPointerType())) {
6678 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
6679 bool IsDecl =
6680 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6681 Diag(VD->getLocation(),
6682 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6683 << VD;
6684 continue;
6685 }
6686
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006687 // Build private copy of original var.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006688 auto *Private = buildVarDecl(*this, ELoc, QType, VD->getName(),
6689 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006690 auto *PrivateRef = buildDeclRefExpr(
6691 *this, Private, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman3276a272015-03-21 10:12:56 +00006692 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006693 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00006694 Expr *InitExpr;
6695 if (LinKind == OMPC_LINEAR_uval)
6696 InitExpr = VD->getInit();
6697 else
6698 InitExpr = DE;
6699 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexander Musman3276a272015-03-21 10:12:56 +00006700 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006701 auto InitRef = buildDeclRefExpr(
6702 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00006703 DSAStack->addDSA(VD, DE, OMPC_linear);
6704 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006705 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00006706 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00006707 }
6708
6709 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006710 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00006711
6712 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00006713 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00006714 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
6715 !Step->isInstantiationDependent() &&
6716 !Step->containsUnexpandedParameterPack()) {
6717 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006718 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00006719 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006720 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006721 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00006722
Alexander Musman3276a272015-03-21 10:12:56 +00006723 // Build var to save the step value.
6724 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006725 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00006726 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006727 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00006728 ExprResult CalcStep =
6729 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006730 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00006731
Alexander Musman8dba6642014-04-22 13:09:42 +00006732 // Warn about zero linear step (it would be probably better specified as
6733 // making corresponding variables 'const').
6734 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00006735 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
6736 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00006737 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
6738 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00006739 if (!IsConstant && CalcStep.isUsable()) {
6740 // Calculate the step beforehand instead of doing this on each iteration.
6741 // (This is not used if the number of iterations may be kfold-ed).
6742 CalcStepExpr = CalcStep.get();
6743 }
Alexander Musman8dba6642014-04-22 13:09:42 +00006744 }
6745
Alexey Bataev182227b2015-08-20 10:54:39 +00006746 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
6747 ColonLoc, EndLoc, Vars, Privates, Inits,
6748 StepExpr, CalcStepExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00006749}
6750
6751static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
6752 Expr *NumIterations, Sema &SemaRef,
6753 Scope *S) {
6754 // Walk the vars and build update/final expressions for the CodeGen.
6755 SmallVector<Expr *, 8> Updates;
6756 SmallVector<Expr *, 8> Finals;
6757 Expr *Step = Clause.getStep();
6758 Expr *CalcStep = Clause.getCalcStep();
6759 // OpenMP [2.14.3.7, linear clause]
6760 // If linear-step is not specified it is assumed to be 1.
6761 if (Step == nullptr)
6762 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
6763 else if (CalcStep)
6764 Step = cast<BinaryOperator>(CalcStep)->getLHS();
6765 bool HasErrors = false;
6766 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006767 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00006768 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00006769 for (auto &RefExpr : Clause.varlists()) {
6770 Expr *InitExpr = *CurInit;
6771
6772 // Build privatized reference to the current linear var.
6773 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00006774 Expr *CapturedRef;
6775 if (LinKind == OMPC_LINEAR_uval)
6776 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
6777 else
6778 CapturedRef =
6779 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
6780 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
6781 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00006782
6783 // Build update: Var = InitExpr + IV * Step
6784 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006785 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00006786 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006787 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
6788 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00006789
6790 // Build final: Var = InitExpr + NumIterations * Step
6791 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006792 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00006793 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006794 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
6795 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00006796 if (!Update.isUsable() || !Final.isUsable()) {
6797 Updates.push_back(nullptr);
6798 Finals.push_back(nullptr);
6799 HasErrors = true;
6800 } else {
6801 Updates.push_back(Update.get());
6802 Finals.push_back(Final.get());
6803 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006804 ++CurInit, ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00006805 }
6806 Clause.setUpdates(Updates);
6807 Clause.setFinals(Finals);
6808 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00006809}
6810
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006811OMPClause *Sema::ActOnOpenMPAlignedClause(
6812 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
6813 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
6814
6815 SmallVector<Expr *, 8> Vars;
6816 for (auto &RefExpr : VarList) {
6817 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
6818 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6819 // It will be analyzed later.
6820 Vars.push_back(RefExpr);
6821 continue;
6822 }
6823
6824 SourceLocation ELoc = RefExpr->getExprLoc();
6825 // OpenMP [2.1, C/C++]
6826 // A list item is a variable name.
6827 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
6828 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6829 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6830 continue;
6831 }
6832
6833 VarDecl *VD = cast<VarDecl>(DE->getDecl());
6834
6835 // OpenMP [2.8.1, simd construct, Restrictions]
6836 // The type of list items appearing in the aligned clause must be
6837 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006838 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006839 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006840 const Type *Ty = QType.getTypePtrOrNull();
6841 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
6842 !Ty->isPointerType())) {
6843 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
6844 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
6845 bool IsDecl =
6846 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6847 Diag(VD->getLocation(),
6848 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6849 << VD;
6850 continue;
6851 }
6852
6853 // OpenMP [2.8.1, simd construct, Restrictions]
6854 // A list-item cannot appear in more than one aligned clause.
6855 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
6856 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
6857 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
6858 << getOpenMPClauseName(OMPC_aligned);
6859 continue;
6860 }
6861
6862 Vars.push_back(DE);
6863 }
6864
6865 // OpenMP [2.8.1, simd construct, Description]
6866 // The parameter of the aligned clause, alignment, must be a constant
6867 // positive integer expression.
6868 // If no optional parameter is specified, implementation-defined default
6869 // alignments for SIMD instructions on the target platforms are assumed.
6870 if (Alignment != nullptr) {
6871 ExprResult AlignResult =
6872 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
6873 if (AlignResult.isInvalid())
6874 return nullptr;
6875 Alignment = AlignResult.get();
6876 }
6877 if (Vars.empty())
6878 return nullptr;
6879
6880 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
6881 EndLoc, Vars, Alignment);
6882}
6883
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006884OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
6885 SourceLocation StartLoc,
6886 SourceLocation LParenLoc,
6887 SourceLocation EndLoc) {
6888 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006889 SmallVector<Expr *, 8> SrcExprs;
6890 SmallVector<Expr *, 8> DstExprs;
6891 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00006892 for (auto &RefExpr : VarList) {
6893 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
6894 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006895 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006896 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006897 SrcExprs.push_back(nullptr);
6898 DstExprs.push_back(nullptr);
6899 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006900 continue;
6901 }
6902
Alexey Bataeved09d242014-05-28 05:53:51 +00006903 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006904 // OpenMP [2.1, C/C++]
6905 // A list item is a variable name.
6906 // OpenMP [2.14.4.1, Restrictions, p.1]
6907 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00006908 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006909 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006910 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006911 continue;
6912 }
6913
6914 Decl *D = DE->getDecl();
6915 VarDecl *VD = cast<VarDecl>(D);
6916
6917 QualType Type = VD->getType();
6918 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6919 // It will be analyzed later.
6920 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006921 SrcExprs.push_back(nullptr);
6922 DstExprs.push_back(nullptr);
6923 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006924 continue;
6925 }
6926
6927 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
6928 // A list item that appears in a copyin clause must be threadprivate.
6929 if (!DSAStack->isThreadPrivate(VD)) {
6930 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00006931 << getOpenMPClauseName(OMPC_copyin)
6932 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006933 continue;
6934 }
6935
6936 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
6937 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00006938 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006939 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006940 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006941 auto *SrcVD =
6942 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
6943 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006944 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006945 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
6946 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006947 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
6948 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006949 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006950 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006951 // For arrays generate assignment operation for single element and replace
6952 // it by the original array element in CodeGen.
6953 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6954 PseudoDstExpr, PseudoSrcExpr);
6955 if (AssignmentOp.isInvalid())
6956 continue;
6957 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6958 /*DiscardedValue=*/true);
6959 if (AssignmentOp.isInvalid())
6960 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006961
6962 DSAStack->addDSA(VD, DE, OMPC_copyin);
6963 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006964 SrcExprs.push_back(PseudoSrcExpr);
6965 DstExprs.push_back(PseudoDstExpr);
6966 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006967 }
6968
Alexey Bataeved09d242014-05-28 05:53:51 +00006969 if (Vars.empty())
6970 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006971
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006972 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
6973 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006974}
6975
Alexey Bataevbae9a792014-06-27 10:37:06 +00006976OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
6977 SourceLocation StartLoc,
6978 SourceLocation LParenLoc,
6979 SourceLocation EndLoc) {
6980 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00006981 SmallVector<Expr *, 8> SrcExprs;
6982 SmallVector<Expr *, 8> DstExprs;
6983 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006984 for (auto &RefExpr : VarList) {
6985 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
6986 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6987 // It will be analyzed later.
6988 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006989 SrcExprs.push_back(nullptr);
6990 DstExprs.push_back(nullptr);
6991 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006992 continue;
6993 }
6994
6995 SourceLocation ELoc = RefExpr->getExprLoc();
6996 // OpenMP [2.1, C/C++]
6997 // A list item is a variable name.
6998 // OpenMP [2.14.4.1, Restrictions, p.1]
6999 // A list item that appears in a copyin clause must be threadprivate.
7000 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
7001 if (!DE || !isa<VarDecl>(DE->getDecl())) {
7002 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
7003 continue;
7004 }
7005
7006 Decl *D = DE->getDecl();
7007 VarDecl *VD = cast<VarDecl>(D);
7008
7009 QualType Type = VD->getType();
7010 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7011 // It will be analyzed later.
7012 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007013 SrcExprs.push_back(nullptr);
7014 DstExprs.push_back(nullptr);
7015 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007016 continue;
7017 }
7018
7019 // OpenMP [2.14.4.2, Restrictions, p.2]
7020 // A list item that appears in a copyprivate clause may not appear in a
7021 // private or firstprivate clause on the single construct.
7022 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007023 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007024 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
7025 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00007026 Diag(ELoc, diag::err_omp_wrong_dsa)
7027 << getOpenMPClauseName(DVar.CKind)
7028 << getOpenMPClauseName(OMPC_copyprivate);
7029 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7030 continue;
7031 }
7032
7033 // OpenMP [2.11.4.2, Restrictions, p.1]
7034 // All list items that appear in a copyprivate clause must be either
7035 // threadprivate or private in the enclosing context.
7036 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007037 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007038 if (DVar.CKind == OMPC_shared) {
7039 Diag(ELoc, diag::err_omp_required_access)
7040 << getOpenMPClauseName(OMPC_copyprivate)
7041 << "threadprivate or private in the enclosing context";
7042 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7043 continue;
7044 }
7045 }
7046 }
7047
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007048 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007049 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007050 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007051 << getOpenMPClauseName(OMPC_copyprivate) << Type
7052 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007053 bool IsDecl =
7054 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7055 Diag(VD->getLocation(),
7056 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7057 << VD;
7058 continue;
7059 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007060
Alexey Bataevbae9a792014-06-27 10:37:06 +00007061 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
7062 // A variable of class type (or array thereof) that appears in a
7063 // copyin clause requires an accessible, unambiguous copy assignment
7064 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007065 Type = Context.getBaseElementType(Type.getNonReferenceType())
7066 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00007067 auto *SrcVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007068 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src",
7069 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00007070 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007071 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00007072 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007073 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst",
7074 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00007075 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007076 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00007077 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7078 PseudoDstExpr, PseudoSrcExpr);
7079 if (AssignmentOp.isInvalid())
7080 continue;
7081 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7082 /*DiscardedValue=*/true);
7083 if (AssignmentOp.isInvalid())
7084 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007085
7086 // No need to mark vars as copyprivate, they are already threadprivate or
7087 // implicitly private.
7088 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007089 SrcExprs.push_back(PseudoSrcExpr);
7090 DstExprs.push_back(PseudoDstExpr);
7091 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00007092 }
7093
7094 if (Vars.empty())
7095 return nullptr;
7096
Alexey Bataeva63048e2015-03-23 06:18:07 +00007097 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
7098 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007099}
7100
Alexey Bataev6125da92014-07-21 11:26:11 +00007101OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
7102 SourceLocation StartLoc,
7103 SourceLocation LParenLoc,
7104 SourceLocation EndLoc) {
7105 if (VarList.empty())
7106 return nullptr;
7107
7108 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
7109}
Alexey Bataevdea47612014-07-23 07:46:59 +00007110
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007111OMPClause *
7112Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
7113 SourceLocation DepLoc, SourceLocation ColonLoc,
7114 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
7115 SourceLocation LParenLoc, SourceLocation EndLoc) {
7116 if (DepKind == OMPC_DEPEND_unknown) {
7117 std::string Values;
7118 std::string Sep(", ");
7119 for (unsigned i = 0; i < OMPC_DEPEND_unknown; ++i) {
7120 Values += "'";
7121 Values += getOpenMPSimpleClauseTypeName(OMPC_depend, i);
7122 Values += "'";
7123 switch (i) {
7124 case OMPC_DEPEND_unknown - 2:
7125 Values += " or ";
7126 break;
7127 case OMPC_DEPEND_unknown - 1:
7128 break;
7129 default:
7130 Values += Sep;
7131 break;
7132 }
7133 }
7134 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
7135 << Values << getOpenMPClauseName(OMPC_depend);
7136 return nullptr;
7137 }
7138 SmallVector<Expr *, 8> Vars;
7139 for (auto &RefExpr : VarList) {
7140 assert(RefExpr && "NULL expr in OpenMP shared clause.");
7141 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7142 // It will be analyzed later.
7143 Vars.push_back(RefExpr);
7144 continue;
7145 }
7146
7147 SourceLocation ELoc = RefExpr->getExprLoc();
7148 // OpenMP [2.11.1.1, Restrictions, p.3]
7149 // A variable that is part of another variable (such as a field of a
7150 // structure) but is not an array element or an array section cannot appear
7151 // in a depend clause.
7152 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
Alexey Bataev1a3320e2015-08-25 14:24:04 +00007153 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
7154 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
7155 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
7156 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
7157 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007158 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
7159 !ASE->getBase()->getType()->isArrayType())) {
7160 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
7161 << RefExpr->getSourceRange();
7162 continue;
7163 }
7164
7165 Vars.push_back(RefExpr->IgnoreParenImpCasts());
7166 }
7167
7168 if (Vars.empty())
7169 return nullptr;
7170
7171 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
7172 DepLoc, ColonLoc, Vars);
7173}
Michael Wonge710d542015-08-07 16:16:36 +00007174
7175OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
7176 SourceLocation LParenLoc,
7177 SourceLocation EndLoc) {
7178 Expr *ValExpr = Device;
7179 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
7180 !ValExpr->isInstantiationDependent()) {
7181 SourceLocation Loc = ValExpr->getExprLoc();
7182 ExprResult Value = PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
7183 if (Value.isInvalid())
7184 return nullptr;
7185
7186 // OpenMP [2.9.1, Restrictions]
7187 // The device expression must evaluate to a non-negative integer value.
7188 llvm::APSInt Result;
7189 if (Value.get()->isIntegerConstantExpr(Result, Context) &&
7190 Result.isSigned() && !Result.isStrictlyPositive()) {
7191 Diag(Loc, diag::err_omp_negative_expression_in_clause)
7192 << "device" << ValExpr->getSourceRange();
7193 return nullptr;
7194 }
7195 }
7196
7197 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
7198}